xref: /illumos-gate/usr/src/cmd/zdb/zdb.c (revision 732885fca09e11183dd0ea69aaaab5588fb7dbff)
1fa9e4066Sahrens /*
2fa9e4066Sahrens  * CDDL HEADER START
3fa9e4066Sahrens  *
4fa9e4066Sahrens  * The contents of this file are subject to the terms of the
5ea8dc4b6Seschrock  * Common Development and Distribution License (the "License").
6ea8dc4b6Seschrock  * You may not use this file except in compliance with the License.
7fa9e4066Sahrens  *
8fa9e4066Sahrens  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9fa9e4066Sahrens  * or http://www.opensolaris.org/os/licensing.
10fa9e4066Sahrens  * See the License for the specific language governing permissions
11fa9e4066Sahrens  * and limitations under the License.
12fa9e4066Sahrens  *
13fa9e4066Sahrens  * When distributing Covered Code, include this CDDL HEADER in each
14fa9e4066Sahrens  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15fa9e4066Sahrens  * If applicable, add the following below this CDDL HEADER, with the
16fa9e4066Sahrens  * fields enclosed by brackets "[]" replaced with your own identifying
17fa9e4066Sahrens  * information: Portions Copyright [yyyy] [name of copyright owner]
18fa9e4066Sahrens  *
19fa9e4066Sahrens  * CDDL HEADER END
20fa9e4066Sahrens  */
21ad135b5dSChristopher Siden 
22fa9e4066Sahrens /*
238f2529deSMark Shellenbaum  * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
247802d7bfSMatthew Ahrens  * Copyright (c) 2011, 2014 by Delphix. All rights reserved.
25fa9e4066Sahrens  */
26fa9e4066Sahrens 
27fa9e4066Sahrens #include <stdio.h>
28490d05b9SMatthew Ahrens #include <unistd.h>
29004388ebScasper #include <stdio_ext.h>
30fa9e4066Sahrens #include <stdlib.h>
3144cd46caSbillm #include <ctype.h>
32fa9e4066Sahrens #include <sys/zfs_context.h>
33fa9e4066Sahrens #include <sys/spa.h>
34fa9e4066Sahrens #include <sys/spa_impl.h>
35fa9e4066Sahrens #include <sys/dmu.h>
36fa9e4066Sahrens #include <sys/zap.h>
37fa9e4066Sahrens #include <sys/fs/zfs.h>
38fa9e4066Sahrens #include <sys/zfs_znode.h>
390a586ceaSMark Shellenbaum #include <sys/zfs_sa.h>
400a586ceaSMark Shellenbaum #include <sys/sa.h>
410a586ceaSMark Shellenbaum #include <sys/sa_impl.h>
42fa9e4066Sahrens #include <sys/vdev.h>
43fa9e4066Sahrens #include <sys/vdev_impl.h>
44fa9e4066Sahrens #include <sys/metaslab_impl.h>
45fa9e4066Sahrens #include <sys/dmu_objset.h>
46fa9e4066Sahrens #include <sys/dsl_dir.h>
47fa9e4066Sahrens #include <sys/dsl_dataset.h>
48fa9e4066Sahrens #include <sys/dsl_pool.h>
49fa9e4066Sahrens #include <sys/dbuf.h>
50fa9e4066Sahrens #include <sys/zil.h>
51fa9e4066Sahrens #include <sys/zil_impl.h>
52fa9e4066Sahrens #include <sys/stat.h>
53fa9e4066Sahrens #include <sys/resource.h>
54fa9e4066Sahrens #include <sys/dmu_traverse.h>
55fa9e4066Sahrens #include <sys/zio_checksum.h>
56fa9e4066Sahrens #include <sys/zio_compress.h>
57e0d35c44Smarks #include <sys/zfs_fuid.h>
5888b7b0f2SMatthew Ahrens #include <sys/arc.h>
59b24ab676SJeff Bonwick #include <sys/ddt.h>
60ad135b5dSChristopher Siden #include <sys/zfeature.h>
614445fffbSMatthew Ahrens #include <zfs_comutil.h>
62de6628f0Sck #undef ZFS_MAXNAMELEN
63de6628f0Sck #undef verify
64de6628f0Sck #include <libzfs.h>
65fa9e4066Sahrens 
66e690fb27SChristopher Siden #define	ZDB_COMPRESS_NAME(idx) ((idx) < ZIO_COMPRESS_FUNCTIONS ?	\
67e690fb27SChristopher Siden 	zio_compress_table[(idx)].ci_name : "UNKNOWN")
68e690fb27SChristopher Siden #define	ZDB_CHECKSUM_NAME(idx) ((idx) < ZIO_CHECKSUM_FUNCTIONS ?	\
69e690fb27SChristopher Siden 	zio_checksum_table[(idx)].ci_name : "UNKNOWN")
70e690fb27SChristopher Siden #define	ZDB_OT_NAME(idx) ((idx) < DMU_OT_NUMTYPES ?	\
71e690fb27SChristopher Siden 	dmu_ot[(idx)].ot_name : DMU_OT_IS_VALID(idx) ?	\
72e690fb27SChristopher Siden 	dmu_ot_byteswap[DMU_OT_BYTESWAP(idx)].ob_name : "UNKNOWN")
73e690fb27SChristopher Siden #define	ZDB_OT_TYPE(idx) ((idx) < DMU_OT_NUMTYPES ? (idx) :		\
74e690fb27SChristopher Siden 	(((idx) == DMU_OTN_ZAP_DATA || (idx) == DMU_OTN_ZAP_METADATA) ?	\
75e690fb27SChristopher Siden 	DMU_OT_ZAP_OTHER : DMU_OT_NUMTYPES))
766de8f417SVictor Latushkin 
77feef89cfSVictor Latushkin #ifndef lint
787fd05ac4SMatthew Ahrens extern boolean_t zfs_recover;
7906be9802SMatthew Ahrens extern uint64_t zfs_arc_max, zfs_arc_meta_limit;
80f7950bf1SMatthew Ahrens extern int zfs_vdev_async_read_max_active;
81feef89cfSVictor Latushkin #else
827fd05ac4SMatthew Ahrens boolean_t zfs_recover;
8306be9802SMatthew Ahrens uint64_t zfs_arc_max, zfs_arc_meta_limit;
84f7950bf1SMatthew Ahrens int zfs_vdev_async_read_max_active;
85feef89cfSVictor Latushkin #endif
86feef89cfSVictor Latushkin 
87fa9e4066Sahrens const char cmdname[] = "zdb";
88fa9e4066Sahrens uint8_t dump_opt[256];
89fa9e4066Sahrens 
90fa9e4066Sahrens typedef void object_viewer_t(objset_t *, uint64_t, void *data, size_t size);
91fa9e4066Sahrens 
92fa9e4066Sahrens extern void dump_intent_log(zilog_t *);
93fa9e4066Sahrens uint64_t *zopt_object = NULL;
94fa9e4066Sahrens int zopt_objects = 0;
95de6628f0Sck libzfs_handle_t *g_zfs;
9606be9802SMatthew Ahrens uint64_t max_inflight = 1000;
97fa9e4066Sahrens 
98*732885fcSMatthew Ahrens static void snprintf_blkptr_compact(char *, size_t, const blkptr_t *);
99*732885fcSMatthew Ahrens 
100fa9e4066Sahrens /*
101fa9e4066Sahrens  * These libumem hooks provide a reasonable set of defaults for the allocator's
102fa9e4066Sahrens  * debugging facilities.
103fa9e4066Sahrens  */
104fa9e4066Sahrens const char *
105fa9e4066Sahrens _umem_debug_init()
106fa9e4066Sahrens {
107fa9e4066Sahrens 	return ("default,verbose"); /* $UMEM_DEBUG setting */
108fa9e4066Sahrens }
109fa9e4066Sahrens 
110fa9e4066Sahrens const char *
111fa9e4066Sahrens _umem_logging_init(void)
112fa9e4066Sahrens {
113fa9e4066Sahrens 	return ("fail,contents"); /* $UMEM_LOGGING setting */
114fa9e4066Sahrens }
115fa9e4066Sahrens 
116fa9e4066Sahrens static void
117fa9e4066Sahrens usage(void)
118fa9e4066Sahrens {
119fa9e4066Sahrens 	(void) fprintf(stderr,
1202e4c9986SGeorge Wilson 	    "Usage: %s [-CumMdibcsDvhLXFPA] [-t txg] [-e [-p path...]] "
1212e4c9986SGeorge Wilson 	    "[-U config] [-I inflight I/Os] [-x dumpdir] poolname [object...]\n"
12231d7e8faSGeorge Wilson 	    "       %s [-divPA] [-e -p path...] [-U config] dataset "
12331d7e8faSGeorge Wilson 	    "[object...]\n"
1242e4c9986SGeorge Wilson 	    "       %s -mM [-LXFPA] [-t txg] [-e [-p path...]] [-U config] "
12590e894e2SRichard Lowe 	    "poolname [vdev [metaslab...]]\n"
12690e894e2SRichard Lowe 	    "       %s -R [-A] [-e [-p path...]] poolname "
12790e894e2SRichard Lowe 	    "vdev:offset:size[:flags]\n"
12831d7e8faSGeorge Wilson 	    "       %s -S [-PA] [-e [-p path...]] [-U config] poolname\n"
12990e894e2SRichard Lowe 	    "       %s -l [-uA] device\n"
13090e894e2SRichard Lowe 	    "       %s -C [-A] [-U config]\n\n",
131bbfd46c4SJeff Bonwick 	    cmdname, cmdname, cmdname, cmdname, cmdname, cmdname, cmdname);
1323ad6c7f9SVictor Latushkin 
1333ad6c7f9SVictor Latushkin 	(void) fprintf(stderr, "    Dataset name must include at least one "
1343ad6c7f9SVictor Latushkin 	    "separator character '/' or '@'\n");
1353ad6c7f9SVictor Latushkin 	(void) fprintf(stderr, "    If dataset name is specified, only that "
1363ad6c7f9SVictor Latushkin 	    "dataset is dumped\n");
1373ad6c7f9SVictor Latushkin 	(void) fprintf(stderr, "    If object numbers are specified, only "
1383ad6c7f9SVictor Latushkin 	    "those objects are dumped\n\n");
1393ad6c7f9SVictor Latushkin 	(void) fprintf(stderr, "    Options to control amount of output:\n");
1403ad6c7f9SVictor Latushkin 	(void) fprintf(stderr, "        -u uberblock\n");
1413ad6c7f9SVictor Latushkin 	(void) fprintf(stderr, "        -d dataset(s)\n");
1423ad6c7f9SVictor Latushkin 	(void) fprintf(stderr, "        -i intent logs\n");
14307428bdfSVictor Latushkin 	(void) fprintf(stderr, "        -C config (or cachefile if alone)\n");
1448f18d1faSGeorge Wilson 	(void) fprintf(stderr, "        -h pool history\n");
1453ad6c7f9SVictor Latushkin 	(void) fprintf(stderr, "        -b block statistics\n");
1463ad6c7f9SVictor Latushkin 	(void) fprintf(stderr, "        -m metaslabs\n");
1472e4c9986SGeorge Wilson 	(void) fprintf(stderr, "        -M metaslab groups\n");
1483ad6c7f9SVictor Latushkin 	(void) fprintf(stderr, "        -c checksum all metadata (twice for "
1496365109dSVictor Latushkin 	    "all data) blocks\n");
1503ad6c7f9SVictor Latushkin 	(void) fprintf(stderr, "        -s report stats on zdb's I/O\n");
151f0ba89beSJeff Bonwick 	(void) fprintf(stderr, "        -D dedup statistics\n");
152b24ab676SJeff Bonwick 	(void) fprintf(stderr, "        -S simulate dedup to measure effect\n");
1533ad6c7f9SVictor Latushkin 	(void) fprintf(stderr, "        -v verbose (applies to all others)\n");
154fa9e4066Sahrens 	(void) fprintf(stderr, "        -l dump label contents\n");
15582a0a985SVictor Latushkin 	(void) fprintf(stderr, "        -L disable leak tracking (do not "
15682a0a985SVictor Latushkin 	    "load spacemaps)\n");
157d41e7643Sek 	(void) fprintf(stderr, "        -R read and display block from a "
1583ad6c7f9SVictor Latushkin 	    "device\n\n");
1593ad6c7f9SVictor Latushkin 	(void) fprintf(stderr, "    Below options are intended for use "
160df15e419SMatthew Ahrens 	    "with other options:\n");
161feef89cfSVictor Latushkin 	(void) fprintf(stderr, "        -A ignore assertions (-A), enable "
162feef89cfSVictor Latushkin 	    "panic recovery (-AA) or both (-AAA)\n");
163c8ee1847SVictor Latushkin 	(void) fprintf(stderr, "        -F attempt automatic rewind within "
164c8ee1847SVictor Latushkin 	    "safe range of transaction groups\n");
1653ad6c7f9SVictor Latushkin 	(void) fprintf(stderr, "        -U <cachefile_path> -- use alternate "
1663ad6c7f9SVictor Latushkin 	    "cachefile\n");
167c8ee1847SVictor Latushkin 	(void) fprintf(stderr, "        -X attempt extreme rewind (does not "
168c8ee1847SVictor Latushkin 	    "work with dataset)\n");
1693ad6c7f9SVictor Latushkin 	(void) fprintf(stderr, "        -e pool is exported/destroyed/"
1703ad6c7f9SVictor Latushkin 	    "has altroot/not in a cachefile\n");
1713ad6c7f9SVictor Latushkin 	(void) fprintf(stderr, "        -p <path> -- use one or more with "
1723ad6c7f9SVictor Latushkin 	    "-e to specify path to vdev dir\n");
173df15e419SMatthew Ahrens 	(void) fprintf(stderr, "        -x <dumpdir> -- "
174df15e419SMatthew Ahrens 	    "dump all read blocks into specified directory\n");
17590e894e2SRichard Lowe 	(void) fprintf(stderr, "        -P print numbers in parseable form\n");
1763ad6c7f9SVictor Latushkin 	(void) fprintf(stderr, "        -t <txg> -- highest txg to use when "
1772e551927SVictor Latushkin 	    "searching for uberblocks\n");
1782e4c9986SGeorge Wilson 	(void) fprintf(stderr, "        -I <number of inflight I/Os> -- "
179df15e419SMatthew Ahrens 	    "specify the maximum number of "
180df15e419SMatthew Ahrens 	    "checksumming I/Os [default is 200]\n");
181fa9e4066Sahrens 	(void) fprintf(stderr, "Specify an option more than once (e.g. -bb) "
182fa9e4066Sahrens 	    "to make only that option verbose\n");
183fa9e4066Sahrens 	(void) fprintf(stderr, "Default is to dump everything non-verbosely\n");
184fa9e4066Sahrens 	exit(1);
185fa9e4066Sahrens }
186fa9e4066Sahrens 
187ccba0801SRich Morris /*
188ccba0801SRich Morris  * Called for usage errors that are discovered after a call to spa_open(),
189ccba0801SRich Morris  * dmu_bonus_hold(), or pool_match().  abort() is called for other errors.
190ccba0801SRich Morris  */
191ccba0801SRich Morris 
192fa9e4066Sahrens static void
193fa9e4066Sahrens fatal(const char *fmt, ...)
194fa9e4066Sahrens {
195fa9e4066Sahrens 	va_list ap;
196fa9e4066Sahrens 
197fa9e4066Sahrens 	va_start(ap, fmt);
198fa9e4066Sahrens 	(void) fprintf(stderr, "%s: ", cmdname);
199fa9e4066Sahrens 	(void) vfprintf(stderr, fmt, ap);
200fa9e4066Sahrens 	va_end(ap);
201fa9e4066Sahrens 	(void) fprintf(stderr, "\n");
202fa9e4066Sahrens 
203ccba0801SRich Morris 	exit(1);
204fa9e4066Sahrens }
205fa9e4066Sahrens 
206fa9e4066Sahrens /* ARGSUSED */
207fa9e4066Sahrens static void
208fa9e4066Sahrens dump_packed_nvlist(objset_t *os, uint64_t object, void *data, size_t size)
209fa9e4066Sahrens {
210fa9e4066Sahrens 	nvlist_t *nv;
211fa9e4066Sahrens 	size_t nvsize = *(uint64_t *)data;
212fa9e4066Sahrens 	char *packed = umem_alloc(nvsize, UMEM_NOFAIL);
213fa9e4066Sahrens 
2147bfdf011SNeil Perrin 	VERIFY(0 == dmu_read(os, object, 0, nvsize, packed, DMU_READ_PREFETCH));
215fa9e4066Sahrens 
216fa9e4066Sahrens 	VERIFY(nvlist_unpack(packed, nvsize, &nv, 0) == 0);
217fa9e4066Sahrens 
218fa9e4066Sahrens 	umem_free(packed, nvsize);
219fa9e4066Sahrens 
220fa9e4066Sahrens 	dump_nvlist(nv, 8);
221fa9e4066Sahrens 
222fa9e4066Sahrens 	nvlist_free(nv);
223fa9e4066Sahrens }
224fa9e4066Sahrens 
2254445fffbSMatthew Ahrens /* ARGSUSED */
2264445fffbSMatthew Ahrens static void
2274445fffbSMatthew Ahrens dump_history_offsets(objset_t *os, uint64_t object, void *data, size_t size)
2284445fffbSMatthew Ahrens {
2294445fffbSMatthew Ahrens 	spa_history_phys_t *shp = data;
2304445fffbSMatthew Ahrens 
2314445fffbSMatthew Ahrens 	if (shp == NULL)
2324445fffbSMatthew Ahrens 		return;
2334445fffbSMatthew Ahrens 
2344445fffbSMatthew Ahrens 	(void) printf("\t\tpool_create_len = %llu\n",
2354445fffbSMatthew Ahrens 	    (u_longlong_t)shp->sh_pool_create_len);
2364445fffbSMatthew Ahrens 	(void) printf("\t\tphys_max_off = %llu\n",
2374445fffbSMatthew Ahrens 	    (u_longlong_t)shp->sh_phys_max_off);
2384445fffbSMatthew Ahrens 	(void) printf("\t\tbof = %llu\n",
2394445fffbSMatthew Ahrens 	    (u_longlong_t)shp->sh_bof);
2404445fffbSMatthew Ahrens 	(void) printf("\t\teof = %llu\n",
2414445fffbSMatthew Ahrens 	    (u_longlong_t)shp->sh_eof);
2424445fffbSMatthew Ahrens 	(void) printf("\t\trecords_lost = %llu\n",
2434445fffbSMatthew Ahrens 	    (u_longlong_t)shp->sh_records_lost);
2444445fffbSMatthew Ahrens }
2454445fffbSMatthew Ahrens 
2463f9d6ad7SLin Ling static void
2473f9d6ad7SLin Ling zdb_nicenum(uint64_t num, char *buf)
2483f9d6ad7SLin Ling {
2493f9d6ad7SLin Ling 	if (dump_opt['P'])
2503f9d6ad7SLin Ling 		(void) sprintf(buf, "%llu", (longlong_t)num);
2513f9d6ad7SLin Ling 	else
2523f9d6ad7SLin Ling 		nicenum(num, buf);
2533f9d6ad7SLin Ling }
2543f9d6ad7SLin Ling 
255490d05b9SMatthew Ahrens const char histo_stars[] = "****************************************";
256490d05b9SMatthew Ahrens const int histo_width = sizeof (histo_stars) - 1;
257fa9e4066Sahrens 
258fa9e4066Sahrens static void
2590713e232SGeorge Wilson dump_histogram(const uint64_t *histo, int size, int offset)
260fa9e4066Sahrens {
261fa9e4066Sahrens 	int i;
262490d05b9SMatthew Ahrens 	int minidx = size - 1;
263fa9e4066Sahrens 	int maxidx = 0;
264fa9e4066Sahrens 	uint64_t max = 0;
265fa9e4066Sahrens 
266490d05b9SMatthew Ahrens 	for (i = 0; i < size; i++) {
267fa9e4066Sahrens 		if (histo[i] > max)
268fa9e4066Sahrens 			max = histo[i];
269fa9e4066Sahrens 		if (histo[i] > 0 && i > maxidx)
270fa9e4066Sahrens 			maxidx = i;
271fa9e4066Sahrens 		if (histo[i] > 0 && i < minidx)
272fa9e4066Sahrens 			minidx = i;
273fa9e4066Sahrens 	}
274fa9e4066Sahrens 
275490d05b9SMatthew Ahrens 	if (max < histo_width)
276490d05b9SMatthew Ahrens 		max = histo_width;
277fa9e4066Sahrens 
278490d05b9SMatthew Ahrens 	for (i = minidx; i <= maxidx; i++) {
279490d05b9SMatthew Ahrens 		(void) printf("\t\t\t%3u: %6llu %s\n",
2800713e232SGeorge Wilson 		    i + offset, (u_longlong_t)histo[i],
281490d05b9SMatthew Ahrens 		    &histo_stars[(max - histo[i]) * histo_width / max]);
282490d05b9SMatthew Ahrens 	}
283fa9e4066Sahrens }
284fa9e4066Sahrens 
285fa9e4066Sahrens static void
286fa9e4066Sahrens dump_zap_stats(objset_t *os, uint64_t object)
287fa9e4066Sahrens {
288fa9e4066Sahrens 	int error;
289fa9e4066Sahrens 	zap_stats_t zs;
290fa9e4066Sahrens 
291fa9e4066Sahrens 	error = zap_get_stats(os, object, &zs);
292fa9e4066Sahrens 	if (error)
293fa9e4066Sahrens 		return;
294fa9e4066Sahrens 
295fa9e4066Sahrens 	if (zs.zs_ptrtbl_len == 0) {
296fa9e4066Sahrens 		ASSERT(zs.zs_num_blocks == 1);
297fa9e4066Sahrens 		(void) printf("\tmicrozap: %llu bytes, %llu entries\n",
298fa9e4066Sahrens 		    (u_longlong_t)zs.zs_blocksize,
299fa9e4066Sahrens 		    (u_longlong_t)zs.zs_num_entries);
300fa9e4066Sahrens 		return;
301fa9e4066Sahrens 	}
302fa9e4066Sahrens 
303fa9e4066Sahrens 	(void) printf("\tFat ZAP stats:\n");
3048248818dSnd 
3058248818dSnd 	(void) printf("\t\tPointer table:\n");
3068248818dSnd 	(void) printf("\t\t\t%llu elements\n",
307fa9e4066Sahrens 	    (u_longlong_t)zs.zs_ptrtbl_len);
3088248818dSnd 	(void) printf("\t\t\tzt_blk: %llu\n",
3098248818dSnd 	    (u_longlong_t)zs.zs_ptrtbl_zt_blk);
3108248818dSnd 	(void) printf("\t\t\tzt_numblks: %llu\n",
3118248818dSnd 	    (u_longlong_t)zs.zs_ptrtbl_zt_numblks);
3128248818dSnd 	(void) printf("\t\t\tzt_shift: %llu\n",
3138248818dSnd 	    (u_longlong_t)zs.zs_ptrtbl_zt_shift);
3148248818dSnd 	(void) printf("\t\t\tzt_blks_copied: %llu\n",
3158248818dSnd 	    (u_longlong_t)zs.zs_ptrtbl_blks_copied);
3168248818dSnd 	(void) printf("\t\t\tzt_nextblk: %llu\n",
3178248818dSnd 	    (u_longlong_t)zs.zs_ptrtbl_nextblk);
3188248818dSnd 
319fa9e4066Sahrens 	(void) printf("\t\tZAP entries: %llu\n",
320fa9e4066Sahrens 	    (u_longlong_t)zs.zs_num_entries);
321fa9e4066Sahrens 	(void) printf("\t\tLeaf blocks: %llu\n",
322fa9e4066Sahrens 	    (u_longlong_t)zs.zs_num_leafs);
323fa9e4066Sahrens 	(void) printf("\t\tTotal blocks: %llu\n",
324fa9e4066Sahrens 	    (u_longlong_t)zs.zs_num_blocks);
3258248818dSnd 	(void) printf("\t\tzap_block_type: 0x%llx\n",
3268248818dSnd 	    (u_longlong_t)zs.zs_block_type);
3278248818dSnd 	(void) printf("\t\tzap_magic: 0x%llx\n",
3288248818dSnd 	    (u_longlong_t)zs.zs_magic);
3298248818dSnd 	(void) printf("\t\tzap_salt: 0x%llx\n",
3308248818dSnd 	    (u_longlong_t)zs.zs_salt);
331fa9e4066Sahrens 
332fa9e4066Sahrens 	(void) printf("\t\tLeafs with 2^n pointers:\n");
3330713e232SGeorge Wilson 	dump_histogram(zs.zs_leafs_with_2n_pointers, ZAP_HISTOGRAM_SIZE, 0);
334fa9e4066Sahrens 
335fa9e4066Sahrens 	(void) printf("\t\tBlocks with n*5 entries:\n");
3360713e232SGeorge Wilson 	dump_histogram(zs.zs_blocks_with_n5_entries, ZAP_HISTOGRAM_SIZE, 0);
337fa9e4066Sahrens 
338fa9e4066Sahrens 	(void) printf("\t\tBlocks n/10 full:\n");
3390713e232SGeorge Wilson 	dump_histogram(zs.zs_blocks_n_tenths_full, ZAP_HISTOGRAM_SIZE, 0);
340fa9e4066Sahrens 
341fa9e4066Sahrens 	(void) printf("\t\tEntries with n chunks:\n");
3420713e232SGeorge Wilson 	dump_histogram(zs.zs_entries_using_n_chunks, ZAP_HISTOGRAM_SIZE, 0);
343fa9e4066Sahrens 
344fa9e4066Sahrens 	(void) printf("\t\tBuckets with n entries:\n");
3450713e232SGeorge Wilson 	dump_histogram(zs.zs_buckets_with_n_entries, ZAP_HISTOGRAM_SIZE, 0);
346fa9e4066Sahrens }
347fa9e4066Sahrens 
348fa9e4066Sahrens /*ARGSUSED*/
349fa9e4066Sahrens static void
350fa9e4066Sahrens dump_none(objset_t *os, uint64_t object, void *data, size_t size)
351fa9e4066Sahrens {
352fa9e4066Sahrens }
353fa9e4066Sahrens 
3546de8f417SVictor Latushkin /*ARGSUSED*/
3556de8f417SVictor Latushkin static void
3566de8f417SVictor Latushkin dump_unknown(objset_t *os, uint64_t object, void *data, size_t size)
3576de8f417SVictor Latushkin {
3586de8f417SVictor Latushkin 	(void) printf("\tUNKNOWN OBJECT TYPE\n");
3596de8f417SVictor Latushkin }
3606de8f417SVictor Latushkin 
361fa9e4066Sahrens /*ARGSUSED*/
362fa9e4066Sahrens void
363fa9e4066Sahrens dump_uint8(objset_t *os, uint64_t object, void *data, size_t size)
364fa9e4066Sahrens {
365fa9e4066Sahrens }
366fa9e4066Sahrens 
367fa9e4066Sahrens /*ARGSUSED*/
368fa9e4066Sahrens static void
369fa9e4066Sahrens dump_uint64(objset_t *os, uint64_t object, void *data, size_t size)
370fa9e4066Sahrens {
371fa9e4066Sahrens }
372fa9e4066Sahrens 
373fa9e4066Sahrens /*ARGSUSED*/
374fa9e4066Sahrens static void
375fa9e4066Sahrens dump_zap(objset_t *os, uint64_t object, void *data, size_t size)
376fa9e4066Sahrens {
377fa9e4066Sahrens 	zap_cursor_t zc;
378fa9e4066Sahrens 	zap_attribute_t attr;
379fa9e4066Sahrens 	void *prop;
380fa9e4066Sahrens 	int i;
381fa9e4066Sahrens 
382fa9e4066Sahrens 	dump_zap_stats(os, object);
383fa9e4066Sahrens 	(void) printf("\n");
384fa9e4066Sahrens 
385fa9e4066Sahrens 	for (zap_cursor_init(&zc, os, object);
386fa9e4066Sahrens 	    zap_cursor_retrieve(&zc, &attr) == 0;
387fa9e4066Sahrens 	    zap_cursor_advance(&zc)) {
388fa9e4066Sahrens 		(void) printf("\t\t%s = ", attr.za_name);
389fa9e4066Sahrens 		if (attr.za_num_integers == 0) {
390fa9e4066Sahrens 			(void) printf("\n");
391fa9e4066Sahrens 			continue;
392fa9e4066Sahrens 		}
393fa9e4066Sahrens 		prop = umem_zalloc(attr.za_num_integers *
394fa9e4066Sahrens 		    attr.za_integer_length, UMEM_NOFAIL);
395fa9e4066Sahrens 		(void) zap_lookup(os, object, attr.za_name,
396fa9e4066Sahrens 		    attr.za_integer_length, attr.za_num_integers, prop);
397fa9e4066Sahrens 		if (attr.za_integer_length == 1) {
398fa9e4066Sahrens 			(void) printf("%s", (char *)prop);
399fa9e4066Sahrens 		} else {
400fa9e4066Sahrens 			for (i = 0; i < attr.za_num_integers; i++) {
401fa9e4066Sahrens 				switch (attr.za_integer_length) {
402fa9e4066Sahrens 				case 2:
403fa9e4066Sahrens 					(void) printf("%u ",
404fa9e4066Sahrens 					    ((uint16_t *)prop)[i]);
405fa9e4066Sahrens 					break;
406fa9e4066Sahrens 				case 4:
407fa9e4066Sahrens 					(void) printf("%u ",
408fa9e4066Sahrens 					    ((uint32_t *)prop)[i]);
409fa9e4066Sahrens 					break;
410fa9e4066Sahrens 				case 8:
411fa9e4066Sahrens 					(void) printf("%lld ",
412fa9e4066Sahrens 					    (u_longlong_t)((int64_t *)prop)[i]);
413fa9e4066Sahrens 					break;
414fa9e4066Sahrens 				}
415fa9e4066Sahrens 			}
416fa9e4066Sahrens 		}
417fa9e4066Sahrens 		(void) printf("\n");
418fa9e4066Sahrens 		umem_free(prop, attr.za_num_integers * attr.za_integer_length);
419fa9e4066Sahrens 	}
42087e5029aSahrens 	zap_cursor_fini(&zc);
421fa9e4066Sahrens }
422fa9e4066Sahrens 
423*732885fcSMatthew Ahrens static void
424*732885fcSMatthew Ahrens dump_bpobj(objset_t *os, uint64_t object, void *data, size_t size)
425*732885fcSMatthew Ahrens {
426*732885fcSMatthew Ahrens 	bpobj_phys_t *bpop = data;
427*732885fcSMatthew Ahrens 	char bytes[32], comp[32], uncomp[32];
428*732885fcSMatthew Ahrens 
429*732885fcSMatthew Ahrens 	if (bpop == NULL)
430*732885fcSMatthew Ahrens 		return;
431*732885fcSMatthew Ahrens 
432*732885fcSMatthew Ahrens 	zdb_nicenum(bpop->bpo_bytes, bytes);
433*732885fcSMatthew Ahrens 	zdb_nicenum(bpop->bpo_comp, comp);
434*732885fcSMatthew Ahrens 	zdb_nicenum(bpop->bpo_uncomp, uncomp);
435*732885fcSMatthew Ahrens 
436*732885fcSMatthew Ahrens 	(void) printf("\t\tnum_blkptrs = %llu\n",
437*732885fcSMatthew Ahrens 	    (u_longlong_t)bpop->bpo_num_blkptrs);
438*732885fcSMatthew Ahrens 	(void) printf("\t\tbytes = %s\n", bytes);
439*732885fcSMatthew Ahrens 	if (size >= BPOBJ_SIZE_V1) {
440*732885fcSMatthew Ahrens 		(void) printf("\t\tcomp = %s\n", comp);
441*732885fcSMatthew Ahrens 		(void) printf("\t\tuncomp = %s\n", uncomp);
442*732885fcSMatthew Ahrens 	}
443*732885fcSMatthew Ahrens 	if (size >= sizeof (*bpop)) {
444*732885fcSMatthew Ahrens 		(void) printf("\t\tsubobjs = %llu\n",
445*732885fcSMatthew Ahrens 		    (u_longlong_t)bpop->bpo_subobjs);
446*732885fcSMatthew Ahrens 		(void) printf("\t\tnum_subobjs = %llu\n",
447*732885fcSMatthew Ahrens 		    (u_longlong_t)bpop->bpo_num_subobjs);
448*732885fcSMatthew Ahrens 	}
449*732885fcSMatthew Ahrens 
450*732885fcSMatthew Ahrens 	if (dump_opt['d'] < 5)
451*732885fcSMatthew Ahrens 		return;
452*732885fcSMatthew Ahrens 
453*732885fcSMatthew Ahrens 	for (uint64_t i = 0; i < bpop->bpo_num_blkptrs; i++) {
454*732885fcSMatthew Ahrens 		char blkbuf[BP_SPRINTF_LEN];
455*732885fcSMatthew Ahrens 		blkptr_t bp;
456*732885fcSMatthew Ahrens 
457*732885fcSMatthew Ahrens 		int err = dmu_read(os, object,
458*732885fcSMatthew Ahrens 		    i * sizeof (bp), sizeof (bp), &bp, 0);
459*732885fcSMatthew Ahrens 		if (err != 0) {
460*732885fcSMatthew Ahrens 			(void) printf("got error %u from dmu_read\n", err);
461*732885fcSMatthew Ahrens 			break;
462*732885fcSMatthew Ahrens 		}
463*732885fcSMatthew Ahrens 		snprintf_blkptr_compact(blkbuf, sizeof (blkbuf), &bp);
464*732885fcSMatthew Ahrens 		(void) printf("\t%s\n", blkbuf);
465*732885fcSMatthew Ahrens 	}
466*732885fcSMatthew Ahrens }
467*732885fcSMatthew Ahrens 
468*732885fcSMatthew Ahrens /* ARGSUSED */
469*732885fcSMatthew Ahrens static void
470*732885fcSMatthew Ahrens dump_bpobj_subobjs(objset_t *os, uint64_t object, void *data, size_t size)
471*732885fcSMatthew Ahrens {
472*732885fcSMatthew Ahrens 	dmu_object_info_t doi;
473*732885fcSMatthew Ahrens 
474*732885fcSMatthew Ahrens 	VERIFY0(dmu_object_info(os, object, &doi));
475*732885fcSMatthew Ahrens 	uint64_t *subobjs = kmem_alloc(doi.doi_max_offset, KM_SLEEP);
476*732885fcSMatthew Ahrens 
477*732885fcSMatthew Ahrens 	int err = dmu_read(os, object, 0, doi.doi_max_offset, subobjs, 0);
478*732885fcSMatthew Ahrens 	if (err != 0) {
479*732885fcSMatthew Ahrens 		(void) printf("got error %u from dmu_read\n", err);
480*732885fcSMatthew Ahrens 		kmem_free(subobjs, doi.doi_max_offset);
481*732885fcSMatthew Ahrens 		return;
482*732885fcSMatthew Ahrens 	}
483*732885fcSMatthew Ahrens 
484*732885fcSMatthew Ahrens 	int64_t last_nonzero = -1;
485*732885fcSMatthew Ahrens 	for (uint64_t i = 0; i < doi.doi_max_offset / 8; i++) {
486*732885fcSMatthew Ahrens 		if (subobjs[i] != 0)
487*732885fcSMatthew Ahrens 			last_nonzero = i;
488*732885fcSMatthew Ahrens 	}
489*732885fcSMatthew Ahrens 
490*732885fcSMatthew Ahrens 	for (int64_t i = 0; i <= last_nonzero; i++) {
491*732885fcSMatthew Ahrens 		(void) printf("\t%llu\n", (longlong_t)subobjs[i]);
492*732885fcSMatthew Ahrens 	}
493*732885fcSMatthew Ahrens 	kmem_free(subobjs, doi.doi_max_offset);
494*732885fcSMatthew Ahrens }
495*732885fcSMatthew Ahrens 
496486ae710SMatthew Ahrens /*ARGSUSED*/
497486ae710SMatthew Ahrens static void
498486ae710SMatthew Ahrens dump_ddt_zap(objset_t *os, uint64_t object, void *data, size_t size)
499486ae710SMatthew Ahrens {
500486ae710SMatthew Ahrens 	dump_zap_stats(os, object);
501486ae710SMatthew Ahrens 	/* contents are printed elsewhere, properly decoded */
502486ae710SMatthew Ahrens }
503486ae710SMatthew Ahrens 
5040a586ceaSMark Shellenbaum /*ARGSUSED*/
5050a586ceaSMark Shellenbaum static void
5060a586ceaSMark Shellenbaum dump_sa_attrs(objset_t *os, uint64_t object, void *data, size_t size)
5070a586ceaSMark Shellenbaum {
5080a586ceaSMark Shellenbaum 	zap_cursor_t zc;
5090a586ceaSMark Shellenbaum 	zap_attribute_t attr;
5100a586ceaSMark Shellenbaum 
5110a586ceaSMark Shellenbaum 	dump_zap_stats(os, object);
5120a586ceaSMark Shellenbaum 	(void) printf("\n");
5130a586ceaSMark Shellenbaum 
5140a586ceaSMark Shellenbaum 	for (zap_cursor_init(&zc, os, object);
5150a586ceaSMark Shellenbaum 	    zap_cursor_retrieve(&zc, &attr) == 0;
5160a586ceaSMark Shellenbaum 	    zap_cursor_advance(&zc)) {
5170a586ceaSMark Shellenbaum 		(void) printf("\t\t%s = ", attr.za_name);
5180a586ceaSMark Shellenbaum 		if (attr.za_num_integers == 0) {
5190a586ceaSMark Shellenbaum 			(void) printf("\n");
5200a586ceaSMark Shellenbaum 			continue;
5210a586ceaSMark Shellenbaum 		}
5220a586ceaSMark Shellenbaum 		(void) printf(" %llx : [%d:%d:%d]\n",
5230a586ceaSMark Shellenbaum 		    (u_longlong_t)attr.za_first_integer,
5240a586ceaSMark Shellenbaum 		    (int)ATTR_LENGTH(attr.za_first_integer),
5250a586ceaSMark Shellenbaum 		    (int)ATTR_BSWAP(attr.za_first_integer),
5260a586ceaSMark Shellenbaum 		    (int)ATTR_NUM(attr.za_first_integer));
5270a586ceaSMark Shellenbaum 	}
5280a586ceaSMark Shellenbaum 	zap_cursor_fini(&zc);
5290a586ceaSMark Shellenbaum }
5300a586ceaSMark Shellenbaum 
5310a586ceaSMark Shellenbaum /*ARGSUSED*/
5320a586ceaSMark Shellenbaum static void
5330a586ceaSMark Shellenbaum dump_sa_layouts(objset_t *os, uint64_t object, void *data, size_t size)
5340a586ceaSMark Shellenbaum {
5350a586ceaSMark Shellenbaum 	zap_cursor_t zc;
5360a586ceaSMark Shellenbaum 	zap_attribute_t attr;
5370a586ceaSMark Shellenbaum 	uint16_t *layout_attrs;
5380a586ceaSMark Shellenbaum 	int i;
5390a586ceaSMark Shellenbaum 
5400a586ceaSMark Shellenbaum 	dump_zap_stats(os, object);
5410a586ceaSMark Shellenbaum 	(void) printf("\n");
5420a586ceaSMark Shellenbaum 
5430a586ceaSMark Shellenbaum 	for (zap_cursor_init(&zc, os, object);
5440a586ceaSMark Shellenbaum 	    zap_cursor_retrieve(&zc, &attr) == 0;
5450a586ceaSMark Shellenbaum 	    zap_cursor_advance(&zc)) {
5460a586ceaSMark Shellenbaum 		(void) printf("\t\t%s = [", attr.za_name);
5470a586ceaSMark Shellenbaum 		if (attr.za_num_integers == 0) {
5480a586ceaSMark Shellenbaum 			(void) printf("\n");
5490a586ceaSMark Shellenbaum 			continue;
5500a586ceaSMark Shellenbaum 		}
5510a586ceaSMark Shellenbaum 
5520a586ceaSMark Shellenbaum 		VERIFY(attr.za_integer_length == 2);
5530a586ceaSMark Shellenbaum 		layout_attrs = umem_zalloc(attr.za_num_integers *
5540a586ceaSMark Shellenbaum 		    attr.za_integer_length, UMEM_NOFAIL);
5550a586ceaSMark Shellenbaum 
5560a586ceaSMark Shellenbaum 		VERIFY(zap_lookup(os, object, attr.za_name,
5570a586ceaSMark Shellenbaum 		    attr.za_integer_length,
5580a586ceaSMark Shellenbaum 		    attr.za_num_integers, layout_attrs) == 0);
5590a586ceaSMark Shellenbaum 
5600a586ceaSMark Shellenbaum 		for (i = 0; i != attr.za_num_integers; i++)
5610a586ceaSMark Shellenbaum 			(void) printf(" %d ", (int)layout_attrs[i]);
5620a586ceaSMark Shellenbaum 		(void) printf("]\n");
5630a586ceaSMark Shellenbaum 		umem_free(layout_attrs,
5640a586ceaSMark Shellenbaum 		    attr.za_num_integers * attr.za_integer_length);
5650a586ceaSMark Shellenbaum 	}
5660a586ceaSMark Shellenbaum 	zap_cursor_fini(&zc);
5670a586ceaSMark Shellenbaum }
5680a586ceaSMark Shellenbaum 
569e7437265Sahrens /*ARGSUSED*/
570e7437265Sahrens static void
571e7437265Sahrens dump_zpldir(objset_t *os, uint64_t object, void *data, size_t size)
572e7437265Sahrens {
573e7437265Sahrens 	zap_cursor_t zc;
574e7437265Sahrens 	zap_attribute_t attr;
575e7437265Sahrens 	const char *typenames[] = {
576e7437265Sahrens 		/* 0 */ "not specified",
577e7437265Sahrens 		/* 1 */ "FIFO",
578e7437265Sahrens 		/* 2 */ "Character Device",
579e7437265Sahrens 		/* 3 */ "3 (invalid)",
580e7437265Sahrens 		/* 4 */ "Directory",
581e7437265Sahrens 		/* 5 */ "5 (invalid)",
582e7437265Sahrens 		/* 6 */ "Block Device",
583e7437265Sahrens 		/* 7 */ "7 (invalid)",
584e7437265Sahrens 		/* 8 */ "Regular File",
585e7437265Sahrens 		/* 9 */ "9 (invalid)",
586e7437265Sahrens 		/* 10 */ "Symbolic Link",
587e7437265Sahrens 		/* 11 */ "11 (invalid)",
588e7437265Sahrens 		/* 12 */ "Socket",
589e7437265Sahrens 		/* 13 */ "Door",
590e7437265Sahrens 		/* 14 */ "Event Port",
591e7437265Sahrens 		/* 15 */ "15 (invalid)",
592e7437265Sahrens 	};
593e7437265Sahrens 
594e7437265Sahrens 	dump_zap_stats(os, object);
595e7437265Sahrens 	(void) printf("\n");
596e7437265Sahrens 
597e7437265Sahrens 	for (zap_cursor_init(&zc, os, object);
598e7437265Sahrens 	    zap_cursor_retrieve(&zc, &attr) == 0;
599e7437265Sahrens 	    zap_cursor_advance(&zc)) {
600e7437265Sahrens 		(void) printf("\t\t%s = %lld (type: %s)\n",
601e7437265Sahrens 		    attr.za_name, ZFS_DIRENT_OBJ(attr.za_first_integer),
602e7437265Sahrens 		    typenames[ZFS_DIRENT_TYPE(attr.za_first_integer)]);
603e7437265Sahrens 	}
604e7437265Sahrens 	zap_cursor_fini(&zc);
605e7437265Sahrens }
606e7437265Sahrens 
6070713e232SGeorge Wilson int
6080713e232SGeorge Wilson get_dtl_refcount(vdev_t *vd)
6090713e232SGeorge Wilson {
6100713e232SGeorge Wilson 	int refcount = 0;
6110713e232SGeorge Wilson 
6120713e232SGeorge Wilson 	if (vd->vdev_ops->vdev_op_leaf) {
6130713e232SGeorge Wilson 		space_map_t *sm = vd->vdev_dtl_sm;
6140713e232SGeorge Wilson 
6150713e232SGeorge Wilson 		if (sm != NULL &&
6160713e232SGeorge Wilson 		    sm->sm_dbuf->db_size == sizeof (space_map_phys_t))
6170713e232SGeorge Wilson 			return (1);
6180713e232SGeorge Wilson 		return (0);
6190713e232SGeorge Wilson 	}
6200713e232SGeorge Wilson 
6210713e232SGeorge Wilson 	for (int c = 0; c < vd->vdev_children; c++)
6220713e232SGeorge Wilson 		refcount += get_dtl_refcount(vd->vdev_child[c]);
6230713e232SGeorge Wilson 	return (refcount);
6240713e232SGeorge Wilson }
6250713e232SGeorge Wilson 
6260713e232SGeorge Wilson int
6270713e232SGeorge Wilson get_metaslab_refcount(vdev_t *vd)
6280713e232SGeorge Wilson {
6290713e232SGeorge Wilson 	int refcount = 0;
6300713e232SGeorge Wilson 
6312e4c9986SGeorge Wilson 	if (vd->vdev_top == vd && !vd->vdev_removing) {
6320713e232SGeorge Wilson 		for (int m = 0; m < vd->vdev_ms_count; m++) {
6330713e232SGeorge Wilson 			space_map_t *sm = vd->vdev_ms[m]->ms_sm;
6340713e232SGeorge Wilson 
6350713e232SGeorge Wilson 			if (sm != NULL &&
6360713e232SGeorge Wilson 			    sm->sm_dbuf->db_size == sizeof (space_map_phys_t))
6370713e232SGeorge Wilson 				refcount++;
6380713e232SGeorge Wilson 		}
6390713e232SGeorge Wilson 	}
6400713e232SGeorge Wilson 	for (int c = 0; c < vd->vdev_children; c++)
6410713e232SGeorge Wilson 		refcount += get_metaslab_refcount(vd->vdev_child[c]);
6420713e232SGeorge Wilson 
6430713e232SGeorge Wilson 	return (refcount);
6440713e232SGeorge Wilson }
6450713e232SGeorge Wilson 
6460713e232SGeorge Wilson static int
6470713e232SGeorge Wilson verify_spacemap_refcounts(spa_t *spa)
6480713e232SGeorge Wilson {
6492acef22dSMatthew Ahrens 	uint64_t expected_refcount = 0;
6502acef22dSMatthew Ahrens 	uint64_t actual_refcount;
6510713e232SGeorge Wilson 
6522acef22dSMatthew Ahrens 	(void) feature_get_refcount(spa,
6532acef22dSMatthew Ahrens 	    &spa_feature_table[SPA_FEATURE_SPACEMAP_HISTOGRAM],
6542acef22dSMatthew Ahrens 	    &expected_refcount);
6550713e232SGeorge Wilson 	actual_refcount = get_dtl_refcount(spa->spa_root_vdev);
6560713e232SGeorge Wilson 	actual_refcount += get_metaslab_refcount(spa->spa_root_vdev);
6570713e232SGeorge Wilson 
6580713e232SGeorge Wilson 	if (expected_refcount != actual_refcount) {
6592acef22dSMatthew Ahrens 		(void) printf("space map refcount mismatch: expected %lld != "
6602acef22dSMatthew Ahrens 		    "actual %lld\n",
6612acef22dSMatthew Ahrens 		    (longlong_t)expected_refcount,
6622acef22dSMatthew Ahrens 		    (longlong_t)actual_refcount);
6630713e232SGeorge Wilson 		return (2);
6640713e232SGeorge Wilson 	}
6650713e232SGeorge Wilson 	return (0);
6660713e232SGeorge Wilson }
6670713e232SGeorge Wilson 
668fa9e4066Sahrens static void
6690713e232SGeorge Wilson dump_spacemap(objset_t *os, space_map_t *sm)
670fa9e4066Sahrens {
671fa9e4066Sahrens 	uint64_t alloc, offset, entry;
6728053a263Sck 	char *ddata[] = { "ALLOC", "FREE", "CONDENSE", "INVALID",
6738053a263Sck 			    "INVALID", "INVALID", "INVALID", "INVALID" };
674fa9e4066Sahrens 
6750713e232SGeorge Wilson 	if (sm == NULL)
676fa9e4066Sahrens 		return;
677fa9e4066Sahrens 
678fa9e4066Sahrens 	/*
679fa9e4066Sahrens 	 * Print out the freelist entries in both encoded and decoded form.
680fa9e4066Sahrens 	 */
681fa9e4066Sahrens 	alloc = 0;
6820713e232SGeorge Wilson 	for (offset = 0; offset < space_map_length(sm);
6830713e232SGeorge Wilson 	    offset += sizeof (entry)) {
6840713e232SGeorge Wilson 		uint8_t mapshift = sm->sm_shift;
6850713e232SGeorge Wilson 
6860713e232SGeorge Wilson 		VERIFY0(dmu_read(os, space_map_object(sm), offset,
6877bfdf011SNeil Perrin 		    sizeof (entry), &entry, DMU_READ_PREFETCH));
688fa9e4066Sahrens 		if (SM_DEBUG_DECODE(entry)) {
6890713e232SGeorge Wilson 
69087219db7SVictor Latushkin 			(void) printf("\t    [%6llu] %s: txg %llu, pass %llu\n",
691fa9e4066Sahrens 			    (u_longlong_t)(offset / sizeof (entry)),
692fa9e4066Sahrens 			    ddata[SM_DEBUG_ACTION_DECODE(entry)],
6935ad82045Snd 			    (u_longlong_t)SM_DEBUG_TXG_DECODE(entry),
6945ad82045Snd 			    (u_longlong_t)SM_DEBUG_SYNCPASS_DECODE(entry));
695fa9e4066Sahrens 		} else {
69687219db7SVictor Latushkin 			(void) printf("\t    [%6llu]    %c  range:"
69787219db7SVictor Latushkin 			    " %010llx-%010llx  size: %06llx\n",
698fa9e4066Sahrens 			    (u_longlong_t)(offset / sizeof (entry)),
699fa9e4066Sahrens 			    SM_TYPE_DECODE(entry) == SM_ALLOC ? 'A' : 'F',
7005ad82045Snd 			    (u_longlong_t)((SM_OFFSET_DECODE(entry) <<
7010713e232SGeorge Wilson 			    mapshift) + sm->sm_start),
7025ad82045Snd 			    (u_longlong_t)((SM_OFFSET_DECODE(entry) <<
7030713e232SGeorge Wilson 			    mapshift) + sm->sm_start +
7040713e232SGeorge Wilson 			    (SM_RUN_DECODE(entry) << mapshift)),
7055ad82045Snd 			    (u_longlong_t)(SM_RUN_DECODE(entry) << mapshift));
706fa9e4066Sahrens 			if (SM_TYPE_DECODE(entry) == SM_ALLOC)
707fa9e4066Sahrens 				alloc += SM_RUN_DECODE(entry) << mapshift;
708fa9e4066Sahrens 			else
709fa9e4066Sahrens 				alloc -= SM_RUN_DECODE(entry) << mapshift;
710fa9e4066Sahrens 		}
711fa9e4066Sahrens 	}
7120713e232SGeorge Wilson 	if (alloc != space_map_allocated(sm)) {
713fa9e4066Sahrens 		(void) printf("space_map_object alloc (%llu) INCONSISTENT "
714fa9e4066Sahrens 		    "with space map summary (%llu)\n",
7150713e232SGeorge Wilson 		    (u_longlong_t)space_map_allocated(sm), (u_longlong_t)alloc);
716fa9e4066Sahrens 	}
717fa9e4066Sahrens }
718fa9e4066Sahrens 
719d6e555bdSGeorge Wilson static void
720d6e555bdSGeorge Wilson dump_metaslab_stats(metaslab_t *msp)
721d6e555bdSGeorge Wilson {
7223f9d6ad7SLin Ling 	char maxbuf[32];
7230713e232SGeorge Wilson 	range_tree_t *rt = msp->ms_tree;
7240713e232SGeorge Wilson 	avl_tree_t *t = &msp->ms_size_tree;
7250713e232SGeorge Wilson 	int free_pct = range_tree_space(rt) * 100 / msp->ms_size;
726d6e555bdSGeorge Wilson 
7270713e232SGeorge Wilson 	zdb_nicenum(metaslab_block_maxsize(msp), maxbuf);
728d6e555bdSGeorge Wilson 
72987219db7SVictor Latushkin 	(void) printf("\t %25s %10lu   %7s  %6s   %4s %4d%%\n",
730d6e555bdSGeorge Wilson 	    "segments", avl_numnodes(t), "maxsize", maxbuf,
731d6e555bdSGeorge Wilson 	    "freepct", free_pct);
7320713e232SGeorge Wilson 	(void) printf("\tIn-memory histogram:\n");
7330713e232SGeorge Wilson 	dump_histogram(rt->rt_histogram, RANGE_TREE_HISTOGRAM_SIZE, 0);
734d6e555bdSGeorge Wilson }
735d6e555bdSGeorge Wilson 
736fa9e4066Sahrens static void
737fa9e4066Sahrens dump_metaslab(metaslab_t *msp)
738fa9e4066Sahrens {
739fa9e4066Sahrens 	vdev_t *vd = msp->ms_group->mg_vd;
740fa9e4066Sahrens 	spa_t *spa = vd->vdev_spa;
7410713e232SGeorge Wilson 	space_map_t *sm = msp->ms_sm;
7423f9d6ad7SLin Ling 	char freebuf[32];
743fa9e4066Sahrens 
7440713e232SGeorge Wilson 	zdb_nicenum(msp->ms_size - space_map_allocated(sm), freebuf);
745fa9e4066Sahrens 
746fa9e4066Sahrens 	(void) printf(
74787219db7SVictor Latushkin 	    "\tmetaslab %6llu   offset %12llx   spacemap %6llu   free    %5s\n",
7480713e232SGeorge Wilson 	    (u_longlong_t)msp->ms_id, (u_longlong_t)msp->ms_start,
7490713e232SGeorge Wilson 	    (u_longlong_t)space_map_object(sm), freebuf);
750fa9e4066Sahrens 
7510713e232SGeorge Wilson 	if (dump_opt['m'] > 2 && !dump_opt['L']) {
752d6e555bdSGeorge Wilson 		mutex_enter(&msp->ms_lock);
7530713e232SGeorge Wilson 		metaslab_load_wait(msp);
7540713e232SGeorge Wilson 		if (!msp->ms_loaded) {
7550713e232SGeorge Wilson 			VERIFY0(metaslab_load(msp));
7560713e232SGeorge Wilson 			range_tree_stat_verify(msp->ms_tree);
7570713e232SGeorge Wilson 		}
758bc3975b5SVictor Latushkin 		dump_metaslab_stats(msp);
7590713e232SGeorge Wilson 		metaslab_unload(msp);
760d6e555bdSGeorge Wilson 		mutex_exit(&msp->ms_lock);
761d6e555bdSGeorge Wilson 	}
762d6e555bdSGeorge Wilson 
7630713e232SGeorge Wilson 	if (dump_opt['m'] > 1 && sm != NULL &&
7642acef22dSMatthew Ahrens 	    spa_feature_is_active(spa, SPA_FEATURE_SPACEMAP_HISTOGRAM)) {
7650713e232SGeorge Wilson 		/*
7660713e232SGeorge Wilson 		 * The space map histogram represents free space in chunks
7670713e232SGeorge Wilson 		 * of sm_shift (i.e. bucket 0 refers to 2^sm_shift).
7680713e232SGeorge Wilson 		 */
7692e4c9986SGeorge Wilson 		(void) printf("\tOn-disk histogram:\t\tfragmentation %llu\n",
7702e4c9986SGeorge Wilson 		    (u_longlong_t)msp->ms_fragmentation);
7710713e232SGeorge Wilson 		dump_histogram(sm->sm_phys->smp_histogram,
7722e4c9986SGeorge Wilson 		    SPACE_MAP_HISTOGRAM_SIZE, sm->sm_shift);
7730713e232SGeorge Wilson 	}
7740713e232SGeorge Wilson 
7750713e232SGeorge Wilson 	if (dump_opt['d'] > 5 || dump_opt['m'] > 3) {
7760713e232SGeorge Wilson 		ASSERT(msp->ms_size == (1ULL << vd->vdev_ms_shift));
777d6e555bdSGeorge Wilson 
778d6e555bdSGeorge Wilson 		mutex_enter(&msp->ms_lock);
7790713e232SGeorge Wilson 		dump_spacemap(spa->spa_meta_objset, msp->ms_sm);
780d6e555bdSGeorge Wilson 		mutex_exit(&msp->ms_lock);
781d6e555bdSGeorge Wilson 	}
78287219db7SVictor Latushkin }
783fa9e4066Sahrens 
78487219db7SVictor Latushkin static void
78587219db7SVictor Latushkin print_vdev_metaslab_header(vdev_t *vd)
78687219db7SVictor Latushkin {
78787219db7SVictor Latushkin 	(void) printf("\tvdev %10llu\n\t%-10s%5llu   %-19s   %-15s   %-10s\n",
78887219db7SVictor Latushkin 	    (u_longlong_t)vd->vdev_id,
78987219db7SVictor Latushkin 	    "metaslabs", (u_longlong_t)vd->vdev_ms_count,
79087219db7SVictor Latushkin 	    "offset", "spacemap", "free");
79187219db7SVictor Latushkin 	(void) printf("\t%15s   %19s   %15s   %10s\n",
79287219db7SVictor Latushkin 	    "---------------", "-------------------",
79387219db7SVictor Latushkin 	    "---------------", "-------------");
794fa9e4066Sahrens }
795fa9e4066Sahrens 
7962e4c9986SGeorge Wilson static void
7972e4c9986SGeorge Wilson dump_metaslab_groups(spa_t *spa)
7982e4c9986SGeorge Wilson {
7992e4c9986SGeorge Wilson 	vdev_t *rvd = spa->spa_root_vdev;
8002e4c9986SGeorge Wilson 	metaslab_class_t *mc = spa_normal_class(spa);
8012e4c9986SGeorge Wilson 	uint64_t fragmentation;
8022e4c9986SGeorge Wilson 
8032e4c9986SGeorge Wilson 	metaslab_class_histogram_verify(mc);
8042e4c9986SGeorge Wilson 
8052e4c9986SGeorge Wilson 	for (int c = 0; c < rvd->vdev_children; c++) {
8062e4c9986SGeorge Wilson 		vdev_t *tvd = rvd->vdev_child[c];
8072e4c9986SGeorge Wilson 		metaslab_group_t *mg = tvd->vdev_mg;
8082e4c9986SGeorge Wilson 
8092e4c9986SGeorge Wilson 		if (mg->mg_class != mc)
8102e4c9986SGeorge Wilson 			continue;
8112e4c9986SGeorge Wilson 
8122e4c9986SGeorge Wilson 		metaslab_group_histogram_verify(mg);
8132e4c9986SGeorge Wilson 		mg->mg_fragmentation = metaslab_group_fragmentation(mg);
8142e4c9986SGeorge Wilson 
8152e4c9986SGeorge Wilson 		(void) printf("\tvdev %10llu\t\tmetaslabs%5llu\t\t"
8162e4c9986SGeorge Wilson 		    "fragmentation",
8172e4c9986SGeorge Wilson 		    (u_longlong_t)tvd->vdev_id,
8182e4c9986SGeorge Wilson 		    (u_longlong_t)tvd->vdev_ms_count);
8192e4c9986SGeorge Wilson 		if (mg->mg_fragmentation == ZFS_FRAG_INVALID) {
8202e4c9986SGeorge Wilson 			(void) printf("%3s\n", "-");
8212e4c9986SGeorge Wilson 		} else {
8222e4c9986SGeorge Wilson 			(void) printf("%3llu%%\n",
8232e4c9986SGeorge Wilson 			    (u_longlong_t)mg->mg_fragmentation);
8242e4c9986SGeorge Wilson 		}
8252e4c9986SGeorge Wilson 		dump_histogram(mg->mg_histogram, RANGE_TREE_HISTOGRAM_SIZE, 0);
8262e4c9986SGeorge Wilson 	}
8272e4c9986SGeorge Wilson 
8282e4c9986SGeorge Wilson 	(void) printf("\tpool %s\tfragmentation", spa_name(spa));
8292e4c9986SGeorge Wilson 	fragmentation = metaslab_class_fragmentation(mc);
8302e4c9986SGeorge Wilson 	if (fragmentation == ZFS_FRAG_INVALID)
8312e4c9986SGeorge Wilson 		(void) printf("\t%3s\n", "-");
8322e4c9986SGeorge Wilson 	else
8332e4c9986SGeorge Wilson 		(void) printf("\t%3llu%%\n", (u_longlong_t)fragmentation);
8342e4c9986SGeorge Wilson 	dump_histogram(mc->mc_histogram, RANGE_TREE_HISTOGRAM_SIZE, 0);
8352e4c9986SGeorge Wilson }
8362e4c9986SGeorge Wilson 
837fa9e4066Sahrens static void
838fa9e4066Sahrens dump_metaslabs(spa_t *spa)
839fa9e4066Sahrens {
84087219db7SVictor Latushkin 	vdev_t *vd, *rvd = spa->spa_root_vdev;
84187219db7SVictor Latushkin 	uint64_t m, c = 0, children = rvd->vdev_children;
842fa9e4066Sahrens 
843fa9e4066Sahrens 	(void) printf("\nMetaslabs:\n");
844fa9e4066Sahrens 
84587219db7SVictor Latushkin 	if (!dump_opt['d'] && zopt_objects > 0) {
84687219db7SVictor Latushkin 		c = zopt_object[0];
84787219db7SVictor Latushkin 
84887219db7SVictor Latushkin 		if (c >= children)
84987219db7SVictor Latushkin 			(void) fatal("bad vdev id: %llu", (u_longlong_t)c);
850fa9e4066Sahrens 
85187219db7SVictor Latushkin 		if (zopt_objects > 1) {
85287219db7SVictor Latushkin 			vd = rvd->vdev_child[c];
85387219db7SVictor Latushkin 			print_vdev_metaslab_header(vd);
85487219db7SVictor Latushkin 
85587219db7SVictor Latushkin 			for (m = 1; m < zopt_objects; m++) {
85687219db7SVictor Latushkin 				if (zopt_object[m] < vd->vdev_ms_count)
85787219db7SVictor Latushkin 					dump_metaslab(
85887219db7SVictor Latushkin 					    vd->vdev_ms[zopt_object[m]]);
85987219db7SVictor Latushkin 				else
86087219db7SVictor Latushkin 					(void) fprintf(stderr, "bad metaslab "
86187219db7SVictor Latushkin 					    "number %llu\n",
86287219db7SVictor Latushkin 					    (u_longlong_t)zopt_object[m]);
86387219db7SVictor Latushkin 			}
86487219db7SVictor Latushkin 			(void) printf("\n");
86587219db7SVictor Latushkin 			return;
86687219db7SVictor Latushkin 		}
86787219db7SVictor Latushkin 		children = c + 1;
86887219db7SVictor Latushkin 	}
86987219db7SVictor Latushkin 	for (; c < children; c++) {
87087219db7SVictor Latushkin 		vd = rvd->vdev_child[c];
87187219db7SVictor Latushkin 		print_vdev_metaslab_header(vd);
872fa9e4066Sahrens 
873fa9e4066Sahrens 		for (m = 0; m < vd->vdev_ms_count; m++)
874fa9e4066Sahrens 			dump_metaslab(vd->vdev_ms[m]);
875fa9e4066Sahrens 		(void) printf("\n");
876fa9e4066Sahrens 	}
877fa9e4066Sahrens }
878fa9e4066Sahrens 
879b24ab676SJeff Bonwick static void
880b24ab676SJeff Bonwick dump_dde(const ddt_t *ddt, const ddt_entry_t *dde, uint64_t index)
881b24ab676SJeff Bonwick {
882b24ab676SJeff Bonwick 	const ddt_phys_t *ddp = dde->dde_phys;
883b24ab676SJeff Bonwick 	const ddt_key_t *ddk = &dde->dde_key;
884b24ab676SJeff Bonwick 	char *types[4] = { "ditto", "single", "double", "triple" };
885b24ab676SJeff Bonwick 	char blkbuf[BP_SPRINTF_LEN];
886b24ab676SJeff Bonwick 	blkptr_t blk;
887b24ab676SJeff Bonwick 
888b24ab676SJeff Bonwick 	for (int p = 0; p < DDT_PHYS_TYPES; p++, ddp++) {
889b24ab676SJeff Bonwick 		if (ddp->ddp_phys_birth == 0)
890b24ab676SJeff Bonwick 			continue;
891bbfd46c4SJeff Bonwick 		ddt_bp_create(ddt->ddt_checksum, ddk, ddp, &blk);
89243466aaeSMax Grossman 		snprintf_blkptr(blkbuf, sizeof (blkbuf), &blk);
893b24ab676SJeff Bonwick 		(void) printf("index %llx refcnt %llu %s %s\n",
894b24ab676SJeff Bonwick 		    (u_longlong_t)index, (u_longlong_t)ddp->ddp_refcnt,
895b24ab676SJeff Bonwick 		    types[p], blkbuf);
896b24ab676SJeff Bonwick 	}
897b24ab676SJeff Bonwick }
898b24ab676SJeff Bonwick 
899b24ab676SJeff Bonwick static void
900b24ab676SJeff Bonwick dump_dedup_ratio(const ddt_stat_t *dds)
901b24ab676SJeff Bonwick {
902b24ab676SJeff Bonwick 	double rL, rP, rD, D, dedup, compress, copies;
903b24ab676SJeff Bonwick 
904b24ab676SJeff Bonwick 	if (dds->dds_blocks == 0)
905b24ab676SJeff Bonwick 		return;
906b24ab676SJeff Bonwick 
907b24ab676SJeff Bonwick 	rL = (double)dds->dds_ref_lsize;
908b24ab676SJeff Bonwick 	rP = (double)dds->dds_ref_psize;
909b24ab676SJeff Bonwick 	rD = (double)dds->dds_ref_dsize;
910b24ab676SJeff Bonwick 	D = (double)dds->dds_dsize;
911b24ab676SJeff Bonwick 
912b24ab676SJeff Bonwick 	dedup = rD / D;
913b24ab676SJeff Bonwick 	compress = rL / rP;
914b24ab676SJeff Bonwick 	copies = rD / rP;
915b24ab676SJeff Bonwick 
916b24ab676SJeff Bonwick 	(void) printf("dedup = %.2f, compress = %.2f, copies = %.2f, "
917b24ab676SJeff Bonwick 	    "dedup * compress / copies = %.2f\n\n",
918b24ab676SJeff Bonwick 	    dedup, compress, copies, dedup * compress / copies);
919b24ab676SJeff Bonwick }
920b24ab676SJeff Bonwick 
921b24ab676SJeff Bonwick static void
922b24ab676SJeff Bonwick dump_ddt(ddt_t *ddt, enum ddt_type type, enum ddt_class class)
923b24ab676SJeff Bonwick {
924b24ab676SJeff Bonwick 	char name[DDT_NAMELEN];
925b24ab676SJeff Bonwick 	ddt_entry_t dde;
926b24ab676SJeff Bonwick 	uint64_t walk = 0;
927b24ab676SJeff Bonwick 	dmu_object_info_t doi;
928b24ab676SJeff Bonwick 	uint64_t count, dspace, mspace;
929b24ab676SJeff Bonwick 	int error;
930b24ab676SJeff Bonwick 
931b24ab676SJeff Bonwick 	error = ddt_object_info(ddt, type, class, &doi);
932b24ab676SJeff Bonwick 
933b24ab676SJeff Bonwick 	if (error == ENOENT)
934b24ab676SJeff Bonwick 		return;
935b24ab676SJeff Bonwick 	ASSERT(error == 0);
936b24ab676SJeff Bonwick 
9377448a079SGeorge Wilson 	if ((count = ddt_object_count(ddt, type, class)) == 0)
9387448a079SGeorge Wilson 		return;
9397448a079SGeorge Wilson 
940b24ab676SJeff Bonwick 	dspace = doi.doi_physical_blocks_512 << 9;
941b24ab676SJeff Bonwick 	mspace = doi.doi_fill_count * doi.doi_data_block_size;
942b24ab676SJeff Bonwick 
943b24ab676SJeff Bonwick 	ddt_object_name(ddt, type, class, name);
944b24ab676SJeff Bonwick 
945b24ab676SJeff Bonwick 	(void) printf("%s: %llu entries, size %llu on disk, %llu in core\n",
946b24ab676SJeff Bonwick 	    name,
947b24ab676SJeff Bonwick 	    (u_longlong_t)count,
948b24ab676SJeff Bonwick 	    (u_longlong_t)(dspace / count),
949b24ab676SJeff Bonwick 	    (u_longlong_t)(mspace / count));
950b24ab676SJeff Bonwick 
951b24ab676SJeff Bonwick 	if (dump_opt['D'] < 3)
952b24ab676SJeff Bonwick 		return;
953b24ab676SJeff Bonwick 
9549eb19f4dSGeorge Wilson 	zpool_dump_ddt(NULL, &ddt->ddt_histogram[type][class]);
955b24ab676SJeff Bonwick 
956b24ab676SJeff Bonwick 	if (dump_opt['D'] < 4)
957b24ab676SJeff Bonwick 		return;
958b24ab676SJeff Bonwick 
959b24ab676SJeff Bonwick 	if (dump_opt['D'] < 5 && class == DDT_CLASS_UNIQUE)
960b24ab676SJeff Bonwick 		return;
961b24ab676SJeff Bonwick 
962b24ab676SJeff Bonwick 	(void) printf("%s contents:\n\n", name);
963b24ab676SJeff Bonwick 
964bbfd46c4SJeff Bonwick 	while ((error = ddt_object_walk(ddt, type, class, &walk, &dde)) == 0)
965b24ab676SJeff Bonwick 		dump_dde(ddt, &dde, walk);
966b24ab676SJeff Bonwick 
967b24ab676SJeff Bonwick 	ASSERT(error == ENOENT);
968b24ab676SJeff Bonwick 
969b24ab676SJeff Bonwick 	(void) printf("\n");
970b24ab676SJeff Bonwick }
971b24ab676SJeff Bonwick 
972b24ab676SJeff Bonwick static void
973b24ab676SJeff Bonwick dump_all_ddts(spa_t *spa)
974b24ab676SJeff Bonwick {
975b24ab676SJeff Bonwick 	ddt_histogram_t ddh_total = { 0 };
976b24ab676SJeff Bonwick 	ddt_stat_t dds_total = { 0 };
977b24ab676SJeff Bonwick 
978b24ab676SJeff Bonwick 	for (enum zio_checksum c = 0; c < ZIO_CHECKSUM_FUNCTIONS; c++) {
979b24ab676SJeff Bonwick 		ddt_t *ddt = spa->spa_ddt[c];
980b24ab676SJeff Bonwick 		for (enum ddt_type type = 0; type < DDT_TYPES; type++) {
981b24ab676SJeff Bonwick 			for (enum ddt_class class = 0; class < DDT_CLASSES;
982b24ab676SJeff Bonwick 			    class++) {
983b24ab676SJeff Bonwick 				dump_ddt(ddt, type, class);
984b24ab676SJeff Bonwick 			}
985b24ab676SJeff Bonwick 		}
986b24ab676SJeff Bonwick 	}
987b24ab676SJeff Bonwick 
9889eb19f4dSGeorge Wilson 	ddt_get_dedup_stats(spa, &dds_total);
989b24ab676SJeff Bonwick 
990b24ab676SJeff Bonwick 	if (dds_total.dds_blocks == 0) {
991b24ab676SJeff Bonwick 		(void) printf("All DDTs are empty\n");
992b24ab676SJeff Bonwick 		return;
993b24ab676SJeff Bonwick 	}
994b24ab676SJeff Bonwick 
995b24ab676SJeff Bonwick 	(void) printf("\n");
996b24ab676SJeff Bonwick 
997b24ab676SJeff Bonwick 	if (dump_opt['D'] > 1) {
998b24ab676SJeff Bonwick 		(void) printf("DDT histogram (aggregated over all DDTs):\n");
9999eb19f4dSGeorge Wilson 		ddt_get_dedup_histogram(spa, &ddh_total);
10009eb19f4dSGeorge Wilson 		zpool_dump_ddt(&dds_total, &ddh_total);
1001b24ab676SJeff Bonwick 	}
1002b24ab676SJeff Bonwick 
1003b24ab676SJeff Bonwick 	dump_dedup_ratio(&dds_total);
1004b24ab676SJeff Bonwick }
1005b24ab676SJeff Bonwick 
10068ad4d6ddSJeff Bonwick static void
10070713e232SGeorge Wilson dump_dtl_seg(void *arg, uint64_t start, uint64_t size)
10088ad4d6ddSJeff Bonwick {
10090713e232SGeorge Wilson 	char *prefix = arg;
10108ad4d6ddSJeff Bonwick 
10118ad4d6ddSJeff Bonwick 	(void) printf("%s [%llu,%llu) length %llu\n",
10128ad4d6ddSJeff Bonwick 	    prefix,
10138ad4d6ddSJeff Bonwick 	    (u_longlong_t)start,
10148ad4d6ddSJeff Bonwick 	    (u_longlong_t)(start + size),
10158ad4d6ddSJeff Bonwick 	    (u_longlong_t)(size));
10168ad4d6ddSJeff Bonwick }
10178ad4d6ddSJeff Bonwick 
1018fa9e4066Sahrens static void
1019fa9e4066Sahrens dump_dtl(vdev_t *vd, int indent)
1020fa9e4066Sahrens {
10218ad4d6ddSJeff Bonwick 	spa_t *spa = vd->vdev_spa;
10228ad4d6ddSJeff Bonwick 	boolean_t required;
10238ad4d6ddSJeff Bonwick 	char *name[DTL_TYPES] = { "missing", "partial", "scrub", "outage" };
10248ad4d6ddSJeff Bonwick 	char prefix[256];
10258ad4d6ddSJeff Bonwick 
10268f18d1faSGeorge Wilson 	spa_vdev_state_enter(spa, SCL_NONE);
10278ad4d6ddSJeff Bonwick 	required = vdev_dtl_required(vd);
10288ad4d6ddSJeff Bonwick 	(void) spa_vdev_state_exit(spa, NULL, 0);
1029fa9e4066Sahrens 
1030fa9e4066Sahrens 	if (indent == 0)
1031fa9e4066Sahrens 		(void) printf("\nDirty time logs:\n\n");
1032fa9e4066Sahrens 
10338ad4d6ddSJeff Bonwick 	(void) printf("\t%*s%s [%s]\n", indent, "",
1034e14bb325SJeff Bonwick 	    vd->vdev_path ? vd->vdev_path :
10358ad4d6ddSJeff Bonwick 	    vd->vdev_parent ? vd->vdev_ops->vdev_op_type : spa_name(spa),
10368ad4d6ddSJeff Bonwick 	    required ? "DTL-required" : "DTL-expendable");
1037fa9e4066Sahrens 
10388ad4d6ddSJeff Bonwick 	for (int t = 0; t < DTL_TYPES; t++) {
10390713e232SGeorge Wilson 		range_tree_t *rt = vd->vdev_dtl[t];
10400713e232SGeorge Wilson 		if (range_tree_space(rt) == 0)
10418ad4d6ddSJeff Bonwick 			continue;
10428ad4d6ddSJeff Bonwick 		(void) snprintf(prefix, sizeof (prefix), "\t%*s%s",
10438ad4d6ddSJeff Bonwick 		    indent + 2, "", name[t]);
10440713e232SGeorge Wilson 		mutex_enter(rt->rt_lock);
10450713e232SGeorge Wilson 		range_tree_walk(rt, dump_dtl_seg, prefix);
10460713e232SGeorge Wilson 		mutex_exit(rt->rt_lock);
10478ad4d6ddSJeff Bonwick 		if (dump_opt['d'] > 5 && vd->vdev_children == 0)
10480713e232SGeorge Wilson 			dump_spacemap(spa->spa_meta_objset, vd->vdev_dtl_sm);
1049fa9e4066Sahrens 	}
1050fa9e4066Sahrens 
10518ad4d6ddSJeff Bonwick 	for (int c = 0; c < vd->vdev_children; c++)
1052fa9e4066Sahrens 		dump_dtl(vd->vdev_child[c], indent + 4);
1053fa9e4066Sahrens }
1054fa9e4066Sahrens 
10558f18d1faSGeorge Wilson static void
10568f18d1faSGeorge Wilson dump_history(spa_t *spa)
10578f18d1faSGeorge Wilson {
10588f18d1faSGeorge Wilson 	nvlist_t **events = NULL;
10598f18d1faSGeorge Wilson 	char buf[SPA_MAXBLOCKSIZE];
1060e4161df6SVictor Latushkin 	uint64_t resid, len, off = 0;
10618f18d1faSGeorge Wilson 	uint_t num = 0;
10628f18d1faSGeorge Wilson 	int error;
10638f18d1faSGeorge Wilson 	time_t tsec;
10648f18d1faSGeorge Wilson 	struct tm t;
10658f18d1faSGeorge Wilson 	char tbuf[30];
10668f18d1faSGeorge Wilson 	char internalstr[MAXPATHLEN];
10678f18d1faSGeorge Wilson 
10688f18d1faSGeorge Wilson 	do {
1069e4161df6SVictor Latushkin 		len = sizeof (buf);
1070e4161df6SVictor Latushkin 
10718f18d1faSGeorge Wilson 		if ((error = spa_history_get(spa, &off, &len, buf)) != 0) {
10728f18d1faSGeorge Wilson 			(void) fprintf(stderr, "Unable to read history: "
10738f18d1faSGeorge Wilson 			    "error %d\n", error);
10748f18d1faSGeorge Wilson 			return;
10758f18d1faSGeorge Wilson 		}
10768f18d1faSGeorge Wilson 
10778f18d1faSGeorge Wilson 		if (zpool_history_unpack(buf, len, &resid, &events, &num) != 0)
10788f18d1faSGeorge Wilson 			break;
10798f18d1faSGeorge Wilson 
10808f18d1faSGeorge Wilson 		off -= resid;
10818f18d1faSGeorge Wilson 	} while (len != 0);
10828f18d1faSGeorge Wilson 
10838f18d1faSGeorge Wilson 	(void) printf("\nHistory:\n");
10848f18d1faSGeorge Wilson 	for (int i = 0; i < num; i++) {
10858f18d1faSGeorge Wilson 		uint64_t time, txg, ievent;
10868f18d1faSGeorge Wilson 		char *cmd, *intstr;
10874445fffbSMatthew Ahrens 		boolean_t printed = B_FALSE;
10888f18d1faSGeorge Wilson 
10898f18d1faSGeorge Wilson 		if (nvlist_lookup_uint64(events[i], ZPOOL_HIST_TIME,
10908f18d1faSGeorge Wilson 		    &time) != 0)
10914445fffbSMatthew Ahrens 			goto next;
10928f18d1faSGeorge Wilson 		if (nvlist_lookup_string(events[i], ZPOOL_HIST_CMD,
10938f18d1faSGeorge Wilson 		    &cmd) != 0) {
10948f18d1faSGeorge Wilson 			if (nvlist_lookup_uint64(events[i],
10958f18d1faSGeorge Wilson 			    ZPOOL_HIST_INT_EVENT, &ievent) != 0)
10964445fffbSMatthew Ahrens 				goto next;
10978f18d1faSGeorge Wilson 			verify(nvlist_lookup_uint64(events[i],
10988f18d1faSGeorge Wilson 			    ZPOOL_HIST_TXG, &txg) == 0);
10998f18d1faSGeorge Wilson 			verify(nvlist_lookup_string(events[i],
11008f18d1faSGeorge Wilson 			    ZPOOL_HIST_INT_STR, &intstr) == 0);
11014445fffbSMatthew Ahrens 			if (ievent >= ZFS_NUM_LEGACY_HISTORY_EVENTS)
11024445fffbSMatthew Ahrens 				goto next;
11038f18d1faSGeorge Wilson 
11048f18d1faSGeorge Wilson 			(void) snprintf(internalstr,
11058f18d1faSGeorge Wilson 			    sizeof (internalstr),
11068f18d1faSGeorge Wilson 			    "[internal %s txg:%lld] %s",
11073f9d6ad7SLin Ling 			    zfs_history_event_names[ievent], txg,
11088f18d1faSGeorge Wilson 			    intstr);
11098f18d1faSGeorge Wilson 			cmd = internalstr;
11108f18d1faSGeorge Wilson 		}
11118f18d1faSGeorge Wilson 		tsec = time;
11128f18d1faSGeorge Wilson 		(void) localtime_r(&tsec, &t);
11138f18d1faSGeorge Wilson 		(void) strftime(tbuf, sizeof (tbuf), "%F.%T", &t);
11148f18d1faSGeorge Wilson 		(void) printf("%s %s\n", tbuf, cmd);
11154445fffbSMatthew Ahrens 		printed = B_TRUE;
11164445fffbSMatthew Ahrens 
11174445fffbSMatthew Ahrens next:
11184445fffbSMatthew Ahrens 		if (dump_opt['h'] > 1) {
11194445fffbSMatthew Ahrens 			if (!printed)
11204445fffbSMatthew Ahrens 				(void) printf("unrecognized record:\n");
11214445fffbSMatthew Ahrens 			dump_nvlist(events[i], 2);
11224445fffbSMatthew Ahrens 		}
11238f18d1faSGeorge Wilson 	}
11248f18d1faSGeorge Wilson }
11258f18d1faSGeorge Wilson 
1126fa9e4066Sahrens /*ARGSUSED*/
1127fa9e4066Sahrens static void
1128fa9e4066Sahrens dump_dnode(objset_t *os, uint64_t object, void *data, size_t size)
1129fa9e4066Sahrens {
1130fa9e4066Sahrens }
1131fa9e4066Sahrens 
1132fa9e4066Sahrens static uint64_t
11337802d7bfSMatthew Ahrens blkid2offset(const dnode_phys_t *dnp, const blkptr_t *bp,
11347802d7bfSMatthew Ahrens     const zbookmark_phys_t *zb)
1135fa9e4066Sahrens {
1136b24ab676SJeff Bonwick 	if (dnp == NULL) {
1137b24ab676SJeff Bonwick 		ASSERT(zb->zb_level < 0);
1138b24ab676SJeff Bonwick 		if (zb->zb_object == 0)
1139b24ab676SJeff Bonwick 			return (zb->zb_blkid);
1140b24ab676SJeff Bonwick 		return (zb->zb_blkid * BP_GET_LSIZE(bp));
1141b24ab676SJeff Bonwick 	}
1142b24ab676SJeff Bonwick 
1143b24ab676SJeff Bonwick 	ASSERT(zb->zb_level >= 0);
1144fa9e4066Sahrens 
1145b24ab676SJeff Bonwick 	return ((zb->zb_blkid <<
1146b24ab676SJeff Bonwick 	    (zb->zb_level * (dnp->dn_indblkshift - SPA_BLKPTRSHIFT))) *
1147fa9e4066Sahrens 	    dnp->dn_datablkszsec << SPA_MINBLOCKSHIFT);
1148fa9e4066Sahrens }
1149fa9e4066Sahrens 
115044cd46caSbillm static void
115143466aaeSMax Grossman snprintf_blkptr_compact(char *blkbuf, size_t buflen, const blkptr_t *bp)
115244cd46caSbillm {
1153cde58dbcSMatthew Ahrens 	const dva_t *dva = bp->blk_dva;
1154b24ab676SJeff Bonwick 	int ndvas = dump_opt['d'] > 5 ? BP_GET_NDVAS(bp) : 1;
1155b24ab676SJeff Bonwick 
1156490d05b9SMatthew Ahrens 	if (dump_opt['b'] >= 6) {
115743466aaeSMax Grossman 		snprintf_blkptr(blkbuf, buflen, bp);
1158b24ab676SJeff Bonwick 		return;
1159b24ab676SJeff Bonwick 	}
116044cd46caSbillm 
11615d7b4d43SMatthew Ahrens 	if (BP_IS_EMBEDDED(bp)) {
11625d7b4d43SMatthew Ahrens 		(void) sprintf(blkbuf,
11635d7b4d43SMatthew Ahrens 		    "EMBEDDED et=%u %llxL/%llxP B=%llu",
11645d7b4d43SMatthew Ahrens 		    (int)BPE_GET_ETYPE(bp),
11655d7b4d43SMatthew Ahrens 		    (u_longlong_t)BPE_GET_LSIZE(bp),
11665d7b4d43SMatthew Ahrens 		    (u_longlong_t)BPE_GET_PSIZE(bp),
11675d7b4d43SMatthew Ahrens 		    (u_longlong_t)bp->blk_birth);
11685d7b4d43SMatthew Ahrens 		return;
11695d7b4d43SMatthew Ahrens 	}
117044cd46caSbillm 
11715d7b4d43SMatthew Ahrens 	blkbuf[0] = '\0';
1172b24ab676SJeff Bonwick 	for (int i = 0; i < ndvas; i++)
117343466aaeSMax Grossman 		(void) snprintf(blkbuf + strlen(blkbuf),
117443466aaeSMax Grossman 		    buflen - strlen(blkbuf), "%llu:%llx:%llx ",
117544cd46caSbillm 		    (u_longlong_t)DVA_GET_VDEV(&dva[i]),
117644cd46caSbillm 		    (u_longlong_t)DVA_GET_OFFSET(&dva[i]),
117744cd46caSbillm 		    (u_longlong_t)DVA_GET_ASIZE(&dva[i]));
117844cd46caSbillm 
117943466aaeSMax Grossman 	if (BP_IS_HOLE(bp)) {
118043466aaeSMax Grossman 		(void) snprintf(blkbuf + strlen(blkbuf),
118170163ac5SPrakash Surya 		    buflen - strlen(blkbuf),
118270163ac5SPrakash Surya 		    "%llxL B=%llu",
118370163ac5SPrakash Surya 		    (u_longlong_t)BP_GET_LSIZE(bp),
118443466aaeSMax Grossman 		    (u_longlong_t)bp->blk_birth);
118543466aaeSMax Grossman 	} else {
118643466aaeSMax Grossman 		(void) snprintf(blkbuf + strlen(blkbuf),
118743466aaeSMax Grossman 		    buflen - strlen(blkbuf),
118843466aaeSMax Grossman 		    "%llxL/%llxP F=%llu B=%llu/%llu",
118943466aaeSMax Grossman 		    (u_longlong_t)BP_GET_LSIZE(bp),
119043466aaeSMax Grossman 		    (u_longlong_t)BP_GET_PSIZE(bp),
11915d7b4d43SMatthew Ahrens 		    (u_longlong_t)BP_GET_FILL(bp),
119243466aaeSMax Grossman 		    (u_longlong_t)bp->blk_birth,
119343466aaeSMax Grossman 		    (u_longlong_t)BP_PHYSICAL_BIRTH(bp));
119443466aaeSMax Grossman 	}
119544cd46caSbillm }
119644cd46caSbillm 
119788b7b0f2SMatthew Ahrens static void
11987802d7bfSMatthew Ahrens print_indirect(blkptr_t *bp, const zbookmark_phys_t *zb,
119988b7b0f2SMatthew Ahrens     const dnode_phys_t *dnp)
1200fa9e4066Sahrens {
120188b7b0f2SMatthew Ahrens 	char blkbuf[BP_SPRINTF_LEN];
1202fa9e4066Sahrens 	int l;
1203fa9e4066Sahrens 
12045d7b4d43SMatthew Ahrens 	if (!BP_IS_EMBEDDED(bp)) {
12055d7b4d43SMatthew Ahrens 		ASSERT3U(BP_GET_TYPE(bp), ==, dnp->dn_type);
12065d7b4d43SMatthew Ahrens 		ASSERT3U(BP_GET_LEVEL(bp), ==, zb->zb_level);
12075d7b4d43SMatthew Ahrens 	}
1208fa9e4066Sahrens 
1209b24ab676SJeff Bonwick 	(void) printf("%16llx ", (u_longlong_t)blkid2offset(dnp, bp, zb));
1210fa9e4066Sahrens 
1211fa9e4066Sahrens 	ASSERT(zb->zb_level >= 0);
1212fa9e4066Sahrens 
1213fa9e4066Sahrens 	for (l = dnp->dn_nlevels - 1; l >= -1; l--) {
1214fa9e4066Sahrens 		if (l == zb->zb_level) {
121588b7b0f2SMatthew Ahrens 			(void) printf("L%llx", (u_longlong_t)zb->zb_level);
1216fa9e4066Sahrens 		} else {
121788b7b0f2SMatthew Ahrens 			(void) printf(" ");
1218fa9e4066Sahrens 		}
1219fa9e4066Sahrens 	}
1220fa9e4066Sahrens 
122143466aaeSMax Grossman 	snprintf_blkptr_compact(blkbuf, sizeof (blkbuf), bp);
122288b7b0f2SMatthew Ahrens 	(void) printf("%s\n", blkbuf);
122388b7b0f2SMatthew Ahrens }
122488b7b0f2SMatthew Ahrens 
122588b7b0f2SMatthew Ahrens static int
122688b7b0f2SMatthew Ahrens visit_indirect(spa_t *spa, const dnode_phys_t *dnp,
12277802d7bfSMatthew Ahrens     blkptr_t *bp, const zbookmark_phys_t *zb)
122888b7b0f2SMatthew Ahrens {
1229e4161df6SVictor Latushkin 	int err = 0;
123088b7b0f2SMatthew Ahrens 
123188b7b0f2SMatthew Ahrens 	if (bp->blk_birth == 0)
123288b7b0f2SMatthew Ahrens 		return (0);
123388b7b0f2SMatthew Ahrens 
123488b7b0f2SMatthew Ahrens 	print_indirect(bp, zb, dnp);
123588b7b0f2SMatthew Ahrens 
123643466aaeSMax Grossman 	if (BP_GET_LEVEL(bp) > 0 && !BP_IS_HOLE(bp)) {
12377adb730bSGeorge Wilson 		arc_flags_t flags = ARC_FLAG_WAIT;
123888b7b0f2SMatthew Ahrens 		int i;
123988b7b0f2SMatthew Ahrens 		blkptr_t *cbp;
124088b7b0f2SMatthew Ahrens 		int epb = BP_GET_LSIZE(bp) >> SPA_BLKPTRSHIFT;
124188b7b0f2SMatthew Ahrens 		arc_buf_t *buf;
124288b7b0f2SMatthew Ahrens 		uint64_t fill = 0;
124388b7b0f2SMatthew Ahrens 
12441b912ec7SGeorge Wilson 		err = arc_read(NULL, spa, bp, arc_getbuf_func, &buf,
124588b7b0f2SMatthew Ahrens 		    ZIO_PRIORITY_ASYNC_READ, ZIO_FLAG_CANFAIL, &flags, zb);
124688b7b0f2SMatthew Ahrens 		if (err)
124788b7b0f2SMatthew Ahrens 			return (err);
12483f9d6ad7SLin Ling 		ASSERT(buf->b_data);
124988b7b0f2SMatthew Ahrens 
125088b7b0f2SMatthew Ahrens 		/* recursively visit blocks below this */
125188b7b0f2SMatthew Ahrens 		cbp = buf->b_data;
125288b7b0f2SMatthew Ahrens 		for (i = 0; i < epb; i++, cbp++) {
12537802d7bfSMatthew Ahrens 			zbookmark_phys_t czb;
125488b7b0f2SMatthew Ahrens 
125588b7b0f2SMatthew Ahrens 			SET_BOOKMARK(&czb, zb->zb_objset, zb->zb_object,
125688b7b0f2SMatthew Ahrens 			    zb->zb_level - 1,
125788b7b0f2SMatthew Ahrens 			    zb->zb_blkid * epb + i);
125888b7b0f2SMatthew Ahrens 			err = visit_indirect(spa, dnp, cbp, &czb);
125988b7b0f2SMatthew Ahrens 			if (err)
126088b7b0f2SMatthew Ahrens 				break;
12615d7b4d43SMatthew Ahrens 			fill += BP_GET_FILL(cbp);
126288b7b0f2SMatthew Ahrens 		}
12638ad4d6ddSJeff Bonwick 		if (!err)
12645d7b4d43SMatthew Ahrens 			ASSERT3U(fill, ==, BP_GET_FILL(bp));
126588b7b0f2SMatthew Ahrens 		(void) arc_buf_remove_ref(buf, &buf);
1266fa9e4066Sahrens 	}
1267fa9e4066Sahrens 
126888b7b0f2SMatthew Ahrens 	return (err);
1269fa9e4066Sahrens }
1270fa9e4066Sahrens 
1271fa9e4066Sahrens /*ARGSUSED*/
1272fa9e4066Sahrens static void
127388b7b0f2SMatthew Ahrens dump_indirect(dnode_t *dn)
1274fa9e4066Sahrens {
127588b7b0f2SMatthew Ahrens 	dnode_phys_t *dnp = dn->dn_phys;
127688b7b0f2SMatthew Ahrens 	int j;
12777802d7bfSMatthew Ahrens 	zbookmark_phys_t czb;
1278fa9e4066Sahrens 
1279fa9e4066Sahrens 	(void) printf("Indirect blocks:\n");
1280fa9e4066Sahrens 
1281503ad85cSMatthew Ahrens 	SET_BOOKMARK(&czb, dmu_objset_id(dn->dn_objset),
128288b7b0f2SMatthew Ahrens 	    dn->dn_object, dnp->dn_nlevels - 1, 0);
128388b7b0f2SMatthew Ahrens 	for (j = 0; j < dnp->dn_nblkptr; j++) {
128488b7b0f2SMatthew Ahrens 		czb.zb_blkid = j;
1285503ad85cSMatthew Ahrens 		(void) visit_indirect(dmu_objset_spa(dn->dn_objset), dnp,
128688b7b0f2SMatthew Ahrens 		    &dnp->dn_blkptr[j], &czb);
128788b7b0f2SMatthew Ahrens 	}
1288fa9e4066Sahrens 
1289fa9e4066Sahrens 	(void) printf("\n");
1290fa9e4066Sahrens }
1291fa9e4066Sahrens 
1292fa9e4066Sahrens /*ARGSUSED*/
1293fa9e4066Sahrens static void
1294fa9e4066Sahrens dump_dsl_dir(objset_t *os, uint64_t object, void *data, size_t size)
1295fa9e4066Sahrens {
1296fa9e4066Sahrens 	dsl_dir_phys_t *dd = data;
1297fa9e4066Sahrens 	time_t crtime;
12983f9d6ad7SLin Ling 	char nice[32];
1299fa9e4066Sahrens 
1300fa9e4066Sahrens 	if (dd == NULL)
1301fa9e4066Sahrens 		return;
1302fa9e4066Sahrens 
1303da6c28aaSamw 	ASSERT3U(size, >=, sizeof (dsl_dir_phys_t));
1304fa9e4066Sahrens 
1305fa9e4066Sahrens 	crtime = dd->dd_creation_time;
1306fa9e4066Sahrens 	(void) printf("\t\tcreation_time = %s", ctime(&crtime));
1307fa9e4066Sahrens 	(void) printf("\t\thead_dataset_obj = %llu\n",
1308fa9e4066Sahrens 	    (u_longlong_t)dd->dd_head_dataset_obj);
1309fa9e4066Sahrens 	(void) printf("\t\tparent_dir_obj = %llu\n",
1310fa9e4066Sahrens 	    (u_longlong_t)dd->dd_parent_obj);
13113cb34c60Sahrens 	(void) printf("\t\torigin_obj = %llu\n",
13123cb34c60Sahrens 	    (u_longlong_t)dd->dd_origin_obj);
1313fa9e4066Sahrens 	(void) printf("\t\tchild_dir_zapobj = %llu\n",
1314fa9e4066Sahrens 	    (u_longlong_t)dd->dd_child_dir_zapobj);
13153f9d6ad7SLin Ling 	zdb_nicenum(dd->dd_used_bytes, nice);
131674e7dc98SMatthew Ahrens 	(void) printf("\t\tused_bytes = %s\n", nice);
13173f9d6ad7SLin Ling 	zdb_nicenum(dd->dd_compressed_bytes, nice);
131874e7dc98SMatthew Ahrens 	(void) printf("\t\tcompressed_bytes = %s\n", nice);
13193f9d6ad7SLin Ling 	zdb_nicenum(dd->dd_uncompressed_bytes, nice);
132074e7dc98SMatthew Ahrens 	(void) printf("\t\tuncompressed_bytes = %s\n", nice);
13213f9d6ad7SLin Ling 	zdb_nicenum(dd->dd_quota, nice);
132274e7dc98SMatthew Ahrens 	(void) printf("\t\tquota = %s\n", nice);
13233f9d6ad7SLin Ling 	zdb_nicenum(dd->dd_reserved, nice);
132474e7dc98SMatthew Ahrens 	(void) printf("\t\treserved = %s\n", nice);
1325fa9e4066Sahrens 	(void) printf("\t\tprops_zapobj = %llu\n",
1326fa9e4066Sahrens 	    (u_longlong_t)dd->dd_props_zapobj);
1327ecd6cf80Smarks 	(void) printf("\t\tdeleg_zapobj = %llu\n",
1328ecd6cf80Smarks 	    (u_longlong_t)dd->dd_deleg_zapobj);
132974e7dc98SMatthew Ahrens 	(void) printf("\t\tflags = %llx\n",
133074e7dc98SMatthew Ahrens 	    (u_longlong_t)dd->dd_flags);
133174e7dc98SMatthew Ahrens 
133274e7dc98SMatthew Ahrens #define	DO(which) \
13333f9d6ad7SLin Ling 	zdb_nicenum(dd->dd_used_breakdown[DD_USED_ ## which], nice); \
133474e7dc98SMatthew Ahrens 	(void) printf("\t\tused_breakdown[" #which "] = %s\n", nice)
133574e7dc98SMatthew Ahrens 	DO(HEAD);
133674e7dc98SMatthew Ahrens 	DO(SNAP);
133774e7dc98SMatthew Ahrens 	DO(CHILD);
133874e7dc98SMatthew Ahrens 	DO(CHILD_RSRV);
133974e7dc98SMatthew Ahrens 	DO(REFRSRV);
134074e7dc98SMatthew Ahrens #undef DO
1341fa9e4066Sahrens }
1342fa9e4066Sahrens 
1343fa9e4066Sahrens /*ARGSUSED*/
1344fa9e4066Sahrens static void
1345fa9e4066Sahrens dump_dsl_dataset(objset_t *os, uint64_t object, void *data, size_t size)
1346fa9e4066Sahrens {
1347fa9e4066Sahrens 	dsl_dataset_phys_t *ds = data;
1348fa9e4066Sahrens 	time_t crtime;
13493f9d6ad7SLin Ling 	char used[32], compressed[32], uncompressed[32], unique[32];
1350fbabab8fSmaybee 	char blkbuf[BP_SPRINTF_LEN];
1351fa9e4066Sahrens 
1352fa9e4066Sahrens 	if (ds == NULL)
1353fa9e4066Sahrens 		return;
1354fa9e4066Sahrens 
1355fa9e4066Sahrens 	ASSERT(size == sizeof (*ds));
1356fa9e4066Sahrens 	crtime = ds->ds_creation_time;
1357ad135b5dSChristopher Siden 	zdb_nicenum(ds->ds_referenced_bytes, used);
13583f9d6ad7SLin Ling 	zdb_nicenum(ds->ds_compressed_bytes, compressed);
13593f9d6ad7SLin Ling 	zdb_nicenum(ds->ds_uncompressed_bytes, uncompressed);
13603f9d6ad7SLin Ling 	zdb_nicenum(ds->ds_unique_bytes, unique);
136143466aaeSMax Grossman 	snprintf_blkptr(blkbuf, sizeof (blkbuf), &ds->ds_bp);
1362fa9e4066Sahrens 
1363088f3894Sahrens 	(void) printf("\t\tdir_obj = %llu\n",
1364fa9e4066Sahrens 	    (u_longlong_t)ds->ds_dir_obj);
1365fa9e4066Sahrens 	(void) printf("\t\tprev_snap_obj = %llu\n",
1366fa9e4066Sahrens 	    (u_longlong_t)ds->ds_prev_snap_obj);
1367fa9e4066Sahrens 	(void) printf("\t\tprev_snap_txg = %llu\n",
1368fa9e4066Sahrens 	    (u_longlong_t)ds->ds_prev_snap_txg);
1369fa9e4066Sahrens 	(void) printf("\t\tnext_snap_obj = %llu\n",
1370fa9e4066Sahrens 	    (u_longlong_t)ds->ds_next_snap_obj);
1371fa9e4066Sahrens 	(void) printf("\t\tsnapnames_zapobj = %llu\n",
1372fa9e4066Sahrens 	    (u_longlong_t)ds->ds_snapnames_zapobj);
1373fa9e4066Sahrens 	(void) printf("\t\tnum_children = %llu\n",
1374fa9e4066Sahrens 	    (u_longlong_t)ds->ds_num_children);
1375842727c2SChris Kirby 	(void) printf("\t\tuserrefs_obj = %llu\n",
1376842727c2SChris Kirby 	    (u_longlong_t)ds->ds_userrefs_obj);
1377fa9e4066Sahrens 	(void) printf("\t\tcreation_time = %s", ctime(&crtime));
1378fa9e4066Sahrens 	(void) printf("\t\tcreation_txg = %llu\n",
1379fa9e4066Sahrens 	    (u_longlong_t)ds->ds_creation_txg);
1380fa9e4066Sahrens 	(void) printf("\t\tdeadlist_obj = %llu\n",
1381fa9e4066Sahrens 	    (u_longlong_t)ds->ds_deadlist_obj);
1382fa9e4066Sahrens 	(void) printf("\t\tused_bytes = %s\n", used);
1383fa9e4066Sahrens 	(void) printf("\t\tcompressed_bytes = %s\n", compressed);
1384fa9e4066Sahrens 	(void) printf("\t\tuncompressed_bytes = %s\n", uncompressed);
1385fa9e4066Sahrens 	(void) printf("\t\tunique = %s\n", unique);
1386fa9e4066Sahrens 	(void) printf("\t\tfsid_guid = %llu\n",
1387fa9e4066Sahrens 	    (u_longlong_t)ds->ds_fsid_guid);
1388fa9e4066Sahrens 	(void) printf("\t\tguid = %llu\n",
1389fa9e4066Sahrens 	    (u_longlong_t)ds->ds_guid);
139099653d4eSeschrock 	(void) printf("\t\tflags = %llx\n",
139199653d4eSeschrock 	    (u_longlong_t)ds->ds_flags);
1392088f3894Sahrens 	(void) printf("\t\tnext_clones_obj = %llu\n",
1393088f3894Sahrens 	    (u_longlong_t)ds->ds_next_clones_obj);
1394bb0ade09Sahrens 	(void) printf("\t\tprops_obj = %llu\n",
1395bb0ade09Sahrens 	    (u_longlong_t)ds->ds_props_obj);
1396fa9e4066Sahrens 	(void) printf("\t\tbp = %s\n", blkbuf);
1397fa9e4066Sahrens }
1398fa9e4066Sahrens 
1399ad135b5dSChristopher Siden /* ARGSUSED */
1400ad135b5dSChristopher Siden static int
1401ad135b5dSChristopher Siden dump_bptree_cb(void *arg, const blkptr_t *bp, dmu_tx_t *tx)
1402ad135b5dSChristopher Siden {
1403ad135b5dSChristopher Siden 	char blkbuf[BP_SPRINTF_LEN];
1404ad135b5dSChristopher Siden 
1405ad135b5dSChristopher Siden 	if (bp->blk_birth != 0) {
140643466aaeSMax Grossman 		snprintf_blkptr(blkbuf, sizeof (blkbuf), bp);
1407ad135b5dSChristopher Siden 		(void) printf("\t%s\n", blkbuf);
1408ad135b5dSChristopher Siden 	}
1409ad135b5dSChristopher Siden 	return (0);
1410ad135b5dSChristopher Siden }
1411ad135b5dSChristopher Siden 
1412ad135b5dSChristopher Siden static void
1413ad135b5dSChristopher Siden dump_bptree(objset_t *os, uint64_t obj, char *name)
1414ad135b5dSChristopher Siden {
1415ad135b5dSChristopher Siden 	char bytes[32];
1416ad135b5dSChristopher Siden 	bptree_phys_t *bt;
1417ad135b5dSChristopher Siden 	dmu_buf_t *db;
1418ad135b5dSChristopher Siden 
1419ad135b5dSChristopher Siden 	if (dump_opt['d'] < 3)
1420ad135b5dSChristopher Siden 		return;
1421ad135b5dSChristopher Siden 
1422b420f3adSRichard Lowe 	VERIFY3U(0, ==, dmu_bonus_hold(os, obj, FTAG, &db));
1423ad135b5dSChristopher Siden 	bt = db->db_data;
1424ad135b5dSChristopher Siden 	zdb_nicenum(bt->bt_bytes, bytes);
1425ad135b5dSChristopher Siden 	(void) printf("\n    %s: %llu datasets, %s\n",
1426ad135b5dSChristopher Siden 	    name, (unsigned long long)(bt->bt_end - bt->bt_begin), bytes);
1427ad135b5dSChristopher Siden 	dmu_buf_rele(db, FTAG);
1428ad135b5dSChristopher Siden 
1429ad135b5dSChristopher Siden 	if (dump_opt['d'] < 5)
1430ad135b5dSChristopher Siden 		return;
1431ad135b5dSChristopher Siden 
1432ad135b5dSChristopher Siden 	(void) printf("\n");
1433ad135b5dSChristopher Siden 
1434ad135b5dSChristopher Siden 	(void) bptree_iterate(os, obj, B_FALSE, dump_bptree_cb, NULL, NULL);
1435ad135b5dSChristopher Siden }
1436ad135b5dSChristopher Siden 
1437cde58dbcSMatthew Ahrens /* ARGSUSED */
1438cde58dbcSMatthew Ahrens static int
1439cde58dbcSMatthew Ahrens dump_bpobj_cb(void *arg, const blkptr_t *bp, dmu_tx_t *tx)
1440cde58dbcSMatthew Ahrens {
1441cde58dbcSMatthew Ahrens 	char blkbuf[BP_SPRINTF_LEN];
1442cde58dbcSMatthew Ahrens 
1443cde58dbcSMatthew Ahrens 	ASSERT(bp->blk_birth != 0);
144443466aaeSMax Grossman 	snprintf_blkptr_compact(blkbuf, sizeof (blkbuf), bp);
1445cde58dbcSMatthew Ahrens 	(void) printf("\t%s\n", blkbuf);
1446cde58dbcSMatthew Ahrens 	return (0);
1447cde58dbcSMatthew Ahrens }
1448cde58dbcSMatthew Ahrens 
1449fa9e4066Sahrens static void
1450*732885fcSMatthew Ahrens dump_full_bpobj(bpobj_t *bpo, char *name, int indent)
1451fa9e4066Sahrens {
14523f9d6ad7SLin Ling 	char bytes[32];
14533f9d6ad7SLin Ling 	char comp[32];
14543f9d6ad7SLin Ling 	char uncomp[32];
1455fa9e4066Sahrens 
1456fa9e4066Sahrens 	if (dump_opt['d'] < 3)
1457fa9e4066Sahrens 		return;
1458fa9e4066Sahrens 
1459cde58dbcSMatthew Ahrens 	zdb_nicenum(bpo->bpo_phys->bpo_bytes, bytes);
1460d0475637SMatthew Ahrens 	if (bpo->bpo_havesubobj && bpo->bpo_phys->bpo_subobjs != 0) {
1461cde58dbcSMatthew Ahrens 		zdb_nicenum(bpo->bpo_phys->bpo_comp, comp);
1462cde58dbcSMatthew Ahrens 		zdb_nicenum(bpo->bpo_phys->bpo_uncomp, uncomp);
1463d0475637SMatthew Ahrens 		(void) printf("    %*s: object %llu, %llu local blkptrs, "
1464*732885fcSMatthew Ahrens 		    "%llu subobjs in object %llu, %s (%s/%s comp)\n",
1465d0475637SMatthew Ahrens 		    indent * 8, name,
1466d0475637SMatthew Ahrens 		    (u_longlong_t)bpo->bpo_object,
1467d0475637SMatthew Ahrens 		    (u_longlong_t)bpo->bpo_phys->bpo_num_blkptrs,
1468cde58dbcSMatthew Ahrens 		    (u_longlong_t)bpo->bpo_phys->bpo_num_subobjs,
1469*732885fcSMatthew Ahrens 		    (u_longlong_t)bpo->bpo_phys->bpo_subobjs,
147099653d4eSeschrock 		    bytes, comp, uncomp);
1471d0475637SMatthew Ahrens 
1472d0475637SMatthew Ahrens 		for (uint64_t i = 0; i < bpo->bpo_phys->bpo_num_subobjs; i++) {
1473d0475637SMatthew Ahrens 			uint64_t subobj;
1474d0475637SMatthew Ahrens 			bpobj_t subbpo;
1475d0475637SMatthew Ahrens 			int error;
1476d0475637SMatthew Ahrens 			VERIFY0(dmu_read(bpo->bpo_os,
1477d0475637SMatthew Ahrens 			    bpo->bpo_phys->bpo_subobjs,
1478d0475637SMatthew Ahrens 			    i * sizeof (subobj), sizeof (subobj), &subobj, 0));
1479d0475637SMatthew Ahrens 			error = bpobj_open(&subbpo, bpo->bpo_os, subobj);
1480d0475637SMatthew Ahrens 			if (error != 0) {
1481d0475637SMatthew Ahrens 				(void) printf("ERROR %u while trying to open "
1482d0475637SMatthew Ahrens 				    "subobj id %llu\n",
1483d0475637SMatthew Ahrens 				    error, (u_longlong_t)subobj);
1484d0475637SMatthew Ahrens 				continue;
1485d0475637SMatthew Ahrens 			}
1486*732885fcSMatthew Ahrens 			dump_full_bpobj(&subbpo, "subobj", indent + 1);
148777061867SMatthew Ahrens 			bpobj_close(&subbpo);
1488d0475637SMatthew Ahrens 		}
148999653d4eSeschrock 	} else {
1490d0475637SMatthew Ahrens 		(void) printf("    %*s: object %llu, %llu blkptrs, %s\n",
1491d0475637SMatthew Ahrens 		    indent * 8, name,
1492d0475637SMatthew Ahrens 		    (u_longlong_t)bpo->bpo_object,
1493d0475637SMatthew Ahrens 		    (u_longlong_t)bpo->bpo_phys->bpo_num_blkptrs,
1494d0475637SMatthew Ahrens 		    bytes);
149599653d4eSeschrock 	}
1496fa9e4066Sahrens 
1497cde58dbcSMatthew Ahrens 	if (dump_opt['d'] < 5)
1498fa9e4066Sahrens 		return;
1499fa9e4066Sahrens 
1500fa9e4066Sahrens 
1501d0475637SMatthew Ahrens 	if (indent == 0) {
1502d0475637SMatthew Ahrens 		(void) bpobj_iterate_nofree(bpo, dump_bpobj_cb, NULL, NULL);
1503d0475637SMatthew Ahrens 		(void) printf("\n");
1504d0475637SMatthew Ahrens 	}
1505cde58dbcSMatthew Ahrens }
150644cd46caSbillm 
1507cde58dbcSMatthew Ahrens static void
1508cde58dbcSMatthew Ahrens dump_deadlist(dsl_deadlist_t *dl)
1509cde58dbcSMatthew Ahrens {
1510cde58dbcSMatthew Ahrens 	dsl_deadlist_entry_t *dle;
1511d0475637SMatthew Ahrens 	uint64_t unused;
1512cde58dbcSMatthew Ahrens 	char bytes[32];
1513cde58dbcSMatthew Ahrens 	char comp[32];
1514cde58dbcSMatthew Ahrens 	char uncomp[32];
1515cde58dbcSMatthew Ahrens 
1516cde58dbcSMatthew Ahrens 	if (dump_opt['d'] < 3)
1517cde58dbcSMatthew Ahrens 		return;
1518cde58dbcSMatthew Ahrens 
151990c76c66SMatthew Ahrens 	if (dl->dl_oldfmt) {
1520*732885fcSMatthew Ahrens 		dump_full_bpobj(&dl->dl_bpobj, "old-format deadlist", 0);
152190c76c66SMatthew Ahrens 		return;
152290c76c66SMatthew Ahrens 	}
152390c76c66SMatthew Ahrens 
1524cde58dbcSMatthew Ahrens 	zdb_nicenum(dl->dl_phys->dl_used, bytes);
1525cde58dbcSMatthew Ahrens 	zdb_nicenum(dl->dl_phys->dl_comp, comp);
1526cde58dbcSMatthew Ahrens 	zdb_nicenum(dl->dl_phys->dl_uncomp, uncomp);
1527cde58dbcSMatthew Ahrens 	(void) printf("\n    Deadlist: %s (%s/%s comp)\n",
1528cde58dbcSMatthew Ahrens 	    bytes, comp, uncomp);
1529cde58dbcSMatthew Ahrens 
1530cde58dbcSMatthew Ahrens 	if (dump_opt['d'] < 4)
1531cde58dbcSMatthew Ahrens 		return;
1532cde58dbcSMatthew Ahrens 
1533cde58dbcSMatthew Ahrens 	(void) printf("\n");
1534cde58dbcSMatthew Ahrens 
1535d0475637SMatthew Ahrens 	/* force the tree to be loaded */
1536d0475637SMatthew Ahrens 	dsl_deadlist_space_range(dl, 0, UINT64_MAX, &unused, &unused, &unused);
1537d0475637SMatthew Ahrens 
1538cde58dbcSMatthew Ahrens 	for (dle = avl_first(&dl->dl_tree); dle;
1539cde58dbcSMatthew Ahrens 	    dle = AVL_NEXT(&dl->dl_tree, dle)) {
1540d0475637SMatthew Ahrens 		if (dump_opt['d'] >= 5) {
1541d0475637SMatthew Ahrens 			char buf[128];
1542d0475637SMatthew Ahrens 			(void) snprintf(buf, sizeof (buf), "mintxg %llu -> ",
1543d0475637SMatthew Ahrens 			    (longlong_t)dle->dle_mintxg,
1544d0475637SMatthew Ahrens 			    (longlong_t)dle->dle_bpobj.bpo_object);
1545fa9e4066Sahrens 
1546*732885fcSMatthew Ahrens 			dump_full_bpobj(&dle->dle_bpobj, buf, 0);
1547d0475637SMatthew Ahrens 		} else {
1548d0475637SMatthew Ahrens 			(void) printf("mintxg %llu -> obj %llu\n",
1549d0475637SMatthew Ahrens 			    (longlong_t)dle->dle_mintxg,
1550d0475637SMatthew Ahrens 			    (longlong_t)dle->dle_bpobj.bpo_object);
1551d0475637SMatthew Ahrens 
1552d0475637SMatthew Ahrens 		}
1553cde58dbcSMatthew Ahrens 	}
1554fa9e4066Sahrens }
1555fa9e4066Sahrens 
1556e0d35c44Smarks static avl_tree_t idx_tree;
1557e0d35c44Smarks static avl_tree_t domain_tree;
1558e0d35c44Smarks static boolean_t fuid_table_loaded;
15590a586ceaSMark Shellenbaum static boolean_t sa_loaded;
15600a586ceaSMark Shellenbaum sa_attr_type_t *sa_attr_table;
1561e0d35c44Smarks 
1562e0d35c44Smarks static void
1563e0d35c44Smarks fuid_table_destroy()
1564e0d35c44Smarks {
1565e0d35c44Smarks 	if (fuid_table_loaded) {
1566e0d35c44Smarks 		zfs_fuid_table_destroy(&idx_tree, &domain_tree);
1567e0d35c44Smarks 		fuid_table_loaded = B_FALSE;
1568e0d35c44Smarks 	}
1569e0d35c44Smarks }
1570e0d35c44Smarks 
1571e0d35c44Smarks /*
1572e0d35c44Smarks  * print uid or gid information.
1573e0d35c44Smarks  * For normal POSIX id just the id is printed in decimal format.
1574e0d35c44Smarks  * For CIFS files with FUID the fuid is printed in hex followed by
1575d0475637SMatthew Ahrens  * the domain-rid string.
1576e0d35c44Smarks  */
1577e0d35c44Smarks static void
1578e0d35c44Smarks print_idstr(uint64_t id, const char *id_type)
1579e0d35c44Smarks {
1580e0d35c44Smarks 	if (FUID_INDEX(id)) {
1581e0d35c44Smarks 		char *domain;
1582e0d35c44Smarks 
1583e0d35c44Smarks 		domain = zfs_fuid_idx_domain(&idx_tree, FUID_INDEX(id));
1584e0d35c44Smarks 		(void) printf("\t%s     %llx [%s-%d]\n", id_type,
1585e0d35c44Smarks 		    (u_longlong_t)id, domain, (int)FUID_RID(id));
1586e0d35c44Smarks 	} else {
1587e0d35c44Smarks 		(void) printf("\t%s     %llu\n", id_type, (u_longlong_t)id);
1588e0d35c44Smarks 	}
1589e0d35c44Smarks 
1590e0d35c44Smarks }
1591e0d35c44Smarks 
1592e0d35c44Smarks static void
15930a586ceaSMark Shellenbaum dump_uidgid(objset_t *os, uint64_t uid, uint64_t gid)
1594e0d35c44Smarks {
1595e0d35c44Smarks 	uint32_t uid_idx, gid_idx;
1596e0d35c44Smarks 
15970a586ceaSMark Shellenbaum 	uid_idx = FUID_INDEX(uid);
15980a586ceaSMark Shellenbaum 	gid_idx = FUID_INDEX(gid);
1599e0d35c44Smarks 
1600e0d35c44Smarks 	/* Load domain table, if not already loaded */
1601e0d35c44Smarks 	if (!fuid_table_loaded && (uid_idx || gid_idx)) {
1602e0d35c44Smarks 		uint64_t fuid_obj;
1603e0d35c44Smarks 
1604e0d35c44Smarks 		/* first find the fuid object.  It lives in the master node */
1605e0d35c44Smarks 		VERIFY(zap_lookup(os, MASTER_NODE_OBJ, ZFS_FUID_TABLES,
1606e0d35c44Smarks 		    8, 1, &fuid_obj) == 0);
160789459e17SMark Shellenbaum 		zfs_fuid_avl_tree_create(&idx_tree, &domain_tree);
1608e0d35c44Smarks 		(void) zfs_fuid_table_load(os, fuid_obj,
1609e0d35c44Smarks 		    &idx_tree, &domain_tree);
1610e0d35c44Smarks 		fuid_table_loaded = B_TRUE;
1611e0d35c44Smarks 	}
1612e0d35c44Smarks 
16130a586ceaSMark Shellenbaum 	print_idstr(uid, "uid");
16140a586ceaSMark Shellenbaum 	print_idstr(gid, "gid");
1615e0d35c44Smarks }
1616e0d35c44Smarks 
1617fa9e4066Sahrens /*ARGSUSED*/
1618fa9e4066Sahrens static void
1619fa9e4066Sahrens dump_znode(objset_t *os, uint64_t object, void *data, size_t size)
1620fa9e4066Sahrens {
1621fa9e4066Sahrens 	char path[MAXPATHLEN * 2];	/* allow for xattr and failure prefix */
16220a586ceaSMark Shellenbaum 	sa_handle_t *hdl;
16230a586ceaSMark Shellenbaum 	uint64_t xattr, rdev, gen;
16240a586ceaSMark Shellenbaum 	uint64_t uid, gid, mode, fsize, parent, links;
16258f2529deSMark Shellenbaum 	uint64_t pflags;
16260a586ceaSMark Shellenbaum 	uint64_t acctm[2], modtm[2], chgtm[2], crtm[2];
16270a586ceaSMark Shellenbaum 	time_t z_crtime, z_atime, z_mtime, z_ctime;
16288f2529deSMark Shellenbaum 	sa_bulk_attr_t bulk[12];
16290a586ceaSMark Shellenbaum 	int idx = 0;
163055434c77Sek 	int error;
1631fa9e4066Sahrens 
16320a586ceaSMark Shellenbaum 	if (!sa_loaded) {
16330a586ceaSMark Shellenbaum 		uint64_t sa_attrs = 0;
16340a586ceaSMark Shellenbaum 		uint64_t version;
16350a586ceaSMark Shellenbaum 
16360a586ceaSMark Shellenbaum 		VERIFY(zap_lookup(os, MASTER_NODE_OBJ, ZPL_VERSION_STR,
16370a586ceaSMark Shellenbaum 		    8, 1, &version) == 0);
16380a586ceaSMark Shellenbaum 		if (version >= ZPL_VERSION_SA) {
16390a586ceaSMark Shellenbaum 			VERIFY(zap_lookup(os, MASTER_NODE_OBJ, ZFS_SA_ATTRS,
16400a586ceaSMark Shellenbaum 			    8, 1, &sa_attrs) == 0);
16410a586ceaSMark Shellenbaum 		}
16421d8ccc7bSMark Shellenbaum 		if ((error = sa_setup(os, sa_attrs, zfs_attr_table,
16431d8ccc7bSMark Shellenbaum 		    ZPL_END, &sa_attr_table)) != 0) {
16441d8ccc7bSMark Shellenbaum 			(void) printf("sa_setup failed errno %d, can't "
16451d8ccc7bSMark Shellenbaum 			    "display znode contents\n", error);
16461d8ccc7bSMark Shellenbaum 			return;
16471d8ccc7bSMark Shellenbaum 		}
16480a586ceaSMark Shellenbaum 		sa_loaded = B_TRUE;
16490a586ceaSMark Shellenbaum 	}
16500a586ceaSMark Shellenbaum 
16510a586ceaSMark Shellenbaum 	if (sa_handle_get(os, object, NULL, SA_HDL_PRIVATE, &hdl)) {
16520a586ceaSMark Shellenbaum 		(void) printf("Failed to get handle for SA znode\n");
16530a586ceaSMark Shellenbaum 		return;
16540a586ceaSMark Shellenbaum 	}
16550a586ceaSMark Shellenbaum 
16560a586ceaSMark Shellenbaum 	SA_ADD_BULK_ATTR(bulk, idx, sa_attr_table[ZPL_UID], NULL, &uid, 8);
16570a586ceaSMark Shellenbaum 	SA_ADD_BULK_ATTR(bulk, idx, sa_attr_table[ZPL_GID], NULL, &gid, 8);
16580a586ceaSMark Shellenbaum 	SA_ADD_BULK_ATTR(bulk, idx, sa_attr_table[ZPL_LINKS], NULL,
16590a586ceaSMark Shellenbaum 	    &links, 8);
16600a586ceaSMark Shellenbaum 	SA_ADD_BULK_ATTR(bulk, idx, sa_attr_table[ZPL_GEN], NULL, &gen, 8);
16610a586ceaSMark Shellenbaum 	SA_ADD_BULK_ATTR(bulk, idx, sa_attr_table[ZPL_MODE], NULL,
16620a586ceaSMark Shellenbaum 	    &mode, 8);
16630a586ceaSMark Shellenbaum 	SA_ADD_BULK_ATTR(bulk, idx, sa_attr_table[ZPL_PARENT],
16640a586ceaSMark Shellenbaum 	    NULL, &parent, 8);
16650a586ceaSMark Shellenbaum 	SA_ADD_BULK_ATTR(bulk, idx, sa_attr_table[ZPL_SIZE], NULL,
16660a586ceaSMark Shellenbaum 	    &fsize, 8);
16670a586ceaSMark Shellenbaum 	SA_ADD_BULK_ATTR(bulk, idx, sa_attr_table[ZPL_ATIME], NULL,
16680a586ceaSMark Shellenbaum 	    acctm, 16);
16690a586ceaSMark Shellenbaum 	SA_ADD_BULK_ATTR(bulk, idx, sa_attr_table[ZPL_MTIME], NULL,
16700a586ceaSMark Shellenbaum 	    modtm, 16);
16710a586ceaSMark Shellenbaum 	SA_ADD_BULK_ATTR(bulk, idx, sa_attr_table[ZPL_CRTIME], NULL,
16720a586ceaSMark Shellenbaum 	    crtm, 16);
16730a586ceaSMark Shellenbaum 	SA_ADD_BULK_ATTR(bulk, idx, sa_attr_table[ZPL_CTIME], NULL,
16740a586ceaSMark Shellenbaum 	    chgtm, 16);
16758f2529deSMark Shellenbaum 	SA_ADD_BULK_ATTR(bulk, idx, sa_attr_table[ZPL_FLAGS], NULL,
16768f2529deSMark Shellenbaum 	    &pflags, 8);
16770a586ceaSMark Shellenbaum 
16780a586ceaSMark Shellenbaum 	if (sa_bulk_lookup(hdl, bulk, idx)) {
16790a586ceaSMark Shellenbaum 		(void) sa_handle_destroy(hdl);
16800a586ceaSMark Shellenbaum 		return;
16810a586ceaSMark Shellenbaum 	}
1682fa9e4066Sahrens 
168355434c77Sek 	error = zfs_obj_to_path(os, object, path, sizeof (path));
168455434c77Sek 	if (error != 0) {
168555434c77Sek 		(void) snprintf(path, sizeof (path), "\?\?\?<object#%llu>",
168655434c77Sek 		    (u_longlong_t)object);
168755434c77Sek 	}
1688fa9e4066Sahrens 	if (dump_opt['d'] < 3) {
168955434c77Sek 		(void) printf("\t%s\n", path);
16900a586ceaSMark Shellenbaum 		(void) sa_handle_destroy(hdl);
1691fa9e4066Sahrens 		return;
1692fa9e4066Sahrens 	}
1693fa9e4066Sahrens 
16940a586ceaSMark Shellenbaum 	z_crtime = (time_t)crtm[0];
16950a586ceaSMark Shellenbaum 	z_atime = (time_t)acctm[0];
16960a586ceaSMark Shellenbaum 	z_mtime = (time_t)modtm[0];
16970a586ceaSMark Shellenbaum 	z_ctime = (time_t)chgtm[0];
1698fa9e4066Sahrens 
169955434c77Sek 	(void) printf("\tpath	%s\n", path);
17000a586ceaSMark Shellenbaum 	dump_uidgid(os, uid, gid);
1701fa9e4066Sahrens 	(void) printf("\tatime	%s", ctime(&z_atime));
1702fa9e4066Sahrens 	(void) printf("\tmtime	%s", ctime(&z_mtime));
1703fa9e4066Sahrens 	(void) printf("\tctime	%s", ctime(&z_ctime));
1704fa9e4066Sahrens 	(void) printf("\tcrtime	%s", ctime(&z_crtime));
17050a586ceaSMark Shellenbaum 	(void) printf("\tgen	%llu\n", (u_longlong_t)gen);
17060a586ceaSMark Shellenbaum 	(void) printf("\tmode	%llo\n", (u_longlong_t)mode);
17070a586ceaSMark Shellenbaum 	(void) printf("\tsize	%llu\n", (u_longlong_t)fsize);
17080a586ceaSMark Shellenbaum 	(void) printf("\tparent	%llu\n", (u_longlong_t)parent);
17090a586ceaSMark Shellenbaum 	(void) printf("\tlinks	%llu\n", (u_longlong_t)links);
17108f2529deSMark Shellenbaum 	(void) printf("\tpflags	%llx\n", (u_longlong_t)pflags);
17110a586ceaSMark Shellenbaum 	if (sa_lookup(hdl, sa_attr_table[ZPL_XATTR], &xattr,
17120a586ceaSMark Shellenbaum 	    sizeof (uint64_t)) == 0)
17130a586ceaSMark Shellenbaum 		(void) printf("\txattr	%llu\n", (u_longlong_t)xattr);
17140a586ceaSMark Shellenbaum 	if (sa_lookup(hdl, sa_attr_table[ZPL_RDEV], &rdev,
17150a586ceaSMark Shellenbaum 	    sizeof (uint64_t)) == 0)
17160a586ceaSMark Shellenbaum 		(void) printf("\trdev	0x%016llx\n", (u_longlong_t)rdev);
17170a586ceaSMark Shellenbaum 	sa_handle_destroy(hdl);
1718fa9e4066Sahrens }
1719fa9e4066Sahrens 
1720fa9e4066Sahrens /*ARGSUSED*/
1721fa9e4066Sahrens static void
1722fa9e4066Sahrens dump_acl(objset_t *os, uint64_t object, void *data, size_t size)
1723fa9e4066Sahrens {
1724fa9e4066Sahrens }
1725fa9e4066Sahrens 
1726fa9e4066Sahrens /*ARGSUSED*/
1727fa9e4066Sahrens static void
1728fa9e4066Sahrens dump_dmu_objset(objset_t *os, uint64_t object, void *data, size_t size)
1729fa9e4066Sahrens {
1730fa9e4066Sahrens }
1731fa9e4066Sahrens 
17326de8f417SVictor Latushkin static object_viewer_t *object_viewer[DMU_OT_NUMTYPES + 1] = {
1733fa9e4066Sahrens 	dump_none,		/* unallocated			*/
1734fa9e4066Sahrens 	dump_zap,		/* object directory		*/
1735fa9e4066Sahrens 	dump_uint64,		/* object array			*/
1736fa9e4066Sahrens 	dump_none,		/* packed nvlist		*/
1737fa9e4066Sahrens 	dump_packed_nvlist,	/* packed nvlist size		*/
1738*732885fcSMatthew Ahrens 	dump_none,		/* bpobj			*/
1739*732885fcSMatthew Ahrens 	dump_bpobj,		/* bpobj header			*/
1740fa9e4066Sahrens 	dump_none,		/* SPA space map header		*/
1741fa9e4066Sahrens 	dump_none,		/* SPA space map		*/
1742fa9e4066Sahrens 	dump_none,		/* ZIL intent log		*/
1743fa9e4066Sahrens 	dump_dnode,		/* DMU dnode			*/
1744fa9e4066Sahrens 	dump_dmu_objset,	/* DMU objset			*/
1745ea8dc4b6Seschrock 	dump_dsl_dir,		/* DSL directory		*/
1746fa9e4066Sahrens 	dump_zap,		/* DSL directory child map	*/
1747fa9e4066Sahrens 	dump_zap,		/* DSL dataset snap map		*/
1748fa9e4066Sahrens 	dump_zap,		/* DSL props			*/
1749fa9e4066Sahrens 	dump_dsl_dataset,	/* DSL dataset			*/
1750fa9e4066Sahrens 	dump_znode,		/* ZFS znode			*/
1751da6c28aaSamw 	dump_acl,		/* ZFS V0 ACL			*/
1752fa9e4066Sahrens 	dump_uint8,		/* ZFS plain file		*/
1753e7437265Sahrens 	dump_zpldir,		/* ZFS directory		*/
1754fa9e4066Sahrens 	dump_zap,		/* ZFS master node		*/
1755fa9e4066Sahrens 	dump_zap,		/* ZFS delete queue		*/
1756fa9e4066Sahrens 	dump_uint8,		/* zvol object			*/
1757fa9e4066Sahrens 	dump_zap,		/* zvol prop			*/
1758fa9e4066Sahrens 	dump_uint8,		/* other uint8[]		*/
1759fa9e4066Sahrens 	dump_uint64,		/* other uint64[]		*/
1760fa9e4066Sahrens 	dump_zap,		/* other ZAP			*/
1761ea8dc4b6Seschrock 	dump_zap,		/* persistent error log		*/
176206eeb2adSek 	dump_uint8,		/* SPA history			*/
17634445fffbSMatthew Ahrens 	dump_history_offsets,	/* SPA history offsets		*/
1764b1b8ab34Slling 	dump_zap,		/* Pool properties		*/
1765ecd6cf80Smarks 	dump_zap,		/* DSL permissions		*/
1766da6c28aaSamw 	dump_acl,		/* ZFS ACL			*/
1767da6c28aaSamw 	dump_uint8,		/* ZFS SYSACL			*/
1768da6c28aaSamw 	dump_none,		/* FUID nvlist			*/
1769da6c28aaSamw 	dump_packed_nvlist,	/* FUID nvlist size		*/
1770088f3894Sahrens 	dump_zap,		/* DSL dataset next clones	*/
1771088f3894Sahrens 	dump_zap,		/* DSL scrub queue		*/
177214843421SMatthew Ahrens 	dump_zap,		/* ZFS user/group used		*/
177314843421SMatthew Ahrens 	dump_zap,		/* ZFS user/group quota		*/
1774842727c2SChris Kirby 	dump_zap,		/* snapshot refcount tags	*/
1775486ae710SMatthew Ahrens 	dump_ddt_zap,		/* DDT ZAP object		*/
1776b24ab676SJeff Bonwick 	dump_zap,		/* DDT statistics		*/
17770a586ceaSMark Shellenbaum 	dump_znode,		/* SA object			*/
17780a586ceaSMark Shellenbaum 	dump_zap,		/* SA Master Node		*/
17790a586ceaSMark Shellenbaum 	dump_sa_attrs,		/* SA attribute registration	*/
17800a586ceaSMark Shellenbaum 	dump_sa_layouts,	/* SA attribute layouts		*/
17813f9d6ad7SLin Ling 	dump_zap,		/* DSL scrub translations	*/
17823f9d6ad7SLin Ling 	dump_none,		/* fake dedup BP		*/
1783cde58dbcSMatthew Ahrens 	dump_zap,		/* deadlist			*/
1784cde58dbcSMatthew Ahrens 	dump_none,		/* deadlist hdr			*/
1785cde58dbcSMatthew Ahrens 	dump_zap,		/* dsl clones			*/
1786*732885fcSMatthew Ahrens 	dump_bpobj_subobjs,	/* bpobj subobjs		*/
17870a586ceaSMark Shellenbaum 	dump_unknown,		/* Unknown type, must be last	*/
1788fa9e4066Sahrens };
1789fa9e4066Sahrens 
1790fa9e4066Sahrens static void
1791fa9e4066Sahrens dump_object(objset_t *os, uint64_t object, int verbosity, int *print_header)
1792fa9e4066Sahrens {
1793fa9e4066Sahrens 	dmu_buf_t *db = NULL;
1794fa9e4066Sahrens 	dmu_object_info_t doi;
1795fa9e4066Sahrens 	dnode_t *dn;
1796fa9e4066Sahrens 	void *bonus = NULL;
1797fa9e4066Sahrens 	size_t bsize = 0;
17983f9d6ad7SLin Ling 	char iblk[32], dblk[32], lsize[32], asize[32], fill[32];
17993f9d6ad7SLin Ling 	char bonus_size[32];
1800fa9e4066Sahrens 	char aux[50];
1801fa9e4066Sahrens 	int error;
1802fa9e4066Sahrens 
1803fa9e4066Sahrens 	if (*print_header) {
1804b24ab676SJeff Bonwick 		(void) printf("\n%10s  %3s  %5s  %5s  %5s  %5s  %6s  %s\n",
1805b24ab676SJeff Bonwick 		    "Object", "lvl", "iblk", "dblk", "dsize", "lsize",
1806b24ab676SJeff Bonwick 		    "%full", "type");
1807fa9e4066Sahrens 		*print_header = 0;
1808fa9e4066Sahrens 	}
1809fa9e4066Sahrens 
1810fa9e4066Sahrens 	if (object == 0) {
1811744947dcSTom Erickson 		dn = DMU_META_DNODE(os);
1812fa9e4066Sahrens 	} else {
1813ea8dc4b6Seschrock 		error = dmu_bonus_hold(os, object, FTAG, &db);
1814ea8dc4b6Seschrock 		if (error)
1815ea8dc4b6Seschrock 			fatal("dmu_bonus_hold(%llu) failed, errno %u",
1816ea8dc4b6Seschrock 			    object, error);
1817fa9e4066Sahrens 		bonus = db->db_data;
1818fa9e4066Sahrens 		bsize = db->db_size;
1819744947dcSTom Erickson 		dn = DB_DNODE((dmu_buf_impl_t *)db);
1820fa9e4066Sahrens 	}
1821fa9e4066Sahrens 	dmu_object_info_from_dnode(dn, &doi);
1822fa9e4066Sahrens 
18233f9d6ad7SLin Ling 	zdb_nicenum(doi.doi_metadata_block_size, iblk);
18243f9d6ad7SLin Ling 	zdb_nicenum(doi.doi_data_block_size, dblk);
18253f9d6ad7SLin Ling 	zdb_nicenum(doi.doi_max_offset, lsize);
18263f9d6ad7SLin Ling 	zdb_nicenum(doi.doi_physical_blocks_512 << 9, asize);
18273f9d6ad7SLin Ling 	zdb_nicenum(doi.doi_bonus_size, bonus_size);
1828b24ab676SJeff Bonwick 	(void) sprintf(fill, "%6.2f", 100.0 * doi.doi_fill_count *
1829bbfd46c4SJeff Bonwick 	    doi.doi_data_block_size / (object == 0 ? DNODES_PER_BLOCK : 1) /
1830bbfd46c4SJeff Bonwick 	    doi.doi_max_offset);
1831fa9e4066Sahrens 
1832fa9e4066Sahrens 	aux[0] = '\0';
1833fa9e4066Sahrens 
1834e7437265Sahrens 	if (doi.doi_checksum != ZIO_CHECKSUM_INHERIT || verbosity >= 6) {
1835fa9e4066Sahrens 		(void) snprintf(aux + strlen(aux), sizeof (aux), " (K=%s)",
18366de8f417SVictor Latushkin 		    ZDB_CHECKSUM_NAME(doi.doi_checksum));
1837e7437265Sahrens 	}
1838fa9e4066Sahrens 
1839e7437265Sahrens 	if (doi.doi_compress != ZIO_COMPRESS_INHERIT || verbosity >= 6) {
1840fa9e4066Sahrens 		(void) snprintf(aux + strlen(aux), sizeof (aux), " (Z=%s)",
18416de8f417SVictor Latushkin 		    ZDB_COMPRESS_NAME(doi.doi_compress));
1842e7437265Sahrens 	}
1843fa9e4066Sahrens 
1844b24ab676SJeff Bonwick 	(void) printf("%10lld  %3u  %5s  %5s  %5s  %5s  %6s  %s%s\n",
1845b24ab676SJeff Bonwick 	    (u_longlong_t)object, doi.doi_indirection, iblk, dblk,
1846b24ab676SJeff Bonwick 	    asize, lsize, fill, ZDB_OT_NAME(doi.doi_type), aux);
1847fa9e4066Sahrens 
1848fa9e4066Sahrens 	if (doi.doi_bonus_type != DMU_OT_NONE && verbosity > 3) {
1849b24ab676SJeff Bonwick 		(void) printf("%10s  %3s  %5s  %5s  %5s  %5s  %6s  %s\n",
1850b24ab676SJeff Bonwick 		    "", "", "", "", "", bonus_size, "bonus",
18516de8f417SVictor Latushkin 		    ZDB_OT_NAME(doi.doi_bonus_type));
1852fa9e4066Sahrens 	}
1853fa9e4066Sahrens 
1854fa9e4066Sahrens 	if (verbosity >= 4) {
18550a586ceaSMark Shellenbaum 		(void) printf("\tdnode flags: %s%s%s\n",
185614843421SMatthew Ahrens 		    (dn->dn_phys->dn_flags & DNODE_FLAG_USED_BYTES) ?
185714843421SMatthew Ahrens 		    "USED_BYTES " : "",
185814843421SMatthew Ahrens 		    (dn->dn_phys->dn_flags & DNODE_FLAG_USERUSED_ACCOUNTED) ?
18590a586ceaSMark Shellenbaum 		    "USERUSED_ACCOUNTED " : "",
18600a586ceaSMark Shellenbaum 		    (dn->dn_phys->dn_flags & DNODE_FLAG_SPILL_BLKPTR) ?
18610a586ceaSMark Shellenbaum 		    "SPILL_BLKPTR" : "");
186214843421SMatthew Ahrens 		(void) printf("\tdnode maxblkid: %llu\n",
186314843421SMatthew Ahrens 		    (longlong_t)dn->dn_phys->dn_maxblkid);
186414843421SMatthew Ahrens 
18656de8f417SVictor Latushkin 		object_viewer[ZDB_OT_TYPE(doi.doi_bonus_type)](os, object,
18666de8f417SVictor Latushkin 		    bonus, bsize);
18676de8f417SVictor Latushkin 		object_viewer[ZDB_OT_TYPE(doi.doi_type)](os, object, NULL, 0);
1868fa9e4066Sahrens 		*print_header = 1;
1869fa9e4066Sahrens 	}
1870fa9e4066Sahrens 
1871fa9e4066Sahrens 	if (verbosity >= 5)
187288b7b0f2SMatthew Ahrens 		dump_indirect(dn);
1873fa9e4066Sahrens 
1874fa9e4066Sahrens 	if (verbosity >= 5) {
1875fa9e4066Sahrens 		/*
1876fa9e4066Sahrens 		 * Report the list of segments that comprise the object.
1877fa9e4066Sahrens 		 */
1878fa9e4066Sahrens 		uint64_t start = 0;
1879fa9e4066Sahrens 		uint64_t end;
1880fa9e4066Sahrens 		uint64_t blkfill = 1;
1881fa9e4066Sahrens 		int minlvl = 1;
1882fa9e4066Sahrens 
1883fa9e4066Sahrens 		if (dn->dn_type == DMU_OT_DNODE) {
1884fa9e4066Sahrens 			minlvl = 0;
1885fa9e4066Sahrens 			blkfill = DNODES_PER_BLOCK;
1886fa9e4066Sahrens 		}
1887fa9e4066Sahrens 
1888fa9e4066Sahrens 		for (;;) {
18893f9d6ad7SLin Ling 			char segsize[32];
1890cdb0ab79Smaybee 			error = dnode_next_offset(dn,
1891cdb0ab79Smaybee 			    0, &start, minlvl, blkfill, 0);
1892fa9e4066Sahrens 			if (error)
1893fa9e4066Sahrens 				break;
1894fa9e4066Sahrens 			end = start;
1895cdb0ab79Smaybee 			error = dnode_next_offset(dn,
1896cdb0ab79Smaybee 			    DNODE_FIND_HOLE, &end, minlvl, blkfill, 0);
18973f9d6ad7SLin Ling 			zdb_nicenum(end - start, segsize);
1898fa9e4066Sahrens 			(void) printf("\t\tsegment [%016llx, %016llx)"
1899fa9e4066Sahrens 			    " size %5s\n", (u_longlong_t)start,
1900fa9e4066Sahrens 			    (u_longlong_t)end, segsize);
1901fa9e4066Sahrens 			if (error)
1902fa9e4066Sahrens 				break;
1903fa9e4066Sahrens 			start = end;
1904fa9e4066Sahrens 		}
1905fa9e4066Sahrens 	}
1906fa9e4066Sahrens 
1907fa9e4066Sahrens 	if (db != NULL)
1908ea8dc4b6Seschrock 		dmu_buf_rele(db, FTAG);
1909fa9e4066Sahrens }
1910fa9e4066Sahrens 
1911fa9e4066Sahrens static char *objset_types[DMU_OST_NUMTYPES] = {
1912fa9e4066Sahrens 	"NONE", "META", "ZPL", "ZVOL", "OTHER", "ANY" };
1913fa9e4066Sahrens 
1914fa9e4066Sahrens static void
1915fa9e4066Sahrens dump_dir(objset_t *os)
1916fa9e4066Sahrens {
1917fa9e4066Sahrens 	dmu_objset_stats_t dds;
1918fa9e4066Sahrens 	uint64_t object, object_count;
1919a2eea2e1Sahrens 	uint64_t refdbytes, usedobjs, scratch;
19203f9d6ad7SLin Ling 	char numbuf[32];
192114843421SMatthew Ahrens 	char blkbuf[BP_SPRINTF_LEN + 20];
1922fa9e4066Sahrens 	char osname[MAXNAMELEN];
1923fa9e4066Sahrens 	char *type = "UNKNOWN";
1924fa9e4066Sahrens 	int verbosity = dump_opt['d'];
1925fa9e4066Sahrens 	int print_header = 1;
1926fa9e4066Sahrens 	int i, error;
1927fa9e4066Sahrens 
19283b2aab18SMatthew Ahrens 	dsl_pool_config_enter(dmu_objset_pool(os), FTAG);
1929a2eea2e1Sahrens 	dmu_objset_fast_stat(os, &dds);
19303b2aab18SMatthew Ahrens 	dsl_pool_config_exit(dmu_objset_pool(os), FTAG);
1931fa9e4066Sahrens 
1932fa9e4066Sahrens 	if (dds.dds_type < DMU_OST_NUMTYPES)
1933fa9e4066Sahrens 		type = objset_types[dds.dds_type];
1934fa9e4066Sahrens 
1935fa9e4066Sahrens 	if (dds.dds_type == DMU_OST_META) {
1936fa9e4066Sahrens 		dds.dds_creation_txg = TXG_INITIAL;
19375d7b4d43SMatthew Ahrens 		usedobjs = BP_GET_FILL(os->os_rootbp);
1938c1379625SJustin T. Gibbs 		refdbytes = dsl_dir_phys(os->os_spa->spa_dsl_pool->dp_mos_dir)->
1939c1379625SJustin T. Gibbs 		    dd_used_bytes;
1940a2eea2e1Sahrens 	} else {
1941a2eea2e1Sahrens 		dmu_objset_space(os, &refdbytes, &scratch, &usedobjs, &scratch);
1942fa9e4066Sahrens 	}
1943fa9e4066Sahrens 
19445d7b4d43SMatthew Ahrens 	ASSERT3U(usedobjs, ==, BP_GET_FILL(os->os_rootbp));
1945fa9e4066Sahrens 
19463f9d6ad7SLin Ling 	zdb_nicenum(refdbytes, numbuf);
1947fa9e4066Sahrens 
1948fa9e4066Sahrens 	if (verbosity >= 4) {
194943466aaeSMax Grossman 		(void) snprintf(blkbuf, sizeof (blkbuf), ", rootbp ");
195043466aaeSMax Grossman 		(void) snprintf_blkptr(blkbuf + strlen(blkbuf),
195143466aaeSMax Grossman 		    sizeof (blkbuf) - strlen(blkbuf), os->os_rootbp);
1952fa9e4066Sahrens 	} else {
1953fa9e4066Sahrens 		blkbuf[0] = '\0';
1954fa9e4066Sahrens 	}
1955fa9e4066Sahrens 
1956fa9e4066Sahrens 	dmu_objset_name(os, osname);
1957fa9e4066Sahrens 
1958a2eea2e1Sahrens 	(void) printf("Dataset %s [%s], ID %llu, cr_txg %llu, "
1959fa9e4066Sahrens 	    "%s, %llu objects%s\n",
1960fa9e4066Sahrens 	    osname, type, (u_longlong_t)dmu_objset_id(os),
1961fa9e4066Sahrens 	    (u_longlong_t)dds.dds_creation_txg,
1962a2eea2e1Sahrens 	    numbuf, (u_longlong_t)usedobjs, blkbuf);
1963fa9e4066Sahrens 
1964b24ab676SJeff Bonwick 	if (zopt_objects != 0) {
1965b24ab676SJeff Bonwick 		for (i = 0; i < zopt_objects; i++)
1966b24ab676SJeff Bonwick 			dump_object(os, zopt_object[i], verbosity,
1967b24ab676SJeff Bonwick 			    &print_header);
1968b24ab676SJeff Bonwick 		(void) printf("\n");
1969b24ab676SJeff Bonwick 		return;
1970b24ab676SJeff Bonwick 	}
1971b24ab676SJeff Bonwick 
1972b24ab676SJeff Bonwick 	if (dump_opt['i'] != 0 || verbosity >= 2)
1973b24ab676SJeff Bonwick 		dump_intent_log(dmu_objset_zil(os));
1974fa9e4066Sahrens 
1975fa9e4066Sahrens 	if (dmu_objset_ds(os) != NULL)
1976cde58dbcSMatthew Ahrens 		dump_deadlist(&dmu_objset_ds(os)->ds_deadlist);
1977fa9e4066Sahrens 
1978fa9e4066Sahrens 	if (verbosity < 2)
1979fa9e4066Sahrens 		return;
1980fa9e4066Sahrens 
198143466aaeSMax Grossman 	if (BP_IS_HOLE(os->os_rootbp))
1982088f3894Sahrens 		return;
1983088f3894Sahrens 
1984fa9e4066Sahrens 	dump_object(os, 0, verbosity, &print_header);
198514843421SMatthew Ahrens 	object_count = 0;
1986744947dcSTom Erickson 	if (DMU_USERUSED_DNODE(os) != NULL &&
1987744947dcSTom Erickson 	    DMU_USERUSED_DNODE(os)->dn_type != 0) {
198814843421SMatthew Ahrens 		dump_object(os, DMU_USERUSED_OBJECT, verbosity, &print_header);
198914843421SMatthew Ahrens 		dump_object(os, DMU_GROUPUSED_OBJECT, verbosity, &print_header);
199014843421SMatthew Ahrens 	}
1991fa9e4066Sahrens 
1992fa9e4066Sahrens 	object = 0;
19936754306eSahrens 	while ((error = dmu_object_next(os, &object, B_FALSE, 0)) == 0) {
1994fa9e4066Sahrens 		dump_object(os, object, verbosity, &print_header);
1995fa9e4066Sahrens 		object_count++;
1996fa9e4066Sahrens 	}
1997fa9e4066Sahrens 
1998a2eea2e1Sahrens 	ASSERT3U(object_count, ==, usedobjs);
1999fa9e4066Sahrens 
2000fa9e4066Sahrens 	(void) printf("\n");
2001fa9e4066Sahrens 
2002ccba0801SRich Morris 	if (error != ESRCH) {
2003ccba0801SRich Morris 		(void) fprintf(stderr, "dmu_object_next() = %d\n", error);
2004ccba0801SRich Morris 		abort();
2005ccba0801SRich Morris 	}
2006fa9e4066Sahrens }
2007fa9e4066Sahrens 
2008fa9e4066Sahrens static void
200953b9a4a9SVictor Latushkin dump_uberblock(uberblock_t *ub, const char *header, const char *footer)
2010fa9e4066Sahrens {
2011fa9e4066Sahrens 	time_t timestamp = ub->ub_timestamp;
2012fa9e4066Sahrens 
201353b9a4a9SVictor Latushkin 	(void) printf(header ? header : "");
2014fa9e4066Sahrens 	(void) printf("\tmagic = %016llx\n", (u_longlong_t)ub->ub_magic);
2015fa9e4066Sahrens 	(void) printf("\tversion = %llu\n", (u_longlong_t)ub->ub_version);
2016fa9e4066Sahrens 	(void) printf("\ttxg = %llu\n", (u_longlong_t)ub->ub_txg);
2017fa9e4066Sahrens 	(void) printf("\tguid_sum = %llu\n", (u_longlong_t)ub->ub_guid_sum);
2018fa9e4066Sahrens 	(void) printf("\ttimestamp = %llu UTC = %s",
2019fa9e4066Sahrens 	    (u_longlong_t)ub->ub_timestamp, asctime(localtime(&timestamp)));
2020fa9e4066Sahrens 	if (dump_opt['u'] >= 3) {
2021fbabab8fSmaybee 		char blkbuf[BP_SPRINTF_LEN];
202243466aaeSMax Grossman 		snprintf_blkptr(blkbuf, sizeof (blkbuf), &ub->ub_rootbp);
2023fa9e4066Sahrens 		(void) printf("\trootbp = %s\n", blkbuf);
2024fa9e4066Sahrens 	}
202553b9a4a9SVictor Latushkin 	(void) printf(footer ? footer : "");
2026fa9e4066Sahrens }
2027fa9e4066Sahrens 
2028fa9e4066Sahrens static void
202907428bdfSVictor Latushkin dump_config(spa_t *spa)
2030fa9e4066Sahrens {
203107428bdfSVictor Latushkin 	dmu_buf_t *db;
203207428bdfSVictor Latushkin 	size_t nvsize = 0;
203307428bdfSVictor Latushkin 	int error = 0;
203407428bdfSVictor Latushkin 
2035fa9e4066Sahrens 
203607428bdfSVictor Latushkin 	error = dmu_bonus_hold(spa->spa_meta_objset,
203707428bdfSVictor Latushkin 	    spa->spa_config_object, FTAG, &db);
203807428bdfSVictor Latushkin 
203907428bdfSVictor Latushkin 	if (error == 0) {
204007428bdfSVictor Latushkin 		nvsize = *(uint64_t *)db->db_data;
204107428bdfSVictor Latushkin 		dmu_buf_rele(db, FTAG);
204207428bdfSVictor Latushkin 
204307428bdfSVictor Latushkin 		(void) printf("\nMOS Configuration:\n");
204407428bdfSVictor Latushkin 		dump_packed_nvlist(spa->spa_meta_objset,
204507428bdfSVictor Latushkin 		    spa->spa_config_object, (void *)&nvsize, 1);
204607428bdfSVictor Latushkin 	} else {
204707428bdfSVictor Latushkin 		(void) fprintf(stderr, "dmu_bonus_hold(%llu) failed, errno %d",
204807428bdfSVictor Latushkin 		    (u_longlong_t)spa->spa_config_object, error);
2049fa9e4066Sahrens 	}
2050fa9e4066Sahrens }
2051fa9e4066Sahrens 
2052c5904d13Seschrock static void
2053c5904d13Seschrock dump_cachefile(const char *cachefile)
2054c5904d13Seschrock {
2055c5904d13Seschrock 	int fd;
2056c5904d13Seschrock 	struct stat64 statbuf;
2057c5904d13Seschrock 	char *buf;
2058c5904d13Seschrock 	nvlist_t *config;
2059c5904d13Seschrock 
2060c5904d13Seschrock 	if ((fd = open64(cachefile, O_RDONLY)) < 0) {
2061c5904d13Seschrock 		(void) printf("cannot open '%s': %s\n", cachefile,
2062c5904d13Seschrock 		    strerror(errno));
2063c5904d13Seschrock 		exit(1);
2064c5904d13Seschrock 	}
2065c5904d13Seschrock 
2066c5904d13Seschrock 	if (fstat64(fd, &statbuf) != 0) {
2067c5904d13Seschrock 		(void) printf("failed to stat '%s': %s\n", cachefile,
2068c5904d13Seschrock 		    strerror(errno));
2069c5904d13Seschrock 		exit(1);
2070c5904d13Seschrock 	}
2071c5904d13Seschrock 
2072c5904d13Seschrock 	if ((buf = malloc(statbuf.st_size)) == NULL) {
2073c5904d13Seschrock 		(void) fprintf(stderr, "failed to allocate %llu bytes\n",
2074c5904d13Seschrock 		    (u_longlong_t)statbuf.st_size);
2075c5904d13Seschrock 		exit(1);
2076c5904d13Seschrock 	}
2077c5904d13Seschrock 
2078c5904d13Seschrock 	if (read(fd, buf, statbuf.st_size) != statbuf.st_size) {
2079c5904d13Seschrock 		(void) fprintf(stderr, "failed to read %llu bytes\n",
2080c5904d13Seschrock 		    (u_longlong_t)statbuf.st_size);
2081c5904d13Seschrock 		exit(1);
2082c5904d13Seschrock 	}
2083c5904d13Seschrock 
2084c5904d13Seschrock 	(void) close(fd);
2085c5904d13Seschrock 
2086c5904d13Seschrock 	if (nvlist_unpack(buf, statbuf.st_size, &config, 0) != 0) {
2087c5904d13Seschrock 		(void) fprintf(stderr, "failed to unpack nvlist\n");
2088c5904d13Seschrock 		exit(1);
2089c5904d13Seschrock 	}
2090c5904d13Seschrock 
2091c5904d13Seschrock 	free(buf);
2092c5904d13Seschrock 
2093c5904d13Seschrock 	dump_nvlist(config, 0);
2094c5904d13Seschrock 
2095c5904d13Seschrock 	nvlist_free(config);
2096c5904d13Seschrock }
2097c5904d13Seschrock 
209853b9a4a9SVictor Latushkin #define	ZDB_MAX_UB_HEADER_SIZE 32
209953b9a4a9SVictor Latushkin 
210053b9a4a9SVictor Latushkin static void
210153b9a4a9SVictor Latushkin dump_label_uberblocks(vdev_label_t *lbl, uint64_t ashift)
210253b9a4a9SVictor Latushkin {
210353b9a4a9SVictor Latushkin 	vdev_t vd;
210453b9a4a9SVictor Latushkin 	vdev_t *vdp = &vd;
210553b9a4a9SVictor Latushkin 	char header[ZDB_MAX_UB_HEADER_SIZE];
210653b9a4a9SVictor Latushkin 
210753b9a4a9SVictor Latushkin 	vd.vdev_ashift = ashift;
210853b9a4a9SVictor Latushkin 	vdp->vdev_top = vdp;
210953b9a4a9SVictor Latushkin 
211053b9a4a9SVictor Latushkin 	for (int i = 0; i < VDEV_UBERBLOCK_COUNT(vdp); i++) {
211153b9a4a9SVictor Latushkin 		uint64_t uoff = VDEV_UBERBLOCK_OFFSET(vdp, i);
211253b9a4a9SVictor Latushkin 		uberblock_t *ub = (void *)((char *)lbl + uoff);
211353b9a4a9SVictor Latushkin 
211453b9a4a9SVictor Latushkin 		if (uberblock_verify(ub))
211553b9a4a9SVictor Latushkin 			continue;
211653b9a4a9SVictor Latushkin 		(void) snprintf(header, ZDB_MAX_UB_HEADER_SIZE,
211753b9a4a9SVictor Latushkin 		    "Uberblock[%d]\n", i);
211853b9a4a9SVictor Latushkin 		dump_uberblock(ub, header, "");
211953b9a4a9SVictor Latushkin 	}
212053b9a4a9SVictor Latushkin }
212153b9a4a9SVictor Latushkin 
2122fa9e4066Sahrens static void
2123fa9e4066Sahrens dump_label(const char *dev)
2124fa9e4066Sahrens {
2125fa9e4066Sahrens 	int fd;
2126fa9e4066Sahrens 	vdev_label_t label;
2127c6065d0fSGeorge Wilson 	char *path, *buf = label.vl_vdev_phys.vp_nvlist;
2128fa9e4066Sahrens 	size_t buflen = sizeof (label.vl_vdev_phys.vp_nvlist);
2129fa9e4066Sahrens 	struct stat64 statbuf;
213053b9a4a9SVictor Latushkin 	uint64_t psize, ashift;
2131c6065d0fSGeorge Wilson 	int len = strlen(dev) + 1;
2132fa9e4066Sahrens 
2133c6065d0fSGeorge Wilson 	if (strncmp(dev, "/dev/dsk/", 9) == 0) {
2134c6065d0fSGeorge Wilson 		len++;
2135c6065d0fSGeorge Wilson 		path = malloc(len);
2136c6065d0fSGeorge Wilson 		(void) snprintf(path, len, "%s%s", "/dev/rdsk/", dev + 9);
2137c6065d0fSGeorge Wilson 	} else {
2138c6065d0fSGeorge Wilson 		path = strdup(dev);
2139c6065d0fSGeorge Wilson 	}
2140c6065d0fSGeorge Wilson 
2141c6065d0fSGeorge Wilson 	if ((fd = open64(path, O_RDONLY)) < 0) {
2142c6065d0fSGeorge Wilson 		(void) printf("cannot open '%s': %s\n", path, strerror(errno));
2143c6065d0fSGeorge Wilson 		free(path);
2144fa9e4066Sahrens 		exit(1);
2145fa9e4066Sahrens 	}
2146fa9e4066Sahrens 
2147fa9e4066Sahrens 	if (fstat64(fd, &statbuf) != 0) {
2148c6065d0fSGeorge Wilson 		(void) printf("failed to stat '%s': %s\n", path,
2149fa9e4066Sahrens 		    strerror(errno));
2150c6065d0fSGeorge Wilson 		free(path);
2151c6065d0fSGeorge Wilson 		(void) close(fd);
2152c6065d0fSGeorge Wilson 		exit(1);
2153c6065d0fSGeorge Wilson 	}
2154c6065d0fSGeorge Wilson 
2155c6065d0fSGeorge Wilson 	if (S_ISBLK(statbuf.st_mode)) {
2156c6065d0fSGeorge Wilson 		(void) printf("cannot use '%s': character device required\n",
2157c6065d0fSGeorge Wilson 		    path);
2158c6065d0fSGeorge Wilson 		free(path);
2159c6065d0fSGeorge Wilson 		(void) close(fd);
2160c6065d0fSGeorge Wilson 		exit(1);
2161fa9e4066Sahrens 	}
2162fa9e4066Sahrens 
2163fa9e4066Sahrens 	psize = statbuf.st_size;
2164fa9e4066Sahrens 	psize = P2ALIGN(psize, (uint64_t)sizeof (vdev_label_t));
2165fa9e4066Sahrens 
216653b9a4a9SVictor Latushkin 	for (int l = 0; l < VDEV_LABELS; l++) {
2167fa9e4066Sahrens 		nvlist_t *config = NULL;
2168fa9e4066Sahrens 
2169fa9e4066Sahrens 		(void) printf("--------------------------------------------\n");
2170fa9e4066Sahrens 		(void) printf("LABEL %d\n", l);
2171fa9e4066Sahrens 		(void) printf("--------------------------------------------\n");
2172fa9e4066Sahrens 
21730d981225Seschrock 		if (pread64(fd, &label, sizeof (label),
2174fa9e4066Sahrens 		    vdev_label_offset(psize, l, 0)) != sizeof (label)) {
2175fa9e4066Sahrens 			(void) printf("failed to read label %d\n", l);
2176fa9e4066Sahrens 			continue;
2177fa9e4066Sahrens 		}
2178fa9e4066Sahrens 
2179fa9e4066Sahrens 		if (nvlist_unpack(buf, buflen, &config, 0) != 0) {
2180fa9e4066Sahrens 			(void) printf("failed to unpack label %d\n", l);
218153b9a4a9SVictor Latushkin 			ashift = SPA_MINBLOCKSHIFT;
218253b9a4a9SVictor Latushkin 		} else {
218353b9a4a9SVictor Latushkin 			nvlist_t *vdev_tree = NULL;
218453b9a4a9SVictor Latushkin 
218553b9a4a9SVictor Latushkin 			dump_nvlist(config, 4);
218653b9a4a9SVictor Latushkin 			if ((nvlist_lookup_nvlist(config,
218753b9a4a9SVictor Latushkin 			    ZPOOL_CONFIG_VDEV_TREE, &vdev_tree) != 0) ||
218853b9a4a9SVictor Latushkin 			    (nvlist_lookup_uint64(vdev_tree,
218953b9a4a9SVictor Latushkin 			    ZPOOL_CONFIG_ASHIFT, &ashift) != 0))
219053b9a4a9SVictor Latushkin 				ashift = SPA_MINBLOCKSHIFT;
219153b9a4a9SVictor Latushkin 			nvlist_free(config);
2192fa9e4066Sahrens 		}
219353b9a4a9SVictor Latushkin 		if (dump_opt['u'])
219453b9a4a9SVictor Latushkin 			dump_label_uberblocks(&label, ashift);
2195fa9e4066Sahrens 	}
2196c6065d0fSGeorge Wilson 
2197c6065d0fSGeorge Wilson 	free(path);
2198c6065d0fSGeorge Wilson 	(void) close(fd);
2199fa9e4066Sahrens }
2200fa9e4066Sahrens 
2201b5152584SMatthew Ahrens static uint64_t num_large_blocks;
2202b5152584SMatthew Ahrens 
2203fa9e4066Sahrens /*ARGSUSED*/
22041d452cf5Sahrens static int
2205fd136879SMatthew Ahrens dump_one_dir(const char *dsname, void *arg)
2206fa9e4066Sahrens {
2207fa9e4066Sahrens 	int error;
2208fa9e4066Sahrens 	objset_t *os;
2209fa9e4066Sahrens 
2210503ad85cSMatthew Ahrens 	error = dmu_objset_own(dsname, DMU_OST_ANY, B_TRUE, FTAG, &os);
2211fa9e4066Sahrens 	if (error) {
2212b24ab676SJeff Bonwick 		(void) printf("Could not open %s, error %d\n", dsname, error);
22131d452cf5Sahrens 		return (0);
2214fa9e4066Sahrens 	}
2215b5152584SMatthew Ahrens 	if (dmu_objset_ds(os)->ds_large_blocks)
2216b5152584SMatthew Ahrens 		num_large_blocks++;
2217fa9e4066Sahrens 	dump_dir(os);
2218503ad85cSMatthew Ahrens 	dmu_objset_disown(os, FTAG);
2219e0d35c44Smarks 	fuid_table_destroy();
22200a586ceaSMark Shellenbaum 	sa_loaded = B_FALSE;
22211d452cf5Sahrens 	return (0);
2222fa9e4066Sahrens }
2223fa9e4066Sahrens 
2224fa9e4066Sahrens /*
2225b24ab676SJeff Bonwick  * Block statistics.
2226fa9e4066Sahrens  */
2227b5152584SMatthew Ahrens #define	PSIZE_HISTO_SIZE (SPA_OLD_MAXBLOCKSIZE / SPA_MINBLOCKSIZE + 2)
2228fa9e4066Sahrens typedef struct zdb_blkstats {
2229490d05b9SMatthew Ahrens 	uint64_t zb_asize;
2230490d05b9SMatthew Ahrens 	uint64_t zb_lsize;
2231490d05b9SMatthew Ahrens 	uint64_t zb_psize;
2232490d05b9SMatthew Ahrens 	uint64_t zb_count;
2233d5ee8a13SMatthew Ahrens 	uint64_t zb_gangs;
2234d5ee8a13SMatthew Ahrens 	uint64_t zb_ditto_samevdev;
2235490d05b9SMatthew Ahrens 	uint64_t zb_psize_histogram[PSIZE_HISTO_SIZE];
2236fa9e4066Sahrens } zdb_blkstats_t;
2237fa9e4066Sahrens 
2238b24ab676SJeff Bonwick /*
2239b24ab676SJeff Bonwick  * Extended object types to report deferred frees and dedup auto-ditto blocks.
2240b24ab676SJeff Bonwick  */
2241b24ab676SJeff Bonwick #define	ZDB_OT_DEFERRED	(DMU_OT_NUMTYPES + 0)
2242b24ab676SJeff Bonwick #define	ZDB_OT_DITTO	(DMU_OT_NUMTYPES + 1)
2243ad135b5dSChristopher Siden #define	ZDB_OT_OTHER	(DMU_OT_NUMTYPES + 2)
2244ad135b5dSChristopher Siden #define	ZDB_OT_TOTAL	(DMU_OT_NUMTYPES + 3)
2245b24ab676SJeff Bonwick 
2246b24ab676SJeff Bonwick static char *zdb_ot_extname[] = {
2247b24ab676SJeff Bonwick 	"deferred free",
2248b24ab676SJeff Bonwick 	"dedup ditto",
2249ad135b5dSChristopher Siden 	"other",
2250b24ab676SJeff Bonwick 	"Total",
2251b24ab676SJeff Bonwick };
2252fa9e4066Sahrens 
225388b7b0f2SMatthew Ahrens #define	ZB_TOTAL	DN_MAX_LEVELS
2254fa9e4066Sahrens 
2255fa9e4066Sahrens typedef struct zdb_cb {
2256b24ab676SJeff Bonwick 	zdb_blkstats_t	zcb_type[ZB_TOTAL + 1][ZDB_OT_TOTAL + 1];
2257b24ab676SJeff Bonwick 	uint64_t	zcb_dedup_asize;
2258b24ab676SJeff Bonwick 	uint64_t	zcb_dedup_blocks;
22595d7b4d43SMatthew Ahrens 	uint64_t	zcb_embedded_blocks[NUM_BP_EMBEDDED_TYPES];
22605d7b4d43SMatthew Ahrens 	uint64_t	zcb_embedded_histogram[NUM_BP_EMBEDDED_TYPES]
22615d7b4d43SMatthew Ahrens 	    [BPE_PAYLOAD_SIZE];
2262490d05b9SMatthew Ahrens 	uint64_t	zcb_start;
2263490d05b9SMatthew Ahrens 	uint64_t	zcb_lastprint;
2264490d05b9SMatthew Ahrens 	uint64_t	zcb_totalasize;
2265fa9e4066Sahrens 	uint64_t	zcb_errors[256];
2266fa9e4066Sahrens 	int		zcb_readfails;
2267fa9e4066Sahrens 	int		zcb_haderrors;
2268cde58dbcSMatthew Ahrens 	spa_t		*zcb_spa;
2269fa9e4066Sahrens } zdb_cb_t;
2270fa9e4066Sahrens 
2271fa9e4066Sahrens static void
2272cde58dbcSMatthew Ahrens zdb_count_block(zdb_cb_t *zcb, zilog_t *zilog, const blkptr_t *bp,
2273b24ab676SJeff Bonwick     dmu_object_type_t type)
2274fa9e4066Sahrens {
2275b24ab676SJeff Bonwick 	uint64_t refcnt = 0;
2276b24ab676SJeff Bonwick 
2277b24ab676SJeff Bonwick 	ASSERT(type < ZDB_OT_TOTAL);
2278b24ab676SJeff Bonwick 
2279b24ab676SJeff Bonwick 	if (zilog && zil_bp_tree_add(zilog, bp) != 0)
2280b24ab676SJeff Bonwick 		return;
2281b24ab676SJeff Bonwick 
2282e14bb325SJeff Bonwick 	for (int i = 0; i < 4; i++) {
2283fa9e4066Sahrens 		int l = (i < 2) ? BP_GET_LEVEL(bp) : ZB_TOTAL;
2284b24ab676SJeff Bonwick 		int t = (i & 1) ? type : ZDB_OT_TOTAL;
2285d5ee8a13SMatthew Ahrens 		int equal;
2286fa9e4066Sahrens 		zdb_blkstats_t *zb = &zcb->zcb_type[l][t];
2287fa9e4066Sahrens 
2288fa9e4066Sahrens 		zb->zb_asize += BP_GET_ASIZE(bp);
2289fa9e4066Sahrens 		zb->zb_lsize += BP_GET_LSIZE(bp);
2290fa9e4066Sahrens 		zb->zb_psize += BP_GET_PSIZE(bp);
2291fa9e4066Sahrens 		zb->zb_count++;
2292b5152584SMatthew Ahrens 
2293b5152584SMatthew Ahrens 		/*
2294b5152584SMatthew Ahrens 		 * The histogram is only big enough to record blocks up to
2295b5152584SMatthew Ahrens 		 * SPA_OLD_MAXBLOCKSIZE; larger blocks go into the last,
2296b5152584SMatthew Ahrens 		 * "other", bucket.
2297b5152584SMatthew Ahrens 		 */
2298b5152584SMatthew Ahrens 		int idx = BP_GET_PSIZE(bp) >> SPA_MINBLOCKSHIFT;
2299b5152584SMatthew Ahrens 		idx = MIN(idx, SPA_OLD_MAXBLOCKSIZE / SPA_MINBLOCKSIZE + 1);
2300b5152584SMatthew Ahrens 		zb->zb_psize_histogram[idx]++;
2301d5ee8a13SMatthew Ahrens 
2302d5ee8a13SMatthew Ahrens 		zb->zb_gangs += BP_COUNT_GANG(bp);
2303d5ee8a13SMatthew Ahrens 
2304d5ee8a13SMatthew Ahrens 		switch (BP_GET_NDVAS(bp)) {
2305d5ee8a13SMatthew Ahrens 		case 2:
2306d5ee8a13SMatthew Ahrens 			if (DVA_GET_VDEV(&bp->blk_dva[0]) ==
2307d5ee8a13SMatthew Ahrens 			    DVA_GET_VDEV(&bp->blk_dva[1]))
2308d5ee8a13SMatthew Ahrens 				zb->zb_ditto_samevdev++;
2309d5ee8a13SMatthew Ahrens 			break;
2310d5ee8a13SMatthew Ahrens 		case 3:
2311d5ee8a13SMatthew Ahrens 			equal = (DVA_GET_VDEV(&bp->blk_dva[0]) ==
2312d5ee8a13SMatthew Ahrens 			    DVA_GET_VDEV(&bp->blk_dva[1])) +
2313d5ee8a13SMatthew Ahrens 			    (DVA_GET_VDEV(&bp->blk_dva[0]) ==
2314d5ee8a13SMatthew Ahrens 			    DVA_GET_VDEV(&bp->blk_dva[2])) +
2315d5ee8a13SMatthew Ahrens 			    (DVA_GET_VDEV(&bp->blk_dva[1]) ==
2316d5ee8a13SMatthew Ahrens 			    DVA_GET_VDEV(&bp->blk_dva[2]));
2317d5ee8a13SMatthew Ahrens 			if (equal != 0)
2318d5ee8a13SMatthew Ahrens 				zb->zb_ditto_samevdev++;
2319d5ee8a13SMatthew Ahrens 			break;
2320d5ee8a13SMatthew Ahrens 		}
2321d5ee8a13SMatthew Ahrens 
2322fa9e4066Sahrens 	}
2323fa9e4066Sahrens 
23245d7b4d43SMatthew Ahrens 	if (BP_IS_EMBEDDED(bp)) {
23255d7b4d43SMatthew Ahrens 		zcb->zcb_embedded_blocks[BPE_GET_ETYPE(bp)]++;
23265d7b4d43SMatthew Ahrens 		zcb->zcb_embedded_histogram[BPE_GET_ETYPE(bp)]
23275d7b4d43SMatthew Ahrens 		    [BPE_GET_PSIZE(bp)]++;
23285d7b4d43SMatthew Ahrens 		return;
23295d7b4d43SMatthew Ahrens 	}
23305d7b4d43SMatthew Ahrens 
2331b24ab676SJeff Bonwick 	if (dump_opt['L'])
2332b24ab676SJeff Bonwick 		return;
2333b24ab676SJeff Bonwick 
2334b24ab676SJeff Bonwick 	if (BP_GET_DEDUP(bp)) {
2335b24ab676SJeff Bonwick 		ddt_t *ddt;
2336b24ab676SJeff Bonwick 		ddt_entry_t *dde;
2337b24ab676SJeff Bonwick 
2338cde58dbcSMatthew Ahrens 		ddt = ddt_select(zcb->zcb_spa, bp);
2339b24ab676SJeff Bonwick 		ddt_enter(ddt);
2340b24ab676SJeff Bonwick 		dde = ddt_lookup(ddt, bp, B_FALSE);
2341b24ab676SJeff Bonwick 
2342b24ab676SJeff Bonwick 		if (dde == NULL) {
2343b24ab676SJeff Bonwick 			refcnt = 0;
2344b24ab676SJeff Bonwick 		} else {
2345b24ab676SJeff Bonwick 			ddt_phys_t *ddp = ddt_phys_select(dde, bp);
2346b24ab676SJeff Bonwick 			ddt_phys_decref(ddp);
2347b24ab676SJeff Bonwick 			refcnt = ddp->ddp_refcnt;
2348b24ab676SJeff Bonwick 			if (ddt_phys_total_refcnt(dde) == 0)
2349b24ab676SJeff Bonwick 				ddt_remove(ddt, dde);
2350d41e7643Sek 		}
2351b24ab676SJeff Bonwick 		ddt_exit(ddt);
2352d41e7643Sek 	}
2353d41e7643Sek 
2354cde58dbcSMatthew Ahrens 	VERIFY3U(zio_wait(zio_claim(NULL, zcb->zcb_spa,
2355cde58dbcSMatthew Ahrens 	    refcnt ? 0 : spa_first_txg(zcb->zcb_spa),
2356b24ab676SJeff Bonwick 	    bp, NULL, NULL, ZIO_FLAG_CANFAIL)), ==, 0);
2357fa9e4066Sahrens }
2358fa9e4066Sahrens 
235931d7e8faSGeorge Wilson static void
236031d7e8faSGeorge Wilson zdb_blkptr_done(zio_t *zio)
236131d7e8faSGeorge Wilson {
236231d7e8faSGeorge Wilson 	spa_t *spa = zio->io_spa;
236331d7e8faSGeorge Wilson 	blkptr_t *bp = zio->io_bp;
236431d7e8faSGeorge Wilson 	int ioerr = zio->io_error;
236531d7e8faSGeorge Wilson 	zdb_cb_t *zcb = zio->io_private;
23667802d7bfSMatthew Ahrens 	zbookmark_phys_t *zb = &zio->io_bookmark;
236731d7e8faSGeorge Wilson 
236831d7e8faSGeorge Wilson 	zio_data_buf_free(zio->io_data, zio->io_size);
236931d7e8faSGeorge Wilson 
237031d7e8faSGeorge Wilson 	mutex_enter(&spa->spa_scrub_lock);
237131d7e8faSGeorge Wilson 	spa->spa_scrub_inflight--;
237231d7e8faSGeorge Wilson 	cv_broadcast(&spa->spa_scrub_io_cv);
237331d7e8faSGeorge Wilson 
237431d7e8faSGeorge Wilson 	if (ioerr && !(zio->io_flags & ZIO_FLAG_SPECULATIVE)) {
237531d7e8faSGeorge Wilson 		char blkbuf[BP_SPRINTF_LEN];
237631d7e8faSGeorge Wilson 
237731d7e8faSGeorge Wilson 		zcb->zcb_haderrors = 1;
237831d7e8faSGeorge Wilson 		zcb->zcb_errors[ioerr]++;
237931d7e8faSGeorge Wilson 
238031d7e8faSGeorge Wilson 		if (dump_opt['b'] >= 2)
238143466aaeSMax Grossman 			snprintf_blkptr(blkbuf, sizeof (blkbuf), bp);
238231d7e8faSGeorge Wilson 		else
238331d7e8faSGeorge Wilson 			blkbuf[0] = '\0';
238431d7e8faSGeorge Wilson 
238531d7e8faSGeorge Wilson 		(void) printf("zdb_blkptr_cb: "
238631d7e8faSGeorge Wilson 		    "Got error %d reading "
238731d7e8faSGeorge Wilson 		    "<%llu, %llu, %lld, %llx> %s -- skipping\n",
238831d7e8faSGeorge Wilson 		    ioerr,
238931d7e8faSGeorge Wilson 		    (u_longlong_t)zb->zb_objset,
239031d7e8faSGeorge Wilson 		    (u_longlong_t)zb->zb_object,
239131d7e8faSGeorge Wilson 		    (u_longlong_t)zb->zb_level,
239231d7e8faSGeorge Wilson 		    (u_longlong_t)zb->zb_blkid,
239331d7e8faSGeorge Wilson 		    blkbuf);
239431d7e8faSGeorge Wilson 	}
239531d7e8faSGeorge Wilson 	mutex_exit(&spa->spa_scrub_lock);
239631d7e8faSGeorge Wilson }
239731d7e8faSGeorge Wilson 
2398fa9e4066Sahrens static int
23991b912ec7SGeorge Wilson zdb_blkptr_cb(spa_t *spa, zilog_t *zilog, const blkptr_t *bp,
24007802d7bfSMatthew Ahrens     const zbookmark_phys_t *zb, const dnode_phys_t *dnp, void *arg)
2401fa9e4066Sahrens {
2402fa9e4066Sahrens 	zdb_cb_t *zcb = arg;
24036365109dSVictor Latushkin 	dmu_object_type_t type;
2404468c413aSTim Haley 	boolean_t is_metadata;
2405fa9e4066Sahrens 
240643466aaeSMax Grossman 	if (dump_opt['b'] >= 5 && bp->blk_birth > 0) {
240743466aaeSMax Grossman 		char blkbuf[BP_SPRINTF_LEN];
240843466aaeSMax Grossman 		snprintf_blkptr(blkbuf, sizeof (blkbuf), bp);
240943466aaeSMax Grossman 		(void) printf("objset %llu object %llu "
241043466aaeSMax Grossman 		    "level %lld offset 0x%llx %s\n",
241143466aaeSMax Grossman 		    (u_longlong_t)zb->zb_objset,
241243466aaeSMax Grossman 		    (u_longlong_t)zb->zb_object,
241343466aaeSMax Grossman 		    (longlong_t)zb->zb_level,
241443466aaeSMax Grossman 		    (u_longlong_t)blkid2offset(dnp, bp, zb),
241543466aaeSMax Grossman 		    blkbuf);
241643466aaeSMax Grossman 	}
241743466aaeSMax Grossman 
241843466aaeSMax Grossman 	if (BP_IS_HOLE(bp))
241988b7b0f2SMatthew Ahrens 		return (0);
2420e14bb325SJeff Bonwick 
24216365109dSVictor Latushkin 	type = BP_GET_TYPE(bp);
2422e14bb325SJeff Bonwick 
2423ad135b5dSChristopher Siden 	zdb_count_block(zcb, zilog, bp,
2424ad135b5dSChristopher Siden 	    (type & DMU_OT_NEWTYPE) ? ZDB_OT_OTHER : type);
24256365109dSVictor Latushkin 
2426ad135b5dSChristopher Siden 	is_metadata = (BP_GET_LEVEL(bp) != 0 || DMU_OT_IS_METADATA(type));
24276365109dSVictor Latushkin 
24285d7b4d43SMatthew Ahrens 	if (!BP_IS_EMBEDDED(bp) &&
24295d7b4d43SMatthew Ahrens 	    (dump_opt['c'] > 1 || (dump_opt['c'] && is_metadata))) {
2430468c413aSTim Haley 		size_t size = BP_GET_PSIZE(bp);
243131d7e8faSGeorge Wilson 		void *data = zio_data_buf_alloc(size);
2432b24ab676SJeff Bonwick 		int flags = ZIO_FLAG_CANFAIL | ZIO_FLAG_SCRUB | ZIO_FLAG_RAW;
2433b24ab676SJeff Bonwick 
2434b24ab676SJeff Bonwick 		/* If it's an intent log block, failure is expected. */
2435b24ab676SJeff Bonwick 		if (zb->zb_level == ZB_ZIL_LEVEL)
2436b24ab676SJeff Bonwick 			flags |= ZIO_FLAG_SPECULATIVE;
2437b24ab676SJeff Bonwick 
243831d7e8faSGeorge Wilson 		mutex_enter(&spa->spa_scrub_lock);
243931d7e8faSGeorge Wilson 		while (spa->spa_scrub_inflight > max_inflight)
244031d7e8faSGeorge Wilson 			cv_wait(&spa->spa_scrub_io_cv, &spa->spa_scrub_lock);
244131d7e8faSGeorge Wilson 		spa->spa_scrub_inflight++;
244231d7e8faSGeorge Wilson 		mutex_exit(&spa->spa_scrub_lock);
2443b24ab676SJeff Bonwick 
244431d7e8faSGeorge Wilson 		zio_nowait(zio_read(NULL, spa, bp, data, size,
244531d7e8faSGeorge Wilson 		    zdb_blkptr_done, zcb, ZIO_PRIORITY_ASYNC_READ, flags, zb));
2446fa9e4066Sahrens 	}
2447fa9e4066Sahrens 
2448fa9e4066Sahrens 	zcb->zcb_readfails = 0;
2449fa9e4066Sahrens 
2450f7950bf1SMatthew Ahrens 	/* only call gethrtime() every 100 blocks */
2451f7950bf1SMatthew Ahrens 	static int iters;
2452f7950bf1SMatthew Ahrens 	if (++iters > 100)
2453f7950bf1SMatthew Ahrens 		iters = 0;
2454f7950bf1SMatthew Ahrens 	else
2455f7950bf1SMatthew Ahrens 		return (0);
2456f7950bf1SMatthew Ahrens 
2457f7950bf1SMatthew Ahrens 	if (dump_opt['b'] < 5 && gethrtime() > zcb->zcb_lastprint + NANOSEC) {
2458490d05b9SMatthew Ahrens 		uint64_t now = gethrtime();
2459490d05b9SMatthew Ahrens 		char buf[10];
2460490d05b9SMatthew Ahrens 		uint64_t bytes = zcb->zcb_type[ZB_TOTAL][ZDB_OT_TOTAL].zb_asize;
2461490d05b9SMatthew Ahrens 		int kb_per_sec =
2462490d05b9SMatthew Ahrens 		    1 + bytes / (1 + ((now - zcb->zcb_start) / 1000 / 1000));
2463490d05b9SMatthew Ahrens 		int sec_remaining =
2464490d05b9SMatthew Ahrens 		    (zcb->zcb_totalasize - bytes) / 1024 / kb_per_sec;
2465490d05b9SMatthew Ahrens 
2466490d05b9SMatthew Ahrens 		zfs_nicenum(bytes, buf, sizeof (buf));
2467490d05b9SMatthew Ahrens 		(void) fprintf(stderr,
2468490d05b9SMatthew Ahrens 		    "\r%5s completed (%4dMB/s) "
2469490d05b9SMatthew Ahrens 		    "estimated time remaining: %uhr %02umin %02usec        ",
2470490d05b9SMatthew Ahrens 		    buf, kb_per_sec / 1024,
2471490d05b9SMatthew Ahrens 		    sec_remaining / 60 / 60,
2472490d05b9SMatthew Ahrens 		    sec_remaining / 60 % 60,
2473490d05b9SMatthew Ahrens 		    sec_remaining % 60);
2474490d05b9SMatthew Ahrens 
2475490d05b9SMatthew Ahrens 		zcb->zcb_lastprint = now;
2476490d05b9SMatthew Ahrens 	}
2477490d05b9SMatthew Ahrens 
2478fa9e4066Sahrens 	return (0);
2479fa9e4066Sahrens }
2480fa9e4066Sahrens 
2481b24ab676SJeff Bonwick static void
24820713e232SGeorge Wilson zdb_leak(void *arg, uint64_t start, uint64_t size)
2483b24ab676SJeff Bonwick {
24840713e232SGeorge Wilson 	vdev_t *vd = arg;
2485b24ab676SJeff Bonwick 
2486b24ab676SJeff Bonwick 	(void) printf("leaked space: vdev %llu, offset 0x%llx, size %llu\n",
2487b24ab676SJeff Bonwick 	    (u_longlong_t)vd->vdev_id, (u_longlong_t)start, (u_longlong_t)size);
2488b24ab676SJeff Bonwick }
2489b24ab676SJeff Bonwick 
24900713e232SGeorge Wilson static metaslab_ops_t zdb_metaslab_ops = {
24912e4c9986SGeorge Wilson 	NULL	/* alloc */
2492b24ab676SJeff Bonwick };
2493b24ab676SJeff Bonwick 
2494b24ab676SJeff Bonwick static void
2495bbfd46c4SJeff Bonwick zdb_ddt_leak_init(spa_t *spa, zdb_cb_t *zcb)
2496b24ab676SJeff Bonwick {
2497bbfd46c4SJeff Bonwick 	ddt_bookmark_t ddb = { 0 };
2498b24ab676SJeff Bonwick 	ddt_entry_t dde;
2499b24ab676SJeff Bonwick 	int error;
2500b24ab676SJeff Bonwick 
2501bbfd46c4SJeff Bonwick 	while ((error = ddt_walk(spa, &ddb, &dde)) == 0) {
2502b24ab676SJeff Bonwick 		blkptr_t blk;
2503b24ab676SJeff Bonwick 		ddt_phys_t *ddp = dde.dde_phys;
2504bbfd46c4SJeff Bonwick 
2505bbfd46c4SJeff Bonwick 		if (ddb.ddb_class == DDT_CLASS_UNIQUE)
2506bbfd46c4SJeff Bonwick 			return;
2507bbfd46c4SJeff Bonwick 
2508b24ab676SJeff Bonwick 		ASSERT(ddt_phys_total_refcnt(&dde) > 1);
2509bbfd46c4SJeff Bonwick 
2510b24ab676SJeff Bonwick 		for (int p = 0; p < DDT_PHYS_TYPES; p++, ddp++) {
2511b24ab676SJeff Bonwick 			if (ddp->ddp_phys_birth == 0)
2512b24ab676SJeff Bonwick 				continue;
2513bbfd46c4SJeff Bonwick 			ddt_bp_create(ddb.ddb_checksum,
2514bbfd46c4SJeff Bonwick 			    &dde.dde_key, ddp, &blk);
2515b24ab676SJeff Bonwick 			if (p == DDT_PHYS_DITTO) {
2516cde58dbcSMatthew Ahrens 				zdb_count_block(zcb, NULL, &blk, ZDB_OT_DITTO);
2517b24ab676SJeff Bonwick 			} else {
2518b24ab676SJeff Bonwick 				zcb->zcb_dedup_asize +=
2519b24ab676SJeff Bonwick 				    BP_GET_ASIZE(&blk) * (ddp->ddp_refcnt - 1);
2520b24ab676SJeff Bonwick 				zcb->zcb_dedup_blocks++;
2521b24ab676SJeff Bonwick 			}
2522b24ab676SJeff Bonwick 		}
2523b24ab676SJeff Bonwick 		if (!dump_opt['L']) {
2524bbfd46c4SJeff Bonwick 			ddt_t *ddt = spa->spa_ddt[ddb.ddb_checksum];
2525b24ab676SJeff Bonwick 			ddt_enter(ddt);
2526b24ab676SJeff Bonwick 			VERIFY(ddt_lookup(ddt, &blk, B_TRUE) != NULL);
2527b24ab676SJeff Bonwick 			ddt_exit(ddt);
2528b24ab676SJeff Bonwick 		}
2529b24ab676SJeff Bonwick 	}
2530b24ab676SJeff Bonwick 
2531b24ab676SJeff Bonwick 	ASSERT(error == ENOENT);
2532b24ab676SJeff Bonwick }
2533b24ab676SJeff Bonwick 
2534b24ab676SJeff Bonwick static void
2535b24ab676SJeff Bonwick zdb_leak_init(spa_t *spa, zdb_cb_t *zcb)
2536b24ab676SJeff Bonwick {
2537cde58dbcSMatthew Ahrens 	zcb->zcb_spa = spa;
2538cde58dbcSMatthew Ahrens 
2539b24ab676SJeff Bonwick 	if (!dump_opt['L']) {
2540b24ab676SJeff Bonwick 		vdev_t *rvd = spa->spa_root_vdev;
254106be9802SMatthew Ahrens 		for (uint64_t c = 0; c < rvd->vdev_children; c++) {
2542b24ab676SJeff Bonwick 			vdev_t *vd = rvd->vdev_child[c];
254306be9802SMatthew Ahrens 			for (uint64_t m = 0; m < vd->vdev_ms_count; m++) {
2544b24ab676SJeff Bonwick 				metaslab_t *msp = vd->vdev_ms[m];
2545b24ab676SJeff Bonwick 				mutex_enter(&msp->ms_lock);
25460713e232SGeorge Wilson 				metaslab_unload(msp);
25470713e232SGeorge Wilson 
25480713e232SGeorge Wilson 				/*
25490713e232SGeorge Wilson 				 * For leak detection, we overload the metaslab
25500713e232SGeorge Wilson 				 * ms_tree to contain allocated segments
25510713e232SGeorge Wilson 				 * instead of free segments. As a result,
25520713e232SGeorge Wilson 				 * we can't use the normal metaslab_load/unload
25530713e232SGeorge Wilson 				 * interfaces.
25540713e232SGeorge Wilson 				 */
25550713e232SGeorge Wilson 				if (msp->ms_sm != NULL) {
255606be9802SMatthew Ahrens 					(void) fprintf(stderr,
255706be9802SMatthew Ahrens 					    "\rloading space map for "
255806be9802SMatthew Ahrens 					    "vdev %llu of %llu, "
255906be9802SMatthew Ahrens 					    "metaslab %llu of %llu ...",
256006be9802SMatthew Ahrens 					    (longlong_t)c,
256106be9802SMatthew Ahrens 					    (longlong_t)rvd->vdev_children,
256206be9802SMatthew Ahrens 					    (longlong_t)m,
256306be9802SMatthew Ahrens 					    (longlong_t)vd->vdev_ms_count);
256406be9802SMatthew Ahrens 
25650713e232SGeorge Wilson 					msp->ms_ops = &zdb_metaslab_ops;
2566f7950bf1SMatthew Ahrens 
2567f7950bf1SMatthew Ahrens 					/*
2568f7950bf1SMatthew Ahrens 					 * We don't want to spend the CPU
2569f7950bf1SMatthew Ahrens 					 * manipulating the size-ordered
2570f7950bf1SMatthew Ahrens 					 * tree, so clear the range_tree
2571f7950bf1SMatthew Ahrens 					 * ops.
2572f7950bf1SMatthew Ahrens 					 */
2573f7950bf1SMatthew Ahrens 					msp->ms_tree->rt_ops = NULL;
25740713e232SGeorge Wilson 					VERIFY0(space_map_load(msp->ms_sm,
25750713e232SGeorge Wilson 					    msp->ms_tree, SM_ALLOC));
25760713e232SGeorge Wilson 					msp->ms_loaded = B_TRUE;
25770713e232SGeorge Wilson 				}
2578b24ab676SJeff Bonwick 				mutex_exit(&msp->ms_lock);
2579b24ab676SJeff Bonwick 			}
2580b24ab676SJeff Bonwick 		}
258106be9802SMatthew Ahrens 		(void) fprintf(stderr, "\n");
2582b24ab676SJeff Bonwick 	}
2583b24ab676SJeff Bonwick 
2584b24ab676SJeff Bonwick 	spa_config_enter(spa, SCL_CONFIG, FTAG, RW_READER);
2585b24ab676SJeff Bonwick 
2586bbfd46c4SJeff Bonwick 	zdb_ddt_leak_init(spa, zcb);
2587b24ab676SJeff Bonwick 
2588b24ab676SJeff Bonwick 	spa_config_exit(spa, SCL_CONFIG, FTAG);
2589b24ab676SJeff Bonwick }
2590b24ab676SJeff Bonwick 
2591b24ab676SJeff Bonwick static void
2592b24ab676SJeff Bonwick zdb_leak_fini(spa_t *spa)
2593b24ab676SJeff Bonwick {
2594b24ab676SJeff Bonwick 	if (!dump_opt['L']) {
2595b24ab676SJeff Bonwick 		vdev_t *rvd = spa->spa_root_vdev;
2596b24ab676SJeff Bonwick 		for (int c = 0; c < rvd->vdev_children; c++) {
2597b24ab676SJeff Bonwick 			vdev_t *vd = rvd->vdev_child[c];
2598b24ab676SJeff Bonwick 			for (int m = 0; m < vd->vdev_ms_count; m++) {
2599b24ab676SJeff Bonwick 				metaslab_t *msp = vd->vdev_ms[m];
2600b24ab676SJeff Bonwick 				mutex_enter(&msp->ms_lock);
26010713e232SGeorge Wilson 
26020713e232SGeorge Wilson 				/*
26030713e232SGeorge Wilson 				 * The ms_tree has been overloaded to
26040713e232SGeorge Wilson 				 * contain allocated segments. Now that we
26050713e232SGeorge Wilson 				 * finished traversing all blocks, any
26060713e232SGeorge Wilson 				 * block that remains in the ms_tree
26070713e232SGeorge Wilson 				 * represents an allocated block that we
26080713e232SGeorge Wilson 				 * did not claim during the traversal.
26090713e232SGeorge Wilson 				 * Claimed blocks would have been removed
26100713e232SGeorge Wilson 				 * from the ms_tree.
26110713e232SGeorge Wilson 				 */
26120713e232SGeorge Wilson 				range_tree_vacate(msp->ms_tree, zdb_leak, vd);
26130713e232SGeorge Wilson 				msp->ms_loaded = B_FALSE;
26140713e232SGeorge Wilson 
2615b24ab676SJeff Bonwick 				mutex_exit(&msp->ms_lock);
2616b24ab676SJeff Bonwick 			}
2617b24ab676SJeff Bonwick 		}
2618b24ab676SJeff Bonwick 	}
2619b24ab676SJeff Bonwick }
2620b24ab676SJeff Bonwick 
2621cde58dbcSMatthew Ahrens /* ARGSUSED */
2622cde58dbcSMatthew Ahrens static int
2623cde58dbcSMatthew Ahrens count_block_cb(void *arg, const blkptr_t *bp, dmu_tx_t *tx)
2624cde58dbcSMatthew Ahrens {
2625cde58dbcSMatthew Ahrens 	zdb_cb_t *zcb = arg;
2626cde58dbcSMatthew Ahrens 
2627490d05b9SMatthew Ahrens 	if (dump_opt['b'] >= 5) {
2628cde58dbcSMatthew Ahrens 		char blkbuf[BP_SPRINTF_LEN];
262943466aaeSMax Grossman 		snprintf_blkptr(blkbuf, sizeof (blkbuf), bp);
2630cde58dbcSMatthew Ahrens 		(void) printf("[%s] %s\n",
2631cde58dbcSMatthew Ahrens 		    "deferred free", blkbuf);
2632cde58dbcSMatthew Ahrens 	}
2633cde58dbcSMatthew Ahrens 	zdb_count_block(zcb, NULL, bp, ZDB_OT_DEFERRED);
2634cde58dbcSMatthew Ahrens 	return (0);
2635cde58dbcSMatthew Ahrens }
2636cde58dbcSMatthew Ahrens 
2637fa9e4066Sahrens static int
2638fa9e4066Sahrens dump_block_stats(spa_t *spa)
2639fa9e4066Sahrens {
2640fa9e4066Sahrens 	zdb_cb_t zcb = { 0 };
2641fa9e4066Sahrens 	zdb_blkstats_t *zb, *tzb;
2642b24ab676SJeff Bonwick 	uint64_t norm_alloc, norm_space, total_alloc, total_found;
2643cd088ea4SVictor Latushkin 	int flags = TRAVERSE_PRE | TRAVERSE_PREFETCH_METADATA | TRAVERSE_HARD;
26445d7b4d43SMatthew Ahrens 	boolean_t leaks = B_FALSE;
2645fa9e4066Sahrens 
2646490d05b9SMatthew Ahrens 	(void) printf("\nTraversing all blocks %s%s%s%s%s...\n\n",
2647b24ab676SJeff Bonwick 	    (dump_opt['c'] || !dump_opt['L']) ? "to verify " : "",
2648b24ab676SJeff Bonwick 	    (dump_opt['c'] == 1) ? "metadata " : "",
2649b24ab676SJeff Bonwick 	    dump_opt['c'] ? "checksums " : "",
2650b24ab676SJeff Bonwick 	    (dump_opt['c'] && !dump_opt['L']) ? "and verify " : "",
2651b24ab676SJeff Bonwick 	    !dump_opt['L'] ? "nothing leaked " : "");
2652fa9e4066Sahrens 
2653fa9e4066Sahrens 	/*
2654e14bb325SJeff Bonwick 	 * Load all space maps as SM_ALLOC maps, then traverse the pool
2655e14bb325SJeff Bonwick 	 * claiming each block we discover.  If the pool is perfectly
2656e14bb325SJeff Bonwick 	 * consistent, the space maps will be empty when we're done.
2657e14bb325SJeff Bonwick 	 * Anything left over is a leak; any block we can't claim (because
2658e14bb325SJeff Bonwick 	 * it's not part of any space map) is a double allocation,
2659e14bb325SJeff Bonwick 	 * reference to a freed block, or an unclaimed log block.
2660fa9e4066Sahrens 	 */
2661b24ab676SJeff Bonwick 	zdb_leak_init(spa, &zcb);
2662fa9e4066Sahrens 
2663fa9e4066Sahrens 	/*
2664fa9e4066Sahrens 	 * If there's a deferred-free bplist, process that first.
2665fa9e4066Sahrens 	 */
2666cde58dbcSMatthew Ahrens 	(void) bpobj_iterate_nofree(&spa->spa_deferred_bpobj,
2667cde58dbcSMatthew Ahrens 	    count_block_cb, &zcb, NULL);
26683b2aab18SMatthew Ahrens 	if (spa_version(spa) >= SPA_VERSION_DEADLISTS) {
26693b2aab18SMatthew Ahrens 		(void) bpobj_iterate_nofree(&spa->spa_dsl_pool->dp_free_bpobj,
26703b2aab18SMatthew Ahrens 		    count_block_cb, &zcb, NULL);
26713b2aab18SMatthew Ahrens 	}
26722acef22dSMatthew Ahrens 	if (spa_feature_is_active(spa, SPA_FEATURE_ASYNC_DESTROY)) {
2673b420f3adSRichard Lowe 		VERIFY3U(0, ==, bptree_iterate(spa->spa_meta_objset,
2674ad135b5dSChristopher Siden 		    spa->spa_dsl_pool->dp_bptree_obj, B_FALSE, count_block_cb,
2675ad135b5dSChristopher Siden 		    &zcb, NULL));
2676ad135b5dSChristopher Siden 	}
2677fa9e4066Sahrens 
2678bbfd46c4SJeff Bonwick 	if (dump_opt['c'] > 1)
2679bbfd46c4SJeff Bonwick 		flags |= TRAVERSE_PREFETCH_DATA;
2680bbfd46c4SJeff Bonwick 
2681490d05b9SMatthew Ahrens 	zcb.zcb_totalasize = metaslab_class_get_alloc(spa_normal_class(spa));
2682490d05b9SMatthew Ahrens 	zcb.zcb_start = zcb.zcb_lastprint = gethrtime();
2683bbfd46c4SJeff Bonwick 	zcb.zcb_haderrors |= traverse_pool(spa, 0, flags, zdb_blkptr_cb, &zcb);
2684fa9e4066Sahrens 
268531d7e8faSGeorge Wilson 	/*
268631d7e8faSGeorge Wilson 	 * If we've traversed the data blocks then we need to wait for those
268731d7e8faSGeorge Wilson 	 * I/Os to complete. We leverage "The Godfather" zio to wait on
268831d7e8faSGeorge Wilson 	 * all async I/Os to complete.
268931d7e8faSGeorge Wilson 	 */
269031d7e8faSGeorge Wilson 	if (dump_opt['c']) {
26916f834bc1SMatthew Ahrens 		for (int i = 0; i < max_ncpus; i++) {
26926f834bc1SMatthew Ahrens 			(void) zio_wait(spa->spa_async_zio_root[i]);
26936f834bc1SMatthew Ahrens 			spa->spa_async_zio_root[i] = zio_root(spa, NULL, NULL,
26946f834bc1SMatthew Ahrens 			    ZIO_FLAG_CANFAIL | ZIO_FLAG_SPECULATIVE |
26956f834bc1SMatthew Ahrens 			    ZIO_FLAG_GODFATHER);
26966f834bc1SMatthew Ahrens 		}
269731d7e8faSGeorge Wilson 	}
269831d7e8faSGeorge Wilson 
2699b24ab676SJeff Bonwick 	if (zcb.zcb_haderrors) {
2700fa9e4066Sahrens 		(void) printf("\nError counts:\n\n");
2701fa9e4066Sahrens 		(void) printf("\t%5s  %s\n", "errno", "count");
2702b24ab676SJeff Bonwick 		for (int e = 0; e < 256; e++) {
2703fa9e4066Sahrens 			if (zcb.zcb_errors[e] != 0) {
2704fa9e4066Sahrens 				(void) printf("\t%5d  %llu\n",
2705fa9e4066Sahrens 				    e, (u_longlong_t)zcb.zcb_errors[e]);
2706fa9e4066Sahrens 			}
2707fa9e4066Sahrens 		}
2708fa9e4066Sahrens 	}
2709fa9e4066Sahrens 
2710fa9e4066Sahrens 	/*
2711fa9e4066Sahrens 	 * Report any leaked segments.
2712fa9e4066Sahrens 	 */
2713b24ab676SJeff Bonwick 	zdb_leak_fini(spa);
2714fa9e4066Sahrens 
2715b24ab676SJeff Bonwick 	tzb = &zcb.zcb_type[ZB_TOTAL][ZDB_OT_TOTAL];
27168654d025Sperrin 
2717b24ab676SJeff Bonwick 	norm_alloc = metaslab_class_get_alloc(spa_normal_class(spa));
2718b24ab676SJeff Bonwick 	norm_space = metaslab_class_get_space(spa_normal_class(spa));
27198654d025Sperrin 
2720b24ab676SJeff Bonwick 	total_alloc = norm_alloc + metaslab_class_get_alloc(spa_log_class(spa));
2721b24ab676SJeff Bonwick 	total_found = tzb->zb_asize - zcb.zcb_dedup_asize;
2722fa9e4066Sahrens 
2723b24ab676SJeff Bonwick 	if (total_found == total_alloc) {
272482a0a985SVictor Latushkin 		if (!dump_opt['L'])
272582a0a985SVictor Latushkin 			(void) printf("\n\tNo leaks (block sum matches space"
272682a0a985SVictor Latushkin 			    " maps exactly)\n");
2727fa9e4066Sahrens 	} else {
2728fa9e4066Sahrens 		(void) printf("block traversal size %llu != alloc %llu "
272982a0a985SVictor Latushkin 		    "(%s %lld)\n",
2730b24ab676SJeff Bonwick 		    (u_longlong_t)total_found,
2731b24ab676SJeff Bonwick 		    (u_longlong_t)total_alloc,
273282a0a985SVictor Latushkin 		    (dump_opt['L']) ? "unreachable" : "leaked",
2733b24ab676SJeff Bonwick 		    (longlong_t)(total_alloc - total_found));
27345d7b4d43SMatthew Ahrens 		leaks = B_TRUE;
2735fa9e4066Sahrens 	}
2736fa9e4066Sahrens 
2737fa9e4066Sahrens 	if (tzb->zb_count == 0)
2738fa9e4066Sahrens 		return (2);
2739fa9e4066Sahrens 
2740fa9e4066Sahrens 	(void) printf("\n");
2741fa9e4066Sahrens 	(void) printf("\tbp count:      %10llu\n",
2742fa9e4066Sahrens 	    (u_longlong_t)tzb->zb_count);
2743d5ee8a13SMatthew Ahrens 	(void) printf("\tganged count:  %10llu\n",
2744d5ee8a13SMatthew Ahrens 	    (longlong_t)tzb->zb_gangs);
2745b24ab676SJeff Bonwick 	(void) printf("\tbp logical:    %10llu      avg: %6llu\n",
2746fa9e4066Sahrens 	    (u_longlong_t)tzb->zb_lsize,
2747fa9e4066Sahrens 	    (u_longlong_t)(tzb->zb_lsize / tzb->zb_count));
2748b24ab676SJeff Bonwick 	(void) printf("\tbp physical:   %10llu      avg:"
2749b24ab676SJeff Bonwick 	    " %6llu     compression: %6.2f\n",
2750fa9e4066Sahrens 	    (u_longlong_t)tzb->zb_psize,
2751fa9e4066Sahrens 	    (u_longlong_t)(tzb->zb_psize / tzb->zb_count),
2752fa9e4066Sahrens 	    (double)tzb->zb_lsize / tzb->zb_psize);
2753b24ab676SJeff Bonwick 	(void) printf("\tbp allocated:  %10llu      avg:"
2754b24ab676SJeff Bonwick 	    " %6llu     compression: %6.2f\n",
2755fa9e4066Sahrens 	    (u_longlong_t)tzb->zb_asize,
2756fa9e4066Sahrens 	    (u_longlong_t)(tzb->zb_asize / tzb->zb_count),
2757fa9e4066Sahrens 	    (double)tzb->zb_lsize / tzb->zb_asize);
2758b24ab676SJeff Bonwick 	(void) printf("\tbp deduped:    %10llu    ref>1:"
2759b24ab676SJeff Bonwick 	    " %6llu   deduplication: %6.2f\n",
2760b24ab676SJeff Bonwick 	    (u_longlong_t)zcb.zcb_dedup_asize,
2761b24ab676SJeff Bonwick 	    (u_longlong_t)zcb.zcb_dedup_blocks,
2762b24ab676SJeff Bonwick 	    (double)zcb.zcb_dedup_asize / tzb->zb_asize + 1.0);
2763b24ab676SJeff Bonwick 	(void) printf("\tSPA allocated: %10llu     used: %5.2f%%\n",
2764b24ab676SJeff Bonwick 	    (u_longlong_t)norm_alloc, 100.0 * norm_alloc / norm_space);
2765fa9e4066Sahrens 
27665d7b4d43SMatthew Ahrens 	for (bp_embedded_type_t i = 0; i < NUM_BP_EMBEDDED_TYPES; i++) {
27675d7b4d43SMatthew Ahrens 		if (zcb.zcb_embedded_blocks[i] == 0)
27685d7b4d43SMatthew Ahrens 			continue;
27695d7b4d43SMatthew Ahrens 		(void) printf("\n");
27705d7b4d43SMatthew Ahrens 		(void) printf("\tadditional, non-pointer bps of type %u: "
27715d7b4d43SMatthew Ahrens 		    "%10llu\n",
27725d7b4d43SMatthew Ahrens 		    i, (u_longlong_t)zcb.zcb_embedded_blocks[i]);
27735d7b4d43SMatthew Ahrens 
27745d7b4d43SMatthew Ahrens 		if (dump_opt['b'] >= 3) {
27755d7b4d43SMatthew Ahrens 			(void) printf("\t number of (compressed) bytes:  "
27765d7b4d43SMatthew Ahrens 			    "number of bps\n");
27775d7b4d43SMatthew Ahrens 			dump_histogram(zcb.zcb_embedded_histogram[i],
27785d7b4d43SMatthew Ahrens 			    sizeof (zcb.zcb_embedded_histogram[i]) /
27795d7b4d43SMatthew Ahrens 			    sizeof (zcb.zcb_embedded_histogram[i][0]), 0);
27805d7b4d43SMatthew Ahrens 		}
27815d7b4d43SMatthew Ahrens 	}
27825d7b4d43SMatthew Ahrens 
2783d5ee8a13SMatthew Ahrens 	if (tzb->zb_ditto_samevdev != 0) {
2784d5ee8a13SMatthew Ahrens 		(void) printf("\tDittoed blocks on same vdev: %llu\n",
2785d5ee8a13SMatthew Ahrens 		    (longlong_t)tzb->zb_ditto_samevdev);
2786d5ee8a13SMatthew Ahrens 	}
2787d5ee8a13SMatthew Ahrens 
2788fa9e4066Sahrens 	if (dump_opt['b'] >= 2) {
2789fa9e4066Sahrens 		int l, t, level;
2790fa9e4066Sahrens 		(void) printf("\nBlocks\tLSIZE\tPSIZE\tASIZE"
2791fa9e4066Sahrens 		    "\t  avg\t comp\t%%Total\tType\n");
2792fa9e4066Sahrens 
2793b24ab676SJeff Bonwick 		for (t = 0; t <= ZDB_OT_TOTAL; t++) {
27943f9d6ad7SLin Ling 			char csize[32], lsize[32], psize[32], asize[32];
2795d5ee8a13SMatthew Ahrens 			char avg[32], gang[32];
2796fa9e4066Sahrens 			char *typename;
2797fa9e4066Sahrens 
2798b24ab676SJeff Bonwick 			if (t < DMU_OT_NUMTYPES)
2799b24ab676SJeff Bonwick 				typename = dmu_ot[t].ot_name;
2800b24ab676SJeff Bonwick 			else
2801b24ab676SJeff Bonwick 				typename = zdb_ot_extname[t - DMU_OT_NUMTYPES];
2802fa9e4066Sahrens 
2803fa9e4066Sahrens 			if (zcb.zcb_type[ZB_TOTAL][t].zb_asize == 0) {
2804fa9e4066Sahrens 				(void) printf("%6s\t%5s\t%5s\t%5s"
2805fa9e4066Sahrens 				    "\t%5s\t%5s\t%6s\t%s\n",
2806fa9e4066Sahrens 				    "-",
2807fa9e4066Sahrens 				    "-",
2808fa9e4066Sahrens 				    "-",
2809fa9e4066Sahrens 				    "-",
2810fa9e4066Sahrens 				    "-",
2811fa9e4066Sahrens 				    "-",
2812fa9e4066Sahrens 				    "-",
2813fa9e4066Sahrens 				    typename);
2814fa9e4066Sahrens 				continue;
2815fa9e4066Sahrens 			}
2816fa9e4066Sahrens 
2817fa9e4066Sahrens 			for (l = ZB_TOTAL - 1; l >= -1; l--) {
2818fa9e4066Sahrens 				level = (l == -1 ? ZB_TOTAL : l);
2819fa9e4066Sahrens 				zb = &zcb.zcb_type[level][t];
2820fa9e4066Sahrens 
2821fa9e4066Sahrens 				if (zb->zb_asize == 0)
2822fa9e4066Sahrens 					continue;
2823fa9e4066Sahrens 
2824fa9e4066Sahrens 				if (dump_opt['b'] < 3 && level != ZB_TOTAL)
2825fa9e4066Sahrens 					continue;
2826fa9e4066Sahrens 
2827fa9e4066Sahrens 				if (level == 0 && zb->zb_asize ==
2828fa9e4066Sahrens 				    zcb.zcb_type[ZB_TOTAL][t].zb_asize)
2829fa9e4066Sahrens 					continue;
2830fa9e4066Sahrens 
28313f9d6ad7SLin Ling 				zdb_nicenum(zb->zb_count, csize);
28323f9d6ad7SLin Ling 				zdb_nicenum(zb->zb_lsize, lsize);
28333f9d6ad7SLin Ling 				zdb_nicenum(zb->zb_psize, psize);
28343f9d6ad7SLin Ling 				zdb_nicenum(zb->zb_asize, asize);
28353f9d6ad7SLin Ling 				zdb_nicenum(zb->zb_asize / zb->zb_count, avg);
2836d5ee8a13SMatthew Ahrens 				zdb_nicenum(zb->zb_gangs, gang);
2837fa9e4066Sahrens 
2838fa9e4066Sahrens 				(void) printf("%6s\t%5s\t%5s\t%5s\t%5s"
2839fa9e4066Sahrens 				    "\t%5.2f\t%6.2f\t",
2840fa9e4066Sahrens 				    csize, lsize, psize, asize, avg,
2841fa9e4066Sahrens 				    (double)zb->zb_lsize / zb->zb_psize,
2842fa9e4066Sahrens 				    100.0 * zb->zb_asize / tzb->zb_asize);
2843fa9e4066Sahrens 
2844fa9e4066Sahrens 				if (level == ZB_TOTAL)
2845fa9e4066Sahrens 					(void) printf("%s\n", typename);
2846fa9e4066Sahrens 				else
2847fa9e4066Sahrens 					(void) printf("    L%d %s\n",
2848fa9e4066Sahrens 					    level, typename);
2849490d05b9SMatthew Ahrens 
2850d5ee8a13SMatthew Ahrens 				if (dump_opt['b'] >= 3 && zb->zb_gangs > 0) {
2851d5ee8a13SMatthew Ahrens 					(void) printf("\t number of ganged "
2852d5ee8a13SMatthew Ahrens 					    "blocks: %s\n", gang);
2853d5ee8a13SMatthew Ahrens 				}
2854d5ee8a13SMatthew Ahrens 
2855490d05b9SMatthew Ahrens 				if (dump_opt['b'] >= 4) {
2856490d05b9SMatthew Ahrens 					(void) printf("psize "
2857490d05b9SMatthew Ahrens 					    "(in 512-byte sectors): "
2858490d05b9SMatthew Ahrens 					    "number of blocks\n");
2859490d05b9SMatthew Ahrens 					dump_histogram(zb->zb_psize_histogram,
28600713e232SGeorge Wilson 					    PSIZE_HISTO_SIZE, 0);
2861490d05b9SMatthew Ahrens 				}
2862fa9e4066Sahrens 			}
2863fa9e4066Sahrens 		}
2864fa9e4066Sahrens 	}
2865fa9e4066Sahrens 
2866fa9e4066Sahrens 	(void) printf("\n");
2867fa9e4066Sahrens 
2868fa9e4066Sahrens 	if (leaks)
2869fa9e4066Sahrens 		return (2);
2870fa9e4066Sahrens 
2871fa9e4066Sahrens 	if (zcb.zcb_haderrors)
2872fa9e4066Sahrens 		return (3);
2873fa9e4066Sahrens 
2874fa9e4066Sahrens 	return (0);
2875fa9e4066Sahrens }
2876fa9e4066Sahrens 
2877b24ab676SJeff Bonwick typedef struct zdb_ddt_entry {
2878b24ab676SJeff Bonwick 	ddt_key_t	zdde_key;
2879b24ab676SJeff Bonwick 	uint64_t	zdde_ref_blocks;
2880b24ab676SJeff Bonwick 	uint64_t	zdde_ref_lsize;
2881b24ab676SJeff Bonwick 	uint64_t	zdde_ref_psize;
2882b24ab676SJeff Bonwick 	uint64_t	zdde_ref_dsize;
2883b24ab676SJeff Bonwick 	avl_node_t	zdde_node;
2884b24ab676SJeff Bonwick } zdb_ddt_entry_t;
2885b24ab676SJeff Bonwick 
2886b24ab676SJeff Bonwick /* ARGSUSED */
2887b24ab676SJeff Bonwick static int
2888b24ab676SJeff Bonwick zdb_ddt_add_cb(spa_t *spa, zilog_t *zilog, const blkptr_t *bp,
28897802d7bfSMatthew Ahrens     const zbookmark_phys_t *zb, const dnode_phys_t *dnp, void *arg)
2890b24ab676SJeff Bonwick {
2891b24ab676SJeff Bonwick 	avl_tree_t *t = arg;
2892b24ab676SJeff Bonwick 	avl_index_t where;
2893b24ab676SJeff Bonwick 	zdb_ddt_entry_t *zdde, zdde_search;
2894b24ab676SJeff Bonwick 
28955d7b4d43SMatthew Ahrens 	if (BP_IS_HOLE(bp) || BP_IS_EMBEDDED(bp))
2896b24ab676SJeff Bonwick 		return (0);
2897b24ab676SJeff Bonwick 
2898b24ab676SJeff Bonwick 	if (dump_opt['S'] > 1 && zb->zb_level == ZB_ROOT_LEVEL) {
2899b24ab676SJeff Bonwick 		(void) printf("traversing objset %llu, %llu objects, "
2900b24ab676SJeff Bonwick 		    "%lu blocks so far\n",
2901b24ab676SJeff Bonwick 		    (u_longlong_t)zb->zb_objset,
29025d7b4d43SMatthew Ahrens 		    (u_longlong_t)BP_GET_FILL(bp),
2903b24ab676SJeff Bonwick 		    avl_numnodes(t));
2904b24ab676SJeff Bonwick 	}
2905b24ab676SJeff Bonwick 
2906bbfd46c4SJeff Bonwick 	if (BP_IS_HOLE(bp) || BP_GET_CHECKSUM(bp) == ZIO_CHECKSUM_OFF ||
2907ad135b5dSChristopher Siden 	    BP_GET_LEVEL(bp) > 0 || DMU_OT_IS_METADATA(BP_GET_TYPE(bp)))
2908b24ab676SJeff Bonwick 		return (0);
2909b24ab676SJeff Bonwick 
2910b24ab676SJeff Bonwick 	ddt_key_fill(&zdde_search.zdde_key, bp);
2911b24ab676SJeff Bonwick 
2912b24ab676SJeff Bonwick 	zdde = avl_find(t, &zdde_search, &where);
2913b24ab676SJeff Bonwick 
2914b24ab676SJeff Bonwick 	if (zdde == NULL) {
2915b24ab676SJeff Bonwick 		zdde = umem_zalloc(sizeof (*zdde), UMEM_NOFAIL);
2916b24ab676SJeff Bonwick 		zdde->zdde_key = zdde_search.zdde_key;
2917b24ab676SJeff Bonwick 		avl_insert(t, zdde, where);
2918b24ab676SJeff Bonwick 	}
2919b24ab676SJeff Bonwick 
2920b24ab676SJeff Bonwick 	zdde->zdde_ref_blocks += 1;
2921b24ab676SJeff Bonwick 	zdde->zdde_ref_lsize += BP_GET_LSIZE(bp);
2922b24ab676SJeff Bonwick 	zdde->zdde_ref_psize += BP_GET_PSIZE(bp);
2923b24ab676SJeff Bonwick 	zdde->zdde_ref_dsize += bp_get_dsize_sync(spa, bp);
2924b24ab676SJeff Bonwick 
2925b24ab676SJeff Bonwick 	return (0);
2926b24ab676SJeff Bonwick }
2927b24ab676SJeff Bonwick 
2928b24ab676SJeff Bonwick static void
2929b24ab676SJeff Bonwick dump_simulated_ddt(spa_t *spa)
2930b24ab676SJeff Bonwick {
2931b24ab676SJeff Bonwick 	avl_tree_t t;
2932b24ab676SJeff Bonwick 	void *cookie = NULL;
2933b24ab676SJeff Bonwick 	zdb_ddt_entry_t *zdde;
2934b24ab676SJeff Bonwick 	ddt_histogram_t ddh_total = { 0 };
2935b24ab676SJeff Bonwick 	ddt_stat_t dds_total = { 0 };
2936b24ab676SJeff Bonwick 
2937b24ab676SJeff Bonwick 	avl_create(&t, ddt_entry_compare,
2938b24ab676SJeff Bonwick 	    sizeof (zdb_ddt_entry_t), offsetof(zdb_ddt_entry_t, zdde_node));
2939b24ab676SJeff Bonwick 
2940b24ab676SJeff Bonwick 	spa_config_enter(spa, SCL_CONFIG, FTAG, RW_READER);
2941b24ab676SJeff Bonwick 
2942bbfd46c4SJeff Bonwick 	(void) traverse_pool(spa, 0, TRAVERSE_PRE | TRAVERSE_PREFETCH_METADATA,
2943bbfd46c4SJeff Bonwick 	    zdb_ddt_add_cb, &t);
2944b24ab676SJeff Bonwick 
2945b24ab676SJeff Bonwick 	spa_config_exit(spa, SCL_CONFIG, FTAG);
2946b24ab676SJeff Bonwick 
2947b24ab676SJeff Bonwick 	while ((zdde = avl_destroy_nodes(&t, &cookie)) != NULL) {
2948b24ab676SJeff Bonwick 		ddt_stat_t dds;
2949b24ab676SJeff Bonwick 		uint64_t refcnt = zdde->zdde_ref_blocks;
2950b24ab676SJeff Bonwick 		ASSERT(refcnt != 0);
2951b24ab676SJeff Bonwick 
2952b24ab676SJeff Bonwick 		dds.dds_blocks = zdde->zdde_ref_blocks / refcnt;
2953b24ab676SJeff Bonwick 		dds.dds_lsize = zdde->zdde_ref_lsize / refcnt;
2954b24ab676SJeff Bonwick 		dds.dds_psize = zdde->zdde_ref_psize / refcnt;
2955b24ab676SJeff Bonwick 		dds.dds_dsize = zdde->zdde_ref_dsize / refcnt;
2956b24ab676SJeff Bonwick 
2957b24ab676SJeff Bonwick 		dds.dds_ref_blocks = zdde->zdde_ref_blocks;
2958b24ab676SJeff Bonwick 		dds.dds_ref_lsize = zdde->zdde_ref_lsize;
2959b24ab676SJeff Bonwick 		dds.dds_ref_psize = zdde->zdde_ref_psize;
2960b24ab676SJeff Bonwick 		dds.dds_ref_dsize = zdde->zdde_ref_dsize;
2961b24ab676SJeff Bonwick 
2962bf16b11eSMatthew Ahrens 		ddt_stat_add(&ddh_total.ddh_stat[highbit64(refcnt) - 1],
2963bf16b11eSMatthew Ahrens 		    &dds, 0);
2964b24ab676SJeff Bonwick 
2965b24ab676SJeff Bonwick 		umem_free(zdde, sizeof (*zdde));
2966b24ab676SJeff Bonwick 	}
2967b24ab676SJeff Bonwick 
2968b24ab676SJeff Bonwick 	avl_destroy(&t);
2969b24ab676SJeff Bonwick 
2970b24ab676SJeff Bonwick 	ddt_histogram_stat(&dds_total, &ddh_total);
2971b24ab676SJeff Bonwick 
2972b24ab676SJeff Bonwick 	(void) printf("Simulated DDT histogram:\n");
2973b24ab676SJeff Bonwick 
29749eb19f4dSGeorge Wilson 	zpool_dump_ddt(&dds_total, &ddh_total);
2975b24ab676SJeff Bonwick 
2976b24ab676SJeff Bonwick 	dump_dedup_ratio(&dds_total);
2977b24ab676SJeff Bonwick }
2978b24ab676SJeff Bonwick 
2979fa9e4066Sahrens static void
2980fa9e4066Sahrens dump_zpool(spa_t *spa)
2981fa9e4066Sahrens {
2982fa9e4066Sahrens 	dsl_pool_t *dp = spa_get_dsl(spa);
2983fa9e4066Sahrens 	int rc = 0;
2984fa9e4066Sahrens 
2985b24ab676SJeff Bonwick 	if (dump_opt['S']) {
2986b24ab676SJeff Bonwick 		dump_simulated_ddt(spa);
2987b24ab676SJeff Bonwick 		return;
2988b24ab676SJeff Bonwick 	}
2989b24ab676SJeff Bonwick 
299007428bdfSVictor Latushkin 	if (!dump_opt['e'] && dump_opt['C'] > 1) {
299107428bdfSVictor Latushkin 		(void) printf("\nCached configuration:\n");
299207428bdfSVictor Latushkin 		dump_nvlist(spa->spa_config, 8);
299307428bdfSVictor Latushkin 	}
299407428bdfSVictor Latushkin 
299507428bdfSVictor Latushkin 	if (dump_opt['C'])
299607428bdfSVictor Latushkin 		dump_config(spa);
299707428bdfSVictor Latushkin 
2998fa9e4066Sahrens 	if (dump_opt['u'])
299953b9a4a9SVictor Latushkin 		dump_uberblock(&spa->spa_uberblock, "\nUberblock:\n", "\n");
3000fa9e4066Sahrens 
3001b24ab676SJeff Bonwick 	if (dump_opt['D'])
3002b24ab676SJeff Bonwick 		dump_all_ddts(spa);
3003b24ab676SJeff Bonwick 
300487219db7SVictor Latushkin 	if (dump_opt['d'] > 2 || dump_opt['m'])
300587219db7SVictor Latushkin 		dump_metaslabs(spa);
30062e4c9986SGeorge Wilson 	if (dump_opt['M'])
30072e4c9986SGeorge Wilson 		dump_metaslab_groups(spa);
300887219db7SVictor Latushkin 
300987219db7SVictor Latushkin 	if (dump_opt['d'] || dump_opt['i']) {
3010b5152584SMatthew Ahrens 		uint64_t refcount;
3011fa9e4066Sahrens 		dump_dir(dp->dp_meta_objset);
3012fa9e4066Sahrens 		if (dump_opt['d'] >= 3) {
3013*732885fcSMatthew Ahrens 			dump_full_bpobj(&spa->spa_deferred_bpobj,
3014d0475637SMatthew Ahrens 			    "Deferred frees", 0);
3015cde58dbcSMatthew Ahrens 			if (spa_version(spa) >= SPA_VERSION_DEADLISTS) {
3016*732885fcSMatthew Ahrens 				dump_full_bpobj(
3017*732885fcSMatthew Ahrens 				    &spa->spa_dsl_pool->dp_free_bpobj,
3018d0475637SMatthew Ahrens 				    "Pool snapshot frees", 0);
3019ad135b5dSChristopher Siden 			}
3020ad135b5dSChristopher Siden 
3021ad135b5dSChristopher Siden 			if (spa_feature_is_active(spa,
30222acef22dSMatthew Ahrens 			    SPA_FEATURE_ASYNC_DESTROY)) {
3023ad135b5dSChristopher Siden 				dump_bptree(spa->spa_meta_objset,
3024ad135b5dSChristopher Siden 				    spa->spa_dsl_pool->dp_bptree_obj,
3025ad135b5dSChristopher Siden 				    "Pool dataset frees");
3026cde58dbcSMatthew Ahrens 			}
3027fa9e4066Sahrens 			dump_dtl(spa->spa_root_vdev, 0);
3028fa9e4066Sahrens 		}
302907428bdfSVictor Latushkin 		(void) dmu_objset_find(spa_name(spa), dump_one_dir,
303007428bdfSVictor Latushkin 		    NULL, DS_FIND_SNAPSHOTS | DS_FIND_CHILDREN);
3031b5152584SMatthew Ahrens 
3032b5152584SMatthew Ahrens 		(void) feature_get_refcount(spa,
3033b5152584SMatthew Ahrens 		    &spa_feature_table[SPA_FEATURE_LARGE_BLOCKS], &refcount);
3034b5152584SMatthew Ahrens 		if (num_large_blocks != refcount) {
3035b5152584SMatthew Ahrens 			(void) printf("large_blocks feature refcount mismatch: "
3036b5152584SMatthew Ahrens 			    "expected %lld != actual %lld\n",
3037b5152584SMatthew Ahrens 			    (longlong_t)num_large_blocks,
3038b5152584SMatthew Ahrens 			    (longlong_t)refcount);
3039b5152584SMatthew Ahrens 			rc = 2;
3040b5152584SMatthew Ahrens 		} else {
3041b5152584SMatthew Ahrens 			(void) printf("Verified large_blocks feature refcount "
3042b5152584SMatthew Ahrens 			    "is correct (%llu)\n", (longlong_t)refcount);
3043b5152584SMatthew Ahrens 		}
3044fa9e4066Sahrens 	}
3045b5152584SMatthew Ahrens 	if (rc == 0 && (dump_opt['b'] || dump_opt['c']))
3046fa9e4066Sahrens 		rc = dump_block_stats(spa);
3047fa9e4066Sahrens 
30480713e232SGeorge Wilson 	if (rc == 0)
30490713e232SGeorge Wilson 		rc = verify_spacemap_refcounts(spa);
30500713e232SGeorge Wilson 
3051fa9e4066Sahrens 	if (dump_opt['s'])
3052fa9e4066Sahrens 		show_pool_stats(spa);
3053fa9e4066Sahrens 
30548f18d1faSGeorge Wilson 	if (dump_opt['h'])
30558f18d1faSGeorge Wilson 		dump_history(spa);
30568f18d1faSGeorge Wilson 
3057fa9e4066Sahrens 	if (rc != 0)
3058fa9e4066Sahrens 		exit(rc);
3059fa9e4066Sahrens }
3060fa9e4066Sahrens 
306144cd46caSbillm #define	ZDB_FLAG_CHECKSUM	0x0001
306244cd46caSbillm #define	ZDB_FLAG_DECOMPRESS	0x0002
306344cd46caSbillm #define	ZDB_FLAG_BSWAP		0x0004
306444cd46caSbillm #define	ZDB_FLAG_GBH		0x0008
306544cd46caSbillm #define	ZDB_FLAG_INDIRECT	0x0010
306644cd46caSbillm #define	ZDB_FLAG_PHYS		0x0020
306744cd46caSbillm #define	ZDB_FLAG_RAW		0x0040
306844cd46caSbillm #define	ZDB_FLAG_PRINT_BLKPTR	0x0080
306944cd46caSbillm 
307044cd46caSbillm int flagbits[256];
307144cd46caSbillm 
307244cd46caSbillm static void
307344cd46caSbillm zdb_print_blkptr(blkptr_t *bp, int flags)
307444cd46caSbillm {
3075b24ab676SJeff Bonwick 	char blkbuf[BP_SPRINTF_LEN];
307644cd46caSbillm 
307744cd46caSbillm 	if (flags & ZDB_FLAG_BSWAP)
307844cd46caSbillm 		byteswap_uint64_array((void *)bp, sizeof (blkptr_t));
3079b24ab676SJeff Bonwick 
308043466aaeSMax Grossman 	snprintf_blkptr(blkbuf, sizeof (blkbuf), bp);
3081b24ab676SJeff Bonwick 	(void) printf("%s\n", blkbuf);
308244cd46caSbillm }
308344cd46caSbillm 
308444cd46caSbillm static void
308544cd46caSbillm zdb_dump_indirect(blkptr_t *bp, int nbps, int flags)
308644cd46caSbillm {
308744cd46caSbillm 	int i;
308844cd46caSbillm 
308944cd46caSbillm 	for (i = 0; i < nbps; i++)
309044cd46caSbillm 		zdb_print_blkptr(&bp[i], flags);
309144cd46caSbillm }
309244cd46caSbillm 
309344cd46caSbillm static void
309444cd46caSbillm zdb_dump_gbh(void *buf, int flags)
309544cd46caSbillm {
309644cd46caSbillm 	zdb_dump_indirect((blkptr_t *)buf, SPA_GBH_NBLKPTRS, flags);
309744cd46caSbillm }
309844cd46caSbillm 
309944cd46caSbillm static void
310044cd46caSbillm zdb_dump_block_raw(void *buf, uint64_t size, int flags)
310144cd46caSbillm {
310244cd46caSbillm 	if (flags & ZDB_FLAG_BSWAP)
310344cd46caSbillm 		byteswap_uint64_array(buf, size);
3104b24ab676SJeff Bonwick 	(void) write(1, buf, size);
310544cd46caSbillm }
310644cd46caSbillm 
310744cd46caSbillm static void
310844cd46caSbillm zdb_dump_block(char *label, void *buf, uint64_t size, int flags)
310944cd46caSbillm {
311044cd46caSbillm 	uint64_t *d = (uint64_t *)buf;
311144cd46caSbillm 	int nwords = size / sizeof (uint64_t);
311244cd46caSbillm 	int do_bswap = !!(flags & ZDB_FLAG_BSWAP);
311344cd46caSbillm 	int i, j;
311444cd46caSbillm 	char *hdr, *c;
311544cd46caSbillm 
311644cd46caSbillm 
311744cd46caSbillm 	if (do_bswap)
311844cd46caSbillm 		hdr = " 7 6 5 4 3 2 1 0   f e d c b a 9 8";
311944cd46caSbillm 	else
312044cd46caSbillm 		hdr = " 0 1 2 3 4 5 6 7   8 9 a b c d e f";
312144cd46caSbillm 
312244cd46caSbillm 	(void) printf("\n%s\n%6s   %s  0123456789abcdef\n", label, "", hdr);
312344cd46caSbillm 
312444cd46caSbillm 	for (i = 0; i < nwords; i += 2) {
312544cd46caSbillm 		(void) printf("%06llx:  %016llx  %016llx  ",
312644cd46caSbillm 		    (u_longlong_t)(i * sizeof (uint64_t)),
312744cd46caSbillm 		    (u_longlong_t)(do_bswap ? BSWAP_64(d[i]) : d[i]),
312844cd46caSbillm 		    (u_longlong_t)(do_bswap ? BSWAP_64(d[i + 1]) : d[i + 1]));
312944cd46caSbillm 
313044cd46caSbillm 		c = (char *)&d[i];
313144cd46caSbillm 		for (j = 0; j < 2 * sizeof (uint64_t); j++)
313244cd46caSbillm 			(void) printf("%c", isprint(c[j]) ? c[j] : '.');
313344cd46caSbillm 		(void) printf("\n");
313444cd46caSbillm 	}
313544cd46caSbillm }
313644cd46caSbillm 
313744cd46caSbillm /*
313844cd46caSbillm  * There are two acceptable formats:
313944cd46caSbillm  *	leaf_name	  - For example: c1t0d0 or /tmp/ztest.0a
314044cd46caSbillm  *	child[.child]*    - For example: 0.1.1
314144cd46caSbillm  *
314244cd46caSbillm  * The second form can be used to specify arbitrary vdevs anywhere
314344cd46caSbillm  * in the heirarchy.  For example, in a pool with a mirror of
314444cd46caSbillm  * RAID-Zs, you can specify either RAID-Z vdev with 0.0 or 0.1 .
314544cd46caSbillm  */
314644cd46caSbillm static vdev_t *
314744cd46caSbillm zdb_vdev_lookup(vdev_t *vdev, char *path)
314844cd46caSbillm {
314944cd46caSbillm 	char *s, *p, *q;
315044cd46caSbillm 	int i;
315144cd46caSbillm 
315244cd46caSbillm 	if (vdev == NULL)
315344cd46caSbillm 		return (NULL);
315444cd46caSbillm 
315544cd46caSbillm 	/* First, assume the x.x.x.x format */
315644cd46caSbillm 	i = (int)strtoul(path, &s, 10);
315744cd46caSbillm 	if (s == path || (s && *s != '.' && *s != '\0'))
315844cd46caSbillm 		goto name;
315944cd46caSbillm 	if (i < 0 || i >= vdev->vdev_children)
316044cd46caSbillm 		return (NULL);
316144cd46caSbillm 
316244cd46caSbillm 	vdev = vdev->vdev_child[i];
316344cd46caSbillm 	if (*s == '\0')
316444cd46caSbillm 		return (vdev);
316544cd46caSbillm 	return (zdb_vdev_lookup(vdev, s+1));
316644cd46caSbillm 
316744cd46caSbillm name:
316844cd46caSbillm 	for (i = 0; i < vdev->vdev_children; i++) {
316944cd46caSbillm 		vdev_t *vc = vdev->vdev_child[i];
317044cd46caSbillm 
317144cd46caSbillm 		if (vc->vdev_path == NULL) {
317244cd46caSbillm 			vc = zdb_vdev_lookup(vc, path);
317344cd46caSbillm 			if (vc == NULL)
317444cd46caSbillm 				continue;
317544cd46caSbillm 			else
317644cd46caSbillm 				return (vc);
317744cd46caSbillm 		}
317844cd46caSbillm 
317944cd46caSbillm 		p = strrchr(vc->vdev_path, '/');
318044cd46caSbillm 		p = p ? p + 1 : vc->vdev_path;
318144cd46caSbillm 		q = &vc->vdev_path[strlen(vc->vdev_path) - 2];
318244cd46caSbillm 
318344cd46caSbillm 		if (strcmp(vc->vdev_path, path) == 0)
318444cd46caSbillm 			return (vc);
318544cd46caSbillm 		if (strcmp(p, path) == 0)
318644cd46caSbillm 			return (vc);
318744cd46caSbillm 		if (strcmp(q, "s0") == 0 && strncmp(p, path, q - p) == 0)
318844cd46caSbillm 			return (vc);
318944cd46caSbillm 	}
319044cd46caSbillm 
319144cd46caSbillm 	return (NULL);
319244cd46caSbillm }
319344cd46caSbillm 
319444cd46caSbillm /*
319544cd46caSbillm  * Read a block from a pool and print it out.  The syntax of the
319644cd46caSbillm  * block descriptor is:
319744cd46caSbillm  *
319844cd46caSbillm  *	pool:vdev_specifier:offset:size[:flags]
319944cd46caSbillm  *
320044cd46caSbillm  *	pool           - The name of the pool you wish to read from
320144cd46caSbillm  *	vdev_specifier - Which vdev (see comment for zdb_vdev_lookup)
320244cd46caSbillm  *	offset         - offset, in hex, in bytes
320344cd46caSbillm  *	size           - Amount of data to read, in hex, in bytes
320444cd46caSbillm  *	flags          - A string of characters specifying options
320544cd46caSbillm  *		 b: Decode a blkptr at given offset within block
320644cd46caSbillm  *		*c: Calculate and display checksums
3207b24ab676SJeff Bonwick  *		 d: Decompress data before dumping
320844cd46caSbillm  *		 e: Byteswap data before dumping
3209b24ab676SJeff Bonwick  *		 g: Display data as a gang block header
3210b24ab676SJeff Bonwick  *		 i: Display as an indirect block
321144cd46caSbillm  *		 p: Do I/O to physical offset
321244cd46caSbillm  *		 r: Dump raw data to stdout
321344cd46caSbillm  *
321444cd46caSbillm  *              * = not yet implemented
321544cd46caSbillm  */
321644cd46caSbillm static void
321707428bdfSVictor Latushkin zdb_read_block(char *thing, spa_t *spa)
321844cd46caSbillm {
3219b24ab676SJeff Bonwick 	blkptr_t blk, *bp = &blk;
3220b24ab676SJeff Bonwick 	dva_t *dva = bp->blk_dva;
322144cd46caSbillm 	int flags = 0;
3222b24ab676SJeff Bonwick 	uint64_t offset = 0, size = 0, psize = 0, lsize = 0, blkptr_offset = 0;
322344cd46caSbillm 	zio_t *zio;
322444cd46caSbillm 	vdev_t *vd;
3225b24ab676SJeff Bonwick 	void *pbuf, *lbuf, *buf;
322607428bdfSVictor Latushkin 	char *s, *p, *dup, *vdev, *flagstr;
3227b24ab676SJeff Bonwick 	int i, error;
322844cd46caSbillm 
322944cd46caSbillm 	dup = strdup(thing);
323044cd46caSbillm 	s = strtok(dup, ":");
323144cd46caSbillm 	vdev = s ? s : "";
323244cd46caSbillm 	s = strtok(NULL, ":");
323344cd46caSbillm 	offset = strtoull(s ? s : "", NULL, 16);
323444cd46caSbillm 	s = strtok(NULL, ":");
323544cd46caSbillm 	size = strtoull(s ? s : "", NULL, 16);
323644cd46caSbillm 	s = strtok(NULL, ":");
323744cd46caSbillm 	flagstr = s ? s : "";
323844cd46caSbillm 
323944cd46caSbillm 	s = NULL;
324044cd46caSbillm 	if (size == 0)
324144cd46caSbillm 		s = "size must not be zero";
324244cd46caSbillm 	if (!IS_P2ALIGNED(size, DEV_BSIZE))
324344cd46caSbillm 		s = "size must be a multiple of sector size";
324444cd46caSbillm 	if (!IS_P2ALIGNED(offset, DEV_BSIZE))
324544cd46caSbillm 		s = "offset must be a multiple of sector size";
324644cd46caSbillm 	if (s) {
324744cd46caSbillm 		(void) printf("Invalid block specifier: %s  - %s\n", thing, s);
324844cd46caSbillm 		free(dup);
324944cd46caSbillm 		return;
325044cd46caSbillm 	}
325144cd46caSbillm 
325244cd46caSbillm 	for (s = strtok(flagstr, ":"); s; s = strtok(NULL, ":")) {
325344cd46caSbillm 		for (i = 0; flagstr[i]; i++) {
32545ad82045Snd 			int bit = flagbits[(uchar_t)flagstr[i]];
325544cd46caSbillm 
325644cd46caSbillm 			if (bit == 0) {
325744cd46caSbillm 				(void) printf("***Invalid flag: %c\n",
325844cd46caSbillm 				    flagstr[i]);
325944cd46caSbillm 				continue;
326044cd46caSbillm 			}
326144cd46caSbillm 			flags |= bit;
326244cd46caSbillm 
326344cd46caSbillm 			/* If it's not something with an argument, keep going */
3264b24ab676SJeff Bonwick 			if ((bit & (ZDB_FLAG_CHECKSUM |
326544cd46caSbillm 			    ZDB_FLAG_PRINT_BLKPTR)) == 0)
326644cd46caSbillm 				continue;
326744cd46caSbillm 
326844cd46caSbillm 			p = &flagstr[i + 1];
326944cd46caSbillm 			if (bit == ZDB_FLAG_PRINT_BLKPTR)
327044cd46caSbillm 				blkptr_offset = strtoull(p, &p, 16);
327144cd46caSbillm 			if (*p != ':' && *p != '\0') {
327244cd46caSbillm 				(void) printf("***Invalid flag arg: '%s'\n", s);
327344cd46caSbillm 				free(dup);
327444cd46caSbillm 				return;
327544cd46caSbillm 			}
327644cd46caSbillm 		}
327744cd46caSbillm 	}
327844cd46caSbillm 
327944cd46caSbillm 	vd = zdb_vdev_lookup(spa->spa_root_vdev, vdev);
328044cd46caSbillm 	if (vd == NULL) {
328144cd46caSbillm 		(void) printf("***Invalid vdev: %s\n", vdev);
328244cd46caSbillm 		free(dup);
328344cd46caSbillm 		return;
328444cd46caSbillm 	} else {
328544cd46caSbillm 		if (vd->vdev_path)
3286b24ab676SJeff Bonwick 			(void) fprintf(stderr, "Found vdev: %s\n",
3287b24ab676SJeff Bonwick 			    vd->vdev_path);
328844cd46caSbillm 		else
3289b24ab676SJeff Bonwick 			(void) fprintf(stderr, "Found vdev type: %s\n",
329044cd46caSbillm 			    vd->vdev_ops->vdev_op_type);
329144cd46caSbillm 	}
329244cd46caSbillm 
3293b24ab676SJeff Bonwick 	psize = size;
3294b24ab676SJeff Bonwick 	lsize = size;
329544cd46caSbillm 
3296b24ab676SJeff Bonwick 	pbuf = umem_alloc(SPA_MAXBLOCKSIZE, UMEM_NOFAIL);
3297b24ab676SJeff Bonwick 	lbuf = umem_alloc(SPA_MAXBLOCKSIZE, UMEM_NOFAIL);
3298b24ab676SJeff Bonwick 
3299b24ab676SJeff Bonwick 	BP_ZERO(bp);
3300b24ab676SJeff Bonwick 
3301b24ab676SJeff Bonwick 	DVA_SET_VDEV(&dva[0], vd->vdev_id);
3302b24ab676SJeff Bonwick 	DVA_SET_OFFSET(&dva[0], offset);
3303b24ab676SJeff Bonwick 	DVA_SET_GANG(&dva[0], !!(flags & ZDB_FLAG_GBH));
3304b24ab676SJeff Bonwick 	DVA_SET_ASIZE(&dva[0], vdev_psize_to_asize(vd, psize));
3305b24ab676SJeff Bonwick 
3306b24ab676SJeff Bonwick 	BP_SET_BIRTH(bp, TXG_INITIAL, TXG_INITIAL);
3307b24ab676SJeff Bonwick 
3308b24ab676SJeff Bonwick 	BP_SET_LSIZE(bp, lsize);
3309b24ab676SJeff Bonwick 	BP_SET_PSIZE(bp, psize);
3310b24ab676SJeff Bonwick 	BP_SET_COMPRESS(bp, ZIO_COMPRESS_OFF);
3311b24ab676SJeff Bonwick 	BP_SET_CHECKSUM(bp, ZIO_CHECKSUM_OFF);
3312b24ab676SJeff Bonwick 	BP_SET_TYPE(bp, DMU_OT_NONE);
3313b24ab676SJeff Bonwick 	BP_SET_LEVEL(bp, 0);
3314b24ab676SJeff Bonwick 	BP_SET_DEDUP(bp, 0);
3315b24ab676SJeff Bonwick 	BP_SET_BYTEORDER(bp, ZFS_HOST_BYTEORDER);
331644cd46caSbillm 
3317e14bb325SJeff Bonwick 	spa_config_enter(spa, SCL_STATE, FTAG, RW_READER);
331844cd46caSbillm 	zio = zio_root(spa, NULL, NULL, 0);
3319b24ab676SJeff Bonwick 
3320b24ab676SJeff Bonwick 	if (vd == vd->vdev_top) {
3321b24ab676SJeff Bonwick 		/*
3322b24ab676SJeff Bonwick 		 * Treat this as a normal block read.
3323b24ab676SJeff Bonwick 		 */
3324b24ab676SJeff Bonwick 		zio_nowait(zio_read(zio, spa, bp, pbuf, psize, NULL, NULL,
3325b24ab676SJeff Bonwick 		    ZIO_PRIORITY_SYNC_READ,
3326b24ab676SJeff Bonwick 		    ZIO_FLAG_CANFAIL | ZIO_FLAG_RAW, NULL));
3327b24ab676SJeff Bonwick 	} else {
3328b24ab676SJeff Bonwick 		/*
3329b24ab676SJeff Bonwick 		 * Treat this as a vdev child I/O.
3330b24ab676SJeff Bonwick 		 */
3331b24ab676SJeff Bonwick 		zio_nowait(zio_vdev_child_io(zio, bp, vd, offset, pbuf, psize,
3332b24ab676SJeff Bonwick 		    ZIO_TYPE_READ, ZIO_PRIORITY_SYNC_READ,
3333b24ab676SJeff Bonwick 		    ZIO_FLAG_DONT_CACHE | ZIO_FLAG_DONT_QUEUE |
3334b24ab676SJeff Bonwick 		    ZIO_FLAG_DONT_PROPAGATE | ZIO_FLAG_DONT_RETRY |
3335b24ab676SJeff Bonwick 		    ZIO_FLAG_CANFAIL | ZIO_FLAG_RAW, NULL, NULL));
3336b24ab676SJeff Bonwick 	}
3337b24ab676SJeff Bonwick 
333844cd46caSbillm 	error = zio_wait(zio);
3339e14bb325SJeff Bonwick 	spa_config_exit(spa, SCL_STATE, FTAG);
334044cd46caSbillm 
334144cd46caSbillm 	if (error) {
334244cd46caSbillm 		(void) printf("Read of %s failed, error: %d\n", thing, error);
334344cd46caSbillm 		goto out;
334444cd46caSbillm 	}
334544cd46caSbillm 
3346b24ab676SJeff Bonwick 	if (flags & ZDB_FLAG_DECOMPRESS) {
3347b24ab676SJeff Bonwick 		/*
3348b24ab676SJeff Bonwick 		 * We don't know how the data was compressed, so just try
3349b24ab676SJeff Bonwick 		 * every decompress function at every inflated blocksize.
3350b24ab676SJeff Bonwick 		 */
3351b24ab676SJeff Bonwick 		enum zio_compress c;
3352b24ab676SJeff Bonwick 		void *pbuf2 = umem_alloc(SPA_MAXBLOCKSIZE, UMEM_NOFAIL);
3353b24ab676SJeff Bonwick 		void *lbuf2 = umem_alloc(SPA_MAXBLOCKSIZE, UMEM_NOFAIL);
3354b24ab676SJeff Bonwick 
3355b24ab676SJeff Bonwick 		bcopy(pbuf, pbuf2, psize);
3356b24ab676SJeff Bonwick 
3357b24ab676SJeff Bonwick 		VERIFY(random_get_pseudo_bytes((uint8_t *)pbuf + psize,
3358b24ab676SJeff Bonwick 		    SPA_MAXBLOCKSIZE - psize) == 0);
3359b24ab676SJeff Bonwick 
3360b24ab676SJeff Bonwick 		VERIFY(random_get_pseudo_bytes((uint8_t *)pbuf2 + psize,
3361b24ab676SJeff Bonwick 		    SPA_MAXBLOCKSIZE - psize) == 0);
3362b24ab676SJeff Bonwick 
3363b24ab676SJeff Bonwick 		for (lsize = SPA_MAXBLOCKSIZE; lsize > psize;
3364b24ab676SJeff Bonwick 		    lsize -= SPA_MINBLOCKSIZE) {
3365b24ab676SJeff Bonwick 			for (c = 0; c < ZIO_COMPRESS_FUNCTIONS; c++) {
3366b24ab676SJeff Bonwick 				if (zio_decompress_data(c, pbuf, lbuf,
3367b24ab676SJeff Bonwick 				    psize, lsize) == 0 &&
3368b24ab676SJeff Bonwick 				    zio_decompress_data(c, pbuf2, lbuf2,
3369b24ab676SJeff Bonwick 				    psize, lsize) == 0 &&
3370b24ab676SJeff Bonwick 				    bcmp(lbuf, lbuf2, lsize) == 0)
3371b24ab676SJeff Bonwick 					break;
3372b24ab676SJeff Bonwick 			}
3373b24ab676SJeff Bonwick 			if (c != ZIO_COMPRESS_FUNCTIONS)
3374b24ab676SJeff Bonwick 				break;
3375b24ab676SJeff Bonwick 			lsize -= SPA_MINBLOCKSIZE;
3376b24ab676SJeff Bonwick 		}
3377b24ab676SJeff Bonwick 
3378b24ab676SJeff Bonwick 		umem_free(pbuf2, SPA_MAXBLOCKSIZE);
3379b24ab676SJeff Bonwick 		umem_free(lbuf2, SPA_MAXBLOCKSIZE);
3380b24ab676SJeff Bonwick 
3381b24ab676SJeff Bonwick 		if (lsize <= psize) {
3382b24ab676SJeff Bonwick 			(void) printf("Decompress of %s failed\n", thing);
3383b24ab676SJeff Bonwick 			goto out;
3384b24ab676SJeff Bonwick 		}
3385b24ab676SJeff Bonwick 		buf = lbuf;
3386b24ab676SJeff Bonwick 		size = lsize;
3387b24ab676SJeff Bonwick 	} else {
3388b24ab676SJeff Bonwick 		buf = pbuf;
3389b24ab676SJeff Bonwick 		size = psize;
3390b24ab676SJeff Bonwick 	}
3391b24ab676SJeff Bonwick 
339244cd46caSbillm 	if (flags & ZDB_FLAG_PRINT_BLKPTR)
339344cd46caSbillm 		zdb_print_blkptr((blkptr_t *)(void *)
339444cd46caSbillm 		    ((uintptr_t)buf + (uintptr_t)blkptr_offset), flags);
339544cd46caSbillm 	else if (flags & ZDB_FLAG_RAW)
339644cd46caSbillm 		zdb_dump_block_raw(buf, size, flags);
339744cd46caSbillm 	else if (flags & ZDB_FLAG_INDIRECT)
339844cd46caSbillm 		zdb_dump_indirect((blkptr_t *)buf, size / sizeof (blkptr_t),
339944cd46caSbillm 		    flags);
340044cd46caSbillm 	else if (flags & ZDB_FLAG_GBH)
340144cd46caSbillm 		zdb_dump_gbh(buf, flags);
340244cd46caSbillm 	else
340344cd46caSbillm 		zdb_dump_block(thing, buf, size, flags);
340444cd46caSbillm 
340544cd46caSbillm out:
3406b24ab676SJeff Bonwick 	umem_free(pbuf, SPA_MAXBLOCKSIZE);
3407b24ab676SJeff Bonwick 	umem_free(lbuf, SPA_MAXBLOCKSIZE);
340844cd46caSbillm 	free(dup);
340944cd46caSbillm }
341044cd46caSbillm 
3411de6628f0Sck static boolean_t
34123ad6c7f9SVictor Latushkin pool_match(nvlist_t *cfg, char *tgt)
3413de6628f0Sck {
34143ad6c7f9SVictor Latushkin 	uint64_t v, guid = strtoull(tgt, NULL, 0);
3415de6628f0Sck 	char *s;
3416de6628f0Sck 
3417de6628f0Sck 	if (guid != 0) {
34183ad6c7f9SVictor Latushkin 		if (nvlist_lookup_uint64(cfg, ZPOOL_CONFIG_POOL_GUID, &v) == 0)
34193ad6c7f9SVictor Latushkin 			return (v == guid);
3420de6628f0Sck 	} else {
34213ad6c7f9SVictor Latushkin 		if (nvlist_lookup_string(cfg, ZPOOL_CONFIG_POOL_NAME, &s) == 0)
34223ad6c7f9SVictor Latushkin 			return (strcmp(s, tgt) == 0);
3423de6628f0Sck 	}
34243ad6c7f9SVictor Latushkin 	return (B_FALSE);
3425de6628f0Sck }
3426de6628f0Sck 
34273ad6c7f9SVictor Latushkin static char *
34283ad6c7f9SVictor Latushkin find_zpool(char **target, nvlist_t **configp, int dirc, char **dirv)
3429de6628f0Sck {
3430de6628f0Sck 	nvlist_t *pools;
3431de6628f0Sck 	nvlist_t *match = NULL;
34323ad6c7f9SVictor Latushkin 	char *name = NULL;
34333ad6c7f9SVictor Latushkin 	char *sepp = NULL;
34343ad6c7f9SVictor Latushkin 	char sep;
34353ad6c7f9SVictor Latushkin 	int count = 0;
3436d41c4376SMark J Musante 	importargs_t args = { 0 };
3437d41c4376SMark J Musante 
3438d41c4376SMark J Musante 	args.paths = dirc;
3439d41c4376SMark J Musante 	args.path = dirv;
3440d41c4376SMark J Musante 	args.can_be_active = B_TRUE;
3441de6628f0Sck 
34423ad6c7f9SVictor Latushkin 	if ((sepp = strpbrk(*target, "/@")) != NULL) {
34433ad6c7f9SVictor Latushkin 		sep = *sepp;
34443ad6c7f9SVictor Latushkin 		*sepp = '\0';
34453ad6c7f9SVictor Latushkin 	}
34463ad6c7f9SVictor Latushkin 
3447d41c4376SMark J Musante 	pools = zpool_search_import(g_zfs, &args);
3448de6628f0Sck 
3449de6628f0Sck 	if (pools != NULL) {
3450de6628f0Sck 		nvpair_t *elem = NULL;
3451de6628f0Sck 		while ((elem = nvlist_next_nvpair(pools, elem)) != NULL) {
3452de6628f0Sck 			verify(nvpair_value_nvlist(elem, configp) == 0);
34533ad6c7f9SVictor Latushkin 			if (pool_match(*configp, *target)) {
34543ad6c7f9SVictor Latushkin 				count++;
3455de6628f0Sck 				if (match != NULL) {
34563ad6c7f9SVictor Latushkin 					/* print previously found config */
34573ad6c7f9SVictor Latushkin 					if (name != NULL) {
34583ad6c7f9SVictor Latushkin 						(void) printf("%s\n", name);
34593ad6c7f9SVictor Latushkin 						dump_nvlist(match, 8);
34603ad6c7f9SVictor Latushkin 						name = NULL;
34613ad6c7f9SVictor Latushkin 					}
34623ad6c7f9SVictor Latushkin 					(void) printf("%s\n",
34633ad6c7f9SVictor Latushkin 					    nvpair_name(elem));
34643ad6c7f9SVictor Latushkin 					dump_nvlist(*configp, 8);
3465de6628f0Sck 				} else {
3466de6628f0Sck 					match = *configp;
34673ad6c7f9SVictor Latushkin 					name = nvpair_name(elem);
3468de6628f0Sck 				}
3469de6628f0Sck 			}
3470de6628f0Sck 		}
3471de6628f0Sck 	}
34723ad6c7f9SVictor Latushkin 	if (count > 1)
34733ad6c7f9SVictor Latushkin 		(void) fatal("\tMatched %d pools - use pool GUID "
34743ad6c7f9SVictor Latushkin 		    "instead of pool name or \n"
34753ad6c7f9SVictor Latushkin 		    "\tpool name part of a dataset name to select pool", count);
34763ad6c7f9SVictor Latushkin 
34773ad6c7f9SVictor Latushkin 	if (sepp)
34783ad6c7f9SVictor Latushkin 		*sepp = sep;
34793ad6c7f9SVictor Latushkin 	/*
34803ad6c7f9SVictor Latushkin 	 * If pool GUID was specified for pool id, replace it with pool name
34813ad6c7f9SVictor Latushkin 	 */
34823ad6c7f9SVictor Latushkin 	if (name && (strstr(*target, name) != *target)) {
34833ad6c7f9SVictor Latushkin 		int sz = 1 + strlen(name) + ((sepp) ? strlen(sepp) : 0);
34843ad6c7f9SVictor Latushkin 
34853ad6c7f9SVictor Latushkin 		*target = umem_alloc(sz, UMEM_NOFAIL);
34863ad6c7f9SVictor Latushkin 		(void) snprintf(*target, sz, "%s%s", name, sepp ? sepp : "");
34873ad6c7f9SVictor Latushkin 	}
3488de6628f0Sck 
34893ad6c7f9SVictor Latushkin 	*configp = name ? match : NULL;
3490de6628f0Sck 
34913ad6c7f9SVictor Latushkin 	return (name);
3492de6628f0Sck }
3493de6628f0Sck 
3494fa9e4066Sahrens int
3495fa9e4066Sahrens main(int argc, char **argv)
3496fa9e4066Sahrens {
3497fa9e4066Sahrens 	int i, c;
3498fa9e4066Sahrens 	struct rlimit rl = { 1024, 1024 };
34993ad6c7f9SVictor Latushkin 	spa_t *spa = NULL;
3500fa9e4066Sahrens 	objset_t *os = NULL;
3501fa9e4066Sahrens 	int dump_all = 1;
3502fa9e4066Sahrens 	int verbose = 0;
3503c8ee1847SVictor Latushkin 	int error = 0;
35043ad6c7f9SVictor Latushkin 	char **searchdirs = NULL;
35053ad6c7f9SVictor Latushkin 	int nsearch = 0;
35063ad6c7f9SVictor Latushkin 	char *target;
3507468c413aSTim Haley 	nvlist_t *policy = NULL;
3508468c413aSTim Haley 	uint64_t max_txg = UINT64_MAX;
3509c8ee1847SVictor Latushkin 	int rewind = ZPOOL_NEVER_REWIND;
3510fa9e4066Sahrens 
3511fa9e4066Sahrens 	(void) setrlimit(RLIMIT_NOFILE, &rl);
3512004388ebScasper 	(void) enable_extended_FILE_stdio(-1, -1);
3513fa9e4066Sahrens 
3514fa9e4066Sahrens 	dprintf_setup(&argc, argv);
3515fa9e4066Sahrens 
3516df15e419SMatthew Ahrens 	while ((c = getopt(argc, argv,
35172e4c9986SGeorge Wilson 	    "bcdhilmMI:suCDRSAFLXx:evp:t:U:P")) != -1) {
3518fa9e4066Sahrens 		switch (c) {
3519fa9e4066Sahrens 		case 'b':
3520fa9e4066Sahrens 		case 'c':
3521b24ab676SJeff Bonwick 		case 'd':
3522b24ab676SJeff Bonwick 		case 'h':
3523b24ab676SJeff Bonwick 		case 'i':
3524b24ab676SJeff Bonwick 		case 'l':
3525d6e555bdSGeorge Wilson 		case 'm':
3526fa9e4066Sahrens 		case 's':
3527b24ab676SJeff Bonwick 		case 'u':
3528fa9e4066Sahrens 		case 'C':
3529b24ab676SJeff Bonwick 		case 'D':
35302e4c9986SGeorge Wilson 		case 'M':
353144cd46caSbillm 		case 'R':
3532b24ab676SJeff Bonwick 		case 'S':
3533fa9e4066Sahrens 			dump_opt[c]++;
3534fa9e4066Sahrens 			dump_all = 0;
3535fa9e4066Sahrens 			break;
3536feef89cfSVictor Latushkin 		case 'A':
3537c8ee1847SVictor Latushkin 		case 'F':
353882a0a985SVictor Latushkin 		case 'L':
3539c8ee1847SVictor Latushkin 		case 'X':
35403ad6c7f9SVictor Latushkin 		case 'e':
35413f9d6ad7SLin Ling 		case 'P':
354282a0a985SVictor Latushkin 			dump_opt[c]++;
354382a0a985SVictor Latushkin 			break;
35442e4c9986SGeorge Wilson 		case 'I':
354531d7e8faSGeorge Wilson 			max_inflight = strtoull(optarg, NULL, 0);
354631d7e8faSGeorge Wilson 			if (max_inflight == 0) {
354731d7e8faSGeorge Wilson 				(void) fprintf(stderr, "maximum number "
354831d7e8faSGeorge Wilson 				    "of inflight I/Os must be greater "
354931d7e8faSGeorge Wilson 				    "than 0\n");
355031d7e8faSGeorge Wilson 				usage();
355131d7e8faSGeorge Wilson 			}
355231d7e8faSGeorge Wilson 			break;
3553de6628f0Sck 		case 'p':
35543ad6c7f9SVictor Latushkin 			if (searchdirs == NULL) {
35553ad6c7f9SVictor Latushkin 				searchdirs = umem_alloc(sizeof (char *),
35563ad6c7f9SVictor Latushkin 				    UMEM_NOFAIL);
35573ad6c7f9SVictor Latushkin 			} else {
35583ad6c7f9SVictor Latushkin 				char **tmp = umem_alloc((nsearch + 1) *
35593ad6c7f9SVictor Latushkin 				    sizeof (char *), UMEM_NOFAIL);
35603ad6c7f9SVictor Latushkin 				bcopy(searchdirs, tmp, nsearch *
35613ad6c7f9SVictor Latushkin 				    sizeof (char *));
35623ad6c7f9SVictor Latushkin 				umem_free(searchdirs,
35633ad6c7f9SVictor Latushkin 				    nsearch * sizeof (char *));
35643ad6c7f9SVictor Latushkin 				searchdirs = tmp;
35653ad6c7f9SVictor Latushkin 			}
35663ad6c7f9SVictor Latushkin 			searchdirs[nsearch++] = optarg;
3567de6628f0Sck 			break;
35682e551927SVictor Latushkin 		case 't':
3569468c413aSTim Haley 			max_txg = strtoull(optarg, NULL, 0);
3570468c413aSTim Haley 			if (max_txg < TXG_INITIAL) {
35712e551927SVictor Latushkin 				(void) fprintf(stderr, "incorrect txg "
35722e551927SVictor Latushkin 				    "specified: %s\n", optarg);
35732e551927SVictor Latushkin 				usage();
35742e551927SVictor Latushkin 			}
35752e551927SVictor Latushkin 			break;
3576b24ab676SJeff Bonwick 		case 'U':
3577b24ab676SJeff Bonwick 			spa_config_path = optarg;
3578b24ab676SJeff Bonwick 			break;
35792e4c9986SGeorge Wilson 		case 'v':
35802e4c9986SGeorge Wilson 			verbose++;
35812e4c9986SGeorge Wilson 			break;
35822e4c9986SGeorge Wilson 		case 'x':
35832e4c9986SGeorge Wilson 			vn_dumpdir = optarg;
35842e4c9986SGeorge Wilson 			break;
3585fa9e4066Sahrens 		default:
3586fa9e4066Sahrens 			usage();
3587fa9e4066Sahrens 			break;
3588fa9e4066Sahrens 		}
3589fa9e4066Sahrens 	}
3590fa9e4066Sahrens 
35913ad6c7f9SVictor Latushkin 	if (!dump_opt['e'] && searchdirs != NULL) {
359288b7b0f2SMatthew Ahrens 		(void) fprintf(stderr, "-p option requires use of -e\n");
359388b7b0f2SMatthew Ahrens 		usage();
359488b7b0f2SMatthew Ahrens 	}
3595de6628f0Sck 
359606be9802SMatthew Ahrens 	/*
359706be9802SMatthew Ahrens 	 * ZDB does not typically re-read blocks; therefore limit the ARC
359806be9802SMatthew Ahrens 	 * to 256 MB, which can be used entirely for metadata.
359906be9802SMatthew Ahrens 	 */
360006be9802SMatthew Ahrens 	zfs_arc_max = zfs_arc_meta_limit = 256 * 1024 * 1024;
360106be9802SMatthew Ahrens 
3602f7950bf1SMatthew Ahrens 	/*
3603f7950bf1SMatthew Ahrens 	 * "zdb -c" uses checksum-verifying scrub i/os which are async reads.
3604f7950bf1SMatthew Ahrens 	 * "zdb -b" uses traversal prefetch which uses async reads.
3605f7950bf1SMatthew Ahrens 	 * For good performance, let several of them be active at once.
3606f7950bf1SMatthew Ahrens 	 */
3607f7950bf1SMatthew Ahrens 	zfs_vdev_async_read_max_active = 10;
3608f7950bf1SMatthew Ahrens 
3609fa9e4066Sahrens 	kernel_init(FREAD);
3610de6628f0Sck 	g_zfs = libzfs_init();
361191ebeef5Sahrens 	ASSERT(g_zfs != NULL);
3612fa9e4066Sahrens 
3613b24ab676SJeff Bonwick 	if (dump_all)
3614b24ab676SJeff Bonwick 		verbose = MAX(verbose, 1);
3615b24ab676SJeff Bonwick 
3616fa9e4066Sahrens 	for (c = 0; c < 256; c++) {
36173f9d6ad7SLin Ling 		if (dump_all && !strchr("elAFLRSXP", c))
3618fa9e4066Sahrens 			dump_opt[c] = 1;
3619fa9e4066Sahrens 		if (dump_opt[c])
3620fa9e4066Sahrens 			dump_opt[c] += verbose;
3621fa9e4066Sahrens 	}
3622fa9e4066Sahrens 
3623feef89cfSVictor Latushkin 	aok = (dump_opt['A'] == 1) || (dump_opt['A'] > 2);
3624feef89cfSVictor Latushkin 	zfs_recover = (dump_opt['A'] > 1);
3625feef89cfSVictor Latushkin 
3626fa9e4066Sahrens 	argc -= optind;
3627fa9e4066Sahrens 	argv += optind;
3628fa9e4066Sahrens 
362907428bdfSVictor Latushkin 	if (argc < 2 && dump_opt['R'])
363007428bdfSVictor Latushkin 		usage();
3631fa9e4066Sahrens 	if (argc < 1) {
36323ad6c7f9SVictor Latushkin 		if (!dump_opt['e'] && dump_opt['C']) {
3633e829d913Sck 			dump_cachefile(spa_config_path);
3634fa9e4066Sahrens 			return (0);
3635fa9e4066Sahrens 		}
3636fa9e4066Sahrens 		usage();
3637fa9e4066Sahrens 	}
3638fa9e4066Sahrens 
3639fa9e4066Sahrens 	if (dump_opt['l']) {
3640fa9e4066Sahrens 		dump_label(argv[0]);
3641fa9e4066Sahrens 		return (0);
3642fa9e4066Sahrens 	}
3643fa9e4066Sahrens 
3644c8ee1847SVictor Latushkin 	if (dump_opt['X'] || dump_opt['F'])
3645c8ee1847SVictor Latushkin 		rewind = ZPOOL_DO_REWIND |
3646c8ee1847SVictor Latushkin 		    (dump_opt['X'] ? ZPOOL_EXTREME_REWIND : 0);
3647c8ee1847SVictor Latushkin 
3648c8ee1847SVictor Latushkin 	if (nvlist_alloc(&policy, NV_UNIQUE_NAME_TYPE, 0) != 0 ||
3649c8ee1847SVictor Latushkin 	    nvlist_add_uint64(policy, ZPOOL_REWIND_REQUEST_TXG, max_txg) != 0 ||
3650c8ee1847SVictor Latushkin 	    nvlist_add_uint32(policy, ZPOOL_REWIND_REQUEST, rewind) != 0)
3651c8ee1847SVictor Latushkin 		fatal("internal error: %s", strerror(ENOMEM));
3652c8ee1847SVictor Latushkin 
3653c5904d13Seschrock 	error = 0;
36543ad6c7f9SVictor Latushkin 	target = argv[0];
3655990b4856Slling 
36563ad6c7f9SVictor Latushkin 	if (dump_opt['e']) {
36573ad6c7f9SVictor Latushkin 		nvlist_t *cfg = NULL;
36583ad6c7f9SVictor Latushkin 		char *name = find_zpool(&target, &cfg, nsearch, searchdirs);
3659990b4856Slling 
36603ad6c7f9SVictor Latushkin 		error = ENOENT;
36613ad6c7f9SVictor Latushkin 		if (name) {
366207428bdfSVictor Latushkin 			if (dump_opt['C'] > 1) {
366307428bdfSVictor Latushkin 				(void) printf("\nConfiguration for import:\n");
366407428bdfSVictor Latushkin 				dump_nvlist(cfg, 8);
366507428bdfSVictor Latushkin 			}
3666c8ee1847SVictor Latushkin 			if (nvlist_add_nvlist(cfg,
3667468c413aSTim Haley 			    ZPOOL_REWIND_POLICY, policy) != 0) {
3668468c413aSTim Haley 				fatal("can't open '%s': %s",
3669468c413aSTim Haley 				    target, strerror(ENOMEM));
3670468c413aSTim Haley 			}
36714b964adaSGeorge Wilson 			if ((error = spa_import(name, cfg, NULL,
36724b964adaSGeorge Wilson 			    ZFS_IMPORT_MISSING_LOG)) != 0) {
36734b964adaSGeorge Wilson 				error = spa_import(name, cfg, NULL,
36744b964adaSGeorge Wilson 				    ZFS_IMPORT_VERBATIM);
36754b964adaSGeorge Wilson 			}
3676de6628f0Sck 		}
3677c5904d13Seschrock 	}
3678c5904d13Seschrock 
3679c5904d13Seschrock 	if (error == 0) {
368007428bdfSVictor Latushkin 		if (strpbrk(target, "/@") == NULL || dump_opt['R']) {
368180eb36f2SGeorge Wilson 			error = spa_open_rewind(target, &spa, FTAG, policy,
368280eb36f2SGeorge Wilson 			    NULL);
36838f18d1faSGeorge Wilson 			if (error) {
36848f18d1faSGeorge Wilson 				/*
36858f18d1faSGeorge Wilson 				 * If we're missing the log device then
36868f18d1faSGeorge Wilson 				 * try opening the pool after clearing the
36878f18d1faSGeorge Wilson 				 * log state.
36888f18d1faSGeorge Wilson 				 */
36898f18d1faSGeorge Wilson 				mutex_enter(&spa_namespace_lock);
36903ad6c7f9SVictor Latushkin 				if ((spa = spa_lookup(target)) != NULL &&
36918f18d1faSGeorge Wilson 				    spa->spa_log_state == SPA_LOG_MISSING) {
36928f18d1faSGeorge Wilson 					spa->spa_log_state = SPA_LOG_CLEAR;
36938f18d1faSGeorge Wilson 					error = 0;
36948f18d1faSGeorge Wilson 				}
36958f18d1faSGeorge Wilson 				mutex_exit(&spa_namespace_lock);
36968f18d1faSGeorge Wilson 
369780eb36f2SGeorge Wilson 				if (!error) {
369880eb36f2SGeorge Wilson 					error = spa_open_rewind(target, &spa,
369980eb36f2SGeorge Wilson 					    FTAG, policy, NULL);
370080eb36f2SGeorge Wilson 				}
37018f18d1faSGeorge Wilson 			}
370207428bdfSVictor Latushkin 		} else {
370307428bdfSVictor Latushkin 			error = dmu_objset_own(target, DMU_OST_ANY,
370407428bdfSVictor Latushkin 			    B_TRUE, FTAG, &os);
3705c5904d13Seschrock 		}
3706fa9e4066Sahrens 	}
370780eb36f2SGeorge Wilson 	nvlist_free(policy);
370880eb36f2SGeorge Wilson 
3709fa9e4066Sahrens 	if (error)
37103ad6c7f9SVictor Latushkin 		fatal("can't open '%s': %s", target, strerror(error));
3711fa9e4066Sahrens 
3712fa9e4066Sahrens 	argv++;
371307428bdfSVictor Latushkin 	argc--;
371407428bdfSVictor Latushkin 	if (!dump_opt['R']) {
371507428bdfSVictor Latushkin 		if (argc > 0) {
371607428bdfSVictor Latushkin 			zopt_objects = argc;
371707428bdfSVictor Latushkin 			zopt_object = calloc(zopt_objects, sizeof (uint64_t));
371807428bdfSVictor Latushkin 			for (i = 0; i < zopt_objects; i++) {
371907428bdfSVictor Latushkin 				errno = 0;
372007428bdfSVictor Latushkin 				zopt_object[i] = strtoull(argv[i], NULL, 0);
372107428bdfSVictor Latushkin 				if (zopt_object[i] == 0 && errno != 0)
372287219db7SVictor Latushkin 					fatal("bad number %s: %s",
372307428bdfSVictor Latushkin 					    argv[i], strerror(errno));
372407428bdfSVictor Latushkin 			}
3725fa9e4066Sahrens 		}
3726e690fb27SChristopher Siden 		if (os != NULL) {
3727e690fb27SChristopher Siden 			dump_dir(os);
3728e690fb27SChristopher Siden 		} else if (zopt_objects > 0 && !dump_opt['m']) {
3729e690fb27SChristopher Siden 			dump_dir(spa->spa_meta_objset);
3730e690fb27SChristopher Siden 		} else {
3731e690fb27SChristopher Siden 			dump_zpool(spa);
3732e690fb27SChristopher Siden 		}
3733fa9e4066Sahrens 	} else {
373407428bdfSVictor Latushkin 		flagbits['b'] = ZDB_FLAG_PRINT_BLKPTR;
373507428bdfSVictor Latushkin 		flagbits['c'] = ZDB_FLAG_CHECKSUM;
373607428bdfSVictor Latushkin 		flagbits['d'] = ZDB_FLAG_DECOMPRESS;
373707428bdfSVictor Latushkin 		flagbits['e'] = ZDB_FLAG_BSWAP;
373807428bdfSVictor Latushkin 		flagbits['g'] = ZDB_FLAG_GBH;
373907428bdfSVictor Latushkin 		flagbits['i'] = ZDB_FLAG_INDIRECT;
374007428bdfSVictor Latushkin 		flagbits['p'] = ZDB_FLAG_PHYS;
374107428bdfSVictor Latushkin 		flagbits['r'] = ZDB_FLAG_RAW;
374207428bdfSVictor Latushkin 
374307428bdfSVictor Latushkin 		for (i = 0; i < argc; i++)
374407428bdfSVictor Latushkin 			zdb_read_block(argv[i], spa);
3745fa9e4066Sahrens 	}
3746fa9e4066Sahrens 
374707428bdfSVictor Latushkin 	(os != NULL) ? dmu_objset_disown(os, FTAG) : spa_close(spa, FTAG);
374807428bdfSVictor Latushkin 
3749e0d35c44Smarks 	fuid_table_destroy();
37500a586ceaSMark Shellenbaum 	sa_loaded = B_FALSE;
3751e0d35c44Smarks 
3752de6628f0Sck 	libzfs_fini(g_zfs);
3753fa9e4066Sahrens 	kernel_fini();
3754fa9e4066Sahrens 
3755fa9e4066Sahrens 	return (0);
3756fa9e4066Sahrens }
3757