xref: /titanic_51/usr/src/cmd/zdb/zdb.c (revision 7adb730b589e553bf3b1ccfd9bae2df91c5c1061)
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>
62de6628f0Sck153898 #undef ZFS_MAXNAMELEN
63de6628f0Sck153898 #undef verify
64de6628f0Sck153898 #include <libzfs.h>
65fa9e4066Sahrens 
666de8f417SVictor Latushkin #define	ZDB_COMPRESS_NAME(idx) ((idx) < ZIO_COMPRESS_FUNCTIONS ?	\
676de8f417SVictor Latushkin 	zio_compress_table[(idx)].ci_name : "UNKNOWN")
686de8f417SVictor Latushkin #define	ZDB_CHECKSUM_NAME(idx) ((idx) < ZIO_CHECKSUM_FUNCTIONS ?	\
696de8f417SVictor Latushkin 	zio_checksum_table[(idx)].ci_name : "UNKNOWN")
706de8f417SVictor Latushkin #define	ZDB_OT_NAME(idx) ((idx) < DMU_OT_NUMTYPES ?	\
71ad135b5dSChristopher Siden 	dmu_ot[(idx)].ot_name : DMU_OT_IS_VALID(idx) ?	\
72ad135b5dSChristopher 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;
95de6628f0Sck153898 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);
130fa9e4066Sahrens 
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");
138fa9e4066Sahrens 	(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");
143fa9e4066Sahrens 	(void) fprintf(stderr, "        -b block statistics\n");
144d6e555bdSGeorge Wilson 	(void) fprintf(stderr, "        -m metaslabs\n");
1452e4c9986SGeorge Wilson 	(void) fprintf(stderr, "        -M metaslab groups\n");
1466365109dSVictor Latushkin 	(void) fprintf(stderr, "        -c checksum all metadata (twice for "
1476365109dSVictor Latushkin 	    "all data) blocks\n");
148fa9e4066Sahrens 	(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");
151fa9e4066Sahrens 	(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");
15544cd46caSbillm 	(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");
3028248818dSnd150628 
3038248818dSnd150628 	(void) printf("\t\tPointer table:\n");
3048248818dSnd150628 	(void) printf("\t\t\t%llu elements\n",
305fa9e4066Sahrens 	    (u_longlong_t)zs.zs_ptrtbl_len);
3068248818dSnd150628 	(void) printf("\t\t\tzt_blk: %llu\n",
3078248818dSnd150628 	    (u_longlong_t)zs.zs_ptrtbl_zt_blk);
3088248818dSnd150628 	(void) printf("\t\t\tzt_numblks: %llu\n",
3098248818dSnd150628 	    (u_longlong_t)zs.zs_ptrtbl_zt_numblks);
3108248818dSnd150628 	(void) printf("\t\t\tzt_shift: %llu\n",
3118248818dSnd150628 	    (u_longlong_t)zs.zs_ptrtbl_zt_shift);
3128248818dSnd150628 	(void) printf("\t\t\tzt_blks_copied: %llu\n",
3138248818dSnd150628 	    (u_longlong_t)zs.zs_ptrtbl_blks_copied);
3148248818dSnd150628 	(void) printf("\t\t\tzt_nextblk: %llu\n",
3158248818dSnd150628 	    (u_longlong_t)zs.zs_ptrtbl_nextblk);
3168248818dSnd150628 
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);
3238248818dSnd150628 	(void) printf("\t\tzap_block_type: 0x%llx\n",
3248248818dSnd150628 	    (u_longlong_t)zs.zs_block_type);
3258248818dSnd150628 	(void) printf("\t\tzap_magic: 0x%llx\n",
3268248818dSnd150628 	    (u_longlong_t)zs.zs_magic);
3278248818dSnd150628 	(void) printf("\t\tzap_salt: 0x%llx\n",
3288248818dSnd150628 	    (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 
352fa9e4066Sahrens /*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 
3596de8f417SVictor Latushkin /*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 
421e7437265Sahrens /*ARGSUSED*/
422e7437265Sahrens 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 
429486ae710SMatthew Ahrens /*ARGSUSED*/
430486ae710SMatthew Ahrens 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 
4940a586ceaSMark Shellenbaum /*ARGSUSED*/
4950a586ceaSMark Shellenbaum 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;
5978053a263Sck153898 	char *ddata[] = { "ALLOC", "FREE", "CONDENSE", "INVALID",
5988053a263Sck153898 			    "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)],
6185ad82045Snd150628 			    (u_longlong_t)SM_DEBUG_TXG_DECODE(entry),
6195ad82045Snd150628 			    (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',
6255ad82045Snd150628 			    (u_longlong_t)((SM_OFFSET_DECODE(entry) <<
6260713e232SGeorge Wilson 			    mapshift) + sm->sm_start),
6275ad82045Snd150628 			    (u_longlong_t)((SM_OFFSET_DECODE(entry) <<
6280713e232SGeorge Wilson 			    mapshift) + sm->sm_start +
6290713e232SGeorge Wilson 			    (SM_RUN_DECODE(entry) << mapshift)),
6305ad82045Snd150628 			    (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 
644fa9e4066Sahrens 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 
661d6e555bdSGeorge Wilson 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 		}
683d6e555bdSGeorge Wilson 		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));
702fa9e4066Sahrens 
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 }
708d6e555bdSGeorge Wilson 
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 
721fa9e4066Sahrens 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 
7622e4c9986SGeorge Wilson 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];
772fa9e4066Sahrens 
77387219db7SVictor Latushkin 		if (c >= children)
77487219db7SVictor Latushkin 			(void) fatal("bad vdev id: %llu", (u_longlong_t)c);
77587219db7SVictor Latushkin 
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 
804fa9e4066Sahrens 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 
931b24ab676SJeff 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 
9438ad4d6ddSJeff Bonwick 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 	}
1067fa9e4066Sahrens 
1068b24ab676SJeff Bonwick 	ASSERT(zb->zb_level >= 0);
1069b24ab676SJeff Bonwick 
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),
110643466aaeSMax Grossman 		    buflen - strlen(blkbuf), "B=%llu",
110743466aaeSMax Grossman 		    (u_longlong_t)bp->blk_birth);
110843466aaeSMax Grossman 	} else {
110943466aaeSMax Grossman 		(void) snprintf(blkbuf + strlen(blkbuf),
111043466aaeSMax Grossman 		    buflen - strlen(blkbuf),
1111b24ab676SJeff Bonwick 		    "%llxL/%llxP F=%llu B=%llu/%llu",
111244cd46caSbillm 		    (u_longlong_t)BP_GET_LSIZE(bp),
111344cd46caSbillm 		    (u_longlong_t)BP_GET_PSIZE(bp),
11145d7b4d43SMatthew Ahrens 		    (u_longlong_t)BP_GET_FILL(bp),
1115b24ab676SJeff Bonwick 		    (u_longlong_t)bp->blk_birth,
1116b24ab676SJeff Bonwick 		    (u_longlong_t)BP_PHYSICAL_BIRTH(bp));
111744cd46caSbillm 	}
111843466aaeSMax Grossman }
111944cd46caSbillm 
112088b7b0f2SMatthew Ahrens static void
11217802d7bfSMatthew Ahrens print_indirect(blkptr_t *bp, const zbookmark_phys_t *zb,
112288b7b0f2SMatthew Ahrens     const dnode_phys_t *dnp)
1123fa9e4066Sahrens {
112488b7b0f2SMatthew Ahrens 	char blkbuf[BP_SPRINTF_LEN];
1125fa9e4066Sahrens 	int l;
1126fa9e4066Sahrens 
11275d7b4d43SMatthew Ahrens 	if (!BP_IS_EMBEDDED(bp)) {
1128fa9e4066Sahrens 		ASSERT3U(BP_GET_TYPE(bp), ==, dnp->dn_type);
1129fa9e4066Sahrens 		ASSERT3U(BP_GET_LEVEL(bp), ==, zb->zb_level);
11305d7b4d43SMatthew Ahrens 	}
1131fa9e4066Sahrens 
1132b24ab676SJeff Bonwick 	(void) printf("%16llx ", (u_longlong_t)blkid2offset(dnp, bp, zb));
1133fa9e4066Sahrens 
1134fa9e4066Sahrens 	ASSERT(zb->zb_level >= 0);
1135fa9e4066Sahrens 
1136fa9e4066Sahrens 	for (l = dnp->dn_nlevels - 1; l >= -1; l--) {
1137fa9e4066Sahrens 		if (l == zb->zb_level) {
113888b7b0f2SMatthew Ahrens 			(void) printf("L%llx", (u_longlong_t)zb->zb_level);
1139fa9e4066Sahrens 		} else {
114088b7b0f2SMatthew Ahrens 			(void) printf(" ");
1141fa9e4066Sahrens 		}
1142fa9e4066Sahrens 	}
1143fa9e4066Sahrens 
114443466aaeSMax Grossman 	snprintf_blkptr_compact(blkbuf, sizeof (blkbuf), bp);
114544cd46caSbillm 	(void) printf("%s\n", blkbuf);
1146fa9e4066Sahrens }
1147fa9e4066Sahrens 
114888b7b0f2SMatthew Ahrens static int
114988b7b0f2SMatthew Ahrens visit_indirect(spa_t *spa, const dnode_phys_t *dnp,
11507802d7bfSMatthew Ahrens     blkptr_t *bp, const zbookmark_phys_t *zb)
115188b7b0f2SMatthew Ahrens {
1152e4161df6SVictor Latushkin 	int err = 0;
115388b7b0f2SMatthew Ahrens 
115488b7b0f2SMatthew Ahrens 	if (bp->blk_birth == 0)
115588b7b0f2SMatthew Ahrens 		return (0);
115688b7b0f2SMatthew Ahrens 
115788b7b0f2SMatthew Ahrens 	print_indirect(bp, zb, dnp);
115888b7b0f2SMatthew Ahrens 
115943466aaeSMax Grossman 	if (BP_GET_LEVEL(bp) > 0 && !BP_IS_HOLE(bp)) {
1160*7adb730bSGeorge Wilson 		arc_flags_t flags = ARC_FLAG_WAIT;
116188b7b0f2SMatthew Ahrens 		int i;
116288b7b0f2SMatthew Ahrens 		blkptr_t *cbp;
116388b7b0f2SMatthew Ahrens 		int epb = BP_GET_LSIZE(bp) >> SPA_BLKPTRSHIFT;
116488b7b0f2SMatthew Ahrens 		arc_buf_t *buf;
116588b7b0f2SMatthew Ahrens 		uint64_t fill = 0;
116688b7b0f2SMatthew Ahrens 
11671b912ec7SGeorge Wilson 		err = arc_read(NULL, spa, bp, arc_getbuf_func, &buf,
116888b7b0f2SMatthew Ahrens 		    ZIO_PRIORITY_ASYNC_READ, ZIO_FLAG_CANFAIL, &flags, zb);
116988b7b0f2SMatthew Ahrens 		if (err)
117088b7b0f2SMatthew Ahrens 			return (err);
11713f9d6ad7SLin Ling 		ASSERT(buf->b_data);
117288b7b0f2SMatthew Ahrens 
117388b7b0f2SMatthew Ahrens 		/* recursively visit blocks below this */
117488b7b0f2SMatthew Ahrens 		cbp = buf->b_data;
117588b7b0f2SMatthew Ahrens 		for (i = 0; i < epb; i++, cbp++) {
11767802d7bfSMatthew Ahrens 			zbookmark_phys_t czb;
117788b7b0f2SMatthew Ahrens 
117888b7b0f2SMatthew Ahrens 			SET_BOOKMARK(&czb, zb->zb_objset, zb->zb_object,
117988b7b0f2SMatthew Ahrens 			    zb->zb_level - 1,
118088b7b0f2SMatthew Ahrens 			    zb->zb_blkid * epb + i);
118188b7b0f2SMatthew Ahrens 			err = visit_indirect(spa, dnp, cbp, &czb);
118288b7b0f2SMatthew Ahrens 			if (err)
118388b7b0f2SMatthew Ahrens 				break;
11845d7b4d43SMatthew Ahrens 			fill += BP_GET_FILL(cbp);
118588b7b0f2SMatthew Ahrens 		}
11868ad4d6ddSJeff Bonwick 		if (!err)
11875d7b4d43SMatthew Ahrens 			ASSERT3U(fill, ==, BP_GET_FILL(bp));
118888b7b0f2SMatthew Ahrens 		(void) arc_buf_remove_ref(buf, &buf);
118988b7b0f2SMatthew Ahrens 	}
119088b7b0f2SMatthew Ahrens 
119188b7b0f2SMatthew Ahrens 	return (err);
1192fa9e4066Sahrens }
1193fa9e4066Sahrens 
1194fa9e4066Sahrens /*ARGSUSED*/
1195fa9e4066Sahrens static void
119688b7b0f2SMatthew Ahrens dump_indirect(dnode_t *dn)
1197fa9e4066Sahrens {
119888b7b0f2SMatthew Ahrens 	dnode_phys_t *dnp = dn->dn_phys;
119988b7b0f2SMatthew Ahrens 	int j;
12007802d7bfSMatthew Ahrens 	zbookmark_phys_t czb;
1201fa9e4066Sahrens 
1202fa9e4066Sahrens 	(void) printf("Indirect blocks:\n");
1203fa9e4066Sahrens 
1204503ad85cSMatthew Ahrens 	SET_BOOKMARK(&czb, dmu_objset_id(dn->dn_objset),
120588b7b0f2SMatthew Ahrens 	    dn->dn_object, dnp->dn_nlevels - 1, 0);
120688b7b0f2SMatthew Ahrens 	for (j = 0; j < dnp->dn_nblkptr; j++) {
120788b7b0f2SMatthew Ahrens 		czb.zb_blkid = j;
1208503ad85cSMatthew Ahrens 		(void) visit_indirect(dmu_objset_spa(dn->dn_objset), dnp,
120988b7b0f2SMatthew Ahrens 		    &dnp->dn_blkptr[j], &czb);
121088b7b0f2SMatthew Ahrens 	}
1211fa9e4066Sahrens 
1212fa9e4066Sahrens 	(void) printf("\n");
1213fa9e4066Sahrens }
1214fa9e4066Sahrens 
1215fa9e4066Sahrens /*ARGSUSED*/
1216fa9e4066Sahrens static void
1217fa9e4066Sahrens dump_dsl_dir(objset_t *os, uint64_t object, void *data, size_t size)
1218fa9e4066Sahrens {
1219fa9e4066Sahrens 	dsl_dir_phys_t *dd = data;
1220fa9e4066Sahrens 	time_t crtime;
12213f9d6ad7SLin Ling 	char nice[32];
1222fa9e4066Sahrens 
1223fa9e4066Sahrens 	if (dd == NULL)
1224fa9e4066Sahrens 		return;
1225fa9e4066Sahrens 
1226da6c28aaSamw 	ASSERT3U(size, >=, sizeof (dsl_dir_phys_t));
1227fa9e4066Sahrens 
1228fa9e4066Sahrens 	crtime = dd->dd_creation_time;
1229fa9e4066Sahrens 	(void) printf("\t\tcreation_time = %s", ctime(&crtime));
1230fa9e4066Sahrens 	(void) printf("\t\thead_dataset_obj = %llu\n",
1231fa9e4066Sahrens 	    (u_longlong_t)dd->dd_head_dataset_obj);
1232fa9e4066Sahrens 	(void) printf("\t\tparent_dir_obj = %llu\n",
1233fa9e4066Sahrens 	    (u_longlong_t)dd->dd_parent_obj);
12343cb34c60Sahrens 	(void) printf("\t\torigin_obj = %llu\n",
12353cb34c60Sahrens 	    (u_longlong_t)dd->dd_origin_obj);
1236fa9e4066Sahrens 	(void) printf("\t\tchild_dir_zapobj = %llu\n",
1237fa9e4066Sahrens 	    (u_longlong_t)dd->dd_child_dir_zapobj);
12383f9d6ad7SLin Ling 	zdb_nicenum(dd->dd_used_bytes, nice);
123974e7dc98SMatthew Ahrens 	(void) printf("\t\tused_bytes = %s\n", nice);
12403f9d6ad7SLin Ling 	zdb_nicenum(dd->dd_compressed_bytes, nice);
124174e7dc98SMatthew Ahrens 	(void) printf("\t\tcompressed_bytes = %s\n", nice);
12423f9d6ad7SLin Ling 	zdb_nicenum(dd->dd_uncompressed_bytes, nice);
124374e7dc98SMatthew Ahrens 	(void) printf("\t\tuncompressed_bytes = %s\n", nice);
12443f9d6ad7SLin Ling 	zdb_nicenum(dd->dd_quota, nice);
124574e7dc98SMatthew Ahrens 	(void) printf("\t\tquota = %s\n", nice);
12463f9d6ad7SLin Ling 	zdb_nicenum(dd->dd_reserved, nice);
124774e7dc98SMatthew Ahrens 	(void) printf("\t\treserved = %s\n", nice);
1248fa9e4066Sahrens 	(void) printf("\t\tprops_zapobj = %llu\n",
1249fa9e4066Sahrens 	    (u_longlong_t)dd->dd_props_zapobj);
1250ecd6cf80Smarks 	(void) printf("\t\tdeleg_zapobj = %llu\n",
1251ecd6cf80Smarks 	    (u_longlong_t)dd->dd_deleg_zapobj);
125274e7dc98SMatthew Ahrens 	(void) printf("\t\tflags = %llx\n",
125374e7dc98SMatthew Ahrens 	    (u_longlong_t)dd->dd_flags);
125474e7dc98SMatthew Ahrens 
125574e7dc98SMatthew Ahrens #define	DO(which) \
12563f9d6ad7SLin Ling 	zdb_nicenum(dd->dd_used_breakdown[DD_USED_ ## which], nice); \
125774e7dc98SMatthew Ahrens 	(void) printf("\t\tused_breakdown[" #which "] = %s\n", nice)
125874e7dc98SMatthew Ahrens 	DO(HEAD);
125974e7dc98SMatthew Ahrens 	DO(SNAP);
126074e7dc98SMatthew Ahrens 	DO(CHILD);
126174e7dc98SMatthew Ahrens 	DO(CHILD_RSRV);
126274e7dc98SMatthew Ahrens 	DO(REFRSRV);
126374e7dc98SMatthew Ahrens #undef DO
1264fa9e4066Sahrens }
1265fa9e4066Sahrens 
1266fa9e4066Sahrens /*ARGSUSED*/
1267fa9e4066Sahrens static void
1268fa9e4066Sahrens dump_dsl_dataset(objset_t *os, uint64_t object, void *data, size_t size)
1269fa9e4066Sahrens {
1270fa9e4066Sahrens 	dsl_dataset_phys_t *ds = data;
1271fa9e4066Sahrens 	time_t crtime;
12723f9d6ad7SLin Ling 	char used[32], compressed[32], uncompressed[32], unique[32];
1273fbabab8fSmaybee 	char blkbuf[BP_SPRINTF_LEN];
1274fa9e4066Sahrens 
1275fa9e4066Sahrens 	if (ds == NULL)
1276fa9e4066Sahrens 		return;
1277fa9e4066Sahrens 
1278fa9e4066Sahrens 	ASSERT(size == sizeof (*ds));
1279fa9e4066Sahrens 	crtime = ds->ds_creation_time;
1280ad135b5dSChristopher Siden 	zdb_nicenum(ds->ds_referenced_bytes, used);
12813f9d6ad7SLin Ling 	zdb_nicenum(ds->ds_compressed_bytes, compressed);
12823f9d6ad7SLin Ling 	zdb_nicenum(ds->ds_uncompressed_bytes, uncompressed);
12833f9d6ad7SLin Ling 	zdb_nicenum(ds->ds_unique_bytes, unique);
128443466aaeSMax Grossman 	snprintf_blkptr(blkbuf, sizeof (blkbuf), &ds->ds_bp);
1285fa9e4066Sahrens 
1286088f3894Sahrens 	(void) printf("\t\tdir_obj = %llu\n",
1287fa9e4066Sahrens 	    (u_longlong_t)ds->ds_dir_obj);
1288fa9e4066Sahrens 	(void) printf("\t\tprev_snap_obj = %llu\n",
1289fa9e4066Sahrens 	    (u_longlong_t)ds->ds_prev_snap_obj);
1290fa9e4066Sahrens 	(void) printf("\t\tprev_snap_txg = %llu\n",
1291fa9e4066Sahrens 	    (u_longlong_t)ds->ds_prev_snap_txg);
1292fa9e4066Sahrens 	(void) printf("\t\tnext_snap_obj = %llu\n",
1293fa9e4066Sahrens 	    (u_longlong_t)ds->ds_next_snap_obj);
1294fa9e4066Sahrens 	(void) printf("\t\tsnapnames_zapobj = %llu\n",
1295fa9e4066Sahrens 	    (u_longlong_t)ds->ds_snapnames_zapobj);
1296fa9e4066Sahrens 	(void) printf("\t\tnum_children = %llu\n",
1297fa9e4066Sahrens 	    (u_longlong_t)ds->ds_num_children);
1298842727c2SChris Kirby 	(void) printf("\t\tuserrefs_obj = %llu\n",
1299842727c2SChris Kirby 	    (u_longlong_t)ds->ds_userrefs_obj);
1300fa9e4066Sahrens 	(void) printf("\t\tcreation_time = %s", ctime(&crtime));
1301fa9e4066Sahrens 	(void) printf("\t\tcreation_txg = %llu\n",
1302fa9e4066Sahrens 	    (u_longlong_t)ds->ds_creation_txg);
1303fa9e4066Sahrens 	(void) printf("\t\tdeadlist_obj = %llu\n",
1304fa9e4066Sahrens 	    (u_longlong_t)ds->ds_deadlist_obj);
1305fa9e4066Sahrens 	(void) printf("\t\tused_bytes = %s\n", used);
1306fa9e4066Sahrens 	(void) printf("\t\tcompressed_bytes = %s\n", compressed);
1307fa9e4066Sahrens 	(void) printf("\t\tuncompressed_bytes = %s\n", uncompressed);
1308fa9e4066Sahrens 	(void) printf("\t\tunique = %s\n", unique);
1309fa9e4066Sahrens 	(void) printf("\t\tfsid_guid = %llu\n",
1310fa9e4066Sahrens 	    (u_longlong_t)ds->ds_fsid_guid);
1311fa9e4066Sahrens 	(void) printf("\t\tguid = %llu\n",
1312fa9e4066Sahrens 	    (u_longlong_t)ds->ds_guid);
131399653d4eSeschrock 	(void) printf("\t\tflags = %llx\n",
131499653d4eSeschrock 	    (u_longlong_t)ds->ds_flags);
1315088f3894Sahrens 	(void) printf("\t\tnext_clones_obj = %llu\n",
1316088f3894Sahrens 	    (u_longlong_t)ds->ds_next_clones_obj);
1317bb0ade09Sahrens 	(void) printf("\t\tprops_obj = %llu\n",
1318bb0ade09Sahrens 	    (u_longlong_t)ds->ds_props_obj);
1319fa9e4066Sahrens 	(void) printf("\t\tbp = %s\n", blkbuf);
1320fa9e4066Sahrens }
1321fa9e4066Sahrens 
1322cde58dbcSMatthew Ahrens /* ARGSUSED */
1323cde58dbcSMatthew Ahrens static int
1324ad135b5dSChristopher Siden dump_bptree_cb(void *arg, const blkptr_t *bp, dmu_tx_t *tx)
1325ad135b5dSChristopher Siden {
1326ad135b5dSChristopher Siden 	char blkbuf[BP_SPRINTF_LEN];
1327ad135b5dSChristopher Siden 
1328ad135b5dSChristopher Siden 	if (bp->blk_birth != 0) {
132943466aaeSMax Grossman 		snprintf_blkptr(blkbuf, sizeof (blkbuf), bp);
1330ad135b5dSChristopher Siden 		(void) printf("\t%s\n", blkbuf);
1331ad135b5dSChristopher Siden 	}
1332ad135b5dSChristopher Siden 	return (0);
1333ad135b5dSChristopher Siden }
1334ad135b5dSChristopher Siden 
1335ad135b5dSChristopher Siden static void
1336ad135b5dSChristopher Siden dump_bptree(objset_t *os, uint64_t obj, char *name)
1337ad135b5dSChristopher Siden {
1338ad135b5dSChristopher Siden 	char bytes[32];
1339ad135b5dSChristopher Siden 	bptree_phys_t *bt;
1340ad135b5dSChristopher Siden 	dmu_buf_t *db;
1341ad135b5dSChristopher Siden 
1342ad135b5dSChristopher Siden 	if (dump_opt['d'] < 3)
1343ad135b5dSChristopher Siden 		return;
1344ad135b5dSChristopher Siden 
1345b420f3adSRichard Lowe 	VERIFY3U(0, ==, dmu_bonus_hold(os, obj, FTAG, &db));
1346ad135b5dSChristopher Siden 	bt = db->db_data;
1347ad135b5dSChristopher Siden 	zdb_nicenum(bt->bt_bytes, bytes);
1348ad135b5dSChristopher Siden 	(void) printf("\n    %s: %llu datasets, %s\n",
1349ad135b5dSChristopher Siden 	    name, (unsigned long long)(bt->bt_end - bt->bt_begin), bytes);
1350ad135b5dSChristopher Siden 	dmu_buf_rele(db, FTAG);
1351ad135b5dSChristopher Siden 
1352ad135b5dSChristopher Siden 	if (dump_opt['d'] < 5)
1353ad135b5dSChristopher Siden 		return;
1354ad135b5dSChristopher Siden 
1355ad135b5dSChristopher Siden 	(void) printf("\n");
1356ad135b5dSChristopher Siden 
1357ad135b5dSChristopher Siden 	(void) bptree_iterate(os, obj, B_FALSE, dump_bptree_cb, NULL, NULL);
1358ad135b5dSChristopher Siden }
1359ad135b5dSChristopher Siden 
1360ad135b5dSChristopher Siden /* ARGSUSED */
1361ad135b5dSChristopher Siden static int
1362cde58dbcSMatthew Ahrens dump_bpobj_cb(void *arg, const blkptr_t *bp, dmu_tx_t *tx)
1363fa9e4066Sahrens {
1364cde58dbcSMatthew Ahrens 	char blkbuf[BP_SPRINTF_LEN];
1365cde58dbcSMatthew Ahrens 
1366cde58dbcSMatthew Ahrens 	ASSERT(bp->blk_birth != 0);
136743466aaeSMax Grossman 	snprintf_blkptr_compact(blkbuf, sizeof (blkbuf), bp);
1368cde58dbcSMatthew Ahrens 	(void) printf("\t%s\n", blkbuf);
1369cde58dbcSMatthew Ahrens 	return (0);
1370cde58dbcSMatthew Ahrens }
1371cde58dbcSMatthew Ahrens 
1372cde58dbcSMatthew Ahrens static void
1373d0475637SMatthew Ahrens dump_bpobj(bpobj_t *bpo, char *name, int indent)
1374cde58dbcSMatthew Ahrens {
13753f9d6ad7SLin Ling 	char bytes[32];
13763f9d6ad7SLin Ling 	char comp[32];
13773f9d6ad7SLin Ling 	char uncomp[32];
1378fa9e4066Sahrens 
1379fa9e4066Sahrens 	if (dump_opt['d'] < 3)
1380fa9e4066Sahrens 		return;
1381fa9e4066Sahrens 
1382cde58dbcSMatthew Ahrens 	zdb_nicenum(bpo->bpo_phys->bpo_bytes, bytes);
1383d0475637SMatthew Ahrens 	if (bpo->bpo_havesubobj && bpo->bpo_phys->bpo_subobjs != 0) {
1384cde58dbcSMatthew Ahrens 		zdb_nicenum(bpo->bpo_phys->bpo_comp, comp);
1385cde58dbcSMatthew Ahrens 		zdb_nicenum(bpo->bpo_phys->bpo_uncomp, uncomp);
1386d0475637SMatthew Ahrens 		(void) printf("    %*s: object %llu, %llu local blkptrs, "
1387d0475637SMatthew Ahrens 		    "%llu subobjs, %s (%s/%s comp)\n",
1388d0475637SMatthew Ahrens 		    indent * 8, name,
1389d0475637SMatthew Ahrens 		    (u_longlong_t)bpo->bpo_object,
1390d0475637SMatthew Ahrens 		    (u_longlong_t)bpo->bpo_phys->bpo_num_blkptrs,
1391cde58dbcSMatthew Ahrens 		    (u_longlong_t)bpo->bpo_phys->bpo_num_subobjs,
139299653d4eSeschrock 		    bytes, comp, uncomp);
1393d0475637SMatthew Ahrens 
1394d0475637SMatthew Ahrens 		for (uint64_t i = 0; i < bpo->bpo_phys->bpo_num_subobjs; i++) {
1395d0475637SMatthew Ahrens 			uint64_t subobj;
1396d0475637SMatthew Ahrens 			bpobj_t subbpo;
1397d0475637SMatthew Ahrens 			int error;
1398d0475637SMatthew Ahrens 			VERIFY0(dmu_read(bpo->bpo_os,
1399d0475637SMatthew Ahrens 			    bpo->bpo_phys->bpo_subobjs,
1400d0475637SMatthew Ahrens 			    i * sizeof (subobj), sizeof (subobj), &subobj, 0));
1401d0475637SMatthew Ahrens 			error = bpobj_open(&subbpo, bpo->bpo_os, subobj);
1402d0475637SMatthew Ahrens 			if (error != 0) {
1403d0475637SMatthew Ahrens 				(void) printf("ERROR %u while trying to open "
1404d0475637SMatthew Ahrens 				    "subobj id %llu\n",
1405d0475637SMatthew Ahrens 				    error, (u_longlong_t)subobj);
1406d0475637SMatthew Ahrens 				continue;
1407d0475637SMatthew Ahrens 			}
1408d0475637SMatthew Ahrens 			dump_bpobj(&subbpo, "subobj", indent + 1);
140977061867SMatthew Ahrens 			bpobj_close(&subbpo);
1410d0475637SMatthew Ahrens 		}
141199653d4eSeschrock 	} else {
1412d0475637SMatthew Ahrens 		(void) printf("    %*s: object %llu, %llu blkptrs, %s\n",
1413d0475637SMatthew Ahrens 		    indent * 8, name,
1414d0475637SMatthew Ahrens 		    (u_longlong_t)bpo->bpo_object,
1415d0475637SMatthew Ahrens 		    (u_longlong_t)bpo->bpo_phys->bpo_num_blkptrs,
1416d0475637SMatthew Ahrens 		    bytes);
141799653d4eSeschrock 	}
1418fa9e4066Sahrens 
1419cde58dbcSMatthew Ahrens 	if (dump_opt['d'] < 5)
1420fa9e4066Sahrens 		return;
1421fa9e4066Sahrens 
1422fa9e4066Sahrens 
1423d0475637SMatthew Ahrens 	if (indent == 0) {
1424cde58dbcSMatthew Ahrens 		(void) bpobj_iterate_nofree(bpo, dump_bpobj_cb, NULL, NULL);
1425d0475637SMatthew Ahrens 		(void) printf("\n");
1426d0475637SMatthew Ahrens 	}
1427fa9e4066Sahrens }
1428fa9e4066Sahrens 
1429cde58dbcSMatthew Ahrens static void
1430cde58dbcSMatthew Ahrens dump_deadlist(dsl_deadlist_t *dl)
1431cde58dbcSMatthew Ahrens {
1432cde58dbcSMatthew Ahrens 	dsl_deadlist_entry_t *dle;
1433d0475637SMatthew Ahrens 	uint64_t unused;
1434cde58dbcSMatthew Ahrens 	char bytes[32];
1435cde58dbcSMatthew Ahrens 	char comp[32];
1436cde58dbcSMatthew Ahrens 	char uncomp[32];
1437cde58dbcSMatthew Ahrens 
1438cde58dbcSMatthew Ahrens 	if (dump_opt['d'] < 3)
1439cde58dbcSMatthew Ahrens 		return;
1440cde58dbcSMatthew Ahrens 
144190c76c66SMatthew Ahrens 	if (dl->dl_oldfmt) {
144290c76c66SMatthew Ahrens 		dump_bpobj(&dl->dl_bpobj, "old-format deadlist", 0);
144390c76c66SMatthew Ahrens 		return;
144490c76c66SMatthew Ahrens 	}
144590c76c66SMatthew Ahrens 
1446cde58dbcSMatthew Ahrens 	zdb_nicenum(dl->dl_phys->dl_used, bytes);
1447cde58dbcSMatthew Ahrens 	zdb_nicenum(dl->dl_phys->dl_comp, comp);
1448cde58dbcSMatthew Ahrens 	zdb_nicenum(dl->dl_phys->dl_uncomp, uncomp);
1449cde58dbcSMatthew Ahrens 	(void) printf("\n    Deadlist: %s (%s/%s comp)\n",
1450cde58dbcSMatthew Ahrens 	    bytes, comp, uncomp);
1451cde58dbcSMatthew Ahrens 
1452cde58dbcSMatthew Ahrens 	if (dump_opt['d'] < 4)
1453cde58dbcSMatthew Ahrens 		return;
1454cde58dbcSMatthew Ahrens 
1455cde58dbcSMatthew Ahrens 	(void) printf("\n");
1456cde58dbcSMatthew Ahrens 
1457d0475637SMatthew Ahrens 	/* force the tree to be loaded */
1458d0475637SMatthew Ahrens 	dsl_deadlist_space_range(dl, 0, UINT64_MAX, &unused, &unused, &unused);
1459d0475637SMatthew Ahrens 
1460cde58dbcSMatthew Ahrens 	for (dle = avl_first(&dl->dl_tree); dle;
1461cde58dbcSMatthew Ahrens 	    dle = AVL_NEXT(&dl->dl_tree, dle)) {
1462d0475637SMatthew Ahrens 		if (dump_opt['d'] >= 5) {
1463d0475637SMatthew Ahrens 			char buf[128];
1464d0475637SMatthew Ahrens 			(void) snprintf(buf, sizeof (buf), "mintxg %llu -> ",
1465d0475637SMatthew Ahrens 			    (longlong_t)dle->dle_mintxg,
1466d0475637SMatthew Ahrens 			    (longlong_t)dle->dle_bpobj.bpo_object);
1467d0475637SMatthew Ahrens 
1468d0475637SMatthew Ahrens 			dump_bpobj(&dle->dle_bpobj, buf, 0);
1469d0475637SMatthew Ahrens 		} else {
1470cde58dbcSMatthew Ahrens 			(void) printf("mintxg %llu -> obj %llu\n",
1471cde58dbcSMatthew Ahrens 			    (longlong_t)dle->dle_mintxg,
1472cde58dbcSMatthew Ahrens 			    (longlong_t)dle->dle_bpobj.bpo_object);
1473cde58dbcSMatthew Ahrens 
1474d0475637SMatthew Ahrens 		}
1475cde58dbcSMatthew Ahrens 	}
1476fa9e4066Sahrens }
1477fa9e4066Sahrens 
1478e0d35c44Smarks static avl_tree_t idx_tree;
1479e0d35c44Smarks static avl_tree_t domain_tree;
1480e0d35c44Smarks static boolean_t fuid_table_loaded;
14810a586ceaSMark Shellenbaum static boolean_t sa_loaded;
14820a586ceaSMark Shellenbaum sa_attr_type_t *sa_attr_table;
1483e0d35c44Smarks 
1484e0d35c44Smarks static void
1485e0d35c44Smarks fuid_table_destroy()
1486e0d35c44Smarks {
1487e0d35c44Smarks 	if (fuid_table_loaded) {
1488e0d35c44Smarks 		zfs_fuid_table_destroy(&idx_tree, &domain_tree);
1489e0d35c44Smarks 		fuid_table_loaded = B_FALSE;
1490e0d35c44Smarks 	}
1491e0d35c44Smarks }
1492e0d35c44Smarks 
1493e0d35c44Smarks /*
1494e0d35c44Smarks  * print uid or gid information.
1495e0d35c44Smarks  * For normal POSIX id just the id is printed in decimal format.
1496e0d35c44Smarks  * For CIFS files with FUID the fuid is printed in hex followed by
1497d0475637SMatthew Ahrens  * the domain-rid string.
1498e0d35c44Smarks  */
1499e0d35c44Smarks static void
1500e0d35c44Smarks print_idstr(uint64_t id, const char *id_type)
1501e0d35c44Smarks {
1502e0d35c44Smarks 	if (FUID_INDEX(id)) {
1503e0d35c44Smarks 		char *domain;
1504e0d35c44Smarks 
1505e0d35c44Smarks 		domain = zfs_fuid_idx_domain(&idx_tree, FUID_INDEX(id));
1506e0d35c44Smarks 		(void) printf("\t%s     %llx [%s-%d]\n", id_type,
1507e0d35c44Smarks 		    (u_longlong_t)id, domain, (int)FUID_RID(id));
1508e0d35c44Smarks 	} else {
1509e0d35c44Smarks 		(void) printf("\t%s     %llu\n", id_type, (u_longlong_t)id);
1510e0d35c44Smarks 	}
1511e0d35c44Smarks 
1512e0d35c44Smarks }
1513e0d35c44Smarks 
1514e0d35c44Smarks static void
15150a586ceaSMark Shellenbaum dump_uidgid(objset_t *os, uint64_t uid, uint64_t gid)
1516e0d35c44Smarks {
1517e0d35c44Smarks 	uint32_t uid_idx, gid_idx;
1518e0d35c44Smarks 
15190a586ceaSMark Shellenbaum 	uid_idx = FUID_INDEX(uid);
15200a586ceaSMark Shellenbaum 	gid_idx = FUID_INDEX(gid);
1521e0d35c44Smarks 
1522e0d35c44Smarks 	/* Load domain table, if not already loaded */
1523e0d35c44Smarks 	if (!fuid_table_loaded && (uid_idx || gid_idx)) {
1524e0d35c44Smarks 		uint64_t fuid_obj;
1525e0d35c44Smarks 
1526e0d35c44Smarks 		/* first find the fuid object.  It lives in the master node */
1527e0d35c44Smarks 		VERIFY(zap_lookup(os, MASTER_NODE_OBJ, ZFS_FUID_TABLES,
1528e0d35c44Smarks 		    8, 1, &fuid_obj) == 0);
152989459e17SMark Shellenbaum 		zfs_fuid_avl_tree_create(&idx_tree, &domain_tree);
1530e0d35c44Smarks 		(void) zfs_fuid_table_load(os, fuid_obj,
1531e0d35c44Smarks 		    &idx_tree, &domain_tree);
1532e0d35c44Smarks 		fuid_table_loaded = B_TRUE;
1533e0d35c44Smarks 	}
1534e0d35c44Smarks 
15350a586ceaSMark Shellenbaum 	print_idstr(uid, "uid");
15360a586ceaSMark Shellenbaum 	print_idstr(gid, "gid");
1537e0d35c44Smarks }
1538e0d35c44Smarks 
1539fa9e4066Sahrens /*ARGSUSED*/
1540fa9e4066Sahrens static void
1541fa9e4066Sahrens dump_znode(objset_t *os, uint64_t object, void *data, size_t size)
1542fa9e4066Sahrens {
1543fa9e4066Sahrens 	char path[MAXPATHLEN * 2];	/* allow for xattr and failure prefix */
15440a586ceaSMark Shellenbaum 	sa_handle_t *hdl;
15450a586ceaSMark Shellenbaum 	uint64_t xattr, rdev, gen;
15460a586ceaSMark Shellenbaum 	uint64_t uid, gid, mode, fsize, parent, links;
15478f2529deSMark Shellenbaum 	uint64_t pflags;
15480a586ceaSMark Shellenbaum 	uint64_t acctm[2], modtm[2], chgtm[2], crtm[2];
15490a586ceaSMark Shellenbaum 	time_t z_crtime, z_atime, z_mtime, z_ctime;
15508f2529deSMark Shellenbaum 	sa_bulk_attr_t bulk[12];
15510a586ceaSMark Shellenbaum 	int idx = 0;
155255434c77Sek110237 	int error;
1553fa9e4066Sahrens 
15540a586ceaSMark Shellenbaum 	if (!sa_loaded) {
15550a586ceaSMark Shellenbaum 		uint64_t sa_attrs = 0;
15560a586ceaSMark Shellenbaum 		uint64_t version;
15570a586ceaSMark Shellenbaum 
15580a586ceaSMark Shellenbaum 		VERIFY(zap_lookup(os, MASTER_NODE_OBJ, ZPL_VERSION_STR,
15590a586ceaSMark Shellenbaum 		    8, 1, &version) == 0);
15600a586ceaSMark Shellenbaum 		if (version >= ZPL_VERSION_SA) {
15610a586ceaSMark Shellenbaum 			VERIFY(zap_lookup(os, MASTER_NODE_OBJ, ZFS_SA_ATTRS,
15620a586ceaSMark Shellenbaum 			    8, 1, &sa_attrs) == 0);
15630a586ceaSMark Shellenbaum 		}
15641d8ccc7bSMark Shellenbaum 		if ((error = sa_setup(os, sa_attrs, zfs_attr_table,
15651d8ccc7bSMark Shellenbaum 		    ZPL_END, &sa_attr_table)) != 0) {
15661d8ccc7bSMark Shellenbaum 			(void) printf("sa_setup failed errno %d, can't "
15671d8ccc7bSMark Shellenbaum 			    "display znode contents\n", error);
15681d8ccc7bSMark Shellenbaum 			return;
15691d8ccc7bSMark Shellenbaum 		}
15700a586ceaSMark Shellenbaum 		sa_loaded = B_TRUE;
15710a586ceaSMark Shellenbaum 	}
15720a586ceaSMark Shellenbaum 
15730a586ceaSMark Shellenbaum 	if (sa_handle_get(os, object, NULL, SA_HDL_PRIVATE, &hdl)) {
15740a586ceaSMark Shellenbaum 		(void) printf("Failed to get handle for SA znode\n");
15750a586ceaSMark Shellenbaum 		return;
15760a586ceaSMark Shellenbaum 	}
15770a586ceaSMark Shellenbaum 
15780a586ceaSMark Shellenbaum 	SA_ADD_BULK_ATTR(bulk, idx, sa_attr_table[ZPL_UID], NULL, &uid, 8);
15790a586ceaSMark Shellenbaum 	SA_ADD_BULK_ATTR(bulk, idx, sa_attr_table[ZPL_GID], NULL, &gid, 8);
15800a586ceaSMark Shellenbaum 	SA_ADD_BULK_ATTR(bulk, idx, sa_attr_table[ZPL_LINKS], NULL,
15810a586ceaSMark Shellenbaum 	    &links, 8);
15820a586ceaSMark Shellenbaum 	SA_ADD_BULK_ATTR(bulk, idx, sa_attr_table[ZPL_GEN], NULL, &gen, 8);
15830a586ceaSMark Shellenbaum 	SA_ADD_BULK_ATTR(bulk, idx, sa_attr_table[ZPL_MODE], NULL,
15840a586ceaSMark Shellenbaum 	    &mode, 8);
15850a586ceaSMark Shellenbaum 	SA_ADD_BULK_ATTR(bulk, idx, sa_attr_table[ZPL_PARENT],
15860a586ceaSMark Shellenbaum 	    NULL, &parent, 8);
15870a586ceaSMark Shellenbaum 	SA_ADD_BULK_ATTR(bulk, idx, sa_attr_table[ZPL_SIZE], NULL,
15880a586ceaSMark Shellenbaum 	    &fsize, 8);
15890a586ceaSMark Shellenbaum 	SA_ADD_BULK_ATTR(bulk, idx, sa_attr_table[ZPL_ATIME], NULL,
15900a586ceaSMark Shellenbaum 	    acctm, 16);
15910a586ceaSMark Shellenbaum 	SA_ADD_BULK_ATTR(bulk, idx, sa_attr_table[ZPL_MTIME], NULL,
15920a586ceaSMark Shellenbaum 	    modtm, 16);
15930a586ceaSMark Shellenbaum 	SA_ADD_BULK_ATTR(bulk, idx, sa_attr_table[ZPL_CRTIME], NULL,
15940a586ceaSMark Shellenbaum 	    crtm, 16);
15950a586ceaSMark Shellenbaum 	SA_ADD_BULK_ATTR(bulk, idx, sa_attr_table[ZPL_CTIME], NULL,
15960a586ceaSMark Shellenbaum 	    chgtm, 16);
15978f2529deSMark Shellenbaum 	SA_ADD_BULK_ATTR(bulk, idx, sa_attr_table[ZPL_FLAGS], NULL,
15988f2529deSMark Shellenbaum 	    &pflags, 8);
15990a586ceaSMark Shellenbaum 
16000a586ceaSMark Shellenbaum 	if (sa_bulk_lookup(hdl, bulk, idx)) {
16010a586ceaSMark Shellenbaum 		(void) sa_handle_destroy(hdl);
16020a586ceaSMark Shellenbaum 		return;
16030a586ceaSMark Shellenbaum 	}
1604fa9e4066Sahrens 
160555434c77Sek110237 	error = zfs_obj_to_path(os, object, path, sizeof (path));
160655434c77Sek110237 	if (error != 0) {
160755434c77Sek110237 		(void) snprintf(path, sizeof (path), "\?\?\?<object#%llu>",
160855434c77Sek110237 		    (u_longlong_t)object);
160955434c77Sek110237 	}
1610fa9e4066Sahrens 	if (dump_opt['d'] < 3) {
161155434c77Sek110237 		(void) printf("\t%s\n", path);
16120a586ceaSMark Shellenbaum 		(void) sa_handle_destroy(hdl);
1613fa9e4066Sahrens 		return;
1614fa9e4066Sahrens 	}
1615fa9e4066Sahrens 
16160a586ceaSMark Shellenbaum 	z_crtime = (time_t)crtm[0];
16170a586ceaSMark Shellenbaum 	z_atime = (time_t)acctm[0];
16180a586ceaSMark Shellenbaum 	z_mtime = (time_t)modtm[0];
16190a586ceaSMark Shellenbaum 	z_ctime = (time_t)chgtm[0];
1620fa9e4066Sahrens 
162155434c77Sek110237 	(void) printf("\tpath	%s\n", path);
16220a586ceaSMark Shellenbaum 	dump_uidgid(os, uid, gid);
1623fa9e4066Sahrens 	(void) printf("\tatime	%s", ctime(&z_atime));
1624fa9e4066Sahrens 	(void) printf("\tmtime	%s", ctime(&z_mtime));
1625fa9e4066Sahrens 	(void) printf("\tctime	%s", ctime(&z_ctime));
1626fa9e4066Sahrens 	(void) printf("\tcrtime	%s", ctime(&z_crtime));
16270a586ceaSMark Shellenbaum 	(void) printf("\tgen	%llu\n", (u_longlong_t)gen);
16280a586ceaSMark Shellenbaum 	(void) printf("\tmode	%llo\n", (u_longlong_t)mode);
16290a586ceaSMark Shellenbaum 	(void) printf("\tsize	%llu\n", (u_longlong_t)fsize);
16300a586ceaSMark Shellenbaum 	(void) printf("\tparent	%llu\n", (u_longlong_t)parent);
16310a586ceaSMark Shellenbaum 	(void) printf("\tlinks	%llu\n", (u_longlong_t)links);
16328f2529deSMark Shellenbaum 	(void) printf("\tpflags	%llx\n", (u_longlong_t)pflags);
16330a586ceaSMark Shellenbaum 	if (sa_lookup(hdl, sa_attr_table[ZPL_XATTR], &xattr,
16340a586ceaSMark Shellenbaum 	    sizeof (uint64_t)) == 0)
16350a586ceaSMark Shellenbaum 		(void) printf("\txattr	%llu\n", (u_longlong_t)xattr);
16360a586ceaSMark Shellenbaum 	if (sa_lookup(hdl, sa_attr_table[ZPL_RDEV], &rdev,
16370a586ceaSMark Shellenbaum 	    sizeof (uint64_t)) == 0)
16380a586ceaSMark Shellenbaum 		(void) printf("\trdev	0x%016llx\n", (u_longlong_t)rdev);
16390a586ceaSMark Shellenbaum 	sa_handle_destroy(hdl);
1640fa9e4066Sahrens }
1641fa9e4066Sahrens 
1642fa9e4066Sahrens /*ARGSUSED*/
1643fa9e4066Sahrens static void
1644fa9e4066Sahrens dump_acl(objset_t *os, uint64_t object, void *data, size_t size)
1645fa9e4066Sahrens {
1646fa9e4066Sahrens }
1647fa9e4066Sahrens 
1648fa9e4066Sahrens /*ARGSUSED*/
1649fa9e4066Sahrens static void
1650fa9e4066Sahrens dump_dmu_objset(objset_t *os, uint64_t object, void *data, size_t size)
1651fa9e4066Sahrens {
1652fa9e4066Sahrens }
1653fa9e4066Sahrens 
16546de8f417SVictor Latushkin static object_viewer_t *object_viewer[DMU_OT_NUMTYPES + 1] = {
1655fa9e4066Sahrens 	dump_none,		/* unallocated			*/
1656fa9e4066Sahrens 	dump_zap,		/* object directory		*/
1657fa9e4066Sahrens 	dump_uint64,		/* object array			*/
1658fa9e4066Sahrens 	dump_none,		/* packed nvlist		*/
1659fa9e4066Sahrens 	dump_packed_nvlist,	/* packed nvlist size		*/
1660fa9e4066Sahrens 	dump_none,		/* bplist			*/
1661fa9e4066Sahrens 	dump_none,		/* bplist header		*/
1662fa9e4066Sahrens 	dump_none,		/* SPA space map header		*/
1663fa9e4066Sahrens 	dump_none,		/* SPA space map		*/
1664fa9e4066Sahrens 	dump_none,		/* ZIL intent log		*/
1665fa9e4066Sahrens 	dump_dnode,		/* DMU dnode			*/
1666fa9e4066Sahrens 	dump_dmu_objset,	/* DMU objset			*/
1667fa9e4066Sahrens 	dump_dsl_dir,		/* DSL directory		*/
1668fa9e4066Sahrens 	dump_zap,		/* DSL directory child map	*/
1669fa9e4066Sahrens 	dump_zap,		/* DSL dataset snap map		*/
1670fa9e4066Sahrens 	dump_zap,		/* DSL props			*/
1671fa9e4066Sahrens 	dump_dsl_dataset,	/* DSL dataset			*/
1672fa9e4066Sahrens 	dump_znode,		/* ZFS znode			*/
1673da6c28aaSamw 	dump_acl,		/* ZFS V0 ACL			*/
1674fa9e4066Sahrens 	dump_uint8,		/* ZFS plain file		*/
1675e7437265Sahrens 	dump_zpldir,		/* ZFS directory		*/
1676fa9e4066Sahrens 	dump_zap,		/* ZFS master node		*/
1677fa9e4066Sahrens 	dump_zap,		/* ZFS delete queue		*/
1678fa9e4066Sahrens 	dump_uint8,		/* zvol object			*/
1679fa9e4066Sahrens 	dump_zap,		/* zvol prop			*/
1680fa9e4066Sahrens 	dump_uint8,		/* other uint8[]		*/
1681fa9e4066Sahrens 	dump_uint64,		/* other uint64[]		*/
1682fa9e4066Sahrens 	dump_zap,		/* other ZAP			*/
1683ea8dc4b6Seschrock 	dump_zap,		/* persistent error log		*/
168406eeb2adSek110237 	dump_uint8,		/* SPA history			*/
16854445fffbSMatthew Ahrens 	dump_history_offsets,	/* SPA history offsets		*/
1686b1b8ab34Slling 	dump_zap,		/* Pool properties		*/
1687ecd6cf80Smarks 	dump_zap,		/* DSL permissions		*/
1688da6c28aaSamw 	dump_acl,		/* ZFS ACL			*/
1689da6c28aaSamw 	dump_uint8,		/* ZFS SYSACL			*/
1690da6c28aaSamw 	dump_none,		/* FUID nvlist			*/
1691da6c28aaSamw 	dump_packed_nvlist,	/* FUID nvlist size		*/
1692088f3894Sahrens 	dump_zap,		/* DSL dataset next clones	*/
1693088f3894Sahrens 	dump_zap,		/* DSL scrub queue		*/
169414843421SMatthew Ahrens 	dump_zap,		/* ZFS user/group used		*/
169514843421SMatthew Ahrens 	dump_zap,		/* ZFS user/group quota		*/
1696842727c2SChris Kirby 	dump_zap,		/* snapshot refcount tags	*/
1697486ae710SMatthew Ahrens 	dump_ddt_zap,		/* DDT ZAP object		*/
1698b24ab676SJeff Bonwick 	dump_zap,		/* DDT statistics		*/
16990a586ceaSMark Shellenbaum 	dump_znode,		/* SA object			*/
17000a586ceaSMark Shellenbaum 	dump_zap,		/* SA Master Node		*/
17010a586ceaSMark Shellenbaum 	dump_sa_attrs,		/* SA attribute registration	*/
17020a586ceaSMark Shellenbaum 	dump_sa_layouts,	/* SA attribute layouts		*/
17033f9d6ad7SLin Ling 	dump_zap,		/* DSL scrub translations	*/
17043f9d6ad7SLin Ling 	dump_none,		/* fake dedup BP		*/
1705cde58dbcSMatthew Ahrens 	dump_zap,		/* deadlist			*/
1706cde58dbcSMatthew Ahrens 	dump_none,		/* deadlist hdr			*/
1707cde58dbcSMatthew Ahrens 	dump_zap,		/* dsl clones			*/
1708cde58dbcSMatthew Ahrens 	dump_none,		/* bpobj subobjs		*/
17090a586ceaSMark Shellenbaum 	dump_unknown,		/* Unknown type, must be last	*/
1710fa9e4066Sahrens };
1711fa9e4066Sahrens 
1712fa9e4066Sahrens static void
1713fa9e4066Sahrens dump_object(objset_t *os, uint64_t object, int verbosity, int *print_header)
1714fa9e4066Sahrens {
1715fa9e4066Sahrens 	dmu_buf_t *db = NULL;
1716fa9e4066Sahrens 	dmu_object_info_t doi;
1717fa9e4066Sahrens 	dnode_t *dn;
1718fa9e4066Sahrens 	void *bonus = NULL;
1719fa9e4066Sahrens 	size_t bsize = 0;
17203f9d6ad7SLin Ling 	char iblk[32], dblk[32], lsize[32], asize[32], fill[32];
17213f9d6ad7SLin Ling 	char bonus_size[32];
1722fa9e4066Sahrens 	char aux[50];
1723fa9e4066Sahrens 	int error;
1724fa9e4066Sahrens 
1725fa9e4066Sahrens 	if (*print_header) {
1726b24ab676SJeff Bonwick 		(void) printf("\n%10s  %3s  %5s  %5s  %5s  %5s  %6s  %s\n",
1727b24ab676SJeff Bonwick 		    "Object", "lvl", "iblk", "dblk", "dsize", "lsize",
1728b24ab676SJeff Bonwick 		    "%full", "type");
1729fa9e4066Sahrens 		*print_header = 0;
1730fa9e4066Sahrens 	}
1731fa9e4066Sahrens 
1732fa9e4066Sahrens 	if (object == 0) {
1733744947dcSTom Erickson 		dn = DMU_META_DNODE(os);
1734fa9e4066Sahrens 	} else {
1735ea8dc4b6Seschrock 		error = dmu_bonus_hold(os, object, FTAG, &db);
1736ea8dc4b6Seschrock 		if (error)
1737ea8dc4b6Seschrock 			fatal("dmu_bonus_hold(%llu) failed, errno %u",
1738ea8dc4b6Seschrock 			    object, error);
1739fa9e4066Sahrens 		bonus = db->db_data;
1740fa9e4066Sahrens 		bsize = db->db_size;
1741744947dcSTom Erickson 		dn = DB_DNODE((dmu_buf_impl_t *)db);
1742fa9e4066Sahrens 	}
1743fa9e4066Sahrens 	dmu_object_info_from_dnode(dn, &doi);
1744fa9e4066Sahrens 
17453f9d6ad7SLin Ling 	zdb_nicenum(doi.doi_metadata_block_size, iblk);
17463f9d6ad7SLin Ling 	zdb_nicenum(doi.doi_data_block_size, dblk);
17473f9d6ad7SLin Ling 	zdb_nicenum(doi.doi_max_offset, lsize);
17483f9d6ad7SLin Ling 	zdb_nicenum(doi.doi_physical_blocks_512 << 9, asize);
17493f9d6ad7SLin Ling 	zdb_nicenum(doi.doi_bonus_size, bonus_size);
1750b24ab676SJeff Bonwick 	(void) sprintf(fill, "%6.2f", 100.0 * doi.doi_fill_count *
1751bbfd46c4SJeff Bonwick 	    doi.doi_data_block_size / (object == 0 ? DNODES_PER_BLOCK : 1) /
1752bbfd46c4SJeff Bonwick 	    doi.doi_max_offset);
1753fa9e4066Sahrens 
1754fa9e4066Sahrens 	aux[0] = '\0';
1755fa9e4066Sahrens 
1756e7437265Sahrens 	if (doi.doi_checksum != ZIO_CHECKSUM_INHERIT || verbosity >= 6) {
1757fa9e4066Sahrens 		(void) snprintf(aux + strlen(aux), sizeof (aux), " (K=%s)",
17586de8f417SVictor Latushkin 		    ZDB_CHECKSUM_NAME(doi.doi_checksum));
1759e7437265Sahrens 	}
1760fa9e4066Sahrens 
1761e7437265Sahrens 	if (doi.doi_compress != ZIO_COMPRESS_INHERIT || verbosity >= 6) {
1762fa9e4066Sahrens 		(void) snprintf(aux + strlen(aux), sizeof (aux), " (Z=%s)",
17636de8f417SVictor Latushkin 		    ZDB_COMPRESS_NAME(doi.doi_compress));
1764e7437265Sahrens 	}
1765fa9e4066Sahrens 
1766b24ab676SJeff Bonwick 	(void) printf("%10lld  %3u  %5s  %5s  %5s  %5s  %6s  %s%s\n",
1767b24ab676SJeff Bonwick 	    (u_longlong_t)object, doi.doi_indirection, iblk, dblk,
1768b24ab676SJeff Bonwick 	    asize, lsize, fill, ZDB_OT_NAME(doi.doi_type), aux);
1769fa9e4066Sahrens 
1770fa9e4066Sahrens 	if (doi.doi_bonus_type != DMU_OT_NONE && verbosity > 3) {
1771b24ab676SJeff Bonwick 		(void) printf("%10s  %3s  %5s  %5s  %5s  %5s  %6s  %s\n",
1772b24ab676SJeff Bonwick 		    "", "", "", "", "", bonus_size, "bonus",
17736de8f417SVictor Latushkin 		    ZDB_OT_NAME(doi.doi_bonus_type));
1774fa9e4066Sahrens 	}
1775fa9e4066Sahrens 
1776fa9e4066Sahrens 	if (verbosity >= 4) {
17770a586ceaSMark Shellenbaum 		(void) printf("\tdnode flags: %s%s%s\n",
177814843421SMatthew Ahrens 		    (dn->dn_phys->dn_flags & DNODE_FLAG_USED_BYTES) ?
177914843421SMatthew Ahrens 		    "USED_BYTES " : "",
178014843421SMatthew Ahrens 		    (dn->dn_phys->dn_flags & DNODE_FLAG_USERUSED_ACCOUNTED) ?
17810a586ceaSMark Shellenbaum 		    "USERUSED_ACCOUNTED " : "",
17820a586ceaSMark Shellenbaum 		    (dn->dn_phys->dn_flags & DNODE_FLAG_SPILL_BLKPTR) ?
17830a586ceaSMark Shellenbaum 		    "SPILL_BLKPTR" : "");
178414843421SMatthew Ahrens 		(void) printf("\tdnode maxblkid: %llu\n",
178514843421SMatthew Ahrens 		    (longlong_t)dn->dn_phys->dn_maxblkid);
178614843421SMatthew Ahrens 
17876de8f417SVictor Latushkin 		object_viewer[ZDB_OT_TYPE(doi.doi_bonus_type)](os, object,
17886de8f417SVictor Latushkin 		    bonus, bsize);
17896de8f417SVictor Latushkin 		object_viewer[ZDB_OT_TYPE(doi.doi_type)](os, object, NULL, 0);
1790fa9e4066Sahrens 		*print_header = 1;
1791fa9e4066Sahrens 	}
1792fa9e4066Sahrens 
1793fa9e4066Sahrens 	if (verbosity >= 5)
179488b7b0f2SMatthew Ahrens 		dump_indirect(dn);
1795fa9e4066Sahrens 
1796fa9e4066Sahrens 	if (verbosity >= 5) {
1797fa9e4066Sahrens 		/*
1798fa9e4066Sahrens 		 * Report the list of segments that comprise the object.
1799fa9e4066Sahrens 		 */
1800fa9e4066Sahrens 		uint64_t start = 0;
1801fa9e4066Sahrens 		uint64_t end;
1802fa9e4066Sahrens 		uint64_t blkfill = 1;
1803fa9e4066Sahrens 		int minlvl = 1;
1804fa9e4066Sahrens 
1805fa9e4066Sahrens 		if (dn->dn_type == DMU_OT_DNODE) {
1806fa9e4066Sahrens 			minlvl = 0;
1807fa9e4066Sahrens 			blkfill = DNODES_PER_BLOCK;
1808fa9e4066Sahrens 		}
1809fa9e4066Sahrens 
1810fa9e4066Sahrens 		for (;;) {
18113f9d6ad7SLin Ling 			char segsize[32];
1812cdb0ab79Smaybee 			error = dnode_next_offset(dn,
1813cdb0ab79Smaybee 			    0, &start, minlvl, blkfill, 0);
1814fa9e4066Sahrens 			if (error)
1815fa9e4066Sahrens 				break;
1816fa9e4066Sahrens 			end = start;
1817cdb0ab79Smaybee 			error = dnode_next_offset(dn,
1818cdb0ab79Smaybee 			    DNODE_FIND_HOLE, &end, minlvl, blkfill, 0);
18193f9d6ad7SLin Ling 			zdb_nicenum(end - start, segsize);
1820fa9e4066Sahrens 			(void) printf("\t\tsegment [%016llx, %016llx)"
1821fa9e4066Sahrens 			    " size %5s\n", (u_longlong_t)start,
1822fa9e4066Sahrens 			    (u_longlong_t)end, segsize);
1823fa9e4066Sahrens 			if (error)
1824fa9e4066Sahrens 				break;
1825fa9e4066Sahrens 			start = end;
1826fa9e4066Sahrens 		}
1827fa9e4066Sahrens 	}
1828fa9e4066Sahrens 
1829fa9e4066Sahrens 	if (db != NULL)
1830ea8dc4b6Seschrock 		dmu_buf_rele(db, FTAG);
1831fa9e4066Sahrens }
1832fa9e4066Sahrens 
1833fa9e4066Sahrens static char *objset_types[DMU_OST_NUMTYPES] = {
1834fa9e4066Sahrens 	"NONE", "META", "ZPL", "ZVOL", "OTHER", "ANY" };
1835fa9e4066Sahrens 
1836fa9e4066Sahrens static void
1837fa9e4066Sahrens dump_dir(objset_t *os)
1838fa9e4066Sahrens {
1839fa9e4066Sahrens 	dmu_objset_stats_t dds;
1840fa9e4066Sahrens 	uint64_t object, object_count;
1841a2eea2e1Sahrens 	uint64_t refdbytes, usedobjs, scratch;
18423f9d6ad7SLin Ling 	char numbuf[32];
184314843421SMatthew Ahrens 	char blkbuf[BP_SPRINTF_LEN + 20];
1844fa9e4066Sahrens 	char osname[MAXNAMELEN];
1845fa9e4066Sahrens 	char *type = "UNKNOWN";
1846fa9e4066Sahrens 	int verbosity = dump_opt['d'];
1847fa9e4066Sahrens 	int print_header = 1;
1848fa9e4066Sahrens 	int i, error;
1849fa9e4066Sahrens 
18503b2aab18SMatthew Ahrens 	dsl_pool_config_enter(dmu_objset_pool(os), FTAG);
1851a2eea2e1Sahrens 	dmu_objset_fast_stat(os, &dds);
18523b2aab18SMatthew Ahrens 	dsl_pool_config_exit(dmu_objset_pool(os), FTAG);
1853fa9e4066Sahrens 
1854fa9e4066Sahrens 	if (dds.dds_type < DMU_OST_NUMTYPES)
1855fa9e4066Sahrens 		type = objset_types[dds.dds_type];
1856fa9e4066Sahrens 
1857fa9e4066Sahrens 	if (dds.dds_type == DMU_OST_META) {
1858fa9e4066Sahrens 		dds.dds_creation_txg = TXG_INITIAL;
18595d7b4d43SMatthew Ahrens 		usedobjs = BP_GET_FILL(os->os_rootbp);
1860c1379625SJustin T. Gibbs 		refdbytes = dsl_dir_phys(os->os_spa->spa_dsl_pool->dp_mos_dir)->
1861c1379625SJustin T. Gibbs 		    dd_used_bytes;
1862a2eea2e1Sahrens 	} else {
1863a2eea2e1Sahrens 		dmu_objset_space(os, &refdbytes, &scratch, &usedobjs, &scratch);
1864fa9e4066Sahrens 	}
1865fa9e4066Sahrens 
18665d7b4d43SMatthew Ahrens 	ASSERT3U(usedobjs, ==, BP_GET_FILL(os->os_rootbp));
1867fa9e4066Sahrens 
18683f9d6ad7SLin Ling 	zdb_nicenum(refdbytes, numbuf);
1869fa9e4066Sahrens 
1870fa9e4066Sahrens 	if (verbosity >= 4) {
187143466aaeSMax Grossman 		(void) snprintf(blkbuf, sizeof (blkbuf), ", rootbp ");
187243466aaeSMax Grossman 		(void) snprintf_blkptr(blkbuf + strlen(blkbuf),
187343466aaeSMax Grossman 		    sizeof (blkbuf) - strlen(blkbuf), os->os_rootbp);
1874fa9e4066Sahrens 	} else {
1875fa9e4066Sahrens 		blkbuf[0] = '\0';
1876fa9e4066Sahrens 	}
1877fa9e4066Sahrens 
1878fa9e4066Sahrens 	dmu_objset_name(os, osname);
1879fa9e4066Sahrens 
1880a2eea2e1Sahrens 	(void) printf("Dataset %s [%s], ID %llu, cr_txg %llu, "
1881fa9e4066Sahrens 	    "%s, %llu objects%s\n",
1882fa9e4066Sahrens 	    osname, type, (u_longlong_t)dmu_objset_id(os),
1883fa9e4066Sahrens 	    (u_longlong_t)dds.dds_creation_txg,
1884a2eea2e1Sahrens 	    numbuf, (u_longlong_t)usedobjs, blkbuf);
1885fa9e4066Sahrens 
1886b24ab676SJeff Bonwick 	if (zopt_objects != 0) {
1887b24ab676SJeff Bonwick 		for (i = 0; i < zopt_objects; i++)
1888b24ab676SJeff Bonwick 			dump_object(os, zopt_object[i], verbosity,
1889b24ab676SJeff Bonwick 			    &print_header);
1890b24ab676SJeff Bonwick 		(void) printf("\n");
1891b24ab676SJeff Bonwick 		return;
1892b24ab676SJeff Bonwick 	}
1893b24ab676SJeff Bonwick 
1894b24ab676SJeff Bonwick 	if (dump_opt['i'] != 0 || verbosity >= 2)
1895fa9e4066Sahrens 		dump_intent_log(dmu_objset_zil(os));
1896fa9e4066Sahrens 
1897fa9e4066Sahrens 	if (dmu_objset_ds(os) != NULL)
1898cde58dbcSMatthew Ahrens 		dump_deadlist(&dmu_objset_ds(os)->ds_deadlist);
1899fa9e4066Sahrens 
1900fa9e4066Sahrens 	if (verbosity < 2)
1901fa9e4066Sahrens 		return;
1902fa9e4066Sahrens 
190343466aaeSMax Grossman 	if (BP_IS_HOLE(os->os_rootbp))
1904088f3894Sahrens 		return;
1905088f3894Sahrens 
1906fa9e4066Sahrens 	dump_object(os, 0, verbosity, &print_header);
190714843421SMatthew Ahrens 	object_count = 0;
1908744947dcSTom Erickson 	if (DMU_USERUSED_DNODE(os) != NULL &&
1909744947dcSTom Erickson 	    DMU_USERUSED_DNODE(os)->dn_type != 0) {
191014843421SMatthew Ahrens 		dump_object(os, DMU_USERUSED_OBJECT, verbosity, &print_header);
191114843421SMatthew Ahrens 		dump_object(os, DMU_GROUPUSED_OBJECT, verbosity, &print_header);
191214843421SMatthew Ahrens 	}
1913fa9e4066Sahrens 
1914fa9e4066Sahrens 	object = 0;
19156754306eSahrens 	while ((error = dmu_object_next(os, &object, B_FALSE, 0)) == 0) {
1916fa9e4066Sahrens 		dump_object(os, object, verbosity, &print_header);
1917fa9e4066Sahrens 		object_count++;
1918fa9e4066Sahrens 	}
1919fa9e4066Sahrens 
1920a2eea2e1Sahrens 	ASSERT3U(object_count, ==, usedobjs);
1921fa9e4066Sahrens 
1922fa9e4066Sahrens 	(void) printf("\n");
1923fa9e4066Sahrens 
1924ccba0801SRich Morris 	if (error != ESRCH) {
1925ccba0801SRich Morris 		(void) fprintf(stderr, "dmu_object_next() = %d\n", error);
1926ccba0801SRich Morris 		abort();
1927ccba0801SRich Morris 	}
1928fa9e4066Sahrens }
1929fa9e4066Sahrens 
1930fa9e4066Sahrens static void
193153b9a4a9SVictor Latushkin dump_uberblock(uberblock_t *ub, const char *header, const char *footer)
1932fa9e4066Sahrens {
1933fa9e4066Sahrens 	time_t timestamp = ub->ub_timestamp;
1934fa9e4066Sahrens 
193553b9a4a9SVictor Latushkin 	(void) printf(header ? header : "");
1936fa9e4066Sahrens 	(void) printf("\tmagic = %016llx\n", (u_longlong_t)ub->ub_magic);
1937fa9e4066Sahrens 	(void) printf("\tversion = %llu\n", (u_longlong_t)ub->ub_version);
1938fa9e4066Sahrens 	(void) printf("\ttxg = %llu\n", (u_longlong_t)ub->ub_txg);
1939fa9e4066Sahrens 	(void) printf("\tguid_sum = %llu\n", (u_longlong_t)ub->ub_guid_sum);
1940fa9e4066Sahrens 	(void) printf("\ttimestamp = %llu UTC = %s",
1941fa9e4066Sahrens 	    (u_longlong_t)ub->ub_timestamp, asctime(localtime(&timestamp)));
1942fa9e4066Sahrens 	if (dump_opt['u'] >= 3) {
1943fbabab8fSmaybee 		char blkbuf[BP_SPRINTF_LEN];
194443466aaeSMax Grossman 		snprintf_blkptr(blkbuf, sizeof (blkbuf), &ub->ub_rootbp);
1945fa9e4066Sahrens 		(void) printf("\trootbp = %s\n", blkbuf);
1946fa9e4066Sahrens 	}
194753b9a4a9SVictor Latushkin 	(void) printf(footer ? footer : "");
1948fa9e4066Sahrens }
1949fa9e4066Sahrens 
1950fa9e4066Sahrens static void
195107428bdfSVictor Latushkin dump_config(spa_t *spa)
1952fa9e4066Sahrens {
195307428bdfSVictor Latushkin 	dmu_buf_t *db;
195407428bdfSVictor Latushkin 	size_t nvsize = 0;
195507428bdfSVictor Latushkin 	int error = 0;
1956fa9e4066Sahrens 
195707428bdfSVictor Latushkin 
195807428bdfSVictor Latushkin 	error = dmu_bonus_hold(spa->spa_meta_objset,
195907428bdfSVictor Latushkin 	    spa->spa_config_object, FTAG, &db);
196007428bdfSVictor Latushkin 
196107428bdfSVictor Latushkin 	if (error == 0) {
196207428bdfSVictor Latushkin 		nvsize = *(uint64_t *)db->db_data;
196307428bdfSVictor Latushkin 		dmu_buf_rele(db, FTAG);
196407428bdfSVictor Latushkin 
196507428bdfSVictor Latushkin 		(void) printf("\nMOS Configuration:\n");
196607428bdfSVictor Latushkin 		dump_packed_nvlist(spa->spa_meta_objset,
196707428bdfSVictor Latushkin 		    spa->spa_config_object, (void *)&nvsize, 1);
196807428bdfSVictor Latushkin 	} else {
196907428bdfSVictor Latushkin 		(void) fprintf(stderr, "dmu_bonus_hold(%llu) failed, errno %d",
197007428bdfSVictor Latushkin 		    (u_longlong_t)spa->spa_config_object, error);
1971fa9e4066Sahrens 	}
1972fa9e4066Sahrens }
1973fa9e4066Sahrens 
1974fa9e4066Sahrens static void
1975c5904d13Seschrock dump_cachefile(const char *cachefile)
1976c5904d13Seschrock {
1977c5904d13Seschrock 	int fd;
1978c5904d13Seschrock 	struct stat64 statbuf;
1979c5904d13Seschrock 	char *buf;
1980c5904d13Seschrock 	nvlist_t *config;
1981c5904d13Seschrock 
1982c5904d13Seschrock 	if ((fd = open64(cachefile, O_RDONLY)) < 0) {
1983c5904d13Seschrock 		(void) printf("cannot open '%s': %s\n", cachefile,
1984c5904d13Seschrock 		    strerror(errno));
1985c5904d13Seschrock 		exit(1);
1986c5904d13Seschrock 	}
1987c5904d13Seschrock 
1988c5904d13Seschrock 	if (fstat64(fd, &statbuf) != 0) {
1989c5904d13Seschrock 		(void) printf("failed to stat '%s': %s\n", cachefile,
1990c5904d13Seschrock 		    strerror(errno));
1991c5904d13Seschrock 		exit(1);
1992c5904d13Seschrock 	}
1993c5904d13Seschrock 
1994c5904d13Seschrock 	if ((buf = malloc(statbuf.st_size)) == NULL) {
1995c5904d13Seschrock 		(void) fprintf(stderr, "failed to allocate %llu bytes\n",
1996c5904d13Seschrock 		    (u_longlong_t)statbuf.st_size);
1997c5904d13Seschrock 		exit(1);
1998c5904d13Seschrock 	}
1999c5904d13Seschrock 
2000c5904d13Seschrock 	if (read(fd, buf, statbuf.st_size) != statbuf.st_size) {
2001c5904d13Seschrock 		(void) fprintf(stderr, "failed to read %llu bytes\n",
2002c5904d13Seschrock 		    (u_longlong_t)statbuf.st_size);
2003c5904d13Seschrock 		exit(1);
2004c5904d13Seschrock 	}
2005c5904d13Seschrock 
2006c5904d13Seschrock 	(void) close(fd);
2007c5904d13Seschrock 
2008c5904d13Seschrock 	if (nvlist_unpack(buf, statbuf.st_size, &config, 0) != 0) {
2009c5904d13Seschrock 		(void) fprintf(stderr, "failed to unpack nvlist\n");
2010c5904d13Seschrock 		exit(1);
2011c5904d13Seschrock 	}
2012c5904d13Seschrock 
2013c5904d13Seschrock 	free(buf);
2014c5904d13Seschrock 
2015c5904d13Seschrock 	dump_nvlist(config, 0);
2016c5904d13Seschrock 
2017c5904d13Seschrock 	nvlist_free(config);
2018c5904d13Seschrock }
2019c5904d13Seschrock 
202053b9a4a9SVictor Latushkin #define	ZDB_MAX_UB_HEADER_SIZE 32
202153b9a4a9SVictor Latushkin 
202253b9a4a9SVictor Latushkin static void
202353b9a4a9SVictor Latushkin dump_label_uberblocks(vdev_label_t *lbl, uint64_t ashift)
202453b9a4a9SVictor Latushkin {
202553b9a4a9SVictor Latushkin 	vdev_t vd;
202653b9a4a9SVictor Latushkin 	vdev_t *vdp = &vd;
202753b9a4a9SVictor Latushkin 	char header[ZDB_MAX_UB_HEADER_SIZE];
202853b9a4a9SVictor Latushkin 
202953b9a4a9SVictor Latushkin 	vd.vdev_ashift = ashift;
203053b9a4a9SVictor Latushkin 	vdp->vdev_top = vdp;
203153b9a4a9SVictor Latushkin 
203253b9a4a9SVictor Latushkin 	for (int i = 0; i < VDEV_UBERBLOCK_COUNT(vdp); i++) {
203353b9a4a9SVictor Latushkin 		uint64_t uoff = VDEV_UBERBLOCK_OFFSET(vdp, i);
203453b9a4a9SVictor Latushkin 		uberblock_t *ub = (void *)((char *)lbl + uoff);
203553b9a4a9SVictor Latushkin 
203653b9a4a9SVictor Latushkin 		if (uberblock_verify(ub))
203753b9a4a9SVictor Latushkin 			continue;
203853b9a4a9SVictor Latushkin 		(void) snprintf(header, ZDB_MAX_UB_HEADER_SIZE,
203953b9a4a9SVictor Latushkin 		    "Uberblock[%d]\n", i);
204053b9a4a9SVictor Latushkin 		dump_uberblock(ub, header, "");
204153b9a4a9SVictor Latushkin 	}
204253b9a4a9SVictor Latushkin }
204353b9a4a9SVictor Latushkin 
2044c5904d13Seschrock static void
2045fa9e4066Sahrens dump_label(const char *dev)
2046fa9e4066Sahrens {
2047fa9e4066Sahrens 	int fd;
2048fa9e4066Sahrens 	vdev_label_t label;
2049c6065d0fSGeorge Wilson 	char *path, *buf = label.vl_vdev_phys.vp_nvlist;
2050fa9e4066Sahrens 	size_t buflen = sizeof (label.vl_vdev_phys.vp_nvlist);
2051fa9e4066Sahrens 	struct stat64 statbuf;
205253b9a4a9SVictor Latushkin 	uint64_t psize, ashift;
2053c6065d0fSGeorge Wilson 	int len = strlen(dev) + 1;
2054fa9e4066Sahrens 
2055c6065d0fSGeorge Wilson 	if (strncmp(dev, "/dev/dsk/", 9) == 0) {
2056c6065d0fSGeorge Wilson 		len++;
2057c6065d0fSGeorge Wilson 		path = malloc(len);
2058c6065d0fSGeorge Wilson 		(void) snprintf(path, len, "%s%s", "/dev/rdsk/", dev + 9);
2059c6065d0fSGeorge Wilson 	} else {
2060c6065d0fSGeorge Wilson 		path = strdup(dev);
2061c6065d0fSGeorge Wilson 	}
2062c6065d0fSGeorge Wilson 
2063c6065d0fSGeorge Wilson 	if ((fd = open64(path, O_RDONLY)) < 0) {
2064c6065d0fSGeorge Wilson 		(void) printf("cannot open '%s': %s\n", path, strerror(errno));
2065c6065d0fSGeorge Wilson 		free(path);
2066fa9e4066Sahrens 		exit(1);
2067fa9e4066Sahrens 	}
2068fa9e4066Sahrens 
2069fa9e4066Sahrens 	if (fstat64(fd, &statbuf) != 0) {
2070c6065d0fSGeorge Wilson 		(void) printf("failed to stat '%s': %s\n", path,
2071fa9e4066Sahrens 		    strerror(errno));
2072c6065d0fSGeorge Wilson 		free(path);
2073c6065d0fSGeorge Wilson 		(void) close(fd);
2074c6065d0fSGeorge Wilson 		exit(1);
2075c6065d0fSGeorge Wilson 	}
2076c6065d0fSGeorge Wilson 
2077c6065d0fSGeorge Wilson 	if (S_ISBLK(statbuf.st_mode)) {
2078c6065d0fSGeorge Wilson 		(void) printf("cannot use '%s': character device required\n",
2079c6065d0fSGeorge Wilson 		    path);
2080c6065d0fSGeorge Wilson 		free(path);
2081c6065d0fSGeorge Wilson 		(void) close(fd);
2082c6065d0fSGeorge Wilson 		exit(1);
2083fa9e4066Sahrens 	}
2084fa9e4066Sahrens 
2085fa9e4066Sahrens 	psize = statbuf.st_size;
2086fa9e4066Sahrens 	psize = P2ALIGN(psize, (uint64_t)sizeof (vdev_label_t));
2087fa9e4066Sahrens 
208853b9a4a9SVictor Latushkin 	for (int l = 0; l < VDEV_LABELS; l++) {
2089fa9e4066Sahrens 		nvlist_t *config = NULL;
2090fa9e4066Sahrens 
2091fa9e4066Sahrens 		(void) printf("--------------------------------------------\n");
2092fa9e4066Sahrens 		(void) printf("LABEL %d\n", l);
2093fa9e4066Sahrens 		(void) printf("--------------------------------------------\n");
2094fa9e4066Sahrens 
20950d981225Seschrock 		if (pread64(fd, &label, sizeof (label),
2096fa9e4066Sahrens 		    vdev_label_offset(psize, l, 0)) != sizeof (label)) {
2097fa9e4066Sahrens 			(void) printf("failed to read label %d\n", l);
2098fa9e4066Sahrens 			continue;
2099fa9e4066Sahrens 		}
2100fa9e4066Sahrens 
2101fa9e4066Sahrens 		if (nvlist_unpack(buf, buflen, &config, 0) != 0) {
2102fa9e4066Sahrens 			(void) printf("failed to unpack label %d\n", l);
210353b9a4a9SVictor Latushkin 			ashift = SPA_MINBLOCKSHIFT;
210453b9a4a9SVictor Latushkin 		} else {
210553b9a4a9SVictor Latushkin 			nvlist_t *vdev_tree = NULL;
210653b9a4a9SVictor Latushkin 
2107fa9e4066Sahrens 			dump_nvlist(config, 4);
210853b9a4a9SVictor Latushkin 			if ((nvlist_lookup_nvlist(config,
210953b9a4a9SVictor Latushkin 			    ZPOOL_CONFIG_VDEV_TREE, &vdev_tree) != 0) ||
211053b9a4a9SVictor Latushkin 			    (nvlist_lookup_uint64(vdev_tree,
211153b9a4a9SVictor Latushkin 			    ZPOOL_CONFIG_ASHIFT, &ashift) != 0))
211253b9a4a9SVictor Latushkin 				ashift = SPA_MINBLOCKSHIFT;
2113fa9e4066Sahrens 			nvlist_free(config);
2114fa9e4066Sahrens 		}
211553b9a4a9SVictor Latushkin 		if (dump_opt['u'])
211653b9a4a9SVictor Latushkin 			dump_label_uberblocks(&label, ashift);
211753b9a4a9SVictor Latushkin 	}
2118c6065d0fSGeorge Wilson 
2119c6065d0fSGeorge Wilson 	free(path);
2120c6065d0fSGeorge Wilson 	(void) close(fd);
2121fa9e4066Sahrens }
2122fa9e4066Sahrens 
2123b5152584SMatthew Ahrens static uint64_t num_large_blocks;
2124b5152584SMatthew Ahrens 
2125fa9e4066Sahrens /*ARGSUSED*/
21261d452cf5Sahrens static int
2127fd136879SMatthew Ahrens dump_one_dir(const char *dsname, void *arg)
2128fa9e4066Sahrens {
2129fa9e4066Sahrens 	int error;
2130fa9e4066Sahrens 	objset_t *os;
2131fa9e4066Sahrens 
2132503ad85cSMatthew Ahrens 	error = dmu_objset_own(dsname, DMU_OST_ANY, B_TRUE, FTAG, &os);
2133fa9e4066Sahrens 	if (error) {
2134b24ab676SJeff Bonwick 		(void) printf("Could not open %s, error %d\n", dsname, error);
21351d452cf5Sahrens 		return (0);
2136fa9e4066Sahrens 	}
2137b5152584SMatthew Ahrens 	if (dmu_objset_ds(os)->ds_large_blocks)
2138b5152584SMatthew Ahrens 		num_large_blocks++;
2139fa9e4066Sahrens 	dump_dir(os);
2140503ad85cSMatthew Ahrens 	dmu_objset_disown(os, FTAG);
2141e0d35c44Smarks 	fuid_table_destroy();
21420a586ceaSMark Shellenbaum 	sa_loaded = B_FALSE;
21431d452cf5Sahrens 	return (0);
2144fa9e4066Sahrens }
2145fa9e4066Sahrens 
2146b24ab676SJeff Bonwick /*
2147b24ab676SJeff Bonwick  * Block statistics.
2148b24ab676SJeff Bonwick  */
2149b5152584SMatthew Ahrens #define	PSIZE_HISTO_SIZE (SPA_OLD_MAXBLOCKSIZE / SPA_MINBLOCKSIZE + 2)
2150b24ab676SJeff Bonwick typedef struct zdb_blkstats {
2151b24ab676SJeff Bonwick 	uint64_t zb_asize;
2152b24ab676SJeff Bonwick 	uint64_t zb_lsize;
2153b24ab676SJeff Bonwick 	uint64_t zb_psize;
2154b24ab676SJeff Bonwick 	uint64_t zb_count;
2155d5ee8a13SMatthew Ahrens 	uint64_t zb_gangs;
2156d5ee8a13SMatthew Ahrens 	uint64_t zb_ditto_samevdev;
2157490d05b9SMatthew Ahrens 	uint64_t zb_psize_histogram[PSIZE_HISTO_SIZE];
2158b24ab676SJeff Bonwick } zdb_blkstats_t;
2159b24ab676SJeff Bonwick 
2160b24ab676SJeff Bonwick /*
2161b24ab676SJeff Bonwick  * Extended object types to report deferred frees and dedup auto-ditto blocks.
2162b24ab676SJeff Bonwick  */
2163b24ab676SJeff Bonwick #define	ZDB_OT_DEFERRED	(DMU_OT_NUMTYPES + 0)
2164b24ab676SJeff Bonwick #define	ZDB_OT_DITTO	(DMU_OT_NUMTYPES + 1)
2165ad135b5dSChristopher Siden #define	ZDB_OT_OTHER	(DMU_OT_NUMTYPES + 2)
2166ad135b5dSChristopher Siden #define	ZDB_OT_TOTAL	(DMU_OT_NUMTYPES + 3)
2167b24ab676SJeff Bonwick 
2168b24ab676SJeff Bonwick static char *zdb_ot_extname[] = {
2169b24ab676SJeff Bonwick 	"deferred free",
2170b24ab676SJeff Bonwick 	"dedup ditto",
2171ad135b5dSChristopher Siden 	"other",
2172b24ab676SJeff Bonwick 	"Total",
2173b24ab676SJeff Bonwick };
2174b24ab676SJeff Bonwick 
2175b24ab676SJeff Bonwick #define	ZB_TOTAL	DN_MAX_LEVELS
2176b24ab676SJeff Bonwick 
2177b24ab676SJeff Bonwick typedef struct zdb_cb {
2178b24ab676SJeff Bonwick 	zdb_blkstats_t	zcb_type[ZB_TOTAL + 1][ZDB_OT_TOTAL + 1];
2179b24ab676SJeff Bonwick 	uint64_t	zcb_dedup_asize;
2180b24ab676SJeff Bonwick 	uint64_t	zcb_dedup_blocks;
21815d7b4d43SMatthew Ahrens 	uint64_t	zcb_embedded_blocks[NUM_BP_EMBEDDED_TYPES];
21825d7b4d43SMatthew Ahrens 	uint64_t	zcb_embedded_histogram[NUM_BP_EMBEDDED_TYPES]
21835d7b4d43SMatthew Ahrens 	    [BPE_PAYLOAD_SIZE];
2184490d05b9SMatthew Ahrens 	uint64_t	zcb_start;
2185490d05b9SMatthew Ahrens 	uint64_t	zcb_lastprint;
2186490d05b9SMatthew Ahrens 	uint64_t	zcb_totalasize;
2187b24ab676SJeff Bonwick 	uint64_t	zcb_errors[256];
2188b24ab676SJeff Bonwick 	int		zcb_readfails;
2189b24ab676SJeff Bonwick 	int		zcb_haderrors;
2190cde58dbcSMatthew Ahrens 	spa_t		*zcb_spa;
2191b24ab676SJeff Bonwick } zdb_cb_t;
2192b24ab676SJeff Bonwick 
2193b24ab676SJeff Bonwick static void
2194cde58dbcSMatthew Ahrens zdb_count_block(zdb_cb_t *zcb, zilog_t *zilog, const blkptr_t *bp,
2195b24ab676SJeff Bonwick     dmu_object_type_t type)
2196b24ab676SJeff Bonwick {
2197b24ab676SJeff Bonwick 	uint64_t refcnt = 0;
2198b24ab676SJeff Bonwick 
2199b24ab676SJeff Bonwick 	ASSERT(type < ZDB_OT_TOTAL);
2200b24ab676SJeff Bonwick 
2201b24ab676SJeff Bonwick 	if (zilog && zil_bp_tree_add(zilog, bp) != 0)
2202b24ab676SJeff Bonwick 		return;
2203b24ab676SJeff Bonwick 
2204b24ab676SJeff Bonwick 	for (int i = 0; i < 4; i++) {
2205b24ab676SJeff Bonwick 		int l = (i < 2) ? BP_GET_LEVEL(bp) : ZB_TOTAL;
2206b24ab676SJeff Bonwick 		int t = (i & 1) ? type : ZDB_OT_TOTAL;
2207d5ee8a13SMatthew Ahrens 		int equal;
2208b24ab676SJeff Bonwick 		zdb_blkstats_t *zb = &zcb->zcb_type[l][t];
2209b24ab676SJeff Bonwick 
2210b24ab676SJeff Bonwick 		zb->zb_asize += BP_GET_ASIZE(bp);
2211b24ab676SJeff Bonwick 		zb->zb_lsize += BP_GET_LSIZE(bp);
2212b24ab676SJeff Bonwick 		zb->zb_psize += BP_GET_PSIZE(bp);
2213b24ab676SJeff Bonwick 		zb->zb_count++;
2214b5152584SMatthew Ahrens 
2215b5152584SMatthew Ahrens 		/*
2216b5152584SMatthew Ahrens 		 * The histogram is only big enough to record blocks up to
2217b5152584SMatthew Ahrens 		 * SPA_OLD_MAXBLOCKSIZE; larger blocks go into the last,
2218b5152584SMatthew Ahrens 		 * "other", bucket.
2219b5152584SMatthew Ahrens 		 */
2220b5152584SMatthew Ahrens 		int idx = BP_GET_PSIZE(bp) >> SPA_MINBLOCKSHIFT;
2221b5152584SMatthew Ahrens 		idx = MIN(idx, SPA_OLD_MAXBLOCKSIZE / SPA_MINBLOCKSIZE + 1);
2222b5152584SMatthew Ahrens 		zb->zb_psize_histogram[idx]++;
2223d5ee8a13SMatthew Ahrens 
2224d5ee8a13SMatthew Ahrens 		zb->zb_gangs += BP_COUNT_GANG(bp);
2225d5ee8a13SMatthew Ahrens 
2226d5ee8a13SMatthew Ahrens 		switch (BP_GET_NDVAS(bp)) {
2227d5ee8a13SMatthew Ahrens 		case 2:
2228d5ee8a13SMatthew Ahrens 			if (DVA_GET_VDEV(&bp->blk_dva[0]) ==
2229d5ee8a13SMatthew Ahrens 			    DVA_GET_VDEV(&bp->blk_dva[1]))
2230d5ee8a13SMatthew Ahrens 				zb->zb_ditto_samevdev++;
2231d5ee8a13SMatthew Ahrens 			break;
2232d5ee8a13SMatthew Ahrens 		case 3:
2233d5ee8a13SMatthew Ahrens 			equal = (DVA_GET_VDEV(&bp->blk_dva[0]) ==
2234d5ee8a13SMatthew Ahrens 			    DVA_GET_VDEV(&bp->blk_dva[1])) +
2235d5ee8a13SMatthew Ahrens 			    (DVA_GET_VDEV(&bp->blk_dva[0]) ==
2236d5ee8a13SMatthew Ahrens 			    DVA_GET_VDEV(&bp->blk_dva[2])) +
2237d5ee8a13SMatthew Ahrens 			    (DVA_GET_VDEV(&bp->blk_dva[1]) ==
2238d5ee8a13SMatthew Ahrens 			    DVA_GET_VDEV(&bp->blk_dva[2]));
2239d5ee8a13SMatthew Ahrens 			if (equal != 0)
2240d5ee8a13SMatthew Ahrens 				zb->zb_ditto_samevdev++;
2241d5ee8a13SMatthew Ahrens 			break;
2242d5ee8a13SMatthew Ahrens 		}
2243d5ee8a13SMatthew Ahrens 
2244b24ab676SJeff Bonwick 	}
2245b24ab676SJeff Bonwick 
22465d7b4d43SMatthew Ahrens 	if (BP_IS_EMBEDDED(bp)) {
22475d7b4d43SMatthew Ahrens 		zcb->zcb_embedded_blocks[BPE_GET_ETYPE(bp)]++;
22485d7b4d43SMatthew Ahrens 		zcb->zcb_embedded_histogram[BPE_GET_ETYPE(bp)]
22495d7b4d43SMatthew Ahrens 		    [BPE_GET_PSIZE(bp)]++;
22505d7b4d43SMatthew Ahrens 		return;
22515d7b4d43SMatthew Ahrens 	}
22525d7b4d43SMatthew Ahrens 
2253b24ab676SJeff Bonwick 	if (dump_opt['L'])
2254b24ab676SJeff Bonwick 		return;
2255b24ab676SJeff Bonwick 
2256b24ab676SJeff Bonwick 	if (BP_GET_DEDUP(bp)) {
2257b24ab676SJeff Bonwick 		ddt_t *ddt;
2258b24ab676SJeff Bonwick 		ddt_entry_t *dde;
2259b24ab676SJeff Bonwick 
2260cde58dbcSMatthew Ahrens 		ddt = ddt_select(zcb->zcb_spa, bp);
2261b24ab676SJeff Bonwick 		ddt_enter(ddt);
2262b24ab676SJeff Bonwick 		dde = ddt_lookup(ddt, bp, B_FALSE);
2263b24ab676SJeff Bonwick 
2264b24ab676SJeff Bonwick 		if (dde == NULL) {
2265b24ab676SJeff Bonwick 			refcnt = 0;
2266b24ab676SJeff Bonwick 		} else {
2267b24ab676SJeff Bonwick 			ddt_phys_t *ddp = ddt_phys_select(dde, bp);
2268b24ab676SJeff Bonwick 			ddt_phys_decref(ddp);
2269b24ab676SJeff Bonwick 			refcnt = ddp->ddp_refcnt;
2270b24ab676SJeff Bonwick 			if (ddt_phys_total_refcnt(dde) == 0)
2271b24ab676SJeff Bonwick 				ddt_remove(ddt, dde);
2272b24ab676SJeff Bonwick 		}
2273b24ab676SJeff Bonwick 		ddt_exit(ddt);
2274b24ab676SJeff Bonwick 	}
2275b24ab676SJeff Bonwick 
2276cde58dbcSMatthew Ahrens 	VERIFY3U(zio_wait(zio_claim(NULL, zcb->zcb_spa,
2277cde58dbcSMatthew Ahrens 	    refcnt ? 0 : spa_first_txg(zcb->zcb_spa),
2278b24ab676SJeff Bonwick 	    bp, NULL, NULL, ZIO_FLAG_CANFAIL)), ==, 0);
2279b24ab676SJeff Bonwick }
2280b24ab676SJeff Bonwick 
228131d7e8faSGeorge Wilson static void
228231d7e8faSGeorge Wilson zdb_blkptr_done(zio_t *zio)
228331d7e8faSGeorge Wilson {
228431d7e8faSGeorge Wilson 	spa_t *spa = zio->io_spa;
228531d7e8faSGeorge Wilson 	blkptr_t *bp = zio->io_bp;
228631d7e8faSGeorge Wilson 	int ioerr = zio->io_error;
228731d7e8faSGeorge Wilson 	zdb_cb_t *zcb = zio->io_private;
22887802d7bfSMatthew Ahrens 	zbookmark_phys_t *zb = &zio->io_bookmark;
228931d7e8faSGeorge Wilson 
229031d7e8faSGeorge Wilson 	zio_data_buf_free(zio->io_data, zio->io_size);
229131d7e8faSGeorge Wilson 
229231d7e8faSGeorge Wilson 	mutex_enter(&spa->spa_scrub_lock);
229331d7e8faSGeorge Wilson 	spa->spa_scrub_inflight--;
229431d7e8faSGeorge Wilson 	cv_broadcast(&spa->spa_scrub_io_cv);
229531d7e8faSGeorge Wilson 
229631d7e8faSGeorge Wilson 	if (ioerr && !(zio->io_flags & ZIO_FLAG_SPECULATIVE)) {
229731d7e8faSGeorge Wilson 		char blkbuf[BP_SPRINTF_LEN];
229831d7e8faSGeorge Wilson 
229931d7e8faSGeorge Wilson 		zcb->zcb_haderrors = 1;
230031d7e8faSGeorge Wilson 		zcb->zcb_errors[ioerr]++;
230131d7e8faSGeorge Wilson 
230231d7e8faSGeorge Wilson 		if (dump_opt['b'] >= 2)
230343466aaeSMax Grossman 			snprintf_blkptr(blkbuf, sizeof (blkbuf), bp);
230431d7e8faSGeorge Wilson 		else
230531d7e8faSGeorge Wilson 			blkbuf[0] = '\0';
230631d7e8faSGeorge Wilson 
230731d7e8faSGeorge Wilson 		(void) printf("zdb_blkptr_cb: "
230831d7e8faSGeorge Wilson 		    "Got error %d reading "
230931d7e8faSGeorge Wilson 		    "<%llu, %llu, %lld, %llx> %s -- skipping\n",
231031d7e8faSGeorge Wilson 		    ioerr,
231131d7e8faSGeorge Wilson 		    (u_longlong_t)zb->zb_objset,
231231d7e8faSGeorge Wilson 		    (u_longlong_t)zb->zb_object,
231331d7e8faSGeorge Wilson 		    (u_longlong_t)zb->zb_level,
231431d7e8faSGeorge Wilson 		    (u_longlong_t)zb->zb_blkid,
231531d7e8faSGeorge Wilson 		    blkbuf);
231631d7e8faSGeorge Wilson 	}
231731d7e8faSGeorge Wilson 	mutex_exit(&spa->spa_scrub_lock);
231831d7e8faSGeorge Wilson }
231931d7e8faSGeorge Wilson 
2320b24ab676SJeff Bonwick static int
23211b912ec7SGeorge Wilson zdb_blkptr_cb(spa_t *spa, zilog_t *zilog, const blkptr_t *bp,
23227802d7bfSMatthew Ahrens     const zbookmark_phys_t *zb, const dnode_phys_t *dnp, void *arg)
2323b24ab676SJeff Bonwick {
2324b24ab676SJeff Bonwick 	zdb_cb_t *zcb = arg;
2325b24ab676SJeff Bonwick 	dmu_object_type_t type;
2326b24ab676SJeff Bonwick 	boolean_t is_metadata;
2327b24ab676SJeff Bonwick 
232843466aaeSMax Grossman 	if (dump_opt['b'] >= 5 && bp->blk_birth > 0) {
232943466aaeSMax Grossman 		char blkbuf[BP_SPRINTF_LEN];
233043466aaeSMax Grossman 		snprintf_blkptr(blkbuf, sizeof (blkbuf), bp);
233143466aaeSMax Grossman 		(void) printf("objset %llu object %llu "
233243466aaeSMax Grossman 		    "level %lld offset 0x%llx %s\n",
233343466aaeSMax Grossman 		    (u_longlong_t)zb->zb_objset,
233443466aaeSMax Grossman 		    (u_longlong_t)zb->zb_object,
233543466aaeSMax Grossman 		    (longlong_t)zb->zb_level,
233643466aaeSMax Grossman 		    (u_longlong_t)blkid2offset(dnp, bp, zb),
233743466aaeSMax Grossman 		    blkbuf);
233843466aaeSMax Grossman 	}
233943466aaeSMax Grossman 
234043466aaeSMax Grossman 	if (BP_IS_HOLE(bp))
2341b24ab676SJeff Bonwick 		return (0);
2342b24ab676SJeff Bonwick 
2343b24ab676SJeff Bonwick 	type = BP_GET_TYPE(bp);
2344b24ab676SJeff Bonwick 
2345ad135b5dSChristopher Siden 	zdb_count_block(zcb, zilog, bp,
2346ad135b5dSChristopher Siden 	    (type & DMU_OT_NEWTYPE) ? ZDB_OT_OTHER : type);
2347b24ab676SJeff Bonwick 
2348ad135b5dSChristopher Siden 	is_metadata = (BP_GET_LEVEL(bp) != 0 || DMU_OT_IS_METADATA(type));
2349b24ab676SJeff Bonwick 
23505d7b4d43SMatthew Ahrens 	if (!BP_IS_EMBEDDED(bp) &&
23515d7b4d43SMatthew Ahrens 	    (dump_opt['c'] > 1 || (dump_opt['c'] && is_metadata))) {
2352b24ab676SJeff Bonwick 		size_t size = BP_GET_PSIZE(bp);
235331d7e8faSGeorge Wilson 		void *data = zio_data_buf_alloc(size);
2354b24ab676SJeff Bonwick 		int flags = ZIO_FLAG_CANFAIL | ZIO_FLAG_SCRUB | ZIO_FLAG_RAW;
2355b24ab676SJeff Bonwick 
2356b24ab676SJeff Bonwick 		/* If it's an intent log block, failure is expected. */
2357b24ab676SJeff Bonwick 		if (zb->zb_level == ZB_ZIL_LEVEL)
2358b24ab676SJeff Bonwick 			flags |= ZIO_FLAG_SPECULATIVE;
2359b24ab676SJeff Bonwick 
236031d7e8faSGeorge Wilson 		mutex_enter(&spa->spa_scrub_lock);
236131d7e8faSGeorge Wilson 		while (spa->spa_scrub_inflight > max_inflight)
236231d7e8faSGeorge Wilson 			cv_wait(&spa->spa_scrub_io_cv, &spa->spa_scrub_lock);
236331d7e8faSGeorge Wilson 		spa->spa_scrub_inflight++;
236431d7e8faSGeorge Wilson 		mutex_exit(&spa->spa_scrub_lock);
2365b24ab676SJeff Bonwick 
236631d7e8faSGeorge Wilson 		zio_nowait(zio_read(NULL, spa, bp, data, size,
236731d7e8faSGeorge Wilson 		    zdb_blkptr_done, zcb, ZIO_PRIORITY_ASYNC_READ, flags, zb));
2368b24ab676SJeff Bonwick 	}
2369b24ab676SJeff Bonwick 
2370b24ab676SJeff Bonwick 	zcb->zcb_readfails = 0;
2371b24ab676SJeff Bonwick 
2372f7950bf1SMatthew Ahrens 	/* only call gethrtime() every 100 blocks */
2373f7950bf1SMatthew Ahrens 	static int iters;
2374f7950bf1SMatthew Ahrens 	if (++iters > 100)
2375f7950bf1SMatthew Ahrens 		iters = 0;
2376f7950bf1SMatthew Ahrens 	else
2377f7950bf1SMatthew Ahrens 		return (0);
2378f7950bf1SMatthew Ahrens 
2379f7950bf1SMatthew Ahrens 	if (dump_opt['b'] < 5 && gethrtime() > zcb->zcb_lastprint + NANOSEC) {
2380490d05b9SMatthew Ahrens 		uint64_t now = gethrtime();
2381490d05b9SMatthew Ahrens 		char buf[10];
2382490d05b9SMatthew Ahrens 		uint64_t bytes = zcb->zcb_type[ZB_TOTAL][ZDB_OT_TOTAL].zb_asize;
2383490d05b9SMatthew Ahrens 		int kb_per_sec =
2384490d05b9SMatthew Ahrens 		    1 + bytes / (1 + ((now - zcb->zcb_start) / 1000 / 1000));
2385490d05b9SMatthew Ahrens 		int sec_remaining =
2386490d05b9SMatthew Ahrens 		    (zcb->zcb_totalasize - bytes) / 1024 / kb_per_sec;
2387490d05b9SMatthew Ahrens 
2388490d05b9SMatthew Ahrens 		zfs_nicenum(bytes, buf, sizeof (buf));
2389490d05b9SMatthew Ahrens 		(void) fprintf(stderr,
2390490d05b9SMatthew Ahrens 		    "\r%5s completed (%4dMB/s) "
2391490d05b9SMatthew Ahrens 		    "estimated time remaining: %uhr %02umin %02usec        ",
2392490d05b9SMatthew Ahrens 		    buf, kb_per_sec / 1024,
2393490d05b9SMatthew Ahrens 		    sec_remaining / 60 / 60,
2394490d05b9SMatthew Ahrens 		    sec_remaining / 60 % 60,
2395490d05b9SMatthew Ahrens 		    sec_remaining % 60);
2396490d05b9SMatthew Ahrens 
2397490d05b9SMatthew Ahrens 		zcb->zcb_lastprint = now;
2398490d05b9SMatthew Ahrens 	}
2399490d05b9SMatthew Ahrens 
2400b24ab676SJeff Bonwick 	return (0);
2401b24ab676SJeff Bonwick }
2402b24ab676SJeff Bonwick 
2403fa9e4066Sahrens static void
24040713e232SGeorge Wilson zdb_leak(void *arg, uint64_t start, uint64_t size)
2405fa9e4066Sahrens {
24060713e232SGeorge Wilson 	vdev_t *vd = arg;
2407fa9e4066Sahrens 
2408fa9e4066Sahrens 	(void) printf("leaked space: vdev %llu, offset 0x%llx, size %llu\n",
2409e14bb325SJeff Bonwick 	    (u_longlong_t)vd->vdev_id, (u_longlong_t)start, (u_longlong_t)size);
2410e14bb325SJeff Bonwick }
2411e14bb325SJeff Bonwick 
24120713e232SGeorge Wilson static metaslab_ops_t zdb_metaslab_ops = {
24132e4c9986SGeorge Wilson 	NULL	/* alloc */
2414e14bb325SJeff Bonwick };
2415e14bb325SJeff Bonwick 
2416e14bb325SJeff Bonwick static void
2417bbfd46c4SJeff Bonwick zdb_ddt_leak_init(spa_t *spa, zdb_cb_t *zcb)
2418fa9e4066Sahrens {
2419bbfd46c4SJeff Bonwick 	ddt_bookmark_t ddb = { 0 };
2420b24ab676SJeff Bonwick 	ddt_entry_t dde;
2421b24ab676SJeff Bonwick 	int error;
2422fa9e4066Sahrens 
2423bbfd46c4SJeff Bonwick 	while ((error = ddt_walk(spa, &ddb, &dde)) == 0) {
2424b24ab676SJeff Bonwick 		blkptr_t blk;
2425b24ab676SJeff Bonwick 		ddt_phys_t *ddp = dde.dde_phys;
2426bbfd46c4SJeff Bonwick 
2427bbfd46c4SJeff Bonwick 		if (ddb.ddb_class == DDT_CLASS_UNIQUE)
2428bbfd46c4SJeff Bonwick 			return;
2429bbfd46c4SJeff Bonwick 
2430b24ab676SJeff Bonwick 		ASSERT(ddt_phys_total_refcnt(&dde) > 1);
2431bbfd46c4SJeff Bonwick 
2432b24ab676SJeff Bonwick 		for (int p = 0; p < DDT_PHYS_TYPES; p++, ddp++) {
2433b24ab676SJeff Bonwick 			if (ddp->ddp_phys_birth == 0)
2434b24ab676SJeff Bonwick 				continue;
2435bbfd46c4SJeff Bonwick 			ddt_bp_create(ddb.ddb_checksum,
2436bbfd46c4SJeff Bonwick 			    &dde.dde_key, ddp, &blk);
2437b24ab676SJeff Bonwick 			if (p == DDT_PHYS_DITTO) {
2438cde58dbcSMatthew Ahrens 				zdb_count_block(zcb, NULL, &blk, ZDB_OT_DITTO);
2439b24ab676SJeff Bonwick 			} else {
2440b24ab676SJeff Bonwick 				zcb->zcb_dedup_asize +=
2441b24ab676SJeff Bonwick 				    BP_GET_ASIZE(&blk) * (ddp->ddp_refcnt - 1);
2442b24ab676SJeff Bonwick 				zcb->zcb_dedup_blocks++;
2443b24ab676SJeff Bonwick 			}
2444b24ab676SJeff Bonwick 		}
2445b24ab676SJeff Bonwick 		if (!dump_opt['L']) {
2446bbfd46c4SJeff Bonwick 			ddt_t *ddt = spa->spa_ddt[ddb.ddb_checksum];
2447b24ab676SJeff Bonwick 			ddt_enter(ddt);
2448b24ab676SJeff Bonwick 			VERIFY(ddt_lookup(ddt, &blk, B_TRUE) != NULL);
2449b24ab676SJeff Bonwick 			ddt_exit(ddt);
2450b24ab676SJeff Bonwick 		}
2451b24ab676SJeff Bonwick 	}
2452b24ab676SJeff Bonwick 
2453b24ab676SJeff Bonwick 	ASSERT(error == ENOENT);
2454b24ab676SJeff Bonwick }
2455b24ab676SJeff Bonwick 
2456b24ab676SJeff Bonwick static void
2457b24ab676SJeff Bonwick zdb_leak_init(spa_t *spa, zdb_cb_t *zcb)
2458b24ab676SJeff Bonwick {
2459cde58dbcSMatthew Ahrens 	zcb->zcb_spa = spa;
2460cde58dbcSMatthew Ahrens 
2461b24ab676SJeff Bonwick 	if (!dump_opt['L']) {
2462b24ab676SJeff Bonwick 		vdev_t *rvd = spa->spa_root_vdev;
246306be9802SMatthew Ahrens 		for (uint64_t c = 0; c < rvd->vdev_children; c++) {
2464e14bb325SJeff Bonwick 			vdev_t *vd = rvd->vdev_child[c];
246506be9802SMatthew Ahrens 			for (uint64_t m = 0; m < vd->vdev_ms_count; m++) {
2466fa9e4066Sahrens 				metaslab_t *msp = vd->vdev_ms[m];
2467fa9e4066Sahrens 				mutex_enter(&msp->ms_lock);
24680713e232SGeorge Wilson 				metaslab_unload(msp);
24690713e232SGeorge Wilson 
24700713e232SGeorge Wilson 				/*
24710713e232SGeorge Wilson 				 * For leak detection, we overload the metaslab
24720713e232SGeorge Wilson 				 * ms_tree to contain allocated segments
24730713e232SGeorge Wilson 				 * instead of free segments. As a result,
24740713e232SGeorge Wilson 				 * we can't use the normal metaslab_load/unload
24750713e232SGeorge Wilson 				 * interfaces.
24760713e232SGeorge Wilson 				 */
24770713e232SGeorge Wilson 				if (msp->ms_sm != NULL) {
247806be9802SMatthew Ahrens 					(void) fprintf(stderr,
247906be9802SMatthew Ahrens 					    "\rloading space map for "
248006be9802SMatthew Ahrens 					    "vdev %llu of %llu, "
248106be9802SMatthew Ahrens 					    "metaslab %llu of %llu ...",
248206be9802SMatthew Ahrens 					    (longlong_t)c,
248306be9802SMatthew Ahrens 					    (longlong_t)rvd->vdev_children,
248406be9802SMatthew Ahrens 					    (longlong_t)m,
248506be9802SMatthew Ahrens 					    (longlong_t)vd->vdev_ms_count);
248606be9802SMatthew Ahrens 
24870713e232SGeorge Wilson 					msp->ms_ops = &zdb_metaslab_ops;
2488f7950bf1SMatthew Ahrens 
2489f7950bf1SMatthew Ahrens 					/*
2490f7950bf1SMatthew Ahrens 					 * We don't want to spend the CPU
2491f7950bf1SMatthew Ahrens 					 * manipulating the size-ordered
2492f7950bf1SMatthew Ahrens 					 * tree, so clear the range_tree
2493f7950bf1SMatthew Ahrens 					 * ops.
2494f7950bf1SMatthew Ahrens 					 */
2495f7950bf1SMatthew Ahrens 					msp->ms_tree->rt_ops = NULL;
24960713e232SGeorge Wilson 					VERIFY0(space_map_load(msp->ms_sm,
24970713e232SGeorge Wilson 					    msp->ms_tree, SM_ALLOC));
24980713e232SGeorge Wilson 					msp->ms_loaded = B_TRUE;
24990713e232SGeorge Wilson 				}
2500fa9e4066Sahrens 				mutex_exit(&msp->ms_lock);
2501fa9e4066Sahrens 			}
2502fa9e4066Sahrens 		}
250306be9802SMatthew Ahrens 		(void) fprintf(stderr, "\n");
2504fa9e4066Sahrens 	}
2505fa9e4066Sahrens 
2506b24ab676SJeff Bonwick 	spa_config_enter(spa, SCL_CONFIG, FTAG, RW_READER);
2507b24ab676SJeff Bonwick 
2508bbfd46c4SJeff Bonwick 	zdb_ddt_leak_init(spa, zcb);
2509b24ab676SJeff Bonwick 
2510b24ab676SJeff Bonwick 	spa_config_exit(spa, SCL_CONFIG, FTAG);
2511b24ab676SJeff Bonwick }
2512b24ab676SJeff Bonwick 
2513fa9e4066Sahrens static void
2514e14bb325SJeff Bonwick zdb_leak_fini(spa_t *spa)
2515fa9e4066Sahrens {
2516b24ab676SJeff Bonwick 	if (!dump_opt['L']) {
2517fa9e4066Sahrens 		vdev_t *rvd = spa->spa_root_vdev;
2518e14bb325SJeff Bonwick 		for (int c = 0; c < rvd->vdev_children; c++) {
2519e14bb325SJeff Bonwick 			vdev_t *vd = rvd->vdev_child[c];
2520e14bb325SJeff Bonwick 			for (int m = 0; m < vd->vdev_ms_count; m++) {
2521e14bb325SJeff Bonwick 				metaslab_t *msp = vd->vdev_ms[m];
2522e14bb325SJeff Bonwick 				mutex_enter(&msp->ms_lock);
25230713e232SGeorge Wilson 
25240713e232SGeorge Wilson 				/*
25250713e232SGeorge Wilson 				 * The ms_tree has been overloaded to
25260713e232SGeorge Wilson 				 * contain allocated segments. Now that we
25270713e232SGeorge Wilson 				 * finished traversing all blocks, any
25280713e232SGeorge Wilson 				 * block that remains in the ms_tree
25290713e232SGeorge Wilson 				 * represents an allocated block that we
25300713e232SGeorge Wilson 				 * did not claim during the traversal.
25310713e232SGeorge Wilson 				 * Claimed blocks would have been removed
25320713e232SGeorge Wilson 				 * from the ms_tree.
25330713e232SGeorge Wilson 				 */
25340713e232SGeorge Wilson 				range_tree_vacate(msp->ms_tree, zdb_leak, vd);
25350713e232SGeorge Wilson 				msp->ms_loaded = B_FALSE;
25360713e232SGeorge Wilson 
2537e14bb325SJeff Bonwick 				mutex_exit(&msp->ms_lock);
2538e14bb325SJeff Bonwick 			}
2539e14bb325SJeff Bonwick 		}
2540fa9e4066Sahrens 	}
2541fa9e4066Sahrens }
2542fa9e4066Sahrens 
2543cde58dbcSMatthew Ahrens /* ARGSUSED */
2544cde58dbcSMatthew Ahrens static int
2545cde58dbcSMatthew Ahrens count_block_cb(void *arg, const blkptr_t *bp, dmu_tx_t *tx)
2546cde58dbcSMatthew Ahrens {
2547cde58dbcSMatthew Ahrens 	zdb_cb_t *zcb = arg;
2548cde58dbcSMatthew Ahrens 
2549490d05b9SMatthew Ahrens 	if (dump_opt['b'] >= 5) {
2550cde58dbcSMatthew Ahrens 		char blkbuf[BP_SPRINTF_LEN];
255143466aaeSMax Grossman 		snprintf_blkptr(blkbuf, sizeof (blkbuf), bp);
2552cde58dbcSMatthew Ahrens 		(void) printf("[%s] %s\n",
2553cde58dbcSMatthew Ahrens 		    "deferred free", blkbuf);
2554cde58dbcSMatthew Ahrens 	}
2555cde58dbcSMatthew Ahrens 	zdb_count_block(zcb, NULL, bp, ZDB_OT_DEFERRED);
2556cde58dbcSMatthew Ahrens 	return (0);
2557cde58dbcSMatthew Ahrens }
2558cde58dbcSMatthew Ahrens 
2559fa9e4066Sahrens static int
2560fa9e4066Sahrens dump_block_stats(spa_t *spa)
2561fa9e4066Sahrens {
2562fa9e4066Sahrens 	zdb_cb_t zcb = { 0 };
2563fa9e4066Sahrens 	zdb_blkstats_t *zb, *tzb;
2564b24ab676SJeff Bonwick 	uint64_t norm_alloc, norm_space, total_alloc, total_found;
2565cd088ea4SVictor Latushkin 	int flags = TRAVERSE_PRE | TRAVERSE_PREFETCH_METADATA | TRAVERSE_HARD;
25665d7b4d43SMatthew Ahrens 	boolean_t leaks = B_FALSE;
2567fa9e4066Sahrens 
2568490d05b9SMatthew Ahrens 	(void) printf("\nTraversing all blocks %s%s%s%s%s...\n\n",
256982a0a985SVictor Latushkin 	    (dump_opt['c'] || !dump_opt['L']) ? "to verify " : "",
25706365109dSVictor Latushkin 	    (dump_opt['c'] == 1) ? "metadata " : "",
257182a0a985SVictor Latushkin 	    dump_opt['c'] ? "checksums " : "",
257282a0a985SVictor Latushkin 	    (dump_opt['c'] && !dump_opt['L']) ? "and verify " : "",
257382a0a985SVictor Latushkin 	    !dump_opt['L'] ? "nothing leaked " : "");
2574fa9e4066Sahrens 
2575fa9e4066Sahrens 	/*
2576e14bb325SJeff Bonwick 	 * Load all space maps as SM_ALLOC maps, then traverse the pool
2577e14bb325SJeff Bonwick 	 * claiming each block we discover.  If the pool is perfectly
2578e14bb325SJeff Bonwick 	 * consistent, the space maps will be empty when we're done.
2579e14bb325SJeff Bonwick 	 * Anything left over is a leak; any block we can't claim (because
2580e14bb325SJeff Bonwick 	 * it's not part of any space map) is a double allocation,
2581e14bb325SJeff Bonwick 	 * reference to a freed block, or an unclaimed log block.
2582fa9e4066Sahrens 	 */
2583b24ab676SJeff Bonwick 	zdb_leak_init(spa, &zcb);
2584fa9e4066Sahrens 
2585fa9e4066Sahrens 	/*
2586fa9e4066Sahrens 	 * If there's a deferred-free bplist, process that first.
2587fa9e4066Sahrens 	 */
2588cde58dbcSMatthew Ahrens 	(void) bpobj_iterate_nofree(&spa->spa_deferred_bpobj,
2589cde58dbcSMatthew Ahrens 	    count_block_cb, &zcb, NULL);
25903b2aab18SMatthew Ahrens 	if (spa_version(spa) >= SPA_VERSION_DEADLISTS) {
2591cde58dbcSMatthew Ahrens 		(void) bpobj_iterate_nofree(&spa->spa_dsl_pool->dp_free_bpobj,
2592cde58dbcSMatthew Ahrens 		    count_block_cb, &zcb, NULL);
25933b2aab18SMatthew Ahrens 	}
25942acef22dSMatthew Ahrens 	if (spa_feature_is_active(spa, SPA_FEATURE_ASYNC_DESTROY)) {
2595b420f3adSRichard Lowe 		VERIFY3U(0, ==, bptree_iterate(spa->spa_meta_objset,
2596ad135b5dSChristopher Siden 		    spa->spa_dsl_pool->dp_bptree_obj, B_FALSE, count_block_cb,
2597ad135b5dSChristopher Siden 		    &zcb, NULL));
2598ad135b5dSChristopher Siden 	}
2599fa9e4066Sahrens 
2600bbfd46c4SJeff Bonwick 	if (dump_opt['c'] > 1)
2601bbfd46c4SJeff Bonwick 		flags |= TRAVERSE_PREFETCH_DATA;
2602bbfd46c4SJeff Bonwick 
2603490d05b9SMatthew Ahrens 	zcb.zcb_totalasize = metaslab_class_get_alloc(spa_normal_class(spa));
2604490d05b9SMatthew Ahrens 	zcb.zcb_start = zcb.zcb_lastprint = gethrtime();
2605bbfd46c4SJeff Bonwick 	zcb.zcb_haderrors |= traverse_pool(spa, 0, flags, zdb_blkptr_cb, &zcb);
2606fa9e4066Sahrens 
260731d7e8faSGeorge Wilson 	/*
260831d7e8faSGeorge Wilson 	 * If we've traversed the data blocks then we need to wait for those
260931d7e8faSGeorge Wilson 	 * I/Os to complete. We leverage "The Godfather" zio to wait on
261031d7e8faSGeorge Wilson 	 * all async I/Os to complete.
261131d7e8faSGeorge Wilson 	 */
261231d7e8faSGeorge Wilson 	if (dump_opt['c']) {
26136f834bc1SMatthew Ahrens 		for (int i = 0; i < max_ncpus; i++) {
26146f834bc1SMatthew Ahrens 			(void) zio_wait(spa->spa_async_zio_root[i]);
26156f834bc1SMatthew Ahrens 			spa->spa_async_zio_root[i] = zio_root(spa, NULL, NULL,
261631d7e8faSGeorge Wilson 			    ZIO_FLAG_CANFAIL | ZIO_FLAG_SPECULATIVE |
261731d7e8faSGeorge Wilson 			    ZIO_FLAG_GODFATHER);
261831d7e8faSGeorge Wilson 		}
26196f834bc1SMatthew Ahrens 	}
262031d7e8faSGeorge Wilson 
2621b24ab676SJeff Bonwick 	if (zcb.zcb_haderrors) {
2622fa9e4066Sahrens 		(void) printf("\nError counts:\n\n");
2623fa9e4066Sahrens 		(void) printf("\t%5s  %s\n", "errno", "count");
2624b24ab676SJeff Bonwick 		for (int e = 0; e < 256; e++) {
2625fa9e4066Sahrens 			if (zcb.zcb_errors[e] != 0) {
2626fa9e4066Sahrens 				(void) printf("\t%5d  %llu\n",
2627fa9e4066Sahrens 				    e, (u_longlong_t)zcb.zcb_errors[e]);
2628fa9e4066Sahrens 			}
2629fa9e4066Sahrens 		}
2630fa9e4066Sahrens 	}
2631fa9e4066Sahrens 
2632fa9e4066Sahrens 	/*
2633fa9e4066Sahrens 	 * Report any leaked segments.
2634fa9e4066Sahrens 	 */
2635e14bb325SJeff Bonwick 	zdb_leak_fini(spa);
2636fa9e4066Sahrens 
2637b24ab676SJeff Bonwick 	tzb = &zcb.zcb_type[ZB_TOTAL][ZDB_OT_TOTAL];
2638d41e7643Sek110237 
2639b24ab676SJeff Bonwick 	norm_alloc = metaslab_class_get_alloc(spa_normal_class(spa));
2640b24ab676SJeff Bonwick 	norm_space = metaslab_class_get_space(spa_normal_class(spa));
2641fa9e4066Sahrens 
2642b24ab676SJeff Bonwick 	total_alloc = norm_alloc + metaslab_class_get_alloc(spa_log_class(spa));
2643b24ab676SJeff Bonwick 	total_found = tzb->zb_asize - zcb.zcb_dedup_asize;
26448654d025Sperrin 
2645b24ab676SJeff Bonwick 	if (total_found == total_alloc) {
264682a0a985SVictor Latushkin 		if (!dump_opt['L'])
2647fa9e4066Sahrens 			(void) printf("\n\tNo leaks (block sum matches space"
2648fa9e4066Sahrens 			    " maps exactly)\n");
2649fa9e4066Sahrens 	} else {
2650fa9e4066Sahrens 		(void) printf("block traversal size %llu != alloc %llu "
265182a0a985SVictor Latushkin 		    "(%s %lld)\n",
2652b24ab676SJeff Bonwick 		    (u_longlong_t)total_found,
2653b24ab676SJeff Bonwick 		    (u_longlong_t)total_alloc,
265482a0a985SVictor Latushkin 		    (dump_opt['L']) ? "unreachable" : "leaked",
2655b24ab676SJeff Bonwick 		    (longlong_t)(total_alloc - total_found));
26565d7b4d43SMatthew Ahrens 		leaks = B_TRUE;
2657fa9e4066Sahrens 	}
2658fa9e4066Sahrens 
2659fa9e4066Sahrens 	if (tzb->zb_count == 0)
2660fa9e4066Sahrens 		return (2);
2661fa9e4066Sahrens 
2662fa9e4066Sahrens 	(void) printf("\n");
2663fa9e4066Sahrens 	(void) printf("\tbp count:      %10llu\n",
2664fa9e4066Sahrens 	    (u_longlong_t)tzb->zb_count);
2665d5ee8a13SMatthew Ahrens 	(void) printf("\tganged count:  %10llu\n",
2666d5ee8a13SMatthew Ahrens 	    (longlong_t)tzb->zb_gangs);
2667b24ab676SJeff Bonwick 	(void) printf("\tbp logical:    %10llu      avg: %6llu\n",
2668fa9e4066Sahrens 	    (u_longlong_t)tzb->zb_lsize,
2669fa9e4066Sahrens 	    (u_longlong_t)(tzb->zb_lsize / tzb->zb_count));
2670b24ab676SJeff Bonwick 	(void) printf("\tbp physical:   %10llu      avg:"
2671b24ab676SJeff Bonwick 	    " %6llu     compression: %6.2f\n",
2672fa9e4066Sahrens 	    (u_longlong_t)tzb->zb_psize,
2673fa9e4066Sahrens 	    (u_longlong_t)(tzb->zb_psize / tzb->zb_count),
2674fa9e4066Sahrens 	    (double)tzb->zb_lsize / tzb->zb_psize);
2675b24ab676SJeff Bonwick 	(void) printf("\tbp allocated:  %10llu      avg:"
2676b24ab676SJeff Bonwick 	    " %6llu     compression: %6.2f\n",
2677fa9e4066Sahrens 	    (u_longlong_t)tzb->zb_asize,
2678fa9e4066Sahrens 	    (u_longlong_t)(tzb->zb_asize / tzb->zb_count),
2679fa9e4066Sahrens 	    (double)tzb->zb_lsize / tzb->zb_asize);
2680b24ab676SJeff Bonwick 	(void) printf("\tbp deduped:    %10llu    ref>1:"
2681b24ab676SJeff Bonwick 	    " %6llu   deduplication: %6.2f\n",
2682b24ab676SJeff Bonwick 	    (u_longlong_t)zcb.zcb_dedup_asize,
2683b24ab676SJeff Bonwick 	    (u_longlong_t)zcb.zcb_dedup_blocks,
2684b24ab676SJeff Bonwick 	    (double)zcb.zcb_dedup_asize / tzb->zb_asize + 1.0);
2685b24ab676SJeff Bonwick 	(void) printf("\tSPA allocated: %10llu     used: %5.2f%%\n",
2686b24ab676SJeff Bonwick 	    (u_longlong_t)norm_alloc, 100.0 * norm_alloc / norm_space);
2687fa9e4066Sahrens 
26885d7b4d43SMatthew Ahrens 	for (bp_embedded_type_t i = 0; i < NUM_BP_EMBEDDED_TYPES; i++) {
26895d7b4d43SMatthew Ahrens 		if (zcb.zcb_embedded_blocks[i] == 0)
26905d7b4d43SMatthew Ahrens 			continue;
26915d7b4d43SMatthew Ahrens 		(void) printf("\n");
26925d7b4d43SMatthew Ahrens 		(void) printf("\tadditional, non-pointer bps of type %u: "
26935d7b4d43SMatthew Ahrens 		    "%10llu\n",
26945d7b4d43SMatthew Ahrens 		    i, (u_longlong_t)zcb.zcb_embedded_blocks[i]);
26955d7b4d43SMatthew Ahrens 
26965d7b4d43SMatthew Ahrens 		if (dump_opt['b'] >= 3) {
26975d7b4d43SMatthew Ahrens 			(void) printf("\t number of (compressed) bytes:  "
26985d7b4d43SMatthew Ahrens 			    "number of bps\n");
26995d7b4d43SMatthew Ahrens 			dump_histogram(zcb.zcb_embedded_histogram[i],
27005d7b4d43SMatthew Ahrens 			    sizeof (zcb.zcb_embedded_histogram[i]) /
27015d7b4d43SMatthew Ahrens 			    sizeof (zcb.zcb_embedded_histogram[i][0]), 0);
27025d7b4d43SMatthew Ahrens 		}
27035d7b4d43SMatthew Ahrens 	}
27045d7b4d43SMatthew Ahrens 
2705d5ee8a13SMatthew Ahrens 	if (tzb->zb_ditto_samevdev != 0) {
2706d5ee8a13SMatthew Ahrens 		(void) printf("\tDittoed blocks on same vdev: %llu\n",
2707d5ee8a13SMatthew Ahrens 		    (longlong_t)tzb->zb_ditto_samevdev);
2708d5ee8a13SMatthew Ahrens 	}
2709d5ee8a13SMatthew Ahrens 
2710fa9e4066Sahrens 	if (dump_opt['b'] >= 2) {
2711fa9e4066Sahrens 		int l, t, level;
2712fa9e4066Sahrens 		(void) printf("\nBlocks\tLSIZE\tPSIZE\tASIZE"
2713fa9e4066Sahrens 		    "\t  avg\t comp\t%%Total\tType\n");
2714fa9e4066Sahrens 
2715b24ab676SJeff Bonwick 		for (t = 0; t <= ZDB_OT_TOTAL; t++) {
27163f9d6ad7SLin Ling 			char csize[32], lsize[32], psize[32], asize[32];
2717d5ee8a13SMatthew Ahrens 			char avg[32], gang[32];
2718fa9e4066Sahrens 			char *typename;
2719fa9e4066Sahrens 
2720b24ab676SJeff Bonwick 			if (t < DMU_OT_NUMTYPES)
2721b24ab676SJeff Bonwick 				typename = dmu_ot[t].ot_name;
2722b24ab676SJeff Bonwick 			else
2723b24ab676SJeff Bonwick 				typename = zdb_ot_extname[t - DMU_OT_NUMTYPES];
2724fa9e4066Sahrens 
2725fa9e4066Sahrens 			if (zcb.zcb_type[ZB_TOTAL][t].zb_asize == 0) {
2726fa9e4066Sahrens 				(void) printf("%6s\t%5s\t%5s\t%5s"
2727fa9e4066Sahrens 				    "\t%5s\t%5s\t%6s\t%s\n",
2728fa9e4066Sahrens 				    "-",
2729fa9e4066Sahrens 				    "-",
2730fa9e4066Sahrens 				    "-",
2731fa9e4066Sahrens 				    "-",
2732fa9e4066Sahrens 				    "-",
2733fa9e4066Sahrens 				    "-",
2734fa9e4066Sahrens 				    "-",
2735fa9e4066Sahrens 				    typename);
2736fa9e4066Sahrens 				continue;
2737fa9e4066Sahrens 			}
2738fa9e4066Sahrens 
2739fa9e4066Sahrens 			for (l = ZB_TOTAL - 1; l >= -1; l--) {
2740fa9e4066Sahrens 				level = (l == -1 ? ZB_TOTAL : l);
2741fa9e4066Sahrens 				zb = &zcb.zcb_type[level][t];
2742fa9e4066Sahrens 
2743fa9e4066Sahrens 				if (zb->zb_asize == 0)
2744fa9e4066Sahrens 					continue;
2745fa9e4066Sahrens 
2746fa9e4066Sahrens 				if (dump_opt['b'] < 3 && level != ZB_TOTAL)
2747fa9e4066Sahrens 					continue;
2748fa9e4066Sahrens 
2749fa9e4066Sahrens 				if (level == 0 && zb->zb_asize ==
2750fa9e4066Sahrens 				    zcb.zcb_type[ZB_TOTAL][t].zb_asize)
2751fa9e4066Sahrens 					continue;
2752fa9e4066Sahrens 
27533f9d6ad7SLin Ling 				zdb_nicenum(zb->zb_count, csize);
27543f9d6ad7SLin Ling 				zdb_nicenum(zb->zb_lsize, lsize);
27553f9d6ad7SLin Ling 				zdb_nicenum(zb->zb_psize, psize);
27563f9d6ad7SLin Ling 				zdb_nicenum(zb->zb_asize, asize);
27573f9d6ad7SLin Ling 				zdb_nicenum(zb->zb_asize / zb->zb_count, avg);
2758d5ee8a13SMatthew Ahrens 				zdb_nicenum(zb->zb_gangs, gang);
2759fa9e4066Sahrens 
2760fa9e4066Sahrens 				(void) printf("%6s\t%5s\t%5s\t%5s\t%5s"
2761fa9e4066Sahrens 				    "\t%5.2f\t%6.2f\t",
2762fa9e4066Sahrens 				    csize, lsize, psize, asize, avg,
2763fa9e4066Sahrens 				    (double)zb->zb_lsize / zb->zb_psize,
2764fa9e4066Sahrens 				    100.0 * zb->zb_asize / tzb->zb_asize);
2765fa9e4066Sahrens 
2766fa9e4066Sahrens 				if (level == ZB_TOTAL)
2767fa9e4066Sahrens 					(void) printf("%s\n", typename);
2768fa9e4066Sahrens 				else
2769fa9e4066Sahrens 					(void) printf("    L%d %s\n",
2770fa9e4066Sahrens 					    level, typename);
2771490d05b9SMatthew Ahrens 
2772d5ee8a13SMatthew Ahrens 				if (dump_opt['b'] >= 3 && zb->zb_gangs > 0) {
2773d5ee8a13SMatthew Ahrens 					(void) printf("\t number of ganged "
2774d5ee8a13SMatthew Ahrens 					    "blocks: %s\n", gang);
2775d5ee8a13SMatthew Ahrens 				}
2776d5ee8a13SMatthew Ahrens 
2777490d05b9SMatthew Ahrens 				if (dump_opt['b'] >= 4) {
2778490d05b9SMatthew Ahrens 					(void) printf("psize "
2779490d05b9SMatthew Ahrens 					    "(in 512-byte sectors): "
2780490d05b9SMatthew Ahrens 					    "number of blocks\n");
2781490d05b9SMatthew Ahrens 					dump_histogram(zb->zb_psize_histogram,
27820713e232SGeorge Wilson 					    PSIZE_HISTO_SIZE, 0);
2783490d05b9SMatthew Ahrens 				}
2784fa9e4066Sahrens 			}
2785fa9e4066Sahrens 		}
2786fa9e4066Sahrens 	}
2787fa9e4066Sahrens 
2788fa9e4066Sahrens 	(void) printf("\n");
2789fa9e4066Sahrens 
2790fa9e4066Sahrens 	if (leaks)
2791fa9e4066Sahrens 		return (2);
2792fa9e4066Sahrens 
2793fa9e4066Sahrens 	if (zcb.zcb_haderrors)
2794fa9e4066Sahrens 		return (3);
2795fa9e4066Sahrens 
2796fa9e4066Sahrens 	return (0);
2797fa9e4066Sahrens }
2798fa9e4066Sahrens 
2799b24ab676SJeff Bonwick typedef struct zdb_ddt_entry {
2800b24ab676SJeff Bonwick 	ddt_key_t	zdde_key;
2801b24ab676SJeff Bonwick 	uint64_t	zdde_ref_blocks;
2802b24ab676SJeff Bonwick 	uint64_t	zdde_ref_lsize;
2803b24ab676SJeff Bonwick 	uint64_t	zdde_ref_psize;
2804b24ab676SJeff Bonwick 	uint64_t	zdde_ref_dsize;
2805b24ab676SJeff Bonwick 	avl_node_t	zdde_node;
2806b24ab676SJeff Bonwick } zdb_ddt_entry_t;
2807b24ab676SJeff Bonwick 
2808b24ab676SJeff Bonwick /* ARGSUSED */
2809b24ab676SJeff Bonwick static int
2810b24ab676SJeff Bonwick zdb_ddt_add_cb(spa_t *spa, zilog_t *zilog, const blkptr_t *bp,
28117802d7bfSMatthew Ahrens     const zbookmark_phys_t *zb, const dnode_phys_t *dnp, void *arg)
2812b24ab676SJeff Bonwick {
2813b24ab676SJeff Bonwick 	avl_tree_t *t = arg;
2814b24ab676SJeff Bonwick 	avl_index_t where;
2815b24ab676SJeff Bonwick 	zdb_ddt_entry_t *zdde, zdde_search;
2816b24ab676SJeff Bonwick 
28175d7b4d43SMatthew Ahrens 	if (BP_IS_HOLE(bp) || BP_IS_EMBEDDED(bp))
2818b24ab676SJeff Bonwick 		return (0);
2819b24ab676SJeff Bonwick 
2820b24ab676SJeff Bonwick 	if (dump_opt['S'] > 1 && zb->zb_level == ZB_ROOT_LEVEL) {
2821b24ab676SJeff Bonwick 		(void) printf("traversing objset %llu, %llu objects, "
2822b24ab676SJeff Bonwick 		    "%lu blocks so far\n",
2823b24ab676SJeff Bonwick 		    (u_longlong_t)zb->zb_objset,
28245d7b4d43SMatthew Ahrens 		    (u_longlong_t)BP_GET_FILL(bp),
2825b24ab676SJeff Bonwick 		    avl_numnodes(t));
2826b24ab676SJeff Bonwick 	}
2827b24ab676SJeff Bonwick 
2828bbfd46c4SJeff Bonwick 	if (BP_IS_HOLE(bp) || BP_GET_CHECKSUM(bp) == ZIO_CHECKSUM_OFF ||
2829ad135b5dSChristopher Siden 	    BP_GET_LEVEL(bp) > 0 || DMU_OT_IS_METADATA(BP_GET_TYPE(bp)))
2830b24ab676SJeff Bonwick 		return (0);
2831b24ab676SJeff Bonwick 
2832b24ab676SJeff Bonwick 	ddt_key_fill(&zdde_search.zdde_key, bp);
2833b24ab676SJeff Bonwick 
2834b24ab676SJeff Bonwick 	zdde = avl_find(t, &zdde_search, &where);
2835b24ab676SJeff Bonwick 
2836b24ab676SJeff Bonwick 	if (zdde == NULL) {
2837b24ab676SJeff Bonwick 		zdde = umem_zalloc(sizeof (*zdde), UMEM_NOFAIL);
2838b24ab676SJeff Bonwick 		zdde->zdde_key = zdde_search.zdde_key;
2839b24ab676SJeff Bonwick 		avl_insert(t, zdde, where);
2840b24ab676SJeff Bonwick 	}
2841b24ab676SJeff Bonwick 
2842b24ab676SJeff Bonwick 	zdde->zdde_ref_blocks += 1;
2843b24ab676SJeff Bonwick 	zdde->zdde_ref_lsize += BP_GET_LSIZE(bp);
2844b24ab676SJeff Bonwick 	zdde->zdde_ref_psize += BP_GET_PSIZE(bp);
2845b24ab676SJeff Bonwick 	zdde->zdde_ref_dsize += bp_get_dsize_sync(spa, bp);
2846b24ab676SJeff Bonwick 
2847b24ab676SJeff Bonwick 	return (0);
2848b24ab676SJeff Bonwick }
2849b24ab676SJeff Bonwick 
2850b24ab676SJeff Bonwick static void
2851b24ab676SJeff Bonwick dump_simulated_ddt(spa_t *spa)
2852b24ab676SJeff Bonwick {
2853b24ab676SJeff Bonwick 	avl_tree_t t;
2854b24ab676SJeff Bonwick 	void *cookie = NULL;
2855b24ab676SJeff Bonwick 	zdb_ddt_entry_t *zdde;
2856b24ab676SJeff Bonwick 	ddt_histogram_t ddh_total = { 0 };
2857b24ab676SJeff Bonwick 	ddt_stat_t dds_total = { 0 };
2858b24ab676SJeff Bonwick 
2859b24ab676SJeff Bonwick 	avl_create(&t, ddt_entry_compare,
2860b24ab676SJeff Bonwick 	    sizeof (zdb_ddt_entry_t), offsetof(zdb_ddt_entry_t, zdde_node));
2861b24ab676SJeff Bonwick 
2862b24ab676SJeff Bonwick 	spa_config_enter(spa, SCL_CONFIG, FTAG, RW_READER);
2863b24ab676SJeff Bonwick 
2864bbfd46c4SJeff Bonwick 	(void) traverse_pool(spa, 0, TRAVERSE_PRE | TRAVERSE_PREFETCH_METADATA,
2865bbfd46c4SJeff Bonwick 	    zdb_ddt_add_cb, &t);
2866b24ab676SJeff Bonwick 
2867b24ab676SJeff Bonwick 	spa_config_exit(spa, SCL_CONFIG, FTAG);
2868b24ab676SJeff Bonwick 
2869b24ab676SJeff Bonwick 	while ((zdde = avl_destroy_nodes(&t, &cookie)) != NULL) {
2870b24ab676SJeff Bonwick 		ddt_stat_t dds;
2871b24ab676SJeff Bonwick 		uint64_t refcnt = zdde->zdde_ref_blocks;
2872b24ab676SJeff Bonwick 		ASSERT(refcnt != 0);
2873b24ab676SJeff Bonwick 
2874b24ab676SJeff Bonwick 		dds.dds_blocks = zdde->zdde_ref_blocks / refcnt;
2875b24ab676SJeff Bonwick 		dds.dds_lsize = zdde->zdde_ref_lsize / refcnt;
2876b24ab676SJeff Bonwick 		dds.dds_psize = zdde->zdde_ref_psize / refcnt;
2877b24ab676SJeff Bonwick 		dds.dds_dsize = zdde->zdde_ref_dsize / refcnt;
2878b24ab676SJeff Bonwick 
2879b24ab676SJeff Bonwick 		dds.dds_ref_blocks = zdde->zdde_ref_blocks;
2880b24ab676SJeff Bonwick 		dds.dds_ref_lsize = zdde->zdde_ref_lsize;
2881b24ab676SJeff Bonwick 		dds.dds_ref_psize = zdde->zdde_ref_psize;
2882b24ab676SJeff Bonwick 		dds.dds_ref_dsize = zdde->zdde_ref_dsize;
2883b24ab676SJeff Bonwick 
2884bf16b11eSMatthew Ahrens 		ddt_stat_add(&ddh_total.ddh_stat[highbit64(refcnt) - 1],
2885bf16b11eSMatthew Ahrens 		    &dds, 0);
2886b24ab676SJeff Bonwick 
2887b24ab676SJeff Bonwick 		umem_free(zdde, sizeof (*zdde));
2888b24ab676SJeff Bonwick 	}
2889b24ab676SJeff Bonwick 
2890b24ab676SJeff Bonwick 	avl_destroy(&t);
2891b24ab676SJeff Bonwick 
2892b24ab676SJeff Bonwick 	ddt_histogram_stat(&dds_total, &ddh_total);
2893b24ab676SJeff Bonwick 
2894b24ab676SJeff Bonwick 	(void) printf("Simulated DDT histogram:\n");
2895b24ab676SJeff Bonwick 
28969eb19f4dSGeorge Wilson 	zpool_dump_ddt(&dds_total, &ddh_total);
2897b24ab676SJeff Bonwick 
2898b24ab676SJeff Bonwick 	dump_dedup_ratio(&dds_total);
2899b24ab676SJeff Bonwick }
2900b24ab676SJeff Bonwick 
2901fa9e4066Sahrens static void
2902fa9e4066Sahrens dump_zpool(spa_t *spa)
2903fa9e4066Sahrens {
2904fa9e4066Sahrens 	dsl_pool_t *dp = spa_get_dsl(spa);
2905fa9e4066Sahrens 	int rc = 0;
2906fa9e4066Sahrens 
2907b24ab676SJeff Bonwick 	if (dump_opt['S']) {
2908b24ab676SJeff Bonwick 		dump_simulated_ddt(spa);
2909b24ab676SJeff Bonwick 		return;
2910b24ab676SJeff Bonwick 	}
2911b24ab676SJeff Bonwick 
291207428bdfSVictor Latushkin 	if (!dump_opt['e'] && dump_opt['C'] > 1) {
291307428bdfSVictor Latushkin 		(void) printf("\nCached configuration:\n");
291407428bdfSVictor Latushkin 		dump_nvlist(spa->spa_config, 8);
291507428bdfSVictor Latushkin 	}
291607428bdfSVictor Latushkin 
291707428bdfSVictor Latushkin 	if (dump_opt['C'])
291807428bdfSVictor Latushkin 		dump_config(spa);
291907428bdfSVictor Latushkin 
2920fa9e4066Sahrens 	if (dump_opt['u'])
292153b9a4a9SVictor Latushkin 		dump_uberblock(&spa->spa_uberblock, "\nUberblock:\n", "\n");
2922fa9e4066Sahrens 
2923b24ab676SJeff Bonwick 	if (dump_opt['D'])
2924b24ab676SJeff Bonwick 		dump_all_ddts(spa);
2925b24ab676SJeff Bonwick 
292687219db7SVictor Latushkin 	if (dump_opt['d'] > 2 || dump_opt['m'])
292787219db7SVictor Latushkin 		dump_metaslabs(spa);
29282e4c9986SGeorge Wilson 	if (dump_opt['M'])
29292e4c9986SGeorge Wilson 		dump_metaslab_groups(spa);
293087219db7SVictor Latushkin 
293187219db7SVictor Latushkin 	if (dump_opt['d'] || dump_opt['i']) {
2932b5152584SMatthew Ahrens 		uint64_t refcount;
2933fa9e4066Sahrens 		dump_dir(dp->dp_meta_objset);
2934fa9e4066Sahrens 		if (dump_opt['d'] >= 3) {
2935d0475637SMatthew Ahrens 			dump_bpobj(&spa->spa_deferred_bpobj,
2936d0475637SMatthew Ahrens 			    "Deferred frees", 0);
2937cde58dbcSMatthew Ahrens 			if (spa_version(spa) >= SPA_VERSION_DEADLISTS) {
2938cde58dbcSMatthew Ahrens 				dump_bpobj(&spa->spa_dsl_pool->dp_free_bpobj,
2939d0475637SMatthew Ahrens 				    "Pool snapshot frees", 0);
2940ad135b5dSChristopher Siden 			}
2941ad135b5dSChristopher Siden 
2942ad135b5dSChristopher Siden 			if (spa_feature_is_active(spa,
29432acef22dSMatthew Ahrens 			    SPA_FEATURE_ASYNC_DESTROY)) {
2944ad135b5dSChristopher Siden 				dump_bptree(spa->spa_meta_objset,
2945ad135b5dSChristopher Siden 				    spa->spa_dsl_pool->dp_bptree_obj,
2946ad135b5dSChristopher Siden 				    "Pool dataset frees");
2947cde58dbcSMatthew Ahrens 			}
2948fa9e4066Sahrens 			dump_dtl(spa->spa_root_vdev, 0);
2949fa9e4066Sahrens 		}
295007428bdfSVictor Latushkin 		(void) dmu_objset_find(spa_name(spa), dump_one_dir,
295107428bdfSVictor Latushkin 		    NULL, DS_FIND_SNAPSHOTS | DS_FIND_CHILDREN);
2952b5152584SMatthew Ahrens 
2953b5152584SMatthew Ahrens 		(void) feature_get_refcount(spa,
2954b5152584SMatthew Ahrens 		    &spa_feature_table[SPA_FEATURE_LARGE_BLOCKS], &refcount);
2955b5152584SMatthew Ahrens 		if (num_large_blocks != refcount) {
2956b5152584SMatthew Ahrens 			(void) printf("large_blocks feature refcount mismatch: "
2957b5152584SMatthew Ahrens 			    "expected %lld != actual %lld\n",
2958b5152584SMatthew Ahrens 			    (longlong_t)num_large_blocks,
2959b5152584SMatthew Ahrens 			    (longlong_t)refcount);
2960b5152584SMatthew Ahrens 			rc = 2;
2961b5152584SMatthew Ahrens 		} else {
2962b5152584SMatthew Ahrens 			(void) printf("Verified large_blocks feature refcount "
2963b5152584SMatthew Ahrens 			    "is correct (%llu)\n", (longlong_t)refcount);
2964fa9e4066Sahrens 		}
2965b5152584SMatthew Ahrens 	}
2966b5152584SMatthew Ahrens 	if (rc == 0 && (dump_opt['b'] || dump_opt['c']))
2967fa9e4066Sahrens 		rc = dump_block_stats(spa);
2968fa9e4066Sahrens 
29690713e232SGeorge Wilson 	if (rc == 0)
29700713e232SGeorge Wilson 		rc = verify_spacemap_refcounts(spa);
29710713e232SGeorge Wilson 
2972fa9e4066Sahrens 	if (dump_opt['s'])
2973fa9e4066Sahrens 		show_pool_stats(spa);
2974fa9e4066Sahrens 
29758f18d1faSGeorge Wilson 	if (dump_opt['h'])
29768f18d1faSGeorge Wilson 		dump_history(spa);
29778f18d1faSGeorge Wilson 
2978fa9e4066Sahrens 	if (rc != 0)
2979fa9e4066Sahrens 		exit(rc);
2980fa9e4066Sahrens }
2981fa9e4066Sahrens 
298244cd46caSbillm #define	ZDB_FLAG_CHECKSUM	0x0001
298344cd46caSbillm #define	ZDB_FLAG_DECOMPRESS	0x0002
298444cd46caSbillm #define	ZDB_FLAG_BSWAP		0x0004
298544cd46caSbillm #define	ZDB_FLAG_GBH		0x0008
298644cd46caSbillm #define	ZDB_FLAG_INDIRECT	0x0010
298744cd46caSbillm #define	ZDB_FLAG_PHYS		0x0020
298844cd46caSbillm #define	ZDB_FLAG_RAW		0x0040
298944cd46caSbillm #define	ZDB_FLAG_PRINT_BLKPTR	0x0080
299044cd46caSbillm 
299144cd46caSbillm int flagbits[256];
299244cd46caSbillm 
299344cd46caSbillm static void
299444cd46caSbillm zdb_print_blkptr(blkptr_t *bp, int flags)
299544cd46caSbillm {
2996b24ab676SJeff Bonwick 	char blkbuf[BP_SPRINTF_LEN];
299744cd46caSbillm 
299844cd46caSbillm 	if (flags & ZDB_FLAG_BSWAP)
299944cd46caSbillm 		byteswap_uint64_array((void *)bp, sizeof (blkptr_t));
3000b24ab676SJeff Bonwick 
300143466aaeSMax Grossman 	snprintf_blkptr(blkbuf, sizeof (blkbuf), bp);
3002b24ab676SJeff Bonwick 	(void) printf("%s\n", blkbuf);
300344cd46caSbillm }
300444cd46caSbillm 
300544cd46caSbillm static void
300644cd46caSbillm zdb_dump_indirect(blkptr_t *bp, int nbps, int flags)
300744cd46caSbillm {
300844cd46caSbillm 	int i;
300944cd46caSbillm 
301044cd46caSbillm 	for (i = 0; i < nbps; i++)
301144cd46caSbillm 		zdb_print_blkptr(&bp[i], flags);
301244cd46caSbillm }
301344cd46caSbillm 
301444cd46caSbillm static void
301544cd46caSbillm zdb_dump_gbh(void *buf, int flags)
301644cd46caSbillm {
301744cd46caSbillm 	zdb_dump_indirect((blkptr_t *)buf, SPA_GBH_NBLKPTRS, flags);
301844cd46caSbillm }
301944cd46caSbillm 
302044cd46caSbillm static void
302144cd46caSbillm zdb_dump_block_raw(void *buf, uint64_t size, int flags)
302244cd46caSbillm {
302344cd46caSbillm 	if (flags & ZDB_FLAG_BSWAP)
302444cd46caSbillm 		byteswap_uint64_array(buf, size);
3025b24ab676SJeff Bonwick 	(void) write(1, buf, size);
302644cd46caSbillm }
302744cd46caSbillm 
302844cd46caSbillm static void
302944cd46caSbillm zdb_dump_block(char *label, void *buf, uint64_t size, int flags)
303044cd46caSbillm {
303144cd46caSbillm 	uint64_t *d = (uint64_t *)buf;
303244cd46caSbillm 	int nwords = size / sizeof (uint64_t);
303344cd46caSbillm 	int do_bswap = !!(flags & ZDB_FLAG_BSWAP);
303444cd46caSbillm 	int i, j;
303544cd46caSbillm 	char *hdr, *c;
303644cd46caSbillm 
303744cd46caSbillm 
303844cd46caSbillm 	if (do_bswap)
303944cd46caSbillm 		hdr = " 7 6 5 4 3 2 1 0   f e d c b a 9 8";
304044cd46caSbillm 	else
304144cd46caSbillm 		hdr = " 0 1 2 3 4 5 6 7   8 9 a b c d e f";
304244cd46caSbillm 
304344cd46caSbillm 	(void) printf("\n%s\n%6s   %s  0123456789abcdef\n", label, "", hdr);
304444cd46caSbillm 
304544cd46caSbillm 	for (i = 0; i < nwords; i += 2) {
304644cd46caSbillm 		(void) printf("%06llx:  %016llx  %016llx  ",
304744cd46caSbillm 		    (u_longlong_t)(i * sizeof (uint64_t)),
304844cd46caSbillm 		    (u_longlong_t)(do_bswap ? BSWAP_64(d[i]) : d[i]),
304944cd46caSbillm 		    (u_longlong_t)(do_bswap ? BSWAP_64(d[i + 1]) : d[i + 1]));
305044cd46caSbillm 
305144cd46caSbillm 		c = (char *)&d[i];
305244cd46caSbillm 		for (j = 0; j < 2 * sizeof (uint64_t); j++)
305344cd46caSbillm 			(void) printf("%c", isprint(c[j]) ? c[j] : '.');
305444cd46caSbillm 		(void) printf("\n");
305544cd46caSbillm 	}
305644cd46caSbillm }
305744cd46caSbillm 
305844cd46caSbillm /*
305944cd46caSbillm  * There are two acceptable formats:
306044cd46caSbillm  *	leaf_name	  - For example: c1t0d0 or /tmp/ztest.0a
306144cd46caSbillm  *	child[.child]*    - For example: 0.1.1
306244cd46caSbillm  *
306344cd46caSbillm  * The second form can be used to specify arbitrary vdevs anywhere
306444cd46caSbillm  * in the heirarchy.  For example, in a pool with a mirror of
306544cd46caSbillm  * RAID-Zs, you can specify either RAID-Z vdev with 0.0 or 0.1 .
306644cd46caSbillm  */
306744cd46caSbillm static vdev_t *
306844cd46caSbillm zdb_vdev_lookup(vdev_t *vdev, char *path)
306944cd46caSbillm {
307044cd46caSbillm 	char *s, *p, *q;
307144cd46caSbillm 	int i;
307244cd46caSbillm 
307344cd46caSbillm 	if (vdev == NULL)
307444cd46caSbillm 		return (NULL);
307544cd46caSbillm 
307644cd46caSbillm 	/* First, assume the x.x.x.x format */
307744cd46caSbillm 	i = (int)strtoul(path, &s, 10);
307844cd46caSbillm 	if (s == path || (s && *s != '.' && *s != '\0'))
307944cd46caSbillm 		goto name;
308044cd46caSbillm 	if (i < 0 || i >= vdev->vdev_children)
308144cd46caSbillm 		return (NULL);
308244cd46caSbillm 
308344cd46caSbillm 	vdev = vdev->vdev_child[i];
308444cd46caSbillm 	if (*s == '\0')
308544cd46caSbillm 		return (vdev);
308644cd46caSbillm 	return (zdb_vdev_lookup(vdev, s+1));
308744cd46caSbillm 
308844cd46caSbillm name:
308944cd46caSbillm 	for (i = 0; i < vdev->vdev_children; i++) {
309044cd46caSbillm 		vdev_t *vc = vdev->vdev_child[i];
309144cd46caSbillm 
309244cd46caSbillm 		if (vc->vdev_path == NULL) {
309344cd46caSbillm 			vc = zdb_vdev_lookup(vc, path);
309444cd46caSbillm 			if (vc == NULL)
309544cd46caSbillm 				continue;
309644cd46caSbillm 			else
309744cd46caSbillm 				return (vc);
309844cd46caSbillm 		}
309944cd46caSbillm 
310044cd46caSbillm 		p = strrchr(vc->vdev_path, '/');
310144cd46caSbillm 		p = p ? p + 1 : vc->vdev_path;
310244cd46caSbillm 		q = &vc->vdev_path[strlen(vc->vdev_path) - 2];
310344cd46caSbillm 
310444cd46caSbillm 		if (strcmp(vc->vdev_path, path) == 0)
310544cd46caSbillm 			return (vc);
310644cd46caSbillm 		if (strcmp(p, path) == 0)
310744cd46caSbillm 			return (vc);
310844cd46caSbillm 		if (strcmp(q, "s0") == 0 && strncmp(p, path, q - p) == 0)
310944cd46caSbillm 			return (vc);
311044cd46caSbillm 	}
311144cd46caSbillm 
311244cd46caSbillm 	return (NULL);
311344cd46caSbillm }
311444cd46caSbillm 
311544cd46caSbillm /*
311644cd46caSbillm  * Read a block from a pool and print it out.  The syntax of the
311744cd46caSbillm  * block descriptor is:
311844cd46caSbillm  *
311944cd46caSbillm  *	pool:vdev_specifier:offset:size[:flags]
312044cd46caSbillm  *
312144cd46caSbillm  *	pool           - The name of the pool you wish to read from
312244cd46caSbillm  *	vdev_specifier - Which vdev (see comment for zdb_vdev_lookup)
312344cd46caSbillm  *	offset         - offset, in hex, in bytes
312444cd46caSbillm  *	size           - Amount of data to read, in hex, in bytes
312544cd46caSbillm  *	flags          - A string of characters specifying options
312644cd46caSbillm  *		 b: Decode a blkptr at given offset within block
312744cd46caSbillm  *		*c: Calculate and display checksums
3128b24ab676SJeff Bonwick  *		 d: Decompress data before dumping
312944cd46caSbillm  *		 e: Byteswap data before dumping
3130b24ab676SJeff Bonwick  *		 g: Display data as a gang block header
3131b24ab676SJeff Bonwick  *		 i: Display as an indirect block
313244cd46caSbillm  *		 p: Do I/O to physical offset
313344cd46caSbillm  *		 r: Dump raw data to stdout
313444cd46caSbillm  *
313544cd46caSbillm  *              * = not yet implemented
313644cd46caSbillm  */
313744cd46caSbillm static void
313807428bdfSVictor Latushkin zdb_read_block(char *thing, spa_t *spa)
313944cd46caSbillm {
3140b24ab676SJeff Bonwick 	blkptr_t blk, *bp = &blk;
3141b24ab676SJeff Bonwick 	dva_t *dva = bp->blk_dva;
314244cd46caSbillm 	int flags = 0;
3143b24ab676SJeff Bonwick 	uint64_t offset = 0, size = 0, psize = 0, lsize = 0, blkptr_offset = 0;
314444cd46caSbillm 	zio_t *zio;
314544cd46caSbillm 	vdev_t *vd;
3146b24ab676SJeff Bonwick 	void *pbuf, *lbuf, *buf;
314707428bdfSVictor Latushkin 	char *s, *p, *dup, *vdev, *flagstr;
3148b24ab676SJeff Bonwick 	int i, error;
314944cd46caSbillm 
315044cd46caSbillm 	dup = strdup(thing);
315144cd46caSbillm 	s = strtok(dup, ":");
315244cd46caSbillm 	vdev = s ? s : "";
315344cd46caSbillm 	s = strtok(NULL, ":");
315444cd46caSbillm 	offset = strtoull(s ? s : "", NULL, 16);
315544cd46caSbillm 	s = strtok(NULL, ":");
315644cd46caSbillm 	size = strtoull(s ? s : "", NULL, 16);
315744cd46caSbillm 	s = strtok(NULL, ":");
315844cd46caSbillm 	flagstr = s ? s : "";
315944cd46caSbillm 
316044cd46caSbillm 	s = NULL;
316144cd46caSbillm 	if (size == 0)
316244cd46caSbillm 		s = "size must not be zero";
316344cd46caSbillm 	if (!IS_P2ALIGNED(size, DEV_BSIZE))
316444cd46caSbillm 		s = "size must be a multiple of sector size";
316544cd46caSbillm 	if (!IS_P2ALIGNED(offset, DEV_BSIZE))
316644cd46caSbillm 		s = "offset must be a multiple of sector size";
316744cd46caSbillm 	if (s) {
316844cd46caSbillm 		(void) printf("Invalid block specifier: %s  - %s\n", thing, s);
316944cd46caSbillm 		free(dup);
317044cd46caSbillm 		return;
317144cd46caSbillm 	}
317244cd46caSbillm 
317344cd46caSbillm 	for (s = strtok(flagstr, ":"); s; s = strtok(NULL, ":")) {
317444cd46caSbillm 		for (i = 0; flagstr[i]; i++) {
31755ad82045Snd150628 			int bit = flagbits[(uchar_t)flagstr[i]];
317644cd46caSbillm 
317744cd46caSbillm 			if (bit == 0) {
317844cd46caSbillm 				(void) printf("***Invalid flag: %c\n",
317944cd46caSbillm 				    flagstr[i]);
318044cd46caSbillm 				continue;
318144cd46caSbillm 			}
318244cd46caSbillm 			flags |= bit;
318344cd46caSbillm 
318444cd46caSbillm 			/* If it's not something with an argument, keep going */
3185b24ab676SJeff Bonwick 			if ((bit & (ZDB_FLAG_CHECKSUM |
318644cd46caSbillm 			    ZDB_FLAG_PRINT_BLKPTR)) == 0)
318744cd46caSbillm 				continue;
318844cd46caSbillm 
318944cd46caSbillm 			p = &flagstr[i + 1];
319044cd46caSbillm 			if (bit == ZDB_FLAG_PRINT_BLKPTR)
319144cd46caSbillm 				blkptr_offset = strtoull(p, &p, 16);
319244cd46caSbillm 			if (*p != ':' && *p != '\0') {
319344cd46caSbillm 				(void) printf("***Invalid flag arg: '%s'\n", s);
319444cd46caSbillm 				free(dup);
319544cd46caSbillm 				return;
319644cd46caSbillm 			}
319744cd46caSbillm 		}
319844cd46caSbillm 	}
319944cd46caSbillm 
320044cd46caSbillm 	vd = zdb_vdev_lookup(spa->spa_root_vdev, vdev);
320144cd46caSbillm 	if (vd == NULL) {
320244cd46caSbillm 		(void) printf("***Invalid vdev: %s\n", vdev);
320344cd46caSbillm 		free(dup);
320444cd46caSbillm 		return;
320544cd46caSbillm 	} else {
320644cd46caSbillm 		if (vd->vdev_path)
3207b24ab676SJeff Bonwick 			(void) fprintf(stderr, "Found vdev: %s\n",
3208b24ab676SJeff Bonwick 			    vd->vdev_path);
320944cd46caSbillm 		else
3210b24ab676SJeff Bonwick 			(void) fprintf(stderr, "Found vdev type: %s\n",
321144cd46caSbillm 			    vd->vdev_ops->vdev_op_type);
321244cd46caSbillm 	}
321344cd46caSbillm 
3214b24ab676SJeff Bonwick 	psize = size;
3215b24ab676SJeff Bonwick 	lsize = size;
321644cd46caSbillm 
3217b24ab676SJeff Bonwick 	pbuf = umem_alloc(SPA_MAXBLOCKSIZE, UMEM_NOFAIL);
3218b24ab676SJeff Bonwick 	lbuf = umem_alloc(SPA_MAXBLOCKSIZE, UMEM_NOFAIL);
3219b24ab676SJeff Bonwick 
3220b24ab676SJeff Bonwick 	BP_ZERO(bp);
3221b24ab676SJeff Bonwick 
3222b24ab676SJeff Bonwick 	DVA_SET_VDEV(&dva[0], vd->vdev_id);
3223b24ab676SJeff Bonwick 	DVA_SET_OFFSET(&dva[0], offset);
3224b24ab676SJeff Bonwick 	DVA_SET_GANG(&dva[0], !!(flags & ZDB_FLAG_GBH));
3225b24ab676SJeff Bonwick 	DVA_SET_ASIZE(&dva[0], vdev_psize_to_asize(vd, psize));
3226b24ab676SJeff Bonwick 
3227b24ab676SJeff Bonwick 	BP_SET_BIRTH(bp, TXG_INITIAL, TXG_INITIAL);
3228b24ab676SJeff Bonwick 
3229b24ab676SJeff Bonwick 	BP_SET_LSIZE(bp, lsize);
3230b24ab676SJeff Bonwick 	BP_SET_PSIZE(bp, psize);
3231b24ab676SJeff Bonwick 	BP_SET_COMPRESS(bp, ZIO_COMPRESS_OFF);
3232b24ab676SJeff Bonwick 	BP_SET_CHECKSUM(bp, ZIO_CHECKSUM_OFF);
3233b24ab676SJeff Bonwick 	BP_SET_TYPE(bp, DMU_OT_NONE);
3234b24ab676SJeff Bonwick 	BP_SET_LEVEL(bp, 0);
3235b24ab676SJeff Bonwick 	BP_SET_DEDUP(bp, 0);
3236b24ab676SJeff Bonwick 	BP_SET_BYTEORDER(bp, ZFS_HOST_BYTEORDER);
323744cd46caSbillm 
3238e14bb325SJeff Bonwick 	spa_config_enter(spa, SCL_STATE, FTAG, RW_READER);
323944cd46caSbillm 	zio = zio_root(spa, NULL, NULL, 0);
3240b24ab676SJeff Bonwick 
3241b24ab676SJeff Bonwick 	if (vd == vd->vdev_top) {
3242b24ab676SJeff Bonwick 		/*
3243b24ab676SJeff Bonwick 		 * Treat this as a normal block read.
3244b24ab676SJeff Bonwick 		 */
3245b24ab676SJeff Bonwick 		zio_nowait(zio_read(zio, spa, bp, pbuf, psize, NULL, NULL,
3246b24ab676SJeff Bonwick 		    ZIO_PRIORITY_SYNC_READ,
3247b24ab676SJeff Bonwick 		    ZIO_FLAG_CANFAIL | ZIO_FLAG_RAW, NULL));
3248b24ab676SJeff Bonwick 	} else {
3249b24ab676SJeff Bonwick 		/*
3250b24ab676SJeff Bonwick 		 * Treat this as a vdev child I/O.
3251b24ab676SJeff Bonwick 		 */
3252b24ab676SJeff Bonwick 		zio_nowait(zio_vdev_child_io(zio, bp, vd, offset, pbuf, psize,
3253b24ab676SJeff Bonwick 		    ZIO_TYPE_READ, ZIO_PRIORITY_SYNC_READ,
3254b24ab676SJeff Bonwick 		    ZIO_FLAG_DONT_CACHE | ZIO_FLAG_DONT_QUEUE |
3255b24ab676SJeff Bonwick 		    ZIO_FLAG_DONT_PROPAGATE | ZIO_FLAG_DONT_RETRY |
3256b24ab676SJeff Bonwick 		    ZIO_FLAG_CANFAIL | ZIO_FLAG_RAW, NULL, NULL));
3257b24ab676SJeff Bonwick 	}
3258b24ab676SJeff Bonwick 
325944cd46caSbillm 	error = zio_wait(zio);
3260e14bb325SJeff Bonwick 	spa_config_exit(spa, SCL_STATE, FTAG);
326144cd46caSbillm 
326244cd46caSbillm 	if (error) {
326344cd46caSbillm 		(void) printf("Read of %s failed, error: %d\n", thing, error);
326444cd46caSbillm 		goto out;
326544cd46caSbillm 	}
326644cd46caSbillm 
3267b24ab676SJeff Bonwick 	if (flags & ZDB_FLAG_DECOMPRESS) {
3268b24ab676SJeff Bonwick 		/*
3269b24ab676SJeff Bonwick 		 * We don't know how the data was compressed, so just try
3270b24ab676SJeff Bonwick 		 * every decompress function at every inflated blocksize.
3271b24ab676SJeff Bonwick 		 */
3272b24ab676SJeff Bonwick 		enum zio_compress c;
3273b24ab676SJeff Bonwick 		void *pbuf2 = umem_alloc(SPA_MAXBLOCKSIZE, UMEM_NOFAIL);
3274b24ab676SJeff Bonwick 		void *lbuf2 = umem_alloc(SPA_MAXBLOCKSIZE, UMEM_NOFAIL);
3275b24ab676SJeff Bonwick 
3276b24ab676SJeff Bonwick 		bcopy(pbuf, pbuf2, psize);
3277b24ab676SJeff Bonwick 
3278b24ab676SJeff Bonwick 		VERIFY(random_get_pseudo_bytes((uint8_t *)pbuf + psize,
3279b24ab676SJeff Bonwick 		    SPA_MAXBLOCKSIZE - psize) == 0);
3280b24ab676SJeff Bonwick 
3281b24ab676SJeff Bonwick 		VERIFY(random_get_pseudo_bytes((uint8_t *)pbuf2 + psize,
3282b24ab676SJeff Bonwick 		    SPA_MAXBLOCKSIZE - psize) == 0);
3283b24ab676SJeff Bonwick 
3284b24ab676SJeff Bonwick 		for (lsize = SPA_MAXBLOCKSIZE; lsize > psize;
3285b24ab676SJeff Bonwick 		    lsize -= SPA_MINBLOCKSIZE) {
3286b24ab676SJeff Bonwick 			for (c = 0; c < ZIO_COMPRESS_FUNCTIONS; c++) {
3287b24ab676SJeff Bonwick 				if (zio_decompress_data(c, pbuf, lbuf,
3288b24ab676SJeff Bonwick 				    psize, lsize) == 0 &&
3289b24ab676SJeff Bonwick 				    zio_decompress_data(c, pbuf2, lbuf2,
3290b24ab676SJeff Bonwick 				    psize, lsize) == 0 &&
3291b24ab676SJeff Bonwick 				    bcmp(lbuf, lbuf2, lsize) == 0)
3292b24ab676SJeff Bonwick 					break;
3293b24ab676SJeff Bonwick 			}
3294b24ab676SJeff Bonwick 			if (c != ZIO_COMPRESS_FUNCTIONS)
3295b24ab676SJeff Bonwick 				break;
3296b24ab676SJeff Bonwick 			lsize -= SPA_MINBLOCKSIZE;
3297b24ab676SJeff Bonwick 		}
3298b24ab676SJeff Bonwick 
3299b24ab676SJeff Bonwick 		umem_free(pbuf2, SPA_MAXBLOCKSIZE);
3300b24ab676SJeff Bonwick 		umem_free(lbuf2, SPA_MAXBLOCKSIZE);
3301b24ab676SJeff Bonwick 
3302b24ab676SJeff Bonwick 		if (lsize <= psize) {
3303b24ab676SJeff Bonwick 			(void) printf("Decompress of %s failed\n", thing);
3304b24ab676SJeff Bonwick 			goto out;
3305b24ab676SJeff Bonwick 		}
3306b24ab676SJeff Bonwick 		buf = lbuf;
3307b24ab676SJeff Bonwick 		size = lsize;
3308b24ab676SJeff Bonwick 	} else {
3309b24ab676SJeff Bonwick 		buf = pbuf;
3310b24ab676SJeff Bonwick 		size = psize;
3311b24ab676SJeff Bonwick 	}
3312b24ab676SJeff Bonwick 
331344cd46caSbillm 	if (flags & ZDB_FLAG_PRINT_BLKPTR)
331444cd46caSbillm 		zdb_print_blkptr((blkptr_t *)(void *)
331544cd46caSbillm 		    ((uintptr_t)buf + (uintptr_t)blkptr_offset), flags);
331644cd46caSbillm 	else if (flags & ZDB_FLAG_RAW)
331744cd46caSbillm 		zdb_dump_block_raw(buf, size, flags);
331844cd46caSbillm 	else if (flags & ZDB_FLAG_INDIRECT)
331944cd46caSbillm 		zdb_dump_indirect((blkptr_t *)buf, size / sizeof (blkptr_t),
332044cd46caSbillm 		    flags);
332144cd46caSbillm 	else if (flags & ZDB_FLAG_GBH)
332244cd46caSbillm 		zdb_dump_gbh(buf, flags);
332344cd46caSbillm 	else
332444cd46caSbillm 		zdb_dump_block(thing, buf, size, flags);
332544cd46caSbillm 
332644cd46caSbillm out:
3327b24ab676SJeff Bonwick 	umem_free(pbuf, SPA_MAXBLOCKSIZE);
3328b24ab676SJeff Bonwick 	umem_free(lbuf, SPA_MAXBLOCKSIZE);
332944cd46caSbillm 	free(dup);
333044cd46caSbillm }
333144cd46caSbillm 
3332de6628f0Sck153898 static boolean_t
33333ad6c7f9SVictor Latushkin pool_match(nvlist_t *cfg, char *tgt)
3334de6628f0Sck153898 {
33353ad6c7f9SVictor Latushkin 	uint64_t v, guid = strtoull(tgt, NULL, 0);
3336de6628f0Sck153898 	char *s;
3337de6628f0Sck153898 
33383ad6c7f9SVictor Latushkin 	if (guid != 0) {
33393ad6c7f9SVictor Latushkin 		if (nvlist_lookup_uint64(cfg, ZPOOL_CONFIG_POOL_GUID, &v) == 0)
33403ad6c7f9SVictor Latushkin 			return (v == guid);
33413ad6c7f9SVictor Latushkin 	} else {
33423ad6c7f9SVictor Latushkin 		if (nvlist_lookup_string(cfg, ZPOOL_CONFIG_POOL_NAME, &s) == 0)
3343de6628f0Sck153898 			return (strcmp(s, tgt) == 0);
3344de6628f0Sck153898 	}
3345de6628f0Sck153898 	return (B_FALSE);
3346de6628f0Sck153898 }
3347de6628f0Sck153898 
33483ad6c7f9SVictor Latushkin static char *
33493ad6c7f9SVictor Latushkin find_zpool(char **target, nvlist_t **configp, int dirc, char **dirv)
3350de6628f0Sck153898 {
3351de6628f0Sck153898 	nvlist_t *pools;
3352de6628f0Sck153898 	nvlist_t *match = NULL;
33533ad6c7f9SVictor Latushkin 	char *name = NULL;
33543ad6c7f9SVictor Latushkin 	char *sepp = NULL;
33553ad6c7f9SVictor Latushkin 	char sep;
33563ad6c7f9SVictor Latushkin 	int count = 0;
3357d41c4376SMark J Musante 	importargs_t args = { 0 };
3358d41c4376SMark J Musante 
3359d41c4376SMark J Musante 	args.paths = dirc;
3360d41c4376SMark J Musante 	args.path = dirv;
3361d41c4376SMark J Musante 	args.can_be_active = B_TRUE;
3362de6628f0Sck153898 
33633ad6c7f9SVictor Latushkin 	if ((sepp = strpbrk(*target, "/@")) != NULL) {
33643ad6c7f9SVictor Latushkin 		sep = *sepp;
33653ad6c7f9SVictor Latushkin 		*sepp = '\0';
33663ad6c7f9SVictor Latushkin 	}
33673ad6c7f9SVictor Latushkin 
3368d41c4376SMark J Musante 	pools = zpool_search_import(g_zfs, &args);
3369de6628f0Sck153898 
3370de6628f0Sck153898 	if (pools != NULL) {
3371de6628f0Sck153898 		nvpair_t *elem = NULL;
3372de6628f0Sck153898 		while ((elem = nvlist_next_nvpair(pools, elem)) != NULL) {
3373de6628f0Sck153898 			verify(nvpair_value_nvlist(elem, configp) == 0);
33743ad6c7f9SVictor Latushkin 			if (pool_match(*configp, *target)) {
33753ad6c7f9SVictor Latushkin 				count++;
3376de6628f0Sck153898 				if (match != NULL) {
33773ad6c7f9SVictor Latushkin 					/* print previously found config */
33783ad6c7f9SVictor Latushkin 					if (name != NULL) {
33793ad6c7f9SVictor Latushkin 						(void) printf("%s\n", name);
33803ad6c7f9SVictor Latushkin 						dump_nvlist(match, 8);
33813ad6c7f9SVictor Latushkin 						name = NULL;
33823ad6c7f9SVictor Latushkin 					}
33833ad6c7f9SVictor Latushkin 					(void) printf("%s\n",
33843ad6c7f9SVictor Latushkin 					    nvpair_name(elem));
33853ad6c7f9SVictor Latushkin 					dump_nvlist(*configp, 8);
3386de6628f0Sck153898 				} else {
3387de6628f0Sck153898 					match = *configp;
33883ad6c7f9SVictor Latushkin 					name = nvpair_name(elem);
3389de6628f0Sck153898 				}
3390de6628f0Sck153898 			}
3391de6628f0Sck153898 		}
3392de6628f0Sck153898 	}
33933ad6c7f9SVictor Latushkin 	if (count > 1)
33943ad6c7f9SVictor Latushkin 		(void) fatal("\tMatched %d pools - use pool GUID "
33953ad6c7f9SVictor Latushkin 		    "instead of pool name or \n"
33963ad6c7f9SVictor Latushkin 		    "\tpool name part of a dataset name to select pool", count);
33973ad6c7f9SVictor Latushkin 
33983ad6c7f9SVictor Latushkin 	if (sepp)
33993ad6c7f9SVictor Latushkin 		*sepp = sep;
34003ad6c7f9SVictor Latushkin 	/*
34013ad6c7f9SVictor Latushkin 	 * If pool GUID was specified for pool id, replace it with pool name
34023ad6c7f9SVictor Latushkin 	 */
34033ad6c7f9SVictor Latushkin 	if (name && (strstr(*target, name) != *target)) {
34043ad6c7f9SVictor Latushkin 		int sz = 1 + strlen(name) + ((sepp) ? strlen(sepp) : 0);
34053ad6c7f9SVictor Latushkin 
34063ad6c7f9SVictor Latushkin 		*target = umem_alloc(sz, UMEM_NOFAIL);
34073ad6c7f9SVictor Latushkin 		(void) snprintf(*target, sz, "%s%s", name, sepp ? sepp : "");
34083ad6c7f9SVictor Latushkin 	}
3409de6628f0Sck153898 
34103ad6c7f9SVictor Latushkin 	*configp = name ? match : NULL;
3411de6628f0Sck153898 
34123ad6c7f9SVictor Latushkin 	return (name);
3413de6628f0Sck153898 }
3414de6628f0Sck153898 
3415fa9e4066Sahrens int
3416fa9e4066Sahrens main(int argc, char **argv)
3417fa9e4066Sahrens {
3418fa9e4066Sahrens 	int i, c;
3419fa9e4066Sahrens 	struct rlimit rl = { 1024, 1024 };
34203ad6c7f9SVictor Latushkin 	spa_t *spa = NULL;
3421fa9e4066Sahrens 	objset_t *os = NULL;
3422fa9e4066Sahrens 	int dump_all = 1;
3423fa9e4066Sahrens 	int verbose = 0;
3424c8ee1847SVictor Latushkin 	int error = 0;
34253ad6c7f9SVictor Latushkin 	char **searchdirs = NULL;
34263ad6c7f9SVictor Latushkin 	int nsearch = 0;
34273ad6c7f9SVictor Latushkin 	char *target;
3428468c413aSTim Haley 	nvlist_t *policy = NULL;
3429468c413aSTim Haley 	uint64_t max_txg = UINT64_MAX;
3430c8ee1847SVictor Latushkin 	int rewind = ZPOOL_NEVER_REWIND;
3431fa9e4066Sahrens 
3432fa9e4066Sahrens 	(void) setrlimit(RLIMIT_NOFILE, &rl);
3433004388ebScasper 	(void) enable_extended_FILE_stdio(-1, -1);
3434fa9e4066Sahrens 
3435fa9e4066Sahrens 	dprintf_setup(&argc, argv);
3436fa9e4066Sahrens 
3437df15e419SMatthew Ahrens 	while ((c = getopt(argc, argv,
34382e4c9986SGeorge Wilson 	    "bcdhilmMI:suCDRSAFLXx:evp:t:U:P")) != -1) {
3439fa9e4066Sahrens 		switch (c) {
3440fa9e4066Sahrens 		case 'b':
3441fa9e4066Sahrens 		case 'c':
3442b24ab676SJeff Bonwick 		case 'd':
3443b24ab676SJeff Bonwick 		case 'h':
3444b24ab676SJeff Bonwick 		case 'i':
3445b24ab676SJeff Bonwick 		case 'l':
3446d6e555bdSGeorge Wilson 		case 'm':
3447fa9e4066Sahrens 		case 's':
3448b24ab676SJeff Bonwick 		case 'u':
3449fa9e4066Sahrens 		case 'C':
3450b24ab676SJeff Bonwick 		case 'D':
34512e4c9986SGeorge Wilson 		case 'M':
345244cd46caSbillm 		case 'R':
3453b24ab676SJeff Bonwick 		case 'S':
3454fa9e4066Sahrens 			dump_opt[c]++;
3455fa9e4066Sahrens 			dump_all = 0;
3456fa9e4066Sahrens 			break;
3457feef89cfSVictor Latushkin 		case 'A':
3458c8ee1847SVictor Latushkin 		case 'F':
345982a0a985SVictor Latushkin 		case 'L':
3460c8ee1847SVictor Latushkin 		case 'X':
34613ad6c7f9SVictor Latushkin 		case 'e':
34623f9d6ad7SLin Ling 		case 'P':
346382a0a985SVictor Latushkin 			dump_opt[c]++;
346482a0a985SVictor Latushkin 			break;
34652e4c9986SGeorge Wilson 		case 'I':
346631d7e8faSGeorge Wilson 			max_inflight = strtoull(optarg, NULL, 0);
346731d7e8faSGeorge Wilson 			if (max_inflight == 0) {
346831d7e8faSGeorge Wilson 				(void) fprintf(stderr, "maximum number "
346931d7e8faSGeorge Wilson 				    "of inflight I/Os must be greater "
347031d7e8faSGeorge Wilson 				    "than 0\n");
347131d7e8faSGeorge Wilson 				usage();
347231d7e8faSGeorge Wilson 			}
347331d7e8faSGeorge Wilson 			break;
3474de6628f0Sck153898 		case 'p':
34753ad6c7f9SVictor Latushkin 			if (searchdirs == NULL) {
34763ad6c7f9SVictor Latushkin 				searchdirs = umem_alloc(sizeof (char *),
34773ad6c7f9SVictor Latushkin 				    UMEM_NOFAIL);
34783ad6c7f9SVictor Latushkin 			} else {
34793ad6c7f9SVictor Latushkin 				char **tmp = umem_alloc((nsearch + 1) *
34803ad6c7f9SVictor Latushkin 				    sizeof (char *), UMEM_NOFAIL);
34813ad6c7f9SVictor Latushkin 				bcopy(searchdirs, tmp, nsearch *
34823ad6c7f9SVictor Latushkin 				    sizeof (char *));
34833ad6c7f9SVictor Latushkin 				umem_free(searchdirs,
34843ad6c7f9SVictor Latushkin 				    nsearch * sizeof (char *));
34853ad6c7f9SVictor Latushkin 				searchdirs = tmp;
34863ad6c7f9SVictor Latushkin 			}
34873ad6c7f9SVictor Latushkin 			searchdirs[nsearch++] = optarg;
3488de6628f0Sck153898 			break;
34892e551927SVictor Latushkin 		case 't':
3490468c413aSTim Haley 			max_txg = strtoull(optarg, NULL, 0);
3491468c413aSTim Haley 			if (max_txg < TXG_INITIAL) {
34922e551927SVictor Latushkin 				(void) fprintf(stderr, "incorrect txg "
34932e551927SVictor Latushkin 				    "specified: %s\n", optarg);
34942e551927SVictor Latushkin 				usage();
34952e551927SVictor Latushkin 			}
34962e551927SVictor Latushkin 			break;
3497b24ab676SJeff Bonwick 		case 'U':
3498b24ab676SJeff Bonwick 			spa_config_path = optarg;
3499b24ab676SJeff Bonwick 			break;
35002e4c9986SGeorge Wilson 		case 'v':
35012e4c9986SGeorge Wilson 			verbose++;
35022e4c9986SGeorge Wilson 			break;
35032e4c9986SGeorge Wilson 		case 'x':
35042e4c9986SGeorge Wilson 			vn_dumpdir = optarg;
35052e4c9986SGeorge Wilson 			break;
3506fa9e4066Sahrens 		default:
3507fa9e4066Sahrens 			usage();
3508fa9e4066Sahrens 			break;
3509fa9e4066Sahrens 		}
3510fa9e4066Sahrens 	}
3511fa9e4066Sahrens 
35123ad6c7f9SVictor Latushkin 	if (!dump_opt['e'] && searchdirs != NULL) {
351388b7b0f2SMatthew Ahrens 		(void) fprintf(stderr, "-p option requires use of -e\n");
351488b7b0f2SMatthew Ahrens 		usage();
351588b7b0f2SMatthew Ahrens 	}
3516de6628f0Sck153898 
351706be9802SMatthew Ahrens 	/*
351806be9802SMatthew Ahrens 	 * ZDB does not typically re-read blocks; therefore limit the ARC
351906be9802SMatthew Ahrens 	 * to 256 MB, which can be used entirely for metadata.
352006be9802SMatthew Ahrens 	 */
352106be9802SMatthew Ahrens 	zfs_arc_max = zfs_arc_meta_limit = 256 * 1024 * 1024;
352206be9802SMatthew Ahrens 
3523f7950bf1SMatthew Ahrens 	/*
3524f7950bf1SMatthew Ahrens 	 * "zdb -c" uses checksum-verifying scrub i/os which are async reads.
3525f7950bf1SMatthew Ahrens 	 * "zdb -b" uses traversal prefetch which uses async reads.
3526f7950bf1SMatthew Ahrens 	 * For good performance, let several of them be active at once.
3527f7950bf1SMatthew Ahrens 	 */
3528f7950bf1SMatthew Ahrens 	zfs_vdev_async_read_max_active = 10;
3529f7950bf1SMatthew Ahrens 
3530fa9e4066Sahrens 	kernel_init(FREAD);
3531de6628f0Sck153898 	g_zfs = libzfs_init();
353291ebeef5Sahrens 	ASSERT(g_zfs != NULL);
3533fa9e4066Sahrens 
3534b24ab676SJeff Bonwick 	if (dump_all)
3535b24ab676SJeff Bonwick 		verbose = MAX(verbose, 1);
3536b24ab676SJeff Bonwick 
3537fa9e4066Sahrens 	for (c = 0; c < 256; c++) {
35383f9d6ad7SLin Ling 		if (dump_all && !strchr("elAFLRSXP", c))
3539fa9e4066Sahrens 			dump_opt[c] = 1;
3540fa9e4066Sahrens 		if (dump_opt[c])
3541fa9e4066Sahrens 			dump_opt[c] += verbose;
3542fa9e4066Sahrens 	}
3543fa9e4066Sahrens 
3544feef89cfSVictor Latushkin 	aok = (dump_opt['A'] == 1) || (dump_opt['A'] > 2);
3545feef89cfSVictor Latushkin 	zfs_recover = (dump_opt['A'] > 1);
3546feef89cfSVictor Latushkin 
3547fa9e4066Sahrens 	argc -= optind;
3548fa9e4066Sahrens 	argv += optind;
3549fa9e4066Sahrens 
355007428bdfSVictor Latushkin 	if (argc < 2 && dump_opt['R'])
355107428bdfSVictor Latushkin 		usage();
3552fa9e4066Sahrens 	if (argc < 1) {
35533ad6c7f9SVictor Latushkin 		if (!dump_opt['e'] && dump_opt['C']) {
3554e829d913Sck153898 			dump_cachefile(spa_config_path);
3555fa9e4066Sahrens 			return (0);
3556fa9e4066Sahrens 		}
3557fa9e4066Sahrens 		usage();
3558fa9e4066Sahrens 	}
3559fa9e4066Sahrens 
3560fa9e4066Sahrens 	if (dump_opt['l']) {
3561fa9e4066Sahrens 		dump_label(argv[0]);
3562fa9e4066Sahrens 		return (0);
3563fa9e4066Sahrens 	}
3564fa9e4066Sahrens 
3565c8ee1847SVictor Latushkin 	if (dump_opt['X'] || dump_opt['F'])
3566c8ee1847SVictor Latushkin 		rewind = ZPOOL_DO_REWIND |
3567c8ee1847SVictor Latushkin 		    (dump_opt['X'] ? ZPOOL_EXTREME_REWIND : 0);
3568c8ee1847SVictor Latushkin 
3569c8ee1847SVictor Latushkin 	if (nvlist_alloc(&policy, NV_UNIQUE_NAME_TYPE, 0) != 0 ||
3570c8ee1847SVictor Latushkin 	    nvlist_add_uint64(policy, ZPOOL_REWIND_REQUEST_TXG, max_txg) != 0 ||
3571c8ee1847SVictor Latushkin 	    nvlist_add_uint32(policy, ZPOOL_REWIND_REQUEST, rewind) != 0)
3572c8ee1847SVictor Latushkin 		fatal("internal error: %s", strerror(ENOMEM));
3573c8ee1847SVictor Latushkin 
3574c5904d13Seschrock 	error = 0;
35753ad6c7f9SVictor Latushkin 	target = argv[0];
3576de6628f0Sck153898 
35773ad6c7f9SVictor Latushkin 	if (dump_opt['e']) {
35783ad6c7f9SVictor Latushkin 		nvlist_t *cfg = NULL;
35793ad6c7f9SVictor Latushkin 		char *name = find_zpool(&target, &cfg, nsearch, searchdirs);
3580c5904d13Seschrock 
35813ad6c7f9SVictor Latushkin 		error = ENOENT;
35823ad6c7f9SVictor Latushkin 		if (name) {
358307428bdfSVictor Latushkin 			if (dump_opt['C'] > 1) {
358407428bdfSVictor Latushkin 				(void) printf("\nConfiguration for import:\n");
358507428bdfSVictor Latushkin 				dump_nvlist(cfg, 8);
358607428bdfSVictor Latushkin 			}
3587c8ee1847SVictor Latushkin 			if (nvlist_add_nvlist(cfg,
3588468c413aSTim Haley 			    ZPOOL_REWIND_POLICY, policy) != 0) {
3589468c413aSTim Haley 				fatal("can't open '%s': %s",
3590468c413aSTim Haley 				    target, strerror(ENOMEM));
3591468c413aSTim Haley 			}
35924b964adaSGeorge Wilson 			if ((error = spa_import(name, cfg, NULL,
35934b964adaSGeorge Wilson 			    ZFS_IMPORT_MISSING_LOG)) != 0) {
35944b964adaSGeorge Wilson 				error = spa_import(name, cfg, NULL,
35954b964adaSGeorge Wilson 				    ZFS_IMPORT_VERBATIM);
35964b964adaSGeorge Wilson 			}
3597990b4856Slling 		}
3598c5904d13Seschrock 	}
3599c5904d13Seschrock 
3600c5904d13Seschrock 	if (error == 0) {
360107428bdfSVictor Latushkin 		if (strpbrk(target, "/@") == NULL || dump_opt['R']) {
360280eb36f2SGeorge Wilson 			error = spa_open_rewind(target, &spa, FTAG, policy,
360380eb36f2SGeorge Wilson 			    NULL);
36048f18d1faSGeorge Wilson 			if (error) {
36058f18d1faSGeorge Wilson 				/*
36068f18d1faSGeorge Wilson 				 * If we're missing the log device then
36078f18d1faSGeorge Wilson 				 * try opening the pool after clearing the
36088f18d1faSGeorge Wilson 				 * log state.
36098f18d1faSGeorge Wilson 				 */
36108f18d1faSGeorge Wilson 				mutex_enter(&spa_namespace_lock);
36113ad6c7f9SVictor Latushkin 				if ((spa = spa_lookup(target)) != NULL &&
36128f18d1faSGeorge Wilson 				    spa->spa_log_state == SPA_LOG_MISSING) {
36138f18d1faSGeorge Wilson 					spa->spa_log_state = SPA_LOG_CLEAR;
36148f18d1faSGeorge Wilson 					error = 0;
36158f18d1faSGeorge Wilson 				}
36168f18d1faSGeorge Wilson 				mutex_exit(&spa_namespace_lock);
36178f18d1faSGeorge Wilson 
361880eb36f2SGeorge Wilson 				if (!error) {
361980eb36f2SGeorge Wilson 					error = spa_open_rewind(target, &spa,
362080eb36f2SGeorge Wilson 					    FTAG, policy, NULL);
362180eb36f2SGeorge Wilson 				}
36228f18d1faSGeorge Wilson 			}
362307428bdfSVictor Latushkin 		} else {
362407428bdfSVictor Latushkin 			error = dmu_objset_own(target, DMU_OST_ANY,
362507428bdfSVictor Latushkin 			    B_TRUE, FTAG, &os);
3626c5904d13Seschrock 		}
3627de6628f0Sck153898 	}
362880eb36f2SGeorge Wilson 	nvlist_free(policy);
362980eb36f2SGeorge Wilson 
3630fa9e4066Sahrens 	if (error)
36313ad6c7f9SVictor Latushkin 		fatal("can't open '%s': %s", target, strerror(error));
3632fa9e4066Sahrens 
3633fa9e4066Sahrens 	argv++;
363407428bdfSVictor Latushkin 	argc--;
363507428bdfSVictor Latushkin 	if (!dump_opt['R']) {
363607428bdfSVictor Latushkin 		if (argc > 0) {
3637fa9e4066Sahrens 			zopt_objects = argc;
3638fa9e4066Sahrens 			zopt_object = calloc(zopt_objects, sizeof (uint64_t));
3639fa9e4066Sahrens 			for (i = 0; i < zopt_objects; i++) {
3640fa9e4066Sahrens 				errno = 0;
3641fa9e4066Sahrens 				zopt_object[i] = strtoull(argv[i], NULL, 0);
3642fa9e4066Sahrens 				if (zopt_object[i] == 0 && errno != 0)
364387219db7SVictor Latushkin 					fatal("bad number %s: %s",
3644fa9e4066Sahrens 					    argv[i], strerror(errno));
3645fa9e4066Sahrens 			}
3646fa9e4066Sahrens 		}
3647e690fb27SChristopher Siden 		if (os != NULL) {
3648e690fb27SChristopher Siden 			dump_dir(os);
3649e690fb27SChristopher Siden 		} else if (zopt_objects > 0 && !dump_opt['m']) {
3650e690fb27SChristopher Siden 			dump_dir(spa->spa_meta_objset);
3651e690fb27SChristopher Siden 		} else {
3652e690fb27SChristopher Siden 			dump_zpool(spa);
3653e690fb27SChristopher Siden 		}
3654fa9e4066Sahrens 	} else {
365507428bdfSVictor Latushkin 		flagbits['b'] = ZDB_FLAG_PRINT_BLKPTR;
365607428bdfSVictor Latushkin 		flagbits['c'] = ZDB_FLAG_CHECKSUM;
365707428bdfSVictor Latushkin 		flagbits['d'] = ZDB_FLAG_DECOMPRESS;
365807428bdfSVictor Latushkin 		flagbits['e'] = ZDB_FLAG_BSWAP;
365907428bdfSVictor Latushkin 		flagbits['g'] = ZDB_FLAG_GBH;
366007428bdfSVictor Latushkin 		flagbits['i'] = ZDB_FLAG_INDIRECT;
366107428bdfSVictor Latushkin 		flagbits['p'] = ZDB_FLAG_PHYS;
366207428bdfSVictor Latushkin 		flagbits['r'] = ZDB_FLAG_RAW;
366307428bdfSVictor Latushkin 
366407428bdfSVictor Latushkin 		for (i = 0; i < argc; i++)
366507428bdfSVictor Latushkin 			zdb_read_block(argv[i], spa);
3666fa9e4066Sahrens 	}
3667fa9e4066Sahrens 
366807428bdfSVictor Latushkin 	(os != NULL) ? dmu_objset_disown(os, FTAG) : spa_close(spa, FTAG);
366907428bdfSVictor Latushkin 
3670e0d35c44Smarks 	fuid_table_destroy();
36710a586ceaSMark Shellenbaum 	sa_loaded = B_FALSE;
3672e0d35c44Smarks 
3673de6628f0Sck153898 	libzfs_fini(g_zfs);
3674fa9e4066Sahrens 	kernel_fini();
3675fa9e4066Sahrens 
3676fa9e4066Sahrens 	return (0);
3677fa9e4066Sahrens }
3678