xref: /illumos-gate/usr/src/cmd/zdb/zdb.c (revision c3d26abc)
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.
24ca0cc391SMatthew Ahrens  * Copyright (c) 2011, 2015 by Delphix. All rights reserved.
25*c3d26abcSMatthew Ahrens  * Copyright (c) 2014 Integros [integros.com]
26fa9e4066Sahrens  */
27fa9e4066Sahrens 
28fa9e4066Sahrens #include <stdio.h>
29490d05b9SMatthew Ahrens #include <unistd.h>
30004388ebScasper #include <stdio_ext.h>
31fa9e4066Sahrens #include <stdlib.h>
3244cd46caSbillm #include <ctype.h>
33fa9e4066Sahrens #include <sys/zfs_context.h>
34fa9e4066Sahrens #include <sys/spa.h>
35fa9e4066Sahrens #include <sys/spa_impl.h>
36fa9e4066Sahrens #include <sys/dmu.h>
37fa9e4066Sahrens #include <sys/zap.h>
38fa9e4066Sahrens #include <sys/fs/zfs.h>
39fa9e4066Sahrens #include <sys/zfs_znode.h>
400a586ceaSMark Shellenbaum #include <sys/zfs_sa.h>
410a586ceaSMark Shellenbaum #include <sys/sa.h>
420a586ceaSMark Shellenbaum #include <sys/sa_impl.h>
43fa9e4066Sahrens #include <sys/vdev.h>
44fa9e4066Sahrens #include <sys/vdev_impl.h>
45fa9e4066Sahrens #include <sys/metaslab_impl.h>
46fa9e4066Sahrens #include <sys/dmu_objset.h>
47fa9e4066Sahrens #include <sys/dsl_dir.h>
48fa9e4066Sahrens #include <sys/dsl_dataset.h>
49fa9e4066Sahrens #include <sys/dsl_pool.h>
50fa9e4066Sahrens #include <sys/dbuf.h>
51fa9e4066Sahrens #include <sys/zil.h>
52fa9e4066Sahrens #include <sys/zil_impl.h>
53fa9e4066Sahrens #include <sys/stat.h>
54fa9e4066Sahrens #include <sys/resource.h>
55fa9e4066Sahrens #include <sys/dmu_traverse.h>
56fa9e4066Sahrens #include <sys/zio_checksum.h>
57fa9e4066Sahrens #include <sys/zio_compress.h>
58e0d35c44Smarks #include <sys/zfs_fuid.h>
5988b7b0f2SMatthew Ahrens #include <sys/arc.h>
60b24ab676SJeff Bonwick #include <sys/ddt.h>
61ad135b5dSChristopher Siden #include <sys/zfeature.h>
624445fffbSMatthew Ahrens #include <zfs_comutil.h>
63de6628f0Sck #undef ZFS_MAXNAMELEN
64de6628f0Sck #undef verify
65de6628f0Sck #include <libzfs.h>
66fa9e4066Sahrens 
67e690fb27SChristopher Siden #define	ZDB_COMPRESS_NAME(idx) ((idx) < ZIO_COMPRESS_FUNCTIONS ?	\
68e690fb27SChristopher Siden 	zio_compress_table[(idx)].ci_name : "UNKNOWN")
69e690fb27SChristopher Siden #define	ZDB_CHECKSUM_NAME(idx) ((idx) < ZIO_CHECKSUM_FUNCTIONS ?	\
70e690fb27SChristopher Siden 	zio_checksum_table[(idx)].ci_name : "UNKNOWN")
71e690fb27SChristopher Siden #define	ZDB_OT_NAME(idx) ((idx) < DMU_OT_NUMTYPES ?	\
72e690fb27SChristopher Siden 	dmu_ot[(idx)].ot_name : DMU_OT_IS_VALID(idx) ?	\
73e690fb27SChristopher Siden 	dmu_ot_byteswap[DMU_OT_BYTESWAP(idx)].ob_name : "UNKNOWN")
74e690fb27SChristopher Siden #define	ZDB_OT_TYPE(idx) ((idx) < DMU_OT_NUMTYPES ? (idx) :		\
75e690fb27SChristopher Siden 	(((idx) == DMU_OTN_ZAP_DATA || (idx) == DMU_OTN_ZAP_METADATA) ?	\
76e690fb27SChristopher Siden 	DMU_OT_ZAP_OTHER : DMU_OT_NUMTYPES))
776de8f417SVictor Latushkin 
78feef89cfSVictor Latushkin #ifndef lint
797fd05ac4SMatthew Ahrens extern boolean_t zfs_recover;
8006be9802SMatthew Ahrens extern uint64_t zfs_arc_max, zfs_arc_meta_limit;
81f7950bf1SMatthew Ahrens extern int zfs_vdev_async_read_max_active;
82feef89cfSVictor Latushkin #else
837fd05ac4SMatthew Ahrens boolean_t zfs_recover;
8406be9802SMatthew Ahrens uint64_t zfs_arc_max, zfs_arc_meta_limit;
85f7950bf1SMatthew Ahrens int zfs_vdev_async_read_max_active;
86feef89cfSVictor Latushkin #endif
87feef89cfSVictor Latushkin 
88fa9e4066Sahrens const char cmdname[] = "zdb";
89fa9e4066Sahrens uint8_t dump_opt[256];
90fa9e4066Sahrens 
91fa9e4066Sahrens typedef void object_viewer_t(objset_t *, uint64_t, void *data, size_t size);
92fa9e4066Sahrens 
93fa9e4066Sahrens extern void dump_intent_log(zilog_t *);
94fa9e4066Sahrens uint64_t *zopt_object = NULL;
95fa9e4066Sahrens int zopt_objects = 0;
96de6628f0Sck libzfs_handle_t *g_zfs;
9706be9802SMatthew Ahrens uint64_t max_inflight = 1000;
98fa9e4066Sahrens 
99732885fcSMatthew Ahrens static void snprintf_blkptr_compact(char *, size_t, const blkptr_t *);
100732885fcSMatthew Ahrens 
101fa9e4066Sahrens /*
102fa9e4066Sahrens  * These libumem hooks provide a reasonable set of defaults for the allocator's
103fa9e4066Sahrens  * debugging facilities.
104fa9e4066Sahrens  */
105fa9e4066Sahrens const char *
106fa9e4066Sahrens _umem_debug_init()
107fa9e4066Sahrens {
108fa9e4066Sahrens 	return ("default,verbose"); /* $UMEM_DEBUG setting */
109fa9e4066Sahrens }
110fa9e4066Sahrens 
111fa9e4066Sahrens const char *
112fa9e4066Sahrens _umem_logging_init(void)
113fa9e4066Sahrens {
114fa9e4066Sahrens 	return ("fail,contents"); /* $UMEM_LOGGING setting */
115fa9e4066Sahrens }
116fa9e4066Sahrens 
117fa9e4066Sahrens static void
118fa9e4066Sahrens usage(void)
119fa9e4066Sahrens {
120fa9e4066Sahrens 	(void) fprintf(stderr,
1212e4c9986SGeorge Wilson 	    "Usage: %s [-CumMdibcsDvhLXFPA] [-t txg] [-e [-p path...]] "
1222e4c9986SGeorge Wilson 	    "[-U config] [-I inflight I/Os] [-x dumpdir] poolname [object...]\n"
12331d7e8faSGeorge Wilson 	    "       %s [-divPA] [-e -p path...] [-U config] dataset "
12431d7e8faSGeorge Wilson 	    "[object...]\n"
1252e4c9986SGeorge Wilson 	    "       %s -mM [-LXFPA] [-t txg] [-e [-p path...]] [-U config] "
12690e894e2SRichard Lowe 	    "poolname [vdev [metaslab...]]\n"
12790e894e2SRichard Lowe 	    "       %s -R [-A] [-e [-p path...]] poolname "
12890e894e2SRichard Lowe 	    "vdev:offset:size[:flags]\n"
12931d7e8faSGeorge Wilson 	    "       %s -S [-PA] [-e [-p path...]] [-U config] poolname\n"
13090e894e2SRichard Lowe 	    "       %s -l [-uA] device\n"
13190e894e2SRichard Lowe 	    "       %s -C [-A] [-U config]\n\n",
132bbfd46c4SJeff Bonwick 	    cmdname, cmdname, cmdname, cmdname, cmdname, cmdname, cmdname);
1333ad6c7f9SVictor Latushkin 
1343ad6c7f9SVictor Latushkin 	(void) fprintf(stderr, "    Dataset name must include at least one "
1353ad6c7f9SVictor Latushkin 	    "separator character '/' or '@'\n");
1363ad6c7f9SVictor Latushkin 	(void) fprintf(stderr, "    If dataset name is specified, only that "
1373ad6c7f9SVictor Latushkin 	    "dataset is dumped\n");
1383ad6c7f9SVictor Latushkin 	(void) fprintf(stderr, "    If object numbers are specified, only "
1393ad6c7f9SVictor Latushkin 	    "those objects are dumped\n\n");
1403ad6c7f9SVictor Latushkin 	(void) fprintf(stderr, "    Options to control amount of output:\n");
1413ad6c7f9SVictor Latushkin 	(void) fprintf(stderr, "        -u uberblock\n");
1423ad6c7f9SVictor Latushkin 	(void) fprintf(stderr, "        -d dataset(s)\n");
1433ad6c7f9SVictor Latushkin 	(void) fprintf(stderr, "        -i intent logs\n");
14407428bdfSVictor Latushkin 	(void) fprintf(stderr, "        -C config (or cachefile if alone)\n");
1458f18d1faSGeorge Wilson 	(void) fprintf(stderr, "        -h pool history\n");
1463ad6c7f9SVictor Latushkin 	(void) fprintf(stderr, "        -b block statistics\n");
1473ad6c7f9SVictor Latushkin 	(void) fprintf(stderr, "        -m metaslabs\n");
1482e4c9986SGeorge Wilson 	(void) fprintf(stderr, "        -M metaslab groups\n");
1493ad6c7f9SVictor Latushkin 	(void) fprintf(stderr, "        -c checksum all metadata (twice for "
1506365109dSVictor Latushkin 	    "all data) blocks\n");
1513ad6c7f9SVictor Latushkin 	(void) fprintf(stderr, "        -s report stats on zdb's I/O\n");
152f0ba89beSJeff Bonwick 	(void) fprintf(stderr, "        -D dedup statistics\n");
153b24ab676SJeff Bonwick 	(void) fprintf(stderr, "        -S simulate dedup to measure effect\n");
1543ad6c7f9SVictor Latushkin 	(void) fprintf(stderr, "        -v verbose (applies to all others)\n");
155fa9e4066Sahrens 	(void) fprintf(stderr, "        -l dump label contents\n");
15682a0a985SVictor Latushkin 	(void) fprintf(stderr, "        -L disable leak tracking (do not "
15782a0a985SVictor Latushkin 	    "load spacemaps)\n");
158d41e7643Sek 	(void) fprintf(stderr, "        -R read and display block from a "
1593ad6c7f9SVictor Latushkin 	    "device\n\n");
1603ad6c7f9SVictor Latushkin 	(void) fprintf(stderr, "    Below options are intended for use "
161df15e419SMatthew Ahrens 	    "with other options:\n");
162feef89cfSVictor Latushkin 	(void) fprintf(stderr, "        -A ignore assertions (-A), enable "
163feef89cfSVictor Latushkin 	    "panic recovery (-AA) or both (-AAA)\n");
164c8ee1847SVictor Latushkin 	(void) fprintf(stderr, "        -F attempt automatic rewind within "
165c8ee1847SVictor Latushkin 	    "safe range of transaction groups\n");
1663ad6c7f9SVictor Latushkin 	(void) fprintf(stderr, "        -U <cachefile_path> -- use alternate "
1673ad6c7f9SVictor Latushkin 	    "cachefile\n");
168c8ee1847SVictor Latushkin 	(void) fprintf(stderr, "        -X attempt extreme rewind (does not "
169c8ee1847SVictor Latushkin 	    "work with dataset)\n");
1703ad6c7f9SVictor Latushkin 	(void) fprintf(stderr, "        -e pool is exported/destroyed/"
1713ad6c7f9SVictor Latushkin 	    "has altroot/not in a cachefile\n");
1723ad6c7f9SVictor Latushkin 	(void) fprintf(stderr, "        -p <path> -- use one or more with "
1733ad6c7f9SVictor Latushkin 	    "-e to specify path to vdev dir\n");
174df15e419SMatthew Ahrens 	(void) fprintf(stderr, "        -x <dumpdir> -- "
175df15e419SMatthew Ahrens 	    "dump all read blocks into specified directory\n");
17690e894e2SRichard Lowe 	(void) fprintf(stderr, "        -P print numbers in parseable form\n");
1773ad6c7f9SVictor Latushkin 	(void) fprintf(stderr, "        -t <txg> -- highest txg to use when "
1782e551927SVictor Latushkin 	    "searching for uberblocks\n");
1792e4c9986SGeorge Wilson 	(void) fprintf(stderr, "        -I <number of inflight I/Os> -- "
180df15e419SMatthew Ahrens 	    "specify the maximum number of "
181df15e419SMatthew Ahrens 	    "checksumming I/Os [default is 200]\n");
182fa9e4066Sahrens 	(void) fprintf(stderr, "Specify an option more than once (e.g. -bb) "
183fa9e4066Sahrens 	    "to make only that option verbose\n");
184fa9e4066Sahrens 	(void) fprintf(stderr, "Default is to dump everything non-verbosely\n");
185fa9e4066Sahrens 	exit(1);
186fa9e4066Sahrens }
187fa9e4066Sahrens 
188ccba0801SRich Morris /*
189ccba0801SRich Morris  * Called for usage errors that are discovered after a call to spa_open(),
190ccba0801SRich Morris  * dmu_bonus_hold(), or pool_match().  abort() is called for other errors.
191ccba0801SRich Morris  */
192ccba0801SRich Morris 
193fa9e4066Sahrens static void
194fa9e4066Sahrens fatal(const char *fmt, ...)
195fa9e4066Sahrens {
196fa9e4066Sahrens 	va_list ap;
197fa9e4066Sahrens 
198fa9e4066Sahrens 	va_start(ap, fmt);
199fa9e4066Sahrens 	(void) fprintf(stderr, "%s: ", cmdname);
200fa9e4066Sahrens 	(void) vfprintf(stderr, fmt, ap);
201fa9e4066Sahrens 	va_end(ap);
202fa9e4066Sahrens 	(void) fprintf(stderr, "\n");
203fa9e4066Sahrens 
204ccba0801SRich Morris 	exit(1);
205fa9e4066Sahrens }
206fa9e4066Sahrens 
207fa9e4066Sahrens /* ARGSUSED */
208fa9e4066Sahrens static void
209fa9e4066Sahrens dump_packed_nvlist(objset_t *os, uint64_t object, void *data, size_t size)
210fa9e4066Sahrens {
211fa9e4066Sahrens 	nvlist_t *nv;
212fa9e4066Sahrens 	size_t nvsize = *(uint64_t *)data;
213fa9e4066Sahrens 	char *packed = umem_alloc(nvsize, UMEM_NOFAIL);
214fa9e4066Sahrens 
2157bfdf011SNeil Perrin 	VERIFY(0 == dmu_read(os, object, 0, nvsize, packed, DMU_READ_PREFETCH));
216fa9e4066Sahrens 
217fa9e4066Sahrens 	VERIFY(nvlist_unpack(packed, nvsize, &nv, 0) == 0);
218fa9e4066Sahrens 
219fa9e4066Sahrens 	umem_free(packed, nvsize);
220fa9e4066Sahrens 
221fa9e4066Sahrens 	dump_nvlist(nv, 8);
222fa9e4066Sahrens 
223fa9e4066Sahrens 	nvlist_free(nv);
224fa9e4066Sahrens }
225fa9e4066Sahrens 
2264445fffbSMatthew Ahrens /* ARGSUSED */
2274445fffbSMatthew Ahrens static void
2284445fffbSMatthew Ahrens dump_history_offsets(objset_t *os, uint64_t object, void *data, size_t size)
2294445fffbSMatthew Ahrens {
2304445fffbSMatthew Ahrens 	spa_history_phys_t *shp = data;
2314445fffbSMatthew Ahrens 
2324445fffbSMatthew Ahrens 	if (shp == NULL)
2334445fffbSMatthew Ahrens 		return;
2344445fffbSMatthew Ahrens 
2354445fffbSMatthew Ahrens 	(void) printf("\t\tpool_create_len = %llu\n",
2364445fffbSMatthew Ahrens 	    (u_longlong_t)shp->sh_pool_create_len);
2374445fffbSMatthew Ahrens 	(void) printf("\t\tphys_max_off = %llu\n",
2384445fffbSMatthew Ahrens 	    (u_longlong_t)shp->sh_phys_max_off);
2394445fffbSMatthew Ahrens 	(void) printf("\t\tbof = %llu\n",
2404445fffbSMatthew Ahrens 	    (u_longlong_t)shp->sh_bof);
2414445fffbSMatthew Ahrens 	(void) printf("\t\teof = %llu\n",
2424445fffbSMatthew Ahrens 	    (u_longlong_t)shp->sh_eof);
2434445fffbSMatthew Ahrens 	(void) printf("\t\trecords_lost = %llu\n",
2444445fffbSMatthew Ahrens 	    (u_longlong_t)shp->sh_records_lost);
2454445fffbSMatthew Ahrens }
2464445fffbSMatthew Ahrens 
2473f9d6ad7SLin Ling static void
2483f9d6ad7SLin Ling zdb_nicenum(uint64_t num, char *buf)
2493f9d6ad7SLin Ling {
2503f9d6ad7SLin Ling 	if (dump_opt['P'])
2513f9d6ad7SLin Ling 		(void) sprintf(buf, "%llu", (longlong_t)num);
2523f9d6ad7SLin Ling 	else
2533f9d6ad7SLin Ling 		nicenum(num, buf);
2543f9d6ad7SLin Ling }
2553f9d6ad7SLin Ling 
256490d05b9SMatthew Ahrens const char histo_stars[] = "****************************************";
257490d05b9SMatthew Ahrens const int histo_width = sizeof (histo_stars) - 1;
258fa9e4066Sahrens 
259fa9e4066Sahrens static void
2600713e232SGeorge Wilson dump_histogram(const uint64_t *histo, int size, int offset)
261fa9e4066Sahrens {
262fa9e4066Sahrens 	int i;
263490d05b9SMatthew Ahrens 	int minidx = size - 1;
264fa9e4066Sahrens 	int maxidx = 0;
265fa9e4066Sahrens 	uint64_t max = 0;
266fa9e4066Sahrens 
267490d05b9SMatthew Ahrens 	for (i = 0; i < size; i++) {
268fa9e4066Sahrens 		if (histo[i] > max)
269fa9e4066Sahrens 			max = histo[i];
270fa9e4066Sahrens 		if (histo[i] > 0 && i > maxidx)
271fa9e4066Sahrens 			maxidx = i;
272fa9e4066Sahrens 		if (histo[i] > 0 && i < minidx)
273fa9e4066Sahrens 			minidx = i;
274fa9e4066Sahrens 	}
275fa9e4066Sahrens 
276490d05b9SMatthew Ahrens 	if (max < histo_width)
277490d05b9SMatthew Ahrens 		max = histo_width;
278fa9e4066Sahrens 
279490d05b9SMatthew Ahrens 	for (i = minidx; i <= maxidx; i++) {
280490d05b9SMatthew Ahrens 		(void) printf("\t\t\t%3u: %6llu %s\n",
2810713e232SGeorge Wilson 		    i + offset, (u_longlong_t)histo[i],
282490d05b9SMatthew Ahrens 		    &histo_stars[(max - histo[i]) * histo_width / max]);
283490d05b9SMatthew Ahrens 	}
284fa9e4066Sahrens }
285fa9e4066Sahrens 
286fa9e4066Sahrens static void
287fa9e4066Sahrens dump_zap_stats(objset_t *os, uint64_t object)
288fa9e4066Sahrens {
289fa9e4066Sahrens 	int error;
290fa9e4066Sahrens 	zap_stats_t zs;
291fa9e4066Sahrens 
292fa9e4066Sahrens 	error = zap_get_stats(os, object, &zs);
293fa9e4066Sahrens 	if (error)
294fa9e4066Sahrens 		return;
295fa9e4066Sahrens 
296fa9e4066Sahrens 	if (zs.zs_ptrtbl_len == 0) {
297fa9e4066Sahrens 		ASSERT(zs.zs_num_blocks == 1);
298fa9e4066Sahrens 		(void) printf("\tmicrozap: %llu bytes, %llu entries\n",
299fa9e4066Sahrens 		    (u_longlong_t)zs.zs_blocksize,
300fa9e4066Sahrens 		    (u_longlong_t)zs.zs_num_entries);
301fa9e4066Sahrens 		return;
302fa9e4066Sahrens 	}
303fa9e4066Sahrens 
304fa9e4066Sahrens 	(void) printf("\tFat ZAP stats:\n");
3058248818dSnd 
3068248818dSnd 	(void) printf("\t\tPointer table:\n");
3078248818dSnd 	(void) printf("\t\t\t%llu elements\n",
308fa9e4066Sahrens 	    (u_longlong_t)zs.zs_ptrtbl_len);
3098248818dSnd 	(void) printf("\t\t\tzt_blk: %llu\n",
3108248818dSnd 	    (u_longlong_t)zs.zs_ptrtbl_zt_blk);
3118248818dSnd 	(void) printf("\t\t\tzt_numblks: %llu\n",
3128248818dSnd 	    (u_longlong_t)zs.zs_ptrtbl_zt_numblks);
3138248818dSnd 	(void) printf("\t\t\tzt_shift: %llu\n",
3148248818dSnd 	    (u_longlong_t)zs.zs_ptrtbl_zt_shift);
3158248818dSnd 	(void) printf("\t\t\tzt_blks_copied: %llu\n",
3168248818dSnd 	    (u_longlong_t)zs.zs_ptrtbl_blks_copied);
3178248818dSnd 	(void) printf("\t\t\tzt_nextblk: %llu\n",
3188248818dSnd 	    (u_longlong_t)zs.zs_ptrtbl_nextblk);
3198248818dSnd 
320fa9e4066Sahrens 	(void) printf("\t\tZAP entries: %llu\n",
321fa9e4066Sahrens 	    (u_longlong_t)zs.zs_num_entries);
322fa9e4066Sahrens 	(void) printf("\t\tLeaf blocks: %llu\n",
323fa9e4066Sahrens 	    (u_longlong_t)zs.zs_num_leafs);
324fa9e4066Sahrens 	(void) printf("\t\tTotal blocks: %llu\n",
325fa9e4066Sahrens 	    (u_longlong_t)zs.zs_num_blocks);
3268248818dSnd 	(void) printf("\t\tzap_block_type: 0x%llx\n",
3278248818dSnd 	    (u_longlong_t)zs.zs_block_type);
3288248818dSnd 	(void) printf("\t\tzap_magic: 0x%llx\n",
3298248818dSnd 	    (u_longlong_t)zs.zs_magic);
3308248818dSnd 	(void) printf("\t\tzap_salt: 0x%llx\n",
3318248818dSnd 	    (u_longlong_t)zs.zs_salt);
332fa9e4066Sahrens 
333fa9e4066Sahrens 	(void) printf("\t\tLeafs with 2^n pointers:\n");
3340713e232SGeorge Wilson 	dump_histogram(zs.zs_leafs_with_2n_pointers, ZAP_HISTOGRAM_SIZE, 0);
335fa9e4066Sahrens 
336fa9e4066Sahrens 	(void) printf("\t\tBlocks with n*5 entries:\n");
3370713e232SGeorge Wilson 	dump_histogram(zs.zs_blocks_with_n5_entries, ZAP_HISTOGRAM_SIZE, 0);
338fa9e4066Sahrens 
339fa9e4066Sahrens 	(void) printf("\t\tBlocks n/10 full:\n");
3400713e232SGeorge Wilson 	dump_histogram(zs.zs_blocks_n_tenths_full, ZAP_HISTOGRAM_SIZE, 0);
341fa9e4066Sahrens 
342fa9e4066Sahrens 	(void) printf("\t\tEntries with n chunks:\n");
3430713e232SGeorge Wilson 	dump_histogram(zs.zs_entries_using_n_chunks, ZAP_HISTOGRAM_SIZE, 0);
344fa9e4066Sahrens 
345fa9e4066Sahrens 	(void) printf("\t\tBuckets with n entries:\n");
3460713e232SGeorge Wilson 	dump_histogram(zs.zs_buckets_with_n_entries, ZAP_HISTOGRAM_SIZE, 0);
347fa9e4066Sahrens }
348fa9e4066Sahrens 
349fa9e4066Sahrens /*ARGSUSED*/
350fa9e4066Sahrens static void
351fa9e4066Sahrens dump_none(objset_t *os, uint64_t object, void *data, size_t size)
352fa9e4066Sahrens {
353fa9e4066Sahrens }
354fa9e4066Sahrens 
3556de8f417SVictor Latushkin /*ARGSUSED*/
3566de8f417SVictor Latushkin static void
3576de8f417SVictor Latushkin dump_unknown(objset_t *os, uint64_t object, void *data, size_t size)
3586de8f417SVictor Latushkin {
3596de8f417SVictor Latushkin 	(void) printf("\tUNKNOWN OBJECT TYPE\n");
3606de8f417SVictor Latushkin }
3616de8f417SVictor Latushkin 
362fa9e4066Sahrens /*ARGSUSED*/
363fa9e4066Sahrens void
364fa9e4066Sahrens dump_uint8(objset_t *os, uint64_t object, void *data, size_t size)
365fa9e4066Sahrens {
366fa9e4066Sahrens }
367fa9e4066Sahrens 
368fa9e4066Sahrens /*ARGSUSED*/
369fa9e4066Sahrens static void
370fa9e4066Sahrens dump_uint64(objset_t *os, uint64_t object, void *data, size_t size)
371fa9e4066Sahrens {
372fa9e4066Sahrens }
373fa9e4066Sahrens 
374fa9e4066Sahrens /*ARGSUSED*/
375fa9e4066Sahrens static void
376fa9e4066Sahrens dump_zap(objset_t *os, uint64_t object, void *data, size_t size)
377fa9e4066Sahrens {
378fa9e4066Sahrens 	zap_cursor_t zc;
379fa9e4066Sahrens 	zap_attribute_t attr;
380fa9e4066Sahrens 	void *prop;
381fa9e4066Sahrens 	int i;
382fa9e4066Sahrens 
383fa9e4066Sahrens 	dump_zap_stats(os, object);
384fa9e4066Sahrens 	(void) printf("\n");
385fa9e4066Sahrens 
386fa9e4066Sahrens 	for (zap_cursor_init(&zc, os, object);
387fa9e4066Sahrens 	    zap_cursor_retrieve(&zc, &attr) == 0;
388fa9e4066Sahrens 	    zap_cursor_advance(&zc)) {
389fa9e4066Sahrens 		(void) printf("\t\t%s = ", attr.za_name);
390fa9e4066Sahrens 		if (attr.za_num_integers == 0) {
391fa9e4066Sahrens 			(void) printf("\n");
392fa9e4066Sahrens 			continue;
393fa9e4066Sahrens 		}
394fa9e4066Sahrens 		prop = umem_zalloc(attr.za_num_integers *
395fa9e4066Sahrens 		    attr.za_integer_length, UMEM_NOFAIL);
396fa9e4066Sahrens 		(void) zap_lookup(os, object, attr.za_name,
397fa9e4066Sahrens 		    attr.za_integer_length, attr.za_num_integers, prop);
398fa9e4066Sahrens 		if (attr.za_integer_length == 1) {
399fa9e4066Sahrens 			(void) printf("%s", (char *)prop);
400fa9e4066Sahrens 		} else {
401fa9e4066Sahrens 			for (i = 0; i < attr.za_num_integers; i++) {
402fa9e4066Sahrens 				switch (attr.za_integer_length) {
403fa9e4066Sahrens 				case 2:
404fa9e4066Sahrens 					(void) printf("%u ",
405fa9e4066Sahrens 					    ((uint16_t *)prop)[i]);
406fa9e4066Sahrens 					break;
407fa9e4066Sahrens 				case 4:
408fa9e4066Sahrens 					(void) printf("%u ",
409fa9e4066Sahrens 					    ((uint32_t *)prop)[i]);
410fa9e4066Sahrens 					break;
411fa9e4066Sahrens 				case 8:
412fa9e4066Sahrens 					(void) printf("%lld ",
413fa9e4066Sahrens 					    (u_longlong_t)((int64_t *)prop)[i]);
414fa9e4066Sahrens 					break;
415fa9e4066Sahrens 				}
416fa9e4066Sahrens 			}
417fa9e4066Sahrens 		}
418fa9e4066Sahrens 		(void) printf("\n");
419fa9e4066Sahrens 		umem_free(prop, attr.za_num_integers * attr.za_integer_length);
420fa9e4066Sahrens 	}
42187e5029aSahrens 	zap_cursor_fini(&zc);
422fa9e4066Sahrens }
423fa9e4066Sahrens 
424732885fcSMatthew Ahrens static void
425732885fcSMatthew Ahrens dump_bpobj(objset_t *os, uint64_t object, void *data, size_t size)
426732885fcSMatthew Ahrens {
427732885fcSMatthew Ahrens 	bpobj_phys_t *bpop = data;
428732885fcSMatthew Ahrens 	char bytes[32], comp[32], uncomp[32];
429732885fcSMatthew Ahrens 
430732885fcSMatthew Ahrens 	if (bpop == NULL)
431732885fcSMatthew Ahrens 		return;
432732885fcSMatthew Ahrens 
433732885fcSMatthew Ahrens 	zdb_nicenum(bpop->bpo_bytes, bytes);
434732885fcSMatthew Ahrens 	zdb_nicenum(bpop->bpo_comp, comp);
435732885fcSMatthew Ahrens 	zdb_nicenum(bpop->bpo_uncomp, uncomp);
436732885fcSMatthew Ahrens 
437732885fcSMatthew Ahrens 	(void) printf("\t\tnum_blkptrs = %llu\n",
438732885fcSMatthew Ahrens 	    (u_longlong_t)bpop->bpo_num_blkptrs);
439732885fcSMatthew Ahrens 	(void) printf("\t\tbytes = %s\n", bytes);
440732885fcSMatthew Ahrens 	if (size >= BPOBJ_SIZE_V1) {
441732885fcSMatthew Ahrens 		(void) printf("\t\tcomp = %s\n", comp);
442732885fcSMatthew Ahrens 		(void) printf("\t\tuncomp = %s\n", uncomp);
443732885fcSMatthew Ahrens 	}
444732885fcSMatthew Ahrens 	if (size >= sizeof (*bpop)) {
445732885fcSMatthew Ahrens 		(void) printf("\t\tsubobjs = %llu\n",
446732885fcSMatthew Ahrens 		    (u_longlong_t)bpop->bpo_subobjs);
447732885fcSMatthew Ahrens 		(void) printf("\t\tnum_subobjs = %llu\n",
448732885fcSMatthew Ahrens 		    (u_longlong_t)bpop->bpo_num_subobjs);
449732885fcSMatthew Ahrens 	}
450732885fcSMatthew Ahrens 
451732885fcSMatthew Ahrens 	if (dump_opt['d'] < 5)
452732885fcSMatthew Ahrens 		return;
453732885fcSMatthew Ahrens 
454732885fcSMatthew Ahrens 	for (uint64_t i = 0; i < bpop->bpo_num_blkptrs; i++) {
455732885fcSMatthew Ahrens 		char blkbuf[BP_SPRINTF_LEN];
456732885fcSMatthew Ahrens 		blkptr_t bp;
457732885fcSMatthew Ahrens 
458732885fcSMatthew Ahrens 		int err = dmu_read(os, object,
459732885fcSMatthew Ahrens 		    i * sizeof (bp), sizeof (bp), &bp, 0);
460732885fcSMatthew Ahrens 		if (err != 0) {
461732885fcSMatthew Ahrens 			(void) printf("got error %u from dmu_read\n", err);
462732885fcSMatthew Ahrens 			break;
463732885fcSMatthew Ahrens 		}
464732885fcSMatthew Ahrens 		snprintf_blkptr_compact(blkbuf, sizeof (blkbuf), &bp);
465732885fcSMatthew Ahrens 		(void) printf("\t%s\n", blkbuf);
466732885fcSMatthew Ahrens 	}
467732885fcSMatthew Ahrens }
468732885fcSMatthew Ahrens 
469732885fcSMatthew Ahrens /* ARGSUSED */
470732885fcSMatthew Ahrens static void
471732885fcSMatthew Ahrens dump_bpobj_subobjs(objset_t *os, uint64_t object, void *data, size_t size)
472732885fcSMatthew Ahrens {
473732885fcSMatthew Ahrens 	dmu_object_info_t doi;
474732885fcSMatthew Ahrens 
475732885fcSMatthew Ahrens 	VERIFY0(dmu_object_info(os, object, &doi));
476732885fcSMatthew Ahrens 	uint64_t *subobjs = kmem_alloc(doi.doi_max_offset, KM_SLEEP);
477732885fcSMatthew Ahrens 
478732885fcSMatthew Ahrens 	int err = dmu_read(os, object, 0, doi.doi_max_offset, subobjs, 0);
479732885fcSMatthew Ahrens 	if (err != 0) {
480732885fcSMatthew Ahrens 		(void) printf("got error %u from dmu_read\n", err);
481732885fcSMatthew Ahrens 		kmem_free(subobjs, doi.doi_max_offset);
482732885fcSMatthew Ahrens 		return;
483732885fcSMatthew Ahrens 	}
484732885fcSMatthew Ahrens 
485732885fcSMatthew Ahrens 	int64_t last_nonzero = -1;
486732885fcSMatthew Ahrens 	for (uint64_t i = 0; i < doi.doi_max_offset / 8; i++) {
487732885fcSMatthew Ahrens 		if (subobjs[i] != 0)
488732885fcSMatthew Ahrens 			last_nonzero = i;
489732885fcSMatthew Ahrens 	}
490732885fcSMatthew Ahrens 
491732885fcSMatthew Ahrens 	for (int64_t i = 0; i <= last_nonzero; i++) {
492732885fcSMatthew Ahrens 		(void) printf("\t%llu\n", (longlong_t)subobjs[i]);
493732885fcSMatthew Ahrens 	}
494732885fcSMatthew Ahrens 	kmem_free(subobjs, doi.doi_max_offset);
495732885fcSMatthew Ahrens }
496732885fcSMatthew Ahrens 
497486ae710SMatthew Ahrens /*ARGSUSED*/
498486ae710SMatthew Ahrens static void
499486ae710SMatthew Ahrens dump_ddt_zap(objset_t *os, uint64_t object, void *data, size_t size)
500486ae710SMatthew Ahrens {
501486ae710SMatthew Ahrens 	dump_zap_stats(os, object);
502486ae710SMatthew Ahrens 	/* contents are printed elsewhere, properly decoded */
503486ae710SMatthew Ahrens }
504486ae710SMatthew Ahrens 
5050a586ceaSMark Shellenbaum /*ARGSUSED*/
5060a586ceaSMark Shellenbaum static void
5070a586ceaSMark Shellenbaum dump_sa_attrs(objset_t *os, uint64_t object, void *data, size_t size)
5080a586ceaSMark Shellenbaum {
5090a586ceaSMark Shellenbaum 	zap_cursor_t zc;
5100a586ceaSMark Shellenbaum 	zap_attribute_t attr;
5110a586ceaSMark Shellenbaum 
5120a586ceaSMark Shellenbaum 	dump_zap_stats(os, object);
5130a586ceaSMark Shellenbaum 	(void) printf("\n");
5140a586ceaSMark Shellenbaum 
5150a586ceaSMark Shellenbaum 	for (zap_cursor_init(&zc, os, object);
5160a586ceaSMark Shellenbaum 	    zap_cursor_retrieve(&zc, &attr) == 0;
5170a586ceaSMark Shellenbaum 	    zap_cursor_advance(&zc)) {
5180a586ceaSMark Shellenbaum 		(void) printf("\t\t%s = ", attr.za_name);
5190a586ceaSMark Shellenbaum 		if (attr.za_num_integers == 0) {
5200a586ceaSMark Shellenbaum 			(void) printf("\n");
5210a586ceaSMark Shellenbaum 			continue;
5220a586ceaSMark Shellenbaum 		}
5230a586ceaSMark Shellenbaum 		(void) printf(" %llx : [%d:%d:%d]\n",
5240a586ceaSMark Shellenbaum 		    (u_longlong_t)attr.za_first_integer,
5250a586ceaSMark Shellenbaum 		    (int)ATTR_LENGTH(attr.za_first_integer),
5260a586ceaSMark Shellenbaum 		    (int)ATTR_BSWAP(attr.za_first_integer),
5270a586ceaSMark Shellenbaum 		    (int)ATTR_NUM(attr.za_first_integer));
5280a586ceaSMark Shellenbaum 	}
5290a586ceaSMark Shellenbaum 	zap_cursor_fini(&zc);
5300a586ceaSMark Shellenbaum }
5310a586ceaSMark Shellenbaum 
5320a586ceaSMark Shellenbaum /*ARGSUSED*/
5330a586ceaSMark Shellenbaum static void
5340a586ceaSMark Shellenbaum dump_sa_layouts(objset_t *os, uint64_t object, void *data, size_t size)
5350a586ceaSMark Shellenbaum {
5360a586ceaSMark Shellenbaum 	zap_cursor_t zc;
5370a586ceaSMark Shellenbaum 	zap_attribute_t attr;
5380a586ceaSMark Shellenbaum 	uint16_t *layout_attrs;
5390a586ceaSMark Shellenbaum 	int i;
5400a586ceaSMark Shellenbaum 
5410a586ceaSMark Shellenbaum 	dump_zap_stats(os, object);
5420a586ceaSMark Shellenbaum 	(void) printf("\n");
5430a586ceaSMark Shellenbaum 
5440a586ceaSMark Shellenbaum 	for (zap_cursor_init(&zc, os, object);
5450a586ceaSMark Shellenbaum 	    zap_cursor_retrieve(&zc, &attr) == 0;
5460a586ceaSMark Shellenbaum 	    zap_cursor_advance(&zc)) {
5470a586ceaSMark Shellenbaum 		(void) printf("\t\t%s = [", attr.za_name);
5480a586ceaSMark Shellenbaum 		if (attr.za_num_integers == 0) {
5490a586ceaSMark Shellenbaum 			(void) printf("\n");
5500a586ceaSMark Shellenbaum 			continue;
5510a586ceaSMark Shellenbaum 		}
5520a586ceaSMark Shellenbaum 
5530a586ceaSMark Shellenbaum 		VERIFY(attr.za_integer_length == 2);
5540a586ceaSMark Shellenbaum 		layout_attrs = umem_zalloc(attr.za_num_integers *
5550a586ceaSMark Shellenbaum 		    attr.za_integer_length, UMEM_NOFAIL);
5560a586ceaSMark Shellenbaum 
5570a586ceaSMark Shellenbaum 		VERIFY(zap_lookup(os, object, attr.za_name,
5580a586ceaSMark Shellenbaum 		    attr.za_integer_length,
5590a586ceaSMark Shellenbaum 		    attr.za_num_integers, layout_attrs) == 0);
5600a586ceaSMark Shellenbaum 
5610a586ceaSMark Shellenbaum 		for (i = 0; i != attr.za_num_integers; i++)
5620a586ceaSMark Shellenbaum 			(void) printf(" %d ", (int)layout_attrs[i]);
5630a586ceaSMark Shellenbaum 		(void) printf("]\n");
5640a586ceaSMark Shellenbaum 		umem_free(layout_attrs,
5650a586ceaSMark Shellenbaum 		    attr.za_num_integers * attr.za_integer_length);
5660a586ceaSMark Shellenbaum 	}
5670a586ceaSMark Shellenbaum 	zap_cursor_fini(&zc);
5680a586ceaSMark Shellenbaum }
5690a586ceaSMark Shellenbaum 
570e7437265Sahrens /*ARGSUSED*/
571e7437265Sahrens static void
572e7437265Sahrens dump_zpldir(objset_t *os, uint64_t object, void *data, size_t size)
573e7437265Sahrens {
574e7437265Sahrens 	zap_cursor_t zc;
575e7437265Sahrens 	zap_attribute_t attr;
576e7437265Sahrens 	const char *typenames[] = {
577e7437265Sahrens 		/* 0 */ "not specified",
578e7437265Sahrens 		/* 1 */ "FIFO",
579e7437265Sahrens 		/* 2 */ "Character Device",
580e7437265Sahrens 		/* 3 */ "3 (invalid)",
581e7437265Sahrens 		/* 4 */ "Directory",
582e7437265Sahrens 		/* 5 */ "5 (invalid)",
583e7437265Sahrens 		/* 6 */ "Block Device",
584e7437265Sahrens 		/* 7 */ "7 (invalid)",
585e7437265Sahrens 		/* 8 */ "Regular File",
586e7437265Sahrens 		/* 9 */ "9 (invalid)",
587e7437265Sahrens 		/* 10 */ "Symbolic Link",
588e7437265Sahrens 		/* 11 */ "11 (invalid)",
589e7437265Sahrens 		/* 12 */ "Socket",
590e7437265Sahrens 		/* 13 */ "Door",
591e7437265Sahrens 		/* 14 */ "Event Port",
592e7437265Sahrens 		/* 15 */ "15 (invalid)",
593e7437265Sahrens 	};
594e7437265Sahrens 
595e7437265Sahrens 	dump_zap_stats(os, object);
596e7437265Sahrens 	(void) printf("\n");
597e7437265Sahrens 
598e7437265Sahrens 	for (zap_cursor_init(&zc, os, object);
599e7437265Sahrens 	    zap_cursor_retrieve(&zc, &attr) == 0;
600e7437265Sahrens 	    zap_cursor_advance(&zc)) {
601e7437265Sahrens 		(void) printf("\t\t%s = %lld (type: %s)\n",
602e7437265Sahrens 		    attr.za_name, ZFS_DIRENT_OBJ(attr.za_first_integer),
603e7437265Sahrens 		    typenames[ZFS_DIRENT_TYPE(attr.za_first_integer)]);
604e7437265Sahrens 	}
605e7437265Sahrens 	zap_cursor_fini(&zc);
606e7437265Sahrens }
607e7437265Sahrens 
6080713e232SGeorge Wilson int
6090713e232SGeorge Wilson get_dtl_refcount(vdev_t *vd)
6100713e232SGeorge Wilson {
6110713e232SGeorge Wilson 	int refcount = 0;
6120713e232SGeorge Wilson 
6130713e232SGeorge Wilson 	if (vd->vdev_ops->vdev_op_leaf) {
6140713e232SGeorge Wilson 		space_map_t *sm = vd->vdev_dtl_sm;
6150713e232SGeorge Wilson 
6160713e232SGeorge Wilson 		if (sm != NULL &&
6170713e232SGeorge Wilson 		    sm->sm_dbuf->db_size == sizeof (space_map_phys_t))
6180713e232SGeorge Wilson 			return (1);
6190713e232SGeorge Wilson 		return (0);
6200713e232SGeorge Wilson 	}
6210713e232SGeorge Wilson 
6220713e232SGeorge Wilson 	for (int c = 0; c < vd->vdev_children; c++)
6230713e232SGeorge Wilson 		refcount += get_dtl_refcount(vd->vdev_child[c]);
6240713e232SGeorge Wilson 	return (refcount);
6250713e232SGeorge Wilson }
6260713e232SGeorge Wilson 
6270713e232SGeorge Wilson int
6280713e232SGeorge Wilson get_metaslab_refcount(vdev_t *vd)
6290713e232SGeorge Wilson {
6300713e232SGeorge Wilson 	int refcount = 0;
6310713e232SGeorge Wilson 
6322e4c9986SGeorge Wilson 	if (vd->vdev_top == vd && !vd->vdev_removing) {
6330713e232SGeorge Wilson 		for (int m = 0; m < vd->vdev_ms_count; m++) {
6340713e232SGeorge Wilson 			space_map_t *sm = vd->vdev_ms[m]->ms_sm;
6350713e232SGeorge Wilson 
6360713e232SGeorge Wilson 			if (sm != NULL &&
6370713e232SGeorge Wilson 			    sm->sm_dbuf->db_size == sizeof (space_map_phys_t))
6380713e232SGeorge Wilson 				refcount++;
6390713e232SGeorge Wilson 		}
6400713e232SGeorge Wilson 	}
6410713e232SGeorge Wilson 	for (int c = 0; c < vd->vdev_children; c++)
6420713e232SGeorge Wilson 		refcount += get_metaslab_refcount(vd->vdev_child[c]);
6430713e232SGeorge Wilson 
6440713e232SGeorge Wilson 	return (refcount);
6450713e232SGeorge Wilson }
6460713e232SGeorge Wilson 
6470713e232SGeorge Wilson static int
6480713e232SGeorge Wilson verify_spacemap_refcounts(spa_t *spa)
6490713e232SGeorge Wilson {
6502acef22dSMatthew Ahrens 	uint64_t expected_refcount = 0;
6512acef22dSMatthew Ahrens 	uint64_t actual_refcount;
6520713e232SGeorge Wilson 
6532acef22dSMatthew Ahrens 	(void) feature_get_refcount(spa,
6542acef22dSMatthew Ahrens 	    &spa_feature_table[SPA_FEATURE_SPACEMAP_HISTOGRAM],
6552acef22dSMatthew Ahrens 	    &expected_refcount);
6560713e232SGeorge Wilson 	actual_refcount = get_dtl_refcount(spa->spa_root_vdev);
6570713e232SGeorge Wilson 	actual_refcount += get_metaslab_refcount(spa->spa_root_vdev);
6580713e232SGeorge Wilson 
6590713e232SGeorge Wilson 	if (expected_refcount != actual_refcount) {
6602acef22dSMatthew Ahrens 		(void) printf("space map refcount mismatch: expected %lld != "
6612acef22dSMatthew Ahrens 		    "actual %lld\n",
6622acef22dSMatthew Ahrens 		    (longlong_t)expected_refcount,
6632acef22dSMatthew Ahrens 		    (longlong_t)actual_refcount);
6640713e232SGeorge Wilson 		return (2);
6650713e232SGeorge Wilson 	}
6660713e232SGeorge Wilson 	return (0);
6670713e232SGeorge Wilson }
6680713e232SGeorge Wilson 
669fa9e4066Sahrens static void
6700713e232SGeorge Wilson dump_spacemap(objset_t *os, space_map_t *sm)
671fa9e4066Sahrens {
672fa9e4066Sahrens 	uint64_t alloc, offset, entry;
6738053a263Sck 	char *ddata[] = { "ALLOC", "FREE", "CONDENSE", "INVALID",
6748053a263Sck 			    "INVALID", "INVALID", "INVALID", "INVALID" };
675fa9e4066Sahrens 
6760713e232SGeorge Wilson 	if (sm == NULL)
677fa9e4066Sahrens 		return;
678fa9e4066Sahrens 
679fa9e4066Sahrens 	/*
680fa9e4066Sahrens 	 * Print out the freelist entries in both encoded and decoded form.
681fa9e4066Sahrens 	 */
682fa9e4066Sahrens 	alloc = 0;
6830713e232SGeorge Wilson 	for (offset = 0; offset < space_map_length(sm);
6840713e232SGeorge Wilson 	    offset += sizeof (entry)) {
6850713e232SGeorge Wilson 		uint8_t mapshift = sm->sm_shift;
6860713e232SGeorge Wilson 
6870713e232SGeorge Wilson 		VERIFY0(dmu_read(os, space_map_object(sm), offset,
6887bfdf011SNeil Perrin 		    sizeof (entry), &entry, DMU_READ_PREFETCH));
689fa9e4066Sahrens 		if (SM_DEBUG_DECODE(entry)) {
6900713e232SGeorge Wilson 
69187219db7SVictor Latushkin 			(void) printf("\t    [%6llu] %s: txg %llu, pass %llu\n",
692fa9e4066Sahrens 			    (u_longlong_t)(offset / sizeof (entry)),
693fa9e4066Sahrens 			    ddata[SM_DEBUG_ACTION_DECODE(entry)],
6945ad82045Snd 			    (u_longlong_t)SM_DEBUG_TXG_DECODE(entry),
6955ad82045Snd 			    (u_longlong_t)SM_DEBUG_SYNCPASS_DECODE(entry));
696fa9e4066Sahrens 		} else {
69787219db7SVictor Latushkin 			(void) printf("\t    [%6llu]    %c  range:"
69887219db7SVictor Latushkin 			    " %010llx-%010llx  size: %06llx\n",
699fa9e4066Sahrens 			    (u_longlong_t)(offset / sizeof (entry)),
700fa9e4066Sahrens 			    SM_TYPE_DECODE(entry) == SM_ALLOC ? 'A' : 'F',
7015ad82045Snd 			    (u_longlong_t)((SM_OFFSET_DECODE(entry) <<
7020713e232SGeorge Wilson 			    mapshift) + sm->sm_start),
7035ad82045Snd 			    (u_longlong_t)((SM_OFFSET_DECODE(entry) <<
7040713e232SGeorge Wilson 			    mapshift) + sm->sm_start +
7050713e232SGeorge Wilson 			    (SM_RUN_DECODE(entry) << mapshift)),
7065ad82045Snd 			    (u_longlong_t)(SM_RUN_DECODE(entry) << mapshift));
707fa9e4066Sahrens 			if (SM_TYPE_DECODE(entry) == SM_ALLOC)
708fa9e4066Sahrens 				alloc += SM_RUN_DECODE(entry) << mapshift;
709fa9e4066Sahrens 			else
710fa9e4066Sahrens 				alloc -= SM_RUN_DECODE(entry) << mapshift;
711fa9e4066Sahrens 		}
712fa9e4066Sahrens 	}
7130713e232SGeorge Wilson 	if (alloc != space_map_allocated(sm)) {
714fa9e4066Sahrens 		(void) printf("space_map_object alloc (%llu) INCONSISTENT "
715fa9e4066Sahrens 		    "with space map summary (%llu)\n",
7160713e232SGeorge Wilson 		    (u_longlong_t)space_map_allocated(sm), (u_longlong_t)alloc);
717fa9e4066Sahrens 	}
718fa9e4066Sahrens }
719fa9e4066Sahrens 
720d6e555bdSGeorge Wilson static void
721d6e555bdSGeorge Wilson dump_metaslab_stats(metaslab_t *msp)
722d6e555bdSGeorge Wilson {
7233f9d6ad7SLin Ling 	char maxbuf[32];
7240713e232SGeorge Wilson 	range_tree_t *rt = msp->ms_tree;
7250713e232SGeorge Wilson 	avl_tree_t *t = &msp->ms_size_tree;
7260713e232SGeorge Wilson 	int free_pct = range_tree_space(rt) * 100 / msp->ms_size;
727d6e555bdSGeorge Wilson 
7280713e232SGeorge Wilson 	zdb_nicenum(metaslab_block_maxsize(msp), maxbuf);
729d6e555bdSGeorge Wilson 
73087219db7SVictor Latushkin 	(void) printf("\t %25s %10lu   %7s  %6s   %4s %4d%%\n",
731d6e555bdSGeorge Wilson 	    "segments", avl_numnodes(t), "maxsize", maxbuf,
732d6e555bdSGeorge Wilson 	    "freepct", free_pct);
7330713e232SGeorge Wilson 	(void) printf("\tIn-memory histogram:\n");
7340713e232SGeorge Wilson 	dump_histogram(rt->rt_histogram, RANGE_TREE_HISTOGRAM_SIZE, 0);
735d6e555bdSGeorge Wilson }
736d6e555bdSGeorge Wilson 
737fa9e4066Sahrens static void
738fa9e4066Sahrens dump_metaslab(metaslab_t *msp)
739fa9e4066Sahrens {
740fa9e4066Sahrens 	vdev_t *vd = msp->ms_group->mg_vd;
741fa9e4066Sahrens 	spa_t *spa = vd->vdev_spa;
7420713e232SGeorge Wilson 	space_map_t *sm = msp->ms_sm;
7433f9d6ad7SLin Ling 	char freebuf[32];
744fa9e4066Sahrens 
7450713e232SGeorge Wilson 	zdb_nicenum(msp->ms_size - space_map_allocated(sm), freebuf);
746fa9e4066Sahrens 
747fa9e4066Sahrens 	(void) printf(
74887219db7SVictor Latushkin 	    "\tmetaslab %6llu   offset %12llx   spacemap %6llu   free    %5s\n",
7490713e232SGeorge Wilson 	    (u_longlong_t)msp->ms_id, (u_longlong_t)msp->ms_start,
7500713e232SGeorge Wilson 	    (u_longlong_t)space_map_object(sm), freebuf);
751fa9e4066Sahrens 
7520713e232SGeorge Wilson 	if (dump_opt['m'] > 2 && !dump_opt['L']) {
753d6e555bdSGeorge Wilson 		mutex_enter(&msp->ms_lock);
7540713e232SGeorge Wilson 		metaslab_load_wait(msp);
7550713e232SGeorge Wilson 		if (!msp->ms_loaded) {
7560713e232SGeorge Wilson 			VERIFY0(metaslab_load(msp));
7570713e232SGeorge Wilson 			range_tree_stat_verify(msp->ms_tree);
7580713e232SGeorge Wilson 		}
759bc3975b5SVictor Latushkin 		dump_metaslab_stats(msp);
7600713e232SGeorge Wilson 		metaslab_unload(msp);
761d6e555bdSGeorge Wilson 		mutex_exit(&msp->ms_lock);
762d6e555bdSGeorge Wilson 	}
763d6e555bdSGeorge Wilson 
7640713e232SGeorge Wilson 	if (dump_opt['m'] > 1 && sm != NULL &&
7652acef22dSMatthew Ahrens 	    spa_feature_is_active(spa, SPA_FEATURE_SPACEMAP_HISTOGRAM)) {
7660713e232SGeorge Wilson 		/*
7670713e232SGeorge Wilson 		 * The space map histogram represents free space in chunks
7680713e232SGeorge Wilson 		 * of sm_shift (i.e. bucket 0 refers to 2^sm_shift).
7690713e232SGeorge Wilson 		 */
7702e4c9986SGeorge Wilson 		(void) printf("\tOn-disk histogram:\t\tfragmentation %llu\n",
7712e4c9986SGeorge Wilson 		    (u_longlong_t)msp->ms_fragmentation);
7720713e232SGeorge Wilson 		dump_histogram(sm->sm_phys->smp_histogram,
7732e4c9986SGeorge Wilson 		    SPACE_MAP_HISTOGRAM_SIZE, sm->sm_shift);
7740713e232SGeorge Wilson 	}
7750713e232SGeorge Wilson 
7760713e232SGeorge Wilson 	if (dump_opt['d'] > 5 || dump_opt['m'] > 3) {
7770713e232SGeorge Wilson 		ASSERT(msp->ms_size == (1ULL << vd->vdev_ms_shift));
778d6e555bdSGeorge Wilson 
779d6e555bdSGeorge Wilson 		mutex_enter(&msp->ms_lock);
7800713e232SGeorge Wilson 		dump_spacemap(spa->spa_meta_objset, msp->ms_sm);
781d6e555bdSGeorge Wilson 		mutex_exit(&msp->ms_lock);
782d6e555bdSGeorge Wilson 	}
78387219db7SVictor Latushkin }
784fa9e4066Sahrens 
78587219db7SVictor Latushkin static void
78687219db7SVictor Latushkin print_vdev_metaslab_header(vdev_t *vd)
78787219db7SVictor Latushkin {
78887219db7SVictor Latushkin 	(void) printf("\tvdev %10llu\n\t%-10s%5llu   %-19s   %-15s   %-10s\n",
78987219db7SVictor Latushkin 	    (u_longlong_t)vd->vdev_id,
79087219db7SVictor Latushkin 	    "metaslabs", (u_longlong_t)vd->vdev_ms_count,
79187219db7SVictor Latushkin 	    "offset", "spacemap", "free");
79287219db7SVictor Latushkin 	(void) printf("\t%15s   %19s   %15s   %10s\n",
79387219db7SVictor Latushkin 	    "---------------", "-------------------",
79487219db7SVictor Latushkin 	    "---------------", "-------------");
795fa9e4066Sahrens }
796fa9e4066Sahrens 
7972e4c9986SGeorge Wilson static void
7982e4c9986SGeorge Wilson dump_metaslab_groups(spa_t *spa)
7992e4c9986SGeorge Wilson {
8002e4c9986SGeorge Wilson 	vdev_t *rvd = spa->spa_root_vdev;
8012e4c9986SGeorge Wilson 	metaslab_class_t *mc = spa_normal_class(spa);
8022e4c9986SGeorge Wilson 	uint64_t fragmentation;
8032e4c9986SGeorge Wilson 
8042e4c9986SGeorge Wilson 	metaslab_class_histogram_verify(mc);
8052e4c9986SGeorge Wilson 
8062e4c9986SGeorge Wilson 	for (int c = 0; c < rvd->vdev_children; c++) {
8072e4c9986SGeorge Wilson 		vdev_t *tvd = rvd->vdev_child[c];
8082e4c9986SGeorge Wilson 		metaslab_group_t *mg = tvd->vdev_mg;
8092e4c9986SGeorge Wilson 
8102e4c9986SGeorge Wilson 		if (mg->mg_class != mc)
8112e4c9986SGeorge Wilson 			continue;
8122e4c9986SGeorge Wilson 
8132e4c9986SGeorge Wilson 		metaslab_group_histogram_verify(mg);
8142e4c9986SGeorge Wilson 		mg->mg_fragmentation = metaslab_group_fragmentation(mg);
8152e4c9986SGeorge Wilson 
8162e4c9986SGeorge Wilson 		(void) printf("\tvdev %10llu\t\tmetaslabs%5llu\t\t"
8172e4c9986SGeorge Wilson 		    "fragmentation",
8182e4c9986SGeorge Wilson 		    (u_longlong_t)tvd->vdev_id,
8192e4c9986SGeorge Wilson 		    (u_longlong_t)tvd->vdev_ms_count);
8202e4c9986SGeorge Wilson 		if (mg->mg_fragmentation == ZFS_FRAG_INVALID) {
8212e4c9986SGeorge Wilson 			(void) printf("%3s\n", "-");
8222e4c9986SGeorge Wilson 		} else {
8232e4c9986SGeorge Wilson 			(void) printf("%3llu%%\n",
8242e4c9986SGeorge Wilson 			    (u_longlong_t)mg->mg_fragmentation);
8252e4c9986SGeorge Wilson 		}
8262e4c9986SGeorge Wilson 		dump_histogram(mg->mg_histogram, RANGE_TREE_HISTOGRAM_SIZE, 0);
8272e4c9986SGeorge Wilson 	}
8282e4c9986SGeorge Wilson 
8292e4c9986SGeorge Wilson 	(void) printf("\tpool %s\tfragmentation", spa_name(spa));
8302e4c9986SGeorge Wilson 	fragmentation = metaslab_class_fragmentation(mc);
8312e4c9986SGeorge Wilson 	if (fragmentation == ZFS_FRAG_INVALID)
8322e4c9986SGeorge Wilson 		(void) printf("\t%3s\n", "-");
8332e4c9986SGeorge Wilson 	else
8342e4c9986SGeorge Wilson 		(void) printf("\t%3llu%%\n", (u_longlong_t)fragmentation);
8352e4c9986SGeorge Wilson 	dump_histogram(mc->mc_histogram, RANGE_TREE_HISTOGRAM_SIZE, 0);
8362e4c9986SGeorge Wilson }
8372e4c9986SGeorge Wilson 
838fa9e4066Sahrens static void
839fa9e4066Sahrens dump_metaslabs(spa_t *spa)
840fa9e4066Sahrens {
84187219db7SVictor Latushkin 	vdev_t *vd, *rvd = spa->spa_root_vdev;
84287219db7SVictor Latushkin 	uint64_t m, c = 0, children = rvd->vdev_children;
843fa9e4066Sahrens 
844fa9e4066Sahrens 	(void) printf("\nMetaslabs:\n");
845fa9e4066Sahrens 
84687219db7SVictor Latushkin 	if (!dump_opt['d'] && zopt_objects > 0) {
84787219db7SVictor Latushkin 		c = zopt_object[0];
84887219db7SVictor Latushkin 
84987219db7SVictor Latushkin 		if (c >= children)
85087219db7SVictor Latushkin 			(void) fatal("bad vdev id: %llu", (u_longlong_t)c);
851fa9e4066Sahrens 
85287219db7SVictor Latushkin 		if (zopt_objects > 1) {
85387219db7SVictor Latushkin 			vd = rvd->vdev_child[c];
85487219db7SVictor Latushkin 			print_vdev_metaslab_header(vd);
85587219db7SVictor Latushkin 
85687219db7SVictor Latushkin 			for (m = 1; m < zopt_objects; m++) {
85787219db7SVictor Latushkin 				if (zopt_object[m] < vd->vdev_ms_count)
85887219db7SVictor Latushkin 					dump_metaslab(
85987219db7SVictor Latushkin 					    vd->vdev_ms[zopt_object[m]]);
86087219db7SVictor Latushkin 				else
86187219db7SVictor Latushkin 					(void) fprintf(stderr, "bad metaslab "
86287219db7SVictor Latushkin 					    "number %llu\n",
86387219db7SVictor Latushkin 					    (u_longlong_t)zopt_object[m]);
86487219db7SVictor Latushkin 			}
86587219db7SVictor Latushkin 			(void) printf("\n");
86687219db7SVictor Latushkin 			return;
86787219db7SVictor Latushkin 		}
86887219db7SVictor Latushkin 		children = c + 1;
86987219db7SVictor Latushkin 	}
87087219db7SVictor Latushkin 	for (; c < children; c++) {
87187219db7SVictor Latushkin 		vd = rvd->vdev_child[c];
87287219db7SVictor Latushkin 		print_vdev_metaslab_header(vd);
873fa9e4066Sahrens 
874fa9e4066Sahrens 		for (m = 0; m < vd->vdev_ms_count; m++)
875fa9e4066Sahrens 			dump_metaslab(vd->vdev_ms[m]);
876fa9e4066Sahrens 		(void) printf("\n");
877fa9e4066Sahrens 	}
878fa9e4066Sahrens }
879fa9e4066Sahrens 
880b24ab676SJeff Bonwick static void
881b24ab676SJeff Bonwick dump_dde(const ddt_t *ddt, const ddt_entry_t *dde, uint64_t index)
882b24ab676SJeff Bonwick {
883b24ab676SJeff Bonwick 	const ddt_phys_t *ddp = dde->dde_phys;
884b24ab676SJeff Bonwick 	const ddt_key_t *ddk = &dde->dde_key;
885b24ab676SJeff Bonwick 	char *types[4] = { "ditto", "single", "double", "triple" };
886b24ab676SJeff Bonwick 	char blkbuf[BP_SPRINTF_LEN];
887b24ab676SJeff Bonwick 	blkptr_t blk;
888b24ab676SJeff Bonwick 
889b24ab676SJeff Bonwick 	for (int p = 0; p < DDT_PHYS_TYPES; p++, ddp++) {
890b24ab676SJeff Bonwick 		if (ddp->ddp_phys_birth == 0)
891b24ab676SJeff Bonwick 			continue;
892bbfd46c4SJeff Bonwick 		ddt_bp_create(ddt->ddt_checksum, ddk, ddp, &blk);
89343466aaeSMax Grossman 		snprintf_blkptr(blkbuf, sizeof (blkbuf), &blk);
894b24ab676SJeff Bonwick 		(void) printf("index %llx refcnt %llu %s %s\n",
895b24ab676SJeff Bonwick 		    (u_longlong_t)index, (u_longlong_t)ddp->ddp_refcnt,
896b24ab676SJeff Bonwick 		    types[p], blkbuf);
897b24ab676SJeff Bonwick 	}
898b24ab676SJeff Bonwick }
899b24ab676SJeff Bonwick 
900b24ab676SJeff Bonwick static void
901b24ab676SJeff Bonwick dump_dedup_ratio(const ddt_stat_t *dds)
902b24ab676SJeff Bonwick {
903b24ab676SJeff Bonwick 	double rL, rP, rD, D, dedup, compress, copies;
904b24ab676SJeff Bonwick 
905b24ab676SJeff Bonwick 	if (dds->dds_blocks == 0)
906b24ab676SJeff Bonwick 		return;
907b24ab676SJeff Bonwick 
908b24ab676SJeff Bonwick 	rL = (double)dds->dds_ref_lsize;
909b24ab676SJeff Bonwick 	rP = (double)dds->dds_ref_psize;
910b24ab676SJeff Bonwick 	rD = (double)dds->dds_ref_dsize;
911b24ab676SJeff Bonwick 	D = (double)dds->dds_dsize;
912b24ab676SJeff Bonwick 
913b24ab676SJeff Bonwick 	dedup = rD / D;
914b24ab676SJeff Bonwick 	compress = rL / rP;
915b24ab676SJeff Bonwick 	copies = rD / rP;
916b24ab676SJeff Bonwick 
917b24ab676SJeff Bonwick 	(void) printf("dedup = %.2f, compress = %.2f, copies = %.2f, "
918b24ab676SJeff Bonwick 	    "dedup * compress / copies = %.2f\n\n",
919b24ab676SJeff Bonwick 	    dedup, compress, copies, dedup * compress / copies);
920b24ab676SJeff Bonwick }
921b24ab676SJeff Bonwick 
922b24ab676SJeff Bonwick static void
923b24ab676SJeff Bonwick dump_ddt(ddt_t *ddt, enum ddt_type type, enum ddt_class class)
924b24ab676SJeff Bonwick {
925b24ab676SJeff Bonwick 	char name[DDT_NAMELEN];
926b24ab676SJeff Bonwick 	ddt_entry_t dde;
927b24ab676SJeff Bonwick 	uint64_t walk = 0;
928b24ab676SJeff Bonwick 	dmu_object_info_t doi;
929b24ab676SJeff Bonwick 	uint64_t count, dspace, mspace;
930b24ab676SJeff Bonwick 	int error;
931b24ab676SJeff Bonwick 
932b24ab676SJeff Bonwick 	error = ddt_object_info(ddt, type, class, &doi);
933b24ab676SJeff Bonwick 
934b24ab676SJeff Bonwick 	if (error == ENOENT)
935b24ab676SJeff Bonwick 		return;
936b24ab676SJeff Bonwick 	ASSERT(error == 0);
937b24ab676SJeff Bonwick 
9387448a079SGeorge Wilson 	if ((count = ddt_object_count(ddt, type, class)) == 0)
9397448a079SGeorge Wilson 		return;
9407448a079SGeorge Wilson 
941b24ab676SJeff Bonwick 	dspace = doi.doi_physical_blocks_512 << 9;
942b24ab676SJeff Bonwick 	mspace = doi.doi_fill_count * doi.doi_data_block_size;
943b24ab676SJeff Bonwick 
944b24ab676SJeff Bonwick 	ddt_object_name(ddt, type, class, name);
945b24ab676SJeff Bonwick 
946b24ab676SJeff Bonwick 	(void) printf("%s: %llu entries, size %llu on disk, %llu in core\n",
947b24ab676SJeff Bonwick 	    name,
948b24ab676SJeff Bonwick 	    (u_longlong_t)count,
949b24ab676SJeff Bonwick 	    (u_longlong_t)(dspace / count),
950b24ab676SJeff Bonwick 	    (u_longlong_t)(mspace / count));
951b24ab676SJeff Bonwick 
952b24ab676SJeff Bonwick 	if (dump_opt['D'] < 3)
953b24ab676SJeff Bonwick 		return;
954b24ab676SJeff Bonwick 
9559eb19f4dSGeorge Wilson 	zpool_dump_ddt(NULL, &ddt->ddt_histogram[type][class]);
956b24ab676SJeff Bonwick 
957b24ab676SJeff Bonwick 	if (dump_opt['D'] < 4)
958b24ab676SJeff Bonwick 		return;
959b24ab676SJeff Bonwick 
960b24ab676SJeff Bonwick 	if (dump_opt['D'] < 5 && class == DDT_CLASS_UNIQUE)
961b24ab676SJeff Bonwick 		return;
962b24ab676SJeff Bonwick 
963b24ab676SJeff Bonwick 	(void) printf("%s contents:\n\n", name);
964b24ab676SJeff Bonwick 
965bbfd46c4SJeff Bonwick 	while ((error = ddt_object_walk(ddt, type, class, &walk, &dde)) == 0)
966b24ab676SJeff Bonwick 		dump_dde(ddt, &dde, walk);
967b24ab676SJeff Bonwick 
968b24ab676SJeff Bonwick 	ASSERT(error == ENOENT);
969b24ab676SJeff Bonwick 
970b24ab676SJeff Bonwick 	(void) printf("\n");
971b24ab676SJeff Bonwick }
972b24ab676SJeff Bonwick 
973b24ab676SJeff Bonwick static void
974b24ab676SJeff Bonwick dump_all_ddts(spa_t *spa)
975b24ab676SJeff Bonwick {
976b24ab676SJeff Bonwick 	ddt_histogram_t ddh_total = { 0 };
977b24ab676SJeff Bonwick 	ddt_stat_t dds_total = { 0 };
978b24ab676SJeff Bonwick 
979b24ab676SJeff Bonwick 	for (enum zio_checksum c = 0; c < ZIO_CHECKSUM_FUNCTIONS; c++) {
980b24ab676SJeff Bonwick 		ddt_t *ddt = spa->spa_ddt[c];
981b24ab676SJeff Bonwick 		for (enum ddt_type type = 0; type < DDT_TYPES; type++) {
982b24ab676SJeff Bonwick 			for (enum ddt_class class = 0; class < DDT_CLASSES;
983b24ab676SJeff Bonwick 			    class++) {
984b24ab676SJeff Bonwick 				dump_ddt(ddt, type, class);
985b24ab676SJeff Bonwick 			}
986b24ab676SJeff Bonwick 		}
987b24ab676SJeff Bonwick 	}
988b24ab676SJeff Bonwick 
9899eb19f4dSGeorge Wilson 	ddt_get_dedup_stats(spa, &dds_total);
990b24ab676SJeff Bonwick 
991b24ab676SJeff Bonwick 	if (dds_total.dds_blocks == 0) {
992b24ab676SJeff Bonwick 		(void) printf("All DDTs are empty\n");
993b24ab676SJeff Bonwick 		return;
994b24ab676SJeff Bonwick 	}
995b24ab676SJeff Bonwick 
996b24ab676SJeff Bonwick 	(void) printf("\n");
997b24ab676SJeff Bonwick 
998b24ab676SJeff Bonwick 	if (dump_opt['D'] > 1) {
999b24ab676SJeff Bonwick 		(void) printf("DDT histogram (aggregated over all DDTs):\n");
10009eb19f4dSGeorge Wilson 		ddt_get_dedup_histogram(spa, &ddh_total);
10019eb19f4dSGeorge Wilson 		zpool_dump_ddt(&dds_total, &ddh_total);
1002b24ab676SJeff Bonwick 	}
1003b24ab676SJeff Bonwick 
1004b24ab676SJeff Bonwick 	dump_dedup_ratio(&dds_total);
1005b24ab676SJeff Bonwick }
1006b24ab676SJeff Bonwick 
10078ad4d6ddSJeff Bonwick static void
10080713e232SGeorge Wilson dump_dtl_seg(void *arg, uint64_t start, uint64_t size)
10098ad4d6ddSJeff Bonwick {
10100713e232SGeorge Wilson 	char *prefix = arg;
10118ad4d6ddSJeff Bonwick 
10128ad4d6ddSJeff Bonwick 	(void) printf("%s [%llu,%llu) length %llu\n",
10138ad4d6ddSJeff Bonwick 	    prefix,
10148ad4d6ddSJeff Bonwick 	    (u_longlong_t)start,
10158ad4d6ddSJeff Bonwick 	    (u_longlong_t)(start + size),
10168ad4d6ddSJeff Bonwick 	    (u_longlong_t)(size));
10178ad4d6ddSJeff Bonwick }
10188ad4d6ddSJeff Bonwick 
1019fa9e4066Sahrens static void
1020fa9e4066Sahrens dump_dtl(vdev_t *vd, int indent)
1021fa9e4066Sahrens {
10228ad4d6ddSJeff Bonwick 	spa_t *spa = vd->vdev_spa;
10238ad4d6ddSJeff Bonwick 	boolean_t required;
10248ad4d6ddSJeff Bonwick 	char *name[DTL_TYPES] = { "missing", "partial", "scrub", "outage" };
10258ad4d6ddSJeff Bonwick 	char prefix[256];
10268ad4d6ddSJeff Bonwick 
10278f18d1faSGeorge Wilson 	spa_vdev_state_enter(spa, SCL_NONE);
10288ad4d6ddSJeff Bonwick 	required = vdev_dtl_required(vd);
10298ad4d6ddSJeff Bonwick 	(void) spa_vdev_state_exit(spa, NULL, 0);
1030fa9e4066Sahrens 
1031fa9e4066Sahrens 	if (indent == 0)
1032fa9e4066Sahrens 		(void) printf("\nDirty time logs:\n\n");
1033fa9e4066Sahrens 
10348ad4d6ddSJeff Bonwick 	(void) printf("\t%*s%s [%s]\n", indent, "",
1035e14bb325SJeff Bonwick 	    vd->vdev_path ? vd->vdev_path :
10368ad4d6ddSJeff Bonwick 	    vd->vdev_parent ? vd->vdev_ops->vdev_op_type : spa_name(spa),
10378ad4d6ddSJeff Bonwick 	    required ? "DTL-required" : "DTL-expendable");
1038fa9e4066Sahrens 
10398ad4d6ddSJeff Bonwick 	for (int t = 0; t < DTL_TYPES; t++) {
10400713e232SGeorge Wilson 		range_tree_t *rt = vd->vdev_dtl[t];
10410713e232SGeorge Wilson 		if (range_tree_space(rt) == 0)
10428ad4d6ddSJeff Bonwick 			continue;
10438ad4d6ddSJeff Bonwick 		(void) snprintf(prefix, sizeof (prefix), "\t%*s%s",
10448ad4d6ddSJeff Bonwick 		    indent + 2, "", name[t]);
10450713e232SGeorge Wilson 		mutex_enter(rt->rt_lock);
10460713e232SGeorge Wilson 		range_tree_walk(rt, dump_dtl_seg, prefix);
10470713e232SGeorge Wilson 		mutex_exit(rt->rt_lock);
10488ad4d6ddSJeff Bonwick 		if (dump_opt['d'] > 5 && vd->vdev_children == 0)
10490713e232SGeorge Wilson 			dump_spacemap(spa->spa_meta_objset, vd->vdev_dtl_sm);
1050fa9e4066Sahrens 	}
1051fa9e4066Sahrens 
10528ad4d6ddSJeff Bonwick 	for (int c = 0; c < vd->vdev_children; c++)
1053fa9e4066Sahrens 		dump_dtl(vd->vdev_child[c], indent + 4);
1054fa9e4066Sahrens }
1055fa9e4066Sahrens 
10568f18d1faSGeorge Wilson static void
10578f18d1faSGeorge Wilson dump_history(spa_t *spa)
10588f18d1faSGeorge Wilson {
10598f18d1faSGeorge Wilson 	nvlist_t **events = NULL;
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 
10683f84190cSMatthew Ahrens 	char *buf = umem_alloc(SPA_MAXBLOCKSIZE, UMEM_NOFAIL);
10698f18d1faSGeorge Wilson 	do {
10703f84190cSMatthew Ahrens 		len = SPA_MAXBLOCKSIZE;
1071e4161df6SVictor Latushkin 
10728f18d1faSGeorge Wilson 		if ((error = spa_history_get(spa, &off, &len, buf)) != 0) {
10738f18d1faSGeorge Wilson 			(void) fprintf(stderr, "Unable to read history: "
10748f18d1faSGeorge Wilson 			    "error %d\n", error);
10753f84190cSMatthew Ahrens 			umem_free(buf, SPA_MAXBLOCKSIZE);
10768f18d1faSGeorge Wilson 			return;
10778f18d1faSGeorge Wilson 		}
10788f18d1faSGeorge Wilson 
10798f18d1faSGeorge Wilson 		if (zpool_history_unpack(buf, len, &resid, &events, &num) != 0)
10808f18d1faSGeorge Wilson 			break;
10818f18d1faSGeorge Wilson 
10828f18d1faSGeorge Wilson 		off -= resid;
10838f18d1faSGeorge Wilson 	} while (len != 0);
10843f84190cSMatthew Ahrens 	umem_free(buf, SPA_MAXBLOCKSIZE);
10858f18d1faSGeorge Wilson 
10868f18d1faSGeorge Wilson 	(void) printf("\nHistory:\n");
10878f18d1faSGeorge Wilson 	for (int i = 0; i < num; i++) {
10888f18d1faSGeorge Wilson 		uint64_t time, txg, ievent;
10898f18d1faSGeorge Wilson 		char *cmd, *intstr;
10904445fffbSMatthew Ahrens 		boolean_t printed = B_FALSE;
10918f18d1faSGeorge Wilson 
10928f18d1faSGeorge Wilson 		if (nvlist_lookup_uint64(events[i], ZPOOL_HIST_TIME,
10938f18d1faSGeorge Wilson 		    &time) != 0)
10944445fffbSMatthew Ahrens 			goto next;
10958f18d1faSGeorge Wilson 		if (nvlist_lookup_string(events[i], ZPOOL_HIST_CMD,
10968f18d1faSGeorge Wilson 		    &cmd) != 0) {
10978f18d1faSGeorge Wilson 			if (nvlist_lookup_uint64(events[i],
10988f18d1faSGeorge Wilson 			    ZPOOL_HIST_INT_EVENT, &ievent) != 0)
10994445fffbSMatthew Ahrens 				goto next;
11008f18d1faSGeorge Wilson 			verify(nvlist_lookup_uint64(events[i],
11018f18d1faSGeorge Wilson 			    ZPOOL_HIST_TXG, &txg) == 0);
11028f18d1faSGeorge Wilson 			verify(nvlist_lookup_string(events[i],
11038f18d1faSGeorge Wilson 			    ZPOOL_HIST_INT_STR, &intstr) == 0);
11044445fffbSMatthew Ahrens 			if (ievent >= ZFS_NUM_LEGACY_HISTORY_EVENTS)
11054445fffbSMatthew Ahrens 				goto next;
11068f18d1faSGeorge Wilson 
11078f18d1faSGeorge Wilson 			(void) snprintf(internalstr,
11088f18d1faSGeorge Wilson 			    sizeof (internalstr),
11098f18d1faSGeorge Wilson 			    "[internal %s txg:%lld] %s",
11103f9d6ad7SLin Ling 			    zfs_history_event_names[ievent], txg,
11118f18d1faSGeorge Wilson 			    intstr);
11128f18d1faSGeorge Wilson 			cmd = internalstr;
11138f18d1faSGeorge Wilson 		}
11148f18d1faSGeorge Wilson 		tsec = time;
11158f18d1faSGeorge Wilson 		(void) localtime_r(&tsec, &t);
11168f18d1faSGeorge Wilson 		(void) strftime(tbuf, sizeof (tbuf), "%F.%T", &t);
11178f18d1faSGeorge Wilson 		(void) printf("%s %s\n", tbuf, cmd);
11184445fffbSMatthew Ahrens 		printed = B_TRUE;
11194445fffbSMatthew Ahrens 
11204445fffbSMatthew Ahrens next:
11214445fffbSMatthew Ahrens 		if (dump_opt['h'] > 1) {
11224445fffbSMatthew Ahrens 			if (!printed)
11234445fffbSMatthew Ahrens 				(void) printf("unrecognized record:\n");
11244445fffbSMatthew Ahrens 			dump_nvlist(events[i], 2);
11254445fffbSMatthew Ahrens 		}
11268f18d1faSGeorge Wilson 	}
11278f18d1faSGeorge Wilson }
11288f18d1faSGeorge Wilson 
1129fa9e4066Sahrens /*ARGSUSED*/
1130fa9e4066Sahrens static void
1131fa9e4066Sahrens dump_dnode(objset_t *os, uint64_t object, void *data, size_t size)
1132fa9e4066Sahrens {
1133fa9e4066Sahrens }
1134fa9e4066Sahrens 
1135fa9e4066Sahrens static uint64_t
11367802d7bfSMatthew Ahrens blkid2offset(const dnode_phys_t *dnp, const blkptr_t *bp,
11377802d7bfSMatthew Ahrens     const zbookmark_phys_t *zb)
1138fa9e4066Sahrens {
1139b24ab676SJeff Bonwick 	if (dnp == NULL) {
1140b24ab676SJeff Bonwick 		ASSERT(zb->zb_level < 0);
1141b24ab676SJeff Bonwick 		if (zb->zb_object == 0)
1142b24ab676SJeff Bonwick 			return (zb->zb_blkid);
1143b24ab676SJeff Bonwick 		return (zb->zb_blkid * BP_GET_LSIZE(bp));
1144b24ab676SJeff Bonwick 	}
1145b24ab676SJeff Bonwick 
1146b24ab676SJeff Bonwick 	ASSERT(zb->zb_level >= 0);
1147fa9e4066Sahrens 
1148b24ab676SJeff Bonwick 	return ((zb->zb_blkid <<
1149b24ab676SJeff Bonwick 	    (zb->zb_level * (dnp->dn_indblkshift - SPA_BLKPTRSHIFT))) *
1150fa9e4066Sahrens 	    dnp->dn_datablkszsec << SPA_MINBLOCKSHIFT);
1151fa9e4066Sahrens }
1152fa9e4066Sahrens 
115344cd46caSbillm static void
115443466aaeSMax Grossman snprintf_blkptr_compact(char *blkbuf, size_t buflen, const blkptr_t *bp)
115544cd46caSbillm {
1156cde58dbcSMatthew Ahrens 	const dva_t *dva = bp->blk_dva;
1157b24ab676SJeff Bonwick 	int ndvas = dump_opt['d'] > 5 ? BP_GET_NDVAS(bp) : 1;
1158b24ab676SJeff Bonwick 
1159490d05b9SMatthew Ahrens 	if (dump_opt['b'] >= 6) {
116043466aaeSMax Grossman 		snprintf_blkptr(blkbuf, buflen, bp);
1161b24ab676SJeff Bonwick 		return;
1162b24ab676SJeff Bonwick 	}
116344cd46caSbillm 
11645d7b4d43SMatthew Ahrens 	if (BP_IS_EMBEDDED(bp)) {
11655d7b4d43SMatthew Ahrens 		(void) sprintf(blkbuf,
11665d7b4d43SMatthew Ahrens 		    "EMBEDDED et=%u %llxL/%llxP B=%llu",
11675d7b4d43SMatthew Ahrens 		    (int)BPE_GET_ETYPE(bp),
11685d7b4d43SMatthew Ahrens 		    (u_longlong_t)BPE_GET_LSIZE(bp),
11695d7b4d43SMatthew Ahrens 		    (u_longlong_t)BPE_GET_PSIZE(bp),
11705d7b4d43SMatthew Ahrens 		    (u_longlong_t)bp->blk_birth);
11715d7b4d43SMatthew Ahrens 		return;
11725d7b4d43SMatthew Ahrens 	}
117344cd46caSbillm 
11745d7b4d43SMatthew Ahrens 	blkbuf[0] = '\0';
1175b24ab676SJeff Bonwick 	for (int i = 0; i < ndvas; i++)
117643466aaeSMax Grossman 		(void) snprintf(blkbuf + strlen(blkbuf),
117743466aaeSMax Grossman 		    buflen - strlen(blkbuf), "%llu:%llx:%llx ",
117844cd46caSbillm 		    (u_longlong_t)DVA_GET_VDEV(&dva[i]),
117944cd46caSbillm 		    (u_longlong_t)DVA_GET_OFFSET(&dva[i]),
118044cd46caSbillm 		    (u_longlong_t)DVA_GET_ASIZE(&dva[i]));
118144cd46caSbillm 
118243466aaeSMax Grossman 	if (BP_IS_HOLE(bp)) {
118343466aaeSMax Grossman 		(void) snprintf(blkbuf + strlen(blkbuf),
118470163ac5SPrakash Surya 		    buflen - strlen(blkbuf),
118570163ac5SPrakash Surya 		    "%llxL B=%llu",
118670163ac5SPrakash Surya 		    (u_longlong_t)BP_GET_LSIZE(bp),
118743466aaeSMax Grossman 		    (u_longlong_t)bp->blk_birth);
118843466aaeSMax Grossman 	} else {
118943466aaeSMax Grossman 		(void) snprintf(blkbuf + strlen(blkbuf),
119043466aaeSMax Grossman 		    buflen - strlen(blkbuf),
119143466aaeSMax Grossman 		    "%llxL/%llxP F=%llu B=%llu/%llu",
119243466aaeSMax Grossman 		    (u_longlong_t)BP_GET_LSIZE(bp),
119343466aaeSMax Grossman 		    (u_longlong_t)BP_GET_PSIZE(bp),
11945d7b4d43SMatthew Ahrens 		    (u_longlong_t)BP_GET_FILL(bp),
119543466aaeSMax Grossman 		    (u_longlong_t)bp->blk_birth,
119643466aaeSMax Grossman 		    (u_longlong_t)BP_PHYSICAL_BIRTH(bp));
119743466aaeSMax Grossman 	}
119844cd46caSbillm }
119944cd46caSbillm 
120088b7b0f2SMatthew Ahrens static void
12017802d7bfSMatthew Ahrens print_indirect(blkptr_t *bp, const zbookmark_phys_t *zb,
120288b7b0f2SMatthew Ahrens     const dnode_phys_t *dnp)
1203fa9e4066Sahrens {
120488b7b0f2SMatthew Ahrens 	char blkbuf[BP_SPRINTF_LEN];
1205fa9e4066Sahrens 	int l;
1206fa9e4066Sahrens 
12075d7b4d43SMatthew Ahrens 	if (!BP_IS_EMBEDDED(bp)) {
12085d7b4d43SMatthew Ahrens 		ASSERT3U(BP_GET_TYPE(bp), ==, dnp->dn_type);
12095d7b4d43SMatthew Ahrens 		ASSERT3U(BP_GET_LEVEL(bp), ==, zb->zb_level);
12105d7b4d43SMatthew Ahrens 	}
1211fa9e4066Sahrens 
1212b24ab676SJeff Bonwick 	(void) printf("%16llx ", (u_longlong_t)blkid2offset(dnp, bp, zb));
1213fa9e4066Sahrens 
1214fa9e4066Sahrens 	ASSERT(zb->zb_level >= 0);
1215fa9e4066Sahrens 
1216fa9e4066Sahrens 	for (l = dnp->dn_nlevels - 1; l >= -1; l--) {
1217fa9e4066Sahrens 		if (l == zb->zb_level) {
121888b7b0f2SMatthew Ahrens 			(void) printf("L%llx", (u_longlong_t)zb->zb_level);
1219fa9e4066Sahrens 		} else {
122088b7b0f2SMatthew Ahrens 			(void) printf(" ");
1221fa9e4066Sahrens 		}
1222fa9e4066Sahrens 	}
1223fa9e4066Sahrens 
122443466aaeSMax Grossman 	snprintf_blkptr_compact(blkbuf, sizeof (blkbuf), bp);
122588b7b0f2SMatthew Ahrens 	(void) printf("%s\n", blkbuf);
122688b7b0f2SMatthew Ahrens }
122788b7b0f2SMatthew Ahrens 
122888b7b0f2SMatthew Ahrens static int
122988b7b0f2SMatthew Ahrens visit_indirect(spa_t *spa, const dnode_phys_t *dnp,
12307802d7bfSMatthew Ahrens     blkptr_t *bp, const zbookmark_phys_t *zb)
123188b7b0f2SMatthew Ahrens {
1232e4161df6SVictor Latushkin 	int err = 0;
123388b7b0f2SMatthew Ahrens 
123488b7b0f2SMatthew Ahrens 	if (bp->blk_birth == 0)
123588b7b0f2SMatthew Ahrens 		return (0);
123688b7b0f2SMatthew Ahrens 
123788b7b0f2SMatthew Ahrens 	print_indirect(bp, zb, dnp);
123888b7b0f2SMatthew Ahrens 
123943466aaeSMax Grossman 	if (BP_GET_LEVEL(bp) > 0 && !BP_IS_HOLE(bp)) {
12407adb730bSGeorge Wilson 		arc_flags_t flags = ARC_FLAG_WAIT;
124188b7b0f2SMatthew Ahrens 		int i;
124288b7b0f2SMatthew Ahrens 		blkptr_t *cbp;
124388b7b0f2SMatthew Ahrens 		int epb = BP_GET_LSIZE(bp) >> SPA_BLKPTRSHIFT;
124488b7b0f2SMatthew Ahrens 		arc_buf_t *buf;
124588b7b0f2SMatthew Ahrens 		uint64_t fill = 0;
124688b7b0f2SMatthew Ahrens 
12471b912ec7SGeorge Wilson 		err = arc_read(NULL, spa, bp, arc_getbuf_func, &buf,
124888b7b0f2SMatthew Ahrens 		    ZIO_PRIORITY_ASYNC_READ, ZIO_FLAG_CANFAIL, &flags, zb);
124988b7b0f2SMatthew Ahrens 		if (err)
125088b7b0f2SMatthew Ahrens 			return (err);
12513f9d6ad7SLin Ling 		ASSERT(buf->b_data);
125288b7b0f2SMatthew Ahrens 
125388b7b0f2SMatthew Ahrens 		/* recursively visit blocks below this */
125488b7b0f2SMatthew Ahrens 		cbp = buf->b_data;
125588b7b0f2SMatthew Ahrens 		for (i = 0; i < epb; i++, cbp++) {
12567802d7bfSMatthew Ahrens 			zbookmark_phys_t czb;
125788b7b0f2SMatthew Ahrens 
125888b7b0f2SMatthew Ahrens 			SET_BOOKMARK(&czb, zb->zb_objset, zb->zb_object,
125988b7b0f2SMatthew Ahrens 			    zb->zb_level - 1,
126088b7b0f2SMatthew Ahrens 			    zb->zb_blkid * epb + i);
126188b7b0f2SMatthew Ahrens 			err = visit_indirect(spa, dnp, cbp, &czb);
126288b7b0f2SMatthew Ahrens 			if (err)
126388b7b0f2SMatthew Ahrens 				break;
12645d7b4d43SMatthew Ahrens 			fill += BP_GET_FILL(cbp);
126588b7b0f2SMatthew Ahrens 		}
12668ad4d6ddSJeff Bonwick 		if (!err)
12675d7b4d43SMatthew Ahrens 			ASSERT3U(fill, ==, BP_GET_FILL(bp));
126888b7b0f2SMatthew Ahrens 		(void) arc_buf_remove_ref(buf, &buf);
1269fa9e4066Sahrens 	}
1270fa9e4066Sahrens 
127188b7b0f2SMatthew Ahrens 	return (err);
1272fa9e4066Sahrens }
1273fa9e4066Sahrens 
1274fa9e4066Sahrens /*ARGSUSED*/
1275fa9e4066Sahrens static void
127688b7b0f2SMatthew Ahrens dump_indirect(dnode_t *dn)
1277fa9e4066Sahrens {
127888b7b0f2SMatthew Ahrens 	dnode_phys_t *dnp = dn->dn_phys;
127988b7b0f2SMatthew Ahrens 	int j;
12807802d7bfSMatthew Ahrens 	zbookmark_phys_t czb;
1281fa9e4066Sahrens 
1282fa9e4066Sahrens 	(void) printf("Indirect blocks:\n");
1283fa9e4066Sahrens 
1284503ad85cSMatthew Ahrens 	SET_BOOKMARK(&czb, dmu_objset_id(dn->dn_objset),
128588b7b0f2SMatthew Ahrens 	    dn->dn_object, dnp->dn_nlevels - 1, 0);
128688b7b0f2SMatthew Ahrens 	for (j = 0; j < dnp->dn_nblkptr; j++) {
128788b7b0f2SMatthew Ahrens 		czb.zb_blkid = j;
1288503ad85cSMatthew Ahrens 		(void) visit_indirect(dmu_objset_spa(dn->dn_objset), dnp,
128988b7b0f2SMatthew Ahrens 		    &dnp->dn_blkptr[j], &czb);
129088b7b0f2SMatthew Ahrens 	}
1291fa9e4066Sahrens 
1292fa9e4066Sahrens 	(void) printf("\n");
1293fa9e4066Sahrens }
1294fa9e4066Sahrens 
1295fa9e4066Sahrens /*ARGSUSED*/
1296fa9e4066Sahrens static void
1297fa9e4066Sahrens dump_dsl_dir(objset_t *os, uint64_t object, void *data, size_t size)
1298fa9e4066Sahrens {
1299fa9e4066Sahrens 	dsl_dir_phys_t *dd = data;
1300fa9e4066Sahrens 	time_t crtime;
13013f9d6ad7SLin Ling 	char nice[32];
1302fa9e4066Sahrens 
1303fa9e4066Sahrens 	if (dd == NULL)
1304fa9e4066Sahrens 		return;
1305fa9e4066Sahrens 
1306da6c28aaSamw 	ASSERT3U(size, >=, sizeof (dsl_dir_phys_t));
1307fa9e4066Sahrens 
1308fa9e4066Sahrens 	crtime = dd->dd_creation_time;
1309fa9e4066Sahrens 	(void) printf("\t\tcreation_time = %s", ctime(&crtime));
1310fa9e4066Sahrens 	(void) printf("\t\thead_dataset_obj = %llu\n",
1311fa9e4066Sahrens 	    (u_longlong_t)dd->dd_head_dataset_obj);
1312fa9e4066Sahrens 	(void) printf("\t\tparent_dir_obj = %llu\n",
1313fa9e4066Sahrens 	    (u_longlong_t)dd->dd_parent_obj);
13143cb34c60Sahrens 	(void) printf("\t\torigin_obj = %llu\n",
13153cb34c60Sahrens 	    (u_longlong_t)dd->dd_origin_obj);
1316fa9e4066Sahrens 	(void) printf("\t\tchild_dir_zapobj = %llu\n",
1317fa9e4066Sahrens 	    (u_longlong_t)dd->dd_child_dir_zapobj);
13183f9d6ad7SLin Ling 	zdb_nicenum(dd->dd_used_bytes, nice);
131974e7dc98SMatthew Ahrens 	(void) printf("\t\tused_bytes = %s\n", nice);
13203f9d6ad7SLin Ling 	zdb_nicenum(dd->dd_compressed_bytes, nice);
132174e7dc98SMatthew Ahrens 	(void) printf("\t\tcompressed_bytes = %s\n", nice);
13223f9d6ad7SLin Ling 	zdb_nicenum(dd->dd_uncompressed_bytes, nice);
132374e7dc98SMatthew Ahrens 	(void) printf("\t\tuncompressed_bytes = %s\n", nice);
13243f9d6ad7SLin Ling 	zdb_nicenum(dd->dd_quota, nice);
132574e7dc98SMatthew Ahrens 	(void) printf("\t\tquota = %s\n", nice);
13263f9d6ad7SLin Ling 	zdb_nicenum(dd->dd_reserved, nice);
132774e7dc98SMatthew Ahrens 	(void) printf("\t\treserved = %s\n", nice);
1328fa9e4066Sahrens 	(void) printf("\t\tprops_zapobj = %llu\n",
1329fa9e4066Sahrens 	    (u_longlong_t)dd->dd_props_zapobj);
1330ecd6cf80Smarks 	(void) printf("\t\tdeleg_zapobj = %llu\n",
1331ecd6cf80Smarks 	    (u_longlong_t)dd->dd_deleg_zapobj);
133274e7dc98SMatthew Ahrens 	(void) printf("\t\tflags = %llx\n",
133374e7dc98SMatthew Ahrens 	    (u_longlong_t)dd->dd_flags);
133474e7dc98SMatthew Ahrens 
133574e7dc98SMatthew Ahrens #define	DO(which) \
13363f9d6ad7SLin Ling 	zdb_nicenum(dd->dd_used_breakdown[DD_USED_ ## which], nice); \
133774e7dc98SMatthew Ahrens 	(void) printf("\t\tused_breakdown[" #which "] = %s\n", nice)
133874e7dc98SMatthew Ahrens 	DO(HEAD);
133974e7dc98SMatthew Ahrens 	DO(SNAP);
134074e7dc98SMatthew Ahrens 	DO(CHILD);
134174e7dc98SMatthew Ahrens 	DO(CHILD_RSRV);
134274e7dc98SMatthew Ahrens 	DO(REFRSRV);
134374e7dc98SMatthew Ahrens #undef DO
1344fa9e4066Sahrens }
1345fa9e4066Sahrens 
1346fa9e4066Sahrens /*ARGSUSED*/
1347fa9e4066Sahrens static void
1348fa9e4066Sahrens dump_dsl_dataset(objset_t *os, uint64_t object, void *data, size_t size)
1349fa9e4066Sahrens {
1350fa9e4066Sahrens 	dsl_dataset_phys_t *ds = data;
1351fa9e4066Sahrens 	time_t crtime;
13523f9d6ad7SLin Ling 	char used[32], compressed[32], uncompressed[32], unique[32];
1353fbabab8fSmaybee 	char blkbuf[BP_SPRINTF_LEN];
1354fa9e4066Sahrens 
1355fa9e4066Sahrens 	if (ds == NULL)
1356fa9e4066Sahrens 		return;
1357fa9e4066Sahrens 
1358fa9e4066Sahrens 	ASSERT(size == sizeof (*ds));
1359fa9e4066Sahrens 	crtime = ds->ds_creation_time;
1360ad135b5dSChristopher Siden 	zdb_nicenum(ds->ds_referenced_bytes, used);
13613f9d6ad7SLin Ling 	zdb_nicenum(ds->ds_compressed_bytes, compressed);
13623f9d6ad7SLin Ling 	zdb_nicenum(ds->ds_uncompressed_bytes, uncompressed);
13633f9d6ad7SLin Ling 	zdb_nicenum(ds->ds_unique_bytes, unique);
136443466aaeSMax Grossman 	snprintf_blkptr(blkbuf, sizeof (blkbuf), &ds->ds_bp);
1365fa9e4066Sahrens 
1366088f3894Sahrens 	(void) printf("\t\tdir_obj = %llu\n",
1367fa9e4066Sahrens 	    (u_longlong_t)ds->ds_dir_obj);
1368fa9e4066Sahrens 	(void) printf("\t\tprev_snap_obj = %llu\n",
1369fa9e4066Sahrens 	    (u_longlong_t)ds->ds_prev_snap_obj);
1370fa9e4066Sahrens 	(void) printf("\t\tprev_snap_txg = %llu\n",
1371fa9e4066Sahrens 	    (u_longlong_t)ds->ds_prev_snap_txg);
1372fa9e4066Sahrens 	(void) printf("\t\tnext_snap_obj = %llu\n",
1373fa9e4066Sahrens 	    (u_longlong_t)ds->ds_next_snap_obj);
1374fa9e4066Sahrens 	(void) printf("\t\tsnapnames_zapobj = %llu\n",
1375fa9e4066Sahrens 	    (u_longlong_t)ds->ds_snapnames_zapobj);
1376fa9e4066Sahrens 	(void) printf("\t\tnum_children = %llu\n",
1377fa9e4066Sahrens 	    (u_longlong_t)ds->ds_num_children);
1378842727c2SChris Kirby 	(void) printf("\t\tuserrefs_obj = %llu\n",
1379842727c2SChris Kirby 	    (u_longlong_t)ds->ds_userrefs_obj);
1380fa9e4066Sahrens 	(void) printf("\t\tcreation_time = %s", ctime(&crtime));
1381fa9e4066Sahrens 	(void) printf("\t\tcreation_txg = %llu\n",
1382fa9e4066Sahrens 	    (u_longlong_t)ds->ds_creation_txg);
1383fa9e4066Sahrens 	(void) printf("\t\tdeadlist_obj = %llu\n",
1384fa9e4066Sahrens 	    (u_longlong_t)ds->ds_deadlist_obj);
1385fa9e4066Sahrens 	(void) printf("\t\tused_bytes = %s\n", used);
1386fa9e4066Sahrens 	(void) printf("\t\tcompressed_bytes = %s\n", compressed);
1387fa9e4066Sahrens 	(void) printf("\t\tuncompressed_bytes = %s\n", uncompressed);
1388fa9e4066Sahrens 	(void) printf("\t\tunique = %s\n", unique);
1389fa9e4066Sahrens 	(void) printf("\t\tfsid_guid = %llu\n",
1390fa9e4066Sahrens 	    (u_longlong_t)ds->ds_fsid_guid);
1391fa9e4066Sahrens 	(void) printf("\t\tguid = %llu\n",
1392fa9e4066Sahrens 	    (u_longlong_t)ds->ds_guid);
139399653d4eSeschrock 	(void) printf("\t\tflags = %llx\n",
139499653d4eSeschrock 	    (u_longlong_t)ds->ds_flags);
1395088f3894Sahrens 	(void) printf("\t\tnext_clones_obj = %llu\n",
1396088f3894Sahrens 	    (u_longlong_t)ds->ds_next_clones_obj);
1397bb0ade09Sahrens 	(void) printf("\t\tprops_obj = %llu\n",
1398bb0ade09Sahrens 	    (u_longlong_t)ds->ds_props_obj);
1399fa9e4066Sahrens 	(void) printf("\t\tbp = %s\n", blkbuf);
1400fa9e4066Sahrens }
1401fa9e4066Sahrens 
1402ad135b5dSChristopher Siden /* ARGSUSED */
1403ad135b5dSChristopher Siden static int
1404ad135b5dSChristopher Siden dump_bptree_cb(void *arg, const blkptr_t *bp, dmu_tx_t *tx)
1405ad135b5dSChristopher Siden {
1406ad135b5dSChristopher Siden 	char blkbuf[BP_SPRINTF_LEN];
1407ad135b5dSChristopher Siden 
1408ad135b5dSChristopher Siden 	if (bp->blk_birth != 0) {
140943466aaeSMax Grossman 		snprintf_blkptr(blkbuf, sizeof (blkbuf), bp);
1410ad135b5dSChristopher Siden 		(void) printf("\t%s\n", blkbuf);
1411ad135b5dSChristopher Siden 	}
1412ad135b5dSChristopher Siden 	return (0);
1413ad135b5dSChristopher Siden }
1414ad135b5dSChristopher Siden 
1415ad135b5dSChristopher Siden static void
1416ad135b5dSChristopher Siden dump_bptree(objset_t *os, uint64_t obj, char *name)
1417ad135b5dSChristopher Siden {
1418ad135b5dSChristopher Siden 	char bytes[32];
1419ad135b5dSChristopher Siden 	bptree_phys_t *bt;
1420ad135b5dSChristopher Siden 	dmu_buf_t *db;
1421ad135b5dSChristopher Siden 
1422ad135b5dSChristopher Siden 	if (dump_opt['d'] < 3)
1423ad135b5dSChristopher Siden 		return;
1424ad135b5dSChristopher Siden 
1425b420f3adSRichard Lowe 	VERIFY3U(0, ==, dmu_bonus_hold(os, obj, FTAG, &db));
1426ad135b5dSChristopher Siden 	bt = db->db_data;
1427ad135b5dSChristopher Siden 	zdb_nicenum(bt->bt_bytes, bytes);
1428ad135b5dSChristopher Siden 	(void) printf("\n    %s: %llu datasets, %s\n",
1429ad135b5dSChristopher Siden 	    name, (unsigned long long)(bt->bt_end - bt->bt_begin), bytes);
1430ad135b5dSChristopher Siden 	dmu_buf_rele(db, FTAG);
1431ad135b5dSChristopher Siden 
1432ad135b5dSChristopher Siden 	if (dump_opt['d'] < 5)
1433ad135b5dSChristopher Siden 		return;
1434ad135b5dSChristopher Siden 
1435ad135b5dSChristopher Siden 	(void) printf("\n");
1436ad135b5dSChristopher Siden 
1437ad135b5dSChristopher Siden 	(void) bptree_iterate(os, obj, B_FALSE, dump_bptree_cb, NULL, NULL);
1438ad135b5dSChristopher Siden }
1439ad135b5dSChristopher Siden 
1440cde58dbcSMatthew Ahrens /* ARGSUSED */
1441cde58dbcSMatthew Ahrens static int
1442cde58dbcSMatthew Ahrens dump_bpobj_cb(void *arg, const blkptr_t *bp, dmu_tx_t *tx)
1443cde58dbcSMatthew Ahrens {
1444cde58dbcSMatthew Ahrens 	char blkbuf[BP_SPRINTF_LEN];
1445cde58dbcSMatthew Ahrens 
1446cde58dbcSMatthew Ahrens 	ASSERT(bp->blk_birth != 0);
144743466aaeSMax Grossman 	snprintf_blkptr_compact(blkbuf, sizeof (blkbuf), bp);
1448cde58dbcSMatthew Ahrens 	(void) printf("\t%s\n", blkbuf);
1449cde58dbcSMatthew Ahrens 	return (0);
1450cde58dbcSMatthew Ahrens }
1451cde58dbcSMatthew Ahrens 
1452fa9e4066Sahrens static void
1453732885fcSMatthew Ahrens dump_full_bpobj(bpobj_t *bpo, char *name, int indent)
1454fa9e4066Sahrens {
14553f9d6ad7SLin Ling 	char bytes[32];
14563f9d6ad7SLin Ling 	char comp[32];
14573f9d6ad7SLin Ling 	char uncomp[32];
1458fa9e4066Sahrens 
1459fa9e4066Sahrens 	if (dump_opt['d'] < 3)
1460fa9e4066Sahrens 		return;
1461fa9e4066Sahrens 
1462cde58dbcSMatthew Ahrens 	zdb_nicenum(bpo->bpo_phys->bpo_bytes, bytes);
1463d0475637SMatthew Ahrens 	if (bpo->bpo_havesubobj && bpo->bpo_phys->bpo_subobjs != 0) {
1464cde58dbcSMatthew Ahrens 		zdb_nicenum(bpo->bpo_phys->bpo_comp, comp);
1465cde58dbcSMatthew Ahrens 		zdb_nicenum(bpo->bpo_phys->bpo_uncomp, uncomp);
1466d0475637SMatthew Ahrens 		(void) printf("    %*s: object %llu, %llu local blkptrs, "
1467732885fcSMatthew Ahrens 		    "%llu subobjs in object %llu, %s (%s/%s comp)\n",
1468d0475637SMatthew Ahrens 		    indent * 8, name,
1469d0475637SMatthew Ahrens 		    (u_longlong_t)bpo->bpo_object,
1470d0475637SMatthew Ahrens 		    (u_longlong_t)bpo->bpo_phys->bpo_num_blkptrs,
1471cde58dbcSMatthew Ahrens 		    (u_longlong_t)bpo->bpo_phys->bpo_num_subobjs,
1472732885fcSMatthew Ahrens 		    (u_longlong_t)bpo->bpo_phys->bpo_subobjs,
147399653d4eSeschrock 		    bytes, comp, uncomp);
1474d0475637SMatthew Ahrens 
1475d0475637SMatthew Ahrens 		for (uint64_t i = 0; i < bpo->bpo_phys->bpo_num_subobjs; i++) {
1476d0475637SMatthew Ahrens 			uint64_t subobj;
1477d0475637SMatthew Ahrens 			bpobj_t subbpo;
1478d0475637SMatthew Ahrens 			int error;
1479d0475637SMatthew Ahrens 			VERIFY0(dmu_read(bpo->bpo_os,
1480d0475637SMatthew Ahrens 			    bpo->bpo_phys->bpo_subobjs,
1481d0475637SMatthew Ahrens 			    i * sizeof (subobj), sizeof (subobj), &subobj, 0));
1482d0475637SMatthew Ahrens 			error = bpobj_open(&subbpo, bpo->bpo_os, subobj);
1483d0475637SMatthew Ahrens 			if (error != 0) {
1484d0475637SMatthew Ahrens 				(void) printf("ERROR %u while trying to open "
1485d0475637SMatthew Ahrens 				    "subobj id %llu\n",
1486d0475637SMatthew Ahrens 				    error, (u_longlong_t)subobj);
1487d0475637SMatthew Ahrens 				continue;
1488d0475637SMatthew Ahrens 			}
1489732885fcSMatthew Ahrens 			dump_full_bpobj(&subbpo, "subobj", indent + 1);
149077061867SMatthew Ahrens 			bpobj_close(&subbpo);
1491d0475637SMatthew Ahrens 		}
149299653d4eSeschrock 	} else {
1493d0475637SMatthew Ahrens 		(void) printf("    %*s: object %llu, %llu blkptrs, %s\n",
1494d0475637SMatthew Ahrens 		    indent * 8, name,
1495d0475637SMatthew Ahrens 		    (u_longlong_t)bpo->bpo_object,
1496d0475637SMatthew Ahrens 		    (u_longlong_t)bpo->bpo_phys->bpo_num_blkptrs,
1497d0475637SMatthew Ahrens 		    bytes);
149899653d4eSeschrock 	}
1499fa9e4066Sahrens 
1500cde58dbcSMatthew Ahrens 	if (dump_opt['d'] < 5)
1501fa9e4066Sahrens 		return;
1502fa9e4066Sahrens 
1503fa9e4066Sahrens 
1504d0475637SMatthew Ahrens 	if (indent == 0) {
1505d0475637SMatthew Ahrens 		(void) bpobj_iterate_nofree(bpo, dump_bpobj_cb, NULL, NULL);
1506d0475637SMatthew Ahrens 		(void) printf("\n");
1507d0475637SMatthew Ahrens 	}
1508cde58dbcSMatthew Ahrens }
150944cd46caSbillm 
1510cde58dbcSMatthew Ahrens static void
1511cde58dbcSMatthew Ahrens dump_deadlist(dsl_deadlist_t *dl)
1512cde58dbcSMatthew Ahrens {
1513cde58dbcSMatthew Ahrens 	dsl_deadlist_entry_t *dle;
1514d0475637SMatthew Ahrens 	uint64_t unused;
1515cde58dbcSMatthew Ahrens 	char bytes[32];
1516cde58dbcSMatthew Ahrens 	char comp[32];
1517cde58dbcSMatthew Ahrens 	char uncomp[32];
1518cde58dbcSMatthew Ahrens 
1519cde58dbcSMatthew Ahrens 	if (dump_opt['d'] < 3)
1520cde58dbcSMatthew Ahrens 		return;
1521cde58dbcSMatthew Ahrens 
152290c76c66SMatthew Ahrens 	if (dl->dl_oldfmt) {
1523732885fcSMatthew Ahrens 		dump_full_bpobj(&dl->dl_bpobj, "old-format deadlist", 0);
152490c76c66SMatthew Ahrens 		return;
152590c76c66SMatthew Ahrens 	}
152690c76c66SMatthew Ahrens 
1527cde58dbcSMatthew Ahrens 	zdb_nicenum(dl->dl_phys->dl_used, bytes);
1528cde58dbcSMatthew Ahrens 	zdb_nicenum(dl->dl_phys->dl_comp, comp);
1529cde58dbcSMatthew Ahrens 	zdb_nicenum(dl->dl_phys->dl_uncomp, uncomp);
1530cde58dbcSMatthew Ahrens 	(void) printf("\n    Deadlist: %s (%s/%s comp)\n",
1531cde58dbcSMatthew Ahrens 	    bytes, comp, uncomp);
1532cde58dbcSMatthew Ahrens 
1533cde58dbcSMatthew Ahrens 	if (dump_opt['d'] < 4)
1534cde58dbcSMatthew Ahrens 		return;
1535cde58dbcSMatthew Ahrens 
1536cde58dbcSMatthew Ahrens 	(void) printf("\n");
1537cde58dbcSMatthew Ahrens 
1538d0475637SMatthew Ahrens 	/* force the tree to be loaded */
1539d0475637SMatthew Ahrens 	dsl_deadlist_space_range(dl, 0, UINT64_MAX, &unused, &unused, &unused);
1540d0475637SMatthew Ahrens 
1541cde58dbcSMatthew Ahrens 	for (dle = avl_first(&dl->dl_tree); dle;
1542cde58dbcSMatthew Ahrens 	    dle = AVL_NEXT(&dl->dl_tree, dle)) {
1543d0475637SMatthew Ahrens 		if (dump_opt['d'] >= 5) {
1544d0475637SMatthew Ahrens 			char buf[128];
1545d0475637SMatthew Ahrens 			(void) snprintf(buf, sizeof (buf), "mintxg %llu -> ",
1546d0475637SMatthew Ahrens 			    (longlong_t)dle->dle_mintxg,
1547d0475637SMatthew Ahrens 			    (longlong_t)dle->dle_bpobj.bpo_object);
1548fa9e4066Sahrens 
1549732885fcSMatthew Ahrens 			dump_full_bpobj(&dle->dle_bpobj, buf, 0);
1550d0475637SMatthew Ahrens 		} else {
1551d0475637SMatthew Ahrens 			(void) printf("mintxg %llu -> obj %llu\n",
1552d0475637SMatthew Ahrens 			    (longlong_t)dle->dle_mintxg,
1553d0475637SMatthew Ahrens 			    (longlong_t)dle->dle_bpobj.bpo_object);
1554d0475637SMatthew Ahrens 
1555d0475637SMatthew Ahrens 		}
1556cde58dbcSMatthew Ahrens 	}
1557fa9e4066Sahrens }
1558fa9e4066Sahrens 
1559e0d35c44Smarks static avl_tree_t idx_tree;
1560e0d35c44Smarks static avl_tree_t domain_tree;
1561e0d35c44Smarks static boolean_t fuid_table_loaded;
15620a586ceaSMark Shellenbaum static boolean_t sa_loaded;
15630a586ceaSMark Shellenbaum sa_attr_type_t *sa_attr_table;
1564e0d35c44Smarks 
1565e0d35c44Smarks static void
1566e0d35c44Smarks fuid_table_destroy()
1567e0d35c44Smarks {
1568e0d35c44Smarks 	if (fuid_table_loaded) {
1569e0d35c44Smarks 		zfs_fuid_table_destroy(&idx_tree, &domain_tree);
1570e0d35c44Smarks 		fuid_table_loaded = B_FALSE;
1571e0d35c44Smarks 	}
1572e0d35c44Smarks }
1573e0d35c44Smarks 
1574e0d35c44Smarks /*
1575e0d35c44Smarks  * print uid or gid information.
1576e0d35c44Smarks  * For normal POSIX id just the id is printed in decimal format.
1577e0d35c44Smarks  * For CIFS files with FUID the fuid is printed in hex followed by
1578d0475637SMatthew Ahrens  * the domain-rid string.
1579e0d35c44Smarks  */
1580e0d35c44Smarks static void
1581e0d35c44Smarks print_idstr(uint64_t id, const char *id_type)
1582e0d35c44Smarks {
1583e0d35c44Smarks 	if (FUID_INDEX(id)) {
1584e0d35c44Smarks 		char *domain;
1585e0d35c44Smarks 
1586e0d35c44Smarks 		domain = zfs_fuid_idx_domain(&idx_tree, FUID_INDEX(id));
1587e0d35c44Smarks 		(void) printf("\t%s     %llx [%s-%d]\n", id_type,
1588e0d35c44Smarks 		    (u_longlong_t)id, domain, (int)FUID_RID(id));
1589e0d35c44Smarks 	} else {
1590e0d35c44Smarks 		(void) printf("\t%s     %llu\n", id_type, (u_longlong_t)id);
1591e0d35c44Smarks 	}
1592e0d35c44Smarks 
1593e0d35c44Smarks }
1594e0d35c44Smarks 
1595e0d35c44Smarks static void
15960a586ceaSMark Shellenbaum dump_uidgid(objset_t *os, uint64_t uid, uint64_t gid)
1597e0d35c44Smarks {
1598e0d35c44Smarks 	uint32_t uid_idx, gid_idx;
1599e0d35c44Smarks 
16000a586ceaSMark Shellenbaum 	uid_idx = FUID_INDEX(uid);
16010a586ceaSMark Shellenbaum 	gid_idx = FUID_INDEX(gid);
1602e0d35c44Smarks 
1603e0d35c44Smarks 	/* Load domain table, if not already loaded */
1604e0d35c44Smarks 	if (!fuid_table_loaded && (uid_idx || gid_idx)) {
1605e0d35c44Smarks 		uint64_t fuid_obj;
1606e0d35c44Smarks 
1607e0d35c44Smarks 		/* first find the fuid object.  It lives in the master node */
1608e0d35c44Smarks 		VERIFY(zap_lookup(os, MASTER_NODE_OBJ, ZFS_FUID_TABLES,
1609e0d35c44Smarks 		    8, 1, &fuid_obj) == 0);
161089459e17SMark Shellenbaum 		zfs_fuid_avl_tree_create(&idx_tree, &domain_tree);
1611e0d35c44Smarks 		(void) zfs_fuid_table_load(os, fuid_obj,
1612e0d35c44Smarks 		    &idx_tree, &domain_tree);
1613e0d35c44Smarks 		fuid_table_loaded = B_TRUE;
1614e0d35c44Smarks 	}
1615e0d35c44Smarks 
16160a586ceaSMark Shellenbaum 	print_idstr(uid, "uid");
16170a586ceaSMark Shellenbaum 	print_idstr(gid, "gid");
1618e0d35c44Smarks }
1619e0d35c44Smarks 
1620fa9e4066Sahrens /*ARGSUSED*/
1621fa9e4066Sahrens static void
1622fa9e4066Sahrens dump_znode(objset_t *os, uint64_t object, void *data, size_t size)
1623fa9e4066Sahrens {
1624fa9e4066Sahrens 	char path[MAXPATHLEN * 2];	/* allow for xattr and failure prefix */
16250a586ceaSMark Shellenbaum 	sa_handle_t *hdl;
16260a586ceaSMark Shellenbaum 	uint64_t xattr, rdev, gen;
16270a586ceaSMark Shellenbaum 	uint64_t uid, gid, mode, fsize, parent, links;
16288f2529deSMark Shellenbaum 	uint64_t pflags;
16290a586ceaSMark Shellenbaum 	uint64_t acctm[2], modtm[2], chgtm[2], crtm[2];
16300a586ceaSMark Shellenbaum 	time_t z_crtime, z_atime, z_mtime, z_ctime;
16318f2529deSMark Shellenbaum 	sa_bulk_attr_t bulk[12];
16320a586ceaSMark Shellenbaum 	int idx = 0;
163355434c77Sek 	int error;
1634fa9e4066Sahrens 
16350a586ceaSMark Shellenbaum 	if (!sa_loaded) {
16360a586ceaSMark Shellenbaum 		uint64_t sa_attrs = 0;
16370a586ceaSMark Shellenbaum 		uint64_t version;
16380a586ceaSMark Shellenbaum 
16390a586ceaSMark Shellenbaum 		VERIFY(zap_lookup(os, MASTER_NODE_OBJ, ZPL_VERSION_STR,
16400a586ceaSMark Shellenbaum 		    8, 1, &version) == 0);
16410a586ceaSMark Shellenbaum 		if (version >= ZPL_VERSION_SA) {
16420a586ceaSMark Shellenbaum 			VERIFY(zap_lookup(os, MASTER_NODE_OBJ, ZFS_SA_ATTRS,
16430a586ceaSMark Shellenbaum 			    8, 1, &sa_attrs) == 0);
16440a586ceaSMark Shellenbaum 		}
16451d8ccc7bSMark Shellenbaum 		if ((error = sa_setup(os, sa_attrs, zfs_attr_table,
16461d8ccc7bSMark Shellenbaum 		    ZPL_END, &sa_attr_table)) != 0) {
16471d8ccc7bSMark Shellenbaum 			(void) printf("sa_setup failed errno %d, can't "
16481d8ccc7bSMark Shellenbaum 			    "display znode contents\n", error);
16491d8ccc7bSMark Shellenbaum 			return;
16501d8ccc7bSMark Shellenbaum 		}
16510a586ceaSMark Shellenbaum 		sa_loaded = B_TRUE;
16520a586ceaSMark Shellenbaum 	}
16530a586ceaSMark Shellenbaum 
16540a586ceaSMark Shellenbaum 	if (sa_handle_get(os, object, NULL, SA_HDL_PRIVATE, &hdl)) {
16550a586ceaSMark Shellenbaum 		(void) printf("Failed to get handle for SA znode\n");
16560a586ceaSMark Shellenbaum 		return;
16570a586ceaSMark Shellenbaum 	}
16580a586ceaSMark Shellenbaum 
16590a586ceaSMark Shellenbaum 	SA_ADD_BULK_ATTR(bulk, idx, sa_attr_table[ZPL_UID], NULL, &uid, 8);
16600a586ceaSMark Shellenbaum 	SA_ADD_BULK_ATTR(bulk, idx, sa_attr_table[ZPL_GID], NULL, &gid, 8);
16610a586ceaSMark Shellenbaum 	SA_ADD_BULK_ATTR(bulk, idx, sa_attr_table[ZPL_LINKS], NULL,
16620a586ceaSMark Shellenbaum 	    &links, 8);
16630a586ceaSMark Shellenbaum 	SA_ADD_BULK_ATTR(bulk, idx, sa_attr_table[ZPL_GEN], NULL, &gen, 8);
16640a586ceaSMark Shellenbaum 	SA_ADD_BULK_ATTR(bulk, idx, sa_attr_table[ZPL_MODE], NULL,
16650a586ceaSMark Shellenbaum 	    &mode, 8);
16660a586ceaSMark Shellenbaum 	SA_ADD_BULK_ATTR(bulk, idx, sa_attr_table[ZPL_PARENT],
16670a586ceaSMark Shellenbaum 	    NULL, &parent, 8);
16680a586ceaSMark Shellenbaum 	SA_ADD_BULK_ATTR(bulk, idx, sa_attr_table[ZPL_SIZE], NULL,
16690a586ceaSMark Shellenbaum 	    &fsize, 8);
16700a586ceaSMark Shellenbaum 	SA_ADD_BULK_ATTR(bulk, idx, sa_attr_table[ZPL_ATIME], NULL,
16710a586ceaSMark Shellenbaum 	    acctm, 16);
16720a586ceaSMark Shellenbaum 	SA_ADD_BULK_ATTR(bulk, idx, sa_attr_table[ZPL_MTIME], NULL,
16730a586ceaSMark Shellenbaum 	    modtm, 16);
16740a586ceaSMark Shellenbaum 	SA_ADD_BULK_ATTR(bulk, idx, sa_attr_table[ZPL_CRTIME], NULL,
16750a586ceaSMark Shellenbaum 	    crtm, 16);
16760a586ceaSMark Shellenbaum 	SA_ADD_BULK_ATTR(bulk, idx, sa_attr_table[ZPL_CTIME], NULL,
16770a586ceaSMark Shellenbaum 	    chgtm, 16);
16788f2529deSMark Shellenbaum 	SA_ADD_BULK_ATTR(bulk, idx, sa_attr_table[ZPL_FLAGS], NULL,
16798f2529deSMark Shellenbaum 	    &pflags, 8);
16800a586ceaSMark Shellenbaum 
16810a586ceaSMark Shellenbaum 	if (sa_bulk_lookup(hdl, bulk, idx)) {
16820a586ceaSMark Shellenbaum 		(void) sa_handle_destroy(hdl);
16830a586ceaSMark Shellenbaum 		return;
16840a586ceaSMark Shellenbaum 	}
1685fa9e4066Sahrens 
168655434c77Sek 	error = zfs_obj_to_path(os, object, path, sizeof (path));
168755434c77Sek 	if (error != 0) {
168855434c77Sek 		(void) snprintf(path, sizeof (path), "\?\?\?<object#%llu>",
168955434c77Sek 		    (u_longlong_t)object);
169055434c77Sek 	}
1691fa9e4066Sahrens 	if (dump_opt['d'] < 3) {
169255434c77Sek 		(void) printf("\t%s\n", path);
16930a586ceaSMark Shellenbaum 		(void) sa_handle_destroy(hdl);
1694fa9e4066Sahrens 		return;
1695fa9e4066Sahrens 	}
1696fa9e4066Sahrens 
16970a586ceaSMark Shellenbaum 	z_crtime = (time_t)crtm[0];
16980a586ceaSMark Shellenbaum 	z_atime = (time_t)acctm[0];
16990a586ceaSMark Shellenbaum 	z_mtime = (time_t)modtm[0];
17000a586ceaSMark Shellenbaum 	z_ctime = (time_t)chgtm[0];
1701fa9e4066Sahrens 
170255434c77Sek 	(void) printf("\tpath	%s\n", path);
17030a586ceaSMark Shellenbaum 	dump_uidgid(os, uid, gid);
1704fa9e4066Sahrens 	(void) printf("\tatime	%s", ctime(&z_atime));
1705fa9e4066Sahrens 	(void) printf("\tmtime	%s", ctime(&z_mtime));
1706fa9e4066Sahrens 	(void) printf("\tctime	%s", ctime(&z_ctime));
1707fa9e4066Sahrens 	(void) printf("\tcrtime	%s", ctime(&z_crtime));
17080a586ceaSMark Shellenbaum 	(void) printf("\tgen	%llu\n", (u_longlong_t)gen);
17090a586ceaSMark Shellenbaum 	(void) printf("\tmode	%llo\n", (u_longlong_t)mode);
17100a586ceaSMark Shellenbaum 	(void) printf("\tsize	%llu\n", (u_longlong_t)fsize);
17110a586ceaSMark Shellenbaum 	(void) printf("\tparent	%llu\n", (u_longlong_t)parent);
17120a586ceaSMark Shellenbaum 	(void) printf("\tlinks	%llu\n", (u_longlong_t)links);
17138f2529deSMark Shellenbaum 	(void) printf("\tpflags	%llx\n", (u_longlong_t)pflags);
17140a586ceaSMark Shellenbaum 	if (sa_lookup(hdl, sa_attr_table[ZPL_XATTR], &xattr,
17150a586ceaSMark Shellenbaum 	    sizeof (uint64_t)) == 0)
17160a586ceaSMark Shellenbaum 		(void) printf("\txattr	%llu\n", (u_longlong_t)xattr);
17170a586ceaSMark Shellenbaum 	if (sa_lookup(hdl, sa_attr_table[ZPL_RDEV], &rdev,
17180a586ceaSMark Shellenbaum 	    sizeof (uint64_t)) == 0)
17190a586ceaSMark Shellenbaum 		(void) printf("\trdev	0x%016llx\n", (u_longlong_t)rdev);
17200a586ceaSMark Shellenbaum 	sa_handle_destroy(hdl);
1721fa9e4066Sahrens }
1722fa9e4066Sahrens 
1723fa9e4066Sahrens /*ARGSUSED*/
1724fa9e4066Sahrens static void
1725fa9e4066Sahrens dump_acl(objset_t *os, uint64_t object, void *data, size_t size)
1726fa9e4066Sahrens {
1727fa9e4066Sahrens }
1728fa9e4066Sahrens 
1729fa9e4066Sahrens /*ARGSUSED*/
1730fa9e4066Sahrens static void
1731fa9e4066Sahrens dump_dmu_objset(objset_t *os, uint64_t object, void *data, size_t size)
1732fa9e4066Sahrens {
1733fa9e4066Sahrens }
1734fa9e4066Sahrens 
17356de8f417SVictor Latushkin static object_viewer_t *object_viewer[DMU_OT_NUMTYPES + 1] = {
1736fa9e4066Sahrens 	dump_none,		/* unallocated			*/
1737fa9e4066Sahrens 	dump_zap,		/* object directory		*/
1738fa9e4066Sahrens 	dump_uint64,		/* object array			*/
1739fa9e4066Sahrens 	dump_none,		/* packed nvlist		*/
1740fa9e4066Sahrens 	dump_packed_nvlist,	/* packed nvlist size		*/
1741732885fcSMatthew Ahrens 	dump_none,		/* bpobj			*/
1742732885fcSMatthew Ahrens 	dump_bpobj,		/* bpobj header			*/
1743fa9e4066Sahrens 	dump_none,		/* SPA space map header		*/
1744fa9e4066Sahrens 	dump_none,		/* SPA space map		*/
1745fa9e4066Sahrens 	dump_none,		/* ZIL intent log		*/
1746fa9e4066Sahrens 	dump_dnode,		/* DMU dnode			*/
1747fa9e4066Sahrens 	dump_dmu_objset,	/* DMU objset			*/
1748ea8dc4b6Seschrock 	dump_dsl_dir,		/* DSL directory		*/
1749fa9e4066Sahrens 	dump_zap,		/* DSL directory child map	*/
1750fa9e4066Sahrens 	dump_zap,		/* DSL dataset snap map		*/
1751fa9e4066Sahrens 	dump_zap,		/* DSL props			*/
1752fa9e4066Sahrens 	dump_dsl_dataset,	/* DSL dataset			*/
1753fa9e4066Sahrens 	dump_znode,		/* ZFS znode			*/
1754da6c28aaSamw 	dump_acl,		/* ZFS V0 ACL			*/
1755fa9e4066Sahrens 	dump_uint8,		/* ZFS plain file		*/
1756e7437265Sahrens 	dump_zpldir,		/* ZFS directory		*/
1757fa9e4066Sahrens 	dump_zap,		/* ZFS master node		*/
1758fa9e4066Sahrens 	dump_zap,		/* ZFS delete queue		*/
1759fa9e4066Sahrens 	dump_uint8,		/* zvol object			*/
1760fa9e4066Sahrens 	dump_zap,		/* zvol prop			*/
1761fa9e4066Sahrens 	dump_uint8,		/* other uint8[]		*/
1762fa9e4066Sahrens 	dump_uint64,		/* other uint64[]		*/
1763fa9e4066Sahrens 	dump_zap,		/* other ZAP			*/
1764ea8dc4b6Seschrock 	dump_zap,		/* persistent error log		*/
176506eeb2adSek 	dump_uint8,		/* SPA history			*/
17664445fffbSMatthew Ahrens 	dump_history_offsets,	/* SPA history offsets		*/
1767b1b8ab34Slling 	dump_zap,		/* Pool properties		*/
1768ecd6cf80Smarks 	dump_zap,		/* DSL permissions		*/
1769da6c28aaSamw 	dump_acl,		/* ZFS ACL			*/
1770da6c28aaSamw 	dump_uint8,		/* ZFS SYSACL			*/
1771da6c28aaSamw 	dump_none,		/* FUID nvlist			*/
1772da6c28aaSamw 	dump_packed_nvlist,	/* FUID nvlist size		*/
1773088f3894Sahrens 	dump_zap,		/* DSL dataset next clones	*/
1774088f3894Sahrens 	dump_zap,		/* DSL scrub queue		*/
177514843421SMatthew Ahrens 	dump_zap,		/* ZFS user/group used		*/
177614843421SMatthew Ahrens 	dump_zap,		/* ZFS user/group quota		*/
1777842727c2SChris Kirby 	dump_zap,		/* snapshot refcount tags	*/
1778486ae710SMatthew Ahrens 	dump_ddt_zap,		/* DDT ZAP object		*/
1779b24ab676SJeff Bonwick 	dump_zap,		/* DDT statistics		*/
17800a586ceaSMark Shellenbaum 	dump_znode,		/* SA object			*/
17810a586ceaSMark Shellenbaum 	dump_zap,		/* SA Master Node		*/
17820a586ceaSMark Shellenbaum 	dump_sa_attrs,		/* SA attribute registration	*/
17830a586ceaSMark Shellenbaum 	dump_sa_layouts,	/* SA attribute layouts		*/
17843f9d6ad7SLin Ling 	dump_zap,		/* DSL scrub translations	*/
17853f9d6ad7SLin Ling 	dump_none,		/* fake dedup BP		*/
1786cde58dbcSMatthew Ahrens 	dump_zap,		/* deadlist			*/
1787cde58dbcSMatthew Ahrens 	dump_none,		/* deadlist hdr			*/
1788cde58dbcSMatthew Ahrens 	dump_zap,		/* dsl clones			*/
1789732885fcSMatthew Ahrens 	dump_bpobj_subobjs,	/* bpobj subobjs		*/
17900a586ceaSMark Shellenbaum 	dump_unknown,		/* Unknown type, must be last	*/
1791fa9e4066Sahrens };
1792fa9e4066Sahrens 
1793fa9e4066Sahrens static void
1794fa9e4066Sahrens dump_object(objset_t *os, uint64_t object, int verbosity, int *print_header)
1795fa9e4066Sahrens {
1796fa9e4066Sahrens 	dmu_buf_t *db = NULL;
1797fa9e4066Sahrens 	dmu_object_info_t doi;
1798fa9e4066Sahrens 	dnode_t *dn;
1799fa9e4066Sahrens 	void *bonus = NULL;
1800fa9e4066Sahrens 	size_t bsize = 0;
18013f9d6ad7SLin Ling 	char iblk[32], dblk[32], lsize[32], asize[32], fill[32];
18023f9d6ad7SLin Ling 	char bonus_size[32];
1803fa9e4066Sahrens 	char aux[50];
1804fa9e4066Sahrens 	int error;
1805fa9e4066Sahrens 
1806fa9e4066Sahrens 	if (*print_header) {
1807b24ab676SJeff Bonwick 		(void) printf("\n%10s  %3s  %5s  %5s  %5s  %5s  %6s  %s\n",
1808b24ab676SJeff Bonwick 		    "Object", "lvl", "iblk", "dblk", "dsize", "lsize",
1809b24ab676SJeff Bonwick 		    "%full", "type");
1810fa9e4066Sahrens 		*print_header = 0;
1811fa9e4066Sahrens 	}
1812fa9e4066Sahrens 
1813fa9e4066Sahrens 	if (object == 0) {
1814744947dcSTom Erickson 		dn = DMU_META_DNODE(os);
1815fa9e4066Sahrens 	} else {
1816ea8dc4b6Seschrock 		error = dmu_bonus_hold(os, object, FTAG, &db);
1817ea8dc4b6Seschrock 		if (error)
1818ea8dc4b6Seschrock 			fatal("dmu_bonus_hold(%llu) failed, errno %u",
1819ea8dc4b6Seschrock 			    object, error);
1820fa9e4066Sahrens 		bonus = db->db_data;
1821fa9e4066Sahrens 		bsize = db->db_size;
1822744947dcSTom Erickson 		dn = DB_DNODE((dmu_buf_impl_t *)db);
1823fa9e4066Sahrens 	}
1824fa9e4066Sahrens 	dmu_object_info_from_dnode(dn, &doi);
1825fa9e4066Sahrens 
18263f9d6ad7SLin Ling 	zdb_nicenum(doi.doi_metadata_block_size, iblk);
18273f9d6ad7SLin Ling 	zdb_nicenum(doi.doi_data_block_size, dblk);
18283f9d6ad7SLin Ling 	zdb_nicenum(doi.doi_max_offset, lsize);
18293f9d6ad7SLin Ling 	zdb_nicenum(doi.doi_physical_blocks_512 << 9, asize);
18303f9d6ad7SLin Ling 	zdb_nicenum(doi.doi_bonus_size, bonus_size);
1831b24ab676SJeff Bonwick 	(void) sprintf(fill, "%6.2f", 100.0 * doi.doi_fill_count *
1832bbfd46c4SJeff Bonwick 	    doi.doi_data_block_size / (object == 0 ? DNODES_PER_BLOCK : 1) /
1833bbfd46c4SJeff Bonwick 	    doi.doi_max_offset);
1834fa9e4066Sahrens 
1835fa9e4066Sahrens 	aux[0] = '\0';
1836fa9e4066Sahrens 
1837e7437265Sahrens 	if (doi.doi_checksum != ZIO_CHECKSUM_INHERIT || verbosity >= 6) {
1838fa9e4066Sahrens 		(void) snprintf(aux + strlen(aux), sizeof (aux), " (K=%s)",
18396de8f417SVictor Latushkin 		    ZDB_CHECKSUM_NAME(doi.doi_checksum));
1840e7437265Sahrens 	}
1841fa9e4066Sahrens 
1842e7437265Sahrens 	if (doi.doi_compress != ZIO_COMPRESS_INHERIT || verbosity >= 6) {
1843fa9e4066Sahrens 		(void) snprintf(aux + strlen(aux), sizeof (aux), " (Z=%s)",
18446de8f417SVictor Latushkin 		    ZDB_COMPRESS_NAME(doi.doi_compress));
1845e7437265Sahrens 	}
1846fa9e4066Sahrens 
1847b24ab676SJeff Bonwick 	(void) printf("%10lld  %3u  %5s  %5s  %5s  %5s  %6s  %s%s\n",
1848b24ab676SJeff Bonwick 	    (u_longlong_t)object, doi.doi_indirection, iblk, dblk,
1849b24ab676SJeff Bonwick 	    asize, lsize, fill, ZDB_OT_NAME(doi.doi_type), aux);
1850fa9e4066Sahrens 
1851fa9e4066Sahrens 	if (doi.doi_bonus_type != DMU_OT_NONE && verbosity > 3) {
1852b24ab676SJeff Bonwick 		(void) printf("%10s  %3s  %5s  %5s  %5s  %5s  %6s  %s\n",
1853b24ab676SJeff Bonwick 		    "", "", "", "", "", bonus_size, "bonus",
18546de8f417SVictor Latushkin 		    ZDB_OT_NAME(doi.doi_bonus_type));
1855fa9e4066Sahrens 	}
1856fa9e4066Sahrens 
1857fa9e4066Sahrens 	if (verbosity >= 4) {
18580a586ceaSMark Shellenbaum 		(void) printf("\tdnode flags: %s%s%s\n",
185914843421SMatthew Ahrens 		    (dn->dn_phys->dn_flags & DNODE_FLAG_USED_BYTES) ?
186014843421SMatthew Ahrens 		    "USED_BYTES " : "",
186114843421SMatthew Ahrens 		    (dn->dn_phys->dn_flags & DNODE_FLAG_USERUSED_ACCOUNTED) ?
18620a586ceaSMark Shellenbaum 		    "USERUSED_ACCOUNTED " : "",
18630a586ceaSMark Shellenbaum 		    (dn->dn_phys->dn_flags & DNODE_FLAG_SPILL_BLKPTR) ?
18640a586ceaSMark Shellenbaum 		    "SPILL_BLKPTR" : "");
186514843421SMatthew Ahrens 		(void) printf("\tdnode maxblkid: %llu\n",
186614843421SMatthew Ahrens 		    (longlong_t)dn->dn_phys->dn_maxblkid);
186714843421SMatthew Ahrens 
18686de8f417SVictor Latushkin 		object_viewer[ZDB_OT_TYPE(doi.doi_bonus_type)](os, object,
18696de8f417SVictor Latushkin 		    bonus, bsize);
18706de8f417SVictor Latushkin 		object_viewer[ZDB_OT_TYPE(doi.doi_type)](os, object, NULL, 0);
1871fa9e4066Sahrens 		*print_header = 1;
1872fa9e4066Sahrens 	}
1873fa9e4066Sahrens 
1874fa9e4066Sahrens 	if (verbosity >= 5)
187588b7b0f2SMatthew Ahrens 		dump_indirect(dn);
1876fa9e4066Sahrens 
1877fa9e4066Sahrens 	if (verbosity >= 5) {
1878fa9e4066Sahrens 		/*
1879fa9e4066Sahrens 		 * Report the list of segments that comprise the object.
1880fa9e4066Sahrens 		 */
1881fa9e4066Sahrens 		uint64_t start = 0;
1882fa9e4066Sahrens 		uint64_t end;
1883fa9e4066Sahrens 		uint64_t blkfill = 1;
1884fa9e4066Sahrens 		int minlvl = 1;
1885fa9e4066Sahrens 
1886fa9e4066Sahrens 		if (dn->dn_type == DMU_OT_DNODE) {
1887fa9e4066Sahrens 			minlvl = 0;
1888fa9e4066Sahrens 			blkfill = DNODES_PER_BLOCK;
1889fa9e4066Sahrens 		}
1890fa9e4066Sahrens 
1891fa9e4066Sahrens 		for (;;) {
18923f9d6ad7SLin Ling 			char segsize[32];
1893cdb0ab79Smaybee 			error = dnode_next_offset(dn,
1894cdb0ab79Smaybee 			    0, &start, minlvl, blkfill, 0);
1895fa9e4066Sahrens 			if (error)
1896fa9e4066Sahrens 				break;
1897fa9e4066Sahrens 			end = start;
1898cdb0ab79Smaybee 			error = dnode_next_offset(dn,
1899cdb0ab79Smaybee 			    DNODE_FIND_HOLE, &end, minlvl, blkfill, 0);
19003f9d6ad7SLin Ling 			zdb_nicenum(end - start, segsize);
1901fa9e4066Sahrens 			(void) printf("\t\tsegment [%016llx, %016llx)"
1902fa9e4066Sahrens 			    " size %5s\n", (u_longlong_t)start,
1903fa9e4066Sahrens 			    (u_longlong_t)end, segsize);
1904fa9e4066Sahrens 			if (error)
1905fa9e4066Sahrens 				break;
1906fa9e4066Sahrens 			start = end;
1907fa9e4066Sahrens 		}
1908fa9e4066Sahrens 	}
1909fa9e4066Sahrens 
1910fa9e4066Sahrens 	if (db != NULL)
1911ea8dc4b6Seschrock 		dmu_buf_rele(db, FTAG);
1912fa9e4066Sahrens }
1913fa9e4066Sahrens 
1914fa9e4066Sahrens static char *objset_types[DMU_OST_NUMTYPES] = {
1915fa9e4066Sahrens 	"NONE", "META", "ZPL", "ZVOL", "OTHER", "ANY" };
1916fa9e4066Sahrens 
1917fa9e4066Sahrens static void
1918fa9e4066Sahrens dump_dir(objset_t *os)
1919fa9e4066Sahrens {
1920fa9e4066Sahrens 	dmu_objset_stats_t dds;
1921fa9e4066Sahrens 	uint64_t object, object_count;
1922a2eea2e1Sahrens 	uint64_t refdbytes, usedobjs, scratch;
19233f9d6ad7SLin Ling 	char numbuf[32];
192414843421SMatthew Ahrens 	char blkbuf[BP_SPRINTF_LEN + 20];
1925fa9e4066Sahrens 	char osname[MAXNAMELEN];
1926fa9e4066Sahrens 	char *type = "UNKNOWN";
1927fa9e4066Sahrens 	int verbosity = dump_opt['d'];
1928fa9e4066Sahrens 	int print_header = 1;
1929fa9e4066Sahrens 	int i, error;
1930fa9e4066Sahrens 
19313b2aab18SMatthew Ahrens 	dsl_pool_config_enter(dmu_objset_pool(os), FTAG);
1932a2eea2e1Sahrens 	dmu_objset_fast_stat(os, &dds);
19333b2aab18SMatthew Ahrens 	dsl_pool_config_exit(dmu_objset_pool(os), FTAG);
1934fa9e4066Sahrens 
1935fa9e4066Sahrens 	if (dds.dds_type < DMU_OST_NUMTYPES)
1936fa9e4066Sahrens 		type = objset_types[dds.dds_type];
1937fa9e4066Sahrens 
1938fa9e4066Sahrens 	if (dds.dds_type == DMU_OST_META) {
1939fa9e4066Sahrens 		dds.dds_creation_txg = TXG_INITIAL;
19405d7b4d43SMatthew Ahrens 		usedobjs = BP_GET_FILL(os->os_rootbp);
1941c1379625SJustin T. Gibbs 		refdbytes = dsl_dir_phys(os->os_spa->spa_dsl_pool->dp_mos_dir)->
1942c1379625SJustin T. Gibbs 		    dd_used_bytes;
1943a2eea2e1Sahrens 	} else {
1944a2eea2e1Sahrens 		dmu_objset_space(os, &refdbytes, &scratch, &usedobjs, &scratch);
1945fa9e4066Sahrens 	}
1946fa9e4066Sahrens 
19475d7b4d43SMatthew Ahrens 	ASSERT3U(usedobjs, ==, BP_GET_FILL(os->os_rootbp));
1948fa9e4066Sahrens 
19493f9d6ad7SLin Ling 	zdb_nicenum(refdbytes, numbuf);
1950fa9e4066Sahrens 
1951fa9e4066Sahrens 	if (verbosity >= 4) {
195243466aaeSMax Grossman 		(void) snprintf(blkbuf, sizeof (blkbuf), ", rootbp ");
195343466aaeSMax Grossman 		(void) snprintf_blkptr(blkbuf + strlen(blkbuf),
195443466aaeSMax Grossman 		    sizeof (blkbuf) - strlen(blkbuf), os->os_rootbp);
1955fa9e4066Sahrens 	} else {
1956fa9e4066Sahrens 		blkbuf[0] = '\0';
1957fa9e4066Sahrens 	}
1958fa9e4066Sahrens 
1959fa9e4066Sahrens 	dmu_objset_name(os, osname);
1960fa9e4066Sahrens 
1961a2eea2e1Sahrens 	(void) printf("Dataset %s [%s], ID %llu, cr_txg %llu, "
1962fa9e4066Sahrens 	    "%s, %llu objects%s\n",
1963fa9e4066Sahrens 	    osname, type, (u_longlong_t)dmu_objset_id(os),
1964fa9e4066Sahrens 	    (u_longlong_t)dds.dds_creation_txg,
1965a2eea2e1Sahrens 	    numbuf, (u_longlong_t)usedobjs, blkbuf);
1966fa9e4066Sahrens 
1967b24ab676SJeff Bonwick 	if (zopt_objects != 0) {
1968b24ab676SJeff Bonwick 		for (i = 0; i < zopt_objects; i++)
1969b24ab676SJeff Bonwick 			dump_object(os, zopt_object[i], verbosity,
1970b24ab676SJeff Bonwick 			    &print_header);
1971b24ab676SJeff Bonwick 		(void) printf("\n");
1972b24ab676SJeff Bonwick 		return;
1973b24ab676SJeff Bonwick 	}
1974b24ab676SJeff Bonwick 
1975b24ab676SJeff Bonwick 	if (dump_opt['i'] != 0 || verbosity >= 2)
1976b24ab676SJeff Bonwick 		dump_intent_log(dmu_objset_zil(os));
1977fa9e4066Sahrens 
1978fa9e4066Sahrens 	if (dmu_objset_ds(os) != NULL)
1979cde58dbcSMatthew Ahrens 		dump_deadlist(&dmu_objset_ds(os)->ds_deadlist);
1980fa9e4066Sahrens 
1981fa9e4066Sahrens 	if (verbosity < 2)
1982fa9e4066Sahrens 		return;
1983fa9e4066Sahrens 
198443466aaeSMax Grossman 	if (BP_IS_HOLE(os->os_rootbp))
1985088f3894Sahrens 		return;
1986088f3894Sahrens 
1987fa9e4066Sahrens 	dump_object(os, 0, verbosity, &print_header);
198814843421SMatthew Ahrens 	object_count = 0;
1989744947dcSTom Erickson 	if (DMU_USERUSED_DNODE(os) != NULL &&
1990744947dcSTom Erickson 	    DMU_USERUSED_DNODE(os)->dn_type != 0) {
199114843421SMatthew Ahrens 		dump_object(os, DMU_USERUSED_OBJECT, verbosity, &print_header);
199214843421SMatthew Ahrens 		dump_object(os, DMU_GROUPUSED_OBJECT, verbosity, &print_header);
199314843421SMatthew Ahrens 	}
1994fa9e4066Sahrens 
1995fa9e4066Sahrens 	object = 0;
19966754306eSahrens 	while ((error = dmu_object_next(os, &object, B_FALSE, 0)) == 0) {
1997fa9e4066Sahrens 		dump_object(os, object, verbosity, &print_header);
1998fa9e4066Sahrens 		object_count++;
1999fa9e4066Sahrens 	}
2000fa9e4066Sahrens 
2001a2eea2e1Sahrens 	ASSERT3U(object_count, ==, usedobjs);
2002fa9e4066Sahrens 
2003fa9e4066Sahrens 	(void) printf("\n");
2004fa9e4066Sahrens 
2005ccba0801SRich Morris 	if (error != ESRCH) {
2006ccba0801SRich Morris 		(void) fprintf(stderr, "dmu_object_next() = %d\n", error);
2007ccba0801SRich Morris 		abort();
2008ccba0801SRich Morris 	}
2009fa9e4066Sahrens }
2010fa9e4066Sahrens 
2011fa9e4066Sahrens static void
201253b9a4a9SVictor Latushkin dump_uberblock(uberblock_t *ub, const char *header, const char *footer)
2013fa9e4066Sahrens {
2014fa9e4066Sahrens 	time_t timestamp = ub->ub_timestamp;
2015fa9e4066Sahrens 
201653b9a4a9SVictor Latushkin 	(void) printf(header ? header : "");
2017fa9e4066Sahrens 	(void) printf("\tmagic = %016llx\n", (u_longlong_t)ub->ub_magic);
2018fa9e4066Sahrens 	(void) printf("\tversion = %llu\n", (u_longlong_t)ub->ub_version);
2019fa9e4066Sahrens 	(void) printf("\ttxg = %llu\n", (u_longlong_t)ub->ub_txg);
2020fa9e4066Sahrens 	(void) printf("\tguid_sum = %llu\n", (u_longlong_t)ub->ub_guid_sum);
2021fa9e4066Sahrens 	(void) printf("\ttimestamp = %llu UTC = %s",
2022fa9e4066Sahrens 	    (u_longlong_t)ub->ub_timestamp, asctime(localtime(&timestamp)));
2023fa9e4066Sahrens 	if (dump_opt['u'] >= 3) {
2024fbabab8fSmaybee 		char blkbuf[BP_SPRINTF_LEN];
202543466aaeSMax Grossman 		snprintf_blkptr(blkbuf, sizeof (blkbuf), &ub->ub_rootbp);
2026fa9e4066Sahrens 		(void) printf("\trootbp = %s\n", blkbuf);
2027fa9e4066Sahrens 	}
202853b9a4a9SVictor Latushkin 	(void) printf(footer ? footer : "");
2029fa9e4066Sahrens }
2030fa9e4066Sahrens 
2031fa9e4066Sahrens static void
203207428bdfSVictor Latushkin dump_config(spa_t *spa)
2033fa9e4066Sahrens {
203407428bdfSVictor Latushkin 	dmu_buf_t *db;
203507428bdfSVictor Latushkin 	size_t nvsize = 0;
203607428bdfSVictor Latushkin 	int error = 0;
203707428bdfSVictor Latushkin 
2038fa9e4066Sahrens 
203907428bdfSVictor Latushkin 	error = dmu_bonus_hold(spa->spa_meta_objset,
204007428bdfSVictor Latushkin 	    spa->spa_config_object, FTAG, &db);
204107428bdfSVictor Latushkin 
204207428bdfSVictor Latushkin 	if (error == 0) {
204307428bdfSVictor Latushkin 		nvsize = *(uint64_t *)db->db_data;
204407428bdfSVictor Latushkin 		dmu_buf_rele(db, FTAG);
204507428bdfSVictor Latushkin 
204607428bdfSVictor Latushkin 		(void) printf("\nMOS Configuration:\n");
204707428bdfSVictor Latushkin 		dump_packed_nvlist(spa->spa_meta_objset,
204807428bdfSVictor Latushkin 		    spa->spa_config_object, (void *)&nvsize, 1);
204907428bdfSVictor Latushkin 	} else {
205007428bdfSVictor Latushkin 		(void) fprintf(stderr, "dmu_bonus_hold(%llu) failed, errno %d",
205107428bdfSVictor Latushkin 		    (u_longlong_t)spa->spa_config_object, error);
2052fa9e4066Sahrens 	}
2053fa9e4066Sahrens }
2054fa9e4066Sahrens 
2055c5904d13Seschrock static void
2056c5904d13Seschrock dump_cachefile(const char *cachefile)
2057c5904d13Seschrock {
2058c5904d13Seschrock 	int fd;
2059c5904d13Seschrock 	struct stat64 statbuf;
2060c5904d13Seschrock 	char *buf;
2061c5904d13Seschrock 	nvlist_t *config;
2062c5904d13Seschrock 
2063c5904d13Seschrock 	if ((fd = open64(cachefile, O_RDONLY)) < 0) {
2064c5904d13Seschrock 		(void) printf("cannot open '%s': %s\n", cachefile,
2065c5904d13Seschrock 		    strerror(errno));
2066c5904d13Seschrock 		exit(1);
2067c5904d13Seschrock 	}
2068c5904d13Seschrock 
2069c5904d13Seschrock 	if (fstat64(fd, &statbuf) != 0) {
2070c5904d13Seschrock 		(void) printf("failed to stat '%s': %s\n", cachefile,
2071c5904d13Seschrock 		    strerror(errno));
2072c5904d13Seschrock 		exit(1);
2073c5904d13Seschrock 	}
2074c5904d13Seschrock 
2075c5904d13Seschrock 	if ((buf = malloc(statbuf.st_size)) == NULL) {
2076c5904d13Seschrock 		(void) fprintf(stderr, "failed to allocate %llu bytes\n",
2077c5904d13Seschrock 		    (u_longlong_t)statbuf.st_size);
2078c5904d13Seschrock 		exit(1);
2079c5904d13Seschrock 	}
2080c5904d13Seschrock 
2081c5904d13Seschrock 	if (read(fd, buf, statbuf.st_size) != statbuf.st_size) {
2082c5904d13Seschrock 		(void) fprintf(stderr, "failed to read %llu bytes\n",
2083c5904d13Seschrock 		    (u_longlong_t)statbuf.st_size);
2084c5904d13Seschrock 		exit(1);
2085c5904d13Seschrock 	}
2086c5904d13Seschrock 
2087c5904d13Seschrock 	(void) close(fd);
2088c5904d13Seschrock 
2089c5904d13Seschrock 	if (nvlist_unpack(buf, statbuf.st_size, &config, 0) != 0) {
2090c5904d13Seschrock 		(void) fprintf(stderr, "failed to unpack nvlist\n");
2091c5904d13Seschrock 		exit(1);
2092c5904d13Seschrock 	}
2093c5904d13Seschrock 
2094c5904d13Seschrock 	free(buf);
2095c5904d13Seschrock 
2096c5904d13Seschrock 	dump_nvlist(config, 0);
2097c5904d13Seschrock 
2098c5904d13Seschrock 	nvlist_free(config);
2099c5904d13Seschrock }
2100c5904d13Seschrock 
210153b9a4a9SVictor Latushkin #define	ZDB_MAX_UB_HEADER_SIZE 32
210253b9a4a9SVictor Latushkin 
210353b9a4a9SVictor Latushkin static void
210453b9a4a9SVictor Latushkin dump_label_uberblocks(vdev_label_t *lbl, uint64_t ashift)
210553b9a4a9SVictor Latushkin {
210653b9a4a9SVictor Latushkin 	vdev_t vd;
210753b9a4a9SVictor Latushkin 	vdev_t *vdp = &vd;
210853b9a4a9SVictor Latushkin 	char header[ZDB_MAX_UB_HEADER_SIZE];
210953b9a4a9SVictor Latushkin 
211053b9a4a9SVictor Latushkin 	vd.vdev_ashift = ashift;
211153b9a4a9SVictor Latushkin 	vdp->vdev_top = vdp;
211253b9a4a9SVictor Latushkin 
211353b9a4a9SVictor Latushkin 	for (int i = 0; i < VDEV_UBERBLOCK_COUNT(vdp); i++) {
211453b9a4a9SVictor Latushkin 		uint64_t uoff = VDEV_UBERBLOCK_OFFSET(vdp, i);
211553b9a4a9SVictor Latushkin 		uberblock_t *ub = (void *)((char *)lbl + uoff);
211653b9a4a9SVictor Latushkin 
211753b9a4a9SVictor Latushkin 		if (uberblock_verify(ub))
211853b9a4a9SVictor Latushkin 			continue;
211953b9a4a9SVictor Latushkin 		(void) snprintf(header, ZDB_MAX_UB_HEADER_SIZE,
212053b9a4a9SVictor Latushkin 		    "Uberblock[%d]\n", i);
212153b9a4a9SVictor Latushkin 		dump_uberblock(ub, header, "");
212253b9a4a9SVictor Latushkin 	}
212353b9a4a9SVictor Latushkin }
212453b9a4a9SVictor Latushkin 
2125fa9e4066Sahrens static void
2126fa9e4066Sahrens dump_label(const char *dev)
2127fa9e4066Sahrens {
2128fa9e4066Sahrens 	int fd;
2129fa9e4066Sahrens 	vdev_label_t label;
2130c6065d0fSGeorge Wilson 	char *path, *buf = label.vl_vdev_phys.vp_nvlist;
2131fa9e4066Sahrens 	size_t buflen = sizeof (label.vl_vdev_phys.vp_nvlist);
2132fa9e4066Sahrens 	struct stat64 statbuf;
213353b9a4a9SVictor Latushkin 	uint64_t psize, ashift;
2134c6065d0fSGeorge Wilson 	int len = strlen(dev) + 1;
2135fa9e4066Sahrens 
2136c6065d0fSGeorge Wilson 	if (strncmp(dev, "/dev/dsk/", 9) == 0) {
2137c6065d0fSGeorge Wilson 		len++;
2138c6065d0fSGeorge Wilson 		path = malloc(len);
2139c6065d0fSGeorge Wilson 		(void) snprintf(path, len, "%s%s", "/dev/rdsk/", dev + 9);
2140c6065d0fSGeorge Wilson 	} else {
2141c6065d0fSGeorge Wilson 		path = strdup(dev);
2142c6065d0fSGeorge Wilson 	}
2143c6065d0fSGeorge Wilson 
2144c6065d0fSGeorge Wilson 	if ((fd = open64(path, O_RDONLY)) < 0) {
2145c6065d0fSGeorge Wilson 		(void) printf("cannot open '%s': %s\n", path, strerror(errno));
2146c6065d0fSGeorge Wilson 		free(path);
2147fa9e4066Sahrens 		exit(1);
2148fa9e4066Sahrens 	}
2149fa9e4066Sahrens 
2150fa9e4066Sahrens 	if (fstat64(fd, &statbuf) != 0) {
2151c6065d0fSGeorge Wilson 		(void) printf("failed to stat '%s': %s\n", path,
2152fa9e4066Sahrens 		    strerror(errno));
2153c6065d0fSGeorge Wilson 		free(path);
2154c6065d0fSGeorge Wilson 		(void) close(fd);
2155c6065d0fSGeorge Wilson 		exit(1);
2156c6065d0fSGeorge Wilson 	}
2157c6065d0fSGeorge Wilson 
2158c6065d0fSGeorge Wilson 	if (S_ISBLK(statbuf.st_mode)) {
2159c6065d0fSGeorge Wilson 		(void) printf("cannot use '%s': character device required\n",
2160c6065d0fSGeorge Wilson 		    path);
2161c6065d0fSGeorge Wilson 		free(path);
2162c6065d0fSGeorge Wilson 		(void) close(fd);
2163c6065d0fSGeorge Wilson 		exit(1);
2164fa9e4066Sahrens 	}
2165fa9e4066Sahrens 
2166fa9e4066Sahrens 	psize = statbuf.st_size;
2167fa9e4066Sahrens 	psize = P2ALIGN(psize, (uint64_t)sizeof (vdev_label_t));
2168fa9e4066Sahrens 
216953b9a4a9SVictor Latushkin 	for (int l = 0; l < VDEV_LABELS; l++) {
2170fa9e4066Sahrens 		nvlist_t *config = NULL;
2171fa9e4066Sahrens 
2172fa9e4066Sahrens 		(void) printf("--------------------------------------------\n");
2173fa9e4066Sahrens 		(void) printf("LABEL %d\n", l);
2174fa9e4066Sahrens 		(void) printf("--------------------------------------------\n");
2175fa9e4066Sahrens 
21760d981225Seschrock 		if (pread64(fd, &label, sizeof (label),
2177fa9e4066Sahrens 		    vdev_label_offset(psize, l, 0)) != sizeof (label)) {
2178fa9e4066Sahrens 			(void) printf("failed to read label %d\n", l);
2179fa9e4066Sahrens 			continue;
2180fa9e4066Sahrens 		}
2181fa9e4066Sahrens 
2182fa9e4066Sahrens 		if (nvlist_unpack(buf, buflen, &config, 0) != 0) {
2183fa9e4066Sahrens 			(void) printf("failed to unpack label %d\n", l);
218453b9a4a9SVictor Latushkin 			ashift = SPA_MINBLOCKSHIFT;
218553b9a4a9SVictor Latushkin 		} else {
218653b9a4a9SVictor Latushkin 			nvlist_t *vdev_tree = NULL;
218753b9a4a9SVictor Latushkin 
218853b9a4a9SVictor Latushkin 			dump_nvlist(config, 4);
218953b9a4a9SVictor Latushkin 			if ((nvlist_lookup_nvlist(config,
219053b9a4a9SVictor Latushkin 			    ZPOOL_CONFIG_VDEV_TREE, &vdev_tree) != 0) ||
219153b9a4a9SVictor Latushkin 			    (nvlist_lookup_uint64(vdev_tree,
219253b9a4a9SVictor Latushkin 			    ZPOOL_CONFIG_ASHIFT, &ashift) != 0))
219353b9a4a9SVictor Latushkin 				ashift = SPA_MINBLOCKSHIFT;
219453b9a4a9SVictor Latushkin 			nvlist_free(config);
2195fa9e4066Sahrens 		}
219653b9a4a9SVictor Latushkin 		if (dump_opt['u'])
219753b9a4a9SVictor Latushkin 			dump_label_uberblocks(&label, ashift);
2198fa9e4066Sahrens 	}
2199c6065d0fSGeorge Wilson 
2200c6065d0fSGeorge Wilson 	free(path);
2201c6065d0fSGeorge Wilson 	(void) close(fd);
2202fa9e4066Sahrens }
2203fa9e4066Sahrens 
2204ca0cc391SMatthew Ahrens static uint64_t dataset_feature_count[SPA_FEATURES];
2205b5152584SMatthew Ahrens 
2206fa9e4066Sahrens /*ARGSUSED*/
22071d452cf5Sahrens static int
2208fd136879SMatthew Ahrens dump_one_dir(const char *dsname, void *arg)
2209fa9e4066Sahrens {
2210fa9e4066Sahrens 	int error;
2211fa9e4066Sahrens 	objset_t *os;
2212fa9e4066Sahrens 
2213503ad85cSMatthew Ahrens 	error = dmu_objset_own(dsname, DMU_OST_ANY, B_TRUE, FTAG, &os);
2214fa9e4066Sahrens 	if (error) {
2215b24ab676SJeff Bonwick 		(void) printf("Could not open %s, error %d\n", dsname, error);
22161d452cf5Sahrens 		return (0);
2217fa9e4066Sahrens 	}
2218ca0cc391SMatthew Ahrens 
2219ca0cc391SMatthew Ahrens 	for (spa_feature_t f = 0; f < SPA_FEATURES; f++) {
2220ca0cc391SMatthew Ahrens 		if (!dmu_objset_ds(os)->ds_feature_inuse[f])
2221ca0cc391SMatthew Ahrens 			continue;
2222ca0cc391SMatthew Ahrens 		ASSERT(spa_feature_table[f].fi_flags &
2223ca0cc391SMatthew Ahrens 		    ZFEATURE_FLAG_PER_DATASET);
2224ca0cc391SMatthew Ahrens 		dataset_feature_count[f]++;
2225ca0cc391SMatthew Ahrens 	}
2226ca0cc391SMatthew Ahrens 
2227fa9e4066Sahrens 	dump_dir(os);
2228503ad85cSMatthew Ahrens 	dmu_objset_disown(os, FTAG);
2229e0d35c44Smarks 	fuid_table_destroy();
22300a586ceaSMark Shellenbaum 	sa_loaded = B_FALSE;
22311d452cf5Sahrens 	return (0);
2232fa9e4066Sahrens }
2233fa9e4066Sahrens 
2234fa9e4066Sahrens /*
2235b24ab676SJeff Bonwick  * Block statistics.
2236fa9e4066Sahrens  */
2237b5152584SMatthew Ahrens #define	PSIZE_HISTO_SIZE (SPA_OLD_MAXBLOCKSIZE / SPA_MINBLOCKSIZE + 2)
2238fa9e4066Sahrens typedef struct zdb_blkstats {
2239490d05b9SMatthew Ahrens 	uint64_t zb_asize;
2240490d05b9SMatthew Ahrens 	uint64_t zb_lsize;
2241490d05b9SMatthew Ahrens 	uint64_t zb_psize;
2242490d05b9SMatthew Ahrens 	uint64_t zb_count;
2243d5ee8a13SMatthew Ahrens 	uint64_t zb_gangs;
2244d5ee8a13SMatthew Ahrens 	uint64_t zb_ditto_samevdev;
2245490d05b9SMatthew Ahrens 	uint64_t zb_psize_histogram[PSIZE_HISTO_SIZE];
2246fa9e4066Sahrens } zdb_blkstats_t;
2247fa9e4066Sahrens 
2248b24ab676SJeff Bonwick /*
2249b24ab676SJeff Bonwick  * Extended object types to report deferred frees and dedup auto-ditto blocks.
2250b24ab676SJeff Bonwick  */
2251b24ab676SJeff Bonwick #define	ZDB_OT_DEFERRED	(DMU_OT_NUMTYPES + 0)
2252b24ab676SJeff Bonwick #define	ZDB_OT_DITTO	(DMU_OT_NUMTYPES + 1)
2253ad135b5dSChristopher Siden #define	ZDB_OT_OTHER	(DMU_OT_NUMTYPES + 2)
2254ad135b5dSChristopher Siden #define	ZDB_OT_TOTAL	(DMU_OT_NUMTYPES + 3)
2255b24ab676SJeff Bonwick 
2256b24ab676SJeff Bonwick static char *zdb_ot_extname[] = {
2257b24ab676SJeff Bonwick 	"deferred free",
2258b24ab676SJeff Bonwick 	"dedup ditto",
2259ad135b5dSChristopher Siden 	"other",
2260b24ab676SJeff Bonwick 	"Total",
2261b24ab676SJeff Bonwick };
2262fa9e4066Sahrens 
226388b7b0f2SMatthew Ahrens #define	ZB_TOTAL	DN_MAX_LEVELS
2264fa9e4066Sahrens 
2265fa9e4066Sahrens typedef struct zdb_cb {
2266b24ab676SJeff Bonwick 	zdb_blkstats_t	zcb_type[ZB_TOTAL + 1][ZDB_OT_TOTAL + 1];
2267b24ab676SJeff Bonwick 	uint64_t	zcb_dedup_asize;
2268b24ab676SJeff Bonwick 	uint64_t	zcb_dedup_blocks;
22695d7b4d43SMatthew Ahrens 	uint64_t	zcb_embedded_blocks[NUM_BP_EMBEDDED_TYPES];
22705d7b4d43SMatthew Ahrens 	uint64_t	zcb_embedded_histogram[NUM_BP_EMBEDDED_TYPES]
22715d7b4d43SMatthew Ahrens 	    [BPE_PAYLOAD_SIZE];
2272490d05b9SMatthew Ahrens 	uint64_t	zcb_start;
2273490d05b9SMatthew Ahrens 	uint64_t	zcb_lastprint;
2274490d05b9SMatthew Ahrens 	uint64_t	zcb_totalasize;
2275fa9e4066Sahrens 	uint64_t	zcb_errors[256];
2276fa9e4066Sahrens 	int		zcb_readfails;
2277fa9e4066Sahrens 	int		zcb_haderrors;
2278cde58dbcSMatthew Ahrens 	spa_t		*zcb_spa;
2279fa9e4066Sahrens } zdb_cb_t;
2280fa9e4066Sahrens 
2281fa9e4066Sahrens static void
2282cde58dbcSMatthew Ahrens zdb_count_block(zdb_cb_t *zcb, zilog_t *zilog, const blkptr_t *bp,
2283b24ab676SJeff Bonwick     dmu_object_type_t type)
2284fa9e4066Sahrens {
2285b24ab676SJeff Bonwick 	uint64_t refcnt = 0;
2286b24ab676SJeff Bonwick 
2287b24ab676SJeff Bonwick 	ASSERT(type < ZDB_OT_TOTAL);
2288b24ab676SJeff Bonwick 
2289b24ab676SJeff Bonwick 	if (zilog && zil_bp_tree_add(zilog, bp) != 0)
2290b24ab676SJeff Bonwick 		return;
2291b24ab676SJeff Bonwick 
2292e14bb325SJeff Bonwick 	for (int i = 0; i < 4; i++) {
2293fa9e4066Sahrens 		int l = (i < 2) ? BP_GET_LEVEL(bp) : ZB_TOTAL;
2294b24ab676SJeff Bonwick 		int t = (i & 1) ? type : ZDB_OT_TOTAL;
2295d5ee8a13SMatthew Ahrens 		int equal;
2296fa9e4066Sahrens 		zdb_blkstats_t *zb = &zcb->zcb_type[l][t];
2297fa9e4066Sahrens 
2298fa9e4066Sahrens 		zb->zb_asize += BP_GET_ASIZE(bp);
2299fa9e4066Sahrens 		zb->zb_lsize += BP_GET_LSIZE(bp);
2300fa9e4066Sahrens 		zb->zb_psize += BP_GET_PSIZE(bp);
2301fa9e4066Sahrens 		zb->zb_count++;
2302b5152584SMatthew Ahrens 
2303b5152584SMatthew Ahrens 		/*
2304b5152584SMatthew Ahrens 		 * The histogram is only big enough to record blocks up to
2305b5152584SMatthew Ahrens 		 * SPA_OLD_MAXBLOCKSIZE; larger blocks go into the last,
2306b5152584SMatthew Ahrens 		 * "other", bucket.
2307b5152584SMatthew Ahrens 		 */
2308b5152584SMatthew Ahrens 		int idx = BP_GET_PSIZE(bp) >> SPA_MINBLOCKSHIFT;
2309b5152584SMatthew Ahrens 		idx = MIN(idx, SPA_OLD_MAXBLOCKSIZE / SPA_MINBLOCKSIZE + 1);
2310b5152584SMatthew Ahrens 		zb->zb_psize_histogram[idx]++;
2311d5ee8a13SMatthew Ahrens 
2312d5ee8a13SMatthew Ahrens 		zb->zb_gangs += BP_COUNT_GANG(bp);
2313d5ee8a13SMatthew Ahrens 
2314d5ee8a13SMatthew Ahrens 		switch (BP_GET_NDVAS(bp)) {
2315d5ee8a13SMatthew Ahrens 		case 2:
2316d5ee8a13SMatthew Ahrens 			if (DVA_GET_VDEV(&bp->blk_dva[0]) ==
2317d5ee8a13SMatthew Ahrens 			    DVA_GET_VDEV(&bp->blk_dva[1]))
2318d5ee8a13SMatthew Ahrens 				zb->zb_ditto_samevdev++;
2319d5ee8a13SMatthew Ahrens 			break;
2320d5ee8a13SMatthew Ahrens 		case 3:
2321d5ee8a13SMatthew Ahrens 			equal = (DVA_GET_VDEV(&bp->blk_dva[0]) ==
2322d5ee8a13SMatthew Ahrens 			    DVA_GET_VDEV(&bp->blk_dva[1])) +
2323d5ee8a13SMatthew Ahrens 			    (DVA_GET_VDEV(&bp->blk_dva[0]) ==
2324d5ee8a13SMatthew Ahrens 			    DVA_GET_VDEV(&bp->blk_dva[2])) +
2325d5ee8a13SMatthew Ahrens 			    (DVA_GET_VDEV(&bp->blk_dva[1]) ==
2326d5ee8a13SMatthew Ahrens 			    DVA_GET_VDEV(&bp->blk_dva[2]));
2327d5ee8a13SMatthew Ahrens 			if (equal != 0)
2328d5ee8a13SMatthew Ahrens 				zb->zb_ditto_samevdev++;
2329d5ee8a13SMatthew Ahrens 			break;
2330d5ee8a13SMatthew Ahrens 		}
2331d5ee8a13SMatthew Ahrens 
2332fa9e4066Sahrens 	}
2333fa9e4066Sahrens 
23345d7b4d43SMatthew Ahrens 	if (BP_IS_EMBEDDED(bp)) {
23355d7b4d43SMatthew Ahrens 		zcb->zcb_embedded_blocks[BPE_GET_ETYPE(bp)]++;
23365d7b4d43SMatthew Ahrens 		zcb->zcb_embedded_histogram[BPE_GET_ETYPE(bp)]
23375d7b4d43SMatthew Ahrens 		    [BPE_GET_PSIZE(bp)]++;
23385d7b4d43SMatthew Ahrens 		return;
23395d7b4d43SMatthew Ahrens 	}
23405d7b4d43SMatthew Ahrens 
2341b24ab676SJeff Bonwick 	if (dump_opt['L'])
2342b24ab676SJeff Bonwick 		return;
2343b24ab676SJeff Bonwick 
2344b24ab676SJeff Bonwick 	if (BP_GET_DEDUP(bp)) {
2345b24ab676SJeff Bonwick 		ddt_t *ddt;
2346b24ab676SJeff Bonwick 		ddt_entry_t *dde;
2347b24ab676SJeff Bonwick 
2348cde58dbcSMatthew Ahrens 		ddt = ddt_select(zcb->zcb_spa, bp);
2349b24ab676SJeff Bonwick 		ddt_enter(ddt);
2350b24ab676SJeff Bonwick 		dde = ddt_lookup(ddt, bp, B_FALSE);
2351b24ab676SJeff Bonwick 
2352b24ab676SJeff Bonwick 		if (dde == NULL) {
2353b24ab676SJeff Bonwick 			refcnt = 0;
2354b24ab676SJeff Bonwick 		} else {
2355b24ab676SJeff Bonwick 			ddt_phys_t *ddp = ddt_phys_select(dde, bp);
2356b24ab676SJeff Bonwick 			ddt_phys_decref(ddp);
2357b24ab676SJeff Bonwick 			refcnt = ddp->ddp_refcnt;
2358b24ab676SJeff Bonwick 			if (ddt_phys_total_refcnt(dde) == 0)
2359b24ab676SJeff Bonwick 				ddt_remove(ddt, dde);
2360d41e7643Sek 		}
2361b24ab676SJeff Bonwick 		ddt_exit(ddt);
2362d41e7643Sek 	}
2363d41e7643Sek 
2364cde58dbcSMatthew Ahrens 	VERIFY3U(zio_wait(zio_claim(NULL, zcb->zcb_spa,
2365cde58dbcSMatthew Ahrens 	    refcnt ? 0 : spa_first_txg(zcb->zcb_spa),
2366b24ab676SJeff Bonwick 	    bp, NULL, NULL, ZIO_FLAG_CANFAIL)), ==, 0);
2367fa9e4066Sahrens }
2368fa9e4066Sahrens 
236931d7e8faSGeorge Wilson static void
237031d7e8faSGeorge Wilson zdb_blkptr_done(zio_t *zio)
237131d7e8faSGeorge Wilson {
237231d7e8faSGeorge Wilson 	spa_t *spa = zio->io_spa;
237331d7e8faSGeorge Wilson 	blkptr_t *bp = zio->io_bp;
237431d7e8faSGeorge Wilson 	int ioerr = zio->io_error;
237531d7e8faSGeorge Wilson 	zdb_cb_t *zcb = zio->io_private;
23767802d7bfSMatthew Ahrens 	zbookmark_phys_t *zb = &zio->io_bookmark;
237731d7e8faSGeorge Wilson 
237831d7e8faSGeorge Wilson 	zio_data_buf_free(zio->io_data, zio->io_size);
237931d7e8faSGeorge Wilson 
238031d7e8faSGeorge Wilson 	mutex_enter(&spa->spa_scrub_lock);
238131d7e8faSGeorge Wilson 	spa->spa_scrub_inflight--;
238231d7e8faSGeorge Wilson 	cv_broadcast(&spa->spa_scrub_io_cv);
238331d7e8faSGeorge Wilson 
238431d7e8faSGeorge Wilson 	if (ioerr && !(zio->io_flags & ZIO_FLAG_SPECULATIVE)) {
238531d7e8faSGeorge Wilson 		char blkbuf[BP_SPRINTF_LEN];
238631d7e8faSGeorge Wilson 
238731d7e8faSGeorge Wilson 		zcb->zcb_haderrors = 1;
238831d7e8faSGeorge Wilson 		zcb->zcb_errors[ioerr]++;
238931d7e8faSGeorge Wilson 
239031d7e8faSGeorge Wilson 		if (dump_opt['b'] >= 2)
239143466aaeSMax Grossman 			snprintf_blkptr(blkbuf, sizeof (blkbuf), bp);
239231d7e8faSGeorge Wilson 		else
239331d7e8faSGeorge Wilson 			blkbuf[0] = '\0';
239431d7e8faSGeorge Wilson 
239531d7e8faSGeorge Wilson 		(void) printf("zdb_blkptr_cb: "
239631d7e8faSGeorge Wilson 		    "Got error %d reading "
239731d7e8faSGeorge Wilson 		    "<%llu, %llu, %lld, %llx> %s -- skipping\n",
239831d7e8faSGeorge Wilson 		    ioerr,
239931d7e8faSGeorge Wilson 		    (u_longlong_t)zb->zb_objset,
240031d7e8faSGeorge Wilson 		    (u_longlong_t)zb->zb_object,
240131d7e8faSGeorge Wilson 		    (u_longlong_t)zb->zb_level,
240231d7e8faSGeorge Wilson 		    (u_longlong_t)zb->zb_blkid,
240331d7e8faSGeorge Wilson 		    blkbuf);
240431d7e8faSGeorge Wilson 	}
240531d7e8faSGeorge Wilson 	mutex_exit(&spa->spa_scrub_lock);
240631d7e8faSGeorge Wilson }
240731d7e8faSGeorge Wilson 
2408fa9e4066Sahrens static int
24091b912ec7SGeorge Wilson zdb_blkptr_cb(spa_t *spa, zilog_t *zilog, const blkptr_t *bp,
24107802d7bfSMatthew Ahrens     const zbookmark_phys_t *zb, const dnode_phys_t *dnp, void *arg)
2411fa9e4066Sahrens {
2412fa9e4066Sahrens 	zdb_cb_t *zcb = arg;
24136365109dSVictor Latushkin 	dmu_object_type_t type;
2414468c413aSTim Haley 	boolean_t is_metadata;
2415fa9e4066Sahrens 
2416a2cdcdd2SPaul Dagnelie 	if (bp == NULL)
2417a2cdcdd2SPaul Dagnelie 		return (0);
2418a2cdcdd2SPaul Dagnelie 
241943466aaeSMax Grossman 	if (dump_opt['b'] >= 5 && bp->blk_birth > 0) {
242043466aaeSMax Grossman 		char blkbuf[BP_SPRINTF_LEN];
242143466aaeSMax Grossman 		snprintf_blkptr(blkbuf, sizeof (blkbuf), bp);
242243466aaeSMax Grossman 		(void) printf("objset %llu object %llu "
242343466aaeSMax Grossman 		    "level %lld offset 0x%llx %s\n",
242443466aaeSMax Grossman 		    (u_longlong_t)zb->zb_objset,
242543466aaeSMax Grossman 		    (u_longlong_t)zb->zb_object,
242643466aaeSMax Grossman 		    (longlong_t)zb->zb_level,
242743466aaeSMax Grossman 		    (u_longlong_t)blkid2offset(dnp, bp, zb),
242843466aaeSMax Grossman 		    blkbuf);
242943466aaeSMax Grossman 	}
243043466aaeSMax Grossman 
243143466aaeSMax Grossman 	if (BP_IS_HOLE(bp))
243288b7b0f2SMatthew Ahrens 		return (0);
2433e14bb325SJeff Bonwick 
24346365109dSVictor Latushkin 	type = BP_GET_TYPE(bp);
2435e14bb325SJeff Bonwick 
2436ad135b5dSChristopher Siden 	zdb_count_block(zcb, zilog, bp,
2437ad135b5dSChristopher Siden 	    (type & DMU_OT_NEWTYPE) ? ZDB_OT_OTHER : type);
24386365109dSVictor Latushkin 
2439ad135b5dSChristopher Siden 	is_metadata = (BP_GET_LEVEL(bp) != 0 || DMU_OT_IS_METADATA(type));
24406365109dSVictor Latushkin 
24415d7b4d43SMatthew Ahrens 	if (!BP_IS_EMBEDDED(bp) &&
24425d7b4d43SMatthew Ahrens 	    (dump_opt['c'] > 1 || (dump_opt['c'] && is_metadata))) {
2443468c413aSTim Haley 		size_t size = BP_GET_PSIZE(bp);
244431d7e8faSGeorge Wilson 		void *data = zio_data_buf_alloc(size);
2445b24ab676SJeff Bonwick 		int flags = ZIO_FLAG_CANFAIL | ZIO_FLAG_SCRUB | ZIO_FLAG_RAW;
2446b24ab676SJeff Bonwick 
2447b24ab676SJeff Bonwick 		/* If it's an intent log block, failure is expected. */
2448b24ab676SJeff Bonwick 		if (zb->zb_level == ZB_ZIL_LEVEL)
2449b24ab676SJeff Bonwick 			flags |= ZIO_FLAG_SPECULATIVE;
2450b24ab676SJeff Bonwick 
245131d7e8faSGeorge Wilson 		mutex_enter(&spa->spa_scrub_lock);
245231d7e8faSGeorge Wilson 		while (spa->spa_scrub_inflight > max_inflight)
245331d7e8faSGeorge Wilson 			cv_wait(&spa->spa_scrub_io_cv, &spa->spa_scrub_lock);
245431d7e8faSGeorge Wilson 		spa->spa_scrub_inflight++;
245531d7e8faSGeorge Wilson 		mutex_exit(&spa->spa_scrub_lock);
2456b24ab676SJeff Bonwick 
245731d7e8faSGeorge Wilson 		zio_nowait(zio_read(NULL, spa, bp, data, size,
245831d7e8faSGeorge Wilson 		    zdb_blkptr_done, zcb, ZIO_PRIORITY_ASYNC_READ, flags, zb));
2459fa9e4066Sahrens 	}
2460fa9e4066Sahrens 
2461fa9e4066Sahrens 	zcb->zcb_readfails = 0;
2462fa9e4066Sahrens 
2463f7950bf1SMatthew Ahrens 	/* only call gethrtime() every 100 blocks */
2464f7950bf1SMatthew Ahrens 	static int iters;
2465f7950bf1SMatthew Ahrens 	if (++iters > 100)
2466f7950bf1SMatthew Ahrens 		iters = 0;
2467f7950bf1SMatthew Ahrens 	else
2468f7950bf1SMatthew Ahrens 		return (0);
2469f7950bf1SMatthew Ahrens 
2470f7950bf1SMatthew Ahrens 	if (dump_opt['b'] < 5 && gethrtime() > zcb->zcb_lastprint + NANOSEC) {
2471490d05b9SMatthew Ahrens 		uint64_t now = gethrtime();
2472490d05b9SMatthew Ahrens 		char buf[10];
2473490d05b9SMatthew Ahrens 		uint64_t bytes = zcb->zcb_type[ZB_TOTAL][ZDB_OT_TOTAL].zb_asize;
2474490d05b9SMatthew Ahrens 		int kb_per_sec =
2475490d05b9SMatthew Ahrens 		    1 + bytes / (1 + ((now - zcb->zcb_start) / 1000 / 1000));
2476490d05b9SMatthew Ahrens 		int sec_remaining =
2477490d05b9SMatthew Ahrens 		    (zcb->zcb_totalasize - bytes) / 1024 / kb_per_sec;
2478490d05b9SMatthew Ahrens 
2479490d05b9SMatthew Ahrens 		zfs_nicenum(bytes, buf, sizeof (buf));
2480490d05b9SMatthew Ahrens 		(void) fprintf(stderr,
2481490d05b9SMatthew Ahrens 		    "\r%5s completed (%4dMB/s) "
2482490d05b9SMatthew Ahrens 		    "estimated time remaining: %uhr %02umin %02usec        ",
2483490d05b9SMatthew Ahrens 		    buf, kb_per_sec / 1024,
2484490d05b9SMatthew Ahrens 		    sec_remaining / 60 / 60,
2485490d05b9SMatthew Ahrens 		    sec_remaining / 60 % 60,
2486490d05b9SMatthew Ahrens 		    sec_remaining % 60);
2487490d05b9SMatthew Ahrens 
2488490d05b9SMatthew Ahrens 		zcb->zcb_lastprint = now;
2489490d05b9SMatthew Ahrens 	}
2490490d05b9SMatthew Ahrens 
2491fa9e4066Sahrens 	return (0);
2492fa9e4066Sahrens }
2493fa9e4066Sahrens 
2494b24ab676SJeff Bonwick static void
24950713e232SGeorge Wilson zdb_leak(void *arg, uint64_t start, uint64_t size)
2496b24ab676SJeff Bonwick {
24970713e232SGeorge Wilson 	vdev_t *vd = arg;
2498b24ab676SJeff Bonwick 
2499b24ab676SJeff Bonwick 	(void) printf("leaked space: vdev %llu, offset 0x%llx, size %llu\n",
2500b24ab676SJeff Bonwick 	    (u_longlong_t)vd->vdev_id, (u_longlong_t)start, (u_longlong_t)size);
2501b24ab676SJeff Bonwick }
2502b24ab676SJeff Bonwick 
25030713e232SGeorge Wilson static metaslab_ops_t zdb_metaslab_ops = {
25042e4c9986SGeorge Wilson 	NULL	/* alloc */
2505b24ab676SJeff Bonwick };
2506b24ab676SJeff Bonwick 
2507b24ab676SJeff Bonwick static void
2508bbfd46c4SJeff Bonwick zdb_ddt_leak_init(spa_t *spa, zdb_cb_t *zcb)
2509b24ab676SJeff Bonwick {
2510bbfd46c4SJeff Bonwick 	ddt_bookmark_t ddb = { 0 };
2511b24ab676SJeff Bonwick 	ddt_entry_t dde;
2512b24ab676SJeff Bonwick 	int error;
2513b24ab676SJeff Bonwick 
2514bbfd46c4SJeff Bonwick 	while ((error = ddt_walk(spa, &ddb, &dde)) == 0) {
2515b24ab676SJeff Bonwick 		blkptr_t blk;
2516b24ab676SJeff Bonwick 		ddt_phys_t *ddp = dde.dde_phys;
2517bbfd46c4SJeff Bonwick 
2518bbfd46c4SJeff Bonwick 		if (ddb.ddb_class == DDT_CLASS_UNIQUE)
2519bbfd46c4SJeff Bonwick 			return;
2520bbfd46c4SJeff Bonwick 
2521b24ab676SJeff Bonwick 		ASSERT(ddt_phys_total_refcnt(&dde) > 1);
2522bbfd46c4SJeff Bonwick 
2523b24ab676SJeff Bonwick 		for (int p = 0; p < DDT_PHYS_TYPES; p++, ddp++) {
2524b24ab676SJeff Bonwick 			if (ddp->ddp_phys_birth == 0)
2525b24ab676SJeff Bonwick 				continue;
2526bbfd46c4SJeff Bonwick 			ddt_bp_create(ddb.ddb_checksum,
2527bbfd46c4SJeff Bonwick 			    &dde.dde_key, ddp, &blk);
2528b24ab676SJeff Bonwick 			if (p == DDT_PHYS_DITTO) {
2529cde58dbcSMatthew Ahrens 				zdb_count_block(zcb, NULL, &blk, ZDB_OT_DITTO);
2530b24ab676SJeff Bonwick 			} else {
2531b24ab676SJeff Bonwick 				zcb->zcb_dedup_asize +=
2532b24ab676SJeff Bonwick 				    BP_GET_ASIZE(&blk) * (ddp->ddp_refcnt - 1);
2533b24ab676SJeff Bonwick 				zcb->zcb_dedup_blocks++;
2534b24ab676SJeff Bonwick 			}
2535b24ab676SJeff Bonwick 		}
2536b24ab676SJeff Bonwick 		if (!dump_opt['L']) {
2537bbfd46c4SJeff Bonwick 			ddt_t *ddt = spa->spa_ddt[ddb.ddb_checksum];
2538b24ab676SJeff Bonwick 			ddt_enter(ddt);
2539b24ab676SJeff Bonwick 			VERIFY(ddt_lookup(ddt, &blk, B_TRUE) != NULL);
2540b24ab676SJeff Bonwick 			ddt_exit(ddt);
2541b24ab676SJeff Bonwick 		}
2542b24ab676SJeff Bonwick 	}
2543b24ab676SJeff Bonwick 
2544b24ab676SJeff Bonwick 	ASSERT(error == ENOENT);
2545b24ab676SJeff Bonwick }
2546b24ab676SJeff Bonwick 
2547b24ab676SJeff Bonwick static void
2548b24ab676SJeff Bonwick zdb_leak_init(spa_t *spa, zdb_cb_t *zcb)
2549b24ab676SJeff Bonwick {
2550cde58dbcSMatthew Ahrens 	zcb->zcb_spa = spa;
2551cde58dbcSMatthew Ahrens 
2552b24ab676SJeff Bonwick 	if (!dump_opt['L']) {
2553b24ab676SJeff Bonwick 		vdev_t *rvd = spa->spa_root_vdev;
255406be9802SMatthew Ahrens 		for (uint64_t c = 0; c < rvd->vdev_children; c++) {
2555b24ab676SJeff Bonwick 			vdev_t *vd = rvd->vdev_child[c];
255606be9802SMatthew Ahrens 			for (uint64_t m = 0; m < vd->vdev_ms_count; m++) {
2557b24ab676SJeff Bonwick 				metaslab_t *msp = vd->vdev_ms[m];
2558b24ab676SJeff Bonwick 				mutex_enter(&msp->ms_lock);
25590713e232SGeorge Wilson 				metaslab_unload(msp);
25600713e232SGeorge Wilson 
25610713e232SGeorge Wilson 				/*
25620713e232SGeorge Wilson 				 * For leak detection, we overload the metaslab
25630713e232SGeorge Wilson 				 * ms_tree to contain allocated segments
25640713e232SGeorge Wilson 				 * instead of free segments. As a result,
25650713e232SGeorge Wilson 				 * we can't use the normal metaslab_load/unload
25660713e232SGeorge Wilson 				 * interfaces.
25670713e232SGeorge Wilson 				 */
25680713e232SGeorge Wilson 				if (msp->ms_sm != NULL) {
256906be9802SMatthew Ahrens 					(void) fprintf(stderr,
257006be9802SMatthew Ahrens 					    "\rloading space map for "
257106be9802SMatthew Ahrens 					    "vdev %llu of %llu, "
257206be9802SMatthew Ahrens 					    "metaslab %llu of %llu ...",
257306be9802SMatthew Ahrens 					    (longlong_t)c,
257406be9802SMatthew Ahrens 					    (longlong_t)rvd->vdev_children,
257506be9802SMatthew Ahrens 					    (longlong_t)m,
257606be9802SMatthew Ahrens 					    (longlong_t)vd->vdev_ms_count);
257706be9802SMatthew Ahrens 
25780713e232SGeorge Wilson 					msp->ms_ops = &zdb_metaslab_ops;
2579f7950bf1SMatthew Ahrens 
2580f7950bf1SMatthew Ahrens 					/*
2581f7950bf1SMatthew Ahrens 					 * We don't want to spend the CPU
2582f7950bf1SMatthew Ahrens 					 * manipulating the size-ordered
2583f7950bf1SMatthew Ahrens 					 * tree, so clear the range_tree
2584f7950bf1SMatthew Ahrens 					 * ops.
2585f7950bf1SMatthew Ahrens 					 */
2586f7950bf1SMatthew Ahrens 					msp->ms_tree->rt_ops = NULL;
25870713e232SGeorge Wilson 					VERIFY0(space_map_load(msp->ms_sm,
25880713e232SGeorge Wilson 					    msp->ms_tree, SM_ALLOC));
25890713e232SGeorge Wilson 					msp->ms_loaded = B_TRUE;
25900713e232SGeorge Wilson 				}
2591b24ab676SJeff Bonwick 				mutex_exit(&msp->ms_lock);
2592b24ab676SJeff Bonwick 			}
2593b24ab676SJeff Bonwick 		}
259406be9802SMatthew Ahrens 		(void) fprintf(stderr, "\n");
2595b24ab676SJeff Bonwick 	}
2596b24ab676SJeff Bonwick 
2597b24ab676SJeff Bonwick 	spa_config_enter(spa, SCL_CONFIG, FTAG, RW_READER);
2598b24ab676SJeff Bonwick 
2599bbfd46c4SJeff Bonwick 	zdb_ddt_leak_init(spa, zcb);
2600b24ab676SJeff Bonwick 
2601b24ab676SJeff Bonwick 	spa_config_exit(spa, SCL_CONFIG, FTAG);
2602b24ab676SJeff Bonwick }
2603b24ab676SJeff Bonwick 
2604b24ab676SJeff Bonwick static void
2605b24ab676SJeff Bonwick zdb_leak_fini(spa_t *spa)
2606b24ab676SJeff Bonwick {
2607b24ab676SJeff Bonwick 	if (!dump_opt['L']) {
2608b24ab676SJeff Bonwick 		vdev_t *rvd = spa->spa_root_vdev;
2609b24ab676SJeff Bonwick 		for (int c = 0; c < rvd->vdev_children; c++) {
2610b24ab676SJeff Bonwick 			vdev_t *vd = rvd->vdev_child[c];
2611b24ab676SJeff Bonwick 			for (int m = 0; m < vd->vdev_ms_count; m++) {
2612b24ab676SJeff Bonwick 				metaslab_t *msp = vd->vdev_ms[m];
2613b24ab676SJeff Bonwick 				mutex_enter(&msp->ms_lock);
26140713e232SGeorge Wilson 
26150713e232SGeorge Wilson 				/*
26160713e232SGeorge Wilson 				 * The ms_tree has been overloaded to
26170713e232SGeorge Wilson 				 * contain allocated segments. Now that we
26180713e232SGeorge Wilson 				 * finished traversing all blocks, any
26190713e232SGeorge Wilson 				 * block that remains in the ms_tree
26200713e232SGeorge Wilson 				 * represents an allocated block that we
26210713e232SGeorge Wilson 				 * did not claim during the traversal.
26220713e232SGeorge Wilson 				 * Claimed blocks would have been removed
26230713e232SGeorge Wilson 				 * from the ms_tree.
26240713e232SGeorge Wilson 				 */
26250713e232SGeorge Wilson 				range_tree_vacate(msp->ms_tree, zdb_leak, vd);
26260713e232SGeorge Wilson 				msp->ms_loaded = B_FALSE;
26270713e232SGeorge Wilson 
2628b24ab676SJeff Bonwick 				mutex_exit(&msp->ms_lock);
2629b24ab676SJeff Bonwick 			}
2630b24ab676SJeff Bonwick 		}
2631b24ab676SJeff Bonwick 	}
2632b24ab676SJeff Bonwick }
2633b24ab676SJeff Bonwick 
2634cde58dbcSMatthew Ahrens /* ARGSUSED */
2635cde58dbcSMatthew Ahrens static int
2636cde58dbcSMatthew Ahrens count_block_cb(void *arg, const blkptr_t *bp, dmu_tx_t *tx)
2637cde58dbcSMatthew Ahrens {
2638cde58dbcSMatthew Ahrens 	zdb_cb_t *zcb = arg;
2639cde58dbcSMatthew Ahrens 
2640490d05b9SMatthew Ahrens 	if (dump_opt['b'] >= 5) {
2641cde58dbcSMatthew Ahrens 		char blkbuf[BP_SPRINTF_LEN];
264243466aaeSMax Grossman 		snprintf_blkptr(blkbuf, sizeof (blkbuf), bp);
2643cde58dbcSMatthew Ahrens 		(void) printf("[%s] %s\n",
2644cde58dbcSMatthew Ahrens 		    "deferred free", blkbuf);
2645cde58dbcSMatthew Ahrens 	}
2646cde58dbcSMatthew Ahrens 	zdb_count_block(zcb, NULL, bp, ZDB_OT_DEFERRED);
2647cde58dbcSMatthew Ahrens 	return (0);
2648cde58dbcSMatthew Ahrens }
2649cde58dbcSMatthew Ahrens 
2650fa9e4066Sahrens static int
2651fa9e4066Sahrens dump_block_stats(spa_t *spa)
2652fa9e4066Sahrens {
2653fa9e4066Sahrens 	zdb_cb_t zcb = { 0 };
2654fa9e4066Sahrens 	zdb_blkstats_t *zb, *tzb;
2655b24ab676SJeff Bonwick 	uint64_t norm_alloc, norm_space, total_alloc, total_found;
2656cd088ea4SVictor Latushkin 	int flags = TRAVERSE_PRE | TRAVERSE_PREFETCH_METADATA | TRAVERSE_HARD;
26575d7b4d43SMatthew Ahrens 	boolean_t leaks = B_FALSE;
2658fa9e4066Sahrens 
2659490d05b9SMatthew Ahrens 	(void) printf("\nTraversing all blocks %s%s%s%s%s...\n\n",
2660b24ab676SJeff Bonwick 	    (dump_opt['c'] || !dump_opt['L']) ? "to verify " : "",
2661b24ab676SJeff Bonwick 	    (dump_opt['c'] == 1) ? "metadata " : "",
2662b24ab676SJeff Bonwick 	    dump_opt['c'] ? "checksums " : "",
2663b24ab676SJeff Bonwick 	    (dump_opt['c'] && !dump_opt['L']) ? "and verify " : "",
2664b24ab676SJeff Bonwick 	    !dump_opt['L'] ? "nothing leaked " : "");
2665fa9e4066Sahrens 
2666fa9e4066Sahrens 	/*
2667e14bb325SJeff Bonwick 	 * Load all space maps as SM_ALLOC maps, then traverse the pool
2668e14bb325SJeff Bonwick 	 * claiming each block we discover.  If the pool is perfectly
2669e14bb325SJeff Bonwick 	 * consistent, the space maps will be empty when we're done.
2670e14bb325SJeff Bonwick 	 * Anything left over is a leak; any block we can't claim (because
2671e14bb325SJeff Bonwick 	 * it's not part of any space map) is a double allocation,
2672e14bb325SJeff Bonwick 	 * reference to a freed block, or an unclaimed log block.
2673fa9e4066Sahrens 	 */
2674b24ab676SJeff Bonwick 	zdb_leak_init(spa, &zcb);
2675fa9e4066Sahrens 
2676fa9e4066Sahrens 	/*
2677fa9e4066Sahrens 	 * If there's a deferred-free bplist, process that first.
2678fa9e4066Sahrens 	 */
2679cde58dbcSMatthew Ahrens 	(void) bpobj_iterate_nofree(&spa->spa_deferred_bpobj,
2680cde58dbcSMatthew Ahrens 	    count_block_cb, &zcb, NULL);
26813b2aab18SMatthew Ahrens 	if (spa_version(spa) >= SPA_VERSION_DEADLISTS) {
26823b2aab18SMatthew Ahrens 		(void) bpobj_iterate_nofree(&spa->spa_dsl_pool->dp_free_bpobj,
26833b2aab18SMatthew Ahrens 		    count_block_cb, &zcb, NULL);
26843b2aab18SMatthew Ahrens 	}
26852acef22dSMatthew Ahrens 	if (spa_feature_is_active(spa, SPA_FEATURE_ASYNC_DESTROY)) {
2686b420f3adSRichard Lowe 		VERIFY3U(0, ==, bptree_iterate(spa->spa_meta_objset,
2687ad135b5dSChristopher Siden 		    spa->spa_dsl_pool->dp_bptree_obj, B_FALSE, count_block_cb,
2688ad135b5dSChristopher Siden 		    &zcb, NULL));
2689ad135b5dSChristopher Siden 	}
2690fa9e4066Sahrens 
2691bbfd46c4SJeff Bonwick 	if (dump_opt['c'] > 1)
2692bbfd46c4SJeff Bonwick 		flags |= TRAVERSE_PREFETCH_DATA;
2693bbfd46c4SJeff Bonwick 
2694490d05b9SMatthew Ahrens 	zcb.zcb_totalasize = metaslab_class_get_alloc(spa_normal_class(spa));
2695490d05b9SMatthew Ahrens 	zcb.zcb_start = zcb.zcb_lastprint = gethrtime();
2696bbfd46c4SJeff Bonwick 	zcb.zcb_haderrors |= traverse_pool(spa, 0, flags, zdb_blkptr_cb, &zcb);
2697fa9e4066Sahrens 
269831d7e8faSGeorge Wilson 	/*
269931d7e8faSGeorge Wilson 	 * If we've traversed the data blocks then we need to wait for those
270031d7e8faSGeorge Wilson 	 * I/Os to complete. We leverage "The Godfather" zio to wait on
270131d7e8faSGeorge Wilson 	 * all async I/Os to complete.
270231d7e8faSGeorge Wilson 	 */
270331d7e8faSGeorge Wilson 	if (dump_opt['c']) {
27046f834bc1SMatthew Ahrens 		for (int i = 0; i < max_ncpus; i++) {
27056f834bc1SMatthew Ahrens 			(void) zio_wait(spa->spa_async_zio_root[i]);
27066f834bc1SMatthew Ahrens 			spa->spa_async_zio_root[i] = zio_root(spa, NULL, NULL,
27076f834bc1SMatthew Ahrens 			    ZIO_FLAG_CANFAIL | ZIO_FLAG_SPECULATIVE |
27086f834bc1SMatthew Ahrens 			    ZIO_FLAG_GODFATHER);
27096f834bc1SMatthew Ahrens 		}
271031d7e8faSGeorge Wilson 	}
271131d7e8faSGeorge Wilson 
2712b24ab676SJeff Bonwick 	if (zcb.zcb_haderrors) {
2713fa9e4066Sahrens 		(void) printf("\nError counts:\n\n");
2714fa9e4066Sahrens 		(void) printf("\t%5s  %s\n", "errno", "count");
2715b24ab676SJeff Bonwick 		for (int e = 0; e < 256; e++) {
2716fa9e4066Sahrens 			if (zcb.zcb_errors[e] != 0) {
2717fa9e4066Sahrens 				(void) printf("\t%5d  %llu\n",
2718fa9e4066Sahrens 				    e, (u_longlong_t)zcb.zcb_errors[e]);
2719fa9e4066Sahrens 			}
2720fa9e4066Sahrens 		}
2721fa9e4066Sahrens 	}
2722fa9e4066Sahrens 
2723fa9e4066Sahrens 	/*
2724fa9e4066Sahrens 	 * Report any leaked segments.
2725fa9e4066Sahrens 	 */
2726b24ab676SJeff Bonwick 	zdb_leak_fini(spa);
2727fa9e4066Sahrens 
2728b24ab676SJeff Bonwick 	tzb = &zcb.zcb_type[ZB_TOTAL][ZDB_OT_TOTAL];
27298654d025Sperrin 
2730b24ab676SJeff Bonwick 	norm_alloc = metaslab_class_get_alloc(spa_normal_class(spa));
2731b24ab676SJeff Bonwick 	norm_space = metaslab_class_get_space(spa_normal_class(spa));
27328654d025Sperrin 
2733b24ab676SJeff Bonwick 	total_alloc = norm_alloc + metaslab_class_get_alloc(spa_log_class(spa));
2734b24ab676SJeff Bonwick 	total_found = tzb->zb_asize - zcb.zcb_dedup_asize;
2735fa9e4066Sahrens 
2736b24ab676SJeff Bonwick 	if (total_found == total_alloc) {
273782a0a985SVictor Latushkin 		if (!dump_opt['L'])
273882a0a985SVictor Latushkin 			(void) printf("\n\tNo leaks (block sum matches space"
273982a0a985SVictor Latushkin 			    " maps exactly)\n");
2740fa9e4066Sahrens 	} else {
2741fa9e4066Sahrens 		(void) printf("block traversal size %llu != alloc %llu "
274282a0a985SVictor Latushkin 		    "(%s %lld)\n",
2743b24ab676SJeff Bonwick 		    (u_longlong_t)total_found,
2744b24ab676SJeff Bonwick 		    (u_longlong_t)total_alloc,
274582a0a985SVictor Latushkin 		    (dump_opt['L']) ? "unreachable" : "leaked",
2746b24ab676SJeff Bonwick 		    (longlong_t)(total_alloc - total_found));
27475d7b4d43SMatthew Ahrens 		leaks = B_TRUE;
2748fa9e4066Sahrens 	}
2749fa9e4066Sahrens 
2750fa9e4066Sahrens 	if (tzb->zb_count == 0)
2751fa9e4066Sahrens 		return (2);
2752fa9e4066Sahrens 
2753fa9e4066Sahrens 	(void) printf("\n");
2754fa9e4066Sahrens 	(void) printf("\tbp count:      %10llu\n",
2755fa9e4066Sahrens 	    (u_longlong_t)tzb->zb_count);
2756d5ee8a13SMatthew Ahrens 	(void) printf("\tganged count:  %10llu\n",
2757d5ee8a13SMatthew Ahrens 	    (longlong_t)tzb->zb_gangs);
2758b24ab676SJeff Bonwick 	(void) printf("\tbp logical:    %10llu      avg: %6llu\n",
2759fa9e4066Sahrens 	    (u_longlong_t)tzb->zb_lsize,
2760fa9e4066Sahrens 	    (u_longlong_t)(tzb->zb_lsize / tzb->zb_count));
2761b24ab676SJeff Bonwick 	(void) printf("\tbp physical:   %10llu      avg:"
2762b24ab676SJeff Bonwick 	    " %6llu     compression: %6.2f\n",
2763fa9e4066Sahrens 	    (u_longlong_t)tzb->zb_psize,
2764fa9e4066Sahrens 	    (u_longlong_t)(tzb->zb_psize / tzb->zb_count),
2765fa9e4066Sahrens 	    (double)tzb->zb_lsize / tzb->zb_psize);
2766b24ab676SJeff Bonwick 	(void) printf("\tbp allocated:  %10llu      avg:"
2767b24ab676SJeff Bonwick 	    " %6llu     compression: %6.2f\n",
2768fa9e4066Sahrens 	    (u_longlong_t)tzb->zb_asize,
2769fa9e4066Sahrens 	    (u_longlong_t)(tzb->zb_asize / tzb->zb_count),
2770fa9e4066Sahrens 	    (double)tzb->zb_lsize / tzb->zb_asize);
2771b24ab676SJeff Bonwick 	(void) printf("\tbp deduped:    %10llu    ref>1:"
2772b24ab676SJeff Bonwick 	    " %6llu   deduplication: %6.2f\n",
2773b24ab676SJeff Bonwick 	    (u_longlong_t)zcb.zcb_dedup_asize,
2774b24ab676SJeff Bonwick 	    (u_longlong_t)zcb.zcb_dedup_blocks,
2775b24ab676SJeff Bonwick 	    (double)zcb.zcb_dedup_asize / tzb->zb_asize + 1.0);
2776b24ab676SJeff Bonwick 	(void) printf("\tSPA allocated: %10llu     used: %5.2f%%\n",
2777b24ab676SJeff Bonwick 	    (u_longlong_t)norm_alloc, 100.0 * norm_alloc / norm_space);
2778fa9e4066Sahrens 
27795d7b4d43SMatthew Ahrens 	for (bp_embedded_type_t i = 0; i < NUM_BP_EMBEDDED_TYPES; i++) {
27805d7b4d43SMatthew Ahrens 		if (zcb.zcb_embedded_blocks[i] == 0)
27815d7b4d43SMatthew Ahrens 			continue;
27825d7b4d43SMatthew Ahrens 		(void) printf("\n");
27835d7b4d43SMatthew Ahrens 		(void) printf("\tadditional, non-pointer bps of type %u: "
27845d7b4d43SMatthew Ahrens 		    "%10llu\n",
27855d7b4d43SMatthew Ahrens 		    i, (u_longlong_t)zcb.zcb_embedded_blocks[i]);
27865d7b4d43SMatthew Ahrens 
27875d7b4d43SMatthew Ahrens 		if (dump_opt['b'] >= 3) {
27885d7b4d43SMatthew Ahrens 			(void) printf("\t number of (compressed) bytes:  "
27895d7b4d43SMatthew Ahrens 			    "number of bps\n");
27905d7b4d43SMatthew Ahrens 			dump_histogram(zcb.zcb_embedded_histogram[i],
27915d7b4d43SMatthew Ahrens 			    sizeof (zcb.zcb_embedded_histogram[i]) /
27925d7b4d43SMatthew Ahrens 			    sizeof (zcb.zcb_embedded_histogram[i][0]), 0);
27935d7b4d43SMatthew Ahrens 		}
27945d7b4d43SMatthew Ahrens 	}
27955d7b4d43SMatthew Ahrens 
2796d5ee8a13SMatthew Ahrens 	if (tzb->zb_ditto_samevdev != 0) {
2797d5ee8a13SMatthew Ahrens 		(void) printf("\tDittoed blocks on same vdev: %llu\n",
2798d5ee8a13SMatthew Ahrens 		    (longlong_t)tzb->zb_ditto_samevdev);
2799d5ee8a13SMatthew Ahrens 	}
2800d5ee8a13SMatthew Ahrens 
2801fa9e4066Sahrens 	if (dump_opt['b'] >= 2) {
2802fa9e4066Sahrens 		int l, t, level;
2803fa9e4066Sahrens 		(void) printf("\nBlocks\tLSIZE\tPSIZE\tASIZE"
2804fa9e4066Sahrens 		    "\t  avg\t comp\t%%Total\tType\n");
2805fa9e4066Sahrens 
2806b24ab676SJeff Bonwick 		for (t = 0; t <= ZDB_OT_TOTAL; t++) {
28073f9d6ad7SLin Ling 			char csize[32], lsize[32], psize[32], asize[32];
2808d5ee8a13SMatthew Ahrens 			char avg[32], gang[32];
2809fa9e4066Sahrens 			char *typename;
2810fa9e4066Sahrens 
2811b24ab676SJeff Bonwick 			if (t < DMU_OT_NUMTYPES)
2812b24ab676SJeff Bonwick 				typename = dmu_ot[t].ot_name;
2813b24ab676SJeff Bonwick 			else
2814b24ab676SJeff Bonwick 				typename = zdb_ot_extname[t - DMU_OT_NUMTYPES];
2815fa9e4066Sahrens 
2816fa9e4066Sahrens 			if (zcb.zcb_type[ZB_TOTAL][t].zb_asize == 0) {
2817fa9e4066Sahrens 				(void) printf("%6s\t%5s\t%5s\t%5s"
2818fa9e4066Sahrens 				    "\t%5s\t%5s\t%6s\t%s\n",
2819fa9e4066Sahrens 				    "-",
2820fa9e4066Sahrens 				    "-",
2821fa9e4066Sahrens 				    "-",
2822fa9e4066Sahrens 				    "-",
2823fa9e4066Sahrens 				    "-",
2824fa9e4066Sahrens 				    "-",
2825fa9e4066Sahrens 				    "-",
2826fa9e4066Sahrens 				    typename);
2827fa9e4066Sahrens 				continue;
2828fa9e4066Sahrens 			}
2829fa9e4066Sahrens 
2830fa9e4066Sahrens 			for (l = ZB_TOTAL - 1; l >= -1; l--) {
2831fa9e4066Sahrens 				level = (l == -1 ? ZB_TOTAL : l);
2832fa9e4066Sahrens 				zb = &zcb.zcb_type[level][t];
2833fa9e4066Sahrens 
2834fa9e4066Sahrens 				if (zb->zb_asize == 0)
2835fa9e4066Sahrens 					continue;
2836fa9e4066Sahrens 
2837fa9e4066Sahrens 				if (dump_opt['b'] < 3 && level != ZB_TOTAL)
2838fa9e4066Sahrens 					continue;
2839fa9e4066Sahrens 
2840fa9e4066Sahrens 				if (level == 0 && zb->zb_asize ==
2841fa9e4066Sahrens 				    zcb.zcb_type[ZB_TOTAL][t].zb_asize)
2842fa9e4066Sahrens 					continue;
2843fa9e4066Sahrens 
28443f9d6ad7SLin Ling 				zdb_nicenum(zb->zb_count, csize);
28453f9d6ad7SLin Ling 				zdb_nicenum(zb->zb_lsize, lsize);
28463f9d6ad7SLin Ling 				zdb_nicenum(zb->zb_psize, psize);
28473f9d6ad7SLin Ling 				zdb_nicenum(zb->zb_asize, asize);
28483f9d6ad7SLin Ling 				zdb_nicenum(zb->zb_asize / zb->zb_count, avg);
2849d5ee8a13SMatthew Ahrens 				zdb_nicenum(zb->zb_gangs, gang);
2850fa9e4066Sahrens 
2851fa9e4066Sahrens 				(void) printf("%6s\t%5s\t%5s\t%5s\t%5s"
2852fa9e4066Sahrens 				    "\t%5.2f\t%6.2f\t",
2853fa9e4066Sahrens 				    csize, lsize, psize, asize, avg,
2854fa9e4066Sahrens 				    (double)zb->zb_lsize / zb->zb_psize,
2855fa9e4066Sahrens 				    100.0 * zb->zb_asize / tzb->zb_asize);
2856fa9e4066Sahrens 
2857fa9e4066Sahrens 				if (level == ZB_TOTAL)
2858fa9e4066Sahrens 					(void) printf("%s\n", typename);
2859fa9e4066Sahrens 				else
2860fa9e4066Sahrens 					(void) printf("    L%d %s\n",
2861fa9e4066Sahrens 					    level, typename);
2862490d05b9SMatthew Ahrens 
2863d5ee8a13SMatthew Ahrens 				if (dump_opt['b'] >= 3 && zb->zb_gangs > 0) {
2864d5ee8a13SMatthew Ahrens 					(void) printf("\t number of ganged "
2865d5ee8a13SMatthew Ahrens 					    "blocks: %s\n", gang);
2866d5ee8a13SMatthew Ahrens 				}
2867d5ee8a13SMatthew Ahrens 
2868490d05b9SMatthew Ahrens 				if (dump_opt['b'] >= 4) {
2869490d05b9SMatthew Ahrens 					(void) printf("psize "
2870490d05b9SMatthew Ahrens 					    "(in 512-byte sectors): "
2871490d05b9SMatthew Ahrens 					    "number of blocks\n");
2872490d05b9SMatthew Ahrens 					dump_histogram(zb->zb_psize_histogram,
28730713e232SGeorge Wilson 					    PSIZE_HISTO_SIZE, 0);
2874490d05b9SMatthew Ahrens 				}
2875fa9e4066Sahrens 			}
2876fa9e4066Sahrens 		}
2877fa9e4066Sahrens 	}
2878fa9e4066Sahrens 
2879fa9e4066Sahrens 	(void) printf("\n");
2880fa9e4066Sahrens 
2881fa9e4066Sahrens 	if (leaks)
2882fa9e4066Sahrens 		return (2);
2883fa9e4066Sahrens 
2884fa9e4066Sahrens 	if (zcb.zcb_haderrors)
2885fa9e4066Sahrens 		return (3);
2886fa9e4066Sahrens 
2887fa9e4066Sahrens 	return (0);
2888fa9e4066Sahrens }
2889fa9e4066Sahrens 
2890b24ab676SJeff Bonwick typedef struct zdb_ddt_entry {
2891b24ab676SJeff Bonwick 	ddt_key_t	zdde_key;
2892b24ab676SJeff Bonwick 	uint64_t	zdde_ref_blocks;
2893b24ab676SJeff Bonwick 	uint64_t	zdde_ref_lsize;
2894b24ab676SJeff Bonwick 	uint64_t	zdde_ref_psize;
2895b24ab676SJeff Bonwick 	uint64_t	zdde_ref_dsize;
2896b24ab676SJeff Bonwick 	avl_node_t	zdde_node;
2897b24ab676SJeff Bonwick } zdb_ddt_entry_t;
2898b24ab676SJeff Bonwick 
2899b24ab676SJeff Bonwick /* ARGSUSED */
2900b24ab676SJeff Bonwick static int
2901b24ab676SJeff Bonwick zdb_ddt_add_cb(spa_t *spa, zilog_t *zilog, const blkptr_t *bp,
29027802d7bfSMatthew Ahrens     const zbookmark_phys_t *zb, const dnode_phys_t *dnp, void *arg)
2903b24ab676SJeff Bonwick {
2904b24ab676SJeff Bonwick 	avl_tree_t *t = arg;
2905b24ab676SJeff Bonwick 	avl_index_t where;
2906b24ab676SJeff Bonwick 	zdb_ddt_entry_t *zdde, zdde_search;
2907b24ab676SJeff Bonwick 
2908a2cdcdd2SPaul Dagnelie 	if (bp == NULL || BP_IS_HOLE(bp) || BP_IS_EMBEDDED(bp))
2909b24ab676SJeff Bonwick 		return (0);
2910b24ab676SJeff Bonwick 
2911b24ab676SJeff Bonwick 	if (dump_opt['S'] > 1 && zb->zb_level == ZB_ROOT_LEVEL) {
2912b24ab676SJeff Bonwick 		(void) printf("traversing objset %llu, %llu objects, "
2913b24ab676SJeff Bonwick 		    "%lu blocks so far\n",
2914b24ab676SJeff Bonwick 		    (u_longlong_t)zb->zb_objset,
29155d7b4d43SMatthew Ahrens 		    (u_longlong_t)BP_GET_FILL(bp),
2916b24ab676SJeff Bonwick 		    avl_numnodes(t));
2917b24ab676SJeff Bonwick 	}
2918b24ab676SJeff Bonwick 
2919bbfd46c4SJeff Bonwick 	if (BP_IS_HOLE(bp) || BP_GET_CHECKSUM(bp) == ZIO_CHECKSUM_OFF ||
2920ad135b5dSChristopher Siden 	    BP_GET_LEVEL(bp) > 0 || DMU_OT_IS_METADATA(BP_GET_TYPE(bp)))
2921b24ab676SJeff Bonwick 		return (0);
2922b24ab676SJeff Bonwick 
2923b24ab676SJeff Bonwick 	ddt_key_fill(&zdde_search.zdde_key, bp);
2924b24ab676SJeff Bonwick 
2925b24ab676SJeff Bonwick 	zdde = avl_find(t, &zdde_search, &where);
2926b24ab676SJeff Bonwick 
2927b24ab676SJeff Bonwick 	if (zdde == NULL) {
2928b24ab676SJeff Bonwick 		zdde = umem_zalloc(sizeof (*zdde), UMEM_NOFAIL);
2929b24ab676SJeff Bonwick 		zdde->zdde_key = zdde_search.zdde_key;
2930b24ab676SJeff Bonwick 		avl_insert(t, zdde, where);
2931b24ab676SJeff Bonwick 	}
2932b24ab676SJeff Bonwick 
2933b24ab676SJeff Bonwick 	zdde->zdde_ref_blocks += 1;
2934b24ab676SJeff Bonwick 	zdde->zdde_ref_lsize += BP_GET_LSIZE(bp);
2935b24ab676SJeff Bonwick 	zdde->zdde_ref_psize += BP_GET_PSIZE(bp);
2936b24ab676SJeff Bonwick 	zdde->zdde_ref_dsize += bp_get_dsize_sync(spa, bp);
2937b24ab676SJeff Bonwick 
2938b24ab676SJeff Bonwick 	return (0);
2939b24ab676SJeff Bonwick }
2940b24ab676SJeff Bonwick 
2941b24ab676SJeff Bonwick static void
2942b24ab676SJeff Bonwick dump_simulated_ddt(spa_t *spa)
2943b24ab676SJeff Bonwick {
2944b24ab676SJeff Bonwick 	avl_tree_t t;
2945b24ab676SJeff Bonwick 	void *cookie = NULL;
2946b24ab676SJeff Bonwick 	zdb_ddt_entry_t *zdde;
2947b24ab676SJeff Bonwick 	ddt_histogram_t ddh_total = { 0 };
2948b24ab676SJeff Bonwick 	ddt_stat_t dds_total = { 0 };
2949b24ab676SJeff Bonwick 
2950b24ab676SJeff Bonwick 	avl_create(&t, ddt_entry_compare,
2951b24ab676SJeff Bonwick 	    sizeof (zdb_ddt_entry_t), offsetof(zdb_ddt_entry_t, zdde_node));
2952b24ab676SJeff Bonwick 
2953b24ab676SJeff Bonwick 	spa_config_enter(spa, SCL_CONFIG, FTAG, RW_READER);
2954b24ab676SJeff Bonwick 
2955bbfd46c4SJeff Bonwick 	(void) traverse_pool(spa, 0, TRAVERSE_PRE | TRAVERSE_PREFETCH_METADATA,
2956bbfd46c4SJeff Bonwick 	    zdb_ddt_add_cb, &t);
2957b24ab676SJeff Bonwick 
2958b24ab676SJeff Bonwick 	spa_config_exit(spa, SCL_CONFIG, FTAG);
2959b24ab676SJeff Bonwick 
2960b24ab676SJeff Bonwick 	while ((zdde = avl_destroy_nodes(&t, &cookie)) != NULL) {
2961b24ab676SJeff Bonwick 		ddt_stat_t dds;
2962b24ab676SJeff Bonwick 		uint64_t refcnt = zdde->zdde_ref_blocks;
2963b24ab676SJeff Bonwick 		ASSERT(refcnt != 0);
2964b24ab676SJeff Bonwick 
2965b24ab676SJeff Bonwick 		dds.dds_blocks = zdde->zdde_ref_blocks / refcnt;
2966b24ab676SJeff Bonwick 		dds.dds_lsize = zdde->zdde_ref_lsize / refcnt;
2967b24ab676SJeff Bonwick 		dds.dds_psize = zdde->zdde_ref_psize / refcnt;
2968b24ab676SJeff Bonwick 		dds.dds_dsize = zdde->zdde_ref_dsize / refcnt;
2969b24ab676SJeff Bonwick 
2970b24ab676SJeff Bonwick 		dds.dds_ref_blocks = zdde->zdde_ref_blocks;
2971b24ab676SJeff Bonwick 		dds.dds_ref_lsize = zdde->zdde_ref_lsize;
2972b24ab676SJeff Bonwick 		dds.dds_ref_psize = zdde->zdde_ref_psize;
2973b24ab676SJeff Bonwick 		dds.dds_ref_dsize = zdde->zdde_ref_dsize;
2974b24ab676SJeff Bonwick 
2975bf16b11eSMatthew Ahrens 		ddt_stat_add(&ddh_total.ddh_stat[highbit64(refcnt) - 1],
2976bf16b11eSMatthew Ahrens 		    &dds, 0);
2977b24ab676SJeff Bonwick 
2978b24ab676SJeff Bonwick 		umem_free(zdde, sizeof (*zdde));
2979b24ab676SJeff Bonwick 	}
2980b24ab676SJeff Bonwick 
2981b24ab676SJeff Bonwick 	avl_destroy(&t);
2982b24ab676SJeff Bonwick 
2983b24ab676SJeff Bonwick 	ddt_histogram_stat(&dds_total, &ddh_total);
2984b24ab676SJeff Bonwick 
2985b24ab676SJeff Bonwick 	(void) printf("Simulated DDT histogram:\n");
2986b24ab676SJeff Bonwick 
29879eb19f4dSGeorge Wilson 	zpool_dump_ddt(&dds_total, &ddh_total);
2988b24ab676SJeff Bonwick 
2989b24ab676SJeff Bonwick 	dump_dedup_ratio(&dds_total);
2990b24ab676SJeff Bonwick }
2991b24ab676SJeff Bonwick 
2992fa9e4066Sahrens static void
2993fa9e4066Sahrens dump_zpool(spa_t *spa)
2994fa9e4066Sahrens {
2995fa9e4066Sahrens 	dsl_pool_t *dp = spa_get_dsl(spa);
2996fa9e4066Sahrens 	int rc = 0;
2997fa9e4066Sahrens 
2998b24ab676SJeff Bonwick 	if (dump_opt['S']) {
2999b24ab676SJeff Bonwick 		dump_simulated_ddt(spa);
3000b24ab676SJeff Bonwick 		return;
3001b24ab676SJeff Bonwick 	}
3002b24ab676SJeff Bonwick 
300307428bdfSVictor Latushkin 	if (!dump_opt['e'] && dump_opt['C'] > 1) {
300407428bdfSVictor Latushkin 		(void) printf("\nCached configuration:\n");
300507428bdfSVictor Latushkin 		dump_nvlist(spa->spa_config, 8);
300607428bdfSVictor Latushkin 	}
300707428bdfSVictor Latushkin 
300807428bdfSVictor Latushkin 	if (dump_opt['C'])
300907428bdfSVictor Latushkin 		dump_config(spa);
301007428bdfSVictor Latushkin 
3011fa9e4066Sahrens 	if (dump_opt['u'])
301253b9a4a9SVictor Latushkin 		dump_uberblock(&spa->spa_uberblock, "\nUberblock:\n", "\n");
3013fa9e4066Sahrens 
3014b24ab676SJeff Bonwick 	if (dump_opt['D'])
3015b24ab676SJeff Bonwick 		dump_all_ddts(spa);
3016b24ab676SJeff Bonwick 
301787219db7SVictor Latushkin 	if (dump_opt['d'] > 2 || dump_opt['m'])
301887219db7SVictor Latushkin 		dump_metaslabs(spa);
30192e4c9986SGeorge Wilson 	if (dump_opt['M'])
30202e4c9986SGeorge Wilson 		dump_metaslab_groups(spa);
302187219db7SVictor Latushkin 
302287219db7SVictor Latushkin 	if (dump_opt['d'] || dump_opt['i']) {
3023fa9e4066Sahrens 		dump_dir(dp->dp_meta_objset);
3024fa9e4066Sahrens 		if (dump_opt['d'] >= 3) {
3025732885fcSMatthew Ahrens 			dump_full_bpobj(&spa->spa_deferred_bpobj,
3026d0475637SMatthew Ahrens 			    "Deferred frees", 0);
3027cde58dbcSMatthew Ahrens 			if (spa_version(spa) >= SPA_VERSION_DEADLISTS) {
3028732885fcSMatthew Ahrens 				dump_full_bpobj(
3029732885fcSMatthew Ahrens 				    &spa->spa_dsl_pool->dp_free_bpobj,
3030d0475637SMatthew Ahrens 				    "Pool snapshot frees", 0);
3031ad135b5dSChristopher Siden 			}
3032ad135b5dSChristopher Siden 
3033ad135b5dSChristopher Siden 			if (spa_feature_is_active(spa,
30342acef22dSMatthew Ahrens 			    SPA_FEATURE_ASYNC_DESTROY)) {
3035ad135b5dSChristopher Siden 				dump_bptree(spa->spa_meta_objset,
3036ad135b5dSChristopher Siden 				    spa->spa_dsl_pool->dp_bptree_obj,
3037ad135b5dSChristopher Siden 				    "Pool dataset frees");
3038cde58dbcSMatthew Ahrens 			}
3039fa9e4066Sahrens 			dump_dtl(spa->spa_root_vdev, 0);
3040fa9e4066Sahrens 		}
304107428bdfSVictor Latushkin 		(void) dmu_objset_find(spa_name(spa), dump_one_dir,
304207428bdfSVictor Latushkin 		    NULL, DS_FIND_SNAPSHOTS | DS_FIND_CHILDREN);
3043b5152584SMatthew Ahrens 
3044ca0cc391SMatthew Ahrens 		for (spa_feature_t f = 0; f < SPA_FEATURES; f++) {
3045ca0cc391SMatthew Ahrens 			uint64_t refcount;
3046ca0cc391SMatthew Ahrens 
3047ca0cc391SMatthew Ahrens 			if (!(spa_feature_table[f].fi_flags &
3048ca0cc391SMatthew Ahrens 			    ZFEATURE_FLAG_PER_DATASET)) {
3049ca0cc391SMatthew Ahrens 				ASSERT0(dataset_feature_count[f]);
3050ca0cc391SMatthew Ahrens 				continue;
3051ca0cc391SMatthew Ahrens 			}
3052ca0cc391SMatthew Ahrens 			(void) feature_get_refcount(spa,
3053ca0cc391SMatthew Ahrens 			    &spa_feature_table[f], &refcount);
3054ca0cc391SMatthew Ahrens 			if (dataset_feature_count[f] != refcount) {
3055ca0cc391SMatthew Ahrens 				(void) printf("%s feature refcount mismatch: "
3056ca0cc391SMatthew Ahrens 				    "%lld datasets != %lld refcount\n",
3057ca0cc391SMatthew Ahrens 				    spa_feature_table[f].fi_uname,
3058ca0cc391SMatthew Ahrens 				    (longlong_t)dataset_feature_count[f],
3059ca0cc391SMatthew Ahrens 				    (longlong_t)refcount);
3060ca0cc391SMatthew Ahrens 				rc = 2;
3061ca0cc391SMatthew Ahrens 			} else {
3062ca0cc391SMatthew Ahrens 				(void) printf("Verified %s feature refcount "
3063ca0cc391SMatthew Ahrens 				    "of %llu is correct\n",
3064ca0cc391SMatthew Ahrens 				    spa_feature_table[f].fi_uname,
3065ca0cc391SMatthew Ahrens 				    (longlong_t)refcount);
3066ca0cc391SMatthew Ahrens 			}
3067b5152584SMatthew Ahrens 		}
3068fa9e4066Sahrens 	}
3069b5152584SMatthew Ahrens 	if (rc == 0 && (dump_opt['b'] || dump_opt['c']))
3070fa9e4066Sahrens 		rc = dump_block_stats(spa);
3071fa9e4066Sahrens 
30720713e232SGeorge Wilson 	if (rc == 0)
30730713e232SGeorge Wilson 		rc = verify_spacemap_refcounts(spa);
30740713e232SGeorge Wilson 
3075fa9e4066Sahrens 	if (dump_opt['s'])
3076fa9e4066Sahrens 		show_pool_stats(spa);
3077fa9e4066Sahrens 
30788f18d1faSGeorge Wilson 	if (dump_opt['h'])
30798f18d1faSGeorge Wilson 		dump_history(spa);
30808f18d1faSGeorge Wilson 
3081fa9e4066Sahrens 	if (rc != 0)
3082fa9e4066Sahrens 		exit(rc);
3083fa9e4066Sahrens }
3084fa9e4066Sahrens 
308544cd46caSbillm #define	ZDB_FLAG_CHECKSUM	0x0001
308644cd46caSbillm #define	ZDB_FLAG_DECOMPRESS	0x0002
308744cd46caSbillm #define	ZDB_FLAG_BSWAP		0x0004
308844cd46caSbillm #define	ZDB_FLAG_GBH		0x0008
308944cd46caSbillm #define	ZDB_FLAG_INDIRECT	0x0010
309044cd46caSbillm #define	ZDB_FLAG_PHYS		0x0020
309144cd46caSbillm #define	ZDB_FLAG_RAW		0x0040
309244cd46caSbillm #define	ZDB_FLAG_PRINT_BLKPTR	0x0080
309344cd46caSbillm 
309444cd46caSbillm int flagbits[256];
309544cd46caSbillm 
309644cd46caSbillm static void
309744cd46caSbillm zdb_print_blkptr(blkptr_t *bp, int flags)
309844cd46caSbillm {
3099b24ab676SJeff Bonwick 	char blkbuf[BP_SPRINTF_LEN];
310044cd46caSbillm 
310144cd46caSbillm 	if (flags & ZDB_FLAG_BSWAP)
310244cd46caSbillm 		byteswap_uint64_array((void *)bp, sizeof (blkptr_t));
3103b24ab676SJeff Bonwick 
310443466aaeSMax Grossman 	snprintf_blkptr(blkbuf, sizeof (blkbuf), bp);
3105b24ab676SJeff Bonwick 	(void) printf("%s\n", blkbuf);
310644cd46caSbillm }
310744cd46caSbillm 
310844cd46caSbillm static void
310944cd46caSbillm zdb_dump_indirect(blkptr_t *bp, int nbps, int flags)
311044cd46caSbillm {
311144cd46caSbillm 	int i;
311244cd46caSbillm 
311344cd46caSbillm 	for (i = 0; i < nbps; i++)
311444cd46caSbillm 		zdb_print_blkptr(&bp[i], flags);
311544cd46caSbillm }
311644cd46caSbillm 
311744cd46caSbillm static void
311844cd46caSbillm zdb_dump_gbh(void *buf, int flags)
311944cd46caSbillm {
312044cd46caSbillm 	zdb_dump_indirect((blkptr_t *)buf, SPA_GBH_NBLKPTRS, flags);
312144cd46caSbillm }
312244cd46caSbillm 
312344cd46caSbillm static void
312444cd46caSbillm zdb_dump_block_raw(void *buf, uint64_t size, int flags)
312544cd46caSbillm {
312644cd46caSbillm 	if (flags & ZDB_FLAG_BSWAP)
312744cd46caSbillm 		byteswap_uint64_array(buf, size);
3128b24ab676SJeff Bonwick 	(void) write(1, buf, size);
312944cd46caSbillm }
313044cd46caSbillm 
313144cd46caSbillm static void
313244cd46caSbillm zdb_dump_block(char *label, void *buf, uint64_t size, int flags)
313344cd46caSbillm {
313444cd46caSbillm 	uint64_t *d = (uint64_t *)buf;
313544cd46caSbillm 	int nwords = size / sizeof (uint64_t);
313644cd46caSbillm 	int do_bswap = !!(flags & ZDB_FLAG_BSWAP);
313744cd46caSbillm 	int i, j;
313844cd46caSbillm 	char *hdr, *c;
313944cd46caSbillm 
314044cd46caSbillm 
314144cd46caSbillm 	if (do_bswap)
314244cd46caSbillm 		hdr = " 7 6 5 4 3 2 1 0   f e d c b a 9 8";
314344cd46caSbillm 	else
314444cd46caSbillm 		hdr = " 0 1 2 3 4 5 6 7   8 9 a b c d e f";
314544cd46caSbillm 
314644cd46caSbillm 	(void) printf("\n%s\n%6s   %s  0123456789abcdef\n", label, "", hdr);
314744cd46caSbillm 
314844cd46caSbillm 	for (i = 0; i < nwords; i += 2) {
314944cd46caSbillm 		(void) printf("%06llx:  %016llx  %016llx  ",
315044cd46caSbillm 		    (u_longlong_t)(i * sizeof (uint64_t)),
315144cd46caSbillm 		    (u_longlong_t)(do_bswap ? BSWAP_64(d[i]) : d[i]),
315244cd46caSbillm 		    (u_longlong_t)(do_bswap ? BSWAP_64(d[i + 1]) : d[i + 1]));
315344cd46caSbillm 
315444cd46caSbillm 		c = (char *)&d[i];
315544cd46caSbillm 		for (j = 0; j < 2 * sizeof (uint64_t); j++)
315644cd46caSbillm 			(void) printf("%c", isprint(c[j]) ? c[j] : '.');
315744cd46caSbillm 		(void) printf("\n");
315844cd46caSbillm 	}
315944cd46caSbillm }
316044cd46caSbillm 
316144cd46caSbillm /*
316244cd46caSbillm  * There are two acceptable formats:
316344cd46caSbillm  *	leaf_name	  - For example: c1t0d0 or /tmp/ztest.0a
316444cd46caSbillm  *	child[.child]*    - For example: 0.1.1
316544cd46caSbillm  *
316644cd46caSbillm  * The second form can be used to specify arbitrary vdevs anywhere
316744cd46caSbillm  * in the heirarchy.  For example, in a pool with a mirror of
316844cd46caSbillm  * RAID-Zs, you can specify either RAID-Z vdev with 0.0 or 0.1 .
316944cd46caSbillm  */
317044cd46caSbillm static vdev_t *
317144cd46caSbillm zdb_vdev_lookup(vdev_t *vdev, char *path)
317244cd46caSbillm {
317344cd46caSbillm 	char *s, *p, *q;
317444cd46caSbillm 	int i;
317544cd46caSbillm 
317644cd46caSbillm 	if (vdev == NULL)
317744cd46caSbillm 		return (NULL);
317844cd46caSbillm 
317944cd46caSbillm 	/* First, assume the x.x.x.x format */
318044cd46caSbillm 	i = (int)strtoul(path, &s, 10);
318144cd46caSbillm 	if (s == path || (s && *s != '.' && *s != '\0'))
318244cd46caSbillm 		goto name;
318344cd46caSbillm 	if (i < 0 || i >= vdev->vdev_children)
318444cd46caSbillm 		return (NULL);
318544cd46caSbillm 
318644cd46caSbillm 	vdev = vdev->vdev_child[i];
318744cd46caSbillm 	if (*s == '\0')
318844cd46caSbillm 		return (vdev);
318944cd46caSbillm 	return (zdb_vdev_lookup(vdev, s+1));
319044cd46caSbillm 
319144cd46caSbillm name:
319244cd46caSbillm 	for (i = 0; i < vdev->vdev_children; i++) {
319344cd46caSbillm 		vdev_t *vc = vdev->vdev_child[i];
319444cd46caSbillm 
319544cd46caSbillm 		if (vc->vdev_path == NULL) {
319644cd46caSbillm 			vc = zdb_vdev_lookup(vc, path);
319744cd46caSbillm 			if (vc == NULL)
319844cd46caSbillm 				continue;
319944cd46caSbillm 			else
320044cd46caSbillm 				return (vc);
320144cd46caSbillm 		}
320244cd46caSbillm 
320344cd46caSbillm 		p = strrchr(vc->vdev_path, '/');
320444cd46caSbillm 		p = p ? p + 1 : vc->vdev_path;
320544cd46caSbillm 		q = &vc->vdev_path[strlen(vc->vdev_path) - 2];
320644cd46caSbillm 
320744cd46caSbillm 		if (strcmp(vc->vdev_path, path) == 0)
320844cd46caSbillm 			return (vc);
320944cd46caSbillm 		if (strcmp(p, path) == 0)
321044cd46caSbillm 			return (vc);
321144cd46caSbillm 		if (strcmp(q, "s0") == 0 && strncmp(p, path, q - p) == 0)
321244cd46caSbillm 			return (vc);
321344cd46caSbillm 	}
321444cd46caSbillm 
321544cd46caSbillm 	return (NULL);
321644cd46caSbillm }
321744cd46caSbillm 
321844cd46caSbillm /*
321944cd46caSbillm  * Read a block from a pool and print it out.  The syntax of the
322044cd46caSbillm  * block descriptor is:
322144cd46caSbillm  *
322244cd46caSbillm  *	pool:vdev_specifier:offset:size[:flags]
322344cd46caSbillm  *
322444cd46caSbillm  *	pool           - The name of the pool you wish to read from
322544cd46caSbillm  *	vdev_specifier - Which vdev (see comment for zdb_vdev_lookup)
322644cd46caSbillm  *	offset         - offset, in hex, in bytes
322744cd46caSbillm  *	size           - Amount of data to read, in hex, in bytes
322844cd46caSbillm  *	flags          - A string of characters specifying options
322944cd46caSbillm  *		 b: Decode a blkptr at given offset within block
323044cd46caSbillm  *		*c: Calculate and display checksums
3231b24ab676SJeff Bonwick  *		 d: Decompress data before dumping
323244cd46caSbillm  *		 e: Byteswap data before dumping
3233b24ab676SJeff Bonwick  *		 g: Display data as a gang block header
3234b24ab676SJeff Bonwick  *		 i: Display as an indirect block
323544cd46caSbillm  *		 p: Do I/O to physical offset
323644cd46caSbillm  *		 r: Dump raw data to stdout
323744cd46caSbillm  *
323844cd46caSbillm  *              * = not yet implemented
323944cd46caSbillm  */
324044cd46caSbillm static void
324107428bdfSVictor Latushkin zdb_read_block(char *thing, spa_t *spa)
324244cd46caSbillm {
3243b24ab676SJeff Bonwick 	blkptr_t blk, *bp = &blk;
3244b24ab676SJeff Bonwick 	dva_t *dva = bp->blk_dva;
324544cd46caSbillm 	int flags = 0;
3246b24ab676SJeff Bonwick 	uint64_t offset = 0, size = 0, psize = 0, lsize = 0, blkptr_offset = 0;
324744cd46caSbillm 	zio_t *zio;
324844cd46caSbillm 	vdev_t *vd;
3249b24ab676SJeff Bonwick 	void *pbuf, *lbuf, *buf;
325007428bdfSVictor Latushkin 	char *s, *p, *dup, *vdev, *flagstr;
3251b24ab676SJeff Bonwick 	int i, error;
325244cd46caSbillm 
325344cd46caSbillm 	dup = strdup(thing);
325444cd46caSbillm 	s = strtok(dup, ":");
325544cd46caSbillm 	vdev = s ? s : "";
325644cd46caSbillm 	s = strtok(NULL, ":");
325744cd46caSbillm 	offset = strtoull(s ? s : "", NULL, 16);
325844cd46caSbillm 	s = strtok(NULL, ":");
325944cd46caSbillm 	size = strtoull(s ? s : "", NULL, 16);
326044cd46caSbillm 	s = strtok(NULL, ":");
326144cd46caSbillm 	flagstr = s ? s : "";
326244cd46caSbillm 
326344cd46caSbillm 	s = NULL;
326444cd46caSbillm 	if (size == 0)
326544cd46caSbillm 		s = "size must not be zero";
326644cd46caSbillm 	if (!IS_P2ALIGNED(size, DEV_BSIZE))
326744cd46caSbillm 		s = "size must be a multiple of sector size";
326844cd46caSbillm 	if (!IS_P2ALIGNED(offset, DEV_BSIZE))
326944cd46caSbillm 		s = "offset must be a multiple of sector size";
327044cd46caSbillm 	if (s) {
327144cd46caSbillm 		(void) printf("Invalid block specifier: %s  - %s\n", thing, s);
327244cd46caSbillm 		free(dup);
327344cd46caSbillm 		return;
327444cd46caSbillm 	}
327544cd46caSbillm 
327644cd46caSbillm 	for (s = strtok(flagstr, ":"); s; s = strtok(NULL, ":")) {
327744cd46caSbillm 		for (i = 0; flagstr[i]; i++) {
32785ad82045Snd 			int bit = flagbits[(uchar_t)flagstr[i]];
327944cd46caSbillm 
328044cd46caSbillm 			if (bit == 0) {
328144cd46caSbillm 				(void) printf("***Invalid flag: %c\n",
328244cd46caSbillm 				    flagstr[i]);
328344cd46caSbillm 				continue;
328444cd46caSbillm 			}
328544cd46caSbillm 			flags |= bit;
328644cd46caSbillm 
328744cd46caSbillm 			/* If it's not something with an argument, keep going */
3288b24ab676SJeff Bonwick 			if ((bit & (ZDB_FLAG_CHECKSUM |
328944cd46caSbillm 			    ZDB_FLAG_PRINT_BLKPTR)) == 0)
329044cd46caSbillm 				continue;
329144cd46caSbillm 
329244cd46caSbillm 			p = &flagstr[i + 1];
329344cd46caSbillm 			if (bit == ZDB_FLAG_PRINT_BLKPTR)
329444cd46caSbillm 				blkptr_offset = strtoull(p, &p, 16);
329544cd46caSbillm 			if (*p != ':' && *p != '\0') {
329644cd46caSbillm 				(void) printf("***Invalid flag arg: '%s'\n", s);
329744cd46caSbillm 				free(dup);
329844cd46caSbillm 				return;
329944cd46caSbillm 			}
330044cd46caSbillm 		}
330144cd46caSbillm 	}
330244cd46caSbillm 
330344cd46caSbillm 	vd = zdb_vdev_lookup(spa->spa_root_vdev, vdev);
330444cd46caSbillm 	if (vd == NULL) {
330544cd46caSbillm 		(void) printf("***Invalid vdev: %s\n", vdev);
330644cd46caSbillm 		free(dup);
330744cd46caSbillm 		return;
330844cd46caSbillm 	} else {
330944cd46caSbillm 		if (vd->vdev_path)
3310b24ab676SJeff Bonwick 			(void) fprintf(stderr, "Found vdev: %s\n",
3311b24ab676SJeff Bonwick 			    vd->vdev_path);
331244cd46caSbillm 		else
3313b24ab676SJeff Bonwick 			(void) fprintf(stderr, "Found vdev type: %s\n",
331444cd46caSbillm 			    vd->vdev_ops->vdev_op_type);
331544cd46caSbillm 	}
331644cd46caSbillm 
3317b24ab676SJeff Bonwick 	psize = size;
3318b24ab676SJeff Bonwick 	lsize = size;
331944cd46caSbillm 
3320b24ab676SJeff Bonwick 	pbuf = umem_alloc(SPA_MAXBLOCKSIZE, UMEM_NOFAIL);
3321b24ab676SJeff Bonwick 	lbuf = umem_alloc(SPA_MAXBLOCKSIZE, UMEM_NOFAIL);
3322b24ab676SJeff Bonwick 
3323b24ab676SJeff Bonwick 	BP_ZERO(bp);
3324b24ab676SJeff Bonwick 
3325b24ab676SJeff Bonwick 	DVA_SET_VDEV(&dva[0], vd->vdev_id);
3326b24ab676SJeff Bonwick 	DVA_SET_OFFSET(&dva[0], offset);
3327b24ab676SJeff Bonwick 	DVA_SET_GANG(&dva[0], !!(flags & ZDB_FLAG_GBH));
3328b24ab676SJeff Bonwick 	DVA_SET_ASIZE(&dva[0], vdev_psize_to_asize(vd, psize));
3329b24ab676SJeff Bonwick 
3330b24ab676SJeff Bonwick 	BP_SET_BIRTH(bp, TXG_INITIAL, TXG_INITIAL);
3331b24ab676SJeff Bonwick 
3332b24ab676SJeff Bonwick 	BP_SET_LSIZE(bp, lsize);
3333b24ab676SJeff Bonwick 	BP_SET_PSIZE(bp, psize);
3334b24ab676SJeff Bonwick 	BP_SET_COMPRESS(bp, ZIO_COMPRESS_OFF);
3335b24ab676SJeff Bonwick 	BP_SET_CHECKSUM(bp, ZIO_CHECKSUM_OFF);
3336b24ab676SJeff Bonwick 	BP_SET_TYPE(bp, DMU_OT_NONE);
3337b24ab676SJeff Bonwick 	BP_SET_LEVEL(bp, 0);
3338b24ab676SJeff Bonwick 	BP_SET_DEDUP(bp, 0);
3339b24ab676SJeff Bonwick 	BP_SET_BYTEORDER(bp, ZFS_HOST_BYTEORDER);
334044cd46caSbillm 
3341e14bb325SJeff Bonwick 	spa_config_enter(spa, SCL_STATE, FTAG, RW_READER);
334244cd46caSbillm 	zio = zio_root(spa, NULL, NULL, 0);
3343b24ab676SJeff Bonwick 
3344b24ab676SJeff Bonwick 	if (vd == vd->vdev_top) {
3345b24ab676SJeff Bonwick 		/*
3346b24ab676SJeff Bonwick 		 * Treat this as a normal block read.
3347b24ab676SJeff Bonwick 		 */
3348b24ab676SJeff Bonwick 		zio_nowait(zio_read(zio, spa, bp, pbuf, psize, NULL, NULL,
3349b24ab676SJeff Bonwick 		    ZIO_PRIORITY_SYNC_READ,
3350b24ab676SJeff Bonwick 		    ZIO_FLAG_CANFAIL | ZIO_FLAG_RAW, NULL));
3351b24ab676SJeff Bonwick 	} else {
3352b24ab676SJeff Bonwick 		/*
3353b24ab676SJeff Bonwick 		 * Treat this as a vdev child I/O.
3354b24ab676SJeff Bonwick 		 */
3355b24ab676SJeff Bonwick 		zio_nowait(zio_vdev_child_io(zio, bp, vd, offset, pbuf, psize,
3356b24ab676SJeff Bonwick 		    ZIO_TYPE_READ, ZIO_PRIORITY_SYNC_READ,
3357b24ab676SJeff Bonwick 		    ZIO_FLAG_DONT_CACHE | ZIO_FLAG_DONT_QUEUE |
3358b24ab676SJeff Bonwick 		    ZIO_FLAG_DONT_PROPAGATE | ZIO_FLAG_DONT_RETRY |
3359b24ab676SJeff Bonwick 		    ZIO_FLAG_CANFAIL | ZIO_FLAG_RAW, NULL, NULL));
3360b24ab676SJeff Bonwick 	}
3361b24ab676SJeff Bonwick 
336244cd46caSbillm 	error = zio_wait(zio);
3363e14bb325SJeff Bonwick 	spa_config_exit(spa, SCL_STATE, FTAG);
336444cd46caSbillm 
336544cd46caSbillm 	if (error) {
336644cd46caSbillm 		(void) printf("Read of %s failed, error: %d\n", thing, error);
336744cd46caSbillm 		goto out;
336844cd46caSbillm 	}
336944cd46caSbillm 
3370b24ab676SJeff Bonwick 	if (flags & ZDB_FLAG_DECOMPRESS) {
3371b24ab676SJeff Bonwick 		/*
3372b24ab676SJeff Bonwick 		 * We don't know how the data was compressed, so just try
3373b24ab676SJeff Bonwick 		 * every decompress function at every inflated blocksize.
3374b24ab676SJeff Bonwick 		 */
3375b24ab676SJeff Bonwick 		enum zio_compress c;
3376b24ab676SJeff Bonwick 		void *pbuf2 = umem_alloc(SPA_MAXBLOCKSIZE, UMEM_NOFAIL);
3377b24ab676SJeff Bonwick 		void *lbuf2 = umem_alloc(SPA_MAXBLOCKSIZE, UMEM_NOFAIL);
3378b24ab676SJeff Bonwick 
3379b24ab676SJeff Bonwick 		bcopy(pbuf, pbuf2, psize);
3380b24ab676SJeff Bonwick 
3381b24ab676SJeff Bonwick 		VERIFY(random_get_pseudo_bytes((uint8_t *)pbuf + psize,
3382b24ab676SJeff Bonwick 		    SPA_MAXBLOCKSIZE - psize) == 0);
3383b24ab676SJeff Bonwick 
3384b24ab676SJeff Bonwick 		VERIFY(random_get_pseudo_bytes((uint8_t *)pbuf2 + psize,
3385b24ab676SJeff Bonwick 		    SPA_MAXBLOCKSIZE - psize) == 0);
3386b24ab676SJeff Bonwick 
3387b24ab676SJeff Bonwick 		for (lsize = SPA_MAXBLOCKSIZE; lsize > psize;
3388b24ab676SJeff Bonwick 		    lsize -= SPA_MINBLOCKSIZE) {
3389b24ab676SJeff Bonwick 			for (c = 0; c < ZIO_COMPRESS_FUNCTIONS; c++) {
3390b24ab676SJeff Bonwick 				if (zio_decompress_data(c, pbuf, lbuf,
3391b24ab676SJeff Bonwick 				    psize, lsize) == 0 &&
3392b24ab676SJeff Bonwick 				    zio_decompress_data(c, pbuf2, lbuf2,
3393b24ab676SJeff Bonwick 				    psize, lsize) == 0 &&
3394b24ab676SJeff Bonwick 				    bcmp(lbuf, lbuf2, lsize) == 0)
3395b24ab676SJeff Bonwick 					break;
3396b24ab676SJeff Bonwick 			}
3397b24ab676SJeff Bonwick 			if (c != ZIO_COMPRESS_FUNCTIONS)
3398b24ab676SJeff Bonwick 				break;
3399b24ab676SJeff Bonwick 			lsize -= SPA_MINBLOCKSIZE;
3400b24ab676SJeff Bonwick 		}
3401b24ab676SJeff Bonwick 
3402b24ab676SJeff Bonwick 		umem_free(pbuf2, SPA_MAXBLOCKSIZE);
3403b24ab676SJeff Bonwick 		umem_free(lbuf2, SPA_MAXBLOCKSIZE);
3404b24ab676SJeff Bonwick 
3405b24ab676SJeff Bonwick 		if (lsize <= psize) {
3406b24ab676SJeff Bonwick 			(void) printf("Decompress of %s failed\n", thing);
3407b24ab676SJeff Bonwick 			goto out;
3408b24ab676SJeff Bonwick 		}
3409b24ab676SJeff Bonwick 		buf = lbuf;
3410b24ab676SJeff Bonwick 		size = lsize;
3411b24ab676SJeff Bonwick 	} else {
3412b24ab676SJeff Bonwick 		buf = pbuf;
3413b24ab676SJeff Bonwick 		size = psize;
3414b24ab676SJeff Bonwick 	}
3415b24ab676SJeff Bonwick 
341644cd46caSbillm 	if (flags & ZDB_FLAG_PRINT_BLKPTR)
341744cd46caSbillm 		zdb_print_blkptr((blkptr_t *)(void *)
341844cd46caSbillm 		    ((uintptr_t)buf + (uintptr_t)blkptr_offset), flags);
341944cd46caSbillm 	else if (flags & ZDB_FLAG_RAW)
342044cd46caSbillm 		zdb_dump_block_raw(buf, size, flags);
342144cd46caSbillm 	else if (flags & ZDB_FLAG_INDIRECT)
342244cd46caSbillm 		zdb_dump_indirect((blkptr_t *)buf, size / sizeof (blkptr_t),
342344cd46caSbillm 		    flags);
342444cd46caSbillm 	else if (flags & ZDB_FLAG_GBH)
342544cd46caSbillm 		zdb_dump_gbh(buf, flags);
342644cd46caSbillm 	else
342744cd46caSbillm 		zdb_dump_block(thing, buf, size, flags);
342844cd46caSbillm 
342944cd46caSbillm out:
3430b24ab676SJeff Bonwick 	umem_free(pbuf, SPA_MAXBLOCKSIZE);
3431b24ab676SJeff Bonwick 	umem_free(lbuf, SPA_MAXBLOCKSIZE);
343244cd46caSbillm 	free(dup);
343344cd46caSbillm }
343444cd46caSbillm 
3435de6628f0Sck static boolean_t
34363ad6c7f9SVictor Latushkin pool_match(nvlist_t *cfg, char *tgt)
3437de6628f0Sck {
34383ad6c7f9SVictor Latushkin 	uint64_t v, guid = strtoull(tgt, NULL, 0);
3439de6628f0Sck 	char *s;
3440de6628f0Sck 
3441de6628f0Sck 	if (guid != 0) {
34423ad6c7f9SVictor Latushkin 		if (nvlist_lookup_uint64(cfg, ZPOOL_CONFIG_POOL_GUID, &v) == 0)
34433ad6c7f9SVictor Latushkin 			return (v == guid);
3444de6628f0Sck 	} else {
34453ad6c7f9SVictor Latushkin 		if (nvlist_lookup_string(cfg, ZPOOL_CONFIG_POOL_NAME, &s) == 0)
34463ad6c7f9SVictor Latushkin 			return (strcmp(s, tgt) == 0);
3447de6628f0Sck 	}
34483ad6c7f9SVictor Latushkin 	return (B_FALSE);
3449de6628f0Sck }
3450de6628f0Sck 
34513ad6c7f9SVictor Latushkin static char *
34523ad6c7f9SVictor Latushkin find_zpool(char **target, nvlist_t **configp, int dirc, char **dirv)
3453de6628f0Sck {
3454de6628f0Sck 	nvlist_t *pools;
3455de6628f0Sck 	nvlist_t *match = NULL;
34563ad6c7f9SVictor Latushkin 	char *name = NULL;
34573ad6c7f9SVictor Latushkin 	char *sepp = NULL;
34583ad6c7f9SVictor Latushkin 	char sep;
34593ad6c7f9SVictor Latushkin 	int count = 0;
3460d41c4376SMark J Musante 	importargs_t args = { 0 };
3461d41c4376SMark J Musante 
3462d41c4376SMark J Musante 	args.paths = dirc;
3463d41c4376SMark J Musante 	args.path = dirv;
3464d41c4376SMark J Musante 	args.can_be_active = B_TRUE;
3465de6628f0Sck 
34663ad6c7f9SVictor Latushkin 	if ((sepp = strpbrk(*target, "/@")) != NULL) {
34673ad6c7f9SVictor Latushkin 		sep = *sepp;
34683ad6c7f9SVictor Latushkin 		*sepp = '\0';
34693ad6c7f9SVictor Latushkin 	}
34703ad6c7f9SVictor Latushkin 
3471d41c4376SMark J Musante 	pools = zpool_search_import(g_zfs, &args);
3472de6628f0Sck 
3473de6628f0Sck 	if (pools != NULL) {
3474de6628f0Sck 		nvpair_t *elem = NULL;
3475de6628f0Sck 		while ((elem = nvlist_next_nvpair(pools, elem)) != NULL) {
3476de6628f0Sck 			verify(nvpair_value_nvlist(elem, configp) == 0);
34773ad6c7f9SVictor Latushkin 			if (pool_match(*configp, *target)) {
34783ad6c7f9SVictor Latushkin 				count++;
3479de6628f0Sck 				if (match != NULL) {
34803ad6c7f9SVictor Latushkin 					/* print previously found config */
34813ad6c7f9SVictor Latushkin 					if (name != NULL) {
34823ad6c7f9SVictor Latushkin 						(void) printf("%s\n", name);
34833ad6c7f9SVictor Latushkin 						dump_nvlist(match, 8);
34843ad6c7f9SVictor Latushkin 						name = NULL;
34853ad6c7f9SVictor Latushkin 					}
34863ad6c7f9SVictor Latushkin 					(void) printf("%s\n",
34873ad6c7f9SVictor Latushkin 					    nvpair_name(elem));
34883ad6c7f9SVictor Latushkin 					dump_nvlist(*configp, 8);
3489de6628f0Sck 				} else {
3490de6628f0Sck 					match = *configp;
34913ad6c7f9SVictor Latushkin 					name = nvpair_name(elem);
3492de6628f0Sck 				}
3493de6628f0Sck 			}
3494de6628f0Sck 		}
3495de6628f0Sck 	}
34963ad6c7f9SVictor Latushkin 	if (count > 1)
34973ad6c7f9SVictor Latushkin 		(void) fatal("\tMatched %d pools - use pool GUID "
34983ad6c7f9SVictor Latushkin 		    "instead of pool name or \n"
34993ad6c7f9SVictor Latushkin 		    "\tpool name part of a dataset name to select pool", count);
35003ad6c7f9SVictor Latushkin 
35013ad6c7f9SVictor Latushkin 	if (sepp)
35023ad6c7f9SVictor Latushkin 		*sepp = sep;
35033ad6c7f9SVictor Latushkin 	/*
35043ad6c7f9SVictor Latushkin 	 * If pool GUID was specified for pool id, replace it with pool name
35053ad6c7f9SVictor Latushkin 	 */
35063ad6c7f9SVictor Latushkin 	if (name && (strstr(*target, name) != *target)) {
35073ad6c7f9SVictor Latushkin 		int sz = 1 + strlen(name) + ((sepp) ? strlen(sepp) : 0);
35083ad6c7f9SVictor Latushkin 
35093ad6c7f9SVictor Latushkin 		*target = umem_alloc(sz, UMEM_NOFAIL);
35103ad6c7f9SVictor Latushkin 		(void) snprintf(*target, sz, "%s%s", name, sepp ? sepp : "");
35113ad6c7f9SVictor Latushkin 	}
3512de6628f0Sck 
35133ad6c7f9SVictor Latushkin 	*configp = name ? match : NULL;
3514de6628f0Sck 
35153ad6c7f9SVictor Latushkin 	return (name);
3516de6628f0Sck }
3517de6628f0Sck 
3518fa9e4066Sahrens int
3519fa9e4066Sahrens main(int argc, char **argv)
3520fa9e4066Sahrens {
3521fa9e4066Sahrens 	int i, c;
3522fa9e4066Sahrens 	struct rlimit rl = { 1024, 1024 };
35233ad6c7f9SVictor Latushkin 	spa_t *spa = NULL;
3524fa9e4066Sahrens 	objset_t *os = NULL;
3525fa9e4066Sahrens 	int dump_all = 1;
3526fa9e4066Sahrens 	int verbose = 0;
3527c8ee1847SVictor Latushkin 	int error = 0;
35283ad6c7f9SVictor Latushkin 	char **searchdirs = NULL;
35293ad6c7f9SVictor Latushkin 	int nsearch = 0;
35303ad6c7f9SVictor Latushkin 	char *target;
3531468c413aSTim Haley 	nvlist_t *policy = NULL;
3532468c413aSTim Haley 	uint64_t max_txg = UINT64_MAX;
3533c8ee1847SVictor Latushkin 	int rewind = ZPOOL_NEVER_REWIND;
3534ae24175bSCyril Plisko 	char *spa_config_path_env;
3535fa9e4066Sahrens 
3536fa9e4066Sahrens 	(void) setrlimit(RLIMIT_NOFILE, &rl);
3537004388ebScasper 	(void) enable_extended_FILE_stdio(-1, -1);
3538fa9e4066Sahrens 
3539fa9e4066Sahrens 	dprintf_setup(&argc, argv);
3540fa9e4066Sahrens 
3541ae24175bSCyril Plisko 	/*
3542ae24175bSCyril Plisko 	 * If there is an environment variable SPA_CONFIG_PATH it overrides
3543ae24175bSCyril Plisko 	 * default spa_config_path setting. If -U flag is specified it will
3544ae24175bSCyril Plisko 	 * override this environment variable settings once again.
3545ae24175bSCyril Plisko 	 */
3546ae24175bSCyril Plisko 	spa_config_path_env = getenv("SPA_CONFIG_PATH");
3547ae24175bSCyril Plisko 	if (spa_config_path_env != NULL)
3548ae24175bSCyril Plisko 		spa_config_path = spa_config_path_env;
3549ae24175bSCyril Plisko 
3550df15e419SMatthew Ahrens 	while ((c = getopt(argc, argv,
35512e4c9986SGeorge Wilson 	    "bcdhilmMI:suCDRSAFLXx:evp:t:U:P")) != -1) {
3552fa9e4066Sahrens 		switch (c) {
3553fa9e4066Sahrens 		case 'b':
3554fa9e4066Sahrens 		case 'c':
3555b24ab676SJeff Bonwick 		case 'd':
3556b24ab676SJeff Bonwick 		case 'h':
3557b24ab676SJeff Bonwick 		case 'i':
3558b24ab676SJeff Bonwick 		case 'l':
3559d6e555bdSGeorge Wilson 		case 'm':
3560fa9e4066Sahrens 		case 's':
3561b24ab676SJeff Bonwick 		case 'u':
3562fa9e4066Sahrens 		case 'C':
3563b24ab676SJeff Bonwick 		case 'D':
35642e4c9986SGeorge Wilson 		case 'M':
356544cd46caSbillm 		case 'R':
3566b24ab676SJeff Bonwick 		case 'S':
3567fa9e4066Sahrens 			dump_opt[c]++;
3568fa9e4066Sahrens 			dump_all = 0;
3569fa9e4066Sahrens 			break;
3570feef89cfSVictor Latushkin 		case 'A':
3571c8ee1847SVictor Latushkin 		case 'F':
357282a0a985SVictor Latushkin 		case 'L':
3573c8ee1847SVictor Latushkin 		case 'X':
35743ad6c7f9SVictor Latushkin 		case 'e':
35753f9d6ad7SLin Ling 		case 'P':
357682a0a985SVictor Latushkin 			dump_opt[c]++;
357782a0a985SVictor Latushkin 			break;
35782e4c9986SGeorge Wilson 		case 'I':
357931d7e8faSGeorge Wilson 			max_inflight = strtoull(optarg, NULL, 0);
358031d7e8faSGeorge Wilson 			if (max_inflight == 0) {
358131d7e8faSGeorge Wilson 				(void) fprintf(stderr, "maximum number "
358231d7e8faSGeorge Wilson 				    "of inflight I/Os must be greater "
358331d7e8faSGeorge Wilson 				    "than 0\n");
358431d7e8faSGeorge Wilson 				usage();
358531d7e8faSGeorge Wilson 			}
358631d7e8faSGeorge Wilson 			break;
3587de6628f0Sck 		case 'p':
35883ad6c7f9SVictor Latushkin 			if (searchdirs == NULL) {
35893ad6c7f9SVictor Latushkin 				searchdirs = umem_alloc(sizeof (char *),
35903ad6c7f9SVictor Latushkin 				    UMEM_NOFAIL);
35913ad6c7f9SVictor Latushkin 			} else {
35923ad6c7f9SVictor Latushkin 				char **tmp = umem_alloc((nsearch + 1) *
35933ad6c7f9SVictor Latushkin 				    sizeof (char *), UMEM_NOFAIL);
35943ad6c7f9SVictor Latushkin 				bcopy(searchdirs, tmp, nsearch *
35953ad6c7f9SVictor Latushkin 				    sizeof (char *));
35963ad6c7f9SVictor Latushkin 				umem_free(searchdirs,
35973ad6c7f9SVictor Latushkin 				    nsearch * sizeof (char *));
35983ad6c7f9SVictor Latushkin 				searchdirs = tmp;
35993ad6c7f9SVictor Latushkin 			}
36003ad6c7f9SVictor Latushkin 			searchdirs[nsearch++] = optarg;
3601de6628f0Sck 			break;
36022e551927SVictor Latushkin 		case 't':
3603468c413aSTim Haley 			max_txg = strtoull(optarg, NULL, 0);
3604468c413aSTim Haley 			if (max_txg < TXG_INITIAL) {
36052e551927SVictor Latushkin 				(void) fprintf(stderr, "incorrect txg "
36062e551927SVictor Latushkin 				    "specified: %s\n", optarg);
36072e551927SVictor Latushkin 				usage();
36082e551927SVictor Latushkin 			}
36092e551927SVictor Latushkin 			break;
3610b24ab676SJeff Bonwick 		case 'U':
3611b24ab676SJeff Bonwick 			spa_config_path = optarg;
3612b24ab676SJeff Bonwick 			break;
36132e4c9986SGeorge Wilson 		case 'v':
36142e4c9986SGeorge Wilson 			verbose++;
36152e4c9986SGeorge Wilson 			break;
36162e4c9986SGeorge Wilson 		case 'x':
36172e4c9986SGeorge Wilson 			vn_dumpdir = optarg;
36182e4c9986SGeorge Wilson 			break;
3619fa9e4066Sahrens 		default:
3620fa9e4066Sahrens 			usage();
3621fa9e4066Sahrens 			break;
3622fa9e4066Sahrens 		}
3623fa9e4066Sahrens 	}
3624fa9e4066Sahrens 
36253ad6c7f9SVictor Latushkin 	if (!dump_opt['e'] && searchdirs != NULL) {
362688b7b0f2SMatthew Ahrens 		(void) fprintf(stderr, "-p option requires use of -e\n");
362788b7b0f2SMatthew Ahrens 		usage();
362888b7b0f2SMatthew Ahrens 	}
3629de6628f0Sck 
363006be9802SMatthew Ahrens 	/*
363106be9802SMatthew Ahrens 	 * ZDB does not typically re-read blocks; therefore limit the ARC
363206be9802SMatthew Ahrens 	 * to 256 MB, which can be used entirely for metadata.
363306be9802SMatthew Ahrens 	 */
363406be9802SMatthew Ahrens 	zfs_arc_max = zfs_arc_meta_limit = 256 * 1024 * 1024;
363506be9802SMatthew Ahrens 
3636f7950bf1SMatthew Ahrens 	/*
3637f7950bf1SMatthew Ahrens 	 * "zdb -c" uses checksum-verifying scrub i/os which are async reads.
3638f7950bf1SMatthew Ahrens 	 * "zdb -b" uses traversal prefetch which uses async reads.
3639f7950bf1SMatthew Ahrens 	 * For good performance, let several of them be active at once.
3640f7950bf1SMatthew Ahrens 	 */
3641f7950bf1SMatthew Ahrens 	zfs_vdev_async_read_max_active = 10;
3642f7950bf1SMatthew Ahrens 
3643fa9e4066Sahrens 	kernel_init(FREAD);
3644de6628f0Sck 	g_zfs = libzfs_init();
364591ebeef5Sahrens 	ASSERT(g_zfs != NULL);
3646fa9e4066Sahrens 
3647b24ab676SJeff Bonwick 	if (dump_all)
3648b24ab676SJeff Bonwick 		verbose = MAX(verbose, 1);
3649b24ab676SJeff Bonwick 
3650fa9e4066Sahrens 	for (c = 0; c < 256; c++) {
36513f9d6ad7SLin Ling 		if (dump_all && !strchr("elAFLRSXP", c))
3652fa9e4066Sahrens 			dump_opt[c] = 1;
3653fa9e4066Sahrens 		if (dump_opt[c])
3654fa9e4066Sahrens 			dump_opt[c] += verbose;
3655fa9e4066Sahrens 	}
3656fa9e4066Sahrens 
3657feef89cfSVictor Latushkin 	aok = (dump_opt['A'] == 1) || (dump_opt['A'] > 2);
3658feef89cfSVictor Latushkin 	zfs_recover = (dump_opt['A'] > 1);
3659feef89cfSVictor Latushkin 
3660fa9e4066Sahrens 	argc -= optind;
3661fa9e4066Sahrens 	argv += optind;
3662fa9e4066Sahrens 
366307428bdfSVictor Latushkin 	if (argc < 2 && dump_opt['R'])
366407428bdfSVictor Latushkin 		usage();
3665fa9e4066Sahrens 	if (argc < 1) {
36663ad6c7f9SVictor Latushkin 		if (!dump_opt['e'] && dump_opt['C']) {
3667e829d913Sck 			dump_cachefile(spa_config_path);
3668fa9e4066Sahrens 			return (0);
3669fa9e4066Sahrens 		}
3670fa9e4066Sahrens 		usage();
3671fa9e4066Sahrens 	}
3672fa9e4066Sahrens 
3673fa9e4066Sahrens 	if (dump_opt['l']) {
3674fa9e4066Sahrens 		dump_label(argv[0]);
3675fa9e4066Sahrens 		return (0);
3676fa9e4066Sahrens 	}
3677fa9e4066Sahrens 
3678c8ee1847SVictor Latushkin 	if (dump_opt['X'] || dump_opt['F'])
3679c8ee1847SVictor Latushkin 		rewind = ZPOOL_DO_REWIND |
3680c8ee1847SVictor Latushkin 		    (dump_opt['X'] ? ZPOOL_EXTREME_REWIND : 0);
3681c8ee1847SVictor Latushkin 
3682c8ee1847SVictor Latushkin 	if (nvlist_alloc(&policy, NV_UNIQUE_NAME_TYPE, 0) != 0 ||
3683c8ee1847SVictor Latushkin 	    nvlist_add_uint64(policy, ZPOOL_REWIND_REQUEST_TXG, max_txg) != 0 ||
3684c8ee1847SVictor Latushkin 	    nvlist_add_uint32(policy, ZPOOL_REWIND_REQUEST, rewind) != 0)
3685c8ee1847SVictor Latushkin 		fatal("internal error: %s", strerror(ENOMEM));
3686c8ee1847SVictor Latushkin 
3687c5904d13Seschrock 	error = 0;
36883ad6c7f9SVictor Latushkin 	target = argv[0];
3689990b4856Slling 
36903ad6c7f9SVictor Latushkin 	if (dump_opt['e']) {
36913ad6c7f9SVictor Latushkin 		nvlist_t *cfg = NULL;
36923ad6c7f9SVictor Latushkin 		char *name = find_zpool(&target, &cfg, nsearch, searchdirs);
3693990b4856Slling 
36943ad6c7f9SVictor Latushkin 		error = ENOENT;
36953ad6c7f9SVictor Latushkin 		if (name) {
369607428bdfSVictor Latushkin 			if (dump_opt['C'] > 1) {
369707428bdfSVictor Latushkin 				(void) printf("\nConfiguration for import:\n");
369807428bdfSVictor Latushkin 				dump_nvlist(cfg, 8);
369907428bdfSVictor Latushkin 			}
3700c8ee1847SVictor Latushkin 			if (nvlist_add_nvlist(cfg,
3701468c413aSTim Haley 			    ZPOOL_REWIND_POLICY, policy) != 0) {
3702468c413aSTim Haley 				fatal("can't open '%s': %s",
3703468c413aSTim Haley 				    target, strerror(ENOMEM));
3704468c413aSTim Haley 			}
37054b964adaSGeorge Wilson 			if ((error = spa_import(name, cfg, NULL,
37064b964adaSGeorge Wilson 			    ZFS_IMPORT_MISSING_LOG)) != 0) {
37074b964adaSGeorge Wilson 				error = spa_import(name, cfg, NULL,
37084b964adaSGeorge Wilson 				    ZFS_IMPORT_VERBATIM);
37094b964adaSGeorge Wilson 			}
3710de6628f0Sck 		}
3711c5904d13Seschrock 	}
3712c5904d13Seschrock 
3713c5904d13Seschrock 	if (error == 0) {
371407428bdfSVictor Latushkin 		if (strpbrk(target, "/@") == NULL || dump_opt['R']) {
371580eb36f2SGeorge Wilson 			error = spa_open_rewind(target, &spa, FTAG, policy,
371680eb36f2SGeorge Wilson 			    NULL);
37178f18d1faSGeorge Wilson 			if (error) {
37188f18d1faSGeorge Wilson 				/*
37198f18d1faSGeorge Wilson 				 * If we're missing the log device then
37208f18d1faSGeorge Wilson 				 * try opening the pool after clearing the
37218f18d1faSGeorge Wilson 				 * log state.
37228f18d1faSGeorge Wilson 				 */
37238f18d1faSGeorge Wilson 				mutex_enter(&spa_namespace_lock);
37243ad6c7f9SVictor Latushkin 				if ((spa = spa_lookup(target)) != NULL &&
37258f18d1faSGeorge Wilson 				    spa->spa_log_state == SPA_LOG_MISSING) {
37268f18d1faSGeorge Wilson 					spa->spa_log_state = SPA_LOG_CLEAR;
37278f18d1faSGeorge Wilson 					error = 0;
37288f18d1faSGeorge Wilson 				}
37298f18d1faSGeorge Wilson 				mutex_exit(&spa_namespace_lock);
37308f18d1faSGeorge Wilson 
373180eb36f2SGeorge Wilson 				if (!error) {
373280eb36f2SGeorge Wilson 					error = spa_open_rewind(target, &spa,
373380eb36f2SGeorge Wilson 					    FTAG, policy, NULL);
373480eb36f2SGeorge Wilson 				}
37358f18d1faSGeorge Wilson 			}
373607428bdfSVictor Latushkin 		} else {
373707428bdfSVictor Latushkin 			error = dmu_objset_own(target, DMU_OST_ANY,
373807428bdfSVictor Latushkin 			    B_TRUE, FTAG, &os);
3739c5904d13Seschrock 		}
3740fa9e4066Sahrens 	}
374180eb36f2SGeorge Wilson 	nvlist_free(policy);
374280eb36f2SGeorge Wilson 
3743fa9e4066Sahrens 	if (error)
37443ad6c7f9SVictor Latushkin 		fatal("can't open '%s': %s", target, strerror(error));
3745fa9e4066Sahrens 
3746fa9e4066Sahrens 	argv++;
374707428bdfSVictor Latushkin 	argc--;
374807428bdfSVictor Latushkin 	if (!dump_opt['R']) {
374907428bdfSVictor Latushkin 		if (argc > 0) {
375007428bdfSVictor Latushkin 			zopt_objects = argc;
375107428bdfSVictor Latushkin 			zopt_object = calloc(zopt_objects, sizeof (uint64_t));
375207428bdfSVictor Latushkin 			for (i = 0; i < zopt_objects; i++) {
375307428bdfSVictor Latushkin 				errno = 0;
375407428bdfSVictor Latushkin 				zopt_object[i] = strtoull(argv[i], NULL, 0);
375507428bdfSVictor Latushkin 				if (zopt_object[i] == 0 && errno != 0)
375687219db7SVictor Latushkin 					fatal("bad number %s: %s",
375707428bdfSVictor Latushkin 					    argv[i], strerror(errno));
375807428bdfSVictor Latushkin 			}
3759fa9e4066Sahrens 		}
3760e690fb27SChristopher Siden 		if (os != NULL) {
3761e690fb27SChristopher Siden 			dump_dir(os);
3762e690fb27SChristopher Siden 		} else if (zopt_objects > 0 && !dump_opt['m']) {
3763e690fb27SChristopher Siden 			dump_dir(spa->spa_meta_objset);
3764e690fb27SChristopher Siden 		} else {
3765e690fb27SChristopher Siden 			dump_zpool(spa);
3766e690fb27SChristopher Siden 		}
3767fa9e4066Sahrens 	} else {
376807428bdfSVictor Latushkin 		flagbits['b'] = ZDB_FLAG_PRINT_BLKPTR;
376907428bdfSVictor Latushkin 		flagbits['c'] = ZDB_FLAG_CHECKSUM;
377007428bdfSVictor Latushkin 		flagbits['d'] = ZDB_FLAG_DECOMPRESS;
377107428bdfSVictor Latushkin 		flagbits['e'] = ZDB_FLAG_BSWAP;
377207428bdfSVictor Latushkin 		flagbits['g'] = ZDB_FLAG_GBH;
377307428bdfSVictor Latushkin 		flagbits['i'] = ZDB_FLAG_INDIRECT;
377407428bdfSVictor Latushkin 		flagbits['p'] = ZDB_FLAG_PHYS;
377507428bdfSVictor Latushkin 		flagbits['r'] = ZDB_FLAG_RAW;
377607428bdfSVictor Latushkin 
377707428bdfSVictor Latushkin 		for (i = 0; i < argc; i++)
377807428bdfSVictor Latushkin 			zdb_read_block(argv[i], spa);
3779fa9e4066Sahrens 	}
3780fa9e4066Sahrens 
378107428bdfSVictor Latushkin 	(os != NULL) ? dmu_objset_disown(os, FTAG) : spa_close(spa, FTAG);
378207428bdfSVictor Latushkin 
3783e0d35c44Smarks 	fuid_table_destroy();
37840a586ceaSMark Shellenbaum 	sa_loaded = B_FALSE;
3785e0d35c44Smarks 
3786de6628f0Sck 	libzfs_fini(g_zfs);
3787fa9e4066Sahrens 	kernel_fini();
3788fa9e4066Sahrens 
3789fa9e4066Sahrens 	return (0);
3790fa9e4066Sahrens }
3791