xref: /titanic_51/usr/src/cmd/zdb/zdb.c (revision ca0cc3918a1789fa839194af2a9245f801a06b1a)
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.
24*ca0cc391SMatthew Ahrens  * Copyright (c) 2011, 2015 by Delphix. All rights reserved.
25fa9e4066Sahrens  */
26fa9e4066Sahrens 
27fa9e4066Sahrens #include <stdio.h>
28490d05b9SMatthew Ahrens #include <unistd.h>
29004388ebScasper #include <stdio_ext.h>
30fa9e4066Sahrens #include <stdlib.h>
3144cd46caSbillm #include <ctype.h>
32fa9e4066Sahrens #include <sys/zfs_context.h>
33fa9e4066Sahrens #include <sys/spa.h>
34fa9e4066Sahrens #include <sys/spa_impl.h>
35fa9e4066Sahrens #include <sys/dmu.h>
36fa9e4066Sahrens #include <sys/zap.h>
37fa9e4066Sahrens #include <sys/fs/zfs.h>
38fa9e4066Sahrens #include <sys/zfs_znode.h>
390a586ceaSMark Shellenbaum #include <sys/zfs_sa.h>
400a586ceaSMark Shellenbaum #include <sys/sa.h>
410a586ceaSMark Shellenbaum #include <sys/sa_impl.h>
42fa9e4066Sahrens #include <sys/vdev.h>
43fa9e4066Sahrens #include <sys/vdev_impl.h>
44fa9e4066Sahrens #include <sys/metaslab_impl.h>
45fa9e4066Sahrens #include <sys/dmu_objset.h>
46fa9e4066Sahrens #include <sys/dsl_dir.h>
47fa9e4066Sahrens #include <sys/dsl_dataset.h>
48fa9e4066Sahrens #include <sys/dsl_pool.h>
49fa9e4066Sahrens #include <sys/dbuf.h>
50fa9e4066Sahrens #include <sys/zil.h>
51fa9e4066Sahrens #include <sys/zil_impl.h>
52fa9e4066Sahrens #include <sys/stat.h>
53fa9e4066Sahrens #include <sys/resource.h>
54fa9e4066Sahrens #include <sys/dmu_traverse.h>
55fa9e4066Sahrens #include <sys/zio_checksum.h>
56fa9e4066Sahrens #include <sys/zio_compress.h>
57e0d35c44Smarks #include <sys/zfs_fuid.h>
5888b7b0f2SMatthew Ahrens #include <sys/arc.h>
59b24ab676SJeff Bonwick #include <sys/ddt.h>
60ad135b5dSChristopher Siden #include <sys/zfeature.h>
614445fffbSMatthew Ahrens #include <zfs_comutil.h>
62de6628f0Sck153898 #undef ZFS_MAXNAMELEN
63de6628f0Sck153898 #undef verify
64de6628f0Sck153898 #include <libzfs.h>
65fa9e4066Sahrens 
666de8f417SVictor Latushkin #define	ZDB_COMPRESS_NAME(idx) ((idx) < ZIO_COMPRESS_FUNCTIONS ?	\
676de8f417SVictor Latushkin 	zio_compress_table[(idx)].ci_name : "UNKNOWN")
686de8f417SVictor Latushkin #define	ZDB_CHECKSUM_NAME(idx) ((idx) < ZIO_CHECKSUM_FUNCTIONS ?	\
696de8f417SVictor Latushkin 	zio_checksum_table[(idx)].ci_name : "UNKNOWN")
706de8f417SVictor Latushkin #define	ZDB_OT_NAME(idx) ((idx) < DMU_OT_NUMTYPES ?	\
71ad135b5dSChristopher Siden 	dmu_ot[(idx)].ot_name : DMU_OT_IS_VALID(idx) ?	\
72ad135b5dSChristopher Siden 	dmu_ot_byteswap[DMU_OT_BYTESWAP(idx)].ob_name : "UNKNOWN")
73e690fb27SChristopher Siden #define	ZDB_OT_TYPE(idx) ((idx) < DMU_OT_NUMTYPES ? (idx) :		\
74e690fb27SChristopher Siden 	(((idx) == DMU_OTN_ZAP_DATA || (idx) == DMU_OTN_ZAP_METADATA) ?	\
75e690fb27SChristopher Siden 	DMU_OT_ZAP_OTHER : DMU_OT_NUMTYPES))
766de8f417SVictor Latushkin 
77feef89cfSVictor Latushkin #ifndef lint
787fd05ac4SMatthew Ahrens extern boolean_t zfs_recover;
7906be9802SMatthew Ahrens extern uint64_t zfs_arc_max, zfs_arc_meta_limit;
80f7950bf1SMatthew Ahrens extern int zfs_vdev_async_read_max_active;
81feef89cfSVictor Latushkin #else
827fd05ac4SMatthew Ahrens boolean_t zfs_recover;
8306be9802SMatthew Ahrens uint64_t zfs_arc_max, zfs_arc_meta_limit;
84f7950bf1SMatthew Ahrens int zfs_vdev_async_read_max_active;
85feef89cfSVictor Latushkin #endif
86feef89cfSVictor Latushkin 
87fa9e4066Sahrens const char cmdname[] = "zdb";
88fa9e4066Sahrens uint8_t dump_opt[256];
89fa9e4066Sahrens 
90fa9e4066Sahrens typedef void object_viewer_t(objset_t *, uint64_t, void *data, size_t size);
91fa9e4066Sahrens 
92fa9e4066Sahrens extern void dump_intent_log(zilog_t *);
93fa9e4066Sahrens uint64_t *zopt_object = NULL;
94fa9e4066Sahrens int zopt_objects = 0;
95de6628f0Sck153898 libzfs_handle_t *g_zfs;
9606be9802SMatthew Ahrens uint64_t max_inflight = 1000;
97fa9e4066Sahrens 
98732885fcSMatthew Ahrens static void snprintf_blkptr_compact(char *, size_t, const blkptr_t *);
99732885fcSMatthew Ahrens 
100fa9e4066Sahrens /*
101fa9e4066Sahrens  * These libumem hooks provide a reasonable set of defaults for the allocator's
102fa9e4066Sahrens  * debugging facilities.
103fa9e4066Sahrens  */
104fa9e4066Sahrens const char *
105fa9e4066Sahrens _umem_debug_init()
106fa9e4066Sahrens {
107fa9e4066Sahrens 	return ("default,verbose"); /* $UMEM_DEBUG setting */
108fa9e4066Sahrens }
109fa9e4066Sahrens 
110fa9e4066Sahrens const char *
111fa9e4066Sahrens _umem_logging_init(void)
112fa9e4066Sahrens {
113fa9e4066Sahrens 	return ("fail,contents"); /* $UMEM_LOGGING setting */
114fa9e4066Sahrens }
115fa9e4066Sahrens 
116fa9e4066Sahrens static void
117fa9e4066Sahrens usage(void)
118fa9e4066Sahrens {
119fa9e4066Sahrens 	(void) fprintf(stderr,
1202e4c9986SGeorge Wilson 	    "Usage: %s [-CumMdibcsDvhLXFPA] [-t txg] [-e [-p path...]] "
1212e4c9986SGeorge Wilson 	    "[-U config] [-I inflight I/Os] [-x dumpdir] poolname [object...]\n"
12231d7e8faSGeorge Wilson 	    "       %s [-divPA] [-e -p path...] [-U config] dataset "
12331d7e8faSGeorge Wilson 	    "[object...]\n"
1242e4c9986SGeorge Wilson 	    "       %s -mM [-LXFPA] [-t txg] [-e [-p path...]] [-U config] "
12590e894e2SRichard Lowe 	    "poolname [vdev [metaslab...]]\n"
12690e894e2SRichard Lowe 	    "       %s -R [-A] [-e [-p path...]] poolname "
12790e894e2SRichard Lowe 	    "vdev:offset:size[:flags]\n"
12831d7e8faSGeorge Wilson 	    "       %s -S [-PA] [-e [-p path...]] [-U config] poolname\n"
12990e894e2SRichard Lowe 	    "       %s -l [-uA] device\n"
13090e894e2SRichard Lowe 	    "       %s -C [-A] [-U config]\n\n",
131bbfd46c4SJeff Bonwick 	    cmdname, cmdname, cmdname, cmdname, cmdname, cmdname, cmdname);
132fa9e4066Sahrens 
1333ad6c7f9SVictor Latushkin 	(void) fprintf(stderr, "    Dataset name must include at least one "
1343ad6c7f9SVictor Latushkin 	    "separator character '/' or '@'\n");
1353ad6c7f9SVictor Latushkin 	(void) fprintf(stderr, "    If dataset name is specified, only that "
1363ad6c7f9SVictor Latushkin 	    "dataset is dumped\n");
1373ad6c7f9SVictor Latushkin 	(void) fprintf(stderr, "    If object numbers are specified, only "
1383ad6c7f9SVictor Latushkin 	    "those objects are dumped\n\n");
1393ad6c7f9SVictor Latushkin 	(void) fprintf(stderr, "    Options to control amount of output:\n");
140fa9e4066Sahrens 	(void) fprintf(stderr, "        -u uberblock\n");
1413ad6c7f9SVictor Latushkin 	(void) fprintf(stderr, "        -d dataset(s)\n");
1423ad6c7f9SVictor Latushkin 	(void) fprintf(stderr, "        -i intent logs\n");
14307428bdfSVictor Latushkin 	(void) fprintf(stderr, "        -C config (or cachefile if alone)\n");
1448f18d1faSGeorge Wilson 	(void) fprintf(stderr, "        -h pool history\n");
145fa9e4066Sahrens 	(void) fprintf(stderr, "        -b block statistics\n");
146d6e555bdSGeorge Wilson 	(void) fprintf(stderr, "        -m metaslabs\n");
1472e4c9986SGeorge Wilson 	(void) fprintf(stderr, "        -M metaslab groups\n");
1486365109dSVictor Latushkin 	(void) fprintf(stderr, "        -c checksum all metadata (twice for "
1496365109dSVictor Latushkin 	    "all data) blocks\n");
150fa9e4066Sahrens 	(void) fprintf(stderr, "        -s report stats on zdb's I/O\n");
151f0ba89beSJeff Bonwick 	(void) fprintf(stderr, "        -D dedup statistics\n");
152b24ab676SJeff Bonwick 	(void) fprintf(stderr, "        -S simulate dedup to measure effect\n");
153fa9e4066Sahrens 	(void) fprintf(stderr, "        -v verbose (applies to all others)\n");
154fa9e4066Sahrens 	(void) fprintf(stderr, "        -l dump label contents\n");
15582a0a985SVictor Latushkin 	(void) fprintf(stderr, "        -L disable leak tracking (do not "
15682a0a985SVictor Latushkin 	    "load spacemaps)\n");
15744cd46caSbillm 	(void) fprintf(stderr, "        -R read and display block from a "
1583ad6c7f9SVictor Latushkin 	    "device\n\n");
1593ad6c7f9SVictor Latushkin 	(void) fprintf(stderr, "    Below options are intended for use "
160df15e419SMatthew Ahrens 	    "with other options:\n");
161feef89cfSVictor Latushkin 	(void) fprintf(stderr, "        -A ignore assertions (-A), enable "
162feef89cfSVictor Latushkin 	    "panic recovery (-AA) or both (-AAA)\n");
163c8ee1847SVictor Latushkin 	(void) fprintf(stderr, "        -F attempt automatic rewind within "
164c8ee1847SVictor Latushkin 	    "safe range of transaction groups\n");
1653ad6c7f9SVictor Latushkin 	(void) fprintf(stderr, "        -U <cachefile_path> -- use alternate "
1663ad6c7f9SVictor Latushkin 	    "cachefile\n");
167c8ee1847SVictor Latushkin 	(void) fprintf(stderr, "        -X attempt extreme rewind (does not "
168c8ee1847SVictor Latushkin 	    "work with dataset)\n");
1693ad6c7f9SVictor Latushkin 	(void) fprintf(stderr, "        -e pool is exported/destroyed/"
1703ad6c7f9SVictor Latushkin 	    "has altroot/not in a cachefile\n");
1713ad6c7f9SVictor Latushkin 	(void) fprintf(stderr, "        -p <path> -- use one or more with "
1723ad6c7f9SVictor Latushkin 	    "-e to specify path to vdev dir\n");
173df15e419SMatthew Ahrens 	(void) fprintf(stderr, "        -x <dumpdir> -- "
174df15e419SMatthew Ahrens 	    "dump all read blocks into specified directory\n");
17590e894e2SRichard Lowe 	(void) fprintf(stderr, "        -P print numbers in parseable form\n");
1763ad6c7f9SVictor Latushkin 	(void) fprintf(stderr, "        -t <txg> -- highest txg to use when "
1772e551927SVictor Latushkin 	    "searching for uberblocks\n");
1782e4c9986SGeorge Wilson 	(void) fprintf(stderr, "        -I <number of inflight I/Os> -- "
179df15e419SMatthew Ahrens 	    "specify the maximum number of "
180df15e419SMatthew Ahrens 	    "checksumming I/Os [default is 200]\n");
181fa9e4066Sahrens 	(void) fprintf(stderr, "Specify an option more than once (e.g. -bb) "
182fa9e4066Sahrens 	    "to make only that option verbose\n");
183fa9e4066Sahrens 	(void) fprintf(stderr, "Default is to dump everything non-verbosely\n");
184fa9e4066Sahrens 	exit(1);
185fa9e4066Sahrens }
186fa9e4066Sahrens 
187ccba0801SRich Morris /*
188ccba0801SRich Morris  * Called for usage errors that are discovered after a call to spa_open(),
189ccba0801SRich Morris  * dmu_bonus_hold(), or pool_match().  abort() is called for other errors.
190ccba0801SRich Morris  */
191ccba0801SRich Morris 
192fa9e4066Sahrens static void
193fa9e4066Sahrens fatal(const char *fmt, ...)
194fa9e4066Sahrens {
195fa9e4066Sahrens 	va_list ap;
196fa9e4066Sahrens 
197fa9e4066Sahrens 	va_start(ap, fmt);
198fa9e4066Sahrens 	(void) fprintf(stderr, "%s: ", cmdname);
199fa9e4066Sahrens 	(void) vfprintf(stderr, fmt, ap);
200fa9e4066Sahrens 	va_end(ap);
201fa9e4066Sahrens 	(void) fprintf(stderr, "\n");
202fa9e4066Sahrens 
203ccba0801SRich Morris 	exit(1);
204fa9e4066Sahrens }
205fa9e4066Sahrens 
206fa9e4066Sahrens /* ARGSUSED */
207fa9e4066Sahrens static void
208fa9e4066Sahrens dump_packed_nvlist(objset_t *os, uint64_t object, void *data, size_t size)
209fa9e4066Sahrens {
210fa9e4066Sahrens 	nvlist_t *nv;
211fa9e4066Sahrens 	size_t nvsize = *(uint64_t *)data;
212fa9e4066Sahrens 	char *packed = umem_alloc(nvsize, UMEM_NOFAIL);
213fa9e4066Sahrens 
2147bfdf011SNeil Perrin 	VERIFY(0 == dmu_read(os, object, 0, nvsize, packed, DMU_READ_PREFETCH));
215fa9e4066Sahrens 
216fa9e4066Sahrens 	VERIFY(nvlist_unpack(packed, nvsize, &nv, 0) == 0);
217fa9e4066Sahrens 
218fa9e4066Sahrens 	umem_free(packed, nvsize);
219fa9e4066Sahrens 
220fa9e4066Sahrens 	dump_nvlist(nv, 8);
221fa9e4066Sahrens 
222fa9e4066Sahrens 	nvlist_free(nv);
223fa9e4066Sahrens }
224fa9e4066Sahrens 
2254445fffbSMatthew Ahrens /* ARGSUSED */
2264445fffbSMatthew Ahrens static void
2274445fffbSMatthew Ahrens dump_history_offsets(objset_t *os, uint64_t object, void *data, size_t size)
2284445fffbSMatthew Ahrens {
2294445fffbSMatthew Ahrens 	spa_history_phys_t *shp = data;
2304445fffbSMatthew Ahrens 
2314445fffbSMatthew Ahrens 	if (shp == NULL)
2324445fffbSMatthew Ahrens 		return;
2334445fffbSMatthew Ahrens 
2344445fffbSMatthew Ahrens 	(void) printf("\t\tpool_create_len = %llu\n",
2354445fffbSMatthew Ahrens 	    (u_longlong_t)shp->sh_pool_create_len);
2364445fffbSMatthew Ahrens 	(void) printf("\t\tphys_max_off = %llu\n",
2374445fffbSMatthew Ahrens 	    (u_longlong_t)shp->sh_phys_max_off);
2384445fffbSMatthew Ahrens 	(void) printf("\t\tbof = %llu\n",
2394445fffbSMatthew Ahrens 	    (u_longlong_t)shp->sh_bof);
2404445fffbSMatthew Ahrens 	(void) printf("\t\teof = %llu\n",
2414445fffbSMatthew Ahrens 	    (u_longlong_t)shp->sh_eof);
2424445fffbSMatthew Ahrens 	(void) printf("\t\trecords_lost = %llu\n",
2434445fffbSMatthew Ahrens 	    (u_longlong_t)shp->sh_records_lost);
2444445fffbSMatthew Ahrens }
2454445fffbSMatthew Ahrens 
2463f9d6ad7SLin Ling static void
2473f9d6ad7SLin Ling zdb_nicenum(uint64_t num, char *buf)
2483f9d6ad7SLin Ling {
2493f9d6ad7SLin Ling 	if (dump_opt['P'])
2503f9d6ad7SLin Ling 		(void) sprintf(buf, "%llu", (longlong_t)num);
2513f9d6ad7SLin Ling 	else
2523f9d6ad7SLin Ling 		nicenum(num, buf);
2533f9d6ad7SLin Ling }
2543f9d6ad7SLin Ling 
255490d05b9SMatthew Ahrens const char histo_stars[] = "****************************************";
256490d05b9SMatthew Ahrens const int histo_width = sizeof (histo_stars) - 1;
257fa9e4066Sahrens 
258fa9e4066Sahrens static void
2590713e232SGeorge Wilson dump_histogram(const uint64_t *histo, int size, int offset)
260fa9e4066Sahrens {
261fa9e4066Sahrens 	int i;
262490d05b9SMatthew Ahrens 	int minidx = size - 1;
263fa9e4066Sahrens 	int maxidx = 0;
264fa9e4066Sahrens 	uint64_t max = 0;
265fa9e4066Sahrens 
266490d05b9SMatthew Ahrens 	for (i = 0; i < size; i++) {
267fa9e4066Sahrens 		if (histo[i] > max)
268fa9e4066Sahrens 			max = histo[i];
269fa9e4066Sahrens 		if (histo[i] > 0 && i > maxidx)
270fa9e4066Sahrens 			maxidx = i;
271fa9e4066Sahrens 		if (histo[i] > 0 && i < minidx)
272fa9e4066Sahrens 			minidx = i;
273fa9e4066Sahrens 	}
274fa9e4066Sahrens 
275490d05b9SMatthew Ahrens 	if (max < histo_width)
276490d05b9SMatthew Ahrens 		max = histo_width;
277fa9e4066Sahrens 
278490d05b9SMatthew Ahrens 	for (i = minidx; i <= maxidx; i++) {
279490d05b9SMatthew Ahrens 		(void) printf("\t\t\t%3u: %6llu %s\n",
2800713e232SGeorge Wilson 		    i + offset, (u_longlong_t)histo[i],
281490d05b9SMatthew Ahrens 		    &histo_stars[(max - histo[i]) * histo_width / max]);
282490d05b9SMatthew Ahrens 	}
283fa9e4066Sahrens }
284fa9e4066Sahrens 
285fa9e4066Sahrens static void
286fa9e4066Sahrens dump_zap_stats(objset_t *os, uint64_t object)
287fa9e4066Sahrens {
288fa9e4066Sahrens 	int error;
289fa9e4066Sahrens 	zap_stats_t zs;
290fa9e4066Sahrens 
291fa9e4066Sahrens 	error = zap_get_stats(os, object, &zs);
292fa9e4066Sahrens 	if (error)
293fa9e4066Sahrens 		return;
294fa9e4066Sahrens 
295fa9e4066Sahrens 	if (zs.zs_ptrtbl_len == 0) {
296fa9e4066Sahrens 		ASSERT(zs.zs_num_blocks == 1);
297fa9e4066Sahrens 		(void) printf("\tmicrozap: %llu bytes, %llu entries\n",
298fa9e4066Sahrens 		    (u_longlong_t)zs.zs_blocksize,
299fa9e4066Sahrens 		    (u_longlong_t)zs.zs_num_entries);
300fa9e4066Sahrens 		return;
301fa9e4066Sahrens 	}
302fa9e4066Sahrens 
303fa9e4066Sahrens 	(void) printf("\tFat ZAP stats:\n");
3048248818dSnd150628 
3058248818dSnd150628 	(void) printf("\t\tPointer table:\n");
3068248818dSnd150628 	(void) printf("\t\t\t%llu elements\n",
307fa9e4066Sahrens 	    (u_longlong_t)zs.zs_ptrtbl_len);
3088248818dSnd150628 	(void) printf("\t\t\tzt_blk: %llu\n",
3098248818dSnd150628 	    (u_longlong_t)zs.zs_ptrtbl_zt_blk);
3108248818dSnd150628 	(void) printf("\t\t\tzt_numblks: %llu\n",
3118248818dSnd150628 	    (u_longlong_t)zs.zs_ptrtbl_zt_numblks);
3128248818dSnd150628 	(void) printf("\t\t\tzt_shift: %llu\n",
3138248818dSnd150628 	    (u_longlong_t)zs.zs_ptrtbl_zt_shift);
3148248818dSnd150628 	(void) printf("\t\t\tzt_blks_copied: %llu\n",
3158248818dSnd150628 	    (u_longlong_t)zs.zs_ptrtbl_blks_copied);
3168248818dSnd150628 	(void) printf("\t\t\tzt_nextblk: %llu\n",
3178248818dSnd150628 	    (u_longlong_t)zs.zs_ptrtbl_nextblk);
3188248818dSnd150628 
319fa9e4066Sahrens 	(void) printf("\t\tZAP entries: %llu\n",
320fa9e4066Sahrens 	    (u_longlong_t)zs.zs_num_entries);
321fa9e4066Sahrens 	(void) printf("\t\tLeaf blocks: %llu\n",
322fa9e4066Sahrens 	    (u_longlong_t)zs.zs_num_leafs);
323fa9e4066Sahrens 	(void) printf("\t\tTotal blocks: %llu\n",
324fa9e4066Sahrens 	    (u_longlong_t)zs.zs_num_blocks);
3258248818dSnd150628 	(void) printf("\t\tzap_block_type: 0x%llx\n",
3268248818dSnd150628 	    (u_longlong_t)zs.zs_block_type);
3278248818dSnd150628 	(void) printf("\t\tzap_magic: 0x%llx\n",
3288248818dSnd150628 	    (u_longlong_t)zs.zs_magic);
3298248818dSnd150628 	(void) printf("\t\tzap_salt: 0x%llx\n",
3308248818dSnd150628 	    (u_longlong_t)zs.zs_salt);
331fa9e4066Sahrens 
332fa9e4066Sahrens 	(void) printf("\t\tLeafs with 2^n pointers:\n");
3330713e232SGeorge Wilson 	dump_histogram(zs.zs_leafs_with_2n_pointers, ZAP_HISTOGRAM_SIZE, 0);
334fa9e4066Sahrens 
335fa9e4066Sahrens 	(void) printf("\t\tBlocks with n*5 entries:\n");
3360713e232SGeorge Wilson 	dump_histogram(zs.zs_blocks_with_n5_entries, ZAP_HISTOGRAM_SIZE, 0);
337fa9e4066Sahrens 
338fa9e4066Sahrens 	(void) printf("\t\tBlocks n/10 full:\n");
3390713e232SGeorge Wilson 	dump_histogram(zs.zs_blocks_n_tenths_full, ZAP_HISTOGRAM_SIZE, 0);
340fa9e4066Sahrens 
341fa9e4066Sahrens 	(void) printf("\t\tEntries with n chunks:\n");
3420713e232SGeorge Wilson 	dump_histogram(zs.zs_entries_using_n_chunks, ZAP_HISTOGRAM_SIZE, 0);
343fa9e4066Sahrens 
344fa9e4066Sahrens 	(void) printf("\t\tBuckets with n entries:\n");
3450713e232SGeorge Wilson 	dump_histogram(zs.zs_buckets_with_n_entries, ZAP_HISTOGRAM_SIZE, 0);
346fa9e4066Sahrens }
347fa9e4066Sahrens 
348fa9e4066Sahrens /*ARGSUSED*/
349fa9e4066Sahrens static void
350fa9e4066Sahrens dump_none(objset_t *os, uint64_t object, void *data, size_t size)
351fa9e4066Sahrens {
352fa9e4066Sahrens }
353fa9e4066Sahrens 
354fa9e4066Sahrens /*ARGSUSED*/
3556de8f417SVictor Latushkin static void
3566de8f417SVictor Latushkin dump_unknown(objset_t *os, uint64_t object, void *data, size_t size)
3576de8f417SVictor Latushkin {
3586de8f417SVictor Latushkin 	(void) printf("\tUNKNOWN OBJECT TYPE\n");
3596de8f417SVictor Latushkin }
3606de8f417SVictor Latushkin 
3616de8f417SVictor Latushkin /*ARGSUSED*/
362fa9e4066Sahrens void
363fa9e4066Sahrens dump_uint8(objset_t *os, uint64_t object, void *data, size_t size)
364fa9e4066Sahrens {
365fa9e4066Sahrens }
366fa9e4066Sahrens 
367fa9e4066Sahrens /*ARGSUSED*/
368fa9e4066Sahrens static void
369fa9e4066Sahrens dump_uint64(objset_t *os, uint64_t object, void *data, size_t size)
370fa9e4066Sahrens {
371fa9e4066Sahrens }
372fa9e4066Sahrens 
373fa9e4066Sahrens /*ARGSUSED*/
374fa9e4066Sahrens static void
375fa9e4066Sahrens dump_zap(objset_t *os, uint64_t object, void *data, size_t size)
376fa9e4066Sahrens {
377fa9e4066Sahrens 	zap_cursor_t zc;
378fa9e4066Sahrens 	zap_attribute_t attr;
379fa9e4066Sahrens 	void *prop;
380fa9e4066Sahrens 	int i;
381fa9e4066Sahrens 
382fa9e4066Sahrens 	dump_zap_stats(os, object);
383fa9e4066Sahrens 	(void) printf("\n");
384fa9e4066Sahrens 
385fa9e4066Sahrens 	for (zap_cursor_init(&zc, os, object);
386fa9e4066Sahrens 	    zap_cursor_retrieve(&zc, &attr) == 0;
387fa9e4066Sahrens 	    zap_cursor_advance(&zc)) {
388fa9e4066Sahrens 		(void) printf("\t\t%s = ", attr.za_name);
389fa9e4066Sahrens 		if (attr.za_num_integers == 0) {
390fa9e4066Sahrens 			(void) printf("\n");
391fa9e4066Sahrens 			continue;
392fa9e4066Sahrens 		}
393fa9e4066Sahrens 		prop = umem_zalloc(attr.za_num_integers *
394fa9e4066Sahrens 		    attr.za_integer_length, UMEM_NOFAIL);
395fa9e4066Sahrens 		(void) zap_lookup(os, object, attr.za_name,
396fa9e4066Sahrens 		    attr.za_integer_length, attr.za_num_integers, prop);
397fa9e4066Sahrens 		if (attr.za_integer_length == 1) {
398fa9e4066Sahrens 			(void) printf("%s", (char *)prop);
399fa9e4066Sahrens 		} else {
400fa9e4066Sahrens 			for (i = 0; i < attr.za_num_integers; i++) {
401fa9e4066Sahrens 				switch (attr.za_integer_length) {
402fa9e4066Sahrens 				case 2:
403fa9e4066Sahrens 					(void) printf("%u ",
404fa9e4066Sahrens 					    ((uint16_t *)prop)[i]);
405fa9e4066Sahrens 					break;
406fa9e4066Sahrens 				case 4:
407fa9e4066Sahrens 					(void) printf("%u ",
408fa9e4066Sahrens 					    ((uint32_t *)prop)[i]);
409fa9e4066Sahrens 					break;
410fa9e4066Sahrens 				case 8:
411fa9e4066Sahrens 					(void) printf("%lld ",
412fa9e4066Sahrens 					    (u_longlong_t)((int64_t *)prop)[i]);
413fa9e4066Sahrens 					break;
414fa9e4066Sahrens 				}
415fa9e4066Sahrens 			}
416fa9e4066Sahrens 		}
417fa9e4066Sahrens 		(void) printf("\n");
418fa9e4066Sahrens 		umem_free(prop, attr.za_num_integers * attr.za_integer_length);
419fa9e4066Sahrens 	}
42087e5029aSahrens 	zap_cursor_fini(&zc);
421fa9e4066Sahrens }
422fa9e4066Sahrens 
423732885fcSMatthew Ahrens static void
424732885fcSMatthew Ahrens dump_bpobj(objset_t *os, uint64_t object, void *data, size_t size)
425732885fcSMatthew Ahrens {
426732885fcSMatthew Ahrens 	bpobj_phys_t *bpop = data;
427732885fcSMatthew Ahrens 	char bytes[32], comp[32], uncomp[32];
428732885fcSMatthew Ahrens 
429732885fcSMatthew Ahrens 	if (bpop == NULL)
430732885fcSMatthew Ahrens 		return;
431732885fcSMatthew Ahrens 
432732885fcSMatthew Ahrens 	zdb_nicenum(bpop->bpo_bytes, bytes);
433732885fcSMatthew Ahrens 	zdb_nicenum(bpop->bpo_comp, comp);
434732885fcSMatthew Ahrens 	zdb_nicenum(bpop->bpo_uncomp, uncomp);
435732885fcSMatthew Ahrens 
436732885fcSMatthew Ahrens 	(void) printf("\t\tnum_blkptrs = %llu\n",
437732885fcSMatthew Ahrens 	    (u_longlong_t)bpop->bpo_num_blkptrs);
438732885fcSMatthew Ahrens 	(void) printf("\t\tbytes = %s\n", bytes);
439732885fcSMatthew Ahrens 	if (size >= BPOBJ_SIZE_V1) {
440732885fcSMatthew Ahrens 		(void) printf("\t\tcomp = %s\n", comp);
441732885fcSMatthew Ahrens 		(void) printf("\t\tuncomp = %s\n", uncomp);
442732885fcSMatthew Ahrens 	}
443732885fcSMatthew Ahrens 	if (size >= sizeof (*bpop)) {
444732885fcSMatthew Ahrens 		(void) printf("\t\tsubobjs = %llu\n",
445732885fcSMatthew Ahrens 		    (u_longlong_t)bpop->bpo_subobjs);
446732885fcSMatthew Ahrens 		(void) printf("\t\tnum_subobjs = %llu\n",
447732885fcSMatthew Ahrens 		    (u_longlong_t)bpop->bpo_num_subobjs);
448732885fcSMatthew Ahrens 	}
449732885fcSMatthew Ahrens 
450732885fcSMatthew Ahrens 	if (dump_opt['d'] < 5)
451732885fcSMatthew Ahrens 		return;
452732885fcSMatthew Ahrens 
453732885fcSMatthew Ahrens 	for (uint64_t i = 0; i < bpop->bpo_num_blkptrs; i++) {
454732885fcSMatthew Ahrens 		char blkbuf[BP_SPRINTF_LEN];
455732885fcSMatthew Ahrens 		blkptr_t bp;
456732885fcSMatthew Ahrens 
457732885fcSMatthew Ahrens 		int err = dmu_read(os, object,
458732885fcSMatthew Ahrens 		    i * sizeof (bp), sizeof (bp), &bp, 0);
459732885fcSMatthew Ahrens 		if (err != 0) {
460732885fcSMatthew Ahrens 			(void) printf("got error %u from dmu_read\n", err);
461732885fcSMatthew Ahrens 			break;
462732885fcSMatthew Ahrens 		}
463732885fcSMatthew Ahrens 		snprintf_blkptr_compact(blkbuf, sizeof (blkbuf), &bp);
464732885fcSMatthew Ahrens 		(void) printf("\t%s\n", blkbuf);
465732885fcSMatthew Ahrens 	}
466732885fcSMatthew Ahrens }
467732885fcSMatthew Ahrens 
468732885fcSMatthew Ahrens /* ARGSUSED */
469732885fcSMatthew Ahrens static void
470732885fcSMatthew Ahrens dump_bpobj_subobjs(objset_t *os, uint64_t object, void *data, size_t size)
471732885fcSMatthew Ahrens {
472732885fcSMatthew Ahrens 	dmu_object_info_t doi;
473732885fcSMatthew Ahrens 
474732885fcSMatthew Ahrens 	VERIFY0(dmu_object_info(os, object, &doi));
475732885fcSMatthew Ahrens 	uint64_t *subobjs = kmem_alloc(doi.doi_max_offset, KM_SLEEP);
476732885fcSMatthew Ahrens 
477732885fcSMatthew Ahrens 	int err = dmu_read(os, object, 0, doi.doi_max_offset, subobjs, 0);
478732885fcSMatthew Ahrens 	if (err != 0) {
479732885fcSMatthew Ahrens 		(void) printf("got error %u from dmu_read\n", err);
480732885fcSMatthew Ahrens 		kmem_free(subobjs, doi.doi_max_offset);
481732885fcSMatthew Ahrens 		return;
482732885fcSMatthew Ahrens 	}
483732885fcSMatthew Ahrens 
484732885fcSMatthew Ahrens 	int64_t last_nonzero = -1;
485732885fcSMatthew Ahrens 	for (uint64_t i = 0; i < doi.doi_max_offset / 8; i++) {
486732885fcSMatthew Ahrens 		if (subobjs[i] != 0)
487732885fcSMatthew Ahrens 			last_nonzero = i;
488732885fcSMatthew Ahrens 	}
489732885fcSMatthew Ahrens 
490732885fcSMatthew Ahrens 	for (int64_t i = 0; i <= last_nonzero; i++) {
491732885fcSMatthew Ahrens 		(void) printf("\t%llu\n", (longlong_t)subobjs[i]);
492732885fcSMatthew Ahrens 	}
493732885fcSMatthew Ahrens 	kmem_free(subobjs, doi.doi_max_offset);
494732885fcSMatthew Ahrens }
495732885fcSMatthew Ahrens 
496e7437265Sahrens /*ARGSUSED*/
497e7437265Sahrens static void
498486ae710SMatthew Ahrens dump_ddt_zap(objset_t *os, uint64_t object, void *data, size_t size)
499486ae710SMatthew Ahrens {
500486ae710SMatthew Ahrens 	dump_zap_stats(os, object);
501486ae710SMatthew Ahrens 	/* contents are printed elsewhere, properly decoded */
502486ae710SMatthew Ahrens }
503486ae710SMatthew Ahrens 
504486ae710SMatthew Ahrens /*ARGSUSED*/
505486ae710SMatthew Ahrens static void
5060a586ceaSMark Shellenbaum dump_sa_attrs(objset_t *os, uint64_t object, void *data, size_t size)
5070a586ceaSMark Shellenbaum {
5080a586ceaSMark Shellenbaum 	zap_cursor_t zc;
5090a586ceaSMark Shellenbaum 	zap_attribute_t attr;
5100a586ceaSMark Shellenbaum 
5110a586ceaSMark Shellenbaum 	dump_zap_stats(os, object);
5120a586ceaSMark Shellenbaum 	(void) printf("\n");
5130a586ceaSMark Shellenbaum 
5140a586ceaSMark Shellenbaum 	for (zap_cursor_init(&zc, os, object);
5150a586ceaSMark Shellenbaum 	    zap_cursor_retrieve(&zc, &attr) == 0;
5160a586ceaSMark Shellenbaum 	    zap_cursor_advance(&zc)) {
5170a586ceaSMark Shellenbaum 		(void) printf("\t\t%s = ", attr.za_name);
5180a586ceaSMark Shellenbaum 		if (attr.za_num_integers == 0) {
5190a586ceaSMark Shellenbaum 			(void) printf("\n");
5200a586ceaSMark Shellenbaum 			continue;
5210a586ceaSMark Shellenbaum 		}
5220a586ceaSMark Shellenbaum 		(void) printf(" %llx : [%d:%d:%d]\n",
5230a586ceaSMark Shellenbaum 		    (u_longlong_t)attr.za_first_integer,
5240a586ceaSMark Shellenbaum 		    (int)ATTR_LENGTH(attr.za_first_integer),
5250a586ceaSMark Shellenbaum 		    (int)ATTR_BSWAP(attr.za_first_integer),
5260a586ceaSMark Shellenbaum 		    (int)ATTR_NUM(attr.za_first_integer));
5270a586ceaSMark Shellenbaum 	}
5280a586ceaSMark Shellenbaum 	zap_cursor_fini(&zc);
5290a586ceaSMark Shellenbaum }
5300a586ceaSMark Shellenbaum 
5310a586ceaSMark Shellenbaum /*ARGSUSED*/
5320a586ceaSMark Shellenbaum static void
5330a586ceaSMark Shellenbaum dump_sa_layouts(objset_t *os, uint64_t object, void *data, size_t size)
5340a586ceaSMark Shellenbaum {
5350a586ceaSMark Shellenbaum 	zap_cursor_t zc;
5360a586ceaSMark Shellenbaum 	zap_attribute_t attr;
5370a586ceaSMark Shellenbaum 	uint16_t *layout_attrs;
5380a586ceaSMark Shellenbaum 	int i;
5390a586ceaSMark Shellenbaum 
5400a586ceaSMark Shellenbaum 	dump_zap_stats(os, object);
5410a586ceaSMark Shellenbaum 	(void) printf("\n");
5420a586ceaSMark Shellenbaum 
5430a586ceaSMark Shellenbaum 	for (zap_cursor_init(&zc, os, object);
5440a586ceaSMark Shellenbaum 	    zap_cursor_retrieve(&zc, &attr) == 0;
5450a586ceaSMark Shellenbaum 	    zap_cursor_advance(&zc)) {
5460a586ceaSMark Shellenbaum 		(void) printf("\t\t%s = [", attr.za_name);
5470a586ceaSMark Shellenbaum 		if (attr.za_num_integers == 0) {
5480a586ceaSMark Shellenbaum 			(void) printf("\n");
5490a586ceaSMark Shellenbaum 			continue;
5500a586ceaSMark Shellenbaum 		}
5510a586ceaSMark Shellenbaum 
5520a586ceaSMark Shellenbaum 		VERIFY(attr.za_integer_length == 2);
5530a586ceaSMark Shellenbaum 		layout_attrs = umem_zalloc(attr.za_num_integers *
5540a586ceaSMark Shellenbaum 		    attr.za_integer_length, UMEM_NOFAIL);
5550a586ceaSMark Shellenbaum 
5560a586ceaSMark Shellenbaum 		VERIFY(zap_lookup(os, object, attr.za_name,
5570a586ceaSMark Shellenbaum 		    attr.za_integer_length,
5580a586ceaSMark Shellenbaum 		    attr.za_num_integers, layout_attrs) == 0);
5590a586ceaSMark Shellenbaum 
5600a586ceaSMark Shellenbaum 		for (i = 0; i != attr.za_num_integers; i++)
5610a586ceaSMark Shellenbaum 			(void) printf(" %d ", (int)layout_attrs[i]);
5620a586ceaSMark Shellenbaum 		(void) printf("]\n");
5630a586ceaSMark Shellenbaum 		umem_free(layout_attrs,
5640a586ceaSMark Shellenbaum 		    attr.za_num_integers * attr.za_integer_length);
5650a586ceaSMark Shellenbaum 	}
5660a586ceaSMark Shellenbaum 	zap_cursor_fini(&zc);
5670a586ceaSMark Shellenbaum }
5680a586ceaSMark Shellenbaum 
5690a586ceaSMark Shellenbaum /*ARGSUSED*/
5700a586ceaSMark Shellenbaum static void
571e7437265Sahrens dump_zpldir(objset_t *os, uint64_t object, void *data, size_t size)
572e7437265Sahrens {
573e7437265Sahrens 	zap_cursor_t zc;
574e7437265Sahrens 	zap_attribute_t attr;
575e7437265Sahrens 	const char *typenames[] = {
576e7437265Sahrens 		/* 0 */ "not specified",
577e7437265Sahrens 		/* 1 */ "FIFO",
578e7437265Sahrens 		/* 2 */ "Character Device",
579e7437265Sahrens 		/* 3 */ "3 (invalid)",
580e7437265Sahrens 		/* 4 */ "Directory",
581e7437265Sahrens 		/* 5 */ "5 (invalid)",
582e7437265Sahrens 		/* 6 */ "Block Device",
583e7437265Sahrens 		/* 7 */ "7 (invalid)",
584e7437265Sahrens 		/* 8 */ "Regular File",
585e7437265Sahrens 		/* 9 */ "9 (invalid)",
586e7437265Sahrens 		/* 10 */ "Symbolic Link",
587e7437265Sahrens 		/* 11 */ "11 (invalid)",
588e7437265Sahrens 		/* 12 */ "Socket",
589e7437265Sahrens 		/* 13 */ "Door",
590e7437265Sahrens 		/* 14 */ "Event Port",
591e7437265Sahrens 		/* 15 */ "15 (invalid)",
592e7437265Sahrens 	};
593e7437265Sahrens 
594e7437265Sahrens 	dump_zap_stats(os, object);
595e7437265Sahrens 	(void) printf("\n");
596e7437265Sahrens 
597e7437265Sahrens 	for (zap_cursor_init(&zc, os, object);
598e7437265Sahrens 	    zap_cursor_retrieve(&zc, &attr) == 0;
599e7437265Sahrens 	    zap_cursor_advance(&zc)) {
600e7437265Sahrens 		(void) printf("\t\t%s = %lld (type: %s)\n",
601e7437265Sahrens 		    attr.za_name, ZFS_DIRENT_OBJ(attr.za_first_integer),
602e7437265Sahrens 		    typenames[ZFS_DIRENT_TYPE(attr.za_first_integer)]);
603e7437265Sahrens 	}
604e7437265Sahrens 	zap_cursor_fini(&zc);
605e7437265Sahrens }
606e7437265Sahrens 
6070713e232SGeorge Wilson int
6080713e232SGeorge Wilson get_dtl_refcount(vdev_t *vd)
6090713e232SGeorge Wilson {
6100713e232SGeorge Wilson 	int refcount = 0;
6110713e232SGeorge Wilson 
6120713e232SGeorge Wilson 	if (vd->vdev_ops->vdev_op_leaf) {
6130713e232SGeorge Wilson 		space_map_t *sm = vd->vdev_dtl_sm;
6140713e232SGeorge Wilson 
6150713e232SGeorge Wilson 		if (sm != NULL &&
6160713e232SGeorge Wilson 		    sm->sm_dbuf->db_size == sizeof (space_map_phys_t))
6170713e232SGeorge Wilson 			return (1);
6180713e232SGeorge Wilson 		return (0);
6190713e232SGeorge Wilson 	}
6200713e232SGeorge Wilson 
6210713e232SGeorge Wilson 	for (int c = 0; c < vd->vdev_children; c++)
6220713e232SGeorge Wilson 		refcount += get_dtl_refcount(vd->vdev_child[c]);
6230713e232SGeorge Wilson 	return (refcount);
6240713e232SGeorge Wilson }
6250713e232SGeorge Wilson 
6260713e232SGeorge Wilson int
6270713e232SGeorge Wilson get_metaslab_refcount(vdev_t *vd)
6280713e232SGeorge Wilson {
6290713e232SGeorge Wilson 	int refcount = 0;
6300713e232SGeorge Wilson 
6312e4c9986SGeorge Wilson 	if (vd->vdev_top == vd && !vd->vdev_removing) {
6320713e232SGeorge Wilson 		for (int m = 0; m < vd->vdev_ms_count; m++) {
6330713e232SGeorge Wilson 			space_map_t *sm = vd->vdev_ms[m]->ms_sm;
6340713e232SGeorge Wilson 
6350713e232SGeorge Wilson 			if (sm != NULL &&
6360713e232SGeorge Wilson 			    sm->sm_dbuf->db_size == sizeof (space_map_phys_t))
6370713e232SGeorge Wilson 				refcount++;
6380713e232SGeorge Wilson 		}
6390713e232SGeorge Wilson 	}
6400713e232SGeorge Wilson 	for (int c = 0; c < vd->vdev_children; c++)
6410713e232SGeorge Wilson 		refcount += get_metaslab_refcount(vd->vdev_child[c]);
6420713e232SGeorge Wilson 
6430713e232SGeorge Wilson 	return (refcount);
6440713e232SGeorge Wilson }
6450713e232SGeorge Wilson 
6460713e232SGeorge Wilson static int
6470713e232SGeorge Wilson verify_spacemap_refcounts(spa_t *spa)
6480713e232SGeorge Wilson {
6492acef22dSMatthew Ahrens 	uint64_t expected_refcount = 0;
6502acef22dSMatthew Ahrens 	uint64_t actual_refcount;
6510713e232SGeorge Wilson 
6522acef22dSMatthew Ahrens 	(void) feature_get_refcount(spa,
6532acef22dSMatthew Ahrens 	    &spa_feature_table[SPA_FEATURE_SPACEMAP_HISTOGRAM],
6542acef22dSMatthew Ahrens 	    &expected_refcount);
6550713e232SGeorge Wilson 	actual_refcount = get_dtl_refcount(spa->spa_root_vdev);
6560713e232SGeorge Wilson 	actual_refcount += get_metaslab_refcount(spa->spa_root_vdev);
6570713e232SGeorge Wilson 
6580713e232SGeorge Wilson 	if (expected_refcount != actual_refcount) {
6592acef22dSMatthew Ahrens 		(void) printf("space map refcount mismatch: expected %lld != "
6602acef22dSMatthew Ahrens 		    "actual %lld\n",
6612acef22dSMatthew Ahrens 		    (longlong_t)expected_refcount,
6622acef22dSMatthew Ahrens 		    (longlong_t)actual_refcount);
6630713e232SGeorge Wilson 		return (2);
6640713e232SGeorge Wilson 	}
6650713e232SGeorge Wilson 	return (0);
6660713e232SGeorge Wilson }
6670713e232SGeorge Wilson 
668fa9e4066Sahrens static void
6690713e232SGeorge Wilson dump_spacemap(objset_t *os, space_map_t *sm)
670fa9e4066Sahrens {
671fa9e4066Sahrens 	uint64_t alloc, offset, entry;
6728053a263Sck153898 	char *ddata[] = { "ALLOC", "FREE", "CONDENSE", "INVALID",
6738053a263Sck153898 			    "INVALID", "INVALID", "INVALID", "INVALID" };
674fa9e4066Sahrens 
6750713e232SGeorge Wilson 	if (sm == NULL)
676fa9e4066Sahrens 		return;
677fa9e4066Sahrens 
678fa9e4066Sahrens 	/*
679fa9e4066Sahrens 	 * Print out the freelist entries in both encoded and decoded form.
680fa9e4066Sahrens 	 */
681fa9e4066Sahrens 	alloc = 0;
6820713e232SGeorge Wilson 	for (offset = 0; offset < space_map_length(sm);
6830713e232SGeorge Wilson 	    offset += sizeof (entry)) {
6840713e232SGeorge Wilson 		uint8_t mapshift = sm->sm_shift;
6850713e232SGeorge Wilson 
6860713e232SGeorge Wilson 		VERIFY0(dmu_read(os, space_map_object(sm), offset,
6877bfdf011SNeil Perrin 		    sizeof (entry), &entry, DMU_READ_PREFETCH));
688fa9e4066Sahrens 		if (SM_DEBUG_DECODE(entry)) {
6890713e232SGeorge Wilson 
69087219db7SVictor Latushkin 			(void) printf("\t    [%6llu] %s: txg %llu, pass %llu\n",
691fa9e4066Sahrens 			    (u_longlong_t)(offset / sizeof (entry)),
692fa9e4066Sahrens 			    ddata[SM_DEBUG_ACTION_DECODE(entry)],
6935ad82045Snd150628 			    (u_longlong_t)SM_DEBUG_TXG_DECODE(entry),
6945ad82045Snd150628 			    (u_longlong_t)SM_DEBUG_SYNCPASS_DECODE(entry));
695fa9e4066Sahrens 		} else {
69687219db7SVictor Latushkin 			(void) printf("\t    [%6llu]    %c  range:"
69787219db7SVictor Latushkin 			    " %010llx-%010llx  size: %06llx\n",
698fa9e4066Sahrens 			    (u_longlong_t)(offset / sizeof (entry)),
699fa9e4066Sahrens 			    SM_TYPE_DECODE(entry) == SM_ALLOC ? 'A' : 'F',
7005ad82045Snd150628 			    (u_longlong_t)((SM_OFFSET_DECODE(entry) <<
7010713e232SGeorge Wilson 			    mapshift) + sm->sm_start),
7025ad82045Snd150628 			    (u_longlong_t)((SM_OFFSET_DECODE(entry) <<
7030713e232SGeorge Wilson 			    mapshift) + sm->sm_start +
7040713e232SGeorge Wilson 			    (SM_RUN_DECODE(entry) << mapshift)),
7055ad82045Snd150628 			    (u_longlong_t)(SM_RUN_DECODE(entry) << mapshift));
706fa9e4066Sahrens 			if (SM_TYPE_DECODE(entry) == SM_ALLOC)
707fa9e4066Sahrens 				alloc += SM_RUN_DECODE(entry) << mapshift;
708fa9e4066Sahrens 			else
709fa9e4066Sahrens 				alloc -= SM_RUN_DECODE(entry) << mapshift;
710fa9e4066Sahrens 		}
711fa9e4066Sahrens 	}
7120713e232SGeorge Wilson 	if (alloc != space_map_allocated(sm)) {
713fa9e4066Sahrens 		(void) printf("space_map_object alloc (%llu) INCONSISTENT "
714fa9e4066Sahrens 		    "with space map summary (%llu)\n",
7150713e232SGeorge Wilson 		    (u_longlong_t)space_map_allocated(sm), (u_longlong_t)alloc);
716fa9e4066Sahrens 	}
717fa9e4066Sahrens }
718fa9e4066Sahrens 
719fa9e4066Sahrens static void
720d6e555bdSGeorge Wilson dump_metaslab_stats(metaslab_t *msp)
721d6e555bdSGeorge Wilson {
7223f9d6ad7SLin Ling 	char maxbuf[32];
7230713e232SGeorge Wilson 	range_tree_t *rt = msp->ms_tree;
7240713e232SGeorge Wilson 	avl_tree_t *t = &msp->ms_size_tree;
7250713e232SGeorge Wilson 	int free_pct = range_tree_space(rt) * 100 / msp->ms_size;
726d6e555bdSGeorge Wilson 
7270713e232SGeorge Wilson 	zdb_nicenum(metaslab_block_maxsize(msp), maxbuf);
728d6e555bdSGeorge Wilson 
72987219db7SVictor Latushkin 	(void) printf("\t %25s %10lu   %7s  %6s   %4s %4d%%\n",
730d6e555bdSGeorge Wilson 	    "segments", avl_numnodes(t), "maxsize", maxbuf,
731d6e555bdSGeorge Wilson 	    "freepct", free_pct);
7320713e232SGeorge Wilson 	(void) printf("\tIn-memory histogram:\n");
7330713e232SGeorge Wilson 	dump_histogram(rt->rt_histogram, RANGE_TREE_HISTOGRAM_SIZE, 0);
734d6e555bdSGeorge Wilson }
735d6e555bdSGeorge Wilson 
736d6e555bdSGeorge Wilson static void
737fa9e4066Sahrens dump_metaslab(metaslab_t *msp)
738fa9e4066Sahrens {
739fa9e4066Sahrens 	vdev_t *vd = msp->ms_group->mg_vd;
740fa9e4066Sahrens 	spa_t *spa = vd->vdev_spa;
7410713e232SGeorge Wilson 	space_map_t *sm = msp->ms_sm;
7423f9d6ad7SLin Ling 	char freebuf[32];
743fa9e4066Sahrens 
7440713e232SGeorge Wilson 	zdb_nicenum(msp->ms_size - space_map_allocated(sm), freebuf);
745fa9e4066Sahrens 
746fa9e4066Sahrens 	(void) printf(
74787219db7SVictor Latushkin 	    "\tmetaslab %6llu   offset %12llx   spacemap %6llu   free    %5s\n",
7480713e232SGeorge Wilson 	    (u_longlong_t)msp->ms_id, (u_longlong_t)msp->ms_start,
7490713e232SGeorge Wilson 	    (u_longlong_t)space_map_object(sm), freebuf);
750fa9e4066Sahrens 
7510713e232SGeorge Wilson 	if (dump_opt['m'] > 2 && !dump_opt['L']) {
752d6e555bdSGeorge Wilson 		mutex_enter(&msp->ms_lock);
7530713e232SGeorge Wilson 		metaslab_load_wait(msp);
7540713e232SGeorge Wilson 		if (!msp->ms_loaded) {
7550713e232SGeorge Wilson 			VERIFY0(metaslab_load(msp));
7560713e232SGeorge Wilson 			range_tree_stat_verify(msp->ms_tree);
7570713e232SGeorge Wilson 		}
758d6e555bdSGeorge Wilson 		dump_metaslab_stats(msp);
7590713e232SGeorge Wilson 		metaslab_unload(msp);
760d6e555bdSGeorge Wilson 		mutex_exit(&msp->ms_lock);
761d6e555bdSGeorge Wilson 	}
762d6e555bdSGeorge Wilson 
7630713e232SGeorge Wilson 	if (dump_opt['m'] > 1 && sm != NULL &&
7642acef22dSMatthew Ahrens 	    spa_feature_is_active(spa, SPA_FEATURE_SPACEMAP_HISTOGRAM)) {
7650713e232SGeorge Wilson 		/*
7660713e232SGeorge Wilson 		 * The space map histogram represents free space in chunks
7670713e232SGeorge Wilson 		 * of sm_shift (i.e. bucket 0 refers to 2^sm_shift).
7680713e232SGeorge Wilson 		 */
7692e4c9986SGeorge Wilson 		(void) printf("\tOn-disk histogram:\t\tfragmentation %llu\n",
7702e4c9986SGeorge Wilson 		    (u_longlong_t)msp->ms_fragmentation);
7710713e232SGeorge Wilson 		dump_histogram(sm->sm_phys->smp_histogram,
7722e4c9986SGeorge Wilson 		    SPACE_MAP_HISTOGRAM_SIZE, sm->sm_shift);
7730713e232SGeorge Wilson 	}
7740713e232SGeorge Wilson 
7750713e232SGeorge Wilson 	if (dump_opt['d'] > 5 || dump_opt['m'] > 3) {
7760713e232SGeorge Wilson 		ASSERT(msp->ms_size == (1ULL << vd->vdev_ms_shift));
777fa9e4066Sahrens 
778d6e555bdSGeorge Wilson 		mutex_enter(&msp->ms_lock);
7790713e232SGeorge Wilson 		dump_spacemap(spa->spa_meta_objset, msp->ms_sm);
780d6e555bdSGeorge Wilson 		mutex_exit(&msp->ms_lock);
781d6e555bdSGeorge Wilson 	}
78287219db7SVictor Latushkin }
783d6e555bdSGeorge Wilson 
78487219db7SVictor Latushkin static void
78587219db7SVictor Latushkin print_vdev_metaslab_header(vdev_t *vd)
78687219db7SVictor Latushkin {
78787219db7SVictor Latushkin 	(void) printf("\tvdev %10llu\n\t%-10s%5llu   %-19s   %-15s   %-10s\n",
78887219db7SVictor Latushkin 	    (u_longlong_t)vd->vdev_id,
78987219db7SVictor Latushkin 	    "metaslabs", (u_longlong_t)vd->vdev_ms_count,
79087219db7SVictor Latushkin 	    "offset", "spacemap", "free");
79187219db7SVictor Latushkin 	(void) printf("\t%15s   %19s   %15s   %10s\n",
79287219db7SVictor Latushkin 	    "---------------", "-------------------",
79387219db7SVictor Latushkin 	    "---------------", "-------------");
794fa9e4066Sahrens }
795fa9e4066Sahrens 
796fa9e4066Sahrens static void
7972e4c9986SGeorge Wilson dump_metaslab_groups(spa_t *spa)
7982e4c9986SGeorge Wilson {
7992e4c9986SGeorge Wilson 	vdev_t *rvd = spa->spa_root_vdev;
8002e4c9986SGeorge Wilson 	metaslab_class_t *mc = spa_normal_class(spa);
8012e4c9986SGeorge Wilson 	uint64_t fragmentation;
8022e4c9986SGeorge Wilson 
8032e4c9986SGeorge Wilson 	metaslab_class_histogram_verify(mc);
8042e4c9986SGeorge Wilson 
8052e4c9986SGeorge Wilson 	for (int c = 0; c < rvd->vdev_children; c++) {
8062e4c9986SGeorge Wilson 		vdev_t *tvd = rvd->vdev_child[c];
8072e4c9986SGeorge Wilson 		metaslab_group_t *mg = tvd->vdev_mg;
8082e4c9986SGeorge Wilson 
8092e4c9986SGeorge Wilson 		if (mg->mg_class != mc)
8102e4c9986SGeorge Wilson 			continue;
8112e4c9986SGeorge Wilson 
8122e4c9986SGeorge Wilson 		metaslab_group_histogram_verify(mg);
8132e4c9986SGeorge Wilson 		mg->mg_fragmentation = metaslab_group_fragmentation(mg);
8142e4c9986SGeorge Wilson 
8152e4c9986SGeorge Wilson 		(void) printf("\tvdev %10llu\t\tmetaslabs%5llu\t\t"
8162e4c9986SGeorge Wilson 		    "fragmentation",
8172e4c9986SGeorge Wilson 		    (u_longlong_t)tvd->vdev_id,
8182e4c9986SGeorge Wilson 		    (u_longlong_t)tvd->vdev_ms_count);
8192e4c9986SGeorge Wilson 		if (mg->mg_fragmentation == ZFS_FRAG_INVALID) {
8202e4c9986SGeorge Wilson 			(void) printf("%3s\n", "-");
8212e4c9986SGeorge Wilson 		} else {
8222e4c9986SGeorge Wilson 			(void) printf("%3llu%%\n",
8232e4c9986SGeorge Wilson 			    (u_longlong_t)mg->mg_fragmentation);
8242e4c9986SGeorge Wilson 		}
8252e4c9986SGeorge Wilson 		dump_histogram(mg->mg_histogram, RANGE_TREE_HISTOGRAM_SIZE, 0);
8262e4c9986SGeorge Wilson 	}
8272e4c9986SGeorge Wilson 
8282e4c9986SGeorge Wilson 	(void) printf("\tpool %s\tfragmentation", spa_name(spa));
8292e4c9986SGeorge Wilson 	fragmentation = metaslab_class_fragmentation(mc);
8302e4c9986SGeorge Wilson 	if (fragmentation == ZFS_FRAG_INVALID)
8312e4c9986SGeorge Wilson 		(void) printf("\t%3s\n", "-");
8322e4c9986SGeorge Wilson 	else
8332e4c9986SGeorge Wilson 		(void) printf("\t%3llu%%\n", (u_longlong_t)fragmentation);
8342e4c9986SGeorge Wilson 	dump_histogram(mc->mc_histogram, RANGE_TREE_HISTOGRAM_SIZE, 0);
8352e4c9986SGeorge Wilson }
8362e4c9986SGeorge Wilson 
8372e4c9986SGeorge Wilson static void
838fa9e4066Sahrens dump_metaslabs(spa_t *spa)
839fa9e4066Sahrens {
84087219db7SVictor Latushkin 	vdev_t *vd, *rvd = spa->spa_root_vdev;
84187219db7SVictor Latushkin 	uint64_t m, c = 0, children = rvd->vdev_children;
842fa9e4066Sahrens 
843fa9e4066Sahrens 	(void) printf("\nMetaslabs:\n");
844fa9e4066Sahrens 
84587219db7SVictor Latushkin 	if (!dump_opt['d'] && zopt_objects > 0) {
84687219db7SVictor Latushkin 		c = zopt_object[0];
847fa9e4066Sahrens 
84887219db7SVictor Latushkin 		if (c >= children)
84987219db7SVictor Latushkin 			(void) fatal("bad vdev id: %llu", (u_longlong_t)c);
85087219db7SVictor Latushkin 
85187219db7SVictor Latushkin 		if (zopt_objects > 1) {
85287219db7SVictor Latushkin 			vd = rvd->vdev_child[c];
85387219db7SVictor Latushkin 			print_vdev_metaslab_header(vd);
85487219db7SVictor Latushkin 
85587219db7SVictor Latushkin 			for (m = 1; m < zopt_objects; m++) {
85687219db7SVictor Latushkin 				if (zopt_object[m] < vd->vdev_ms_count)
85787219db7SVictor Latushkin 					dump_metaslab(
85887219db7SVictor Latushkin 					    vd->vdev_ms[zopt_object[m]]);
85987219db7SVictor Latushkin 				else
86087219db7SVictor Latushkin 					(void) fprintf(stderr, "bad metaslab "
86187219db7SVictor Latushkin 					    "number %llu\n",
86287219db7SVictor Latushkin 					    (u_longlong_t)zopt_object[m]);
86387219db7SVictor Latushkin 			}
86487219db7SVictor Latushkin 			(void) printf("\n");
86587219db7SVictor Latushkin 			return;
86687219db7SVictor Latushkin 		}
86787219db7SVictor Latushkin 		children = c + 1;
86887219db7SVictor Latushkin 	}
86987219db7SVictor Latushkin 	for (; c < children; c++) {
87087219db7SVictor Latushkin 		vd = rvd->vdev_child[c];
87187219db7SVictor Latushkin 		print_vdev_metaslab_header(vd);
872fa9e4066Sahrens 
873fa9e4066Sahrens 		for (m = 0; m < vd->vdev_ms_count; m++)
874fa9e4066Sahrens 			dump_metaslab(vd->vdev_ms[m]);
875fa9e4066Sahrens 		(void) printf("\n");
876fa9e4066Sahrens 	}
877fa9e4066Sahrens }
878fa9e4066Sahrens 
879fa9e4066Sahrens static void
880b24ab676SJeff Bonwick dump_dde(const ddt_t *ddt, const ddt_entry_t *dde, uint64_t index)
881b24ab676SJeff Bonwick {
882b24ab676SJeff Bonwick 	const ddt_phys_t *ddp = dde->dde_phys;
883b24ab676SJeff Bonwick 	const ddt_key_t *ddk = &dde->dde_key;
884b24ab676SJeff Bonwick 	char *types[4] = { "ditto", "single", "double", "triple" };
885b24ab676SJeff Bonwick 	char blkbuf[BP_SPRINTF_LEN];
886b24ab676SJeff Bonwick 	blkptr_t blk;
887b24ab676SJeff Bonwick 
888b24ab676SJeff Bonwick 	for (int p = 0; p < DDT_PHYS_TYPES; p++, ddp++) {
889b24ab676SJeff Bonwick 		if (ddp->ddp_phys_birth == 0)
890b24ab676SJeff Bonwick 			continue;
891bbfd46c4SJeff Bonwick 		ddt_bp_create(ddt->ddt_checksum, ddk, ddp, &blk);
89243466aaeSMax Grossman 		snprintf_blkptr(blkbuf, sizeof (blkbuf), &blk);
893b24ab676SJeff Bonwick 		(void) printf("index %llx refcnt %llu %s %s\n",
894b24ab676SJeff Bonwick 		    (u_longlong_t)index, (u_longlong_t)ddp->ddp_refcnt,
895b24ab676SJeff Bonwick 		    types[p], blkbuf);
896b24ab676SJeff Bonwick 	}
897b24ab676SJeff Bonwick }
898b24ab676SJeff Bonwick 
899b24ab676SJeff Bonwick static void
900b24ab676SJeff Bonwick dump_dedup_ratio(const ddt_stat_t *dds)
901b24ab676SJeff Bonwick {
902b24ab676SJeff Bonwick 	double rL, rP, rD, D, dedup, compress, copies;
903b24ab676SJeff Bonwick 
904b24ab676SJeff Bonwick 	if (dds->dds_blocks == 0)
905b24ab676SJeff Bonwick 		return;
906b24ab676SJeff Bonwick 
907b24ab676SJeff Bonwick 	rL = (double)dds->dds_ref_lsize;
908b24ab676SJeff Bonwick 	rP = (double)dds->dds_ref_psize;
909b24ab676SJeff Bonwick 	rD = (double)dds->dds_ref_dsize;
910b24ab676SJeff Bonwick 	D = (double)dds->dds_dsize;
911b24ab676SJeff Bonwick 
912b24ab676SJeff Bonwick 	dedup = rD / D;
913b24ab676SJeff Bonwick 	compress = rL / rP;
914b24ab676SJeff Bonwick 	copies = rD / rP;
915b24ab676SJeff Bonwick 
916b24ab676SJeff Bonwick 	(void) printf("dedup = %.2f, compress = %.2f, copies = %.2f, "
917b24ab676SJeff Bonwick 	    "dedup * compress / copies = %.2f\n\n",
918b24ab676SJeff Bonwick 	    dedup, compress, copies, dedup * compress / copies);
919b24ab676SJeff Bonwick }
920b24ab676SJeff Bonwick 
921b24ab676SJeff Bonwick static void
922b24ab676SJeff Bonwick dump_ddt(ddt_t *ddt, enum ddt_type type, enum ddt_class class)
923b24ab676SJeff Bonwick {
924b24ab676SJeff Bonwick 	char name[DDT_NAMELEN];
925b24ab676SJeff Bonwick 	ddt_entry_t dde;
926b24ab676SJeff Bonwick 	uint64_t walk = 0;
927b24ab676SJeff Bonwick 	dmu_object_info_t doi;
928b24ab676SJeff Bonwick 	uint64_t count, dspace, mspace;
929b24ab676SJeff Bonwick 	int error;
930b24ab676SJeff Bonwick 
931b24ab676SJeff Bonwick 	error = ddt_object_info(ddt, type, class, &doi);
932b24ab676SJeff Bonwick 
933b24ab676SJeff Bonwick 	if (error == ENOENT)
934b24ab676SJeff Bonwick 		return;
935b24ab676SJeff Bonwick 	ASSERT(error == 0);
936b24ab676SJeff Bonwick 
9377448a079SGeorge Wilson 	if ((count = ddt_object_count(ddt, type, class)) == 0)
9387448a079SGeorge Wilson 		return;
9397448a079SGeorge Wilson 
940b24ab676SJeff Bonwick 	dspace = doi.doi_physical_blocks_512 << 9;
941b24ab676SJeff Bonwick 	mspace = doi.doi_fill_count * doi.doi_data_block_size;
942b24ab676SJeff Bonwick 
943b24ab676SJeff Bonwick 	ddt_object_name(ddt, type, class, name);
944b24ab676SJeff Bonwick 
945b24ab676SJeff Bonwick 	(void) printf("%s: %llu entries, size %llu on disk, %llu in core\n",
946b24ab676SJeff Bonwick 	    name,
947b24ab676SJeff Bonwick 	    (u_longlong_t)count,
948b24ab676SJeff Bonwick 	    (u_longlong_t)(dspace / count),
949b24ab676SJeff Bonwick 	    (u_longlong_t)(mspace / count));
950b24ab676SJeff Bonwick 
951b24ab676SJeff Bonwick 	if (dump_opt['D'] < 3)
952b24ab676SJeff Bonwick 		return;
953b24ab676SJeff Bonwick 
9549eb19f4dSGeorge Wilson 	zpool_dump_ddt(NULL, &ddt->ddt_histogram[type][class]);
955b24ab676SJeff Bonwick 
956b24ab676SJeff Bonwick 	if (dump_opt['D'] < 4)
957b24ab676SJeff Bonwick 		return;
958b24ab676SJeff Bonwick 
959b24ab676SJeff Bonwick 	if (dump_opt['D'] < 5 && class == DDT_CLASS_UNIQUE)
960b24ab676SJeff Bonwick 		return;
961b24ab676SJeff Bonwick 
962b24ab676SJeff Bonwick 	(void) printf("%s contents:\n\n", name);
963b24ab676SJeff Bonwick 
964bbfd46c4SJeff Bonwick 	while ((error = ddt_object_walk(ddt, type, class, &walk, &dde)) == 0)
965b24ab676SJeff Bonwick 		dump_dde(ddt, &dde, walk);
966b24ab676SJeff Bonwick 
967b24ab676SJeff Bonwick 	ASSERT(error == ENOENT);
968b24ab676SJeff Bonwick 
969b24ab676SJeff Bonwick 	(void) printf("\n");
970b24ab676SJeff Bonwick }
971b24ab676SJeff Bonwick 
972b24ab676SJeff Bonwick static void
973b24ab676SJeff Bonwick dump_all_ddts(spa_t *spa)
974b24ab676SJeff Bonwick {
975b24ab676SJeff Bonwick 	ddt_histogram_t ddh_total = { 0 };
976b24ab676SJeff Bonwick 	ddt_stat_t dds_total = { 0 };
977b24ab676SJeff Bonwick 
978b24ab676SJeff Bonwick 	for (enum zio_checksum c = 0; c < ZIO_CHECKSUM_FUNCTIONS; c++) {
979b24ab676SJeff Bonwick 		ddt_t *ddt = spa->spa_ddt[c];
980b24ab676SJeff Bonwick 		for (enum ddt_type type = 0; type < DDT_TYPES; type++) {
981b24ab676SJeff Bonwick 			for (enum ddt_class class = 0; class < DDT_CLASSES;
982b24ab676SJeff Bonwick 			    class++) {
983b24ab676SJeff Bonwick 				dump_ddt(ddt, type, class);
984b24ab676SJeff Bonwick 			}
985b24ab676SJeff Bonwick 		}
986b24ab676SJeff Bonwick 	}
987b24ab676SJeff Bonwick 
9889eb19f4dSGeorge Wilson 	ddt_get_dedup_stats(spa, &dds_total);
989b24ab676SJeff Bonwick 
990b24ab676SJeff Bonwick 	if (dds_total.dds_blocks == 0) {
991b24ab676SJeff Bonwick 		(void) printf("All DDTs are empty\n");
992b24ab676SJeff Bonwick 		return;
993b24ab676SJeff Bonwick 	}
994b24ab676SJeff Bonwick 
995b24ab676SJeff Bonwick 	(void) printf("\n");
996b24ab676SJeff Bonwick 
997b24ab676SJeff Bonwick 	if (dump_opt['D'] > 1) {
998b24ab676SJeff Bonwick 		(void) printf("DDT histogram (aggregated over all DDTs):\n");
9999eb19f4dSGeorge Wilson 		ddt_get_dedup_histogram(spa, &ddh_total);
10009eb19f4dSGeorge Wilson 		zpool_dump_ddt(&dds_total, &ddh_total);
1001b24ab676SJeff Bonwick 	}
1002b24ab676SJeff Bonwick 
1003b24ab676SJeff Bonwick 	dump_dedup_ratio(&dds_total);
1004b24ab676SJeff Bonwick }
1005b24ab676SJeff Bonwick 
1006b24ab676SJeff Bonwick static void
10070713e232SGeorge Wilson dump_dtl_seg(void *arg, uint64_t start, uint64_t size)
10088ad4d6ddSJeff Bonwick {
10090713e232SGeorge Wilson 	char *prefix = arg;
10108ad4d6ddSJeff Bonwick 
10118ad4d6ddSJeff Bonwick 	(void) printf("%s [%llu,%llu) length %llu\n",
10128ad4d6ddSJeff Bonwick 	    prefix,
10138ad4d6ddSJeff Bonwick 	    (u_longlong_t)start,
10148ad4d6ddSJeff Bonwick 	    (u_longlong_t)(start + size),
10158ad4d6ddSJeff Bonwick 	    (u_longlong_t)(size));
10168ad4d6ddSJeff Bonwick }
10178ad4d6ddSJeff Bonwick 
10188ad4d6ddSJeff Bonwick static void
1019fa9e4066Sahrens dump_dtl(vdev_t *vd, int indent)
1020fa9e4066Sahrens {
10218ad4d6ddSJeff Bonwick 	spa_t *spa = vd->vdev_spa;
10228ad4d6ddSJeff Bonwick 	boolean_t required;
10238ad4d6ddSJeff Bonwick 	char *name[DTL_TYPES] = { "missing", "partial", "scrub", "outage" };
10248ad4d6ddSJeff Bonwick 	char prefix[256];
10258ad4d6ddSJeff Bonwick 
10268f18d1faSGeorge Wilson 	spa_vdev_state_enter(spa, SCL_NONE);
10278ad4d6ddSJeff Bonwick 	required = vdev_dtl_required(vd);
10288ad4d6ddSJeff Bonwick 	(void) spa_vdev_state_exit(spa, NULL, 0);
1029fa9e4066Sahrens 
1030fa9e4066Sahrens 	if (indent == 0)
1031fa9e4066Sahrens 		(void) printf("\nDirty time logs:\n\n");
1032fa9e4066Sahrens 
10338ad4d6ddSJeff Bonwick 	(void) printf("\t%*s%s [%s]\n", indent, "",
1034e14bb325SJeff Bonwick 	    vd->vdev_path ? vd->vdev_path :
10358ad4d6ddSJeff Bonwick 	    vd->vdev_parent ? vd->vdev_ops->vdev_op_type : spa_name(spa),
10368ad4d6ddSJeff Bonwick 	    required ? "DTL-required" : "DTL-expendable");
1037fa9e4066Sahrens 
10388ad4d6ddSJeff Bonwick 	for (int t = 0; t < DTL_TYPES; t++) {
10390713e232SGeorge Wilson 		range_tree_t *rt = vd->vdev_dtl[t];
10400713e232SGeorge Wilson 		if (range_tree_space(rt) == 0)
10418ad4d6ddSJeff Bonwick 			continue;
10428ad4d6ddSJeff Bonwick 		(void) snprintf(prefix, sizeof (prefix), "\t%*s%s",
10438ad4d6ddSJeff Bonwick 		    indent + 2, "", name[t]);
10440713e232SGeorge Wilson 		mutex_enter(rt->rt_lock);
10450713e232SGeorge Wilson 		range_tree_walk(rt, dump_dtl_seg, prefix);
10460713e232SGeorge Wilson 		mutex_exit(rt->rt_lock);
10478ad4d6ddSJeff Bonwick 		if (dump_opt['d'] > 5 && vd->vdev_children == 0)
10480713e232SGeorge Wilson 			dump_spacemap(spa->spa_meta_objset, vd->vdev_dtl_sm);
1049fa9e4066Sahrens 	}
1050fa9e4066Sahrens 
10518ad4d6ddSJeff Bonwick 	for (int c = 0; c < vd->vdev_children; c++)
1052fa9e4066Sahrens 		dump_dtl(vd->vdev_child[c], indent + 4);
1053fa9e4066Sahrens }
1054fa9e4066Sahrens 
10558f18d1faSGeorge Wilson static void
10568f18d1faSGeorge Wilson dump_history(spa_t *spa)
10578f18d1faSGeorge Wilson {
10588f18d1faSGeorge Wilson 	nvlist_t **events = NULL;
10598f18d1faSGeorge Wilson 	char buf[SPA_MAXBLOCKSIZE];
1060e4161df6SVictor Latushkin 	uint64_t resid, len, off = 0;
10618f18d1faSGeorge Wilson 	uint_t num = 0;
10628f18d1faSGeorge Wilson 	int error;
10638f18d1faSGeorge Wilson 	time_t tsec;
10648f18d1faSGeorge Wilson 	struct tm t;
10658f18d1faSGeorge Wilson 	char tbuf[30];
10668f18d1faSGeorge Wilson 	char internalstr[MAXPATHLEN];
10678f18d1faSGeorge Wilson 
10688f18d1faSGeorge Wilson 	do {
1069e4161df6SVictor Latushkin 		len = sizeof (buf);
1070e4161df6SVictor Latushkin 
10718f18d1faSGeorge Wilson 		if ((error = spa_history_get(spa, &off, &len, buf)) != 0) {
10728f18d1faSGeorge Wilson 			(void) fprintf(stderr, "Unable to read history: "
10738f18d1faSGeorge Wilson 			    "error %d\n", error);
10748f18d1faSGeorge Wilson 			return;
10758f18d1faSGeorge Wilson 		}
10768f18d1faSGeorge Wilson 
10778f18d1faSGeorge Wilson 		if (zpool_history_unpack(buf, len, &resid, &events, &num) != 0)
10788f18d1faSGeorge Wilson 			break;
10798f18d1faSGeorge Wilson 
10808f18d1faSGeorge Wilson 		off -= resid;
10818f18d1faSGeorge Wilson 	} while (len != 0);
10828f18d1faSGeorge Wilson 
10838f18d1faSGeorge Wilson 	(void) printf("\nHistory:\n");
10848f18d1faSGeorge Wilson 	for (int i = 0; i < num; i++) {
10858f18d1faSGeorge Wilson 		uint64_t time, txg, ievent;
10868f18d1faSGeorge Wilson 		char *cmd, *intstr;
10874445fffbSMatthew Ahrens 		boolean_t printed = B_FALSE;
10888f18d1faSGeorge Wilson 
10898f18d1faSGeorge Wilson 		if (nvlist_lookup_uint64(events[i], ZPOOL_HIST_TIME,
10908f18d1faSGeorge Wilson 		    &time) != 0)
10914445fffbSMatthew Ahrens 			goto next;
10928f18d1faSGeorge Wilson 		if (nvlist_lookup_string(events[i], ZPOOL_HIST_CMD,
10938f18d1faSGeorge Wilson 		    &cmd) != 0) {
10948f18d1faSGeorge Wilson 			if (nvlist_lookup_uint64(events[i],
10958f18d1faSGeorge Wilson 			    ZPOOL_HIST_INT_EVENT, &ievent) != 0)
10964445fffbSMatthew Ahrens 				goto next;
10978f18d1faSGeorge Wilson 			verify(nvlist_lookup_uint64(events[i],
10988f18d1faSGeorge Wilson 			    ZPOOL_HIST_TXG, &txg) == 0);
10998f18d1faSGeorge Wilson 			verify(nvlist_lookup_string(events[i],
11008f18d1faSGeorge Wilson 			    ZPOOL_HIST_INT_STR, &intstr) == 0);
11014445fffbSMatthew Ahrens 			if (ievent >= ZFS_NUM_LEGACY_HISTORY_EVENTS)
11024445fffbSMatthew Ahrens 				goto next;
11038f18d1faSGeorge Wilson 
11048f18d1faSGeorge Wilson 			(void) snprintf(internalstr,
11058f18d1faSGeorge Wilson 			    sizeof (internalstr),
11068f18d1faSGeorge Wilson 			    "[internal %s txg:%lld] %s",
11073f9d6ad7SLin Ling 			    zfs_history_event_names[ievent], txg,
11088f18d1faSGeorge Wilson 			    intstr);
11098f18d1faSGeorge Wilson 			cmd = internalstr;
11108f18d1faSGeorge Wilson 		}
11118f18d1faSGeorge Wilson 		tsec = time;
11128f18d1faSGeorge Wilson 		(void) localtime_r(&tsec, &t);
11138f18d1faSGeorge Wilson 		(void) strftime(tbuf, sizeof (tbuf), "%F.%T", &t);
11148f18d1faSGeorge Wilson 		(void) printf("%s %s\n", tbuf, cmd);
11154445fffbSMatthew Ahrens 		printed = B_TRUE;
11164445fffbSMatthew Ahrens 
11174445fffbSMatthew Ahrens next:
11184445fffbSMatthew Ahrens 		if (dump_opt['h'] > 1) {
11194445fffbSMatthew Ahrens 			if (!printed)
11204445fffbSMatthew Ahrens 				(void) printf("unrecognized record:\n");
11214445fffbSMatthew Ahrens 			dump_nvlist(events[i], 2);
11224445fffbSMatthew Ahrens 		}
11238f18d1faSGeorge Wilson 	}
11248f18d1faSGeorge Wilson }
11258f18d1faSGeorge Wilson 
1126fa9e4066Sahrens /*ARGSUSED*/
1127fa9e4066Sahrens static void
1128fa9e4066Sahrens dump_dnode(objset_t *os, uint64_t object, void *data, size_t size)
1129fa9e4066Sahrens {
1130fa9e4066Sahrens }
1131fa9e4066Sahrens 
1132fa9e4066Sahrens static uint64_t
11337802d7bfSMatthew Ahrens blkid2offset(const dnode_phys_t *dnp, const blkptr_t *bp,
11347802d7bfSMatthew Ahrens     const zbookmark_phys_t *zb)
1135fa9e4066Sahrens {
1136b24ab676SJeff Bonwick 	if (dnp == NULL) {
1137b24ab676SJeff Bonwick 		ASSERT(zb->zb_level < 0);
1138b24ab676SJeff Bonwick 		if (zb->zb_object == 0)
1139b24ab676SJeff Bonwick 			return (zb->zb_blkid);
1140b24ab676SJeff Bonwick 		return (zb->zb_blkid * BP_GET_LSIZE(bp));
1141b24ab676SJeff Bonwick 	}
1142fa9e4066Sahrens 
1143b24ab676SJeff Bonwick 	ASSERT(zb->zb_level >= 0);
1144b24ab676SJeff Bonwick 
1145b24ab676SJeff Bonwick 	return ((zb->zb_blkid <<
1146b24ab676SJeff Bonwick 	    (zb->zb_level * (dnp->dn_indblkshift - SPA_BLKPTRSHIFT))) *
1147fa9e4066Sahrens 	    dnp->dn_datablkszsec << SPA_MINBLOCKSHIFT);
1148fa9e4066Sahrens }
1149fa9e4066Sahrens 
115044cd46caSbillm static void
115143466aaeSMax Grossman snprintf_blkptr_compact(char *blkbuf, size_t buflen, const blkptr_t *bp)
115244cd46caSbillm {
1153cde58dbcSMatthew Ahrens 	const dva_t *dva = bp->blk_dva;
1154b24ab676SJeff Bonwick 	int ndvas = dump_opt['d'] > 5 ? BP_GET_NDVAS(bp) : 1;
1155b24ab676SJeff Bonwick 
1156490d05b9SMatthew Ahrens 	if (dump_opt['b'] >= 6) {
115743466aaeSMax Grossman 		snprintf_blkptr(blkbuf, buflen, bp);
1158b24ab676SJeff Bonwick 		return;
1159b24ab676SJeff Bonwick 	}
116044cd46caSbillm 
11615d7b4d43SMatthew Ahrens 	if (BP_IS_EMBEDDED(bp)) {
11625d7b4d43SMatthew Ahrens 		(void) sprintf(blkbuf,
11635d7b4d43SMatthew Ahrens 		    "EMBEDDED et=%u %llxL/%llxP B=%llu",
11645d7b4d43SMatthew Ahrens 		    (int)BPE_GET_ETYPE(bp),
11655d7b4d43SMatthew Ahrens 		    (u_longlong_t)BPE_GET_LSIZE(bp),
11665d7b4d43SMatthew Ahrens 		    (u_longlong_t)BPE_GET_PSIZE(bp),
11675d7b4d43SMatthew Ahrens 		    (u_longlong_t)bp->blk_birth);
11685d7b4d43SMatthew Ahrens 		return;
11695d7b4d43SMatthew Ahrens 	}
117044cd46caSbillm 
11715d7b4d43SMatthew Ahrens 	blkbuf[0] = '\0';
1172b24ab676SJeff Bonwick 	for (int i = 0; i < ndvas; i++)
117343466aaeSMax Grossman 		(void) snprintf(blkbuf + strlen(blkbuf),
117443466aaeSMax Grossman 		    buflen - strlen(blkbuf), "%llu:%llx:%llx ",
117544cd46caSbillm 		    (u_longlong_t)DVA_GET_VDEV(&dva[i]),
117644cd46caSbillm 		    (u_longlong_t)DVA_GET_OFFSET(&dva[i]),
117744cd46caSbillm 		    (u_longlong_t)DVA_GET_ASIZE(&dva[i]));
117844cd46caSbillm 
117943466aaeSMax Grossman 	if (BP_IS_HOLE(bp)) {
118043466aaeSMax Grossman 		(void) snprintf(blkbuf + strlen(blkbuf),
118170163ac5SPrakash Surya 		    buflen - strlen(blkbuf),
118270163ac5SPrakash Surya 		    "%llxL B=%llu",
118370163ac5SPrakash Surya 		    (u_longlong_t)BP_GET_LSIZE(bp),
118443466aaeSMax Grossman 		    (u_longlong_t)bp->blk_birth);
118543466aaeSMax Grossman 	} else {
118643466aaeSMax Grossman 		(void) snprintf(blkbuf + strlen(blkbuf),
118743466aaeSMax Grossman 		    buflen - strlen(blkbuf),
1188b24ab676SJeff Bonwick 		    "%llxL/%llxP F=%llu B=%llu/%llu",
118944cd46caSbillm 		    (u_longlong_t)BP_GET_LSIZE(bp),
119044cd46caSbillm 		    (u_longlong_t)BP_GET_PSIZE(bp),
11915d7b4d43SMatthew Ahrens 		    (u_longlong_t)BP_GET_FILL(bp),
1192b24ab676SJeff Bonwick 		    (u_longlong_t)bp->blk_birth,
1193b24ab676SJeff Bonwick 		    (u_longlong_t)BP_PHYSICAL_BIRTH(bp));
119444cd46caSbillm 	}
119543466aaeSMax Grossman }
119644cd46caSbillm 
119788b7b0f2SMatthew Ahrens static void
11987802d7bfSMatthew Ahrens print_indirect(blkptr_t *bp, const zbookmark_phys_t *zb,
119988b7b0f2SMatthew Ahrens     const dnode_phys_t *dnp)
1200fa9e4066Sahrens {
120188b7b0f2SMatthew Ahrens 	char blkbuf[BP_SPRINTF_LEN];
1202fa9e4066Sahrens 	int l;
1203fa9e4066Sahrens 
12045d7b4d43SMatthew Ahrens 	if (!BP_IS_EMBEDDED(bp)) {
1205fa9e4066Sahrens 		ASSERT3U(BP_GET_TYPE(bp), ==, dnp->dn_type);
1206fa9e4066Sahrens 		ASSERT3U(BP_GET_LEVEL(bp), ==, zb->zb_level);
12075d7b4d43SMatthew Ahrens 	}
1208fa9e4066Sahrens 
1209b24ab676SJeff Bonwick 	(void) printf("%16llx ", (u_longlong_t)blkid2offset(dnp, bp, zb));
1210fa9e4066Sahrens 
1211fa9e4066Sahrens 	ASSERT(zb->zb_level >= 0);
1212fa9e4066Sahrens 
1213fa9e4066Sahrens 	for (l = dnp->dn_nlevels - 1; l >= -1; l--) {
1214fa9e4066Sahrens 		if (l == zb->zb_level) {
121588b7b0f2SMatthew Ahrens 			(void) printf("L%llx", (u_longlong_t)zb->zb_level);
1216fa9e4066Sahrens 		} else {
121788b7b0f2SMatthew Ahrens 			(void) printf(" ");
1218fa9e4066Sahrens 		}
1219fa9e4066Sahrens 	}
1220fa9e4066Sahrens 
122143466aaeSMax Grossman 	snprintf_blkptr_compact(blkbuf, sizeof (blkbuf), bp);
122244cd46caSbillm 	(void) printf("%s\n", blkbuf);
1223fa9e4066Sahrens }
1224fa9e4066Sahrens 
122588b7b0f2SMatthew Ahrens static int
122688b7b0f2SMatthew Ahrens visit_indirect(spa_t *spa, const dnode_phys_t *dnp,
12277802d7bfSMatthew Ahrens     blkptr_t *bp, const zbookmark_phys_t *zb)
122888b7b0f2SMatthew Ahrens {
1229e4161df6SVictor Latushkin 	int err = 0;
123088b7b0f2SMatthew Ahrens 
123188b7b0f2SMatthew Ahrens 	if (bp->blk_birth == 0)
123288b7b0f2SMatthew Ahrens 		return (0);
123388b7b0f2SMatthew Ahrens 
123488b7b0f2SMatthew Ahrens 	print_indirect(bp, zb, dnp);
123588b7b0f2SMatthew Ahrens 
123643466aaeSMax Grossman 	if (BP_GET_LEVEL(bp) > 0 && !BP_IS_HOLE(bp)) {
12377adb730bSGeorge Wilson 		arc_flags_t flags = ARC_FLAG_WAIT;
123888b7b0f2SMatthew Ahrens 		int i;
123988b7b0f2SMatthew Ahrens 		blkptr_t *cbp;
124088b7b0f2SMatthew Ahrens 		int epb = BP_GET_LSIZE(bp) >> SPA_BLKPTRSHIFT;
124188b7b0f2SMatthew Ahrens 		arc_buf_t *buf;
124288b7b0f2SMatthew Ahrens 		uint64_t fill = 0;
124388b7b0f2SMatthew Ahrens 
12441b912ec7SGeorge Wilson 		err = arc_read(NULL, spa, bp, arc_getbuf_func, &buf,
124588b7b0f2SMatthew Ahrens 		    ZIO_PRIORITY_ASYNC_READ, ZIO_FLAG_CANFAIL, &flags, zb);
124688b7b0f2SMatthew Ahrens 		if (err)
124788b7b0f2SMatthew Ahrens 			return (err);
12483f9d6ad7SLin Ling 		ASSERT(buf->b_data);
124988b7b0f2SMatthew Ahrens 
125088b7b0f2SMatthew Ahrens 		/* recursively visit blocks below this */
125188b7b0f2SMatthew Ahrens 		cbp = buf->b_data;
125288b7b0f2SMatthew Ahrens 		for (i = 0; i < epb; i++, cbp++) {
12537802d7bfSMatthew Ahrens 			zbookmark_phys_t czb;
125488b7b0f2SMatthew Ahrens 
125588b7b0f2SMatthew Ahrens 			SET_BOOKMARK(&czb, zb->zb_objset, zb->zb_object,
125688b7b0f2SMatthew Ahrens 			    zb->zb_level - 1,
125788b7b0f2SMatthew Ahrens 			    zb->zb_blkid * epb + i);
125888b7b0f2SMatthew Ahrens 			err = visit_indirect(spa, dnp, cbp, &czb);
125988b7b0f2SMatthew Ahrens 			if (err)
126088b7b0f2SMatthew Ahrens 				break;
12615d7b4d43SMatthew Ahrens 			fill += BP_GET_FILL(cbp);
126288b7b0f2SMatthew Ahrens 		}
12638ad4d6ddSJeff Bonwick 		if (!err)
12645d7b4d43SMatthew Ahrens 			ASSERT3U(fill, ==, BP_GET_FILL(bp));
126588b7b0f2SMatthew Ahrens 		(void) arc_buf_remove_ref(buf, &buf);
126688b7b0f2SMatthew Ahrens 	}
126788b7b0f2SMatthew Ahrens 
126888b7b0f2SMatthew Ahrens 	return (err);
1269fa9e4066Sahrens }
1270fa9e4066Sahrens 
1271fa9e4066Sahrens /*ARGSUSED*/
1272fa9e4066Sahrens static void
127388b7b0f2SMatthew Ahrens dump_indirect(dnode_t *dn)
1274fa9e4066Sahrens {
127588b7b0f2SMatthew Ahrens 	dnode_phys_t *dnp = dn->dn_phys;
127688b7b0f2SMatthew Ahrens 	int j;
12777802d7bfSMatthew Ahrens 	zbookmark_phys_t czb;
1278fa9e4066Sahrens 
1279fa9e4066Sahrens 	(void) printf("Indirect blocks:\n");
1280fa9e4066Sahrens 
1281503ad85cSMatthew Ahrens 	SET_BOOKMARK(&czb, dmu_objset_id(dn->dn_objset),
128288b7b0f2SMatthew Ahrens 	    dn->dn_object, dnp->dn_nlevels - 1, 0);
128388b7b0f2SMatthew Ahrens 	for (j = 0; j < dnp->dn_nblkptr; j++) {
128488b7b0f2SMatthew Ahrens 		czb.zb_blkid = j;
1285503ad85cSMatthew Ahrens 		(void) visit_indirect(dmu_objset_spa(dn->dn_objset), dnp,
128688b7b0f2SMatthew Ahrens 		    &dnp->dn_blkptr[j], &czb);
128788b7b0f2SMatthew Ahrens 	}
1288fa9e4066Sahrens 
1289fa9e4066Sahrens 	(void) printf("\n");
1290fa9e4066Sahrens }
1291fa9e4066Sahrens 
1292fa9e4066Sahrens /*ARGSUSED*/
1293fa9e4066Sahrens static void
1294fa9e4066Sahrens dump_dsl_dir(objset_t *os, uint64_t object, void *data, size_t size)
1295fa9e4066Sahrens {
1296fa9e4066Sahrens 	dsl_dir_phys_t *dd = data;
1297fa9e4066Sahrens 	time_t crtime;
12983f9d6ad7SLin Ling 	char nice[32];
1299fa9e4066Sahrens 
1300fa9e4066Sahrens 	if (dd == NULL)
1301fa9e4066Sahrens 		return;
1302fa9e4066Sahrens 
1303da6c28aaSamw 	ASSERT3U(size, >=, sizeof (dsl_dir_phys_t));
1304fa9e4066Sahrens 
1305fa9e4066Sahrens 	crtime = dd->dd_creation_time;
1306fa9e4066Sahrens 	(void) printf("\t\tcreation_time = %s", ctime(&crtime));
1307fa9e4066Sahrens 	(void) printf("\t\thead_dataset_obj = %llu\n",
1308fa9e4066Sahrens 	    (u_longlong_t)dd->dd_head_dataset_obj);
1309fa9e4066Sahrens 	(void) printf("\t\tparent_dir_obj = %llu\n",
1310fa9e4066Sahrens 	    (u_longlong_t)dd->dd_parent_obj);
13113cb34c60Sahrens 	(void) printf("\t\torigin_obj = %llu\n",
13123cb34c60Sahrens 	    (u_longlong_t)dd->dd_origin_obj);
1313fa9e4066Sahrens 	(void) printf("\t\tchild_dir_zapobj = %llu\n",
1314fa9e4066Sahrens 	    (u_longlong_t)dd->dd_child_dir_zapobj);
13153f9d6ad7SLin Ling 	zdb_nicenum(dd->dd_used_bytes, nice);
131674e7dc98SMatthew Ahrens 	(void) printf("\t\tused_bytes = %s\n", nice);
13173f9d6ad7SLin Ling 	zdb_nicenum(dd->dd_compressed_bytes, nice);
131874e7dc98SMatthew Ahrens 	(void) printf("\t\tcompressed_bytes = %s\n", nice);
13193f9d6ad7SLin Ling 	zdb_nicenum(dd->dd_uncompressed_bytes, nice);
132074e7dc98SMatthew Ahrens 	(void) printf("\t\tuncompressed_bytes = %s\n", nice);
13213f9d6ad7SLin Ling 	zdb_nicenum(dd->dd_quota, nice);
132274e7dc98SMatthew Ahrens 	(void) printf("\t\tquota = %s\n", nice);
13233f9d6ad7SLin Ling 	zdb_nicenum(dd->dd_reserved, nice);
132474e7dc98SMatthew Ahrens 	(void) printf("\t\treserved = %s\n", nice);
1325fa9e4066Sahrens 	(void) printf("\t\tprops_zapobj = %llu\n",
1326fa9e4066Sahrens 	    (u_longlong_t)dd->dd_props_zapobj);
1327ecd6cf80Smarks 	(void) printf("\t\tdeleg_zapobj = %llu\n",
1328ecd6cf80Smarks 	    (u_longlong_t)dd->dd_deleg_zapobj);
132974e7dc98SMatthew Ahrens 	(void) printf("\t\tflags = %llx\n",
133074e7dc98SMatthew Ahrens 	    (u_longlong_t)dd->dd_flags);
133174e7dc98SMatthew Ahrens 
133274e7dc98SMatthew Ahrens #define	DO(which) \
13333f9d6ad7SLin Ling 	zdb_nicenum(dd->dd_used_breakdown[DD_USED_ ## which], nice); \
133474e7dc98SMatthew Ahrens 	(void) printf("\t\tused_breakdown[" #which "] = %s\n", nice)
133574e7dc98SMatthew Ahrens 	DO(HEAD);
133674e7dc98SMatthew Ahrens 	DO(SNAP);
133774e7dc98SMatthew Ahrens 	DO(CHILD);
133874e7dc98SMatthew Ahrens 	DO(CHILD_RSRV);
133974e7dc98SMatthew Ahrens 	DO(REFRSRV);
134074e7dc98SMatthew Ahrens #undef DO
1341fa9e4066Sahrens }
1342fa9e4066Sahrens 
1343fa9e4066Sahrens /*ARGSUSED*/
1344fa9e4066Sahrens static void
1345fa9e4066Sahrens dump_dsl_dataset(objset_t *os, uint64_t object, void *data, size_t size)
1346fa9e4066Sahrens {
1347fa9e4066Sahrens 	dsl_dataset_phys_t *ds = data;
1348fa9e4066Sahrens 	time_t crtime;
13493f9d6ad7SLin Ling 	char used[32], compressed[32], uncompressed[32], unique[32];
1350fbabab8fSmaybee 	char blkbuf[BP_SPRINTF_LEN];
1351fa9e4066Sahrens 
1352fa9e4066Sahrens 	if (ds == NULL)
1353fa9e4066Sahrens 		return;
1354fa9e4066Sahrens 
1355fa9e4066Sahrens 	ASSERT(size == sizeof (*ds));
1356fa9e4066Sahrens 	crtime = ds->ds_creation_time;
1357ad135b5dSChristopher Siden 	zdb_nicenum(ds->ds_referenced_bytes, used);
13583f9d6ad7SLin Ling 	zdb_nicenum(ds->ds_compressed_bytes, compressed);
13593f9d6ad7SLin Ling 	zdb_nicenum(ds->ds_uncompressed_bytes, uncompressed);
13603f9d6ad7SLin Ling 	zdb_nicenum(ds->ds_unique_bytes, unique);
136143466aaeSMax Grossman 	snprintf_blkptr(blkbuf, sizeof (blkbuf), &ds->ds_bp);
1362fa9e4066Sahrens 
1363088f3894Sahrens 	(void) printf("\t\tdir_obj = %llu\n",
1364fa9e4066Sahrens 	    (u_longlong_t)ds->ds_dir_obj);
1365fa9e4066Sahrens 	(void) printf("\t\tprev_snap_obj = %llu\n",
1366fa9e4066Sahrens 	    (u_longlong_t)ds->ds_prev_snap_obj);
1367fa9e4066Sahrens 	(void) printf("\t\tprev_snap_txg = %llu\n",
1368fa9e4066Sahrens 	    (u_longlong_t)ds->ds_prev_snap_txg);
1369fa9e4066Sahrens 	(void) printf("\t\tnext_snap_obj = %llu\n",
1370fa9e4066Sahrens 	    (u_longlong_t)ds->ds_next_snap_obj);
1371fa9e4066Sahrens 	(void) printf("\t\tsnapnames_zapobj = %llu\n",
1372fa9e4066Sahrens 	    (u_longlong_t)ds->ds_snapnames_zapobj);
1373fa9e4066Sahrens 	(void) printf("\t\tnum_children = %llu\n",
1374fa9e4066Sahrens 	    (u_longlong_t)ds->ds_num_children);
1375842727c2SChris Kirby 	(void) printf("\t\tuserrefs_obj = %llu\n",
1376842727c2SChris Kirby 	    (u_longlong_t)ds->ds_userrefs_obj);
1377fa9e4066Sahrens 	(void) printf("\t\tcreation_time = %s", ctime(&crtime));
1378fa9e4066Sahrens 	(void) printf("\t\tcreation_txg = %llu\n",
1379fa9e4066Sahrens 	    (u_longlong_t)ds->ds_creation_txg);
1380fa9e4066Sahrens 	(void) printf("\t\tdeadlist_obj = %llu\n",
1381fa9e4066Sahrens 	    (u_longlong_t)ds->ds_deadlist_obj);
1382fa9e4066Sahrens 	(void) printf("\t\tused_bytes = %s\n", used);
1383fa9e4066Sahrens 	(void) printf("\t\tcompressed_bytes = %s\n", compressed);
1384fa9e4066Sahrens 	(void) printf("\t\tuncompressed_bytes = %s\n", uncompressed);
1385fa9e4066Sahrens 	(void) printf("\t\tunique = %s\n", unique);
1386fa9e4066Sahrens 	(void) printf("\t\tfsid_guid = %llu\n",
1387fa9e4066Sahrens 	    (u_longlong_t)ds->ds_fsid_guid);
1388fa9e4066Sahrens 	(void) printf("\t\tguid = %llu\n",
1389fa9e4066Sahrens 	    (u_longlong_t)ds->ds_guid);
139099653d4eSeschrock 	(void) printf("\t\tflags = %llx\n",
139199653d4eSeschrock 	    (u_longlong_t)ds->ds_flags);
1392088f3894Sahrens 	(void) printf("\t\tnext_clones_obj = %llu\n",
1393088f3894Sahrens 	    (u_longlong_t)ds->ds_next_clones_obj);
1394bb0ade09Sahrens 	(void) printf("\t\tprops_obj = %llu\n",
1395bb0ade09Sahrens 	    (u_longlong_t)ds->ds_props_obj);
1396fa9e4066Sahrens 	(void) printf("\t\tbp = %s\n", blkbuf);
1397fa9e4066Sahrens }
1398fa9e4066Sahrens 
1399cde58dbcSMatthew Ahrens /* ARGSUSED */
1400cde58dbcSMatthew Ahrens static int
1401ad135b5dSChristopher Siden dump_bptree_cb(void *arg, const blkptr_t *bp, dmu_tx_t *tx)
1402ad135b5dSChristopher Siden {
1403ad135b5dSChristopher Siden 	char blkbuf[BP_SPRINTF_LEN];
1404ad135b5dSChristopher Siden 
1405ad135b5dSChristopher Siden 	if (bp->blk_birth != 0) {
140643466aaeSMax Grossman 		snprintf_blkptr(blkbuf, sizeof (blkbuf), bp);
1407ad135b5dSChristopher Siden 		(void) printf("\t%s\n", blkbuf);
1408ad135b5dSChristopher Siden 	}
1409ad135b5dSChristopher Siden 	return (0);
1410ad135b5dSChristopher Siden }
1411ad135b5dSChristopher Siden 
1412ad135b5dSChristopher Siden static void
1413ad135b5dSChristopher Siden dump_bptree(objset_t *os, uint64_t obj, char *name)
1414ad135b5dSChristopher Siden {
1415ad135b5dSChristopher Siden 	char bytes[32];
1416ad135b5dSChristopher Siden 	bptree_phys_t *bt;
1417ad135b5dSChristopher Siden 	dmu_buf_t *db;
1418ad135b5dSChristopher Siden 
1419ad135b5dSChristopher Siden 	if (dump_opt['d'] < 3)
1420ad135b5dSChristopher Siden 		return;
1421ad135b5dSChristopher Siden 
1422b420f3adSRichard Lowe 	VERIFY3U(0, ==, dmu_bonus_hold(os, obj, FTAG, &db));
1423ad135b5dSChristopher Siden 	bt = db->db_data;
1424ad135b5dSChristopher Siden 	zdb_nicenum(bt->bt_bytes, bytes);
1425ad135b5dSChristopher Siden 	(void) printf("\n    %s: %llu datasets, %s\n",
1426ad135b5dSChristopher Siden 	    name, (unsigned long long)(bt->bt_end - bt->bt_begin), bytes);
1427ad135b5dSChristopher Siden 	dmu_buf_rele(db, FTAG);
1428ad135b5dSChristopher Siden 
1429ad135b5dSChristopher Siden 	if (dump_opt['d'] < 5)
1430ad135b5dSChristopher Siden 		return;
1431ad135b5dSChristopher Siden 
1432ad135b5dSChristopher Siden 	(void) printf("\n");
1433ad135b5dSChristopher Siden 
1434ad135b5dSChristopher Siden 	(void) bptree_iterate(os, obj, B_FALSE, dump_bptree_cb, NULL, NULL);
1435ad135b5dSChristopher Siden }
1436ad135b5dSChristopher Siden 
1437ad135b5dSChristopher Siden /* ARGSUSED */
1438ad135b5dSChristopher Siden static int
1439cde58dbcSMatthew Ahrens dump_bpobj_cb(void *arg, const blkptr_t *bp, dmu_tx_t *tx)
1440fa9e4066Sahrens {
1441cde58dbcSMatthew Ahrens 	char blkbuf[BP_SPRINTF_LEN];
1442cde58dbcSMatthew Ahrens 
1443cde58dbcSMatthew Ahrens 	ASSERT(bp->blk_birth != 0);
144443466aaeSMax Grossman 	snprintf_blkptr_compact(blkbuf, sizeof (blkbuf), bp);
1445cde58dbcSMatthew Ahrens 	(void) printf("\t%s\n", blkbuf);
1446cde58dbcSMatthew Ahrens 	return (0);
1447cde58dbcSMatthew Ahrens }
1448cde58dbcSMatthew Ahrens 
1449cde58dbcSMatthew Ahrens static void
1450732885fcSMatthew Ahrens dump_full_bpobj(bpobj_t *bpo, char *name, int indent)
1451cde58dbcSMatthew Ahrens {
14523f9d6ad7SLin Ling 	char bytes[32];
14533f9d6ad7SLin Ling 	char comp[32];
14543f9d6ad7SLin Ling 	char uncomp[32];
1455fa9e4066Sahrens 
1456fa9e4066Sahrens 	if (dump_opt['d'] < 3)
1457fa9e4066Sahrens 		return;
1458fa9e4066Sahrens 
1459cde58dbcSMatthew Ahrens 	zdb_nicenum(bpo->bpo_phys->bpo_bytes, bytes);
1460d0475637SMatthew Ahrens 	if (bpo->bpo_havesubobj && bpo->bpo_phys->bpo_subobjs != 0) {
1461cde58dbcSMatthew Ahrens 		zdb_nicenum(bpo->bpo_phys->bpo_comp, comp);
1462cde58dbcSMatthew Ahrens 		zdb_nicenum(bpo->bpo_phys->bpo_uncomp, uncomp);
1463d0475637SMatthew Ahrens 		(void) printf("    %*s: object %llu, %llu local blkptrs, "
1464732885fcSMatthew Ahrens 		    "%llu subobjs in object %llu, %s (%s/%s comp)\n",
1465d0475637SMatthew Ahrens 		    indent * 8, name,
1466d0475637SMatthew Ahrens 		    (u_longlong_t)bpo->bpo_object,
1467d0475637SMatthew Ahrens 		    (u_longlong_t)bpo->bpo_phys->bpo_num_blkptrs,
1468cde58dbcSMatthew Ahrens 		    (u_longlong_t)bpo->bpo_phys->bpo_num_subobjs,
1469732885fcSMatthew Ahrens 		    (u_longlong_t)bpo->bpo_phys->bpo_subobjs,
147099653d4eSeschrock 		    bytes, comp, uncomp);
1471d0475637SMatthew Ahrens 
1472d0475637SMatthew Ahrens 		for (uint64_t i = 0; i < bpo->bpo_phys->bpo_num_subobjs; i++) {
1473d0475637SMatthew Ahrens 			uint64_t subobj;
1474d0475637SMatthew Ahrens 			bpobj_t subbpo;
1475d0475637SMatthew Ahrens 			int error;
1476d0475637SMatthew Ahrens 			VERIFY0(dmu_read(bpo->bpo_os,
1477d0475637SMatthew Ahrens 			    bpo->bpo_phys->bpo_subobjs,
1478d0475637SMatthew Ahrens 			    i * sizeof (subobj), sizeof (subobj), &subobj, 0));
1479d0475637SMatthew Ahrens 			error = bpobj_open(&subbpo, bpo->bpo_os, subobj);
1480d0475637SMatthew Ahrens 			if (error != 0) {
1481d0475637SMatthew Ahrens 				(void) printf("ERROR %u while trying to open "
1482d0475637SMatthew Ahrens 				    "subobj id %llu\n",
1483d0475637SMatthew Ahrens 				    error, (u_longlong_t)subobj);
1484d0475637SMatthew Ahrens 				continue;
1485d0475637SMatthew Ahrens 			}
1486732885fcSMatthew Ahrens 			dump_full_bpobj(&subbpo, "subobj", indent + 1);
148777061867SMatthew Ahrens 			bpobj_close(&subbpo);
1488d0475637SMatthew Ahrens 		}
148999653d4eSeschrock 	} else {
1490d0475637SMatthew Ahrens 		(void) printf("    %*s: object %llu, %llu blkptrs, %s\n",
1491d0475637SMatthew Ahrens 		    indent * 8, name,
1492d0475637SMatthew Ahrens 		    (u_longlong_t)bpo->bpo_object,
1493d0475637SMatthew Ahrens 		    (u_longlong_t)bpo->bpo_phys->bpo_num_blkptrs,
1494d0475637SMatthew Ahrens 		    bytes);
149599653d4eSeschrock 	}
1496fa9e4066Sahrens 
1497cde58dbcSMatthew Ahrens 	if (dump_opt['d'] < 5)
1498fa9e4066Sahrens 		return;
1499fa9e4066Sahrens 
1500fa9e4066Sahrens 
1501d0475637SMatthew Ahrens 	if (indent == 0) {
1502cde58dbcSMatthew Ahrens 		(void) bpobj_iterate_nofree(bpo, dump_bpobj_cb, NULL, NULL);
1503d0475637SMatthew Ahrens 		(void) printf("\n");
1504d0475637SMatthew Ahrens 	}
1505fa9e4066Sahrens }
1506fa9e4066Sahrens 
1507cde58dbcSMatthew Ahrens static void
1508cde58dbcSMatthew Ahrens dump_deadlist(dsl_deadlist_t *dl)
1509cde58dbcSMatthew Ahrens {
1510cde58dbcSMatthew Ahrens 	dsl_deadlist_entry_t *dle;
1511d0475637SMatthew Ahrens 	uint64_t unused;
1512cde58dbcSMatthew Ahrens 	char bytes[32];
1513cde58dbcSMatthew Ahrens 	char comp[32];
1514cde58dbcSMatthew Ahrens 	char uncomp[32];
1515cde58dbcSMatthew Ahrens 
1516cde58dbcSMatthew Ahrens 	if (dump_opt['d'] < 3)
1517cde58dbcSMatthew Ahrens 		return;
1518cde58dbcSMatthew Ahrens 
151990c76c66SMatthew Ahrens 	if (dl->dl_oldfmt) {
1520732885fcSMatthew Ahrens 		dump_full_bpobj(&dl->dl_bpobj, "old-format deadlist", 0);
152190c76c66SMatthew Ahrens 		return;
152290c76c66SMatthew Ahrens 	}
152390c76c66SMatthew Ahrens 
1524cde58dbcSMatthew Ahrens 	zdb_nicenum(dl->dl_phys->dl_used, bytes);
1525cde58dbcSMatthew Ahrens 	zdb_nicenum(dl->dl_phys->dl_comp, comp);
1526cde58dbcSMatthew Ahrens 	zdb_nicenum(dl->dl_phys->dl_uncomp, uncomp);
1527cde58dbcSMatthew Ahrens 	(void) printf("\n    Deadlist: %s (%s/%s comp)\n",
1528cde58dbcSMatthew Ahrens 	    bytes, comp, uncomp);
1529cde58dbcSMatthew Ahrens 
1530cde58dbcSMatthew Ahrens 	if (dump_opt['d'] < 4)
1531cde58dbcSMatthew Ahrens 		return;
1532cde58dbcSMatthew Ahrens 
1533cde58dbcSMatthew Ahrens 	(void) printf("\n");
1534cde58dbcSMatthew Ahrens 
1535d0475637SMatthew Ahrens 	/* force the tree to be loaded */
1536d0475637SMatthew Ahrens 	dsl_deadlist_space_range(dl, 0, UINT64_MAX, &unused, &unused, &unused);
1537d0475637SMatthew Ahrens 
1538cde58dbcSMatthew Ahrens 	for (dle = avl_first(&dl->dl_tree); dle;
1539cde58dbcSMatthew Ahrens 	    dle = AVL_NEXT(&dl->dl_tree, dle)) {
1540d0475637SMatthew Ahrens 		if (dump_opt['d'] >= 5) {
1541d0475637SMatthew Ahrens 			char buf[128];
1542d0475637SMatthew Ahrens 			(void) snprintf(buf, sizeof (buf), "mintxg %llu -> ",
1543d0475637SMatthew Ahrens 			    (longlong_t)dle->dle_mintxg,
1544d0475637SMatthew Ahrens 			    (longlong_t)dle->dle_bpobj.bpo_object);
1545d0475637SMatthew Ahrens 
1546732885fcSMatthew Ahrens 			dump_full_bpobj(&dle->dle_bpobj, buf, 0);
1547d0475637SMatthew Ahrens 		} else {
1548cde58dbcSMatthew Ahrens 			(void) printf("mintxg %llu -> obj %llu\n",
1549cde58dbcSMatthew Ahrens 			    (longlong_t)dle->dle_mintxg,
1550cde58dbcSMatthew Ahrens 			    (longlong_t)dle->dle_bpobj.bpo_object);
1551cde58dbcSMatthew Ahrens 
1552d0475637SMatthew Ahrens 		}
1553cde58dbcSMatthew Ahrens 	}
1554fa9e4066Sahrens }
1555fa9e4066Sahrens 
1556e0d35c44Smarks static avl_tree_t idx_tree;
1557e0d35c44Smarks static avl_tree_t domain_tree;
1558e0d35c44Smarks static boolean_t fuid_table_loaded;
15590a586ceaSMark Shellenbaum static boolean_t sa_loaded;
15600a586ceaSMark Shellenbaum sa_attr_type_t *sa_attr_table;
1561e0d35c44Smarks 
1562e0d35c44Smarks static void
1563e0d35c44Smarks fuid_table_destroy()
1564e0d35c44Smarks {
1565e0d35c44Smarks 	if (fuid_table_loaded) {
1566e0d35c44Smarks 		zfs_fuid_table_destroy(&idx_tree, &domain_tree);
1567e0d35c44Smarks 		fuid_table_loaded = B_FALSE;
1568e0d35c44Smarks 	}
1569e0d35c44Smarks }
1570e0d35c44Smarks 
1571e0d35c44Smarks /*
1572e0d35c44Smarks  * print uid or gid information.
1573e0d35c44Smarks  * For normal POSIX id just the id is printed in decimal format.
1574e0d35c44Smarks  * For CIFS files with FUID the fuid is printed in hex followed by
1575d0475637SMatthew Ahrens  * the domain-rid string.
1576e0d35c44Smarks  */
1577e0d35c44Smarks static void
1578e0d35c44Smarks print_idstr(uint64_t id, const char *id_type)
1579e0d35c44Smarks {
1580e0d35c44Smarks 	if (FUID_INDEX(id)) {
1581e0d35c44Smarks 		char *domain;
1582e0d35c44Smarks 
1583e0d35c44Smarks 		domain = zfs_fuid_idx_domain(&idx_tree, FUID_INDEX(id));
1584e0d35c44Smarks 		(void) printf("\t%s     %llx [%s-%d]\n", id_type,
1585e0d35c44Smarks 		    (u_longlong_t)id, domain, (int)FUID_RID(id));
1586e0d35c44Smarks 	} else {
1587e0d35c44Smarks 		(void) printf("\t%s     %llu\n", id_type, (u_longlong_t)id);
1588e0d35c44Smarks 	}
1589e0d35c44Smarks 
1590e0d35c44Smarks }
1591e0d35c44Smarks 
1592e0d35c44Smarks static void
15930a586ceaSMark Shellenbaum dump_uidgid(objset_t *os, uint64_t uid, uint64_t gid)
1594e0d35c44Smarks {
1595e0d35c44Smarks 	uint32_t uid_idx, gid_idx;
1596e0d35c44Smarks 
15970a586ceaSMark Shellenbaum 	uid_idx = FUID_INDEX(uid);
15980a586ceaSMark Shellenbaum 	gid_idx = FUID_INDEX(gid);
1599e0d35c44Smarks 
1600e0d35c44Smarks 	/* Load domain table, if not already loaded */
1601e0d35c44Smarks 	if (!fuid_table_loaded && (uid_idx || gid_idx)) {
1602e0d35c44Smarks 		uint64_t fuid_obj;
1603e0d35c44Smarks 
1604e0d35c44Smarks 		/* first find the fuid object.  It lives in the master node */
1605e0d35c44Smarks 		VERIFY(zap_lookup(os, MASTER_NODE_OBJ, ZFS_FUID_TABLES,
1606e0d35c44Smarks 		    8, 1, &fuid_obj) == 0);
160789459e17SMark Shellenbaum 		zfs_fuid_avl_tree_create(&idx_tree, &domain_tree);
1608e0d35c44Smarks 		(void) zfs_fuid_table_load(os, fuid_obj,
1609e0d35c44Smarks 		    &idx_tree, &domain_tree);
1610e0d35c44Smarks 		fuid_table_loaded = B_TRUE;
1611e0d35c44Smarks 	}
1612e0d35c44Smarks 
16130a586ceaSMark Shellenbaum 	print_idstr(uid, "uid");
16140a586ceaSMark Shellenbaum 	print_idstr(gid, "gid");
1615e0d35c44Smarks }
1616e0d35c44Smarks 
1617fa9e4066Sahrens /*ARGSUSED*/
1618fa9e4066Sahrens static void
1619fa9e4066Sahrens dump_znode(objset_t *os, uint64_t object, void *data, size_t size)
1620fa9e4066Sahrens {
1621fa9e4066Sahrens 	char path[MAXPATHLEN * 2];	/* allow for xattr and failure prefix */
16220a586ceaSMark Shellenbaum 	sa_handle_t *hdl;
16230a586ceaSMark Shellenbaum 	uint64_t xattr, rdev, gen;
16240a586ceaSMark Shellenbaum 	uint64_t uid, gid, mode, fsize, parent, links;
16258f2529deSMark Shellenbaum 	uint64_t pflags;
16260a586ceaSMark Shellenbaum 	uint64_t acctm[2], modtm[2], chgtm[2], crtm[2];
16270a586ceaSMark Shellenbaum 	time_t z_crtime, z_atime, z_mtime, z_ctime;
16288f2529deSMark Shellenbaum 	sa_bulk_attr_t bulk[12];
16290a586ceaSMark Shellenbaum 	int idx = 0;
163055434c77Sek110237 	int error;
1631fa9e4066Sahrens 
16320a586ceaSMark Shellenbaum 	if (!sa_loaded) {
16330a586ceaSMark Shellenbaum 		uint64_t sa_attrs = 0;
16340a586ceaSMark Shellenbaum 		uint64_t version;
16350a586ceaSMark Shellenbaum 
16360a586ceaSMark Shellenbaum 		VERIFY(zap_lookup(os, MASTER_NODE_OBJ, ZPL_VERSION_STR,
16370a586ceaSMark Shellenbaum 		    8, 1, &version) == 0);
16380a586ceaSMark Shellenbaum 		if (version >= ZPL_VERSION_SA) {
16390a586ceaSMark Shellenbaum 			VERIFY(zap_lookup(os, MASTER_NODE_OBJ, ZFS_SA_ATTRS,
16400a586ceaSMark Shellenbaum 			    8, 1, &sa_attrs) == 0);
16410a586ceaSMark Shellenbaum 		}
16421d8ccc7bSMark Shellenbaum 		if ((error = sa_setup(os, sa_attrs, zfs_attr_table,
16431d8ccc7bSMark Shellenbaum 		    ZPL_END, &sa_attr_table)) != 0) {
16441d8ccc7bSMark Shellenbaum 			(void) printf("sa_setup failed errno %d, can't "
16451d8ccc7bSMark Shellenbaum 			    "display znode contents\n", error);
16461d8ccc7bSMark Shellenbaum 			return;
16471d8ccc7bSMark Shellenbaum 		}
16480a586ceaSMark Shellenbaum 		sa_loaded = B_TRUE;
16490a586ceaSMark Shellenbaum 	}
16500a586ceaSMark Shellenbaum 
16510a586ceaSMark Shellenbaum 	if (sa_handle_get(os, object, NULL, SA_HDL_PRIVATE, &hdl)) {
16520a586ceaSMark Shellenbaum 		(void) printf("Failed to get handle for SA znode\n");
16530a586ceaSMark Shellenbaum 		return;
16540a586ceaSMark Shellenbaum 	}
16550a586ceaSMark Shellenbaum 
16560a586ceaSMark Shellenbaum 	SA_ADD_BULK_ATTR(bulk, idx, sa_attr_table[ZPL_UID], NULL, &uid, 8);
16570a586ceaSMark Shellenbaum 	SA_ADD_BULK_ATTR(bulk, idx, sa_attr_table[ZPL_GID], NULL, &gid, 8);
16580a586ceaSMark Shellenbaum 	SA_ADD_BULK_ATTR(bulk, idx, sa_attr_table[ZPL_LINKS], NULL,
16590a586ceaSMark Shellenbaum 	    &links, 8);
16600a586ceaSMark Shellenbaum 	SA_ADD_BULK_ATTR(bulk, idx, sa_attr_table[ZPL_GEN], NULL, &gen, 8);
16610a586ceaSMark Shellenbaum 	SA_ADD_BULK_ATTR(bulk, idx, sa_attr_table[ZPL_MODE], NULL,
16620a586ceaSMark Shellenbaum 	    &mode, 8);
16630a586ceaSMark Shellenbaum 	SA_ADD_BULK_ATTR(bulk, idx, sa_attr_table[ZPL_PARENT],
16640a586ceaSMark Shellenbaum 	    NULL, &parent, 8);
16650a586ceaSMark Shellenbaum 	SA_ADD_BULK_ATTR(bulk, idx, sa_attr_table[ZPL_SIZE], NULL,
16660a586ceaSMark Shellenbaum 	    &fsize, 8);
16670a586ceaSMark Shellenbaum 	SA_ADD_BULK_ATTR(bulk, idx, sa_attr_table[ZPL_ATIME], NULL,
16680a586ceaSMark Shellenbaum 	    acctm, 16);
16690a586ceaSMark Shellenbaum 	SA_ADD_BULK_ATTR(bulk, idx, sa_attr_table[ZPL_MTIME], NULL,
16700a586ceaSMark Shellenbaum 	    modtm, 16);
16710a586ceaSMark Shellenbaum 	SA_ADD_BULK_ATTR(bulk, idx, sa_attr_table[ZPL_CRTIME], NULL,
16720a586ceaSMark Shellenbaum 	    crtm, 16);
16730a586ceaSMark Shellenbaum 	SA_ADD_BULK_ATTR(bulk, idx, sa_attr_table[ZPL_CTIME], NULL,
16740a586ceaSMark Shellenbaum 	    chgtm, 16);
16758f2529deSMark Shellenbaum 	SA_ADD_BULK_ATTR(bulk, idx, sa_attr_table[ZPL_FLAGS], NULL,
16768f2529deSMark Shellenbaum 	    &pflags, 8);
16770a586ceaSMark Shellenbaum 
16780a586ceaSMark Shellenbaum 	if (sa_bulk_lookup(hdl, bulk, idx)) {
16790a586ceaSMark Shellenbaum 		(void) sa_handle_destroy(hdl);
16800a586ceaSMark Shellenbaum 		return;
16810a586ceaSMark Shellenbaum 	}
1682fa9e4066Sahrens 
168355434c77Sek110237 	error = zfs_obj_to_path(os, object, path, sizeof (path));
168455434c77Sek110237 	if (error != 0) {
168555434c77Sek110237 		(void) snprintf(path, sizeof (path), "\?\?\?<object#%llu>",
168655434c77Sek110237 		    (u_longlong_t)object);
168755434c77Sek110237 	}
1688fa9e4066Sahrens 	if (dump_opt['d'] < 3) {
168955434c77Sek110237 		(void) printf("\t%s\n", path);
16900a586ceaSMark Shellenbaum 		(void) sa_handle_destroy(hdl);
1691fa9e4066Sahrens 		return;
1692fa9e4066Sahrens 	}
1693fa9e4066Sahrens 
16940a586ceaSMark Shellenbaum 	z_crtime = (time_t)crtm[0];
16950a586ceaSMark Shellenbaum 	z_atime = (time_t)acctm[0];
16960a586ceaSMark Shellenbaum 	z_mtime = (time_t)modtm[0];
16970a586ceaSMark Shellenbaum 	z_ctime = (time_t)chgtm[0];
1698fa9e4066Sahrens 
169955434c77Sek110237 	(void) printf("\tpath	%s\n", path);
17000a586ceaSMark Shellenbaum 	dump_uidgid(os, uid, gid);
1701fa9e4066Sahrens 	(void) printf("\tatime	%s", ctime(&z_atime));
1702fa9e4066Sahrens 	(void) printf("\tmtime	%s", ctime(&z_mtime));
1703fa9e4066Sahrens 	(void) printf("\tctime	%s", ctime(&z_ctime));
1704fa9e4066Sahrens 	(void) printf("\tcrtime	%s", ctime(&z_crtime));
17050a586ceaSMark Shellenbaum 	(void) printf("\tgen	%llu\n", (u_longlong_t)gen);
17060a586ceaSMark Shellenbaum 	(void) printf("\tmode	%llo\n", (u_longlong_t)mode);
17070a586ceaSMark Shellenbaum 	(void) printf("\tsize	%llu\n", (u_longlong_t)fsize);
17080a586ceaSMark Shellenbaum 	(void) printf("\tparent	%llu\n", (u_longlong_t)parent);
17090a586ceaSMark Shellenbaum 	(void) printf("\tlinks	%llu\n", (u_longlong_t)links);
17108f2529deSMark Shellenbaum 	(void) printf("\tpflags	%llx\n", (u_longlong_t)pflags);
17110a586ceaSMark Shellenbaum 	if (sa_lookup(hdl, sa_attr_table[ZPL_XATTR], &xattr,
17120a586ceaSMark Shellenbaum 	    sizeof (uint64_t)) == 0)
17130a586ceaSMark Shellenbaum 		(void) printf("\txattr	%llu\n", (u_longlong_t)xattr);
17140a586ceaSMark Shellenbaum 	if (sa_lookup(hdl, sa_attr_table[ZPL_RDEV], &rdev,
17150a586ceaSMark Shellenbaum 	    sizeof (uint64_t)) == 0)
17160a586ceaSMark Shellenbaum 		(void) printf("\trdev	0x%016llx\n", (u_longlong_t)rdev);
17170a586ceaSMark Shellenbaum 	sa_handle_destroy(hdl);
1718fa9e4066Sahrens }
1719fa9e4066Sahrens 
1720fa9e4066Sahrens /*ARGSUSED*/
1721fa9e4066Sahrens static void
1722fa9e4066Sahrens dump_acl(objset_t *os, uint64_t object, void *data, size_t size)
1723fa9e4066Sahrens {
1724fa9e4066Sahrens }
1725fa9e4066Sahrens 
1726fa9e4066Sahrens /*ARGSUSED*/
1727fa9e4066Sahrens static void
1728fa9e4066Sahrens dump_dmu_objset(objset_t *os, uint64_t object, void *data, size_t size)
1729fa9e4066Sahrens {
1730fa9e4066Sahrens }
1731fa9e4066Sahrens 
17326de8f417SVictor Latushkin static object_viewer_t *object_viewer[DMU_OT_NUMTYPES + 1] = {
1733fa9e4066Sahrens 	dump_none,		/* unallocated			*/
1734fa9e4066Sahrens 	dump_zap,		/* object directory		*/
1735fa9e4066Sahrens 	dump_uint64,		/* object array			*/
1736fa9e4066Sahrens 	dump_none,		/* packed nvlist		*/
1737fa9e4066Sahrens 	dump_packed_nvlist,	/* packed nvlist size		*/
1738732885fcSMatthew Ahrens 	dump_none,		/* bpobj			*/
1739732885fcSMatthew Ahrens 	dump_bpobj,		/* bpobj header			*/
1740fa9e4066Sahrens 	dump_none,		/* SPA space map header		*/
1741fa9e4066Sahrens 	dump_none,		/* SPA space map		*/
1742fa9e4066Sahrens 	dump_none,		/* ZIL intent log		*/
1743fa9e4066Sahrens 	dump_dnode,		/* DMU dnode			*/
1744fa9e4066Sahrens 	dump_dmu_objset,	/* DMU objset			*/
1745fa9e4066Sahrens 	dump_dsl_dir,		/* DSL directory		*/
1746fa9e4066Sahrens 	dump_zap,		/* DSL directory child map	*/
1747fa9e4066Sahrens 	dump_zap,		/* DSL dataset snap map		*/
1748fa9e4066Sahrens 	dump_zap,		/* DSL props			*/
1749fa9e4066Sahrens 	dump_dsl_dataset,	/* DSL dataset			*/
1750fa9e4066Sahrens 	dump_znode,		/* ZFS znode			*/
1751da6c28aaSamw 	dump_acl,		/* ZFS V0 ACL			*/
1752fa9e4066Sahrens 	dump_uint8,		/* ZFS plain file		*/
1753e7437265Sahrens 	dump_zpldir,		/* ZFS directory		*/
1754fa9e4066Sahrens 	dump_zap,		/* ZFS master node		*/
1755fa9e4066Sahrens 	dump_zap,		/* ZFS delete queue		*/
1756fa9e4066Sahrens 	dump_uint8,		/* zvol object			*/
1757fa9e4066Sahrens 	dump_zap,		/* zvol prop			*/
1758fa9e4066Sahrens 	dump_uint8,		/* other uint8[]		*/
1759fa9e4066Sahrens 	dump_uint64,		/* other uint64[]		*/
1760fa9e4066Sahrens 	dump_zap,		/* other ZAP			*/
1761ea8dc4b6Seschrock 	dump_zap,		/* persistent error log		*/
176206eeb2adSek110237 	dump_uint8,		/* SPA history			*/
17634445fffbSMatthew Ahrens 	dump_history_offsets,	/* SPA history offsets		*/
1764b1b8ab34Slling 	dump_zap,		/* Pool properties		*/
1765ecd6cf80Smarks 	dump_zap,		/* DSL permissions		*/
1766da6c28aaSamw 	dump_acl,		/* ZFS ACL			*/
1767da6c28aaSamw 	dump_uint8,		/* ZFS SYSACL			*/
1768da6c28aaSamw 	dump_none,		/* FUID nvlist			*/
1769da6c28aaSamw 	dump_packed_nvlist,	/* FUID nvlist size		*/
1770088f3894Sahrens 	dump_zap,		/* DSL dataset next clones	*/
1771088f3894Sahrens 	dump_zap,		/* DSL scrub queue		*/
177214843421SMatthew Ahrens 	dump_zap,		/* ZFS user/group used		*/
177314843421SMatthew Ahrens 	dump_zap,		/* ZFS user/group quota		*/
1774842727c2SChris Kirby 	dump_zap,		/* snapshot refcount tags	*/
1775486ae710SMatthew Ahrens 	dump_ddt_zap,		/* DDT ZAP object		*/
1776b24ab676SJeff Bonwick 	dump_zap,		/* DDT statistics		*/
17770a586ceaSMark Shellenbaum 	dump_znode,		/* SA object			*/
17780a586ceaSMark Shellenbaum 	dump_zap,		/* SA Master Node		*/
17790a586ceaSMark Shellenbaum 	dump_sa_attrs,		/* SA attribute registration	*/
17800a586ceaSMark Shellenbaum 	dump_sa_layouts,	/* SA attribute layouts		*/
17813f9d6ad7SLin Ling 	dump_zap,		/* DSL scrub translations	*/
17823f9d6ad7SLin Ling 	dump_none,		/* fake dedup BP		*/
1783cde58dbcSMatthew Ahrens 	dump_zap,		/* deadlist			*/
1784cde58dbcSMatthew Ahrens 	dump_none,		/* deadlist hdr			*/
1785cde58dbcSMatthew Ahrens 	dump_zap,		/* dsl clones			*/
1786732885fcSMatthew Ahrens 	dump_bpobj_subobjs,	/* bpobj subobjs		*/
17870a586ceaSMark Shellenbaum 	dump_unknown,		/* Unknown type, must be last	*/
1788fa9e4066Sahrens };
1789fa9e4066Sahrens 
1790fa9e4066Sahrens static void
1791fa9e4066Sahrens dump_object(objset_t *os, uint64_t object, int verbosity, int *print_header)
1792fa9e4066Sahrens {
1793fa9e4066Sahrens 	dmu_buf_t *db = NULL;
1794fa9e4066Sahrens 	dmu_object_info_t doi;
1795fa9e4066Sahrens 	dnode_t *dn;
1796fa9e4066Sahrens 	void *bonus = NULL;
1797fa9e4066Sahrens 	size_t bsize = 0;
17983f9d6ad7SLin Ling 	char iblk[32], dblk[32], lsize[32], asize[32], fill[32];
17993f9d6ad7SLin Ling 	char bonus_size[32];
1800fa9e4066Sahrens 	char aux[50];
1801fa9e4066Sahrens 	int error;
1802fa9e4066Sahrens 
1803fa9e4066Sahrens 	if (*print_header) {
1804b24ab676SJeff Bonwick 		(void) printf("\n%10s  %3s  %5s  %5s  %5s  %5s  %6s  %s\n",
1805b24ab676SJeff Bonwick 		    "Object", "lvl", "iblk", "dblk", "dsize", "lsize",
1806b24ab676SJeff Bonwick 		    "%full", "type");
1807fa9e4066Sahrens 		*print_header = 0;
1808fa9e4066Sahrens 	}
1809fa9e4066Sahrens 
1810fa9e4066Sahrens 	if (object == 0) {
1811744947dcSTom Erickson 		dn = DMU_META_DNODE(os);
1812fa9e4066Sahrens 	} else {
1813ea8dc4b6Seschrock 		error = dmu_bonus_hold(os, object, FTAG, &db);
1814ea8dc4b6Seschrock 		if (error)
1815ea8dc4b6Seschrock 			fatal("dmu_bonus_hold(%llu) failed, errno %u",
1816ea8dc4b6Seschrock 			    object, error);
1817fa9e4066Sahrens 		bonus = db->db_data;
1818fa9e4066Sahrens 		bsize = db->db_size;
1819744947dcSTom Erickson 		dn = DB_DNODE((dmu_buf_impl_t *)db);
1820fa9e4066Sahrens 	}
1821fa9e4066Sahrens 	dmu_object_info_from_dnode(dn, &doi);
1822fa9e4066Sahrens 
18233f9d6ad7SLin Ling 	zdb_nicenum(doi.doi_metadata_block_size, iblk);
18243f9d6ad7SLin Ling 	zdb_nicenum(doi.doi_data_block_size, dblk);
18253f9d6ad7SLin Ling 	zdb_nicenum(doi.doi_max_offset, lsize);
18263f9d6ad7SLin Ling 	zdb_nicenum(doi.doi_physical_blocks_512 << 9, asize);
18273f9d6ad7SLin Ling 	zdb_nicenum(doi.doi_bonus_size, bonus_size);
1828b24ab676SJeff Bonwick 	(void) sprintf(fill, "%6.2f", 100.0 * doi.doi_fill_count *
1829bbfd46c4SJeff Bonwick 	    doi.doi_data_block_size / (object == 0 ? DNODES_PER_BLOCK : 1) /
1830bbfd46c4SJeff Bonwick 	    doi.doi_max_offset);
1831fa9e4066Sahrens 
1832fa9e4066Sahrens 	aux[0] = '\0';
1833fa9e4066Sahrens 
1834e7437265Sahrens 	if (doi.doi_checksum != ZIO_CHECKSUM_INHERIT || verbosity >= 6) {
1835fa9e4066Sahrens 		(void) snprintf(aux + strlen(aux), sizeof (aux), " (K=%s)",
18366de8f417SVictor Latushkin 		    ZDB_CHECKSUM_NAME(doi.doi_checksum));
1837e7437265Sahrens 	}
1838fa9e4066Sahrens 
1839e7437265Sahrens 	if (doi.doi_compress != ZIO_COMPRESS_INHERIT || verbosity >= 6) {
1840fa9e4066Sahrens 		(void) snprintf(aux + strlen(aux), sizeof (aux), " (Z=%s)",
18416de8f417SVictor Latushkin 		    ZDB_COMPRESS_NAME(doi.doi_compress));
1842e7437265Sahrens 	}
1843fa9e4066Sahrens 
1844b24ab676SJeff Bonwick 	(void) printf("%10lld  %3u  %5s  %5s  %5s  %5s  %6s  %s%s\n",
1845b24ab676SJeff Bonwick 	    (u_longlong_t)object, doi.doi_indirection, iblk, dblk,
1846b24ab676SJeff Bonwick 	    asize, lsize, fill, ZDB_OT_NAME(doi.doi_type), aux);
1847fa9e4066Sahrens 
1848fa9e4066Sahrens 	if (doi.doi_bonus_type != DMU_OT_NONE && verbosity > 3) {
1849b24ab676SJeff Bonwick 		(void) printf("%10s  %3s  %5s  %5s  %5s  %5s  %6s  %s\n",
1850b24ab676SJeff Bonwick 		    "", "", "", "", "", bonus_size, "bonus",
18516de8f417SVictor Latushkin 		    ZDB_OT_NAME(doi.doi_bonus_type));
1852fa9e4066Sahrens 	}
1853fa9e4066Sahrens 
1854fa9e4066Sahrens 	if (verbosity >= 4) {
18550a586ceaSMark Shellenbaum 		(void) printf("\tdnode flags: %s%s%s\n",
185614843421SMatthew Ahrens 		    (dn->dn_phys->dn_flags & DNODE_FLAG_USED_BYTES) ?
185714843421SMatthew Ahrens 		    "USED_BYTES " : "",
185814843421SMatthew Ahrens 		    (dn->dn_phys->dn_flags & DNODE_FLAG_USERUSED_ACCOUNTED) ?
18590a586ceaSMark Shellenbaum 		    "USERUSED_ACCOUNTED " : "",
18600a586ceaSMark Shellenbaum 		    (dn->dn_phys->dn_flags & DNODE_FLAG_SPILL_BLKPTR) ?
18610a586ceaSMark Shellenbaum 		    "SPILL_BLKPTR" : "");
186214843421SMatthew Ahrens 		(void) printf("\tdnode maxblkid: %llu\n",
186314843421SMatthew Ahrens 		    (longlong_t)dn->dn_phys->dn_maxblkid);
186414843421SMatthew Ahrens 
18656de8f417SVictor Latushkin 		object_viewer[ZDB_OT_TYPE(doi.doi_bonus_type)](os, object,
18666de8f417SVictor Latushkin 		    bonus, bsize);
18676de8f417SVictor Latushkin 		object_viewer[ZDB_OT_TYPE(doi.doi_type)](os, object, NULL, 0);
1868fa9e4066Sahrens 		*print_header = 1;
1869fa9e4066Sahrens 	}
1870fa9e4066Sahrens 
1871fa9e4066Sahrens 	if (verbosity >= 5)
187288b7b0f2SMatthew Ahrens 		dump_indirect(dn);
1873fa9e4066Sahrens 
1874fa9e4066Sahrens 	if (verbosity >= 5) {
1875fa9e4066Sahrens 		/*
1876fa9e4066Sahrens 		 * Report the list of segments that comprise the object.
1877fa9e4066Sahrens 		 */
1878fa9e4066Sahrens 		uint64_t start = 0;
1879fa9e4066Sahrens 		uint64_t end;
1880fa9e4066Sahrens 		uint64_t blkfill = 1;
1881fa9e4066Sahrens 		int minlvl = 1;
1882fa9e4066Sahrens 
1883fa9e4066Sahrens 		if (dn->dn_type == DMU_OT_DNODE) {
1884fa9e4066Sahrens 			minlvl = 0;
1885fa9e4066Sahrens 			blkfill = DNODES_PER_BLOCK;
1886fa9e4066Sahrens 		}
1887fa9e4066Sahrens 
1888fa9e4066Sahrens 		for (;;) {
18893f9d6ad7SLin Ling 			char segsize[32];
1890cdb0ab79Smaybee 			error = dnode_next_offset(dn,
1891cdb0ab79Smaybee 			    0, &start, minlvl, blkfill, 0);
1892fa9e4066Sahrens 			if (error)
1893fa9e4066Sahrens 				break;
1894fa9e4066Sahrens 			end = start;
1895cdb0ab79Smaybee 			error = dnode_next_offset(dn,
1896cdb0ab79Smaybee 			    DNODE_FIND_HOLE, &end, minlvl, blkfill, 0);
18973f9d6ad7SLin Ling 			zdb_nicenum(end - start, segsize);
1898fa9e4066Sahrens 			(void) printf("\t\tsegment [%016llx, %016llx)"
1899fa9e4066Sahrens 			    " size %5s\n", (u_longlong_t)start,
1900fa9e4066Sahrens 			    (u_longlong_t)end, segsize);
1901fa9e4066Sahrens 			if (error)
1902fa9e4066Sahrens 				break;
1903fa9e4066Sahrens 			start = end;
1904fa9e4066Sahrens 		}
1905fa9e4066Sahrens 	}
1906fa9e4066Sahrens 
1907fa9e4066Sahrens 	if (db != NULL)
1908ea8dc4b6Seschrock 		dmu_buf_rele(db, FTAG);
1909fa9e4066Sahrens }
1910fa9e4066Sahrens 
1911fa9e4066Sahrens static char *objset_types[DMU_OST_NUMTYPES] = {
1912fa9e4066Sahrens 	"NONE", "META", "ZPL", "ZVOL", "OTHER", "ANY" };
1913fa9e4066Sahrens 
1914fa9e4066Sahrens static void
1915fa9e4066Sahrens dump_dir(objset_t *os)
1916fa9e4066Sahrens {
1917fa9e4066Sahrens 	dmu_objset_stats_t dds;
1918fa9e4066Sahrens 	uint64_t object, object_count;
1919a2eea2e1Sahrens 	uint64_t refdbytes, usedobjs, scratch;
19203f9d6ad7SLin Ling 	char numbuf[32];
192114843421SMatthew Ahrens 	char blkbuf[BP_SPRINTF_LEN + 20];
1922fa9e4066Sahrens 	char osname[MAXNAMELEN];
1923fa9e4066Sahrens 	char *type = "UNKNOWN";
1924fa9e4066Sahrens 	int verbosity = dump_opt['d'];
1925fa9e4066Sahrens 	int print_header = 1;
1926fa9e4066Sahrens 	int i, error;
1927fa9e4066Sahrens 
19283b2aab18SMatthew Ahrens 	dsl_pool_config_enter(dmu_objset_pool(os), FTAG);
1929a2eea2e1Sahrens 	dmu_objset_fast_stat(os, &dds);
19303b2aab18SMatthew Ahrens 	dsl_pool_config_exit(dmu_objset_pool(os), FTAG);
1931fa9e4066Sahrens 
1932fa9e4066Sahrens 	if (dds.dds_type < DMU_OST_NUMTYPES)
1933fa9e4066Sahrens 		type = objset_types[dds.dds_type];
1934fa9e4066Sahrens 
1935fa9e4066Sahrens 	if (dds.dds_type == DMU_OST_META) {
1936fa9e4066Sahrens 		dds.dds_creation_txg = TXG_INITIAL;
19375d7b4d43SMatthew Ahrens 		usedobjs = BP_GET_FILL(os->os_rootbp);
1938c1379625SJustin T. Gibbs 		refdbytes = dsl_dir_phys(os->os_spa->spa_dsl_pool->dp_mos_dir)->
1939c1379625SJustin T. Gibbs 		    dd_used_bytes;
1940a2eea2e1Sahrens 	} else {
1941a2eea2e1Sahrens 		dmu_objset_space(os, &refdbytes, &scratch, &usedobjs, &scratch);
1942fa9e4066Sahrens 	}
1943fa9e4066Sahrens 
19445d7b4d43SMatthew Ahrens 	ASSERT3U(usedobjs, ==, BP_GET_FILL(os->os_rootbp));
1945fa9e4066Sahrens 
19463f9d6ad7SLin Ling 	zdb_nicenum(refdbytes, numbuf);
1947fa9e4066Sahrens 
1948fa9e4066Sahrens 	if (verbosity >= 4) {
194943466aaeSMax Grossman 		(void) snprintf(blkbuf, sizeof (blkbuf), ", rootbp ");
195043466aaeSMax Grossman 		(void) snprintf_blkptr(blkbuf + strlen(blkbuf),
195143466aaeSMax Grossman 		    sizeof (blkbuf) - strlen(blkbuf), os->os_rootbp);
1952fa9e4066Sahrens 	} else {
1953fa9e4066Sahrens 		blkbuf[0] = '\0';
1954fa9e4066Sahrens 	}
1955fa9e4066Sahrens 
1956fa9e4066Sahrens 	dmu_objset_name(os, osname);
1957fa9e4066Sahrens 
1958a2eea2e1Sahrens 	(void) printf("Dataset %s [%s], ID %llu, cr_txg %llu, "
1959fa9e4066Sahrens 	    "%s, %llu objects%s\n",
1960fa9e4066Sahrens 	    osname, type, (u_longlong_t)dmu_objset_id(os),
1961fa9e4066Sahrens 	    (u_longlong_t)dds.dds_creation_txg,
1962a2eea2e1Sahrens 	    numbuf, (u_longlong_t)usedobjs, blkbuf);
1963fa9e4066Sahrens 
1964b24ab676SJeff Bonwick 	if (zopt_objects != 0) {
1965b24ab676SJeff Bonwick 		for (i = 0; i < zopt_objects; i++)
1966b24ab676SJeff Bonwick 			dump_object(os, zopt_object[i], verbosity,
1967b24ab676SJeff Bonwick 			    &print_header);
1968b24ab676SJeff Bonwick 		(void) printf("\n");
1969b24ab676SJeff Bonwick 		return;
1970b24ab676SJeff Bonwick 	}
1971b24ab676SJeff Bonwick 
1972b24ab676SJeff Bonwick 	if (dump_opt['i'] != 0 || verbosity >= 2)
1973fa9e4066Sahrens 		dump_intent_log(dmu_objset_zil(os));
1974fa9e4066Sahrens 
1975fa9e4066Sahrens 	if (dmu_objset_ds(os) != NULL)
1976cde58dbcSMatthew Ahrens 		dump_deadlist(&dmu_objset_ds(os)->ds_deadlist);
1977fa9e4066Sahrens 
1978fa9e4066Sahrens 	if (verbosity < 2)
1979fa9e4066Sahrens 		return;
1980fa9e4066Sahrens 
198143466aaeSMax Grossman 	if (BP_IS_HOLE(os->os_rootbp))
1982088f3894Sahrens 		return;
1983088f3894Sahrens 
1984fa9e4066Sahrens 	dump_object(os, 0, verbosity, &print_header);
198514843421SMatthew Ahrens 	object_count = 0;
1986744947dcSTom Erickson 	if (DMU_USERUSED_DNODE(os) != NULL &&
1987744947dcSTom Erickson 	    DMU_USERUSED_DNODE(os)->dn_type != 0) {
198814843421SMatthew Ahrens 		dump_object(os, DMU_USERUSED_OBJECT, verbosity, &print_header);
198914843421SMatthew Ahrens 		dump_object(os, DMU_GROUPUSED_OBJECT, verbosity, &print_header);
199014843421SMatthew Ahrens 	}
1991fa9e4066Sahrens 
1992fa9e4066Sahrens 	object = 0;
19936754306eSahrens 	while ((error = dmu_object_next(os, &object, B_FALSE, 0)) == 0) {
1994fa9e4066Sahrens 		dump_object(os, object, verbosity, &print_header);
1995fa9e4066Sahrens 		object_count++;
1996fa9e4066Sahrens 	}
1997fa9e4066Sahrens 
1998a2eea2e1Sahrens 	ASSERT3U(object_count, ==, usedobjs);
1999fa9e4066Sahrens 
2000fa9e4066Sahrens 	(void) printf("\n");
2001fa9e4066Sahrens 
2002ccba0801SRich Morris 	if (error != ESRCH) {
2003ccba0801SRich Morris 		(void) fprintf(stderr, "dmu_object_next() = %d\n", error);
2004ccba0801SRich Morris 		abort();
2005ccba0801SRich Morris 	}
2006fa9e4066Sahrens }
2007fa9e4066Sahrens 
2008fa9e4066Sahrens static void
200953b9a4a9SVictor Latushkin dump_uberblock(uberblock_t *ub, const char *header, const char *footer)
2010fa9e4066Sahrens {
2011fa9e4066Sahrens 	time_t timestamp = ub->ub_timestamp;
2012fa9e4066Sahrens 
201353b9a4a9SVictor Latushkin 	(void) printf(header ? header : "");
2014fa9e4066Sahrens 	(void) printf("\tmagic = %016llx\n", (u_longlong_t)ub->ub_magic);
2015fa9e4066Sahrens 	(void) printf("\tversion = %llu\n", (u_longlong_t)ub->ub_version);
2016fa9e4066Sahrens 	(void) printf("\ttxg = %llu\n", (u_longlong_t)ub->ub_txg);
2017fa9e4066Sahrens 	(void) printf("\tguid_sum = %llu\n", (u_longlong_t)ub->ub_guid_sum);
2018fa9e4066Sahrens 	(void) printf("\ttimestamp = %llu UTC = %s",
2019fa9e4066Sahrens 	    (u_longlong_t)ub->ub_timestamp, asctime(localtime(&timestamp)));
2020fa9e4066Sahrens 	if (dump_opt['u'] >= 3) {
2021fbabab8fSmaybee 		char blkbuf[BP_SPRINTF_LEN];
202243466aaeSMax Grossman 		snprintf_blkptr(blkbuf, sizeof (blkbuf), &ub->ub_rootbp);
2023fa9e4066Sahrens 		(void) printf("\trootbp = %s\n", blkbuf);
2024fa9e4066Sahrens 	}
202553b9a4a9SVictor Latushkin 	(void) printf(footer ? footer : "");
2026fa9e4066Sahrens }
2027fa9e4066Sahrens 
2028fa9e4066Sahrens static void
202907428bdfSVictor Latushkin dump_config(spa_t *spa)
2030fa9e4066Sahrens {
203107428bdfSVictor Latushkin 	dmu_buf_t *db;
203207428bdfSVictor Latushkin 	size_t nvsize = 0;
203307428bdfSVictor Latushkin 	int error = 0;
2034fa9e4066Sahrens 
203507428bdfSVictor Latushkin 
203607428bdfSVictor Latushkin 	error = dmu_bonus_hold(spa->spa_meta_objset,
203707428bdfSVictor Latushkin 	    spa->spa_config_object, FTAG, &db);
203807428bdfSVictor Latushkin 
203907428bdfSVictor Latushkin 	if (error == 0) {
204007428bdfSVictor Latushkin 		nvsize = *(uint64_t *)db->db_data;
204107428bdfSVictor Latushkin 		dmu_buf_rele(db, FTAG);
204207428bdfSVictor Latushkin 
204307428bdfSVictor Latushkin 		(void) printf("\nMOS Configuration:\n");
204407428bdfSVictor Latushkin 		dump_packed_nvlist(spa->spa_meta_objset,
204507428bdfSVictor Latushkin 		    spa->spa_config_object, (void *)&nvsize, 1);
204607428bdfSVictor Latushkin 	} else {
204707428bdfSVictor Latushkin 		(void) fprintf(stderr, "dmu_bonus_hold(%llu) failed, errno %d",
204807428bdfSVictor Latushkin 		    (u_longlong_t)spa->spa_config_object, error);
2049fa9e4066Sahrens 	}
2050fa9e4066Sahrens }
2051fa9e4066Sahrens 
2052fa9e4066Sahrens static void
2053c5904d13Seschrock dump_cachefile(const char *cachefile)
2054c5904d13Seschrock {
2055c5904d13Seschrock 	int fd;
2056c5904d13Seschrock 	struct stat64 statbuf;
2057c5904d13Seschrock 	char *buf;
2058c5904d13Seschrock 	nvlist_t *config;
2059c5904d13Seschrock 
2060c5904d13Seschrock 	if ((fd = open64(cachefile, O_RDONLY)) < 0) {
2061c5904d13Seschrock 		(void) printf("cannot open '%s': %s\n", cachefile,
2062c5904d13Seschrock 		    strerror(errno));
2063c5904d13Seschrock 		exit(1);
2064c5904d13Seschrock 	}
2065c5904d13Seschrock 
2066c5904d13Seschrock 	if (fstat64(fd, &statbuf) != 0) {
2067c5904d13Seschrock 		(void) printf("failed to stat '%s': %s\n", cachefile,
2068c5904d13Seschrock 		    strerror(errno));
2069c5904d13Seschrock 		exit(1);
2070c5904d13Seschrock 	}
2071c5904d13Seschrock 
2072c5904d13Seschrock 	if ((buf = malloc(statbuf.st_size)) == NULL) {
2073c5904d13Seschrock 		(void) fprintf(stderr, "failed to allocate %llu bytes\n",
2074c5904d13Seschrock 		    (u_longlong_t)statbuf.st_size);
2075c5904d13Seschrock 		exit(1);
2076c5904d13Seschrock 	}
2077c5904d13Seschrock 
2078c5904d13Seschrock 	if (read(fd, buf, statbuf.st_size) != statbuf.st_size) {
2079c5904d13Seschrock 		(void) fprintf(stderr, "failed to read %llu bytes\n",
2080c5904d13Seschrock 		    (u_longlong_t)statbuf.st_size);
2081c5904d13Seschrock 		exit(1);
2082c5904d13Seschrock 	}
2083c5904d13Seschrock 
2084c5904d13Seschrock 	(void) close(fd);
2085c5904d13Seschrock 
2086c5904d13Seschrock 	if (nvlist_unpack(buf, statbuf.st_size, &config, 0) != 0) {
2087c5904d13Seschrock 		(void) fprintf(stderr, "failed to unpack nvlist\n");
2088c5904d13Seschrock 		exit(1);
2089c5904d13Seschrock 	}
2090c5904d13Seschrock 
2091c5904d13Seschrock 	free(buf);
2092c5904d13Seschrock 
2093c5904d13Seschrock 	dump_nvlist(config, 0);
2094c5904d13Seschrock 
2095c5904d13Seschrock 	nvlist_free(config);
2096c5904d13Seschrock }
2097c5904d13Seschrock 
209853b9a4a9SVictor Latushkin #define	ZDB_MAX_UB_HEADER_SIZE 32
209953b9a4a9SVictor Latushkin 
210053b9a4a9SVictor Latushkin static void
210153b9a4a9SVictor Latushkin dump_label_uberblocks(vdev_label_t *lbl, uint64_t ashift)
210253b9a4a9SVictor Latushkin {
210353b9a4a9SVictor Latushkin 	vdev_t vd;
210453b9a4a9SVictor Latushkin 	vdev_t *vdp = &vd;
210553b9a4a9SVictor Latushkin 	char header[ZDB_MAX_UB_HEADER_SIZE];
210653b9a4a9SVictor Latushkin 
210753b9a4a9SVictor Latushkin 	vd.vdev_ashift = ashift;
210853b9a4a9SVictor Latushkin 	vdp->vdev_top = vdp;
210953b9a4a9SVictor Latushkin 
211053b9a4a9SVictor Latushkin 	for (int i = 0; i < VDEV_UBERBLOCK_COUNT(vdp); i++) {
211153b9a4a9SVictor Latushkin 		uint64_t uoff = VDEV_UBERBLOCK_OFFSET(vdp, i);
211253b9a4a9SVictor Latushkin 		uberblock_t *ub = (void *)((char *)lbl + uoff);
211353b9a4a9SVictor Latushkin 
211453b9a4a9SVictor Latushkin 		if (uberblock_verify(ub))
211553b9a4a9SVictor Latushkin 			continue;
211653b9a4a9SVictor Latushkin 		(void) snprintf(header, ZDB_MAX_UB_HEADER_SIZE,
211753b9a4a9SVictor Latushkin 		    "Uberblock[%d]\n", i);
211853b9a4a9SVictor Latushkin 		dump_uberblock(ub, header, "");
211953b9a4a9SVictor Latushkin 	}
212053b9a4a9SVictor Latushkin }
212153b9a4a9SVictor Latushkin 
2122c5904d13Seschrock static void
2123fa9e4066Sahrens dump_label(const char *dev)
2124fa9e4066Sahrens {
2125fa9e4066Sahrens 	int fd;
2126fa9e4066Sahrens 	vdev_label_t label;
2127c6065d0fSGeorge Wilson 	char *path, *buf = label.vl_vdev_phys.vp_nvlist;
2128fa9e4066Sahrens 	size_t buflen = sizeof (label.vl_vdev_phys.vp_nvlist);
2129fa9e4066Sahrens 	struct stat64 statbuf;
213053b9a4a9SVictor Latushkin 	uint64_t psize, ashift;
2131c6065d0fSGeorge Wilson 	int len = strlen(dev) + 1;
2132fa9e4066Sahrens 
2133c6065d0fSGeorge Wilson 	if (strncmp(dev, "/dev/dsk/", 9) == 0) {
2134c6065d0fSGeorge Wilson 		len++;
2135c6065d0fSGeorge Wilson 		path = malloc(len);
2136c6065d0fSGeorge Wilson 		(void) snprintf(path, len, "%s%s", "/dev/rdsk/", dev + 9);
2137c6065d0fSGeorge Wilson 	} else {
2138c6065d0fSGeorge Wilson 		path = strdup(dev);
2139c6065d0fSGeorge Wilson 	}
2140c6065d0fSGeorge Wilson 
2141c6065d0fSGeorge Wilson 	if ((fd = open64(path, O_RDONLY)) < 0) {
2142c6065d0fSGeorge Wilson 		(void) printf("cannot open '%s': %s\n", path, strerror(errno));
2143c6065d0fSGeorge Wilson 		free(path);
2144fa9e4066Sahrens 		exit(1);
2145fa9e4066Sahrens 	}
2146fa9e4066Sahrens 
2147fa9e4066Sahrens 	if (fstat64(fd, &statbuf) != 0) {
2148c6065d0fSGeorge Wilson 		(void) printf("failed to stat '%s': %s\n", path,
2149fa9e4066Sahrens 		    strerror(errno));
2150c6065d0fSGeorge Wilson 		free(path);
2151c6065d0fSGeorge Wilson 		(void) close(fd);
2152c6065d0fSGeorge Wilson 		exit(1);
2153c6065d0fSGeorge Wilson 	}
2154c6065d0fSGeorge Wilson 
2155c6065d0fSGeorge Wilson 	if (S_ISBLK(statbuf.st_mode)) {
2156c6065d0fSGeorge Wilson 		(void) printf("cannot use '%s': character device required\n",
2157c6065d0fSGeorge Wilson 		    path);
2158c6065d0fSGeorge Wilson 		free(path);
2159c6065d0fSGeorge Wilson 		(void) close(fd);
2160c6065d0fSGeorge Wilson 		exit(1);
2161fa9e4066Sahrens 	}
2162fa9e4066Sahrens 
2163fa9e4066Sahrens 	psize = statbuf.st_size;
2164fa9e4066Sahrens 	psize = P2ALIGN(psize, (uint64_t)sizeof (vdev_label_t));
2165fa9e4066Sahrens 
216653b9a4a9SVictor Latushkin 	for (int l = 0; l < VDEV_LABELS; l++) {
2167fa9e4066Sahrens 		nvlist_t *config = NULL;
2168fa9e4066Sahrens 
2169fa9e4066Sahrens 		(void) printf("--------------------------------------------\n");
2170fa9e4066Sahrens 		(void) printf("LABEL %d\n", l);
2171fa9e4066Sahrens 		(void) printf("--------------------------------------------\n");
2172fa9e4066Sahrens 
21730d981225Seschrock 		if (pread64(fd, &label, sizeof (label),
2174fa9e4066Sahrens 		    vdev_label_offset(psize, l, 0)) != sizeof (label)) {
2175fa9e4066Sahrens 			(void) printf("failed to read label %d\n", l);
2176fa9e4066Sahrens 			continue;
2177fa9e4066Sahrens 		}
2178fa9e4066Sahrens 
2179fa9e4066Sahrens 		if (nvlist_unpack(buf, buflen, &config, 0) != 0) {
2180fa9e4066Sahrens 			(void) printf("failed to unpack label %d\n", l);
218153b9a4a9SVictor Latushkin 			ashift = SPA_MINBLOCKSHIFT;
218253b9a4a9SVictor Latushkin 		} else {
218353b9a4a9SVictor Latushkin 			nvlist_t *vdev_tree = NULL;
218453b9a4a9SVictor Latushkin 
2185fa9e4066Sahrens 			dump_nvlist(config, 4);
218653b9a4a9SVictor Latushkin 			if ((nvlist_lookup_nvlist(config,
218753b9a4a9SVictor Latushkin 			    ZPOOL_CONFIG_VDEV_TREE, &vdev_tree) != 0) ||
218853b9a4a9SVictor Latushkin 			    (nvlist_lookup_uint64(vdev_tree,
218953b9a4a9SVictor Latushkin 			    ZPOOL_CONFIG_ASHIFT, &ashift) != 0))
219053b9a4a9SVictor Latushkin 				ashift = SPA_MINBLOCKSHIFT;
2191fa9e4066Sahrens 			nvlist_free(config);
2192fa9e4066Sahrens 		}
219353b9a4a9SVictor Latushkin 		if (dump_opt['u'])
219453b9a4a9SVictor Latushkin 			dump_label_uberblocks(&label, ashift);
219553b9a4a9SVictor Latushkin 	}
2196c6065d0fSGeorge Wilson 
2197c6065d0fSGeorge Wilson 	free(path);
2198c6065d0fSGeorge Wilson 	(void) close(fd);
2199fa9e4066Sahrens }
2200fa9e4066Sahrens 
2201*ca0cc391SMatthew Ahrens static uint64_t dataset_feature_count[SPA_FEATURES];
2202b5152584SMatthew Ahrens 
2203fa9e4066Sahrens /*ARGSUSED*/
22041d452cf5Sahrens static int
2205fd136879SMatthew Ahrens dump_one_dir(const char *dsname, void *arg)
2206fa9e4066Sahrens {
2207fa9e4066Sahrens 	int error;
2208fa9e4066Sahrens 	objset_t *os;
2209fa9e4066Sahrens 
2210503ad85cSMatthew Ahrens 	error = dmu_objset_own(dsname, DMU_OST_ANY, B_TRUE, FTAG, &os);
2211fa9e4066Sahrens 	if (error) {
2212b24ab676SJeff Bonwick 		(void) printf("Could not open %s, error %d\n", dsname, error);
22131d452cf5Sahrens 		return (0);
2214fa9e4066Sahrens 	}
2215*ca0cc391SMatthew Ahrens 
2216*ca0cc391SMatthew Ahrens 	for (spa_feature_t f = 0; f < SPA_FEATURES; f++) {
2217*ca0cc391SMatthew Ahrens 		if (!dmu_objset_ds(os)->ds_feature_inuse[f])
2218*ca0cc391SMatthew Ahrens 			continue;
2219*ca0cc391SMatthew Ahrens 		ASSERT(spa_feature_table[f].fi_flags &
2220*ca0cc391SMatthew Ahrens 		    ZFEATURE_FLAG_PER_DATASET);
2221*ca0cc391SMatthew Ahrens 		dataset_feature_count[f]++;
2222*ca0cc391SMatthew Ahrens 	}
2223*ca0cc391SMatthew Ahrens 
2224fa9e4066Sahrens 	dump_dir(os);
2225503ad85cSMatthew Ahrens 	dmu_objset_disown(os, FTAG);
2226e0d35c44Smarks 	fuid_table_destroy();
22270a586ceaSMark Shellenbaum 	sa_loaded = B_FALSE;
22281d452cf5Sahrens 	return (0);
2229fa9e4066Sahrens }
2230fa9e4066Sahrens 
2231b24ab676SJeff Bonwick /*
2232b24ab676SJeff Bonwick  * Block statistics.
2233b24ab676SJeff Bonwick  */
2234b5152584SMatthew Ahrens #define	PSIZE_HISTO_SIZE (SPA_OLD_MAXBLOCKSIZE / SPA_MINBLOCKSIZE + 2)
2235b24ab676SJeff Bonwick typedef struct zdb_blkstats {
2236b24ab676SJeff Bonwick 	uint64_t zb_asize;
2237b24ab676SJeff Bonwick 	uint64_t zb_lsize;
2238b24ab676SJeff Bonwick 	uint64_t zb_psize;
2239b24ab676SJeff Bonwick 	uint64_t zb_count;
2240d5ee8a13SMatthew Ahrens 	uint64_t zb_gangs;
2241d5ee8a13SMatthew Ahrens 	uint64_t zb_ditto_samevdev;
2242490d05b9SMatthew Ahrens 	uint64_t zb_psize_histogram[PSIZE_HISTO_SIZE];
2243b24ab676SJeff Bonwick } zdb_blkstats_t;
2244b24ab676SJeff Bonwick 
2245b24ab676SJeff Bonwick /*
2246b24ab676SJeff Bonwick  * Extended object types to report deferred frees and dedup auto-ditto blocks.
2247b24ab676SJeff Bonwick  */
2248b24ab676SJeff Bonwick #define	ZDB_OT_DEFERRED	(DMU_OT_NUMTYPES + 0)
2249b24ab676SJeff Bonwick #define	ZDB_OT_DITTO	(DMU_OT_NUMTYPES + 1)
2250ad135b5dSChristopher Siden #define	ZDB_OT_OTHER	(DMU_OT_NUMTYPES + 2)
2251ad135b5dSChristopher Siden #define	ZDB_OT_TOTAL	(DMU_OT_NUMTYPES + 3)
2252b24ab676SJeff Bonwick 
2253b24ab676SJeff Bonwick static char *zdb_ot_extname[] = {
2254b24ab676SJeff Bonwick 	"deferred free",
2255b24ab676SJeff Bonwick 	"dedup ditto",
2256ad135b5dSChristopher Siden 	"other",
2257b24ab676SJeff Bonwick 	"Total",
2258b24ab676SJeff Bonwick };
2259b24ab676SJeff Bonwick 
2260b24ab676SJeff Bonwick #define	ZB_TOTAL	DN_MAX_LEVELS
2261b24ab676SJeff Bonwick 
2262b24ab676SJeff Bonwick typedef struct zdb_cb {
2263b24ab676SJeff Bonwick 	zdb_blkstats_t	zcb_type[ZB_TOTAL + 1][ZDB_OT_TOTAL + 1];
2264b24ab676SJeff Bonwick 	uint64_t	zcb_dedup_asize;
2265b24ab676SJeff Bonwick 	uint64_t	zcb_dedup_blocks;
22665d7b4d43SMatthew Ahrens 	uint64_t	zcb_embedded_blocks[NUM_BP_EMBEDDED_TYPES];
22675d7b4d43SMatthew Ahrens 	uint64_t	zcb_embedded_histogram[NUM_BP_EMBEDDED_TYPES]
22685d7b4d43SMatthew Ahrens 	    [BPE_PAYLOAD_SIZE];
2269490d05b9SMatthew Ahrens 	uint64_t	zcb_start;
2270490d05b9SMatthew Ahrens 	uint64_t	zcb_lastprint;
2271490d05b9SMatthew Ahrens 	uint64_t	zcb_totalasize;
2272b24ab676SJeff Bonwick 	uint64_t	zcb_errors[256];
2273b24ab676SJeff Bonwick 	int		zcb_readfails;
2274b24ab676SJeff Bonwick 	int		zcb_haderrors;
2275cde58dbcSMatthew Ahrens 	spa_t		*zcb_spa;
2276b24ab676SJeff Bonwick } zdb_cb_t;
2277b24ab676SJeff Bonwick 
2278b24ab676SJeff Bonwick static void
2279cde58dbcSMatthew Ahrens zdb_count_block(zdb_cb_t *zcb, zilog_t *zilog, const blkptr_t *bp,
2280b24ab676SJeff Bonwick     dmu_object_type_t type)
2281b24ab676SJeff Bonwick {
2282b24ab676SJeff Bonwick 	uint64_t refcnt = 0;
2283b24ab676SJeff Bonwick 
2284b24ab676SJeff Bonwick 	ASSERT(type < ZDB_OT_TOTAL);
2285b24ab676SJeff Bonwick 
2286b24ab676SJeff Bonwick 	if (zilog && zil_bp_tree_add(zilog, bp) != 0)
2287b24ab676SJeff Bonwick 		return;
2288b24ab676SJeff Bonwick 
2289b24ab676SJeff Bonwick 	for (int i = 0; i < 4; i++) {
2290b24ab676SJeff Bonwick 		int l = (i < 2) ? BP_GET_LEVEL(bp) : ZB_TOTAL;
2291b24ab676SJeff Bonwick 		int t = (i & 1) ? type : ZDB_OT_TOTAL;
2292d5ee8a13SMatthew Ahrens 		int equal;
2293b24ab676SJeff Bonwick 		zdb_blkstats_t *zb = &zcb->zcb_type[l][t];
2294b24ab676SJeff Bonwick 
2295b24ab676SJeff Bonwick 		zb->zb_asize += BP_GET_ASIZE(bp);
2296b24ab676SJeff Bonwick 		zb->zb_lsize += BP_GET_LSIZE(bp);
2297b24ab676SJeff Bonwick 		zb->zb_psize += BP_GET_PSIZE(bp);
2298b24ab676SJeff Bonwick 		zb->zb_count++;
2299b5152584SMatthew Ahrens 
2300b5152584SMatthew Ahrens 		/*
2301b5152584SMatthew Ahrens 		 * The histogram is only big enough to record blocks up to
2302b5152584SMatthew Ahrens 		 * SPA_OLD_MAXBLOCKSIZE; larger blocks go into the last,
2303b5152584SMatthew Ahrens 		 * "other", bucket.
2304b5152584SMatthew Ahrens 		 */
2305b5152584SMatthew Ahrens 		int idx = BP_GET_PSIZE(bp) >> SPA_MINBLOCKSHIFT;
2306b5152584SMatthew Ahrens 		idx = MIN(idx, SPA_OLD_MAXBLOCKSIZE / SPA_MINBLOCKSIZE + 1);
2307b5152584SMatthew Ahrens 		zb->zb_psize_histogram[idx]++;
2308d5ee8a13SMatthew Ahrens 
2309d5ee8a13SMatthew Ahrens 		zb->zb_gangs += BP_COUNT_GANG(bp);
2310d5ee8a13SMatthew Ahrens 
2311d5ee8a13SMatthew Ahrens 		switch (BP_GET_NDVAS(bp)) {
2312d5ee8a13SMatthew Ahrens 		case 2:
2313d5ee8a13SMatthew Ahrens 			if (DVA_GET_VDEV(&bp->blk_dva[0]) ==
2314d5ee8a13SMatthew Ahrens 			    DVA_GET_VDEV(&bp->blk_dva[1]))
2315d5ee8a13SMatthew Ahrens 				zb->zb_ditto_samevdev++;
2316d5ee8a13SMatthew Ahrens 			break;
2317d5ee8a13SMatthew Ahrens 		case 3:
2318d5ee8a13SMatthew Ahrens 			equal = (DVA_GET_VDEV(&bp->blk_dva[0]) ==
2319d5ee8a13SMatthew Ahrens 			    DVA_GET_VDEV(&bp->blk_dva[1])) +
2320d5ee8a13SMatthew Ahrens 			    (DVA_GET_VDEV(&bp->blk_dva[0]) ==
2321d5ee8a13SMatthew Ahrens 			    DVA_GET_VDEV(&bp->blk_dva[2])) +
2322d5ee8a13SMatthew Ahrens 			    (DVA_GET_VDEV(&bp->blk_dva[1]) ==
2323d5ee8a13SMatthew Ahrens 			    DVA_GET_VDEV(&bp->blk_dva[2]));
2324d5ee8a13SMatthew Ahrens 			if (equal != 0)
2325d5ee8a13SMatthew Ahrens 				zb->zb_ditto_samevdev++;
2326d5ee8a13SMatthew Ahrens 			break;
2327d5ee8a13SMatthew Ahrens 		}
2328d5ee8a13SMatthew Ahrens 
2329b24ab676SJeff Bonwick 	}
2330b24ab676SJeff Bonwick 
23315d7b4d43SMatthew Ahrens 	if (BP_IS_EMBEDDED(bp)) {
23325d7b4d43SMatthew Ahrens 		zcb->zcb_embedded_blocks[BPE_GET_ETYPE(bp)]++;
23335d7b4d43SMatthew Ahrens 		zcb->zcb_embedded_histogram[BPE_GET_ETYPE(bp)]
23345d7b4d43SMatthew Ahrens 		    [BPE_GET_PSIZE(bp)]++;
23355d7b4d43SMatthew Ahrens 		return;
23365d7b4d43SMatthew Ahrens 	}
23375d7b4d43SMatthew Ahrens 
2338b24ab676SJeff Bonwick 	if (dump_opt['L'])
2339b24ab676SJeff Bonwick 		return;
2340b24ab676SJeff Bonwick 
2341b24ab676SJeff Bonwick 	if (BP_GET_DEDUP(bp)) {
2342b24ab676SJeff Bonwick 		ddt_t *ddt;
2343b24ab676SJeff Bonwick 		ddt_entry_t *dde;
2344b24ab676SJeff Bonwick 
2345cde58dbcSMatthew Ahrens 		ddt = ddt_select(zcb->zcb_spa, bp);
2346b24ab676SJeff Bonwick 		ddt_enter(ddt);
2347b24ab676SJeff Bonwick 		dde = ddt_lookup(ddt, bp, B_FALSE);
2348b24ab676SJeff Bonwick 
2349b24ab676SJeff Bonwick 		if (dde == NULL) {
2350b24ab676SJeff Bonwick 			refcnt = 0;
2351b24ab676SJeff Bonwick 		} else {
2352b24ab676SJeff Bonwick 			ddt_phys_t *ddp = ddt_phys_select(dde, bp);
2353b24ab676SJeff Bonwick 			ddt_phys_decref(ddp);
2354b24ab676SJeff Bonwick 			refcnt = ddp->ddp_refcnt;
2355b24ab676SJeff Bonwick 			if (ddt_phys_total_refcnt(dde) == 0)
2356b24ab676SJeff Bonwick 				ddt_remove(ddt, dde);
2357b24ab676SJeff Bonwick 		}
2358b24ab676SJeff Bonwick 		ddt_exit(ddt);
2359b24ab676SJeff Bonwick 	}
2360b24ab676SJeff Bonwick 
2361cde58dbcSMatthew Ahrens 	VERIFY3U(zio_wait(zio_claim(NULL, zcb->zcb_spa,
2362cde58dbcSMatthew Ahrens 	    refcnt ? 0 : spa_first_txg(zcb->zcb_spa),
2363b24ab676SJeff Bonwick 	    bp, NULL, NULL, ZIO_FLAG_CANFAIL)), ==, 0);
2364b24ab676SJeff Bonwick }
2365b24ab676SJeff Bonwick 
236631d7e8faSGeorge Wilson static void
236731d7e8faSGeorge Wilson zdb_blkptr_done(zio_t *zio)
236831d7e8faSGeorge Wilson {
236931d7e8faSGeorge Wilson 	spa_t *spa = zio->io_spa;
237031d7e8faSGeorge Wilson 	blkptr_t *bp = zio->io_bp;
237131d7e8faSGeorge Wilson 	int ioerr = zio->io_error;
237231d7e8faSGeorge Wilson 	zdb_cb_t *zcb = zio->io_private;
23737802d7bfSMatthew Ahrens 	zbookmark_phys_t *zb = &zio->io_bookmark;
237431d7e8faSGeorge Wilson 
237531d7e8faSGeorge Wilson 	zio_data_buf_free(zio->io_data, zio->io_size);
237631d7e8faSGeorge Wilson 
237731d7e8faSGeorge Wilson 	mutex_enter(&spa->spa_scrub_lock);
237831d7e8faSGeorge Wilson 	spa->spa_scrub_inflight--;
237931d7e8faSGeorge Wilson 	cv_broadcast(&spa->spa_scrub_io_cv);
238031d7e8faSGeorge Wilson 
238131d7e8faSGeorge Wilson 	if (ioerr && !(zio->io_flags & ZIO_FLAG_SPECULATIVE)) {
238231d7e8faSGeorge Wilson 		char blkbuf[BP_SPRINTF_LEN];
238331d7e8faSGeorge Wilson 
238431d7e8faSGeorge Wilson 		zcb->zcb_haderrors = 1;
238531d7e8faSGeorge Wilson 		zcb->zcb_errors[ioerr]++;
238631d7e8faSGeorge Wilson 
238731d7e8faSGeorge Wilson 		if (dump_opt['b'] >= 2)
238843466aaeSMax Grossman 			snprintf_blkptr(blkbuf, sizeof (blkbuf), bp);
238931d7e8faSGeorge Wilson 		else
239031d7e8faSGeorge Wilson 			blkbuf[0] = '\0';
239131d7e8faSGeorge Wilson 
239231d7e8faSGeorge Wilson 		(void) printf("zdb_blkptr_cb: "
239331d7e8faSGeorge Wilson 		    "Got error %d reading "
239431d7e8faSGeorge Wilson 		    "<%llu, %llu, %lld, %llx> %s -- skipping\n",
239531d7e8faSGeorge Wilson 		    ioerr,
239631d7e8faSGeorge Wilson 		    (u_longlong_t)zb->zb_objset,
239731d7e8faSGeorge Wilson 		    (u_longlong_t)zb->zb_object,
239831d7e8faSGeorge Wilson 		    (u_longlong_t)zb->zb_level,
239931d7e8faSGeorge Wilson 		    (u_longlong_t)zb->zb_blkid,
240031d7e8faSGeorge Wilson 		    blkbuf);
240131d7e8faSGeorge Wilson 	}
240231d7e8faSGeorge Wilson 	mutex_exit(&spa->spa_scrub_lock);
240331d7e8faSGeorge Wilson }
240431d7e8faSGeorge Wilson 
2405b24ab676SJeff Bonwick static int
24061b912ec7SGeorge Wilson zdb_blkptr_cb(spa_t *spa, zilog_t *zilog, const blkptr_t *bp,
24077802d7bfSMatthew Ahrens     const zbookmark_phys_t *zb, const dnode_phys_t *dnp, void *arg)
2408b24ab676SJeff Bonwick {
2409b24ab676SJeff Bonwick 	zdb_cb_t *zcb = arg;
2410b24ab676SJeff Bonwick 	dmu_object_type_t type;
2411b24ab676SJeff Bonwick 	boolean_t is_metadata;
2412b24ab676SJeff Bonwick 
2413a2cdcdd2SPaul Dagnelie 	if (bp == NULL)
2414a2cdcdd2SPaul Dagnelie 		return (0);
2415a2cdcdd2SPaul Dagnelie 
241643466aaeSMax Grossman 	if (dump_opt['b'] >= 5 && bp->blk_birth > 0) {
241743466aaeSMax Grossman 		char blkbuf[BP_SPRINTF_LEN];
241843466aaeSMax Grossman 		snprintf_blkptr(blkbuf, sizeof (blkbuf), bp);
241943466aaeSMax Grossman 		(void) printf("objset %llu object %llu "
242043466aaeSMax Grossman 		    "level %lld offset 0x%llx %s\n",
242143466aaeSMax Grossman 		    (u_longlong_t)zb->zb_objset,
242243466aaeSMax Grossman 		    (u_longlong_t)zb->zb_object,
242343466aaeSMax Grossman 		    (longlong_t)zb->zb_level,
242443466aaeSMax Grossman 		    (u_longlong_t)blkid2offset(dnp, bp, zb),
242543466aaeSMax Grossman 		    blkbuf);
242643466aaeSMax Grossman 	}
242743466aaeSMax Grossman 
242843466aaeSMax Grossman 	if (BP_IS_HOLE(bp))
2429b24ab676SJeff Bonwick 		return (0);
2430b24ab676SJeff Bonwick 
2431b24ab676SJeff Bonwick 	type = BP_GET_TYPE(bp);
2432b24ab676SJeff Bonwick 
2433ad135b5dSChristopher Siden 	zdb_count_block(zcb, zilog, bp,
2434ad135b5dSChristopher Siden 	    (type & DMU_OT_NEWTYPE) ? ZDB_OT_OTHER : type);
2435b24ab676SJeff Bonwick 
2436ad135b5dSChristopher Siden 	is_metadata = (BP_GET_LEVEL(bp) != 0 || DMU_OT_IS_METADATA(type));
2437b24ab676SJeff Bonwick 
24385d7b4d43SMatthew Ahrens 	if (!BP_IS_EMBEDDED(bp) &&
24395d7b4d43SMatthew Ahrens 	    (dump_opt['c'] > 1 || (dump_opt['c'] && is_metadata))) {
2440b24ab676SJeff Bonwick 		size_t size = BP_GET_PSIZE(bp);
244131d7e8faSGeorge Wilson 		void *data = zio_data_buf_alloc(size);
2442b24ab676SJeff Bonwick 		int flags = ZIO_FLAG_CANFAIL | ZIO_FLAG_SCRUB | ZIO_FLAG_RAW;
2443b24ab676SJeff Bonwick 
2444b24ab676SJeff Bonwick 		/* If it's an intent log block, failure is expected. */
2445b24ab676SJeff Bonwick 		if (zb->zb_level == ZB_ZIL_LEVEL)
2446b24ab676SJeff Bonwick 			flags |= ZIO_FLAG_SPECULATIVE;
2447b24ab676SJeff Bonwick 
244831d7e8faSGeorge Wilson 		mutex_enter(&spa->spa_scrub_lock);
244931d7e8faSGeorge Wilson 		while (spa->spa_scrub_inflight > max_inflight)
245031d7e8faSGeorge Wilson 			cv_wait(&spa->spa_scrub_io_cv, &spa->spa_scrub_lock);
245131d7e8faSGeorge Wilson 		spa->spa_scrub_inflight++;
245231d7e8faSGeorge Wilson 		mutex_exit(&spa->spa_scrub_lock);
2453b24ab676SJeff Bonwick 
245431d7e8faSGeorge Wilson 		zio_nowait(zio_read(NULL, spa, bp, data, size,
245531d7e8faSGeorge Wilson 		    zdb_blkptr_done, zcb, ZIO_PRIORITY_ASYNC_READ, flags, zb));
2456b24ab676SJeff Bonwick 	}
2457b24ab676SJeff Bonwick 
2458b24ab676SJeff Bonwick 	zcb->zcb_readfails = 0;
2459b24ab676SJeff Bonwick 
2460f7950bf1SMatthew Ahrens 	/* only call gethrtime() every 100 blocks */
2461f7950bf1SMatthew Ahrens 	static int iters;
2462f7950bf1SMatthew Ahrens 	if (++iters > 100)
2463f7950bf1SMatthew Ahrens 		iters = 0;
2464f7950bf1SMatthew Ahrens 	else
2465f7950bf1SMatthew Ahrens 		return (0);
2466f7950bf1SMatthew Ahrens 
2467f7950bf1SMatthew Ahrens 	if (dump_opt['b'] < 5 && gethrtime() > zcb->zcb_lastprint + NANOSEC) {
2468490d05b9SMatthew Ahrens 		uint64_t now = gethrtime();
2469490d05b9SMatthew Ahrens 		char buf[10];
2470490d05b9SMatthew Ahrens 		uint64_t bytes = zcb->zcb_type[ZB_TOTAL][ZDB_OT_TOTAL].zb_asize;
2471490d05b9SMatthew Ahrens 		int kb_per_sec =
2472490d05b9SMatthew Ahrens 		    1 + bytes / (1 + ((now - zcb->zcb_start) / 1000 / 1000));
2473490d05b9SMatthew Ahrens 		int sec_remaining =
2474490d05b9SMatthew Ahrens 		    (zcb->zcb_totalasize - bytes) / 1024 / kb_per_sec;
2475490d05b9SMatthew Ahrens 
2476490d05b9SMatthew Ahrens 		zfs_nicenum(bytes, buf, sizeof (buf));
2477490d05b9SMatthew Ahrens 		(void) fprintf(stderr,
2478490d05b9SMatthew Ahrens 		    "\r%5s completed (%4dMB/s) "
2479490d05b9SMatthew Ahrens 		    "estimated time remaining: %uhr %02umin %02usec        ",
2480490d05b9SMatthew Ahrens 		    buf, kb_per_sec / 1024,
2481490d05b9SMatthew Ahrens 		    sec_remaining / 60 / 60,
2482490d05b9SMatthew Ahrens 		    sec_remaining / 60 % 60,
2483490d05b9SMatthew Ahrens 		    sec_remaining % 60);
2484490d05b9SMatthew Ahrens 
2485490d05b9SMatthew Ahrens 		zcb->zcb_lastprint = now;
2486490d05b9SMatthew Ahrens 	}
2487490d05b9SMatthew Ahrens 
2488b24ab676SJeff Bonwick 	return (0);
2489b24ab676SJeff Bonwick }
2490b24ab676SJeff Bonwick 
2491fa9e4066Sahrens static void
24920713e232SGeorge Wilson zdb_leak(void *arg, uint64_t start, uint64_t size)
2493fa9e4066Sahrens {
24940713e232SGeorge Wilson 	vdev_t *vd = arg;
2495fa9e4066Sahrens 
2496fa9e4066Sahrens 	(void) printf("leaked space: vdev %llu, offset 0x%llx, size %llu\n",
2497e14bb325SJeff Bonwick 	    (u_longlong_t)vd->vdev_id, (u_longlong_t)start, (u_longlong_t)size);
2498e14bb325SJeff Bonwick }
2499e14bb325SJeff Bonwick 
25000713e232SGeorge Wilson static metaslab_ops_t zdb_metaslab_ops = {
25012e4c9986SGeorge Wilson 	NULL	/* alloc */
2502e14bb325SJeff Bonwick };
2503e14bb325SJeff Bonwick 
2504e14bb325SJeff Bonwick static void
2505bbfd46c4SJeff Bonwick zdb_ddt_leak_init(spa_t *spa, zdb_cb_t *zcb)
2506fa9e4066Sahrens {
2507bbfd46c4SJeff Bonwick 	ddt_bookmark_t ddb = { 0 };
2508b24ab676SJeff Bonwick 	ddt_entry_t dde;
2509b24ab676SJeff Bonwick 	int error;
2510fa9e4066Sahrens 
2511bbfd46c4SJeff Bonwick 	while ((error = ddt_walk(spa, &ddb, &dde)) == 0) {
2512b24ab676SJeff Bonwick 		blkptr_t blk;
2513b24ab676SJeff Bonwick 		ddt_phys_t *ddp = dde.dde_phys;
2514bbfd46c4SJeff Bonwick 
2515bbfd46c4SJeff Bonwick 		if (ddb.ddb_class == DDT_CLASS_UNIQUE)
2516bbfd46c4SJeff Bonwick 			return;
2517bbfd46c4SJeff Bonwick 
2518b24ab676SJeff Bonwick 		ASSERT(ddt_phys_total_refcnt(&dde) > 1);
2519bbfd46c4SJeff Bonwick 
2520b24ab676SJeff Bonwick 		for (int p = 0; p < DDT_PHYS_TYPES; p++, ddp++) {
2521b24ab676SJeff Bonwick 			if (ddp->ddp_phys_birth == 0)
2522b24ab676SJeff Bonwick 				continue;
2523bbfd46c4SJeff Bonwick 			ddt_bp_create(ddb.ddb_checksum,
2524bbfd46c4SJeff Bonwick 			    &dde.dde_key, ddp, &blk);
2525b24ab676SJeff Bonwick 			if (p == DDT_PHYS_DITTO) {
2526cde58dbcSMatthew Ahrens 				zdb_count_block(zcb, NULL, &blk, ZDB_OT_DITTO);
2527b24ab676SJeff Bonwick 			} else {
2528b24ab676SJeff Bonwick 				zcb->zcb_dedup_asize +=
2529b24ab676SJeff Bonwick 				    BP_GET_ASIZE(&blk) * (ddp->ddp_refcnt - 1);
2530b24ab676SJeff Bonwick 				zcb->zcb_dedup_blocks++;
2531b24ab676SJeff Bonwick 			}
2532b24ab676SJeff Bonwick 		}
2533b24ab676SJeff Bonwick 		if (!dump_opt['L']) {
2534bbfd46c4SJeff Bonwick 			ddt_t *ddt = spa->spa_ddt[ddb.ddb_checksum];
2535b24ab676SJeff Bonwick 			ddt_enter(ddt);
2536b24ab676SJeff Bonwick 			VERIFY(ddt_lookup(ddt, &blk, B_TRUE) != NULL);
2537b24ab676SJeff Bonwick 			ddt_exit(ddt);
2538b24ab676SJeff Bonwick 		}
2539b24ab676SJeff Bonwick 	}
2540b24ab676SJeff Bonwick 
2541b24ab676SJeff Bonwick 	ASSERT(error == ENOENT);
2542b24ab676SJeff Bonwick }
2543b24ab676SJeff Bonwick 
2544b24ab676SJeff Bonwick static void
2545b24ab676SJeff Bonwick zdb_leak_init(spa_t *spa, zdb_cb_t *zcb)
2546b24ab676SJeff Bonwick {
2547cde58dbcSMatthew Ahrens 	zcb->zcb_spa = spa;
2548cde58dbcSMatthew Ahrens 
2549b24ab676SJeff Bonwick 	if (!dump_opt['L']) {
2550b24ab676SJeff Bonwick 		vdev_t *rvd = spa->spa_root_vdev;
255106be9802SMatthew Ahrens 		for (uint64_t c = 0; c < rvd->vdev_children; c++) {
2552e14bb325SJeff Bonwick 			vdev_t *vd = rvd->vdev_child[c];
255306be9802SMatthew Ahrens 			for (uint64_t m = 0; m < vd->vdev_ms_count; m++) {
2554fa9e4066Sahrens 				metaslab_t *msp = vd->vdev_ms[m];
2555fa9e4066Sahrens 				mutex_enter(&msp->ms_lock);
25560713e232SGeorge Wilson 				metaslab_unload(msp);
25570713e232SGeorge Wilson 
25580713e232SGeorge Wilson 				/*
25590713e232SGeorge Wilson 				 * For leak detection, we overload the metaslab
25600713e232SGeorge Wilson 				 * ms_tree to contain allocated segments
25610713e232SGeorge Wilson 				 * instead of free segments. As a result,
25620713e232SGeorge Wilson 				 * we can't use the normal metaslab_load/unload
25630713e232SGeorge Wilson 				 * interfaces.
25640713e232SGeorge Wilson 				 */
25650713e232SGeorge Wilson 				if (msp->ms_sm != NULL) {
256606be9802SMatthew Ahrens 					(void) fprintf(stderr,
256706be9802SMatthew Ahrens 					    "\rloading space map for "
256806be9802SMatthew Ahrens 					    "vdev %llu of %llu, "
256906be9802SMatthew Ahrens 					    "metaslab %llu of %llu ...",
257006be9802SMatthew Ahrens 					    (longlong_t)c,
257106be9802SMatthew Ahrens 					    (longlong_t)rvd->vdev_children,
257206be9802SMatthew Ahrens 					    (longlong_t)m,
257306be9802SMatthew Ahrens 					    (longlong_t)vd->vdev_ms_count);
257406be9802SMatthew Ahrens 
25750713e232SGeorge Wilson 					msp->ms_ops = &zdb_metaslab_ops;
2576f7950bf1SMatthew Ahrens 
2577f7950bf1SMatthew Ahrens 					/*
2578f7950bf1SMatthew Ahrens 					 * We don't want to spend the CPU
2579f7950bf1SMatthew Ahrens 					 * manipulating the size-ordered
2580f7950bf1SMatthew Ahrens 					 * tree, so clear the range_tree
2581f7950bf1SMatthew Ahrens 					 * ops.
2582f7950bf1SMatthew Ahrens 					 */
2583f7950bf1SMatthew Ahrens 					msp->ms_tree->rt_ops = NULL;
25840713e232SGeorge Wilson 					VERIFY0(space_map_load(msp->ms_sm,
25850713e232SGeorge Wilson 					    msp->ms_tree, SM_ALLOC));
25860713e232SGeorge Wilson 					msp->ms_loaded = B_TRUE;
25870713e232SGeorge Wilson 				}
2588fa9e4066Sahrens 				mutex_exit(&msp->ms_lock);
2589fa9e4066Sahrens 			}
2590fa9e4066Sahrens 		}
259106be9802SMatthew Ahrens 		(void) fprintf(stderr, "\n");
2592fa9e4066Sahrens 	}
2593fa9e4066Sahrens 
2594b24ab676SJeff Bonwick 	spa_config_enter(spa, SCL_CONFIG, FTAG, RW_READER);
2595b24ab676SJeff Bonwick 
2596bbfd46c4SJeff Bonwick 	zdb_ddt_leak_init(spa, zcb);
2597b24ab676SJeff Bonwick 
2598b24ab676SJeff Bonwick 	spa_config_exit(spa, SCL_CONFIG, FTAG);
2599b24ab676SJeff Bonwick }
2600b24ab676SJeff Bonwick 
2601fa9e4066Sahrens static void
2602e14bb325SJeff Bonwick zdb_leak_fini(spa_t *spa)
2603fa9e4066Sahrens {
2604b24ab676SJeff Bonwick 	if (!dump_opt['L']) {
2605fa9e4066Sahrens 		vdev_t *rvd = spa->spa_root_vdev;
2606e14bb325SJeff Bonwick 		for (int c = 0; c < rvd->vdev_children; c++) {
2607e14bb325SJeff Bonwick 			vdev_t *vd = rvd->vdev_child[c];
2608e14bb325SJeff Bonwick 			for (int m = 0; m < vd->vdev_ms_count; m++) {
2609e14bb325SJeff Bonwick 				metaslab_t *msp = vd->vdev_ms[m];
2610e14bb325SJeff Bonwick 				mutex_enter(&msp->ms_lock);
26110713e232SGeorge Wilson 
26120713e232SGeorge Wilson 				/*
26130713e232SGeorge Wilson 				 * The ms_tree has been overloaded to
26140713e232SGeorge Wilson 				 * contain allocated segments. Now that we
26150713e232SGeorge Wilson 				 * finished traversing all blocks, any
26160713e232SGeorge Wilson 				 * block that remains in the ms_tree
26170713e232SGeorge Wilson 				 * represents an allocated block that we
26180713e232SGeorge Wilson 				 * did not claim during the traversal.
26190713e232SGeorge Wilson 				 * Claimed blocks would have been removed
26200713e232SGeorge Wilson 				 * from the ms_tree.
26210713e232SGeorge Wilson 				 */
26220713e232SGeorge Wilson 				range_tree_vacate(msp->ms_tree, zdb_leak, vd);
26230713e232SGeorge Wilson 				msp->ms_loaded = B_FALSE;
26240713e232SGeorge Wilson 
2625e14bb325SJeff Bonwick 				mutex_exit(&msp->ms_lock);
2626e14bb325SJeff Bonwick 			}
2627e14bb325SJeff Bonwick 		}
2628fa9e4066Sahrens 	}
2629fa9e4066Sahrens }
2630fa9e4066Sahrens 
2631cde58dbcSMatthew Ahrens /* ARGSUSED */
2632cde58dbcSMatthew Ahrens static int
2633cde58dbcSMatthew Ahrens count_block_cb(void *arg, const blkptr_t *bp, dmu_tx_t *tx)
2634cde58dbcSMatthew Ahrens {
2635cde58dbcSMatthew Ahrens 	zdb_cb_t *zcb = arg;
2636cde58dbcSMatthew Ahrens 
2637490d05b9SMatthew Ahrens 	if (dump_opt['b'] >= 5) {
2638cde58dbcSMatthew Ahrens 		char blkbuf[BP_SPRINTF_LEN];
263943466aaeSMax Grossman 		snprintf_blkptr(blkbuf, sizeof (blkbuf), bp);
2640cde58dbcSMatthew Ahrens 		(void) printf("[%s] %s\n",
2641cde58dbcSMatthew Ahrens 		    "deferred free", blkbuf);
2642cde58dbcSMatthew Ahrens 	}
2643cde58dbcSMatthew Ahrens 	zdb_count_block(zcb, NULL, bp, ZDB_OT_DEFERRED);
2644cde58dbcSMatthew Ahrens 	return (0);
2645cde58dbcSMatthew Ahrens }
2646cde58dbcSMatthew Ahrens 
2647fa9e4066Sahrens static int
2648fa9e4066Sahrens dump_block_stats(spa_t *spa)
2649fa9e4066Sahrens {
2650fa9e4066Sahrens 	zdb_cb_t zcb = { 0 };
2651fa9e4066Sahrens 	zdb_blkstats_t *zb, *tzb;
2652b24ab676SJeff Bonwick 	uint64_t norm_alloc, norm_space, total_alloc, total_found;
2653cd088ea4SVictor Latushkin 	int flags = TRAVERSE_PRE | TRAVERSE_PREFETCH_METADATA | TRAVERSE_HARD;
26545d7b4d43SMatthew Ahrens 	boolean_t leaks = B_FALSE;
2655fa9e4066Sahrens 
2656490d05b9SMatthew Ahrens 	(void) printf("\nTraversing all blocks %s%s%s%s%s...\n\n",
265782a0a985SVictor Latushkin 	    (dump_opt['c'] || !dump_opt['L']) ? "to verify " : "",
26586365109dSVictor Latushkin 	    (dump_opt['c'] == 1) ? "metadata " : "",
265982a0a985SVictor Latushkin 	    dump_opt['c'] ? "checksums " : "",
266082a0a985SVictor Latushkin 	    (dump_opt['c'] && !dump_opt['L']) ? "and verify " : "",
266182a0a985SVictor Latushkin 	    !dump_opt['L'] ? "nothing leaked " : "");
2662fa9e4066Sahrens 
2663fa9e4066Sahrens 	/*
2664e14bb325SJeff Bonwick 	 * Load all space maps as SM_ALLOC maps, then traverse the pool
2665e14bb325SJeff Bonwick 	 * claiming each block we discover.  If the pool is perfectly
2666e14bb325SJeff Bonwick 	 * consistent, the space maps will be empty when we're done.
2667e14bb325SJeff Bonwick 	 * Anything left over is a leak; any block we can't claim (because
2668e14bb325SJeff Bonwick 	 * it's not part of any space map) is a double allocation,
2669e14bb325SJeff Bonwick 	 * reference to a freed block, or an unclaimed log block.
2670fa9e4066Sahrens 	 */
2671b24ab676SJeff Bonwick 	zdb_leak_init(spa, &zcb);
2672fa9e4066Sahrens 
2673fa9e4066Sahrens 	/*
2674fa9e4066Sahrens 	 * If there's a deferred-free bplist, process that first.
2675fa9e4066Sahrens 	 */
2676cde58dbcSMatthew Ahrens 	(void) bpobj_iterate_nofree(&spa->spa_deferred_bpobj,
2677cde58dbcSMatthew Ahrens 	    count_block_cb, &zcb, NULL);
26783b2aab18SMatthew Ahrens 	if (spa_version(spa) >= SPA_VERSION_DEADLISTS) {
2679cde58dbcSMatthew Ahrens 		(void) bpobj_iterate_nofree(&spa->spa_dsl_pool->dp_free_bpobj,
2680cde58dbcSMatthew Ahrens 		    count_block_cb, &zcb, NULL);
26813b2aab18SMatthew Ahrens 	}
26822acef22dSMatthew Ahrens 	if (spa_feature_is_active(spa, SPA_FEATURE_ASYNC_DESTROY)) {
2683b420f3adSRichard Lowe 		VERIFY3U(0, ==, bptree_iterate(spa->spa_meta_objset,
2684ad135b5dSChristopher Siden 		    spa->spa_dsl_pool->dp_bptree_obj, B_FALSE, count_block_cb,
2685ad135b5dSChristopher Siden 		    &zcb, NULL));
2686ad135b5dSChristopher Siden 	}
2687fa9e4066Sahrens 
2688bbfd46c4SJeff Bonwick 	if (dump_opt['c'] > 1)
2689bbfd46c4SJeff Bonwick 		flags |= TRAVERSE_PREFETCH_DATA;
2690bbfd46c4SJeff Bonwick 
2691490d05b9SMatthew Ahrens 	zcb.zcb_totalasize = metaslab_class_get_alloc(spa_normal_class(spa));
2692490d05b9SMatthew Ahrens 	zcb.zcb_start = zcb.zcb_lastprint = gethrtime();
2693bbfd46c4SJeff Bonwick 	zcb.zcb_haderrors |= traverse_pool(spa, 0, flags, zdb_blkptr_cb, &zcb);
2694fa9e4066Sahrens 
269531d7e8faSGeorge Wilson 	/*
269631d7e8faSGeorge Wilson 	 * If we've traversed the data blocks then we need to wait for those
269731d7e8faSGeorge Wilson 	 * I/Os to complete. We leverage "The Godfather" zio to wait on
269831d7e8faSGeorge Wilson 	 * all async I/Os to complete.
269931d7e8faSGeorge Wilson 	 */
270031d7e8faSGeorge Wilson 	if (dump_opt['c']) {
27016f834bc1SMatthew Ahrens 		for (int i = 0; i < max_ncpus; i++) {
27026f834bc1SMatthew Ahrens 			(void) zio_wait(spa->spa_async_zio_root[i]);
27036f834bc1SMatthew Ahrens 			spa->spa_async_zio_root[i] = zio_root(spa, NULL, NULL,
270431d7e8faSGeorge Wilson 			    ZIO_FLAG_CANFAIL | ZIO_FLAG_SPECULATIVE |
270531d7e8faSGeorge Wilson 			    ZIO_FLAG_GODFATHER);
270631d7e8faSGeorge Wilson 		}
27076f834bc1SMatthew Ahrens 	}
270831d7e8faSGeorge Wilson 
2709b24ab676SJeff Bonwick 	if (zcb.zcb_haderrors) {
2710fa9e4066Sahrens 		(void) printf("\nError counts:\n\n");
2711fa9e4066Sahrens 		(void) printf("\t%5s  %s\n", "errno", "count");
2712b24ab676SJeff Bonwick 		for (int e = 0; e < 256; e++) {
2713fa9e4066Sahrens 			if (zcb.zcb_errors[e] != 0) {
2714fa9e4066Sahrens 				(void) printf("\t%5d  %llu\n",
2715fa9e4066Sahrens 				    e, (u_longlong_t)zcb.zcb_errors[e]);
2716fa9e4066Sahrens 			}
2717fa9e4066Sahrens 		}
2718fa9e4066Sahrens 	}
2719fa9e4066Sahrens 
2720fa9e4066Sahrens 	/*
2721fa9e4066Sahrens 	 * Report any leaked segments.
2722fa9e4066Sahrens 	 */
2723e14bb325SJeff Bonwick 	zdb_leak_fini(spa);
2724fa9e4066Sahrens 
2725b24ab676SJeff Bonwick 	tzb = &zcb.zcb_type[ZB_TOTAL][ZDB_OT_TOTAL];
2726d41e7643Sek110237 
2727b24ab676SJeff Bonwick 	norm_alloc = metaslab_class_get_alloc(spa_normal_class(spa));
2728b24ab676SJeff Bonwick 	norm_space = metaslab_class_get_space(spa_normal_class(spa));
2729fa9e4066Sahrens 
2730b24ab676SJeff Bonwick 	total_alloc = norm_alloc + metaslab_class_get_alloc(spa_log_class(spa));
2731b24ab676SJeff Bonwick 	total_found = tzb->zb_asize - zcb.zcb_dedup_asize;
27328654d025Sperrin 
2733b24ab676SJeff Bonwick 	if (total_found == total_alloc) {
273482a0a985SVictor Latushkin 		if (!dump_opt['L'])
2735fa9e4066Sahrens 			(void) printf("\n\tNo leaks (block sum matches space"
2736fa9e4066Sahrens 			    " maps exactly)\n");
2737fa9e4066Sahrens 	} else {
2738fa9e4066Sahrens 		(void) printf("block traversal size %llu != alloc %llu "
273982a0a985SVictor Latushkin 		    "(%s %lld)\n",
2740b24ab676SJeff Bonwick 		    (u_longlong_t)total_found,
2741b24ab676SJeff Bonwick 		    (u_longlong_t)total_alloc,
274282a0a985SVictor Latushkin 		    (dump_opt['L']) ? "unreachable" : "leaked",
2743b24ab676SJeff Bonwick 		    (longlong_t)(total_alloc - total_found));
27445d7b4d43SMatthew Ahrens 		leaks = B_TRUE;
2745fa9e4066Sahrens 	}
2746fa9e4066Sahrens 
2747fa9e4066Sahrens 	if (tzb->zb_count == 0)
2748fa9e4066Sahrens 		return (2);
2749fa9e4066Sahrens 
2750fa9e4066Sahrens 	(void) printf("\n");
2751fa9e4066Sahrens 	(void) printf("\tbp count:      %10llu\n",
2752fa9e4066Sahrens 	    (u_longlong_t)tzb->zb_count);
2753d5ee8a13SMatthew Ahrens 	(void) printf("\tganged count:  %10llu\n",
2754d5ee8a13SMatthew Ahrens 	    (longlong_t)tzb->zb_gangs);
2755b24ab676SJeff Bonwick 	(void) printf("\tbp logical:    %10llu      avg: %6llu\n",
2756fa9e4066Sahrens 	    (u_longlong_t)tzb->zb_lsize,
2757fa9e4066Sahrens 	    (u_longlong_t)(tzb->zb_lsize / tzb->zb_count));
2758b24ab676SJeff Bonwick 	(void) printf("\tbp physical:   %10llu      avg:"
2759b24ab676SJeff Bonwick 	    " %6llu     compression: %6.2f\n",
2760fa9e4066Sahrens 	    (u_longlong_t)tzb->zb_psize,
2761fa9e4066Sahrens 	    (u_longlong_t)(tzb->zb_psize / tzb->zb_count),
2762fa9e4066Sahrens 	    (double)tzb->zb_lsize / tzb->zb_psize);
2763b24ab676SJeff Bonwick 	(void) printf("\tbp allocated:  %10llu      avg:"
2764b24ab676SJeff Bonwick 	    " %6llu     compression: %6.2f\n",
2765fa9e4066Sahrens 	    (u_longlong_t)tzb->zb_asize,
2766fa9e4066Sahrens 	    (u_longlong_t)(tzb->zb_asize / tzb->zb_count),
2767fa9e4066Sahrens 	    (double)tzb->zb_lsize / tzb->zb_asize);
2768b24ab676SJeff Bonwick 	(void) printf("\tbp deduped:    %10llu    ref>1:"
2769b24ab676SJeff Bonwick 	    " %6llu   deduplication: %6.2f\n",
2770b24ab676SJeff Bonwick 	    (u_longlong_t)zcb.zcb_dedup_asize,
2771b24ab676SJeff Bonwick 	    (u_longlong_t)zcb.zcb_dedup_blocks,
2772b24ab676SJeff Bonwick 	    (double)zcb.zcb_dedup_asize / tzb->zb_asize + 1.0);
2773b24ab676SJeff Bonwick 	(void) printf("\tSPA allocated: %10llu     used: %5.2f%%\n",
2774b24ab676SJeff Bonwick 	    (u_longlong_t)norm_alloc, 100.0 * norm_alloc / norm_space);
2775fa9e4066Sahrens 
27765d7b4d43SMatthew Ahrens 	for (bp_embedded_type_t i = 0; i < NUM_BP_EMBEDDED_TYPES; i++) {
27775d7b4d43SMatthew Ahrens 		if (zcb.zcb_embedded_blocks[i] == 0)
27785d7b4d43SMatthew Ahrens 			continue;
27795d7b4d43SMatthew Ahrens 		(void) printf("\n");
27805d7b4d43SMatthew Ahrens 		(void) printf("\tadditional, non-pointer bps of type %u: "
27815d7b4d43SMatthew Ahrens 		    "%10llu\n",
27825d7b4d43SMatthew Ahrens 		    i, (u_longlong_t)zcb.zcb_embedded_blocks[i]);
27835d7b4d43SMatthew Ahrens 
27845d7b4d43SMatthew Ahrens 		if (dump_opt['b'] >= 3) {
27855d7b4d43SMatthew Ahrens 			(void) printf("\t number of (compressed) bytes:  "
27865d7b4d43SMatthew Ahrens 			    "number of bps\n");
27875d7b4d43SMatthew Ahrens 			dump_histogram(zcb.zcb_embedded_histogram[i],
27885d7b4d43SMatthew Ahrens 			    sizeof (zcb.zcb_embedded_histogram[i]) /
27895d7b4d43SMatthew Ahrens 			    sizeof (zcb.zcb_embedded_histogram[i][0]), 0);
27905d7b4d43SMatthew Ahrens 		}
27915d7b4d43SMatthew Ahrens 	}
27925d7b4d43SMatthew Ahrens 
2793d5ee8a13SMatthew Ahrens 	if (tzb->zb_ditto_samevdev != 0) {
2794d5ee8a13SMatthew Ahrens 		(void) printf("\tDittoed blocks on same vdev: %llu\n",
2795d5ee8a13SMatthew Ahrens 		    (longlong_t)tzb->zb_ditto_samevdev);
2796d5ee8a13SMatthew Ahrens 	}
2797d5ee8a13SMatthew Ahrens 
2798fa9e4066Sahrens 	if (dump_opt['b'] >= 2) {
2799fa9e4066Sahrens 		int l, t, level;
2800fa9e4066Sahrens 		(void) printf("\nBlocks\tLSIZE\tPSIZE\tASIZE"
2801fa9e4066Sahrens 		    "\t  avg\t comp\t%%Total\tType\n");
2802fa9e4066Sahrens 
2803b24ab676SJeff Bonwick 		for (t = 0; t <= ZDB_OT_TOTAL; t++) {
28043f9d6ad7SLin Ling 			char csize[32], lsize[32], psize[32], asize[32];
2805d5ee8a13SMatthew Ahrens 			char avg[32], gang[32];
2806fa9e4066Sahrens 			char *typename;
2807fa9e4066Sahrens 
2808b24ab676SJeff Bonwick 			if (t < DMU_OT_NUMTYPES)
2809b24ab676SJeff Bonwick 				typename = dmu_ot[t].ot_name;
2810b24ab676SJeff Bonwick 			else
2811b24ab676SJeff Bonwick 				typename = zdb_ot_extname[t - DMU_OT_NUMTYPES];
2812fa9e4066Sahrens 
2813fa9e4066Sahrens 			if (zcb.zcb_type[ZB_TOTAL][t].zb_asize == 0) {
2814fa9e4066Sahrens 				(void) printf("%6s\t%5s\t%5s\t%5s"
2815fa9e4066Sahrens 				    "\t%5s\t%5s\t%6s\t%s\n",
2816fa9e4066Sahrens 				    "-",
2817fa9e4066Sahrens 				    "-",
2818fa9e4066Sahrens 				    "-",
2819fa9e4066Sahrens 				    "-",
2820fa9e4066Sahrens 				    "-",
2821fa9e4066Sahrens 				    "-",
2822fa9e4066Sahrens 				    "-",
2823fa9e4066Sahrens 				    typename);
2824fa9e4066Sahrens 				continue;
2825fa9e4066Sahrens 			}
2826fa9e4066Sahrens 
2827fa9e4066Sahrens 			for (l = ZB_TOTAL - 1; l >= -1; l--) {
2828fa9e4066Sahrens 				level = (l == -1 ? ZB_TOTAL : l);
2829fa9e4066Sahrens 				zb = &zcb.zcb_type[level][t];
2830fa9e4066Sahrens 
2831fa9e4066Sahrens 				if (zb->zb_asize == 0)
2832fa9e4066Sahrens 					continue;
2833fa9e4066Sahrens 
2834fa9e4066Sahrens 				if (dump_opt['b'] < 3 && level != ZB_TOTAL)
2835fa9e4066Sahrens 					continue;
2836fa9e4066Sahrens 
2837fa9e4066Sahrens 				if (level == 0 && zb->zb_asize ==
2838fa9e4066Sahrens 				    zcb.zcb_type[ZB_TOTAL][t].zb_asize)
2839fa9e4066Sahrens 					continue;
2840fa9e4066Sahrens 
28413f9d6ad7SLin Ling 				zdb_nicenum(zb->zb_count, csize);
28423f9d6ad7SLin Ling 				zdb_nicenum(zb->zb_lsize, lsize);
28433f9d6ad7SLin Ling 				zdb_nicenum(zb->zb_psize, psize);
28443f9d6ad7SLin Ling 				zdb_nicenum(zb->zb_asize, asize);
28453f9d6ad7SLin Ling 				zdb_nicenum(zb->zb_asize / zb->zb_count, avg);
2846d5ee8a13SMatthew Ahrens 				zdb_nicenum(zb->zb_gangs, gang);
2847fa9e4066Sahrens 
2848fa9e4066Sahrens 				(void) printf("%6s\t%5s\t%5s\t%5s\t%5s"
2849fa9e4066Sahrens 				    "\t%5.2f\t%6.2f\t",
2850fa9e4066Sahrens 				    csize, lsize, psize, asize, avg,
2851fa9e4066Sahrens 				    (double)zb->zb_lsize / zb->zb_psize,
2852fa9e4066Sahrens 				    100.0 * zb->zb_asize / tzb->zb_asize);
2853fa9e4066Sahrens 
2854fa9e4066Sahrens 				if (level == ZB_TOTAL)
2855fa9e4066Sahrens 					(void) printf("%s\n", typename);
2856fa9e4066Sahrens 				else
2857fa9e4066Sahrens 					(void) printf("    L%d %s\n",
2858fa9e4066Sahrens 					    level, typename);
2859490d05b9SMatthew Ahrens 
2860d5ee8a13SMatthew Ahrens 				if (dump_opt['b'] >= 3 && zb->zb_gangs > 0) {
2861d5ee8a13SMatthew Ahrens 					(void) printf("\t number of ganged "
2862d5ee8a13SMatthew Ahrens 					    "blocks: %s\n", gang);
2863d5ee8a13SMatthew Ahrens 				}
2864d5ee8a13SMatthew Ahrens 
2865490d05b9SMatthew Ahrens 				if (dump_opt['b'] >= 4) {
2866490d05b9SMatthew Ahrens 					(void) printf("psize "
2867490d05b9SMatthew Ahrens 					    "(in 512-byte sectors): "
2868490d05b9SMatthew Ahrens 					    "number of blocks\n");
2869490d05b9SMatthew Ahrens 					dump_histogram(zb->zb_psize_histogram,
28700713e232SGeorge Wilson 					    PSIZE_HISTO_SIZE, 0);
2871490d05b9SMatthew Ahrens 				}
2872fa9e4066Sahrens 			}
2873fa9e4066Sahrens 		}
2874fa9e4066Sahrens 	}
2875fa9e4066Sahrens 
2876fa9e4066Sahrens 	(void) printf("\n");
2877fa9e4066Sahrens 
2878fa9e4066Sahrens 	if (leaks)
2879fa9e4066Sahrens 		return (2);
2880fa9e4066Sahrens 
2881fa9e4066Sahrens 	if (zcb.zcb_haderrors)
2882fa9e4066Sahrens 		return (3);
2883fa9e4066Sahrens 
2884fa9e4066Sahrens 	return (0);
2885fa9e4066Sahrens }
2886fa9e4066Sahrens 
2887b24ab676SJeff Bonwick typedef struct zdb_ddt_entry {
2888b24ab676SJeff Bonwick 	ddt_key_t	zdde_key;
2889b24ab676SJeff Bonwick 	uint64_t	zdde_ref_blocks;
2890b24ab676SJeff Bonwick 	uint64_t	zdde_ref_lsize;
2891b24ab676SJeff Bonwick 	uint64_t	zdde_ref_psize;
2892b24ab676SJeff Bonwick 	uint64_t	zdde_ref_dsize;
2893b24ab676SJeff Bonwick 	avl_node_t	zdde_node;
2894b24ab676SJeff Bonwick } zdb_ddt_entry_t;
2895b24ab676SJeff Bonwick 
2896b24ab676SJeff Bonwick /* ARGSUSED */
2897b24ab676SJeff Bonwick static int
2898b24ab676SJeff Bonwick zdb_ddt_add_cb(spa_t *spa, zilog_t *zilog, const blkptr_t *bp,
28997802d7bfSMatthew Ahrens     const zbookmark_phys_t *zb, const dnode_phys_t *dnp, void *arg)
2900b24ab676SJeff Bonwick {
2901b24ab676SJeff Bonwick 	avl_tree_t *t = arg;
2902b24ab676SJeff Bonwick 	avl_index_t where;
2903b24ab676SJeff Bonwick 	zdb_ddt_entry_t *zdde, zdde_search;
2904b24ab676SJeff Bonwick 
2905a2cdcdd2SPaul Dagnelie 	if (bp == NULL || BP_IS_HOLE(bp) || BP_IS_EMBEDDED(bp))
2906b24ab676SJeff Bonwick 		return (0);
2907b24ab676SJeff Bonwick 
2908b24ab676SJeff Bonwick 	if (dump_opt['S'] > 1 && zb->zb_level == ZB_ROOT_LEVEL) {
2909b24ab676SJeff Bonwick 		(void) printf("traversing objset %llu, %llu objects, "
2910b24ab676SJeff Bonwick 		    "%lu blocks so far\n",
2911b24ab676SJeff Bonwick 		    (u_longlong_t)zb->zb_objset,
29125d7b4d43SMatthew Ahrens 		    (u_longlong_t)BP_GET_FILL(bp),
2913b24ab676SJeff Bonwick 		    avl_numnodes(t));
2914b24ab676SJeff Bonwick 	}
2915b24ab676SJeff Bonwick 
2916bbfd46c4SJeff Bonwick 	if (BP_IS_HOLE(bp) || BP_GET_CHECKSUM(bp) == ZIO_CHECKSUM_OFF ||
2917ad135b5dSChristopher Siden 	    BP_GET_LEVEL(bp) > 0 || DMU_OT_IS_METADATA(BP_GET_TYPE(bp)))
2918b24ab676SJeff Bonwick 		return (0);
2919b24ab676SJeff Bonwick 
2920b24ab676SJeff Bonwick 	ddt_key_fill(&zdde_search.zdde_key, bp);
2921b24ab676SJeff Bonwick 
2922b24ab676SJeff Bonwick 	zdde = avl_find(t, &zdde_search, &where);
2923b24ab676SJeff Bonwick 
2924b24ab676SJeff Bonwick 	if (zdde == NULL) {
2925b24ab676SJeff Bonwick 		zdde = umem_zalloc(sizeof (*zdde), UMEM_NOFAIL);
2926b24ab676SJeff Bonwick 		zdde->zdde_key = zdde_search.zdde_key;
2927b24ab676SJeff Bonwick 		avl_insert(t, zdde, where);
2928b24ab676SJeff Bonwick 	}
2929b24ab676SJeff Bonwick 
2930b24ab676SJeff Bonwick 	zdde->zdde_ref_blocks += 1;
2931b24ab676SJeff Bonwick 	zdde->zdde_ref_lsize += BP_GET_LSIZE(bp);
2932b24ab676SJeff Bonwick 	zdde->zdde_ref_psize += BP_GET_PSIZE(bp);
2933b24ab676SJeff Bonwick 	zdde->zdde_ref_dsize += bp_get_dsize_sync(spa, bp);
2934b24ab676SJeff Bonwick 
2935b24ab676SJeff Bonwick 	return (0);
2936b24ab676SJeff Bonwick }
2937b24ab676SJeff Bonwick 
2938b24ab676SJeff Bonwick static void
2939b24ab676SJeff Bonwick dump_simulated_ddt(spa_t *spa)
2940b24ab676SJeff Bonwick {
2941b24ab676SJeff Bonwick 	avl_tree_t t;
2942b24ab676SJeff Bonwick 	void *cookie = NULL;
2943b24ab676SJeff Bonwick 	zdb_ddt_entry_t *zdde;
2944b24ab676SJeff Bonwick 	ddt_histogram_t ddh_total = { 0 };
2945b24ab676SJeff Bonwick 	ddt_stat_t dds_total = { 0 };
2946b24ab676SJeff Bonwick 
2947b24ab676SJeff Bonwick 	avl_create(&t, ddt_entry_compare,
2948b24ab676SJeff Bonwick 	    sizeof (zdb_ddt_entry_t), offsetof(zdb_ddt_entry_t, zdde_node));
2949b24ab676SJeff Bonwick 
2950b24ab676SJeff Bonwick 	spa_config_enter(spa, SCL_CONFIG, FTAG, RW_READER);
2951b24ab676SJeff Bonwick 
2952bbfd46c4SJeff Bonwick 	(void) traverse_pool(spa, 0, TRAVERSE_PRE | TRAVERSE_PREFETCH_METADATA,
2953bbfd46c4SJeff Bonwick 	    zdb_ddt_add_cb, &t);
2954b24ab676SJeff Bonwick 
2955b24ab676SJeff Bonwick 	spa_config_exit(spa, SCL_CONFIG, FTAG);
2956b24ab676SJeff Bonwick 
2957b24ab676SJeff Bonwick 	while ((zdde = avl_destroy_nodes(&t, &cookie)) != NULL) {
2958b24ab676SJeff Bonwick 		ddt_stat_t dds;
2959b24ab676SJeff Bonwick 		uint64_t refcnt = zdde->zdde_ref_blocks;
2960b24ab676SJeff Bonwick 		ASSERT(refcnt != 0);
2961b24ab676SJeff Bonwick 
2962b24ab676SJeff Bonwick 		dds.dds_blocks = zdde->zdde_ref_blocks / refcnt;
2963b24ab676SJeff Bonwick 		dds.dds_lsize = zdde->zdde_ref_lsize / refcnt;
2964b24ab676SJeff Bonwick 		dds.dds_psize = zdde->zdde_ref_psize / refcnt;
2965b24ab676SJeff Bonwick 		dds.dds_dsize = zdde->zdde_ref_dsize / refcnt;
2966b24ab676SJeff Bonwick 
2967b24ab676SJeff Bonwick 		dds.dds_ref_blocks = zdde->zdde_ref_blocks;
2968b24ab676SJeff Bonwick 		dds.dds_ref_lsize = zdde->zdde_ref_lsize;
2969b24ab676SJeff Bonwick 		dds.dds_ref_psize = zdde->zdde_ref_psize;
2970b24ab676SJeff Bonwick 		dds.dds_ref_dsize = zdde->zdde_ref_dsize;
2971b24ab676SJeff Bonwick 
2972bf16b11eSMatthew Ahrens 		ddt_stat_add(&ddh_total.ddh_stat[highbit64(refcnt) - 1],
2973bf16b11eSMatthew Ahrens 		    &dds, 0);
2974b24ab676SJeff Bonwick 
2975b24ab676SJeff Bonwick 		umem_free(zdde, sizeof (*zdde));
2976b24ab676SJeff Bonwick 	}
2977b24ab676SJeff Bonwick 
2978b24ab676SJeff Bonwick 	avl_destroy(&t);
2979b24ab676SJeff Bonwick 
2980b24ab676SJeff Bonwick 	ddt_histogram_stat(&dds_total, &ddh_total);
2981b24ab676SJeff Bonwick 
2982b24ab676SJeff Bonwick 	(void) printf("Simulated DDT histogram:\n");
2983b24ab676SJeff Bonwick 
29849eb19f4dSGeorge Wilson 	zpool_dump_ddt(&dds_total, &ddh_total);
2985b24ab676SJeff Bonwick 
2986b24ab676SJeff Bonwick 	dump_dedup_ratio(&dds_total);
2987b24ab676SJeff Bonwick }
2988b24ab676SJeff Bonwick 
2989fa9e4066Sahrens static void
2990fa9e4066Sahrens dump_zpool(spa_t *spa)
2991fa9e4066Sahrens {
2992fa9e4066Sahrens 	dsl_pool_t *dp = spa_get_dsl(spa);
2993fa9e4066Sahrens 	int rc = 0;
2994fa9e4066Sahrens 
2995b24ab676SJeff Bonwick 	if (dump_opt['S']) {
2996b24ab676SJeff Bonwick 		dump_simulated_ddt(spa);
2997b24ab676SJeff Bonwick 		return;
2998b24ab676SJeff Bonwick 	}
2999b24ab676SJeff Bonwick 
300007428bdfSVictor Latushkin 	if (!dump_opt['e'] && dump_opt['C'] > 1) {
300107428bdfSVictor Latushkin 		(void) printf("\nCached configuration:\n");
300207428bdfSVictor Latushkin 		dump_nvlist(spa->spa_config, 8);
300307428bdfSVictor Latushkin 	}
300407428bdfSVictor Latushkin 
300507428bdfSVictor Latushkin 	if (dump_opt['C'])
300607428bdfSVictor Latushkin 		dump_config(spa);
300707428bdfSVictor Latushkin 
3008fa9e4066Sahrens 	if (dump_opt['u'])
300953b9a4a9SVictor Latushkin 		dump_uberblock(&spa->spa_uberblock, "\nUberblock:\n", "\n");
3010fa9e4066Sahrens 
3011b24ab676SJeff Bonwick 	if (dump_opt['D'])
3012b24ab676SJeff Bonwick 		dump_all_ddts(spa);
3013b24ab676SJeff Bonwick 
301487219db7SVictor Latushkin 	if (dump_opt['d'] > 2 || dump_opt['m'])
301587219db7SVictor Latushkin 		dump_metaslabs(spa);
30162e4c9986SGeorge Wilson 	if (dump_opt['M'])
30172e4c9986SGeorge Wilson 		dump_metaslab_groups(spa);
301887219db7SVictor Latushkin 
301987219db7SVictor Latushkin 	if (dump_opt['d'] || dump_opt['i']) {
3020fa9e4066Sahrens 		dump_dir(dp->dp_meta_objset);
3021fa9e4066Sahrens 		if (dump_opt['d'] >= 3) {
3022732885fcSMatthew Ahrens 			dump_full_bpobj(&spa->spa_deferred_bpobj,
3023d0475637SMatthew Ahrens 			    "Deferred frees", 0);
3024cde58dbcSMatthew Ahrens 			if (spa_version(spa) >= SPA_VERSION_DEADLISTS) {
3025732885fcSMatthew Ahrens 				dump_full_bpobj(
3026732885fcSMatthew Ahrens 				    &spa->spa_dsl_pool->dp_free_bpobj,
3027d0475637SMatthew Ahrens 				    "Pool snapshot frees", 0);
3028ad135b5dSChristopher Siden 			}
3029ad135b5dSChristopher Siden 
3030ad135b5dSChristopher Siden 			if (spa_feature_is_active(spa,
30312acef22dSMatthew Ahrens 			    SPA_FEATURE_ASYNC_DESTROY)) {
3032ad135b5dSChristopher Siden 				dump_bptree(spa->spa_meta_objset,
3033ad135b5dSChristopher Siden 				    spa->spa_dsl_pool->dp_bptree_obj,
3034ad135b5dSChristopher Siden 				    "Pool dataset frees");
3035cde58dbcSMatthew Ahrens 			}
3036fa9e4066Sahrens 			dump_dtl(spa->spa_root_vdev, 0);
3037fa9e4066Sahrens 		}
303807428bdfSVictor Latushkin 		(void) dmu_objset_find(spa_name(spa), dump_one_dir,
303907428bdfSVictor Latushkin 		    NULL, DS_FIND_SNAPSHOTS | DS_FIND_CHILDREN);
3040b5152584SMatthew Ahrens 
3041*ca0cc391SMatthew Ahrens 		for (spa_feature_t f = 0; f < SPA_FEATURES; f++) {
3042*ca0cc391SMatthew Ahrens 			uint64_t refcount;
3043*ca0cc391SMatthew Ahrens 
3044*ca0cc391SMatthew Ahrens 			if (!(spa_feature_table[f].fi_flags &
3045*ca0cc391SMatthew Ahrens 			    ZFEATURE_FLAG_PER_DATASET)) {
3046*ca0cc391SMatthew Ahrens 				ASSERT0(dataset_feature_count[f]);
3047*ca0cc391SMatthew Ahrens 				continue;
3048*ca0cc391SMatthew Ahrens 			}
3049b5152584SMatthew Ahrens 			(void) feature_get_refcount(spa,
3050*ca0cc391SMatthew Ahrens 			    &spa_feature_table[f], &refcount);
3051*ca0cc391SMatthew Ahrens 			if (dataset_feature_count[f] != refcount) {
3052*ca0cc391SMatthew Ahrens 				(void) printf("%s feature refcount mismatch: "
3053*ca0cc391SMatthew Ahrens 				    "%lld datasets != %lld refcount\n",
3054*ca0cc391SMatthew Ahrens 				    spa_feature_table[f].fi_uname,
3055*ca0cc391SMatthew Ahrens 				    (longlong_t)dataset_feature_count[f],
3056b5152584SMatthew Ahrens 				    (longlong_t)refcount);
3057b5152584SMatthew Ahrens 				rc = 2;
3058b5152584SMatthew Ahrens 			} else {
3059*ca0cc391SMatthew Ahrens 				(void) printf("Verified %s feature refcount "
3060*ca0cc391SMatthew Ahrens 				    "of %llu is correct\n",
3061*ca0cc391SMatthew Ahrens 				    spa_feature_table[f].fi_uname,
3062*ca0cc391SMatthew Ahrens 				    (longlong_t)refcount);
3063*ca0cc391SMatthew Ahrens 			}
3064fa9e4066Sahrens 		}
3065b5152584SMatthew Ahrens 	}
3066b5152584SMatthew Ahrens 	if (rc == 0 && (dump_opt['b'] || dump_opt['c']))
3067fa9e4066Sahrens 		rc = dump_block_stats(spa);
3068fa9e4066Sahrens 
30690713e232SGeorge Wilson 	if (rc == 0)
30700713e232SGeorge Wilson 		rc = verify_spacemap_refcounts(spa);
30710713e232SGeorge Wilson 
3072fa9e4066Sahrens 	if (dump_opt['s'])
3073fa9e4066Sahrens 		show_pool_stats(spa);
3074fa9e4066Sahrens 
30758f18d1faSGeorge Wilson 	if (dump_opt['h'])
30768f18d1faSGeorge Wilson 		dump_history(spa);
30778f18d1faSGeorge Wilson 
3078fa9e4066Sahrens 	if (rc != 0)
3079fa9e4066Sahrens 		exit(rc);
3080fa9e4066Sahrens }
3081fa9e4066Sahrens 
308244cd46caSbillm #define	ZDB_FLAG_CHECKSUM	0x0001
308344cd46caSbillm #define	ZDB_FLAG_DECOMPRESS	0x0002
308444cd46caSbillm #define	ZDB_FLAG_BSWAP		0x0004
308544cd46caSbillm #define	ZDB_FLAG_GBH		0x0008
308644cd46caSbillm #define	ZDB_FLAG_INDIRECT	0x0010
308744cd46caSbillm #define	ZDB_FLAG_PHYS		0x0020
308844cd46caSbillm #define	ZDB_FLAG_RAW		0x0040
308944cd46caSbillm #define	ZDB_FLAG_PRINT_BLKPTR	0x0080
309044cd46caSbillm 
309144cd46caSbillm int flagbits[256];
309244cd46caSbillm 
309344cd46caSbillm static void
309444cd46caSbillm zdb_print_blkptr(blkptr_t *bp, int flags)
309544cd46caSbillm {
3096b24ab676SJeff Bonwick 	char blkbuf[BP_SPRINTF_LEN];
309744cd46caSbillm 
309844cd46caSbillm 	if (flags & ZDB_FLAG_BSWAP)
309944cd46caSbillm 		byteswap_uint64_array((void *)bp, sizeof (blkptr_t));
3100b24ab676SJeff Bonwick 
310143466aaeSMax Grossman 	snprintf_blkptr(blkbuf, sizeof (blkbuf), bp);
3102b24ab676SJeff Bonwick 	(void) printf("%s\n", blkbuf);
310344cd46caSbillm }
310444cd46caSbillm 
310544cd46caSbillm static void
310644cd46caSbillm zdb_dump_indirect(blkptr_t *bp, int nbps, int flags)
310744cd46caSbillm {
310844cd46caSbillm 	int i;
310944cd46caSbillm 
311044cd46caSbillm 	for (i = 0; i < nbps; i++)
311144cd46caSbillm 		zdb_print_blkptr(&bp[i], flags);
311244cd46caSbillm }
311344cd46caSbillm 
311444cd46caSbillm static void
311544cd46caSbillm zdb_dump_gbh(void *buf, int flags)
311644cd46caSbillm {
311744cd46caSbillm 	zdb_dump_indirect((blkptr_t *)buf, SPA_GBH_NBLKPTRS, flags);
311844cd46caSbillm }
311944cd46caSbillm 
312044cd46caSbillm static void
312144cd46caSbillm zdb_dump_block_raw(void *buf, uint64_t size, int flags)
312244cd46caSbillm {
312344cd46caSbillm 	if (flags & ZDB_FLAG_BSWAP)
312444cd46caSbillm 		byteswap_uint64_array(buf, size);
3125b24ab676SJeff Bonwick 	(void) write(1, buf, size);
312644cd46caSbillm }
312744cd46caSbillm 
312844cd46caSbillm static void
312944cd46caSbillm zdb_dump_block(char *label, void *buf, uint64_t size, int flags)
313044cd46caSbillm {
313144cd46caSbillm 	uint64_t *d = (uint64_t *)buf;
313244cd46caSbillm 	int nwords = size / sizeof (uint64_t);
313344cd46caSbillm 	int do_bswap = !!(flags & ZDB_FLAG_BSWAP);
313444cd46caSbillm 	int i, j;
313544cd46caSbillm 	char *hdr, *c;
313644cd46caSbillm 
313744cd46caSbillm 
313844cd46caSbillm 	if (do_bswap)
313944cd46caSbillm 		hdr = " 7 6 5 4 3 2 1 0   f e d c b a 9 8";
314044cd46caSbillm 	else
314144cd46caSbillm 		hdr = " 0 1 2 3 4 5 6 7   8 9 a b c d e f";
314244cd46caSbillm 
314344cd46caSbillm 	(void) printf("\n%s\n%6s   %s  0123456789abcdef\n", label, "", hdr);
314444cd46caSbillm 
314544cd46caSbillm 	for (i = 0; i < nwords; i += 2) {
314644cd46caSbillm 		(void) printf("%06llx:  %016llx  %016llx  ",
314744cd46caSbillm 		    (u_longlong_t)(i * sizeof (uint64_t)),
314844cd46caSbillm 		    (u_longlong_t)(do_bswap ? BSWAP_64(d[i]) : d[i]),
314944cd46caSbillm 		    (u_longlong_t)(do_bswap ? BSWAP_64(d[i + 1]) : d[i + 1]));
315044cd46caSbillm 
315144cd46caSbillm 		c = (char *)&d[i];
315244cd46caSbillm 		for (j = 0; j < 2 * sizeof (uint64_t); j++)
315344cd46caSbillm 			(void) printf("%c", isprint(c[j]) ? c[j] : '.');
315444cd46caSbillm 		(void) printf("\n");
315544cd46caSbillm 	}
315644cd46caSbillm }
315744cd46caSbillm 
315844cd46caSbillm /*
315944cd46caSbillm  * There are two acceptable formats:
316044cd46caSbillm  *	leaf_name	  - For example: c1t0d0 or /tmp/ztest.0a
316144cd46caSbillm  *	child[.child]*    - For example: 0.1.1
316244cd46caSbillm  *
316344cd46caSbillm  * The second form can be used to specify arbitrary vdevs anywhere
316444cd46caSbillm  * in the heirarchy.  For example, in a pool with a mirror of
316544cd46caSbillm  * RAID-Zs, you can specify either RAID-Z vdev with 0.0 or 0.1 .
316644cd46caSbillm  */
316744cd46caSbillm static vdev_t *
316844cd46caSbillm zdb_vdev_lookup(vdev_t *vdev, char *path)
316944cd46caSbillm {
317044cd46caSbillm 	char *s, *p, *q;
317144cd46caSbillm 	int i;
317244cd46caSbillm 
317344cd46caSbillm 	if (vdev == NULL)
317444cd46caSbillm 		return (NULL);
317544cd46caSbillm 
317644cd46caSbillm 	/* First, assume the x.x.x.x format */
317744cd46caSbillm 	i = (int)strtoul(path, &s, 10);
317844cd46caSbillm 	if (s == path || (s && *s != '.' && *s != '\0'))
317944cd46caSbillm 		goto name;
318044cd46caSbillm 	if (i < 0 || i >= vdev->vdev_children)
318144cd46caSbillm 		return (NULL);
318244cd46caSbillm 
318344cd46caSbillm 	vdev = vdev->vdev_child[i];
318444cd46caSbillm 	if (*s == '\0')
318544cd46caSbillm 		return (vdev);
318644cd46caSbillm 	return (zdb_vdev_lookup(vdev, s+1));
318744cd46caSbillm 
318844cd46caSbillm name:
318944cd46caSbillm 	for (i = 0; i < vdev->vdev_children; i++) {
319044cd46caSbillm 		vdev_t *vc = vdev->vdev_child[i];
319144cd46caSbillm 
319244cd46caSbillm 		if (vc->vdev_path == NULL) {
319344cd46caSbillm 			vc = zdb_vdev_lookup(vc, path);
319444cd46caSbillm 			if (vc == NULL)
319544cd46caSbillm 				continue;
319644cd46caSbillm 			else
319744cd46caSbillm 				return (vc);
319844cd46caSbillm 		}
319944cd46caSbillm 
320044cd46caSbillm 		p = strrchr(vc->vdev_path, '/');
320144cd46caSbillm 		p = p ? p + 1 : vc->vdev_path;
320244cd46caSbillm 		q = &vc->vdev_path[strlen(vc->vdev_path) - 2];
320344cd46caSbillm 
320444cd46caSbillm 		if (strcmp(vc->vdev_path, path) == 0)
320544cd46caSbillm 			return (vc);
320644cd46caSbillm 		if (strcmp(p, path) == 0)
320744cd46caSbillm 			return (vc);
320844cd46caSbillm 		if (strcmp(q, "s0") == 0 && strncmp(p, path, q - p) == 0)
320944cd46caSbillm 			return (vc);
321044cd46caSbillm 	}
321144cd46caSbillm 
321244cd46caSbillm 	return (NULL);
321344cd46caSbillm }
321444cd46caSbillm 
321544cd46caSbillm /*
321644cd46caSbillm  * Read a block from a pool and print it out.  The syntax of the
321744cd46caSbillm  * block descriptor is:
321844cd46caSbillm  *
321944cd46caSbillm  *	pool:vdev_specifier:offset:size[:flags]
322044cd46caSbillm  *
322144cd46caSbillm  *	pool           - The name of the pool you wish to read from
322244cd46caSbillm  *	vdev_specifier - Which vdev (see comment for zdb_vdev_lookup)
322344cd46caSbillm  *	offset         - offset, in hex, in bytes
322444cd46caSbillm  *	size           - Amount of data to read, in hex, in bytes
322544cd46caSbillm  *	flags          - A string of characters specifying options
322644cd46caSbillm  *		 b: Decode a blkptr at given offset within block
322744cd46caSbillm  *		*c: Calculate and display checksums
3228b24ab676SJeff Bonwick  *		 d: Decompress data before dumping
322944cd46caSbillm  *		 e: Byteswap data before dumping
3230b24ab676SJeff Bonwick  *		 g: Display data as a gang block header
3231b24ab676SJeff Bonwick  *		 i: Display as an indirect block
323244cd46caSbillm  *		 p: Do I/O to physical offset
323344cd46caSbillm  *		 r: Dump raw data to stdout
323444cd46caSbillm  *
323544cd46caSbillm  *              * = not yet implemented
323644cd46caSbillm  */
323744cd46caSbillm static void
323807428bdfSVictor Latushkin zdb_read_block(char *thing, spa_t *spa)
323944cd46caSbillm {
3240b24ab676SJeff Bonwick 	blkptr_t blk, *bp = &blk;
3241b24ab676SJeff Bonwick 	dva_t *dva = bp->blk_dva;
324244cd46caSbillm 	int flags = 0;
3243b24ab676SJeff Bonwick 	uint64_t offset = 0, size = 0, psize = 0, lsize = 0, blkptr_offset = 0;
324444cd46caSbillm 	zio_t *zio;
324544cd46caSbillm 	vdev_t *vd;
3246b24ab676SJeff Bonwick 	void *pbuf, *lbuf, *buf;
324707428bdfSVictor Latushkin 	char *s, *p, *dup, *vdev, *flagstr;
3248b24ab676SJeff Bonwick 	int i, error;
324944cd46caSbillm 
325044cd46caSbillm 	dup = strdup(thing);
325144cd46caSbillm 	s = strtok(dup, ":");
325244cd46caSbillm 	vdev = s ? s : "";
325344cd46caSbillm 	s = strtok(NULL, ":");
325444cd46caSbillm 	offset = strtoull(s ? s : "", NULL, 16);
325544cd46caSbillm 	s = strtok(NULL, ":");
325644cd46caSbillm 	size = strtoull(s ? s : "", NULL, 16);
325744cd46caSbillm 	s = strtok(NULL, ":");
325844cd46caSbillm 	flagstr = s ? s : "";
325944cd46caSbillm 
326044cd46caSbillm 	s = NULL;
326144cd46caSbillm 	if (size == 0)
326244cd46caSbillm 		s = "size must not be zero";
326344cd46caSbillm 	if (!IS_P2ALIGNED(size, DEV_BSIZE))
326444cd46caSbillm 		s = "size must be a multiple of sector size";
326544cd46caSbillm 	if (!IS_P2ALIGNED(offset, DEV_BSIZE))
326644cd46caSbillm 		s = "offset must be a multiple of sector size";
326744cd46caSbillm 	if (s) {
326844cd46caSbillm 		(void) printf("Invalid block specifier: %s  - %s\n", thing, s);
326944cd46caSbillm 		free(dup);
327044cd46caSbillm 		return;
327144cd46caSbillm 	}
327244cd46caSbillm 
327344cd46caSbillm 	for (s = strtok(flagstr, ":"); s; s = strtok(NULL, ":")) {
327444cd46caSbillm 		for (i = 0; flagstr[i]; i++) {
32755ad82045Snd150628 			int bit = flagbits[(uchar_t)flagstr[i]];
327644cd46caSbillm 
327744cd46caSbillm 			if (bit == 0) {
327844cd46caSbillm 				(void) printf("***Invalid flag: %c\n",
327944cd46caSbillm 				    flagstr[i]);
328044cd46caSbillm 				continue;
328144cd46caSbillm 			}
328244cd46caSbillm 			flags |= bit;
328344cd46caSbillm 
328444cd46caSbillm 			/* If it's not something with an argument, keep going */
3285b24ab676SJeff Bonwick 			if ((bit & (ZDB_FLAG_CHECKSUM |
328644cd46caSbillm 			    ZDB_FLAG_PRINT_BLKPTR)) == 0)
328744cd46caSbillm 				continue;
328844cd46caSbillm 
328944cd46caSbillm 			p = &flagstr[i + 1];
329044cd46caSbillm 			if (bit == ZDB_FLAG_PRINT_BLKPTR)
329144cd46caSbillm 				blkptr_offset = strtoull(p, &p, 16);
329244cd46caSbillm 			if (*p != ':' && *p != '\0') {
329344cd46caSbillm 				(void) printf("***Invalid flag arg: '%s'\n", s);
329444cd46caSbillm 				free(dup);
329544cd46caSbillm 				return;
329644cd46caSbillm 			}
329744cd46caSbillm 		}
329844cd46caSbillm 	}
329944cd46caSbillm 
330044cd46caSbillm 	vd = zdb_vdev_lookup(spa->spa_root_vdev, vdev);
330144cd46caSbillm 	if (vd == NULL) {
330244cd46caSbillm 		(void) printf("***Invalid vdev: %s\n", vdev);
330344cd46caSbillm 		free(dup);
330444cd46caSbillm 		return;
330544cd46caSbillm 	} else {
330644cd46caSbillm 		if (vd->vdev_path)
3307b24ab676SJeff Bonwick 			(void) fprintf(stderr, "Found vdev: %s\n",
3308b24ab676SJeff Bonwick 			    vd->vdev_path);
330944cd46caSbillm 		else
3310b24ab676SJeff Bonwick 			(void) fprintf(stderr, "Found vdev type: %s\n",
331144cd46caSbillm 			    vd->vdev_ops->vdev_op_type);
331244cd46caSbillm 	}
331344cd46caSbillm 
3314b24ab676SJeff Bonwick 	psize = size;
3315b24ab676SJeff Bonwick 	lsize = size;
331644cd46caSbillm 
3317b24ab676SJeff Bonwick 	pbuf = umem_alloc(SPA_MAXBLOCKSIZE, UMEM_NOFAIL);
3318b24ab676SJeff Bonwick 	lbuf = umem_alloc(SPA_MAXBLOCKSIZE, UMEM_NOFAIL);
3319b24ab676SJeff Bonwick 
3320b24ab676SJeff Bonwick 	BP_ZERO(bp);
3321b24ab676SJeff Bonwick 
3322b24ab676SJeff Bonwick 	DVA_SET_VDEV(&dva[0], vd->vdev_id);
3323b24ab676SJeff Bonwick 	DVA_SET_OFFSET(&dva[0], offset);
3324b24ab676SJeff Bonwick 	DVA_SET_GANG(&dva[0], !!(flags & ZDB_FLAG_GBH));
3325b24ab676SJeff Bonwick 	DVA_SET_ASIZE(&dva[0], vdev_psize_to_asize(vd, psize));
3326b24ab676SJeff Bonwick 
3327b24ab676SJeff Bonwick 	BP_SET_BIRTH(bp, TXG_INITIAL, TXG_INITIAL);
3328b24ab676SJeff Bonwick 
3329b24ab676SJeff Bonwick 	BP_SET_LSIZE(bp, lsize);
3330b24ab676SJeff Bonwick 	BP_SET_PSIZE(bp, psize);
3331b24ab676SJeff Bonwick 	BP_SET_COMPRESS(bp, ZIO_COMPRESS_OFF);
3332b24ab676SJeff Bonwick 	BP_SET_CHECKSUM(bp, ZIO_CHECKSUM_OFF);
3333b24ab676SJeff Bonwick 	BP_SET_TYPE(bp, DMU_OT_NONE);
3334b24ab676SJeff Bonwick 	BP_SET_LEVEL(bp, 0);
3335b24ab676SJeff Bonwick 	BP_SET_DEDUP(bp, 0);
3336b24ab676SJeff Bonwick 	BP_SET_BYTEORDER(bp, ZFS_HOST_BYTEORDER);
333744cd46caSbillm 
3338e14bb325SJeff Bonwick 	spa_config_enter(spa, SCL_STATE, FTAG, RW_READER);
333944cd46caSbillm 	zio = zio_root(spa, NULL, NULL, 0);
3340b24ab676SJeff Bonwick 
3341b24ab676SJeff Bonwick 	if (vd == vd->vdev_top) {
3342b24ab676SJeff Bonwick 		/*
3343b24ab676SJeff Bonwick 		 * Treat this as a normal block read.
3344b24ab676SJeff Bonwick 		 */
3345b24ab676SJeff Bonwick 		zio_nowait(zio_read(zio, spa, bp, pbuf, psize, NULL, NULL,
3346b24ab676SJeff Bonwick 		    ZIO_PRIORITY_SYNC_READ,
3347b24ab676SJeff Bonwick 		    ZIO_FLAG_CANFAIL | ZIO_FLAG_RAW, NULL));
3348b24ab676SJeff Bonwick 	} else {
3349b24ab676SJeff Bonwick 		/*
3350b24ab676SJeff Bonwick 		 * Treat this as a vdev child I/O.
3351b24ab676SJeff Bonwick 		 */
3352b24ab676SJeff Bonwick 		zio_nowait(zio_vdev_child_io(zio, bp, vd, offset, pbuf, psize,
3353b24ab676SJeff Bonwick 		    ZIO_TYPE_READ, ZIO_PRIORITY_SYNC_READ,
3354b24ab676SJeff Bonwick 		    ZIO_FLAG_DONT_CACHE | ZIO_FLAG_DONT_QUEUE |
3355b24ab676SJeff Bonwick 		    ZIO_FLAG_DONT_PROPAGATE | ZIO_FLAG_DONT_RETRY |
3356b24ab676SJeff Bonwick 		    ZIO_FLAG_CANFAIL | ZIO_FLAG_RAW, NULL, NULL));
3357b24ab676SJeff Bonwick 	}
3358b24ab676SJeff Bonwick 
335944cd46caSbillm 	error = zio_wait(zio);
3360e14bb325SJeff Bonwick 	spa_config_exit(spa, SCL_STATE, FTAG);
336144cd46caSbillm 
336244cd46caSbillm 	if (error) {
336344cd46caSbillm 		(void) printf("Read of %s failed, error: %d\n", thing, error);
336444cd46caSbillm 		goto out;
336544cd46caSbillm 	}
336644cd46caSbillm 
3367b24ab676SJeff Bonwick 	if (flags & ZDB_FLAG_DECOMPRESS) {
3368b24ab676SJeff Bonwick 		/*
3369b24ab676SJeff Bonwick 		 * We don't know how the data was compressed, so just try
3370b24ab676SJeff Bonwick 		 * every decompress function at every inflated blocksize.
3371b24ab676SJeff Bonwick 		 */
3372b24ab676SJeff Bonwick 		enum zio_compress c;
3373b24ab676SJeff Bonwick 		void *pbuf2 = umem_alloc(SPA_MAXBLOCKSIZE, UMEM_NOFAIL);
3374b24ab676SJeff Bonwick 		void *lbuf2 = umem_alloc(SPA_MAXBLOCKSIZE, UMEM_NOFAIL);
3375b24ab676SJeff Bonwick 
3376b24ab676SJeff Bonwick 		bcopy(pbuf, pbuf2, psize);
3377b24ab676SJeff Bonwick 
3378b24ab676SJeff Bonwick 		VERIFY(random_get_pseudo_bytes((uint8_t *)pbuf + psize,
3379b24ab676SJeff Bonwick 		    SPA_MAXBLOCKSIZE - psize) == 0);
3380b24ab676SJeff Bonwick 
3381b24ab676SJeff Bonwick 		VERIFY(random_get_pseudo_bytes((uint8_t *)pbuf2 + psize,
3382b24ab676SJeff Bonwick 		    SPA_MAXBLOCKSIZE - psize) == 0);
3383b24ab676SJeff Bonwick 
3384b24ab676SJeff Bonwick 		for (lsize = SPA_MAXBLOCKSIZE; lsize > psize;
3385b24ab676SJeff Bonwick 		    lsize -= SPA_MINBLOCKSIZE) {
3386b24ab676SJeff Bonwick 			for (c = 0; c < ZIO_COMPRESS_FUNCTIONS; c++) {
3387b24ab676SJeff Bonwick 				if (zio_decompress_data(c, pbuf, lbuf,
3388b24ab676SJeff Bonwick 				    psize, lsize) == 0 &&
3389b24ab676SJeff Bonwick 				    zio_decompress_data(c, pbuf2, lbuf2,
3390b24ab676SJeff Bonwick 				    psize, lsize) == 0 &&
3391b24ab676SJeff Bonwick 				    bcmp(lbuf, lbuf2, lsize) == 0)
3392b24ab676SJeff Bonwick 					break;
3393b24ab676SJeff Bonwick 			}
3394b24ab676SJeff Bonwick 			if (c != ZIO_COMPRESS_FUNCTIONS)
3395b24ab676SJeff Bonwick 				break;
3396b24ab676SJeff Bonwick 			lsize -= SPA_MINBLOCKSIZE;
3397b24ab676SJeff Bonwick 		}
3398b24ab676SJeff Bonwick 
3399b24ab676SJeff Bonwick 		umem_free(pbuf2, SPA_MAXBLOCKSIZE);
3400b24ab676SJeff Bonwick 		umem_free(lbuf2, SPA_MAXBLOCKSIZE);
3401b24ab676SJeff Bonwick 
3402b24ab676SJeff Bonwick 		if (lsize <= psize) {
3403b24ab676SJeff Bonwick 			(void) printf("Decompress of %s failed\n", thing);
3404b24ab676SJeff Bonwick 			goto out;
3405b24ab676SJeff Bonwick 		}
3406b24ab676SJeff Bonwick 		buf = lbuf;
3407b24ab676SJeff Bonwick 		size = lsize;
3408b24ab676SJeff Bonwick 	} else {
3409b24ab676SJeff Bonwick 		buf = pbuf;
3410b24ab676SJeff Bonwick 		size = psize;
3411b24ab676SJeff Bonwick 	}
3412b24ab676SJeff Bonwick 
341344cd46caSbillm 	if (flags & ZDB_FLAG_PRINT_BLKPTR)
341444cd46caSbillm 		zdb_print_blkptr((blkptr_t *)(void *)
341544cd46caSbillm 		    ((uintptr_t)buf + (uintptr_t)blkptr_offset), flags);
341644cd46caSbillm 	else if (flags & ZDB_FLAG_RAW)
341744cd46caSbillm 		zdb_dump_block_raw(buf, size, flags);
341844cd46caSbillm 	else if (flags & ZDB_FLAG_INDIRECT)
341944cd46caSbillm 		zdb_dump_indirect((blkptr_t *)buf, size / sizeof (blkptr_t),
342044cd46caSbillm 		    flags);
342144cd46caSbillm 	else if (flags & ZDB_FLAG_GBH)
342244cd46caSbillm 		zdb_dump_gbh(buf, flags);
342344cd46caSbillm 	else
342444cd46caSbillm 		zdb_dump_block(thing, buf, size, flags);
342544cd46caSbillm 
342644cd46caSbillm out:
3427b24ab676SJeff Bonwick 	umem_free(pbuf, SPA_MAXBLOCKSIZE);
3428b24ab676SJeff Bonwick 	umem_free(lbuf, SPA_MAXBLOCKSIZE);
342944cd46caSbillm 	free(dup);
343044cd46caSbillm }
343144cd46caSbillm 
3432de6628f0Sck153898 static boolean_t
34333ad6c7f9SVictor Latushkin pool_match(nvlist_t *cfg, char *tgt)
3434de6628f0Sck153898 {
34353ad6c7f9SVictor Latushkin 	uint64_t v, guid = strtoull(tgt, NULL, 0);
3436de6628f0Sck153898 	char *s;
3437de6628f0Sck153898 
34383ad6c7f9SVictor Latushkin 	if (guid != 0) {
34393ad6c7f9SVictor Latushkin 		if (nvlist_lookup_uint64(cfg, ZPOOL_CONFIG_POOL_GUID, &v) == 0)
34403ad6c7f9SVictor Latushkin 			return (v == guid);
34413ad6c7f9SVictor Latushkin 	} else {
34423ad6c7f9SVictor Latushkin 		if (nvlist_lookup_string(cfg, ZPOOL_CONFIG_POOL_NAME, &s) == 0)
3443de6628f0Sck153898 			return (strcmp(s, tgt) == 0);
3444de6628f0Sck153898 	}
3445de6628f0Sck153898 	return (B_FALSE);
3446de6628f0Sck153898 }
3447de6628f0Sck153898 
34483ad6c7f9SVictor Latushkin static char *
34493ad6c7f9SVictor Latushkin find_zpool(char **target, nvlist_t **configp, int dirc, char **dirv)
3450de6628f0Sck153898 {
3451de6628f0Sck153898 	nvlist_t *pools;
3452de6628f0Sck153898 	nvlist_t *match = NULL;
34533ad6c7f9SVictor Latushkin 	char *name = NULL;
34543ad6c7f9SVictor Latushkin 	char *sepp = NULL;
34553ad6c7f9SVictor Latushkin 	char sep;
34563ad6c7f9SVictor Latushkin 	int count = 0;
3457d41c4376SMark J Musante 	importargs_t args = { 0 };
3458d41c4376SMark J Musante 
3459d41c4376SMark J Musante 	args.paths = dirc;
3460d41c4376SMark J Musante 	args.path = dirv;
3461d41c4376SMark J Musante 	args.can_be_active = B_TRUE;
3462de6628f0Sck153898 
34633ad6c7f9SVictor Latushkin 	if ((sepp = strpbrk(*target, "/@")) != NULL) {
34643ad6c7f9SVictor Latushkin 		sep = *sepp;
34653ad6c7f9SVictor Latushkin 		*sepp = '\0';
34663ad6c7f9SVictor Latushkin 	}
34673ad6c7f9SVictor Latushkin 
3468d41c4376SMark J Musante 	pools = zpool_search_import(g_zfs, &args);
3469de6628f0Sck153898 
3470de6628f0Sck153898 	if (pools != NULL) {
3471de6628f0Sck153898 		nvpair_t *elem = NULL;
3472de6628f0Sck153898 		while ((elem = nvlist_next_nvpair(pools, elem)) != NULL) {
3473de6628f0Sck153898 			verify(nvpair_value_nvlist(elem, configp) == 0);
34743ad6c7f9SVictor Latushkin 			if (pool_match(*configp, *target)) {
34753ad6c7f9SVictor Latushkin 				count++;
3476de6628f0Sck153898 				if (match != NULL) {
34773ad6c7f9SVictor Latushkin 					/* print previously found config */
34783ad6c7f9SVictor Latushkin 					if (name != NULL) {
34793ad6c7f9SVictor Latushkin 						(void) printf("%s\n", name);
34803ad6c7f9SVictor Latushkin 						dump_nvlist(match, 8);
34813ad6c7f9SVictor Latushkin 						name = NULL;
34823ad6c7f9SVictor Latushkin 					}
34833ad6c7f9SVictor Latushkin 					(void) printf("%s\n",
34843ad6c7f9SVictor Latushkin 					    nvpair_name(elem));
34853ad6c7f9SVictor Latushkin 					dump_nvlist(*configp, 8);
3486de6628f0Sck153898 				} else {
3487de6628f0Sck153898 					match = *configp;
34883ad6c7f9SVictor Latushkin 					name = nvpair_name(elem);
3489de6628f0Sck153898 				}
3490de6628f0Sck153898 			}
3491de6628f0Sck153898 		}
3492de6628f0Sck153898 	}
34933ad6c7f9SVictor Latushkin 	if (count > 1)
34943ad6c7f9SVictor Latushkin 		(void) fatal("\tMatched %d pools - use pool GUID "
34953ad6c7f9SVictor Latushkin 		    "instead of pool name or \n"
34963ad6c7f9SVictor Latushkin 		    "\tpool name part of a dataset name to select pool", count);
34973ad6c7f9SVictor Latushkin 
34983ad6c7f9SVictor Latushkin 	if (sepp)
34993ad6c7f9SVictor Latushkin 		*sepp = sep;
35003ad6c7f9SVictor Latushkin 	/*
35013ad6c7f9SVictor Latushkin 	 * If pool GUID was specified for pool id, replace it with pool name
35023ad6c7f9SVictor Latushkin 	 */
35033ad6c7f9SVictor Latushkin 	if (name && (strstr(*target, name) != *target)) {
35043ad6c7f9SVictor Latushkin 		int sz = 1 + strlen(name) + ((sepp) ? strlen(sepp) : 0);
35053ad6c7f9SVictor Latushkin 
35063ad6c7f9SVictor Latushkin 		*target = umem_alloc(sz, UMEM_NOFAIL);
35073ad6c7f9SVictor Latushkin 		(void) snprintf(*target, sz, "%s%s", name, sepp ? sepp : "");
35083ad6c7f9SVictor Latushkin 	}
3509de6628f0Sck153898 
35103ad6c7f9SVictor Latushkin 	*configp = name ? match : NULL;
3511de6628f0Sck153898 
35123ad6c7f9SVictor Latushkin 	return (name);
3513de6628f0Sck153898 }
3514de6628f0Sck153898 
3515fa9e4066Sahrens int
3516fa9e4066Sahrens main(int argc, char **argv)
3517fa9e4066Sahrens {
3518fa9e4066Sahrens 	int i, c;
3519fa9e4066Sahrens 	struct rlimit rl = { 1024, 1024 };
35203ad6c7f9SVictor Latushkin 	spa_t *spa = NULL;
3521fa9e4066Sahrens 	objset_t *os = NULL;
3522fa9e4066Sahrens 	int dump_all = 1;
3523fa9e4066Sahrens 	int verbose = 0;
3524c8ee1847SVictor Latushkin 	int error = 0;
35253ad6c7f9SVictor Latushkin 	char **searchdirs = NULL;
35263ad6c7f9SVictor Latushkin 	int nsearch = 0;
35273ad6c7f9SVictor Latushkin 	char *target;
3528468c413aSTim Haley 	nvlist_t *policy = NULL;
3529468c413aSTim Haley 	uint64_t max_txg = UINT64_MAX;
3530c8ee1847SVictor Latushkin 	int rewind = ZPOOL_NEVER_REWIND;
3531fa9e4066Sahrens 
3532fa9e4066Sahrens 	(void) setrlimit(RLIMIT_NOFILE, &rl);
3533004388ebScasper 	(void) enable_extended_FILE_stdio(-1, -1);
3534fa9e4066Sahrens 
3535fa9e4066Sahrens 	dprintf_setup(&argc, argv);
3536fa9e4066Sahrens 
3537df15e419SMatthew Ahrens 	while ((c = getopt(argc, argv,
35382e4c9986SGeorge Wilson 	    "bcdhilmMI:suCDRSAFLXx:evp:t:U:P")) != -1) {
3539fa9e4066Sahrens 		switch (c) {
3540fa9e4066Sahrens 		case 'b':
3541fa9e4066Sahrens 		case 'c':
3542b24ab676SJeff Bonwick 		case 'd':
3543b24ab676SJeff Bonwick 		case 'h':
3544b24ab676SJeff Bonwick 		case 'i':
3545b24ab676SJeff Bonwick 		case 'l':
3546d6e555bdSGeorge Wilson 		case 'm':
3547fa9e4066Sahrens 		case 's':
3548b24ab676SJeff Bonwick 		case 'u':
3549fa9e4066Sahrens 		case 'C':
3550b24ab676SJeff Bonwick 		case 'D':
35512e4c9986SGeorge Wilson 		case 'M':
355244cd46caSbillm 		case 'R':
3553b24ab676SJeff Bonwick 		case 'S':
3554fa9e4066Sahrens 			dump_opt[c]++;
3555fa9e4066Sahrens 			dump_all = 0;
3556fa9e4066Sahrens 			break;
3557feef89cfSVictor Latushkin 		case 'A':
3558c8ee1847SVictor Latushkin 		case 'F':
355982a0a985SVictor Latushkin 		case 'L':
3560c8ee1847SVictor Latushkin 		case 'X':
35613ad6c7f9SVictor Latushkin 		case 'e':
35623f9d6ad7SLin Ling 		case 'P':
356382a0a985SVictor Latushkin 			dump_opt[c]++;
356482a0a985SVictor Latushkin 			break;
35652e4c9986SGeorge Wilson 		case 'I':
356631d7e8faSGeorge Wilson 			max_inflight = strtoull(optarg, NULL, 0);
356731d7e8faSGeorge Wilson 			if (max_inflight == 0) {
356831d7e8faSGeorge Wilson 				(void) fprintf(stderr, "maximum number "
356931d7e8faSGeorge Wilson 				    "of inflight I/Os must be greater "
357031d7e8faSGeorge Wilson 				    "than 0\n");
357131d7e8faSGeorge Wilson 				usage();
357231d7e8faSGeorge Wilson 			}
357331d7e8faSGeorge Wilson 			break;
3574de6628f0Sck153898 		case 'p':
35753ad6c7f9SVictor Latushkin 			if (searchdirs == NULL) {
35763ad6c7f9SVictor Latushkin 				searchdirs = umem_alloc(sizeof (char *),
35773ad6c7f9SVictor Latushkin 				    UMEM_NOFAIL);
35783ad6c7f9SVictor Latushkin 			} else {
35793ad6c7f9SVictor Latushkin 				char **tmp = umem_alloc((nsearch + 1) *
35803ad6c7f9SVictor Latushkin 				    sizeof (char *), UMEM_NOFAIL);
35813ad6c7f9SVictor Latushkin 				bcopy(searchdirs, tmp, nsearch *
35823ad6c7f9SVictor Latushkin 				    sizeof (char *));
35833ad6c7f9SVictor Latushkin 				umem_free(searchdirs,
35843ad6c7f9SVictor Latushkin 				    nsearch * sizeof (char *));
35853ad6c7f9SVictor Latushkin 				searchdirs = tmp;
35863ad6c7f9SVictor Latushkin 			}
35873ad6c7f9SVictor Latushkin 			searchdirs[nsearch++] = optarg;
3588de6628f0Sck153898 			break;
35892e551927SVictor Latushkin 		case 't':
3590468c413aSTim Haley 			max_txg = strtoull(optarg, NULL, 0);
3591468c413aSTim Haley 			if (max_txg < TXG_INITIAL) {
35922e551927SVictor Latushkin 				(void) fprintf(stderr, "incorrect txg "
35932e551927SVictor Latushkin 				    "specified: %s\n", optarg);
35942e551927SVictor Latushkin 				usage();
35952e551927SVictor Latushkin 			}
35962e551927SVictor Latushkin 			break;
3597b24ab676SJeff Bonwick 		case 'U':
3598b24ab676SJeff Bonwick 			spa_config_path = optarg;
3599b24ab676SJeff Bonwick 			break;
36002e4c9986SGeorge Wilson 		case 'v':
36012e4c9986SGeorge Wilson 			verbose++;
36022e4c9986SGeorge Wilson 			break;
36032e4c9986SGeorge Wilson 		case 'x':
36042e4c9986SGeorge Wilson 			vn_dumpdir = optarg;
36052e4c9986SGeorge Wilson 			break;
3606fa9e4066Sahrens 		default:
3607fa9e4066Sahrens 			usage();
3608fa9e4066Sahrens 			break;
3609fa9e4066Sahrens 		}
3610fa9e4066Sahrens 	}
3611fa9e4066Sahrens 
36123ad6c7f9SVictor Latushkin 	if (!dump_opt['e'] && searchdirs != NULL) {
361388b7b0f2SMatthew Ahrens 		(void) fprintf(stderr, "-p option requires use of -e\n");
361488b7b0f2SMatthew Ahrens 		usage();
361588b7b0f2SMatthew Ahrens 	}
3616de6628f0Sck153898 
361706be9802SMatthew Ahrens 	/*
361806be9802SMatthew Ahrens 	 * ZDB does not typically re-read blocks; therefore limit the ARC
361906be9802SMatthew Ahrens 	 * to 256 MB, which can be used entirely for metadata.
362006be9802SMatthew Ahrens 	 */
362106be9802SMatthew Ahrens 	zfs_arc_max = zfs_arc_meta_limit = 256 * 1024 * 1024;
362206be9802SMatthew Ahrens 
3623f7950bf1SMatthew Ahrens 	/*
3624f7950bf1SMatthew Ahrens 	 * "zdb -c" uses checksum-verifying scrub i/os which are async reads.
3625f7950bf1SMatthew Ahrens 	 * "zdb -b" uses traversal prefetch which uses async reads.
3626f7950bf1SMatthew Ahrens 	 * For good performance, let several of them be active at once.
3627f7950bf1SMatthew Ahrens 	 */
3628f7950bf1SMatthew Ahrens 	zfs_vdev_async_read_max_active = 10;
3629f7950bf1SMatthew Ahrens 
3630fa9e4066Sahrens 	kernel_init(FREAD);
3631de6628f0Sck153898 	g_zfs = libzfs_init();
363291ebeef5Sahrens 	ASSERT(g_zfs != NULL);
3633fa9e4066Sahrens 
3634b24ab676SJeff Bonwick 	if (dump_all)
3635b24ab676SJeff Bonwick 		verbose = MAX(verbose, 1);
3636b24ab676SJeff Bonwick 
3637fa9e4066Sahrens 	for (c = 0; c < 256; c++) {
36383f9d6ad7SLin Ling 		if (dump_all && !strchr("elAFLRSXP", c))
3639fa9e4066Sahrens 			dump_opt[c] = 1;
3640fa9e4066Sahrens 		if (dump_opt[c])
3641fa9e4066Sahrens 			dump_opt[c] += verbose;
3642fa9e4066Sahrens 	}
3643fa9e4066Sahrens 
3644feef89cfSVictor Latushkin 	aok = (dump_opt['A'] == 1) || (dump_opt['A'] > 2);
3645feef89cfSVictor Latushkin 	zfs_recover = (dump_opt['A'] > 1);
3646feef89cfSVictor Latushkin 
3647fa9e4066Sahrens 	argc -= optind;
3648fa9e4066Sahrens 	argv += optind;
3649fa9e4066Sahrens 
365007428bdfSVictor Latushkin 	if (argc < 2 && dump_opt['R'])
365107428bdfSVictor Latushkin 		usage();
3652fa9e4066Sahrens 	if (argc < 1) {
36533ad6c7f9SVictor Latushkin 		if (!dump_opt['e'] && dump_opt['C']) {
3654e829d913Sck153898 			dump_cachefile(spa_config_path);
3655fa9e4066Sahrens 			return (0);
3656fa9e4066Sahrens 		}
3657fa9e4066Sahrens 		usage();
3658fa9e4066Sahrens 	}
3659fa9e4066Sahrens 
3660fa9e4066Sahrens 	if (dump_opt['l']) {
3661fa9e4066Sahrens 		dump_label(argv[0]);
3662fa9e4066Sahrens 		return (0);
3663fa9e4066Sahrens 	}
3664fa9e4066Sahrens 
3665c8ee1847SVictor Latushkin 	if (dump_opt['X'] || dump_opt['F'])
3666c8ee1847SVictor Latushkin 		rewind = ZPOOL_DO_REWIND |
3667c8ee1847SVictor Latushkin 		    (dump_opt['X'] ? ZPOOL_EXTREME_REWIND : 0);
3668c8ee1847SVictor Latushkin 
3669c8ee1847SVictor Latushkin 	if (nvlist_alloc(&policy, NV_UNIQUE_NAME_TYPE, 0) != 0 ||
3670c8ee1847SVictor Latushkin 	    nvlist_add_uint64(policy, ZPOOL_REWIND_REQUEST_TXG, max_txg) != 0 ||
3671c8ee1847SVictor Latushkin 	    nvlist_add_uint32(policy, ZPOOL_REWIND_REQUEST, rewind) != 0)
3672c8ee1847SVictor Latushkin 		fatal("internal error: %s", strerror(ENOMEM));
3673c8ee1847SVictor Latushkin 
3674c5904d13Seschrock 	error = 0;
36753ad6c7f9SVictor Latushkin 	target = argv[0];
3676de6628f0Sck153898 
36773ad6c7f9SVictor Latushkin 	if (dump_opt['e']) {
36783ad6c7f9SVictor Latushkin 		nvlist_t *cfg = NULL;
36793ad6c7f9SVictor Latushkin 		char *name = find_zpool(&target, &cfg, nsearch, searchdirs);
3680c5904d13Seschrock 
36813ad6c7f9SVictor Latushkin 		error = ENOENT;
36823ad6c7f9SVictor Latushkin 		if (name) {
368307428bdfSVictor Latushkin 			if (dump_opt['C'] > 1) {
368407428bdfSVictor Latushkin 				(void) printf("\nConfiguration for import:\n");
368507428bdfSVictor Latushkin 				dump_nvlist(cfg, 8);
368607428bdfSVictor Latushkin 			}
3687c8ee1847SVictor Latushkin 			if (nvlist_add_nvlist(cfg,
3688468c413aSTim Haley 			    ZPOOL_REWIND_POLICY, policy) != 0) {
3689468c413aSTim Haley 				fatal("can't open '%s': %s",
3690468c413aSTim Haley 				    target, strerror(ENOMEM));
3691468c413aSTim Haley 			}
36924b964adaSGeorge Wilson 			if ((error = spa_import(name, cfg, NULL,
36934b964adaSGeorge Wilson 			    ZFS_IMPORT_MISSING_LOG)) != 0) {
36944b964adaSGeorge Wilson 				error = spa_import(name, cfg, NULL,
36954b964adaSGeorge Wilson 				    ZFS_IMPORT_VERBATIM);
36964b964adaSGeorge Wilson 			}
3697990b4856Slling 		}
3698c5904d13Seschrock 	}
3699c5904d13Seschrock 
3700c5904d13Seschrock 	if (error == 0) {
370107428bdfSVictor Latushkin 		if (strpbrk(target, "/@") == NULL || dump_opt['R']) {
370280eb36f2SGeorge Wilson 			error = spa_open_rewind(target, &spa, FTAG, policy,
370380eb36f2SGeorge Wilson 			    NULL);
37048f18d1faSGeorge Wilson 			if (error) {
37058f18d1faSGeorge Wilson 				/*
37068f18d1faSGeorge Wilson 				 * If we're missing the log device then
37078f18d1faSGeorge Wilson 				 * try opening the pool after clearing the
37088f18d1faSGeorge Wilson 				 * log state.
37098f18d1faSGeorge Wilson 				 */
37108f18d1faSGeorge Wilson 				mutex_enter(&spa_namespace_lock);
37113ad6c7f9SVictor Latushkin 				if ((spa = spa_lookup(target)) != NULL &&
37128f18d1faSGeorge Wilson 				    spa->spa_log_state == SPA_LOG_MISSING) {
37138f18d1faSGeorge Wilson 					spa->spa_log_state = SPA_LOG_CLEAR;
37148f18d1faSGeorge Wilson 					error = 0;
37158f18d1faSGeorge Wilson 				}
37168f18d1faSGeorge Wilson 				mutex_exit(&spa_namespace_lock);
37178f18d1faSGeorge Wilson 
371880eb36f2SGeorge Wilson 				if (!error) {
371980eb36f2SGeorge Wilson 					error = spa_open_rewind(target, &spa,
372080eb36f2SGeorge Wilson 					    FTAG, policy, NULL);
372180eb36f2SGeorge Wilson 				}
37228f18d1faSGeorge Wilson 			}
372307428bdfSVictor Latushkin 		} else {
372407428bdfSVictor Latushkin 			error = dmu_objset_own(target, DMU_OST_ANY,
372507428bdfSVictor Latushkin 			    B_TRUE, FTAG, &os);
3726c5904d13Seschrock 		}
3727de6628f0Sck153898 	}
372880eb36f2SGeorge Wilson 	nvlist_free(policy);
372980eb36f2SGeorge Wilson 
3730fa9e4066Sahrens 	if (error)
37313ad6c7f9SVictor Latushkin 		fatal("can't open '%s': %s", target, strerror(error));
3732fa9e4066Sahrens 
3733fa9e4066Sahrens 	argv++;
373407428bdfSVictor Latushkin 	argc--;
373507428bdfSVictor Latushkin 	if (!dump_opt['R']) {
373607428bdfSVictor Latushkin 		if (argc > 0) {
3737fa9e4066Sahrens 			zopt_objects = argc;
3738fa9e4066Sahrens 			zopt_object = calloc(zopt_objects, sizeof (uint64_t));
3739fa9e4066Sahrens 			for (i = 0; i < zopt_objects; i++) {
3740fa9e4066Sahrens 				errno = 0;
3741fa9e4066Sahrens 				zopt_object[i] = strtoull(argv[i], NULL, 0);
3742fa9e4066Sahrens 				if (zopt_object[i] == 0 && errno != 0)
374387219db7SVictor Latushkin 					fatal("bad number %s: %s",
3744fa9e4066Sahrens 					    argv[i], strerror(errno));
3745fa9e4066Sahrens 			}
3746fa9e4066Sahrens 		}
3747e690fb27SChristopher Siden 		if (os != NULL) {
3748e690fb27SChristopher Siden 			dump_dir(os);
3749e690fb27SChristopher Siden 		} else if (zopt_objects > 0 && !dump_opt['m']) {
3750e690fb27SChristopher Siden 			dump_dir(spa->spa_meta_objset);
3751e690fb27SChristopher Siden 		} else {
3752e690fb27SChristopher Siden 			dump_zpool(spa);
3753e690fb27SChristopher Siden 		}
3754fa9e4066Sahrens 	} else {
375507428bdfSVictor Latushkin 		flagbits['b'] = ZDB_FLAG_PRINT_BLKPTR;
375607428bdfSVictor Latushkin 		flagbits['c'] = ZDB_FLAG_CHECKSUM;
375707428bdfSVictor Latushkin 		flagbits['d'] = ZDB_FLAG_DECOMPRESS;
375807428bdfSVictor Latushkin 		flagbits['e'] = ZDB_FLAG_BSWAP;
375907428bdfSVictor Latushkin 		flagbits['g'] = ZDB_FLAG_GBH;
376007428bdfSVictor Latushkin 		flagbits['i'] = ZDB_FLAG_INDIRECT;
376107428bdfSVictor Latushkin 		flagbits['p'] = ZDB_FLAG_PHYS;
376207428bdfSVictor Latushkin 		flagbits['r'] = ZDB_FLAG_RAW;
376307428bdfSVictor Latushkin 
376407428bdfSVictor Latushkin 		for (i = 0; i < argc; i++)
376507428bdfSVictor Latushkin 			zdb_read_block(argv[i], spa);
3766fa9e4066Sahrens 	}
3767fa9e4066Sahrens 
376807428bdfSVictor Latushkin 	(os != NULL) ? dmu_objset_disown(os, FTAG) : spa_close(spa, FTAG);
376907428bdfSVictor Latushkin 
3770e0d35c44Smarks 	fuid_table_destroy();
37710a586ceaSMark Shellenbaum 	sa_loaded = B_FALSE;
3772e0d35c44Smarks 
3773de6628f0Sck153898 	libzfs_fini(g_zfs);
3774fa9e4066Sahrens 	kernel_fini();
3775fa9e4066Sahrens 
3776fa9e4066Sahrens 	return (0);
3777fa9e4066Sahrens }
3778