xref: /titanic_53/usr/src/cmd/zdb/zdb.c (revision 90c76c66a2e21307c5370ae1bd3c7145a60c874f)
1fa9e4066Sahrens /*
2fa9e4066Sahrens  * CDDL HEADER START
3fa9e4066Sahrens  *
4fa9e4066Sahrens  * The contents of this file are subject to the terms of the
5ea8dc4b6Seschrock  * Common Development and Distribution License (the "License").
6ea8dc4b6Seschrock  * You may not use this file except in compliance with the License.
7fa9e4066Sahrens  *
8fa9e4066Sahrens  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9fa9e4066Sahrens  * or http://www.opensolaris.org/os/licensing.
10fa9e4066Sahrens  * See the License for the specific language governing permissions
11fa9e4066Sahrens  * and limitations under the License.
12fa9e4066Sahrens  *
13fa9e4066Sahrens  * When distributing Covered Code, include this CDDL HEADER in each
14fa9e4066Sahrens  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15fa9e4066Sahrens  * If applicable, add the following below this CDDL HEADER, with the
16fa9e4066Sahrens  * fields enclosed by brackets "[]" replaced with your own identifying
17fa9e4066Sahrens  * information: Portions Copyright [yyyy] [name of copyright owner]
18fa9e4066Sahrens  *
19fa9e4066Sahrens  * CDDL HEADER END
20fa9e4066Sahrens  */
21ad135b5dSChristopher Siden 
22fa9e4066Sahrens /*
238f2529deSMark Shellenbaum  * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
247802d7bfSMatthew Ahrens  * Copyright (c) 2011, 2014 by Delphix. All rights reserved.
25fa9e4066Sahrens  */
26fa9e4066Sahrens 
27fa9e4066Sahrens #include <stdio.h>
28490d05b9SMatthew Ahrens #include <unistd.h>
29004388ebScasper #include <stdio_ext.h>
30fa9e4066Sahrens #include <stdlib.h>
3144cd46caSbillm #include <ctype.h>
32fa9e4066Sahrens #include <sys/zfs_context.h>
33fa9e4066Sahrens #include <sys/spa.h>
34fa9e4066Sahrens #include <sys/spa_impl.h>
35fa9e4066Sahrens #include <sys/dmu.h>
36fa9e4066Sahrens #include <sys/zap.h>
37fa9e4066Sahrens #include <sys/fs/zfs.h>
38fa9e4066Sahrens #include <sys/zfs_znode.h>
390a586ceaSMark Shellenbaum #include <sys/zfs_sa.h>
400a586ceaSMark Shellenbaum #include <sys/sa.h>
410a586ceaSMark Shellenbaum #include <sys/sa_impl.h>
42fa9e4066Sahrens #include <sys/vdev.h>
43fa9e4066Sahrens #include <sys/vdev_impl.h>
44fa9e4066Sahrens #include <sys/metaslab_impl.h>
45fa9e4066Sahrens #include <sys/dmu_objset.h>
46fa9e4066Sahrens #include <sys/dsl_dir.h>
47fa9e4066Sahrens #include <sys/dsl_dataset.h>
48fa9e4066Sahrens #include <sys/dsl_pool.h>
49fa9e4066Sahrens #include <sys/dbuf.h>
50fa9e4066Sahrens #include <sys/zil.h>
51fa9e4066Sahrens #include <sys/zil_impl.h>
52fa9e4066Sahrens #include <sys/stat.h>
53fa9e4066Sahrens #include <sys/resource.h>
54fa9e4066Sahrens #include <sys/dmu_traverse.h>
55fa9e4066Sahrens #include <sys/zio_checksum.h>
56fa9e4066Sahrens #include <sys/zio_compress.h>
57e0d35c44Smarks #include <sys/zfs_fuid.h>
5888b7b0f2SMatthew Ahrens #include <sys/arc.h>
59b24ab676SJeff Bonwick #include <sys/ddt.h>
60ad135b5dSChristopher Siden #include <sys/zfeature.h>
614445fffbSMatthew Ahrens #include <zfs_comutil.h>
62de6628f0Sck153898 #undef ZFS_MAXNAMELEN
63de6628f0Sck153898 #undef verify
64de6628f0Sck153898 #include <libzfs.h>
65fa9e4066Sahrens 
666de8f417SVictor Latushkin #define	ZDB_COMPRESS_NAME(idx) ((idx) < ZIO_COMPRESS_FUNCTIONS ?	\
676de8f417SVictor Latushkin 	zio_compress_table[(idx)].ci_name : "UNKNOWN")
686de8f417SVictor Latushkin #define	ZDB_CHECKSUM_NAME(idx) ((idx) < ZIO_CHECKSUM_FUNCTIONS ?	\
696de8f417SVictor Latushkin 	zio_checksum_table[(idx)].ci_name : "UNKNOWN")
706de8f417SVictor Latushkin #define	ZDB_OT_NAME(idx) ((idx) < DMU_OT_NUMTYPES ?	\
71ad135b5dSChristopher Siden 	dmu_ot[(idx)].ot_name : DMU_OT_IS_VALID(idx) ?	\
72ad135b5dSChristopher Siden 	dmu_ot_byteswap[DMU_OT_BYTESWAP(idx)].ob_name : "UNKNOWN")
73e690fb27SChristopher Siden #define	ZDB_OT_TYPE(idx) ((idx) < DMU_OT_NUMTYPES ? (idx) :		\
74e690fb27SChristopher Siden 	(((idx) == DMU_OTN_ZAP_DATA || (idx) == DMU_OTN_ZAP_METADATA) ?	\
75e690fb27SChristopher Siden 	DMU_OT_ZAP_OTHER : DMU_OT_NUMTYPES))
766de8f417SVictor Latushkin 
77feef89cfSVictor Latushkin #ifndef lint
787fd05ac4SMatthew Ahrens extern boolean_t zfs_recover;
79feef89cfSVictor Latushkin #else
807fd05ac4SMatthew Ahrens boolean_t zfs_recover;
81feef89cfSVictor Latushkin #endif
82feef89cfSVictor Latushkin 
83fa9e4066Sahrens const char cmdname[] = "zdb";
84fa9e4066Sahrens uint8_t dump_opt[256];
85fa9e4066Sahrens 
86fa9e4066Sahrens typedef void object_viewer_t(objset_t *, uint64_t, void *data, size_t size);
87fa9e4066Sahrens 
88fa9e4066Sahrens extern void dump_intent_log(zilog_t *);
89fa9e4066Sahrens uint64_t *zopt_object = NULL;
90fa9e4066Sahrens int zopt_objects = 0;
91de6628f0Sck153898 libzfs_handle_t *g_zfs;
9231d7e8faSGeorge Wilson uint64_t max_inflight = 200;
93fa9e4066Sahrens 
94fa9e4066Sahrens /*
95fa9e4066Sahrens  * These libumem hooks provide a reasonable set of defaults for the allocator's
96fa9e4066Sahrens  * debugging facilities.
97fa9e4066Sahrens  */
98fa9e4066Sahrens const char *
99fa9e4066Sahrens _umem_debug_init()
100fa9e4066Sahrens {
101fa9e4066Sahrens 	return ("default,verbose"); /* $UMEM_DEBUG setting */
102fa9e4066Sahrens }
103fa9e4066Sahrens 
104fa9e4066Sahrens const char *
105fa9e4066Sahrens _umem_logging_init(void)
106fa9e4066Sahrens {
107fa9e4066Sahrens 	return ("fail,contents"); /* $UMEM_LOGGING setting */
108fa9e4066Sahrens }
109fa9e4066Sahrens 
110fa9e4066Sahrens static void
111fa9e4066Sahrens usage(void)
112fa9e4066Sahrens {
113fa9e4066Sahrens 	(void) fprintf(stderr,
1142e4c9986SGeorge Wilson 	    "Usage: %s [-CumMdibcsDvhLXFPA] [-t txg] [-e [-p path...]] "
1152e4c9986SGeorge Wilson 	    "[-U config] [-I inflight I/Os] [-x dumpdir] poolname [object...]\n"
11631d7e8faSGeorge Wilson 	    "       %s [-divPA] [-e -p path...] [-U config] dataset "
11731d7e8faSGeorge Wilson 	    "[object...]\n"
1182e4c9986SGeorge Wilson 	    "       %s -mM [-LXFPA] [-t txg] [-e [-p path...]] [-U config] "
11990e894e2SRichard Lowe 	    "poolname [vdev [metaslab...]]\n"
12090e894e2SRichard Lowe 	    "       %s -R [-A] [-e [-p path...]] poolname "
12190e894e2SRichard Lowe 	    "vdev:offset:size[:flags]\n"
12231d7e8faSGeorge Wilson 	    "       %s -S [-PA] [-e [-p path...]] [-U config] poolname\n"
12390e894e2SRichard Lowe 	    "       %s -l [-uA] device\n"
12490e894e2SRichard Lowe 	    "       %s -C [-A] [-U config]\n\n",
125bbfd46c4SJeff Bonwick 	    cmdname, cmdname, cmdname, cmdname, cmdname, cmdname, cmdname);
126fa9e4066Sahrens 
1273ad6c7f9SVictor Latushkin 	(void) fprintf(stderr, "    Dataset name must include at least one "
1283ad6c7f9SVictor Latushkin 	    "separator character '/' or '@'\n");
1293ad6c7f9SVictor Latushkin 	(void) fprintf(stderr, "    If dataset name is specified, only that "
1303ad6c7f9SVictor Latushkin 	    "dataset is dumped\n");
1313ad6c7f9SVictor Latushkin 	(void) fprintf(stderr, "    If object numbers are specified, only "
1323ad6c7f9SVictor Latushkin 	    "those objects are dumped\n\n");
1333ad6c7f9SVictor Latushkin 	(void) fprintf(stderr, "    Options to control amount of output:\n");
134fa9e4066Sahrens 	(void) fprintf(stderr, "        -u uberblock\n");
1353ad6c7f9SVictor Latushkin 	(void) fprintf(stderr, "        -d dataset(s)\n");
1363ad6c7f9SVictor Latushkin 	(void) fprintf(stderr, "        -i intent logs\n");
13707428bdfSVictor Latushkin 	(void) fprintf(stderr, "        -C config (or cachefile if alone)\n");
1388f18d1faSGeorge Wilson 	(void) fprintf(stderr, "        -h pool history\n");
139fa9e4066Sahrens 	(void) fprintf(stderr, "        -b block statistics\n");
140d6e555bdSGeorge Wilson 	(void) fprintf(stderr, "        -m metaslabs\n");
1412e4c9986SGeorge Wilson 	(void) fprintf(stderr, "        -M metaslab groups\n");
1426365109dSVictor Latushkin 	(void) fprintf(stderr, "        -c checksum all metadata (twice for "
1436365109dSVictor Latushkin 	    "all data) blocks\n");
144fa9e4066Sahrens 	(void) fprintf(stderr, "        -s report stats on zdb's I/O\n");
145f0ba89beSJeff Bonwick 	(void) fprintf(stderr, "        -D dedup statistics\n");
146b24ab676SJeff Bonwick 	(void) fprintf(stderr, "        -S simulate dedup to measure effect\n");
147fa9e4066Sahrens 	(void) fprintf(stderr, "        -v verbose (applies to all others)\n");
148fa9e4066Sahrens 	(void) fprintf(stderr, "        -l dump label contents\n");
14982a0a985SVictor Latushkin 	(void) fprintf(stderr, "        -L disable leak tracking (do not "
15082a0a985SVictor Latushkin 	    "load spacemaps)\n");
15144cd46caSbillm 	(void) fprintf(stderr, "        -R read and display block from a "
1523ad6c7f9SVictor Latushkin 	    "device\n\n");
1533ad6c7f9SVictor Latushkin 	(void) fprintf(stderr, "    Below options are intended for use "
154df15e419SMatthew Ahrens 	    "with other options:\n");
155feef89cfSVictor Latushkin 	(void) fprintf(stderr, "        -A ignore assertions (-A), enable "
156feef89cfSVictor Latushkin 	    "panic recovery (-AA) or both (-AAA)\n");
157c8ee1847SVictor Latushkin 	(void) fprintf(stderr, "        -F attempt automatic rewind within "
158c8ee1847SVictor Latushkin 	    "safe range of transaction groups\n");
1593ad6c7f9SVictor Latushkin 	(void) fprintf(stderr, "        -U <cachefile_path> -- use alternate "
1603ad6c7f9SVictor Latushkin 	    "cachefile\n");
161c8ee1847SVictor Latushkin 	(void) fprintf(stderr, "        -X attempt extreme rewind (does not "
162c8ee1847SVictor Latushkin 	    "work with dataset)\n");
1633ad6c7f9SVictor Latushkin 	(void) fprintf(stderr, "        -e pool is exported/destroyed/"
1643ad6c7f9SVictor Latushkin 	    "has altroot/not in a cachefile\n");
1653ad6c7f9SVictor Latushkin 	(void) fprintf(stderr, "        -p <path> -- use one or more with "
1663ad6c7f9SVictor Latushkin 	    "-e to specify path to vdev dir\n");
167df15e419SMatthew Ahrens 	(void) fprintf(stderr, "        -x <dumpdir> -- "
168df15e419SMatthew Ahrens 	    "dump all read blocks into specified directory\n");
16990e894e2SRichard Lowe 	(void) fprintf(stderr, "        -P print numbers in parseable form\n");
1703ad6c7f9SVictor Latushkin 	(void) fprintf(stderr, "        -t <txg> -- highest txg to use when "
1712e551927SVictor Latushkin 	    "searching for uberblocks\n");
1722e4c9986SGeorge Wilson 	(void) fprintf(stderr, "        -I <number of inflight I/Os> -- "
173df15e419SMatthew Ahrens 	    "specify the maximum number of "
174df15e419SMatthew Ahrens 	    "checksumming I/Os [default is 200]\n");
175fa9e4066Sahrens 	(void) fprintf(stderr, "Specify an option more than once (e.g. -bb) "
176fa9e4066Sahrens 	    "to make only that option verbose\n");
177fa9e4066Sahrens 	(void) fprintf(stderr, "Default is to dump everything non-verbosely\n");
178fa9e4066Sahrens 	exit(1);
179fa9e4066Sahrens }
180fa9e4066Sahrens 
181ccba0801SRich Morris /*
182ccba0801SRich Morris  * Called for usage errors that are discovered after a call to spa_open(),
183ccba0801SRich Morris  * dmu_bonus_hold(), or pool_match().  abort() is called for other errors.
184ccba0801SRich Morris  */
185ccba0801SRich Morris 
186fa9e4066Sahrens static void
187fa9e4066Sahrens fatal(const char *fmt, ...)
188fa9e4066Sahrens {
189fa9e4066Sahrens 	va_list ap;
190fa9e4066Sahrens 
191fa9e4066Sahrens 	va_start(ap, fmt);
192fa9e4066Sahrens 	(void) fprintf(stderr, "%s: ", cmdname);
193fa9e4066Sahrens 	(void) vfprintf(stderr, fmt, ap);
194fa9e4066Sahrens 	va_end(ap);
195fa9e4066Sahrens 	(void) fprintf(stderr, "\n");
196fa9e4066Sahrens 
197ccba0801SRich Morris 	exit(1);
198fa9e4066Sahrens }
199fa9e4066Sahrens 
200fa9e4066Sahrens /* ARGSUSED */
201fa9e4066Sahrens static void
202fa9e4066Sahrens dump_packed_nvlist(objset_t *os, uint64_t object, void *data, size_t size)
203fa9e4066Sahrens {
204fa9e4066Sahrens 	nvlist_t *nv;
205fa9e4066Sahrens 	size_t nvsize = *(uint64_t *)data;
206fa9e4066Sahrens 	char *packed = umem_alloc(nvsize, UMEM_NOFAIL);
207fa9e4066Sahrens 
2087bfdf011SNeil Perrin 	VERIFY(0 == dmu_read(os, object, 0, nvsize, packed, DMU_READ_PREFETCH));
209fa9e4066Sahrens 
210fa9e4066Sahrens 	VERIFY(nvlist_unpack(packed, nvsize, &nv, 0) == 0);
211fa9e4066Sahrens 
212fa9e4066Sahrens 	umem_free(packed, nvsize);
213fa9e4066Sahrens 
214fa9e4066Sahrens 	dump_nvlist(nv, 8);
215fa9e4066Sahrens 
216fa9e4066Sahrens 	nvlist_free(nv);
217fa9e4066Sahrens }
218fa9e4066Sahrens 
2194445fffbSMatthew Ahrens /* ARGSUSED */
2204445fffbSMatthew Ahrens static void
2214445fffbSMatthew Ahrens dump_history_offsets(objset_t *os, uint64_t object, void *data, size_t size)
2224445fffbSMatthew Ahrens {
2234445fffbSMatthew Ahrens 	spa_history_phys_t *shp = data;
2244445fffbSMatthew Ahrens 
2254445fffbSMatthew Ahrens 	if (shp == NULL)
2264445fffbSMatthew Ahrens 		return;
2274445fffbSMatthew Ahrens 
2284445fffbSMatthew Ahrens 	(void) printf("\t\tpool_create_len = %llu\n",
2294445fffbSMatthew Ahrens 	    (u_longlong_t)shp->sh_pool_create_len);
2304445fffbSMatthew Ahrens 	(void) printf("\t\tphys_max_off = %llu\n",
2314445fffbSMatthew Ahrens 	    (u_longlong_t)shp->sh_phys_max_off);
2324445fffbSMatthew Ahrens 	(void) printf("\t\tbof = %llu\n",
2334445fffbSMatthew Ahrens 	    (u_longlong_t)shp->sh_bof);
2344445fffbSMatthew Ahrens 	(void) printf("\t\teof = %llu\n",
2354445fffbSMatthew Ahrens 	    (u_longlong_t)shp->sh_eof);
2364445fffbSMatthew Ahrens 	(void) printf("\t\trecords_lost = %llu\n",
2374445fffbSMatthew Ahrens 	    (u_longlong_t)shp->sh_records_lost);
2384445fffbSMatthew Ahrens }
2394445fffbSMatthew Ahrens 
2403f9d6ad7SLin Ling static void
2413f9d6ad7SLin Ling zdb_nicenum(uint64_t num, char *buf)
2423f9d6ad7SLin Ling {
2433f9d6ad7SLin Ling 	if (dump_opt['P'])
2443f9d6ad7SLin Ling 		(void) sprintf(buf, "%llu", (longlong_t)num);
2453f9d6ad7SLin Ling 	else
2463f9d6ad7SLin Ling 		nicenum(num, buf);
2473f9d6ad7SLin Ling }
2483f9d6ad7SLin Ling 
249490d05b9SMatthew Ahrens const char histo_stars[] = "****************************************";
250490d05b9SMatthew Ahrens const int histo_width = sizeof (histo_stars) - 1;
251fa9e4066Sahrens 
252fa9e4066Sahrens static void
2530713e232SGeorge Wilson dump_histogram(const uint64_t *histo, int size, int offset)
254fa9e4066Sahrens {
255fa9e4066Sahrens 	int i;
256490d05b9SMatthew Ahrens 	int minidx = size - 1;
257fa9e4066Sahrens 	int maxidx = 0;
258fa9e4066Sahrens 	uint64_t max = 0;
259fa9e4066Sahrens 
260490d05b9SMatthew Ahrens 	for (i = 0; i < size; i++) {
261fa9e4066Sahrens 		if (histo[i] > max)
262fa9e4066Sahrens 			max = histo[i];
263fa9e4066Sahrens 		if (histo[i] > 0 && i > maxidx)
264fa9e4066Sahrens 			maxidx = i;
265fa9e4066Sahrens 		if (histo[i] > 0 && i < minidx)
266fa9e4066Sahrens 			minidx = i;
267fa9e4066Sahrens 	}
268fa9e4066Sahrens 
269490d05b9SMatthew Ahrens 	if (max < histo_width)
270490d05b9SMatthew Ahrens 		max = histo_width;
271fa9e4066Sahrens 
272490d05b9SMatthew Ahrens 	for (i = minidx; i <= maxidx; i++) {
273490d05b9SMatthew Ahrens 		(void) printf("\t\t\t%3u: %6llu %s\n",
2740713e232SGeorge Wilson 		    i + offset, (u_longlong_t)histo[i],
275490d05b9SMatthew Ahrens 		    &histo_stars[(max - histo[i]) * histo_width / max]);
276490d05b9SMatthew Ahrens 	}
277fa9e4066Sahrens }
278fa9e4066Sahrens 
279fa9e4066Sahrens static void
280fa9e4066Sahrens dump_zap_stats(objset_t *os, uint64_t object)
281fa9e4066Sahrens {
282fa9e4066Sahrens 	int error;
283fa9e4066Sahrens 	zap_stats_t zs;
284fa9e4066Sahrens 
285fa9e4066Sahrens 	error = zap_get_stats(os, object, &zs);
286fa9e4066Sahrens 	if (error)
287fa9e4066Sahrens 		return;
288fa9e4066Sahrens 
289fa9e4066Sahrens 	if (zs.zs_ptrtbl_len == 0) {
290fa9e4066Sahrens 		ASSERT(zs.zs_num_blocks == 1);
291fa9e4066Sahrens 		(void) printf("\tmicrozap: %llu bytes, %llu entries\n",
292fa9e4066Sahrens 		    (u_longlong_t)zs.zs_blocksize,
293fa9e4066Sahrens 		    (u_longlong_t)zs.zs_num_entries);
294fa9e4066Sahrens 		return;
295fa9e4066Sahrens 	}
296fa9e4066Sahrens 
297fa9e4066Sahrens 	(void) printf("\tFat ZAP stats:\n");
2988248818dSnd150628 
2998248818dSnd150628 	(void) printf("\t\tPointer table:\n");
3008248818dSnd150628 	(void) printf("\t\t\t%llu elements\n",
301fa9e4066Sahrens 	    (u_longlong_t)zs.zs_ptrtbl_len);
3028248818dSnd150628 	(void) printf("\t\t\tzt_blk: %llu\n",
3038248818dSnd150628 	    (u_longlong_t)zs.zs_ptrtbl_zt_blk);
3048248818dSnd150628 	(void) printf("\t\t\tzt_numblks: %llu\n",
3058248818dSnd150628 	    (u_longlong_t)zs.zs_ptrtbl_zt_numblks);
3068248818dSnd150628 	(void) printf("\t\t\tzt_shift: %llu\n",
3078248818dSnd150628 	    (u_longlong_t)zs.zs_ptrtbl_zt_shift);
3088248818dSnd150628 	(void) printf("\t\t\tzt_blks_copied: %llu\n",
3098248818dSnd150628 	    (u_longlong_t)zs.zs_ptrtbl_blks_copied);
3108248818dSnd150628 	(void) printf("\t\t\tzt_nextblk: %llu\n",
3118248818dSnd150628 	    (u_longlong_t)zs.zs_ptrtbl_nextblk);
3128248818dSnd150628 
313fa9e4066Sahrens 	(void) printf("\t\tZAP entries: %llu\n",
314fa9e4066Sahrens 	    (u_longlong_t)zs.zs_num_entries);
315fa9e4066Sahrens 	(void) printf("\t\tLeaf blocks: %llu\n",
316fa9e4066Sahrens 	    (u_longlong_t)zs.zs_num_leafs);
317fa9e4066Sahrens 	(void) printf("\t\tTotal blocks: %llu\n",
318fa9e4066Sahrens 	    (u_longlong_t)zs.zs_num_blocks);
3198248818dSnd150628 	(void) printf("\t\tzap_block_type: 0x%llx\n",
3208248818dSnd150628 	    (u_longlong_t)zs.zs_block_type);
3218248818dSnd150628 	(void) printf("\t\tzap_magic: 0x%llx\n",
3228248818dSnd150628 	    (u_longlong_t)zs.zs_magic);
3238248818dSnd150628 	(void) printf("\t\tzap_salt: 0x%llx\n",
3248248818dSnd150628 	    (u_longlong_t)zs.zs_salt);
325fa9e4066Sahrens 
326fa9e4066Sahrens 	(void) printf("\t\tLeafs with 2^n pointers:\n");
3270713e232SGeorge Wilson 	dump_histogram(zs.zs_leafs_with_2n_pointers, ZAP_HISTOGRAM_SIZE, 0);
328fa9e4066Sahrens 
329fa9e4066Sahrens 	(void) printf("\t\tBlocks with n*5 entries:\n");
3300713e232SGeorge Wilson 	dump_histogram(zs.zs_blocks_with_n5_entries, ZAP_HISTOGRAM_SIZE, 0);
331fa9e4066Sahrens 
332fa9e4066Sahrens 	(void) printf("\t\tBlocks n/10 full:\n");
3330713e232SGeorge Wilson 	dump_histogram(zs.zs_blocks_n_tenths_full, ZAP_HISTOGRAM_SIZE, 0);
334fa9e4066Sahrens 
335fa9e4066Sahrens 	(void) printf("\t\tEntries with n chunks:\n");
3360713e232SGeorge Wilson 	dump_histogram(zs.zs_entries_using_n_chunks, ZAP_HISTOGRAM_SIZE, 0);
337fa9e4066Sahrens 
338fa9e4066Sahrens 	(void) printf("\t\tBuckets with n entries:\n");
3390713e232SGeorge Wilson 	dump_histogram(zs.zs_buckets_with_n_entries, ZAP_HISTOGRAM_SIZE, 0);
340fa9e4066Sahrens }
341fa9e4066Sahrens 
342fa9e4066Sahrens /*ARGSUSED*/
343fa9e4066Sahrens static void
344fa9e4066Sahrens dump_none(objset_t *os, uint64_t object, void *data, size_t size)
345fa9e4066Sahrens {
346fa9e4066Sahrens }
347fa9e4066Sahrens 
348fa9e4066Sahrens /*ARGSUSED*/
3496de8f417SVictor Latushkin static void
3506de8f417SVictor Latushkin dump_unknown(objset_t *os, uint64_t object, void *data, size_t size)
3516de8f417SVictor Latushkin {
3526de8f417SVictor Latushkin 	(void) printf("\tUNKNOWN OBJECT TYPE\n");
3536de8f417SVictor Latushkin }
3546de8f417SVictor Latushkin 
3556de8f417SVictor Latushkin /*ARGSUSED*/
356fa9e4066Sahrens void
357fa9e4066Sahrens dump_uint8(objset_t *os, uint64_t object, void *data, size_t size)
358fa9e4066Sahrens {
359fa9e4066Sahrens }
360fa9e4066Sahrens 
361fa9e4066Sahrens /*ARGSUSED*/
362fa9e4066Sahrens static void
363fa9e4066Sahrens dump_uint64(objset_t *os, uint64_t object, void *data, size_t size)
364fa9e4066Sahrens {
365fa9e4066Sahrens }
366fa9e4066Sahrens 
367fa9e4066Sahrens /*ARGSUSED*/
368fa9e4066Sahrens static void
369fa9e4066Sahrens dump_zap(objset_t *os, uint64_t object, void *data, size_t size)
370fa9e4066Sahrens {
371fa9e4066Sahrens 	zap_cursor_t zc;
372fa9e4066Sahrens 	zap_attribute_t attr;
373fa9e4066Sahrens 	void *prop;
374fa9e4066Sahrens 	int i;
375fa9e4066Sahrens 
376fa9e4066Sahrens 	dump_zap_stats(os, object);
377fa9e4066Sahrens 	(void) printf("\n");
378fa9e4066Sahrens 
379fa9e4066Sahrens 	for (zap_cursor_init(&zc, os, object);
380fa9e4066Sahrens 	    zap_cursor_retrieve(&zc, &attr) == 0;
381fa9e4066Sahrens 	    zap_cursor_advance(&zc)) {
382fa9e4066Sahrens 		(void) printf("\t\t%s = ", attr.za_name);
383fa9e4066Sahrens 		if (attr.za_num_integers == 0) {
384fa9e4066Sahrens 			(void) printf("\n");
385fa9e4066Sahrens 			continue;
386fa9e4066Sahrens 		}
387fa9e4066Sahrens 		prop = umem_zalloc(attr.za_num_integers *
388fa9e4066Sahrens 		    attr.za_integer_length, UMEM_NOFAIL);
389fa9e4066Sahrens 		(void) zap_lookup(os, object, attr.za_name,
390fa9e4066Sahrens 		    attr.za_integer_length, attr.za_num_integers, prop);
391fa9e4066Sahrens 		if (attr.za_integer_length == 1) {
392fa9e4066Sahrens 			(void) printf("%s", (char *)prop);
393fa9e4066Sahrens 		} else {
394fa9e4066Sahrens 			for (i = 0; i < attr.za_num_integers; i++) {
395fa9e4066Sahrens 				switch (attr.za_integer_length) {
396fa9e4066Sahrens 				case 2:
397fa9e4066Sahrens 					(void) printf("%u ",
398fa9e4066Sahrens 					    ((uint16_t *)prop)[i]);
399fa9e4066Sahrens 					break;
400fa9e4066Sahrens 				case 4:
401fa9e4066Sahrens 					(void) printf("%u ",
402fa9e4066Sahrens 					    ((uint32_t *)prop)[i]);
403fa9e4066Sahrens 					break;
404fa9e4066Sahrens 				case 8:
405fa9e4066Sahrens 					(void) printf("%lld ",
406fa9e4066Sahrens 					    (u_longlong_t)((int64_t *)prop)[i]);
407fa9e4066Sahrens 					break;
408fa9e4066Sahrens 				}
409fa9e4066Sahrens 			}
410fa9e4066Sahrens 		}
411fa9e4066Sahrens 		(void) printf("\n");
412fa9e4066Sahrens 		umem_free(prop, attr.za_num_integers * attr.za_integer_length);
413fa9e4066Sahrens 	}
41487e5029aSahrens 	zap_cursor_fini(&zc);
415fa9e4066Sahrens }
416fa9e4066Sahrens 
417e7437265Sahrens /*ARGSUSED*/
418e7437265Sahrens static void
419486ae710SMatthew Ahrens dump_ddt_zap(objset_t *os, uint64_t object, void *data, size_t size)
420486ae710SMatthew Ahrens {
421486ae710SMatthew Ahrens 	dump_zap_stats(os, object);
422486ae710SMatthew Ahrens 	/* contents are printed elsewhere, properly decoded */
423486ae710SMatthew Ahrens }
424486ae710SMatthew Ahrens 
425486ae710SMatthew Ahrens /*ARGSUSED*/
426486ae710SMatthew Ahrens static void
4270a586ceaSMark Shellenbaum dump_sa_attrs(objset_t *os, uint64_t object, void *data, size_t size)
4280a586ceaSMark Shellenbaum {
4290a586ceaSMark Shellenbaum 	zap_cursor_t zc;
4300a586ceaSMark Shellenbaum 	zap_attribute_t attr;
4310a586ceaSMark Shellenbaum 
4320a586ceaSMark Shellenbaum 	dump_zap_stats(os, object);
4330a586ceaSMark Shellenbaum 	(void) printf("\n");
4340a586ceaSMark Shellenbaum 
4350a586ceaSMark Shellenbaum 	for (zap_cursor_init(&zc, os, object);
4360a586ceaSMark Shellenbaum 	    zap_cursor_retrieve(&zc, &attr) == 0;
4370a586ceaSMark Shellenbaum 	    zap_cursor_advance(&zc)) {
4380a586ceaSMark Shellenbaum 		(void) printf("\t\t%s = ", attr.za_name);
4390a586ceaSMark Shellenbaum 		if (attr.za_num_integers == 0) {
4400a586ceaSMark Shellenbaum 			(void) printf("\n");
4410a586ceaSMark Shellenbaum 			continue;
4420a586ceaSMark Shellenbaum 		}
4430a586ceaSMark Shellenbaum 		(void) printf(" %llx : [%d:%d:%d]\n",
4440a586ceaSMark Shellenbaum 		    (u_longlong_t)attr.za_first_integer,
4450a586ceaSMark Shellenbaum 		    (int)ATTR_LENGTH(attr.za_first_integer),
4460a586ceaSMark Shellenbaum 		    (int)ATTR_BSWAP(attr.za_first_integer),
4470a586ceaSMark Shellenbaum 		    (int)ATTR_NUM(attr.za_first_integer));
4480a586ceaSMark Shellenbaum 	}
4490a586ceaSMark Shellenbaum 	zap_cursor_fini(&zc);
4500a586ceaSMark Shellenbaum }
4510a586ceaSMark Shellenbaum 
4520a586ceaSMark Shellenbaum /*ARGSUSED*/
4530a586ceaSMark Shellenbaum static void
4540a586ceaSMark Shellenbaum dump_sa_layouts(objset_t *os, uint64_t object, void *data, size_t size)
4550a586ceaSMark Shellenbaum {
4560a586ceaSMark Shellenbaum 	zap_cursor_t zc;
4570a586ceaSMark Shellenbaum 	zap_attribute_t attr;
4580a586ceaSMark Shellenbaum 	uint16_t *layout_attrs;
4590a586ceaSMark Shellenbaum 	int i;
4600a586ceaSMark Shellenbaum 
4610a586ceaSMark Shellenbaum 	dump_zap_stats(os, object);
4620a586ceaSMark Shellenbaum 	(void) printf("\n");
4630a586ceaSMark Shellenbaum 
4640a586ceaSMark Shellenbaum 	for (zap_cursor_init(&zc, os, object);
4650a586ceaSMark Shellenbaum 	    zap_cursor_retrieve(&zc, &attr) == 0;
4660a586ceaSMark Shellenbaum 	    zap_cursor_advance(&zc)) {
4670a586ceaSMark Shellenbaum 		(void) printf("\t\t%s = [", attr.za_name);
4680a586ceaSMark Shellenbaum 		if (attr.za_num_integers == 0) {
4690a586ceaSMark Shellenbaum 			(void) printf("\n");
4700a586ceaSMark Shellenbaum 			continue;
4710a586ceaSMark Shellenbaum 		}
4720a586ceaSMark Shellenbaum 
4730a586ceaSMark Shellenbaum 		VERIFY(attr.za_integer_length == 2);
4740a586ceaSMark Shellenbaum 		layout_attrs = umem_zalloc(attr.za_num_integers *
4750a586ceaSMark Shellenbaum 		    attr.za_integer_length, UMEM_NOFAIL);
4760a586ceaSMark Shellenbaum 
4770a586ceaSMark Shellenbaum 		VERIFY(zap_lookup(os, object, attr.za_name,
4780a586ceaSMark Shellenbaum 		    attr.za_integer_length,
4790a586ceaSMark Shellenbaum 		    attr.za_num_integers, layout_attrs) == 0);
4800a586ceaSMark Shellenbaum 
4810a586ceaSMark Shellenbaum 		for (i = 0; i != attr.za_num_integers; i++)
4820a586ceaSMark Shellenbaum 			(void) printf(" %d ", (int)layout_attrs[i]);
4830a586ceaSMark Shellenbaum 		(void) printf("]\n");
4840a586ceaSMark Shellenbaum 		umem_free(layout_attrs,
4850a586ceaSMark Shellenbaum 		    attr.za_num_integers * attr.za_integer_length);
4860a586ceaSMark Shellenbaum 	}
4870a586ceaSMark Shellenbaum 	zap_cursor_fini(&zc);
4880a586ceaSMark Shellenbaum }
4890a586ceaSMark Shellenbaum 
4900a586ceaSMark Shellenbaum /*ARGSUSED*/
4910a586ceaSMark Shellenbaum static void
492e7437265Sahrens dump_zpldir(objset_t *os, uint64_t object, void *data, size_t size)
493e7437265Sahrens {
494e7437265Sahrens 	zap_cursor_t zc;
495e7437265Sahrens 	zap_attribute_t attr;
496e7437265Sahrens 	const char *typenames[] = {
497e7437265Sahrens 		/* 0 */ "not specified",
498e7437265Sahrens 		/* 1 */ "FIFO",
499e7437265Sahrens 		/* 2 */ "Character Device",
500e7437265Sahrens 		/* 3 */ "3 (invalid)",
501e7437265Sahrens 		/* 4 */ "Directory",
502e7437265Sahrens 		/* 5 */ "5 (invalid)",
503e7437265Sahrens 		/* 6 */ "Block Device",
504e7437265Sahrens 		/* 7 */ "7 (invalid)",
505e7437265Sahrens 		/* 8 */ "Regular File",
506e7437265Sahrens 		/* 9 */ "9 (invalid)",
507e7437265Sahrens 		/* 10 */ "Symbolic Link",
508e7437265Sahrens 		/* 11 */ "11 (invalid)",
509e7437265Sahrens 		/* 12 */ "Socket",
510e7437265Sahrens 		/* 13 */ "Door",
511e7437265Sahrens 		/* 14 */ "Event Port",
512e7437265Sahrens 		/* 15 */ "15 (invalid)",
513e7437265Sahrens 	};
514e7437265Sahrens 
515e7437265Sahrens 	dump_zap_stats(os, object);
516e7437265Sahrens 	(void) printf("\n");
517e7437265Sahrens 
518e7437265Sahrens 	for (zap_cursor_init(&zc, os, object);
519e7437265Sahrens 	    zap_cursor_retrieve(&zc, &attr) == 0;
520e7437265Sahrens 	    zap_cursor_advance(&zc)) {
521e7437265Sahrens 		(void) printf("\t\t%s = %lld (type: %s)\n",
522e7437265Sahrens 		    attr.za_name, ZFS_DIRENT_OBJ(attr.za_first_integer),
523e7437265Sahrens 		    typenames[ZFS_DIRENT_TYPE(attr.za_first_integer)]);
524e7437265Sahrens 	}
525e7437265Sahrens 	zap_cursor_fini(&zc);
526e7437265Sahrens }
527e7437265Sahrens 
5280713e232SGeorge Wilson int
5290713e232SGeorge Wilson get_dtl_refcount(vdev_t *vd)
5300713e232SGeorge Wilson {
5310713e232SGeorge Wilson 	int refcount = 0;
5320713e232SGeorge Wilson 
5330713e232SGeorge Wilson 	if (vd->vdev_ops->vdev_op_leaf) {
5340713e232SGeorge Wilson 		space_map_t *sm = vd->vdev_dtl_sm;
5350713e232SGeorge Wilson 
5360713e232SGeorge Wilson 		if (sm != NULL &&
5370713e232SGeorge Wilson 		    sm->sm_dbuf->db_size == sizeof (space_map_phys_t))
5380713e232SGeorge Wilson 			return (1);
5390713e232SGeorge Wilson 		return (0);
5400713e232SGeorge Wilson 	}
5410713e232SGeorge Wilson 
5420713e232SGeorge Wilson 	for (int c = 0; c < vd->vdev_children; c++)
5430713e232SGeorge Wilson 		refcount += get_dtl_refcount(vd->vdev_child[c]);
5440713e232SGeorge Wilson 	return (refcount);
5450713e232SGeorge Wilson }
5460713e232SGeorge Wilson 
5470713e232SGeorge Wilson int
5480713e232SGeorge Wilson get_metaslab_refcount(vdev_t *vd)
5490713e232SGeorge Wilson {
5500713e232SGeorge Wilson 	int refcount = 0;
5510713e232SGeorge Wilson 
5522e4c9986SGeorge Wilson 	if (vd->vdev_top == vd && !vd->vdev_removing) {
5530713e232SGeorge Wilson 		for (int m = 0; m < vd->vdev_ms_count; m++) {
5540713e232SGeorge Wilson 			space_map_t *sm = vd->vdev_ms[m]->ms_sm;
5550713e232SGeorge Wilson 
5560713e232SGeorge Wilson 			if (sm != NULL &&
5570713e232SGeorge Wilson 			    sm->sm_dbuf->db_size == sizeof (space_map_phys_t))
5580713e232SGeorge Wilson 				refcount++;
5590713e232SGeorge Wilson 		}
5600713e232SGeorge Wilson 	}
5610713e232SGeorge Wilson 	for (int c = 0; c < vd->vdev_children; c++)
5620713e232SGeorge Wilson 		refcount += get_metaslab_refcount(vd->vdev_child[c]);
5630713e232SGeorge Wilson 
5640713e232SGeorge Wilson 	return (refcount);
5650713e232SGeorge Wilson }
5660713e232SGeorge Wilson 
5670713e232SGeorge Wilson static int
5680713e232SGeorge Wilson verify_spacemap_refcounts(spa_t *spa)
5690713e232SGeorge Wilson {
5702acef22dSMatthew Ahrens 	uint64_t expected_refcount = 0;
5712acef22dSMatthew Ahrens 	uint64_t actual_refcount;
5720713e232SGeorge Wilson 
5732acef22dSMatthew Ahrens 	(void) feature_get_refcount(spa,
5742acef22dSMatthew Ahrens 	    &spa_feature_table[SPA_FEATURE_SPACEMAP_HISTOGRAM],
5752acef22dSMatthew Ahrens 	    &expected_refcount);
5760713e232SGeorge Wilson 	actual_refcount = get_dtl_refcount(spa->spa_root_vdev);
5770713e232SGeorge Wilson 	actual_refcount += get_metaslab_refcount(spa->spa_root_vdev);
5780713e232SGeorge Wilson 
5790713e232SGeorge Wilson 	if (expected_refcount != actual_refcount) {
5802acef22dSMatthew Ahrens 		(void) printf("space map refcount mismatch: expected %lld != "
5812acef22dSMatthew Ahrens 		    "actual %lld\n",
5822acef22dSMatthew Ahrens 		    (longlong_t)expected_refcount,
5832acef22dSMatthew Ahrens 		    (longlong_t)actual_refcount);
5840713e232SGeorge Wilson 		return (2);
5850713e232SGeorge Wilson 	}
5860713e232SGeorge Wilson 	return (0);
5870713e232SGeorge Wilson }
5880713e232SGeorge Wilson 
589fa9e4066Sahrens static void
5900713e232SGeorge Wilson dump_spacemap(objset_t *os, space_map_t *sm)
591fa9e4066Sahrens {
592fa9e4066Sahrens 	uint64_t alloc, offset, entry;
5938053a263Sck153898 	char *ddata[] = { "ALLOC", "FREE", "CONDENSE", "INVALID",
5948053a263Sck153898 			    "INVALID", "INVALID", "INVALID", "INVALID" };
595fa9e4066Sahrens 
5960713e232SGeorge Wilson 	if (sm == NULL)
597fa9e4066Sahrens 		return;
598fa9e4066Sahrens 
599fa9e4066Sahrens 	/*
600fa9e4066Sahrens 	 * Print out the freelist entries in both encoded and decoded form.
601fa9e4066Sahrens 	 */
602fa9e4066Sahrens 	alloc = 0;
6030713e232SGeorge Wilson 	for (offset = 0; offset < space_map_length(sm);
6040713e232SGeorge Wilson 	    offset += sizeof (entry)) {
6050713e232SGeorge Wilson 		uint8_t mapshift = sm->sm_shift;
6060713e232SGeorge Wilson 
6070713e232SGeorge Wilson 		VERIFY0(dmu_read(os, space_map_object(sm), offset,
6087bfdf011SNeil Perrin 		    sizeof (entry), &entry, DMU_READ_PREFETCH));
609fa9e4066Sahrens 		if (SM_DEBUG_DECODE(entry)) {
6100713e232SGeorge Wilson 
61187219db7SVictor Latushkin 			(void) printf("\t    [%6llu] %s: txg %llu, pass %llu\n",
612fa9e4066Sahrens 			    (u_longlong_t)(offset / sizeof (entry)),
613fa9e4066Sahrens 			    ddata[SM_DEBUG_ACTION_DECODE(entry)],
6145ad82045Snd150628 			    (u_longlong_t)SM_DEBUG_TXG_DECODE(entry),
6155ad82045Snd150628 			    (u_longlong_t)SM_DEBUG_SYNCPASS_DECODE(entry));
616fa9e4066Sahrens 		} else {
61787219db7SVictor Latushkin 			(void) printf("\t    [%6llu]    %c  range:"
61887219db7SVictor Latushkin 			    " %010llx-%010llx  size: %06llx\n",
619fa9e4066Sahrens 			    (u_longlong_t)(offset / sizeof (entry)),
620fa9e4066Sahrens 			    SM_TYPE_DECODE(entry) == SM_ALLOC ? 'A' : 'F',
6215ad82045Snd150628 			    (u_longlong_t)((SM_OFFSET_DECODE(entry) <<
6220713e232SGeorge Wilson 			    mapshift) + sm->sm_start),
6235ad82045Snd150628 			    (u_longlong_t)((SM_OFFSET_DECODE(entry) <<
6240713e232SGeorge Wilson 			    mapshift) + sm->sm_start +
6250713e232SGeorge Wilson 			    (SM_RUN_DECODE(entry) << mapshift)),
6265ad82045Snd150628 			    (u_longlong_t)(SM_RUN_DECODE(entry) << mapshift));
627fa9e4066Sahrens 			if (SM_TYPE_DECODE(entry) == SM_ALLOC)
628fa9e4066Sahrens 				alloc += SM_RUN_DECODE(entry) << mapshift;
629fa9e4066Sahrens 			else
630fa9e4066Sahrens 				alloc -= SM_RUN_DECODE(entry) << mapshift;
631fa9e4066Sahrens 		}
632fa9e4066Sahrens 	}
6330713e232SGeorge Wilson 	if (alloc != space_map_allocated(sm)) {
634fa9e4066Sahrens 		(void) printf("space_map_object alloc (%llu) INCONSISTENT "
635fa9e4066Sahrens 		    "with space map summary (%llu)\n",
6360713e232SGeorge Wilson 		    (u_longlong_t)space_map_allocated(sm), (u_longlong_t)alloc);
637fa9e4066Sahrens 	}
638fa9e4066Sahrens }
639fa9e4066Sahrens 
640fa9e4066Sahrens static void
641d6e555bdSGeorge Wilson dump_metaslab_stats(metaslab_t *msp)
642d6e555bdSGeorge Wilson {
6433f9d6ad7SLin Ling 	char maxbuf[32];
6440713e232SGeorge Wilson 	range_tree_t *rt = msp->ms_tree;
6450713e232SGeorge Wilson 	avl_tree_t *t = &msp->ms_size_tree;
6460713e232SGeorge Wilson 	int free_pct = range_tree_space(rt) * 100 / msp->ms_size;
647d6e555bdSGeorge Wilson 
6480713e232SGeorge Wilson 	zdb_nicenum(metaslab_block_maxsize(msp), maxbuf);
649d6e555bdSGeorge Wilson 
65087219db7SVictor Latushkin 	(void) printf("\t %25s %10lu   %7s  %6s   %4s %4d%%\n",
651d6e555bdSGeorge Wilson 	    "segments", avl_numnodes(t), "maxsize", maxbuf,
652d6e555bdSGeorge Wilson 	    "freepct", free_pct);
6530713e232SGeorge Wilson 	(void) printf("\tIn-memory histogram:\n");
6540713e232SGeorge Wilson 	dump_histogram(rt->rt_histogram, RANGE_TREE_HISTOGRAM_SIZE, 0);
655d6e555bdSGeorge Wilson }
656d6e555bdSGeorge Wilson 
657d6e555bdSGeorge Wilson static void
658fa9e4066Sahrens dump_metaslab(metaslab_t *msp)
659fa9e4066Sahrens {
660fa9e4066Sahrens 	vdev_t *vd = msp->ms_group->mg_vd;
661fa9e4066Sahrens 	spa_t *spa = vd->vdev_spa;
6620713e232SGeorge Wilson 	space_map_t *sm = msp->ms_sm;
6633f9d6ad7SLin Ling 	char freebuf[32];
664fa9e4066Sahrens 
6650713e232SGeorge Wilson 	zdb_nicenum(msp->ms_size - space_map_allocated(sm), freebuf);
666fa9e4066Sahrens 
667fa9e4066Sahrens 	(void) printf(
66887219db7SVictor Latushkin 	    "\tmetaslab %6llu   offset %12llx   spacemap %6llu   free    %5s\n",
6690713e232SGeorge Wilson 	    (u_longlong_t)msp->ms_id, (u_longlong_t)msp->ms_start,
6700713e232SGeorge Wilson 	    (u_longlong_t)space_map_object(sm), freebuf);
671fa9e4066Sahrens 
6720713e232SGeorge Wilson 	if (dump_opt['m'] > 2 && !dump_opt['L']) {
673d6e555bdSGeorge Wilson 		mutex_enter(&msp->ms_lock);
6740713e232SGeorge Wilson 		metaslab_load_wait(msp);
6750713e232SGeorge Wilson 		if (!msp->ms_loaded) {
6760713e232SGeorge Wilson 			VERIFY0(metaslab_load(msp));
6770713e232SGeorge Wilson 			range_tree_stat_verify(msp->ms_tree);
6780713e232SGeorge Wilson 		}
679d6e555bdSGeorge Wilson 		dump_metaslab_stats(msp);
6800713e232SGeorge Wilson 		metaslab_unload(msp);
681d6e555bdSGeorge Wilson 		mutex_exit(&msp->ms_lock);
682d6e555bdSGeorge Wilson 	}
683d6e555bdSGeorge Wilson 
6840713e232SGeorge Wilson 	if (dump_opt['m'] > 1 && sm != NULL &&
6852acef22dSMatthew Ahrens 	    spa_feature_is_active(spa, SPA_FEATURE_SPACEMAP_HISTOGRAM)) {
6860713e232SGeorge Wilson 		/*
6870713e232SGeorge Wilson 		 * The space map histogram represents free space in chunks
6880713e232SGeorge Wilson 		 * of sm_shift (i.e. bucket 0 refers to 2^sm_shift).
6890713e232SGeorge Wilson 		 */
6902e4c9986SGeorge Wilson 		(void) printf("\tOn-disk histogram:\t\tfragmentation %llu\n",
6912e4c9986SGeorge Wilson 		    (u_longlong_t)msp->ms_fragmentation);
6920713e232SGeorge Wilson 		dump_histogram(sm->sm_phys->smp_histogram,
6932e4c9986SGeorge Wilson 		    SPACE_MAP_HISTOGRAM_SIZE, sm->sm_shift);
6940713e232SGeorge Wilson 	}
6950713e232SGeorge Wilson 
6960713e232SGeorge Wilson 	if (dump_opt['d'] > 5 || dump_opt['m'] > 3) {
6970713e232SGeorge Wilson 		ASSERT(msp->ms_size == (1ULL << vd->vdev_ms_shift));
698fa9e4066Sahrens 
699d6e555bdSGeorge Wilson 		mutex_enter(&msp->ms_lock);
7000713e232SGeorge Wilson 		dump_spacemap(spa->spa_meta_objset, msp->ms_sm);
701d6e555bdSGeorge Wilson 		mutex_exit(&msp->ms_lock);
702d6e555bdSGeorge Wilson 	}
70387219db7SVictor Latushkin }
704d6e555bdSGeorge Wilson 
70587219db7SVictor Latushkin static void
70687219db7SVictor Latushkin print_vdev_metaslab_header(vdev_t *vd)
70787219db7SVictor Latushkin {
70887219db7SVictor Latushkin 	(void) printf("\tvdev %10llu\n\t%-10s%5llu   %-19s   %-15s   %-10s\n",
70987219db7SVictor Latushkin 	    (u_longlong_t)vd->vdev_id,
71087219db7SVictor Latushkin 	    "metaslabs", (u_longlong_t)vd->vdev_ms_count,
71187219db7SVictor Latushkin 	    "offset", "spacemap", "free");
71287219db7SVictor Latushkin 	(void) printf("\t%15s   %19s   %15s   %10s\n",
71387219db7SVictor Latushkin 	    "---------------", "-------------------",
71487219db7SVictor Latushkin 	    "---------------", "-------------");
715fa9e4066Sahrens }
716fa9e4066Sahrens 
717fa9e4066Sahrens static void
7182e4c9986SGeorge Wilson dump_metaslab_groups(spa_t *spa)
7192e4c9986SGeorge Wilson {
7202e4c9986SGeorge Wilson 	vdev_t *rvd = spa->spa_root_vdev;
7212e4c9986SGeorge Wilson 	metaslab_class_t *mc = spa_normal_class(spa);
7222e4c9986SGeorge Wilson 	uint64_t fragmentation;
7232e4c9986SGeorge Wilson 
7242e4c9986SGeorge Wilson 	metaslab_class_histogram_verify(mc);
7252e4c9986SGeorge Wilson 
7262e4c9986SGeorge Wilson 	for (int c = 0; c < rvd->vdev_children; c++) {
7272e4c9986SGeorge Wilson 		vdev_t *tvd = rvd->vdev_child[c];
7282e4c9986SGeorge Wilson 		metaslab_group_t *mg = tvd->vdev_mg;
7292e4c9986SGeorge Wilson 
7302e4c9986SGeorge Wilson 		if (mg->mg_class != mc)
7312e4c9986SGeorge Wilson 			continue;
7322e4c9986SGeorge Wilson 
7332e4c9986SGeorge Wilson 		metaslab_group_histogram_verify(mg);
7342e4c9986SGeorge Wilson 		mg->mg_fragmentation = metaslab_group_fragmentation(mg);
7352e4c9986SGeorge Wilson 
7362e4c9986SGeorge Wilson 		(void) printf("\tvdev %10llu\t\tmetaslabs%5llu\t\t"
7372e4c9986SGeorge Wilson 		    "fragmentation",
7382e4c9986SGeorge Wilson 		    (u_longlong_t)tvd->vdev_id,
7392e4c9986SGeorge Wilson 		    (u_longlong_t)tvd->vdev_ms_count);
7402e4c9986SGeorge Wilson 		if (mg->mg_fragmentation == ZFS_FRAG_INVALID) {
7412e4c9986SGeorge Wilson 			(void) printf("%3s\n", "-");
7422e4c9986SGeorge Wilson 		} else {
7432e4c9986SGeorge Wilson 			(void) printf("%3llu%%\n",
7442e4c9986SGeorge Wilson 			    (u_longlong_t)mg->mg_fragmentation);
7452e4c9986SGeorge Wilson 		}
7462e4c9986SGeorge Wilson 		dump_histogram(mg->mg_histogram, RANGE_TREE_HISTOGRAM_SIZE, 0);
7472e4c9986SGeorge Wilson 	}
7482e4c9986SGeorge Wilson 
7492e4c9986SGeorge Wilson 	(void) printf("\tpool %s\tfragmentation", spa_name(spa));
7502e4c9986SGeorge Wilson 	fragmentation = metaslab_class_fragmentation(mc);
7512e4c9986SGeorge Wilson 	if (fragmentation == ZFS_FRAG_INVALID)
7522e4c9986SGeorge Wilson 		(void) printf("\t%3s\n", "-");
7532e4c9986SGeorge Wilson 	else
7542e4c9986SGeorge Wilson 		(void) printf("\t%3llu%%\n", (u_longlong_t)fragmentation);
7552e4c9986SGeorge Wilson 	dump_histogram(mc->mc_histogram, RANGE_TREE_HISTOGRAM_SIZE, 0);
7562e4c9986SGeorge Wilson }
7572e4c9986SGeorge Wilson 
7582e4c9986SGeorge Wilson static void
759fa9e4066Sahrens dump_metaslabs(spa_t *spa)
760fa9e4066Sahrens {
76187219db7SVictor Latushkin 	vdev_t *vd, *rvd = spa->spa_root_vdev;
76287219db7SVictor Latushkin 	uint64_t m, c = 0, children = rvd->vdev_children;
763fa9e4066Sahrens 
764fa9e4066Sahrens 	(void) printf("\nMetaslabs:\n");
765fa9e4066Sahrens 
76687219db7SVictor Latushkin 	if (!dump_opt['d'] && zopt_objects > 0) {
76787219db7SVictor Latushkin 		c = zopt_object[0];
768fa9e4066Sahrens 
76987219db7SVictor Latushkin 		if (c >= children)
77087219db7SVictor Latushkin 			(void) fatal("bad vdev id: %llu", (u_longlong_t)c);
77187219db7SVictor Latushkin 
77287219db7SVictor Latushkin 		if (zopt_objects > 1) {
77387219db7SVictor Latushkin 			vd = rvd->vdev_child[c];
77487219db7SVictor Latushkin 			print_vdev_metaslab_header(vd);
77587219db7SVictor Latushkin 
77687219db7SVictor Latushkin 			for (m = 1; m < zopt_objects; m++) {
77787219db7SVictor Latushkin 				if (zopt_object[m] < vd->vdev_ms_count)
77887219db7SVictor Latushkin 					dump_metaslab(
77987219db7SVictor Latushkin 					    vd->vdev_ms[zopt_object[m]]);
78087219db7SVictor Latushkin 				else
78187219db7SVictor Latushkin 					(void) fprintf(stderr, "bad metaslab "
78287219db7SVictor Latushkin 					    "number %llu\n",
78387219db7SVictor Latushkin 					    (u_longlong_t)zopt_object[m]);
78487219db7SVictor Latushkin 			}
78587219db7SVictor Latushkin 			(void) printf("\n");
78687219db7SVictor Latushkin 			return;
78787219db7SVictor Latushkin 		}
78887219db7SVictor Latushkin 		children = c + 1;
78987219db7SVictor Latushkin 	}
79087219db7SVictor Latushkin 	for (; c < children; c++) {
79187219db7SVictor Latushkin 		vd = rvd->vdev_child[c];
79287219db7SVictor Latushkin 		print_vdev_metaslab_header(vd);
793fa9e4066Sahrens 
794fa9e4066Sahrens 		for (m = 0; m < vd->vdev_ms_count; m++)
795fa9e4066Sahrens 			dump_metaslab(vd->vdev_ms[m]);
796fa9e4066Sahrens 		(void) printf("\n");
797fa9e4066Sahrens 	}
798fa9e4066Sahrens }
799fa9e4066Sahrens 
800fa9e4066Sahrens static void
801b24ab676SJeff Bonwick dump_dde(const ddt_t *ddt, const ddt_entry_t *dde, uint64_t index)
802b24ab676SJeff Bonwick {
803b24ab676SJeff Bonwick 	const ddt_phys_t *ddp = dde->dde_phys;
804b24ab676SJeff Bonwick 	const ddt_key_t *ddk = &dde->dde_key;
805b24ab676SJeff Bonwick 	char *types[4] = { "ditto", "single", "double", "triple" };
806b24ab676SJeff Bonwick 	char blkbuf[BP_SPRINTF_LEN];
807b24ab676SJeff Bonwick 	blkptr_t blk;
808b24ab676SJeff Bonwick 
809b24ab676SJeff Bonwick 	for (int p = 0; p < DDT_PHYS_TYPES; p++, ddp++) {
810b24ab676SJeff Bonwick 		if (ddp->ddp_phys_birth == 0)
811b24ab676SJeff Bonwick 			continue;
812bbfd46c4SJeff Bonwick 		ddt_bp_create(ddt->ddt_checksum, ddk, ddp, &blk);
81343466aaeSMax Grossman 		snprintf_blkptr(blkbuf, sizeof (blkbuf), &blk);
814b24ab676SJeff Bonwick 		(void) printf("index %llx refcnt %llu %s %s\n",
815b24ab676SJeff Bonwick 		    (u_longlong_t)index, (u_longlong_t)ddp->ddp_refcnt,
816b24ab676SJeff Bonwick 		    types[p], blkbuf);
817b24ab676SJeff Bonwick 	}
818b24ab676SJeff Bonwick }
819b24ab676SJeff Bonwick 
820b24ab676SJeff Bonwick static void
821b24ab676SJeff Bonwick dump_dedup_ratio(const ddt_stat_t *dds)
822b24ab676SJeff Bonwick {
823b24ab676SJeff Bonwick 	double rL, rP, rD, D, dedup, compress, copies;
824b24ab676SJeff Bonwick 
825b24ab676SJeff Bonwick 	if (dds->dds_blocks == 0)
826b24ab676SJeff Bonwick 		return;
827b24ab676SJeff Bonwick 
828b24ab676SJeff Bonwick 	rL = (double)dds->dds_ref_lsize;
829b24ab676SJeff Bonwick 	rP = (double)dds->dds_ref_psize;
830b24ab676SJeff Bonwick 	rD = (double)dds->dds_ref_dsize;
831b24ab676SJeff Bonwick 	D = (double)dds->dds_dsize;
832b24ab676SJeff Bonwick 
833b24ab676SJeff Bonwick 	dedup = rD / D;
834b24ab676SJeff Bonwick 	compress = rL / rP;
835b24ab676SJeff Bonwick 	copies = rD / rP;
836b24ab676SJeff Bonwick 
837b24ab676SJeff Bonwick 	(void) printf("dedup = %.2f, compress = %.2f, copies = %.2f, "
838b24ab676SJeff Bonwick 	    "dedup * compress / copies = %.2f\n\n",
839b24ab676SJeff Bonwick 	    dedup, compress, copies, dedup * compress / copies);
840b24ab676SJeff Bonwick }
841b24ab676SJeff Bonwick 
842b24ab676SJeff Bonwick static void
843b24ab676SJeff Bonwick dump_ddt(ddt_t *ddt, enum ddt_type type, enum ddt_class class)
844b24ab676SJeff Bonwick {
845b24ab676SJeff Bonwick 	char name[DDT_NAMELEN];
846b24ab676SJeff Bonwick 	ddt_entry_t dde;
847b24ab676SJeff Bonwick 	uint64_t walk = 0;
848b24ab676SJeff Bonwick 	dmu_object_info_t doi;
849b24ab676SJeff Bonwick 	uint64_t count, dspace, mspace;
850b24ab676SJeff Bonwick 	int error;
851b24ab676SJeff Bonwick 
852b24ab676SJeff Bonwick 	error = ddt_object_info(ddt, type, class, &doi);
853b24ab676SJeff Bonwick 
854b24ab676SJeff Bonwick 	if (error == ENOENT)
855b24ab676SJeff Bonwick 		return;
856b24ab676SJeff Bonwick 	ASSERT(error == 0);
857b24ab676SJeff Bonwick 
8587448a079SGeorge Wilson 	if ((count = ddt_object_count(ddt, type, class)) == 0)
8597448a079SGeorge Wilson 		return;
8607448a079SGeorge Wilson 
861b24ab676SJeff Bonwick 	dspace = doi.doi_physical_blocks_512 << 9;
862b24ab676SJeff Bonwick 	mspace = doi.doi_fill_count * doi.doi_data_block_size;
863b24ab676SJeff Bonwick 
864b24ab676SJeff Bonwick 	ddt_object_name(ddt, type, class, name);
865b24ab676SJeff Bonwick 
866b24ab676SJeff Bonwick 	(void) printf("%s: %llu entries, size %llu on disk, %llu in core\n",
867b24ab676SJeff Bonwick 	    name,
868b24ab676SJeff Bonwick 	    (u_longlong_t)count,
869b24ab676SJeff Bonwick 	    (u_longlong_t)(dspace / count),
870b24ab676SJeff Bonwick 	    (u_longlong_t)(mspace / count));
871b24ab676SJeff Bonwick 
872b24ab676SJeff Bonwick 	if (dump_opt['D'] < 3)
873b24ab676SJeff Bonwick 		return;
874b24ab676SJeff Bonwick 
8759eb19f4dSGeorge Wilson 	zpool_dump_ddt(NULL, &ddt->ddt_histogram[type][class]);
876b24ab676SJeff Bonwick 
877b24ab676SJeff Bonwick 	if (dump_opt['D'] < 4)
878b24ab676SJeff Bonwick 		return;
879b24ab676SJeff Bonwick 
880b24ab676SJeff Bonwick 	if (dump_opt['D'] < 5 && class == DDT_CLASS_UNIQUE)
881b24ab676SJeff Bonwick 		return;
882b24ab676SJeff Bonwick 
883b24ab676SJeff Bonwick 	(void) printf("%s contents:\n\n", name);
884b24ab676SJeff Bonwick 
885bbfd46c4SJeff Bonwick 	while ((error = ddt_object_walk(ddt, type, class, &walk, &dde)) == 0)
886b24ab676SJeff Bonwick 		dump_dde(ddt, &dde, walk);
887b24ab676SJeff Bonwick 
888b24ab676SJeff Bonwick 	ASSERT(error == ENOENT);
889b24ab676SJeff Bonwick 
890b24ab676SJeff Bonwick 	(void) printf("\n");
891b24ab676SJeff Bonwick }
892b24ab676SJeff Bonwick 
893b24ab676SJeff Bonwick static void
894b24ab676SJeff Bonwick dump_all_ddts(spa_t *spa)
895b24ab676SJeff Bonwick {
896b24ab676SJeff Bonwick 	ddt_histogram_t ddh_total = { 0 };
897b24ab676SJeff Bonwick 	ddt_stat_t dds_total = { 0 };
898b24ab676SJeff Bonwick 
899b24ab676SJeff Bonwick 	for (enum zio_checksum c = 0; c < ZIO_CHECKSUM_FUNCTIONS; c++) {
900b24ab676SJeff Bonwick 		ddt_t *ddt = spa->spa_ddt[c];
901b24ab676SJeff Bonwick 		for (enum ddt_type type = 0; type < DDT_TYPES; type++) {
902b24ab676SJeff Bonwick 			for (enum ddt_class class = 0; class < DDT_CLASSES;
903b24ab676SJeff Bonwick 			    class++) {
904b24ab676SJeff Bonwick 				dump_ddt(ddt, type, class);
905b24ab676SJeff Bonwick 			}
906b24ab676SJeff Bonwick 		}
907b24ab676SJeff Bonwick 	}
908b24ab676SJeff Bonwick 
9099eb19f4dSGeorge Wilson 	ddt_get_dedup_stats(spa, &dds_total);
910b24ab676SJeff Bonwick 
911b24ab676SJeff Bonwick 	if (dds_total.dds_blocks == 0) {
912b24ab676SJeff Bonwick 		(void) printf("All DDTs are empty\n");
913b24ab676SJeff Bonwick 		return;
914b24ab676SJeff Bonwick 	}
915b24ab676SJeff Bonwick 
916b24ab676SJeff Bonwick 	(void) printf("\n");
917b24ab676SJeff Bonwick 
918b24ab676SJeff Bonwick 	if (dump_opt['D'] > 1) {
919b24ab676SJeff Bonwick 		(void) printf("DDT histogram (aggregated over all DDTs):\n");
9209eb19f4dSGeorge Wilson 		ddt_get_dedup_histogram(spa, &ddh_total);
9219eb19f4dSGeorge Wilson 		zpool_dump_ddt(&dds_total, &ddh_total);
922b24ab676SJeff Bonwick 	}
923b24ab676SJeff Bonwick 
924b24ab676SJeff Bonwick 	dump_dedup_ratio(&dds_total);
925b24ab676SJeff Bonwick }
926b24ab676SJeff Bonwick 
927b24ab676SJeff Bonwick static void
9280713e232SGeorge Wilson dump_dtl_seg(void *arg, uint64_t start, uint64_t size)
9298ad4d6ddSJeff Bonwick {
9300713e232SGeorge Wilson 	char *prefix = arg;
9318ad4d6ddSJeff Bonwick 
9328ad4d6ddSJeff Bonwick 	(void) printf("%s [%llu,%llu) length %llu\n",
9338ad4d6ddSJeff Bonwick 	    prefix,
9348ad4d6ddSJeff Bonwick 	    (u_longlong_t)start,
9358ad4d6ddSJeff Bonwick 	    (u_longlong_t)(start + size),
9368ad4d6ddSJeff Bonwick 	    (u_longlong_t)(size));
9378ad4d6ddSJeff Bonwick }
9388ad4d6ddSJeff Bonwick 
9398ad4d6ddSJeff Bonwick static void
940fa9e4066Sahrens dump_dtl(vdev_t *vd, int indent)
941fa9e4066Sahrens {
9428ad4d6ddSJeff Bonwick 	spa_t *spa = vd->vdev_spa;
9438ad4d6ddSJeff Bonwick 	boolean_t required;
9448ad4d6ddSJeff Bonwick 	char *name[DTL_TYPES] = { "missing", "partial", "scrub", "outage" };
9458ad4d6ddSJeff Bonwick 	char prefix[256];
9468ad4d6ddSJeff Bonwick 
9478f18d1faSGeorge Wilson 	spa_vdev_state_enter(spa, SCL_NONE);
9488ad4d6ddSJeff Bonwick 	required = vdev_dtl_required(vd);
9498ad4d6ddSJeff Bonwick 	(void) spa_vdev_state_exit(spa, NULL, 0);
950fa9e4066Sahrens 
951fa9e4066Sahrens 	if (indent == 0)
952fa9e4066Sahrens 		(void) printf("\nDirty time logs:\n\n");
953fa9e4066Sahrens 
9548ad4d6ddSJeff Bonwick 	(void) printf("\t%*s%s [%s]\n", indent, "",
955e14bb325SJeff Bonwick 	    vd->vdev_path ? vd->vdev_path :
9568ad4d6ddSJeff Bonwick 	    vd->vdev_parent ? vd->vdev_ops->vdev_op_type : spa_name(spa),
9578ad4d6ddSJeff Bonwick 	    required ? "DTL-required" : "DTL-expendable");
958fa9e4066Sahrens 
9598ad4d6ddSJeff Bonwick 	for (int t = 0; t < DTL_TYPES; t++) {
9600713e232SGeorge Wilson 		range_tree_t *rt = vd->vdev_dtl[t];
9610713e232SGeorge Wilson 		if (range_tree_space(rt) == 0)
9628ad4d6ddSJeff Bonwick 			continue;
9638ad4d6ddSJeff Bonwick 		(void) snprintf(prefix, sizeof (prefix), "\t%*s%s",
9648ad4d6ddSJeff Bonwick 		    indent + 2, "", name[t]);
9650713e232SGeorge Wilson 		mutex_enter(rt->rt_lock);
9660713e232SGeorge Wilson 		range_tree_walk(rt, dump_dtl_seg, prefix);
9670713e232SGeorge Wilson 		mutex_exit(rt->rt_lock);
9688ad4d6ddSJeff Bonwick 		if (dump_opt['d'] > 5 && vd->vdev_children == 0)
9690713e232SGeorge Wilson 			dump_spacemap(spa->spa_meta_objset, vd->vdev_dtl_sm);
970fa9e4066Sahrens 	}
971fa9e4066Sahrens 
9728ad4d6ddSJeff Bonwick 	for (int c = 0; c < vd->vdev_children; c++)
973fa9e4066Sahrens 		dump_dtl(vd->vdev_child[c], indent + 4);
974fa9e4066Sahrens }
975fa9e4066Sahrens 
9768f18d1faSGeorge Wilson static void
9778f18d1faSGeorge Wilson dump_history(spa_t *spa)
9788f18d1faSGeorge Wilson {
9798f18d1faSGeorge Wilson 	nvlist_t **events = NULL;
9808f18d1faSGeorge Wilson 	char buf[SPA_MAXBLOCKSIZE];
981e4161df6SVictor Latushkin 	uint64_t resid, len, off = 0;
9828f18d1faSGeorge Wilson 	uint_t num = 0;
9838f18d1faSGeorge Wilson 	int error;
9848f18d1faSGeorge Wilson 	time_t tsec;
9858f18d1faSGeorge Wilson 	struct tm t;
9868f18d1faSGeorge Wilson 	char tbuf[30];
9878f18d1faSGeorge Wilson 	char internalstr[MAXPATHLEN];
9888f18d1faSGeorge Wilson 
9898f18d1faSGeorge Wilson 	do {
990e4161df6SVictor Latushkin 		len = sizeof (buf);
991e4161df6SVictor Latushkin 
9928f18d1faSGeorge Wilson 		if ((error = spa_history_get(spa, &off, &len, buf)) != 0) {
9938f18d1faSGeorge Wilson 			(void) fprintf(stderr, "Unable to read history: "
9948f18d1faSGeorge Wilson 			    "error %d\n", error);
9958f18d1faSGeorge Wilson 			return;
9968f18d1faSGeorge Wilson 		}
9978f18d1faSGeorge Wilson 
9988f18d1faSGeorge Wilson 		if (zpool_history_unpack(buf, len, &resid, &events, &num) != 0)
9998f18d1faSGeorge Wilson 			break;
10008f18d1faSGeorge Wilson 
10018f18d1faSGeorge Wilson 		off -= resid;
10028f18d1faSGeorge Wilson 	} while (len != 0);
10038f18d1faSGeorge Wilson 
10048f18d1faSGeorge Wilson 	(void) printf("\nHistory:\n");
10058f18d1faSGeorge Wilson 	for (int i = 0; i < num; i++) {
10068f18d1faSGeorge Wilson 		uint64_t time, txg, ievent;
10078f18d1faSGeorge Wilson 		char *cmd, *intstr;
10084445fffbSMatthew Ahrens 		boolean_t printed = B_FALSE;
10098f18d1faSGeorge Wilson 
10108f18d1faSGeorge Wilson 		if (nvlist_lookup_uint64(events[i], ZPOOL_HIST_TIME,
10118f18d1faSGeorge Wilson 		    &time) != 0)
10124445fffbSMatthew Ahrens 			goto next;
10138f18d1faSGeorge Wilson 		if (nvlist_lookup_string(events[i], ZPOOL_HIST_CMD,
10148f18d1faSGeorge Wilson 		    &cmd) != 0) {
10158f18d1faSGeorge Wilson 			if (nvlist_lookup_uint64(events[i],
10168f18d1faSGeorge Wilson 			    ZPOOL_HIST_INT_EVENT, &ievent) != 0)
10174445fffbSMatthew Ahrens 				goto next;
10188f18d1faSGeorge Wilson 			verify(nvlist_lookup_uint64(events[i],
10198f18d1faSGeorge Wilson 			    ZPOOL_HIST_TXG, &txg) == 0);
10208f18d1faSGeorge Wilson 			verify(nvlist_lookup_string(events[i],
10218f18d1faSGeorge Wilson 			    ZPOOL_HIST_INT_STR, &intstr) == 0);
10224445fffbSMatthew Ahrens 			if (ievent >= ZFS_NUM_LEGACY_HISTORY_EVENTS)
10234445fffbSMatthew Ahrens 				goto next;
10248f18d1faSGeorge Wilson 
10258f18d1faSGeorge Wilson 			(void) snprintf(internalstr,
10268f18d1faSGeorge Wilson 			    sizeof (internalstr),
10278f18d1faSGeorge Wilson 			    "[internal %s txg:%lld] %s",
10283f9d6ad7SLin Ling 			    zfs_history_event_names[ievent], txg,
10298f18d1faSGeorge Wilson 			    intstr);
10308f18d1faSGeorge Wilson 			cmd = internalstr;
10318f18d1faSGeorge Wilson 		}
10328f18d1faSGeorge Wilson 		tsec = time;
10338f18d1faSGeorge Wilson 		(void) localtime_r(&tsec, &t);
10348f18d1faSGeorge Wilson 		(void) strftime(tbuf, sizeof (tbuf), "%F.%T", &t);
10358f18d1faSGeorge Wilson 		(void) printf("%s %s\n", tbuf, cmd);
10364445fffbSMatthew Ahrens 		printed = B_TRUE;
10374445fffbSMatthew Ahrens 
10384445fffbSMatthew Ahrens next:
10394445fffbSMatthew Ahrens 		if (dump_opt['h'] > 1) {
10404445fffbSMatthew Ahrens 			if (!printed)
10414445fffbSMatthew Ahrens 				(void) printf("unrecognized record:\n");
10424445fffbSMatthew Ahrens 			dump_nvlist(events[i], 2);
10434445fffbSMatthew Ahrens 		}
10448f18d1faSGeorge Wilson 	}
10458f18d1faSGeorge Wilson }
10468f18d1faSGeorge Wilson 
1047fa9e4066Sahrens /*ARGSUSED*/
1048fa9e4066Sahrens static void
1049fa9e4066Sahrens dump_dnode(objset_t *os, uint64_t object, void *data, size_t size)
1050fa9e4066Sahrens {
1051fa9e4066Sahrens }
1052fa9e4066Sahrens 
1053fa9e4066Sahrens static uint64_t
10547802d7bfSMatthew Ahrens blkid2offset(const dnode_phys_t *dnp, const blkptr_t *bp,
10557802d7bfSMatthew Ahrens     const zbookmark_phys_t *zb)
1056fa9e4066Sahrens {
1057b24ab676SJeff Bonwick 	if (dnp == NULL) {
1058b24ab676SJeff Bonwick 		ASSERT(zb->zb_level < 0);
1059b24ab676SJeff Bonwick 		if (zb->zb_object == 0)
1060b24ab676SJeff Bonwick 			return (zb->zb_blkid);
1061b24ab676SJeff Bonwick 		return (zb->zb_blkid * BP_GET_LSIZE(bp));
1062b24ab676SJeff Bonwick 	}
1063fa9e4066Sahrens 
1064b24ab676SJeff Bonwick 	ASSERT(zb->zb_level >= 0);
1065b24ab676SJeff Bonwick 
1066b24ab676SJeff Bonwick 	return ((zb->zb_blkid <<
1067b24ab676SJeff Bonwick 	    (zb->zb_level * (dnp->dn_indblkshift - SPA_BLKPTRSHIFT))) *
1068fa9e4066Sahrens 	    dnp->dn_datablkszsec << SPA_MINBLOCKSHIFT);
1069fa9e4066Sahrens }
1070fa9e4066Sahrens 
107144cd46caSbillm static void
107243466aaeSMax Grossman snprintf_blkptr_compact(char *blkbuf, size_t buflen, const blkptr_t *bp)
107344cd46caSbillm {
1074cde58dbcSMatthew Ahrens 	const dva_t *dva = bp->blk_dva;
1075b24ab676SJeff Bonwick 	int ndvas = dump_opt['d'] > 5 ? BP_GET_NDVAS(bp) : 1;
1076b24ab676SJeff Bonwick 
1077490d05b9SMatthew Ahrens 	if (dump_opt['b'] >= 6) {
107843466aaeSMax Grossman 		snprintf_blkptr(blkbuf, buflen, bp);
1079b24ab676SJeff Bonwick 		return;
1080b24ab676SJeff Bonwick 	}
108144cd46caSbillm 
10825d7b4d43SMatthew Ahrens 	if (BP_IS_EMBEDDED(bp)) {
10835d7b4d43SMatthew Ahrens 		(void) sprintf(blkbuf,
10845d7b4d43SMatthew Ahrens 		    "EMBEDDED et=%u %llxL/%llxP B=%llu",
10855d7b4d43SMatthew Ahrens 		    (int)BPE_GET_ETYPE(bp),
10865d7b4d43SMatthew Ahrens 		    (u_longlong_t)BPE_GET_LSIZE(bp),
10875d7b4d43SMatthew Ahrens 		    (u_longlong_t)BPE_GET_PSIZE(bp),
10885d7b4d43SMatthew Ahrens 		    (u_longlong_t)bp->blk_birth);
10895d7b4d43SMatthew Ahrens 		return;
10905d7b4d43SMatthew Ahrens 	}
109144cd46caSbillm 
10925d7b4d43SMatthew Ahrens 	blkbuf[0] = '\0';
1093b24ab676SJeff Bonwick 	for (int i = 0; i < ndvas; i++)
109443466aaeSMax Grossman 		(void) snprintf(blkbuf + strlen(blkbuf),
109543466aaeSMax Grossman 		    buflen - strlen(blkbuf), "%llu:%llx:%llx ",
109644cd46caSbillm 		    (u_longlong_t)DVA_GET_VDEV(&dva[i]),
109744cd46caSbillm 		    (u_longlong_t)DVA_GET_OFFSET(&dva[i]),
109844cd46caSbillm 		    (u_longlong_t)DVA_GET_ASIZE(&dva[i]));
109944cd46caSbillm 
110043466aaeSMax Grossman 	if (BP_IS_HOLE(bp)) {
110143466aaeSMax Grossman 		(void) snprintf(blkbuf + strlen(blkbuf),
110243466aaeSMax Grossman 		    buflen - strlen(blkbuf), "B=%llu",
110343466aaeSMax Grossman 		    (u_longlong_t)bp->blk_birth);
110443466aaeSMax Grossman 	} else {
110543466aaeSMax Grossman 		(void) snprintf(blkbuf + strlen(blkbuf),
110643466aaeSMax Grossman 		    buflen - strlen(blkbuf),
1107b24ab676SJeff Bonwick 		    "%llxL/%llxP F=%llu B=%llu/%llu",
110844cd46caSbillm 		    (u_longlong_t)BP_GET_LSIZE(bp),
110944cd46caSbillm 		    (u_longlong_t)BP_GET_PSIZE(bp),
11105d7b4d43SMatthew Ahrens 		    (u_longlong_t)BP_GET_FILL(bp),
1111b24ab676SJeff Bonwick 		    (u_longlong_t)bp->blk_birth,
1112b24ab676SJeff Bonwick 		    (u_longlong_t)BP_PHYSICAL_BIRTH(bp));
111344cd46caSbillm 	}
111443466aaeSMax Grossman }
111544cd46caSbillm 
111688b7b0f2SMatthew Ahrens static void
11177802d7bfSMatthew Ahrens print_indirect(blkptr_t *bp, const zbookmark_phys_t *zb,
111888b7b0f2SMatthew Ahrens     const dnode_phys_t *dnp)
1119fa9e4066Sahrens {
112088b7b0f2SMatthew Ahrens 	char blkbuf[BP_SPRINTF_LEN];
1121fa9e4066Sahrens 	int l;
1122fa9e4066Sahrens 
11235d7b4d43SMatthew Ahrens 	if (!BP_IS_EMBEDDED(bp)) {
1124fa9e4066Sahrens 		ASSERT3U(BP_GET_TYPE(bp), ==, dnp->dn_type);
1125fa9e4066Sahrens 		ASSERT3U(BP_GET_LEVEL(bp), ==, zb->zb_level);
11265d7b4d43SMatthew Ahrens 	}
1127fa9e4066Sahrens 
1128b24ab676SJeff Bonwick 	(void) printf("%16llx ", (u_longlong_t)blkid2offset(dnp, bp, zb));
1129fa9e4066Sahrens 
1130fa9e4066Sahrens 	ASSERT(zb->zb_level >= 0);
1131fa9e4066Sahrens 
1132fa9e4066Sahrens 	for (l = dnp->dn_nlevels - 1; l >= -1; l--) {
1133fa9e4066Sahrens 		if (l == zb->zb_level) {
113488b7b0f2SMatthew Ahrens 			(void) printf("L%llx", (u_longlong_t)zb->zb_level);
1135fa9e4066Sahrens 		} else {
113688b7b0f2SMatthew Ahrens 			(void) printf(" ");
1137fa9e4066Sahrens 		}
1138fa9e4066Sahrens 	}
1139fa9e4066Sahrens 
114043466aaeSMax Grossman 	snprintf_blkptr_compact(blkbuf, sizeof (blkbuf), bp);
114144cd46caSbillm 	(void) printf("%s\n", blkbuf);
1142fa9e4066Sahrens }
1143fa9e4066Sahrens 
114488b7b0f2SMatthew Ahrens static int
114588b7b0f2SMatthew Ahrens visit_indirect(spa_t *spa, const dnode_phys_t *dnp,
11467802d7bfSMatthew Ahrens     blkptr_t *bp, const zbookmark_phys_t *zb)
114788b7b0f2SMatthew Ahrens {
1148e4161df6SVictor Latushkin 	int err = 0;
114988b7b0f2SMatthew Ahrens 
115088b7b0f2SMatthew Ahrens 	if (bp->blk_birth == 0)
115188b7b0f2SMatthew Ahrens 		return (0);
115288b7b0f2SMatthew Ahrens 
115388b7b0f2SMatthew Ahrens 	print_indirect(bp, zb, dnp);
115488b7b0f2SMatthew Ahrens 
115543466aaeSMax Grossman 	if (BP_GET_LEVEL(bp) > 0 && !BP_IS_HOLE(bp)) {
115688b7b0f2SMatthew Ahrens 		uint32_t flags = ARC_WAIT;
115788b7b0f2SMatthew Ahrens 		int i;
115888b7b0f2SMatthew Ahrens 		blkptr_t *cbp;
115988b7b0f2SMatthew Ahrens 		int epb = BP_GET_LSIZE(bp) >> SPA_BLKPTRSHIFT;
116088b7b0f2SMatthew Ahrens 		arc_buf_t *buf;
116188b7b0f2SMatthew Ahrens 		uint64_t fill = 0;
116288b7b0f2SMatthew Ahrens 
11631b912ec7SGeorge Wilson 		err = arc_read(NULL, spa, bp, arc_getbuf_func, &buf,
116488b7b0f2SMatthew Ahrens 		    ZIO_PRIORITY_ASYNC_READ, ZIO_FLAG_CANFAIL, &flags, zb);
116588b7b0f2SMatthew Ahrens 		if (err)
116688b7b0f2SMatthew Ahrens 			return (err);
11673f9d6ad7SLin Ling 		ASSERT(buf->b_data);
116888b7b0f2SMatthew Ahrens 
116988b7b0f2SMatthew Ahrens 		/* recursively visit blocks below this */
117088b7b0f2SMatthew Ahrens 		cbp = buf->b_data;
117188b7b0f2SMatthew Ahrens 		for (i = 0; i < epb; i++, cbp++) {
11727802d7bfSMatthew Ahrens 			zbookmark_phys_t czb;
117388b7b0f2SMatthew Ahrens 
117488b7b0f2SMatthew Ahrens 			SET_BOOKMARK(&czb, zb->zb_objset, zb->zb_object,
117588b7b0f2SMatthew Ahrens 			    zb->zb_level - 1,
117688b7b0f2SMatthew Ahrens 			    zb->zb_blkid * epb + i);
117788b7b0f2SMatthew Ahrens 			err = visit_indirect(spa, dnp, cbp, &czb);
117888b7b0f2SMatthew Ahrens 			if (err)
117988b7b0f2SMatthew Ahrens 				break;
11805d7b4d43SMatthew Ahrens 			fill += BP_GET_FILL(cbp);
118188b7b0f2SMatthew Ahrens 		}
11828ad4d6ddSJeff Bonwick 		if (!err)
11835d7b4d43SMatthew Ahrens 			ASSERT3U(fill, ==, BP_GET_FILL(bp));
118488b7b0f2SMatthew Ahrens 		(void) arc_buf_remove_ref(buf, &buf);
118588b7b0f2SMatthew Ahrens 	}
118688b7b0f2SMatthew Ahrens 
118788b7b0f2SMatthew Ahrens 	return (err);
1188fa9e4066Sahrens }
1189fa9e4066Sahrens 
1190fa9e4066Sahrens /*ARGSUSED*/
1191fa9e4066Sahrens static void
119288b7b0f2SMatthew Ahrens dump_indirect(dnode_t *dn)
1193fa9e4066Sahrens {
119488b7b0f2SMatthew Ahrens 	dnode_phys_t *dnp = dn->dn_phys;
119588b7b0f2SMatthew Ahrens 	int j;
11967802d7bfSMatthew Ahrens 	zbookmark_phys_t czb;
1197fa9e4066Sahrens 
1198fa9e4066Sahrens 	(void) printf("Indirect blocks:\n");
1199fa9e4066Sahrens 
1200503ad85cSMatthew Ahrens 	SET_BOOKMARK(&czb, dmu_objset_id(dn->dn_objset),
120188b7b0f2SMatthew Ahrens 	    dn->dn_object, dnp->dn_nlevels - 1, 0);
120288b7b0f2SMatthew Ahrens 	for (j = 0; j < dnp->dn_nblkptr; j++) {
120388b7b0f2SMatthew Ahrens 		czb.zb_blkid = j;
1204503ad85cSMatthew Ahrens 		(void) visit_indirect(dmu_objset_spa(dn->dn_objset), dnp,
120588b7b0f2SMatthew Ahrens 		    &dnp->dn_blkptr[j], &czb);
120688b7b0f2SMatthew Ahrens 	}
1207fa9e4066Sahrens 
1208fa9e4066Sahrens 	(void) printf("\n");
1209fa9e4066Sahrens }
1210fa9e4066Sahrens 
1211fa9e4066Sahrens /*ARGSUSED*/
1212fa9e4066Sahrens static void
1213fa9e4066Sahrens dump_dsl_dir(objset_t *os, uint64_t object, void *data, size_t size)
1214fa9e4066Sahrens {
1215fa9e4066Sahrens 	dsl_dir_phys_t *dd = data;
1216fa9e4066Sahrens 	time_t crtime;
12173f9d6ad7SLin Ling 	char nice[32];
1218fa9e4066Sahrens 
1219fa9e4066Sahrens 	if (dd == NULL)
1220fa9e4066Sahrens 		return;
1221fa9e4066Sahrens 
1222da6c28aaSamw 	ASSERT3U(size, >=, sizeof (dsl_dir_phys_t));
1223fa9e4066Sahrens 
1224fa9e4066Sahrens 	crtime = dd->dd_creation_time;
1225fa9e4066Sahrens 	(void) printf("\t\tcreation_time = %s", ctime(&crtime));
1226fa9e4066Sahrens 	(void) printf("\t\thead_dataset_obj = %llu\n",
1227fa9e4066Sahrens 	    (u_longlong_t)dd->dd_head_dataset_obj);
1228fa9e4066Sahrens 	(void) printf("\t\tparent_dir_obj = %llu\n",
1229fa9e4066Sahrens 	    (u_longlong_t)dd->dd_parent_obj);
12303cb34c60Sahrens 	(void) printf("\t\torigin_obj = %llu\n",
12313cb34c60Sahrens 	    (u_longlong_t)dd->dd_origin_obj);
1232fa9e4066Sahrens 	(void) printf("\t\tchild_dir_zapobj = %llu\n",
1233fa9e4066Sahrens 	    (u_longlong_t)dd->dd_child_dir_zapobj);
12343f9d6ad7SLin Ling 	zdb_nicenum(dd->dd_used_bytes, nice);
123574e7dc98SMatthew Ahrens 	(void) printf("\t\tused_bytes = %s\n", nice);
12363f9d6ad7SLin Ling 	zdb_nicenum(dd->dd_compressed_bytes, nice);
123774e7dc98SMatthew Ahrens 	(void) printf("\t\tcompressed_bytes = %s\n", nice);
12383f9d6ad7SLin Ling 	zdb_nicenum(dd->dd_uncompressed_bytes, nice);
123974e7dc98SMatthew Ahrens 	(void) printf("\t\tuncompressed_bytes = %s\n", nice);
12403f9d6ad7SLin Ling 	zdb_nicenum(dd->dd_quota, nice);
124174e7dc98SMatthew Ahrens 	(void) printf("\t\tquota = %s\n", nice);
12423f9d6ad7SLin Ling 	zdb_nicenum(dd->dd_reserved, nice);
124374e7dc98SMatthew Ahrens 	(void) printf("\t\treserved = %s\n", nice);
1244fa9e4066Sahrens 	(void) printf("\t\tprops_zapobj = %llu\n",
1245fa9e4066Sahrens 	    (u_longlong_t)dd->dd_props_zapobj);
1246ecd6cf80Smarks 	(void) printf("\t\tdeleg_zapobj = %llu\n",
1247ecd6cf80Smarks 	    (u_longlong_t)dd->dd_deleg_zapobj);
124874e7dc98SMatthew Ahrens 	(void) printf("\t\tflags = %llx\n",
124974e7dc98SMatthew Ahrens 	    (u_longlong_t)dd->dd_flags);
125074e7dc98SMatthew Ahrens 
125174e7dc98SMatthew Ahrens #define	DO(which) \
12523f9d6ad7SLin Ling 	zdb_nicenum(dd->dd_used_breakdown[DD_USED_ ## which], nice); \
125374e7dc98SMatthew Ahrens 	(void) printf("\t\tused_breakdown[" #which "] = %s\n", nice)
125474e7dc98SMatthew Ahrens 	DO(HEAD);
125574e7dc98SMatthew Ahrens 	DO(SNAP);
125674e7dc98SMatthew Ahrens 	DO(CHILD);
125774e7dc98SMatthew Ahrens 	DO(CHILD_RSRV);
125874e7dc98SMatthew Ahrens 	DO(REFRSRV);
125974e7dc98SMatthew Ahrens #undef DO
1260fa9e4066Sahrens }
1261fa9e4066Sahrens 
1262fa9e4066Sahrens /*ARGSUSED*/
1263fa9e4066Sahrens static void
1264fa9e4066Sahrens dump_dsl_dataset(objset_t *os, uint64_t object, void *data, size_t size)
1265fa9e4066Sahrens {
1266fa9e4066Sahrens 	dsl_dataset_phys_t *ds = data;
1267fa9e4066Sahrens 	time_t crtime;
12683f9d6ad7SLin Ling 	char used[32], compressed[32], uncompressed[32], unique[32];
1269fbabab8fSmaybee 	char blkbuf[BP_SPRINTF_LEN];
1270fa9e4066Sahrens 
1271fa9e4066Sahrens 	if (ds == NULL)
1272fa9e4066Sahrens 		return;
1273fa9e4066Sahrens 
1274fa9e4066Sahrens 	ASSERT(size == sizeof (*ds));
1275fa9e4066Sahrens 	crtime = ds->ds_creation_time;
1276ad135b5dSChristopher Siden 	zdb_nicenum(ds->ds_referenced_bytes, used);
12773f9d6ad7SLin Ling 	zdb_nicenum(ds->ds_compressed_bytes, compressed);
12783f9d6ad7SLin Ling 	zdb_nicenum(ds->ds_uncompressed_bytes, uncompressed);
12793f9d6ad7SLin Ling 	zdb_nicenum(ds->ds_unique_bytes, unique);
128043466aaeSMax Grossman 	snprintf_blkptr(blkbuf, sizeof (blkbuf), &ds->ds_bp);
1281fa9e4066Sahrens 
1282088f3894Sahrens 	(void) printf("\t\tdir_obj = %llu\n",
1283fa9e4066Sahrens 	    (u_longlong_t)ds->ds_dir_obj);
1284fa9e4066Sahrens 	(void) printf("\t\tprev_snap_obj = %llu\n",
1285fa9e4066Sahrens 	    (u_longlong_t)ds->ds_prev_snap_obj);
1286fa9e4066Sahrens 	(void) printf("\t\tprev_snap_txg = %llu\n",
1287fa9e4066Sahrens 	    (u_longlong_t)ds->ds_prev_snap_txg);
1288fa9e4066Sahrens 	(void) printf("\t\tnext_snap_obj = %llu\n",
1289fa9e4066Sahrens 	    (u_longlong_t)ds->ds_next_snap_obj);
1290fa9e4066Sahrens 	(void) printf("\t\tsnapnames_zapobj = %llu\n",
1291fa9e4066Sahrens 	    (u_longlong_t)ds->ds_snapnames_zapobj);
1292fa9e4066Sahrens 	(void) printf("\t\tnum_children = %llu\n",
1293fa9e4066Sahrens 	    (u_longlong_t)ds->ds_num_children);
1294842727c2SChris Kirby 	(void) printf("\t\tuserrefs_obj = %llu\n",
1295842727c2SChris Kirby 	    (u_longlong_t)ds->ds_userrefs_obj);
1296fa9e4066Sahrens 	(void) printf("\t\tcreation_time = %s", ctime(&crtime));
1297fa9e4066Sahrens 	(void) printf("\t\tcreation_txg = %llu\n",
1298fa9e4066Sahrens 	    (u_longlong_t)ds->ds_creation_txg);
1299fa9e4066Sahrens 	(void) printf("\t\tdeadlist_obj = %llu\n",
1300fa9e4066Sahrens 	    (u_longlong_t)ds->ds_deadlist_obj);
1301fa9e4066Sahrens 	(void) printf("\t\tused_bytes = %s\n", used);
1302fa9e4066Sahrens 	(void) printf("\t\tcompressed_bytes = %s\n", compressed);
1303fa9e4066Sahrens 	(void) printf("\t\tuncompressed_bytes = %s\n", uncompressed);
1304fa9e4066Sahrens 	(void) printf("\t\tunique = %s\n", unique);
1305fa9e4066Sahrens 	(void) printf("\t\tfsid_guid = %llu\n",
1306fa9e4066Sahrens 	    (u_longlong_t)ds->ds_fsid_guid);
1307fa9e4066Sahrens 	(void) printf("\t\tguid = %llu\n",
1308fa9e4066Sahrens 	    (u_longlong_t)ds->ds_guid);
130999653d4eSeschrock 	(void) printf("\t\tflags = %llx\n",
131099653d4eSeschrock 	    (u_longlong_t)ds->ds_flags);
1311088f3894Sahrens 	(void) printf("\t\tnext_clones_obj = %llu\n",
1312088f3894Sahrens 	    (u_longlong_t)ds->ds_next_clones_obj);
1313bb0ade09Sahrens 	(void) printf("\t\tprops_obj = %llu\n",
1314bb0ade09Sahrens 	    (u_longlong_t)ds->ds_props_obj);
1315fa9e4066Sahrens 	(void) printf("\t\tbp = %s\n", blkbuf);
1316fa9e4066Sahrens }
1317fa9e4066Sahrens 
1318cde58dbcSMatthew Ahrens /* ARGSUSED */
1319cde58dbcSMatthew Ahrens static int
1320ad135b5dSChristopher Siden dump_bptree_cb(void *arg, const blkptr_t *bp, dmu_tx_t *tx)
1321ad135b5dSChristopher Siden {
1322ad135b5dSChristopher Siden 	char blkbuf[BP_SPRINTF_LEN];
1323ad135b5dSChristopher Siden 
1324ad135b5dSChristopher Siden 	if (bp->blk_birth != 0) {
132543466aaeSMax Grossman 		snprintf_blkptr(blkbuf, sizeof (blkbuf), bp);
1326ad135b5dSChristopher Siden 		(void) printf("\t%s\n", blkbuf);
1327ad135b5dSChristopher Siden 	}
1328ad135b5dSChristopher Siden 	return (0);
1329ad135b5dSChristopher Siden }
1330ad135b5dSChristopher Siden 
1331ad135b5dSChristopher Siden static void
1332ad135b5dSChristopher Siden dump_bptree(objset_t *os, uint64_t obj, char *name)
1333ad135b5dSChristopher Siden {
1334ad135b5dSChristopher Siden 	char bytes[32];
1335ad135b5dSChristopher Siden 	bptree_phys_t *bt;
1336ad135b5dSChristopher Siden 	dmu_buf_t *db;
1337ad135b5dSChristopher Siden 
1338ad135b5dSChristopher Siden 	if (dump_opt['d'] < 3)
1339ad135b5dSChristopher Siden 		return;
1340ad135b5dSChristopher Siden 
1341b420f3adSRichard Lowe 	VERIFY3U(0, ==, dmu_bonus_hold(os, obj, FTAG, &db));
1342ad135b5dSChristopher Siden 	bt = db->db_data;
1343ad135b5dSChristopher Siden 	zdb_nicenum(bt->bt_bytes, bytes);
1344ad135b5dSChristopher Siden 	(void) printf("\n    %s: %llu datasets, %s\n",
1345ad135b5dSChristopher Siden 	    name, (unsigned long long)(bt->bt_end - bt->bt_begin), bytes);
1346ad135b5dSChristopher Siden 	dmu_buf_rele(db, FTAG);
1347ad135b5dSChristopher Siden 
1348ad135b5dSChristopher Siden 	if (dump_opt['d'] < 5)
1349ad135b5dSChristopher Siden 		return;
1350ad135b5dSChristopher Siden 
1351ad135b5dSChristopher Siden 	(void) printf("\n");
1352ad135b5dSChristopher Siden 
1353ad135b5dSChristopher Siden 	(void) bptree_iterate(os, obj, B_FALSE, dump_bptree_cb, NULL, NULL);
1354ad135b5dSChristopher Siden }
1355ad135b5dSChristopher Siden 
1356ad135b5dSChristopher Siden /* ARGSUSED */
1357ad135b5dSChristopher Siden static int
1358cde58dbcSMatthew Ahrens dump_bpobj_cb(void *arg, const blkptr_t *bp, dmu_tx_t *tx)
1359fa9e4066Sahrens {
1360cde58dbcSMatthew Ahrens 	char blkbuf[BP_SPRINTF_LEN];
1361cde58dbcSMatthew Ahrens 
1362cde58dbcSMatthew Ahrens 	ASSERT(bp->blk_birth != 0);
136343466aaeSMax Grossman 	snprintf_blkptr_compact(blkbuf, sizeof (blkbuf), bp);
1364cde58dbcSMatthew Ahrens 	(void) printf("\t%s\n", blkbuf);
1365cde58dbcSMatthew Ahrens 	return (0);
1366cde58dbcSMatthew Ahrens }
1367cde58dbcSMatthew Ahrens 
1368cde58dbcSMatthew Ahrens static void
1369d0475637SMatthew Ahrens dump_bpobj(bpobj_t *bpo, char *name, int indent)
1370cde58dbcSMatthew Ahrens {
13713f9d6ad7SLin Ling 	char bytes[32];
13723f9d6ad7SLin Ling 	char comp[32];
13733f9d6ad7SLin Ling 	char uncomp[32];
1374fa9e4066Sahrens 
1375fa9e4066Sahrens 	if (dump_opt['d'] < 3)
1376fa9e4066Sahrens 		return;
1377fa9e4066Sahrens 
1378cde58dbcSMatthew Ahrens 	zdb_nicenum(bpo->bpo_phys->bpo_bytes, bytes);
1379d0475637SMatthew Ahrens 	if (bpo->bpo_havesubobj && bpo->bpo_phys->bpo_subobjs != 0) {
1380cde58dbcSMatthew Ahrens 		zdb_nicenum(bpo->bpo_phys->bpo_comp, comp);
1381cde58dbcSMatthew Ahrens 		zdb_nicenum(bpo->bpo_phys->bpo_uncomp, uncomp);
1382d0475637SMatthew Ahrens 		(void) printf("    %*s: object %llu, %llu local blkptrs, "
1383d0475637SMatthew Ahrens 		    "%llu subobjs, %s (%s/%s comp)\n",
1384d0475637SMatthew Ahrens 		    indent * 8, name,
1385d0475637SMatthew Ahrens 		    (u_longlong_t)bpo->bpo_object,
1386d0475637SMatthew Ahrens 		    (u_longlong_t)bpo->bpo_phys->bpo_num_blkptrs,
1387cde58dbcSMatthew Ahrens 		    (u_longlong_t)bpo->bpo_phys->bpo_num_subobjs,
138899653d4eSeschrock 		    bytes, comp, uncomp);
1389d0475637SMatthew Ahrens 
1390d0475637SMatthew Ahrens 		for (uint64_t i = 0; i < bpo->bpo_phys->bpo_num_subobjs; i++) {
1391d0475637SMatthew Ahrens 			uint64_t subobj;
1392d0475637SMatthew Ahrens 			bpobj_t subbpo;
1393d0475637SMatthew Ahrens 			int error;
1394d0475637SMatthew Ahrens 			VERIFY0(dmu_read(bpo->bpo_os,
1395d0475637SMatthew Ahrens 			    bpo->bpo_phys->bpo_subobjs,
1396d0475637SMatthew Ahrens 			    i * sizeof (subobj), sizeof (subobj), &subobj, 0));
1397d0475637SMatthew Ahrens 			error = bpobj_open(&subbpo, bpo->bpo_os, subobj);
1398d0475637SMatthew Ahrens 			if (error != 0) {
1399d0475637SMatthew Ahrens 				(void) printf("ERROR %u while trying to open "
1400d0475637SMatthew Ahrens 				    "subobj id %llu\n",
1401d0475637SMatthew Ahrens 				    error, (u_longlong_t)subobj);
1402d0475637SMatthew Ahrens 				continue;
1403d0475637SMatthew Ahrens 			}
1404d0475637SMatthew Ahrens 			dump_bpobj(&subbpo, "subobj", indent + 1);
140577061867SMatthew Ahrens 			bpobj_close(&subbpo);
1406d0475637SMatthew Ahrens 		}
140799653d4eSeschrock 	} else {
1408d0475637SMatthew Ahrens 		(void) printf("    %*s: object %llu, %llu blkptrs, %s\n",
1409d0475637SMatthew Ahrens 		    indent * 8, name,
1410d0475637SMatthew Ahrens 		    (u_longlong_t)bpo->bpo_object,
1411d0475637SMatthew Ahrens 		    (u_longlong_t)bpo->bpo_phys->bpo_num_blkptrs,
1412d0475637SMatthew Ahrens 		    bytes);
141399653d4eSeschrock 	}
1414fa9e4066Sahrens 
1415cde58dbcSMatthew Ahrens 	if (dump_opt['d'] < 5)
1416fa9e4066Sahrens 		return;
1417fa9e4066Sahrens 
1418fa9e4066Sahrens 
1419d0475637SMatthew Ahrens 	if (indent == 0) {
1420cde58dbcSMatthew Ahrens 		(void) bpobj_iterate_nofree(bpo, dump_bpobj_cb, NULL, NULL);
1421d0475637SMatthew Ahrens 		(void) printf("\n");
1422d0475637SMatthew Ahrens 	}
1423fa9e4066Sahrens }
1424fa9e4066Sahrens 
1425cde58dbcSMatthew Ahrens static void
1426cde58dbcSMatthew Ahrens dump_deadlist(dsl_deadlist_t *dl)
1427cde58dbcSMatthew Ahrens {
1428cde58dbcSMatthew Ahrens 	dsl_deadlist_entry_t *dle;
1429d0475637SMatthew Ahrens 	uint64_t unused;
1430cde58dbcSMatthew Ahrens 	char bytes[32];
1431cde58dbcSMatthew Ahrens 	char comp[32];
1432cde58dbcSMatthew Ahrens 	char uncomp[32];
1433cde58dbcSMatthew Ahrens 
1434cde58dbcSMatthew Ahrens 	if (dump_opt['d'] < 3)
1435cde58dbcSMatthew Ahrens 		return;
1436cde58dbcSMatthew Ahrens 
1437*90c76c66SMatthew Ahrens 	if (dl->dl_oldfmt) {
1438*90c76c66SMatthew Ahrens 		dump_bpobj(&dl->dl_bpobj, "old-format deadlist", 0);
1439*90c76c66SMatthew Ahrens 		return;
1440*90c76c66SMatthew Ahrens 	}
1441*90c76c66SMatthew Ahrens 
1442cde58dbcSMatthew Ahrens 	zdb_nicenum(dl->dl_phys->dl_used, bytes);
1443cde58dbcSMatthew Ahrens 	zdb_nicenum(dl->dl_phys->dl_comp, comp);
1444cde58dbcSMatthew Ahrens 	zdb_nicenum(dl->dl_phys->dl_uncomp, uncomp);
1445cde58dbcSMatthew Ahrens 	(void) printf("\n    Deadlist: %s (%s/%s comp)\n",
1446cde58dbcSMatthew Ahrens 	    bytes, comp, uncomp);
1447cde58dbcSMatthew Ahrens 
1448cde58dbcSMatthew Ahrens 	if (dump_opt['d'] < 4)
1449cde58dbcSMatthew Ahrens 		return;
1450cde58dbcSMatthew Ahrens 
1451cde58dbcSMatthew Ahrens 	(void) printf("\n");
1452cde58dbcSMatthew Ahrens 
1453d0475637SMatthew Ahrens 	/* force the tree to be loaded */
1454d0475637SMatthew Ahrens 	dsl_deadlist_space_range(dl, 0, UINT64_MAX, &unused, &unused, &unused);
1455d0475637SMatthew Ahrens 
1456cde58dbcSMatthew Ahrens 	for (dle = avl_first(&dl->dl_tree); dle;
1457cde58dbcSMatthew Ahrens 	    dle = AVL_NEXT(&dl->dl_tree, dle)) {
1458d0475637SMatthew Ahrens 		if (dump_opt['d'] >= 5) {
1459d0475637SMatthew Ahrens 			char buf[128];
1460d0475637SMatthew Ahrens 			(void) snprintf(buf, sizeof (buf), "mintxg %llu -> ",
1461d0475637SMatthew Ahrens 			    (longlong_t)dle->dle_mintxg,
1462d0475637SMatthew Ahrens 			    (longlong_t)dle->dle_bpobj.bpo_object);
1463d0475637SMatthew Ahrens 
1464d0475637SMatthew Ahrens 			dump_bpobj(&dle->dle_bpobj, buf, 0);
1465d0475637SMatthew Ahrens 		} else {
1466cde58dbcSMatthew Ahrens 			(void) printf("mintxg %llu -> obj %llu\n",
1467cde58dbcSMatthew Ahrens 			    (longlong_t)dle->dle_mintxg,
1468cde58dbcSMatthew Ahrens 			    (longlong_t)dle->dle_bpobj.bpo_object);
1469cde58dbcSMatthew Ahrens 
1470d0475637SMatthew Ahrens 		}
1471cde58dbcSMatthew Ahrens 	}
1472fa9e4066Sahrens }
1473fa9e4066Sahrens 
1474e0d35c44Smarks static avl_tree_t idx_tree;
1475e0d35c44Smarks static avl_tree_t domain_tree;
1476e0d35c44Smarks static boolean_t fuid_table_loaded;
14770a586ceaSMark Shellenbaum static boolean_t sa_loaded;
14780a586ceaSMark Shellenbaum sa_attr_type_t *sa_attr_table;
1479e0d35c44Smarks 
1480e0d35c44Smarks static void
1481e0d35c44Smarks fuid_table_destroy()
1482e0d35c44Smarks {
1483e0d35c44Smarks 	if (fuid_table_loaded) {
1484e0d35c44Smarks 		zfs_fuid_table_destroy(&idx_tree, &domain_tree);
1485e0d35c44Smarks 		fuid_table_loaded = B_FALSE;
1486e0d35c44Smarks 	}
1487e0d35c44Smarks }
1488e0d35c44Smarks 
1489e0d35c44Smarks /*
1490e0d35c44Smarks  * print uid or gid information.
1491e0d35c44Smarks  * For normal POSIX id just the id is printed in decimal format.
1492e0d35c44Smarks  * For CIFS files with FUID the fuid is printed in hex followed by
1493d0475637SMatthew Ahrens  * the domain-rid string.
1494e0d35c44Smarks  */
1495e0d35c44Smarks static void
1496e0d35c44Smarks print_idstr(uint64_t id, const char *id_type)
1497e0d35c44Smarks {
1498e0d35c44Smarks 	if (FUID_INDEX(id)) {
1499e0d35c44Smarks 		char *domain;
1500e0d35c44Smarks 
1501e0d35c44Smarks 		domain = zfs_fuid_idx_domain(&idx_tree, FUID_INDEX(id));
1502e0d35c44Smarks 		(void) printf("\t%s     %llx [%s-%d]\n", id_type,
1503e0d35c44Smarks 		    (u_longlong_t)id, domain, (int)FUID_RID(id));
1504e0d35c44Smarks 	} else {
1505e0d35c44Smarks 		(void) printf("\t%s     %llu\n", id_type, (u_longlong_t)id);
1506e0d35c44Smarks 	}
1507e0d35c44Smarks 
1508e0d35c44Smarks }
1509e0d35c44Smarks 
1510e0d35c44Smarks static void
15110a586ceaSMark Shellenbaum dump_uidgid(objset_t *os, uint64_t uid, uint64_t gid)
1512e0d35c44Smarks {
1513e0d35c44Smarks 	uint32_t uid_idx, gid_idx;
1514e0d35c44Smarks 
15150a586ceaSMark Shellenbaum 	uid_idx = FUID_INDEX(uid);
15160a586ceaSMark Shellenbaum 	gid_idx = FUID_INDEX(gid);
1517e0d35c44Smarks 
1518e0d35c44Smarks 	/* Load domain table, if not already loaded */
1519e0d35c44Smarks 	if (!fuid_table_loaded && (uid_idx || gid_idx)) {
1520e0d35c44Smarks 		uint64_t fuid_obj;
1521e0d35c44Smarks 
1522e0d35c44Smarks 		/* first find the fuid object.  It lives in the master node */
1523e0d35c44Smarks 		VERIFY(zap_lookup(os, MASTER_NODE_OBJ, ZFS_FUID_TABLES,
1524e0d35c44Smarks 		    8, 1, &fuid_obj) == 0);
152589459e17SMark Shellenbaum 		zfs_fuid_avl_tree_create(&idx_tree, &domain_tree);
1526e0d35c44Smarks 		(void) zfs_fuid_table_load(os, fuid_obj,
1527e0d35c44Smarks 		    &idx_tree, &domain_tree);
1528e0d35c44Smarks 		fuid_table_loaded = B_TRUE;
1529e0d35c44Smarks 	}
1530e0d35c44Smarks 
15310a586ceaSMark Shellenbaum 	print_idstr(uid, "uid");
15320a586ceaSMark Shellenbaum 	print_idstr(gid, "gid");
1533e0d35c44Smarks }
1534e0d35c44Smarks 
1535fa9e4066Sahrens /*ARGSUSED*/
1536fa9e4066Sahrens static void
1537fa9e4066Sahrens dump_znode(objset_t *os, uint64_t object, void *data, size_t size)
1538fa9e4066Sahrens {
1539fa9e4066Sahrens 	char path[MAXPATHLEN * 2];	/* allow for xattr and failure prefix */
15400a586ceaSMark Shellenbaum 	sa_handle_t *hdl;
15410a586ceaSMark Shellenbaum 	uint64_t xattr, rdev, gen;
15420a586ceaSMark Shellenbaum 	uint64_t uid, gid, mode, fsize, parent, links;
15438f2529deSMark Shellenbaum 	uint64_t pflags;
15440a586ceaSMark Shellenbaum 	uint64_t acctm[2], modtm[2], chgtm[2], crtm[2];
15450a586ceaSMark Shellenbaum 	time_t z_crtime, z_atime, z_mtime, z_ctime;
15468f2529deSMark Shellenbaum 	sa_bulk_attr_t bulk[12];
15470a586ceaSMark Shellenbaum 	int idx = 0;
154855434c77Sek110237 	int error;
1549fa9e4066Sahrens 
15500a586ceaSMark Shellenbaum 	if (!sa_loaded) {
15510a586ceaSMark Shellenbaum 		uint64_t sa_attrs = 0;
15520a586ceaSMark Shellenbaum 		uint64_t version;
15530a586ceaSMark Shellenbaum 
15540a586ceaSMark Shellenbaum 		VERIFY(zap_lookup(os, MASTER_NODE_OBJ, ZPL_VERSION_STR,
15550a586ceaSMark Shellenbaum 		    8, 1, &version) == 0);
15560a586ceaSMark Shellenbaum 		if (version >= ZPL_VERSION_SA) {
15570a586ceaSMark Shellenbaum 			VERIFY(zap_lookup(os, MASTER_NODE_OBJ, ZFS_SA_ATTRS,
15580a586ceaSMark Shellenbaum 			    8, 1, &sa_attrs) == 0);
15590a586ceaSMark Shellenbaum 		}
15601d8ccc7bSMark Shellenbaum 		if ((error = sa_setup(os, sa_attrs, zfs_attr_table,
15611d8ccc7bSMark Shellenbaum 		    ZPL_END, &sa_attr_table)) != 0) {
15621d8ccc7bSMark Shellenbaum 			(void) printf("sa_setup failed errno %d, can't "
15631d8ccc7bSMark Shellenbaum 			    "display znode contents\n", error);
15641d8ccc7bSMark Shellenbaum 			return;
15651d8ccc7bSMark Shellenbaum 		}
15660a586ceaSMark Shellenbaum 		sa_loaded = B_TRUE;
15670a586ceaSMark Shellenbaum 	}
15680a586ceaSMark Shellenbaum 
15690a586ceaSMark Shellenbaum 	if (sa_handle_get(os, object, NULL, SA_HDL_PRIVATE, &hdl)) {
15700a586ceaSMark Shellenbaum 		(void) printf("Failed to get handle for SA znode\n");
15710a586ceaSMark Shellenbaum 		return;
15720a586ceaSMark Shellenbaum 	}
15730a586ceaSMark Shellenbaum 
15740a586ceaSMark Shellenbaum 	SA_ADD_BULK_ATTR(bulk, idx, sa_attr_table[ZPL_UID], NULL, &uid, 8);
15750a586ceaSMark Shellenbaum 	SA_ADD_BULK_ATTR(bulk, idx, sa_attr_table[ZPL_GID], NULL, &gid, 8);
15760a586ceaSMark Shellenbaum 	SA_ADD_BULK_ATTR(bulk, idx, sa_attr_table[ZPL_LINKS], NULL,
15770a586ceaSMark Shellenbaum 	    &links, 8);
15780a586ceaSMark Shellenbaum 	SA_ADD_BULK_ATTR(bulk, idx, sa_attr_table[ZPL_GEN], NULL, &gen, 8);
15790a586ceaSMark Shellenbaum 	SA_ADD_BULK_ATTR(bulk, idx, sa_attr_table[ZPL_MODE], NULL,
15800a586ceaSMark Shellenbaum 	    &mode, 8);
15810a586ceaSMark Shellenbaum 	SA_ADD_BULK_ATTR(bulk, idx, sa_attr_table[ZPL_PARENT],
15820a586ceaSMark Shellenbaum 	    NULL, &parent, 8);
15830a586ceaSMark Shellenbaum 	SA_ADD_BULK_ATTR(bulk, idx, sa_attr_table[ZPL_SIZE], NULL,
15840a586ceaSMark Shellenbaum 	    &fsize, 8);
15850a586ceaSMark Shellenbaum 	SA_ADD_BULK_ATTR(bulk, idx, sa_attr_table[ZPL_ATIME], NULL,
15860a586ceaSMark Shellenbaum 	    acctm, 16);
15870a586ceaSMark Shellenbaum 	SA_ADD_BULK_ATTR(bulk, idx, sa_attr_table[ZPL_MTIME], NULL,
15880a586ceaSMark Shellenbaum 	    modtm, 16);
15890a586ceaSMark Shellenbaum 	SA_ADD_BULK_ATTR(bulk, idx, sa_attr_table[ZPL_CRTIME], NULL,
15900a586ceaSMark Shellenbaum 	    crtm, 16);
15910a586ceaSMark Shellenbaum 	SA_ADD_BULK_ATTR(bulk, idx, sa_attr_table[ZPL_CTIME], NULL,
15920a586ceaSMark Shellenbaum 	    chgtm, 16);
15938f2529deSMark Shellenbaum 	SA_ADD_BULK_ATTR(bulk, idx, sa_attr_table[ZPL_FLAGS], NULL,
15948f2529deSMark Shellenbaum 	    &pflags, 8);
15950a586ceaSMark Shellenbaum 
15960a586ceaSMark Shellenbaum 	if (sa_bulk_lookup(hdl, bulk, idx)) {
15970a586ceaSMark Shellenbaum 		(void) sa_handle_destroy(hdl);
15980a586ceaSMark Shellenbaum 		return;
15990a586ceaSMark Shellenbaum 	}
1600fa9e4066Sahrens 
160155434c77Sek110237 	error = zfs_obj_to_path(os, object, path, sizeof (path));
160255434c77Sek110237 	if (error != 0) {
160355434c77Sek110237 		(void) snprintf(path, sizeof (path), "\?\?\?<object#%llu>",
160455434c77Sek110237 		    (u_longlong_t)object);
160555434c77Sek110237 	}
1606fa9e4066Sahrens 	if (dump_opt['d'] < 3) {
160755434c77Sek110237 		(void) printf("\t%s\n", path);
16080a586ceaSMark Shellenbaum 		(void) sa_handle_destroy(hdl);
1609fa9e4066Sahrens 		return;
1610fa9e4066Sahrens 	}
1611fa9e4066Sahrens 
16120a586ceaSMark Shellenbaum 	z_crtime = (time_t)crtm[0];
16130a586ceaSMark Shellenbaum 	z_atime = (time_t)acctm[0];
16140a586ceaSMark Shellenbaum 	z_mtime = (time_t)modtm[0];
16150a586ceaSMark Shellenbaum 	z_ctime = (time_t)chgtm[0];
1616fa9e4066Sahrens 
161755434c77Sek110237 	(void) printf("\tpath	%s\n", path);
16180a586ceaSMark Shellenbaum 	dump_uidgid(os, uid, gid);
1619fa9e4066Sahrens 	(void) printf("\tatime	%s", ctime(&z_atime));
1620fa9e4066Sahrens 	(void) printf("\tmtime	%s", ctime(&z_mtime));
1621fa9e4066Sahrens 	(void) printf("\tctime	%s", ctime(&z_ctime));
1622fa9e4066Sahrens 	(void) printf("\tcrtime	%s", ctime(&z_crtime));
16230a586ceaSMark Shellenbaum 	(void) printf("\tgen	%llu\n", (u_longlong_t)gen);
16240a586ceaSMark Shellenbaum 	(void) printf("\tmode	%llo\n", (u_longlong_t)mode);
16250a586ceaSMark Shellenbaum 	(void) printf("\tsize	%llu\n", (u_longlong_t)fsize);
16260a586ceaSMark Shellenbaum 	(void) printf("\tparent	%llu\n", (u_longlong_t)parent);
16270a586ceaSMark Shellenbaum 	(void) printf("\tlinks	%llu\n", (u_longlong_t)links);
16288f2529deSMark Shellenbaum 	(void) printf("\tpflags	%llx\n", (u_longlong_t)pflags);
16290a586ceaSMark Shellenbaum 	if (sa_lookup(hdl, sa_attr_table[ZPL_XATTR], &xattr,
16300a586ceaSMark Shellenbaum 	    sizeof (uint64_t)) == 0)
16310a586ceaSMark Shellenbaum 		(void) printf("\txattr	%llu\n", (u_longlong_t)xattr);
16320a586ceaSMark Shellenbaum 	if (sa_lookup(hdl, sa_attr_table[ZPL_RDEV], &rdev,
16330a586ceaSMark Shellenbaum 	    sizeof (uint64_t)) == 0)
16340a586ceaSMark Shellenbaum 		(void) printf("\trdev	0x%016llx\n", (u_longlong_t)rdev);
16350a586ceaSMark Shellenbaum 	sa_handle_destroy(hdl);
1636fa9e4066Sahrens }
1637fa9e4066Sahrens 
1638fa9e4066Sahrens /*ARGSUSED*/
1639fa9e4066Sahrens static void
1640fa9e4066Sahrens dump_acl(objset_t *os, uint64_t object, void *data, size_t size)
1641fa9e4066Sahrens {
1642fa9e4066Sahrens }
1643fa9e4066Sahrens 
1644fa9e4066Sahrens /*ARGSUSED*/
1645fa9e4066Sahrens static void
1646fa9e4066Sahrens dump_dmu_objset(objset_t *os, uint64_t object, void *data, size_t size)
1647fa9e4066Sahrens {
1648fa9e4066Sahrens }
1649fa9e4066Sahrens 
16506de8f417SVictor Latushkin static object_viewer_t *object_viewer[DMU_OT_NUMTYPES + 1] = {
1651fa9e4066Sahrens 	dump_none,		/* unallocated			*/
1652fa9e4066Sahrens 	dump_zap,		/* object directory		*/
1653fa9e4066Sahrens 	dump_uint64,		/* object array			*/
1654fa9e4066Sahrens 	dump_none,		/* packed nvlist		*/
1655fa9e4066Sahrens 	dump_packed_nvlist,	/* packed nvlist size		*/
1656fa9e4066Sahrens 	dump_none,		/* bplist			*/
1657fa9e4066Sahrens 	dump_none,		/* bplist header		*/
1658fa9e4066Sahrens 	dump_none,		/* SPA space map header		*/
1659fa9e4066Sahrens 	dump_none,		/* SPA space map		*/
1660fa9e4066Sahrens 	dump_none,		/* ZIL intent log		*/
1661fa9e4066Sahrens 	dump_dnode,		/* DMU dnode			*/
1662fa9e4066Sahrens 	dump_dmu_objset,	/* DMU objset			*/
1663fa9e4066Sahrens 	dump_dsl_dir,		/* DSL directory		*/
1664fa9e4066Sahrens 	dump_zap,		/* DSL directory child map	*/
1665fa9e4066Sahrens 	dump_zap,		/* DSL dataset snap map		*/
1666fa9e4066Sahrens 	dump_zap,		/* DSL props			*/
1667fa9e4066Sahrens 	dump_dsl_dataset,	/* DSL dataset			*/
1668fa9e4066Sahrens 	dump_znode,		/* ZFS znode			*/
1669da6c28aaSamw 	dump_acl,		/* ZFS V0 ACL			*/
1670fa9e4066Sahrens 	dump_uint8,		/* ZFS plain file		*/
1671e7437265Sahrens 	dump_zpldir,		/* ZFS directory		*/
1672fa9e4066Sahrens 	dump_zap,		/* ZFS master node		*/
1673fa9e4066Sahrens 	dump_zap,		/* ZFS delete queue		*/
1674fa9e4066Sahrens 	dump_uint8,		/* zvol object			*/
1675fa9e4066Sahrens 	dump_zap,		/* zvol prop			*/
1676fa9e4066Sahrens 	dump_uint8,		/* other uint8[]		*/
1677fa9e4066Sahrens 	dump_uint64,		/* other uint64[]		*/
1678fa9e4066Sahrens 	dump_zap,		/* other ZAP			*/
1679ea8dc4b6Seschrock 	dump_zap,		/* persistent error log		*/
168006eeb2adSek110237 	dump_uint8,		/* SPA history			*/
16814445fffbSMatthew Ahrens 	dump_history_offsets,	/* SPA history offsets		*/
1682b1b8ab34Slling 	dump_zap,		/* Pool properties		*/
1683ecd6cf80Smarks 	dump_zap,		/* DSL permissions		*/
1684da6c28aaSamw 	dump_acl,		/* ZFS ACL			*/
1685da6c28aaSamw 	dump_uint8,		/* ZFS SYSACL			*/
1686da6c28aaSamw 	dump_none,		/* FUID nvlist			*/
1687da6c28aaSamw 	dump_packed_nvlist,	/* FUID nvlist size		*/
1688088f3894Sahrens 	dump_zap,		/* DSL dataset next clones	*/
1689088f3894Sahrens 	dump_zap,		/* DSL scrub queue		*/
169014843421SMatthew Ahrens 	dump_zap,		/* ZFS user/group used		*/
169114843421SMatthew Ahrens 	dump_zap,		/* ZFS user/group quota		*/
1692842727c2SChris Kirby 	dump_zap,		/* snapshot refcount tags	*/
1693486ae710SMatthew Ahrens 	dump_ddt_zap,		/* DDT ZAP object		*/
1694b24ab676SJeff Bonwick 	dump_zap,		/* DDT statistics		*/
16950a586ceaSMark Shellenbaum 	dump_znode,		/* SA object			*/
16960a586ceaSMark Shellenbaum 	dump_zap,		/* SA Master Node		*/
16970a586ceaSMark Shellenbaum 	dump_sa_attrs,		/* SA attribute registration	*/
16980a586ceaSMark Shellenbaum 	dump_sa_layouts,	/* SA attribute layouts		*/
16993f9d6ad7SLin Ling 	dump_zap,		/* DSL scrub translations	*/
17003f9d6ad7SLin Ling 	dump_none,		/* fake dedup BP		*/
1701cde58dbcSMatthew Ahrens 	dump_zap,		/* deadlist			*/
1702cde58dbcSMatthew Ahrens 	dump_none,		/* deadlist hdr			*/
1703cde58dbcSMatthew Ahrens 	dump_zap,		/* dsl clones			*/
1704cde58dbcSMatthew Ahrens 	dump_none,		/* bpobj subobjs		*/
17050a586ceaSMark Shellenbaum 	dump_unknown,		/* Unknown type, must be last	*/
1706fa9e4066Sahrens };
1707fa9e4066Sahrens 
1708fa9e4066Sahrens static void
1709fa9e4066Sahrens dump_object(objset_t *os, uint64_t object, int verbosity, int *print_header)
1710fa9e4066Sahrens {
1711fa9e4066Sahrens 	dmu_buf_t *db = NULL;
1712fa9e4066Sahrens 	dmu_object_info_t doi;
1713fa9e4066Sahrens 	dnode_t *dn;
1714fa9e4066Sahrens 	void *bonus = NULL;
1715fa9e4066Sahrens 	size_t bsize = 0;
17163f9d6ad7SLin Ling 	char iblk[32], dblk[32], lsize[32], asize[32], fill[32];
17173f9d6ad7SLin Ling 	char bonus_size[32];
1718fa9e4066Sahrens 	char aux[50];
1719fa9e4066Sahrens 	int error;
1720fa9e4066Sahrens 
1721fa9e4066Sahrens 	if (*print_header) {
1722b24ab676SJeff Bonwick 		(void) printf("\n%10s  %3s  %5s  %5s  %5s  %5s  %6s  %s\n",
1723b24ab676SJeff Bonwick 		    "Object", "lvl", "iblk", "dblk", "dsize", "lsize",
1724b24ab676SJeff Bonwick 		    "%full", "type");
1725fa9e4066Sahrens 		*print_header = 0;
1726fa9e4066Sahrens 	}
1727fa9e4066Sahrens 
1728fa9e4066Sahrens 	if (object == 0) {
1729744947dcSTom Erickson 		dn = DMU_META_DNODE(os);
1730fa9e4066Sahrens 	} else {
1731ea8dc4b6Seschrock 		error = dmu_bonus_hold(os, object, FTAG, &db);
1732ea8dc4b6Seschrock 		if (error)
1733ea8dc4b6Seschrock 			fatal("dmu_bonus_hold(%llu) failed, errno %u",
1734ea8dc4b6Seschrock 			    object, error);
1735fa9e4066Sahrens 		bonus = db->db_data;
1736fa9e4066Sahrens 		bsize = db->db_size;
1737744947dcSTom Erickson 		dn = DB_DNODE((dmu_buf_impl_t *)db);
1738fa9e4066Sahrens 	}
1739fa9e4066Sahrens 	dmu_object_info_from_dnode(dn, &doi);
1740fa9e4066Sahrens 
17413f9d6ad7SLin Ling 	zdb_nicenum(doi.doi_metadata_block_size, iblk);
17423f9d6ad7SLin Ling 	zdb_nicenum(doi.doi_data_block_size, dblk);
17433f9d6ad7SLin Ling 	zdb_nicenum(doi.doi_max_offset, lsize);
17443f9d6ad7SLin Ling 	zdb_nicenum(doi.doi_physical_blocks_512 << 9, asize);
17453f9d6ad7SLin Ling 	zdb_nicenum(doi.doi_bonus_size, bonus_size);
1746b24ab676SJeff Bonwick 	(void) sprintf(fill, "%6.2f", 100.0 * doi.doi_fill_count *
1747bbfd46c4SJeff Bonwick 	    doi.doi_data_block_size / (object == 0 ? DNODES_PER_BLOCK : 1) /
1748bbfd46c4SJeff Bonwick 	    doi.doi_max_offset);
1749fa9e4066Sahrens 
1750fa9e4066Sahrens 	aux[0] = '\0';
1751fa9e4066Sahrens 
1752e7437265Sahrens 	if (doi.doi_checksum != ZIO_CHECKSUM_INHERIT || verbosity >= 6) {
1753fa9e4066Sahrens 		(void) snprintf(aux + strlen(aux), sizeof (aux), " (K=%s)",
17546de8f417SVictor Latushkin 		    ZDB_CHECKSUM_NAME(doi.doi_checksum));
1755e7437265Sahrens 	}
1756fa9e4066Sahrens 
1757e7437265Sahrens 	if (doi.doi_compress != ZIO_COMPRESS_INHERIT || verbosity >= 6) {
1758fa9e4066Sahrens 		(void) snprintf(aux + strlen(aux), sizeof (aux), " (Z=%s)",
17596de8f417SVictor Latushkin 		    ZDB_COMPRESS_NAME(doi.doi_compress));
1760e7437265Sahrens 	}
1761fa9e4066Sahrens 
1762b24ab676SJeff Bonwick 	(void) printf("%10lld  %3u  %5s  %5s  %5s  %5s  %6s  %s%s\n",
1763b24ab676SJeff Bonwick 	    (u_longlong_t)object, doi.doi_indirection, iblk, dblk,
1764b24ab676SJeff Bonwick 	    asize, lsize, fill, ZDB_OT_NAME(doi.doi_type), aux);
1765fa9e4066Sahrens 
1766fa9e4066Sahrens 	if (doi.doi_bonus_type != DMU_OT_NONE && verbosity > 3) {
1767b24ab676SJeff Bonwick 		(void) printf("%10s  %3s  %5s  %5s  %5s  %5s  %6s  %s\n",
1768b24ab676SJeff Bonwick 		    "", "", "", "", "", bonus_size, "bonus",
17696de8f417SVictor Latushkin 		    ZDB_OT_NAME(doi.doi_bonus_type));
1770fa9e4066Sahrens 	}
1771fa9e4066Sahrens 
1772fa9e4066Sahrens 	if (verbosity >= 4) {
17730a586ceaSMark Shellenbaum 		(void) printf("\tdnode flags: %s%s%s\n",
177414843421SMatthew Ahrens 		    (dn->dn_phys->dn_flags & DNODE_FLAG_USED_BYTES) ?
177514843421SMatthew Ahrens 		    "USED_BYTES " : "",
177614843421SMatthew Ahrens 		    (dn->dn_phys->dn_flags & DNODE_FLAG_USERUSED_ACCOUNTED) ?
17770a586ceaSMark Shellenbaum 		    "USERUSED_ACCOUNTED " : "",
17780a586ceaSMark Shellenbaum 		    (dn->dn_phys->dn_flags & DNODE_FLAG_SPILL_BLKPTR) ?
17790a586ceaSMark Shellenbaum 		    "SPILL_BLKPTR" : "");
178014843421SMatthew Ahrens 		(void) printf("\tdnode maxblkid: %llu\n",
178114843421SMatthew Ahrens 		    (longlong_t)dn->dn_phys->dn_maxblkid);
178214843421SMatthew Ahrens 
17836de8f417SVictor Latushkin 		object_viewer[ZDB_OT_TYPE(doi.doi_bonus_type)](os, object,
17846de8f417SVictor Latushkin 		    bonus, bsize);
17856de8f417SVictor Latushkin 		object_viewer[ZDB_OT_TYPE(doi.doi_type)](os, object, NULL, 0);
1786fa9e4066Sahrens 		*print_header = 1;
1787fa9e4066Sahrens 	}
1788fa9e4066Sahrens 
1789fa9e4066Sahrens 	if (verbosity >= 5)
179088b7b0f2SMatthew Ahrens 		dump_indirect(dn);
1791fa9e4066Sahrens 
1792fa9e4066Sahrens 	if (verbosity >= 5) {
1793fa9e4066Sahrens 		/*
1794fa9e4066Sahrens 		 * Report the list of segments that comprise the object.
1795fa9e4066Sahrens 		 */
1796fa9e4066Sahrens 		uint64_t start = 0;
1797fa9e4066Sahrens 		uint64_t end;
1798fa9e4066Sahrens 		uint64_t blkfill = 1;
1799fa9e4066Sahrens 		int minlvl = 1;
1800fa9e4066Sahrens 
1801fa9e4066Sahrens 		if (dn->dn_type == DMU_OT_DNODE) {
1802fa9e4066Sahrens 			minlvl = 0;
1803fa9e4066Sahrens 			blkfill = DNODES_PER_BLOCK;
1804fa9e4066Sahrens 		}
1805fa9e4066Sahrens 
1806fa9e4066Sahrens 		for (;;) {
18073f9d6ad7SLin Ling 			char segsize[32];
1808cdb0ab79Smaybee 			error = dnode_next_offset(dn,
1809cdb0ab79Smaybee 			    0, &start, minlvl, blkfill, 0);
1810fa9e4066Sahrens 			if (error)
1811fa9e4066Sahrens 				break;
1812fa9e4066Sahrens 			end = start;
1813cdb0ab79Smaybee 			error = dnode_next_offset(dn,
1814cdb0ab79Smaybee 			    DNODE_FIND_HOLE, &end, minlvl, blkfill, 0);
18153f9d6ad7SLin Ling 			zdb_nicenum(end - start, segsize);
1816fa9e4066Sahrens 			(void) printf("\t\tsegment [%016llx, %016llx)"
1817fa9e4066Sahrens 			    " size %5s\n", (u_longlong_t)start,
1818fa9e4066Sahrens 			    (u_longlong_t)end, segsize);
1819fa9e4066Sahrens 			if (error)
1820fa9e4066Sahrens 				break;
1821fa9e4066Sahrens 			start = end;
1822fa9e4066Sahrens 		}
1823fa9e4066Sahrens 	}
1824fa9e4066Sahrens 
1825fa9e4066Sahrens 	if (db != NULL)
1826ea8dc4b6Seschrock 		dmu_buf_rele(db, FTAG);
1827fa9e4066Sahrens }
1828fa9e4066Sahrens 
1829fa9e4066Sahrens static char *objset_types[DMU_OST_NUMTYPES] = {
1830fa9e4066Sahrens 	"NONE", "META", "ZPL", "ZVOL", "OTHER", "ANY" };
1831fa9e4066Sahrens 
1832fa9e4066Sahrens static void
1833fa9e4066Sahrens dump_dir(objset_t *os)
1834fa9e4066Sahrens {
1835fa9e4066Sahrens 	dmu_objset_stats_t dds;
1836fa9e4066Sahrens 	uint64_t object, object_count;
1837a2eea2e1Sahrens 	uint64_t refdbytes, usedobjs, scratch;
18383f9d6ad7SLin Ling 	char numbuf[32];
183914843421SMatthew Ahrens 	char blkbuf[BP_SPRINTF_LEN + 20];
1840fa9e4066Sahrens 	char osname[MAXNAMELEN];
1841fa9e4066Sahrens 	char *type = "UNKNOWN";
1842fa9e4066Sahrens 	int verbosity = dump_opt['d'];
1843fa9e4066Sahrens 	int print_header = 1;
1844fa9e4066Sahrens 	int i, error;
1845fa9e4066Sahrens 
18463b2aab18SMatthew Ahrens 	dsl_pool_config_enter(dmu_objset_pool(os), FTAG);
1847a2eea2e1Sahrens 	dmu_objset_fast_stat(os, &dds);
18483b2aab18SMatthew Ahrens 	dsl_pool_config_exit(dmu_objset_pool(os), FTAG);
1849fa9e4066Sahrens 
1850fa9e4066Sahrens 	if (dds.dds_type < DMU_OST_NUMTYPES)
1851fa9e4066Sahrens 		type = objset_types[dds.dds_type];
1852fa9e4066Sahrens 
1853fa9e4066Sahrens 	if (dds.dds_type == DMU_OST_META) {
1854fa9e4066Sahrens 		dds.dds_creation_txg = TXG_INITIAL;
18555d7b4d43SMatthew Ahrens 		usedobjs = BP_GET_FILL(os->os_rootbp);
1856503ad85cSMatthew Ahrens 		refdbytes = os->os_spa->spa_dsl_pool->
185774e7dc98SMatthew Ahrens 		    dp_mos_dir->dd_phys->dd_used_bytes;
1858a2eea2e1Sahrens 	} else {
1859a2eea2e1Sahrens 		dmu_objset_space(os, &refdbytes, &scratch, &usedobjs, &scratch);
1860fa9e4066Sahrens 	}
1861fa9e4066Sahrens 
18625d7b4d43SMatthew Ahrens 	ASSERT3U(usedobjs, ==, BP_GET_FILL(os->os_rootbp));
1863fa9e4066Sahrens 
18643f9d6ad7SLin Ling 	zdb_nicenum(refdbytes, numbuf);
1865fa9e4066Sahrens 
1866fa9e4066Sahrens 	if (verbosity >= 4) {
186743466aaeSMax Grossman 		(void) snprintf(blkbuf, sizeof (blkbuf), ", rootbp ");
186843466aaeSMax Grossman 		(void) snprintf_blkptr(blkbuf + strlen(blkbuf),
186943466aaeSMax Grossman 		    sizeof (blkbuf) - strlen(blkbuf), os->os_rootbp);
1870fa9e4066Sahrens 	} else {
1871fa9e4066Sahrens 		blkbuf[0] = '\0';
1872fa9e4066Sahrens 	}
1873fa9e4066Sahrens 
1874fa9e4066Sahrens 	dmu_objset_name(os, osname);
1875fa9e4066Sahrens 
1876a2eea2e1Sahrens 	(void) printf("Dataset %s [%s], ID %llu, cr_txg %llu, "
1877fa9e4066Sahrens 	    "%s, %llu objects%s\n",
1878fa9e4066Sahrens 	    osname, type, (u_longlong_t)dmu_objset_id(os),
1879fa9e4066Sahrens 	    (u_longlong_t)dds.dds_creation_txg,
1880a2eea2e1Sahrens 	    numbuf, (u_longlong_t)usedobjs, blkbuf);
1881fa9e4066Sahrens 
1882b24ab676SJeff Bonwick 	if (zopt_objects != 0) {
1883b24ab676SJeff Bonwick 		for (i = 0; i < zopt_objects; i++)
1884b24ab676SJeff Bonwick 			dump_object(os, zopt_object[i], verbosity,
1885b24ab676SJeff Bonwick 			    &print_header);
1886b24ab676SJeff Bonwick 		(void) printf("\n");
1887b24ab676SJeff Bonwick 		return;
1888b24ab676SJeff Bonwick 	}
1889b24ab676SJeff Bonwick 
1890b24ab676SJeff Bonwick 	if (dump_opt['i'] != 0 || verbosity >= 2)
1891fa9e4066Sahrens 		dump_intent_log(dmu_objset_zil(os));
1892fa9e4066Sahrens 
1893fa9e4066Sahrens 	if (dmu_objset_ds(os) != NULL)
1894cde58dbcSMatthew Ahrens 		dump_deadlist(&dmu_objset_ds(os)->ds_deadlist);
1895fa9e4066Sahrens 
1896fa9e4066Sahrens 	if (verbosity < 2)
1897fa9e4066Sahrens 		return;
1898fa9e4066Sahrens 
189943466aaeSMax Grossman 	if (BP_IS_HOLE(os->os_rootbp))
1900088f3894Sahrens 		return;
1901088f3894Sahrens 
1902fa9e4066Sahrens 	dump_object(os, 0, verbosity, &print_header);
190314843421SMatthew Ahrens 	object_count = 0;
1904744947dcSTom Erickson 	if (DMU_USERUSED_DNODE(os) != NULL &&
1905744947dcSTom Erickson 	    DMU_USERUSED_DNODE(os)->dn_type != 0) {
190614843421SMatthew Ahrens 		dump_object(os, DMU_USERUSED_OBJECT, verbosity, &print_header);
190714843421SMatthew Ahrens 		dump_object(os, DMU_GROUPUSED_OBJECT, verbosity, &print_header);
190814843421SMatthew Ahrens 	}
1909fa9e4066Sahrens 
1910fa9e4066Sahrens 	object = 0;
19116754306eSahrens 	while ((error = dmu_object_next(os, &object, B_FALSE, 0)) == 0) {
1912fa9e4066Sahrens 		dump_object(os, object, verbosity, &print_header);
1913fa9e4066Sahrens 		object_count++;
1914fa9e4066Sahrens 	}
1915fa9e4066Sahrens 
1916a2eea2e1Sahrens 	ASSERT3U(object_count, ==, usedobjs);
1917fa9e4066Sahrens 
1918fa9e4066Sahrens 	(void) printf("\n");
1919fa9e4066Sahrens 
1920ccba0801SRich Morris 	if (error != ESRCH) {
1921ccba0801SRich Morris 		(void) fprintf(stderr, "dmu_object_next() = %d\n", error);
1922ccba0801SRich Morris 		abort();
1923ccba0801SRich Morris 	}
1924fa9e4066Sahrens }
1925fa9e4066Sahrens 
1926fa9e4066Sahrens static void
192753b9a4a9SVictor Latushkin dump_uberblock(uberblock_t *ub, const char *header, const char *footer)
1928fa9e4066Sahrens {
1929fa9e4066Sahrens 	time_t timestamp = ub->ub_timestamp;
1930fa9e4066Sahrens 
193153b9a4a9SVictor Latushkin 	(void) printf(header ? header : "");
1932fa9e4066Sahrens 	(void) printf("\tmagic = %016llx\n", (u_longlong_t)ub->ub_magic);
1933fa9e4066Sahrens 	(void) printf("\tversion = %llu\n", (u_longlong_t)ub->ub_version);
1934fa9e4066Sahrens 	(void) printf("\ttxg = %llu\n", (u_longlong_t)ub->ub_txg);
1935fa9e4066Sahrens 	(void) printf("\tguid_sum = %llu\n", (u_longlong_t)ub->ub_guid_sum);
1936fa9e4066Sahrens 	(void) printf("\ttimestamp = %llu UTC = %s",
1937fa9e4066Sahrens 	    (u_longlong_t)ub->ub_timestamp, asctime(localtime(&timestamp)));
1938fa9e4066Sahrens 	if (dump_opt['u'] >= 3) {
1939fbabab8fSmaybee 		char blkbuf[BP_SPRINTF_LEN];
194043466aaeSMax Grossman 		snprintf_blkptr(blkbuf, sizeof (blkbuf), &ub->ub_rootbp);
1941fa9e4066Sahrens 		(void) printf("\trootbp = %s\n", blkbuf);
1942fa9e4066Sahrens 	}
194353b9a4a9SVictor Latushkin 	(void) printf(footer ? footer : "");
1944fa9e4066Sahrens }
1945fa9e4066Sahrens 
1946fa9e4066Sahrens static void
194707428bdfSVictor Latushkin dump_config(spa_t *spa)
1948fa9e4066Sahrens {
194907428bdfSVictor Latushkin 	dmu_buf_t *db;
195007428bdfSVictor Latushkin 	size_t nvsize = 0;
195107428bdfSVictor Latushkin 	int error = 0;
1952fa9e4066Sahrens 
195307428bdfSVictor Latushkin 
195407428bdfSVictor Latushkin 	error = dmu_bonus_hold(spa->spa_meta_objset,
195507428bdfSVictor Latushkin 	    spa->spa_config_object, FTAG, &db);
195607428bdfSVictor Latushkin 
195707428bdfSVictor Latushkin 	if (error == 0) {
195807428bdfSVictor Latushkin 		nvsize = *(uint64_t *)db->db_data;
195907428bdfSVictor Latushkin 		dmu_buf_rele(db, FTAG);
196007428bdfSVictor Latushkin 
196107428bdfSVictor Latushkin 		(void) printf("\nMOS Configuration:\n");
196207428bdfSVictor Latushkin 		dump_packed_nvlist(spa->spa_meta_objset,
196307428bdfSVictor Latushkin 		    spa->spa_config_object, (void *)&nvsize, 1);
196407428bdfSVictor Latushkin 	} else {
196507428bdfSVictor Latushkin 		(void) fprintf(stderr, "dmu_bonus_hold(%llu) failed, errno %d",
196607428bdfSVictor Latushkin 		    (u_longlong_t)spa->spa_config_object, error);
1967fa9e4066Sahrens 	}
1968fa9e4066Sahrens }
1969fa9e4066Sahrens 
1970fa9e4066Sahrens static void
1971c5904d13Seschrock dump_cachefile(const char *cachefile)
1972c5904d13Seschrock {
1973c5904d13Seschrock 	int fd;
1974c5904d13Seschrock 	struct stat64 statbuf;
1975c5904d13Seschrock 	char *buf;
1976c5904d13Seschrock 	nvlist_t *config;
1977c5904d13Seschrock 
1978c5904d13Seschrock 	if ((fd = open64(cachefile, O_RDONLY)) < 0) {
1979c5904d13Seschrock 		(void) printf("cannot open '%s': %s\n", cachefile,
1980c5904d13Seschrock 		    strerror(errno));
1981c5904d13Seschrock 		exit(1);
1982c5904d13Seschrock 	}
1983c5904d13Seschrock 
1984c5904d13Seschrock 	if (fstat64(fd, &statbuf) != 0) {
1985c5904d13Seschrock 		(void) printf("failed to stat '%s': %s\n", cachefile,
1986c5904d13Seschrock 		    strerror(errno));
1987c5904d13Seschrock 		exit(1);
1988c5904d13Seschrock 	}
1989c5904d13Seschrock 
1990c5904d13Seschrock 	if ((buf = malloc(statbuf.st_size)) == NULL) {
1991c5904d13Seschrock 		(void) fprintf(stderr, "failed to allocate %llu bytes\n",
1992c5904d13Seschrock 		    (u_longlong_t)statbuf.st_size);
1993c5904d13Seschrock 		exit(1);
1994c5904d13Seschrock 	}
1995c5904d13Seschrock 
1996c5904d13Seschrock 	if (read(fd, buf, statbuf.st_size) != statbuf.st_size) {
1997c5904d13Seschrock 		(void) fprintf(stderr, "failed to read %llu bytes\n",
1998c5904d13Seschrock 		    (u_longlong_t)statbuf.st_size);
1999c5904d13Seschrock 		exit(1);
2000c5904d13Seschrock 	}
2001c5904d13Seschrock 
2002c5904d13Seschrock 	(void) close(fd);
2003c5904d13Seschrock 
2004c5904d13Seschrock 	if (nvlist_unpack(buf, statbuf.st_size, &config, 0) != 0) {
2005c5904d13Seschrock 		(void) fprintf(stderr, "failed to unpack nvlist\n");
2006c5904d13Seschrock 		exit(1);
2007c5904d13Seschrock 	}
2008c5904d13Seschrock 
2009c5904d13Seschrock 	free(buf);
2010c5904d13Seschrock 
2011c5904d13Seschrock 	dump_nvlist(config, 0);
2012c5904d13Seschrock 
2013c5904d13Seschrock 	nvlist_free(config);
2014c5904d13Seschrock }
2015c5904d13Seschrock 
201653b9a4a9SVictor Latushkin #define	ZDB_MAX_UB_HEADER_SIZE 32
201753b9a4a9SVictor Latushkin 
201853b9a4a9SVictor Latushkin static void
201953b9a4a9SVictor Latushkin dump_label_uberblocks(vdev_label_t *lbl, uint64_t ashift)
202053b9a4a9SVictor Latushkin {
202153b9a4a9SVictor Latushkin 	vdev_t vd;
202253b9a4a9SVictor Latushkin 	vdev_t *vdp = &vd;
202353b9a4a9SVictor Latushkin 	char header[ZDB_MAX_UB_HEADER_SIZE];
202453b9a4a9SVictor Latushkin 
202553b9a4a9SVictor Latushkin 	vd.vdev_ashift = ashift;
202653b9a4a9SVictor Latushkin 	vdp->vdev_top = vdp;
202753b9a4a9SVictor Latushkin 
202853b9a4a9SVictor Latushkin 	for (int i = 0; i < VDEV_UBERBLOCK_COUNT(vdp); i++) {
202953b9a4a9SVictor Latushkin 		uint64_t uoff = VDEV_UBERBLOCK_OFFSET(vdp, i);
203053b9a4a9SVictor Latushkin 		uberblock_t *ub = (void *)((char *)lbl + uoff);
203153b9a4a9SVictor Latushkin 
203253b9a4a9SVictor Latushkin 		if (uberblock_verify(ub))
203353b9a4a9SVictor Latushkin 			continue;
203453b9a4a9SVictor Latushkin 		(void) snprintf(header, ZDB_MAX_UB_HEADER_SIZE,
203553b9a4a9SVictor Latushkin 		    "Uberblock[%d]\n", i);
203653b9a4a9SVictor Latushkin 		dump_uberblock(ub, header, "");
203753b9a4a9SVictor Latushkin 	}
203853b9a4a9SVictor Latushkin }
203953b9a4a9SVictor Latushkin 
2040c5904d13Seschrock static void
2041fa9e4066Sahrens dump_label(const char *dev)
2042fa9e4066Sahrens {
2043fa9e4066Sahrens 	int fd;
2044fa9e4066Sahrens 	vdev_label_t label;
2045c6065d0fSGeorge Wilson 	char *path, *buf = label.vl_vdev_phys.vp_nvlist;
2046fa9e4066Sahrens 	size_t buflen = sizeof (label.vl_vdev_phys.vp_nvlist);
2047fa9e4066Sahrens 	struct stat64 statbuf;
204853b9a4a9SVictor Latushkin 	uint64_t psize, ashift;
2049c6065d0fSGeorge Wilson 	int len = strlen(dev) + 1;
2050fa9e4066Sahrens 
2051c6065d0fSGeorge Wilson 	if (strncmp(dev, "/dev/dsk/", 9) == 0) {
2052c6065d0fSGeorge Wilson 		len++;
2053c6065d0fSGeorge Wilson 		path = malloc(len);
2054c6065d0fSGeorge Wilson 		(void) snprintf(path, len, "%s%s", "/dev/rdsk/", dev + 9);
2055c6065d0fSGeorge Wilson 	} else {
2056c6065d0fSGeorge Wilson 		path = strdup(dev);
2057c6065d0fSGeorge Wilson 	}
2058c6065d0fSGeorge Wilson 
2059c6065d0fSGeorge Wilson 	if ((fd = open64(path, O_RDONLY)) < 0) {
2060c6065d0fSGeorge Wilson 		(void) printf("cannot open '%s': %s\n", path, strerror(errno));
2061c6065d0fSGeorge Wilson 		free(path);
2062fa9e4066Sahrens 		exit(1);
2063fa9e4066Sahrens 	}
2064fa9e4066Sahrens 
2065fa9e4066Sahrens 	if (fstat64(fd, &statbuf) != 0) {
2066c6065d0fSGeorge Wilson 		(void) printf("failed to stat '%s': %s\n", path,
2067fa9e4066Sahrens 		    strerror(errno));
2068c6065d0fSGeorge Wilson 		free(path);
2069c6065d0fSGeorge Wilson 		(void) close(fd);
2070c6065d0fSGeorge Wilson 		exit(1);
2071c6065d0fSGeorge Wilson 	}
2072c6065d0fSGeorge Wilson 
2073c6065d0fSGeorge Wilson 	if (S_ISBLK(statbuf.st_mode)) {
2074c6065d0fSGeorge Wilson 		(void) printf("cannot use '%s': character device required\n",
2075c6065d0fSGeorge Wilson 		    path);
2076c6065d0fSGeorge Wilson 		free(path);
2077c6065d0fSGeorge Wilson 		(void) close(fd);
2078c6065d0fSGeorge Wilson 		exit(1);
2079fa9e4066Sahrens 	}
2080fa9e4066Sahrens 
2081fa9e4066Sahrens 	psize = statbuf.st_size;
2082fa9e4066Sahrens 	psize = P2ALIGN(psize, (uint64_t)sizeof (vdev_label_t));
2083fa9e4066Sahrens 
208453b9a4a9SVictor Latushkin 	for (int l = 0; l < VDEV_LABELS; l++) {
2085fa9e4066Sahrens 		nvlist_t *config = NULL;
2086fa9e4066Sahrens 
2087fa9e4066Sahrens 		(void) printf("--------------------------------------------\n");
2088fa9e4066Sahrens 		(void) printf("LABEL %d\n", l);
2089fa9e4066Sahrens 		(void) printf("--------------------------------------------\n");
2090fa9e4066Sahrens 
20910d981225Seschrock 		if (pread64(fd, &label, sizeof (label),
2092fa9e4066Sahrens 		    vdev_label_offset(psize, l, 0)) != sizeof (label)) {
2093fa9e4066Sahrens 			(void) printf("failed to read label %d\n", l);
2094fa9e4066Sahrens 			continue;
2095fa9e4066Sahrens 		}
2096fa9e4066Sahrens 
2097fa9e4066Sahrens 		if (nvlist_unpack(buf, buflen, &config, 0) != 0) {
2098fa9e4066Sahrens 			(void) printf("failed to unpack label %d\n", l);
209953b9a4a9SVictor Latushkin 			ashift = SPA_MINBLOCKSHIFT;
210053b9a4a9SVictor Latushkin 		} else {
210153b9a4a9SVictor Latushkin 			nvlist_t *vdev_tree = NULL;
210253b9a4a9SVictor Latushkin 
2103fa9e4066Sahrens 			dump_nvlist(config, 4);
210453b9a4a9SVictor Latushkin 			if ((nvlist_lookup_nvlist(config,
210553b9a4a9SVictor Latushkin 			    ZPOOL_CONFIG_VDEV_TREE, &vdev_tree) != 0) ||
210653b9a4a9SVictor Latushkin 			    (nvlist_lookup_uint64(vdev_tree,
210753b9a4a9SVictor Latushkin 			    ZPOOL_CONFIG_ASHIFT, &ashift) != 0))
210853b9a4a9SVictor Latushkin 				ashift = SPA_MINBLOCKSHIFT;
2109fa9e4066Sahrens 			nvlist_free(config);
2110fa9e4066Sahrens 		}
211153b9a4a9SVictor Latushkin 		if (dump_opt['u'])
211253b9a4a9SVictor Latushkin 			dump_label_uberblocks(&label, ashift);
211353b9a4a9SVictor Latushkin 	}
2114c6065d0fSGeorge Wilson 
2115c6065d0fSGeorge Wilson 	free(path);
2116c6065d0fSGeorge Wilson 	(void) close(fd);
2117fa9e4066Sahrens }
2118fa9e4066Sahrens 
2119fa9e4066Sahrens /*ARGSUSED*/
21201d452cf5Sahrens static int
2121fd136879SMatthew Ahrens dump_one_dir(const char *dsname, void *arg)
2122fa9e4066Sahrens {
2123fa9e4066Sahrens 	int error;
2124fa9e4066Sahrens 	objset_t *os;
2125fa9e4066Sahrens 
2126503ad85cSMatthew Ahrens 	error = dmu_objset_own(dsname, DMU_OST_ANY, B_TRUE, FTAG, &os);
2127fa9e4066Sahrens 	if (error) {
2128b24ab676SJeff Bonwick 		(void) printf("Could not open %s, error %d\n", dsname, error);
21291d452cf5Sahrens 		return (0);
2130fa9e4066Sahrens 	}
2131fa9e4066Sahrens 	dump_dir(os);
2132503ad85cSMatthew Ahrens 	dmu_objset_disown(os, FTAG);
2133e0d35c44Smarks 	fuid_table_destroy();
21340a586ceaSMark Shellenbaum 	sa_loaded = B_FALSE;
21351d452cf5Sahrens 	return (0);
2136fa9e4066Sahrens }
2137fa9e4066Sahrens 
2138b24ab676SJeff Bonwick /*
2139b24ab676SJeff Bonwick  * Block statistics.
2140b24ab676SJeff Bonwick  */
2141490d05b9SMatthew Ahrens #define	PSIZE_HISTO_SIZE (SPA_MAXBLOCKSIZE / SPA_MINBLOCKSIZE + 1)
2142b24ab676SJeff Bonwick typedef struct zdb_blkstats {
2143b24ab676SJeff Bonwick 	uint64_t zb_asize;
2144b24ab676SJeff Bonwick 	uint64_t zb_lsize;
2145b24ab676SJeff Bonwick 	uint64_t zb_psize;
2146b24ab676SJeff Bonwick 	uint64_t zb_count;
2147d5ee8a13SMatthew Ahrens 	uint64_t zb_gangs;
2148d5ee8a13SMatthew Ahrens 	uint64_t zb_ditto_samevdev;
2149490d05b9SMatthew Ahrens 	uint64_t zb_psize_histogram[PSIZE_HISTO_SIZE];
2150b24ab676SJeff Bonwick } zdb_blkstats_t;
2151b24ab676SJeff Bonwick 
2152b24ab676SJeff Bonwick /*
2153b24ab676SJeff Bonwick  * Extended object types to report deferred frees and dedup auto-ditto blocks.
2154b24ab676SJeff Bonwick  */
2155b24ab676SJeff Bonwick #define	ZDB_OT_DEFERRED	(DMU_OT_NUMTYPES + 0)
2156b24ab676SJeff Bonwick #define	ZDB_OT_DITTO	(DMU_OT_NUMTYPES + 1)
2157ad135b5dSChristopher Siden #define	ZDB_OT_OTHER	(DMU_OT_NUMTYPES + 2)
2158ad135b5dSChristopher Siden #define	ZDB_OT_TOTAL	(DMU_OT_NUMTYPES + 3)
2159b24ab676SJeff Bonwick 
2160b24ab676SJeff Bonwick static char *zdb_ot_extname[] = {
2161b24ab676SJeff Bonwick 	"deferred free",
2162b24ab676SJeff Bonwick 	"dedup ditto",
2163ad135b5dSChristopher Siden 	"other",
2164b24ab676SJeff Bonwick 	"Total",
2165b24ab676SJeff Bonwick };
2166b24ab676SJeff Bonwick 
2167b24ab676SJeff Bonwick #define	ZB_TOTAL	DN_MAX_LEVELS
2168b24ab676SJeff Bonwick 
2169b24ab676SJeff Bonwick typedef struct zdb_cb {
2170b24ab676SJeff Bonwick 	zdb_blkstats_t	zcb_type[ZB_TOTAL + 1][ZDB_OT_TOTAL + 1];
2171b24ab676SJeff Bonwick 	uint64_t	zcb_dedup_asize;
2172b24ab676SJeff Bonwick 	uint64_t	zcb_dedup_blocks;
21735d7b4d43SMatthew Ahrens 	uint64_t	zcb_embedded_blocks[NUM_BP_EMBEDDED_TYPES];
21745d7b4d43SMatthew Ahrens 	uint64_t	zcb_embedded_histogram[NUM_BP_EMBEDDED_TYPES]
21755d7b4d43SMatthew Ahrens 	    [BPE_PAYLOAD_SIZE];
2176490d05b9SMatthew Ahrens 	uint64_t	zcb_start;
2177490d05b9SMatthew Ahrens 	uint64_t	zcb_lastprint;
2178490d05b9SMatthew Ahrens 	uint64_t	zcb_totalasize;
2179b24ab676SJeff Bonwick 	uint64_t	zcb_errors[256];
2180b24ab676SJeff Bonwick 	int		zcb_readfails;
2181b24ab676SJeff Bonwick 	int		zcb_haderrors;
2182cde58dbcSMatthew Ahrens 	spa_t		*zcb_spa;
2183b24ab676SJeff Bonwick } zdb_cb_t;
2184b24ab676SJeff Bonwick 
2185b24ab676SJeff Bonwick static void
2186cde58dbcSMatthew Ahrens zdb_count_block(zdb_cb_t *zcb, zilog_t *zilog, const blkptr_t *bp,
2187b24ab676SJeff Bonwick     dmu_object_type_t type)
2188b24ab676SJeff Bonwick {
2189b24ab676SJeff Bonwick 	uint64_t refcnt = 0;
2190b24ab676SJeff Bonwick 
2191b24ab676SJeff Bonwick 	ASSERT(type < ZDB_OT_TOTAL);
2192b24ab676SJeff Bonwick 
2193b24ab676SJeff Bonwick 	if (zilog && zil_bp_tree_add(zilog, bp) != 0)
2194b24ab676SJeff Bonwick 		return;
2195b24ab676SJeff Bonwick 
2196b24ab676SJeff Bonwick 	for (int i = 0; i < 4; i++) {
2197b24ab676SJeff Bonwick 		int l = (i < 2) ? BP_GET_LEVEL(bp) : ZB_TOTAL;
2198b24ab676SJeff Bonwick 		int t = (i & 1) ? type : ZDB_OT_TOTAL;
2199d5ee8a13SMatthew Ahrens 		int equal;
2200b24ab676SJeff Bonwick 		zdb_blkstats_t *zb = &zcb->zcb_type[l][t];
2201b24ab676SJeff Bonwick 
2202b24ab676SJeff Bonwick 		zb->zb_asize += BP_GET_ASIZE(bp);
2203b24ab676SJeff Bonwick 		zb->zb_lsize += BP_GET_LSIZE(bp);
2204b24ab676SJeff Bonwick 		zb->zb_psize += BP_GET_PSIZE(bp);
2205b24ab676SJeff Bonwick 		zb->zb_count++;
2206490d05b9SMatthew Ahrens 		zb->zb_psize_histogram[BP_GET_PSIZE(bp) >> SPA_MINBLOCKSHIFT]++;
2207d5ee8a13SMatthew Ahrens 
2208d5ee8a13SMatthew Ahrens 		zb->zb_gangs += BP_COUNT_GANG(bp);
2209d5ee8a13SMatthew Ahrens 
2210d5ee8a13SMatthew Ahrens 		switch (BP_GET_NDVAS(bp)) {
2211d5ee8a13SMatthew Ahrens 		case 2:
2212d5ee8a13SMatthew Ahrens 			if (DVA_GET_VDEV(&bp->blk_dva[0]) ==
2213d5ee8a13SMatthew Ahrens 			    DVA_GET_VDEV(&bp->blk_dva[1]))
2214d5ee8a13SMatthew Ahrens 				zb->zb_ditto_samevdev++;
2215d5ee8a13SMatthew Ahrens 			break;
2216d5ee8a13SMatthew Ahrens 		case 3:
2217d5ee8a13SMatthew Ahrens 			equal = (DVA_GET_VDEV(&bp->blk_dva[0]) ==
2218d5ee8a13SMatthew Ahrens 			    DVA_GET_VDEV(&bp->blk_dva[1])) +
2219d5ee8a13SMatthew Ahrens 			    (DVA_GET_VDEV(&bp->blk_dva[0]) ==
2220d5ee8a13SMatthew Ahrens 			    DVA_GET_VDEV(&bp->blk_dva[2])) +
2221d5ee8a13SMatthew Ahrens 			    (DVA_GET_VDEV(&bp->blk_dva[1]) ==
2222d5ee8a13SMatthew Ahrens 			    DVA_GET_VDEV(&bp->blk_dva[2]));
2223d5ee8a13SMatthew Ahrens 			if (equal != 0)
2224d5ee8a13SMatthew Ahrens 				zb->zb_ditto_samevdev++;
2225d5ee8a13SMatthew Ahrens 			break;
2226d5ee8a13SMatthew Ahrens 		}
2227d5ee8a13SMatthew Ahrens 
2228b24ab676SJeff Bonwick 	}
2229b24ab676SJeff Bonwick 
22305d7b4d43SMatthew Ahrens 	if (BP_IS_EMBEDDED(bp)) {
22315d7b4d43SMatthew Ahrens 		zcb->zcb_embedded_blocks[BPE_GET_ETYPE(bp)]++;
22325d7b4d43SMatthew Ahrens 		zcb->zcb_embedded_histogram[BPE_GET_ETYPE(bp)]
22335d7b4d43SMatthew Ahrens 		    [BPE_GET_PSIZE(bp)]++;
22345d7b4d43SMatthew Ahrens 		return;
22355d7b4d43SMatthew Ahrens 	}
22365d7b4d43SMatthew Ahrens 
2237b24ab676SJeff Bonwick 	if (dump_opt['L'])
2238b24ab676SJeff Bonwick 		return;
2239b24ab676SJeff Bonwick 
2240b24ab676SJeff Bonwick 	if (BP_GET_DEDUP(bp)) {
2241b24ab676SJeff Bonwick 		ddt_t *ddt;
2242b24ab676SJeff Bonwick 		ddt_entry_t *dde;
2243b24ab676SJeff Bonwick 
2244cde58dbcSMatthew Ahrens 		ddt = ddt_select(zcb->zcb_spa, bp);
2245b24ab676SJeff Bonwick 		ddt_enter(ddt);
2246b24ab676SJeff Bonwick 		dde = ddt_lookup(ddt, bp, B_FALSE);
2247b24ab676SJeff Bonwick 
2248b24ab676SJeff Bonwick 		if (dde == NULL) {
2249b24ab676SJeff Bonwick 			refcnt = 0;
2250b24ab676SJeff Bonwick 		} else {
2251b24ab676SJeff Bonwick 			ddt_phys_t *ddp = ddt_phys_select(dde, bp);
2252b24ab676SJeff Bonwick 			ddt_phys_decref(ddp);
2253b24ab676SJeff Bonwick 			refcnt = ddp->ddp_refcnt;
2254b24ab676SJeff Bonwick 			if (ddt_phys_total_refcnt(dde) == 0)
2255b24ab676SJeff Bonwick 				ddt_remove(ddt, dde);
2256b24ab676SJeff Bonwick 		}
2257b24ab676SJeff Bonwick 		ddt_exit(ddt);
2258b24ab676SJeff Bonwick 	}
2259b24ab676SJeff Bonwick 
2260cde58dbcSMatthew Ahrens 	VERIFY3U(zio_wait(zio_claim(NULL, zcb->zcb_spa,
2261cde58dbcSMatthew Ahrens 	    refcnt ? 0 : spa_first_txg(zcb->zcb_spa),
2262b24ab676SJeff Bonwick 	    bp, NULL, NULL, ZIO_FLAG_CANFAIL)), ==, 0);
2263b24ab676SJeff Bonwick }
2264b24ab676SJeff Bonwick 
226531d7e8faSGeorge Wilson static void
226631d7e8faSGeorge Wilson zdb_blkptr_done(zio_t *zio)
226731d7e8faSGeorge Wilson {
226831d7e8faSGeorge Wilson 	spa_t *spa = zio->io_spa;
226931d7e8faSGeorge Wilson 	blkptr_t *bp = zio->io_bp;
227031d7e8faSGeorge Wilson 	int ioerr = zio->io_error;
227131d7e8faSGeorge Wilson 	zdb_cb_t *zcb = zio->io_private;
22727802d7bfSMatthew Ahrens 	zbookmark_phys_t *zb = &zio->io_bookmark;
227331d7e8faSGeorge Wilson 
227431d7e8faSGeorge Wilson 	zio_data_buf_free(zio->io_data, zio->io_size);
227531d7e8faSGeorge Wilson 
227631d7e8faSGeorge Wilson 	mutex_enter(&spa->spa_scrub_lock);
227731d7e8faSGeorge Wilson 	spa->spa_scrub_inflight--;
227831d7e8faSGeorge Wilson 	cv_broadcast(&spa->spa_scrub_io_cv);
227931d7e8faSGeorge Wilson 
228031d7e8faSGeorge Wilson 	if (ioerr && !(zio->io_flags & ZIO_FLAG_SPECULATIVE)) {
228131d7e8faSGeorge Wilson 		char blkbuf[BP_SPRINTF_LEN];
228231d7e8faSGeorge Wilson 
228331d7e8faSGeorge Wilson 		zcb->zcb_haderrors = 1;
228431d7e8faSGeorge Wilson 		zcb->zcb_errors[ioerr]++;
228531d7e8faSGeorge Wilson 
228631d7e8faSGeorge Wilson 		if (dump_opt['b'] >= 2)
228743466aaeSMax Grossman 			snprintf_blkptr(blkbuf, sizeof (blkbuf), bp);
228831d7e8faSGeorge Wilson 		else
228931d7e8faSGeorge Wilson 			blkbuf[0] = '\0';
229031d7e8faSGeorge Wilson 
229131d7e8faSGeorge Wilson 		(void) printf("zdb_blkptr_cb: "
229231d7e8faSGeorge Wilson 		    "Got error %d reading "
229331d7e8faSGeorge Wilson 		    "<%llu, %llu, %lld, %llx> %s -- skipping\n",
229431d7e8faSGeorge Wilson 		    ioerr,
229531d7e8faSGeorge Wilson 		    (u_longlong_t)zb->zb_objset,
229631d7e8faSGeorge Wilson 		    (u_longlong_t)zb->zb_object,
229731d7e8faSGeorge Wilson 		    (u_longlong_t)zb->zb_level,
229831d7e8faSGeorge Wilson 		    (u_longlong_t)zb->zb_blkid,
229931d7e8faSGeorge Wilson 		    blkbuf);
230031d7e8faSGeorge Wilson 	}
230131d7e8faSGeorge Wilson 	mutex_exit(&spa->spa_scrub_lock);
230231d7e8faSGeorge Wilson }
230331d7e8faSGeorge Wilson 
2304b24ab676SJeff Bonwick static int
23051b912ec7SGeorge Wilson zdb_blkptr_cb(spa_t *spa, zilog_t *zilog, const blkptr_t *bp,
23067802d7bfSMatthew Ahrens     const zbookmark_phys_t *zb, const dnode_phys_t *dnp, void *arg)
2307b24ab676SJeff Bonwick {
2308b24ab676SJeff Bonwick 	zdb_cb_t *zcb = arg;
2309b24ab676SJeff Bonwick 	dmu_object_type_t type;
2310b24ab676SJeff Bonwick 	boolean_t is_metadata;
2311b24ab676SJeff Bonwick 
231243466aaeSMax Grossman 	if (dump_opt['b'] >= 5 && bp->blk_birth > 0) {
231343466aaeSMax Grossman 		char blkbuf[BP_SPRINTF_LEN];
231443466aaeSMax Grossman 		snprintf_blkptr(blkbuf, sizeof (blkbuf), bp);
231543466aaeSMax Grossman 		(void) printf("objset %llu object %llu "
231643466aaeSMax Grossman 		    "level %lld offset 0x%llx %s\n",
231743466aaeSMax Grossman 		    (u_longlong_t)zb->zb_objset,
231843466aaeSMax Grossman 		    (u_longlong_t)zb->zb_object,
231943466aaeSMax Grossman 		    (longlong_t)zb->zb_level,
232043466aaeSMax Grossman 		    (u_longlong_t)blkid2offset(dnp, bp, zb),
232143466aaeSMax Grossman 		    blkbuf);
232243466aaeSMax Grossman 	}
232343466aaeSMax Grossman 
232443466aaeSMax Grossman 	if (BP_IS_HOLE(bp))
2325b24ab676SJeff Bonwick 		return (0);
2326b24ab676SJeff Bonwick 
2327b24ab676SJeff Bonwick 	type = BP_GET_TYPE(bp);
2328b24ab676SJeff Bonwick 
2329ad135b5dSChristopher Siden 	zdb_count_block(zcb, zilog, bp,
2330ad135b5dSChristopher Siden 	    (type & DMU_OT_NEWTYPE) ? ZDB_OT_OTHER : type);
2331b24ab676SJeff Bonwick 
2332ad135b5dSChristopher Siden 	is_metadata = (BP_GET_LEVEL(bp) != 0 || DMU_OT_IS_METADATA(type));
2333b24ab676SJeff Bonwick 
23345d7b4d43SMatthew Ahrens 	if (!BP_IS_EMBEDDED(bp) &&
23355d7b4d43SMatthew Ahrens 	    (dump_opt['c'] > 1 || (dump_opt['c'] && is_metadata))) {
2336b24ab676SJeff Bonwick 		size_t size = BP_GET_PSIZE(bp);
233731d7e8faSGeorge Wilson 		void *data = zio_data_buf_alloc(size);
2338b24ab676SJeff Bonwick 		int flags = ZIO_FLAG_CANFAIL | ZIO_FLAG_SCRUB | ZIO_FLAG_RAW;
2339b24ab676SJeff Bonwick 
2340b24ab676SJeff Bonwick 		/* If it's an intent log block, failure is expected. */
2341b24ab676SJeff Bonwick 		if (zb->zb_level == ZB_ZIL_LEVEL)
2342b24ab676SJeff Bonwick 			flags |= ZIO_FLAG_SPECULATIVE;
2343b24ab676SJeff Bonwick 
234431d7e8faSGeorge Wilson 		mutex_enter(&spa->spa_scrub_lock);
234531d7e8faSGeorge Wilson 		while (spa->spa_scrub_inflight > max_inflight)
234631d7e8faSGeorge Wilson 			cv_wait(&spa->spa_scrub_io_cv, &spa->spa_scrub_lock);
234731d7e8faSGeorge Wilson 		spa->spa_scrub_inflight++;
234831d7e8faSGeorge Wilson 		mutex_exit(&spa->spa_scrub_lock);
2349b24ab676SJeff Bonwick 
235031d7e8faSGeorge Wilson 		zio_nowait(zio_read(NULL, spa, bp, data, size,
235131d7e8faSGeorge Wilson 		    zdb_blkptr_done, zcb, ZIO_PRIORITY_ASYNC_READ, flags, zb));
2352b24ab676SJeff Bonwick 	}
2353b24ab676SJeff Bonwick 
2354b24ab676SJeff Bonwick 	zcb->zcb_readfails = 0;
2355b24ab676SJeff Bonwick 
2356490d05b9SMatthew Ahrens 	if (dump_opt['b'] < 5 && isatty(STDERR_FILENO) &&
2357490d05b9SMatthew Ahrens 	    gethrtime() > zcb->zcb_lastprint + NANOSEC) {
2358490d05b9SMatthew Ahrens 		uint64_t now = gethrtime();
2359490d05b9SMatthew Ahrens 		char buf[10];
2360490d05b9SMatthew Ahrens 		uint64_t bytes = zcb->zcb_type[ZB_TOTAL][ZDB_OT_TOTAL].zb_asize;
2361490d05b9SMatthew Ahrens 		int kb_per_sec =
2362490d05b9SMatthew Ahrens 		    1 + bytes / (1 + ((now - zcb->zcb_start) / 1000 / 1000));
2363490d05b9SMatthew Ahrens 		int sec_remaining =
2364490d05b9SMatthew Ahrens 		    (zcb->zcb_totalasize - bytes) / 1024 / kb_per_sec;
2365490d05b9SMatthew Ahrens 
2366490d05b9SMatthew Ahrens 		zfs_nicenum(bytes, buf, sizeof (buf));
2367490d05b9SMatthew Ahrens 		(void) fprintf(stderr,
2368490d05b9SMatthew Ahrens 		    "\r%5s completed (%4dMB/s) "
2369490d05b9SMatthew Ahrens 		    "estimated time remaining: %uhr %02umin %02usec        ",
2370490d05b9SMatthew Ahrens 		    buf, kb_per_sec / 1024,
2371490d05b9SMatthew Ahrens 		    sec_remaining / 60 / 60,
2372490d05b9SMatthew Ahrens 		    sec_remaining / 60 % 60,
2373490d05b9SMatthew Ahrens 		    sec_remaining % 60);
2374490d05b9SMatthew Ahrens 
2375490d05b9SMatthew Ahrens 		zcb->zcb_lastprint = now;
2376490d05b9SMatthew Ahrens 	}
2377490d05b9SMatthew Ahrens 
2378b24ab676SJeff Bonwick 	return (0);
2379b24ab676SJeff Bonwick }
2380b24ab676SJeff Bonwick 
2381fa9e4066Sahrens static void
23820713e232SGeorge Wilson zdb_leak(void *arg, uint64_t start, uint64_t size)
2383fa9e4066Sahrens {
23840713e232SGeorge Wilson 	vdev_t *vd = arg;
2385fa9e4066Sahrens 
2386fa9e4066Sahrens 	(void) printf("leaked space: vdev %llu, offset 0x%llx, size %llu\n",
2387e14bb325SJeff Bonwick 	    (u_longlong_t)vd->vdev_id, (u_longlong_t)start, (u_longlong_t)size);
2388e14bb325SJeff Bonwick }
2389e14bb325SJeff Bonwick 
23900713e232SGeorge Wilson static metaslab_ops_t zdb_metaslab_ops = {
23912e4c9986SGeorge Wilson 	NULL	/* alloc */
2392e14bb325SJeff Bonwick };
2393e14bb325SJeff Bonwick 
2394e14bb325SJeff Bonwick static void
2395bbfd46c4SJeff Bonwick zdb_ddt_leak_init(spa_t *spa, zdb_cb_t *zcb)
2396fa9e4066Sahrens {
2397bbfd46c4SJeff Bonwick 	ddt_bookmark_t ddb = { 0 };
2398b24ab676SJeff Bonwick 	ddt_entry_t dde;
2399b24ab676SJeff Bonwick 	int error;
2400fa9e4066Sahrens 
2401bbfd46c4SJeff Bonwick 	while ((error = ddt_walk(spa, &ddb, &dde)) == 0) {
2402b24ab676SJeff Bonwick 		blkptr_t blk;
2403b24ab676SJeff Bonwick 		ddt_phys_t *ddp = dde.dde_phys;
2404bbfd46c4SJeff Bonwick 
2405bbfd46c4SJeff Bonwick 		if (ddb.ddb_class == DDT_CLASS_UNIQUE)
2406bbfd46c4SJeff Bonwick 			return;
2407bbfd46c4SJeff Bonwick 
2408b24ab676SJeff Bonwick 		ASSERT(ddt_phys_total_refcnt(&dde) > 1);
2409bbfd46c4SJeff Bonwick 
2410b24ab676SJeff Bonwick 		for (int p = 0; p < DDT_PHYS_TYPES; p++, ddp++) {
2411b24ab676SJeff Bonwick 			if (ddp->ddp_phys_birth == 0)
2412b24ab676SJeff Bonwick 				continue;
2413bbfd46c4SJeff Bonwick 			ddt_bp_create(ddb.ddb_checksum,
2414bbfd46c4SJeff Bonwick 			    &dde.dde_key, ddp, &blk);
2415b24ab676SJeff Bonwick 			if (p == DDT_PHYS_DITTO) {
2416cde58dbcSMatthew Ahrens 				zdb_count_block(zcb, NULL, &blk, ZDB_OT_DITTO);
2417b24ab676SJeff Bonwick 			} else {
2418b24ab676SJeff Bonwick 				zcb->zcb_dedup_asize +=
2419b24ab676SJeff Bonwick 				    BP_GET_ASIZE(&blk) * (ddp->ddp_refcnt - 1);
2420b24ab676SJeff Bonwick 				zcb->zcb_dedup_blocks++;
2421b24ab676SJeff Bonwick 			}
2422b24ab676SJeff Bonwick 		}
2423b24ab676SJeff Bonwick 		if (!dump_opt['L']) {
2424bbfd46c4SJeff Bonwick 			ddt_t *ddt = spa->spa_ddt[ddb.ddb_checksum];
2425b24ab676SJeff Bonwick 			ddt_enter(ddt);
2426b24ab676SJeff Bonwick 			VERIFY(ddt_lookup(ddt, &blk, B_TRUE) != NULL);
2427b24ab676SJeff Bonwick 			ddt_exit(ddt);
2428b24ab676SJeff Bonwick 		}
2429b24ab676SJeff Bonwick 	}
2430b24ab676SJeff Bonwick 
2431b24ab676SJeff Bonwick 	ASSERT(error == ENOENT);
2432b24ab676SJeff Bonwick }
2433b24ab676SJeff Bonwick 
2434b24ab676SJeff Bonwick static void
2435b24ab676SJeff Bonwick zdb_leak_init(spa_t *spa, zdb_cb_t *zcb)
2436b24ab676SJeff Bonwick {
2437cde58dbcSMatthew Ahrens 	zcb->zcb_spa = spa;
2438cde58dbcSMatthew Ahrens 
2439b24ab676SJeff Bonwick 	if (!dump_opt['L']) {
2440b24ab676SJeff Bonwick 		vdev_t *rvd = spa->spa_root_vdev;
2441e14bb325SJeff Bonwick 		for (int c = 0; c < rvd->vdev_children; c++) {
2442e14bb325SJeff Bonwick 			vdev_t *vd = rvd->vdev_child[c];
2443e14bb325SJeff Bonwick 			for (int m = 0; m < vd->vdev_ms_count; m++) {
2444fa9e4066Sahrens 				metaslab_t *msp = vd->vdev_ms[m];
2445fa9e4066Sahrens 				mutex_enter(&msp->ms_lock);
24460713e232SGeorge Wilson 				metaslab_unload(msp);
24470713e232SGeorge Wilson 
24480713e232SGeorge Wilson 				/*
24490713e232SGeorge Wilson 				 * For leak detection, we overload the metaslab
24500713e232SGeorge Wilson 				 * ms_tree to contain allocated segments
24510713e232SGeorge Wilson 				 * instead of free segments. As a result,
24520713e232SGeorge Wilson 				 * we can't use the normal metaslab_load/unload
24530713e232SGeorge Wilson 				 * interfaces.
24540713e232SGeorge Wilson 				 */
24550713e232SGeorge Wilson 				if (msp->ms_sm != NULL) {
24560713e232SGeorge Wilson 					msp->ms_ops = &zdb_metaslab_ops;
24570713e232SGeorge Wilson 					VERIFY0(space_map_load(msp->ms_sm,
24580713e232SGeorge Wilson 					    msp->ms_tree, SM_ALLOC));
24590713e232SGeorge Wilson 					msp->ms_loaded = B_TRUE;
24600713e232SGeorge Wilson 				}
2461fa9e4066Sahrens 				mutex_exit(&msp->ms_lock);
2462fa9e4066Sahrens 			}
2463fa9e4066Sahrens 		}
2464fa9e4066Sahrens 	}
2465fa9e4066Sahrens 
2466b24ab676SJeff Bonwick 	spa_config_enter(spa, SCL_CONFIG, FTAG, RW_READER);
2467b24ab676SJeff Bonwick 
2468bbfd46c4SJeff Bonwick 	zdb_ddt_leak_init(spa, zcb);
2469b24ab676SJeff Bonwick 
2470b24ab676SJeff Bonwick 	spa_config_exit(spa, SCL_CONFIG, FTAG);
2471b24ab676SJeff Bonwick }
2472b24ab676SJeff Bonwick 
2473fa9e4066Sahrens static void
2474e14bb325SJeff Bonwick zdb_leak_fini(spa_t *spa)
2475fa9e4066Sahrens {
2476b24ab676SJeff Bonwick 	if (!dump_opt['L']) {
2477fa9e4066Sahrens 		vdev_t *rvd = spa->spa_root_vdev;
2478e14bb325SJeff Bonwick 		for (int c = 0; c < rvd->vdev_children; c++) {
2479e14bb325SJeff Bonwick 			vdev_t *vd = rvd->vdev_child[c];
2480e14bb325SJeff Bonwick 			for (int m = 0; m < vd->vdev_ms_count; m++) {
2481e14bb325SJeff Bonwick 				metaslab_t *msp = vd->vdev_ms[m];
2482e14bb325SJeff Bonwick 				mutex_enter(&msp->ms_lock);
24830713e232SGeorge Wilson 
24840713e232SGeorge Wilson 				/*
24850713e232SGeorge Wilson 				 * The ms_tree has been overloaded to
24860713e232SGeorge Wilson 				 * contain allocated segments. Now that we
24870713e232SGeorge Wilson 				 * finished traversing all blocks, any
24880713e232SGeorge Wilson 				 * block that remains in the ms_tree
24890713e232SGeorge Wilson 				 * represents an allocated block that we
24900713e232SGeorge Wilson 				 * did not claim during the traversal.
24910713e232SGeorge Wilson 				 * Claimed blocks would have been removed
24920713e232SGeorge Wilson 				 * from the ms_tree.
24930713e232SGeorge Wilson 				 */
24940713e232SGeorge Wilson 				range_tree_vacate(msp->ms_tree, zdb_leak, vd);
24950713e232SGeorge Wilson 				msp->ms_loaded = B_FALSE;
24960713e232SGeorge Wilson 
2497e14bb325SJeff Bonwick 				mutex_exit(&msp->ms_lock);
2498e14bb325SJeff Bonwick 			}
2499e14bb325SJeff Bonwick 		}
2500fa9e4066Sahrens 	}
2501fa9e4066Sahrens }
2502fa9e4066Sahrens 
2503cde58dbcSMatthew Ahrens /* ARGSUSED */
2504cde58dbcSMatthew Ahrens static int
2505cde58dbcSMatthew Ahrens count_block_cb(void *arg, const blkptr_t *bp, dmu_tx_t *tx)
2506cde58dbcSMatthew Ahrens {
2507cde58dbcSMatthew Ahrens 	zdb_cb_t *zcb = arg;
2508cde58dbcSMatthew Ahrens 
2509490d05b9SMatthew Ahrens 	if (dump_opt['b'] >= 5) {
2510cde58dbcSMatthew Ahrens 		char blkbuf[BP_SPRINTF_LEN];
251143466aaeSMax Grossman 		snprintf_blkptr(blkbuf, sizeof (blkbuf), bp);
2512cde58dbcSMatthew Ahrens 		(void) printf("[%s] %s\n",
2513cde58dbcSMatthew Ahrens 		    "deferred free", blkbuf);
2514cde58dbcSMatthew Ahrens 	}
2515cde58dbcSMatthew Ahrens 	zdb_count_block(zcb, NULL, bp, ZDB_OT_DEFERRED);
2516cde58dbcSMatthew Ahrens 	return (0);
2517cde58dbcSMatthew Ahrens }
2518cde58dbcSMatthew Ahrens 
2519fa9e4066Sahrens static int
2520fa9e4066Sahrens dump_block_stats(spa_t *spa)
2521fa9e4066Sahrens {
2522fa9e4066Sahrens 	zdb_cb_t zcb = { 0 };
2523fa9e4066Sahrens 	zdb_blkstats_t *zb, *tzb;
2524b24ab676SJeff Bonwick 	uint64_t norm_alloc, norm_space, total_alloc, total_found;
2525cd088ea4SVictor Latushkin 	int flags = TRAVERSE_PRE | TRAVERSE_PREFETCH_METADATA | TRAVERSE_HARD;
25265d7b4d43SMatthew Ahrens 	boolean_t leaks = B_FALSE;
2527fa9e4066Sahrens 
2528490d05b9SMatthew Ahrens 	(void) printf("\nTraversing all blocks %s%s%s%s%s...\n\n",
252982a0a985SVictor Latushkin 	    (dump_opt['c'] || !dump_opt['L']) ? "to verify " : "",
25306365109dSVictor Latushkin 	    (dump_opt['c'] == 1) ? "metadata " : "",
253182a0a985SVictor Latushkin 	    dump_opt['c'] ? "checksums " : "",
253282a0a985SVictor Latushkin 	    (dump_opt['c'] && !dump_opt['L']) ? "and verify " : "",
253382a0a985SVictor Latushkin 	    !dump_opt['L'] ? "nothing leaked " : "");
2534fa9e4066Sahrens 
2535fa9e4066Sahrens 	/*
2536e14bb325SJeff Bonwick 	 * Load all space maps as SM_ALLOC maps, then traverse the pool
2537e14bb325SJeff Bonwick 	 * claiming each block we discover.  If the pool is perfectly
2538e14bb325SJeff Bonwick 	 * consistent, the space maps will be empty when we're done.
2539e14bb325SJeff Bonwick 	 * Anything left over is a leak; any block we can't claim (because
2540e14bb325SJeff Bonwick 	 * it's not part of any space map) is a double allocation,
2541e14bb325SJeff Bonwick 	 * reference to a freed block, or an unclaimed log block.
2542fa9e4066Sahrens 	 */
2543b24ab676SJeff Bonwick 	zdb_leak_init(spa, &zcb);
2544fa9e4066Sahrens 
2545fa9e4066Sahrens 	/*
2546fa9e4066Sahrens 	 * If there's a deferred-free bplist, process that first.
2547fa9e4066Sahrens 	 */
2548cde58dbcSMatthew Ahrens 	(void) bpobj_iterate_nofree(&spa->spa_deferred_bpobj,
2549cde58dbcSMatthew Ahrens 	    count_block_cb, &zcb, NULL);
25503b2aab18SMatthew Ahrens 	if (spa_version(spa) >= SPA_VERSION_DEADLISTS) {
2551cde58dbcSMatthew Ahrens 		(void) bpobj_iterate_nofree(&spa->spa_dsl_pool->dp_free_bpobj,
2552cde58dbcSMatthew Ahrens 		    count_block_cb, &zcb, NULL);
25533b2aab18SMatthew Ahrens 	}
25542acef22dSMatthew Ahrens 	if (spa_feature_is_active(spa, SPA_FEATURE_ASYNC_DESTROY)) {
2555b420f3adSRichard Lowe 		VERIFY3U(0, ==, bptree_iterate(spa->spa_meta_objset,
2556ad135b5dSChristopher Siden 		    spa->spa_dsl_pool->dp_bptree_obj, B_FALSE, count_block_cb,
2557ad135b5dSChristopher Siden 		    &zcb, NULL));
2558ad135b5dSChristopher Siden 	}
2559fa9e4066Sahrens 
2560bbfd46c4SJeff Bonwick 	if (dump_opt['c'] > 1)
2561bbfd46c4SJeff Bonwick 		flags |= TRAVERSE_PREFETCH_DATA;
2562bbfd46c4SJeff Bonwick 
2563490d05b9SMatthew Ahrens 	zcb.zcb_totalasize = metaslab_class_get_alloc(spa_normal_class(spa));
2564490d05b9SMatthew Ahrens 	zcb.zcb_start = zcb.zcb_lastprint = gethrtime();
2565bbfd46c4SJeff Bonwick 	zcb.zcb_haderrors |= traverse_pool(spa, 0, flags, zdb_blkptr_cb, &zcb);
2566fa9e4066Sahrens 
256731d7e8faSGeorge Wilson 	/*
256831d7e8faSGeorge Wilson 	 * If we've traversed the data blocks then we need to wait for those
256931d7e8faSGeorge Wilson 	 * I/Os to complete. We leverage "The Godfather" zio to wait on
257031d7e8faSGeorge Wilson 	 * all async I/Os to complete.
257131d7e8faSGeorge Wilson 	 */
257231d7e8faSGeorge Wilson 	if (dump_opt['c']) {
25736f834bc1SMatthew Ahrens 		for (int i = 0; i < max_ncpus; i++) {
25746f834bc1SMatthew Ahrens 			(void) zio_wait(spa->spa_async_zio_root[i]);
25756f834bc1SMatthew Ahrens 			spa->spa_async_zio_root[i] = zio_root(spa, NULL, NULL,
257631d7e8faSGeorge Wilson 			    ZIO_FLAG_CANFAIL | ZIO_FLAG_SPECULATIVE |
257731d7e8faSGeorge Wilson 			    ZIO_FLAG_GODFATHER);
257831d7e8faSGeorge Wilson 		}
25796f834bc1SMatthew Ahrens 	}
258031d7e8faSGeorge Wilson 
2581b24ab676SJeff Bonwick 	if (zcb.zcb_haderrors) {
2582fa9e4066Sahrens 		(void) printf("\nError counts:\n\n");
2583fa9e4066Sahrens 		(void) printf("\t%5s  %s\n", "errno", "count");
2584b24ab676SJeff Bonwick 		for (int e = 0; e < 256; e++) {
2585fa9e4066Sahrens 			if (zcb.zcb_errors[e] != 0) {
2586fa9e4066Sahrens 				(void) printf("\t%5d  %llu\n",
2587fa9e4066Sahrens 				    e, (u_longlong_t)zcb.zcb_errors[e]);
2588fa9e4066Sahrens 			}
2589fa9e4066Sahrens 		}
2590fa9e4066Sahrens 	}
2591fa9e4066Sahrens 
2592fa9e4066Sahrens 	/*
2593fa9e4066Sahrens 	 * Report any leaked segments.
2594fa9e4066Sahrens 	 */
2595e14bb325SJeff Bonwick 	zdb_leak_fini(spa);
2596fa9e4066Sahrens 
2597b24ab676SJeff Bonwick 	tzb = &zcb.zcb_type[ZB_TOTAL][ZDB_OT_TOTAL];
2598d41e7643Sek110237 
2599b24ab676SJeff Bonwick 	norm_alloc = metaslab_class_get_alloc(spa_normal_class(spa));
2600b24ab676SJeff Bonwick 	norm_space = metaslab_class_get_space(spa_normal_class(spa));
2601fa9e4066Sahrens 
2602b24ab676SJeff Bonwick 	total_alloc = norm_alloc + metaslab_class_get_alloc(spa_log_class(spa));
2603b24ab676SJeff Bonwick 	total_found = tzb->zb_asize - zcb.zcb_dedup_asize;
26048654d025Sperrin 
2605b24ab676SJeff Bonwick 	if (total_found == total_alloc) {
260682a0a985SVictor Latushkin 		if (!dump_opt['L'])
2607fa9e4066Sahrens 			(void) printf("\n\tNo leaks (block sum matches space"
2608fa9e4066Sahrens 			    " maps exactly)\n");
2609fa9e4066Sahrens 	} else {
2610fa9e4066Sahrens 		(void) printf("block traversal size %llu != alloc %llu "
261182a0a985SVictor Latushkin 		    "(%s %lld)\n",
2612b24ab676SJeff Bonwick 		    (u_longlong_t)total_found,
2613b24ab676SJeff Bonwick 		    (u_longlong_t)total_alloc,
261482a0a985SVictor Latushkin 		    (dump_opt['L']) ? "unreachable" : "leaked",
2615b24ab676SJeff Bonwick 		    (longlong_t)(total_alloc - total_found));
26165d7b4d43SMatthew Ahrens 		leaks = B_TRUE;
2617fa9e4066Sahrens 	}
2618fa9e4066Sahrens 
2619fa9e4066Sahrens 	if (tzb->zb_count == 0)
2620fa9e4066Sahrens 		return (2);
2621fa9e4066Sahrens 
2622fa9e4066Sahrens 	(void) printf("\n");
2623fa9e4066Sahrens 	(void) printf("\tbp count:      %10llu\n",
2624fa9e4066Sahrens 	    (u_longlong_t)tzb->zb_count);
2625d5ee8a13SMatthew Ahrens 	(void) printf("\tganged count:  %10llu\n",
2626d5ee8a13SMatthew Ahrens 	    (longlong_t)tzb->zb_gangs);
2627b24ab676SJeff Bonwick 	(void) printf("\tbp logical:    %10llu      avg: %6llu\n",
2628fa9e4066Sahrens 	    (u_longlong_t)tzb->zb_lsize,
2629fa9e4066Sahrens 	    (u_longlong_t)(tzb->zb_lsize / tzb->zb_count));
2630b24ab676SJeff Bonwick 	(void) printf("\tbp physical:   %10llu      avg:"
2631b24ab676SJeff Bonwick 	    " %6llu     compression: %6.2f\n",
2632fa9e4066Sahrens 	    (u_longlong_t)tzb->zb_psize,
2633fa9e4066Sahrens 	    (u_longlong_t)(tzb->zb_psize / tzb->zb_count),
2634fa9e4066Sahrens 	    (double)tzb->zb_lsize / tzb->zb_psize);
2635b24ab676SJeff Bonwick 	(void) printf("\tbp allocated:  %10llu      avg:"
2636b24ab676SJeff Bonwick 	    " %6llu     compression: %6.2f\n",
2637fa9e4066Sahrens 	    (u_longlong_t)tzb->zb_asize,
2638fa9e4066Sahrens 	    (u_longlong_t)(tzb->zb_asize / tzb->zb_count),
2639fa9e4066Sahrens 	    (double)tzb->zb_lsize / tzb->zb_asize);
2640b24ab676SJeff Bonwick 	(void) printf("\tbp deduped:    %10llu    ref>1:"
2641b24ab676SJeff Bonwick 	    " %6llu   deduplication: %6.2f\n",
2642b24ab676SJeff Bonwick 	    (u_longlong_t)zcb.zcb_dedup_asize,
2643b24ab676SJeff Bonwick 	    (u_longlong_t)zcb.zcb_dedup_blocks,
2644b24ab676SJeff Bonwick 	    (double)zcb.zcb_dedup_asize / tzb->zb_asize + 1.0);
2645b24ab676SJeff Bonwick 	(void) printf("\tSPA allocated: %10llu     used: %5.2f%%\n",
2646b24ab676SJeff Bonwick 	    (u_longlong_t)norm_alloc, 100.0 * norm_alloc / norm_space);
2647fa9e4066Sahrens 
26485d7b4d43SMatthew Ahrens 	for (bp_embedded_type_t i = 0; i < NUM_BP_EMBEDDED_TYPES; i++) {
26495d7b4d43SMatthew Ahrens 		if (zcb.zcb_embedded_blocks[i] == 0)
26505d7b4d43SMatthew Ahrens 			continue;
26515d7b4d43SMatthew Ahrens 		(void) printf("\n");
26525d7b4d43SMatthew Ahrens 		(void) printf("\tadditional, non-pointer bps of type %u: "
26535d7b4d43SMatthew Ahrens 		    "%10llu\n",
26545d7b4d43SMatthew Ahrens 		    i, (u_longlong_t)zcb.zcb_embedded_blocks[i]);
26555d7b4d43SMatthew Ahrens 
26565d7b4d43SMatthew Ahrens 		if (dump_opt['b'] >= 3) {
26575d7b4d43SMatthew Ahrens 			(void) printf("\t number of (compressed) bytes:  "
26585d7b4d43SMatthew Ahrens 			    "number of bps\n");
26595d7b4d43SMatthew Ahrens 			dump_histogram(zcb.zcb_embedded_histogram[i],
26605d7b4d43SMatthew Ahrens 			    sizeof (zcb.zcb_embedded_histogram[i]) /
26615d7b4d43SMatthew Ahrens 			    sizeof (zcb.zcb_embedded_histogram[i][0]), 0);
26625d7b4d43SMatthew Ahrens 		}
26635d7b4d43SMatthew Ahrens 	}
26645d7b4d43SMatthew Ahrens 
2665d5ee8a13SMatthew Ahrens 	if (tzb->zb_ditto_samevdev != 0) {
2666d5ee8a13SMatthew Ahrens 		(void) printf("\tDittoed blocks on same vdev: %llu\n",
2667d5ee8a13SMatthew Ahrens 		    (longlong_t)tzb->zb_ditto_samevdev);
2668d5ee8a13SMatthew Ahrens 	}
2669d5ee8a13SMatthew Ahrens 
2670fa9e4066Sahrens 	if (dump_opt['b'] >= 2) {
2671fa9e4066Sahrens 		int l, t, level;
2672fa9e4066Sahrens 		(void) printf("\nBlocks\tLSIZE\tPSIZE\tASIZE"
2673fa9e4066Sahrens 		    "\t  avg\t comp\t%%Total\tType\n");
2674fa9e4066Sahrens 
2675b24ab676SJeff Bonwick 		for (t = 0; t <= ZDB_OT_TOTAL; t++) {
26763f9d6ad7SLin Ling 			char csize[32], lsize[32], psize[32], asize[32];
2677d5ee8a13SMatthew Ahrens 			char avg[32], gang[32];
2678fa9e4066Sahrens 			char *typename;
2679fa9e4066Sahrens 
2680b24ab676SJeff Bonwick 			if (t < DMU_OT_NUMTYPES)
2681b24ab676SJeff Bonwick 				typename = dmu_ot[t].ot_name;
2682b24ab676SJeff Bonwick 			else
2683b24ab676SJeff Bonwick 				typename = zdb_ot_extname[t - DMU_OT_NUMTYPES];
2684fa9e4066Sahrens 
2685fa9e4066Sahrens 			if (zcb.zcb_type[ZB_TOTAL][t].zb_asize == 0) {
2686fa9e4066Sahrens 				(void) printf("%6s\t%5s\t%5s\t%5s"
2687fa9e4066Sahrens 				    "\t%5s\t%5s\t%6s\t%s\n",
2688fa9e4066Sahrens 				    "-",
2689fa9e4066Sahrens 				    "-",
2690fa9e4066Sahrens 				    "-",
2691fa9e4066Sahrens 				    "-",
2692fa9e4066Sahrens 				    "-",
2693fa9e4066Sahrens 				    "-",
2694fa9e4066Sahrens 				    "-",
2695fa9e4066Sahrens 				    typename);
2696fa9e4066Sahrens 				continue;
2697fa9e4066Sahrens 			}
2698fa9e4066Sahrens 
2699fa9e4066Sahrens 			for (l = ZB_TOTAL - 1; l >= -1; l--) {
2700fa9e4066Sahrens 				level = (l == -1 ? ZB_TOTAL : l);
2701fa9e4066Sahrens 				zb = &zcb.zcb_type[level][t];
2702fa9e4066Sahrens 
2703fa9e4066Sahrens 				if (zb->zb_asize == 0)
2704fa9e4066Sahrens 					continue;
2705fa9e4066Sahrens 
2706fa9e4066Sahrens 				if (dump_opt['b'] < 3 && level != ZB_TOTAL)
2707fa9e4066Sahrens 					continue;
2708fa9e4066Sahrens 
2709fa9e4066Sahrens 				if (level == 0 && zb->zb_asize ==
2710fa9e4066Sahrens 				    zcb.zcb_type[ZB_TOTAL][t].zb_asize)
2711fa9e4066Sahrens 					continue;
2712fa9e4066Sahrens 
27133f9d6ad7SLin Ling 				zdb_nicenum(zb->zb_count, csize);
27143f9d6ad7SLin Ling 				zdb_nicenum(zb->zb_lsize, lsize);
27153f9d6ad7SLin Ling 				zdb_nicenum(zb->zb_psize, psize);
27163f9d6ad7SLin Ling 				zdb_nicenum(zb->zb_asize, asize);
27173f9d6ad7SLin Ling 				zdb_nicenum(zb->zb_asize / zb->zb_count, avg);
2718d5ee8a13SMatthew Ahrens 				zdb_nicenum(zb->zb_gangs, gang);
2719fa9e4066Sahrens 
2720fa9e4066Sahrens 				(void) printf("%6s\t%5s\t%5s\t%5s\t%5s"
2721fa9e4066Sahrens 				    "\t%5.2f\t%6.2f\t",
2722fa9e4066Sahrens 				    csize, lsize, psize, asize, avg,
2723fa9e4066Sahrens 				    (double)zb->zb_lsize / zb->zb_psize,
2724fa9e4066Sahrens 				    100.0 * zb->zb_asize / tzb->zb_asize);
2725fa9e4066Sahrens 
2726fa9e4066Sahrens 				if (level == ZB_TOTAL)
2727fa9e4066Sahrens 					(void) printf("%s\n", typename);
2728fa9e4066Sahrens 				else
2729fa9e4066Sahrens 					(void) printf("    L%d %s\n",
2730fa9e4066Sahrens 					    level, typename);
2731490d05b9SMatthew Ahrens 
2732d5ee8a13SMatthew Ahrens 				if (dump_opt['b'] >= 3 && zb->zb_gangs > 0) {
2733d5ee8a13SMatthew Ahrens 					(void) printf("\t number of ganged "
2734d5ee8a13SMatthew Ahrens 					    "blocks: %s\n", gang);
2735d5ee8a13SMatthew Ahrens 				}
2736d5ee8a13SMatthew Ahrens 
2737490d05b9SMatthew Ahrens 				if (dump_opt['b'] >= 4) {
2738490d05b9SMatthew Ahrens 					(void) printf("psize "
2739490d05b9SMatthew Ahrens 					    "(in 512-byte sectors): "
2740490d05b9SMatthew Ahrens 					    "number of blocks\n");
2741490d05b9SMatthew Ahrens 					dump_histogram(zb->zb_psize_histogram,
27420713e232SGeorge Wilson 					    PSIZE_HISTO_SIZE, 0);
2743490d05b9SMatthew Ahrens 				}
2744fa9e4066Sahrens 			}
2745fa9e4066Sahrens 		}
2746fa9e4066Sahrens 	}
2747fa9e4066Sahrens 
2748fa9e4066Sahrens 	(void) printf("\n");
2749fa9e4066Sahrens 
2750fa9e4066Sahrens 	if (leaks)
2751fa9e4066Sahrens 		return (2);
2752fa9e4066Sahrens 
2753fa9e4066Sahrens 	if (zcb.zcb_haderrors)
2754fa9e4066Sahrens 		return (3);
2755fa9e4066Sahrens 
2756fa9e4066Sahrens 	return (0);
2757fa9e4066Sahrens }
2758fa9e4066Sahrens 
2759b24ab676SJeff Bonwick typedef struct zdb_ddt_entry {
2760b24ab676SJeff Bonwick 	ddt_key_t	zdde_key;
2761b24ab676SJeff Bonwick 	uint64_t	zdde_ref_blocks;
2762b24ab676SJeff Bonwick 	uint64_t	zdde_ref_lsize;
2763b24ab676SJeff Bonwick 	uint64_t	zdde_ref_psize;
2764b24ab676SJeff Bonwick 	uint64_t	zdde_ref_dsize;
2765b24ab676SJeff Bonwick 	avl_node_t	zdde_node;
2766b24ab676SJeff Bonwick } zdb_ddt_entry_t;
2767b24ab676SJeff Bonwick 
2768b24ab676SJeff Bonwick /* ARGSUSED */
2769b24ab676SJeff Bonwick static int
2770b24ab676SJeff Bonwick zdb_ddt_add_cb(spa_t *spa, zilog_t *zilog, const blkptr_t *bp,
27717802d7bfSMatthew Ahrens     const zbookmark_phys_t *zb, const dnode_phys_t *dnp, void *arg)
2772b24ab676SJeff Bonwick {
2773b24ab676SJeff Bonwick 	avl_tree_t *t = arg;
2774b24ab676SJeff Bonwick 	avl_index_t where;
2775b24ab676SJeff Bonwick 	zdb_ddt_entry_t *zdde, zdde_search;
2776b24ab676SJeff Bonwick 
27775d7b4d43SMatthew Ahrens 	if (BP_IS_HOLE(bp) || BP_IS_EMBEDDED(bp))
2778b24ab676SJeff Bonwick 		return (0);
2779b24ab676SJeff Bonwick 
2780b24ab676SJeff Bonwick 	if (dump_opt['S'] > 1 && zb->zb_level == ZB_ROOT_LEVEL) {
2781b24ab676SJeff Bonwick 		(void) printf("traversing objset %llu, %llu objects, "
2782b24ab676SJeff Bonwick 		    "%lu blocks so far\n",
2783b24ab676SJeff Bonwick 		    (u_longlong_t)zb->zb_objset,
27845d7b4d43SMatthew Ahrens 		    (u_longlong_t)BP_GET_FILL(bp),
2785b24ab676SJeff Bonwick 		    avl_numnodes(t));
2786b24ab676SJeff Bonwick 	}
2787b24ab676SJeff Bonwick 
2788bbfd46c4SJeff Bonwick 	if (BP_IS_HOLE(bp) || BP_GET_CHECKSUM(bp) == ZIO_CHECKSUM_OFF ||
2789ad135b5dSChristopher Siden 	    BP_GET_LEVEL(bp) > 0 || DMU_OT_IS_METADATA(BP_GET_TYPE(bp)))
2790b24ab676SJeff Bonwick 		return (0);
2791b24ab676SJeff Bonwick 
2792b24ab676SJeff Bonwick 	ddt_key_fill(&zdde_search.zdde_key, bp);
2793b24ab676SJeff Bonwick 
2794b24ab676SJeff Bonwick 	zdde = avl_find(t, &zdde_search, &where);
2795b24ab676SJeff Bonwick 
2796b24ab676SJeff Bonwick 	if (zdde == NULL) {
2797b24ab676SJeff Bonwick 		zdde = umem_zalloc(sizeof (*zdde), UMEM_NOFAIL);
2798b24ab676SJeff Bonwick 		zdde->zdde_key = zdde_search.zdde_key;
2799b24ab676SJeff Bonwick 		avl_insert(t, zdde, where);
2800b24ab676SJeff Bonwick 	}
2801b24ab676SJeff Bonwick 
2802b24ab676SJeff Bonwick 	zdde->zdde_ref_blocks += 1;
2803b24ab676SJeff Bonwick 	zdde->zdde_ref_lsize += BP_GET_LSIZE(bp);
2804b24ab676SJeff Bonwick 	zdde->zdde_ref_psize += BP_GET_PSIZE(bp);
2805b24ab676SJeff Bonwick 	zdde->zdde_ref_dsize += bp_get_dsize_sync(spa, bp);
2806b24ab676SJeff Bonwick 
2807b24ab676SJeff Bonwick 	return (0);
2808b24ab676SJeff Bonwick }
2809b24ab676SJeff Bonwick 
2810b24ab676SJeff Bonwick static void
2811b24ab676SJeff Bonwick dump_simulated_ddt(spa_t *spa)
2812b24ab676SJeff Bonwick {
2813b24ab676SJeff Bonwick 	avl_tree_t t;
2814b24ab676SJeff Bonwick 	void *cookie = NULL;
2815b24ab676SJeff Bonwick 	zdb_ddt_entry_t *zdde;
2816b24ab676SJeff Bonwick 	ddt_histogram_t ddh_total = { 0 };
2817b24ab676SJeff Bonwick 	ddt_stat_t dds_total = { 0 };
2818b24ab676SJeff Bonwick 
2819b24ab676SJeff Bonwick 	avl_create(&t, ddt_entry_compare,
2820b24ab676SJeff Bonwick 	    sizeof (zdb_ddt_entry_t), offsetof(zdb_ddt_entry_t, zdde_node));
2821b24ab676SJeff Bonwick 
2822b24ab676SJeff Bonwick 	spa_config_enter(spa, SCL_CONFIG, FTAG, RW_READER);
2823b24ab676SJeff Bonwick 
2824bbfd46c4SJeff Bonwick 	(void) traverse_pool(spa, 0, TRAVERSE_PRE | TRAVERSE_PREFETCH_METADATA,
2825bbfd46c4SJeff Bonwick 	    zdb_ddt_add_cb, &t);
2826b24ab676SJeff Bonwick 
2827b24ab676SJeff Bonwick 	spa_config_exit(spa, SCL_CONFIG, FTAG);
2828b24ab676SJeff Bonwick 
2829b24ab676SJeff Bonwick 	while ((zdde = avl_destroy_nodes(&t, &cookie)) != NULL) {
2830b24ab676SJeff Bonwick 		ddt_stat_t dds;
2831b24ab676SJeff Bonwick 		uint64_t refcnt = zdde->zdde_ref_blocks;
2832b24ab676SJeff Bonwick 		ASSERT(refcnt != 0);
2833b24ab676SJeff Bonwick 
2834b24ab676SJeff Bonwick 		dds.dds_blocks = zdde->zdde_ref_blocks / refcnt;
2835b24ab676SJeff Bonwick 		dds.dds_lsize = zdde->zdde_ref_lsize / refcnt;
2836b24ab676SJeff Bonwick 		dds.dds_psize = zdde->zdde_ref_psize / refcnt;
2837b24ab676SJeff Bonwick 		dds.dds_dsize = zdde->zdde_ref_dsize / refcnt;
2838b24ab676SJeff Bonwick 
2839b24ab676SJeff Bonwick 		dds.dds_ref_blocks = zdde->zdde_ref_blocks;
2840b24ab676SJeff Bonwick 		dds.dds_ref_lsize = zdde->zdde_ref_lsize;
2841b24ab676SJeff Bonwick 		dds.dds_ref_psize = zdde->zdde_ref_psize;
2842b24ab676SJeff Bonwick 		dds.dds_ref_dsize = zdde->zdde_ref_dsize;
2843b24ab676SJeff Bonwick 
2844bf16b11eSMatthew Ahrens 		ddt_stat_add(&ddh_total.ddh_stat[highbit64(refcnt) - 1],
2845bf16b11eSMatthew Ahrens 		    &dds, 0);
2846b24ab676SJeff Bonwick 
2847b24ab676SJeff Bonwick 		umem_free(zdde, sizeof (*zdde));
2848b24ab676SJeff Bonwick 	}
2849b24ab676SJeff Bonwick 
2850b24ab676SJeff Bonwick 	avl_destroy(&t);
2851b24ab676SJeff Bonwick 
2852b24ab676SJeff Bonwick 	ddt_histogram_stat(&dds_total, &ddh_total);
2853b24ab676SJeff Bonwick 
2854b24ab676SJeff Bonwick 	(void) printf("Simulated DDT histogram:\n");
2855b24ab676SJeff Bonwick 
28569eb19f4dSGeorge Wilson 	zpool_dump_ddt(&dds_total, &ddh_total);
2857b24ab676SJeff Bonwick 
2858b24ab676SJeff Bonwick 	dump_dedup_ratio(&dds_total);
2859b24ab676SJeff Bonwick }
2860b24ab676SJeff Bonwick 
2861fa9e4066Sahrens static void
2862fa9e4066Sahrens dump_zpool(spa_t *spa)
2863fa9e4066Sahrens {
2864fa9e4066Sahrens 	dsl_pool_t *dp = spa_get_dsl(spa);
2865fa9e4066Sahrens 	int rc = 0;
2866fa9e4066Sahrens 
2867b24ab676SJeff Bonwick 	if (dump_opt['S']) {
2868b24ab676SJeff Bonwick 		dump_simulated_ddt(spa);
2869b24ab676SJeff Bonwick 		return;
2870b24ab676SJeff Bonwick 	}
2871b24ab676SJeff Bonwick 
287207428bdfSVictor Latushkin 	if (!dump_opt['e'] && dump_opt['C'] > 1) {
287307428bdfSVictor Latushkin 		(void) printf("\nCached configuration:\n");
287407428bdfSVictor Latushkin 		dump_nvlist(spa->spa_config, 8);
287507428bdfSVictor Latushkin 	}
287607428bdfSVictor Latushkin 
287707428bdfSVictor Latushkin 	if (dump_opt['C'])
287807428bdfSVictor Latushkin 		dump_config(spa);
287907428bdfSVictor Latushkin 
2880fa9e4066Sahrens 	if (dump_opt['u'])
288153b9a4a9SVictor Latushkin 		dump_uberblock(&spa->spa_uberblock, "\nUberblock:\n", "\n");
2882fa9e4066Sahrens 
2883b24ab676SJeff Bonwick 	if (dump_opt['D'])
2884b24ab676SJeff Bonwick 		dump_all_ddts(spa);
2885b24ab676SJeff Bonwick 
288687219db7SVictor Latushkin 	if (dump_opt['d'] > 2 || dump_opt['m'])
288787219db7SVictor Latushkin 		dump_metaslabs(spa);
28882e4c9986SGeorge Wilson 	if (dump_opt['M'])
28892e4c9986SGeorge Wilson 		dump_metaslab_groups(spa);
289087219db7SVictor Latushkin 
289187219db7SVictor Latushkin 	if (dump_opt['d'] || dump_opt['i']) {
2892fa9e4066Sahrens 		dump_dir(dp->dp_meta_objset);
2893fa9e4066Sahrens 		if (dump_opt['d'] >= 3) {
2894d0475637SMatthew Ahrens 			dump_bpobj(&spa->spa_deferred_bpobj,
2895d0475637SMatthew Ahrens 			    "Deferred frees", 0);
2896cde58dbcSMatthew Ahrens 			if (spa_version(spa) >= SPA_VERSION_DEADLISTS) {
2897cde58dbcSMatthew Ahrens 				dump_bpobj(&spa->spa_dsl_pool->dp_free_bpobj,
2898d0475637SMatthew Ahrens 				    "Pool snapshot frees", 0);
2899ad135b5dSChristopher Siden 			}
2900ad135b5dSChristopher Siden 
2901ad135b5dSChristopher Siden 			if (spa_feature_is_active(spa,
29022acef22dSMatthew Ahrens 			    SPA_FEATURE_ASYNC_DESTROY)) {
2903ad135b5dSChristopher Siden 				dump_bptree(spa->spa_meta_objset,
2904ad135b5dSChristopher Siden 				    spa->spa_dsl_pool->dp_bptree_obj,
2905ad135b5dSChristopher Siden 				    "Pool dataset frees");
2906cde58dbcSMatthew Ahrens 			}
2907fa9e4066Sahrens 			dump_dtl(spa->spa_root_vdev, 0);
2908fa9e4066Sahrens 		}
290907428bdfSVictor Latushkin 		(void) dmu_objset_find(spa_name(spa), dump_one_dir,
291007428bdfSVictor Latushkin 		    NULL, DS_FIND_SNAPSHOTS | DS_FIND_CHILDREN);
2911fa9e4066Sahrens 	}
2912b24ab676SJeff Bonwick 	if (dump_opt['b'] || dump_opt['c'])
2913fa9e4066Sahrens 		rc = dump_block_stats(spa);
2914fa9e4066Sahrens 
29150713e232SGeorge Wilson 	if (rc == 0)
29160713e232SGeorge Wilson 		rc = verify_spacemap_refcounts(spa);
29170713e232SGeorge Wilson 
2918fa9e4066Sahrens 	if (dump_opt['s'])
2919fa9e4066Sahrens 		show_pool_stats(spa);
2920fa9e4066Sahrens 
29218f18d1faSGeorge Wilson 	if (dump_opt['h'])
29228f18d1faSGeorge Wilson 		dump_history(spa);
29238f18d1faSGeorge Wilson 
2924fa9e4066Sahrens 	if (rc != 0)
2925fa9e4066Sahrens 		exit(rc);
2926fa9e4066Sahrens }
2927fa9e4066Sahrens 
292844cd46caSbillm #define	ZDB_FLAG_CHECKSUM	0x0001
292944cd46caSbillm #define	ZDB_FLAG_DECOMPRESS	0x0002
293044cd46caSbillm #define	ZDB_FLAG_BSWAP		0x0004
293144cd46caSbillm #define	ZDB_FLAG_GBH		0x0008
293244cd46caSbillm #define	ZDB_FLAG_INDIRECT	0x0010
293344cd46caSbillm #define	ZDB_FLAG_PHYS		0x0020
293444cd46caSbillm #define	ZDB_FLAG_RAW		0x0040
293544cd46caSbillm #define	ZDB_FLAG_PRINT_BLKPTR	0x0080
293644cd46caSbillm 
293744cd46caSbillm int flagbits[256];
293844cd46caSbillm 
293944cd46caSbillm static void
294044cd46caSbillm zdb_print_blkptr(blkptr_t *bp, int flags)
294144cd46caSbillm {
2942b24ab676SJeff Bonwick 	char blkbuf[BP_SPRINTF_LEN];
294344cd46caSbillm 
294444cd46caSbillm 	if (flags & ZDB_FLAG_BSWAP)
294544cd46caSbillm 		byteswap_uint64_array((void *)bp, sizeof (blkptr_t));
2946b24ab676SJeff Bonwick 
294743466aaeSMax Grossman 	snprintf_blkptr(blkbuf, sizeof (blkbuf), bp);
2948b24ab676SJeff Bonwick 	(void) printf("%s\n", blkbuf);
294944cd46caSbillm }
295044cd46caSbillm 
295144cd46caSbillm static void
295244cd46caSbillm zdb_dump_indirect(blkptr_t *bp, int nbps, int flags)
295344cd46caSbillm {
295444cd46caSbillm 	int i;
295544cd46caSbillm 
295644cd46caSbillm 	for (i = 0; i < nbps; i++)
295744cd46caSbillm 		zdb_print_blkptr(&bp[i], flags);
295844cd46caSbillm }
295944cd46caSbillm 
296044cd46caSbillm static void
296144cd46caSbillm zdb_dump_gbh(void *buf, int flags)
296244cd46caSbillm {
296344cd46caSbillm 	zdb_dump_indirect((blkptr_t *)buf, SPA_GBH_NBLKPTRS, flags);
296444cd46caSbillm }
296544cd46caSbillm 
296644cd46caSbillm static void
296744cd46caSbillm zdb_dump_block_raw(void *buf, uint64_t size, int flags)
296844cd46caSbillm {
296944cd46caSbillm 	if (flags & ZDB_FLAG_BSWAP)
297044cd46caSbillm 		byteswap_uint64_array(buf, size);
2971b24ab676SJeff Bonwick 	(void) write(1, buf, size);
297244cd46caSbillm }
297344cd46caSbillm 
297444cd46caSbillm static void
297544cd46caSbillm zdb_dump_block(char *label, void *buf, uint64_t size, int flags)
297644cd46caSbillm {
297744cd46caSbillm 	uint64_t *d = (uint64_t *)buf;
297844cd46caSbillm 	int nwords = size / sizeof (uint64_t);
297944cd46caSbillm 	int do_bswap = !!(flags & ZDB_FLAG_BSWAP);
298044cd46caSbillm 	int i, j;
298144cd46caSbillm 	char *hdr, *c;
298244cd46caSbillm 
298344cd46caSbillm 
298444cd46caSbillm 	if (do_bswap)
298544cd46caSbillm 		hdr = " 7 6 5 4 3 2 1 0   f e d c b a 9 8";
298644cd46caSbillm 	else
298744cd46caSbillm 		hdr = " 0 1 2 3 4 5 6 7   8 9 a b c d e f";
298844cd46caSbillm 
298944cd46caSbillm 	(void) printf("\n%s\n%6s   %s  0123456789abcdef\n", label, "", hdr);
299044cd46caSbillm 
299144cd46caSbillm 	for (i = 0; i < nwords; i += 2) {
299244cd46caSbillm 		(void) printf("%06llx:  %016llx  %016llx  ",
299344cd46caSbillm 		    (u_longlong_t)(i * sizeof (uint64_t)),
299444cd46caSbillm 		    (u_longlong_t)(do_bswap ? BSWAP_64(d[i]) : d[i]),
299544cd46caSbillm 		    (u_longlong_t)(do_bswap ? BSWAP_64(d[i + 1]) : d[i + 1]));
299644cd46caSbillm 
299744cd46caSbillm 		c = (char *)&d[i];
299844cd46caSbillm 		for (j = 0; j < 2 * sizeof (uint64_t); j++)
299944cd46caSbillm 			(void) printf("%c", isprint(c[j]) ? c[j] : '.');
300044cd46caSbillm 		(void) printf("\n");
300144cd46caSbillm 	}
300244cd46caSbillm }
300344cd46caSbillm 
300444cd46caSbillm /*
300544cd46caSbillm  * There are two acceptable formats:
300644cd46caSbillm  *	leaf_name	  - For example: c1t0d0 or /tmp/ztest.0a
300744cd46caSbillm  *	child[.child]*    - For example: 0.1.1
300844cd46caSbillm  *
300944cd46caSbillm  * The second form can be used to specify arbitrary vdevs anywhere
301044cd46caSbillm  * in the heirarchy.  For example, in a pool with a mirror of
301144cd46caSbillm  * RAID-Zs, you can specify either RAID-Z vdev with 0.0 or 0.1 .
301244cd46caSbillm  */
301344cd46caSbillm static vdev_t *
301444cd46caSbillm zdb_vdev_lookup(vdev_t *vdev, char *path)
301544cd46caSbillm {
301644cd46caSbillm 	char *s, *p, *q;
301744cd46caSbillm 	int i;
301844cd46caSbillm 
301944cd46caSbillm 	if (vdev == NULL)
302044cd46caSbillm 		return (NULL);
302144cd46caSbillm 
302244cd46caSbillm 	/* First, assume the x.x.x.x format */
302344cd46caSbillm 	i = (int)strtoul(path, &s, 10);
302444cd46caSbillm 	if (s == path || (s && *s != '.' && *s != '\0'))
302544cd46caSbillm 		goto name;
302644cd46caSbillm 	if (i < 0 || i >= vdev->vdev_children)
302744cd46caSbillm 		return (NULL);
302844cd46caSbillm 
302944cd46caSbillm 	vdev = vdev->vdev_child[i];
303044cd46caSbillm 	if (*s == '\0')
303144cd46caSbillm 		return (vdev);
303244cd46caSbillm 	return (zdb_vdev_lookup(vdev, s+1));
303344cd46caSbillm 
303444cd46caSbillm name:
303544cd46caSbillm 	for (i = 0; i < vdev->vdev_children; i++) {
303644cd46caSbillm 		vdev_t *vc = vdev->vdev_child[i];
303744cd46caSbillm 
303844cd46caSbillm 		if (vc->vdev_path == NULL) {
303944cd46caSbillm 			vc = zdb_vdev_lookup(vc, path);
304044cd46caSbillm 			if (vc == NULL)
304144cd46caSbillm 				continue;
304244cd46caSbillm 			else
304344cd46caSbillm 				return (vc);
304444cd46caSbillm 		}
304544cd46caSbillm 
304644cd46caSbillm 		p = strrchr(vc->vdev_path, '/');
304744cd46caSbillm 		p = p ? p + 1 : vc->vdev_path;
304844cd46caSbillm 		q = &vc->vdev_path[strlen(vc->vdev_path) - 2];
304944cd46caSbillm 
305044cd46caSbillm 		if (strcmp(vc->vdev_path, path) == 0)
305144cd46caSbillm 			return (vc);
305244cd46caSbillm 		if (strcmp(p, path) == 0)
305344cd46caSbillm 			return (vc);
305444cd46caSbillm 		if (strcmp(q, "s0") == 0 && strncmp(p, path, q - p) == 0)
305544cd46caSbillm 			return (vc);
305644cd46caSbillm 	}
305744cd46caSbillm 
305844cd46caSbillm 	return (NULL);
305944cd46caSbillm }
306044cd46caSbillm 
306144cd46caSbillm /*
306244cd46caSbillm  * Read a block from a pool and print it out.  The syntax of the
306344cd46caSbillm  * block descriptor is:
306444cd46caSbillm  *
306544cd46caSbillm  *	pool:vdev_specifier:offset:size[:flags]
306644cd46caSbillm  *
306744cd46caSbillm  *	pool           - The name of the pool you wish to read from
306844cd46caSbillm  *	vdev_specifier - Which vdev (see comment for zdb_vdev_lookup)
306944cd46caSbillm  *	offset         - offset, in hex, in bytes
307044cd46caSbillm  *	size           - Amount of data to read, in hex, in bytes
307144cd46caSbillm  *	flags          - A string of characters specifying options
307244cd46caSbillm  *		 b: Decode a blkptr at given offset within block
307344cd46caSbillm  *		*c: Calculate and display checksums
3074b24ab676SJeff Bonwick  *		 d: Decompress data before dumping
307544cd46caSbillm  *		 e: Byteswap data before dumping
3076b24ab676SJeff Bonwick  *		 g: Display data as a gang block header
3077b24ab676SJeff Bonwick  *		 i: Display as an indirect block
307844cd46caSbillm  *		 p: Do I/O to physical offset
307944cd46caSbillm  *		 r: Dump raw data to stdout
308044cd46caSbillm  *
308144cd46caSbillm  *              * = not yet implemented
308244cd46caSbillm  */
308344cd46caSbillm static void
308407428bdfSVictor Latushkin zdb_read_block(char *thing, spa_t *spa)
308544cd46caSbillm {
3086b24ab676SJeff Bonwick 	blkptr_t blk, *bp = &blk;
3087b24ab676SJeff Bonwick 	dva_t *dva = bp->blk_dva;
308844cd46caSbillm 	int flags = 0;
3089b24ab676SJeff Bonwick 	uint64_t offset = 0, size = 0, psize = 0, lsize = 0, blkptr_offset = 0;
309044cd46caSbillm 	zio_t *zio;
309144cd46caSbillm 	vdev_t *vd;
3092b24ab676SJeff Bonwick 	void *pbuf, *lbuf, *buf;
309307428bdfSVictor Latushkin 	char *s, *p, *dup, *vdev, *flagstr;
3094b24ab676SJeff Bonwick 	int i, error;
309544cd46caSbillm 
309644cd46caSbillm 	dup = strdup(thing);
309744cd46caSbillm 	s = strtok(dup, ":");
309844cd46caSbillm 	vdev = s ? s : "";
309944cd46caSbillm 	s = strtok(NULL, ":");
310044cd46caSbillm 	offset = strtoull(s ? s : "", NULL, 16);
310144cd46caSbillm 	s = strtok(NULL, ":");
310244cd46caSbillm 	size = strtoull(s ? s : "", NULL, 16);
310344cd46caSbillm 	s = strtok(NULL, ":");
310444cd46caSbillm 	flagstr = s ? s : "";
310544cd46caSbillm 
310644cd46caSbillm 	s = NULL;
310744cd46caSbillm 	if (size == 0)
310844cd46caSbillm 		s = "size must not be zero";
310944cd46caSbillm 	if (!IS_P2ALIGNED(size, DEV_BSIZE))
311044cd46caSbillm 		s = "size must be a multiple of sector size";
311144cd46caSbillm 	if (!IS_P2ALIGNED(offset, DEV_BSIZE))
311244cd46caSbillm 		s = "offset must be a multiple of sector size";
311344cd46caSbillm 	if (s) {
311444cd46caSbillm 		(void) printf("Invalid block specifier: %s  - %s\n", thing, s);
311544cd46caSbillm 		free(dup);
311644cd46caSbillm 		return;
311744cd46caSbillm 	}
311844cd46caSbillm 
311944cd46caSbillm 	for (s = strtok(flagstr, ":"); s; s = strtok(NULL, ":")) {
312044cd46caSbillm 		for (i = 0; flagstr[i]; i++) {
31215ad82045Snd150628 			int bit = flagbits[(uchar_t)flagstr[i]];
312244cd46caSbillm 
312344cd46caSbillm 			if (bit == 0) {
312444cd46caSbillm 				(void) printf("***Invalid flag: %c\n",
312544cd46caSbillm 				    flagstr[i]);
312644cd46caSbillm 				continue;
312744cd46caSbillm 			}
312844cd46caSbillm 			flags |= bit;
312944cd46caSbillm 
313044cd46caSbillm 			/* If it's not something with an argument, keep going */
3131b24ab676SJeff Bonwick 			if ((bit & (ZDB_FLAG_CHECKSUM |
313244cd46caSbillm 			    ZDB_FLAG_PRINT_BLKPTR)) == 0)
313344cd46caSbillm 				continue;
313444cd46caSbillm 
313544cd46caSbillm 			p = &flagstr[i + 1];
313644cd46caSbillm 			if (bit == ZDB_FLAG_PRINT_BLKPTR)
313744cd46caSbillm 				blkptr_offset = strtoull(p, &p, 16);
313844cd46caSbillm 			if (*p != ':' && *p != '\0') {
313944cd46caSbillm 				(void) printf("***Invalid flag arg: '%s'\n", s);
314044cd46caSbillm 				free(dup);
314144cd46caSbillm 				return;
314244cd46caSbillm 			}
314344cd46caSbillm 		}
314444cd46caSbillm 	}
314544cd46caSbillm 
314644cd46caSbillm 	vd = zdb_vdev_lookup(spa->spa_root_vdev, vdev);
314744cd46caSbillm 	if (vd == NULL) {
314844cd46caSbillm 		(void) printf("***Invalid vdev: %s\n", vdev);
314944cd46caSbillm 		free(dup);
315044cd46caSbillm 		return;
315144cd46caSbillm 	} else {
315244cd46caSbillm 		if (vd->vdev_path)
3153b24ab676SJeff Bonwick 			(void) fprintf(stderr, "Found vdev: %s\n",
3154b24ab676SJeff Bonwick 			    vd->vdev_path);
315544cd46caSbillm 		else
3156b24ab676SJeff Bonwick 			(void) fprintf(stderr, "Found vdev type: %s\n",
315744cd46caSbillm 			    vd->vdev_ops->vdev_op_type);
315844cd46caSbillm 	}
315944cd46caSbillm 
3160b24ab676SJeff Bonwick 	psize = size;
3161b24ab676SJeff Bonwick 	lsize = size;
316244cd46caSbillm 
3163b24ab676SJeff Bonwick 	pbuf = umem_alloc(SPA_MAXBLOCKSIZE, UMEM_NOFAIL);
3164b24ab676SJeff Bonwick 	lbuf = umem_alloc(SPA_MAXBLOCKSIZE, UMEM_NOFAIL);
3165b24ab676SJeff Bonwick 
3166b24ab676SJeff Bonwick 	BP_ZERO(bp);
3167b24ab676SJeff Bonwick 
3168b24ab676SJeff Bonwick 	DVA_SET_VDEV(&dva[0], vd->vdev_id);
3169b24ab676SJeff Bonwick 	DVA_SET_OFFSET(&dva[0], offset);
3170b24ab676SJeff Bonwick 	DVA_SET_GANG(&dva[0], !!(flags & ZDB_FLAG_GBH));
3171b24ab676SJeff Bonwick 	DVA_SET_ASIZE(&dva[0], vdev_psize_to_asize(vd, psize));
3172b24ab676SJeff Bonwick 
3173b24ab676SJeff Bonwick 	BP_SET_BIRTH(bp, TXG_INITIAL, TXG_INITIAL);
3174b24ab676SJeff Bonwick 
3175b24ab676SJeff Bonwick 	BP_SET_LSIZE(bp, lsize);
3176b24ab676SJeff Bonwick 	BP_SET_PSIZE(bp, psize);
3177b24ab676SJeff Bonwick 	BP_SET_COMPRESS(bp, ZIO_COMPRESS_OFF);
3178b24ab676SJeff Bonwick 	BP_SET_CHECKSUM(bp, ZIO_CHECKSUM_OFF);
3179b24ab676SJeff Bonwick 	BP_SET_TYPE(bp, DMU_OT_NONE);
3180b24ab676SJeff Bonwick 	BP_SET_LEVEL(bp, 0);
3181b24ab676SJeff Bonwick 	BP_SET_DEDUP(bp, 0);
3182b24ab676SJeff Bonwick 	BP_SET_BYTEORDER(bp, ZFS_HOST_BYTEORDER);
318344cd46caSbillm 
3184e14bb325SJeff Bonwick 	spa_config_enter(spa, SCL_STATE, FTAG, RW_READER);
318544cd46caSbillm 	zio = zio_root(spa, NULL, NULL, 0);
3186b24ab676SJeff Bonwick 
3187b24ab676SJeff Bonwick 	if (vd == vd->vdev_top) {
3188b24ab676SJeff Bonwick 		/*
3189b24ab676SJeff Bonwick 		 * Treat this as a normal block read.
3190b24ab676SJeff Bonwick 		 */
3191b24ab676SJeff Bonwick 		zio_nowait(zio_read(zio, spa, bp, pbuf, psize, NULL, NULL,
3192b24ab676SJeff Bonwick 		    ZIO_PRIORITY_SYNC_READ,
3193b24ab676SJeff Bonwick 		    ZIO_FLAG_CANFAIL | ZIO_FLAG_RAW, NULL));
3194b24ab676SJeff Bonwick 	} else {
3195b24ab676SJeff Bonwick 		/*
3196b24ab676SJeff Bonwick 		 * Treat this as a vdev child I/O.
3197b24ab676SJeff Bonwick 		 */
3198b24ab676SJeff Bonwick 		zio_nowait(zio_vdev_child_io(zio, bp, vd, offset, pbuf, psize,
3199b24ab676SJeff Bonwick 		    ZIO_TYPE_READ, ZIO_PRIORITY_SYNC_READ,
3200b24ab676SJeff Bonwick 		    ZIO_FLAG_DONT_CACHE | ZIO_FLAG_DONT_QUEUE |
3201b24ab676SJeff Bonwick 		    ZIO_FLAG_DONT_PROPAGATE | ZIO_FLAG_DONT_RETRY |
3202b24ab676SJeff Bonwick 		    ZIO_FLAG_CANFAIL | ZIO_FLAG_RAW, NULL, NULL));
3203b24ab676SJeff Bonwick 	}
3204b24ab676SJeff Bonwick 
320544cd46caSbillm 	error = zio_wait(zio);
3206e14bb325SJeff Bonwick 	spa_config_exit(spa, SCL_STATE, FTAG);
320744cd46caSbillm 
320844cd46caSbillm 	if (error) {
320944cd46caSbillm 		(void) printf("Read of %s failed, error: %d\n", thing, error);
321044cd46caSbillm 		goto out;
321144cd46caSbillm 	}
321244cd46caSbillm 
3213b24ab676SJeff Bonwick 	if (flags & ZDB_FLAG_DECOMPRESS) {
3214b24ab676SJeff Bonwick 		/*
3215b24ab676SJeff Bonwick 		 * We don't know how the data was compressed, so just try
3216b24ab676SJeff Bonwick 		 * every decompress function at every inflated blocksize.
3217b24ab676SJeff Bonwick 		 */
3218b24ab676SJeff Bonwick 		enum zio_compress c;
3219b24ab676SJeff Bonwick 		void *pbuf2 = umem_alloc(SPA_MAXBLOCKSIZE, UMEM_NOFAIL);
3220b24ab676SJeff Bonwick 		void *lbuf2 = umem_alloc(SPA_MAXBLOCKSIZE, UMEM_NOFAIL);
3221b24ab676SJeff Bonwick 
3222b24ab676SJeff Bonwick 		bcopy(pbuf, pbuf2, psize);
3223b24ab676SJeff Bonwick 
3224b24ab676SJeff Bonwick 		VERIFY(random_get_pseudo_bytes((uint8_t *)pbuf + psize,
3225b24ab676SJeff Bonwick 		    SPA_MAXBLOCKSIZE - psize) == 0);
3226b24ab676SJeff Bonwick 
3227b24ab676SJeff Bonwick 		VERIFY(random_get_pseudo_bytes((uint8_t *)pbuf2 + psize,
3228b24ab676SJeff Bonwick 		    SPA_MAXBLOCKSIZE - psize) == 0);
3229b24ab676SJeff Bonwick 
3230b24ab676SJeff Bonwick 		for (lsize = SPA_MAXBLOCKSIZE; lsize > psize;
3231b24ab676SJeff Bonwick 		    lsize -= SPA_MINBLOCKSIZE) {
3232b24ab676SJeff Bonwick 			for (c = 0; c < ZIO_COMPRESS_FUNCTIONS; c++) {
3233b24ab676SJeff Bonwick 				if (zio_decompress_data(c, pbuf, lbuf,
3234b24ab676SJeff Bonwick 				    psize, lsize) == 0 &&
3235b24ab676SJeff Bonwick 				    zio_decompress_data(c, pbuf2, lbuf2,
3236b24ab676SJeff Bonwick 				    psize, lsize) == 0 &&
3237b24ab676SJeff Bonwick 				    bcmp(lbuf, lbuf2, lsize) == 0)
3238b24ab676SJeff Bonwick 					break;
3239b24ab676SJeff Bonwick 			}
3240b24ab676SJeff Bonwick 			if (c != ZIO_COMPRESS_FUNCTIONS)
3241b24ab676SJeff Bonwick 				break;
3242b24ab676SJeff Bonwick 			lsize -= SPA_MINBLOCKSIZE;
3243b24ab676SJeff Bonwick 		}
3244b24ab676SJeff Bonwick 
3245b24ab676SJeff Bonwick 		umem_free(pbuf2, SPA_MAXBLOCKSIZE);
3246b24ab676SJeff Bonwick 		umem_free(lbuf2, SPA_MAXBLOCKSIZE);
3247b24ab676SJeff Bonwick 
3248b24ab676SJeff Bonwick 		if (lsize <= psize) {
3249b24ab676SJeff Bonwick 			(void) printf("Decompress of %s failed\n", thing);
3250b24ab676SJeff Bonwick 			goto out;
3251b24ab676SJeff Bonwick 		}
3252b24ab676SJeff Bonwick 		buf = lbuf;
3253b24ab676SJeff Bonwick 		size = lsize;
3254b24ab676SJeff Bonwick 	} else {
3255b24ab676SJeff Bonwick 		buf = pbuf;
3256b24ab676SJeff Bonwick 		size = psize;
3257b24ab676SJeff Bonwick 	}
3258b24ab676SJeff Bonwick 
325944cd46caSbillm 	if (flags & ZDB_FLAG_PRINT_BLKPTR)
326044cd46caSbillm 		zdb_print_blkptr((blkptr_t *)(void *)
326144cd46caSbillm 		    ((uintptr_t)buf + (uintptr_t)blkptr_offset), flags);
326244cd46caSbillm 	else if (flags & ZDB_FLAG_RAW)
326344cd46caSbillm 		zdb_dump_block_raw(buf, size, flags);
326444cd46caSbillm 	else if (flags & ZDB_FLAG_INDIRECT)
326544cd46caSbillm 		zdb_dump_indirect((blkptr_t *)buf, size / sizeof (blkptr_t),
326644cd46caSbillm 		    flags);
326744cd46caSbillm 	else if (flags & ZDB_FLAG_GBH)
326844cd46caSbillm 		zdb_dump_gbh(buf, flags);
326944cd46caSbillm 	else
327044cd46caSbillm 		zdb_dump_block(thing, buf, size, flags);
327144cd46caSbillm 
327244cd46caSbillm out:
3273b24ab676SJeff Bonwick 	umem_free(pbuf, SPA_MAXBLOCKSIZE);
3274b24ab676SJeff Bonwick 	umem_free(lbuf, SPA_MAXBLOCKSIZE);
327544cd46caSbillm 	free(dup);
327644cd46caSbillm }
327744cd46caSbillm 
3278de6628f0Sck153898 static boolean_t
32793ad6c7f9SVictor Latushkin pool_match(nvlist_t *cfg, char *tgt)
3280de6628f0Sck153898 {
32813ad6c7f9SVictor Latushkin 	uint64_t v, guid = strtoull(tgt, NULL, 0);
3282de6628f0Sck153898 	char *s;
3283de6628f0Sck153898 
32843ad6c7f9SVictor Latushkin 	if (guid != 0) {
32853ad6c7f9SVictor Latushkin 		if (nvlist_lookup_uint64(cfg, ZPOOL_CONFIG_POOL_GUID, &v) == 0)
32863ad6c7f9SVictor Latushkin 			return (v == guid);
32873ad6c7f9SVictor Latushkin 	} else {
32883ad6c7f9SVictor Latushkin 		if (nvlist_lookup_string(cfg, ZPOOL_CONFIG_POOL_NAME, &s) == 0)
3289de6628f0Sck153898 			return (strcmp(s, tgt) == 0);
3290de6628f0Sck153898 	}
3291de6628f0Sck153898 	return (B_FALSE);
3292de6628f0Sck153898 }
3293de6628f0Sck153898 
32943ad6c7f9SVictor Latushkin static char *
32953ad6c7f9SVictor Latushkin find_zpool(char **target, nvlist_t **configp, int dirc, char **dirv)
3296de6628f0Sck153898 {
3297de6628f0Sck153898 	nvlist_t *pools;
3298de6628f0Sck153898 	nvlist_t *match = NULL;
32993ad6c7f9SVictor Latushkin 	char *name = NULL;
33003ad6c7f9SVictor Latushkin 	char *sepp = NULL;
33013ad6c7f9SVictor Latushkin 	char sep;
33023ad6c7f9SVictor Latushkin 	int count = 0;
3303d41c4376SMark J Musante 	importargs_t args = { 0 };
3304d41c4376SMark J Musante 
3305d41c4376SMark J Musante 	args.paths = dirc;
3306d41c4376SMark J Musante 	args.path = dirv;
3307d41c4376SMark J Musante 	args.can_be_active = B_TRUE;
3308de6628f0Sck153898 
33093ad6c7f9SVictor Latushkin 	if ((sepp = strpbrk(*target, "/@")) != NULL) {
33103ad6c7f9SVictor Latushkin 		sep = *sepp;
33113ad6c7f9SVictor Latushkin 		*sepp = '\0';
33123ad6c7f9SVictor Latushkin 	}
33133ad6c7f9SVictor Latushkin 
3314d41c4376SMark J Musante 	pools = zpool_search_import(g_zfs, &args);
3315de6628f0Sck153898 
3316de6628f0Sck153898 	if (pools != NULL) {
3317de6628f0Sck153898 		nvpair_t *elem = NULL;
3318de6628f0Sck153898 		while ((elem = nvlist_next_nvpair(pools, elem)) != NULL) {
3319de6628f0Sck153898 			verify(nvpair_value_nvlist(elem, configp) == 0);
33203ad6c7f9SVictor Latushkin 			if (pool_match(*configp, *target)) {
33213ad6c7f9SVictor Latushkin 				count++;
3322de6628f0Sck153898 				if (match != NULL) {
33233ad6c7f9SVictor Latushkin 					/* print previously found config */
33243ad6c7f9SVictor Latushkin 					if (name != NULL) {
33253ad6c7f9SVictor Latushkin 						(void) printf("%s\n", name);
33263ad6c7f9SVictor Latushkin 						dump_nvlist(match, 8);
33273ad6c7f9SVictor Latushkin 						name = NULL;
33283ad6c7f9SVictor Latushkin 					}
33293ad6c7f9SVictor Latushkin 					(void) printf("%s\n",
33303ad6c7f9SVictor Latushkin 					    nvpair_name(elem));
33313ad6c7f9SVictor Latushkin 					dump_nvlist(*configp, 8);
3332de6628f0Sck153898 				} else {
3333de6628f0Sck153898 					match = *configp;
33343ad6c7f9SVictor Latushkin 					name = nvpair_name(elem);
3335de6628f0Sck153898 				}
3336de6628f0Sck153898 			}
3337de6628f0Sck153898 		}
3338de6628f0Sck153898 	}
33393ad6c7f9SVictor Latushkin 	if (count > 1)
33403ad6c7f9SVictor Latushkin 		(void) fatal("\tMatched %d pools - use pool GUID "
33413ad6c7f9SVictor Latushkin 		    "instead of pool name or \n"
33423ad6c7f9SVictor Latushkin 		    "\tpool name part of a dataset name to select pool", count);
33433ad6c7f9SVictor Latushkin 
33443ad6c7f9SVictor Latushkin 	if (sepp)
33453ad6c7f9SVictor Latushkin 		*sepp = sep;
33463ad6c7f9SVictor Latushkin 	/*
33473ad6c7f9SVictor Latushkin 	 * If pool GUID was specified for pool id, replace it with pool name
33483ad6c7f9SVictor Latushkin 	 */
33493ad6c7f9SVictor Latushkin 	if (name && (strstr(*target, name) != *target)) {
33503ad6c7f9SVictor Latushkin 		int sz = 1 + strlen(name) + ((sepp) ? strlen(sepp) : 0);
33513ad6c7f9SVictor Latushkin 
33523ad6c7f9SVictor Latushkin 		*target = umem_alloc(sz, UMEM_NOFAIL);
33533ad6c7f9SVictor Latushkin 		(void) snprintf(*target, sz, "%s%s", name, sepp ? sepp : "");
33543ad6c7f9SVictor Latushkin 	}
3355de6628f0Sck153898 
33563ad6c7f9SVictor Latushkin 	*configp = name ? match : NULL;
3357de6628f0Sck153898 
33583ad6c7f9SVictor Latushkin 	return (name);
3359de6628f0Sck153898 }
3360de6628f0Sck153898 
3361fa9e4066Sahrens int
3362fa9e4066Sahrens main(int argc, char **argv)
3363fa9e4066Sahrens {
3364fa9e4066Sahrens 	int i, c;
3365fa9e4066Sahrens 	struct rlimit rl = { 1024, 1024 };
33663ad6c7f9SVictor Latushkin 	spa_t *spa = NULL;
3367fa9e4066Sahrens 	objset_t *os = NULL;
3368fa9e4066Sahrens 	int dump_all = 1;
3369fa9e4066Sahrens 	int verbose = 0;
3370c8ee1847SVictor Latushkin 	int error = 0;
33713ad6c7f9SVictor Latushkin 	char **searchdirs = NULL;
33723ad6c7f9SVictor Latushkin 	int nsearch = 0;
33733ad6c7f9SVictor Latushkin 	char *target;
3374468c413aSTim Haley 	nvlist_t *policy = NULL;
3375468c413aSTim Haley 	uint64_t max_txg = UINT64_MAX;
3376c8ee1847SVictor Latushkin 	int rewind = ZPOOL_NEVER_REWIND;
3377fa9e4066Sahrens 
3378fa9e4066Sahrens 	(void) setrlimit(RLIMIT_NOFILE, &rl);
3379004388ebScasper 	(void) enable_extended_FILE_stdio(-1, -1);
3380fa9e4066Sahrens 
3381fa9e4066Sahrens 	dprintf_setup(&argc, argv);
3382fa9e4066Sahrens 
3383df15e419SMatthew Ahrens 	while ((c = getopt(argc, argv,
33842e4c9986SGeorge Wilson 	    "bcdhilmMI:suCDRSAFLXx:evp:t:U:P")) != -1) {
3385fa9e4066Sahrens 		switch (c) {
3386fa9e4066Sahrens 		case 'b':
3387fa9e4066Sahrens 		case 'c':
3388b24ab676SJeff Bonwick 		case 'd':
3389b24ab676SJeff Bonwick 		case 'h':
3390b24ab676SJeff Bonwick 		case 'i':
3391b24ab676SJeff Bonwick 		case 'l':
3392d6e555bdSGeorge Wilson 		case 'm':
3393fa9e4066Sahrens 		case 's':
3394b24ab676SJeff Bonwick 		case 'u':
3395fa9e4066Sahrens 		case 'C':
3396b24ab676SJeff Bonwick 		case 'D':
33972e4c9986SGeorge Wilson 		case 'M':
339844cd46caSbillm 		case 'R':
3399b24ab676SJeff Bonwick 		case 'S':
3400fa9e4066Sahrens 			dump_opt[c]++;
3401fa9e4066Sahrens 			dump_all = 0;
3402fa9e4066Sahrens 			break;
3403feef89cfSVictor Latushkin 		case 'A':
3404c8ee1847SVictor Latushkin 		case 'F':
340582a0a985SVictor Latushkin 		case 'L':
3406c8ee1847SVictor Latushkin 		case 'X':
34073ad6c7f9SVictor Latushkin 		case 'e':
34083f9d6ad7SLin Ling 		case 'P':
340982a0a985SVictor Latushkin 			dump_opt[c]++;
341082a0a985SVictor Latushkin 			break;
34112e4c9986SGeorge Wilson 		case 'I':
341231d7e8faSGeorge Wilson 			max_inflight = strtoull(optarg, NULL, 0);
341331d7e8faSGeorge Wilson 			if (max_inflight == 0) {
341431d7e8faSGeorge Wilson 				(void) fprintf(stderr, "maximum number "
341531d7e8faSGeorge Wilson 				    "of inflight I/Os must be greater "
341631d7e8faSGeorge Wilson 				    "than 0\n");
341731d7e8faSGeorge Wilson 				usage();
341831d7e8faSGeorge Wilson 			}
341931d7e8faSGeorge Wilson 			break;
3420de6628f0Sck153898 		case 'p':
34213ad6c7f9SVictor Latushkin 			if (searchdirs == NULL) {
34223ad6c7f9SVictor Latushkin 				searchdirs = umem_alloc(sizeof (char *),
34233ad6c7f9SVictor Latushkin 				    UMEM_NOFAIL);
34243ad6c7f9SVictor Latushkin 			} else {
34253ad6c7f9SVictor Latushkin 				char **tmp = umem_alloc((nsearch + 1) *
34263ad6c7f9SVictor Latushkin 				    sizeof (char *), UMEM_NOFAIL);
34273ad6c7f9SVictor Latushkin 				bcopy(searchdirs, tmp, nsearch *
34283ad6c7f9SVictor Latushkin 				    sizeof (char *));
34293ad6c7f9SVictor Latushkin 				umem_free(searchdirs,
34303ad6c7f9SVictor Latushkin 				    nsearch * sizeof (char *));
34313ad6c7f9SVictor Latushkin 				searchdirs = tmp;
34323ad6c7f9SVictor Latushkin 			}
34333ad6c7f9SVictor Latushkin 			searchdirs[nsearch++] = optarg;
3434de6628f0Sck153898 			break;
34352e551927SVictor Latushkin 		case 't':
3436468c413aSTim Haley 			max_txg = strtoull(optarg, NULL, 0);
3437468c413aSTim Haley 			if (max_txg < TXG_INITIAL) {
34382e551927SVictor Latushkin 				(void) fprintf(stderr, "incorrect txg "
34392e551927SVictor Latushkin 				    "specified: %s\n", optarg);
34402e551927SVictor Latushkin 				usage();
34412e551927SVictor Latushkin 			}
34422e551927SVictor Latushkin 			break;
3443b24ab676SJeff Bonwick 		case 'U':
3444b24ab676SJeff Bonwick 			spa_config_path = optarg;
3445b24ab676SJeff Bonwick 			break;
34462e4c9986SGeorge Wilson 		case 'v':
34472e4c9986SGeorge Wilson 			verbose++;
34482e4c9986SGeorge Wilson 			break;
34492e4c9986SGeorge Wilson 		case 'x':
34502e4c9986SGeorge Wilson 			vn_dumpdir = optarg;
34512e4c9986SGeorge Wilson 			break;
3452fa9e4066Sahrens 		default:
3453fa9e4066Sahrens 			usage();
3454fa9e4066Sahrens 			break;
3455fa9e4066Sahrens 		}
3456fa9e4066Sahrens 	}
3457fa9e4066Sahrens 
34583ad6c7f9SVictor Latushkin 	if (!dump_opt['e'] && searchdirs != NULL) {
345988b7b0f2SMatthew Ahrens 		(void) fprintf(stderr, "-p option requires use of -e\n");
346088b7b0f2SMatthew Ahrens 		usage();
346188b7b0f2SMatthew Ahrens 	}
3462de6628f0Sck153898 
3463fa9e4066Sahrens 	kernel_init(FREAD);
3464de6628f0Sck153898 	g_zfs = libzfs_init();
346591ebeef5Sahrens 	ASSERT(g_zfs != NULL);
3466fa9e4066Sahrens 
3467b24ab676SJeff Bonwick 	if (dump_all)
3468b24ab676SJeff Bonwick 		verbose = MAX(verbose, 1);
3469b24ab676SJeff Bonwick 
3470fa9e4066Sahrens 	for (c = 0; c < 256; c++) {
34713f9d6ad7SLin Ling 		if (dump_all && !strchr("elAFLRSXP", c))
3472fa9e4066Sahrens 			dump_opt[c] = 1;
3473fa9e4066Sahrens 		if (dump_opt[c])
3474fa9e4066Sahrens 			dump_opt[c] += verbose;
3475fa9e4066Sahrens 	}
3476fa9e4066Sahrens 
3477feef89cfSVictor Latushkin 	aok = (dump_opt['A'] == 1) || (dump_opt['A'] > 2);
3478feef89cfSVictor Latushkin 	zfs_recover = (dump_opt['A'] > 1);
3479feef89cfSVictor Latushkin 
3480fa9e4066Sahrens 	argc -= optind;
3481fa9e4066Sahrens 	argv += optind;
3482fa9e4066Sahrens 
348307428bdfSVictor Latushkin 	if (argc < 2 && dump_opt['R'])
348407428bdfSVictor Latushkin 		usage();
3485fa9e4066Sahrens 	if (argc < 1) {
34863ad6c7f9SVictor Latushkin 		if (!dump_opt['e'] && dump_opt['C']) {
3487e829d913Sck153898 			dump_cachefile(spa_config_path);
3488fa9e4066Sahrens 			return (0);
3489fa9e4066Sahrens 		}
3490fa9e4066Sahrens 		usage();
3491fa9e4066Sahrens 	}
3492fa9e4066Sahrens 
3493fa9e4066Sahrens 	if (dump_opt['l']) {
3494fa9e4066Sahrens 		dump_label(argv[0]);
3495fa9e4066Sahrens 		return (0);
3496fa9e4066Sahrens 	}
3497fa9e4066Sahrens 
3498c8ee1847SVictor Latushkin 	if (dump_opt['X'] || dump_opt['F'])
3499c8ee1847SVictor Latushkin 		rewind = ZPOOL_DO_REWIND |
3500c8ee1847SVictor Latushkin 		    (dump_opt['X'] ? ZPOOL_EXTREME_REWIND : 0);
3501c8ee1847SVictor Latushkin 
3502c8ee1847SVictor Latushkin 	if (nvlist_alloc(&policy, NV_UNIQUE_NAME_TYPE, 0) != 0 ||
3503c8ee1847SVictor Latushkin 	    nvlist_add_uint64(policy, ZPOOL_REWIND_REQUEST_TXG, max_txg) != 0 ||
3504c8ee1847SVictor Latushkin 	    nvlist_add_uint32(policy, ZPOOL_REWIND_REQUEST, rewind) != 0)
3505c8ee1847SVictor Latushkin 		fatal("internal error: %s", strerror(ENOMEM));
3506c8ee1847SVictor Latushkin 
3507c5904d13Seschrock 	error = 0;
35083ad6c7f9SVictor Latushkin 	target = argv[0];
3509de6628f0Sck153898 
35103ad6c7f9SVictor Latushkin 	if (dump_opt['e']) {
35113ad6c7f9SVictor Latushkin 		nvlist_t *cfg = NULL;
35123ad6c7f9SVictor Latushkin 		char *name = find_zpool(&target, &cfg, nsearch, searchdirs);
3513c5904d13Seschrock 
35143ad6c7f9SVictor Latushkin 		error = ENOENT;
35153ad6c7f9SVictor Latushkin 		if (name) {
351607428bdfSVictor Latushkin 			if (dump_opt['C'] > 1) {
351707428bdfSVictor Latushkin 				(void) printf("\nConfiguration for import:\n");
351807428bdfSVictor Latushkin 				dump_nvlist(cfg, 8);
351907428bdfSVictor Latushkin 			}
3520c8ee1847SVictor Latushkin 			if (nvlist_add_nvlist(cfg,
3521468c413aSTim Haley 			    ZPOOL_REWIND_POLICY, policy) != 0) {
3522468c413aSTim Haley 				fatal("can't open '%s': %s",
3523468c413aSTim Haley 				    target, strerror(ENOMEM));
3524468c413aSTim Haley 			}
35254b964adaSGeorge Wilson 			if ((error = spa_import(name, cfg, NULL,
35264b964adaSGeorge Wilson 			    ZFS_IMPORT_MISSING_LOG)) != 0) {
35274b964adaSGeorge Wilson 				error = spa_import(name, cfg, NULL,
35284b964adaSGeorge Wilson 				    ZFS_IMPORT_VERBATIM);
35294b964adaSGeorge Wilson 			}
3530990b4856Slling 		}
3531c5904d13Seschrock 	}
3532c5904d13Seschrock 
3533c5904d13Seschrock 	if (error == 0) {
353407428bdfSVictor Latushkin 		if (strpbrk(target, "/@") == NULL || dump_opt['R']) {
353580eb36f2SGeorge Wilson 			error = spa_open_rewind(target, &spa, FTAG, policy,
353680eb36f2SGeorge Wilson 			    NULL);
35378f18d1faSGeorge Wilson 			if (error) {
35388f18d1faSGeorge Wilson 				/*
35398f18d1faSGeorge Wilson 				 * If we're missing the log device then
35408f18d1faSGeorge Wilson 				 * try opening the pool after clearing the
35418f18d1faSGeorge Wilson 				 * log state.
35428f18d1faSGeorge Wilson 				 */
35438f18d1faSGeorge Wilson 				mutex_enter(&spa_namespace_lock);
35443ad6c7f9SVictor Latushkin 				if ((spa = spa_lookup(target)) != NULL &&
35458f18d1faSGeorge Wilson 				    spa->spa_log_state == SPA_LOG_MISSING) {
35468f18d1faSGeorge Wilson 					spa->spa_log_state = SPA_LOG_CLEAR;
35478f18d1faSGeorge Wilson 					error = 0;
35488f18d1faSGeorge Wilson 				}
35498f18d1faSGeorge Wilson 				mutex_exit(&spa_namespace_lock);
35508f18d1faSGeorge Wilson 
355180eb36f2SGeorge Wilson 				if (!error) {
355280eb36f2SGeorge Wilson 					error = spa_open_rewind(target, &spa,
355380eb36f2SGeorge Wilson 					    FTAG, policy, NULL);
355480eb36f2SGeorge Wilson 				}
35558f18d1faSGeorge Wilson 			}
355607428bdfSVictor Latushkin 		} else {
355707428bdfSVictor Latushkin 			error = dmu_objset_own(target, DMU_OST_ANY,
355807428bdfSVictor Latushkin 			    B_TRUE, FTAG, &os);
3559c5904d13Seschrock 		}
3560de6628f0Sck153898 	}
356180eb36f2SGeorge Wilson 	nvlist_free(policy);
356280eb36f2SGeorge Wilson 
3563fa9e4066Sahrens 	if (error)
35643ad6c7f9SVictor Latushkin 		fatal("can't open '%s': %s", target, strerror(error));
3565fa9e4066Sahrens 
3566fa9e4066Sahrens 	argv++;
356707428bdfSVictor Latushkin 	argc--;
356807428bdfSVictor Latushkin 	if (!dump_opt['R']) {
356907428bdfSVictor Latushkin 		if (argc > 0) {
3570fa9e4066Sahrens 			zopt_objects = argc;
3571fa9e4066Sahrens 			zopt_object = calloc(zopt_objects, sizeof (uint64_t));
3572fa9e4066Sahrens 			for (i = 0; i < zopt_objects; i++) {
3573fa9e4066Sahrens 				errno = 0;
3574fa9e4066Sahrens 				zopt_object[i] = strtoull(argv[i], NULL, 0);
3575fa9e4066Sahrens 				if (zopt_object[i] == 0 && errno != 0)
357687219db7SVictor Latushkin 					fatal("bad number %s: %s",
3577fa9e4066Sahrens 					    argv[i], strerror(errno));
3578fa9e4066Sahrens 			}
3579fa9e4066Sahrens 		}
3580e690fb27SChristopher Siden 		if (os != NULL) {
3581e690fb27SChristopher Siden 			dump_dir(os);
3582e690fb27SChristopher Siden 		} else if (zopt_objects > 0 && !dump_opt['m']) {
3583e690fb27SChristopher Siden 			dump_dir(spa->spa_meta_objset);
3584e690fb27SChristopher Siden 		} else {
3585e690fb27SChristopher Siden 			dump_zpool(spa);
3586e690fb27SChristopher Siden 		}
3587fa9e4066Sahrens 	} else {
358807428bdfSVictor Latushkin 		flagbits['b'] = ZDB_FLAG_PRINT_BLKPTR;
358907428bdfSVictor Latushkin 		flagbits['c'] = ZDB_FLAG_CHECKSUM;
359007428bdfSVictor Latushkin 		flagbits['d'] = ZDB_FLAG_DECOMPRESS;
359107428bdfSVictor Latushkin 		flagbits['e'] = ZDB_FLAG_BSWAP;
359207428bdfSVictor Latushkin 		flagbits['g'] = ZDB_FLAG_GBH;
359307428bdfSVictor Latushkin 		flagbits['i'] = ZDB_FLAG_INDIRECT;
359407428bdfSVictor Latushkin 		flagbits['p'] = ZDB_FLAG_PHYS;
359507428bdfSVictor Latushkin 		flagbits['r'] = ZDB_FLAG_RAW;
359607428bdfSVictor Latushkin 
359707428bdfSVictor Latushkin 		for (i = 0; i < argc; i++)
359807428bdfSVictor Latushkin 			zdb_read_block(argv[i], spa);
3599fa9e4066Sahrens 	}
3600fa9e4066Sahrens 
360107428bdfSVictor Latushkin 	(os != NULL) ? dmu_objset_disown(os, FTAG) : spa_close(spa, FTAG);
360207428bdfSVictor Latushkin 
3603e0d35c44Smarks 	fuid_table_destroy();
36040a586ceaSMark Shellenbaum 	sa_loaded = B_FALSE;
3605e0d35c44Smarks 
3606de6628f0Sck153898 	libzfs_fini(g_zfs);
3607fa9e4066Sahrens 	kernel_fini();
3608fa9e4066Sahrens 
3609fa9e4066Sahrens 	return (0);
3610fa9e4066Sahrens }
3611