xref: /illumos-gate/usr/src/cmd/zdb/zdb.c (revision f0a05239)
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.
24af1d63abSPaul Dagnelie  * Copyright (c) 2011, 2019 by Delphix. All rights reserved.
25c3d26abcSMatthew Ahrens  * Copyright (c) 2014 Integros [integros.com]
26ed61ec1dSYuri Pankov  * Copyright 2017 Nexenta Systems, Inc.
27e0f1c0afSOlaf Faaland  * Copyright (c) 2017, 2018 Lawrence Livermore National Security, LLC.
28f06dce2cSAndrew Stormont  * Copyright 2017 RackTop Systems.
29fa9e4066Sahrens  */
30fa9e4066Sahrens 
31fa9e4066Sahrens #include <stdio.h>
32490d05b9SMatthew Ahrens #include <unistd.h>
33004388ebScasper #include <stdio_ext.h>
34fa9e4066Sahrens #include <stdlib.h>
3544cd46caSbillm #include <ctype.h>
36fa9e4066Sahrens #include <sys/zfs_context.h>
37fa9e4066Sahrens #include <sys/spa.h>
38fa9e4066Sahrens #include <sys/spa_impl.h>
39fa9e4066Sahrens #include <sys/dmu.h>
40fa9e4066Sahrens #include <sys/zap.h>
41fa9e4066Sahrens #include <sys/fs/zfs.h>
42fa9e4066Sahrens #include <sys/zfs_znode.h>
430a586ceaSMark Shellenbaum #include <sys/zfs_sa.h>
440a586ceaSMark Shellenbaum #include <sys/sa.h>
450a586ceaSMark Shellenbaum #include <sys/sa_impl.h>
46fa9e4066Sahrens #include <sys/vdev.h>
47fa9e4066Sahrens #include <sys/vdev_impl.h>
48fa9e4066Sahrens #include <sys/metaslab_impl.h>
49fa9e4066Sahrens #include <sys/dmu_objset.h>
50fa9e4066Sahrens #include <sys/dsl_dir.h>
51fa9e4066Sahrens #include <sys/dsl_dataset.h>
52fa9e4066Sahrens #include <sys/dsl_pool.h>
53fa9e4066Sahrens #include <sys/dbuf.h>
54fa9e4066Sahrens #include <sys/zil.h>
55fa9e4066Sahrens #include <sys/zil_impl.h>
56fa9e4066Sahrens #include <sys/stat.h>
57fa9e4066Sahrens #include <sys/resource.h>
58fa9e4066Sahrens #include <sys/dmu_traverse.h>
59fa9e4066Sahrens #include <sys/zio_checksum.h>
60fa9e4066Sahrens #include <sys/zio_compress.h>
61*f0a05239SGeorge Amanakis #include <zfs_fletcher.h>
62e0d35c44Smarks #include <sys/zfs_fuid.h>
6388b7b0f2SMatthew Ahrens #include <sys/arc.h>
64*f0a05239SGeorge Amanakis #include <sys/arc_impl.h>
65b24ab676SJeff Bonwick #include <sys/ddt.h>
66ad135b5dSChristopher Siden #include <sys/zfeature.h>
67770499e1SDan Kimmel #include <sys/abd.h>
684923c69fSMatthew Ahrens #include <sys/blkptr.h>
6917fb938fSMatthew Ahrens #include <sys/dsl_scan.h>
70eb633035STom Caputi #include <sys/dsl_crypt.h>
714445fffbSMatthew Ahrens #include <zfs_comutil.h>
720a055120SJason King #include <libcmdutils.h>
73de6628f0Sck #undef verify
74de6628f0Sck #include <libzfs.h>
75fa9e4066Sahrens 
76d8ab6e12SDon Brady #include <libnvpair.h>
77d8ab6e12SDon Brady #include <libzutil.h>
78d8ab6e12SDon Brady 
793f7978d0SAlan Somers #include "zdb.h"
803f7978d0SAlan Somers 
81e690fb27SChristopher Siden #define	ZDB_COMPRESS_NAME(idx) ((idx) < ZIO_COMPRESS_FUNCTIONS ?	\
82e690fb27SChristopher Siden 	zio_compress_table[(idx)].ci_name : "UNKNOWN")
83e690fb27SChristopher Siden #define	ZDB_CHECKSUM_NAME(idx) ((idx) < ZIO_CHECKSUM_FUNCTIONS ?	\
84e690fb27SChristopher Siden 	zio_checksum_table[(idx)].ci_name : "UNKNOWN")
85e690fb27SChristopher Siden #define	ZDB_OT_NAME(idx) ((idx) < DMU_OT_NUMTYPES ?	\
86e690fb27SChristopher Siden 	dmu_ot[(idx)].ot_name : DMU_OT_IS_VALID(idx) ?	\
87e690fb27SChristopher Siden 	dmu_ot_byteswap[DMU_OT_BYTESWAP(idx)].ob_name : "UNKNOWN")
88e690fb27SChristopher Siden #define	ZDB_OT_TYPE(idx) ((idx) < DMU_OT_NUMTYPES ? (idx) :		\
895cabbc6bSPrashanth Sreenivasa 	(idx) == DMU_OTN_ZAP_DATA || (idx) == DMU_OTN_ZAP_METADATA ?	\
905cabbc6bSPrashanth Sreenivasa 	DMU_OT_ZAP_OTHER : \
915cabbc6bSPrashanth Sreenivasa 	(idx) == DMU_OTN_UINT64_DATA || (idx) == DMU_OTN_UINT64_METADATA ? \
925cabbc6bSPrashanth Sreenivasa 	DMU_OT_UINT64_OTHER : DMU_OT_NUMTYPES)
936de8f417SVictor Latushkin 
944dd77f9eSMatthew Ahrens extern int reference_tracking_enable;
957fd05ac4SMatthew Ahrens extern boolean_t zfs_recover;
9606be9802SMatthew Ahrens extern uint64_t zfs_arc_max, zfs_arc_meta_limit;
97f7950bf1SMatthew Ahrens extern int zfs_vdev_async_read_max_active;
98f06dce2cSAndrew Stormont extern int aok;
99e144c4e6SPavel Zakharov extern boolean_t spa_load_verify_dryrun;
1004d7988d6SPaul Dagnelie extern int zfs_btree_verify_intensity;
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;
10906be9802SMatthew Ahrens uint64_t max_inflight = 1000;
11020b5dafbSPaul Dagnelie static int leaked_objects = 0;
111fa9e4066Sahrens 
112732885fcSMatthew Ahrens static void snprintf_blkptr_compact(char *, size_t, const blkptr_t *);
11317fb938fSMatthew Ahrens static void mos_obj_refd(uint64_t);
114732885fcSMatthew Ahrens 
115fa9e4066Sahrens /*
116fa9e4066Sahrens  * These libumem hooks provide a reasonable set of defaults for the allocator's
117fa9e4066Sahrens  * debugging facilities.
118fa9e4066Sahrens  */
119fa9e4066Sahrens const char *
120fa9e4066Sahrens _umem_debug_init()
121fa9e4066Sahrens {
122fa9e4066Sahrens 	return ("default,verbose"); /* $UMEM_DEBUG setting */
123fa9e4066Sahrens }
124fa9e4066Sahrens 
125fa9e4066Sahrens const char *
126fa9e4066Sahrens _umem_logging_init(void)
127fa9e4066Sahrens {
128fa9e4066Sahrens 	return ("fail,contents"); /* $UMEM_LOGGING setting */
129fa9e4066Sahrens }
130fa9e4066Sahrens 
131fa9e4066Sahrens static void
132fa9e4066Sahrens usage(void)
133fa9e4066Sahrens {
134fa9e4066Sahrens 	(void) fprintf(stderr,
13586714001SSerapheim Dimitropoulos 	    "Usage:\t%s [-AbcdDFGhikLMPsvX] [-e [-V] [-p <path> ...]] "
136ed61ec1dSYuri Pankov 	    "[-I <inflight I/Os>]\n"
137ed61ec1dSYuri Pankov 	    "\t\t[-o <var>=<value>]... [-t <txg>] [-U <cache>] [-x <dumpdir>]\n"
138ed61ec1dSYuri Pankov 	    "\t\t[<poolname> [<object> ...]]\n"
139dfd5965fSRichard Yao 	    "\t%s [-AdiPv] [-e [-V] [-p <path> ...]] [-U <cache>] <dataset> "
140ed61ec1dSYuri Pankov 	    "[<object> ...]\n"
141ed61ec1dSYuri Pankov 	    "\t%s -C [-A] [-U <cache>]\n"
142ed61ec1dSYuri Pankov 	    "\t%s -l [-Aqu] <device>\n"
143dfd5965fSRichard Yao 	    "\t%s -m [-AFLPX] [-e [-V] [-p <path> ...]] [-t <txg>] "
144dfd5965fSRichard Yao 	    "[-U <cache>]\n\t\t<poolname> [<vdev> [<metaslab> ...]]\n"
145ed61ec1dSYuri Pankov 	    "\t%s -O <dataset> <path>\n"
146dfd5965fSRichard Yao 	    "\t%s -R [-A] [-e [-V] [-p <path> ...]] [-U <cache>]\n"
147ed61ec1dSYuri Pankov 	    "\t\t<poolname> <vdev>:<offset>:<size>[:<flags>]\n"
1484923c69fSMatthew Ahrens 	    "\t%s -E [-A] word0:word1:...:word15\n"
149dfd5965fSRichard Yao 	    "\t%s -S [-AP] [-e [-V] [-p <path> ...]] [-U <cache>] "
150dfd5965fSRichard Yao 	    "<poolname>\n\n",
151ed61ec1dSYuri Pankov 	    cmdname, cmdname, cmdname, cmdname, cmdname, cmdname, cmdname,
1524923c69fSMatthew Ahrens 	    cmdname, cmdname);
1533ad6c7f9SVictor Latushkin 
1543ad6c7f9SVictor Latushkin 	(void) fprintf(stderr, "    Dataset name must include at least one "
1553ad6c7f9SVictor Latushkin 	    "separator character '/' or '@'\n");
1563ad6c7f9SVictor Latushkin 	(void) fprintf(stderr, "    If dataset name is specified, only that "
1573ad6c7f9SVictor Latushkin 	    "dataset is dumped\n");
1583ad6c7f9SVictor Latushkin 	(void) fprintf(stderr, "    If object numbers are specified, only "
1593ad6c7f9SVictor Latushkin 	    "those objects are dumped\n\n");
1603ad6c7f9SVictor Latushkin 	(void) fprintf(stderr, "    Options to control amount of output:\n");
1613ad6c7f9SVictor Latushkin 	(void) fprintf(stderr, "        -b block statistics\n");
1623ad6c7f9SVictor Latushkin 	(void) fprintf(stderr, "        -c checksum all metadata (twice for "
1636365109dSVictor Latushkin 	    "all data) blocks\n");
164ed61ec1dSYuri Pankov 	(void) fprintf(stderr, "        -C config (or cachefile if alone)\n");
165ed61ec1dSYuri Pankov 	(void) fprintf(stderr, "        -d dataset(s)\n");
166f0ba89beSJeff Bonwick 	(void) fprintf(stderr, "        -D dedup statistics\n");
1674923c69fSMatthew Ahrens 	(void) fprintf(stderr, "        -E decode and display block from an "
1684923c69fSMatthew Ahrens 	    "embedded block pointer\n");
169ed61ec1dSYuri Pankov 	(void) fprintf(stderr, "        -h pool history\n");
170ed61ec1dSYuri Pankov 	(void) fprintf(stderr, "        -i intent logs\n");
17164723e36SYuri Pankov 	(void) fprintf(stderr, "        -l read label contents\n");
17286714001SSerapheim Dimitropoulos 	(void) fprintf(stderr, "        -k examine the checkpointed state "
17386714001SSerapheim Dimitropoulos 	    "of the pool\n");
17482a0a985SVictor Latushkin 	(void) fprintf(stderr, "        -L disable leak tracking (do not "
17582a0a985SVictor Latushkin 	    "load spacemaps)\n");
176ed61ec1dSYuri Pankov 	(void) fprintf(stderr, "        -m metaslabs\n");
177ed61ec1dSYuri Pankov 	(void) fprintf(stderr, "        -M metaslab groups\n");
178ed61ec1dSYuri Pankov 	(void) fprintf(stderr, "        -O perform object lookups by path\n");
179d41e7643Sek 	(void) fprintf(stderr, "        -R read and display block from a "
180ed61ec1dSYuri Pankov 	    "device\n");
181ed61ec1dSYuri Pankov 	(void) fprintf(stderr, "        -s report stats on zdb's I/O\n");
182ed61ec1dSYuri Pankov 	(void) fprintf(stderr, "        -S simulate dedup to measure effect\n");
183ed61ec1dSYuri Pankov 	(void) fprintf(stderr, "        -v verbose (applies to all "
184ed61ec1dSYuri Pankov 	    "others)\n\n");
1853ad6c7f9SVictor Latushkin 	(void) fprintf(stderr, "    Below options are intended for use "
186df15e419SMatthew Ahrens 	    "with other options:\n");
187feef89cfSVictor Latushkin 	(void) fprintf(stderr, "        -A ignore assertions (-A), enable "
188feef89cfSVictor Latushkin 	    "panic recovery (-AA) or both (-AAA)\n");
1893ad6c7f9SVictor Latushkin 	(void) fprintf(stderr, "        -e pool is exported/destroyed/"
1903ad6c7f9SVictor Latushkin 	    "has altroot/not in a cachefile\n");
191ed61ec1dSYuri Pankov 	(void) fprintf(stderr, "        -F attempt automatic rewind within "
192ed61ec1dSYuri Pankov 	    "safe range of transaction groups\n");
193ed61ec1dSYuri Pankov 	(void) fprintf(stderr, "        -G dump zfs_dbgmsg buffer before "
194ed61ec1dSYuri Pankov 	    "exiting\n");
1952e4c9986SGeorge Wilson 	(void) fprintf(stderr, "        -I <number of inflight I/Os> -- "
196df15e419SMatthew Ahrens 	    "specify the maximum number of "
197df15e419SMatthew Ahrens 	    "checksumming I/Os [default is 200]\n");
1980e60744cSPavel Zakharov 	(void) fprintf(stderr, "        -o <variable>=<value> set global "
1990e60744cSPavel Zakharov 	    "variable to an unsigned 32-bit integer value\n");
200ed61ec1dSYuri Pankov 	(void) fprintf(stderr, "        -p <path> -- use one or more with "
201ed61ec1dSYuri Pankov 	    "-e to specify path to vdev dir\n");
202ed61ec1dSYuri Pankov 	(void) fprintf(stderr, "        -P print numbers in parseable form\n");
20364723e36SYuri Pankov 	(void) fprintf(stderr, "        -q don't print label contents\n");
204ed61ec1dSYuri Pankov 	(void) fprintf(stderr, "        -t <txg> -- highest txg to use when "
205ed61ec1dSYuri Pankov 	    "searching for uberblocks\n");
20664723e36SYuri Pankov 	(void) fprintf(stderr, "        -u uberblock\n");
207ed61ec1dSYuri Pankov 	(void) fprintf(stderr, "        -U <cachefile_path> -- use alternate "
208ed61ec1dSYuri Pankov 	    "cachefile\n");
209dfd5965fSRichard Yao 	(void) fprintf(stderr, "        -V do verbatim import\n");
210ed61ec1dSYuri Pankov 	(void) fprintf(stderr, "        -x <dumpdir> -- "
211ed61ec1dSYuri Pankov 	    "dump all read blocks into specified directory\n");
212ed61ec1dSYuri Pankov 	(void) fprintf(stderr, "        -X attempt extreme rewind (does not "
213ed61ec1dSYuri Pankov 	    "work with dataset)\n\n");
214fa9e4066Sahrens 	(void) fprintf(stderr, "Specify an option more than once (e.g. -bb) "
215fa9e4066Sahrens 	    "to make only that option verbose\n");
216fa9e4066Sahrens 	(void) fprintf(stderr, "Default is to dump everything non-verbosely\n");
217fa9e4066Sahrens 	exit(1);
218fa9e4066Sahrens }
219fa9e4066Sahrens 
22029bdd2f9SPavel Zakharov static void
22129bdd2f9SPavel Zakharov dump_debug_buffer()
22229bdd2f9SPavel Zakharov {
22329bdd2f9SPavel Zakharov 	if (dump_opt['G']) {
22429bdd2f9SPavel Zakharov 		(void) printf("\n");
22529bdd2f9SPavel Zakharov 		zfs_dbgmsg_print("zdb");
22629bdd2f9SPavel Zakharov 	}
22729bdd2f9SPavel Zakharov }
22829bdd2f9SPavel Zakharov 
229ccba0801SRich Morris /*
230ccba0801SRich Morris  * Called for usage errors that are discovered after a call to spa_open(),
231ccba0801SRich Morris  * dmu_bonus_hold(), or pool_match().  abort() is called for other errors.
232ccba0801SRich Morris  */
233ccba0801SRich Morris 
234fa9e4066Sahrens static void
235fa9e4066Sahrens fatal(const char *fmt, ...)
236fa9e4066Sahrens {
237fa9e4066Sahrens 	va_list ap;
238fa9e4066Sahrens 
239fa9e4066Sahrens 	va_start(ap, fmt);
240fa9e4066Sahrens 	(void) fprintf(stderr, "%s: ", cmdname);
241fa9e4066Sahrens 	(void) vfprintf(stderr, fmt, ap);
242fa9e4066Sahrens 	va_end(ap);
243fa9e4066Sahrens 	(void) fprintf(stderr, "\n");
244fa9e4066Sahrens 
24529bdd2f9SPavel Zakharov 	dump_debug_buffer();
24629bdd2f9SPavel Zakharov 
247ccba0801SRich Morris 	exit(1);
248fa9e4066Sahrens }
249fa9e4066Sahrens 
250fa9e4066Sahrens /* ARGSUSED */
251fa9e4066Sahrens static void
252fa9e4066Sahrens dump_packed_nvlist(objset_t *os, uint64_t object, void *data, size_t size)
253fa9e4066Sahrens {
254fa9e4066Sahrens 	nvlist_t *nv;
255fa9e4066Sahrens 	size_t nvsize = *(uint64_t *)data;
256fa9e4066Sahrens 	char *packed = umem_alloc(nvsize, UMEM_NOFAIL);
257fa9e4066Sahrens 
2587bfdf011SNeil Perrin 	VERIFY(0 == dmu_read(os, object, 0, nvsize, packed, DMU_READ_PREFETCH));
259fa9e4066Sahrens 
260fa9e4066Sahrens 	VERIFY(nvlist_unpack(packed, nvsize, &nv, 0) == 0);
261fa9e4066Sahrens 
262fa9e4066Sahrens 	umem_free(packed, nvsize);
263fa9e4066Sahrens 
264fa9e4066Sahrens 	dump_nvlist(nv, 8);
265fa9e4066Sahrens 
266fa9e4066Sahrens 	nvlist_free(nv);
267fa9e4066Sahrens }
268fa9e4066Sahrens 
2694445fffbSMatthew Ahrens /* ARGSUSED */
2704445fffbSMatthew Ahrens static void
2714445fffbSMatthew Ahrens dump_history_offsets(objset_t *os, uint64_t object, void *data, size_t size)
2724445fffbSMatthew Ahrens {
2734445fffbSMatthew Ahrens 	spa_history_phys_t *shp = data;
2744445fffbSMatthew Ahrens 
2754445fffbSMatthew Ahrens 	if (shp == NULL)
2764445fffbSMatthew Ahrens 		return;
2774445fffbSMatthew Ahrens 
2784445fffbSMatthew Ahrens 	(void) printf("\t\tpool_create_len = %llu\n",
2794445fffbSMatthew Ahrens 	    (u_longlong_t)shp->sh_pool_create_len);
2804445fffbSMatthew Ahrens 	(void) printf("\t\tphys_max_off = %llu\n",
2814445fffbSMatthew Ahrens 	    (u_longlong_t)shp->sh_phys_max_off);
2824445fffbSMatthew Ahrens 	(void) printf("\t\tbof = %llu\n",
2834445fffbSMatthew Ahrens 	    (u_longlong_t)shp->sh_bof);
2844445fffbSMatthew Ahrens 	(void) printf("\t\teof = %llu\n",
2854445fffbSMatthew Ahrens 	    (u_longlong_t)shp->sh_eof);
2864445fffbSMatthew Ahrens 	(void) printf("\t\trecords_lost = %llu\n",
2874445fffbSMatthew Ahrens 	    (u_longlong_t)shp->sh_records_lost);
2884445fffbSMatthew Ahrens }
2894445fffbSMatthew Ahrens 
2903f9d6ad7SLin Ling static void
2910a055120SJason King zdb_nicenum(uint64_t num, char *buf, size_t buflen)
2923f9d6ad7SLin Ling {
2933f9d6ad7SLin Ling 	if (dump_opt['P'])
2940a055120SJason King 		(void) snprintf(buf, buflen, "%llu", (longlong_t)num);
2953f9d6ad7SLin Ling 	else
2960a055120SJason King 		nicenum(num, buf, sizeof (buf));
2973f9d6ad7SLin Ling }
2983f9d6ad7SLin Ling 
2993f7978d0SAlan Somers static const char histo_stars[] = "****************************************";
3003f7978d0SAlan Somers static const uint64_t histo_width = sizeof (histo_stars) - 1;
301fa9e4066Sahrens 
302fa9e4066Sahrens static void
3030713e232SGeorge Wilson dump_histogram(const uint64_t *histo, int size, int offset)
304fa9e4066Sahrens {
305fa9e4066Sahrens 	int i;
306490d05b9SMatthew Ahrens 	int minidx = size - 1;
307fa9e4066Sahrens 	int maxidx = 0;
308fa9e4066Sahrens 	uint64_t max = 0;
309fa9e4066Sahrens 
310490d05b9SMatthew Ahrens 	for (i = 0; i < size; i++) {
311fa9e4066Sahrens 		if (histo[i] > max)
312fa9e4066Sahrens 			max = histo[i];
313fa9e4066Sahrens 		if (histo[i] > 0 && i > maxidx)
314fa9e4066Sahrens 			maxidx = i;
315fa9e4066Sahrens 		if (histo[i] > 0 && i < minidx)
316fa9e4066Sahrens 			minidx = i;
317fa9e4066Sahrens 	}
318fa9e4066Sahrens 
319490d05b9SMatthew Ahrens 	if (max < histo_width)
320490d05b9SMatthew Ahrens 		max = histo_width;
321fa9e4066Sahrens 
322490d05b9SMatthew Ahrens 	for (i = minidx; i <= maxidx; i++) {
323490d05b9SMatthew Ahrens 		(void) printf("\t\t\t%3u: %6llu %s\n",
3240713e232SGeorge Wilson 		    i + offset, (u_longlong_t)histo[i],
325490d05b9SMatthew Ahrens 		    &histo_stars[(max - histo[i]) * histo_width / max]);
326490d05b9SMatthew Ahrens 	}
327fa9e4066Sahrens }
328fa9e4066Sahrens 
329fa9e4066Sahrens static void
330fa9e4066Sahrens dump_zap_stats(objset_t *os, uint64_t object)
331fa9e4066Sahrens {
332fa9e4066Sahrens 	int error;
333fa9e4066Sahrens 	zap_stats_t zs;
334fa9e4066Sahrens 
335fa9e4066Sahrens 	error = zap_get_stats(os, object, &zs);
336fa9e4066Sahrens 	if (error)
337fa9e4066Sahrens 		return;
338fa9e4066Sahrens 
339fa9e4066Sahrens 	if (zs.zs_ptrtbl_len == 0) {
340fa9e4066Sahrens 		ASSERT(zs.zs_num_blocks == 1);
341fa9e4066Sahrens 		(void) printf("\tmicrozap: %llu bytes, %llu entries\n",
342fa9e4066Sahrens 		    (u_longlong_t)zs.zs_blocksize,
343fa9e4066Sahrens 		    (u_longlong_t)zs.zs_num_entries);
344fa9e4066Sahrens 		return;
345fa9e4066Sahrens 	}
346fa9e4066Sahrens 
347fa9e4066Sahrens 	(void) printf("\tFat ZAP stats:\n");
3488248818dSnd 
3498248818dSnd 	(void) printf("\t\tPointer table:\n");
3508248818dSnd 	(void) printf("\t\t\t%llu elements\n",
351fa9e4066Sahrens 	    (u_longlong_t)zs.zs_ptrtbl_len);
3528248818dSnd 	(void) printf("\t\t\tzt_blk: %llu\n",
3538248818dSnd 	    (u_longlong_t)zs.zs_ptrtbl_zt_blk);
3548248818dSnd 	(void) printf("\t\t\tzt_numblks: %llu\n",
3558248818dSnd 	    (u_longlong_t)zs.zs_ptrtbl_zt_numblks);
3568248818dSnd 	(void) printf("\t\t\tzt_shift: %llu\n",
3578248818dSnd 	    (u_longlong_t)zs.zs_ptrtbl_zt_shift);
3588248818dSnd 	(void) printf("\t\t\tzt_blks_copied: %llu\n",
3598248818dSnd 	    (u_longlong_t)zs.zs_ptrtbl_blks_copied);
3608248818dSnd 	(void) printf("\t\t\tzt_nextblk: %llu\n",
3618248818dSnd 	    (u_longlong_t)zs.zs_ptrtbl_nextblk);
3628248818dSnd 
363fa9e4066Sahrens 	(void) printf("\t\tZAP entries: %llu\n",
364fa9e4066Sahrens 	    (u_longlong_t)zs.zs_num_entries);
365fa9e4066Sahrens 	(void) printf("\t\tLeaf blocks: %llu\n",
366fa9e4066Sahrens 	    (u_longlong_t)zs.zs_num_leafs);
367fa9e4066Sahrens 	(void) printf("\t\tTotal blocks: %llu\n",
368fa9e4066Sahrens 	    (u_longlong_t)zs.zs_num_blocks);
3698248818dSnd 	(void) printf("\t\tzap_block_type: 0x%llx\n",
3708248818dSnd 	    (u_longlong_t)zs.zs_block_type);
3718248818dSnd 	(void) printf("\t\tzap_magic: 0x%llx\n",
3728248818dSnd 	    (u_longlong_t)zs.zs_magic);
3738248818dSnd 	(void) printf("\t\tzap_salt: 0x%llx\n",
3748248818dSnd 	    (u_longlong_t)zs.zs_salt);
375fa9e4066Sahrens 
376fa9e4066Sahrens 	(void) printf("\t\tLeafs with 2^n pointers:\n");
3770713e232SGeorge Wilson 	dump_histogram(zs.zs_leafs_with_2n_pointers, ZAP_HISTOGRAM_SIZE, 0);
378fa9e4066Sahrens 
379fa9e4066Sahrens 	(void) printf("\t\tBlocks with n*5 entries:\n");
3800713e232SGeorge Wilson 	dump_histogram(zs.zs_blocks_with_n5_entries, ZAP_HISTOGRAM_SIZE, 0);
381fa9e4066Sahrens 
382fa9e4066Sahrens 	(void) printf("\t\tBlocks n/10 full:\n");
3830713e232SGeorge Wilson 	dump_histogram(zs.zs_blocks_n_tenths_full, ZAP_HISTOGRAM_SIZE, 0);
384fa9e4066Sahrens 
385fa9e4066Sahrens 	(void) printf("\t\tEntries with n chunks:\n");
3860713e232SGeorge Wilson 	dump_histogram(zs.zs_entries_using_n_chunks, ZAP_HISTOGRAM_SIZE, 0);
387fa9e4066Sahrens 
388fa9e4066Sahrens 	(void) printf("\t\tBuckets with n entries:\n");
3890713e232SGeorge Wilson 	dump_histogram(zs.zs_buckets_with_n_entries, ZAP_HISTOGRAM_SIZE, 0);
390fa9e4066Sahrens }
391fa9e4066Sahrens 
392fa9e4066Sahrens /*ARGSUSED*/
393fa9e4066Sahrens static void
394fa9e4066Sahrens dump_none(objset_t *os, uint64_t object, void *data, size_t size)
395fa9e4066Sahrens {
396fa9e4066Sahrens }
397fa9e4066Sahrens 
3986de8f417SVictor Latushkin /*ARGSUSED*/
3996de8f417SVictor Latushkin static void
4006de8f417SVictor Latushkin dump_unknown(objset_t *os, uint64_t object, void *data, size_t size)
4016de8f417SVictor Latushkin {
4026de8f417SVictor Latushkin 	(void) printf("\tUNKNOWN OBJECT TYPE\n");
4036de8f417SVictor Latushkin }
4046de8f417SVictor Latushkin 
405fa9e4066Sahrens /*ARGSUSED*/
4063f7978d0SAlan Somers static void
407fa9e4066Sahrens dump_uint8(objset_t *os, uint64_t object, void *data, size_t size)
408fa9e4066Sahrens {
409fa9e4066Sahrens }
410fa9e4066Sahrens 
411fa9e4066Sahrens /*ARGSUSED*/
412fa9e4066Sahrens static void
413fa9e4066Sahrens dump_uint64(objset_t *os, uint64_t object, void *data, size_t size)
414fa9e4066Sahrens {
415fa9e4066Sahrens }
416fa9e4066Sahrens 
417fa9e4066Sahrens /*ARGSUSED*/
418fa9e4066Sahrens static void
419fa9e4066Sahrens dump_zap(objset_t *os, uint64_t object, void *data, size_t size)
420fa9e4066Sahrens {
421fa9e4066Sahrens 	zap_cursor_t zc;
422fa9e4066Sahrens 	zap_attribute_t attr;
423fa9e4066Sahrens 	void *prop;
4243f7978d0SAlan Somers 	unsigned i;
425fa9e4066Sahrens 
426fa9e4066Sahrens 	dump_zap_stats(os, object);
427fa9e4066Sahrens 	(void) printf("\n");
428fa9e4066Sahrens 
429fa9e4066Sahrens 	for (zap_cursor_init(&zc, os, object);
430fa9e4066Sahrens 	    zap_cursor_retrieve(&zc, &attr) == 0;
431fa9e4066Sahrens 	    zap_cursor_advance(&zc)) {
432fa9e4066Sahrens 		(void) printf("\t\t%s = ", attr.za_name);
433fa9e4066Sahrens 		if (attr.za_num_integers == 0) {
434fa9e4066Sahrens 			(void) printf("\n");
435fa9e4066Sahrens 			continue;
436fa9e4066Sahrens 		}
437fa9e4066Sahrens 		prop = umem_zalloc(attr.za_num_integers *
438fa9e4066Sahrens 		    attr.za_integer_length, UMEM_NOFAIL);
439fa9e4066Sahrens 		(void) zap_lookup(os, object, attr.za_name,
440fa9e4066Sahrens 		    attr.za_integer_length, attr.za_num_integers, prop);
441fa9e4066Sahrens 		if (attr.za_integer_length == 1) {
4422d37a1a3SToomas Soome 			if (strcmp(attr.za_name,
4432d37a1a3SToomas Soome 			    DSL_CRYPTO_KEY_MASTER_KEY) == 0 ||
4442d37a1a3SToomas Soome 			    strcmp(attr.za_name,
4452d37a1a3SToomas Soome 			    DSL_CRYPTO_KEY_HMAC_KEY) == 0 ||
4462d37a1a3SToomas Soome 			    strcmp(attr.za_name, DSL_CRYPTO_KEY_IV) == 0 ||
44732114f88SToomas Soome 			    strcmp(attr.za_name, DSL_CRYPTO_KEY_MAC) == 0 ||
44832114f88SToomas Soome 			    strcmp(attr.za_name, DMU_POOL_CHECKSUM_SALT) == 0) {
4492d37a1a3SToomas Soome 				uint8_t *u8 = prop;
4502d37a1a3SToomas Soome 
4512d37a1a3SToomas Soome 				for (i = 0; i < attr.za_num_integers; i++) {
4522d37a1a3SToomas Soome 					(void) printf("%02x", u8[i]);
4532d37a1a3SToomas Soome 				}
4542d37a1a3SToomas Soome 			} else {
4552d37a1a3SToomas Soome 				(void) printf("%s", (char *)prop);
4562d37a1a3SToomas Soome 			}
457fa9e4066Sahrens 		} else {
458fa9e4066Sahrens 			for (i = 0; i < attr.za_num_integers; i++) {
459fa9e4066Sahrens 				switch (attr.za_integer_length) {
460fa9e4066Sahrens 				case 2:
461fa9e4066Sahrens 					(void) printf("%u ",
462fa9e4066Sahrens 					    ((uint16_t *)prop)[i]);
463fa9e4066Sahrens 					break;
464fa9e4066Sahrens 				case 4:
465fa9e4066Sahrens 					(void) printf("%u ",
466fa9e4066Sahrens 					    ((uint32_t *)prop)[i]);
467fa9e4066Sahrens 					break;
468fa9e4066Sahrens 				case 8:
469fa9e4066Sahrens 					(void) printf("%lld ",
470fa9e4066Sahrens 					    (u_longlong_t)((int64_t *)prop)[i]);
471fa9e4066Sahrens 					break;
472fa9e4066Sahrens 				}
473fa9e4066Sahrens 			}
474fa9e4066Sahrens 		}
475fa9e4066Sahrens 		(void) printf("\n");
476fa9e4066Sahrens 		umem_free(prop, attr.za_num_integers * attr.za_integer_length);
477fa9e4066Sahrens 	}
47887e5029aSahrens 	zap_cursor_fini(&zc);
479fa9e4066Sahrens }
480fa9e4066Sahrens 
481732885fcSMatthew Ahrens static void
482732885fcSMatthew Ahrens dump_bpobj(objset_t *os, uint64_t object, void *data, size_t size)
483732885fcSMatthew Ahrens {
484732885fcSMatthew Ahrens 	bpobj_phys_t *bpop = data;
485732885fcSMatthew Ahrens 	char bytes[32], comp[32], uncomp[32];
486732885fcSMatthew Ahrens 
4870a055120SJason King 	/* make sure the output won't get truncated */
4880a055120SJason King 	CTASSERT(sizeof (bytes) >= NN_NUMBUF_SZ);
4890a055120SJason King 	CTASSERT(sizeof (comp) >= NN_NUMBUF_SZ);
4900a055120SJason King 	CTASSERT(sizeof (uncomp) >= NN_NUMBUF_SZ);
4910a055120SJason King 
492732885fcSMatthew Ahrens 	if (bpop == NULL)
493732885fcSMatthew Ahrens 		return;
494732885fcSMatthew Ahrens 
4950a055120SJason King 	zdb_nicenum(bpop->bpo_bytes, bytes, sizeof (bytes));
4960a055120SJason King 	zdb_nicenum(bpop->bpo_comp, comp, sizeof (comp));
4970a055120SJason King 	zdb_nicenum(bpop->bpo_uncomp, uncomp, sizeof (uncomp));
498732885fcSMatthew Ahrens 
499732885fcSMatthew Ahrens 	(void) printf("\t\tnum_blkptrs = %llu\n",
500732885fcSMatthew Ahrens 	    (u_longlong_t)bpop->bpo_num_blkptrs);
501732885fcSMatthew Ahrens 	(void) printf("\t\tbytes = %s\n", bytes);
502732885fcSMatthew Ahrens 	if (size >= BPOBJ_SIZE_V1) {
503732885fcSMatthew Ahrens 		(void) printf("\t\tcomp = %s\n", comp);
504732885fcSMatthew Ahrens 		(void) printf("\t\tuncomp = %s\n", uncomp);
505732885fcSMatthew Ahrens 	}
506732885fcSMatthew Ahrens 	if (size >= sizeof (*bpop)) {
507732885fcSMatthew Ahrens 		(void) printf("\t\tsubobjs = %llu\n",
508732885fcSMatthew Ahrens 		    (u_longlong_t)bpop->bpo_subobjs);
509732885fcSMatthew Ahrens 		(void) printf("\t\tnum_subobjs = %llu\n",
510732885fcSMatthew Ahrens 		    (u_longlong_t)bpop->bpo_num_subobjs);
511732885fcSMatthew Ahrens 	}
512732885fcSMatthew Ahrens 
513732885fcSMatthew Ahrens 	if (dump_opt['d'] < 5)
514732885fcSMatthew Ahrens 		return;
515732885fcSMatthew Ahrens 
516732885fcSMatthew Ahrens 	for (uint64_t i = 0; i < bpop->bpo_num_blkptrs; i++) {
517732885fcSMatthew Ahrens 		char blkbuf[BP_SPRINTF_LEN];
518732885fcSMatthew Ahrens 		blkptr_t bp;
519732885fcSMatthew Ahrens 
520732885fcSMatthew Ahrens 		int err = dmu_read(os, object,
521732885fcSMatthew Ahrens 		    i * sizeof (bp), sizeof (bp), &bp, 0);
522732885fcSMatthew Ahrens 		if (err != 0) {
523732885fcSMatthew Ahrens 			(void) printf("got error %u from dmu_read\n", err);
524732885fcSMatthew Ahrens 			break;
525732885fcSMatthew Ahrens 		}
526732885fcSMatthew Ahrens 		snprintf_blkptr_compact(blkbuf, sizeof (blkbuf), &bp);
527732885fcSMatthew Ahrens 		(void) printf("\t%s\n", blkbuf);
528732885fcSMatthew Ahrens 	}
529732885fcSMatthew Ahrens }
530732885fcSMatthew Ahrens 
531732885fcSMatthew Ahrens /* ARGSUSED */
532732885fcSMatthew Ahrens static void
533732885fcSMatthew Ahrens dump_bpobj_subobjs(objset_t *os, uint64_t object, void *data, size_t size)
534732885fcSMatthew Ahrens {
535732885fcSMatthew Ahrens 	dmu_object_info_t doi;
536732885fcSMatthew Ahrens 
537732885fcSMatthew Ahrens 	VERIFY0(dmu_object_info(os, object, &doi));
538732885fcSMatthew Ahrens 	uint64_t *subobjs = kmem_alloc(doi.doi_max_offset, KM_SLEEP);
539732885fcSMatthew Ahrens 
540732885fcSMatthew Ahrens 	int err = dmu_read(os, object, 0, doi.doi_max_offset, subobjs, 0);
541732885fcSMatthew Ahrens 	if (err != 0) {
542732885fcSMatthew Ahrens 		(void) printf("got error %u from dmu_read\n", err);
543732885fcSMatthew Ahrens 		kmem_free(subobjs, doi.doi_max_offset);
544732885fcSMatthew Ahrens 		return;
545732885fcSMatthew Ahrens 	}
546732885fcSMatthew Ahrens 
547732885fcSMatthew Ahrens 	int64_t last_nonzero = -1;
548732885fcSMatthew Ahrens 	for (uint64_t i = 0; i < doi.doi_max_offset / 8; i++) {
549732885fcSMatthew Ahrens 		if (subobjs[i] != 0)
550732885fcSMatthew Ahrens 			last_nonzero = i;
551732885fcSMatthew Ahrens 	}
552732885fcSMatthew Ahrens 
553732885fcSMatthew Ahrens 	for (int64_t i = 0; i <= last_nonzero; i++) {
554732885fcSMatthew Ahrens 		(void) printf("\t%llu\n", (longlong_t)subobjs[i]);
555732885fcSMatthew Ahrens 	}
556732885fcSMatthew Ahrens 	kmem_free(subobjs, doi.doi_max_offset);
557732885fcSMatthew Ahrens }
558732885fcSMatthew Ahrens 
559486ae710SMatthew Ahrens /*ARGSUSED*/
560486ae710SMatthew Ahrens static void
561486ae710SMatthew Ahrens dump_ddt_zap(objset_t *os, uint64_t object, void *data, size_t size)
562486ae710SMatthew Ahrens {
563486ae710SMatthew Ahrens 	dump_zap_stats(os, object);
564486ae710SMatthew Ahrens 	/* contents are printed elsewhere, properly decoded */
565486ae710SMatthew Ahrens }
566486ae710SMatthew Ahrens 
5670a586ceaSMark Shellenbaum /*ARGSUSED*/
5680a586ceaSMark Shellenbaum static void
5690a586ceaSMark Shellenbaum dump_sa_attrs(objset_t *os, uint64_t object, void *data, size_t size)
5700a586ceaSMark Shellenbaum {
5710a586ceaSMark Shellenbaum 	zap_cursor_t zc;
5720a586ceaSMark Shellenbaum 	zap_attribute_t attr;
5730a586ceaSMark Shellenbaum 
5740a586ceaSMark Shellenbaum 	dump_zap_stats(os, object);
5750a586ceaSMark Shellenbaum 	(void) printf("\n");
5760a586ceaSMark Shellenbaum 
5770a586ceaSMark Shellenbaum 	for (zap_cursor_init(&zc, os, object);
5780a586ceaSMark Shellenbaum 	    zap_cursor_retrieve(&zc, &attr) == 0;
5790a586ceaSMark Shellenbaum 	    zap_cursor_advance(&zc)) {
5800a586ceaSMark Shellenbaum 		(void) printf("\t\t%s = ", attr.za_name);
5810a586ceaSMark Shellenbaum 		if (attr.za_num_integers == 0) {
5820a586ceaSMark Shellenbaum 			(void) printf("\n");
5830a586ceaSMark Shellenbaum 			continue;
5840a586ceaSMark Shellenbaum 		}
5850a586ceaSMark Shellenbaum 		(void) printf(" %llx : [%d:%d:%d]\n",
5860a586ceaSMark Shellenbaum 		    (u_longlong_t)attr.za_first_integer,
5870a586ceaSMark Shellenbaum 		    (int)ATTR_LENGTH(attr.za_first_integer),
5880a586ceaSMark Shellenbaum 		    (int)ATTR_BSWAP(attr.za_first_integer),
5890a586ceaSMark Shellenbaum 		    (int)ATTR_NUM(attr.za_first_integer));
5900a586ceaSMark Shellenbaum 	}
5910a586ceaSMark Shellenbaum 	zap_cursor_fini(&zc);
5920a586ceaSMark Shellenbaum }
5930a586ceaSMark Shellenbaum 
5940a586ceaSMark Shellenbaum /*ARGSUSED*/
5950a586ceaSMark Shellenbaum static void
5960a586ceaSMark Shellenbaum dump_sa_layouts(objset_t *os, uint64_t object, void *data, size_t size)
5970a586ceaSMark Shellenbaum {
5980a586ceaSMark Shellenbaum 	zap_cursor_t zc;
5990a586ceaSMark Shellenbaum 	zap_attribute_t attr;
6000a586ceaSMark Shellenbaum 	uint16_t *layout_attrs;
6013f7978d0SAlan Somers 	unsigned i;
6020a586ceaSMark Shellenbaum 
6030a586ceaSMark Shellenbaum 	dump_zap_stats(os, object);
6040a586ceaSMark Shellenbaum 	(void) printf("\n");
6050a586ceaSMark Shellenbaum 
6060a586ceaSMark Shellenbaum 	for (zap_cursor_init(&zc, os, object);
6070a586ceaSMark Shellenbaum 	    zap_cursor_retrieve(&zc, &attr) == 0;
6080a586ceaSMark Shellenbaum 	    zap_cursor_advance(&zc)) {
6090a586ceaSMark Shellenbaum 		(void) printf("\t\t%s = [", attr.za_name);
6100a586ceaSMark Shellenbaum 		if (attr.za_num_integers == 0) {
6110a586ceaSMark Shellenbaum 			(void) printf("\n");
6120a586ceaSMark Shellenbaum 			continue;
6130a586ceaSMark Shellenbaum 		}
6140a586ceaSMark Shellenbaum 
6150a586ceaSMark Shellenbaum 		VERIFY(attr.za_integer_length == 2);
6160a586ceaSMark Shellenbaum 		layout_attrs = umem_zalloc(attr.za_num_integers *
6170a586ceaSMark Shellenbaum 		    attr.za_integer_length, UMEM_NOFAIL);
6180a586ceaSMark Shellenbaum 
6190a586ceaSMark Shellenbaum 		VERIFY(zap_lookup(os, object, attr.za_name,
6200a586ceaSMark Shellenbaum 		    attr.za_integer_length,
6210a586ceaSMark Shellenbaum 		    attr.za_num_integers, layout_attrs) == 0);
6220a586ceaSMark Shellenbaum 
6230a586ceaSMark Shellenbaum 		for (i = 0; i != attr.za_num_integers; i++)
6240a586ceaSMark Shellenbaum 			(void) printf(" %d ", (int)layout_attrs[i]);
6250a586ceaSMark Shellenbaum 		(void) printf("]\n");
6260a586ceaSMark Shellenbaum 		umem_free(layout_attrs,
6270a586ceaSMark Shellenbaum 		    attr.za_num_integers * attr.za_integer_length);
6280a586ceaSMark Shellenbaum 	}
6290a586ceaSMark Shellenbaum 	zap_cursor_fini(&zc);
6300a586ceaSMark Shellenbaum }
6310a586ceaSMark Shellenbaum 
632e7437265Sahrens /*ARGSUSED*/
633e7437265Sahrens static void
634e7437265Sahrens dump_zpldir(objset_t *os, uint64_t object, void *data, size_t size)
635e7437265Sahrens {
636e7437265Sahrens 	zap_cursor_t zc;
637e7437265Sahrens 	zap_attribute_t attr;
638e7437265Sahrens 	const char *typenames[] = {
639e7437265Sahrens 		/* 0 */ "not specified",
640e7437265Sahrens 		/* 1 */ "FIFO",
641e7437265Sahrens 		/* 2 */ "Character Device",
642e7437265Sahrens 		/* 3 */ "3 (invalid)",
643e7437265Sahrens 		/* 4 */ "Directory",
644e7437265Sahrens 		/* 5 */ "5 (invalid)",
645e7437265Sahrens 		/* 6 */ "Block Device",
646e7437265Sahrens 		/* 7 */ "7 (invalid)",
647e7437265Sahrens 		/* 8 */ "Regular File",
648e7437265Sahrens 		/* 9 */ "9 (invalid)",
649e7437265Sahrens 		/* 10 */ "Symbolic Link",
650e7437265Sahrens 		/* 11 */ "11 (invalid)",
651e7437265Sahrens 		/* 12 */ "Socket",
652e7437265Sahrens 		/* 13 */ "Door",
653e7437265Sahrens 		/* 14 */ "Event Port",
654e7437265Sahrens 		/* 15 */ "15 (invalid)",
655e7437265Sahrens 	};
656e7437265Sahrens 
657e7437265Sahrens 	dump_zap_stats(os, object);
658e7437265Sahrens 	(void) printf("\n");
659e7437265Sahrens 
660e7437265Sahrens 	for (zap_cursor_init(&zc, os, object);
661e7437265Sahrens 	    zap_cursor_retrieve(&zc, &attr) == 0;
662e7437265Sahrens 	    zap_cursor_advance(&zc)) {
663e7437265Sahrens 		(void) printf("\t\t%s = %lld (type: %s)\n",
664e7437265Sahrens 		    attr.za_name, ZFS_DIRENT_OBJ(attr.za_first_integer),
665e7437265Sahrens 		    typenames[ZFS_DIRENT_TYPE(attr.za_first_integer)]);
666e7437265Sahrens 	}
667e7437265Sahrens 	zap_cursor_fini(&zc);
668e7437265Sahrens }
669e7437265Sahrens 
6703f7978d0SAlan Somers static int
6710713e232SGeorge Wilson get_dtl_refcount(vdev_t *vd)
6720713e232SGeorge Wilson {
6730713e232SGeorge Wilson 	int refcount = 0;
6740713e232SGeorge Wilson 
6750713e232SGeorge Wilson 	if (vd->vdev_ops->vdev_op_leaf) {
6760713e232SGeorge Wilson 		space_map_t *sm = vd->vdev_dtl_sm;
6770713e232SGeorge Wilson 
6780713e232SGeorge Wilson 		if (sm != NULL &&
6790713e232SGeorge Wilson 		    sm->sm_dbuf->db_size == sizeof (space_map_phys_t))
6800713e232SGeorge Wilson 			return (1);
6810713e232SGeorge Wilson 		return (0);
6820713e232SGeorge Wilson 	}
6830713e232SGeorge Wilson 
6843f7978d0SAlan Somers 	for (unsigned c = 0; c < vd->vdev_children; c++)
6850713e232SGeorge Wilson 		refcount += get_dtl_refcount(vd->vdev_child[c]);
6860713e232SGeorge Wilson 	return (refcount);
6870713e232SGeorge Wilson }
6880713e232SGeorge Wilson 
6893f7978d0SAlan Somers static int
6900713e232SGeorge Wilson get_metaslab_refcount(vdev_t *vd)
6910713e232SGeorge Wilson {
6920713e232SGeorge Wilson 	int refcount = 0;
6930713e232SGeorge Wilson 
6945cabbc6bSPrashanth Sreenivasa 	if (vd->vdev_top == vd) {
6955cabbc6bSPrashanth Sreenivasa 		for (uint64_t m = 0; m < vd->vdev_ms_count; m++) {
6960713e232SGeorge Wilson 			space_map_t *sm = vd->vdev_ms[m]->ms_sm;
6970713e232SGeorge Wilson 
6980713e232SGeorge Wilson 			if (sm != NULL &&
6990713e232SGeorge Wilson 			    sm->sm_dbuf->db_size == sizeof (space_map_phys_t))
7000713e232SGeorge Wilson 				refcount++;
7010713e232SGeorge Wilson 		}
7020713e232SGeorge Wilson 	}
7033f7978d0SAlan Somers 	for (unsigned c = 0; c < vd->vdev_children; c++)
7040713e232SGeorge Wilson 		refcount += get_metaslab_refcount(vd->vdev_child[c]);
7050713e232SGeorge Wilson 
7060713e232SGeorge Wilson 	return (refcount);
7070713e232SGeorge Wilson }
7080713e232SGeorge Wilson 
7095cabbc6bSPrashanth Sreenivasa static int
7105cabbc6bSPrashanth Sreenivasa get_obsolete_refcount(vdev_t *vd)
7115cabbc6bSPrashanth Sreenivasa {
7125cabbc6bSPrashanth Sreenivasa 	int refcount = 0;
7135cabbc6bSPrashanth Sreenivasa 
7145cabbc6bSPrashanth Sreenivasa 	uint64_t obsolete_sm_obj = vdev_obsolete_sm_object(vd);
7155cabbc6bSPrashanth Sreenivasa 	if (vd->vdev_top == vd && obsolete_sm_obj != 0) {
7165cabbc6bSPrashanth Sreenivasa 		dmu_object_info_t doi;
7175cabbc6bSPrashanth Sreenivasa 		VERIFY0(dmu_object_info(vd->vdev_spa->spa_meta_objset,
7185cabbc6bSPrashanth Sreenivasa 		    obsolete_sm_obj, &doi));
7195cabbc6bSPrashanth Sreenivasa 		if (doi.doi_bonus_size == sizeof (space_map_phys_t)) {
7205cabbc6bSPrashanth Sreenivasa 			refcount++;
7215cabbc6bSPrashanth Sreenivasa 		}
7225cabbc6bSPrashanth Sreenivasa 	} else {
7235cabbc6bSPrashanth Sreenivasa 		ASSERT3P(vd->vdev_obsolete_sm, ==, NULL);
7245cabbc6bSPrashanth Sreenivasa 		ASSERT3U(obsolete_sm_obj, ==, 0);
7255cabbc6bSPrashanth Sreenivasa 	}
7265cabbc6bSPrashanth Sreenivasa 	for (unsigned c = 0; c < vd->vdev_children; c++) {
7275cabbc6bSPrashanth Sreenivasa 		refcount += get_obsolete_refcount(vd->vdev_child[c]);
7285cabbc6bSPrashanth Sreenivasa 	}
7295cabbc6bSPrashanth Sreenivasa 
7305cabbc6bSPrashanth Sreenivasa 	return (refcount);
7315cabbc6bSPrashanth Sreenivasa }
7325cabbc6bSPrashanth Sreenivasa 
7335cabbc6bSPrashanth Sreenivasa static int
7345cabbc6bSPrashanth Sreenivasa get_prev_obsolete_spacemap_refcount(spa_t *spa)
7355cabbc6bSPrashanth Sreenivasa {
7365cabbc6bSPrashanth Sreenivasa 	uint64_t prev_obj =
7375cabbc6bSPrashanth Sreenivasa 	    spa->spa_condensing_indirect_phys.scip_prev_obsolete_sm_object;
7385cabbc6bSPrashanth Sreenivasa 	if (prev_obj != 0) {
7395cabbc6bSPrashanth Sreenivasa 		dmu_object_info_t doi;
7405cabbc6bSPrashanth Sreenivasa 		VERIFY0(dmu_object_info(spa->spa_meta_objset, prev_obj, &doi));
7415cabbc6bSPrashanth Sreenivasa 		if (doi.doi_bonus_size == sizeof (space_map_phys_t)) {
7425cabbc6bSPrashanth Sreenivasa 			return (1);
7435cabbc6bSPrashanth Sreenivasa 		}
7445cabbc6bSPrashanth Sreenivasa 	}
7455cabbc6bSPrashanth Sreenivasa 	return (0);
7465cabbc6bSPrashanth Sreenivasa }
7475cabbc6bSPrashanth Sreenivasa 
74886714001SSerapheim Dimitropoulos static int
74986714001SSerapheim Dimitropoulos get_checkpoint_refcount(vdev_t *vd)
75086714001SSerapheim Dimitropoulos {
75186714001SSerapheim Dimitropoulos 	int refcount = 0;
75286714001SSerapheim Dimitropoulos 
75386714001SSerapheim Dimitropoulos 	if (vd->vdev_top == vd && vd->vdev_top_zap != 0 &&
75486714001SSerapheim Dimitropoulos 	    zap_contains(spa_meta_objset(vd->vdev_spa),
75586714001SSerapheim Dimitropoulos 	    vd->vdev_top_zap, VDEV_TOP_ZAP_POOL_CHECKPOINT_SM) == 0)
75686714001SSerapheim Dimitropoulos 		refcount++;
75786714001SSerapheim Dimitropoulos 
75886714001SSerapheim Dimitropoulos 	for (uint64_t c = 0; c < vd->vdev_children; c++)
75986714001SSerapheim Dimitropoulos 		refcount += get_checkpoint_refcount(vd->vdev_child[c]);
76086714001SSerapheim Dimitropoulos 
76186714001SSerapheim Dimitropoulos 	return (refcount);
76286714001SSerapheim Dimitropoulos }
76386714001SSerapheim Dimitropoulos 
764814dcd43SSerapheim Dimitropoulos static int
765814dcd43SSerapheim Dimitropoulos get_log_spacemap_refcount(spa_t *spa)
766814dcd43SSerapheim Dimitropoulos {
767814dcd43SSerapheim Dimitropoulos 	return (avl_numnodes(&spa->spa_sm_logs_by_txg));
768814dcd43SSerapheim Dimitropoulos }
769814dcd43SSerapheim Dimitropoulos 
7700713e232SGeorge Wilson static int
7710713e232SGeorge Wilson verify_spacemap_refcounts(spa_t *spa)
7720713e232SGeorge Wilson {
7732acef22dSMatthew Ahrens 	uint64_t expected_refcount = 0;
7742acef22dSMatthew Ahrens 	uint64_t actual_refcount;
7750713e232SGeorge Wilson 
7762acef22dSMatthew Ahrens 	(void) feature_get_refcount(spa,
7772acef22dSMatthew Ahrens 	    &spa_feature_table[SPA_FEATURE_SPACEMAP_HISTOGRAM],
7782acef22dSMatthew Ahrens 	    &expected_refcount);
7790713e232SGeorge Wilson 	actual_refcount = get_dtl_refcount(spa->spa_root_vdev);
7800713e232SGeorge Wilson 	actual_refcount += get_metaslab_refcount(spa->spa_root_vdev);
7815cabbc6bSPrashanth Sreenivasa 	actual_refcount += get_obsolete_refcount(spa->spa_root_vdev);
7825cabbc6bSPrashanth Sreenivasa 	actual_refcount += get_prev_obsolete_spacemap_refcount(spa);
78386714001SSerapheim Dimitropoulos 	actual_refcount += get_checkpoint_refcount(spa->spa_root_vdev);
784814dcd43SSerapheim Dimitropoulos 	actual_refcount += get_log_spacemap_refcount(spa);
7850713e232SGeorge Wilson 
7860713e232SGeorge Wilson 	if (expected_refcount != actual_refcount) {
7872acef22dSMatthew Ahrens 		(void) printf("space map refcount mismatch: expected %lld != "
7882acef22dSMatthew Ahrens 		    "actual %lld\n",
7892acef22dSMatthew Ahrens 		    (longlong_t)expected_refcount,
7902acef22dSMatthew Ahrens 		    (longlong_t)actual_refcount);
7910713e232SGeorge Wilson 		return (2);
7920713e232SGeorge Wilson 	}
7930713e232SGeorge Wilson 	return (0);
7940713e232SGeorge Wilson }
7950713e232SGeorge Wilson 
796fa9e4066Sahrens static void
7970713e232SGeorge Wilson dump_spacemap(objset_t *os, space_map_t *sm)
798fa9e4066Sahrens {
7995cabbc6bSPrashanth Sreenivasa 	char *ddata[] = { "ALLOC", "FREE", "CONDENSE", "INVALID",
8005cabbc6bSPrashanth Sreenivasa 	    "INVALID", "INVALID", "INVALID", "INVALID" };
801fa9e4066Sahrens 
8020713e232SGeorge Wilson 	if (sm == NULL)
803fa9e4066Sahrens 		return;
804fa9e4066Sahrens 
8055cabbc6bSPrashanth Sreenivasa 	(void) printf("space map object %llu:\n",
806555d674dSSerapheim Dimitropoulos 	    (longlong_t)sm->sm_object);
807555d674dSSerapheim Dimitropoulos 	(void) printf("  smp_length = 0x%llx\n",
808555d674dSSerapheim Dimitropoulos 	    (longlong_t)sm->sm_phys->smp_length);
8095cabbc6bSPrashanth Sreenivasa 	(void) printf("  smp_alloc = 0x%llx\n",
8105cabbc6bSPrashanth Sreenivasa 	    (longlong_t)sm->sm_phys->smp_alloc);
8115cabbc6bSPrashanth Sreenivasa 
812555d674dSSerapheim Dimitropoulos 	if (dump_opt['d'] < 6 && dump_opt['m'] < 4)
813555d674dSSerapheim Dimitropoulos 		return;
814555d674dSSerapheim Dimitropoulos 
815fa9e4066Sahrens 	/*
816fa9e4066Sahrens 	 * Print out the freelist entries in both encoded and decoded form.
817fa9e4066Sahrens 	 */
81817f11284SSerapheim Dimitropoulos 	uint8_t mapshift = sm->sm_shift;
81917f11284SSerapheim Dimitropoulos 	int64_t alloc = 0;
820555d674dSSerapheim Dimitropoulos 	uint64_t word, entry_id = 0;
82117f11284SSerapheim Dimitropoulos 	for (uint64_t offset = 0; offset < space_map_length(sm);
82217f11284SSerapheim Dimitropoulos 	    offset += sizeof (word)) {
8230713e232SGeorge Wilson 
8240713e232SGeorge Wilson 		VERIFY0(dmu_read(os, space_map_object(sm), offset,
82517f11284SSerapheim Dimitropoulos 		    sizeof (word), &word, DMU_READ_PREFETCH));
8260713e232SGeorge Wilson 
82717f11284SSerapheim Dimitropoulos 		if (sm_entry_is_debug(word)) {
828555d674dSSerapheim Dimitropoulos 			(void) printf("\t    [%6llu] %s: txg %llu pass %llu\n",
829555d674dSSerapheim Dimitropoulos 			    (u_longlong_t)entry_id,
83017f11284SSerapheim Dimitropoulos 			    ddata[SM_DEBUG_ACTION_DECODE(word)],
83117f11284SSerapheim Dimitropoulos 			    (u_longlong_t)SM_DEBUG_TXG_DECODE(word),
83217f11284SSerapheim Dimitropoulos 			    (u_longlong_t)SM_DEBUG_SYNCPASS_DECODE(word));
833555d674dSSerapheim Dimitropoulos 			entry_id++;
83417f11284SSerapheim Dimitropoulos 			continue;
83517f11284SSerapheim Dimitropoulos 		}
83617f11284SSerapheim Dimitropoulos 
83717f11284SSerapheim Dimitropoulos 		uint8_t words;
83817f11284SSerapheim Dimitropoulos 		char entry_type;
83917f11284SSerapheim Dimitropoulos 		uint64_t entry_off, entry_run, entry_vdev = SM_NO_VDEVID;
84017f11284SSerapheim Dimitropoulos 
84117f11284SSerapheim Dimitropoulos 		if (sm_entry_is_single_word(word)) {
84217f11284SSerapheim Dimitropoulos 			entry_type = (SM_TYPE_DECODE(word) == SM_ALLOC) ?
84317f11284SSerapheim Dimitropoulos 			    'A' : 'F';
84417f11284SSerapheim Dimitropoulos 			entry_off = (SM_OFFSET_DECODE(word) << mapshift) +
84517f11284SSerapheim Dimitropoulos 			    sm->sm_start;
84617f11284SSerapheim Dimitropoulos 			entry_run = SM_RUN_DECODE(word) << mapshift;
84717f11284SSerapheim Dimitropoulos 			words = 1;
848fa9e4066Sahrens 		} else {
84917f11284SSerapheim Dimitropoulos 			/* it is a two-word entry so we read another word */
85017f11284SSerapheim Dimitropoulos 			ASSERT(sm_entry_is_double_word(word));
85117f11284SSerapheim Dimitropoulos 
85217f11284SSerapheim Dimitropoulos 			uint64_t extra_word;
85317f11284SSerapheim Dimitropoulos 			offset += sizeof (extra_word);
85417f11284SSerapheim Dimitropoulos 			VERIFY0(dmu_read(os, space_map_object(sm), offset,
85517f11284SSerapheim Dimitropoulos 			    sizeof (extra_word), &extra_word,
85617f11284SSerapheim Dimitropoulos 			    DMU_READ_PREFETCH));
85717f11284SSerapheim Dimitropoulos 
85817f11284SSerapheim Dimitropoulos 			ASSERT3U(offset, <=, space_map_length(sm));
85917f11284SSerapheim Dimitropoulos 
86017f11284SSerapheim Dimitropoulos 			entry_run = SM2_RUN_DECODE(word) << mapshift;
86117f11284SSerapheim Dimitropoulos 			entry_vdev = SM2_VDEV_DECODE(word);
86217f11284SSerapheim Dimitropoulos 			entry_type = (SM2_TYPE_DECODE(extra_word) == SM_ALLOC) ?
86317f11284SSerapheim Dimitropoulos 			    'A' : 'F';
86417f11284SSerapheim Dimitropoulos 			entry_off = (SM2_OFFSET_DECODE(extra_word) <<
86517f11284SSerapheim Dimitropoulos 			    mapshift) + sm->sm_start;
86617f11284SSerapheim Dimitropoulos 			words = 2;
867fa9e4066Sahrens 		}
86817f11284SSerapheim Dimitropoulos 
86917f11284SSerapheim Dimitropoulos 		(void) printf("\t    [%6llu]    %c  range:"
87017f11284SSerapheim Dimitropoulos 		    " %010llx-%010llx  size: %06llx vdev: %06llu words: %u\n",
871555d674dSSerapheim Dimitropoulos 		    (u_longlong_t)entry_id,
87217f11284SSerapheim Dimitropoulos 		    entry_type, (u_longlong_t)entry_off,
87317f11284SSerapheim Dimitropoulos 		    (u_longlong_t)(entry_off + entry_run),
87417f11284SSerapheim Dimitropoulos 		    (u_longlong_t)entry_run,
87517f11284SSerapheim Dimitropoulos 		    (u_longlong_t)entry_vdev, words);
87617f11284SSerapheim Dimitropoulos 
87717f11284SSerapheim Dimitropoulos 		if (entry_type == 'A')
87817f11284SSerapheim Dimitropoulos 			alloc += entry_run;
87917f11284SSerapheim Dimitropoulos 		else
88017f11284SSerapheim Dimitropoulos 			alloc -= entry_run;
881555d674dSSerapheim Dimitropoulos 		entry_id++;
882fa9e4066Sahrens 	}
883555d674dSSerapheim Dimitropoulos 	if (alloc != space_map_allocated(sm)) {
88417f11284SSerapheim Dimitropoulos 		(void) printf("space_map_object alloc (%lld) INCONSISTENT "
88517f11284SSerapheim Dimitropoulos 		    "with space map summary (%lld)\n",
88617f11284SSerapheim Dimitropoulos 		    (longlong_t)space_map_allocated(sm), (longlong_t)alloc);
887fa9e4066Sahrens 	}
888fa9e4066Sahrens }
889fa9e4066Sahrens 
890d6e555bdSGeorge Wilson static void
891d6e555bdSGeorge Wilson dump_metaslab_stats(metaslab_t *msp)
892d6e555bdSGeorge Wilson {
8933f9d6ad7SLin Ling 	char maxbuf[32];
89486714001SSerapheim Dimitropoulos 	range_tree_t *rt = msp->ms_allocatable;
8954d7988d6SPaul Dagnelie 	zfs_btree_t *t = &msp->ms_allocatable_by_size;
8960713e232SGeorge Wilson 	int free_pct = range_tree_space(rt) * 100 / msp->ms_size;
897d6e555bdSGeorge Wilson 
8980a055120SJason King 	/* max sure nicenum has enough space */
8990a055120SJason King 	CTASSERT(sizeof (maxbuf) >= NN_NUMBUF_SZ);
9000a055120SJason King 
901af1d63abSPaul Dagnelie 	zdb_nicenum(metaslab_largest_allocatable(msp), maxbuf, sizeof (maxbuf));
902d6e555bdSGeorge Wilson 
90387219db7SVictor Latushkin 	(void) printf("\t %25s %10lu   %7s  %6s   %4s %4d%%\n",
9044d7988d6SPaul Dagnelie 	    "segments", zfs_btree_numnodes(t), "maxsize", maxbuf,
905d6e555bdSGeorge Wilson 	    "freepct", free_pct);
9060713e232SGeorge Wilson 	(void) printf("\tIn-memory histogram:\n");
9070713e232SGeorge Wilson 	dump_histogram(rt->rt_histogram, RANGE_TREE_HISTOGRAM_SIZE, 0);
908d6e555bdSGeorge Wilson }
909d6e555bdSGeorge Wilson 
910fa9e4066Sahrens static void
911fa9e4066Sahrens dump_metaslab(metaslab_t *msp)
912fa9e4066Sahrens {
913fa9e4066Sahrens 	vdev_t *vd = msp->ms_group->mg_vd;
914fa9e4066Sahrens 	spa_t *spa = vd->vdev_spa;
9150713e232SGeorge Wilson 	space_map_t *sm = msp->ms_sm;
9163f9d6ad7SLin Ling 	char freebuf[32];
917fa9e4066Sahrens 
9180a055120SJason King 	zdb_nicenum(msp->ms_size - space_map_allocated(sm), freebuf,
9190a055120SJason King 	    sizeof (freebuf));
920fa9e4066Sahrens 
921fa9e4066Sahrens 	(void) printf(
92287219db7SVictor Latushkin 	    "\tmetaslab %6llu   offset %12llx   spacemap %6llu   free    %5s\n",
9230713e232SGeorge Wilson 	    (u_longlong_t)msp->ms_id, (u_longlong_t)msp->ms_start,
9240713e232SGeorge Wilson 	    (u_longlong_t)space_map_object(sm), freebuf);
925fa9e4066Sahrens 
9260713e232SGeorge Wilson 	if (dump_opt['m'] > 2 && !dump_opt['L']) {
927d6e555bdSGeorge Wilson 		mutex_enter(&msp->ms_lock);
928a0b03b16SSerapheim Dimitropoulos 		VERIFY0(metaslab_load(msp));
929a0b03b16SSerapheim Dimitropoulos 		range_tree_stat_verify(msp->ms_allocatable);
930bc3975b5SVictor Latushkin 		dump_metaslab_stats(msp);
9310713e232SGeorge Wilson 		metaslab_unload(msp);
932d6e555bdSGeorge Wilson 		mutex_exit(&msp->ms_lock);
933d6e555bdSGeorge Wilson 	}
934d6e555bdSGeorge Wilson 
9350713e232SGeorge Wilson 	if (dump_opt['m'] > 1 && sm != NULL &&
9362acef22dSMatthew Ahrens 	    spa_feature_is_active(spa, SPA_FEATURE_SPACEMAP_HISTOGRAM)) {
9370713e232SGeorge Wilson 		/*
9380713e232SGeorge Wilson 		 * The space map histogram represents free space in chunks
9390713e232SGeorge Wilson 		 * of sm_shift (i.e. bucket 0 refers to 2^sm_shift).
9400713e232SGeorge Wilson 		 */
9412e4c9986SGeorge Wilson 		(void) printf("\tOn-disk histogram:\t\tfragmentation %llu\n",
9422e4c9986SGeorge Wilson 		    (u_longlong_t)msp->ms_fragmentation);
9430713e232SGeorge Wilson 		dump_histogram(sm->sm_phys->smp_histogram,
9442e4c9986SGeorge Wilson 		    SPACE_MAP_HISTOGRAM_SIZE, sm->sm_shift);
9450713e232SGeorge Wilson 	}
9460713e232SGeorge Wilson 
947555d674dSSerapheim Dimitropoulos 	ASSERT(msp->ms_size == (1ULL << vd->vdev_ms_shift));
948555d674dSSerapheim Dimitropoulos 	dump_spacemap(spa->spa_meta_objset, msp->ms_sm);
949814dcd43SSerapheim Dimitropoulos 
950814dcd43SSerapheim Dimitropoulos 	if (spa_feature_is_active(spa, SPA_FEATURE_LOG_SPACEMAP)) {
951814dcd43SSerapheim Dimitropoulos 		(void) printf("\tFlush data:\n\tunflushed txg=%llu\n\n",
952814dcd43SSerapheim Dimitropoulos 		    (u_longlong_t)metaslab_unflushed_txg(msp));
953814dcd43SSerapheim Dimitropoulos 	}
95487219db7SVictor Latushkin }
955fa9e4066Sahrens 
95687219db7SVictor Latushkin static void
95787219db7SVictor Latushkin print_vdev_metaslab_header(vdev_t *vd)
95887219db7SVictor Latushkin {
959663207adSDon Brady 	vdev_alloc_bias_t alloc_bias = vd->vdev_alloc_bias;
960814dcd43SSerapheim Dimitropoulos 	const char *bias_str = "";
961814dcd43SSerapheim Dimitropoulos 
962814dcd43SSerapheim Dimitropoulos 	if (alloc_bias == VDEV_BIAS_LOG || vd->vdev_islog) {
963814dcd43SSerapheim Dimitropoulos 		bias_str = VDEV_ALLOC_BIAS_LOG;
964814dcd43SSerapheim Dimitropoulos 	} else if (alloc_bias == VDEV_BIAS_SPECIAL) {
965814dcd43SSerapheim Dimitropoulos 		bias_str = VDEV_ALLOC_BIAS_SPECIAL;
966814dcd43SSerapheim Dimitropoulos 	} else if (alloc_bias == VDEV_BIAS_DEDUP) {
967814dcd43SSerapheim Dimitropoulos 		bias_str = VDEV_ALLOC_BIAS_DEDUP;
968814dcd43SSerapheim Dimitropoulos 	}
969814dcd43SSerapheim Dimitropoulos 
970814dcd43SSerapheim Dimitropoulos 	uint64_t ms_flush_data_obj = 0;
971814dcd43SSerapheim Dimitropoulos 	if (vd->vdev_top_zap != 0) {
972814dcd43SSerapheim Dimitropoulos 		int error = zap_lookup(spa_meta_objset(vd->vdev_spa),
973814dcd43SSerapheim Dimitropoulos 		    vd->vdev_top_zap, VDEV_TOP_ZAP_MS_UNFLUSHED_PHYS_TXGS,
974814dcd43SSerapheim Dimitropoulos 		    sizeof (uint64_t), 1, &ms_flush_data_obj);
975814dcd43SSerapheim Dimitropoulos 		if (error != ENOENT) {
976814dcd43SSerapheim Dimitropoulos 			ASSERT0(error);
977814dcd43SSerapheim Dimitropoulos 		}
978814dcd43SSerapheim Dimitropoulos 	}
979814dcd43SSerapheim Dimitropoulos 
980814dcd43SSerapheim Dimitropoulos 	(void) printf("\tvdev %10llu   %s",
981814dcd43SSerapheim Dimitropoulos 	    (u_longlong_t)vd->vdev_id, bias_str);
982663207adSDon Brady 
983814dcd43SSerapheim Dimitropoulos 	if (ms_flush_data_obj != 0) {
984814dcd43SSerapheim Dimitropoulos 		(void) printf("   ms_unflushed_phys object %llu",
985814dcd43SSerapheim Dimitropoulos 		    (u_longlong_t)ms_flush_data_obj);
986814dcd43SSerapheim Dimitropoulos 	}
987663207adSDon Brady 
988814dcd43SSerapheim Dimitropoulos 	(void) printf("\n\t%-10s%5llu   %-19s   %-15s   %-12s\n",
98987219db7SVictor Latushkin 	    "metaslabs", (u_longlong_t)vd->vdev_ms_count,
99087219db7SVictor Latushkin 	    "offset", "spacemap", "free");
991663207adSDon Brady 	(void) printf("\t%15s   %19s   %15s   %12s\n",
99287219db7SVictor Latushkin 	    "---------------", "-------------------",
993663207adSDon Brady 	    "---------------", "------------");
994fa9e4066Sahrens }
995fa9e4066Sahrens 
9962e4c9986SGeorge Wilson static void
9972e4c9986SGeorge Wilson dump_metaslab_groups(spa_t *spa)
9982e4c9986SGeorge Wilson {
9992e4c9986SGeorge Wilson 	vdev_t *rvd = spa->spa_root_vdev;
10002e4c9986SGeorge Wilson 	metaslab_class_t *mc = spa_normal_class(spa);
10012e4c9986SGeorge Wilson 	uint64_t fragmentation;
10022e4c9986SGeorge Wilson 
10032e4c9986SGeorge Wilson 	metaslab_class_histogram_verify(mc);
10042e4c9986SGeorge Wilson 
10053f7978d0SAlan Somers 	for (unsigned c = 0; c < rvd->vdev_children; c++) {
10062e4c9986SGeorge Wilson 		vdev_t *tvd = rvd->vdev_child[c];
10072e4c9986SGeorge Wilson 		metaslab_group_t *mg = tvd->vdev_mg;
10082e4c9986SGeorge Wilson 
1009663207adSDon Brady 		if (mg == NULL || mg->mg_class != mc)
10102e4c9986SGeorge Wilson 			continue;
10112e4c9986SGeorge Wilson 
10122e4c9986SGeorge Wilson 		metaslab_group_histogram_verify(mg);
10132e4c9986SGeorge Wilson 		mg->mg_fragmentation = metaslab_group_fragmentation(mg);
10142e4c9986SGeorge Wilson 
10152e4c9986SGeorge Wilson 		(void) printf("\tvdev %10llu\t\tmetaslabs%5llu\t\t"
10162e4c9986SGeorge Wilson 		    "fragmentation",
10172e4c9986SGeorge Wilson 		    (u_longlong_t)tvd->vdev_id,
10182e4c9986SGeorge Wilson 		    (u_longlong_t)tvd->vdev_ms_count);
10192e4c9986SGeorge Wilson 		if (mg->mg_fragmentation == ZFS_FRAG_INVALID) {
10202e4c9986SGeorge Wilson 			(void) printf("%3s\n", "-");
10212e4c9986SGeorge Wilson 		} else {
10222e4c9986SGeorge Wilson 			(void) printf("%3llu%%\n",
10232e4c9986SGeorge Wilson 			    (u_longlong_t)mg->mg_fragmentation);
10242e4c9986SGeorge Wilson 		}
10252e4c9986SGeorge Wilson 		dump_histogram(mg->mg_histogram, RANGE_TREE_HISTOGRAM_SIZE, 0);
10262e4c9986SGeorge Wilson 	}
10272e4c9986SGeorge Wilson 
10282e4c9986SGeorge Wilson 	(void) printf("\tpool %s\tfragmentation", spa_name(spa));
10292e4c9986SGeorge Wilson 	fragmentation = metaslab_class_fragmentation(mc);
10302e4c9986SGeorge Wilson 	if (fragmentation == ZFS_FRAG_INVALID)
10312e4c9986SGeorge Wilson 		(void) printf("\t%3s\n", "-");
10322e4c9986SGeorge Wilson 	else
10332e4c9986SGeorge Wilson 		(void) printf("\t%3llu%%\n", (u_longlong_t)fragmentation);
10342e4c9986SGeorge Wilson 	dump_histogram(mc->mc_histogram, RANGE_TREE_HISTOGRAM_SIZE, 0);
10352e4c9986SGeorge Wilson }
10362e4c9986SGeorge Wilson 
10375cabbc6bSPrashanth Sreenivasa static void
10385cabbc6bSPrashanth Sreenivasa print_vdev_indirect(vdev_t *vd)
10395cabbc6bSPrashanth Sreenivasa {
10405cabbc6bSPrashanth Sreenivasa 	vdev_indirect_config_t *vic = &vd->vdev_indirect_config;
10415cabbc6bSPrashanth Sreenivasa 	vdev_indirect_mapping_t *vim = vd->vdev_indirect_mapping;
10425cabbc6bSPrashanth Sreenivasa 	vdev_indirect_births_t *vib = vd->vdev_indirect_births;
10435cabbc6bSPrashanth Sreenivasa 
10445cabbc6bSPrashanth Sreenivasa 	if (vim == NULL) {
10455cabbc6bSPrashanth Sreenivasa 		ASSERT3P(vib, ==, NULL);
10465cabbc6bSPrashanth Sreenivasa 		return;
10475cabbc6bSPrashanth Sreenivasa 	}
10485cabbc6bSPrashanth Sreenivasa 
10495cabbc6bSPrashanth Sreenivasa 	ASSERT3U(vdev_indirect_mapping_object(vim), ==,
10505cabbc6bSPrashanth Sreenivasa 	    vic->vic_mapping_object);
10515cabbc6bSPrashanth Sreenivasa 	ASSERT3U(vdev_indirect_births_object(vib), ==,
10525cabbc6bSPrashanth Sreenivasa 	    vic->vic_births_object);
10535cabbc6bSPrashanth Sreenivasa 
10545cabbc6bSPrashanth Sreenivasa 	(void) printf("indirect births obj %llu:\n",
10555cabbc6bSPrashanth Sreenivasa 	    (longlong_t)vic->vic_births_object);
10565cabbc6bSPrashanth Sreenivasa 	(void) printf("    vib_count = %llu\n",
10575cabbc6bSPrashanth Sreenivasa 	    (longlong_t)vdev_indirect_births_count(vib));
10585cabbc6bSPrashanth Sreenivasa 	for (uint64_t i = 0; i < vdev_indirect_births_count(vib); i++) {
10595cabbc6bSPrashanth Sreenivasa 		vdev_indirect_birth_entry_phys_t *cur_vibe =
10605cabbc6bSPrashanth Sreenivasa 		    &vib->vib_entries[i];
10615cabbc6bSPrashanth Sreenivasa 		(void) printf("\toffset %llx -> txg %llu\n",
10625cabbc6bSPrashanth Sreenivasa 		    (longlong_t)cur_vibe->vibe_offset,
10635cabbc6bSPrashanth Sreenivasa 		    (longlong_t)cur_vibe->vibe_phys_birth_txg);
10645cabbc6bSPrashanth Sreenivasa 	}
10655cabbc6bSPrashanth Sreenivasa 	(void) printf("\n");
10665cabbc6bSPrashanth Sreenivasa 
10675cabbc6bSPrashanth Sreenivasa 	(void) printf("indirect mapping obj %llu:\n",
10685cabbc6bSPrashanth Sreenivasa 	    (longlong_t)vic->vic_mapping_object);
10695cabbc6bSPrashanth Sreenivasa 	(void) printf("    vim_max_offset = 0x%llx\n",
10705cabbc6bSPrashanth Sreenivasa 	    (longlong_t)vdev_indirect_mapping_max_offset(vim));
10715cabbc6bSPrashanth Sreenivasa 	(void) printf("    vim_bytes_mapped = 0x%llx\n",
10725cabbc6bSPrashanth Sreenivasa 	    (longlong_t)vdev_indirect_mapping_bytes_mapped(vim));
10735cabbc6bSPrashanth Sreenivasa 	(void) printf("    vim_count = %llu\n",
10745cabbc6bSPrashanth Sreenivasa 	    (longlong_t)vdev_indirect_mapping_num_entries(vim));
10755cabbc6bSPrashanth Sreenivasa 
10765cabbc6bSPrashanth Sreenivasa 	if (dump_opt['d'] <= 5 && dump_opt['m'] <= 3)
10775cabbc6bSPrashanth Sreenivasa 		return;
10785cabbc6bSPrashanth Sreenivasa 
10795cabbc6bSPrashanth Sreenivasa 	uint32_t *counts = vdev_indirect_mapping_load_obsolete_counts(vim);
10805cabbc6bSPrashanth Sreenivasa 
10815cabbc6bSPrashanth Sreenivasa 	for (uint64_t i = 0; i < vdev_indirect_mapping_num_entries(vim); i++) {
10825cabbc6bSPrashanth Sreenivasa 		vdev_indirect_mapping_entry_phys_t *vimep =
10835cabbc6bSPrashanth Sreenivasa 		    &vim->vim_entries[i];
10845cabbc6bSPrashanth Sreenivasa 		(void) printf("\t<%llx:%llx:%llx> -> "
10855cabbc6bSPrashanth Sreenivasa 		    "<%llx:%llx:%llx> (%x obsolete)\n",
10865cabbc6bSPrashanth Sreenivasa 		    (longlong_t)vd->vdev_id,
10875cabbc6bSPrashanth Sreenivasa 		    (longlong_t)DVA_MAPPING_GET_SRC_OFFSET(vimep),
10885cabbc6bSPrashanth Sreenivasa 		    (longlong_t)DVA_GET_ASIZE(&vimep->vimep_dst),
10895cabbc6bSPrashanth Sreenivasa 		    (longlong_t)DVA_GET_VDEV(&vimep->vimep_dst),
10905cabbc6bSPrashanth Sreenivasa 		    (longlong_t)DVA_GET_OFFSET(&vimep->vimep_dst),
10915cabbc6bSPrashanth Sreenivasa 		    (longlong_t)DVA_GET_ASIZE(&vimep->vimep_dst),
10925cabbc6bSPrashanth Sreenivasa 		    counts[i]);
10935cabbc6bSPrashanth Sreenivasa 	}
10945cabbc6bSPrashanth Sreenivasa 	(void) printf("\n");
10955cabbc6bSPrashanth Sreenivasa 
10965cabbc6bSPrashanth Sreenivasa 	uint64_t obsolete_sm_object = vdev_obsolete_sm_object(vd);
10975cabbc6bSPrashanth Sreenivasa 	if (obsolete_sm_object != 0) {
10985cabbc6bSPrashanth Sreenivasa 		objset_t *mos = vd->vdev_spa->spa_meta_objset;
10995cabbc6bSPrashanth Sreenivasa 		(void) printf("obsolete space map object %llu:\n",
11005cabbc6bSPrashanth Sreenivasa 		    (u_longlong_t)obsolete_sm_object);
11015cabbc6bSPrashanth Sreenivasa 		ASSERT(vd->vdev_obsolete_sm != NULL);
11025cabbc6bSPrashanth Sreenivasa 		ASSERT3U(space_map_object(vd->vdev_obsolete_sm), ==,
11035cabbc6bSPrashanth Sreenivasa 		    obsolete_sm_object);
11045cabbc6bSPrashanth Sreenivasa 		dump_spacemap(mos, vd->vdev_obsolete_sm);
11055cabbc6bSPrashanth Sreenivasa 		(void) printf("\n");
11065cabbc6bSPrashanth Sreenivasa 	}
11075cabbc6bSPrashanth Sreenivasa }
11085cabbc6bSPrashanth Sreenivasa 
1109fa9e4066Sahrens static void
1110fa9e4066Sahrens dump_metaslabs(spa_t *spa)
1111fa9e4066Sahrens {
111287219db7SVictor Latushkin 	vdev_t *vd, *rvd = spa->spa_root_vdev;
111387219db7SVictor Latushkin 	uint64_t m, c = 0, children = rvd->vdev_children;
1114fa9e4066Sahrens 
1115fa9e4066Sahrens 	(void) printf("\nMetaslabs:\n");
1116fa9e4066Sahrens 
111787219db7SVictor Latushkin 	if (!dump_opt['d'] && zopt_objects > 0) {
111887219db7SVictor Latushkin 		c = zopt_object[0];
111987219db7SVictor Latushkin 
112087219db7SVictor Latushkin 		if (c >= children)
112187219db7SVictor Latushkin 			(void) fatal("bad vdev id: %llu", (u_longlong_t)c);
1122fa9e4066Sahrens 
112387219db7SVictor Latushkin 		if (zopt_objects > 1) {
112487219db7SVictor Latushkin 			vd = rvd->vdev_child[c];
112587219db7SVictor Latushkin 			print_vdev_metaslab_header(vd);
112687219db7SVictor Latushkin 
112787219db7SVictor Latushkin 			for (m = 1; m < zopt_objects; m++) {
112887219db7SVictor Latushkin 				if (zopt_object[m] < vd->vdev_ms_count)
112987219db7SVictor Latushkin 					dump_metaslab(
113087219db7SVictor Latushkin 					    vd->vdev_ms[zopt_object[m]]);
113187219db7SVictor Latushkin 				else
113287219db7SVictor Latushkin 					(void) fprintf(stderr, "bad metaslab "
113387219db7SVictor Latushkin 					    "number %llu\n",
113487219db7SVictor Latushkin 					    (u_longlong_t)zopt_object[m]);
113587219db7SVictor Latushkin 			}
113687219db7SVictor Latushkin 			(void) printf("\n");
113787219db7SVictor Latushkin 			return;
113887219db7SVictor Latushkin 		}
113987219db7SVictor Latushkin 		children = c + 1;
114087219db7SVictor Latushkin 	}
114187219db7SVictor Latushkin 	for (; c < children; c++) {
114287219db7SVictor Latushkin 		vd = rvd->vdev_child[c];
114387219db7SVictor Latushkin 		print_vdev_metaslab_header(vd);
1144fa9e4066Sahrens 
11455cabbc6bSPrashanth Sreenivasa 		print_vdev_indirect(vd);
11465cabbc6bSPrashanth Sreenivasa 
1147fa9e4066Sahrens 		for (m = 0; m < vd->vdev_ms_count; m++)
1148fa9e4066Sahrens 			dump_metaslab(vd->vdev_ms[m]);
1149fa9e4066Sahrens 		(void) printf("\n");
1150fa9e4066Sahrens 	}
1151fa9e4066Sahrens }
1152fa9e4066Sahrens 
1153814dcd43SSerapheim Dimitropoulos static void
1154814dcd43SSerapheim Dimitropoulos dump_log_spacemaps(spa_t *spa)
1155814dcd43SSerapheim Dimitropoulos {
1156814dcd43SSerapheim Dimitropoulos 	if (!spa_feature_is_active(spa, SPA_FEATURE_LOG_SPACEMAP))
1157814dcd43SSerapheim Dimitropoulos 		return;
1158814dcd43SSerapheim Dimitropoulos 
1159814dcd43SSerapheim Dimitropoulos 	(void) printf("\nLog Space Maps in Pool:\n");
1160814dcd43SSerapheim Dimitropoulos 	for (spa_log_sm_t *sls = avl_first(&spa->spa_sm_logs_by_txg);
1161814dcd43SSerapheim Dimitropoulos 	    sls; sls = AVL_NEXT(&spa->spa_sm_logs_by_txg, sls)) {
1162814dcd43SSerapheim Dimitropoulos 		space_map_t *sm = NULL;
1163814dcd43SSerapheim Dimitropoulos 		VERIFY0(space_map_open(&sm, spa_meta_objset(spa),
1164814dcd43SSerapheim Dimitropoulos 		    sls->sls_sm_obj, 0, UINT64_MAX, SPA_MINBLOCKSHIFT));
1165814dcd43SSerapheim Dimitropoulos 
1166814dcd43SSerapheim Dimitropoulos 		(void) printf("Log Spacemap object %llu txg %llu\n",
1167814dcd43SSerapheim Dimitropoulos 		    (u_longlong_t)sls->sls_sm_obj, (u_longlong_t)sls->sls_txg);
1168814dcd43SSerapheim Dimitropoulos 		dump_spacemap(spa->spa_meta_objset, sm);
1169814dcd43SSerapheim Dimitropoulos 		space_map_close(sm);
1170814dcd43SSerapheim Dimitropoulos 	}
1171814dcd43SSerapheim Dimitropoulos 	(void) printf("\n");
1172814dcd43SSerapheim Dimitropoulos }
1173814dcd43SSerapheim Dimitropoulos 
1174b24ab676SJeff Bonwick static void
1175b24ab676SJeff Bonwick dump_dde(const ddt_t *ddt, const ddt_entry_t *dde, uint64_t index)
1176b24ab676SJeff Bonwick {
1177b24ab676SJeff Bonwick 	const ddt_phys_t *ddp = dde->dde_phys;
1178b24ab676SJeff Bonwick 	const ddt_key_t *ddk = &dde->dde_key;
11793f7978d0SAlan Somers 	const char *types[4] = { "ditto", "single", "double", "triple" };
1180b24ab676SJeff Bonwick 	char blkbuf[BP_SPRINTF_LEN];
1181b24ab676SJeff Bonwick 	blkptr_t blk;
1182b24ab676SJeff Bonwick 
1183b24ab676SJeff Bonwick 	for (int p = 0; p < DDT_PHYS_TYPES; p++, ddp++) {
1184b24ab676SJeff Bonwick 		if (ddp->ddp_phys_birth == 0)
1185b24ab676SJeff Bonwick 			continue;
1186bbfd46c4SJeff Bonwick 		ddt_bp_create(ddt->ddt_checksum, ddk, ddp, &blk);
118743466aaeSMax Grossman 		snprintf_blkptr(blkbuf, sizeof (blkbuf), &blk);
1188b24ab676SJeff Bonwick 		(void) printf("index %llx refcnt %llu %s %s\n",
1189b24ab676SJeff Bonwick 		    (u_longlong_t)index, (u_longlong_t)ddp->ddp_refcnt,
1190b24ab676SJeff Bonwick 		    types[p], blkbuf);
1191b24ab676SJeff Bonwick 	}
1192b24ab676SJeff Bonwick }
1193b24ab676SJeff Bonwick 
1194b24ab676SJeff Bonwick static void
1195b24ab676SJeff Bonwick dump_dedup_ratio(const ddt_stat_t *dds)
1196b24ab676SJeff Bonwick {
1197b24ab676SJeff Bonwick 	double rL, rP, rD, D, dedup, compress, copies;
1198b24ab676SJeff Bonwick 
1199b24ab676SJeff Bonwick 	if (dds->dds_blocks == 0)
1200b24ab676SJeff Bonwick 		return;
1201b24ab676SJeff Bonwick 
1202b24ab676SJeff Bonwick 	rL = (double)dds->dds_ref_lsize;
1203b24ab676SJeff Bonwick 	rP = (double)dds->dds_ref_psize;
1204b24ab676SJeff Bonwick 	rD = (double)dds->dds_ref_dsize;
1205b24ab676SJeff Bonwick 	D = (double)dds->dds_dsize;
1206b24ab676SJeff Bonwick 
1207b24ab676SJeff Bonwick 	dedup = rD / D;
1208b24ab676SJeff Bonwick 	compress = rL / rP;
1209b24ab676SJeff Bonwick 	copies = rD / rP;
1210b24ab676SJeff Bonwick 
1211b24ab676SJeff Bonwick 	(void) printf("dedup = %.2f, compress = %.2f, copies = %.2f, "
1212b24ab676SJeff Bonwick 	    "dedup * compress / copies = %.2f\n\n",
1213b24ab676SJeff Bonwick 	    dedup, compress, copies, dedup * compress / copies);
1214b24ab676SJeff Bonwick }
1215b24ab676SJeff Bonwick 
1216b24ab676SJeff Bonwick static void
1217b24ab676SJeff Bonwick dump_ddt(ddt_t *ddt, enum ddt_type type, enum ddt_class class)
1218b24ab676SJeff Bonwick {
1219b24ab676SJeff Bonwick 	char name[DDT_NAMELEN];
1220b24ab676SJeff Bonwick 	ddt_entry_t dde;
1221b24ab676SJeff Bonwick 	uint64_t walk = 0;
1222b24ab676SJeff Bonwick 	dmu_object_info_t doi;
1223b24ab676SJeff Bonwick 	uint64_t count, dspace, mspace;
1224b24ab676SJeff Bonwick 	int error;
1225b24ab676SJeff Bonwick 
1226b24ab676SJeff Bonwick 	error = ddt_object_info(ddt, type, class, &doi);
1227b24ab676SJeff Bonwick 
1228b24ab676SJeff Bonwick 	if (error == ENOENT)
1229b24ab676SJeff Bonwick 		return;
1230b24ab676SJeff Bonwick 	ASSERT(error == 0);
1231b24ab676SJeff Bonwick 
12327448a079SGeorge Wilson 	if ((count = ddt_object_count(ddt, type, class)) == 0)
12337448a079SGeorge Wilson 		return;
12347448a079SGeorge Wilson 
1235b24ab676SJeff Bonwick 	dspace = doi.doi_physical_blocks_512 << 9;
1236b24ab676SJeff Bonwick 	mspace = doi.doi_fill_count * doi.doi_data_block_size;
1237b24ab676SJeff Bonwick 
1238b24ab676SJeff Bonwick 	ddt_object_name(ddt, type, class, name);
1239b24ab676SJeff Bonwick 
1240b24ab676SJeff Bonwick 	(void) printf("%s: %llu entries, size %llu on disk, %llu in core\n",
1241b24ab676SJeff Bonwick 	    name,
1242b24ab676SJeff Bonwick 	    (u_longlong_t)count,
1243b24ab676SJeff Bonwick 	    (u_longlong_t)(dspace / count),
1244b24ab676SJeff Bonwick 	    (u_longlong_t)(mspace / count));
1245b24ab676SJeff Bonwick 
1246b24ab676SJeff Bonwick 	if (dump_opt['D'] < 3)
1247b24ab676SJeff Bonwick 		return;
1248b24ab676SJeff Bonwick 
12499eb19f4dSGeorge Wilson 	zpool_dump_ddt(NULL, &ddt->ddt_histogram[type][class]);
1250b24ab676SJeff Bonwick 
1251b24ab676SJeff Bonwick 	if (dump_opt['D'] < 4)
1252b24ab676SJeff Bonwick 		return;
1253b24ab676SJeff Bonwick 
1254b24ab676SJeff Bonwick 	if (dump_opt['D'] < 5 && class == DDT_CLASS_UNIQUE)
1255b24ab676SJeff Bonwick 		return;
1256b24ab676SJeff Bonwick 
1257b24ab676SJeff Bonwick 	(void) printf("%s contents:\n\n", name);
1258b24ab676SJeff Bonwick 
1259bbfd46c4SJeff Bonwick 	while ((error = ddt_object_walk(ddt, type, class, &walk, &dde)) == 0)
1260b24ab676SJeff Bonwick 		dump_dde(ddt, &dde, walk);
1261b24ab676SJeff Bonwick 
126217f11284SSerapheim Dimitropoulos 	ASSERT3U(error, ==, ENOENT);
1263b24ab676SJeff Bonwick 
1264b24ab676SJeff Bonwick 	(void) printf("\n");
1265b24ab676SJeff Bonwick }
1266b24ab676SJeff Bonwick 
1267b24ab676SJeff Bonwick static void
1268b24ab676SJeff Bonwick dump_all_ddts(spa_t *spa)
1269b24ab676SJeff Bonwick {
12703f7978d0SAlan Somers 	ddt_histogram_t ddh_total;
12713f7978d0SAlan Somers 	ddt_stat_t dds_total;
12723f7978d0SAlan Somers 
12733f7978d0SAlan Somers 	bzero(&ddh_total, sizeof (ddh_total));
12743f7978d0SAlan Somers 	bzero(&dds_total, sizeof (dds_total));
1275b24ab676SJeff Bonwick 
1276b24ab676SJeff Bonwick 	for (enum zio_checksum c = 0; c < ZIO_CHECKSUM_FUNCTIONS; c++) {
1277b24ab676SJeff Bonwick 		ddt_t *ddt = spa->spa_ddt[c];
1278b24ab676SJeff Bonwick 		for (enum ddt_type type = 0; type < DDT_TYPES; type++) {
1279b24ab676SJeff Bonwick 			for (enum ddt_class class = 0; class < DDT_CLASSES;
1280b24ab676SJeff Bonwick 			    class++) {
1281b24ab676SJeff Bonwick 				dump_ddt(ddt, type, class);
1282b24ab676SJeff Bonwick 			}
1283b24ab676SJeff Bonwick 		}
1284b24ab676SJeff Bonwick 	}
1285b24ab676SJeff Bonwick 
12869eb19f4dSGeorge Wilson 	ddt_get_dedup_stats(spa, &dds_total);
1287b24ab676SJeff Bonwick 
1288b24ab676SJeff Bonwick 	if (dds_total.dds_blocks == 0) {
1289b24ab676SJeff Bonwick 		(void) printf("All DDTs are empty\n");
1290b24ab676SJeff Bonwick 		return;
1291b24ab676SJeff Bonwick 	}
1292b24ab676SJeff Bonwick 
1293b24ab676SJeff Bonwick 	(void) printf("\n");
1294b24ab676SJeff Bonwick 
1295b24ab676SJeff Bonwick 	if (dump_opt['D'] > 1) {
1296b24ab676SJeff Bonwick 		(void) printf("DDT histogram (aggregated over all DDTs):\n");
12979eb19f4dSGeorge Wilson 		ddt_get_dedup_histogram(spa, &ddh_total);
12989eb19f4dSGeorge Wilson 		zpool_dump_ddt(&dds_total, &ddh_total);
1299b24ab676SJeff Bonwick 	}
1300b24ab676SJeff Bonwick 
1301b24ab676SJeff Bonwick 	dump_dedup_ratio(&dds_total);
1302b24ab676SJeff Bonwick }
1303b24ab676SJeff Bonwick 
13048ad4d6ddSJeff Bonwick static void
13050713e232SGeorge Wilson dump_dtl_seg(void *arg, uint64_t start, uint64_t size)
13068ad4d6ddSJeff Bonwick {
13070713e232SGeorge Wilson 	char *prefix = arg;
13088ad4d6ddSJeff Bonwick 
13098ad4d6ddSJeff Bonwick 	(void) printf("%s [%llu,%llu) length %llu\n",
13108ad4d6ddSJeff Bonwick 	    prefix,
13118ad4d6ddSJeff Bonwick 	    (u_longlong_t)start,
13128ad4d6ddSJeff Bonwick 	    (u_longlong_t)(start + size),
13138ad4d6ddSJeff Bonwick 	    (u_longlong_t)(size));
13148ad4d6ddSJeff Bonwick }
13158ad4d6ddSJeff Bonwick 
1316fa9e4066Sahrens static void
1317fa9e4066Sahrens dump_dtl(vdev_t *vd, int indent)
1318fa9e4066Sahrens {
13198ad4d6ddSJeff Bonwick 	spa_t *spa = vd->vdev_spa;
13208ad4d6ddSJeff Bonwick 	boolean_t required;
13213f7978d0SAlan Somers 	const char *name[DTL_TYPES] = { "missing", "partial", "scrub",
13223f7978d0SAlan Somers 		"outage" };
13238ad4d6ddSJeff Bonwick 	char prefix[256];
13248ad4d6ddSJeff Bonwick 
13258f18d1faSGeorge Wilson 	spa_vdev_state_enter(spa, SCL_NONE);
13268ad4d6ddSJeff Bonwick 	required = vdev_dtl_required(vd);
13278ad4d6ddSJeff Bonwick 	(void) spa_vdev_state_exit(spa, NULL, 0);
1328fa9e4066Sahrens 
1329fa9e4066Sahrens 	if (indent == 0)
1330fa9e4066Sahrens 		(void) printf("\nDirty time logs:\n\n");
1331fa9e4066Sahrens 
13328ad4d6ddSJeff Bonwick 	(void) printf("\t%*s%s [%s]\n", indent, "",
1333e14bb325SJeff Bonwick 	    vd->vdev_path ? vd->vdev_path :
13348ad4d6ddSJeff Bonwick 	    vd->vdev_parent ? vd->vdev_ops->vdev_op_type : spa_name(spa),
13358ad4d6ddSJeff Bonwick 	    required ? "DTL-required" : "DTL-expendable");
1336fa9e4066Sahrens 
13378ad4d6ddSJeff Bonwick 	for (int t = 0; t < DTL_TYPES; t++) {
13380713e232SGeorge Wilson 		range_tree_t *rt = vd->vdev_dtl[t];
13390713e232SGeorge Wilson 		if (range_tree_space(rt) == 0)
13408ad4d6ddSJeff Bonwick 			continue;
13418ad4d6ddSJeff Bonwick 		(void) snprintf(prefix, sizeof (prefix), "\t%*s%s",
13428ad4d6ddSJeff Bonwick 		    indent + 2, "", name[t]);
13430713e232SGeorge Wilson 		range_tree_walk(rt, dump_dtl_seg, prefix);
13448ad4d6ddSJeff Bonwick 		if (dump_opt['d'] > 5 && vd->vdev_children == 0)
13450713e232SGeorge Wilson 			dump_spacemap(spa->spa_meta_objset, vd->vdev_dtl_sm);
1346fa9e4066Sahrens 	}
1347fa9e4066Sahrens 
13483f7978d0SAlan Somers 	for (unsigned c = 0; c < vd->vdev_children; c++)
1349fa9e4066Sahrens 		dump_dtl(vd->vdev_child[c], indent + 4);
1350fa9e4066Sahrens }
1351fa9e4066Sahrens 
13528f18d1faSGeorge Wilson static void
13538f18d1faSGeorge Wilson dump_history(spa_t *spa)
13548f18d1faSGeorge Wilson {
13558f18d1faSGeorge Wilson 	nvlist_t **events = NULL;
1356e4161df6SVictor Latushkin 	uint64_t resid, len, off = 0;
13578f18d1faSGeorge Wilson 	uint_t num = 0;
13588f18d1faSGeorge Wilson 	int error;
13598f18d1faSGeorge Wilson 	time_t tsec;
13608f18d1faSGeorge Wilson 	struct tm t;
13618f18d1faSGeorge Wilson 	char tbuf[30];
13628f18d1faSGeorge Wilson 	char internalstr[MAXPATHLEN];
13638f18d1faSGeorge Wilson 
13643f84190cSMatthew Ahrens 	char *buf = umem_alloc(SPA_MAXBLOCKSIZE, UMEM_NOFAIL);
13658f18d1faSGeorge Wilson 	do {
13663f84190cSMatthew Ahrens 		len = SPA_MAXBLOCKSIZE;
1367e4161df6SVictor Latushkin 
13688f18d1faSGeorge Wilson 		if ((error = spa_history_get(spa, &off, &len, buf)) != 0) {
13698f18d1faSGeorge Wilson 			(void) fprintf(stderr, "Unable to read history: "
13708f18d1faSGeorge Wilson 			    "error %d\n", error);
13713f84190cSMatthew Ahrens 			umem_free(buf, SPA_MAXBLOCKSIZE);
13728f18d1faSGeorge Wilson 			return;
13738f18d1faSGeorge Wilson 		}
13748f18d1faSGeorge Wilson 
13758f18d1faSGeorge Wilson 		if (zpool_history_unpack(buf, len, &resid, &events, &num) != 0)
13768f18d1faSGeorge Wilson 			break;
13778f18d1faSGeorge Wilson 
13788f18d1faSGeorge Wilson 		off -= resid;
13798f18d1faSGeorge Wilson 	} while (len != 0);
13803f84190cSMatthew Ahrens 	umem_free(buf, SPA_MAXBLOCKSIZE);
13818f18d1faSGeorge Wilson 
13828f18d1faSGeorge Wilson 	(void) printf("\nHistory:\n");
13833f7978d0SAlan Somers 	for (unsigned i = 0; i < num; i++) {
13848f18d1faSGeorge Wilson 		uint64_t time, txg, ievent;
13858f18d1faSGeorge Wilson 		char *cmd, *intstr;
13864445fffbSMatthew Ahrens 		boolean_t printed = B_FALSE;
13878f18d1faSGeorge Wilson 
13888f18d1faSGeorge Wilson 		if (nvlist_lookup_uint64(events[i], ZPOOL_HIST_TIME,
13898f18d1faSGeorge Wilson 		    &time) != 0)
13904445fffbSMatthew Ahrens 			goto next;
13918f18d1faSGeorge Wilson 		if (nvlist_lookup_string(events[i], ZPOOL_HIST_CMD,
13928f18d1faSGeorge Wilson 		    &cmd) != 0) {
13938f18d1faSGeorge Wilson 			if (nvlist_lookup_uint64(events[i],
13948f18d1faSGeorge Wilson 			    ZPOOL_HIST_INT_EVENT, &ievent) != 0)
13954445fffbSMatthew Ahrens 				goto next;
13968f18d1faSGeorge Wilson 			verify(nvlist_lookup_uint64(events[i],
13978f18d1faSGeorge Wilson 			    ZPOOL_HIST_TXG, &txg) == 0);
13988f18d1faSGeorge Wilson 			verify(nvlist_lookup_string(events[i],
13998f18d1faSGeorge Wilson 			    ZPOOL_HIST_INT_STR, &intstr) == 0);
14004445fffbSMatthew Ahrens 			if (ievent >= ZFS_NUM_LEGACY_HISTORY_EVENTS)
14014445fffbSMatthew Ahrens 				goto next;
14028f18d1faSGeorge Wilson 
14038f18d1faSGeorge Wilson 			(void) snprintf(internalstr,
14048f18d1faSGeorge Wilson 			    sizeof (internalstr),
14053f7978d0SAlan Somers 			    "[internal %s txg:%ju] %s",
14063f7978d0SAlan Somers 			    zfs_history_event_names[ievent], (uintmax_t)txg,
14078f18d1faSGeorge Wilson 			    intstr);
14088f18d1faSGeorge Wilson 			cmd = internalstr;
14098f18d1faSGeorge Wilson 		}
14108f18d1faSGeorge Wilson 		tsec = time;
14118f18d1faSGeorge Wilson 		(void) localtime_r(&tsec, &t);
14128f18d1faSGeorge Wilson 		(void) strftime(tbuf, sizeof (tbuf), "%F.%T", &t);
14138f18d1faSGeorge Wilson 		(void) printf("%s %s\n", tbuf, cmd);
14144445fffbSMatthew Ahrens 		printed = B_TRUE;
14154445fffbSMatthew Ahrens 
14164445fffbSMatthew Ahrens next:
14174445fffbSMatthew Ahrens 		if (dump_opt['h'] > 1) {
14184445fffbSMatthew Ahrens 			if (!printed)
14194445fffbSMatthew Ahrens 				(void) printf("unrecognized record:\n");
14204445fffbSMatthew Ahrens 			dump_nvlist(events[i], 2);
14214445fffbSMatthew Ahrens 		}
14228f18d1faSGeorge Wilson 	}
14238f18d1faSGeorge Wilson }
14248f18d1faSGeorge Wilson 
1425fa9e4066Sahrens /*ARGSUSED*/
1426fa9e4066Sahrens static void
1427fa9e4066Sahrens dump_dnode(objset_t *os, uint64_t object, void *data, size_t size)
1428fa9e4066Sahrens {
1429fa9e4066Sahrens }
1430fa9e4066Sahrens 
1431fa9e4066Sahrens static uint64_t
14327802d7bfSMatthew Ahrens blkid2offset(const dnode_phys_t *dnp, const blkptr_t *bp,
14337802d7bfSMatthew Ahrens     const zbookmark_phys_t *zb)
1434fa9e4066Sahrens {
1435b24ab676SJeff Bonwick 	if (dnp == NULL) {
1436b24ab676SJeff Bonwick 		ASSERT(zb->zb_level < 0);
1437b24ab676SJeff Bonwick 		if (zb->zb_object == 0)
1438b24ab676SJeff Bonwick 			return (zb->zb_blkid);
1439b24ab676SJeff Bonwick 		return (zb->zb_blkid * BP_GET_LSIZE(bp));
1440b24ab676SJeff Bonwick 	}
1441b24ab676SJeff Bonwick 
1442b24ab676SJeff Bonwick 	ASSERT(zb->zb_level >= 0);
1443fa9e4066Sahrens 
1444b24ab676SJeff Bonwick 	return ((zb->zb_blkid <<
1445b24ab676SJeff Bonwick 	    (zb->zb_level * (dnp->dn_indblkshift - SPA_BLKPTRSHIFT))) *
1446fa9e4066Sahrens 	    dnp->dn_datablkszsec << SPA_MINBLOCKSHIFT);
1447fa9e4066Sahrens }
1448fa9e4066Sahrens 
144944cd46caSbillm static void
145043466aaeSMax Grossman snprintf_blkptr_compact(char *blkbuf, size_t buflen, const blkptr_t *bp)
145144cd46caSbillm {
1452cde58dbcSMatthew Ahrens 	const dva_t *dva = bp->blk_dva;
1453eb633035STom Caputi 	unsigned int ndvas = dump_opt['d'] > 5 ? BP_GET_NDVAS(bp) : 1;
1454b24ab676SJeff Bonwick 
1455490d05b9SMatthew Ahrens 	if (dump_opt['b'] >= 6) {
145643466aaeSMax Grossman 		snprintf_blkptr(blkbuf, buflen, bp);
1457b24ab676SJeff Bonwick 		return;
1458b24ab676SJeff Bonwick 	}
145944cd46caSbillm 
14605d7b4d43SMatthew Ahrens 	if (BP_IS_EMBEDDED(bp)) {
14615d7b4d43SMatthew Ahrens 		(void) sprintf(blkbuf,
14625d7b4d43SMatthew Ahrens 		    "EMBEDDED et=%u %llxL/%llxP B=%llu",
14635d7b4d43SMatthew Ahrens 		    (int)BPE_GET_ETYPE(bp),
14645d7b4d43SMatthew Ahrens 		    (u_longlong_t)BPE_GET_LSIZE(bp),
14655d7b4d43SMatthew Ahrens 		    (u_longlong_t)BPE_GET_PSIZE(bp),
14665d7b4d43SMatthew Ahrens 		    (u_longlong_t)bp->blk_birth);
14675d7b4d43SMatthew Ahrens 		return;
14685d7b4d43SMatthew Ahrens 	}
146944cd46caSbillm 
14705d7b4d43SMatthew Ahrens 	blkbuf[0] = '\0';
1471eb633035STom Caputi 	for (unsigned int i = 0; i < ndvas; i++)
147243466aaeSMax Grossman 		(void) snprintf(blkbuf + strlen(blkbuf),
147343466aaeSMax Grossman 		    buflen - strlen(blkbuf), "%llu:%llx:%llx ",
147444cd46caSbillm 		    (u_longlong_t)DVA_GET_VDEV(&dva[i]),
147544cd46caSbillm 		    (u_longlong_t)DVA_GET_OFFSET(&dva[i]),
147644cd46caSbillm 		    (u_longlong_t)DVA_GET_ASIZE(&dva[i]));
147744cd46caSbillm 
147843466aaeSMax Grossman 	if (BP_IS_HOLE(bp)) {
147943466aaeSMax Grossman 		(void) snprintf(blkbuf + strlen(blkbuf),
148070163ac5SPrakash Surya 		    buflen - strlen(blkbuf),
148170163ac5SPrakash Surya 		    "%llxL B=%llu",
148270163ac5SPrakash Surya 		    (u_longlong_t)BP_GET_LSIZE(bp),
148343466aaeSMax Grossman 		    (u_longlong_t)bp->blk_birth);
148443466aaeSMax Grossman 	} else {
148543466aaeSMax Grossman 		(void) snprintf(blkbuf + strlen(blkbuf),
148643466aaeSMax Grossman 		    buflen - strlen(blkbuf),
148743466aaeSMax Grossman 		    "%llxL/%llxP F=%llu B=%llu/%llu",
148843466aaeSMax Grossman 		    (u_longlong_t)BP_GET_LSIZE(bp),
148943466aaeSMax Grossman 		    (u_longlong_t)BP_GET_PSIZE(bp),
14905d7b4d43SMatthew Ahrens 		    (u_longlong_t)BP_GET_FILL(bp),
149143466aaeSMax Grossman 		    (u_longlong_t)bp->blk_birth,
149243466aaeSMax Grossman 		    (u_longlong_t)BP_PHYSICAL_BIRTH(bp));
149343466aaeSMax Grossman 	}
149444cd46caSbillm }
149544cd46caSbillm 
149688b7b0f2SMatthew Ahrens static void
14977802d7bfSMatthew Ahrens print_indirect(blkptr_t *bp, const zbookmark_phys_t *zb,
149888b7b0f2SMatthew Ahrens     const dnode_phys_t *dnp)
1499fa9e4066Sahrens {
150088b7b0f2SMatthew Ahrens 	char blkbuf[BP_SPRINTF_LEN];
1501fa9e4066Sahrens 	int l;
1502fa9e4066Sahrens 
15035d7b4d43SMatthew Ahrens 	if (!BP_IS_EMBEDDED(bp)) {
15045d7b4d43SMatthew Ahrens 		ASSERT3U(BP_GET_TYPE(bp), ==, dnp->dn_type);
15055d7b4d43SMatthew Ahrens 		ASSERT3U(BP_GET_LEVEL(bp), ==, zb->zb_level);
15065d7b4d43SMatthew Ahrens 	}
1507fa9e4066Sahrens 
1508b24ab676SJeff Bonwick 	(void) printf("%16llx ", (u_longlong_t)blkid2offset(dnp, bp, zb));
1509fa9e4066Sahrens 
1510fa9e4066Sahrens 	ASSERT(zb->zb_level >= 0);
1511fa9e4066Sahrens 
1512fa9e4066Sahrens 	for (l = dnp->dn_nlevels - 1; l >= -1; l--) {
1513fa9e4066Sahrens 		if (l == zb->zb_level) {
151488b7b0f2SMatthew Ahrens 			(void) printf("L%llx", (u_longlong_t)zb->zb_level);
1515fa9e4066Sahrens 		} else {
151688b7b0f2SMatthew Ahrens 			(void) printf(" ");
1517fa9e4066Sahrens 		}
1518fa9e4066Sahrens 	}
1519fa9e4066Sahrens 
152043466aaeSMax Grossman 	snprintf_blkptr_compact(blkbuf, sizeof (blkbuf), bp);
152188b7b0f2SMatthew Ahrens 	(void) printf("%s\n", blkbuf);
152288b7b0f2SMatthew Ahrens }
152388b7b0f2SMatthew Ahrens 
152488b7b0f2SMatthew Ahrens static int
152588b7b0f2SMatthew Ahrens visit_indirect(spa_t *spa, const dnode_phys_t *dnp,
15267802d7bfSMatthew Ahrens     blkptr_t *bp, const zbookmark_phys_t *zb)
152788b7b0f2SMatthew Ahrens {
1528e4161df6SVictor Latushkin 	int err = 0;
152988b7b0f2SMatthew Ahrens 
153088b7b0f2SMatthew Ahrens 	if (bp->blk_birth == 0)
153188b7b0f2SMatthew Ahrens 		return (0);
153288b7b0f2SMatthew Ahrens 
153388b7b0f2SMatthew Ahrens 	print_indirect(bp, zb, dnp);
153488b7b0f2SMatthew Ahrens 
153543466aaeSMax Grossman 	if (BP_GET_LEVEL(bp) > 0 && !BP_IS_HOLE(bp)) {
15367adb730bSGeorge Wilson 		arc_flags_t flags = ARC_FLAG_WAIT;
153788b7b0f2SMatthew Ahrens 		int i;
153888b7b0f2SMatthew Ahrens 		blkptr_t *cbp;
153988b7b0f2SMatthew Ahrens 		int epb = BP_GET_LSIZE(bp) >> SPA_BLKPTRSHIFT;
154088b7b0f2SMatthew Ahrens 		arc_buf_t *buf;
154188b7b0f2SMatthew Ahrens 		uint64_t fill = 0;
154288b7b0f2SMatthew Ahrens 
15431b912ec7SGeorge Wilson 		err = arc_read(NULL, spa, bp, arc_getbuf_func, &buf,
154488b7b0f2SMatthew Ahrens 		    ZIO_PRIORITY_ASYNC_READ, ZIO_FLAG_CANFAIL, &flags, zb);
154588b7b0f2SMatthew Ahrens 		if (err)
154688b7b0f2SMatthew Ahrens 			return (err);
15473f9d6ad7SLin Ling 		ASSERT(buf->b_data);
154888b7b0f2SMatthew Ahrens 
154988b7b0f2SMatthew Ahrens 		/* recursively visit blocks below this */
155088b7b0f2SMatthew Ahrens 		cbp = buf->b_data;
155188b7b0f2SMatthew Ahrens 		for (i = 0; i < epb; i++, cbp++) {
15527802d7bfSMatthew Ahrens 			zbookmark_phys_t czb;
155388b7b0f2SMatthew Ahrens 
155488b7b0f2SMatthew Ahrens 			SET_BOOKMARK(&czb, zb->zb_objset, zb->zb_object,
155588b7b0f2SMatthew Ahrens 			    zb->zb_level - 1,
155688b7b0f2SMatthew Ahrens 			    zb->zb_blkid * epb + i);
155788b7b0f2SMatthew Ahrens 			err = visit_indirect(spa, dnp, cbp, &czb);
155888b7b0f2SMatthew Ahrens 			if (err)
155988b7b0f2SMatthew Ahrens 				break;
15605d7b4d43SMatthew Ahrens 			fill += BP_GET_FILL(cbp);
156188b7b0f2SMatthew Ahrens 		}
15628ad4d6ddSJeff Bonwick 		if (!err)
15635d7b4d43SMatthew Ahrens 			ASSERT3U(fill, ==, BP_GET_FILL(bp));
1564dcbf3bd6SGeorge Wilson 		arc_buf_destroy(buf, &buf);
1565fa9e4066Sahrens 	}
1566fa9e4066Sahrens 
156788b7b0f2SMatthew Ahrens 	return (err);
1568fa9e4066Sahrens }
1569fa9e4066Sahrens 
1570fa9e4066Sahrens /*ARGSUSED*/
1571fa9e4066Sahrens static void
157288b7b0f2SMatthew Ahrens dump_indirect(dnode_t *dn)
1573fa9e4066Sahrens {
157488b7b0f2SMatthew Ahrens 	dnode_phys_t *dnp = dn->dn_phys;
157588b7b0f2SMatthew Ahrens 	int j;
15767802d7bfSMatthew Ahrens 	zbookmark_phys_t czb;
1577fa9e4066Sahrens 
1578fa9e4066Sahrens 	(void) printf("Indirect blocks:\n");
1579fa9e4066Sahrens 
1580503ad85cSMatthew Ahrens 	SET_BOOKMARK(&czb, dmu_objset_id(dn->dn_objset),
158188b7b0f2SMatthew Ahrens 	    dn->dn_object, dnp->dn_nlevels - 1, 0);
158288b7b0f2SMatthew Ahrens 	for (j = 0; j < dnp->dn_nblkptr; j++) {
158388b7b0f2SMatthew Ahrens 		czb.zb_blkid = j;
1584503ad85cSMatthew Ahrens 		(void) visit_indirect(dmu_objset_spa(dn->dn_objset), dnp,
158588b7b0f2SMatthew Ahrens 		    &dnp->dn_blkptr[j], &czb);
158688b7b0f2SMatthew Ahrens 	}
1587fa9e4066Sahrens 
1588fa9e4066Sahrens 	(void) printf("\n");
1589fa9e4066Sahrens }
1590fa9e4066Sahrens 
1591fa9e4066Sahrens /*ARGSUSED*/
1592fa9e4066Sahrens static void
1593fa9e4066Sahrens dump_dsl_dir(objset_t *os, uint64_t object, void *data, size_t size)
1594fa9e4066Sahrens {
1595fa9e4066Sahrens 	dsl_dir_phys_t *dd = data;
1596fa9e4066Sahrens 	time_t crtime;
15973f9d6ad7SLin Ling 	char nice[32];
1598fa9e4066Sahrens 
15990a055120SJason King 	/* make sure nicenum has enough space */
16000a055120SJason King 	CTASSERT(sizeof (nice) >= NN_NUMBUF_SZ);
16010a055120SJason King 
1602fa9e4066Sahrens 	if (dd == NULL)
1603fa9e4066Sahrens 		return;
1604fa9e4066Sahrens 
1605da6c28aaSamw 	ASSERT3U(size, >=, sizeof (dsl_dir_phys_t));
1606fa9e4066Sahrens 
1607fa9e4066Sahrens 	crtime = dd->dd_creation_time;
1608fa9e4066Sahrens 	(void) printf("\t\tcreation_time = %s", ctime(&crtime));
1609fa9e4066Sahrens 	(void) printf("\t\thead_dataset_obj = %llu\n",
1610fa9e4066Sahrens 	    (u_longlong_t)dd->dd_head_dataset_obj);
1611fa9e4066Sahrens 	(void) printf("\t\tparent_dir_obj = %llu\n",
1612fa9e4066Sahrens 	    (u_longlong_t)dd->dd_parent_obj);
16133cb34c60Sahrens 	(void) printf("\t\torigin_obj = %llu\n",
16143cb34c60Sahrens 	    (u_longlong_t)dd->dd_origin_obj);
1615fa9e4066Sahrens 	(void) printf("\t\tchild_dir_zapobj = %llu\n",
1616fa9e4066Sahrens 	    (u_longlong_t)dd->dd_child_dir_zapobj);
16170a055120SJason King 	zdb_nicenum(dd->dd_used_bytes, nice, sizeof (nice));
161874e7dc98SMatthew Ahrens 	(void) printf("\t\tused_bytes = %s\n", nice);
16190a055120SJason King 	zdb_nicenum(dd->dd_compressed_bytes, nice, sizeof (nice));
162074e7dc98SMatthew Ahrens 	(void) printf("\t\tcompressed_bytes = %s\n", nice);
16210a055120SJason King 	zdb_nicenum(dd->dd_uncompressed_bytes, nice, sizeof (nice));
162274e7dc98SMatthew Ahrens 	(void) printf("\t\tuncompressed_bytes = %s\n", nice);
16230a055120SJason King 	zdb_nicenum(dd->dd_quota, nice, sizeof (nice));
162474e7dc98SMatthew Ahrens 	(void) printf("\t\tquota = %s\n", nice);
16250a055120SJason King 	zdb_nicenum(dd->dd_reserved, nice, sizeof (nice));
162674e7dc98SMatthew Ahrens 	(void) printf("\t\treserved = %s\n", nice);
1627fa9e4066Sahrens 	(void) printf("\t\tprops_zapobj = %llu\n",
1628fa9e4066Sahrens 	    (u_longlong_t)dd->dd_props_zapobj);
1629ecd6cf80Smarks 	(void) printf("\t\tdeleg_zapobj = %llu\n",
1630ecd6cf80Smarks 	    (u_longlong_t)dd->dd_deleg_zapobj);
163174e7dc98SMatthew Ahrens 	(void) printf("\t\tflags = %llx\n",
163274e7dc98SMatthew Ahrens 	    (u_longlong_t)dd->dd_flags);
163374e7dc98SMatthew Ahrens 
163474e7dc98SMatthew Ahrens #define	DO(which) \
16350a055120SJason King 	zdb_nicenum(dd->dd_used_breakdown[DD_USED_ ## which], nice, \
16360a055120SJason King 	    sizeof (nice)); \
163774e7dc98SMatthew Ahrens 	(void) printf("\t\tused_breakdown[" #which "] = %s\n", nice)
163874e7dc98SMatthew Ahrens 	DO(HEAD);
163974e7dc98SMatthew Ahrens 	DO(SNAP);
164074e7dc98SMatthew Ahrens 	DO(CHILD);
164174e7dc98SMatthew Ahrens 	DO(CHILD_RSRV);
164274e7dc98SMatthew Ahrens 	DO(REFRSRV);
164374e7dc98SMatthew Ahrens #undef DO
164417fb938fSMatthew Ahrens 	(void) printf("\t\tclones = %llu\n",
164517fb938fSMatthew Ahrens 	    (u_longlong_t)dd->dd_clones);
1646fa9e4066Sahrens }
1647fa9e4066Sahrens 
1648fa9e4066Sahrens /*ARGSUSED*/
1649fa9e4066Sahrens static void
1650fa9e4066Sahrens dump_dsl_dataset(objset_t *os, uint64_t object, void *data, size_t size)
1651fa9e4066Sahrens {
1652fa9e4066Sahrens 	dsl_dataset_phys_t *ds = data;
1653fa9e4066Sahrens 	time_t crtime;
16543f9d6ad7SLin Ling 	char used[32], compressed[32], uncompressed[32], unique[32];
1655fbabab8fSmaybee 	char blkbuf[BP_SPRINTF_LEN];
1656fa9e4066Sahrens 
16570a055120SJason King 	/* make sure nicenum has enough space */
16580a055120SJason King 	CTASSERT(sizeof (used) >= NN_NUMBUF_SZ);
16590a055120SJason King 	CTASSERT(sizeof (compressed) >= NN_NUMBUF_SZ);
16600a055120SJason King 	CTASSERT(sizeof (uncompressed) >= NN_NUMBUF_SZ);
16610a055120SJason King 	CTASSERT(sizeof (unique) >= NN_NUMBUF_SZ);
16620a055120SJason King 
1663fa9e4066Sahrens 	if (ds == NULL)
1664fa9e4066Sahrens 		return;
1665fa9e4066Sahrens 
1666fa9e4066Sahrens 	ASSERT(size == sizeof (*ds));
1667fa9e4066Sahrens 	crtime = ds->ds_creation_time;
16680a055120SJason King 	zdb_nicenum(ds->ds_referenced_bytes, used, sizeof (used));
16690a055120SJason King 	zdb_nicenum(ds->ds_compressed_bytes, compressed, sizeof (compressed));
16700a055120SJason King 	zdb_nicenum(ds->ds_uncompressed_bytes, uncompressed,
16710a055120SJason King 	    sizeof (uncompressed));
16720a055120SJason King 	zdb_nicenum(ds->ds_unique_bytes, unique, sizeof (unique));
167343466aaeSMax Grossman 	snprintf_blkptr(blkbuf, sizeof (blkbuf), &ds->ds_bp);
1674fa9e4066Sahrens 
1675088f3894Sahrens 	(void) printf("\t\tdir_obj = %llu\n",
1676fa9e4066Sahrens 	    (u_longlong_t)ds->ds_dir_obj);
1677fa9e4066Sahrens 	(void) printf("\t\tprev_snap_obj = %llu\n",
1678fa9e4066Sahrens 	    (u_longlong_t)ds->ds_prev_snap_obj);
1679fa9e4066Sahrens 	(void) printf("\t\tprev_snap_txg = %llu\n",
1680fa9e4066Sahrens 	    (u_longlong_t)ds->ds_prev_snap_txg);
1681fa9e4066Sahrens 	(void) printf("\t\tnext_snap_obj = %llu\n",
1682fa9e4066Sahrens 	    (u_longlong_t)ds->ds_next_snap_obj);
1683fa9e4066Sahrens 	(void) printf("\t\tsnapnames_zapobj = %llu\n",
1684fa9e4066Sahrens 	    (u_longlong_t)ds->ds_snapnames_zapobj);
1685fa9e4066Sahrens 	(void) printf("\t\tnum_children = %llu\n",
1686fa9e4066Sahrens 	    (u_longlong_t)ds->ds_num_children);
1687842727c2SChris Kirby 	(void) printf("\t\tuserrefs_obj = %llu\n",
1688842727c2SChris Kirby 	    (u_longlong_t)ds->ds_userrefs_obj);
1689fa9e4066Sahrens 	(void) printf("\t\tcreation_time = %s", ctime(&crtime));
1690fa9e4066Sahrens 	(void) printf("\t\tcreation_txg = %llu\n",
1691fa9e4066Sahrens 	    (u_longlong_t)ds->ds_creation_txg);
1692fa9e4066Sahrens 	(void) printf("\t\tdeadlist_obj = %llu\n",
1693fa9e4066Sahrens 	    (u_longlong_t)ds->ds_deadlist_obj);
1694fa9e4066Sahrens 	(void) printf("\t\tused_bytes = %s\n", used);
1695fa9e4066Sahrens 	(void) printf("\t\tcompressed_bytes = %s\n", compressed);
1696fa9e4066Sahrens 	(void) printf("\t\tuncompressed_bytes = %s\n", uncompressed);
1697fa9e4066Sahrens 	(void) printf("\t\tunique = %s\n", unique);
1698fa9e4066Sahrens 	(void) printf("\t\tfsid_guid = %llu\n",
1699fa9e4066Sahrens 	    (u_longlong_t)ds->ds_fsid_guid);
1700fa9e4066Sahrens 	(void) printf("\t\tguid = %llu\n",
1701fa9e4066Sahrens 	    (u_longlong_t)ds->ds_guid);
170299653d4eSeschrock 	(void) printf("\t\tflags = %llx\n",
170399653d4eSeschrock 	    (u_longlong_t)ds->ds_flags);
1704088f3894Sahrens 	(void) printf("\t\tnext_clones_obj = %llu\n",
1705088f3894Sahrens 	    (u_longlong_t)ds->ds_next_clones_obj);
1706bb0ade09Sahrens 	(void) printf("\t\tprops_obj = %llu\n",
1707bb0ade09Sahrens 	    (u_longlong_t)ds->ds_props_obj);
1708fa9e4066Sahrens 	(void) printf("\t\tbp = %s\n", blkbuf);
1709fa9e4066Sahrens }
1710fa9e4066Sahrens 
1711ad135b5dSChristopher Siden /* ARGSUSED */
1712ad135b5dSChristopher Siden static int
1713ad135b5dSChristopher Siden dump_bptree_cb(void *arg, const blkptr_t *bp, dmu_tx_t *tx)
1714ad135b5dSChristopher Siden {
1715ad135b5dSChristopher Siden 	char blkbuf[BP_SPRINTF_LEN];
1716ad135b5dSChristopher Siden 
1717ad135b5dSChristopher Siden 	if (bp->blk_birth != 0) {
171843466aaeSMax Grossman 		snprintf_blkptr(blkbuf, sizeof (blkbuf), bp);
1719ad135b5dSChristopher Siden 		(void) printf("\t%s\n", blkbuf);
1720ad135b5dSChristopher Siden 	}
1721ad135b5dSChristopher Siden 	return (0);
1722ad135b5dSChristopher Siden }
1723ad135b5dSChristopher Siden 
1724ad135b5dSChristopher Siden static void
17253f7978d0SAlan Somers dump_bptree(objset_t *os, uint64_t obj, const char *name)
1726ad135b5dSChristopher Siden {
1727ad135b5dSChristopher Siden 	char bytes[32];
1728ad135b5dSChristopher Siden 	bptree_phys_t *bt;
1729ad135b5dSChristopher Siden 	dmu_buf_t *db;
1730ad135b5dSChristopher Siden 
17310a055120SJason King 	/* make sure nicenum has enough space */
17320a055120SJason King 	CTASSERT(sizeof (bytes) >= NN_NUMBUF_SZ);
17330a055120SJason King 
1734ad135b5dSChristopher Siden 	if (dump_opt['d'] < 3)
1735ad135b5dSChristopher Siden 		return;
1736ad135b5dSChristopher Siden 
1737b420f3adSRichard Lowe 	VERIFY3U(0, ==, dmu_bonus_hold(os, obj, FTAG, &db));
1738ad135b5dSChristopher Siden 	bt = db->db_data;
17390a055120SJason King 	zdb_nicenum(bt->bt_bytes, bytes, sizeof (bytes));
1740ad135b5dSChristopher Siden 	(void) printf("\n    %s: %llu datasets, %s\n",
1741ad135b5dSChristopher Siden 	    name, (unsigned long long)(bt->bt_end - bt->bt_begin), bytes);
1742ad135b5dSChristopher Siden 	dmu_buf_rele(db, FTAG);
1743ad135b5dSChristopher Siden 
1744ad135b5dSChristopher Siden 	if (dump_opt['d'] < 5)
1745ad135b5dSChristopher Siden 		return;
1746ad135b5dSChristopher Siden 
1747ad135b5dSChristopher Siden 	(void) printf("\n");
1748ad135b5dSChristopher Siden 
1749ad135b5dSChristopher Siden 	(void) bptree_iterate(os, obj, B_FALSE, dump_bptree_cb, NULL, NULL);
1750ad135b5dSChristopher Siden }
1751ad135b5dSChristopher Siden 
1752cde58dbcSMatthew Ahrens /* ARGSUSED */
1753cde58dbcSMatthew Ahrens static int
1754cde58dbcSMatthew Ahrens dump_bpobj_cb(void *arg, const blkptr_t *bp, dmu_tx_t *tx)
1755cde58dbcSMatthew Ahrens {
1756cde58dbcSMatthew Ahrens 	char blkbuf[BP_SPRINTF_LEN];
1757cde58dbcSMatthew Ahrens 
1758cde58dbcSMatthew Ahrens 	ASSERT(bp->blk_birth != 0);
175943466aaeSMax Grossman 	snprintf_blkptr_compact(blkbuf, sizeof (blkbuf), bp);
1760cde58dbcSMatthew Ahrens 	(void) printf("\t%s\n", blkbuf);
1761cde58dbcSMatthew Ahrens 	return (0);
1762cde58dbcSMatthew Ahrens }
1763cde58dbcSMatthew Ahrens 
1764fa9e4066Sahrens static void
17653f7978d0SAlan Somers dump_full_bpobj(bpobj_t *bpo, const char *name, int indent)
1766fa9e4066Sahrens {
17673f9d6ad7SLin Ling 	char bytes[32];
17683f9d6ad7SLin Ling 	char comp[32];
17693f9d6ad7SLin Ling 	char uncomp[32];
1770fa9e4066Sahrens 
17710a055120SJason King 	/* make sure nicenum has enough space */
17720a055120SJason King 	CTASSERT(sizeof (bytes) >= NN_NUMBUF_SZ);
17730a055120SJason King 	CTASSERT(sizeof (comp) >= NN_NUMBUF_SZ);
17740a055120SJason King 	CTASSERT(sizeof (uncomp) >= NN_NUMBUF_SZ);
17750a055120SJason King 
1776fa9e4066Sahrens 	if (dump_opt['d'] < 3)
1777fa9e4066Sahrens 		return;
1778fa9e4066Sahrens 
17790a055120SJason King 	zdb_nicenum(bpo->bpo_phys->bpo_bytes, bytes, sizeof (bytes));
1780d0475637SMatthew Ahrens 	if (bpo->bpo_havesubobj && bpo->bpo_phys->bpo_subobjs != 0) {
17810a055120SJason King 		zdb_nicenum(bpo->bpo_phys->bpo_comp, comp, sizeof (comp));
17820a055120SJason King 		zdb_nicenum(bpo->bpo_phys->bpo_uncomp, uncomp, sizeof (uncomp));
1783d0475637SMatthew Ahrens 		(void) printf("    %*s: object %llu, %llu local blkptrs, "
1784732885fcSMatthew Ahrens 		    "%llu subobjs in object %llu, %s (%s/%s comp)\n",
1785d0475637SMatthew Ahrens 		    indent * 8, name,
1786d0475637SMatthew Ahrens 		    (u_longlong_t)bpo->bpo_object,
1787d0475637SMatthew Ahrens 		    (u_longlong_t)bpo->bpo_phys->bpo_num_blkptrs,
1788cde58dbcSMatthew Ahrens 		    (u_longlong_t)bpo->bpo_phys->bpo_num_subobjs,
1789732885fcSMatthew Ahrens 		    (u_longlong_t)bpo->bpo_phys->bpo_subobjs,
179099653d4eSeschrock 		    bytes, comp, uncomp);
1791d0475637SMatthew Ahrens 
1792d0475637SMatthew Ahrens 		for (uint64_t i = 0; i < bpo->bpo_phys->bpo_num_subobjs; i++) {
1793d0475637SMatthew Ahrens 			uint64_t subobj;
1794d0475637SMatthew Ahrens 			bpobj_t subbpo;
1795d0475637SMatthew Ahrens 			int error;
1796d0475637SMatthew Ahrens 			VERIFY0(dmu_read(bpo->bpo_os,
1797d0475637SMatthew Ahrens 			    bpo->bpo_phys->bpo_subobjs,
1798d0475637SMatthew Ahrens 			    i * sizeof (subobj), sizeof (subobj), &subobj, 0));
1799d0475637SMatthew Ahrens 			error = bpobj_open(&subbpo, bpo->bpo_os, subobj);
1800d0475637SMatthew Ahrens 			if (error != 0) {
1801d0475637SMatthew Ahrens 				(void) printf("ERROR %u while trying to open "
1802d0475637SMatthew Ahrens 				    "subobj id %llu\n",
1803d0475637SMatthew Ahrens 				    error, (u_longlong_t)subobj);
1804d0475637SMatthew Ahrens 				continue;
1805d0475637SMatthew Ahrens 			}
1806732885fcSMatthew Ahrens 			dump_full_bpobj(&subbpo, "subobj", indent + 1);
180777061867SMatthew Ahrens 			bpobj_close(&subbpo);
1808d0475637SMatthew Ahrens 		}
180999653d4eSeschrock 	} else {
1810d0475637SMatthew Ahrens 		(void) printf("    %*s: object %llu, %llu blkptrs, %s\n",
1811d0475637SMatthew Ahrens 		    indent * 8, name,
1812d0475637SMatthew Ahrens 		    (u_longlong_t)bpo->bpo_object,
1813d0475637SMatthew Ahrens 		    (u_longlong_t)bpo->bpo_phys->bpo_num_blkptrs,
1814d0475637SMatthew Ahrens 		    bytes);
181599653d4eSeschrock 	}
1816fa9e4066Sahrens 
1817cde58dbcSMatthew Ahrens 	if (dump_opt['d'] < 5)
1818fa9e4066Sahrens 		return;
1819fa9e4066Sahrens 
1820fa9e4066Sahrens 
1821d0475637SMatthew Ahrens 	if (indent == 0) {
1822d0475637SMatthew Ahrens 		(void) bpobj_iterate_nofree(bpo, dump_bpobj_cb, NULL, NULL);
1823d0475637SMatthew Ahrens 		(void) printf("\n");
1824d0475637SMatthew Ahrens 	}
1825cde58dbcSMatthew Ahrens }
182644cd46caSbillm 
182717fb938fSMatthew Ahrens static void
182817fb938fSMatthew Ahrens bpobj_count_refd(bpobj_t *bpo)
182917fb938fSMatthew Ahrens {
183017fb938fSMatthew Ahrens 	mos_obj_refd(bpo->bpo_object);
183117fb938fSMatthew Ahrens 
183217fb938fSMatthew Ahrens 	if (bpo->bpo_havesubobj && bpo->bpo_phys->bpo_subobjs != 0) {
183317fb938fSMatthew Ahrens 		mos_obj_refd(bpo->bpo_phys->bpo_subobjs);
183417fb938fSMatthew Ahrens 		for (uint64_t i = 0; i < bpo->bpo_phys->bpo_num_subobjs; i++) {
183517fb938fSMatthew Ahrens 			uint64_t subobj;
183617fb938fSMatthew Ahrens 			bpobj_t subbpo;
183717fb938fSMatthew Ahrens 			int error;
183817fb938fSMatthew Ahrens 			VERIFY0(dmu_read(bpo->bpo_os,
183917fb938fSMatthew Ahrens 			    bpo->bpo_phys->bpo_subobjs,
184017fb938fSMatthew Ahrens 			    i * sizeof (subobj), sizeof (subobj), &subobj, 0));
184117fb938fSMatthew Ahrens 			error = bpobj_open(&subbpo, bpo->bpo_os, subobj);
184217fb938fSMatthew Ahrens 			if (error != 0) {
184317fb938fSMatthew Ahrens 				(void) printf("ERROR %u while trying to open "
184417fb938fSMatthew Ahrens 				    "subobj id %llu\n",
184517fb938fSMatthew Ahrens 				    error, (u_longlong_t)subobj);
184617fb938fSMatthew Ahrens 				continue;
184717fb938fSMatthew Ahrens 			}
184817fb938fSMatthew Ahrens 			bpobj_count_refd(&subbpo);
184917fb938fSMatthew Ahrens 			bpobj_close(&subbpo);
185017fb938fSMatthew Ahrens 		}
185117fb938fSMatthew Ahrens 	}
185217fb938fSMatthew Ahrens }
185317fb938fSMatthew Ahrens 
1854cde58dbcSMatthew Ahrens static void
1855cde58dbcSMatthew Ahrens dump_deadlist(dsl_deadlist_t *dl)
1856cde58dbcSMatthew Ahrens {
1857cde58dbcSMatthew Ahrens 	dsl_deadlist_entry_t *dle;
1858d0475637SMatthew Ahrens 	uint64_t unused;
1859cde58dbcSMatthew Ahrens 	char bytes[32];
1860cde58dbcSMatthew Ahrens 	char comp[32];
1861cde58dbcSMatthew Ahrens 	char uncomp[32];
186217fb938fSMatthew Ahrens 	uint64_t empty_bpobj =
186317fb938fSMatthew Ahrens 	    dmu_objset_spa(dl->dl_os)->spa_dsl_pool->dp_empty_bpobj;
186417fb938fSMatthew Ahrens 
186517fb938fSMatthew Ahrens 	/* force the tree to be loaded */
186617fb938fSMatthew Ahrens 	dsl_deadlist_space_range(dl, 0, UINT64_MAX, &unused, &unused, &unused);
186717fb938fSMatthew Ahrens 
186817fb938fSMatthew Ahrens 	if (dl->dl_oldfmt) {
186917fb938fSMatthew Ahrens 		if (dl->dl_bpobj.bpo_object != empty_bpobj)
187017fb938fSMatthew Ahrens 			bpobj_count_refd(&dl->dl_bpobj);
187117fb938fSMatthew Ahrens 	} else {
187217fb938fSMatthew Ahrens 		mos_obj_refd(dl->dl_object);
187317fb938fSMatthew Ahrens 		for (dle = avl_first(&dl->dl_tree); dle;
187417fb938fSMatthew Ahrens 		    dle = AVL_NEXT(&dl->dl_tree, dle)) {
187517fb938fSMatthew Ahrens 			if (dle->dle_bpobj.bpo_object != empty_bpobj)
187617fb938fSMatthew Ahrens 				bpobj_count_refd(&dle->dle_bpobj);
187717fb938fSMatthew Ahrens 		}
187817fb938fSMatthew Ahrens 	}
1879cde58dbcSMatthew Ahrens 
18800a055120SJason King 	/* make sure nicenum has enough space */
18810a055120SJason King 	CTASSERT(sizeof (bytes) >= NN_NUMBUF_SZ);
18820a055120SJason King 	CTASSERT(sizeof (comp) >= NN_NUMBUF_SZ);
18830a055120SJason King 	CTASSERT(sizeof (uncomp) >= NN_NUMBUF_SZ);
18840a055120SJason King 
1885cde58dbcSMatthew Ahrens 	if (dump_opt['d'] < 3)
1886cde58dbcSMatthew Ahrens 		return;
1887cde58dbcSMatthew Ahrens 
188890c76c66SMatthew Ahrens 	if (dl->dl_oldfmt) {
1889732885fcSMatthew Ahrens 		dump_full_bpobj(&dl->dl_bpobj, "old-format deadlist", 0);
189090c76c66SMatthew Ahrens 		return;
189190c76c66SMatthew Ahrens 	}
189290c76c66SMatthew Ahrens 
18930a055120SJason King 	zdb_nicenum(dl->dl_phys->dl_used, bytes, sizeof (bytes));
18940a055120SJason King 	zdb_nicenum(dl->dl_phys->dl_comp, comp, sizeof (comp));
18950a055120SJason King 	zdb_nicenum(dl->dl_phys->dl_uncomp, uncomp, sizeof (uncomp));
1896cde58dbcSMatthew Ahrens 	(void) printf("\n    Deadlist: %s (%s/%s comp)\n",
1897cde58dbcSMatthew Ahrens 	    bytes, comp, uncomp);
1898cde58dbcSMatthew Ahrens 
1899cde58dbcSMatthew Ahrens 	if (dump_opt['d'] < 4)
1900cde58dbcSMatthew Ahrens 		return;
1901cde58dbcSMatthew Ahrens 
1902cde58dbcSMatthew Ahrens 	(void) printf("\n");
1903cde58dbcSMatthew Ahrens 
1904cde58dbcSMatthew Ahrens 	for (dle = avl_first(&dl->dl_tree); dle;
1905cde58dbcSMatthew Ahrens 	    dle = AVL_NEXT(&dl->dl_tree, dle)) {
1906d0475637SMatthew Ahrens 		if (dump_opt['d'] >= 5) {
1907d0475637SMatthew Ahrens 			char buf[128];
1908de05b588SRichard Yao 			(void) snprintf(buf, sizeof (buf),
1909de05b588SRichard Yao 			    "mintxg %llu -> obj %llu",
1910d0475637SMatthew Ahrens 			    (longlong_t)dle->dle_mintxg,
1911d0475637SMatthew Ahrens 			    (longlong_t)dle->dle_bpobj.bpo_object);
1912fa9e4066Sahrens 
1913732885fcSMatthew Ahrens 			dump_full_bpobj(&dle->dle_bpobj, buf, 0);
1914d0475637SMatthew Ahrens 		} else {
1915d0475637SMatthew Ahrens 			(void) printf("mintxg %llu -> obj %llu\n",
1916d0475637SMatthew Ahrens 			    (longlong_t)dle->dle_mintxg,
1917d0475637SMatthew Ahrens 			    (longlong_t)dle->dle_bpobj.bpo_object);
1918d0475637SMatthew Ahrens 		}
1919cde58dbcSMatthew Ahrens 	}
1920fa9e4066Sahrens }
1921fa9e4066Sahrens 
1922e0d35c44Smarks static avl_tree_t idx_tree;
1923e0d35c44Smarks static avl_tree_t domain_tree;
1924e0d35c44Smarks static boolean_t fuid_table_loaded;
1925ed61ec1dSYuri Pankov static objset_t *sa_os = NULL;
1926ed61ec1dSYuri Pankov static sa_attr_type_t *sa_attr_table = NULL;
1927ed61ec1dSYuri Pankov 
1928ed61ec1dSYuri Pankov static int
1929ed61ec1dSYuri Pankov open_objset(const char *path, dmu_objset_type_t type, void *tag, objset_t **osp)
1930ed61ec1dSYuri Pankov {
1931ed61ec1dSYuri Pankov 	int err;
1932ed61ec1dSYuri Pankov 	uint64_t sa_attrs = 0;
1933ed61ec1dSYuri Pankov 	uint64_t version = 0;
1934ed61ec1dSYuri Pankov 
1935ed61ec1dSYuri Pankov 	VERIFY3P(sa_os, ==, NULL);
1936eb633035STom Caputi 	err = dmu_objset_own(path, type, B_TRUE, B_FALSE, tag, osp);
1937ed61ec1dSYuri Pankov 	if (err != 0) {
1938ed61ec1dSYuri Pankov 		(void) fprintf(stderr, "failed to own dataset '%s': %s\n", path,
1939ed61ec1dSYuri Pankov 		    strerror(err));
1940ed61ec1dSYuri Pankov 		return (err);
1941ed61ec1dSYuri Pankov 	}
1942ed61ec1dSYuri Pankov 
1943eb633035STom Caputi 	if (dmu_objset_type(*osp) == DMU_OST_ZFS && !(*osp)->os_encrypted) {
1944ed61ec1dSYuri Pankov 		(void) zap_lookup(*osp, MASTER_NODE_OBJ, ZPL_VERSION_STR,
1945ed61ec1dSYuri Pankov 		    8, 1, &version);
1946ed61ec1dSYuri Pankov 		if (version >= ZPL_VERSION_SA) {
1947ed61ec1dSYuri Pankov 			(void) zap_lookup(*osp, MASTER_NODE_OBJ, ZFS_SA_ATTRS,
1948ed61ec1dSYuri Pankov 			    8, 1, &sa_attrs);
1949ed61ec1dSYuri Pankov 		}
1950ed61ec1dSYuri Pankov 		err = sa_setup(*osp, sa_attrs, zfs_attr_table, ZPL_END,
1951ed61ec1dSYuri Pankov 		    &sa_attr_table);
1952ed61ec1dSYuri Pankov 		if (err != 0) {
1953ed61ec1dSYuri Pankov 			(void) fprintf(stderr, "sa_setup failed: %s\n",
1954ed61ec1dSYuri Pankov 			    strerror(err));
1955eb633035STom Caputi 			dmu_objset_disown(*osp, B_FALSE, tag);
1956ed61ec1dSYuri Pankov 			*osp = NULL;
1957ed61ec1dSYuri Pankov 		}
1958ed61ec1dSYuri Pankov 	}
1959ed61ec1dSYuri Pankov 	sa_os = *osp;
1960ed61ec1dSYuri Pankov 
1961ed61ec1dSYuri Pankov 	return (0);
1962ed61ec1dSYuri Pankov }
1963ed61ec1dSYuri Pankov 
1964ed61ec1dSYuri Pankov static void
1965ed61ec1dSYuri Pankov close_objset(objset_t *os, void *tag)
1966ed61ec1dSYuri Pankov {
1967ed61ec1dSYuri Pankov 	VERIFY3P(os, ==, sa_os);
1968ed61ec1dSYuri Pankov 	if (os->os_sa != NULL)
1969ed61ec1dSYuri Pankov 		sa_tear_down(os);
1970eb633035STom Caputi 	dmu_objset_disown(os, B_FALSE, tag);
1971ed61ec1dSYuri Pankov 	sa_attr_table = NULL;
1972ed61ec1dSYuri Pankov 	sa_os = NULL;
1973ed61ec1dSYuri Pankov }
1974e0d35c44Smarks 
1975e0d35c44Smarks static void
1976e0d35c44Smarks fuid_table_destroy()
1977e0d35c44Smarks {
1978e0d35c44Smarks 	if (fuid_table_loaded) {
1979e0d35c44Smarks 		zfs_fuid_table_destroy(&idx_tree, &domain_tree);
1980e0d35c44Smarks 		fuid_table_loaded = B_FALSE;
1981e0d35c44Smarks 	}
1982e0d35c44Smarks }
1983e0d35c44Smarks 
1984e0d35c44Smarks /*
1985e0d35c44Smarks  * print uid or gid information.
1986e0d35c44Smarks  * For normal POSIX id just the id is printed in decimal format.
1987e0d35c44Smarks  * For CIFS files with FUID the fuid is printed in hex followed by
1988d0475637SMatthew Ahrens  * the domain-rid string.
1989e0d35c44Smarks  */
1990e0d35c44Smarks static void
1991e0d35c44Smarks print_idstr(uint64_t id, const char *id_type)
1992e0d35c44Smarks {
1993e0d35c44Smarks 	if (FUID_INDEX(id)) {
1994e0d35c44Smarks 		char *domain;
1995e0d35c44Smarks 
1996e0d35c44Smarks 		domain = zfs_fuid_idx_domain(&idx_tree, FUID_INDEX(id));
1997e0d35c44Smarks 		(void) printf("\t%s     %llx [%s-%d]\n", id_type,
1998e0d35c44Smarks 		    (u_longlong_t)id, domain, (int)FUID_RID(id));
1999e0d35c44Smarks 	} else {
2000e0d35c44Smarks 		(void) printf("\t%s     %llu\n", id_type, (u_longlong_t)id);
2001e0d35c44Smarks 	}
2002e0d35c44Smarks 
2003e0d35c44Smarks }
2004e0d35c44Smarks 
2005e0d35c44Smarks static void
20060a586ceaSMark Shellenbaum dump_uidgid(objset_t *os, uint64_t uid, uint64_t gid)
2007e0d35c44Smarks {
2008e0d35c44Smarks 	uint32_t uid_idx, gid_idx;
2009e0d35c44Smarks 
20100a586ceaSMark Shellenbaum 	uid_idx = FUID_INDEX(uid);
20110a586ceaSMark Shellenbaum 	gid_idx = FUID_INDEX(gid);
2012e0d35c44Smarks 
2013e0d35c44Smarks 	/* Load domain table, if not already loaded */
2014e0d35c44Smarks 	if (!fuid_table_loaded && (uid_idx || gid_idx)) {
2015e0d35c44Smarks 		uint64_t fuid_obj;
2016e0d35c44Smarks 
2017e0d35c44Smarks 		/* first find the fuid object.  It lives in the master node */
2018e0d35c44Smarks 		VERIFY(zap_lookup(os, MASTER_NODE_OBJ, ZFS_FUID_TABLES,
2019e0d35c44Smarks 		    8, 1, &fuid_obj) == 0);
202089459e17SMark Shellenbaum 		zfs_fuid_avl_tree_create(&idx_tree, &domain_tree);
2021e0d35c44Smarks 		(void) zfs_fuid_table_load(os, fuid_obj,
2022e0d35c44Smarks 		    &idx_tree, &domain_tree);
2023e0d35c44Smarks 		fuid_table_loaded = B_TRUE;
2024e0d35c44Smarks 	}
2025e0d35c44Smarks 
20260a586ceaSMark Shellenbaum 	print_idstr(uid, "uid");
20270a586ceaSMark Shellenbaum 	print_idstr(gid, "gid");
2028e0d35c44Smarks }
2029e0d35c44Smarks 
2030fa9e4066Sahrens /*ARGSUSED*/
2031fa9e4066Sahrens static void
2032fa9e4066Sahrens dump_znode(objset_t *os, uint64_t object, void *data, size_t size)
2033fa9e4066Sahrens {
2034fa9e4066Sahrens 	char path[MAXPATHLEN * 2];	/* allow for xattr and failure prefix */
20350a586ceaSMark Shellenbaum 	sa_handle_t *hdl;
20360a586ceaSMark Shellenbaum 	uint64_t xattr, rdev, gen;
20370a586ceaSMark Shellenbaum 	uint64_t uid, gid, mode, fsize, parent, links;
20388f2529deSMark Shellenbaum 	uint64_t pflags;
20390a586ceaSMark Shellenbaum 	uint64_t acctm[2], modtm[2], chgtm[2], crtm[2];
20400a586ceaSMark Shellenbaum 	time_t z_crtime, z_atime, z_mtime, z_ctime;
20418f2529deSMark Shellenbaum 	sa_bulk_attr_t bulk[12];
20420a586ceaSMark Shellenbaum 	int idx = 0;
204355434c77Sek 	int error;
2044fa9e4066Sahrens 
2045ed61ec1dSYuri Pankov 	VERIFY3P(os, ==, sa_os);
20460a586ceaSMark Shellenbaum 	if (sa_handle_get(os, object, NULL, SA_HDL_PRIVATE, &hdl)) {
20470a586ceaSMark Shellenbaum 		(void) printf("Failed to get handle for SA znode\n");
20480a586ceaSMark Shellenbaum 		return;
20490a586ceaSMark Shellenbaum 	}
20500a586ceaSMark Shellenbaum 
20510a586ceaSMark Shellenbaum 	SA_ADD_BULK_ATTR(bulk, idx, sa_attr_table[ZPL_UID], NULL, &uid, 8);
20520a586ceaSMark Shellenbaum 	SA_ADD_BULK_ATTR(bulk, idx, sa_attr_table[ZPL_GID], NULL, &gid, 8);
20530a586ceaSMark Shellenbaum 	SA_ADD_BULK_ATTR(bulk, idx, sa_attr_table[ZPL_LINKS], NULL,
20540a586ceaSMark Shellenbaum 	    &links, 8);
20550a586ceaSMark Shellenbaum 	SA_ADD_BULK_ATTR(bulk, idx, sa_attr_table[ZPL_GEN], NULL, &gen, 8);
20560a586ceaSMark Shellenbaum 	SA_ADD_BULK_ATTR(bulk, idx, sa_attr_table[ZPL_MODE], NULL,
20570a586ceaSMark Shellenbaum 	    &mode, 8);
20580a586ceaSMark Shellenbaum 	SA_ADD_BULK_ATTR(bulk, idx, sa_attr_table[ZPL_PARENT],
20590a586ceaSMark Shellenbaum 	    NULL, &parent, 8);
20600a586ceaSMark Shellenbaum 	SA_ADD_BULK_ATTR(bulk, idx, sa_attr_table[ZPL_SIZE], NULL,
20610a586ceaSMark Shellenbaum 	    &fsize, 8);
20620a586ceaSMark Shellenbaum 	SA_ADD_BULK_ATTR(bulk, idx, sa_attr_table[ZPL_ATIME], NULL,
20630a586ceaSMark Shellenbaum 	    acctm, 16);
20640a586ceaSMark Shellenbaum 	SA_ADD_BULK_ATTR(bulk, idx, sa_attr_table[ZPL_MTIME], NULL,
20650a586ceaSMark Shellenbaum 	    modtm, 16);
20660a586ceaSMark Shellenbaum 	SA_ADD_BULK_ATTR(bulk, idx, sa_attr_table[ZPL_CRTIME], NULL,
20670a586ceaSMark Shellenbaum 	    crtm, 16);
20680a586ceaSMark Shellenbaum 	SA_ADD_BULK_ATTR(bulk, idx, sa_attr_table[ZPL_CTIME], NULL,
20690a586ceaSMark Shellenbaum 	    chgtm, 16);
20708f2529deSMark Shellenbaum 	SA_ADD_BULK_ATTR(bulk, idx, sa_attr_table[ZPL_FLAGS], NULL,
20718f2529deSMark Shellenbaum 	    &pflags, 8);
20720a586ceaSMark Shellenbaum 
20730a586ceaSMark Shellenbaum 	if (sa_bulk_lookup(hdl, bulk, idx)) {
20740a586ceaSMark Shellenbaum 		(void) sa_handle_destroy(hdl);
20750a586ceaSMark Shellenbaum 		return;
20760a586ceaSMark Shellenbaum 	}
2077fa9e4066Sahrens 
20780a586ceaSMark Shellenbaum 	z_crtime = (time_t)crtm[0];
20790a586ceaSMark Shellenbaum 	z_atime = (time_t)acctm[0];
20800a586ceaSMark Shellenbaum 	z_mtime = (time_t)modtm[0];
20810a586ceaSMark Shellenbaum 	z_ctime = (time_t)chgtm[0];
2082fa9e4066Sahrens 
2083e548d2faSAlan Somers 	if (dump_opt['d'] > 4) {
2084e548d2faSAlan Somers 		error = zfs_obj_to_path(os, object, path, sizeof (path));
208520b5dafbSPaul Dagnelie 		if (error == ESTALE) {
208620b5dafbSPaul Dagnelie 			(void) snprintf(path, sizeof (path), "on delete queue");
208720b5dafbSPaul Dagnelie 		} else if (error != 0) {
208820b5dafbSPaul Dagnelie 			leaked_objects++;
2089e548d2faSAlan Somers 			(void) snprintf(path, sizeof (path),
209020b5dafbSPaul Dagnelie 			    "path not found, possibly leaked");
2091e548d2faSAlan Somers 		}
2092e548d2faSAlan Somers 		(void) printf("\tpath	%s\n", path);
2093e548d2faSAlan Somers 	}
20940a586ceaSMark Shellenbaum 	dump_uidgid(os, uid, gid);
2095fa9e4066Sahrens 	(void) printf("\tatime	%s", ctime(&z_atime));
2096fa9e4066Sahrens 	(void) printf("\tmtime	%s", ctime(&z_mtime));
2097fa9e4066Sahrens 	(void) printf("\tctime	%s", ctime(&z_ctime));
2098fa9e4066Sahrens 	(void) printf("\tcrtime	%s", ctime(&z_crtime));
20990a586ceaSMark Shellenbaum 	(void) printf("\tgen	%llu\n", (u_longlong_t)gen);
21000a586ceaSMark Shellenbaum 	(void) printf("\tmode	%llo\n", (u_longlong_t)mode);
21010a586ceaSMark Shellenbaum 	(void) printf("\tsize	%llu\n", (u_longlong_t)fsize);
21020a586ceaSMark Shellenbaum 	(void) printf("\tparent	%llu\n", (u_longlong_t)parent);
21030a586ceaSMark Shellenbaum 	(void) printf("\tlinks	%llu\n", (u_longlong_t)links);
21048f2529deSMark Shellenbaum 	(void) printf("\tpflags	%llx\n", (u_longlong_t)pflags);
2105f67950b2SNasf-Fan 	if (dmu_objset_projectquota_enabled(os) && (pflags & ZFS_PROJID)) {
2106f67950b2SNasf-Fan 		uint64_t projid;
2107f67950b2SNasf-Fan 
2108f67950b2SNasf-Fan 		if (sa_lookup(hdl, sa_attr_table[ZPL_PROJID], &projid,
2109f67950b2SNasf-Fan 		    sizeof (uint64_t)) == 0)
2110f67950b2SNasf-Fan 			(void) printf("\tprojid	%llu\n", (u_longlong_t)projid);
2111f67950b2SNasf-Fan 	}
21120a586ceaSMark Shellenbaum 	if (sa_lookup(hdl, sa_attr_table[ZPL_XATTR], &xattr,
21130a586ceaSMark Shellenbaum 	    sizeof (uint64_t)) == 0)
21140a586ceaSMark Shellenbaum 		(void) printf("\txattr	%llu\n", (u_longlong_t)xattr);
21150a586ceaSMark Shellenbaum 	if (sa_lookup(hdl, sa_attr_table[ZPL_RDEV], &rdev,
21160a586ceaSMark Shellenbaum 	    sizeof (uint64_t)) == 0)
21170a586ceaSMark Shellenbaum 		(void) printf("\trdev	0x%016llx\n", (u_longlong_t)rdev);
21180a586ceaSMark Shellenbaum 	sa_handle_destroy(hdl);
2119fa9e4066Sahrens }
2120fa9e4066Sahrens 
2121fa9e4066Sahrens /*ARGSUSED*/
2122fa9e4066Sahrens static void
2123fa9e4066Sahrens dump_acl(objset_t *os, uint64_t object, void *data, size_t size)
2124fa9e4066Sahrens {
2125fa9e4066Sahrens }
2126fa9e4066Sahrens 
2127fa9e4066Sahrens /*ARGSUSED*/
2128fa9e4066Sahrens static void
2129fa9e4066Sahrens dump_dmu_objset(objset_t *os, uint64_t object, void *data, size_t size)
2130fa9e4066Sahrens {
2131fa9e4066Sahrens }
2132fa9e4066Sahrens 
2133eb633035STom Caputi 
21346de8f417SVictor Latushkin static object_viewer_t *object_viewer[DMU_OT_NUMTYPES + 1] = {
2135fa9e4066Sahrens 	dump_none,		/* unallocated			*/
2136fa9e4066Sahrens 	dump_zap,		/* object directory		*/
2137fa9e4066Sahrens 	dump_uint64,		/* object array			*/
2138fa9e4066Sahrens 	dump_none,		/* packed nvlist		*/
2139fa9e4066Sahrens 	dump_packed_nvlist,	/* packed nvlist size		*/
2140732885fcSMatthew Ahrens 	dump_none,		/* bpobj			*/
2141732885fcSMatthew Ahrens 	dump_bpobj,		/* bpobj header			*/
2142fa9e4066Sahrens 	dump_none,		/* SPA space map header		*/
2143fa9e4066Sahrens 	dump_none,		/* SPA space map		*/
2144fa9e4066Sahrens 	dump_none,		/* ZIL intent log		*/
2145fa9e4066Sahrens 	dump_dnode,		/* DMU dnode			*/
2146fa9e4066Sahrens 	dump_dmu_objset,	/* DMU objset			*/
2147ea8dc4b6Seschrock 	dump_dsl_dir,		/* DSL directory		*/
2148fa9e4066Sahrens 	dump_zap,		/* DSL directory child map	*/
2149fa9e4066Sahrens 	dump_zap,		/* DSL dataset snap map		*/
2150fa9e4066Sahrens 	dump_zap,		/* DSL props			*/
2151fa9e4066Sahrens 	dump_dsl_dataset,	/* DSL dataset			*/
2152fa9e4066Sahrens 	dump_znode,		/* ZFS znode			*/
2153da6c28aaSamw 	dump_acl,		/* ZFS V0 ACL			*/
2154fa9e4066Sahrens 	dump_uint8,		/* ZFS plain file		*/
2155e7437265Sahrens 	dump_zpldir,		/* ZFS directory		*/
2156fa9e4066Sahrens 	dump_zap,		/* ZFS master node		*/
2157fa9e4066Sahrens 	dump_zap,		/* ZFS delete queue		*/
2158fa9e4066Sahrens 	dump_uint8,		/* zvol object			*/
2159fa9e4066Sahrens 	dump_zap,		/* zvol prop			*/
2160fa9e4066Sahrens 	dump_uint8,		/* other uint8[]		*/
2161fa9e4066Sahrens 	dump_uint64,		/* other uint64[]		*/
2162fa9e4066Sahrens 	dump_zap,		/* other ZAP			*/
2163ea8dc4b6Seschrock 	dump_zap,		/* persistent error log		*/
216406eeb2adSek 	dump_uint8,		/* SPA history			*/
21654445fffbSMatthew Ahrens 	dump_history_offsets,	/* SPA history offsets		*/
2166b1b8ab34Slling 	dump_zap,		/* Pool properties		*/
2167ecd6cf80Smarks 	dump_zap,		/* DSL permissions		*/
2168da6c28aaSamw 	dump_acl,		/* ZFS ACL			*/
2169da6c28aaSamw 	dump_uint8,		/* ZFS SYSACL			*/
2170da6c28aaSamw 	dump_none,		/* FUID nvlist			*/
2171da6c28aaSamw 	dump_packed_nvlist,	/* FUID nvlist size		*/
2172088f3894Sahrens 	dump_zap,		/* DSL dataset next clones	*/
2173088f3894Sahrens 	dump_zap,		/* DSL scrub queue		*/
2174f67950b2SNasf-Fan 	dump_zap,		/* ZFS user/group/project used	*/
2175f67950b2SNasf-Fan 	dump_zap,		/* ZFS user/group/project quota	*/
2176842727c2SChris Kirby 	dump_zap,		/* snapshot refcount tags	*/
2177486ae710SMatthew Ahrens 	dump_ddt_zap,		/* DDT ZAP object		*/
2178b24ab676SJeff Bonwick 	dump_zap,		/* DDT statistics		*/
21790a586ceaSMark Shellenbaum 	dump_znode,		/* SA object			*/
21800a586ceaSMark Shellenbaum 	dump_zap,		/* SA Master Node		*/
21810a586ceaSMark Shellenbaum 	dump_sa_attrs,		/* SA attribute registration	*/
21820a586ceaSMark Shellenbaum 	dump_sa_layouts,	/* SA attribute layouts		*/
21833f9d6ad7SLin Ling 	dump_zap,		/* DSL scrub translations	*/
21843f9d6ad7SLin Ling 	dump_none,		/* fake dedup BP		*/
2185cde58dbcSMatthew Ahrens 	dump_zap,		/* deadlist			*/
2186cde58dbcSMatthew Ahrens 	dump_none,		/* deadlist hdr			*/
2187cde58dbcSMatthew Ahrens 	dump_zap,		/* dsl clones			*/
2188732885fcSMatthew Ahrens 	dump_bpobj_subobjs,	/* bpobj subobjs		*/
21890a586ceaSMark Shellenbaum 	dump_unknown,		/* Unknown type, must be last	*/
2190fa9e4066Sahrens };
2191fa9e4066Sahrens 
2192fa9e4066Sahrens static void
219354811da5SToomas Soome dump_object(objset_t *os, uint64_t object, int verbosity, int *print_header,
219454811da5SToomas Soome     uint64_t *dnode_slots_used)
2195fa9e4066Sahrens {
2196fa9e4066Sahrens 	dmu_buf_t *db = NULL;
2197fa9e4066Sahrens 	dmu_object_info_t doi;
2198fa9e4066Sahrens 	dnode_t *dn;
2199eb633035STom Caputi 	boolean_t dnode_held = B_FALSE;
2200fa9e4066Sahrens 	void *bonus = NULL;
2201fa9e4066Sahrens 	size_t bsize = 0;
220254811da5SToomas Soome 	char iblk[32], dblk[32], lsize[32], asize[32], fill[32], dnsize[32];
22033f9d6ad7SLin Ling 	char bonus_size[32];
2204fa9e4066Sahrens 	char aux[50];
2205fa9e4066Sahrens 	int error;
2206fa9e4066Sahrens 
22070a055120SJason King 	/* make sure nicenum has enough space */
22080a055120SJason King 	CTASSERT(sizeof (iblk) >= NN_NUMBUF_SZ);
22090a055120SJason King 	CTASSERT(sizeof (dblk) >= NN_NUMBUF_SZ);
22100a055120SJason King 	CTASSERT(sizeof (lsize) >= NN_NUMBUF_SZ);
22110a055120SJason King 	CTASSERT(sizeof (asize) >= NN_NUMBUF_SZ);
22120a055120SJason King 	CTASSERT(sizeof (bonus_size) >= NN_NUMBUF_SZ);
22130a055120SJason King 
2214fa9e4066Sahrens 	if (*print_header) {
221554811da5SToomas Soome 		(void) printf("\n%10s  %3s  %5s  %5s  %5s  %6s  %5s  %6s  %s\n",
221654811da5SToomas Soome 		    "Object", "lvl", "iblk", "dblk", "dsize", "dnsize",
221754811da5SToomas Soome 		    "lsize", "%full", "type");
2218fa9e4066Sahrens 		*print_header = 0;
2219fa9e4066Sahrens 	}
2220fa9e4066Sahrens 
2221fa9e4066Sahrens 	if (object == 0) {
2222744947dcSTom Erickson 		dn = DMU_META_DNODE(os);
2223eb633035STom Caputi 		dmu_object_info_from_dnode(dn, &doi);
2224fa9e4066Sahrens 	} else {
2225eb633035STom Caputi 		/*
2226eb633035STom Caputi 		 * Encrypted datasets will have sensitive bonus buffers
2227eb633035STom Caputi 		 * encrypted. Therefore we cannot hold the bonus buffer and
2228eb633035STom Caputi 		 * must hold the dnode itself instead.
2229eb633035STom Caputi 		 */
2230eb633035STom Caputi 		error = dmu_object_info(os, object, &doi);
2231ea8dc4b6Seschrock 		if (error)
2232eb633035STom Caputi 			fatal("dmu_object_info() failed, errno %u", error);
2233eb633035STom Caputi 
2234eb633035STom Caputi 		if (os->os_encrypted &&
2235eb633035STom Caputi 		    DMU_OT_IS_ENCRYPTED(doi.doi_bonus_type)) {
2236eb633035STom Caputi 			error = dnode_hold(os, object, FTAG, &dn);
2237eb633035STom Caputi 			if (error)
2238eb633035STom Caputi 				fatal("dnode_hold() failed, errno %u", error);
2239eb633035STom Caputi 			dnode_held = B_TRUE;
2240eb633035STom Caputi 		} else {
2241eb633035STom Caputi 			error = dmu_bonus_hold(os, object, FTAG, &db);
2242eb633035STom Caputi 			if (error)
2243eb633035STom Caputi 				fatal("dmu_bonus_hold(%llu) failed, errno %u",
2244eb633035STom Caputi 				    object, error);
2245eb633035STom Caputi 			bonus = db->db_data;
2246eb633035STom Caputi 			bsize = db->db_size;
2247eb633035STom Caputi 			dn = DB_DNODE((dmu_buf_impl_t *)db);
2248eb633035STom Caputi 		}
2249fa9e4066Sahrens 	}
2250fa9e4066Sahrens 
225154811da5SToomas Soome 	if (dnode_slots_used != NULL)
225254811da5SToomas Soome 		*dnode_slots_used = doi.doi_dnodesize / DNODE_MIN_SIZE;
225354811da5SToomas Soome 
22540a055120SJason King 	zdb_nicenum(doi.doi_metadata_block_size, iblk, sizeof (iblk));
22550a055120SJason King 	zdb_nicenum(doi.doi_data_block_size, dblk, sizeof (dblk));
22560a055120SJason King 	zdb_nicenum(doi.doi_max_offset, lsize, sizeof (lsize));
22570a055120SJason King 	zdb_nicenum(doi.doi_physical_blocks_512 << 9, asize, sizeof (asize));
22580a055120SJason King 	zdb_nicenum(doi.doi_bonus_size, bonus_size, sizeof (bonus_size));
225954811da5SToomas Soome 	zdb_nicenum(doi.doi_dnodesize, dnsize, sizeof (dnsize));
2260b24ab676SJeff Bonwick 	(void) sprintf(fill, "%6.2f", 100.0 * doi.doi_fill_count *
2261bbfd46c4SJeff Bonwick 	    doi.doi_data_block_size / (object == 0 ? DNODES_PER_BLOCK : 1) /
2262bbfd46c4SJeff Bonwick 	    doi.doi_max_offset);
2263fa9e4066Sahrens 
2264fa9e4066Sahrens 	aux[0] = '\0';
2265fa9e4066Sahrens 
2266e7437265Sahrens 	if (doi.doi_checksum != ZIO_CHECKSUM_INHERIT || verbosity >= 6) {
2267fa9e4066Sahrens 		(void) snprintf(aux + strlen(aux), sizeof (aux), " (K=%s)",
22686de8f417SVictor Latushkin 		    ZDB_CHECKSUM_NAME(doi.doi_checksum));
2269e7437265Sahrens 	}
2270fa9e4066Sahrens 
2271e7437265Sahrens 	if (doi.doi_compress != ZIO_COMPRESS_INHERIT || verbosity >= 6) {
2272fa9e4066Sahrens 		(void) snprintf(aux + strlen(aux), sizeof (aux), " (Z=%s)",
22736de8f417SVictor Latushkin 		    ZDB_COMPRESS_NAME(doi.doi_compress));
2274e7437265Sahrens 	}
2275fa9e4066Sahrens 
227654811da5SToomas Soome 	(void) printf("%10" PRIu64
227754811da5SToomas Soome 	    "  %3u  %5s  %5s  %5s  %5s  %5s  %6s  %s%s\n",
227854811da5SToomas Soome 	    object, doi.doi_indirection, iblk, dblk,
227954811da5SToomas Soome 	    asize, dnsize, lsize, fill, ZDB_OT_NAME(doi.doi_type), aux);
2280fa9e4066Sahrens 
2281fa9e4066Sahrens 	if (doi.doi_bonus_type != DMU_OT_NONE && verbosity > 3) {
228254811da5SToomas Soome 		(void) printf("%10s  %3s  %5s  %5s  %5s  %5s  %5s  %6s  %s\n",
228354811da5SToomas Soome 		    "", "", "", "", "", "", bonus_size, "bonus",
22846de8f417SVictor Latushkin 		    ZDB_OT_NAME(doi.doi_bonus_type));
2285fa9e4066Sahrens 	}
2286fa9e4066Sahrens 
2287fa9e4066Sahrens 	if (verbosity >= 4) {
2288f67950b2SNasf-Fan 		(void) printf("\tdnode flags: %s%s%s%s\n",
228914843421SMatthew Ahrens 		    (dn->dn_phys->dn_flags & DNODE_FLAG_USED_BYTES) ?
229014843421SMatthew Ahrens 		    "USED_BYTES " : "",
229114843421SMatthew Ahrens 		    (dn->dn_phys->dn_flags & DNODE_FLAG_USERUSED_ACCOUNTED) ?
22920a586ceaSMark Shellenbaum 		    "USERUSED_ACCOUNTED " : "",
2293f67950b2SNasf-Fan 		    (dn->dn_phys->dn_flags & DNODE_FLAG_USEROBJUSED_ACCOUNTED) ?
2294f67950b2SNasf-Fan 		    "USEROBJUSED_ACCOUNTED " : "",
22950a586ceaSMark Shellenbaum 		    (dn->dn_phys->dn_flags & DNODE_FLAG_SPILL_BLKPTR) ?
22960a586ceaSMark Shellenbaum 		    "SPILL_BLKPTR" : "");
229714843421SMatthew Ahrens 		(void) printf("\tdnode maxblkid: %llu\n",
229814843421SMatthew Ahrens 		    (longlong_t)dn->dn_phys->dn_maxblkid);
229914843421SMatthew Ahrens 
2300eb633035STom Caputi 		if (!dnode_held) {
2301eb633035STom Caputi 			object_viewer[ZDB_OT_TYPE(doi.doi_bonus_type)](os,
2302eb633035STom Caputi 			    object, bonus, bsize);
2303eb633035STom Caputi 		} else {
2304eb633035STom Caputi 			(void) printf("\t\t(bonus encrypted)\n");
2305eb633035STom Caputi 		}
2306eb633035STom Caputi 
2307eb633035STom Caputi 		if (!os->os_encrypted || !DMU_OT_IS_ENCRYPTED(doi.doi_type)) {
2308eb633035STom Caputi 			object_viewer[ZDB_OT_TYPE(doi.doi_type)](os, object,
2309eb633035STom Caputi 			    NULL, 0);
2310eb633035STom Caputi 		} else {
2311eb633035STom Caputi 			(void) printf("\t\t(object encrypted)\n");
2312eb633035STom Caputi 		}
2313eb633035STom Caputi 
2314fa9e4066Sahrens 		*print_header = 1;
2315fa9e4066Sahrens 	}
2316fa9e4066Sahrens 
2317fa9e4066Sahrens 	if (verbosity >= 5)
231888b7b0f2SMatthew Ahrens 		dump_indirect(dn);
2319fa9e4066Sahrens 
2320fa9e4066Sahrens 	if (verbosity >= 5) {
2321fa9e4066Sahrens 		/*
2322fa9e4066Sahrens 		 * Report the list of segments that comprise the object.
2323fa9e4066Sahrens 		 */
2324fa9e4066Sahrens 		uint64_t start = 0;
2325fa9e4066Sahrens 		uint64_t end;
2326fa9e4066Sahrens 		uint64_t blkfill = 1;
2327fa9e4066Sahrens 		int minlvl = 1;
2328fa9e4066Sahrens 
2329fa9e4066Sahrens 		if (dn->dn_type == DMU_OT_DNODE) {
2330fa9e4066Sahrens 			minlvl = 0;
2331fa9e4066Sahrens 			blkfill = DNODES_PER_BLOCK;
2332fa9e4066Sahrens 		}
2333fa9e4066Sahrens 
2334fa9e4066Sahrens 		for (;;) {
23353f9d6ad7SLin Ling 			char segsize[32];
23360a055120SJason King 			/* make sure nicenum has enough space */
23370a055120SJason King 			CTASSERT(sizeof (segsize) >= NN_NUMBUF_SZ);
2338cdb0ab79Smaybee 			error = dnode_next_offset(dn,
2339cdb0ab79Smaybee 			    0, &start, minlvl, blkfill, 0);
2340fa9e4066Sahrens 			if (error)
2341fa9e4066Sahrens 				break;
2342fa9e4066Sahrens 			end = start;
2343cdb0ab79Smaybee 			error = dnode_next_offset(dn,
2344cdb0ab79Smaybee 			    DNODE_FIND_HOLE, &end, minlvl, blkfill, 0);
23450a055120SJason King 			zdb_nicenum(end - start, segsize, sizeof (segsize));
2346fa9e4066Sahrens 			(void) printf("\t\tsegment [%016llx, %016llx)"
2347fa9e4066Sahrens 			    " size %5s\n", (u_longlong_t)start,
2348fa9e4066Sahrens 			    (u_longlong_t)end, segsize);
2349fa9e4066Sahrens 			if (error)
2350fa9e4066Sahrens 				break;
2351fa9e4066Sahrens 			start = end;
2352fa9e4066Sahrens 		}
2353fa9e4066Sahrens 	}
2354fa9e4066Sahrens 
2355fa9e4066Sahrens 	if (db != NULL)
2356ea8dc4b6Seschrock 		dmu_buf_rele(db, FTAG);
2357eb633035STom Caputi 	if (dnode_held)
2358eb633035STom Caputi 		dnode_rele(dn, FTAG);
2359fa9e4066Sahrens }
2360fa9e4066Sahrens 
236117fb938fSMatthew Ahrens static void
236217fb938fSMatthew Ahrens count_dir_mos_objects(dsl_dir_t *dd)
236317fb938fSMatthew Ahrens {
236417fb938fSMatthew Ahrens 	mos_obj_refd(dd->dd_object);
236517fb938fSMatthew Ahrens 	mos_obj_refd(dsl_dir_phys(dd)->dd_child_dir_zapobj);
236617fb938fSMatthew Ahrens 	mos_obj_refd(dsl_dir_phys(dd)->dd_deleg_zapobj);
236717fb938fSMatthew Ahrens 	mos_obj_refd(dsl_dir_phys(dd)->dd_props_zapobj);
236817fb938fSMatthew Ahrens 	mos_obj_refd(dsl_dir_phys(dd)->dd_clones);
236917fb938fSMatthew Ahrens }
237017fb938fSMatthew Ahrens 
237117fb938fSMatthew Ahrens static void
237217fb938fSMatthew Ahrens count_ds_mos_objects(dsl_dataset_t *ds)
237317fb938fSMatthew Ahrens {
237417fb938fSMatthew Ahrens 	mos_obj_refd(ds->ds_object);
237517fb938fSMatthew Ahrens 	mos_obj_refd(dsl_dataset_phys(ds)->ds_next_clones_obj);
237617fb938fSMatthew Ahrens 	mos_obj_refd(dsl_dataset_phys(ds)->ds_props_obj);
237717fb938fSMatthew Ahrens 	mos_obj_refd(dsl_dataset_phys(ds)->ds_userrefs_obj);
237817fb938fSMatthew Ahrens 	mos_obj_refd(dsl_dataset_phys(ds)->ds_snapnames_zapobj);
237917fb938fSMatthew Ahrens 
238017fb938fSMatthew Ahrens 	if (!dsl_dataset_is_snapshot(ds)) {
238117fb938fSMatthew Ahrens 		count_dir_mos_objects(ds->ds_dir);
238217fb938fSMatthew Ahrens 	}
238317fb938fSMatthew Ahrens }
238417fb938fSMatthew Ahrens 
23853f7978d0SAlan Somers static const char *objset_types[DMU_OST_NUMTYPES] = {
2386fa9e4066Sahrens 	"NONE", "META", "ZPL", "ZVOL", "OTHER", "ANY" };
2387fa9e4066Sahrens 
2388fa9e4066Sahrens static void
2389fa9e4066Sahrens dump_dir(objset_t *os)
2390fa9e4066Sahrens {
2391fa9e4066Sahrens 	dmu_objset_stats_t dds;
2392fa9e4066Sahrens 	uint64_t object, object_count;
2393a2eea2e1Sahrens 	uint64_t refdbytes, usedobjs, scratch;
23943f9d6ad7SLin Ling 	char numbuf[32];
239514843421SMatthew Ahrens 	char blkbuf[BP_SPRINTF_LEN + 20];
23969adfa60dSMatthew Ahrens 	char osname[ZFS_MAX_DATASET_NAME_LEN];
23973f7978d0SAlan Somers 	const char *type = "UNKNOWN";
2398fa9e4066Sahrens 	int verbosity = dump_opt['d'];
2399fa9e4066Sahrens 	int print_header = 1;
24003f7978d0SAlan Somers 	unsigned i;
24013f7978d0SAlan Somers 	int error;
240254811da5SToomas Soome 	uint64_t total_slots_used = 0;
240354811da5SToomas Soome 	uint64_t max_slot_used = 0;
240454811da5SToomas Soome 	uint64_t dnode_slots;
2405fa9e4066Sahrens 
24060a055120SJason King 	/* make sure nicenum has enough space */
24070a055120SJason King 	CTASSERT(sizeof (numbuf) >= NN_NUMBUF_SZ);
24080a055120SJason King 
24093b2aab18SMatthew Ahrens 	dsl_pool_config_enter(dmu_objset_pool(os), FTAG);
2410a2eea2e1Sahrens 	dmu_objset_fast_stat(os, &dds);
24113b2aab18SMatthew Ahrens 	dsl_pool_config_exit(dmu_objset_pool(os), FTAG);
2412fa9e4066Sahrens 
2413fa9e4066Sahrens 	if (dds.dds_type < DMU_OST_NUMTYPES)
2414fa9e4066Sahrens 		type = objset_types[dds.dds_type];
2415fa9e4066Sahrens 
2416fa9e4066Sahrens 	if (dds.dds_type == DMU_OST_META) {
2417fa9e4066Sahrens 		dds.dds_creation_txg = TXG_INITIAL;
24185d7b4d43SMatthew Ahrens 		usedobjs = BP_GET_FILL(os->os_rootbp);
2419c1379625SJustin T. Gibbs 		refdbytes = dsl_dir_phys(os->os_spa->spa_dsl_pool->dp_mos_dir)->
2420c1379625SJustin T. Gibbs 		    dd_used_bytes;
2421a2eea2e1Sahrens 	} else {
2422a2eea2e1Sahrens 		dmu_objset_space(os, &refdbytes, &scratch, &usedobjs, &scratch);
2423fa9e4066Sahrens 	}
2424fa9e4066Sahrens 
24255d7b4d43SMatthew Ahrens 	ASSERT3U(usedobjs, ==, BP_GET_FILL(os->os_rootbp));
2426fa9e4066Sahrens 
24270a055120SJason King 	zdb_nicenum(refdbytes, numbuf, sizeof (numbuf));
2428fa9e4066Sahrens 
2429fa9e4066Sahrens 	if (verbosity >= 4) {
243043466aaeSMax Grossman 		(void) snprintf(blkbuf, sizeof (blkbuf), ", rootbp ");
243143466aaeSMax Grossman 		(void) snprintf_blkptr(blkbuf + strlen(blkbuf),
243243466aaeSMax Grossman 		    sizeof (blkbuf) - strlen(blkbuf), os->os_rootbp);
2433fa9e4066Sahrens 	} else {
2434fa9e4066Sahrens 		blkbuf[0] = '\0';
2435fa9e4066Sahrens 	}
2436fa9e4066Sahrens 
2437fa9e4066Sahrens 	dmu_objset_name(os, osname);
2438fa9e4066Sahrens 
2439a2eea2e1Sahrens 	(void) printf("Dataset %s [%s], ID %llu, cr_txg %llu, "
2440ade2c828SSerapheim Dimitropoulos 	    "%s, %llu objects%s%s\n",
2441fa9e4066Sahrens 	    osname, type, (u_longlong_t)dmu_objset_id(os),
2442fa9e4066Sahrens 	    (u_longlong_t)dds.dds_creation_txg,
2443ade2c828SSerapheim Dimitropoulos 	    numbuf, (u_longlong_t)usedobjs, blkbuf,
2444ade2c828SSerapheim Dimitropoulos 	    (dds.dds_inconsistent) ? " (inconsistent)" : "");
2445fa9e4066Sahrens 
2446b24ab676SJeff Bonwick 	if (zopt_objects != 0) {
2447b24ab676SJeff Bonwick 		for (i = 0; i < zopt_objects; i++)
2448b24ab676SJeff Bonwick 			dump_object(os, zopt_object[i], verbosity,
244954811da5SToomas Soome 			    &print_header, NULL);
2450b24ab676SJeff Bonwick 		(void) printf("\n");
2451b24ab676SJeff Bonwick 		return;
2452b24ab676SJeff Bonwick 	}
2453b24ab676SJeff Bonwick 
2454b24ab676SJeff Bonwick 	if (dump_opt['i'] != 0 || verbosity >= 2)
2455b24ab676SJeff Bonwick 		dump_intent_log(dmu_objset_zil(os));
2456fa9e4066Sahrens 
24575cabbc6bSPrashanth Sreenivasa 	if (dmu_objset_ds(os) != NULL) {
24585cabbc6bSPrashanth Sreenivasa 		dsl_dataset_t *ds = dmu_objset_ds(os);
24595cabbc6bSPrashanth Sreenivasa 		dump_deadlist(&ds->ds_deadlist);
24605cabbc6bSPrashanth Sreenivasa 
24615cabbc6bSPrashanth Sreenivasa 		if (dsl_dataset_remap_deadlist_exists(ds)) {
24625cabbc6bSPrashanth Sreenivasa 			(void) printf("ds_remap_deadlist:\n");
24635cabbc6bSPrashanth Sreenivasa 			dump_deadlist(&ds->ds_remap_deadlist);
24645cabbc6bSPrashanth Sreenivasa 		}
246517fb938fSMatthew Ahrens 		count_ds_mos_objects(ds);
24665cabbc6bSPrashanth Sreenivasa 	}
2467fa9e4066Sahrens 
2468fa9e4066Sahrens 	if (verbosity < 2)
2469fa9e4066Sahrens 		return;
2470fa9e4066Sahrens 
247143466aaeSMax Grossman 	if (BP_IS_HOLE(os->os_rootbp))
2472088f3894Sahrens 		return;
2473088f3894Sahrens 
247454811da5SToomas Soome 	dump_object(os, 0, verbosity, &print_header, NULL);
247514843421SMatthew Ahrens 	object_count = 0;
2476744947dcSTom Erickson 	if (DMU_USERUSED_DNODE(os) != NULL &&
2477744947dcSTom Erickson 	    DMU_USERUSED_DNODE(os)->dn_type != 0) {
247854811da5SToomas Soome 		dump_object(os, DMU_USERUSED_OBJECT, verbosity, &print_header,
247954811da5SToomas Soome 		    NULL);
248054811da5SToomas Soome 		dump_object(os, DMU_GROUPUSED_OBJECT, verbosity, &print_header,
248154811da5SToomas Soome 		    NULL);
248214843421SMatthew Ahrens 	}
2483fa9e4066Sahrens 
2484f67950b2SNasf-Fan 	if (DMU_PROJECTUSED_DNODE(os) != NULL &&
2485f67950b2SNasf-Fan 	    DMU_PROJECTUSED_DNODE(os)->dn_type != 0)
2486f67950b2SNasf-Fan 		dump_object(os, DMU_PROJECTUSED_OBJECT, verbosity,
2487f67950b2SNasf-Fan 		    &print_header, NULL);
2488f67950b2SNasf-Fan 
2489fa9e4066Sahrens 	object = 0;
24906754306eSahrens 	while ((error = dmu_object_next(os, &object, B_FALSE, 0)) == 0) {
249154811da5SToomas Soome 		dump_object(os, object, verbosity, &print_header, &dnode_slots);
2492fa9e4066Sahrens 		object_count++;
249354811da5SToomas Soome 		total_slots_used += dnode_slots;
249454811da5SToomas Soome 		max_slot_used = object + dnode_slots - 1;
2495fa9e4066Sahrens 	}
2496fa9e4066Sahrens 
2497fa9e4066Sahrens 	(void) printf("\n");
2498fa9e4066Sahrens 
249954811da5SToomas Soome 	(void) printf("    Dnode slots:\n");
250054811da5SToomas Soome 	(void) printf("\tTotal used:    %10llu\n",
250154811da5SToomas Soome 	    (u_longlong_t)total_slots_used);
250254811da5SToomas Soome 	(void) printf("\tMax used:      %10llu\n",
250354811da5SToomas Soome 	    (u_longlong_t)max_slot_used);
250454811da5SToomas Soome 	(void) printf("\tPercent empty: %10lf\n",
250554811da5SToomas Soome 	    (double)(max_slot_used - total_slots_used)*100 /
250654811da5SToomas Soome 	    (double)max_slot_used);
250754811da5SToomas Soome 
250854811da5SToomas Soome 	(void) printf("\n");
250954811da5SToomas Soome 
2510ccba0801SRich Morris 	if (error != ESRCH) {
2511ccba0801SRich Morris 		(void) fprintf(stderr, "dmu_object_next() = %d\n", error);
2512ccba0801SRich Morris 		abort();
2513ccba0801SRich Morris 	}
251420b5dafbSPaul Dagnelie 	if (leaked_objects != 0) {
251520b5dafbSPaul Dagnelie 		(void) printf("%d potentially leaked objects detected\n",
251620b5dafbSPaul Dagnelie 		    leaked_objects);
251720b5dafbSPaul Dagnelie 		leaked_objects = 0;
251820b5dafbSPaul Dagnelie 	}
2519a3874b8bSToomas Soome 
2520a3874b8bSToomas Soome 	ASSERT3U(object_count, ==, usedobjs);
2521fa9e4066Sahrens }
2522fa9e4066Sahrens 
2523fa9e4066Sahrens static void
252453b9a4a9SVictor Latushkin dump_uberblock(uberblock_t *ub, const char *header, const char *footer)
2525fa9e4066Sahrens {
2526fa9e4066Sahrens 	time_t timestamp = ub->ub_timestamp;
2527fa9e4066Sahrens 
25283f7978d0SAlan Somers 	(void) printf("%s", header ? header : "");
2529fa9e4066Sahrens 	(void) printf("\tmagic = %016llx\n", (u_longlong_t)ub->ub_magic);
2530fa9e4066Sahrens 	(void) printf("\tversion = %llu\n", (u_longlong_t)ub->ub_version);
2531fa9e4066Sahrens 	(void) printf("\ttxg = %llu\n", (u_longlong_t)ub->ub_txg);
2532fa9e4066Sahrens 	(void) printf("\tguid_sum = %llu\n", (u_longlong_t)ub->ub_guid_sum);
2533fa9e4066Sahrens 	(void) printf("\ttimestamp = %llu UTC = %s",
2534fa9e4066Sahrens 	    (u_longlong_t)ub->ub_timestamp, asctime(localtime(&timestamp)));
2535e0f1c0afSOlaf Faaland 
2536e0f1c0afSOlaf Faaland 	(void) printf("\tmmp_magic = %016llx\n",
2537e0f1c0afSOlaf Faaland 	    (u_longlong_t)ub->ub_mmp_magic);
25384348eb90SOlaf Faaland 	if (MMP_VALID(ub)) {
2539e0f1c0afSOlaf Faaland 		(void) printf("\tmmp_delay = %0llu\n",
2540e0f1c0afSOlaf Faaland 		    (u_longlong_t)ub->ub_mmp_delay);
25414348eb90SOlaf Faaland 		if (MMP_SEQ_VALID(ub))
25424348eb90SOlaf Faaland 			(void) printf("\tmmp_seq = %u\n",
25434348eb90SOlaf Faaland 			    (unsigned int) MMP_SEQ(ub));
25444348eb90SOlaf Faaland 		if (MMP_FAIL_INT_VALID(ub))
25454348eb90SOlaf Faaland 			(void) printf("\tmmp_fail = %u\n",
25464348eb90SOlaf Faaland 			    (unsigned int) MMP_FAIL_INT(ub));
25474348eb90SOlaf Faaland 		if (MMP_INTERVAL_VALID(ub))
25484348eb90SOlaf Faaland 			(void) printf("\tmmp_write = %u\n",
25494348eb90SOlaf Faaland 			    (unsigned int) MMP_INTERVAL(ub));
25504348eb90SOlaf Faaland 		/* After MMP_* to make summarize_uberblock_mmp cleaner */
25514348eb90SOlaf Faaland 		(void) printf("\tmmp_valid = %x\n",
25524348eb90SOlaf Faaland 		    (unsigned int) ub->ub_mmp_config & 0xFF);
25534348eb90SOlaf Faaland 	}
2554e0f1c0afSOlaf Faaland 
25550caa7519SOlaf Faaland 	if (dump_opt['u'] >= 4) {
2556fbabab8fSmaybee 		char blkbuf[BP_SPRINTF_LEN];
255743466aaeSMax Grossman 		snprintf_blkptr(blkbuf, sizeof (blkbuf), &ub->ub_rootbp);
2558fa9e4066Sahrens 		(void) printf("\trootbp = %s\n", blkbuf);
2559fa9e4066Sahrens 	}
256086714001SSerapheim Dimitropoulos 	(void) printf("\tcheckpoint_txg = %llu\n",
256186714001SSerapheim Dimitropoulos 	    (u_longlong_t)ub->ub_checkpoint_txg);
25623f7978d0SAlan Somers 	(void) printf("%s", footer ? footer : "");
2563fa9e4066Sahrens }
2564fa9e4066Sahrens 
2565fa9e4066Sahrens static void
256607428bdfSVictor Latushkin dump_config(spa_t *spa)
2567fa9e4066Sahrens {
256807428bdfSVictor Latushkin 	dmu_buf_t *db;
256907428bdfSVictor Latushkin 	size_t nvsize = 0;
257007428bdfSVictor Latushkin 	int error = 0;
257107428bdfSVictor Latushkin 
2572fa9e4066Sahrens 
257307428bdfSVictor Latushkin 	error = dmu_bonus_hold(spa->spa_meta_objset,
257407428bdfSVictor Latushkin 	    spa->spa_config_object, FTAG, &db);
257507428bdfSVictor Latushkin 
257607428bdfSVictor Latushkin 	if (error == 0) {
257707428bdfSVictor Latushkin 		nvsize = *(uint64_t *)db->db_data;
257807428bdfSVictor Latushkin 		dmu_buf_rele(db, FTAG);
257907428bdfSVictor Latushkin 
258007428bdfSVictor Latushkin 		(void) printf("\nMOS Configuration:\n");
258107428bdfSVictor Latushkin 		dump_packed_nvlist(spa->spa_meta_objset,
258207428bdfSVictor Latushkin 		    spa->spa_config_object, (void *)&nvsize, 1);
258307428bdfSVictor Latushkin 	} else {
258407428bdfSVictor Latushkin 		(void) fprintf(stderr, "dmu_bonus_hold(%llu) failed, errno %d",
258507428bdfSVictor Latushkin 		    (u_longlong_t)spa->spa_config_object, error);
2586fa9e4066Sahrens 	}
2587fa9e4066Sahrens }
2588fa9e4066Sahrens 
2589c5904d13Seschrock static void
2590c5904d13Seschrock dump_cachefile(const char *cachefile)
2591c5904d13Seschrock {
2592c5904d13Seschrock 	int fd;
2593c5904d13Seschrock 	struct stat64 statbuf;
2594c5904d13Seschrock 	char *buf;
2595c5904d13Seschrock 	nvlist_t *config;
2596c5904d13Seschrock 
2597c5904d13Seschrock 	if ((fd = open64(cachefile, O_RDONLY)) < 0) {
2598c5904d13Seschrock 		(void) printf("cannot open '%s': %s\n", cachefile,
2599c5904d13Seschrock 		    strerror(errno));
2600c5904d13Seschrock 		exit(1);
2601c5904d13Seschrock 	}
2602c5904d13Seschrock 
2603c5904d13Seschrock 	if (fstat64(fd, &statbuf) != 0) {
2604c5904d13Seschrock 		(void) printf("failed to stat '%s': %s\n", cachefile,
2605c5904d13Seschrock 		    strerror(errno));
2606c5904d13Seschrock 		exit(1);
2607c5904d13Seschrock 	}
2608c5904d13Seschrock 
2609c5904d13Seschrock 	if ((buf = malloc(statbuf.st_size)) == NULL) {
2610c5904d13Seschrock 		(void) fprintf(stderr, "failed to allocate %llu bytes\n",
2611c5904d13Seschrock 		    (u_longlong_t)statbuf.st_size);
2612c5904d13Seschrock 		exit(1);
2613c5904d13Seschrock 	}
2614c5904d13Seschrock 
2615c5904d13Seschrock 	if (read(fd, buf, statbuf.st_size) != statbuf.st_size) {
2616c5904d13Seschrock 		(void) fprintf(stderr, "failed to read %llu bytes\n",
2617c5904d13Seschrock 		    (u_longlong_t)statbuf.st_size);
2618c5904d13Seschrock 		exit(1);
2619c5904d13Seschrock 	}
2620c5904d13Seschrock 
2621c5904d13Seschrock 	(void) close(fd);
2622c5904d13Seschrock 
2623c5904d13Seschrock 	if (nvlist_unpack(buf, statbuf.st_size, &config, 0) != 0) {
2624c5904d13Seschrock 		(void) fprintf(stderr, "failed to unpack nvlist\n");
2625c5904d13Seschrock 		exit(1);
2626c5904d13Seschrock 	}
2627c5904d13Seschrock 
2628c5904d13Seschrock 	free(buf);
2629c5904d13Seschrock 
2630c5904d13Seschrock 	dump_nvlist(config, 0);
2631c5904d13Seschrock 
2632c5904d13Seschrock 	nvlist_free(config);
2633c5904d13Seschrock }
2634c5904d13Seschrock 
2635*f0a05239SGeorge Amanakis static void
2636*f0a05239SGeorge Amanakis print_l2arc_header(void)
2637*f0a05239SGeorge Amanakis {
2638*f0a05239SGeorge Amanakis 	(void) printf("------------------------------------\n");
2639*f0a05239SGeorge Amanakis 	(void) printf("L2ARC device header\n");
2640*f0a05239SGeorge Amanakis 	(void) printf("------------------------------------\n");
2641*f0a05239SGeorge Amanakis }
2642*f0a05239SGeorge Amanakis 
2643*f0a05239SGeorge Amanakis static void
2644*f0a05239SGeorge Amanakis print_l2arc_log_blocks(void)
2645*f0a05239SGeorge Amanakis {
2646*f0a05239SGeorge Amanakis 	(void) printf("------------------------------------\n");
2647*f0a05239SGeorge Amanakis 	(void) printf("L2ARC device log blocks\n");
2648*f0a05239SGeorge Amanakis 	(void) printf("------------------------------------\n");
2649*f0a05239SGeorge Amanakis }
2650*f0a05239SGeorge Amanakis 
2651*f0a05239SGeorge Amanakis static void
2652*f0a05239SGeorge Amanakis dump_l2arc_log_entries(uint64_t log_entries,
2653*f0a05239SGeorge Amanakis     l2arc_log_ent_phys_t *le, uint64_t i)
2654*f0a05239SGeorge Amanakis {
2655*f0a05239SGeorge Amanakis 	for (uint64_t j = 0; j < log_entries; j++) {
2656*f0a05239SGeorge Amanakis 		dva_t dva = le[j].le_dva;
2657*f0a05239SGeorge Amanakis 		(void) printf("lb[%4llu]\tle[%4d]\tDVA asize: %llu, "
2658*f0a05239SGeorge Amanakis 		    "vdev: %llu, offset: %llu\n",
2659*f0a05239SGeorge Amanakis 		    (u_longlong_t)i, j + 1,
2660*f0a05239SGeorge Amanakis 		    (u_longlong_t)DVA_GET_ASIZE(&dva),
2661*f0a05239SGeorge Amanakis 		    (u_longlong_t)DVA_GET_VDEV(&dva),
2662*f0a05239SGeorge Amanakis 		    (u_longlong_t)DVA_GET_OFFSET(&dva));
2663*f0a05239SGeorge Amanakis 		(void) printf("|\t\t\t\tbirth: %llu\n",
2664*f0a05239SGeorge Amanakis 		    (u_longlong_t)le[j].le_birth);
2665*f0a05239SGeorge Amanakis 		(void) printf("|\t\t\t\tlsize: %llu\n",
2666*f0a05239SGeorge Amanakis 		    (u_longlong_t)L2BLK_GET_LSIZE((&le[j])->le_prop));
2667*f0a05239SGeorge Amanakis 		(void) printf("|\t\t\t\tpsize: %llu\n",
2668*f0a05239SGeorge Amanakis 		    (u_longlong_t)L2BLK_GET_PSIZE((&le[j])->le_prop));
2669*f0a05239SGeorge Amanakis 		(void) printf("|\t\t\t\tcompr: %llu\n",
2670*f0a05239SGeorge Amanakis 		    (u_longlong_t)L2BLK_GET_COMPRESS((&le[j])->le_prop));
2671*f0a05239SGeorge Amanakis 		(void) printf("|\t\t\t\ttype: %llu\n",
2672*f0a05239SGeorge Amanakis 		    (u_longlong_t)L2BLK_GET_TYPE((&le[j])->le_prop));
2673*f0a05239SGeorge Amanakis 		(void) printf("|\t\t\t\tprotected: %llu\n",
2674*f0a05239SGeorge Amanakis 		    (u_longlong_t)L2BLK_GET_PROTECTED((&le[j])->le_prop));
2675*f0a05239SGeorge Amanakis 		(void) printf("|\t\t\t\tprefetch: %llu\n",
2676*f0a05239SGeorge Amanakis 		    (u_longlong_t)L2BLK_GET_PREFETCH((&le[j])->le_prop));
2677*f0a05239SGeorge Amanakis 		(void) printf("|\t\t\t\taddress: %llu\n",
2678*f0a05239SGeorge Amanakis 		    (u_longlong_t)le[j].le_daddr);
2679*f0a05239SGeorge Amanakis 		(void) printf("|\n");
2680*f0a05239SGeorge Amanakis 	}
2681*f0a05239SGeorge Amanakis 	(void) printf("\n");
2682*f0a05239SGeorge Amanakis }
2683*f0a05239SGeorge Amanakis 
2684*f0a05239SGeorge Amanakis static void
2685*f0a05239SGeorge Amanakis dump_l2arc_log_blkptr(l2arc_log_blkptr_t lbps)
2686*f0a05239SGeorge Amanakis {
2687*f0a05239SGeorge Amanakis 	(void) printf("|\t\tdaddr: %llu\n", (u_longlong_t)lbps.lbp_daddr);
2688*f0a05239SGeorge Amanakis 	(void) printf("|\t\tpayload_asize: %llu\n",
2689*f0a05239SGeorge Amanakis 	    (u_longlong_t)lbps.lbp_payload_asize);
2690*f0a05239SGeorge Amanakis 	(void) printf("|\t\tpayload_start: %llu\n",
2691*f0a05239SGeorge Amanakis 	    (u_longlong_t)lbps.lbp_payload_start);
2692*f0a05239SGeorge Amanakis 	(void) printf("|\t\tlsize: %llu\n",
2693*f0a05239SGeorge Amanakis 	    (u_longlong_t)L2BLK_GET_LSIZE((&lbps)->lbp_prop));
2694*f0a05239SGeorge Amanakis 	(void) printf("|\t\tasize: %llu\n",
2695*f0a05239SGeorge Amanakis 	    (u_longlong_t)L2BLK_GET_PSIZE((&lbps)->lbp_prop));
2696*f0a05239SGeorge Amanakis 	(void) printf("|\t\tcompralgo: %llu\n",
2697*f0a05239SGeorge Amanakis 	    (u_longlong_t)L2BLK_GET_COMPRESS((&lbps)->lbp_prop));
2698*f0a05239SGeorge Amanakis 	(void) printf("|\t\tcksumalgo: %llu\n",
2699*f0a05239SGeorge Amanakis 	    (u_longlong_t)L2BLK_GET_CHECKSUM((&lbps)->lbp_prop));
2700*f0a05239SGeorge Amanakis 	(void) printf("|\n\n");
2701*f0a05239SGeorge Amanakis }
2702*f0a05239SGeorge Amanakis 
2703*f0a05239SGeorge Amanakis static void
2704*f0a05239SGeorge Amanakis dump_l2arc_log_blocks(int fd, l2arc_dev_hdr_phys_t l2dhdr,
2705*f0a05239SGeorge Amanakis     l2arc_dev_hdr_phys_t *rebuild)
2706*f0a05239SGeorge Amanakis {
2707*f0a05239SGeorge Amanakis 	l2arc_log_blk_phys_t this_lb;
2708*f0a05239SGeorge Amanakis 	uint64_t asize;
2709*f0a05239SGeorge Amanakis 	l2arc_log_blkptr_t lbps[2];
2710*f0a05239SGeorge Amanakis 	abd_t *abd;
2711*f0a05239SGeorge Amanakis 	zio_cksum_t cksum;
2712*f0a05239SGeorge Amanakis 	int failed = 0;
2713*f0a05239SGeorge Amanakis 	l2arc_dev_t dev;
2714*f0a05239SGeorge Amanakis 
2715*f0a05239SGeorge Amanakis 	if (!dump_opt['q'])
2716*f0a05239SGeorge Amanakis 		print_l2arc_log_blocks();
2717*f0a05239SGeorge Amanakis 	bcopy((&l2dhdr)->dh_start_lbps, lbps, sizeof (lbps));
2718*f0a05239SGeorge Amanakis 
2719*f0a05239SGeorge Amanakis 	dev.l2ad_evict = l2dhdr.dh_evict;
2720*f0a05239SGeorge Amanakis 	dev.l2ad_start = l2dhdr.dh_start;
2721*f0a05239SGeorge Amanakis 	dev.l2ad_end = l2dhdr.dh_end;
2722*f0a05239SGeorge Amanakis 
2723*f0a05239SGeorge Amanakis 	if (l2dhdr.dh_start_lbps[0].lbp_daddr == 0) {
2724*f0a05239SGeorge Amanakis 		/* no log blocks to read */
2725*f0a05239SGeorge Amanakis 		if (!dump_opt['q']) {
2726*f0a05239SGeorge Amanakis 			(void) printf("No log blocks to read\n");
2727*f0a05239SGeorge Amanakis 			(void) printf("\n");
2728*f0a05239SGeorge Amanakis 		}
2729*f0a05239SGeorge Amanakis 		return;
2730*f0a05239SGeorge Amanakis 	} else {
2731*f0a05239SGeorge Amanakis 		dev.l2ad_hand = lbps[0].lbp_daddr +
2732*f0a05239SGeorge Amanakis 		    L2BLK_GET_PSIZE((&lbps[0])->lbp_prop);
2733*f0a05239SGeorge Amanakis 	}
2734*f0a05239SGeorge Amanakis 
2735*f0a05239SGeorge Amanakis 	dev.l2ad_first = !!(l2dhdr.dh_flags & L2ARC_DEV_HDR_EVICT_FIRST);
2736*f0a05239SGeorge Amanakis 
2737*f0a05239SGeorge Amanakis 	for (;;) {
2738*f0a05239SGeorge Amanakis 		if (!l2arc_log_blkptr_valid(&dev, &lbps[0]))
2739*f0a05239SGeorge Amanakis 			break;
2740*f0a05239SGeorge Amanakis 
2741*f0a05239SGeorge Amanakis 		/* L2BLK_GET_PSIZE returns aligned size for log blocks */
2742*f0a05239SGeorge Amanakis 		asize = L2BLK_GET_PSIZE((&lbps[0])->lbp_prop);
2743*f0a05239SGeorge Amanakis 		if (pread64(fd, &this_lb, asize, lbps[0].lbp_daddr) !=
2744*f0a05239SGeorge Amanakis 		    (ssize_t)asize) {
2745*f0a05239SGeorge Amanakis 			if (!dump_opt['q']) {
2746*f0a05239SGeorge Amanakis 				(void) printf("Error while reading next log "
2747*f0a05239SGeorge Amanakis 				    "block\n\n");
2748*f0a05239SGeorge Amanakis 			}
2749*f0a05239SGeorge Amanakis 			break;
2750*f0a05239SGeorge Amanakis 		}
2751*f0a05239SGeorge Amanakis 
2752*f0a05239SGeorge Amanakis 		fletcher_4_native(&this_lb, asize, NULL, &cksum);
2753*f0a05239SGeorge Amanakis 		if (!ZIO_CHECKSUM_EQUAL(cksum, lbps[0].lbp_cksum)) {
2754*f0a05239SGeorge Amanakis 			failed++;
2755*f0a05239SGeorge Amanakis 			if (!dump_opt['q']) {
2756*f0a05239SGeorge Amanakis 				(void) printf("Invalid cksum\n");
2757*f0a05239SGeorge Amanakis 				dump_l2arc_log_blkptr(lbps[0]);
2758*f0a05239SGeorge Amanakis 			}
2759*f0a05239SGeorge Amanakis 			break;
2760*f0a05239SGeorge Amanakis 		}
2761*f0a05239SGeorge Amanakis 
2762*f0a05239SGeorge Amanakis 		switch (L2BLK_GET_COMPRESS((&lbps[0])->lbp_prop)) {
2763*f0a05239SGeorge Amanakis 		case ZIO_COMPRESS_OFF:
2764*f0a05239SGeorge Amanakis 			break;
2765*f0a05239SGeorge Amanakis 		case ZIO_COMPRESS_LZ4:
2766*f0a05239SGeorge Amanakis 			abd = abd_alloc_for_io(asize, B_TRUE);
2767*f0a05239SGeorge Amanakis 			abd_copy_from_buf_off(abd, &this_lb, 0, asize);
2768*f0a05239SGeorge Amanakis 			zio_decompress_data(L2BLK_GET_COMPRESS(
2769*f0a05239SGeorge Amanakis 			    (&lbps[0])->lbp_prop), abd, &this_lb,
2770*f0a05239SGeorge Amanakis 			    asize, sizeof (this_lb));
2771*f0a05239SGeorge Amanakis 			abd_free(abd);
2772*f0a05239SGeorge Amanakis 			break;
2773*f0a05239SGeorge Amanakis 		default:
2774*f0a05239SGeorge Amanakis 			break;
2775*f0a05239SGeorge Amanakis 		}
2776*f0a05239SGeorge Amanakis 
2777*f0a05239SGeorge Amanakis 		if (this_lb.lb_magic == BSWAP_64(L2ARC_LOG_BLK_MAGIC))
2778*f0a05239SGeorge Amanakis 			byteswap_uint64_array(&this_lb, sizeof (this_lb));
2779*f0a05239SGeorge Amanakis 		if (this_lb.lb_magic != L2ARC_LOG_BLK_MAGIC) {
2780*f0a05239SGeorge Amanakis 			if (!dump_opt['q'])
2781*f0a05239SGeorge Amanakis 				(void) printf("Invalid log block magic\n\n");
2782*f0a05239SGeorge Amanakis 			break;
2783*f0a05239SGeorge Amanakis 		}
2784*f0a05239SGeorge Amanakis 
2785*f0a05239SGeorge Amanakis 		rebuild->dh_lb_count++;
2786*f0a05239SGeorge Amanakis 		rebuild->dh_lb_asize += asize;
2787*f0a05239SGeorge Amanakis 		if (dump_opt['l'] > 1 && !dump_opt['q']) {
2788*f0a05239SGeorge Amanakis 			(void) printf("lb[%4llu]\tmagic: %llu\n",
2789*f0a05239SGeorge Amanakis 			    (u_longlong_t)rebuild->dh_lb_count,
2790*f0a05239SGeorge Amanakis 			    (u_longlong_t)this_lb.lb_magic);
2791*f0a05239SGeorge Amanakis 			dump_l2arc_log_blkptr(lbps[0]);
2792*f0a05239SGeorge Amanakis 		}
2793*f0a05239SGeorge Amanakis 
2794*f0a05239SGeorge Amanakis 		if (dump_opt['l'] > 2 && !dump_opt['q'])
2795*f0a05239SGeorge Amanakis 			dump_l2arc_log_entries(l2dhdr.dh_log_entries,
2796*f0a05239SGeorge Amanakis 			    this_lb.lb_entries,
2797*f0a05239SGeorge Amanakis 			    rebuild->dh_lb_count);
2798*f0a05239SGeorge Amanakis 
2799*f0a05239SGeorge Amanakis 		if (l2arc_range_check_overlap(lbps[1].lbp_payload_start,
2800*f0a05239SGeorge Amanakis 		    lbps[0].lbp_payload_start, dev.l2ad_evict) &&
2801*f0a05239SGeorge Amanakis 		    !dev.l2ad_first)
2802*f0a05239SGeorge Amanakis 			break;
2803*f0a05239SGeorge Amanakis 
2804*f0a05239SGeorge Amanakis 		lbps[0] = lbps[1];
2805*f0a05239SGeorge Amanakis 		lbps[1] = this_lb.lb_prev_lbp;
2806*f0a05239SGeorge Amanakis 	}
2807*f0a05239SGeorge Amanakis 
2808*f0a05239SGeorge Amanakis 	if (!dump_opt['q']) {
2809*f0a05239SGeorge Amanakis 		(void) printf("log_blk_count:\t %llu with valid cksum\n",
2810*f0a05239SGeorge Amanakis 		    (u_longlong_t)rebuild->dh_lb_count);
2811*f0a05239SGeorge Amanakis 		(void) printf("\t\t %d with invalid cksum\n", failed);
2812*f0a05239SGeorge Amanakis 		(void) printf("log_blk_asize:\t %llu\n\n",
2813*f0a05239SGeorge Amanakis 		    (u_longlong_t)rebuild->dh_lb_asize);
2814*f0a05239SGeorge Amanakis 	}
2815*f0a05239SGeorge Amanakis }
2816*f0a05239SGeorge Amanakis 
2817*f0a05239SGeorge Amanakis static int
2818*f0a05239SGeorge Amanakis dump_l2arc_header(int fd)
2819*f0a05239SGeorge Amanakis {
2820*f0a05239SGeorge Amanakis 	l2arc_dev_hdr_phys_t l2dhdr, rebuild;
2821*f0a05239SGeorge Amanakis 	int error = B_FALSE;
2822*f0a05239SGeorge Amanakis 
2823*f0a05239SGeorge Amanakis 	bzero(&l2dhdr, sizeof (l2dhdr));
2824*f0a05239SGeorge Amanakis 	bzero(&rebuild, sizeof (rebuild));
2825*f0a05239SGeorge Amanakis 
2826*f0a05239SGeorge Amanakis 	if (pread64(fd, &l2dhdr, sizeof (l2dhdr),
2827*f0a05239SGeorge Amanakis 	    VDEV_LABEL_START_SIZE) != sizeof (l2dhdr)) {
2828*f0a05239SGeorge Amanakis 		error = B_TRUE;
2829*f0a05239SGeorge Amanakis 	} else {
2830*f0a05239SGeorge Amanakis 		if (l2dhdr.dh_magic == BSWAP_64(L2ARC_DEV_HDR_MAGIC))
2831*f0a05239SGeorge Amanakis 			byteswap_uint64_array(&l2dhdr, sizeof (l2dhdr));
2832*f0a05239SGeorge Amanakis 
2833*f0a05239SGeorge Amanakis 		if (l2dhdr.dh_magic != L2ARC_DEV_HDR_MAGIC)
2834*f0a05239SGeorge Amanakis 			error = B_TRUE;
2835*f0a05239SGeorge Amanakis 	}
2836*f0a05239SGeorge Amanakis 
2837*f0a05239SGeorge Amanakis 	if (error) {
2838*f0a05239SGeorge Amanakis 		(void) printf("L2ARC device header not found\n\n");
2839*f0a05239SGeorge Amanakis 		/* Do not return an error here for backward compatibility */
2840*f0a05239SGeorge Amanakis 		return (0);
2841*f0a05239SGeorge Amanakis 	} else if (!dump_opt['q']) {
2842*f0a05239SGeorge Amanakis 		print_l2arc_header();
2843*f0a05239SGeorge Amanakis 
2844*f0a05239SGeorge Amanakis 		(void) printf("    magic: %llu\n",
2845*f0a05239SGeorge Amanakis 		    (u_longlong_t)l2dhdr.dh_magic);
2846*f0a05239SGeorge Amanakis 		(void) printf("    version: %llu\n",
2847*f0a05239SGeorge Amanakis 		    (u_longlong_t)l2dhdr.dh_version);
2848*f0a05239SGeorge Amanakis 		(void) printf("    pool_guid: %llu\n",
2849*f0a05239SGeorge Amanakis 		    (u_longlong_t)l2dhdr.dh_spa_guid);
2850*f0a05239SGeorge Amanakis 		(void) printf("    flags: %llu\n",
2851*f0a05239SGeorge Amanakis 		    (u_longlong_t)l2dhdr.dh_flags);
2852*f0a05239SGeorge Amanakis 		(void) printf("    start_lbps[0]: %llu\n",
2853*f0a05239SGeorge Amanakis 		    (u_longlong_t)
2854*f0a05239SGeorge Amanakis 		    l2dhdr.dh_start_lbps[0].lbp_daddr);
2855*f0a05239SGeorge Amanakis 		(void) printf("    start_lbps[1]: %llu\n",
2856*f0a05239SGeorge Amanakis 		    (u_longlong_t)
2857*f0a05239SGeorge Amanakis 		    l2dhdr.dh_start_lbps[1].lbp_daddr);
2858*f0a05239SGeorge Amanakis 		(void) printf("    log_blk_ent: %llu\n",
2859*f0a05239SGeorge Amanakis 		    (u_longlong_t)l2dhdr.dh_log_entries);
2860*f0a05239SGeorge Amanakis 		(void) printf("    start: %llu\n",
2861*f0a05239SGeorge Amanakis 		    (u_longlong_t)l2dhdr.dh_start);
2862*f0a05239SGeorge Amanakis 		(void) printf("    end: %llu\n",
2863*f0a05239SGeorge Amanakis 		    (u_longlong_t)l2dhdr.dh_end);
2864*f0a05239SGeorge Amanakis 		(void) printf("    evict: %llu\n",
2865*f0a05239SGeorge Amanakis 		    (u_longlong_t)l2dhdr.dh_evict);
2866*f0a05239SGeorge Amanakis 		(void) printf("    lb_asize_refcount: %llu\n",
2867*f0a05239SGeorge Amanakis 		    (u_longlong_t)l2dhdr.dh_lb_asize);
2868*f0a05239SGeorge Amanakis 		(void) printf("    lb_count_refcount: %llu\n\n",
2869*f0a05239SGeorge Amanakis 		    (u_longlong_t)l2dhdr.dh_lb_count);
2870*f0a05239SGeorge Amanakis 	}
2871*f0a05239SGeorge Amanakis 
2872*f0a05239SGeorge Amanakis 	dump_l2arc_log_blocks(fd, l2dhdr, &rebuild);
2873*f0a05239SGeorge Amanakis 	/*
2874*f0a05239SGeorge Amanakis 	 * The total aligned size of log blocks and the number of log blocks
2875*f0a05239SGeorge Amanakis 	 * reported in the header of the device may be less than what zdb
2876*f0a05239SGeorge Amanakis 	 * reports by dump_l2arc_log_blocks() which emulates l2arc_rebuild().
2877*f0a05239SGeorge Amanakis 	 * This happens because dump_l2arc_log_blocks() lacks the memory
2878*f0a05239SGeorge Amanakis 	 * pressure valve that l2arc_rebuild() has. Thus, if we are on a system
2879*f0a05239SGeorge Amanakis 	 * with low memory, l2arc_rebuild will exit prematurely and dh_lb_asize
2880*f0a05239SGeorge Amanakis 	 * and dh_lb_count will be lower to begin with than what exists on the
2881*f0a05239SGeorge Amanakis 	 * device. This is normal and zdb should not exit with an error. The
2882*f0a05239SGeorge Amanakis 	 * opposite case should never happen though, the values reported in the
2883*f0a05239SGeorge Amanakis 	 * header should never be higher than what dump_l2arc_log_blocks() and
2884*f0a05239SGeorge Amanakis 	 * l2arc_rebuild() report. If this happens there is a leak in the
2885*f0a05239SGeorge Amanakis 	 * accounting of log blocks.
2886*f0a05239SGeorge Amanakis 	 */
2887*f0a05239SGeorge Amanakis 	if (l2dhdr.dh_lb_asize > rebuild.dh_lb_asize ||
2888*f0a05239SGeorge Amanakis 	    l2dhdr.dh_lb_count > rebuild.dh_lb_count)
2889*f0a05239SGeorge Amanakis 		return (1);
2890*f0a05239SGeorge Amanakis 
2891*f0a05239SGeorge Amanakis 	return (0);
2892*f0a05239SGeorge Amanakis }
2893*f0a05239SGeorge Amanakis 
2894ed61ec1dSYuri Pankov static char curpath[PATH_MAX];
2895ed61ec1dSYuri Pankov 
2896ed61ec1dSYuri Pankov /*
2897ed61ec1dSYuri Pankov  * Iterate through the path components, recursively passing
2898ed61ec1dSYuri Pankov  * current one's obj and remaining path until we find the obj
2899ed61ec1dSYuri Pankov  * for the last one.
2900ed61ec1dSYuri Pankov  */
2901ed61ec1dSYuri Pankov static int
2902ed61ec1dSYuri Pankov dump_path_impl(objset_t *os, uint64_t obj, char *name)
2903ed61ec1dSYuri Pankov {
2904ed61ec1dSYuri Pankov 	int err;
2905ed61ec1dSYuri Pankov 	int header = 1;
2906ed61ec1dSYuri Pankov 	uint64_t child_obj;
2907ed61ec1dSYuri Pankov 	char *s;
2908ed61ec1dSYuri Pankov 	dmu_buf_t *db;
2909ed61ec1dSYuri Pankov 	dmu_object_info_t doi;
2910ed61ec1dSYuri Pankov 
2911ed61ec1dSYuri Pankov 	if ((s = strchr(name, '/')) != NULL)
2912ed61ec1dSYuri Pankov 		*s = '\0';
2913ed61ec1dSYuri Pankov 	err = zap_lookup(os, obj, name, 8, 1, &child_obj);
2914ed61ec1dSYuri Pankov 
2915ed61ec1dSYuri Pankov 	(void) strlcat(curpath, name, sizeof (curpath));
2916ed61ec1dSYuri Pankov 
2917ed61ec1dSYuri Pankov 	if (err != 0) {
2918ed61ec1dSYuri Pankov 		(void) fprintf(stderr, "failed to lookup %s: %s\n",
2919ed61ec1dSYuri Pankov 		    curpath, strerror(err));
2920ed61ec1dSYuri Pankov 		return (err);
2921ed61ec1dSYuri Pankov 	}
2922ed61ec1dSYuri Pankov 
2923ed61ec1dSYuri Pankov 	child_obj = ZFS_DIRENT_OBJ(child_obj);
2924ed61ec1dSYuri Pankov 	err = sa_buf_hold(os, child_obj, FTAG, &db);
2925ed61ec1dSYuri Pankov 	if (err != 0) {
2926ed61ec1dSYuri Pankov 		(void) fprintf(stderr,
2927ed61ec1dSYuri Pankov 		    "failed to get SA dbuf for obj %llu: %s\n",
2928ed61ec1dSYuri Pankov 		    (u_longlong_t)child_obj, strerror(err));
2929ed61ec1dSYuri Pankov 		return (EINVAL);
2930ed61ec1dSYuri Pankov 	}
2931ed61ec1dSYuri Pankov 	dmu_object_info_from_db(db, &doi);
2932ed61ec1dSYuri Pankov 	sa_buf_rele(db, FTAG);
2933ed61ec1dSYuri Pankov 
2934ed61ec1dSYuri Pankov 	if (doi.doi_bonus_type != DMU_OT_SA &&
2935ed61ec1dSYuri Pankov 	    doi.doi_bonus_type != DMU_OT_ZNODE) {
2936ed61ec1dSYuri Pankov 		(void) fprintf(stderr, "invalid bonus type %d for obj %llu\n",
2937ed61ec1dSYuri Pankov 		    doi.doi_bonus_type, (u_longlong_t)child_obj);
2938ed61ec1dSYuri Pankov 		return (EINVAL);
2939ed61ec1dSYuri Pankov 	}
2940ed61ec1dSYuri Pankov 
2941ed61ec1dSYuri Pankov 	if (dump_opt['v'] > 6) {
2942ed61ec1dSYuri Pankov 		(void) printf("obj=%llu %s type=%d bonustype=%d\n",
2943ed61ec1dSYuri Pankov 		    (u_longlong_t)child_obj, curpath, doi.doi_type,
2944ed61ec1dSYuri Pankov 		    doi.doi_bonus_type);
2945ed61ec1dSYuri Pankov 	}
2946ed61ec1dSYuri Pankov 
2947ed61ec1dSYuri Pankov 	(void) strlcat(curpath, "/", sizeof (curpath));
2948ed61ec1dSYuri Pankov 
2949ed61ec1dSYuri Pankov 	switch (doi.doi_type) {
2950ed61ec1dSYuri Pankov 	case DMU_OT_DIRECTORY_CONTENTS:
2951ed61ec1dSYuri Pankov 		if (s != NULL && *(s + 1) != '\0')
2952ed61ec1dSYuri Pankov 			return (dump_path_impl(os, child_obj, s + 1));
2953ed61ec1dSYuri Pankov 		/*FALLTHROUGH*/
2954ed61ec1dSYuri Pankov 	case DMU_OT_PLAIN_FILE_CONTENTS:
295554811da5SToomas Soome 		dump_object(os, child_obj, dump_opt['v'], &header, NULL);
2956ed61ec1dSYuri Pankov 		return (0);
2957ed61ec1dSYuri Pankov 	default:
2958ed61ec1dSYuri Pankov 		(void) fprintf(stderr, "object %llu has non-file/directory "
2959ed61ec1dSYuri Pankov 		    "type %d\n", (u_longlong_t)obj, doi.doi_type);
2960ed61ec1dSYuri Pankov 		break;
2961ed61ec1dSYuri Pankov 	}
2962ed61ec1dSYuri Pankov 
2963ed61ec1dSYuri Pankov 	return (EINVAL);
2964ed61ec1dSYuri Pankov }
2965ed61ec1dSYuri Pankov 
2966ed61ec1dSYuri Pankov /*
2967ed61ec1dSYuri Pankov  * Dump the blocks for the object specified by path inside the dataset.
2968ed61ec1dSYuri Pankov  */
2969ed61ec1dSYuri Pankov static int
2970ed61ec1dSYuri Pankov dump_path(char *ds, char *path)
2971ed61ec1dSYuri Pankov {
2972ed61ec1dSYuri Pankov 	int err;
2973ed61ec1dSYuri Pankov 	objset_t *os;
2974ed61ec1dSYuri Pankov 	uint64_t root_obj;
2975ed61ec1dSYuri Pankov 
2976ed61ec1dSYuri Pankov 	err = open_objset(ds, DMU_OST_ZFS, FTAG, &os);
2977ed61ec1dSYuri Pankov 	if (err != 0)
2978ed61ec1dSYuri Pankov 		return (err);
2979ed61ec1dSYuri Pankov 
2980ed61ec1dSYuri Pankov 	err = zap_lookup(os, MASTER_NODE_OBJ, ZFS_ROOT_OBJ, 8, 1, &root_obj);
2981ed61ec1dSYuri Pankov 	if (err != 0) {
2982ed61ec1dSYuri Pankov 		(void) fprintf(stderr, "can't lookup root znode: %s\n",
2983ed61ec1dSYuri Pankov 		    strerror(err));
2984eb633035STom Caputi 		dmu_objset_disown(os, B_FALSE, FTAG);
2985ed61ec1dSYuri Pankov 		return (EINVAL);
2986ed61ec1dSYuri Pankov 	}
2987ed61ec1dSYuri Pankov 
2988ed61ec1dSYuri Pankov 	(void) snprintf(curpath, sizeof (curpath), "dataset=%s path=/", ds);
2989ed61ec1dSYuri Pankov 
2990ed61ec1dSYuri Pankov 	err = dump_path_impl(os, root_obj, path);
2991ed61ec1dSYuri Pankov 
2992ed61ec1dSYuri Pankov 	close_objset(os, FTAG);
2993ed61ec1dSYuri Pankov 	return (err);
2994ed61ec1dSYuri Pankov }
2995ed61ec1dSYuri Pankov 
29960caa7519SOlaf Faaland typedef struct cksum_record {
29970caa7519SOlaf Faaland 	zio_cksum_t cksum;
29980caa7519SOlaf Faaland 	boolean_t labels[VDEV_LABELS];
29990caa7519SOlaf Faaland 	avl_node_t link;
30000caa7519SOlaf Faaland } cksum_record_t;
30010caa7519SOlaf Faaland 
300264723e36SYuri Pankov static int
30030caa7519SOlaf Faaland cksum_record_compare(const void *x1, const void *x2)
3004fa9e4066Sahrens {
30050caa7519SOlaf Faaland 	const cksum_record_t *l = (cksum_record_t *)x1;
30060caa7519SOlaf Faaland 	const cksum_record_t *r = (cksum_record_t *)x2;
30070caa7519SOlaf Faaland 	int arraysize = ARRAY_SIZE(l->cksum.zc_word);
30080caa7519SOlaf Faaland 	int difference;
30090caa7519SOlaf Faaland 
30100caa7519SOlaf Faaland 	for (int i = 0; i < arraysize; i++) {
30110caa7519SOlaf Faaland 		difference = AVL_CMP(l->cksum.zc_word[i], r->cksum.zc_word[i]);
30120caa7519SOlaf Faaland 		if (difference)
30130caa7519SOlaf Faaland 			break;
30140caa7519SOlaf Faaland 	}
30150caa7519SOlaf Faaland 
30160caa7519SOlaf Faaland 	return (difference);
30170caa7519SOlaf Faaland }
30180caa7519SOlaf Faaland 
30190caa7519SOlaf Faaland static cksum_record_t *
30200caa7519SOlaf Faaland cksum_record_alloc(zio_cksum_t *cksum, int l)
30210caa7519SOlaf Faaland {
30220caa7519SOlaf Faaland 	cksum_record_t *rec;
30230caa7519SOlaf Faaland 
30240caa7519SOlaf Faaland 	rec = umem_zalloc(sizeof (*rec), UMEM_NOFAIL);
30250caa7519SOlaf Faaland 	rec->cksum = *cksum;
30260caa7519SOlaf Faaland 	rec->labels[l] = B_TRUE;
30270caa7519SOlaf Faaland 
30280caa7519SOlaf Faaland 	return (rec);
30290caa7519SOlaf Faaland }
30300caa7519SOlaf Faaland 
30310caa7519SOlaf Faaland static cksum_record_t *
30320caa7519SOlaf Faaland cksum_record_lookup(avl_tree_t *tree, zio_cksum_t *cksum)
30330caa7519SOlaf Faaland {
30340caa7519SOlaf Faaland 	cksum_record_t lookup = { .cksum = *cksum };
30350caa7519SOlaf Faaland 	avl_index_t where;
30360caa7519SOlaf Faaland 
30370caa7519SOlaf Faaland 	return (avl_find(tree, &lookup, &where));
30380caa7519SOlaf Faaland }
30390caa7519SOlaf Faaland 
30400caa7519SOlaf Faaland static cksum_record_t *
30410caa7519SOlaf Faaland cksum_record_insert(avl_tree_t *tree, zio_cksum_t *cksum, int l)
30420caa7519SOlaf Faaland {
30430caa7519SOlaf Faaland 	cksum_record_t *rec;
30440caa7519SOlaf Faaland 
30450caa7519SOlaf Faaland 	rec = cksum_record_lookup(tree, cksum);
30460caa7519SOlaf Faaland 	if (rec) {
30470caa7519SOlaf Faaland 		rec->labels[l] = B_TRUE;
30480caa7519SOlaf Faaland 	} else {
30490caa7519SOlaf Faaland 		rec = cksum_record_alloc(cksum, l);
30500caa7519SOlaf Faaland 		avl_add(tree, rec);
30510caa7519SOlaf Faaland 	}
30520caa7519SOlaf Faaland 
30530caa7519SOlaf Faaland 	return (rec);
30540caa7519SOlaf Faaland }
30550caa7519SOlaf Faaland 
30560caa7519SOlaf Faaland static int
30570caa7519SOlaf Faaland first_label(cksum_record_t *rec)
30580caa7519SOlaf Faaland {
30590caa7519SOlaf Faaland 	for (int i = 0; i < VDEV_LABELS; i++)
30600caa7519SOlaf Faaland 		if (rec->labels[i])
30610caa7519SOlaf Faaland 			return (i);
30620caa7519SOlaf Faaland 
30630caa7519SOlaf Faaland 	return (-1);
30640caa7519SOlaf Faaland }
30650caa7519SOlaf Faaland 
30660caa7519SOlaf Faaland static void
30670caa7519SOlaf Faaland print_label_numbers(char *prefix, cksum_record_t *rec)
30680caa7519SOlaf Faaland {
30690caa7519SOlaf Faaland 	printf("%s", prefix);
30700caa7519SOlaf Faaland 	for (int i = 0; i < VDEV_LABELS; i++)
30710caa7519SOlaf Faaland 		if (rec->labels[i] == B_TRUE)
30720caa7519SOlaf Faaland 			printf("%d ", i);
30730caa7519SOlaf Faaland 	printf("\n");
30740caa7519SOlaf Faaland }
30750caa7519SOlaf Faaland 
30760caa7519SOlaf Faaland #define	MAX_UBERBLOCK_COUNT (VDEV_UBERBLOCK_RING >> UBERBLOCK_SHIFT)
30770caa7519SOlaf Faaland 
30780caa7519SOlaf Faaland typedef struct zdb_label {
3079fa9e4066Sahrens 	vdev_label_t label;
30800caa7519SOlaf Faaland 	nvlist_t *config_nv;
30810caa7519SOlaf Faaland 	cksum_record_t *config;
30820caa7519SOlaf Faaland 	cksum_record_t *uberblocks[MAX_UBERBLOCK_COUNT];
30830caa7519SOlaf Faaland 	boolean_t header_printed;
30840caa7519SOlaf Faaland 	boolean_t read_failed;
30850caa7519SOlaf Faaland } zdb_label_t;
30860caa7519SOlaf Faaland 
30870caa7519SOlaf Faaland static void
30880caa7519SOlaf Faaland print_label_header(zdb_label_t *label, int l)
30890caa7519SOlaf Faaland {
30900caa7519SOlaf Faaland 
30910caa7519SOlaf Faaland 	if (dump_opt['q'])
30920caa7519SOlaf Faaland 		return;
30930caa7519SOlaf Faaland 
30940caa7519SOlaf Faaland 	if (label->header_printed == B_TRUE)
30950caa7519SOlaf Faaland 		return;
30960caa7519SOlaf Faaland 
30970caa7519SOlaf Faaland 	(void) printf("------------------------------------\n");
30980caa7519SOlaf Faaland 	(void) printf("LABEL %d\n", l);
30990caa7519SOlaf Faaland 	(void) printf("------------------------------------\n");
31000caa7519SOlaf Faaland 
31010caa7519SOlaf Faaland 	label->header_printed = B_TRUE;
31020caa7519SOlaf Faaland }
31030caa7519SOlaf Faaland 
31040caa7519SOlaf Faaland static void
31050caa7519SOlaf Faaland dump_config_from_label(zdb_label_t *label, size_t buflen, int l)
31060caa7519SOlaf Faaland {
31070caa7519SOlaf Faaland 	if (dump_opt['q'])
31080caa7519SOlaf Faaland 		return;
31090caa7519SOlaf Faaland 
31100caa7519SOlaf Faaland 	if ((dump_opt['l'] < 3) && (first_label(label->config) != l))
31110caa7519SOlaf Faaland 		return;
31120caa7519SOlaf Faaland 
31130caa7519SOlaf Faaland 	print_label_header(label, l);
31140caa7519SOlaf Faaland 	dump_nvlist(label->config_nv, 4);
31150caa7519SOlaf Faaland 	print_label_numbers("    labels = ", label->config);
31160caa7519SOlaf Faaland }
31170caa7519SOlaf Faaland 
31180caa7519SOlaf Faaland #define	ZDB_MAX_UB_HEADER_SIZE 32
31190caa7519SOlaf Faaland 
31200caa7519SOlaf Faaland static void
31210caa7519SOlaf Faaland dump_label_uberblocks(zdb_label_t *label, uint64_t ashift, int label_num)
31220caa7519SOlaf Faaland {
31230caa7519SOlaf Faaland 	vdev_t vd;
31240caa7519SOlaf Faaland 	char header[ZDB_MAX_UB_HEADER_SIZE];
31250caa7519SOlaf Faaland 
31260caa7519SOlaf Faaland 	vd.vdev_ashift = ashift;
31270caa7519SOlaf Faaland 	vd.vdev_top = &vd;
31280caa7519SOlaf Faaland 
31290caa7519SOlaf Faaland 	for (int i = 0; i < VDEV_UBERBLOCK_COUNT(&vd); i++) {
31300caa7519SOlaf Faaland 		uint64_t uoff = VDEV_UBERBLOCK_OFFSET(&vd, i);
31310caa7519SOlaf Faaland 		uberblock_t *ub = (void *)((char *)&label->label + uoff);
31320caa7519SOlaf Faaland 		cksum_record_t *rec = label->uberblocks[i];
31330caa7519SOlaf Faaland 
31340caa7519SOlaf Faaland 		if (rec == NULL) {
31350caa7519SOlaf Faaland 			if (dump_opt['u'] >= 2) {
31360caa7519SOlaf Faaland 				print_label_header(label, label_num);
31370caa7519SOlaf Faaland 				(void) printf("    Uberblock[%d] invalid\n", i);
31380caa7519SOlaf Faaland 			}
31390caa7519SOlaf Faaland 			continue;
31400caa7519SOlaf Faaland 		}
31410caa7519SOlaf Faaland 
31420caa7519SOlaf Faaland 		if ((dump_opt['u'] < 3) && (first_label(rec) != label_num))
31430caa7519SOlaf Faaland 			continue;
31440caa7519SOlaf Faaland 
31450caa7519SOlaf Faaland 		print_label_header(label, label_num);
31460caa7519SOlaf Faaland 		(void) snprintf(header, ZDB_MAX_UB_HEADER_SIZE,
31470caa7519SOlaf Faaland 		    "    Uberblock[%d]\n", i);
31480caa7519SOlaf Faaland 		dump_uberblock(ub, header, "");
31490caa7519SOlaf Faaland 		print_label_numbers("        labels = ", rec);
31500caa7519SOlaf Faaland 	}
31510caa7519SOlaf Faaland }
31520caa7519SOlaf Faaland 
31530caa7519SOlaf Faaland static int
31540caa7519SOlaf Faaland dump_label(const char *dev)
31550caa7519SOlaf Faaland {
315664723e36SYuri Pankov 	char path[MAXPATHLEN];
31570caa7519SOlaf Faaland 	zdb_label_t labels[VDEV_LABELS];
3158*f0a05239SGeorge Amanakis 	uint64_t psize, ashift, l2cache;
31590caa7519SOlaf Faaland 	struct stat64 statbuf;
31600caa7519SOlaf Faaland 	boolean_t config_found = B_FALSE;
31610caa7519SOlaf Faaland 	boolean_t error = B_FALSE;
3162*f0a05239SGeorge Amanakis 	boolean_t read_l2arc_header = B_FALSE;
31630caa7519SOlaf Faaland 	avl_tree_t config_tree;
31640caa7519SOlaf Faaland 	avl_tree_t uberblock_tree;
31650caa7519SOlaf Faaland 	void *node, *cookie;
31660caa7519SOlaf Faaland 	int fd;
31670caa7519SOlaf Faaland 
31680caa7519SOlaf Faaland 	bzero(labels, sizeof (labels));
316964723e36SYuri Pankov 
317064723e36SYuri Pankov 	(void) strlcpy(path, dev, sizeof (path));
317164723e36SYuri Pankov 	if (dev[0] == '/') {
317264723e36SYuri Pankov 		if (strncmp(dev, ZFS_DISK_ROOTD,
317364723e36SYuri Pankov 		    strlen(ZFS_DISK_ROOTD)) == 0) {
317464723e36SYuri Pankov 			(void) snprintf(path, sizeof (path), "%s%s",
317564723e36SYuri Pankov 			    ZFS_RDISK_ROOTD, dev + strlen(ZFS_DISK_ROOTD));
317664723e36SYuri Pankov 		}
317764723e36SYuri Pankov 	} else if (stat64(path, &statbuf) != 0) {
317864723e36SYuri Pankov 		char *s;
3179c6065d0fSGeorge Wilson 
318064723e36SYuri Pankov 		(void) snprintf(path, sizeof (path), "%s%s", ZFS_RDISK_ROOTD,
318164723e36SYuri Pankov 		    dev);
318222c8b958SYuri Pankov 		if (((s = strrchr(dev, 's')) == NULL &&
318322c8b958SYuri Pankov 		    (s = strchr(dev, 'p')) == NULL) ||
318422c8b958SYuri Pankov 		    !isdigit(*(s + 1)))
318564723e36SYuri Pankov 			(void) strlcat(path, "s0", sizeof (path));
3186fa9e4066Sahrens 	}
3187fa9e4066Sahrens 
318822c8b958SYuri Pankov 	if ((fd = open64(path, O_RDONLY)) < 0) {
318922c8b958SYuri Pankov 		(void) fprintf(stderr, "cannot open '%s': %s\n", path,
3190fa9e4066Sahrens 		    strerror(errno));
3191c6065d0fSGeorge Wilson 		exit(1);
3192c6065d0fSGeorge Wilson 	}
3193c6065d0fSGeorge Wilson 
319422c8b958SYuri Pankov 	if (fstat64(fd, &statbuf) != 0) {
319522c8b958SYuri Pankov 		(void) fprintf(stderr, "failed to stat '%s': %s\n", path,
319622c8b958SYuri Pankov 		    strerror(errno));
319722c8b958SYuri Pankov 		(void) close(fd);
319864723e36SYuri Pankov 		exit(1);
319964723e36SYuri Pankov 	}
320064723e36SYuri Pankov 
320122c8b958SYuri Pankov 	if (S_ISBLK(statbuf.st_mode)) {
320222c8b958SYuri Pankov 		(void) fprintf(stderr,
320322c8b958SYuri Pankov 		    "cannot use '%s': character device required\n", path);
320422c8b958SYuri Pankov 		(void) close(fd);
3205c6065d0fSGeorge Wilson 		exit(1);
3206fa9e4066Sahrens 	}
3207fa9e4066Sahrens 
32080caa7519SOlaf Faaland 	avl_create(&config_tree, cksum_record_compare,
32090caa7519SOlaf Faaland 	    sizeof (cksum_record_t), offsetof(cksum_record_t, link));
32100caa7519SOlaf Faaland 	avl_create(&uberblock_tree, cksum_record_compare,
32110caa7519SOlaf Faaland 	    sizeof (cksum_record_t), offsetof(cksum_record_t, link));
32120caa7519SOlaf Faaland 
3213fa9e4066Sahrens 	psize = statbuf.st_size;
3214fa9e4066Sahrens 	psize = P2ALIGN(psize, (uint64_t)sizeof (vdev_label_t));
32150caa7519SOlaf Faaland 	ashift = SPA_MINBLOCKSHIFT;
3216fa9e4066Sahrens 
32170caa7519SOlaf Faaland 	/*
32180caa7519SOlaf Faaland 	 * 1. Read the label from disk
32190caa7519SOlaf Faaland 	 * 2. Unpack the configuration and insert in config tree.
32200caa7519SOlaf Faaland 	 * 3. Traverse all uberblocks and insert in uberblock tree.
32210caa7519SOlaf Faaland 	 */
322253b9a4a9SVictor Latushkin 	for (int l = 0; l < VDEV_LABELS; l++) {
32230caa7519SOlaf Faaland 		zdb_label_t *label = &labels[l];
32240caa7519SOlaf Faaland 		char *buf = label->label.vl_vdev_phys.vp_nvlist;
32250caa7519SOlaf Faaland 		size_t buflen = sizeof (label->label.vl_vdev_phys.vp_nvlist);
32260caa7519SOlaf Faaland 		nvlist_t *config;
32270caa7519SOlaf Faaland 		cksum_record_t *rec;
32280caa7519SOlaf Faaland 		zio_cksum_t cksum;
32290caa7519SOlaf Faaland 		vdev_t vd;
32300caa7519SOlaf Faaland 
32310caa7519SOlaf Faaland 		if (pread64(fd, &label->label, sizeof (label->label),
32320caa7519SOlaf Faaland 		    vdev_label_offset(psize, l, 0)) != sizeof (label->label)) {
323364723e36SYuri Pankov 			if (!dump_opt['q'])
323464723e36SYuri Pankov 				(void) printf("failed to read label %d\n", l);
32350caa7519SOlaf Faaland 			label->read_failed = B_TRUE;
32360caa7519SOlaf Faaland 			error = B_TRUE;
3237fa9e4066Sahrens 			continue;
3238fa9e4066Sahrens 		}
3239fa9e4066Sahrens 
32400caa7519SOlaf Faaland 		label->read_failed = B_FALSE;
32410caa7519SOlaf Faaland 
32420caa7519SOlaf Faaland 		if (nvlist_unpack(buf, buflen, &config, 0) == 0) {
324353b9a4a9SVictor Latushkin 			nvlist_t *vdev_tree = NULL;
32440caa7519SOlaf Faaland 			size_t size;
324553b9a4a9SVictor Latushkin 
324653b9a4a9SVictor Latushkin 			if ((nvlist_lookup_nvlist(config,
324753b9a4a9SVictor Latushkin 			    ZPOOL_CONFIG_VDEV_TREE, &vdev_tree) != 0) ||
324853b9a4a9SVictor Latushkin 			    (nvlist_lookup_uint64(vdev_tree,
324953b9a4a9SVictor Latushkin 			    ZPOOL_CONFIG_ASHIFT, &ashift) != 0))
325053b9a4a9SVictor Latushkin 				ashift = SPA_MINBLOCKSHIFT;
32510caa7519SOlaf Faaland 
3252*f0a05239SGeorge Amanakis 			/* If the device is a cache device clear the header. */
3253*f0a05239SGeorge Amanakis 			if (!read_l2arc_header) {
3254*f0a05239SGeorge Amanakis 				if (nvlist_lookup_uint64(config,
3255*f0a05239SGeorge Amanakis 				    ZPOOL_CONFIG_POOL_STATE, &l2cache) == 0 &&
3256*f0a05239SGeorge Amanakis 				    l2cache == POOL_STATE_L2CACHE) {
3257*f0a05239SGeorge Amanakis 					read_l2arc_header = B_TRUE;
3258*f0a05239SGeorge Amanakis 				}
3259*f0a05239SGeorge Amanakis 			}
3260*f0a05239SGeorge Amanakis 
32610caa7519SOlaf Faaland 			if (nvlist_size(config, &size, NV_ENCODE_XDR) != 0)
32620caa7519SOlaf Faaland 				size = buflen;
32630caa7519SOlaf Faaland 
32640caa7519SOlaf Faaland 			fletcher_4_native(buf, size, NULL, &cksum);
32650caa7519SOlaf Faaland 			rec = cksum_record_insert(&config_tree, &cksum, l);
32660caa7519SOlaf Faaland 
32670caa7519SOlaf Faaland 			label->config = rec;
32680caa7519SOlaf Faaland 			label->config_nv = config;
32690caa7519SOlaf Faaland 			config_found = B_TRUE;
32700caa7519SOlaf Faaland 		} else {
32710caa7519SOlaf Faaland 			error = B_TRUE;
32720caa7519SOlaf Faaland 		}
32730caa7519SOlaf Faaland 
32740caa7519SOlaf Faaland 		vd.vdev_ashift = ashift;
32750caa7519SOlaf Faaland 		vd.vdev_top = &vd;
32760caa7519SOlaf Faaland 
32770caa7519SOlaf Faaland 		for (int i = 0; i < VDEV_UBERBLOCK_COUNT(&vd); i++) {
32780caa7519SOlaf Faaland 			uint64_t uoff = VDEV_UBERBLOCK_OFFSET(&vd, i);
32790caa7519SOlaf Faaland 			uberblock_t *ub = (void *)((char *)label + uoff);
32800caa7519SOlaf Faaland 
32810caa7519SOlaf Faaland 			if (uberblock_verify(ub))
32820caa7519SOlaf Faaland 				continue;
32830caa7519SOlaf Faaland 
32840caa7519SOlaf Faaland 			fletcher_4_native(ub, sizeof (*ub), NULL, &cksum);
32850caa7519SOlaf Faaland 			rec = cksum_record_insert(&uberblock_tree, &cksum, l);
32860caa7519SOlaf Faaland 
32870caa7519SOlaf Faaland 			label->uberblocks[i] = rec;
32880caa7519SOlaf Faaland 		}
32890caa7519SOlaf Faaland 	}
32900caa7519SOlaf Faaland 
32910caa7519SOlaf Faaland 	/*
32920caa7519SOlaf Faaland 	 * Dump the label and uberblocks.
32930caa7519SOlaf Faaland 	 */
32940caa7519SOlaf Faaland 	for (int l = 0; l < VDEV_LABELS; l++) {
32950caa7519SOlaf Faaland 		zdb_label_t *label = &labels[l];
32960caa7519SOlaf Faaland 		size_t buflen = sizeof (label->label.vl_vdev_phys.vp_nvlist);
32970caa7519SOlaf Faaland 
32980caa7519SOlaf Faaland 		if (label->read_failed == B_TRUE)
32990caa7519SOlaf Faaland 			continue;
33000caa7519SOlaf Faaland 
33010caa7519SOlaf Faaland 		if (label->config_nv) {
33020caa7519SOlaf Faaland 			dump_config_from_label(label, buflen, l);
33030caa7519SOlaf Faaland 		} else {
33040caa7519SOlaf Faaland 			if (!dump_opt['q'])
33050caa7519SOlaf Faaland 				(void) printf("failed to unpack label %d\n", l);
3306fa9e4066Sahrens 		}
3307*f0a05239SGeorge Amanakis 
330853b9a4a9SVictor Latushkin 		if (dump_opt['u'])
33090caa7519SOlaf Faaland 			dump_label_uberblocks(label, ashift, l);
33100caa7519SOlaf Faaland 
33110caa7519SOlaf Faaland 		nvlist_free(label->config_nv);
3312fa9e4066Sahrens 	}
3313c6065d0fSGeorge Wilson 
3314*f0a05239SGeorge Amanakis 	/*
3315*f0a05239SGeorge Amanakis 	 * Dump the L2ARC header, if existent.
3316*f0a05239SGeorge Amanakis 	 */
3317*f0a05239SGeorge Amanakis 	if (read_l2arc_header)
3318*f0a05239SGeorge Amanakis 		error |= dump_l2arc_header(fd);
3319*f0a05239SGeorge Amanakis 
33200caa7519SOlaf Faaland 	cookie = NULL;
33210caa7519SOlaf Faaland 	while ((node = avl_destroy_nodes(&config_tree, &cookie)) != NULL)
33220caa7519SOlaf Faaland 		umem_free(node, sizeof (cksum_record_t));
33230caa7519SOlaf Faaland 
33240caa7519SOlaf Faaland 	cookie = NULL;
33250caa7519SOlaf Faaland 	while ((node = avl_destroy_nodes(&uberblock_tree, &cookie)) != NULL)
33260caa7519SOlaf Faaland 		umem_free(node, sizeof (cksum_record_t));
33270caa7519SOlaf Faaland 
33280caa7519SOlaf Faaland 	avl_destroy(&config_tree);
33290caa7519SOlaf Faaland 	avl_destroy(&uberblock_tree);
33300caa7519SOlaf Faaland 
3331c6065d0fSGeorge Wilson 	(void) close(fd);
333264723e36SYuri Pankov 
33330caa7519SOlaf Faaland 	return (config_found == B_FALSE ? 2 :
33340caa7519SOlaf Faaland 	    (error == B_TRUE ? 1 : 0));
3335fa9e4066Sahrens }
3336fa9e4066Sahrens 
3337ca0cc391SMatthew Ahrens static uint64_t dataset_feature_count[SPA_FEATURES];
33385cabbc6bSPrashanth Sreenivasa static uint64_t remap_deadlist_count = 0;
3339b5152584SMatthew Ahrens 
33401d452cf5Sahrens static int
3341*f0a05239SGeorge Amanakis dump_one_dir(const char *dsname, void *arg __unused)
3342fa9e4066Sahrens {
3343fa9e4066Sahrens 	int error;
3344fa9e4066Sahrens 	objset_t *os;
3345fa9e4066Sahrens 
3346ed61ec1dSYuri Pankov 	error = open_objset(dsname, DMU_OST_ANY, FTAG, &os);
3347ed61ec1dSYuri Pankov 	if (error != 0)
33481d452cf5Sahrens 		return (0);
3349ca0cc391SMatthew Ahrens 
3350ca0cc391SMatthew Ahrens 	for (spa_feature_t f = 0; f < SPA_FEATURES; f++) {
3351ca0cc391SMatthew Ahrens 		if (!dmu_objset_ds(os)->ds_feature_inuse[f])
3352ca0cc391SMatthew Ahrens 			continue;
3353ca0cc391SMatthew Ahrens 		ASSERT(spa_feature_table[f].fi_flags &
3354ca0cc391SMatthew Ahrens 		    ZFEATURE_FLAG_PER_DATASET);
3355ca0cc391SMatthew Ahrens 		dataset_feature_count[f]++;
3356ca0cc391SMatthew Ahrens 	}
3357ca0cc391SMatthew Ahrens 
33585cabbc6bSPrashanth Sreenivasa 	if (dsl_dataset_remap_deadlist_exists(dmu_objset_ds(os))) {
33595cabbc6bSPrashanth Sreenivasa 		remap_deadlist_count++;
33605cabbc6bSPrashanth Sreenivasa 	}
33615cabbc6bSPrashanth Sreenivasa 
3362fa9e4066Sahrens 	dump_dir(os);
3363ed61ec1dSYuri Pankov 	close_objset(os, FTAG);
3364e0d35c44Smarks 	fuid_table_destroy();
33651d452cf5Sahrens 	return (0);
3366fa9e4066Sahrens }
3367fa9e4066Sahrens 
3368fa9e4066Sahrens /*
3369b24ab676SJeff Bonwick  * Block statistics.
3370fa9e4066Sahrens  */
3371b5152584SMatthew Ahrens #define	PSIZE_HISTO_SIZE (SPA_OLD_MAXBLOCKSIZE / SPA_MINBLOCKSIZE + 2)
3372fa9e4066Sahrens typedef struct zdb_blkstats {
3373490d05b9SMatthew Ahrens 	uint64_t zb_asize;
3374490d05b9SMatthew Ahrens 	uint64_t zb_lsize;
3375490d05b9SMatthew Ahrens 	uint64_t zb_psize;
3376490d05b9SMatthew Ahrens 	uint64_t zb_count;
3377d5ee8a13SMatthew Ahrens 	uint64_t zb_gangs;
3378d5ee8a13SMatthew Ahrens 	uint64_t zb_ditto_samevdev;
3379663207adSDon Brady 	uint64_t zb_ditto_same_ms;
3380490d05b9SMatthew Ahrens 	uint64_t zb_psize_histogram[PSIZE_HISTO_SIZE];
3381fa9e4066Sahrens } zdb_blkstats_t;
3382fa9e4066Sahrens 
3383b24ab676SJeff Bonwick /*
3384b24ab676SJeff Bonwick  * Extended object types to report deferred frees and dedup auto-ditto blocks.
3385b24ab676SJeff Bonwick  */
3386b24ab676SJeff Bonwick #define	ZDB_OT_DEFERRED	(DMU_OT_NUMTYPES + 0)
3387b24ab676SJeff Bonwick #define	ZDB_OT_DITTO	(DMU_OT_NUMTYPES + 1)
3388ad135b5dSChristopher Siden #define	ZDB_OT_OTHER	(DMU_OT_NUMTYPES + 2)
3389ad135b5dSChristopher Siden #define	ZDB_OT_TOTAL	(DMU_OT_NUMTYPES + 3)
3390b24ab676SJeff Bonwick 
33913f7978d0SAlan Somers static const char *zdb_ot_extname[] = {
3392b24ab676SJeff Bonwick 	"deferred free",
3393b24ab676SJeff Bonwick 	"dedup ditto",
3394ad135b5dSChristopher Siden 	"other",
3395b24ab676SJeff Bonwick 	"Total",
3396b24ab676SJeff Bonwick };
3397fa9e4066Sahrens 
339888b7b0f2SMatthew Ahrens #define	ZB_TOTAL	DN_MAX_LEVELS
3399fa9e4066Sahrens 
3400fa9e4066Sahrens typedef struct zdb_cb {
3401b24ab676SJeff Bonwick 	zdb_blkstats_t	zcb_type[ZB_TOTAL + 1][ZDB_OT_TOTAL + 1];
34025cabbc6bSPrashanth Sreenivasa 	uint64_t	zcb_removing_size;
340386714001SSerapheim Dimitropoulos 	uint64_t	zcb_checkpoint_size;
3404b24ab676SJeff Bonwick 	uint64_t	zcb_dedup_asize;
3405b24ab676SJeff Bonwick 	uint64_t	zcb_dedup_blocks;
34065d7b4d43SMatthew Ahrens 	uint64_t	zcb_embedded_blocks[NUM_BP_EMBEDDED_TYPES];
34075d7b4d43SMatthew Ahrens 	uint64_t	zcb_embedded_histogram[NUM_BP_EMBEDDED_TYPES]
34085d7b4d43SMatthew Ahrens 	    [BPE_PAYLOAD_SIZE];
3409490d05b9SMatthew Ahrens 	uint64_t	zcb_start;
34103f7978d0SAlan Somers 	hrtime_t	zcb_lastprint;
3411490d05b9SMatthew Ahrens 	uint64_t	zcb_totalasize;
3412fa9e4066Sahrens 	uint64_t	zcb_errors[256];
3413fa9e4066Sahrens 	int		zcb_readfails;
3414fa9e4066Sahrens 	int		zcb_haderrors;
3415cde58dbcSMatthew Ahrens 	spa_t		*zcb_spa;
34165cabbc6bSPrashanth Sreenivasa 	uint32_t	**zcb_vd_obsolete_counts;
3417fa9e4066Sahrens } zdb_cb_t;
3418fa9e4066Sahrens 
3419663207adSDon Brady /* test if two DVA offsets from same vdev are within the same metaslab */
3420663207adSDon Brady static boolean_t
3421663207adSDon Brady same_metaslab(spa_t *spa, uint64_t vdev, uint64_t off1, uint64_t off2)
3422663207adSDon Brady {
3423663207adSDon Brady 	vdev_t *vd = vdev_lookup_top(spa, vdev);
3424663207adSDon Brady 	uint64_t ms_shift = vd->vdev_ms_shift;
3425663207adSDon Brady 
3426663207adSDon Brady 	return ((off1 >> ms_shift) == (off2 >> ms_shift));
3427663207adSDon Brady }
3428663207adSDon Brady 
3429fa9e4066Sahrens static void
3430cde58dbcSMatthew Ahrens zdb_count_block(zdb_cb_t *zcb, zilog_t *zilog, const blkptr_t *bp,
3431b24ab676SJeff Bonwick     dmu_object_type_t type)
3432fa9e4066Sahrens {
3433b24ab676SJeff Bonwick 	uint64_t refcnt = 0;
3434b24ab676SJeff Bonwick 
3435b24ab676SJeff Bonwick 	ASSERT(type < ZDB_OT_TOTAL);
3436b24ab676SJeff Bonwick 
3437b24ab676SJeff Bonwick 	if (zilog && zil_bp_tree_add(zilog, bp) != 0)
3438b24ab676SJeff Bonwick 		return;
3439b24ab676SJeff Bonwick 
3440663207adSDon Brady 	spa_config_enter(zcb->zcb_spa, SCL_CONFIG, FTAG, RW_READER);
3441663207adSDon Brady 
3442e14bb325SJeff Bonwick 	for (int i = 0; i < 4; i++) {
3443fa9e4066Sahrens 		int l = (i < 2) ? BP_GET_LEVEL(bp) : ZB_TOTAL;
3444b24ab676SJeff Bonwick 		int t = (i & 1) ? type : ZDB_OT_TOTAL;
3445d5ee8a13SMatthew Ahrens 		int equal;
3446fa9e4066Sahrens 		zdb_blkstats_t *zb = &zcb->zcb_type[l][t];
3447fa9e4066Sahrens 
3448fa9e4066Sahrens 		zb->zb_asize += BP_GET_ASIZE(bp);
3449fa9e4066Sahrens 		zb->zb_lsize += BP_GET_LSIZE(bp);
3450fa9e4066Sahrens 		zb->zb_psize += BP_GET_PSIZE(bp);
3451fa9e4066Sahrens 		zb->zb_count++;
3452b5152584SMatthew Ahrens 
3453b5152584SMatthew Ahrens 		/*
3454b5152584SMatthew Ahrens 		 * The histogram is only big enough to record blocks up to
3455b5152584SMatthew Ahrens 		 * SPA_OLD_MAXBLOCKSIZE; larger blocks go into the last,
3456b5152584SMatthew Ahrens 		 * "other", bucket.
3457b5152584SMatthew Ahrens 		 */
34583f7978d0SAlan Somers 		unsigned idx = BP_GET_PSIZE(bp) >> SPA_MINBLOCKSHIFT;
3459b5152584SMatthew Ahrens 		idx = MIN(idx, SPA_OLD_MAXBLOCKSIZE / SPA_MINBLOCKSIZE + 1);
3460b5152584SMatthew Ahrens 		zb->zb_psize_histogram[idx]++;
3461d5ee8a13SMatthew Ahrens 
3462d5ee8a13SMatthew Ahrens 		zb->zb_gangs += BP_COUNT_GANG(bp);
3463d5ee8a13SMatthew Ahrens 
3464d5ee8a13SMatthew Ahrens 		switch (BP_GET_NDVAS(bp)) {
3465d5ee8a13SMatthew Ahrens 		case 2:
3466d5ee8a13SMatthew Ahrens 			if (DVA_GET_VDEV(&bp->blk_dva[0]) ==
3467663207adSDon Brady 			    DVA_GET_VDEV(&bp->blk_dva[1])) {
3468d5ee8a13SMatthew Ahrens 				zb->zb_ditto_samevdev++;
3469663207adSDon Brady 
3470663207adSDon Brady 				if (same_metaslab(zcb->zcb_spa,
3471663207adSDon Brady 				    DVA_GET_VDEV(&bp->blk_dva[0]),
3472663207adSDon Brady 				    DVA_GET_OFFSET(&bp->blk_dva[0]),
3473663207adSDon Brady 				    DVA_GET_OFFSET(&bp->blk_dva[1])))
3474663207adSDon Brady 					zb->zb_ditto_same_ms++;
3475663207adSDon Brady 			}
3476d5ee8a13SMatthew Ahrens 			break;
3477d5ee8a13SMatthew Ahrens 		case 3:
3478d5ee8a13SMatthew Ahrens 			equal = (DVA_GET_VDEV(&bp->blk_dva[0]) ==
3479d5ee8a13SMatthew Ahrens 			    DVA_GET_VDEV(&bp->blk_dva[1])) +
3480d5ee8a13SMatthew Ahrens 			    (DVA_GET_VDEV(&bp->blk_dva[0]) ==
3481d5ee8a13SMatthew Ahrens 			    DVA_GET_VDEV(&bp->blk_dva[2])) +
3482d5ee8a13SMatthew Ahrens 			    (DVA_GET_VDEV(&bp->blk_dva[1]) ==
3483d5ee8a13SMatthew Ahrens 			    DVA_GET_VDEV(&bp->blk_dva[2]));
3484663207adSDon Brady 			if (equal != 0) {
3485d5ee8a13SMatthew Ahrens 				zb->zb_ditto_samevdev++;
3486663207adSDon Brady 
3487663207adSDon Brady 				if (DVA_GET_VDEV(&bp->blk_dva[0]) ==
3488663207adSDon Brady 				    DVA_GET_VDEV(&bp->blk_dva[1]) &&
3489663207adSDon Brady 				    same_metaslab(zcb->zcb_spa,
3490663207adSDon Brady 				    DVA_GET_VDEV(&bp->blk_dva[0]),
3491663207adSDon Brady 				    DVA_GET_OFFSET(&bp->blk_dva[0]),
3492663207adSDon Brady 				    DVA_GET_OFFSET(&bp->blk_dva[1])))
3493663207adSDon Brady 					zb->zb_ditto_same_ms++;
3494663207adSDon Brady 				else if (DVA_GET_VDEV(&bp->blk_dva[0]) ==
3495663207adSDon Brady 				    DVA_GET_VDEV(&bp->blk_dva[2]) &&
3496663207adSDon Brady 				    same_metaslab(zcb->zcb_spa,
3497663207adSDon Brady 				    DVA_GET_VDEV(&bp->blk_dva[0]),
3498663207adSDon Brady 				    DVA_GET_OFFSET(&bp->blk_dva[0]),
3499663207adSDon Brady 				    DVA_GET_OFFSET(&bp->blk_dva[2])))
3500663207adSDon Brady 					zb->zb_ditto_same_ms++;
3501663207adSDon Brady 				else if (DVA_GET_VDEV(&bp->blk_dva[1]) ==
3502663207adSDon Brady 				    DVA_GET_VDEV(&bp->blk_dva[2]) &&
3503663207adSDon Brady 				    same_metaslab(zcb->zcb_spa,
3504663207adSDon Brady 				    DVA_GET_VDEV(&bp->blk_dva[1]),
3505663207adSDon Brady 				    DVA_GET_OFFSET(&bp->blk_dva[1]),
3506663207adSDon Brady 				    DVA_GET_OFFSET(&bp->blk_dva[2])))
3507663207adSDon Brady 					zb->zb_ditto_same_ms++;
3508663207adSDon Brady 			}
3509d5ee8a13SMatthew Ahrens 			break;
3510d5ee8a13SMatthew Ahrens 		}
3511fa9e4066Sahrens 	}
3512fa9e4066Sahrens 
3513663207adSDon Brady 	spa_config_exit(zcb->zcb_spa, SCL_CONFIG, FTAG);
3514663207adSDon Brady 
35155d7b4d43SMatthew Ahrens 	if (BP_IS_EMBEDDED(bp)) {
35165d7b4d43SMatthew Ahrens 		zcb->zcb_embedded_blocks[BPE_GET_ETYPE(bp)]++;
35175d7b4d43SMatthew Ahrens 		zcb->zcb_embedded_histogram[BPE_GET_ETYPE(bp)]
35185d7b4d43SMatthew Ahrens 		    [BPE_GET_PSIZE(bp)]++;
35195d7b4d43SMatthew Ahrens 		return;
35205d7b4d43SMatthew Ahrens 	}
35215d7b4d43SMatthew Ahrens 
3522b24ab676SJeff Bonwick 	if (dump_opt['L'])
3523b24ab676SJeff Bonwick 		return;
3524b24ab676SJeff Bonwick 
3525b24ab676SJeff Bonwick 	if (BP_GET_DEDUP(bp)) {
3526b24ab676SJeff Bonwick 		ddt_t *ddt;
3527b24ab676SJeff Bonwick 		ddt_entry_t *dde;
3528b24ab676SJeff Bonwick 
3529cde58dbcSMatthew Ahrens 		ddt = ddt_select(zcb->zcb_spa, bp);
3530b24ab676SJeff Bonwick 		ddt_enter(ddt);
3531b24ab676SJeff Bonwick 		dde = ddt_lookup(ddt, bp, B_FALSE);
3532b24ab676SJeff Bonwick 
3533b24ab676SJeff Bonwick 		if (dde == NULL) {
3534b24ab676SJeff Bonwick 			refcnt = 0;
3535b24ab676SJeff Bonwick 		} else {
3536b24ab676SJeff Bonwick 			ddt_phys_t *ddp = ddt_phys_select(dde, bp);
3537b24ab676SJeff Bonwick 			ddt_phys_decref(ddp);
3538b24ab676SJeff Bonwick 			refcnt = ddp->ddp_refcnt;
3539b24ab676SJeff Bonwick 			if (ddt_phys_total_refcnt(dde) == 0)
3540b24ab676SJeff Bonwick 				ddt_remove(ddt, dde);
3541d41e7643Sek 		}
3542b24ab676SJeff Bonwick 		ddt_exit(ddt);
3543d41e7643Sek 	}
3544d41e7643Sek 
3545cde58dbcSMatthew Ahrens 	VERIFY3U(zio_wait(zio_claim(NULL, zcb->zcb_spa,
354686714001SSerapheim Dimitropoulos 	    refcnt ? 0 : spa_min_claim_txg(zcb->zcb_spa),
3547b24ab676SJeff Bonwick 	    bp, NULL, NULL, ZIO_FLAG_CANFAIL)), ==, 0);
3548fa9e4066Sahrens }
3549fa9e4066Sahrens 
355031d7e8faSGeorge Wilson static void
355131d7e8faSGeorge Wilson zdb_blkptr_done(zio_t *zio)
355231d7e8faSGeorge Wilson {
355331d7e8faSGeorge Wilson 	spa_t *spa = zio->io_spa;
355431d7e8faSGeorge Wilson 	blkptr_t *bp = zio->io_bp;
355531d7e8faSGeorge Wilson 	int ioerr = zio->io_error;
355631d7e8faSGeorge Wilson 	zdb_cb_t *zcb = zio->io_private;
35577802d7bfSMatthew Ahrens 	zbookmark_phys_t *zb = &zio->io_bookmark;
355831d7e8faSGeorge Wilson 
3559770499e1SDan Kimmel 	abd_free(zio->io_abd);
356031d7e8faSGeorge Wilson 
356131d7e8faSGeorge Wilson 	mutex_enter(&spa->spa_scrub_lock);
3562a3874b8bSToomas Soome 	spa->spa_load_verify_ios--;
356331d7e8faSGeorge Wilson 	cv_broadcast(&spa->spa_scrub_io_cv);
356431d7e8faSGeorge Wilson 
356531d7e8faSGeorge Wilson 	if (ioerr && !(zio->io_flags & ZIO_FLAG_SPECULATIVE)) {
356631d7e8faSGeorge Wilson 		char blkbuf[BP_SPRINTF_LEN];
356731d7e8faSGeorge Wilson 
356831d7e8faSGeorge Wilson 		zcb->zcb_haderrors = 1;
356931d7e8faSGeorge Wilson 		zcb->zcb_errors[ioerr]++;
357031d7e8faSGeorge Wilson 
357131d7e8faSGeorge Wilson 		if (dump_opt['b'] >= 2)
357243466aaeSMax Grossman 			snprintf_blkptr(blkbuf, sizeof (blkbuf), bp);
357331d7e8faSGeorge Wilson 		else
357431d7e8faSGeorge Wilson 			blkbuf[0] = '\0';
357531d7e8faSGeorge Wilson 
357631d7e8faSGeorge Wilson 		(void) printf("zdb_blkptr_cb: "
357731d7e8faSGeorge Wilson 		    "Got error %d reading "
357831d7e8faSGeorge Wilson 		    "<%llu, %llu, %lld, %llx> %s -- skipping\n",
357931d7e8faSGeorge Wilson 		    ioerr,
358031d7e8faSGeorge Wilson 		    (u_longlong_t)zb->zb_objset,
358131d7e8faSGeorge Wilson 		    (u_longlong_t)zb->zb_object,
358231d7e8faSGeorge Wilson 		    (u_longlong_t)zb->zb_level,
358331d7e8faSGeorge Wilson 		    (u_longlong_t)zb->zb_blkid,
358431d7e8faSGeorge Wilson 		    blkbuf);
358531d7e8faSGeorge Wilson 	}
358631d7e8faSGeorge Wilson 	mutex_exit(&spa->spa_scrub_lock);
358731d7e8faSGeorge Wilson }
358831d7e8faSGeorge Wilson 
3589fa9e4066Sahrens static int
35901b912ec7SGeorge Wilson zdb_blkptr_cb(spa_t *spa, zilog_t *zilog, const blkptr_t *bp,
35917802d7bfSMatthew Ahrens     const zbookmark_phys_t *zb, const dnode_phys_t *dnp, void *arg)
3592fa9e4066Sahrens {
3593fa9e4066Sahrens 	zdb_cb_t *zcb = arg;
35946365109dSVictor Latushkin 	dmu_object_type_t type;
3595468c413aSTim Haley 	boolean_t is_metadata;
3596fa9e4066Sahrens 
3597a2cdcdd2SPaul Dagnelie 	if (bp == NULL)
3598a2cdcdd2SPaul Dagnelie 		return (0);
3599a2cdcdd2SPaul Dagnelie 
360043466aaeSMax Grossman 	if (dump_opt['b'] >= 5 && bp->blk_birth > 0) {
360143466aaeSMax Grossman 		char blkbuf[BP_SPRINTF_LEN];
360243466aaeSMax Grossman 		snprintf_blkptr(blkbuf, sizeof (blkbuf), bp);
360343466aaeSMax Grossman 		(void) printf("objset %llu object %llu "
360443466aaeSMax Grossman 		    "level %lld offset 0x%llx %s\n",
360543466aaeSMax Grossman 		    (u_longlong_t)zb->zb_objset,
360643466aaeSMax Grossman 		    (u_longlong_t)zb->zb_object,
360743466aaeSMax Grossman 		    (longlong_t)zb->zb_level,
360843466aaeSMax Grossman 		    (u_longlong_t)blkid2offset(dnp, bp, zb),
360943466aaeSMax Grossman 		    blkbuf);
361043466aaeSMax Grossman 	}
361143466aaeSMax Grossman 
361243466aaeSMax Grossman 	if (BP_IS_HOLE(bp))
361388b7b0f2SMatthew Ahrens 		return (0);
3614e14bb325SJeff Bonwick 
36156365109dSVictor Latushkin 	type = BP_GET_TYPE(bp);
3616e14bb325SJeff Bonwick 
3617ad135b5dSChristopher Siden 	zdb_count_block(zcb, zilog, bp,
3618ad135b5dSChristopher Siden 	    (type & DMU_OT_NEWTYPE) ? ZDB_OT_OTHER : type);
36196365109dSVictor Latushkin 
3620ad135b5dSChristopher Siden 	is_metadata = (BP_GET_LEVEL(bp) != 0 || DMU_OT_IS_METADATA(type));
36216365109dSVictor Latushkin 
36225d7b4d43SMatthew Ahrens 	if (!BP_IS_EMBEDDED(bp) &&
36235d7b4d43SMatthew Ahrens 	    (dump_opt['c'] > 1 || (dump_opt['c'] && is_metadata))) {
3624468c413aSTim Haley 		size_t size = BP_GET_PSIZE(bp);
3625770499e1SDan Kimmel 		abd_t *abd = abd_alloc(size, B_FALSE);
3626b24ab676SJeff Bonwick 		int flags = ZIO_FLAG_CANFAIL | ZIO_FLAG_SCRUB | ZIO_FLAG_RAW;
3627b24ab676SJeff Bonwick 
3628b24ab676SJeff Bonwick 		/* If it's an intent log block, failure is expected. */
3629b24ab676SJeff Bonwick 		if (zb->zb_level == ZB_ZIL_LEVEL)
3630b24ab676SJeff Bonwick 			flags |= ZIO_FLAG_SPECULATIVE;
3631b24ab676SJeff Bonwick 
363231d7e8faSGeorge Wilson 		mutex_enter(&spa->spa_scrub_lock);
3633a3874b8bSToomas Soome 		while (spa->spa_load_verify_ios > max_inflight)
363431d7e8faSGeorge Wilson 			cv_wait(&spa->spa_scrub_io_cv, &spa->spa_scrub_lock);
3635a3874b8bSToomas Soome 		spa->spa_load_verify_ios++;
363631d7e8faSGeorge Wilson 		mutex_exit(&spa->spa_scrub_lock);
3637b24ab676SJeff Bonwick 
3638770499e1SDan Kimmel 		zio_nowait(zio_read(NULL, spa, bp, abd, size,
363931d7e8faSGeorge Wilson 		    zdb_blkptr_done, zcb, ZIO_PRIORITY_ASYNC_READ, flags, zb));
3640fa9e4066Sahrens 	}
3641fa9e4066Sahrens 
3642fa9e4066Sahrens 	zcb->zcb_readfails = 0;
3643fa9e4066Sahrens 
3644f7950bf1SMatthew Ahrens 	/* only call gethrtime() every 100 blocks */
3645f7950bf1SMatthew Ahrens 	static int iters;
3646f7950bf1SMatthew Ahrens 	if (++iters > 100)
3647f7950bf1SMatthew Ahrens 		iters = 0;
3648f7950bf1SMatthew Ahrens 	else
3649f7950bf1SMatthew Ahrens 		return (0);
3650f7950bf1SMatthew Ahrens 
3651f7950bf1SMatthew Ahrens 	if (dump_opt['b'] < 5 && gethrtime() > zcb->zcb_lastprint + NANOSEC) {
3652490d05b9SMatthew Ahrens 		uint64_t now = gethrtime();
3653490d05b9SMatthew Ahrens 		char buf[10];
3654490d05b9SMatthew Ahrens 		uint64_t bytes = zcb->zcb_type[ZB_TOTAL][ZDB_OT_TOTAL].zb_asize;
3655490d05b9SMatthew Ahrens 		int kb_per_sec =
3656490d05b9SMatthew Ahrens 		    1 + bytes / (1 + ((now - zcb->zcb_start) / 1000 / 1000));
3657490d05b9SMatthew Ahrens 		int sec_remaining =
3658490d05b9SMatthew Ahrens 		    (zcb->zcb_totalasize - bytes) / 1024 / kb_per_sec;
3659490d05b9SMatthew Ahrens 
36600a055120SJason King 		/* make sure nicenum has enough space */
36610a055120SJason King 		CTASSERT(sizeof (buf) >= NN_NUMBUF_SZ);
36620a055120SJason King 
3663490d05b9SMatthew Ahrens 		zfs_nicenum(bytes, buf, sizeof (buf));
3664490d05b9SMatthew Ahrens 		(void) fprintf(stderr,
3665490d05b9SMatthew Ahrens 		    "\r%5s completed (%4dMB/s) "
3666490d05b9SMatthew Ahrens 		    "estimated time remaining: %uhr %02umin %02usec        ",
3667490d05b9SMatthew Ahrens 		    buf, kb_per_sec / 1024,
3668490d05b9SMatthew Ahrens 		    sec_remaining / 60 / 60,
3669490d05b9SMatthew Ahrens 		    sec_remaining / 60 % 60,
3670490d05b9SMatthew Ahrens 		    sec_remaining % 60);
3671490d05b9SMatthew Ahrens 
3672490d05b9SMatthew Ahrens 		zcb->zcb_lastprint = now;
3673490d05b9SMatthew Ahrens 	}
3674490d05b9SMatthew Ahrens 
3675fa9e4066Sahrens 	return (0);
3676fa9e4066Sahrens }
3677fa9e4066Sahrens 
3678b24ab676SJeff Bonwick static void
36790713e232SGeorge Wilson zdb_leak(void *arg, uint64_t start, uint64_t size)
3680b24ab676SJeff Bonwick {
36810713e232SGeorge Wilson 	vdev_t *vd = arg;
3682b24ab676SJeff Bonwick 
3683b24ab676SJeff Bonwick 	(void) printf("leaked space: vdev %llu, offset 0x%llx, size %llu\n",
3684b24ab676SJeff Bonwick 	    (u_longlong_t)vd->vdev_id, (u_longlong_t)start, (u_longlong_t)size);
3685b24ab676SJeff Bonwick }
3686b24ab676SJeff Bonwick 
36870713e232SGeorge Wilson static metaslab_ops_t zdb_metaslab_ops = {
36882e4c9986SGeorge Wilson 	NULL	/* alloc */
3689b24ab676SJeff Bonwick };
3690b24ab676SJeff Bonwick 
3691814dcd43SSerapheim Dimitropoulos typedef int (*zdb_log_sm_cb_t)(spa_t *spa, space_map_entry_t *sme,
3692814dcd43SSerapheim Dimitropoulos     uint64_t txg, void *arg);
3693814dcd43SSerapheim Dimitropoulos 
3694814dcd43SSerapheim Dimitropoulos typedef struct unflushed_iter_cb_arg {
3695814dcd43SSerapheim Dimitropoulos 	spa_t *uic_spa;
3696814dcd43SSerapheim Dimitropoulos 	uint64_t uic_txg;
3697814dcd43SSerapheim Dimitropoulos 	void *uic_arg;
3698814dcd43SSerapheim Dimitropoulos 	zdb_log_sm_cb_t uic_cb;
3699814dcd43SSerapheim Dimitropoulos } unflushed_iter_cb_arg_t;
3700814dcd43SSerapheim Dimitropoulos 
3701814dcd43SSerapheim Dimitropoulos static int
3702814dcd43SSerapheim Dimitropoulos iterate_through_spacemap_logs_cb(space_map_entry_t *sme, void *arg)
3703814dcd43SSerapheim Dimitropoulos {
3704814dcd43SSerapheim Dimitropoulos 	unflushed_iter_cb_arg_t *uic = arg;
3705814dcd43SSerapheim Dimitropoulos 
3706814dcd43SSerapheim Dimitropoulos 	return (uic->uic_cb(uic->uic_spa, sme, uic->uic_txg, uic->uic_arg));
3707814dcd43SSerapheim Dimitropoulos }
3708814dcd43SSerapheim Dimitropoulos 
3709814dcd43SSerapheim Dimitropoulos static void
3710814dcd43SSerapheim Dimitropoulos iterate_through_spacemap_logs(spa_t *spa, zdb_log_sm_cb_t cb, void *arg)
3711814dcd43SSerapheim Dimitropoulos {
3712814dcd43SSerapheim Dimitropoulos 	if (!spa_feature_is_active(spa, SPA_FEATURE_LOG_SPACEMAP))
3713814dcd43SSerapheim Dimitropoulos 		return;
3714814dcd43SSerapheim Dimitropoulos 
3715814dcd43SSerapheim Dimitropoulos 	spa_config_enter(spa, SCL_CONFIG, FTAG, RW_READER);
3716814dcd43SSerapheim Dimitropoulos 	for (spa_log_sm_t *sls = avl_first(&spa->spa_sm_logs_by_txg);
3717814dcd43SSerapheim Dimitropoulos 	    sls; sls = AVL_NEXT(&spa->spa_sm_logs_by_txg, sls)) {
3718814dcd43SSerapheim Dimitropoulos 		space_map_t *sm = NULL;
3719814dcd43SSerapheim Dimitropoulos 		VERIFY0(space_map_open(&sm, spa_meta_objset(spa),
3720814dcd43SSerapheim Dimitropoulos 		    sls->sls_sm_obj, 0, UINT64_MAX, SPA_MINBLOCKSHIFT));
3721814dcd43SSerapheim Dimitropoulos 
3722814dcd43SSerapheim Dimitropoulos 		unflushed_iter_cb_arg_t uic = {
3723814dcd43SSerapheim Dimitropoulos 			.uic_spa = spa,
3724814dcd43SSerapheim Dimitropoulos 			.uic_txg = sls->sls_txg,
3725814dcd43SSerapheim Dimitropoulos 			.uic_arg = arg,
3726814dcd43SSerapheim Dimitropoulos 			.uic_cb = cb
3727814dcd43SSerapheim Dimitropoulos 		};
3728814dcd43SSerapheim Dimitropoulos 
3729814dcd43SSerapheim Dimitropoulos 		VERIFY0(space_map_iterate(sm, space_map_length(sm),
3730814dcd43SSerapheim Dimitropoulos 		    iterate_through_spacemap_logs_cb, &uic));
3731814dcd43SSerapheim Dimitropoulos 		space_map_close(sm);
3732814dcd43SSerapheim Dimitropoulos 	}
3733814dcd43SSerapheim Dimitropoulos 	spa_config_exit(spa, SCL_CONFIG, FTAG);
3734814dcd43SSerapheim Dimitropoulos }
3735814dcd43SSerapheim Dimitropoulos 
3736814dcd43SSerapheim Dimitropoulos /* ARGSUSED */
3737814dcd43SSerapheim Dimitropoulos static int
3738814dcd43SSerapheim Dimitropoulos load_unflushed_svr_segs_cb(spa_t *spa, space_map_entry_t *sme,
3739814dcd43SSerapheim Dimitropoulos     uint64_t txg, void *arg)
3740814dcd43SSerapheim Dimitropoulos {
3741814dcd43SSerapheim Dimitropoulos 	spa_vdev_removal_t *svr = arg;
3742814dcd43SSerapheim Dimitropoulos 
3743814dcd43SSerapheim Dimitropoulos 	uint64_t offset = sme->sme_offset;
3744814dcd43SSerapheim Dimitropoulos 	uint64_t size = sme->sme_run;
3745814dcd43SSerapheim Dimitropoulos 
3746814dcd43SSerapheim Dimitropoulos 	/* skip vdevs we don't care about */
3747814dcd43SSerapheim Dimitropoulos 	if (sme->sme_vdev != svr->svr_vdev_id)
3748814dcd43SSerapheim Dimitropoulos 		return (0);
3749814dcd43SSerapheim Dimitropoulos 
3750814dcd43SSerapheim Dimitropoulos 	vdev_t *vd = vdev_lookup_top(spa, sme->sme_vdev);
3751814dcd43SSerapheim Dimitropoulos 	metaslab_t *ms = vd->vdev_ms[offset >> vd->vdev_ms_shift];
3752814dcd43SSerapheim Dimitropoulos 	ASSERT(sme->sme_type == SM_ALLOC || sme->sme_type == SM_FREE);
3753814dcd43SSerapheim Dimitropoulos 
3754814dcd43SSerapheim Dimitropoulos 	if (txg < metaslab_unflushed_txg(ms))
3755814dcd43SSerapheim Dimitropoulos 		return (0);
3756814dcd43SSerapheim Dimitropoulos 
3757814dcd43SSerapheim Dimitropoulos 	vdev_indirect_mapping_t *vim = vd->vdev_indirect_mapping;
3758814dcd43SSerapheim Dimitropoulos 	ASSERT(vim != NULL);
3759814dcd43SSerapheim Dimitropoulos 	if (offset >= vdev_indirect_mapping_max_offset(vim))
3760814dcd43SSerapheim Dimitropoulos 		return (0);
3761814dcd43SSerapheim Dimitropoulos 
3762814dcd43SSerapheim Dimitropoulos 	if (sme->sme_type == SM_ALLOC)
3763814dcd43SSerapheim Dimitropoulos 		range_tree_add(svr->svr_allocd_segs, offset, size);
3764814dcd43SSerapheim Dimitropoulos 	else
3765814dcd43SSerapheim Dimitropoulos 		range_tree_remove(svr->svr_allocd_segs, offset, size);
3766814dcd43SSerapheim Dimitropoulos 
3767814dcd43SSerapheim Dimitropoulos 	return (0);
3768814dcd43SSerapheim Dimitropoulos }
3769814dcd43SSerapheim Dimitropoulos 
3770b24ab676SJeff Bonwick static void
3771bbfd46c4SJeff Bonwick zdb_ddt_leak_init(spa_t *spa, zdb_cb_t *zcb)
3772b24ab676SJeff Bonwick {
37733f7978d0SAlan Somers 	ddt_bookmark_t ddb;
3774b24ab676SJeff Bonwick 	ddt_entry_t dde;
3775b24ab676SJeff Bonwick 	int error;
3776b24ab676SJeff Bonwick 
3777555d674dSSerapheim Dimitropoulos 	ASSERT(!dump_opt['L']);
3778555d674dSSerapheim Dimitropoulos 
37793f7978d0SAlan Somers 	bzero(&ddb, sizeof (ddb));
3780bbfd46c4SJeff Bonwick 	while ((error = ddt_walk(spa, &ddb, &dde)) == 0) {
3781b24ab676SJeff Bonwick 		blkptr_t blk;
3782b24ab676SJeff Bonwick 		ddt_phys_t *ddp = dde.dde_phys;
3783bbfd46c4SJeff Bonwick 
3784bbfd46c4SJeff Bonwick 		if (ddb.ddb_class == DDT_CLASS_UNIQUE)
3785bbfd46c4SJeff Bonwick 			return;
3786bbfd46c4SJeff Bonwick 
3787b24ab676SJeff Bonwick 		ASSERT(ddt_phys_total_refcnt(&dde) > 1);
3788bbfd46c4SJeff Bonwick 
3789b24ab676SJeff Bonwick 		for (int p = 0; p < DDT_PHYS_TYPES; p++, ddp++) {
3790b24ab676SJeff Bonwick 			if (ddp->ddp_phys_birth == 0)
3791b24ab676SJeff Bonwick 				continue;
3792bbfd46c4SJeff Bonwick 			ddt_bp_create(ddb.ddb_checksum,
3793bbfd46c4SJeff Bonwick 			    &dde.dde_key, ddp, &blk);
3794b24ab676SJeff Bonwick 			if (p == DDT_PHYS_DITTO) {
3795cde58dbcSMatthew Ahrens 				zdb_count_block(zcb, NULL, &blk, ZDB_OT_DITTO);
3796b24ab676SJeff Bonwick 			} else {
3797b24ab676SJeff Bonwick 				zcb->zcb_dedup_asize +=
3798b24ab676SJeff Bonwick 				    BP_GET_ASIZE(&blk) * (ddp->ddp_refcnt - 1);
3799b24ab676SJeff Bonwick 				zcb->zcb_dedup_blocks++;
3800b24ab676SJeff Bonwick 			}
3801b24ab676SJeff Bonwick 		}
3802555d674dSSerapheim Dimitropoulos 		ddt_t *ddt = spa->spa_ddt[ddb.ddb_checksum];
3803555d674dSSerapheim Dimitropoulos 		ddt_enter(ddt);
3804555d674dSSerapheim Dimitropoulos 		VERIFY(ddt_lookup(ddt, &blk, B_TRUE) != NULL);
3805555d674dSSerapheim Dimitropoulos 		ddt_exit(ddt);
3806b24ab676SJeff Bonwick 	}
3807b24ab676SJeff Bonwick 
3808b24ab676SJeff Bonwick 	ASSERT(error == ENOENT);
3809b24ab676SJeff Bonwick }
3810b24ab676SJeff Bonwick 
38115cabbc6bSPrashanth Sreenivasa /* ARGSUSED */
38125cabbc6bSPrashanth Sreenivasa static void
38135cabbc6bSPrashanth Sreenivasa claim_segment_impl_cb(uint64_t inner_offset, vdev_t *vd, uint64_t offset,
38145cabbc6bSPrashanth Sreenivasa     uint64_t size, void *arg)
38155cabbc6bSPrashanth Sreenivasa {
38165cabbc6bSPrashanth Sreenivasa 	/*
38175cabbc6bSPrashanth Sreenivasa 	 * This callback was called through a remap from
38185cabbc6bSPrashanth Sreenivasa 	 * a device being removed. Therefore, the vdev that
38195cabbc6bSPrashanth Sreenivasa 	 * this callback is applied to is a concrete
38205cabbc6bSPrashanth Sreenivasa 	 * vdev.
38215cabbc6bSPrashanth Sreenivasa 	 */
38225cabbc6bSPrashanth Sreenivasa 	ASSERT(vdev_is_concrete(vd));
38235cabbc6bSPrashanth Sreenivasa 
38245cabbc6bSPrashanth Sreenivasa 	VERIFY0(metaslab_claim_impl(vd, offset, size,
382586714001SSerapheim Dimitropoulos 	    spa_min_claim_txg(vd->vdev_spa)));
38265cabbc6bSPrashanth Sreenivasa }
38275cabbc6bSPrashanth Sreenivasa 
38285cabbc6bSPrashanth Sreenivasa static void
38295cabbc6bSPrashanth Sreenivasa claim_segment_cb(void *arg, uint64_t offset, uint64_t size)
38305cabbc6bSPrashanth Sreenivasa {
38315cabbc6bSPrashanth Sreenivasa 	vdev_t *vd = arg;
38325cabbc6bSPrashanth Sreenivasa 
38335cabbc6bSPrashanth Sreenivasa 	vdev_indirect_ops.vdev_op_remap(vd, offset, size,
38345cabbc6bSPrashanth Sreenivasa 	    claim_segment_impl_cb, NULL);
38355cabbc6bSPrashanth Sreenivasa }
38365cabbc6bSPrashanth Sreenivasa 
38375cabbc6bSPrashanth Sreenivasa /*
38385cabbc6bSPrashanth Sreenivasa  * After accounting for all allocated blocks that are directly referenced,
38395cabbc6bSPrashanth Sreenivasa  * we might have missed a reference to a block from a partially complete
38405cabbc6bSPrashanth Sreenivasa  * (and thus unused) indirect mapping object. We perform a secondary pass
38415cabbc6bSPrashanth Sreenivasa  * through the metaslabs we have already mapped and claim the destination
38425cabbc6bSPrashanth Sreenivasa  * blocks.
38435cabbc6bSPrashanth Sreenivasa  */
38445cabbc6bSPrashanth Sreenivasa static void
38455cabbc6bSPrashanth Sreenivasa zdb_claim_removing(spa_t *spa, zdb_cb_t *zcb)
38465cabbc6bSPrashanth Sreenivasa {
3847555d674dSSerapheim Dimitropoulos 	if (dump_opt['L'])
3848555d674dSSerapheim Dimitropoulos 		return;
3849555d674dSSerapheim Dimitropoulos 
38505cabbc6bSPrashanth Sreenivasa 	if (spa->spa_vdev_removal == NULL)
38515cabbc6bSPrashanth Sreenivasa 		return;
38525cabbc6bSPrashanth Sreenivasa 
38535cabbc6bSPrashanth Sreenivasa 	spa_config_enter(spa, SCL_CONFIG, FTAG, RW_READER);
38545cabbc6bSPrashanth Sreenivasa 
38555cabbc6bSPrashanth Sreenivasa 	spa_vdev_removal_t *svr = spa->spa_vdev_removal;
38563a4b1be9SMatthew Ahrens 	vdev_t *vd = vdev_lookup_top(spa, svr->svr_vdev_id);
38575cabbc6bSPrashanth Sreenivasa 	vdev_indirect_mapping_t *vim = vd->vdev_indirect_mapping;
38585cabbc6bSPrashanth Sreenivasa 
3859814dcd43SSerapheim Dimitropoulos 	ASSERT0(range_tree_space(svr->svr_allocd_segs));
3860814dcd43SSerapheim Dimitropoulos 
38614d7988d6SPaul Dagnelie 	range_tree_t *allocs = range_tree_create(NULL, RANGE_SEG64, NULL, 0, 0);
38625cabbc6bSPrashanth Sreenivasa 	for (uint64_t msi = 0; msi < vd->vdev_ms_count; msi++) {
38635cabbc6bSPrashanth Sreenivasa 		metaslab_t *msp = vd->vdev_ms[msi];
38645cabbc6bSPrashanth Sreenivasa 
38655cabbc6bSPrashanth Sreenivasa 		if (msp->ms_start >= vdev_indirect_mapping_max_offset(vim))
38665cabbc6bSPrashanth Sreenivasa 			break;
38675cabbc6bSPrashanth Sreenivasa 
3868814dcd43SSerapheim Dimitropoulos 		ASSERT0(range_tree_space(allocs));
3869814dcd43SSerapheim Dimitropoulos 		if (msp->ms_sm != NULL)
3870814dcd43SSerapheim Dimitropoulos 			VERIFY0(space_map_load(msp->ms_sm, allocs, SM_ALLOC));
3871814dcd43SSerapheim Dimitropoulos 		range_tree_vacate(allocs, range_tree_add, svr->svr_allocd_segs);
3872814dcd43SSerapheim Dimitropoulos 	}
3873814dcd43SSerapheim Dimitropoulos 	range_tree_destroy(allocs);
38745cabbc6bSPrashanth Sreenivasa 
3875814dcd43SSerapheim Dimitropoulos 	iterate_through_spacemap_logs(spa, load_unflushed_svr_segs_cb, svr);
38765cabbc6bSPrashanth Sreenivasa 
3877814dcd43SSerapheim Dimitropoulos 	/*
3878814dcd43SSerapheim Dimitropoulos 	 * Clear everything past what has been synced,
3879814dcd43SSerapheim Dimitropoulos 	 * because we have not allocated mappings for
3880814dcd43SSerapheim Dimitropoulos 	 * it yet.
3881814dcd43SSerapheim Dimitropoulos 	 */
3882814dcd43SSerapheim Dimitropoulos 	range_tree_clear(svr->svr_allocd_segs,
3883814dcd43SSerapheim Dimitropoulos 	    vdev_indirect_mapping_max_offset(vim),
3884814dcd43SSerapheim Dimitropoulos 	    vd->vdev_asize - vdev_indirect_mapping_max_offset(vim));
38855cabbc6bSPrashanth Sreenivasa 
3886814dcd43SSerapheim Dimitropoulos 	zcb->zcb_removing_size += range_tree_space(svr->svr_allocd_segs);
3887814dcd43SSerapheim Dimitropoulos 	range_tree_vacate(svr->svr_allocd_segs, claim_segment_cb, vd);
38885cabbc6bSPrashanth Sreenivasa 
38895cabbc6bSPrashanth Sreenivasa 	spa_config_exit(spa, SCL_CONFIG, FTAG);
38905cabbc6bSPrashanth Sreenivasa }
38915cabbc6bSPrashanth Sreenivasa 
38925cabbc6bSPrashanth Sreenivasa /* ARGSUSED */
38935cabbc6bSPrashanth Sreenivasa static int
38945cabbc6bSPrashanth Sreenivasa increment_indirect_mapping_cb(void *arg, const blkptr_t *bp, dmu_tx_t *tx)
38955cabbc6bSPrashanth Sreenivasa {
38965cabbc6bSPrashanth Sreenivasa 	zdb_cb_t *zcb = arg;
38975cabbc6bSPrashanth Sreenivasa 	spa_t *spa = zcb->zcb_spa;
38985cabbc6bSPrashanth Sreenivasa 	vdev_t *vd;
38995cabbc6bSPrashanth Sreenivasa 	const dva_t *dva = &bp->blk_dva[0];
39005cabbc6bSPrashanth Sreenivasa 
39015cabbc6bSPrashanth Sreenivasa 	ASSERT(!dump_opt['L']);
39025cabbc6bSPrashanth Sreenivasa 	ASSERT3U(BP_GET_NDVAS(bp), ==, 1);
39035cabbc6bSPrashanth Sreenivasa 
39045cabbc6bSPrashanth Sreenivasa 	spa_config_enter(spa, SCL_VDEV, FTAG, RW_READER);
39055cabbc6bSPrashanth Sreenivasa 	vd = vdev_lookup_top(zcb->zcb_spa, DVA_GET_VDEV(dva));
39065cabbc6bSPrashanth Sreenivasa 	ASSERT3P(vd, !=, NULL);
39075cabbc6bSPrashanth Sreenivasa 	spa_config_exit(spa, SCL_VDEV, FTAG);
39085cabbc6bSPrashanth Sreenivasa 
39095cabbc6bSPrashanth Sreenivasa 	ASSERT(vd->vdev_indirect_config.vic_mapping_object != 0);
39105cabbc6bSPrashanth Sreenivasa 	ASSERT3P(zcb->zcb_vd_obsolete_counts[vd->vdev_id], !=, NULL);
39115cabbc6bSPrashanth Sreenivasa 
39125cabbc6bSPrashanth Sreenivasa 	vdev_indirect_mapping_increment_obsolete_count(
39135cabbc6bSPrashanth Sreenivasa 	    vd->vdev_indirect_mapping,
39145cabbc6bSPrashanth Sreenivasa 	    DVA_GET_OFFSET(dva), DVA_GET_ASIZE(dva),
39155cabbc6bSPrashanth Sreenivasa 	    zcb->zcb_vd_obsolete_counts[vd->vdev_id]);
39165cabbc6bSPrashanth Sreenivasa 
39175cabbc6bSPrashanth Sreenivasa 	return (0);
39185cabbc6bSPrashanth Sreenivasa }
39195cabbc6bSPrashanth Sreenivasa 
39205cabbc6bSPrashanth Sreenivasa static uint32_t *
39215cabbc6bSPrashanth Sreenivasa zdb_load_obsolete_counts(vdev_t *vd)
39225cabbc6bSPrashanth Sreenivasa {
39235cabbc6bSPrashanth Sreenivasa 	vdev_indirect_mapping_t *vim = vd->vdev_indirect_mapping;
39245cabbc6bSPrashanth Sreenivasa 	spa_t *spa = vd->vdev_spa;
39255cabbc6bSPrashanth Sreenivasa 	spa_condensing_indirect_phys_t *scip =
39265cabbc6bSPrashanth Sreenivasa 	    &spa->spa_condensing_indirect_phys;
39275cabbc6bSPrashanth Sreenivasa 	uint32_t *counts;
39285cabbc6bSPrashanth Sreenivasa 
39295cabbc6bSPrashanth Sreenivasa 	EQUIV(vdev_obsolete_sm_object(vd) != 0, vd->vdev_obsolete_sm != NULL);
39305cabbc6bSPrashanth Sreenivasa 	counts = vdev_indirect_mapping_load_obsolete_counts(vim);
39315cabbc6bSPrashanth Sreenivasa 	if (vd->vdev_obsolete_sm != NULL) {
39325cabbc6bSPrashanth Sreenivasa 		vdev_indirect_mapping_load_obsolete_spacemap(vim, counts,
39335cabbc6bSPrashanth Sreenivasa 		    vd->vdev_obsolete_sm);
39345cabbc6bSPrashanth Sreenivasa 	}
39355cabbc6bSPrashanth Sreenivasa 	if (scip->scip_vdev == vd->vdev_id &&
39365cabbc6bSPrashanth Sreenivasa 	    scip->scip_prev_obsolete_sm_object != 0) {
39375cabbc6bSPrashanth Sreenivasa 		space_map_t *prev_obsolete_sm = NULL;
39385cabbc6bSPrashanth Sreenivasa 		VERIFY0(space_map_open(&prev_obsolete_sm, spa->spa_meta_objset,
39395cabbc6bSPrashanth Sreenivasa 		    scip->scip_prev_obsolete_sm_object, 0, vd->vdev_asize, 0));
39405cabbc6bSPrashanth Sreenivasa 		vdev_indirect_mapping_load_obsolete_spacemap(vim, counts,
39415cabbc6bSPrashanth Sreenivasa 		    prev_obsolete_sm);
39425cabbc6bSPrashanth Sreenivasa 		space_map_close(prev_obsolete_sm);
39435cabbc6bSPrashanth Sreenivasa 	}
39445cabbc6bSPrashanth Sreenivasa 	return (counts);
39455cabbc6bSPrashanth Sreenivasa }
39465cabbc6bSPrashanth Sreenivasa 
394786714001SSerapheim Dimitropoulos typedef struct checkpoint_sm_exclude_entry_arg {
394886714001SSerapheim Dimitropoulos 	vdev_t *cseea_vd;
394986714001SSerapheim Dimitropoulos 	uint64_t cseea_checkpoint_size;
395086714001SSerapheim Dimitropoulos } checkpoint_sm_exclude_entry_arg_t;
395186714001SSerapheim Dimitropoulos 
395286714001SSerapheim Dimitropoulos static int
395317f11284SSerapheim Dimitropoulos checkpoint_sm_exclude_entry_cb(space_map_entry_t *sme, void *arg)
395486714001SSerapheim Dimitropoulos {
395586714001SSerapheim Dimitropoulos 	checkpoint_sm_exclude_entry_arg_t *cseea = arg;
395686714001SSerapheim Dimitropoulos 	vdev_t *vd = cseea->cseea_vd;
395717f11284SSerapheim Dimitropoulos 	metaslab_t *ms = vd->vdev_ms[sme->sme_offset >> vd->vdev_ms_shift];
395817f11284SSerapheim Dimitropoulos 	uint64_t end = sme->sme_offset + sme->sme_run;
395986714001SSerapheim Dimitropoulos 
396017f11284SSerapheim Dimitropoulos 	ASSERT(sme->sme_type == SM_FREE);
396186714001SSerapheim Dimitropoulos 
396286714001SSerapheim Dimitropoulos 	/*
396386714001SSerapheim Dimitropoulos 	 * Since the vdev_checkpoint_sm exists in the vdev level
396486714001SSerapheim Dimitropoulos 	 * and the ms_sm space maps exist in the metaslab level,
396586714001SSerapheim Dimitropoulos 	 * an entry in the checkpoint space map could theoretically
396686714001SSerapheim Dimitropoulos 	 * cross the boundaries of the metaslab that it belongs.
396786714001SSerapheim Dimitropoulos 	 *
396886714001SSerapheim Dimitropoulos 	 * In reality, because of the way that we populate and
396986714001SSerapheim Dimitropoulos 	 * manipulate the checkpoint's space maps currently,
397086714001SSerapheim Dimitropoulos 	 * there shouldn't be any entries that cross metaslabs.
397186714001SSerapheim Dimitropoulos 	 * Hence the assertion below.
397286714001SSerapheim Dimitropoulos 	 *
397386714001SSerapheim Dimitropoulos 	 * That said, there is no fundamental requirement that
397486714001SSerapheim Dimitropoulos 	 * the checkpoint's space map entries should not cross
397586714001SSerapheim Dimitropoulos 	 * metaslab boundaries. So if needed we could add code
397686714001SSerapheim Dimitropoulos 	 * that handles metaslab-crossing segments in the future.
397786714001SSerapheim Dimitropoulos 	 */
397817f11284SSerapheim Dimitropoulos 	VERIFY3U(sme->sme_offset, >=, ms->ms_start);
397986714001SSerapheim Dimitropoulos 	VERIFY3U(end, <=, ms->ms_start + ms->ms_size);
398086714001SSerapheim Dimitropoulos 
398186714001SSerapheim Dimitropoulos 	/*
398286714001SSerapheim Dimitropoulos 	 * By removing the entry from the allocated segments we
398386714001SSerapheim Dimitropoulos 	 * also verify that the entry is there to begin with.
398486714001SSerapheim Dimitropoulos 	 */
398586714001SSerapheim Dimitropoulos 	mutex_enter(&ms->ms_lock);
398617f11284SSerapheim Dimitropoulos 	range_tree_remove(ms->ms_allocatable, sme->sme_offset, sme->sme_run);
398786714001SSerapheim Dimitropoulos 	mutex_exit(&ms->ms_lock);
398886714001SSerapheim Dimitropoulos 
398917f11284SSerapheim Dimitropoulos 	cseea->cseea_checkpoint_size += sme->sme_run;
399086714001SSerapheim Dimitropoulos 	return (0);
399186714001SSerapheim Dimitropoulos }
399286714001SSerapheim Dimitropoulos 
399386714001SSerapheim Dimitropoulos static void
399486714001SSerapheim Dimitropoulos zdb_leak_init_vdev_exclude_checkpoint(vdev_t *vd, zdb_cb_t *zcb)
399586714001SSerapheim Dimitropoulos {
399686714001SSerapheim Dimitropoulos 	spa_t *spa = vd->vdev_spa;
399786714001SSerapheim Dimitropoulos 	space_map_t *checkpoint_sm = NULL;
399886714001SSerapheim Dimitropoulos 	uint64_t checkpoint_sm_obj;
399986714001SSerapheim Dimitropoulos 
400086714001SSerapheim Dimitropoulos 	/*
400186714001SSerapheim Dimitropoulos 	 * If there is no vdev_top_zap, we are in a pool whose
400286714001SSerapheim Dimitropoulos 	 * version predates the pool checkpoint feature.
400386714001SSerapheim Dimitropoulos 	 */
400486714001SSerapheim Dimitropoulos 	if (vd->vdev_top_zap == 0)
400586714001SSerapheim Dimitropoulos 		return;
400686714001SSerapheim Dimitropoulos 
400786714001SSerapheim Dimitropoulos 	/*
400886714001SSerapheim Dimitropoulos 	 * If there is no reference of the vdev_checkpoint_sm in
400986714001SSerapheim Dimitropoulos 	 * the vdev_top_zap, then one of the following scenarios
401086714001SSerapheim Dimitropoulos 	 * is true:
401186714001SSerapheim Dimitropoulos 	 *
401286714001SSerapheim Dimitropoulos 	 * 1] There is no checkpoint
401386714001SSerapheim Dimitropoulos 	 * 2] There is a checkpoint, but no checkpointed blocks
401486714001SSerapheim Dimitropoulos 	 *    have been freed yet
401586714001SSerapheim Dimitropoulos 	 * 3] The current vdev is indirect
401686714001SSerapheim Dimitropoulos 	 *
401786714001SSerapheim Dimitropoulos 	 * In these cases we return immediately.
401886714001SSerapheim Dimitropoulos 	 */
401986714001SSerapheim Dimitropoulos 	if (zap_contains(spa_meta_objset(spa), vd->vdev_top_zap,
402086714001SSerapheim Dimitropoulos 	    VDEV_TOP_ZAP_POOL_CHECKPOINT_SM) != 0)
402186714001SSerapheim Dimitropoulos 		return;
402286714001SSerapheim Dimitropoulos 
402386714001SSerapheim Dimitropoulos 	VERIFY0(zap_lookup(spa_meta_objset(spa), vd->vdev_top_zap,
402486714001SSerapheim Dimitropoulos 	    VDEV_TOP_ZAP_POOL_CHECKPOINT_SM, sizeof (uint64_t), 1,
402586714001SSerapheim Dimitropoulos 	    &checkpoint_sm_obj));
402686714001SSerapheim Dimitropoulos 
402786714001SSerapheim Dimitropoulos 	checkpoint_sm_exclude_entry_arg_t cseea;
402886714001SSerapheim Dimitropoulos 	cseea.cseea_vd = vd;
402986714001SSerapheim Dimitropoulos 	cseea.cseea_checkpoint_size = 0;
403086714001SSerapheim Dimitropoulos 
403186714001SSerapheim Dimitropoulos 	VERIFY0(space_map_open(&checkpoint_sm, spa_meta_objset(spa),
403286714001SSerapheim Dimitropoulos 	    checkpoint_sm_obj, 0, vd->vdev_asize, vd->vdev_ashift));
403386714001SSerapheim Dimitropoulos 
403486714001SSerapheim Dimitropoulos 	VERIFY0(space_map_iterate(checkpoint_sm,
4035555d674dSSerapheim Dimitropoulos 	    space_map_length(checkpoint_sm),
403686714001SSerapheim Dimitropoulos 	    checkpoint_sm_exclude_entry_cb, &cseea));
403786714001SSerapheim Dimitropoulos 	space_map_close(checkpoint_sm);
403886714001SSerapheim Dimitropoulos 
403986714001SSerapheim Dimitropoulos 	zcb->zcb_checkpoint_size += cseea.cseea_checkpoint_size;
404086714001SSerapheim Dimitropoulos }
404186714001SSerapheim Dimitropoulos 
404286714001SSerapheim Dimitropoulos static void
404386714001SSerapheim Dimitropoulos zdb_leak_init_exclude_checkpoint(spa_t *spa, zdb_cb_t *zcb)
404486714001SSerapheim Dimitropoulos {
4045555d674dSSerapheim Dimitropoulos 	ASSERT(!dump_opt['L']);
4046555d674dSSerapheim Dimitropoulos 
404786714001SSerapheim Dimitropoulos 	vdev_t *rvd = spa->spa_root_vdev;
404886714001SSerapheim Dimitropoulos 	for (uint64_t c = 0; c < rvd->vdev_children; c++) {
404986714001SSerapheim Dimitropoulos 		ASSERT3U(c, ==, rvd->vdev_child[c]->vdev_id);
405086714001SSerapheim Dimitropoulos 		zdb_leak_init_vdev_exclude_checkpoint(rvd->vdev_child[c], zcb);
405186714001SSerapheim Dimitropoulos 	}
405286714001SSerapheim Dimitropoulos }
405386714001SSerapheim Dimitropoulos 
4054814dcd43SSerapheim Dimitropoulos static int
4055814dcd43SSerapheim Dimitropoulos count_unflushed_space_cb(spa_t *spa, space_map_entry_t *sme,
4056814dcd43SSerapheim Dimitropoulos     uint64_t txg, void *arg)
4057814dcd43SSerapheim Dimitropoulos {
4058814dcd43SSerapheim Dimitropoulos 	int64_t *ualloc_space = arg;
4059814dcd43SSerapheim Dimitropoulos 	uint64_t offset = sme->sme_offset;
4060814dcd43SSerapheim Dimitropoulos 	uint64_t vdev_id = sme->sme_vdev;
4061814dcd43SSerapheim Dimitropoulos 
4062814dcd43SSerapheim Dimitropoulos 	vdev_t *vd = vdev_lookup_top(spa, vdev_id);
4063814dcd43SSerapheim Dimitropoulos 	if (!vdev_is_concrete(vd))
4064814dcd43SSerapheim Dimitropoulos 		return (0);
4065814dcd43SSerapheim Dimitropoulos 
4066814dcd43SSerapheim Dimitropoulos 	metaslab_t *ms = vd->vdev_ms[offset >> vd->vdev_ms_shift];
4067814dcd43SSerapheim Dimitropoulos 	ASSERT(sme->sme_type == SM_ALLOC || sme->sme_type == SM_FREE);
4068814dcd43SSerapheim Dimitropoulos 
4069814dcd43SSerapheim Dimitropoulos 	if (txg < metaslab_unflushed_txg(ms))
4070814dcd43SSerapheim Dimitropoulos 		return (0);
4071814dcd43SSerapheim Dimitropoulos 
4072814dcd43SSerapheim Dimitropoulos 	if (sme->sme_type == SM_ALLOC)
4073814dcd43SSerapheim Dimitropoulos 		*ualloc_space += sme->sme_run;
4074814dcd43SSerapheim Dimitropoulos 	else
4075814dcd43SSerapheim Dimitropoulos 		*ualloc_space -= sme->sme_run;
4076814dcd43SSerapheim Dimitropoulos 
4077814dcd43SSerapheim Dimitropoulos 	return (0);
4078814dcd43SSerapheim Dimitropoulos }
4079814dcd43SSerapheim Dimitropoulos 
4080814dcd43SSerapheim Dimitropoulos static int64_t
4081814dcd43SSerapheim Dimitropoulos get_unflushed_alloc_space(spa_t *spa)
4082814dcd43SSerapheim Dimitropoulos {
4083814dcd43SSerapheim Dimitropoulos 	if (dump_opt['L'])
4084814dcd43SSerapheim Dimitropoulos 		return (0);
4085814dcd43SSerapheim Dimitropoulos 
4086814dcd43SSerapheim Dimitropoulos 	int64_t ualloc_space = 0;
4087814dcd43SSerapheim Dimitropoulos 	iterate_through_spacemap_logs(spa, count_unflushed_space_cb,
4088814dcd43SSerapheim Dimitropoulos 	    &ualloc_space);
4089814dcd43SSerapheim Dimitropoulos 	return (ualloc_space);
4090814dcd43SSerapheim Dimitropoulos }
4091814dcd43SSerapheim Dimitropoulos 
4092814dcd43SSerapheim Dimitropoulos static int
4093814dcd43SSerapheim Dimitropoulos load_unflushed_cb(spa_t *spa, space_map_entry_t *sme, uint64_t txg, void *arg)
4094814dcd43SSerapheim Dimitropoulos {
4095814dcd43SSerapheim Dimitropoulos 	maptype_t *uic_maptype = arg;
4096814dcd43SSerapheim Dimitropoulos 	uint64_t offset = sme->sme_offset;
4097814dcd43SSerapheim Dimitropoulos 	uint64_t size = sme->sme_run;
4098814dcd43SSerapheim Dimitropoulos 	uint64_t vdev_id = sme->sme_vdev;
4099814dcd43SSerapheim Dimitropoulos 	vdev_t *vd = vdev_lookup_top(spa, vdev_id);
4100814dcd43SSerapheim Dimitropoulos 
4101814dcd43SSerapheim Dimitropoulos 	/* skip indirect vdevs */
4102814dcd43SSerapheim Dimitropoulos 	if (!vdev_is_concrete(vd))
4103814dcd43SSerapheim Dimitropoulos 		return (0);
4104814dcd43SSerapheim Dimitropoulos 
4105814dcd43SSerapheim Dimitropoulos 	metaslab_t *ms = vd->vdev_ms[offset >> vd->vdev_ms_shift];
4106814dcd43SSerapheim Dimitropoulos 
4107814dcd43SSerapheim Dimitropoulos 	ASSERT(sme->sme_type == SM_ALLOC || sme->sme_type == SM_FREE);
4108814dcd43SSerapheim Dimitropoulos 	ASSERT(*uic_maptype == SM_ALLOC || *uic_maptype == SM_FREE);
4109814dcd43SSerapheim Dimitropoulos 
4110814dcd43SSerapheim Dimitropoulos 	if (txg < metaslab_unflushed_txg(ms))
4111814dcd43SSerapheim Dimitropoulos 		return (0);
4112814dcd43SSerapheim Dimitropoulos 
4113814dcd43SSerapheim Dimitropoulos 	if (*uic_maptype == sme->sme_type)
4114814dcd43SSerapheim Dimitropoulos 		range_tree_add(ms->ms_allocatable, offset, size);
4115814dcd43SSerapheim Dimitropoulos 	else
4116814dcd43SSerapheim Dimitropoulos 		range_tree_remove(ms->ms_allocatable, offset, size);
4117814dcd43SSerapheim Dimitropoulos 
4118814dcd43SSerapheim Dimitropoulos 	return (0);
4119814dcd43SSerapheim Dimitropoulos }
4120814dcd43SSerapheim Dimitropoulos 
4121814dcd43SSerapheim Dimitropoulos static void
4122814dcd43SSerapheim Dimitropoulos load_unflushed_to_ms_allocatables(spa_t *spa, maptype_t maptype)
4123814dcd43SSerapheim Dimitropoulos {
4124814dcd43SSerapheim Dimitropoulos 	iterate_through_spacemap_logs(spa, load_unflushed_cb, &maptype);
4125814dcd43SSerapheim Dimitropoulos }
4126814dcd43SSerapheim Dimitropoulos 
412786714001SSerapheim Dimitropoulos static void
412886714001SSerapheim Dimitropoulos load_concrete_ms_allocatable_trees(spa_t *spa, maptype_t maptype)
412986714001SSerapheim Dimitropoulos {
413086714001SSerapheim Dimitropoulos 	vdev_t *rvd = spa->spa_root_vdev;
413186714001SSerapheim Dimitropoulos 	for (uint64_t i = 0; i < rvd->vdev_children; i++) {
413286714001SSerapheim Dimitropoulos 		vdev_t *vd = rvd->vdev_child[i];
413386714001SSerapheim Dimitropoulos 
413486714001SSerapheim Dimitropoulos 		ASSERT3U(i, ==, vd->vdev_id);
413586714001SSerapheim Dimitropoulos 
413686714001SSerapheim Dimitropoulos 		if (vd->vdev_ops == &vdev_indirect_ops)
413786714001SSerapheim Dimitropoulos 			continue;
413886714001SSerapheim Dimitropoulos 
413986714001SSerapheim Dimitropoulos 		for (uint64_t m = 0; m < vd->vdev_ms_count; m++) {
414086714001SSerapheim Dimitropoulos 			metaslab_t *msp = vd->vdev_ms[m];
414186714001SSerapheim Dimitropoulos 
414286714001SSerapheim Dimitropoulos 			(void) fprintf(stderr,
414386714001SSerapheim Dimitropoulos 			    "\rloading concrete vdev %llu, "
414486714001SSerapheim Dimitropoulos 			    "metaslab %llu of %llu ...",
414586714001SSerapheim Dimitropoulos 			    (longlong_t)vd->vdev_id,
414686714001SSerapheim Dimitropoulos 			    (longlong_t)msp->ms_id,
414786714001SSerapheim Dimitropoulos 			    (longlong_t)vd->vdev_ms_count);
414886714001SSerapheim Dimitropoulos 
414986714001SSerapheim Dimitropoulos 			mutex_enter(&msp->ms_lock);
4150814dcd43SSerapheim Dimitropoulos 			range_tree_vacate(msp->ms_allocatable, NULL, NULL);
415186714001SSerapheim Dimitropoulos 
415286714001SSerapheim Dimitropoulos 			/*
415386714001SSerapheim Dimitropoulos 			 * We don't want to spend the CPU manipulating the
415486714001SSerapheim Dimitropoulos 			 * size-ordered tree, so clear the range_tree ops.
415586714001SSerapheim Dimitropoulos 			 */
415686714001SSerapheim Dimitropoulos 			msp->ms_allocatable->rt_ops = NULL;
415786714001SSerapheim Dimitropoulos 
415886714001SSerapheim Dimitropoulos 			if (msp->ms_sm != NULL) {
415986714001SSerapheim Dimitropoulos 				VERIFY0(space_map_load(msp->ms_sm,
416086714001SSerapheim Dimitropoulos 				    msp->ms_allocatable, maptype));
416186714001SSerapheim Dimitropoulos 			}
416286714001SSerapheim Dimitropoulos 			if (!msp->ms_loaded)
416386714001SSerapheim Dimitropoulos 				msp->ms_loaded = B_TRUE;
416486714001SSerapheim Dimitropoulos 			mutex_exit(&msp->ms_lock);
416586714001SSerapheim Dimitropoulos 		}
416686714001SSerapheim Dimitropoulos 	}
4167814dcd43SSerapheim Dimitropoulos 
4168814dcd43SSerapheim Dimitropoulos 	load_unflushed_to_ms_allocatables(spa, maptype);
416986714001SSerapheim Dimitropoulos }
417086714001SSerapheim Dimitropoulos 
417186714001SSerapheim Dimitropoulos /*
417286714001SSerapheim Dimitropoulos  * vm_idxp is an in-out parameter which (for indirect vdevs) is the
417386714001SSerapheim Dimitropoulos  * index in vim_entries that has the first entry in this metaslab.
417486714001SSerapheim Dimitropoulos  * On return, it will be set to the first entry after this metaslab.
417586714001SSerapheim Dimitropoulos  */
417686714001SSerapheim Dimitropoulos static void
417786714001SSerapheim Dimitropoulos load_indirect_ms_allocatable_tree(vdev_t *vd, metaslab_t *msp,
417886714001SSerapheim Dimitropoulos     uint64_t *vim_idxp)
417986714001SSerapheim Dimitropoulos {
418086714001SSerapheim Dimitropoulos 	vdev_indirect_mapping_t *vim = vd->vdev_indirect_mapping;
418186714001SSerapheim Dimitropoulos 
418286714001SSerapheim Dimitropoulos 	mutex_enter(&msp->ms_lock);
4183814dcd43SSerapheim Dimitropoulos 	range_tree_vacate(msp->ms_allocatable, NULL, NULL);
418486714001SSerapheim Dimitropoulos 
418586714001SSerapheim Dimitropoulos 	/*
418686714001SSerapheim Dimitropoulos 	 * We don't want to spend the CPU manipulating the
418786714001SSerapheim Dimitropoulos 	 * size-ordered tree, so clear the range_tree ops.
418886714001SSerapheim Dimitropoulos 	 */
418986714001SSerapheim Dimitropoulos 	msp->ms_allocatable->rt_ops = NULL;
419086714001SSerapheim Dimitropoulos 
419186714001SSerapheim Dimitropoulos 	for (; *vim_idxp < vdev_indirect_mapping_num_entries(vim);
419286714001SSerapheim Dimitropoulos 	    (*vim_idxp)++) {
419386714001SSerapheim Dimitropoulos 		vdev_indirect_mapping_entry_phys_t *vimep =
419486714001SSerapheim Dimitropoulos 		    &vim->vim_entries[*vim_idxp];
419586714001SSerapheim Dimitropoulos 		uint64_t ent_offset = DVA_MAPPING_GET_SRC_OFFSET(vimep);
419686714001SSerapheim Dimitropoulos 		uint64_t ent_len = DVA_GET_ASIZE(&vimep->vimep_dst);
419786714001SSerapheim Dimitropoulos 		ASSERT3U(ent_offset, >=, msp->ms_start);
419886714001SSerapheim Dimitropoulos 		if (ent_offset >= msp->ms_start + msp->ms_size)
419986714001SSerapheim Dimitropoulos 			break;
420086714001SSerapheim Dimitropoulos 
420186714001SSerapheim Dimitropoulos 		/*
420286714001SSerapheim Dimitropoulos 		 * Mappings do not cross metaslab boundaries,
420386714001SSerapheim Dimitropoulos 		 * because we create them by walking the metaslabs.
420486714001SSerapheim Dimitropoulos 		 */
420586714001SSerapheim Dimitropoulos 		ASSERT3U(ent_offset + ent_len, <=,
420686714001SSerapheim Dimitropoulos 		    msp->ms_start + msp->ms_size);
420786714001SSerapheim Dimitropoulos 		range_tree_add(msp->ms_allocatable, ent_offset, ent_len);
420886714001SSerapheim Dimitropoulos 	}
420986714001SSerapheim Dimitropoulos 
421086714001SSerapheim Dimitropoulos 	if (!msp->ms_loaded)
421186714001SSerapheim Dimitropoulos 		msp->ms_loaded = B_TRUE;
421286714001SSerapheim Dimitropoulos 	mutex_exit(&msp->ms_lock);
421386714001SSerapheim Dimitropoulos }
421486714001SSerapheim Dimitropoulos 
421586714001SSerapheim Dimitropoulos static void
421686714001SSerapheim Dimitropoulos zdb_leak_init_prepare_indirect_vdevs(spa_t *spa, zdb_cb_t *zcb)
421786714001SSerapheim Dimitropoulos {
4218555d674dSSerapheim Dimitropoulos 	ASSERT(!dump_opt['L']);
4219555d674dSSerapheim Dimitropoulos 
422086714001SSerapheim Dimitropoulos 	vdev_t *rvd = spa->spa_root_vdev;
422186714001SSerapheim Dimitropoulos 	for (uint64_t c = 0; c < rvd->vdev_children; c++) {
422286714001SSerapheim Dimitropoulos 		vdev_t *vd = rvd->vdev_child[c];
422386714001SSerapheim Dimitropoulos 
422486714001SSerapheim Dimitropoulos 		ASSERT3U(c, ==, vd->vdev_id);
422586714001SSerapheim Dimitropoulos 
422686714001SSerapheim Dimitropoulos 		if (vd->vdev_ops != &vdev_indirect_ops)
422786714001SSerapheim Dimitropoulos 			continue;
422886714001SSerapheim Dimitropoulos 
422986714001SSerapheim Dimitropoulos 		/*
423086714001SSerapheim Dimitropoulos 		 * Note: we don't check for mapping leaks on
423186714001SSerapheim Dimitropoulos 		 * removing vdevs because their ms_allocatable's
423286714001SSerapheim Dimitropoulos 		 * are used to look for leaks in allocated space.
423386714001SSerapheim Dimitropoulos 		 */
423486714001SSerapheim Dimitropoulos 		zcb->zcb_vd_obsolete_counts[c] = zdb_load_obsolete_counts(vd);
423586714001SSerapheim Dimitropoulos 
423686714001SSerapheim Dimitropoulos 		/*
423786714001SSerapheim Dimitropoulos 		 * Normally, indirect vdevs don't have any
423886714001SSerapheim Dimitropoulos 		 * metaslabs.  We want to set them up for
423986714001SSerapheim Dimitropoulos 		 * zio_claim().
424086714001SSerapheim Dimitropoulos 		 */
424186714001SSerapheim Dimitropoulos 		VERIFY0(vdev_metaslab_init(vd, 0));
424286714001SSerapheim Dimitropoulos 
424386714001SSerapheim Dimitropoulos 		vdev_indirect_mapping_t *vim = vd->vdev_indirect_mapping;
424486714001SSerapheim Dimitropoulos 		uint64_t vim_idx = 0;
424586714001SSerapheim Dimitropoulos 		for (uint64_t m = 0; m < vd->vdev_ms_count; m++) {
424686714001SSerapheim Dimitropoulos 
424786714001SSerapheim Dimitropoulos 			(void) fprintf(stderr,
424886714001SSerapheim Dimitropoulos 			    "\rloading indirect vdev %llu, "
424986714001SSerapheim Dimitropoulos 			    "metaslab %llu of %llu ...",
425086714001SSerapheim Dimitropoulos 			    (longlong_t)vd->vdev_id,
425186714001SSerapheim Dimitropoulos 			    (longlong_t)vd->vdev_ms[m]->ms_id,
425286714001SSerapheim Dimitropoulos 			    (longlong_t)vd->vdev_ms_count);
425386714001SSerapheim Dimitropoulos 
425486714001SSerapheim Dimitropoulos 			load_indirect_ms_allocatable_tree(vd, vd->vdev_ms[m],
425586714001SSerapheim Dimitropoulos 			    &vim_idx);
425686714001SSerapheim Dimitropoulos 		}
425786714001SSerapheim Dimitropoulos 		ASSERT3U(vim_idx, ==, vdev_indirect_mapping_num_entries(vim));
425886714001SSerapheim Dimitropoulos 	}
425986714001SSerapheim Dimitropoulos }
426086714001SSerapheim Dimitropoulos 
4261b24ab676SJeff Bonwick static void
4262b24ab676SJeff Bonwick zdb_leak_init(spa_t *spa, zdb_cb_t *zcb)
4263b24ab676SJeff Bonwick {
4264cde58dbcSMatthew Ahrens 	zcb->zcb_spa = spa;
4265cde58dbcSMatthew Ahrens 
4266555d674dSSerapheim Dimitropoulos 	if (dump_opt['L'])
4267555d674dSSerapheim Dimitropoulos 		return;
42688363e80aSGeorge Wilson 
4269555d674dSSerapheim Dimitropoulos 	dsl_pool_t *dp = spa->spa_dsl_pool;
4270555d674dSSerapheim Dimitropoulos 	vdev_t *rvd = spa->spa_root_vdev;
42718363e80aSGeorge Wilson 
4272555d674dSSerapheim Dimitropoulos 	/*
4273555d674dSSerapheim Dimitropoulos 	 * We are going to be changing the meaning of the metaslab's
4274555d674dSSerapheim Dimitropoulos 	 * ms_allocatable.  Ensure that the allocator doesn't try to
4275555d674dSSerapheim Dimitropoulos 	 * use the tree.
4276555d674dSSerapheim Dimitropoulos 	 */
4277555d674dSSerapheim Dimitropoulos 	spa->spa_normal_class->mc_ops = &zdb_metaslab_ops;
4278555d674dSSerapheim Dimitropoulos 	spa->spa_log_class->mc_ops = &zdb_metaslab_ops;
42795cabbc6bSPrashanth Sreenivasa 
4280555d674dSSerapheim Dimitropoulos 	zcb->zcb_vd_obsolete_counts =
4281555d674dSSerapheim Dimitropoulos 	    umem_zalloc(rvd->vdev_children * sizeof (uint32_t *),
4282555d674dSSerapheim Dimitropoulos 	    UMEM_NOFAIL);
42835cabbc6bSPrashanth Sreenivasa 
4284555d674dSSerapheim Dimitropoulos 	/*
4285555d674dSSerapheim Dimitropoulos 	 * For leak detection, we overload the ms_allocatable trees
4286555d674dSSerapheim Dimitropoulos 	 * to contain allocated segments instead of free segments.
4287555d674dSSerapheim Dimitropoulos 	 * As a result, we can't use the normal metaslab_load/unload
4288555d674dSSerapheim Dimitropoulos 	 * interfaces.
4289555d674dSSerapheim Dimitropoulos 	 */
4290555d674dSSerapheim Dimitropoulos 	zdb_leak_init_prepare_indirect_vdevs(spa, zcb);
4291555d674dSSerapheim Dimitropoulos 	load_concrete_ms_allocatable_trees(spa, SM_ALLOC);
42925cabbc6bSPrashanth Sreenivasa 
4293555d674dSSerapheim Dimitropoulos 	/*
4294555d674dSSerapheim Dimitropoulos 	 * On load_concrete_ms_allocatable_trees() we loaded all the
4295555d674dSSerapheim Dimitropoulos 	 * allocated entries from the ms_sm to the ms_allocatable for
4296555d674dSSerapheim Dimitropoulos 	 * each metaslab. If the pool has a checkpoint or is in the
4297555d674dSSerapheim Dimitropoulos 	 * middle of discarding a checkpoint, some of these blocks
4298555d674dSSerapheim Dimitropoulos 	 * may have been freed but their ms_sm may not have been
4299555d674dSSerapheim Dimitropoulos 	 * updated because they are referenced by the checkpoint. In
4300555d674dSSerapheim Dimitropoulos 	 * order to avoid false-positives during leak-detection, we
4301555d674dSSerapheim Dimitropoulos 	 * go through the vdev's checkpoint space map and exclude all
4302555d674dSSerapheim Dimitropoulos 	 * its entries from their relevant ms_allocatable.
4303555d674dSSerapheim Dimitropoulos 	 *
4304555d674dSSerapheim Dimitropoulos 	 * We also aggregate the space held by the checkpoint and add
4305555d674dSSerapheim Dimitropoulos 	 * it to zcb_checkpoint_size.
4306555d674dSSerapheim Dimitropoulos 	 *
4307555d674dSSerapheim Dimitropoulos 	 * Note that at this point we are also verifying that all the
4308555d674dSSerapheim Dimitropoulos 	 * entries on the checkpoint_sm are marked as allocated in
4309555d674dSSerapheim Dimitropoulos 	 * the ms_sm of their relevant metaslab.
4310555d674dSSerapheim Dimitropoulos 	 * [see comment in checkpoint_sm_exclude_entry_cb()]
4311555d674dSSerapheim Dimitropoulos 	 */
4312555d674dSSerapheim Dimitropoulos 	zdb_leak_init_exclude_checkpoint(spa, zcb);
4313555d674dSSerapheim Dimitropoulos 	ASSERT3U(zcb->zcb_checkpoint_size, ==, spa_get_checkpoint_space(spa));
43145cabbc6bSPrashanth Sreenivasa 
4315555d674dSSerapheim Dimitropoulos 	/* for cleaner progress output */
4316555d674dSSerapheim Dimitropoulos 	(void) fprintf(stderr, "\n");
4317555d674dSSerapheim Dimitropoulos 
4318555d674dSSerapheim Dimitropoulos 	if (bpobj_is_open(&dp->dp_obsolete_bpobj)) {
4319555d674dSSerapheim Dimitropoulos 		ASSERT(spa_feature_is_enabled(spa,
4320555d674dSSerapheim Dimitropoulos 		    SPA_FEATURE_DEVICE_REMOVAL));
4321555d674dSSerapheim Dimitropoulos 		(void) bpobj_iterate_nofree(&dp->dp_obsolete_bpobj,
4322555d674dSSerapheim Dimitropoulos 		    increment_indirect_mapping_cb, zcb, NULL);
4323b24ab676SJeff Bonwick 	}
4324b24ab676SJeff Bonwick 
4325b24ab676SJeff Bonwick 	spa_config_enter(spa, SCL_CONFIG, FTAG, RW_READER);
4326bbfd46c4SJeff Bonwick 	zdb_ddt_leak_init(spa, zcb);
4327b24ab676SJeff Bonwick 	spa_config_exit(spa, SCL_CONFIG, FTAG);
4328b24ab676SJeff Bonwick }
4329b24ab676SJeff Bonwick 
43305cabbc6bSPrashanth Sreenivasa static boolean_t
43315cabbc6bSPrashanth Sreenivasa zdb_check_for_obsolete_leaks(vdev_t *vd, zdb_cb_t *zcb)
43325cabbc6bSPrashanth Sreenivasa {
43335cabbc6bSPrashanth Sreenivasa 	boolean_t leaks = B_FALSE;
43345cabbc6bSPrashanth Sreenivasa 	vdev_indirect_mapping_t *vim = vd->vdev_indirect_mapping;
43355cabbc6bSPrashanth Sreenivasa 	uint64_t total_leaked = 0;
43365cabbc6bSPrashanth Sreenivasa 
43375cabbc6bSPrashanth Sreenivasa 	ASSERT(vim != NULL);
43385cabbc6bSPrashanth Sreenivasa 
43395cabbc6bSPrashanth Sreenivasa 	for (uint64_t i = 0; i < vdev_indirect_mapping_num_entries(vim); i++) {
43405cabbc6bSPrashanth Sreenivasa 		vdev_indirect_mapping_entry_phys_t *vimep =
43415cabbc6bSPrashanth Sreenivasa 		    &vim->vim_entries[i];
43425cabbc6bSPrashanth Sreenivasa 		uint64_t obsolete_bytes = 0;
43435cabbc6bSPrashanth Sreenivasa 		uint64_t offset = DVA_MAPPING_GET_SRC_OFFSET(vimep);
43445cabbc6bSPrashanth Sreenivasa 		metaslab_t *msp = vd->vdev_ms[offset >> vd->vdev_ms_shift];
43455cabbc6bSPrashanth Sreenivasa 
43465cabbc6bSPrashanth Sreenivasa 		/*
43475cabbc6bSPrashanth Sreenivasa 		 * This is not very efficient but it's easy to
43485cabbc6bSPrashanth Sreenivasa 		 * verify correctness.
43495cabbc6bSPrashanth Sreenivasa 		 */
43505cabbc6bSPrashanth Sreenivasa 		for (uint64_t inner_offset = 0;
43515cabbc6bSPrashanth Sreenivasa 		    inner_offset < DVA_GET_ASIZE(&vimep->vimep_dst);
43525cabbc6bSPrashanth Sreenivasa 		    inner_offset += 1 << vd->vdev_ashift) {
435386714001SSerapheim Dimitropoulos 			if (range_tree_contains(msp->ms_allocatable,
43545cabbc6bSPrashanth Sreenivasa 			    offset + inner_offset, 1 << vd->vdev_ashift)) {
43555cabbc6bSPrashanth Sreenivasa 				obsolete_bytes += 1 << vd->vdev_ashift;
43565cabbc6bSPrashanth Sreenivasa 			}
43575cabbc6bSPrashanth Sreenivasa 		}
43585cabbc6bSPrashanth Sreenivasa 
43595cabbc6bSPrashanth Sreenivasa 		int64_t bytes_leaked = obsolete_bytes -
43605cabbc6bSPrashanth Sreenivasa 		    zcb->zcb_vd_obsolete_counts[vd->vdev_id][i];
43615cabbc6bSPrashanth Sreenivasa 		ASSERT3U(DVA_GET_ASIZE(&vimep->vimep_dst), >=,
43625cabbc6bSPrashanth Sreenivasa 		    zcb->zcb_vd_obsolete_counts[vd->vdev_id][i]);
43635cabbc6bSPrashanth Sreenivasa 		if (bytes_leaked != 0 &&
43645cabbc6bSPrashanth Sreenivasa 		    (vdev_obsolete_counts_are_precise(vd) ||
43655cabbc6bSPrashanth Sreenivasa 		    dump_opt['d'] >= 5)) {
43665cabbc6bSPrashanth Sreenivasa 			(void) printf("obsolete indirect mapping count "
43675cabbc6bSPrashanth Sreenivasa 			    "mismatch on %llu:%llx:%llx : %llx bytes leaked\n",
43685cabbc6bSPrashanth Sreenivasa 			    (u_longlong_t)vd->vdev_id,
43695cabbc6bSPrashanth Sreenivasa 			    (u_longlong_t)DVA_MAPPING_GET_SRC_OFFSET(vimep),
43705cabbc6bSPrashanth Sreenivasa 			    (u_longlong_t)DVA_GET_ASIZE(&vimep->vimep_dst),
43715cabbc6bSPrashanth Sreenivasa 			    (u_longlong_t)bytes_leaked);
43725cabbc6bSPrashanth Sreenivasa 		}
43735cabbc6bSPrashanth Sreenivasa 		total_leaked += ABS(bytes_leaked);
43745cabbc6bSPrashanth Sreenivasa 	}
43755cabbc6bSPrashanth Sreenivasa 
43765cabbc6bSPrashanth Sreenivasa 	if (!vdev_obsolete_counts_are_precise(vd) && total_leaked > 0) {
43775cabbc6bSPrashanth Sreenivasa 		int pct_leaked = total_leaked * 100 /
43785cabbc6bSPrashanth Sreenivasa 		    vdev_indirect_mapping_bytes_mapped(vim);
43795cabbc6bSPrashanth Sreenivasa 		(void) printf("cannot verify obsolete indirect mapping "
43805cabbc6bSPrashanth Sreenivasa 		    "counts of vdev %llu because precise feature was not "
43815cabbc6bSPrashanth Sreenivasa 		    "enabled when it was removed: %d%% (%llx bytes) of mapping"
43825cabbc6bSPrashanth Sreenivasa 		    "unreferenced\n",
43835cabbc6bSPrashanth Sreenivasa 		    (u_longlong_t)vd->vdev_id, pct_leaked,
43845cabbc6bSPrashanth Sreenivasa 		    (u_longlong_t)total_leaked);
43855cabbc6bSPrashanth Sreenivasa 	} else if (total_leaked > 0) {
43865cabbc6bSPrashanth Sreenivasa 		(void) printf("obsolete indirect mapping count mismatch "
43875cabbc6bSPrashanth Sreenivasa 		    "for vdev %llu -- %llx total bytes mismatched\n",
43885cabbc6bSPrashanth Sreenivasa 		    (u_longlong_t)vd->vdev_id,
43895cabbc6bSPrashanth Sreenivasa 		    (u_longlong_t)total_leaked);
43905cabbc6bSPrashanth Sreenivasa 		leaks |= B_TRUE;
43915cabbc6bSPrashanth Sreenivasa 	}
43925cabbc6bSPrashanth Sreenivasa 
43935cabbc6bSPrashanth Sreenivasa 	vdev_indirect_mapping_free_obsolete_counts(vim,
43945cabbc6bSPrashanth Sreenivasa 	    zcb->zcb_vd_obsolete_counts[vd->vdev_id]);
43955cabbc6bSPrashanth Sreenivasa 	zcb->zcb_vd_obsolete_counts[vd->vdev_id] = NULL;
43965cabbc6bSPrashanth Sreenivasa 
43975cabbc6bSPrashanth Sreenivasa 	return (leaks);
43985cabbc6bSPrashanth Sreenivasa }
43995cabbc6bSPrashanth Sreenivasa 
44005cabbc6bSPrashanth Sreenivasa static boolean_t
44015cabbc6bSPrashanth Sreenivasa zdb_leak_fini(spa_t *spa, zdb_cb_t *zcb)
4402b24ab676SJeff Bonwick {
4403555d674dSSerapheim Dimitropoulos 	if (dump_opt['L'])
4404555d674dSSerapheim Dimitropoulos 		return (B_FALSE);
4405555d674dSSerapheim Dimitropoulos 
44065cabbc6bSPrashanth Sreenivasa 	boolean_t leaks = B_FALSE;
44075cabbc6bSPrashanth Sreenivasa 
4408555d674dSSerapheim Dimitropoulos 	vdev_t *rvd = spa->spa_root_vdev;
4409555d674dSSerapheim Dimitropoulos 	for (unsigned c = 0; c < rvd->vdev_children; c++) {
4410555d674dSSerapheim Dimitropoulos 		vdev_t *vd = rvd->vdev_child[c];
4411555d674dSSerapheim Dimitropoulos #if DEBUG
4412555d674dSSerapheim Dimitropoulos 		metaslab_group_t *mg = vd->vdev_mg;
4413555d674dSSerapheim Dimitropoulos #endif
44148363e80aSGeorge Wilson 
4415555d674dSSerapheim Dimitropoulos 		if (zcb->zcb_vd_obsolete_counts[c] != NULL) {
4416555d674dSSerapheim Dimitropoulos 			leaks |= zdb_check_for_obsolete_leaks(vd, zcb);
4417555d674dSSerapheim Dimitropoulos 		}
4418555d674dSSerapheim Dimitropoulos 
4419555d674dSSerapheim Dimitropoulos 		for (uint64_t m = 0; m < vd->vdev_ms_count; m++) {
4420555d674dSSerapheim Dimitropoulos 			metaslab_t *msp = vd->vdev_ms[m];
4421555d674dSSerapheim Dimitropoulos 			ASSERT3P(mg, ==, msp->ms_group);
4422555d674dSSerapheim Dimitropoulos 
4423555d674dSSerapheim Dimitropoulos 			/*
4424555d674dSSerapheim Dimitropoulos 			 * ms_allocatable has been overloaded
4425555d674dSSerapheim Dimitropoulos 			 * to contain allocated segments. Now that
4426555d674dSSerapheim Dimitropoulos 			 * we finished traversing all blocks, any
4427555d674dSSerapheim Dimitropoulos 			 * block that remains in the ms_allocatable
4428555d674dSSerapheim Dimitropoulos 			 * represents an allocated block that we
4429555d674dSSerapheim Dimitropoulos 			 * did not claim during the traversal.
4430555d674dSSerapheim Dimitropoulos 			 * Claimed blocks would have been removed
4431555d674dSSerapheim Dimitropoulos 			 * from the ms_allocatable.  For indirect
4432555d674dSSerapheim Dimitropoulos 			 * vdevs, space remaining in the tree
4433555d674dSSerapheim Dimitropoulos 			 * represents parts of the mapping that are
4434555d674dSSerapheim Dimitropoulos 			 * not referenced, which is not a bug.
4435555d674dSSerapheim Dimitropoulos 			 */
4436555d674dSSerapheim Dimitropoulos 			if (vd->vdev_ops == &vdev_indirect_ops) {
4437555d674dSSerapheim Dimitropoulos 				range_tree_vacate(msp->ms_allocatable,
4438555d674dSSerapheim Dimitropoulos 				    NULL, NULL);
4439555d674dSSerapheim Dimitropoulos 			} else {
4440555d674dSSerapheim Dimitropoulos 				range_tree_vacate(msp->ms_allocatable,
4441555d674dSSerapheim Dimitropoulos 				    zdb_leak, vd);
4442555d674dSSerapheim Dimitropoulos 			}
4443555d674dSSerapheim Dimitropoulos 			if (msp->ms_loaded) {
4444555d674dSSerapheim Dimitropoulos 				msp->ms_loaded = B_FALSE;
4445b24ab676SJeff Bonwick 			}
4446b24ab676SJeff Bonwick 		}
44475cabbc6bSPrashanth Sreenivasa 
4448b24ab676SJeff Bonwick 	}
4449555d674dSSerapheim Dimitropoulos 
4450555d674dSSerapheim Dimitropoulos 	umem_free(zcb->zcb_vd_obsolete_counts,
4451555d674dSSerapheim Dimitropoulos 	    rvd->vdev_children * sizeof (uint32_t *));
4452555d674dSSerapheim Dimitropoulos 	zcb->zcb_vd_obsolete_counts = NULL;
4453555d674dSSerapheim Dimitropoulos 
44545cabbc6bSPrashanth Sreenivasa 	return (leaks);
4455b24ab676SJeff Bonwick }
4456b24ab676SJeff Bonwick 
4457cde58dbcSMatthew Ahrens /* ARGSUSED */
4458cde58dbcSMatthew Ahrens static int
4459cde58dbcSMatthew Ahrens count_block_cb(void *arg, const blkptr_t *bp, dmu_tx_t *tx)
4460cde58dbcSMatthew Ahrens {
4461cde58dbcSMatthew Ahrens 	zdb_cb_t *zcb = arg;
4462cde58dbcSMatthew Ahrens 
4463490d05b9SMatthew Ahrens 	if (dump_opt['b'] >= 5) {
4464cde58dbcSMatthew Ahrens 		char blkbuf[BP_SPRINTF_LEN];
446543466aaeSMax Grossman 		snprintf_blkptr(blkbuf, sizeof (blkbuf), bp);
4466cde58dbcSMatthew Ahrens 		(void) printf("[%s] %s\n",
4467cde58dbcSMatthew Ahrens 		    "deferred free", blkbuf);
4468cde58dbcSMatthew Ahrens 	}
4469cde58dbcSMatthew Ahrens 	zdb_count_block(zcb, NULL, bp, ZDB_OT_DEFERRED);
4470cde58dbcSMatthew Ahrens 	return (0);
4471cde58dbcSMatthew Ahrens }
4472cde58dbcSMatthew Ahrens 
4473fa9e4066Sahrens static int
4474fa9e4066Sahrens dump_block_stats(spa_t *spa)
4475fa9e4066Sahrens {
44763f7978d0SAlan Somers 	zdb_cb_t zcb;
4477fa9e4066Sahrens 	zdb_blkstats_t *zb, *tzb;
4478b24ab676SJeff Bonwick 	uint64_t norm_alloc, norm_space, total_alloc, total_found;
4479eb633035STom Caputi 	int flags = TRAVERSE_PRE | TRAVERSE_PREFETCH_METADATA |
4480eb633035STom Caputi 	    TRAVERSE_NO_DECRYPT | TRAVERSE_HARD;
44815d7b4d43SMatthew Ahrens 	boolean_t leaks = B_FALSE;
4482663207adSDon Brady 	int err;
4483fa9e4066Sahrens 
44843f7978d0SAlan Somers 	bzero(&zcb, sizeof (zcb));
4485490d05b9SMatthew Ahrens 	(void) printf("\nTraversing all blocks %s%s%s%s%s...\n\n",
4486b24ab676SJeff Bonwick 	    (dump_opt['c'] || !dump_opt['L']) ? "to verify " : "",
4487b24ab676SJeff Bonwick 	    (dump_opt['c'] == 1) ? "metadata " : "",
4488b24ab676SJeff Bonwick 	    dump_opt['c'] ? "checksums " : "",
4489b24ab676SJeff Bonwick 	    (dump_opt['c'] && !dump_opt['L']) ? "and verify " : "",
4490b24ab676SJeff Bonwick 	    !dump_opt['L'] ? "nothing leaked " : "");
4491fa9e4066Sahrens 
4492fa9e4066Sahrens 	/*
4493555d674dSSerapheim Dimitropoulos 	 * When leak detection is enabled we load all space maps as SM_ALLOC
4494555d674dSSerapheim Dimitropoulos 	 * maps, then traverse the pool claiming each block we discover. If
4495555d674dSSerapheim Dimitropoulos 	 * the pool is perfectly consistent, the segment trees will be empty
4496555d674dSSerapheim Dimitropoulos 	 * when we're done. Anything left over is a leak; any block we can't
4497555d674dSSerapheim Dimitropoulos 	 * claim (because it's not part of any space map) is a double
4498555d674dSSerapheim Dimitropoulos 	 * allocation, reference to a freed block, or an unclaimed log block.
4499555d674dSSerapheim Dimitropoulos 	 *
4500555d674dSSerapheim Dimitropoulos 	 * When leak detection is disabled (-L option) we still traverse the
4501555d674dSSerapheim Dimitropoulos 	 * pool claiming each block we discover, but we skip opening any space
4502555d674dSSerapheim Dimitropoulos 	 * maps.
4503fa9e4066Sahrens 	 */
4504555d674dSSerapheim Dimitropoulos 	bzero(&zcb, sizeof (zdb_cb_t));
4505b24ab676SJeff Bonwick 	zdb_leak_init(spa, &zcb);
4506fa9e4066Sahrens 
4507fa9e4066Sahrens 	/*
4508fa9e4066Sahrens 	 * If there's a deferred-free bplist, process that first.
4509fa9e4066Sahrens 	 */
4510cde58dbcSMatthew Ahrens 	(void) bpobj_iterate_nofree(&spa->spa_deferred_bpobj,
4511cde58dbcSMatthew Ahrens 	    count_block_cb, &zcb, NULL);
45125cabbc6bSPrashanth Sreenivasa 
45133b2aab18SMatthew Ahrens 	if (spa_version(spa) >= SPA_VERSION_DEADLISTS) {
45143b2aab18SMatthew Ahrens 		(void) bpobj_iterate_nofree(&spa->spa_dsl_pool->dp_free_bpobj,
45153b2aab18SMatthew Ahrens 		    count_block_cb, &zcb, NULL);
45163b2aab18SMatthew Ahrens 	}
45175cabbc6bSPrashanth Sreenivasa 
45185cabbc6bSPrashanth Sreenivasa 	zdb_claim_removing(spa, &zcb);
45195cabbc6bSPrashanth Sreenivasa 
45202acef22dSMatthew Ahrens 	if (spa_feature_is_active(spa, SPA_FEATURE_ASYNC_DESTROY)) {
4521b420f3adSRichard Lowe 		VERIFY3U(0, ==, bptree_iterate(spa->spa_meta_objset,
4522ad135b5dSChristopher Siden 		    spa->spa_dsl_pool->dp_bptree_obj, B_FALSE, count_block_cb,
4523ad135b5dSChristopher Siden 		    &zcb, NULL));
4524ad135b5dSChristopher Siden 	}
4525fa9e4066Sahrens 
4526bbfd46c4SJeff Bonwick 	if (dump_opt['c'] > 1)
4527bbfd46c4SJeff Bonwick 		flags |= TRAVERSE_PREFETCH_DATA;
4528bbfd46c4SJeff Bonwick 
4529490d05b9SMatthew Ahrens 	zcb.zcb_totalasize = metaslab_class_get_alloc(spa_normal_class(spa));
4530663207adSDon Brady 	zcb.zcb_totalasize += metaslab_class_get_alloc(spa_special_class(spa));
4531663207adSDon Brady 	zcb.zcb_totalasize += metaslab_class_get_alloc(spa_dedup_class(spa));
4532490d05b9SMatthew Ahrens 	zcb.zcb_start = zcb.zcb_lastprint = gethrtime();
4533663207adSDon Brady 	err = traverse_pool(spa, 0, flags, zdb_blkptr_cb, &zcb);
4534fa9e4066Sahrens 
453531d7e8faSGeorge Wilson 	/*
453631d7e8faSGeorge Wilson 	 * If we've traversed the data blocks then we need to wait for those
453731d7e8faSGeorge Wilson 	 * I/Os to complete. We leverage "The Godfather" zio to wait on
453831d7e8faSGeorge Wilson 	 * all async I/Os to complete.
453931d7e8faSGeorge Wilson 	 */
454031d7e8faSGeorge Wilson 	if (dump_opt['c']) {
45416f834bc1SMatthew Ahrens 		for (int i = 0; i < max_ncpus; i++) {
45426f834bc1SMatthew Ahrens 			(void) zio_wait(spa->spa_async_zio_root[i]);
45436f834bc1SMatthew Ahrens 			spa->spa_async_zio_root[i] = zio_root(spa, NULL, NULL,
45446f834bc1SMatthew Ahrens 			    ZIO_FLAG_CANFAIL | ZIO_FLAG_SPECULATIVE |
45456f834bc1SMatthew Ahrens 			    ZIO_FLAG_GODFATHER);
45466f834bc1SMatthew Ahrens 		}
454731d7e8faSGeorge Wilson 	}
454831d7e8faSGeorge Wilson 
4549663207adSDon Brady 	/*
4550663207adSDon Brady 	 * Done after zio_wait() since zcb_haderrors is modified in
4551663207adSDon Brady 	 * zdb_blkptr_done()
4552663207adSDon Brady 	 */
4553663207adSDon Brady 	zcb.zcb_haderrors |= err;
4554663207adSDon Brady 
4555b24ab676SJeff Bonwick 	if (zcb.zcb_haderrors) {
4556fa9e4066Sahrens 		(void) printf("\nError counts:\n\n");
4557fa9e4066Sahrens 		(void) printf("\t%5s  %s\n", "errno", "count");
4558b24ab676SJeff Bonwick 		for (int e = 0; e < 256; e++) {
4559fa9e4066Sahrens 			if (zcb.zcb_errors[e] != 0) {
4560fa9e4066Sahrens 				(void) printf("\t%5d  %llu\n",
4561fa9e4066Sahrens 				    e, (u_longlong_t)zcb.zcb_errors[e]);
4562fa9e4066Sahrens 			}
4563fa9e4066Sahrens 		}
4564fa9e4066Sahrens 	}
4565fa9e4066Sahrens 
4566fa9e4066Sahrens 	/*
4567fa9e4066Sahrens 	 * Report any leaked segments.
4568fa9e4066Sahrens 	 */
45695cabbc6bSPrashanth Sreenivasa 	leaks |= zdb_leak_fini(spa, &zcb);
4570fa9e4066Sahrens 
4571b24ab676SJeff Bonwick 	tzb = &zcb.zcb_type[ZB_TOTAL][ZDB_OT_TOTAL];
45728654d025Sperrin 
4573b24ab676SJeff Bonwick 	norm_alloc = metaslab_class_get_alloc(spa_normal_class(spa));
4574b24ab676SJeff Bonwick 	norm_space = metaslab_class_get_space(spa_normal_class(spa));
45758654d025Sperrin 
4576663207adSDon Brady 	total_alloc = norm_alloc +
4577663207adSDon Brady 	    metaslab_class_get_alloc(spa_log_class(spa)) +
4578663207adSDon Brady 	    metaslab_class_get_alloc(spa_special_class(spa)) +
4579814dcd43SSerapheim Dimitropoulos 	    metaslab_class_get_alloc(spa_dedup_class(spa)) +
4580814dcd43SSerapheim Dimitropoulos 	    get_unflushed_alloc_space(spa);
45815cabbc6bSPrashanth Sreenivasa 	total_found = tzb->zb_asize - zcb.zcb_dedup_asize +
458286714001SSerapheim Dimitropoulos 	    zcb.zcb_removing_size + zcb.zcb_checkpoint_size;
4583fa9e4066Sahrens 
4584555d674dSSerapheim Dimitropoulos 	if (total_found == total_alloc && !dump_opt['L']) {
4585555d674dSSerapheim Dimitropoulos 		(void) printf("\n\tNo leaks (block sum matches space"
4586555d674dSSerapheim Dimitropoulos 		    " maps exactly)\n");
4587555d674dSSerapheim Dimitropoulos 	} else if (!dump_opt['L']) {
4588fa9e4066Sahrens 		(void) printf("block traversal size %llu != alloc %llu "
458982a0a985SVictor Latushkin 		    "(%s %lld)\n",
4590b24ab676SJeff Bonwick 		    (u_longlong_t)total_found,
4591b24ab676SJeff Bonwick 		    (u_longlong_t)total_alloc,
459282a0a985SVictor Latushkin 		    (dump_opt['L']) ? "unreachable" : "leaked",
4593b24ab676SJeff Bonwick 		    (longlong_t)(total_alloc - total_found));
45945d7b4d43SMatthew Ahrens 		leaks = B_TRUE;
4595fa9e4066Sahrens 	}
4596fa9e4066Sahrens 
4597fa9e4066Sahrens 	if (tzb->zb_count == 0)
4598fa9e4066Sahrens 		return (2);
4599fa9e4066Sahrens 
4600fa9e4066Sahrens 	(void) printf("\n");
4601663207adSDon Brady 	(void) printf("\t%-16s %14llu\n", "bp count:",
4602fa9e4066Sahrens 	    (u_longlong_t)tzb->zb_count);
4603663207adSDon Brady 	(void) printf("\t%-16s %14llu\n", "ganged count:",
4604d5ee8a13SMatthew Ahrens 	    (longlong_t)tzb->zb_gangs);
4605663207adSDon Brady 	(void) printf("\t%-16s %14llu      avg: %6llu\n", "bp logical:",
4606fa9e4066Sahrens 	    (u_longlong_t)tzb->zb_lsize,
4607fa9e4066Sahrens 	    (u_longlong_t)(tzb->zb_lsize / tzb->zb_count));
4608663207adSDon Brady 	(void) printf("\t%-16s %14llu      avg: %6llu     compression: %6.2f\n",
4609663207adSDon Brady 	    "bp physical:", (u_longlong_t)tzb->zb_psize,
4610fa9e4066Sahrens 	    (u_longlong_t)(tzb->zb_psize / tzb->zb_count),
4611fa9e4066Sahrens 	    (double)tzb->zb_lsize / tzb->zb_psize);
4612663207adSDon Brady 	(void) printf("\t%-16s %14llu      avg: %6llu     compression: %6.2f\n",
4613663207adSDon Brady 	    "bp allocated:", (u_longlong_t)tzb->zb_asize,
4614fa9e4066Sahrens 	    (u_longlong_t)(tzb->zb_asize / tzb->zb_count),
4615fa9e4066Sahrens 	    (double)tzb->zb_lsize / tzb->zb_asize);
4616663207adSDon Brady 	(void) printf("\t%-16s %14llu    ref>1: %6llu   deduplication: %6.2f\n",
4617663207adSDon Brady 	    "bp deduped:", (u_longlong_t)zcb.zcb_dedup_asize,
4618b24ab676SJeff Bonwick 	    (u_longlong_t)zcb.zcb_dedup_blocks,
4619b24ab676SJeff Bonwick 	    (double)zcb.zcb_dedup_asize / tzb->zb_asize + 1.0);
4620663207adSDon Brady 	(void) printf("\t%-16s %14llu     used: %5.2f%%\n", "Normal class:",
4621b24ab676SJeff Bonwick 	    (u_longlong_t)norm_alloc, 100.0 * norm_alloc / norm_space);
4622fa9e4066Sahrens 
4623663207adSDon Brady 	if (spa_special_class(spa)->mc_rotor != NULL) {
4624663207adSDon Brady 		uint64_t alloc = metaslab_class_get_alloc(
4625663207adSDon Brady 		    spa_special_class(spa));
4626663207adSDon Brady 		uint64_t space = metaslab_class_get_space(
4627663207adSDon Brady 		    spa_special_class(spa));
4628663207adSDon Brady 
4629663207adSDon Brady 		(void) printf("\t%-16s %14llu     used: %5.2f%%\n",
4630663207adSDon Brady 		    "Special class", (u_longlong_t)alloc,
4631663207adSDon Brady 		    100.0 * alloc / space);
4632663207adSDon Brady 	}
4633663207adSDon Brady 
4634663207adSDon Brady 	if (spa_dedup_class(spa)->mc_rotor != NULL) {
4635663207adSDon Brady 		uint64_t alloc = metaslab_class_get_alloc(
4636663207adSDon Brady 		    spa_dedup_class(spa));
4637663207adSDon Brady 		uint64_t space = metaslab_class_get_space(
4638663207adSDon Brady 		    spa_dedup_class(spa));
4639663207adSDon Brady 
4640663207adSDon Brady 		(void) printf("\t%-16s %14llu     used: %5.2f%%\n",
4641663207adSDon Brady 		    "Dedup class", (u_longlong_t)alloc,
4642663207adSDon Brady 		    100.0 * alloc / space);
4643663207adSDon Brady 	}
4644663207adSDon Brady 
46455d7b4d43SMatthew Ahrens 	for (bp_embedded_type_t i = 0; i < NUM_BP_EMBEDDED_TYPES; i++) {
46465d7b4d43SMatthew Ahrens 		if (zcb.zcb_embedded_blocks[i] == 0)
46475d7b4d43SMatthew Ahrens 			continue;
46485d7b4d43SMatthew Ahrens 		(void) printf("\n");
46495d7b4d43SMatthew Ahrens 		(void) printf("\tadditional, non-pointer bps of type %u: "
46505d7b4d43SMatthew Ahrens 		    "%10llu\n",
46515d7b4d43SMatthew Ahrens 		    i, (u_longlong_t)zcb.zcb_embedded_blocks[i]);
46525d7b4d43SMatthew Ahrens 
46535d7b4d43SMatthew Ahrens 		if (dump_opt['b'] >= 3) {
46545d7b4d43SMatthew Ahrens 			(void) printf("\t number of (compressed) bytes:  "
46555d7b4d43SMatthew Ahrens 			    "number of bps\n");
46565d7b4d43SMatthew Ahrens 			dump_histogram(zcb.zcb_embedded_histogram[i],
46575d7b4d43SMatthew Ahrens 			    sizeof (zcb.zcb_embedded_histogram[i]) /
46585d7b4d43SMatthew Ahrens 			    sizeof (zcb.zcb_embedded_histogram[i][0]), 0);
46595d7b4d43SMatthew Ahrens 		}
46605d7b4d43SMatthew Ahrens 	}
46615d7b4d43SMatthew Ahrens 
4662d5ee8a13SMatthew Ahrens 	if (tzb->zb_ditto_samevdev != 0) {
4663d5ee8a13SMatthew Ahrens 		(void) printf("\tDittoed blocks on same vdev: %llu\n",
4664d5ee8a13SMatthew Ahrens 		    (longlong_t)tzb->zb_ditto_samevdev);
4665d5ee8a13SMatthew Ahrens 	}
4666663207adSDon Brady 	if (tzb->zb_ditto_same_ms != 0) {
4667663207adSDon Brady 		(void) printf("\tDittoed blocks in same metaslab: %llu\n",
4668663207adSDon Brady 		    (longlong_t)tzb->zb_ditto_same_ms);
4669663207adSDon Brady 	}
4670d5ee8a13SMatthew Ahrens 
46715cabbc6bSPrashanth Sreenivasa 	for (uint64_t v = 0; v < spa->spa_root_vdev->vdev_children; v++) {
46725cabbc6bSPrashanth Sreenivasa 		vdev_t *vd = spa->spa_root_vdev->vdev_child[v];
46735cabbc6bSPrashanth Sreenivasa 		vdev_indirect_mapping_t *vim = vd->vdev_indirect_mapping;
46745cabbc6bSPrashanth Sreenivasa 
46755cabbc6bSPrashanth Sreenivasa 		if (vim == NULL) {
46765cabbc6bSPrashanth Sreenivasa 			continue;
46775cabbc6bSPrashanth Sreenivasa 		}
46785cabbc6bSPrashanth Sreenivasa 
46795cabbc6bSPrashanth Sreenivasa 		char mem[32];
46805cabbc6bSPrashanth Sreenivasa 		zdb_nicenum(vdev_indirect_mapping_num_entries(vim),
46815cabbc6bSPrashanth Sreenivasa 		    mem, vdev_indirect_mapping_size(vim));
46825cabbc6bSPrashanth Sreenivasa 
46835cabbc6bSPrashanth Sreenivasa 		(void) printf("\tindirect vdev id %llu has %llu segments "
46845cabbc6bSPrashanth Sreenivasa 		    "(%s in memory)\n",
46855cabbc6bSPrashanth Sreenivasa 		    (longlong_t)vd->vdev_id,
46865cabbc6bSPrashanth Sreenivasa 		    (longlong_t)vdev_indirect_mapping_num_entries(vim), mem);
46875cabbc6bSPrashanth Sreenivasa 	}
46885cabbc6bSPrashanth Sreenivasa 
4689fa9e4066Sahrens 	if (dump_opt['b'] >= 2) {
4690fa9e4066Sahrens 		int l, t, level;
4691fa9e4066Sahrens 		(void) printf("\nBlocks\tLSIZE\tPSIZE\tASIZE"
4692fa9e4066Sahrens 		    "\t  avg\t comp\t%%Total\tType\n");
4693fa9e4066Sahrens 
4694b24ab676SJeff Bonwick 		for (t = 0; t <= ZDB_OT_TOTAL; t++) {
46953f9d6ad7SLin Ling 			char csize[32], lsize[32], psize[32], asize[32];
4696d5ee8a13SMatthew Ahrens 			char avg[32], gang[32];
46973f7978d0SAlan Somers 			const char *typename;
4698fa9e4066Sahrens 
46990a055120SJason King 			/* make sure nicenum has enough space */
47000a055120SJason King 			CTASSERT(sizeof (csize) >= NN_NUMBUF_SZ);
47010a055120SJason King 			CTASSERT(sizeof (lsize) >= NN_NUMBUF_SZ);
47020a055120SJason King 			CTASSERT(sizeof (psize) >= NN_NUMBUF_SZ);
47030a055120SJason King 			CTASSERT(sizeof (asize) >= NN_NUMBUF_SZ);
47040a055120SJason King 			CTASSERT(sizeof (avg) >= NN_NUMBUF_SZ);
47050a055120SJason King 			CTASSERT(sizeof (gang) >= NN_NUMBUF_SZ);
47060a055120SJason King 
4707b24ab676SJeff Bonwick 			if (t < DMU_OT_NUMTYPES)
4708b24ab676SJeff Bonwick 				typename = dmu_ot[t].ot_name;
4709b24ab676SJeff Bonwick 			else
4710b24ab676SJeff Bonwick 				typename = zdb_ot_extname[t - DMU_OT_NUMTYPES];
4711fa9e4066Sahrens 
4712fa9e4066Sahrens 			if (zcb.zcb_type[ZB_TOTAL][t].zb_asize == 0) {
4713fa9e4066Sahrens 				(void) printf("%6s\t%5s\t%5s\t%5s"
4714fa9e4066Sahrens 				    "\t%5s\t%5s\t%6s\t%s\n",
4715fa9e4066Sahrens 				    "-",
4716fa9e4066Sahrens 				    "-",
4717fa9e4066Sahrens 				    "-",
4718fa9e4066Sahrens 				    "-",
4719fa9e4066Sahrens 				    "-",
4720fa9e4066Sahrens 				    "-",
4721fa9e4066Sahrens 				    "-",
4722fa9e4066Sahrens 				    typename);
4723fa9e4066Sahrens 				continue;
4724fa9e4066Sahrens 			}
4725fa9e4066Sahrens 
4726fa9e4066Sahrens 			for (l = ZB_TOTAL - 1; l >= -1; l--) {
4727fa9e4066Sahrens 				level = (l == -1 ? ZB_TOTAL : l);
4728fa9e4066Sahrens 				zb = &zcb.zcb_type[level][t];
4729fa9e4066Sahrens 
4730fa9e4066Sahrens 				if (zb->zb_asize == 0)
4731fa9e4066Sahrens 					continue;
4732fa9e4066Sahrens 
4733fa9e4066Sahrens 				if (dump_opt['b'] < 3 && level != ZB_TOTAL)
4734fa9e4066Sahrens 					continue;
4735fa9e4066Sahrens 
4736fa9e4066Sahrens 				if (level == 0 && zb->zb_asize ==
4737fa9e4066Sahrens 				    zcb.zcb_type[ZB_TOTAL][t].zb_asize)
4738fa9e4066Sahrens 					continue;
4739fa9e4066Sahrens 
47400a055120SJason King 				zdb_nicenum(zb->zb_count, csize,
47410a055120SJason King 				    sizeof (csize));
47420a055120SJason King 				zdb_nicenum(zb->zb_lsize, lsize,
47430a055120SJason King 				    sizeof (lsize));
47440a055120SJason King 				zdb_nicenum(zb->zb_psize, psize,
47450a055120SJason King 				    sizeof (psize));
47460a055120SJason King 				zdb_nicenum(zb->zb_asize, asize,
47470a055120SJason King 				    sizeof (asize));
47480a055120SJason King 				zdb_nicenum(zb->zb_asize / zb->zb_count, avg,
47490a055120SJason King 				    sizeof (avg));
47500a055120SJason King 				zdb_nicenum(zb->zb_gangs, gang, sizeof (gang));
4751fa9e4066Sahrens 
4752fa9e4066Sahrens 				(void) printf("%6s\t%5s\t%5s\t%5s\t%5s"
4753fa9e4066Sahrens 				    "\t%5.2f\t%6.2f\t",
4754fa9e4066Sahrens 				    csize, lsize, psize, asize, avg,
4755fa9e4066Sahrens 				    (double)zb->zb_lsize / zb->zb_psize,
4756fa9e4066Sahrens 				    100.0 * zb->zb_asize / tzb->zb_asize);
4757fa9e4066Sahrens 
4758fa9e4066Sahrens 				if (level == ZB_TOTAL)
4759fa9e4066Sahrens 					(void) printf("%s\n", typename);
4760fa9e4066Sahrens 				else
4761fa9e4066Sahrens 					(void) printf("    L%d %s\n",
4762fa9e4066Sahrens 					    level, typename);
4763490d05b9SMatthew Ahrens 
4764d5ee8a13SMatthew Ahrens 				if (dump_opt['b'] >= 3 && zb->zb_gangs > 0) {
4765d5ee8a13SMatthew Ahrens 					(void) printf("\t number of ganged "
4766d5ee8a13SMatthew Ahrens 					    "blocks: %s\n", gang);
4767d5ee8a13SMatthew Ahrens 				}
4768d5ee8a13SMatthew Ahrens 
4769490d05b9SMatthew Ahrens 				if (dump_opt['b'] >= 4) {
4770490d05b9SMatthew Ahrens 					(void) printf("psize "
4771490d05b9SMatthew Ahrens 					    "(in 512-byte sectors): "
4772490d05b9SMatthew Ahrens 					    "number of blocks\n");
4773490d05b9SMatthew Ahrens 					dump_histogram(zb->zb_psize_histogram,
47740713e232SGeorge Wilson 					    PSIZE_HISTO_SIZE, 0);
4775490d05b9SMatthew Ahrens 				}
4776fa9e4066Sahrens 			}
4777fa9e4066Sahrens 		}
4778fa9e4066Sahrens 	}
4779fa9e4066Sahrens 
4780fa9e4066Sahrens 	(void) printf("\n");
4781fa9e4066Sahrens 
4782fa9e4066Sahrens 	if (leaks)
4783fa9e4066Sahrens 		return (2);
4784fa9e4066Sahrens 
4785fa9e4066Sahrens 	if (zcb.zcb_haderrors)
4786fa9e4066Sahrens 		return (3);
4787fa9e4066Sahrens 
4788fa9e4066Sahrens 	return (0);
4789fa9e4066Sahrens }
4790fa9e4066Sahrens 
4791b24ab676SJeff Bonwick typedef struct zdb_ddt_entry {
4792b24ab676SJeff Bonwick 	ddt_key_t	zdde_key;
4793b24ab676SJeff Bonwick 	uint64_t	zdde_ref_blocks;
4794b24ab676SJeff Bonwick 	uint64_t	zdde_ref_lsize;
4795b24ab676SJeff Bonwick 	uint64_t	zdde_ref_psize;
4796b24ab676SJeff Bonwick 	uint64_t	zdde_ref_dsize;
4797b24ab676SJeff Bonwick 	avl_node_t	zdde_node;
4798b24ab676SJeff Bonwick } zdb_ddt_entry_t;
4799b24ab676SJeff Bonwick 
4800b24ab676SJeff Bonwick /* ARGSUSED */
4801b24ab676SJeff Bonwick static int
4802b24ab676SJeff Bonwick zdb_ddt_add_cb(spa_t *spa, zilog_t *zilog, const blkptr_t *bp,
48037802d7bfSMatthew Ahrens     const zbookmark_phys_t *zb, const dnode_phys_t *dnp, void *arg)
4804b24ab676SJeff Bonwick {
4805b24ab676SJeff Bonwick 	avl_tree_t *t = arg;
4806b24ab676SJeff Bonwick 	avl_index_t where;
4807b24ab676SJeff Bonwick 	zdb_ddt_entry_t *zdde, zdde_search;
4808b24ab676SJeff Bonwick 
4809a2cdcdd2SPaul Dagnelie 	if (bp == NULL || BP_IS_HOLE(bp) || BP_IS_EMBEDDED(bp))
4810b24ab676SJeff Bonwick 		return (0);
4811b24ab676SJeff Bonwick 
4812b24ab676SJeff Bonwick 	if (dump_opt['S'] > 1 && zb->zb_level == ZB_ROOT_LEVEL) {
4813b24ab676SJeff Bonwick 		(void) printf("traversing objset %llu, %llu objects, "
4814b24ab676SJeff Bonwick 		    "%lu blocks so far\n",
4815b24ab676SJeff Bonwick 		    (u_longlong_t)zb->zb_objset,
48165d7b4d43SMatthew Ahrens 		    (u_longlong_t)BP_GET_FILL(bp),
4817b24ab676SJeff Bonwick 		    avl_numnodes(t));
4818b24ab676SJeff Bonwick 	}
4819b24ab676SJeff Bonwick 
4820bbfd46c4SJeff Bonwick 	if (BP_IS_HOLE(bp) || BP_GET_CHECKSUM(bp) == ZIO_CHECKSUM_OFF ||
4821ad135b5dSChristopher Siden 	    BP_GET_LEVEL(bp) > 0 || DMU_OT_IS_METADATA(BP_GET_TYPE(bp)))
4822b24ab676SJeff Bonwick 		return (0);
4823b24ab676SJeff Bonwick 
4824b24ab676SJeff Bonwick 	ddt_key_fill(&zdde_search.zdde_key, bp);
4825b24ab676SJeff Bonwick 
4826b24ab676SJeff Bonwick 	zdde = avl_find(t, &zdde_search, &where);
4827b24ab676SJeff Bonwick 
4828b24ab676SJeff Bonwick 	if (zdde == NULL) {
4829b24ab676SJeff Bonwick 		zdde = umem_zalloc(sizeof (*zdde), UMEM_NOFAIL);
4830b24ab676SJeff Bonwick 		zdde->zdde_key = zdde_search.zdde_key;
4831b24ab676SJeff Bonwick 		avl_insert(t, zdde, where);
4832b24ab676SJeff Bonwick 	}
4833b24ab676SJeff Bonwick 
4834b24ab676SJeff Bonwick 	zdde->zdde_ref_blocks += 1;
4835b24ab676SJeff Bonwick 	zdde->zdde_ref_lsize += BP_GET_LSIZE(bp);
4836b24ab676SJeff Bonwick 	zdde->zdde_ref_psize += BP_GET_PSIZE(bp);
4837b24ab676SJeff Bonwick 	zdde->zdde_ref_dsize += bp_get_dsize_sync(spa, bp);
4838b24ab676SJeff Bonwick 
4839b24ab676SJeff Bonwick 	return (0);
4840b24ab676SJeff Bonwick }
4841b24ab676SJeff Bonwick 
4842b24ab676SJeff Bonwick static void
4843b24ab676SJeff Bonwick dump_simulated_ddt(spa_t *spa)
4844b24ab676SJeff Bonwick {
4845b24ab676SJeff Bonwick 	avl_tree_t t;
4846b24ab676SJeff Bonwick 	void *cookie = NULL;
4847b24ab676SJeff Bonwick 	zdb_ddt_entry_t *zdde;
48483f7978d0SAlan Somers 	ddt_histogram_t ddh_total;
48493f7978d0SAlan Somers 	ddt_stat_t dds_total;
4850b24ab676SJeff Bonwick 
48513f7978d0SAlan Somers 	bzero(&ddh_total, sizeof (ddh_total));
48523f7978d0SAlan Somers 	bzero(&dds_total, sizeof (dds_total));
4853b24ab676SJeff Bonwick 	avl_create(&t, ddt_entry_compare,
4854b24ab676SJeff Bonwick 	    sizeof (zdb_ddt_entry_t), offsetof(zdb_ddt_entry_t, zdde_node));
4855b24ab676SJeff Bonwick 
4856b24ab676SJeff Bonwick 	spa_config_enter(spa, SCL_CONFIG, FTAG, RW_READER);
4857b24ab676SJeff Bonwick 
4858eb633035STom Caputi 	(void) traverse_pool(spa, 0, TRAVERSE_PRE | TRAVERSE_PREFETCH_METADATA |
4859eb633035STom Caputi 	    TRAVERSE_NO_DECRYPT, zdb_ddt_add_cb, &t);
4860b24ab676SJeff Bonwick 
4861b24ab676SJeff Bonwick 	spa_config_exit(spa, SCL_CONFIG, FTAG);
4862b24ab676SJeff Bonwick 
4863b24ab676SJeff Bonwick 	while ((zdde = avl_destroy_nodes(&t, &cookie)) != NULL) {
4864b24ab676SJeff Bonwick 		ddt_stat_t dds;
4865b24ab676SJeff Bonwick 		uint64_t refcnt = zdde->zdde_ref_blocks;
4866b24ab676SJeff Bonwick 		ASSERT(refcnt != 0);
4867b24ab676SJeff Bonwick 
4868b24ab676SJeff Bonwick 		dds.dds_blocks = zdde->zdde_ref_blocks / refcnt;
4869b24ab676SJeff Bonwick 		dds.dds_lsize = zdde->zdde_ref_lsize / refcnt;
4870b24ab676SJeff Bonwick 		dds.dds_psize = zdde->zdde_ref_psize / refcnt;
4871b24ab676SJeff Bonwick 		dds.dds_dsize = zdde->zdde_ref_dsize / refcnt;
4872b24ab676SJeff Bonwick 
4873b24ab676SJeff Bonwick 		dds.dds_ref_blocks = zdde->zdde_ref_blocks;
4874b24ab676SJeff Bonwick 		dds.dds_ref_lsize = zdde->zdde_ref_lsize;
4875b24ab676SJeff Bonwick 		dds.dds_ref_psize = zdde->zdde_ref_psize;
4876b24ab676SJeff Bonwick 		dds.dds_ref_dsize = zdde->zdde_ref_dsize;
4877b24ab676SJeff Bonwick 
4878bf16b11eSMatthew Ahrens 		ddt_stat_add(&ddh_total.ddh_stat[highbit64(refcnt) - 1],
4879bf16b11eSMatthew Ahrens 		    &dds, 0);
4880b24ab676SJeff Bonwick 
4881b24ab676SJeff Bonwick 		umem_free(zdde, sizeof (*zdde));
4882b24ab676SJeff Bonwick 	}
4883b24ab676SJeff Bonwick 
4884b24ab676SJeff Bonwick 	avl_destroy(&t);
4885b24ab676SJeff Bonwick 
4886b24ab676SJeff Bonwick 	ddt_histogram_stat(&dds_total, &ddh_total);
4887b24ab676SJeff Bonwick 
4888b24ab676SJeff Bonwick 	(void) printf("Simulated DDT histogram:\n");
4889b24ab676SJeff Bonwick 
48909eb19f4dSGeorge Wilson 	zpool_dump_ddt(&dds_total, &ddh_total);
4891b24ab676SJeff Bonwick 
4892b24ab676SJeff Bonwick 	dump_dedup_ratio(&dds_total);
4893b24ab676SJeff Bonwick }
4894b24ab676SJeff Bonwick 
48955cabbc6bSPrashanth Sreenivasa static int
48965cabbc6bSPrashanth Sreenivasa verify_device_removal_feature_counts(spa_t *spa)
48975cabbc6bSPrashanth Sreenivasa {
48985cabbc6bSPrashanth Sreenivasa 	uint64_t dr_feature_refcount = 0;
48995cabbc6bSPrashanth Sreenivasa 	uint64_t oc_feature_refcount = 0;
49005cabbc6bSPrashanth Sreenivasa 	uint64_t indirect_vdev_count = 0;
49015cabbc6bSPrashanth Sreenivasa 	uint64_t precise_vdev_count = 0;
49025cabbc6bSPrashanth Sreenivasa 	uint64_t obsolete_counts_object_count = 0;
49035cabbc6bSPrashanth Sreenivasa 	uint64_t obsolete_sm_count = 0;
49045cabbc6bSPrashanth Sreenivasa 	uint64_t obsolete_counts_count = 0;
49055cabbc6bSPrashanth Sreenivasa 	uint64_t scip_count = 0;
49065cabbc6bSPrashanth Sreenivasa 	uint64_t obsolete_bpobj_count = 0;
49075cabbc6bSPrashanth Sreenivasa 	int ret = 0;
49085cabbc6bSPrashanth Sreenivasa 
49095cabbc6bSPrashanth Sreenivasa 	spa_condensing_indirect_phys_t *scip =
49105cabbc6bSPrashanth Sreenivasa 	    &spa->spa_condensing_indirect_phys;
49115cabbc6bSPrashanth Sreenivasa 	if (scip->scip_next_mapping_object != 0) {
49125cabbc6bSPrashanth Sreenivasa 		vdev_t *vd = spa->spa_root_vdev->vdev_child[scip->scip_vdev];
49135cabbc6bSPrashanth Sreenivasa 		ASSERT(scip->scip_prev_obsolete_sm_object != 0);
49145cabbc6bSPrashanth Sreenivasa 		ASSERT3P(vd->vdev_ops, ==, &vdev_indirect_ops);
49155cabbc6bSPrashanth Sreenivasa 
49165cabbc6bSPrashanth Sreenivasa 		(void) printf("Condensing indirect vdev %llu: new mapping "
49175cabbc6bSPrashanth Sreenivasa 		    "object %llu, prev obsolete sm %llu\n",
49185cabbc6bSPrashanth Sreenivasa 		    (u_longlong_t)scip->scip_vdev,
49195cabbc6bSPrashanth Sreenivasa 		    (u_longlong_t)scip->scip_next_mapping_object,
49205cabbc6bSPrashanth Sreenivasa 		    (u_longlong_t)scip->scip_prev_obsolete_sm_object);
49215cabbc6bSPrashanth Sreenivasa 		if (scip->scip_prev_obsolete_sm_object != 0) {
49225cabbc6bSPrashanth Sreenivasa 			space_map_t *prev_obsolete_sm = NULL;
49235cabbc6bSPrashanth Sreenivasa 			VERIFY0(space_map_open(&prev_obsolete_sm,
49245cabbc6bSPrashanth Sreenivasa 			    spa->spa_meta_objset,
49255cabbc6bSPrashanth Sreenivasa 			    scip->scip_prev_obsolete_sm_object,
49265cabbc6bSPrashanth Sreenivasa 			    0, vd->vdev_asize, 0));
49275cabbc6bSPrashanth Sreenivasa 			dump_spacemap(spa->spa_meta_objset, prev_obsolete_sm);
49285cabbc6bSPrashanth Sreenivasa 			(void) printf("\n");
49295cabbc6bSPrashanth Sreenivasa 			space_map_close(prev_obsolete_sm);
49305cabbc6bSPrashanth Sreenivasa 		}
49315cabbc6bSPrashanth Sreenivasa 
49325cabbc6bSPrashanth Sreenivasa 		scip_count += 2;
49335cabbc6bSPrashanth Sreenivasa 	}
49345cabbc6bSPrashanth Sreenivasa 
49355cabbc6bSPrashanth Sreenivasa 	for (uint64_t i = 0; i < spa->spa_root_vdev->vdev_children; i++) {
49365cabbc6bSPrashanth Sreenivasa 		vdev_t *vd = spa->spa_root_vdev->vdev_child[i];
49375cabbc6bSPrashanth Sreenivasa 		vdev_indirect_config_t *vic = &vd->vdev_indirect_config;
49385cabbc6bSPrashanth Sreenivasa 
49395cabbc6bSPrashanth Sreenivasa 		if (vic->vic_mapping_object != 0) {
49405cabbc6bSPrashanth Sreenivasa 			ASSERT(vd->vdev_ops == &vdev_indirect_ops ||
49415cabbc6bSPrashanth Sreenivasa 			    vd->vdev_removing);
49425cabbc6bSPrashanth Sreenivasa 			indirect_vdev_count++;
49435cabbc6bSPrashanth Sreenivasa 
49445cabbc6bSPrashanth Sreenivasa 			if (vd->vdev_indirect_mapping->vim_havecounts) {
49455cabbc6bSPrashanth Sreenivasa 				obsolete_counts_count++;
49465cabbc6bSPrashanth Sreenivasa 			}
49475cabbc6bSPrashanth Sreenivasa 		}
49485cabbc6bSPrashanth Sreenivasa 		if (vdev_obsolete_counts_are_precise(vd)) {
49495cabbc6bSPrashanth Sreenivasa 			ASSERT(vic->vic_mapping_object != 0);
49505cabbc6bSPrashanth Sreenivasa 			precise_vdev_count++;
49515cabbc6bSPrashanth Sreenivasa 		}
49525cabbc6bSPrashanth Sreenivasa 		if (vdev_obsolete_sm_object(vd) != 0) {
49535cabbc6bSPrashanth Sreenivasa 			ASSERT(vic->vic_mapping_object != 0);
49545cabbc6bSPrashanth Sreenivasa 			obsolete_sm_count++;
49555cabbc6bSPrashanth Sreenivasa 		}
49565cabbc6bSPrashanth Sreenivasa 	}
49575cabbc6bSPrashanth Sreenivasa 
49585cabbc6bSPrashanth Sreenivasa 	(void) feature_get_refcount(spa,
49595cabbc6bSPrashanth Sreenivasa 	    &spa_feature_table[SPA_FEATURE_DEVICE_REMOVAL],
49605cabbc6bSPrashanth Sreenivasa 	    &dr_feature_refcount);
49615cabbc6bSPrashanth Sreenivasa 	(void) feature_get_refcount(spa,
49625cabbc6bSPrashanth Sreenivasa 	    &spa_feature_table[SPA_FEATURE_OBSOLETE_COUNTS],
49635cabbc6bSPrashanth Sreenivasa 	    &oc_feature_refcount);
49645cabbc6bSPrashanth Sreenivasa 
49655cabbc6bSPrashanth Sreenivasa 	if (dr_feature_refcount != indirect_vdev_count) {
49665cabbc6bSPrashanth Sreenivasa 		ret = 1;
49675cabbc6bSPrashanth Sreenivasa 		(void) printf("Number of indirect vdevs (%llu) " \
49685cabbc6bSPrashanth Sreenivasa 		    "does not match feature count (%llu)\n",
49695cabbc6bSPrashanth Sreenivasa 		    (u_longlong_t)indirect_vdev_count,
49705cabbc6bSPrashanth Sreenivasa 		    (u_longlong_t)dr_feature_refcount);
49715cabbc6bSPrashanth Sreenivasa 	} else {
49725cabbc6bSPrashanth Sreenivasa 		(void) printf("Verified device_removal feature refcount " \
49735cabbc6bSPrashanth Sreenivasa 		    "of %llu is correct\n",
49745cabbc6bSPrashanth Sreenivasa 		    (u_longlong_t)dr_feature_refcount);
49755cabbc6bSPrashanth Sreenivasa 	}
49765cabbc6bSPrashanth Sreenivasa 
49775cabbc6bSPrashanth Sreenivasa 	if (zap_contains(spa_meta_objset(spa), DMU_POOL_DIRECTORY_OBJECT,
49785cabbc6bSPrashanth Sreenivasa 	    DMU_POOL_OBSOLETE_BPOBJ) == 0) {
49795cabbc6bSPrashanth Sreenivasa 		obsolete_bpobj_count++;
49805cabbc6bSPrashanth Sreenivasa 	}
49815cabbc6bSPrashanth Sreenivasa 
49825cabbc6bSPrashanth Sreenivasa 
49835cabbc6bSPrashanth Sreenivasa 	obsolete_counts_object_count = precise_vdev_count;
49845cabbc6bSPrashanth Sreenivasa 	obsolete_counts_object_count += obsolete_sm_count;
49855cabbc6bSPrashanth Sreenivasa 	obsolete_counts_object_count += obsolete_counts_count;
49865cabbc6bSPrashanth Sreenivasa 	obsolete_counts_object_count += scip_count;
49875cabbc6bSPrashanth Sreenivasa 	obsolete_counts_object_count += obsolete_bpobj_count;
49885cabbc6bSPrashanth Sreenivasa 	obsolete_counts_object_count += remap_deadlist_count;
49895cabbc6bSPrashanth Sreenivasa 
49905cabbc6bSPrashanth Sreenivasa 	if (oc_feature_refcount != obsolete_counts_object_count) {
49915cabbc6bSPrashanth Sreenivasa 		ret = 1;
49925cabbc6bSPrashanth Sreenivasa 		(void) printf("Number of obsolete counts objects (%llu) " \
49935cabbc6bSPrashanth Sreenivasa 		    "does not match feature count (%llu)\n",
49945cabbc6bSPrashanth Sreenivasa 		    (u_longlong_t)obsolete_counts_object_count,
49955cabbc6bSPrashanth Sreenivasa 		    (u_longlong_t)oc_feature_refcount);
49965cabbc6bSPrashanth Sreenivasa 		(void) printf("pv:%llu os:%llu oc:%llu sc:%llu "
49975cabbc6bSPrashanth Sreenivasa 		    "ob:%llu rd:%llu\n",
49985cabbc6bSPrashanth Sreenivasa 		    (u_longlong_t)precise_vdev_count,
49995cabbc6bSPrashanth Sreenivasa 		    (u_longlong_t)obsolete_sm_count,
50005cabbc6bSPrashanth Sreenivasa 		    (u_longlong_t)obsolete_counts_count,
50015cabbc6bSPrashanth Sreenivasa 		    (u_longlong_t)scip_count,
50025cabbc6bSPrashanth Sreenivasa 		    (u_longlong_t)obsolete_bpobj_count,
50035cabbc6bSPrashanth Sreenivasa 		    (u_longlong_t)remap_deadlist_count);
50045cabbc6bSPrashanth Sreenivasa 	} else {
50055cabbc6bSPrashanth Sreenivasa 		(void) printf("Verified indirect_refcount feature refcount " \
50065cabbc6bSPrashanth Sreenivasa 		    "of %llu is correct\n",
50075cabbc6bSPrashanth Sreenivasa 		    (u_longlong_t)oc_feature_refcount);
50085cabbc6bSPrashanth Sreenivasa 	}
50095cabbc6bSPrashanth Sreenivasa 	return (ret);
50105cabbc6bSPrashanth Sreenivasa }
50115cabbc6bSPrashanth Sreenivasa 
5012e0f1c0afSOlaf Faaland static void
5013e0f1c0afSOlaf Faaland zdb_set_skip_mmp(char *target)
5014e0f1c0afSOlaf Faaland {
5015e0f1c0afSOlaf Faaland 	spa_t *spa;
5016e0f1c0afSOlaf Faaland 
5017e0f1c0afSOlaf Faaland 	/*
5018e0f1c0afSOlaf Faaland 	 * Disable the activity check to allow examination of
5019e0f1c0afSOlaf Faaland 	 * active pools.
5020e0f1c0afSOlaf Faaland 	 */
5021e0f1c0afSOlaf Faaland 	mutex_enter(&spa_namespace_lock);
5022e0f1c0afSOlaf Faaland 	if ((spa = spa_lookup(target)) != NULL) {
5023e0f1c0afSOlaf Faaland 		spa->spa_import_flags |= ZFS_IMPORT_SKIP_MMP;
5024e0f1c0afSOlaf Faaland 	}
5025e0f1c0afSOlaf Faaland 	mutex_exit(&spa_namespace_lock);
5026e0f1c0afSOlaf Faaland }
5027e0f1c0afSOlaf Faaland 
502886714001SSerapheim Dimitropoulos #define	BOGUS_SUFFIX "_CHECKPOINTED_UNIVERSE"
502986714001SSerapheim Dimitropoulos /*
503086714001SSerapheim Dimitropoulos  * Import the checkpointed state of the pool specified by the target
503186714001SSerapheim Dimitropoulos  * parameter as readonly. The function also accepts a pool config
503286714001SSerapheim Dimitropoulos  * as an optional parameter, else it attempts to infer the config by
503386714001SSerapheim Dimitropoulos  * the name of the target pool.
503486714001SSerapheim Dimitropoulos  *
503586714001SSerapheim Dimitropoulos  * Note that the checkpointed state's pool name will be the name of
503686714001SSerapheim Dimitropoulos  * the original pool with the above suffix appened to it. In addition,
503786714001SSerapheim Dimitropoulos  * if the target is not a pool name (e.g. a path to a dataset) then
503886714001SSerapheim Dimitropoulos  * the new_path parameter is populated with the updated path to
503986714001SSerapheim Dimitropoulos  * reflect the fact that we are looking into the checkpointed state.
504086714001SSerapheim Dimitropoulos  *
504186714001SSerapheim Dimitropoulos  * The function returns a newly-allocated copy of the name of the
504286714001SSerapheim Dimitropoulos  * pool containing the checkpointed state. When this copy is no
504386714001SSerapheim Dimitropoulos  * longer needed it should be freed with free(3C). Same thing
504486714001SSerapheim Dimitropoulos  * applies to the new_path parameter if allocated.
504586714001SSerapheim Dimitropoulos  */
504686714001SSerapheim Dimitropoulos static char *
504786714001SSerapheim Dimitropoulos import_checkpointed_state(char *target, nvlist_t *cfg, char **new_path)
504886714001SSerapheim Dimitropoulos {
504986714001SSerapheim Dimitropoulos 	int error = 0;
505086714001SSerapheim Dimitropoulos 	char *poolname, *bogus_name;
505186714001SSerapheim Dimitropoulos 
505286714001SSerapheim Dimitropoulos 	/* If the target is not a pool, the extract the pool name */
505386714001SSerapheim Dimitropoulos 	char *path_start = strchr(target, '/');
505486714001SSerapheim Dimitropoulos 	if (path_start != NULL) {
505586714001SSerapheim Dimitropoulos 		size_t poolname_len = path_start - target;
505686714001SSerapheim Dimitropoulos 		poolname = strndup(target, poolname_len);
505786714001SSerapheim Dimitropoulos 	} else {
505886714001SSerapheim Dimitropoulos 		poolname = target;
505986714001SSerapheim Dimitropoulos 	}
506086714001SSerapheim Dimitropoulos 
506186714001SSerapheim Dimitropoulos 	if (cfg == NULL) {
5062e0f1c0afSOlaf Faaland 		zdb_set_skip_mmp(poolname);
506386714001SSerapheim Dimitropoulos 		error = spa_get_stats(poolname, &cfg, NULL, 0);
506486714001SSerapheim Dimitropoulos 		if (error != 0) {
506586714001SSerapheim Dimitropoulos 			fatal("Tried to read config of pool \"%s\" but "
506686714001SSerapheim Dimitropoulos 			    "spa_get_stats() failed with error %d\n",
506786714001SSerapheim Dimitropoulos 			    poolname, error);
506886714001SSerapheim Dimitropoulos 		}
506986714001SSerapheim Dimitropoulos 	}
507086714001SSerapheim Dimitropoulos 
507186714001SSerapheim Dimitropoulos 	(void) asprintf(&bogus_name, "%s%s", poolname, BOGUS_SUFFIX);
507286714001SSerapheim Dimitropoulos 	fnvlist_add_string(cfg, ZPOOL_CONFIG_POOL_NAME, bogus_name);
507386714001SSerapheim Dimitropoulos 
507486714001SSerapheim Dimitropoulos 	error = spa_import(bogus_name, cfg, NULL,
5075e0f1c0afSOlaf Faaland 	    ZFS_IMPORT_MISSING_LOG | ZFS_IMPORT_CHECKPOINT |
5076e0f1c0afSOlaf Faaland 	    ZFS_IMPORT_SKIP_MMP);
507786714001SSerapheim Dimitropoulos 	if (error != 0) {
507886714001SSerapheim Dimitropoulos 		fatal("Tried to import pool \"%s\" but spa_import() failed "
507986714001SSerapheim Dimitropoulos 		    "with error %d\n", bogus_name, error);
508086714001SSerapheim Dimitropoulos 	}
508186714001SSerapheim Dimitropoulos 
508286714001SSerapheim Dimitropoulos 	if (new_path != NULL && path_start != NULL)
508386714001SSerapheim Dimitropoulos 		(void) asprintf(new_path, "%s%s", bogus_name, path_start);
508486714001SSerapheim Dimitropoulos 
508586714001SSerapheim Dimitropoulos 	if (target != poolname)
508686714001SSerapheim Dimitropoulos 		free(poolname);
508786714001SSerapheim Dimitropoulos 
508886714001SSerapheim Dimitropoulos 	return (bogus_name);
508986714001SSerapheim Dimitropoulos }
509086714001SSerapheim Dimitropoulos 
509186714001SSerapheim Dimitropoulos typedef struct verify_checkpoint_sm_entry_cb_arg {
509286714001SSerapheim Dimitropoulos 	vdev_t *vcsec_vd;
509386714001SSerapheim Dimitropoulos 
509486714001SSerapheim Dimitropoulos 	/* the following fields are only used for printing progress */
509586714001SSerapheim Dimitropoulos 	uint64_t vcsec_entryid;
509686714001SSerapheim Dimitropoulos 	uint64_t vcsec_num_entries;
509786714001SSerapheim Dimitropoulos } verify_checkpoint_sm_entry_cb_arg_t;
509886714001SSerapheim Dimitropoulos 
509986714001SSerapheim Dimitropoulos #define	ENTRIES_PER_PROGRESS_UPDATE 10000
510086714001SSerapheim Dimitropoulos 
510186714001SSerapheim Dimitropoulos static int
510217f11284SSerapheim Dimitropoulos verify_checkpoint_sm_entry_cb(space_map_entry_t *sme, void *arg)
510386714001SSerapheim Dimitropoulos {
510486714001SSerapheim Dimitropoulos 	verify_checkpoint_sm_entry_cb_arg_t *vcsec = arg;
510586714001SSerapheim Dimitropoulos 	vdev_t *vd = vcsec->vcsec_vd;
510617f11284SSerapheim Dimitropoulos 	metaslab_t *ms = vd->vdev_ms[sme->sme_offset >> vd->vdev_ms_shift];
510717f11284SSerapheim Dimitropoulos 	uint64_t end = sme->sme_offset + sme->sme_run;
510886714001SSerapheim Dimitropoulos 
510917f11284SSerapheim Dimitropoulos 	ASSERT(sme->sme_type == SM_FREE);
511086714001SSerapheim Dimitropoulos 
511186714001SSerapheim Dimitropoulos 	if ((vcsec->vcsec_entryid % ENTRIES_PER_PROGRESS_UPDATE) == 0) {
511286714001SSerapheim Dimitropoulos 		(void) fprintf(stderr,
511386714001SSerapheim Dimitropoulos 		    "\rverifying vdev %llu, space map entry %llu of %llu ...",
511486714001SSerapheim Dimitropoulos 		    (longlong_t)vd->vdev_id,
511586714001SSerapheim Dimitropoulos 		    (longlong_t)vcsec->vcsec_entryid,
511686714001SSerapheim Dimitropoulos 		    (longlong_t)vcsec->vcsec_num_entries);
511786714001SSerapheim Dimitropoulos 	}
511886714001SSerapheim Dimitropoulos 	vcsec->vcsec_entryid++;
511986714001SSerapheim Dimitropoulos 
512086714001SSerapheim Dimitropoulos 	/*
512186714001SSerapheim Dimitropoulos 	 * See comment in checkpoint_sm_exclude_entry_cb()
512286714001SSerapheim Dimitropoulos 	 */
512317f11284SSerapheim Dimitropoulos 	VERIFY3U(sme->sme_offset, >=, ms->ms_start);
512486714001SSerapheim Dimitropoulos 	VERIFY3U(end, <=, ms->ms_start + ms->ms_size);
512586714001SSerapheim Dimitropoulos 
512686714001SSerapheim Dimitropoulos 	/*
512786714001SSerapheim Dimitropoulos 	 * The entries in the vdev_checkpoint_sm should be marked as
512886714001SSerapheim Dimitropoulos 	 * allocated in the checkpointed state of the pool, therefore
512986714001SSerapheim Dimitropoulos 	 * their respective ms_allocateable trees should not contain them.
513086714001SSerapheim Dimitropoulos 	 */
513186714001SSerapheim Dimitropoulos 	mutex_enter(&ms->ms_lock);
5132555d674dSSerapheim Dimitropoulos 	range_tree_verify_not_present(ms->ms_allocatable,
5133555d674dSSerapheim Dimitropoulos 	    sme->sme_offset, sme->sme_run);
513486714001SSerapheim Dimitropoulos 	mutex_exit(&ms->ms_lock);
513586714001SSerapheim Dimitropoulos 
513686714001SSerapheim Dimitropoulos 	return (0);
513786714001SSerapheim Dimitropoulos }
513886714001SSerapheim Dimitropoulos 
513986714001SSerapheim Dimitropoulos /*
514086714001SSerapheim Dimitropoulos  * Verify that all segments in the vdev_checkpoint_sm are allocated
514186714001SSerapheim Dimitropoulos  * according to the checkpoint's ms_sm (i.e. are not in the checkpoint's
514286714001SSerapheim Dimitropoulos  * ms_allocatable).
514386714001SSerapheim Dimitropoulos  *
514486714001SSerapheim Dimitropoulos  * Do so by comparing the checkpoint space maps (vdev_checkpoint_sm) of
514586714001SSerapheim Dimitropoulos  * each vdev in the current state of the pool to the metaslab space maps
514686714001SSerapheim Dimitropoulos  * (ms_sm) of the checkpointed state of the pool.
514786714001SSerapheim Dimitropoulos  *
514886714001SSerapheim Dimitropoulos  * Note that the function changes the state of the ms_allocatable
514986714001SSerapheim Dimitropoulos  * trees of the current spa_t. The entries of these ms_allocatable
515086714001SSerapheim Dimitropoulos  * trees are cleared out and then repopulated from with the free
515186714001SSerapheim Dimitropoulos  * entries of their respective ms_sm space maps.
515286714001SSerapheim Dimitropoulos  */
515386714001SSerapheim Dimitropoulos static void
515486714001SSerapheim Dimitropoulos verify_checkpoint_vdev_spacemaps(spa_t *checkpoint, spa_t *current)
515586714001SSerapheim Dimitropoulos {
515686714001SSerapheim Dimitropoulos 	vdev_t *ckpoint_rvd = checkpoint->spa_root_vdev;
515786714001SSerapheim Dimitropoulos 	vdev_t *current_rvd = current->spa_root_vdev;
515886714001SSerapheim Dimitropoulos 
515986714001SSerapheim Dimitropoulos 	load_concrete_ms_allocatable_trees(checkpoint, SM_FREE);
516086714001SSerapheim Dimitropoulos 
516186714001SSerapheim Dimitropoulos 	for (uint64_t c = 0; c < ckpoint_rvd->vdev_children; c++) {
516286714001SSerapheim Dimitropoulos 		vdev_t *ckpoint_vd = ckpoint_rvd->vdev_child[c];
516386714001SSerapheim Dimitropoulos 		vdev_t *current_vd = current_rvd->vdev_child[c];
516486714001SSerapheim Dimitropoulos 
516586714001SSerapheim Dimitropoulos 		space_map_t *checkpoint_sm = NULL;
516686714001SSerapheim Dimitropoulos 		uint64_t checkpoint_sm_obj;
516786714001SSerapheim Dimitropoulos 
516886714001SSerapheim Dimitropoulos 		if (ckpoint_vd->vdev_ops == &vdev_indirect_ops) {
516986714001SSerapheim Dimitropoulos 			/*
517086714001SSerapheim Dimitropoulos 			 * Since we don't allow device removal in a pool
517186714001SSerapheim Dimitropoulos 			 * that has a checkpoint, we expect that all removed
517286714001SSerapheim Dimitropoulos 			 * vdevs were removed from the pool before the
517386714001SSerapheim Dimitropoulos 			 * checkpoint.
517486714001SSerapheim Dimitropoulos 			 */
517586714001SSerapheim Dimitropoulos 			ASSERT3P(current_vd->vdev_ops, ==, &vdev_indirect_ops);
517686714001SSerapheim Dimitropoulos 			continue;
517786714001SSerapheim Dimitropoulos 		}
517886714001SSerapheim Dimitropoulos 
517986714001SSerapheim Dimitropoulos 		/*
518086714001SSerapheim Dimitropoulos 		 * If the checkpoint space map doesn't exist, then nothing
518186714001SSerapheim Dimitropoulos 		 * here is checkpointed so there's nothing to verify.
518286714001SSerapheim Dimitropoulos 		 */
518386714001SSerapheim Dimitropoulos 		if (current_vd->vdev_top_zap == 0 ||
518486714001SSerapheim Dimitropoulos 		    zap_contains(spa_meta_objset(current),
518586714001SSerapheim Dimitropoulos 		    current_vd->vdev_top_zap,
518686714001SSerapheim Dimitropoulos 		    VDEV_TOP_ZAP_POOL_CHECKPOINT_SM) != 0)
518786714001SSerapheim Dimitropoulos 			continue;
518886714001SSerapheim Dimitropoulos 
518986714001SSerapheim Dimitropoulos 		VERIFY0(zap_lookup(spa_meta_objset(current),
519086714001SSerapheim Dimitropoulos 		    current_vd->vdev_top_zap, VDEV_TOP_ZAP_POOL_CHECKPOINT_SM,
519186714001SSerapheim Dimitropoulos 		    sizeof (uint64_t), 1, &checkpoint_sm_obj));
519286714001SSerapheim Dimitropoulos 
519386714001SSerapheim Dimitropoulos 		VERIFY0(space_map_open(&checkpoint_sm, spa_meta_objset(current),
519486714001SSerapheim Dimitropoulos 		    checkpoint_sm_obj, 0, current_vd->vdev_asize,
519586714001SSerapheim Dimitropoulos 		    current_vd->vdev_ashift));
519686714001SSerapheim Dimitropoulos 
519786714001SSerapheim Dimitropoulos 		verify_checkpoint_sm_entry_cb_arg_t vcsec;
519886714001SSerapheim Dimitropoulos 		vcsec.vcsec_vd = ckpoint_vd;
519986714001SSerapheim Dimitropoulos 		vcsec.vcsec_entryid = 0;
520086714001SSerapheim Dimitropoulos 		vcsec.vcsec_num_entries =
520186714001SSerapheim Dimitropoulos 		    space_map_length(checkpoint_sm) / sizeof (uint64_t);
520286714001SSerapheim Dimitropoulos 		VERIFY0(space_map_iterate(checkpoint_sm,
5203555d674dSSerapheim Dimitropoulos 		    space_map_length(checkpoint_sm),
520486714001SSerapheim Dimitropoulos 		    verify_checkpoint_sm_entry_cb, &vcsec));
520586714001SSerapheim Dimitropoulos 		dump_spacemap(current->spa_meta_objset, checkpoint_sm);
520686714001SSerapheim Dimitropoulos 		space_map_close(checkpoint_sm);
520786714001SSerapheim Dimitropoulos 	}
520886714001SSerapheim Dimitropoulos 
520986714001SSerapheim Dimitropoulos 	/*
521086714001SSerapheim Dimitropoulos 	 * If we've added vdevs since we took the checkpoint, ensure
521186714001SSerapheim Dimitropoulos 	 * that their checkpoint space maps are empty.
521286714001SSerapheim Dimitropoulos 	 */
521386714001SSerapheim Dimitropoulos 	if (ckpoint_rvd->vdev_children < current_rvd->vdev_children) {
521486714001SSerapheim Dimitropoulos 		for (uint64_t c = ckpoint_rvd->vdev_children;
521586714001SSerapheim Dimitropoulos 		    c < current_rvd->vdev_children; c++) {
521686714001SSerapheim Dimitropoulos 			vdev_t *current_vd = current_rvd->vdev_child[c];
521786714001SSerapheim Dimitropoulos 			ASSERT3P(current_vd->vdev_checkpoint_sm, ==, NULL);
521886714001SSerapheim Dimitropoulos 		}
521986714001SSerapheim Dimitropoulos 	}
522086714001SSerapheim Dimitropoulos 
522186714001SSerapheim Dimitropoulos 	/* for cleaner progress output */
522286714001SSerapheim Dimitropoulos 	(void) fprintf(stderr, "\n");
522386714001SSerapheim Dimitropoulos }
522486714001SSerapheim Dimitropoulos 
522586714001SSerapheim Dimitropoulos /*
522686714001SSerapheim Dimitropoulos  * Verifies that all space that's allocated in the checkpoint is
522786714001SSerapheim Dimitropoulos  * still allocated in the current version, by checking that everything
522886714001SSerapheim Dimitropoulos  * in checkpoint's ms_allocatable (which is actually allocated, not
522986714001SSerapheim Dimitropoulos  * allocatable/free) is not present in current's ms_allocatable.
523086714001SSerapheim Dimitropoulos  *
523186714001SSerapheim Dimitropoulos  * Note that the function changes the state of the ms_allocatable
523286714001SSerapheim Dimitropoulos  * trees of both spas when called. The entries of all ms_allocatable
523386714001SSerapheim Dimitropoulos  * trees are cleared out and then repopulated from their respective
523486714001SSerapheim Dimitropoulos  * ms_sm space maps. In the checkpointed state we load the allocated
523586714001SSerapheim Dimitropoulos  * entries, and in the current state we load the free entries.
523686714001SSerapheim Dimitropoulos  */
523786714001SSerapheim Dimitropoulos static void
523886714001SSerapheim Dimitropoulos verify_checkpoint_ms_spacemaps(spa_t *checkpoint, spa_t *current)
523986714001SSerapheim Dimitropoulos {
524086714001SSerapheim Dimitropoulos 	vdev_t *ckpoint_rvd = checkpoint->spa_root_vdev;
524186714001SSerapheim Dimitropoulos 	vdev_t *current_rvd = current->spa_root_vdev;
524286714001SSerapheim Dimitropoulos 
524386714001SSerapheim Dimitropoulos 	load_concrete_ms_allocatable_trees(checkpoint, SM_ALLOC);
524486714001SSerapheim Dimitropoulos 	load_concrete_ms_allocatable_trees(current, SM_FREE);
524586714001SSerapheim Dimitropoulos 
524686714001SSerapheim Dimitropoulos 	for (uint64_t i = 0; i < ckpoint_rvd->vdev_children; i++) {
524786714001SSerapheim Dimitropoulos 		vdev_t *ckpoint_vd = ckpoint_rvd->vdev_child[i];
524886714001SSerapheim Dimitropoulos 		vdev_t *current_vd = current_rvd->vdev_child[i];
524986714001SSerapheim Dimitropoulos 
525086714001SSerapheim Dimitropoulos 		if (ckpoint_vd->vdev_ops == &vdev_indirect_ops) {
525186714001SSerapheim Dimitropoulos 			/*
525286714001SSerapheim Dimitropoulos 			 * See comment in verify_checkpoint_vdev_spacemaps()
525386714001SSerapheim Dimitropoulos 			 */
525486714001SSerapheim Dimitropoulos 			ASSERT3P(current_vd->vdev_ops, ==, &vdev_indirect_ops);
525586714001SSerapheim Dimitropoulos 			continue;
525686714001SSerapheim Dimitropoulos 		}
525786714001SSerapheim Dimitropoulos 
525886714001SSerapheim Dimitropoulos 		for (uint64_t m = 0; m < ckpoint_vd->vdev_ms_count; m++) {
525986714001SSerapheim Dimitropoulos 			metaslab_t *ckpoint_msp = ckpoint_vd->vdev_ms[m];
526086714001SSerapheim Dimitropoulos 			metaslab_t *current_msp = current_vd->vdev_ms[m];
526186714001SSerapheim Dimitropoulos 
526286714001SSerapheim Dimitropoulos 			(void) fprintf(stderr,
526386714001SSerapheim Dimitropoulos 			    "\rverifying vdev %llu of %llu, "
526486714001SSerapheim Dimitropoulos 			    "metaslab %llu of %llu ...",
526586714001SSerapheim Dimitropoulos 			    (longlong_t)current_vd->vdev_id,
526686714001SSerapheim Dimitropoulos 			    (longlong_t)current_rvd->vdev_children,
526786714001SSerapheim Dimitropoulos 			    (longlong_t)current_vd->vdev_ms[m]->ms_id,
526886714001SSerapheim Dimitropoulos 			    (longlong_t)current_vd->vdev_ms_count);
526986714001SSerapheim Dimitropoulos 
527086714001SSerapheim Dimitropoulos 			/*
527186714001SSerapheim Dimitropoulos 			 * We walk through the ms_allocatable trees that
527286714001SSerapheim Dimitropoulos 			 * are loaded with the allocated blocks from the
527386714001SSerapheim Dimitropoulos 			 * ms_sm spacemaps of the checkpoint. For each
527486714001SSerapheim Dimitropoulos 			 * one of these ranges we ensure that none of them
527586714001SSerapheim Dimitropoulos 			 * exists in the ms_allocatable trees of the
527686714001SSerapheim Dimitropoulos 			 * current state which are loaded with the ranges
527786714001SSerapheim Dimitropoulos 			 * that are currently free.
527886714001SSerapheim Dimitropoulos 			 *
527986714001SSerapheim Dimitropoulos 			 * This way we ensure that none of the blocks that
528086714001SSerapheim Dimitropoulos 			 * are part of the checkpoint were freed by mistake.
528186714001SSerapheim Dimitropoulos 			 */
528286714001SSerapheim Dimitropoulos 			range_tree_walk(ckpoint_msp->ms_allocatable,
5283555d674dSSerapheim Dimitropoulos 			    (range_tree_func_t *)range_tree_verify_not_present,
528486714001SSerapheim Dimitropoulos 			    current_msp->ms_allocatable);
528586714001SSerapheim Dimitropoulos 		}
528686714001SSerapheim Dimitropoulos 	}
528786714001SSerapheim Dimitropoulos 
528886714001SSerapheim Dimitropoulos 	/* for cleaner progress output */
528986714001SSerapheim Dimitropoulos 	(void) fprintf(stderr, "\n");
529086714001SSerapheim Dimitropoulos }
529186714001SSerapheim Dimitropoulos 
529286714001SSerapheim Dimitropoulos static void
529386714001SSerapheim Dimitropoulos verify_checkpoint_blocks(spa_t *spa)
529486714001SSerapheim Dimitropoulos {
5295555d674dSSerapheim Dimitropoulos 	ASSERT(!dump_opt['L']);
5296555d674dSSerapheim Dimitropoulos 
529786714001SSerapheim Dimitropoulos 	spa_t *checkpoint_spa;
529886714001SSerapheim Dimitropoulos 	char *checkpoint_pool;
529986714001SSerapheim Dimitropoulos 	nvlist_t *config = NULL;
530086714001SSerapheim Dimitropoulos 	int error = 0;
530186714001SSerapheim Dimitropoulos 
530286714001SSerapheim Dimitropoulos 	/*
530386714001SSerapheim Dimitropoulos 	 * We import the checkpointed state of the pool (under a different
530486714001SSerapheim Dimitropoulos 	 * name) so we can do verification on it against the current state
530586714001SSerapheim Dimitropoulos 	 * of the pool.
530686714001SSerapheim Dimitropoulos 	 */
530786714001SSerapheim Dimitropoulos 	checkpoint_pool = import_checkpointed_state(spa->spa_name, config,
530886714001SSerapheim Dimitropoulos 	    NULL);
530986714001SSerapheim Dimitropoulos 	ASSERT(strcmp(spa->spa_name, checkpoint_pool) != 0);
531086714001SSerapheim Dimitropoulos 
531186714001SSerapheim Dimitropoulos 	error = spa_open(checkpoint_pool, &checkpoint_spa, FTAG);
531286714001SSerapheim Dimitropoulos 	if (error != 0) {
531386714001SSerapheim Dimitropoulos 		fatal("Tried to open pool \"%s\" but spa_open() failed with "
531486714001SSerapheim Dimitropoulos 		    "error %d\n", checkpoint_pool, error);
531586714001SSerapheim Dimitropoulos 	}
531686714001SSerapheim Dimitropoulos 
531786714001SSerapheim Dimitropoulos 	/*
531886714001SSerapheim Dimitropoulos 	 * Ensure that ranges in the checkpoint space maps of each vdev
531986714001SSerapheim Dimitropoulos 	 * are allocated according to the checkpointed state's metaslab
532086714001SSerapheim Dimitropoulos 	 * space maps.
532186714001SSerapheim Dimitropoulos 	 */
532286714001SSerapheim Dimitropoulos 	verify_checkpoint_vdev_spacemaps(checkpoint_spa, spa);
532386714001SSerapheim Dimitropoulos 
532486714001SSerapheim Dimitropoulos 	/*
532586714001SSerapheim Dimitropoulos 	 * Ensure that allocated ranges in the checkpoint's metaslab
532686714001SSerapheim Dimitropoulos 	 * space maps remain allocated in the metaslab space maps of
532786714001SSerapheim Dimitropoulos 	 * the current state.
532886714001SSerapheim Dimitropoulos 	 */
532986714001SSerapheim Dimitropoulos 	verify_checkpoint_ms_spacemaps(checkpoint_spa, spa);
533086714001SSerapheim Dimitropoulos 
533186714001SSerapheim Dimitropoulos 	/*
533286714001SSerapheim Dimitropoulos 	 * Once we are done, we get rid of the checkpointed state.
533386714001SSerapheim Dimitropoulos 	 */
533486714001SSerapheim Dimitropoulos 	spa_close(checkpoint_spa, FTAG);
533586714001SSerapheim Dimitropoulos 	free(checkpoint_pool);
533686714001SSerapheim Dimitropoulos }
533786714001SSerapheim Dimitropoulos 
533886714001SSerapheim Dimitropoulos static void
533986714001SSerapheim Dimitropoulos dump_leftover_checkpoint_blocks(spa_t *spa)
534086714001SSerapheim Dimitropoulos {
534186714001SSerapheim Dimitropoulos 	vdev_t *rvd = spa->spa_root_vdev;
534286714001SSerapheim Dimitropoulos 
534386714001SSerapheim Dimitropoulos 	for (uint64_t i = 0; i < rvd->vdev_children; i++) {
534486714001SSerapheim Dimitropoulos 		vdev_t *vd = rvd->vdev_child[i];
534586714001SSerapheim Dimitropoulos 
534686714001SSerapheim Dimitropoulos 		space_map_t *checkpoint_sm = NULL;
534786714001SSerapheim Dimitropoulos 		uint64_t checkpoint_sm_obj;
534886714001SSerapheim Dimitropoulos 
534986714001SSerapheim Dimitropoulos 		if (vd->vdev_top_zap == 0)
535086714001SSerapheim Dimitropoulos 			continue;
535186714001SSerapheim Dimitropoulos 
535286714001SSerapheim Dimitropoulos 		if (zap_contains(spa_meta_objset(spa), vd->vdev_top_zap,
535386714001SSerapheim Dimitropoulos 		    VDEV_TOP_ZAP_POOL_CHECKPOINT_SM) != 0)
535486714001SSerapheim Dimitropoulos 			continue;
535586714001SSerapheim Dimitropoulos 
535686714001SSerapheim Dimitropoulos 		VERIFY0(zap_lookup(spa_meta_objset(spa), vd->vdev_top_zap,
535786714001SSerapheim Dimitropoulos 		    VDEV_TOP_ZAP_POOL_CHECKPOINT_SM,
535886714001SSerapheim Dimitropoulos 		    sizeof (uint64_t), 1, &checkpoint_sm_obj));
535986714001SSerapheim Dimitropoulos 
536086714001SSerapheim Dimitropoulos 		VERIFY0(space_map_open(&checkpoint_sm, spa_meta_objset(spa),
536186714001SSerapheim Dimitropoulos 		    checkpoint_sm_obj, 0, vd->vdev_asize, vd->vdev_ashift));
536286714001SSerapheim Dimitropoulos 		dump_spacemap(spa->spa_meta_objset, checkpoint_sm);
536386714001SSerapheim Dimitropoulos 		space_map_close(checkpoint_sm);
536486714001SSerapheim Dimitropoulos 	}
536586714001SSerapheim Dimitropoulos }
536686714001SSerapheim Dimitropoulos 
536786714001SSerapheim Dimitropoulos static int
536886714001SSerapheim Dimitropoulos verify_checkpoint(spa_t *spa)
536986714001SSerapheim Dimitropoulos {
537086714001SSerapheim Dimitropoulos 	uberblock_t checkpoint;
537186714001SSerapheim Dimitropoulos 	int error;
537286714001SSerapheim Dimitropoulos 
537386714001SSerapheim Dimitropoulos 	if (!spa_feature_is_active(spa, SPA_FEATURE_POOL_CHECKPOINT))
537486714001SSerapheim Dimitropoulos 		return (0);
537586714001SSerapheim Dimitropoulos 
537686714001SSerapheim Dimitropoulos 	error = zap_lookup(spa->spa_meta_objset, DMU_POOL_DIRECTORY_OBJECT,
537786714001SSerapheim Dimitropoulos 	    DMU_POOL_ZPOOL_CHECKPOINT, sizeof (uint64_t),
537886714001SSerapheim Dimitropoulos 	    sizeof (uberblock_t) / sizeof (uint64_t), &checkpoint);
537986714001SSerapheim Dimitropoulos 
538017f11284SSerapheim Dimitropoulos 	if (error == ENOENT && !dump_opt['L']) {
538186714001SSerapheim Dimitropoulos 		/*
538286714001SSerapheim Dimitropoulos 		 * If the feature is active but the uberblock is missing
538386714001SSerapheim Dimitropoulos 		 * then we must be in the middle of discarding the
538486714001SSerapheim Dimitropoulos 		 * checkpoint.
538586714001SSerapheim Dimitropoulos 		 */
538686714001SSerapheim Dimitropoulos 		(void) printf("\nPartially discarded checkpoint "
538786714001SSerapheim Dimitropoulos 		    "state found:\n");
538886714001SSerapheim Dimitropoulos 		dump_leftover_checkpoint_blocks(spa);
538986714001SSerapheim Dimitropoulos 		return (0);
539086714001SSerapheim Dimitropoulos 	} else if (error != 0) {
539186714001SSerapheim Dimitropoulos 		(void) printf("lookup error %d when looking for "
539286714001SSerapheim Dimitropoulos 		    "checkpointed uberblock in MOS\n", error);
539386714001SSerapheim Dimitropoulos 		return (error);
539486714001SSerapheim Dimitropoulos 	}
539586714001SSerapheim Dimitropoulos 	dump_uberblock(&checkpoint, "\nCheckpointed uberblock found:\n", "\n");
539686714001SSerapheim Dimitropoulos 
539786714001SSerapheim Dimitropoulos 	if (checkpoint.ub_checkpoint_txg == 0) {
539886714001SSerapheim Dimitropoulos 		(void) printf("\nub_checkpoint_txg not set in checkpointed "
539986714001SSerapheim Dimitropoulos 		    "uberblock\n");
540086714001SSerapheim Dimitropoulos 		error = 3;
540186714001SSerapheim Dimitropoulos 	}
540286714001SSerapheim Dimitropoulos 
540317f11284SSerapheim Dimitropoulos 	if (error == 0 && !dump_opt['L'])
540486714001SSerapheim Dimitropoulos 		verify_checkpoint_blocks(spa);
540586714001SSerapheim Dimitropoulos 
540686714001SSerapheim Dimitropoulos 	return (error);
540786714001SSerapheim Dimitropoulos }
540886714001SSerapheim Dimitropoulos 
540917fb938fSMatthew Ahrens /* ARGSUSED */
541017fb938fSMatthew Ahrens static void
541117fb938fSMatthew Ahrens mos_leaks_cb(void *arg, uint64_t start, uint64_t size)
541217fb938fSMatthew Ahrens {
541317fb938fSMatthew Ahrens 	for (uint64_t i = start; i < size; i++) {
541417fb938fSMatthew Ahrens 		(void) printf("MOS object %llu referenced but not allocated\n",
541517fb938fSMatthew Ahrens 		    (u_longlong_t)i);
541617fb938fSMatthew Ahrens 	}
541717fb938fSMatthew Ahrens }
541817fb938fSMatthew Ahrens 
541917fb938fSMatthew Ahrens static range_tree_t *mos_refd_objs;
542017fb938fSMatthew Ahrens 
542117fb938fSMatthew Ahrens static void
542217fb938fSMatthew Ahrens mos_obj_refd(uint64_t obj)
542317fb938fSMatthew Ahrens {
542417fb938fSMatthew Ahrens 	if (obj != 0 && mos_refd_objs != NULL)
542517fb938fSMatthew Ahrens 		range_tree_add(mos_refd_objs, obj, 1);
542617fb938fSMatthew Ahrens }
542717fb938fSMatthew Ahrens 
5428814dcd43SSerapheim Dimitropoulos static void
5429814dcd43SSerapheim Dimitropoulos mos_leak_vdev_top_zap(vdev_t *vd)
5430814dcd43SSerapheim Dimitropoulos {
5431814dcd43SSerapheim Dimitropoulos 	uint64_t ms_flush_data_obj;
5432814dcd43SSerapheim Dimitropoulos 
5433814dcd43SSerapheim Dimitropoulos 	int error = zap_lookup(spa_meta_objset(vd->vdev_spa),
5434814dcd43SSerapheim Dimitropoulos 	    vd->vdev_top_zap, VDEV_TOP_ZAP_MS_UNFLUSHED_PHYS_TXGS,
5435814dcd43SSerapheim Dimitropoulos 	    sizeof (ms_flush_data_obj), 1, &ms_flush_data_obj);
5436814dcd43SSerapheim Dimitropoulos 	if (error == ENOENT)
5437814dcd43SSerapheim Dimitropoulos 		return;
5438814dcd43SSerapheim Dimitropoulos 	ASSERT0(error);
5439814dcd43SSerapheim Dimitropoulos 
5440814dcd43SSerapheim Dimitropoulos 	mos_obj_refd(ms_flush_data_obj);
5441814dcd43SSerapheim Dimitropoulos }
5442814dcd43SSerapheim Dimitropoulos 
544317fb938fSMatthew Ahrens static void
544417fb938fSMatthew Ahrens mos_leak_vdev(vdev_t *vd)
544517fb938fSMatthew Ahrens {
544617fb938fSMatthew Ahrens 	mos_obj_refd(vd->vdev_dtl_object);
544717fb938fSMatthew Ahrens 	mos_obj_refd(vd->vdev_ms_array);
544817fb938fSMatthew Ahrens 	mos_obj_refd(vd->vdev_indirect_config.vic_births_object);
544917fb938fSMatthew Ahrens 	mos_obj_refd(vd->vdev_indirect_config.vic_mapping_object);
545017fb938fSMatthew Ahrens 	mos_obj_refd(vd->vdev_leaf_zap);
545117fb938fSMatthew Ahrens 	if (vd->vdev_checkpoint_sm != NULL)
545217fb938fSMatthew Ahrens 		mos_obj_refd(vd->vdev_checkpoint_sm->sm_object);
545317fb938fSMatthew Ahrens 	if (vd->vdev_indirect_mapping != NULL) {
545417fb938fSMatthew Ahrens 		mos_obj_refd(vd->vdev_indirect_mapping->
545517fb938fSMatthew Ahrens 		    vim_phys->vimp_counts_object);
545617fb938fSMatthew Ahrens 	}
545717fb938fSMatthew Ahrens 	if (vd->vdev_obsolete_sm != NULL)
545817fb938fSMatthew Ahrens 		mos_obj_refd(vd->vdev_obsolete_sm->sm_object);
545917fb938fSMatthew Ahrens 
546017fb938fSMatthew Ahrens 	for (uint64_t m = 0; m < vd->vdev_ms_count; m++) {
546117fb938fSMatthew Ahrens 		metaslab_t *ms = vd->vdev_ms[m];
546217fb938fSMatthew Ahrens 		mos_obj_refd(space_map_object(ms->ms_sm));
546317fb938fSMatthew Ahrens 	}
546417fb938fSMatthew Ahrens 
5465814dcd43SSerapheim Dimitropoulos 	if (vd->vdev_top_zap != 0) {
5466814dcd43SSerapheim Dimitropoulos 		mos_obj_refd(vd->vdev_top_zap);
5467814dcd43SSerapheim Dimitropoulos 		mos_leak_vdev_top_zap(vd);
5468814dcd43SSerapheim Dimitropoulos 	}
5469814dcd43SSerapheim Dimitropoulos 
547017fb938fSMatthew Ahrens 	for (uint64_t c = 0; c < vd->vdev_children; c++) {
547117fb938fSMatthew Ahrens 		mos_leak_vdev(vd->vdev_child[c]);
547217fb938fSMatthew Ahrens 	}
547317fb938fSMatthew Ahrens }
547417fb938fSMatthew Ahrens 
5475814dcd43SSerapheim Dimitropoulos static void
5476814dcd43SSerapheim Dimitropoulos mos_leak_log_spacemaps(spa_t *spa)
5477814dcd43SSerapheim Dimitropoulos {
5478814dcd43SSerapheim Dimitropoulos 	uint64_t spacemap_zap;
5479814dcd43SSerapheim Dimitropoulos 
5480814dcd43SSerapheim Dimitropoulos 	int error = zap_lookup(spa_meta_objset(spa),
5481814dcd43SSerapheim Dimitropoulos 	    DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_LOG_SPACEMAP_ZAP,
5482814dcd43SSerapheim Dimitropoulos 	    sizeof (spacemap_zap), 1, &spacemap_zap);
5483814dcd43SSerapheim Dimitropoulos 	if (error == ENOENT)
5484814dcd43SSerapheim Dimitropoulos 		return;
5485814dcd43SSerapheim Dimitropoulos 	ASSERT0(error);
5486814dcd43SSerapheim Dimitropoulos 
5487814dcd43SSerapheim Dimitropoulos 	mos_obj_refd(spacemap_zap);
5488814dcd43SSerapheim Dimitropoulos 	for (spa_log_sm_t *sls = avl_first(&spa->spa_sm_logs_by_txg);
5489814dcd43SSerapheim Dimitropoulos 	    sls; sls = AVL_NEXT(&spa->spa_sm_logs_by_txg, sls))
5490814dcd43SSerapheim Dimitropoulos 		mos_obj_refd(sls->sls_sm_obj);
5491814dcd43SSerapheim Dimitropoulos }
5492814dcd43SSerapheim Dimitropoulos 
549317fb938fSMatthew Ahrens static int
549417fb938fSMatthew Ahrens dump_mos_leaks(spa_t *spa)
549517fb938fSMatthew Ahrens {
549617fb938fSMatthew Ahrens 	int rv = 0;
549717fb938fSMatthew Ahrens 	objset_t *mos = spa->spa_meta_objset;
549817fb938fSMatthew Ahrens 	dsl_pool_t *dp = spa->spa_dsl_pool;
549917fb938fSMatthew Ahrens 
550017fb938fSMatthew Ahrens 	/* Visit and mark all referenced objects in the MOS */
550117fb938fSMatthew Ahrens 
550217fb938fSMatthew Ahrens 	mos_obj_refd(DMU_POOL_DIRECTORY_OBJECT);
550317fb938fSMatthew Ahrens 	mos_obj_refd(spa->spa_pool_props_object);
550417fb938fSMatthew Ahrens 	mos_obj_refd(spa->spa_config_object);
550517fb938fSMatthew Ahrens 	mos_obj_refd(spa->spa_ddt_stat_object);
550617fb938fSMatthew Ahrens 	mos_obj_refd(spa->spa_feat_desc_obj);
550717fb938fSMatthew Ahrens 	mos_obj_refd(spa->spa_feat_enabled_txg_obj);
550817fb938fSMatthew Ahrens 	mos_obj_refd(spa->spa_feat_for_read_obj);
550917fb938fSMatthew Ahrens 	mos_obj_refd(spa->spa_feat_for_write_obj);
551017fb938fSMatthew Ahrens 	mos_obj_refd(spa->spa_history);
551117fb938fSMatthew Ahrens 	mos_obj_refd(spa->spa_errlog_last);
551217fb938fSMatthew Ahrens 	mos_obj_refd(spa->spa_errlog_scrub);
551317fb938fSMatthew Ahrens 	mos_obj_refd(spa->spa_all_vdev_zaps);
551417fb938fSMatthew Ahrens 	mos_obj_refd(spa->spa_dsl_pool->dp_bptree_obj);
551517fb938fSMatthew Ahrens 	mos_obj_refd(spa->spa_dsl_pool->dp_tmp_userrefs_obj);
551617fb938fSMatthew Ahrens 	mos_obj_refd(spa->spa_dsl_pool->dp_scan->scn_phys.scn_queue_obj);
551717fb938fSMatthew Ahrens 	bpobj_count_refd(&spa->spa_deferred_bpobj);
551817fb938fSMatthew Ahrens 	mos_obj_refd(dp->dp_empty_bpobj);
551917fb938fSMatthew Ahrens 	bpobj_count_refd(&dp->dp_obsolete_bpobj);
552017fb938fSMatthew Ahrens 	bpobj_count_refd(&dp->dp_free_bpobj);
552117fb938fSMatthew Ahrens 	mos_obj_refd(spa->spa_l2cache.sav_object);
552217fb938fSMatthew Ahrens 	mos_obj_refd(spa->spa_spares.sav_object);
552317fb938fSMatthew Ahrens 
5524814dcd43SSerapheim Dimitropoulos 	if (spa->spa_syncing_log_sm != NULL)
5525814dcd43SSerapheim Dimitropoulos 		mos_obj_refd(spa->spa_syncing_log_sm->sm_object);
5526814dcd43SSerapheim Dimitropoulos 	mos_leak_log_spacemaps(spa);
5527814dcd43SSerapheim Dimitropoulos 
552817fb938fSMatthew Ahrens 	mos_obj_refd(spa->spa_condensing_indirect_phys.
552917fb938fSMatthew Ahrens 	    scip_next_mapping_object);
553017fb938fSMatthew Ahrens 	mos_obj_refd(spa->spa_condensing_indirect_phys.
553117fb938fSMatthew Ahrens 	    scip_prev_obsolete_sm_object);
553217fb938fSMatthew Ahrens 	if (spa->spa_condensing_indirect_phys.scip_next_mapping_object != 0) {
553317fb938fSMatthew Ahrens 		vdev_indirect_mapping_t *vim =
553417fb938fSMatthew Ahrens 		    vdev_indirect_mapping_open(mos,
553517fb938fSMatthew Ahrens 		    spa->spa_condensing_indirect_phys.scip_next_mapping_object);
553617fb938fSMatthew Ahrens 		mos_obj_refd(vim->vim_phys->vimp_counts_object);
553717fb938fSMatthew Ahrens 		vdev_indirect_mapping_close(vim);
553817fb938fSMatthew Ahrens 	}
553917fb938fSMatthew Ahrens 
554017fb938fSMatthew Ahrens 	if (dp->dp_origin_snap != NULL) {
554117fb938fSMatthew Ahrens 		dsl_dataset_t *ds;
554217fb938fSMatthew Ahrens 
554317fb938fSMatthew Ahrens 		dsl_pool_config_enter(dp, FTAG);
554417fb938fSMatthew Ahrens 		VERIFY0(dsl_dataset_hold_obj(dp,
554517fb938fSMatthew Ahrens 		    dsl_dataset_phys(dp->dp_origin_snap)->ds_next_snap_obj,
554617fb938fSMatthew Ahrens 		    FTAG, &ds));
554717fb938fSMatthew Ahrens 		count_ds_mos_objects(ds);
554817fb938fSMatthew Ahrens 		dump_deadlist(&ds->ds_deadlist);
554917fb938fSMatthew Ahrens 		dsl_dataset_rele(ds, FTAG);
555017fb938fSMatthew Ahrens 		dsl_pool_config_exit(dp, FTAG);
555117fb938fSMatthew Ahrens 
555217fb938fSMatthew Ahrens 		count_ds_mos_objects(dp->dp_origin_snap);
555317fb938fSMatthew Ahrens 		dump_deadlist(&dp->dp_origin_snap->ds_deadlist);
555417fb938fSMatthew Ahrens 	}
555517fb938fSMatthew Ahrens 	count_dir_mos_objects(dp->dp_mos_dir);
555617fb938fSMatthew Ahrens 	if (dp->dp_free_dir != NULL)
555717fb938fSMatthew Ahrens 		count_dir_mos_objects(dp->dp_free_dir);
555817fb938fSMatthew Ahrens 	if (dp->dp_leak_dir != NULL)
555917fb938fSMatthew Ahrens 		count_dir_mos_objects(dp->dp_leak_dir);
556017fb938fSMatthew Ahrens 
556117fb938fSMatthew Ahrens 	mos_leak_vdev(spa->spa_root_vdev);
556217fb938fSMatthew Ahrens 
556317fb938fSMatthew Ahrens 	for (uint64_t class = 0; class < DDT_CLASSES; class++) {
556417fb938fSMatthew Ahrens 		for (uint64_t type = 0; type < DDT_TYPES; type++) {
556517fb938fSMatthew Ahrens 			for (uint64_t cksum = 0;
556617fb938fSMatthew Ahrens 			    cksum < ZIO_CHECKSUM_FUNCTIONS; cksum++) {
556717fb938fSMatthew Ahrens 				ddt_t *ddt = spa->spa_ddt[cksum];
556817fb938fSMatthew Ahrens 				mos_obj_refd(ddt->ddt_object[type][class]);
556917fb938fSMatthew Ahrens 			}
557017fb938fSMatthew Ahrens 		}
557117fb938fSMatthew Ahrens 	}
557217fb938fSMatthew Ahrens 
557317fb938fSMatthew Ahrens 	/*
557417fb938fSMatthew Ahrens 	 * Visit all allocated objects and make sure they are referenced.
557517fb938fSMatthew Ahrens 	 */
557617fb938fSMatthew Ahrens 	uint64_t object = 0;
557717fb938fSMatthew Ahrens 	while (dmu_object_next(mos, &object, B_FALSE, 0) == 0) {
557817fb938fSMatthew Ahrens 		if (range_tree_contains(mos_refd_objs, object, 1)) {
557917fb938fSMatthew Ahrens 			range_tree_remove(mos_refd_objs, object, 1);
558017fb938fSMatthew Ahrens 		} else {
558117fb938fSMatthew Ahrens 			dmu_object_info_t doi;
558217fb938fSMatthew Ahrens 			const char *name;
558317fb938fSMatthew Ahrens 			dmu_object_info(mos, object, &doi);
558417fb938fSMatthew Ahrens 			if (doi.doi_type & DMU_OT_NEWTYPE) {
558517fb938fSMatthew Ahrens 				dmu_object_byteswap_t bswap =
558617fb938fSMatthew Ahrens 				    DMU_OT_BYTESWAP(doi.doi_type);
558717fb938fSMatthew Ahrens 				name = dmu_ot_byteswap[bswap].ob_name;
558817fb938fSMatthew Ahrens 			} else {
558917fb938fSMatthew Ahrens 				name = dmu_ot[doi.doi_type].ot_name;
559017fb938fSMatthew Ahrens 			}
559117fb938fSMatthew Ahrens 
559217fb938fSMatthew Ahrens 			(void) printf("MOS object %llu (%s) leaked\n",
559317fb938fSMatthew Ahrens 			    (u_longlong_t)object, name);
559417fb938fSMatthew Ahrens 			rv = 2;
559517fb938fSMatthew Ahrens 		}
559617fb938fSMatthew Ahrens 	}
559717fb938fSMatthew Ahrens 	(void) range_tree_walk(mos_refd_objs, mos_leaks_cb, NULL);
559817fb938fSMatthew Ahrens 	if (!range_tree_is_empty(mos_refd_objs))
559917fb938fSMatthew Ahrens 		rv = 2;
560017fb938fSMatthew Ahrens 	range_tree_vacate(mos_refd_objs, NULL, NULL);
560117fb938fSMatthew Ahrens 	range_tree_destroy(mos_refd_objs);
560217fb938fSMatthew Ahrens 	return (rv);
560317fb938fSMatthew Ahrens }
560417fb938fSMatthew Ahrens 
5605814dcd43SSerapheim Dimitropoulos typedef struct log_sm_obsolete_stats_arg {
5606814dcd43SSerapheim Dimitropoulos 	uint64_t lsos_current_txg;
5607814dcd43SSerapheim Dimitropoulos 
5608814dcd43SSerapheim Dimitropoulos 	uint64_t lsos_total_entries;
5609814dcd43SSerapheim Dimitropoulos 	uint64_t lsos_valid_entries;
5610814dcd43SSerapheim Dimitropoulos 
5611814dcd43SSerapheim Dimitropoulos 	uint64_t lsos_sm_entries;
5612814dcd43SSerapheim Dimitropoulos 	uint64_t lsos_valid_sm_entries;
5613814dcd43SSerapheim Dimitropoulos } log_sm_obsolete_stats_arg_t;
5614814dcd43SSerapheim Dimitropoulos 
5615814dcd43SSerapheim Dimitropoulos static int
5616814dcd43SSerapheim Dimitropoulos log_spacemap_obsolete_stats_cb(spa_t *spa, space_map_entry_t *sme,
5617814dcd43SSerapheim Dimitropoulos     uint64_t txg, void *arg)
5618814dcd43SSerapheim Dimitropoulos {
5619814dcd43SSerapheim Dimitropoulos 	log_sm_obsolete_stats_arg_t *lsos = arg;
5620814dcd43SSerapheim Dimitropoulos 	uint64_t offset = sme->sme_offset;
5621814dcd43SSerapheim Dimitropoulos 	uint64_t vdev_id = sme->sme_vdev;
5622814dcd43SSerapheim Dimitropoulos 
5623814dcd43SSerapheim Dimitropoulos 	if (lsos->lsos_current_txg == 0) {
5624814dcd43SSerapheim Dimitropoulos 		/* this is the first log */
5625814dcd43SSerapheim Dimitropoulos 		lsos->lsos_current_txg = txg;
5626814dcd43SSerapheim Dimitropoulos 	} else if (lsos->lsos_current_txg < txg) {
5627814dcd43SSerapheim Dimitropoulos 		/* we just changed log - print stats and reset */
5628814dcd43SSerapheim Dimitropoulos 		(void) printf("%-8llu valid entries out of %-8llu - txg %llu\n",
5629814dcd43SSerapheim Dimitropoulos 		    (u_longlong_t)lsos->lsos_valid_sm_entries,
5630814dcd43SSerapheim Dimitropoulos 		    (u_longlong_t)lsos->lsos_sm_entries,
5631814dcd43SSerapheim Dimitropoulos 		    (u_longlong_t)lsos->lsos_current_txg);
5632814dcd43SSerapheim Dimitropoulos 		lsos->lsos_valid_sm_entries = 0;
5633814dcd43SSerapheim Dimitropoulos 		lsos->lsos_sm_entries = 0;
5634814dcd43SSerapheim Dimitropoulos 		lsos->lsos_current_txg = txg;
5635814dcd43SSerapheim Dimitropoulos 	}
5636814dcd43SSerapheim Dimitropoulos 	ASSERT3U(lsos->lsos_current_txg, ==, txg);
5637814dcd43SSerapheim Dimitropoulos 
5638814dcd43SSerapheim Dimitropoulos 	lsos->lsos_sm_entries++;
5639814dcd43SSerapheim Dimitropoulos 	lsos->lsos_total_entries++;
5640814dcd43SSerapheim Dimitropoulos 
5641814dcd43SSerapheim Dimitropoulos 	vdev_t *vd = vdev_lookup_top(spa, vdev_id);
5642814dcd43SSerapheim Dimitropoulos 	if (!vdev_is_concrete(vd))
5643814dcd43SSerapheim Dimitropoulos 		return (0);
5644814dcd43SSerapheim Dimitropoulos 
5645814dcd43SSerapheim Dimitropoulos 	metaslab_t *ms = vd->vdev_ms[offset >> vd->vdev_ms_shift];
5646814dcd43SSerapheim Dimitropoulos 	ASSERT(sme->sme_type == SM_ALLOC || sme->sme_type == SM_FREE);
5647814dcd43SSerapheim Dimitropoulos 
5648814dcd43SSerapheim Dimitropoulos 	if (txg < metaslab_unflushed_txg(ms))
5649814dcd43SSerapheim Dimitropoulos 		return (0);
5650814dcd43SSerapheim Dimitropoulos 	lsos->lsos_valid_sm_entries++;
5651814dcd43SSerapheim Dimitropoulos 	lsos->lsos_valid_entries++;
5652814dcd43SSerapheim Dimitropoulos 	return (0);
5653814dcd43SSerapheim Dimitropoulos }
5654814dcd43SSerapheim Dimitropoulos 
5655814dcd43SSerapheim Dimitropoulos static void
5656814dcd43SSerapheim Dimitropoulos dump_log_spacemap_obsolete_stats(spa_t *spa)
5657814dcd43SSerapheim Dimitropoulos {
5658814dcd43SSerapheim Dimitropoulos 	if (!spa_feature_is_active(spa, SPA_FEATURE_LOG_SPACEMAP))
5659814dcd43SSerapheim Dimitropoulos 		return;
5660814dcd43SSerapheim Dimitropoulos 
5661814dcd43SSerapheim Dimitropoulos 	log_sm_obsolete_stats_arg_t lsos;
5662814dcd43SSerapheim Dimitropoulos 	bzero(&lsos, sizeof (lsos));
5663814dcd43SSerapheim Dimitropoulos 
5664814dcd43SSerapheim Dimitropoulos 	(void) printf("Log Space Map Obsolete Entry Statistics:\n");
5665814dcd43SSerapheim Dimitropoulos 
5666814dcd43SSerapheim Dimitropoulos 	iterate_through_spacemap_logs(spa,
5667814dcd43SSerapheim Dimitropoulos 	    log_spacemap_obsolete_stats_cb, &lsos);
5668814dcd43SSerapheim Dimitropoulos 
5669814dcd43SSerapheim Dimitropoulos 	/* print stats for latest log */
5670814dcd43SSerapheim Dimitropoulos 	(void) printf("%-8llu valid entries out of %-8llu - txg %llu\n",
5671814dcd43SSerapheim Dimitropoulos 	    (u_longlong_t)lsos.lsos_valid_sm_entries,
5672814dcd43SSerapheim Dimitropoulos 	    (u_longlong_t)lsos.lsos_sm_entries,
5673814dcd43SSerapheim Dimitropoulos 	    (u_longlong_t)lsos.lsos_current_txg);
5674814dcd43SSerapheim Dimitropoulos 
5675814dcd43SSerapheim Dimitropoulos 	(void) printf("%-8llu valid entries out of %-8llu - total\n\n",
5676814dcd43SSerapheim Dimitropoulos 	    (u_longlong_t)lsos.lsos_valid_entries,
5677814dcd43SSerapheim Dimitropoulos 	    (u_longlong_t)lsos.lsos_total_entries);
5678814dcd43SSerapheim Dimitropoulos }
5679814dcd43SSerapheim Dimitropoulos 
5680fa9e4066Sahrens static void
5681fa9e4066Sahrens dump_zpool(spa_t *spa)
5682fa9e4066Sahrens {
5683fa9e4066Sahrens 	dsl_pool_t *dp = spa_get_dsl(spa);
5684fa9e4066Sahrens 	int rc = 0;
5685fa9e4066Sahrens 
5686b24ab676SJeff Bonwick 	if (dump_opt['S']) {
5687b24ab676SJeff Bonwick 		dump_simulated_ddt(spa);
5688b24ab676SJeff Bonwick 		return;
5689b24ab676SJeff Bonwick 	}
5690b24ab676SJeff Bonwick 
569107428bdfSVictor Latushkin 	if (!dump_opt['e'] && dump_opt['C'] > 1) {
569207428bdfSVictor Latushkin 		(void) printf("\nCached configuration:\n");
569307428bdfSVictor Latushkin 		dump_nvlist(spa->spa_config, 8);
569407428bdfSVictor Latushkin 	}
569507428bdfSVictor Latushkin 
569607428bdfSVictor Latushkin 	if (dump_opt['C'])
569707428bdfSVictor Latushkin 		dump_config(spa);
569807428bdfSVictor Latushkin 
5699fa9e4066Sahrens 	if (dump_opt['u'])
570053b9a4a9SVictor Latushkin 		dump_uberblock(&spa->spa_uberblock, "\nUberblock:\n", "\n");
5701fa9e4066Sahrens 
5702b24ab676SJeff Bonwick 	if (dump_opt['D'])
5703b24ab676SJeff Bonwick 		dump_all_ddts(spa);
5704b24ab676SJeff Bonwick 
570587219db7SVictor Latushkin 	if (dump_opt['d'] > 2 || dump_opt['m'])
570687219db7SVictor Latushkin 		dump_metaslabs(spa);
57072e4c9986SGeorge Wilson 	if (dump_opt['M'])
57082e4c9986SGeorge Wilson 		dump_metaslab_groups(spa);
5709814dcd43SSerapheim Dimitropoulos 	if (dump_opt['d'] > 2 || dump_opt['m']) {
5710814dcd43SSerapheim Dimitropoulos 		dump_log_spacemaps(spa);
5711814dcd43SSerapheim Dimitropoulos 		dump_log_spacemap_obsolete_stats(spa);
5712814dcd43SSerapheim Dimitropoulos 	}
571387219db7SVictor Latushkin 
571487219db7SVictor Latushkin 	if (dump_opt['d'] || dump_opt['i']) {
57154d7988d6SPaul Dagnelie 		mos_refd_objs = range_tree_create(NULL, RANGE_SEG64, NULL, 0,
57164d7988d6SPaul Dagnelie 		    0);
5717fa9e4066Sahrens 		dump_dir(dp->dp_meta_objset);
571817fb938fSMatthew Ahrens 
5719fa9e4066Sahrens 		if (dump_opt['d'] >= 3) {
57205cabbc6bSPrashanth Sreenivasa 			dsl_pool_t *dp = spa->spa_dsl_pool;
5721732885fcSMatthew Ahrens 			dump_full_bpobj(&spa->spa_deferred_bpobj,
5722d0475637SMatthew Ahrens 			    "Deferred frees", 0);
5723cde58dbcSMatthew Ahrens 			if (spa_version(spa) >= SPA_VERSION_DEADLISTS) {
57245cabbc6bSPrashanth Sreenivasa 				dump_full_bpobj(&dp->dp_free_bpobj,
5725d0475637SMatthew Ahrens 				    "Pool snapshot frees", 0);
5726ad135b5dSChristopher Siden 			}
57275cabbc6bSPrashanth Sreenivasa 			if (bpobj_is_open(&dp->dp_obsolete_bpobj)) {
57285cabbc6bSPrashanth Sreenivasa 				ASSERT(spa_feature_is_enabled(spa,
57295cabbc6bSPrashanth Sreenivasa 				    SPA_FEATURE_DEVICE_REMOVAL));
57305cabbc6bSPrashanth Sreenivasa 				dump_full_bpobj(&dp->dp_obsolete_bpobj,
57315cabbc6bSPrashanth Sreenivasa 				    "Pool obsolete blocks", 0);
57325cabbc6bSPrashanth Sreenivasa 			}
5733ad135b5dSChristopher Siden 
5734ad135b5dSChristopher Siden 			if (spa_feature_is_active(spa,
57352acef22dSMatthew Ahrens 			    SPA_FEATURE_ASYNC_DESTROY)) {
5736ad135b5dSChristopher Siden 				dump_bptree(spa->spa_meta_objset,
57375cabbc6bSPrashanth Sreenivasa 				    dp->dp_bptree_obj,
5738ad135b5dSChristopher Siden 				    "Pool dataset frees");
5739cde58dbcSMatthew Ahrens 			}
5740fa9e4066Sahrens 			dump_dtl(spa->spa_root_vdev, 0);
5741fa9e4066Sahrens 		}
574207428bdfSVictor Latushkin 		(void) dmu_objset_find(spa_name(spa), dump_one_dir,
574307428bdfSVictor Latushkin 		    NULL, DS_FIND_SNAPSHOTS | DS_FIND_CHILDREN);
5744b5152584SMatthew Ahrens 
574517fb938fSMatthew Ahrens 		if (rc == 0 && !dump_opt['L'])
574617fb938fSMatthew Ahrens 			rc = dump_mos_leaks(spa);
574717fb938fSMatthew Ahrens 
5748ca0cc391SMatthew Ahrens 		for (spa_feature_t f = 0; f < SPA_FEATURES; f++) {
5749ca0cc391SMatthew Ahrens 			uint64_t refcount;
5750ca0cc391SMatthew Ahrens 
5751ca0cc391SMatthew Ahrens 			if (!(spa_feature_table[f].fi_flags &
5752c5d1600cSMatthew Ahrens 			    ZFEATURE_FLAG_PER_DATASET) ||
5753c5d1600cSMatthew Ahrens 			    !spa_feature_is_enabled(spa, f)) {
5754ca0cc391SMatthew Ahrens 				ASSERT0(dataset_feature_count[f]);
5755ca0cc391SMatthew Ahrens 				continue;
5756ca0cc391SMatthew Ahrens 			}
5757ca0cc391SMatthew Ahrens 			(void) feature_get_refcount(spa,
5758ca0cc391SMatthew Ahrens 			    &spa_feature_table[f], &refcount);
5759ca0cc391SMatthew Ahrens 			if (dataset_feature_count[f] != refcount) {
5760ca0cc391SMatthew Ahrens 				(void) printf("%s feature refcount mismatch: "
5761ca0cc391SMatthew Ahrens 				    "%lld datasets != %lld refcount\n",
5762ca0cc391SMatthew Ahrens 				    spa_feature_table[f].fi_uname,
5763ca0cc391SMatthew Ahrens 				    (longlong_t)dataset_feature_count[f],
5764ca0cc391SMatthew Ahrens 				    (longlong_t)refcount);
5765ca0cc391SMatthew Ahrens 				rc = 2;
5766ca0cc391SMatthew Ahrens 			} else {
5767ca0cc391SMatthew Ahrens 				(void) printf("Verified %s feature refcount "
5768ca0cc391SMatthew Ahrens 				    "of %llu is correct\n",
5769ca0cc391SMatthew Ahrens 				    spa_feature_table[f].fi_uname,
5770ca0cc391SMatthew Ahrens 				    (longlong_t)refcount);
5771ca0cc391SMatthew Ahrens 			}
5772b5152584SMatthew Ahrens 		}
57735cabbc6bSPrashanth Sreenivasa 
5774814dcd43SSerapheim Dimitropoulos 		if (rc == 0)
57755cabbc6bSPrashanth Sreenivasa 			rc = verify_device_removal_feature_counts(spa);
5776fa9e4066Sahrens 	}
577717fb938fSMatthew Ahrens 
5778b5152584SMatthew Ahrens 	if (rc == 0 && (dump_opt['b'] || dump_opt['c']))
5779fa9e4066Sahrens 		rc = dump_block_stats(spa);
5780fa9e4066Sahrens 
57810713e232SGeorge Wilson 	if (rc == 0)
57820713e232SGeorge Wilson 		rc = verify_spacemap_refcounts(spa);
57830713e232SGeorge Wilson 
5784fa9e4066Sahrens 	if (dump_opt['s'])
5785fa9e4066Sahrens 		show_pool_stats(spa);
5786fa9e4066Sahrens 
57878f18d1faSGeorge Wilson 	if (dump_opt['h'])
57888f18d1faSGeorge Wilson 		dump_history(spa);
57898f18d1faSGeorge Wilson 
579017f11284SSerapheim Dimitropoulos 	if (rc == 0)
579186714001SSerapheim Dimitropoulos 		rc = verify_checkpoint(spa);
579286714001SSerapheim Dimitropoulos 
579329bdd2f9SPavel Zakharov 	if (rc != 0) {
579429bdd2f9SPavel Zakharov 		dump_debug_buffer();
5795fa9e4066Sahrens 		exit(rc);
579629bdd2f9SPavel Zakharov 	}
5797fa9e4066Sahrens }
5798fa9e4066Sahrens 
579944cd46caSbillm #define	ZDB_FLAG_CHECKSUM	0x0001
580044cd46caSbillm #define	ZDB_FLAG_DECOMPRESS	0x0002
580144cd46caSbillm #define	ZDB_FLAG_BSWAP		0x0004
580244cd46caSbillm #define	ZDB_FLAG_GBH		0x0008
580344cd46caSbillm #define	ZDB_FLAG_INDIRECT	0x0010
580444cd46caSbillm #define	ZDB_FLAG_PHYS		0x0020
580544cd46caSbillm #define	ZDB_FLAG_RAW		0x0040
580644cd46caSbillm #define	ZDB_FLAG_PRINT_BLKPTR	0x0080
580744cd46caSbillm 
58083f7978d0SAlan Somers static int flagbits[256];
580944cd46caSbillm 
581044cd46caSbillm static void
581144cd46caSbillm zdb_print_blkptr(blkptr_t *bp, int flags)
581244cd46caSbillm {
5813b24ab676SJeff Bonwick 	char blkbuf[BP_SPRINTF_LEN];
581444cd46caSbillm 
581544cd46caSbillm 	if (flags & ZDB_FLAG_BSWAP)
581644cd46caSbillm 		byteswap_uint64_array((void *)bp, sizeof (blkptr_t));
5817b24ab676SJeff Bonwick 
581843466aaeSMax Grossman 	snprintf_blkptr(blkbuf, sizeof (blkbuf), bp);
5819b24ab676SJeff Bonwick 	(void) printf("%s\n", blkbuf);
582044cd46caSbillm }
582144cd46caSbillm 
582244cd46caSbillm static void
582344cd46caSbillm zdb_dump_indirect(blkptr_t *bp, int nbps, int flags)
582444cd46caSbillm {
582544cd46caSbillm 	int i;
582644cd46caSbillm 
582744cd46caSbillm 	for (i = 0; i < nbps; i++)
582844cd46caSbillm 		zdb_print_blkptr(&bp[i], flags);
582944cd46caSbillm }
583044cd46caSbillm 
583144cd46caSbillm static void
583244cd46caSbillm zdb_dump_gbh(void *buf, int flags)
583344cd46caSbillm {
583444cd46caSbillm 	zdb_dump_indirect((blkptr_t *)buf, SPA_GBH_NBLKPTRS, flags);
583544cd46caSbillm }
583644cd46caSbillm 
583744cd46caSbillm static void
583844cd46caSbillm zdb_dump_block_raw(void *buf, uint64_t size, int flags)
583944cd46caSbillm {
584044cd46caSbillm 	if (flags & ZDB_FLAG_BSWAP)
584144cd46caSbillm 		byteswap_uint64_array(buf, size);
5842b24ab676SJeff Bonwick 	(void) write(1, buf, size);
584344cd46caSbillm }
584444cd46caSbillm 
584544cd46caSbillm static void
584644cd46caSbillm zdb_dump_block(char *label, void *buf, uint64_t size, int flags)
584744cd46caSbillm {
584844cd46caSbillm 	uint64_t *d = (uint64_t *)buf;
58493f7978d0SAlan Somers 	unsigned nwords = size / sizeof (uint64_t);
585044cd46caSbillm 	int do_bswap = !!(flags & ZDB_FLAG_BSWAP);
58513f7978d0SAlan Somers 	unsigned i, j;
58523f7978d0SAlan Somers 	const char *hdr;
58533f7978d0SAlan Somers 	char *c;
585444cd46caSbillm 
585544cd46caSbillm 
585644cd46caSbillm 	if (do_bswap)
585744cd46caSbillm 		hdr = " 7 6 5 4 3 2 1 0   f e d c b a 9 8";
585844cd46caSbillm 	else
585944cd46caSbillm 		hdr = " 0 1 2 3 4 5 6 7   8 9 a b c d e f";
586044cd46caSbillm 
586144cd46caSbillm 	(void) printf("\n%s\n%6s   %s  0123456789abcdef\n", label, "", hdr);
586244cd46caSbillm 
586344cd46caSbillm 	for (i = 0; i < nwords; i += 2) {
586444cd46caSbillm 		(void) printf("%06llx:  %016llx  %016llx  ",
586544cd46caSbillm 		    (u_longlong_t)(i * sizeof (uint64_t)),
586644cd46caSbillm 		    (u_longlong_t)(do_bswap ? BSWAP_64(d[i]) : d[i]),
586744cd46caSbillm 		    (u_longlong_t)(do_bswap ? BSWAP_64(d[i + 1]) : d[i + 1]));
586844cd46caSbillm 
586944cd46caSbillm 		c = (char *)&d[i];
587044cd46caSbillm 		for (j = 0; j < 2 * sizeof (uint64_t); j++)
587144cd46caSbillm 			(void) printf("%c", isprint(c[j]) ? c[j] : '.');
587244cd46caSbillm 		(void) printf("\n");
587344cd46caSbillm 	}
587444cd46caSbillm }
587544cd46caSbillm 
587644cd46caSbillm /*
587744cd46caSbillm  * There are two acceptable formats:
587844cd46caSbillm  *	leaf_name	  - For example: c1t0d0 or /tmp/ztest.0a
587944cd46caSbillm  *	child[.child]*    - For example: 0.1.1
588044cd46caSbillm  *
588144cd46caSbillm  * The second form can be used to specify arbitrary vdevs anywhere
588244cd46caSbillm  * in the heirarchy.  For example, in a pool with a mirror of
588344cd46caSbillm  * RAID-Zs, you can specify either RAID-Z vdev with 0.0 or 0.1 .
588444cd46caSbillm  */
588544cd46caSbillm static vdev_t *
58863f7978d0SAlan Somers zdb_vdev_lookup(vdev_t *vdev, const char *path)
588744cd46caSbillm {
588844cd46caSbillm 	char *s, *p, *q;
58893f7978d0SAlan Somers 	unsigned i;
589044cd46caSbillm 
589144cd46caSbillm 	if (vdev == NULL)
589244cd46caSbillm 		return (NULL);
589344cd46caSbillm 
589444cd46caSbillm 	/* First, assume the x.x.x.x format */
58953f7978d0SAlan Somers 	i = strtoul(path, &s, 10);
589644cd46caSbillm 	if (s == path || (s && *s != '.' && *s != '\0'))
589744cd46caSbillm 		goto name;
58983f7978d0SAlan Somers 	if (i >= vdev->vdev_children)
589944cd46caSbillm 		return (NULL);
590044cd46caSbillm 
590144cd46caSbillm 	vdev = vdev->vdev_child[i];
590244cd46caSbillm 	if (*s == '\0')
590344cd46caSbillm 		return (vdev);
590444cd46caSbillm 	return (zdb_vdev_lookup(vdev, s+1));
590544cd46caSbillm 
590644cd46caSbillm name:
590744cd46caSbillm 	for (i = 0; i < vdev->vdev_children; i++) {
590844cd46caSbillm 		vdev_t *vc = vdev->vdev_child[i];
590944cd46caSbillm 
591044cd46caSbillm 		if (vc->vdev_path == NULL) {
591144cd46caSbillm 			vc = zdb_vdev_lookup(vc, path);
591244cd46caSbillm 			if (vc == NULL)
591344cd46caSbillm 				continue;
591444cd46caSbillm 			else
591544cd46caSbillm 				return (vc);
591644cd46caSbillm 		}
591744cd46caSbillm 
591844cd46caSbillm 		p = strrchr(vc->vdev_path, '/');
591944cd46caSbillm 		p = p ? p + 1 : vc->vdev_path;
592044cd46caSbillm 		q = &vc->vdev_path[strlen(vc->vdev_path) - 2];
592144cd46caSbillm 
592244cd46caSbillm 		if (strcmp(vc->vdev_path, path) == 0)
592344cd46caSbillm 			return (vc);
592444cd46caSbillm 		if (strcmp(p, path) == 0)
592544cd46caSbillm 			return (vc);
592644cd46caSbillm 		if (strcmp(q, "s0") == 0 && strncmp(p, path, q - p) == 0)
592744cd46caSbillm 			return (vc);
592844cd46caSbillm 	}
592944cd46caSbillm 
593044cd46caSbillm 	return (NULL);
593144cd46caSbillm }
593244cd46caSbillm 
5933770499e1SDan Kimmel /* ARGSUSED */
5934770499e1SDan Kimmel static int
5935770499e1SDan Kimmel random_get_pseudo_bytes_cb(void *buf, size_t len, void *unused)
5936770499e1SDan Kimmel {
5937770499e1SDan Kimmel 	return (random_get_pseudo_bytes(buf, len));
5938770499e1SDan Kimmel }
5939770499e1SDan Kimmel 
594044cd46caSbillm /*
594144cd46caSbillm  * Read a block from a pool and print it out.  The syntax of the
594244cd46caSbillm  * block descriptor is:
594344cd46caSbillm  *
594444cd46caSbillm  *	pool:vdev_specifier:offset:size[:flags]
594544cd46caSbillm  *
594644cd46caSbillm  *	pool           - The name of the pool you wish to read from
594744cd46caSbillm  *	vdev_specifier - Which vdev (see comment for zdb_vdev_lookup)
594844cd46caSbillm  *	offset         - offset, in hex, in bytes
594944cd46caSbillm  *	size           - Amount of data to read, in hex, in bytes
595044cd46caSbillm  *	flags          - A string of characters specifying options
595144cd46caSbillm  *		 b: Decode a blkptr at given offset within block
595244cd46caSbillm  *		*c: Calculate and display checksums
5953b24ab676SJeff Bonwick  *		 d: Decompress data before dumping
595444cd46caSbillm  *		 e: Byteswap data before dumping
5955b24ab676SJeff Bonwick  *		 g: Display data as a gang block header
5956b24ab676SJeff Bonwick  *		 i: Display as an indirect block
595744cd46caSbillm  *		 p: Do I/O to physical offset
595844cd46caSbillm  *		 r: Dump raw data to stdout
595944cd46caSbillm  *
596044cd46caSbillm  *              * = not yet implemented
596144cd46caSbillm  */
596244cd46caSbillm static void
596307428bdfSVictor Latushkin zdb_read_block(char *thing, spa_t *spa)
596444cd46caSbillm {
5965b24ab676SJeff Bonwick 	blkptr_t blk, *bp = &blk;
5966b24ab676SJeff Bonwick 	dva_t *dva = bp->blk_dva;
596744cd46caSbillm 	int flags = 0;
5968b24ab676SJeff Bonwick 	uint64_t offset = 0, size = 0, psize = 0, lsize = 0, blkptr_offset = 0;
596944cd46caSbillm 	zio_t *zio;
597044cd46caSbillm 	vdev_t *vd;
5971770499e1SDan Kimmel 	abd_t *pabd;
5972770499e1SDan Kimmel 	void *lbuf, *buf;
59733f7978d0SAlan Somers 	const char *s, *vdev;
59743f7978d0SAlan Somers 	char *p, *dup, *flagstr;
5975b24ab676SJeff Bonwick 	int i, error;
597644cd46caSbillm 
597744cd46caSbillm 	dup = strdup(thing);
597844cd46caSbillm 	s = strtok(dup, ":");
597944cd46caSbillm 	vdev = s ? s : "";
598044cd46caSbillm 	s = strtok(NULL, ":");
598144cd46caSbillm 	offset = strtoull(s ? s : "", NULL, 16);
598244cd46caSbillm 	s = strtok(NULL, ":");
598344cd46caSbillm 	size = strtoull(s ? s : "", NULL, 16);
598444cd46caSbillm 	s = strtok(NULL, ":");
59853f7978d0SAlan Somers 	if (s)
59863f7978d0SAlan Somers 		flagstr = strdup(s);
59873f7978d0SAlan Somers 	else
59883f7978d0SAlan Somers 		flagstr = strdup("");
598944cd46caSbillm 
599044cd46caSbillm 	s = NULL;
599144cd46caSbillm 	if (size == 0)
599244cd46caSbillm 		s = "size must not be zero";
599344cd46caSbillm 	if (!IS_P2ALIGNED(size, DEV_BSIZE))
599444cd46caSbillm 		s = "size must be a multiple of sector size";
599544cd46caSbillm 	if (!IS_P2ALIGNED(offset, DEV_BSIZE))
599644cd46caSbillm 		s = "offset must be a multiple of sector size";
599744cd46caSbillm 	if (s) {
599844cd46caSbillm 		(void) printf("Invalid block specifier: %s  - %s\n", thing, s);
599944cd46caSbillm 		free(dup);
600044cd46caSbillm 		return;
600144cd46caSbillm 	}
600244cd46caSbillm 
600344cd46caSbillm 	for (s = strtok(flagstr, ":"); s; s = strtok(NULL, ":")) {
600444cd46caSbillm 		for (i = 0; flagstr[i]; i++) {
60055ad82045Snd 			int bit = flagbits[(uchar_t)flagstr[i]];
600644cd46caSbillm 
600744cd46caSbillm 			if (bit == 0) {
600844cd46caSbillm 				(void) printf("***Invalid flag: %c\n",
600944cd46caSbillm 				    flagstr[i]);
601044cd46caSbillm 				continue;
601144cd46caSbillm 			}
601244cd46caSbillm 			flags |= bit;
601344cd46caSbillm 
601444cd46caSbillm 			/* If it's not something with an argument, keep going */
6015b24ab676SJeff Bonwick 			if ((bit & (ZDB_FLAG_CHECKSUM |
601644cd46caSbillm 			    ZDB_FLAG_PRINT_BLKPTR)) == 0)
601744cd46caSbillm 				continue;
601844cd46caSbillm 
601944cd46caSbillm 			p = &flagstr[i + 1];
602044cd46caSbillm 			if (bit == ZDB_FLAG_PRINT_BLKPTR)
602144cd46caSbillm 				blkptr_offset = strtoull(p, &p, 16);
602244cd46caSbillm 			if (*p != ':' && *p != '\0') {
602344cd46caSbillm 				(void) printf("***Invalid flag arg: '%s'\n", s);
602444cd46caSbillm 				free(dup);
602544cd46caSbillm 				return;
602644cd46caSbillm 			}
602744cd46caSbillm 		}
602844cd46caSbillm 	}
60293f7978d0SAlan Somers 	free(flagstr);
603044cd46caSbillm 
603144cd46caSbillm 	vd = zdb_vdev_lookup(spa->spa_root_vdev, vdev);
603244cd46caSbillm 	if (vd == NULL) {
603344cd46caSbillm 		(void) printf("***Invalid vdev: %s\n", vdev);
603444cd46caSbillm 		free(dup);
603544cd46caSbillm 		return;
603644cd46caSbillm 	} else {
603744cd46caSbillm 		if (vd->vdev_path)
6038b24ab676SJeff Bonwick 			(void) fprintf(stderr, "Found vdev: %s\n",
6039b24ab676SJeff Bonwick 			    vd->vdev_path);
604044cd46caSbillm 		else
6041b24ab676SJeff Bonwick 			(void) fprintf(stderr, "Found vdev type: %s\n",
604244cd46caSbillm 			    vd->vdev_ops->vdev_op_type);
604344cd46caSbillm 	}
604444cd46caSbillm 
6045b24ab676SJeff Bonwick 	psize = size;
6046b24ab676SJeff Bonwick 	lsize = size;
604744cd46caSbillm 
6048770499e1SDan Kimmel 	pabd = abd_alloc_linear(SPA_MAXBLOCKSIZE, B_FALSE);
6049b24ab676SJeff Bonwick 	lbuf = umem_alloc(SPA_MAXBLOCKSIZE, UMEM_NOFAIL);
6050b24ab676SJeff Bonwick 
6051b24ab676SJeff Bonwick 	BP_ZERO(bp);
6052b24ab676SJeff Bonwick 
6053b24ab676SJeff Bonwick 	DVA_SET_VDEV(&dva[0], vd->vdev_id);
6054b24ab676SJeff Bonwick 	DVA_SET_OFFSET(&dva[0], offset);
6055b24ab676SJeff Bonwick 	DVA_SET_GANG(&dva[0], !!(flags & ZDB_FLAG_GBH));
6056b24ab676SJeff Bonwick 	DVA_SET_ASIZE(&dva[0], vdev_psize_to_asize(vd, psize));
6057b24ab676SJeff Bonwick 
6058b24ab676SJeff Bonwick 	BP_SET_BIRTH(bp, TXG_INITIAL, TXG_INITIAL);
6059b24ab676SJeff Bonwick 
6060b24ab676SJeff Bonwick 	BP_SET_LSIZE(bp, lsize);
6061b24ab676SJeff Bonwick 	BP_SET_PSIZE(bp, psize);
6062b24ab676SJeff Bonwick 	BP_SET_COMPRESS(bp, ZIO_COMPRESS_OFF);
6063b24ab676SJeff Bonwick 	BP_SET_CHECKSUM(bp, ZIO_CHECKSUM_OFF);
6064b24ab676SJeff Bonwick 	BP_SET_TYPE(bp, DMU_OT_NONE);
6065b24ab676SJeff Bonwick 	BP_SET_LEVEL(bp, 0);
6066b24ab676SJeff Bonwick 	BP_SET_DEDUP(bp, 0);
6067b24ab676SJeff Bonwick 	BP_SET_BYTEORDER(bp, ZFS_HOST_BYTEORDER);
606844cd46caSbillm 
6069e14bb325SJeff Bonwick 	spa_config_enter(spa, SCL_STATE, FTAG, RW_READER);
607044cd46caSbillm 	zio = zio_root(spa, NULL, NULL, 0);
6071b24ab676SJeff Bonwick 
6072b24ab676SJeff Bonwick 	if (vd == vd->vdev_top) {
6073b24ab676SJeff Bonwick 		/*
6074b24ab676SJeff Bonwick 		 * Treat this as a normal block read.
6075b24ab676SJeff Bonwick 		 */
6076770499e1SDan Kimmel 		zio_nowait(zio_read(zio, spa, bp, pabd, psize, NULL, NULL,
6077b24ab676SJeff Bonwick 		    ZIO_PRIORITY_SYNC_READ,
6078b24ab676SJeff Bonwick 		    ZIO_FLAG_CANFAIL | ZIO_FLAG_RAW, NULL));
6079b24ab676SJeff Bonwick 	} else {
6080b24ab676SJeff Bonwick 		/*
6081b24ab676SJeff Bonwick 		 * Treat this as a vdev child I/O.
6082b24ab676SJeff Bonwick 		 */
6083770499e1SDan Kimmel 		zio_nowait(zio_vdev_child_io(zio, bp, vd, offset, pabd,
6084770499e1SDan Kimmel 		    psize, ZIO_TYPE_READ, ZIO_PRIORITY_SYNC_READ,
6085b24ab676SJeff Bonwick 		    ZIO_FLAG_DONT_CACHE | ZIO_FLAG_DONT_QUEUE |
6086b24ab676SJeff Bonwick 		    ZIO_FLAG_DONT_PROPAGATE | ZIO_FLAG_DONT_RETRY |
60875cabbc6bSPrashanth Sreenivasa 		    ZIO_FLAG_CANFAIL | ZIO_FLAG_RAW | ZIO_FLAG_OPTIONAL,
60885cabbc6bSPrashanth Sreenivasa 		    NULL, NULL));
6089b24ab676SJeff Bonwick 	}
6090b24ab676SJeff Bonwick 
609144cd46caSbillm 	error = zio_wait(zio);
6092e14bb325SJeff Bonwick 	spa_config_exit(spa, SCL_STATE, FTAG);
609344cd46caSbillm 
609444cd46caSbillm 	if (error) {
609544cd46caSbillm 		(void) printf("Read of %s failed, error: %d\n", thing, error);
609644cd46caSbillm 		goto out;
609744cd46caSbillm 	}
609844cd46caSbillm 
6099b24ab676SJeff Bonwick 	if (flags & ZDB_FLAG_DECOMPRESS) {
6100b24ab676SJeff Bonwick 		/*
6101b24ab676SJeff Bonwick 		 * We don't know how the data was compressed, so just try
6102b24ab676SJeff Bonwick 		 * every decompress function at every inflated blocksize.
6103b24ab676SJeff Bonwick 		 */
6104b24ab676SJeff Bonwick 		enum zio_compress c;
6105b24ab676SJeff Bonwick 		void *pbuf2 = umem_alloc(SPA_MAXBLOCKSIZE, UMEM_NOFAIL);
6106b24ab676SJeff Bonwick 		void *lbuf2 = umem_alloc(SPA_MAXBLOCKSIZE, UMEM_NOFAIL);
6107b24ab676SJeff Bonwick 
6108770499e1SDan Kimmel 		abd_copy_to_buf(pbuf2, pabd, psize);
6109b24ab676SJeff Bonwick 
6110770499e1SDan Kimmel 		VERIFY0(abd_iterate_func(pabd, psize, SPA_MAXBLOCKSIZE - psize,
6111770499e1SDan Kimmel 		    random_get_pseudo_bytes_cb, NULL));
6112b24ab676SJeff Bonwick 
6113770499e1SDan Kimmel 		VERIFY0(random_get_pseudo_bytes((uint8_t *)pbuf2 + psize,
6114770499e1SDan Kimmel 		    SPA_MAXBLOCKSIZE - psize));
6115b24ab676SJeff Bonwick 
6116b24ab676SJeff Bonwick 		for (lsize = SPA_MAXBLOCKSIZE; lsize > psize;
6117b24ab676SJeff Bonwick 		    lsize -= SPA_MINBLOCKSIZE) {
6118b24ab676SJeff Bonwick 			for (c = 0; c < ZIO_COMPRESS_FUNCTIONS; c++) {
6119770499e1SDan Kimmel 				if (zio_decompress_data(c, pabd,
6120770499e1SDan Kimmel 				    lbuf, psize, lsize) == 0 &&
6121770499e1SDan Kimmel 				    zio_decompress_data_buf(c, pbuf2,
6122770499e1SDan Kimmel 				    lbuf2, psize, lsize) == 0 &&
6123b24ab676SJeff Bonwick 				    bcmp(lbuf, lbuf2, lsize) == 0)
6124b24ab676SJeff Bonwick 					break;
6125b24ab676SJeff Bonwick 			}
6126b24ab676SJeff Bonwick 			if (c != ZIO_COMPRESS_FUNCTIONS)
6127b24ab676SJeff Bonwick 				break;
6128b24ab676SJeff Bonwick 			lsize -= SPA_MINBLOCKSIZE;
6129b24ab676SJeff Bonwick 		}
6130b24ab676SJeff Bonwick 
6131b24ab676SJeff Bonwick 		umem_free(pbuf2, SPA_MAXBLOCKSIZE);
6132b24ab676SJeff Bonwick 		umem_free(lbuf2, SPA_MAXBLOCKSIZE);
6133b24ab676SJeff Bonwick 
6134b24ab676SJeff Bonwick 		if (lsize <= psize) {
6135b24ab676SJeff Bonwick 			(void) printf("Decompress of %s failed\n", thing);
6136b24ab676SJeff Bonwick 			goto out;
6137b24ab676SJeff Bonwick 		}
6138b24ab676SJeff Bonwick 		buf = lbuf;
6139b24ab676SJeff Bonwick 		size = lsize;
6140b24ab676SJeff Bonwick 	} else {
6141770499e1SDan Kimmel 		buf = abd_to_buf(pabd);
6142b24ab676SJeff Bonwick 		size = psize;
6143b24ab676SJeff Bonwick 	}
6144b24ab676SJeff Bonwick 
614544cd46caSbillm 	if (flags & ZDB_FLAG_PRINT_BLKPTR)
614644cd46caSbillm 		zdb_print_blkptr((blkptr_t *)(void *)
614744cd46caSbillm 		    ((uintptr_t)buf + (uintptr_t)blkptr_offset), flags);
614844cd46caSbillm 	else if (flags & ZDB_FLAG_RAW)
614944cd46caSbillm 		zdb_dump_block_raw(buf, size, flags);
615044cd46caSbillm 	else if (flags & ZDB_FLAG_INDIRECT)
615144cd46caSbillm 		zdb_dump_indirect((blkptr_t *)buf, size / sizeof (blkptr_t),
615244cd46caSbillm 		    flags);
615344cd46caSbillm 	else if (flags & ZDB_FLAG_GBH)
615444cd46caSbillm 		zdb_dump_gbh(buf, flags);
615544cd46caSbillm 	else
615644cd46caSbillm 		zdb_dump_block(thing, buf, size, flags);
615744cd46caSbillm 
615844cd46caSbillm out:
6159770499e1SDan Kimmel 	abd_free(pabd);
6160b24ab676SJeff Bonwick 	umem_free(lbuf, SPA_MAXBLOCKSIZE);
616144cd46caSbillm 	free(dup);
616244cd46caSbillm }
616344cd46caSbillm 
61644923c69fSMatthew Ahrens static void
61654923c69fSMatthew Ahrens zdb_embedded_block(char *thing)
61664923c69fSMatthew Ahrens {
61673f7978d0SAlan Somers 	blkptr_t bp;
61684923c69fSMatthew Ahrens 	unsigned long long *words = (void *)&bp;
616903a4c2f4SJorgen Lundman 	char *buf;
61704923c69fSMatthew Ahrens 	int err;
61714923c69fSMatthew Ahrens 
61723f7978d0SAlan Somers 	bzero(&bp, sizeof (bp));
61734923c69fSMatthew Ahrens 	err = sscanf(thing, "%llx:%llx:%llx:%llx:%llx:%llx:%llx:%llx:"
61744923c69fSMatthew Ahrens 	    "%llx:%llx:%llx:%llx:%llx:%llx:%llx:%llx",
61754923c69fSMatthew Ahrens 	    words + 0, words + 1, words + 2, words + 3,
61764923c69fSMatthew Ahrens 	    words + 4, words + 5, words + 6, words + 7,
61774923c69fSMatthew Ahrens 	    words + 8, words + 9, words + 10, words + 11,
61784923c69fSMatthew Ahrens 	    words + 12, words + 13, words + 14, words + 15);
61794923c69fSMatthew Ahrens 	if (err != 16) {
618003a4c2f4SJorgen Lundman 		(void) fprintf(stderr, "invalid input format\n");
61814923c69fSMatthew Ahrens 		exit(1);
61824923c69fSMatthew Ahrens 	}
61834923c69fSMatthew Ahrens 	ASSERT3U(BPE_GET_LSIZE(&bp), <=, SPA_MAXBLOCKSIZE);
618403a4c2f4SJorgen Lundman 	buf = malloc(SPA_MAXBLOCKSIZE);
618503a4c2f4SJorgen Lundman 	if (buf == NULL) {
618603a4c2f4SJorgen Lundman 		(void) fprintf(stderr, "out of memory\n");
618703a4c2f4SJorgen Lundman 		exit(1);
618803a4c2f4SJorgen Lundman 	}
61894923c69fSMatthew Ahrens 	err = decode_embedded_bp(&bp, buf, BPE_GET_LSIZE(&bp));
61904923c69fSMatthew Ahrens 	if (err != 0) {
619103a4c2f4SJorgen Lundman 		(void) fprintf(stderr, "decode failed: %u\n", err);
61924923c69fSMatthew Ahrens 		exit(1);
61934923c69fSMatthew Ahrens 	}
61944923c69fSMatthew Ahrens 	zdb_dump_block_raw(buf, BPE_GET_LSIZE(&bp), 0);
619503a4c2f4SJorgen Lundman 	free(buf);
61964923c69fSMatthew Ahrens }
61974923c69fSMatthew Ahrens 
6198fa9e4066Sahrens int
6199fa9e4066Sahrens main(int argc, char **argv)
6200fa9e4066Sahrens {
62013f7978d0SAlan Somers 	int c;
6202fa9e4066Sahrens 	struct rlimit rl = { 1024, 1024 };
62033ad6c7f9SVictor Latushkin 	spa_t *spa = NULL;
6204fa9e4066Sahrens 	objset_t *os = NULL;
6205fa9e4066Sahrens 	int dump_all = 1;
6206fa9e4066Sahrens 	int verbose = 0;
6207c8ee1847SVictor Latushkin 	int error = 0;
62083ad6c7f9SVictor Latushkin 	char **searchdirs = NULL;
62093ad6c7f9SVictor Latushkin 	int nsearch = 0;
6210e0f1c0afSOlaf Faaland 	char *target, *target_pool;
6211468c413aSTim Haley 	nvlist_t *policy = NULL;
6212468c413aSTim Haley 	uint64_t max_txg = UINT64_MAX;
6213dfd5965fSRichard Yao 	int flags = ZFS_IMPORT_MISSING_LOG;
6214c8ee1847SVictor Latushkin 	int rewind = ZPOOL_NEVER_REWIND;
6215ae24175bSCyril Plisko 	char *spa_config_path_env;
6216b702644aSTim Chase 	boolean_t target_is_spa = B_TRUE;
621786714001SSerapheim Dimitropoulos 	nvlist_t *cfg = NULL;
6218fa9e4066Sahrens 
6219fa9e4066Sahrens 	(void) setrlimit(RLIMIT_NOFILE, &rl);
6220004388ebScasper 	(void) enable_extended_FILE_stdio(-1, -1);
6221fa9e4066Sahrens 
6222fa9e4066Sahrens 	dprintf_setup(&argc, argv);
6223fa9e4066Sahrens 
6224ae24175bSCyril Plisko 	/*
6225ae24175bSCyril Plisko 	 * If there is an environment variable SPA_CONFIG_PATH it overrides
6226ae24175bSCyril Plisko 	 * default spa_config_path setting. If -U flag is specified it will
6227ae24175bSCyril Plisko 	 * override this environment variable settings once again.
6228ae24175bSCyril Plisko 	 */
6229ae24175bSCyril Plisko 	spa_config_path_env = getenv("SPA_CONFIG_PATH");
6230ae24175bSCyril Plisko 	if (spa_config_path_env != NULL)
6231ae24175bSCyril Plisko 		spa_config_path = spa_config_path_env;
6232ae24175bSCyril Plisko 
62334d7988d6SPaul Dagnelie 	/*
62344d7988d6SPaul Dagnelie 	 * For performance reasons, we set this tunable down. We do so before
62354d7988d6SPaul Dagnelie 	 * the arg parsing section so that the user can override this value if
62364d7988d6SPaul Dagnelie 	 * they choose.
62374d7988d6SPaul Dagnelie 	 */
62384d7988d6SPaul Dagnelie 	zfs_btree_verify_intensity = 3;
62394d7988d6SPaul Dagnelie 
6240df15e419SMatthew Ahrens 	while ((c = getopt(argc, argv,
624186714001SSerapheim Dimitropoulos 	    "AbcCdDeEFGhiI:klLmMo:Op:PqRsSt:uU:vVx:X")) != -1) {
6242fa9e4066Sahrens 		switch (c) {
6243fa9e4066Sahrens 		case 'b':
6244fa9e4066Sahrens 		case 'c':
6245ed61ec1dSYuri Pankov 		case 'C':
6246b24ab676SJeff Bonwick 		case 'd':
6247ed61ec1dSYuri Pankov 		case 'D':
62484923c69fSMatthew Ahrens 		case 'E':
6249ed61ec1dSYuri Pankov 		case 'G':
6250b24ab676SJeff Bonwick 		case 'h':
6251b24ab676SJeff Bonwick 		case 'i':
6252b24ab676SJeff Bonwick 		case 'l':
6253d6e555bdSGeorge Wilson 		case 'm':
62542e4c9986SGeorge Wilson 		case 'M':
6255ed61ec1dSYuri Pankov 		case 'O':
625644cd46caSbillm 		case 'R':
6257ed61ec1dSYuri Pankov 		case 's':
6258b24ab676SJeff Bonwick 		case 'S':
6259ed61ec1dSYuri Pankov 		case 'u':
6260fa9e4066Sahrens 			dump_opt[c]++;
6261fa9e4066Sahrens 			dump_all = 0;
6262fa9e4066Sahrens 			break;
6263feef89cfSVictor Latushkin 		case 'A':
6264ed61ec1dSYuri Pankov 		case 'e':
6265c8ee1847SVictor Latushkin 		case 'F':
626686714001SSerapheim Dimitropoulos 		case 'k':
626782a0a985SVictor Latushkin 		case 'L':
62683f9d6ad7SLin Ling 		case 'P':
626964723e36SYuri Pankov 		case 'q':
6270ed61ec1dSYuri Pankov 		case 'X':
627182a0a985SVictor Latushkin 			dump_opt[c]++;
627282a0a985SVictor Latushkin 			break;
6273ed61ec1dSYuri Pankov 		/* NB: Sort single match options below. */
62742e4c9986SGeorge Wilson 		case 'I':
627531d7e8faSGeorge Wilson 			max_inflight = strtoull(optarg, NULL, 0);
627631d7e8faSGeorge Wilson 			if (max_inflight == 0) {
627731d7e8faSGeorge Wilson 				(void) fprintf(stderr, "maximum number "
627831d7e8faSGeorge Wilson 				    "of inflight I/Os must be greater "
627931d7e8faSGeorge Wilson 				    "than 0\n");
628031d7e8faSGeorge Wilson 				usage();
628131d7e8faSGeorge Wilson 			}
628231d7e8faSGeorge Wilson 			break;
6283ed61ec1dSYuri Pankov 		case 'o':
6284ed61ec1dSYuri Pankov 			error = set_global_var(optarg);
6285ed61ec1dSYuri Pankov 			if (error != 0)
6286ed61ec1dSYuri Pankov 				usage();
6287ed61ec1dSYuri Pankov 			break;
6288de6628f0Sck 		case 'p':
62893ad6c7f9SVictor Latushkin 			if (searchdirs == NULL) {
62903ad6c7f9SVictor Latushkin 				searchdirs = umem_alloc(sizeof (char *),
62913ad6c7f9SVictor Latushkin 				    UMEM_NOFAIL);
62923ad6c7f9SVictor Latushkin 			} else {
62933ad6c7f9SVictor Latushkin 				char **tmp = umem_alloc((nsearch + 1) *
62943ad6c7f9SVictor Latushkin 				    sizeof (char *), UMEM_NOFAIL);
62953ad6c7f9SVictor Latushkin 				bcopy(searchdirs, tmp, nsearch *
62963ad6c7f9SVictor Latushkin 				    sizeof (char *));
62973ad6c7f9SVictor Latushkin 				umem_free(searchdirs,
62983ad6c7f9SVictor Latushkin 				    nsearch * sizeof (char *));
62993ad6c7f9SVictor Latushkin 				searchdirs = tmp;
63003ad6c7f9SVictor Latushkin 			}
63013ad6c7f9SVictor Latushkin 			searchdirs[nsearch++] = optarg;
6302de6628f0Sck 			break;
63032e551927SVictor Latushkin 		case 't':
6304468c413aSTim Haley 			max_txg = strtoull(optarg, NULL, 0);
6305468c413aSTim Haley 			if (max_txg < TXG_INITIAL) {
63062e551927SVictor Latushkin 				(void) fprintf(stderr, "incorrect txg "
63072e551927SVictor Latushkin 				    "specified: %s\n", optarg);
63082e551927SVictor Latushkin 				usage();
63092e551927SVictor Latushkin 			}
63102e551927SVictor Latushkin 			break;
6311b24ab676SJeff Bonwick 		case 'U':
6312b24ab676SJeff Bonwick 			spa_config_path = optarg;
63134923c69fSMatthew Ahrens 			if (spa_config_path[0] != '/') {
63144923c69fSMatthew Ahrens 				(void) fprintf(stderr,
63154923c69fSMatthew Ahrens 				    "cachefile must be an absolute path "
63164923c69fSMatthew Ahrens 				    "(i.e. start with a slash)\n");
63174923c69fSMatthew Ahrens 				usage();
63184923c69fSMatthew Ahrens 			}
6319b24ab676SJeff Bonwick 			break;
63202e4c9986SGeorge Wilson 		case 'v':
63212e4c9986SGeorge Wilson 			verbose++;
63222e4c9986SGeorge Wilson 			break;
6323dfd5965fSRichard Yao 		case 'V':
6324dfd5965fSRichard Yao 			flags = ZFS_IMPORT_VERBATIM;
6325dfd5965fSRichard Yao 			break;
63262e4c9986SGeorge Wilson 		case 'x':
63272e4c9986SGeorge Wilson 			vn_dumpdir = optarg;
63282e4c9986SGeorge Wilson 			break;
6329fa9e4066Sahrens 		default:
6330fa9e4066Sahrens 			usage();
6331fa9e4066Sahrens 			break;
6332fa9e4066Sahrens 		}
6333fa9e4066Sahrens 	}
6334fa9e4066Sahrens 
63353ad6c7f9SVictor Latushkin 	if (!dump_opt['e'] && searchdirs != NULL) {
633688b7b0f2SMatthew Ahrens 		(void) fprintf(stderr, "-p option requires use of -e\n");
633788b7b0f2SMatthew Ahrens 		usage();
633888b7b0f2SMatthew Ahrens 	}
6339de6628f0Sck 
634006be9802SMatthew Ahrens 	/*
634106be9802SMatthew Ahrens 	 * ZDB does not typically re-read blocks; therefore limit the ARC
634206be9802SMatthew Ahrens 	 * to 256 MB, which can be used entirely for metadata.
634306be9802SMatthew Ahrens 	 */
634406be9802SMatthew Ahrens 	zfs_arc_max = zfs_arc_meta_limit = 256 * 1024 * 1024;
634506be9802SMatthew Ahrens 
6346f7950bf1SMatthew Ahrens 	/*
6347f7950bf1SMatthew Ahrens 	 * "zdb -c" uses checksum-verifying scrub i/os which are async reads.
6348f7950bf1SMatthew Ahrens 	 * "zdb -b" uses traversal prefetch which uses async reads.
6349f7950bf1SMatthew Ahrens 	 * For good performance, let several of them be active at once.
6350f7950bf1SMatthew Ahrens 	 */
6351f7950bf1SMatthew Ahrens 	zfs_vdev_async_read_max_active = 10;
6352f7950bf1SMatthew Ahrens 
63534dd77f9eSMatthew Ahrens 	/*
63544dd77f9eSMatthew Ahrens 	 * Disable reference tracking for better performance.
63554dd77f9eSMatthew Ahrens 	 */
63564dd77f9eSMatthew Ahrens 	reference_tracking_enable = B_FALSE;
63574dd77f9eSMatthew Ahrens 
6358e144c4e6SPavel Zakharov 	/*
6359e144c4e6SPavel Zakharov 	 * Do not fail spa_load when spa_load_verify fails. This is needed
6360e144c4e6SPavel Zakharov 	 * to load non-idle pools.
6361e144c4e6SPavel Zakharov 	 */
6362e144c4e6SPavel Zakharov 	spa_load_verify_dryrun = B_TRUE;
6363e144c4e6SPavel Zakharov 
6364fa9e4066Sahrens 	kernel_init(FREAD);
6365fa9e4066Sahrens 
6366b24ab676SJeff Bonwick 	if (dump_all)
6367b24ab676SJeff Bonwick 		verbose = MAX(verbose, 1);
6368b24ab676SJeff Bonwick 
6369fa9e4066Sahrens 	for (c = 0; c < 256; c++) {
637086714001SSerapheim Dimitropoulos 		if (dump_all && strchr("AeEFklLOPRSX", c) == NULL)
6371fa9e4066Sahrens 			dump_opt[c] = 1;
6372fa9e4066Sahrens 		if (dump_opt[c])
6373fa9e4066Sahrens 			dump_opt[c] += verbose;
6374fa9e4066Sahrens 	}
6375fa9e4066Sahrens 
6376feef89cfSVictor Latushkin 	aok = (dump_opt['A'] == 1) || (dump_opt['A'] > 2);
6377feef89cfSVictor Latushkin 	zfs_recover = (dump_opt['A'] > 1);
6378feef89cfSVictor Latushkin 
6379fa9e4066Sahrens 	argc -= optind;
6380fa9e4066Sahrens 	argv += optind;
6381fa9e4066Sahrens 
638207428bdfSVictor Latushkin 	if (argc < 2 && dump_opt['R'])
638307428bdfSVictor Latushkin 		usage();
63844923c69fSMatthew Ahrens 
63854923c69fSMatthew Ahrens 	if (dump_opt['E']) {
63864923c69fSMatthew Ahrens 		if (argc != 1)
63874923c69fSMatthew Ahrens 			usage();
63884923c69fSMatthew Ahrens 		zdb_embedded_block(argv[0]);
63894923c69fSMatthew Ahrens 		return (0);
63904923c69fSMatthew Ahrens 	}
63914923c69fSMatthew Ahrens 
6392fa9e4066Sahrens 	if (argc < 1) {
63933ad6c7f9SVictor Latushkin 		if (!dump_opt['e'] && dump_opt['C']) {
6394e829d913Sck 			dump_cachefile(spa_config_path);
6395fa9e4066Sahrens 			return (0);
6396fa9e4066Sahrens 		}
6397fa9e4066Sahrens 		usage();
6398fa9e4066Sahrens 	}
6399fa9e4066Sahrens 
640064723e36SYuri Pankov 	if (dump_opt['l'])
640164723e36SYuri Pankov 		return (dump_label(argv[0]));
6402fa9e4066Sahrens 
6403ed61ec1dSYuri Pankov 	if (dump_opt['O']) {
6404ed61ec1dSYuri Pankov 		if (argc != 2)
6405ed61ec1dSYuri Pankov 			usage();
6406ed61ec1dSYuri Pankov 		dump_opt['v'] = verbose + 3;
6407ed61ec1dSYuri Pankov 		return (dump_path(argv[0], argv[1]));
6408ed61ec1dSYuri Pankov 	}
6409ed61ec1dSYuri Pankov 
6410c8ee1847SVictor Latushkin 	if (dump_opt['X'] || dump_opt['F'])
6411c8ee1847SVictor Latushkin 		rewind = ZPOOL_DO_REWIND |
6412c8ee1847SVictor Latushkin 		    (dump_opt['X'] ? ZPOOL_EXTREME_REWIND : 0);
6413c8ee1847SVictor Latushkin 
6414c8ee1847SVictor Latushkin 	if (nvlist_alloc(&policy, NV_UNIQUE_NAME_TYPE, 0) != 0 ||
64155dafeea3SPavel Zakharov 	    nvlist_add_uint64(policy, ZPOOL_LOAD_REQUEST_TXG, max_txg) != 0 ||
64165dafeea3SPavel Zakharov 	    nvlist_add_uint32(policy, ZPOOL_LOAD_REWIND_POLICY, rewind) != 0)
6417c8ee1847SVictor Latushkin 		fatal("internal error: %s", strerror(ENOMEM));
6418c8ee1847SVictor Latushkin 
6419c5904d13Seschrock 	error = 0;
64203ad6c7f9SVictor Latushkin 	target = argv[0];
6421990b4856Slling 
6422e0f1c0afSOlaf Faaland 	if (strpbrk(target, "/@") != NULL) {
6423e0f1c0afSOlaf Faaland 		size_t targetlen;
6424e0f1c0afSOlaf Faaland 
6425e0f1c0afSOlaf Faaland 		target_pool = strdup(target);
6426e0f1c0afSOlaf Faaland 		*strpbrk(target_pool, "/@") = '\0';
6427e0f1c0afSOlaf Faaland 
6428e0f1c0afSOlaf Faaland 		target_is_spa = B_FALSE;
6429e0f1c0afSOlaf Faaland 		targetlen = strlen(target);
6430e0f1c0afSOlaf Faaland 		if (targetlen && target[targetlen - 1] == '/')
6431e0f1c0afSOlaf Faaland 			target[targetlen - 1] = '\0';
6432e0f1c0afSOlaf Faaland 	} else {
6433e0f1c0afSOlaf Faaland 		target_pool = target;
6434e0f1c0afSOlaf Faaland 	}
6435e0f1c0afSOlaf Faaland 
64363ad6c7f9SVictor Latushkin 	if (dump_opt['e']) {
6437e0f1c0afSOlaf Faaland 		importargs_t args = { 0 };
6438990b4856Slling 
6439e0f1c0afSOlaf Faaland 		args.paths = nsearch;
6440e0f1c0afSOlaf Faaland 		args.path = searchdirs;
6441e0f1c0afSOlaf Faaland 		args.can_be_active = B_TRUE;
6442e0f1c0afSOlaf Faaland 
6443d8ab6e12SDon Brady 		error = zpool_find_config(NULL, target_pool, &cfg, &args,
6444d8ab6e12SDon Brady 		    &libzpool_config_ops);
6445e0f1c0afSOlaf Faaland 
6446e0f1c0afSOlaf Faaland 		if (error == 0) {
644786714001SSerapheim Dimitropoulos 
6448c8ee1847SVictor Latushkin 			if (nvlist_add_nvlist(cfg,
64495dafeea3SPavel Zakharov 			    ZPOOL_LOAD_POLICY, policy) != 0) {
6450468c413aSTim Haley 				fatal("can't open '%s': %s",
6451468c413aSTim Haley 				    target, strerror(ENOMEM));
6452468c413aSTim Haley 			}
6453e0f1c0afSOlaf Faaland 
6454e0f1c0afSOlaf Faaland 			if (dump_opt['C'] > 1) {
6455e0f1c0afSOlaf Faaland 				(void) printf("\nConfiguration for import:\n");
6456e0f1c0afSOlaf Faaland 				dump_nvlist(cfg, 8);
6457e0f1c0afSOlaf Faaland 			}
6458e0f1c0afSOlaf Faaland 
6459e0f1c0afSOlaf Faaland 			/*
6460e0f1c0afSOlaf Faaland 			 * Disable the activity check to allow examination of
6461e0f1c0afSOlaf Faaland 			 * active pools.
6462e0f1c0afSOlaf Faaland 			 */
6463e0f1c0afSOlaf Faaland 			error = spa_import(target_pool, cfg, NULL,
6464e0f1c0afSOlaf Faaland 			    flags | ZFS_IMPORT_SKIP_MMP);
6465de6628f0Sck 		}
6466c5904d13Seschrock 	}
6467c5904d13Seschrock 
646886714001SSerapheim Dimitropoulos 	char *checkpoint_pool = NULL;
646986714001SSerapheim Dimitropoulos 	char *checkpoint_target = NULL;
647086714001SSerapheim Dimitropoulos 	if (dump_opt['k']) {
647186714001SSerapheim Dimitropoulos 		checkpoint_pool = import_checkpointed_state(target, cfg,
647286714001SSerapheim Dimitropoulos 		    &checkpoint_target);
647386714001SSerapheim Dimitropoulos 
647486714001SSerapheim Dimitropoulos 		if (checkpoint_target != NULL)
647586714001SSerapheim Dimitropoulos 			target = checkpoint_target;
647686714001SSerapheim Dimitropoulos 
647786714001SSerapheim Dimitropoulos 	}
647886714001SSerapheim Dimitropoulos 
6479c5904d13Seschrock 	if (error == 0) {
648086714001SSerapheim Dimitropoulos 		if (dump_opt['k'] && (target_is_spa || dump_opt['R'])) {
648186714001SSerapheim Dimitropoulos 			ASSERT(checkpoint_pool != NULL);
648286714001SSerapheim Dimitropoulos 			ASSERT(checkpoint_target == NULL);
648386714001SSerapheim Dimitropoulos 
648486714001SSerapheim Dimitropoulos 			error = spa_open(checkpoint_pool, &spa, FTAG);
648586714001SSerapheim Dimitropoulos 			if (error != 0) {
648686714001SSerapheim Dimitropoulos 				fatal("Tried to open pool \"%s\" but "
648786714001SSerapheim Dimitropoulos 				    "spa_open() failed with error %d\n",
648886714001SSerapheim Dimitropoulos 				    checkpoint_pool, error);
648986714001SSerapheim Dimitropoulos 			}
649086714001SSerapheim Dimitropoulos 
649186714001SSerapheim Dimitropoulos 		} else if (target_is_spa || dump_opt['R']) {
6492e0f1c0afSOlaf Faaland 			zdb_set_skip_mmp(target);
649380eb36f2SGeorge Wilson 			error = spa_open_rewind(target, &spa, FTAG, policy,
649480eb36f2SGeorge Wilson 			    NULL);
64958f18d1faSGeorge Wilson 			if (error) {
64968f18d1faSGeorge Wilson 				/*
64978f18d1faSGeorge Wilson 				 * If we're missing the log device then
64988f18d1faSGeorge Wilson 				 * try opening the pool after clearing the
64998f18d1faSGeorge Wilson 				 * log state.
65008f18d1faSGeorge Wilson 				 */
65018f18d1faSGeorge Wilson 				mutex_enter(&spa_namespace_lock);
65023ad6c7f9SVictor Latushkin 				if ((spa = spa_lookup(target)) != NULL &&
65038f18d1faSGeorge Wilson 				    spa->spa_log_state == SPA_LOG_MISSING) {
65048f18d1faSGeorge Wilson 					spa->spa_log_state = SPA_LOG_CLEAR;
65058f18d1faSGeorge Wilson 					error = 0;
65068f18d1faSGeorge Wilson 				}
65078f18d1faSGeorge Wilson 				mutex_exit(&spa_namespace_lock);
65088f18d1faSGeorge Wilson 
650980eb36f2SGeorge Wilson 				if (!error) {
651080eb36f2SGeorge Wilson 					error = spa_open_rewind(target, &spa,
651180eb36f2SGeorge Wilson 					    FTAG, policy, NULL);
651280eb36f2SGeorge Wilson 				}
65138f18d1faSGeorge Wilson 			}
651407428bdfSVictor Latushkin 		} else {
6515e0f1c0afSOlaf Faaland 			zdb_set_skip_mmp(target);
6516ed61ec1dSYuri Pankov 			error = open_objset(target, DMU_OST_ANY, FTAG, &os);
6517c5904d13Seschrock 		}
6518fa9e4066Sahrens 	}
651980eb36f2SGeorge Wilson 	nvlist_free(policy);
652080eb36f2SGeorge Wilson 
6521fa9e4066Sahrens 	if (error)
65223ad6c7f9SVictor Latushkin 		fatal("can't open '%s': %s", target, strerror(error));
6523fa9e4066Sahrens 
6524fa9e4066Sahrens 	argv++;
652507428bdfSVictor Latushkin 	argc--;
652607428bdfSVictor Latushkin 	if (!dump_opt['R']) {
652707428bdfSVictor Latushkin 		if (argc > 0) {
652807428bdfSVictor Latushkin 			zopt_objects = argc;
652907428bdfSVictor Latushkin 			zopt_object = calloc(zopt_objects, sizeof (uint64_t));
65303f7978d0SAlan Somers 			for (unsigned i = 0; i < zopt_objects; i++) {
653107428bdfSVictor Latushkin 				errno = 0;
653207428bdfSVictor Latushkin 				zopt_object[i] = strtoull(argv[i], NULL, 0);
653307428bdfSVictor Latushkin 				if (zopt_object[i] == 0 && errno != 0)
653487219db7SVictor Latushkin 					fatal("bad number %s: %s",
653507428bdfSVictor Latushkin 					    argv[i], strerror(errno));
653607428bdfSVictor Latushkin 			}
6537fa9e4066Sahrens 		}
6538e690fb27SChristopher Siden 		if (os != NULL) {
6539e690fb27SChristopher Siden 			dump_dir(os);
6540e690fb27SChristopher Siden 		} else if (zopt_objects > 0 && !dump_opt['m']) {
6541e690fb27SChristopher Siden 			dump_dir(spa->spa_meta_objset);
6542e690fb27SChristopher Siden 		} else {
6543e690fb27SChristopher Siden 			dump_zpool(spa);
6544e690fb27SChristopher Siden 		}
6545fa9e4066Sahrens 	} else {
654607428bdfSVictor Latushkin 		flagbits['b'] = ZDB_FLAG_PRINT_BLKPTR;
654707428bdfSVictor Latushkin 		flagbits['c'] = ZDB_FLAG_CHECKSUM;
654807428bdfSVictor Latushkin 		flagbits['d'] = ZDB_FLAG_DECOMPRESS;
654907428bdfSVictor Latushkin 		flagbits['e'] = ZDB_FLAG_BSWAP;
655007428bdfSVictor Latushkin 		flagbits['g'] = ZDB_FLAG_GBH;
655107428bdfSVictor Latushkin 		flagbits['i'] = ZDB_FLAG_INDIRECT;
655207428bdfSVictor Latushkin 		flagbits['p'] = ZDB_FLAG_PHYS;
655307428bdfSVictor Latushkin 		flagbits['r'] = ZDB_FLAG_RAW;
655407428bdfSVictor Latushkin 
65553f7978d0SAlan Somers 		for (int i = 0; i < argc; i++)
655607428bdfSVictor Latushkin 			zdb_read_block(argv[i], spa);
6557fa9e4066Sahrens 	}
6558fa9e4066Sahrens 
655986714001SSerapheim Dimitropoulos 	if (dump_opt['k']) {
656086714001SSerapheim Dimitropoulos 		free(checkpoint_pool);
656186714001SSerapheim Dimitropoulos 		if (!target_is_spa)
656286714001SSerapheim Dimitropoulos 			free(checkpoint_target);
656386714001SSerapheim Dimitropoulos 	}
656486714001SSerapheim Dimitropoulos 
6565ed61ec1dSYuri Pankov 	if (os != NULL)
6566ed61ec1dSYuri Pankov 		close_objset(os, FTAG);
6567ed61ec1dSYuri Pankov 	else
6568ed61ec1dSYuri Pankov 		spa_close(spa, FTAG);
656907428bdfSVictor Latushkin 
6570e0d35c44Smarks 	fuid_table_destroy();
6571e0d35c44Smarks 
657229bdd2f9SPavel Zakharov 	dump_debug_buffer();
657329bdd2f9SPavel Zakharov 
6574fa9e4066Sahrens 	kernel_fini();
6575fa9e4066Sahrens 
657620b5dafbSPaul Dagnelie 	return (error);
6577fa9e4066Sahrens }
6578