xref: /illumos-gate/usr/src/cmd/zdb/zdb.c (revision 06be9802)
1fa9e4066Sahrens /*
2fa9e4066Sahrens  * CDDL HEADER START
3fa9e4066Sahrens  *
4fa9e4066Sahrens  * The contents of this file are subject to the terms of the
5ea8dc4b6Seschrock  * Common Development and Distribution License (the "License").
6ea8dc4b6Seschrock  * You may not use this file except in compliance with the License.
7fa9e4066Sahrens  *
8fa9e4066Sahrens  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9fa9e4066Sahrens  * or http://www.opensolaris.org/os/licensing.
10fa9e4066Sahrens  * See the License for the specific language governing permissions
11fa9e4066Sahrens  * and limitations under the License.
12fa9e4066Sahrens  *
13fa9e4066Sahrens  * When distributing Covered Code, include this CDDL HEADER in each
14fa9e4066Sahrens  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15fa9e4066Sahrens  * If applicable, add the following below this CDDL HEADER, with the
16fa9e4066Sahrens  * fields enclosed by brackets "[]" replaced with your own identifying
17fa9e4066Sahrens  * information: Portions Copyright [yyyy] [name of copyright owner]
18fa9e4066Sahrens  *
19fa9e4066Sahrens  * CDDL HEADER END
20fa9e4066Sahrens  */
21ad135b5dSChristopher Siden 
22fa9e4066Sahrens /*
238f2529deSMark Shellenbaum  * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
247802d7bfSMatthew Ahrens  * Copyright (c) 2011, 2014 by Delphix. All rights reserved.
25fa9e4066Sahrens  */
26fa9e4066Sahrens 
27fa9e4066Sahrens #include <stdio.h>
28490d05b9SMatthew Ahrens #include <unistd.h>
29004388ebScasper #include <stdio_ext.h>
30fa9e4066Sahrens #include <stdlib.h>
3144cd46caSbillm #include <ctype.h>
32fa9e4066Sahrens #include <sys/zfs_context.h>
33fa9e4066Sahrens #include <sys/spa.h>
34fa9e4066Sahrens #include <sys/spa_impl.h>
35fa9e4066Sahrens #include <sys/dmu.h>
36fa9e4066Sahrens #include <sys/zap.h>
37fa9e4066Sahrens #include <sys/fs/zfs.h>
38fa9e4066Sahrens #include <sys/zfs_znode.h>
390a586ceaSMark Shellenbaum #include <sys/zfs_sa.h>
400a586ceaSMark Shellenbaum #include <sys/sa.h>
410a586ceaSMark Shellenbaum #include <sys/sa_impl.h>
42fa9e4066Sahrens #include <sys/vdev.h>
43fa9e4066Sahrens #include <sys/vdev_impl.h>
44fa9e4066Sahrens #include <sys/metaslab_impl.h>
45fa9e4066Sahrens #include <sys/dmu_objset.h>
46fa9e4066Sahrens #include <sys/dsl_dir.h>
47fa9e4066Sahrens #include <sys/dsl_dataset.h>
48fa9e4066Sahrens #include <sys/dsl_pool.h>
49fa9e4066Sahrens #include <sys/dbuf.h>
50fa9e4066Sahrens #include <sys/zil.h>
51fa9e4066Sahrens #include <sys/zil_impl.h>
52fa9e4066Sahrens #include <sys/stat.h>
53fa9e4066Sahrens #include <sys/resource.h>
54fa9e4066Sahrens #include <sys/dmu_traverse.h>
55fa9e4066Sahrens #include <sys/zio_checksum.h>
56fa9e4066Sahrens #include <sys/zio_compress.h>
57e0d35c44Smarks #include <sys/zfs_fuid.h>
5888b7b0f2SMatthew Ahrens #include <sys/arc.h>
59b24ab676SJeff Bonwick #include <sys/ddt.h>
60ad135b5dSChristopher Siden #include <sys/zfeature.h>
614445fffbSMatthew Ahrens #include <zfs_comutil.h>
62de6628f0Sck #undef ZFS_MAXNAMELEN
63de6628f0Sck #undef verify
64de6628f0Sck #include <libzfs.h>
65fa9e4066Sahrens 
66e690fb27SChristopher Siden #define	ZDB_COMPRESS_NAME(idx) ((idx) < ZIO_COMPRESS_FUNCTIONS ?	\
67e690fb27SChristopher Siden 	zio_compress_table[(idx)].ci_name : "UNKNOWN")
68e690fb27SChristopher Siden #define	ZDB_CHECKSUM_NAME(idx) ((idx) < ZIO_CHECKSUM_FUNCTIONS ?	\
69e690fb27SChristopher Siden 	zio_checksum_table[(idx)].ci_name : "UNKNOWN")
70e690fb27SChristopher Siden #define	ZDB_OT_NAME(idx) ((idx) < DMU_OT_NUMTYPES ?	\
71e690fb27SChristopher Siden 	dmu_ot[(idx)].ot_name : DMU_OT_IS_VALID(idx) ?	\
72e690fb27SChristopher Siden 	dmu_ot_byteswap[DMU_OT_BYTESWAP(idx)].ob_name : "UNKNOWN")
73e690fb27SChristopher Siden #define	ZDB_OT_TYPE(idx) ((idx) < DMU_OT_NUMTYPES ? (idx) :		\
74e690fb27SChristopher Siden 	(((idx) == DMU_OTN_ZAP_DATA || (idx) == DMU_OTN_ZAP_METADATA) ?	\
75e690fb27SChristopher Siden 	DMU_OT_ZAP_OTHER : DMU_OT_NUMTYPES))
766de8f417SVictor Latushkin 
77feef89cfSVictor Latushkin #ifndef lint
787fd05ac4SMatthew Ahrens extern boolean_t zfs_recover;
79*06be9802SMatthew Ahrens extern uint64_t zfs_arc_max, zfs_arc_meta_limit;
80feef89cfSVictor Latushkin #else
817fd05ac4SMatthew Ahrens boolean_t zfs_recover;
82*06be9802SMatthew Ahrens uint64_t zfs_arc_max, zfs_arc_meta_limit;
83feef89cfSVictor Latushkin #endif
84feef89cfSVictor Latushkin 
85fa9e4066Sahrens const char cmdname[] = "zdb";
86fa9e4066Sahrens uint8_t dump_opt[256];
87fa9e4066Sahrens 
88fa9e4066Sahrens typedef void object_viewer_t(objset_t *, uint64_t, void *data, size_t size);
89fa9e4066Sahrens 
90fa9e4066Sahrens extern void dump_intent_log(zilog_t *);
91fa9e4066Sahrens uint64_t *zopt_object = NULL;
92fa9e4066Sahrens int zopt_objects = 0;
93de6628f0Sck libzfs_handle_t *g_zfs;
94*06be9802SMatthew Ahrens uint64_t max_inflight = 1000;
95fa9e4066Sahrens 
96fa9e4066Sahrens /*
97fa9e4066Sahrens  * These libumem hooks provide a reasonable set of defaults for the allocator's
98fa9e4066Sahrens  * debugging facilities.
99fa9e4066Sahrens  */
100fa9e4066Sahrens const char *
101fa9e4066Sahrens _umem_debug_init()
102fa9e4066Sahrens {
103fa9e4066Sahrens 	return ("default,verbose"); /* $UMEM_DEBUG setting */
104fa9e4066Sahrens }
105fa9e4066Sahrens 
106fa9e4066Sahrens const char *
107fa9e4066Sahrens _umem_logging_init(void)
108fa9e4066Sahrens {
109fa9e4066Sahrens 	return ("fail,contents"); /* $UMEM_LOGGING setting */
110fa9e4066Sahrens }
111fa9e4066Sahrens 
112fa9e4066Sahrens static void
113fa9e4066Sahrens usage(void)
114fa9e4066Sahrens {
115fa9e4066Sahrens 	(void) fprintf(stderr,
1162e4c9986SGeorge Wilson 	    "Usage: %s [-CumMdibcsDvhLXFPA] [-t txg] [-e [-p path...]] "
1172e4c9986SGeorge Wilson 	    "[-U config] [-I inflight I/Os] [-x dumpdir] poolname [object...]\n"
11831d7e8faSGeorge Wilson 	    "       %s [-divPA] [-e -p path...] [-U config] dataset "
11931d7e8faSGeorge Wilson 	    "[object...]\n"
1202e4c9986SGeorge Wilson 	    "       %s -mM [-LXFPA] [-t txg] [-e [-p path...]] [-U config] "
12190e894e2SRichard Lowe 	    "poolname [vdev [metaslab...]]\n"
12290e894e2SRichard Lowe 	    "       %s -R [-A] [-e [-p path...]] poolname "
12390e894e2SRichard Lowe 	    "vdev:offset:size[:flags]\n"
12431d7e8faSGeorge Wilson 	    "       %s -S [-PA] [-e [-p path...]] [-U config] poolname\n"
12590e894e2SRichard Lowe 	    "       %s -l [-uA] device\n"
12690e894e2SRichard Lowe 	    "       %s -C [-A] [-U config]\n\n",
127bbfd46c4SJeff Bonwick 	    cmdname, cmdname, cmdname, cmdname, cmdname, cmdname, cmdname);
1283ad6c7f9SVictor Latushkin 
1293ad6c7f9SVictor Latushkin 	(void) fprintf(stderr, "    Dataset name must include at least one "
1303ad6c7f9SVictor Latushkin 	    "separator character '/' or '@'\n");
1313ad6c7f9SVictor Latushkin 	(void) fprintf(stderr, "    If dataset name is specified, only that "
1323ad6c7f9SVictor Latushkin 	    "dataset is dumped\n");
1333ad6c7f9SVictor Latushkin 	(void) fprintf(stderr, "    If object numbers are specified, only "
1343ad6c7f9SVictor Latushkin 	    "those objects are dumped\n\n");
1353ad6c7f9SVictor Latushkin 	(void) fprintf(stderr, "    Options to control amount of output:\n");
1363ad6c7f9SVictor Latushkin 	(void) fprintf(stderr, "        -u uberblock\n");
1373ad6c7f9SVictor Latushkin 	(void) fprintf(stderr, "        -d dataset(s)\n");
1383ad6c7f9SVictor Latushkin 	(void) fprintf(stderr, "        -i intent logs\n");
13907428bdfSVictor Latushkin 	(void) fprintf(stderr, "        -C config (or cachefile if alone)\n");
1408f18d1faSGeorge Wilson 	(void) fprintf(stderr, "        -h pool history\n");
1413ad6c7f9SVictor Latushkin 	(void) fprintf(stderr, "        -b block statistics\n");
1423ad6c7f9SVictor Latushkin 	(void) fprintf(stderr, "        -m metaslabs\n");
1432e4c9986SGeorge Wilson 	(void) fprintf(stderr, "        -M metaslab groups\n");
1443ad6c7f9SVictor Latushkin 	(void) fprintf(stderr, "        -c checksum all metadata (twice for "
1456365109dSVictor Latushkin 	    "all data) blocks\n");
1463ad6c7f9SVictor Latushkin 	(void) fprintf(stderr, "        -s report stats on zdb's I/O\n");
147f0ba89beSJeff Bonwick 	(void) fprintf(stderr, "        -D dedup statistics\n");
148b24ab676SJeff Bonwick 	(void) fprintf(stderr, "        -S simulate dedup to measure effect\n");
1493ad6c7f9SVictor Latushkin 	(void) fprintf(stderr, "        -v verbose (applies to all others)\n");
150fa9e4066Sahrens 	(void) fprintf(stderr, "        -l dump label contents\n");
15182a0a985SVictor Latushkin 	(void) fprintf(stderr, "        -L disable leak tracking (do not "
15282a0a985SVictor Latushkin 	    "load spacemaps)\n");
153d41e7643Sek 	(void) fprintf(stderr, "        -R read and display block from a "
1543ad6c7f9SVictor Latushkin 	    "device\n\n");
1553ad6c7f9SVictor Latushkin 	(void) fprintf(stderr, "    Below options are intended for use "
156df15e419SMatthew Ahrens 	    "with other options:\n");
157feef89cfSVictor Latushkin 	(void) fprintf(stderr, "        -A ignore assertions (-A), enable "
158feef89cfSVictor Latushkin 	    "panic recovery (-AA) or both (-AAA)\n");
159c8ee1847SVictor Latushkin 	(void) fprintf(stderr, "        -F attempt automatic rewind within "
160c8ee1847SVictor Latushkin 	    "safe range of transaction groups\n");
1613ad6c7f9SVictor Latushkin 	(void) fprintf(stderr, "        -U <cachefile_path> -- use alternate "
1623ad6c7f9SVictor Latushkin 	    "cachefile\n");
163c8ee1847SVictor Latushkin 	(void) fprintf(stderr, "        -X attempt extreme rewind (does not "
164c8ee1847SVictor Latushkin 	    "work with dataset)\n");
1653ad6c7f9SVictor Latushkin 	(void) fprintf(stderr, "        -e pool is exported/destroyed/"
1663ad6c7f9SVictor Latushkin 	    "has altroot/not in a cachefile\n");
1673ad6c7f9SVictor Latushkin 	(void) fprintf(stderr, "        -p <path> -- use one or more with "
1683ad6c7f9SVictor Latushkin 	    "-e to specify path to vdev dir\n");
169df15e419SMatthew Ahrens 	(void) fprintf(stderr, "        -x <dumpdir> -- "
170df15e419SMatthew Ahrens 	    "dump all read blocks into specified directory\n");
17190e894e2SRichard Lowe 	(void) fprintf(stderr, "        -P print numbers in parseable form\n");
1723ad6c7f9SVictor Latushkin 	(void) fprintf(stderr, "        -t <txg> -- highest txg to use when "
1732e551927SVictor Latushkin 	    "searching for uberblocks\n");
1742e4c9986SGeorge Wilson 	(void) fprintf(stderr, "        -I <number of inflight I/Os> -- "
175df15e419SMatthew Ahrens 	    "specify the maximum number of "
176df15e419SMatthew Ahrens 	    "checksumming I/Os [default is 200]\n");
177fa9e4066Sahrens 	(void) fprintf(stderr, "Specify an option more than once (e.g. -bb) "
178fa9e4066Sahrens 	    "to make only that option verbose\n");
179fa9e4066Sahrens 	(void) fprintf(stderr, "Default is to dump everything non-verbosely\n");
180fa9e4066Sahrens 	exit(1);
181fa9e4066Sahrens }
182fa9e4066Sahrens 
183ccba0801SRich Morris /*
184ccba0801SRich Morris  * Called for usage errors that are discovered after a call to spa_open(),
185ccba0801SRich Morris  * dmu_bonus_hold(), or pool_match().  abort() is called for other errors.
186ccba0801SRich Morris  */
187ccba0801SRich Morris 
188fa9e4066Sahrens static void
189fa9e4066Sahrens fatal(const char *fmt, ...)
190fa9e4066Sahrens {
191fa9e4066Sahrens 	va_list ap;
192fa9e4066Sahrens 
193fa9e4066Sahrens 	va_start(ap, fmt);
194fa9e4066Sahrens 	(void) fprintf(stderr, "%s: ", cmdname);
195fa9e4066Sahrens 	(void) vfprintf(stderr, fmt, ap);
196fa9e4066Sahrens 	va_end(ap);
197fa9e4066Sahrens 	(void) fprintf(stderr, "\n");
198fa9e4066Sahrens 
199ccba0801SRich Morris 	exit(1);
200fa9e4066Sahrens }
201fa9e4066Sahrens 
202fa9e4066Sahrens /* ARGSUSED */
203fa9e4066Sahrens static void
204fa9e4066Sahrens dump_packed_nvlist(objset_t *os, uint64_t object, void *data, size_t size)
205fa9e4066Sahrens {
206fa9e4066Sahrens 	nvlist_t *nv;
207fa9e4066Sahrens 	size_t nvsize = *(uint64_t *)data;
208fa9e4066Sahrens 	char *packed = umem_alloc(nvsize, UMEM_NOFAIL);
209fa9e4066Sahrens 
2107bfdf011SNeil Perrin 	VERIFY(0 == dmu_read(os, object, 0, nvsize, packed, DMU_READ_PREFETCH));
211fa9e4066Sahrens 
212fa9e4066Sahrens 	VERIFY(nvlist_unpack(packed, nvsize, &nv, 0) == 0);
213fa9e4066Sahrens 
214fa9e4066Sahrens 	umem_free(packed, nvsize);
215fa9e4066Sahrens 
216fa9e4066Sahrens 	dump_nvlist(nv, 8);
217fa9e4066Sahrens 
218fa9e4066Sahrens 	nvlist_free(nv);
219fa9e4066Sahrens }
220fa9e4066Sahrens 
2214445fffbSMatthew Ahrens /* ARGSUSED */
2224445fffbSMatthew Ahrens static void
2234445fffbSMatthew Ahrens dump_history_offsets(objset_t *os, uint64_t object, void *data, size_t size)
2244445fffbSMatthew Ahrens {
2254445fffbSMatthew Ahrens 	spa_history_phys_t *shp = data;
2264445fffbSMatthew Ahrens 
2274445fffbSMatthew Ahrens 	if (shp == NULL)
2284445fffbSMatthew Ahrens 		return;
2294445fffbSMatthew Ahrens 
2304445fffbSMatthew Ahrens 	(void) printf("\t\tpool_create_len = %llu\n",
2314445fffbSMatthew Ahrens 	    (u_longlong_t)shp->sh_pool_create_len);
2324445fffbSMatthew Ahrens 	(void) printf("\t\tphys_max_off = %llu\n",
2334445fffbSMatthew Ahrens 	    (u_longlong_t)shp->sh_phys_max_off);
2344445fffbSMatthew Ahrens 	(void) printf("\t\tbof = %llu\n",
2354445fffbSMatthew Ahrens 	    (u_longlong_t)shp->sh_bof);
2364445fffbSMatthew Ahrens 	(void) printf("\t\teof = %llu\n",
2374445fffbSMatthew Ahrens 	    (u_longlong_t)shp->sh_eof);
2384445fffbSMatthew Ahrens 	(void) printf("\t\trecords_lost = %llu\n",
2394445fffbSMatthew Ahrens 	    (u_longlong_t)shp->sh_records_lost);
2404445fffbSMatthew Ahrens }
2414445fffbSMatthew Ahrens 
2423f9d6ad7SLin Ling static void
2433f9d6ad7SLin Ling zdb_nicenum(uint64_t num, char *buf)
2443f9d6ad7SLin Ling {
2453f9d6ad7SLin Ling 	if (dump_opt['P'])
2463f9d6ad7SLin Ling 		(void) sprintf(buf, "%llu", (longlong_t)num);
2473f9d6ad7SLin Ling 	else
2483f9d6ad7SLin Ling 		nicenum(num, buf);
2493f9d6ad7SLin Ling }
2503f9d6ad7SLin Ling 
251490d05b9SMatthew Ahrens const char histo_stars[] = "****************************************";
252490d05b9SMatthew Ahrens const int histo_width = sizeof (histo_stars) - 1;
253fa9e4066Sahrens 
254fa9e4066Sahrens static void
2550713e232SGeorge Wilson dump_histogram(const uint64_t *histo, int size, int offset)
256fa9e4066Sahrens {
257fa9e4066Sahrens 	int i;
258490d05b9SMatthew Ahrens 	int minidx = size - 1;
259fa9e4066Sahrens 	int maxidx = 0;
260fa9e4066Sahrens 	uint64_t max = 0;
261fa9e4066Sahrens 
262490d05b9SMatthew Ahrens 	for (i = 0; i < size; i++) {
263fa9e4066Sahrens 		if (histo[i] > max)
264fa9e4066Sahrens 			max = histo[i];
265fa9e4066Sahrens 		if (histo[i] > 0 && i > maxidx)
266fa9e4066Sahrens 			maxidx = i;
267fa9e4066Sahrens 		if (histo[i] > 0 && i < minidx)
268fa9e4066Sahrens 			minidx = i;
269fa9e4066Sahrens 	}
270fa9e4066Sahrens 
271490d05b9SMatthew Ahrens 	if (max < histo_width)
272490d05b9SMatthew Ahrens 		max = histo_width;
273fa9e4066Sahrens 
274490d05b9SMatthew Ahrens 	for (i = minidx; i <= maxidx; i++) {
275490d05b9SMatthew Ahrens 		(void) printf("\t\t\t%3u: %6llu %s\n",
2760713e232SGeorge Wilson 		    i + offset, (u_longlong_t)histo[i],
277490d05b9SMatthew Ahrens 		    &histo_stars[(max - histo[i]) * histo_width / max]);
278490d05b9SMatthew Ahrens 	}
279fa9e4066Sahrens }
280fa9e4066Sahrens 
281fa9e4066Sahrens static void
282fa9e4066Sahrens dump_zap_stats(objset_t *os, uint64_t object)
283fa9e4066Sahrens {
284fa9e4066Sahrens 	int error;
285fa9e4066Sahrens 	zap_stats_t zs;
286fa9e4066Sahrens 
287fa9e4066Sahrens 	error = zap_get_stats(os, object, &zs);
288fa9e4066Sahrens 	if (error)
289fa9e4066Sahrens 		return;
290fa9e4066Sahrens 
291fa9e4066Sahrens 	if (zs.zs_ptrtbl_len == 0) {
292fa9e4066Sahrens 		ASSERT(zs.zs_num_blocks == 1);
293fa9e4066Sahrens 		(void) printf("\tmicrozap: %llu bytes, %llu entries\n",
294fa9e4066Sahrens 		    (u_longlong_t)zs.zs_blocksize,
295fa9e4066Sahrens 		    (u_longlong_t)zs.zs_num_entries);
296fa9e4066Sahrens 		return;
297fa9e4066Sahrens 	}
298fa9e4066Sahrens 
299fa9e4066Sahrens 	(void) printf("\tFat ZAP stats:\n");
3008248818dSnd 
3018248818dSnd 	(void) printf("\t\tPointer table:\n");
3028248818dSnd 	(void) printf("\t\t\t%llu elements\n",
303fa9e4066Sahrens 	    (u_longlong_t)zs.zs_ptrtbl_len);
3048248818dSnd 	(void) printf("\t\t\tzt_blk: %llu\n",
3058248818dSnd 	    (u_longlong_t)zs.zs_ptrtbl_zt_blk);
3068248818dSnd 	(void) printf("\t\t\tzt_numblks: %llu\n",
3078248818dSnd 	    (u_longlong_t)zs.zs_ptrtbl_zt_numblks);
3088248818dSnd 	(void) printf("\t\t\tzt_shift: %llu\n",
3098248818dSnd 	    (u_longlong_t)zs.zs_ptrtbl_zt_shift);
3108248818dSnd 	(void) printf("\t\t\tzt_blks_copied: %llu\n",
3118248818dSnd 	    (u_longlong_t)zs.zs_ptrtbl_blks_copied);
3128248818dSnd 	(void) printf("\t\t\tzt_nextblk: %llu\n",
3138248818dSnd 	    (u_longlong_t)zs.zs_ptrtbl_nextblk);
3148248818dSnd 
315fa9e4066Sahrens 	(void) printf("\t\tZAP entries: %llu\n",
316fa9e4066Sahrens 	    (u_longlong_t)zs.zs_num_entries);
317fa9e4066Sahrens 	(void) printf("\t\tLeaf blocks: %llu\n",
318fa9e4066Sahrens 	    (u_longlong_t)zs.zs_num_leafs);
319fa9e4066Sahrens 	(void) printf("\t\tTotal blocks: %llu\n",
320fa9e4066Sahrens 	    (u_longlong_t)zs.zs_num_blocks);
3218248818dSnd 	(void) printf("\t\tzap_block_type: 0x%llx\n",
3228248818dSnd 	    (u_longlong_t)zs.zs_block_type);
3238248818dSnd 	(void) printf("\t\tzap_magic: 0x%llx\n",
3248248818dSnd 	    (u_longlong_t)zs.zs_magic);
3258248818dSnd 	(void) printf("\t\tzap_salt: 0x%llx\n",
3268248818dSnd 	    (u_longlong_t)zs.zs_salt);
327fa9e4066Sahrens 
328fa9e4066Sahrens 	(void) printf("\t\tLeafs with 2^n pointers:\n");
3290713e232SGeorge Wilson 	dump_histogram(zs.zs_leafs_with_2n_pointers, ZAP_HISTOGRAM_SIZE, 0);
330fa9e4066Sahrens 
331fa9e4066Sahrens 	(void) printf("\t\tBlocks with n*5 entries:\n");
3320713e232SGeorge Wilson 	dump_histogram(zs.zs_blocks_with_n5_entries, ZAP_HISTOGRAM_SIZE, 0);
333fa9e4066Sahrens 
334fa9e4066Sahrens 	(void) printf("\t\tBlocks n/10 full:\n");
3350713e232SGeorge Wilson 	dump_histogram(zs.zs_blocks_n_tenths_full, ZAP_HISTOGRAM_SIZE, 0);
336fa9e4066Sahrens 
337fa9e4066Sahrens 	(void) printf("\t\tEntries with n chunks:\n");
3380713e232SGeorge Wilson 	dump_histogram(zs.zs_entries_using_n_chunks, ZAP_HISTOGRAM_SIZE, 0);
339fa9e4066Sahrens 
340fa9e4066Sahrens 	(void) printf("\t\tBuckets with n entries:\n");
3410713e232SGeorge Wilson 	dump_histogram(zs.zs_buckets_with_n_entries, ZAP_HISTOGRAM_SIZE, 0);
342fa9e4066Sahrens }
343fa9e4066Sahrens 
344fa9e4066Sahrens /*ARGSUSED*/
345fa9e4066Sahrens static void
346fa9e4066Sahrens dump_none(objset_t *os, uint64_t object, void *data, size_t size)
347fa9e4066Sahrens {
348fa9e4066Sahrens }
349fa9e4066Sahrens 
3506de8f417SVictor Latushkin /*ARGSUSED*/
3516de8f417SVictor Latushkin static void
3526de8f417SVictor Latushkin dump_unknown(objset_t *os, uint64_t object, void *data, size_t size)
3536de8f417SVictor Latushkin {
3546de8f417SVictor Latushkin 	(void) printf("\tUNKNOWN OBJECT TYPE\n");
3556de8f417SVictor Latushkin }
3566de8f417SVictor Latushkin 
357fa9e4066Sahrens /*ARGSUSED*/
358fa9e4066Sahrens void
359fa9e4066Sahrens dump_uint8(objset_t *os, uint64_t object, void *data, size_t size)
360fa9e4066Sahrens {
361fa9e4066Sahrens }
362fa9e4066Sahrens 
363fa9e4066Sahrens /*ARGSUSED*/
364fa9e4066Sahrens static void
365fa9e4066Sahrens dump_uint64(objset_t *os, uint64_t object, void *data, size_t size)
366fa9e4066Sahrens {
367fa9e4066Sahrens }
368fa9e4066Sahrens 
369fa9e4066Sahrens /*ARGSUSED*/
370fa9e4066Sahrens static void
371fa9e4066Sahrens dump_zap(objset_t *os, uint64_t object, void *data, size_t size)
372fa9e4066Sahrens {
373fa9e4066Sahrens 	zap_cursor_t zc;
374fa9e4066Sahrens 	zap_attribute_t attr;
375fa9e4066Sahrens 	void *prop;
376fa9e4066Sahrens 	int i;
377fa9e4066Sahrens 
378fa9e4066Sahrens 	dump_zap_stats(os, object);
379fa9e4066Sahrens 	(void) printf("\n");
380fa9e4066Sahrens 
381fa9e4066Sahrens 	for (zap_cursor_init(&zc, os, object);
382fa9e4066Sahrens 	    zap_cursor_retrieve(&zc, &attr) == 0;
383fa9e4066Sahrens 	    zap_cursor_advance(&zc)) {
384fa9e4066Sahrens 		(void) printf("\t\t%s = ", attr.za_name);
385fa9e4066Sahrens 		if (attr.za_num_integers == 0) {
386fa9e4066Sahrens 			(void) printf("\n");
387fa9e4066Sahrens 			continue;
388fa9e4066Sahrens 		}
389fa9e4066Sahrens 		prop = umem_zalloc(attr.za_num_integers *
390fa9e4066Sahrens 		    attr.za_integer_length, UMEM_NOFAIL);
391fa9e4066Sahrens 		(void) zap_lookup(os, object, attr.za_name,
392fa9e4066Sahrens 		    attr.za_integer_length, attr.za_num_integers, prop);
393fa9e4066Sahrens 		if (attr.za_integer_length == 1) {
394fa9e4066Sahrens 			(void) printf("%s", (char *)prop);
395fa9e4066Sahrens 		} else {
396fa9e4066Sahrens 			for (i = 0; i < attr.za_num_integers; i++) {
397fa9e4066Sahrens 				switch (attr.za_integer_length) {
398fa9e4066Sahrens 				case 2:
399fa9e4066Sahrens 					(void) printf("%u ",
400fa9e4066Sahrens 					    ((uint16_t *)prop)[i]);
401fa9e4066Sahrens 					break;
402fa9e4066Sahrens 				case 4:
403fa9e4066Sahrens 					(void) printf("%u ",
404fa9e4066Sahrens 					    ((uint32_t *)prop)[i]);
405fa9e4066Sahrens 					break;
406fa9e4066Sahrens 				case 8:
407fa9e4066Sahrens 					(void) printf("%lld ",
408fa9e4066Sahrens 					    (u_longlong_t)((int64_t *)prop)[i]);
409fa9e4066Sahrens 					break;
410fa9e4066Sahrens 				}
411fa9e4066Sahrens 			}
412fa9e4066Sahrens 		}
413fa9e4066Sahrens 		(void) printf("\n");
414fa9e4066Sahrens 		umem_free(prop, attr.za_num_integers * attr.za_integer_length);
415fa9e4066Sahrens 	}
41687e5029aSahrens 	zap_cursor_fini(&zc);
417fa9e4066Sahrens }
418fa9e4066Sahrens 
419486ae710SMatthew Ahrens /*ARGSUSED*/
420486ae710SMatthew Ahrens static void
421486ae710SMatthew Ahrens dump_ddt_zap(objset_t *os, uint64_t object, void *data, size_t size)
422486ae710SMatthew Ahrens {
423486ae710SMatthew Ahrens 	dump_zap_stats(os, object);
424486ae710SMatthew Ahrens 	/* contents are printed elsewhere, properly decoded */
425486ae710SMatthew Ahrens }
426486ae710SMatthew Ahrens 
4270a586ceaSMark Shellenbaum /*ARGSUSED*/
4280a586ceaSMark Shellenbaum static void
4290a586ceaSMark Shellenbaum dump_sa_attrs(objset_t *os, uint64_t object, void *data, size_t size)
4300a586ceaSMark Shellenbaum {
4310a586ceaSMark Shellenbaum 	zap_cursor_t zc;
4320a586ceaSMark Shellenbaum 	zap_attribute_t attr;
4330a586ceaSMark Shellenbaum 
4340a586ceaSMark Shellenbaum 	dump_zap_stats(os, object);
4350a586ceaSMark Shellenbaum 	(void) printf("\n");
4360a586ceaSMark Shellenbaum 
4370a586ceaSMark Shellenbaum 	for (zap_cursor_init(&zc, os, object);
4380a586ceaSMark Shellenbaum 	    zap_cursor_retrieve(&zc, &attr) == 0;
4390a586ceaSMark Shellenbaum 	    zap_cursor_advance(&zc)) {
4400a586ceaSMark Shellenbaum 		(void) printf("\t\t%s = ", attr.za_name);
4410a586ceaSMark Shellenbaum 		if (attr.za_num_integers == 0) {
4420a586ceaSMark Shellenbaum 			(void) printf("\n");
4430a586ceaSMark Shellenbaum 			continue;
4440a586ceaSMark Shellenbaum 		}
4450a586ceaSMark Shellenbaum 		(void) printf(" %llx : [%d:%d:%d]\n",
4460a586ceaSMark Shellenbaum 		    (u_longlong_t)attr.za_first_integer,
4470a586ceaSMark Shellenbaum 		    (int)ATTR_LENGTH(attr.za_first_integer),
4480a586ceaSMark Shellenbaum 		    (int)ATTR_BSWAP(attr.za_first_integer),
4490a586ceaSMark Shellenbaum 		    (int)ATTR_NUM(attr.za_first_integer));
4500a586ceaSMark Shellenbaum 	}
4510a586ceaSMark Shellenbaum 	zap_cursor_fini(&zc);
4520a586ceaSMark Shellenbaum }
4530a586ceaSMark Shellenbaum 
4540a586ceaSMark Shellenbaum /*ARGSUSED*/
4550a586ceaSMark Shellenbaum static void
4560a586ceaSMark Shellenbaum dump_sa_layouts(objset_t *os, uint64_t object, void *data, size_t size)
4570a586ceaSMark Shellenbaum {
4580a586ceaSMark Shellenbaum 	zap_cursor_t zc;
4590a586ceaSMark Shellenbaum 	zap_attribute_t attr;
4600a586ceaSMark Shellenbaum 	uint16_t *layout_attrs;
4610a586ceaSMark Shellenbaum 	int i;
4620a586ceaSMark Shellenbaum 
4630a586ceaSMark Shellenbaum 	dump_zap_stats(os, object);
4640a586ceaSMark Shellenbaum 	(void) printf("\n");
4650a586ceaSMark Shellenbaum 
4660a586ceaSMark Shellenbaum 	for (zap_cursor_init(&zc, os, object);
4670a586ceaSMark Shellenbaum 	    zap_cursor_retrieve(&zc, &attr) == 0;
4680a586ceaSMark Shellenbaum 	    zap_cursor_advance(&zc)) {
4690a586ceaSMark Shellenbaum 		(void) printf("\t\t%s = [", attr.za_name);
4700a586ceaSMark Shellenbaum 		if (attr.za_num_integers == 0) {
4710a586ceaSMark Shellenbaum 			(void) printf("\n");
4720a586ceaSMark Shellenbaum 			continue;
4730a586ceaSMark Shellenbaum 		}
4740a586ceaSMark Shellenbaum 
4750a586ceaSMark Shellenbaum 		VERIFY(attr.za_integer_length == 2);
4760a586ceaSMark Shellenbaum 		layout_attrs = umem_zalloc(attr.za_num_integers *
4770a586ceaSMark Shellenbaum 		    attr.za_integer_length, UMEM_NOFAIL);
4780a586ceaSMark Shellenbaum 
4790a586ceaSMark Shellenbaum 		VERIFY(zap_lookup(os, object, attr.za_name,
4800a586ceaSMark Shellenbaum 		    attr.za_integer_length,
4810a586ceaSMark Shellenbaum 		    attr.za_num_integers, layout_attrs) == 0);
4820a586ceaSMark Shellenbaum 
4830a586ceaSMark Shellenbaum 		for (i = 0; i != attr.za_num_integers; i++)
4840a586ceaSMark Shellenbaum 			(void) printf(" %d ", (int)layout_attrs[i]);
4850a586ceaSMark Shellenbaum 		(void) printf("]\n");
4860a586ceaSMark Shellenbaum 		umem_free(layout_attrs,
4870a586ceaSMark Shellenbaum 		    attr.za_num_integers * attr.za_integer_length);
4880a586ceaSMark Shellenbaum 	}
4890a586ceaSMark Shellenbaum 	zap_cursor_fini(&zc);
4900a586ceaSMark Shellenbaum }
4910a586ceaSMark Shellenbaum 
492e7437265Sahrens /*ARGSUSED*/
493e7437265Sahrens static void
494e7437265Sahrens dump_zpldir(objset_t *os, uint64_t object, void *data, size_t size)
495e7437265Sahrens {
496e7437265Sahrens 	zap_cursor_t zc;
497e7437265Sahrens 	zap_attribute_t attr;
498e7437265Sahrens 	const char *typenames[] = {
499e7437265Sahrens 		/* 0 */ "not specified",
500e7437265Sahrens 		/* 1 */ "FIFO",
501e7437265Sahrens 		/* 2 */ "Character Device",
502e7437265Sahrens 		/* 3 */ "3 (invalid)",
503e7437265Sahrens 		/* 4 */ "Directory",
504e7437265Sahrens 		/* 5 */ "5 (invalid)",
505e7437265Sahrens 		/* 6 */ "Block Device",
506e7437265Sahrens 		/* 7 */ "7 (invalid)",
507e7437265Sahrens 		/* 8 */ "Regular File",
508e7437265Sahrens 		/* 9 */ "9 (invalid)",
509e7437265Sahrens 		/* 10 */ "Symbolic Link",
510e7437265Sahrens 		/* 11 */ "11 (invalid)",
511e7437265Sahrens 		/* 12 */ "Socket",
512e7437265Sahrens 		/* 13 */ "Door",
513e7437265Sahrens 		/* 14 */ "Event Port",
514e7437265Sahrens 		/* 15 */ "15 (invalid)",
515e7437265Sahrens 	};
516e7437265Sahrens 
517e7437265Sahrens 	dump_zap_stats(os, object);
518e7437265Sahrens 	(void) printf("\n");
519e7437265Sahrens 
520e7437265Sahrens 	for (zap_cursor_init(&zc, os, object);
521e7437265Sahrens 	    zap_cursor_retrieve(&zc, &attr) == 0;
522e7437265Sahrens 	    zap_cursor_advance(&zc)) {
523e7437265Sahrens 		(void) printf("\t\t%s = %lld (type: %s)\n",
524e7437265Sahrens 		    attr.za_name, ZFS_DIRENT_OBJ(attr.za_first_integer),
525e7437265Sahrens 		    typenames[ZFS_DIRENT_TYPE(attr.za_first_integer)]);
526e7437265Sahrens 	}
527e7437265Sahrens 	zap_cursor_fini(&zc);
528e7437265Sahrens }
529e7437265Sahrens 
5300713e232SGeorge Wilson int
5310713e232SGeorge Wilson get_dtl_refcount(vdev_t *vd)
5320713e232SGeorge Wilson {
5330713e232SGeorge Wilson 	int refcount = 0;
5340713e232SGeorge Wilson 
5350713e232SGeorge Wilson 	if (vd->vdev_ops->vdev_op_leaf) {
5360713e232SGeorge Wilson 		space_map_t *sm = vd->vdev_dtl_sm;
5370713e232SGeorge Wilson 
5380713e232SGeorge Wilson 		if (sm != NULL &&
5390713e232SGeorge Wilson 		    sm->sm_dbuf->db_size == sizeof (space_map_phys_t))
5400713e232SGeorge Wilson 			return (1);
5410713e232SGeorge Wilson 		return (0);
5420713e232SGeorge Wilson 	}
5430713e232SGeorge Wilson 
5440713e232SGeorge Wilson 	for (int c = 0; c < vd->vdev_children; c++)
5450713e232SGeorge Wilson 		refcount += get_dtl_refcount(vd->vdev_child[c]);
5460713e232SGeorge Wilson 	return (refcount);
5470713e232SGeorge Wilson }
5480713e232SGeorge Wilson 
5490713e232SGeorge Wilson int
5500713e232SGeorge Wilson get_metaslab_refcount(vdev_t *vd)
5510713e232SGeorge Wilson {
5520713e232SGeorge Wilson 	int refcount = 0;
5530713e232SGeorge Wilson 
5542e4c9986SGeorge Wilson 	if (vd->vdev_top == vd && !vd->vdev_removing) {
5550713e232SGeorge Wilson 		for (int m = 0; m < vd->vdev_ms_count; m++) {
5560713e232SGeorge Wilson 			space_map_t *sm = vd->vdev_ms[m]->ms_sm;
5570713e232SGeorge Wilson 
5580713e232SGeorge Wilson 			if (sm != NULL &&
5590713e232SGeorge Wilson 			    sm->sm_dbuf->db_size == sizeof (space_map_phys_t))
5600713e232SGeorge Wilson 				refcount++;
5610713e232SGeorge Wilson 		}
5620713e232SGeorge Wilson 	}
5630713e232SGeorge Wilson 	for (int c = 0; c < vd->vdev_children; c++)
5640713e232SGeorge Wilson 		refcount += get_metaslab_refcount(vd->vdev_child[c]);
5650713e232SGeorge Wilson 
5660713e232SGeorge Wilson 	return (refcount);
5670713e232SGeorge Wilson }
5680713e232SGeorge Wilson 
5690713e232SGeorge Wilson static int
5700713e232SGeorge Wilson verify_spacemap_refcounts(spa_t *spa)
5710713e232SGeorge Wilson {
5722acef22dSMatthew Ahrens 	uint64_t expected_refcount = 0;
5732acef22dSMatthew Ahrens 	uint64_t actual_refcount;
5740713e232SGeorge Wilson 
5752acef22dSMatthew Ahrens 	(void) feature_get_refcount(spa,
5762acef22dSMatthew Ahrens 	    &spa_feature_table[SPA_FEATURE_SPACEMAP_HISTOGRAM],
5772acef22dSMatthew Ahrens 	    &expected_refcount);
5780713e232SGeorge Wilson 	actual_refcount = get_dtl_refcount(spa->spa_root_vdev);
5790713e232SGeorge Wilson 	actual_refcount += get_metaslab_refcount(spa->spa_root_vdev);
5800713e232SGeorge Wilson 
5810713e232SGeorge Wilson 	if (expected_refcount != actual_refcount) {
5822acef22dSMatthew Ahrens 		(void) printf("space map refcount mismatch: expected %lld != "
5832acef22dSMatthew Ahrens 		    "actual %lld\n",
5842acef22dSMatthew Ahrens 		    (longlong_t)expected_refcount,
5852acef22dSMatthew Ahrens 		    (longlong_t)actual_refcount);
5860713e232SGeorge Wilson 		return (2);
5870713e232SGeorge Wilson 	}
5880713e232SGeorge Wilson 	return (0);
5890713e232SGeorge Wilson }
5900713e232SGeorge Wilson 
591fa9e4066Sahrens static void
5920713e232SGeorge Wilson dump_spacemap(objset_t *os, space_map_t *sm)
593fa9e4066Sahrens {
594fa9e4066Sahrens 	uint64_t alloc, offset, entry;
5958053a263Sck 	char *ddata[] = { "ALLOC", "FREE", "CONDENSE", "INVALID",
5968053a263Sck 			    "INVALID", "INVALID", "INVALID", "INVALID" };
597fa9e4066Sahrens 
5980713e232SGeorge Wilson 	if (sm == NULL)
599fa9e4066Sahrens 		return;
600fa9e4066Sahrens 
601fa9e4066Sahrens 	/*
602fa9e4066Sahrens 	 * Print out the freelist entries in both encoded and decoded form.
603fa9e4066Sahrens 	 */
604fa9e4066Sahrens 	alloc = 0;
6050713e232SGeorge Wilson 	for (offset = 0; offset < space_map_length(sm);
6060713e232SGeorge Wilson 	    offset += sizeof (entry)) {
6070713e232SGeorge Wilson 		uint8_t mapshift = sm->sm_shift;
6080713e232SGeorge Wilson 
6090713e232SGeorge Wilson 		VERIFY0(dmu_read(os, space_map_object(sm), offset,
6107bfdf011SNeil Perrin 		    sizeof (entry), &entry, DMU_READ_PREFETCH));
611fa9e4066Sahrens 		if (SM_DEBUG_DECODE(entry)) {
6120713e232SGeorge Wilson 
61387219db7SVictor Latushkin 			(void) printf("\t    [%6llu] %s: txg %llu, pass %llu\n",
614fa9e4066Sahrens 			    (u_longlong_t)(offset / sizeof (entry)),
615fa9e4066Sahrens 			    ddata[SM_DEBUG_ACTION_DECODE(entry)],
6165ad82045Snd 			    (u_longlong_t)SM_DEBUG_TXG_DECODE(entry),
6175ad82045Snd 			    (u_longlong_t)SM_DEBUG_SYNCPASS_DECODE(entry));
618fa9e4066Sahrens 		} else {
61987219db7SVictor Latushkin 			(void) printf("\t    [%6llu]    %c  range:"
62087219db7SVictor Latushkin 			    " %010llx-%010llx  size: %06llx\n",
621fa9e4066Sahrens 			    (u_longlong_t)(offset / sizeof (entry)),
622fa9e4066Sahrens 			    SM_TYPE_DECODE(entry) == SM_ALLOC ? 'A' : 'F',
6235ad82045Snd 			    (u_longlong_t)((SM_OFFSET_DECODE(entry) <<
6240713e232SGeorge Wilson 			    mapshift) + sm->sm_start),
6255ad82045Snd 			    (u_longlong_t)((SM_OFFSET_DECODE(entry) <<
6260713e232SGeorge Wilson 			    mapshift) + sm->sm_start +
6270713e232SGeorge Wilson 			    (SM_RUN_DECODE(entry) << mapshift)),
6285ad82045Snd 			    (u_longlong_t)(SM_RUN_DECODE(entry) << mapshift));
629fa9e4066Sahrens 			if (SM_TYPE_DECODE(entry) == SM_ALLOC)
630fa9e4066Sahrens 				alloc += SM_RUN_DECODE(entry) << mapshift;
631fa9e4066Sahrens 			else
632fa9e4066Sahrens 				alloc -= SM_RUN_DECODE(entry) << mapshift;
633fa9e4066Sahrens 		}
634fa9e4066Sahrens 	}
6350713e232SGeorge Wilson 	if (alloc != space_map_allocated(sm)) {
636fa9e4066Sahrens 		(void) printf("space_map_object alloc (%llu) INCONSISTENT "
637fa9e4066Sahrens 		    "with space map summary (%llu)\n",
6380713e232SGeorge Wilson 		    (u_longlong_t)space_map_allocated(sm), (u_longlong_t)alloc);
639fa9e4066Sahrens 	}
640fa9e4066Sahrens }
641fa9e4066Sahrens 
642d6e555bdSGeorge Wilson static void
643d6e555bdSGeorge Wilson dump_metaslab_stats(metaslab_t *msp)
644d6e555bdSGeorge Wilson {
6453f9d6ad7SLin Ling 	char maxbuf[32];
6460713e232SGeorge Wilson 	range_tree_t *rt = msp->ms_tree;
6470713e232SGeorge Wilson 	avl_tree_t *t = &msp->ms_size_tree;
6480713e232SGeorge Wilson 	int free_pct = range_tree_space(rt) * 100 / msp->ms_size;
649d6e555bdSGeorge Wilson 
6500713e232SGeorge Wilson 	zdb_nicenum(metaslab_block_maxsize(msp), maxbuf);
651d6e555bdSGeorge Wilson 
65287219db7SVictor Latushkin 	(void) printf("\t %25s %10lu   %7s  %6s   %4s %4d%%\n",
653d6e555bdSGeorge Wilson 	    "segments", avl_numnodes(t), "maxsize", maxbuf,
654d6e555bdSGeorge Wilson 	    "freepct", free_pct);
6550713e232SGeorge Wilson 	(void) printf("\tIn-memory histogram:\n");
6560713e232SGeorge Wilson 	dump_histogram(rt->rt_histogram, RANGE_TREE_HISTOGRAM_SIZE, 0);
657d6e555bdSGeorge Wilson }
658d6e555bdSGeorge Wilson 
659fa9e4066Sahrens static void
660fa9e4066Sahrens dump_metaslab(metaslab_t *msp)
661fa9e4066Sahrens {
662fa9e4066Sahrens 	vdev_t *vd = msp->ms_group->mg_vd;
663fa9e4066Sahrens 	spa_t *spa = vd->vdev_spa;
6640713e232SGeorge Wilson 	space_map_t *sm = msp->ms_sm;
6653f9d6ad7SLin Ling 	char freebuf[32];
666fa9e4066Sahrens 
6670713e232SGeorge Wilson 	zdb_nicenum(msp->ms_size - space_map_allocated(sm), freebuf);
668fa9e4066Sahrens 
669fa9e4066Sahrens 	(void) printf(
67087219db7SVictor Latushkin 	    "\tmetaslab %6llu   offset %12llx   spacemap %6llu   free    %5s\n",
6710713e232SGeorge Wilson 	    (u_longlong_t)msp->ms_id, (u_longlong_t)msp->ms_start,
6720713e232SGeorge Wilson 	    (u_longlong_t)space_map_object(sm), freebuf);
673fa9e4066Sahrens 
6740713e232SGeorge Wilson 	if (dump_opt['m'] > 2 && !dump_opt['L']) {
675d6e555bdSGeorge Wilson 		mutex_enter(&msp->ms_lock);
6760713e232SGeorge Wilson 		metaslab_load_wait(msp);
6770713e232SGeorge Wilson 		if (!msp->ms_loaded) {
6780713e232SGeorge Wilson 			VERIFY0(metaslab_load(msp));
6790713e232SGeorge Wilson 			range_tree_stat_verify(msp->ms_tree);
6800713e232SGeorge Wilson 		}
681bc3975b5SVictor Latushkin 		dump_metaslab_stats(msp);
6820713e232SGeorge Wilson 		metaslab_unload(msp);
683d6e555bdSGeorge Wilson 		mutex_exit(&msp->ms_lock);
684d6e555bdSGeorge Wilson 	}
685d6e555bdSGeorge Wilson 
6860713e232SGeorge Wilson 	if (dump_opt['m'] > 1 && sm != NULL &&
6872acef22dSMatthew Ahrens 	    spa_feature_is_active(spa, SPA_FEATURE_SPACEMAP_HISTOGRAM)) {
6880713e232SGeorge Wilson 		/*
6890713e232SGeorge Wilson 		 * The space map histogram represents free space in chunks
6900713e232SGeorge Wilson 		 * of sm_shift (i.e. bucket 0 refers to 2^sm_shift).
6910713e232SGeorge Wilson 		 */
6922e4c9986SGeorge Wilson 		(void) printf("\tOn-disk histogram:\t\tfragmentation %llu\n",
6932e4c9986SGeorge Wilson 		    (u_longlong_t)msp->ms_fragmentation);
6940713e232SGeorge Wilson 		dump_histogram(sm->sm_phys->smp_histogram,
6952e4c9986SGeorge Wilson 		    SPACE_MAP_HISTOGRAM_SIZE, sm->sm_shift);
6960713e232SGeorge Wilson 	}
6970713e232SGeorge Wilson 
6980713e232SGeorge Wilson 	if (dump_opt['d'] > 5 || dump_opt['m'] > 3) {
6990713e232SGeorge Wilson 		ASSERT(msp->ms_size == (1ULL << vd->vdev_ms_shift));
700d6e555bdSGeorge Wilson 
701d6e555bdSGeorge Wilson 		mutex_enter(&msp->ms_lock);
7020713e232SGeorge Wilson 		dump_spacemap(spa->spa_meta_objset, msp->ms_sm);
703d6e555bdSGeorge Wilson 		mutex_exit(&msp->ms_lock);
704d6e555bdSGeorge Wilson 	}
70587219db7SVictor Latushkin }
706fa9e4066Sahrens 
70787219db7SVictor Latushkin static void
70887219db7SVictor Latushkin print_vdev_metaslab_header(vdev_t *vd)
70987219db7SVictor Latushkin {
71087219db7SVictor Latushkin 	(void) printf("\tvdev %10llu\n\t%-10s%5llu   %-19s   %-15s   %-10s\n",
71187219db7SVictor Latushkin 	    (u_longlong_t)vd->vdev_id,
71287219db7SVictor Latushkin 	    "metaslabs", (u_longlong_t)vd->vdev_ms_count,
71387219db7SVictor Latushkin 	    "offset", "spacemap", "free");
71487219db7SVictor Latushkin 	(void) printf("\t%15s   %19s   %15s   %10s\n",
71587219db7SVictor Latushkin 	    "---------------", "-------------------",
71687219db7SVictor Latushkin 	    "---------------", "-------------");
717fa9e4066Sahrens }
718fa9e4066Sahrens 
7192e4c9986SGeorge Wilson static void
7202e4c9986SGeorge Wilson dump_metaslab_groups(spa_t *spa)
7212e4c9986SGeorge Wilson {
7222e4c9986SGeorge Wilson 	vdev_t *rvd = spa->spa_root_vdev;
7232e4c9986SGeorge Wilson 	metaslab_class_t *mc = spa_normal_class(spa);
7242e4c9986SGeorge Wilson 	uint64_t fragmentation;
7252e4c9986SGeorge Wilson 
7262e4c9986SGeorge Wilson 	metaslab_class_histogram_verify(mc);
7272e4c9986SGeorge Wilson 
7282e4c9986SGeorge Wilson 	for (int c = 0; c < rvd->vdev_children; c++) {
7292e4c9986SGeorge Wilson 		vdev_t *tvd = rvd->vdev_child[c];
7302e4c9986SGeorge Wilson 		metaslab_group_t *mg = tvd->vdev_mg;
7312e4c9986SGeorge Wilson 
7322e4c9986SGeorge Wilson 		if (mg->mg_class != mc)
7332e4c9986SGeorge Wilson 			continue;
7342e4c9986SGeorge Wilson 
7352e4c9986SGeorge Wilson 		metaslab_group_histogram_verify(mg);
7362e4c9986SGeorge Wilson 		mg->mg_fragmentation = metaslab_group_fragmentation(mg);
7372e4c9986SGeorge Wilson 
7382e4c9986SGeorge Wilson 		(void) printf("\tvdev %10llu\t\tmetaslabs%5llu\t\t"
7392e4c9986SGeorge Wilson 		    "fragmentation",
7402e4c9986SGeorge Wilson 		    (u_longlong_t)tvd->vdev_id,
7412e4c9986SGeorge Wilson 		    (u_longlong_t)tvd->vdev_ms_count);
7422e4c9986SGeorge Wilson 		if (mg->mg_fragmentation == ZFS_FRAG_INVALID) {
7432e4c9986SGeorge Wilson 			(void) printf("%3s\n", "-");
7442e4c9986SGeorge Wilson 		} else {
7452e4c9986SGeorge Wilson 			(void) printf("%3llu%%\n",
7462e4c9986SGeorge Wilson 			    (u_longlong_t)mg->mg_fragmentation);
7472e4c9986SGeorge Wilson 		}
7482e4c9986SGeorge Wilson 		dump_histogram(mg->mg_histogram, RANGE_TREE_HISTOGRAM_SIZE, 0);
7492e4c9986SGeorge Wilson 	}
7502e4c9986SGeorge Wilson 
7512e4c9986SGeorge Wilson 	(void) printf("\tpool %s\tfragmentation", spa_name(spa));
7522e4c9986SGeorge Wilson 	fragmentation = metaslab_class_fragmentation(mc);
7532e4c9986SGeorge Wilson 	if (fragmentation == ZFS_FRAG_INVALID)
7542e4c9986SGeorge Wilson 		(void) printf("\t%3s\n", "-");
7552e4c9986SGeorge Wilson 	else
7562e4c9986SGeorge Wilson 		(void) printf("\t%3llu%%\n", (u_longlong_t)fragmentation);
7572e4c9986SGeorge Wilson 	dump_histogram(mc->mc_histogram, RANGE_TREE_HISTOGRAM_SIZE, 0);
7582e4c9986SGeorge Wilson }
7592e4c9986SGeorge Wilson 
760fa9e4066Sahrens static void
761fa9e4066Sahrens dump_metaslabs(spa_t *spa)
762fa9e4066Sahrens {
76387219db7SVictor Latushkin 	vdev_t *vd, *rvd = spa->spa_root_vdev;
76487219db7SVictor Latushkin 	uint64_t m, c = 0, children = rvd->vdev_children;
765fa9e4066Sahrens 
766fa9e4066Sahrens 	(void) printf("\nMetaslabs:\n");
767fa9e4066Sahrens 
76887219db7SVictor Latushkin 	if (!dump_opt['d'] && zopt_objects > 0) {
76987219db7SVictor Latushkin 		c = zopt_object[0];
77087219db7SVictor Latushkin 
77187219db7SVictor Latushkin 		if (c >= children)
77287219db7SVictor Latushkin 			(void) fatal("bad vdev id: %llu", (u_longlong_t)c);
773fa9e4066Sahrens 
77487219db7SVictor Latushkin 		if (zopt_objects > 1) {
77587219db7SVictor Latushkin 			vd = rvd->vdev_child[c];
77687219db7SVictor Latushkin 			print_vdev_metaslab_header(vd);
77787219db7SVictor Latushkin 
77887219db7SVictor Latushkin 			for (m = 1; m < zopt_objects; m++) {
77987219db7SVictor Latushkin 				if (zopt_object[m] < vd->vdev_ms_count)
78087219db7SVictor Latushkin 					dump_metaslab(
78187219db7SVictor Latushkin 					    vd->vdev_ms[zopt_object[m]]);
78287219db7SVictor Latushkin 				else
78387219db7SVictor Latushkin 					(void) fprintf(stderr, "bad metaslab "
78487219db7SVictor Latushkin 					    "number %llu\n",
78587219db7SVictor Latushkin 					    (u_longlong_t)zopt_object[m]);
78687219db7SVictor Latushkin 			}
78787219db7SVictor Latushkin 			(void) printf("\n");
78887219db7SVictor Latushkin 			return;
78987219db7SVictor Latushkin 		}
79087219db7SVictor Latushkin 		children = c + 1;
79187219db7SVictor Latushkin 	}
79287219db7SVictor Latushkin 	for (; c < children; c++) {
79387219db7SVictor Latushkin 		vd = rvd->vdev_child[c];
79487219db7SVictor Latushkin 		print_vdev_metaslab_header(vd);
795fa9e4066Sahrens 
796fa9e4066Sahrens 		for (m = 0; m < vd->vdev_ms_count; m++)
797fa9e4066Sahrens 			dump_metaslab(vd->vdev_ms[m]);
798fa9e4066Sahrens 		(void) printf("\n");
799fa9e4066Sahrens 	}
800fa9e4066Sahrens }
801fa9e4066Sahrens 
802b24ab676SJeff Bonwick static void
803b24ab676SJeff Bonwick dump_dde(const ddt_t *ddt, const ddt_entry_t *dde, uint64_t index)
804b24ab676SJeff Bonwick {
805b24ab676SJeff Bonwick 	const ddt_phys_t *ddp = dde->dde_phys;
806b24ab676SJeff Bonwick 	const ddt_key_t *ddk = &dde->dde_key;
807b24ab676SJeff Bonwick 	char *types[4] = { "ditto", "single", "double", "triple" };
808b24ab676SJeff Bonwick 	char blkbuf[BP_SPRINTF_LEN];
809b24ab676SJeff Bonwick 	blkptr_t blk;
810b24ab676SJeff Bonwick 
811b24ab676SJeff Bonwick 	for (int p = 0; p < DDT_PHYS_TYPES; p++, ddp++) {
812b24ab676SJeff Bonwick 		if (ddp->ddp_phys_birth == 0)
813b24ab676SJeff Bonwick 			continue;
814bbfd46c4SJeff Bonwick 		ddt_bp_create(ddt->ddt_checksum, ddk, ddp, &blk);
81543466aaeSMax Grossman 		snprintf_blkptr(blkbuf, sizeof (blkbuf), &blk);
816b24ab676SJeff Bonwick 		(void) printf("index %llx refcnt %llu %s %s\n",
817b24ab676SJeff Bonwick 		    (u_longlong_t)index, (u_longlong_t)ddp->ddp_refcnt,
818b24ab676SJeff Bonwick 		    types[p], blkbuf);
819b24ab676SJeff Bonwick 	}
820b24ab676SJeff Bonwick }
821b24ab676SJeff Bonwick 
822b24ab676SJeff Bonwick static void
823b24ab676SJeff Bonwick dump_dedup_ratio(const ddt_stat_t *dds)
824b24ab676SJeff Bonwick {
825b24ab676SJeff Bonwick 	double rL, rP, rD, D, dedup, compress, copies;
826b24ab676SJeff Bonwick 
827b24ab676SJeff Bonwick 	if (dds->dds_blocks == 0)
828b24ab676SJeff Bonwick 		return;
829b24ab676SJeff Bonwick 
830b24ab676SJeff Bonwick 	rL = (double)dds->dds_ref_lsize;
831b24ab676SJeff Bonwick 	rP = (double)dds->dds_ref_psize;
832b24ab676SJeff Bonwick 	rD = (double)dds->dds_ref_dsize;
833b24ab676SJeff Bonwick 	D = (double)dds->dds_dsize;
834b24ab676SJeff Bonwick 
835b24ab676SJeff Bonwick 	dedup = rD / D;
836b24ab676SJeff Bonwick 	compress = rL / rP;
837b24ab676SJeff Bonwick 	copies = rD / rP;
838b24ab676SJeff Bonwick 
839b24ab676SJeff Bonwick 	(void) printf("dedup = %.2f, compress = %.2f, copies = %.2f, "
840b24ab676SJeff Bonwick 	    "dedup * compress / copies = %.2f\n\n",
841b24ab676SJeff Bonwick 	    dedup, compress, copies, dedup * compress / copies);
842b24ab676SJeff Bonwick }
843b24ab676SJeff Bonwick 
844b24ab676SJeff Bonwick static void
845b24ab676SJeff Bonwick dump_ddt(ddt_t *ddt, enum ddt_type type, enum ddt_class class)
846b24ab676SJeff Bonwick {
847b24ab676SJeff Bonwick 	char name[DDT_NAMELEN];
848b24ab676SJeff Bonwick 	ddt_entry_t dde;
849b24ab676SJeff Bonwick 	uint64_t walk = 0;
850b24ab676SJeff Bonwick 	dmu_object_info_t doi;
851b24ab676SJeff Bonwick 	uint64_t count, dspace, mspace;
852b24ab676SJeff Bonwick 	int error;
853b24ab676SJeff Bonwick 
854b24ab676SJeff Bonwick 	error = ddt_object_info(ddt, type, class, &doi);
855b24ab676SJeff Bonwick 
856b24ab676SJeff Bonwick 	if (error == ENOENT)
857b24ab676SJeff Bonwick 		return;
858b24ab676SJeff Bonwick 	ASSERT(error == 0);
859b24ab676SJeff Bonwick 
8607448a079SGeorge Wilson 	if ((count = ddt_object_count(ddt, type, class)) == 0)
8617448a079SGeorge Wilson 		return;
8627448a079SGeorge Wilson 
863b24ab676SJeff Bonwick 	dspace = doi.doi_physical_blocks_512 << 9;
864b24ab676SJeff Bonwick 	mspace = doi.doi_fill_count * doi.doi_data_block_size;
865b24ab676SJeff Bonwick 
866b24ab676SJeff Bonwick 	ddt_object_name(ddt, type, class, name);
867b24ab676SJeff Bonwick 
868b24ab676SJeff Bonwick 	(void) printf("%s: %llu entries, size %llu on disk, %llu in core\n",
869b24ab676SJeff Bonwick 	    name,
870b24ab676SJeff Bonwick 	    (u_longlong_t)count,
871b24ab676SJeff Bonwick 	    (u_longlong_t)(dspace / count),
872b24ab676SJeff Bonwick 	    (u_longlong_t)(mspace / count));
873b24ab676SJeff Bonwick 
874b24ab676SJeff Bonwick 	if (dump_opt['D'] < 3)
875b24ab676SJeff Bonwick 		return;
876b24ab676SJeff Bonwick 
8779eb19f4dSGeorge Wilson 	zpool_dump_ddt(NULL, &ddt->ddt_histogram[type][class]);
878b24ab676SJeff Bonwick 
879b24ab676SJeff Bonwick 	if (dump_opt['D'] < 4)
880b24ab676SJeff Bonwick 		return;
881b24ab676SJeff Bonwick 
882b24ab676SJeff Bonwick 	if (dump_opt['D'] < 5 && class == DDT_CLASS_UNIQUE)
883b24ab676SJeff Bonwick 		return;
884b24ab676SJeff Bonwick 
885b24ab676SJeff Bonwick 	(void) printf("%s contents:\n\n", name);
886b24ab676SJeff Bonwick 
887bbfd46c4SJeff Bonwick 	while ((error = ddt_object_walk(ddt, type, class, &walk, &dde)) == 0)
888b24ab676SJeff Bonwick 		dump_dde(ddt, &dde, walk);
889b24ab676SJeff Bonwick 
890b24ab676SJeff Bonwick 	ASSERT(error == ENOENT);
891b24ab676SJeff Bonwick 
892b24ab676SJeff Bonwick 	(void) printf("\n");
893b24ab676SJeff Bonwick }
894b24ab676SJeff Bonwick 
895b24ab676SJeff Bonwick static void
896b24ab676SJeff Bonwick dump_all_ddts(spa_t *spa)
897b24ab676SJeff Bonwick {
898b24ab676SJeff Bonwick 	ddt_histogram_t ddh_total = { 0 };
899b24ab676SJeff Bonwick 	ddt_stat_t dds_total = { 0 };
900b24ab676SJeff Bonwick 
901b24ab676SJeff Bonwick 	for (enum zio_checksum c = 0; c < ZIO_CHECKSUM_FUNCTIONS; c++) {
902b24ab676SJeff Bonwick 		ddt_t *ddt = spa->spa_ddt[c];
903b24ab676SJeff Bonwick 		for (enum ddt_type type = 0; type < DDT_TYPES; type++) {
904b24ab676SJeff Bonwick 			for (enum ddt_class class = 0; class < DDT_CLASSES;
905b24ab676SJeff Bonwick 			    class++) {
906b24ab676SJeff Bonwick 				dump_ddt(ddt, type, class);
907b24ab676SJeff Bonwick 			}
908b24ab676SJeff Bonwick 		}
909b24ab676SJeff Bonwick 	}
910b24ab676SJeff Bonwick 
9119eb19f4dSGeorge Wilson 	ddt_get_dedup_stats(spa, &dds_total);
912b24ab676SJeff Bonwick 
913b24ab676SJeff Bonwick 	if (dds_total.dds_blocks == 0) {
914b24ab676SJeff Bonwick 		(void) printf("All DDTs are empty\n");
915b24ab676SJeff Bonwick 		return;
916b24ab676SJeff Bonwick 	}
917b24ab676SJeff Bonwick 
918b24ab676SJeff Bonwick 	(void) printf("\n");
919b24ab676SJeff Bonwick 
920b24ab676SJeff Bonwick 	if (dump_opt['D'] > 1) {
921b24ab676SJeff Bonwick 		(void) printf("DDT histogram (aggregated over all DDTs):\n");
9229eb19f4dSGeorge Wilson 		ddt_get_dedup_histogram(spa, &ddh_total);
9239eb19f4dSGeorge Wilson 		zpool_dump_ddt(&dds_total, &ddh_total);
924b24ab676SJeff Bonwick 	}
925b24ab676SJeff Bonwick 
926b24ab676SJeff Bonwick 	dump_dedup_ratio(&dds_total);
927b24ab676SJeff Bonwick }
928b24ab676SJeff Bonwick 
9298ad4d6ddSJeff Bonwick static void
9300713e232SGeorge Wilson dump_dtl_seg(void *arg, uint64_t start, uint64_t size)
9318ad4d6ddSJeff Bonwick {
9320713e232SGeorge Wilson 	char *prefix = arg;
9338ad4d6ddSJeff Bonwick 
9348ad4d6ddSJeff Bonwick 	(void) printf("%s [%llu,%llu) length %llu\n",
9358ad4d6ddSJeff Bonwick 	    prefix,
9368ad4d6ddSJeff Bonwick 	    (u_longlong_t)start,
9378ad4d6ddSJeff Bonwick 	    (u_longlong_t)(start + size),
9388ad4d6ddSJeff Bonwick 	    (u_longlong_t)(size));
9398ad4d6ddSJeff Bonwick }
9408ad4d6ddSJeff Bonwick 
941fa9e4066Sahrens static void
942fa9e4066Sahrens dump_dtl(vdev_t *vd, int indent)
943fa9e4066Sahrens {
9448ad4d6ddSJeff Bonwick 	spa_t *spa = vd->vdev_spa;
9458ad4d6ddSJeff Bonwick 	boolean_t required;
9468ad4d6ddSJeff Bonwick 	char *name[DTL_TYPES] = { "missing", "partial", "scrub", "outage" };
9478ad4d6ddSJeff Bonwick 	char prefix[256];
9488ad4d6ddSJeff Bonwick 
9498f18d1faSGeorge Wilson 	spa_vdev_state_enter(spa, SCL_NONE);
9508ad4d6ddSJeff Bonwick 	required = vdev_dtl_required(vd);
9518ad4d6ddSJeff Bonwick 	(void) spa_vdev_state_exit(spa, NULL, 0);
952fa9e4066Sahrens 
953fa9e4066Sahrens 	if (indent == 0)
954fa9e4066Sahrens 		(void) printf("\nDirty time logs:\n\n");
955fa9e4066Sahrens 
9568ad4d6ddSJeff Bonwick 	(void) printf("\t%*s%s [%s]\n", indent, "",
957e14bb325SJeff Bonwick 	    vd->vdev_path ? vd->vdev_path :
9588ad4d6ddSJeff Bonwick 	    vd->vdev_parent ? vd->vdev_ops->vdev_op_type : spa_name(spa),
9598ad4d6ddSJeff Bonwick 	    required ? "DTL-required" : "DTL-expendable");
960fa9e4066Sahrens 
9618ad4d6ddSJeff Bonwick 	for (int t = 0; t < DTL_TYPES; t++) {
9620713e232SGeorge Wilson 		range_tree_t *rt = vd->vdev_dtl[t];
9630713e232SGeorge Wilson 		if (range_tree_space(rt) == 0)
9648ad4d6ddSJeff Bonwick 			continue;
9658ad4d6ddSJeff Bonwick 		(void) snprintf(prefix, sizeof (prefix), "\t%*s%s",
9668ad4d6ddSJeff Bonwick 		    indent + 2, "", name[t]);
9670713e232SGeorge Wilson 		mutex_enter(rt->rt_lock);
9680713e232SGeorge Wilson 		range_tree_walk(rt, dump_dtl_seg, prefix);
9690713e232SGeorge Wilson 		mutex_exit(rt->rt_lock);
9708ad4d6ddSJeff Bonwick 		if (dump_opt['d'] > 5 && vd->vdev_children == 0)
9710713e232SGeorge Wilson 			dump_spacemap(spa->spa_meta_objset, vd->vdev_dtl_sm);
972fa9e4066Sahrens 	}
973fa9e4066Sahrens 
9748ad4d6ddSJeff Bonwick 	for (int c = 0; c < vd->vdev_children; c++)
975fa9e4066Sahrens 		dump_dtl(vd->vdev_child[c], indent + 4);
976fa9e4066Sahrens }
977fa9e4066Sahrens 
9788f18d1faSGeorge Wilson static void
9798f18d1faSGeorge Wilson dump_history(spa_t *spa)
9808f18d1faSGeorge Wilson {
9818f18d1faSGeorge Wilson 	nvlist_t **events = NULL;
9828f18d1faSGeorge Wilson 	char buf[SPA_MAXBLOCKSIZE];
983e4161df6SVictor Latushkin 	uint64_t resid, len, off = 0;
9848f18d1faSGeorge Wilson 	uint_t num = 0;
9858f18d1faSGeorge Wilson 	int error;
9868f18d1faSGeorge Wilson 	time_t tsec;
9878f18d1faSGeorge Wilson 	struct tm t;
9888f18d1faSGeorge Wilson 	char tbuf[30];
9898f18d1faSGeorge Wilson 	char internalstr[MAXPATHLEN];
9908f18d1faSGeorge Wilson 
9918f18d1faSGeorge Wilson 	do {
992e4161df6SVictor Latushkin 		len = sizeof (buf);
993e4161df6SVictor Latushkin 
9948f18d1faSGeorge Wilson 		if ((error = spa_history_get(spa, &off, &len, buf)) != 0) {
9958f18d1faSGeorge Wilson 			(void) fprintf(stderr, "Unable to read history: "
9968f18d1faSGeorge Wilson 			    "error %d\n", error);
9978f18d1faSGeorge Wilson 			return;
9988f18d1faSGeorge Wilson 		}
9998f18d1faSGeorge Wilson 
10008f18d1faSGeorge Wilson 		if (zpool_history_unpack(buf, len, &resid, &events, &num) != 0)
10018f18d1faSGeorge Wilson 			break;
10028f18d1faSGeorge Wilson 
10038f18d1faSGeorge Wilson 		off -= resid;
10048f18d1faSGeorge Wilson 	} while (len != 0);
10058f18d1faSGeorge Wilson 
10068f18d1faSGeorge Wilson 	(void) printf("\nHistory:\n");
10078f18d1faSGeorge Wilson 	for (int i = 0; i < num; i++) {
10088f18d1faSGeorge Wilson 		uint64_t time, txg, ievent;
10098f18d1faSGeorge Wilson 		char *cmd, *intstr;
10104445fffbSMatthew Ahrens 		boolean_t printed = B_FALSE;
10118f18d1faSGeorge Wilson 
10128f18d1faSGeorge Wilson 		if (nvlist_lookup_uint64(events[i], ZPOOL_HIST_TIME,
10138f18d1faSGeorge Wilson 		    &time) != 0)
10144445fffbSMatthew Ahrens 			goto next;
10158f18d1faSGeorge Wilson 		if (nvlist_lookup_string(events[i], ZPOOL_HIST_CMD,
10168f18d1faSGeorge Wilson 		    &cmd) != 0) {
10178f18d1faSGeorge Wilson 			if (nvlist_lookup_uint64(events[i],
10188f18d1faSGeorge Wilson 			    ZPOOL_HIST_INT_EVENT, &ievent) != 0)
10194445fffbSMatthew Ahrens 				goto next;
10208f18d1faSGeorge Wilson 			verify(nvlist_lookup_uint64(events[i],
10218f18d1faSGeorge Wilson 			    ZPOOL_HIST_TXG, &txg) == 0);
10228f18d1faSGeorge Wilson 			verify(nvlist_lookup_string(events[i],
10238f18d1faSGeorge Wilson 			    ZPOOL_HIST_INT_STR, &intstr) == 0);
10244445fffbSMatthew Ahrens 			if (ievent >= ZFS_NUM_LEGACY_HISTORY_EVENTS)
10254445fffbSMatthew Ahrens 				goto next;
10268f18d1faSGeorge Wilson 
10278f18d1faSGeorge Wilson 			(void) snprintf(internalstr,
10288f18d1faSGeorge Wilson 			    sizeof (internalstr),
10298f18d1faSGeorge Wilson 			    "[internal %s txg:%lld] %s",
10303f9d6ad7SLin Ling 			    zfs_history_event_names[ievent], txg,
10318f18d1faSGeorge Wilson 			    intstr);
10328f18d1faSGeorge Wilson 			cmd = internalstr;
10338f18d1faSGeorge Wilson 		}
10348f18d1faSGeorge Wilson 		tsec = time;
10358f18d1faSGeorge Wilson 		(void) localtime_r(&tsec, &t);
10368f18d1faSGeorge Wilson 		(void) strftime(tbuf, sizeof (tbuf), "%F.%T", &t);
10378f18d1faSGeorge Wilson 		(void) printf("%s %s\n", tbuf, cmd);
10384445fffbSMatthew Ahrens 		printed = B_TRUE;
10394445fffbSMatthew Ahrens 
10404445fffbSMatthew Ahrens next:
10414445fffbSMatthew Ahrens 		if (dump_opt['h'] > 1) {
10424445fffbSMatthew Ahrens 			if (!printed)
10434445fffbSMatthew Ahrens 				(void) printf("unrecognized record:\n");
10444445fffbSMatthew Ahrens 			dump_nvlist(events[i], 2);
10454445fffbSMatthew Ahrens 		}
10468f18d1faSGeorge Wilson 	}
10478f18d1faSGeorge Wilson }
10488f18d1faSGeorge Wilson 
1049fa9e4066Sahrens /*ARGSUSED*/
1050fa9e4066Sahrens static void
1051fa9e4066Sahrens dump_dnode(objset_t *os, uint64_t object, void *data, size_t size)
1052fa9e4066Sahrens {
1053fa9e4066Sahrens }
1054fa9e4066Sahrens 
1055fa9e4066Sahrens static uint64_t
10567802d7bfSMatthew Ahrens blkid2offset(const dnode_phys_t *dnp, const blkptr_t *bp,
10577802d7bfSMatthew Ahrens     const zbookmark_phys_t *zb)
1058fa9e4066Sahrens {
1059b24ab676SJeff Bonwick 	if (dnp == NULL) {
1060b24ab676SJeff Bonwick 		ASSERT(zb->zb_level < 0);
1061b24ab676SJeff Bonwick 		if (zb->zb_object == 0)
1062b24ab676SJeff Bonwick 			return (zb->zb_blkid);
1063b24ab676SJeff Bonwick 		return (zb->zb_blkid * BP_GET_LSIZE(bp));
1064b24ab676SJeff Bonwick 	}
1065b24ab676SJeff Bonwick 
1066b24ab676SJeff Bonwick 	ASSERT(zb->zb_level >= 0);
1067fa9e4066Sahrens 
1068b24ab676SJeff Bonwick 	return ((zb->zb_blkid <<
1069b24ab676SJeff Bonwick 	    (zb->zb_level * (dnp->dn_indblkshift - SPA_BLKPTRSHIFT))) *
1070fa9e4066Sahrens 	    dnp->dn_datablkszsec << SPA_MINBLOCKSHIFT);
1071fa9e4066Sahrens }
1072fa9e4066Sahrens 
107344cd46caSbillm static void
107443466aaeSMax Grossman snprintf_blkptr_compact(char *blkbuf, size_t buflen, const blkptr_t *bp)
107544cd46caSbillm {
1076cde58dbcSMatthew Ahrens 	const dva_t *dva = bp->blk_dva;
1077b24ab676SJeff Bonwick 	int ndvas = dump_opt['d'] > 5 ? BP_GET_NDVAS(bp) : 1;
1078b24ab676SJeff Bonwick 
1079490d05b9SMatthew Ahrens 	if (dump_opt['b'] >= 6) {
108043466aaeSMax Grossman 		snprintf_blkptr(blkbuf, buflen, bp);
1081b24ab676SJeff Bonwick 		return;
1082b24ab676SJeff Bonwick 	}
108344cd46caSbillm 
10845d7b4d43SMatthew Ahrens 	if (BP_IS_EMBEDDED(bp)) {
10855d7b4d43SMatthew Ahrens 		(void) sprintf(blkbuf,
10865d7b4d43SMatthew Ahrens 		    "EMBEDDED et=%u %llxL/%llxP B=%llu",
10875d7b4d43SMatthew Ahrens 		    (int)BPE_GET_ETYPE(bp),
10885d7b4d43SMatthew Ahrens 		    (u_longlong_t)BPE_GET_LSIZE(bp),
10895d7b4d43SMatthew Ahrens 		    (u_longlong_t)BPE_GET_PSIZE(bp),
10905d7b4d43SMatthew Ahrens 		    (u_longlong_t)bp->blk_birth);
10915d7b4d43SMatthew Ahrens 		return;
10925d7b4d43SMatthew Ahrens 	}
109344cd46caSbillm 
10945d7b4d43SMatthew Ahrens 	blkbuf[0] = '\0';
1095b24ab676SJeff Bonwick 	for (int i = 0; i < ndvas; i++)
109643466aaeSMax Grossman 		(void) snprintf(blkbuf + strlen(blkbuf),
109743466aaeSMax Grossman 		    buflen - strlen(blkbuf), "%llu:%llx:%llx ",
109844cd46caSbillm 		    (u_longlong_t)DVA_GET_VDEV(&dva[i]),
109944cd46caSbillm 		    (u_longlong_t)DVA_GET_OFFSET(&dva[i]),
110044cd46caSbillm 		    (u_longlong_t)DVA_GET_ASIZE(&dva[i]));
110144cd46caSbillm 
110243466aaeSMax Grossman 	if (BP_IS_HOLE(bp)) {
110343466aaeSMax Grossman 		(void) snprintf(blkbuf + strlen(blkbuf),
110443466aaeSMax Grossman 		    buflen - strlen(blkbuf), "B=%llu",
110543466aaeSMax Grossman 		    (u_longlong_t)bp->blk_birth);
110643466aaeSMax Grossman 	} else {
110743466aaeSMax Grossman 		(void) snprintf(blkbuf + strlen(blkbuf),
110843466aaeSMax Grossman 		    buflen - strlen(blkbuf),
110943466aaeSMax Grossman 		    "%llxL/%llxP F=%llu B=%llu/%llu",
111043466aaeSMax Grossman 		    (u_longlong_t)BP_GET_LSIZE(bp),
111143466aaeSMax Grossman 		    (u_longlong_t)BP_GET_PSIZE(bp),
11125d7b4d43SMatthew Ahrens 		    (u_longlong_t)BP_GET_FILL(bp),
111343466aaeSMax Grossman 		    (u_longlong_t)bp->blk_birth,
111443466aaeSMax Grossman 		    (u_longlong_t)BP_PHYSICAL_BIRTH(bp));
111543466aaeSMax Grossman 	}
111644cd46caSbillm }
111744cd46caSbillm 
111888b7b0f2SMatthew Ahrens static void
11197802d7bfSMatthew Ahrens print_indirect(blkptr_t *bp, const zbookmark_phys_t *zb,
112088b7b0f2SMatthew Ahrens     const dnode_phys_t *dnp)
1121fa9e4066Sahrens {
112288b7b0f2SMatthew Ahrens 	char blkbuf[BP_SPRINTF_LEN];
1123fa9e4066Sahrens 	int l;
1124fa9e4066Sahrens 
11255d7b4d43SMatthew Ahrens 	if (!BP_IS_EMBEDDED(bp)) {
11265d7b4d43SMatthew Ahrens 		ASSERT3U(BP_GET_TYPE(bp), ==, dnp->dn_type);
11275d7b4d43SMatthew Ahrens 		ASSERT3U(BP_GET_LEVEL(bp), ==, zb->zb_level);
11285d7b4d43SMatthew Ahrens 	}
1129fa9e4066Sahrens 
1130b24ab676SJeff Bonwick 	(void) printf("%16llx ", (u_longlong_t)blkid2offset(dnp, bp, zb));
1131fa9e4066Sahrens 
1132fa9e4066Sahrens 	ASSERT(zb->zb_level >= 0);
1133fa9e4066Sahrens 
1134fa9e4066Sahrens 	for (l = dnp->dn_nlevels - 1; l >= -1; l--) {
1135fa9e4066Sahrens 		if (l == zb->zb_level) {
113688b7b0f2SMatthew Ahrens 			(void) printf("L%llx", (u_longlong_t)zb->zb_level);
1137fa9e4066Sahrens 		} else {
113888b7b0f2SMatthew Ahrens 			(void) printf(" ");
1139fa9e4066Sahrens 		}
1140fa9e4066Sahrens 	}
1141fa9e4066Sahrens 
114243466aaeSMax Grossman 	snprintf_blkptr_compact(blkbuf, sizeof (blkbuf), bp);
114388b7b0f2SMatthew Ahrens 	(void) printf("%s\n", blkbuf);
114488b7b0f2SMatthew Ahrens }
114588b7b0f2SMatthew Ahrens 
114688b7b0f2SMatthew Ahrens static int
114788b7b0f2SMatthew Ahrens visit_indirect(spa_t *spa, const dnode_phys_t *dnp,
11487802d7bfSMatthew Ahrens     blkptr_t *bp, const zbookmark_phys_t *zb)
114988b7b0f2SMatthew Ahrens {
1150e4161df6SVictor Latushkin 	int err = 0;
115188b7b0f2SMatthew Ahrens 
115288b7b0f2SMatthew Ahrens 	if (bp->blk_birth == 0)
115388b7b0f2SMatthew Ahrens 		return (0);
115488b7b0f2SMatthew Ahrens 
115588b7b0f2SMatthew Ahrens 	print_indirect(bp, zb, dnp);
115688b7b0f2SMatthew Ahrens 
115743466aaeSMax Grossman 	if (BP_GET_LEVEL(bp) > 0 && !BP_IS_HOLE(bp)) {
115888b7b0f2SMatthew Ahrens 		uint32_t flags = ARC_WAIT;
115988b7b0f2SMatthew Ahrens 		int i;
116088b7b0f2SMatthew Ahrens 		blkptr_t *cbp;
116188b7b0f2SMatthew Ahrens 		int epb = BP_GET_LSIZE(bp) >> SPA_BLKPTRSHIFT;
116288b7b0f2SMatthew Ahrens 		arc_buf_t *buf;
116388b7b0f2SMatthew Ahrens 		uint64_t fill = 0;
116488b7b0f2SMatthew Ahrens 
11651b912ec7SGeorge Wilson 		err = arc_read(NULL, spa, bp, arc_getbuf_func, &buf,
116688b7b0f2SMatthew Ahrens 		    ZIO_PRIORITY_ASYNC_READ, ZIO_FLAG_CANFAIL, &flags, zb);
116788b7b0f2SMatthew Ahrens 		if (err)
116888b7b0f2SMatthew Ahrens 			return (err);
11693f9d6ad7SLin Ling 		ASSERT(buf->b_data);
117088b7b0f2SMatthew Ahrens 
117188b7b0f2SMatthew Ahrens 		/* recursively visit blocks below this */
117288b7b0f2SMatthew Ahrens 		cbp = buf->b_data;
117388b7b0f2SMatthew Ahrens 		for (i = 0; i < epb; i++, cbp++) {
11747802d7bfSMatthew Ahrens 			zbookmark_phys_t czb;
117588b7b0f2SMatthew Ahrens 
117688b7b0f2SMatthew Ahrens 			SET_BOOKMARK(&czb, zb->zb_objset, zb->zb_object,
117788b7b0f2SMatthew Ahrens 			    zb->zb_level - 1,
117888b7b0f2SMatthew Ahrens 			    zb->zb_blkid * epb + i);
117988b7b0f2SMatthew Ahrens 			err = visit_indirect(spa, dnp, cbp, &czb);
118088b7b0f2SMatthew Ahrens 			if (err)
118188b7b0f2SMatthew Ahrens 				break;
11825d7b4d43SMatthew Ahrens 			fill += BP_GET_FILL(cbp);
118388b7b0f2SMatthew Ahrens 		}
11848ad4d6ddSJeff Bonwick 		if (!err)
11855d7b4d43SMatthew Ahrens 			ASSERT3U(fill, ==, BP_GET_FILL(bp));
118688b7b0f2SMatthew Ahrens 		(void) arc_buf_remove_ref(buf, &buf);
1187fa9e4066Sahrens 	}
1188fa9e4066Sahrens 
118988b7b0f2SMatthew Ahrens 	return (err);
1190fa9e4066Sahrens }
1191fa9e4066Sahrens 
1192fa9e4066Sahrens /*ARGSUSED*/
1193fa9e4066Sahrens static void
119488b7b0f2SMatthew Ahrens dump_indirect(dnode_t *dn)
1195fa9e4066Sahrens {
119688b7b0f2SMatthew Ahrens 	dnode_phys_t *dnp = dn->dn_phys;
119788b7b0f2SMatthew Ahrens 	int j;
11987802d7bfSMatthew Ahrens 	zbookmark_phys_t czb;
1199fa9e4066Sahrens 
1200fa9e4066Sahrens 	(void) printf("Indirect blocks:\n");
1201fa9e4066Sahrens 
1202503ad85cSMatthew Ahrens 	SET_BOOKMARK(&czb, dmu_objset_id(dn->dn_objset),
120388b7b0f2SMatthew Ahrens 	    dn->dn_object, dnp->dn_nlevels - 1, 0);
120488b7b0f2SMatthew Ahrens 	for (j = 0; j < dnp->dn_nblkptr; j++) {
120588b7b0f2SMatthew Ahrens 		czb.zb_blkid = j;
1206503ad85cSMatthew Ahrens 		(void) visit_indirect(dmu_objset_spa(dn->dn_objset), dnp,
120788b7b0f2SMatthew Ahrens 		    &dnp->dn_blkptr[j], &czb);
120888b7b0f2SMatthew Ahrens 	}
1209fa9e4066Sahrens 
1210fa9e4066Sahrens 	(void) printf("\n");
1211fa9e4066Sahrens }
1212fa9e4066Sahrens 
1213fa9e4066Sahrens /*ARGSUSED*/
1214fa9e4066Sahrens static void
1215fa9e4066Sahrens dump_dsl_dir(objset_t *os, uint64_t object, void *data, size_t size)
1216fa9e4066Sahrens {
1217fa9e4066Sahrens 	dsl_dir_phys_t *dd = data;
1218fa9e4066Sahrens 	time_t crtime;
12193f9d6ad7SLin Ling 	char nice[32];
1220fa9e4066Sahrens 
1221fa9e4066Sahrens 	if (dd == NULL)
1222fa9e4066Sahrens 		return;
1223fa9e4066Sahrens 
1224da6c28aaSamw 	ASSERT3U(size, >=, sizeof (dsl_dir_phys_t));
1225fa9e4066Sahrens 
1226fa9e4066Sahrens 	crtime = dd->dd_creation_time;
1227fa9e4066Sahrens 	(void) printf("\t\tcreation_time = %s", ctime(&crtime));
1228fa9e4066Sahrens 	(void) printf("\t\thead_dataset_obj = %llu\n",
1229fa9e4066Sahrens 	    (u_longlong_t)dd->dd_head_dataset_obj);
1230fa9e4066Sahrens 	(void) printf("\t\tparent_dir_obj = %llu\n",
1231fa9e4066Sahrens 	    (u_longlong_t)dd->dd_parent_obj);
12323cb34c60Sahrens 	(void) printf("\t\torigin_obj = %llu\n",
12333cb34c60Sahrens 	    (u_longlong_t)dd->dd_origin_obj);
1234fa9e4066Sahrens 	(void) printf("\t\tchild_dir_zapobj = %llu\n",
1235fa9e4066Sahrens 	    (u_longlong_t)dd->dd_child_dir_zapobj);
12363f9d6ad7SLin Ling 	zdb_nicenum(dd->dd_used_bytes, nice);
123774e7dc98SMatthew Ahrens 	(void) printf("\t\tused_bytes = %s\n", nice);
12383f9d6ad7SLin Ling 	zdb_nicenum(dd->dd_compressed_bytes, nice);
123974e7dc98SMatthew Ahrens 	(void) printf("\t\tcompressed_bytes = %s\n", nice);
12403f9d6ad7SLin Ling 	zdb_nicenum(dd->dd_uncompressed_bytes, nice);
124174e7dc98SMatthew Ahrens 	(void) printf("\t\tuncompressed_bytes = %s\n", nice);
12423f9d6ad7SLin Ling 	zdb_nicenum(dd->dd_quota, nice);
124374e7dc98SMatthew Ahrens 	(void) printf("\t\tquota = %s\n", nice);
12443f9d6ad7SLin Ling 	zdb_nicenum(dd->dd_reserved, nice);
124574e7dc98SMatthew Ahrens 	(void) printf("\t\treserved = %s\n", nice);
1246fa9e4066Sahrens 	(void) printf("\t\tprops_zapobj = %llu\n",
1247fa9e4066Sahrens 	    (u_longlong_t)dd->dd_props_zapobj);
1248ecd6cf80Smarks 	(void) printf("\t\tdeleg_zapobj = %llu\n",
1249ecd6cf80Smarks 	    (u_longlong_t)dd->dd_deleg_zapobj);
125074e7dc98SMatthew Ahrens 	(void) printf("\t\tflags = %llx\n",
125174e7dc98SMatthew Ahrens 	    (u_longlong_t)dd->dd_flags);
125274e7dc98SMatthew Ahrens 
125374e7dc98SMatthew Ahrens #define	DO(which) \
12543f9d6ad7SLin Ling 	zdb_nicenum(dd->dd_used_breakdown[DD_USED_ ## which], nice); \
125574e7dc98SMatthew Ahrens 	(void) printf("\t\tused_breakdown[" #which "] = %s\n", nice)
125674e7dc98SMatthew Ahrens 	DO(HEAD);
125774e7dc98SMatthew Ahrens 	DO(SNAP);
125874e7dc98SMatthew Ahrens 	DO(CHILD);
125974e7dc98SMatthew Ahrens 	DO(CHILD_RSRV);
126074e7dc98SMatthew Ahrens 	DO(REFRSRV);
126174e7dc98SMatthew Ahrens #undef DO
1262fa9e4066Sahrens }
1263fa9e4066Sahrens 
1264fa9e4066Sahrens /*ARGSUSED*/
1265fa9e4066Sahrens static void
1266fa9e4066Sahrens dump_dsl_dataset(objset_t *os, uint64_t object, void *data, size_t size)
1267fa9e4066Sahrens {
1268fa9e4066Sahrens 	dsl_dataset_phys_t *ds = data;
1269fa9e4066Sahrens 	time_t crtime;
12703f9d6ad7SLin Ling 	char used[32], compressed[32], uncompressed[32], unique[32];
1271fbabab8fSmaybee 	char blkbuf[BP_SPRINTF_LEN];
1272fa9e4066Sahrens 
1273fa9e4066Sahrens 	if (ds == NULL)
1274fa9e4066Sahrens 		return;
1275fa9e4066Sahrens 
1276fa9e4066Sahrens 	ASSERT(size == sizeof (*ds));
1277fa9e4066Sahrens 	crtime = ds->ds_creation_time;
1278ad135b5dSChristopher Siden 	zdb_nicenum(ds->ds_referenced_bytes, used);
12793f9d6ad7SLin Ling 	zdb_nicenum(ds->ds_compressed_bytes, compressed);
12803f9d6ad7SLin Ling 	zdb_nicenum(ds->ds_uncompressed_bytes, uncompressed);
12813f9d6ad7SLin Ling 	zdb_nicenum(ds->ds_unique_bytes, unique);
128243466aaeSMax Grossman 	snprintf_blkptr(blkbuf, sizeof (blkbuf), &ds->ds_bp);
1283fa9e4066Sahrens 
1284088f3894Sahrens 	(void) printf("\t\tdir_obj = %llu\n",
1285fa9e4066Sahrens 	    (u_longlong_t)ds->ds_dir_obj);
1286fa9e4066Sahrens 	(void) printf("\t\tprev_snap_obj = %llu\n",
1287fa9e4066Sahrens 	    (u_longlong_t)ds->ds_prev_snap_obj);
1288fa9e4066Sahrens 	(void) printf("\t\tprev_snap_txg = %llu\n",
1289fa9e4066Sahrens 	    (u_longlong_t)ds->ds_prev_snap_txg);
1290fa9e4066Sahrens 	(void) printf("\t\tnext_snap_obj = %llu\n",
1291fa9e4066Sahrens 	    (u_longlong_t)ds->ds_next_snap_obj);
1292fa9e4066Sahrens 	(void) printf("\t\tsnapnames_zapobj = %llu\n",
1293fa9e4066Sahrens 	    (u_longlong_t)ds->ds_snapnames_zapobj);
1294fa9e4066Sahrens 	(void) printf("\t\tnum_children = %llu\n",
1295fa9e4066Sahrens 	    (u_longlong_t)ds->ds_num_children);
1296842727c2SChris Kirby 	(void) printf("\t\tuserrefs_obj = %llu\n",
1297842727c2SChris Kirby 	    (u_longlong_t)ds->ds_userrefs_obj);
1298fa9e4066Sahrens 	(void) printf("\t\tcreation_time = %s", ctime(&crtime));
1299fa9e4066Sahrens 	(void) printf("\t\tcreation_txg = %llu\n",
1300fa9e4066Sahrens 	    (u_longlong_t)ds->ds_creation_txg);
1301fa9e4066Sahrens 	(void) printf("\t\tdeadlist_obj = %llu\n",
1302fa9e4066Sahrens 	    (u_longlong_t)ds->ds_deadlist_obj);
1303fa9e4066Sahrens 	(void) printf("\t\tused_bytes = %s\n", used);
1304fa9e4066Sahrens 	(void) printf("\t\tcompressed_bytes = %s\n", compressed);
1305fa9e4066Sahrens 	(void) printf("\t\tuncompressed_bytes = %s\n", uncompressed);
1306fa9e4066Sahrens 	(void) printf("\t\tunique = %s\n", unique);
1307fa9e4066Sahrens 	(void) printf("\t\tfsid_guid = %llu\n",
1308fa9e4066Sahrens 	    (u_longlong_t)ds->ds_fsid_guid);
1309fa9e4066Sahrens 	(void) printf("\t\tguid = %llu\n",
1310fa9e4066Sahrens 	    (u_longlong_t)ds->ds_guid);
131199653d4eSeschrock 	(void) printf("\t\tflags = %llx\n",
131299653d4eSeschrock 	    (u_longlong_t)ds->ds_flags);
1313088f3894Sahrens 	(void) printf("\t\tnext_clones_obj = %llu\n",
1314088f3894Sahrens 	    (u_longlong_t)ds->ds_next_clones_obj);
1315bb0ade09Sahrens 	(void) printf("\t\tprops_obj = %llu\n",
1316bb0ade09Sahrens 	    (u_longlong_t)ds->ds_props_obj);
1317fa9e4066Sahrens 	(void) printf("\t\tbp = %s\n", blkbuf);
1318fa9e4066Sahrens }
1319fa9e4066Sahrens 
1320ad135b5dSChristopher Siden /* ARGSUSED */
1321ad135b5dSChristopher Siden static int
1322ad135b5dSChristopher Siden dump_bptree_cb(void *arg, const blkptr_t *bp, dmu_tx_t *tx)
1323ad135b5dSChristopher Siden {
1324ad135b5dSChristopher Siden 	char blkbuf[BP_SPRINTF_LEN];
1325ad135b5dSChristopher Siden 
1326ad135b5dSChristopher Siden 	if (bp->blk_birth != 0) {
132743466aaeSMax Grossman 		snprintf_blkptr(blkbuf, sizeof (blkbuf), bp);
1328ad135b5dSChristopher Siden 		(void) printf("\t%s\n", blkbuf);
1329ad135b5dSChristopher Siden 	}
1330ad135b5dSChristopher Siden 	return (0);
1331ad135b5dSChristopher Siden }
1332ad135b5dSChristopher Siden 
1333ad135b5dSChristopher Siden static void
1334ad135b5dSChristopher Siden dump_bptree(objset_t *os, uint64_t obj, char *name)
1335ad135b5dSChristopher Siden {
1336ad135b5dSChristopher Siden 	char bytes[32];
1337ad135b5dSChristopher Siden 	bptree_phys_t *bt;
1338ad135b5dSChristopher Siden 	dmu_buf_t *db;
1339ad135b5dSChristopher Siden 
1340ad135b5dSChristopher Siden 	if (dump_opt['d'] < 3)
1341ad135b5dSChristopher Siden 		return;
1342ad135b5dSChristopher Siden 
1343b420f3adSRichard Lowe 	VERIFY3U(0, ==, dmu_bonus_hold(os, obj, FTAG, &db));
1344ad135b5dSChristopher Siden 	bt = db->db_data;
1345ad135b5dSChristopher Siden 	zdb_nicenum(bt->bt_bytes, bytes);
1346ad135b5dSChristopher Siden 	(void) printf("\n    %s: %llu datasets, %s\n",
1347ad135b5dSChristopher Siden 	    name, (unsigned long long)(bt->bt_end - bt->bt_begin), bytes);
1348ad135b5dSChristopher Siden 	dmu_buf_rele(db, FTAG);
1349ad135b5dSChristopher Siden 
1350ad135b5dSChristopher Siden 	if (dump_opt['d'] < 5)
1351ad135b5dSChristopher Siden 		return;
1352ad135b5dSChristopher Siden 
1353ad135b5dSChristopher Siden 	(void) printf("\n");
1354ad135b5dSChristopher Siden 
1355ad135b5dSChristopher Siden 	(void) bptree_iterate(os, obj, B_FALSE, dump_bptree_cb, NULL, NULL);
1356ad135b5dSChristopher Siden }
1357ad135b5dSChristopher Siden 
1358cde58dbcSMatthew Ahrens /* ARGSUSED */
1359cde58dbcSMatthew Ahrens static int
1360cde58dbcSMatthew Ahrens dump_bpobj_cb(void *arg, const blkptr_t *bp, dmu_tx_t *tx)
1361cde58dbcSMatthew Ahrens {
1362cde58dbcSMatthew Ahrens 	char blkbuf[BP_SPRINTF_LEN];
1363cde58dbcSMatthew Ahrens 
1364cde58dbcSMatthew Ahrens 	ASSERT(bp->blk_birth != 0);
136543466aaeSMax Grossman 	snprintf_blkptr_compact(blkbuf, sizeof (blkbuf), bp);
1366cde58dbcSMatthew Ahrens 	(void) printf("\t%s\n", blkbuf);
1367cde58dbcSMatthew Ahrens 	return (0);
1368cde58dbcSMatthew Ahrens }
1369cde58dbcSMatthew Ahrens 
1370fa9e4066Sahrens static void
1371d0475637SMatthew Ahrens dump_bpobj(bpobj_t *bpo, char *name, int indent)
1372fa9e4066Sahrens {
13733f9d6ad7SLin Ling 	char bytes[32];
13743f9d6ad7SLin Ling 	char comp[32];
13753f9d6ad7SLin Ling 	char uncomp[32];
1376fa9e4066Sahrens 
1377fa9e4066Sahrens 	if (dump_opt['d'] < 3)
1378fa9e4066Sahrens 		return;
1379fa9e4066Sahrens 
1380cde58dbcSMatthew Ahrens 	zdb_nicenum(bpo->bpo_phys->bpo_bytes, bytes);
1381d0475637SMatthew Ahrens 	if (bpo->bpo_havesubobj && bpo->bpo_phys->bpo_subobjs != 0) {
1382cde58dbcSMatthew Ahrens 		zdb_nicenum(bpo->bpo_phys->bpo_comp, comp);
1383cde58dbcSMatthew Ahrens 		zdb_nicenum(bpo->bpo_phys->bpo_uncomp, uncomp);
1384d0475637SMatthew Ahrens 		(void) printf("    %*s: object %llu, %llu local blkptrs, "
1385d0475637SMatthew Ahrens 		    "%llu subobjs, %s (%s/%s comp)\n",
1386d0475637SMatthew Ahrens 		    indent * 8, name,
1387d0475637SMatthew Ahrens 		    (u_longlong_t)bpo->bpo_object,
1388d0475637SMatthew Ahrens 		    (u_longlong_t)bpo->bpo_phys->bpo_num_blkptrs,
1389cde58dbcSMatthew Ahrens 		    (u_longlong_t)bpo->bpo_phys->bpo_num_subobjs,
139099653d4eSeschrock 		    bytes, comp, uncomp);
1391d0475637SMatthew Ahrens 
1392d0475637SMatthew Ahrens 		for (uint64_t i = 0; i < bpo->bpo_phys->bpo_num_subobjs; i++) {
1393d0475637SMatthew Ahrens 			uint64_t subobj;
1394d0475637SMatthew Ahrens 			bpobj_t subbpo;
1395d0475637SMatthew Ahrens 			int error;
1396d0475637SMatthew Ahrens 			VERIFY0(dmu_read(bpo->bpo_os,
1397d0475637SMatthew Ahrens 			    bpo->bpo_phys->bpo_subobjs,
1398d0475637SMatthew Ahrens 			    i * sizeof (subobj), sizeof (subobj), &subobj, 0));
1399d0475637SMatthew Ahrens 			error = bpobj_open(&subbpo, bpo->bpo_os, subobj);
1400d0475637SMatthew Ahrens 			if (error != 0) {
1401d0475637SMatthew Ahrens 				(void) printf("ERROR %u while trying to open "
1402d0475637SMatthew Ahrens 				    "subobj id %llu\n",
1403d0475637SMatthew Ahrens 				    error, (u_longlong_t)subobj);
1404d0475637SMatthew Ahrens 				continue;
1405d0475637SMatthew Ahrens 			}
1406d0475637SMatthew Ahrens 			dump_bpobj(&subbpo, "subobj", indent + 1);
140777061867SMatthew Ahrens 			bpobj_close(&subbpo);
1408d0475637SMatthew Ahrens 		}
140999653d4eSeschrock 	} else {
1410d0475637SMatthew Ahrens 		(void) printf("    %*s: object %llu, %llu blkptrs, %s\n",
1411d0475637SMatthew Ahrens 		    indent * 8, name,
1412d0475637SMatthew Ahrens 		    (u_longlong_t)bpo->bpo_object,
1413d0475637SMatthew Ahrens 		    (u_longlong_t)bpo->bpo_phys->bpo_num_blkptrs,
1414d0475637SMatthew Ahrens 		    bytes);
141599653d4eSeschrock 	}
1416fa9e4066Sahrens 
1417cde58dbcSMatthew Ahrens 	if (dump_opt['d'] < 5)
1418fa9e4066Sahrens 		return;
1419fa9e4066Sahrens 
1420fa9e4066Sahrens 
1421d0475637SMatthew Ahrens 	if (indent == 0) {
1422d0475637SMatthew Ahrens 		(void) bpobj_iterate_nofree(bpo, dump_bpobj_cb, NULL, NULL);
1423d0475637SMatthew Ahrens 		(void) printf("\n");
1424d0475637SMatthew Ahrens 	}
1425cde58dbcSMatthew Ahrens }
142644cd46caSbillm 
1427cde58dbcSMatthew Ahrens static void
1428cde58dbcSMatthew Ahrens dump_deadlist(dsl_deadlist_t *dl)
1429cde58dbcSMatthew Ahrens {
1430cde58dbcSMatthew Ahrens 	dsl_deadlist_entry_t *dle;
1431d0475637SMatthew Ahrens 	uint64_t unused;
1432cde58dbcSMatthew Ahrens 	char bytes[32];
1433cde58dbcSMatthew Ahrens 	char comp[32];
1434cde58dbcSMatthew Ahrens 	char uncomp[32];
1435cde58dbcSMatthew Ahrens 
1436cde58dbcSMatthew Ahrens 	if (dump_opt['d'] < 3)
1437cde58dbcSMatthew Ahrens 		return;
1438cde58dbcSMatthew Ahrens 
143990c76c66SMatthew Ahrens 	if (dl->dl_oldfmt) {
144090c76c66SMatthew Ahrens 		dump_bpobj(&dl->dl_bpobj, "old-format deadlist", 0);
144190c76c66SMatthew Ahrens 		return;
144290c76c66SMatthew Ahrens 	}
144390c76c66SMatthew Ahrens 
1444cde58dbcSMatthew Ahrens 	zdb_nicenum(dl->dl_phys->dl_used, bytes);
1445cde58dbcSMatthew Ahrens 	zdb_nicenum(dl->dl_phys->dl_comp, comp);
1446cde58dbcSMatthew Ahrens 	zdb_nicenum(dl->dl_phys->dl_uncomp, uncomp);
1447cde58dbcSMatthew Ahrens 	(void) printf("\n    Deadlist: %s (%s/%s comp)\n",
1448cde58dbcSMatthew Ahrens 	    bytes, comp, uncomp);
1449cde58dbcSMatthew Ahrens 
1450cde58dbcSMatthew Ahrens 	if (dump_opt['d'] < 4)
1451cde58dbcSMatthew Ahrens 		return;
1452cde58dbcSMatthew Ahrens 
1453cde58dbcSMatthew Ahrens 	(void) printf("\n");
1454cde58dbcSMatthew Ahrens 
1455d0475637SMatthew Ahrens 	/* force the tree to be loaded */
1456d0475637SMatthew Ahrens 	dsl_deadlist_space_range(dl, 0, UINT64_MAX, &unused, &unused, &unused);
1457d0475637SMatthew Ahrens 
1458cde58dbcSMatthew Ahrens 	for (dle = avl_first(&dl->dl_tree); dle;
1459cde58dbcSMatthew Ahrens 	    dle = AVL_NEXT(&dl->dl_tree, dle)) {
1460d0475637SMatthew Ahrens 		if (dump_opt['d'] >= 5) {
1461d0475637SMatthew Ahrens 			char buf[128];
1462d0475637SMatthew Ahrens 			(void) snprintf(buf, sizeof (buf), "mintxg %llu -> ",
1463d0475637SMatthew Ahrens 			    (longlong_t)dle->dle_mintxg,
1464d0475637SMatthew Ahrens 			    (longlong_t)dle->dle_bpobj.bpo_object);
1465fa9e4066Sahrens 
1466d0475637SMatthew Ahrens 			dump_bpobj(&dle->dle_bpobj, buf, 0);
1467d0475637SMatthew Ahrens 		} else {
1468d0475637SMatthew Ahrens 			(void) printf("mintxg %llu -> obj %llu\n",
1469d0475637SMatthew Ahrens 			    (longlong_t)dle->dle_mintxg,
1470d0475637SMatthew Ahrens 			    (longlong_t)dle->dle_bpobj.bpo_object);
1471d0475637SMatthew Ahrens 
1472d0475637SMatthew Ahrens 		}
1473cde58dbcSMatthew Ahrens 	}
1474fa9e4066Sahrens }
1475fa9e4066Sahrens 
1476e0d35c44Smarks static avl_tree_t idx_tree;
1477e0d35c44Smarks static avl_tree_t domain_tree;
1478e0d35c44Smarks static boolean_t fuid_table_loaded;
14790a586ceaSMark Shellenbaum static boolean_t sa_loaded;
14800a586ceaSMark Shellenbaum sa_attr_type_t *sa_attr_table;
1481e0d35c44Smarks 
1482e0d35c44Smarks static void
1483e0d35c44Smarks fuid_table_destroy()
1484e0d35c44Smarks {
1485e0d35c44Smarks 	if (fuid_table_loaded) {
1486e0d35c44Smarks 		zfs_fuid_table_destroy(&idx_tree, &domain_tree);
1487e0d35c44Smarks 		fuid_table_loaded = B_FALSE;
1488e0d35c44Smarks 	}
1489e0d35c44Smarks }
1490e0d35c44Smarks 
1491e0d35c44Smarks /*
1492e0d35c44Smarks  * print uid or gid information.
1493e0d35c44Smarks  * For normal POSIX id just the id is printed in decimal format.
1494e0d35c44Smarks  * For CIFS files with FUID the fuid is printed in hex followed by
1495d0475637SMatthew Ahrens  * the domain-rid string.
1496e0d35c44Smarks  */
1497e0d35c44Smarks static void
1498e0d35c44Smarks print_idstr(uint64_t id, const char *id_type)
1499e0d35c44Smarks {
1500e0d35c44Smarks 	if (FUID_INDEX(id)) {
1501e0d35c44Smarks 		char *domain;
1502e0d35c44Smarks 
1503e0d35c44Smarks 		domain = zfs_fuid_idx_domain(&idx_tree, FUID_INDEX(id));
1504e0d35c44Smarks 		(void) printf("\t%s     %llx [%s-%d]\n", id_type,
1505e0d35c44Smarks 		    (u_longlong_t)id, domain, (int)FUID_RID(id));
1506e0d35c44Smarks 	} else {
1507e0d35c44Smarks 		(void) printf("\t%s     %llu\n", id_type, (u_longlong_t)id);
1508e0d35c44Smarks 	}
1509e0d35c44Smarks 
1510e0d35c44Smarks }
1511e0d35c44Smarks 
1512e0d35c44Smarks static void
15130a586ceaSMark Shellenbaum dump_uidgid(objset_t *os, uint64_t uid, uint64_t gid)
1514e0d35c44Smarks {
1515e0d35c44Smarks 	uint32_t uid_idx, gid_idx;
1516e0d35c44Smarks 
15170a586ceaSMark Shellenbaum 	uid_idx = FUID_INDEX(uid);
15180a586ceaSMark Shellenbaum 	gid_idx = FUID_INDEX(gid);
1519e0d35c44Smarks 
1520e0d35c44Smarks 	/* Load domain table, if not already loaded */
1521e0d35c44Smarks 	if (!fuid_table_loaded && (uid_idx || gid_idx)) {
1522e0d35c44Smarks 		uint64_t fuid_obj;
1523e0d35c44Smarks 
1524e0d35c44Smarks 		/* first find the fuid object.  It lives in the master node */
1525e0d35c44Smarks 		VERIFY(zap_lookup(os, MASTER_NODE_OBJ, ZFS_FUID_TABLES,
1526e0d35c44Smarks 		    8, 1, &fuid_obj) == 0);
152789459e17SMark Shellenbaum 		zfs_fuid_avl_tree_create(&idx_tree, &domain_tree);
1528e0d35c44Smarks 		(void) zfs_fuid_table_load(os, fuid_obj,
1529e0d35c44Smarks 		    &idx_tree, &domain_tree);
1530e0d35c44Smarks 		fuid_table_loaded = B_TRUE;
1531e0d35c44Smarks 	}
1532e0d35c44Smarks 
15330a586ceaSMark Shellenbaum 	print_idstr(uid, "uid");
15340a586ceaSMark Shellenbaum 	print_idstr(gid, "gid");
1535e0d35c44Smarks }
1536e0d35c44Smarks 
1537fa9e4066Sahrens /*ARGSUSED*/
1538fa9e4066Sahrens static void
1539fa9e4066Sahrens dump_znode(objset_t *os, uint64_t object, void *data, size_t size)
1540fa9e4066Sahrens {
1541fa9e4066Sahrens 	char path[MAXPATHLEN * 2];	/* allow for xattr and failure prefix */
15420a586ceaSMark Shellenbaum 	sa_handle_t *hdl;
15430a586ceaSMark Shellenbaum 	uint64_t xattr, rdev, gen;
15440a586ceaSMark Shellenbaum 	uint64_t uid, gid, mode, fsize, parent, links;
15458f2529deSMark Shellenbaum 	uint64_t pflags;
15460a586ceaSMark Shellenbaum 	uint64_t acctm[2], modtm[2], chgtm[2], crtm[2];
15470a586ceaSMark Shellenbaum 	time_t z_crtime, z_atime, z_mtime, z_ctime;
15488f2529deSMark Shellenbaum 	sa_bulk_attr_t bulk[12];
15490a586ceaSMark Shellenbaum 	int idx = 0;
155055434c77Sek 	int error;
1551fa9e4066Sahrens 
15520a586ceaSMark Shellenbaum 	if (!sa_loaded) {
15530a586ceaSMark Shellenbaum 		uint64_t sa_attrs = 0;
15540a586ceaSMark Shellenbaum 		uint64_t version;
15550a586ceaSMark Shellenbaum 
15560a586ceaSMark Shellenbaum 		VERIFY(zap_lookup(os, MASTER_NODE_OBJ, ZPL_VERSION_STR,
15570a586ceaSMark Shellenbaum 		    8, 1, &version) == 0);
15580a586ceaSMark Shellenbaum 		if (version >= ZPL_VERSION_SA) {
15590a586ceaSMark Shellenbaum 			VERIFY(zap_lookup(os, MASTER_NODE_OBJ, ZFS_SA_ATTRS,
15600a586ceaSMark Shellenbaum 			    8, 1, &sa_attrs) == 0);
15610a586ceaSMark Shellenbaum 		}
15621d8ccc7bSMark Shellenbaum 		if ((error = sa_setup(os, sa_attrs, zfs_attr_table,
15631d8ccc7bSMark Shellenbaum 		    ZPL_END, &sa_attr_table)) != 0) {
15641d8ccc7bSMark Shellenbaum 			(void) printf("sa_setup failed errno %d, can't "
15651d8ccc7bSMark Shellenbaum 			    "display znode contents\n", error);
15661d8ccc7bSMark Shellenbaum 			return;
15671d8ccc7bSMark Shellenbaum 		}
15680a586ceaSMark Shellenbaum 		sa_loaded = B_TRUE;
15690a586ceaSMark Shellenbaum 	}
15700a586ceaSMark Shellenbaum 
15710a586ceaSMark Shellenbaum 	if (sa_handle_get(os, object, NULL, SA_HDL_PRIVATE, &hdl)) {
15720a586ceaSMark Shellenbaum 		(void) printf("Failed to get handle for SA znode\n");
15730a586ceaSMark Shellenbaum 		return;
15740a586ceaSMark Shellenbaum 	}
15750a586ceaSMark Shellenbaum 
15760a586ceaSMark Shellenbaum 	SA_ADD_BULK_ATTR(bulk, idx, sa_attr_table[ZPL_UID], NULL, &uid, 8);
15770a586ceaSMark Shellenbaum 	SA_ADD_BULK_ATTR(bulk, idx, sa_attr_table[ZPL_GID], NULL, &gid, 8);
15780a586ceaSMark Shellenbaum 	SA_ADD_BULK_ATTR(bulk, idx, sa_attr_table[ZPL_LINKS], NULL,
15790a586ceaSMark Shellenbaum 	    &links, 8);
15800a586ceaSMark Shellenbaum 	SA_ADD_BULK_ATTR(bulk, idx, sa_attr_table[ZPL_GEN], NULL, &gen, 8);
15810a586ceaSMark Shellenbaum 	SA_ADD_BULK_ATTR(bulk, idx, sa_attr_table[ZPL_MODE], NULL,
15820a586ceaSMark Shellenbaum 	    &mode, 8);
15830a586ceaSMark Shellenbaum 	SA_ADD_BULK_ATTR(bulk, idx, sa_attr_table[ZPL_PARENT],
15840a586ceaSMark Shellenbaum 	    NULL, &parent, 8);
15850a586ceaSMark Shellenbaum 	SA_ADD_BULK_ATTR(bulk, idx, sa_attr_table[ZPL_SIZE], NULL,
15860a586ceaSMark Shellenbaum 	    &fsize, 8);
15870a586ceaSMark Shellenbaum 	SA_ADD_BULK_ATTR(bulk, idx, sa_attr_table[ZPL_ATIME], NULL,
15880a586ceaSMark Shellenbaum 	    acctm, 16);
15890a586ceaSMark Shellenbaum 	SA_ADD_BULK_ATTR(bulk, idx, sa_attr_table[ZPL_MTIME], NULL,
15900a586ceaSMark Shellenbaum 	    modtm, 16);
15910a586ceaSMark Shellenbaum 	SA_ADD_BULK_ATTR(bulk, idx, sa_attr_table[ZPL_CRTIME], NULL,
15920a586ceaSMark Shellenbaum 	    crtm, 16);
15930a586ceaSMark Shellenbaum 	SA_ADD_BULK_ATTR(bulk, idx, sa_attr_table[ZPL_CTIME], NULL,
15940a586ceaSMark Shellenbaum 	    chgtm, 16);
15958f2529deSMark Shellenbaum 	SA_ADD_BULK_ATTR(bulk, idx, sa_attr_table[ZPL_FLAGS], NULL,
15968f2529deSMark Shellenbaum 	    &pflags, 8);
15970a586ceaSMark Shellenbaum 
15980a586ceaSMark Shellenbaum 	if (sa_bulk_lookup(hdl, bulk, idx)) {
15990a586ceaSMark Shellenbaum 		(void) sa_handle_destroy(hdl);
16000a586ceaSMark Shellenbaum 		return;
16010a586ceaSMark Shellenbaum 	}
1602fa9e4066Sahrens 
160355434c77Sek 	error = zfs_obj_to_path(os, object, path, sizeof (path));
160455434c77Sek 	if (error != 0) {
160555434c77Sek 		(void) snprintf(path, sizeof (path), "\?\?\?<object#%llu>",
160655434c77Sek 		    (u_longlong_t)object);
160755434c77Sek 	}
1608fa9e4066Sahrens 	if (dump_opt['d'] < 3) {
160955434c77Sek 		(void) printf("\t%s\n", path);
16100a586ceaSMark Shellenbaum 		(void) sa_handle_destroy(hdl);
1611fa9e4066Sahrens 		return;
1612fa9e4066Sahrens 	}
1613fa9e4066Sahrens 
16140a586ceaSMark Shellenbaum 	z_crtime = (time_t)crtm[0];
16150a586ceaSMark Shellenbaum 	z_atime = (time_t)acctm[0];
16160a586ceaSMark Shellenbaum 	z_mtime = (time_t)modtm[0];
16170a586ceaSMark Shellenbaum 	z_ctime = (time_t)chgtm[0];
1618fa9e4066Sahrens 
161955434c77Sek 	(void) printf("\tpath	%s\n", path);
16200a586ceaSMark Shellenbaum 	dump_uidgid(os, uid, gid);
1621fa9e4066Sahrens 	(void) printf("\tatime	%s", ctime(&z_atime));
1622fa9e4066Sahrens 	(void) printf("\tmtime	%s", ctime(&z_mtime));
1623fa9e4066Sahrens 	(void) printf("\tctime	%s", ctime(&z_ctime));
1624fa9e4066Sahrens 	(void) printf("\tcrtime	%s", ctime(&z_crtime));
16250a586ceaSMark Shellenbaum 	(void) printf("\tgen	%llu\n", (u_longlong_t)gen);
16260a586ceaSMark Shellenbaum 	(void) printf("\tmode	%llo\n", (u_longlong_t)mode);
16270a586ceaSMark Shellenbaum 	(void) printf("\tsize	%llu\n", (u_longlong_t)fsize);
16280a586ceaSMark Shellenbaum 	(void) printf("\tparent	%llu\n", (u_longlong_t)parent);
16290a586ceaSMark Shellenbaum 	(void) printf("\tlinks	%llu\n", (u_longlong_t)links);
16308f2529deSMark Shellenbaum 	(void) printf("\tpflags	%llx\n", (u_longlong_t)pflags);
16310a586ceaSMark Shellenbaum 	if (sa_lookup(hdl, sa_attr_table[ZPL_XATTR], &xattr,
16320a586ceaSMark Shellenbaum 	    sizeof (uint64_t)) == 0)
16330a586ceaSMark Shellenbaum 		(void) printf("\txattr	%llu\n", (u_longlong_t)xattr);
16340a586ceaSMark Shellenbaum 	if (sa_lookup(hdl, sa_attr_table[ZPL_RDEV], &rdev,
16350a586ceaSMark Shellenbaum 	    sizeof (uint64_t)) == 0)
16360a586ceaSMark Shellenbaum 		(void) printf("\trdev	0x%016llx\n", (u_longlong_t)rdev);
16370a586ceaSMark Shellenbaum 	sa_handle_destroy(hdl);
1638fa9e4066Sahrens }
1639fa9e4066Sahrens 
1640fa9e4066Sahrens /*ARGSUSED*/
1641fa9e4066Sahrens static void
1642fa9e4066Sahrens dump_acl(objset_t *os, uint64_t object, void *data, size_t size)
1643fa9e4066Sahrens {
1644fa9e4066Sahrens }
1645fa9e4066Sahrens 
1646fa9e4066Sahrens /*ARGSUSED*/
1647fa9e4066Sahrens static void
1648fa9e4066Sahrens dump_dmu_objset(objset_t *os, uint64_t object, void *data, size_t size)
1649fa9e4066Sahrens {
1650fa9e4066Sahrens }
1651fa9e4066Sahrens 
16526de8f417SVictor Latushkin static object_viewer_t *object_viewer[DMU_OT_NUMTYPES + 1] = {
1653fa9e4066Sahrens 	dump_none,		/* unallocated			*/
1654fa9e4066Sahrens 	dump_zap,		/* object directory		*/
1655fa9e4066Sahrens 	dump_uint64,		/* object array			*/
1656fa9e4066Sahrens 	dump_none,		/* packed nvlist		*/
1657fa9e4066Sahrens 	dump_packed_nvlist,	/* packed nvlist size		*/
1658fa9e4066Sahrens 	dump_none,		/* bplist			*/
1659fa9e4066Sahrens 	dump_none,		/* bplist header		*/
1660fa9e4066Sahrens 	dump_none,		/* SPA space map header		*/
1661fa9e4066Sahrens 	dump_none,		/* SPA space map		*/
1662fa9e4066Sahrens 	dump_none,		/* ZIL intent log		*/
1663fa9e4066Sahrens 	dump_dnode,		/* DMU dnode			*/
1664fa9e4066Sahrens 	dump_dmu_objset,	/* DMU objset			*/
1665ea8dc4b6Seschrock 	dump_dsl_dir,		/* DSL directory		*/
1666fa9e4066Sahrens 	dump_zap,		/* DSL directory child map	*/
1667fa9e4066Sahrens 	dump_zap,		/* DSL dataset snap map		*/
1668fa9e4066Sahrens 	dump_zap,		/* DSL props			*/
1669fa9e4066Sahrens 	dump_dsl_dataset,	/* DSL dataset			*/
1670fa9e4066Sahrens 	dump_znode,		/* ZFS znode			*/
1671da6c28aaSamw 	dump_acl,		/* ZFS V0 ACL			*/
1672fa9e4066Sahrens 	dump_uint8,		/* ZFS plain file		*/
1673e7437265Sahrens 	dump_zpldir,		/* ZFS directory		*/
1674fa9e4066Sahrens 	dump_zap,		/* ZFS master node		*/
1675fa9e4066Sahrens 	dump_zap,		/* ZFS delete queue		*/
1676fa9e4066Sahrens 	dump_uint8,		/* zvol object			*/
1677fa9e4066Sahrens 	dump_zap,		/* zvol prop			*/
1678fa9e4066Sahrens 	dump_uint8,		/* other uint8[]		*/
1679fa9e4066Sahrens 	dump_uint64,		/* other uint64[]		*/
1680fa9e4066Sahrens 	dump_zap,		/* other ZAP			*/
1681ea8dc4b6Seschrock 	dump_zap,		/* persistent error log		*/
168206eeb2adSek 	dump_uint8,		/* SPA history			*/
16834445fffbSMatthew Ahrens 	dump_history_offsets,	/* SPA history offsets		*/
1684b1b8ab34Slling 	dump_zap,		/* Pool properties		*/
1685ecd6cf80Smarks 	dump_zap,		/* DSL permissions		*/
1686da6c28aaSamw 	dump_acl,		/* ZFS ACL			*/
1687da6c28aaSamw 	dump_uint8,		/* ZFS SYSACL			*/
1688da6c28aaSamw 	dump_none,		/* FUID nvlist			*/
1689da6c28aaSamw 	dump_packed_nvlist,	/* FUID nvlist size		*/
1690088f3894Sahrens 	dump_zap,		/* DSL dataset next clones	*/
1691088f3894Sahrens 	dump_zap,		/* DSL scrub queue		*/
169214843421SMatthew Ahrens 	dump_zap,		/* ZFS user/group used		*/
169314843421SMatthew Ahrens 	dump_zap,		/* ZFS user/group quota		*/
1694842727c2SChris Kirby 	dump_zap,		/* snapshot refcount tags	*/
1695486ae710SMatthew Ahrens 	dump_ddt_zap,		/* DDT ZAP object		*/
1696b24ab676SJeff Bonwick 	dump_zap,		/* DDT statistics		*/
16970a586ceaSMark Shellenbaum 	dump_znode,		/* SA object			*/
16980a586ceaSMark Shellenbaum 	dump_zap,		/* SA Master Node		*/
16990a586ceaSMark Shellenbaum 	dump_sa_attrs,		/* SA attribute registration	*/
17000a586ceaSMark Shellenbaum 	dump_sa_layouts,	/* SA attribute layouts		*/
17013f9d6ad7SLin Ling 	dump_zap,		/* DSL scrub translations	*/
17023f9d6ad7SLin Ling 	dump_none,		/* fake dedup BP		*/
1703cde58dbcSMatthew Ahrens 	dump_zap,		/* deadlist			*/
1704cde58dbcSMatthew Ahrens 	dump_none,		/* deadlist hdr			*/
1705cde58dbcSMatthew Ahrens 	dump_zap,		/* dsl clones			*/
1706cde58dbcSMatthew Ahrens 	dump_none,		/* bpobj subobjs		*/
17070a586ceaSMark Shellenbaum 	dump_unknown,		/* Unknown type, must be last	*/
1708fa9e4066Sahrens };
1709fa9e4066Sahrens 
1710fa9e4066Sahrens static void
1711fa9e4066Sahrens dump_object(objset_t *os, uint64_t object, int verbosity, int *print_header)
1712fa9e4066Sahrens {
1713fa9e4066Sahrens 	dmu_buf_t *db = NULL;
1714fa9e4066Sahrens 	dmu_object_info_t doi;
1715fa9e4066Sahrens 	dnode_t *dn;
1716fa9e4066Sahrens 	void *bonus = NULL;
1717fa9e4066Sahrens 	size_t bsize = 0;
17183f9d6ad7SLin Ling 	char iblk[32], dblk[32], lsize[32], asize[32], fill[32];
17193f9d6ad7SLin Ling 	char bonus_size[32];
1720fa9e4066Sahrens 	char aux[50];
1721fa9e4066Sahrens 	int error;
1722fa9e4066Sahrens 
1723fa9e4066Sahrens 	if (*print_header) {
1724b24ab676SJeff Bonwick 		(void) printf("\n%10s  %3s  %5s  %5s  %5s  %5s  %6s  %s\n",
1725b24ab676SJeff Bonwick 		    "Object", "lvl", "iblk", "dblk", "dsize", "lsize",
1726b24ab676SJeff Bonwick 		    "%full", "type");
1727fa9e4066Sahrens 		*print_header = 0;
1728fa9e4066Sahrens 	}
1729fa9e4066Sahrens 
1730fa9e4066Sahrens 	if (object == 0) {
1731744947dcSTom Erickson 		dn = DMU_META_DNODE(os);
1732fa9e4066Sahrens 	} else {
1733ea8dc4b6Seschrock 		error = dmu_bonus_hold(os, object, FTAG, &db);
1734ea8dc4b6Seschrock 		if (error)
1735ea8dc4b6Seschrock 			fatal("dmu_bonus_hold(%llu) failed, errno %u",
1736ea8dc4b6Seschrock 			    object, error);
1737fa9e4066Sahrens 		bonus = db->db_data;
1738fa9e4066Sahrens 		bsize = db->db_size;
1739744947dcSTom Erickson 		dn = DB_DNODE((dmu_buf_impl_t *)db);
1740fa9e4066Sahrens 	}
1741fa9e4066Sahrens 	dmu_object_info_from_dnode(dn, &doi);
1742fa9e4066Sahrens 
17433f9d6ad7SLin Ling 	zdb_nicenum(doi.doi_metadata_block_size, iblk);
17443f9d6ad7SLin Ling 	zdb_nicenum(doi.doi_data_block_size, dblk);
17453f9d6ad7SLin Ling 	zdb_nicenum(doi.doi_max_offset, lsize);
17463f9d6ad7SLin Ling 	zdb_nicenum(doi.doi_physical_blocks_512 << 9, asize);
17473f9d6ad7SLin Ling 	zdb_nicenum(doi.doi_bonus_size, bonus_size);
1748b24ab676SJeff Bonwick 	(void) sprintf(fill, "%6.2f", 100.0 * doi.doi_fill_count *
1749bbfd46c4SJeff Bonwick 	    doi.doi_data_block_size / (object == 0 ? DNODES_PER_BLOCK : 1) /
1750bbfd46c4SJeff Bonwick 	    doi.doi_max_offset);
1751fa9e4066Sahrens 
1752fa9e4066Sahrens 	aux[0] = '\0';
1753fa9e4066Sahrens 
1754e7437265Sahrens 	if (doi.doi_checksum != ZIO_CHECKSUM_INHERIT || verbosity >= 6) {
1755fa9e4066Sahrens 		(void) snprintf(aux + strlen(aux), sizeof (aux), " (K=%s)",
17566de8f417SVictor Latushkin 		    ZDB_CHECKSUM_NAME(doi.doi_checksum));
1757e7437265Sahrens 	}
1758fa9e4066Sahrens 
1759e7437265Sahrens 	if (doi.doi_compress != ZIO_COMPRESS_INHERIT || verbosity >= 6) {
1760fa9e4066Sahrens 		(void) snprintf(aux + strlen(aux), sizeof (aux), " (Z=%s)",
17616de8f417SVictor Latushkin 		    ZDB_COMPRESS_NAME(doi.doi_compress));
1762e7437265Sahrens 	}
1763fa9e4066Sahrens 
1764b24ab676SJeff Bonwick 	(void) printf("%10lld  %3u  %5s  %5s  %5s  %5s  %6s  %s%s\n",
1765b24ab676SJeff Bonwick 	    (u_longlong_t)object, doi.doi_indirection, iblk, dblk,
1766b24ab676SJeff Bonwick 	    asize, lsize, fill, ZDB_OT_NAME(doi.doi_type), aux);
1767fa9e4066Sahrens 
1768fa9e4066Sahrens 	if (doi.doi_bonus_type != DMU_OT_NONE && verbosity > 3) {
1769b24ab676SJeff Bonwick 		(void) printf("%10s  %3s  %5s  %5s  %5s  %5s  %6s  %s\n",
1770b24ab676SJeff Bonwick 		    "", "", "", "", "", bonus_size, "bonus",
17716de8f417SVictor Latushkin 		    ZDB_OT_NAME(doi.doi_bonus_type));
1772fa9e4066Sahrens 	}
1773fa9e4066Sahrens 
1774fa9e4066Sahrens 	if (verbosity >= 4) {
17750a586ceaSMark Shellenbaum 		(void) printf("\tdnode flags: %s%s%s\n",
177614843421SMatthew Ahrens 		    (dn->dn_phys->dn_flags & DNODE_FLAG_USED_BYTES) ?
177714843421SMatthew Ahrens 		    "USED_BYTES " : "",
177814843421SMatthew Ahrens 		    (dn->dn_phys->dn_flags & DNODE_FLAG_USERUSED_ACCOUNTED) ?
17790a586ceaSMark Shellenbaum 		    "USERUSED_ACCOUNTED " : "",
17800a586ceaSMark Shellenbaum 		    (dn->dn_phys->dn_flags & DNODE_FLAG_SPILL_BLKPTR) ?
17810a586ceaSMark Shellenbaum 		    "SPILL_BLKPTR" : "");
178214843421SMatthew Ahrens 		(void) printf("\tdnode maxblkid: %llu\n",
178314843421SMatthew Ahrens 		    (longlong_t)dn->dn_phys->dn_maxblkid);
178414843421SMatthew Ahrens 
17856de8f417SVictor Latushkin 		object_viewer[ZDB_OT_TYPE(doi.doi_bonus_type)](os, object,
17866de8f417SVictor Latushkin 		    bonus, bsize);
17876de8f417SVictor Latushkin 		object_viewer[ZDB_OT_TYPE(doi.doi_type)](os, object, NULL, 0);
1788fa9e4066Sahrens 		*print_header = 1;
1789fa9e4066Sahrens 	}
1790fa9e4066Sahrens 
1791fa9e4066Sahrens 	if (verbosity >= 5)
179288b7b0f2SMatthew Ahrens 		dump_indirect(dn);
1793fa9e4066Sahrens 
1794fa9e4066Sahrens 	if (verbosity >= 5) {
1795fa9e4066Sahrens 		/*
1796fa9e4066Sahrens 		 * Report the list of segments that comprise the object.
1797fa9e4066Sahrens 		 */
1798fa9e4066Sahrens 		uint64_t start = 0;
1799fa9e4066Sahrens 		uint64_t end;
1800fa9e4066Sahrens 		uint64_t blkfill = 1;
1801fa9e4066Sahrens 		int minlvl = 1;
1802fa9e4066Sahrens 
1803fa9e4066Sahrens 		if (dn->dn_type == DMU_OT_DNODE) {
1804fa9e4066Sahrens 			minlvl = 0;
1805fa9e4066Sahrens 			blkfill = DNODES_PER_BLOCK;
1806fa9e4066Sahrens 		}
1807fa9e4066Sahrens 
1808fa9e4066Sahrens 		for (;;) {
18093f9d6ad7SLin Ling 			char segsize[32];
1810cdb0ab79Smaybee 			error = dnode_next_offset(dn,
1811cdb0ab79Smaybee 			    0, &start, minlvl, blkfill, 0);
1812fa9e4066Sahrens 			if (error)
1813fa9e4066Sahrens 				break;
1814fa9e4066Sahrens 			end = start;
1815cdb0ab79Smaybee 			error = dnode_next_offset(dn,
1816cdb0ab79Smaybee 			    DNODE_FIND_HOLE, &end, minlvl, blkfill, 0);
18173f9d6ad7SLin Ling 			zdb_nicenum(end - start, segsize);
1818fa9e4066Sahrens 			(void) printf("\t\tsegment [%016llx, %016llx)"
1819fa9e4066Sahrens 			    " size %5s\n", (u_longlong_t)start,
1820fa9e4066Sahrens 			    (u_longlong_t)end, segsize);
1821fa9e4066Sahrens 			if (error)
1822fa9e4066Sahrens 				break;
1823fa9e4066Sahrens 			start = end;
1824fa9e4066Sahrens 		}
1825fa9e4066Sahrens 	}
1826fa9e4066Sahrens 
1827fa9e4066Sahrens 	if (db != NULL)
1828ea8dc4b6Seschrock 		dmu_buf_rele(db, FTAG);
1829fa9e4066Sahrens }
1830fa9e4066Sahrens 
1831fa9e4066Sahrens static char *objset_types[DMU_OST_NUMTYPES] = {
1832fa9e4066Sahrens 	"NONE", "META", "ZPL", "ZVOL", "OTHER", "ANY" };
1833fa9e4066Sahrens 
1834fa9e4066Sahrens static void
1835fa9e4066Sahrens dump_dir(objset_t *os)
1836fa9e4066Sahrens {
1837fa9e4066Sahrens 	dmu_objset_stats_t dds;
1838fa9e4066Sahrens 	uint64_t object, object_count;
1839a2eea2e1Sahrens 	uint64_t refdbytes, usedobjs, scratch;
18403f9d6ad7SLin Ling 	char numbuf[32];
184114843421SMatthew Ahrens 	char blkbuf[BP_SPRINTF_LEN + 20];
1842fa9e4066Sahrens 	char osname[MAXNAMELEN];
1843fa9e4066Sahrens 	char *type = "UNKNOWN";
1844fa9e4066Sahrens 	int verbosity = dump_opt['d'];
1845fa9e4066Sahrens 	int print_header = 1;
1846fa9e4066Sahrens 	int i, error;
1847fa9e4066Sahrens 
18483b2aab18SMatthew Ahrens 	dsl_pool_config_enter(dmu_objset_pool(os), FTAG);
1849a2eea2e1Sahrens 	dmu_objset_fast_stat(os, &dds);
18503b2aab18SMatthew Ahrens 	dsl_pool_config_exit(dmu_objset_pool(os), FTAG);
1851fa9e4066Sahrens 
1852fa9e4066Sahrens 	if (dds.dds_type < DMU_OST_NUMTYPES)
1853fa9e4066Sahrens 		type = objset_types[dds.dds_type];
1854fa9e4066Sahrens 
1855fa9e4066Sahrens 	if (dds.dds_type == DMU_OST_META) {
1856fa9e4066Sahrens 		dds.dds_creation_txg = TXG_INITIAL;
18575d7b4d43SMatthew Ahrens 		usedobjs = BP_GET_FILL(os->os_rootbp);
1858503ad85cSMatthew Ahrens 		refdbytes = os->os_spa->spa_dsl_pool->
185974e7dc98SMatthew Ahrens 		    dp_mos_dir->dd_phys->dd_used_bytes;
1860a2eea2e1Sahrens 	} else {
1861a2eea2e1Sahrens 		dmu_objset_space(os, &refdbytes, &scratch, &usedobjs, &scratch);
1862fa9e4066Sahrens 	}
1863fa9e4066Sahrens 
18645d7b4d43SMatthew Ahrens 	ASSERT3U(usedobjs, ==, BP_GET_FILL(os->os_rootbp));
1865fa9e4066Sahrens 
18663f9d6ad7SLin Ling 	zdb_nicenum(refdbytes, numbuf);
1867fa9e4066Sahrens 
1868fa9e4066Sahrens 	if (verbosity >= 4) {
186943466aaeSMax Grossman 		(void) snprintf(blkbuf, sizeof (blkbuf), ", rootbp ");
187043466aaeSMax Grossman 		(void) snprintf_blkptr(blkbuf + strlen(blkbuf),
187143466aaeSMax Grossman 		    sizeof (blkbuf) - strlen(blkbuf), os->os_rootbp);
1872fa9e4066Sahrens 	} else {
1873fa9e4066Sahrens 		blkbuf[0] = '\0';
1874fa9e4066Sahrens 	}
1875fa9e4066Sahrens 
1876fa9e4066Sahrens 	dmu_objset_name(os, osname);
1877fa9e4066Sahrens 
1878a2eea2e1Sahrens 	(void) printf("Dataset %s [%s], ID %llu, cr_txg %llu, "
1879fa9e4066Sahrens 	    "%s, %llu objects%s\n",
1880fa9e4066Sahrens 	    osname, type, (u_longlong_t)dmu_objset_id(os),
1881fa9e4066Sahrens 	    (u_longlong_t)dds.dds_creation_txg,
1882a2eea2e1Sahrens 	    numbuf, (u_longlong_t)usedobjs, blkbuf);
1883fa9e4066Sahrens 
1884b24ab676SJeff Bonwick 	if (zopt_objects != 0) {
1885b24ab676SJeff Bonwick 		for (i = 0; i < zopt_objects; i++)
1886b24ab676SJeff Bonwick 			dump_object(os, zopt_object[i], verbosity,
1887b24ab676SJeff Bonwick 			    &print_header);
1888b24ab676SJeff Bonwick 		(void) printf("\n");
1889b24ab676SJeff Bonwick 		return;
1890b24ab676SJeff Bonwick 	}
1891b24ab676SJeff Bonwick 
1892b24ab676SJeff Bonwick 	if (dump_opt['i'] != 0 || verbosity >= 2)
1893b24ab676SJeff Bonwick 		dump_intent_log(dmu_objset_zil(os));
1894fa9e4066Sahrens 
1895fa9e4066Sahrens 	if (dmu_objset_ds(os) != NULL)
1896cde58dbcSMatthew Ahrens 		dump_deadlist(&dmu_objset_ds(os)->ds_deadlist);
1897fa9e4066Sahrens 
1898fa9e4066Sahrens 	if (verbosity < 2)
1899fa9e4066Sahrens 		return;
1900fa9e4066Sahrens 
190143466aaeSMax Grossman 	if (BP_IS_HOLE(os->os_rootbp))
1902088f3894Sahrens 		return;
1903088f3894Sahrens 
1904fa9e4066Sahrens 	dump_object(os, 0, verbosity, &print_header);
190514843421SMatthew Ahrens 	object_count = 0;
1906744947dcSTom Erickson 	if (DMU_USERUSED_DNODE(os) != NULL &&
1907744947dcSTom Erickson 	    DMU_USERUSED_DNODE(os)->dn_type != 0) {
190814843421SMatthew Ahrens 		dump_object(os, DMU_USERUSED_OBJECT, verbosity, &print_header);
190914843421SMatthew Ahrens 		dump_object(os, DMU_GROUPUSED_OBJECT, verbosity, &print_header);
191014843421SMatthew Ahrens 	}
1911fa9e4066Sahrens 
1912fa9e4066Sahrens 	object = 0;
19136754306eSahrens 	while ((error = dmu_object_next(os, &object, B_FALSE, 0)) == 0) {
1914fa9e4066Sahrens 		dump_object(os, object, verbosity, &print_header);
1915fa9e4066Sahrens 		object_count++;
1916fa9e4066Sahrens 	}
1917fa9e4066Sahrens 
1918a2eea2e1Sahrens 	ASSERT3U(object_count, ==, usedobjs);
1919fa9e4066Sahrens 
1920fa9e4066Sahrens 	(void) printf("\n");
1921fa9e4066Sahrens 
1922ccba0801SRich Morris 	if (error != ESRCH) {
1923ccba0801SRich Morris 		(void) fprintf(stderr, "dmu_object_next() = %d\n", error);
1924ccba0801SRich Morris 		abort();
1925ccba0801SRich Morris 	}
1926fa9e4066Sahrens }
1927fa9e4066Sahrens 
1928fa9e4066Sahrens static void
192953b9a4a9SVictor Latushkin dump_uberblock(uberblock_t *ub, const char *header, const char *footer)
1930fa9e4066Sahrens {
1931fa9e4066Sahrens 	time_t timestamp = ub->ub_timestamp;
1932fa9e4066Sahrens 
193353b9a4a9SVictor Latushkin 	(void) printf(header ? header : "");
1934fa9e4066Sahrens 	(void) printf("\tmagic = %016llx\n", (u_longlong_t)ub->ub_magic);
1935fa9e4066Sahrens 	(void) printf("\tversion = %llu\n", (u_longlong_t)ub->ub_version);
1936fa9e4066Sahrens 	(void) printf("\ttxg = %llu\n", (u_longlong_t)ub->ub_txg);
1937fa9e4066Sahrens 	(void) printf("\tguid_sum = %llu\n", (u_longlong_t)ub->ub_guid_sum);
1938fa9e4066Sahrens 	(void) printf("\ttimestamp = %llu UTC = %s",
1939fa9e4066Sahrens 	    (u_longlong_t)ub->ub_timestamp, asctime(localtime(&timestamp)));
1940fa9e4066Sahrens 	if (dump_opt['u'] >= 3) {
1941fbabab8fSmaybee 		char blkbuf[BP_SPRINTF_LEN];
194243466aaeSMax Grossman 		snprintf_blkptr(blkbuf, sizeof (blkbuf), &ub->ub_rootbp);
1943fa9e4066Sahrens 		(void) printf("\trootbp = %s\n", blkbuf);
1944fa9e4066Sahrens 	}
194553b9a4a9SVictor Latushkin 	(void) printf(footer ? footer : "");
1946fa9e4066Sahrens }
1947fa9e4066Sahrens 
1948fa9e4066Sahrens static void
194907428bdfSVictor Latushkin dump_config(spa_t *spa)
1950fa9e4066Sahrens {
195107428bdfSVictor Latushkin 	dmu_buf_t *db;
195207428bdfSVictor Latushkin 	size_t nvsize = 0;
195307428bdfSVictor Latushkin 	int error = 0;
195407428bdfSVictor Latushkin 
1955fa9e4066Sahrens 
195607428bdfSVictor Latushkin 	error = dmu_bonus_hold(spa->spa_meta_objset,
195707428bdfSVictor Latushkin 	    spa->spa_config_object, FTAG, &db);
195807428bdfSVictor Latushkin 
195907428bdfSVictor Latushkin 	if (error == 0) {
196007428bdfSVictor Latushkin 		nvsize = *(uint64_t *)db->db_data;
196107428bdfSVictor Latushkin 		dmu_buf_rele(db, FTAG);
196207428bdfSVictor Latushkin 
196307428bdfSVictor Latushkin 		(void) printf("\nMOS Configuration:\n");
196407428bdfSVictor Latushkin 		dump_packed_nvlist(spa->spa_meta_objset,
196507428bdfSVictor Latushkin 		    spa->spa_config_object, (void *)&nvsize, 1);
196607428bdfSVictor Latushkin 	} else {
196707428bdfSVictor Latushkin 		(void) fprintf(stderr, "dmu_bonus_hold(%llu) failed, errno %d",
196807428bdfSVictor Latushkin 		    (u_longlong_t)spa->spa_config_object, error);
1969fa9e4066Sahrens 	}
1970fa9e4066Sahrens }
1971fa9e4066Sahrens 
1972c5904d13Seschrock static void
1973c5904d13Seschrock dump_cachefile(const char *cachefile)
1974c5904d13Seschrock {
1975c5904d13Seschrock 	int fd;
1976c5904d13Seschrock 	struct stat64 statbuf;
1977c5904d13Seschrock 	char *buf;
1978c5904d13Seschrock 	nvlist_t *config;
1979c5904d13Seschrock 
1980c5904d13Seschrock 	if ((fd = open64(cachefile, O_RDONLY)) < 0) {
1981c5904d13Seschrock 		(void) printf("cannot open '%s': %s\n", cachefile,
1982c5904d13Seschrock 		    strerror(errno));
1983c5904d13Seschrock 		exit(1);
1984c5904d13Seschrock 	}
1985c5904d13Seschrock 
1986c5904d13Seschrock 	if (fstat64(fd, &statbuf) != 0) {
1987c5904d13Seschrock 		(void) printf("failed to stat '%s': %s\n", cachefile,
1988c5904d13Seschrock 		    strerror(errno));
1989c5904d13Seschrock 		exit(1);
1990c5904d13Seschrock 	}
1991c5904d13Seschrock 
1992c5904d13Seschrock 	if ((buf = malloc(statbuf.st_size)) == NULL) {
1993c5904d13Seschrock 		(void) fprintf(stderr, "failed to allocate %llu bytes\n",
1994c5904d13Seschrock 		    (u_longlong_t)statbuf.st_size);
1995c5904d13Seschrock 		exit(1);
1996c5904d13Seschrock 	}
1997c5904d13Seschrock 
1998c5904d13Seschrock 	if (read(fd, buf, statbuf.st_size) != statbuf.st_size) {
1999c5904d13Seschrock 		(void) fprintf(stderr, "failed to read %llu bytes\n",
2000c5904d13Seschrock 		    (u_longlong_t)statbuf.st_size);
2001c5904d13Seschrock 		exit(1);
2002c5904d13Seschrock 	}
2003c5904d13Seschrock 
2004c5904d13Seschrock 	(void) close(fd);
2005c5904d13Seschrock 
2006c5904d13Seschrock 	if (nvlist_unpack(buf, statbuf.st_size, &config, 0) != 0) {
2007c5904d13Seschrock 		(void) fprintf(stderr, "failed to unpack nvlist\n");
2008c5904d13Seschrock 		exit(1);
2009c5904d13Seschrock 	}
2010c5904d13Seschrock 
2011c5904d13Seschrock 	free(buf);
2012c5904d13Seschrock 
2013c5904d13Seschrock 	dump_nvlist(config, 0);
2014c5904d13Seschrock 
2015c5904d13Seschrock 	nvlist_free(config);
2016c5904d13Seschrock }
2017c5904d13Seschrock 
201853b9a4a9SVictor Latushkin #define	ZDB_MAX_UB_HEADER_SIZE 32
201953b9a4a9SVictor Latushkin 
202053b9a4a9SVictor Latushkin static void
202153b9a4a9SVictor Latushkin dump_label_uberblocks(vdev_label_t *lbl, uint64_t ashift)
202253b9a4a9SVictor Latushkin {
202353b9a4a9SVictor Latushkin 	vdev_t vd;
202453b9a4a9SVictor Latushkin 	vdev_t *vdp = &vd;
202553b9a4a9SVictor Latushkin 	char header[ZDB_MAX_UB_HEADER_SIZE];
202653b9a4a9SVictor Latushkin 
202753b9a4a9SVictor Latushkin 	vd.vdev_ashift = ashift;
202853b9a4a9SVictor Latushkin 	vdp->vdev_top = vdp;
202953b9a4a9SVictor Latushkin 
203053b9a4a9SVictor Latushkin 	for (int i = 0; i < VDEV_UBERBLOCK_COUNT(vdp); i++) {
203153b9a4a9SVictor Latushkin 		uint64_t uoff = VDEV_UBERBLOCK_OFFSET(vdp, i);
203253b9a4a9SVictor Latushkin 		uberblock_t *ub = (void *)((char *)lbl + uoff);
203353b9a4a9SVictor Latushkin 
203453b9a4a9SVictor Latushkin 		if (uberblock_verify(ub))
203553b9a4a9SVictor Latushkin 			continue;
203653b9a4a9SVictor Latushkin 		(void) snprintf(header, ZDB_MAX_UB_HEADER_SIZE,
203753b9a4a9SVictor Latushkin 		    "Uberblock[%d]\n", i);
203853b9a4a9SVictor Latushkin 		dump_uberblock(ub, header, "");
203953b9a4a9SVictor Latushkin 	}
204053b9a4a9SVictor Latushkin }
204153b9a4a9SVictor Latushkin 
2042fa9e4066Sahrens static void
2043fa9e4066Sahrens dump_label(const char *dev)
2044fa9e4066Sahrens {
2045fa9e4066Sahrens 	int fd;
2046fa9e4066Sahrens 	vdev_label_t label;
2047c6065d0fSGeorge Wilson 	char *path, *buf = label.vl_vdev_phys.vp_nvlist;
2048fa9e4066Sahrens 	size_t buflen = sizeof (label.vl_vdev_phys.vp_nvlist);
2049fa9e4066Sahrens 	struct stat64 statbuf;
205053b9a4a9SVictor Latushkin 	uint64_t psize, ashift;
2051c6065d0fSGeorge Wilson 	int len = strlen(dev) + 1;
2052fa9e4066Sahrens 
2053c6065d0fSGeorge Wilson 	if (strncmp(dev, "/dev/dsk/", 9) == 0) {
2054c6065d0fSGeorge Wilson 		len++;
2055c6065d0fSGeorge Wilson 		path = malloc(len);
2056c6065d0fSGeorge Wilson 		(void) snprintf(path, len, "%s%s", "/dev/rdsk/", dev + 9);
2057c6065d0fSGeorge Wilson 	} else {
2058c6065d0fSGeorge Wilson 		path = strdup(dev);
2059c6065d0fSGeorge Wilson 	}
2060c6065d0fSGeorge Wilson 
2061c6065d0fSGeorge Wilson 	if ((fd = open64(path, O_RDONLY)) < 0) {
2062c6065d0fSGeorge Wilson 		(void) printf("cannot open '%s': %s\n", path, strerror(errno));
2063c6065d0fSGeorge Wilson 		free(path);
2064fa9e4066Sahrens 		exit(1);
2065fa9e4066Sahrens 	}
2066fa9e4066Sahrens 
2067fa9e4066Sahrens 	if (fstat64(fd, &statbuf) != 0) {
2068c6065d0fSGeorge Wilson 		(void) printf("failed to stat '%s': %s\n", path,
2069fa9e4066Sahrens 		    strerror(errno));
2070c6065d0fSGeorge Wilson 		free(path);
2071c6065d0fSGeorge Wilson 		(void) close(fd);
2072c6065d0fSGeorge Wilson 		exit(1);
2073c6065d0fSGeorge Wilson 	}
2074c6065d0fSGeorge Wilson 
2075c6065d0fSGeorge Wilson 	if (S_ISBLK(statbuf.st_mode)) {
2076c6065d0fSGeorge Wilson 		(void) printf("cannot use '%s': character device required\n",
2077c6065d0fSGeorge Wilson 		    path);
2078c6065d0fSGeorge Wilson 		free(path);
2079c6065d0fSGeorge Wilson 		(void) close(fd);
2080c6065d0fSGeorge Wilson 		exit(1);
2081fa9e4066Sahrens 	}
2082fa9e4066Sahrens 
2083fa9e4066Sahrens 	psize = statbuf.st_size;
2084fa9e4066Sahrens 	psize = P2ALIGN(psize, (uint64_t)sizeof (vdev_label_t));
2085fa9e4066Sahrens 
208653b9a4a9SVictor Latushkin 	for (int l = 0; l < VDEV_LABELS; l++) {
2087fa9e4066Sahrens 		nvlist_t *config = NULL;
2088fa9e4066Sahrens 
2089fa9e4066Sahrens 		(void) printf("--------------------------------------------\n");
2090fa9e4066Sahrens 		(void) printf("LABEL %d\n", l);
2091fa9e4066Sahrens 		(void) printf("--------------------------------------------\n");
2092fa9e4066Sahrens 
20930d981225Seschrock 		if (pread64(fd, &label, sizeof (label),
2094fa9e4066Sahrens 		    vdev_label_offset(psize, l, 0)) != sizeof (label)) {
2095fa9e4066Sahrens 			(void) printf("failed to read label %d\n", l);
2096fa9e4066Sahrens 			continue;
2097fa9e4066Sahrens 		}
2098fa9e4066Sahrens 
2099fa9e4066Sahrens 		if (nvlist_unpack(buf, buflen, &config, 0) != 0) {
2100fa9e4066Sahrens 			(void) printf("failed to unpack label %d\n", l);
210153b9a4a9SVictor Latushkin 			ashift = SPA_MINBLOCKSHIFT;
210253b9a4a9SVictor Latushkin 		} else {
210353b9a4a9SVictor Latushkin 			nvlist_t *vdev_tree = NULL;
210453b9a4a9SVictor Latushkin 
210553b9a4a9SVictor Latushkin 			dump_nvlist(config, 4);
210653b9a4a9SVictor Latushkin 			if ((nvlist_lookup_nvlist(config,
210753b9a4a9SVictor Latushkin 			    ZPOOL_CONFIG_VDEV_TREE, &vdev_tree) != 0) ||
210853b9a4a9SVictor Latushkin 			    (nvlist_lookup_uint64(vdev_tree,
210953b9a4a9SVictor Latushkin 			    ZPOOL_CONFIG_ASHIFT, &ashift) != 0))
211053b9a4a9SVictor Latushkin 				ashift = SPA_MINBLOCKSHIFT;
211153b9a4a9SVictor Latushkin 			nvlist_free(config);
2112fa9e4066Sahrens 		}
211353b9a4a9SVictor Latushkin 		if (dump_opt['u'])
211453b9a4a9SVictor Latushkin 			dump_label_uberblocks(&label, ashift);
2115fa9e4066Sahrens 	}
2116c6065d0fSGeorge Wilson 
2117c6065d0fSGeorge Wilson 	free(path);
2118c6065d0fSGeorge Wilson 	(void) close(fd);
2119fa9e4066Sahrens }
2120fa9e4066Sahrens 
2121fa9e4066Sahrens /*ARGSUSED*/
21221d452cf5Sahrens static int
2123fd136879SMatthew Ahrens dump_one_dir(const char *dsname, void *arg)
2124fa9e4066Sahrens {
2125fa9e4066Sahrens 	int error;
2126fa9e4066Sahrens 	objset_t *os;
2127fa9e4066Sahrens 
2128503ad85cSMatthew Ahrens 	error = dmu_objset_own(dsname, DMU_OST_ANY, B_TRUE, FTAG, &os);
2129fa9e4066Sahrens 	if (error) {
2130b24ab676SJeff Bonwick 		(void) printf("Could not open %s, error %d\n", dsname, error);
21311d452cf5Sahrens 		return (0);
2132fa9e4066Sahrens 	}
2133fa9e4066Sahrens 	dump_dir(os);
2134503ad85cSMatthew Ahrens 	dmu_objset_disown(os, FTAG);
2135e0d35c44Smarks 	fuid_table_destroy();
21360a586ceaSMark Shellenbaum 	sa_loaded = B_FALSE;
21371d452cf5Sahrens 	return (0);
2138fa9e4066Sahrens }
2139fa9e4066Sahrens 
2140fa9e4066Sahrens /*
2141b24ab676SJeff Bonwick  * Block statistics.
2142fa9e4066Sahrens  */
2143490d05b9SMatthew Ahrens #define	PSIZE_HISTO_SIZE (SPA_MAXBLOCKSIZE / SPA_MINBLOCKSIZE + 1)
2144fa9e4066Sahrens typedef struct zdb_blkstats {
2145490d05b9SMatthew Ahrens 	uint64_t zb_asize;
2146490d05b9SMatthew Ahrens 	uint64_t zb_lsize;
2147490d05b9SMatthew Ahrens 	uint64_t zb_psize;
2148490d05b9SMatthew Ahrens 	uint64_t zb_count;
2149d5ee8a13SMatthew Ahrens 	uint64_t zb_gangs;
2150d5ee8a13SMatthew Ahrens 	uint64_t zb_ditto_samevdev;
2151490d05b9SMatthew Ahrens 	uint64_t zb_psize_histogram[PSIZE_HISTO_SIZE];
2152fa9e4066Sahrens } zdb_blkstats_t;
2153fa9e4066Sahrens 
2154b24ab676SJeff Bonwick /*
2155b24ab676SJeff Bonwick  * Extended object types to report deferred frees and dedup auto-ditto blocks.
2156b24ab676SJeff Bonwick  */
2157b24ab676SJeff Bonwick #define	ZDB_OT_DEFERRED	(DMU_OT_NUMTYPES + 0)
2158b24ab676SJeff Bonwick #define	ZDB_OT_DITTO	(DMU_OT_NUMTYPES + 1)
2159ad135b5dSChristopher Siden #define	ZDB_OT_OTHER	(DMU_OT_NUMTYPES + 2)
2160ad135b5dSChristopher Siden #define	ZDB_OT_TOTAL	(DMU_OT_NUMTYPES + 3)
2161b24ab676SJeff Bonwick 
2162b24ab676SJeff Bonwick static char *zdb_ot_extname[] = {
2163b24ab676SJeff Bonwick 	"deferred free",
2164b24ab676SJeff Bonwick 	"dedup ditto",
2165ad135b5dSChristopher Siden 	"other",
2166b24ab676SJeff Bonwick 	"Total",
2167b24ab676SJeff Bonwick };
2168fa9e4066Sahrens 
216988b7b0f2SMatthew Ahrens #define	ZB_TOTAL	DN_MAX_LEVELS
2170fa9e4066Sahrens 
2171fa9e4066Sahrens typedef struct zdb_cb {
2172b24ab676SJeff Bonwick 	zdb_blkstats_t	zcb_type[ZB_TOTAL + 1][ZDB_OT_TOTAL + 1];
2173b24ab676SJeff Bonwick 	uint64_t	zcb_dedup_asize;
2174b24ab676SJeff Bonwick 	uint64_t	zcb_dedup_blocks;
21755d7b4d43SMatthew Ahrens 	uint64_t	zcb_embedded_blocks[NUM_BP_EMBEDDED_TYPES];
21765d7b4d43SMatthew Ahrens 	uint64_t	zcb_embedded_histogram[NUM_BP_EMBEDDED_TYPES]
21775d7b4d43SMatthew Ahrens 	    [BPE_PAYLOAD_SIZE];
2178490d05b9SMatthew Ahrens 	uint64_t	zcb_start;
2179490d05b9SMatthew Ahrens 	uint64_t	zcb_lastprint;
2180490d05b9SMatthew Ahrens 	uint64_t	zcb_totalasize;
2181fa9e4066Sahrens 	uint64_t	zcb_errors[256];
2182fa9e4066Sahrens 	int		zcb_readfails;
2183fa9e4066Sahrens 	int		zcb_haderrors;
2184cde58dbcSMatthew Ahrens 	spa_t		*zcb_spa;
2185fa9e4066Sahrens } zdb_cb_t;
2186fa9e4066Sahrens 
2187fa9e4066Sahrens static void
2188cde58dbcSMatthew Ahrens zdb_count_block(zdb_cb_t *zcb, zilog_t *zilog, const blkptr_t *bp,
2189b24ab676SJeff Bonwick     dmu_object_type_t type)
2190fa9e4066Sahrens {
2191b24ab676SJeff Bonwick 	uint64_t refcnt = 0;
2192b24ab676SJeff Bonwick 
2193b24ab676SJeff Bonwick 	ASSERT(type < ZDB_OT_TOTAL);
2194b24ab676SJeff Bonwick 
2195b24ab676SJeff Bonwick 	if (zilog && zil_bp_tree_add(zilog, bp) != 0)
2196b24ab676SJeff Bonwick 		return;
2197b24ab676SJeff Bonwick 
2198e14bb325SJeff Bonwick 	for (int i = 0; i < 4; i++) {
2199fa9e4066Sahrens 		int l = (i < 2) ? BP_GET_LEVEL(bp) : ZB_TOTAL;
2200b24ab676SJeff Bonwick 		int t = (i & 1) ? type : ZDB_OT_TOTAL;
2201d5ee8a13SMatthew Ahrens 		int equal;
2202fa9e4066Sahrens 		zdb_blkstats_t *zb = &zcb->zcb_type[l][t];
2203fa9e4066Sahrens 
2204fa9e4066Sahrens 		zb->zb_asize += BP_GET_ASIZE(bp);
2205fa9e4066Sahrens 		zb->zb_lsize += BP_GET_LSIZE(bp);
2206fa9e4066Sahrens 		zb->zb_psize += BP_GET_PSIZE(bp);
2207fa9e4066Sahrens 		zb->zb_count++;
2208490d05b9SMatthew Ahrens 		zb->zb_psize_histogram[BP_GET_PSIZE(bp) >> SPA_MINBLOCKSHIFT]++;
2209d5ee8a13SMatthew Ahrens 
2210d5ee8a13SMatthew Ahrens 		zb->zb_gangs += BP_COUNT_GANG(bp);
2211d5ee8a13SMatthew Ahrens 
2212d5ee8a13SMatthew Ahrens 		switch (BP_GET_NDVAS(bp)) {
2213d5ee8a13SMatthew Ahrens 		case 2:
2214d5ee8a13SMatthew Ahrens 			if (DVA_GET_VDEV(&bp->blk_dva[0]) ==
2215d5ee8a13SMatthew Ahrens 			    DVA_GET_VDEV(&bp->blk_dva[1]))
2216d5ee8a13SMatthew Ahrens 				zb->zb_ditto_samevdev++;
2217d5ee8a13SMatthew Ahrens 			break;
2218d5ee8a13SMatthew Ahrens 		case 3:
2219d5ee8a13SMatthew Ahrens 			equal = (DVA_GET_VDEV(&bp->blk_dva[0]) ==
2220d5ee8a13SMatthew Ahrens 			    DVA_GET_VDEV(&bp->blk_dva[1])) +
2221d5ee8a13SMatthew Ahrens 			    (DVA_GET_VDEV(&bp->blk_dva[0]) ==
2222d5ee8a13SMatthew Ahrens 			    DVA_GET_VDEV(&bp->blk_dva[2])) +
2223d5ee8a13SMatthew Ahrens 			    (DVA_GET_VDEV(&bp->blk_dva[1]) ==
2224d5ee8a13SMatthew Ahrens 			    DVA_GET_VDEV(&bp->blk_dva[2]));
2225d5ee8a13SMatthew Ahrens 			if (equal != 0)
2226d5ee8a13SMatthew Ahrens 				zb->zb_ditto_samevdev++;
2227d5ee8a13SMatthew Ahrens 			break;
2228d5ee8a13SMatthew Ahrens 		}
2229d5ee8a13SMatthew Ahrens 
2230fa9e4066Sahrens 	}
2231fa9e4066Sahrens 
22325d7b4d43SMatthew Ahrens 	if (BP_IS_EMBEDDED(bp)) {
22335d7b4d43SMatthew Ahrens 		zcb->zcb_embedded_blocks[BPE_GET_ETYPE(bp)]++;
22345d7b4d43SMatthew Ahrens 		zcb->zcb_embedded_histogram[BPE_GET_ETYPE(bp)]
22355d7b4d43SMatthew Ahrens 		    [BPE_GET_PSIZE(bp)]++;
22365d7b4d43SMatthew Ahrens 		return;
22375d7b4d43SMatthew Ahrens 	}
22385d7b4d43SMatthew Ahrens 
2239b24ab676SJeff Bonwick 	if (dump_opt['L'])
2240b24ab676SJeff Bonwick 		return;
2241b24ab676SJeff Bonwick 
2242b24ab676SJeff Bonwick 	if (BP_GET_DEDUP(bp)) {
2243b24ab676SJeff Bonwick 		ddt_t *ddt;
2244b24ab676SJeff Bonwick 		ddt_entry_t *dde;
2245b24ab676SJeff Bonwick 
2246cde58dbcSMatthew Ahrens 		ddt = ddt_select(zcb->zcb_spa, bp);
2247b24ab676SJeff Bonwick 		ddt_enter(ddt);
2248b24ab676SJeff Bonwick 		dde = ddt_lookup(ddt, bp, B_FALSE);
2249b24ab676SJeff Bonwick 
2250b24ab676SJeff Bonwick 		if (dde == NULL) {
2251b24ab676SJeff Bonwick 			refcnt = 0;
2252b24ab676SJeff Bonwick 		} else {
2253b24ab676SJeff Bonwick 			ddt_phys_t *ddp = ddt_phys_select(dde, bp);
2254b24ab676SJeff Bonwick 			ddt_phys_decref(ddp);
2255b24ab676SJeff Bonwick 			refcnt = ddp->ddp_refcnt;
2256b24ab676SJeff Bonwick 			if (ddt_phys_total_refcnt(dde) == 0)
2257b24ab676SJeff Bonwick 				ddt_remove(ddt, dde);
2258d41e7643Sek 		}
2259b24ab676SJeff Bonwick 		ddt_exit(ddt);
2260d41e7643Sek 	}
2261d41e7643Sek 
2262cde58dbcSMatthew Ahrens 	VERIFY3U(zio_wait(zio_claim(NULL, zcb->zcb_spa,
2263cde58dbcSMatthew Ahrens 	    refcnt ? 0 : spa_first_txg(zcb->zcb_spa),
2264b24ab676SJeff Bonwick 	    bp, NULL, NULL, ZIO_FLAG_CANFAIL)), ==, 0);
2265fa9e4066Sahrens }
2266fa9e4066Sahrens 
226731d7e8faSGeorge Wilson static void
226831d7e8faSGeorge Wilson zdb_blkptr_done(zio_t *zio)
226931d7e8faSGeorge Wilson {
227031d7e8faSGeorge Wilson 	spa_t *spa = zio->io_spa;
227131d7e8faSGeorge Wilson 	blkptr_t *bp = zio->io_bp;
227231d7e8faSGeorge Wilson 	int ioerr = zio->io_error;
227331d7e8faSGeorge Wilson 	zdb_cb_t *zcb = zio->io_private;
22747802d7bfSMatthew Ahrens 	zbookmark_phys_t *zb = &zio->io_bookmark;
227531d7e8faSGeorge Wilson 
227631d7e8faSGeorge Wilson 	zio_data_buf_free(zio->io_data, zio->io_size);
227731d7e8faSGeorge Wilson 
227831d7e8faSGeorge Wilson 	mutex_enter(&spa->spa_scrub_lock);
227931d7e8faSGeorge Wilson 	spa->spa_scrub_inflight--;
228031d7e8faSGeorge Wilson 	cv_broadcast(&spa->spa_scrub_io_cv);
228131d7e8faSGeorge Wilson 
228231d7e8faSGeorge Wilson 	if (ioerr && !(zio->io_flags & ZIO_FLAG_SPECULATIVE)) {
228331d7e8faSGeorge Wilson 		char blkbuf[BP_SPRINTF_LEN];
228431d7e8faSGeorge Wilson 
228531d7e8faSGeorge Wilson 		zcb->zcb_haderrors = 1;
228631d7e8faSGeorge Wilson 		zcb->zcb_errors[ioerr]++;
228731d7e8faSGeorge Wilson 
228831d7e8faSGeorge Wilson 		if (dump_opt['b'] >= 2)
228943466aaeSMax Grossman 			snprintf_blkptr(blkbuf, sizeof (blkbuf), bp);
229031d7e8faSGeorge Wilson 		else
229131d7e8faSGeorge Wilson 			blkbuf[0] = '\0';
229231d7e8faSGeorge Wilson 
229331d7e8faSGeorge Wilson 		(void) printf("zdb_blkptr_cb: "
229431d7e8faSGeorge Wilson 		    "Got error %d reading "
229531d7e8faSGeorge Wilson 		    "<%llu, %llu, %lld, %llx> %s -- skipping\n",
229631d7e8faSGeorge Wilson 		    ioerr,
229731d7e8faSGeorge Wilson 		    (u_longlong_t)zb->zb_objset,
229831d7e8faSGeorge Wilson 		    (u_longlong_t)zb->zb_object,
229931d7e8faSGeorge Wilson 		    (u_longlong_t)zb->zb_level,
230031d7e8faSGeorge Wilson 		    (u_longlong_t)zb->zb_blkid,
230131d7e8faSGeorge Wilson 		    blkbuf);
230231d7e8faSGeorge Wilson 	}
230331d7e8faSGeorge Wilson 	mutex_exit(&spa->spa_scrub_lock);
230431d7e8faSGeorge Wilson }
230531d7e8faSGeorge Wilson 
2306fa9e4066Sahrens static int
23071b912ec7SGeorge Wilson zdb_blkptr_cb(spa_t *spa, zilog_t *zilog, const blkptr_t *bp,
23087802d7bfSMatthew Ahrens     const zbookmark_phys_t *zb, const dnode_phys_t *dnp, void *arg)
2309fa9e4066Sahrens {
2310fa9e4066Sahrens 	zdb_cb_t *zcb = arg;
23116365109dSVictor Latushkin 	dmu_object_type_t type;
2312468c413aSTim Haley 	boolean_t is_metadata;
2313fa9e4066Sahrens 
231443466aaeSMax Grossman 	if (dump_opt['b'] >= 5 && bp->blk_birth > 0) {
231543466aaeSMax Grossman 		char blkbuf[BP_SPRINTF_LEN];
231643466aaeSMax Grossman 		snprintf_blkptr(blkbuf, sizeof (blkbuf), bp);
231743466aaeSMax Grossman 		(void) printf("objset %llu object %llu "
231843466aaeSMax Grossman 		    "level %lld offset 0x%llx %s\n",
231943466aaeSMax Grossman 		    (u_longlong_t)zb->zb_objset,
232043466aaeSMax Grossman 		    (u_longlong_t)zb->zb_object,
232143466aaeSMax Grossman 		    (longlong_t)zb->zb_level,
232243466aaeSMax Grossman 		    (u_longlong_t)blkid2offset(dnp, bp, zb),
232343466aaeSMax Grossman 		    blkbuf);
232443466aaeSMax Grossman 	}
232543466aaeSMax Grossman 
232643466aaeSMax Grossman 	if (BP_IS_HOLE(bp))
232788b7b0f2SMatthew Ahrens 		return (0);
2328e14bb325SJeff Bonwick 
23296365109dSVictor Latushkin 	type = BP_GET_TYPE(bp);
2330e14bb325SJeff Bonwick 
2331ad135b5dSChristopher Siden 	zdb_count_block(zcb, zilog, bp,
2332ad135b5dSChristopher Siden 	    (type & DMU_OT_NEWTYPE) ? ZDB_OT_OTHER : type);
23336365109dSVictor Latushkin 
2334ad135b5dSChristopher Siden 	is_metadata = (BP_GET_LEVEL(bp) != 0 || DMU_OT_IS_METADATA(type));
23356365109dSVictor Latushkin 
23365d7b4d43SMatthew Ahrens 	if (!BP_IS_EMBEDDED(bp) &&
23375d7b4d43SMatthew Ahrens 	    (dump_opt['c'] > 1 || (dump_opt['c'] && is_metadata))) {
2338468c413aSTim Haley 		size_t size = BP_GET_PSIZE(bp);
233931d7e8faSGeorge Wilson 		void *data = zio_data_buf_alloc(size);
2340b24ab676SJeff Bonwick 		int flags = ZIO_FLAG_CANFAIL | ZIO_FLAG_SCRUB | ZIO_FLAG_RAW;
2341b24ab676SJeff Bonwick 
2342b24ab676SJeff Bonwick 		/* If it's an intent log block, failure is expected. */
2343b24ab676SJeff Bonwick 		if (zb->zb_level == ZB_ZIL_LEVEL)
2344b24ab676SJeff Bonwick 			flags |= ZIO_FLAG_SPECULATIVE;
2345b24ab676SJeff Bonwick 
234631d7e8faSGeorge Wilson 		mutex_enter(&spa->spa_scrub_lock);
234731d7e8faSGeorge Wilson 		while (spa->spa_scrub_inflight > max_inflight)
234831d7e8faSGeorge Wilson 			cv_wait(&spa->spa_scrub_io_cv, &spa->spa_scrub_lock);
234931d7e8faSGeorge Wilson 		spa->spa_scrub_inflight++;
235031d7e8faSGeorge Wilson 		mutex_exit(&spa->spa_scrub_lock);
2351b24ab676SJeff Bonwick 
235231d7e8faSGeorge Wilson 		zio_nowait(zio_read(NULL, spa, bp, data, size,
235331d7e8faSGeorge Wilson 		    zdb_blkptr_done, zcb, ZIO_PRIORITY_ASYNC_READ, flags, zb));
2354fa9e4066Sahrens 	}
2355fa9e4066Sahrens 
2356fa9e4066Sahrens 	zcb->zcb_readfails = 0;
2357fa9e4066Sahrens 
2358*06be9802SMatthew Ahrens 	if (dump_opt['b'] < 5 &&
2359490d05b9SMatthew Ahrens 	    gethrtime() > zcb->zcb_lastprint + NANOSEC) {
2360490d05b9SMatthew Ahrens 		uint64_t now = gethrtime();
2361490d05b9SMatthew Ahrens 		char buf[10];
2362490d05b9SMatthew Ahrens 		uint64_t bytes = zcb->zcb_type[ZB_TOTAL][ZDB_OT_TOTAL].zb_asize;
2363490d05b9SMatthew Ahrens 		int kb_per_sec =
2364490d05b9SMatthew Ahrens 		    1 + bytes / (1 + ((now - zcb->zcb_start) / 1000 / 1000));
2365490d05b9SMatthew Ahrens 		int sec_remaining =
2366490d05b9SMatthew Ahrens 		    (zcb->zcb_totalasize - bytes) / 1024 / kb_per_sec;
2367490d05b9SMatthew Ahrens 
2368490d05b9SMatthew Ahrens 		zfs_nicenum(bytes, buf, sizeof (buf));
2369490d05b9SMatthew Ahrens 		(void) fprintf(stderr,
2370490d05b9SMatthew Ahrens 		    "\r%5s completed (%4dMB/s) "
2371490d05b9SMatthew Ahrens 		    "estimated time remaining: %uhr %02umin %02usec        ",
2372490d05b9SMatthew Ahrens 		    buf, kb_per_sec / 1024,
2373490d05b9SMatthew Ahrens 		    sec_remaining / 60 / 60,
2374490d05b9SMatthew Ahrens 		    sec_remaining / 60 % 60,
2375490d05b9SMatthew Ahrens 		    sec_remaining % 60);
2376490d05b9SMatthew Ahrens 
2377490d05b9SMatthew Ahrens 		zcb->zcb_lastprint = now;
2378490d05b9SMatthew Ahrens 	}
2379490d05b9SMatthew Ahrens 
2380fa9e4066Sahrens 	return (0);
2381fa9e4066Sahrens }
2382fa9e4066Sahrens 
2383b24ab676SJeff Bonwick static void
23840713e232SGeorge Wilson zdb_leak(void *arg, uint64_t start, uint64_t size)
2385b24ab676SJeff Bonwick {
23860713e232SGeorge Wilson 	vdev_t *vd = arg;
2387b24ab676SJeff Bonwick 
2388b24ab676SJeff Bonwick 	(void) printf("leaked space: vdev %llu, offset 0x%llx, size %llu\n",
2389b24ab676SJeff Bonwick 	    (u_longlong_t)vd->vdev_id, (u_longlong_t)start, (u_longlong_t)size);
2390b24ab676SJeff Bonwick }
2391b24ab676SJeff Bonwick 
23920713e232SGeorge Wilson static metaslab_ops_t zdb_metaslab_ops = {
23932e4c9986SGeorge Wilson 	NULL	/* alloc */
2394b24ab676SJeff Bonwick };
2395b24ab676SJeff Bonwick 
2396b24ab676SJeff Bonwick static void
2397bbfd46c4SJeff Bonwick zdb_ddt_leak_init(spa_t *spa, zdb_cb_t *zcb)
2398b24ab676SJeff Bonwick {
2399bbfd46c4SJeff Bonwick 	ddt_bookmark_t ddb = { 0 };
2400b24ab676SJeff Bonwick 	ddt_entry_t dde;
2401b24ab676SJeff Bonwick 	int error;
2402b24ab676SJeff Bonwick 
2403bbfd46c4SJeff Bonwick 	while ((error = ddt_walk(spa, &ddb, &dde)) == 0) {
2404b24ab676SJeff Bonwick 		blkptr_t blk;
2405b24ab676SJeff Bonwick 		ddt_phys_t *ddp = dde.dde_phys;
2406bbfd46c4SJeff Bonwick 
2407bbfd46c4SJeff Bonwick 		if (ddb.ddb_class == DDT_CLASS_UNIQUE)
2408bbfd46c4SJeff Bonwick 			return;
2409bbfd46c4SJeff Bonwick 
2410b24ab676SJeff Bonwick 		ASSERT(ddt_phys_total_refcnt(&dde) > 1);
2411bbfd46c4SJeff Bonwick 
2412b24ab676SJeff Bonwick 		for (int p = 0; p < DDT_PHYS_TYPES; p++, ddp++) {
2413b24ab676SJeff Bonwick 			if (ddp->ddp_phys_birth == 0)
2414b24ab676SJeff Bonwick 				continue;
2415bbfd46c4SJeff Bonwick 			ddt_bp_create(ddb.ddb_checksum,
2416bbfd46c4SJeff Bonwick 			    &dde.dde_key, ddp, &blk);
2417b24ab676SJeff Bonwick 			if (p == DDT_PHYS_DITTO) {
2418cde58dbcSMatthew Ahrens 				zdb_count_block(zcb, NULL, &blk, ZDB_OT_DITTO);
2419b24ab676SJeff Bonwick 			} else {
2420b24ab676SJeff Bonwick 				zcb->zcb_dedup_asize +=
2421b24ab676SJeff Bonwick 				    BP_GET_ASIZE(&blk) * (ddp->ddp_refcnt - 1);
2422b24ab676SJeff Bonwick 				zcb->zcb_dedup_blocks++;
2423b24ab676SJeff Bonwick 			}
2424b24ab676SJeff Bonwick 		}
2425b24ab676SJeff Bonwick 		if (!dump_opt['L']) {
2426bbfd46c4SJeff Bonwick 			ddt_t *ddt = spa->spa_ddt[ddb.ddb_checksum];
2427b24ab676SJeff Bonwick 			ddt_enter(ddt);
2428b24ab676SJeff Bonwick 			VERIFY(ddt_lookup(ddt, &blk, B_TRUE) != NULL);
2429b24ab676SJeff Bonwick 			ddt_exit(ddt);
2430b24ab676SJeff Bonwick 		}
2431b24ab676SJeff Bonwick 	}
2432b24ab676SJeff Bonwick 
2433b24ab676SJeff Bonwick 	ASSERT(error == ENOENT);
2434b24ab676SJeff Bonwick }
2435b24ab676SJeff Bonwick 
2436b24ab676SJeff Bonwick static void
2437b24ab676SJeff Bonwick zdb_leak_init(spa_t *spa, zdb_cb_t *zcb)
2438b24ab676SJeff Bonwick {
2439cde58dbcSMatthew Ahrens 	zcb->zcb_spa = spa;
2440cde58dbcSMatthew Ahrens 
2441b24ab676SJeff Bonwick 	if (!dump_opt['L']) {
2442b24ab676SJeff Bonwick 		vdev_t *rvd = spa->spa_root_vdev;
2443*06be9802SMatthew Ahrens 		for (uint64_t c = 0; c < rvd->vdev_children; c++) {
2444b24ab676SJeff Bonwick 			vdev_t *vd = rvd->vdev_child[c];
2445*06be9802SMatthew Ahrens 			for (uint64_t m = 0; m < vd->vdev_ms_count; m++) {
2446b24ab676SJeff Bonwick 				metaslab_t *msp = vd->vdev_ms[m];
2447b24ab676SJeff Bonwick 				mutex_enter(&msp->ms_lock);
24480713e232SGeorge Wilson 				metaslab_unload(msp);
24490713e232SGeorge Wilson 
24500713e232SGeorge Wilson 				/*
24510713e232SGeorge Wilson 				 * For leak detection, we overload the metaslab
24520713e232SGeorge Wilson 				 * ms_tree to contain allocated segments
24530713e232SGeorge Wilson 				 * instead of free segments. As a result,
24540713e232SGeorge Wilson 				 * we can't use the normal metaslab_load/unload
24550713e232SGeorge Wilson 				 * interfaces.
24560713e232SGeorge Wilson 				 */
24570713e232SGeorge Wilson 				if (msp->ms_sm != NULL) {
2458*06be9802SMatthew Ahrens 					(void) fprintf(stderr,
2459*06be9802SMatthew Ahrens 					    "\rloading space map for "
2460*06be9802SMatthew Ahrens 					    "vdev %llu of %llu, "
2461*06be9802SMatthew Ahrens 					    "metaslab %llu of %llu ...",
2462*06be9802SMatthew Ahrens 					    (longlong_t)c,
2463*06be9802SMatthew Ahrens 					    (longlong_t)rvd->vdev_children,
2464*06be9802SMatthew Ahrens 					    (longlong_t)m,
2465*06be9802SMatthew Ahrens 					    (longlong_t)vd->vdev_ms_count);
2466*06be9802SMatthew Ahrens 
24670713e232SGeorge Wilson 					msp->ms_ops = &zdb_metaslab_ops;
24680713e232SGeorge Wilson 					VERIFY0(space_map_load(msp->ms_sm,
24690713e232SGeorge Wilson 					    msp->ms_tree, SM_ALLOC));
24700713e232SGeorge Wilson 					msp->ms_loaded = B_TRUE;
24710713e232SGeorge Wilson 				}
2472b24ab676SJeff Bonwick 				mutex_exit(&msp->ms_lock);
2473b24ab676SJeff Bonwick 			}
2474b24ab676SJeff Bonwick 		}
2475*06be9802SMatthew Ahrens 		(void) fprintf(stderr, "\n");
2476b24ab676SJeff Bonwick 	}
2477b24ab676SJeff Bonwick 
2478b24ab676SJeff Bonwick 	spa_config_enter(spa, SCL_CONFIG, FTAG, RW_READER);
2479b24ab676SJeff Bonwick 
2480bbfd46c4SJeff Bonwick 	zdb_ddt_leak_init(spa, zcb);
2481b24ab676SJeff Bonwick 
2482b24ab676SJeff Bonwick 	spa_config_exit(spa, SCL_CONFIG, FTAG);
2483b24ab676SJeff Bonwick }
2484b24ab676SJeff Bonwick 
2485b24ab676SJeff Bonwick static void
2486b24ab676SJeff Bonwick zdb_leak_fini(spa_t *spa)
2487b24ab676SJeff Bonwick {
2488b24ab676SJeff Bonwick 	if (!dump_opt['L']) {
2489b24ab676SJeff Bonwick 		vdev_t *rvd = spa->spa_root_vdev;
2490b24ab676SJeff Bonwick 		for (int c = 0; c < rvd->vdev_children; c++) {
2491b24ab676SJeff Bonwick 			vdev_t *vd = rvd->vdev_child[c];
2492b24ab676SJeff Bonwick 			for (int m = 0; m < vd->vdev_ms_count; m++) {
2493b24ab676SJeff Bonwick 				metaslab_t *msp = vd->vdev_ms[m];
2494b24ab676SJeff Bonwick 				mutex_enter(&msp->ms_lock);
24950713e232SGeorge Wilson 
24960713e232SGeorge Wilson 				/*
24970713e232SGeorge Wilson 				 * The ms_tree has been overloaded to
24980713e232SGeorge Wilson 				 * contain allocated segments. Now that we
24990713e232SGeorge Wilson 				 * finished traversing all blocks, any
25000713e232SGeorge Wilson 				 * block that remains in the ms_tree
25010713e232SGeorge Wilson 				 * represents an allocated block that we
25020713e232SGeorge Wilson 				 * did not claim during the traversal.
25030713e232SGeorge Wilson 				 * Claimed blocks would have been removed
25040713e232SGeorge Wilson 				 * from the ms_tree.
25050713e232SGeorge Wilson 				 */
25060713e232SGeorge Wilson 				range_tree_vacate(msp->ms_tree, zdb_leak, vd);
25070713e232SGeorge Wilson 				msp->ms_loaded = B_FALSE;
25080713e232SGeorge Wilson 
2509b24ab676SJeff Bonwick 				mutex_exit(&msp->ms_lock);
2510b24ab676SJeff Bonwick 			}
2511b24ab676SJeff Bonwick 		}
2512b24ab676SJeff Bonwick 	}
2513b24ab676SJeff Bonwick }
2514b24ab676SJeff Bonwick 
2515cde58dbcSMatthew Ahrens /* ARGSUSED */
2516cde58dbcSMatthew Ahrens static int
2517cde58dbcSMatthew Ahrens count_block_cb(void *arg, const blkptr_t *bp, dmu_tx_t *tx)
2518cde58dbcSMatthew Ahrens {
2519cde58dbcSMatthew Ahrens 	zdb_cb_t *zcb = arg;
2520cde58dbcSMatthew Ahrens 
2521490d05b9SMatthew Ahrens 	if (dump_opt['b'] >= 5) {
2522cde58dbcSMatthew Ahrens 		char blkbuf[BP_SPRINTF_LEN];
252343466aaeSMax Grossman 		snprintf_blkptr(blkbuf, sizeof (blkbuf), bp);
2524cde58dbcSMatthew Ahrens 		(void) printf("[%s] %s\n",
2525cde58dbcSMatthew Ahrens 		    "deferred free", blkbuf);
2526cde58dbcSMatthew Ahrens 	}
2527cde58dbcSMatthew Ahrens 	zdb_count_block(zcb, NULL, bp, ZDB_OT_DEFERRED);
2528cde58dbcSMatthew Ahrens 	return (0);
2529cde58dbcSMatthew Ahrens }
2530cde58dbcSMatthew Ahrens 
2531fa9e4066Sahrens static int
2532fa9e4066Sahrens dump_block_stats(spa_t *spa)
2533fa9e4066Sahrens {
2534fa9e4066Sahrens 	zdb_cb_t zcb = { 0 };
2535fa9e4066Sahrens 	zdb_blkstats_t *zb, *tzb;
2536b24ab676SJeff Bonwick 	uint64_t norm_alloc, norm_space, total_alloc, total_found;
2537cd088ea4SVictor Latushkin 	int flags = TRAVERSE_PRE | TRAVERSE_PREFETCH_METADATA | TRAVERSE_HARD;
25385d7b4d43SMatthew Ahrens 	boolean_t leaks = B_FALSE;
2539fa9e4066Sahrens 
2540490d05b9SMatthew Ahrens 	(void) printf("\nTraversing all blocks %s%s%s%s%s...\n\n",
2541b24ab676SJeff Bonwick 	    (dump_opt['c'] || !dump_opt['L']) ? "to verify " : "",
2542b24ab676SJeff Bonwick 	    (dump_opt['c'] == 1) ? "metadata " : "",
2543b24ab676SJeff Bonwick 	    dump_opt['c'] ? "checksums " : "",
2544b24ab676SJeff Bonwick 	    (dump_opt['c'] && !dump_opt['L']) ? "and verify " : "",
2545b24ab676SJeff Bonwick 	    !dump_opt['L'] ? "nothing leaked " : "");
2546fa9e4066Sahrens 
2547fa9e4066Sahrens 	/*
2548e14bb325SJeff Bonwick 	 * Load all space maps as SM_ALLOC maps, then traverse the pool
2549e14bb325SJeff Bonwick 	 * claiming each block we discover.  If the pool is perfectly
2550e14bb325SJeff Bonwick 	 * consistent, the space maps will be empty when we're done.
2551e14bb325SJeff Bonwick 	 * Anything left over is a leak; any block we can't claim (because
2552e14bb325SJeff Bonwick 	 * it's not part of any space map) is a double allocation,
2553e14bb325SJeff Bonwick 	 * reference to a freed block, or an unclaimed log block.
2554fa9e4066Sahrens 	 */
2555b24ab676SJeff Bonwick 	zdb_leak_init(spa, &zcb);
2556fa9e4066Sahrens 
2557fa9e4066Sahrens 	/*
2558fa9e4066Sahrens 	 * If there's a deferred-free bplist, process that first.
2559fa9e4066Sahrens 	 */
2560cde58dbcSMatthew Ahrens 	(void) bpobj_iterate_nofree(&spa->spa_deferred_bpobj,
2561cde58dbcSMatthew Ahrens 	    count_block_cb, &zcb, NULL);
25623b2aab18SMatthew Ahrens 	if (spa_version(spa) >= SPA_VERSION_DEADLISTS) {
25633b2aab18SMatthew Ahrens 		(void) bpobj_iterate_nofree(&spa->spa_dsl_pool->dp_free_bpobj,
25643b2aab18SMatthew Ahrens 		    count_block_cb, &zcb, NULL);
25653b2aab18SMatthew Ahrens 	}
25662acef22dSMatthew Ahrens 	if (spa_feature_is_active(spa, SPA_FEATURE_ASYNC_DESTROY)) {
2567b420f3adSRichard Lowe 		VERIFY3U(0, ==, bptree_iterate(spa->spa_meta_objset,
2568ad135b5dSChristopher Siden 		    spa->spa_dsl_pool->dp_bptree_obj, B_FALSE, count_block_cb,
2569ad135b5dSChristopher Siden 		    &zcb, NULL));
2570ad135b5dSChristopher Siden 	}
2571fa9e4066Sahrens 
2572bbfd46c4SJeff Bonwick 	if (dump_opt['c'] > 1)
2573bbfd46c4SJeff Bonwick 		flags |= TRAVERSE_PREFETCH_DATA;
2574bbfd46c4SJeff Bonwick 
2575490d05b9SMatthew Ahrens 	zcb.zcb_totalasize = metaslab_class_get_alloc(spa_normal_class(spa));
2576490d05b9SMatthew Ahrens 	zcb.zcb_start = zcb.zcb_lastprint = gethrtime();
2577bbfd46c4SJeff Bonwick 	zcb.zcb_haderrors |= traverse_pool(spa, 0, flags, zdb_blkptr_cb, &zcb);
2578fa9e4066Sahrens 
257931d7e8faSGeorge Wilson 	/*
258031d7e8faSGeorge Wilson 	 * If we've traversed the data blocks then we need to wait for those
258131d7e8faSGeorge Wilson 	 * I/Os to complete. We leverage "The Godfather" zio to wait on
258231d7e8faSGeorge Wilson 	 * all async I/Os to complete.
258331d7e8faSGeorge Wilson 	 */
258431d7e8faSGeorge Wilson 	if (dump_opt['c']) {
25856f834bc1SMatthew Ahrens 		for (int i = 0; i < max_ncpus; i++) {
25866f834bc1SMatthew Ahrens 			(void) zio_wait(spa->spa_async_zio_root[i]);
25876f834bc1SMatthew Ahrens 			spa->spa_async_zio_root[i] = zio_root(spa, NULL, NULL,
25886f834bc1SMatthew Ahrens 			    ZIO_FLAG_CANFAIL | ZIO_FLAG_SPECULATIVE |
25896f834bc1SMatthew Ahrens 			    ZIO_FLAG_GODFATHER);
25906f834bc1SMatthew Ahrens 		}
259131d7e8faSGeorge Wilson 	}
259231d7e8faSGeorge Wilson 
2593b24ab676SJeff Bonwick 	if (zcb.zcb_haderrors) {
2594fa9e4066Sahrens 		(void) printf("\nError counts:\n\n");
2595fa9e4066Sahrens 		(void) printf("\t%5s  %s\n", "errno", "count");
2596b24ab676SJeff Bonwick 		for (int e = 0; e < 256; e++) {
2597fa9e4066Sahrens 			if (zcb.zcb_errors[e] != 0) {
2598fa9e4066Sahrens 				(void) printf("\t%5d  %llu\n",
2599fa9e4066Sahrens 				    e, (u_longlong_t)zcb.zcb_errors[e]);
2600fa9e4066Sahrens 			}
2601fa9e4066Sahrens 		}
2602fa9e4066Sahrens 	}
2603fa9e4066Sahrens 
2604fa9e4066Sahrens 	/*
2605fa9e4066Sahrens 	 * Report any leaked segments.
2606fa9e4066Sahrens 	 */
2607b24ab676SJeff Bonwick 	zdb_leak_fini(spa);
2608fa9e4066Sahrens 
2609b24ab676SJeff Bonwick 	tzb = &zcb.zcb_type[ZB_TOTAL][ZDB_OT_TOTAL];
26108654d025Sperrin 
2611b24ab676SJeff Bonwick 	norm_alloc = metaslab_class_get_alloc(spa_normal_class(spa));
2612b24ab676SJeff Bonwick 	norm_space = metaslab_class_get_space(spa_normal_class(spa));
26138654d025Sperrin 
2614b24ab676SJeff Bonwick 	total_alloc = norm_alloc + metaslab_class_get_alloc(spa_log_class(spa));
2615b24ab676SJeff Bonwick 	total_found = tzb->zb_asize - zcb.zcb_dedup_asize;
2616fa9e4066Sahrens 
2617b24ab676SJeff Bonwick 	if (total_found == total_alloc) {
261882a0a985SVictor Latushkin 		if (!dump_opt['L'])
261982a0a985SVictor Latushkin 			(void) printf("\n\tNo leaks (block sum matches space"
262082a0a985SVictor Latushkin 			    " maps exactly)\n");
2621fa9e4066Sahrens 	} else {
2622fa9e4066Sahrens 		(void) printf("block traversal size %llu != alloc %llu "
262382a0a985SVictor Latushkin 		    "(%s %lld)\n",
2624b24ab676SJeff Bonwick 		    (u_longlong_t)total_found,
2625b24ab676SJeff Bonwick 		    (u_longlong_t)total_alloc,
262682a0a985SVictor Latushkin 		    (dump_opt['L']) ? "unreachable" : "leaked",
2627b24ab676SJeff Bonwick 		    (longlong_t)(total_alloc - total_found));
26285d7b4d43SMatthew Ahrens 		leaks = B_TRUE;
2629fa9e4066Sahrens 	}
2630fa9e4066Sahrens 
2631fa9e4066Sahrens 	if (tzb->zb_count == 0)
2632fa9e4066Sahrens 		return (2);
2633fa9e4066Sahrens 
2634fa9e4066Sahrens 	(void) printf("\n");
2635fa9e4066Sahrens 	(void) printf("\tbp count:      %10llu\n",
2636fa9e4066Sahrens 	    (u_longlong_t)tzb->zb_count);
2637d5ee8a13SMatthew Ahrens 	(void) printf("\tganged count:  %10llu\n",
2638d5ee8a13SMatthew Ahrens 	    (longlong_t)tzb->zb_gangs);
2639b24ab676SJeff Bonwick 	(void) printf("\tbp logical:    %10llu      avg: %6llu\n",
2640fa9e4066Sahrens 	    (u_longlong_t)tzb->zb_lsize,
2641fa9e4066Sahrens 	    (u_longlong_t)(tzb->zb_lsize / tzb->zb_count));
2642b24ab676SJeff Bonwick 	(void) printf("\tbp physical:   %10llu      avg:"
2643b24ab676SJeff Bonwick 	    " %6llu     compression: %6.2f\n",
2644fa9e4066Sahrens 	    (u_longlong_t)tzb->zb_psize,
2645fa9e4066Sahrens 	    (u_longlong_t)(tzb->zb_psize / tzb->zb_count),
2646fa9e4066Sahrens 	    (double)tzb->zb_lsize / tzb->zb_psize);
2647b24ab676SJeff Bonwick 	(void) printf("\tbp allocated:  %10llu      avg:"
2648b24ab676SJeff Bonwick 	    " %6llu     compression: %6.2f\n",
2649fa9e4066Sahrens 	    (u_longlong_t)tzb->zb_asize,
2650fa9e4066Sahrens 	    (u_longlong_t)(tzb->zb_asize / tzb->zb_count),
2651fa9e4066Sahrens 	    (double)tzb->zb_lsize / tzb->zb_asize);
2652b24ab676SJeff Bonwick 	(void) printf("\tbp deduped:    %10llu    ref>1:"
2653b24ab676SJeff Bonwick 	    " %6llu   deduplication: %6.2f\n",
2654b24ab676SJeff Bonwick 	    (u_longlong_t)zcb.zcb_dedup_asize,
2655b24ab676SJeff Bonwick 	    (u_longlong_t)zcb.zcb_dedup_blocks,
2656b24ab676SJeff Bonwick 	    (double)zcb.zcb_dedup_asize / tzb->zb_asize + 1.0);
2657b24ab676SJeff Bonwick 	(void) printf("\tSPA allocated: %10llu     used: %5.2f%%\n",
2658b24ab676SJeff Bonwick 	    (u_longlong_t)norm_alloc, 100.0 * norm_alloc / norm_space);
2659fa9e4066Sahrens 
26605d7b4d43SMatthew Ahrens 	for (bp_embedded_type_t i = 0; i < NUM_BP_EMBEDDED_TYPES; i++) {
26615d7b4d43SMatthew Ahrens 		if (zcb.zcb_embedded_blocks[i] == 0)
26625d7b4d43SMatthew Ahrens 			continue;
26635d7b4d43SMatthew Ahrens 		(void) printf("\n");
26645d7b4d43SMatthew Ahrens 		(void) printf("\tadditional, non-pointer bps of type %u: "
26655d7b4d43SMatthew Ahrens 		    "%10llu\n",
26665d7b4d43SMatthew Ahrens 		    i, (u_longlong_t)zcb.zcb_embedded_blocks[i]);
26675d7b4d43SMatthew Ahrens 
26685d7b4d43SMatthew Ahrens 		if (dump_opt['b'] >= 3) {
26695d7b4d43SMatthew Ahrens 			(void) printf("\t number of (compressed) bytes:  "
26705d7b4d43SMatthew Ahrens 			    "number of bps\n");
26715d7b4d43SMatthew Ahrens 			dump_histogram(zcb.zcb_embedded_histogram[i],
26725d7b4d43SMatthew Ahrens 			    sizeof (zcb.zcb_embedded_histogram[i]) /
26735d7b4d43SMatthew Ahrens 			    sizeof (zcb.zcb_embedded_histogram[i][0]), 0);
26745d7b4d43SMatthew Ahrens 		}
26755d7b4d43SMatthew Ahrens 	}
26765d7b4d43SMatthew Ahrens 
2677d5ee8a13SMatthew Ahrens 	if (tzb->zb_ditto_samevdev != 0) {
2678d5ee8a13SMatthew Ahrens 		(void) printf("\tDittoed blocks on same vdev: %llu\n",
2679d5ee8a13SMatthew Ahrens 		    (longlong_t)tzb->zb_ditto_samevdev);
2680d5ee8a13SMatthew Ahrens 	}
2681d5ee8a13SMatthew Ahrens 
2682fa9e4066Sahrens 	if (dump_opt['b'] >= 2) {
2683fa9e4066Sahrens 		int l, t, level;
2684fa9e4066Sahrens 		(void) printf("\nBlocks\tLSIZE\tPSIZE\tASIZE"
2685fa9e4066Sahrens 		    "\t  avg\t comp\t%%Total\tType\n");
2686fa9e4066Sahrens 
2687b24ab676SJeff Bonwick 		for (t = 0; t <= ZDB_OT_TOTAL; t++) {
26883f9d6ad7SLin Ling 			char csize[32], lsize[32], psize[32], asize[32];
2689d5ee8a13SMatthew Ahrens 			char avg[32], gang[32];
2690fa9e4066Sahrens 			char *typename;
2691fa9e4066Sahrens 
2692b24ab676SJeff Bonwick 			if (t < DMU_OT_NUMTYPES)
2693b24ab676SJeff Bonwick 				typename = dmu_ot[t].ot_name;
2694b24ab676SJeff Bonwick 			else
2695b24ab676SJeff Bonwick 				typename = zdb_ot_extname[t - DMU_OT_NUMTYPES];
2696fa9e4066Sahrens 
2697fa9e4066Sahrens 			if (zcb.zcb_type[ZB_TOTAL][t].zb_asize == 0) {
2698fa9e4066Sahrens 				(void) printf("%6s\t%5s\t%5s\t%5s"
2699fa9e4066Sahrens 				    "\t%5s\t%5s\t%6s\t%s\n",
2700fa9e4066Sahrens 				    "-",
2701fa9e4066Sahrens 				    "-",
2702fa9e4066Sahrens 				    "-",
2703fa9e4066Sahrens 				    "-",
2704fa9e4066Sahrens 				    "-",
2705fa9e4066Sahrens 				    "-",
2706fa9e4066Sahrens 				    "-",
2707fa9e4066Sahrens 				    typename);
2708fa9e4066Sahrens 				continue;
2709fa9e4066Sahrens 			}
2710fa9e4066Sahrens 
2711fa9e4066Sahrens 			for (l = ZB_TOTAL - 1; l >= -1; l--) {
2712fa9e4066Sahrens 				level = (l == -1 ? ZB_TOTAL : l);
2713fa9e4066Sahrens 				zb = &zcb.zcb_type[level][t];
2714fa9e4066Sahrens 
2715fa9e4066Sahrens 				if (zb->zb_asize == 0)
2716fa9e4066Sahrens 					continue;
2717fa9e4066Sahrens 
2718fa9e4066Sahrens 				if (dump_opt['b'] < 3 && level != ZB_TOTAL)
2719fa9e4066Sahrens 					continue;
2720fa9e4066Sahrens 
2721fa9e4066Sahrens 				if (level == 0 && zb->zb_asize ==
2722fa9e4066Sahrens 				    zcb.zcb_type[ZB_TOTAL][t].zb_asize)
2723fa9e4066Sahrens 					continue;
2724fa9e4066Sahrens 
27253f9d6ad7SLin Ling 				zdb_nicenum(zb->zb_count, csize);
27263f9d6ad7SLin Ling 				zdb_nicenum(zb->zb_lsize, lsize);
27273f9d6ad7SLin Ling 				zdb_nicenum(zb->zb_psize, psize);
27283f9d6ad7SLin Ling 				zdb_nicenum(zb->zb_asize, asize);
27293f9d6ad7SLin Ling 				zdb_nicenum(zb->zb_asize / zb->zb_count, avg);
2730d5ee8a13SMatthew Ahrens 				zdb_nicenum(zb->zb_gangs, gang);
2731fa9e4066Sahrens 
2732fa9e4066Sahrens 				(void) printf("%6s\t%5s\t%5s\t%5s\t%5s"
2733fa9e4066Sahrens 				    "\t%5.2f\t%6.2f\t",
2734fa9e4066Sahrens 				    csize, lsize, psize, asize, avg,
2735fa9e4066Sahrens 				    (double)zb->zb_lsize / zb->zb_psize,
2736fa9e4066Sahrens 				    100.0 * zb->zb_asize / tzb->zb_asize);
2737fa9e4066Sahrens 
2738fa9e4066Sahrens 				if (level == ZB_TOTAL)
2739fa9e4066Sahrens 					(void) printf("%s\n", typename);
2740fa9e4066Sahrens 				else
2741fa9e4066Sahrens 					(void) printf("    L%d %s\n",
2742fa9e4066Sahrens 					    level, typename);
2743490d05b9SMatthew Ahrens 
2744d5ee8a13SMatthew Ahrens 				if (dump_opt['b'] >= 3 && zb->zb_gangs > 0) {
2745d5ee8a13SMatthew Ahrens 					(void) printf("\t number of ganged "
2746d5ee8a13SMatthew Ahrens 					    "blocks: %s\n", gang);
2747d5ee8a13SMatthew Ahrens 				}
2748d5ee8a13SMatthew Ahrens 
2749490d05b9SMatthew Ahrens 				if (dump_opt['b'] >= 4) {
2750490d05b9SMatthew Ahrens 					(void) printf("psize "
2751490d05b9SMatthew Ahrens 					    "(in 512-byte sectors): "
2752490d05b9SMatthew Ahrens 					    "number of blocks\n");
2753490d05b9SMatthew Ahrens 					dump_histogram(zb->zb_psize_histogram,
27540713e232SGeorge Wilson 					    PSIZE_HISTO_SIZE, 0);
2755490d05b9SMatthew Ahrens 				}
2756fa9e4066Sahrens 			}
2757fa9e4066Sahrens 		}
2758fa9e4066Sahrens 	}
2759fa9e4066Sahrens 
2760fa9e4066Sahrens 	(void) printf("\n");
2761fa9e4066Sahrens 
2762fa9e4066Sahrens 	if (leaks)
2763fa9e4066Sahrens 		return (2);
2764fa9e4066Sahrens 
2765fa9e4066Sahrens 	if (zcb.zcb_haderrors)
2766fa9e4066Sahrens 		return (3);
2767fa9e4066Sahrens 
2768fa9e4066Sahrens 	return (0);
2769fa9e4066Sahrens }
2770fa9e4066Sahrens 
2771b24ab676SJeff Bonwick typedef struct zdb_ddt_entry {
2772b24ab676SJeff Bonwick 	ddt_key_t	zdde_key;
2773b24ab676SJeff Bonwick 	uint64_t	zdde_ref_blocks;
2774b24ab676SJeff Bonwick 	uint64_t	zdde_ref_lsize;
2775b24ab676SJeff Bonwick 	uint64_t	zdde_ref_psize;
2776b24ab676SJeff Bonwick 	uint64_t	zdde_ref_dsize;
2777b24ab676SJeff Bonwick 	avl_node_t	zdde_node;
2778b24ab676SJeff Bonwick } zdb_ddt_entry_t;
2779b24ab676SJeff Bonwick 
2780b24ab676SJeff Bonwick /* ARGSUSED */
2781b24ab676SJeff Bonwick static int
2782b24ab676SJeff Bonwick zdb_ddt_add_cb(spa_t *spa, zilog_t *zilog, const blkptr_t *bp,
27837802d7bfSMatthew Ahrens     const zbookmark_phys_t *zb, const dnode_phys_t *dnp, void *arg)
2784b24ab676SJeff Bonwick {
2785b24ab676SJeff Bonwick 	avl_tree_t *t = arg;
2786b24ab676SJeff Bonwick 	avl_index_t where;
2787b24ab676SJeff Bonwick 	zdb_ddt_entry_t *zdde, zdde_search;
2788b24ab676SJeff Bonwick 
27895d7b4d43SMatthew Ahrens 	if (BP_IS_HOLE(bp) || BP_IS_EMBEDDED(bp))
2790b24ab676SJeff Bonwick 		return (0);
2791b24ab676SJeff Bonwick 
2792b24ab676SJeff Bonwick 	if (dump_opt['S'] > 1 && zb->zb_level == ZB_ROOT_LEVEL) {
2793b24ab676SJeff Bonwick 		(void) printf("traversing objset %llu, %llu objects, "
2794b24ab676SJeff Bonwick 		    "%lu blocks so far\n",
2795b24ab676SJeff Bonwick 		    (u_longlong_t)zb->zb_objset,
27965d7b4d43SMatthew Ahrens 		    (u_longlong_t)BP_GET_FILL(bp),
2797b24ab676SJeff Bonwick 		    avl_numnodes(t));
2798b24ab676SJeff Bonwick 	}
2799b24ab676SJeff Bonwick 
2800bbfd46c4SJeff Bonwick 	if (BP_IS_HOLE(bp) || BP_GET_CHECKSUM(bp) == ZIO_CHECKSUM_OFF ||
2801ad135b5dSChristopher Siden 	    BP_GET_LEVEL(bp) > 0 || DMU_OT_IS_METADATA(BP_GET_TYPE(bp)))
2802b24ab676SJeff Bonwick 		return (0);
2803b24ab676SJeff Bonwick 
2804b24ab676SJeff Bonwick 	ddt_key_fill(&zdde_search.zdde_key, bp);
2805b24ab676SJeff Bonwick 
2806b24ab676SJeff Bonwick 	zdde = avl_find(t, &zdde_search, &where);
2807b24ab676SJeff Bonwick 
2808b24ab676SJeff Bonwick 	if (zdde == NULL) {
2809b24ab676SJeff Bonwick 		zdde = umem_zalloc(sizeof (*zdde), UMEM_NOFAIL);
2810b24ab676SJeff Bonwick 		zdde->zdde_key = zdde_search.zdde_key;
2811b24ab676SJeff Bonwick 		avl_insert(t, zdde, where);
2812b24ab676SJeff Bonwick 	}
2813b24ab676SJeff Bonwick 
2814b24ab676SJeff Bonwick 	zdde->zdde_ref_blocks += 1;
2815b24ab676SJeff Bonwick 	zdde->zdde_ref_lsize += BP_GET_LSIZE(bp);
2816b24ab676SJeff Bonwick 	zdde->zdde_ref_psize += BP_GET_PSIZE(bp);
2817b24ab676SJeff Bonwick 	zdde->zdde_ref_dsize += bp_get_dsize_sync(spa, bp);
2818b24ab676SJeff Bonwick 
2819b24ab676SJeff Bonwick 	return (0);
2820b24ab676SJeff Bonwick }
2821b24ab676SJeff Bonwick 
2822b24ab676SJeff Bonwick static void
2823b24ab676SJeff Bonwick dump_simulated_ddt(spa_t *spa)
2824b24ab676SJeff Bonwick {
2825b24ab676SJeff Bonwick 	avl_tree_t t;
2826b24ab676SJeff Bonwick 	void *cookie = NULL;
2827b24ab676SJeff Bonwick 	zdb_ddt_entry_t *zdde;
2828b24ab676SJeff Bonwick 	ddt_histogram_t ddh_total = { 0 };
2829b24ab676SJeff Bonwick 	ddt_stat_t dds_total = { 0 };
2830b24ab676SJeff Bonwick 
2831b24ab676SJeff Bonwick 	avl_create(&t, ddt_entry_compare,
2832b24ab676SJeff Bonwick 	    sizeof (zdb_ddt_entry_t), offsetof(zdb_ddt_entry_t, zdde_node));
2833b24ab676SJeff Bonwick 
2834b24ab676SJeff Bonwick 	spa_config_enter(spa, SCL_CONFIG, FTAG, RW_READER);
2835b24ab676SJeff Bonwick 
2836bbfd46c4SJeff Bonwick 	(void) traverse_pool(spa, 0, TRAVERSE_PRE | TRAVERSE_PREFETCH_METADATA,
2837bbfd46c4SJeff Bonwick 	    zdb_ddt_add_cb, &t);
2838b24ab676SJeff Bonwick 
2839b24ab676SJeff Bonwick 	spa_config_exit(spa, SCL_CONFIG, FTAG);
2840b24ab676SJeff Bonwick 
2841b24ab676SJeff Bonwick 	while ((zdde = avl_destroy_nodes(&t, &cookie)) != NULL) {
2842b24ab676SJeff Bonwick 		ddt_stat_t dds;
2843b24ab676SJeff Bonwick 		uint64_t refcnt = zdde->zdde_ref_blocks;
2844b24ab676SJeff Bonwick 		ASSERT(refcnt != 0);
2845b24ab676SJeff Bonwick 
2846b24ab676SJeff Bonwick 		dds.dds_blocks = zdde->zdde_ref_blocks / refcnt;
2847b24ab676SJeff Bonwick 		dds.dds_lsize = zdde->zdde_ref_lsize / refcnt;
2848b24ab676SJeff Bonwick 		dds.dds_psize = zdde->zdde_ref_psize / refcnt;
2849b24ab676SJeff Bonwick 		dds.dds_dsize = zdde->zdde_ref_dsize / refcnt;
2850b24ab676SJeff Bonwick 
2851b24ab676SJeff Bonwick 		dds.dds_ref_blocks = zdde->zdde_ref_blocks;
2852b24ab676SJeff Bonwick 		dds.dds_ref_lsize = zdde->zdde_ref_lsize;
2853b24ab676SJeff Bonwick 		dds.dds_ref_psize = zdde->zdde_ref_psize;
2854b24ab676SJeff Bonwick 		dds.dds_ref_dsize = zdde->zdde_ref_dsize;
2855b24ab676SJeff Bonwick 
2856bf16b11eSMatthew Ahrens 		ddt_stat_add(&ddh_total.ddh_stat[highbit64(refcnt) - 1],
2857bf16b11eSMatthew Ahrens 		    &dds, 0);
2858b24ab676SJeff Bonwick 
2859b24ab676SJeff Bonwick 		umem_free(zdde, sizeof (*zdde));
2860b24ab676SJeff Bonwick 	}
2861b24ab676SJeff Bonwick 
2862b24ab676SJeff Bonwick 	avl_destroy(&t);
2863b24ab676SJeff Bonwick 
2864b24ab676SJeff Bonwick 	ddt_histogram_stat(&dds_total, &ddh_total);
2865b24ab676SJeff Bonwick 
2866b24ab676SJeff Bonwick 	(void) printf("Simulated DDT histogram:\n");
2867b24ab676SJeff Bonwick 
28689eb19f4dSGeorge Wilson 	zpool_dump_ddt(&dds_total, &ddh_total);
2869b24ab676SJeff Bonwick 
2870b24ab676SJeff Bonwick 	dump_dedup_ratio(&dds_total);
2871b24ab676SJeff Bonwick }
2872b24ab676SJeff Bonwick 
2873fa9e4066Sahrens static void
2874fa9e4066Sahrens dump_zpool(spa_t *spa)
2875fa9e4066Sahrens {
2876fa9e4066Sahrens 	dsl_pool_t *dp = spa_get_dsl(spa);
2877fa9e4066Sahrens 	int rc = 0;
2878fa9e4066Sahrens 
2879b24ab676SJeff Bonwick 	if (dump_opt['S']) {
2880b24ab676SJeff Bonwick 		dump_simulated_ddt(spa);
2881b24ab676SJeff Bonwick 		return;
2882b24ab676SJeff Bonwick 	}
2883b24ab676SJeff Bonwick 
288407428bdfSVictor Latushkin 	if (!dump_opt['e'] && dump_opt['C'] > 1) {
288507428bdfSVictor Latushkin 		(void) printf("\nCached configuration:\n");
288607428bdfSVictor Latushkin 		dump_nvlist(spa->spa_config, 8);
288707428bdfSVictor Latushkin 	}
288807428bdfSVictor Latushkin 
288907428bdfSVictor Latushkin 	if (dump_opt['C'])
289007428bdfSVictor Latushkin 		dump_config(spa);
289107428bdfSVictor Latushkin 
2892fa9e4066Sahrens 	if (dump_opt['u'])
289353b9a4a9SVictor Latushkin 		dump_uberblock(&spa->spa_uberblock, "\nUberblock:\n", "\n");
2894fa9e4066Sahrens 
2895b24ab676SJeff Bonwick 	if (dump_opt['D'])
2896b24ab676SJeff Bonwick 		dump_all_ddts(spa);
2897b24ab676SJeff Bonwick 
289887219db7SVictor Latushkin 	if (dump_opt['d'] > 2 || dump_opt['m'])
289987219db7SVictor Latushkin 		dump_metaslabs(spa);
29002e4c9986SGeorge Wilson 	if (dump_opt['M'])
29012e4c9986SGeorge Wilson 		dump_metaslab_groups(spa);
290287219db7SVictor Latushkin 
290387219db7SVictor Latushkin 	if (dump_opt['d'] || dump_opt['i']) {
2904fa9e4066Sahrens 		dump_dir(dp->dp_meta_objset);
2905fa9e4066Sahrens 		if (dump_opt['d'] >= 3) {
2906d0475637SMatthew Ahrens 			dump_bpobj(&spa->spa_deferred_bpobj,
2907d0475637SMatthew Ahrens 			    "Deferred frees", 0);
2908cde58dbcSMatthew Ahrens 			if (spa_version(spa) >= SPA_VERSION_DEADLISTS) {
2909cde58dbcSMatthew Ahrens 				dump_bpobj(&spa->spa_dsl_pool->dp_free_bpobj,
2910d0475637SMatthew Ahrens 				    "Pool snapshot frees", 0);
2911ad135b5dSChristopher Siden 			}
2912ad135b5dSChristopher Siden 
2913ad135b5dSChristopher Siden 			if (spa_feature_is_active(spa,
29142acef22dSMatthew Ahrens 			    SPA_FEATURE_ASYNC_DESTROY)) {
2915ad135b5dSChristopher Siden 				dump_bptree(spa->spa_meta_objset,
2916ad135b5dSChristopher Siden 				    spa->spa_dsl_pool->dp_bptree_obj,
2917ad135b5dSChristopher Siden 				    "Pool dataset frees");
2918cde58dbcSMatthew Ahrens 			}
2919fa9e4066Sahrens 			dump_dtl(spa->spa_root_vdev, 0);
2920fa9e4066Sahrens 		}
292107428bdfSVictor Latushkin 		(void) dmu_objset_find(spa_name(spa), dump_one_dir,
292207428bdfSVictor Latushkin 		    NULL, DS_FIND_SNAPSHOTS | DS_FIND_CHILDREN);
2923fa9e4066Sahrens 	}
2924b24ab676SJeff Bonwick 	if (dump_opt['b'] || dump_opt['c'])
2925fa9e4066Sahrens 		rc = dump_block_stats(spa);
2926fa9e4066Sahrens 
29270713e232SGeorge Wilson 	if (rc == 0)
29280713e232SGeorge Wilson 		rc = verify_spacemap_refcounts(spa);
29290713e232SGeorge Wilson 
2930fa9e4066Sahrens 	if (dump_opt['s'])
2931fa9e4066Sahrens 		show_pool_stats(spa);
2932fa9e4066Sahrens 
29338f18d1faSGeorge Wilson 	if (dump_opt['h'])
29348f18d1faSGeorge Wilson 		dump_history(spa);
29358f18d1faSGeorge Wilson 
2936fa9e4066Sahrens 	if (rc != 0)
2937fa9e4066Sahrens 		exit(rc);
2938fa9e4066Sahrens }
2939fa9e4066Sahrens 
294044cd46caSbillm #define	ZDB_FLAG_CHECKSUM	0x0001
294144cd46caSbillm #define	ZDB_FLAG_DECOMPRESS	0x0002
294244cd46caSbillm #define	ZDB_FLAG_BSWAP		0x0004
294344cd46caSbillm #define	ZDB_FLAG_GBH		0x0008
294444cd46caSbillm #define	ZDB_FLAG_INDIRECT	0x0010
294544cd46caSbillm #define	ZDB_FLAG_PHYS		0x0020
294644cd46caSbillm #define	ZDB_FLAG_RAW		0x0040
294744cd46caSbillm #define	ZDB_FLAG_PRINT_BLKPTR	0x0080
294844cd46caSbillm 
294944cd46caSbillm int flagbits[256];
295044cd46caSbillm 
295144cd46caSbillm static void
295244cd46caSbillm zdb_print_blkptr(blkptr_t *bp, int flags)
295344cd46caSbillm {
2954b24ab676SJeff Bonwick 	char blkbuf[BP_SPRINTF_LEN];
295544cd46caSbillm 
295644cd46caSbillm 	if (flags & ZDB_FLAG_BSWAP)
295744cd46caSbillm 		byteswap_uint64_array((void *)bp, sizeof (blkptr_t));
2958b24ab676SJeff Bonwick 
295943466aaeSMax Grossman 	snprintf_blkptr(blkbuf, sizeof (blkbuf), bp);
2960b24ab676SJeff Bonwick 	(void) printf("%s\n", blkbuf);
296144cd46caSbillm }
296244cd46caSbillm 
296344cd46caSbillm static void
296444cd46caSbillm zdb_dump_indirect(blkptr_t *bp, int nbps, int flags)
296544cd46caSbillm {
296644cd46caSbillm 	int i;
296744cd46caSbillm 
296844cd46caSbillm 	for (i = 0; i < nbps; i++)
296944cd46caSbillm 		zdb_print_blkptr(&bp[i], flags);
297044cd46caSbillm }
297144cd46caSbillm 
297244cd46caSbillm static void
297344cd46caSbillm zdb_dump_gbh(void *buf, int flags)
297444cd46caSbillm {
297544cd46caSbillm 	zdb_dump_indirect((blkptr_t *)buf, SPA_GBH_NBLKPTRS, flags);
297644cd46caSbillm }
297744cd46caSbillm 
297844cd46caSbillm static void
297944cd46caSbillm zdb_dump_block_raw(void *buf, uint64_t size, int flags)
298044cd46caSbillm {
298144cd46caSbillm 	if (flags & ZDB_FLAG_BSWAP)
298244cd46caSbillm 		byteswap_uint64_array(buf, size);
2983b24ab676SJeff Bonwick 	(void) write(1, buf, size);
298444cd46caSbillm }
298544cd46caSbillm 
298644cd46caSbillm static void
298744cd46caSbillm zdb_dump_block(char *label, void *buf, uint64_t size, int flags)
298844cd46caSbillm {
298944cd46caSbillm 	uint64_t *d = (uint64_t *)buf;
299044cd46caSbillm 	int nwords = size / sizeof (uint64_t);
299144cd46caSbillm 	int do_bswap = !!(flags & ZDB_FLAG_BSWAP);
299244cd46caSbillm 	int i, j;
299344cd46caSbillm 	char *hdr, *c;
299444cd46caSbillm 
299544cd46caSbillm 
299644cd46caSbillm 	if (do_bswap)
299744cd46caSbillm 		hdr = " 7 6 5 4 3 2 1 0   f e d c b a 9 8";
299844cd46caSbillm 	else
299944cd46caSbillm 		hdr = " 0 1 2 3 4 5 6 7   8 9 a b c d e f";
300044cd46caSbillm 
300144cd46caSbillm 	(void) printf("\n%s\n%6s   %s  0123456789abcdef\n", label, "", hdr);
300244cd46caSbillm 
300344cd46caSbillm 	for (i = 0; i < nwords; i += 2) {
300444cd46caSbillm 		(void) printf("%06llx:  %016llx  %016llx  ",
300544cd46caSbillm 		    (u_longlong_t)(i * sizeof (uint64_t)),
300644cd46caSbillm 		    (u_longlong_t)(do_bswap ? BSWAP_64(d[i]) : d[i]),
300744cd46caSbillm 		    (u_longlong_t)(do_bswap ? BSWAP_64(d[i + 1]) : d[i + 1]));
300844cd46caSbillm 
300944cd46caSbillm 		c = (char *)&d[i];
301044cd46caSbillm 		for (j = 0; j < 2 * sizeof (uint64_t); j++)
301144cd46caSbillm 			(void) printf("%c", isprint(c[j]) ? c[j] : '.');
301244cd46caSbillm 		(void) printf("\n");
301344cd46caSbillm 	}
301444cd46caSbillm }
301544cd46caSbillm 
301644cd46caSbillm /*
301744cd46caSbillm  * There are two acceptable formats:
301844cd46caSbillm  *	leaf_name	  - For example: c1t0d0 or /tmp/ztest.0a
301944cd46caSbillm  *	child[.child]*    - For example: 0.1.1
302044cd46caSbillm  *
302144cd46caSbillm  * The second form can be used to specify arbitrary vdevs anywhere
302244cd46caSbillm  * in the heirarchy.  For example, in a pool with a mirror of
302344cd46caSbillm  * RAID-Zs, you can specify either RAID-Z vdev with 0.0 or 0.1 .
302444cd46caSbillm  */
302544cd46caSbillm static vdev_t *
302644cd46caSbillm zdb_vdev_lookup(vdev_t *vdev, char *path)
302744cd46caSbillm {
302844cd46caSbillm 	char *s, *p, *q;
302944cd46caSbillm 	int i;
303044cd46caSbillm 
303144cd46caSbillm 	if (vdev == NULL)
303244cd46caSbillm 		return (NULL);
303344cd46caSbillm 
303444cd46caSbillm 	/* First, assume the x.x.x.x format */
303544cd46caSbillm 	i = (int)strtoul(path, &s, 10);
303644cd46caSbillm 	if (s == path || (s && *s != '.' && *s != '\0'))
303744cd46caSbillm 		goto name;
303844cd46caSbillm 	if (i < 0 || i >= vdev->vdev_children)
303944cd46caSbillm 		return (NULL);
304044cd46caSbillm 
304144cd46caSbillm 	vdev = vdev->vdev_child[i];
304244cd46caSbillm 	if (*s == '\0')
304344cd46caSbillm 		return (vdev);
304444cd46caSbillm 	return (zdb_vdev_lookup(vdev, s+1));
304544cd46caSbillm 
304644cd46caSbillm name:
304744cd46caSbillm 	for (i = 0; i < vdev->vdev_children; i++) {
304844cd46caSbillm 		vdev_t *vc = vdev->vdev_child[i];
304944cd46caSbillm 
305044cd46caSbillm 		if (vc->vdev_path == NULL) {
305144cd46caSbillm 			vc = zdb_vdev_lookup(vc, path);
305244cd46caSbillm 			if (vc == NULL)
305344cd46caSbillm 				continue;
305444cd46caSbillm 			else
305544cd46caSbillm 				return (vc);
305644cd46caSbillm 		}
305744cd46caSbillm 
305844cd46caSbillm 		p = strrchr(vc->vdev_path, '/');
305944cd46caSbillm 		p = p ? p + 1 : vc->vdev_path;
306044cd46caSbillm 		q = &vc->vdev_path[strlen(vc->vdev_path) - 2];
306144cd46caSbillm 
306244cd46caSbillm 		if (strcmp(vc->vdev_path, path) == 0)
306344cd46caSbillm 			return (vc);
306444cd46caSbillm 		if (strcmp(p, path) == 0)
306544cd46caSbillm 			return (vc);
306644cd46caSbillm 		if (strcmp(q, "s0") == 0 && strncmp(p, path, q - p) == 0)
306744cd46caSbillm 			return (vc);
306844cd46caSbillm 	}
306944cd46caSbillm 
307044cd46caSbillm 	return (NULL);
307144cd46caSbillm }
307244cd46caSbillm 
307344cd46caSbillm /*
307444cd46caSbillm  * Read a block from a pool and print it out.  The syntax of the
307544cd46caSbillm  * block descriptor is:
307644cd46caSbillm  *
307744cd46caSbillm  *	pool:vdev_specifier:offset:size[:flags]
307844cd46caSbillm  *
307944cd46caSbillm  *	pool           - The name of the pool you wish to read from
308044cd46caSbillm  *	vdev_specifier - Which vdev (see comment for zdb_vdev_lookup)
308144cd46caSbillm  *	offset         - offset, in hex, in bytes
308244cd46caSbillm  *	size           - Amount of data to read, in hex, in bytes
308344cd46caSbillm  *	flags          - A string of characters specifying options
308444cd46caSbillm  *		 b: Decode a blkptr at given offset within block
308544cd46caSbillm  *		*c: Calculate and display checksums
3086b24ab676SJeff Bonwick  *		 d: Decompress data before dumping
308744cd46caSbillm  *		 e: Byteswap data before dumping
3088b24ab676SJeff Bonwick  *		 g: Display data as a gang block header
3089b24ab676SJeff Bonwick  *		 i: Display as an indirect block
309044cd46caSbillm  *		 p: Do I/O to physical offset
309144cd46caSbillm  *		 r: Dump raw data to stdout
309244cd46caSbillm  *
309344cd46caSbillm  *              * = not yet implemented
309444cd46caSbillm  */
309544cd46caSbillm static void
309607428bdfSVictor Latushkin zdb_read_block(char *thing, spa_t *spa)
309744cd46caSbillm {
3098b24ab676SJeff Bonwick 	blkptr_t blk, *bp = &blk;
3099b24ab676SJeff Bonwick 	dva_t *dva = bp->blk_dva;
310044cd46caSbillm 	int flags = 0;
3101b24ab676SJeff Bonwick 	uint64_t offset = 0, size = 0, psize = 0, lsize = 0, blkptr_offset = 0;
310244cd46caSbillm 	zio_t *zio;
310344cd46caSbillm 	vdev_t *vd;
3104b24ab676SJeff Bonwick 	void *pbuf, *lbuf, *buf;
310507428bdfSVictor Latushkin 	char *s, *p, *dup, *vdev, *flagstr;
3106b24ab676SJeff Bonwick 	int i, error;
310744cd46caSbillm 
310844cd46caSbillm 	dup = strdup(thing);
310944cd46caSbillm 	s = strtok(dup, ":");
311044cd46caSbillm 	vdev = s ? s : "";
311144cd46caSbillm 	s = strtok(NULL, ":");
311244cd46caSbillm 	offset = strtoull(s ? s : "", NULL, 16);
311344cd46caSbillm 	s = strtok(NULL, ":");
311444cd46caSbillm 	size = strtoull(s ? s : "", NULL, 16);
311544cd46caSbillm 	s = strtok(NULL, ":");
311644cd46caSbillm 	flagstr = s ? s : "";
311744cd46caSbillm 
311844cd46caSbillm 	s = NULL;
311944cd46caSbillm 	if (size == 0)
312044cd46caSbillm 		s = "size must not be zero";
312144cd46caSbillm 	if (!IS_P2ALIGNED(size, DEV_BSIZE))
312244cd46caSbillm 		s = "size must be a multiple of sector size";
312344cd46caSbillm 	if (!IS_P2ALIGNED(offset, DEV_BSIZE))
312444cd46caSbillm 		s = "offset must be a multiple of sector size";
312544cd46caSbillm 	if (s) {
312644cd46caSbillm 		(void) printf("Invalid block specifier: %s  - %s\n", thing, s);
312744cd46caSbillm 		free(dup);
312844cd46caSbillm 		return;
312944cd46caSbillm 	}
313044cd46caSbillm 
313144cd46caSbillm 	for (s = strtok(flagstr, ":"); s; s = strtok(NULL, ":")) {
313244cd46caSbillm 		for (i = 0; flagstr[i]; i++) {
31335ad82045Snd 			int bit = flagbits[(uchar_t)flagstr[i]];
313444cd46caSbillm 
313544cd46caSbillm 			if (bit == 0) {
313644cd46caSbillm 				(void) printf("***Invalid flag: %c\n",
313744cd46caSbillm 				    flagstr[i]);
313844cd46caSbillm 				continue;
313944cd46caSbillm 			}
314044cd46caSbillm 			flags |= bit;
314144cd46caSbillm 
314244cd46caSbillm 			/* If it's not something with an argument, keep going */
3143b24ab676SJeff Bonwick 			if ((bit & (ZDB_FLAG_CHECKSUM |
314444cd46caSbillm 			    ZDB_FLAG_PRINT_BLKPTR)) == 0)
314544cd46caSbillm 				continue;
314644cd46caSbillm 
314744cd46caSbillm 			p = &flagstr[i + 1];
314844cd46caSbillm 			if (bit == ZDB_FLAG_PRINT_BLKPTR)
314944cd46caSbillm 				blkptr_offset = strtoull(p, &p, 16);
315044cd46caSbillm 			if (*p != ':' && *p != '\0') {
315144cd46caSbillm 				(void) printf("***Invalid flag arg: '%s'\n", s);
315244cd46caSbillm 				free(dup);
315344cd46caSbillm 				return;
315444cd46caSbillm 			}
315544cd46caSbillm 		}
315644cd46caSbillm 	}
315744cd46caSbillm 
315844cd46caSbillm 	vd = zdb_vdev_lookup(spa->spa_root_vdev, vdev);
315944cd46caSbillm 	if (vd == NULL) {
316044cd46caSbillm 		(void) printf("***Invalid vdev: %s\n", vdev);
316144cd46caSbillm 		free(dup);
316244cd46caSbillm 		return;
316344cd46caSbillm 	} else {
316444cd46caSbillm 		if (vd->vdev_path)
3165b24ab676SJeff Bonwick 			(void) fprintf(stderr, "Found vdev: %s\n",
3166b24ab676SJeff Bonwick 			    vd->vdev_path);
316744cd46caSbillm 		else
3168b24ab676SJeff Bonwick 			(void) fprintf(stderr, "Found vdev type: %s\n",
316944cd46caSbillm 			    vd->vdev_ops->vdev_op_type);
317044cd46caSbillm 	}
317144cd46caSbillm 
3172b24ab676SJeff Bonwick 	psize = size;
3173b24ab676SJeff Bonwick 	lsize = size;
317444cd46caSbillm 
3175b24ab676SJeff Bonwick 	pbuf = umem_alloc(SPA_MAXBLOCKSIZE, UMEM_NOFAIL);
3176b24ab676SJeff Bonwick 	lbuf = umem_alloc(SPA_MAXBLOCKSIZE, UMEM_NOFAIL);
3177b24ab676SJeff Bonwick 
3178b24ab676SJeff Bonwick 	BP_ZERO(bp);
3179b24ab676SJeff Bonwick 
3180b24ab676SJeff Bonwick 	DVA_SET_VDEV(&dva[0], vd->vdev_id);
3181b24ab676SJeff Bonwick 	DVA_SET_OFFSET(&dva[0], offset);
3182b24ab676SJeff Bonwick 	DVA_SET_GANG(&dva[0], !!(flags & ZDB_FLAG_GBH));
3183b24ab676SJeff Bonwick 	DVA_SET_ASIZE(&dva[0], vdev_psize_to_asize(vd, psize));
3184b24ab676SJeff Bonwick 
3185b24ab676SJeff Bonwick 	BP_SET_BIRTH(bp, TXG_INITIAL, TXG_INITIAL);
3186b24ab676SJeff Bonwick 
3187b24ab676SJeff Bonwick 	BP_SET_LSIZE(bp, lsize);
3188b24ab676SJeff Bonwick 	BP_SET_PSIZE(bp, psize);
3189b24ab676SJeff Bonwick 	BP_SET_COMPRESS(bp, ZIO_COMPRESS_OFF);
3190b24ab676SJeff Bonwick 	BP_SET_CHECKSUM(bp, ZIO_CHECKSUM_OFF);
3191b24ab676SJeff Bonwick 	BP_SET_TYPE(bp, DMU_OT_NONE);
3192b24ab676SJeff Bonwick 	BP_SET_LEVEL(bp, 0);
3193b24ab676SJeff Bonwick 	BP_SET_DEDUP(bp, 0);
3194b24ab676SJeff Bonwick 	BP_SET_BYTEORDER(bp, ZFS_HOST_BYTEORDER);
319544cd46caSbillm 
3196e14bb325SJeff Bonwick 	spa_config_enter(spa, SCL_STATE, FTAG, RW_READER);
319744cd46caSbillm 	zio = zio_root(spa, NULL, NULL, 0);
3198b24ab676SJeff Bonwick 
3199b24ab676SJeff Bonwick 	if (vd == vd->vdev_top) {
3200b24ab676SJeff Bonwick 		/*
3201b24ab676SJeff Bonwick 		 * Treat this as a normal block read.
3202b24ab676SJeff Bonwick 		 */
3203b24ab676SJeff Bonwick 		zio_nowait(zio_read(zio, spa, bp, pbuf, psize, NULL, NULL,
3204b24ab676SJeff Bonwick 		    ZIO_PRIORITY_SYNC_READ,
3205b24ab676SJeff Bonwick 		    ZIO_FLAG_CANFAIL | ZIO_FLAG_RAW, NULL));
3206b24ab676SJeff Bonwick 	} else {
3207b24ab676SJeff Bonwick 		/*
3208b24ab676SJeff Bonwick 		 * Treat this as a vdev child I/O.
3209b24ab676SJeff Bonwick 		 */
3210b24ab676SJeff Bonwick 		zio_nowait(zio_vdev_child_io(zio, bp, vd, offset, pbuf, psize,
3211b24ab676SJeff Bonwick 		    ZIO_TYPE_READ, ZIO_PRIORITY_SYNC_READ,
3212b24ab676SJeff Bonwick 		    ZIO_FLAG_DONT_CACHE | ZIO_FLAG_DONT_QUEUE |
3213b24ab676SJeff Bonwick 		    ZIO_FLAG_DONT_PROPAGATE | ZIO_FLAG_DONT_RETRY |
3214b24ab676SJeff Bonwick 		    ZIO_FLAG_CANFAIL | ZIO_FLAG_RAW, NULL, NULL));
3215b24ab676SJeff Bonwick 	}
3216b24ab676SJeff Bonwick 
321744cd46caSbillm 	error = zio_wait(zio);
3218e14bb325SJeff Bonwick 	spa_config_exit(spa, SCL_STATE, FTAG);
321944cd46caSbillm 
322044cd46caSbillm 	if (error) {
322144cd46caSbillm 		(void) printf("Read of %s failed, error: %d\n", thing, error);
322244cd46caSbillm 		goto out;
322344cd46caSbillm 	}
322444cd46caSbillm 
3225b24ab676SJeff Bonwick 	if (flags & ZDB_FLAG_DECOMPRESS) {
3226b24ab676SJeff Bonwick 		/*
3227b24ab676SJeff Bonwick 		 * We don't know how the data was compressed, so just try
3228b24ab676SJeff Bonwick 		 * every decompress function at every inflated blocksize.
3229b24ab676SJeff Bonwick 		 */
3230b24ab676SJeff Bonwick 		enum zio_compress c;
3231b24ab676SJeff Bonwick 		void *pbuf2 = umem_alloc(SPA_MAXBLOCKSIZE, UMEM_NOFAIL);
3232b24ab676SJeff Bonwick 		void *lbuf2 = umem_alloc(SPA_MAXBLOCKSIZE, UMEM_NOFAIL);
3233b24ab676SJeff Bonwick 
3234b24ab676SJeff Bonwick 		bcopy(pbuf, pbuf2, psize);
3235b24ab676SJeff Bonwick 
3236b24ab676SJeff Bonwick 		VERIFY(random_get_pseudo_bytes((uint8_t *)pbuf + psize,
3237b24ab676SJeff Bonwick 		    SPA_MAXBLOCKSIZE - psize) == 0);
3238b24ab676SJeff Bonwick 
3239b24ab676SJeff Bonwick 		VERIFY(random_get_pseudo_bytes((uint8_t *)pbuf2 + psize,
3240b24ab676SJeff Bonwick 		    SPA_MAXBLOCKSIZE - psize) == 0);
3241b24ab676SJeff Bonwick 
3242b24ab676SJeff Bonwick 		for (lsize = SPA_MAXBLOCKSIZE; lsize > psize;
3243b24ab676SJeff Bonwick 		    lsize -= SPA_MINBLOCKSIZE) {
3244b24ab676SJeff Bonwick 			for (c = 0; c < ZIO_COMPRESS_FUNCTIONS; c++) {
3245b24ab676SJeff Bonwick 				if (zio_decompress_data(c, pbuf, lbuf,
3246b24ab676SJeff Bonwick 				    psize, lsize) == 0 &&
3247b24ab676SJeff Bonwick 				    zio_decompress_data(c, pbuf2, lbuf2,
3248b24ab676SJeff Bonwick 				    psize, lsize) == 0 &&
3249b24ab676SJeff Bonwick 				    bcmp(lbuf, lbuf2, lsize) == 0)
3250b24ab676SJeff Bonwick 					break;
3251b24ab676SJeff Bonwick 			}
3252b24ab676SJeff Bonwick 			if (c != ZIO_COMPRESS_FUNCTIONS)
3253b24ab676SJeff Bonwick 				break;
3254b24ab676SJeff Bonwick 			lsize -= SPA_MINBLOCKSIZE;
3255b24ab676SJeff Bonwick 		}
3256b24ab676SJeff Bonwick 
3257b24ab676SJeff Bonwick 		umem_free(pbuf2, SPA_MAXBLOCKSIZE);
3258b24ab676SJeff Bonwick 		umem_free(lbuf2, SPA_MAXBLOCKSIZE);
3259b24ab676SJeff Bonwick 
3260b24ab676SJeff Bonwick 		if (lsize <= psize) {
3261b24ab676SJeff Bonwick 			(void) printf("Decompress of %s failed\n", thing);
3262b24ab676SJeff Bonwick 			goto out;
3263b24ab676SJeff Bonwick 		}
3264b24ab676SJeff Bonwick 		buf = lbuf;
3265b24ab676SJeff Bonwick 		size = lsize;
3266b24ab676SJeff Bonwick 	} else {
3267b24ab676SJeff Bonwick 		buf = pbuf;
3268b24ab676SJeff Bonwick 		size = psize;
3269b24ab676SJeff Bonwick 	}
3270b24ab676SJeff Bonwick 
327144cd46caSbillm 	if (flags & ZDB_FLAG_PRINT_BLKPTR)
327244cd46caSbillm 		zdb_print_blkptr((blkptr_t *)(void *)
327344cd46caSbillm 		    ((uintptr_t)buf + (uintptr_t)blkptr_offset), flags);
327444cd46caSbillm 	else if (flags & ZDB_FLAG_RAW)
327544cd46caSbillm 		zdb_dump_block_raw(buf, size, flags);
327644cd46caSbillm 	else if (flags & ZDB_FLAG_INDIRECT)
327744cd46caSbillm 		zdb_dump_indirect((blkptr_t *)buf, size / sizeof (blkptr_t),
327844cd46caSbillm 		    flags);
327944cd46caSbillm 	else if (flags & ZDB_FLAG_GBH)
328044cd46caSbillm 		zdb_dump_gbh(buf, flags);
328144cd46caSbillm 	else
328244cd46caSbillm 		zdb_dump_block(thing, buf, size, flags);
328344cd46caSbillm 
328444cd46caSbillm out:
3285b24ab676SJeff Bonwick 	umem_free(pbuf, SPA_MAXBLOCKSIZE);
3286b24ab676SJeff Bonwick 	umem_free(lbuf, SPA_MAXBLOCKSIZE);
328744cd46caSbillm 	free(dup);
328844cd46caSbillm }
328944cd46caSbillm 
3290de6628f0Sck static boolean_t
32913ad6c7f9SVictor Latushkin pool_match(nvlist_t *cfg, char *tgt)
3292de6628f0Sck {
32933ad6c7f9SVictor Latushkin 	uint64_t v, guid = strtoull(tgt, NULL, 0);
3294de6628f0Sck 	char *s;
3295de6628f0Sck 
3296de6628f0Sck 	if (guid != 0) {
32973ad6c7f9SVictor Latushkin 		if (nvlist_lookup_uint64(cfg, ZPOOL_CONFIG_POOL_GUID, &v) == 0)
32983ad6c7f9SVictor Latushkin 			return (v == guid);
3299de6628f0Sck 	} else {
33003ad6c7f9SVictor Latushkin 		if (nvlist_lookup_string(cfg, ZPOOL_CONFIG_POOL_NAME, &s) == 0)
33013ad6c7f9SVictor Latushkin 			return (strcmp(s, tgt) == 0);
3302de6628f0Sck 	}
33033ad6c7f9SVictor Latushkin 	return (B_FALSE);
3304de6628f0Sck }
3305de6628f0Sck 
33063ad6c7f9SVictor Latushkin static char *
33073ad6c7f9SVictor Latushkin find_zpool(char **target, nvlist_t **configp, int dirc, char **dirv)
3308de6628f0Sck {
3309de6628f0Sck 	nvlist_t *pools;
3310de6628f0Sck 	nvlist_t *match = NULL;
33113ad6c7f9SVictor Latushkin 	char *name = NULL;
33123ad6c7f9SVictor Latushkin 	char *sepp = NULL;
33133ad6c7f9SVictor Latushkin 	char sep;
33143ad6c7f9SVictor Latushkin 	int count = 0;
3315d41c4376SMark J Musante 	importargs_t args = { 0 };
3316d41c4376SMark J Musante 
3317d41c4376SMark J Musante 	args.paths = dirc;
3318d41c4376SMark J Musante 	args.path = dirv;
3319d41c4376SMark J Musante 	args.can_be_active = B_TRUE;
3320de6628f0Sck 
33213ad6c7f9SVictor Latushkin 	if ((sepp = strpbrk(*target, "/@")) != NULL) {
33223ad6c7f9SVictor Latushkin 		sep = *sepp;
33233ad6c7f9SVictor Latushkin 		*sepp = '\0';
33243ad6c7f9SVictor Latushkin 	}
33253ad6c7f9SVictor Latushkin 
3326d41c4376SMark J Musante 	pools = zpool_search_import(g_zfs, &args);
3327de6628f0Sck 
3328de6628f0Sck 	if (pools != NULL) {
3329de6628f0Sck 		nvpair_t *elem = NULL;
3330de6628f0Sck 		while ((elem = nvlist_next_nvpair(pools, elem)) != NULL) {
3331de6628f0Sck 			verify(nvpair_value_nvlist(elem, configp) == 0);
33323ad6c7f9SVictor Latushkin 			if (pool_match(*configp, *target)) {
33333ad6c7f9SVictor Latushkin 				count++;
3334de6628f0Sck 				if (match != NULL) {
33353ad6c7f9SVictor Latushkin 					/* print previously found config */
33363ad6c7f9SVictor Latushkin 					if (name != NULL) {
33373ad6c7f9SVictor Latushkin 						(void) printf("%s\n", name);
33383ad6c7f9SVictor Latushkin 						dump_nvlist(match, 8);
33393ad6c7f9SVictor Latushkin 						name = NULL;
33403ad6c7f9SVictor Latushkin 					}
33413ad6c7f9SVictor Latushkin 					(void) printf("%s\n",
33423ad6c7f9SVictor Latushkin 					    nvpair_name(elem));
33433ad6c7f9SVictor Latushkin 					dump_nvlist(*configp, 8);
3344de6628f0Sck 				} else {
3345de6628f0Sck 					match = *configp;
33463ad6c7f9SVictor Latushkin 					name = nvpair_name(elem);
3347de6628f0Sck 				}
3348de6628f0Sck 			}
3349de6628f0Sck 		}
3350de6628f0Sck 	}
33513ad6c7f9SVictor Latushkin 	if (count > 1)
33523ad6c7f9SVictor Latushkin 		(void) fatal("\tMatched %d pools - use pool GUID "
33533ad6c7f9SVictor Latushkin 		    "instead of pool name or \n"
33543ad6c7f9SVictor Latushkin 		    "\tpool name part of a dataset name to select pool", count);
33553ad6c7f9SVictor Latushkin 
33563ad6c7f9SVictor Latushkin 	if (sepp)
33573ad6c7f9SVictor Latushkin 		*sepp = sep;
33583ad6c7f9SVictor Latushkin 	/*
33593ad6c7f9SVictor Latushkin 	 * If pool GUID was specified for pool id, replace it with pool name
33603ad6c7f9SVictor Latushkin 	 */
33613ad6c7f9SVictor Latushkin 	if (name && (strstr(*target, name) != *target)) {
33623ad6c7f9SVictor Latushkin 		int sz = 1 + strlen(name) + ((sepp) ? strlen(sepp) : 0);
33633ad6c7f9SVictor Latushkin 
33643ad6c7f9SVictor Latushkin 		*target = umem_alloc(sz, UMEM_NOFAIL);
33653ad6c7f9SVictor Latushkin 		(void) snprintf(*target, sz, "%s%s", name, sepp ? sepp : "");
33663ad6c7f9SVictor Latushkin 	}
3367de6628f0Sck 
33683ad6c7f9SVictor Latushkin 	*configp = name ? match : NULL;
3369de6628f0Sck 
33703ad6c7f9SVictor Latushkin 	return (name);
3371de6628f0Sck }
3372de6628f0Sck 
3373fa9e4066Sahrens int
3374fa9e4066Sahrens main(int argc, char **argv)
3375fa9e4066Sahrens {
3376fa9e4066Sahrens 	int i, c;
3377fa9e4066Sahrens 	struct rlimit rl = { 1024, 1024 };
33783ad6c7f9SVictor Latushkin 	spa_t *spa = NULL;
3379fa9e4066Sahrens 	objset_t *os = NULL;
3380fa9e4066Sahrens 	int dump_all = 1;
3381fa9e4066Sahrens 	int verbose = 0;
3382c8ee1847SVictor Latushkin 	int error = 0;
33833ad6c7f9SVictor Latushkin 	char **searchdirs = NULL;
33843ad6c7f9SVictor Latushkin 	int nsearch = 0;
33853ad6c7f9SVictor Latushkin 	char *target;
3386468c413aSTim Haley 	nvlist_t *policy = NULL;
3387468c413aSTim Haley 	uint64_t max_txg = UINT64_MAX;
3388c8ee1847SVictor Latushkin 	int rewind = ZPOOL_NEVER_REWIND;
3389fa9e4066Sahrens 
3390fa9e4066Sahrens 	(void) setrlimit(RLIMIT_NOFILE, &rl);
3391004388ebScasper 	(void) enable_extended_FILE_stdio(-1, -1);
3392fa9e4066Sahrens 
3393fa9e4066Sahrens 	dprintf_setup(&argc, argv);
3394fa9e4066Sahrens 
3395df15e419SMatthew Ahrens 	while ((c = getopt(argc, argv,
33962e4c9986SGeorge Wilson 	    "bcdhilmMI:suCDRSAFLXx:evp:t:U:P")) != -1) {
3397fa9e4066Sahrens 		switch (c) {
3398fa9e4066Sahrens 		case 'b':
3399fa9e4066Sahrens 		case 'c':
3400b24ab676SJeff Bonwick 		case 'd':
3401b24ab676SJeff Bonwick 		case 'h':
3402b24ab676SJeff Bonwick 		case 'i':
3403b24ab676SJeff Bonwick 		case 'l':
3404d6e555bdSGeorge Wilson 		case 'm':
3405fa9e4066Sahrens 		case 's':
3406b24ab676SJeff Bonwick 		case 'u':
3407fa9e4066Sahrens 		case 'C':
3408b24ab676SJeff Bonwick 		case 'D':
34092e4c9986SGeorge Wilson 		case 'M':
341044cd46caSbillm 		case 'R':
3411b24ab676SJeff Bonwick 		case 'S':
3412fa9e4066Sahrens 			dump_opt[c]++;
3413fa9e4066Sahrens 			dump_all = 0;
3414fa9e4066Sahrens 			break;
3415feef89cfSVictor Latushkin 		case 'A':
3416c8ee1847SVictor Latushkin 		case 'F':
341782a0a985SVictor Latushkin 		case 'L':
3418c8ee1847SVictor Latushkin 		case 'X':
34193ad6c7f9SVictor Latushkin 		case 'e':
34203f9d6ad7SLin Ling 		case 'P':
342182a0a985SVictor Latushkin 			dump_opt[c]++;
342282a0a985SVictor Latushkin 			break;
34232e4c9986SGeorge Wilson 		case 'I':
342431d7e8faSGeorge Wilson 			max_inflight = strtoull(optarg, NULL, 0);
342531d7e8faSGeorge Wilson 			if (max_inflight == 0) {
342631d7e8faSGeorge Wilson 				(void) fprintf(stderr, "maximum number "
342731d7e8faSGeorge Wilson 				    "of inflight I/Os must be greater "
342831d7e8faSGeorge Wilson 				    "than 0\n");
342931d7e8faSGeorge Wilson 				usage();
343031d7e8faSGeorge Wilson 			}
343131d7e8faSGeorge Wilson 			break;
3432de6628f0Sck 		case 'p':
34333ad6c7f9SVictor Latushkin 			if (searchdirs == NULL) {
34343ad6c7f9SVictor Latushkin 				searchdirs = umem_alloc(sizeof (char *),
34353ad6c7f9SVictor Latushkin 				    UMEM_NOFAIL);
34363ad6c7f9SVictor Latushkin 			} else {
34373ad6c7f9SVictor Latushkin 				char **tmp = umem_alloc((nsearch + 1) *
34383ad6c7f9SVictor Latushkin 				    sizeof (char *), UMEM_NOFAIL);
34393ad6c7f9SVictor Latushkin 				bcopy(searchdirs, tmp, nsearch *
34403ad6c7f9SVictor Latushkin 				    sizeof (char *));
34413ad6c7f9SVictor Latushkin 				umem_free(searchdirs,
34423ad6c7f9SVictor Latushkin 				    nsearch * sizeof (char *));
34433ad6c7f9SVictor Latushkin 				searchdirs = tmp;
34443ad6c7f9SVictor Latushkin 			}
34453ad6c7f9SVictor Latushkin 			searchdirs[nsearch++] = optarg;
3446de6628f0Sck 			break;
34472e551927SVictor Latushkin 		case 't':
3448468c413aSTim Haley 			max_txg = strtoull(optarg, NULL, 0);
3449468c413aSTim Haley 			if (max_txg < TXG_INITIAL) {
34502e551927SVictor Latushkin 				(void) fprintf(stderr, "incorrect txg "
34512e551927SVictor Latushkin 				    "specified: %s\n", optarg);
34522e551927SVictor Latushkin 				usage();
34532e551927SVictor Latushkin 			}
34542e551927SVictor Latushkin 			break;
3455b24ab676SJeff Bonwick 		case 'U':
3456b24ab676SJeff Bonwick 			spa_config_path = optarg;
3457b24ab676SJeff Bonwick 			break;
34582e4c9986SGeorge Wilson 		case 'v':
34592e4c9986SGeorge Wilson 			verbose++;
34602e4c9986SGeorge Wilson 			break;
34612e4c9986SGeorge Wilson 		case 'x':
34622e4c9986SGeorge Wilson 			vn_dumpdir = optarg;
34632e4c9986SGeorge Wilson 			break;
3464fa9e4066Sahrens 		default:
3465fa9e4066Sahrens 			usage();
3466fa9e4066Sahrens 			break;
3467fa9e4066Sahrens 		}
3468fa9e4066Sahrens 	}
3469fa9e4066Sahrens 
34703ad6c7f9SVictor Latushkin 	if (!dump_opt['e'] && searchdirs != NULL) {
347188b7b0f2SMatthew Ahrens 		(void) fprintf(stderr, "-p option requires use of -e\n");
347288b7b0f2SMatthew Ahrens 		usage();
347388b7b0f2SMatthew Ahrens 	}
3474de6628f0Sck 
3475*06be9802SMatthew Ahrens 	/*
3476*06be9802SMatthew Ahrens 	 * ZDB does not typically re-read blocks; therefore limit the ARC
3477*06be9802SMatthew Ahrens 	 * to 256 MB, which can be used entirely for metadata.
3478*06be9802SMatthew Ahrens 	 */
3479*06be9802SMatthew Ahrens 	zfs_arc_max = zfs_arc_meta_limit = 256 * 1024 * 1024;
3480*06be9802SMatthew Ahrens 
3481fa9e4066Sahrens 	kernel_init(FREAD);
3482de6628f0Sck 	g_zfs = libzfs_init();
348391ebeef5Sahrens 	ASSERT(g_zfs != NULL);
3484fa9e4066Sahrens 
3485b24ab676SJeff Bonwick 	if (dump_all)
3486b24ab676SJeff Bonwick 		verbose = MAX(verbose, 1);
3487b24ab676SJeff Bonwick 
3488fa9e4066Sahrens 	for (c = 0; c < 256; c++) {
34893f9d6ad7SLin Ling 		if (dump_all && !strchr("elAFLRSXP", c))
3490fa9e4066Sahrens 			dump_opt[c] = 1;
3491fa9e4066Sahrens 		if (dump_opt[c])
3492fa9e4066Sahrens 			dump_opt[c] += verbose;
3493fa9e4066Sahrens 	}
3494fa9e4066Sahrens 
3495feef89cfSVictor Latushkin 	aok = (dump_opt['A'] == 1) || (dump_opt['A'] > 2);
3496feef89cfSVictor Latushkin 	zfs_recover = (dump_opt['A'] > 1);
3497feef89cfSVictor Latushkin 
3498fa9e4066Sahrens 	argc -= optind;
3499fa9e4066Sahrens 	argv += optind;
3500fa9e4066Sahrens 
350107428bdfSVictor Latushkin 	if (argc < 2 && dump_opt['R'])
350207428bdfSVictor Latushkin 		usage();
3503fa9e4066Sahrens 	if (argc < 1) {
35043ad6c7f9SVictor Latushkin 		if (!dump_opt['e'] && dump_opt['C']) {
3505e829d913Sck 			dump_cachefile(spa_config_path);
3506fa9e4066Sahrens 			return (0);
3507fa9e4066Sahrens 		}
3508fa9e4066Sahrens 		usage();
3509fa9e4066Sahrens 	}
3510fa9e4066Sahrens 
3511fa9e4066Sahrens 	if (dump_opt['l']) {
3512fa9e4066Sahrens 		dump_label(argv[0]);
3513fa9e4066Sahrens 		return (0);
3514fa9e4066Sahrens 	}
3515fa9e4066Sahrens 
3516c8ee1847SVictor Latushkin 	if (dump_opt['X'] || dump_opt['F'])
3517c8ee1847SVictor Latushkin 		rewind = ZPOOL_DO_REWIND |
3518c8ee1847SVictor Latushkin 		    (dump_opt['X'] ? ZPOOL_EXTREME_REWIND : 0);
3519c8ee1847SVictor Latushkin 
3520c8ee1847SVictor Latushkin 	if (nvlist_alloc(&policy, NV_UNIQUE_NAME_TYPE, 0) != 0 ||
3521c8ee1847SVictor Latushkin 	    nvlist_add_uint64(policy, ZPOOL_REWIND_REQUEST_TXG, max_txg) != 0 ||
3522c8ee1847SVictor Latushkin 	    nvlist_add_uint32(policy, ZPOOL_REWIND_REQUEST, rewind) != 0)
3523c8ee1847SVictor Latushkin 		fatal("internal error: %s", strerror(ENOMEM));
3524c8ee1847SVictor Latushkin 
3525c5904d13Seschrock 	error = 0;
35263ad6c7f9SVictor Latushkin 	target = argv[0];
3527990b4856Slling 
35283ad6c7f9SVictor Latushkin 	if (dump_opt['e']) {
35293ad6c7f9SVictor Latushkin 		nvlist_t *cfg = NULL;
35303ad6c7f9SVictor Latushkin 		char *name = find_zpool(&target, &cfg, nsearch, searchdirs);
3531990b4856Slling 
35323ad6c7f9SVictor Latushkin 		error = ENOENT;
35333ad6c7f9SVictor Latushkin 		if (name) {
353407428bdfSVictor Latushkin 			if (dump_opt['C'] > 1) {
353507428bdfSVictor Latushkin 				(void) printf("\nConfiguration for import:\n");
353607428bdfSVictor Latushkin 				dump_nvlist(cfg, 8);
353707428bdfSVictor Latushkin 			}
3538c8ee1847SVictor Latushkin 			if (nvlist_add_nvlist(cfg,
3539468c413aSTim Haley 			    ZPOOL_REWIND_POLICY, policy) != 0) {
3540468c413aSTim Haley 				fatal("can't open '%s': %s",
3541468c413aSTim Haley 				    target, strerror(ENOMEM));
3542468c413aSTim Haley 			}
35434b964adaSGeorge Wilson 			if ((error = spa_import(name, cfg, NULL,
35444b964adaSGeorge Wilson 			    ZFS_IMPORT_MISSING_LOG)) != 0) {
35454b964adaSGeorge Wilson 				error = spa_import(name, cfg, NULL,
35464b964adaSGeorge Wilson 				    ZFS_IMPORT_VERBATIM);
35474b964adaSGeorge Wilson 			}
3548de6628f0Sck 		}
3549c5904d13Seschrock 	}
3550c5904d13Seschrock 
3551c5904d13Seschrock 	if (error == 0) {
355207428bdfSVictor Latushkin 		if (strpbrk(target, "/@") == NULL || dump_opt['R']) {
355380eb36f2SGeorge Wilson 			error = spa_open_rewind(target, &spa, FTAG, policy,
355480eb36f2SGeorge Wilson 			    NULL);
35558f18d1faSGeorge Wilson 			if (error) {
35568f18d1faSGeorge Wilson 				/*
35578f18d1faSGeorge Wilson 				 * If we're missing the log device then
35588f18d1faSGeorge Wilson 				 * try opening the pool after clearing the
35598f18d1faSGeorge Wilson 				 * log state.
35608f18d1faSGeorge Wilson 				 */
35618f18d1faSGeorge Wilson 				mutex_enter(&spa_namespace_lock);
35623ad6c7f9SVictor Latushkin 				if ((spa = spa_lookup(target)) != NULL &&
35638f18d1faSGeorge Wilson 				    spa->spa_log_state == SPA_LOG_MISSING) {
35648f18d1faSGeorge Wilson 					spa->spa_log_state = SPA_LOG_CLEAR;
35658f18d1faSGeorge Wilson 					error = 0;
35668f18d1faSGeorge Wilson 				}
35678f18d1faSGeorge Wilson 				mutex_exit(&spa_namespace_lock);
35688f18d1faSGeorge Wilson 
356980eb36f2SGeorge Wilson 				if (!error) {
357080eb36f2SGeorge Wilson 					error = spa_open_rewind(target, &spa,
357180eb36f2SGeorge Wilson 					    FTAG, policy, NULL);
357280eb36f2SGeorge Wilson 				}
35738f18d1faSGeorge Wilson 			}
357407428bdfSVictor Latushkin 		} else {
357507428bdfSVictor Latushkin 			error = dmu_objset_own(target, DMU_OST_ANY,
357607428bdfSVictor Latushkin 			    B_TRUE, FTAG, &os);
3577c5904d13Seschrock 		}
3578fa9e4066Sahrens 	}
357980eb36f2SGeorge Wilson 	nvlist_free(policy);
358080eb36f2SGeorge Wilson 
3581fa9e4066Sahrens 	if (error)
35823ad6c7f9SVictor Latushkin 		fatal("can't open '%s': %s", target, strerror(error));
3583fa9e4066Sahrens 
3584fa9e4066Sahrens 	argv++;
358507428bdfSVictor Latushkin 	argc--;
358607428bdfSVictor Latushkin 	if (!dump_opt['R']) {
358707428bdfSVictor Latushkin 		if (argc > 0) {
358807428bdfSVictor Latushkin 			zopt_objects = argc;
358907428bdfSVictor Latushkin 			zopt_object = calloc(zopt_objects, sizeof (uint64_t));
359007428bdfSVictor Latushkin 			for (i = 0; i < zopt_objects; i++) {
359107428bdfSVictor Latushkin 				errno = 0;
359207428bdfSVictor Latushkin 				zopt_object[i] = strtoull(argv[i], NULL, 0);
359307428bdfSVictor Latushkin 				if (zopt_object[i] == 0 && errno != 0)
359487219db7SVictor Latushkin 					fatal("bad number %s: %s",
359507428bdfSVictor Latushkin 					    argv[i], strerror(errno));
359607428bdfSVictor Latushkin 			}
3597fa9e4066Sahrens 		}
3598e690fb27SChristopher Siden 		if (os != NULL) {
3599e690fb27SChristopher Siden 			dump_dir(os);
3600e690fb27SChristopher Siden 		} else if (zopt_objects > 0 && !dump_opt['m']) {
3601e690fb27SChristopher Siden 			dump_dir(spa->spa_meta_objset);
3602e690fb27SChristopher Siden 		} else {
3603e690fb27SChristopher Siden 			dump_zpool(spa);
3604e690fb27SChristopher Siden 		}
3605fa9e4066Sahrens 	} else {
360607428bdfSVictor Latushkin 		flagbits['b'] = ZDB_FLAG_PRINT_BLKPTR;
360707428bdfSVictor Latushkin 		flagbits['c'] = ZDB_FLAG_CHECKSUM;
360807428bdfSVictor Latushkin 		flagbits['d'] = ZDB_FLAG_DECOMPRESS;
360907428bdfSVictor Latushkin 		flagbits['e'] = ZDB_FLAG_BSWAP;
361007428bdfSVictor Latushkin 		flagbits['g'] = ZDB_FLAG_GBH;
361107428bdfSVictor Latushkin 		flagbits['i'] = ZDB_FLAG_INDIRECT;
361207428bdfSVictor Latushkin 		flagbits['p'] = ZDB_FLAG_PHYS;
361307428bdfSVictor Latushkin 		flagbits['r'] = ZDB_FLAG_RAW;
361407428bdfSVictor Latushkin 
361507428bdfSVictor Latushkin 		for (i = 0; i < argc; i++)
361607428bdfSVictor Latushkin 			zdb_read_block(argv[i], spa);
3617fa9e4066Sahrens 	}
3618fa9e4066Sahrens 
361907428bdfSVictor Latushkin 	(os != NULL) ? dmu_objset_disown(os, FTAG) : spa_close(spa, FTAG);
362007428bdfSVictor Latushkin 
3621e0d35c44Smarks 	fuid_table_destroy();
36220a586ceaSMark Shellenbaum 	sa_loaded = B_FALSE;
3623e0d35c44Smarks 
3624de6628f0Sck 	libzfs_fini(g_zfs);
3625fa9e4066Sahrens 	kernel_fini();
3626fa9e4066Sahrens 
3627fa9e4066Sahrens 	return (0);
3628fa9e4066Sahrens }
3629