xref: /titanic_51/usr/src/cmd/zdb/zdb.c (revision fc30d4661fdf50c4c69dacbc572af35439c28cd8)
1fa9e4066Sahrens /*
2fa9e4066Sahrens  * CDDL HEADER START
3fa9e4066Sahrens  *
4fa9e4066Sahrens  * The contents of this file are subject to the terms of the
5ea8dc4b6Seschrock  * Common Development and Distribution License (the "License").
6ea8dc4b6Seschrock  * You may not use this file except in compliance with the License.
7fa9e4066Sahrens  *
8fa9e4066Sahrens  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9fa9e4066Sahrens  * or http://www.opensolaris.org/os/licensing.
10fa9e4066Sahrens  * See the License for the specific language governing permissions
11fa9e4066Sahrens  * and limitations under the License.
12fa9e4066Sahrens  *
13fa9e4066Sahrens  * When distributing Covered Code, include this CDDL HEADER in each
14fa9e4066Sahrens  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15fa9e4066Sahrens  * If applicable, add the following below this CDDL HEADER, with the
16fa9e4066Sahrens  * fields enclosed by brackets "[]" replaced with your own identifying
17fa9e4066Sahrens  * information: Portions Copyright [yyyy] [name of copyright owner]
18fa9e4066Sahrens  *
19fa9e4066Sahrens  * CDDL HEADER END
20fa9e4066Sahrens  */
21ad135b5dSChristopher Siden 
22fa9e4066Sahrens /*
238f2529deSMark Shellenbaum  * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
24ca0cc391SMatthew Ahrens  * Copyright (c) 2011, 2015 by Delphix. All rights reserved.
25c3d26abcSMatthew Ahrens  * Copyright (c) 2014 Integros [integros.com]
26fa9e4066Sahrens  */
27fa9e4066Sahrens 
28fa9e4066Sahrens #include <stdio.h>
29490d05b9SMatthew Ahrens #include <unistd.h>
30004388ebScasper #include <stdio_ext.h>
31fa9e4066Sahrens #include <stdlib.h>
3244cd46caSbillm #include <ctype.h>
33fa9e4066Sahrens #include <sys/zfs_context.h>
34fa9e4066Sahrens #include <sys/spa.h>
35fa9e4066Sahrens #include <sys/spa_impl.h>
36fa9e4066Sahrens #include <sys/dmu.h>
37fa9e4066Sahrens #include <sys/zap.h>
38fa9e4066Sahrens #include <sys/fs/zfs.h>
39fa9e4066Sahrens #include <sys/zfs_znode.h>
400a586ceaSMark Shellenbaum #include <sys/zfs_sa.h>
410a586ceaSMark Shellenbaum #include <sys/sa.h>
420a586ceaSMark Shellenbaum #include <sys/sa_impl.h>
43fa9e4066Sahrens #include <sys/vdev.h>
44fa9e4066Sahrens #include <sys/vdev_impl.h>
45fa9e4066Sahrens #include <sys/metaslab_impl.h>
46fa9e4066Sahrens #include <sys/dmu_objset.h>
47fa9e4066Sahrens #include <sys/dsl_dir.h>
48fa9e4066Sahrens #include <sys/dsl_dataset.h>
49fa9e4066Sahrens #include <sys/dsl_pool.h>
50fa9e4066Sahrens #include <sys/dbuf.h>
51fa9e4066Sahrens #include <sys/zil.h>
52fa9e4066Sahrens #include <sys/zil_impl.h>
53fa9e4066Sahrens #include <sys/stat.h>
54fa9e4066Sahrens #include <sys/resource.h>
55fa9e4066Sahrens #include <sys/dmu_traverse.h>
56fa9e4066Sahrens #include <sys/zio_checksum.h>
57fa9e4066Sahrens #include <sys/zio_compress.h>
58e0d35c44Smarks #include <sys/zfs_fuid.h>
5988b7b0f2SMatthew Ahrens #include <sys/arc.h>
60b24ab676SJeff Bonwick #include <sys/ddt.h>
61ad135b5dSChristopher Siden #include <sys/zfeature.h>
624445fffbSMatthew Ahrens #include <zfs_comutil.h>
63*fc30d466SJason King #include <libcmdutils.h>
64de6628f0Sck153898 #undef verify
65de6628f0Sck153898 #include <libzfs.h>
66fa9e4066Sahrens 
676de8f417SVictor Latushkin #define	ZDB_COMPRESS_NAME(idx) ((idx) < ZIO_COMPRESS_FUNCTIONS ?	\
686de8f417SVictor Latushkin 	zio_compress_table[(idx)].ci_name : "UNKNOWN")
696de8f417SVictor Latushkin #define	ZDB_CHECKSUM_NAME(idx) ((idx) < ZIO_CHECKSUM_FUNCTIONS ?	\
706de8f417SVictor Latushkin 	zio_checksum_table[(idx)].ci_name : "UNKNOWN")
716de8f417SVictor Latushkin #define	ZDB_OT_NAME(idx) ((idx) < DMU_OT_NUMTYPES ?	\
72ad135b5dSChristopher Siden 	dmu_ot[(idx)].ot_name : DMU_OT_IS_VALID(idx) ?	\
73ad135b5dSChristopher Siden 	dmu_ot_byteswap[DMU_OT_BYTESWAP(idx)].ob_name : "UNKNOWN")
74e690fb27SChristopher Siden #define	ZDB_OT_TYPE(idx) ((idx) < DMU_OT_NUMTYPES ? (idx) :		\
75e690fb27SChristopher Siden 	(((idx) == DMU_OTN_ZAP_DATA || (idx) == DMU_OTN_ZAP_METADATA) ?	\
76e690fb27SChristopher Siden 	DMU_OT_ZAP_OTHER : DMU_OT_NUMTYPES))
776de8f417SVictor Latushkin 
78feef89cfSVictor Latushkin #ifndef lint
797fd05ac4SMatthew Ahrens extern boolean_t zfs_recover;
8006be9802SMatthew Ahrens extern uint64_t zfs_arc_max, zfs_arc_meta_limit;
81f7950bf1SMatthew Ahrens extern int zfs_vdev_async_read_max_active;
82feef89cfSVictor Latushkin #else
837fd05ac4SMatthew Ahrens boolean_t zfs_recover;
8406be9802SMatthew Ahrens uint64_t zfs_arc_max, zfs_arc_meta_limit;
85f7950bf1SMatthew Ahrens int zfs_vdev_async_read_max_active;
86feef89cfSVictor Latushkin #endif
87feef89cfSVictor Latushkin 
88fa9e4066Sahrens const char cmdname[] = "zdb";
89fa9e4066Sahrens uint8_t dump_opt[256];
90fa9e4066Sahrens 
91fa9e4066Sahrens typedef void object_viewer_t(objset_t *, uint64_t, void *data, size_t size);
92fa9e4066Sahrens 
93fa9e4066Sahrens extern void dump_intent_log(zilog_t *);
94fa9e4066Sahrens uint64_t *zopt_object = NULL;
95fa9e4066Sahrens int zopt_objects = 0;
96de6628f0Sck153898 libzfs_handle_t *g_zfs;
9706be9802SMatthew Ahrens uint64_t max_inflight = 1000;
98fa9e4066Sahrens 
99732885fcSMatthew Ahrens static void snprintf_blkptr_compact(char *, size_t, const blkptr_t *);
100732885fcSMatthew Ahrens 
101fa9e4066Sahrens /*
102fa9e4066Sahrens  * These libumem hooks provide a reasonable set of defaults for the allocator's
103fa9e4066Sahrens  * debugging facilities.
104fa9e4066Sahrens  */
105fa9e4066Sahrens const char *
106fa9e4066Sahrens _umem_debug_init()
107fa9e4066Sahrens {
108fa9e4066Sahrens 	return ("default,verbose"); /* $UMEM_DEBUG setting */
109fa9e4066Sahrens }
110fa9e4066Sahrens 
111fa9e4066Sahrens const char *
112fa9e4066Sahrens _umem_logging_init(void)
113fa9e4066Sahrens {
114fa9e4066Sahrens 	return ("fail,contents"); /* $UMEM_LOGGING setting */
115fa9e4066Sahrens }
116fa9e4066Sahrens 
117fa9e4066Sahrens static void
118fa9e4066Sahrens usage(void)
119fa9e4066Sahrens {
120fa9e4066Sahrens 	(void) fprintf(stderr,
1212e4c9986SGeorge Wilson 	    "Usage: %s [-CumMdibcsDvhLXFPA] [-t txg] [-e [-p path...]] "
1222e4c9986SGeorge Wilson 	    "[-U config] [-I inflight I/Os] [-x dumpdir] poolname [object...]\n"
12331d7e8faSGeorge Wilson 	    "       %s [-divPA] [-e -p path...] [-U config] dataset "
12431d7e8faSGeorge Wilson 	    "[object...]\n"
1252e4c9986SGeorge Wilson 	    "       %s -mM [-LXFPA] [-t txg] [-e [-p path...]] [-U config] "
12690e894e2SRichard Lowe 	    "poolname [vdev [metaslab...]]\n"
12790e894e2SRichard Lowe 	    "       %s -R [-A] [-e [-p path...]] poolname "
12890e894e2SRichard Lowe 	    "vdev:offset:size[:flags]\n"
12931d7e8faSGeorge Wilson 	    "       %s -S [-PA] [-e [-p path...]] [-U config] poolname\n"
13090e894e2SRichard Lowe 	    "       %s -l [-uA] device\n"
13190e894e2SRichard Lowe 	    "       %s -C [-A] [-U config]\n\n",
132bbfd46c4SJeff Bonwick 	    cmdname, cmdname, cmdname, cmdname, cmdname, cmdname, cmdname);
133fa9e4066Sahrens 
1343ad6c7f9SVictor Latushkin 	(void) fprintf(stderr, "    Dataset name must include at least one "
1353ad6c7f9SVictor Latushkin 	    "separator character '/' or '@'\n");
1363ad6c7f9SVictor Latushkin 	(void) fprintf(stderr, "    If dataset name is specified, only that "
1373ad6c7f9SVictor Latushkin 	    "dataset is dumped\n");
1383ad6c7f9SVictor Latushkin 	(void) fprintf(stderr, "    If object numbers are specified, only "
1393ad6c7f9SVictor Latushkin 	    "those objects are dumped\n\n");
1403ad6c7f9SVictor Latushkin 	(void) fprintf(stderr, "    Options to control amount of output:\n");
141fa9e4066Sahrens 	(void) fprintf(stderr, "        -u uberblock\n");
1423ad6c7f9SVictor Latushkin 	(void) fprintf(stderr, "        -d dataset(s)\n");
1433ad6c7f9SVictor Latushkin 	(void) fprintf(stderr, "        -i intent logs\n");
14407428bdfSVictor Latushkin 	(void) fprintf(stderr, "        -C config (or cachefile if alone)\n");
1458f18d1faSGeorge Wilson 	(void) fprintf(stderr, "        -h pool history\n");
146fa9e4066Sahrens 	(void) fprintf(stderr, "        -b block statistics\n");
147d6e555bdSGeorge Wilson 	(void) fprintf(stderr, "        -m metaslabs\n");
1482e4c9986SGeorge Wilson 	(void) fprintf(stderr, "        -M metaslab groups\n");
1496365109dSVictor Latushkin 	(void) fprintf(stderr, "        -c checksum all metadata (twice for "
1506365109dSVictor Latushkin 	    "all data) blocks\n");
151fa9e4066Sahrens 	(void) fprintf(stderr, "        -s report stats on zdb's I/O\n");
152f0ba89beSJeff Bonwick 	(void) fprintf(stderr, "        -D dedup statistics\n");
153b24ab676SJeff Bonwick 	(void) fprintf(stderr, "        -S simulate dedup to measure effect\n");
154fa9e4066Sahrens 	(void) fprintf(stderr, "        -v verbose (applies to all others)\n");
155fa9e4066Sahrens 	(void) fprintf(stderr, "        -l dump label contents\n");
15682a0a985SVictor Latushkin 	(void) fprintf(stderr, "        -L disable leak tracking (do not "
15782a0a985SVictor Latushkin 	    "load spacemaps)\n");
15844cd46caSbillm 	(void) fprintf(stderr, "        -R read and display block from a "
1593ad6c7f9SVictor Latushkin 	    "device\n\n");
1603ad6c7f9SVictor Latushkin 	(void) fprintf(stderr, "    Below options are intended for use "
161df15e419SMatthew Ahrens 	    "with other options:\n");
162feef89cfSVictor Latushkin 	(void) fprintf(stderr, "        -A ignore assertions (-A), enable "
163feef89cfSVictor Latushkin 	    "panic recovery (-AA) or both (-AAA)\n");
164c8ee1847SVictor Latushkin 	(void) fprintf(stderr, "        -F attempt automatic rewind within "
165c8ee1847SVictor Latushkin 	    "safe range of transaction groups\n");
1663ad6c7f9SVictor Latushkin 	(void) fprintf(stderr, "        -U <cachefile_path> -- use alternate "
1673ad6c7f9SVictor Latushkin 	    "cachefile\n");
168c8ee1847SVictor Latushkin 	(void) fprintf(stderr, "        -X attempt extreme rewind (does not "
169c8ee1847SVictor Latushkin 	    "work with dataset)\n");
1703ad6c7f9SVictor Latushkin 	(void) fprintf(stderr, "        -e pool is exported/destroyed/"
1713ad6c7f9SVictor Latushkin 	    "has altroot/not in a cachefile\n");
1723ad6c7f9SVictor Latushkin 	(void) fprintf(stderr, "        -p <path> -- use one or more with "
1733ad6c7f9SVictor Latushkin 	    "-e to specify path to vdev dir\n");
174df15e419SMatthew Ahrens 	(void) fprintf(stderr, "        -x <dumpdir> -- "
175df15e419SMatthew Ahrens 	    "dump all read blocks into specified directory\n");
17690e894e2SRichard Lowe 	(void) fprintf(stderr, "        -P print numbers in parseable form\n");
1773ad6c7f9SVictor Latushkin 	(void) fprintf(stderr, "        -t <txg> -- highest txg to use when "
1782e551927SVictor Latushkin 	    "searching for uberblocks\n");
1792e4c9986SGeorge Wilson 	(void) fprintf(stderr, "        -I <number of inflight I/Os> -- "
180df15e419SMatthew Ahrens 	    "specify the maximum number of "
181df15e419SMatthew Ahrens 	    "checksumming I/Os [default is 200]\n");
182fa9e4066Sahrens 	(void) fprintf(stderr, "Specify an option more than once (e.g. -bb) "
183fa9e4066Sahrens 	    "to make only that option verbose\n");
184fa9e4066Sahrens 	(void) fprintf(stderr, "Default is to dump everything non-verbosely\n");
185fa9e4066Sahrens 	exit(1);
186fa9e4066Sahrens }
187fa9e4066Sahrens 
188ccba0801SRich Morris /*
189ccba0801SRich Morris  * Called for usage errors that are discovered after a call to spa_open(),
190ccba0801SRich Morris  * dmu_bonus_hold(), or pool_match().  abort() is called for other errors.
191ccba0801SRich Morris  */
192ccba0801SRich Morris 
193fa9e4066Sahrens static void
194fa9e4066Sahrens fatal(const char *fmt, ...)
195fa9e4066Sahrens {
196fa9e4066Sahrens 	va_list ap;
197fa9e4066Sahrens 
198fa9e4066Sahrens 	va_start(ap, fmt);
199fa9e4066Sahrens 	(void) fprintf(stderr, "%s: ", cmdname);
200fa9e4066Sahrens 	(void) vfprintf(stderr, fmt, ap);
201fa9e4066Sahrens 	va_end(ap);
202fa9e4066Sahrens 	(void) fprintf(stderr, "\n");
203fa9e4066Sahrens 
204ccba0801SRich Morris 	exit(1);
205fa9e4066Sahrens }
206fa9e4066Sahrens 
207fa9e4066Sahrens /* ARGSUSED */
208fa9e4066Sahrens static void
209fa9e4066Sahrens dump_packed_nvlist(objset_t *os, uint64_t object, void *data, size_t size)
210fa9e4066Sahrens {
211fa9e4066Sahrens 	nvlist_t *nv;
212fa9e4066Sahrens 	size_t nvsize = *(uint64_t *)data;
213fa9e4066Sahrens 	char *packed = umem_alloc(nvsize, UMEM_NOFAIL);
214fa9e4066Sahrens 
2157bfdf011SNeil Perrin 	VERIFY(0 == dmu_read(os, object, 0, nvsize, packed, DMU_READ_PREFETCH));
216fa9e4066Sahrens 
217fa9e4066Sahrens 	VERIFY(nvlist_unpack(packed, nvsize, &nv, 0) == 0);
218fa9e4066Sahrens 
219fa9e4066Sahrens 	umem_free(packed, nvsize);
220fa9e4066Sahrens 
221fa9e4066Sahrens 	dump_nvlist(nv, 8);
222fa9e4066Sahrens 
223fa9e4066Sahrens 	nvlist_free(nv);
224fa9e4066Sahrens }
225fa9e4066Sahrens 
2264445fffbSMatthew Ahrens /* ARGSUSED */
2274445fffbSMatthew Ahrens static void
2284445fffbSMatthew Ahrens dump_history_offsets(objset_t *os, uint64_t object, void *data, size_t size)
2294445fffbSMatthew Ahrens {
2304445fffbSMatthew Ahrens 	spa_history_phys_t *shp = data;
2314445fffbSMatthew Ahrens 
2324445fffbSMatthew Ahrens 	if (shp == NULL)
2334445fffbSMatthew Ahrens 		return;
2344445fffbSMatthew Ahrens 
2354445fffbSMatthew Ahrens 	(void) printf("\t\tpool_create_len = %llu\n",
2364445fffbSMatthew Ahrens 	    (u_longlong_t)shp->sh_pool_create_len);
2374445fffbSMatthew Ahrens 	(void) printf("\t\tphys_max_off = %llu\n",
2384445fffbSMatthew Ahrens 	    (u_longlong_t)shp->sh_phys_max_off);
2394445fffbSMatthew Ahrens 	(void) printf("\t\tbof = %llu\n",
2404445fffbSMatthew Ahrens 	    (u_longlong_t)shp->sh_bof);
2414445fffbSMatthew Ahrens 	(void) printf("\t\teof = %llu\n",
2424445fffbSMatthew Ahrens 	    (u_longlong_t)shp->sh_eof);
2434445fffbSMatthew Ahrens 	(void) printf("\t\trecords_lost = %llu\n",
2444445fffbSMatthew Ahrens 	    (u_longlong_t)shp->sh_records_lost);
2454445fffbSMatthew Ahrens }
2464445fffbSMatthew Ahrens 
2473f9d6ad7SLin Ling static void
248*fc30d466SJason King zdb_nicenum(uint64_t num, char *buf, size_t buflen)
2493f9d6ad7SLin Ling {
2503f9d6ad7SLin Ling 	if (dump_opt['P'])
251*fc30d466SJason King 		(void) snprintf(buf, buflen, "%llu", (longlong_t)num);
2523f9d6ad7SLin Ling 	else
253*fc30d466SJason King 		nicenum(num, buf, sizeof (buf));
2543f9d6ad7SLin Ling }
2553f9d6ad7SLin Ling 
256490d05b9SMatthew Ahrens const char histo_stars[] = "****************************************";
257490d05b9SMatthew Ahrens const int histo_width = sizeof (histo_stars) - 1;
258fa9e4066Sahrens 
259fa9e4066Sahrens static void
2600713e232SGeorge Wilson dump_histogram(const uint64_t *histo, int size, int offset)
261fa9e4066Sahrens {
262fa9e4066Sahrens 	int i;
263490d05b9SMatthew Ahrens 	int minidx = size - 1;
264fa9e4066Sahrens 	int maxidx = 0;
265fa9e4066Sahrens 	uint64_t max = 0;
266fa9e4066Sahrens 
267490d05b9SMatthew Ahrens 	for (i = 0; i < size; i++) {
268fa9e4066Sahrens 		if (histo[i] > max)
269fa9e4066Sahrens 			max = histo[i];
270fa9e4066Sahrens 		if (histo[i] > 0 && i > maxidx)
271fa9e4066Sahrens 			maxidx = i;
272fa9e4066Sahrens 		if (histo[i] > 0 && i < minidx)
273fa9e4066Sahrens 			minidx = i;
274fa9e4066Sahrens 	}
275fa9e4066Sahrens 
276490d05b9SMatthew Ahrens 	if (max < histo_width)
277490d05b9SMatthew Ahrens 		max = histo_width;
278fa9e4066Sahrens 
279490d05b9SMatthew Ahrens 	for (i = minidx; i <= maxidx; i++) {
280490d05b9SMatthew Ahrens 		(void) printf("\t\t\t%3u: %6llu %s\n",
2810713e232SGeorge Wilson 		    i + offset, (u_longlong_t)histo[i],
282490d05b9SMatthew Ahrens 		    &histo_stars[(max - histo[i]) * histo_width / max]);
283490d05b9SMatthew Ahrens 	}
284fa9e4066Sahrens }
285fa9e4066Sahrens 
286fa9e4066Sahrens static void
287fa9e4066Sahrens dump_zap_stats(objset_t *os, uint64_t object)
288fa9e4066Sahrens {
289fa9e4066Sahrens 	int error;
290fa9e4066Sahrens 	zap_stats_t zs;
291fa9e4066Sahrens 
292fa9e4066Sahrens 	error = zap_get_stats(os, object, &zs);
293fa9e4066Sahrens 	if (error)
294fa9e4066Sahrens 		return;
295fa9e4066Sahrens 
296fa9e4066Sahrens 	if (zs.zs_ptrtbl_len == 0) {
297fa9e4066Sahrens 		ASSERT(zs.zs_num_blocks == 1);
298fa9e4066Sahrens 		(void) printf("\tmicrozap: %llu bytes, %llu entries\n",
299fa9e4066Sahrens 		    (u_longlong_t)zs.zs_blocksize,
300fa9e4066Sahrens 		    (u_longlong_t)zs.zs_num_entries);
301fa9e4066Sahrens 		return;
302fa9e4066Sahrens 	}
303fa9e4066Sahrens 
304fa9e4066Sahrens 	(void) printf("\tFat ZAP stats:\n");
3058248818dSnd150628 
3068248818dSnd150628 	(void) printf("\t\tPointer table:\n");
3078248818dSnd150628 	(void) printf("\t\t\t%llu elements\n",
308fa9e4066Sahrens 	    (u_longlong_t)zs.zs_ptrtbl_len);
3098248818dSnd150628 	(void) printf("\t\t\tzt_blk: %llu\n",
3108248818dSnd150628 	    (u_longlong_t)zs.zs_ptrtbl_zt_blk);
3118248818dSnd150628 	(void) printf("\t\t\tzt_numblks: %llu\n",
3128248818dSnd150628 	    (u_longlong_t)zs.zs_ptrtbl_zt_numblks);
3138248818dSnd150628 	(void) printf("\t\t\tzt_shift: %llu\n",
3148248818dSnd150628 	    (u_longlong_t)zs.zs_ptrtbl_zt_shift);
3158248818dSnd150628 	(void) printf("\t\t\tzt_blks_copied: %llu\n",
3168248818dSnd150628 	    (u_longlong_t)zs.zs_ptrtbl_blks_copied);
3178248818dSnd150628 	(void) printf("\t\t\tzt_nextblk: %llu\n",
3188248818dSnd150628 	    (u_longlong_t)zs.zs_ptrtbl_nextblk);
3198248818dSnd150628 
320fa9e4066Sahrens 	(void) printf("\t\tZAP entries: %llu\n",
321fa9e4066Sahrens 	    (u_longlong_t)zs.zs_num_entries);
322fa9e4066Sahrens 	(void) printf("\t\tLeaf blocks: %llu\n",
323fa9e4066Sahrens 	    (u_longlong_t)zs.zs_num_leafs);
324fa9e4066Sahrens 	(void) printf("\t\tTotal blocks: %llu\n",
325fa9e4066Sahrens 	    (u_longlong_t)zs.zs_num_blocks);
3268248818dSnd150628 	(void) printf("\t\tzap_block_type: 0x%llx\n",
3278248818dSnd150628 	    (u_longlong_t)zs.zs_block_type);
3288248818dSnd150628 	(void) printf("\t\tzap_magic: 0x%llx\n",
3298248818dSnd150628 	    (u_longlong_t)zs.zs_magic);
3308248818dSnd150628 	(void) printf("\t\tzap_salt: 0x%llx\n",
3318248818dSnd150628 	    (u_longlong_t)zs.zs_salt);
332fa9e4066Sahrens 
333fa9e4066Sahrens 	(void) printf("\t\tLeafs with 2^n pointers:\n");
3340713e232SGeorge Wilson 	dump_histogram(zs.zs_leafs_with_2n_pointers, ZAP_HISTOGRAM_SIZE, 0);
335fa9e4066Sahrens 
336fa9e4066Sahrens 	(void) printf("\t\tBlocks with n*5 entries:\n");
3370713e232SGeorge Wilson 	dump_histogram(zs.zs_blocks_with_n5_entries, ZAP_HISTOGRAM_SIZE, 0);
338fa9e4066Sahrens 
339fa9e4066Sahrens 	(void) printf("\t\tBlocks n/10 full:\n");
3400713e232SGeorge Wilson 	dump_histogram(zs.zs_blocks_n_tenths_full, ZAP_HISTOGRAM_SIZE, 0);
341fa9e4066Sahrens 
342fa9e4066Sahrens 	(void) printf("\t\tEntries with n chunks:\n");
3430713e232SGeorge Wilson 	dump_histogram(zs.zs_entries_using_n_chunks, ZAP_HISTOGRAM_SIZE, 0);
344fa9e4066Sahrens 
345fa9e4066Sahrens 	(void) printf("\t\tBuckets with n entries:\n");
3460713e232SGeorge Wilson 	dump_histogram(zs.zs_buckets_with_n_entries, ZAP_HISTOGRAM_SIZE, 0);
347fa9e4066Sahrens }
348fa9e4066Sahrens 
349fa9e4066Sahrens /*ARGSUSED*/
350fa9e4066Sahrens static void
351fa9e4066Sahrens dump_none(objset_t *os, uint64_t object, void *data, size_t size)
352fa9e4066Sahrens {
353fa9e4066Sahrens }
354fa9e4066Sahrens 
355fa9e4066Sahrens /*ARGSUSED*/
3566de8f417SVictor Latushkin static void
3576de8f417SVictor Latushkin dump_unknown(objset_t *os, uint64_t object, void *data, size_t size)
3586de8f417SVictor Latushkin {
3596de8f417SVictor Latushkin 	(void) printf("\tUNKNOWN OBJECT TYPE\n");
3606de8f417SVictor Latushkin }
3616de8f417SVictor Latushkin 
3626de8f417SVictor Latushkin /*ARGSUSED*/
363fa9e4066Sahrens void
364fa9e4066Sahrens dump_uint8(objset_t *os, uint64_t object, void *data, size_t size)
365fa9e4066Sahrens {
366fa9e4066Sahrens }
367fa9e4066Sahrens 
368fa9e4066Sahrens /*ARGSUSED*/
369fa9e4066Sahrens static void
370fa9e4066Sahrens dump_uint64(objset_t *os, uint64_t object, void *data, size_t size)
371fa9e4066Sahrens {
372fa9e4066Sahrens }
373fa9e4066Sahrens 
374fa9e4066Sahrens /*ARGSUSED*/
375fa9e4066Sahrens static void
376fa9e4066Sahrens dump_zap(objset_t *os, uint64_t object, void *data, size_t size)
377fa9e4066Sahrens {
378fa9e4066Sahrens 	zap_cursor_t zc;
379fa9e4066Sahrens 	zap_attribute_t attr;
380fa9e4066Sahrens 	void *prop;
381fa9e4066Sahrens 	int i;
382fa9e4066Sahrens 
383fa9e4066Sahrens 	dump_zap_stats(os, object);
384fa9e4066Sahrens 	(void) printf("\n");
385fa9e4066Sahrens 
386fa9e4066Sahrens 	for (zap_cursor_init(&zc, os, object);
387fa9e4066Sahrens 	    zap_cursor_retrieve(&zc, &attr) == 0;
388fa9e4066Sahrens 	    zap_cursor_advance(&zc)) {
389fa9e4066Sahrens 		(void) printf("\t\t%s = ", attr.za_name);
390fa9e4066Sahrens 		if (attr.za_num_integers == 0) {
391fa9e4066Sahrens 			(void) printf("\n");
392fa9e4066Sahrens 			continue;
393fa9e4066Sahrens 		}
394fa9e4066Sahrens 		prop = umem_zalloc(attr.za_num_integers *
395fa9e4066Sahrens 		    attr.za_integer_length, UMEM_NOFAIL);
396fa9e4066Sahrens 		(void) zap_lookup(os, object, attr.za_name,
397fa9e4066Sahrens 		    attr.za_integer_length, attr.za_num_integers, prop);
398fa9e4066Sahrens 		if (attr.za_integer_length == 1) {
399fa9e4066Sahrens 			(void) printf("%s", (char *)prop);
400fa9e4066Sahrens 		} else {
401fa9e4066Sahrens 			for (i = 0; i < attr.za_num_integers; i++) {
402fa9e4066Sahrens 				switch (attr.za_integer_length) {
403fa9e4066Sahrens 				case 2:
404fa9e4066Sahrens 					(void) printf("%u ",
405fa9e4066Sahrens 					    ((uint16_t *)prop)[i]);
406fa9e4066Sahrens 					break;
407fa9e4066Sahrens 				case 4:
408fa9e4066Sahrens 					(void) printf("%u ",
409fa9e4066Sahrens 					    ((uint32_t *)prop)[i]);
410fa9e4066Sahrens 					break;
411fa9e4066Sahrens 				case 8:
412fa9e4066Sahrens 					(void) printf("%lld ",
413fa9e4066Sahrens 					    (u_longlong_t)((int64_t *)prop)[i]);
414fa9e4066Sahrens 					break;
415fa9e4066Sahrens 				}
416fa9e4066Sahrens 			}
417fa9e4066Sahrens 		}
418fa9e4066Sahrens 		(void) printf("\n");
419fa9e4066Sahrens 		umem_free(prop, attr.za_num_integers * attr.za_integer_length);
420fa9e4066Sahrens 	}
42187e5029aSahrens 	zap_cursor_fini(&zc);
422fa9e4066Sahrens }
423fa9e4066Sahrens 
424732885fcSMatthew Ahrens static void
425732885fcSMatthew Ahrens dump_bpobj(objset_t *os, uint64_t object, void *data, size_t size)
426732885fcSMatthew Ahrens {
427732885fcSMatthew Ahrens 	bpobj_phys_t *bpop = data;
428732885fcSMatthew Ahrens 	char bytes[32], comp[32], uncomp[32];
429732885fcSMatthew Ahrens 
430*fc30d466SJason King 	/* make sure the output won't get truncated */
431*fc30d466SJason King 	CTASSERT(sizeof (bytes) >= NN_NUMBUF_SZ);
432*fc30d466SJason King 	CTASSERT(sizeof (comp) >= NN_NUMBUF_SZ);
433*fc30d466SJason King 	CTASSERT(sizeof (uncomp) >= NN_NUMBUF_SZ);
434*fc30d466SJason King 
435732885fcSMatthew Ahrens 	if (bpop == NULL)
436732885fcSMatthew Ahrens 		return;
437732885fcSMatthew Ahrens 
438*fc30d466SJason King 	zdb_nicenum(bpop->bpo_bytes, bytes, sizeof (bytes));
439*fc30d466SJason King 	zdb_nicenum(bpop->bpo_comp, comp, sizeof (comp));
440*fc30d466SJason King 	zdb_nicenum(bpop->bpo_uncomp, uncomp, sizeof (uncomp));
441732885fcSMatthew Ahrens 
442732885fcSMatthew Ahrens 	(void) printf("\t\tnum_blkptrs = %llu\n",
443732885fcSMatthew Ahrens 	    (u_longlong_t)bpop->bpo_num_blkptrs);
444732885fcSMatthew Ahrens 	(void) printf("\t\tbytes = %s\n", bytes);
445732885fcSMatthew Ahrens 	if (size >= BPOBJ_SIZE_V1) {
446732885fcSMatthew Ahrens 		(void) printf("\t\tcomp = %s\n", comp);
447732885fcSMatthew Ahrens 		(void) printf("\t\tuncomp = %s\n", uncomp);
448732885fcSMatthew Ahrens 	}
449732885fcSMatthew Ahrens 	if (size >= sizeof (*bpop)) {
450732885fcSMatthew Ahrens 		(void) printf("\t\tsubobjs = %llu\n",
451732885fcSMatthew Ahrens 		    (u_longlong_t)bpop->bpo_subobjs);
452732885fcSMatthew Ahrens 		(void) printf("\t\tnum_subobjs = %llu\n",
453732885fcSMatthew Ahrens 		    (u_longlong_t)bpop->bpo_num_subobjs);
454732885fcSMatthew Ahrens 	}
455732885fcSMatthew Ahrens 
456732885fcSMatthew Ahrens 	if (dump_opt['d'] < 5)
457732885fcSMatthew Ahrens 		return;
458732885fcSMatthew Ahrens 
459732885fcSMatthew Ahrens 	for (uint64_t i = 0; i < bpop->bpo_num_blkptrs; i++) {
460732885fcSMatthew Ahrens 		char blkbuf[BP_SPRINTF_LEN];
461732885fcSMatthew Ahrens 		blkptr_t bp;
462732885fcSMatthew Ahrens 
463732885fcSMatthew Ahrens 		int err = dmu_read(os, object,
464732885fcSMatthew Ahrens 		    i * sizeof (bp), sizeof (bp), &bp, 0);
465732885fcSMatthew Ahrens 		if (err != 0) {
466732885fcSMatthew Ahrens 			(void) printf("got error %u from dmu_read\n", err);
467732885fcSMatthew Ahrens 			break;
468732885fcSMatthew Ahrens 		}
469732885fcSMatthew Ahrens 		snprintf_blkptr_compact(blkbuf, sizeof (blkbuf), &bp);
470732885fcSMatthew Ahrens 		(void) printf("\t%s\n", blkbuf);
471732885fcSMatthew Ahrens 	}
472732885fcSMatthew Ahrens }
473732885fcSMatthew Ahrens 
474732885fcSMatthew Ahrens /* ARGSUSED */
475732885fcSMatthew Ahrens static void
476732885fcSMatthew Ahrens dump_bpobj_subobjs(objset_t *os, uint64_t object, void *data, size_t size)
477732885fcSMatthew Ahrens {
478732885fcSMatthew Ahrens 	dmu_object_info_t doi;
479732885fcSMatthew Ahrens 
480732885fcSMatthew Ahrens 	VERIFY0(dmu_object_info(os, object, &doi));
481732885fcSMatthew Ahrens 	uint64_t *subobjs = kmem_alloc(doi.doi_max_offset, KM_SLEEP);
482732885fcSMatthew Ahrens 
483732885fcSMatthew Ahrens 	int err = dmu_read(os, object, 0, doi.doi_max_offset, subobjs, 0);
484732885fcSMatthew Ahrens 	if (err != 0) {
485732885fcSMatthew Ahrens 		(void) printf("got error %u from dmu_read\n", err);
486732885fcSMatthew Ahrens 		kmem_free(subobjs, doi.doi_max_offset);
487732885fcSMatthew Ahrens 		return;
488732885fcSMatthew Ahrens 	}
489732885fcSMatthew Ahrens 
490732885fcSMatthew Ahrens 	int64_t last_nonzero = -1;
491732885fcSMatthew Ahrens 	for (uint64_t i = 0; i < doi.doi_max_offset / 8; i++) {
492732885fcSMatthew Ahrens 		if (subobjs[i] != 0)
493732885fcSMatthew Ahrens 			last_nonzero = i;
494732885fcSMatthew Ahrens 	}
495732885fcSMatthew Ahrens 
496732885fcSMatthew Ahrens 	for (int64_t i = 0; i <= last_nonzero; i++) {
497732885fcSMatthew Ahrens 		(void) printf("\t%llu\n", (longlong_t)subobjs[i]);
498732885fcSMatthew Ahrens 	}
499732885fcSMatthew Ahrens 	kmem_free(subobjs, doi.doi_max_offset);
500732885fcSMatthew Ahrens }
501732885fcSMatthew Ahrens 
502e7437265Sahrens /*ARGSUSED*/
503e7437265Sahrens static void
504486ae710SMatthew Ahrens dump_ddt_zap(objset_t *os, uint64_t object, void *data, size_t size)
505486ae710SMatthew Ahrens {
506486ae710SMatthew Ahrens 	dump_zap_stats(os, object);
507486ae710SMatthew Ahrens 	/* contents are printed elsewhere, properly decoded */
508486ae710SMatthew Ahrens }
509486ae710SMatthew Ahrens 
510486ae710SMatthew Ahrens /*ARGSUSED*/
511486ae710SMatthew Ahrens static void
5120a586ceaSMark Shellenbaum dump_sa_attrs(objset_t *os, uint64_t object, void *data, size_t size)
5130a586ceaSMark Shellenbaum {
5140a586ceaSMark Shellenbaum 	zap_cursor_t zc;
5150a586ceaSMark Shellenbaum 	zap_attribute_t attr;
5160a586ceaSMark Shellenbaum 
5170a586ceaSMark Shellenbaum 	dump_zap_stats(os, object);
5180a586ceaSMark Shellenbaum 	(void) printf("\n");
5190a586ceaSMark Shellenbaum 
5200a586ceaSMark Shellenbaum 	for (zap_cursor_init(&zc, os, object);
5210a586ceaSMark Shellenbaum 	    zap_cursor_retrieve(&zc, &attr) == 0;
5220a586ceaSMark Shellenbaum 	    zap_cursor_advance(&zc)) {
5230a586ceaSMark Shellenbaum 		(void) printf("\t\t%s = ", attr.za_name);
5240a586ceaSMark Shellenbaum 		if (attr.za_num_integers == 0) {
5250a586ceaSMark Shellenbaum 			(void) printf("\n");
5260a586ceaSMark Shellenbaum 			continue;
5270a586ceaSMark Shellenbaum 		}
5280a586ceaSMark Shellenbaum 		(void) printf(" %llx : [%d:%d:%d]\n",
5290a586ceaSMark Shellenbaum 		    (u_longlong_t)attr.za_first_integer,
5300a586ceaSMark Shellenbaum 		    (int)ATTR_LENGTH(attr.za_first_integer),
5310a586ceaSMark Shellenbaum 		    (int)ATTR_BSWAP(attr.za_first_integer),
5320a586ceaSMark Shellenbaum 		    (int)ATTR_NUM(attr.za_first_integer));
5330a586ceaSMark Shellenbaum 	}
5340a586ceaSMark Shellenbaum 	zap_cursor_fini(&zc);
5350a586ceaSMark Shellenbaum }
5360a586ceaSMark Shellenbaum 
5370a586ceaSMark Shellenbaum /*ARGSUSED*/
5380a586ceaSMark Shellenbaum static void
5390a586ceaSMark Shellenbaum dump_sa_layouts(objset_t *os, uint64_t object, void *data, size_t size)
5400a586ceaSMark Shellenbaum {
5410a586ceaSMark Shellenbaum 	zap_cursor_t zc;
5420a586ceaSMark Shellenbaum 	zap_attribute_t attr;
5430a586ceaSMark Shellenbaum 	uint16_t *layout_attrs;
5440a586ceaSMark Shellenbaum 	int i;
5450a586ceaSMark Shellenbaum 
5460a586ceaSMark Shellenbaum 	dump_zap_stats(os, object);
5470a586ceaSMark Shellenbaum 	(void) printf("\n");
5480a586ceaSMark Shellenbaum 
5490a586ceaSMark Shellenbaum 	for (zap_cursor_init(&zc, os, object);
5500a586ceaSMark Shellenbaum 	    zap_cursor_retrieve(&zc, &attr) == 0;
5510a586ceaSMark Shellenbaum 	    zap_cursor_advance(&zc)) {
5520a586ceaSMark Shellenbaum 		(void) printf("\t\t%s = [", attr.za_name);
5530a586ceaSMark Shellenbaum 		if (attr.za_num_integers == 0) {
5540a586ceaSMark Shellenbaum 			(void) printf("\n");
5550a586ceaSMark Shellenbaum 			continue;
5560a586ceaSMark Shellenbaum 		}
5570a586ceaSMark Shellenbaum 
5580a586ceaSMark Shellenbaum 		VERIFY(attr.za_integer_length == 2);
5590a586ceaSMark Shellenbaum 		layout_attrs = umem_zalloc(attr.za_num_integers *
5600a586ceaSMark Shellenbaum 		    attr.za_integer_length, UMEM_NOFAIL);
5610a586ceaSMark Shellenbaum 
5620a586ceaSMark Shellenbaum 		VERIFY(zap_lookup(os, object, attr.za_name,
5630a586ceaSMark Shellenbaum 		    attr.za_integer_length,
5640a586ceaSMark Shellenbaum 		    attr.za_num_integers, layout_attrs) == 0);
5650a586ceaSMark Shellenbaum 
5660a586ceaSMark Shellenbaum 		for (i = 0; i != attr.za_num_integers; i++)
5670a586ceaSMark Shellenbaum 			(void) printf(" %d ", (int)layout_attrs[i]);
5680a586ceaSMark Shellenbaum 		(void) printf("]\n");
5690a586ceaSMark Shellenbaum 		umem_free(layout_attrs,
5700a586ceaSMark Shellenbaum 		    attr.za_num_integers * attr.za_integer_length);
5710a586ceaSMark Shellenbaum 	}
5720a586ceaSMark Shellenbaum 	zap_cursor_fini(&zc);
5730a586ceaSMark Shellenbaum }
5740a586ceaSMark Shellenbaum 
5750a586ceaSMark Shellenbaum /*ARGSUSED*/
5760a586ceaSMark Shellenbaum static void
577e7437265Sahrens dump_zpldir(objset_t *os, uint64_t object, void *data, size_t size)
578e7437265Sahrens {
579e7437265Sahrens 	zap_cursor_t zc;
580e7437265Sahrens 	zap_attribute_t attr;
581e7437265Sahrens 	const char *typenames[] = {
582e7437265Sahrens 		/* 0 */ "not specified",
583e7437265Sahrens 		/* 1 */ "FIFO",
584e7437265Sahrens 		/* 2 */ "Character Device",
585e7437265Sahrens 		/* 3 */ "3 (invalid)",
586e7437265Sahrens 		/* 4 */ "Directory",
587e7437265Sahrens 		/* 5 */ "5 (invalid)",
588e7437265Sahrens 		/* 6 */ "Block Device",
589e7437265Sahrens 		/* 7 */ "7 (invalid)",
590e7437265Sahrens 		/* 8 */ "Regular File",
591e7437265Sahrens 		/* 9 */ "9 (invalid)",
592e7437265Sahrens 		/* 10 */ "Symbolic Link",
593e7437265Sahrens 		/* 11 */ "11 (invalid)",
594e7437265Sahrens 		/* 12 */ "Socket",
595e7437265Sahrens 		/* 13 */ "Door",
596e7437265Sahrens 		/* 14 */ "Event Port",
597e7437265Sahrens 		/* 15 */ "15 (invalid)",
598e7437265Sahrens 	};
599e7437265Sahrens 
600e7437265Sahrens 	dump_zap_stats(os, object);
601e7437265Sahrens 	(void) printf("\n");
602e7437265Sahrens 
603e7437265Sahrens 	for (zap_cursor_init(&zc, os, object);
604e7437265Sahrens 	    zap_cursor_retrieve(&zc, &attr) == 0;
605e7437265Sahrens 	    zap_cursor_advance(&zc)) {
606e7437265Sahrens 		(void) printf("\t\t%s = %lld (type: %s)\n",
607e7437265Sahrens 		    attr.za_name, ZFS_DIRENT_OBJ(attr.za_first_integer),
608e7437265Sahrens 		    typenames[ZFS_DIRENT_TYPE(attr.za_first_integer)]);
609e7437265Sahrens 	}
610e7437265Sahrens 	zap_cursor_fini(&zc);
611e7437265Sahrens }
612e7437265Sahrens 
6130713e232SGeorge Wilson int
6140713e232SGeorge Wilson get_dtl_refcount(vdev_t *vd)
6150713e232SGeorge Wilson {
6160713e232SGeorge Wilson 	int refcount = 0;
6170713e232SGeorge Wilson 
6180713e232SGeorge Wilson 	if (vd->vdev_ops->vdev_op_leaf) {
6190713e232SGeorge Wilson 		space_map_t *sm = vd->vdev_dtl_sm;
6200713e232SGeorge Wilson 
6210713e232SGeorge Wilson 		if (sm != NULL &&
6220713e232SGeorge Wilson 		    sm->sm_dbuf->db_size == sizeof (space_map_phys_t))
6230713e232SGeorge Wilson 			return (1);
6240713e232SGeorge Wilson 		return (0);
6250713e232SGeorge Wilson 	}
6260713e232SGeorge Wilson 
6270713e232SGeorge Wilson 	for (int c = 0; c < vd->vdev_children; c++)
6280713e232SGeorge Wilson 		refcount += get_dtl_refcount(vd->vdev_child[c]);
6290713e232SGeorge Wilson 	return (refcount);
6300713e232SGeorge Wilson }
6310713e232SGeorge Wilson 
6320713e232SGeorge Wilson int
6330713e232SGeorge Wilson get_metaslab_refcount(vdev_t *vd)
6340713e232SGeorge Wilson {
6350713e232SGeorge Wilson 	int refcount = 0;
6360713e232SGeorge Wilson 
6372e4c9986SGeorge Wilson 	if (vd->vdev_top == vd && !vd->vdev_removing) {
6380713e232SGeorge Wilson 		for (int m = 0; m < vd->vdev_ms_count; m++) {
6390713e232SGeorge Wilson 			space_map_t *sm = vd->vdev_ms[m]->ms_sm;
6400713e232SGeorge Wilson 
6410713e232SGeorge Wilson 			if (sm != NULL &&
6420713e232SGeorge Wilson 			    sm->sm_dbuf->db_size == sizeof (space_map_phys_t))
6430713e232SGeorge Wilson 				refcount++;
6440713e232SGeorge Wilson 		}
6450713e232SGeorge Wilson 	}
6460713e232SGeorge Wilson 	for (int c = 0; c < vd->vdev_children; c++)
6470713e232SGeorge Wilson 		refcount += get_metaslab_refcount(vd->vdev_child[c]);
6480713e232SGeorge Wilson 
6490713e232SGeorge Wilson 	return (refcount);
6500713e232SGeorge Wilson }
6510713e232SGeorge Wilson 
6520713e232SGeorge Wilson static int
6530713e232SGeorge Wilson verify_spacemap_refcounts(spa_t *spa)
6540713e232SGeorge Wilson {
6552acef22dSMatthew Ahrens 	uint64_t expected_refcount = 0;
6562acef22dSMatthew Ahrens 	uint64_t actual_refcount;
6570713e232SGeorge Wilson 
6582acef22dSMatthew Ahrens 	(void) feature_get_refcount(spa,
6592acef22dSMatthew Ahrens 	    &spa_feature_table[SPA_FEATURE_SPACEMAP_HISTOGRAM],
6602acef22dSMatthew Ahrens 	    &expected_refcount);
6610713e232SGeorge Wilson 	actual_refcount = get_dtl_refcount(spa->spa_root_vdev);
6620713e232SGeorge Wilson 	actual_refcount += get_metaslab_refcount(spa->spa_root_vdev);
6630713e232SGeorge Wilson 
6640713e232SGeorge Wilson 	if (expected_refcount != actual_refcount) {
6652acef22dSMatthew Ahrens 		(void) printf("space map refcount mismatch: expected %lld != "
6662acef22dSMatthew Ahrens 		    "actual %lld\n",
6672acef22dSMatthew Ahrens 		    (longlong_t)expected_refcount,
6682acef22dSMatthew Ahrens 		    (longlong_t)actual_refcount);
6690713e232SGeorge Wilson 		return (2);
6700713e232SGeorge Wilson 	}
6710713e232SGeorge Wilson 	return (0);
6720713e232SGeorge Wilson }
6730713e232SGeorge Wilson 
674fa9e4066Sahrens static void
6750713e232SGeorge Wilson dump_spacemap(objset_t *os, space_map_t *sm)
676fa9e4066Sahrens {
677fa9e4066Sahrens 	uint64_t alloc, offset, entry;
6788053a263Sck153898 	char *ddata[] = { "ALLOC", "FREE", "CONDENSE", "INVALID",
6798053a263Sck153898 			    "INVALID", "INVALID", "INVALID", "INVALID" };
680fa9e4066Sahrens 
6810713e232SGeorge Wilson 	if (sm == NULL)
682fa9e4066Sahrens 		return;
683fa9e4066Sahrens 
684fa9e4066Sahrens 	/*
685fa9e4066Sahrens 	 * Print out the freelist entries in both encoded and decoded form.
686fa9e4066Sahrens 	 */
687fa9e4066Sahrens 	alloc = 0;
6880713e232SGeorge Wilson 	for (offset = 0; offset < space_map_length(sm);
6890713e232SGeorge Wilson 	    offset += sizeof (entry)) {
6900713e232SGeorge Wilson 		uint8_t mapshift = sm->sm_shift;
6910713e232SGeorge Wilson 
6920713e232SGeorge Wilson 		VERIFY0(dmu_read(os, space_map_object(sm), offset,
6937bfdf011SNeil Perrin 		    sizeof (entry), &entry, DMU_READ_PREFETCH));
694fa9e4066Sahrens 		if (SM_DEBUG_DECODE(entry)) {
6950713e232SGeorge Wilson 
69687219db7SVictor Latushkin 			(void) printf("\t    [%6llu] %s: txg %llu, pass %llu\n",
697fa9e4066Sahrens 			    (u_longlong_t)(offset / sizeof (entry)),
698fa9e4066Sahrens 			    ddata[SM_DEBUG_ACTION_DECODE(entry)],
6995ad82045Snd150628 			    (u_longlong_t)SM_DEBUG_TXG_DECODE(entry),
7005ad82045Snd150628 			    (u_longlong_t)SM_DEBUG_SYNCPASS_DECODE(entry));
701fa9e4066Sahrens 		} else {
70287219db7SVictor Latushkin 			(void) printf("\t    [%6llu]    %c  range:"
70387219db7SVictor Latushkin 			    " %010llx-%010llx  size: %06llx\n",
704fa9e4066Sahrens 			    (u_longlong_t)(offset / sizeof (entry)),
705fa9e4066Sahrens 			    SM_TYPE_DECODE(entry) == SM_ALLOC ? 'A' : 'F',
7065ad82045Snd150628 			    (u_longlong_t)((SM_OFFSET_DECODE(entry) <<
7070713e232SGeorge Wilson 			    mapshift) + sm->sm_start),
7085ad82045Snd150628 			    (u_longlong_t)((SM_OFFSET_DECODE(entry) <<
7090713e232SGeorge Wilson 			    mapshift) + sm->sm_start +
7100713e232SGeorge Wilson 			    (SM_RUN_DECODE(entry) << mapshift)),
7115ad82045Snd150628 			    (u_longlong_t)(SM_RUN_DECODE(entry) << mapshift));
712fa9e4066Sahrens 			if (SM_TYPE_DECODE(entry) == SM_ALLOC)
713fa9e4066Sahrens 				alloc += SM_RUN_DECODE(entry) << mapshift;
714fa9e4066Sahrens 			else
715fa9e4066Sahrens 				alloc -= SM_RUN_DECODE(entry) << mapshift;
716fa9e4066Sahrens 		}
717fa9e4066Sahrens 	}
7180713e232SGeorge Wilson 	if (alloc != space_map_allocated(sm)) {
719fa9e4066Sahrens 		(void) printf("space_map_object alloc (%llu) INCONSISTENT "
720fa9e4066Sahrens 		    "with space map summary (%llu)\n",
7210713e232SGeorge Wilson 		    (u_longlong_t)space_map_allocated(sm), (u_longlong_t)alloc);
722fa9e4066Sahrens 	}
723fa9e4066Sahrens }
724fa9e4066Sahrens 
725fa9e4066Sahrens static void
726d6e555bdSGeorge Wilson dump_metaslab_stats(metaslab_t *msp)
727d6e555bdSGeorge Wilson {
7283f9d6ad7SLin Ling 	char maxbuf[32];
7290713e232SGeorge Wilson 	range_tree_t *rt = msp->ms_tree;
7300713e232SGeorge Wilson 	avl_tree_t *t = &msp->ms_size_tree;
7310713e232SGeorge Wilson 	int free_pct = range_tree_space(rt) * 100 / msp->ms_size;
732d6e555bdSGeorge Wilson 
733*fc30d466SJason King 	/* max sure nicenum has enough space */
734*fc30d466SJason King 	CTASSERT(sizeof (maxbuf) >= NN_NUMBUF_SZ);
735*fc30d466SJason King 
736*fc30d466SJason King 	zdb_nicenum(metaslab_block_maxsize(msp), maxbuf, sizeof (maxbuf));
737d6e555bdSGeorge Wilson 
73887219db7SVictor Latushkin 	(void) printf("\t %25s %10lu   %7s  %6s   %4s %4d%%\n",
739d6e555bdSGeorge Wilson 	    "segments", avl_numnodes(t), "maxsize", maxbuf,
740d6e555bdSGeorge Wilson 	    "freepct", free_pct);
7410713e232SGeorge Wilson 	(void) printf("\tIn-memory histogram:\n");
7420713e232SGeorge Wilson 	dump_histogram(rt->rt_histogram, RANGE_TREE_HISTOGRAM_SIZE, 0);
743d6e555bdSGeorge Wilson }
744d6e555bdSGeorge Wilson 
745d6e555bdSGeorge Wilson static void
746fa9e4066Sahrens dump_metaslab(metaslab_t *msp)
747fa9e4066Sahrens {
748fa9e4066Sahrens 	vdev_t *vd = msp->ms_group->mg_vd;
749fa9e4066Sahrens 	spa_t *spa = vd->vdev_spa;
7500713e232SGeorge Wilson 	space_map_t *sm = msp->ms_sm;
7513f9d6ad7SLin Ling 	char freebuf[32];
752fa9e4066Sahrens 
753*fc30d466SJason King 	zdb_nicenum(msp->ms_size - space_map_allocated(sm), freebuf,
754*fc30d466SJason King 	    sizeof (freebuf));
755fa9e4066Sahrens 
756fa9e4066Sahrens 	(void) printf(
75787219db7SVictor Latushkin 	    "\tmetaslab %6llu   offset %12llx   spacemap %6llu   free    %5s\n",
7580713e232SGeorge Wilson 	    (u_longlong_t)msp->ms_id, (u_longlong_t)msp->ms_start,
7590713e232SGeorge Wilson 	    (u_longlong_t)space_map_object(sm), freebuf);
760fa9e4066Sahrens 
7610713e232SGeorge Wilson 	if (dump_opt['m'] > 2 && !dump_opt['L']) {
762d6e555bdSGeorge Wilson 		mutex_enter(&msp->ms_lock);
7630713e232SGeorge Wilson 		metaslab_load_wait(msp);
7640713e232SGeorge Wilson 		if (!msp->ms_loaded) {
7650713e232SGeorge Wilson 			VERIFY0(metaslab_load(msp));
7660713e232SGeorge Wilson 			range_tree_stat_verify(msp->ms_tree);
7670713e232SGeorge Wilson 		}
768d6e555bdSGeorge Wilson 		dump_metaslab_stats(msp);
7690713e232SGeorge Wilson 		metaslab_unload(msp);
770d6e555bdSGeorge Wilson 		mutex_exit(&msp->ms_lock);
771d6e555bdSGeorge Wilson 	}
772d6e555bdSGeorge Wilson 
7730713e232SGeorge Wilson 	if (dump_opt['m'] > 1 && sm != NULL &&
7742acef22dSMatthew Ahrens 	    spa_feature_is_active(spa, SPA_FEATURE_SPACEMAP_HISTOGRAM)) {
7750713e232SGeorge Wilson 		/*
7760713e232SGeorge Wilson 		 * The space map histogram represents free space in chunks
7770713e232SGeorge Wilson 		 * of sm_shift (i.e. bucket 0 refers to 2^sm_shift).
7780713e232SGeorge Wilson 		 */
7792e4c9986SGeorge Wilson 		(void) printf("\tOn-disk histogram:\t\tfragmentation %llu\n",
7802e4c9986SGeorge Wilson 		    (u_longlong_t)msp->ms_fragmentation);
7810713e232SGeorge Wilson 		dump_histogram(sm->sm_phys->smp_histogram,
7822e4c9986SGeorge Wilson 		    SPACE_MAP_HISTOGRAM_SIZE, sm->sm_shift);
7830713e232SGeorge Wilson 	}
7840713e232SGeorge Wilson 
7850713e232SGeorge Wilson 	if (dump_opt['d'] > 5 || dump_opt['m'] > 3) {
7860713e232SGeorge Wilson 		ASSERT(msp->ms_size == (1ULL << vd->vdev_ms_shift));
787fa9e4066Sahrens 
788d6e555bdSGeorge Wilson 		mutex_enter(&msp->ms_lock);
7890713e232SGeorge Wilson 		dump_spacemap(spa->spa_meta_objset, msp->ms_sm);
790d6e555bdSGeorge Wilson 		mutex_exit(&msp->ms_lock);
791d6e555bdSGeorge Wilson 	}
79287219db7SVictor Latushkin }
793d6e555bdSGeorge Wilson 
79487219db7SVictor Latushkin static void
79587219db7SVictor Latushkin print_vdev_metaslab_header(vdev_t *vd)
79687219db7SVictor Latushkin {
79787219db7SVictor Latushkin 	(void) printf("\tvdev %10llu\n\t%-10s%5llu   %-19s   %-15s   %-10s\n",
79887219db7SVictor Latushkin 	    (u_longlong_t)vd->vdev_id,
79987219db7SVictor Latushkin 	    "metaslabs", (u_longlong_t)vd->vdev_ms_count,
80087219db7SVictor Latushkin 	    "offset", "spacemap", "free");
80187219db7SVictor Latushkin 	(void) printf("\t%15s   %19s   %15s   %10s\n",
80287219db7SVictor Latushkin 	    "---------------", "-------------------",
80387219db7SVictor Latushkin 	    "---------------", "-------------");
804fa9e4066Sahrens }
805fa9e4066Sahrens 
806fa9e4066Sahrens static void
8072e4c9986SGeorge Wilson dump_metaslab_groups(spa_t *spa)
8082e4c9986SGeorge Wilson {
8092e4c9986SGeorge Wilson 	vdev_t *rvd = spa->spa_root_vdev;
8102e4c9986SGeorge Wilson 	metaslab_class_t *mc = spa_normal_class(spa);
8112e4c9986SGeorge Wilson 	uint64_t fragmentation;
8122e4c9986SGeorge Wilson 
8132e4c9986SGeorge Wilson 	metaslab_class_histogram_verify(mc);
8142e4c9986SGeorge Wilson 
8152e4c9986SGeorge Wilson 	for (int c = 0; c < rvd->vdev_children; c++) {
8162e4c9986SGeorge Wilson 		vdev_t *tvd = rvd->vdev_child[c];
8172e4c9986SGeorge Wilson 		metaslab_group_t *mg = tvd->vdev_mg;
8182e4c9986SGeorge Wilson 
8192e4c9986SGeorge Wilson 		if (mg->mg_class != mc)
8202e4c9986SGeorge Wilson 			continue;
8212e4c9986SGeorge Wilson 
8222e4c9986SGeorge Wilson 		metaslab_group_histogram_verify(mg);
8232e4c9986SGeorge Wilson 		mg->mg_fragmentation = metaslab_group_fragmentation(mg);
8242e4c9986SGeorge Wilson 
8252e4c9986SGeorge Wilson 		(void) printf("\tvdev %10llu\t\tmetaslabs%5llu\t\t"
8262e4c9986SGeorge Wilson 		    "fragmentation",
8272e4c9986SGeorge Wilson 		    (u_longlong_t)tvd->vdev_id,
8282e4c9986SGeorge Wilson 		    (u_longlong_t)tvd->vdev_ms_count);
8292e4c9986SGeorge Wilson 		if (mg->mg_fragmentation == ZFS_FRAG_INVALID) {
8302e4c9986SGeorge Wilson 			(void) printf("%3s\n", "-");
8312e4c9986SGeorge Wilson 		} else {
8322e4c9986SGeorge Wilson 			(void) printf("%3llu%%\n",
8332e4c9986SGeorge Wilson 			    (u_longlong_t)mg->mg_fragmentation);
8342e4c9986SGeorge Wilson 		}
8352e4c9986SGeorge Wilson 		dump_histogram(mg->mg_histogram, RANGE_TREE_HISTOGRAM_SIZE, 0);
8362e4c9986SGeorge Wilson 	}
8372e4c9986SGeorge Wilson 
8382e4c9986SGeorge Wilson 	(void) printf("\tpool %s\tfragmentation", spa_name(spa));
8392e4c9986SGeorge Wilson 	fragmentation = metaslab_class_fragmentation(mc);
8402e4c9986SGeorge Wilson 	if (fragmentation == ZFS_FRAG_INVALID)
8412e4c9986SGeorge Wilson 		(void) printf("\t%3s\n", "-");
8422e4c9986SGeorge Wilson 	else
8432e4c9986SGeorge Wilson 		(void) printf("\t%3llu%%\n", (u_longlong_t)fragmentation);
8442e4c9986SGeorge Wilson 	dump_histogram(mc->mc_histogram, RANGE_TREE_HISTOGRAM_SIZE, 0);
8452e4c9986SGeorge Wilson }
8462e4c9986SGeorge Wilson 
8472e4c9986SGeorge Wilson static void
848fa9e4066Sahrens dump_metaslabs(spa_t *spa)
849fa9e4066Sahrens {
85087219db7SVictor Latushkin 	vdev_t *vd, *rvd = spa->spa_root_vdev;
85187219db7SVictor Latushkin 	uint64_t m, c = 0, children = rvd->vdev_children;
852fa9e4066Sahrens 
853fa9e4066Sahrens 	(void) printf("\nMetaslabs:\n");
854fa9e4066Sahrens 
85587219db7SVictor Latushkin 	if (!dump_opt['d'] && zopt_objects > 0) {
85687219db7SVictor Latushkin 		c = zopt_object[0];
857fa9e4066Sahrens 
85887219db7SVictor Latushkin 		if (c >= children)
85987219db7SVictor Latushkin 			(void) fatal("bad vdev id: %llu", (u_longlong_t)c);
86087219db7SVictor Latushkin 
86187219db7SVictor Latushkin 		if (zopt_objects > 1) {
86287219db7SVictor Latushkin 			vd = rvd->vdev_child[c];
86387219db7SVictor Latushkin 			print_vdev_metaslab_header(vd);
86487219db7SVictor Latushkin 
86587219db7SVictor Latushkin 			for (m = 1; m < zopt_objects; m++) {
86687219db7SVictor Latushkin 				if (zopt_object[m] < vd->vdev_ms_count)
86787219db7SVictor Latushkin 					dump_metaslab(
86887219db7SVictor Latushkin 					    vd->vdev_ms[zopt_object[m]]);
86987219db7SVictor Latushkin 				else
87087219db7SVictor Latushkin 					(void) fprintf(stderr, "bad metaslab "
87187219db7SVictor Latushkin 					    "number %llu\n",
87287219db7SVictor Latushkin 					    (u_longlong_t)zopt_object[m]);
87387219db7SVictor Latushkin 			}
87487219db7SVictor Latushkin 			(void) printf("\n");
87587219db7SVictor Latushkin 			return;
87687219db7SVictor Latushkin 		}
87787219db7SVictor Latushkin 		children = c + 1;
87887219db7SVictor Latushkin 	}
87987219db7SVictor Latushkin 	for (; c < children; c++) {
88087219db7SVictor Latushkin 		vd = rvd->vdev_child[c];
88187219db7SVictor Latushkin 		print_vdev_metaslab_header(vd);
882fa9e4066Sahrens 
883fa9e4066Sahrens 		for (m = 0; m < vd->vdev_ms_count; m++)
884fa9e4066Sahrens 			dump_metaslab(vd->vdev_ms[m]);
885fa9e4066Sahrens 		(void) printf("\n");
886fa9e4066Sahrens 	}
887fa9e4066Sahrens }
888fa9e4066Sahrens 
889fa9e4066Sahrens static void
890b24ab676SJeff Bonwick dump_dde(const ddt_t *ddt, const ddt_entry_t *dde, uint64_t index)
891b24ab676SJeff Bonwick {
892b24ab676SJeff Bonwick 	const ddt_phys_t *ddp = dde->dde_phys;
893b24ab676SJeff Bonwick 	const ddt_key_t *ddk = &dde->dde_key;
894b24ab676SJeff Bonwick 	char *types[4] = { "ditto", "single", "double", "triple" };
895b24ab676SJeff Bonwick 	char blkbuf[BP_SPRINTF_LEN];
896b24ab676SJeff Bonwick 	blkptr_t blk;
897b24ab676SJeff Bonwick 
898b24ab676SJeff Bonwick 	for (int p = 0; p < DDT_PHYS_TYPES; p++, ddp++) {
899b24ab676SJeff Bonwick 		if (ddp->ddp_phys_birth == 0)
900b24ab676SJeff Bonwick 			continue;
901bbfd46c4SJeff Bonwick 		ddt_bp_create(ddt->ddt_checksum, ddk, ddp, &blk);
90243466aaeSMax Grossman 		snprintf_blkptr(blkbuf, sizeof (blkbuf), &blk);
903b24ab676SJeff Bonwick 		(void) printf("index %llx refcnt %llu %s %s\n",
904b24ab676SJeff Bonwick 		    (u_longlong_t)index, (u_longlong_t)ddp->ddp_refcnt,
905b24ab676SJeff Bonwick 		    types[p], blkbuf);
906b24ab676SJeff Bonwick 	}
907b24ab676SJeff Bonwick }
908b24ab676SJeff Bonwick 
909b24ab676SJeff Bonwick static void
910b24ab676SJeff Bonwick dump_dedup_ratio(const ddt_stat_t *dds)
911b24ab676SJeff Bonwick {
912b24ab676SJeff Bonwick 	double rL, rP, rD, D, dedup, compress, copies;
913b24ab676SJeff Bonwick 
914b24ab676SJeff Bonwick 	if (dds->dds_blocks == 0)
915b24ab676SJeff Bonwick 		return;
916b24ab676SJeff Bonwick 
917b24ab676SJeff Bonwick 	rL = (double)dds->dds_ref_lsize;
918b24ab676SJeff Bonwick 	rP = (double)dds->dds_ref_psize;
919b24ab676SJeff Bonwick 	rD = (double)dds->dds_ref_dsize;
920b24ab676SJeff Bonwick 	D = (double)dds->dds_dsize;
921b24ab676SJeff Bonwick 
922b24ab676SJeff Bonwick 	dedup = rD / D;
923b24ab676SJeff Bonwick 	compress = rL / rP;
924b24ab676SJeff Bonwick 	copies = rD / rP;
925b24ab676SJeff Bonwick 
926b24ab676SJeff Bonwick 	(void) printf("dedup = %.2f, compress = %.2f, copies = %.2f, "
927b24ab676SJeff Bonwick 	    "dedup * compress / copies = %.2f\n\n",
928b24ab676SJeff Bonwick 	    dedup, compress, copies, dedup * compress / copies);
929b24ab676SJeff Bonwick }
930b24ab676SJeff Bonwick 
931b24ab676SJeff Bonwick static void
932b24ab676SJeff Bonwick dump_ddt(ddt_t *ddt, enum ddt_type type, enum ddt_class class)
933b24ab676SJeff Bonwick {
934b24ab676SJeff Bonwick 	char name[DDT_NAMELEN];
935b24ab676SJeff Bonwick 	ddt_entry_t dde;
936b24ab676SJeff Bonwick 	uint64_t walk = 0;
937b24ab676SJeff Bonwick 	dmu_object_info_t doi;
938b24ab676SJeff Bonwick 	uint64_t count, dspace, mspace;
939b24ab676SJeff Bonwick 	int error;
940b24ab676SJeff Bonwick 
941b24ab676SJeff Bonwick 	error = ddt_object_info(ddt, type, class, &doi);
942b24ab676SJeff Bonwick 
943b24ab676SJeff Bonwick 	if (error == ENOENT)
944b24ab676SJeff Bonwick 		return;
945b24ab676SJeff Bonwick 	ASSERT(error == 0);
946b24ab676SJeff Bonwick 
9477448a079SGeorge Wilson 	if ((count = ddt_object_count(ddt, type, class)) == 0)
9487448a079SGeorge Wilson 		return;
9497448a079SGeorge Wilson 
950b24ab676SJeff Bonwick 	dspace = doi.doi_physical_blocks_512 << 9;
951b24ab676SJeff Bonwick 	mspace = doi.doi_fill_count * doi.doi_data_block_size;
952b24ab676SJeff Bonwick 
953b24ab676SJeff Bonwick 	ddt_object_name(ddt, type, class, name);
954b24ab676SJeff Bonwick 
955b24ab676SJeff Bonwick 	(void) printf("%s: %llu entries, size %llu on disk, %llu in core\n",
956b24ab676SJeff Bonwick 	    name,
957b24ab676SJeff Bonwick 	    (u_longlong_t)count,
958b24ab676SJeff Bonwick 	    (u_longlong_t)(dspace / count),
959b24ab676SJeff Bonwick 	    (u_longlong_t)(mspace / count));
960b24ab676SJeff Bonwick 
961b24ab676SJeff Bonwick 	if (dump_opt['D'] < 3)
962b24ab676SJeff Bonwick 		return;
963b24ab676SJeff Bonwick 
9649eb19f4dSGeorge Wilson 	zpool_dump_ddt(NULL, &ddt->ddt_histogram[type][class]);
965b24ab676SJeff Bonwick 
966b24ab676SJeff Bonwick 	if (dump_opt['D'] < 4)
967b24ab676SJeff Bonwick 		return;
968b24ab676SJeff Bonwick 
969b24ab676SJeff Bonwick 	if (dump_opt['D'] < 5 && class == DDT_CLASS_UNIQUE)
970b24ab676SJeff Bonwick 		return;
971b24ab676SJeff Bonwick 
972b24ab676SJeff Bonwick 	(void) printf("%s contents:\n\n", name);
973b24ab676SJeff Bonwick 
974bbfd46c4SJeff Bonwick 	while ((error = ddt_object_walk(ddt, type, class, &walk, &dde)) == 0)
975b24ab676SJeff Bonwick 		dump_dde(ddt, &dde, walk);
976b24ab676SJeff Bonwick 
977b24ab676SJeff Bonwick 	ASSERT(error == ENOENT);
978b24ab676SJeff Bonwick 
979b24ab676SJeff Bonwick 	(void) printf("\n");
980b24ab676SJeff Bonwick }
981b24ab676SJeff Bonwick 
982b24ab676SJeff Bonwick static void
983b24ab676SJeff Bonwick dump_all_ddts(spa_t *spa)
984b24ab676SJeff Bonwick {
985b24ab676SJeff Bonwick 	ddt_histogram_t ddh_total = { 0 };
986b24ab676SJeff Bonwick 	ddt_stat_t dds_total = { 0 };
987b24ab676SJeff Bonwick 
988b24ab676SJeff Bonwick 	for (enum zio_checksum c = 0; c < ZIO_CHECKSUM_FUNCTIONS; c++) {
989b24ab676SJeff Bonwick 		ddt_t *ddt = spa->spa_ddt[c];
990b24ab676SJeff Bonwick 		for (enum ddt_type type = 0; type < DDT_TYPES; type++) {
991b24ab676SJeff Bonwick 			for (enum ddt_class class = 0; class < DDT_CLASSES;
992b24ab676SJeff Bonwick 			    class++) {
993b24ab676SJeff Bonwick 				dump_ddt(ddt, type, class);
994b24ab676SJeff Bonwick 			}
995b24ab676SJeff Bonwick 		}
996b24ab676SJeff Bonwick 	}
997b24ab676SJeff Bonwick 
9989eb19f4dSGeorge Wilson 	ddt_get_dedup_stats(spa, &dds_total);
999b24ab676SJeff Bonwick 
1000b24ab676SJeff Bonwick 	if (dds_total.dds_blocks == 0) {
1001b24ab676SJeff Bonwick 		(void) printf("All DDTs are empty\n");
1002b24ab676SJeff Bonwick 		return;
1003b24ab676SJeff Bonwick 	}
1004b24ab676SJeff Bonwick 
1005b24ab676SJeff Bonwick 	(void) printf("\n");
1006b24ab676SJeff Bonwick 
1007b24ab676SJeff Bonwick 	if (dump_opt['D'] > 1) {
1008b24ab676SJeff Bonwick 		(void) printf("DDT histogram (aggregated over all DDTs):\n");
10099eb19f4dSGeorge Wilson 		ddt_get_dedup_histogram(spa, &ddh_total);
10109eb19f4dSGeorge Wilson 		zpool_dump_ddt(&dds_total, &ddh_total);
1011b24ab676SJeff Bonwick 	}
1012b24ab676SJeff Bonwick 
1013b24ab676SJeff Bonwick 	dump_dedup_ratio(&dds_total);
1014b24ab676SJeff Bonwick }
1015b24ab676SJeff Bonwick 
1016b24ab676SJeff Bonwick static void
10170713e232SGeorge Wilson dump_dtl_seg(void *arg, uint64_t start, uint64_t size)
10188ad4d6ddSJeff Bonwick {
10190713e232SGeorge Wilson 	char *prefix = arg;
10208ad4d6ddSJeff Bonwick 
10218ad4d6ddSJeff Bonwick 	(void) printf("%s [%llu,%llu) length %llu\n",
10228ad4d6ddSJeff Bonwick 	    prefix,
10238ad4d6ddSJeff Bonwick 	    (u_longlong_t)start,
10248ad4d6ddSJeff Bonwick 	    (u_longlong_t)(start + size),
10258ad4d6ddSJeff Bonwick 	    (u_longlong_t)(size));
10268ad4d6ddSJeff Bonwick }
10278ad4d6ddSJeff Bonwick 
10288ad4d6ddSJeff Bonwick static void
1029fa9e4066Sahrens dump_dtl(vdev_t *vd, int indent)
1030fa9e4066Sahrens {
10318ad4d6ddSJeff Bonwick 	spa_t *spa = vd->vdev_spa;
10328ad4d6ddSJeff Bonwick 	boolean_t required;
10338ad4d6ddSJeff Bonwick 	char *name[DTL_TYPES] = { "missing", "partial", "scrub", "outage" };
10348ad4d6ddSJeff Bonwick 	char prefix[256];
10358ad4d6ddSJeff Bonwick 
10368f18d1faSGeorge Wilson 	spa_vdev_state_enter(spa, SCL_NONE);
10378ad4d6ddSJeff Bonwick 	required = vdev_dtl_required(vd);
10388ad4d6ddSJeff Bonwick 	(void) spa_vdev_state_exit(spa, NULL, 0);
1039fa9e4066Sahrens 
1040fa9e4066Sahrens 	if (indent == 0)
1041fa9e4066Sahrens 		(void) printf("\nDirty time logs:\n\n");
1042fa9e4066Sahrens 
10438ad4d6ddSJeff Bonwick 	(void) printf("\t%*s%s [%s]\n", indent, "",
1044e14bb325SJeff Bonwick 	    vd->vdev_path ? vd->vdev_path :
10458ad4d6ddSJeff Bonwick 	    vd->vdev_parent ? vd->vdev_ops->vdev_op_type : spa_name(spa),
10468ad4d6ddSJeff Bonwick 	    required ? "DTL-required" : "DTL-expendable");
1047fa9e4066Sahrens 
10488ad4d6ddSJeff Bonwick 	for (int t = 0; t < DTL_TYPES; t++) {
10490713e232SGeorge Wilson 		range_tree_t *rt = vd->vdev_dtl[t];
10500713e232SGeorge Wilson 		if (range_tree_space(rt) == 0)
10518ad4d6ddSJeff Bonwick 			continue;
10528ad4d6ddSJeff Bonwick 		(void) snprintf(prefix, sizeof (prefix), "\t%*s%s",
10538ad4d6ddSJeff Bonwick 		    indent + 2, "", name[t]);
10540713e232SGeorge Wilson 		mutex_enter(rt->rt_lock);
10550713e232SGeorge Wilson 		range_tree_walk(rt, dump_dtl_seg, prefix);
10560713e232SGeorge Wilson 		mutex_exit(rt->rt_lock);
10578ad4d6ddSJeff Bonwick 		if (dump_opt['d'] > 5 && vd->vdev_children == 0)
10580713e232SGeorge Wilson 			dump_spacemap(spa->spa_meta_objset, vd->vdev_dtl_sm);
1059fa9e4066Sahrens 	}
1060fa9e4066Sahrens 
10618ad4d6ddSJeff Bonwick 	for (int c = 0; c < vd->vdev_children; c++)
1062fa9e4066Sahrens 		dump_dtl(vd->vdev_child[c], indent + 4);
1063fa9e4066Sahrens }
1064fa9e4066Sahrens 
10658f18d1faSGeorge Wilson static void
10668f18d1faSGeorge Wilson dump_history(spa_t *spa)
10678f18d1faSGeorge Wilson {
10688f18d1faSGeorge Wilson 	nvlist_t **events = NULL;
1069e4161df6SVictor Latushkin 	uint64_t resid, len, off = 0;
10708f18d1faSGeorge Wilson 	uint_t num = 0;
10718f18d1faSGeorge Wilson 	int error;
10728f18d1faSGeorge Wilson 	time_t tsec;
10738f18d1faSGeorge Wilson 	struct tm t;
10748f18d1faSGeorge Wilson 	char tbuf[30];
10758f18d1faSGeorge Wilson 	char internalstr[MAXPATHLEN];
10768f18d1faSGeorge Wilson 
10773f84190cSMatthew Ahrens 	char *buf = umem_alloc(SPA_MAXBLOCKSIZE, UMEM_NOFAIL);
10788f18d1faSGeorge Wilson 	do {
10793f84190cSMatthew Ahrens 		len = SPA_MAXBLOCKSIZE;
1080e4161df6SVictor Latushkin 
10818f18d1faSGeorge Wilson 		if ((error = spa_history_get(spa, &off, &len, buf)) != 0) {
10828f18d1faSGeorge Wilson 			(void) fprintf(stderr, "Unable to read history: "
10838f18d1faSGeorge Wilson 			    "error %d\n", error);
10843f84190cSMatthew Ahrens 			umem_free(buf, SPA_MAXBLOCKSIZE);
10858f18d1faSGeorge Wilson 			return;
10868f18d1faSGeorge Wilson 		}
10878f18d1faSGeorge Wilson 
10888f18d1faSGeorge Wilson 		if (zpool_history_unpack(buf, len, &resid, &events, &num) != 0)
10898f18d1faSGeorge Wilson 			break;
10908f18d1faSGeorge Wilson 
10918f18d1faSGeorge Wilson 		off -= resid;
10928f18d1faSGeorge Wilson 	} while (len != 0);
10933f84190cSMatthew Ahrens 	umem_free(buf, SPA_MAXBLOCKSIZE);
10948f18d1faSGeorge Wilson 
10958f18d1faSGeorge Wilson 	(void) printf("\nHistory:\n");
10968f18d1faSGeorge Wilson 	for (int i = 0; i < num; i++) {
10978f18d1faSGeorge Wilson 		uint64_t time, txg, ievent;
10988f18d1faSGeorge Wilson 		char *cmd, *intstr;
10994445fffbSMatthew Ahrens 		boolean_t printed = B_FALSE;
11008f18d1faSGeorge Wilson 
11018f18d1faSGeorge Wilson 		if (nvlist_lookup_uint64(events[i], ZPOOL_HIST_TIME,
11028f18d1faSGeorge Wilson 		    &time) != 0)
11034445fffbSMatthew Ahrens 			goto next;
11048f18d1faSGeorge Wilson 		if (nvlist_lookup_string(events[i], ZPOOL_HIST_CMD,
11058f18d1faSGeorge Wilson 		    &cmd) != 0) {
11068f18d1faSGeorge Wilson 			if (nvlist_lookup_uint64(events[i],
11078f18d1faSGeorge Wilson 			    ZPOOL_HIST_INT_EVENT, &ievent) != 0)
11084445fffbSMatthew Ahrens 				goto next;
11098f18d1faSGeorge Wilson 			verify(nvlist_lookup_uint64(events[i],
11108f18d1faSGeorge Wilson 			    ZPOOL_HIST_TXG, &txg) == 0);
11118f18d1faSGeorge Wilson 			verify(nvlist_lookup_string(events[i],
11128f18d1faSGeorge Wilson 			    ZPOOL_HIST_INT_STR, &intstr) == 0);
11134445fffbSMatthew Ahrens 			if (ievent >= ZFS_NUM_LEGACY_HISTORY_EVENTS)
11144445fffbSMatthew Ahrens 				goto next;
11158f18d1faSGeorge Wilson 
11168f18d1faSGeorge Wilson 			(void) snprintf(internalstr,
11178f18d1faSGeorge Wilson 			    sizeof (internalstr),
11188f18d1faSGeorge Wilson 			    "[internal %s txg:%lld] %s",
11193f9d6ad7SLin Ling 			    zfs_history_event_names[ievent], txg,
11208f18d1faSGeorge Wilson 			    intstr);
11218f18d1faSGeorge Wilson 			cmd = internalstr;
11228f18d1faSGeorge Wilson 		}
11238f18d1faSGeorge Wilson 		tsec = time;
11248f18d1faSGeorge Wilson 		(void) localtime_r(&tsec, &t);
11258f18d1faSGeorge Wilson 		(void) strftime(tbuf, sizeof (tbuf), "%F.%T", &t);
11268f18d1faSGeorge Wilson 		(void) printf("%s %s\n", tbuf, cmd);
11274445fffbSMatthew Ahrens 		printed = B_TRUE;
11284445fffbSMatthew Ahrens 
11294445fffbSMatthew Ahrens next:
11304445fffbSMatthew Ahrens 		if (dump_opt['h'] > 1) {
11314445fffbSMatthew Ahrens 			if (!printed)
11324445fffbSMatthew Ahrens 				(void) printf("unrecognized record:\n");
11334445fffbSMatthew Ahrens 			dump_nvlist(events[i], 2);
11344445fffbSMatthew Ahrens 		}
11358f18d1faSGeorge Wilson 	}
11368f18d1faSGeorge Wilson }
11378f18d1faSGeorge Wilson 
1138fa9e4066Sahrens /*ARGSUSED*/
1139fa9e4066Sahrens static void
1140fa9e4066Sahrens dump_dnode(objset_t *os, uint64_t object, void *data, size_t size)
1141fa9e4066Sahrens {
1142fa9e4066Sahrens }
1143fa9e4066Sahrens 
1144fa9e4066Sahrens static uint64_t
11457802d7bfSMatthew Ahrens blkid2offset(const dnode_phys_t *dnp, const blkptr_t *bp,
11467802d7bfSMatthew Ahrens     const zbookmark_phys_t *zb)
1147fa9e4066Sahrens {
1148b24ab676SJeff Bonwick 	if (dnp == NULL) {
1149b24ab676SJeff Bonwick 		ASSERT(zb->zb_level < 0);
1150b24ab676SJeff Bonwick 		if (zb->zb_object == 0)
1151b24ab676SJeff Bonwick 			return (zb->zb_blkid);
1152b24ab676SJeff Bonwick 		return (zb->zb_blkid * BP_GET_LSIZE(bp));
1153b24ab676SJeff Bonwick 	}
1154fa9e4066Sahrens 
1155b24ab676SJeff Bonwick 	ASSERT(zb->zb_level >= 0);
1156b24ab676SJeff Bonwick 
1157b24ab676SJeff Bonwick 	return ((zb->zb_blkid <<
1158b24ab676SJeff Bonwick 	    (zb->zb_level * (dnp->dn_indblkshift - SPA_BLKPTRSHIFT))) *
1159fa9e4066Sahrens 	    dnp->dn_datablkszsec << SPA_MINBLOCKSHIFT);
1160fa9e4066Sahrens }
1161fa9e4066Sahrens 
116244cd46caSbillm static void
116343466aaeSMax Grossman snprintf_blkptr_compact(char *blkbuf, size_t buflen, const blkptr_t *bp)
116444cd46caSbillm {
1165cde58dbcSMatthew Ahrens 	const dva_t *dva = bp->blk_dva;
1166b24ab676SJeff Bonwick 	int ndvas = dump_opt['d'] > 5 ? BP_GET_NDVAS(bp) : 1;
1167b24ab676SJeff Bonwick 
1168490d05b9SMatthew Ahrens 	if (dump_opt['b'] >= 6) {
116943466aaeSMax Grossman 		snprintf_blkptr(blkbuf, buflen, bp);
1170b24ab676SJeff Bonwick 		return;
1171b24ab676SJeff Bonwick 	}
117244cd46caSbillm 
11735d7b4d43SMatthew Ahrens 	if (BP_IS_EMBEDDED(bp)) {
11745d7b4d43SMatthew Ahrens 		(void) sprintf(blkbuf,
11755d7b4d43SMatthew Ahrens 		    "EMBEDDED et=%u %llxL/%llxP B=%llu",
11765d7b4d43SMatthew Ahrens 		    (int)BPE_GET_ETYPE(bp),
11775d7b4d43SMatthew Ahrens 		    (u_longlong_t)BPE_GET_LSIZE(bp),
11785d7b4d43SMatthew Ahrens 		    (u_longlong_t)BPE_GET_PSIZE(bp),
11795d7b4d43SMatthew Ahrens 		    (u_longlong_t)bp->blk_birth);
11805d7b4d43SMatthew Ahrens 		return;
11815d7b4d43SMatthew Ahrens 	}
118244cd46caSbillm 
11835d7b4d43SMatthew Ahrens 	blkbuf[0] = '\0';
1184b24ab676SJeff Bonwick 	for (int i = 0; i < ndvas; i++)
118543466aaeSMax Grossman 		(void) snprintf(blkbuf + strlen(blkbuf),
118643466aaeSMax Grossman 		    buflen - strlen(blkbuf), "%llu:%llx:%llx ",
118744cd46caSbillm 		    (u_longlong_t)DVA_GET_VDEV(&dva[i]),
118844cd46caSbillm 		    (u_longlong_t)DVA_GET_OFFSET(&dva[i]),
118944cd46caSbillm 		    (u_longlong_t)DVA_GET_ASIZE(&dva[i]));
119044cd46caSbillm 
119143466aaeSMax Grossman 	if (BP_IS_HOLE(bp)) {
119243466aaeSMax Grossman 		(void) snprintf(blkbuf + strlen(blkbuf),
119370163ac5SPrakash Surya 		    buflen - strlen(blkbuf),
119470163ac5SPrakash Surya 		    "%llxL B=%llu",
119570163ac5SPrakash Surya 		    (u_longlong_t)BP_GET_LSIZE(bp),
119643466aaeSMax Grossman 		    (u_longlong_t)bp->blk_birth);
119743466aaeSMax Grossman 	} else {
119843466aaeSMax Grossman 		(void) snprintf(blkbuf + strlen(blkbuf),
119943466aaeSMax Grossman 		    buflen - strlen(blkbuf),
1200b24ab676SJeff Bonwick 		    "%llxL/%llxP F=%llu B=%llu/%llu",
120144cd46caSbillm 		    (u_longlong_t)BP_GET_LSIZE(bp),
120244cd46caSbillm 		    (u_longlong_t)BP_GET_PSIZE(bp),
12035d7b4d43SMatthew Ahrens 		    (u_longlong_t)BP_GET_FILL(bp),
1204b24ab676SJeff Bonwick 		    (u_longlong_t)bp->blk_birth,
1205b24ab676SJeff Bonwick 		    (u_longlong_t)BP_PHYSICAL_BIRTH(bp));
120644cd46caSbillm 	}
120743466aaeSMax Grossman }
120844cd46caSbillm 
120988b7b0f2SMatthew Ahrens static void
12107802d7bfSMatthew Ahrens print_indirect(blkptr_t *bp, const zbookmark_phys_t *zb,
121188b7b0f2SMatthew Ahrens     const dnode_phys_t *dnp)
1212fa9e4066Sahrens {
121388b7b0f2SMatthew Ahrens 	char blkbuf[BP_SPRINTF_LEN];
1214fa9e4066Sahrens 	int l;
1215fa9e4066Sahrens 
12165d7b4d43SMatthew Ahrens 	if (!BP_IS_EMBEDDED(bp)) {
1217fa9e4066Sahrens 		ASSERT3U(BP_GET_TYPE(bp), ==, dnp->dn_type);
1218fa9e4066Sahrens 		ASSERT3U(BP_GET_LEVEL(bp), ==, zb->zb_level);
12195d7b4d43SMatthew Ahrens 	}
1220fa9e4066Sahrens 
1221b24ab676SJeff Bonwick 	(void) printf("%16llx ", (u_longlong_t)blkid2offset(dnp, bp, zb));
1222fa9e4066Sahrens 
1223fa9e4066Sahrens 	ASSERT(zb->zb_level >= 0);
1224fa9e4066Sahrens 
1225fa9e4066Sahrens 	for (l = dnp->dn_nlevels - 1; l >= -1; l--) {
1226fa9e4066Sahrens 		if (l == zb->zb_level) {
122788b7b0f2SMatthew Ahrens 			(void) printf("L%llx", (u_longlong_t)zb->zb_level);
1228fa9e4066Sahrens 		} else {
122988b7b0f2SMatthew Ahrens 			(void) printf(" ");
1230fa9e4066Sahrens 		}
1231fa9e4066Sahrens 	}
1232fa9e4066Sahrens 
123343466aaeSMax Grossman 	snprintf_blkptr_compact(blkbuf, sizeof (blkbuf), bp);
123444cd46caSbillm 	(void) printf("%s\n", blkbuf);
1235fa9e4066Sahrens }
1236fa9e4066Sahrens 
123788b7b0f2SMatthew Ahrens static int
123888b7b0f2SMatthew Ahrens visit_indirect(spa_t *spa, const dnode_phys_t *dnp,
12397802d7bfSMatthew Ahrens     blkptr_t *bp, const zbookmark_phys_t *zb)
124088b7b0f2SMatthew Ahrens {
1241e4161df6SVictor Latushkin 	int err = 0;
124288b7b0f2SMatthew Ahrens 
124388b7b0f2SMatthew Ahrens 	if (bp->blk_birth == 0)
124488b7b0f2SMatthew Ahrens 		return (0);
124588b7b0f2SMatthew Ahrens 
124688b7b0f2SMatthew Ahrens 	print_indirect(bp, zb, dnp);
124788b7b0f2SMatthew Ahrens 
124843466aaeSMax Grossman 	if (BP_GET_LEVEL(bp) > 0 && !BP_IS_HOLE(bp)) {
12497adb730bSGeorge Wilson 		arc_flags_t flags = ARC_FLAG_WAIT;
125088b7b0f2SMatthew Ahrens 		int i;
125188b7b0f2SMatthew Ahrens 		blkptr_t *cbp;
125288b7b0f2SMatthew Ahrens 		int epb = BP_GET_LSIZE(bp) >> SPA_BLKPTRSHIFT;
125388b7b0f2SMatthew Ahrens 		arc_buf_t *buf;
125488b7b0f2SMatthew Ahrens 		uint64_t fill = 0;
125588b7b0f2SMatthew Ahrens 
12561b912ec7SGeorge Wilson 		err = arc_read(NULL, spa, bp, arc_getbuf_func, &buf,
125788b7b0f2SMatthew Ahrens 		    ZIO_PRIORITY_ASYNC_READ, ZIO_FLAG_CANFAIL, &flags, zb);
125888b7b0f2SMatthew Ahrens 		if (err)
125988b7b0f2SMatthew Ahrens 			return (err);
12603f9d6ad7SLin Ling 		ASSERT(buf->b_data);
126188b7b0f2SMatthew Ahrens 
126288b7b0f2SMatthew Ahrens 		/* recursively visit blocks below this */
126388b7b0f2SMatthew Ahrens 		cbp = buf->b_data;
126488b7b0f2SMatthew Ahrens 		for (i = 0; i < epb; i++, cbp++) {
12657802d7bfSMatthew Ahrens 			zbookmark_phys_t czb;
126688b7b0f2SMatthew Ahrens 
126788b7b0f2SMatthew Ahrens 			SET_BOOKMARK(&czb, zb->zb_objset, zb->zb_object,
126888b7b0f2SMatthew Ahrens 			    zb->zb_level - 1,
126988b7b0f2SMatthew Ahrens 			    zb->zb_blkid * epb + i);
127088b7b0f2SMatthew Ahrens 			err = visit_indirect(spa, dnp, cbp, &czb);
127188b7b0f2SMatthew Ahrens 			if (err)
127288b7b0f2SMatthew Ahrens 				break;
12735d7b4d43SMatthew Ahrens 			fill += BP_GET_FILL(cbp);
127488b7b0f2SMatthew Ahrens 		}
12758ad4d6ddSJeff Bonwick 		if (!err)
12765d7b4d43SMatthew Ahrens 			ASSERT3U(fill, ==, BP_GET_FILL(bp));
127788b7b0f2SMatthew Ahrens 		(void) arc_buf_remove_ref(buf, &buf);
127888b7b0f2SMatthew Ahrens 	}
127988b7b0f2SMatthew Ahrens 
128088b7b0f2SMatthew Ahrens 	return (err);
1281fa9e4066Sahrens }
1282fa9e4066Sahrens 
1283fa9e4066Sahrens /*ARGSUSED*/
1284fa9e4066Sahrens static void
128588b7b0f2SMatthew Ahrens dump_indirect(dnode_t *dn)
1286fa9e4066Sahrens {
128788b7b0f2SMatthew Ahrens 	dnode_phys_t *dnp = dn->dn_phys;
128888b7b0f2SMatthew Ahrens 	int j;
12897802d7bfSMatthew Ahrens 	zbookmark_phys_t czb;
1290fa9e4066Sahrens 
1291fa9e4066Sahrens 	(void) printf("Indirect blocks:\n");
1292fa9e4066Sahrens 
1293503ad85cSMatthew Ahrens 	SET_BOOKMARK(&czb, dmu_objset_id(dn->dn_objset),
129488b7b0f2SMatthew Ahrens 	    dn->dn_object, dnp->dn_nlevels - 1, 0);
129588b7b0f2SMatthew Ahrens 	for (j = 0; j < dnp->dn_nblkptr; j++) {
129688b7b0f2SMatthew Ahrens 		czb.zb_blkid = j;
1297503ad85cSMatthew Ahrens 		(void) visit_indirect(dmu_objset_spa(dn->dn_objset), dnp,
129888b7b0f2SMatthew Ahrens 		    &dnp->dn_blkptr[j], &czb);
129988b7b0f2SMatthew Ahrens 	}
1300fa9e4066Sahrens 
1301fa9e4066Sahrens 	(void) printf("\n");
1302fa9e4066Sahrens }
1303fa9e4066Sahrens 
1304fa9e4066Sahrens /*ARGSUSED*/
1305fa9e4066Sahrens static void
1306fa9e4066Sahrens dump_dsl_dir(objset_t *os, uint64_t object, void *data, size_t size)
1307fa9e4066Sahrens {
1308fa9e4066Sahrens 	dsl_dir_phys_t *dd = data;
1309fa9e4066Sahrens 	time_t crtime;
13103f9d6ad7SLin Ling 	char nice[32];
1311fa9e4066Sahrens 
1312*fc30d466SJason King 	/* make sure nicenum has enough space */
1313*fc30d466SJason King 	CTASSERT(sizeof (nice) >= NN_NUMBUF_SZ);
1314*fc30d466SJason King 
1315fa9e4066Sahrens 	if (dd == NULL)
1316fa9e4066Sahrens 		return;
1317fa9e4066Sahrens 
1318da6c28aaSamw 	ASSERT3U(size, >=, sizeof (dsl_dir_phys_t));
1319fa9e4066Sahrens 
1320fa9e4066Sahrens 	crtime = dd->dd_creation_time;
1321fa9e4066Sahrens 	(void) printf("\t\tcreation_time = %s", ctime(&crtime));
1322fa9e4066Sahrens 	(void) printf("\t\thead_dataset_obj = %llu\n",
1323fa9e4066Sahrens 	    (u_longlong_t)dd->dd_head_dataset_obj);
1324fa9e4066Sahrens 	(void) printf("\t\tparent_dir_obj = %llu\n",
1325fa9e4066Sahrens 	    (u_longlong_t)dd->dd_parent_obj);
13263cb34c60Sahrens 	(void) printf("\t\torigin_obj = %llu\n",
13273cb34c60Sahrens 	    (u_longlong_t)dd->dd_origin_obj);
1328fa9e4066Sahrens 	(void) printf("\t\tchild_dir_zapobj = %llu\n",
1329fa9e4066Sahrens 	    (u_longlong_t)dd->dd_child_dir_zapobj);
1330*fc30d466SJason King 	zdb_nicenum(dd->dd_used_bytes, nice, sizeof (nice));
133174e7dc98SMatthew Ahrens 	(void) printf("\t\tused_bytes = %s\n", nice);
1332*fc30d466SJason King 	zdb_nicenum(dd->dd_compressed_bytes, nice, sizeof (nice));
133374e7dc98SMatthew Ahrens 	(void) printf("\t\tcompressed_bytes = %s\n", nice);
1334*fc30d466SJason King 	zdb_nicenum(dd->dd_uncompressed_bytes, nice, sizeof (nice));
133574e7dc98SMatthew Ahrens 	(void) printf("\t\tuncompressed_bytes = %s\n", nice);
1336*fc30d466SJason King 	zdb_nicenum(dd->dd_quota, nice, sizeof (nice));
133774e7dc98SMatthew Ahrens 	(void) printf("\t\tquota = %s\n", nice);
1338*fc30d466SJason King 	zdb_nicenum(dd->dd_reserved, nice, sizeof (nice));
133974e7dc98SMatthew Ahrens 	(void) printf("\t\treserved = %s\n", nice);
1340fa9e4066Sahrens 	(void) printf("\t\tprops_zapobj = %llu\n",
1341fa9e4066Sahrens 	    (u_longlong_t)dd->dd_props_zapobj);
1342ecd6cf80Smarks 	(void) printf("\t\tdeleg_zapobj = %llu\n",
1343ecd6cf80Smarks 	    (u_longlong_t)dd->dd_deleg_zapobj);
134474e7dc98SMatthew Ahrens 	(void) printf("\t\tflags = %llx\n",
134574e7dc98SMatthew Ahrens 	    (u_longlong_t)dd->dd_flags);
134674e7dc98SMatthew Ahrens 
134774e7dc98SMatthew Ahrens #define	DO(which) \
1348*fc30d466SJason King 	zdb_nicenum(dd->dd_used_breakdown[DD_USED_ ## which], nice, \
1349*fc30d466SJason King 	    sizeof (nice)); \
135074e7dc98SMatthew Ahrens 	(void) printf("\t\tused_breakdown[" #which "] = %s\n", nice)
135174e7dc98SMatthew Ahrens 	DO(HEAD);
135274e7dc98SMatthew Ahrens 	DO(SNAP);
135374e7dc98SMatthew Ahrens 	DO(CHILD);
135474e7dc98SMatthew Ahrens 	DO(CHILD_RSRV);
135574e7dc98SMatthew Ahrens 	DO(REFRSRV);
135674e7dc98SMatthew Ahrens #undef DO
1357fa9e4066Sahrens }
1358fa9e4066Sahrens 
1359fa9e4066Sahrens /*ARGSUSED*/
1360fa9e4066Sahrens static void
1361fa9e4066Sahrens dump_dsl_dataset(objset_t *os, uint64_t object, void *data, size_t size)
1362fa9e4066Sahrens {
1363fa9e4066Sahrens 	dsl_dataset_phys_t *ds = data;
1364fa9e4066Sahrens 	time_t crtime;
13653f9d6ad7SLin Ling 	char used[32], compressed[32], uncompressed[32], unique[32];
1366fbabab8fSmaybee 	char blkbuf[BP_SPRINTF_LEN];
1367fa9e4066Sahrens 
1368*fc30d466SJason King 	/* make sure nicenum has enough space */
1369*fc30d466SJason King 	CTASSERT(sizeof (used) >= NN_NUMBUF_SZ);
1370*fc30d466SJason King 	CTASSERT(sizeof (compressed) >= NN_NUMBUF_SZ);
1371*fc30d466SJason King 	CTASSERT(sizeof (uncompressed) >= NN_NUMBUF_SZ);
1372*fc30d466SJason King 	CTASSERT(sizeof (unique) >= NN_NUMBUF_SZ);
1373*fc30d466SJason King 
1374fa9e4066Sahrens 	if (ds == NULL)
1375fa9e4066Sahrens 		return;
1376fa9e4066Sahrens 
1377fa9e4066Sahrens 	ASSERT(size == sizeof (*ds));
1378fa9e4066Sahrens 	crtime = ds->ds_creation_time;
1379*fc30d466SJason King 	zdb_nicenum(ds->ds_referenced_bytes, used, sizeof (used));
1380*fc30d466SJason King 	zdb_nicenum(ds->ds_compressed_bytes, compressed, sizeof (compressed));
1381*fc30d466SJason King 	zdb_nicenum(ds->ds_uncompressed_bytes, uncompressed,
1382*fc30d466SJason King 	    sizeof (uncompressed));
1383*fc30d466SJason King 	zdb_nicenum(ds->ds_unique_bytes, unique, sizeof (unique));
138443466aaeSMax Grossman 	snprintf_blkptr(blkbuf, sizeof (blkbuf), &ds->ds_bp);
1385fa9e4066Sahrens 
1386088f3894Sahrens 	(void) printf("\t\tdir_obj = %llu\n",
1387fa9e4066Sahrens 	    (u_longlong_t)ds->ds_dir_obj);
1388fa9e4066Sahrens 	(void) printf("\t\tprev_snap_obj = %llu\n",
1389fa9e4066Sahrens 	    (u_longlong_t)ds->ds_prev_snap_obj);
1390fa9e4066Sahrens 	(void) printf("\t\tprev_snap_txg = %llu\n",
1391fa9e4066Sahrens 	    (u_longlong_t)ds->ds_prev_snap_txg);
1392fa9e4066Sahrens 	(void) printf("\t\tnext_snap_obj = %llu\n",
1393fa9e4066Sahrens 	    (u_longlong_t)ds->ds_next_snap_obj);
1394fa9e4066Sahrens 	(void) printf("\t\tsnapnames_zapobj = %llu\n",
1395fa9e4066Sahrens 	    (u_longlong_t)ds->ds_snapnames_zapobj);
1396fa9e4066Sahrens 	(void) printf("\t\tnum_children = %llu\n",
1397fa9e4066Sahrens 	    (u_longlong_t)ds->ds_num_children);
1398842727c2SChris Kirby 	(void) printf("\t\tuserrefs_obj = %llu\n",
1399842727c2SChris Kirby 	    (u_longlong_t)ds->ds_userrefs_obj);
1400fa9e4066Sahrens 	(void) printf("\t\tcreation_time = %s", ctime(&crtime));
1401fa9e4066Sahrens 	(void) printf("\t\tcreation_txg = %llu\n",
1402fa9e4066Sahrens 	    (u_longlong_t)ds->ds_creation_txg);
1403fa9e4066Sahrens 	(void) printf("\t\tdeadlist_obj = %llu\n",
1404fa9e4066Sahrens 	    (u_longlong_t)ds->ds_deadlist_obj);
1405fa9e4066Sahrens 	(void) printf("\t\tused_bytes = %s\n", used);
1406fa9e4066Sahrens 	(void) printf("\t\tcompressed_bytes = %s\n", compressed);
1407fa9e4066Sahrens 	(void) printf("\t\tuncompressed_bytes = %s\n", uncompressed);
1408fa9e4066Sahrens 	(void) printf("\t\tunique = %s\n", unique);
1409fa9e4066Sahrens 	(void) printf("\t\tfsid_guid = %llu\n",
1410fa9e4066Sahrens 	    (u_longlong_t)ds->ds_fsid_guid);
1411fa9e4066Sahrens 	(void) printf("\t\tguid = %llu\n",
1412fa9e4066Sahrens 	    (u_longlong_t)ds->ds_guid);
141399653d4eSeschrock 	(void) printf("\t\tflags = %llx\n",
141499653d4eSeschrock 	    (u_longlong_t)ds->ds_flags);
1415088f3894Sahrens 	(void) printf("\t\tnext_clones_obj = %llu\n",
1416088f3894Sahrens 	    (u_longlong_t)ds->ds_next_clones_obj);
1417bb0ade09Sahrens 	(void) printf("\t\tprops_obj = %llu\n",
1418bb0ade09Sahrens 	    (u_longlong_t)ds->ds_props_obj);
1419fa9e4066Sahrens 	(void) printf("\t\tbp = %s\n", blkbuf);
1420fa9e4066Sahrens }
1421fa9e4066Sahrens 
1422cde58dbcSMatthew Ahrens /* ARGSUSED */
1423cde58dbcSMatthew Ahrens static int
1424ad135b5dSChristopher Siden dump_bptree_cb(void *arg, const blkptr_t *bp, dmu_tx_t *tx)
1425ad135b5dSChristopher Siden {
1426ad135b5dSChristopher Siden 	char blkbuf[BP_SPRINTF_LEN];
1427ad135b5dSChristopher Siden 
1428ad135b5dSChristopher Siden 	if (bp->blk_birth != 0) {
142943466aaeSMax Grossman 		snprintf_blkptr(blkbuf, sizeof (blkbuf), bp);
1430ad135b5dSChristopher Siden 		(void) printf("\t%s\n", blkbuf);
1431ad135b5dSChristopher Siden 	}
1432ad135b5dSChristopher Siden 	return (0);
1433ad135b5dSChristopher Siden }
1434ad135b5dSChristopher Siden 
1435ad135b5dSChristopher Siden static void
1436ad135b5dSChristopher Siden dump_bptree(objset_t *os, uint64_t obj, char *name)
1437ad135b5dSChristopher Siden {
1438ad135b5dSChristopher Siden 	char bytes[32];
1439ad135b5dSChristopher Siden 	bptree_phys_t *bt;
1440ad135b5dSChristopher Siden 	dmu_buf_t *db;
1441ad135b5dSChristopher Siden 
1442*fc30d466SJason King 	/* make sure nicenum has enough space */
1443*fc30d466SJason King 	CTASSERT(sizeof (bytes) >= NN_NUMBUF_SZ);
1444*fc30d466SJason King 
1445ad135b5dSChristopher Siden 	if (dump_opt['d'] < 3)
1446ad135b5dSChristopher Siden 		return;
1447ad135b5dSChristopher Siden 
1448b420f3adSRichard Lowe 	VERIFY3U(0, ==, dmu_bonus_hold(os, obj, FTAG, &db));
1449ad135b5dSChristopher Siden 	bt = db->db_data;
1450*fc30d466SJason King 	zdb_nicenum(bt->bt_bytes, bytes, sizeof (bytes));
1451ad135b5dSChristopher Siden 	(void) printf("\n    %s: %llu datasets, %s\n",
1452ad135b5dSChristopher Siden 	    name, (unsigned long long)(bt->bt_end - bt->bt_begin), bytes);
1453ad135b5dSChristopher Siden 	dmu_buf_rele(db, FTAG);
1454ad135b5dSChristopher Siden 
1455ad135b5dSChristopher Siden 	if (dump_opt['d'] < 5)
1456ad135b5dSChristopher Siden 		return;
1457ad135b5dSChristopher Siden 
1458ad135b5dSChristopher Siden 	(void) printf("\n");
1459ad135b5dSChristopher Siden 
1460ad135b5dSChristopher Siden 	(void) bptree_iterate(os, obj, B_FALSE, dump_bptree_cb, NULL, NULL);
1461ad135b5dSChristopher Siden }
1462ad135b5dSChristopher Siden 
1463ad135b5dSChristopher Siden /* ARGSUSED */
1464ad135b5dSChristopher Siden static int
1465cde58dbcSMatthew Ahrens dump_bpobj_cb(void *arg, const blkptr_t *bp, dmu_tx_t *tx)
1466fa9e4066Sahrens {
1467cde58dbcSMatthew Ahrens 	char blkbuf[BP_SPRINTF_LEN];
1468cde58dbcSMatthew Ahrens 
1469cde58dbcSMatthew Ahrens 	ASSERT(bp->blk_birth != 0);
147043466aaeSMax Grossman 	snprintf_blkptr_compact(blkbuf, sizeof (blkbuf), bp);
1471cde58dbcSMatthew Ahrens 	(void) printf("\t%s\n", blkbuf);
1472cde58dbcSMatthew Ahrens 	return (0);
1473cde58dbcSMatthew Ahrens }
1474cde58dbcSMatthew Ahrens 
1475cde58dbcSMatthew Ahrens static void
1476732885fcSMatthew Ahrens dump_full_bpobj(bpobj_t *bpo, char *name, int indent)
1477cde58dbcSMatthew Ahrens {
14783f9d6ad7SLin Ling 	char bytes[32];
14793f9d6ad7SLin Ling 	char comp[32];
14803f9d6ad7SLin Ling 	char uncomp[32];
1481fa9e4066Sahrens 
1482*fc30d466SJason King 	/* make sure nicenum has enough space */
1483*fc30d466SJason King 	CTASSERT(sizeof (bytes) >= NN_NUMBUF_SZ);
1484*fc30d466SJason King 	CTASSERT(sizeof (comp) >= NN_NUMBUF_SZ);
1485*fc30d466SJason King 	CTASSERT(sizeof (uncomp) >= NN_NUMBUF_SZ);
1486*fc30d466SJason King 
1487fa9e4066Sahrens 	if (dump_opt['d'] < 3)
1488fa9e4066Sahrens 		return;
1489fa9e4066Sahrens 
1490*fc30d466SJason King 	zdb_nicenum(bpo->bpo_phys->bpo_bytes, bytes, sizeof (bytes));
1491d0475637SMatthew Ahrens 	if (bpo->bpo_havesubobj && bpo->bpo_phys->bpo_subobjs != 0) {
1492*fc30d466SJason King 		zdb_nicenum(bpo->bpo_phys->bpo_comp, comp, sizeof (comp));
1493*fc30d466SJason King 		zdb_nicenum(bpo->bpo_phys->bpo_uncomp, uncomp, sizeof (uncomp));
1494d0475637SMatthew Ahrens 		(void) printf("    %*s: object %llu, %llu local blkptrs, "
1495732885fcSMatthew Ahrens 		    "%llu subobjs in object %llu, %s (%s/%s comp)\n",
1496d0475637SMatthew Ahrens 		    indent * 8, name,
1497d0475637SMatthew Ahrens 		    (u_longlong_t)bpo->bpo_object,
1498d0475637SMatthew Ahrens 		    (u_longlong_t)bpo->bpo_phys->bpo_num_blkptrs,
1499cde58dbcSMatthew Ahrens 		    (u_longlong_t)bpo->bpo_phys->bpo_num_subobjs,
1500732885fcSMatthew Ahrens 		    (u_longlong_t)bpo->bpo_phys->bpo_subobjs,
150199653d4eSeschrock 		    bytes, comp, uncomp);
1502d0475637SMatthew Ahrens 
1503d0475637SMatthew Ahrens 		for (uint64_t i = 0; i < bpo->bpo_phys->bpo_num_subobjs; i++) {
1504d0475637SMatthew Ahrens 			uint64_t subobj;
1505d0475637SMatthew Ahrens 			bpobj_t subbpo;
1506d0475637SMatthew Ahrens 			int error;
1507d0475637SMatthew Ahrens 			VERIFY0(dmu_read(bpo->bpo_os,
1508d0475637SMatthew Ahrens 			    bpo->bpo_phys->bpo_subobjs,
1509d0475637SMatthew Ahrens 			    i * sizeof (subobj), sizeof (subobj), &subobj, 0));
1510d0475637SMatthew Ahrens 			error = bpobj_open(&subbpo, bpo->bpo_os, subobj);
1511d0475637SMatthew Ahrens 			if (error != 0) {
1512d0475637SMatthew Ahrens 				(void) printf("ERROR %u while trying to open "
1513d0475637SMatthew Ahrens 				    "subobj id %llu\n",
1514d0475637SMatthew Ahrens 				    error, (u_longlong_t)subobj);
1515d0475637SMatthew Ahrens 				continue;
1516d0475637SMatthew Ahrens 			}
1517732885fcSMatthew Ahrens 			dump_full_bpobj(&subbpo, "subobj", indent + 1);
151877061867SMatthew Ahrens 			bpobj_close(&subbpo);
1519d0475637SMatthew Ahrens 		}
152099653d4eSeschrock 	} else {
1521d0475637SMatthew Ahrens 		(void) printf("    %*s: object %llu, %llu blkptrs, %s\n",
1522d0475637SMatthew Ahrens 		    indent * 8, name,
1523d0475637SMatthew Ahrens 		    (u_longlong_t)bpo->bpo_object,
1524d0475637SMatthew Ahrens 		    (u_longlong_t)bpo->bpo_phys->bpo_num_blkptrs,
1525d0475637SMatthew Ahrens 		    bytes);
152699653d4eSeschrock 	}
1527fa9e4066Sahrens 
1528cde58dbcSMatthew Ahrens 	if (dump_opt['d'] < 5)
1529fa9e4066Sahrens 		return;
1530fa9e4066Sahrens 
1531fa9e4066Sahrens 
1532d0475637SMatthew Ahrens 	if (indent == 0) {
1533cde58dbcSMatthew Ahrens 		(void) bpobj_iterate_nofree(bpo, dump_bpobj_cb, NULL, NULL);
1534d0475637SMatthew Ahrens 		(void) printf("\n");
1535d0475637SMatthew Ahrens 	}
1536fa9e4066Sahrens }
1537fa9e4066Sahrens 
1538cde58dbcSMatthew Ahrens static void
1539cde58dbcSMatthew Ahrens dump_deadlist(dsl_deadlist_t *dl)
1540cde58dbcSMatthew Ahrens {
1541cde58dbcSMatthew Ahrens 	dsl_deadlist_entry_t *dle;
1542d0475637SMatthew Ahrens 	uint64_t unused;
1543cde58dbcSMatthew Ahrens 	char bytes[32];
1544cde58dbcSMatthew Ahrens 	char comp[32];
1545cde58dbcSMatthew Ahrens 	char uncomp[32];
1546cde58dbcSMatthew Ahrens 
1547*fc30d466SJason King 	/* make sure nicenum has enough space */
1548*fc30d466SJason King 	CTASSERT(sizeof (bytes) >= NN_NUMBUF_SZ);
1549*fc30d466SJason King 	CTASSERT(sizeof (comp) >= NN_NUMBUF_SZ);
1550*fc30d466SJason King 	CTASSERT(sizeof (uncomp) >= NN_NUMBUF_SZ);
1551*fc30d466SJason King 
1552cde58dbcSMatthew Ahrens 	if (dump_opt['d'] < 3)
1553cde58dbcSMatthew Ahrens 		return;
1554cde58dbcSMatthew Ahrens 
155590c76c66SMatthew Ahrens 	if (dl->dl_oldfmt) {
1556732885fcSMatthew Ahrens 		dump_full_bpobj(&dl->dl_bpobj, "old-format deadlist", 0);
155790c76c66SMatthew Ahrens 		return;
155890c76c66SMatthew Ahrens 	}
155990c76c66SMatthew Ahrens 
1560*fc30d466SJason King 	zdb_nicenum(dl->dl_phys->dl_used, bytes, sizeof (bytes));
1561*fc30d466SJason King 	zdb_nicenum(dl->dl_phys->dl_comp, comp, sizeof (comp));
1562*fc30d466SJason King 	zdb_nicenum(dl->dl_phys->dl_uncomp, uncomp, sizeof (uncomp));
1563cde58dbcSMatthew Ahrens 	(void) printf("\n    Deadlist: %s (%s/%s comp)\n",
1564cde58dbcSMatthew Ahrens 	    bytes, comp, uncomp);
1565cde58dbcSMatthew Ahrens 
1566cde58dbcSMatthew Ahrens 	if (dump_opt['d'] < 4)
1567cde58dbcSMatthew Ahrens 		return;
1568cde58dbcSMatthew Ahrens 
1569cde58dbcSMatthew Ahrens 	(void) printf("\n");
1570cde58dbcSMatthew Ahrens 
1571d0475637SMatthew Ahrens 	/* force the tree to be loaded */
1572d0475637SMatthew Ahrens 	dsl_deadlist_space_range(dl, 0, UINT64_MAX, &unused, &unused, &unused);
1573d0475637SMatthew Ahrens 
1574cde58dbcSMatthew Ahrens 	for (dle = avl_first(&dl->dl_tree); dle;
1575cde58dbcSMatthew Ahrens 	    dle = AVL_NEXT(&dl->dl_tree, dle)) {
1576d0475637SMatthew Ahrens 		if (dump_opt['d'] >= 5) {
1577d0475637SMatthew Ahrens 			char buf[128];
1578d0475637SMatthew Ahrens 			(void) snprintf(buf, sizeof (buf), "mintxg %llu -> ",
1579d0475637SMatthew Ahrens 			    (longlong_t)dle->dle_mintxg,
1580d0475637SMatthew Ahrens 			    (longlong_t)dle->dle_bpobj.bpo_object);
1581d0475637SMatthew Ahrens 
1582732885fcSMatthew Ahrens 			dump_full_bpobj(&dle->dle_bpobj, buf, 0);
1583d0475637SMatthew Ahrens 		} else {
1584cde58dbcSMatthew Ahrens 			(void) printf("mintxg %llu -> obj %llu\n",
1585cde58dbcSMatthew Ahrens 			    (longlong_t)dle->dle_mintxg,
1586cde58dbcSMatthew Ahrens 			    (longlong_t)dle->dle_bpobj.bpo_object);
1587cde58dbcSMatthew Ahrens 
1588d0475637SMatthew Ahrens 		}
1589cde58dbcSMatthew Ahrens 	}
1590fa9e4066Sahrens }
1591fa9e4066Sahrens 
1592e0d35c44Smarks static avl_tree_t idx_tree;
1593e0d35c44Smarks static avl_tree_t domain_tree;
1594e0d35c44Smarks static boolean_t fuid_table_loaded;
15950a586ceaSMark Shellenbaum static boolean_t sa_loaded;
15960a586ceaSMark Shellenbaum sa_attr_type_t *sa_attr_table;
1597e0d35c44Smarks 
1598e0d35c44Smarks static void
1599e0d35c44Smarks fuid_table_destroy()
1600e0d35c44Smarks {
1601e0d35c44Smarks 	if (fuid_table_loaded) {
1602e0d35c44Smarks 		zfs_fuid_table_destroy(&idx_tree, &domain_tree);
1603e0d35c44Smarks 		fuid_table_loaded = B_FALSE;
1604e0d35c44Smarks 	}
1605e0d35c44Smarks }
1606e0d35c44Smarks 
1607e0d35c44Smarks /*
1608e0d35c44Smarks  * print uid or gid information.
1609e0d35c44Smarks  * For normal POSIX id just the id is printed in decimal format.
1610e0d35c44Smarks  * For CIFS files with FUID the fuid is printed in hex followed by
1611d0475637SMatthew Ahrens  * the domain-rid string.
1612e0d35c44Smarks  */
1613e0d35c44Smarks static void
1614e0d35c44Smarks print_idstr(uint64_t id, const char *id_type)
1615e0d35c44Smarks {
1616e0d35c44Smarks 	if (FUID_INDEX(id)) {
1617e0d35c44Smarks 		char *domain;
1618e0d35c44Smarks 
1619e0d35c44Smarks 		domain = zfs_fuid_idx_domain(&idx_tree, FUID_INDEX(id));
1620e0d35c44Smarks 		(void) printf("\t%s     %llx [%s-%d]\n", id_type,
1621e0d35c44Smarks 		    (u_longlong_t)id, domain, (int)FUID_RID(id));
1622e0d35c44Smarks 	} else {
1623e0d35c44Smarks 		(void) printf("\t%s     %llu\n", id_type, (u_longlong_t)id);
1624e0d35c44Smarks 	}
1625e0d35c44Smarks 
1626e0d35c44Smarks }
1627e0d35c44Smarks 
1628e0d35c44Smarks static void
16290a586ceaSMark Shellenbaum dump_uidgid(objset_t *os, uint64_t uid, uint64_t gid)
1630e0d35c44Smarks {
1631e0d35c44Smarks 	uint32_t uid_idx, gid_idx;
1632e0d35c44Smarks 
16330a586ceaSMark Shellenbaum 	uid_idx = FUID_INDEX(uid);
16340a586ceaSMark Shellenbaum 	gid_idx = FUID_INDEX(gid);
1635e0d35c44Smarks 
1636e0d35c44Smarks 	/* Load domain table, if not already loaded */
1637e0d35c44Smarks 	if (!fuid_table_loaded && (uid_idx || gid_idx)) {
1638e0d35c44Smarks 		uint64_t fuid_obj;
1639e0d35c44Smarks 
1640e0d35c44Smarks 		/* first find the fuid object.  It lives in the master node */
1641e0d35c44Smarks 		VERIFY(zap_lookup(os, MASTER_NODE_OBJ, ZFS_FUID_TABLES,
1642e0d35c44Smarks 		    8, 1, &fuid_obj) == 0);
164389459e17SMark Shellenbaum 		zfs_fuid_avl_tree_create(&idx_tree, &domain_tree);
1644e0d35c44Smarks 		(void) zfs_fuid_table_load(os, fuid_obj,
1645e0d35c44Smarks 		    &idx_tree, &domain_tree);
1646e0d35c44Smarks 		fuid_table_loaded = B_TRUE;
1647e0d35c44Smarks 	}
1648e0d35c44Smarks 
16490a586ceaSMark Shellenbaum 	print_idstr(uid, "uid");
16500a586ceaSMark Shellenbaum 	print_idstr(gid, "gid");
1651e0d35c44Smarks }
1652e0d35c44Smarks 
1653fa9e4066Sahrens /*ARGSUSED*/
1654fa9e4066Sahrens static void
1655fa9e4066Sahrens dump_znode(objset_t *os, uint64_t object, void *data, size_t size)
1656fa9e4066Sahrens {
1657fa9e4066Sahrens 	char path[MAXPATHLEN * 2];	/* allow for xattr and failure prefix */
16580a586ceaSMark Shellenbaum 	sa_handle_t *hdl;
16590a586ceaSMark Shellenbaum 	uint64_t xattr, rdev, gen;
16600a586ceaSMark Shellenbaum 	uint64_t uid, gid, mode, fsize, parent, links;
16618f2529deSMark Shellenbaum 	uint64_t pflags;
16620a586ceaSMark Shellenbaum 	uint64_t acctm[2], modtm[2], chgtm[2], crtm[2];
16630a586ceaSMark Shellenbaum 	time_t z_crtime, z_atime, z_mtime, z_ctime;
16648f2529deSMark Shellenbaum 	sa_bulk_attr_t bulk[12];
16650a586ceaSMark Shellenbaum 	int idx = 0;
166655434c77Sek110237 	int error;
1667fa9e4066Sahrens 
16680a586ceaSMark Shellenbaum 	if (!sa_loaded) {
16690a586ceaSMark Shellenbaum 		uint64_t sa_attrs = 0;
16700a586ceaSMark Shellenbaum 		uint64_t version;
16710a586ceaSMark Shellenbaum 
16720a586ceaSMark Shellenbaum 		VERIFY(zap_lookup(os, MASTER_NODE_OBJ, ZPL_VERSION_STR,
16730a586ceaSMark Shellenbaum 		    8, 1, &version) == 0);
16740a586ceaSMark Shellenbaum 		if (version >= ZPL_VERSION_SA) {
16750a586ceaSMark Shellenbaum 			VERIFY(zap_lookup(os, MASTER_NODE_OBJ, ZFS_SA_ATTRS,
16760a586ceaSMark Shellenbaum 			    8, 1, &sa_attrs) == 0);
16770a586ceaSMark Shellenbaum 		}
16781d8ccc7bSMark Shellenbaum 		if ((error = sa_setup(os, sa_attrs, zfs_attr_table,
16791d8ccc7bSMark Shellenbaum 		    ZPL_END, &sa_attr_table)) != 0) {
16801d8ccc7bSMark Shellenbaum 			(void) printf("sa_setup failed errno %d, can't "
16811d8ccc7bSMark Shellenbaum 			    "display znode contents\n", error);
16821d8ccc7bSMark Shellenbaum 			return;
16831d8ccc7bSMark Shellenbaum 		}
16840a586ceaSMark Shellenbaum 		sa_loaded = B_TRUE;
16850a586ceaSMark Shellenbaum 	}
16860a586ceaSMark Shellenbaum 
16870a586ceaSMark Shellenbaum 	if (sa_handle_get(os, object, NULL, SA_HDL_PRIVATE, &hdl)) {
16880a586ceaSMark Shellenbaum 		(void) printf("Failed to get handle for SA znode\n");
16890a586ceaSMark Shellenbaum 		return;
16900a586ceaSMark Shellenbaum 	}
16910a586ceaSMark Shellenbaum 
16920a586ceaSMark Shellenbaum 	SA_ADD_BULK_ATTR(bulk, idx, sa_attr_table[ZPL_UID], NULL, &uid, 8);
16930a586ceaSMark Shellenbaum 	SA_ADD_BULK_ATTR(bulk, idx, sa_attr_table[ZPL_GID], NULL, &gid, 8);
16940a586ceaSMark Shellenbaum 	SA_ADD_BULK_ATTR(bulk, idx, sa_attr_table[ZPL_LINKS], NULL,
16950a586ceaSMark Shellenbaum 	    &links, 8);
16960a586ceaSMark Shellenbaum 	SA_ADD_BULK_ATTR(bulk, idx, sa_attr_table[ZPL_GEN], NULL, &gen, 8);
16970a586ceaSMark Shellenbaum 	SA_ADD_BULK_ATTR(bulk, idx, sa_attr_table[ZPL_MODE], NULL,
16980a586ceaSMark Shellenbaum 	    &mode, 8);
16990a586ceaSMark Shellenbaum 	SA_ADD_BULK_ATTR(bulk, idx, sa_attr_table[ZPL_PARENT],
17000a586ceaSMark Shellenbaum 	    NULL, &parent, 8);
17010a586ceaSMark Shellenbaum 	SA_ADD_BULK_ATTR(bulk, idx, sa_attr_table[ZPL_SIZE], NULL,
17020a586ceaSMark Shellenbaum 	    &fsize, 8);
17030a586ceaSMark Shellenbaum 	SA_ADD_BULK_ATTR(bulk, idx, sa_attr_table[ZPL_ATIME], NULL,
17040a586ceaSMark Shellenbaum 	    acctm, 16);
17050a586ceaSMark Shellenbaum 	SA_ADD_BULK_ATTR(bulk, idx, sa_attr_table[ZPL_MTIME], NULL,
17060a586ceaSMark Shellenbaum 	    modtm, 16);
17070a586ceaSMark Shellenbaum 	SA_ADD_BULK_ATTR(bulk, idx, sa_attr_table[ZPL_CRTIME], NULL,
17080a586ceaSMark Shellenbaum 	    crtm, 16);
17090a586ceaSMark Shellenbaum 	SA_ADD_BULK_ATTR(bulk, idx, sa_attr_table[ZPL_CTIME], NULL,
17100a586ceaSMark Shellenbaum 	    chgtm, 16);
17118f2529deSMark Shellenbaum 	SA_ADD_BULK_ATTR(bulk, idx, sa_attr_table[ZPL_FLAGS], NULL,
17128f2529deSMark Shellenbaum 	    &pflags, 8);
17130a586ceaSMark Shellenbaum 
17140a586ceaSMark Shellenbaum 	if (sa_bulk_lookup(hdl, bulk, idx)) {
17150a586ceaSMark Shellenbaum 		(void) sa_handle_destroy(hdl);
17160a586ceaSMark Shellenbaum 		return;
17170a586ceaSMark Shellenbaum 	}
1718fa9e4066Sahrens 
171955434c77Sek110237 	error = zfs_obj_to_path(os, object, path, sizeof (path));
172055434c77Sek110237 	if (error != 0) {
172155434c77Sek110237 		(void) snprintf(path, sizeof (path), "\?\?\?<object#%llu>",
172255434c77Sek110237 		    (u_longlong_t)object);
172355434c77Sek110237 	}
1724fa9e4066Sahrens 	if (dump_opt['d'] < 3) {
172555434c77Sek110237 		(void) printf("\t%s\n", path);
17260a586ceaSMark Shellenbaum 		(void) sa_handle_destroy(hdl);
1727fa9e4066Sahrens 		return;
1728fa9e4066Sahrens 	}
1729fa9e4066Sahrens 
17300a586ceaSMark Shellenbaum 	z_crtime = (time_t)crtm[0];
17310a586ceaSMark Shellenbaum 	z_atime = (time_t)acctm[0];
17320a586ceaSMark Shellenbaum 	z_mtime = (time_t)modtm[0];
17330a586ceaSMark Shellenbaum 	z_ctime = (time_t)chgtm[0];
1734fa9e4066Sahrens 
173555434c77Sek110237 	(void) printf("\tpath	%s\n", path);
17360a586ceaSMark Shellenbaum 	dump_uidgid(os, uid, gid);
1737fa9e4066Sahrens 	(void) printf("\tatime	%s", ctime(&z_atime));
1738fa9e4066Sahrens 	(void) printf("\tmtime	%s", ctime(&z_mtime));
1739fa9e4066Sahrens 	(void) printf("\tctime	%s", ctime(&z_ctime));
1740fa9e4066Sahrens 	(void) printf("\tcrtime	%s", ctime(&z_crtime));
17410a586ceaSMark Shellenbaum 	(void) printf("\tgen	%llu\n", (u_longlong_t)gen);
17420a586ceaSMark Shellenbaum 	(void) printf("\tmode	%llo\n", (u_longlong_t)mode);
17430a586ceaSMark Shellenbaum 	(void) printf("\tsize	%llu\n", (u_longlong_t)fsize);
17440a586ceaSMark Shellenbaum 	(void) printf("\tparent	%llu\n", (u_longlong_t)parent);
17450a586ceaSMark Shellenbaum 	(void) printf("\tlinks	%llu\n", (u_longlong_t)links);
17468f2529deSMark Shellenbaum 	(void) printf("\tpflags	%llx\n", (u_longlong_t)pflags);
17470a586ceaSMark Shellenbaum 	if (sa_lookup(hdl, sa_attr_table[ZPL_XATTR], &xattr,
17480a586ceaSMark Shellenbaum 	    sizeof (uint64_t)) == 0)
17490a586ceaSMark Shellenbaum 		(void) printf("\txattr	%llu\n", (u_longlong_t)xattr);
17500a586ceaSMark Shellenbaum 	if (sa_lookup(hdl, sa_attr_table[ZPL_RDEV], &rdev,
17510a586ceaSMark Shellenbaum 	    sizeof (uint64_t)) == 0)
17520a586ceaSMark Shellenbaum 		(void) printf("\trdev	0x%016llx\n", (u_longlong_t)rdev);
17530a586ceaSMark Shellenbaum 	sa_handle_destroy(hdl);
1754fa9e4066Sahrens }
1755fa9e4066Sahrens 
1756fa9e4066Sahrens /*ARGSUSED*/
1757fa9e4066Sahrens static void
1758fa9e4066Sahrens dump_acl(objset_t *os, uint64_t object, void *data, size_t size)
1759fa9e4066Sahrens {
1760fa9e4066Sahrens }
1761fa9e4066Sahrens 
1762fa9e4066Sahrens /*ARGSUSED*/
1763fa9e4066Sahrens static void
1764fa9e4066Sahrens dump_dmu_objset(objset_t *os, uint64_t object, void *data, size_t size)
1765fa9e4066Sahrens {
1766fa9e4066Sahrens }
1767fa9e4066Sahrens 
17686de8f417SVictor Latushkin static object_viewer_t *object_viewer[DMU_OT_NUMTYPES + 1] = {
1769fa9e4066Sahrens 	dump_none,		/* unallocated			*/
1770fa9e4066Sahrens 	dump_zap,		/* object directory		*/
1771fa9e4066Sahrens 	dump_uint64,		/* object array			*/
1772fa9e4066Sahrens 	dump_none,		/* packed nvlist		*/
1773fa9e4066Sahrens 	dump_packed_nvlist,	/* packed nvlist size		*/
1774732885fcSMatthew Ahrens 	dump_none,		/* bpobj			*/
1775732885fcSMatthew Ahrens 	dump_bpobj,		/* bpobj header			*/
1776fa9e4066Sahrens 	dump_none,		/* SPA space map header		*/
1777fa9e4066Sahrens 	dump_none,		/* SPA space map		*/
1778fa9e4066Sahrens 	dump_none,		/* ZIL intent log		*/
1779fa9e4066Sahrens 	dump_dnode,		/* DMU dnode			*/
1780fa9e4066Sahrens 	dump_dmu_objset,	/* DMU objset			*/
1781fa9e4066Sahrens 	dump_dsl_dir,		/* DSL directory		*/
1782fa9e4066Sahrens 	dump_zap,		/* DSL directory child map	*/
1783fa9e4066Sahrens 	dump_zap,		/* DSL dataset snap map		*/
1784fa9e4066Sahrens 	dump_zap,		/* DSL props			*/
1785fa9e4066Sahrens 	dump_dsl_dataset,	/* DSL dataset			*/
1786fa9e4066Sahrens 	dump_znode,		/* ZFS znode			*/
1787da6c28aaSamw 	dump_acl,		/* ZFS V0 ACL			*/
1788fa9e4066Sahrens 	dump_uint8,		/* ZFS plain file		*/
1789e7437265Sahrens 	dump_zpldir,		/* ZFS directory		*/
1790fa9e4066Sahrens 	dump_zap,		/* ZFS master node		*/
1791fa9e4066Sahrens 	dump_zap,		/* ZFS delete queue		*/
1792fa9e4066Sahrens 	dump_uint8,		/* zvol object			*/
1793fa9e4066Sahrens 	dump_zap,		/* zvol prop			*/
1794fa9e4066Sahrens 	dump_uint8,		/* other uint8[]		*/
1795fa9e4066Sahrens 	dump_uint64,		/* other uint64[]		*/
1796fa9e4066Sahrens 	dump_zap,		/* other ZAP			*/
1797ea8dc4b6Seschrock 	dump_zap,		/* persistent error log		*/
179806eeb2adSek110237 	dump_uint8,		/* SPA history			*/
17994445fffbSMatthew Ahrens 	dump_history_offsets,	/* SPA history offsets		*/
1800b1b8ab34Slling 	dump_zap,		/* Pool properties		*/
1801ecd6cf80Smarks 	dump_zap,		/* DSL permissions		*/
1802da6c28aaSamw 	dump_acl,		/* ZFS ACL			*/
1803da6c28aaSamw 	dump_uint8,		/* ZFS SYSACL			*/
1804da6c28aaSamw 	dump_none,		/* FUID nvlist			*/
1805da6c28aaSamw 	dump_packed_nvlist,	/* FUID nvlist size		*/
1806088f3894Sahrens 	dump_zap,		/* DSL dataset next clones	*/
1807088f3894Sahrens 	dump_zap,		/* DSL scrub queue		*/
180814843421SMatthew Ahrens 	dump_zap,		/* ZFS user/group used		*/
180914843421SMatthew Ahrens 	dump_zap,		/* ZFS user/group quota		*/
1810842727c2SChris Kirby 	dump_zap,		/* snapshot refcount tags	*/
1811486ae710SMatthew Ahrens 	dump_ddt_zap,		/* DDT ZAP object		*/
1812b24ab676SJeff Bonwick 	dump_zap,		/* DDT statistics		*/
18130a586ceaSMark Shellenbaum 	dump_znode,		/* SA object			*/
18140a586ceaSMark Shellenbaum 	dump_zap,		/* SA Master Node		*/
18150a586ceaSMark Shellenbaum 	dump_sa_attrs,		/* SA attribute registration	*/
18160a586ceaSMark Shellenbaum 	dump_sa_layouts,	/* SA attribute layouts		*/
18173f9d6ad7SLin Ling 	dump_zap,		/* DSL scrub translations	*/
18183f9d6ad7SLin Ling 	dump_none,		/* fake dedup BP		*/
1819cde58dbcSMatthew Ahrens 	dump_zap,		/* deadlist			*/
1820cde58dbcSMatthew Ahrens 	dump_none,		/* deadlist hdr			*/
1821cde58dbcSMatthew Ahrens 	dump_zap,		/* dsl clones			*/
1822732885fcSMatthew Ahrens 	dump_bpobj_subobjs,	/* bpobj subobjs		*/
18230a586ceaSMark Shellenbaum 	dump_unknown,		/* Unknown type, must be last	*/
1824fa9e4066Sahrens };
1825fa9e4066Sahrens 
1826fa9e4066Sahrens static void
1827fa9e4066Sahrens dump_object(objset_t *os, uint64_t object, int verbosity, int *print_header)
1828fa9e4066Sahrens {
1829fa9e4066Sahrens 	dmu_buf_t *db = NULL;
1830fa9e4066Sahrens 	dmu_object_info_t doi;
1831fa9e4066Sahrens 	dnode_t *dn;
1832fa9e4066Sahrens 	void *bonus = NULL;
1833fa9e4066Sahrens 	size_t bsize = 0;
18343f9d6ad7SLin Ling 	char iblk[32], dblk[32], lsize[32], asize[32], fill[32];
18353f9d6ad7SLin Ling 	char bonus_size[32];
1836fa9e4066Sahrens 	char aux[50];
1837fa9e4066Sahrens 	int error;
1838fa9e4066Sahrens 
1839*fc30d466SJason King 	/* make sure nicenum has enough space */
1840*fc30d466SJason King 	CTASSERT(sizeof (iblk) >= NN_NUMBUF_SZ);
1841*fc30d466SJason King 	CTASSERT(sizeof (dblk) >= NN_NUMBUF_SZ);
1842*fc30d466SJason King 	CTASSERT(sizeof (lsize) >= NN_NUMBUF_SZ);
1843*fc30d466SJason King 	CTASSERT(sizeof (asize) >= NN_NUMBUF_SZ);
1844*fc30d466SJason King 	CTASSERT(sizeof (bonus_size) >= NN_NUMBUF_SZ);
1845*fc30d466SJason King 
1846fa9e4066Sahrens 	if (*print_header) {
1847b24ab676SJeff Bonwick 		(void) printf("\n%10s  %3s  %5s  %5s  %5s  %5s  %6s  %s\n",
1848b24ab676SJeff Bonwick 		    "Object", "lvl", "iblk", "dblk", "dsize", "lsize",
1849b24ab676SJeff Bonwick 		    "%full", "type");
1850fa9e4066Sahrens 		*print_header = 0;
1851fa9e4066Sahrens 	}
1852fa9e4066Sahrens 
1853fa9e4066Sahrens 	if (object == 0) {
1854744947dcSTom Erickson 		dn = DMU_META_DNODE(os);
1855fa9e4066Sahrens 	} else {
1856ea8dc4b6Seschrock 		error = dmu_bonus_hold(os, object, FTAG, &db);
1857ea8dc4b6Seschrock 		if (error)
1858ea8dc4b6Seschrock 			fatal("dmu_bonus_hold(%llu) failed, errno %u",
1859ea8dc4b6Seschrock 			    object, error);
1860fa9e4066Sahrens 		bonus = db->db_data;
1861fa9e4066Sahrens 		bsize = db->db_size;
1862744947dcSTom Erickson 		dn = DB_DNODE((dmu_buf_impl_t *)db);
1863fa9e4066Sahrens 	}
1864fa9e4066Sahrens 	dmu_object_info_from_dnode(dn, &doi);
1865fa9e4066Sahrens 
1866*fc30d466SJason King 	zdb_nicenum(doi.doi_metadata_block_size, iblk, sizeof (iblk));
1867*fc30d466SJason King 	zdb_nicenum(doi.doi_data_block_size, dblk, sizeof (dblk));
1868*fc30d466SJason King 	zdb_nicenum(doi.doi_max_offset, lsize, sizeof (lsize));
1869*fc30d466SJason King 	zdb_nicenum(doi.doi_physical_blocks_512 << 9, asize, sizeof (asize));
1870*fc30d466SJason King 	zdb_nicenum(doi.doi_bonus_size, bonus_size, sizeof (bonus_size));
1871b24ab676SJeff Bonwick 	(void) sprintf(fill, "%6.2f", 100.0 * doi.doi_fill_count *
1872bbfd46c4SJeff Bonwick 	    doi.doi_data_block_size / (object == 0 ? DNODES_PER_BLOCK : 1) /
1873bbfd46c4SJeff Bonwick 	    doi.doi_max_offset);
1874fa9e4066Sahrens 
1875fa9e4066Sahrens 	aux[0] = '\0';
1876fa9e4066Sahrens 
1877e7437265Sahrens 	if (doi.doi_checksum != ZIO_CHECKSUM_INHERIT || verbosity >= 6) {
1878fa9e4066Sahrens 		(void) snprintf(aux + strlen(aux), sizeof (aux), " (K=%s)",
18796de8f417SVictor Latushkin 		    ZDB_CHECKSUM_NAME(doi.doi_checksum));
1880e7437265Sahrens 	}
1881fa9e4066Sahrens 
1882e7437265Sahrens 	if (doi.doi_compress != ZIO_COMPRESS_INHERIT || verbosity >= 6) {
1883fa9e4066Sahrens 		(void) snprintf(aux + strlen(aux), sizeof (aux), " (Z=%s)",
18846de8f417SVictor Latushkin 		    ZDB_COMPRESS_NAME(doi.doi_compress));
1885e7437265Sahrens 	}
1886fa9e4066Sahrens 
1887b24ab676SJeff Bonwick 	(void) printf("%10lld  %3u  %5s  %5s  %5s  %5s  %6s  %s%s\n",
1888b24ab676SJeff Bonwick 	    (u_longlong_t)object, doi.doi_indirection, iblk, dblk,
1889b24ab676SJeff Bonwick 	    asize, lsize, fill, ZDB_OT_NAME(doi.doi_type), aux);
1890fa9e4066Sahrens 
1891fa9e4066Sahrens 	if (doi.doi_bonus_type != DMU_OT_NONE && verbosity > 3) {
1892b24ab676SJeff Bonwick 		(void) printf("%10s  %3s  %5s  %5s  %5s  %5s  %6s  %s\n",
1893b24ab676SJeff Bonwick 		    "", "", "", "", "", bonus_size, "bonus",
18946de8f417SVictor Latushkin 		    ZDB_OT_NAME(doi.doi_bonus_type));
1895fa9e4066Sahrens 	}
1896fa9e4066Sahrens 
1897fa9e4066Sahrens 	if (verbosity >= 4) {
18980a586ceaSMark Shellenbaum 		(void) printf("\tdnode flags: %s%s%s\n",
189914843421SMatthew Ahrens 		    (dn->dn_phys->dn_flags & DNODE_FLAG_USED_BYTES) ?
190014843421SMatthew Ahrens 		    "USED_BYTES " : "",
190114843421SMatthew Ahrens 		    (dn->dn_phys->dn_flags & DNODE_FLAG_USERUSED_ACCOUNTED) ?
19020a586ceaSMark Shellenbaum 		    "USERUSED_ACCOUNTED " : "",
19030a586ceaSMark Shellenbaum 		    (dn->dn_phys->dn_flags & DNODE_FLAG_SPILL_BLKPTR) ?
19040a586ceaSMark Shellenbaum 		    "SPILL_BLKPTR" : "");
190514843421SMatthew Ahrens 		(void) printf("\tdnode maxblkid: %llu\n",
190614843421SMatthew Ahrens 		    (longlong_t)dn->dn_phys->dn_maxblkid);
190714843421SMatthew Ahrens 
19086de8f417SVictor Latushkin 		object_viewer[ZDB_OT_TYPE(doi.doi_bonus_type)](os, object,
19096de8f417SVictor Latushkin 		    bonus, bsize);
19106de8f417SVictor Latushkin 		object_viewer[ZDB_OT_TYPE(doi.doi_type)](os, object, NULL, 0);
1911fa9e4066Sahrens 		*print_header = 1;
1912fa9e4066Sahrens 	}
1913fa9e4066Sahrens 
1914fa9e4066Sahrens 	if (verbosity >= 5)
191588b7b0f2SMatthew Ahrens 		dump_indirect(dn);
1916fa9e4066Sahrens 
1917fa9e4066Sahrens 	if (verbosity >= 5) {
1918fa9e4066Sahrens 		/*
1919fa9e4066Sahrens 		 * Report the list of segments that comprise the object.
1920fa9e4066Sahrens 		 */
1921fa9e4066Sahrens 		uint64_t start = 0;
1922fa9e4066Sahrens 		uint64_t end;
1923fa9e4066Sahrens 		uint64_t blkfill = 1;
1924fa9e4066Sahrens 		int minlvl = 1;
1925fa9e4066Sahrens 
1926fa9e4066Sahrens 		if (dn->dn_type == DMU_OT_DNODE) {
1927fa9e4066Sahrens 			minlvl = 0;
1928fa9e4066Sahrens 			blkfill = DNODES_PER_BLOCK;
1929fa9e4066Sahrens 		}
1930fa9e4066Sahrens 
1931fa9e4066Sahrens 		for (;;) {
19323f9d6ad7SLin Ling 			char segsize[32];
1933*fc30d466SJason King 			/* make sure nicenum has enough space */
1934*fc30d466SJason King 			CTASSERT(sizeof (segsize) >= NN_NUMBUF_SZ);
1935cdb0ab79Smaybee 			error = dnode_next_offset(dn,
1936cdb0ab79Smaybee 			    0, &start, minlvl, blkfill, 0);
1937fa9e4066Sahrens 			if (error)
1938fa9e4066Sahrens 				break;
1939fa9e4066Sahrens 			end = start;
1940cdb0ab79Smaybee 			error = dnode_next_offset(dn,
1941cdb0ab79Smaybee 			    DNODE_FIND_HOLE, &end, minlvl, blkfill, 0);
1942*fc30d466SJason King 			zdb_nicenum(end - start, segsize, sizeof (segsize));
1943fa9e4066Sahrens 			(void) printf("\t\tsegment [%016llx, %016llx)"
1944fa9e4066Sahrens 			    " size %5s\n", (u_longlong_t)start,
1945fa9e4066Sahrens 			    (u_longlong_t)end, segsize);
1946fa9e4066Sahrens 			if (error)
1947fa9e4066Sahrens 				break;
1948fa9e4066Sahrens 			start = end;
1949fa9e4066Sahrens 		}
1950fa9e4066Sahrens 	}
1951fa9e4066Sahrens 
1952fa9e4066Sahrens 	if (db != NULL)
1953ea8dc4b6Seschrock 		dmu_buf_rele(db, FTAG);
1954fa9e4066Sahrens }
1955fa9e4066Sahrens 
1956fa9e4066Sahrens static char *objset_types[DMU_OST_NUMTYPES] = {
1957fa9e4066Sahrens 	"NONE", "META", "ZPL", "ZVOL", "OTHER", "ANY" };
1958fa9e4066Sahrens 
1959fa9e4066Sahrens static void
1960fa9e4066Sahrens dump_dir(objset_t *os)
1961fa9e4066Sahrens {
1962fa9e4066Sahrens 	dmu_objset_stats_t dds;
1963fa9e4066Sahrens 	uint64_t object, object_count;
1964a2eea2e1Sahrens 	uint64_t refdbytes, usedobjs, scratch;
19653f9d6ad7SLin Ling 	char numbuf[32];
196614843421SMatthew Ahrens 	char blkbuf[BP_SPRINTF_LEN + 20];
196740a5c998SMatthew Ahrens 	char osname[ZFS_MAX_DATASET_NAME_LEN];
1968fa9e4066Sahrens 	char *type = "UNKNOWN";
1969fa9e4066Sahrens 	int verbosity = dump_opt['d'];
1970fa9e4066Sahrens 	int print_header = 1;
1971fa9e4066Sahrens 	int i, error;
1972fa9e4066Sahrens 
1973*fc30d466SJason King 	/* make sure nicenum has enough space */
1974*fc30d466SJason King 	CTASSERT(sizeof (numbuf) >= NN_NUMBUF_SZ);
1975*fc30d466SJason King 
19763b2aab18SMatthew Ahrens 	dsl_pool_config_enter(dmu_objset_pool(os), FTAG);
1977a2eea2e1Sahrens 	dmu_objset_fast_stat(os, &dds);
19783b2aab18SMatthew Ahrens 	dsl_pool_config_exit(dmu_objset_pool(os), FTAG);
1979fa9e4066Sahrens 
1980fa9e4066Sahrens 	if (dds.dds_type < DMU_OST_NUMTYPES)
1981fa9e4066Sahrens 		type = objset_types[dds.dds_type];
1982fa9e4066Sahrens 
1983fa9e4066Sahrens 	if (dds.dds_type == DMU_OST_META) {
1984fa9e4066Sahrens 		dds.dds_creation_txg = TXG_INITIAL;
19855d7b4d43SMatthew Ahrens 		usedobjs = BP_GET_FILL(os->os_rootbp);
1986c1379625SJustin T. Gibbs 		refdbytes = dsl_dir_phys(os->os_spa->spa_dsl_pool->dp_mos_dir)->
1987c1379625SJustin T. Gibbs 		    dd_used_bytes;
1988a2eea2e1Sahrens 	} else {
1989a2eea2e1Sahrens 		dmu_objset_space(os, &refdbytes, &scratch, &usedobjs, &scratch);
1990fa9e4066Sahrens 	}
1991fa9e4066Sahrens 
19925d7b4d43SMatthew Ahrens 	ASSERT3U(usedobjs, ==, BP_GET_FILL(os->os_rootbp));
1993fa9e4066Sahrens 
1994*fc30d466SJason King 	zdb_nicenum(refdbytes, numbuf, sizeof (numbuf));
1995fa9e4066Sahrens 
1996fa9e4066Sahrens 	if (verbosity >= 4) {
199743466aaeSMax Grossman 		(void) snprintf(blkbuf, sizeof (blkbuf), ", rootbp ");
199843466aaeSMax Grossman 		(void) snprintf_blkptr(blkbuf + strlen(blkbuf),
199943466aaeSMax Grossman 		    sizeof (blkbuf) - strlen(blkbuf), os->os_rootbp);
2000fa9e4066Sahrens 	} else {
2001fa9e4066Sahrens 		blkbuf[0] = '\0';
2002fa9e4066Sahrens 	}
2003fa9e4066Sahrens 
2004fa9e4066Sahrens 	dmu_objset_name(os, osname);
2005fa9e4066Sahrens 
2006a2eea2e1Sahrens 	(void) printf("Dataset %s [%s], ID %llu, cr_txg %llu, "
2007fa9e4066Sahrens 	    "%s, %llu objects%s\n",
2008fa9e4066Sahrens 	    osname, type, (u_longlong_t)dmu_objset_id(os),
2009fa9e4066Sahrens 	    (u_longlong_t)dds.dds_creation_txg,
2010a2eea2e1Sahrens 	    numbuf, (u_longlong_t)usedobjs, blkbuf);
2011fa9e4066Sahrens 
2012b24ab676SJeff Bonwick 	if (zopt_objects != 0) {
2013b24ab676SJeff Bonwick 		for (i = 0; i < zopt_objects; i++)
2014b24ab676SJeff Bonwick 			dump_object(os, zopt_object[i], verbosity,
2015b24ab676SJeff Bonwick 			    &print_header);
2016b24ab676SJeff Bonwick 		(void) printf("\n");
2017b24ab676SJeff Bonwick 		return;
2018b24ab676SJeff Bonwick 	}
2019b24ab676SJeff Bonwick 
2020b24ab676SJeff Bonwick 	if (dump_opt['i'] != 0 || verbosity >= 2)
2021fa9e4066Sahrens 		dump_intent_log(dmu_objset_zil(os));
2022fa9e4066Sahrens 
2023fa9e4066Sahrens 	if (dmu_objset_ds(os) != NULL)
2024cde58dbcSMatthew Ahrens 		dump_deadlist(&dmu_objset_ds(os)->ds_deadlist);
2025fa9e4066Sahrens 
2026fa9e4066Sahrens 	if (verbosity < 2)
2027fa9e4066Sahrens 		return;
2028fa9e4066Sahrens 
202943466aaeSMax Grossman 	if (BP_IS_HOLE(os->os_rootbp))
2030088f3894Sahrens 		return;
2031088f3894Sahrens 
2032fa9e4066Sahrens 	dump_object(os, 0, verbosity, &print_header);
203314843421SMatthew Ahrens 	object_count = 0;
2034744947dcSTom Erickson 	if (DMU_USERUSED_DNODE(os) != NULL &&
2035744947dcSTom Erickson 	    DMU_USERUSED_DNODE(os)->dn_type != 0) {
203614843421SMatthew Ahrens 		dump_object(os, DMU_USERUSED_OBJECT, verbosity, &print_header);
203714843421SMatthew Ahrens 		dump_object(os, DMU_GROUPUSED_OBJECT, verbosity, &print_header);
203814843421SMatthew Ahrens 	}
2039fa9e4066Sahrens 
2040fa9e4066Sahrens 	object = 0;
20416754306eSahrens 	while ((error = dmu_object_next(os, &object, B_FALSE, 0)) == 0) {
2042fa9e4066Sahrens 		dump_object(os, object, verbosity, &print_header);
2043fa9e4066Sahrens 		object_count++;
2044fa9e4066Sahrens 	}
2045fa9e4066Sahrens 
2046a2eea2e1Sahrens 	ASSERT3U(object_count, ==, usedobjs);
2047fa9e4066Sahrens 
2048fa9e4066Sahrens 	(void) printf("\n");
2049fa9e4066Sahrens 
2050ccba0801SRich Morris 	if (error != ESRCH) {
2051ccba0801SRich Morris 		(void) fprintf(stderr, "dmu_object_next() = %d\n", error);
2052ccba0801SRich Morris 		abort();
2053ccba0801SRich Morris 	}
2054fa9e4066Sahrens }
2055fa9e4066Sahrens 
2056fa9e4066Sahrens static void
205753b9a4a9SVictor Latushkin dump_uberblock(uberblock_t *ub, const char *header, const char *footer)
2058fa9e4066Sahrens {
2059fa9e4066Sahrens 	time_t timestamp = ub->ub_timestamp;
2060fa9e4066Sahrens 
206153b9a4a9SVictor Latushkin 	(void) printf(header ? header : "");
2062fa9e4066Sahrens 	(void) printf("\tmagic = %016llx\n", (u_longlong_t)ub->ub_magic);
2063fa9e4066Sahrens 	(void) printf("\tversion = %llu\n", (u_longlong_t)ub->ub_version);
2064fa9e4066Sahrens 	(void) printf("\ttxg = %llu\n", (u_longlong_t)ub->ub_txg);
2065fa9e4066Sahrens 	(void) printf("\tguid_sum = %llu\n", (u_longlong_t)ub->ub_guid_sum);
2066fa9e4066Sahrens 	(void) printf("\ttimestamp = %llu UTC = %s",
2067fa9e4066Sahrens 	    (u_longlong_t)ub->ub_timestamp, asctime(localtime(&timestamp)));
2068fa9e4066Sahrens 	if (dump_opt['u'] >= 3) {
2069fbabab8fSmaybee 		char blkbuf[BP_SPRINTF_LEN];
207043466aaeSMax Grossman 		snprintf_blkptr(blkbuf, sizeof (blkbuf), &ub->ub_rootbp);
2071fa9e4066Sahrens 		(void) printf("\trootbp = %s\n", blkbuf);
2072fa9e4066Sahrens 	}
207353b9a4a9SVictor Latushkin 	(void) printf(footer ? footer : "");
2074fa9e4066Sahrens }
2075fa9e4066Sahrens 
2076fa9e4066Sahrens static void
207707428bdfSVictor Latushkin dump_config(spa_t *spa)
2078fa9e4066Sahrens {
207907428bdfSVictor Latushkin 	dmu_buf_t *db;
208007428bdfSVictor Latushkin 	size_t nvsize = 0;
208107428bdfSVictor Latushkin 	int error = 0;
2082fa9e4066Sahrens 
208307428bdfSVictor Latushkin 
208407428bdfSVictor Latushkin 	error = dmu_bonus_hold(spa->spa_meta_objset,
208507428bdfSVictor Latushkin 	    spa->spa_config_object, FTAG, &db);
208607428bdfSVictor Latushkin 
208707428bdfSVictor Latushkin 	if (error == 0) {
208807428bdfSVictor Latushkin 		nvsize = *(uint64_t *)db->db_data;
208907428bdfSVictor Latushkin 		dmu_buf_rele(db, FTAG);
209007428bdfSVictor Latushkin 
209107428bdfSVictor Latushkin 		(void) printf("\nMOS Configuration:\n");
209207428bdfSVictor Latushkin 		dump_packed_nvlist(spa->spa_meta_objset,
209307428bdfSVictor Latushkin 		    spa->spa_config_object, (void *)&nvsize, 1);
209407428bdfSVictor Latushkin 	} else {
209507428bdfSVictor Latushkin 		(void) fprintf(stderr, "dmu_bonus_hold(%llu) failed, errno %d",
209607428bdfSVictor Latushkin 		    (u_longlong_t)spa->spa_config_object, error);
2097fa9e4066Sahrens 	}
2098fa9e4066Sahrens }
2099fa9e4066Sahrens 
2100fa9e4066Sahrens static void
2101c5904d13Seschrock dump_cachefile(const char *cachefile)
2102c5904d13Seschrock {
2103c5904d13Seschrock 	int fd;
2104c5904d13Seschrock 	struct stat64 statbuf;
2105c5904d13Seschrock 	char *buf;
2106c5904d13Seschrock 	nvlist_t *config;
2107c5904d13Seschrock 
2108c5904d13Seschrock 	if ((fd = open64(cachefile, O_RDONLY)) < 0) {
2109c5904d13Seschrock 		(void) printf("cannot open '%s': %s\n", cachefile,
2110c5904d13Seschrock 		    strerror(errno));
2111c5904d13Seschrock 		exit(1);
2112c5904d13Seschrock 	}
2113c5904d13Seschrock 
2114c5904d13Seschrock 	if (fstat64(fd, &statbuf) != 0) {
2115c5904d13Seschrock 		(void) printf("failed to stat '%s': %s\n", cachefile,
2116c5904d13Seschrock 		    strerror(errno));
2117c5904d13Seschrock 		exit(1);
2118c5904d13Seschrock 	}
2119c5904d13Seschrock 
2120c5904d13Seschrock 	if ((buf = malloc(statbuf.st_size)) == NULL) {
2121c5904d13Seschrock 		(void) fprintf(stderr, "failed to allocate %llu bytes\n",
2122c5904d13Seschrock 		    (u_longlong_t)statbuf.st_size);
2123c5904d13Seschrock 		exit(1);
2124c5904d13Seschrock 	}
2125c5904d13Seschrock 
2126c5904d13Seschrock 	if (read(fd, buf, statbuf.st_size) != statbuf.st_size) {
2127c5904d13Seschrock 		(void) fprintf(stderr, "failed to read %llu bytes\n",
2128c5904d13Seschrock 		    (u_longlong_t)statbuf.st_size);
2129c5904d13Seschrock 		exit(1);
2130c5904d13Seschrock 	}
2131c5904d13Seschrock 
2132c5904d13Seschrock 	(void) close(fd);
2133c5904d13Seschrock 
2134c5904d13Seschrock 	if (nvlist_unpack(buf, statbuf.st_size, &config, 0) != 0) {
2135c5904d13Seschrock 		(void) fprintf(stderr, "failed to unpack nvlist\n");
2136c5904d13Seschrock 		exit(1);
2137c5904d13Seschrock 	}
2138c5904d13Seschrock 
2139c5904d13Seschrock 	free(buf);
2140c5904d13Seschrock 
2141c5904d13Seschrock 	dump_nvlist(config, 0);
2142c5904d13Seschrock 
2143c5904d13Seschrock 	nvlist_free(config);
2144c5904d13Seschrock }
2145c5904d13Seschrock 
214653b9a4a9SVictor Latushkin #define	ZDB_MAX_UB_HEADER_SIZE 32
214753b9a4a9SVictor Latushkin 
214853b9a4a9SVictor Latushkin static void
214953b9a4a9SVictor Latushkin dump_label_uberblocks(vdev_label_t *lbl, uint64_t ashift)
215053b9a4a9SVictor Latushkin {
215153b9a4a9SVictor Latushkin 	vdev_t vd;
215253b9a4a9SVictor Latushkin 	vdev_t *vdp = &vd;
215353b9a4a9SVictor Latushkin 	char header[ZDB_MAX_UB_HEADER_SIZE];
215453b9a4a9SVictor Latushkin 
215553b9a4a9SVictor Latushkin 	vd.vdev_ashift = ashift;
215653b9a4a9SVictor Latushkin 	vdp->vdev_top = vdp;
215753b9a4a9SVictor Latushkin 
215853b9a4a9SVictor Latushkin 	for (int i = 0; i < VDEV_UBERBLOCK_COUNT(vdp); i++) {
215953b9a4a9SVictor Latushkin 		uint64_t uoff = VDEV_UBERBLOCK_OFFSET(vdp, i);
216053b9a4a9SVictor Latushkin 		uberblock_t *ub = (void *)((char *)lbl + uoff);
216153b9a4a9SVictor Latushkin 
216253b9a4a9SVictor Latushkin 		if (uberblock_verify(ub))
216353b9a4a9SVictor Latushkin 			continue;
216453b9a4a9SVictor Latushkin 		(void) snprintf(header, ZDB_MAX_UB_HEADER_SIZE,
216553b9a4a9SVictor Latushkin 		    "Uberblock[%d]\n", i);
216653b9a4a9SVictor Latushkin 		dump_uberblock(ub, header, "");
216753b9a4a9SVictor Latushkin 	}
216853b9a4a9SVictor Latushkin }
216953b9a4a9SVictor Latushkin 
2170c5904d13Seschrock static void
2171fa9e4066Sahrens dump_label(const char *dev)
2172fa9e4066Sahrens {
2173fa9e4066Sahrens 	int fd;
2174fa9e4066Sahrens 	vdev_label_t label;
2175c6065d0fSGeorge Wilson 	char *path, *buf = label.vl_vdev_phys.vp_nvlist;
2176fa9e4066Sahrens 	size_t buflen = sizeof (label.vl_vdev_phys.vp_nvlist);
2177fa9e4066Sahrens 	struct stat64 statbuf;
217853b9a4a9SVictor Latushkin 	uint64_t psize, ashift;
2179c6065d0fSGeorge Wilson 	int len = strlen(dev) + 1;
2180fa9e4066Sahrens 
2181c6065d0fSGeorge Wilson 	if (strncmp(dev, "/dev/dsk/", 9) == 0) {
2182c6065d0fSGeorge Wilson 		len++;
2183c6065d0fSGeorge Wilson 		path = malloc(len);
2184c6065d0fSGeorge Wilson 		(void) snprintf(path, len, "%s%s", "/dev/rdsk/", dev + 9);
2185c6065d0fSGeorge Wilson 	} else {
2186c6065d0fSGeorge Wilson 		path = strdup(dev);
2187c6065d0fSGeorge Wilson 	}
2188c6065d0fSGeorge Wilson 
2189c6065d0fSGeorge Wilson 	if ((fd = open64(path, O_RDONLY)) < 0) {
2190c6065d0fSGeorge Wilson 		(void) printf("cannot open '%s': %s\n", path, strerror(errno));
2191c6065d0fSGeorge Wilson 		free(path);
2192fa9e4066Sahrens 		exit(1);
2193fa9e4066Sahrens 	}
2194fa9e4066Sahrens 
2195fa9e4066Sahrens 	if (fstat64(fd, &statbuf) != 0) {
2196c6065d0fSGeorge Wilson 		(void) printf("failed to stat '%s': %s\n", path,
2197fa9e4066Sahrens 		    strerror(errno));
2198c6065d0fSGeorge Wilson 		free(path);
2199c6065d0fSGeorge Wilson 		(void) close(fd);
2200c6065d0fSGeorge Wilson 		exit(1);
2201c6065d0fSGeorge Wilson 	}
2202c6065d0fSGeorge Wilson 
2203c6065d0fSGeorge Wilson 	if (S_ISBLK(statbuf.st_mode)) {
2204c6065d0fSGeorge Wilson 		(void) printf("cannot use '%s': character device required\n",
2205c6065d0fSGeorge Wilson 		    path);
2206c6065d0fSGeorge Wilson 		free(path);
2207c6065d0fSGeorge Wilson 		(void) close(fd);
2208c6065d0fSGeorge Wilson 		exit(1);
2209fa9e4066Sahrens 	}
2210fa9e4066Sahrens 
2211fa9e4066Sahrens 	psize = statbuf.st_size;
2212fa9e4066Sahrens 	psize = P2ALIGN(psize, (uint64_t)sizeof (vdev_label_t));
2213fa9e4066Sahrens 
221453b9a4a9SVictor Latushkin 	for (int l = 0; l < VDEV_LABELS; l++) {
2215fa9e4066Sahrens 		nvlist_t *config = NULL;
2216fa9e4066Sahrens 
2217fa9e4066Sahrens 		(void) printf("--------------------------------------------\n");
2218fa9e4066Sahrens 		(void) printf("LABEL %d\n", l);
2219fa9e4066Sahrens 		(void) printf("--------------------------------------------\n");
2220fa9e4066Sahrens 
22210d981225Seschrock 		if (pread64(fd, &label, sizeof (label),
2222fa9e4066Sahrens 		    vdev_label_offset(psize, l, 0)) != sizeof (label)) {
2223fa9e4066Sahrens 			(void) printf("failed to read label %d\n", l);
2224fa9e4066Sahrens 			continue;
2225fa9e4066Sahrens 		}
2226fa9e4066Sahrens 
2227fa9e4066Sahrens 		if (nvlist_unpack(buf, buflen, &config, 0) != 0) {
2228fa9e4066Sahrens 			(void) printf("failed to unpack label %d\n", l);
222953b9a4a9SVictor Latushkin 			ashift = SPA_MINBLOCKSHIFT;
223053b9a4a9SVictor Latushkin 		} else {
223153b9a4a9SVictor Latushkin 			nvlist_t *vdev_tree = NULL;
223253b9a4a9SVictor Latushkin 
2233fa9e4066Sahrens 			dump_nvlist(config, 4);
223453b9a4a9SVictor Latushkin 			if ((nvlist_lookup_nvlist(config,
223553b9a4a9SVictor Latushkin 			    ZPOOL_CONFIG_VDEV_TREE, &vdev_tree) != 0) ||
223653b9a4a9SVictor Latushkin 			    (nvlist_lookup_uint64(vdev_tree,
223753b9a4a9SVictor Latushkin 			    ZPOOL_CONFIG_ASHIFT, &ashift) != 0))
223853b9a4a9SVictor Latushkin 				ashift = SPA_MINBLOCKSHIFT;
2239fa9e4066Sahrens 			nvlist_free(config);
2240fa9e4066Sahrens 		}
224153b9a4a9SVictor Latushkin 		if (dump_opt['u'])
224253b9a4a9SVictor Latushkin 			dump_label_uberblocks(&label, ashift);
224353b9a4a9SVictor Latushkin 	}
2244c6065d0fSGeorge Wilson 
2245c6065d0fSGeorge Wilson 	free(path);
2246c6065d0fSGeorge Wilson 	(void) close(fd);
2247fa9e4066Sahrens }
2248fa9e4066Sahrens 
2249ca0cc391SMatthew Ahrens static uint64_t dataset_feature_count[SPA_FEATURES];
2250b5152584SMatthew Ahrens 
2251fa9e4066Sahrens /*ARGSUSED*/
22521d452cf5Sahrens static int
2253fd136879SMatthew Ahrens dump_one_dir(const char *dsname, void *arg)
2254fa9e4066Sahrens {
2255fa9e4066Sahrens 	int error;
2256fa9e4066Sahrens 	objset_t *os;
2257fa9e4066Sahrens 
2258503ad85cSMatthew Ahrens 	error = dmu_objset_own(dsname, DMU_OST_ANY, B_TRUE, FTAG, &os);
2259fa9e4066Sahrens 	if (error) {
2260b24ab676SJeff Bonwick 		(void) printf("Could not open %s, error %d\n", dsname, error);
22611d452cf5Sahrens 		return (0);
2262fa9e4066Sahrens 	}
2263ca0cc391SMatthew Ahrens 
2264ca0cc391SMatthew Ahrens 	for (spa_feature_t f = 0; f < SPA_FEATURES; f++) {
2265ca0cc391SMatthew Ahrens 		if (!dmu_objset_ds(os)->ds_feature_inuse[f])
2266ca0cc391SMatthew Ahrens 			continue;
2267ca0cc391SMatthew Ahrens 		ASSERT(spa_feature_table[f].fi_flags &
2268ca0cc391SMatthew Ahrens 		    ZFEATURE_FLAG_PER_DATASET);
2269ca0cc391SMatthew Ahrens 		dataset_feature_count[f]++;
2270ca0cc391SMatthew Ahrens 	}
2271ca0cc391SMatthew Ahrens 
2272fa9e4066Sahrens 	dump_dir(os);
2273503ad85cSMatthew Ahrens 	dmu_objset_disown(os, FTAG);
2274e0d35c44Smarks 	fuid_table_destroy();
22750a586ceaSMark Shellenbaum 	sa_loaded = B_FALSE;
22761d452cf5Sahrens 	return (0);
2277fa9e4066Sahrens }
2278fa9e4066Sahrens 
2279b24ab676SJeff Bonwick /*
2280b24ab676SJeff Bonwick  * Block statistics.
2281b24ab676SJeff Bonwick  */
2282b5152584SMatthew Ahrens #define	PSIZE_HISTO_SIZE (SPA_OLD_MAXBLOCKSIZE / SPA_MINBLOCKSIZE + 2)
2283b24ab676SJeff Bonwick typedef struct zdb_blkstats {
2284b24ab676SJeff Bonwick 	uint64_t zb_asize;
2285b24ab676SJeff Bonwick 	uint64_t zb_lsize;
2286b24ab676SJeff Bonwick 	uint64_t zb_psize;
2287b24ab676SJeff Bonwick 	uint64_t zb_count;
2288d5ee8a13SMatthew Ahrens 	uint64_t zb_gangs;
2289d5ee8a13SMatthew Ahrens 	uint64_t zb_ditto_samevdev;
2290490d05b9SMatthew Ahrens 	uint64_t zb_psize_histogram[PSIZE_HISTO_SIZE];
2291b24ab676SJeff Bonwick } zdb_blkstats_t;
2292b24ab676SJeff Bonwick 
2293b24ab676SJeff Bonwick /*
2294b24ab676SJeff Bonwick  * Extended object types to report deferred frees and dedup auto-ditto blocks.
2295b24ab676SJeff Bonwick  */
2296b24ab676SJeff Bonwick #define	ZDB_OT_DEFERRED	(DMU_OT_NUMTYPES + 0)
2297b24ab676SJeff Bonwick #define	ZDB_OT_DITTO	(DMU_OT_NUMTYPES + 1)
2298ad135b5dSChristopher Siden #define	ZDB_OT_OTHER	(DMU_OT_NUMTYPES + 2)
2299ad135b5dSChristopher Siden #define	ZDB_OT_TOTAL	(DMU_OT_NUMTYPES + 3)
2300b24ab676SJeff Bonwick 
2301b24ab676SJeff Bonwick static char *zdb_ot_extname[] = {
2302b24ab676SJeff Bonwick 	"deferred free",
2303b24ab676SJeff Bonwick 	"dedup ditto",
2304ad135b5dSChristopher Siden 	"other",
2305b24ab676SJeff Bonwick 	"Total",
2306b24ab676SJeff Bonwick };
2307b24ab676SJeff Bonwick 
2308b24ab676SJeff Bonwick #define	ZB_TOTAL	DN_MAX_LEVELS
2309b24ab676SJeff Bonwick 
2310b24ab676SJeff Bonwick typedef struct zdb_cb {
2311b24ab676SJeff Bonwick 	zdb_blkstats_t	zcb_type[ZB_TOTAL + 1][ZDB_OT_TOTAL + 1];
2312b24ab676SJeff Bonwick 	uint64_t	zcb_dedup_asize;
2313b24ab676SJeff Bonwick 	uint64_t	zcb_dedup_blocks;
23145d7b4d43SMatthew Ahrens 	uint64_t	zcb_embedded_blocks[NUM_BP_EMBEDDED_TYPES];
23155d7b4d43SMatthew Ahrens 	uint64_t	zcb_embedded_histogram[NUM_BP_EMBEDDED_TYPES]
23165d7b4d43SMatthew Ahrens 	    [BPE_PAYLOAD_SIZE];
2317490d05b9SMatthew Ahrens 	uint64_t	zcb_start;
2318490d05b9SMatthew Ahrens 	uint64_t	zcb_lastprint;
2319490d05b9SMatthew Ahrens 	uint64_t	zcb_totalasize;
2320b24ab676SJeff Bonwick 	uint64_t	zcb_errors[256];
2321b24ab676SJeff Bonwick 	int		zcb_readfails;
2322b24ab676SJeff Bonwick 	int		zcb_haderrors;
2323cde58dbcSMatthew Ahrens 	spa_t		*zcb_spa;
2324b24ab676SJeff Bonwick } zdb_cb_t;
2325b24ab676SJeff Bonwick 
2326b24ab676SJeff Bonwick static void
2327cde58dbcSMatthew Ahrens zdb_count_block(zdb_cb_t *zcb, zilog_t *zilog, const blkptr_t *bp,
2328b24ab676SJeff Bonwick     dmu_object_type_t type)
2329b24ab676SJeff Bonwick {
2330b24ab676SJeff Bonwick 	uint64_t refcnt = 0;
2331b24ab676SJeff Bonwick 
2332b24ab676SJeff Bonwick 	ASSERT(type < ZDB_OT_TOTAL);
2333b24ab676SJeff Bonwick 
2334b24ab676SJeff Bonwick 	if (zilog && zil_bp_tree_add(zilog, bp) != 0)
2335b24ab676SJeff Bonwick 		return;
2336b24ab676SJeff Bonwick 
2337b24ab676SJeff Bonwick 	for (int i = 0; i < 4; i++) {
2338b24ab676SJeff Bonwick 		int l = (i < 2) ? BP_GET_LEVEL(bp) : ZB_TOTAL;
2339b24ab676SJeff Bonwick 		int t = (i & 1) ? type : ZDB_OT_TOTAL;
2340d5ee8a13SMatthew Ahrens 		int equal;
2341b24ab676SJeff Bonwick 		zdb_blkstats_t *zb = &zcb->zcb_type[l][t];
2342b24ab676SJeff Bonwick 
2343b24ab676SJeff Bonwick 		zb->zb_asize += BP_GET_ASIZE(bp);
2344b24ab676SJeff Bonwick 		zb->zb_lsize += BP_GET_LSIZE(bp);
2345b24ab676SJeff Bonwick 		zb->zb_psize += BP_GET_PSIZE(bp);
2346b24ab676SJeff Bonwick 		zb->zb_count++;
2347b5152584SMatthew Ahrens 
2348b5152584SMatthew Ahrens 		/*
2349b5152584SMatthew Ahrens 		 * The histogram is only big enough to record blocks up to
2350b5152584SMatthew Ahrens 		 * SPA_OLD_MAXBLOCKSIZE; larger blocks go into the last,
2351b5152584SMatthew Ahrens 		 * "other", bucket.
2352b5152584SMatthew Ahrens 		 */
2353b5152584SMatthew Ahrens 		int idx = BP_GET_PSIZE(bp) >> SPA_MINBLOCKSHIFT;
2354b5152584SMatthew Ahrens 		idx = MIN(idx, SPA_OLD_MAXBLOCKSIZE / SPA_MINBLOCKSIZE + 1);
2355b5152584SMatthew Ahrens 		zb->zb_psize_histogram[idx]++;
2356d5ee8a13SMatthew Ahrens 
2357d5ee8a13SMatthew Ahrens 		zb->zb_gangs += BP_COUNT_GANG(bp);
2358d5ee8a13SMatthew Ahrens 
2359d5ee8a13SMatthew Ahrens 		switch (BP_GET_NDVAS(bp)) {
2360d5ee8a13SMatthew Ahrens 		case 2:
2361d5ee8a13SMatthew Ahrens 			if (DVA_GET_VDEV(&bp->blk_dva[0]) ==
2362d5ee8a13SMatthew Ahrens 			    DVA_GET_VDEV(&bp->blk_dva[1]))
2363d5ee8a13SMatthew Ahrens 				zb->zb_ditto_samevdev++;
2364d5ee8a13SMatthew Ahrens 			break;
2365d5ee8a13SMatthew Ahrens 		case 3:
2366d5ee8a13SMatthew Ahrens 			equal = (DVA_GET_VDEV(&bp->blk_dva[0]) ==
2367d5ee8a13SMatthew Ahrens 			    DVA_GET_VDEV(&bp->blk_dva[1])) +
2368d5ee8a13SMatthew Ahrens 			    (DVA_GET_VDEV(&bp->blk_dva[0]) ==
2369d5ee8a13SMatthew Ahrens 			    DVA_GET_VDEV(&bp->blk_dva[2])) +
2370d5ee8a13SMatthew Ahrens 			    (DVA_GET_VDEV(&bp->blk_dva[1]) ==
2371d5ee8a13SMatthew Ahrens 			    DVA_GET_VDEV(&bp->blk_dva[2]));
2372d5ee8a13SMatthew Ahrens 			if (equal != 0)
2373d5ee8a13SMatthew Ahrens 				zb->zb_ditto_samevdev++;
2374d5ee8a13SMatthew Ahrens 			break;
2375d5ee8a13SMatthew Ahrens 		}
2376d5ee8a13SMatthew Ahrens 
2377b24ab676SJeff Bonwick 	}
2378b24ab676SJeff Bonwick 
23795d7b4d43SMatthew Ahrens 	if (BP_IS_EMBEDDED(bp)) {
23805d7b4d43SMatthew Ahrens 		zcb->zcb_embedded_blocks[BPE_GET_ETYPE(bp)]++;
23815d7b4d43SMatthew Ahrens 		zcb->zcb_embedded_histogram[BPE_GET_ETYPE(bp)]
23825d7b4d43SMatthew Ahrens 		    [BPE_GET_PSIZE(bp)]++;
23835d7b4d43SMatthew Ahrens 		return;
23845d7b4d43SMatthew Ahrens 	}
23855d7b4d43SMatthew Ahrens 
2386b24ab676SJeff Bonwick 	if (dump_opt['L'])
2387b24ab676SJeff Bonwick 		return;
2388b24ab676SJeff Bonwick 
2389b24ab676SJeff Bonwick 	if (BP_GET_DEDUP(bp)) {
2390b24ab676SJeff Bonwick 		ddt_t *ddt;
2391b24ab676SJeff Bonwick 		ddt_entry_t *dde;
2392b24ab676SJeff Bonwick 
2393cde58dbcSMatthew Ahrens 		ddt = ddt_select(zcb->zcb_spa, bp);
2394b24ab676SJeff Bonwick 		ddt_enter(ddt);
2395b24ab676SJeff Bonwick 		dde = ddt_lookup(ddt, bp, B_FALSE);
2396b24ab676SJeff Bonwick 
2397b24ab676SJeff Bonwick 		if (dde == NULL) {
2398b24ab676SJeff Bonwick 			refcnt = 0;
2399b24ab676SJeff Bonwick 		} else {
2400b24ab676SJeff Bonwick 			ddt_phys_t *ddp = ddt_phys_select(dde, bp);
2401b24ab676SJeff Bonwick 			ddt_phys_decref(ddp);
2402b24ab676SJeff Bonwick 			refcnt = ddp->ddp_refcnt;
2403b24ab676SJeff Bonwick 			if (ddt_phys_total_refcnt(dde) == 0)
2404b24ab676SJeff Bonwick 				ddt_remove(ddt, dde);
2405b24ab676SJeff Bonwick 		}
2406b24ab676SJeff Bonwick 		ddt_exit(ddt);
2407b24ab676SJeff Bonwick 	}
2408b24ab676SJeff Bonwick 
2409cde58dbcSMatthew Ahrens 	VERIFY3U(zio_wait(zio_claim(NULL, zcb->zcb_spa,
2410cde58dbcSMatthew Ahrens 	    refcnt ? 0 : spa_first_txg(zcb->zcb_spa),
2411b24ab676SJeff Bonwick 	    bp, NULL, NULL, ZIO_FLAG_CANFAIL)), ==, 0);
2412b24ab676SJeff Bonwick }
2413b24ab676SJeff Bonwick 
241431d7e8faSGeorge Wilson static void
241531d7e8faSGeorge Wilson zdb_blkptr_done(zio_t *zio)
241631d7e8faSGeorge Wilson {
241731d7e8faSGeorge Wilson 	spa_t *spa = zio->io_spa;
241831d7e8faSGeorge Wilson 	blkptr_t *bp = zio->io_bp;
241931d7e8faSGeorge Wilson 	int ioerr = zio->io_error;
242031d7e8faSGeorge Wilson 	zdb_cb_t *zcb = zio->io_private;
24217802d7bfSMatthew Ahrens 	zbookmark_phys_t *zb = &zio->io_bookmark;
242231d7e8faSGeorge Wilson 
242331d7e8faSGeorge Wilson 	zio_data_buf_free(zio->io_data, zio->io_size);
242431d7e8faSGeorge Wilson 
242531d7e8faSGeorge Wilson 	mutex_enter(&spa->spa_scrub_lock);
242631d7e8faSGeorge Wilson 	spa->spa_scrub_inflight--;
242731d7e8faSGeorge Wilson 	cv_broadcast(&spa->spa_scrub_io_cv);
242831d7e8faSGeorge Wilson 
242931d7e8faSGeorge Wilson 	if (ioerr && !(zio->io_flags & ZIO_FLAG_SPECULATIVE)) {
243031d7e8faSGeorge Wilson 		char blkbuf[BP_SPRINTF_LEN];
243131d7e8faSGeorge Wilson 
243231d7e8faSGeorge Wilson 		zcb->zcb_haderrors = 1;
243331d7e8faSGeorge Wilson 		zcb->zcb_errors[ioerr]++;
243431d7e8faSGeorge Wilson 
243531d7e8faSGeorge Wilson 		if (dump_opt['b'] >= 2)
243643466aaeSMax Grossman 			snprintf_blkptr(blkbuf, sizeof (blkbuf), bp);
243731d7e8faSGeorge Wilson 		else
243831d7e8faSGeorge Wilson 			blkbuf[0] = '\0';
243931d7e8faSGeorge Wilson 
244031d7e8faSGeorge Wilson 		(void) printf("zdb_blkptr_cb: "
244131d7e8faSGeorge Wilson 		    "Got error %d reading "
244231d7e8faSGeorge Wilson 		    "<%llu, %llu, %lld, %llx> %s -- skipping\n",
244331d7e8faSGeorge Wilson 		    ioerr,
244431d7e8faSGeorge Wilson 		    (u_longlong_t)zb->zb_objset,
244531d7e8faSGeorge Wilson 		    (u_longlong_t)zb->zb_object,
244631d7e8faSGeorge Wilson 		    (u_longlong_t)zb->zb_level,
244731d7e8faSGeorge Wilson 		    (u_longlong_t)zb->zb_blkid,
244831d7e8faSGeorge Wilson 		    blkbuf);
244931d7e8faSGeorge Wilson 	}
245031d7e8faSGeorge Wilson 	mutex_exit(&spa->spa_scrub_lock);
245131d7e8faSGeorge Wilson }
245231d7e8faSGeorge Wilson 
2453b24ab676SJeff Bonwick static int
24541b912ec7SGeorge Wilson zdb_blkptr_cb(spa_t *spa, zilog_t *zilog, const blkptr_t *bp,
24557802d7bfSMatthew Ahrens     const zbookmark_phys_t *zb, const dnode_phys_t *dnp, void *arg)
2456b24ab676SJeff Bonwick {
2457b24ab676SJeff Bonwick 	zdb_cb_t *zcb = arg;
2458b24ab676SJeff Bonwick 	dmu_object_type_t type;
2459b24ab676SJeff Bonwick 	boolean_t is_metadata;
2460b24ab676SJeff Bonwick 
2461a2cdcdd2SPaul Dagnelie 	if (bp == NULL)
2462a2cdcdd2SPaul Dagnelie 		return (0);
2463a2cdcdd2SPaul Dagnelie 
246443466aaeSMax Grossman 	if (dump_opt['b'] >= 5 && bp->blk_birth > 0) {
246543466aaeSMax Grossman 		char blkbuf[BP_SPRINTF_LEN];
246643466aaeSMax Grossman 		snprintf_blkptr(blkbuf, sizeof (blkbuf), bp);
246743466aaeSMax Grossman 		(void) printf("objset %llu object %llu "
246843466aaeSMax Grossman 		    "level %lld offset 0x%llx %s\n",
246943466aaeSMax Grossman 		    (u_longlong_t)zb->zb_objset,
247043466aaeSMax Grossman 		    (u_longlong_t)zb->zb_object,
247143466aaeSMax Grossman 		    (longlong_t)zb->zb_level,
247243466aaeSMax Grossman 		    (u_longlong_t)blkid2offset(dnp, bp, zb),
247343466aaeSMax Grossman 		    blkbuf);
247443466aaeSMax Grossman 	}
247543466aaeSMax Grossman 
247643466aaeSMax Grossman 	if (BP_IS_HOLE(bp))
2477b24ab676SJeff Bonwick 		return (0);
2478b24ab676SJeff Bonwick 
2479b24ab676SJeff Bonwick 	type = BP_GET_TYPE(bp);
2480b24ab676SJeff Bonwick 
2481ad135b5dSChristopher Siden 	zdb_count_block(zcb, zilog, bp,
2482ad135b5dSChristopher Siden 	    (type & DMU_OT_NEWTYPE) ? ZDB_OT_OTHER : type);
2483b24ab676SJeff Bonwick 
2484ad135b5dSChristopher Siden 	is_metadata = (BP_GET_LEVEL(bp) != 0 || DMU_OT_IS_METADATA(type));
2485b24ab676SJeff Bonwick 
24865d7b4d43SMatthew Ahrens 	if (!BP_IS_EMBEDDED(bp) &&
24875d7b4d43SMatthew Ahrens 	    (dump_opt['c'] > 1 || (dump_opt['c'] && is_metadata))) {
2488b24ab676SJeff Bonwick 		size_t size = BP_GET_PSIZE(bp);
248931d7e8faSGeorge Wilson 		void *data = zio_data_buf_alloc(size);
2490b24ab676SJeff Bonwick 		int flags = ZIO_FLAG_CANFAIL | ZIO_FLAG_SCRUB | ZIO_FLAG_RAW;
2491b24ab676SJeff Bonwick 
2492b24ab676SJeff Bonwick 		/* If it's an intent log block, failure is expected. */
2493b24ab676SJeff Bonwick 		if (zb->zb_level == ZB_ZIL_LEVEL)
2494b24ab676SJeff Bonwick 			flags |= ZIO_FLAG_SPECULATIVE;
2495b24ab676SJeff Bonwick 
249631d7e8faSGeorge Wilson 		mutex_enter(&spa->spa_scrub_lock);
249731d7e8faSGeorge Wilson 		while (spa->spa_scrub_inflight > max_inflight)
249831d7e8faSGeorge Wilson 			cv_wait(&spa->spa_scrub_io_cv, &spa->spa_scrub_lock);
249931d7e8faSGeorge Wilson 		spa->spa_scrub_inflight++;
250031d7e8faSGeorge Wilson 		mutex_exit(&spa->spa_scrub_lock);
2501b24ab676SJeff Bonwick 
250231d7e8faSGeorge Wilson 		zio_nowait(zio_read(NULL, spa, bp, data, size,
250331d7e8faSGeorge Wilson 		    zdb_blkptr_done, zcb, ZIO_PRIORITY_ASYNC_READ, flags, zb));
2504b24ab676SJeff Bonwick 	}
2505b24ab676SJeff Bonwick 
2506b24ab676SJeff Bonwick 	zcb->zcb_readfails = 0;
2507b24ab676SJeff Bonwick 
2508f7950bf1SMatthew Ahrens 	/* only call gethrtime() every 100 blocks */
2509f7950bf1SMatthew Ahrens 	static int iters;
2510f7950bf1SMatthew Ahrens 	if (++iters > 100)
2511f7950bf1SMatthew Ahrens 		iters = 0;
2512f7950bf1SMatthew Ahrens 	else
2513f7950bf1SMatthew Ahrens 		return (0);
2514f7950bf1SMatthew Ahrens 
2515f7950bf1SMatthew Ahrens 	if (dump_opt['b'] < 5 && gethrtime() > zcb->zcb_lastprint + NANOSEC) {
2516490d05b9SMatthew Ahrens 		uint64_t now = gethrtime();
2517490d05b9SMatthew Ahrens 		char buf[10];
2518490d05b9SMatthew Ahrens 		uint64_t bytes = zcb->zcb_type[ZB_TOTAL][ZDB_OT_TOTAL].zb_asize;
2519490d05b9SMatthew Ahrens 		int kb_per_sec =
2520490d05b9SMatthew Ahrens 		    1 + bytes / (1 + ((now - zcb->zcb_start) / 1000 / 1000));
2521490d05b9SMatthew Ahrens 		int sec_remaining =
2522490d05b9SMatthew Ahrens 		    (zcb->zcb_totalasize - bytes) / 1024 / kb_per_sec;
2523490d05b9SMatthew Ahrens 
2524*fc30d466SJason King 		/* make sure nicenum has enough space */
2525*fc30d466SJason King 		CTASSERT(sizeof (buf) >= NN_NUMBUF_SZ);
2526*fc30d466SJason King 
2527490d05b9SMatthew Ahrens 		zfs_nicenum(bytes, buf, sizeof (buf));
2528490d05b9SMatthew Ahrens 		(void) fprintf(stderr,
2529490d05b9SMatthew Ahrens 		    "\r%5s completed (%4dMB/s) "
2530490d05b9SMatthew Ahrens 		    "estimated time remaining: %uhr %02umin %02usec        ",
2531490d05b9SMatthew Ahrens 		    buf, kb_per_sec / 1024,
2532490d05b9SMatthew Ahrens 		    sec_remaining / 60 / 60,
2533490d05b9SMatthew Ahrens 		    sec_remaining / 60 % 60,
2534490d05b9SMatthew Ahrens 		    sec_remaining % 60);
2535490d05b9SMatthew Ahrens 
2536490d05b9SMatthew Ahrens 		zcb->zcb_lastprint = now;
2537490d05b9SMatthew Ahrens 	}
2538490d05b9SMatthew Ahrens 
2539b24ab676SJeff Bonwick 	return (0);
2540b24ab676SJeff Bonwick }
2541b24ab676SJeff Bonwick 
2542fa9e4066Sahrens static void
25430713e232SGeorge Wilson zdb_leak(void *arg, uint64_t start, uint64_t size)
2544fa9e4066Sahrens {
25450713e232SGeorge Wilson 	vdev_t *vd = arg;
2546fa9e4066Sahrens 
2547fa9e4066Sahrens 	(void) printf("leaked space: vdev %llu, offset 0x%llx, size %llu\n",
2548e14bb325SJeff Bonwick 	    (u_longlong_t)vd->vdev_id, (u_longlong_t)start, (u_longlong_t)size);
2549e14bb325SJeff Bonwick }
2550e14bb325SJeff Bonwick 
25510713e232SGeorge Wilson static metaslab_ops_t zdb_metaslab_ops = {
25522e4c9986SGeorge Wilson 	NULL	/* alloc */
2553e14bb325SJeff Bonwick };
2554e14bb325SJeff Bonwick 
2555e14bb325SJeff Bonwick static void
2556bbfd46c4SJeff Bonwick zdb_ddt_leak_init(spa_t *spa, zdb_cb_t *zcb)
2557fa9e4066Sahrens {
2558bbfd46c4SJeff Bonwick 	ddt_bookmark_t ddb = { 0 };
2559b24ab676SJeff Bonwick 	ddt_entry_t dde;
2560b24ab676SJeff Bonwick 	int error;
2561fa9e4066Sahrens 
2562bbfd46c4SJeff Bonwick 	while ((error = ddt_walk(spa, &ddb, &dde)) == 0) {
2563b24ab676SJeff Bonwick 		blkptr_t blk;
2564b24ab676SJeff Bonwick 		ddt_phys_t *ddp = dde.dde_phys;
2565bbfd46c4SJeff Bonwick 
2566bbfd46c4SJeff Bonwick 		if (ddb.ddb_class == DDT_CLASS_UNIQUE)
2567bbfd46c4SJeff Bonwick 			return;
2568bbfd46c4SJeff Bonwick 
2569b24ab676SJeff Bonwick 		ASSERT(ddt_phys_total_refcnt(&dde) > 1);
2570bbfd46c4SJeff Bonwick 
2571b24ab676SJeff Bonwick 		for (int p = 0; p < DDT_PHYS_TYPES; p++, ddp++) {
2572b24ab676SJeff Bonwick 			if (ddp->ddp_phys_birth == 0)
2573b24ab676SJeff Bonwick 				continue;
2574bbfd46c4SJeff Bonwick 			ddt_bp_create(ddb.ddb_checksum,
2575bbfd46c4SJeff Bonwick 			    &dde.dde_key, ddp, &blk);
2576b24ab676SJeff Bonwick 			if (p == DDT_PHYS_DITTO) {
2577cde58dbcSMatthew Ahrens 				zdb_count_block(zcb, NULL, &blk, ZDB_OT_DITTO);
2578b24ab676SJeff Bonwick 			} else {
2579b24ab676SJeff Bonwick 				zcb->zcb_dedup_asize +=
2580b24ab676SJeff Bonwick 				    BP_GET_ASIZE(&blk) * (ddp->ddp_refcnt - 1);
2581b24ab676SJeff Bonwick 				zcb->zcb_dedup_blocks++;
2582b24ab676SJeff Bonwick 			}
2583b24ab676SJeff Bonwick 		}
2584b24ab676SJeff Bonwick 		if (!dump_opt['L']) {
2585bbfd46c4SJeff Bonwick 			ddt_t *ddt = spa->spa_ddt[ddb.ddb_checksum];
2586b24ab676SJeff Bonwick 			ddt_enter(ddt);
2587b24ab676SJeff Bonwick 			VERIFY(ddt_lookup(ddt, &blk, B_TRUE) != NULL);
2588b24ab676SJeff Bonwick 			ddt_exit(ddt);
2589b24ab676SJeff Bonwick 		}
2590b24ab676SJeff Bonwick 	}
2591b24ab676SJeff Bonwick 
2592b24ab676SJeff Bonwick 	ASSERT(error == ENOENT);
2593b24ab676SJeff Bonwick }
2594b24ab676SJeff Bonwick 
2595b24ab676SJeff Bonwick static void
2596b24ab676SJeff Bonwick zdb_leak_init(spa_t *spa, zdb_cb_t *zcb)
2597b24ab676SJeff Bonwick {
2598cde58dbcSMatthew Ahrens 	zcb->zcb_spa = spa;
2599cde58dbcSMatthew Ahrens 
2600b24ab676SJeff Bonwick 	if (!dump_opt['L']) {
2601b24ab676SJeff Bonwick 		vdev_t *rvd = spa->spa_root_vdev;
260206be9802SMatthew Ahrens 		for (uint64_t c = 0; c < rvd->vdev_children; c++) {
2603e14bb325SJeff Bonwick 			vdev_t *vd = rvd->vdev_child[c];
260406be9802SMatthew Ahrens 			for (uint64_t m = 0; m < vd->vdev_ms_count; m++) {
2605fa9e4066Sahrens 				metaslab_t *msp = vd->vdev_ms[m];
2606fa9e4066Sahrens 				mutex_enter(&msp->ms_lock);
26070713e232SGeorge Wilson 				metaslab_unload(msp);
26080713e232SGeorge Wilson 
26090713e232SGeorge Wilson 				/*
26100713e232SGeorge Wilson 				 * For leak detection, we overload the metaslab
26110713e232SGeorge Wilson 				 * ms_tree to contain allocated segments
26120713e232SGeorge Wilson 				 * instead of free segments. As a result,
26130713e232SGeorge Wilson 				 * we can't use the normal metaslab_load/unload
26140713e232SGeorge Wilson 				 * interfaces.
26150713e232SGeorge Wilson 				 */
26160713e232SGeorge Wilson 				if (msp->ms_sm != NULL) {
261706be9802SMatthew Ahrens 					(void) fprintf(stderr,
261806be9802SMatthew Ahrens 					    "\rloading space map for "
261906be9802SMatthew Ahrens 					    "vdev %llu of %llu, "
262006be9802SMatthew Ahrens 					    "metaslab %llu of %llu ...",
262106be9802SMatthew Ahrens 					    (longlong_t)c,
262206be9802SMatthew Ahrens 					    (longlong_t)rvd->vdev_children,
262306be9802SMatthew Ahrens 					    (longlong_t)m,
262406be9802SMatthew Ahrens 					    (longlong_t)vd->vdev_ms_count);
262506be9802SMatthew Ahrens 
26260713e232SGeorge Wilson 					msp->ms_ops = &zdb_metaslab_ops;
2627f7950bf1SMatthew Ahrens 
2628f7950bf1SMatthew Ahrens 					/*
2629f7950bf1SMatthew Ahrens 					 * We don't want to spend the CPU
2630f7950bf1SMatthew Ahrens 					 * manipulating the size-ordered
2631f7950bf1SMatthew Ahrens 					 * tree, so clear the range_tree
2632f7950bf1SMatthew Ahrens 					 * ops.
2633f7950bf1SMatthew Ahrens 					 */
2634f7950bf1SMatthew Ahrens 					msp->ms_tree->rt_ops = NULL;
26350713e232SGeorge Wilson 					VERIFY0(space_map_load(msp->ms_sm,
26360713e232SGeorge Wilson 					    msp->ms_tree, SM_ALLOC));
26370713e232SGeorge Wilson 					msp->ms_loaded = B_TRUE;
26380713e232SGeorge Wilson 				}
2639fa9e4066Sahrens 				mutex_exit(&msp->ms_lock);
2640fa9e4066Sahrens 			}
2641fa9e4066Sahrens 		}
264206be9802SMatthew Ahrens 		(void) fprintf(stderr, "\n");
2643fa9e4066Sahrens 	}
2644fa9e4066Sahrens 
2645b24ab676SJeff Bonwick 	spa_config_enter(spa, SCL_CONFIG, FTAG, RW_READER);
2646b24ab676SJeff Bonwick 
2647bbfd46c4SJeff Bonwick 	zdb_ddt_leak_init(spa, zcb);
2648b24ab676SJeff Bonwick 
2649b24ab676SJeff Bonwick 	spa_config_exit(spa, SCL_CONFIG, FTAG);
2650b24ab676SJeff Bonwick }
2651b24ab676SJeff Bonwick 
2652fa9e4066Sahrens static void
2653e14bb325SJeff Bonwick zdb_leak_fini(spa_t *spa)
2654fa9e4066Sahrens {
2655b24ab676SJeff Bonwick 	if (!dump_opt['L']) {
2656fa9e4066Sahrens 		vdev_t *rvd = spa->spa_root_vdev;
2657e14bb325SJeff Bonwick 		for (int c = 0; c < rvd->vdev_children; c++) {
2658e14bb325SJeff Bonwick 			vdev_t *vd = rvd->vdev_child[c];
2659e14bb325SJeff Bonwick 			for (int m = 0; m < vd->vdev_ms_count; m++) {
2660e14bb325SJeff Bonwick 				metaslab_t *msp = vd->vdev_ms[m];
2661e14bb325SJeff Bonwick 				mutex_enter(&msp->ms_lock);
26620713e232SGeorge Wilson 
26630713e232SGeorge Wilson 				/*
26640713e232SGeorge Wilson 				 * The ms_tree has been overloaded to
26650713e232SGeorge Wilson 				 * contain allocated segments. Now that we
26660713e232SGeorge Wilson 				 * finished traversing all blocks, any
26670713e232SGeorge Wilson 				 * block that remains in the ms_tree
26680713e232SGeorge Wilson 				 * represents an allocated block that we
26690713e232SGeorge Wilson 				 * did not claim during the traversal.
26700713e232SGeorge Wilson 				 * Claimed blocks would have been removed
26710713e232SGeorge Wilson 				 * from the ms_tree.
26720713e232SGeorge Wilson 				 */
26730713e232SGeorge Wilson 				range_tree_vacate(msp->ms_tree, zdb_leak, vd);
26740713e232SGeorge Wilson 				msp->ms_loaded = B_FALSE;
26750713e232SGeorge Wilson 
2676e14bb325SJeff Bonwick 				mutex_exit(&msp->ms_lock);
2677e14bb325SJeff Bonwick 			}
2678e14bb325SJeff Bonwick 		}
2679fa9e4066Sahrens 	}
2680fa9e4066Sahrens }
2681fa9e4066Sahrens 
2682cde58dbcSMatthew Ahrens /* ARGSUSED */
2683cde58dbcSMatthew Ahrens static int
2684cde58dbcSMatthew Ahrens count_block_cb(void *arg, const blkptr_t *bp, dmu_tx_t *tx)
2685cde58dbcSMatthew Ahrens {
2686cde58dbcSMatthew Ahrens 	zdb_cb_t *zcb = arg;
2687cde58dbcSMatthew Ahrens 
2688490d05b9SMatthew Ahrens 	if (dump_opt['b'] >= 5) {
2689cde58dbcSMatthew Ahrens 		char blkbuf[BP_SPRINTF_LEN];
269043466aaeSMax Grossman 		snprintf_blkptr(blkbuf, sizeof (blkbuf), bp);
2691cde58dbcSMatthew Ahrens 		(void) printf("[%s] %s\n",
2692cde58dbcSMatthew Ahrens 		    "deferred free", blkbuf);
2693cde58dbcSMatthew Ahrens 	}
2694cde58dbcSMatthew Ahrens 	zdb_count_block(zcb, NULL, bp, ZDB_OT_DEFERRED);
2695cde58dbcSMatthew Ahrens 	return (0);
2696cde58dbcSMatthew Ahrens }
2697cde58dbcSMatthew Ahrens 
2698fa9e4066Sahrens static int
2699fa9e4066Sahrens dump_block_stats(spa_t *spa)
2700fa9e4066Sahrens {
2701fa9e4066Sahrens 	zdb_cb_t zcb = { 0 };
2702fa9e4066Sahrens 	zdb_blkstats_t *zb, *tzb;
2703b24ab676SJeff Bonwick 	uint64_t norm_alloc, norm_space, total_alloc, total_found;
2704cd088ea4SVictor Latushkin 	int flags = TRAVERSE_PRE | TRAVERSE_PREFETCH_METADATA | TRAVERSE_HARD;
27055d7b4d43SMatthew Ahrens 	boolean_t leaks = B_FALSE;
2706fa9e4066Sahrens 
2707490d05b9SMatthew Ahrens 	(void) printf("\nTraversing all blocks %s%s%s%s%s...\n\n",
270882a0a985SVictor Latushkin 	    (dump_opt['c'] || !dump_opt['L']) ? "to verify " : "",
27096365109dSVictor Latushkin 	    (dump_opt['c'] == 1) ? "metadata " : "",
271082a0a985SVictor Latushkin 	    dump_opt['c'] ? "checksums " : "",
271182a0a985SVictor Latushkin 	    (dump_opt['c'] && !dump_opt['L']) ? "and verify " : "",
271282a0a985SVictor Latushkin 	    !dump_opt['L'] ? "nothing leaked " : "");
2713fa9e4066Sahrens 
2714fa9e4066Sahrens 	/*
2715e14bb325SJeff Bonwick 	 * Load all space maps as SM_ALLOC maps, then traverse the pool
2716e14bb325SJeff Bonwick 	 * claiming each block we discover.  If the pool is perfectly
2717e14bb325SJeff Bonwick 	 * consistent, the space maps will be empty when we're done.
2718e14bb325SJeff Bonwick 	 * Anything left over is a leak; any block we can't claim (because
2719e14bb325SJeff Bonwick 	 * it's not part of any space map) is a double allocation,
2720e14bb325SJeff Bonwick 	 * reference to a freed block, or an unclaimed log block.
2721fa9e4066Sahrens 	 */
2722b24ab676SJeff Bonwick 	zdb_leak_init(spa, &zcb);
2723fa9e4066Sahrens 
2724fa9e4066Sahrens 	/*
2725fa9e4066Sahrens 	 * If there's a deferred-free bplist, process that first.
2726fa9e4066Sahrens 	 */
2727cde58dbcSMatthew Ahrens 	(void) bpobj_iterate_nofree(&spa->spa_deferred_bpobj,
2728cde58dbcSMatthew Ahrens 	    count_block_cb, &zcb, NULL);
27293b2aab18SMatthew Ahrens 	if (spa_version(spa) >= SPA_VERSION_DEADLISTS) {
2730cde58dbcSMatthew Ahrens 		(void) bpobj_iterate_nofree(&spa->spa_dsl_pool->dp_free_bpobj,
2731cde58dbcSMatthew Ahrens 		    count_block_cb, &zcb, NULL);
27323b2aab18SMatthew Ahrens 	}
27332acef22dSMatthew Ahrens 	if (spa_feature_is_active(spa, SPA_FEATURE_ASYNC_DESTROY)) {
2734b420f3adSRichard Lowe 		VERIFY3U(0, ==, bptree_iterate(spa->spa_meta_objset,
2735ad135b5dSChristopher Siden 		    spa->spa_dsl_pool->dp_bptree_obj, B_FALSE, count_block_cb,
2736ad135b5dSChristopher Siden 		    &zcb, NULL));
2737ad135b5dSChristopher Siden 	}
2738fa9e4066Sahrens 
2739bbfd46c4SJeff Bonwick 	if (dump_opt['c'] > 1)
2740bbfd46c4SJeff Bonwick 		flags |= TRAVERSE_PREFETCH_DATA;
2741bbfd46c4SJeff Bonwick 
2742490d05b9SMatthew Ahrens 	zcb.zcb_totalasize = metaslab_class_get_alloc(spa_normal_class(spa));
2743490d05b9SMatthew Ahrens 	zcb.zcb_start = zcb.zcb_lastprint = gethrtime();
2744bbfd46c4SJeff Bonwick 	zcb.zcb_haderrors |= traverse_pool(spa, 0, flags, zdb_blkptr_cb, &zcb);
2745fa9e4066Sahrens 
274631d7e8faSGeorge Wilson 	/*
274731d7e8faSGeorge Wilson 	 * If we've traversed the data blocks then we need to wait for those
274831d7e8faSGeorge Wilson 	 * I/Os to complete. We leverage "The Godfather" zio to wait on
274931d7e8faSGeorge Wilson 	 * all async I/Os to complete.
275031d7e8faSGeorge Wilson 	 */
275131d7e8faSGeorge Wilson 	if (dump_opt['c']) {
27526f834bc1SMatthew Ahrens 		for (int i = 0; i < max_ncpus; i++) {
27536f834bc1SMatthew Ahrens 			(void) zio_wait(spa->spa_async_zio_root[i]);
27546f834bc1SMatthew Ahrens 			spa->spa_async_zio_root[i] = zio_root(spa, NULL, NULL,
275531d7e8faSGeorge Wilson 			    ZIO_FLAG_CANFAIL | ZIO_FLAG_SPECULATIVE |
275631d7e8faSGeorge Wilson 			    ZIO_FLAG_GODFATHER);
275731d7e8faSGeorge Wilson 		}
27586f834bc1SMatthew Ahrens 	}
275931d7e8faSGeorge Wilson 
2760b24ab676SJeff Bonwick 	if (zcb.zcb_haderrors) {
2761fa9e4066Sahrens 		(void) printf("\nError counts:\n\n");
2762fa9e4066Sahrens 		(void) printf("\t%5s  %s\n", "errno", "count");
2763b24ab676SJeff Bonwick 		for (int e = 0; e < 256; e++) {
2764fa9e4066Sahrens 			if (zcb.zcb_errors[e] != 0) {
2765fa9e4066Sahrens 				(void) printf("\t%5d  %llu\n",
2766fa9e4066Sahrens 				    e, (u_longlong_t)zcb.zcb_errors[e]);
2767fa9e4066Sahrens 			}
2768fa9e4066Sahrens 		}
2769fa9e4066Sahrens 	}
2770fa9e4066Sahrens 
2771fa9e4066Sahrens 	/*
2772fa9e4066Sahrens 	 * Report any leaked segments.
2773fa9e4066Sahrens 	 */
2774e14bb325SJeff Bonwick 	zdb_leak_fini(spa);
2775fa9e4066Sahrens 
2776b24ab676SJeff Bonwick 	tzb = &zcb.zcb_type[ZB_TOTAL][ZDB_OT_TOTAL];
2777d41e7643Sek110237 
2778b24ab676SJeff Bonwick 	norm_alloc = metaslab_class_get_alloc(spa_normal_class(spa));
2779b24ab676SJeff Bonwick 	norm_space = metaslab_class_get_space(spa_normal_class(spa));
2780fa9e4066Sahrens 
2781b24ab676SJeff Bonwick 	total_alloc = norm_alloc + metaslab_class_get_alloc(spa_log_class(spa));
2782b24ab676SJeff Bonwick 	total_found = tzb->zb_asize - zcb.zcb_dedup_asize;
27838654d025Sperrin 
2784b24ab676SJeff Bonwick 	if (total_found == total_alloc) {
278582a0a985SVictor Latushkin 		if (!dump_opt['L'])
2786fa9e4066Sahrens 			(void) printf("\n\tNo leaks (block sum matches space"
2787fa9e4066Sahrens 			    " maps exactly)\n");
2788fa9e4066Sahrens 	} else {
2789fa9e4066Sahrens 		(void) printf("block traversal size %llu != alloc %llu "
279082a0a985SVictor Latushkin 		    "(%s %lld)\n",
2791b24ab676SJeff Bonwick 		    (u_longlong_t)total_found,
2792b24ab676SJeff Bonwick 		    (u_longlong_t)total_alloc,
279382a0a985SVictor Latushkin 		    (dump_opt['L']) ? "unreachable" : "leaked",
2794b24ab676SJeff Bonwick 		    (longlong_t)(total_alloc - total_found));
27955d7b4d43SMatthew Ahrens 		leaks = B_TRUE;
2796fa9e4066Sahrens 	}
2797fa9e4066Sahrens 
2798fa9e4066Sahrens 	if (tzb->zb_count == 0)
2799fa9e4066Sahrens 		return (2);
2800fa9e4066Sahrens 
2801fa9e4066Sahrens 	(void) printf("\n");
2802fa9e4066Sahrens 	(void) printf("\tbp count:      %10llu\n",
2803fa9e4066Sahrens 	    (u_longlong_t)tzb->zb_count);
2804d5ee8a13SMatthew Ahrens 	(void) printf("\tganged count:  %10llu\n",
2805d5ee8a13SMatthew Ahrens 	    (longlong_t)tzb->zb_gangs);
2806b24ab676SJeff Bonwick 	(void) printf("\tbp logical:    %10llu      avg: %6llu\n",
2807fa9e4066Sahrens 	    (u_longlong_t)tzb->zb_lsize,
2808fa9e4066Sahrens 	    (u_longlong_t)(tzb->zb_lsize / tzb->zb_count));
2809b24ab676SJeff Bonwick 	(void) printf("\tbp physical:   %10llu      avg:"
2810b24ab676SJeff Bonwick 	    " %6llu     compression: %6.2f\n",
2811fa9e4066Sahrens 	    (u_longlong_t)tzb->zb_psize,
2812fa9e4066Sahrens 	    (u_longlong_t)(tzb->zb_psize / tzb->zb_count),
2813fa9e4066Sahrens 	    (double)tzb->zb_lsize / tzb->zb_psize);
2814b24ab676SJeff Bonwick 	(void) printf("\tbp allocated:  %10llu      avg:"
2815b24ab676SJeff Bonwick 	    " %6llu     compression: %6.2f\n",
2816fa9e4066Sahrens 	    (u_longlong_t)tzb->zb_asize,
2817fa9e4066Sahrens 	    (u_longlong_t)(tzb->zb_asize / tzb->zb_count),
2818fa9e4066Sahrens 	    (double)tzb->zb_lsize / tzb->zb_asize);
2819b24ab676SJeff Bonwick 	(void) printf("\tbp deduped:    %10llu    ref>1:"
2820b24ab676SJeff Bonwick 	    " %6llu   deduplication: %6.2f\n",
2821b24ab676SJeff Bonwick 	    (u_longlong_t)zcb.zcb_dedup_asize,
2822b24ab676SJeff Bonwick 	    (u_longlong_t)zcb.zcb_dedup_blocks,
2823b24ab676SJeff Bonwick 	    (double)zcb.zcb_dedup_asize / tzb->zb_asize + 1.0);
2824b24ab676SJeff Bonwick 	(void) printf("\tSPA allocated: %10llu     used: %5.2f%%\n",
2825b24ab676SJeff Bonwick 	    (u_longlong_t)norm_alloc, 100.0 * norm_alloc / norm_space);
2826fa9e4066Sahrens 
28275d7b4d43SMatthew Ahrens 	for (bp_embedded_type_t i = 0; i < NUM_BP_EMBEDDED_TYPES; i++) {
28285d7b4d43SMatthew Ahrens 		if (zcb.zcb_embedded_blocks[i] == 0)
28295d7b4d43SMatthew Ahrens 			continue;
28305d7b4d43SMatthew Ahrens 		(void) printf("\n");
28315d7b4d43SMatthew Ahrens 		(void) printf("\tadditional, non-pointer bps of type %u: "
28325d7b4d43SMatthew Ahrens 		    "%10llu\n",
28335d7b4d43SMatthew Ahrens 		    i, (u_longlong_t)zcb.zcb_embedded_blocks[i]);
28345d7b4d43SMatthew Ahrens 
28355d7b4d43SMatthew Ahrens 		if (dump_opt['b'] >= 3) {
28365d7b4d43SMatthew Ahrens 			(void) printf("\t number of (compressed) bytes:  "
28375d7b4d43SMatthew Ahrens 			    "number of bps\n");
28385d7b4d43SMatthew Ahrens 			dump_histogram(zcb.zcb_embedded_histogram[i],
28395d7b4d43SMatthew Ahrens 			    sizeof (zcb.zcb_embedded_histogram[i]) /
28405d7b4d43SMatthew Ahrens 			    sizeof (zcb.zcb_embedded_histogram[i][0]), 0);
28415d7b4d43SMatthew Ahrens 		}
28425d7b4d43SMatthew Ahrens 	}
28435d7b4d43SMatthew Ahrens 
2844d5ee8a13SMatthew Ahrens 	if (tzb->zb_ditto_samevdev != 0) {
2845d5ee8a13SMatthew Ahrens 		(void) printf("\tDittoed blocks on same vdev: %llu\n",
2846d5ee8a13SMatthew Ahrens 		    (longlong_t)tzb->zb_ditto_samevdev);
2847d5ee8a13SMatthew Ahrens 	}
2848d5ee8a13SMatthew Ahrens 
2849fa9e4066Sahrens 	if (dump_opt['b'] >= 2) {
2850fa9e4066Sahrens 		int l, t, level;
2851fa9e4066Sahrens 		(void) printf("\nBlocks\tLSIZE\tPSIZE\tASIZE"
2852fa9e4066Sahrens 		    "\t  avg\t comp\t%%Total\tType\n");
2853fa9e4066Sahrens 
2854b24ab676SJeff Bonwick 		for (t = 0; t <= ZDB_OT_TOTAL; t++) {
28553f9d6ad7SLin Ling 			char csize[32], lsize[32], psize[32], asize[32];
2856d5ee8a13SMatthew Ahrens 			char avg[32], gang[32];
2857fa9e4066Sahrens 			char *typename;
2858fa9e4066Sahrens 
2859*fc30d466SJason King 			/* make sure nicenum has enough space */
2860*fc30d466SJason King 			CTASSERT(sizeof (csize) >= NN_NUMBUF_SZ);
2861*fc30d466SJason King 			CTASSERT(sizeof (lsize) >= NN_NUMBUF_SZ);
2862*fc30d466SJason King 			CTASSERT(sizeof (psize) >= NN_NUMBUF_SZ);
2863*fc30d466SJason King 			CTASSERT(sizeof (asize) >= NN_NUMBUF_SZ);
2864*fc30d466SJason King 			CTASSERT(sizeof (avg) >= NN_NUMBUF_SZ);
2865*fc30d466SJason King 			CTASSERT(sizeof (gang) >= NN_NUMBUF_SZ);
2866*fc30d466SJason King 
2867b24ab676SJeff Bonwick 			if (t < DMU_OT_NUMTYPES)
2868b24ab676SJeff Bonwick 				typename = dmu_ot[t].ot_name;
2869b24ab676SJeff Bonwick 			else
2870b24ab676SJeff Bonwick 				typename = zdb_ot_extname[t - DMU_OT_NUMTYPES];
2871fa9e4066Sahrens 
2872fa9e4066Sahrens 			if (zcb.zcb_type[ZB_TOTAL][t].zb_asize == 0) {
2873fa9e4066Sahrens 				(void) printf("%6s\t%5s\t%5s\t%5s"
2874fa9e4066Sahrens 				    "\t%5s\t%5s\t%6s\t%s\n",
2875fa9e4066Sahrens 				    "-",
2876fa9e4066Sahrens 				    "-",
2877fa9e4066Sahrens 				    "-",
2878fa9e4066Sahrens 				    "-",
2879fa9e4066Sahrens 				    "-",
2880fa9e4066Sahrens 				    "-",
2881fa9e4066Sahrens 				    "-",
2882fa9e4066Sahrens 				    typename);
2883fa9e4066Sahrens 				continue;
2884fa9e4066Sahrens 			}
2885fa9e4066Sahrens 
2886fa9e4066Sahrens 			for (l = ZB_TOTAL - 1; l >= -1; l--) {
2887fa9e4066Sahrens 				level = (l == -1 ? ZB_TOTAL : l);
2888fa9e4066Sahrens 				zb = &zcb.zcb_type[level][t];
2889fa9e4066Sahrens 
2890fa9e4066Sahrens 				if (zb->zb_asize == 0)
2891fa9e4066Sahrens 					continue;
2892fa9e4066Sahrens 
2893fa9e4066Sahrens 				if (dump_opt['b'] < 3 && level != ZB_TOTAL)
2894fa9e4066Sahrens 					continue;
2895fa9e4066Sahrens 
2896fa9e4066Sahrens 				if (level == 0 && zb->zb_asize ==
2897fa9e4066Sahrens 				    zcb.zcb_type[ZB_TOTAL][t].zb_asize)
2898fa9e4066Sahrens 					continue;
2899fa9e4066Sahrens 
2900*fc30d466SJason King 				zdb_nicenum(zb->zb_count, csize,
2901*fc30d466SJason King 				    sizeof (csize));
2902*fc30d466SJason King 				zdb_nicenum(zb->zb_lsize, lsize,
2903*fc30d466SJason King 				    sizeof (lsize));
2904*fc30d466SJason King 				zdb_nicenum(zb->zb_psize, psize,
2905*fc30d466SJason King 				    sizeof (psize));
2906*fc30d466SJason King 				zdb_nicenum(zb->zb_asize, asize,
2907*fc30d466SJason King 				    sizeof (asize));
2908*fc30d466SJason King 				zdb_nicenum(zb->zb_asize / zb->zb_count, avg,
2909*fc30d466SJason King 				    sizeof (avg));
2910*fc30d466SJason King 				zdb_nicenum(zb->zb_gangs, gang, sizeof (gang));
2911fa9e4066Sahrens 
2912fa9e4066Sahrens 				(void) printf("%6s\t%5s\t%5s\t%5s\t%5s"
2913fa9e4066Sahrens 				    "\t%5.2f\t%6.2f\t",
2914fa9e4066Sahrens 				    csize, lsize, psize, asize, avg,
2915fa9e4066Sahrens 				    (double)zb->zb_lsize / zb->zb_psize,
2916fa9e4066Sahrens 				    100.0 * zb->zb_asize / tzb->zb_asize);
2917fa9e4066Sahrens 
2918fa9e4066Sahrens 				if (level == ZB_TOTAL)
2919fa9e4066Sahrens 					(void) printf("%s\n", typename);
2920fa9e4066Sahrens 				else
2921fa9e4066Sahrens 					(void) printf("    L%d %s\n",
2922fa9e4066Sahrens 					    level, typename);
2923490d05b9SMatthew Ahrens 
2924d5ee8a13SMatthew Ahrens 				if (dump_opt['b'] >= 3 && zb->zb_gangs > 0) {
2925d5ee8a13SMatthew Ahrens 					(void) printf("\t number of ganged "
2926d5ee8a13SMatthew Ahrens 					    "blocks: %s\n", gang);
2927d5ee8a13SMatthew Ahrens 				}
2928d5ee8a13SMatthew Ahrens 
2929490d05b9SMatthew Ahrens 				if (dump_opt['b'] >= 4) {
2930490d05b9SMatthew Ahrens 					(void) printf("psize "
2931490d05b9SMatthew Ahrens 					    "(in 512-byte sectors): "
2932490d05b9SMatthew Ahrens 					    "number of blocks\n");
2933490d05b9SMatthew Ahrens 					dump_histogram(zb->zb_psize_histogram,
29340713e232SGeorge Wilson 					    PSIZE_HISTO_SIZE, 0);
2935490d05b9SMatthew Ahrens 				}
2936fa9e4066Sahrens 			}
2937fa9e4066Sahrens 		}
2938fa9e4066Sahrens 	}
2939fa9e4066Sahrens 
2940fa9e4066Sahrens 	(void) printf("\n");
2941fa9e4066Sahrens 
2942fa9e4066Sahrens 	if (leaks)
2943fa9e4066Sahrens 		return (2);
2944fa9e4066Sahrens 
2945fa9e4066Sahrens 	if (zcb.zcb_haderrors)
2946fa9e4066Sahrens 		return (3);
2947fa9e4066Sahrens 
2948fa9e4066Sahrens 	return (0);
2949fa9e4066Sahrens }
2950fa9e4066Sahrens 
2951b24ab676SJeff Bonwick typedef struct zdb_ddt_entry {
2952b24ab676SJeff Bonwick 	ddt_key_t	zdde_key;
2953b24ab676SJeff Bonwick 	uint64_t	zdde_ref_blocks;
2954b24ab676SJeff Bonwick 	uint64_t	zdde_ref_lsize;
2955b24ab676SJeff Bonwick 	uint64_t	zdde_ref_psize;
2956b24ab676SJeff Bonwick 	uint64_t	zdde_ref_dsize;
2957b24ab676SJeff Bonwick 	avl_node_t	zdde_node;
2958b24ab676SJeff Bonwick } zdb_ddt_entry_t;
2959b24ab676SJeff Bonwick 
2960b24ab676SJeff Bonwick /* ARGSUSED */
2961b24ab676SJeff Bonwick static int
2962b24ab676SJeff Bonwick zdb_ddt_add_cb(spa_t *spa, zilog_t *zilog, const blkptr_t *bp,
29637802d7bfSMatthew Ahrens     const zbookmark_phys_t *zb, const dnode_phys_t *dnp, void *arg)
2964b24ab676SJeff Bonwick {
2965b24ab676SJeff Bonwick 	avl_tree_t *t = arg;
2966b24ab676SJeff Bonwick 	avl_index_t where;
2967b24ab676SJeff Bonwick 	zdb_ddt_entry_t *zdde, zdde_search;
2968b24ab676SJeff Bonwick 
2969a2cdcdd2SPaul Dagnelie 	if (bp == NULL || BP_IS_HOLE(bp) || BP_IS_EMBEDDED(bp))
2970b24ab676SJeff Bonwick 		return (0);
2971b24ab676SJeff Bonwick 
2972b24ab676SJeff Bonwick 	if (dump_opt['S'] > 1 && zb->zb_level == ZB_ROOT_LEVEL) {
2973b24ab676SJeff Bonwick 		(void) printf("traversing objset %llu, %llu objects, "
2974b24ab676SJeff Bonwick 		    "%lu blocks so far\n",
2975b24ab676SJeff Bonwick 		    (u_longlong_t)zb->zb_objset,
29765d7b4d43SMatthew Ahrens 		    (u_longlong_t)BP_GET_FILL(bp),
2977b24ab676SJeff Bonwick 		    avl_numnodes(t));
2978b24ab676SJeff Bonwick 	}
2979b24ab676SJeff Bonwick 
2980bbfd46c4SJeff Bonwick 	if (BP_IS_HOLE(bp) || BP_GET_CHECKSUM(bp) == ZIO_CHECKSUM_OFF ||
2981ad135b5dSChristopher Siden 	    BP_GET_LEVEL(bp) > 0 || DMU_OT_IS_METADATA(BP_GET_TYPE(bp)))
2982b24ab676SJeff Bonwick 		return (0);
2983b24ab676SJeff Bonwick 
2984b24ab676SJeff Bonwick 	ddt_key_fill(&zdde_search.zdde_key, bp);
2985b24ab676SJeff Bonwick 
2986b24ab676SJeff Bonwick 	zdde = avl_find(t, &zdde_search, &where);
2987b24ab676SJeff Bonwick 
2988b24ab676SJeff Bonwick 	if (zdde == NULL) {
2989b24ab676SJeff Bonwick 		zdde = umem_zalloc(sizeof (*zdde), UMEM_NOFAIL);
2990b24ab676SJeff Bonwick 		zdde->zdde_key = zdde_search.zdde_key;
2991b24ab676SJeff Bonwick 		avl_insert(t, zdde, where);
2992b24ab676SJeff Bonwick 	}
2993b24ab676SJeff Bonwick 
2994b24ab676SJeff Bonwick 	zdde->zdde_ref_blocks += 1;
2995b24ab676SJeff Bonwick 	zdde->zdde_ref_lsize += BP_GET_LSIZE(bp);
2996b24ab676SJeff Bonwick 	zdde->zdde_ref_psize += BP_GET_PSIZE(bp);
2997b24ab676SJeff Bonwick 	zdde->zdde_ref_dsize += bp_get_dsize_sync(spa, bp);
2998b24ab676SJeff Bonwick 
2999b24ab676SJeff Bonwick 	return (0);
3000b24ab676SJeff Bonwick }
3001b24ab676SJeff Bonwick 
3002b24ab676SJeff Bonwick static void
3003b24ab676SJeff Bonwick dump_simulated_ddt(spa_t *spa)
3004b24ab676SJeff Bonwick {
3005b24ab676SJeff Bonwick 	avl_tree_t t;
3006b24ab676SJeff Bonwick 	void *cookie = NULL;
3007b24ab676SJeff Bonwick 	zdb_ddt_entry_t *zdde;
3008b24ab676SJeff Bonwick 	ddt_histogram_t ddh_total = { 0 };
3009b24ab676SJeff Bonwick 	ddt_stat_t dds_total = { 0 };
3010b24ab676SJeff Bonwick 
3011b24ab676SJeff Bonwick 	avl_create(&t, ddt_entry_compare,
3012b24ab676SJeff Bonwick 	    sizeof (zdb_ddt_entry_t), offsetof(zdb_ddt_entry_t, zdde_node));
3013b24ab676SJeff Bonwick 
3014b24ab676SJeff Bonwick 	spa_config_enter(spa, SCL_CONFIG, FTAG, RW_READER);
3015b24ab676SJeff Bonwick 
3016bbfd46c4SJeff Bonwick 	(void) traverse_pool(spa, 0, TRAVERSE_PRE | TRAVERSE_PREFETCH_METADATA,
3017bbfd46c4SJeff Bonwick 	    zdb_ddt_add_cb, &t);
3018b24ab676SJeff Bonwick 
3019b24ab676SJeff Bonwick 	spa_config_exit(spa, SCL_CONFIG, FTAG);
3020b24ab676SJeff Bonwick 
3021b24ab676SJeff Bonwick 	while ((zdde = avl_destroy_nodes(&t, &cookie)) != NULL) {
3022b24ab676SJeff Bonwick 		ddt_stat_t dds;
3023b24ab676SJeff Bonwick 		uint64_t refcnt = zdde->zdde_ref_blocks;
3024b24ab676SJeff Bonwick 		ASSERT(refcnt != 0);
3025b24ab676SJeff Bonwick 
3026b24ab676SJeff Bonwick 		dds.dds_blocks = zdde->zdde_ref_blocks / refcnt;
3027b24ab676SJeff Bonwick 		dds.dds_lsize = zdde->zdde_ref_lsize / refcnt;
3028b24ab676SJeff Bonwick 		dds.dds_psize = zdde->zdde_ref_psize / refcnt;
3029b24ab676SJeff Bonwick 		dds.dds_dsize = zdde->zdde_ref_dsize / refcnt;
3030b24ab676SJeff Bonwick 
3031b24ab676SJeff Bonwick 		dds.dds_ref_blocks = zdde->zdde_ref_blocks;
3032b24ab676SJeff Bonwick 		dds.dds_ref_lsize = zdde->zdde_ref_lsize;
3033b24ab676SJeff Bonwick 		dds.dds_ref_psize = zdde->zdde_ref_psize;
3034b24ab676SJeff Bonwick 		dds.dds_ref_dsize = zdde->zdde_ref_dsize;
3035b24ab676SJeff Bonwick 
3036bf16b11eSMatthew Ahrens 		ddt_stat_add(&ddh_total.ddh_stat[highbit64(refcnt) - 1],
3037bf16b11eSMatthew Ahrens 		    &dds, 0);
3038b24ab676SJeff Bonwick 
3039b24ab676SJeff Bonwick 		umem_free(zdde, sizeof (*zdde));
3040b24ab676SJeff Bonwick 	}
3041b24ab676SJeff Bonwick 
3042b24ab676SJeff Bonwick 	avl_destroy(&t);
3043b24ab676SJeff Bonwick 
3044b24ab676SJeff Bonwick 	ddt_histogram_stat(&dds_total, &ddh_total);
3045b24ab676SJeff Bonwick 
3046b24ab676SJeff Bonwick 	(void) printf("Simulated DDT histogram:\n");
3047b24ab676SJeff Bonwick 
30489eb19f4dSGeorge Wilson 	zpool_dump_ddt(&dds_total, &ddh_total);
3049b24ab676SJeff Bonwick 
3050b24ab676SJeff Bonwick 	dump_dedup_ratio(&dds_total);
3051b24ab676SJeff Bonwick }
3052b24ab676SJeff Bonwick 
3053fa9e4066Sahrens static void
3054fa9e4066Sahrens dump_zpool(spa_t *spa)
3055fa9e4066Sahrens {
3056fa9e4066Sahrens 	dsl_pool_t *dp = spa_get_dsl(spa);
3057fa9e4066Sahrens 	int rc = 0;
3058fa9e4066Sahrens 
3059b24ab676SJeff Bonwick 	if (dump_opt['S']) {
3060b24ab676SJeff Bonwick 		dump_simulated_ddt(spa);
3061b24ab676SJeff Bonwick 		return;
3062b24ab676SJeff Bonwick 	}
3063b24ab676SJeff Bonwick 
306407428bdfSVictor Latushkin 	if (!dump_opt['e'] && dump_opt['C'] > 1) {
306507428bdfSVictor Latushkin 		(void) printf("\nCached configuration:\n");
306607428bdfSVictor Latushkin 		dump_nvlist(spa->spa_config, 8);
306707428bdfSVictor Latushkin 	}
306807428bdfSVictor Latushkin 
306907428bdfSVictor Latushkin 	if (dump_opt['C'])
307007428bdfSVictor Latushkin 		dump_config(spa);
307107428bdfSVictor Latushkin 
3072fa9e4066Sahrens 	if (dump_opt['u'])
307353b9a4a9SVictor Latushkin 		dump_uberblock(&spa->spa_uberblock, "\nUberblock:\n", "\n");
3074fa9e4066Sahrens 
3075b24ab676SJeff Bonwick 	if (dump_opt['D'])
3076b24ab676SJeff Bonwick 		dump_all_ddts(spa);
3077b24ab676SJeff Bonwick 
307887219db7SVictor Latushkin 	if (dump_opt['d'] > 2 || dump_opt['m'])
307987219db7SVictor Latushkin 		dump_metaslabs(spa);
30802e4c9986SGeorge Wilson 	if (dump_opt['M'])
30812e4c9986SGeorge Wilson 		dump_metaslab_groups(spa);
308287219db7SVictor Latushkin 
308387219db7SVictor Latushkin 	if (dump_opt['d'] || dump_opt['i']) {
3084fa9e4066Sahrens 		dump_dir(dp->dp_meta_objset);
3085fa9e4066Sahrens 		if (dump_opt['d'] >= 3) {
3086732885fcSMatthew Ahrens 			dump_full_bpobj(&spa->spa_deferred_bpobj,
3087d0475637SMatthew Ahrens 			    "Deferred frees", 0);
3088cde58dbcSMatthew Ahrens 			if (spa_version(spa) >= SPA_VERSION_DEADLISTS) {
3089732885fcSMatthew Ahrens 				dump_full_bpobj(
3090732885fcSMatthew Ahrens 				    &spa->spa_dsl_pool->dp_free_bpobj,
3091d0475637SMatthew Ahrens 				    "Pool snapshot frees", 0);
3092ad135b5dSChristopher Siden 			}
3093ad135b5dSChristopher Siden 
3094ad135b5dSChristopher Siden 			if (spa_feature_is_active(spa,
30952acef22dSMatthew Ahrens 			    SPA_FEATURE_ASYNC_DESTROY)) {
3096ad135b5dSChristopher Siden 				dump_bptree(spa->spa_meta_objset,
3097ad135b5dSChristopher Siden 				    spa->spa_dsl_pool->dp_bptree_obj,
3098ad135b5dSChristopher Siden 				    "Pool dataset frees");
3099cde58dbcSMatthew Ahrens 			}
3100fa9e4066Sahrens 			dump_dtl(spa->spa_root_vdev, 0);
3101fa9e4066Sahrens 		}
310207428bdfSVictor Latushkin 		(void) dmu_objset_find(spa_name(spa), dump_one_dir,
310307428bdfSVictor Latushkin 		    NULL, DS_FIND_SNAPSHOTS | DS_FIND_CHILDREN);
3104b5152584SMatthew Ahrens 
3105ca0cc391SMatthew Ahrens 		for (spa_feature_t f = 0; f < SPA_FEATURES; f++) {
3106ca0cc391SMatthew Ahrens 			uint64_t refcount;
3107ca0cc391SMatthew Ahrens 
3108ca0cc391SMatthew Ahrens 			if (!(spa_feature_table[f].fi_flags &
3109ca0cc391SMatthew Ahrens 			    ZFEATURE_FLAG_PER_DATASET)) {
3110ca0cc391SMatthew Ahrens 				ASSERT0(dataset_feature_count[f]);
3111ca0cc391SMatthew Ahrens 				continue;
3112ca0cc391SMatthew Ahrens 			}
3113b5152584SMatthew Ahrens 			(void) feature_get_refcount(spa,
3114ca0cc391SMatthew Ahrens 			    &spa_feature_table[f], &refcount);
3115ca0cc391SMatthew Ahrens 			if (dataset_feature_count[f] != refcount) {
3116ca0cc391SMatthew Ahrens 				(void) printf("%s feature refcount mismatch: "
3117ca0cc391SMatthew Ahrens 				    "%lld datasets != %lld refcount\n",
3118ca0cc391SMatthew Ahrens 				    spa_feature_table[f].fi_uname,
3119ca0cc391SMatthew Ahrens 				    (longlong_t)dataset_feature_count[f],
3120b5152584SMatthew Ahrens 				    (longlong_t)refcount);
3121b5152584SMatthew Ahrens 				rc = 2;
3122b5152584SMatthew Ahrens 			} else {
3123ca0cc391SMatthew Ahrens 				(void) printf("Verified %s feature refcount "
3124ca0cc391SMatthew Ahrens 				    "of %llu is correct\n",
3125ca0cc391SMatthew Ahrens 				    spa_feature_table[f].fi_uname,
3126ca0cc391SMatthew Ahrens 				    (longlong_t)refcount);
3127ca0cc391SMatthew Ahrens 			}
3128fa9e4066Sahrens 		}
3129b5152584SMatthew Ahrens 	}
3130b5152584SMatthew Ahrens 	if (rc == 0 && (dump_opt['b'] || dump_opt['c']))
3131fa9e4066Sahrens 		rc = dump_block_stats(spa);
3132fa9e4066Sahrens 
31330713e232SGeorge Wilson 	if (rc == 0)
31340713e232SGeorge Wilson 		rc = verify_spacemap_refcounts(spa);
31350713e232SGeorge Wilson 
3136fa9e4066Sahrens 	if (dump_opt['s'])
3137fa9e4066Sahrens 		show_pool_stats(spa);
3138fa9e4066Sahrens 
31398f18d1faSGeorge Wilson 	if (dump_opt['h'])
31408f18d1faSGeorge Wilson 		dump_history(spa);
31418f18d1faSGeorge Wilson 
3142fa9e4066Sahrens 	if (rc != 0)
3143fa9e4066Sahrens 		exit(rc);
3144fa9e4066Sahrens }
3145fa9e4066Sahrens 
314644cd46caSbillm #define	ZDB_FLAG_CHECKSUM	0x0001
314744cd46caSbillm #define	ZDB_FLAG_DECOMPRESS	0x0002
314844cd46caSbillm #define	ZDB_FLAG_BSWAP		0x0004
314944cd46caSbillm #define	ZDB_FLAG_GBH		0x0008
315044cd46caSbillm #define	ZDB_FLAG_INDIRECT	0x0010
315144cd46caSbillm #define	ZDB_FLAG_PHYS		0x0020
315244cd46caSbillm #define	ZDB_FLAG_RAW		0x0040
315344cd46caSbillm #define	ZDB_FLAG_PRINT_BLKPTR	0x0080
315444cd46caSbillm 
315544cd46caSbillm int flagbits[256];
315644cd46caSbillm 
315744cd46caSbillm static void
315844cd46caSbillm zdb_print_blkptr(blkptr_t *bp, int flags)
315944cd46caSbillm {
3160b24ab676SJeff Bonwick 	char blkbuf[BP_SPRINTF_LEN];
316144cd46caSbillm 
316244cd46caSbillm 	if (flags & ZDB_FLAG_BSWAP)
316344cd46caSbillm 		byteswap_uint64_array((void *)bp, sizeof (blkptr_t));
3164b24ab676SJeff Bonwick 
316543466aaeSMax Grossman 	snprintf_blkptr(blkbuf, sizeof (blkbuf), bp);
3166b24ab676SJeff Bonwick 	(void) printf("%s\n", blkbuf);
316744cd46caSbillm }
316844cd46caSbillm 
316944cd46caSbillm static void
317044cd46caSbillm zdb_dump_indirect(blkptr_t *bp, int nbps, int flags)
317144cd46caSbillm {
317244cd46caSbillm 	int i;
317344cd46caSbillm 
317444cd46caSbillm 	for (i = 0; i < nbps; i++)
317544cd46caSbillm 		zdb_print_blkptr(&bp[i], flags);
317644cd46caSbillm }
317744cd46caSbillm 
317844cd46caSbillm static void
317944cd46caSbillm zdb_dump_gbh(void *buf, int flags)
318044cd46caSbillm {
318144cd46caSbillm 	zdb_dump_indirect((blkptr_t *)buf, SPA_GBH_NBLKPTRS, flags);
318244cd46caSbillm }
318344cd46caSbillm 
318444cd46caSbillm static void
318544cd46caSbillm zdb_dump_block_raw(void *buf, uint64_t size, int flags)
318644cd46caSbillm {
318744cd46caSbillm 	if (flags & ZDB_FLAG_BSWAP)
318844cd46caSbillm 		byteswap_uint64_array(buf, size);
3189b24ab676SJeff Bonwick 	(void) write(1, buf, size);
319044cd46caSbillm }
319144cd46caSbillm 
319244cd46caSbillm static void
319344cd46caSbillm zdb_dump_block(char *label, void *buf, uint64_t size, int flags)
319444cd46caSbillm {
319544cd46caSbillm 	uint64_t *d = (uint64_t *)buf;
319644cd46caSbillm 	int nwords = size / sizeof (uint64_t);
319744cd46caSbillm 	int do_bswap = !!(flags & ZDB_FLAG_BSWAP);
319844cd46caSbillm 	int i, j;
319944cd46caSbillm 	char *hdr, *c;
320044cd46caSbillm 
320144cd46caSbillm 
320244cd46caSbillm 	if (do_bswap)
320344cd46caSbillm 		hdr = " 7 6 5 4 3 2 1 0   f e d c b a 9 8";
320444cd46caSbillm 	else
320544cd46caSbillm 		hdr = " 0 1 2 3 4 5 6 7   8 9 a b c d e f";
320644cd46caSbillm 
320744cd46caSbillm 	(void) printf("\n%s\n%6s   %s  0123456789abcdef\n", label, "", hdr);
320844cd46caSbillm 
320944cd46caSbillm 	for (i = 0; i < nwords; i += 2) {
321044cd46caSbillm 		(void) printf("%06llx:  %016llx  %016llx  ",
321144cd46caSbillm 		    (u_longlong_t)(i * sizeof (uint64_t)),
321244cd46caSbillm 		    (u_longlong_t)(do_bswap ? BSWAP_64(d[i]) : d[i]),
321344cd46caSbillm 		    (u_longlong_t)(do_bswap ? BSWAP_64(d[i + 1]) : d[i + 1]));
321444cd46caSbillm 
321544cd46caSbillm 		c = (char *)&d[i];
321644cd46caSbillm 		for (j = 0; j < 2 * sizeof (uint64_t); j++)
321744cd46caSbillm 			(void) printf("%c", isprint(c[j]) ? c[j] : '.');
321844cd46caSbillm 		(void) printf("\n");
321944cd46caSbillm 	}
322044cd46caSbillm }
322144cd46caSbillm 
322244cd46caSbillm /*
322344cd46caSbillm  * There are two acceptable formats:
322444cd46caSbillm  *	leaf_name	  - For example: c1t0d0 or /tmp/ztest.0a
322544cd46caSbillm  *	child[.child]*    - For example: 0.1.1
322644cd46caSbillm  *
322744cd46caSbillm  * The second form can be used to specify arbitrary vdevs anywhere
322844cd46caSbillm  * in the heirarchy.  For example, in a pool with a mirror of
322944cd46caSbillm  * RAID-Zs, you can specify either RAID-Z vdev with 0.0 or 0.1 .
323044cd46caSbillm  */
323144cd46caSbillm static vdev_t *
323244cd46caSbillm zdb_vdev_lookup(vdev_t *vdev, char *path)
323344cd46caSbillm {
323444cd46caSbillm 	char *s, *p, *q;
323544cd46caSbillm 	int i;
323644cd46caSbillm 
323744cd46caSbillm 	if (vdev == NULL)
323844cd46caSbillm 		return (NULL);
323944cd46caSbillm 
324044cd46caSbillm 	/* First, assume the x.x.x.x format */
324144cd46caSbillm 	i = (int)strtoul(path, &s, 10);
324244cd46caSbillm 	if (s == path || (s && *s != '.' && *s != '\0'))
324344cd46caSbillm 		goto name;
324444cd46caSbillm 	if (i < 0 || i >= vdev->vdev_children)
324544cd46caSbillm 		return (NULL);
324644cd46caSbillm 
324744cd46caSbillm 	vdev = vdev->vdev_child[i];
324844cd46caSbillm 	if (*s == '\0')
324944cd46caSbillm 		return (vdev);
325044cd46caSbillm 	return (zdb_vdev_lookup(vdev, s+1));
325144cd46caSbillm 
325244cd46caSbillm name:
325344cd46caSbillm 	for (i = 0; i < vdev->vdev_children; i++) {
325444cd46caSbillm 		vdev_t *vc = vdev->vdev_child[i];
325544cd46caSbillm 
325644cd46caSbillm 		if (vc->vdev_path == NULL) {
325744cd46caSbillm 			vc = zdb_vdev_lookup(vc, path);
325844cd46caSbillm 			if (vc == NULL)
325944cd46caSbillm 				continue;
326044cd46caSbillm 			else
326144cd46caSbillm 				return (vc);
326244cd46caSbillm 		}
326344cd46caSbillm 
326444cd46caSbillm 		p = strrchr(vc->vdev_path, '/');
326544cd46caSbillm 		p = p ? p + 1 : vc->vdev_path;
326644cd46caSbillm 		q = &vc->vdev_path[strlen(vc->vdev_path) - 2];
326744cd46caSbillm 
326844cd46caSbillm 		if (strcmp(vc->vdev_path, path) == 0)
326944cd46caSbillm 			return (vc);
327044cd46caSbillm 		if (strcmp(p, path) == 0)
327144cd46caSbillm 			return (vc);
327244cd46caSbillm 		if (strcmp(q, "s0") == 0 && strncmp(p, path, q - p) == 0)
327344cd46caSbillm 			return (vc);
327444cd46caSbillm 	}
327544cd46caSbillm 
327644cd46caSbillm 	return (NULL);
327744cd46caSbillm }
327844cd46caSbillm 
327944cd46caSbillm /*
328044cd46caSbillm  * Read a block from a pool and print it out.  The syntax of the
328144cd46caSbillm  * block descriptor is:
328244cd46caSbillm  *
328344cd46caSbillm  *	pool:vdev_specifier:offset:size[:flags]
328444cd46caSbillm  *
328544cd46caSbillm  *	pool           - The name of the pool you wish to read from
328644cd46caSbillm  *	vdev_specifier - Which vdev (see comment for zdb_vdev_lookup)
328744cd46caSbillm  *	offset         - offset, in hex, in bytes
328844cd46caSbillm  *	size           - Amount of data to read, in hex, in bytes
328944cd46caSbillm  *	flags          - A string of characters specifying options
329044cd46caSbillm  *		 b: Decode a blkptr at given offset within block
329144cd46caSbillm  *		*c: Calculate and display checksums
3292b24ab676SJeff Bonwick  *		 d: Decompress data before dumping
329344cd46caSbillm  *		 e: Byteswap data before dumping
3294b24ab676SJeff Bonwick  *		 g: Display data as a gang block header
3295b24ab676SJeff Bonwick  *		 i: Display as an indirect block
329644cd46caSbillm  *		 p: Do I/O to physical offset
329744cd46caSbillm  *		 r: Dump raw data to stdout
329844cd46caSbillm  *
329944cd46caSbillm  *              * = not yet implemented
330044cd46caSbillm  */
330144cd46caSbillm static void
330207428bdfSVictor Latushkin zdb_read_block(char *thing, spa_t *spa)
330344cd46caSbillm {
3304b24ab676SJeff Bonwick 	blkptr_t blk, *bp = &blk;
3305b24ab676SJeff Bonwick 	dva_t *dva = bp->blk_dva;
330644cd46caSbillm 	int flags = 0;
3307b24ab676SJeff Bonwick 	uint64_t offset = 0, size = 0, psize = 0, lsize = 0, blkptr_offset = 0;
330844cd46caSbillm 	zio_t *zio;
330944cd46caSbillm 	vdev_t *vd;
3310b24ab676SJeff Bonwick 	void *pbuf, *lbuf, *buf;
331107428bdfSVictor Latushkin 	char *s, *p, *dup, *vdev, *flagstr;
3312b24ab676SJeff Bonwick 	int i, error;
331344cd46caSbillm 
331444cd46caSbillm 	dup = strdup(thing);
331544cd46caSbillm 	s = strtok(dup, ":");
331644cd46caSbillm 	vdev = s ? s : "";
331744cd46caSbillm 	s = strtok(NULL, ":");
331844cd46caSbillm 	offset = strtoull(s ? s : "", NULL, 16);
331944cd46caSbillm 	s = strtok(NULL, ":");
332044cd46caSbillm 	size = strtoull(s ? s : "", NULL, 16);
332144cd46caSbillm 	s = strtok(NULL, ":");
332244cd46caSbillm 	flagstr = s ? s : "";
332344cd46caSbillm 
332444cd46caSbillm 	s = NULL;
332544cd46caSbillm 	if (size == 0)
332644cd46caSbillm 		s = "size must not be zero";
332744cd46caSbillm 	if (!IS_P2ALIGNED(size, DEV_BSIZE))
332844cd46caSbillm 		s = "size must be a multiple of sector size";
332944cd46caSbillm 	if (!IS_P2ALIGNED(offset, DEV_BSIZE))
333044cd46caSbillm 		s = "offset must be a multiple of sector size";
333144cd46caSbillm 	if (s) {
333244cd46caSbillm 		(void) printf("Invalid block specifier: %s  - %s\n", thing, s);
333344cd46caSbillm 		free(dup);
333444cd46caSbillm 		return;
333544cd46caSbillm 	}
333644cd46caSbillm 
333744cd46caSbillm 	for (s = strtok(flagstr, ":"); s; s = strtok(NULL, ":")) {
333844cd46caSbillm 		for (i = 0; flagstr[i]; i++) {
33395ad82045Snd150628 			int bit = flagbits[(uchar_t)flagstr[i]];
334044cd46caSbillm 
334144cd46caSbillm 			if (bit == 0) {
334244cd46caSbillm 				(void) printf("***Invalid flag: %c\n",
334344cd46caSbillm 				    flagstr[i]);
334444cd46caSbillm 				continue;
334544cd46caSbillm 			}
334644cd46caSbillm 			flags |= bit;
334744cd46caSbillm 
334844cd46caSbillm 			/* If it's not something with an argument, keep going */
3349b24ab676SJeff Bonwick 			if ((bit & (ZDB_FLAG_CHECKSUM |
335044cd46caSbillm 			    ZDB_FLAG_PRINT_BLKPTR)) == 0)
335144cd46caSbillm 				continue;
335244cd46caSbillm 
335344cd46caSbillm 			p = &flagstr[i + 1];
335444cd46caSbillm 			if (bit == ZDB_FLAG_PRINT_BLKPTR)
335544cd46caSbillm 				blkptr_offset = strtoull(p, &p, 16);
335644cd46caSbillm 			if (*p != ':' && *p != '\0') {
335744cd46caSbillm 				(void) printf("***Invalid flag arg: '%s'\n", s);
335844cd46caSbillm 				free(dup);
335944cd46caSbillm 				return;
336044cd46caSbillm 			}
336144cd46caSbillm 		}
336244cd46caSbillm 	}
336344cd46caSbillm 
336444cd46caSbillm 	vd = zdb_vdev_lookup(spa->spa_root_vdev, vdev);
336544cd46caSbillm 	if (vd == NULL) {
336644cd46caSbillm 		(void) printf("***Invalid vdev: %s\n", vdev);
336744cd46caSbillm 		free(dup);
336844cd46caSbillm 		return;
336944cd46caSbillm 	} else {
337044cd46caSbillm 		if (vd->vdev_path)
3371b24ab676SJeff Bonwick 			(void) fprintf(stderr, "Found vdev: %s\n",
3372b24ab676SJeff Bonwick 			    vd->vdev_path);
337344cd46caSbillm 		else
3374b24ab676SJeff Bonwick 			(void) fprintf(stderr, "Found vdev type: %s\n",
337544cd46caSbillm 			    vd->vdev_ops->vdev_op_type);
337644cd46caSbillm 	}
337744cd46caSbillm 
3378b24ab676SJeff Bonwick 	psize = size;
3379b24ab676SJeff Bonwick 	lsize = size;
338044cd46caSbillm 
3381b24ab676SJeff Bonwick 	pbuf = umem_alloc(SPA_MAXBLOCKSIZE, UMEM_NOFAIL);
3382b24ab676SJeff Bonwick 	lbuf = umem_alloc(SPA_MAXBLOCKSIZE, UMEM_NOFAIL);
3383b24ab676SJeff Bonwick 
3384b24ab676SJeff Bonwick 	BP_ZERO(bp);
3385b24ab676SJeff Bonwick 
3386b24ab676SJeff Bonwick 	DVA_SET_VDEV(&dva[0], vd->vdev_id);
3387b24ab676SJeff Bonwick 	DVA_SET_OFFSET(&dva[0], offset);
3388b24ab676SJeff Bonwick 	DVA_SET_GANG(&dva[0], !!(flags & ZDB_FLAG_GBH));
3389b24ab676SJeff Bonwick 	DVA_SET_ASIZE(&dva[0], vdev_psize_to_asize(vd, psize));
3390b24ab676SJeff Bonwick 
3391b24ab676SJeff Bonwick 	BP_SET_BIRTH(bp, TXG_INITIAL, TXG_INITIAL);
3392b24ab676SJeff Bonwick 
3393b24ab676SJeff Bonwick 	BP_SET_LSIZE(bp, lsize);
3394b24ab676SJeff Bonwick 	BP_SET_PSIZE(bp, psize);
3395b24ab676SJeff Bonwick 	BP_SET_COMPRESS(bp, ZIO_COMPRESS_OFF);
3396b24ab676SJeff Bonwick 	BP_SET_CHECKSUM(bp, ZIO_CHECKSUM_OFF);
3397b24ab676SJeff Bonwick 	BP_SET_TYPE(bp, DMU_OT_NONE);
3398b24ab676SJeff Bonwick 	BP_SET_LEVEL(bp, 0);
3399b24ab676SJeff Bonwick 	BP_SET_DEDUP(bp, 0);
3400b24ab676SJeff Bonwick 	BP_SET_BYTEORDER(bp, ZFS_HOST_BYTEORDER);
340144cd46caSbillm 
3402e14bb325SJeff Bonwick 	spa_config_enter(spa, SCL_STATE, FTAG, RW_READER);
340344cd46caSbillm 	zio = zio_root(spa, NULL, NULL, 0);
3404b24ab676SJeff Bonwick 
3405b24ab676SJeff Bonwick 	if (vd == vd->vdev_top) {
3406b24ab676SJeff Bonwick 		/*
3407b24ab676SJeff Bonwick 		 * Treat this as a normal block read.
3408b24ab676SJeff Bonwick 		 */
3409b24ab676SJeff Bonwick 		zio_nowait(zio_read(zio, spa, bp, pbuf, psize, NULL, NULL,
3410b24ab676SJeff Bonwick 		    ZIO_PRIORITY_SYNC_READ,
3411b24ab676SJeff Bonwick 		    ZIO_FLAG_CANFAIL | ZIO_FLAG_RAW, NULL));
3412b24ab676SJeff Bonwick 	} else {
3413b24ab676SJeff Bonwick 		/*
3414b24ab676SJeff Bonwick 		 * Treat this as a vdev child I/O.
3415b24ab676SJeff Bonwick 		 */
3416b24ab676SJeff Bonwick 		zio_nowait(zio_vdev_child_io(zio, bp, vd, offset, pbuf, psize,
3417b24ab676SJeff Bonwick 		    ZIO_TYPE_READ, ZIO_PRIORITY_SYNC_READ,
3418b24ab676SJeff Bonwick 		    ZIO_FLAG_DONT_CACHE | ZIO_FLAG_DONT_QUEUE |
3419b24ab676SJeff Bonwick 		    ZIO_FLAG_DONT_PROPAGATE | ZIO_FLAG_DONT_RETRY |
3420b24ab676SJeff Bonwick 		    ZIO_FLAG_CANFAIL | ZIO_FLAG_RAW, NULL, NULL));
3421b24ab676SJeff Bonwick 	}
3422b24ab676SJeff Bonwick 
342344cd46caSbillm 	error = zio_wait(zio);
3424e14bb325SJeff Bonwick 	spa_config_exit(spa, SCL_STATE, FTAG);
342544cd46caSbillm 
342644cd46caSbillm 	if (error) {
342744cd46caSbillm 		(void) printf("Read of %s failed, error: %d\n", thing, error);
342844cd46caSbillm 		goto out;
342944cd46caSbillm 	}
343044cd46caSbillm 
3431b24ab676SJeff Bonwick 	if (flags & ZDB_FLAG_DECOMPRESS) {
3432b24ab676SJeff Bonwick 		/*
3433b24ab676SJeff Bonwick 		 * We don't know how the data was compressed, so just try
3434b24ab676SJeff Bonwick 		 * every decompress function at every inflated blocksize.
3435b24ab676SJeff Bonwick 		 */
3436b24ab676SJeff Bonwick 		enum zio_compress c;
3437b24ab676SJeff Bonwick 		void *pbuf2 = umem_alloc(SPA_MAXBLOCKSIZE, UMEM_NOFAIL);
3438b24ab676SJeff Bonwick 		void *lbuf2 = umem_alloc(SPA_MAXBLOCKSIZE, UMEM_NOFAIL);
3439b24ab676SJeff Bonwick 
3440b24ab676SJeff Bonwick 		bcopy(pbuf, pbuf2, psize);
3441b24ab676SJeff Bonwick 
3442b24ab676SJeff Bonwick 		VERIFY(random_get_pseudo_bytes((uint8_t *)pbuf + psize,
3443b24ab676SJeff Bonwick 		    SPA_MAXBLOCKSIZE - psize) == 0);
3444b24ab676SJeff Bonwick 
3445b24ab676SJeff Bonwick 		VERIFY(random_get_pseudo_bytes((uint8_t *)pbuf2 + psize,
3446b24ab676SJeff Bonwick 		    SPA_MAXBLOCKSIZE - psize) == 0);
3447b24ab676SJeff Bonwick 
3448b24ab676SJeff Bonwick 		for (lsize = SPA_MAXBLOCKSIZE; lsize > psize;
3449b24ab676SJeff Bonwick 		    lsize -= SPA_MINBLOCKSIZE) {
3450b24ab676SJeff Bonwick 			for (c = 0; c < ZIO_COMPRESS_FUNCTIONS; c++) {
3451b24ab676SJeff Bonwick 				if (zio_decompress_data(c, pbuf, lbuf,
3452b24ab676SJeff Bonwick 				    psize, lsize) == 0 &&
3453b24ab676SJeff Bonwick 				    zio_decompress_data(c, pbuf2, lbuf2,
3454b24ab676SJeff Bonwick 				    psize, lsize) == 0 &&
3455b24ab676SJeff Bonwick 				    bcmp(lbuf, lbuf2, lsize) == 0)
3456b24ab676SJeff Bonwick 					break;
3457b24ab676SJeff Bonwick 			}
3458b24ab676SJeff Bonwick 			if (c != ZIO_COMPRESS_FUNCTIONS)
3459b24ab676SJeff Bonwick 				break;
3460b24ab676SJeff Bonwick 			lsize -= SPA_MINBLOCKSIZE;
3461b24ab676SJeff Bonwick 		}
3462b24ab676SJeff Bonwick 
3463b24ab676SJeff Bonwick 		umem_free(pbuf2, SPA_MAXBLOCKSIZE);
3464b24ab676SJeff Bonwick 		umem_free(lbuf2, SPA_MAXBLOCKSIZE);
3465b24ab676SJeff Bonwick 
3466b24ab676SJeff Bonwick 		if (lsize <= psize) {
3467b24ab676SJeff Bonwick 			(void) printf("Decompress of %s failed\n", thing);
3468b24ab676SJeff Bonwick 			goto out;
3469b24ab676SJeff Bonwick 		}
3470b24ab676SJeff Bonwick 		buf = lbuf;
3471b24ab676SJeff Bonwick 		size = lsize;
3472b24ab676SJeff Bonwick 	} else {
3473b24ab676SJeff Bonwick 		buf = pbuf;
3474b24ab676SJeff Bonwick 		size = psize;
3475b24ab676SJeff Bonwick 	}
3476b24ab676SJeff Bonwick 
347744cd46caSbillm 	if (flags & ZDB_FLAG_PRINT_BLKPTR)
347844cd46caSbillm 		zdb_print_blkptr((blkptr_t *)(void *)
347944cd46caSbillm 		    ((uintptr_t)buf + (uintptr_t)blkptr_offset), flags);
348044cd46caSbillm 	else if (flags & ZDB_FLAG_RAW)
348144cd46caSbillm 		zdb_dump_block_raw(buf, size, flags);
348244cd46caSbillm 	else if (flags & ZDB_FLAG_INDIRECT)
348344cd46caSbillm 		zdb_dump_indirect((blkptr_t *)buf, size / sizeof (blkptr_t),
348444cd46caSbillm 		    flags);
348544cd46caSbillm 	else if (flags & ZDB_FLAG_GBH)
348644cd46caSbillm 		zdb_dump_gbh(buf, flags);
348744cd46caSbillm 	else
348844cd46caSbillm 		zdb_dump_block(thing, buf, size, flags);
348944cd46caSbillm 
349044cd46caSbillm out:
3491b24ab676SJeff Bonwick 	umem_free(pbuf, SPA_MAXBLOCKSIZE);
3492b24ab676SJeff Bonwick 	umem_free(lbuf, SPA_MAXBLOCKSIZE);
349344cd46caSbillm 	free(dup);
349444cd46caSbillm }
349544cd46caSbillm 
3496de6628f0Sck153898 static boolean_t
34973ad6c7f9SVictor Latushkin pool_match(nvlist_t *cfg, char *tgt)
3498de6628f0Sck153898 {
34993ad6c7f9SVictor Latushkin 	uint64_t v, guid = strtoull(tgt, NULL, 0);
3500de6628f0Sck153898 	char *s;
3501de6628f0Sck153898 
35023ad6c7f9SVictor Latushkin 	if (guid != 0) {
35033ad6c7f9SVictor Latushkin 		if (nvlist_lookup_uint64(cfg, ZPOOL_CONFIG_POOL_GUID, &v) == 0)
35043ad6c7f9SVictor Latushkin 			return (v == guid);
35053ad6c7f9SVictor Latushkin 	} else {
35063ad6c7f9SVictor Latushkin 		if (nvlist_lookup_string(cfg, ZPOOL_CONFIG_POOL_NAME, &s) == 0)
3507de6628f0Sck153898 			return (strcmp(s, tgt) == 0);
3508de6628f0Sck153898 	}
3509de6628f0Sck153898 	return (B_FALSE);
3510de6628f0Sck153898 }
3511de6628f0Sck153898 
35123ad6c7f9SVictor Latushkin static char *
35133ad6c7f9SVictor Latushkin find_zpool(char **target, nvlist_t **configp, int dirc, char **dirv)
3514de6628f0Sck153898 {
3515de6628f0Sck153898 	nvlist_t *pools;
3516de6628f0Sck153898 	nvlist_t *match = NULL;
35173ad6c7f9SVictor Latushkin 	char *name = NULL;
35183ad6c7f9SVictor Latushkin 	char *sepp = NULL;
351940a5c998SMatthew Ahrens 	char sep = '\0';
35203ad6c7f9SVictor Latushkin 	int count = 0;
3521d41c4376SMark J Musante 	importargs_t args = { 0 };
3522d41c4376SMark J Musante 
3523d41c4376SMark J Musante 	args.paths = dirc;
3524d41c4376SMark J Musante 	args.path = dirv;
3525d41c4376SMark J Musante 	args.can_be_active = B_TRUE;
3526de6628f0Sck153898 
35273ad6c7f9SVictor Latushkin 	if ((sepp = strpbrk(*target, "/@")) != NULL) {
35283ad6c7f9SVictor Latushkin 		sep = *sepp;
35293ad6c7f9SVictor Latushkin 		*sepp = '\0';
35303ad6c7f9SVictor Latushkin 	}
35313ad6c7f9SVictor Latushkin 
3532d41c4376SMark J Musante 	pools = zpool_search_import(g_zfs, &args);
3533de6628f0Sck153898 
3534de6628f0Sck153898 	if (pools != NULL) {
3535de6628f0Sck153898 		nvpair_t *elem = NULL;
3536de6628f0Sck153898 		while ((elem = nvlist_next_nvpair(pools, elem)) != NULL) {
3537de6628f0Sck153898 			verify(nvpair_value_nvlist(elem, configp) == 0);
35383ad6c7f9SVictor Latushkin 			if (pool_match(*configp, *target)) {
35393ad6c7f9SVictor Latushkin 				count++;
3540de6628f0Sck153898 				if (match != NULL) {
35413ad6c7f9SVictor Latushkin 					/* print previously found config */
35423ad6c7f9SVictor Latushkin 					if (name != NULL) {
35433ad6c7f9SVictor Latushkin 						(void) printf("%s\n", name);
35443ad6c7f9SVictor Latushkin 						dump_nvlist(match, 8);
35453ad6c7f9SVictor Latushkin 						name = NULL;
35463ad6c7f9SVictor Latushkin 					}
35473ad6c7f9SVictor Latushkin 					(void) printf("%s\n",
35483ad6c7f9SVictor Latushkin 					    nvpair_name(elem));
35493ad6c7f9SVictor Latushkin 					dump_nvlist(*configp, 8);
3550de6628f0Sck153898 				} else {
3551de6628f0Sck153898 					match = *configp;
35523ad6c7f9SVictor Latushkin 					name = nvpair_name(elem);
3553de6628f0Sck153898 				}
3554de6628f0Sck153898 			}
3555de6628f0Sck153898 		}
3556de6628f0Sck153898 	}
35573ad6c7f9SVictor Latushkin 	if (count > 1)
35583ad6c7f9SVictor Latushkin 		(void) fatal("\tMatched %d pools - use pool GUID "
35593ad6c7f9SVictor Latushkin 		    "instead of pool name or \n"
35603ad6c7f9SVictor Latushkin 		    "\tpool name part of a dataset name to select pool", count);
35613ad6c7f9SVictor Latushkin 
35623ad6c7f9SVictor Latushkin 	if (sepp)
35633ad6c7f9SVictor Latushkin 		*sepp = sep;
35643ad6c7f9SVictor Latushkin 	/*
35653ad6c7f9SVictor Latushkin 	 * If pool GUID was specified for pool id, replace it with pool name
35663ad6c7f9SVictor Latushkin 	 */
35673ad6c7f9SVictor Latushkin 	if (name && (strstr(*target, name) != *target)) {
35683ad6c7f9SVictor Latushkin 		int sz = 1 + strlen(name) + ((sepp) ? strlen(sepp) : 0);
35693ad6c7f9SVictor Latushkin 
35703ad6c7f9SVictor Latushkin 		*target = umem_alloc(sz, UMEM_NOFAIL);
35713ad6c7f9SVictor Latushkin 		(void) snprintf(*target, sz, "%s%s", name, sepp ? sepp : "");
35723ad6c7f9SVictor Latushkin 	}
3573de6628f0Sck153898 
35743ad6c7f9SVictor Latushkin 	*configp = name ? match : NULL;
3575de6628f0Sck153898 
35763ad6c7f9SVictor Latushkin 	return (name);
3577de6628f0Sck153898 }
3578de6628f0Sck153898 
3579fa9e4066Sahrens int
3580fa9e4066Sahrens main(int argc, char **argv)
3581fa9e4066Sahrens {
3582fa9e4066Sahrens 	int i, c;
3583fa9e4066Sahrens 	struct rlimit rl = { 1024, 1024 };
35843ad6c7f9SVictor Latushkin 	spa_t *spa = NULL;
3585fa9e4066Sahrens 	objset_t *os = NULL;
3586fa9e4066Sahrens 	int dump_all = 1;
3587fa9e4066Sahrens 	int verbose = 0;
3588c8ee1847SVictor Latushkin 	int error = 0;
35893ad6c7f9SVictor Latushkin 	char **searchdirs = NULL;
35903ad6c7f9SVictor Latushkin 	int nsearch = 0;
35913ad6c7f9SVictor Latushkin 	char *target;
3592468c413aSTim Haley 	nvlist_t *policy = NULL;
3593468c413aSTim Haley 	uint64_t max_txg = UINT64_MAX;
3594c8ee1847SVictor Latushkin 	int rewind = ZPOOL_NEVER_REWIND;
3595ae24175bSCyril Plisko 	char *spa_config_path_env;
3596fa9e4066Sahrens 
3597fa9e4066Sahrens 	(void) setrlimit(RLIMIT_NOFILE, &rl);
3598004388ebScasper 	(void) enable_extended_FILE_stdio(-1, -1);
3599fa9e4066Sahrens 
3600fa9e4066Sahrens 	dprintf_setup(&argc, argv);
3601fa9e4066Sahrens 
3602ae24175bSCyril Plisko 	/*
3603ae24175bSCyril Plisko 	 * If there is an environment variable SPA_CONFIG_PATH it overrides
3604ae24175bSCyril Plisko 	 * default spa_config_path setting. If -U flag is specified it will
3605ae24175bSCyril Plisko 	 * override this environment variable settings once again.
3606ae24175bSCyril Plisko 	 */
3607ae24175bSCyril Plisko 	spa_config_path_env = getenv("SPA_CONFIG_PATH");
3608ae24175bSCyril Plisko 	if (spa_config_path_env != NULL)
3609ae24175bSCyril Plisko 		spa_config_path = spa_config_path_env;
3610ae24175bSCyril Plisko 
3611df15e419SMatthew Ahrens 	while ((c = getopt(argc, argv,
36122e4c9986SGeorge Wilson 	    "bcdhilmMI:suCDRSAFLXx:evp:t:U:P")) != -1) {
3613fa9e4066Sahrens 		switch (c) {
3614fa9e4066Sahrens 		case 'b':
3615fa9e4066Sahrens 		case 'c':
3616b24ab676SJeff Bonwick 		case 'd':
3617b24ab676SJeff Bonwick 		case 'h':
3618b24ab676SJeff Bonwick 		case 'i':
3619b24ab676SJeff Bonwick 		case 'l':
3620d6e555bdSGeorge Wilson 		case 'm':
3621fa9e4066Sahrens 		case 's':
3622b24ab676SJeff Bonwick 		case 'u':
3623fa9e4066Sahrens 		case 'C':
3624b24ab676SJeff Bonwick 		case 'D':
36252e4c9986SGeorge Wilson 		case 'M':
362644cd46caSbillm 		case 'R':
3627b24ab676SJeff Bonwick 		case 'S':
3628fa9e4066Sahrens 			dump_opt[c]++;
3629fa9e4066Sahrens 			dump_all = 0;
3630fa9e4066Sahrens 			break;
3631feef89cfSVictor Latushkin 		case 'A':
3632c8ee1847SVictor Latushkin 		case 'F':
363382a0a985SVictor Latushkin 		case 'L':
3634c8ee1847SVictor Latushkin 		case 'X':
36353ad6c7f9SVictor Latushkin 		case 'e':
36363f9d6ad7SLin Ling 		case 'P':
363782a0a985SVictor Latushkin 			dump_opt[c]++;
363882a0a985SVictor Latushkin 			break;
36392e4c9986SGeorge Wilson 		case 'I':
364031d7e8faSGeorge Wilson 			max_inflight = strtoull(optarg, NULL, 0);
364131d7e8faSGeorge Wilson 			if (max_inflight == 0) {
364231d7e8faSGeorge Wilson 				(void) fprintf(stderr, "maximum number "
364331d7e8faSGeorge Wilson 				    "of inflight I/Os must be greater "
364431d7e8faSGeorge Wilson 				    "than 0\n");
364531d7e8faSGeorge Wilson 				usage();
364631d7e8faSGeorge Wilson 			}
364731d7e8faSGeorge Wilson 			break;
3648de6628f0Sck153898 		case 'p':
36493ad6c7f9SVictor Latushkin 			if (searchdirs == NULL) {
36503ad6c7f9SVictor Latushkin 				searchdirs = umem_alloc(sizeof (char *),
36513ad6c7f9SVictor Latushkin 				    UMEM_NOFAIL);
36523ad6c7f9SVictor Latushkin 			} else {
36533ad6c7f9SVictor Latushkin 				char **tmp = umem_alloc((nsearch + 1) *
36543ad6c7f9SVictor Latushkin 				    sizeof (char *), UMEM_NOFAIL);
36553ad6c7f9SVictor Latushkin 				bcopy(searchdirs, tmp, nsearch *
36563ad6c7f9SVictor Latushkin 				    sizeof (char *));
36573ad6c7f9SVictor Latushkin 				umem_free(searchdirs,
36583ad6c7f9SVictor Latushkin 				    nsearch * sizeof (char *));
36593ad6c7f9SVictor Latushkin 				searchdirs = tmp;
36603ad6c7f9SVictor Latushkin 			}
36613ad6c7f9SVictor Latushkin 			searchdirs[nsearch++] = optarg;
3662de6628f0Sck153898 			break;
36632e551927SVictor Latushkin 		case 't':
3664468c413aSTim Haley 			max_txg = strtoull(optarg, NULL, 0);
3665468c413aSTim Haley 			if (max_txg < TXG_INITIAL) {
36662e551927SVictor Latushkin 				(void) fprintf(stderr, "incorrect txg "
36672e551927SVictor Latushkin 				    "specified: %s\n", optarg);
36682e551927SVictor Latushkin 				usage();
36692e551927SVictor Latushkin 			}
36702e551927SVictor Latushkin 			break;
3671b24ab676SJeff Bonwick 		case 'U':
3672b24ab676SJeff Bonwick 			spa_config_path = optarg;
3673b24ab676SJeff Bonwick 			break;
36742e4c9986SGeorge Wilson 		case 'v':
36752e4c9986SGeorge Wilson 			verbose++;
36762e4c9986SGeorge Wilson 			break;
36772e4c9986SGeorge Wilson 		case 'x':
36782e4c9986SGeorge Wilson 			vn_dumpdir = optarg;
36792e4c9986SGeorge Wilson 			break;
3680fa9e4066Sahrens 		default:
3681fa9e4066Sahrens 			usage();
3682fa9e4066Sahrens 			break;
3683fa9e4066Sahrens 		}
3684fa9e4066Sahrens 	}
3685fa9e4066Sahrens 
36863ad6c7f9SVictor Latushkin 	if (!dump_opt['e'] && searchdirs != NULL) {
368788b7b0f2SMatthew Ahrens 		(void) fprintf(stderr, "-p option requires use of -e\n");
368888b7b0f2SMatthew Ahrens 		usage();
368988b7b0f2SMatthew Ahrens 	}
3690de6628f0Sck153898 
369106be9802SMatthew Ahrens 	/*
369206be9802SMatthew Ahrens 	 * ZDB does not typically re-read blocks; therefore limit the ARC
369306be9802SMatthew Ahrens 	 * to 256 MB, which can be used entirely for metadata.
369406be9802SMatthew Ahrens 	 */
369506be9802SMatthew Ahrens 	zfs_arc_max = zfs_arc_meta_limit = 256 * 1024 * 1024;
369606be9802SMatthew Ahrens 
3697f7950bf1SMatthew Ahrens 	/*
3698f7950bf1SMatthew Ahrens 	 * "zdb -c" uses checksum-verifying scrub i/os which are async reads.
3699f7950bf1SMatthew Ahrens 	 * "zdb -b" uses traversal prefetch which uses async reads.
3700f7950bf1SMatthew Ahrens 	 * For good performance, let several of them be active at once.
3701f7950bf1SMatthew Ahrens 	 */
3702f7950bf1SMatthew Ahrens 	zfs_vdev_async_read_max_active = 10;
3703f7950bf1SMatthew Ahrens 
3704fa9e4066Sahrens 	kernel_init(FREAD);
3705de6628f0Sck153898 	g_zfs = libzfs_init();
370691ebeef5Sahrens 	ASSERT(g_zfs != NULL);
3707fa9e4066Sahrens 
3708b24ab676SJeff Bonwick 	if (dump_all)
3709b24ab676SJeff Bonwick 		verbose = MAX(verbose, 1);
3710b24ab676SJeff Bonwick 
3711fa9e4066Sahrens 	for (c = 0; c < 256; c++) {
37123f9d6ad7SLin Ling 		if (dump_all && !strchr("elAFLRSXP", c))
3713fa9e4066Sahrens 			dump_opt[c] = 1;
3714fa9e4066Sahrens 		if (dump_opt[c])
3715fa9e4066Sahrens 			dump_opt[c] += verbose;
3716fa9e4066Sahrens 	}
3717fa9e4066Sahrens 
3718feef89cfSVictor Latushkin 	aok = (dump_opt['A'] == 1) || (dump_opt['A'] > 2);
3719feef89cfSVictor Latushkin 	zfs_recover = (dump_opt['A'] > 1);
3720feef89cfSVictor Latushkin 
3721fa9e4066Sahrens 	argc -= optind;
3722fa9e4066Sahrens 	argv += optind;
3723fa9e4066Sahrens 
372407428bdfSVictor Latushkin 	if (argc < 2 && dump_opt['R'])
372507428bdfSVictor Latushkin 		usage();
3726fa9e4066Sahrens 	if (argc < 1) {
37273ad6c7f9SVictor Latushkin 		if (!dump_opt['e'] && dump_opt['C']) {
3728e829d913Sck153898 			dump_cachefile(spa_config_path);
3729fa9e4066Sahrens 			return (0);
3730fa9e4066Sahrens 		}
3731fa9e4066Sahrens 		usage();
3732fa9e4066Sahrens 	}
3733fa9e4066Sahrens 
3734fa9e4066Sahrens 	if (dump_opt['l']) {
3735fa9e4066Sahrens 		dump_label(argv[0]);
3736fa9e4066Sahrens 		return (0);
3737fa9e4066Sahrens 	}
3738fa9e4066Sahrens 
3739c8ee1847SVictor Latushkin 	if (dump_opt['X'] || dump_opt['F'])
3740c8ee1847SVictor Latushkin 		rewind = ZPOOL_DO_REWIND |
3741c8ee1847SVictor Latushkin 		    (dump_opt['X'] ? ZPOOL_EXTREME_REWIND : 0);
3742c8ee1847SVictor Latushkin 
3743c8ee1847SVictor Latushkin 	if (nvlist_alloc(&policy, NV_UNIQUE_NAME_TYPE, 0) != 0 ||
3744c8ee1847SVictor Latushkin 	    nvlist_add_uint64(policy, ZPOOL_REWIND_REQUEST_TXG, max_txg) != 0 ||
3745c8ee1847SVictor Latushkin 	    nvlist_add_uint32(policy, ZPOOL_REWIND_REQUEST, rewind) != 0)
3746c8ee1847SVictor Latushkin 		fatal("internal error: %s", strerror(ENOMEM));
3747c8ee1847SVictor Latushkin 
3748c5904d13Seschrock 	error = 0;
37493ad6c7f9SVictor Latushkin 	target = argv[0];
3750de6628f0Sck153898 
37513ad6c7f9SVictor Latushkin 	if (dump_opt['e']) {
37523ad6c7f9SVictor Latushkin 		nvlist_t *cfg = NULL;
37533ad6c7f9SVictor Latushkin 		char *name = find_zpool(&target, &cfg, nsearch, searchdirs);
3754c5904d13Seschrock 
37553ad6c7f9SVictor Latushkin 		error = ENOENT;
37563ad6c7f9SVictor Latushkin 		if (name) {
375707428bdfSVictor Latushkin 			if (dump_opt['C'] > 1) {
375807428bdfSVictor Latushkin 				(void) printf("\nConfiguration for import:\n");
375907428bdfSVictor Latushkin 				dump_nvlist(cfg, 8);
376007428bdfSVictor Latushkin 			}
3761c8ee1847SVictor Latushkin 			if (nvlist_add_nvlist(cfg,
3762468c413aSTim Haley 			    ZPOOL_REWIND_POLICY, policy) != 0) {
3763468c413aSTim Haley 				fatal("can't open '%s': %s",
3764468c413aSTim Haley 				    target, strerror(ENOMEM));
3765468c413aSTim Haley 			}
37664b964adaSGeorge Wilson 			if ((error = spa_import(name, cfg, NULL,
37674b964adaSGeorge Wilson 			    ZFS_IMPORT_MISSING_LOG)) != 0) {
37684b964adaSGeorge Wilson 				error = spa_import(name, cfg, NULL,
37694b964adaSGeorge Wilson 				    ZFS_IMPORT_VERBATIM);
37704b964adaSGeorge Wilson 			}
3771990b4856Slling 		}
3772c5904d13Seschrock 	}
3773c5904d13Seschrock 
3774c5904d13Seschrock 	if (error == 0) {
377507428bdfSVictor Latushkin 		if (strpbrk(target, "/@") == NULL || dump_opt['R']) {
377680eb36f2SGeorge Wilson 			error = spa_open_rewind(target, &spa, FTAG, policy,
377780eb36f2SGeorge Wilson 			    NULL);
37788f18d1faSGeorge Wilson 			if (error) {
37798f18d1faSGeorge Wilson 				/*
37808f18d1faSGeorge Wilson 				 * If we're missing the log device then
37818f18d1faSGeorge Wilson 				 * try opening the pool after clearing the
37828f18d1faSGeorge Wilson 				 * log state.
37838f18d1faSGeorge Wilson 				 */
37848f18d1faSGeorge Wilson 				mutex_enter(&spa_namespace_lock);
37853ad6c7f9SVictor Latushkin 				if ((spa = spa_lookup(target)) != NULL &&
37868f18d1faSGeorge Wilson 				    spa->spa_log_state == SPA_LOG_MISSING) {
37878f18d1faSGeorge Wilson 					spa->spa_log_state = SPA_LOG_CLEAR;
37888f18d1faSGeorge Wilson 					error = 0;
37898f18d1faSGeorge Wilson 				}
37908f18d1faSGeorge Wilson 				mutex_exit(&spa_namespace_lock);
37918f18d1faSGeorge Wilson 
379280eb36f2SGeorge Wilson 				if (!error) {
379380eb36f2SGeorge Wilson 					error = spa_open_rewind(target, &spa,
379480eb36f2SGeorge Wilson 					    FTAG, policy, NULL);
379580eb36f2SGeorge Wilson 				}
37968f18d1faSGeorge Wilson 			}
379707428bdfSVictor Latushkin 		} else {
379807428bdfSVictor Latushkin 			error = dmu_objset_own(target, DMU_OST_ANY,
379907428bdfSVictor Latushkin 			    B_TRUE, FTAG, &os);
3800c5904d13Seschrock 		}
3801de6628f0Sck153898 	}
380280eb36f2SGeorge Wilson 	nvlist_free(policy);
380380eb36f2SGeorge Wilson 
3804fa9e4066Sahrens 	if (error)
38053ad6c7f9SVictor Latushkin 		fatal("can't open '%s': %s", target, strerror(error));
3806fa9e4066Sahrens 
3807fa9e4066Sahrens 	argv++;
380807428bdfSVictor Latushkin 	argc--;
380907428bdfSVictor Latushkin 	if (!dump_opt['R']) {
381007428bdfSVictor Latushkin 		if (argc > 0) {
3811fa9e4066Sahrens 			zopt_objects = argc;
3812fa9e4066Sahrens 			zopt_object = calloc(zopt_objects, sizeof (uint64_t));
3813fa9e4066Sahrens 			for (i = 0; i < zopt_objects; i++) {
3814fa9e4066Sahrens 				errno = 0;
3815fa9e4066Sahrens 				zopt_object[i] = strtoull(argv[i], NULL, 0);
3816fa9e4066Sahrens 				if (zopt_object[i] == 0 && errno != 0)
381787219db7SVictor Latushkin 					fatal("bad number %s: %s",
3818fa9e4066Sahrens 					    argv[i], strerror(errno));
3819fa9e4066Sahrens 			}
3820fa9e4066Sahrens 		}
3821e690fb27SChristopher Siden 		if (os != NULL) {
3822e690fb27SChristopher Siden 			dump_dir(os);
3823e690fb27SChristopher Siden 		} else if (zopt_objects > 0 && !dump_opt['m']) {
3824e690fb27SChristopher Siden 			dump_dir(spa->spa_meta_objset);
3825e690fb27SChristopher Siden 		} else {
3826e690fb27SChristopher Siden 			dump_zpool(spa);
3827e690fb27SChristopher Siden 		}
3828fa9e4066Sahrens 	} else {
382907428bdfSVictor Latushkin 		flagbits['b'] = ZDB_FLAG_PRINT_BLKPTR;
383007428bdfSVictor Latushkin 		flagbits['c'] = ZDB_FLAG_CHECKSUM;
383107428bdfSVictor Latushkin 		flagbits['d'] = ZDB_FLAG_DECOMPRESS;
383207428bdfSVictor Latushkin 		flagbits['e'] = ZDB_FLAG_BSWAP;
383307428bdfSVictor Latushkin 		flagbits['g'] = ZDB_FLAG_GBH;
383407428bdfSVictor Latushkin 		flagbits['i'] = ZDB_FLAG_INDIRECT;
383507428bdfSVictor Latushkin 		flagbits['p'] = ZDB_FLAG_PHYS;
383607428bdfSVictor Latushkin 		flagbits['r'] = ZDB_FLAG_RAW;
383707428bdfSVictor Latushkin 
383807428bdfSVictor Latushkin 		for (i = 0; i < argc; i++)
383907428bdfSVictor Latushkin 			zdb_read_block(argv[i], spa);
3840fa9e4066Sahrens 	}
3841fa9e4066Sahrens 
384207428bdfSVictor Latushkin 	(os != NULL) ? dmu_objset_disown(os, FTAG) : spa_close(spa, FTAG);
384307428bdfSVictor Latushkin 
3844e0d35c44Smarks 	fuid_table_destroy();
38450a586ceaSMark Shellenbaum 	sa_loaded = B_FALSE;
3846e0d35c44Smarks 
3847de6628f0Sck153898 	libzfs_fini(g_zfs);
3848fa9e4066Sahrens 	kernel_fini();
3849fa9e4066Sahrens 
3850fa9e4066Sahrens 	return (0);
3851fa9e4066Sahrens }
3852