xref: /illumos-gate/usr/src/cmd/zdb/zdb.c (revision 70163ac5)
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.
247802d7bfSMatthew Ahrens  * Copyright (c) 2011, 2014 by Delphix. All rights reserved.
25fa9e4066Sahrens  */
26fa9e4066Sahrens 
27fa9e4066Sahrens #include <stdio.h>
28490d05b9SMatthew Ahrens #include <unistd.h>
29004388ebScasper #include <stdio_ext.h>
30fa9e4066Sahrens #include <stdlib.h>
3144cd46caSbillm #include <ctype.h>
32fa9e4066Sahrens #include <sys/zfs_context.h>
33fa9e4066Sahrens #include <sys/spa.h>
34fa9e4066Sahrens #include <sys/spa_impl.h>
35fa9e4066Sahrens #include <sys/dmu.h>
36fa9e4066Sahrens #include <sys/zap.h>
37fa9e4066Sahrens #include <sys/fs/zfs.h>
38fa9e4066Sahrens #include <sys/zfs_znode.h>
390a586ceaSMark Shellenbaum #include <sys/zfs_sa.h>
400a586ceaSMark Shellenbaum #include <sys/sa.h>
410a586ceaSMark Shellenbaum #include <sys/sa_impl.h>
42fa9e4066Sahrens #include <sys/vdev.h>
43fa9e4066Sahrens #include <sys/vdev_impl.h>
44fa9e4066Sahrens #include <sys/metaslab_impl.h>
45fa9e4066Sahrens #include <sys/dmu_objset.h>
46fa9e4066Sahrens #include <sys/dsl_dir.h>
47fa9e4066Sahrens #include <sys/dsl_dataset.h>
48fa9e4066Sahrens #include <sys/dsl_pool.h>
49fa9e4066Sahrens #include <sys/dbuf.h>
50fa9e4066Sahrens #include <sys/zil.h>
51fa9e4066Sahrens #include <sys/zil_impl.h>
52fa9e4066Sahrens #include <sys/stat.h>
53fa9e4066Sahrens #include <sys/resource.h>
54fa9e4066Sahrens #include <sys/dmu_traverse.h>
55fa9e4066Sahrens #include <sys/zio_checksum.h>
56fa9e4066Sahrens #include <sys/zio_compress.h>
57e0d35c44Smarks #include <sys/zfs_fuid.h>
5888b7b0f2SMatthew Ahrens #include <sys/arc.h>
59b24ab676SJeff Bonwick #include <sys/ddt.h>
60ad135b5dSChristopher Siden #include <sys/zfeature.h>
614445fffbSMatthew Ahrens #include <zfs_comutil.h>
62de6628f0Sck #undef ZFS_MAXNAMELEN
63de6628f0Sck #undef verify
64de6628f0Sck #include <libzfs.h>
65fa9e4066Sahrens 
66e690fb27SChristopher Siden #define	ZDB_COMPRESS_NAME(idx) ((idx) < ZIO_COMPRESS_FUNCTIONS ?	\
67e690fb27SChristopher Siden 	zio_compress_table[(idx)].ci_name : "UNKNOWN")
68e690fb27SChristopher Siden #define	ZDB_CHECKSUM_NAME(idx) ((idx) < ZIO_CHECKSUM_FUNCTIONS ?	\
69e690fb27SChristopher Siden 	zio_checksum_table[(idx)].ci_name : "UNKNOWN")
70e690fb27SChristopher Siden #define	ZDB_OT_NAME(idx) ((idx) < DMU_OT_NUMTYPES ?	\
71e690fb27SChristopher Siden 	dmu_ot[(idx)].ot_name : DMU_OT_IS_VALID(idx) ?	\
72e690fb27SChristopher Siden 	dmu_ot_byteswap[DMU_OT_BYTESWAP(idx)].ob_name : "UNKNOWN")
73e690fb27SChristopher Siden #define	ZDB_OT_TYPE(idx) ((idx) < DMU_OT_NUMTYPES ? (idx) :		\
74e690fb27SChristopher Siden 	(((idx) == DMU_OTN_ZAP_DATA || (idx) == DMU_OTN_ZAP_METADATA) ?	\
75e690fb27SChristopher Siden 	DMU_OT_ZAP_OTHER : DMU_OT_NUMTYPES))
766de8f417SVictor Latushkin 
77feef89cfSVictor Latushkin #ifndef lint
787fd05ac4SMatthew Ahrens extern boolean_t zfs_recover;
7906be9802SMatthew Ahrens extern uint64_t zfs_arc_max, zfs_arc_meta_limit;
80f7950bf1SMatthew Ahrens extern int zfs_vdev_async_read_max_active;
81feef89cfSVictor Latushkin #else
827fd05ac4SMatthew Ahrens boolean_t zfs_recover;
8306be9802SMatthew Ahrens uint64_t zfs_arc_max, zfs_arc_meta_limit;
84f7950bf1SMatthew Ahrens int zfs_vdev_async_read_max_active;
85feef89cfSVictor Latushkin #endif
86feef89cfSVictor Latushkin 
87fa9e4066Sahrens const char cmdname[] = "zdb";
88fa9e4066Sahrens uint8_t dump_opt[256];
89fa9e4066Sahrens 
90fa9e4066Sahrens typedef void object_viewer_t(objset_t *, uint64_t, void *data, size_t size);
91fa9e4066Sahrens 
92fa9e4066Sahrens extern void dump_intent_log(zilog_t *);
93fa9e4066Sahrens uint64_t *zopt_object = NULL;
94fa9e4066Sahrens int zopt_objects = 0;
95de6628f0Sck libzfs_handle_t *g_zfs;
9606be9802SMatthew Ahrens uint64_t max_inflight = 1000;
97fa9e4066Sahrens 
98fa9e4066Sahrens /*
99fa9e4066Sahrens  * These libumem hooks provide a reasonable set of defaults for the allocator's
100fa9e4066Sahrens  * debugging facilities.
101fa9e4066Sahrens  */
102fa9e4066Sahrens const char *
103fa9e4066Sahrens _umem_debug_init()
104fa9e4066Sahrens {
105fa9e4066Sahrens 	return ("default,verbose"); /* $UMEM_DEBUG setting */
106fa9e4066Sahrens }
107fa9e4066Sahrens 
108fa9e4066Sahrens const char *
109fa9e4066Sahrens _umem_logging_init(void)
110fa9e4066Sahrens {
111fa9e4066Sahrens 	return ("fail,contents"); /* $UMEM_LOGGING setting */
112fa9e4066Sahrens }
113fa9e4066Sahrens 
114fa9e4066Sahrens static void
115fa9e4066Sahrens usage(void)
116fa9e4066Sahrens {
117fa9e4066Sahrens 	(void) fprintf(stderr,
1182e4c9986SGeorge Wilson 	    "Usage: %s [-CumMdibcsDvhLXFPA] [-t txg] [-e [-p path...]] "
1192e4c9986SGeorge Wilson 	    "[-U config] [-I inflight I/Os] [-x dumpdir] poolname [object...]\n"
12031d7e8faSGeorge Wilson 	    "       %s [-divPA] [-e -p path...] [-U config] dataset "
12131d7e8faSGeorge Wilson 	    "[object...]\n"
1222e4c9986SGeorge Wilson 	    "       %s -mM [-LXFPA] [-t txg] [-e [-p path...]] [-U config] "
12390e894e2SRichard Lowe 	    "poolname [vdev [metaslab...]]\n"
12490e894e2SRichard Lowe 	    "       %s -R [-A] [-e [-p path...]] poolname "
12590e894e2SRichard Lowe 	    "vdev:offset:size[:flags]\n"
12631d7e8faSGeorge Wilson 	    "       %s -S [-PA] [-e [-p path...]] [-U config] poolname\n"
12790e894e2SRichard Lowe 	    "       %s -l [-uA] device\n"
12890e894e2SRichard Lowe 	    "       %s -C [-A] [-U config]\n\n",
129bbfd46c4SJeff Bonwick 	    cmdname, cmdname, cmdname, cmdname, cmdname, cmdname, cmdname);
1303ad6c7f9SVictor Latushkin 
1313ad6c7f9SVictor Latushkin 	(void) fprintf(stderr, "    Dataset name must include at least one "
1323ad6c7f9SVictor Latushkin 	    "separator character '/' or '@'\n");
1333ad6c7f9SVictor Latushkin 	(void) fprintf(stderr, "    If dataset name is specified, only that "
1343ad6c7f9SVictor Latushkin 	    "dataset is dumped\n");
1353ad6c7f9SVictor Latushkin 	(void) fprintf(stderr, "    If object numbers are specified, only "
1363ad6c7f9SVictor Latushkin 	    "those objects are dumped\n\n");
1373ad6c7f9SVictor Latushkin 	(void) fprintf(stderr, "    Options to control amount of output:\n");
1383ad6c7f9SVictor Latushkin 	(void) fprintf(stderr, "        -u uberblock\n");
1393ad6c7f9SVictor Latushkin 	(void) fprintf(stderr, "        -d dataset(s)\n");
1403ad6c7f9SVictor Latushkin 	(void) fprintf(stderr, "        -i intent logs\n");
14107428bdfSVictor Latushkin 	(void) fprintf(stderr, "        -C config (or cachefile if alone)\n");
1428f18d1faSGeorge Wilson 	(void) fprintf(stderr, "        -h pool history\n");
1433ad6c7f9SVictor Latushkin 	(void) fprintf(stderr, "        -b block statistics\n");
1443ad6c7f9SVictor Latushkin 	(void) fprintf(stderr, "        -m metaslabs\n");
1452e4c9986SGeorge Wilson 	(void) fprintf(stderr, "        -M metaslab groups\n");
1463ad6c7f9SVictor Latushkin 	(void) fprintf(stderr, "        -c checksum all metadata (twice for "
1476365109dSVictor Latushkin 	    "all data) blocks\n");
1483ad6c7f9SVictor Latushkin 	(void) fprintf(stderr, "        -s report stats on zdb's I/O\n");
149f0ba89beSJeff Bonwick 	(void) fprintf(stderr, "        -D dedup statistics\n");
150b24ab676SJeff Bonwick 	(void) fprintf(stderr, "        -S simulate dedup to measure effect\n");
1513ad6c7f9SVictor Latushkin 	(void) fprintf(stderr, "        -v verbose (applies to all others)\n");
152fa9e4066Sahrens 	(void) fprintf(stderr, "        -l dump label contents\n");
15382a0a985SVictor Latushkin 	(void) fprintf(stderr, "        -L disable leak tracking (do not "
15482a0a985SVictor Latushkin 	    "load spacemaps)\n");
155d41e7643Sek 	(void) fprintf(stderr, "        -R read and display block from a "
1563ad6c7f9SVictor Latushkin 	    "device\n\n");
1573ad6c7f9SVictor Latushkin 	(void) fprintf(stderr, "    Below options are intended for use "
158df15e419SMatthew Ahrens 	    "with other options:\n");
159feef89cfSVictor Latushkin 	(void) fprintf(stderr, "        -A ignore assertions (-A), enable "
160feef89cfSVictor Latushkin 	    "panic recovery (-AA) or both (-AAA)\n");
161c8ee1847SVictor Latushkin 	(void) fprintf(stderr, "        -F attempt automatic rewind within "
162c8ee1847SVictor Latushkin 	    "safe range of transaction groups\n");
1633ad6c7f9SVictor Latushkin 	(void) fprintf(stderr, "        -U <cachefile_path> -- use alternate "
1643ad6c7f9SVictor Latushkin 	    "cachefile\n");
165c8ee1847SVictor Latushkin 	(void) fprintf(stderr, "        -X attempt extreme rewind (does not "
166c8ee1847SVictor Latushkin 	    "work with dataset)\n");
1673ad6c7f9SVictor Latushkin 	(void) fprintf(stderr, "        -e pool is exported/destroyed/"
1683ad6c7f9SVictor Latushkin 	    "has altroot/not in a cachefile\n");
1693ad6c7f9SVictor Latushkin 	(void) fprintf(stderr, "        -p <path> -- use one or more with "
1703ad6c7f9SVictor Latushkin 	    "-e to specify path to vdev dir\n");
171df15e419SMatthew Ahrens 	(void) fprintf(stderr, "        -x <dumpdir> -- "
172df15e419SMatthew Ahrens 	    "dump all read blocks into specified directory\n");
17390e894e2SRichard Lowe 	(void) fprintf(stderr, "        -P print numbers in parseable form\n");
1743ad6c7f9SVictor Latushkin 	(void) fprintf(stderr, "        -t <txg> -- highest txg to use when "
1752e551927SVictor Latushkin 	    "searching for uberblocks\n");
1762e4c9986SGeorge Wilson 	(void) fprintf(stderr, "        -I <number of inflight I/Os> -- "
177df15e419SMatthew Ahrens 	    "specify the maximum number of "
178df15e419SMatthew Ahrens 	    "checksumming I/Os [default is 200]\n");
179fa9e4066Sahrens 	(void) fprintf(stderr, "Specify an option more than once (e.g. -bb) "
180fa9e4066Sahrens 	    "to make only that option verbose\n");
181fa9e4066Sahrens 	(void) fprintf(stderr, "Default is to dump everything non-verbosely\n");
182fa9e4066Sahrens 	exit(1);
183fa9e4066Sahrens }
184fa9e4066Sahrens 
185ccba0801SRich Morris /*
186ccba0801SRich Morris  * Called for usage errors that are discovered after a call to spa_open(),
187ccba0801SRich Morris  * dmu_bonus_hold(), or pool_match().  abort() is called for other errors.
188ccba0801SRich Morris  */
189ccba0801SRich Morris 
190fa9e4066Sahrens static void
191fa9e4066Sahrens fatal(const char *fmt, ...)
192fa9e4066Sahrens {
193fa9e4066Sahrens 	va_list ap;
194fa9e4066Sahrens 
195fa9e4066Sahrens 	va_start(ap, fmt);
196fa9e4066Sahrens 	(void) fprintf(stderr, "%s: ", cmdname);
197fa9e4066Sahrens 	(void) vfprintf(stderr, fmt, ap);
198fa9e4066Sahrens 	va_end(ap);
199fa9e4066Sahrens 	(void) fprintf(stderr, "\n");
200fa9e4066Sahrens 
201ccba0801SRich Morris 	exit(1);
202fa9e4066Sahrens }
203fa9e4066Sahrens 
204fa9e4066Sahrens /* ARGSUSED */
205fa9e4066Sahrens static void
206fa9e4066Sahrens dump_packed_nvlist(objset_t *os, uint64_t object, void *data, size_t size)
207fa9e4066Sahrens {
208fa9e4066Sahrens 	nvlist_t *nv;
209fa9e4066Sahrens 	size_t nvsize = *(uint64_t *)data;
210fa9e4066Sahrens 	char *packed = umem_alloc(nvsize, UMEM_NOFAIL);
211fa9e4066Sahrens 
2127bfdf011SNeil Perrin 	VERIFY(0 == dmu_read(os, object, 0, nvsize, packed, DMU_READ_PREFETCH));
213fa9e4066Sahrens 
214fa9e4066Sahrens 	VERIFY(nvlist_unpack(packed, nvsize, &nv, 0) == 0);
215fa9e4066Sahrens 
216fa9e4066Sahrens 	umem_free(packed, nvsize);
217fa9e4066Sahrens 
218fa9e4066Sahrens 	dump_nvlist(nv, 8);
219fa9e4066Sahrens 
220fa9e4066Sahrens 	nvlist_free(nv);
221fa9e4066Sahrens }
222fa9e4066Sahrens 
2234445fffbSMatthew Ahrens /* ARGSUSED */
2244445fffbSMatthew Ahrens static void
2254445fffbSMatthew Ahrens dump_history_offsets(objset_t *os, uint64_t object, void *data, size_t size)
2264445fffbSMatthew Ahrens {
2274445fffbSMatthew Ahrens 	spa_history_phys_t *shp = data;
2284445fffbSMatthew Ahrens 
2294445fffbSMatthew Ahrens 	if (shp == NULL)
2304445fffbSMatthew Ahrens 		return;
2314445fffbSMatthew Ahrens 
2324445fffbSMatthew Ahrens 	(void) printf("\t\tpool_create_len = %llu\n",
2334445fffbSMatthew Ahrens 	    (u_longlong_t)shp->sh_pool_create_len);
2344445fffbSMatthew Ahrens 	(void) printf("\t\tphys_max_off = %llu\n",
2354445fffbSMatthew Ahrens 	    (u_longlong_t)shp->sh_phys_max_off);
2364445fffbSMatthew Ahrens 	(void) printf("\t\tbof = %llu\n",
2374445fffbSMatthew Ahrens 	    (u_longlong_t)shp->sh_bof);
2384445fffbSMatthew Ahrens 	(void) printf("\t\teof = %llu\n",
2394445fffbSMatthew Ahrens 	    (u_longlong_t)shp->sh_eof);
2404445fffbSMatthew Ahrens 	(void) printf("\t\trecords_lost = %llu\n",
2414445fffbSMatthew Ahrens 	    (u_longlong_t)shp->sh_records_lost);
2424445fffbSMatthew Ahrens }
2434445fffbSMatthew Ahrens 
2443f9d6ad7SLin Ling static void
2453f9d6ad7SLin Ling zdb_nicenum(uint64_t num, char *buf)
2463f9d6ad7SLin Ling {
2473f9d6ad7SLin Ling 	if (dump_opt['P'])
2483f9d6ad7SLin Ling 		(void) sprintf(buf, "%llu", (longlong_t)num);
2493f9d6ad7SLin Ling 	else
2503f9d6ad7SLin Ling 		nicenum(num, buf);
2513f9d6ad7SLin Ling }
2523f9d6ad7SLin Ling 
253490d05b9SMatthew Ahrens const char histo_stars[] = "****************************************";
254490d05b9SMatthew Ahrens const int histo_width = sizeof (histo_stars) - 1;
255fa9e4066Sahrens 
256fa9e4066Sahrens static void
2570713e232SGeorge Wilson dump_histogram(const uint64_t *histo, int size, int offset)
258fa9e4066Sahrens {
259fa9e4066Sahrens 	int i;
260490d05b9SMatthew Ahrens 	int minidx = size - 1;
261fa9e4066Sahrens 	int maxidx = 0;
262fa9e4066Sahrens 	uint64_t max = 0;
263fa9e4066Sahrens 
264490d05b9SMatthew Ahrens 	for (i = 0; i < size; i++) {
265fa9e4066Sahrens 		if (histo[i] > max)
266fa9e4066Sahrens 			max = histo[i];
267fa9e4066Sahrens 		if (histo[i] > 0 && i > maxidx)
268fa9e4066Sahrens 			maxidx = i;
269fa9e4066Sahrens 		if (histo[i] > 0 && i < minidx)
270fa9e4066Sahrens 			minidx = i;
271fa9e4066Sahrens 	}
272fa9e4066Sahrens 
273490d05b9SMatthew Ahrens 	if (max < histo_width)
274490d05b9SMatthew Ahrens 		max = histo_width;
275fa9e4066Sahrens 
276490d05b9SMatthew Ahrens 	for (i = minidx; i <= maxidx; i++) {
277490d05b9SMatthew Ahrens 		(void) printf("\t\t\t%3u: %6llu %s\n",
2780713e232SGeorge Wilson 		    i + offset, (u_longlong_t)histo[i],
279490d05b9SMatthew Ahrens 		    &histo_stars[(max - histo[i]) * histo_width / max]);
280490d05b9SMatthew Ahrens 	}
281fa9e4066Sahrens }
282fa9e4066Sahrens 
283fa9e4066Sahrens static void
284fa9e4066Sahrens dump_zap_stats(objset_t *os, uint64_t object)
285fa9e4066Sahrens {
286fa9e4066Sahrens 	int error;
287fa9e4066Sahrens 	zap_stats_t zs;
288fa9e4066Sahrens 
289fa9e4066Sahrens 	error = zap_get_stats(os, object, &zs);
290fa9e4066Sahrens 	if (error)
291fa9e4066Sahrens 		return;
292fa9e4066Sahrens 
293fa9e4066Sahrens 	if (zs.zs_ptrtbl_len == 0) {
294fa9e4066Sahrens 		ASSERT(zs.zs_num_blocks == 1);
295fa9e4066Sahrens 		(void) printf("\tmicrozap: %llu bytes, %llu entries\n",
296fa9e4066Sahrens 		    (u_longlong_t)zs.zs_blocksize,
297fa9e4066Sahrens 		    (u_longlong_t)zs.zs_num_entries);
298fa9e4066Sahrens 		return;
299fa9e4066Sahrens 	}
300fa9e4066Sahrens 
301fa9e4066Sahrens 	(void) printf("\tFat ZAP stats:\n");
3028248818dSnd 
3038248818dSnd 	(void) printf("\t\tPointer table:\n");
3048248818dSnd 	(void) printf("\t\t\t%llu elements\n",
305fa9e4066Sahrens 	    (u_longlong_t)zs.zs_ptrtbl_len);
3068248818dSnd 	(void) printf("\t\t\tzt_blk: %llu\n",
3078248818dSnd 	    (u_longlong_t)zs.zs_ptrtbl_zt_blk);
3088248818dSnd 	(void) printf("\t\t\tzt_numblks: %llu\n",
3098248818dSnd 	    (u_longlong_t)zs.zs_ptrtbl_zt_numblks);
3108248818dSnd 	(void) printf("\t\t\tzt_shift: %llu\n",
3118248818dSnd 	    (u_longlong_t)zs.zs_ptrtbl_zt_shift);
3128248818dSnd 	(void) printf("\t\t\tzt_blks_copied: %llu\n",
3138248818dSnd 	    (u_longlong_t)zs.zs_ptrtbl_blks_copied);
3148248818dSnd 	(void) printf("\t\t\tzt_nextblk: %llu\n",
3158248818dSnd 	    (u_longlong_t)zs.zs_ptrtbl_nextblk);
3168248818dSnd 
317fa9e4066Sahrens 	(void) printf("\t\tZAP entries: %llu\n",
318fa9e4066Sahrens 	    (u_longlong_t)zs.zs_num_entries);
319fa9e4066Sahrens 	(void) printf("\t\tLeaf blocks: %llu\n",
320fa9e4066Sahrens 	    (u_longlong_t)zs.zs_num_leafs);
321fa9e4066Sahrens 	(void) printf("\t\tTotal blocks: %llu\n",
322fa9e4066Sahrens 	    (u_longlong_t)zs.zs_num_blocks);
3238248818dSnd 	(void) printf("\t\tzap_block_type: 0x%llx\n",
3248248818dSnd 	    (u_longlong_t)zs.zs_block_type);
3258248818dSnd 	(void) printf("\t\tzap_magic: 0x%llx\n",
3268248818dSnd 	    (u_longlong_t)zs.zs_magic);
3278248818dSnd 	(void) printf("\t\tzap_salt: 0x%llx\n",
3288248818dSnd 	    (u_longlong_t)zs.zs_salt);
329fa9e4066Sahrens 
330fa9e4066Sahrens 	(void) printf("\t\tLeafs with 2^n pointers:\n");
3310713e232SGeorge Wilson 	dump_histogram(zs.zs_leafs_with_2n_pointers, ZAP_HISTOGRAM_SIZE, 0);
332fa9e4066Sahrens 
333fa9e4066Sahrens 	(void) printf("\t\tBlocks with n*5 entries:\n");
3340713e232SGeorge Wilson 	dump_histogram(zs.zs_blocks_with_n5_entries, ZAP_HISTOGRAM_SIZE, 0);
335fa9e4066Sahrens 
336fa9e4066Sahrens 	(void) printf("\t\tBlocks n/10 full:\n");
3370713e232SGeorge Wilson 	dump_histogram(zs.zs_blocks_n_tenths_full, ZAP_HISTOGRAM_SIZE, 0);
338fa9e4066Sahrens 
339fa9e4066Sahrens 	(void) printf("\t\tEntries with n chunks:\n");
3400713e232SGeorge Wilson 	dump_histogram(zs.zs_entries_using_n_chunks, ZAP_HISTOGRAM_SIZE, 0);
341fa9e4066Sahrens 
342fa9e4066Sahrens 	(void) printf("\t\tBuckets with n entries:\n");
3430713e232SGeorge Wilson 	dump_histogram(zs.zs_buckets_with_n_entries, ZAP_HISTOGRAM_SIZE, 0);
344fa9e4066Sahrens }
345fa9e4066Sahrens 
346fa9e4066Sahrens /*ARGSUSED*/
347fa9e4066Sahrens static void
348fa9e4066Sahrens dump_none(objset_t *os, uint64_t object, void *data, size_t size)
349fa9e4066Sahrens {
350fa9e4066Sahrens }
351fa9e4066Sahrens 
3526de8f417SVictor Latushkin /*ARGSUSED*/
3536de8f417SVictor Latushkin static void
3546de8f417SVictor Latushkin dump_unknown(objset_t *os, uint64_t object, void *data, size_t size)
3556de8f417SVictor Latushkin {
3566de8f417SVictor Latushkin 	(void) printf("\tUNKNOWN OBJECT TYPE\n");
3576de8f417SVictor Latushkin }
3586de8f417SVictor Latushkin 
359fa9e4066Sahrens /*ARGSUSED*/
360fa9e4066Sahrens void
361fa9e4066Sahrens dump_uint8(objset_t *os, uint64_t object, void *data, size_t size)
362fa9e4066Sahrens {
363fa9e4066Sahrens }
364fa9e4066Sahrens 
365fa9e4066Sahrens /*ARGSUSED*/
366fa9e4066Sahrens static void
367fa9e4066Sahrens dump_uint64(objset_t *os, uint64_t object, void *data, size_t size)
368fa9e4066Sahrens {
369fa9e4066Sahrens }
370fa9e4066Sahrens 
371fa9e4066Sahrens /*ARGSUSED*/
372fa9e4066Sahrens static void
373fa9e4066Sahrens dump_zap(objset_t *os, uint64_t object, void *data, size_t size)
374fa9e4066Sahrens {
375fa9e4066Sahrens 	zap_cursor_t zc;
376fa9e4066Sahrens 	zap_attribute_t attr;
377fa9e4066Sahrens 	void *prop;
378fa9e4066Sahrens 	int i;
379fa9e4066Sahrens 
380fa9e4066Sahrens 	dump_zap_stats(os, object);
381fa9e4066Sahrens 	(void) printf("\n");
382fa9e4066Sahrens 
383fa9e4066Sahrens 	for (zap_cursor_init(&zc, os, object);
384fa9e4066Sahrens 	    zap_cursor_retrieve(&zc, &attr) == 0;
385fa9e4066Sahrens 	    zap_cursor_advance(&zc)) {
386fa9e4066Sahrens 		(void) printf("\t\t%s = ", attr.za_name);
387fa9e4066Sahrens 		if (attr.za_num_integers == 0) {
388fa9e4066Sahrens 			(void) printf("\n");
389fa9e4066Sahrens 			continue;
390fa9e4066Sahrens 		}
391fa9e4066Sahrens 		prop = umem_zalloc(attr.za_num_integers *
392fa9e4066Sahrens 		    attr.za_integer_length, UMEM_NOFAIL);
393fa9e4066Sahrens 		(void) zap_lookup(os, object, attr.za_name,
394fa9e4066Sahrens 		    attr.za_integer_length, attr.za_num_integers, prop);
395fa9e4066Sahrens 		if (attr.za_integer_length == 1) {
396fa9e4066Sahrens 			(void) printf("%s", (char *)prop);
397fa9e4066Sahrens 		} else {
398fa9e4066Sahrens 			for (i = 0; i < attr.za_num_integers; i++) {
399fa9e4066Sahrens 				switch (attr.za_integer_length) {
400fa9e4066Sahrens 				case 2:
401fa9e4066Sahrens 					(void) printf("%u ",
402fa9e4066Sahrens 					    ((uint16_t *)prop)[i]);
403fa9e4066Sahrens 					break;
404fa9e4066Sahrens 				case 4:
405fa9e4066Sahrens 					(void) printf("%u ",
406fa9e4066Sahrens 					    ((uint32_t *)prop)[i]);
407fa9e4066Sahrens 					break;
408fa9e4066Sahrens 				case 8:
409fa9e4066Sahrens 					(void) printf("%lld ",
410fa9e4066Sahrens 					    (u_longlong_t)((int64_t *)prop)[i]);
411fa9e4066Sahrens 					break;
412fa9e4066Sahrens 				}
413fa9e4066Sahrens 			}
414fa9e4066Sahrens 		}
415fa9e4066Sahrens 		(void) printf("\n");
416fa9e4066Sahrens 		umem_free(prop, attr.za_num_integers * attr.za_integer_length);
417fa9e4066Sahrens 	}
41887e5029aSahrens 	zap_cursor_fini(&zc);
419fa9e4066Sahrens }
420fa9e4066Sahrens 
421486ae710SMatthew Ahrens /*ARGSUSED*/
422486ae710SMatthew Ahrens static void
423486ae710SMatthew Ahrens dump_ddt_zap(objset_t *os, uint64_t object, void *data, size_t size)
424486ae710SMatthew Ahrens {
425486ae710SMatthew Ahrens 	dump_zap_stats(os, object);
426486ae710SMatthew Ahrens 	/* contents are printed elsewhere, properly decoded */
427486ae710SMatthew Ahrens }
428486ae710SMatthew Ahrens 
4290a586ceaSMark Shellenbaum /*ARGSUSED*/
4300a586ceaSMark Shellenbaum static void
4310a586ceaSMark Shellenbaum dump_sa_attrs(objset_t *os, uint64_t object, void *data, size_t size)
4320a586ceaSMark Shellenbaum {
4330a586ceaSMark Shellenbaum 	zap_cursor_t zc;
4340a586ceaSMark Shellenbaum 	zap_attribute_t attr;
4350a586ceaSMark Shellenbaum 
4360a586ceaSMark Shellenbaum 	dump_zap_stats(os, object);
4370a586ceaSMark Shellenbaum 	(void) printf("\n");
4380a586ceaSMark Shellenbaum 
4390a586ceaSMark Shellenbaum 	for (zap_cursor_init(&zc, os, object);
4400a586ceaSMark Shellenbaum 	    zap_cursor_retrieve(&zc, &attr) == 0;
4410a586ceaSMark Shellenbaum 	    zap_cursor_advance(&zc)) {
4420a586ceaSMark Shellenbaum 		(void) printf("\t\t%s = ", attr.za_name);
4430a586ceaSMark Shellenbaum 		if (attr.za_num_integers == 0) {
4440a586ceaSMark Shellenbaum 			(void) printf("\n");
4450a586ceaSMark Shellenbaum 			continue;
4460a586ceaSMark Shellenbaum 		}
4470a586ceaSMark Shellenbaum 		(void) printf(" %llx : [%d:%d:%d]\n",
4480a586ceaSMark Shellenbaum 		    (u_longlong_t)attr.za_first_integer,
4490a586ceaSMark Shellenbaum 		    (int)ATTR_LENGTH(attr.za_first_integer),
4500a586ceaSMark Shellenbaum 		    (int)ATTR_BSWAP(attr.za_first_integer),
4510a586ceaSMark Shellenbaum 		    (int)ATTR_NUM(attr.za_first_integer));
4520a586ceaSMark Shellenbaum 	}
4530a586ceaSMark Shellenbaum 	zap_cursor_fini(&zc);
4540a586ceaSMark Shellenbaum }
4550a586ceaSMark Shellenbaum 
4560a586ceaSMark Shellenbaum /*ARGSUSED*/
4570a586ceaSMark Shellenbaum static void
4580a586ceaSMark Shellenbaum dump_sa_layouts(objset_t *os, uint64_t object, void *data, size_t size)
4590a586ceaSMark Shellenbaum {
4600a586ceaSMark Shellenbaum 	zap_cursor_t zc;
4610a586ceaSMark Shellenbaum 	zap_attribute_t attr;
4620a586ceaSMark Shellenbaum 	uint16_t *layout_attrs;
4630a586ceaSMark Shellenbaum 	int i;
4640a586ceaSMark Shellenbaum 
4650a586ceaSMark Shellenbaum 	dump_zap_stats(os, object);
4660a586ceaSMark Shellenbaum 	(void) printf("\n");
4670a586ceaSMark Shellenbaum 
4680a586ceaSMark Shellenbaum 	for (zap_cursor_init(&zc, os, object);
4690a586ceaSMark Shellenbaum 	    zap_cursor_retrieve(&zc, &attr) == 0;
4700a586ceaSMark Shellenbaum 	    zap_cursor_advance(&zc)) {
4710a586ceaSMark Shellenbaum 		(void) printf("\t\t%s = [", attr.za_name);
4720a586ceaSMark Shellenbaum 		if (attr.za_num_integers == 0) {
4730a586ceaSMark Shellenbaum 			(void) printf("\n");
4740a586ceaSMark Shellenbaum 			continue;
4750a586ceaSMark Shellenbaum 		}
4760a586ceaSMark Shellenbaum 
4770a586ceaSMark Shellenbaum 		VERIFY(attr.za_integer_length == 2);
4780a586ceaSMark Shellenbaum 		layout_attrs = umem_zalloc(attr.za_num_integers *
4790a586ceaSMark Shellenbaum 		    attr.za_integer_length, UMEM_NOFAIL);
4800a586ceaSMark Shellenbaum 
4810a586ceaSMark Shellenbaum 		VERIFY(zap_lookup(os, object, attr.za_name,
4820a586ceaSMark Shellenbaum 		    attr.za_integer_length,
4830a586ceaSMark Shellenbaum 		    attr.za_num_integers, layout_attrs) == 0);
4840a586ceaSMark Shellenbaum 
4850a586ceaSMark Shellenbaum 		for (i = 0; i != attr.za_num_integers; i++)
4860a586ceaSMark Shellenbaum 			(void) printf(" %d ", (int)layout_attrs[i]);
4870a586ceaSMark Shellenbaum 		(void) printf("]\n");
4880a586ceaSMark Shellenbaum 		umem_free(layout_attrs,
4890a586ceaSMark Shellenbaum 		    attr.za_num_integers * attr.za_integer_length);
4900a586ceaSMark Shellenbaum 	}
4910a586ceaSMark Shellenbaum 	zap_cursor_fini(&zc);
4920a586ceaSMark Shellenbaum }
4930a586ceaSMark Shellenbaum 
494e7437265Sahrens /*ARGSUSED*/
495e7437265Sahrens static void
496e7437265Sahrens dump_zpldir(objset_t *os, uint64_t object, void *data, size_t size)
497e7437265Sahrens {
498e7437265Sahrens 	zap_cursor_t zc;
499e7437265Sahrens 	zap_attribute_t attr;
500e7437265Sahrens 	const char *typenames[] = {
501e7437265Sahrens 		/* 0 */ "not specified",
502e7437265Sahrens 		/* 1 */ "FIFO",
503e7437265Sahrens 		/* 2 */ "Character Device",
504e7437265Sahrens 		/* 3 */ "3 (invalid)",
505e7437265Sahrens 		/* 4 */ "Directory",
506e7437265Sahrens 		/* 5 */ "5 (invalid)",
507e7437265Sahrens 		/* 6 */ "Block Device",
508e7437265Sahrens 		/* 7 */ "7 (invalid)",
509e7437265Sahrens 		/* 8 */ "Regular File",
510e7437265Sahrens 		/* 9 */ "9 (invalid)",
511e7437265Sahrens 		/* 10 */ "Symbolic Link",
512e7437265Sahrens 		/* 11 */ "11 (invalid)",
513e7437265Sahrens 		/* 12 */ "Socket",
514e7437265Sahrens 		/* 13 */ "Door",
515e7437265Sahrens 		/* 14 */ "Event Port",
516e7437265Sahrens 		/* 15 */ "15 (invalid)",
517e7437265Sahrens 	};
518e7437265Sahrens 
519e7437265Sahrens 	dump_zap_stats(os, object);
520e7437265Sahrens 	(void) printf("\n");
521e7437265Sahrens 
522e7437265Sahrens 	for (zap_cursor_init(&zc, os, object);
523e7437265Sahrens 	    zap_cursor_retrieve(&zc, &attr) == 0;
524e7437265Sahrens 	    zap_cursor_advance(&zc)) {
525e7437265Sahrens 		(void) printf("\t\t%s = %lld (type: %s)\n",
526e7437265Sahrens 		    attr.za_name, ZFS_DIRENT_OBJ(attr.za_first_integer),
527e7437265Sahrens 		    typenames[ZFS_DIRENT_TYPE(attr.za_first_integer)]);
528e7437265Sahrens 	}
529e7437265Sahrens 	zap_cursor_fini(&zc);
530e7437265Sahrens }
531e7437265Sahrens 
5320713e232SGeorge Wilson int
5330713e232SGeorge Wilson get_dtl_refcount(vdev_t *vd)
5340713e232SGeorge Wilson {
5350713e232SGeorge Wilson 	int refcount = 0;
5360713e232SGeorge Wilson 
5370713e232SGeorge Wilson 	if (vd->vdev_ops->vdev_op_leaf) {
5380713e232SGeorge Wilson 		space_map_t *sm = vd->vdev_dtl_sm;
5390713e232SGeorge Wilson 
5400713e232SGeorge Wilson 		if (sm != NULL &&
5410713e232SGeorge Wilson 		    sm->sm_dbuf->db_size == sizeof (space_map_phys_t))
5420713e232SGeorge Wilson 			return (1);
5430713e232SGeorge Wilson 		return (0);
5440713e232SGeorge Wilson 	}
5450713e232SGeorge Wilson 
5460713e232SGeorge Wilson 	for (int c = 0; c < vd->vdev_children; c++)
5470713e232SGeorge Wilson 		refcount += get_dtl_refcount(vd->vdev_child[c]);
5480713e232SGeorge Wilson 	return (refcount);
5490713e232SGeorge Wilson }
5500713e232SGeorge Wilson 
5510713e232SGeorge Wilson int
5520713e232SGeorge Wilson get_metaslab_refcount(vdev_t *vd)
5530713e232SGeorge Wilson {
5540713e232SGeorge Wilson 	int refcount = 0;
5550713e232SGeorge Wilson 
5562e4c9986SGeorge Wilson 	if (vd->vdev_top == vd && !vd->vdev_removing) {
5570713e232SGeorge Wilson 		for (int m = 0; m < vd->vdev_ms_count; m++) {
5580713e232SGeorge Wilson 			space_map_t *sm = vd->vdev_ms[m]->ms_sm;
5590713e232SGeorge Wilson 
5600713e232SGeorge Wilson 			if (sm != NULL &&
5610713e232SGeorge Wilson 			    sm->sm_dbuf->db_size == sizeof (space_map_phys_t))
5620713e232SGeorge Wilson 				refcount++;
5630713e232SGeorge Wilson 		}
5640713e232SGeorge Wilson 	}
5650713e232SGeorge Wilson 	for (int c = 0; c < vd->vdev_children; c++)
5660713e232SGeorge Wilson 		refcount += get_metaslab_refcount(vd->vdev_child[c]);
5670713e232SGeorge Wilson 
5680713e232SGeorge Wilson 	return (refcount);
5690713e232SGeorge Wilson }
5700713e232SGeorge Wilson 
5710713e232SGeorge Wilson static int
5720713e232SGeorge Wilson verify_spacemap_refcounts(spa_t *spa)
5730713e232SGeorge Wilson {
5742acef22dSMatthew Ahrens 	uint64_t expected_refcount = 0;
5752acef22dSMatthew Ahrens 	uint64_t actual_refcount;
5760713e232SGeorge Wilson 
5772acef22dSMatthew Ahrens 	(void) feature_get_refcount(spa,
5782acef22dSMatthew Ahrens 	    &spa_feature_table[SPA_FEATURE_SPACEMAP_HISTOGRAM],
5792acef22dSMatthew Ahrens 	    &expected_refcount);
5800713e232SGeorge Wilson 	actual_refcount = get_dtl_refcount(spa->spa_root_vdev);
5810713e232SGeorge Wilson 	actual_refcount += get_metaslab_refcount(spa->spa_root_vdev);
5820713e232SGeorge Wilson 
5830713e232SGeorge Wilson 	if (expected_refcount != actual_refcount) {
5842acef22dSMatthew Ahrens 		(void) printf("space map refcount mismatch: expected %lld != "
5852acef22dSMatthew Ahrens 		    "actual %lld\n",
5862acef22dSMatthew Ahrens 		    (longlong_t)expected_refcount,
5872acef22dSMatthew Ahrens 		    (longlong_t)actual_refcount);
5880713e232SGeorge Wilson 		return (2);
5890713e232SGeorge Wilson 	}
5900713e232SGeorge Wilson 	return (0);
5910713e232SGeorge Wilson }
5920713e232SGeorge Wilson 
593fa9e4066Sahrens static void
5940713e232SGeorge Wilson dump_spacemap(objset_t *os, space_map_t *sm)
595fa9e4066Sahrens {
596fa9e4066Sahrens 	uint64_t alloc, offset, entry;
5978053a263Sck 	char *ddata[] = { "ALLOC", "FREE", "CONDENSE", "INVALID",
5988053a263Sck 			    "INVALID", "INVALID", "INVALID", "INVALID" };
599fa9e4066Sahrens 
6000713e232SGeorge Wilson 	if (sm == NULL)
601fa9e4066Sahrens 		return;
602fa9e4066Sahrens 
603fa9e4066Sahrens 	/*
604fa9e4066Sahrens 	 * Print out the freelist entries in both encoded and decoded form.
605fa9e4066Sahrens 	 */
606fa9e4066Sahrens 	alloc = 0;
6070713e232SGeorge Wilson 	for (offset = 0; offset < space_map_length(sm);
6080713e232SGeorge Wilson 	    offset += sizeof (entry)) {
6090713e232SGeorge Wilson 		uint8_t mapshift = sm->sm_shift;
6100713e232SGeorge Wilson 
6110713e232SGeorge Wilson 		VERIFY0(dmu_read(os, space_map_object(sm), offset,
6127bfdf011SNeil Perrin 		    sizeof (entry), &entry, DMU_READ_PREFETCH));
613fa9e4066Sahrens 		if (SM_DEBUG_DECODE(entry)) {
6140713e232SGeorge Wilson 
61587219db7SVictor Latushkin 			(void) printf("\t    [%6llu] %s: txg %llu, pass %llu\n",
616fa9e4066Sahrens 			    (u_longlong_t)(offset / sizeof (entry)),
617fa9e4066Sahrens 			    ddata[SM_DEBUG_ACTION_DECODE(entry)],
6185ad82045Snd 			    (u_longlong_t)SM_DEBUG_TXG_DECODE(entry),
6195ad82045Snd 			    (u_longlong_t)SM_DEBUG_SYNCPASS_DECODE(entry));
620fa9e4066Sahrens 		} else {
62187219db7SVictor Latushkin 			(void) printf("\t    [%6llu]    %c  range:"
62287219db7SVictor Latushkin 			    " %010llx-%010llx  size: %06llx\n",
623fa9e4066Sahrens 			    (u_longlong_t)(offset / sizeof (entry)),
624fa9e4066Sahrens 			    SM_TYPE_DECODE(entry) == SM_ALLOC ? 'A' : 'F',
6255ad82045Snd 			    (u_longlong_t)((SM_OFFSET_DECODE(entry) <<
6260713e232SGeorge Wilson 			    mapshift) + sm->sm_start),
6275ad82045Snd 			    (u_longlong_t)((SM_OFFSET_DECODE(entry) <<
6280713e232SGeorge Wilson 			    mapshift) + sm->sm_start +
6290713e232SGeorge Wilson 			    (SM_RUN_DECODE(entry) << mapshift)),
6305ad82045Snd 			    (u_longlong_t)(SM_RUN_DECODE(entry) << mapshift));
631fa9e4066Sahrens 			if (SM_TYPE_DECODE(entry) == SM_ALLOC)
632fa9e4066Sahrens 				alloc += SM_RUN_DECODE(entry) << mapshift;
633fa9e4066Sahrens 			else
634fa9e4066Sahrens 				alloc -= SM_RUN_DECODE(entry) << mapshift;
635fa9e4066Sahrens 		}
636fa9e4066Sahrens 	}
6370713e232SGeorge Wilson 	if (alloc != space_map_allocated(sm)) {
638fa9e4066Sahrens 		(void) printf("space_map_object alloc (%llu) INCONSISTENT "
639fa9e4066Sahrens 		    "with space map summary (%llu)\n",
6400713e232SGeorge Wilson 		    (u_longlong_t)space_map_allocated(sm), (u_longlong_t)alloc);
641fa9e4066Sahrens 	}
642fa9e4066Sahrens }
643fa9e4066Sahrens 
644d6e555bdSGeorge Wilson static void
645d6e555bdSGeorge Wilson dump_metaslab_stats(metaslab_t *msp)
646d6e555bdSGeorge Wilson {
6473f9d6ad7SLin Ling 	char maxbuf[32];
6480713e232SGeorge Wilson 	range_tree_t *rt = msp->ms_tree;
6490713e232SGeorge Wilson 	avl_tree_t *t = &msp->ms_size_tree;
6500713e232SGeorge Wilson 	int free_pct = range_tree_space(rt) * 100 / msp->ms_size;
651d6e555bdSGeorge Wilson 
6520713e232SGeorge Wilson 	zdb_nicenum(metaslab_block_maxsize(msp), maxbuf);
653d6e555bdSGeorge Wilson 
65487219db7SVictor Latushkin 	(void) printf("\t %25s %10lu   %7s  %6s   %4s %4d%%\n",
655d6e555bdSGeorge Wilson 	    "segments", avl_numnodes(t), "maxsize", maxbuf,
656d6e555bdSGeorge Wilson 	    "freepct", free_pct);
6570713e232SGeorge Wilson 	(void) printf("\tIn-memory histogram:\n");
6580713e232SGeorge Wilson 	dump_histogram(rt->rt_histogram, RANGE_TREE_HISTOGRAM_SIZE, 0);
659d6e555bdSGeorge Wilson }
660d6e555bdSGeorge Wilson 
661fa9e4066Sahrens static void
662fa9e4066Sahrens dump_metaslab(metaslab_t *msp)
663fa9e4066Sahrens {
664fa9e4066Sahrens 	vdev_t *vd = msp->ms_group->mg_vd;
665fa9e4066Sahrens 	spa_t *spa = vd->vdev_spa;
6660713e232SGeorge Wilson 	space_map_t *sm = msp->ms_sm;
6673f9d6ad7SLin Ling 	char freebuf[32];
668fa9e4066Sahrens 
6690713e232SGeorge Wilson 	zdb_nicenum(msp->ms_size - space_map_allocated(sm), freebuf);
670fa9e4066Sahrens 
671fa9e4066Sahrens 	(void) printf(
67287219db7SVictor Latushkin 	    "\tmetaslab %6llu   offset %12llx   spacemap %6llu   free    %5s\n",
6730713e232SGeorge Wilson 	    (u_longlong_t)msp->ms_id, (u_longlong_t)msp->ms_start,
6740713e232SGeorge Wilson 	    (u_longlong_t)space_map_object(sm), freebuf);
675fa9e4066Sahrens 
6760713e232SGeorge Wilson 	if (dump_opt['m'] > 2 && !dump_opt['L']) {
677d6e555bdSGeorge Wilson 		mutex_enter(&msp->ms_lock);
6780713e232SGeorge Wilson 		metaslab_load_wait(msp);
6790713e232SGeorge Wilson 		if (!msp->ms_loaded) {
6800713e232SGeorge Wilson 			VERIFY0(metaslab_load(msp));
6810713e232SGeorge Wilson 			range_tree_stat_verify(msp->ms_tree);
6820713e232SGeorge Wilson 		}
683bc3975b5SVictor Latushkin 		dump_metaslab_stats(msp);
6840713e232SGeorge Wilson 		metaslab_unload(msp);
685d6e555bdSGeorge Wilson 		mutex_exit(&msp->ms_lock);
686d6e555bdSGeorge Wilson 	}
687d6e555bdSGeorge Wilson 
6880713e232SGeorge Wilson 	if (dump_opt['m'] > 1 && sm != NULL &&
6892acef22dSMatthew Ahrens 	    spa_feature_is_active(spa, SPA_FEATURE_SPACEMAP_HISTOGRAM)) {
6900713e232SGeorge Wilson 		/*
6910713e232SGeorge Wilson 		 * The space map histogram represents free space in chunks
6920713e232SGeorge Wilson 		 * of sm_shift (i.e. bucket 0 refers to 2^sm_shift).
6930713e232SGeorge Wilson 		 */
6942e4c9986SGeorge Wilson 		(void) printf("\tOn-disk histogram:\t\tfragmentation %llu\n",
6952e4c9986SGeorge Wilson 		    (u_longlong_t)msp->ms_fragmentation);
6960713e232SGeorge Wilson 		dump_histogram(sm->sm_phys->smp_histogram,
6972e4c9986SGeorge Wilson 		    SPACE_MAP_HISTOGRAM_SIZE, sm->sm_shift);
6980713e232SGeorge Wilson 	}
6990713e232SGeorge Wilson 
7000713e232SGeorge Wilson 	if (dump_opt['d'] > 5 || dump_opt['m'] > 3) {
7010713e232SGeorge Wilson 		ASSERT(msp->ms_size == (1ULL << vd->vdev_ms_shift));
702d6e555bdSGeorge Wilson 
703d6e555bdSGeorge Wilson 		mutex_enter(&msp->ms_lock);
7040713e232SGeorge Wilson 		dump_spacemap(spa->spa_meta_objset, msp->ms_sm);
705d6e555bdSGeorge Wilson 		mutex_exit(&msp->ms_lock);
706d6e555bdSGeorge Wilson 	}
70787219db7SVictor Latushkin }
708fa9e4066Sahrens 
70987219db7SVictor Latushkin static void
71087219db7SVictor Latushkin print_vdev_metaslab_header(vdev_t *vd)
71187219db7SVictor Latushkin {
71287219db7SVictor Latushkin 	(void) printf("\tvdev %10llu\n\t%-10s%5llu   %-19s   %-15s   %-10s\n",
71387219db7SVictor Latushkin 	    (u_longlong_t)vd->vdev_id,
71487219db7SVictor Latushkin 	    "metaslabs", (u_longlong_t)vd->vdev_ms_count,
71587219db7SVictor Latushkin 	    "offset", "spacemap", "free");
71687219db7SVictor Latushkin 	(void) printf("\t%15s   %19s   %15s   %10s\n",
71787219db7SVictor Latushkin 	    "---------------", "-------------------",
71887219db7SVictor Latushkin 	    "---------------", "-------------");
719fa9e4066Sahrens }
720fa9e4066Sahrens 
7212e4c9986SGeorge Wilson static void
7222e4c9986SGeorge Wilson dump_metaslab_groups(spa_t *spa)
7232e4c9986SGeorge Wilson {
7242e4c9986SGeorge Wilson 	vdev_t *rvd = spa->spa_root_vdev;
7252e4c9986SGeorge Wilson 	metaslab_class_t *mc = spa_normal_class(spa);
7262e4c9986SGeorge Wilson 	uint64_t fragmentation;
7272e4c9986SGeorge Wilson 
7282e4c9986SGeorge Wilson 	metaslab_class_histogram_verify(mc);
7292e4c9986SGeorge Wilson 
7302e4c9986SGeorge Wilson 	for (int c = 0; c < rvd->vdev_children; c++) {
7312e4c9986SGeorge Wilson 		vdev_t *tvd = rvd->vdev_child[c];
7322e4c9986SGeorge Wilson 		metaslab_group_t *mg = tvd->vdev_mg;
7332e4c9986SGeorge Wilson 
7342e4c9986SGeorge Wilson 		if (mg->mg_class != mc)
7352e4c9986SGeorge Wilson 			continue;
7362e4c9986SGeorge Wilson 
7372e4c9986SGeorge Wilson 		metaslab_group_histogram_verify(mg);
7382e4c9986SGeorge Wilson 		mg->mg_fragmentation = metaslab_group_fragmentation(mg);
7392e4c9986SGeorge Wilson 
7402e4c9986SGeorge Wilson 		(void) printf("\tvdev %10llu\t\tmetaslabs%5llu\t\t"
7412e4c9986SGeorge Wilson 		    "fragmentation",
7422e4c9986SGeorge Wilson 		    (u_longlong_t)tvd->vdev_id,
7432e4c9986SGeorge Wilson 		    (u_longlong_t)tvd->vdev_ms_count);
7442e4c9986SGeorge Wilson 		if (mg->mg_fragmentation == ZFS_FRAG_INVALID) {
7452e4c9986SGeorge Wilson 			(void) printf("%3s\n", "-");
7462e4c9986SGeorge Wilson 		} else {
7472e4c9986SGeorge Wilson 			(void) printf("%3llu%%\n",
7482e4c9986SGeorge Wilson 			    (u_longlong_t)mg->mg_fragmentation);
7492e4c9986SGeorge Wilson 		}
7502e4c9986SGeorge Wilson 		dump_histogram(mg->mg_histogram, RANGE_TREE_HISTOGRAM_SIZE, 0);
7512e4c9986SGeorge Wilson 	}
7522e4c9986SGeorge Wilson 
7532e4c9986SGeorge Wilson 	(void) printf("\tpool %s\tfragmentation", spa_name(spa));
7542e4c9986SGeorge Wilson 	fragmentation = metaslab_class_fragmentation(mc);
7552e4c9986SGeorge Wilson 	if (fragmentation == ZFS_FRAG_INVALID)
7562e4c9986SGeorge Wilson 		(void) printf("\t%3s\n", "-");
7572e4c9986SGeorge Wilson 	else
7582e4c9986SGeorge Wilson 		(void) printf("\t%3llu%%\n", (u_longlong_t)fragmentation);
7592e4c9986SGeorge Wilson 	dump_histogram(mc->mc_histogram, RANGE_TREE_HISTOGRAM_SIZE, 0);
7602e4c9986SGeorge Wilson }
7612e4c9986SGeorge Wilson 
762fa9e4066Sahrens static void
763fa9e4066Sahrens dump_metaslabs(spa_t *spa)
764fa9e4066Sahrens {
76587219db7SVictor Latushkin 	vdev_t *vd, *rvd = spa->spa_root_vdev;
76687219db7SVictor Latushkin 	uint64_t m, c = 0, children = rvd->vdev_children;
767fa9e4066Sahrens 
768fa9e4066Sahrens 	(void) printf("\nMetaslabs:\n");
769fa9e4066Sahrens 
77087219db7SVictor Latushkin 	if (!dump_opt['d'] && zopt_objects > 0) {
77187219db7SVictor Latushkin 		c = zopt_object[0];
77287219db7SVictor Latushkin 
77387219db7SVictor Latushkin 		if (c >= children)
77487219db7SVictor Latushkin 			(void) fatal("bad vdev id: %llu", (u_longlong_t)c);
775fa9e4066Sahrens 
77687219db7SVictor Latushkin 		if (zopt_objects > 1) {
77787219db7SVictor Latushkin 			vd = rvd->vdev_child[c];
77887219db7SVictor Latushkin 			print_vdev_metaslab_header(vd);
77987219db7SVictor Latushkin 
78087219db7SVictor Latushkin 			for (m = 1; m < zopt_objects; m++) {
78187219db7SVictor Latushkin 				if (zopt_object[m] < vd->vdev_ms_count)
78287219db7SVictor Latushkin 					dump_metaslab(
78387219db7SVictor Latushkin 					    vd->vdev_ms[zopt_object[m]]);
78487219db7SVictor Latushkin 				else
78587219db7SVictor Latushkin 					(void) fprintf(stderr, "bad metaslab "
78687219db7SVictor Latushkin 					    "number %llu\n",
78787219db7SVictor Latushkin 					    (u_longlong_t)zopt_object[m]);
78887219db7SVictor Latushkin 			}
78987219db7SVictor Latushkin 			(void) printf("\n");
79087219db7SVictor Latushkin 			return;
79187219db7SVictor Latushkin 		}
79287219db7SVictor Latushkin 		children = c + 1;
79387219db7SVictor Latushkin 	}
79487219db7SVictor Latushkin 	for (; c < children; c++) {
79587219db7SVictor Latushkin 		vd = rvd->vdev_child[c];
79687219db7SVictor Latushkin 		print_vdev_metaslab_header(vd);
797fa9e4066Sahrens 
798fa9e4066Sahrens 		for (m = 0; m < vd->vdev_ms_count; m++)
799fa9e4066Sahrens 			dump_metaslab(vd->vdev_ms[m]);
800fa9e4066Sahrens 		(void) printf("\n");
801fa9e4066Sahrens 	}
802fa9e4066Sahrens }
803fa9e4066Sahrens 
804b24ab676SJeff Bonwick static void
805b24ab676SJeff Bonwick dump_dde(const ddt_t *ddt, const ddt_entry_t *dde, uint64_t index)
806b24ab676SJeff Bonwick {
807b24ab676SJeff Bonwick 	const ddt_phys_t *ddp = dde->dde_phys;
808b24ab676SJeff Bonwick 	const ddt_key_t *ddk = &dde->dde_key;
809b24ab676SJeff Bonwick 	char *types[4] = { "ditto", "single", "double", "triple" };
810b24ab676SJeff Bonwick 	char blkbuf[BP_SPRINTF_LEN];
811b24ab676SJeff Bonwick 	blkptr_t blk;
812b24ab676SJeff Bonwick 
813b24ab676SJeff Bonwick 	for (int p = 0; p < DDT_PHYS_TYPES; p++, ddp++) {
814b24ab676SJeff Bonwick 		if (ddp->ddp_phys_birth == 0)
815b24ab676SJeff Bonwick 			continue;
816bbfd46c4SJeff Bonwick 		ddt_bp_create(ddt->ddt_checksum, ddk, ddp, &blk);
81743466aaeSMax Grossman 		snprintf_blkptr(blkbuf, sizeof (blkbuf), &blk);
818b24ab676SJeff Bonwick 		(void) printf("index %llx refcnt %llu %s %s\n",
819b24ab676SJeff Bonwick 		    (u_longlong_t)index, (u_longlong_t)ddp->ddp_refcnt,
820b24ab676SJeff Bonwick 		    types[p], blkbuf);
821b24ab676SJeff Bonwick 	}
822b24ab676SJeff Bonwick }
823b24ab676SJeff Bonwick 
824b24ab676SJeff Bonwick static void
825b24ab676SJeff Bonwick dump_dedup_ratio(const ddt_stat_t *dds)
826b24ab676SJeff Bonwick {
827b24ab676SJeff Bonwick 	double rL, rP, rD, D, dedup, compress, copies;
828b24ab676SJeff Bonwick 
829b24ab676SJeff Bonwick 	if (dds->dds_blocks == 0)
830b24ab676SJeff Bonwick 		return;
831b24ab676SJeff Bonwick 
832b24ab676SJeff Bonwick 	rL = (double)dds->dds_ref_lsize;
833b24ab676SJeff Bonwick 	rP = (double)dds->dds_ref_psize;
834b24ab676SJeff Bonwick 	rD = (double)dds->dds_ref_dsize;
835b24ab676SJeff Bonwick 	D = (double)dds->dds_dsize;
836b24ab676SJeff Bonwick 
837b24ab676SJeff Bonwick 	dedup = rD / D;
838b24ab676SJeff Bonwick 	compress = rL / rP;
839b24ab676SJeff Bonwick 	copies = rD / rP;
840b24ab676SJeff Bonwick 
841b24ab676SJeff Bonwick 	(void) printf("dedup = %.2f, compress = %.2f, copies = %.2f, "
842b24ab676SJeff Bonwick 	    "dedup * compress / copies = %.2f\n\n",
843b24ab676SJeff Bonwick 	    dedup, compress, copies, dedup * compress / copies);
844b24ab676SJeff Bonwick }
845b24ab676SJeff Bonwick 
846b24ab676SJeff Bonwick static void
847b24ab676SJeff Bonwick dump_ddt(ddt_t *ddt, enum ddt_type type, enum ddt_class class)
848b24ab676SJeff Bonwick {
849b24ab676SJeff Bonwick 	char name[DDT_NAMELEN];
850b24ab676SJeff Bonwick 	ddt_entry_t dde;
851b24ab676SJeff Bonwick 	uint64_t walk = 0;
852b24ab676SJeff Bonwick 	dmu_object_info_t doi;
853b24ab676SJeff Bonwick 	uint64_t count, dspace, mspace;
854b24ab676SJeff Bonwick 	int error;
855b24ab676SJeff Bonwick 
856b24ab676SJeff Bonwick 	error = ddt_object_info(ddt, type, class, &doi);
857b24ab676SJeff Bonwick 
858b24ab676SJeff Bonwick 	if (error == ENOENT)
859b24ab676SJeff Bonwick 		return;
860b24ab676SJeff Bonwick 	ASSERT(error == 0);
861b24ab676SJeff Bonwick 
8627448a079SGeorge Wilson 	if ((count = ddt_object_count(ddt, type, class)) == 0)
8637448a079SGeorge Wilson 		return;
8647448a079SGeorge Wilson 
865b24ab676SJeff Bonwick 	dspace = doi.doi_physical_blocks_512 << 9;
866b24ab676SJeff Bonwick 	mspace = doi.doi_fill_count * doi.doi_data_block_size;
867b24ab676SJeff Bonwick 
868b24ab676SJeff Bonwick 	ddt_object_name(ddt, type, class, name);
869b24ab676SJeff Bonwick 
870b24ab676SJeff Bonwick 	(void) printf("%s: %llu entries, size %llu on disk, %llu in core\n",
871b24ab676SJeff Bonwick 	    name,
872b24ab676SJeff Bonwick 	    (u_longlong_t)count,
873b24ab676SJeff Bonwick 	    (u_longlong_t)(dspace / count),
874b24ab676SJeff Bonwick 	    (u_longlong_t)(mspace / count));
875b24ab676SJeff Bonwick 
876b24ab676SJeff Bonwick 	if (dump_opt['D'] < 3)
877b24ab676SJeff Bonwick 		return;
878b24ab676SJeff Bonwick 
8799eb19f4dSGeorge Wilson 	zpool_dump_ddt(NULL, &ddt->ddt_histogram[type][class]);
880b24ab676SJeff Bonwick 
881b24ab676SJeff Bonwick 	if (dump_opt['D'] < 4)
882b24ab676SJeff Bonwick 		return;
883b24ab676SJeff Bonwick 
884b24ab676SJeff Bonwick 	if (dump_opt['D'] < 5 && class == DDT_CLASS_UNIQUE)
885b24ab676SJeff Bonwick 		return;
886b24ab676SJeff Bonwick 
887b24ab676SJeff Bonwick 	(void) printf("%s contents:\n\n", name);
888b24ab676SJeff Bonwick 
889bbfd46c4SJeff Bonwick 	while ((error = ddt_object_walk(ddt, type, class, &walk, &dde)) == 0)
890b24ab676SJeff Bonwick 		dump_dde(ddt, &dde, walk);
891b24ab676SJeff Bonwick 
892b24ab676SJeff Bonwick 	ASSERT(error == ENOENT);
893b24ab676SJeff Bonwick 
894b24ab676SJeff Bonwick 	(void) printf("\n");
895b24ab676SJeff Bonwick }
896b24ab676SJeff Bonwick 
897b24ab676SJeff Bonwick static void
898b24ab676SJeff Bonwick dump_all_ddts(spa_t *spa)
899b24ab676SJeff Bonwick {
900b24ab676SJeff Bonwick 	ddt_histogram_t ddh_total = { 0 };
901b24ab676SJeff Bonwick 	ddt_stat_t dds_total = { 0 };
902b24ab676SJeff Bonwick 
903b24ab676SJeff Bonwick 	for (enum zio_checksum c = 0; c < ZIO_CHECKSUM_FUNCTIONS; c++) {
904b24ab676SJeff Bonwick 		ddt_t *ddt = spa->spa_ddt[c];
905b24ab676SJeff Bonwick 		for (enum ddt_type type = 0; type < DDT_TYPES; type++) {
906b24ab676SJeff Bonwick 			for (enum ddt_class class = 0; class < DDT_CLASSES;
907b24ab676SJeff Bonwick 			    class++) {
908b24ab676SJeff Bonwick 				dump_ddt(ddt, type, class);
909b24ab676SJeff Bonwick 			}
910b24ab676SJeff Bonwick 		}
911b24ab676SJeff Bonwick 	}
912b24ab676SJeff Bonwick 
9139eb19f4dSGeorge Wilson 	ddt_get_dedup_stats(spa, &dds_total);
914b24ab676SJeff Bonwick 
915b24ab676SJeff Bonwick 	if (dds_total.dds_blocks == 0) {
916b24ab676SJeff Bonwick 		(void) printf("All DDTs are empty\n");
917b24ab676SJeff Bonwick 		return;
918b24ab676SJeff Bonwick 	}
919b24ab676SJeff Bonwick 
920b24ab676SJeff Bonwick 	(void) printf("\n");
921b24ab676SJeff Bonwick 
922b24ab676SJeff Bonwick 	if (dump_opt['D'] > 1) {
923b24ab676SJeff Bonwick 		(void) printf("DDT histogram (aggregated over all DDTs):\n");
9249eb19f4dSGeorge Wilson 		ddt_get_dedup_histogram(spa, &ddh_total);
9259eb19f4dSGeorge Wilson 		zpool_dump_ddt(&dds_total, &ddh_total);
926b24ab676SJeff Bonwick 	}
927b24ab676SJeff Bonwick 
928b24ab676SJeff Bonwick 	dump_dedup_ratio(&dds_total);
929b24ab676SJeff Bonwick }
930b24ab676SJeff Bonwick 
9318ad4d6ddSJeff Bonwick static void
9320713e232SGeorge Wilson dump_dtl_seg(void *arg, uint64_t start, uint64_t size)
9338ad4d6ddSJeff Bonwick {
9340713e232SGeorge Wilson 	char *prefix = arg;
9358ad4d6ddSJeff Bonwick 
9368ad4d6ddSJeff Bonwick 	(void) printf("%s [%llu,%llu) length %llu\n",
9378ad4d6ddSJeff Bonwick 	    prefix,
9388ad4d6ddSJeff Bonwick 	    (u_longlong_t)start,
9398ad4d6ddSJeff Bonwick 	    (u_longlong_t)(start + size),
9408ad4d6ddSJeff Bonwick 	    (u_longlong_t)(size));
9418ad4d6ddSJeff Bonwick }
9428ad4d6ddSJeff Bonwick 
943fa9e4066Sahrens static void
944fa9e4066Sahrens dump_dtl(vdev_t *vd, int indent)
945fa9e4066Sahrens {
9468ad4d6ddSJeff Bonwick 	spa_t *spa = vd->vdev_spa;
9478ad4d6ddSJeff Bonwick 	boolean_t required;
9488ad4d6ddSJeff Bonwick 	char *name[DTL_TYPES] = { "missing", "partial", "scrub", "outage" };
9498ad4d6ddSJeff Bonwick 	char prefix[256];
9508ad4d6ddSJeff Bonwick 
9518f18d1faSGeorge Wilson 	spa_vdev_state_enter(spa, SCL_NONE);
9528ad4d6ddSJeff Bonwick 	required = vdev_dtl_required(vd);
9538ad4d6ddSJeff Bonwick 	(void) spa_vdev_state_exit(spa, NULL, 0);
954fa9e4066Sahrens 
955fa9e4066Sahrens 	if (indent == 0)
956fa9e4066Sahrens 		(void) printf("\nDirty time logs:\n\n");
957fa9e4066Sahrens 
9588ad4d6ddSJeff Bonwick 	(void) printf("\t%*s%s [%s]\n", indent, "",
959e14bb325SJeff Bonwick 	    vd->vdev_path ? vd->vdev_path :
9608ad4d6ddSJeff Bonwick 	    vd->vdev_parent ? vd->vdev_ops->vdev_op_type : spa_name(spa),
9618ad4d6ddSJeff Bonwick 	    required ? "DTL-required" : "DTL-expendable");
962fa9e4066Sahrens 
9638ad4d6ddSJeff Bonwick 	for (int t = 0; t < DTL_TYPES; t++) {
9640713e232SGeorge Wilson 		range_tree_t *rt = vd->vdev_dtl[t];
9650713e232SGeorge Wilson 		if (range_tree_space(rt) == 0)
9668ad4d6ddSJeff Bonwick 			continue;
9678ad4d6ddSJeff Bonwick 		(void) snprintf(prefix, sizeof (prefix), "\t%*s%s",
9688ad4d6ddSJeff Bonwick 		    indent + 2, "", name[t]);
9690713e232SGeorge Wilson 		mutex_enter(rt->rt_lock);
9700713e232SGeorge Wilson 		range_tree_walk(rt, dump_dtl_seg, prefix);
9710713e232SGeorge Wilson 		mutex_exit(rt->rt_lock);
9728ad4d6ddSJeff Bonwick 		if (dump_opt['d'] > 5 && vd->vdev_children == 0)
9730713e232SGeorge Wilson 			dump_spacemap(spa->spa_meta_objset, vd->vdev_dtl_sm);
974fa9e4066Sahrens 	}
975fa9e4066Sahrens 
9768ad4d6ddSJeff Bonwick 	for (int c = 0; c < vd->vdev_children; c++)
977fa9e4066Sahrens 		dump_dtl(vd->vdev_child[c], indent + 4);
978fa9e4066Sahrens }
979fa9e4066Sahrens 
9808f18d1faSGeorge Wilson static void
9818f18d1faSGeorge Wilson dump_history(spa_t *spa)
9828f18d1faSGeorge Wilson {
9838f18d1faSGeorge Wilson 	nvlist_t **events = NULL;
9848f18d1faSGeorge Wilson 	char buf[SPA_MAXBLOCKSIZE];
985e4161df6SVictor Latushkin 	uint64_t resid, len, off = 0;
9868f18d1faSGeorge Wilson 	uint_t num = 0;
9878f18d1faSGeorge Wilson 	int error;
9888f18d1faSGeorge Wilson 	time_t tsec;
9898f18d1faSGeorge Wilson 	struct tm t;
9908f18d1faSGeorge Wilson 	char tbuf[30];
9918f18d1faSGeorge Wilson 	char internalstr[MAXPATHLEN];
9928f18d1faSGeorge Wilson 
9938f18d1faSGeorge Wilson 	do {
994e4161df6SVictor Latushkin 		len = sizeof (buf);
995e4161df6SVictor Latushkin 
9968f18d1faSGeorge Wilson 		if ((error = spa_history_get(spa, &off, &len, buf)) != 0) {
9978f18d1faSGeorge Wilson 			(void) fprintf(stderr, "Unable to read history: "
9988f18d1faSGeorge Wilson 			    "error %d\n", error);
9998f18d1faSGeorge Wilson 			return;
10008f18d1faSGeorge Wilson 		}
10018f18d1faSGeorge Wilson 
10028f18d1faSGeorge Wilson 		if (zpool_history_unpack(buf, len, &resid, &events, &num) != 0)
10038f18d1faSGeorge Wilson 			break;
10048f18d1faSGeorge Wilson 
10058f18d1faSGeorge Wilson 		off -= resid;
10068f18d1faSGeorge Wilson 	} while (len != 0);
10078f18d1faSGeorge Wilson 
10088f18d1faSGeorge Wilson 	(void) printf("\nHistory:\n");
10098f18d1faSGeorge Wilson 	for (int i = 0; i < num; i++) {
10108f18d1faSGeorge Wilson 		uint64_t time, txg, ievent;
10118f18d1faSGeorge Wilson 		char *cmd, *intstr;
10124445fffbSMatthew Ahrens 		boolean_t printed = B_FALSE;
10138f18d1faSGeorge Wilson 
10148f18d1faSGeorge Wilson 		if (nvlist_lookup_uint64(events[i], ZPOOL_HIST_TIME,
10158f18d1faSGeorge Wilson 		    &time) != 0)
10164445fffbSMatthew Ahrens 			goto next;
10178f18d1faSGeorge Wilson 		if (nvlist_lookup_string(events[i], ZPOOL_HIST_CMD,
10188f18d1faSGeorge Wilson 		    &cmd) != 0) {
10198f18d1faSGeorge Wilson 			if (nvlist_lookup_uint64(events[i],
10208f18d1faSGeorge Wilson 			    ZPOOL_HIST_INT_EVENT, &ievent) != 0)
10214445fffbSMatthew Ahrens 				goto next;
10228f18d1faSGeorge Wilson 			verify(nvlist_lookup_uint64(events[i],
10238f18d1faSGeorge Wilson 			    ZPOOL_HIST_TXG, &txg) == 0);
10248f18d1faSGeorge Wilson 			verify(nvlist_lookup_string(events[i],
10258f18d1faSGeorge Wilson 			    ZPOOL_HIST_INT_STR, &intstr) == 0);
10264445fffbSMatthew Ahrens 			if (ievent >= ZFS_NUM_LEGACY_HISTORY_EVENTS)
10274445fffbSMatthew Ahrens 				goto next;
10288f18d1faSGeorge Wilson 
10298f18d1faSGeorge Wilson 			(void) snprintf(internalstr,
10308f18d1faSGeorge Wilson 			    sizeof (internalstr),
10318f18d1faSGeorge Wilson 			    "[internal %s txg:%lld] %s",
10323f9d6ad7SLin Ling 			    zfs_history_event_names[ievent], txg,
10338f18d1faSGeorge Wilson 			    intstr);
10348f18d1faSGeorge Wilson 			cmd = internalstr;
10358f18d1faSGeorge Wilson 		}
10368f18d1faSGeorge Wilson 		tsec = time;
10378f18d1faSGeorge Wilson 		(void) localtime_r(&tsec, &t);
10388f18d1faSGeorge Wilson 		(void) strftime(tbuf, sizeof (tbuf), "%F.%T", &t);
10398f18d1faSGeorge Wilson 		(void) printf("%s %s\n", tbuf, cmd);
10404445fffbSMatthew Ahrens 		printed = B_TRUE;
10414445fffbSMatthew Ahrens 
10424445fffbSMatthew Ahrens next:
10434445fffbSMatthew Ahrens 		if (dump_opt['h'] > 1) {
10444445fffbSMatthew Ahrens 			if (!printed)
10454445fffbSMatthew Ahrens 				(void) printf("unrecognized record:\n");
10464445fffbSMatthew Ahrens 			dump_nvlist(events[i], 2);
10474445fffbSMatthew Ahrens 		}
10488f18d1faSGeorge Wilson 	}
10498f18d1faSGeorge Wilson }
10508f18d1faSGeorge Wilson 
1051fa9e4066Sahrens /*ARGSUSED*/
1052fa9e4066Sahrens static void
1053fa9e4066Sahrens dump_dnode(objset_t *os, uint64_t object, void *data, size_t size)
1054fa9e4066Sahrens {
1055fa9e4066Sahrens }
1056fa9e4066Sahrens 
1057fa9e4066Sahrens static uint64_t
10587802d7bfSMatthew Ahrens blkid2offset(const dnode_phys_t *dnp, const blkptr_t *bp,
10597802d7bfSMatthew Ahrens     const zbookmark_phys_t *zb)
1060fa9e4066Sahrens {
1061b24ab676SJeff Bonwick 	if (dnp == NULL) {
1062b24ab676SJeff Bonwick 		ASSERT(zb->zb_level < 0);
1063b24ab676SJeff Bonwick 		if (zb->zb_object == 0)
1064b24ab676SJeff Bonwick 			return (zb->zb_blkid);
1065b24ab676SJeff Bonwick 		return (zb->zb_blkid * BP_GET_LSIZE(bp));
1066b24ab676SJeff Bonwick 	}
1067b24ab676SJeff Bonwick 
1068b24ab676SJeff Bonwick 	ASSERT(zb->zb_level >= 0);
1069fa9e4066Sahrens 
1070b24ab676SJeff Bonwick 	return ((zb->zb_blkid <<
1071b24ab676SJeff Bonwick 	    (zb->zb_level * (dnp->dn_indblkshift - SPA_BLKPTRSHIFT))) *
1072fa9e4066Sahrens 	    dnp->dn_datablkszsec << SPA_MINBLOCKSHIFT);
1073fa9e4066Sahrens }
1074fa9e4066Sahrens 
107544cd46caSbillm static void
107643466aaeSMax Grossman snprintf_blkptr_compact(char *blkbuf, size_t buflen, const blkptr_t *bp)
107744cd46caSbillm {
1078cde58dbcSMatthew Ahrens 	const dva_t *dva = bp->blk_dva;
1079b24ab676SJeff Bonwick 	int ndvas = dump_opt['d'] > 5 ? BP_GET_NDVAS(bp) : 1;
1080b24ab676SJeff Bonwick 
1081490d05b9SMatthew Ahrens 	if (dump_opt['b'] >= 6) {
108243466aaeSMax Grossman 		snprintf_blkptr(blkbuf, buflen, bp);
1083b24ab676SJeff Bonwick 		return;
1084b24ab676SJeff Bonwick 	}
108544cd46caSbillm 
10865d7b4d43SMatthew Ahrens 	if (BP_IS_EMBEDDED(bp)) {
10875d7b4d43SMatthew Ahrens 		(void) sprintf(blkbuf,
10885d7b4d43SMatthew Ahrens 		    "EMBEDDED et=%u %llxL/%llxP B=%llu",
10895d7b4d43SMatthew Ahrens 		    (int)BPE_GET_ETYPE(bp),
10905d7b4d43SMatthew Ahrens 		    (u_longlong_t)BPE_GET_LSIZE(bp),
10915d7b4d43SMatthew Ahrens 		    (u_longlong_t)BPE_GET_PSIZE(bp),
10925d7b4d43SMatthew Ahrens 		    (u_longlong_t)bp->blk_birth);
10935d7b4d43SMatthew Ahrens 		return;
10945d7b4d43SMatthew Ahrens 	}
109544cd46caSbillm 
10965d7b4d43SMatthew Ahrens 	blkbuf[0] = '\0';
1097b24ab676SJeff Bonwick 	for (int i = 0; i < ndvas; i++)
109843466aaeSMax Grossman 		(void) snprintf(blkbuf + strlen(blkbuf),
109943466aaeSMax Grossman 		    buflen - strlen(blkbuf), "%llu:%llx:%llx ",
110044cd46caSbillm 		    (u_longlong_t)DVA_GET_VDEV(&dva[i]),
110144cd46caSbillm 		    (u_longlong_t)DVA_GET_OFFSET(&dva[i]),
110244cd46caSbillm 		    (u_longlong_t)DVA_GET_ASIZE(&dva[i]));
110344cd46caSbillm 
110443466aaeSMax Grossman 	if (BP_IS_HOLE(bp)) {
110543466aaeSMax Grossman 		(void) snprintf(blkbuf + strlen(blkbuf),
1106*70163ac5SPrakash Surya 		    buflen - strlen(blkbuf),
1107*70163ac5SPrakash Surya 		    "%llxL B=%llu",
1108*70163ac5SPrakash Surya 		    (u_longlong_t)BP_GET_LSIZE(bp),
110943466aaeSMax Grossman 		    (u_longlong_t)bp->blk_birth);
111043466aaeSMax Grossman 	} else {
111143466aaeSMax Grossman 		(void) snprintf(blkbuf + strlen(blkbuf),
111243466aaeSMax Grossman 		    buflen - strlen(blkbuf),
111343466aaeSMax Grossman 		    "%llxL/%llxP F=%llu B=%llu/%llu",
111443466aaeSMax Grossman 		    (u_longlong_t)BP_GET_LSIZE(bp),
111543466aaeSMax Grossman 		    (u_longlong_t)BP_GET_PSIZE(bp),
11165d7b4d43SMatthew Ahrens 		    (u_longlong_t)BP_GET_FILL(bp),
111743466aaeSMax Grossman 		    (u_longlong_t)bp->blk_birth,
111843466aaeSMax Grossman 		    (u_longlong_t)BP_PHYSICAL_BIRTH(bp));
111943466aaeSMax Grossman 	}
112044cd46caSbillm }
112144cd46caSbillm 
112288b7b0f2SMatthew Ahrens static void
11237802d7bfSMatthew Ahrens print_indirect(blkptr_t *bp, const zbookmark_phys_t *zb,
112488b7b0f2SMatthew Ahrens     const dnode_phys_t *dnp)
1125fa9e4066Sahrens {
112688b7b0f2SMatthew Ahrens 	char blkbuf[BP_SPRINTF_LEN];
1127fa9e4066Sahrens 	int l;
1128fa9e4066Sahrens 
11295d7b4d43SMatthew Ahrens 	if (!BP_IS_EMBEDDED(bp)) {
11305d7b4d43SMatthew Ahrens 		ASSERT3U(BP_GET_TYPE(bp), ==, dnp->dn_type);
11315d7b4d43SMatthew Ahrens 		ASSERT3U(BP_GET_LEVEL(bp), ==, zb->zb_level);
11325d7b4d43SMatthew Ahrens 	}
1133fa9e4066Sahrens 
1134b24ab676SJeff Bonwick 	(void) printf("%16llx ", (u_longlong_t)blkid2offset(dnp, bp, zb));
1135fa9e4066Sahrens 
1136fa9e4066Sahrens 	ASSERT(zb->zb_level >= 0);
1137fa9e4066Sahrens 
1138fa9e4066Sahrens 	for (l = dnp->dn_nlevels - 1; l >= -1; l--) {
1139fa9e4066Sahrens 		if (l == zb->zb_level) {
114088b7b0f2SMatthew Ahrens 			(void) printf("L%llx", (u_longlong_t)zb->zb_level);
1141fa9e4066Sahrens 		} else {
114288b7b0f2SMatthew Ahrens 			(void) printf(" ");
1143fa9e4066Sahrens 		}
1144fa9e4066Sahrens 	}
1145fa9e4066Sahrens 
114643466aaeSMax Grossman 	snprintf_blkptr_compact(blkbuf, sizeof (blkbuf), bp);
114788b7b0f2SMatthew Ahrens 	(void) printf("%s\n", blkbuf);
114888b7b0f2SMatthew Ahrens }
114988b7b0f2SMatthew Ahrens 
115088b7b0f2SMatthew Ahrens static int
115188b7b0f2SMatthew Ahrens visit_indirect(spa_t *spa, const dnode_phys_t *dnp,
11527802d7bfSMatthew Ahrens     blkptr_t *bp, const zbookmark_phys_t *zb)
115388b7b0f2SMatthew Ahrens {
1154e4161df6SVictor Latushkin 	int err = 0;
115588b7b0f2SMatthew Ahrens 
115688b7b0f2SMatthew Ahrens 	if (bp->blk_birth == 0)
115788b7b0f2SMatthew Ahrens 		return (0);
115888b7b0f2SMatthew Ahrens 
115988b7b0f2SMatthew Ahrens 	print_indirect(bp, zb, dnp);
116088b7b0f2SMatthew Ahrens 
116143466aaeSMax Grossman 	if (BP_GET_LEVEL(bp) > 0 && !BP_IS_HOLE(bp)) {
11627adb730bSGeorge Wilson 		arc_flags_t flags = ARC_FLAG_WAIT;
116388b7b0f2SMatthew Ahrens 		int i;
116488b7b0f2SMatthew Ahrens 		blkptr_t *cbp;
116588b7b0f2SMatthew Ahrens 		int epb = BP_GET_LSIZE(bp) >> SPA_BLKPTRSHIFT;
116688b7b0f2SMatthew Ahrens 		arc_buf_t *buf;
116788b7b0f2SMatthew Ahrens 		uint64_t fill = 0;
116888b7b0f2SMatthew Ahrens 
11691b912ec7SGeorge Wilson 		err = arc_read(NULL, spa, bp, arc_getbuf_func, &buf,
117088b7b0f2SMatthew Ahrens 		    ZIO_PRIORITY_ASYNC_READ, ZIO_FLAG_CANFAIL, &flags, zb);
117188b7b0f2SMatthew Ahrens 		if (err)
117288b7b0f2SMatthew Ahrens 			return (err);
11733f9d6ad7SLin Ling 		ASSERT(buf->b_data);
117488b7b0f2SMatthew Ahrens 
117588b7b0f2SMatthew Ahrens 		/* recursively visit blocks below this */
117688b7b0f2SMatthew Ahrens 		cbp = buf->b_data;
117788b7b0f2SMatthew Ahrens 		for (i = 0; i < epb; i++, cbp++) {
11787802d7bfSMatthew Ahrens 			zbookmark_phys_t czb;
117988b7b0f2SMatthew Ahrens 
118088b7b0f2SMatthew Ahrens 			SET_BOOKMARK(&czb, zb->zb_objset, zb->zb_object,
118188b7b0f2SMatthew Ahrens 			    zb->zb_level - 1,
118288b7b0f2SMatthew Ahrens 			    zb->zb_blkid * epb + i);
118388b7b0f2SMatthew Ahrens 			err = visit_indirect(spa, dnp, cbp, &czb);
118488b7b0f2SMatthew Ahrens 			if (err)
118588b7b0f2SMatthew Ahrens 				break;
11865d7b4d43SMatthew Ahrens 			fill += BP_GET_FILL(cbp);
118788b7b0f2SMatthew Ahrens 		}
11888ad4d6ddSJeff Bonwick 		if (!err)
11895d7b4d43SMatthew Ahrens 			ASSERT3U(fill, ==, BP_GET_FILL(bp));
119088b7b0f2SMatthew Ahrens 		(void) arc_buf_remove_ref(buf, &buf);
1191fa9e4066Sahrens 	}
1192fa9e4066Sahrens 
119388b7b0f2SMatthew Ahrens 	return (err);
1194fa9e4066Sahrens }
1195fa9e4066Sahrens 
1196fa9e4066Sahrens /*ARGSUSED*/
1197fa9e4066Sahrens static void
119888b7b0f2SMatthew Ahrens dump_indirect(dnode_t *dn)
1199fa9e4066Sahrens {
120088b7b0f2SMatthew Ahrens 	dnode_phys_t *dnp = dn->dn_phys;
120188b7b0f2SMatthew Ahrens 	int j;
12027802d7bfSMatthew Ahrens 	zbookmark_phys_t czb;
1203fa9e4066Sahrens 
1204fa9e4066Sahrens 	(void) printf("Indirect blocks:\n");
1205fa9e4066Sahrens 
1206503ad85cSMatthew Ahrens 	SET_BOOKMARK(&czb, dmu_objset_id(dn->dn_objset),
120788b7b0f2SMatthew Ahrens 	    dn->dn_object, dnp->dn_nlevels - 1, 0);
120888b7b0f2SMatthew Ahrens 	for (j = 0; j < dnp->dn_nblkptr; j++) {
120988b7b0f2SMatthew Ahrens 		czb.zb_blkid = j;
1210503ad85cSMatthew Ahrens 		(void) visit_indirect(dmu_objset_spa(dn->dn_objset), dnp,
121188b7b0f2SMatthew Ahrens 		    &dnp->dn_blkptr[j], &czb);
121288b7b0f2SMatthew Ahrens 	}
1213fa9e4066Sahrens 
1214fa9e4066Sahrens 	(void) printf("\n");
1215fa9e4066Sahrens }
1216fa9e4066Sahrens 
1217fa9e4066Sahrens /*ARGSUSED*/
1218fa9e4066Sahrens static void
1219fa9e4066Sahrens dump_dsl_dir(objset_t *os, uint64_t object, void *data, size_t size)
1220fa9e4066Sahrens {
1221fa9e4066Sahrens 	dsl_dir_phys_t *dd = data;
1222fa9e4066Sahrens 	time_t crtime;
12233f9d6ad7SLin Ling 	char nice[32];
1224fa9e4066Sahrens 
1225fa9e4066Sahrens 	if (dd == NULL)
1226fa9e4066Sahrens 		return;
1227fa9e4066Sahrens 
1228da6c28aaSamw 	ASSERT3U(size, >=, sizeof (dsl_dir_phys_t));
1229fa9e4066Sahrens 
1230fa9e4066Sahrens 	crtime = dd->dd_creation_time;
1231fa9e4066Sahrens 	(void) printf("\t\tcreation_time = %s", ctime(&crtime));
1232fa9e4066Sahrens 	(void) printf("\t\thead_dataset_obj = %llu\n",
1233fa9e4066Sahrens 	    (u_longlong_t)dd->dd_head_dataset_obj);
1234fa9e4066Sahrens 	(void) printf("\t\tparent_dir_obj = %llu\n",
1235fa9e4066Sahrens 	    (u_longlong_t)dd->dd_parent_obj);
12363cb34c60Sahrens 	(void) printf("\t\torigin_obj = %llu\n",
12373cb34c60Sahrens 	    (u_longlong_t)dd->dd_origin_obj);
1238fa9e4066Sahrens 	(void) printf("\t\tchild_dir_zapobj = %llu\n",
1239fa9e4066Sahrens 	    (u_longlong_t)dd->dd_child_dir_zapobj);
12403f9d6ad7SLin Ling 	zdb_nicenum(dd->dd_used_bytes, nice);
124174e7dc98SMatthew Ahrens 	(void) printf("\t\tused_bytes = %s\n", nice);
12423f9d6ad7SLin Ling 	zdb_nicenum(dd->dd_compressed_bytes, nice);
124374e7dc98SMatthew Ahrens 	(void) printf("\t\tcompressed_bytes = %s\n", nice);
12443f9d6ad7SLin Ling 	zdb_nicenum(dd->dd_uncompressed_bytes, nice);
124574e7dc98SMatthew Ahrens 	(void) printf("\t\tuncompressed_bytes = %s\n", nice);
12463f9d6ad7SLin Ling 	zdb_nicenum(dd->dd_quota, nice);
124774e7dc98SMatthew Ahrens 	(void) printf("\t\tquota = %s\n", nice);
12483f9d6ad7SLin Ling 	zdb_nicenum(dd->dd_reserved, nice);
124974e7dc98SMatthew Ahrens 	(void) printf("\t\treserved = %s\n", nice);
1250fa9e4066Sahrens 	(void) printf("\t\tprops_zapobj = %llu\n",
1251fa9e4066Sahrens 	    (u_longlong_t)dd->dd_props_zapobj);
1252ecd6cf80Smarks 	(void) printf("\t\tdeleg_zapobj = %llu\n",
1253ecd6cf80Smarks 	    (u_longlong_t)dd->dd_deleg_zapobj);
125474e7dc98SMatthew Ahrens 	(void) printf("\t\tflags = %llx\n",
125574e7dc98SMatthew Ahrens 	    (u_longlong_t)dd->dd_flags);
125674e7dc98SMatthew Ahrens 
125774e7dc98SMatthew Ahrens #define	DO(which) \
12583f9d6ad7SLin Ling 	zdb_nicenum(dd->dd_used_breakdown[DD_USED_ ## which], nice); \
125974e7dc98SMatthew Ahrens 	(void) printf("\t\tused_breakdown[" #which "] = %s\n", nice)
126074e7dc98SMatthew Ahrens 	DO(HEAD);
126174e7dc98SMatthew Ahrens 	DO(SNAP);
126274e7dc98SMatthew Ahrens 	DO(CHILD);
126374e7dc98SMatthew Ahrens 	DO(CHILD_RSRV);
126474e7dc98SMatthew Ahrens 	DO(REFRSRV);
126574e7dc98SMatthew Ahrens #undef DO
1266fa9e4066Sahrens }
1267fa9e4066Sahrens 
1268fa9e4066Sahrens /*ARGSUSED*/
1269fa9e4066Sahrens static void
1270fa9e4066Sahrens dump_dsl_dataset(objset_t *os, uint64_t object, void *data, size_t size)
1271fa9e4066Sahrens {
1272fa9e4066Sahrens 	dsl_dataset_phys_t *ds = data;
1273fa9e4066Sahrens 	time_t crtime;
12743f9d6ad7SLin Ling 	char used[32], compressed[32], uncompressed[32], unique[32];
1275fbabab8fSmaybee 	char blkbuf[BP_SPRINTF_LEN];
1276fa9e4066Sahrens 
1277fa9e4066Sahrens 	if (ds == NULL)
1278fa9e4066Sahrens 		return;
1279fa9e4066Sahrens 
1280fa9e4066Sahrens 	ASSERT(size == sizeof (*ds));
1281fa9e4066Sahrens 	crtime = ds->ds_creation_time;
1282ad135b5dSChristopher Siden 	zdb_nicenum(ds->ds_referenced_bytes, used);
12833f9d6ad7SLin Ling 	zdb_nicenum(ds->ds_compressed_bytes, compressed);
12843f9d6ad7SLin Ling 	zdb_nicenum(ds->ds_uncompressed_bytes, uncompressed);
12853f9d6ad7SLin Ling 	zdb_nicenum(ds->ds_unique_bytes, unique);
128643466aaeSMax Grossman 	snprintf_blkptr(blkbuf, sizeof (blkbuf), &ds->ds_bp);
1287fa9e4066Sahrens 
1288088f3894Sahrens 	(void) printf("\t\tdir_obj = %llu\n",
1289fa9e4066Sahrens 	    (u_longlong_t)ds->ds_dir_obj);
1290fa9e4066Sahrens 	(void) printf("\t\tprev_snap_obj = %llu\n",
1291fa9e4066Sahrens 	    (u_longlong_t)ds->ds_prev_snap_obj);
1292fa9e4066Sahrens 	(void) printf("\t\tprev_snap_txg = %llu\n",
1293fa9e4066Sahrens 	    (u_longlong_t)ds->ds_prev_snap_txg);
1294fa9e4066Sahrens 	(void) printf("\t\tnext_snap_obj = %llu\n",
1295fa9e4066Sahrens 	    (u_longlong_t)ds->ds_next_snap_obj);
1296fa9e4066Sahrens 	(void) printf("\t\tsnapnames_zapobj = %llu\n",
1297fa9e4066Sahrens 	    (u_longlong_t)ds->ds_snapnames_zapobj);
1298fa9e4066Sahrens 	(void) printf("\t\tnum_children = %llu\n",
1299fa9e4066Sahrens 	    (u_longlong_t)ds->ds_num_children);
1300842727c2SChris Kirby 	(void) printf("\t\tuserrefs_obj = %llu\n",
1301842727c2SChris Kirby 	    (u_longlong_t)ds->ds_userrefs_obj);
1302fa9e4066Sahrens 	(void) printf("\t\tcreation_time = %s", ctime(&crtime));
1303fa9e4066Sahrens 	(void) printf("\t\tcreation_txg = %llu\n",
1304fa9e4066Sahrens 	    (u_longlong_t)ds->ds_creation_txg);
1305fa9e4066Sahrens 	(void) printf("\t\tdeadlist_obj = %llu\n",
1306fa9e4066Sahrens 	    (u_longlong_t)ds->ds_deadlist_obj);
1307fa9e4066Sahrens 	(void) printf("\t\tused_bytes = %s\n", used);
1308fa9e4066Sahrens 	(void) printf("\t\tcompressed_bytes = %s\n", compressed);
1309fa9e4066Sahrens 	(void) printf("\t\tuncompressed_bytes = %s\n", uncompressed);
1310fa9e4066Sahrens 	(void) printf("\t\tunique = %s\n", unique);
1311fa9e4066Sahrens 	(void) printf("\t\tfsid_guid = %llu\n",
1312fa9e4066Sahrens 	    (u_longlong_t)ds->ds_fsid_guid);
1313fa9e4066Sahrens 	(void) printf("\t\tguid = %llu\n",
1314fa9e4066Sahrens 	    (u_longlong_t)ds->ds_guid);
131599653d4eSeschrock 	(void) printf("\t\tflags = %llx\n",
131699653d4eSeschrock 	    (u_longlong_t)ds->ds_flags);
1317088f3894Sahrens 	(void) printf("\t\tnext_clones_obj = %llu\n",
1318088f3894Sahrens 	    (u_longlong_t)ds->ds_next_clones_obj);
1319bb0ade09Sahrens 	(void) printf("\t\tprops_obj = %llu\n",
1320bb0ade09Sahrens 	    (u_longlong_t)ds->ds_props_obj);
1321fa9e4066Sahrens 	(void) printf("\t\tbp = %s\n", blkbuf);
1322fa9e4066Sahrens }
1323fa9e4066Sahrens 
1324ad135b5dSChristopher Siden /* ARGSUSED */
1325ad135b5dSChristopher Siden static int
1326ad135b5dSChristopher Siden dump_bptree_cb(void *arg, const blkptr_t *bp, dmu_tx_t *tx)
1327ad135b5dSChristopher Siden {
1328ad135b5dSChristopher Siden 	char blkbuf[BP_SPRINTF_LEN];
1329ad135b5dSChristopher Siden 
1330ad135b5dSChristopher Siden 	if (bp->blk_birth != 0) {
133143466aaeSMax Grossman 		snprintf_blkptr(blkbuf, sizeof (blkbuf), bp);
1332ad135b5dSChristopher Siden 		(void) printf("\t%s\n", blkbuf);
1333ad135b5dSChristopher Siden 	}
1334ad135b5dSChristopher Siden 	return (0);
1335ad135b5dSChristopher Siden }
1336ad135b5dSChristopher Siden 
1337ad135b5dSChristopher Siden static void
1338ad135b5dSChristopher Siden dump_bptree(objset_t *os, uint64_t obj, char *name)
1339ad135b5dSChristopher Siden {
1340ad135b5dSChristopher Siden 	char bytes[32];
1341ad135b5dSChristopher Siden 	bptree_phys_t *bt;
1342ad135b5dSChristopher Siden 	dmu_buf_t *db;
1343ad135b5dSChristopher Siden 
1344ad135b5dSChristopher Siden 	if (dump_opt['d'] < 3)
1345ad135b5dSChristopher Siden 		return;
1346ad135b5dSChristopher Siden 
1347b420f3adSRichard Lowe 	VERIFY3U(0, ==, dmu_bonus_hold(os, obj, FTAG, &db));
1348ad135b5dSChristopher Siden 	bt = db->db_data;
1349ad135b5dSChristopher Siden 	zdb_nicenum(bt->bt_bytes, bytes);
1350ad135b5dSChristopher Siden 	(void) printf("\n    %s: %llu datasets, %s\n",
1351ad135b5dSChristopher Siden 	    name, (unsigned long long)(bt->bt_end - bt->bt_begin), bytes);
1352ad135b5dSChristopher Siden 	dmu_buf_rele(db, FTAG);
1353ad135b5dSChristopher Siden 
1354ad135b5dSChristopher Siden 	if (dump_opt['d'] < 5)
1355ad135b5dSChristopher Siden 		return;
1356ad135b5dSChristopher Siden 
1357ad135b5dSChristopher Siden 	(void) printf("\n");
1358ad135b5dSChristopher Siden 
1359ad135b5dSChristopher Siden 	(void) bptree_iterate(os, obj, B_FALSE, dump_bptree_cb, NULL, NULL);
1360ad135b5dSChristopher Siden }
1361ad135b5dSChristopher Siden 
1362cde58dbcSMatthew Ahrens /* ARGSUSED */
1363cde58dbcSMatthew Ahrens static int
1364cde58dbcSMatthew Ahrens dump_bpobj_cb(void *arg, const blkptr_t *bp, dmu_tx_t *tx)
1365cde58dbcSMatthew Ahrens {
1366cde58dbcSMatthew Ahrens 	char blkbuf[BP_SPRINTF_LEN];
1367cde58dbcSMatthew Ahrens 
1368cde58dbcSMatthew Ahrens 	ASSERT(bp->blk_birth != 0);
136943466aaeSMax Grossman 	snprintf_blkptr_compact(blkbuf, sizeof (blkbuf), bp);
1370cde58dbcSMatthew Ahrens 	(void) printf("\t%s\n", blkbuf);
1371cde58dbcSMatthew Ahrens 	return (0);
1372cde58dbcSMatthew Ahrens }
1373cde58dbcSMatthew Ahrens 
1374fa9e4066Sahrens static void
1375d0475637SMatthew Ahrens dump_bpobj(bpobj_t *bpo, char *name, int indent)
1376fa9e4066Sahrens {
13773f9d6ad7SLin Ling 	char bytes[32];
13783f9d6ad7SLin Ling 	char comp[32];
13793f9d6ad7SLin Ling 	char uncomp[32];
1380fa9e4066Sahrens 
1381fa9e4066Sahrens 	if (dump_opt['d'] < 3)
1382fa9e4066Sahrens 		return;
1383fa9e4066Sahrens 
1384cde58dbcSMatthew Ahrens 	zdb_nicenum(bpo->bpo_phys->bpo_bytes, bytes);
1385d0475637SMatthew Ahrens 	if (bpo->bpo_havesubobj && bpo->bpo_phys->bpo_subobjs != 0) {
1386cde58dbcSMatthew Ahrens 		zdb_nicenum(bpo->bpo_phys->bpo_comp, comp);
1387cde58dbcSMatthew Ahrens 		zdb_nicenum(bpo->bpo_phys->bpo_uncomp, uncomp);
1388d0475637SMatthew Ahrens 		(void) printf("    %*s: object %llu, %llu local blkptrs, "
1389d0475637SMatthew Ahrens 		    "%llu subobjs, %s (%s/%s comp)\n",
1390d0475637SMatthew Ahrens 		    indent * 8, name,
1391d0475637SMatthew Ahrens 		    (u_longlong_t)bpo->bpo_object,
1392d0475637SMatthew Ahrens 		    (u_longlong_t)bpo->bpo_phys->bpo_num_blkptrs,
1393cde58dbcSMatthew Ahrens 		    (u_longlong_t)bpo->bpo_phys->bpo_num_subobjs,
139499653d4eSeschrock 		    bytes, comp, uncomp);
1395d0475637SMatthew Ahrens 
1396d0475637SMatthew Ahrens 		for (uint64_t i = 0; i < bpo->bpo_phys->bpo_num_subobjs; i++) {
1397d0475637SMatthew Ahrens 			uint64_t subobj;
1398d0475637SMatthew Ahrens 			bpobj_t subbpo;
1399d0475637SMatthew Ahrens 			int error;
1400d0475637SMatthew Ahrens 			VERIFY0(dmu_read(bpo->bpo_os,
1401d0475637SMatthew Ahrens 			    bpo->bpo_phys->bpo_subobjs,
1402d0475637SMatthew Ahrens 			    i * sizeof (subobj), sizeof (subobj), &subobj, 0));
1403d0475637SMatthew Ahrens 			error = bpobj_open(&subbpo, bpo->bpo_os, subobj);
1404d0475637SMatthew Ahrens 			if (error != 0) {
1405d0475637SMatthew Ahrens 				(void) printf("ERROR %u while trying to open "
1406d0475637SMatthew Ahrens 				    "subobj id %llu\n",
1407d0475637SMatthew Ahrens 				    error, (u_longlong_t)subobj);
1408d0475637SMatthew Ahrens 				continue;
1409d0475637SMatthew Ahrens 			}
1410d0475637SMatthew Ahrens 			dump_bpobj(&subbpo, "subobj", indent + 1);
141177061867SMatthew Ahrens 			bpobj_close(&subbpo);
1412d0475637SMatthew Ahrens 		}
141399653d4eSeschrock 	} else {
1414d0475637SMatthew Ahrens 		(void) printf("    %*s: object %llu, %llu blkptrs, %s\n",
1415d0475637SMatthew Ahrens 		    indent * 8, name,
1416d0475637SMatthew Ahrens 		    (u_longlong_t)bpo->bpo_object,
1417d0475637SMatthew Ahrens 		    (u_longlong_t)bpo->bpo_phys->bpo_num_blkptrs,
1418d0475637SMatthew Ahrens 		    bytes);
141999653d4eSeschrock 	}
1420fa9e4066Sahrens 
1421cde58dbcSMatthew Ahrens 	if (dump_opt['d'] < 5)
1422fa9e4066Sahrens 		return;
1423fa9e4066Sahrens 
1424fa9e4066Sahrens 
1425d0475637SMatthew Ahrens 	if (indent == 0) {
1426d0475637SMatthew Ahrens 		(void) bpobj_iterate_nofree(bpo, dump_bpobj_cb, NULL, NULL);
1427d0475637SMatthew Ahrens 		(void) printf("\n");
1428d0475637SMatthew Ahrens 	}
1429cde58dbcSMatthew Ahrens }
143044cd46caSbillm 
1431cde58dbcSMatthew Ahrens static void
1432cde58dbcSMatthew Ahrens dump_deadlist(dsl_deadlist_t *dl)
1433cde58dbcSMatthew Ahrens {
1434cde58dbcSMatthew Ahrens 	dsl_deadlist_entry_t *dle;
1435d0475637SMatthew Ahrens 	uint64_t unused;
1436cde58dbcSMatthew Ahrens 	char bytes[32];
1437cde58dbcSMatthew Ahrens 	char comp[32];
1438cde58dbcSMatthew Ahrens 	char uncomp[32];
1439cde58dbcSMatthew Ahrens 
1440cde58dbcSMatthew Ahrens 	if (dump_opt['d'] < 3)
1441cde58dbcSMatthew Ahrens 		return;
1442cde58dbcSMatthew Ahrens 
144390c76c66SMatthew Ahrens 	if (dl->dl_oldfmt) {
144490c76c66SMatthew Ahrens 		dump_bpobj(&dl->dl_bpobj, "old-format deadlist", 0);
144590c76c66SMatthew Ahrens 		return;
144690c76c66SMatthew Ahrens 	}
144790c76c66SMatthew Ahrens 
1448cde58dbcSMatthew Ahrens 	zdb_nicenum(dl->dl_phys->dl_used, bytes);
1449cde58dbcSMatthew Ahrens 	zdb_nicenum(dl->dl_phys->dl_comp, comp);
1450cde58dbcSMatthew Ahrens 	zdb_nicenum(dl->dl_phys->dl_uncomp, uncomp);
1451cde58dbcSMatthew Ahrens 	(void) printf("\n    Deadlist: %s (%s/%s comp)\n",
1452cde58dbcSMatthew Ahrens 	    bytes, comp, uncomp);
1453cde58dbcSMatthew Ahrens 
1454cde58dbcSMatthew Ahrens 	if (dump_opt['d'] < 4)
1455cde58dbcSMatthew Ahrens 		return;
1456cde58dbcSMatthew Ahrens 
1457cde58dbcSMatthew Ahrens 	(void) printf("\n");
1458cde58dbcSMatthew Ahrens 
1459d0475637SMatthew Ahrens 	/* force the tree to be loaded */
1460d0475637SMatthew Ahrens 	dsl_deadlist_space_range(dl, 0, UINT64_MAX, &unused, &unused, &unused);
1461d0475637SMatthew Ahrens 
1462cde58dbcSMatthew Ahrens 	for (dle = avl_first(&dl->dl_tree); dle;
1463cde58dbcSMatthew Ahrens 	    dle = AVL_NEXT(&dl->dl_tree, dle)) {
1464d0475637SMatthew Ahrens 		if (dump_opt['d'] >= 5) {
1465d0475637SMatthew Ahrens 			char buf[128];
1466d0475637SMatthew Ahrens 			(void) snprintf(buf, sizeof (buf), "mintxg %llu -> ",
1467d0475637SMatthew Ahrens 			    (longlong_t)dle->dle_mintxg,
1468d0475637SMatthew Ahrens 			    (longlong_t)dle->dle_bpobj.bpo_object);
1469fa9e4066Sahrens 
1470d0475637SMatthew Ahrens 			dump_bpobj(&dle->dle_bpobj, buf, 0);
1471d0475637SMatthew Ahrens 		} else {
1472d0475637SMatthew Ahrens 			(void) printf("mintxg %llu -> obj %llu\n",
1473d0475637SMatthew Ahrens 			    (longlong_t)dle->dle_mintxg,
1474d0475637SMatthew Ahrens 			    (longlong_t)dle->dle_bpobj.bpo_object);
1475d0475637SMatthew Ahrens 
1476d0475637SMatthew Ahrens 		}
1477cde58dbcSMatthew Ahrens 	}
1478fa9e4066Sahrens }
1479fa9e4066Sahrens 
1480e0d35c44Smarks static avl_tree_t idx_tree;
1481e0d35c44Smarks static avl_tree_t domain_tree;
1482e0d35c44Smarks static boolean_t fuid_table_loaded;
14830a586ceaSMark Shellenbaum static boolean_t sa_loaded;
14840a586ceaSMark Shellenbaum sa_attr_type_t *sa_attr_table;
1485e0d35c44Smarks 
1486e0d35c44Smarks static void
1487e0d35c44Smarks fuid_table_destroy()
1488e0d35c44Smarks {
1489e0d35c44Smarks 	if (fuid_table_loaded) {
1490e0d35c44Smarks 		zfs_fuid_table_destroy(&idx_tree, &domain_tree);
1491e0d35c44Smarks 		fuid_table_loaded = B_FALSE;
1492e0d35c44Smarks 	}
1493e0d35c44Smarks }
1494e0d35c44Smarks 
1495e0d35c44Smarks /*
1496e0d35c44Smarks  * print uid or gid information.
1497e0d35c44Smarks  * For normal POSIX id just the id is printed in decimal format.
1498e0d35c44Smarks  * For CIFS files with FUID the fuid is printed in hex followed by
1499d0475637SMatthew Ahrens  * the domain-rid string.
1500e0d35c44Smarks  */
1501e0d35c44Smarks static void
1502e0d35c44Smarks print_idstr(uint64_t id, const char *id_type)
1503e0d35c44Smarks {
1504e0d35c44Smarks 	if (FUID_INDEX(id)) {
1505e0d35c44Smarks 		char *domain;
1506e0d35c44Smarks 
1507e0d35c44Smarks 		domain = zfs_fuid_idx_domain(&idx_tree, FUID_INDEX(id));
1508e0d35c44Smarks 		(void) printf("\t%s     %llx [%s-%d]\n", id_type,
1509e0d35c44Smarks 		    (u_longlong_t)id, domain, (int)FUID_RID(id));
1510e0d35c44Smarks 	} else {
1511e0d35c44Smarks 		(void) printf("\t%s     %llu\n", id_type, (u_longlong_t)id);
1512e0d35c44Smarks 	}
1513e0d35c44Smarks 
1514e0d35c44Smarks }
1515e0d35c44Smarks 
1516e0d35c44Smarks static void
15170a586ceaSMark Shellenbaum dump_uidgid(objset_t *os, uint64_t uid, uint64_t gid)
1518e0d35c44Smarks {
1519e0d35c44Smarks 	uint32_t uid_idx, gid_idx;
1520e0d35c44Smarks 
15210a586ceaSMark Shellenbaum 	uid_idx = FUID_INDEX(uid);
15220a586ceaSMark Shellenbaum 	gid_idx = FUID_INDEX(gid);
1523e0d35c44Smarks 
1524e0d35c44Smarks 	/* Load domain table, if not already loaded */
1525e0d35c44Smarks 	if (!fuid_table_loaded && (uid_idx || gid_idx)) {
1526e0d35c44Smarks 		uint64_t fuid_obj;
1527e0d35c44Smarks 
1528e0d35c44Smarks 		/* first find the fuid object.  It lives in the master node */
1529e0d35c44Smarks 		VERIFY(zap_lookup(os, MASTER_NODE_OBJ, ZFS_FUID_TABLES,
1530e0d35c44Smarks 		    8, 1, &fuid_obj) == 0);
153189459e17SMark Shellenbaum 		zfs_fuid_avl_tree_create(&idx_tree, &domain_tree);
1532e0d35c44Smarks 		(void) zfs_fuid_table_load(os, fuid_obj,
1533e0d35c44Smarks 		    &idx_tree, &domain_tree);
1534e0d35c44Smarks 		fuid_table_loaded = B_TRUE;
1535e0d35c44Smarks 	}
1536e0d35c44Smarks 
15370a586ceaSMark Shellenbaum 	print_idstr(uid, "uid");
15380a586ceaSMark Shellenbaum 	print_idstr(gid, "gid");
1539e0d35c44Smarks }
1540e0d35c44Smarks 
1541fa9e4066Sahrens /*ARGSUSED*/
1542fa9e4066Sahrens static void
1543fa9e4066Sahrens dump_znode(objset_t *os, uint64_t object, void *data, size_t size)
1544fa9e4066Sahrens {
1545fa9e4066Sahrens 	char path[MAXPATHLEN * 2];	/* allow for xattr and failure prefix */
15460a586ceaSMark Shellenbaum 	sa_handle_t *hdl;
15470a586ceaSMark Shellenbaum 	uint64_t xattr, rdev, gen;
15480a586ceaSMark Shellenbaum 	uint64_t uid, gid, mode, fsize, parent, links;
15498f2529deSMark Shellenbaum 	uint64_t pflags;
15500a586ceaSMark Shellenbaum 	uint64_t acctm[2], modtm[2], chgtm[2], crtm[2];
15510a586ceaSMark Shellenbaum 	time_t z_crtime, z_atime, z_mtime, z_ctime;
15528f2529deSMark Shellenbaum 	sa_bulk_attr_t bulk[12];
15530a586ceaSMark Shellenbaum 	int idx = 0;
155455434c77Sek 	int error;
1555fa9e4066Sahrens 
15560a586ceaSMark Shellenbaum 	if (!sa_loaded) {
15570a586ceaSMark Shellenbaum 		uint64_t sa_attrs = 0;
15580a586ceaSMark Shellenbaum 		uint64_t version;
15590a586ceaSMark Shellenbaum 
15600a586ceaSMark Shellenbaum 		VERIFY(zap_lookup(os, MASTER_NODE_OBJ, ZPL_VERSION_STR,
15610a586ceaSMark Shellenbaum 		    8, 1, &version) == 0);
15620a586ceaSMark Shellenbaum 		if (version >= ZPL_VERSION_SA) {
15630a586ceaSMark Shellenbaum 			VERIFY(zap_lookup(os, MASTER_NODE_OBJ, ZFS_SA_ATTRS,
15640a586ceaSMark Shellenbaum 			    8, 1, &sa_attrs) == 0);
15650a586ceaSMark Shellenbaum 		}
15661d8ccc7bSMark Shellenbaum 		if ((error = sa_setup(os, sa_attrs, zfs_attr_table,
15671d8ccc7bSMark Shellenbaum 		    ZPL_END, &sa_attr_table)) != 0) {
15681d8ccc7bSMark Shellenbaum 			(void) printf("sa_setup failed errno %d, can't "
15691d8ccc7bSMark Shellenbaum 			    "display znode contents\n", error);
15701d8ccc7bSMark Shellenbaum 			return;
15711d8ccc7bSMark Shellenbaum 		}
15720a586ceaSMark Shellenbaum 		sa_loaded = B_TRUE;
15730a586ceaSMark Shellenbaum 	}
15740a586ceaSMark Shellenbaum 
15750a586ceaSMark Shellenbaum 	if (sa_handle_get(os, object, NULL, SA_HDL_PRIVATE, &hdl)) {
15760a586ceaSMark Shellenbaum 		(void) printf("Failed to get handle for SA znode\n");
15770a586ceaSMark Shellenbaum 		return;
15780a586ceaSMark Shellenbaum 	}
15790a586ceaSMark Shellenbaum 
15800a586ceaSMark Shellenbaum 	SA_ADD_BULK_ATTR(bulk, idx, sa_attr_table[ZPL_UID], NULL, &uid, 8);
15810a586ceaSMark Shellenbaum 	SA_ADD_BULK_ATTR(bulk, idx, sa_attr_table[ZPL_GID], NULL, &gid, 8);
15820a586ceaSMark Shellenbaum 	SA_ADD_BULK_ATTR(bulk, idx, sa_attr_table[ZPL_LINKS], NULL,
15830a586ceaSMark Shellenbaum 	    &links, 8);
15840a586ceaSMark Shellenbaum 	SA_ADD_BULK_ATTR(bulk, idx, sa_attr_table[ZPL_GEN], NULL, &gen, 8);
15850a586ceaSMark Shellenbaum 	SA_ADD_BULK_ATTR(bulk, idx, sa_attr_table[ZPL_MODE], NULL,
15860a586ceaSMark Shellenbaum 	    &mode, 8);
15870a586ceaSMark Shellenbaum 	SA_ADD_BULK_ATTR(bulk, idx, sa_attr_table[ZPL_PARENT],
15880a586ceaSMark Shellenbaum 	    NULL, &parent, 8);
15890a586ceaSMark Shellenbaum 	SA_ADD_BULK_ATTR(bulk, idx, sa_attr_table[ZPL_SIZE], NULL,
15900a586ceaSMark Shellenbaum 	    &fsize, 8);
15910a586ceaSMark Shellenbaum 	SA_ADD_BULK_ATTR(bulk, idx, sa_attr_table[ZPL_ATIME], NULL,
15920a586ceaSMark Shellenbaum 	    acctm, 16);
15930a586ceaSMark Shellenbaum 	SA_ADD_BULK_ATTR(bulk, idx, sa_attr_table[ZPL_MTIME], NULL,
15940a586ceaSMark Shellenbaum 	    modtm, 16);
15950a586ceaSMark Shellenbaum 	SA_ADD_BULK_ATTR(bulk, idx, sa_attr_table[ZPL_CRTIME], NULL,
15960a586ceaSMark Shellenbaum 	    crtm, 16);
15970a586ceaSMark Shellenbaum 	SA_ADD_BULK_ATTR(bulk, idx, sa_attr_table[ZPL_CTIME], NULL,
15980a586ceaSMark Shellenbaum 	    chgtm, 16);
15998f2529deSMark Shellenbaum 	SA_ADD_BULK_ATTR(bulk, idx, sa_attr_table[ZPL_FLAGS], NULL,
16008f2529deSMark Shellenbaum 	    &pflags, 8);
16010a586ceaSMark Shellenbaum 
16020a586ceaSMark Shellenbaum 	if (sa_bulk_lookup(hdl, bulk, idx)) {
16030a586ceaSMark Shellenbaum 		(void) sa_handle_destroy(hdl);
16040a586ceaSMark Shellenbaum 		return;
16050a586ceaSMark Shellenbaum 	}
1606fa9e4066Sahrens 
160755434c77Sek 	error = zfs_obj_to_path(os, object, path, sizeof (path));
160855434c77Sek 	if (error != 0) {
160955434c77Sek 		(void) snprintf(path, sizeof (path), "\?\?\?<object#%llu>",
161055434c77Sek 		    (u_longlong_t)object);
161155434c77Sek 	}
1612fa9e4066Sahrens 	if (dump_opt['d'] < 3) {
161355434c77Sek 		(void) printf("\t%s\n", path);
16140a586ceaSMark Shellenbaum 		(void) sa_handle_destroy(hdl);
1615fa9e4066Sahrens 		return;
1616fa9e4066Sahrens 	}
1617fa9e4066Sahrens 
16180a586ceaSMark Shellenbaum 	z_crtime = (time_t)crtm[0];
16190a586ceaSMark Shellenbaum 	z_atime = (time_t)acctm[0];
16200a586ceaSMark Shellenbaum 	z_mtime = (time_t)modtm[0];
16210a586ceaSMark Shellenbaum 	z_ctime = (time_t)chgtm[0];
1622fa9e4066Sahrens 
162355434c77Sek 	(void) printf("\tpath	%s\n", path);
16240a586ceaSMark Shellenbaum 	dump_uidgid(os, uid, gid);
1625fa9e4066Sahrens 	(void) printf("\tatime	%s", ctime(&z_atime));
1626fa9e4066Sahrens 	(void) printf("\tmtime	%s", ctime(&z_mtime));
1627fa9e4066Sahrens 	(void) printf("\tctime	%s", ctime(&z_ctime));
1628fa9e4066Sahrens 	(void) printf("\tcrtime	%s", ctime(&z_crtime));
16290a586ceaSMark Shellenbaum 	(void) printf("\tgen	%llu\n", (u_longlong_t)gen);
16300a586ceaSMark Shellenbaum 	(void) printf("\tmode	%llo\n", (u_longlong_t)mode);
16310a586ceaSMark Shellenbaum 	(void) printf("\tsize	%llu\n", (u_longlong_t)fsize);
16320a586ceaSMark Shellenbaum 	(void) printf("\tparent	%llu\n", (u_longlong_t)parent);
16330a586ceaSMark Shellenbaum 	(void) printf("\tlinks	%llu\n", (u_longlong_t)links);
16348f2529deSMark Shellenbaum 	(void) printf("\tpflags	%llx\n", (u_longlong_t)pflags);
16350a586ceaSMark Shellenbaum 	if (sa_lookup(hdl, sa_attr_table[ZPL_XATTR], &xattr,
16360a586ceaSMark Shellenbaum 	    sizeof (uint64_t)) == 0)
16370a586ceaSMark Shellenbaum 		(void) printf("\txattr	%llu\n", (u_longlong_t)xattr);
16380a586ceaSMark Shellenbaum 	if (sa_lookup(hdl, sa_attr_table[ZPL_RDEV], &rdev,
16390a586ceaSMark Shellenbaum 	    sizeof (uint64_t)) == 0)
16400a586ceaSMark Shellenbaum 		(void) printf("\trdev	0x%016llx\n", (u_longlong_t)rdev);
16410a586ceaSMark Shellenbaum 	sa_handle_destroy(hdl);
1642fa9e4066Sahrens }
1643fa9e4066Sahrens 
1644fa9e4066Sahrens /*ARGSUSED*/
1645fa9e4066Sahrens static void
1646fa9e4066Sahrens dump_acl(objset_t *os, uint64_t object, void *data, size_t size)
1647fa9e4066Sahrens {
1648fa9e4066Sahrens }
1649fa9e4066Sahrens 
1650fa9e4066Sahrens /*ARGSUSED*/
1651fa9e4066Sahrens static void
1652fa9e4066Sahrens dump_dmu_objset(objset_t *os, uint64_t object, void *data, size_t size)
1653fa9e4066Sahrens {
1654fa9e4066Sahrens }
1655fa9e4066Sahrens 
16566de8f417SVictor Latushkin static object_viewer_t *object_viewer[DMU_OT_NUMTYPES + 1] = {
1657fa9e4066Sahrens 	dump_none,		/* unallocated			*/
1658fa9e4066Sahrens 	dump_zap,		/* object directory		*/
1659fa9e4066Sahrens 	dump_uint64,		/* object array			*/
1660fa9e4066Sahrens 	dump_none,		/* packed nvlist		*/
1661fa9e4066Sahrens 	dump_packed_nvlist,	/* packed nvlist size		*/
1662fa9e4066Sahrens 	dump_none,		/* bplist			*/
1663fa9e4066Sahrens 	dump_none,		/* bplist header		*/
1664fa9e4066Sahrens 	dump_none,		/* SPA space map header		*/
1665fa9e4066Sahrens 	dump_none,		/* SPA space map		*/
1666fa9e4066Sahrens 	dump_none,		/* ZIL intent log		*/
1667fa9e4066Sahrens 	dump_dnode,		/* DMU dnode			*/
1668fa9e4066Sahrens 	dump_dmu_objset,	/* DMU objset			*/
1669ea8dc4b6Seschrock 	dump_dsl_dir,		/* DSL directory		*/
1670fa9e4066Sahrens 	dump_zap,		/* DSL directory child map	*/
1671fa9e4066Sahrens 	dump_zap,		/* DSL dataset snap map		*/
1672fa9e4066Sahrens 	dump_zap,		/* DSL props			*/
1673fa9e4066Sahrens 	dump_dsl_dataset,	/* DSL dataset			*/
1674fa9e4066Sahrens 	dump_znode,		/* ZFS znode			*/
1675da6c28aaSamw 	dump_acl,		/* ZFS V0 ACL			*/
1676fa9e4066Sahrens 	dump_uint8,		/* ZFS plain file		*/
1677e7437265Sahrens 	dump_zpldir,		/* ZFS directory		*/
1678fa9e4066Sahrens 	dump_zap,		/* ZFS master node		*/
1679fa9e4066Sahrens 	dump_zap,		/* ZFS delete queue		*/
1680fa9e4066Sahrens 	dump_uint8,		/* zvol object			*/
1681fa9e4066Sahrens 	dump_zap,		/* zvol prop			*/
1682fa9e4066Sahrens 	dump_uint8,		/* other uint8[]		*/
1683fa9e4066Sahrens 	dump_uint64,		/* other uint64[]		*/
1684fa9e4066Sahrens 	dump_zap,		/* other ZAP			*/
1685ea8dc4b6Seschrock 	dump_zap,		/* persistent error log		*/
168606eeb2adSek 	dump_uint8,		/* SPA history			*/
16874445fffbSMatthew Ahrens 	dump_history_offsets,	/* SPA history offsets		*/
1688b1b8ab34Slling 	dump_zap,		/* Pool properties		*/
1689ecd6cf80Smarks 	dump_zap,		/* DSL permissions		*/
1690da6c28aaSamw 	dump_acl,		/* ZFS ACL			*/
1691da6c28aaSamw 	dump_uint8,		/* ZFS SYSACL			*/
1692da6c28aaSamw 	dump_none,		/* FUID nvlist			*/
1693da6c28aaSamw 	dump_packed_nvlist,	/* FUID nvlist size		*/
1694088f3894Sahrens 	dump_zap,		/* DSL dataset next clones	*/
1695088f3894Sahrens 	dump_zap,		/* DSL scrub queue		*/
169614843421SMatthew Ahrens 	dump_zap,		/* ZFS user/group used		*/
169714843421SMatthew Ahrens 	dump_zap,		/* ZFS user/group quota		*/
1698842727c2SChris Kirby 	dump_zap,		/* snapshot refcount tags	*/
1699486ae710SMatthew Ahrens 	dump_ddt_zap,		/* DDT ZAP object		*/
1700b24ab676SJeff Bonwick 	dump_zap,		/* DDT statistics		*/
17010a586ceaSMark Shellenbaum 	dump_znode,		/* SA object			*/
17020a586ceaSMark Shellenbaum 	dump_zap,		/* SA Master Node		*/
17030a586ceaSMark Shellenbaum 	dump_sa_attrs,		/* SA attribute registration	*/
17040a586ceaSMark Shellenbaum 	dump_sa_layouts,	/* SA attribute layouts		*/
17053f9d6ad7SLin Ling 	dump_zap,		/* DSL scrub translations	*/
17063f9d6ad7SLin Ling 	dump_none,		/* fake dedup BP		*/
1707cde58dbcSMatthew Ahrens 	dump_zap,		/* deadlist			*/
1708cde58dbcSMatthew Ahrens 	dump_none,		/* deadlist hdr			*/
1709cde58dbcSMatthew Ahrens 	dump_zap,		/* dsl clones			*/
1710cde58dbcSMatthew Ahrens 	dump_none,		/* bpobj subobjs		*/
17110a586ceaSMark Shellenbaum 	dump_unknown,		/* Unknown type, must be last	*/
1712fa9e4066Sahrens };
1713fa9e4066Sahrens 
1714fa9e4066Sahrens static void
1715fa9e4066Sahrens dump_object(objset_t *os, uint64_t object, int verbosity, int *print_header)
1716fa9e4066Sahrens {
1717fa9e4066Sahrens 	dmu_buf_t *db = NULL;
1718fa9e4066Sahrens 	dmu_object_info_t doi;
1719fa9e4066Sahrens 	dnode_t *dn;
1720fa9e4066Sahrens 	void *bonus = NULL;
1721fa9e4066Sahrens 	size_t bsize = 0;
17223f9d6ad7SLin Ling 	char iblk[32], dblk[32], lsize[32], asize[32], fill[32];
17233f9d6ad7SLin Ling 	char bonus_size[32];
1724fa9e4066Sahrens 	char aux[50];
1725fa9e4066Sahrens 	int error;
1726fa9e4066Sahrens 
1727fa9e4066Sahrens 	if (*print_header) {
1728b24ab676SJeff Bonwick 		(void) printf("\n%10s  %3s  %5s  %5s  %5s  %5s  %6s  %s\n",
1729b24ab676SJeff Bonwick 		    "Object", "lvl", "iblk", "dblk", "dsize", "lsize",
1730b24ab676SJeff Bonwick 		    "%full", "type");
1731fa9e4066Sahrens 		*print_header = 0;
1732fa9e4066Sahrens 	}
1733fa9e4066Sahrens 
1734fa9e4066Sahrens 	if (object == 0) {
1735744947dcSTom Erickson 		dn = DMU_META_DNODE(os);
1736fa9e4066Sahrens 	} else {
1737ea8dc4b6Seschrock 		error = dmu_bonus_hold(os, object, FTAG, &db);
1738ea8dc4b6Seschrock 		if (error)
1739ea8dc4b6Seschrock 			fatal("dmu_bonus_hold(%llu) failed, errno %u",
1740ea8dc4b6Seschrock 			    object, error);
1741fa9e4066Sahrens 		bonus = db->db_data;
1742fa9e4066Sahrens 		bsize = db->db_size;
1743744947dcSTom Erickson 		dn = DB_DNODE((dmu_buf_impl_t *)db);
1744fa9e4066Sahrens 	}
1745fa9e4066Sahrens 	dmu_object_info_from_dnode(dn, &doi);
1746fa9e4066Sahrens 
17473f9d6ad7SLin Ling 	zdb_nicenum(doi.doi_metadata_block_size, iblk);
17483f9d6ad7SLin Ling 	zdb_nicenum(doi.doi_data_block_size, dblk);
17493f9d6ad7SLin Ling 	zdb_nicenum(doi.doi_max_offset, lsize);
17503f9d6ad7SLin Ling 	zdb_nicenum(doi.doi_physical_blocks_512 << 9, asize);
17513f9d6ad7SLin Ling 	zdb_nicenum(doi.doi_bonus_size, bonus_size);
1752b24ab676SJeff Bonwick 	(void) sprintf(fill, "%6.2f", 100.0 * doi.doi_fill_count *
1753bbfd46c4SJeff Bonwick 	    doi.doi_data_block_size / (object == 0 ? DNODES_PER_BLOCK : 1) /
1754bbfd46c4SJeff Bonwick 	    doi.doi_max_offset);
1755fa9e4066Sahrens 
1756fa9e4066Sahrens 	aux[0] = '\0';
1757fa9e4066Sahrens 
1758e7437265Sahrens 	if (doi.doi_checksum != ZIO_CHECKSUM_INHERIT || verbosity >= 6) {
1759fa9e4066Sahrens 		(void) snprintf(aux + strlen(aux), sizeof (aux), " (K=%s)",
17606de8f417SVictor Latushkin 		    ZDB_CHECKSUM_NAME(doi.doi_checksum));
1761e7437265Sahrens 	}
1762fa9e4066Sahrens 
1763e7437265Sahrens 	if (doi.doi_compress != ZIO_COMPRESS_INHERIT || verbosity >= 6) {
1764fa9e4066Sahrens 		(void) snprintf(aux + strlen(aux), sizeof (aux), " (Z=%s)",
17656de8f417SVictor Latushkin 		    ZDB_COMPRESS_NAME(doi.doi_compress));
1766e7437265Sahrens 	}
1767fa9e4066Sahrens 
1768b24ab676SJeff Bonwick 	(void) printf("%10lld  %3u  %5s  %5s  %5s  %5s  %6s  %s%s\n",
1769b24ab676SJeff Bonwick 	    (u_longlong_t)object, doi.doi_indirection, iblk, dblk,
1770b24ab676SJeff Bonwick 	    asize, lsize, fill, ZDB_OT_NAME(doi.doi_type), aux);
1771fa9e4066Sahrens 
1772fa9e4066Sahrens 	if (doi.doi_bonus_type != DMU_OT_NONE && verbosity > 3) {
1773b24ab676SJeff Bonwick 		(void) printf("%10s  %3s  %5s  %5s  %5s  %5s  %6s  %s\n",
1774b24ab676SJeff Bonwick 		    "", "", "", "", "", bonus_size, "bonus",
17756de8f417SVictor Latushkin 		    ZDB_OT_NAME(doi.doi_bonus_type));
1776fa9e4066Sahrens 	}
1777fa9e4066Sahrens 
1778fa9e4066Sahrens 	if (verbosity >= 4) {
17790a586ceaSMark Shellenbaum 		(void) printf("\tdnode flags: %s%s%s\n",
178014843421SMatthew Ahrens 		    (dn->dn_phys->dn_flags & DNODE_FLAG_USED_BYTES) ?
178114843421SMatthew Ahrens 		    "USED_BYTES " : "",
178214843421SMatthew Ahrens 		    (dn->dn_phys->dn_flags & DNODE_FLAG_USERUSED_ACCOUNTED) ?
17830a586ceaSMark Shellenbaum 		    "USERUSED_ACCOUNTED " : "",
17840a586ceaSMark Shellenbaum 		    (dn->dn_phys->dn_flags & DNODE_FLAG_SPILL_BLKPTR) ?
17850a586ceaSMark Shellenbaum 		    "SPILL_BLKPTR" : "");
178614843421SMatthew Ahrens 		(void) printf("\tdnode maxblkid: %llu\n",
178714843421SMatthew Ahrens 		    (longlong_t)dn->dn_phys->dn_maxblkid);
178814843421SMatthew Ahrens 
17896de8f417SVictor Latushkin 		object_viewer[ZDB_OT_TYPE(doi.doi_bonus_type)](os, object,
17906de8f417SVictor Latushkin 		    bonus, bsize);
17916de8f417SVictor Latushkin 		object_viewer[ZDB_OT_TYPE(doi.doi_type)](os, object, NULL, 0);
1792fa9e4066Sahrens 		*print_header = 1;
1793fa9e4066Sahrens 	}
1794fa9e4066Sahrens 
1795fa9e4066Sahrens 	if (verbosity >= 5)
179688b7b0f2SMatthew Ahrens 		dump_indirect(dn);
1797fa9e4066Sahrens 
1798fa9e4066Sahrens 	if (verbosity >= 5) {
1799fa9e4066Sahrens 		/*
1800fa9e4066Sahrens 		 * Report the list of segments that comprise the object.
1801fa9e4066Sahrens 		 */
1802fa9e4066Sahrens 		uint64_t start = 0;
1803fa9e4066Sahrens 		uint64_t end;
1804fa9e4066Sahrens 		uint64_t blkfill = 1;
1805fa9e4066Sahrens 		int minlvl = 1;
1806fa9e4066Sahrens 
1807fa9e4066Sahrens 		if (dn->dn_type == DMU_OT_DNODE) {
1808fa9e4066Sahrens 			minlvl = 0;
1809fa9e4066Sahrens 			blkfill = DNODES_PER_BLOCK;
1810fa9e4066Sahrens 		}
1811fa9e4066Sahrens 
1812fa9e4066Sahrens 		for (;;) {
18133f9d6ad7SLin Ling 			char segsize[32];
1814cdb0ab79Smaybee 			error = dnode_next_offset(dn,
1815cdb0ab79Smaybee 			    0, &start, minlvl, blkfill, 0);
1816fa9e4066Sahrens 			if (error)
1817fa9e4066Sahrens 				break;
1818fa9e4066Sahrens 			end = start;
1819cdb0ab79Smaybee 			error = dnode_next_offset(dn,
1820cdb0ab79Smaybee 			    DNODE_FIND_HOLE, &end, minlvl, blkfill, 0);
18213f9d6ad7SLin Ling 			zdb_nicenum(end - start, segsize);
1822fa9e4066Sahrens 			(void) printf("\t\tsegment [%016llx, %016llx)"
1823fa9e4066Sahrens 			    " size %5s\n", (u_longlong_t)start,
1824fa9e4066Sahrens 			    (u_longlong_t)end, segsize);
1825fa9e4066Sahrens 			if (error)
1826fa9e4066Sahrens 				break;
1827fa9e4066Sahrens 			start = end;
1828fa9e4066Sahrens 		}
1829fa9e4066Sahrens 	}
1830fa9e4066Sahrens 
1831fa9e4066Sahrens 	if (db != NULL)
1832ea8dc4b6Seschrock 		dmu_buf_rele(db, FTAG);
1833fa9e4066Sahrens }
1834fa9e4066Sahrens 
1835fa9e4066Sahrens static char *objset_types[DMU_OST_NUMTYPES] = {
1836fa9e4066Sahrens 	"NONE", "META", "ZPL", "ZVOL", "OTHER", "ANY" };
1837fa9e4066Sahrens 
1838fa9e4066Sahrens static void
1839fa9e4066Sahrens dump_dir(objset_t *os)
1840fa9e4066Sahrens {
1841fa9e4066Sahrens 	dmu_objset_stats_t dds;
1842fa9e4066Sahrens 	uint64_t object, object_count;
1843a2eea2e1Sahrens 	uint64_t refdbytes, usedobjs, scratch;
18443f9d6ad7SLin Ling 	char numbuf[32];
184514843421SMatthew Ahrens 	char blkbuf[BP_SPRINTF_LEN + 20];
1846fa9e4066Sahrens 	char osname[MAXNAMELEN];
1847fa9e4066Sahrens 	char *type = "UNKNOWN";
1848fa9e4066Sahrens 	int verbosity = dump_opt['d'];
1849fa9e4066Sahrens 	int print_header = 1;
1850fa9e4066Sahrens 	int i, error;
1851fa9e4066Sahrens 
18523b2aab18SMatthew Ahrens 	dsl_pool_config_enter(dmu_objset_pool(os), FTAG);
1853a2eea2e1Sahrens 	dmu_objset_fast_stat(os, &dds);
18543b2aab18SMatthew Ahrens 	dsl_pool_config_exit(dmu_objset_pool(os), FTAG);
1855fa9e4066Sahrens 
1856fa9e4066Sahrens 	if (dds.dds_type < DMU_OST_NUMTYPES)
1857fa9e4066Sahrens 		type = objset_types[dds.dds_type];
1858fa9e4066Sahrens 
1859fa9e4066Sahrens 	if (dds.dds_type == DMU_OST_META) {
1860fa9e4066Sahrens 		dds.dds_creation_txg = TXG_INITIAL;
18615d7b4d43SMatthew Ahrens 		usedobjs = BP_GET_FILL(os->os_rootbp);
1862c1379625SJustin T. Gibbs 		refdbytes = dsl_dir_phys(os->os_spa->spa_dsl_pool->dp_mos_dir)->
1863c1379625SJustin T. Gibbs 		    dd_used_bytes;
1864a2eea2e1Sahrens 	} else {
1865a2eea2e1Sahrens 		dmu_objset_space(os, &refdbytes, &scratch, &usedobjs, &scratch);
1866fa9e4066Sahrens 	}
1867fa9e4066Sahrens 
18685d7b4d43SMatthew Ahrens 	ASSERT3U(usedobjs, ==, BP_GET_FILL(os->os_rootbp));
1869fa9e4066Sahrens 
18703f9d6ad7SLin Ling 	zdb_nicenum(refdbytes, numbuf);
1871fa9e4066Sahrens 
1872fa9e4066Sahrens 	if (verbosity >= 4) {
187343466aaeSMax Grossman 		(void) snprintf(blkbuf, sizeof (blkbuf), ", rootbp ");
187443466aaeSMax Grossman 		(void) snprintf_blkptr(blkbuf + strlen(blkbuf),
187543466aaeSMax Grossman 		    sizeof (blkbuf) - strlen(blkbuf), os->os_rootbp);
1876fa9e4066Sahrens 	} else {
1877fa9e4066Sahrens 		blkbuf[0] = '\0';
1878fa9e4066Sahrens 	}
1879fa9e4066Sahrens 
1880fa9e4066Sahrens 	dmu_objset_name(os, osname);
1881fa9e4066Sahrens 
1882a2eea2e1Sahrens 	(void) printf("Dataset %s [%s], ID %llu, cr_txg %llu, "
1883fa9e4066Sahrens 	    "%s, %llu objects%s\n",
1884fa9e4066Sahrens 	    osname, type, (u_longlong_t)dmu_objset_id(os),
1885fa9e4066Sahrens 	    (u_longlong_t)dds.dds_creation_txg,
1886a2eea2e1Sahrens 	    numbuf, (u_longlong_t)usedobjs, blkbuf);
1887fa9e4066Sahrens 
1888b24ab676SJeff Bonwick 	if (zopt_objects != 0) {
1889b24ab676SJeff Bonwick 		for (i = 0; i < zopt_objects; i++)
1890b24ab676SJeff Bonwick 			dump_object(os, zopt_object[i], verbosity,
1891b24ab676SJeff Bonwick 			    &print_header);
1892b24ab676SJeff Bonwick 		(void) printf("\n");
1893b24ab676SJeff Bonwick 		return;
1894b24ab676SJeff Bonwick 	}
1895b24ab676SJeff Bonwick 
1896b24ab676SJeff Bonwick 	if (dump_opt['i'] != 0 || verbosity >= 2)
1897b24ab676SJeff Bonwick 		dump_intent_log(dmu_objset_zil(os));
1898fa9e4066Sahrens 
1899fa9e4066Sahrens 	if (dmu_objset_ds(os) != NULL)
1900cde58dbcSMatthew Ahrens 		dump_deadlist(&dmu_objset_ds(os)->ds_deadlist);
1901fa9e4066Sahrens 
1902fa9e4066Sahrens 	if (verbosity < 2)
1903fa9e4066Sahrens 		return;
1904fa9e4066Sahrens 
190543466aaeSMax Grossman 	if (BP_IS_HOLE(os->os_rootbp))
1906088f3894Sahrens 		return;
1907088f3894Sahrens 
1908fa9e4066Sahrens 	dump_object(os, 0, verbosity, &print_header);
190914843421SMatthew Ahrens 	object_count = 0;
1910744947dcSTom Erickson 	if (DMU_USERUSED_DNODE(os) != NULL &&
1911744947dcSTom Erickson 	    DMU_USERUSED_DNODE(os)->dn_type != 0) {
191214843421SMatthew Ahrens 		dump_object(os, DMU_USERUSED_OBJECT, verbosity, &print_header);
191314843421SMatthew Ahrens 		dump_object(os, DMU_GROUPUSED_OBJECT, verbosity, &print_header);
191414843421SMatthew Ahrens 	}
1915fa9e4066Sahrens 
1916fa9e4066Sahrens 	object = 0;
19176754306eSahrens 	while ((error = dmu_object_next(os, &object, B_FALSE, 0)) == 0) {
1918fa9e4066Sahrens 		dump_object(os, object, verbosity, &print_header);
1919fa9e4066Sahrens 		object_count++;
1920fa9e4066Sahrens 	}
1921fa9e4066Sahrens 
1922a2eea2e1Sahrens 	ASSERT3U(object_count, ==, usedobjs);
1923fa9e4066Sahrens 
1924fa9e4066Sahrens 	(void) printf("\n");
1925fa9e4066Sahrens 
1926ccba0801SRich Morris 	if (error != ESRCH) {
1927ccba0801SRich Morris 		(void) fprintf(stderr, "dmu_object_next() = %d\n", error);
1928ccba0801SRich Morris 		abort();
1929ccba0801SRich Morris 	}
1930fa9e4066Sahrens }
1931fa9e4066Sahrens 
1932fa9e4066Sahrens static void
193353b9a4a9SVictor Latushkin dump_uberblock(uberblock_t *ub, const char *header, const char *footer)
1934fa9e4066Sahrens {
1935fa9e4066Sahrens 	time_t timestamp = ub->ub_timestamp;
1936fa9e4066Sahrens 
193753b9a4a9SVictor Latushkin 	(void) printf(header ? header : "");
1938fa9e4066Sahrens 	(void) printf("\tmagic = %016llx\n", (u_longlong_t)ub->ub_magic);
1939fa9e4066Sahrens 	(void) printf("\tversion = %llu\n", (u_longlong_t)ub->ub_version);
1940fa9e4066Sahrens 	(void) printf("\ttxg = %llu\n", (u_longlong_t)ub->ub_txg);
1941fa9e4066Sahrens 	(void) printf("\tguid_sum = %llu\n", (u_longlong_t)ub->ub_guid_sum);
1942fa9e4066Sahrens 	(void) printf("\ttimestamp = %llu UTC = %s",
1943fa9e4066Sahrens 	    (u_longlong_t)ub->ub_timestamp, asctime(localtime(&timestamp)));
1944fa9e4066Sahrens 	if (dump_opt['u'] >= 3) {
1945fbabab8fSmaybee 		char blkbuf[BP_SPRINTF_LEN];
194643466aaeSMax Grossman 		snprintf_blkptr(blkbuf, sizeof (blkbuf), &ub->ub_rootbp);
1947fa9e4066Sahrens 		(void) printf("\trootbp = %s\n", blkbuf);
1948fa9e4066Sahrens 	}
194953b9a4a9SVictor Latushkin 	(void) printf(footer ? footer : "");
1950fa9e4066Sahrens }
1951fa9e4066Sahrens 
1952fa9e4066Sahrens static void
195307428bdfSVictor Latushkin dump_config(spa_t *spa)
1954fa9e4066Sahrens {
195507428bdfSVictor Latushkin 	dmu_buf_t *db;
195607428bdfSVictor Latushkin 	size_t nvsize = 0;
195707428bdfSVictor Latushkin 	int error = 0;
195807428bdfSVictor Latushkin 
1959fa9e4066Sahrens 
196007428bdfSVictor Latushkin 	error = dmu_bonus_hold(spa->spa_meta_objset,
196107428bdfSVictor Latushkin 	    spa->spa_config_object, FTAG, &db);
196207428bdfSVictor Latushkin 
196307428bdfSVictor Latushkin 	if (error == 0) {
196407428bdfSVictor Latushkin 		nvsize = *(uint64_t *)db->db_data;
196507428bdfSVictor Latushkin 		dmu_buf_rele(db, FTAG);
196607428bdfSVictor Latushkin 
196707428bdfSVictor Latushkin 		(void) printf("\nMOS Configuration:\n");
196807428bdfSVictor Latushkin 		dump_packed_nvlist(spa->spa_meta_objset,
196907428bdfSVictor Latushkin 		    spa->spa_config_object, (void *)&nvsize, 1);
197007428bdfSVictor Latushkin 	} else {
197107428bdfSVictor Latushkin 		(void) fprintf(stderr, "dmu_bonus_hold(%llu) failed, errno %d",
197207428bdfSVictor Latushkin 		    (u_longlong_t)spa->spa_config_object, error);
1973fa9e4066Sahrens 	}
1974fa9e4066Sahrens }
1975fa9e4066Sahrens 
1976c5904d13Seschrock static void
1977c5904d13Seschrock dump_cachefile(const char *cachefile)
1978c5904d13Seschrock {
1979c5904d13Seschrock 	int fd;
1980c5904d13Seschrock 	struct stat64 statbuf;
1981c5904d13Seschrock 	char *buf;
1982c5904d13Seschrock 	nvlist_t *config;
1983c5904d13Seschrock 
1984c5904d13Seschrock 	if ((fd = open64(cachefile, O_RDONLY)) < 0) {
1985c5904d13Seschrock 		(void) printf("cannot open '%s': %s\n", cachefile,
1986c5904d13Seschrock 		    strerror(errno));
1987c5904d13Seschrock 		exit(1);
1988c5904d13Seschrock 	}
1989c5904d13Seschrock 
1990c5904d13Seschrock 	if (fstat64(fd, &statbuf) != 0) {
1991c5904d13Seschrock 		(void) printf("failed to stat '%s': %s\n", cachefile,
1992c5904d13Seschrock 		    strerror(errno));
1993c5904d13Seschrock 		exit(1);
1994c5904d13Seschrock 	}
1995c5904d13Seschrock 
1996c5904d13Seschrock 	if ((buf = malloc(statbuf.st_size)) == NULL) {
1997c5904d13Seschrock 		(void) fprintf(stderr, "failed to allocate %llu bytes\n",
1998c5904d13Seschrock 		    (u_longlong_t)statbuf.st_size);
1999c5904d13Seschrock 		exit(1);
2000c5904d13Seschrock 	}
2001c5904d13Seschrock 
2002c5904d13Seschrock 	if (read(fd, buf, statbuf.st_size) != statbuf.st_size) {
2003c5904d13Seschrock 		(void) fprintf(stderr, "failed to read %llu bytes\n",
2004c5904d13Seschrock 		    (u_longlong_t)statbuf.st_size);
2005c5904d13Seschrock 		exit(1);
2006c5904d13Seschrock 	}
2007c5904d13Seschrock 
2008c5904d13Seschrock 	(void) close(fd);
2009c5904d13Seschrock 
2010c5904d13Seschrock 	if (nvlist_unpack(buf, statbuf.st_size, &config, 0) != 0) {
2011c5904d13Seschrock 		(void) fprintf(stderr, "failed to unpack nvlist\n");
2012c5904d13Seschrock 		exit(1);
2013c5904d13Seschrock 	}
2014c5904d13Seschrock 
2015c5904d13Seschrock 	free(buf);
2016c5904d13Seschrock 
2017c5904d13Seschrock 	dump_nvlist(config, 0);
2018c5904d13Seschrock 
2019c5904d13Seschrock 	nvlist_free(config);
2020c5904d13Seschrock }
2021c5904d13Seschrock 
202253b9a4a9SVictor Latushkin #define	ZDB_MAX_UB_HEADER_SIZE 32
202353b9a4a9SVictor Latushkin 
202453b9a4a9SVictor Latushkin static void
202553b9a4a9SVictor Latushkin dump_label_uberblocks(vdev_label_t *lbl, uint64_t ashift)
202653b9a4a9SVictor Latushkin {
202753b9a4a9SVictor Latushkin 	vdev_t vd;
202853b9a4a9SVictor Latushkin 	vdev_t *vdp = &vd;
202953b9a4a9SVictor Latushkin 	char header[ZDB_MAX_UB_HEADER_SIZE];
203053b9a4a9SVictor Latushkin 
203153b9a4a9SVictor Latushkin 	vd.vdev_ashift = ashift;
203253b9a4a9SVictor Latushkin 	vdp->vdev_top = vdp;
203353b9a4a9SVictor Latushkin 
203453b9a4a9SVictor Latushkin 	for (int i = 0; i < VDEV_UBERBLOCK_COUNT(vdp); i++) {
203553b9a4a9SVictor Latushkin 		uint64_t uoff = VDEV_UBERBLOCK_OFFSET(vdp, i);
203653b9a4a9SVictor Latushkin 		uberblock_t *ub = (void *)((char *)lbl + uoff);
203753b9a4a9SVictor Latushkin 
203853b9a4a9SVictor Latushkin 		if (uberblock_verify(ub))
203953b9a4a9SVictor Latushkin 			continue;
204053b9a4a9SVictor Latushkin 		(void) snprintf(header, ZDB_MAX_UB_HEADER_SIZE,
204153b9a4a9SVictor Latushkin 		    "Uberblock[%d]\n", i);
204253b9a4a9SVictor Latushkin 		dump_uberblock(ub, header, "");
204353b9a4a9SVictor Latushkin 	}
204453b9a4a9SVictor Latushkin }
204553b9a4a9SVictor Latushkin 
2046fa9e4066Sahrens static void
2047fa9e4066Sahrens dump_label(const char *dev)
2048fa9e4066Sahrens {
2049fa9e4066Sahrens 	int fd;
2050fa9e4066Sahrens 	vdev_label_t label;
2051c6065d0fSGeorge Wilson 	char *path, *buf = label.vl_vdev_phys.vp_nvlist;
2052fa9e4066Sahrens 	size_t buflen = sizeof (label.vl_vdev_phys.vp_nvlist);
2053fa9e4066Sahrens 	struct stat64 statbuf;
205453b9a4a9SVictor Latushkin 	uint64_t psize, ashift;
2055c6065d0fSGeorge Wilson 	int len = strlen(dev) + 1;
2056fa9e4066Sahrens 
2057c6065d0fSGeorge Wilson 	if (strncmp(dev, "/dev/dsk/", 9) == 0) {
2058c6065d0fSGeorge Wilson 		len++;
2059c6065d0fSGeorge Wilson 		path = malloc(len);
2060c6065d0fSGeorge Wilson 		(void) snprintf(path, len, "%s%s", "/dev/rdsk/", dev + 9);
2061c6065d0fSGeorge Wilson 	} else {
2062c6065d0fSGeorge Wilson 		path = strdup(dev);
2063c6065d0fSGeorge Wilson 	}
2064c6065d0fSGeorge Wilson 
2065c6065d0fSGeorge Wilson 	if ((fd = open64(path, O_RDONLY)) < 0) {
2066c6065d0fSGeorge Wilson 		(void) printf("cannot open '%s': %s\n", path, strerror(errno));
2067c6065d0fSGeorge Wilson 		free(path);
2068fa9e4066Sahrens 		exit(1);
2069fa9e4066Sahrens 	}
2070fa9e4066Sahrens 
2071fa9e4066Sahrens 	if (fstat64(fd, &statbuf) != 0) {
2072c6065d0fSGeorge Wilson 		(void) printf("failed to stat '%s': %s\n", path,
2073fa9e4066Sahrens 		    strerror(errno));
2074c6065d0fSGeorge Wilson 		free(path);
2075c6065d0fSGeorge Wilson 		(void) close(fd);
2076c6065d0fSGeorge Wilson 		exit(1);
2077c6065d0fSGeorge Wilson 	}
2078c6065d0fSGeorge Wilson 
2079c6065d0fSGeorge Wilson 	if (S_ISBLK(statbuf.st_mode)) {
2080c6065d0fSGeorge Wilson 		(void) printf("cannot use '%s': character device required\n",
2081c6065d0fSGeorge Wilson 		    path);
2082c6065d0fSGeorge Wilson 		free(path);
2083c6065d0fSGeorge Wilson 		(void) close(fd);
2084c6065d0fSGeorge Wilson 		exit(1);
2085fa9e4066Sahrens 	}
2086fa9e4066Sahrens 
2087fa9e4066Sahrens 	psize = statbuf.st_size;
2088fa9e4066Sahrens 	psize = P2ALIGN(psize, (uint64_t)sizeof (vdev_label_t));
2089fa9e4066Sahrens 
209053b9a4a9SVictor Latushkin 	for (int l = 0; l < VDEV_LABELS; l++) {
2091fa9e4066Sahrens 		nvlist_t *config = NULL;
2092fa9e4066Sahrens 
2093fa9e4066Sahrens 		(void) printf("--------------------------------------------\n");
2094fa9e4066Sahrens 		(void) printf("LABEL %d\n", l);
2095fa9e4066Sahrens 		(void) printf("--------------------------------------------\n");
2096fa9e4066Sahrens 
20970d981225Seschrock 		if (pread64(fd, &label, sizeof (label),
2098fa9e4066Sahrens 		    vdev_label_offset(psize, l, 0)) != sizeof (label)) {
2099fa9e4066Sahrens 			(void) printf("failed to read label %d\n", l);
2100fa9e4066Sahrens 			continue;
2101fa9e4066Sahrens 		}
2102fa9e4066Sahrens 
2103fa9e4066Sahrens 		if (nvlist_unpack(buf, buflen, &config, 0) != 0) {
2104fa9e4066Sahrens 			(void) printf("failed to unpack label %d\n", l);
210553b9a4a9SVictor Latushkin 			ashift = SPA_MINBLOCKSHIFT;
210653b9a4a9SVictor Latushkin 		} else {
210753b9a4a9SVictor Latushkin 			nvlist_t *vdev_tree = NULL;
210853b9a4a9SVictor Latushkin 
210953b9a4a9SVictor Latushkin 			dump_nvlist(config, 4);
211053b9a4a9SVictor Latushkin 			if ((nvlist_lookup_nvlist(config,
211153b9a4a9SVictor Latushkin 			    ZPOOL_CONFIG_VDEV_TREE, &vdev_tree) != 0) ||
211253b9a4a9SVictor Latushkin 			    (nvlist_lookup_uint64(vdev_tree,
211353b9a4a9SVictor Latushkin 			    ZPOOL_CONFIG_ASHIFT, &ashift) != 0))
211453b9a4a9SVictor Latushkin 				ashift = SPA_MINBLOCKSHIFT;
211553b9a4a9SVictor Latushkin 			nvlist_free(config);
2116fa9e4066Sahrens 		}
211753b9a4a9SVictor Latushkin 		if (dump_opt['u'])
211853b9a4a9SVictor Latushkin 			dump_label_uberblocks(&label, ashift);
2119fa9e4066Sahrens 	}
2120c6065d0fSGeorge Wilson 
2121c6065d0fSGeorge Wilson 	free(path);
2122c6065d0fSGeorge Wilson 	(void) close(fd);
2123fa9e4066Sahrens }
2124fa9e4066Sahrens 
2125b5152584SMatthew Ahrens static uint64_t num_large_blocks;
2126b5152584SMatthew Ahrens 
2127fa9e4066Sahrens /*ARGSUSED*/
21281d452cf5Sahrens static int
2129fd136879SMatthew Ahrens dump_one_dir(const char *dsname, void *arg)
2130fa9e4066Sahrens {
2131fa9e4066Sahrens 	int error;
2132fa9e4066Sahrens 	objset_t *os;
2133fa9e4066Sahrens 
2134503ad85cSMatthew Ahrens 	error = dmu_objset_own(dsname, DMU_OST_ANY, B_TRUE, FTAG, &os);
2135fa9e4066Sahrens 	if (error) {
2136b24ab676SJeff Bonwick 		(void) printf("Could not open %s, error %d\n", dsname, error);
21371d452cf5Sahrens 		return (0);
2138fa9e4066Sahrens 	}
2139b5152584SMatthew Ahrens 	if (dmu_objset_ds(os)->ds_large_blocks)
2140b5152584SMatthew Ahrens 		num_large_blocks++;
2141fa9e4066Sahrens 	dump_dir(os);
2142503ad85cSMatthew Ahrens 	dmu_objset_disown(os, FTAG);
2143e0d35c44Smarks 	fuid_table_destroy();
21440a586ceaSMark Shellenbaum 	sa_loaded = B_FALSE;
21451d452cf5Sahrens 	return (0);
2146fa9e4066Sahrens }
2147fa9e4066Sahrens 
2148fa9e4066Sahrens /*
2149b24ab676SJeff Bonwick  * Block statistics.
2150fa9e4066Sahrens  */
2151b5152584SMatthew Ahrens #define	PSIZE_HISTO_SIZE (SPA_OLD_MAXBLOCKSIZE / SPA_MINBLOCKSIZE + 2)
2152fa9e4066Sahrens typedef struct zdb_blkstats {
2153490d05b9SMatthew Ahrens 	uint64_t zb_asize;
2154490d05b9SMatthew Ahrens 	uint64_t zb_lsize;
2155490d05b9SMatthew Ahrens 	uint64_t zb_psize;
2156490d05b9SMatthew Ahrens 	uint64_t zb_count;
2157d5ee8a13SMatthew Ahrens 	uint64_t zb_gangs;
2158d5ee8a13SMatthew Ahrens 	uint64_t zb_ditto_samevdev;
2159490d05b9SMatthew Ahrens 	uint64_t zb_psize_histogram[PSIZE_HISTO_SIZE];
2160fa9e4066Sahrens } zdb_blkstats_t;
2161fa9e4066Sahrens 
2162b24ab676SJeff Bonwick /*
2163b24ab676SJeff Bonwick  * Extended object types to report deferred frees and dedup auto-ditto blocks.
2164b24ab676SJeff Bonwick  */
2165b24ab676SJeff Bonwick #define	ZDB_OT_DEFERRED	(DMU_OT_NUMTYPES + 0)
2166b24ab676SJeff Bonwick #define	ZDB_OT_DITTO	(DMU_OT_NUMTYPES + 1)
2167ad135b5dSChristopher Siden #define	ZDB_OT_OTHER	(DMU_OT_NUMTYPES + 2)
2168ad135b5dSChristopher Siden #define	ZDB_OT_TOTAL	(DMU_OT_NUMTYPES + 3)
2169b24ab676SJeff Bonwick 
2170b24ab676SJeff Bonwick static char *zdb_ot_extname[] = {
2171b24ab676SJeff Bonwick 	"deferred free",
2172b24ab676SJeff Bonwick 	"dedup ditto",
2173ad135b5dSChristopher Siden 	"other",
2174b24ab676SJeff Bonwick 	"Total",
2175b24ab676SJeff Bonwick };
2176fa9e4066Sahrens 
217788b7b0f2SMatthew Ahrens #define	ZB_TOTAL	DN_MAX_LEVELS
2178fa9e4066Sahrens 
2179fa9e4066Sahrens typedef struct zdb_cb {
2180b24ab676SJeff Bonwick 	zdb_blkstats_t	zcb_type[ZB_TOTAL + 1][ZDB_OT_TOTAL + 1];
2181b24ab676SJeff Bonwick 	uint64_t	zcb_dedup_asize;
2182b24ab676SJeff Bonwick 	uint64_t	zcb_dedup_blocks;
21835d7b4d43SMatthew Ahrens 	uint64_t	zcb_embedded_blocks[NUM_BP_EMBEDDED_TYPES];
21845d7b4d43SMatthew Ahrens 	uint64_t	zcb_embedded_histogram[NUM_BP_EMBEDDED_TYPES]
21855d7b4d43SMatthew Ahrens 	    [BPE_PAYLOAD_SIZE];
2186490d05b9SMatthew Ahrens 	uint64_t	zcb_start;
2187490d05b9SMatthew Ahrens 	uint64_t	zcb_lastprint;
2188490d05b9SMatthew Ahrens 	uint64_t	zcb_totalasize;
2189fa9e4066Sahrens 	uint64_t	zcb_errors[256];
2190fa9e4066Sahrens 	int		zcb_readfails;
2191fa9e4066Sahrens 	int		zcb_haderrors;
2192cde58dbcSMatthew Ahrens 	spa_t		*zcb_spa;
2193fa9e4066Sahrens } zdb_cb_t;
2194fa9e4066Sahrens 
2195fa9e4066Sahrens static void
2196cde58dbcSMatthew Ahrens zdb_count_block(zdb_cb_t *zcb, zilog_t *zilog, const blkptr_t *bp,
2197b24ab676SJeff Bonwick     dmu_object_type_t type)
2198fa9e4066Sahrens {
2199b24ab676SJeff Bonwick 	uint64_t refcnt = 0;
2200b24ab676SJeff Bonwick 
2201b24ab676SJeff Bonwick 	ASSERT(type < ZDB_OT_TOTAL);
2202b24ab676SJeff Bonwick 
2203b24ab676SJeff Bonwick 	if (zilog && zil_bp_tree_add(zilog, bp) != 0)
2204b24ab676SJeff Bonwick 		return;
2205b24ab676SJeff Bonwick 
2206e14bb325SJeff Bonwick 	for (int i = 0; i < 4; i++) {
2207fa9e4066Sahrens 		int l = (i < 2) ? BP_GET_LEVEL(bp) : ZB_TOTAL;
2208b24ab676SJeff Bonwick 		int t = (i & 1) ? type : ZDB_OT_TOTAL;
2209d5ee8a13SMatthew Ahrens 		int equal;
2210fa9e4066Sahrens 		zdb_blkstats_t *zb = &zcb->zcb_type[l][t];
2211fa9e4066Sahrens 
2212fa9e4066Sahrens 		zb->zb_asize += BP_GET_ASIZE(bp);
2213fa9e4066Sahrens 		zb->zb_lsize += BP_GET_LSIZE(bp);
2214fa9e4066Sahrens 		zb->zb_psize += BP_GET_PSIZE(bp);
2215fa9e4066Sahrens 		zb->zb_count++;
2216b5152584SMatthew Ahrens 
2217b5152584SMatthew Ahrens 		/*
2218b5152584SMatthew Ahrens 		 * The histogram is only big enough to record blocks up to
2219b5152584SMatthew Ahrens 		 * SPA_OLD_MAXBLOCKSIZE; larger blocks go into the last,
2220b5152584SMatthew Ahrens 		 * "other", bucket.
2221b5152584SMatthew Ahrens 		 */
2222b5152584SMatthew Ahrens 		int idx = BP_GET_PSIZE(bp) >> SPA_MINBLOCKSHIFT;
2223b5152584SMatthew Ahrens 		idx = MIN(idx, SPA_OLD_MAXBLOCKSIZE / SPA_MINBLOCKSIZE + 1);
2224b5152584SMatthew Ahrens 		zb->zb_psize_histogram[idx]++;
2225d5ee8a13SMatthew Ahrens 
2226d5ee8a13SMatthew Ahrens 		zb->zb_gangs += BP_COUNT_GANG(bp);
2227d5ee8a13SMatthew Ahrens 
2228d5ee8a13SMatthew Ahrens 		switch (BP_GET_NDVAS(bp)) {
2229d5ee8a13SMatthew Ahrens 		case 2:
2230d5ee8a13SMatthew Ahrens 			if (DVA_GET_VDEV(&bp->blk_dva[0]) ==
2231d5ee8a13SMatthew Ahrens 			    DVA_GET_VDEV(&bp->blk_dva[1]))
2232d5ee8a13SMatthew Ahrens 				zb->zb_ditto_samevdev++;
2233d5ee8a13SMatthew Ahrens 			break;
2234d5ee8a13SMatthew Ahrens 		case 3:
2235d5ee8a13SMatthew Ahrens 			equal = (DVA_GET_VDEV(&bp->blk_dva[0]) ==
2236d5ee8a13SMatthew Ahrens 			    DVA_GET_VDEV(&bp->blk_dva[1])) +
2237d5ee8a13SMatthew Ahrens 			    (DVA_GET_VDEV(&bp->blk_dva[0]) ==
2238d5ee8a13SMatthew Ahrens 			    DVA_GET_VDEV(&bp->blk_dva[2])) +
2239d5ee8a13SMatthew Ahrens 			    (DVA_GET_VDEV(&bp->blk_dva[1]) ==
2240d5ee8a13SMatthew Ahrens 			    DVA_GET_VDEV(&bp->blk_dva[2]));
2241d5ee8a13SMatthew Ahrens 			if (equal != 0)
2242d5ee8a13SMatthew Ahrens 				zb->zb_ditto_samevdev++;
2243d5ee8a13SMatthew Ahrens 			break;
2244d5ee8a13SMatthew Ahrens 		}
2245d5ee8a13SMatthew Ahrens 
2246fa9e4066Sahrens 	}
2247fa9e4066Sahrens 
22485d7b4d43SMatthew Ahrens 	if (BP_IS_EMBEDDED(bp)) {
22495d7b4d43SMatthew Ahrens 		zcb->zcb_embedded_blocks[BPE_GET_ETYPE(bp)]++;
22505d7b4d43SMatthew Ahrens 		zcb->zcb_embedded_histogram[BPE_GET_ETYPE(bp)]
22515d7b4d43SMatthew Ahrens 		    [BPE_GET_PSIZE(bp)]++;
22525d7b4d43SMatthew Ahrens 		return;
22535d7b4d43SMatthew Ahrens 	}
22545d7b4d43SMatthew Ahrens 
2255b24ab676SJeff Bonwick 	if (dump_opt['L'])
2256b24ab676SJeff Bonwick 		return;
2257b24ab676SJeff Bonwick 
2258b24ab676SJeff Bonwick 	if (BP_GET_DEDUP(bp)) {
2259b24ab676SJeff Bonwick 		ddt_t *ddt;
2260b24ab676SJeff Bonwick 		ddt_entry_t *dde;
2261b24ab676SJeff Bonwick 
2262cde58dbcSMatthew Ahrens 		ddt = ddt_select(zcb->zcb_spa, bp);
2263b24ab676SJeff Bonwick 		ddt_enter(ddt);
2264b24ab676SJeff Bonwick 		dde = ddt_lookup(ddt, bp, B_FALSE);
2265b24ab676SJeff Bonwick 
2266b24ab676SJeff Bonwick 		if (dde == NULL) {
2267b24ab676SJeff Bonwick 			refcnt = 0;
2268b24ab676SJeff Bonwick 		} else {
2269b24ab676SJeff Bonwick 			ddt_phys_t *ddp = ddt_phys_select(dde, bp);
2270b24ab676SJeff Bonwick 			ddt_phys_decref(ddp);
2271b24ab676SJeff Bonwick 			refcnt = ddp->ddp_refcnt;
2272b24ab676SJeff Bonwick 			if (ddt_phys_total_refcnt(dde) == 0)
2273b24ab676SJeff Bonwick 				ddt_remove(ddt, dde);
2274d41e7643Sek 		}
2275b24ab676SJeff Bonwick 		ddt_exit(ddt);
2276d41e7643Sek 	}
2277d41e7643Sek 
2278cde58dbcSMatthew Ahrens 	VERIFY3U(zio_wait(zio_claim(NULL, zcb->zcb_spa,
2279cde58dbcSMatthew Ahrens 	    refcnt ? 0 : spa_first_txg(zcb->zcb_spa),
2280b24ab676SJeff Bonwick 	    bp, NULL, NULL, ZIO_FLAG_CANFAIL)), ==, 0);
2281fa9e4066Sahrens }
2282fa9e4066Sahrens 
228331d7e8faSGeorge Wilson static void
228431d7e8faSGeorge Wilson zdb_blkptr_done(zio_t *zio)
228531d7e8faSGeorge Wilson {
228631d7e8faSGeorge Wilson 	spa_t *spa = zio->io_spa;
228731d7e8faSGeorge Wilson 	blkptr_t *bp = zio->io_bp;
228831d7e8faSGeorge Wilson 	int ioerr = zio->io_error;
228931d7e8faSGeorge Wilson 	zdb_cb_t *zcb = zio->io_private;
22907802d7bfSMatthew Ahrens 	zbookmark_phys_t *zb = &zio->io_bookmark;
229131d7e8faSGeorge Wilson 
229231d7e8faSGeorge Wilson 	zio_data_buf_free(zio->io_data, zio->io_size);
229331d7e8faSGeorge Wilson 
229431d7e8faSGeorge Wilson 	mutex_enter(&spa->spa_scrub_lock);
229531d7e8faSGeorge Wilson 	spa->spa_scrub_inflight--;
229631d7e8faSGeorge Wilson 	cv_broadcast(&spa->spa_scrub_io_cv);
229731d7e8faSGeorge Wilson 
229831d7e8faSGeorge Wilson 	if (ioerr && !(zio->io_flags & ZIO_FLAG_SPECULATIVE)) {
229931d7e8faSGeorge Wilson 		char blkbuf[BP_SPRINTF_LEN];
230031d7e8faSGeorge Wilson 
230131d7e8faSGeorge Wilson 		zcb->zcb_haderrors = 1;
230231d7e8faSGeorge Wilson 		zcb->zcb_errors[ioerr]++;
230331d7e8faSGeorge Wilson 
230431d7e8faSGeorge Wilson 		if (dump_opt['b'] >= 2)
230543466aaeSMax Grossman 			snprintf_blkptr(blkbuf, sizeof (blkbuf), bp);
230631d7e8faSGeorge Wilson 		else
230731d7e8faSGeorge Wilson 			blkbuf[0] = '\0';
230831d7e8faSGeorge Wilson 
230931d7e8faSGeorge Wilson 		(void) printf("zdb_blkptr_cb: "
231031d7e8faSGeorge Wilson 		    "Got error %d reading "
231131d7e8faSGeorge Wilson 		    "<%llu, %llu, %lld, %llx> %s -- skipping\n",
231231d7e8faSGeorge Wilson 		    ioerr,
231331d7e8faSGeorge Wilson 		    (u_longlong_t)zb->zb_objset,
231431d7e8faSGeorge Wilson 		    (u_longlong_t)zb->zb_object,
231531d7e8faSGeorge Wilson 		    (u_longlong_t)zb->zb_level,
231631d7e8faSGeorge Wilson 		    (u_longlong_t)zb->zb_blkid,
231731d7e8faSGeorge Wilson 		    blkbuf);
231831d7e8faSGeorge Wilson 	}
231931d7e8faSGeorge Wilson 	mutex_exit(&spa->spa_scrub_lock);
232031d7e8faSGeorge Wilson }
232131d7e8faSGeorge Wilson 
2322fa9e4066Sahrens static int
23231b912ec7SGeorge Wilson zdb_blkptr_cb(spa_t *spa, zilog_t *zilog, const blkptr_t *bp,
23247802d7bfSMatthew Ahrens     const zbookmark_phys_t *zb, const dnode_phys_t *dnp, void *arg)
2325fa9e4066Sahrens {
2326fa9e4066Sahrens 	zdb_cb_t *zcb = arg;
23276365109dSVictor Latushkin 	dmu_object_type_t type;
2328468c413aSTim Haley 	boolean_t is_metadata;
2329fa9e4066Sahrens 
233043466aaeSMax Grossman 	if (dump_opt['b'] >= 5 && bp->blk_birth > 0) {
233143466aaeSMax Grossman 		char blkbuf[BP_SPRINTF_LEN];
233243466aaeSMax Grossman 		snprintf_blkptr(blkbuf, sizeof (blkbuf), bp);
233343466aaeSMax Grossman 		(void) printf("objset %llu object %llu "
233443466aaeSMax Grossman 		    "level %lld offset 0x%llx %s\n",
233543466aaeSMax Grossman 		    (u_longlong_t)zb->zb_objset,
233643466aaeSMax Grossman 		    (u_longlong_t)zb->zb_object,
233743466aaeSMax Grossman 		    (longlong_t)zb->zb_level,
233843466aaeSMax Grossman 		    (u_longlong_t)blkid2offset(dnp, bp, zb),
233943466aaeSMax Grossman 		    blkbuf);
234043466aaeSMax Grossman 	}
234143466aaeSMax Grossman 
234243466aaeSMax Grossman 	if (BP_IS_HOLE(bp))
234388b7b0f2SMatthew Ahrens 		return (0);
2344e14bb325SJeff Bonwick 
23456365109dSVictor Latushkin 	type = BP_GET_TYPE(bp);
2346e14bb325SJeff Bonwick 
2347ad135b5dSChristopher Siden 	zdb_count_block(zcb, zilog, bp,
2348ad135b5dSChristopher Siden 	    (type & DMU_OT_NEWTYPE) ? ZDB_OT_OTHER : type);
23496365109dSVictor Latushkin 
2350ad135b5dSChristopher Siden 	is_metadata = (BP_GET_LEVEL(bp) != 0 || DMU_OT_IS_METADATA(type));
23516365109dSVictor Latushkin 
23525d7b4d43SMatthew Ahrens 	if (!BP_IS_EMBEDDED(bp) &&
23535d7b4d43SMatthew Ahrens 	    (dump_opt['c'] > 1 || (dump_opt['c'] && is_metadata))) {
2354468c413aSTim Haley 		size_t size = BP_GET_PSIZE(bp);
235531d7e8faSGeorge Wilson 		void *data = zio_data_buf_alloc(size);
2356b24ab676SJeff Bonwick 		int flags = ZIO_FLAG_CANFAIL | ZIO_FLAG_SCRUB | ZIO_FLAG_RAW;
2357b24ab676SJeff Bonwick 
2358b24ab676SJeff Bonwick 		/* If it's an intent log block, failure is expected. */
2359b24ab676SJeff Bonwick 		if (zb->zb_level == ZB_ZIL_LEVEL)
2360b24ab676SJeff Bonwick 			flags |= ZIO_FLAG_SPECULATIVE;
2361b24ab676SJeff Bonwick 
236231d7e8faSGeorge Wilson 		mutex_enter(&spa->spa_scrub_lock);
236331d7e8faSGeorge Wilson 		while (spa->spa_scrub_inflight > max_inflight)
236431d7e8faSGeorge Wilson 			cv_wait(&spa->spa_scrub_io_cv, &spa->spa_scrub_lock);
236531d7e8faSGeorge Wilson 		spa->spa_scrub_inflight++;
236631d7e8faSGeorge Wilson 		mutex_exit(&spa->spa_scrub_lock);
2367b24ab676SJeff Bonwick 
236831d7e8faSGeorge Wilson 		zio_nowait(zio_read(NULL, spa, bp, data, size,
236931d7e8faSGeorge Wilson 		    zdb_blkptr_done, zcb, ZIO_PRIORITY_ASYNC_READ, flags, zb));
2370fa9e4066Sahrens 	}
2371fa9e4066Sahrens 
2372fa9e4066Sahrens 	zcb->zcb_readfails = 0;
2373fa9e4066Sahrens 
2374f7950bf1SMatthew Ahrens 	/* only call gethrtime() every 100 blocks */
2375f7950bf1SMatthew Ahrens 	static int iters;
2376f7950bf1SMatthew Ahrens 	if (++iters > 100)
2377f7950bf1SMatthew Ahrens 		iters = 0;
2378f7950bf1SMatthew Ahrens 	else
2379f7950bf1SMatthew Ahrens 		return (0);
2380f7950bf1SMatthew Ahrens 
2381f7950bf1SMatthew Ahrens 	if (dump_opt['b'] < 5 && gethrtime() > zcb->zcb_lastprint + NANOSEC) {
2382490d05b9SMatthew Ahrens 		uint64_t now = gethrtime();
2383490d05b9SMatthew Ahrens 		char buf[10];
2384490d05b9SMatthew Ahrens 		uint64_t bytes = zcb->zcb_type[ZB_TOTAL][ZDB_OT_TOTAL].zb_asize;
2385490d05b9SMatthew Ahrens 		int kb_per_sec =
2386490d05b9SMatthew Ahrens 		    1 + bytes / (1 + ((now - zcb->zcb_start) / 1000 / 1000));
2387490d05b9SMatthew Ahrens 		int sec_remaining =
2388490d05b9SMatthew Ahrens 		    (zcb->zcb_totalasize - bytes) / 1024 / kb_per_sec;
2389490d05b9SMatthew Ahrens 
2390490d05b9SMatthew Ahrens 		zfs_nicenum(bytes, buf, sizeof (buf));
2391490d05b9SMatthew Ahrens 		(void) fprintf(stderr,
2392490d05b9SMatthew Ahrens 		    "\r%5s completed (%4dMB/s) "
2393490d05b9SMatthew Ahrens 		    "estimated time remaining: %uhr %02umin %02usec        ",
2394490d05b9SMatthew Ahrens 		    buf, kb_per_sec / 1024,
2395490d05b9SMatthew Ahrens 		    sec_remaining / 60 / 60,
2396490d05b9SMatthew Ahrens 		    sec_remaining / 60 % 60,
2397490d05b9SMatthew Ahrens 		    sec_remaining % 60);
2398490d05b9SMatthew Ahrens 
2399490d05b9SMatthew Ahrens 		zcb->zcb_lastprint = now;
2400490d05b9SMatthew Ahrens 	}
2401490d05b9SMatthew Ahrens 
2402fa9e4066Sahrens 	return (0);
2403fa9e4066Sahrens }
2404fa9e4066Sahrens 
2405b24ab676SJeff Bonwick static void
24060713e232SGeorge Wilson zdb_leak(void *arg, uint64_t start, uint64_t size)
2407b24ab676SJeff Bonwick {
24080713e232SGeorge Wilson 	vdev_t *vd = arg;
2409b24ab676SJeff Bonwick 
2410b24ab676SJeff Bonwick 	(void) printf("leaked space: vdev %llu, offset 0x%llx, size %llu\n",
2411b24ab676SJeff Bonwick 	    (u_longlong_t)vd->vdev_id, (u_longlong_t)start, (u_longlong_t)size);
2412b24ab676SJeff Bonwick }
2413b24ab676SJeff Bonwick 
24140713e232SGeorge Wilson static metaslab_ops_t zdb_metaslab_ops = {
24152e4c9986SGeorge Wilson 	NULL	/* alloc */
2416b24ab676SJeff Bonwick };
2417b24ab676SJeff Bonwick 
2418b24ab676SJeff Bonwick static void
2419bbfd46c4SJeff Bonwick zdb_ddt_leak_init(spa_t *spa, zdb_cb_t *zcb)
2420b24ab676SJeff Bonwick {
2421bbfd46c4SJeff Bonwick 	ddt_bookmark_t ddb = { 0 };
2422b24ab676SJeff Bonwick 	ddt_entry_t dde;
2423b24ab676SJeff Bonwick 	int error;
2424b24ab676SJeff Bonwick 
2425bbfd46c4SJeff Bonwick 	while ((error = ddt_walk(spa, &ddb, &dde)) == 0) {
2426b24ab676SJeff Bonwick 		blkptr_t blk;
2427b24ab676SJeff Bonwick 		ddt_phys_t *ddp = dde.dde_phys;
2428bbfd46c4SJeff Bonwick 
2429bbfd46c4SJeff Bonwick 		if (ddb.ddb_class == DDT_CLASS_UNIQUE)
2430bbfd46c4SJeff Bonwick 			return;
2431bbfd46c4SJeff Bonwick 
2432b24ab676SJeff Bonwick 		ASSERT(ddt_phys_total_refcnt(&dde) > 1);
2433bbfd46c4SJeff Bonwick 
2434b24ab676SJeff Bonwick 		for (int p = 0; p < DDT_PHYS_TYPES; p++, ddp++) {
2435b24ab676SJeff Bonwick 			if (ddp->ddp_phys_birth == 0)
2436b24ab676SJeff Bonwick 				continue;
2437bbfd46c4SJeff Bonwick 			ddt_bp_create(ddb.ddb_checksum,
2438bbfd46c4SJeff Bonwick 			    &dde.dde_key, ddp, &blk);
2439b24ab676SJeff Bonwick 			if (p == DDT_PHYS_DITTO) {
2440cde58dbcSMatthew Ahrens 				zdb_count_block(zcb, NULL, &blk, ZDB_OT_DITTO);
2441b24ab676SJeff Bonwick 			} else {
2442b24ab676SJeff Bonwick 				zcb->zcb_dedup_asize +=
2443b24ab676SJeff Bonwick 				    BP_GET_ASIZE(&blk) * (ddp->ddp_refcnt - 1);
2444b24ab676SJeff Bonwick 				zcb->zcb_dedup_blocks++;
2445b24ab676SJeff Bonwick 			}
2446b24ab676SJeff Bonwick 		}
2447b24ab676SJeff Bonwick 		if (!dump_opt['L']) {
2448bbfd46c4SJeff Bonwick 			ddt_t *ddt = spa->spa_ddt[ddb.ddb_checksum];
2449b24ab676SJeff Bonwick 			ddt_enter(ddt);
2450b24ab676SJeff Bonwick 			VERIFY(ddt_lookup(ddt, &blk, B_TRUE) != NULL);
2451b24ab676SJeff Bonwick 			ddt_exit(ddt);
2452b24ab676SJeff Bonwick 		}
2453b24ab676SJeff Bonwick 	}
2454b24ab676SJeff Bonwick 
2455b24ab676SJeff Bonwick 	ASSERT(error == ENOENT);
2456b24ab676SJeff Bonwick }
2457b24ab676SJeff Bonwick 
2458b24ab676SJeff Bonwick static void
2459b24ab676SJeff Bonwick zdb_leak_init(spa_t *spa, zdb_cb_t *zcb)
2460b24ab676SJeff Bonwick {
2461cde58dbcSMatthew Ahrens 	zcb->zcb_spa = spa;
2462cde58dbcSMatthew Ahrens 
2463b24ab676SJeff Bonwick 	if (!dump_opt['L']) {
2464b24ab676SJeff Bonwick 		vdev_t *rvd = spa->spa_root_vdev;
246506be9802SMatthew Ahrens 		for (uint64_t c = 0; c < rvd->vdev_children; c++) {
2466b24ab676SJeff Bonwick 			vdev_t *vd = rvd->vdev_child[c];
246706be9802SMatthew Ahrens 			for (uint64_t m = 0; m < vd->vdev_ms_count; m++) {
2468b24ab676SJeff Bonwick 				metaslab_t *msp = vd->vdev_ms[m];
2469b24ab676SJeff Bonwick 				mutex_enter(&msp->ms_lock);
24700713e232SGeorge Wilson 				metaslab_unload(msp);
24710713e232SGeorge Wilson 
24720713e232SGeorge Wilson 				/*
24730713e232SGeorge Wilson 				 * For leak detection, we overload the metaslab
24740713e232SGeorge Wilson 				 * ms_tree to contain allocated segments
24750713e232SGeorge Wilson 				 * instead of free segments. As a result,
24760713e232SGeorge Wilson 				 * we can't use the normal metaslab_load/unload
24770713e232SGeorge Wilson 				 * interfaces.
24780713e232SGeorge Wilson 				 */
24790713e232SGeorge Wilson 				if (msp->ms_sm != NULL) {
248006be9802SMatthew Ahrens 					(void) fprintf(stderr,
248106be9802SMatthew Ahrens 					    "\rloading space map for "
248206be9802SMatthew Ahrens 					    "vdev %llu of %llu, "
248306be9802SMatthew Ahrens 					    "metaslab %llu of %llu ...",
248406be9802SMatthew Ahrens 					    (longlong_t)c,
248506be9802SMatthew Ahrens 					    (longlong_t)rvd->vdev_children,
248606be9802SMatthew Ahrens 					    (longlong_t)m,
248706be9802SMatthew Ahrens 					    (longlong_t)vd->vdev_ms_count);
248806be9802SMatthew Ahrens 
24890713e232SGeorge Wilson 					msp->ms_ops = &zdb_metaslab_ops;
2490f7950bf1SMatthew Ahrens 
2491f7950bf1SMatthew Ahrens 					/*
2492f7950bf1SMatthew Ahrens 					 * We don't want to spend the CPU
2493f7950bf1SMatthew Ahrens 					 * manipulating the size-ordered
2494f7950bf1SMatthew Ahrens 					 * tree, so clear the range_tree
2495f7950bf1SMatthew Ahrens 					 * ops.
2496f7950bf1SMatthew Ahrens 					 */
2497f7950bf1SMatthew Ahrens 					msp->ms_tree->rt_ops = NULL;
24980713e232SGeorge Wilson 					VERIFY0(space_map_load(msp->ms_sm,
24990713e232SGeorge Wilson 					    msp->ms_tree, SM_ALLOC));
25000713e232SGeorge Wilson 					msp->ms_loaded = B_TRUE;
25010713e232SGeorge Wilson 				}
2502b24ab676SJeff Bonwick 				mutex_exit(&msp->ms_lock);
2503b24ab676SJeff Bonwick 			}
2504b24ab676SJeff Bonwick 		}
250506be9802SMatthew Ahrens 		(void) fprintf(stderr, "\n");
2506b24ab676SJeff Bonwick 	}
2507b24ab676SJeff Bonwick 
2508b24ab676SJeff Bonwick 	spa_config_enter(spa, SCL_CONFIG, FTAG, RW_READER);
2509b24ab676SJeff Bonwick 
2510bbfd46c4SJeff Bonwick 	zdb_ddt_leak_init(spa, zcb);
2511b24ab676SJeff Bonwick 
2512b24ab676SJeff Bonwick 	spa_config_exit(spa, SCL_CONFIG, FTAG);
2513b24ab676SJeff Bonwick }
2514b24ab676SJeff Bonwick 
2515b24ab676SJeff Bonwick static void
2516b24ab676SJeff Bonwick zdb_leak_fini(spa_t *spa)
2517b24ab676SJeff Bonwick {
2518b24ab676SJeff Bonwick 	if (!dump_opt['L']) {
2519b24ab676SJeff Bonwick 		vdev_t *rvd = spa->spa_root_vdev;
2520b24ab676SJeff Bonwick 		for (int c = 0; c < rvd->vdev_children; c++) {
2521b24ab676SJeff Bonwick 			vdev_t *vd = rvd->vdev_child[c];
2522b24ab676SJeff Bonwick 			for (int m = 0; m < vd->vdev_ms_count; m++) {
2523b24ab676SJeff Bonwick 				metaslab_t *msp = vd->vdev_ms[m];
2524b24ab676SJeff Bonwick 				mutex_enter(&msp->ms_lock);
25250713e232SGeorge Wilson 
25260713e232SGeorge Wilson 				/*
25270713e232SGeorge Wilson 				 * The ms_tree has been overloaded to
25280713e232SGeorge Wilson 				 * contain allocated segments. Now that we
25290713e232SGeorge Wilson 				 * finished traversing all blocks, any
25300713e232SGeorge Wilson 				 * block that remains in the ms_tree
25310713e232SGeorge Wilson 				 * represents an allocated block that we
25320713e232SGeorge Wilson 				 * did not claim during the traversal.
25330713e232SGeorge Wilson 				 * Claimed blocks would have been removed
25340713e232SGeorge Wilson 				 * from the ms_tree.
25350713e232SGeorge Wilson 				 */
25360713e232SGeorge Wilson 				range_tree_vacate(msp->ms_tree, zdb_leak, vd);
25370713e232SGeorge Wilson 				msp->ms_loaded = B_FALSE;
25380713e232SGeorge Wilson 
2539b24ab676SJeff Bonwick 				mutex_exit(&msp->ms_lock);
2540b24ab676SJeff Bonwick 			}
2541b24ab676SJeff Bonwick 		}
2542b24ab676SJeff Bonwick 	}
2543b24ab676SJeff Bonwick }
2544b24ab676SJeff Bonwick 
2545cde58dbcSMatthew Ahrens /* ARGSUSED */
2546cde58dbcSMatthew Ahrens static int
2547cde58dbcSMatthew Ahrens count_block_cb(void *arg, const blkptr_t *bp, dmu_tx_t *tx)
2548cde58dbcSMatthew Ahrens {
2549cde58dbcSMatthew Ahrens 	zdb_cb_t *zcb = arg;
2550cde58dbcSMatthew Ahrens 
2551490d05b9SMatthew Ahrens 	if (dump_opt['b'] >= 5) {
2552cde58dbcSMatthew Ahrens 		char blkbuf[BP_SPRINTF_LEN];
255343466aaeSMax Grossman 		snprintf_blkptr(blkbuf, sizeof (blkbuf), bp);
2554cde58dbcSMatthew Ahrens 		(void) printf("[%s] %s\n",
2555cde58dbcSMatthew Ahrens 		    "deferred free", blkbuf);
2556cde58dbcSMatthew Ahrens 	}
2557cde58dbcSMatthew Ahrens 	zdb_count_block(zcb, NULL, bp, ZDB_OT_DEFERRED);
2558cde58dbcSMatthew Ahrens 	return (0);
2559cde58dbcSMatthew Ahrens }
2560cde58dbcSMatthew Ahrens 
2561fa9e4066Sahrens static int
2562fa9e4066Sahrens dump_block_stats(spa_t *spa)
2563fa9e4066Sahrens {
2564fa9e4066Sahrens 	zdb_cb_t zcb = { 0 };
2565fa9e4066Sahrens 	zdb_blkstats_t *zb, *tzb;
2566b24ab676SJeff Bonwick 	uint64_t norm_alloc, norm_space, total_alloc, total_found;
2567cd088ea4SVictor Latushkin 	int flags = TRAVERSE_PRE | TRAVERSE_PREFETCH_METADATA | TRAVERSE_HARD;
25685d7b4d43SMatthew Ahrens 	boolean_t leaks = B_FALSE;
2569fa9e4066Sahrens 
2570490d05b9SMatthew Ahrens 	(void) printf("\nTraversing all blocks %s%s%s%s%s...\n\n",
2571b24ab676SJeff Bonwick 	    (dump_opt['c'] || !dump_opt['L']) ? "to verify " : "",
2572b24ab676SJeff Bonwick 	    (dump_opt['c'] == 1) ? "metadata " : "",
2573b24ab676SJeff Bonwick 	    dump_opt['c'] ? "checksums " : "",
2574b24ab676SJeff Bonwick 	    (dump_opt['c'] && !dump_opt['L']) ? "and verify " : "",
2575b24ab676SJeff Bonwick 	    !dump_opt['L'] ? "nothing leaked " : "");
2576fa9e4066Sahrens 
2577fa9e4066Sahrens 	/*
2578e14bb325SJeff Bonwick 	 * Load all space maps as SM_ALLOC maps, then traverse the pool
2579e14bb325SJeff Bonwick 	 * claiming each block we discover.  If the pool is perfectly
2580e14bb325SJeff Bonwick 	 * consistent, the space maps will be empty when we're done.
2581e14bb325SJeff Bonwick 	 * Anything left over is a leak; any block we can't claim (because
2582e14bb325SJeff Bonwick 	 * it's not part of any space map) is a double allocation,
2583e14bb325SJeff Bonwick 	 * reference to a freed block, or an unclaimed log block.
2584fa9e4066Sahrens 	 */
2585b24ab676SJeff Bonwick 	zdb_leak_init(spa, &zcb);
2586fa9e4066Sahrens 
2587fa9e4066Sahrens 	/*
2588fa9e4066Sahrens 	 * If there's a deferred-free bplist, process that first.
2589fa9e4066Sahrens 	 */
2590cde58dbcSMatthew Ahrens 	(void) bpobj_iterate_nofree(&spa->spa_deferred_bpobj,
2591cde58dbcSMatthew Ahrens 	    count_block_cb, &zcb, NULL);
25923b2aab18SMatthew Ahrens 	if (spa_version(spa) >= SPA_VERSION_DEADLISTS) {
25933b2aab18SMatthew Ahrens 		(void) bpobj_iterate_nofree(&spa->spa_dsl_pool->dp_free_bpobj,
25943b2aab18SMatthew Ahrens 		    count_block_cb, &zcb, NULL);
25953b2aab18SMatthew Ahrens 	}
25962acef22dSMatthew Ahrens 	if (spa_feature_is_active(spa, SPA_FEATURE_ASYNC_DESTROY)) {
2597b420f3adSRichard Lowe 		VERIFY3U(0, ==, bptree_iterate(spa->spa_meta_objset,
2598ad135b5dSChristopher Siden 		    spa->spa_dsl_pool->dp_bptree_obj, B_FALSE, count_block_cb,
2599ad135b5dSChristopher Siden 		    &zcb, NULL));
2600ad135b5dSChristopher Siden 	}
2601fa9e4066Sahrens 
2602bbfd46c4SJeff Bonwick 	if (dump_opt['c'] > 1)
2603bbfd46c4SJeff Bonwick 		flags |= TRAVERSE_PREFETCH_DATA;
2604bbfd46c4SJeff Bonwick 
2605490d05b9SMatthew Ahrens 	zcb.zcb_totalasize = metaslab_class_get_alloc(spa_normal_class(spa));
2606490d05b9SMatthew Ahrens 	zcb.zcb_start = zcb.zcb_lastprint = gethrtime();
2607bbfd46c4SJeff Bonwick 	zcb.zcb_haderrors |= traverse_pool(spa, 0, flags, zdb_blkptr_cb, &zcb);
2608fa9e4066Sahrens 
260931d7e8faSGeorge Wilson 	/*
261031d7e8faSGeorge Wilson 	 * If we've traversed the data blocks then we need to wait for those
261131d7e8faSGeorge Wilson 	 * I/Os to complete. We leverage "The Godfather" zio to wait on
261231d7e8faSGeorge Wilson 	 * all async I/Os to complete.
261331d7e8faSGeorge Wilson 	 */
261431d7e8faSGeorge Wilson 	if (dump_opt['c']) {
26156f834bc1SMatthew Ahrens 		for (int i = 0; i < max_ncpus; i++) {
26166f834bc1SMatthew Ahrens 			(void) zio_wait(spa->spa_async_zio_root[i]);
26176f834bc1SMatthew Ahrens 			spa->spa_async_zio_root[i] = zio_root(spa, NULL, NULL,
26186f834bc1SMatthew Ahrens 			    ZIO_FLAG_CANFAIL | ZIO_FLAG_SPECULATIVE |
26196f834bc1SMatthew Ahrens 			    ZIO_FLAG_GODFATHER);
26206f834bc1SMatthew Ahrens 		}
262131d7e8faSGeorge Wilson 	}
262231d7e8faSGeorge Wilson 
2623b24ab676SJeff Bonwick 	if (zcb.zcb_haderrors) {
2624fa9e4066Sahrens 		(void) printf("\nError counts:\n\n");
2625fa9e4066Sahrens 		(void) printf("\t%5s  %s\n", "errno", "count");
2626b24ab676SJeff Bonwick 		for (int e = 0; e < 256; e++) {
2627fa9e4066Sahrens 			if (zcb.zcb_errors[e] != 0) {
2628fa9e4066Sahrens 				(void) printf("\t%5d  %llu\n",
2629fa9e4066Sahrens 				    e, (u_longlong_t)zcb.zcb_errors[e]);
2630fa9e4066Sahrens 			}
2631fa9e4066Sahrens 		}
2632fa9e4066Sahrens 	}
2633fa9e4066Sahrens 
2634fa9e4066Sahrens 	/*
2635fa9e4066Sahrens 	 * Report any leaked segments.
2636fa9e4066Sahrens 	 */
2637b24ab676SJeff Bonwick 	zdb_leak_fini(spa);
2638fa9e4066Sahrens 
2639b24ab676SJeff Bonwick 	tzb = &zcb.zcb_type[ZB_TOTAL][ZDB_OT_TOTAL];
26408654d025Sperrin 
2641b24ab676SJeff Bonwick 	norm_alloc = metaslab_class_get_alloc(spa_normal_class(spa));
2642b24ab676SJeff Bonwick 	norm_space = metaslab_class_get_space(spa_normal_class(spa));
26438654d025Sperrin 
2644b24ab676SJeff Bonwick 	total_alloc = norm_alloc + metaslab_class_get_alloc(spa_log_class(spa));
2645b24ab676SJeff Bonwick 	total_found = tzb->zb_asize - zcb.zcb_dedup_asize;
2646fa9e4066Sahrens 
2647b24ab676SJeff Bonwick 	if (total_found == total_alloc) {
264882a0a985SVictor Latushkin 		if (!dump_opt['L'])
264982a0a985SVictor Latushkin 			(void) printf("\n\tNo leaks (block sum matches space"
265082a0a985SVictor Latushkin 			    " maps exactly)\n");
2651fa9e4066Sahrens 	} else {
2652fa9e4066Sahrens 		(void) printf("block traversal size %llu != alloc %llu "
265382a0a985SVictor Latushkin 		    "(%s %lld)\n",
2654b24ab676SJeff Bonwick 		    (u_longlong_t)total_found,
2655b24ab676SJeff Bonwick 		    (u_longlong_t)total_alloc,
265682a0a985SVictor Latushkin 		    (dump_opt['L']) ? "unreachable" : "leaked",
2657b24ab676SJeff Bonwick 		    (longlong_t)(total_alloc - total_found));
26585d7b4d43SMatthew Ahrens 		leaks = B_TRUE;
2659fa9e4066Sahrens 	}
2660fa9e4066Sahrens 
2661fa9e4066Sahrens 	if (tzb->zb_count == 0)
2662fa9e4066Sahrens 		return (2);
2663fa9e4066Sahrens 
2664fa9e4066Sahrens 	(void) printf("\n");
2665fa9e4066Sahrens 	(void) printf("\tbp count:      %10llu\n",
2666fa9e4066Sahrens 	    (u_longlong_t)tzb->zb_count);
2667d5ee8a13SMatthew Ahrens 	(void) printf("\tganged count:  %10llu\n",
2668d5ee8a13SMatthew Ahrens 	    (longlong_t)tzb->zb_gangs);
2669b24ab676SJeff Bonwick 	(void) printf("\tbp logical:    %10llu      avg: %6llu\n",
2670fa9e4066Sahrens 	    (u_longlong_t)tzb->zb_lsize,
2671fa9e4066Sahrens 	    (u_longlong_t)(tzb->zb_lsize / tzb->zb_count));
2672b24ab676SJeff Bonwick 	(void) printf("\tbp physical:   %10llu      avg:"
2673b24ab676SJeff Bonwick 	    " %6llu     compression: %6.2f\n",
2674fa9e4066Sahrens 	    (u_longlong_t)tzb->zb_psize,
2675fa9e4066Sahrens 	    (u_longlong_t)(tzb->zb_psize / tzb->zb_count),
2676fa9e4066Sahrens 	    (double)tzb->zb_lsize / tzb->zb_psize);
2677b24ab676SJeff Bonwick 	(void) printf("\tbp allocated:  %10llu      avg:"
2678b24ab676SJeff Bonwick 	    " %6llu     compression: %6.2f\n",
2679fa9e4066Sahrens 	    (u_longlong_t)tzb->zb_asize,
2680fa9e4066Sahrens 	    (u_longlong_t)(tzb->zb_asize / tzb->zb_count),
2681fa9e4066Sahrens 	    (double)tzb->zb_lsize / tzb->zb_asize);
2682b24ab676SJeff Bonwick 	(void) printf("\tbp deduped:    %10llu    ref>1:"
2683b24ab676SJeff Bonwick 	    " %6llu   deduplication: %6.2f\n",
2684b24ab676SJeff Bonwick 	    (u_longlong_t)zcb.zcb_dedup_asize,
2685b24ab676SJeff Bonwick 	    (u_longlong_t)zcb.zcb_dedup_blocks,
2686b24ab676SJeff Bonwick 	    (double)zcb.zcb_dedup_asize / tzb->zb_asize + 1.0);
2687b24ab676SJeff Bonwick 	(void) printf("\tSPA allocated: %10llu     used: %5.2f%%\n",
2688b24ab676SJeff Bonwick 	    (u_longlong_t)norm_alloc, 100.0 * norm_alloc / norm_space);
2689fa9e4066Sahrens 
26905d7b4d43SMatthew Ahrens 	for (bp_embedded_type_t i = 0; i < NUM_BP_EMBEDDED_TYPES; i++) {
26915d7b4d43SMatthew Ahrens 		if (zcb.zcb_embedded_blocks[i] == 0)
26925d7b4d43SMatthew Ahrens 			continue;
26935d7b4d43SMatthew Ahrens 		(void) printf("\n");
26945d7b4d43SMatthew Ahrens 		(void) printf("\tadditional, non-pointer bps of type %u: "
26955d7b4d43SMatthew Ahrens 		    "%10llu\n",
26965d7b4d43SMatthew Ahrens 		    i, (u_longlong_t)zcb.zcb_embedded_blocks[i]);
26975d7b4d43SMatthew Ahrens 
26985d7b4d43SMatthew Ahrens 		if (dump_opt['b'] >= 3) {
26995d7b4d43SMatthew Ahrens 			(void) printf("\t number of (compressed) bytes:  "
27005d7b4d43SMatthew Ahrens 			    "number of bps\n");
27015d7b4d43SMatthew Ahrens 			dump_histogram(zcb.zcb_embedded_histogram[i],
27025d7b4d43SMatthew Ahrens 			    sizeof (zcb.zcb_embedded_histogram[i]) /
27035d7b4d43SMatthew Ahrens 			    sizeof (zcb.zcb_embedded_histogram[i][0]), 0);
27045d7b4d43SMatthew Ahrens 		}
27055d7b4d43SMatthew Ahrens 	}
27065d7b4d43SMatthew Ahrens 
2707d5ee8a13SMatthew Ahrens 	if (tzb->zb_ditto_samevdev != 0) {
2708d5ee8a13SMatthew Ahrens 		(void) printf("\tDittoed blocks on same vdev: %llu\n",
2709d5ee8a13SMatthew Ahrens 		    (longlong_t)tzb->zb_ditto_samevdev);
2710d5ee8a13SMatthew Ahrens 	}
2711d5ee8a13SMatthew Ahrens 
2712fa9e4066Sahrens 	if (dump_opt['b'] >= 2) {
2713fa9e4066Sahrens 		int l, t, level;
2714fa9e4066Sahrens 		(void) printf("\nBlocks\tLSIZE\tPSIZE\tASIZE"
2715fa9e4066Sahrens 		    "\t  avg\t comp\t%%Total\tType\n");
2716fa9e4066Sahrens 
2717b24ab676SJeff Bonwick 		for (t = 0; t <= ZDB_OT_TOTAL; t++) {
27183f9d6ad7SLin Ling 			char csize[32], lsize[32], psize[32], asize[32];
2719d5ee8a13SMatthew Ahrens 			char avg[32], gang[32];
2720fa9e4066Sahrens 			char *typename;
2721fa9e4066Sahrens 
2722b24ab676SJeff Bonwick 			if (t < DMU_OT_NUMTYPES)
2723b24ab676SJeff Bonwick 				typename = dmu_ot[t].ot_name;
2724b24ab676SJeff Bonwick 			else
2725b24ab676SJeff Bonwick 				typename = zdb_ot_extname[t - DMU_OT_NUMTYPES];
2726fa9e4066Sahrens 
2727fa9e4066Sahrens 			if (zcb.zcb_type[ZB_TOTAL][t].zb_asize == 0) {
2728fa9e4066Sahrens 				(void) printf("%6s\t%5s\t%5s\t%5s"
2729fa9e4066Sahrens 				    "\t%5s\t%5s\t%6s\t%s\n",
2730fa9e4066Sahrens 				    "-",
2731fa9e4066Sahrens 				    "-",
2732fa9e4066Sahrens 				    "-",
2733fa9e4066Sahrens 				    "-",
2734fa9e4066Sahrens 				    "-",
2735fa9e4066Sahrens 				    "-",
2736fa9e4066Sahrens 				    "-",
2737fa9e4066Sahrens 				    typename);
2738fa9e4066Sahrens 				continue;
2739fa9e4066Sahrens 			}
2740fa9e4066Sahrens 
2741fa9e4066Sahrens 			for (l = ZB_TOTAL - 1; l >= -1; l--) {
2742fa9e4066Sahrens 				level = (l == -1 ? ZB_TOTAL : l);
2743fa9e4066Sahrens 				zb = &zcb.zcb_type[level][t];
2744fa9e4066Sahrens 
2745fa9e4066Sahrens 				if (zb->zb_asize == 0)
2746fa9e4066Sahrens 					continue;
2747fa9e4066Sahrens 
2748fa9e4066Sahrens 				if (dump_opt['b'] < 3 && level != ZB_TOTAL)
2749fa9e4066Sahrens 					continue;
2750fa9e4066Sahrens 
2751fa9e4066Sahrens 				if (level == 0 && zb->zb_asize ==
2752fa9e4066Sahrens 				    zcb.zcb_type[ZB_TOTAL][t].zb_asize)
2753fa9e4066Sahrens 					continue;
2754fa9e4066Sahrens 
27553f9d6ad7SLin Ling 				zdb_nicenum(zb->zb_count, csize);
27563f9d6ad7SLin Ling 				zdb_nicenum(zb->zb_lsize, lsize);
27573f9d6ad7SLin Ling 				zdb_nicenum(zb->zb_psize, psize);
27583f9d6ad7SLin Ling 				zdb_nicenum(zb->zb_asize, asize);
27593f9d6ad7SLin Ling 				zdb_nicenum(zb->zb_asize / zb->zb_count, avg);
2760d5ee8a13SMatthew Ahrens 				zdb_nicenum(zb->zb_gangs, gang);
2761fa9e4066Sahrens 
2762fa9e4066Sahrens 				(void) printf("%6s\t%5s\t%5s\t%5s\t%5s"
2763fa9e4066Sahrens 				    "\t%5.2f\t%6.2f\t",
2764fa9e4066Sahrens 				    csize, lsize, psize, asize, avg,
2765fa9e4066Sahrens 				    (double)zb->zb_lsize / zb->zb_psize,
2766fa9e4066Sahrens 				    100.0 * zb->zb_asize / tzb->zb_asize);
2767fa9e4066Sahrens 
2768fa9e4066Sahrens 				if (level == ZB_TOTAL)
2769fa9e4066Sahrens 					(void) printf("%s\n", typename);
2770fa9e4066Sahrens 				else
2771fa9e4066Sahrens 					(void) printf("    L%d %s\n",
2772fa9e4066Sahrens 					    level, typename);
2773490d05b9SMatthew Ahrens 
2774d5ee8a13SMatthew Ahrens 				if (dump_opt['b'] >= 3 && zb->zb_gangs > 0) {
2775d5ee8a13SMatthew Ahrens 					(void) printf("\t number of ganged "
2776d5ee8a13SMatthew Ahrens 					    "blocks: %s\n", gang);
2777d5ee8a13SMatthew Ahrens 				}
2778d5ee8a13SMatthew Ahrens 
2779490d05b9SMatthew Ahrens 				if (dump_opt['b'] >= 4) {
2780490d05b9SMatthew Ahrens 					(void) printf("psize "
2781490d05b9SMatthew Ahrens 					    "(in 512-byte sectors): "
2782490d05b9SMatthew Ahrens 					    "number of blocks\n");
2783490d05b9SMatthew Ahrens 					dump_histogram(zb->zb_psize_histogram,
27840713e232SGeorge Wilson 					    PSIZE_HISTO_SIZE, 0);
2785490d05b9SMatthew Ahrens 				}
2786fa9e4066Sahrens 			}
2787fa9e4066Sahrens 		}
2788fa9e4066Sahrens 	}
2789fa9e4066Sahrens 
2790fa9e4066Sahrens 	(void) printf("\n");
2791fa9e4066Sahrens 
2792fa9e4066Sahrens 	if (leaks)
2793fa9e4066Sahrens 		return (2);
2794fa9e4066Sahrens 
2795fa9e4066Sahrens 	if (zcb.zcb_haderrors)
2796fa9e4066Sahrens 		return (3);
2797fa9e4066Sahrens 
2798fa9e4066Sahrens 	return (0);
2799fa9e4066Sahrens }
2800fa9e4066Sahrens 
2801b24ab676SJeff Bonwick typedef struct zdb_ddt_entry {
2802b24ab676SJeff Bonwick 	ddt_key_t	zdde_key;
2803b24ab676SJeff Bonwick 	uint64_t	zdde_ref_blocks;
2804b24ab676SJeff Bonwick 	uint64_t	zdde_ref_lsize;
2805b24ab676SJeff Bonwick 	uint64_t	zdde_ref_psize;
2806b24ab676SJeff Bonwick 	uint64_t	zdde_ref_dsize;
2807b24ab676SJeff Bonwick 	avl_node_t	zdde_node;
2808b24ab676SJeff Bonwick } zdb_ddt_entry_t;
2809b24ab676SJeff Bonwick 
2810b24ab676SJeff Bonwick /* ARGSUSED */
2811b24ab676SJeff Bonwick static int
2812b24ab676SJeff Bonwick zdb_ddt_add_cb(spa_t *spa, zilog_t *zilog, const blkptr_t *bp,
28137802d7bfSMatthew Ahrens     const zbookmark_phys_t *zb, const dnode_phys_t *dnp, void *arg)
2814b24ab676SJeff Bonwick {
2815b24ab676SJeff Bonwick 	avl_tree_t *t = arg;
2816b24ab676SJeff Bonwick 	avl_index_t where;
2817b24ab676SJeff Bonwick 	zdb_ddt_entry_t *zdde, zdde_search;
2818b24ab676SJeff Bonwick 
28195d7b4d43SMatthew Ahrens 	if (BP_IS_HOLE(bp) || BP_IS_EMBEDDED(bp))
2820b24ab676SJeff Bonwick 		return (0);
2821b24ab676SJeff Bonwick 
2822b24ab676SJeff Bonwick 	if (dump_opt['S'] > 1 && zb->zb_level == ZB_ROOT_LEVEL) {
2823b24ab676SJeff Bonwick 		(void) printf("traversing objset %llu, %llu objects, "
2824b24ab676SJeff Bonwick 		    "%lu blocks so far\n",
2825b24ab676SJeff Bonwick 		    (u_longlong_t)zb->zb_objset,
28265d7b4d43SMatthew Ahrens 		    (u_longlong_t)BP_GET_FILL(bp),
2827b24ab676SJeff Bonwick 		    avl_numnodes(t));
2828b24ab676SJeff Bonwick 	}
2829b24ab676SJeff Bonwick 
2830bbfd46c4SJeff Bonwick 	if (BP_IS_HOLE(bp) || BP_GET_CHECKSUM(bp) == ZIO_CHECKSUM_OFF ||
2831ad135b5dSChristopher Siden 	    BP_GET_LEVEL(bp) > 0 || DMU_OT_IS_METADATA(BP_GET_TYPE(bp)))
2832b24ab676SJeff Bonwick 		return (0);
2833b24ab676SJeff Bonwick 
2834b24ab676SJeff Bonwick 	ddt_key_fill(&zdde_search.zdde_key, bp);
2835b24ab676SJeff Bonwick 
2836b24ab676SJeff Bonwick 	zdde = avl_find(t, &zdde_search, &where);
2837b24ab676SJeff Bonwick 
2838b24ab676SJeff Bonwick 	if (zdde == NULL) {
2839b24ab676SJeff Bonwick 		zdde = umem_zalloc(sizeof (*zdde), UMEM_NOFAIL);
2840b24ab676SJeff Bonwick 		zdde->zdde_key = zdde_search.zdde_key;
2841b24ab676SJeff Bonwick 		avl_insert(t, zdde, where);
2842b24ab676SJeff Bonwick 	}
2843b24ab676SJeff Bonwick 
2844b24ab676SJeff Bonwick 	zdde->zdde_ref_blocks += 1;
2845b24ab676SJeff Bonwick 	zdde->zdde_ref_lsize += BP_GET_LSIZE(bp);
2846b24ab676SJeff Bonwick 	zdde->zdde_ref_psize += BP_GET_PSIZE(bp);
2847b24ab676SJeff Bonwick 	zdde->zdde_ref_dsize += bp_get_dsize_sync(spa, bp);
2848b24ab676SJeff Bonwick 
2849b24ab676SJeff Bonwick 	return (0);
2850b24ab676SJeff Bonwick }
2851b24ab676SJeff Bonwick 
2852b24ab676SJeff Bonwick static void
2853b24ab676SJeff Bonwick dump_simulated_ddt(spa_t *spa)
2854b24ab676SJeff Bonwick {
2855b24ab676SJeff Bonwick 	avl_tree_t t;
2856b24ab676SJeff Bonwick 	void *cookie = NULL;
2857b24ab676SJeff Bonwick 	zdb_ddt_entry_t *zdde;
2858b24ab676SJeff Bonwick 	ddt_histogram_t ddh_total = { 0 };
2859b24ab676SJeff Bonwick 	ddt_stat_t dds_total = { 0 };
2860b24ab676SJeff Bonwick 
2861b24ab676SJeff Bonwick 	avl_create(&t, ddt_entry_compare,
2862b24ab676SJeff Bonwick 	    sizeof (zdb_ddt_entry_t), offsetof(zdb_ddt_entry_t, zdde_node));
2863b24ab676SJeff Bonwick 
2864b24ab676SJeff Bonwick 	spa_config_enter(spa, SCL_CONFIG, FTAG, RW_READER);
2865b24ab676SJeff Bonwick 
2866bbfd46c4SJeff Bonwick 	(void) traverse_pool(spa, 0, TRAVERSE_PRE | TRAVERSE_PREFETCH_METADATA,
2867bbfd46c4SJeff Bonwick 	    zdb_ddt_add_cb, &t);
2868b24ab676SJeff Bonwick 
2869b24ab676SJeff Bonwick 	spa_config_exit(spa, SCL_CONFIG, FTAG);
2870b24ab676SJeff Bonwick 
2871b24ab676SJeff Bonwick 	while ((zdde = avl_destroy_nodes(&t, &cookie)) != NULL) {
2872b24ab676SJeff Bonwick 		ddt_stat_t dds;
2873b24ab676SJeff Bonwick 		uint64_t refcnt = zdde->zdde_ref_blocks;
2874b24ab676SJeff Bonwick 		ASSERT(refcnt != 0);
2875b24ab676SJeff Bonwick 
2876b24ab676SJeff Bonwick 		dds.dds_blocks = zdde->zdde_ref_blocks / refcnt;
2877b24ab676SJeff Bonwick 		dds.dds_lsize = zdde->zdde_ref_lsize / refcnt;
2878b24ab676SJeff Bonwick 		dds.dds_psize = zdde->zdde_ref_psize / refcnt;
2879b24ab676SJeff Bonwick 		dds.dds_dsize = zdde->zdde_ref_dsize / refcnt;
2880b24ab676SJeff Bonwick 
2881b24ab676SJeff Bonwick 		dds.dds_ref_blocks = zdde->zdde_ref_blocks;
2882b24ab676SJeff Bonwick 		dds.dds_ref_lsize = zdde->zdde_ref_lsize;
2883b24ab676SJeff Bonwick 		dds.dds_ref_psize = zdde->zdde_ref_psize;
2884b24ab676SJeff Bonwick 		dds.dds_ref_dsize = zdde->zdde_ref_dsize;
2885b24ab676SJeff Bonwick 
2886bf16b11eSMatthew Ahrens 		ddt_stat_add(&ddh_total.ddh_stat[highbit64(refcnt) - 1],
2887bf16b11eSMatthew Ahrens 		    &dds, 0);
2888b24ab676SJeff Bonwick 
2889b24ab676SJeff Bonwick 		umem_free(zdde, sizeof (*zdde));
2890b24ab676SJeff Bonwick 	}
2891b24ab676SJeff Bonwick 
2892b24ab676SJeff Bonwick 	avl_destroy(&t);
2893b24ab676SJeff Bonwick 
2894b24ab676SJeff Bonwick 	ddt_histogram_stat(&dds_total, &ddh_total);
2895b24ab676SJeff Bonwick 
2896b24ab676SJeff Bonwick 	(void) printf("Simulated DDT histogram:\n");
2897b24ab676SJeff Bonwick 
28989eb19f4dSGeorge Wilson 	zpool_dump_ddt(&dds_total, &ddh_total);
2899b24ab676SJeff Bonwick 
2900b24ab676SJeff Bonwick 	dump_dedup_ratio(&dds_total);
2901b24ab676SJeff Bonwick }
2902b24ab676SJeff Bonwick 
2903fa9e4066Sahrens static void
2904fa9e4066Sahrens dump_zpool(spa_t *spa)
2905fa9e4066Sahrens {
2906fa9e4066Sahrens 	dsl_pool_t *dp = spa_get_dsl(spa);
2907fa9e4066Sahrens 	int rc = 0;
2908fa9e4066Sahrens 
2909b24ab676SJeff Bonwick 	if (dump_opt['S']) {
2910b24ab676SJeff Bonwick 		dump_simulated_ddt(spa);
2911b24ab676SJeff Bonwick 		return;
2912b24ab676SJeff Bonwick 	}
2913b24ab676SJeff Bonwick 
291407428bdfSVictor Latushkin 	if (!dump_opt['e'] && dump_opt['C'] > 1) {
291507428bdfSVictor Latushkin 		(void) printf("\nCached configuration:\n");
291607428bdfSVictor Latushkin 		dump_nvlist(spa->spa_config, 8);
291707428bdfSVictor Latushkin 	}
291807428bdfSVictor Latushkin 
291907428bdfSVictor Latushkin 	if (dump_opt['C'])
292007428bdfSVictor Latushkin 		dump_config(spa);
292107428bdfSVictor Latushkin 
2922fa9e4066Sahrens 	if (dump_opt['u'])
292353b9a4a9SVictor Latushkin 		dump_uberblock(&spa->spa_uberblock, "\nUberblock:\n", "\n");
2924fa9e4066Sahrens 
2925b24ab676SJeff Bonwick 	if (dump_opt['D'])
2926b24ab676SJeff Bonwick 		dump_all_ddts(spa);
2927b24ab676SJeff Bonwick 
292887219db7SVictor Latushkin 	if (dump_opt['d'] > 2 || dump_opt['m'])
292987219db7SVictor Latushkin 		dump_metaslabs(spa);
29302e4c9986SGeorge Wilson 	if (dump_opt['M'])
29312e4c9986SGeorge Wilson 		dump_metaslab_groups(spa);
293287219db7SVictor Latushkin 
293387219db7SVictor Latushkin 	if (dump_opt['d'] || dump_opt['i']) {
2934b5152584SMatthew Ahrens 		uint64_t refcount;
2935fa9e4066Sahrens 		dump_dir(dp->dp_meta_objset);
2936fa9e4066Sahrens 		if (dump_opt['d'] >= 3) {
2937d0475637SMatthew Ahrens 			dump_bpobj(&spa->spa_deferred_bpobj,
2938d0475637SMatthew Ahrens 			    "Deferred frees", 0);
2939cde58dbcSMatthew Ahrens 			if (spa_version(spa) >= SPA_VERSION_DEADLISTS) {
2940cde58dbcSMatthew Ahrens 				dump_bpobj(&spa->spa_dsl_pool->dp_free_bpobj,
2941d0475637SMatthew Ahrens 				    "Pool snapshot frees", 0);
2942ad135b5dSChristopher Siden 			}
2943ad135b5dSChristopher Siden 
2944ad135b5dSChristopher Siden 			if (spa_feature_is_active(spa,
29452acef22dSMatthew Ahrens 			    SPA_FEATURE_ASYNC_DESTROY)) {
2946ad135b5dSChristopher Siden 				dump_bptree(spa->spa_meta_objset,
2947ad135b5dSChristopher Siden 				    spa->spa_dsl_pool->dp_bptree_obj,
2948ad135b5dSChristopher Siden 				    "Pool dataset frees");
2949cde58dbcSMatthew Ahrens 			}
2950fa9e4066Sahrens 			dump_dtl(spa->spa_root_vdev, 0);
2951fa9e4066Sahrens 		}
295207428bdfSVictor Latushkin 		(void) dmu_objset_find(spa_name(spa), dump_one_dir,
295307428bdfSVictor Latushkin 		    NULL, DS_FIND_SNAPSHOTS | DS_FIND_CHILDREN);
2954b5152584SMatthew Ahrens 
2955b5152584SMatthew Ahrens 		(void) feature_get_refcount(spa,
2956b5152584SMatthew Ahrens 		    &spa_feature_table[SPA_FEATURE_LARGE_BLOCKS], &refcount);
2957b5152584SMatthew Ahrens 		if (num_large_blocks != refcount) {
2958b5152584SMatthew Ahrens 			(void) printf("large_blocks feature refcount mismatch: "
2959b5152584SMatthew Ahrens 			    "expected %lld != actual %lld\n",
2960b5152584SMatthew Ahrens 			    (longlong_t)num_large_blocks,
2961b5152584SMatthew Ahrens 			    (longlong_t)refcount);
2962b5152584SMatthew Ahrens 			rc = 2;
2963b5152584SMatthew Ahrens 		} else {
2964b5152584SMatthew Ahrens 			(void) printf("Verified large_blocks feature refcount "
2965b5152584SMatthew Ahrens 			    "is correct (%llu)\n", (longlong_t)refcount);
2966b5152584SMatthew Ahrens 		}
2967fa9e4066Sahrens 	}
2968b5152584SMatthew Ahrens 	if (rc == 0 && (dump_opt['b'] || dump_opt['c']))
2969fa9e4066Sahrens 		rc = dump_block_stats(spa);
2970fa9e4066Sahrens 
29710713e232SGeorge Wilson 	if (rc == 0)
29720713e232SGeorge Wilson 		rc = verify_spacemap_refcounts(spa);
29730713e232SGeorge Wilson 
2974fa9e4066Sahrens 	if (dump_opt['s'])
2975fa9e4066Sahrens 		show_pool_stats(spa);
2976fa9e4066Sahrens 
29778f18d1faSGeorge Wilson 	if (dump_opt['h'])
29788f18d1faSGeorge Wilson 		dump_history(spa);
29798f18d1faSGeorge Wilson 
2980fa9e4066Sahrens 	if (rc != 0)
2981fa9e4066Sahrens 		exit(rc);
2982fa9e4066Sahrens }
2983fa9e4066Sahrens 
298444cd46caSbillm #define	ZDB_FLAG_CHECKSUM	0x0001
298544cd46caSbillm #define	ZDB_FLAG_DECOMPRESS	0x0002
298644cd46caSbillm #define	ZDB_FLAG_BSWAP		0x0004
298744cd46caSbillm #define	ZDB_FLAG_GBH		0x0008
298844cd46caSbillm #define	ZDB_FLAG_INDIRECT	0x0010
298944cd46caSbillm #define	ZDB_FLAG_PHYS		0x0020
299044cd46caSbillm #define	ZDB_FLAG_RAW		0x0040
299144cd46caSbillm #define	ZDB_FLAG_PRINT_BLKPTR	0x0080
299244cd46caSbillm 
299344cd46caSbillm int flagbits[256];
299444cd46caSbillm 
299544cd46caSbillm static void
299644cd46caSbillm zdb_print_blkptr(blkptr_t *bp, int flags)
299744cd46caSbillm {
2998b24ab676SJeff Bonwick 	char blkbuf[BP_SPRINTF_LEN];
299944cd46caSbillm 
300044cd46caSbillm 	if (flags & ZDB_FLAG_BSWAP)
300144cd46caSbillm 		byteswap_uint64_array((void *)bp, sizeof (blkptr_t));
3002b24ab676SJeff Bonwick 
300343466aaeSMax Grossman 	snprintf_blkptr(blkbuf, sizeof (blkbuf), bp);
3004b24ab676SJeff Bonwick 	(void) printf("%s\n", blkbuf);
300544cd46caSbillm }
300644cd46caSbillm 
300744cd46caSbillm static void
300844cd46caSbillm zdb_dump_indirect(blkptr_t *bp, int nbps, int flags)
300944cd46caSbillm {
301044cd46caSbillm 	int i;
301144cd46caSbillm 
301244cd46caSbillm 	for (i = 0; i < nbps; i++)
301344cd46caSbillm 		zdb_print_blkptr(&bp[i], flags);
301444cd46caSbillm }
301544cd46caSbillm 
301644cd46caSbillm static void
301744cd46caSbillm zdb_dump_gbh(void *buf, int flags)
301844cd46caSbillm {
301944cd46caSbillm 	zdb_dump_indirect((blkptr_t *)buf, SPA_GBH_NBLKPTRS, flags);
302044cd46caSbillm }
302144cd46caSbillm 
302244cd46caSbillm static void
302344cd46caSbillm zdb_dump_block_raw(void *buf, uint64_t size, int flags)
302444cd46caSbillm {
302544cd46caSbillm 	if (flags & ZDB_FLAG_BSWAP)
302644cd46caSbillm 		byteswap_uint64_array(buf, size);
3027b24ab676SJeff Bonwick 	(void) write(1, buf, size);
302844cd46caSbillm }
302944cd46caSbillm 
303044cd46caSbillm static void
303144cd46caSbillm zdb_dump_block(char *label, void *buf, uint64_t size, int flags)
303244cd46caSbillm {
303344cd46caSbillm 	uint64_t *d = (uint64_t *)buf;
303444cd46caSbillm 	int nwords = size / sizeof (uint64_t);
303544cd46caSbillm 	int do_bswap = !!(flags & ZDB_FLAG_BSWAP);
303644cd46caSbillm 	int i, j;
303744cd46caSbillm 	char *hdr, *c;
303844cd46caSbillm 
303944cd46caSbillm 
304044cd46caSbillm 	if (do_bswap)
304144cd46caSbillm 		hdr = " 7 6 5 4 3 2 1 0   f e d c b a 9 8";
304244cd46caSbillm 	else
304344cd46caSbillm 		hdr = " 0 1 2 3 4 5 6 7   8 9 a b c d e f";
304444cd46caSbillm 
304544cd46caSbillm 	(void) printf("\n%s\n%6s   %s  0123456789abcdef\n", label, "", hdr);
304644cd46caSbillm 
304744cd46caSbillm 	for (i = 0; i < nwords; i += 2) {
304844cd46caSbillm 		(void) printf("%06llx:  %016llx  %016llx  ",
304944cd46caSbillm 		    (u_longlong_t)(i * sizeof (uint64_t)),
305044cd46caSbillm 		    (u_longlong_t)(do_bswap ? BSWAP_64(d[i]) : d[i]),
305144cd46caSbillm 		    (u_longlong_t)(do_bswap ? BSWAP_64(d[i + 1]) : d[i + 1]));
305244cd46caSbillm 
305344cd46caSbillm 		c = (char *)&d[i];
305444cd46caSbillm 		for (j = 0; j < 2 * sizeof (uint64_t); j++)
305544cd46caSbillm 			(void) printf("%c", isprint(c[j]) ? c[j] : '.');
305644cd46caSbillm 		(void) printf("\n");
305744cd46caSbillm 	}
305844cd46caSbillm }
305944cd46caSbillm 
306044cd46caSbillm /*
306144cd46caSbillm  * There are two acceptable formats:
306244cd46caSbillm  *	leaf_name	  - For example: c1t0d0 or /tmp/ztest.0a
306344cd46caSbillm  *	child[.child]*    - For example: 0.1.1
306444cd46caSbillm  *
306544cd46caSbillm  * The second form can be used to specify arbitrary vdevs anywhere
306644cd46caSbillm  * in the heirarchy.  For example, in a pool with a mirror of
306744cd46caSbillm  * RAID-Zs, you can specify either RAID-Z vdev with 0.0 or 0.1 .
306844cd46caSbillm  */
306944cd46caSbillm static vdev_t *
307044cd46caSbillm zdb_vdev_lookup(vdev_t *vdev, char *path)
307144cd46caSbillm {
307244cd46caSbillm 	char *s, *p, *q;
307344cd46caSbillm 	int i;
307444cd46caSbillm 
307544cd46caSbillm 	if (vdev == NULL)
307644cd46caSbillm 		return (NULL);
307744cd46caSbillm 
307844cd46caSbillm 	/* First, assume the x.x.x.x format */
307944cd46caSbillm 	i = (int)strtoul(path, &s, 10);
308044cd46caSbillm 	if (s == path || (s && *s != '.' && *s != '\0'))
308144cd46caSbillm 		goto name;
308244cd46caSbillm 	if (i < 0 || i >= vdev->vdev_children)
308344cd46caSbillm 		return (NULL);
308444cd46caSbillm 
308544cd46caSbillm 	vdev = vdev->vdev_child[i];
308644cd46caSbillm 	if (*s == '\0')
308744cd46caSbillm 		return (vdev);
308844cd46caSbillm 	return (zdb_vdev_lookup(vdev, s+1));
308944cd46caSbillm 
309044cd46caSbillm name:
309144cd46caSbillm 	for (i = 0; i < vdev->vdev_children; i++) {
309244cd46caSbillm 		vdev_t *vc = vdev->vdev_child[i];
309344cd46caSbillm 
309444cd46caSbillm 		if (vc->vdev_path == NULL) {
309544cd46caSbillm 			vc = zdb_vdev_lookup(vc, path);
309644cd46caSbillm 			if (vc == NULL)
309744cd46caSbillm 				continue;
309844cd46caSbillm 			else
309944cd46caSbillm 				return (vc);
310044cd46caSbillm 		}
310144cd46caSbillm 
310244cd46caSbillm 		p = strrchr(vc->vdev_path, '/');
310344cd46caSbillm 		p = p ? p + 1 : vc->vdev_path;
310444cd46caSbillm 		q = &vc->vdev_path[strlen(vc->vdev_path) - 2];
310544cd46caSbillm 
310644cd46caSbillm 		if (strcmp(vc->vdev_path, path) == 0)
310744cd46caSbillm 			return (vc);
310844cd46caSbillm 		if (strcmp(p, path) == 0)
310944cd46caSbillm 			return (vc);
311044cd46caSbillm 		if (strcmp(q, "s0") == 0 && strncmp(p, path, q - p) == 0)
311144cd46caSbillm 			return (vc);
311244cd46caSbillm 	}
311344cd46caSbillm 
311444cd46caSbillm 	return (NULL);
311544cd46caSbillm }
311644cd46caSbillm 
311744cd46caSbillm /*
311844cd46caSbillm  * Read a block from a pool and print it out.  The syntax of the
311944cd46caSbillm  * block descriptor is:
312044cd46caSbillm  *
312144cd46caSbillm  *	pool:vdev_specifier:offset:size[:flags]
312244cd46caSbillm  *
312344cd46caSbillm  *	pool           - The name of the pool you wish to read from
312444cd46caSbillm  *	vdev_specifier - Which vdev (see comment for zdb_vdev_lookup)
312544cd46caSbillm  *	offset         - offset, in hex, in bytes
312644cd46caSbillm  *	size           - Amount of data to read, in hex, in bytes
312744cd46caSbillm  *	flags          - A string of characters specifying options
312844cd46caSbillm  *		 b: Decode a blkptr at given offset within block
312944cd46caSbillm  *		*c: Calculate and display checksums
3130b24ab676SJeff Bonwick  *		 d: Decompress data before dumping
313144cd46caSbillm  *		 e: Byteswap data before dumping
3132b24ab676SJeff Bonwick  *		 g: Display data as a gang block header
3133b24ab676SJeff Bonwick  *		 i: Display as an indirect block
313444cd46caSbillm  *		 p: Do I/O to physical offset
313544cd46caSbillm  *		 r: Dump raw data to stdout
313644cd46caSbillm  *
313744cd46caSbillm  *              * = not yet implemented
313844cd46caSbillm  */
313944cd46caSbillm static void
314007428bdfSVictor Latushkin zdb_read_block(char *thing, spa_t *spa)
314144cd46caSbillm {
3142b24ab676SJeff Bonwick 	blkptr_t blk, *bp = &blk;
3143b24ab676SJeff Bonwick 	dva_t *dva = bp->blk_dva;
314444cd46caSbillm 	int flags = 0;
3145b24ab676SJeff Bonwick 	uint64_t offset = 0, size = 0, psize = 0, lsize = 0, blkptr_offset = 0;
314644cd46caSbillm 	zio_t *zio;
314744cd46caSbillm 	vdev_t *vd;
3148b24ab676SJeff Bonwick 	void *pbuf, *lbuf, *buf;
314907428bdfSVictor Latushkin 	char *s, *p, *dup, *vdev, *flagstr;
3150b24ab676SJeff Bonwick 	int i, error;
315144cd46caSbillm 
315244cd46caSbillm 	dup = strdup(thing);
315344cd46caSbillm 	s = strtok(dup, ":");
315444cd46caSbillm 	vdev = s ? s : "";
315544cd46caSbillm 	s = strtok(NULL, ":");
315644cd46caSbillm 	offset = strtoull(s ? s : "", NULL, 16);
315744cd46caSbillm 	s = strtok(NULL, ":");
315844cd46caSbillm 	size = strtoull(s ? s : "", NULL, 16);
315944cd46caSbillm 	s = strtok(NULL, ":");
316044cd46caSbillm 	flagstr = s ? s : "";
316144cd46caSbillm 
316244cd46caSbillm 	s = NULL;
316344cd46caSbillm 	if (size == 0)
316444cd46caSbillm 		s = "size must not be zero";
316544cd46caSbillm 	if (!IS_P2ALIGNED(size, DEV_BSIZE))
316644cd46caSbillm 		s = "size must be a multiple of sector size";
316744cd46caSbillm 	if (!IS_P2ALIGNED(offset, DEV_BSIZE))
316844cd46caSbillm 		s = "offset must be a multiple of sector size";
316944cd46caSbillm 	if (s) {
317044cd46caSbillm 		(void) printf("Invalid block specifier: %s  - %s\n", thing, s);
317144cd46caSbillm 		free(dup);
317244cd46caSbillm 		return;
317344cd46caSbillm 	}
317444cd46caSbillm 
317544cd46caSbillm 	for (s = strtok(flagstr, ":"); s; s = strtok(NULL, ":")) {
317644cd46caSbillm 		for (i = 0; flagstr[i]; i++) {
31775ad82045Snd 			int bit = flagbits[(uchar_t)flagstr[i]];
317844cd46caSbillm 
317944cd46caSbillm 			if (bit == 0) {
318044cd46caSbillm 				(void) printf("***Invalid flag: %c\n",
318144cd46caSbillm 				    flagstr[i]);
318244cd46caSbillm 				continue;
318344cd46caSbillm 			}
318444cd46caSbillm 			flags |= bit;
318544cd46caSbillm 
318644cd46caSbillm 			/* If it's not something with an argument, keep going */
3187b24ab676SJeff Bonwick 			if ((bit & (ZDB_FLAG_CHECKSUM |
318844cd46caSbillm 			    ZDB_FLAG_PRINT_BLKPTR)) == 0)
318944cd46caSbillm 				continue;
319044cd46caSbillm 
319144cd46caSbillm 			p = &flagstr[i + 1];
319244cd46caSbillm 			if (bit == ZDB_FLAG_PRINT_BLKPTR)
319344cd46caSbillm 				blkptr_offset = strtoull(p, &p, 16);
319444cd46caSbillm 			if (*p != ':' && *p != '\0') {
319544cd46caSbillm 				(void) printf("***Invalid flag arg: '%s'\n", s);
319644cd46caSbillm 				free(dup);
319744cd46caSbillm 				return;
319844cd46caSbillm 			}
319944cd46caSbillm 		}
320044cd46caSbillm 	}
320144cd46caSbillm 
320244cd46caSbillm 	vd = zdb_vdev_lookup(spa->spa_root_vdev, vdev);
320344cd46caSbillm 	if (vd == NULL) {
320444cd46caSbillm 		(void) printf("***Invalid vdev: %s\n", vdev);
320544cd46caSbillm 		free(dup);
320644cd46caSbillm 		return;
320744cd46caSbillm 	} else {
320844cd46caSbillm 		if (vd->vdev_path)
3209b24ab676SJeff Bonwick 			(void) fprintf(stderr, "Found vdev: %s\n",
3210b24ab676SJeff Bonwick 			    vd->vdev_path);
321144cd46caSbillm 		else
3212b24ab676SJeff Bonwick 			(void) fprintf(stderr, "Found vdev type: %s\n",
321344cd46caSbillm 			    vd->vdev_ops->vdev_op_type);
321444cd46caSbillm 	}
321544cd46caSbillm 
3216b24ab676SJeff Bonwick 	psize = size;
3217b24ab676SJeff Bonwick 	lsize = size;
321844cd46caSbillm 
3219b24ab676SJeff Bonwick 	pbuf = umem_alloc(SPA_MAXBLOCKSIZE, UMEM_NOFAIL);
3220b24ab676SJeff Bonwick 	lbuf = umem_alloc(SPA_MAXBLOCKSIZE, UMEM_NOFAIL);
3221b24ab676SJeff Bonwick 
3222b24ab676SJeff Bonwick 	BP_ZERO(bp);
3223b24ab676SJeff Bonwick 
3224b24ab676SJeff Bonwick 	DVA_SET_VDEV(&dva[0], vd->vdev_id);
3225b24ab676SJeff Bonwick 	DVA_SET_OFFSET(&dva[0], offset);
3226b24ab676SJeff Bonwick 	DVA_SET_GANG(&dva[0], !!(flags & ZDB_FLAG_GBH));
3227b24ab676SJeff Bonwick 	DVA_SET_ASIZE(&dva[0], vdev_psize_to_asize(vd, psize));
3228b24ab676SJeff Bonwick 
3229b24ab676SJeff Bonwick 	BP_SET_BIRTH(bp, TXG_INITIAL, TXG_INITIAL);
3230b24ab676SJeff Bonwick 
3231b24ab676SJeff Bonwick 	BP_SET_LSIZE(bp, lsize);
3232b24ab676SJeff Bonwick 	BP_SET_PSIZE(bp, psize);
3233b24ab676SJeff Bonwick 	BP_SET_COMPRESS(bp, ZIO_COMPRESS_OFF);
3234b24ab676SJeff Bonwick 	BP_SET_CHECKSUM(bp, ZIO_CHECKSUM_OFF);
3235b24ab676SJeff Bonwick 	BP_SET_TYPE(bp, DMU_OT_NONE);
3236b24ab676SJeff Bonwick 	BP_SET_LEVEL(bp, 0);
3237b24ab676SJeff Bonwick 	BP_SET_DEDUP(bp, 0);
3238b24ab676SJeff Bonwick 	BP_SET_BYTEORDER(bp, ZFS_HOST_BYTEORDER);
323944cd46caSbillm 
3240e14bb325SJeff Bonwick 	spa_config_enter(spa, SCL_STATE, FTAG, RW_READER);
324144cd46caSbillm 	zio = zio_root(spa, NULL, NULL, 0);
3242b24ab676SJeff Bonwick 
3243b24ab676SJeff Bonwick 	if (vd == vd->vdev_top) {
3244b24ab676SJeff Bonwick 		/*
3245b24ab676SJeff Bonwick 		 * Treat this as a normal block read.
3246b24ab676SJeff Bonwick 		 */
3247b24ab676SJeff Bonwick 		zio_nowait(zio_read(zio, spa, bp, pbuf, psize, NULL, NULL,
3248b24ab676SJeff Bonwick 		    ZIO_PRIORITY_SYNC_READ,
3249b24ab676SJeff Bonwick 		    ZIO_FLAG_CANFAIL | ZIO_FLAG_RAW, NULL));
3250b24ab676SJeff Bonwick 	} else {
3251b24ab676SJeff Bonwick 		/*
3252b24ab676SJeff Bonwick 		 * Treat this as a vdev child I/O.
3253b24ab676SJeff Bonwick 		 */
3254b24ab676SJeff Bonwick 		zio_nowait(zio_vdev_child_io(zio, bp, vd, offset, pbuf, psize,
3255b24ab676SJeff Bonwick 		    ZIO_TYPE_READ, ZIO_PRIORITY_SYNC_READ,
3256b24ab676SJeff Bonwick 		    ZIO_FLAG_DONT_CACHE | ZIO_FLAG_DONT_QUEUE |
3257b24ab676SJeff Bonwick 		    ZIO_FLAG_DONT_PROPAGATE | ZIO_FLAG_DONT_RETRY |
3258b24ab676SJeff Bonwick 		    ZIO_FLAG_CANFAIL | ZIO_FLAG_RAW, NULL, NULL));
3259b24ab676SJeff Bonwick 	}
3260b24ab676SJeff Bonwick 
326144cd46caSbillm 	error = zio_wait(zio);
3262e14bb325SJeff Bonwick 	spa_config_exit(spa, SCL_STATE, FTAG);
326344cd46caSbillm 
326444cd46caSbillm 	if (error) {
326544cd46caSbillm 		(void) printf("Read of %s failed, error: %d\n", thing, error);
326644cd46caSbillm 		goto out;
326744cd46caSbillm 	}
326844cd46caSbillm 
3269b24ab676SJeff Bonwick 	if (flags & ZDB_FLAG_DECOMPRESS) {
3270b24ab676SJeff Bonwick 		/*
3271b24ab676SJeff Bonwick 		 * We don't know how the data was compressed, so just try
3272b24ab676SJeff Bonwick 		 * every decompress function at every inflated blocksize.
3273b24ab676SJeff Bonwick 		 */
3274b24ab676SJeff Bonwick 		enum zio_compress c;
3275b24ab676SJeff Bonwick 		void *pbuf2 = umem_alloc(SPA_MAXBLOCKSIZE, UMEM_NOFAIL);
3276b24ab676SJeff Bonwick 		void *lbuf2 = umem_alloc(SPA_MAXBLOCKSIZE, UMEM_NOFAIL);
3277b24ab676SJeff Bonwick 
3278b24ab676SJeff Bonwick 		bcopy(pbuf, pbuf2, psize);
3279b24ab676SJeff Bonwick 
3280b24ab676SJeff Bonwick 		VERIFY(random_get_pseudo_bytes((uint8_t *)pbuf + psize,
3281b24ab676SJeff Bonwick 		    SPA_MAXBLOCKSIZE - psize) == 0);
3282b24ab676SJeff Bonwick 
3283b24ab676SJeff Bonwick 		VERIFY(random_get_pseudo_bytes((uint8_t *)pbuf2 + psize,
3284b24ab676SJeff Bonwick 		    SPA_MAXBLOCKSIZE - psize) == 0);
3285b24ab676SJeff Bonwick 
3286b24ab676SJeff Bonwick 		for (lsize = SPA_MAXBLOCKSIZE; lsize > psize;
3287b24ab676SJeff Bonwick 		    lsize -= SPA_MINBLOCKSIZE) {
3288b24ab676SJeff Bonwick 			for (c = 0; c < ZIO_COMPRESS_FUNCTIONS; c++) {
3289b24ab676SJeff Bonwick 				if (zio_decompress_data(c, pbuf, lbuf,
3290b24ab676SJeff Bonwick 				    psize, lsize) == 0 &&
3291b24ab676SJeff Bonwick 				    zio_decompress_data(c, pbuf2, lbuf2,
3292b24ab676SJeff Bonwick 				    psize, lsize) == 0 &&
3293b24ab676SJeff Bonwick 				    bcmp(lbuf, lbuf2, lsize) == 0)
3294b24ab676SJeff Bonwick 					break;
3295b24ab676SJeff Bonwick 			}
3296b24ab676SJeff Bonwick 			if (c != ZIO_COMPRESS_FUNCTIONS)
3297b24ab676SJeff Bonwick 				break;
3298b24ab676SJeff Bonwick 			lsize -= SPA_MINBLOCKSIZE;
3299b24ab676SJeff Bonwick 		}
3300b24ab676SJeff Bonwick 
3301b24ab676SJeff Bonwick 		umem_free(pbuf2, SPA_MAXBLOCKSIZE);
3302b24ab676SJeff Bonwick 		umem_free(lbuf2, SPA_MAXBLOCKSIZE);
3303b24ab676SJeff Bonwick 
3304b24ab676SJeff Bonwick 		if (lsize <= psize) {
3305b24ab676SJeff Bonwick 			(void) printf("Decompress of %s failed\n", thing);
3306b24ab676SJeff Bonwick 			goto out;
3307b24ab676SJeff Bonwick 		}
3308b24ab676SJeff Bonwick 		buf = lbuf;
3309b24ab676SJeff Bonwick 		size = lsize;
3310b24ab676SJeff Bonwick 	} else {
3311b24ab676SJeff Bonwick 		buf = pbuf;
3312b24ab676SJeff Bonwick 		size = psize;
3313b24ab676SJeff Bonwick 	}
3314b24ab676SJeff Bonwick 
331544cd46caSbillm 	if (flags & ZDB_FLAG_PRINT_BLKPTR)
331644cd46caSbillm 		zdb_print_blkptr((blkptr_t *)(void *)
331744cd46caSbillm 		    ((uintptr_t)buf + (uintptr_t)blkptr_offset), flags);
331844cd46caSbillm 	else if (flags & ZDB_FLAG_RAW)
331944cd46caSbillm 		zdb_dump_block_raw(buf, size, flags);
332044cd46caSbillm 	else if (flags & ZDB_FLAG_INDIRECT)
332144cd46caSbillm 		zdb_dump_indirect((blkptr_t *)buf, size / sizeof (blkptr_t),
332244cd46caSbillm 		    flags);
332344cd46caSbillm 	else if (flags & ZDB_FLAG_GBH)
332444cd46caSbillm 		zdb_dump_gbh(buf, flags);
332544cd46caSbillm 	else
332644cd46caSbillm 		zdb_dump_block(thing, buf, size, flags);
332744cd46caSbillm 
332844cd46caSbillm out:
3329b24ab676SJeff Bonwick 	umem_free(pbuf, SPA_MAXBLOCKSIZE);
3330b24ab676SJeff Bonwick 	umem_free(lbuf, SPA_MAXBLOCKSIZE);
333144cd46caSbillm 	free(dup);
333244cd46caSbillm }
333344cd46caSbillm 
3334de6628f0Sck static boolean_t
33353ad6c7f9SVictor Latushkin pool_match(nvlist_t *cfg, char *tgt)
3336de6628f0Sck {
33373ad6c7f9SVictor Latushkin 	uint64_t v, guid = strtoull(tgt, NULL, 0);
3338de6628f0Sck 	char *s;
3339de6628f0Sck 
3340de6628f0Sck 	if (guid != 0) {
33413ad6c7f9SVictor Latushkin 		if (nvlist_lookup_uint64(cfg, ZPOOL_CONFIG_POOL_GUID, &v) == 0)
33423ad6c7f9SVictor Latushkin 			return (v == guid);
3343de6628f0Sck 	} else {
33443ad6c7f9SVictor Latushkin 		if (nvlist_lookup_string(cfg, ZPOOL_CONFIG_POOL_NAME, &s) == 0)
33453ad6c7f9SVictor Latushkin 			return (strcmp(s, tgt) == 0);
3346de6628f0Sck 	}
33473ad6c7f9SVictor Latushkin 	return (B_FALSE);
3348de6628f0Sck }
3349de6628f0Sck 
33503ad6c7f9SVictor Latushkin static char *
33513ad6c7f9SVictor Latushkin find_zpool(char **target, nvlist_t **configp, int dirc, char **dirv)
3352de6628f0Sck {
3353de6628f0Sck 	nvlist_t *pools;
3354de6628f0Sck 	nvlist_t *match = NULL;
33553ad6c7f9SVictor Latushkin 	char *name = NULL;
33563ad6c7f9SVictor Latushkin 	char *sepp = NULL;
33573ad6c7f9SVictor Latushkin 	char sep;
33583ad6c7f9SVictor Latushkin 	int count = 0;
3359d41c4376SMark J Musante 	importargs_t args = { 0 };
3360d41c4376SMark J Musante 
3361d41c4376SMark J Musante 	args.paths = dirc;
3362d41c4376SMark J Musante 	args.path = dirv;
3363d41c4376SMark J Musante 	args.can_be_active = B_TRUE;
3364de6628f0Sck 
33653ad6c7f9SVictor Latushkin 	if ((sepp = strpbrk(*target, "/@")) != NULL) {
33663ad6c7f9SVictor Latushkin 		sep = *sepp;
33673ad6c7f9SVictor Latushkin 		*sepp = '\0';
33683ad6c7f9SVictor Latushkin 	}
33693ad6c7f9SVictor Latushkin 
3370d41c4376SMark J Musante 	pools = zpool_search_import(g_zfs, &args);
3371de6628f0Sck 
3372de6628f0Sck 	if (pools != NULL) {
3373de6628f0Sck 		nvpair_t *elem = NULL;
3374de6628f0Sck 		while ((elem = nvlist_next_nvpair(pools, elem)) != NULL) {
3375de6628f0Sck 			verify(nvpair_value_nvlist(elem, configp) == 0);
33763ad6c7f9SVictor Latushkin 			if (pool_match(*configp, *target)) {
33773ad6c7f9SVictor Latushkin 				count++;
3378de6628f0Sck 				if (match != NULL) {
33793ad6c7f9SVictor Latushkin 					/* print previously found config */
33803ad6c7f9SVictor Latushkin 					if (name != NULL) {
33813ad6c7f9SVictor Latushkin 						(void) printf("%s\n", name);
33823ad6c7f9SVictor Latushkin 						dump_nvlist(match, 8);
33833ad6c7f9SVictor Latushkin 						name = NULL;
33843ad6c7f9SVictor Latushkin 					}
33853ad6c7f9SVictor Latushkin 					(void) printf("%s\n",
33863ad6c7f9SVictor Latushkin 					    nvpair_name(elem));
33873ad6c7f9SVictor Latushkin 					dump_nvlist(*configp, 8);
3388de6628f0Sck 				} else {
3389de6628f0Sck 					match = *configp;
33903ad6c7f9SVictor Latushkin 					name = nvpair_name(elem);
3391de6628f0Sck 				}
3392de6628f0Sck 			}
3393de6628f0Sck 		}
3394de6628f0Sck 	}
33953ad6c7f9SVictor Latushkin 	if (count > 1)
33963ad6c7f9SVictor Latushkin 		(void) fatal("\tMatched %d pools - use pool GUID "
33973ad6c7f9SVictor Latushkin 		    "instead of pool name or \n"
33983ad6c7f9SVictor Latushkin 		    "\tpool name part of a dataset name to select pool", count);
33993ad6c7f9SVictor Latushkin 
34003ad6c7f9SVictor Latushkin 	if (sepp)
34013ad6c7f9SVictor Latushkin 		*sepp = sep;
34023ad6c7f9SVictor Latushkin 	/*
34033ad6c7f9SVictor Latushkin 	 * If pool GUID was specified for pool id, replace it with pool name
34043ad6c7f9SVictor Latushkin 	 */
34053ad6c7f9SVictor Latushkin 	if (name && (strstr(*target, name) != *target)) {
34063ad6c7f9SVictor Latushkin 		int sz = 1 + strlen(name) + ((sepp) ? strlen(sepp) : 0);
34073ad6c7f9SVictor Latushkin 
34083ad6c7f9SVictor Latushkin 		*target = umem_alloc(sz, UMEM_NOFAIL);
34093ad6c7f9SVictor Latushkin 		(void) snprintf(*target, sz, "%s%s", name, sepp ? sepp : "");
34103ad6c7f9SVictor Latushkin 	}
3411de6628f0Sck 
34123ad6c7f9SVictor Latushkin 	*configp = name ? match : NULL;
3413de6628f0Sck 
34143ad6c7f9SVictor Latushkin 	return (name);
3415de6628f0Sck }
3416de6628f0Sck 
3417fa9e4066Sahrens int
3418fa9e4066Sahrens main(int argc, char **argv)
3419fa9e4066Sahrens {
3420fa9e4066Sahrens 	int i, c;
3421fa9e4066Sahrens 	struct rlimit rl = { 1024, 1024 };
34223ad6c7f9SVictor Latushkin 	spa_t *spa = NULL;
3423fa9e4066Sahrens 	objset_t *os = NULL;
3424fa9e4066Sahrens 	int dump_all = 1;
3425fa9e4066Sahrens 	int verbose = 0;
3426c8ee1847SVictor Latushkin 	int error = 0;
34273ad6c7f9SVictor Latushkin 	char **searchdirs = NULL;
34283ad6c7f9SVictor Latushkin 	int nsearch = 0;
34293ad6c7f9SVictor Latushkin 	char *target;
3430468c413aSTim Haley 	nvlist_t *policy = NULL;
3431468c413aSTim Haley 	uint64_t max_txg = UINT64_MAX;
3432c8ee1847SVictor Latushkin 	int rewind = ZPOOL_NEVER_REWIND;
3433fa9e4066Sahrens 
3434fa9e4066Sahrens 	(void) setrlimit(RLIMIT_NOFILE, &rl);
3435004388ebScasper 	(void) enable_extended_FILE_stdio(-1, -1);
3436fa9e4066Sahrens 
3437fa9e4066Sahrens 	dprintf_setup(&argc, argv);
3438fa9e4066Sahrens 
3439df15e419SMatthew Ahrens 	while ((c = getopt(argc, argv,
34402e4c9986SGeorge Wilson 	    "bcdhilmMI:suCDRSAFLXx:evp:t:U:P")) != -1) {
3441fa9e4066Sahrens 		switch (c) {
3442fa9e4066Sahrens 		case 'b':
3443fa9e4066Sahrens 		case 'c':
3444b24ab676SJeff Bonwick 		case 'd':
3445b24ab676SJeff Bonwick 		case 'h':
3446b24ab676SJeff Bonwick 		case 'i':
3447b24ab676SJeff Bonwick 		case 'l':
3448d6e555bdSGeorge Wilson 		case 'm':
3449fa9e4066Sahrens 		case 's':
3450b24ab676SJeff Bonwick 		case 'u':
3451fa9e4066Sahrens 		case 'C':
3452b24ab676SJeff Bonwick 		case 'D':
34532e4c9986SGeorge Wilson 		case 'M':
345444cd46caSbillm 		case 'R':
3455b24ab676SJeff Bonwick 		case 'S':
3456fa9e4066Sahrens 			dump_opt[c]++;
3457fa9e4066Sahrens 			dump_all = 0;
3458fa9e4066Sahrens 			break;
3459feef89cfSVictor Latushkin 		case 'A':
3460c8ee1847SVictor Latushkin 		case 'F':
346182a0a985SVictor Latushkin 		case 'L':
3462c8ee1847SVictor Latushkin 		case 'X':
34633ad6c7f9SVictor Latushkin 		case 'e':
34643f9d6ad7SLin Ling 		case 'P':
346582a0a985SVictor Latushkin 			dump_opt[c]++;
346682a0a985SVictor Latushkin 			break;
34672e4c9986SGeorge Wilson 		case 'I':
346831d7e8faSGeorge Wilson 			max_inflight = strtoull(optarg, NULL, 0);
346931d7e8faSGeorge Wilson 			if (max_inflight == 0) {
347031d7e8faSGeorge Wilson 				(void) fprintf(stderr, "maximum number "
347131d7e8faSGeorge Wilson 				    "of inflight I/Os must be greater "
347231d7e8faSGeorge Wilson 				    "than 0\n");
347331d7e8faSGeorge Wilson 				usage();
347431d7e8faSGeorge Wilson 			}
347531d7e8faSGeorge Wilson 			break;
3476de6628f0Sck 		case 'p':
34773ad6c7f9SVictor Latushkin 			if (searchdirs == NULL) {
34783ad6c7f9SVictor Latushkin 				searchdirs = umem_alloc(sizeof (char *),
34793ad6c7f9SVictor Latushkin 				    UMEM_NOFAIL);
34803ad6c7f9SVictor Latushkin 			} else {
34813ad6c7f9SVictor Latushkin 				char **tmp = umem_alloc((nsearch + 1) *
34823ad6c7f9SVictor Latushkin 				    sizeof (char *), UMEM_NOFAIL);
34833ad6c7f9SVictor Latushkin 				bcopy(searchdirs, tmp, nsearch *
34843ad6c7f9SVictor Latushkin 				    sizeof (char *));
34853ad6c7f9SVictor Latushkin 				umem_free(searchdirs,
34863ad6c7f9SVictor Latushkin 				    nsearch * sizeof (char *));
34873ad6c7f9SVictor Latushkin 				searchdirs = tmp;
34883ad6c7f9SVictor Latushkin 			}
34893ad6c7f9SVictor Latushkin 			searchdirs[nsearch++] = optarg;
3490de6628f0Sck 			break;
34912e551927SVictor Latushkin 		case 't':
3492468c413aSTim Haley 			max_txg = strtoull(optarg, NULL, 0);
3493468c413aSTim Haley 			if (max_txg < TXG_INITIAL) {
34942e551927SVictor Latushkin 				(void) fprintf(stderr, "incorrect txg "
34952e551927SVictor Latushkin 				    "specified: %s\n", optarg);
34962e551927SVictor Latushkin 				usage();
34972e551927SVictor Latushkin 			}
34982e551927SVictor Latushkin 			break;
3499b24ab676SJeff Bonwick 		case 'U':
3500b24ab676SJeff Bonwick 			spa_config_path = optarg;
3501b24ab676SJeff Bonwick 			break;
35022e4c9986SGeorge Wilson 		case 'v':
35032e4c9986SGeorge Wilson 			verbose++;
35042e4c9986SGeorge Wilson 			break;
35052e4c9986SGeorge Wilson 		case 'x':
35062e4c9986SGeorge Wilson 			vn_dumpdir = optarg;
35072e4c9986SGeorge Wilson 			break;
3508fa9e4066Sahrens 		default:
3509fa9e4066Sahrens 			usage();
3510fa9e4066Sahrens 			break;
3511fa9e4066Sahrens 		}
3512fa9e4066Sahrens 	}
3513fa9e4066Sahrens 
35143ad6c7f9SVictor Latushkin 	if (!dump_opt['e'] && searchdirs != NULL) {
351588b7b0f2SMatthew Ahrens 		(void) fprintf(stderr, "-p option requires use of -e\n");
351688b7b0f2SMatthew Ahrens 		usage();
351788b7b0f2SMatthew Ahrens 	}
3518de6628f0Sck 
351906be9802SMatthew Ahrens 	/*
352006be9802SMatthew Ahrens 	 * ZDB does not typically re-read blocks; therefore limit the ARC
352106be9802SMatthew Ahrens 	 * to 256 MB, which can be used entirely for metadata.
352206be9802SMatthew Ahrens 	 */
352306be9802SMatthew Ahrens 	zfs_arc_max = zfs_arc_meta_limit = 256 * 1024 * 1024;
352406be9802SMatthew Ahrens 
3525f7950bf1SMatthew Ahrens 	/*
3526f7950bf1SMatthew Ahrens 	 * "zdb -c" uses checksum-verifying scrub i/os which are async reads.
3527f7950bf1SMatthew Ahrens 	 * "zdb -b" uses traversal prefetch which uses async reads.
3528f7950bf1SMatthew Ahrens 	 * For good performance, let several of them be active at once.
3529f7950bf1SMatthew Ahrens 	 */
3530f7950bf1SMatthew Ahrens 	zfs_vdev_async_read_max_active = 10;
3531f7950bf1SMatthew Ahrens 
3532fa9e4066Sahrens 	kernel_init(FREAD);
3533de6628f0Sck 	g_zfs = libzfs_init();
353491ebeef5Sahrens 	ASSERT(g_zfs != NULL);
3535fa9e4066Sahrens 
3536b24ab676SJeff Bonwick 	if (dump_all)
3537b24ab676SJeff Bonwick 		verbose = MAX(verbose, 1);
3538b24ab676SJeff Bonwick 
3539fa9e4066Sahrens 	for (c = 0; c < 256; c++) {
35403f9d6ad7SLin Ling 		if (dump_all && !strchr("elAFLRSXP", c))
3541fa9e4066Sahrens 			dump_opt[c] = 1;
3542fa9e4066Sahrens 		if (dump_opt[c])
3543fa9e4066Sahrens 			dump_opt[c] += verbose;
3544fa9e4066Sahrens 	}
3545fa9e4066Sahrens 
3546feef89cfSVictor Latushkin 	aok = (dump_opt['A'] == 1) || (dump_opt['A'] > 2);
3547feef89cfSVictor Latushkin 	zfs_recover = (dump_opt['A'] > 1);
3548feef89cfSVictor Latushkin 
3549fa9e4066Sahrens 	argc -= optind;
3550fa9e4066Sahrens 	argv += optind;
3551fa9e4066Sahrens 
355207428bdfSVictor Latushkin 	if (argc < 2 && dump_opt['R'])
355307428bdfSVictor Latushkin 		usage();
3554fa9e4066Sahrens 	if (argc < 1) {
35553ad6c7f9SVictor Latushkin 		if (!dump_opt['e'] && dump_opt['C']) {
3556e829d913Sck 			dump_cachefile(spa_config_path);
3557fa9e4066Sahrens 			return (0);
3558fa9e4066Sahrens 		}
3559fa9e4066Sahrens 		usage();
3560fa9e4066Sahrens 	}
3561fa9e4066Sahrens 
3562fa9e4066Sahrens 	if (dump_opt['l']) {
3563fa9e4066Sahrens 		dump_label(argv[0]);
3564fa9e4066Sahrens 		return (0);
3565fa9e4066Sahrens 	}
3566fa9e4066Sahrens 
3567c8ee1847SVictor Latushkin 	if (dump_opt['X'] || dump_opt['F'])
3568c8ee1847SVictor Latushkin 		rewind = ZPOOL_DO_REWIND |
3569c8ee1847SVictor Latushkin 		    (dump_opt['X'] ? ZPOOL_EXTREME_REWIND : 0);
3570c8ee1847SVictor Latushkin 
3571c8ee1847SVictor Latushkin 	if (nvlist_alloc(&policy, NV_UNIQUE_NAME_TYPE, 0) != 0 ||
3572c8ee1847SVictor Latushkin 	    nvlist_add_uint64(policy, ZPOOL_REWIND_REQUEST_TXG, max_txg) != 0 ||
3573c8ee1847SVictor Latushkin 	    nvlist_add_uint32(policy, ZPOOL_REWIND_REQUEST, rewind) != 0)
3574c8ee1847SVictor Latushkin 		fatal("internal error: %s", strerror(ENOMEM));
3575c8ee1847SVictor Latushkin 
3576c5904d13Seschrock 	error = 0;
35773ad6c7f9SVictor Latushkin 	target = argv[0];
3578990b4856Slling 
35793ad6c7f9SVictor Latushkin 	if (dump_opt['e']) {
35803ad6c7f9SVictor Latushkin 		nvlist_t *cfg = NULL;
35813ad6c7f9SVictor Latushkin 		char *name = find_zpool(&target, &cfg, nsearch, searchdirs);
3582990b4856Slling 
35833ad6c7f9SVictor Latushkin 		error = ENOENT;
35843ad6c7f9SVictor Latushkin 		if (name) {
358507428bdfSVictor Latushkin 			if (dump_opt['C'] > 1) {
358607428bdfSVictor Latushkin 				(void) printf("\nConfiguration for import:\n");
358707428bdfSVictor Latushkin 				dump_nvlist(cfg, 8);
358807428bdfSVictor Latushkin 			}
3589c8ee1847SVictor Latushkin 			if (nvlist_add_nvlist(cfg,
3590468c413aSTim Haley 			    ZPOOL_REWIND_POLICY, policy) != 0) {
3591468c413aSTim Haley 				fatal("can't open '%s': %s",
3592468c413aSTim Haley 				    target, strerror(ENOMEM));
3593468c413aSTim Haley 			}
35944b964adaSGeorge Wilson 			if ((error = spa_import(name, cfg, NULL,
35954b964adaSGeorge Wilson 			    ZFS_IMPORT_MISSING_LOG)) != 0) {
35964b964adaSGeorge Wilson 				error = spa_import(name, cfg, NULL,
35974b964adaSGeorge Wilson 				    ZFS_IMPORT_VERBATIM);
35984b964adaSGeorge Wilson 			}
3599de6628f0Sck 		}
3600c5904d13Seschrock 	}
3601c5904d13Seschrock 
3602c5904d13Seschrock 	if (error == 0) {
360307428bdfSVictor Latushkin 		if (strpbrk(target, "/@") == NULL || dump_opt['R']) {
360480eb36f2SGeorge Wilson 			error = spa_open_rewind(target, &spa, FTAG, policy,
360580eb36f2SGeorge Wilson 			    NULL);
36068f18d1faSGeorge Wilson 			if (error) {
36078f18d1faSGeorge Wilson 				/*
36088f18d1faSGeorge Wilson 				 * If we're missing the log device then
36098f18d1faSGeorge Wilson 				 * try opening the pool after clearing the
36108f18d1faSGeorge Wilson 				 * log state.
36118f18d1faSGeorge Wilson 				 */
36128f18d1faSGeorge Wilson 				mutex_enter(&spa_namespace_lock);
36133ad6c7f9SVictor Latushkin 				if ((spa = spa_lookup(target)) != NULL &&
36148f18d1faSGeorge Wilson 				    spa->spa_log_state == SPA_LOG_MISSING) {
36158f18d1faSGeorge Wilson 					spa->spa_log_state = SPA_LOG_CLEAR;
36168f18d1faSGeorge Wilson 					error = 0;
36178f18d1faSGeorge Wilson 				}
36188f18d1faSGeorge Wilson 				mutex_exit(&spa_namespace_lock);
36198f18d1faSGeorge Wilson 
362080eb36f2SGeorge Wilson 				if (!error) {
362180eb36f2SGeorge Wilson 					error = spa_open_rewind(target, &spa,
362280eb36f2SGeorge Wilson 					    FTAG, policy, NULL);
362380eb36f2SGeorge Wilson 				}
36248f18d1faSGeorge Wilson 			}
362507428bdfSVictor Latushkin 		} else {
362607428bdfSVictor Latushkin 			error = dmu_objset_own(target, DMU_OST_ANY,
362707428bdfSVictor Latushkin 			    B_TRUE, FTAG, &os);
3628c5904d13Seschrock 		}
3629fa9e4066Sahrens 	}
363080eb36f2SGeorge Wilson 	nvlist_free(policy);
363180eb36f2SGeorge Wilson 
3632fa9e4066Sahrens 	if (error)
36333ad6c7f9SVictor Latushkin 		fatal("can't open '%s': %s", target, strerror(error));
3634fa9e4066Sahrens 
3635fa9e4066Sahrens 	argv++;
363607428bdfSVictor Latushkin 	argc--;
363707428bdfSVictor Latushkin 	if (!dump_opt['R']) {
363807428bdfSVictor Latushkin 		if (argc > 0) {
363907428bdfSVictor Latushkin 			zopt_objects = argc;
364007428bdfSVictor Latushkin 			zopt_object = calloc(zopt_objects, sizeof (uint64_t));
364107428bdfSVictor Latushkin 			for (i = 0; i < zopt_objects; i++) {
364207428bdfSVictor Latushkin 				errno = 0;
364307428bdfSVictor Latushkin 				zopt_object[i] = strtoull(argv[i], NULL, 0);
364407428bdfSVictor Latushkin 				if (zopt_object[i] == 0 && errno != 0)
364587219db7SVictor Latushkin 					fatal("bad number %s: %s",
364607428bdfSVictor Latushkin 					    argv[i], strerror(errno));
364707428bdfSVictor Latushkin 			}
3648fa9e4066Sahrens 		}
3649e690fb27SChristopher Siden 		if (os != NULL) {
3650e690fb27SChristopher Siden 			dump_dir(os);
3651e690fb27SChristopher Siden 		} else if (zopt_objects > 0 && !dump_opt['m']) {
3652e690fb27SChristopher Siden 			dump_dir(spa->spa_meta_objset);
3653e690fb27SChristopher Siden 		} else {
3654e690fb27SChristopher Siden 			dump_zpool(spa);
3655e690fb27SChristopher Siden 		}
3656fa9e4066Sahrens 	} else {
365707428bdfSVictor Latushkin 		flagbits['b'] = ZDB_FLAG_PRINT_BLKPTR;
365807428bdfSVictor Latushkin 		flagbits['c'] = ZDB_FLAG_CHECKSUM;
365907428bdfSVictor Latushkin 		flagbits['d'] = ZDB_FLAG_DECOMPRESS;
366007428bdfSVictor Latushkin 		flagbits['e'] = ZDB_FLAG_BSWAP;
366107428bdfSVictor Latushkin 		flagbits['g'] = ZDB_FLAG_GBH;
366207428bdfSVictor Latushkin 		flagbits['i'] = ZDB_FLAG_INDIRECT;
366307428bdfSVictor Latushkin 		flagbits['p'] = ZDB_FLAG_PHYS;
366407428bdfSVictor Latushkin 		flagbits['r'] = ZDB_FLAG_RAW;
366507428bdfSVictor Latushkin 
366607428bdfSVictor Latushkin 		for (i = 0; i < argc; i++)
366707428bdfSVictor Latushkin 			zdb_read_block(argv[i], spa);
3668fa9e4066Sahrens 	}
3669fa9e4066Sahrens 
367007428bdfSVictor Latushkin 	(os != NULL) ? dmu_objset_disown(os, FTAG) : spa_close(spa, FTAG);
367107428bdfSVictor Latushkin 
3672e0d35c44Smarks 	fuid_table_destroy();
36730a586ceaSMark Shellenbaum 	sa_loaded = B_FALSE;
3674e0d35c44Smarks 
3675de6628f0Sck 	libzfs_fini(g_zfs);
3676fa9e4066Sahrens 	kernel_fini();
3677fa9e4066Sahrens 
3678fa9e4066Sahrens 	return (0);
3679fa9e4066Sahrens }
3680