xref: /illumos-gate/usr/src/cmd/zdb/zdb.c (revision f7950bf1)
1fa9e4066Sahrens /*
2fa9e4066Sahrens  * CDDL HEADER START
3fa9e4066Sahrens  *
4fa9e4066Sahrens  * The contents of this file are subject to the terms of the
5ea8dc4b6Seschrock  * Common Development and Distribution License (the "License").
6ea8dc4b6Seschrock  * You may not use this file except in compliance with the License.
7fa9e4066Sahrens  *
8fa9e4066Sahrens  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9fa9e4066Sahrens  * or http://www.opensolaris.org/os/licensing.
10fa9e4066Sahrens  * See the License for the specific language governing permissions
11fa9e4066Sahrens  * and limitations under the License.
12fa9e4066Sahrens  *
13fa9e4066Sahrens  * When distributing Covered Code, include this CDDL HEADER in each
14fa9e4066Sahrens  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15fa9e4066Sahrens  * If applicable, add the following below this CDDL HEADER, with the
16fa9e4066Sahrens  * fields enclosed by brackets "[]" replaced with your own identifying
17fa9e4066Sahrens  * information: Portions Copyright [yyyy] [name of copyright owner]
18fa9e4066Sahrens  *
19fa9e4066Sahrens  * CDDL HEADER END
20fa9e4066Sahrens  */
21ad135b5dSChristopher Siden 
22fa9e4066Sahrens /*
238f2529deSMark Shellenbaum  * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
247802d7bfSMatthew Ahrens  * Copyright (c) 2011, 2014 by Delphix. All rights reserved.
25fa9e4066Sahrens  */
26fa9e4066Sahrens 
27fa9e4066Sahrens #include <stdio.h>
28490d05b9SMatthew Ahrens #include <unistd.h>
29004388ebScasper #include <stdio_ext.h>
30fa9e4066Sahrens #include <stdlib.h>
3144cd46caSbillm #include <ctype.h>
32fa9e4066Sahrens #include <sys/zfs_context.h>
33fa9e4066Sahrens #include <sys/spa.h>
34fa9e4066Sahrens #include <sys/spa_impl.h>
35fa9e4066Sahrens #include <sys/dmu.h>
36fa9e4066Sahrens #include <sys/zap.h>
37fa9e4066Sahrens #include <sys/fs/zfs.h>
38fa9e4066Sahrens #include <sys/zfs_znode.h>
390a586ceaSMark Shellenbaum #include <sys/zfs_sa.h>
400a586ceaSMark Shellenbaum #include <sys/sa.h>
410a586ceaSMark Shellenbaum #include <sys/sa_impl.h>
42fa9e4066Sahrens #include <sys/vdev.h>
43fa9e4066Sahrens #include <sys/vdev_impl.h>
44fa9e4066Sahrens #include <sys/metaslab_impl.h>
45fa9e4066Sahrens #include <sys/dmu_objset.h>
46fa9e4066Sahrens #include <sys/dsl_dir.h>
47fa9e4066Sahrens #include <sys/dsl_dataset.h>
48fa9e4066Sahrens #include <sys/dsl_pool.h>
49fa9e4066Sahrens #include <sys/dbuf.h>
50fa9e4066Sahrens #include <sys/zil.h>
51fa9e4066Sahrens #include <sys/zil_impl.h>
52fa9e4066Sahrens #include <sys/stat.h>
53fa9e4066Sahrens #include <sys/resource.h>
54fa9e4066Sahrens #include <sys/dmu_traverse.h>
55fa9e4066Sahrens #include <sys/zio_checksum.h>
56fa9e4066Sahrens #include <sys/zio_compress.h>
57e0d35c44Smarks #include <sys/zfs_fuid.h>
5888b7b0f2SMatthew Ahrens #include <sys/arc.h>
59b24ab676SJeff Bonwick #include <sys/ddt.h>
60ad135b5dSChristopher Siden #include <sys/zfeature.h>
614445fffbSMatthew Ahrens #include <zfs_comutil.h>
62de6628f0Sck #undef ZFS_MAXNAMELEN
63de6628f0Sck #undef verify
64de6628f0Sck #include <libzfs.h>
65fa9e4066Sahrens 
66e690fb27SChristopher Siden #define	ZDB_COMPRESS_NAME(idx) ((idx) < ZIO_COMPRESS_FUNCTIONS ?	\
67e690fb27SChristopher Siden 	zio_compress_table[(idx)].ci_name : "UNKNOWN")
68e690fb27SChristopher Siden #define	ZDB_CHECKSUM_NAME(idx) ((idx) < ZIO_CHECKSUM_FUNCTIONS ?	\
69e690fb27SChristopher Siden 	zio_checksum_table[(idx)].ci_name : "UNKNOWN")
70e690fb27SChristopher Siden #define	ZDB_OT_NAME(idx) ((idx) < DMU_OT_NUMTYPES ?	\
71e690fb27SChristopher Siden 	dmu_ot[(idx)].ot_name : DMU_OT_IS_VALID(idx) ?	\
72e690fb27SChristopher Siden 	dmu_ot_byteswap[DMU_OT_BYTESWAP(idx)].ob_name : "UNKNOWN")
73e690fb27SChristopher Siden #define	ZDB_OT_TYPE(idx) ((idx) < DMU_OT_NUMTYPES ? (idx) :		\
74e690fb27SChristopher Siden 	(((idx) == DMU_OTN_ZAP_DATA || (idx) == DMU_OTN_ZAP_METADATA) ?	\
75e690fb27SChristopher Siden 	DMU_OT_ZAP_OTHER : DMU_OT_NUMTYPES))
766de8f417SVictor Latushkin 
77feef89cfSVictor Latushkin #ifndef lint
787fd05ac4SMatthew Ahrens extern boolean_t zfs_recover;
7906be9802SMatthew Ahrens extern uint64_t zfs_arc_max, zfs_arc_meta_limit;
80*f7950bf1SMatthew 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;
84*f7950bf1SMatthew Ahrens int zfs_vdev_async_read_max_active;
85feef89cfSVictor Latushkin #endif
86feef89cfSVictor Latushkin 
87fa9e4066Sahrens const char cmdname[] = "zdb";
88fa9e4066Sahrens uint8_t dump_opt[256];
89fa9e4066Sahrens 
90fa9e4066Sahrens typedef void object_viewer_t(objset_t *, uint64_t, void *data, size_t size);
91fa9e4066Sahrens 
92fa9e4066Sahrens extern void dump_intent_log(zilog_t *);
93fa9e4066Sahrens uint64_t *zopt_object = NULL;
94fa9e4066Sahrens int zopt_objects = 0;
95de6628f0Sck libzfs_handle_t *g_zfs;
9606be9802SMatthew Ahrens uint64_t max_inflight = 1000;
97fa9e4066Sahrens 
98fa9e4066Sahrens /*
99fa9e4066Sahrens  * These libumem hooks provide a reasonable set of defaults for the allocator's
100fa9e4066Sahrens  * debugging facilities.
101fa9e4066Sahrens  */
102fa9e4066Sahrens const char *
103fa9e4066Sahrens _umem_debug_init()
104fa9e4066Sahrens {
105fa9e4066Sahrens 	return ("default,verbose"); /* $UMEM_DEBUG setting */
106fa9e4066Sahrens }
107fa9e4066Sahrens 
108fa9e4066Sahrens const char *
109fa9e4066Sahrens _umem_logging_init(void)
110fa9e4066Sahrens {
111fa9e4066Sahrens 	return ("fail,contents"); /* $UMEM_LOGGING setting */
112fa9e4066Sahrens }
113fa9e4066Sahrens 
114fa9e4066Sahrens static void
115fa9e4066Sahrens usage(void)
116fa9e4066Sahrens {
117fa9e4066Sahrens 	(void) fprintf(stderr,
1182e4c9986SGeorge Wilson 	    "Usage: %s [-CumMdibcsDvhLXFPA] [-t txg] [-e [-p path...]] "
1192e4c9986SGeorge Wilson 	    "[-U config] [-I inflight I/Os] [-x dumpdir] poolname [object...]\n"
12031d7e8faSGeorge Wilson 	    "       %s [-divPA] [-e -p path...] [-U config] dataset "
12131d7e8faSGeorge Wilson 	    "[object...]\n"
1222e4c9986SGeorge Wilson 	    "       %s -mM [-LXFPA] [-t txg] [-e [-p path...]] [-U config] "
12390e894e2SRichard Lowe 	    "poolname [vdev [metaslab...]]\n"
12490e894e2SRichard Lowe 	    "       %s -R [-A] [-e [-p path...]] poolname "
12590e894e2SRichard Lowe 	    "vdev:offset:size[:flags]\n"
12631d7e8faSGeorge Wilson 	    "       %s -S [-PA] [-e [-p path...]] [-U config] poolname\n"
12790e894e2SRichard Lowe 	    "       %s -l [-uA] device\n"
12890e894e2SRichard Lowe 	    "       %s -C [-A] [-U config]\n\n",
129bbfd46c4SJeff Bonwick 	    cmdname, cmdname, cmdname, cmdname, cmdname, cmdname, cmdname);
1303ad6c7f9SVictor Latushkin 
1313ad6c7f9SVictor Latushkin 	(void) fprintf(stderr, "    Dataset name must include at least one "
1323ad6c7f9SVictor Latushkin 	    "separator character '/' or '@'\n");
1333ad6c7f9SVictor Latushkin 	(void) fprintf(stderr, "    If dataset name is specified, only that "
1343ad6c7f9SVictor Latushkin 	    "dataset is dumped\n");
1353ad6c7f9SVictor Latushkin 	(void) fprintf(stderr, "    If object numbers are specified, only "
1363ad6c7f9SVictor Latushkin 	    "those objects are dumped\n\n");
1373ad6c7f9SVictor Latushkin 	(void) fprintf(stderr, "    Options to control amount of output:\n");
1383ad6c7f9SVictor Latushkin 	(void) fprintf(stderr, "        -u uberblock\n");
1393ad6c7f9SVictor Latushkin 	(void) fprintf(stderr, "        -d dataset(s)\n");
1403ad6c7f9SVictor Latushkin 	(void) fprintf(stderr, "        -i intent logs\n");
14107428bdfSVictor Latushkin 	(void) fprintf(stderr, "        -C config (or cachefile if alone)\n");
1428f18d1faSGeorge Wilson 	(void) fprintf(stderr, "        -h pool history\n");
1433ad6c7f9SVictor Latushkin 	(void) fprintf(stderr, "        -b block statistics\n");
1443ad6c7f9SVictor Latushkin 	(void) fprintf(stderr, "        -m metaslabs\n");
1452e4c9986SGeorge Wilson 	(void) fprintf(stderr, "        -M metaslab groups\n");
1463ad6c7f9SVictor Latushkin 	(void) fprintf(stderr, "        -c checksum all metadata (twice for "
1476365109dSVictor Latushkin 	    "all data) blocks\n");
1483ad6c7f9SVictor Latushkin 	(void) fprintf(stderr, "        -s report stats on zdb's I/O\n");
149f0ba89beSJeff Bonwick 	(void) fprintf(stderr, "        -D dedup statistics\n");
150b24ab676SJeff Bonwick 	(void) fprintf(stderr, "        -S simulate dedup to measure effect\n");
1513ad6c7f9SVictor Latushkin 	(void) fprintf(stderr, "        -v verbose (applies to all others)\n");
152fa9e4066Sahrens 	(void) fprintf(stderr, "        -l dump label contents\n");
15382a0a985SVictor Latushkin 	(void) fprintf(stderr, "        -L disable leak tracking (do not "
15482a0a985SVictor Latushkin 	    "load spacemaps)\n");
155d41e7643Sek 	(void) fprintf(stderr, "        -R read and display block from a "
1563ad6c7f9SVictor Latushkin 	    "device\n\n");
1573ad6c7f9SVictor Latushkin 	(void) fprintf(stderr, "    Below options are intended for use "
158df15e419SMatthew Ahrens 	    "with other options:\n");
159feef89cfSVictor Latushkin 	(void) fprintf(stderr, "        -A ignore assertions (-A), enable "
160feef89cfSVictor Latushkin 	    "panic recovery (-AA) or both (-AAA)\n");
161c8ee1847SVictor Latushkin 	(void) fprintf(stderr, "        -F attempt automatic rewind within "
162c8ee1847SVictor Latushkin 	    "safe range of transaction groups\n");
1633ad6c7f9SVictor Latushkin 	(void) fprintf(stderr, "        -U <cachefile_path> -- use alternate "
1643ad6c7f9SVictor Latushkin 	    "cachefile\n");
165c8ee1847SVictor Latushkin 	(void) fprintf(stderr, "        -X attempt extreme rewind (does not "
166c8ee1847SVictor Latushkin 	    "work with dataset)\n");
1673ad6c7f9SVictor Latushkin 	(void) fprintf(stderr, "        -e pool is exported/destroyed/"
1683ad6c7f9SVictor Latushkin 	    "has altroot/not in a cachefile\n");
1693ad6c7f9SVictor Latushkin 	(void) fprintf(stderr, "        -p <path> -- use one or more with "
1703ad6c7f9SVictor Latushkin 	    "-e to specify path to vdev dir\n");
171df15e419SMatthew Ahrens 	(void) fprintf(stderr, "        -x <dumpdir> -- "
172df15e419SMatthew Ahrens 	    "dump all read blocks into specified directory\n");
17390e894e2SRichard Lowe 	(void) fprintf(stderr, "        -P print numbers in parseable form\n");
1743ad6c7f9SVictor Latushkin 	(void) fprintf(stderr, "        -t <txg> -- highest txg to use when "
1752e551927SVictor Latushkin 	    "searching for uberblocks\n");
1762e4c9986SGeorge Wilson 	(void) fprintf(stderr, "        -I <number of inflight I/Os> -- "
177df15e419SMatthew Ahrens 	    "specify the maximum number of "
178df15e419SMatthew Ahrens 	    "checksumming I/Os [default is 200]\n");
179fa9e4066Sahrens 	(void) fprintf(stderr, "Specify an option more than once (e.g. -bb) "
180fa9e4066Sahrens 	    "to make only that option verbose\n");
181fa9e4066Sahrens 	(void) fprintf(stderr, "Default is to dump everything non-verbosely\n");
182fa9e4066Sahrens 	exit(1);
183fa9e4066Sahrens }
184fa9e4066Sahrens 
185ccba0801SRich Morris /*
186ccba0801SRich Morris  * Called for usage errors that are discovered after a call to spa_open(),
187ccba0801SRich Morris  * dmu_bonus_hold(), or pool_match().  abort() is called for other errors.
188ccba0801SRich Morris  */
189ccba0801SRich Morris 
190fa9e4066Sahrens static void
191fa9e4066Sahrens fatal(const char *fmt, ...)
192fa9e4066Sahrens {
193fa9e4066Sahrens 	va_list ap;
194fa9e4066Sahrens 
195fa9e4066Sahrens 	va_start(ap, fmt);
196fa9e4066Sahrens 	(void) fprintf(stderr, "%s: ", cmdname);
197fa9e4066Sahrens 	(void) vfprintf(stderr, fmt, ap);
198fa9e4066Sahrens 	va_end(ap);
199fa9e4066Sahrens 	(void) fprintf(stderr, "\n");
200fa9e4066Sahrens 
201ccba0801SRich Morris 	exit(1);
202fa9e4066Sahrens }
203fa9e4066Sahrens 
204fa9e4066Sahrens /* ARGSUSED */
205fa9e4066Sahrens static void
206fa9e4066Sahrens dump_packed_nvlist(objset_t *os, uint64_t object, void *data, size_t size)
207fa9e4066Sahrens {
208fa9e4066Sahrens 	nvlist_t *nv;
209fa9e4066Sahrens 	size_t nvsize = *(uint64_t *)data;
210fa9e4066Sahrens 	char *packed = umem_alloc(nvsize, UMEM_NOFAIL);
211fa9e4066Sahrens 
2127bfdf011SNeil Perrin 	VERIFY(0 == dmu_read(os, object, 0, nvsize, packed, DMU_READ_PREFETCH));
213fa9e4066Sahrens 
214fa9e4066Sahrens 	VERIFY(nvlist_unpack(packed, nvsize, &nv, 0) == 0);
215fa9e4066Sahrens 
216fa9e4066Sahrens 	umem_free(packed, nvsize);
217fa9e4066Sahrens 
218fa9e4066Sahrens 	dump_nvlist(nv, 8);
219fa9e4066Sahrens 
220fa9e4066Sahrens 	nvlist_free(nv);
221fa9e4066Sahrens }
222fa9e4066Sahrens 
2234445fffbSMatthew Ahrens /* ARGSUSED */
2244445fffbSMatthew Ahrens static void
2254445fffbSMatthew Ahrens dump_history_offsets(objset_t *os, uint64_t object, void *data, size_t size)
2264445fffbSMatthew Ahrens {
2274445fffbSMatthew Ahrens 	spa_history_phys_t *shp = data;
2284445fffbSMatthew Ahrens 
2294445fffbSMatthew Ahrens 	if (shp == NULL)
2304445fffbSMatthew Ahrens 		return;
2314445fffbSMatthew Ahrens 
2324445fffbSMatthew Ahrens 	(void) printf("\t\tpool_create_len = %llu\n",
2334445fffbSMatthew Ahrens 	    (u_longlong_t)shp->sh_pool_create_len);
2344445fffbSMatthew Ahrens 	(void) printf("\t\tphys_max_off = %llu\n",
2354445fffbSMatthew Ahrens 	    (u_longlong_t)shp->sh_phys_max_off);
2364445fffbSMatthew Ahrens 	(void) printf("\t\tbof = %llu\n",
2374445fffbSMatthew Ahrens 	    (u_longlong_t)shp->sh_bof);
2384445fffbSMatthew Ahrens 	(void) printf("\t\teof = %llu\n",
2394445fffbSMatthew Ahrens 	    (u_longlong_t)shp->sh_eof);
2404445fffbSMatthew Ahrens 	(void) printf("\t\trecords_lost = %llu\n",
2414445fffbSMatthew Ahrens 	    (u_longlong_t)shp->sh_records_lost);
2424445fffbSMatthew Ahrens }
2434445fffbSMatthew Ahrens 
2443f9d6ad7SLin Ling static void
2453f9d6ad7SLin Ling zdb_nicenum(uint64_t num, char *buf)
2463f9d6ad7SLin Ling {
2473f9d6ad7SLin Ling 	if (dump_opt['P'])
2483f9d6ad7SLin Ling 		(void) sprintf(buf, "%llu", (longlong_t)num);
2493f9d6ad7SLin Ling 	else
2503f9d6ad7SLin Ling 		nicenum(num, buf);
2513f9d6ad7SLin Ling }
2523f9d6ad7SLin Ling 
253490d05b9SMatthew Ahrens const char histo_stars[] = "****************************************";
254490d05b9SMatthew Ahrens const int histo_width = sizeof (histo_stars) - 1;
255fa9e4066Sahrens 
256fa9e4066Sahrens static void
2570713e232SGeorge Wilson dump_histogram(const uint64_t *histo, int size, int offset)
258fa9e4066Sahrens {
259fa9e4066Sahrens 	int i;
260490d05b9SMatthew Ahrens 	int minidx = size - 1;
261fa9e4066Sahrens 	int maxidx = 0;
262fa9e4066Sahrens 	uint64_t max = 0;
263fa9e4066Sahrens 
264490d05b9SMatthew Ahrens 	for (i = 0; i < size; i++) {
265fa9e4066Sahrens 		if (histo[i] > max)
266fa9e4066Sahrens 			max = histo[i];
267fa9e4066Sahrens 		if (histo[i] > 0 && i > maxidx)
268fa9e4066Sahrens 			maxidx = i;
269fa9e4066Sahrens 		if (histo[i] > 0 && i < minidx)
270fa9e4066Sahrens 			minidx = i;
271fa9e4066Sahrens 	}
272fa9e4066Sahrens 
273490d05b9SMatthew Ahrens 	if (max < histo_width)
274490d05b9SMatthew Ahrens 		max = histo_width;
275fa9e4066Sahrens 
276490d05b9SMatthew Ahrens 	for (i = minidx; i <= maxidx; i++) {
277490d05b9SMatthew Ahrens 		(void) printf("\t\t\t%3u: %6llu %s\n",
2780713e232SGeorge Wilson 		    i + offset, (u_longlong_t)histo[i],
279490d05b9SMatthew Ahrens 		    &histo_stars[(max - histo[i]) * histo_width / max]);
280490d05b9SMatthew Ahrens 	}
281fa9e4066Sahrens }
282fa9e4066Sahrens 
283fa9e4066Sahrens static void
284fa9e4066Sahrens dump_zap_stats(objset_t *os, uint64_t object)
285fa9e4066Sahrens {
286fa9e4066Sahrens 	int error;
287fa9e4066Sahrens 	zap_stats_t zs;
288fa9e4066Sahrens 
289fa9e4066Sahrens 	error = zap_get_stats(os, object, &zs);
290fa9e4066Sahrens 	if (error)
291fa9e4066Sahrens 		return;
292fa9e4066Sahrens 
293fa9e4066Sahrens 	if (zs.zs_ptrtbl_len == 0) {
294fa9e4066Sahrens 		ASSERT(zs.zs_num_blocks == 1);
295fa9e4066Sahrens 		(void) printf("\tmicrozap: %llu bytes, %llu entries\n",
296fa9e4066Sahrens 		    (u_longlong_t)zs.zs_blocksize,
297fa9e4066Sahrens 		    (u_longlong_t)zs.zs_num_entries);
298fa9e4066Sahrens 		return;
299fa9e4066Sahrens 	}
300fa9e4066Sahrens 
301fa9e4066Sahrens 	(void) printf("\tFat ZAP stats:\n");
3028248818dSnd 
3038248818dSnd 	(void) printf("\t\tPointer table:\n");
3048248818dSnd 	(void) printf("\t\t\t%llu elements\n",
305fa9e4066Sahrens 	    (u_longlong_t)zs.zs_ptrtbl_len);
3068248818dSnd 	(void) printf("\t\t\tzt_blk: %llu\n",
3078248818dSnd 	    (u_longlong_t)zs.zs_ptrtbl_zt_blk);
3088248818dSnd 	(void) printf("\t\t\tzt_numblks: %llu\n",
3098248818dSnd 	    (u_longlong_t)zs.zs_ptrtbl_zt_numblks);
3108248818dSnd 	(void) printf("\t\t\tzt_shift: %llu\n",
3118248818dSnd 	    (u_longlong_t)zs.zs_ptrtbl_zt_shift);
3128248818dSnd 	(void) printf("\t\t\tzt_blks_copied: %llu\n",
3138248818dSnd 	    (u_longlong_t)zs.zs_ptrtbl_blks_copied);
3148248818dSnd 	(void) printf("\t\t\tzt_nextblk: %llu\n",
3158248818dSnd 	    (u_longlong_t)zs.zs_ptrtbl_nextblk);
3168248818dSnd 
317fa9e4066Sahrens 	(void) printf("\t\tZAP entries: %llu\n",
318fa9e4066Sahrens 	    (u_longlong_t)zs.zs_num_entries);
319fa9e4066Sahrens 	(void) printf("\t\tLeaf blocks: %llu\n",
320fa9e4066Sahrens 	    (u_longlong_t)zs.zs_num_leafs);
321fa9e4066Sahrens 	(void) printf("\t\tTotal blocks: %llu\n",
322fa9e4066Sahrens 	    (u_longlong_t)zs.zs_num_blocks);
3238248818dSnd 	(void) printf("\t\tzap_block_type: 0x%llx\n",
3248248818dSnd 	    (u_longlong_t)zs.zs_block_type);
3258248818dSnd 	(void) printf("\t\tzap_magic: 0x%llx\n",
3268248818dSnd 	    (u_longlong_t)zs.zs_magic);
3278248818dSnd 	(void) printf("\t\tzap_salt: 0x%llx\n",
3288248818dSnd 	    (u_longlong_t)zs.zs_salt);
329fa9e4066Sahrens 
330fa9e4066Sahrens 	(void) printf("\t\tLeafs with 2^n pointers:\n");
3310713e232SGeorge Wilson 	dump_histogram(zs.zs_leafs_with_2n_pointers, ZAP_HISTOGRAM_SIZE, 0);
332fa9e4066Sahrens 
333fa9e4066Sahrens 	(void) printf("\t\tBlocks with n*5 entries:\n");
3340713e232SGeorge Wilson 	dump_histogram(zs.zs_blocks_with_n5_entries, ZAP_HISTOGRAM_SIZE, 0);
335fa9e4066Sahrens 
336fa9e4066Sahrens 	(void) printf("\t\tBlocks n/10 full:\n");
3370713e232SGeorge Wilson 	dump_histogram(zs.zs_blocks_n_tenths_full, ZAP_HISTOGRAM_SIZE, 0);
338fa9e4066Sahrens 
339fa9e4066Sahrens 	(void) printf("\t\tEntries with n chunks:\n");
3400713e232SGeorge Wilson 	dump_histogram(zs.zs_entries_using_n_chunks, ZAP_HISTOGRAM_SIZE, 0);
341fa9e4066Sahrens 
342fa9e4066Sahrens 	(void) printf("\t\tBuckets with n entries:\n");
3430713e232SGeorge Wilson 	dump_histogram(zs.zs_buckets_with_n_entries, ZAP_HISTOGRAM_SIZE, 0);
344fa9e4066Sahrens }
345fa9e4066Sahrens 
346fa9e4066Sahrens /*ARGSUSED*/
347fa9e4066Sahrens static void
348fa9e4066Sahrens dump_none(objset_t *os, uint64_t object, void *data, size_t size)
349fa9e4066Sahrens {
350fa9e4066Sahrens }
351fa9e4066Sahrens 
3526de8f417SVictor Latushkin /*ARGSUSED*/
3536de8f417SVictor Latushkin static void
3546de8f417SVictor Latushkin dump_unknown(objset_t *os, uint64_t object, void *data, size_t size)
3556de8f417SVictor Latushkin {
3566de8f417SVictor Latushkin 	(void) printf("\tUNKNOWN OBJECT TYPE\n");
3576de8f417SVictor Latushkin }
3586de8f417SVictor Latushkin 
359fa9e4066Sahrens /*ARGSUSED*/
360fa9e4066Sahrens void
361fa9e4066Sahrens dump_uint8(objset_t *os, uint64_t object, void *data, size_t size)
362fa9e4066Sahrens {
363fa9e4066Sahrens }
364fa9e4066Sahrens 
365fa9e4066Sahrens /*ARGSUSED*/
366fa9e4066Sahrens static void
367fa9e4066Sahrens dump_uint64(objset_t *os, uint64_t object, void *data, size_t size)
368fa9e4066Sahrens {
369fa9e4066Sahrens }
370fa9e4066Sahrens 
371fa9e4066Sahrens /*ARGSUSED*/
372fa9e4066Sahrens static void
373fa9e4066Sahrens dump_zap(objset_t *os, uint64_t object, void *data, size_t size)
374fa9e4066Sahrens {
375fa9e4066Sahrens 	zap_cursor_t zc;
376fa9e4066Sahrens 	zap_attribute_t attr;
377fa9e4066Sahrens 	void *prop;
378fa9e4066Sahrens 	int i;
379fa9e4066Sahrens 
380fa9e4066Sahrens 	dump_zap_stats(os, object);
381fa9e4066Sahrens 	(void) printf("\n");
382fa9e4066Sahrens 
383fa9e4066Sahrens 	for (zap_cursor_init(&zc, os, object);
384fa9e4066Sahrens 	    zap_cursor_retrieve(&zc, &attr) == 0;
385fa9e4066Sahrens 	    zap_cursor_advance(&zc)) {
386fa9e4066Sahrens 		(void) printf("\t\t%s = ", attr.za_name);
387fa9e4066Sahrens 		if (attr.za_num_integers == 0) {
388fa9e4066Sahrens 			(void) printf("\n");
389fa9e4066Sahrens 			continue;
390fa9e4066Sahrens 		}
391fa9e4066Sahrens 		prop = umem_zalloc(attr.za_num_integers *
392fa9e4066Sahrens 		    attr.za_integer_length, UMEM_NOFAIL);
393fa9e4066Sahrens 		(void) zap_lookup(os, object, attr.za_name,
394fa9e4066Sahrens 		    attr.za_integer_length, attr.za_num_integers, prop);
395fa9e4066Sahrens 		if (attr.za_integer_length == 1) {
396fa9e4066Sahrens 			(void) printf("%s", (char *)prop);
397fa9e4066Sahrens 		} else {
398fa9e4066Sahrens 			for (i = 0; i < attr.za_num_integers; i++) {
399fa9e4066Sahrens 				switch (attr.za_integer_length) {
400fa9e4066Sahrens 				case 2:
401fa9e4066Sahrens 					(void) printf("%u ",
402fa9e4066Sahrens 					    ((uint16_t *)prop)[i]);
403fa9e4066Sahrens 					break;
404fa9e4066Sahrens 				case 4:
405fa9e4066Sahrens 					(void) printf("%u ",
406fa9e4066Sahrens 					    ((uint32_t *)prop)[i]);
407fa9e4066Sahrens 					break;
408fa9e4066Sahrens 				case 8:
409fa9e4066Sahrens 					(void) printf("%lld ",
410fa9e4066Sahrens 					    (u_longlong_t)((int64_t *)prop)[i]);
411fa9e4066Sahrens 					break;
412fa9e4066Sahrens 				}
413fa9e4066Sahrens 			}
414fa9e4066Sahrens 		}
415fa9e4066Sahrens 		(void) printf("\n");
416fa9e4066Sahrens 		umem_free(prop, attr.za_num_integers * attr.za_integer_length);
417fa9e4066Sahrens 	}
41887e5029aSahrens 	zap_cursor_fini(&zc);
419fa9e4066Sahrens }
420fa9e4066Sahrens 
421486ae710SMatthew Ahrens /*ARGSUSED*/
422486ae710SMatthew Ahrens static void
423486ae710SMatthew Ahrens dump_ddt_zap(objset_t *os, uint64_t object, void *data, size_t size)
424486ae710SMatthew Ahrens {
425486ae710SMatthew Ahrens 	dump_zap_stats(os, object);
426486ae710SMatthew Ahrens 	/* contents are printed elsewhere, properly decoded */
427486ae710SMatthew Ahrens }
428486ae710SMatthew Ahrens 
4290a586ceaSMark Shellenbaum /*ARGSUSED*/
4300a586ceaSMark Shellenbaum static void
4310a586ceaSMark Shellenbaum dump_sa_attrs(objset_t *os, uint64_t object, void *data, size_t size)
4320a586ceaSMark Shellenbaum {
4330a586ceaSMark Shellenbaum 	zap_cursor_t zc;
4340a586ceaSMark Shellenbaum 	zap_attribute_t attr;
4350a586ceaSMark Shellenbaum 
4360a586ceaSMark Shellenbaum 	dump_zap_stats(os, object);
4370a586ceaSMark Shellenbaum 	(void) printf("\n");
4380a586ceaSMark Shellenbaum 
4390a586ceaSMark Shellenbaum 	for (zap_cursor_init(&zc, os, object);
4400a586ceaSMark Shellenbaum 	    zap_cursor_retrieve(&zc, &attr) == 0;
4410a586ceaSMark Shellenbaum 	    zap_cursor_advance(&zc)) {
4420a586ceaSMark Shellenbaum 		(void) printf("\t\t%s = ", attr.za_name);
4430a586ceaSMark Shellenbaum 		if (attr.za_num_integers == 0) {
4440a586ceaSMark Shellenbaum 			(void) printf("\n");
4450a586ceaSMark Shellenbaum 			continue;
4460a586ceaSMark Shellenbaum 		}
4470a586ceaSMark Shellenbaum 		(void) printf(" %llx : [%d:%d:%d]\n",
4480a586ceaSMark Shellenbaum 		    (u_longlong_t)attr.za_first_integer,
4490a586ceaSMark Shellenbaum 		    (int)ATTR_LENGTH(attr.za_first_integer),
4500a586ceaSMark Shellenbaum 		    (int)ATTR_BSWAP(attr.za_first_integer),
4510a586ceaSMark Shellenbaum 		    (int)ATTR_NUM(attr.za_first_integer));
4520a586ceaSMark Shellenbaum 	}
4530a586ceaSMark Shellenbaum 	zap_cursor_fini(&zc);
4540a586ceaSMark Shellenbaum }
4550a586ceaSMark Shellenbaum 
4560a586ceaSMark Shellenbaum /*ARGSUSED*/
4570a586ceaSMark Shellenbaum static void
4580a586ceaSMark Shellenbaum dump_sa_layouts(objset_t *os, uint64_t object, void *data, size_t size)
4590a586ceaSMark Shellenbaum {
4600a586ceaSMark Shellenbaum 	zap_cursor_t zc;
4610a586ceaSMark Shellenbaum 	zap_attribute_t attr;
4620a586ceaSMark Shellenbaum 	uint16_t *layout_attrs;
4630a586ceaSMark Shellenbaum 	int i;
4640a586ceaSMark Shellenbaum 
4650a586ceaSMark Shellenbaum 	dump_zap_stats(os, object);
4660a586ceaSMark Shellenbaum 	(void) printf("\n");
4670a586ceaSMark Shellenbaum 
4680a586ceaSMark Shellenbaum 	for (zap_cursor_init(&zc, os, object);
4690a586ceaSMark Shellenbaum 	    zap_cursor_retrieve(&zc, &attr) == 0;
4700a586ceaSMark Shellenbaum 	    zap_cursor_advance(&zc)) {
4710a586ceaSMark Shellenbaum 		(void) printf("\t\t%s = [", attr.za_name);
4720a586ceaSMark Shellenbaum 		if (attr.za_num_integers == 0) {
4730a586ceaSMark Shellenbaum 			(void) printf("\n");
4740a586ceaSMark Shellenbaum 			continue;
4750a586ceaSMark Shellenbaum 		}
4760a586ceaSMark Shellenbaum 
4770a586ceaSMark Shellenbaum 		VERIFY(attr.za_integer_length == 2);
4780a586ceaSMark Shellenbaum 		layout_attrs = umem_zalloc(attr.za_num_integers *
4790a586ceaSMark Shellenbaum 		    attr.za_integer_length, UMEM_NOFAIL);
4800a586ceaSMark Shellenbaum 
4810a586ceaSMark Shellenbaum 		VERIFY(zap_lookup(os, object, attr.za_name,
4820a586ceaSMark Shellenbaum 		    attr.za_integer_length,
4830a586ceaSMark Shellenbaum 		    attr.za_num_integers, layout_attrs) == 0);
4840a586ceaSMark Shellenbaum 
4850a586ceaSMark Shellenbaum 		for (i = 0; i != attr.za_num_integers; i++)
4860a586ceaSMark Shellenbaum 			(void) printf(" %d ", (int)layout_attrs[i]);
4870a586ceaSMark Shellenbaum 		(void) printf("]\n");
4880a586ceaSMark Shellenbaum 		umem_free(layout_attrs,
4890a586ceaSMark Shellenbaum 		    attr.za_num_integers * attr.za_integer_length);
4900a586ceaSMark Shellenbaum 	}
4910a586ceaSMark Shellenbaum 	zap_cursor_fini(&zc);
4920a586ceaSMark Shellenbaum }
4930a586ceaSMark Shellenbaum 
494e7437265Sahrens /*ARGSUSED*/
495e7437265Sahrens static void
496e7437265Sahrens dump_zpldir(objset_t *os, uint64_t object, void *data, size_t size)
497e7437265Sahrens {
498e7437265Sahrens 	zap_cursor_t zc;
499e7437265Sahrens 	zap_attribute_t attr;
500e7437265Sahrens 	const char *typenames[] = {
501e7437265Sahrens 		/* 0 */ "not specified",
502e7437265Sahrens 		/* 1 */ "FIFO",
503e7437265Sahrens 		/* 2 */ "Character Device",
504e7437265Sahrens 		/* 3 */ "3 (invalid)",
505e7437265Sahrens 		/* 4 */ "Directory",
506e7437265Sahrens 		/* 5 */ "5 (invalid)",
507e7437265Sahrens 		/* 6 */ "Block Device",
508e7437265Sahrens 		/* 7 */ "7 (invalid)",
509e7437265Sahrens 		/* 8 */ "Regular File",
510e7437265Sahrens 		/* 9 */ "9 (invalid)",
511e7437265Sahrens 		/* 10 */ "Symbolic Link",
512e7437265Sahrens 		/* 11 */ "11 (invalid)",
513e7437265Sahrens 		/* 12 */ "Socket",
514e7437265Sahrens 		/* 13 */ "Door",
515e7437265Sahrens 		/* 14 */ "Event Port",
516e7437265Sahrens 		/* 15 */ "15 (invalid)",
517e7437265Sahrens 	};
518e7437265Sahrens 
519e7437265Sahrens 	dump_zap_stats(os, object);
520e7437265Sahrens 	(void) printf("\n");
521e7437265Sahrens 
522e7437265Sahrens 	for (zap_cursor_init(&zc, os, object);
523e7437265Sahrens 	    zap_cursor_retrieve(&zc, &attr) == 0;
524e7437265Sahrens 	    zap_cursor_advance(&zc)) {
525e7437265Sahrens 		(void) printf("\t\t%s = %lld (type: %s)\n",
526e7437265Sahrens 		    attr.za_name, ZFS_DIRENT_OBJ(attr.za_first_integer),
527e7437265Sahrens 		    typenames[ZFS_DIRENT_TYPE(attr.za_first_integer)]);
528e7437265Sahrens 	}
529e7437265Sahrens 	zap_cursor_fini(&zc);
530e7437265Sahrens }
531e7437265Sahrens 
5320713e232SGeorge Wilson int
5330713e232SGeorge Wilson get_dtl_refcount(vdev_t *vd)
5340713e232SGeorge Wilson {
5350713e232SGeorge Wilson 	int refcount = 0;
5360713e232SGeorge Wilson 
5370713e232SGeorge Wilson 	if (vd->vdev_ops->vdev_op_leaf) {
5380713e232SGeorge Wilson 		space_map_t *sm = vd->vdev_dtl_sm;
5390713e232SGeorge Wilson 
5400713e232SGeorge Wilson 		if (sm != NULL &&
5410713e232SGeorge Wilson 		    sm->sm_dbuf->db_size == sizeof (space_map_phys_t))
5420713e232SGeorge Wilson 			return (1);
5430713e232SGeorge Wilson 		return (0);
5440713e232SGeorge Wilson 	}
5450713e232SGeorge Wilson 
5460713e232SGeorge Wilson 	for (int c = 0; c < vd->vdev_children; c++)
5470713e232SGeorge Wilson 		refcount += get_dtl_refcount(vd->vdev_child[c]);
5480713e232SGeorge Wilson 	return (refcount);
5490713e232SGeorge Wilson }
5500713e232SGeorge Wilson 
5510713e232SGeorge Wilson int
5520713e232SGeorge Wilson get_metaslab_refcount(vdev_t *vd)
5530713e232SGeorge Wilson {
5540713e232SGeorge Wilson 	int refcount = 0;
5550713e232SGeorge Wilson 
5562e4c9986SGeorge Wilson 	if (vd->vdev_top == vd && !vd->vdev_removing) {
5570713e232SGeorge Wilson 		for (int m = 0; m < vd->vdev_ms_count; m++) {
5580713e232SGeorge Wilson 			space_map_t *sm = vd->vdev_ms[m]->ms_sm;
5590713e232SGeorge Wilson 
5600713e232SGeorge Wilson 			if (sm != NULL &&
5610713e232SGeorge Wilson 			    sm->sm_dbuf->db_size == sizeof (space_map_phys_t))
5620713e232SGeorge Wilson 				refcount++;
5630713e232SGeorge Wilson 		}
5640713e232SGeorge Wilson 	}
5650713e232SGeorge Wilson 	for (int c = 0; c < vd->vdev_children; c++)
5660713e232SGeorge Wilson 		refcount += get_metaslab_refcount(vd->vdev_child[c]);
5670713e232SGeorge Wilson 
5680713e232SGeorge Wilson 	return (refcount);
5690713e232SGeorge Wilson }
5700713e232SGeorge Wilson 
5710713e232SGeorge Wilson static int
5720713e232SGeorge Wilson verify_spacemap_refcounts(spa_t *spa)
5730713e232SGeorge Wilson {
5742acef22dSMatthew Ahrens 	uint64_t expected_refcount = 0;
5752acef22dSMatthew Ahrens 	uint64_t actual_refcount;
5760713e232SGeorge Wilson 
5772acef22dSMatthew Ahrens 	(void) feature_get_refcount(spa,
5782acef22dSMatthew Ahrens 	    &spa_feature_table[SPA_FEATURE_SPACEMAP_HISTOGRAM],
5792acef22dSMatthew Ahrens 	    &expected_refcount);
5800713e232SGeorge Wilson 	actual_refcount = get_dtl_refcount(spa->spa_root_vdev);
5810713e232SGeorge Wilson 	actual_refcount += get_metaslab_refcount(spa->spa_root_vdev);
5820713e232SGeorge Wilson 
5830713e232SGeorge Wilson 	if (expected_refcount != actual_refcount) {
5842acef22dSMatthew Ahrens 		(void) printf("space map refcount mismatch: expected %lld != "
5852acef22dSMatthew Ahrens 		    "actual %lld\n",
5862acef22dSMatthew Ahrens 		    (longlong_t)expected_refcount,
5872acef22dSMatthew Ahrens 		    (longlong_t)actual_refcount);
5880713e232SGeorge Wilson 		return (2);
5890713e232SGeorge Wilson 	}
5900713e232SGeorge Wilson 	return (0);
5910713e232SGeorge Wilson }
5920713e232SGeorge Wilson 
593fa9e4066Sahrens static void
5940713e232SGeorge Wilson dump_spacemap(objset_t *os, space_map_t *sm)
595fa9e4066Sahrens {
596fa9e4066Sahrens 	uint64_t alloc, offset, entry;
5978053a263Sck 	char *ddata[] = { "ALLOC", "FREE", "CONDENSE", "INVALID",
5988053a263Sck 			    "INVALID", "INVALID", "INVALID", "INVALID" };
599fa9e4066Sahrens 
6000713e232SGeorge Wilson 	if (sm == NULL)
601fa9e4066Sahrens 		return;
602fa9e4066Sahrens 
603fa9e4066Sahrens 	/*
604fa9e4066Sahrens 	 * Print out the freelist entries in both encoded and decoded form.
605fa9e4066Sahrens 	 */
606fa9e4066Sahrens 	alloc = 0;
6070713e232SGeorge Wilson 	for (offset = 0; offset < space_map_length(sm);
6080713e232SGeorge Wilson 	    offset += sizeof (entry)) {
6090713e232SGeorge Wilson 		uint8_t mapshift = sm->sm_shift;
6100713e232SGeorge Wilson 
6110713e232SGeorge Wilson 		VERIFY0(dmu_read(os, space_map_object(sm), offset,
6127bfdf011SNeil Perrin 		    sizeof (entry), &entry, DMU_READ_PREFETCH));
613fa9e4066Sahrens 		if (SM_DEBUG_DECODE(entry)) {
6140713e232SGeorge Wilson 
61587219db7SVictor Latushkin 			(void) printf("\t    [%6llu] %s: txg %llu, pass %llu\n",
616fa9e4066Sahrens 			    (u_longlong_t)(offset / sizeof (entry)),
617fa9e4066Sahrens 			    ddata[SM_DEBUG_ACTION_DECODE(entry)],
6185ad82045Snd 			    (u_longlong_t)SM_DEBUG_TXG_DECODE(entry),
6195ad82045Snd 			    (u_longlong_t)SM_DEBUG_SYNCPASS_DECODE(entry));
620fa9e4066Sahrens 		} else {
62187219db7SVictor Latushkin 			(void) printf("\t    [%6llu]    %c  range:"
62287219db7SVictor Latushkin 			    " %010llx-%010llx  size: %06llx\n",
623fa9e4066Sahrens 			    (u_longlong_t)(offset / sizeof (entry)),
624fa9e4066Sahrens 			    SM_TYPE_DECODE(entry) == SM_ALLOC ? 'A' : 'F',
6255ad82045Snd 			    (u_longlong_t)((SM_OFFSET_DECODE(entry) <<
6260713e232SGeorge Wilson 			    mapshift) + sm->sm_start),
6275ad82045Snd 			    (u_longlong_t)((SM_OFFSET_DECODE(entry) <<
6280713e232SGeorge Wilson 			    mapshift) + sm->sm_start +
6290713e232SGeorge Wilson 			    (SM_RUN_DECODE(entry) << mapshift)),
6305ad82045Snd 			    (u_longlong_t)(SM_RUN_DECODE(entry) << mapshift));
631fa9e4066Sahrens 			if (SM_TYPE_DECODE(entry) == SM_ALLOC)
632fa9e4066Sahrens 				alloc += SM_RUN_DECODE(entry) << mapshift;
633fa9e4066Sahrens 			else
634fa9e4066Sahrens 				alloc -= SM_RUN_DECODE(entry) << mapshift;
635fa9e4066Sahrens 		}
636fa9e4066Sahrens 	}
6370713e232SGeorge Wilson 	if (alloc != space_map_allocated(sm)) {
638fa9e4066Sahrens 		(void) printf("space_map_object alloc (%llu) INCONSISTENT "
639fa9e4066Sahrens 		    "with space map summary (%llu)\n",
6400713e232SGeorge Wilson 		    (u_longlong_t)space_map_allocated(sm), (u_longlong_t)alloc);
641fa9e4066Sahrens 	}
642fa9e4066Sahrens }
643fa9e4066Sahrens 
644d6e555bdSGeorge Wilson static void
645d6e555bdSGeorge Wilson dump_metaslab_stats(metaslab_t *msp)
646d6e555bdSGeorge Wilson {
6473f9d6ad7SLin Ling 	char maxbuf[32];
6480713e232SGeorge Wilson 	range_tree_t *rt = msp->ms_tree;
6490713e232SGeorge Wilson 	avl_tree_t *t = &msp->ms_size_tree;
6500713e232SGeorge Wilson 	int free_pct = range_tree_space(rt) * 100 / msp->ms_size;
651d6e555bdSGeorge Wilson 
6520713e232SGeorge Wilson 	zdb_nicenum(metaslab_block_maxsize(msp), maxbuf);
653d6e555bdSGeorge Wilson 
65487219db7SVictor Latushkin 	(void) printf("\t %25s %10lu   %7s  %6s   %4s %4d%%\n",
655d6e555bdSGeorge Wilson 	    "segments", avl_numnodes(t), "maxsize", maxbuf,
656d6e555bdSGeorge Wilson 	    "freepct", free_pct);
6570713e232SGeorge Wilson 	(void) printf("\tIn-memory histogram:\n");
6580713e232SGeorge Wilson 	dump_histogram(rt->rt_histogram, RANGE_TREE_HISTOGRAM_SIZE, 0);
659d6e555bdSGeorge Wilson }
660d6e555bdSGeorge Wilson 
661fa9e4066Sahrens static void
662fa9e4066Sahrens dump_metaslab(metaslab_t *msp)
663fa9e4066Sahrens {
664fa9e4066Sahrens 	vdev_t *vd = msp->ms_group->mg_vd;
665fa9e4066Sahrens 	spa_t *spa = vd->vdev_spa;
6660713e232SGeorge Wilson 	space_map_t *sm = msp->ms_sm;
6673f9d6ad7SLin Ling 	char freebuf[32];
668fa9e4066Sahrens 
6690713e232SGeorge Wilson 	zdb_nicenum(msp->ms_size - space_map_allocated(sm), freebuf);
670fa9e4066Sahrens 
671fa9e4066Sahrens 	(void) printf(
67287219db7SVictor Latushkin 	    "\tmetaslab %6llu   offset %12llx   spacemap %6llu   free    %5s\n",
6730713e232SGeorge Wilson 	    (u_longlong_t)msp->ms_id, (u_longlong_t)msp->ms_start,
6740713e232SGeorge Wilson 	    (u_longlong_t)space_map_object(sm), freebuf);
675fa9e4066Sahrens 
6760713e232SGeorge Wilson 	if (dump_opt['m'] > 2 && !dump_opt['L']) {
677d6e555bdSGeorge Wilson 		mutex_enter(&msp->ms_lock);
6780713e232SGeorge Wilson 		metaslab_load_wait(msp);
6790713e232SGeorge Wilson 		if (!msp->ms_loaded) {
6800713e232SGeorge Wilson 			VERIFY0(metaslab_load(msp));
6810713e232SGeorge Wilson 			range_tree_stat_verify(msp->ms_tree);
6820713e232SGeorge Wilson 		}
683bc3975b5SVictor Latushkin 		dump_metaslab_stats(msp);
6840713e232SGeorge Wilson 		metaslab_unload(msp);
685d6e555bdSGeorge Wilson 		mutex_exit(&msp->ms_lock);
686d6e555bdSGeorge Wilson 	}
687d6e555bdSGeorge Wilson 
6880713e232SGeorge Wilson 	if (dump_opt['m'] > 1 && sm != NULL &&
6892acef22dSMatthew Ahrens 	    spa_feature_is_active(spa, SPA_FEATURE_SPACEMAP_HISTOGRAM)) {
6900713e232SGeorge Wilson 		/*
6910713e232SGeorge Wilson 		 * The space map histogram represents free space in chunks
6920713e232SGeorge Wilson 		 * of sm_shift (i.e. bucket 0 refers to 2^sm_shift).
6930713e232SGeorge Wilson 		 */
6942e4c9986SGeorge Wilson 		(void) printf("\tOn-disk histogram:\t\tfragmentation %llu\n",
6952e4c9986SGeorge Wilson 		    (u_longlong_t)msp->ms_fragmentation);
6960713e232SGeorge Wilson 		dump_histogram(sm->sm_phys->smp_histogram,
6972e4c9986SGeorge Wilson 		    SPACE_MAP_HISTOGRAM_SIZE, sm->sm_shift);
6980713e232SGeorge Wilson 	}
6990713e232SGeorge Wilson 
7000713e232SGeorge Wilson 	if (dump_opt['d'] > 5 || dump_opt['m'] > 3) {
7010713e232SGeorge Wilson 		ASSERT(msp->ms_size == (1ULL << vd->vdev_ms_shift));
702d6e555bdSGeorge Wilson 
703d6e555bdSGeorge Wilson 		mutex_enter(&msp->ms_lock);
7040713e232SGeorge Wilson 		dump_spacemap(spa->spa_meta_objset, msp->ms_sm);
705d6e555bdSGeorge Wilson 		mutex_exit(&msp->ms_lock);
706d6e555bdSGeorge Wilson 	}
70787219db7SVictor Latushkin }
708fa9e4066Sahrens 
70987219db7SVictor Latushkin static void
71087219db7SVictor Latushkin print_vdev_metaslab_header(vdev_t *vd)
71187219db7SVictor Latushkin {
71287219db7SVictor Latushkin 	(void) printf("\tvdev %10llu\n\t%-10s%5llu   %-19s   %-15s   %-10s\n",
71387219db7SVictor Latushkin 	    (u_longlong_t)vd->vdev_id,
71487219db7SVictor Latushkin 	    "metaslabs", (u_longlong_t)vd->vdev_ms_count,
71587219db7SVictor Latushkin 	    "offset", "spacemap", "free");
71687219db7SVictor Latushkin 	(void) printf("\t%15s   %19s   %15s   %10s\n",
71787219db7SVictor Latushkin 	    "---------------", "-------------------",
71887219db7SVictor Latushkin 	    "---------------", "-------------");
719fa9e4066Sahrens }
720fa9e4066Sahrens 
7212e4c9986SGeorge Wilson static void
7222e4c9986SGeorge Wilson dump_metaslab_groups(spa_t *spa)
7232e4c9986SGeorge Wilson {
7242e4c9986SGeorge Wilson 	vdev_t *rvd = spa->spa_root_vdev;
7252e4c9986SGeorge Wilson 	metaslab_class_t *mc = spa_normal_class(spa);
7262e4c9986SGeorge Wilson 	uint64_t fragmentation;
7272e4c9986SGeorge Wilson 
7282e4c9986SGeorge Wilson 	metaslab_class_histogram_verify(mc);
7292e4c9986SGeorge Wilson 
7302e4c9986SGeorge Wilson 	for (int c = 0; c < rvd->vdev_children; c++) {
7312e4c9986SGeorge Wilson 		vdev_t *tvd = rvd->vdev_child[c];
7322e4c9986SGeorge Wilson 		metaslab_group_t *mg = tvd->vdev_mg;
7332e4c9986SGeorge Wilson 
7342e4c9986SGeorge Wilson 		if (mg->mg_class != mc)
7352e4c9986SGeorge Wilson 			continue;
7362e4c9986SGeorge Wilson 
7372e4c9986SGeorge Wilson 		metaslab_group_histogram_verify(mg);
7382e4c9986SGeorge Wilson 		mg->mg_fragmentation = metaslab_group_fragmentation(mg);
7392e4c9986SGeorge Wilson 
7402e4c9986SGeorge Wilson 		(void) printf("\tvdev %10llu\t\tmetaslabs%5llu\t\t"
7412e4c9986SGeorge Wilson 		    "fragmentation",
7422e4c9986SGeorge Wilson 		    (u_longlong_t)tvd->vdev_id,
7432e4c9986SGeorge Wilson 		    (u_longlong_t)tvd->vdev_ms_count);
7442e4c9986SGeorge Wilson 		if (mg->mg_fragmentation == ZFS_FRAG_INVALID) {
7452e4c9986SGeorge Wilson 			(void) printf("%3s\n", "-");
7462e4c9986SGeorge Wilson 		} else {
7472e4c9986SGeorge Wilson 			(void) printf("%3llu%%\n",
7482e4c9986SGeorge Wilson 			    (u_longlong_t)mg->mg_fragmentation);
7492e4c9986SGeorge Wilson 		}
7502e4c9986SGeorge Wilson 		dump_histogram(mg->mg_histogram, RANGE_TREE_HISTOGRAM_SIZE, 0);
7512e4c9986SGeorge Wilson 	}
7522e4c9986SGeorge Wilson 
7532e4c9986SGeorge Wilson 	(void) printf("\tpool %s\tfragmentation", spa_name(spa));
7542e4c9986SGeorge Wilson 	fragmentation = metaslab_class_fragmentation(mc);
7552e4c9986SGeorge Wilson 	if (fragmentation == ZFS_FRAG_INVALID)
7562e4c9986SGeorge Wilson 		(void) printf("\t%3s\n", "-");
7572e4c9986SGeorge Wilson 	else
7582e4c9986SGeorge Wilson 		(void) printf("\t%3llu%%\n", (u_longlong_t)fragmentation);
7592e4c9986SGeorge Wilson 	dump_histogram(mc->mc_histogram, RANGE_TREE_HISTOGRAM_SIZE, 0);
7602e4c9986SGeorge Wilson }
7612e4c9986SGeorge Wilson 
762fa9e4066Sahrens static void
763fa9e4066Sahrens dump_metaslabs(spa_t *spa)
764fa9e4066Sahrens {
76587219db7SVictor Latushkin 	vdev_t *vd, *rvd = spa->spa_root_vdev;
76687219db7SVictor Latushkin 	uint64_t m, c = 0, children = rvd->vdev_children;
767fa9e4066Sahrens 
768fa9e4066Sahrens 	(void) printf("\nMetaslabs:\n");
769fa9e4066Sahrens 
77087219db7SVictor Latushkin 	if (!dump_opt['d'] && zopt_objects > 0) {
77187219db7SVictor Latushkin 		c = zopt_object[0];
77287219db7SVictor Latushkin 
77387219db7SVictor Latushkin 		if (c >= children)
77487219db7SVictor Latushkin 			(void) fatal("bad vdev id: %llu", (u_longlong_t)c);
775fa9e4066Sahrens 
77687219db7SVictor Latushkin 		if (zopt_objects > 1) {
77787219db7SVictor Latushkin 			vd = rvd->vdev_child[c];
77887219db7SVictor Latushkin 			print_vdev_metaslab_header(vd);
77987219db7SVictor Latushkin 
78087219db7SVictor Latushkin 			for (m = 1; m < zopt_objects; m++) {
78187219db7SVictor Latushkin 				if (zopt_object[m] < vd->vdev_ms_count)
78287219db7SVictor Latushkin 					dump_metaslab(
78387219db7SVictor Latushkin 					    vd->vdev_ms[zopt_object[m]]);
78487219db7SVictor Latushkin 				else
78587219db7SVictor Latushkin 					(void) fprintf(stderr, "bad metaslab "
78687219db7SVictor Latushkin 					    "number %llu\n",
78787219db7SVictor Latushkin 					    (u_longlong_t)zopt_object[m]);
78887219db7SVictor Latushkin 			}
78987219db7SVictor Latushkin 			(void) printf("\n");
79087219db7SVictor Latushkin 			return;
79187219db7SVictor Latushkin 		}
79287219db7SVictor Latushkin 		children = c + 1;
79387219db7SVictor Latushkin 	}
79487219db7SVictor Latushkin 	for (; c < children; c++) {
79587219db7SVictor Latushkin 		vd = rvd->vdev_child[c];
79687219db7SVictor Latushkin 		print_vdev_metaslab_header(vd);
797fa9e4066Sahrens 
798fa9e4066Sahrens 		for (m = 0; m < vd->vdev_ms_count; m++)
799fa9e4066Sahrens 			dump_metaslab(vd->vdev_ms[m]);
800fa9e4066Sahrens 		(void) printf("\n");
801fa9e4066Sahrens 	}
802fa9e4066Sahrens }
803fa9e4066Sahrens 
804b24ab676SJeff Bonwick static void
805b24ab676SJeff Bonwick dump_dde(const ddt_t *ddt, const ddt_entry_t *dde, uint64_t index)
806b24ab676SJeff Bonwick {
807b24ab676SJeff Bonwick 	const ddt_phys_t *ddp = dde->dde_phys;
808b24ab676SJeff Bonwick 	const ddt_key_t *ddk = &dde->dde_key;
809b24ab676SJeff Bonwick 	char *types[4] = { "ditto", "single", "double", "triple" };
810b24ab676SJeff Bonwick 	char blkbuf[BP_SPRINTF_LEN];
811b24ab676SJeff Bonwick 	blkptr_t blk;
812b24ab676SJeff Bonwick 
813b24ab676SJeff Bonwick 	for (int p = 0; p < DDT_PHYS_TYPES; p++, ddp++) {
814b24ab676SJeff Bonwick 		if (ddp->ddp_phys_birth == 0)
815b24ab676SJeff Bonwick 			continue;
816bbfd46c4SJeff Bonwick 		ddt_bp_create(ddt->ddt_checksum, ddk, ddp, &blk);
81743466aaeSMax Grossman 		snprintf_blkptr(blkbuf, sizeof (blkbuf), &blk);
818b24ab676SJeff Bonwick 		(void) printf("index %llx refcnt %llu %s %s\n",
819b24ab676SJeff Bonwick 		    (u_longlong_t)index, (u_longlong_t)ddp->ddp_refcnt,
820b24ab676SJeff Bonwick 		    types[p], blkbuf);
821b24ab676SJeff Bonwick 	}
822b24ab676SJeff Bonwick }
823b24ab676SJeff Bonwick 
824b24ab676SJeff Bonwick static void
825b24ab676SJeff Bonwick dump_dedup_ratio(const ddt_stat_t *dds)
826b24ab676SJeff Bonwick {
827b24ab676SJeff Bonwick 	double rL, rP, rD, D, dedup, compress, copies;
828b24ab676SJeff Bonwick 
829b24ab676SJeff Bonwick 	if (dds->dds_blocks == 0)
830b24ab676SJeff Bonwick 		return;
831b24ab676SJeff Bonwick 
832b24ab676SJeff Bonwick 	rL = (double)dds->dds_ref_lsize;
833b24ab676SJeff Bonwick 	rP = (double)dds->dds_ref_psize;
834b24ab676SJeff Bonwick 	rD = (double)dds->dds_ref_dsize;
835b24ab676SJeff Bonwick 	D = (double)dds->dds_dsize;
836b24ab676SJeff Bonwick 
837b24ab676SJeff Bonwick 	dedup = rD / D;
838b24ab676SJeff Bonwick 	compress = rL / rP;
839b24ab676SJeff Bonwick 	copies = rD / rP;
840b24ab676SJeff Bonwick 
841b24ab676SJeff Bonwick 	(void) printf("dedup = %.2f, compress = %.2f, copies = %.2f, "
842b24ab676SJeff Bonwick 	    "dedup * compress / copies = %.2f\n\n",
843b24ab676SJeff Bonwick 	    dedup, compress, copies, dedup * compress / copies);
844b24ab676SJeff Bonwick }
845b24ab676SJeff Bonwick 
846b24ab676SJeff Bonwick static void
847b24ab676SJeff Bonwick dump_ddt(ddt_t *ddt, enum ddt_type type, enum ddt_class class)
848b24ab676SJeff Bonwick {
849b24ab676SJeff Bonwick 	char name[DDT_NAMELEN];
850b24ab676SJeff Bonwick 	ddt_entry_t dde;
851b24ab676SJeff Bonwick 	uint64_t walk = 0;
852b24ab676SJeff Bonwick 	dmu_object_info_t doi;
853b24ab676SJeff Bonwick 	uint64_t count, dspace, mspace;
854b24ab676SJeff Bonwick 	int error;
855b24ab676SJeff Bonwick 
856b24ab676SJeff Bonwick 	error = ddt_object_info(ddt, type, class, &doi);
857b24ab676SJeff Bonwick 
858b24ab676SJeff Bonwick 	if (error == ENOENT)
859b24ab676SJeff Bonwick 		return;
860b24ab676SJeff Bonwick 	ASSERT(error == 0);
861b24ab676SJeff Bonwick 
8627448a079SGeorge Wilson 	if ((count = ddt_object_count(ddt, type, class)) == 0)
8637448a079SGeorge Wilson 		return;
8647448a079SGeorge Wilson 
865b24ab676SJeff Bonwick 	dspace = doi.doi_physical_blocks_512 << 9;
866b24ab676SJeff Bonwick 	mspace = doi.doi_fill_count * doi.doi_data_block_size;
867b24ab676SJeff Bonwick 
868b24ab676SJeff Bonwick 	ddt_object_name(ddt, type, class, name);
869b24ab676SJeff Bonwick 
870b24ab676SJeff Bonwick 	(void) printf("%s: %llu entries, size %llu on disk, %llu in core\n",
871b24ab676SJeff Bonwick 	    name,
872b24ab676SJeff Bonwick 	    (u_longlong_t)count,
873b24ab676SJeff Bonwick 	    (u_longlong_t)(dspace / count),
874b24ab676SJeff Bonwick 	    (u_longlong_t)(mspace / count));
875b24ab676SJeff Bonwick 
876b24ab676SJeff Bonwick 	if (dump_opt['D'] < 3)
877b24ab676SJeff Bonwick 		return;
878b24ab676SJeff Bonwick 
8799eb19f4dSGeorge Wilson 	zpool_dump_ddt(NULL, &ddt->ddt_histogram[type][class]);
880b24ab676SJeff Bonwick 
881b24ab676SJeff Bonwick 	if (dump_opt['D'] < 4)
882b24ab676SJeff Bonwick 		return;
883b24ab676SJeff Bonwick 
884b24ab676SJeff Bonwick 	if (dump_opt['D'] < 5 && class == DDT_CLASS_UNIQUE)
885b24ab676SJeff Bonwick 		return;
886b24ab676SJeff Bonwick 
887b24ab676SJeff Bonwick 	(void) printf("%s contents:\n\n", name);
888b24ab676SJeff Bonwick 
889bbfd46c4SJeff Bonwick 	while ((error = ddt_object_walk(ddt, type, class, &walk, &dde)) == 0)
890b24ab676SJeff Bonwick 		dump_dde(ddt, &dde, walk);
891b24ab676SJeff Bonwick 
892b24ab676SJeff Bonwick 	ASSERT(error == ENOENT);
893b24ab676SJeff Bonwick 
894b24ab676SJeff Bonwick 	(void) printf("\n");
895b24ab676SJeff Bonwick }
896b24ab676SJeff Bonwick 
897b24ab676SJeff Bonwick static void
898b24ab676SJeff Bonwick dump_all_ddts(spa_t *spa)
899b24ab676SJeff Bonwick {
900b24ab676SJeff Bonwick 	ddt_histogram_t ddh_total = { 0 };
901b24ab676SJeff Bonwick 	ddt_stat_t dds_total = { 0 };
902b24ab676SJeff Bonwick 
903b24ab676SJeff Bonwick 	for (enum zio_checksum c = 0; c < ZIO_CHECKSUM_FUNCTIONS; c++) {
904b24ab676SJeff Bonwick 		ddt_t *ddt = spa->spa_ddt[c];
905b24ab676SJeff Bonwick 		for (enum ddt_type type = 0; type < DDT_TYPES; type++) {
906b24ab676SJeff Bonwick 			for (enum ddt_class class = 0; class < DDT_CLASSES;
907b24ab676SJeff Bonwick 			    class++) {
908b24ab676SJeff Bonwick 				dump_ddt(ddt, type, class);
909b24ab676SJeff Bonwick 			}
910b24ab676SJeff Bonwick 		}
911b24ab676SJeff Bonwick 	}
912b24ab676SJeff Bonwick 
9139eb19f4dSGeorge Wilson 	ddt_get_dedup_stats(spa, &dds_total);
914b24ab676SJeff Bonwick 
915b24ab676SJeff Bonwick 	if (dds_total.dds_blocks == 0) {
916b24ab676SJeff Bonwick 		(void) printf("All DDTs are empty\n");
917b24ab676SJeff Bonwick 		return;
918b24ab676SJeff Bonwick 	}
919b24ab676SJeff Bonwick 
920b24ab676SJeff Bonwick 	(void) printf("\n");
921b24ab676SJeff Bonwick 
922b24ab676SJeff Bonwick 	if (dump_opt['D'] > 1) {
923b24ab676SJeff Bonwick 		(void) printf("DDT histogram (aggregated over all DDTs):\n");
9249eb19f4dSGeorge Wilson 		ddt_get_dedup_histogram(spa, &ddh_total);
9259eb19f4dSGeorge Wilson 		zpool_dump_ddt(&dds_total, &ddh_total);
926b24ab676SJeff Bonwick 	}
927b24ab676SJeff Bonwick 
928b24ab676SJeff Bonwick 	dump_dedup_ratio(&dds_total);
929b24ab676SJeff Bonwick }
930b24ab676SJeff Bonwick 
9318ad4d6ddSJeff Bonwick static void
9320713e232SGeorge Wilson dump_dtl_seg(void *arg, uint64_t start, uint64_t size)
9338ad4d6ddSJeff Bonwick {
9340713e232SGeorge Wilson 	char *prefix = arg;
9358ad4d6ddSJeff Bonwick 
9368ad4d6ddSJeff Bonwick 	(void) printf("%s [%llu,%llu) length %llu\n",
9378ad4d6ddSJeff Bonwick 	    prefix,
9388ad4d6ddSJeff Bonwick 	    (u_longlong_t)start,
9398ad4d6ddSJeff Bonwick 	    (u_longlong_t)(start + size),
9408ad4d6ddSJeff Bonwick 	    (u_longlong_t)(size));
9418ad4d6ddSJeff Bonwick }
9428ad4d6ddSJeff Bonwick 
943fa9e4066Sahrens static void
944fa9e4066Sahrens dump_dtl(vdev_t *vd, int indent)
945fa9e4066Sahrens {
9468ad4d6ddSJeff Bonwick 	spa_t *spa = vd->vdev_spa;
9478ad4d6ddSJeff Bonwick 	boolean_t required;
9488ad4d6ddSJeff Bonwick 	char *name[DTL_TYPES] = { "missing", "partial", "scrub", "outage" };
9498ad4d6ddSJeff Bonwick 	char prefix[256];
9508ad4d6ddSJeff Bonwick 
9518f18d1faSGeorge Wilson 	spa_vdev_state_enter(spa, SCL_NONE);
9528ad4d6ddSJeff Bonwick 	required = vdev_dtl_required(vd);
9538ad4d6ddSJeff Bonwick 	(void) spa_vdev_state_exit(spa, NULL, 0);
954fa9e4066Sahrens 
955fa9e4066Sahrens 	if (indent == 0)
956fa9e4066Sahrens 		(void) printf("\nDirty time logs:\n\n");
957fa9e4066Sahrens 
9588ad4d6ddSJeff Bonwick 	(void) printf("\t%*s%s [%s]\n", indent, "",
959e14bb325SJeff Bonwick 	    vd->vdev_path ? vd->vdev_path :
9608ad4d6ddSJeff Bonwick 	    vd->vdev_parent ? vd->vdev_ops->vdev_op_type : spa_name(spa),
9618ad4d6ddSJeff Bonwick 	    required ? "DTL-required" : "DTL-expendable");
962fa9e4066Sahrens 
9638ad4d6ddSJeff Bonwick 	for (int t = 0; t < DTL_TYPES; t++) {
9640713e232SGeorge Wilson 		range_tree_t *rt = vd->vdev_dtl[t];
9650713e232SGeorge Wilson 		if (range_tree_space(rt) == 0)
9668ad4d6ddSJeff Bonwick 			continue;
9678ad4d6ddSJeff Bonwick 		(void) snprintf(prefix, sizeof (prefix), "\t%*s%s",
9688ad4d6ddSJeff Bonwick 		    indent + 2, "", name[t]);
9690713e232SGeorge Wilson 		mutex_enter(rt->rt_lock);
9700713e232SGeorge Wilson 		range_tree_walk(rt, dump_dtl_seg, prefix);
9710713e232SGeorge Wilson 		mutex_exit(rt->rt_lock);
9728ad4d6ddSJeff Bonwick 		if (dump_opt['d'] > 5 && vd->vdev_children == 0)
9730713e232SGeorge Wilson 			dump_spacemap(spa->spa_meta_objset, vd->vdev_dtl_sm);
974fa9e4066Sahrens 	}
975fa9e4066Sahrens 
9768ad4d6ddSJeff Bonwick 	for (int c = 0; c < vd->vdev_children; c++)
977fa9e4066Sahrens 		dump_dtl(vd->vdev_child[c], indent + 4);
978fa9e4066Sahrens }
979fa9e4066Sahrens 
9808f18d1faSGeorge Wilson static void
9818f18d1faSGeorge Wilson dump_history(spa_t *spa)
9828f18d1faSGeorge Wilson {
9838f18d1faSGeorge Wilson 	nvlist_t **events = NULL;
9848f18d1faSGeorge Wilson 	char buf[SPA_MAXBLOCKSIZE];
985e4161df6SVictor Latushkin 	uint64_t resid, len, off = 0;
9868f18d1faSGeorge Wilson 	uint_t num = 0;
9878f18d1faSGeorge Wilson 	int error;
9888f18d1faSGeorge Wilson 	time_t tsec;
9898f18d1faSGeorge Wilson 	struct tm t;
9908f18d1faSGeorge Wilson 	char tbuf[30];
9918f18d1faSGeorge Wilson 	char internalstr[MAXPATHLEN];
9928f18d1faSGeorge Wilson 
9938f18d1faSGeorge Wilson 	do {
994e4161df6SVictor Latushkin 		len = sizeof (buf);
995e4161df6SVictor Latushkin 
9968f18d1faSGeorge Wilson 		if ((error = spa_history_get(spa, &off, &len, buf)) != 0) {
9978f18d1faSGeorge Wilson 			(void) fprintf(stderr, "Unable to read history: "
9988f18d1faSGeorge Wilson 			    "error %d\n", error);
9998f18d1faSGeorge Wilson 			return;
10008f18d1faSGeorge Wilson 		}
10018f18d1faSGeorge Wilson 
10028f18d1faSGeorge Wilson 		if (zpool_history_unpack(buf, len, &resid, &events, &num) != 0)
10038f18d1faSGeorge Wilson 			break;
10048f18d1faSGeorge Wilson 
10058f18d1faSGeorge Wilson 		off -= resid;
10068f18d1faSGeorge Wilson 	} while (len != 0);
10078f18d1faSGeorge Wilson 
10088f18d1faSGeorge Wilson 	(void) printf("\nHistory:\n");
10098f18d1faSGeorge Wilson 	for (int i = 0; i < num; i++) {
10108f18d1faSGeorge Wilson 		uint64_t time, txg, ievent;
10118f18d1faSGeorge Wilson 		char *cmd, *intstr;
10124445fffbSMatthew Ahrens 		boolean_t printed = B_FALSE;
10138f18d1faSGeorge Wilson 
10148f18d1faSGeorge Wilson 		if (nvlist_lookup_uint64(events[i], ZPOOL_HIST_TIME,
10158f18d1faSGeorge Wilson 		    &time) != 0)
10164445fffbSMatthew Ahrens 			goto next;
10178f18d1faSGeorge Wilson 		if (nvlist_lookup_string(events[i], ZPOOL_HIST_CMD,
10188f18d1faSGeorge Wilson 		    &cmd) != 0) {
10198f18d1faSGeorge Wilson 			if (nvlist_lookup_uint64(events[i],
10208f18d1faSGeorge Wilson 			    ZPOOL_HIST_INT_EVENT, &ievent) != 0)
10214445fffbSMatthew Ahrens 				goto next;
10228f18d1faSGeorge Wilson 			verify(nvlist_lookup_uint64(events[i],
10238f18d1faSGeorge Wilson 			    ZPOOL_HIST_TXG, &txg) == 0);
10248f18d1faSGeorge Wilson 			verify(nvlist_lookup_string(events[i],
10258f18d1faSGeorge Wilson 			    ZPOOL_HIST_INT_STR, &intstr) == 0);
10264445fffbSMatthew Ahrens 			if (ievent >= ZFS_NUM_LEGACY_HISTORY_EVENTS)
10274445fffbSMatthew Ahrens 				goto next;
10288f18d1faSGeorge Wilson 
10298f18d1faSGeorge Wilson 			(void) snprintf(internalstr,
10308f18d1faSGeorge Wilson 			    sizeof (internalstr),
10318f18d1faSGeorge Wilson 			    "[internal %s txg:%lld] %s",
10323f9d6ad7SLin Ling 			    zfs_history_event_names[ievent], txg,
10338f18d1faSGeorge Wilson 			    intstr);
10348f18d1faSGeorge Wilson 			cmd = internalstr;
10358f18d1faSGeorge Wilson 		}
10368f18d1faSGeorge Wilson 		tsec = time;
10378f18d1faSGeorge Wilson 		(void) localtime_r(&tsec, &t);
10388f18d1faSGeorge Wilson 		(void) strftime(tbuf, sizeof (tbuf), "%F.%T", &t);
10398f18d1faSGeorge Wilson 		(void) printf("%s %s\n", tbuf, cmd);
10404445fffbSMatthew Ahrens 		printed = B_TRUE;
10414445fffbSMatthew Ahrens 
10424445fffbSMatthew Ahrens next:
10434445fffbSMatthew Ahrens 		if (dump_opt['h'] > 1) {
10444445fffbSMatthew Ahrens 			if (!printed)
10454445fffbSMatthew Ahrens 				(void) printf("unrecognized record:\n");
10464445fffbSMatthew Ahrens 			dump_nvlist(events[i], 2);
10474445fffbSMatthew Ahrens 		}
10488f18d1faSGeorge Wilson 	}
10498f18d1faSGeorge Wilson }
10508f18d1faSGeorge Wilson 
1051fa9e4066Sahrens /*ARGSUSED*/
1052fa9e4066Sahrens static void
1053fa9e4066Sahrens dump_dnode(objset_t *os, uint64_t object, void *data, size_t size)
1054fa9e4066Sahrens {
1055fa9e4066Sahrens }
1056fa9e4066Sahrens 
1057fa9e4066Sahrens static uint64_t
10587802d7bfSMatthew Ahrens blkid2offset(const dnode_phys_t *dnp, const blkptr_t *bp,
10597802d7bfSMatthew Ahrens     const zbookmark_phys_t *zb)
1060fa9e4066Sahrens {
1061b24ab676SJeff Bonwick 	if (dnp == NULL) {
1062b24ab676SJeff Bonwick 		ASSERT(zb->zb_level < 0);
1063b24ab676SJeff Bonwick 		if (zb->zb_object == 0)
1064b24ab676SJeff Bonwick 			return (zb->zb_blkid);
1065b24ab676SJeff Bonwick 		return (zb->zb_blkid * BP_GET_LSIZE(bp));
1066b24ab676SJeff Bonwick 	}
1067b24ab676SJeff Bonwick 
1068b24ab676SJeff Bonwick 	ASSERT(zb->zb_level >= 0);
1069fa9e4066Sahrens 
1070b24ab676SJeff Bonwick 	return ((zb->zb_blkid <<
1071b24ab676SJeff Bonwick 	    (zb->zb_level * (dnp->dn_indblkshift - SPA_BLKPTRSHIFT))) *
1072fa9e4066Sahrens 	    dnp->dn_datablkszsec << SPA_MINBLOCKSHIFT);
1073fa9e4066Sahrens }
1074fa9e4066Sahrens 
107544cd46caSbillm static void
107643466aaeSMax Grossman snprintf_blkptr_compact(char *blkbuf, size_t buflen, const blkptr_t *bp)
107744cd46caSbillm {
1078cde58dbcSMatthew Ahrens 	const dva_t *dva = bp->blk_dva;
1079b24ab676SJeff Bonwick 	int ndvas = dump_opt['d'] > 5 ? BP_GET_NDVAS(bp) : 1;
1080b24ab676SJeff Bonwick 
1081490d05b9SMatthew Ahrens 	if (dump_opt['b'] >= 6) {
108243466aaeSMax Grossman 		snprintf_blkptr(blkbuf, buflen, bp);
1083b24ab676SJeff Bonwick 		return;
1084b24ab676SJeff Bonwick 	}
108544cd46caSbillm 
10865d7b4d43SMatthew Ahrens 	if (BP_IS_EMBEDDED(bp)) {
10875d7b4d43SMatthew Ahrens 		(void) sprintf(blkbuf,
10885d7b4d43SMatthew Ahrens 		    "EMBEDDED et=%u %llxL/%llxP B=%llu",
10895d7b4d43SMatthew Ahrens 		    (int)BPE_GET_ETYPE(bp),
10905d7b4d43SMatthew Ahrens 		    (u_longlong_t)BPE_GET_LSIZE(bp),
10915d7b4d43SMatthew Ahrens 		    (u_longlong_t)BPE_GET_PSIZE(bp),
10925d7b4d43SMatthew Ahrens 		    (u_longlong_t)bp->blk_birth);
10935d7b4d43SMatthew Ahrens 		return;
10945d7b4d43SMatthew Ahrens 	}
109544cd46caSbillm 
10965d7b4d43SMatthew Ahrens 	blkbuf[0] = '\0';
1097b24ab676SJeff Bonwick 	for (int i = 0; i < ndvas; i++)
109843466aaeSMax Grossman 		(void) snprintf(blkbuf + strlen(blkbuf),
109943466aaeSMax Grossman 		    buflen - strlen(blkbuf), "%llu:%llx:%llx ",
110044cd46caSbillm 		    (u_longlong_t)DVA_GET_VDEV(&dva[i]),
110144cd46caSbillm 		    (u_longlong_t)DVA_GET_OFFSET(&dva[i]),
110244cd46caSbillm 		    (u_longlong_t)DVA_GET_ASIZE(&dva[i]));
110344cd46caSbillm 
110443466aaeSMax Grossman 	if (BP_IS_HOLE(bp)) {
110543466aaeSMax Grossman 		(void) snprintf(blkbuf + strlen(blkbuf),
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),
111143466aaeSMax Grossman 		    "%llxL/%llxP F=%llu B=%llu/%llu",
111243466aaeSMax Grossman 		    (u_longlong_t)BP_GET_LSIZE(bp),
111343466aaeSMax Grossman 		    (u_longlong_t)BP_GET_PSIZE(bp),
11145d7b4d43SMatthew Ahrens 		    (u_longlong_t)BP_GET_FILL(bp),
111543466aaeSMax Grossman 		    (u_longlong_t)bp->blk_birth,
111643466aaeSMax Grossman 		    (u_longlong_t)BP_PHYSICAL_BIRTH(bp));
111743466aaeSMax Grossman 	}
111844cd46caSbillm }
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)) {
11285d7b4d43SMatthew Ahrens 		ASSERT3U(BP_GET_TYPE(bp), ==, dnp->dn_type);
11295d7b4d43SMatthew Ahrens 		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);
114588b7b0f2SMatthew Ahrens 	(void) printf("%s\n", blkbuf);
114688b7b0f2SMatthew Ahrens }
114788b7b0f2SMatthew Ahrens 
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)) {
116088b7b0f2SMatthew Ahrens 		uint32_t flags = ARC_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);
1189fa9e4066Sahrens 	}
1190fa9e4066Sahrens 
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 
1322ad135b5dSChristopher Siden /* ARGSUSED */
1323ad135b5dSChristopher Siden 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 
1360cde58dbcSMatthew Ahrens /* ARGSUSED */
1361cde58dbcSMatthew Ahrens static int
1362cde58dbcSMatthew Ahrens dump_bpobj_cb(void *arg, const blkptr_t *bp, dmu_tx_t *tx)
1363cde58dbcSMatthew Ahrens {
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 
1372fa9e4066Sahrens static void
1373d0475637SMatthew Ahrens dump_bpobj(bpobj_t *bpo, char *name, int indent)
1374fa9e4066Sahrens {
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) {
1424d0475637SMatthew Ahrens 		(void) bpobj_iterate_nofree(bpo, dump_bpobj_cb, NULL, NULL);
1425d0475637SMatthew Ahrens 		(void) printf("\n");
1426d0475637SMatthew Ahrens 	}
1427cde58dbcSMatthew Ahrens }
142844cd46caSbillm 
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);
1467fa9e4066Sahrens 
1468d0475637SMatthew Ahrens 			dump_bpobj(&dle->dle_bpobj, buf, 0);
1469d0475637SMatthew Ahrens 		} else {
1470d0475637SMatthew Ahrens 			(void) printf("mintxg %llu -> obj %llu\n",
1471d0475637SMatthew Ahrens 			    (longlong_t)dle->dle_mintxg,
1472d0475637SMatthew Ahrens 			    (longlong_t)dle->dle_bpobj.bpo_object);
1473d0475637SMatthew 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;
155255434c77Sek 	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 
160555434c77Sek 	error = zfs_obj_to_path(os, object, path, sizeof (path));
160655434c77Sek 	if (error != 0) {
160755434c77Sek 		(void) snprintf(path, sizeof (path), "\?\?\?<object#%llu>",
160855434c77Sek 		    (u_longlong_t)object);
160955434c77Sek 	}
1610fa9e4066Sahrens 	if (dump_opt['d'] < 3) {
161155434c77Sek 		(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 
162155434c77Sek 	(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			*/
1667ea8dc4b6Seschrock 	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		*/
168406eeb2adSek 	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);
1860503ad85cSMatthew Ahrens 		refdbytes = os->os_spa->spa_dsl_pool->
186174e7dc98SMatthew Ahrens 		    dp_mos_dir->dd_phys->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)
1895b24ab676SJeff Bonwick 		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;
195607428bdfSVictor Latushkin 
1957fa9e4066Sahrens 
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 
1974c5904d13Seschrock 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 
2044fa9e4066Sahrens 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 
210753b9a4a9SVictor Latushkin 			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;
211353b9a4a9SVictor Latushkin 			nvlist_free(config);
2114fa9e4066Sahrens 		}
211553b9a4a9SVictor Latushkin 		if (dump_opt['u'])
211653b9a4a9SVictor Latushkin 			dump_label_uberblocks(&label, ashift);
2117fa9e4066Sahrens 	}
2118c6065d0fSGeorge Wilson 
2119c6065d0fSGeorge Wilson 	free(path);
2120c6065d0fSGeorge Wilson 	(void) close(fd);
2121fa9e4066Sahrens }
2122fa9e4066Sahrens 
2123fa9e4066Sahrens /*ARGSUSED*/
21241d452cf5Sahrens static int
2125fd136879SMatthew Ahrens dump_one_dir(const char *dsname, void *arg)
2126fa9e4066Sahrens {
2127fa9e4066Sahrens 	int error;
2128fa9e4066Sahrens 	objset_t *os;
2129fa9e4066Sahrens 
2130503ad85cSMatthew Ahrens 	error = dmu_objset_own(dsname, DMU_OST_ANY, B_TRUE, FTAG, &os);
2131fa9e4066Sahrens 	if (error) {
2132b24ab676SJeff Bonwick 		(void) printf("Could not open %s, error %d\n", dsname, error);
21331d452cf5Sahrens 		return (0);
2134fa9e4066Sahrens 	}
2135fa9e4066Sahrens 	dump_dir(os);
2136503ad85cSMatthew Ahrens 	dmu_objset_disown(os, FTAG);
2137e0d35c44Smarks 	fuid_table_destroy();
21380a586ceaSMark Shellenbaum 	sa_loaded = B_FALSE;
21391d452cf5Sahrens 	return (0);
2140fa9e4066Sahrens }
2141fa9e4066Sahrens 
2142fa9e4066Sahrens /*
2143b24ab676SJeff Bonwick  * Block statistics.
2144fa9e4066Sahrens  */
2145490d05b9SMatthew Ahrens #define	PSIZE_HISTO_SIZE (SPA_MAXBLOCKSIZE / SPA_MINBLOCKSIZE + 1)
2146fa9e4066Sahrens typedef struct zdb_blkstats {
2147490d05b9SMatthew Ahrens 	uint64_t zb_asize;
2148490d05b9SMatthew Ahrens 	uint64_t zb_lsize;
2149490d05b9SMatthew Ahrens 	uint64_t zb_psize;
2150490d05b9SMatthew Ahrens 	uint64_t zb_count;
2151d5ee8a13SMatthew Ahrens 	uint64_t zb_gangs;
2152d5ee8a13SMatthew Ahrens 	uint64_t zb_ditto_samevdev;
2153490d05b9SMatthew Ahrens 	uint64_t zb_psize_histogram[PSIZE_HISTO_SIZE];
2154fa9e4066Sahrens } zdb_blkstats_t;
2155fa9e4066Sahrens 
2156b24ab676SJeff Bonwick /*
2157b24ab676SJeff Bonwick  * Extended object types to report deferred frees and dedup auto-ditto blocks.
2158b24ab676SJeff Bonwick  */
2159b24ab676SJeff Bonwick #define	ZDB_OT_DEFERRED	(DMU_OT_NUMTYPES + 0)
2160b24ab676SJeff Bonwick #define	ZDB_OT_DITTO	(DMU_OT_NUMTYPES + 1)
2161ad135b5dSChristopher Siden #define	ZDB_OT_OTHER	(DMU_OT_NUMTYPES + 2)
2162ad135b5dSChristopher Siden #define	ZDB_OT_TOTAL	(DMU_OT_NUMTYPES + 3)
2163b24ab676SJeff Bonwick 
2164b24ab676SJeff Bonwick static char *zdb_ot_extname[] = {
2165b24ab676SJeff Bonwick 	"deferred free",
2166b24ab676SJeff Bonwick 	"dedup ditto",
2167ad135b5dSChristopher Siden 	"other",
2168b24ab676SJeff Bonwick 	"Total",
2169b24ab676SJeff Bonwick };
2170fa9e4066Sahrens 
217188b7b0f2SMatthew Ahrens #define	ZB_TOTAL	DN_MAX_LEVELS
2172fa9e4066Sahrens 
2173fa9e4066Sahrens typedef struct zdb_cb {
2174b24ab676SJeff Bonwick 	zdb_blkstats_t	zcb_type[ZB_TOTAL + 1][ZDB_OT_TOTAL + 1];
2175b24ab676SJeff Bonwick 	uint64_t	zcb_dedup_asize;
2176b24ab676SJeff Bonwick 	uint64_t	zcb_dedup_blocks;
21775d7b4d43SMatthew Ahrens 	uint64_t	zcb_embedded_blocks[NUM_BP_EMBEDDED_TYPES];
21785d7b4d43SMatthew Ahrens 	uint64_t	zcb_embedded_histogram[NUM_BP_EMBEDDED_TYPES]
21795d7b4d43SMatthew Ahrens 	    [BPE_PAYLOAD_SIZE];
2180490d05b9SMatthew Ahrens 	uint64_t	zcb_start;
2181490d05b9SMatthew Ahrens 	uint64_t	zcb_lastprint;
2182490d05b9SMatthew Ahrens 	uint64_t	zcb_totalasize;
2183fa9e4066Sahrens 	uint64_t	zcb_errors[256];
2184fa9e4066Sahrens 	int		zcb_readfails;
2185fa9e4066Sahrens 	int		zcb_haderrors;
2186cde58dbcSMatthew Ahrens 	spa_t		*zcb_spa;
2187fa9e4066Sahrens } zdb_cb_t;
2188fa9e4066Sahrens 
2189fa9e4066Sahrens static void
2190cde58dbcSMatthew Ahrens zdb_count_block(zdb_cb_t *zcb, zilog_t *zilog, const blkptr_t *bp,
2191b24ab676SJeff Bonwick     dmu_object_type_t type)
2192fa9e4066Sahrens {
2193b24ab676SJeff Bonwick 	uint64_t refcnt = 0;
2194b24ab676SJeff Bonwick 
2195b24ab676SJeff Bonwick 	ASSERT(type < ZDB_OT_TOTAL);
2196b24ab676SJeff Bonwick 
2197b24ab676SJeff Bonwick 	if (zilog && zil_bp_tree_add(zilog, bp) != 0)
2198b24ab676SJeff Bonwick 		return;
2199b24ab676SJeff Bonwick 
2200e14bb325SJeff Bonwick 	for (int i = 0; i < 4; i++) {
2201fa9e4066Sahrens 		int l = (i < 2) ? BP_GET_LEVEL(bp) : ZB_TOTAL;
2202b24ab676SJeff Bonwick 		int t = (i & 1) ? type : ZDB_OT_TOTAL;
2203d5ee8a13SMatthew Ahrens 		int equal;
2204fa9e4066Sahrens 		zdb_blkstats_t *zb = &zcb->zcb_type[l][t];
2205fa9e4066Sahrens 
2206fa9e4066Sahrens 		zb->zb_asize += BP_GET_ASIZE(bp);
2207fa9e4066Sahrens 		zb->zb_lsize += BP_GET_LSIZE(bp);
2208fa9e4066Sahrens 		zb->zb_psize += BP_GET_PSIZE(bp);
2209fa9e4066Sahrens 		zb->zb_count++;
2210490d05b9SMatthew Ahrens 		zb->zb_psize_histogram[BP_GET_PSIZE(bp) >> SPA_MINBLOCKSHIFT]++;
2211d5ee8a13SMatthew Ahrens 
2212d5ee8a13SMatthew Ahrens 		zb->zb_gangs += BP_COUNT_GANG(bp);
2213d5ee8a13SMatthew Ahrens 
2214d5ee8a13SMatthew Ahrens 		switch (BP_GET_NDVAS(bp)) {
2215d5ee8a13SMatthew Ahrens 		case 2:
2216d5ee8a13SMatthew Ahrens 			if (DVA_GET_VDEV(&bp->blk_dva[0]) ==
2217d5ee8a13SMatthew Ahrens 			    DVA_GET_VDEV(&bp->blk_dva[1]))
2218d5ee8a13SMatthew Ahrens 				zb->zb_ditto_samevdev++;
2219d5ee8a13SMatthew Ahrens 			break;
2220d5ee8a13SMatthew Ahrens 		case 3:
2221d5ee8a13SMatthew Ahrens 			equal = (DVA_GET_VDEV(&bp->blk_dva[0]) ==
2222d5ee8a13SMatthew Ahrens 			    DVA_GET_VDEV(&bp->blk_dva[1])) +
2223d5ee8a13SMatthew Ahrens 			    (DVA_GET_VDEV(&bp->blk_dva[0]) ==
2224d5ee8a13SMatthew Ahrens 			    DVA_GET_VDEV(&bp->blk_dva[2])) +
2225d5ee8a13SMatthew Ahrens 			    (DVA_GET_VDEV(&bp->blk_dva[1]) ==
2226d5ee8a13SMatthew Ahrens 			    DVA_GET_VDEV(&bp->blk_dva[2]));
2227d5ee8a13SMatthew Ahrens 			if (equal != 0)
2228d5ee8a13SMatthew Ahrens 				zb->zb_ditto_samevdev++;
2229d5ee8a13SMatthew Ahrens 			break;
2230d5ee8a13SMatthew Ahrens 		}
2231d5ee8a13SMatthew Ahrens 
2232fa9e4066Sahrens 	}
2233fa9e4066Sahrens 
22345d7b4d43SMatthew Ahrens 	if (BP_IS_EMBEDDED(bp)) {
22355d7b4d43SMatthew Ahrens 		zcb->zcb_embedded_blocks[BPE_GET_ETYPE(bp)]++;
22365d7b4d43SMatthew Ahrens 		zcb->zcb_embedded_histogram[BPE_GET_ETYPE(bp)]
22375d7b4d43SMatthew Ahrens 		    [BPE_GET_PSIZE(bp)]++;
22385d7b4d43SMatthew Ahrens 		return;
22395d7b4d43SMatthew Ahrens 	}
22405d7b4d43SMatthew Ahrens 
2241b24ab676SJeff Bonwick 	if (dump_opt['L'])
2242b24ab676SJeff Bonwick 		return;
2243b24ab676SJeff Bonwick 
2244b24ab676SJeff Bonwick 	if (BP_GET_DEDUP(bp)) {
2245b24ab676SJeff Bonwick 		ddt_t *ddt;
2246b24ab676SJeff Bonwick 		ddt_entry_t *dde;
2247b24ab676SJeff Bonwick 
2248cde58dbcSMatthew Ahrens 		ddt = ddt_select(zcb->zcb_spa, bp);
2249b24ab676SJeff Bonwick 		ddt_enter(ddt);
2250b24ab676SJeff Bonwick 		dde = ddt_lookup(ddt, bp, B_FALSE);
2251b24ab676SJeff Bonwick 
2252b24ab676SJeff Bonwick 		if (dde == NULL) {
2253b24ab676SJeff Bonwick 			refcnt = 0;
2254b24ab676SJeff Bonwick 		} else {
2255b24ab676SJeff Bonwick 			ddt_phys_t *ddp = ddt_phys_select(dde, bp);
2256b24ab676SJeff Bonwick 			ddt_phys_decref(ddp);
2257b24ab676SJeff Bonwick 			refcnt = ddp->ddp_refcnt;
2258b24ab676SJeff Bonwick 			if (ddt_phys_total_refcnt(dde) == 0)
2259b24ab676SJeff Bonwick 				ddt_remove(ddt, dde);
2260d41e7643Sek 		}
2261b24ab676SJeff Bonwick 		ddt_exit(ddt);
2262d41e7643Sek 	}
2263d41e7643Sek 
2264cde58dbcSMatthew Ahrens 	VERIFY3U(zio_wait(zio_claim(NULL, zcb->zcb_spa,
2265cde58dbcSMatthew Ahrens 	    refcnt ? 0 : spa_first_txg(zcb->zcb_spa),
2266b24ab676SJeff Bonwick 	    bp, NULL, NULL, ZIO_FLAG_CANFAIL)), ==, 0);
2267fa9e4066Sahrens }
2268fa9e4066Sahrens 
226931d7e8faSGeorge Wilson static void
227031d7e8faSGeorge Wilson zdb_blkptr_done(zio_t *zio)
227131d7e8faSGeorge Wilson {
227231d7e8faSGeorge Wilson 	spa_t *spa = zio->io_spa;
227331d7e8faSGeorge Wilson 	blkptr_t *bp = zio->io_bp;
227431d7e8faSGeorge Wilson 	int ioerr = zio->io_error;
227531d7e8faSGeorge Wilson 	zdb_cb_t *zcb = zio->io_private;
22767802d7bfSMatthew Ahrens 	zbookmark_phys_t *zb = &zio->io_bookmark;
227731d7e8faSGeorge Wilson 
227831d7e8faSGeorge Wilson 	zio_data_buf_free(zio->io_data, zio->io_size);
227931d7e8faSGeorge Wilson 
228031d7e8faSGeorge Wilson 	mutex_enter(&spa->spa_scrub_lock);
228131d7e8faSGeorge Wilson 	spa->spa_scrub_inflight--;
228231d7e8faSGeorge Wilson 	cv_broadcast(&spa->spa_scrub_io_cv);
228331d7e8faSGeorge Wilson 
228431d7e8faSGeorge Wilson 	if (ioerr && !(zio->io_flags & ZIO_FLAG_SPECULATIVE)) {
228531d7e8faSGeorge Wilson 		char blkbuf[BP_SPRINTF_LEN];
228631d7e8faSGeorge Wilson 
228731d7e8faSGeorge Wilson 		zcb->zcb_haderrors = 1;
228831d7e8faSGeorge Wilson 		zcb->zcb_errors[ioerr]++;
228931d7e8faSGeorge Wilson 
229031d7e8faSGeorge Wilson 		if (dump_opt['b'] >= 2)
229143466aaeSMax Grossman 			snprintf_blkptr(blkbuf, sizeof (blkbuf), bp);
229231d7e8faSGeorge Wilson 		else
229331d7e8faSGeorge Wilson 			blkbuf[0] = '\0';
229431d7e8faSGeorge Wilson 
229531d7e8faSGeorge Wilson 		(void) printf("zdb_blkptr_cb: "
229631d7e8faSGeorge Wilson 		    "Got error %d reading "
229731d7e8faSGeorge Wilson 		    "<%llu, %llu, %lld, %llx> %s -- skipping\n",
229831d7e8faSGeorge Wilson 		    ioerr,
229931d7e8faSGeorge Wilson 		    (u_longlong_t)zb->zb_objset,
230031d7e8faSGeorge Wilson 		    (u_longlong_t)zb->zb_object,
230131d7e8faSGeorge Wilson 		    (u_longlong_t)zb->zb_level,
230231d7e8faSGeorge Wilson 		    (u_longlong_t)zb->zb_blkid,
230331d7e8faSGeorge Wilson 		    blkbuf);
230431d7e8faSGeorge Wilson 	}
230531d7e8faSGeorge Wilson 	mutex_exit(&spa->spa_scrub_lock);
230631d7e8faSGeorge Wilson }
230731d7e8faSGeorge Wilson 
2308fa9e4066Sahrens static int
23091b912ec7SGeorge Wilson zdb_blkptr_cb(spa_t *spa, zilog_t *zilog, const blkptr_t *bp,
23107802d7bfSMatthew Ahrens     const zbookmark_phys_t *zb, const dnode_phys_t *dnp, void *arg)
2311fa9e4066Sahrens {
2312fa9e4066Sahrens 	zdb_cb_t *zcb = arg;
23136365109dSVictor Latushkin 	dmu_object_type_t type;
2314468c413aSTim Haley 	boolean_t is_metadata;
2315fa9e4066Sahrens 
231643466aaeSMax Grossman 	if (dump_opt['b'] >= 5 && bp->blk_birth > 0) {
231743466aaeSMax Grossman 		char blkbuf[BP_SPRINTF_LEN];
231843466aaeSMax Grossman 		snprintf_blkptr(blkbuf, sizeof (blkbuf), bp);
231943466aaeSMax Grossman 		(void) printf("objset %llu object %llu "
232043466aaeSMax Grossman 		    "level %lld offset 0x%llx %s\n",
232143466aaeSMax Grossman 		    (u_longlong_t)zb->zb_objset,
232243466aaeSMax Grossman 		    (u_longlong_t)zb->zb_object,
232343466aaeSMax Grossman 		    (longlong_t)zb->zb_level,
232443466aaeSMax Grossman 		    (u_longlong_t)blkid2offset(dnp, bp, zb),
232543466aaeSMax Grossman 		    blkbuf);
232643466aaeSMax Grossman 	}
232743466aaeSMax Grossman 
232843466aaeSMax Grossman 	if (BP_IS_HOLE(bp))
232988b7b0f2SMatthew Ahrens 		return (0);
2330e14bb325SJeff Bonwick 
23316365109dSVictor Latushkin 	type = BP_GET_TYPE(bp);
2332e14bb325SJeff Bonwick 
2333ad135b5dSChristopher Siden 	zdb_count_block(zcb, zilog, bp,
2334ad135b5dSChristopher Siden 	    (type & DMU_OT_NEWTYPE) ? ZDB_OT_OTHER : type);
23356365109dSVictor Latushkin 
2336ad135b5dSChristopher Siden 	is_metadata = (BP_GET_LEVEL(bp) != 0 || DMU_OT_IS_METADATA(type));
23376365109dSVictor Latushkin 
23385d7b4d43SMatthew Ahrens 	if (!BP_IS_EMBEDDED(bp) &&
23395d7b4d43SMatthew Ahrens 	    (dump_opt['c'] > 1 || (dump_opt['c'] && is_metadata))) {
2340468c413aSTim Haley 		size_t size = BP_GET_PSIZE(bp);
234131d7e8faSGeorge Wilson 		void *data = zio_data_buf_alloc(size);
2342b24ab676SJeff Bonwick 		int flags = ZIO_FLAG_CANFAIL | ZIO_FLAG_SCRUB | ZIO_FLAG_RAW;
2343b24ab676SJeff Bonwick 
2344b24ab676SJeff Bonwick 		/* If it's an intent log block, failure is expected. */
2345b24ab676SJeff Bonwick 		if (zb->zb_level == ZB_ZIL_LEVEL)
2346b24ab676SJeff Bonwick 			flags |= ZIO_FLAG_SPECULATIVE;
2347b24ab676SJeff Bonwick 
234831d7e8faSGeorge Wilson 		mutex_enter(&spa->spa_scrub_lock);
234931d7e8faSGeorge Wilson 		while (spa->spa_scrub_inflight > max_inflight)
235031d7e8faSGeorge Wilson 			cv_wait(&spa->spa_scrub_io_cv, &spa->spa_scrub_lock);
235131d7e8faSGeorge Wilson 		spa->spa_scrub_inflight++;
235231d7e8faSGeorge Wilson 		mutex_exit(&spa->spa_scrub_lock);
2353b24ab676SJeff Bonwick 
235431d7e8faSGeorge Wilson 		zio_nowait(zio_read(NULL, spa, bp, data, size,
235531d7e8faSGeorge Wilson 		    zdb_blkptr_done, zcb, ZIO_PRIORITY_ASYNC_READ, flags, zb));
2356fa9e4066Sahrens 	}
2357fa9e4066Sahrens 
2358fa9e4066Sahrens 	zcb->zcb_readfails = 0;
2359fa9e4066Sahrens 
2360*f7950bf1SMatthew Ahrens 	/* only call gethrtime() every 100 blocks */
2361*f7950bf1SMatthew Ahrens 	static int iters;
2362*f7950bf1SMatthew Ahrens 	if (++iters > 100)
2363*f7950bf1SMatthew Ahrens 		iters = 0;
2364*f7950bf1SMatthew Ahrens 	else
2365*f7950bf1SMatthew Ahrens 		return (0);
2366*f7950bf1SMatthew Ahrens 
2367*f7950bf1SMatthew Ahrens 	if (dump_opt['b'] < 5 && gethrtime() > zcb->zcb_lastprint + NANOSEC) {
2368490d05b9SMatthew Ahrens 		uint64_t now = gethrtime();
2369490d05b9SMatthew Ahrens 		char buf[10];
2370490d05b9SMatthew Ahrens 		uint64_t bytes = zcb->zcb_type[ZB_TOTAL][ZDB_OT_TOTAL].zb_asize;
2371490d05b9SMatthew Ahrens 		int kb_per_sec =
2372490d05b9SMatthew Ahrens 		    1 + bytes / (1 + ((now - zcb->zcb_start) / 1000 / 1000));
2373490d05b9SMatthew Ahrens 		int sec_remaining =
2374490d05b9SMatthew Ahrens 		    (zcb->zcb_totalasize - bytes) / 1024 / kb_per_sec;
2375490d05b9SMatthew Ahrens 
2376490d05b9SMatthew Ahrens 		zfs_nicenum(bytes, buf, sizeof (buf));
2377490d05b9SMatthew Ahrens 		(void) fprintf(stderr,
2378490d05b9SMatthew Ahrens 		    "\r%5s completed (%4dMB/s) "
2379490d05b9SMatthew Ahrens 		    "estimated time remaining: %uhr %02umin %02usec        ",
2380490d05b9SMatthew Ahrens 		    buf, kb_per_sec / 1024,
2381490d05b9SMatthew Ahrens 		    sec_remaining / 60 / 60,
2382490d05b9SMatthew Ahrens 		    sec_remaining / 60 % 60,
2383490d05b9SMatthew Ahrens 		    sec_remaining % 60);
2384490d05b9SMatthew Ahrens 
2385490d05b9SMatthew Ahrens 		zcb->zcb_lastprint = now;
2386490d05b9SMatthew Ahrens 	}
2387490d05b9SMatthew Ahrens 
2388fa9e4066Sahrens 	return (0);
2389fa9e4066Sahrens }
2390fa9e4066Sahrens 
2391b24ab676SJeff Bonwick static void
23920713e232SGeorge Wilson zdb_leak(void *arg, uint64_t start, uint64_t size)
2393b24ab676SJeff Bonwick {
23940713e232SGeorge Wilson 	vdev_t *vd = arg;
2395b24ab676SJeff Bonwick 
2396b24ab676SJeff Bonwick 	(void) printf("leaked space: vdev %llu, offset 0x%llx, size %llu\n",
2397b24ab676SJeff Bonwick 	    (u_longlong_t)vd->vdev_id, (u_longlong_t)start, (u_longlong_t)size);
2398b24ab676SJeff Bonwick }
2399b24ab676SJeff Bonwick 
24000713e232SGeorge Wilson static metaslab_ops_t zdb_metaslab_ops = {
24012e4c9986SGeorge Wilson 	NULL	/* alloc */
2402b24ab676SJeff Bonwick };
2403b24ab676SJeff Bonwick 
2404b24ab676SJeff Bonwick static void
2405bbfd46c4SJeff Bonwick zdb_ddt_leak_init(spa_t *spa, zdb_cb_t *zcb)
2406b24ab676SJeff Bonwick {
2407bbfd46c4SJeff Bonwick 	ddt_bookmark_t ddb = { 0 };
2408b24ab676SJeff Bonwick 	ddt_entry_t dde;
2409b24ab676SJeff Bonwick 	int error;
2410b24ab676SJeff Bonwick 
2411bbfd46c4SJeff Bonwick 	while ((error = ddt_walk(spa, &ddb, &dde)) == 0) {
2412b24ab676SJeff Bonwick 		blkptr_t blk;
2413b24ab676SJeff Bonwick 		ddt_phys_t *ddp = dde.dde_phys;
2414bbfd46c4SJeff Bonwick 
2415bbfd46c4SJeff Bonwick 		if (ddb.ddb_class == DDT_CLASS_UNIQUE)
2416bbfd46c4SJeff Bonwick 			return;
2417bbfd46c4SJeff Bonwick 
2418b24ab676SJeff Bonwick 		ASSERT(ddt_phys_total_refcnt(&dde) > 1);
2419bbfd46c4SJeff Bonwick 
2420b24ab676SJeff Bonwick 		for (int p = 0; p < DDT_PHYS_TYPES; p++, ddp++) {
2421b24ab676SJeff Bonwick 			if (ddp->ddp_phys_birth == 0)
2422b24ab676SJeff Bonwick 				continue;
2423bbfd46c4SJeff Bonwick 			ddt_bp_create(ddb.ddb_checksum,
2424bbfd46c4SJeff Bonwick 			    &dde.dde_key, ddp, &blk);
2425b24ab676SJeff Bonwick 			if (p == DDT_PHYS_DITTO) {
2426cde58dbcSMatthew Ahrens 				zdb_count_block(zcb, NULL, &blk, ZDB_OT_DITTO);
2427b24ab676SJeff Bonwick 			} else {
2428b24ab676SJeff Bonwick 				zcb->zcb_dedup_asize +=
2429b24ab676SJeff Bonwick 				    BP_GET_ASIZE(&blk) * (ddp->ddp_refcnt - 1);
2430b24ab676SJeff Bonwick 				zcb->zcb_dedup_blocks++;
2431b24ab676SJeff Bonwick 			}
2432b24ab676SJeff Bonwick 		}
2433b24ab676SJeff Bonwick 		if (!dump_opt['L']) {
2434bbfd46c4SJeff Bonwick 			ddt_t *ddt = spa->spa_ddt[ddb.ddb_checksum];
2435b24ab676SJeff Bonwick 			ddt_enter(ddt);
2436b24ab676SJeff Bonwick 			VERIFY(ddt_lookup(ddt, &blk, B_TRUE) != NULL);
2437b24ab676SJeff Bonwick 			ddt_exit(ddt);
2438b24ab676SJeff Bonwick 		}
2439b24ab676SJeff Bonwick 	}
2440b24ab676SJeff Bonwick 
2441b24ab676SJeff Bonwick 	ASSERT(error == ENOENT);
2442b24ab676SJeff Bonwick }
2443b24ab676SJeff Bonwick 
2444b24ab676SJeff Bonwick static void
2445b24ab676SJeff Bonwick zdb_leak_init(spa_t *spa, zdb_cb_t *zcb)
2446b24ab676SJeff Bonwick {
2447cde58dbcSMatthew Ahrens 	zcb->zcb_spa = spa;
2448cde58dbcSMatthew Ahrens 
2449b24ab676SJeff Bonwick 	if (!dump_opt['L']) {
2450b24ab676SJeff Bonwick 		vdev_t *rvd = spa->spa_root_vdev;
245106be9802SMatthew Ahrens 		for (uint64_t c = 0; c < rvd->vdev_children; c++) {
2452b24ab676SJeff Bonwick 			vdev_t *vd = rvd->vdev_child[c];
245306be9802SMatthew Ahrens 			for (uint64_t m = 0; m < vd->vdev_ms_count; m++) {
2454b24ab676SJeff Bonwick 				metaslab_t *msp = vd->vdev_ms[m];
2455b24ab676SJeff Bonwick 				mutex_enter(&msp->ms_lock);
24560713e232SGeorge Wilson 				metaslab_unload(msp);
24570713e232SGeorge Wilson 
24580713e232SGeorge Wilson 				/*
24590713e232SGeorge Wilson 				 * For leak detection, we overload the metaslab
24600713e232SGeorge Wilson 				 * ms_tree to contain allocated segments
24610713e232SGeorge Wilson 				 * instead of free segments. As a result,
24620713e232SGeorge Wilson 				 * we can't use the normal metaslab_load/unload
24630713e232SGeorge Wilson 				 * interfaces.
24640713e232SGeorge Wilson 				 */
24650713e232SGeorge Wilson 				if (msp->ms_sm != NULL) {
246606be9802SMatthew Ahrens 					(void) fprintf(stderr,
246706be9802SMatthew Ahrens 					    "\rloading space map for "
246806be9802SMatthew Ahrens 					    "vdev %llu of %llu, "
246906be9802SMatthew Ahrens 					    "metaslab %llu of %llu ...",
247006be9802SMatthew Ahrens 					    (longlong_t)c,
247106be9802SMatthew Ahrens 					    (longlong_t)rvd->vdev_children,
247206be9802SMatthew Ahrens 					    (longlong_t)m,
247306be9802SMatthew Ahrens 					    (longlong_t)vd->vdev_ms_count);
247406be9802SMatthew Ahrens 
24750713e232SGeorge Wilson 					msp->ms_ops = &zdb_metaslab_ops;
2476*f7950bf1SMatthew Ahrens 
2477*f7950bf1SMatthew Ahrens 					/*
2478*f7950bf1SMatthew Ahrens 					 * We don't want to spend the CPU
2479*f7950bf1SMatthew Ahrens 					 * manipulating the size-ordered
2480*f7950bf1SMatthew Ahrens 					 * tree, so clear the range_tree
2481*f7950bf1SMatthew Ahrens 					 * ops.
2482*f7950bf1SMatthew Ahrens 					 */
2483*f7950bf1SMatthew Ahrens 					msp->ms_tree->rt_ops = NULL;
24840713e232SGeorge Wilson 					VERIFY0(space_map_load(msp->ms_sm,
24850713e232SGeorge Wilson 					    msp->ms_tree, SM_ALLOC));
24860713e232SGeorge Wilson 					msp->ms_loaded = B_TRUE;
24870713e232SGeorge Wilson 				}
2488b24ab676SJeff Bonwick 				mutex_exit(&msp->ms_lock);
2489b24ab676SJeff Bonwick 			}
2490b24ab676SJeff Bonwick 		}
249106be9802SMatthew Ahrens 		(void) fprintf(stderr, "\n");
2492b24ab676SJeff Bonwick 	}
2493b24ab676SJeff Bonwick 
2494b24ab676SJeff Bonwick 	spa_config_enter(spa, SCL_CONFIG, FTAG, RW_READER);
2495b24ab676SJeff Bonwick 
2496bbfd46c4SJeff Bonwick 	zdb_ddt_leak_init(spa, zcb);
2497b24ab676SJeff Bonwick 
2498b24ab676SJeff Bonwick 	spa_config_exit(spa, SCL_CONFIG, FTAG);
2499b24ab676SJeff Bonwick }
2500b24ab676SJeff Bonwick 
2501b24ab676SJeff Bonwick static void
2502b24ab676SJeff Bonwick zdb_leak_fini(spa_t *spa)
2503b24ab676SJeff Bonwick {
2504b24ab676SJeff Bonwick 	if (!dump_opt['L']) {
2505b24ab676SJeff Bonwick 		vdev_t *rvd = spa->spa_root_vdev;
2506b24ab676SJeff Bonwick 		for (int c = 0; c < rvd->vdev_children; c++) {
2507b24ab676SJeff Bonwick 			vdev_t *vd = rvd->vdev_child[c];
2508b24ab676SJeff Bonwick 			for (int m = 0; m < vd->vdev_ms_count; m++) {
2509b24ab676SJeff Bonwick 				metaslab_t *msp = vd->vdev_ms[m];
2510b24ab676SJeff Bonwick 				mutex_enter(&msp->ms_lock);
25110713e232SGeorge Wilson 
25120713e232SGeorge Wilson 				/*
25130713e232SGeorge Wilson 				 * The ms_tree has been overloaded to
25140713e232SGeorge Wilson 				 * contain allocated segments. Now that we
25150713e232SGeorge Wilson 				 * finished traversing all blocks, any
25160713e232SGeorge Wilson 				 * block that remains in the ms_tree
25170713e232SGeorge Wilson 				 * represents an allocated block that we
25180713e232SGeorge Wilson 				 * did not claim during the traversal.
25190713e232SGeorge Wilson 				 * Claimed blocks would have been removed
25200713e232SGeorge Wilson 				 * from the ms_tree.
25210713e232SGeorge Wilson 				 */
25220713e232SGeorge Wilson 				range_tree_vacate(msp->ms_tree, zdb_leak, vd);
25230713e232SGeorge Wilson 				msp->ms_loaded = B_FALSE;
25240713e232SGeorge Wilson 
2525b24ab676SJeff Bonwick 				mutex_exit(&msp->ms_lock);
2526b24ab676SJeff Bonwick 			}
2527b24ab676SJeff Bonwick 		}
2528b24ab676SJeff Bonwick 	}
2529b24ab676SJeff Bonwick }
2530b24ab676SJeff Bonwick 
2531cde58dbcSMatthew Ahrens /* ARGSUSED */
2532cde58dbcSMatthew Ahrens static int
2533cde58dbcSMatthew Ahrens count_block_cb(void *arg, const blkptr_t *bp, dmu_tx_t *tx)
2534cde58dbcSMatthew Ahrens {
2535cde58dbcSMatthew Ahrens 	zdb_cb_t *zcb = arg;
2536cde58dbcSMatthew Ahrens 
2537490d05b9SMatthew Ahrens 	if (dump_opt['b'] >= 5) {
2538cde58dbcSMatthew Ahrens 		char blkbuf[BP_SPRINTF_LEN];
253943466aaeSMax Grossman 		snprintf_blkptr(blkbuf, sizeof (blkbuf), bp);
2540cde58dbcSMatthew Ahrens 		(void) printf("[%s] %s\n",
2541cde58dbcSMatthew Ahrens 		    "deferred free", blkbuf);
2542cde58dbcSMatthew Ahrens 	}
2543cde58dbcSMatthew Ahrens 	zdb_count_block(zcb, NULL, bp, ZDB_OT_DEFERRED);
2544cde58dbcSMatthew Ahrens 	return (0);
2545cde58dbcSMatthew Ahrens }
2546cde58dbcSMatthew Ahrens 
2547fa9e4066Sahrens static int
2548fa9e4066Sahrens dump_block_stats(spa_t *spa)
2549fa9e4066Sahrens {
2550fa9e4066Sahrens 	zdb_cb_t zcb = { 0 };
2551fa9e4066Sahrens 	zdb_blkstats_t *zb, *tzb;
2552b24ab676SJeff Bonwick 	uint64_t norm_alloc, norm_space, total_alloc, total_found;
2553cd088ea4SVictor Latushkin 	int flags = TRAVERSE_PRE | TRAVERSE_PREFETCH_METADATA | TRAVERSE_HARD;
25545d7b4d43SMatthew Ahrens 	boolean_t leaks = B_FALSE;
2555fa9e4066Sahrens 
2556490d05b9SMatthew Ahrens 	(void) printf("\nTraversing all blocks %s%s%s%s%s...\n\n",
2557b24ab676SJeff Bonwick 	    (dump_opt['c'] || !dump_opt['L']) ? "to verify " : "",
2558b24ab676SJeff Bonwick 	    (dump_opt['c'] == 1) ? "metadata " : "",
2559b24ab676SJeff Bonwick 	    dump_opt['c'] ? "checksums " : "",
2560b24ab676SJeff Bonwick 	    (dump_opt['c'] && !dump_opt['L']) ? "and verify " : "",
2561b24ab676SJeff Bonwick 	    !dump_opt['L'] ? "nothing leaked " : "");
2562fa9e4066Sahrens 
2563fa9e4066Sahrens 	/*
2564e14bb325SJeff Bonwick 	 * Load all space maps as SM_ALLOC maps, then traverse the pool
2565e14bb325SJeff Bonwick 	 * claiming each block we discover.  If the pool is perfectly
2566e14bb325SJeff Bonwick 	 * consistent, the space maps will be empty when we're done.
2567e14bb325SJeff Bonwick 	 * Anything left over is a leak; any block we can't claim (because
2568e14bb325SJeff Bonwick 	 * it's not part of any space map) is a double allocation,
2569e14bb325SJeff Bonwick 	 * reference to a freed block, or an unclaimed log block.
2570fa9e4066Sahrens 	 */
2571b24ab676SJeff Bonwick 	zdb_leak_init(spa, &zcb);
2572fa9e4066Sahrens 
2573fa9e4066Sahrens 	/*
2574fa9e4066Sahrens 	 * If there's a deferred-free bplist, process that first.
2575fa9e4066Sahrens 	 */
2576cde58dbcSMatthew Ahrens 	(void) bpobj_iterate_nofree(&spa->spa_deferred_bpobj,
2577cde58dbcSMatthew Ahrens 	    count_block_cb, &zcb, NULL);
25783b2aab18SMatthew Ahrens 	if (spa_version(spa) >= SPA_VERSION_DEADLISTS) {
25793b2aab18SMatthew Ahrens 		(void) bpobj_iterate_nofree(&spa->spa_dsl_pool->dp_free_bpobj,
25803b2aab18SMatthew Ahrens 		    count_block_cb, &zcb, NULL);
25813b2aab18SMatthew Ahrens 	}
25822acef22dSMatthew Ahrens 	if (spa_feature_is_active(spa, SPA_FEATURE_ASYNC_DESTROY)) {
2583b420f3adSRichard Lowe 		VERIFY3U(0, ==, bptree_iterate(spa->spa_meta_objset,
2584ad135b5dSChristopher Siden 		    spa->spa_dsl_pool->dp_bptree_obj, B_FALSE, count_block_cb,
2585ad135b5dSChristopher Siden 		    &zcb, NULL));
2586ad135b5dSChristopher Siden 	}
2587fa9e4066Sahrens 
2588bbfd46c4SJeff Bonwick 	if (dump_opt['c'] > 1)
2589bbfd46c4SJeff Bonwick 		flags |= TRAVERSE_PREFETCH_DATA;
2590bbfd46c4SJeff Bonwick 
2591490d05b9SMatthew Ahrens 	zcb.zcb_totalasize = metaslab_class_get_alloc(spa_normal_class(spa));
2592490d05b9SMatthew Ahrens 	zcb.zcb_start = zcb.zcb_lastprint = gethrtime();
2593bbfd46c4SJeff Bonwick 	zcb.zcb_haderrors |= traverse_pool(spa, 0, flags, zdb_blkptr_cb, &zcb);
2594fa9e4066Sahrens 
259531d7e8faSGeorge Wilson 	/*
259631d7e8faSGeorge Wilson 	 * If we've traversed the data blocks then we need to wait for those
259731d7e8faSGeorge Wilson 	 * I/Os to complete. We leverage "The Godfather" zio to wait on
259831d7e8faSGeorge Wilson 	 * all async I/Os to complete.
259931d7e8faSGeorge Wilson 	 */
260031d7e8faSGeorge Wilson 	if (dump_opt['c']) {
26016f834bc1SMatthew Ahrens 		for (int i = 0; i < max_ncpus; i++) {
26026f834bc1SMatthew Ahrens 			(void) zio_wait(spa->spa_async_zio_root[i]);
26036f834bc1SMatthew Ahrens 			spa->spa_async_zio_root[i] = zio_root(spa, NULL, NULL,
26046f834bc1SMatthew Ahrens 			    ZIO_FLAG_CANFAIL | ZIO_FLAG_SPECULATIVE |
26056f834bc1SMatthew Ahrens 			    ZIO_FLAG_GODFATHER);
26066f834bc1SMatthew Ahrens 		}
260731d7e8faSGeorge Wilson 	}
260831d7e8faSGeorge Wilson 
2609b24ab676SJeff Bonwick 	if (zcb.zcb_haderrors) {
2610fa9e4066Sahrens 		(void) printf("\nError counts:\n\n");
2611fa9e4066Sahrens 		(void) printf("\t%5s  %s\n", "errno", "count");
2612b24ab676SJeff Bonwick 		for (int e = 0; e < 256; e++) {
2613fa9e4066Sahrens 			if (zcb.zcb_errors[e] != 0) {
2614fa9e4066Sahrens 				(void) printf("\t%5d  %llu\n",
2615fa9e4066Sahrens 				    e, (u_longlong_t)zcb.zcb_errors[e]);
2616fa9e4066Sahrens 			}
2617fa9e4066Sahrens 		}
2618fa9e4066Sahrens 	}
2619fa9e4066Sahrens 
2620fa9e4066Sahrens 	/*
2621fa9e4066Sahrens 	 * Report any leaked segments.
2622fa9e4066Sahrens 	 */
2623b24ab676SJeff Bonwick 	zdb_leak_fini(spa);
2624fa9e4066Sahrens 
2625b24ab676SJeff Bonwick 	tzb = &zcb.zcb_type[ZB_TOTAL][ZDB_OT_TOTAL];
26268654d025Sperrin 
2627b24ab676SJeff Bonwick 	norm_alloc = metaslab_class_get_alloc(spa_normal_class(spa));
2628b24ab676SJeff Bonwick 	norm_space = metaslab_class_get_space(spa_normal_class(spa));
26298654d025Sperrin 
2630b24ab676SJeff Bonwick 	total_alloc = norm_alloc + metaslab_class_get_alloc(spa_log_class(spa));
2631b24ab676SJeff Bonwick 	total_found = tzb->zb_asize - zcb.zcb_dedup_asize;
2632fa9e4066Sahrens 
2633b24ab676SJeff Bonwick 	if (total_found == total_alloc) {
263482a0a985SVictor Latushkin 		if (!dump_opt['L'])
263582a0a985SVictor Latushkin 			(void) printf("\n\tNo leaks (block sum matches space"
263682a0a985SVictor Latushkin 			    " maps exactly)\n");
2637fa9e4066Sahrens 	} else {
2638fa9e4066Sahrens 		(void) printf("block traversal size %llu != alloc %llu "
263982a0a985SVictor Latushkin 		    "(%s %lld)\n",
2640b24ab676SJeff Bonwick 		    (u_longlong_t)total_found,
2641b24ab676SJeff Bonwick 		    (u_longlong_t)total_alloc,
264282a0a985SVictor Latushkin 		    (dump_opt['L']) ? "unreachable" : "leaked",
2643b24ab676SJeff Bonwick 		    (longlong_t)(total_alloc - total_found));
26445d7b4d43SMatthew Ahrens 		leaks = B_TRUE;
2645fa9e4066Sahrens 	}
2646fa9e4066Sahrens 
2647fa9e4066Sahrens 	if (tzb->zb_count == 0)
2648fa9e4066Sahrens 		return (2);
2649fa9e4066Sahrens 
2650fa9e4066Sahrens 	(void) printf("\n");
2651fa9e4066Sahrens 	(void) printf("\tbp count:      %10llu\n",
2652fa9e4066Sahrens 	    (u_longlong_t)tzb->zb_count);
2653d5ee8a13SMatthew Ahrens 	(void) printf("\tganged count:  %10llu\n",
2654d5ee8a13SMatthew Ahrens 	    (longlong_t)tzb->zb_gangs);
2655b24ab676SJeff Bonwick 	(void) printf("\tbp logical:    %10llu      avg: %6llu\n",
2656fa9e4066Sahrens 	    (u_longlong_t)tzb->zb_lsize,
2657fa9e4066Sahrens 	    (u_longlong_t)(tzb->zb_lsize / tzb->zb_count));
2658b24ab676SJeff Bonwick 	(void) printf("\tbp physical:   %10llu      avg:"
2659b24ab676SJeff Bonwick 	    " %6llu     compression: %6.2f\n",
2660fa9e4066Sahrens 	    (u_longlong_t)tzb->zb_psize,
2661fa9e4066Sahrens 	    (u_longlong_t)(tzb->zb_psize / tzb->zb_count),
2662fa9e4066Sahrens 	    (double)tzb->zb_lsize / tzb->zb_psize);
2663b24ab676SJeff Bonwick 	(void) printf("\tbp allocated:  %10llu      avg:"
2664b24ab676SJeff Bonwick 	    " %6llu     compression: %6.2f\n",
2665fa9e4066Sahrens 	    (u_longlong_t)tzb->zb_asize,
2666fa9e4066Sahrens 	    (u_longlong_t)(tzb->zb_asize / tzb->zb_count),
2667fa9e4066Sahrens 	    (double)tzb->zb_lsize / tzb->zb_asize);
2668b24ab676SJeff Bonwick 	(void) printf("\tbp deduped:    %10llu    ref>1:"
2669b24ab676SJeff Bonwick 	    " %6llu   deduplication: %6.2f\n",
2670b24ab676SJeff Bonwick 	    (u_longlong_t)zcb.zcb_dedup_asize,
2671b24ab676SJeff Bonwick 	    (u_longlong_t)zcb.zcb_dedup_blocks,
2672b24ab676SJeff Bonwick 	    (double)zcb.zcb_dedup_asize / tzb->zb_asize + 1.0);
2673b24ab676SJeff Bonwick 	(void) printf("\tSPA allocated: %10llu     used: %5.2f%%\n",
2674b24ab676SJeff Bonwick 	    (u_longlong_t)norm_alloc, 100.0 * norm_alloc / norm_space);
2675fa9e4066Sahrens 
26765d7b4d43SMatthew Ahrens 	for (bp_embedded_type_t i = 0; i < NUM_BP_EMBEDDED_TYPES; i++) {
26775d7b4d43SMatthew Ahrens 		if (zcb.zcb_embedded_blocks[i] == 0)
26785d7b4d43SMatthew Ahrens 			continue;
26795d7b4d43SMatthew Ahrens 		(void) printf("\n");
26805d7b4d43SMatthew Ahrens 		(void) printf("\tadditional, non-pointer bps of type %u: "
26815d7b4d43SMatthew Ahrens 		    "%10llu\n",
26825d7b4d43SMatthew Ahrens 		    i, (u_longlong_t)zcb.zcb_embedded_blocks[i]);
26835d7b4d43SMatthew Ahrens 
26845d7b4d43SMatthew Ahrens 		if (dump_opt['b'] >= 3) {
26855d7b4d43SMatthew Ahrens 			(void) printf("\t number of (compressed) bytes:  "
26865d7b4d43SMatthew Ahrens 			    "number of bps\n");
26875d7b4d43SMatthew Ahrens 			dump_histogram(zcb.zcb_embedded_histogram[i],
26885d7b4d43SMatthew Ahrens 			    sizeof (zcb.zcb_embedded_histogram[i]) /
26895d7b4d43SMatthew Ahrens 			    sizeof (zcb.zcb_embedded_histogram[i][0]), 0);
26905d7b4d43SMatthew Ahrens 		}
26915d7b4d43SMatthew Ahrens 	}
26925d7b4d43SMatthew Ahrens 
2693d5ee8a13SMatthew Ahrens 	if (tzb->zb_ditto_samevdev != 0) {
2694d5ee8a13SMatthew Ahrens 		(void) printf("\tDittoed blocks on same vdev: %llu\n",
2695d5ee8a13SMatthew Ahrens 		    (longlong_t)tzb->zb_ditto_samevdev);
2696d5ee8a13SMatthew Ahrens 	}
2697d5ee8a13SMatthew Ahrens 
2698fa9e4066Sahrens 	if (dump_opt['b'] >= 2) {
2699fa9e4066Sahrens 		int l, t, level;
2700fa9e4066Sahrens 		(void) printf("\nBlocks\tLSIZE\tPSIZE\tASIZE"
2701fa9e4066Sahrens 		    "\t  avg\t comp\t%%Total\tType\n");
2702fa9e4066Sahrens 
2703b24ab676SJeff Bonwick 		for (t = 0; t <= ZDB_OT_TOTAL; t++) {
27043f9d6ad7SLin Ling 			char csize[32], lsize[32], psize[32], asize[32];
2705d5ee8a13SMatthew Ahrens 			char avg[32], gang[32];
2706fa9e4066Sahrens 			char *typename;
2707fa9e4066Sahrens 
2708b24ab676SJeff Bonwick 			if (t < DMU_OT_NUMTYPES)
2709b24ab676SJeff Bonwick 				typename = dmu_ot[t].ot_name;
2710b24ab676SJeff Bonwick 			else
2711b24ab676SJeff Bonwick 				typename = zdb_ot_extname[t - DMU_OT_NUMTYPES];
2712fa9e4066Sahrens 
2713fa9e4066Sahrens 			if (zcb.zcb_type[ZB_TOTAL][t].zb_asize == 0) {
2714fa9e4066Sahrens 				(void) printf("%6s\t%5s\t%5s\t%5s"
2715fa9e4066Sahrens 				    "\t%5s\t%5s\t%6s\t%s\n",
2716fa9e4066Sahrens 				    "-",
2717fa9e4066Sahrens 				    "-",
2718fa9e4066Sahrens 				    "-",
2719fa9e4066Sahrens 				    "-",
2720fa9e4066Sahrens 				    "-",
2721fa9e4066Sahrens 				    "-",
2722fa9e4066Sahrens 				    "-",
2723fa9e4066Sahrens 				    typename);
2724fa9e4066Sahrens 				continue;
2725fa9e4066Sahrens 			}
2726fa9e4066Sahrens 
2727fa9e4066Sahrens 			for (l = ZB_TOTAL - 1; l >= -1; l--) {
2728fa9e4066Sahrens 				level = (l == -1 ? ZB_TOTAL : l);
2729fa9e4066Sahrens 				zb = &zcb.zcb_type[level][t];
2730fa9e4066Sahrens 
2731fa9e4066Sahrens 				if (zb->zb_asize == 0)
2732fa9e4066Sahrens 					continue;
2733fa9e4066Sahrens 
2734fa9e4066Sahrens 				if (dump_opt['b'] < 3 && level != ZB_TOTAL)
2735fa9e4066Sahrens 					continue;
2736fa9e4066Sahrens 
2737fa9e4066Sahrens 				if (level == 0 && zb->zb_asize ==
2738fa9e4066Sahrens 				    zcb.zcb_type[ZB_TOTAL][t].zb_asize)
2739fa9e4066Sahrens 					continue;
2740fa9e4066Sahrens 
27413f9d6ad7SLin Ling 				zdb_nicenum(zb->zb_count, csize);
27423f9d6ad7SLin Ling 				zdb_nicenum(zb->zb_lsize, lsize);
27433f9d6ad7SLin Ling 				zdb_nicenum(zb->zb_psize, psize);
27443f9d6ad7SLin Ling 				zdb_nicenum(zb->zb_asize, asize);
27453f9d6ad7SLin Ling 				zdb_nicenum(zb->zb_asize / zb->zb_count, avg);
2746d5ee8a13SMatthew Ahrens 				zdb_nicenum(zb->zb_gangs, gang);
2747fa9e4066Sahrens 
2748fa9e4066Sahrens 				(void) printf("%6s\t%5s\t%5s\t%5s\t%5s"
2749fa9e4066Sahrens 				    "\t%5.2f\t%6.2f\t",
2750fa9e4066Sahrens 				    csize, lsize, psize, asize, avg,
2751fa9e4066Sahrens 				    (double)zb->zb_lsize / zb->zb_psize,
2752fa9e4066Sahrens 				    100.0 * zb->zb_asize / tzb->zb_asize);
2753fa9e4066Sahrens 
2754fa9e4066Sahrens 				if (level == ZB_TOTAL)
2755fa9e4066Sahrens 					(void) printf("%s\n", typename);
2756fa9e4066Sahrens 				else
2757fa9e4066Sahrens 					(void) printf("    L%d %s\n",
2758fa9e4066Sahrens 					    level, typename);
2759490d05b9SMatthew Ahrens 
2760d5ee8a13SMatthew Ahrens 				if (dump_opt['b'] >= 3 && zb->zb_gangs > 0) {
2761d5ee8a13SMatthew Ahrens 					(void) printf("\t number of ganged "
2762d5ee8a13SMatthew Ahrens 					    "blocks: %s\n", gang);
2763d5ee8a13SMatthew Ahrens 				}
2764d5ee8a13SMatthew Ahrens 
2765490d05b9SMatthew Ahrens 				if (dump_opt['b'] >= 4) {
2766490d05b9SMatthew Ahrens 					(void) printf("psize "
2767490d05b9SMatthew Ahrens 					    "(in 512-byte sectors): "
2768490d05b9SMatthew Ahrens 					    "number of blocks\n");
2769490d05b9SMatthew Ahrens 					dump_histogram(zb->zb_psize_histogram,
27700713e232SGeorge Wilson 					    PSIZE_HISTO_SIZE, 0);
2771490d05b9SMatthew Ahrens 				}
2772fa9e4066Sahrens 			}
2773fa9e4066Sahrens 		}
2774fa9e4066Sahrens 	}
2775fa9e4066Sahrens 
2776fa9e4066Sahrens 	(void) printf("\n");
2777fa9e4066Sahrens 
2778fa9e4066Sahrens 	if (leaks)
2779fa9e4066Sahrens 		return (2);
2780fa9e4066Sahrens 
2781fa9e4066Sahrens 	if (zcb.zcb_haderrors)
2782fa9e4066Sahrens 		return (3);
2783fa9e4066Sahrens 
2784fa9e4066Sahrens 	return (0);
2785fa9e4066Sahrens }
2786fa9e4066Sahrens 
2787b24ab676SJeff Bonwick typedef struct zdb_ddt_entry {
2788b24ab676SJeff Bonwick 	ddt_key_t	zdde_key;
2789b24ab676SJeff Bonwick 	uint64_t	zdde_ref_blocks;
2790b24ab676SJeff Bonwick 	uint64_t	zdde_ref_lsize;
2791b24ab676SJeff Bonwick 	uint64_t	zdde_ref_psize;
2792b24ab676SJeff Bonwick 	uint64_t	zdde_ref_dsize;
2793b24ab676SJeff Bonwick 	avl_node_t	zdde_node;
2794b24ab676SJeff Bonwick } zdb_ddt_entry_t;
2795b24ab676SJeff Bonwick 
2796b24ab676SJeff Bonwick /* ARGSUSED */
2797b24ab676SJeff Bonwick static int
2798b24ab676SJeff Bonwick zdb_ddt_add_cb(spa_t *spa, zilog_t *zilog, const blkptr_t *bp,
27997802d7bfSMatthew Ahrens     const zbookmark_phys_t *zb, const dnode_phys_t *dnp, void *arg)
2800b24ab676SJeff Bonwick {
2801b24ab676SJeff Bonwick 	avl_tree_t *t = arg;
2802b24ab676SJeff Bonwick 	avl_index_t where;
2803b24ab676SJeff Bonwick 	zdb_ddt_entry_t *zdde, zdde_search;
2804b24ab676SJeff Bonwick 
28055d7b4d43SMatthew Ahrens 	if (BP_IS_HOLE(bp) || BP_IS_EMBEDDED(bp))
2806b24ab676SJeff Bonwick 		return (0);
2807b24ab676SJeff Bonwick 
2808b24ab676SJeff Bonwick 	if (dump_opt['S'] > 1 && zb->zb_level == ZB_ROOT_LEVEL) {
2809b24ab676SJeff Bonwick 		(void) printf("traversing objset %llu, %llu objects, "
2810b24ab676SJeff Bonwick 		    "%lu blocks so far\n",
2811b24ab676SJeff Bonwick 		    (u_longlong_t)zb->zb_objset,
28125d7b4d43SMatthew Ahrens 		    (u_longlong_t)BP_GET_FILL(bp),
2813b24ab676SJeff Bonwick 		    avl_numnodes(t));
2814b24ab676SJeff Bonwick 	}
2815b24ab676SJeff Bonwick 
2816bbfd46c4SJeff Bonwick 	if (BP_IS_HOLE(bp) || BP_GET_CHECKSUM(bp) == ZIO_CHECKSUM_OFF ||
2817ad135b5dSChristopher Siden 	    BP_GET_LEVEL(bp) > 0 || DMU_OT_IS_METADATA(BP_GET_TYPE(bp)))
2818b24ab676SJeff Bonwick 		return (0);
2819b24ab676SJeff Bonwick 
2820b24ab676SJeff Bonwick 	ddt_key_fill(&zdde_search.zdde_key, bp);
2821b24ab676SJeff Bonwick 
2822b24ab676SJeff Bonwick 	zdde = avl_find(t, &zdde_search, &where);
2823b24ab676SJeff Bonwick 
2824b24ab676SJeff Bonwick 	if (zdde == NULL) {
2825b24ab676SJeff Bonwick 		zdde = umem_zalloc(sizeof (*zdde), UMEM_NOFAIL);
2826b24ab676SJeff Bonwick 		zdde->zdde_key = zdde_search.zdde_key;
2827b24ab676SJeff Bonwick 		avl_insert(t, zdde, where);
2828b24ab676SJeff Bonwick 	}
2829b24ab676SJeff Bonwick 
2830b24ab676SJeff Bonwick 	zdde->zdde_ref_blocks += 1;
2831b24ab676SJeff Bonwick 	zdde->zdde_ref_lsize += BP_GET_LSIZE(bp);
2832b24ab676SJeff Bonwick 	zdde->zdde_ref_psize += BP_GET_PSIZE(bp);
2833b24ab676SJeff Bonwick 	zdde->zdde_ref_dsize += bp_get_dsize_sync(spa, bp);
2834b24ab676SJeff Bonwick 
2835b24ab676SJeff Bonwick 	return (0);
2836b24ab676SJeff Bonwick }
2837b24ab676SJeff Bonwick 
2838b24ab676SJeff Bonwick static void
2839b24ab676SJeff Bonwick dump_simulated_ddt(spa_t *spa)
2840b24ab676SJeff Bonwick {
2841b24ab676SJeff Bonwick 	avl_tree_t t;
2842b24ab676SJeff Bonwick 	void *cookie = NULL;
2843b24ab676SJeff Bonwick 	zdb_ddt_entry_t *zdde;
2844b24ab676SJeff Bonwick 	ddt_histogram_t ddh_total = { 0 };
2845b24ab676SJeff Bonwick 	ddt_stat_t dds_total = { 0 };
2846b24ab676SJeff Bonwick 
2847b24ab676SJeff Bonwick 	avl_create(&t, ddt_entry_compare,
2848b24ab676SJeff Bonwick 	    sizeof (zdb_ddt_entry_t), offsetof(zdb_ddt_entry_t, zdde_node));
2849b24ab676SJeff Bonwick 
2850b24ab676SJeff Bonwick 	spa_config_enter(spa, SCL_CONFIG, FTAG, RW_READER);
2851b24ab676SJeff Bonwick 
2852bbfd46c4SJeff Bonwick 	(void) traverse_pool(spa, 0, TRAVERSE_PRE | TRAVERSE_PREFETCH_METADATA,
2853bbfd46c4SJeff Bonwick 	    zdb_ddt_add_cb, &t);
2854b24ab676SJeff Bonwick 
2855b24ab676SJeff Bonwick 	spa_config_exit(spa, SCL_CONFIG, FTAG);
2856b24ab676SJeff Bonwick 
2857b24ab676SJeff Bonwick 	while ((zdde = avl_destroy_nodes(&t, &cookie)) != NULL) {
2858b24ab676SJeff Bonwick 		ddt_stat_t dds;
2859b24ab676SJeff Bonwick 		uint64_t refcnt = zdde->zdde_ref_blocks;
2860b24ab676SJeff Bonwick 		ASSERT(refcnt != 0);
2861b24ab676SJeff Bonwick 
2862b24ab676SJeff Bonwick 		dds.dds_blocks = zdde->zdde_ref_blocks / refcnt;
2863b24ab676SJeff Bonwick 		dds.dds_lsize = zdde->zdde_ref_lsize / refcnt;
2864b24ab676SJeff Bonwick 		dds.dds_psize = zdde->zdde_ref_psize / refcnt;
2865b24ab676SJeff Bonwick 		dds.dds_dsize = zdde->zdde_ref_dsize / refcnt;
2866b24ab676SJeff Bonwick 
2867b24ab676SJeff Bonwick 		dds.dds_ref_blocks = zdde->zdde_ref_blocks;
2868b24ab676SJeff Bonwick 		dds.dds_ref_lsize = zdde->zdde_ref_lsize;
2869b24ab676SJeff Bonwick 		dds.dds_ref_psize = zdde->zdde_ref_psize;
2870b24ab676SJeff Bonwick 		dds.dds_ref_dsize = zdde->zdde_ref_dsize;
2871b24ab676SJeff Bonwick 
2872bf16b11eSMatthew Ahrens 		ddt_stat_add(&ddh_total.ddh_stat[highbit64(refcnt) - 1],
2873bf16b11eSMatthew Ahrens 		    &dds, 0);
2874b24ab676SJeff Bonwick 
2875b24ab676SJeff Bonwick 		umem_free(zdde, sizeof (*zdde));
2876b24ab676SJeff Bonwick 	}
2877b24ab676SJeff Bonwick 
2878b24ab676SJeff Bonwick 	avl_destroy(&t);
2879b24ab676SJeff Bonwick 
2880b24ab676SJeff Bonwick 	ddt_histogram_stat(&dds_total, &ddh_total);
2881b24ab676SJeff Bonwick 
2882b24ab676SJeff Bonwick 	(void) printf("Simulated DDT histogram:\n");
2883b24ab676SJeff Bonwick 
28849eb19f4dSGeorge Wilson 	zpool_dump_ddt(&dds_total, &ddh_total);
2885b24ab676SJeff Bonwick 
2886b24ab676SJeff Bonwick 	dump_dedup_ratio(&dds_total);
2887b24ab676SJeff Bonwick }
2888b24ab676SJeff Bonwick 
2889fa9e4066Sahrens static void
2890fa9e4066Sahrens dump_zpool(spa_t *spa)
2891fa9e4066Sahrens {
2892fa9e4066Sahrens 	dsl_pool_t *dp = spa_get_dsl(spa);
2893fa9e4066Sahrens 	int rc = 0;
2894fa9e4066Sahrens 
2895b24ab676SJeff Bonwick 	if (dump_opt['S']) {
2896b24ab676SJeff Bonwick 		dump_simulated_ddt(spa);
2897b24ab676SJeff Bonwick 		return;
2898b24ab676SJeff Bonwick 	}
2899b24ab676SJeff Bonwick 
290007428bdfSVictor Latushkin 	if (!dump_opt['e'] && dump_opt['C'] > 1) {
290107428bdfSVictor Latushkin 		(void) printf("\nCached configuration:\n");
290207428bdfSVictor Latushkin 		dump_nvlist(spa->spa_config, 8);
290307428bdfSVictor Latushkin 	}
290407428bdfSVictor Latushkin 
290507428bdfSVictor Latushkin 	if (dump_opt['C'])
290607428bdfSVictor Latushkin 		dump_config(spa);
290707428bdfSVictor Latushkin 
2908fa9e4066Sahrens 	if (dump_opt['u'])
290953b9a4a9SVictor Latushkin 		dump_uberblock(&spa->spa_uberblock, "\nUberblock:\n", "\n");
2910fa9e4066Sahrens 
2911b24ab676SJeff Bonwick 	if (dump_opt['D'])
2912b24ab676SJeff Bonwick 		dump_all_ddts(spa);
2913b24ab676SJeff Bonwick 
291487219db7SVictor Latushkin 	if (dump_opt['d'] > 2 || dump_opt['m'])
291587219db7SVictor Latushkin 		dump_metaslabs(spa);
29162e4c9986SGeorge Wilson 	if (dump_opt['M'])
29172e4c9986SGeorge Wilson 		dump_metaslab_groups(spa);
291887219db7SVictor Latushkin 
291987219db7SVictor Latushkin 	if (dump_opt['d'] || dump_opt['i']) {
2920fa9e4066Sahrens 		dump_dir(dp->dp_meta_objset);
2921fa9e4066Sahrens 		if (dump_opt['d'] >= 3) {
2922d0475637SMatthew Ahrens 			dump_bpobj(&spa->spa_deferred_bpobj,
2923d0475637SMatthew Ahrens 			    "Deferred frees", 0);
2924cde58dbcSMatthew Ahrens 			if (spa_version(spa) >= SPA_VERSION_DEADLISTS) {
2925cde58dbcSMatthew Ahrens 				dump_bpobj(&spa->spa_dsl_pool->dp_free_bpobj,
2926d0475637SMatthew Ahrens 				    "Pool snapshot frees", 0);
2927ad135b5dSChristopher Siden 			}
2928ad135b5dSChristopher Siden 
2929ad135b5dSChristopher Siden 			if (spa_feature_is_active(spa,
29302acef22dSMatthew Ahrens 			    SPA_FEATURE_ASYNC_DESTROY)) {
2931ad135b5dSChristopher Siden 				dump_bptree(spa->spa_meta_objset,
2932ad135b5dSChristopher Siden 				    spa->spa_dsl_pool->dp_bptree_obj,
2933ad135b5dSChristopher Siden 				    "Pool dataset frees");
2934cde58dbcSMatthew Ahrens 			}
2935fa9e4066Sahrens 			dump_dtl(spa->spa_root_vdev, 0);
2936fa9e4066Sahrens 		}
293707428bdfSVictor Latushkin 		(void) dmu_objset_find(spa_name(spa), dump_one_dir,
293807428bdfSVictor Latushkin 		    NULL, DS_FIND_SNAPSHOTS | DS_FIND_CHILDREN);
2939fa9e4066Sahrens 	}
2940b24ab676SJeff Bonwick 	if (dump_opt['b'] || dump_opt['c'])
2941fa9e4066Sahrens 		rc = dump_block_stats(spa);
2942fa9e4066Sahrens 
29430713e232SGeorge Wilson 	if (rc == 0)
29440713e232SGeorge Wilson 		rc = verify_spacemap_refcounts(spa);
29450713e232SGeorge Wilson 
2946fa9e4066Sahrens 	if (dump_opt['s'])
2947fa9e4066Sahrens 		show_pool_stats(spa);
2948fa9e4066Sahrens 
29498f18d1faSGeorge Wilson 	if (dump_opt['h'])
29508f18d1faSGeorge Wilson 		dump_history(spa);
29518f18d1faSGeorge Wilson 
2952fa9e4066Sahrens 	if (rc != 0)
2953fa9e4066Sahrens 		exit(rc);
2954fa9e4066Sahrens }
2955fa9e4066Sahrens 
295644cd46caSbillm #define	ZDB_FLAG_CHECKSUM	0x0001
295744cd46caSbillm #define	ZDB_FLAG_DECOMPRESS	0x0002
295844cd46caSbillm #define	ZDB_FLAG_BSWAP		0x0004
295944cd46caSbillm #define	ZDB_FLAG_GBH		0x0008
296044cd46caSbillm #define	ZDB_FLAG_INDIRECT	0x0010
296144cd46caSbillm #define	ZDB_FLAG_PHYS		0x0020
296244cd46caSbillm #define	ZDB_FLAG_RAW		0x0040
296344cd46caSbillm #define	ZDB_FLAG_PRINT_BLKPTR	0x0080
296444cd46caSbillm 
296544cd46caSbillm int flagbits[256];
296644cd46caSbillm 
296744cd46caSbillm static void
296844cd46caSbillm zdb_print_blkptr(blkptr_t *bp, int flags)
296944cd46caSbillm {
2970b24ab676SJeff Bonwick 	char blkbuf[BP_SPRINTF_LEN];
297144cd46caSbillm 
297244cd46caSbillm 	if (flags & ZDB_FLAG_BSWAP)
297344cd46caSbillm 		byteswap_uint64_array((void *)bp, sizeof (blkptr_t));
2974b24ab676SJeff Bonwick 
297543466aaeSMax Grossman 	snprintf_blkptr(blkbuf, sizeof (blkbuf), bp);
2976b24ab676SJeff Bonwick 	(void) printf("%s\n", blkbuf);
297744cd46caSbillm }
297844cd46caSbillm 
297944cd46caSbillm static void
298044cd46caSbillm zdb_dump_indirect(blkptr_t *bp, int nbps, int flags)
298144cd46caSbillm {
298244cd46caSbillm 	int i;
298344cd46caSbillm 
298444cd46caSbillm 	for (i = 0; i < nbps; i++)
298544cd46caSbillm 		zdb_print_blkptr(&bp[i], flags);
298644cd46caSbillm }
298744cd46caSbillm 
298844cd46caSbillm static void
298944cd46caSbillm zdb_dump_gbh(void *buf, int flags)
299044cd46caSbillm {
299144cd46caSbillm 	zdb_dump_indirect((blkptr_t *)buf, SPA_GBH_NBLKPTRS, flags);
299244cd46caSbillm }
299344cd46caSbillm 
299444cd46caSbillm static void
299544cd46caSbillm zdb_dump_block_raw(void *buf, uint64_t size, int flags)
299644cd46caSbillm {
299744cd46caSbillm 	if (flags & ZDB_FLAG_BSWAP)
299844cd46caSbillm 		byteswap_uint64_array(buf, size);
2999b24ab676SJeff Bonwick 	(void) write(1, buf, size);
300044cd46caSbillm }
300144cd46caSbillm 
300244cd46caSbillm static void
300344cd46caSbillm zdb_dump_block(char *label, void *buf, uint64_t size, int flags)
300444cd46caSbillm {
300544cd46caSbillm 	uint64_t *d = (uint64_t *)buf;
300644cd46caSbillm 	int nwords = size / sizeof (uint64_t);
300744cd46caSbillm 	int do_bswap = !!(flags & ZDB_FLAG_BSWAP);
300844cd46caSbillm 	int i, j;
300944cd46caSbillm 	char *hdr, *c;
301044cd46caSbillm 
301144cd46caSbillm 
301244cd46caSbillm 	if (do_bswap)
301344cd46caSbillm 		hdr = " 7 6 5 4 3 2 1 0   f e d c b a 9 8";
301444cd46caSbillm 	else
301544cd46caSbillm 		hdr = " 0 1 2 3 4 5 6 7   8 9 a b c d e f";
301644cd46caSbillm 
301744cd46caSbillm 	(void) printf("\n%s\n%6s   %s  0123456789abcdef\n", label, "", hdr);
301844cd46caSbillm 
301944cd46caSbillm 	for (i = 0; i < nwords; i += 2) {
302044cd46caSbillm 		(void) printf("%06llx:  %016llx  %016llx  ",
302144cd46caSbillm 		    (u_longlong_t)(i * sizeof (uint64_t)),
302244cd46caSbillm 		    (u_longlong_t)(do_bswap ? BSWAP_64(d[i]) : d[i]),
302344cd46caSbillm 		    (u_longlong_t)(do_bswap ? BSWAP_64(d[i + 1]) : d[i + 1]));
302444cd46caSbillm 
302544cd46caSbillm 		c = (char *)&d[i];
302644cd46caSbillm 		for (j = 0; j < 2 * sizeof (uint64_t); j++)
302744cd46caSbillm 			(void) printf("%c", isprint(c[j]) ? c[j] : '.');
302844cd46caSbillm 		(void) printf("\n");
302944cd46caSbillm 	}
303044cd46caSbillm }
303144cd46caSbillm 
303244cd46caSbillm /*
303344cd46caSbillm  * There are two acceptable formats:
303444cd46caSbillm  *	leaf_name	  - For example: c1t0d0 or /tmp/ztest.0a
303544cd46caSbillm  *	child[.child]*    - For example: 0.1.1
303644cd46caSbillm  *
303744cd46caSbillm  * The second form can be used to specify arbitrary vdevs anywhere
303844cd46caSbillm  * in the heirarchy.  For example, in a pool with a mirror of
303944cd46caSbillm  * RAID-Zs, you can specify either RAID-Z vdev with 0.0 or 0.1 .
304044cd46caSbillm  */
304144cd46caSbillm static vdev_t *
304244cd46caSbillm zdb_vdev_lookup(vdev_t *vdev, char *path)
304344cd46caSbillm {
304444cd46caSbillm 	char *s, *p, *q;
304544cd46caSbillm 	int i;
304644cd46caSbillm 
304744cd46caSbillm 	if (vdev == NULL)
304844cd46caSbillm 		return (NULL);
304944cd46caSbillm 
305044cd46caSbillm 	/* First, assume the x.x.x.x format */
305144cd46caSbillm 	i = (int)strtoul(path, &s, 10);
305244cd46caSbillm 	if (s == path || (s && *s != '.' && *s != '\0'))
305344cd46caSbillm 		goto name;
305444cd46caSbillm 	if (i < 0 || i >= vdev->vdev_children)
305544cd46caSbillm 		return (NULL);
305644cd46caSbillm 
305744cd46caSbillm 	vdev = vdev->vdev_child[i];
305844cd46caSbillm 	if (*s == '\0')
305944cd46caSbillm 		return (vdev);
306044cd46caSbillm 	return (zdb_vdev_lookup(vdev, s+1));
306144cd46caSbillm 
306244cd46caSbillm name:
306344cd46caSbillm 	for (i = 0; i < vdev->vdev_children; i++) {
306444cd46caSbillm 		vdev_t *vc = vdev->vdev_child[i];
306544cd46caSbillm 
306644cd46caSbillm 		if (vc->vdev_path == NULL) {
306744cd46caSbillm 			vc = zdb_vdev_lookup(vc, path);
306844cd46caSbillm 			if (vc == NULL)
306944cd46caSbillm 				continue;
307044cd46caSbillm 			else
307144cd46caSbillm 				return (vc);
307244cd46caSbillm 		}
307344cd46caSbillm 
307444cd46caSbillm 		p = strrchr(vc->vdev_path, '/');
307544cd46caSbillm 		p = p ? p + 1 : vc->vdev_path;
307644cd46caSbillm 		q = &vc->vdev_path[strlen(vc->vdev_path) - 2];
307744cd46caSbillm 
307844cd46caSbillm 		if (strcmp(vc->vdev_path, path) == 0)
307944cd46caSbillm 			return (vc);
308044cd46caSbillm 		if (strcmp(p, path) == 0)
308144cd46caSbillm 			return (vc);
308244cd46caSbillm 		if (strcmp(q, "s0") == 0 && strncmp(p, path, q - p) == 0)
308344cd46caSbillm 			return (vc);
308444cd46caSbillm 	}
308544cd46caSbillm 
308644cd46caSbillm 	return (NULL);
308744cd46caSbillm }
308844cd46caSbillm 
308944cd46caSbillm /*
309044cd46caSbillm  * Read a block from a pool and print it out.  The syntax of the
309144cd46caSbillm  * block descriptor is:
309244cd46caSbillm  *
309344cd46caSbillm  *	pool:vdev_specifier:offset:size[:flags]
309444cd46caSbillm  *
309544cd46caSbillm  *	pool           - The name of the pool you wish to read from
309644cd46caSbillm  *	vdev_specifier - Which vdev (see comment for zdb_vdev_lookup)
309744cd46caSbillm  *	offset         - offset, in hex, in bytes
309844cd46caSbillm  *	size           - Amount of data to read, in hex, in bytes
309944cd46caSbillm  *	flags          - A string of characters specifying options
310044cd46caSbillm  *		 b: Decode a blkptr at given offset within block
310144cd46caSbillm  *		*c: Calculate and display checksums
3102b24ab676SJeff Bonwick  *		 d: Decompress data before dumping
310344cd46caSbillm  *		 e: Byteswap data before dumping
3104b24ab676SJeff Bonwick  *		 g: Display data as a gang block header
3105b24ab676SJeff Bonwick  *		 i: Display as an indirect block
310644cd46caSbillm  *		 p: Do I/O to physical offset
310744cd46caSbillm  *		 r: Dump raw data to stdout
310844cd46caSbillm  *
310944cd46caSbillm  *              * = not yet implemented
311044cd46caSbillm  */
311144cd46caSbillm static void
311207428bdfSVictor Latushkin zdb_read_block(char *thing, spa_t *spa)
311344cd46caSbillm {
3114b24ab676SJeff Bonwick 	blkptr_t blk, *bp = &blk;
3115b24ab676SJeff Bonwick 	dva_t *dva = bp->blk_dva;
311644cd46caSbillm 	int flags = 0;
3117b24ab676SJeff Bonwick 	uint64_t offset = 0, size = 0, psize = 0, lsize = 0, blkptr_offset = 0;
311844cd46caSbillm 	zio_t *zio;
311944cd46caSbillm 	vdev_t *vd;
3120b24ab676SJeff Bonwick 	void *pbuf, *lbuf, *buf;
312107428bdfSVictor Latushkin 	char *s, *p, *dup, *vdev, *flagstr;
3122b24ab676SJeff Bonwick 	int i, error;
312344cd46caSbillm 
312444cd46caSbillm 	dup = strdup(thing);
312544cd46caSbillm 	s = strtok(dup, ":");
312644cd46caSbillm 	vdev = s ? s : "";
312744cd46caSbillm 	s = strtok(NULL, ":");
312844cd46caSbillm 	offset = strtoull(s ? s : "", NULL, 16);
312944cd46caSbillm 	s = strtok(NULL, ":");
313044cd46caSbillm 	size = strtoull(s ? s : "", NULL, 16);
313144cd46caSbillm 	s = strtok(NULL, ":");
313244cd46caSbillm 	flagstr = s ? s : "";
313344cd46caSbillm 
313444cd46caSbillm 	s = NULL;
313544cd46caSbillm 	if (size == 0)
313644cd46caSbillm 		s = "size must not be zero";
313744cd46caSbillm 	if (!IS_P2ALIGNED(size, DEV_BSIZE))
313844cd46caSbillm 		s = "size must be a multiple of sector size";
313944cd46caSbillm 	if (!IS_P2ALIGNED(offset, DEV_BSIZE))
314044cd46caSbillm 		s = "offset must be a multiple of sector size";
314144cd46caSbillm 	if (s) {
314244cd46caSbillm 		(void) printf("Invalid block specifier: %s  - %s\n", thing, s);
314344cd46caSbillm 		free(dup);
314444cd46caSbillm 		return;
314544cd46caSbillm 	}
314644cd46caSbillm 
314744cd46caSbillm 	for (s = strtok(flagstr, ":"); s; s = strtok(NULL, ":")) {
314844cd46caSbillm 		for (i = 0; flagstr[i]; i++) {
31495ad82045Snd 			int bit = flagbits[(uchar_t)flagstr[i]];
315044cd46caSbillm 
315144cd46caSbillm 			if (bit == 0) {
315244cd46caSbillm 				(void) printf("***Invalid flag: %c\n",
315344cd46caSbillm 				    flagstr[i]);
315444cd46caSbillm 				continue;
315544cd46caSbillm 			}
315644cd46caSbillm 			flags |= bit;
315744cd46caSbillm 
315844cd46caSbillm 			/* If it's not something with an argument, keep going */
3159b24ab676SJeff Bonwick 			if ((bit & (ZDB_FLAG_CHECKSUM |
316044cd46caSbillm 			    ZDB_FLAG_PRINT_BLKPTR)) == 0)
316144cd46caSbillm 				continue;
316244cd46caSbillm 
316344cd46caSbillm 			p = &flagstr[i + 1];
316444cd46caSbillm 			if (bit == ZDB_FLAG_PRINT_BLKPTR)
316544cd46caSbillm 				blkptr_offset = strtoull(p, &p, 16);
316644cd46caSbillm 			if (*p != ':' && *p != '\0') {
316744cd46caSbillm 				(void) printf("***Invalid flag arg: '%s'\n", s);
316844cd46caSbillm 				free(dup);
316944cd46caSbillm 				return;
317044cd46caSbillm 			}
317144cd46caSbillm 		}
317244cd46caSbillm 	}
317344cd46caSbillm 
317444cd46caSbillm 	vd = zdb_vdev_lookup(spa->spa_root_vdev, vdev);
317544cd46caSbillm 	if (vd == NULL) {
317644cd46caSbillm 		(void) printf("***Invalid vdev: %s\n", vdev);
317744cd46caSbillm 		free(dup);
317844cd46caSbillm 		return;
317944cd46caSbillm 	} else {
318044cd46caSbillm 		if (vd->vdev_path)
3181b24ab676SJeff Bonwick 			(void) fprintf(stderr, "Found vdev: %s\n",
3182b24ab676SJeff Bonwick 			    vd->vdev_path);
318344cd46caSbillm 		else
3184b24ab676SJeff Bonwick 			(void) fprintf(stderr, "Found vdev type: %s\n",
318544cd46caSbillm 			    vd->vdev_ops->vdev_op_type);
318644cd46caSbillm 	}
318744cd46caSbillm 
3188b24ab676SJeff Bonwick 	psize = size;
3189b24ab676SJeff Bonwick 	lsize = size;
319044cd46caSbillm 
3191b24ab676SJeff Bonwick 	pbuf = umem_alloc(SPA_MAXBLOCKSIZE, UMEM_NOFAIL);
3192b24ab676SJeff Bonwick 	lbuf = umem_alloc(SPA_MAXBLOCKSIZE, UMEM_NOFAIL);
3193b24ab676SJeff Bonwick 
3194b24ab676SJeff Bonwick 	BP_ZERO(bp);
3195b24ab676SJeff Bonwick 
3196b24ab676SJeff Bonwick 	DVA_SET_VDEV(&dva[0], vd->vdev_id);
3197b24ab676SJeff Bonwick 	DVA_SET_OFFSET(&dva[0], offset);
3198b24ab676SJeff Bonwick 	DVA_SET_GANG(&dva[0], !!(flags & ZDB_FLAG_GBH));
3199b24ab676SJeff Bonwick 	DVA_SET_ASIZE(&dva[0], vdev_psize_to_asize(vd, psize));
3200b24ab676SJeff Bonwick 
3201b24ab676SJeff Bonwick 	BP_SET_BIRTH(bp, TXG_INITIAL, TXG_INITIAL);
3202b24ab676SJeff Bonwick 
3203b24ab676SJeff Bonwick 	BP_SET_LSIZE(bp, lsize);
3204b24ab676SJeff Bonwick 	BP_SET_PSIZE(bp, psize);
3205b24ab676SJeff Bonwick 	BP_SET_COMPRESS(bp, ZIO_COMPRESS_OFF);
3206b24ab676SJeff Bonwick 	BP_SET_CHECKSUM(bp, ZIO_CHECKSUM_OFF);
3207b24ab676SJeff Bonwick 	BP_SET_TYPE(bp, DMU_OT_NONE);
3208b24ab676SJeff Bonwick 	BP_SET_LEVEL(bp, 0);
3209b24ab676SJeff Bonwick 	BP_SET_DEDUP(bp, 0);
3210b24ab676SJeff Bonwick 	BP_SET_BYTEORDER(bp, ZFS_HOST_BYTEORDER);
321144cd46caSbillm 
3212e14bb325SJeff Bonwick 	spa_config_enter(spa, SCL_STATE, FTAG, RW_READER);
321344cd46caSbillm 	zio = zio_root(spa, NULL, NULL, 0);
3214b24ab676SJeff Bonwick 
3215b24ab676SJeff Bonwick 	if (vd == vd->vdev_top) {
3216b24ab676SJeff Bonwick 		/*
3217b24ab676SJeff Bonwick 		 * Treat this as a normal block read.
3218b24ab676SJeff Bonwick 		 */
3219b24ab676SJeff Bonwick 		zio_nowait(zio_read(zio, spa, bp, pbuf, psize, NULL, NULL,
3220b24ab676SJeff Bonwick 		    ZIO_PRIORITY_SYNC_READ,
3221b24ab676SJeff Bonwick 		    ZIO_FLAG_CANFAIL | ZIO_FLAG_RAW, NULL));
3222b24ab676SJeff Bonwick 	} else {
3223b24ab676SJeff Bonwick 		/*
3224b24ab676SJeff Bonwick 		 * Treat this as a vdev child I/O.
3225b24ab676SJeff Bonwick 		 */
3226b24ab676SJeff Bonwick 		zio_nowait(zio_vdev_child_io(zio, bp, vd, offset, pbuf, psize,
3227b24ab676SJeff Bonwick 		    ZIO_TYPE_READ, ZIO_PRIORITY_SYNC_READ,
3228b24ab676SJeff Bonwick 		    ZIO_FLAG_DONT_CACHE | ZIO_FLAG_DONT_QUEUE |
3229b24ab676SJeff Bonwick 		    ZIO_FLAG_DONT_PROPAGATE | ZIO_FLAG_DONT_RETRY |
3230b24ab676SJeff Bonwick 		    ZIO_FLAG_CANFAIL | ZIO_FLAG_RAW, NULL, NULL));
3231b24ab676SJeff Bonwick 	}
3232b24ab676SJeff Bonwick 
323344cd46caSbillm 	error = zio_wait(zio);
3234e14bb325SJeff Bonwick 	spa_config_exit(spa, SCL_STATE, FTAG);
323544cd46caSbillm 
323644cd46caSbillm 	if (error) {
323744cd46caSbillm 		(void) printf("Read of %s failed, error: %d\n", thing, error);
323844cd46caSbillm 		goto out;
323944cd46caSbillm 	}
324044cd46caSbillm 
3241b24ab676SJeff Bonwick 	if (flags & ZDB_FLAG_DECOMPRESS) {
3242b24ab676SJeff Bonwick 		/*
3243b24ab676SJeff Bonwick 		 * We don't know how the data was compressed, so just try
3244b24ab676SJeff Bonwick 		 * every decompress function at every inflated blocksize.
3245b24ab676SJeff Bonwick 		 */
3246b24ab676SJeff Bonwick 		enum zio_compress c;
3247b24ab676SJeff Bonwick 		void *pbuf2 = umem_alloc(SPA_MAXBLOCKSIZE, UMEM_NOFAIL);
3248b24ab676SJeff Bonwick 		void *lbuf2 = umem_alloc(SPA_MAXBLOCKSIZE, UMEM_NOFAIL);
3249b24ab676SJeff Bonwick 
3250b24ab676SJeff Bonwick 		bcopy(pbuf, pbuf2, psize);
3251b24ab676SJeff Bonwick 
3252b24ab676SJeff Bonwick 		VERIFY(random_get_pseudo_bytes((uint8_t *)pbuf + psize,
3253b24ab676SJeff Bonwick 		    SPA_MAXBLOCKSIZE - psize) == 0);
3254b24ab676SJeff Bonwick 
3255b24ab676SJeff Bonwick 		VERIFY(random_get_pseudo_bytes((uint8_t *)pbuf2 + psize,
3256b24ab676SJeff Bonwick 		    SPA_MAXBLOCKSIZE - psize) == 0);
3257b24ab676SJeff Bonwick 
3258b24ab676SJeff Bonwick 		for (lsize = SPA_MAXBLOCKSIZE; lsize > psize;
3259b24ab676SJeff Bonwick 		    lsize -= SPA_MINBLOCKSIZE) {
3260b24ab676SJeff Bonwick 			for (c = 0; c < ZIO_COMPRESS_FUNCTIONS; c++) {
3261b24ab676SJeff Bonwick 				if (zio_decompress_data(c, pbuf, lbuf,
3262b24ab676SJeff Bonwick 				    psize, lsize) == 0 &&
3263b24ab676SJeff Bonwick 				    zio_decompress_data(c, pbuf2, lbuf2,
3264b24ab676SJeff Bonwick 				    psize, lsize) == 0 &&
3265b24ab676SJeff Bonwick 				    bcmp(lbuf, lbuf2, lsize) == 0)
3266b24ab676SJeff Bonwick 					break;
3267b24ab676SJeff Bonwick 			}
3268b24ab676SJeff Bonwick 			if (c != ZIO_COMPRESS_FUNCTIONS)
3269b24ab676SJeff Bonwick 				break;
3270b24ab676SJeff Bonwick 			lsize -= SPA_MINBLOCKSIZE;
3271b24ab676SJeff Bonwick 		}
3272b24ab676SJeff Bonwick 
3273b24ab676SJeff Bonwick 		umem_free(pbuf2, SPA_MAXBLOCKSIZE);
3274b24ab676SJeff Bonwick 		umem_free(lbuf2, SPA_MAXBLOCKSIZE);
3275b24ab676SJeff Bonwick 
3276b24ab676SJeff Bonwick 		if (lsize <= psize) {
3277b24ab676SJeff Bonwick 			(void) printf("Decompress of %s failed\n", thing);
3278b24ab676SJeff Bonwick 			goto out;
3279b24ab676SJeff Bonwick 		}
3280b24ab676SJeff Bonwick 		buf = lbuf;
3281b24ab676SJeff Bonwick 		size = lsize;
3282b24ab676SJeff Bonwick 	} else {
3283b24ab676SJeff Bonwick 		buf = pbuf;
3284b24ab676SJeff Bonwick 		size = psize;
3285b24ab676SJeff Bonwick 	}
3286b24ab676SJeff Bonwick 
328744cd46caSbillm 	if (flags & ZDB_FLAG_PRINT_BLKPTR)
328844cd46caSbillm 		zdb_print_blkptr((blkptr_t *)(void *)
328944cd46caSbillm 		    ((uintptr_t)buf + (uintptr_t)blkptr_offset), flags);
329044cd46caSbillm 	else if (flags & ZDB_FLAG_RAW)
329144cd46caSbillm 		zdb_dump_block_raw(buf, size, flags);
329244cd46caSbillm 	else if (flags & ZDB_FLAG_INDIRECT)
329344cd46caSbillm 		zdb_dump_indirect((blkptr_t *)buf, size / sizeof (blkptr_t),
329444cd46caSbillm 		    flags);
329544cd46caSbillm 	else if (flags & ZDB_FLAG_GBH)
329644cd46caSbillm 		zdb_dump_gbh(buf, flags);
329744cd46caSbillm 	else
329844cd46caSbillm 		zdb_dump_block(thing, buf, size, flags);
329944cd46caSbillm 
330044cd46caSbillm out:
3301b24ab676SJeff Bonwick 	umem_free(pbuf, SPA_MAXBLOCKSIZE);
3302b24ab676SJeff Bonwick 	umem_free(lbuf, SPA_MAXBLOCKSIZE);
330344cd46caSbillm 	free(dup);
330444cd46caSbillm }
330544cd46caSbillm 
3306de6628f0Sck static boolean_t
33073ad6c7f9SVictor Latushkin pool_match(nvlist_t *cfg, char *tgt)
3308de6628f0Sck {
33093ad6c7f9SVictor Latushkin 	uint64_t v, guid = strtoull(tgt, NULL, 0);
3310de6628f0Sck 	char *s;
3311de6628f0Sck 
3312de6628f0Sck 	if (guid != 0) {
33133ad6c7f9SVictor Latushkin 		if (nvlist_lookup_uint64(cfg, ZPOOL_CONFIG_POOL_GUID, &v) == 0)
33143ad6c7f9SVictor Latushkin 			return (v == guid);
3315de6628f0Sck 	} else {
33163ad6c7f9SVictor Latushkin 		if (nvlist_lookup_string(cfg, ZPOOL_CONFIG_POOL_NAME, &s) == 0)
33173ad6c7f9SVictor Latushkin 			return (strcmp(s, tgt) == 0);
3318de6628f0Sck 	}
33193ad6c7f9SVictor Latushkin 	return (B_FALSE);
3320de6628f0Sck }
3321de6628f0Sck 
33223ad6c7f9SVictor Latushkin static char *
33233ad6c7f9SVictor Latushkin find_zpool(char **target, nvlist_t **configp, int dirc, char **dirv)
3324de6628f0Sck {
3325de6628f0Sck 	nvlist_t *pools;
3326de6628f0Sck 	nvlist_t *match = NULL;
33273ad6c7f9SVictor Latushkin 	char *name = NULL;
33283ad6c7f9SVictor Latushkin 	char *sepp = NULL;
33293ad6c7f9SVictor Latushkin 	char sep;
33303ad6c7f9SVictor Latushkin 	int count = 0;
3331d41c4376SMark J Musante 	importargs_t args = { 0 };
3332d41c4376SMark J Musante 
3333d41c4376SMark J Musante 	args.paths = dirc;
3334d41c4376SMark J Musante 	args.path = dirv;
3335d41c4376SMark J Musante 	args.can_be_active = B_TRUE;
3336de6628f0Sck 
33373ad6c7f9SVictor Latushkin 	if ((sepp = strpbrk(*target, "/@")) != NULL) {
33383ad6c7f9SVictor Latushkin 		sep = *sepp;
33393ad6c7f9SVictor Latushkin 		*sepp = '\0';
33403ad6c7f9SVictor Latushkin 	}
33413ad6c7f9SVictor Latushkin 
3342d41c4376SMark J Musante 	pools = zpool_search_import(g_zfs, &args);
3343de6628f0Sck 
3344de6628f0Sck 	if (pools != NULL) {
3345de6628f0Sck 		nvpair_t *elem = NULL;
3346de6628f0Sck 		while ((elem = nvlist_next_nvpair(pools, elem)) != NULL) {
3347de6628f0Sck 			verify(nvpair_value_nvlist(elem, configp) == 0);
33483ad6c7f9SVictor Latushkin 			if (pool_match(*configp, *target)) {
33493ad6c7f9SVictor Latushkin 				count++;
3350de6628f0Sck 				if (match != NULL) {
33513ad6c7f9SVictor Latushkin 					/* print previously found config */
33523ad6c7f9SVictor Latushkin 					if (name != NULL) {
33533ad6c7f9SVictor Latushkin 						(void) printf("%s\n", name);
33543ad6c7f9SVictor Latushkin 						dump_nvlist(match, 8);
33553ad6c7f9SVictor Latushkin 						name = NULL;
33563ad6c7f9SVictor Latushkin 					}
33573ad6c7f9SVictor Latushkin 					(void) printf("%s\n",
33583ad6c7f9SVictor Latushkin 					    nvpair_name(elem));
33593ad6c7f9SVictor Latushkin 					dump_nvlist(*configp, 8);
3360de6628f0Sck 				} else {
3361de6628f0Sck 					match = *configp;
33623ad6c7f9SVictor Latushkin 					name = nvpair_name(elem);
3363de6628f0Sck 				}
3364de6628f0Sck 			}
3365de6628f0Sck 		}
3366de6628f0Sck 	}
33673ad6c7f9SVictor Latushkin 	if (count > 1)
33683ad6c7f9SVictor Latushkin 		(void) fatal("\tMatched %d pools - use pool GUID "
33693ad6c7f9SVictor Latushkin 		    "instead of pool name or \n"
33703ad6c7f9SVictor Latushkin 		    "\tpool name part of a dataset name to select pool", count);
33713ad6c7f9SVictor Latushkin 
33723ad6c7f9SVictor Latushkin 	if (sepp)
33733ad6c7f9SVictor Latushkin 		*sepp = sep;
33743ad6c7f9SVictor Latushkin 	/*
33753ad6c7f9SVictor Latushkin 	 * If pool GUID was specified for pool id, replace it with pool name
33763ad6c7f9SVictor Latushkin 	 */
33773ad6c7f9SVictor Latushkin 	if (name && (strstr(*target, name) != *target)) {
33783ad6c7f9SVictor Latushkin 		int sz = 1 + strlen(name) + ((sepp) ? strlen(sepp) : 0);
33793ad6c7f9SVictor Latushkin 
33803ad6c7f9SVictor Latushkin 		*target = umem_alloc(sz, UMEM_NOFAIL);
33813ad6c7f9SVictor Latushkin 		(void) snprintf(*target, sz, "%s%s", name, sepp ? sepp : "");
33823ad6c7f9SVictor Latushkin 	}
3383de6628f0Sck 
33843ad6c7f9SVictor Latushkin 	*configp = name ? match : NULL;
3385de6628f0Sck 
33863ad6c7f9SVictor Latushkin 	return (name);
3387de6628f0Sck }
3388de6628f0Sck 
3389fa9e4066Sahrens int
3390fa9e4066Sahrens main(int argc, char **argv)
3391fa9e4066Sahrens {
3392fa9e4066Sahrens 	int i, c;
3393fa9e4066Sahrens 	struct rlimit rl = { 1024, 1024 };
33943ad6c7f9SVictor Latushkin 	spa_t *spa = NULL;
3395fa9e4066Sahrens 	objset_t *os = NULL;
3396fa9e4066Sahrens 	int dump_all = 1;
3397fa9e4066Sahrens 	int verbose = 0;
3398c8ee1847SVictor Latushkin 	int error = 0;
33993ad6c7f9SVictor Latushkin 	char **searchdirs = NULL;
34003ad6c7f9SVictor Latushkin 	int nsearch = 0;
34013ad6c7f9SVictor Latushkin 	char *target;
3402468c413aSTim Haley 	nvlist_t *policy = NULL;
3403468c413aSTim Haley 	uint64_t max_txg = UINT64_MAX;
3404c8ee1847SVictor Latushkin 	int rewind = ZPOOL_NEVER_REWIND;
3405fa9e4066Sahrens 
3406fa9e4066Sahrens 	(void) setrlimit(RLIMIT_NOFILE, &rl);
3407004388ebScasper 	(void) enable_extended_FILE_stdio(-1, -1);
3408fa9e4066Sahrens 
3409fa9e4066Sahrens 	dprintf_setup(&argc, argv);
3410fa9e4066Sahrens 
3411df15e419SMatthew Ahrens 	while ((c = getopt(argc, argv,
34122e4c9986SGeorge Wilson 	    "bcdhilmMI:suCDRSAFLXx:evp:t:U:P")) != -1) {
3413fa9e4066Sahrens 		switch (c) {
3414fa9e4066Sahrens 		case 'b':
3415fa9e4066Sahrens 		case 'c':
3416b24ab676SJeff Bonwick 		case 'd':
3417b24ab676SJeff Bonwick 		case 'h':
3418b24ab676SJeff Bonwick 		case 'i':
3419b24ab676SJeff Bonwick 		case 'l':
3420d6e555bdSGeorge Wilson 		case 'm':
3421fa9e4066Sahrens 		case 's':
3422b24ab676SJeff Bonwick 		case 'u':
3423fa9e4066Sahrens 		case 'C':
3424b24ab676SJeff Bonwick 		case 'D':
34252e4c9986SGeorge Wilson 		case 'M':
342644cd46caSbillm 		case 'R':
3427b24ab676SJeff Bonwick 		case 'S':
3428fa9e4066Sahrens 			dump_opt[c]++;
3429fa9e4066Sahrens 			dump_all = 0;
3430fa9e4066Sahrens 			break;
3431feef89cfSVictor Latushkin 		case 'A':
3432c8ee1847SVictor Latushkin 		case 'F':
343382a0a985SVictor Latushkin 		case 'L':
3434c8ee1847SVictor Latushkin 		case 'X':
34353ad6c7f9SVictor Latushkin 		case 'e':
34363f9d6ad7SLin Ling 		case 'P':
343782a0a985SVictor Latushkin 			dump_opt[c]++;
343882a0a985SVictor Latushkin 			break;
34392e4c9986SGeorge Wilson 		case 'I':
344031d7e8faSGeorge Wilson 			max_inflight = strtoull(optarg, NULL, 0);
344131d7e8faSGeorge Wilson 			if (max_inflight == 0) {
344231d7e8faSGeorge Wilson 				(void) fprintf(stderr, "maximum number "
344331d7e8faSGeorge Wilson 				    "of inflight I/Os must be greater "
344431d7e8faSGeorge Wilson 				    "than 0\n");
344531d7e8faSGeorge Wilson 				usage();
344631d7e8faSGeorge Wilson 			}
344731d7e8faSGeorge Wilson 			break;
3448de6628f0Sck 		case 'p':
34493ad6c7f9SVictor Latushkin 			if (searchdirs == NULL) {
34503ad6c7f9SVictor Latushkin 				searchdirs = umem_alloc(sizeof (char *),
34513ad6c7f9SVictor Latushkin 				    UMEM_NOFAIL);
34523ad6c7f9SVictor Latushkin 			} else {
34533ad6c7f9SVictor Latushkin 				char **tmp = umem_alloc((nsearch + 1) *
34543ad6c7f9SVictor Latushkin 				    sizeof (char *), UMEM_NOFAIL);
34553ad6c7f9SVictor Latushkin 				bcopy(searchdirs, tmp, nsearch *
34563ad6c7f9SVictor Latushkin 				    sizeof (char *));
34573ad6c7f9SVictor Latushkin 				umem_free(searchdirs,
34583ad6c7f9SVictor Latushkin 				    nsearch * sizeof (char *));
34593ad6c7f9SVictor Latushkin 				searchdirs = tmp;
34603ad6c7f9SVictor Latushkin 			}
34613ad6c7f9SVictor Latushkin 			searchdirs[nsearch++] = optarg;
3462de6628f0Sck 			break;
34632e551927SVictor Latushkin 		case 't':
3464468c413aSTim Haley 			max_txg = strtoull(optarg, NULL, 0);
3465468c413aSTim Haley 			if (max_txg < TXG_INITIAL) {
34662e551927SVictor Latushkin 				(void) fprintf(stderr, "incorrect txg "
34672e551927SVictor Latushkin 				    "specified: %s\n", optarg);
34682e551927SVictor Latushkin 				usage();
34692e551927SVictor Latushkin 			}
34702e551927SVictor Latushkin 			break;
3471b24ab676SJeff Bonwick 		case 'U':
3472b24ab676SJeff Bonwick 			spa_config_path = optarg;
3473b24ab676SJeff Bonwick 			break;
34742e4c9986SGeorge Wilson 		case 'v':
34752e4c9986SGeorge Wilson 			verbose++;
34762e4c9986SGeorge Wilson 			break;
34772e4c9986SGeorge Wilson 		case 'x':
34782e4c9986SGeorge Wilson 			vn_dumpdir = optarg;
34792e4c9986SGeorge Wilson 			break;
3480fa9e4066Sahrens 		default:
3481fa9e4066Sahrens 			usage();
3482fa9e4066Sahrens 			break;
3483fa9e4066Sahrens 		}
3484fa9e4066Sahrens 	}
3485fa9e4066Sahrens 
34863ad6c7f9SVictor Latushkin 	if (!dump_opt['e'] && searchdirs != NULL) {
348788b7b0f2SMatthew Ahrens 		(void) fprintf(stderr, "-p option requires use of -e\n");
348888b7b0f2SMatthew Ahrens 		usage();
348988b7b0f2SMatthew Ahrens 	}
3490de6628f0Sck 
349106be9802SMatthew Ahrens 	/*
349206be9802SMatthew Ahrens 	 * ZDB does not typically re-read blocks; therefore limit the ARC
349306be9802SMatthew Ahrens 	 * to 256 MB, which can be used entirely for metadata.
349406be9802SMatthew Ahrens 	 */
349506be9802SMatthew Ahrens 	zfs_arc_max = zfs_arc_meta_limit = 256 * 1024 * 1024;
349606be9802SMatthew Ahrens 
3497*f7950bf1SMatthew Ahrens 	/*
3498*f7950bf1SMatthew Ahrens 	 * "zdb -c" uses checksum-verifying scrub i/os which are async reads.
3499*f7950bf1SMatthew Ahrens 	 * "zdb -b" uses traversal prefetch which uses async reads.
3500*f7950bf1SMatthew Ahrens 	 * For good performance, let several of them be active at once.
3501*f7950bf1SMatthew Ahrens 	 */
3502*f7950bf1SMatthew Ahrens 	zfs_vdev_async_read_max_active = 10;
3503*f7950bf1SMatthew Ahrens 
3504fa9e4066Sahrens 	kernel_init(FREAD);
3505de6628f0Sck 	g_zfs = libzfs_init();
350691ebeef5Sahrens 	ASSERT(g_zfs != NULL);
3507fa9e4066Sahrens 
3508b24ab676SJeff Bonwick 	if (dump_all)
3509b24ab676SJeff Bonwick 		verbose = MAX(verbose, 1);
3510b24ab676SJeff Bonwick 
3511fa9e4066Sahrens 	for (c = 0; c < 256; c++) {
35123f9d6ad7SLin Ling 		if (dump_all && !strchr("elAFLRSXP", c))
3513fa9e4066Sahrens 			dump_opt[c] = 1;
3514fa9e4066Sahrens 		if (dump_opt[c])
3515fa9e4066Sahrens 			dump_opt[c] += verbose;
3516fa9e4066Sahrens 	}
3517fa9e4066Sahrens 
3518feef89cfSVictor Latushkin 	aok = (dump_opt['A'] == 1) || (dump_opt['A'] > 2);
3519feef89cfSVictor Latushkin 	zfs_recover = (dump_opt['A'] > 1);
3520feef89cfSVictor Latushkin 
3521fa9e4066Sahrens 	argc -= optind;
3522fa9e4066Sahrens 	argv += optind;
3523fa9e4066Sahrens 
352407428bdfSVictor Latushkin 	if (argc < 2 && dump_opt['R'])
352507428bdfSVictor Latushkin 		usage();
3526fa9e4066Sahrens 	if (argc < 1) {
35273ad6c7f9SVictor Latushkin 		if (!dump_opt['e'] && dump_opt['C']) {
3528e829d913Sck 			dump_cachefile(spa_config_path);
3529fa9e4066Sahrens 			return (0);
3530fa9e4066Sahrens 		}
3531fa9e4066Sahrens 		usage();
3532fa9e4066Sahrens 	}
3533fa9e4066Sahrens 
3534fa9e4066Sahrens 	if (dump_opt['l']) {
3535fa9e4066Sahrens 		dump_label(argv[0]);
3536fa9e4066Sahrens 		return (0);
3537fa9e4066Sahrens 	}
3538fa9e4066Sahrens 
3539c8ee1847SVictor Latushkin 	if (dump_opt['X'] || dump_opt['F'])
3540c8ee1847SVictor Latushkin 		rewind = ZPOOL_DO_REWIND |
3541c8ee1847SVictor Latushkin 		    (dump_opt['X'] ? ZPOOL_EXTREME_REWIND : 0);
3542c8ee1847SVictor Latushkin 
3543c8ee1847SVictor Latushkin 	if (nvlist_alloc(&policy, NV_UNIQUE_NAME_TYPE, 0) != 0 ||
3544c8ee1847SVictor Latushkin 	    nvlist_add_uint64(policy, ZPOOL_REWIND_REQUEST_TXG, max_txg) != 0 ||
3545c8ee1847SVictor Latushkin 	    nvlist_add_uint32(policy, ZPOOL_REWIND_REQUEST, rewind) != 0)
3546c8ee1847SVictor Latushkin 		fatal("internal error: %s", strerror(ENOMEM));
3547c8ee1847SVictor Latushkin 
3548c5904d13Seschrock 	error = 0;
35493ad6c7f9SVictor Latushkin 	target = argv[0];
3550990b4856Slling 
35513ad6c7f9SVictor Latushkin 	if (dump_opt['e']) {
35523ad6c7f9SVictor Latushkin 		nvlist_t *cfg = NULL;
35533ad6c7f9SVictor Latushkin 		char *name = find_zpool(&target, &cfg, nsearch, searchdirs);
3554990b4856Slling 
35553ad6c7f9SVictor Latushkin 		error = ENOENT;
35563ad6c7f9SVictor Latushkin 		if (name) {
355707428bdfSVictor Latushkin 			if (dump_opt['C'] > 1) {
355807428bdfSVictor Latushkin 				(void) printf("\nConfiguration for import:\n");
355907428bdfSVictor Latushkin 				dump_nvlist(cfg, 8);
356007428bdfSVictor Latushkin 			}
3561c8ee1847SVictor Latushkin 			if (nvlist_add_nvlist(cfg,
3562468c413aSTim Haley 			    ZPOOL_REWIND_POLICY, policy) != 0) {
3563468c413aSTim Haley 				fatal("can't open '%s': %s",
3564468c413aSTim Haley 				    target, strerror(ENOMEM));
3565468c413aSTim Haley 			}
35664b964adaSGeorge Wilson 			if ((error = spa_import(name, cfg, NULL,
35674b964adaSGeorge Wilson 			    ZFS_IMPORT_MISSING_LOG)) != 0) {
35684b964adaSGeorge Wilson 				error = spa_import(name, cfg, NULL,
35694b964adaSGeorge Wilson 				    ZFS_IMPORT_VERBATIM);
35704b964adaSGeorge Wilson 			}
3571de6628f0Sck 		}
3572c5904d13Seschrock 	}
3573c5904d13Seschrock 
3574c5904d13Seschrock 	if (error == 0) {
357507428bdfSVictor Latushkin 		if (strpbrk(target, "/@") == NULL || dump_opt['R']) {
357680eb36f2SGeorge Wilson 			error = spa_open_rewind(target, &spa, FTAG, policy,
357780eb36f2SGeorge Wilson 			    NULL);
35788f18d1faSGeorge Wilson 			if (error) {
35798f18d1faSGeorge Wilson 				/*
35808f18d1faSGeorge Wilson 				 * If we're missing the log device then
35818f18d1faSGeorge Wilson 				 * try opening the pool after clearing the
35828f18d1faSGeorge Wilson 				 * log state.
35838f18d1faSGeorge Wilson 				 */
35848f18d1faSGeorge Wilson 				mutex_enter(&spa_namespace_lock);
35853ad6c7f9SVictor Latushkin 				if ((spa = spa_lookup(target)) != NULL &&
35868f18d1faSGeorge Wilson 				    spa->spa_log_state == SPA_LOG_MISSING) {
35878f18d1faSGeorge Wilson 					spa->spa_log_state = SPA_LOG_CLEAR;
35888f18d1faSGeorge Wilson 					error = 0;
35898f18d1faSGeorge Wilson 				}
35908f18d1faSGeorge Wilson 				mutex_exit(&spa_namespace_lock);
35918f18d1faSGeorge Wilson 
359280eb36f2SGeorge Wilson 				if (!error) {
359380eb36f2SGeorge Wilson 					error = spa_open_rewind(target, &spa,
359480eb36f2SGeorge Wilson 					    FTAG, policy, NULL);
359580eb36f2SGeorge Wilson 				}
35968f18d1faSGeorge Wilson 			}
359707428bdfSVictor Latushkin 		} else {
359807428bdfSVictor Latushkin 			error = dmu_objset_own(target, DMU_OST_ANY,
359907428bdfSVictor Latushkin 			    B_TRUE, FTAG, &os);
3600c5904d13Seschrock 		}
3601fa9e4066Sahrens 	}
360280eb36f2SGeorge Wilson 	nvlist_free(policy);
360380eb36f2SGeorge Wilson 
3604fa9e4066Sahrens 	if (error)
36053ad6c7f9SVictor Latushkin 		fatal("can't open '%s': %s", target, strerror(error));
3606fa9e4066Sahrens 
3607fa9e4066Sahrens 	argv++;
360807428bdfSVictor Latushkin 	argc--;
360907428bdfSVictor Latushkin 	if (!dump_opt['R']) {
361007428bdfSVictor Latushkin 		if (argc > 0) {
361107428bdfSVictor Latushkin 			zopt_objects = argc;
361207428bdfSVictor Latushkin 			zopt_object = calloc(zopt_objects, sizeof (uint64_t));
361307428bdfSVictor Latushkin 			for (i = 0; i < zopt_objects; i++) {
361407428bdfSVictor Latushkin 				errno = 0;
361507428bdfSVictor Latushkin 				zopt_object[i] = strtoull(argv[i], NULL, 0);
361607428bdfSVictor Latushkin 				if (zopt_object[i] == 0 && errno != 0)
361787219db7SVictor Latushkin 					fatal("bad number %s: %s",
361807428bdfSVictor Latushkin 					    argv[i], strerror(errno));
361907428bdfSVictor Latushkin 			}
3620fa9e4066Sahrens 		}
3621e690fb27SChristopher Siden 		if (os != NULL) {
3622e690fb27SChristopher Siden 			dump_dir(os);
3623e690fb27SChristopher Siden 		} else if (zopt_objects > 0 && !dump_opt['m']) {
3624e690fb27SChristopher Siden 			dump_dir(spa->spa_meta_objset);
3625e690fb27SChristopher Siden 		} else {
3626e690fb27SChristopher Siden 			dump_zpool(spa);
3627e690fb27SChristopher Siden 		}
3628fa9e4066Sahrens 	} else {
362907428bdfSVictor Latushkin 		flagbits['b'] = ZDB_FLAG_PRINT_BLKPTR;
363007428bdfSVictor Latushkin 		flagbits['c'] = ZDB_FLAG_CHECKSUM;
363107428bdfSVictor Latushkin 		flagbits['d'] = ZDB_FLAG_DECOMPRESS;
363207428bdfSVictor Latushkin 		flagbits['e'] = ZDB_FLAG_BSWAP;
363307428bdfSVictor Latushkin 		flagbits['g'] = ZDB_FLAG_GBH;
363407428bdfSVictor Latushkin 		flagbits['i'] = ZDB_FLAG_INDIRECT;
363507428bdfSVictor Latushkin 		flagbits['p'] = ZDB_FLAG_PHYS;
363607428bdfSVictor Latushkin 		flagbits['r'] = ZDB_FLAG_RAW;
363707428bdfSVictor Latushkin 
363807428bdfSVictor Latushkin 		for (i = 0; i < argc; i++)
363907428bdfSVictor Latushkin 			zdb_read_block(argv[i], spa);
3640fa9e4066Sahrens 	}
3641fa9e4066Sahrens 
364207428bdfSVictor Latushkin 	(os != NULL) ? dmu_objset_disown(os, FTAG) : spa_close(spa, FTAG);
364307428bdfSVictor Latushkin 
3644e0d35c44Smarks 	fuid_table_destroy();
36450a586ceaSMark Shellenbaum 	sa_loaded = B_FALSE;
3646e0d35c44Smarks 
3647de6628f0Sck 	libzfs_fini(g_zfs);
3648fa9e4066Sahrens 	kernel_fini();
3649fa9e4066Sahrens 
3650fa9e4066Sahrens 	return (0);
3651fa9e4066Sahrens }
3652