xref: /illumos-gate/usr/src/cmd/zdb/zdb.c (revision 4dd77f9e)
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.
2429bdd2f9SPavel Zakharov  * Copyright (c) 2011, 2016 by Delphix. All rights reserved.
25c3d26abcSMatthew Ahrens  * Copyright (c) 2014 Integros [integros.com]
26fa9e4066Sahrens  */
27fa9e4066Sahrens 
28fa9e4066Sahrens #include <stdio.h>
29490d05b9SMatthew Ahrens #include <unistd.h>
30004388ebScasper #include <stdio_ext.h>
31fa9e4066Sahrens #include <stdlib.h>
3244cd46caSbillm #include <ctype.h>
33fa9e4066Sahrens #include <sys/zfs_context.h>
34fa9e4066Sahrens #include <sys/spa.h>
35fa9e4066Sahrens #include <sys/spa_impl.h>
36fa9e4066Sahrens #include <sys/dmu.h>
37fa9e4066Sahrens #include <sys/zap.h>
38fa9e4066Sahrens #include <sys/fs/zfs.h>
39fa9e4066Sahrens #include <sys/zfs_znode.h>
400a586ceaSMark Shellenbaum #include <sys/zfs_sa.h>
410a586ceaSMark Shellenbaum #include <sys/sa.h>
420a586ceaSMark Shellenbaum #include <sys/sa_impl.h>
43fa9e4066Sahrens #include <sys/vdev.h>
44fa9e4066Sahrens #include <sys/vdev_impl.h>
45fa9e4066Sahrens #include <sys/metaslab_impl.h>
46fa9e4066Sahrens #include <sys/dmu_objset.h>
47fa9e4066Sahrens #include <sys/dsl_dir.h>
48fa9e4066Sahrens #include <sys/dsl_dataset.h>
49fa9e4066Sahrens #include <sys/dsl_pool.h>
50fa9e4066Sahrens #include <sys/dbuf.h>
51fa9e4066Sahrens #include <sys/zil.h>
52fa9e4066Sahrens #include <sys/zil_impl.h>
53fa9e4066Sahrens #include <sys/stat.h>
54fa9e4066Sahrens #include <sys/resource.h>
55fa9e4066Sahrens #include <sys/dmu_traverse.h>
56fa9e4066Sahrens #include <sys/zio_checksum.h>
57fa9e4066Sahrens #include <sys/zio_compress.h>
58e0d35c44Smarks #include <sys/zfs_fuid.h>
5988b7b0f2SMatthew Ahrens #include <sys/arc.h>
60b24ab676SJeff Bonwick #include <sys/ddt.h>
61ad135b5dSChristopher Siden #include <sys/zfeature.h>
624445fffbSMatthew Ahrens #include <zfs_comutil.h>
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
78*4dd77f9eSMatthew Ahrens extern int reference_tracking_enable;
797fd05ac4SMatthew Ahrens extern boolean_t zfs_recover;
8006be9802SMatthew Ahrens extern uint64_t zfs_arc_max, zfs_arc_meta_limit;
81f7950bf1SMatthew Ahrens extern int zfs_vdev_async_read_max_active;
82feef89cfSVictor Latushkin #else
83*4dd77f9eSMatthew Ahrens int reference_tracking_enable;
847fd05ac4SMatthew Ahrens boolean_t zfs_recover;
8506be9802SMatthew Ahrens uint64_t zfs_arc_max, zfs_arc_meta_limit;
86f7950bf1SMatthew Ahrens int zfs_vdev_async_read_max_active;
87feef89cfSVictor Latushkin #endif
88feef89cfSVictor Latushkin 
89fa9e4066Sahrens const char cmdname[] = "zdb";
90fa9e4066Sahrens uint8_t dump_opt[256];
91fa9e4066Sahrens 
92fa9e4066Sahrens typedef void object_viewer_t(objset_t *, uint64_t, void *data, size_t size);
93fa9e4066Sahrens 
94fa9e4066Sahrens extern void dump_intent_log(zilog_t *);
95fa9e4066Sahrens uint64_t *zopt_object = NULL;
96fa9e4066Sahrens int zopt_objects = 0;
97de6628f0Sck libzfs_handle_t *g_zfs;
9806be9802SMatthew Ahrens uint64_t max_inflight = 1000;
99fa9e4066Sahrens 
100732885fcSMatthew Ahrens static void snprintf_blkptr_compact(char *, size_t, const blkptr_t *);
101732885fcSMatthew Ahrens 
102fa9e4066Sahrens /*
103fa9e4066Sahrens  * These libumem hooks provide a reasonable set of defaults for the allocator's
104fa9e4066Sahrens  * debugging facilities.
105fa9e4066Sahrens  */
106fa9e4066Sahrens const char *
107fa9e4066Sahrens _umem_debug_init()
108fa9e4066Sahrens {
109fa9e4066Sahrens 	return ("default,verbose"); /* $UMEM_DEBUG setting */
110fa9e4066Sahrens }
111fa9e4066Sahrens 
112fa9e4066Sahrens const char *
113fa9e4066Sahrens _umem_logging_init(void)
114fa9e4066Sahrens {
115fa9e4066Sahrens 	return ("fail,contents"); /* $UMEM_LOGGING setting */
116fa9e4066Sahrens }
117fa9e4066Sahrens 
118fa9e4066Sahrens static void
119fa9e4066Sahrens usage(void)
120fa9e4066Sahrens {
121fa9e4066Sahrens 	(void) fprintf(stderr,
12229bdd2f9SPavel Zakharov 	    "Usage: %s [-CumMdibcsDvhLXFPAG] [-t txg] [-e [-p path...]] "
1230e60744cSPavel Zakharov 	    "[-U config] [-I inflight I/Os] [-x dumpdir] [-o var=value] "
1240e60744cSPavel Zakharov 	    "poolname [object...]\n"
12531d7e8faSGeorge Wilson 	    "       %s [-divPA] [-e -p path...] [-U config] dataset "
12631d7e8faSGeorge Wilson 	    "[object...]\n"
1272e4c9986SGeorge Wilson 	    "       %s -mM [-LXFPA] [-t txg] [-e [-p path...]] [-U config] "
12890e894e2SRichard Lowe 	    "poolname [vdev [metaslab...]]\n"
12990e894e2SRichard Lowe 	    "       %s -R [-A] [-e [-p path...]] poolname "
13090e894e2SRichard Lowe 	    "vdev:offset:size[:flags]\n"
13131d7e8faSGeorge Wilson 	    "       %s -S [-PA] [-e [-p path...]] [-U config] poolname\n"
13290e894e2SRichard Lowe 	    "       %s -l [-uA] device\n"
13390e894e2SRichard Lowe 	    "       %s -C [-A] [-U config]\n\n",
134bbfd46c4SJeff Bonwick 	    cmdname, cmdname, cmdname, cmdname, cmdname, cmdname, cmdname);
1353ad6c7f9SVictor Latushkin 
1363ad6c7f9SVictor Latushkin 	(void) fprintf(stderr, "    Dataset name must include at least one "
1373ad6c7f9SVictor Latushkin 	    "separator character '/' or '@'\n");
1383ad6c7f9SVictor Latushkin 	(void) fprintf(stderr, "    If dataset name is specified, only that "
1393ad6c7f9SVictor Latushkin 	    "dataset is dumped\n");
1403ad6c7f9SVictor Latushkin 	(void) fprintf(stderr, "    If object numbers are specified, only "
1413ad6c7f9SVictor Latushkin 	    "those objects are dumped\n\n");
1423ad6c7f9SVictor Latushkin 	(void) fprintf(stderr, "    Options to control amount of output:\n");
1433ad6c7f9SVictor Latushkin 	(void) fprintf(stderr, "        -u uberblock\n");
1443ad6c7f9SVictor Latushkin 	(void) fprintf(stderr, "        -d dataset(s)\n");
1453ad6c7f9SVictor Latushkin 	(void) fprintf(stderr, "        -i intent logs\n");
14607428bdfSVictor Latushkin 	(void) fprintf(stderr, "        -C config (or cachefile if alone)\n");
1478f18d1faSGeorge Wilson 	(void) fprintf(stderr, "        -h pool history\n");
1483ad6c7f9SVictor Latushkin 	(void) fprintf(stderr, "        -b block statistics\n");
1493ad6c7f9SVictor Latushkin 	(void) fprintf(stderr, "        -m metaslabs\n");
1502e4c9986SGeorge Wilson 	(void) fprintf(stderr, "        -M metaslab groups\n");
1513ad6c7f9SVictor Latushkin 	(void) fprintf(stderr, "        -c checksum all metadata (twice for "
1526365109dSVictor Latushkin 	    "all data) blocks\n");
1533ad6c7f9SVictor Latushkin 	(void) fprintf(stderr, "        -s report stats on zdb's I/O\n");
154f0ba89beSJeff Bonwick 	(void) fprintf(stderr, "        -D dedup statistics\n");
155b24ab676SJeff Bonwick 	(void) fprintf(stderr, "        -S simulate dedup to measure effect\n");
1563ad6c7f9SVictor Latushkin 	(void) fprintf(stderr, "        -v verbose (applies to all others)\n");
157fa9e4066Sahrens 	(void) fprintf(stderr, "        -l dump label contents\n");
15882a0a985SVictor Latushkin 	(void) fprintf(stderr, "        -L disable leak tracking (do not "
15982a0a985SVictor Latushkin 	    "load spacemaps)\n");
160d41e7643Sek 	(void) fprintf(stderr, "        -R read and display block from a "
1613ad6c7f9SVictor Latushkin 	    "device\n\n");
1623ad6c7f9SVictor Latushkin 	(void) fprintf(stderr, "    Below options are intended for use "
163df15e419SMatthew Ahrens 	    "with other options:\n");
164feef89cfSVictor Latushkin 	(void) fprintf(stderr, "        -A ignore assertions (-A), enable "
165feef89cfSVictor Latushkin 	    "panic recovery (-AA) or both (-AAA)\n");
166c8ee1847SVictor Latushkin 	(void) fprintf(stderr, "        -F attempt automatic rewind within "
167c8ee1847SVictor Latushkin 	    "safe range of transaction groups\n");
1683ad6c7f9SVictor Latushkin 	(void) fprintf(stderr, "        -U <cachefile_path> -- use alternate "
1693ad6c7f9SVictor Latushkin 	    "cachefile\n");
170c8ee1847SVictor Latushkin 	(void) fprintf(stderr, "        -X attempt extreme rewind (does not "
171c8ee1847SVictor Latushkin 	    "work with dataset)\n");
1723ad6c7f9SVictor Latushkin 	(void) fprintf(stderr, "        -e pool is exported/destroyed/"
1733ad6c7f9SVictor Latushkin 	    "has altroot/not in a cachefile\n");
1743ad6c7f9SVictor Latushkin 	(void) fprintf(stderr, "        -p <path> -- use one or more with "
1753ad6c7f9SVictor Latushkin 	    "-e to specify path to vdev dir\n");
176df15e419SMatthew Ahrens 	(void) fprintf(stderr, "        -x <dumpdir> -- "
177df15e419SMatthew Ahrens 	    "dump all read blocks into specified directory\n");
17890e894e2SRichard Lowe 	(void) fprintf(stderr, "        -P print numbers in parseable form\n");
1793ad6c7f9SVictor Latushkin 	(void) fprintf(stderr, "        -t <txg> -- highest txg to use when "
1802e551927SVictor Latushkin 	    "searching for uberblocks\n");
1812e4c9986SGeorge Wilson 	(void) fprintf(stderr, "        -I <number of inflight I/Os> -- "
182df15e419SMatthew Ahrens 	    "specify the maximum number of "
183df15e419SMatthew Ahrens 	    "checksumming I/Os [default is 200]\n");
18429bdd2f9SPavel Zakharov 	(void) fprintf(stderr, "        -G dump zfs_dbgmsg buffer before "
18529bdd2f9SPavel Zakharov 	    "exiting\n");
1860e60744cSPavel Zakharov 	(void) fprintf(stderr, "        -o <variable>=<value> set global "
1870e60744cSPavel Zakharov 	    "variable to an unsigned 32-bit integer value\n");
188fa9e4066Sahrens 	(void) fprintf(stderr, "Specify an option more than once (e.g. -bb) "
189fa9e4066Sahrens 	    "to make only that option verbose\n");
190fa9e4066Sahrens 	(void) fprintf(stderr, "Default is to dump everything non-verbosely\n");
191fa9e4066Sahrens 	exit(1);
192fa9e4066Sahrens }
193fa9e4066Sahrens 
19429bdd2f9SPavel Zakharov static void
19529bdd2f9SPavel Zakharov dump_debug_buffer()
19629bdd2f9SPavel Zakharov {
19729bdd2f9SPavel Zakharov 	if (dump_opt['G']) {
19829bdd2f9SPavel Zakharov 		(void) printf("\n");
19929bdd2f9SPavel Zakharov 		zfs_dbgmsg_print("zdb");
20029bdd2f9SPavel Zakharov 	}
20129bdd2f9SPavel Zakharov }
20229bdd2f9SPavel Zakharov 
203ccba0801SRich Morris /*
204ccba0801SRich Morris  * Called for usage errors that are discovered after a call to spa_open(),
205ccba0801SRich Morris  * dmu_bonus_hold(), or pool_match().  abort() is called for other errors.
206ccba0801SRich Morris  */
207ccba0801SRich Morris 
208fa9e4066Sahrens static void
209fa9e4066Sahrens fatal(const char *fmt, ...)
210fa9e4066Sahrens {
211fa9e4066Sahrens 	va_list ap;
212fa9e4066Sahrens 
213fa9e4066Sahrens 	va_start(ap, fmt);
214fa9e4066Sahrens 	(void) fprintf(stderr, "%s: ", cmdname);
215fa9e4066Sahrens 	(void) vfprintf(stderr, fmt, ap);
216fa9e4066Sahrens 	va_end(ap);
217fa9e4066Sahrens 	(void) fprintf(stderr, "\n");
218fa9e4066Sahrens 
21929bdd2f9SPavel Zakharov 	dump_debug_buffer();
22029bdd2f9SPavel Zakharov 
221ccba0801SRich Morris 	exit(1);
222fa9e4066Sahrens }
223fa9e4066Sahrens 
224fa9e4066Sahrens /* ARGSUSED */
225fa9e4066Sahrens static void
226fa9e4066Sahrens dump_packed_nvlist(objset_t *os, uint64_t object, void *data, size_t size)
227fa9e4066Sahrens {
228fa9e4066Sahrens 	nvlist_t *nv;
229fa9e4066Sahrens 	size_t nvsize = *(uint64_t *)data;
230fa9e4066Sahrens 	char *packed = umem_alloc(nvsize, UMEM_NOFAIL);
231fa9e4066Sahrens 
2327bfdf011SNeil Perrin 	VERIFY(0 == dmu_read(os, object, 0, nvsize, packed, DMU_READ_PREFETCH));
233fa9e4066Sahrens 
234fa9e4066Sahrens 	VERIFY(nvlist_unpack(packed, nvsize, &nv, 0) == 0);
235fa9e4066Sahrens 
236fa9e4066Sahrens 	umem_free(packed, nvsize);
237fa9e4066Sahrens 
238fa9e4066Sahrens 	dump_nvlist(nv, 8);
239fa9e4066Sahrens 
240fa9e4066Sahrens 	nvlist_free(nv);
241fa9e4066Sahrens }
242fa9e4066Sahrens 
2434445fffbSMatthew Ahrens /* ARGSUSED */
2444445fffbSMatthew Ahrens static void
2454445fffbSMatthew Ahrens dump_history_offsets(objset_t *os, uint64_t object, void *data, size_t size)
2464445fffbSMatthew Ahrens {
2474445fffbSMatthew Ahrens 	spa_history_phys_t *shp = data;
2484445fffbSMatthew Ahrens 
2494445fffbSMatthew Ahrens 	if (shp == NULL)
2504445fffbSMatthew Ahrens 		return;
2514445fffbSMatthew Ahrens 
2524445fffbSMatthew Ahrens 	(void) printf("\t\tpool_create_len = %llu\n",
2534445fffbSMatthew Ahrens 	    (u_longlong_t)shp->sh_pool_create_len);
2544445fffbSMatthew Ahrens 	(void) printf("\t\tphys_max_off = %llu\n",
2554445fffbSMatthew Ahrens 	    (u_longlong_t)shp->sh_phys_max_off);
2564445fffbSMatthew Ahrens 	(void) printf("\t\tbof = %llu\n",
2574445fffbSMatthew Ahrens 	    (u_longlong_t)shp->sh_bof);
2584445fffbSMatthew Ahrens 	(void) printf("\t\teof = %llu\n",
2594445fffbSMatthew Ahrens 	    (u_longlong_t)shp->sh_eof);
2604445fffbSMatthew Ahrens 	(void) printf("\t\trecords_lost = %llu\n",
2614445fffbSMatthew Ahrens 	    (u_longlong_t)shp->sh_records_lost);
2624445fffbSMatthew Ahrens }
2634445fffbSMatthew Ahrens 
2643f9d6ad7SLin Ling static void
2653f9d6ad7SLin Ling zdb_nicenum(uint64_t num, char *buf)
2663f9d6ad7SLin Ling {
2673f9d6ad7SLin Ling 	if (dump_opt['P'])
2683f9d6ad7SLin Ling 		(void) sprintf(buf, "%llu", (longlong_t)num);
2693f9d6ad7SLin Ling 	else
2703f9d6ad7SLin Ling 		nicenum(num, buf);
2713f9d6ad7SLin Ling }
2723f9d6ad7SLin Ling 
273490d05b9SMatthew Ahrens const char histo_stars[] = "****************************************";
274490d05b9SMatthew Ahrens const int histo_width = sizeof (histo_stars) - 1;
275fa9e4066Sahrens 
276fa9e4066Sahrens static void
2770713e232SGeorge Wilson dump_histogram(const uint64_t *histo, int size, int offset)
278fa9e4066Sahrens {
279fa9e4066Sahrens 	int i;
280490d05b9SMatthew Ahrens 	int minidx = size - 1;
281fa9e4066Sahrens 	int maxidx = 0;
282fa9e4066Sahrens 	uint64_t max = 0;
283fa9e4066Sahrens 
284490d05b9SMatthew Ahrens 	for (i = 0; i < size; i++) {
285fa9e4066Sahrens 		if (histo[i] > max)
286fa9e4066Sahrens 			max = histo[i];
287fa9e4066Sahrens 		if (histo[i] > 0 && i > maxidx)
288fa9e4066Sahrens 			maxidx = i;
289fa9e4066Sahrens 		if (histo[i] > 0 && i < minidx)
290fa9e4066Sahrens 			minidx = i;
291fa9e4066Sahrens 	}
292fa9e4066Sahrens 
293490d05b9SMatthew Ahrens 	if (max < histo_width)
294490d05b9SMatthew Ahrens 		max = histo_width;
295fa9e4066Sahrens 
296490d05b9SMatthew Ahrens 	for (i = minidx; i <= maxidx; i++) {
297490d05b9SMatthew Ahrens 		(void) printf("\t\t\t%3u: %6llu %s\n",
2980713e232SGeorge Wilson 		    i + offset, (u_longlong_t)histo[i],
299490d05b9SMatthew Ahrens 		    &histo_stars[(max - histo[i]) * histo_width / max]);
300490d05b9SMatthew Ahrens 	}
301fa9e4066Sahrens }
302fa9e4066Sahrens 
303fa9e4066Sahrens static void
304fa9e4066Sahrens dump_zap_stats(objset_t *os, uint64_t object)
305fa9e4066Sahrens {
306fa9e4066Sahrens 	int error;
307fa9e4066Sahrens 	zap_stats_t zs;
308fa9e4066Sahrens 
309fa9e4066Sahrens 	error = zap_get_stats(os, object, &zs);
310fa9e4066Sahrens 	if (error)
311fa9e4066Sahrens 		return;
312fa9e4066Sahrens 
313fa9e4066Sahrens 	if (zs.zs_ptrtbl_len == 0) {
314fa9e4066Sahrens 		ASSERT(zs.zs_num_blocks == 1);
315fa9e4066Sahrens 		(void) printf("\tmicrozap: %llu bytes, %llu entries\n",
316fa9e4066Sahrens 		    (u_longlong_t)zs.zs_blocksize,
317fa9e4066Sahrens 		    (u_longlong_t)zs.zs_num_entries);
318fa9e4066Sahrens 		return;
319fa9e4066Sahrens 	}
320fa9e4066Sahrens 
321fa9e4066Sahrens 	(void) printf("\tFat ZAP stats:\n");
3228248818dSnd 
3238248818dSnd 	(void) printf("\t\tPointer table:\n");
3248248818dSnd 	(void) printf("\t\t\t%llu elements\n",
325fa9e4066Sahrens 	    (u_longlong_t)zs.zs_ptrtbl_len);
3268248818dSnd 	(void) printf("\t\t\tzt_blk: %llu\n",
3278248818dSnd 	    (u_longlong_t)zs.zs_ptrtbl_zt_blk);
3288248818dSnd 	(void) printf("\t\t\tzt_numblks: %llu\n",
3298248818dSnd 	    (u_longlong_t)zs.zs_ptrtbl_zt_numblks);
3308248818dSnd 	(void) printf("\t\t\tzt_shift: %llu\n",
3318248818dSnd 	    (u_longlong_t)zs.zs_ptrtbl_zt_shift);
3328248818dSnd 	(void) printf("\t\t\tzt_blks_copied: %llu\n",
3338248818dSnd 	    (u_longlong_t)zs.zs_ptrtbl_blks_copied);
3348248818dSnd 	(void) printf("\t\t\tzt_nextblk: %llu\n",
3358248818dSnd 	    (u_longlong_t)zs.zs_ptrtbl_nextblk);
3368248818dSnd 
337fa9e4066Sahrens 	(void) printf("\t\tZAP entries: %llu\n",
338fa9e4066Sahrens 	    (u_longlong_t)zs.zs_num_entries);
339fa9e4066Sahrens 	(void) printf("\t\tLeaf blocks: %llu\n",
340fa9e4066Sahrens 	    (u_longlong_t)zs.zs_num_leafs);
341fa9e4066Sahrens 	(void) printf("\t\tTotal blocks: %llu\n",
342fa9e4066Sahrens 	    (u_longlong_t)zs.zs_num_blocks);
3438248818dSnd 	(void) printf("\t\tzap_block_type: 0x%llx\n",
3448248818dSnd 	    (u_longlong_t)zs.zs_block_type);
3458248818dSnd 	(void) printf("\t\tzap_magic: 0x%llx\n",
3468248818dSnd 	    (u_longlong_t)zs.zs_magic);
3478248818dSnd 	(void) printf("\t\tzap_salt: 0x%llx\n",
3488248818dSnd 	    (u_longlong_t)zs.zs_salt);
349fa9e4066Sahrens 
350fa9e4066Sahrens 	(void) printf("\t\tLeafs with 2^n pointers:\n");
3510713e232SGeorge Wilson 	dump_histogram(zs.zs_leafs_with_2n_pointers, ZAP_HISTOGRAM_SIZE, 0);
352fa9e4066Sahrens 
353fa9e4066Sahrens 	(void) printf("\t\tBlocks with n*5 entries:\n");
3540713e232SGeorge Wilson 	dump_histogram(zs.zs_blocks_with_n5_entries, ZAP_HISTOGRAM_SIZE, 0);
355fa9e4066Sahrens 
356fa9e4066Sahrens 	(void) printf("\t\tBlocks n/10 full:\n");
3570713e232SGeorge Wilson 	dump_histogram(zs.zs_blocks_n_tenths_full, ZAP_HISTOGRAM_SIZE, 0);
358fa9e4066Sahrens 
359fa9e4066Sahrens 	(void) printf("\t\tEntries with n chunks:\n");
3600713e232SGeorge Wilson 	dump_histogram(zs.zs_entries_using_n_chunks, ZAP_HISTOGRAM_SIZE, 0);
361fa9e4066Sahrens 
362fa9e4066Sahrens 	(void) printf("\t\tBuckets with n entries:\n");
3630713e232SGeorge Wilson 	dump_histogram(zs.zs_buckets_with_n_entries, ZAP_HISTOGRAM_SIZE, 0);
364fa9e4066Sahrens }
365fa9e4066Sahrens 
366fa9e4066Sahrens /*ARGSUSED*/
367fa9e4066Sahrens static void
368fa9e4066Sahrens dump_none(objset_t *os, uint64_t object, void *data, size_t size)
369fa9e4066Sahrens {
370fa9e4066Sahrens }
371fa9e4066Sahrens 
3726de8f417SVictor Latushkin /*ARGSUSED*/
3736de8f417SVictor Latushkin static void
3746de8f417SVictor Latushkin dump_unknown(objset_t *os, uint64_t object, void *data, size_t size)
3756de8f417SVictor Latushkin {
3766de8f417SVictor Latushkin 	(void) printf("\tUNKNOWN OBJECT TYPE\n");
3776de8f417SVictor Latushkin }
3786de8f417SVictor Latushkin 
379fa9e4066Sahrens /*ARGSUSED*/
380fa9e4066Sahrens void
381fa9e4066Sahrens dump_uint8(objset_t *os, uint64_t object, void *data, size_t size)
382fa9e4066Sahrens {
383fa9e4066Sahrens }
384fa9e4066Sahrens 
385fa9e4066Sahrens /*ARGSUSED*/
386fa9e4066Sahrens static void
387fa9e4066Sahrens dump_uint64(objset_t *os, uint64_t object, void *data, size_t size)
388fa9e4066Sahrens {
389fa9e4066Sahrens }
390fa9e4066Sahrens 
391fa9e4066Sahrens /*ARGSUSED*/
392fa9e4066Sahrens static void
393fa9e4066Sahrens dump_zap(objset_t *os, uint64_t object, void *data, size_t size)
394fa9e4066Sahrens {
395fa9e4066Sahrens 	zap_cursor_t zc;
396fa9e4066Sahrens 	zap_attribute_t attr;
397fa9e4066Sahrens 	void *prop;
398fa9e4066Sahrens 	int i;
399fa9e4066Sahrens 
400fa9e4066Sahrens 	dump_zap_stats(os, object);
401fa9e4066Sahrens 	(void) printf("\n");
402fa9e4066Sahrens 
403fa9e4066Sahrens 	for (zap_cursor_init(&zc, os, object);
404fa9e4066Sahrens 	    zap_cursor_retrieve(&zc, &attr) == 0;
405fa9e4066Sahrens 	    zap_cursor_advance(&zc)) {
406fa9e4066Sahrens 		(void) printf("\t\t%s = ", attr.za_name);
407fa9e4066Sahrens 		if (attr.za_num_integers == 0) {
408fa9e4066Sahrens 			(void) printf("\n");
409fa9e4066Sahrens 			continue;
410fa9e4066Sahrens 		}
411fa9e4066Sahrens 		prop = umem_zalloc(attr.za_num_integers *
412fa9e4066Sahrens 		    attr.za_integer_length, UMEM_NOFAIL);
413fa9e4066Sahrens 		(void) zap_lookup(os, object, attr.za_name,
414fa9e4066Sahrens 		    attr.za_integer_length, attr.za_num_integers, prop);
415fa9e4066Sahrens 		if (attr.za_integer_length == 1) {
416fa9e4066Sahrens 			(void) printf("%s", (char *)prop);
417fa9e4066Sahrens 		} else {
418fa9e4066Sahrens 			for (i = 0; i < attr.za_num_integers; i++) {
419fa9e4066Sahrens 				switch (attr.za_integer_length) {
420fa9e4066Sahrens 				case 2:
421fa9e4066Sahrens 					(void) printf("%u ",
422fa9e4066Sahrens 					    ((uint16_t *)prop)[i]);
423fa9e4066Sahrens 					break;
424fa9e4066Sahrens 				case 4:
425fa9e4066Sahrens 					(void) printf("%u ",
426fa9e4066Sahrens 					    ((uint32_t *)prop)[i]);
427fa9e4066Sahrens 					break;
428fa9e4066Sahrens 				case 8:
429fa9e4066Sahrens 					(void) printf("%lld ",
430fa9e4066Sahrens 					    (u_longlong_t)((int64_t *)prop)[i]);
431fa9e4066Sahrens 					break;
432fa9e4066Sahrens 				}
433fa9e4066Sahrens 			}
434fa9e4066Sahrens 		}
435fa9e4066Sahrens 		(void) printf("\n");
436fa9e4066Sahrens 		umem_free(prop, attr.za_num_integers * attr.za_integer_length);
437fa9e4066Sahrens 	}
43887e5029aSahrens 	zap_cursor_fini(&zc);
439fa9e4066Sahrens }
440fa9e4066Sahrens 
441732885fcSMatthew Ahrens static void
442732885fcSMatthew Ahrens dump_bpobj(objset_t *os, uint64_t object, void *data, size_t size)
443732885fcSMatthew Ahrens {
444732885fcSMatthew Ahrens 	bpobj_phys_t *bpop = data;
445732885fcSMatthew Ahrens 	char bytes[32], comp[32], uncomp[32];
446732885fcSMatthew Ahrens 
447732885fcSMatthew Ahrens 	if (bpop == NULL)
448732885fcSMatthew Ahrens 		return;
449732885fcSMatthew Ahrens 
450732885fcSMatthew Ahrens 	zdb_nicenum(bpop->bpo_bytes, bytes);
451732885fcSMatthew Ahrens 	zdb_nicenum(bpop->bpo_comp, comp);
452732885fcSMatthew Ahrens 	zdb_nicenum(bpop->bpo_uncomp, uncomp);
453732885fcSMatthew Ahrens 
454732885fcSMatthew Ahrens 	(void) printf("\t\tnum_blkptrs = %llu\n",
455732885fcSMatthew Ahrens 	    (u_longlong_t)bpop->bpo_num_blkptrs);
456732885fcSMatthew Ahrens 	(void) printf("\t\tbytes = %s\n", bytes);
457732885fcSMatthew Ahrens 	if (size >= BPOBJ_SIZE_V1) {
458732885fcSMatthew Ahrens 		(void) printf("\t\tcomp = %s\n", comp);
459732885fcSMatthew Ahrens 		(void) printf("\t\tuncomp = %s\n", uncomp);
460732885fcSMatthew Ahrens 	}
461732885fcSMatthew Ahrens 	if (size >= sizeof (*bpop)) {
462732885fcSMatthew Ahrens 		(void) printf("\t\tsubobjs = %llu\n",
463732885fcSMatthew Ahrens 		    (u_longlong_t)bpop->bpo_subobjs);
464732885fcSMatthew Ahrens 		(void) printf("\t\tnum_subobjs = %llu\n",
465732885fcSMatthew Ahrens 		    (u_longlong_t)bpop->bpo_num_subobjs);
466732885fcSMatthew Ahrens 	}
467732885fcSMatthew Ahrens 
468732885fcSMatthew Ahrens 	if (dump_opt['d'] < 5)
469732885fcSMatthew Ahrens 		return;
470732885fcSMatthew Ahrens 
471732885fcSMatthew Ahrens 	for (uint64_t i = 0; i < bpop->bpo_num_blkptrs; i++) {
472732885fcSMatthew Ahrens 		char blkbuf[BP_SPRINTF_LEN];
473732885fcSMatthew Ahrens 		blkptr_t bp;
474732885fcSMatthew Ahrens 
475732885fcSMatthew Ahrens 		int err = dmu_read(os, object,
476732885fcSMatthew Ahrens 		    i * sizeof (bp), sizeof (bp), &bp, 0);
477732885fcSMatthew Ahrens 		if (err != 0) {
478732885fcSMatthew Ahrens 			(void) printf("got error %u from dmu_read\n", err);
479732885fcSMatthew Ahrens 			break;
480732885fcSMatthew Ahrens 		}
481732885fcSMatthew Ahrens 		snprintf_blkptr_compact(blkbuf, sizeof (blkbuf), &bp);
482732885fcSMatthew Ahrens 		(void) printf("\t%s\n", blkbuf);
483732885fcSMatthew Ahrens 	}
484732885fcSMatthew Ahrens }
485732885fcSMatthew Ahrens 
486732885fcSMatthew Ahrens /* ARGSUSED */
487732885fcSMatthew Ahrens static void
488732885fcSMatthew Ahrens dump_bpobj_subobjs(objset_t *os, uint64_t object, void *data, size_t size)
489732885fcSMatthew Ahrens {
490732885fcSMatthew Ahrens 	dmu_object_info_t doi;
491732885fcSMatthew Ahrens 
492732885fcSMatthew Ahrens 	VERIFY0(dmu_object_info(os, object, &doi));
493732885fcSMatthew Ahrens 	uint64_t *subobjs = kmem_alloc(doi.doi_max_offset, KM_SLEEP);
494732885fcSMatthew Ahrens 
495732885fcSMatthew Ahrens 	int err = dmu_read(os, object, 0, doi.doi_max_offset, subobjs, 0);
496732885fcSMatthew Ahrens 	if (err != 0) {
497732885fcSMatthew Ahrens 		(void) printf("got error %u from dmu_read\n", err);
498732885fcSMatthew Ahrens 		kmem_free(subobjs, doi.doi_max_offset);
499732885fcSMatthew Ahrens 		return;
500732885fcSMatthew Ahrens 	}
501732885fcSMatthew Ahrens 
502732885fcSMatthew Ahrens 	int64_t last_nonzero = -1;
503732885fcSMatthew Ahrens 	for (uint64_t i = 0; i < doi.doi_max_offset / 8; i++) {
504732885fcSMatthew Ahrens 		if (subobjs[i] != 0)
505732885fcSMatthew Ahrens 			last_nonzero = i;
506732885fcSMatthew Ahrens 	}
507732885fcSMatthew Ahrens 
508732885fcSMatthew Ahrens 	for (int64_t i = 0; i <= last_nonzero; i++) {
509732885fcSMatthew Ahrens 		(void) printf("\t%llu\n", (longlong_t)subobjs[i]);
510732885fcSMatthew Ahrens 	}
511732885fcSMatthew Ahrens 	kmem_free(subobjs, doi.doi_max_offset);
512732885fcSMatthew Ahrens }
513732885fcSMatthew Ahrens 
514486ae710SMatthew Ahrens /*ARGSUSED*/
515486ae710SMatthew Ahrens static void
516486ae710SMatthew Ahrens dump_ddt_zap(objset_t *os, uint64_t object, void *data, size_t size)
517486ae710SMatthew Ahrens {
518486ae710SMatthew Ahrens 	dump_zap_stats(os, object);
519486ae710SMatthew Ahrens 	/* contents are printed elsewhere, properly decoded */
520486ae710SMatthew Ahrens }
521486ae710SMatthew Ahrens 
5220a586ceaSMark Shellenbaum /*ARGSUSED*/
5230a586ceaSMark Shellenbaum static void
5240a586ceaSMark Shellenbaum dump_sa_attrs(objset_t *os, uint64_t object, void *data, size_t size)
5250a586ceaSMark Shellenbaum {
5260a586ceaSMark Shellenbaum 	zap_cursor_t zc;
5270a586ceaSMark Shellenbaum 	zap_attribute_t attr;
5280a586ceaSMark Shellenbaum 
5290a586ceaSMark Shellenbaum 	dump_zap_stats(os, object);
5300a586ceaSMark Shellenbaum 	(void) printf("\n");
5310a586ceaSMark Shellenbaum 
5320a586ceaSMark Shellenbaum 	for (zap_cursor_init(&zc, os, object);
5330a586ceaSMark Shellenbaum 	    zap_cursor_retrieve(&zc, &attr) == 0;
5340a586ceaSMark Shellenbaum 	    zap_cursor_advance(&zc)) {
5350a586ceaSMark Shellenbaum 		(void) printf("\t\t%s = ", attr.za_name);
5360a586ceaSMark Shellenbaum 		if (attr.za_num_integers == 0) {
5370a586ceaSMark Shellenbaum 			(void) printf("\n");
5380a586ceaSMark Shellenbaum 			continue;
5390a586ceaSMark Shellenbaum 		}
5400a586ceaSMark Shellenbaum 		(void) printf(" %llx : [%d:%d:%d]\n",
5410a586ceaSMark Shellenbaum 		    (u_longlong_t)attr.za_first_integer,
5420a586ceaSMark Shellenbaum 		    (int)ATTR_LENGTH(attr.za_first_integer),
5430a586ceaSMark Shellenbaum 		    (int)ATTR_BSWAP(attr.za_first_integer),
5440a586ceaSMark Shellenbaum 		    (int)ATTR_NUM(attr.za_first_integer));
5450a586ceaSMark Shellenbaum 	}
5460a586ceaSMark Shellenbaum 	zap_cursor_fini(&zc);
5470a586ceaSMark Shellenbaum }
5480a586ceaSMark Shellenbaum 
5490a586ceaSMark Shellenbaum /*ARGSUSED*/
5500a586ceaSMark Shellenbaum static void
5510a586ceaSMark Shellenbaum dump_sa_layouts(objset_t *os, uint64_t object, void *data, size_t size)
5520a586ceaSMark Shellenbaum {
5530a586ceaSMark Shellenbaum 	zap_cursor_t zc;
5540a586ceaSMark Shellenbaum 	zap_attribute_t attr;
5550a586ceaSMark Shellenbaum 	uint16_t *layout_attrs;
5560a586ceaSMark Shellenbaum 	int i;
5570a586ceaSMark Shellenbaum 
5580a586ceaSMark Shellenbaum 	dump_zap_stats(os, object);
5590a586ceaSMark Shellenbaum 	(void) printf("\n");
5600a586ceaSMark Shellenbaum 
5610a586ceaSMark Shellenbaum 	for (zap_cursor_init(&zc, os, object);
5620a586ceaSMark Shellenbaum 	    zap_cursor_retrieve(&zc, &attr) == 0;
5630a586ceaSMark Shellenbaum 	    zap_cursor_advance(&zc)) {
5640a586ceaSMark Shellenbaum 		(void) printf("\t\t%s = [", attr.za_name);
5650a586ceaSMark Shellenbaum 		if (attr.za_num_integers == 0) {
5660a586ceaSMark Shellenbaum 			(void) printf("\n");
5670a586ceaSMark Shellenbaum 			continue;
5680a586ceaSMark Shellenbaum 		}
5690a586ceaSMark Shellenbaum 
5700a586ceaSMark Shellenbaum 		VERIFY(attr.za_integer_length == 2);
5710a586ceaSMark Shellenbaum 		layout_attrs = umem_zalloc(attr.za_num_integers *
5720a586ceaSMark Shellenbaum 		    attr.za_integer_length, UMEM_NOFAIL);
5730a586ceaSMark Shellenbaum 
5740a586ceaSMark Shellenbaum 		VERIFY(zap_lookup(os, object, attr.za_name,
5750a586ceaSMark Shellenbaum 		    attr.za_integer_length,
5760a586ceaSMark Shellenbaum 		    attr.za_num_integers, layout_attrs) == 0);
5770a586ceaSMark Shellenbaum 
5780a586ceaSMark Shellenbaum 		for (i = 0; i != attr.za_num_integers; i++)
5790a586ceaSMark Shellenbaum 			(void) printf(" %d ", (int)layout_attrs[i]);
5800a586ceaSMark Shellenbaum 		(void) printf("]\n");
5810a586ceaSMark Shellenbaum 		umem_free(layout_attrs,
5820a586ceaSMark Shellenbaum 		    attr.za_num_integers * attr.za_integer_length);
5830a586ceaSMark Shellenbaum 	}
5840a586ceaSMark Shellenbaum 	zap_cursor_fini(&zc);
5850a586ceaSMark Shellenbaum }
5860a586ceaSMark Shellenbaum 
587e7437265Sahrens /*ARGSUSED*/
588e7437265Sahrens static void
589e7437265Sahrens dump_zpldir(objset_t *os, uint64_t object, void *data, size_t size)
590e7437265Sahrens {
591e7437265Sahrens 	zap_cursor_t zc;
592e7437265Sahrens 	zap_attribute_t attr;
593e7437265Sahrens 	const char *typenames[] = {
594e7437265Sahrens 		/* 0 */ "not specified",
595e7437265Sahrens 		/* 1 */ "FIFO",
596e7437265Sahrens 		/* 2 */ "Character Device",
597e7437265Sahrens 		/* 3 */ "3 (invalid)",
598e7437265Sahrens 		/* 4 */ "Directory",
599e7437265Sahrens 		/* 5 */ "5 (invalid)",
600e7437265Sahrens 		/* 6 */ "Block Device",
601e7437265Sahrens 		/* 7 */ "7 (invalid)",
602e7437265Sahrens 		/* 8 */ "Regular File",
603e7437265Sahrens 		/* 9 */ "9 (invalid)",
604e7437265Sahrens 		/* 10 */ "Symbolic Link",
605e7437265Sahrens 		/* 11 */ "11 (invalid)",
606e7437265Sahrens 		/* 12 */ "Socket",
607e7437265Sahrens 		/* 13 */ "Door",
608e7437265Sahrens 		/* 14 */ "Event Port",
609e7437265Sahrens 		/* 15 */ "15 (invalid)",
610e7437265Sahrens 	};
611e7437265Sahrens 
612e7437265Sahrens 	dump_zap_stats(os, object);
613e7437265Sahrens 	(void) printf("\n");
614e7437265Sahrens 
615e7437265Sahrens 	for (zap_cursor_init(&zc, os, object);
616e7437265Sahrens 	    zap_cursor_retrieve(&zc, &attr) == 0;
617e7437265Sahrens 	    zap_cursor_advance(&zc)) {
618e7437265Sahrens 		(void) printf("\t\t%s = %lld (type: %s)\n",
619e7437265Sahrens 		    attr.za_name, ZFS_DIRENT_OBJ(attr.za_first_integer),
620e7437265Sahrens 		    typenames[ZFS_DIRENT_TYPE(attr.za_first_integer)]);
621e7437265Sahrens 	}
622e7437265Sahrens 	zap_cursor_fini(&zc);
623e7437265Sahrens }
624e7437265Sahrens 
6250713e232SGeorge Wilson int
6260713e232SGeorge Wilson get_dtl_refcount(vdev_t *vd)
6270713e232SGeorge Wilson {
6280713e232SGeorge Wilson 	int refcount = 0;
6290713e232SGeorge Wilson 
6300713e232SGeorge Wilson 	if (vd->vdev_ops->vdev_op_leaf) {
6310713e232SGeorge Wilson 		space_map_t *sm = vd->vdev_dtl_sm;
6320713e232SGeorge Wilson 
6330713e232SGeorge Wilson 		if (sm != NULL &&
6340713e232SGeorge Wilson 		    sm->sm_dbuf->db_size == sizeof (space_map_phys_t))
6350713e232SGeorge Wilson 			return (1);
6360713e232SGeorge Wilson 		return (0);
6370713e232SGeorge Wilson 	}
6380713e232SGeorge Wilson 
6390713e232SGeorge Wilson 	for (int c = 0; c < vd->vdev_children; c++)
6400713e232SGeorge Wilson 		refcount += get_dtl_refcount(vd->vdev_child[c]);
6410713e232SGeorge Wilson 	return (refcount);
6420713e232SGeorge Wilson }
6430713e232SGeorge Wilson 
6440713e232SGeorge Wilson int
6450713e232SGeorge Wilson get_metaslab_refcount(vdev_t *vd)
6460713e232SGeorge Wilson {
6470713e232SGeorge Wilson 	int refcount = 0;
6480713e232SGeorge Wilson 
6492e4c9986SGeorge Wilson 	if (vd->vdev_top == vd && !vd->vdev_removing) {
6500713e232SGeorge Wilson 		for (int m = 0; m < vd->vdev_ms_count; m++) {
6510713e232SGeorge Wilson 			space_map_t *sm = vd->vdev_ms[m]->ms_sm;
6520713e232SGeorge Wilson 
6530713e232SGeorge Wilson 			if (sm != NULL &&
6540713e232SGeorge Wilson 			    sm->sm_dbuf->db_size == sizeof (space_map_phys_t))
6550713e232SGeorge Wilson 				refcount++;
6560713e232SGeorge Wilson 		}
6570713e232SGeorge Wilson 	}
6580713e232SGeorge Wilson 	for (int c = 0; c < vd->vdev_children; c++)
6590713e232SGeorge Wilson 		refcount += get_metaslab_refcount(vd->vdev_child[c]);
6600713e232SGeorge Wilson 
6610713e232SGeorge Wilson 	return (refcount);
6620713e232SGeorge Wilson }
6630713e232SGeorge Wilson 
6640713e232SGeorge Wilson static int
6650713e232SGeorge Wilson verify_spacemap_refcounts(spa_t *spa)
6660713e232SGeorge Wilson {
6672acef22dSMatthew Ahrens 	uint64_t expected_refcount = 0;
6682acef22dSMatthew Ahrens 	uint64_t actual_refcount;
6690713e232SGeorge Wilson 
6702acef22dSMatthew Ahrens 	(void) feature_get_refcount(spa,
6712acef22dSMatthew Ahrens 	    &spa_feature_table[SPA_FEATURE_SPACEMAP_HISTOGRAM],
6722acef22dSMatthew Ahrens 	    &expected_refcount);
6730713e232SGeorge Wilson 	actual_refcount = get_dtl_refcount(spa->spa_root_vdev);
6740713e232SGeorge Wilson 	actual_refcount += get_metaslab_refcount(spa->spa_root_vdev);
6750713e232SGeorge Wilson 
6760713e232SGeorge Wilson 	if (expected_refcount != actual_refcount) {
6772acef22dSMatthew Ahrens 		(void) printf("space map refcount mismatch: expected %lld != "
6782acef22dSMatthew Ahrens 		    "actual %lld\n",
6792acef22dSMatthew Ahrens 		    (longlong_t)expected_refcount,
6802acef22dSMatthew Ahrens 		    (longlong_t)actual_refcount);
6810713e232SGeorge Wilson 		return (2);
6820713e232SGeorge Wilson 	}
6830713e232SGeorge Wilson 	return (0);
6840713e232SGeorge Wilson }
6850713e232SGeorge Wilson 
686fa9e4066Sahrens static void
6870713e232SGeorge Wilson dump_spacemap(objset_t *os, space_map_t *sm)
688fa9e4066Sahrens {
689fa9e4066Sahrens 	uint64_t alloc, offset, entry;
6908053a263Sck 	char *ddata[] = { "ALLOC", "FREE", "CONDENSE", "INVALID",
6918053a263Sck 			    "INVALID", "INVALID", "INVALID", "INVALID" };
692fa9e4066Sahrens 
6930713e232SGeorge Wilson 	if (sm == NULL)
694fa9e4066Sahrens 		return;
695fa9e4066Sahrens 
696fa9e4066Sahrens 	/*
697fa9e4066Sahrens 	 * Print out the freelist entries in both encoded and decoded form.
698fa9e4066Sahrens 	 */
699fa9e4066Sahrens 	alloc = 0;
7000713e232SGeorge Wilson 	for (offset = 0; offset < space_map_length(sm);
7010713e232SGeorge Wilson 	    offset += sizeof (entry)) {
7020713e232SGeorge Wilson 		uint8_t mapshift = sm->sm_shift;
7030713e232SGeorge Wilson 
7040713e232SGeorge Wilson 		VERIFY0(dmu_read(os, space_map_object(sm), offset,
7057bfdf011SNeil Perrin 		    sizeof (entry), &entry, DMU_READ_PREFETCH));
706fa9e4066Sahrens 		if (SM_DEBUG_DECODE(entry)) {
7070713e232SGeorge Wilson 
70887219db7SVictor Latushkin 			(void) printf("\t    [%6llu] %s: txg %llu, pass %llu\n",
709fa9e4066Sahrens 			    (u_longlong_t)(offset / sizeof (entry)),
710fa9e4066Sahrens 			    ddata[SM_DEBUG_ACTION_DECODE(entry)],
7115ad82045Snd 			    (u_longlong_t)SM_DEBUG_TXG_DECODE(entry),
7125ad82045Snd 			    (u_longlong_t)SM_DEBUG_SYNCPASS_DECODE(entry));
713fa9e4066Sahrens 		} else {
71487219db7SVictor Latushkin 			(void) printf("\t    [%6llu]    %c  range:"
71587219db7SVictor Latushkin 			    " %010llx-%010llx  size: %06llx\n",
716fa9e4066Sahrens 			    (u_longlong_t)(offset / sizeof (entry)),
717fa9e4066Sahrens 			    SM_TYPE_DECODE(entry) == SM_ALLOC ? 'A' : 'F',
7185ad82045Snd 			    (u_longlong_t)((SM_OFFSET_DECODE(entry) <<
7190713e232SGeorge Wilson 			    mapshift) + sm->sm_start),
7205ad82045Snd 			    (u_longlong_t)((SM_OFFSET_DECODE(entry) <<
7210713e232SGeorge Wilson 			    mapshift) + sm->sm_start +
7220713e232SGeorge Wilson 			    (SM_RUN_DECODE(entry) << mapshift)),
7235ad82045Snd 			    (u_longlong_t)(SM_RUN_DECODE(entry) << mapshift));
724fa9e4066Sahrens 			if (SM_TYPE_DECODE(entry) == SM_ALLOC)
725fa9e4066Sahrens 				alloc += SM_RUN_DECODE(entry) << mapshift;
726fa9e4066Sahrens 			else
727fa9e4066Sahrens 				alloc -= SM_RUN_DECODE(entry) << mapshift;
728fa9e4066Sahrens 		}
729fa9e4066Sahrens 	}
7300713e232SGeorge Wilson 	if (alloc != space_map_allocated(sm)) {
731fa9e4066Sahrens 		(void) printf("space_map_object alloc (%llu) INCONSISTENT "
732fa9e4066Sahrens 		    "with space map summary (%llu)\n",
7330713e232SGeorge Wilson 		    (u_longlong_t)space_map_allocated(sm), (u_longlong_t)alloc);
734fa9e4066Sahrens 	}
735fa9e4066Sahrens }
736fa9e4066Sahrens 
737d6e555bdSGeorge Wilson static void
738d6e555bdSGeorge Wilson dump_metaslab_stats(metaslab_t *msp)
739d6e555bdSGeorge Wilson {
7403f9d6ad7SLin Ling 	char maxbuf[32];
7410713e232SGeorge Wilson 	range_tree_t *rt = msp->ms_tree;
7420713e232SGeorge Wilson 	avl_tree_t *t = &msp->ms_size_tree;
7430713e232SGeorge Wilson 	int free_pct = range_tree_space(rt) * 100 / msp->ms_size;
744d6e555bdSGeorge Wilson 
7450713e232SGeorge Wilson 	zdb_nicenum(metaslab_block_maxsize(msp), maxbuf);
746d6e555bdSGeorge Wilson 
74787219db7SVictor Latushkin 	(void) printf("\t %25s %10lu   %7s  %6s   %4s %4d%%\n",
748d6e555bdSGeorge Wilson 	    "segments", avl_numnodes(t), "maxsize", maxbuf,
749d6e555bdSGeorge Wilson 	    "freepct", free_pct);
7500713e232SGeorge Wilson 	(void) printf("\tIn-memory histogram:\n");
7510713e232SGeorge Wilson 	dump_histogram(rt->rt_histogram, RANGE_TREE_HISTOGRAM_SIZE, 0);
752d6e555bdSGeorge Wilson }
753d6e555bdSGeorge Wilson 
754fa9e4066Sahrens static void
755fa9e4066Sahrens dump_metaslab(metaslab_t *msp)
756fa9e4066Sahrens {
757fa9e4066Sahrens 	vdev_t *vd = msp->ms_group->mg_vd;
758fa9e4066Sahrens 	spa_t *spa = vd->vdev_spa;
7590713e232SGeorge Wilson 	space_map_t *sm = msp->ms_sm;
7603f9d6ad7SLin Ling 	char freebuf[32];
761fa9e4066Sahrens 
7620713e232SGeorge Wilson 	zdb_nicenum(msp->ms_size - space_map_allocated(sm), freebuf);
763fa9e4066Sahrens 
764fa9e4066Sahrens 	(void) printf(
76587219db7SVictor Latushkin 	    "\tmetaslab %6llu   offset %12llx   spacemap %6llu   free    %5s\n",
7660713e232SGeorge Wilson 	    (u_longlong_t)msp->ms_id, (u_longlong_t)msp->ms_start,
7670713e232SGeorge Wilson 	    (u_longlong_t)space_map_object(sm), freebuf);
768fa9e4066Sahrens 
7690713e232SGeorge Wilson 	if (dump_opt['m'] > 2 && !dump_opt['L']) {
770d6e555bdSGeorge Wilson 		mutex_enter(&msp->ms_lock);
7710713e232SGeorge Wilson 		metaslab_load_wait(msp);
7720713e232SGeorge Wilson 		if (!msp->ms_loaded) {
7730713e232SGeorge Wilson 			VERIFY0(metaslab_load(msp));
7740713e232SGeorge Wilson 			range_tree_stat_verify(msp->ms_tree);
7750713e232SGeorge Wilson 		}
776bc3975b5SVictor Latushkin 		dump_metaslab_stats(msp);
7770713e232SGeorge Wilson 		metaslab_unload(msp);
778d6e555bdSGeorge Wilson 		mutex_exit(&msp->ms_lock);
779d6e555bdSGeorge Wilson 	}
780d6e555bdSGeorge Wilson 
7810713e232SGeorge Wilson 	if (dump_opt['m'] > 1 && sm != NULL &&
7822acef22dSMatthew Ahrens 	    spa_feature_is_active(spa, SPA_FEATURE_SPACEMAP_HISTOGRAM)) {
7830713e232SGeorge Wilson 		/*
7840713e232SGeorge Wilson 		 * The space map histogram represents free space in chunks
7850713e232SGeorge Wilson 		 * of sm_shift (i.e. bucket 0 refers to 2^sm_shift).
7860713e232SGeorge Wilson 		 */
7872e4c9986SGeorge Wilson 		(void) printf("\tOn-disk histogram:\t\tfragmentation %llu\n",
7882e4c9986SGeorge Wilson 		    (u_longlong_t)msp->ms_fragmentation);
7890713e232SGeorge Wilson 		dump_histogram(sm->sm_phys->smp_histogram,
7902e4c9986SGeorge Wilson 		    SPACE_MAP_HISTOGRAM_SIZE, sm->sm_shift);
7910713e232SGeorge Wilson 	}
7920713e232SGeorge Wilson 
7930713e232SGeorge Wilson 	if (dump_opt['d'] > 5 || dump_opt['m'] > 3) {
7940713e232SGeorge Wilson 		ASSERT(msp->ms_size == (1ULL << vd->vdev_ms_shift));
795d6e555bdSGeorge Wilson 
796d6e555bdSGeorge Wilson 		mutex_enter(&msp->ms_lock);
7970713e232SGeorge Wilson 		dump_spacemap(spa->spa_meta_objset, msp->ms_sm);
798d6e555bdSGeorge Wilson 		mutex_exit(&msp->ms_lock);
799d6e555bdSGeorge Wilson 	}
80087219db7SVictor Latushkin }
801fa9e4066Sahrens 
80287219db7SVictor Latushkin static void
80387219db7SVictor Latushkin print_vdev_metaslab_header(vdev_t *vd)
80487219db7SVictor Latushkin {
80587219db7SVictor Latushkin 	(void) printf("\tvdev %10llu\n\t%-10s%5llu   %-19s   %-15s   %-10s\n",
80687219db7SVictor Latushkin 	    (u_longlong_t)vd->vdev_id,
80787219db7SVictor Latushkin 	    "metaslabs", (u_longlong_t)vd->vdev_ms_count,
80887219db7SVictor Latushkin 	    "offset", "spacemap", "free");
80987219db7SVictor Latushkin 	(void) printf("\t%15s   %19s   %15s   %10s\n",
81087219db7SVictor Latushkin 	    "---------------", "-------------------",
81187219db7SVictor Latushkin 	    "---------------", "-------------");
812fa9e4066Sahrens }
813fa9e4066Sahrens 
8142e4c9986SGeorge Wilson static void
8152e4c9986SGeorge Wilson dump_metaslab_groups(spa_t *spa)
8162e4c9986SGeorge Wilson {
8172e4c9986SGeorge Wilson 	vdev_t *rvd = spa->spa_root_vdev;
8182e4c9986SGeorge Wilson 	metaslab_class_t *mc = spa_normal_class(spa);
8192e4c9986SGeorge Wilson 	uint64_t fragmentation;
8202e4c9986SGeorge Wilson 
8212e4c9986SGeorge Wilson 	metaslab_class_histogram_verify(mc);
8222e4c9986SGeorge Wilson 
8232e4c9986SGeorge Wilson 	for (int c = 0; c < rvd->vdev_children; c++) {
8242e4c9986SGeorge Wilson 		vdev_t *tvd = rvd->vdev_child[c];
8252e4c9986SGeorge Wilson 		metaslab_group_t *mg = tvd->vdev_mg;
8262e4c9986SGeorge Wilson 
8272e4c9986SGeorge Wilson 		if (mg->mg_class != mc)
8282e4c9986SGeorge Wilson 			continue;
8292e4c9986SGeorge Wilson 
8302e4c9986SGeorge Wilson 		metaslab_group_histogram_verify(mg);
8312e4c9986SGeorge Wilson 		mg->mg_fragmentation = metaslab_group_fragmentation(mg);
8322e4c9986SGeorge Wilson 
8332e4c9986SGeorge Wilson 		(void) printf("\tvdev %10llu\t\tmetaslabs%5llu\t\t"
8342e4c9986SGeorge Wilson 		    "fragmentation",
8352e4c9986SGeorge Wilson 		    (u_longlong_t)tvd->vdev_id,
8362e4c9986SGeorge Wilson 		    (u_longlong_t)tvd->vdev_ms_count);
8372e4c9986SGeorge Wilson 		if (mg->mg_fragmentation == ZFS_FRAG_INVALID) {
8382e4c9986SGeorge Wilson 			(void) printf("%3s\n", "-");
8392e4c9986SGeorge Wilson 		} else {
8402e4c9986SGeorge Wilson 			(void) printf("%3llu%%\n",
8412e4c9986SGeorge Wilson 			    (u_longlong_t)mg->mg_fragmentation);
8422e4c9986SGeorge Wilson 		}
8432e4c9986SGeorge Wilson 		dump_histogram(mg->mg_histogram, RANGE_TREE_HISTOGRAM_SIZE, 0);
8442e4c9986SGeorge Wilson 	}
8452e4c9986SGeorge Wilson 
8462e4c9986SGeorge Wilson 	(void) printf("\tpool %s\tfragmentation", spa_name(spa));
8472e4c9986SGeorge Wilson 	fragmentation = metaslab_class_fragmentation(mc);
8482e4c9986SGeorge Wilson 	if (fragmentation == ZFS_FRAG_INVALID)
8492e4c9986SGeorge Wilson 		(void) printf("\t%3s\n", "-");
8502e4c9986SGeorge Wilson 	else
8512e4c9986SGeorge Wilson 		(void) printf("\t%3llu%%\n", (u_longlong_t)fragmentation);
8522e4c9986SGeorge Wilson 	dump_histogram(mc->mc_histogram, RANGE_TREE_HISTOGRAM_SIZE, 0);
8532e4c9986SGeorge Wilson }
8542e4c9986SGeorge Wilson 
855fa9e4066Sahrens static void
856fa9e4066Sahrens dump_metaslabs(spa_t *spa)
857fa9e4066Sahrens {
85887219db7SVictor Latushkin 	vdev_t *vd, *rvd = spa->spa_root_vdev;
85987219db7SVictor Latushkin 	uint64_t m, c = 0, children = rvd->vdev_children;
860fa9e4066Sahrens 
861fa9e4066Sahrens 	(void) printf("\nMetaslabs:\n");
862fa9e4066Sahrens 
86387219db7SVictor Latushkin 	if (!dump_opt['d'] && zopt_objects > 0) {
86487219db7SVictor Latushkin 		c = zopt_object[0];
86587219db7SVictor Latushkin 
86687219db7SVictor Latushkin 		if (c >= children)
86787219db7SVictor Latushkin 			(void) fatal("bad vdev id: %llu", (u_longlong_t)c);
868fa9e4066Sahrens 
86987219db7SVictor Latushkin 		if (zopt_objects > 1) {
87087219db7SVictor Latushkin 			vd = rvd->vdev_child[c];
87187219db7SVictor Latushkin 			print_vdev_metaslab_header(vd);
87287219db7SVictor Latushkin 
87387219db7SVictor Latushkin 			for (m = 1; m < zopt_objects; m++) {
87487219db7SVictor Latushkin 				if (zopt_object[m] < vd->vdev_ms_count)
87587219db7SVictor Latushkin 					dump_metaslab(
87687219db7SVictor Latushkin 					    vd->vdev_ms[zopt_object[m]]);
87787219db7SVictor Latushkin 				else
87887219db7SVictor Latushkin 					(void) fprintf(stderr, "bad metaslab "
87987219db7SVictor Latushkin 					    "number %llu\n",
88087219db7SVictor Latushkin 					    (u_longlong_t)zopt_object[m]);
88187219db7SVictor Latushkin 			}
88287219db7SVictor Latushkin 			(void) printf("\n");
88387219db7SVictor Latushkin 			return;
88487219db7SVictor Latushkin 		}
88587219db7SVictor Latushkin 		children = c + 1;
88687219db7SVictor Latushkin 	}
88787219db7SVictor Latushkin 	for (; c < children; c++) {
88887219db7SVictor Latushkin 		vd = rvd->vdev_child[c];
88987219db7SVictor Latushkin 		print_vdev_metaslab_header(vd);
890fa9e4066Sahrens 
891fa9e4066Sahrens 		for (m = 0; m < vd->vdev_ms_count; m++)
892fa9e4066Sahrens 			dump_metaslab(vd->vdev_ms[m]);
893fa9e4066Sahrens 		(void) printf("\n");
894fa9e4066Sahrens 	}
895fa9e4066Sahrens }
896fa9e4066Sahrens 
897b24ab676SJeff Bonwick static void
898b24ab676SJeff Bonwick dump_dde(const ddt_t *ddt, const ddt_entry_t *dde, uint64_t index)
899b24ab676SJeff Bonwick {
900b24ab676SJeff Bonwick 	const ddt_phys_t *ddp = dde->dde_phys;
901b24ab676SJeff Bonwick 	const ddt_key_t *ddk = &dde->dde_key;
902b24ab676SJeff Bonwick 	char *types[4] = { "ditto", "single", "double", "triple" };
903b24ab676SJeff Bonwick 	char blkbuf[BP_SPRINTF_LEN];
904b24ab676SJeff Bonwick 	blkptr_t blk;
905b24ab676SJeff Bonwick 
906b24ab676SJeff Bonwick 	for (int p = 0; p < DDT_PHYS_TYPES; p++, ddp++) {
907b24ab676SJeff Bonwick 		if (ddp->ddp_phys_birth == 0)
908b24ab676SJeff Bonwick 			continue;
909bbfd46c4SJeff Bonwick 		ddt_bp_create(ddt->ddt_checksum, ddk, ddp, &blk);
91043466aaeSMax Grossman 		snprintf_blkptr(blkbuf, sizeof (blkbuf), &blk);
911b24ab676SJeff Bonwick 		(void) printf("index %llx refcnt %llu %s %s\n",
912b24ab676SJeff Bonwick 		    (u_longlong_t)index, (u_longlong_t)ddp->ddp_refcnt,
913b24ab676SJeff Bonwick 		    types[p], blkbuf);
914b24ab676SJeff Bonwick 	}
915b24ab676SJeff Bonwick }
916b24ab676SJeff Bonwick 
917b24ab676SJeff Bonwick static void
918b24ab676SJeff Bonwick dump_dedup_ratio(const ddt_stat_t *dds)
919b24ab676SJeff Bonwick {
920b24ab676SJeff Bonwick 	double rL, rP, rD, D, dedup, compress, copies;
921b24ab676SJeff Bonwick 
922b24ab676SJeff Bonwick 	if (dds->dds_blocks == 0)
923b24ab676SJeff Bonwick 		return;
924b24ab676SJeff Bonwick 
925b24ab676SJeff Bonwick 	rL = (double)dds->dds_ref_lsize;
926b24ab676SJeff Bonwick 	rP = (double)dds->dds_ref_psize;
927b24ab676SJeff Bonwick 	rD = (double)dds->dds_ref_dsize;
928b24ab676SJeff Bonwick 	D = (double)dds->dds_dsize;
929b24ab676SJeff Bonwick 
930b24ab676SJeff Bonwick 	dedup = rD / D;
931b24ab676SJeff Bonwick 	compress = rL / rP;
932b24ab676SJeff Bonwick 	copies = rD / rP;
933b24ab676SJeff Bonwick 
934b24ab676SJeff Bonwick 	(void) printf("dedup = %.2f, compress = %.2f, copies = %.2f, "
935b24ab676SJeff Bonwick 	    "dedup * compress / copies = %.2f\n\n",
936b24ab676SJeff Bonwick 	    dedup, compress, copies, dedup * compress / copies);
937b24ab676SJeff Bonwick }
938b24ab676SJeff Bonwick 
939b24ab676SJeff Bonwick static void
940b24ab676SJeff Bonwick dump_ddt(ddt_t *ddt, enum ddt_type type, enum ddt_class class)
941b24ab676SJeff Bonwick {
942b24ab676SJeff Bonwick 	char name[DDT_NAMELEN];
943b24ab676SJeff Bonwick 	ddt_entry_t dde;
944b24ab676SJeff Bonwick 	uint64_t walk = 0;
945b24ab676SJeff Bonwick 	dmu_object_info_t doi;
946b24ab676SJeff Bonwick 	uint64_t count, dspace, mspace;
947b24ab676SJeff Bonwick 	int error;
948b24ab676SJeff Bonwick 
949b24ab676SJeff Bonwick 	error = ddt_object_info(ddt, type, class, &doi);
950b24ab676SJeff Bonwick 
951b24ab676SJeff Bonwick 	if (error == ENOENT)
952b24ab676SJeff Bonwick 		return;
953b24ab676SJeff Bonwick 	ASSERT(error == 0);
954b24ab676SJeff Bonwick 
9557448a079SGeorge Wilson 	if ((count = ddt_object_count(ddt, type, class)) == 0)
9567448a079SGeorge Wilson 		return;
9577448a079SGeorge Wilson 
958b24ab676SJeff Bonwick 	dspace = doi.doi_physical_blocks_512 << 9;
959b24ab676SJeff Bonwick 	mspace = doi.doi_fill_count * doi.doi_data_block_size;
960b24ab676SJeff Bonwick 
961b24ab676SJeff Bonwick 	ddt_object_name(ddt, type, class, name);
962b24ab676SJeff Bonwick 
963b24ab676SJeff Bonwick 	(void) printf("%s: %llu entries, size %llu on disk, %llu in core\n",
964b24ab676SJeff Bonwick 	    name,
965b24ab676SJeff Bonwick 	    (u_longlong_t)count,
966b24ab676SJeff Bonwick 	    (u_longlong_t)(dspace / count),
967b24ab676SJeff Bonwick 	    (u_longlong_t)(mspace / count));
968b24ab676SJeff Bonwick 
969b24ab676SJeff Bonwick 	if (dump_opt['D'] < 3)
970b24ab676SJeff Bonwick 		return;
971b24ab676SJeff Bonwick 
9729eb19f4dSGeorge Wilson 	zpool_dump_ddt(NULL, &ddt->ddt_histogram[type][class]);
973b24ab676SJeff Bonwick 
974b24ab676SJeff Bonwick 	if (dump_opt['D'] < 4)
975b24ab676SJeff Bonwick 		return;
976b24ab676SJeff Bonwick 
977b24ab676SJeff Bonwick 	if (dump_opt['D'] < 5 && class == DDT_CLASS_UNIQUE)
978b24ab676SJeff Bonwick 		return;
979b24ab676SJeff Bonwick 
980b24ab676SJeff Bonwick 	(void) printf("%s contents:\n\n", name);
981b24ab676SJeff Bonwick 
982bbfd46c4SJeff Bonwick 	while ((error = ddt_object_walk(ddt, type, class, &walk, &dde)) == 0)
983b24ab676SJeff Bonwick 		dump_dde(ddt, &dde, walk);
984b24ab676SJeff Bonwick 
985b24ab676SJeff Bonwick 	ASSERT(error == ENOENT);
986b24ab676SJeff Bonwick 
987b24ab676SJeff Bonwick 	(void) printf("\n");
988b24ab676SJeff Bonwick }
989b24ab676SJeff Bonwick 
990b24ab676SJeff Bonwick static void
991b24ab676SJeff Bonwick dump_all_ddts(spa_t *spa)
992b24ab676SJeff Bonwick {
993b24ab676SJeff Bonwick 	ddt_histogram_t ddh_total = { 0 };
994b24ab676SJeff Bonwick 	ddt_stat_t dds_total = { 0 };
995b24ab676SJeff Bonwick 
996b24ab676SJeff Bonwick 	for (enum zio_checksum c = 0; c < ZIO_CHECKSUM_FUNCTIONS; c++) {
997b24ab676SJeff Bonwick 		ddt_t *ddt = spa->spa_ddt[c];
998b24ab676SJeff Bonwick 		for (enum ddt_type type = 0; type < DDT_TYPES; type++) {
999b24ab676SJeff Bonwick 			for (enum ddt_class class = 0; class < DDT_CLASSES;
1000b24ab676SJeff Bonwick 			    class++) {
1001b24ab676SJeff Bonwick 				dump_ddt(ddt, type, class);
1002b24ab676SJeff Bonwick 			}
1003b24ab676SJeff Bonwick 		}
1004b24ab676SJeff Bonwick 	}
1005b24ab676SJeff Bonwick 
10069eb19f4dSGeorge Wilson 	ddt_get_dedup_stats(spa, &dds_total);
1007b24ab676SJeff Bonwick 
1008b24ab676SJeff Bonwick 	if (dds_total.dds_blocks == 0) {
1009b24ab676SJeff Bonwick 		(void) printf("All DDTs are empty\n");
1010b24ab676SJeff Bonwick 		return;
1011b24ab676SJeff Bonwick 	}
1012b24ab676SJeff Bonwick 
1013b24ab676SJeff Bonwick 	(void) printf("\n");
1014b24ab676SJeff Bonwick 
1015b24ab676SJeff Bonwick 	if (dump_opt['D'] > 1) {
1016b24ab676SJeff Bonwick 		(void) printf("DDT histogram (aggregated over all DDTs):\n");
10179eb19f4dSGeorge Wilson 		ddt_get_dedup_histogram(spa, &ddh_total);
10189eb19f4dSGeorge Wilson 		zpool_dump_ddt(&dds_total, &ddh_total);
1019b24ab676SJeff Bonwick 	}
1020b24ab676SJeff Bonwick 
1021b24ab676SJeff Bonwick 	dump_dedup_ratio(&dds_total);
1022b24ab676SJeff Bonwick }
1023b24ab676SJeff Bonwick 
10248ad4d6ddSJeff Bonwick static void
10250713e232SGeorge Wilson dump_dtl_seg(void *arg, uint64_t start, uint64_t size)
10268ad4d6ddSJeff Bonwick {
10270713e232SGeorge Wilson 	char *prefix = arg;
10288ad4d6ddSJeff Bonwick 
10298ad4d6ddSJeff Bonwick 	(void) printf("%s [%llu,%llu) length %llu\n",
10308ad4d6ddSJeff Bonwick 	    prefix,
10318ad4d6ddSJeff Bonwick 	    (u_longlong_t)start,
10328ad4d6ddSJeff Bonwick 	    (u_longlong_t)(start + size),
10338ad4d6ddSJeff Bonwick 	    (u_longlong_t)(size));
10348ad4d6ddSJeff Bonwick }
10358ad4d6ddSJeff Bonwick 
1036fa9e4066Sahrens static void
1037fa9e4066Sahrens dump_dtl(vdev_t *vd, int indent)
1038fa9e4066Sahrens {
10398ad4d6ddSJeff Bonwick 	spa_t *spa = vd->vdev_spa;
10408ad4d6ddSJeff Bonwick 	boolean_t required;
10418ad4d6ddSJeff Bonwick 	char *name[DTL_TYPES] = { "missing", "partial", "scrub", "outage" };
10428ad4d6ddSJeff Bonwick 	char prefix[256];
10438ad4d6ddSJeff Bonwick 
10448f18d1faSGeorge Wilson 	spa_vdev_state_enter(spa, SCL_NONE);
10458ad4d6ddSJeff Bonwick 	required = vdev_dtl_required(vd);
10468ad4d6ddSJeff Bonwick 	(void) spa_vdev_state_exit(spa, NULL, 0);
1047fa9e4066Sahrens 
1048fa9e4066Sahrens 	if (indent == 0)
1049fa9e4066Sahrens 		(void) printf("\nDirty time logs:\n\n");
1050fa9e4066Sahrens 
10518ad4d6ddSJeff Bonwick 	(void) printf("\t%*s%s [%s]\n", indent, "",
1052e14bb325SJeff Bonwick 	    vd->vdev_path ? vd->vdev_path :
10538ad4d6ddSJeff Bonwick 	    vd->vdev_parent ? vd->vdev_ops->vdev_op_type : spa_name(spa),
10548ad4d6ddSJeff Bonwick 	    required ? "DTL-required" : "DTL-expendable");
1055fa9e4066Sahrens 
10568ad4d6ddSJeff Bonwick 	for (int t = 0; t < DTL_TYPES; t++) {
10570713e232SGeorge Wilson 		range_tree_t *rt = vd->vdev_dtl[t];
10580713e232SGeorge Wilson 		if (range_tree_space(rt) == 0)
10598ad4d6ddSJeff Bonwick 			continue;
10608ad4d6ddSJeff Bonwick 		(void) snprintf(prefix, sizeof (prefix), "\t%*s%s",
10618ad4d6ddSJeff Bonwick 		    indent + 2, "", name[t]);
10620713e232SGeorge Wilson 		mutex_enter(rt->rt_lock);
10630713e232SGeorge Wilson 		range_tree_walk(rt, dump_dtl_seg, prefix);
10640713e232SGeorge Wilson 		mutex_exit(rt->rt_lock);
10658ad4d6ddSJeff Bonwick 		if (dump_opt['d'] > 5 && vd->vdev_children == 0)
10660713e232SGeorge Wilson 			dump_spacemap(spa->spa_meta_objset, vd->vdev_dtl_sm);
1067fa9e4066Sahrens 	}
1068fa9e4066Sahrens 
10698ad4d6ddSJeff Bonwick 	for (int c = 0; c < vd->vdev_children; c++)
1070fa9e4066Sahrens 		dump_dtl(vd->vdev_child[c], indent + 4);
1071fa9e4066Sahrens }
1072fa9e4066Sahrens 
10738f18d1faSGeorge Wilson static void
10748f18d1faSGeorge Wilson dump_history(spa_t *spa)
10758f18d1faSGeorge Wilson {
10768f18d1faSGeorge Wilson 	nvlist_t **events = NULL;
1077e4161df6SVictor Latushkin 	uint64_t resid, len, off = 0;
10788f18d1faSGeorge Wilson 	uint_t num = 0;
10798f18d1faSGeorge Wilson 	int error;
10808f18d1faSGeorge Wilson 	time_t tsec;
10818f18d1faSGeorge Wilson 	struct tm t;
10828f18d1faSGeorge Wilson 	char tbuf[30];
10838f18d1faSGeorge Wilson 	char internalstr[MAXPATHLEN];
10848f18d1faSGeorge Wilson 
10853f84190cSMatthew Ahrens 	char *buf = umem_alloc(SPA_MAXBLOCKSIZE, UMEM_NOFAIL);
10868f18d1faSGeorge Wilson 	do {
10873f84190cSMatthew Ahrens 		len = SPA_MAXBLOCKSIZE;
1088e4161df6SVictor Latushkin 
10898f18d1faSGeorge Wilson 		if ((error = spa_history_get(spa, &off, &len, buf)) != 0) {
10908f18d1faSGeorge Wilson 			(void) fprintf(stderr, "Unable to read history: "
10918f18d1faSGeorge Wilson 			    "error %d\n", error);
10923f84190cSMatthew Ahrens 			umem_free(buf, SPA_MAXBLOCKSIZE);
10938f18d1faSGeorge Wilson 			return;
10948f18d1faSGeorge Wilson 		}
10958f18d1faSGeorge Wilson 
10968f18d1faSGeorge Wilson 		if (zpool_history_unpack(buf, len, &resid, &events, &num) != 0)
10978f18d1faSGeorge Wilson 			break;
10988f18d1faSGeorge Wilson 
10998f18d1faSGeorge Wilson 		off -= resid;
11008f18d1faSGeorge Wilson 	} while (len != 0);
11013f84190cSMatthew Ahrens 	umem_free(buf, SPA_MAXBLOCKSIZE);
11028f18d1faSGeorge Wilson 
11038f18d1faSGeorge Wilson 	(void) printf("\nHistory:\n");
11048f18d1faSGeorge Wilson 	for (int i = 0; i < num; i++) {
11058f18d1faSGeorge Wilson 		uint64_t time, txg, ievent;
11068f18d1faSGeorge Wilson 		char *cmd, *intstr;
11074445fffbSMatthew Ahrens 		boolean_t printed = B_FALSE;
11088f18d1faSGeorge Wilson 
11098f18d1faSGeorge Wilson 		if (nvlist_lookup_uint64(events[i], ZPOOL_HIST_TIME,
11108f18d1faSGeorge Wilson 		    &time) != 0)
11114445fffbSMatthew Ahrens 			goto next;
11128f18d1faSGeorge Wilson 		if (nvlist_lookup_string(events[i], ZPOOL_HIST_CMD,
11138f18d1faSGeorge Wilson 		    &cmd) != 0) {
11148f18d1faSGeorge Wilson 			if (nvlist_lookup_uint64(events[i],
11158f18d1faSGeorge Wilson 			    ZPOOL_HIST_INT_EVENT, &ievent) != 0)
11164445fffbSMatthew Ahrens 				goto next;
11178f18d1faSGeorge Wilson 			verify(nvlist_lookup_uint64(events[i],
11188f18d1faSGeorge Wilson 			    ZPOOL_HIST_TXG, &txg) == 0);
11198f18d1faSGeorge Wilson 			verify(nvlist_lookup_string(events[i],
11208f18d1faSGeorge Wilson 			    ZPOOL_HIST_INT_STR, &intstr) == 0);
11214445fffbSMatthew Ahrens 			if (ievent >= ZFS_NUM_LEGACY_HISTORY_EVENTS)
11224445fffbSMatthew Ahrens 				goto next;
11238f18d1faSGeorge Wilson 
11248f18d1faSGeorge Wilson 			(void) snprintf(internalstr,
11258f18d1faSGeorge Wilson 			    sizeof (internalstr),
11268f18d1faSGeorge Wilson 			    "[internal %s txg:%lld] %s",
11273f9d6ad7SLin Ling 			    zfs_history_event_names[ievent], txg,
11288f18d1faSGeorge Wilson 			    intstr);
11298f18d1faSGeorge Wilson 			cmd = internalstr;
11308f18d1faSGeorge Wilson 		}
11318f18d1faSGeorge Wilson 		tsec = time;
11328f18d1faSGeorge Wilson 		(void) localtime_r(&tsec, &t);
11338f18d1faSGeorge Wilson 		(void) strftime(tbuf, sizeof (tbuf), "%F.%T", &t);
11348f18d1faSGeorge Wilson 		(void) printf("%s %s\n", tbuf, cmd);
11354445fffbSMatthew Ahrens 		printed = B_TRUE;
11364445fffbSMatthew Ahrens 
11374445fffbSMatthew Ahrens next:
11384445fffbSMatthew Ahrens 		if (dump_opt['h'] > 1) {
11394445fffbSMatthew Ahrens 			if (!printed)
11404445fffbSMatthew Ahrens 				(void) printf("unrecognized record:\n");
11414445fffbSMatthew Ahrens 			dump_nvlist(events[i], 2);
11424445fffbSMatthew Ahrens 		}
11438f18d1faSGeorge Wilson 	}
11448f18d1faSGeorge Wilson }
11458f18d1faSGeorge Wilson 
1146fa9e4066Sahrens /*ARGSUSED*/
1147fa9e4066Sahrens static void
1148fa9e4066Sahrens dump_dnode(objset_t *os, uint64_t object, void *data, size_t size)
1149fa9e4066Sahrens {
1150fa9e4066Sahrens }
1151fa9e4066Sahrens 
1152fa9e4066Sahrens static uint64_t
11537802d7bfSMatthew Ahrens blkid2offset(const dnode_phys_t *dnp, const blkptr_t *bp,
11547802d7bfSMatthew Ahrens     const zbookmark_phys_t *zb)
1155fa9e4066Sahrens {
1156b24ab676SJeff Bonwick 	if (dnp == NULL) {
1157b24ab676SJeff Bonwick 		ASSERT(zb->zb_level < 0);
1158b24ab676SJeff Bonwick 		if (zb->zb_object == 0)
1159b24ab676SJeff Bonwick 			return (zb->zb_blkid);
1160b24ab676SJeff Bonwick 		return (zb->zb_blkid * BP_GET_LSIZE(bp));
1161b24ab676SJeff Bonwick 	}
1162b24ab676SJeff Bonwick 
1163b24ab676SJeff Bonwick 	ASSERT(zb->zb_level >= 0);
1164fa9e4066Sahrens 
1165b24ab676SJeff Bonwick 	return ((zb->zb_blkid <<
1166b24ab676SJeff Bonwick 	    (zb->zb_level * (dnp->dn_indblkshift - SPA_BLKPTRSHIFT))) *
1167fa9e4066Sahrens 	    dnp->dn_datablkszsec << SPA_MINBLOCKSHIFT);
1168fa9e4066Sahrens }
1169fa9e4066Sahrens 
117044cd46caSbillm static void
117143466aaeSMax Grossman snprintf_blkptr_compact(char *blkbuf, size_t buflen, const blkptr_t *bp)
117244cd46caSbillm {
1173cde58dbcSMatthew Ahrens 	const dva_t *dva = bp->blk_dva;
1174b24ab676SJeff Bonwick 	int ndvas = dump_opt['d'] > 5 ? BP_GET_NDVAS(bp) : 1;
1175b24ab676SJeff Bonwick 
1176490d05b9SMatthew Ahrens 	if (dump_opt['b'] >= 6) {
117743466aaeSMax Grossman 		snprintf_blkptr(blkbuf, buflen, bp);
1178b24ab676SJeff Bonwick 		return;
1179b24ab676SJeff Bonwick 	}
118044cd46caSbillm 
11815d7b4d43SMatthew Ahrens 	if (BP_IS_EMBEDDED(bp)) {
11825d7b4d43SMatthew Ahrens 		(void) sprintf(blkbuf,
11835d7b4d43SMatthew Ahrens 		    "EMBEDDED et=%u %llxL/%llxP B=%llu",
11845d7b4d43SMatthew Ahrens 		    (int)BPE_GET_ETYPE(bp),
11855d7b4d43SMatthew Ahrens 		    (u_longlong_t)BPE_GET_LSIZE(bp),
11865d7b4d43SMatthew Ahrens 		    (u_longlong_t)BPE_GET_PSIZE(bp),
11875d7b4d43SMatthew Ahrens 		    (u_longlong_t)bp->blk_birth);
11885d7b4d43SMatthew Ahrens 		return;
11895d7b4d43SMatthew Ahrens 	}
119044cd46caSbillm 
11915d7b4d43SMatthew Ahrens 	blkbuf[0] = '\0';
1192b24ab676SJeff Bonwick 	for (int i = 0; i < ndvas; i++)
119343466aaeSMax Grossman 		(void) snprintf(blkbuf + strlen(blkbuf),
119443466aaeSMax Grossman 		    buflen - strlen(blkbuf), "%llu:%llx:%llx ",
119544cd46caSbillm 		    (u_longlong_t)DVA_GET_VDEV(&dva[i]),
119644cd46caSbillm 		    (u_longlong_t)DVA_GET_OFFSET(&dva[i]),
119744cd46caSbillm 		    (u_longlong_t)DVA_GET_ASIZE(&dva[i]));
119844cd46caSbillm 
119943466aaeSMax Grossman 	if (BP_IS_HOLE(bp)) {
120043466aaeSMax Grossman 		(void) snprintf(blkbuf + strlen(blkbuf),
120170163ac5SPrakash Surya 		    buflen - strlen(blkbuf),
120270163ac5SPrakash Surya 		    "%llxL B=%llu",
120370163ac5SPrakash Surya 		    (u_longlong_t)BP_GET_LSIZE(bp),
120443466aaeSMax Grossman 		    (u_longlong_t)bp->blk_birth);
120543466aaeSMax Grossman 	} else {
120643466aaeSMax Grossman 		(void) snprintf(blkbuf + strlen(blkbuf),
120743466aaeSMax Grossman 		    buflen - strlen(blkbuf),
120843466aaeSMax Grossman 		    "%llxL/%llxP F=%llu B=%llu/%llu",
120943466aaeSMax Grossman 		    (u_longlong_t)BP_GET_LSIZE(bp),
121043466aaeSMax Grossman 		    (u_longlong_t)BP_GET_PSIZE(bp),
12115d7b4d43SMatthew Ahrens 		    (u_longlong_t)BP_GET_FILL(bp),
121243466aaeSMax Grossman 		    (u_longlong_t)bp->blk_birth,
121343466aaeSMax Grossman 		    (u_longlong_t)BP_PHYSICAL_BIRTH(bp));
121443466aaeSMax Grossman 	}
121544cd46caSbillm }
121644cd46caSbillm 
121788b7b0f2SMatthew Ahrens static void
12187802d7bfSMatthew Ahrens print_indirect(blkptr_t *bp, const zbookmark_phys_t *zb,
121988b7b0f2SMatthew Ahrens     const dnode_phys_t *dnp)
1220fa9e4066Sahrens {
122188b7b0f2SMatthew Ahrens 	char blkbuf[BP_SPRINTF_LEN];
1222fa9e4066Sahrens 	int l;
1223fa9e4066Sahrens 
12245d7b4d43SMatthew Ahrens 	if (!BP_IS_EMBEDDED(bp)) {
12255d7b4d43SMatthew Ahrens 		ASSERT3U(BP_GET_TYPE(bp), ==, dnp->dn_type);
12265d7b4d43SMatthew Ahrens 		ASSERT3U(BP_GET_LEVEL(bp), ==, zb->zb_level);
12275d7b4d43SMatthew Ahrens 	}
1228fa9e4066Sahrens 
1229b24ab676SJeff Bonwick 	(void) printf("%16llx ", (u_longlong_t)blkid2offset(dnp, bp, zb));
1230fa9e4066Sahrens 
1231fa9e4066Sahrens 	ASSERT(zb->zb_level >= 0);
1232fa9e4066Sahrens 
1233fa9e4066Sahrens 	for (l = dnp->dn_nlevels - 1; l >= -1; l--) {
1234fa9e4066Sahrens 		if (l == zb->zb_level) {
123588b7b0f2SMatthew Ahrens 			(void) printf("L%llx", (u_longlong_t)zb->zb_level);
1236fa9e4066Sahrens 		} else {
123788b7b0f2SMatthew Ahrens 			(void) printf(" ");
1238fa9e4066Sahrens 		}
1239fa9e4066Sahrens 	}
1240fa9e4066Sahrens 
124143466aaeSMax Grossman 	snprintf_blkptr_compact(blkbuf, sizeof (blkbuf), bp);
124288b7b0f2SMatthew Ahrens 	(void) printf("%s\n", blkbuf);
124388b7b0f2SMatthew Ahrens }
124488b7b0f2SMatthew Ahrens 
124588b7b0f2SMatthew Ahrens static int
124688b7b0f2SMatthew Ahrens visit_indirect(spa_t *spa, const dnode_phys_t *dnp,
12477802d7bfSMatthew Ahrens     blkptr_t *bp, const zbookmark_phys_t *zb)
124888b7b0f2SMatthew Ahrens {
1249e4161df6SVictor Latushkin 	int err = 0;
125088b7b0f2SMatthew Ahrens 
125188b7b0f2SMatthew Ahrens 	if (bp->blk_birth == 0)
125288b7b0f2SMatthew Ahrens 		return (0);
125388b7b0f2SMatthew Ahrens 
125488b7b0f2SMatthew Ahrens 	print_indirect(bp, zb, dnp);
125588b7b0f2SMatthew Ahrens 
125643466aaeSMax Grossman 	if (BP_GET_LEVEL(bp) > 0 && !BP_IS_HOLE(bp)) {
12577adb730bSGeorge Wilson 		arc_flags_t flags = ARC_FLAG_WAIT;
125888b7b0f2SMatthew Ahrens 		int i;
125988b7b0f2SMatthew Ahrens 		blkptr_t *cbp;
126088b7b0f2SMatthew Ahrens 		int epb = BP_GET_LSIZE(bp) >> SPA_BLKPTRSHIFT;
126188b7b0f2SMatthew Ahrens 		arc_buf_t *buf;
126288b7b0f2SMatthew Ahrens 		uint64_t fill = 0;
126388b7b0f2SMatthew Ahrens 
12641b912ec7SGeorge Wilson 		err = arc_read(NULL, spa, bp, arc_getbuf_func, &buf,
126588b7b0f2SMatthew Ahrens 		    ZIO_PRIORITY_ASYNC_READ, ZIO_FLAG_CANFAIL, &flags, zb);
126688b7b0f2SMatthew Ahrens 		if (err)
126788b7b0f2SMatthew Ahrens 			return (err);
12683f9d6ad7SLin Ling 		ASSERT(buf->b_data);
126988b7b0f2SMatthew Ahrens 
127088b7b0f2SMatthew Ahrens 		/* recursively visit blocks below this */
127188b7b0f2SMatthew Ahrens 		cbp = buf->b_data;
127288b7b0f2SMatthew Ahrens 		for (i = 0; i < epb; i++, cbp++) {
12737802d7bfSMatthew Ahrens 			zbookmark_phys_t czb;
127488b7b0f2SMatthew Ahrens 
127588b7b0f2SMatthew Ahrens 			SET_BOOKMARK(&czb, zb->zb_objset, zb->zb_object,
127688b7b0f2SMatthew Ahrens 			    zb->zb_level - 1,
127788b7b0f2SMatthew Ahrens 			    zb->zb_blkid * epb + i);
127888b7b0f2SMatthew Ahrens 			err = visit_indirect(spa, dnp, cbp, &czb);
127988b7b0f2SMatthew Ahrens 			if (err)
128088b7b0f2SMatthew Ahrens 				break;
12815d7b4d43SMatthew Ahrens 			fill += BP_GET_FILL(cbp);
128288b7b0f2SMatthew Ahrens 		}
12838ad4d6ddSJeff Bonwick 		if (!err)
12845d7b4d43SMatthew Ahrens 			ASSERT3U(fill, ==, BP_GET_FILL(bp));
1285dcbf3bd6SGeorge Wilson 		arc_buf_destroy(buf, &buf);
1286fa9e4066Sahrens 	}
1287fa9e4066Sahrens 
128888b7b0f2SMatthew Ahrens 	return (err);
1289fa9e4066Sahrens }
1290fa9e4066Sahrens 
1291fa9e4066Sahrens /*ARGSUSED*/
1292fa9e4066Sahrens static void
129388b7b0f2SMatthew Ahrens dump_indirect(dnode_t *dn)
1294fa9e4066Sahrens {
129588b7b0f2SMatthew Ahrens 	dnode_phys_t *dnp = dn->dn_phys;
129688b7b0f2SMatthew Ahrens 	int j;
12977802d7bfSMatthew Ahrens 	zbookmark_phys_t czb;
1298fa9e4066Sahrens 
1299fa9e4066Sahrens 	(void) printf("Indirect blocks:\n");
1300fa9e4066Sahrens 
1301503ad85cSMatthew Ahrens 	SET_BOOKMARK(&czb, dmu_objset_id(dn->dn_objset),
130288b7b0f2SMatthew Ahrens 	    dn->dn_object, dnp->dn_nlevels - 1, 0);
130388b7b0f2SMatthew Ahrens 	for (j = 0; j < dnp->dn_nblkptr; j++) {
130488b7b0f2SMatthew Ahrens 		czb.zb_blkid = j;
1305503ad85cSMatthew Ahrens 		(void) visit_indirect(dmu_objset_spa(dn->dn_objset), dnp,
130688b7b0f2SMatthew Ahrens 		    &dnp->dn_blkptr[j], &czb);
130788b7b0f2SMatthew Ahrens 	}
1308fa9e4066Sahrens 
1309fa9e4066Sahrens 	(void) printf("\n");
1310fa9e4066Sahrens }
1311fa9e4066Sahrens 
1312fa9e4066Sahrens /*ARGSUSED*/
1313fa9e4066Sahrens static void
1314fa9e4066Sahrens dump_dsl_dir(objset_t *os, uint64_t object, void *data, size_t size)
1315fa9e4066Sahrens {
1316fa9e4066Sahrens 	dsl_dir_phys_t *dd = data;
1317fa9e4066Sahrens 	time_t crtime;
13183f9d6ad7SLin Ling 	char nice[32];
1319fa9e4066Sahrens 
1320fa9e4066Sahrens 	if (dd == NULL)
1321fa9e4066Sahrens 		return;
1322fa9e4066Sahrens 
1323da6c28aaSamw 	ASSERT3U(size, >=, sizeof (dsl_dir_phys_t));
1324fa9e4066Sahrens 
1325fa9e4066Sahrens 	crtime = dd->dd_creation_time;
1326fa9e4066Sahrens 	(void) printf("\t\tcreation_time = %s", ctime(&crtime));
1327fa9e4066Sahrens 	(void) printf("\t\thead_dataset_obj = %llu\n",
1328fa9e4066Sahrens 	    (u_longlong_t)dd->dd_head_dataset_obj);
1329fa9e4066Sahrens 	(void) printf("\t\tparent_dir_obj = %llu\n",
1330fa9e4066Sahrens 	    (u_longlong_t)dd->dd_parent_obj);
13313cb34c60Sahrens 	(void) printf("\t\torigin_obj = %llu\n",
13323cb34c60Sahrens 	    (u_longlong_t)dd->dd_origin_obj);
1333fa9e4066Sahrens 	(void) printf("\t\tchild_dir_zapobj = %llu\n",
1334fa9e4066Sahrens 	    (u_longlong_t)dd->dd_child_dir_zapobj);
13353f9d6ad7SLin Ling 	zdb_nicenum(dd->dd_used_bytes, nice);
133674e7dc98SMatthew Ahrens 	(void) printf("\t\tused_bytes = %s\n", nice);
13373f9d6ad7SLin Ling 	zdb_nicenum(dd->dd_compressed_bytes, nice);
133874e7dc98SMatthew Ahrens 	(void) printf("\t\tcompressed_bytes = %s\n", nice);
13393f9d6ad7SLin Ling 	zdb_nicenum(dd->dd_uncompressed_bytes, nice);
134074e7dc98SMatthew Ahrens 	(void) printf("\t\tuncompressed_bytes = %s\n", nice);
13413f9d6ad7SLin Ling 	zdb_nicenum(dd->dd_quota, nice);
134274e7dc98SMatthew Ahrens 	(void) printf("\t\tquota = %s\n", nice);
13433f9d6ad7SLin Ling 	zdb_nicenum(dd->dd_reserved, nice);
134474e7dc98SMatthew Ahrens 	(void) printf("\t\treserved = %s\n", nice);
1345fa9e4066Sahrens 	(void) printf("\t\tprops_zapobj = %llu\n",
1346fa9e4066Sahrens 	    (u_longlong_t)dd->dd_props_zapobj);
1347ecd6cf80Smarks 	(void) printf("\t\tdeleg_zapobj = %llu\n",
1348ecd6cf80Smarks 	    (u_longlong_t)dd->dd_deleg_zapobj);
134974e7dc98SMatthew Ahrens 	(void) printf("\t\tflags = %llx\n",
135074e7dc98SMatthew Ahrens 	    (u_longlong_t)dd->dd_flags);
135174e7dc98SMatthew Ahrens 
135274e7dc98SMatthew Ahrens #define	DO(which) \
13533f9d6ad7SLin Ling 	zdb_nicenum(dd->dd_used_breakdown[DD_USED_ ## which], nice); \
135474e7dc98SMatthew Ahrens 	(void) printf("\t\tused_breakdown[" #which "] = %s\n", nice)
135574e7dc98SMatthew Ahrens 	DO(HEAD);
135674e7dc98SMatthew Ahrens 	DO(SNAP);
135774e7dc98SMatthew Ahrens 	DO(CHILD);
135874e7dc98SMatthew Ahrens 	DO(CHILD_RSRV);
135974e7dc98SMatthew Ahrens 	DO(REFRSRV);
136074e7dc98SMatthew Ahrens #undef DO
1361fa9e4066Sahrens }
1362fa9e4066Sahrens 
1363fa9e4066Sahrens /*ARGSUSED*/
1364fa9e4066Sahrens static void
1365fa9e4066Sahrens dump_dsl_dataset(objset_t *os, uint64_t object, void *data, size_t size)
1366fa9e4066Sahrens {
1367fa9e4066Sahrens 	dsl_dataset_phys_t *ds = data;
1368fa9e4066Sahrens 	time_t crtime;
13693f9d6ad7SLin Ling 	char used[32], compressed[32], uncompressed[32], unique[32];
1370fbabab8fSmaybee 	char blkbuf[BP_SPRINTF_LEN];
1371fa9e4066Sahrens 
1372fa9e4066Sahrens 	if (ds == NULL)
1373fa9e4066Sahrens 		return;
1374fa9e4066Sahrens 
1375fa9e4066Sahrens 	ASSERT(size == sizeof (*ds));
1376fa9e4066Sahrens 	crtime = ds->ds_creation_time;
1377ad135b5dSChristopher Siden 	zdb_nicenum(ds->ds_referenced_bytes, used);
13783f9d6ad7SLin Ling 	zdb_nicenum(ds->ds_compressed_bytes, compressed);
13793f9d6ad7SLin Ling 	zdb_nicenum(ds->ds_uncompressed_bytes, uncompressed);
13803f9d6ad7SLin Ling 	zdb_nicenum(ds->ds_unique_bytes, unique);
138143466aaeSMax Grossman 	snprintf_blkptr(blkbuf, sizeof (blkbuf), &ds->ds_bp);
1382fa9e4066Sahrens 
1383088f3894Sahrens 	(void) printf("\t\tdir_obj = %llu\n",
1384fa9e4066Sahrens 	    (u_longlong_t)ds->ds_dir_obj);
1385fa9e4066Sahrens 	(void) printf("\t\tprev_snap_obj = %llu\n",
1386fa9e4066Sahrens 	    (u_longlong_t)ds->ds_prev_snap_obj);
1387fa9e4066Sahrens 	(void) printf("\t\tprev_snap_txg = %llu\n",
1388fa9e4066Sahrens 	    (u_longlong_t)ds->ds_prev_snap_txg);
1389fa9e4066Sahrens 	(void) printf("\t\tnext_snap_obj = %llu\n",
1390fa9e4066Sahrens 	    (u_longlong_t)ds->ds_next_snap_obj);
1391fa9e4066Sahrens 	(void) printf("\t\tsnapnames_zapobj = %llu\n",
1392fa9e4066Sahrens 	    (u_longlong_t)ds->ds_snapnames_zapobj);
1393fa9e4066Sahrens 	(void) printf("\t\tnum_children = %llu\n",
1394fa9e4066Sahrens 	    (u_longlong_t)ds->ds_num_children);
1395842727c2SChris Kirby 	(void) printf("\t\tuserrefs_obj = %llu\n",
1396842727c2SChris Kirby 	    (u_longlong_t)ds->ds_userrefs_obj);
1397fa9e4066Sahrens 	(void) printf("\t\tcreation_time = %s", ctime(&crtime));
1398fa9e4066Sahrens 	(void) printf("\t\tcreation_txg = %llu\n",
1399fa9e4066Sahrens 	    (u_longlong_t)ds->ds_creation_txg);
1400fa9e4066Sahrens 	(void) printf("\t\tdeadlist_obj = %llu\n",
1401fa9e4066Sahrens 	    (u_longlong_t)ds->ds_deadlist_obj);
1402fa9e4066Sahrens 	(void) printf("\t\tused_bytes = %s\n", used);
1403fa9e4066Sahrens 	(void) printf("\t\tcompressed_bytes = %s\n", compressed);
1404fa9e4066Sahrens 	(void) printf("\t\tuncompressed_bytes = %s\n", uncompressed);
1405fa9e4066Sahrens 	(void) printf("\t\tunique = %s\n", unique);
1406fa9e4066Sahrens 	(void) printf("\t\tfsid_guid = %llu\n",
1407fa9e4066Sahrens 	    (u_longlong_t)ds->ds_fsid_guid);
1408fa9e4066Sahrens 	(void) printf("\t\tguid = %llu\n",
1409fa9e4066Sahrens 	    (u_longlong_t)ds->ds_guid);
141099653d4eSeschrock 	(void) printf("\t\tflags = %llx\n",
141199653d4eSeschrock 	    (u_longlong_t)ds->ds_flags);
1412088f3894Sahrens 	(void) printf("\t\tnext_clones_obj = %llu\n",
1413088f3894Sahrens 	    (u_longlong_t)ds->ds_next_clones_obj);
1414bb0ade09Sahrens 	(void) printf("\t\tprops_obj = %llu\n",
1415bb0ade09Sahrens 	    (u_longlong_t)ds->ds_props_obj);
1416fa9e4066Sahrens 	(void) printf("\t\tbp = %s\n", blkbuf);
1417fa9e4066Sahrens }
1418fa9e4066Sahrens 
1419ad135b5dSChristopher Siden /* ARGSUSED */
1420ad135b5dSChristopher Siden static int
1421ad135b5dSChristopher Siden dump_bptree_cb(void *arg, const blkptr_t *bp, dmu_tx_t *tx)
1422ad135b5dSChristopher Siden {
1423ad135b5dSChristopher Siden 	char blkbuf[BP_SPRINTF_LEN];
1424ad135b5dSChristopher Siden 
1425ad135b5dSChristopher Siden 	if (bp->blk_birth != 0) {
142643466aaeSMax Grossman 		snprintf_blkptr(blkbuf, sizeof (blkbuf), bp);
1427ad135b5dSChristopher Siden 		(void) printf("\t%s\n", blkbuf);
1428ad135b5dSChristopher Siden 	}
1429ad135b5dSChristopher Siden 	return (0);
1430ad135b5dSChristopher Siden }
1431ad135b5dSChristopher Siden 
1432ad135b5dSChristopher Siden static void
1433ad135b5dSChristopher Siden dump_bptree(objset_t *os, uint64_t obj, char *name)
1434ad135b5dSChristopher Siden {
1435ad135b5dSChristopher Siden 	char bytes[32];
1436ad135b5dSChristopher Siden 	bptree_phys_t *bt;
1437ad135b5dSChristopher Siden 	dmu_buf_t *db;
1438ad135b5dSChristopher Siden 
1439ad135b5dSChristopher Siden 	if (dump_opt['d'] < 3)
1440ad135b5dSChristopher Siden 		return;
1441ad135b5dSChristopher Siden 
1442b420f3adSRichard Lowe 	VERIFY3U(0, ==, dmu_bonus_hold(os, obj, FTAG, &db));
1443ad135b5dSChristopher Siden 	bt = db->db_data;
1444ad135b5dSChristopher Siden 	zdb_nicenum(bt->bt_bytes, bytes);
1445ad135b5dSChristopher Siden 	(void) printf("\n    %s: %llu datasets, %s\n",
1446ad135b5dSChristopher Siden 	    name, (unsigned long long)(bt->bt_end - bt->bt_begin), bytes);
1447ad135b5dSChristopher Siden 	dmu_buf_rele(db, FTAG);
1448ad135b5dSChristopher Siden 
1449ad135b5dSChristopher Siden 	if (dump_opt['d'] < 5)
1450ad135b5dSChristopher Siden 		return;
1451ad135b5dSChristopher Siden 
1452ad135b5dSChristopher Siden 	(void) printf("\n");
1453ad135b5dSChristopher Siden 
1454ad135b5dSChristopher Siden 	(void) bptree_iterate(os, obj, B_FALSE, dump_bptree_cb, NULL, NULL);
1455ad135b5dSChristopher Siden }
1456ad135b5dSChristopher Siden 
1457cde58dbcSMatthew Ahrens /* ARGSUSED */
1458cde58dbcSMatthew Ahrens static int
1459cde58dbcSMatthew Ahrens dump_bpobj_cb(void *arg, const blkptr_t *bp, dmu_tx_t *tx)
1460cde58dbcSMatthew Ahrens {
1461cde58dbcSMatthew Ahrens 	char blkbuf[BP_SPRINTF_LEN];
1462cde58dbcSMatthew Ahrens 
1463cde58dbcSMatthew Ahrens 	ASSERT(bp->blk_birth != 0);
146443466aaeSMax Grossman 	snprintf_blkptr_compact(blkbuf, sizeof (blkbuf), bp);
1465cde58dbcSMatthew Ahrens 	(void) printf("\t%s\n", blkbuf);
1466cde58dbcSMatthew Ahrens 	return (0);
1467cde58dbcSMatthew Ahrens }
1468cde58dbcSMatthew Ahrens 
1469fa9e4066Sahrens static void
1470732885fcSMatthew Ahrens dump_full_bpobj(bpobj_t *bpo, char *name, int indent)
1471fa9e4066Sahrens {
14723f9d6ad7SLin Ling 	char bytes[32];
14733f9d6ad7SLin Ling 	char comp[32];
14743f9d6ad7SLin Ling 	char uncomp[32];
1475fa9e4066Sahrens 
1476fa9e4066Sahrens 	if (dump_opt['d'] < 3)
1477fa9e4066Sahrens 		return;
1478fa9e4066Sahrens 
1479cde58dbcSMatthew Ahrens 	zdb_nicenum(bpo->bpo_phys->bpo_bytes, bytes);
1480d0475637SMatthew Ahrens 	if (bpo->bpo_havesubobj && bpo->bpo_phys->bpo_subobjs != 0) {
1481cde58dbcSMatthew Ahrens 		zdb_nicenum(bpo->bpo_phys->bpo_comp, comp);
1482cde58dbcSMatthew Ahrens 		zdb_nicenum(bpo->bpo_phys->bpo_uncomp, uncomp);
1483d0475637SMatthew Ahrens 		(void) printf("    %*s: object %llu, %llu local blkptrs, "
1484732885fcSMatthew Ahrens 		    "%llu subobjs in object %llu, %s (%s/%s comp)\n",
1485d0475637SMatthew Ahrens 		    indent * 8, name,
1486d0475637SMatthew Ahrens 		    (u_longlong_t)bpo->bpo_object,
1487d0475637SMatthew Ahrens 		    (u_longlong_t)bpo->bpo_phys->bpo_num_blkptrs,
1488cde58dbcSMatthew Ahrens 		    (u_longlong_t)bpo->bpo_phys->bpo_num_subobjs,
1489732885fcSMatthew Ahrens 		    (u_longlong_t)bpo->bpo_phys->bpo_subobjs,
149099653d4eSeschrock 		    bytes, comp, uncomp);
1491d0475637SMatthew Ahrens 
1492d0475637SMatthew Ahrens 		for (uint64_t i = 0; i < bpo->bpo_phys->bpo_num_subobjs; i++) {
1493d0475637SMatthew Ahrens 			uint64_t subobj;
1494d0475637SMatthew Ahrens 			bpobj_t subbpo;
1495d0475637SMatthew Ahrens 			int error;
1496d0475637SMatthew Ahrens 			VERIFY0(dmu_read(bpo->bpo_os,
1497d0475637SMatthew Ahrens 			    bpo->bpo_phys->bpo_subobjs,
1498d0475637SMatthew Ahrens 			    i * sizeof (subobj), sizeof (subobj), &subobj, 0));
1499d0475637SMatthew Ahrens 			error = bpobj_open(&subbpo, bpo->bpo_os, subobj);
1500d0475637SMatthew Ahrens 			if (error != 0) {
1501d0475637SMatthew Ahrens 				(void) printf("ERROR %u while trying to open "
1502d0475637SMatthew Ahrens 				    "subobj id %llu\n",
1503d0475637SMatthew Ahrens 				    error, (u_longlong_t)subobj);
1504d0475637SMatthew Ahrens 				continue;
1505d0475637SMatthew Ahrens 			}
1506732885fcSMatthew Ahrens 			dump_full_bpobj(&subbpo, "subobj", indent + 1);
150777061867SMatthew Ahrens 			bpobj_close(&subbpo);
1508d0475637SMatthew Ahrens 		}
150999653d4eSeschrock 	} else {
1510d0475637SMatthew Ahrens 		(void) printf("    %*s: object %llu, %llu blkptrs, %s\n",
1511d0475637SMatthew Ahrens 		    indent * 8, name,
1512d0475637SMatthew Ahrens 		    (u_longlong_t)bpo->bpo_object,
1513d0475637SMatthew Ahrens 		    (u_longlong_t)bpo->bpo_phys->bpo_num_blkptrs,
1514d0475637SMatthew Ahrens 		    bytes);
151599653d4eSeschrock 	}
1516fa9e4066Sahrens 
1517cde58dbcSMatthew Ahrens 	if (dump_opt['d'] < 5)
1518fa9e4066Sahrens 		return;
1519fa9e4066Sahrens 
1520fa9e4066Sahrens 
1521d0475637SMatthew Ahrens 	if (indent == 0) {
1522d0475637SMatthew Ahrens 		(void) bpobj_iterate_nofree(bpo, dump_bpobj_cb, NULL, NULL);
1523d0475637SMatthew Ahrens 		(void) printf("\n");
1524d0475637SMatthew Ahrens 	}
1525cde58dbcSMatthew Ahrens }
152644cd46caSbillm 
1527cde58dbcSMatthew Ahrens static void
1528cde58dbcSMatthew Ahrens dump_deadlist(dsl_deadlist_t *dl)
1529cde58dbcSMatthew Ahrens {
1530cde58dbcSMatthew Ahrens 	dsl_deadlist_entry_t *dle;
1531d0475637SMatthew Ahrens 	uint64_t unused;
1532cde58dbcSMatthew Ahrens 	char bytes[32];
1533cde58dbcSMatthew Ahrens 	char comp[32];
1534cde58dbcSMatthew Ahrens 	char uncomp[32];
1535cde58dbcSMatthew Ahrens 
1536cde58dbcSMatthew Ahrens 	if (dump_opt['d'] < 3)
1537cde58dbcSMatthew Ahrens 		return;
1538cde58dbcSMatthew Ahrens 
153990c76c66SMatthew Ahrens 	if (dl->dl_oldfmt) {
1540732885fcSMatthew Ahrens 		dump_full_bpobj(&dl->dl_bpobj, "old-format deadlist", 0);
154190c76c66SMatthew Ahrens 		return;
154290c76c66SMatthew Ahrens 	}
154390c76c66SMatthew Ahrens 
1544cde58dbcSMatthew Ahrens 	zdb_nicenum(dl->dl_phys->dl_used, bytes);
1545cde58dbcSMatthew Ahrens 	zdb_nicenum(dl->dl_phys->dl_comp, comp);
1546cde58dbcSMatthew Ahrens 	zdb_nicenum(dl->dl_phys->dl_uncomp, uncomp);
1547cde58dbcSMatthew Ahrens 	(void) printf("\n    Deadlist: %s (%s/%s comp)\n",
1548cde58dbcSMatthew Ahrens 	    bytes, comp, uncomp);
1549cde58dbcSMatthew Ahrens 
1550cde58dbcSMatthew Ahrens 	if (dump_opt['d'] < 4)
1551cde58dbcSMatthew Ahrens 		return;
1552cde58dbcSMatthew Ahrens 
1553cde58dbcSMatthew Ahrens 	(void) printf("\n");
1554cde58dbcSMatthew Ahrens 
1555d0475637SMatthew Ahrens 	/* force the tree to be loaded */
1556d0475637SMatthew Ahrens 	dsl_deadlist_space_range(dl, 0, UINT64_MAX, &unused, &unused, &unused);
1557d0475637SMatthew Ahrens 
1558cde58dbcSMatthew Ahrens 	for (dle = avl_first(&dl->dl_tree); dle;
1559cde58dbcSMatthew Ahrens 	    dle = AVL_NEXT(&dl->dl_tree, dle)) {
1560d0475637SMatthew Ahrens 		if (dump_opt['d'] >= 5) {
1561d0475637SMatthew Ahrens 			char buf[128];
1562d0475637SMatthew Ahrens 			(void) snprintf(buf, sizeof (buf), "mintxg %llu -> ",
1563d0475637SMatthew Ahrens 			    (longlong_t)dle->dle_mintxg,
1564d0475637SMatthew Ahrens 			    (longlong_t)dle->dle_bpobj.bpo_object);
1565fa9e4066Sahrens 
1566732885fcSMatthew Ahrens 			dump_full_bpobj(&dle->dle_bpobj, buf, 0);
1567d0475637SMatthew Ahrens 		} else {
1568d0475637SMatthew Ahrens 			(void) printf("mintxg %llu -> obj %llu\n",
1569d0475637SMatthew Ahrens 			    (longlong_t)dle->dle_mintxg,
1570d0475637SMatthew Ahrens 			    (longlong_t)dle->dle_bpobj.bpo_object);
1571d0475637SMatthew Ahrens 
1572d0475637SMatthew Ahrens 		}
1573cde58dbcSMatthew Ahrens 	}
1574fa9e4066Sahrens }
1575fa9e4066Sahrens 
1576e0d35c44Smarks static avl_tree_t idx_tree;
1577e0d35c44Smarks static avl_tree_t domain_tree;
1578e0d35c44Smarks static boolean_t fuid_table_loaded;
15790a586ceaSMark Shellenbaum static boolean_t sa_loaded;
15800a586ceaSMark Shellenbaum sa_attr_type_t *sa_attr_table;
1581e0d35c44Smarks 
1582e0d35c44Smarks static void
1583e0d35c44Smarks fuid_table_destroy()
1584e0d35c44Smarks {
1585e0d35c44Smarks 	if (fuid_table_loaded) {
1586e0d35c44Smarks 		zfs_fuid_table_destroy(&idx_tree, &domain_tree);
1587e0d35c44Smarks 		fuid_table_loaded = B_FALSE;
1588e0d35c44Smarks 	}
1589e0d35c44Smarks }
1590e0d35c44Smarks 
1591e0d35c44Smarks /*
1592e0d35c44Smarks  * print uid or gid information.
1593e0d35c44Smarks  * For normal POSIX id just the id is printed in decimal format.
1594e0d35c44Smarks  * For CIFS files with FUID the fuid is printed in hex followed by
1595d0475637SMatthew Ahrens  * the domain-rid string.
1596e0d35c44Smarks  */
1597e0d35c44Smarks static void
1598e0d35c44Smarks print_idstr(uint64_t id, const char *id_type)
1599e0d35c44Smarks {
1600e0d35c44Smarks 	if (FUID_INDEX(id)) {
1601e0d35c44Smarks 		char *domain;
1602e0d35c44Smarks 
1603e0d35c44Smarks 		domain = zfs_fuid_idx_domain(&idx_tree, FUID_INDEX(id));
1604e0d35c44Smarks 		(void) printf("\t%s     %llx [%s-%d]\n", id_type,
1605e0d35c44Smarks 		    (u_longlong_t)id, domain, (int)FUID_RID(id));
1606e0d35c44Smarks 	} else {
1607e0d35c44Smarks 		(void) printf("\t%s     %llu\n", id_type, (u_longlong_t)id);
1608e0d35c44Smarks 	}
1609e0d35c44Smarks 
1610e0d35c44Smarks }
1611e0d35c44Smarks 
1612e0d35c44Smarks static void
16130a586ceaSMark Shellenbaum dump_uidgid(objset_t *os, uint64_t uid, uint64_t gid)
1614e0d35c44Smarks {
1615e0d35c44Smarks 	uint32_t uid_idx, gid_idx;
1616e0d35c44Smarks 
16170a586ceaSMark Shellenbaum 	uid_idx = FUID_INDEX(uid);
16180a586ceaSMark Shellenbaum 	gid_idx = FUID_INDEX(gid);
1619e0d35c44Smarks 
1620e0d35c44Smarks 	/* Load domain table, if not already loaded */
1621e0d35c44Smarks 	if (!fuid_table_loaded && (uid_idx || gid_idx)) {
1622e0d35c44Smarks 		uint64_t fuid_obj;
1623e0d35c44Smarks 
1624e0d35c44Smarks 		/* first find the fuid object.  It lives in the master node */
1625e0d35c44Smarks 		VERIFY(zap_lookup(os, MASTER_NODE_OBJ, ZFS_FUID_TABLES,
1626e0d35c44Smarks 		    8, 1, &fuid_obj) == 0);
162789459e17SMark Shellenbaum 		zfs_fuid_avl_tree_create(&idx_tree, &domain_tree);
1628e0d35c44Smarks 		(void) zfs_fuid_table_load(os, fuid_obj,
1629e0d35c44Smarks 		    &idx_tree, &domain_tree);
1630e0d35c44Smarks 		fuid_table_loaded = B_TRUE;
1631e0d35c44Smarks 	}
1632e0d35c44Smarks 
16330a586ceaSMark Shellenbaum 	print_idstr(uid, "uid");
16340a586ceaSMark Shellenbaum 	print_idstr(gid, "gid");
1635e0d35c44Smarks }
1636e0d35c44Smarks 
1637fa9e4066Sahrens /*ARGSUSED*/
1638fa9e4066Sahrens static void
1639fa9e4066Sahrens dump_znode(objset_t *os, uint64_t object, void *data, size_t size)
1640fa9e4066Sahrens {
1641fa9e4066Sahrens 	char path[MAXPATHLEN * 2];	/* allow for xattr and failure prefix */
16420a586ceaSMark Shellenbaum 	sa_handle_t *hdl;
16430a586ceaSMark Shellenbaum 	uint64_t xattr, rdev, gen;
16440a586ceaSMark Shellenbaum 	uint64_t uid, gid, mode, fsize, parent, links;
16458f2529deSMark Shellenbaum 	uint64_t pflags;
16460a586ceaSMark Shellenbaum 	uint64_t acctm[2], modtm[2], chgtm[2], crtm[2];
16470a586ceaSMark Shellenbaum 	time_t z_crtime, z_atime, z_mtime, z_ctime;
16488f2529deSMark Shellenbaum 	sa_bulk_attr_t bulk[12];
16490a586ceaSMark Shellenbaum 	int idx = 0;
165055434c77Sek 	int error;
1651fa9e4066Sahrens 
16520a586ceaSMark Shellenbaum 	if (!sa_loaded) {
16530a586ceaSMark Shellenbaum 		uint64_t sa_attrs = 0;
16540a586ceaSMark Shellenbaum 		uint64_t version;
16550a586ceaSMark Shellenbaum 
16560a586ceaSMark Shellenbaum 		VERIFY(zap_lookup(os, MASTER_NODE_OBJ, ZPL_VERSION_STR,
16570a586ceaSMark Shellenbaum 		    8, 1, &version) == 0);
16580a586ceaSMark Shellenbaum 		if (version >= ZPL_VERSION_SA) {
16590a586ceaSMark Shellenbaum 			VERIFY(zap_lookup(os, MASTER_NODE_OBJ, ZFS_SA_ATTRS,
16600a586ceaSMark Shellenbaum 			    8, 1, &sa_attrs) == 0);
16610a586ceaSMark Shellenbaum 		}
16621d8ccc7bSMark Shellenbaum 		if ((error = sa_setup(os, sa_attrs, zfs_attr_table,
16631d8ccc7bSMark Shellenbaum 		    ZPL_END, &sa_attr_table)) != 0) {
16641d8ccc7bSMark Shellenbaum 			(void) printf("sa_setup failed errno %d, can't "
16651d8ccc7bSMark Shellenbaum 			    "display znode contents\n", error);
16661d8ccc7bSMark Shellenbaum 			return;
16671d8ccc7bSMark Shellenbaum 		}
16680a586ceaSMark Shellenbaum 		sa_loaded = B_TRUE;
16690a586ceaSMark Shellenbaum 	}
16700a586ceaSMark Shellenbaum 
16710a586ceaSMark Shellenbaum 	if (sa_handle_get(os, object, NULL, SA_HDL_PRIVATE, &hdl)) {
16720a586ceaSMark Shellenbaum 		(void) printf("Failed to get handle for SA znode\n");
16730a586ceaSMark Shellenbaum 		return;
16740a586ceaSMark Shellenbaum 	}
16750a586ceaSMark Shellenbaum 
16760a586ceaSMark Shellenbaum 	SA_ADD_BULK_ATTR(bulk, idx, sa_attr_table[ZPL_UID], NULL, &uid, 8);
16770a586ceaSMark Shellenbaum 	SA_ADD_BULK_ATTR(bulk, idx, sa_attr_table[ZPL_GID], NULL, &gid, 8);
16780a586ceaSMark Shellenbaum 	SA_ADD_BULK_ATTR(bulk, idx, sa_attr_table[ZPL_LINKS], NULL,
16790a586ceaSMark Shellenbaum 	    &links, 8);
16800a586ceaSMark Shellenbaum 	SA_ADD_BULK_ATTR(bulk, idx, sa_attr_table[ZPL_GEN], NULL, &gen, 8);
16810a586ceaSMark Shellenbaum 	SA_ADD_BULK_ATTR(bulk, idx, sa_attr_table[ZPL_MODE], NULL,
16820a586ceaSMark Shellenbaum 	    &mode, 8);
16830a586ceaSMark Shellenbaum 	SA_ADD_BULK_ATTR(bulk, idx, sa_attr_table[ZPL_PARENT],
16840a586ceaSMark Shellenbaum 	    NULL, &parent, 8);
16850a586ceaSMark Shellenbaum 	SA_ADD_BULK_ATTR(bulk, idx, sa_attr_table[ZPL_SIZE], NULL,
16860a586ceaSMark Shellenbaum 	    &fsize, 8);
16870a586ceaSMark Shellenbaum 	SA_ADD_BULK_ATTR(bulk, idx, sa_attr_table[ZPL_ATIME], NULL,
16880a586ceaSMark Shellenbaum 	    acctm, 16);
16890a586ceaSMark Shellenbaum 	SA_ADD_BULK_ATTR(bulk, idx, sa_attr_table[ZPL_MTIME], NULL,
16900a586ceaSMark Shellenbaum 	    modtm, 16);
16910a586ceaSMark Shellenbaum 	SA_ADD_BULK_ATTR(bulk, idx, sa_attr_table[ZPL_CRTIME], NULL,
16920a586ceaSMark Shellenbaum 	    crtm, 16);
16930a586ceaSMark Shellenbaum 	SA_ADD_BULK_ATTR(bulk, idx, sa_attr_table[ZPL_CTIME], NULL,
16940a586ceaSMark Shellenbaum 	    chgtm, 16);
16958f2529deSMark Shellenbaum 	SA_ADD_BULK_ATTR(bulk, idx, sa_attr_table[ZPL_FLAGS], NULL,
16968f2529deSMark Shellenbaum 	    &pflags, 8);
16970a586ceaSMark Shellenbaum 
16980a586ceaSMark Shellenbaum 	if (sa_bulk_lookup(hdl, bulk, idx)) {
16990a586ceaSMark Shellenbaum 		(void) sa_handle_destroy(hdl);
17000a586ceaSMark Shellenbaum 		return;
17010a586ceaSMark Shellenbaum 	}
1702fa9e4066Sahrens 
170355434c77Sek 	error = zfs_obj_to_path(os, object, path, sizeof (path));
170455434c77Sek 	if (error != 0) {
170555434c77Sek 		(void) snprintf(path, sizeof (path), "\?\?\?<object#%llu>",
170655434c77Sek 		    (u_longlong_t)object);
170755434c77Sek 	}
1708fa9e4066Sahrens 	if (dump_opt['d'] < 3) {
170955434c77Sek 		(void) printf("\t%s\n", path);
17100a586ceaSMark Shellenbaum 		(void) sa_handle_destroy(hdl);
1711fa9e4066Sahrens 		return;
1712fa9e4066Sahrens 	}
1713fa9e4066Sahrens 
17140a586ceaSMark Shellenbaum 	z_crtime = (time_t)crtm[0];
17150a586ceaSMark Shellenbaum 	z_atime = (time_t)acctm[0];
17160a586ceaSMark Shellenbaum 	z_mtime = (time_t)modtm[0];
17170a586ceaSMark Shellenbaum 	z_ctime = (time_t)chgtm[0];
1718fa9e4066Sahrens 
171955434c77Sek 	(void) printf("\tpath	%s\n", path);
17200a586ceaSMark Shellenbaum 	dump_uidgid(os, uid, gid);
1721fa9e4066Sahrens 	(void) printf("\tatime	%s", ctime(&z_atime));
1722fa9e4066Sahrens 	(void) printf("\tmtime	%s", ctime(&z_mtime));
1723fa9e4066Sahrens 	(void) printf("\tctime	%s", ctime(&z_ctime));
1724fa9e4066Sahrens 	(void) printf("\tcrtime	%s", ctime(&z_crtime));
17250a586ceaSMark Shellenbaum 	(void) printf("\tgen	%llu\n", (u_longlong_t)gen);
17260a586ceaSMark Shellenbaum 	(void) printf("\tmode	%llo\n", (u_longlong_t)mode);
17270a586ceaSMark Shellenbaum 	(void) printf("\tsize	%llu\n", (u_longlong_t)fsize);
17280a586ceaSMark Shellenbaum 	(void) printf("\tparent	%llu\n", (u_longlong_t)parent);
17290a586ceaSMark Shellenbaum 	(void) printf("\tlinks	%llu\n", (u_longlong_t)links);
17308f2529deSMark Shellenbaum 	(void) printf("\tpflags	%llx\n", (u_longlong_t)pflags);
17310a586ceaSMark Shellenbaum 	if (sa_lookup(hdl, sa_attr_table[ZPL_XATTR], &xattr,
17320a586ceaSMark Shellenbaum 	    sizeof (uint64_t)) == 0)
17330a586ceaSMark Shellenbaum 		(void) printf("\txattr	%llu\n", (u_longlong_t)xattr);
17340a586ceaSMark Shellenbaum 	if (sa_lookup(hdl, sa_attr_table[ZPL_RDEV], &rdev,
17350a586ceaSMark Shellenbaum 	    sizeof (uint64_t)) == 0)
17360a586ceaSMark Shellenbaum 		(void) printf("\trdev	0x%016llx\n", (u_longlong_t)rdev);
17370a586ceaSMark Shellenbaum 	sa_handle_destroy(hdl);
1738fa9e4066Sahrens }
1739fa9e4066Sahrens 
1740fa9e4066Sahrens /*ARGSUSED*/
1741fa9e4066Sahrens static void
1742fa9e4066Sahrens dump_acl(objset_t *os, uint64_t object, void *data, size_t size)
1743fa9e4066Sahrens {
1744fa9e4066Sahrens }
1745fa9e4066Sahrens 
1746fa9e4066Sahrens /*ARGSUSED*/
1747fa9e4066Sahrens static void
1748fa9e4066Sahrens dump_dmu_objset(objset_t *os, uint64_t object, void *data, size_t size)
1749fa9e4066Sahrens {
1750fa9e4066Sahrens }
1751fa9e4066Sahrens 
17526de8f417SVictor Latushkin static object_viewer_t *object_viewer[DMU_OT_NUMTYPES + 1] = {
1753fa9e4066Sahrens 	dump_none,		/* unallocated			*/
1754fa9e4066Sahrens 	dump_zap,		/* object directory		*/
1755fa9e4066Sahrens 	dump_uint64,		/* object array			*/
1756fa9e4066Sahrens 	dump_none,		/* packed nvlist		*/
1757fa9e4066Sahrens 	dump_packed_nvlist,	/* packed nvlist size		*/
1758732885fcSMatthew Ahrens 	dump_none,		/* bpobj			*/
1759732885fcSMatthew Ahrens 	dump_bpobj,		/* bpobj header			*/
1760fa9e4066Sahrens 	dump_none,		/* SPA space map header		*/
1761fa9e4066Sahrens 	dump_none,		/* SPA space map		*/
1762fa9e4066Sahrens 	dump_none,		/* ZIL intent log		*/
1763fa9e4066Sahrens 	dump_dnode,		/* DMU dnode			*/
1764fa9e4066Sahrens 	dump_dmu_objset,	/* DMU objset			*/
1765ea8dc4b6Seschrock 	dump_dsl_dir,		/* DSL directory		*/
1766fa9e4066Sahrens 	dump_zap,		/* DSL directory child map	*/
1767fa9e4066Sahrens 	dump_zap,		/* DSL dataset snap map		*/
1768fa9e4066Sahrens 	dump_zap,		/* DSL props			*/
1769fa9e4066Sahrens 	dump_dsl_dataset,	/* DSL dataset			*/
1770fa9e4066Sahrens 	dump_znode,		/* ZFS znode			*/
1771da6c28aaSamw 	dump_acl,		/* ZFS V0 ACL			*/
1772fa9e4066Sahrens 	dump_uint8,		/* ZFS plain file		*/
1773e7437265Sahrens 	dump_zpldir,		/* ZFS directory		*/
1774fa9e4066Sahrens 	dump_zap,		/* ZFS master node		*/
1775fa9e4066Sahrens 	dump_zap,		/* ZFS delete queue		*/
1776fa9e4066Sahrens 	dump_uint8,		/* zvol object			*/
1777fa9e4066Sahrens 	dump_zap,		/* zvol prop			*/
1778fa9e4066Sahrens 	dump_uint8,		/* other uint8[]		*/
1779fa9e4066Sahrens 	dump_uint64,		/* other uint64[]		*/
1780fa9e4066Sahrens 	dump_zap,		/* other ZAP			*/
1781ea8dc4b6Seschrock 	dump_zap,		/* persistent error log		*/
178206eeb2adSek 	dump_uint8,		/* SPA history			*/
17834445fffbSMatthew Ahrens 	dump_history_offsets,	/* SPA history offsets		*/
1784b1b8ab34Slling 	dump_zap,		/* Pool properties		*/
1785ecd6cf80Smarks 	dump_zap,		/* DSL permissions		*/
1786da6c28aaSamw 	dump_acl,		/* ZFS ACL			*/
1787da6c28aaSamw 	dump_uint8,		/* ZFS SYSACL			*/
1788da6c28aaSamw 	dump_none,		/* FUID nvlist			*/
1789da6c28aaSamw 	dump_packed_nvlist,	/* FUID nvlist size		*/
1790088f3894Sahrens 	dump_zap,		/* DSL dataset next clones	*/
1791088f3894Sahrens 	dump_zap,		/* DSL scrub queue		*/
179214843421SMatthew Ahrens 	dump_zap,		/* ZFS user/group used		*/
179314843421SMatthew Ahrens 	dump_zap,		/* ZFS user/group quota		*/
1794842727c2SChris Kirby 	dump_zap,		/* snapshot refcount tags	*/
1795486ae710SMatthew Ahrens 	dump_ddt_zap,		/* DDT ZAP object		*/
1796b24ab676SJeff Bonwick 	dump_zap,		/* DDT statistics		*/
17970a586ceaSMark Shellenbaum 	dump_znode,		/* SA object			*/
17980a586ceaSMark Shellenbaum 	dump_zap,		/* SA Master Node		*/
17990a586ceaSMark Shellenbaum 	dump_sa_attrs,		/* SA attribute registration	*/
18000a586ceaSMark Shellenbaum 	dump_sa_layouts,	/* SA attribute layouts		*/
18013f9d6ad7SLin Ling 	dump_zap,		/* DSL scrub translations	*/
18023f9d6ad7SLin Ling 	dump_none,		/* fake dedup BP		*/
1803cde58dbcSMatthew Ahrens 	dump_zap,		/* deadlist			*/
1804cde58dbcSMatthew Ahrens 	dump_none,		/* deadlist hdr			*/
1805cde58dbcSMatthew Ahrens 	dump_zap,		/* dsl clones			*/
1806732885fcSMatthew Ahrens 	dump_bpobj_subobjs,	/* bpobj subobjs		*/
18070a586ceaSMark Shellenbaum 	dump_unknown,		/* Unknown type, must be last	*/
1808fa9e4066Sahrens };
1809fa9e4066Sahrens 
1810fa9e4066Sahrens static void
1811fa9e4066Sahrens dump_object(objset_t *os, uint64_t object, int verbosity, int *print_header)
1812fa9e4066Sahrens {
1813fa9e4066Sahrens 	dmu_buf_t *db = NULL;
1814fa9e4066Sahrens 	dmu_object_info_t doi;
1815fa9e4066Sahrens 	dnode_t *dn;
1816fa9e4066Sahrens 	void *bonus = NULL;
1817fa9e4066Sahrens 	size_t bsize = 0;
18183f9d6ad7SLin Ling 	char iblk[32], dblk[32], lsize[32], asize[32], fill[32];
18193f9d6ad7SLin Ling 	char bonus_size[32];
1820fa9e4066Sahrens 	char aux[50];
1821fa9e4066Sahrens 	int error;
1822fa9e4066Sahrens 
1823fa9e4066Sahrens 	if (*print_header) {
1824b24ab676SJeff Bonwick 		(void) printf("\n%10s  %3s  %5s  %5s  %5s  %5s  %6s  %s\n",
1825b24ab676SJeff Bonwick 		    "Object", "lvl", "iblk", "dblk", "dsize", "lsize",
1826b24ab676SJeff Bonwick 		    "%full", "type");
1827fa9e4066Sahrens 		*print_header = 0;
1828fa9e4066Sahrens 	}
1829fa9e4066Sahrens 
1830fa9e4066Sahrens 	if (object == 0) {
1831744947dcSTom Erickson 		dn = DMU_META_DNODE(os);
1832fa9e4066Sahrens 	} else {
1833ea8dc4b6Seschrock 		error = dmu_bonus_hold(os, object, FTAG, &db);
1834ea8dc4b6Seschrock 		if (error)
1835ea8dc4b6Seschrock 			fatal("dmu_bonus_hold(%llu) failed, errno %u",
1836ea8dc4b6Seschrock 			    object, error);
1837fa9e4066Sahrens 		bonus = db->db_data;
1838fa9e4066Sahrens 		bsize = db->db_size;
1839744947dcSTom Erickson 		dn = DB_DNODE((dmu_buf_impl_t *)db);
1840fa9e4066Sahrens 	}
1841fa9e4066Sahrens 	dmu_object_info_from_dnode(dn, &doi);
1842fa9e4066Sahrens 
18433f9d6ad7SLin Ling 	zdb_nicenum(doi.doi_metadata_block_size, iblk);
18443f9d6ad7SLin Ling 	zdb_nicenum(doi.doi_data_block_size, dblk);
18453f9d6ad7SLin Ling 	zdb_nicenum(doi.doi_max_offset, lsize);
18463f9d6ad7SLin Ling 	zdb_nicenum(doi.doi_physical_blocks_512 << 9, asize);
18473f9d6ad7SLin Ling 	zdb_nicenum(doi.doi_bonus_size, bonus_size);
1848b24ab676SJeff Bonwick 	(void) sprintf(fill, "%6.2f", 100.0 * doi.doi_fill_count *
1849bbfd46c4SJeff Bonwick 	    doi.doi_data_block_size / (object == 0 ? DNODES_PER_BLOCK : 1) /
1850bbfd46c4SJeff Bonwick 	    doi.doi_max_offset);
1851fa9e4066Sahrens 
1852fa9e4066Sahrens 	aux[0] = '\0';
1853fa9e4066Sahrens 
1854e7437265Sahrens 	if (doi.doi_checksum != ZIO_CHECKSUM_INHERIT || verbosity >= 6) {
1855fa9e4066Sahrens 		(void) snprintf(aux + strlen(aux), sizeof (aux), " (K=%s)",
18566de8f417SVictor Latushkin 		    ZDB_CHECKSUM_NAME(doi.doi_checksum));
1857e7437265Sahrens 	}
1858fa9e4066Sahrens 
1859e7437265Sahrens 	if (doi.doi_compress != ZIO_COMPRESS_INHERIT || verbosity >= 6) {
1860fa9e4066Sahrens 		(void) snprintf(aux + strlen(aux), sizeof (aux), " (Z=%s)",
18616de8f417SVictor Latushkin 		    ZDB_COMPRESS_NAME(doi.doi_compress));
1862e7437265Sahrens 	}
1863fa9e4066Sahrens 
1864b24ab676SJeff Bonwick 	(void) printf("%10lld  %3u  %5s  %5s  %5s  %5s  %6s  %s%s\n",
1865b24ab676SJeff Bonwick 	    (u_longlong_t)object, doi.doi_indirection, iblk, dblk,
1866b24ab676SJeff Bonwick 	    asize, lsize, fill, ZDB_OT_NAME(doi.doi_type), aux);
1867fa9e4066Sahrens 
1868fa9e4066Sahrens 	if (doi.doi_bonus_type != DMU_OT_NONE && verbosity > 3) {
1869b24ab676SJeff Bonwick 		(void) printf("%10s  %3s  %5s  %5s  %5s  %5s  %6s  %s\n",
1870b24ab676SJeff Bonwick 		    "", "", "", "", "", bonus_size, "bonus",
18716de8f417SVictor Latushkin 		    ZDB_OT_NAME(doi.doi_bonus_type));
1872fa9e4066Sahrens 	}
1873fa9e4066Sahrens 
1874fa9e4066Sahrens 	if (verbosity >= 4) {
18750a586ceaSMark Shellenbaum 		(void) printf("\tdnode flags: %s%s%s\n",
187614843421SMatthew Ahrens 		    (dn->dn_phys->dn_flags & DNODE_FLAG_USED_BYTES) ?
187714843421SMatthew Ahrens 		    "USED_BYTES " : "",
187814843421SMatthew Ahrens 		    (dn->dn_phys->dn_flags & DNODE_FLAG_USERUSED_ACCOUNTED) ?
18790a586ceaSMark Shellenbaum 		    "USERUSED_ACCOUNTED " : "",
18800a586ceaSMark Shellenbaum 		    (dn->dn_phys->dn_flags & DNODE_FLAG_SPILL_BLKPTR) ?
18810a586ceaSMark Shellenbaum 		    "SPILL_BLKPTR" : "");
188214843421SMatthew Ahrens 		(void) printf("\tdnode maxblkid: %llu\n",
188314843421SMatthew Ahrens 		    (longlong_t)dn->dn_phys->dn_maxblkid);
188414843421SMatthew Ahrens 
18856de8f417SVictor Latushkin 		object_viewer[ZDB_OT_TYPE(doi.doi_bonus_type)](os, object,
18866de8f417SVictor Latushkin 		    bonus, bsize);
18876de8f417SVictor Latushkin 		object_viewer[ZDB_OT_TYPE(doi.doi_type)](os, object, NULL, 0);
1888fa9e4066Sahrens 		*print_header = 1;
1889fa9e4066Sahrens 	}
1890fa9e4066Sahrens 
1891fa9e4066Sahrens 	if (verbosity >= 5)
189288b7b0f2SMatthew Ahrens 		dump_indirect(dn);
1893fa9e4066Sahrens 
1894fa9e4066Sahrens 	if (verbosity >= 5) {
1895fa9e4066Sahrens 		/*
1896fa9e4066Sahrens 		 * Report the list of segments that comprise the object.
1897fa9e4066Sahrens 		 */
1898fa9e4066Sahrens 		uint64_t start = 0;
1899fa9e4066Sahrens 		uint64_t end;
1900fa9e4066Sahrens 		uint64_t blkfill = 1;
1901fa9e4066Sahrens 		int minlvl = 1;
1902fa9e4066Sahrens 
1903fa9e4066Sahrens 		if (dn->dn_type == DMU_OT_DNODE) {
1904fa9e4066Sahrens 			minlvl = 0;
1905fa9e4066Sahrens 			blkfill = DNODES_PER_BLOCK;
1906fa9e4066Sahrens 		}
1907fa9e4066Sahrens 
1908fa9e4066Sahrens 		for (;;) {
19093f9d6ad7SLin Ling 			char segsize[32];
1910cdb0ab79Smaybee 			error = dnode_next_offset(dn,
1911cdb0ab79Smaybee 			    0, &start, minlvl, blkfill, 0);
1912fa9e4066Sahrens 			if (error)
1913fa9e4066Sahrens 				break;
1914fa9e4066Sahrens 			end = start;
1915cdb0ab79Smaybee 			error = dnode_next_offset(dn,
1916cdb0ab79Smaybee 			    DNODE_FIND_HOLE, &end, minlvl, blkfill, 0);
19173f9d6ad7SLin Ling 			zdb_nicenum(end - start, segsize);
1918fa9e4066Sahrens 			(void) printf("\t\tsegment [%016llx, %016llx)"
1919fa9e4066Sahrens 			    " size %5s\n", (u_longlong_t)start,
1920fa9e4066Sahrens 			    (u_longlong_t)end, segsize);
1921fa9e4066Sahrens 			if (error)
1922fa9e4066Sahrens 				break;
1923fa9e4066Sahrens 			start = end;
1924fa9e4066Sahrens 		}
1925fa9e4066Sahrens 	}
1926fa9e4066Sahrens 
1927fa9e4066Sahrens 	if (db != NULL)
1928ea8dc4b6Seschrock 		dmu_buf_rele(db, FTAG);
1929fa9e4066Sahrens }
1930fa9e4066Sahrens 
1931fa9e4066Sahrens static char *objset_types[DMU_OST_NUMTYPES] = {
1932fa9e4066Sahrens 	"NONE", "META", "ZPL", "ZVOL", "OTHER", "ANY" };
1933fa9e4066Sahrens 
1934fa9e4066Sahrens static void
1935fa9e4066Sahrens dump_dir(objset_t *os)
1936fa9e4066Sahrens {
1937fa9e4066Sahrens 	dmu_objset_stats_t dds;
1938fa9e4066Sahrens 	uint64_t object, object_count;
1939a2eea2e1Sahrens 	uint64_t refdbytes, usedobjs, scratch;
19403f9d6ad7SLin Ling 	char numbuf[32];
194114843421SMatthew Ahrens 	char blkbuf[BP_SPRINTF_LEN + 20];
19429adfa60dSMatthew Ahrens 	char osname[ZFS_MAX_DATASET_NAME_LEN];
1943fa9e4066Sahrens 	char *type = "UNKNOWN";
1944fa9e4066Sahrens 	int verbosity = dump_opt['d'];
1945fa9e4066Sahrens 	int print_header = 1;
1946fa9e4066Sahrens 	int i, error;
1947fa9e4066Sahrens 
19483b2aab18SMatthew Ahrens 	dsl_pool_config_enter(dmu_objset_pool(os), FTAG);
1949a2eea2e1Sahrens 	dmu_objset_fast_stat(os, &dds);
19503b2aab18SMatthew Ahrens 	dsl_pool_config_exit(dmu_objset_pool(os), FTAG);
1951fa9e4066Sahrens 
1952fa9e4066Sahrens 	if (dds.dds_type < DMU_OST_NUMTYPES)
1953fa9e4066Sahrens 		type = objset_types[dds.dds_type];
1954fa9e4066Sahrens 
1955fa9e4066Sahrens 	if (dds.dds_type == DMU_OST_META) {
1956fa9e4066Sahrens 		dds.dds_creation_txg = TXG_INITIAL;
19575d7b4d43SMatthew Ahrens 		usedobjs = BP_GET_FILL(os->os_rootbp);
1958c1379625SJustin T. Gibbs 		refdbytes = dsl_dir_phys(os->os_spa->spa_dsl_pool->dp_mos_dir)->
1959c1379625SJustin T. Gibbs 		    dd_used_bytes;
1960a2eea2e1Sahrens 	} else {
1961a2eea2e1Sahrens 		dmu_objset_space(os, &refdbytes, &scratch, &usedobjs, &scratch);
1962fa9e4066Sahrens 	}
1963fa9e4066Sahrens 
19645d7b4d43SMatthew Ahrens 	ASSERT3U(usedobjs, ==, BP_GET_FILL(os->os_rootbp));
1965fa9e4066Sahrens 
19663f9d6ad7SLin Ling 	zdb_nicenum(refdbytes, numbuf);
1967fa9e4066Sahrens 
1968fa9e4066Sahrens 	if (verbosity >= 4) {
196943466aaeSMax Grossman 		(void) snprintf(blkbuf, sizeof (blkbuf), ", rootbp ");
197043466aaeSMax Grossman 		(void) snprintf_blkptr(blkbuf + strlen(blkbuf),
197143466aaeSMax Grossman 		    sizeof (blkbuf) - strlen(blkbuf), os->os_rootbp);
1972fa9e4066Sahrens 	} else {
1973fa9e4066Sahrens 		blkbuf[0] = '\0';
1974fa9e4066Sahrens 	}
1975fa9e4066Sahrens 
1976fa9e4066Sahrens 	dmu_objset_name(os, osname);
1977fa9e4066Sahrens 
1978a2eea2e1Sahrens 	(void) printf("Dataset %s [%s], ID %llu, cr_txg %llu, "
1979fa9e4066Sahrens 	    "%s, %llu objects%s\n",
1980fa9e4066Sahrens 	    osname, type, (u_longlong_t)dmu_objset_id(os),
1981fa9e4066Sahrens 	    (u_longlong_t)dds.dds_creation_txg,
1982a2eea2e1Sahrens 	    numbuf, (u_longlong_t)usedobjs, blkbuf);
1983fa9e4066Sahrens 
1984b24ab676SJeff Bonwick 	if (zopt_objects != 0) {
1985b24ab676SJeff Bonwick 		for (i = 0; i < zopt_objects; i++)
1986b24ab676SJeff Bonwick 			dump_object(os, zopt_object[i], verbosity,
1987b24ab676SJeff Bonwick 			    &print_header);
1988b24ab676SJeff Bonwick 		(void) printf("\n");
1989b24ab676SJeff Bonwick 		return;
1990b24ab676SJeff Bonwick 	}
1991b24ab676SJeff Bonwick 
1992b24ab676SJeff Bonwick 	if (dump_opt['i'] != 0 || verbosity >= 2)
1993b24ab676SJeff Bonwick 		dump_intent_log(dmu_objset_zil(os));
1994fa9e4066Sahrens 
1995fa9e4066Sahrens 	if (dmu_objset_ds(os) != NULL)
1996cde58dbcSMatthew Ahrens 		dump_deadlist(&dmu_objset_ds(os)->ds_deadlist);
1997fa9e4066Sahrens 
1998fa9e4066Sahrens 	if (verbosity < 2)
1999fa9e4066Sahrens 		return;
2000fa9e4066Sahrens 
200143466aaeSMax Grossman 	if (BP_IS_HOLE(os->os_rootbp))
2002088f3894Sahrens 		return;
2003088f3894Sahrens 
2004fa9e4066Sahrens 	dump_object(os, 0, verbosity, &print_header);
200514843421SMatthew Ahrens 	object_count = 0;
2006744947dcSTom Erickson 	if (DMU_USERUSED_DNODE(os) != NULL &&
2007744947dcSTom Erickson 	    DMU_USERUSED_DNODE(os)->dn_type != 0) {
200814843421SMatthew Ahrens 		dump_object(os, DMU_USERUSED_OBJECT, verbosity, &print_header);
200914843421SMatthew Ahrens 		dump_object(os, DMU_GROUPUSED_OBJECT, verbosity, &print_header);
201014843421SMatthew Ahrens 	}
2011fa9e4066Sahrens 
2012fa9e4066Sahrens 	object = 0;
20136754306eSahrens 	while ((error = dmu_object_next(os, &object, B_FALSE, 0)) == 0) {
2014fa9e4066Sahrens 		dump_object(os, object, verbosity, &print_header);
2015fa9e4066Sahrens 		object_count++;
2016fa9e4066Sahrens 	}
2017fa9e4066Sahrens 
2018a2eea2e1Sahrens 	ASSERT3U(object_count, ==, usedobjs);
2019fa9e4066Sahrens 
2020fa9e4066Sahrens 	(void) printf("\n");
2021fa9e4066Sahrens 
2022ccba0801SRich Morris 	if (error != ESRCH) {
2023ccba0801SRich Morris 		(void) fprintf(stderr, "dmu_object_next() = %d\n", error);
2024ccba0801SRich Morris 		abort();
2025ccba0801SRich Morris 	}
2026fa9e4066Sahrens }
2027fa9e4066Sahrens 
2028fa9e4066Sahrens static void
202953b9a4a9SVictor Latushkin dump_uberblock(uberblock_t *ub, const char *header, const char *footer)
2030fa9e4066Sahrens {
2031fa9e4066Sahrens 	time_t timestamp = ub->ub_timestamp;
2032fa9e4066Sahrens 
203353b9a4a9SVictor Latushkin 	(void) printf(header ? header : "");
2034fa9e4066Sahrens 	(void) printf("\tmagic = %016llx\n", (u_longlong_t)ub->ub_magic);
2035fa9e4066Sahrens 	(void) printf("\tversion = %llu\n", (u_longlong_t)ub->ub_version);
2036fa9e4066Sahrens 	(void) printf("\ttxg = %llu\n", (u_longlong_t)ub->ub_txg);
2037fa9e4066Sahrens 	(void) printf("\tguid_sum = %llu\n", (u_longlong_t)ub->ub_guid_sum);
2038fa9e4066Sahrens 	(void) printf("\ttimestamp = %llu UTC = %s",
2039fa9e4066Sahrens 	    (u_longlong_t)ub->ub_timestamp, asctime(localtime(&timestamp)));
2040fa9e4066Sahrens 	if (dump_opt['u'] >= 3) {
2041fbabab8fSmaybee 		char blkbuf[BP_SPRINTF_LEN];
204243466aaeSMax Grossman 		snprintf_blkptr(blkbuf, sizeof (blkbuf), &ub->ub_rootbp);
2043fa9e4066Sahrens 		(void) printf("\trootbp = %s\n", blkbuf);
2044fa9e4066Sahrens 	}
204553b9a4a9SVictor Latushkin 	(void) printf(footer ? footer : "");
2046fa9e4066Sahrens }
2047fa9e4066Sahrens 
2048fa9e4066Sahrens static void
204907428bdfSVictor Latushkin dump_config(spa_t *spa)
2050fa9e4066Sahrens {
205107428bdfSVictor Latushkin 	dmu_buf_t *db;
205207428bdfSVictor Latushkin 	size_t nvsize = 0;
205307428bdfSVictor Latushkin 	int error = 0;
205407428bdfSVictor Latushkin 
2055fa9e4066Sahrens 
205607428bdfSVictor Latushkin 	error = dmu_bonus_hold(spa->spa_meta_objset,
205707428bdfSVictor Latushkin 	    spa->spa_config_object, FTAG, &db);
205807428bdfSVictor Latushkin 
205907428bdfSVictor Latushkin 	if (error == 0) {
206007428bdfSVictor Latushkin 		nvsize = *(uint64_t *)db->db_data;
206107428bdfSVictor Latushkin 		dmu_buf_rele(db, FTAG);
206207428bdfSVictor Latushkin 
206307428bdfSVictor Latushkin 		(void) printf("\nMOS Configuration:\n");
206407428bdfSVictor Latushkin 		dump_packed_nvlist(spa->spa_meta_objset,
206507428bdfSVictor Latushkin 		    spa->spa_config_object, (void *)&nvsize, 1);
206607428bdfSVictor Latushkin 	} else {
206707428bdfSVictor Latushkin 		(void) fprintf(stderr, "dmu_bonus_hold(%llu) failed, errno %d",
206807428bdfSVictor Latushkin 		    (u_longlong_t)spa->spa_config_object, error);
2069fa9e4066Sahrens 	}
2070fa9e4066Sahrens }
2071fa9e4066Sahrens 
2072c5904d13Seschrock static void
2073c5904d13Seschrock dump_cachefile(const char *cachefile)
2074c5904d13Seschrock {
2075c5904d13Seschrock 	int fd;
2076c5904d13Seschrock 	struct stat64 statbuf;
2077c5904d13Seschrock 	char *buf;
2078c5904d13Seschrock 	nvlist_t *config;
2079c5904d13Seschrock 
2080c5904d13Seschrock 	if ((fd = open64(cachefile, O_RDONLY)) < 0) {
2081c5904d13Seschrock 		(void) printf("cannot open '%s': %s\n", cachefile,
2082c5904d13Seschrock 		    strerror(errno));
2083c5904d13Seschrock 		exit(1);
2084c5904d13Seschrock 	}
2085c5904d13Seschrock 
2086c5904d13Seschrock 	if (fstat64(fd, &statbuf) != 0) {
2087c5904d13Seschrock 		(void) printf("failed to stat '%s': %s\n", cachefile,
2088c5904d13Seschrock 		    strerror(errno));
2089c5904d13Seschrock 		exit(1);
2090c5904d13Seschrock 	}
2091c5904d13Seschrock 
2092c5904d13Seschrock 	if ((buf = malloc(statbuf.st_size)) == NULL) {
2093c5904d13Seschrock 		(void) fprintf(stderr, "failed to allocate %llu bytes\n",
2094c5904d13Seschrock 		    (u_longlong_t)statbuf.st_size);
2095c5904d13Seschrock 		exit(1);
2096c5904d13Seschrock 	}
2097c5904d13Seschrock 
2098c5904d13Seschrock 	if (read(fd, buf, statbuf.st_size) != statbuf.st_size) {
2099c5904d13Seschrock 		(void) fprintf(stderr, "failed to read %llu bytes\n",
2100c5904d13Seschrock 		    (u_longlong_t)statbuf.st_size);
2101c5904d13Seschrock 		exit(1);
2102c5904d13Seschrock 	}
2103c5904d13Seschrock 
2104c5904d13Seschrock 	(void) close(fd);
2105c5904d13Seschrock 
2106c5904d13Seschrock 	if (nvlist_unpack(buf, statbuf.st_size, &config, 0) != 0) {
2107c5904d13Seschrock 		(void) fprintf(stderr, "failed to unpack nvlist\n");
2108c5904d13Seschrock 		exit(1);
2109c5904d13Seschrock 	}
2110c5904d13Seschrock 
2111c5904d13Seschrock 	free(buf);
2112c5904d13Seschrock 
2113c5904d13Seschrock 	dump_nvlist(config, 0);
2114c5904d13Seschrock 
2115c5904d13Seschrock 	nvlist_free(config);
2116c5904d13Seschrock }
2117c5904d13Seschrock 
211853b9a4a9SVictor Latushkin #define	ZDB_MAX_UB_HEADER_SIZE 32
211953b9a4a9SVictor Latushkin 
212053b9a4a9SVictor Latushkin static void
212153b9a4a9SVictor Latushkin dump_label_uberblocks(vdev_label_t *lbl, uint64_t ashift)
212253b9a4a9SVictor Latushkin {
212353b9a4a9SVictor Latushkin 	vdev_t vd;
212453b9a4a9SVictor Latushkin 	vdev_t *vdp = &vd;
212553b9a4a9SVictor Latushkin 	char header[ZDB_MAX_UB_HEADER_SIZE];
212653b9a4a9SVictor Latushkin 
212753b9a4a9SVictor Latushkin 	vd.vdev_ashift = ashift;
212853b9a4a9SVictor Latushkin 	vdp->vdev_top = vdp;
212953b9a4a9SVictor Latushkin 
213053b9a4a9SVictor Latushkin 	for (int i = 0; i < VDEV_UBERBLOCK_COUNT(vdp); i++) {
213153b9a4a9SVictor Latushkin 		uint64_t uoff = VDEV_UBERBLOCK_OFFSET(vdp, i);
213253b9a4a9SVictor Latushkin 		uberblock_t *ub = (void *)((char *)lbl + uoff);
213353b9a4a9SVictor Latushkin 
213453b9a4a9SVictor Latushkin 		if (uberblock_verify(ub))
213553b9a4a9SVictor Latushkin 			continue;
213653b9a4a9SVictor Latushkin 		(void) snprintf(header, ZDB_MAX_UB_HEADER_SIZE,
213753b9a4a9SVictor Latushkin 		    "Uberblock[%d]\n", i);
213853b9a4a9SVictor Latushkin 		dump_uberblock(ub, header, "");
213953b9a4a9SVictor Latushkin 	}
214053b9a4a9SVictor Latushkin }
214153b9a4a9SVictor Latushkin 
2142fa9e4066Sahrens static void
2143fa9e4066Sahrens dump_label(const char *dev)
2144fa9e4066Sahrens {
2145fa9e4066Sahrens 	int fd;
2146fa9e4066Sahrens 	vdev_label_t label;
2147c6065d0fSGeorge Wilson 	char *path, *buf = label.vl_vdev_phys.vp_nvlist;
2148fa9e4066Sahrens 	size_t buflen = sizeof (label.vl_vdev_phys.vp_nvlist);
2149fa9e4066Sahrens 	struct stat64 statbuf;
215053b9a4a9SVictor Latushkin 	uint64_t psize, ashift;
2151c6065d0fSGeorge Wilson 	int len = strlen(dev) + 1;
2152fa9e4066Sahrens 
21536401734dSWill Andrews 	if (strncmp(dev, ZFS_DISK_ROOTD, strlen(ZFS_DISK_ROOTD)) == 0) {
2154c6065d0fSGeorge Wilson 		len++;
2155c6065d0fSGeorge Wilson 		path = malloc(len);
21566401734dSWill Andrews 		(void) snprintf(path, len, "%s%s", ZFS_RDISK_ROOTD,
21576401734dSWill Andrews 		    dev + strlen(ZFS_DISK_ROOTD));
2158c6065d0fSGeorge Wilson 	} else {
2159c6065d0fSGeorge Wilson 		path = strdup(dev);
2160c6065d0fSGeorge Wilson 	}
2161c6065d0fSGeorge Wilson 
2162c6065d0fSGeorge Wilson 	if ((fd = open64(path, O_RDONLY)) < 0) {
2163c6065d0fSGeorge Wilson 		(void) printf("cannot open '%s': %s\n", path, strerror(errno));
2164c6065d0fSGeorge Wilson 		free(path);
2165fa9e4066Sahrens 		exit(1);
2166fa9e4066Sahrens 	}
2167fa9e4066Sahrens 
2168fa9e4066Sahrens 	if (fstat64(fd, &statbuf) != 0) {
2169c6065d0fSGeorge Wilson 		(void) printf("failed to stat '%s': %s\n", path,
2170fa9e4066Sahrens 		    strerror(errno));
2171c6065d0fSGeorge Wilson 		free(path);
2172c6065d0fSGeorge Wilson 		(void) close(fd);
2173c6065d0fSGeorge Wilson 		exit(1);
2174c6065d0fSGeorge Wilson 	}
2175c6065d0fSGeorge Wilson 
2176c6065d0fSGeorge Wilson 	if (S_ISBLK(statbuf.st_mode)) {
2177c6065d0fSGeorge Wilson 		(void) printf("cannot use '%s': character device required\n",
2178c6065d0fSGeorge Wilson 		    path);
2179c6065d0fSGeorge Wilson 		free(path);
2180c6065d0fSGeorge Wilson 		(void) close(fd);
2181c6065d0fSGeorge Wilson 		exit(1);
2182fa9e4066Sahrens 	}
2183fa9e4066Sahrens 
2184fa9e4066Sahrens 	psize = statbuf.st_size;
2185fa9e4066Sahrens 	psize = P2ALIGN(psize, (uint64_t)sizeof (vdev_label_t));
2186fa9e4066Sahrens 
218753b9a4a9SVictor Latushkin 	for (int l = 0; l < VDEV_LABELS; l++) {
2188fa9e4066Sahrens 		nvlist_t *config = NULL;
2189fa9e4066Sahrens 
2190fa9e4066Sahrens 		(void) printf("--------------------------------------------\n");
2191fa9e4066Sahrens 		(void) printf("LABEL %d\n", l);
2192fa9e4066Sahrens 		(void) printf("--------------------------------------------\n");
2193fa9e4066Sahrens 
21940d981225Seschrock 		if (pread64(fd, &label, sizeof (label),
2195fa9e4066Sahrens 		    vdev_label_offset(psize, l, 0)) != sizeof (label)) {
2196fa9e4066Sahrens 			(void) printf("failed to read label %d\n", l);
2197fa9e4066Sahrens 			continue;
2198fa9e4066Sahrens 		}
2199fa9e4066Sahrens 
2200fa9e4066Sahrens 		if (nvlist_unpack(buf, buflen, &config, 0) != 0) {
2201fa9e4066Sahrens 			(void) printf("failed to unpack label %d\n", l);
220253b9a4a9SVictor Latushkin 			ashift = SPA_MINBLOCKSHIFT;
220353b9a4a9SVictor Latushkin 		} else {
220453b9a4a9SVictor Latushkin 			nvlist_t *vdev_tree = NULL;
220553b9a4a9SVictor Latushkin 
220653b9a4a9SVictor Latushkin 			dump_nvlist(config, 4);
220753b9a4a9SVictor Latushkin 			if ((nvlist_lookup_nvlist(config,
220853b9a4a9SVictor Latushkin 			    ZPOOL_CONFIG_VDEV_TREE, &vdev_tree) != 0) ||
220953b9a4a9SVictor Latushkin 			    (nvlist_lookup_uint64(vdev_tree,
221053b9a4a9SVictor Latushkin 			    ZPOOL_CONFIG_ASHIFT, &ashift) != 0))
221153b9a4a9SVictor Latushkin 				ashift = SPA_MINBLOCKSHIFT;
221253b9a4a9SVictor Latushkin 			nvlist_free(config);
2213fa9e4066Sahrens 		}
221453b9a4a9SVictor Latushkin 		if (dump_opt['u'])
221553b9a4a9SVictor Latushkin 			dump_label_uberblocks(&label, ashift);
2216fa9e4066Sahrens 	}
2217c6065d0fSGeorge Wilson 
2218c6065d0fSGeorge Wilson 	free(path);
2219c6065d0fSGeorge Wilson 	(void) close(fd);
2220fa9e4066Sahrens }
2221fa9e4066Sahrens 
2222ca0cc391SMatthew Ahrens static uint64_t dataset_feature_count[SPA_FEATURES];
2223b5152584SMatthew Ahrens 
2224fa9e4066Sahrens /*ARGSUSED*/
22251d452cf5Sahrens static int
2226fd136879SMatthew Ahrens dump_one_dir(const char *dsname, void *arg)
2227fa9e4066Sahrens {
2228fa9e4066Sahrens 	int error;
2229fa9e4066Sahrens 	objset_t *os;
2230fa9e4066Sahrens 
2231503ad85cSMatthew Ahrens 	error = dmu_objset_own(dsname, DMU_OST_ANY, B_TRUE, FTAG, &os);
2232fa9e4066Sahrens 	if (error) {
2233b24ab676SJeff Bonwick 		(void) printf("Could not open %s, error %d\n", dsname, error);
22341d452cf5Sahrens 		return (0);
2235fa9e4066Sahrens 	}
2236ca0cc391SMatthew Ahrens 
2237ca0cc391SMatthew Ahrens 	for (spa_feature_t f = 0; f < SPA_FEATURES; f++) {
2238ca0cc391SMatthew Ahrens 		if (!dmu_objset_ds(os)->ds_feature_inuse[f])
2239ca0cc391SMatthew Ahrens 			continue;
2240ca0cc391SMatthew Ahrens 		ASSERT(spa_feature_table[f].fi_flags &
2241ca0cc391SMatthew Ahrens 		    ZFEATURE_FLAG_PER_DATASET);
2242ca0cc391SMatthew Ahrens 		dataset_feature_count[f]++;
2243ca0cc391SMatthew Ahrens 	}
2244ca0cc391SMatthew Ahrens 
2245fa9e4066Sahrens 	dump_dir(os);
2246503ad85cSMatthew Ahrens 	dmu_objset_disown(os, FTAG);
2247e0d35c44Smarks 	fuid_table_destroy();
22480a586ceaSMark Shellenbaum 	sa_loaded = B_FALSE;
22491d452cf5Sahrens 	return (0);
2250fa9e4066Sahrens }
2251fa9e4066Sahrens 
2252fa9e4066Sahrens /*
2253b24ab676SJeff Bonwick  * Block statistics.
2254fa9e4066Sahrens  */
2255b5152584SMatthew Ahrens #define	PSIZE_HISTO_SIZE (SPA_OLD_MAXBLOCKSIZE / SPA_MINBLOCKSIZE + 2)
2256fa9e4066Sahrens typedef struct zdb_blkstats {
2257490d05b9SMatthew Ahrens 	uint64_t zb_asize;
2258490d05b9SMatthew Ahrens 	uint64_t zb_lsize;
2259490d05b9SMatthew Ahrens 	uint64_t zb_psize;
2260490d05b9SMatthew Ahrens 	uint64_t zb_count;
2261d5ee8a13SMatthew Ahrens 	uint64_t zb_gangs;
2262d5ee8a13SMatthew Ahrens 	uint64_t zb_ditto_samevdev;
2263490d05b9SMatthew Ahrens 	uint64_t zb_psize_histogram[PSIZE_HISTO_SIZE];
2264fa9e4066Sahrens } zdb_blkstats_t;
2265fa9e4066Sahrens 
2266b24ab676SJeff Bonwick /*
2267b24ab676SJeff Bonwick  * Extended object types to report deferred frees and dedup auto-ditto blocks.
2268b24ab676SJeff Bonwick  */
2269b24ab676SJeff Bonwick #define	ZDB_OT_DEFERRED	(DMU_OT_NUMTYPES + 0)
2270b24ab676SJeff Bonwick #define	ZDB_OT_DITTO	(DMU_OT_NUMTYPES + 1)
2271ad135b5dSChristopher Siden #define	ZDB_OT_OTHER	(DMU_OT_NUMTYPES + 2)
2272ad135b5dSChristopher Siden #define	ZDB_OT_TOTAL	(DMU_OT_NUMTYPES + 3)
2273b24ab676SJeff Bonwick 
2274b24ab676SJeff Bonwick static char *zdb_ot_extname[] = {
2275b24ab676SJeff Bonwick 	"deferred free",
2276b24ab676SJeff Bonwick 	"dedup ditto",
2277ad135b5dSChristopher Siden 	"other",
2278b24ab676SJeff Bonwick 	"Total",
2279b24ab676SJeff Bonwick };
2280fa9e4066Sahrens 
228188b7b0f2SMatthew Ahrens #define	ZB_TOTAL	DN_MAX_LEVELS
2282fa9e4066Sahrens 
2283fa9e4066Sahrens typedef struct zdb_cb {
2284b24ab676SJeff Bonwick 	zdb_blkstats_t	zcb_type[ZB_TOTAL + 1][ZDB_OT_TOTAL + 1];
2285b24ab676SJeff Bonwick 	uint64_t	zcb_dedup_asize;
2286b24ab676SJeff Bonwick 	uint64_t	zcb_dedup_blocks;
22875d7b4d43SMatthew Ahrens 	uint64_t	zcb_embedded_blocks[NUM_BP_EMBEDDED_TYPES];
22885d7b4d43SMatthew Ahrens 	uint64_t	zcb_embedded_histogram[NUM_BP_EMBEDDED_TYPES]
22895d7b4d43SMatthew Ahrens 	    [BPE_PAYLOAD_SIZE];
2290490d05b9SMatthew Ahrens 	uint64_t	zcb_start;
2291490d05b9SMatthew Ahrens 	uint64_t	zcb_lastprint;
2292490d05b9SMatthew Ahrens 	uint64_t	zcb_totalasize;
2293fa9e4066Sahrens 	uint64_t	zcb_errors[256];
2294fa9e4066Sahrens 	int		zcb_readfails;
2295fa9e4066Sahrens 	int		zcb_haderrors;
2296cde58dbcSMatthew Ahrens 	spa_t		*zcb_spa;
2297fa9e4066Sahrens } zdb_cb_t;
2298fa9e4066Sahrens 
2299fa9e4066Sahrens static void
2300cde58dbcSMatthew Ahrens zdb_count_block(zdb_cb_t *zcb, zilog_t *zilog, const blkptr_t *bp,
2301b24ab676SJeff Bonwick     dmu_object_type_t type)
2302fa9e4066Sahrens {
2303b24ab676SJeff Bonwick 	uint64_t refcnt = 0;
2304b24ab676SJeff Bonwick 
2305b24ab676SJeff Bonwick 	ASSERT(type < ZDB_OT_TOTAL);
2306b24ab676SJeff Bonwick 
2307b24ab676SJeff Bonwick 	if (zilog && zil_bp_tree_add(zilog, bp) != 0)
2308b24ab676SJeff Bonwick 		return;
2309b24ab676SJeff Bonwick 
2310e14bb325SJeff Bonwick 	for (int i = 0; i < 4; i++) {
2311fa9e4066Sahrens 		int l = (i < 2) ? BP_GET_LEVEL(bp) : ZB_TOTAL;
2312b24ab676SJeff Bonwick 		int t = (i & 1) ? type : ZDB_OT_TOTAL;
2313d5ee8a13SMatthew Ahrens 		int equal;
2314fa9e4066Sahrens 		zdb_blkstats_t *zb = &zcb->zcb_type[l][t];
2315fa9e4066Sahrens 
2316fa9e4066Sahrens 		zb->zb_asize += BP_GET_ASIZE(bp);
2317fa9e4066Sahrens 		zb->zb_lsize += BP_GET_LSIZE(bp);
2318fa9e4066Sahrens 		zb->zb_psize += BP_GET_PSIZE(bp);
2319fa9e4066Sahrens 		zb->zb_count++;
2320b5152584SMatthew Ahrens 
2321b5152584SMatthew Ahrens 		/*
2322b5152584SMatthew Ahrens 		 * The histogram is only big enough to record blocks up to
2323b5152584SMatthew Ahrens 		 * SPA_OLD_MAXBLOCKSIZE; larger blocks go into the last,
2324b5152584SMatthew Ahrens 		 * "other", bucket.
2325b5152584SMatthew Ahrens 		 */
2326b5152584SMatthew Ahrens 		int idx = BP_GET_PSIZE(bp) >> SPA_MINBLOCKSHIFT;
2327b5152584SMatthew Ahrens 		idx = MIN(idx, SPA_OLD_MAXBLOCKSIZE / SPA_MINBLOCKSIZE + 1);
2328b5152584SMatthew Ahrens 		zb->zb_psize_histogram[idx]++;
2329d5ee8a13SMatthew Ahrens 
2330d5ee8a13SMatthew Ahrens 		zb->zb_gangs += BP_COUNT_GANG(bp);
2331d5ee8a13SMatthew Ahrens 
2332d5ee8a13SMatthew Ahrens 		switch (BP_GET_NDVAS(bp)) {
2333d5ee8a13SMatthew Ahrens 		case 2:
2334d5ee8a13SMatthew Ahrens 			if (DVA_GET_VDEV(&bp->blk_dva[0]) ==
2335d5ee8a13SMatthew Ahrens 			    DVA_GET_VDEV(&bp->blk_dva[1]))
2336d5ee8a13SMatthew Ahrens 				zb->zb_ditto_samevdev++;
2337d5ee8a13SMatthew Ahrens 			break;
2338d5ee8a13SMatthew Ahrens 		case 3:
2339d5ee8a13SMatthew Ahrens 			equal = (DVA_GET_VDEV(&bp->blk_dva[0]) ==
2340d5ee8a13SMatthew Ahrens 			    DVA_GET_VDEV(&bp->blk_dva[1])) +
2341d5ee8a13SMatthew Ahrens 			    (DVA_GET_VDEV(&bp->blk_dva[0]) ==
2342d5ee8a13SMatthew Ahrens 			    DVA_GET_VDEV(&bp->blk_dva[2])) +
2343d5ee8a13SMatthew Ahrens 			    (DVA_GET_VDEV(&bp->blk_dva[1]) ==
2344d5ee8a13SMatthew Ahrens 			    DVA_GET_VDEV(&bp->blk_dva[2]));
2345d5ee8a13SMatthew Ahrens 			if (equal != 0)
2346d5ee8a13SMatthew Ahrens 				zb->zb_ditto_samevdev++;
2347d5ee8a13SMatthew Ahrens 			break;
2348d5ee8a13SMatthew Ahrens 		}
2349d5ee8a13SMatthew Ahrens 
2350fa9e4066Sahrens 	}
2351fa9e4066Sahrens 
23525d7b4d43SMatthew Ahrens 	if (BP_IS_EMBEDDED(bp)) {
23535d7b4d43SMatthew Ahrens 		zcb->zcb_embedded_blocks[BPE_GET_ETYPE(bp)]++;
23545d7b4d43SMatthew Ahrens 		zcb->zcb_embedded_histogram[BPE_GET_ETYPE(bp)]
23555d7b4d43SMatthew Ahrens 		    [BPE_GET_PSIZE(bp)]++;
23565d7b4d43SMatthew Ahrens 		return;
23575d7b4d43SMatthew Ahrens 	}
23585d7b4d43SMatthew Ahrens 
2359b24ab676SJeff Bonwick 	if (dump_opt['L'])
2360b24ab676SJeff Bonwick 		return;
2361b24ab676SJeff Bonwick 
2362b24ab676SJeff Bonwick 	if (BP_GET_DEDUP(bp)) {
2363b24ab676SJeff Bonwick 		ddt_t *ddt;
2364b24ab676SJeff Bonwick 		ddt_entry_t *dde;
2365b24ab676SJeff Bonwick 
2366cde58dbcSMatthew Ahrens 		ddt = ddt_select(zcb->zcb_spa, bp);
2367b24ab676SJeff Bonwick 		ddt_enter(ddt);
2368b24ab676SJeff Bonwick 		dde = ddt_lookup(ddt, bp, B_FALSE);
2369b24ab676SJeff Bonwick 
2370b24ab676SJeff Bonwick 		if (dde == NULL) {
2371b24ab676SJeff Bonwick 			refcnt = 0;
2372b24ab676SJeff Bonwick 		} else {
2373b24ab676SJeff Bonwick 			ddt_phys_t *ddp = ddt_phys_select(dde, bp);
2374b24ab676SJeff Bonwick 			ddt_phys_decref(ddp);
2375b24ab676SJeff Bonwick 			refcnt = ddp->ddp_refcnt;
2376b24ab676SJeff Bonwick 			if (ddt_phys_total_refcnt(dde) == 0)
2377b24ab676SJeff Bonwick 				ddt_remove(ddt, dde);
2378d41e7643Sek 		}
2379b24ab676SJeff Bonwick 		ddt_exit(ddt);
2380d41e7643Sek 	}
2381d41e7643Sek 
2382cde58dbcSMatthew Ahrens 	VERIFY3U(zio_wait(zio_claim(NULL, zcb->zcb_spa,
2383cde58dbcSMatthew Ahrens 	    refcnt ? 0 : spa_first_txg(zcb->zcb_spa),
2384b24ab676SJeff Bonwick 	    bp, NULL, NULL, ZIO_FLAG_CANFAIL)), ==, 0);
2385fa9e4066Sahrens }
2386fa9e4066Sahrens 
238731d7e8faSGeorge Wilson static void
238831d7e8faSGeorge Wilson zdb_blkptr_done(zio_t *zio)
238931d7e8faSGeorge Wilson {
239031d7e8faSGeorge Wilson 	spa_t *spa = zio->io_spa;
239131d7e8faSGeorge Wilson 	blkptr_t *bp = zio->io_bp;
239231d7e8faSGeorge Wilson 	int ioerr = zio->io_error;
239331d7e8faSGeorge Wilson 	zdb_cb_t *zcb = zio->io_private;
23947802d7bfSMatthew Ahrens 	zbookmark_phys_t *zb = &zio->io_bookmark;
239531d7e8faSGeorge Wilson 
239631d7e8faSGeorge Wilson 	zio_data_buf_free(zio->io_data, zio->io_size);
239731d7e8faSGeorge Wilson 
239831d7e8faSGeorge Wilson 	mutex_enter(&spa->spa_scrub_lock);
239931d7e8faSGeorge Wilson 	spa->spa_scrub_inflight--;
240031d7e8faSGeorge Wilson 	cv_broadcast(&spa->spa_scrub_io_cv);
240131d7e8faSGeorge Wilson 
240231d7e8faSGeorge Wilson 	if (ioerr && !(zio->io_flags & ZIO_FLAG_SPECULATIVE)) {
240331d7e8faSGeorge Wilson 		char blkbuf[BP_SPRINTF_LEN];
240431d7e8faSGeorge Wilson 
240531d7e8faSGeorge Wilson 		zcb->zcb_haderrors = 1;
240631d7e8faSGeorge Wilson 		zcb->zcb_errors[ioerr]++;
240731d7e8faSGeorge Wilson 
240831d7e8faSGeorge Wilson 		if (dump_opt['b'] >= 2)
240943466aaeSMax Grossman 			snprintf_blkptr(blkbuf, sizeof (blkbuf), bp);
241031d7e8faSGeorge Wilson 		else
241131d7e8faSGeorge Wilson 			blkbuf[0] = '\0';
241231d7e8faSGeorge Wilson 
241331d7e8faSGeorge Wilson 		(void) printf("zdb_blkptr_cb: "
241431d7e8faSGeorge Wilson 		    "Got error %d reading "
241531d7e8faSGeorge Wilson 		    "<%llu, %llu, %lld, %llx> %s -- skipping\n",
241631d7e8faSGeorge Wilson 		    ioerr,
241731d7e8faSGeorge Wilson 		    (u_longlong_t)zb->zb_objset,
241831d7e8faSGeorge Wilson 		    (u_longlong_t)zb->zb_object,
241931d7e8faSGeorge Wilson 		    (u_longlong_t)zb->zb_level,
242031d7e8faSGeorge Wilson 		    (u_longlong_t)zb->zb_blkid,
242131d7e8faSGeorge Wilson 		    blkbuf);
242231d7e8faSGeorge Wilson 	}
242331d7e8faSGeorge Wilson 	mutex_exit(&spa->spa_scrub_lock);
242431d7e8faSGeorge Wilson }
242531d7e8faSGeorge Wilson 
2426fa9e4066Sahrens static int
24271b912ec7SGeorge Wilson zdb_blkptr_cb(spa_t *spa, zilog_t *zilog, const blkptr_t *bp,
24287802d7bfSMatthew Ahrens     const zbookmark_phys_t *zb, const dnode_phys_t *dnp, void *arg)
2429fa9e4066Sahrens {
2430fa9e4066Sahrens 	zdb_cb_t *zcb = arg;
24316365109dSVictor Latushkin 	dmu_object_type_t type;
2432468c413aSTim Haley 	boolean_t is_metadata;
2433fa9e4066Sahrens 
2434a2cdcdd2SPaul Dagnelie 	if (bp == NULL)
2435a2cdcdd2SPaul Dagnelie 		return (0);
2436a2cdcdd2SPaul Dagnelie 
243743466aaeSMax Grossman 	if (dump_opt['b'] >= 5 && bp->blk_birth > 0) {
243843466aaeSMax Grossman 		char blkbuf[BP_SPRINTF_LEN];
243943466aaeSMax Grossman 		snprintf_blkptr(blkbuf, sizeof (blkbuf), bp);
244043466aaeSMax Grossman 		(void) printf("objset %llu object %llu "
244143466aaeSMax Grossman 		    "level %lld offset 0x%llx %s\n",
244243466aaeSMax Grossman 		    (u_longlong_t)zb->zb_objset,
244343466aaeSMax Grossman 		    (u_longlong_t)zb->zb_object,
244443466aaeSMax Grossman 		    (longlong_t)zb->zb_level,
244543466aaeSMax Grossman 		    (u_longlong_t)blkid2offset(dnp, bp, zb),
244643466aaeSMax Grossman 		    blkbuf);
244743466aaeSMax Grossman 	}
244843466aaeSMax Grossman 
244943466aaeSMax Grossman 	if (BP_IS_HOLE(bp))
245088b7b0f2SMatthew Ahrens 		return (0);
2451e14bb325SJeff Bonwick 
24526365109dSVictor Latushkin 	type = BP_GET_TYPE(bp);
2453e14bb325SJeff Bonwick 
2454ad135b5dSChristopher Siden 	zdb_count_block(zcb, zilog, bp,
2455ad135b5dSChristopher Siden 	    (type & DMU_OT_NEWTYPE) ? ZDB_OT_OTHER : type);
24566365109dSVictor Latushkin 
2457ad135b5dSChristopher Siden 	is_metadata = (BP_GET_LEVEL(bp) != 0 || DMU_OT_IS_METADATA(type));
24586365109dSVictor Latushkin 
24595d7b4d43SMatthew Ahrens 	if (!BP_IS_EMBEDDED(bp) &&
24605d7b4d43SMatthew Ahrens 	    (dump_opt['c'] > 1 || (dump_opt['c'] && is_metadata))) {
2461468c413aSTim Haley 		size_t size = BP_GET_PSIZE(bp);
246231d7e8faSGeorge Wilson 		void *data = zio_data_buf_alloc(size);
2463b24ab676SJeff Bonwick 		int flags = ZIO_FLAG_CANFAIL | ZIO_FLAG_SCRUB | ZIO_FLAG_RAW;
2464b24ab676SJeff Bonwick 
2465b24ab676SJeff Bonwick 		/* If it's an intent log block, failure is expected. */
2466b24ab676SJeff Bonwick 		if (zb->zb_level == ZB_ZIL_LEVEL)
2467b24ab676SJeff Bonwick 			flags |= ZIO_FLAG_SPECULATIVE;
2468b24ab676SJeff Bonwick 
246931d7e8faSGeorge Wilson 		mutex_enter(&spa->spa_scrub_lock);
247031d7e8faSGeorge Wilson 		while (spa->spa_scrub_inflight > max_inflight)
247131d7e8faSGeorge Wilson 			cv_wait(&spa->spa_scrub_io_cv, &spa->spa_scrub_lock);
247231d7e8faSGeorge Wilson 		spa->spa_scrub_inflight++;
247331d7e8faSGeorge Wilson 		mutex_exit(&spa->spa_scrub_lock);
2474b24ab676SJeff Bonwick 
247531d7e8faSGeorge Wilson 		zio_nowait(zio_read(NULL, spa, bp, data, size,
247631d7e8faSGeorge Wilson 		    zdb_blkptr_done, zcb, ZIO_PRIORITY_ASYNC_READ, flags, zb));
2477fa9e4066Sahrens 	}
2478fa9e4066Sahrens 
2479fa9e4066Sahrens 	zcb->zcb_readfails = 0;
2480fa9e4066Sahrens 
2481f7950bf1SMatthew Ahrens 	/* only call gethrtime() every 100 blocks */
2482f7950bf1SMatthew Ahrens 	static int iters;
2483f7950bf1SMatthew Ahrens 	if (++iters > 100)
2484f7950bf1SMatthew Ahrens 		iters = 0;
2485f7950bf1SMatthew Ahrens 	else
2486f7950bf1SMatthew Ahrens 		return (0);
2487f7950bf1SMatthew Ahrens 
2488f7950bf1SMatthew Ahrens 	if (dump_opt['b'] < 5 && gethrtime() > zcb->zcb_lastprint + NANOSEC) {
2489490d05b9SMatthew Ahrens 		uint64_t now = gethrtime();
2490490d05b9SMatthew Ahrens 		char buf[10];
2491490d05b9SMatthew Ahrens 		uint64_t bytes = zcb->zcb_type[ZB_TOTAL][ZDB_OT_TOTAL].zb_asize;
2492490d05b9SMatthew Ahrens 		int kb_per_sec =
2493490d05b9SMatthew Ahrens 		    1 + bytes / (1 + ((now - zcb->zcb_start) / 1000 / 1000));
2494490d05b9SMatthew Ahrens 		int sec_remaining =
2495490d05b9SMatthew Ahrens 		    (zcb->zcb_totalasize - bytes) / 1024 / kb_per_sec;
2496490d05b9SMatthew Ahrens 
2497490d05b9SMatthew Ahrens 		zfs_nicenum(bytes, buf, sizeof (buf));
2498490d05b9SMatthew Ahrens 		(void) fprintf(stderr,
2499490d05b9SMatthew Ahrens 		    "\r%5s completed (%4dMB/s) "
2500490d05b9SMatthew Ahrens 		    "estimated time remaining: %uhr %02umin %02usec        ",
2501490d05b9SMatthew Ahrens 		    buf, kb_per_sec / 1024,
2502490d05b9SMatthew Ahrens 		    sec_remaining / 60 / 60,
2503490d05b9SMatthew Ahrens 		    sec_remaining / 60 % 60,
2504490d05b9SMatthew Ahrens 		    sec_remaining % 60);
2505490d05b9SMatthew Ahrens 
2506490d05b9SMatthew Ahrens 		zcb->zcb_lastprint = now;
2507490d05b9SMatthew Ahrens 	}
2508490d05b9SMatthew Ahrens 
2509fa9e4066Sahrens 	return (0);
2510fa9e4066Sahrens }
2511fa9e4066Sahrens 
2512b24ab676SJeff Bonwick static void
25130713e232SGeorge Wilson zdb_leak(void *arg, uint64_t start, uint64_t size)
2514b24ab676SJeff Bonwick {
25150713e232SGeorge Wilson 	vdev_t *vd = arg;
2516b24ab676SJeff Bonwick 
2517b24ab676SJeff Bonwick 	(void) printf("leaked space: vdev %llu, offset 0x%llx, size %llu\n",
2518b24ab676SJeff Bonwick 	    (u_longlong_t)vd->vdev_id, (u_longlong_t)start, (u_longlong_t)size);
2519b24ab676SJeff Bonwick }
2520b24ab676SJeff Bonwick 
25210713e232SGeorge Wilson static metaslab_ops_t zdb_metaslab_ops = {
25222e4c9986SGeorge Wilson 	NULL	/* alloc */
2523b24ab676SJeff Bonwick };
2524b24ab676SJeff Bonwick 
2525b24ab676SJeff Bonwick static void
2526bbfd46c4SJeff Bonwick zdb_ddt_leak_init(spa_t *spa, zdb_cb_t *zcb)
2527b24ab676SJeff Bonwick {
2528bbfd46c4SJeff Bonwick 	ddt_bookmark_t ddb = { 0 };
2529b24ab676SJeff Bonwick 	ddt_entry_t dde;
2530b24ab676SJeff Bonwick 	int error;
2531b24ab676SJeff Bonwick 
2532bbfd46c4SJeff Bonwick 	while ((error = ddt_walk(spa, &ddb, &dde)) == 0) {
2533b24ab676SJeff Bonwick 		blkptr_t blk;
2534b24ab676SJeff Bonwick 		ddt_phys_t *ddp = dde.dde_phys;
2535bbfd46c4SJeff Bonwick 
2536bbfd46c4SJeff Bonwick 		if (ddb.ddb_class == DDT_CLASS_UNIQUE)
2537bbfd46c4SJeff Bonwick 			return;
2538bbfd46c4SJeff Bonwick 
2539b24ab676SJeff Bonwick 		ASSERT(ddt_phys_total_refcnt(&dde) > 1);
2540bbfd46c4SJeff Bonwick 
2541b24ab676SJeff Bonwick 		for (int p = 0; p < DDT_PHYS_TYPES; p++, ddp++) {
2542b24ab676SJeff Bonwick 			if (ddp->ddp_phys_birth == 0)
2543b24ab676SJeff Bonwick 				continue;
2544bbfd46c4SJeff Bonwick 			ddt_bp_create(ddb.ddb_checksum,
2545bbfd46c4SJeff Bonwick 			    &dde.dde_key, ddp, &blk);
2546b24ab676SJeff Bonwick 			if (p == DDT_PHYS_DITTO) {
2547cde58dbcSMatthew Ahrens 				zdb_count_block(zcb, NULL, &blk, ZDB_OT_DITTO);
2548b24ab676SJeff Bonwick 			} else {
2549b24ab676SJeff Bonwick 				zcb->zcb_dedup_asize +=
2550b24ab676SJeff Bonwick 				    BP_GET_ASIZE(&blk) * (ddp->ddp_refcnt - 1);
2551b24ab676SJeff Bonwick 				zcb->zcb_dedup_blocks++;
2552b24ab676SJeff Bonwick 			}
2553b24ab676SJeff Bonwick 		}
2554b24ab676SJeff Bonwick 		if (!dump_opt['L']) {
2555bbfd46c4SJeff Bonwick 			ddt_t *ddt = spa->spa_ddt[ddb.ddb_checksum];
2556b24ab676SJeff Bonwick 			ddt_enter(ddt);
2557b24ab676SJeff Bonwick 			VERIFY(ddt_lookup(ddt, &blk, B_TRUE) != NULL);
2558b24ab676SJeff Bonwick 			ddt_exit(ddt);
2559b24ab676SJeff Bonwick 		}
2560b24ab676SJeff Bonwick 	}
2561b24ab676SJeff Bonwick 
2562b24ab676SJeff Bonwick 	ASSERT(error == ENOENT);
2563b24ab676SJeff Bonwick }
2564b24ab676SJeff Bonwick 
2565b24ab676SJeff Bonwick static void
2566b24ab676SJeff Bonwick zdb_leak_init(spa_t *spa, zdb_cb_t *zcb)
2567b24ab676SJeff Bonwick {
2568cde58dbcSMatthew Ahrens 	zcb->zcb_spa = spa;
2569cde58dbcSMatthew Ahrens 
2570b24ab676SJeff Bonwick 	if (!dump_opt['L']) {
2571b24ab676SJeff Bonwick 		vdev_t *rvd = spa->spa_root_vdev;
257206be9802SMatthew Ahrens 		for (uint64_t c = 0; c < rvd->vdev_children; c++) {
2573b24ab676SJeff Bonwick 			vdev_t *vd = rvd->vdev_child[c];
257406be9802SMatthew Ahrens 			for (uint64_t m = 0; m < vd->vdev_ms_count; m++) {
2575b24ab676SJeff Bonwick 				metaslab_t *msp = vd->vdev_ms[m];
2576b24ab676SJeff Bonwick 				mutex_enter(&msp->ms_lock);
25770713e232SGeorge Wilson 				metaslab_unload(msp);
25780713e232SGeorge Wilson 
25790713e232SGeorge Wilson 				/*
25800713e232SGeorge Wilson 				 * For leak detection, we overload the metaslab
25810713e232SGeorge Wilson 				 * ms_tree to contain allocated segments
25820713e232SGeorge Wilson 				 * instead of free segments. As a result,
25830713e232SGeorge Wilson 				 * we can't use the normal metaslab_load/unload
25840713e232SGeorge Wilson 				 * interfaces.
25850713e232SGeorge Wilson 				 */
25860713e232SGeorge Wilson 				if (msp->ms_sm != NULL) {
258706be9802SMatthew Ahrens 					(void) fprintf(stderr,
258806be9802SMatthew Ahrens 					    "\rloading space map for "
258906be9802SMatthew Ahrens 					    "vdev %llu of %llu, "
259006be9802SMatthew Ahrens 					    "metaslab %llu of %llu ...",
259106be9802SMatthew Ahrens 					    (longlong_t)c,
259206be9802SMatthew Ahrens 					    (longlong_t)rvd->vdev_children,
259306be9802SMatthew Ahrens 					    (longlong_t)m,
259406be9802SMatthew Ahrens 					    (longlong_t)vd->vdev_ms_count);
259506be9802SMatthew Ahrens 
25960713e232SGeorge Wilson 					msp->ms_ops = &zdb_metaslab_ops;
2597f7950bf1SMatthew Ahrens 
2598f7950bf1SMatthew Ahrens 					/*
2599f7950bf1SMatthew Ahrens 					 * We don't want to spend the CPU
2600f7950bf1SMatthew Ahrens 					 * manipulating the size-ordered
2601f7950bf1SMatthew Ahrens 					 * tree, so clear the range_tree
2602f7950bf1SMatthew Ahrens 					 * ops.
2603f7950bf1SMatthew Ahrens 					 */
2604f7950bf1SMatthew Ahrens 					msp->ms_tree->rt_ops = NULL;
26050713e232SGeorge Wilson 					VERIFY0(space_map_load(msp->ms_sm,
26060713e232SGeorge Wilson 					    msp->ms_tree, SM_ALLOC));
26070713e232SGeorge Wilson 					msp->ms_loaded = B_TRUE;
26080713e232SGeorge Wilson 				}
2609b24ab676SJeff Bonwick 				mutex_exit(&msp->ms_lock);
2610b24ab676SJeff Bonwick 			}
2611b24ab676SJeff Bonwick 		}
261206be9802SMatthew Ahrens 		(void) fprintf(stderr, "\n");
2613b24ab676SJeff Bonwick 	}
2614b24ab676SJeff Bonwick 
2615b24ab676SJeff Bonwick 	spa_config_enter(spa, SCL_CONFIG, FTAG, RW_READER);
2616b24ab676SJeff Bonwick 
2617bbfd46c4SJeff Bonwick 	zdb_ddt_leak_init(spa, zcb);
2618b24ab676SJeff Bonwick 
2619b24ab676SJeff Bonwick 	spa_config_exit(spa, SCL_CONFIG, FTAG);
2620b24ab676SJeff Bonwick }
2621b24ab676SJeff Bonwick 
2622b24ab676SJeff Bonwick static void
2623b24ab676SJeff Bonwick zdb_leak_fini(spa_t *spa)
2624b24ab676SJeff Bonwick {
2625b24ab676SJeff Bonwick 	if (!dump_opt['L']) {
2626b24ab676SJeff Bonwick 		vdev_t *rvd = spa->spa_root_vdev;
2627b24ab676SJeff Bonwick 		for (int c = 0; c < rvd->vdev_children; c++) {
2628b24ab676SJeff Bonwick 			vdev_t *vd = rvd->vdev_child[c];
2629b24ab676SJeff Bonwick 			for (int m = 0; m < vd->vdev_ms_count; m++) {
2630b24ab676SJeff Bonwick 				metaslab_t *msp = vd->vdev_ms[m];
2631b24ab676SJeff Bonwick 				mutex_enter(&msp->ms_lock);
26320713e232SGeorge Wilson 
26330713e232SGeorge Wilson 				/*
26340713e232SGeorge Wilson 				 * The ms_tree has been overloaded to
26350713e232SGeorge Wilson 				 * contain allocated segments. Now that we
26360713e232SGeorge Wilson 				 * finished traversing all blocks, any
26370713e232SGeorge Wilson 				 * block that remains in the ms_tree
26380713e232SGeorge Wilson 				 * represents an allocated block that we
26390713e232SGeorge Wilson 				 * did not claim during the traversal.
26400713e232SGeorge Wilson 				 * Claimed blocks would have been removed
26410713e232SGeorge Wilson 				 * from the ms_tree.
26420713e232SGeorge Wilson 				 */
26430713e232SGeorge Wilson 				range_tree_vacate(msp->ms_tree, zdb_leak, vd);
26440713e232SGeorge Wilson 				msp->ms_loaded = B_FALSE;
26450713e232SGeorge Wilson 
2646b24ab676SJeff Bonwick 				mutex_exit(&msp->ms_lock);
2647b24ab676SJeff Bonwick 			}
2648b24ab676SJeff Bonwick 		}
2649b24ab676SJeff Bonwick 	}
2650b24ab676SJeff Bonwick }
2651b24ab676SJeff Bonwick 
2652cde58dbcSMatthew Ahrens /* ARGSUSED */
2653cde58dbcSMatthew Ahrens static int
2654cde58dbcSMatthew Ahrens count_block_cb(void *arg, const blkptr_t *bp, dmu_tx_t *tx)
2655cde58dbcSMatthew Ahrens {
2656cde58dbcSMatthew Ahrens 	zdb_cb_t *zcb = arg;
2657cde58dbcSMatthew Ahrens 
2658490d05b9SMatthew Ahrens 	if (dump_opt['b'] >= 5) {
2659cde58dbcSMatthew Ahrens 		char blkbuf[BP_SPRINTF_LEN];
266043466aaeSMax Grossman 		snprintf_blkptr(blkbuf, sizeof (blkbuf), bp);
2661cde58dbcSMatthew Ahrens 		(void) printf("[%s] %s\n",
2662cde58dbcSMatthew Ahrens 		    "deferred free", blkbuf);
2663cde58dbcSMatthew Ahrens 	}
2664cde58dbcSMatthew Ahrens 	zdb_count_block(zcb, NULL, bp, ZDB_OT_DEFERRED);
2665cde58dbcSMatthew Ahrens 	return (0);
2666cde58dbcSMatthew Ahrens }
2667cde58dbcSMatthew Ahrens 
2668fa9e4066Sahrens static int
2669fa9e4066Sahrens dump_block_stats(spa_t *spa)
2670fa9e4066Sahrens {
2671fa9e4066Sahrens 	zdb_cb_t zcb = { 0 };
2672fa9e4066Sahrens 	zdb_blkstats_t *zb, *tzb;
2673b24ab676SJeff Bonwick 	uint64_t norm_alloc, norm_space, total_alloc, total_found;
2674cd088ea4SVictor Latushkin 	int flags = TRAVERSE_PRE | TRAVERSE_PREFETCH_METADATA | TRAVERSE_HARD;
26755d7b4d43SMatthew Ahrens 	boolean_t leaks = B_FALSE;
2676fa9e4066Sahrens 
2677490d05b9SMatthew Ahrens 	(void) printf("\nTraversing all blocks %s%s%s%s%s...\n\n",
2678b24ab676SJeff Bonwick 	    (dump_opt['c'] || !dump_opt['L']) ? "to verify " : "",
2679b24ab676SJeff Bonwick 	    (dump_opt['c'] == 1) ? "metadata " : "",
2680b24ab676SJeff Bonwick 	    dump_opt['c'] ? "checksums " : "",
2681b24ab676SJeff Bonwick 	    (dump_opt['c'] && !dump_opt['L']) ? "and verify " : "",
2682b24ab676SJeff Bonwick 	    !dump_opt['L'] ? "nothing leaked " : "");
2683fa9e4066Sahrens 
2684fa9e4066Sahrens 	/*
2685e14bb325SJeff Bonwick 	 * Load all space maps as SM_ALLOC maps, then traverse the pool
2686e14bb325SJeff Bonwick 	 * claiming each block we discover.  If the pool is perfectly
2687e14bb325SJeff Bonwick 	 * consistent, the space maps will be empty when we're done.
2688e14bb325SJeff Bonwick 	 * Anything left over is a leak; any block we can't claim (because
2689e14bb325SJeff Bonwick 	 * it's not part of any space map) is a double allocation,
2690e14bb325SJeff Bonwick 	 * reference to a freed block, or an unclaimed log block.
2691fa9e4066Sahrens 	 */
2692b24ab676SJeff Bonwick 	zdb_leak_init(spa, &zcb);
2693fa9e4066Sahrens 
2694fa9e4066Sahrens 	/*
2695fa9e4066Sahrens 	 * If there's a deferred-free bplist, process that first.
2696fa9e4066Sahrens 	 */
2697cde58dbcSMatthew Ahrens 	(void) bpobj_iterate_nofree(&spa->spa_deferred_bpobj,
2698cde58dbcSMatthew Ahrens 	    count_block_cb, &zcb, NULL);
26993b2aab18SMatthew Ahrens 	if (spa_version(spa) >= SPA_VERSION_DEADLISTS) {
27003b2aab18SMatthew Ahrens 		(void) bpobj_iterate_nofree(&spa->spa_dsl_pool->dp_free_bpobj,
27013b2aab18SMatthew Ahrens 		    count_block_cb, &zcb, NULL);
27023b2aab18SMatthew Ahrens 	}
27032acef22dSMatthew Ahrens 	if (spa_feature_is_active(spa, SPA_FEATURE_ASYNC_DESTROY)) {
2704b420f3adSRichard Lowe 		VERIFY3U(0, ==, bptree_iterate(spa->spa_meta_objset,
2705ad135b5dSChristopher Siden 		    spa->spa_dsl_pool->dp_bptree_obj, B_FALSE, count_block_cb,
2706ad135b5dSChristopher Siden 		    &zcb, NULL));
2707ad135b5dSChristopher Siden 	}
2708fa9e4066Sahrens 
2709bbfd46c4SJeff Bonwick 	if (dump_opt['c'] > 1)
2710bbfd46c4SJeff Bonwick 		flags |= TRAVERSE_PREFETCH_DATA;
2711bbfd46c4SJeff Bonwick 
2712490d05b9SMatthew Ahrens 	zcb.zcb_totalasize = metaslab_class_get_alloc(spa_normal_class(spa));
2713490d05b9SMatthew Ahrens 	zcb.zcb_start = zcb.zcb_lastprint = gethrtime();
2714bbfd46c4SJeff Bonwick 	zcb.zcb_haderrors |= traverse_pool(spa, 0, flags, zdb_blkptr_cb, &zcb);
2715fa9e4066Sahrens 
271631d7e8faSGeorge Wilson 	/*
271731d7e8faSGeorge Wilson 	 * If we've traversed the data blocks then we need to wait for those
271831d7e8faSGeorge Wilson 	 * I/Os to complete. We leverage "The Godfather" zio to wait on
271931d7e8faSGeorge Wilson 	 * all async I/Os to complete.
272031d7e8faSGeorge Wilson 	 */
272131d7e8faSGeorge Wilson 	if (dump_opt['c']) {
27226f834bc1SMatthew Ahrens 		for (int i = 0; i < max_ncpus; i++) {
27236f834bc1SMatthew Ahrens 			(void) zio_wait(spa->spa_async_zio_root[i]);
27246f834bc1SMatthew Ahrens 			spa->spa_async_zio_root[i] = zio_root(spa, NULL, NULL,
27256f834bc1SMatthew Ahrens 			    ZIO_FLAG_CANFAIL | ZIO_FLAG_SPECULATIVE |
27266f834bc1SMatthew Ahrens 			    ZIO_FLAG_GODFATHER);
27276f834bc1SMatthew Ahrens 		}
272831d7e8faSGeorge Wilson 	}
272931d7e8faSGeorge Wilson 
2730b24ab676SJeff Bonwick 	if (zcb.zcb_haderrors) {
2731fa9e4066Sahrens 		(void) printf("\nError counts:\n\n");
2732fa9e4066Sahrens 		(void) printf("\t%5s  %s\n", "errno", "count");
2733b24ab676SJeff Bonwick 		for (int e = 0; e < 256; e++) {
2734fa9e4066Sahrens 			if (zcb.zcb_errors[e] != 0) {
2735fa9e4066Sahrens 				(void) printf("\t%5d  %llu\n",
2736fa9e4066Sahrens 				    e, (u_longlong_t)zcb.zcb_errors[e]);
2737fa9e4066Sahrens 			}
2738fa9e4066Sahrens 		}
2739fa9e4066Sahrens 	}
2740fa9e4066Sahrens 
2741fa9e4066Sahrens 	/*
2742fa9e4066Sahrens 	 * Report any leaked segments.
2743fa9e4066Sahrens 	 */
2744b24ab676SJeff Bonwick 	zdb_leak_fini(spa);
2745fa9e4066Sahrens 
2746b24ab676SJeff Bonwick 	tzb = &zcb.zcb_type[ZB_TOTAL][ZDB_OT_TOTAL];
27478654d025Sperrin 
2748b24ab676SJeff Bonwick 	norm_alloc = metaslab_class_get_alloc(spa_normal_class(spa));
2749b24ab676SJeff Bonwick 	norm_space = metaslab_class_get_space(spa_normal_class(spa));
27508654d025Sperrin 
2751b24ab676SJeff Bonwick 	total_alloc = norm_alloc + metaslab_class_get_alloc(spa_log_class(spa));
2752b24ab676SJeff Bonwick 	total_found = tzb->zb_asize - zcb.zcb_dedup_asize;
2753fa9e4066Sahrens 
2754b24ab676SJeff Bonwick 	if (total_found == total_alloc) {
275582a0a985SVictor Latushkin 		if (!dump_opt['L'])
275682a0a985SVictor Latushkin 			(void) printf("\n\tNo leaks (block sum matches space"
275782a0a985SVictor Latushkin 			    " maps exactly)\n");
2758fa9e4066Sahrens 	} else {
2759fa9e4066Sahrens 		(void) printf("block traversal size %llu != alloc %llu "
276082a0a985SVictor Latushkin 		    "(%s %lld)\n",
2761b24ab676SJeff Bonwick 		    (u_longlong_t)total_found,
2762b24ab676SJeff Bonwick 		    (u_longlong_t)total_alloc,
276382a0a985SVictor Latushkin 		    (dump_opt['L']) ? "unreachable" : "leaked",
2764b24ab676SJeff Bonwick 		    (longlong_t)(total_alloc - total_found));
27655d7b4d43SMatthew Ahrens 		leaks = B_TRUE;
2766fa9e4066Sahrens 	}
2767fa9e4066Sahrens 
2768fa9e4066Sahrens 	if (tzb->zb_count == 0)
2769fa9e4066Sahrens 		return (2);
2770fa9e4066Sahrens 
2771fa9e4066Sahrens 	(void) printf("\n");
2772fa9e4066Sahrens 	(void) printf("\tbp count:      %10llu\n",
2773fa9e4066Sahrens 	    (u_longlong_t)tzb->zb_count);
2774d5ee8a13SMatthew Ahrens 	(void) printf("\tganged count:  %10llu\n",
2775d5ee8a13SMatthew Ahrens 	    (longlong_t)tzb->zb_gangs);
2776b24ab676SJeff Bonwick 	(void) printf("\tbp logical:    %10llu      avg: %6llu\n",
2777fa9e4066Sahrens 	    (u_longlong_t)tzb->zb_lsize,
2778fa9e4066Sahrens 	    (u_longlong_t)(tzb->zb_lsize / tzb->zb_count));
2779b24ab676SJeff Bonwick 	(void) printf("\tbp physical:   %10llu      avg:"
2780b24ab676SJeff Bonwick 	    " %6llu     compression: %6.2f\n",
2781fa9e4066Sahrens 	    (u_longlong_t)tzb->zb_psize,
2782fa9e4066Sahrens 	    (u_longlong_t)(tzb->zb_psize / tzb->zb_count),
2783fa9e4066Sahrens 	    (double)tzb->zb_lsize / tzb->zb_psize);
2784b24ab676SJeff Bonwick 	(void) printf("\tbp allocated:  %10llu      avg:"
2785b24ab676SJeff Bonwick 	    " %6llu     compression: %6.2f\n",
2786fa9e4066Sahrens 	    (u_longlong_t)tzb->zb_asize,
2787fa9e4066Sahrens 	    (u_longlong_t)(tzb->zb_asize / tzb->zb_count),
2788fa9e4066Sahrens 	    (double)tzb->zb_lsize / tzb->zb_asize);
2789b24ab676SJeff Bonwick 	(void) printf("\tbp deduped:    %10llu    ref>1:"
2790b24ab676SJeff Bonwick 	    " %6llu   deduplication: %6.2f\n",
2791b24ab676SJeff Bonwick 	    (u_longlong_t)zcb.zcb_dedup_asize,
2792b24ab676SJeff Bonwick 	    (u_longlong_t)zcb.zcb_dedup_blocks,
2793b24ab676SJeff Bonwick 	    (double)zcb.zcb_dedup_asize / tzb->zb_asize + 1.0);
2794b24ab676SJeff Bonwick 	(void) printf("\tSPA allocated: %10llu     used: %5.2f%%\n",
2795b24ab676SJeff Bonwick 	    (u_longlong_t)norm_alloc, 100.0 * norm_alloc / norm_space);
2796fa9e4066Sahrens 
27975d7b4d43SMatthew Ahrens 	for (bp_embedded_type_t i = 0; i < NUM_BP_EMBEDDED_TYPES; i++) {
27985d7b4d43SMatthew Ahrens 		if (zcb.zcb_embedded_blocks[i] == 0)
27995d7b4d43SMatthew Ahrens 			continue;
28005d7b4d43SMatthew Ahrens 		(void) printf("\n");
28015d7b4d43SMatthew Ahrens 		(void) printf("\tadditional, non-pointer bps of type %u: "
28025d7b4d43SMatthew Ahrens 		    "%10llu\n",
28035d7b4d43SMatthew Ahrens 		    i, (u_longlong_t)zcb.zcb_embedded_blocks[i]);
28045d7b4d43SMatthew Ahrens 
28055d7b4d43SMatthew Ahrens 		if (dump_opt['b'] >= 3) {
28065d7b4d43SMatthew Ahrens 			(void) printf("\t number of (compressed) bytes:  "
28075d7b4d43SMatthew Ahrens 			    "number of bps\n");
28085d7b4d43SMatthew Ahrens 			dump_histogram(zcb.zcb_embedded_histogram[i],
28095d7b4d43SMatthew Ahrens 			    sizeof (zcb.zcb_embedded_histogram[i]) /
28105d7b4d43SMatthew Ahrens 			    sizeof (zcb.zcb_embedded_histogram[i][0]), 0);
28115d7b4d43SMatthew Ahrens 		}
28125d7b4d43SMatthew Ahrens 	}
28135d7b4d43SMatthew Ahrens 
2814d5ee8a13SMatthew Ahrens 	if (tzb->zb_ditto_samevdev != 0) {
2815d5ee8a13SMatthew Ahrens 		(void) printf("\tDittoed blocks on same vdev: %llu\n",
2816d5ee8a13SMatthew Ahrens 		    (longlong_t)tzb->zb_ditto_samevdev);
2817d5ee8a13SMatthew Ahrens 	}
2818d5ee8a13SMatthew Ahrens 
2819fa9e4066Sahrens 	if (dump_opt['b'] >= 2) {
2820fa9e4066Sahrens 		int l, t, level;
2821fa9e4066Sahrens 		(void) printf("\nBlocks\tLSIZE\tPSIZE\tASIZE"
2822fa9e4066Sahrens 		    "\t  avg\t comp\t%%Total\tType\n");
2823fa9e4066Sahrens 
2824b24ab676SJeff Bonwick 		for (t = 0; t <= ZDB_OT_TOTAL; t++) {
28253f9d6ad7SLin Ling 			char csize[32], lsize[32], psize[32], asize[32];
2826d5ee8a13SMatthew Ahrens 			char avg[32], gang[32];
2827fa9e4066Sahrens 			char *typename;
2828fa9e4066Sahrens 
2829b24ab676SJeff Bonwick 			if (t < DMU_OT_NUMTYPES)
2830b24ab676SJeff Bonwick 				typename = dmu_ot[t].ot_name;
2831b24ab676SJeff Bonwick 			else
2832b24ab676SJeff Bonwick 				typename = zdb_ot_extname[t - DMU_OT_NUMTYPES];
2833fa9e4066Sahrens 
2834fa9e4066Sahrens 			if (zcb.zcb_type[ZB_TOTAL][t].zb_asize == 0) {
2835fa9e4066Sahrens 				(void) printf("%6s\t%5s\t%5s\t%5s"
2836fa9e4066Sahrens 				    "\t%5s\t%5s\t%6s\t%s\n",
2837fa9e4066Sahrens 				    "-",
2838fa9e4066Sahrens 				    "-",
2839fa9e4066Sahrens 				    "-",
2840fa9e4066Sahrens 				    "-",
2841fa9e4066Sahrens 				    "-",
2842fa9e4066Sahrens 				    "-",
2843fa9e4066Sahrens 				    "-",
2844fa9e4066Sahrens 				    typename);
2845fa9e4066Sahrens 				continue;
2846fa9e4066Sahrens 			}
2847fa9e4066Sahrens 
2848fa9e4066Sahrens 			for (l = ZB_TOTAL - 1; l >= -1; l--) {
2849fa9e4066Sahrens 				level = (l == -1 ? ZB_TOTAL : l);
2850fa9e4066Sahrens 				zb = &zcb.zcb_type[level][t];
2851fa9e4066Sahrens 
2852fa9e4066Sahrens 				if (zb->zb_asize == 0)
2853fa9e4066Sahrens 					continue;
2854fa9e4066Sahrens 
2855fa9e4066Sahrens 				if (dump_opt['b'] < 3 && level != ZB_TOTAL)
2856fa9e4066Sahrens 					continue;
2857fa9e4066Sahrens 
2858fa9e4066Sahrens 				if (level == 0 && zb->zb_asize ==
2859fa9e4066Sahrens 				    zcb.zcb_type[ZB_TOTAL][t].zb_asize)
2860fa9e4066Sahrens 					continue;
2861fa9e4066Sahrens 
28623f9d6ad7SLin Ling 				zdb_nicenum(zb->zb_count, csize);
28633f9d6ad7SLin Ling 				zdb_nicenum(zb->zb_lsize, lsize);
28643f9d6ad7SLin Ling 				zdb_nicenum(zb->zb_psize, psize);
28653f9d6ad7SLin Ling 				zdb_nicenum(zb->zb_asize, asize);
28663f9d6ad7SLin Ling 				zdb_nicenum(zb->zb_asize / zb->zb_count, avg);
2867d5ee8a13SMatthew Ahrens 				zdb_nicenum(zb->zb_gangs, gang);
2868fa9e4066Sahrens 
2869fa9e4066Sahrens 				(void) printf("%6s\t%5s\t%5s\t%5s\t%5s"
2870fa9e4066Sahrens 				    "\t%5.2f\t%6.2f\t",
2871fa9e4066Sahrens 				    csize, lsize, psize, asize, avg,
2872fa9e4066Sahrens 				    (double)zb->zb_lsize / zb->zb_psize,
2873fa9e4066Sahrens 				    100.0 * zb->zb_asize / tzb->zb_asize);
2874fa9e4066Sahrens 
2875fa9e4066Sahrens 				if (level == ZB_TOTAL)
2876fa9e4066Sahrens 					(void) printf("%s\n", typename);
2877fa9e4066Sahrens 				else
2878fa9e4066Sahrens 					(void) printf("    L%d %s\n",
2879fa9e4066Sahrens 					    level, typename);
2880490d05b9SMatthew Ahrens 
2881d5ee8a13SMatthew Ahrens 				if (dump_opt['b'] >= 3 && zb->zb_gangs > 0) {
2882d5ee8a13SMatthew Ahrens 					(void) printf("\t number of ganged "
2883d5ee8a13SMatthew Ahrens 					    "blocks: %s\n", gang);
2884d5ee8a13SMatthew Ahrens 				}
2885d5ee8a13SMatthew Ahrens 
2886490d05b9SMatthew Ahrens 				if (dump_opt['b'] >= 4) {
2887490d05b9SMatthew Ahrens 					(void) printf("psize "
2888490d05b9SMatthew Ahrens 					    "(in 512-byte sectors): "
2889490d05b9SMatthew Ahrens 					    "number of blocks\n");
2890490d05b9SMatthew Ahrens 					dump_histogram(zb->zb_psize_histogram,
28910713e232SGeorge Wilson 					    PSIZE_HISTO_SIZE, 0);
2892490d05b9SMatthew Ahrens 				}
2893fa9e4066Sahrens 			}
2894fa9e4066Sahrens 		}
2895fa9e4066Sahrens 	}
2896fa9e4066Sahrens 
2897fa9e4066Sahrens 	(void) printf("\n");
2898fa9e4066Sahrens 
2899fa9e4066Sahrens 	if (leaks)
2900fa9e4066Sahrens 		return (2);
2901fa9e4066Sahrens 
2902fa9e4066Sahrens 	if (zcb.zcb_haderrors)
2903fa9e4066Sahrens 		return (3);
2904fa9e4066Sahrens 
2905fa9e4066Sahrens 	return (0);
2906fa9e4066Sahrens }
2907fa9e4066Sahrens 
2908b24ab676SJeff Bonwick typedef struct zdb_ddt_entry {
2909b24ab676SJeff Bonwick 	ddt_key_t	zdde_key;
2910b24ab676SJeff Bonwick 	uint64_t	zdde_ref_blocks;
2911b24ab676SJeff Bonwick 	uint64_t	zdde_ref_lsize;
2912b24ab676SJeff Bonwick 	uint64_t	zdde_ref_psize;
2913b24ab676SJeff Bonwick 	uint64_t	zdde_ref_dsize;
2914b24ab676SJeff Bonwick 	avl_node_t	zdde_node;
2915b24ab676SJeff Bonwick } zdb_ddt_entry_t;
2916b24ab676SJeff Bonwick 
2917b24ab676SJeff Bonwick /* ARGSUSED */
2918b24ab676SJeff Bonwick static int
2919b24ab676SJeff Bonwick zdb_ddt_add_cb(spa_t *spa, zilog_t *zilog, const blkptr_t *bp,
29207802d7bfSMatthew Ahrens     const zbookmark_phys_t *zb, const dnode_phys_t *dnp, void *arg)
2921b24ab676SJeff Bonwick {
2922b24ab676SJeff Bonwick 	avl_tree_t *t = arg;
2923b24ab676SJeff Bonwick 	avl_index_t where;
2924b24ab676SJeff Bonwick 	zdb_ddt_entry_t *zdde, zdde_search;
2925b24ab676SJeff Bonwick 
2926a2cdcdd2SPaul Dagnelie 	if (bp == NULL || BP_IS_HOLE(bp) || BP_IS_EMBEDDED(bp))
2927b24ab676SJeff Bonwick 		return (0);
2928b24ab676SJeff Bonwick 
2929b24ab676SJeff Bonwick 	if (dump_opt['S'] > 1 && zb->zb_level == ZB_ROOT_LEVEL) {
2930b24ab676SJeff Bonwick 		(void) printf("traversing objset %llu, %llu objects, "
2931b24ab676SJeff Bonwick 		    "%lu blocks so far\n",
2932b24ab676SJeff Bonwick 		    (u_longlong_t)zb->zb_objset,
29335d7b4d43SMatthew Ahrens 		    (u_longlong_t)BP_GET_FILL(bp),
2934b24ab676SJeff Bonwick 		    avl_numnodes(t));
2935b24ab676SJeff Bonwick 	}
2936b24ab676SJeff Bonwick 
2937bbfd46c4SJeff Bonwick 	if (BP_IS_HOLE(bp) || BP_GET_CHECKSUM(bp) == ZIO_CHECKSUM_OFF ||
2938ad135b5dSChristopher Siden 	    BP_GET_LEVEL(bp) > 0 || DMU_OT_IS_METADATA(BP_GET_TYPE(bp)))
2939b24ab676SJeff Bonwick 		return (0);
2940b24ab676SJeff Bonwick 
2941b24ab676SJeff Bonwick 	ddt_key_fill(&zdde_search.zdde_key, bp);
2942b24ab676SJeff Bonwick 
2943b24ab676SJeff Bonwick 	zdde = avl_find(t, &zdde_search, &where);
2944b24ab676SJeff Bonwick 
2945b24ab676SJeff Bonwick 	if (zdde == NULL) {
2946b24ab676SJeff Bonwick 		zdde = umem_zalloc(sizeof (*zdde), UMEM_NOFAIL);
2947b24ab676SJeff Bonwick 		zdde->zdde_key = zdde_search.zdde_key;
2948b24ab676SJeff Bonwick 		avl_insert(t, zdde, where);
2949b24ab676SJeff Bonwick 	}
2950b24ab676SJeff Bonwick 
2951b24ab676SJeff Bonwick 	zdde->zdde_ref_blocks += 1;
2952b24ab676SJeff Bonwick 	zdde->zdde_ref_lsize += BP_GET_LSIZE(bp);
2953b24ab676SJeff Bonwick 	zdde->zdde_ref_psize += BP_GET_PSIZE(bp);
2954b24ab676SJeff Bonwick 	zdde->zdde_ref_dsize += bp_get_dsize_sync(spa, bp);
2955b24ab676SJeff Bonwick 
2956b24ab676SJeff Bonwick 	return (0);
2957b24ab676SJeff Bonwick }
2958b24ab676SJeff Bonwick 
2959b24ab676SJeff Bonwick static void
2960b24ab676SJeff Bonwick dump_simulated_ddt(spa_t *spa)
2961b24ab676SJeff Bonwick {
2962b24ab676SJeff Bonwick 	avl_tree_t t;
2963b24ab676SJeff Bonwick 	void *cookie = NULL;
2964b24ab676SJeff Bonwick 	zdb_ddt_entry_t *zdde;
2965b24ab676SJeff Bonwick 	ddt_histogram_t ddh_total = { 0 };
2966b24ab676SJeff Bonwick 	ddt_stat_t dds_total = { 0 };
2967b24ab676SJeff Bonwick 
2968b24ab676SJeff Bonwick 	avl_create(&t, ddt_entry_compare,
2969b24ab676SJeff Bonwick 	    sizeof (zdb_ddt_entry_t), offsetof(zdb_ddt_entry_t, zdde_node));
2970b24ab676SJeff Bonwick 
2971b24ab676SJeff Bonwick 	spa_config_enter(spa, SCL_CONFIG, FTAG, RW_READER);
2972b24ab676SJeff Bonwick 
2973bbfd46c4SJeff Bonwick 	(void) traverse_pool(spa, 0, TRAVERSE_PRE | TRAVERSE_PREFETCH_METADATA,
2974bbfd46c4SJeff Bonwick 	    zdb_ddt_add_cb, &t);
2975b24ab676SJeff Bonwick 
2976b24ab676SJeff Bonwick 	spa_config_exit(spa, SCL_CONFIG, FTAG);
2977b24ab676SJeff Bonwick 
2978b24ab676SJeff Bonwick 	while ((zdde = avl_destroy_nodes(&t, &cookie)) != NULL) {
2979b24ab676SJeff Bonwick 		ddt_stat_t dds;
2980b24ab676SJeff Bonwick 		uint64_t refcnt = zdde->zdde_ref_blocks;
2981b24ab676SJeff Bonwick 		ASSERT(refcnt != 0);
2982b24ab676SJeff Bonwick 
2983b24ab676SJeff Bonwick 		dds.dds_blocks = zdde->zdde_ref_blocks / refcnt;
2984b24ab676SJeff Bonwick 		dds.dds_lsize = zdde->zdde_ref_lsize / refcnt;
2985b24ab676SJeff Bonwick 		dds.dds_psize = zdde->zdde_ref_psize / refcnt;
2986b24ab676SJeff Bonwick 		dds.dds_dsize = zdde->zdde_ref_dsize / refcnt;
2987b24ab676SJeff Bonwick 
2988b24ab676SJeff Bonwick 		dds.dds_ref_blocks = zdde->zdde_ref_blocks;
2989b24ab676SJeff Bonwick 		dds.dds_ref_lsize = zdde->zdde_ref_lsize;
2990b24ab676SJeff Bonwick 		dds.dds_ref_psize = zdde->zdde_ref_psize;
2991b24ab676SJeff Bonwick 		dds.dds_ref_dsize = zdde->zdde_ref_dsize;
2992b24ab676SJeff Bonwick 
2993bf16b11eSMatthew Ahrens 		ddt_stat_add(&ddh_total.ddh_stat[highbit64(refcnt) - 1],
2994bf16b11eSMatthew Ahrens 		    &dds, 0);
2995b24ab676SJeff Bonwick 
2996b24ab676SJeff Bonwick 		umem_free(zdde, sizeof (*zdde));
2997b24ab676SJeff Bonwick 	}
2998b24ab676SJeff Bonwick 
2999b24ab676SJeff Bonwick 	avl_destroy(&t);
3000b24ab676SJeff Bonwick 
3001b24ab676SJeff Bonwick 	ddt_histogram_stat(&dds_total, &ddh_total);
3002b24ab676SJeff Bonwick 
3003b24ab676SJeff Bonwick 	(void) printf("Simulated DDT histogram:\n");
3004b24ab676SJeff Bonwick 
30059eb19f4dSGeorge Wilson 	zpool_dump_ddt(&dds_total, &ddh_total);
3006b24ab676SJeff Bonwick 
3007b24ab676SJeff Bonwick 	dump_dedup_ratio(&dds_total);
3008b24ab676SJeff Bonwick }
3009b24ab676SJeff Bonwick 
3010fa9e4066Sahrens static void
3011fa9e4066Sahrens dump_zpool(spa_t *spa)
3012fa9e4066Sahrens {
3013fa9e4066Sahrens 	dsl_pool_t *dp = spa_get_dsl(spa);
3014fa9e4066Sahrens 	int rc = 0;
3015fa9e4066Sahrens 
3016b24ab676SJeff Bonwick 	if (dump_opt['S']) {
3017b24ab676SJeff Bonwick 		dump_simulated_ddt(spa);
3018b24ab676SJeff Bonwick 		return;
3019b24ab676SJeff Bonwick 	}
3020b24ab676SJeff Bonwick 
302107428bdfSVictor Latushkin 	if (!dump_opt['e'] && dump_opt['C'] > 1) {
302207428bdfSVictor Latushkin 		(void) printf("\nCached configuration:\n");
302307428bdfSVictor Latushkin 		dump_nvlist(spa->spa_config, 8);
302407428bdfSVictor Latushkin 	}
302507428bdfSVictor Latushkin 
302607428bdfSVictor Latushkin 	if (dump_opt['C'])
302707428bdfSVictor Latushkin 		dump_config(spa);
302807428bdfSVictor Latushkin 
3029fa9e4066Sahrens 	if (dump_opt['u'])
303053b9a4a9SVictor Latushkin 		dump_uberblock(&spa->spa_uberblock, "\nUberblock:\n", "\n");
3031fa9e4066Sahrens 
3032b24ab676SJeff Bonwick 	if (dump_opt['D'])
3033b24ab676SJeff Bonwick 		dump_all_ddts(spa);
3034b24ab676SJeff Bonwick 
303587219db7SVictor Latushkin 	if (dump_opt['d'] > 2 || dump_opt['m'])
303687219db7SVictor Latushkin 		dump_metaslabs(spa);
30372e4c9986SGeorge Wilson 	if (dump_opt['M'])
30382e4c9986SGeorge Wilson 		dump_metaslab_groups(spa);
303987219db7SVictor Latushkin 
304087219db7SVictor Latushkin 	if (dump_opt['d'] || dump_opt['i']) {
3041fa9e4066Sahrens 		dump_dir(dp->dp_meta_objset);
3042fa9e4066Sahrens 		if (dump_opt['d'] >= 3) {
3043732885fcSMatthew Ahrens 			dump_full_bpobj(&spa->spa_deferred_bpobj,
3044d0475637SMatthew Ahrens 			    "Deferred frees", 0);
3045cde58dbcSMatthew Ahrens 			if (spa_version(spa) >= SPA_VERSION_DEADLISTS) {
3046732885fcSMatthew Ahrens 				dump_full_bpobj(
3047732885fcSMatthew Ahrens 				    &spa->spa_dsl_pool->dp_free_bpobj,
3048d0475637SMatthew Ahrens 				    "Pool snapshot frees", 0);
3049ad135b5dSChristopher Siden 			}
3050ad135b5dSChristopher Siden 
3051ad135b5dSChristopher Siden 			if (spa_feature_is_active(spa,
30522acef22dSMatthew Ahrens 			    SPA_FEATURE_ASYNC_DESTROY)) {
3053ad135b5dSChristopher Siden 				dump_bptree(spa->spa_meta_objset,
3054ad135b5dSChristopher Siden 				    spa->spa_dsl_pool->dp_bptree_obj,
3055ad135b5dSChristopher Siden 				    "Pool dataset frees");
3056cde58dbcSMatthew Ahrens 			}
3057fa9e4066Sahrens 			dump_dtl(spa->spa_root_vdev, 0);
3058fa9e4066Sahrens 		}
305907428bdfSVictor Latushkin 		(void) dmu_objset_find(spa_name(spa), dump_one_dir,
306007428bdfSVictor Latushkin 		    NULL, DS_FIND_SNAPSHOTS | DS_FIND_CHILDREN);
3061b5152584SMatthew Ahrens 
3062ca0cc391SMatthew Ahrens 		for (spa_feature_t f = 0; f < SPA_FEATURES; f++) {
3063ca0cc391SMatthew Ahrens 			uint64_t refcount;
3064ca0cc391SMatthew Ahrens 
3065ca0cc391SMatthew Ahrens 			if (!(spa_feature_table[f].fi_flags &
3066c5d1600cSMatthew Ahrens 			    ZFEATURE_FLAG_PER_DATASET) ||
3067c5d1600cSMatthew Ahrens 			    !spa_feature_is_enabled(spa, f)) {
3068ca0cc391SMatthew Ahrens 				ASSERT0(dataset_feature_count[f]);
3069ca0cc391SMatthew Ahrens 				continue;
3070ca0cc391SMatthew Ahrens 			}
3071ca0cc391SMatthew Ahrens 			(void) feature_get_refcount(spa,
3072ca0cc391SMatthew Ahrens 			    &spa_feature_table[f], &refcount);
3073ca0cc391SMatthew Ahrens 			if (dataset_feature_count[f] != refcount) {
3074ca0cc391SMatthew Ahrens 				(void) printf("%s feature refcount mismatch: "
3075ca0cc391SMatthew Ahrens 				    "%lld datasets != %lld refcount\n",
3076ca0cc391SMatthew Ahrens 				    spa_feature_table[f].fi_uname,
3077ca0cc391SMatthew Ahrens 				    (longlong_t)dataset_feature_count[f],
3078ca0cc391SMatthew Ahrens 				    (longlong_t)refcount);
3079ca0cc391SMatthew Ahrens 				rc = 2;
3080ca0cc391SMatthew Ahrens 			} else {
3081ca0cc391SMatthew Ahrens 				(void) printf("Verified %s feature refcount "
3082ca0cc391SMatthew Ahrens 				    "of %llu is correct\n",
3083ca0cc391SMatthew Ahrens 				    spa_feature_table[f].fi_uname,
3084ca0cc391SMatthew Ahrens 				    (longlong_t)refcount);
3085ca0cc391SMatthew Ahrens 			}
3086b5152584SMatthew Ahrens 		}
3087fa9e4066Sahrens 	}
3088b5152584SMatthew Ahrens 	if (rc == 0 && (dump_opt['b'] || dump_opt['c']))
3089fa9e4066Sahrens 		rc = dump_block_stats(spa);
3090fa9e4066Sahrens 
30910713e232SGeorge Wilson 	if (rc == 0)
30920713e232SGeorge Wilson 		rc = verify_spacemap_refcounts(spa);
30930713e232SGeorge Wilson 
3094fa9e4066Sahrens 	if (dump_opt['s'])
3095fa9e4066Sahrens 		show_pool_stats(spa);
3096fa9e4066Sahrens 
30978f18d1faSGeorge Wilson 	if (dump_opt['h'])
30988f18d1faSGeorge Wilson 		dump_history(spa);
30998f18d1faSGeorge Wilson 
310029bdd2f9SPavel Zakharov 	if (rc != 0) {
310129bdd2f9SPavel Zakharov 		dump_debug_buffer();
3102fa9e4066Sahrens 		exit(rc);
310329bdd2f9SPavel Zakharov 	}
3104fa9e4066Sahrens }
3105fa9e4066Sahrens 
310644cd46caSbillm #define	ZDB_FLAG_CHECKSUM	0x0001
310744cd46caSbillm #define	ZDB_FLAG_DECOMPRESS	0x0002
310844cd46caSbillm #define	ZDB_FLAG_BSWAP		0x0004
310944cd46caSbillm #define	ZDB_FLAG_GBH		0x0008
311044cd46caSbillm #define	ZDB_FLAG_INDIRECT	0x0010
311144cd46caSbillm #define	ZDB_FLAG_PHYS		0x0020
311244cd46caSbillm #define	ZDB_FLAG_RAW		0x0040
311344cd46caSbillm #define	ZDB_FLAG_PRINT_BLKPTR	0x0080
311444cd46caSbillm 
311544cd46caSbillm int flagbits[256];
311644cd46caSbillm 
311744cd46caSbillm static void
311844cd46caSbillm zdb_print_blkptr(blkptr_t *bp, int flags)
311944cd46caSbillm {
3120b24ab676SJeff Bonwick 	char blkbuf[BP_SPRINTF_LEN];
312144cd46caSbillm 
312244cd46caSbillm 	if (flags & ZDB_FLAG_BSWAP)
312344cd46caSbillm 		byteswap_uint64_array((void *)bp, sizeof (blkptr_t));
3124b24ab676SJeff Bonwick 
312543466aaeSMax Grossman 	snprintf_blkptr(blkbuf, sizeof (blkbuf), bp);
3126b24ab676SJeff Bonwick 	(void) printf("%s\n", blkbuf);
312744cd46caSbillm }
312844cd46caSbillm 
312944cd46caSbillm static void
313044cd46caSbillm zdb_dump_indirect(blkptr_t *bp, int nbps, int flags)
313144cd46caSbillm {
313244cd46caSbillm 	int i;
313344cd46caSbillm 
313444cd46caSbillm 	for (i = 0; i < nbps; i++)
313544cd46caSbillm 		zdb_print_blkptr(&bp[i], flags);
313644cd46caSbillm }
313744cd46caSbillm 
313844cd46caSbillm static void
313944cd46caSbillm zdb_dump_gbh(void *buf, int flags)
314044cd46caSbillm {
314144cd46caSbillm 	zdb_dump_indirect((blkptr_t *)buf, SPA_GBH_NBLKPTRS, flags);
314244cd46caSbillm }
314344cd46caSbillm 
314444cd46caSbillm static void
314544cd46caSbillm zdb_dump_block_raw(void *buf, uint64_t size, int flags)
314644cd46caSbillm {
314744cd46caSbillm 	if (flags & ZDB_FLAG_BSWAP)
314844cd46caSbillm 		byteswap_uint64_array(buf, size);
3149b24ab676SJeff Bonwick 	(void) write(1, buf, size);
315044cd46caSbillm }
315144cd46caSbillm 
315244cd46caSbillm static void
315344cd46caSbillm zdb_dump_block(char *label, void *buf, uint64_t size, int flags)
315444cd46caSbillm {
315544cd46caSbillm 	uint64_t *d = (uint64_t *)buf;
315644cd46caSbillm 	int nwords = size / sizeof (uint64_t);
315744cd46caSbillm 	int do_bswap = !!(flags & ZDB_FLAG_BSWAP);
315844cd46caSbillm 	int i, j;
315944cd46caSbillm 	char *hdr, *c;
316044cd46caSbillm 
316144cd46caSbillm 
316244cd46caSbillm 	if (do_bswap)
316344cd46caSbillm 		hdr = " 7 6 5 4 3 2 1 0   f e d c b a 9 8";
316444cd46caSbillm 	else
316544cd46caSbillm 		hdr = " 0 1 2 3 4 5 6 7   8 9 a b c d e f";
316644cd46caSbillm 
316744cd46caSbillm 	(void) printf("\n%s\n%6s   %s  0123456789abcdef\n", label, "", hdr);
316844cd46caSbillm 
316944cd46caSbillm 	for (i = 0; i < nwords; i += 2) {
317044cd46caSbillm 		(void) printf("%06llx:  %016llx  %016llx  ",
317144cd46caSbillm 		    (u_longlong_t)(i * sizeof (uint64_t)),
317244cd46caSbillm 		    (u_longlong_t)(do_bswap ? BSWAP_64(d[i]) : d[i]),
317344cd46caSbillm 		    (u_longlong_t)(do_bswap ? BSWAP_64(d[i + 1]) : d[i + 1]));
317444cd46caSbillm 
317544cd46caSbillm 		c = (char *)&d[i];
317644cd46caSbillm 		for (j = 0; j < 2 * sizeof (uint64_t); j++)
317744cd46caSbillm 			(void) printf("%c", isprint(c[j]) ? c[j] : '.');
317844cd46caSbillm 		(void) printf("\n");
317944cd46caSbillm 	}
318044cd46caSbillm }
318144cd46caSbillm 
318244cd46caSbillm /*
318344cd46caSbillm  * There are two acceptable formats:
318444cd46caSbillm  *	leaf_name	  - For example: c1t0d0 or /tmp/ztest.0a
318544cd46caSbillm  *	child[.child]*    - For example: 0.1.1
318644cd46caSbillm  *
318744cd46caSbillm  * The second form can be used to specify arbitrary vdevs anywhere
318844cd46caSbillm  * in the heirarchy.  For example, in a pool with a mirror of
318944cd46caSbillm  * RAID-Zs, you can specify either RAID-Z vdev with 0.0 or 0.1 .
319044cd46caSbillm  */
319144cd46caSbillm static vdev_t *
319244cd46caSbillm zdb_vdev_lookup(vdev_t *vdev, char *path)
319344cd46caSbillm {
319444cd46caSbillm 	char *s, *p, *q;
319544cd46caSbillm 	int i;
319644cd46caSbillm 
319744cd46caSbillm 	if (vdev == NULL)
319844cd46caSbillm 		return (NULL);
319944cd46caSbillm 
320044cd46caSbillm 	/* First, assume the x.x.x.x format */
320144cd46caSbillm 	i = (int)strtoul(path, &s, 10);
320244cd46caSbillm 	if (s == path || (s && *s != '.' && *s != '\0'))
320344cd46caSbillm 		goto name;
320444cd46caSbillm 	if (i < 0 || i >= vdev->vdev_children)
320544cd46caSbillm 		return (NULL);
320644cd46caSbillm 
320744cd46caSbillm 	vdev = vdev->vdev_child[i];
320844cd46caSbillm 	if (*s == '\0')
320944cd46caSbillm 		return (vdev);
321044cd46caSbillm 	return (zdb_vdev_lookup(vdev, s+1));
321144cd46caSbillm 
321244cd46caSbillm name:
321344cd46caSbillm 	for (i = 0; i < vdev->vdev_children; i++) {
321444cd46caSbillm 		vdev_t *vc = vdev->vdev_child[i];
321544cd46caSbillm 
321644cd46caSbillm 		if (vc->vdev_path == NULL) {
321744cd46caSbillm 			vc = zdb_vdev_lookup(vc, path);
321844cd46caSbillm 			if (vc == NULL)
321944cd46caSbillm 				continue;
322044cd46caSbillm 			else
322144cd46caSbillm 				return (vc);
322244cd46caSbillm 		}
322344cd46caSbillm 
322444cd46caSbillm 		p = strrchr(vc->vdev_path, '/');
322544cd46caSbillm 		p = p ? p + 1 : vc->vdev_path;
322644cd46caSbillm 		q = &vc->vdev_path[strlen(vc->vdev_path) - 2];
322744cd46caSbillm 
322844cd46caSbillm 		if (strcmp(vc->vdev_path, path) == 0)
322944cd46caSbillm 			return (vc);
323044cd46caSbillm 		if (strcmp(p, path) == 0)
323144cd46caSbillm 			return (vc);
323244cd46caSbillm 		if (strcmp(q, "s0") == 0 && strncmp(p, path, q - p) == 0)
323344cd46caSbillm 			return (vc);
323444cd46caSbillm 	}
323544cd46caSbillm 
323644cd46caSbillm 	return (NULL);
323744cd46caSbillm }
323844cd46caSbillm 
323944cd46caSbillm /*
324044cd46caSbillm  * Read a block from a pool and print it out.  The syntax of the
324144cd46caSbillm  * block descriptor is:
324244cd46caSbillm  *
324344cd46caSbillm  *	pool:vdev_specifier:offset:size[:flags]
324444cd46caSbillm  *
324544cd46caSbillm  *	pool           - The name of the pool you wish to read from
324644cd46caSbillm  *	vdev_specifier - Which vdev (see comment for zdb_vdev_lookup)
324744cd46caSbillm  *	offset         - offset, in hex, in bytes
324844cd46caSbillm  *	size           - Amount of data to read, in hex, in bytes
324944cd46caSbillm  *	flags          - A string of characters specifying options
325044cd46caSbillm  *		 b: Decode a blkptr at given offset within block
325144cd46caSbillm  *		*c: Calculate and display checksums
3252b24ab676SJeff Bonwick  *		 d: Decompress data before dumping
325344cd46caSbillm  *		 e: Byteswap data before dumping
3254b24ab676SJeff Bonwick  *		 g: Display data as a gang block header
3255b24ab676SJeff Bonwick  *		 i: Display as an indirect block
325644cd46caSbillm  *		 p: Do I/O to physical offset
325744cd46caSbillm  *		 r: Dump raw data to stdout
325844cd46caSbillm  *
325944cd46caSbillm  *              * = not yet implemented
326044cd46caSbillm  */
326144cd46caSbillm static void
326207428bdfSVictor Latushkin zdb_read_block(char *thing, spa_t *spa)
326344cd46caSbillm {
3264b24ab676SJeff Bonwick 	blkptr_t blk, *bp = &blk;
3265b24ab676SJeff Bonwick 	dva_t *dva = bp->blk_dva;
326644cd46caSbillm 	int flags = 0;
3267b24ab676SJeff Bonwick 	uint64_t offset = 0, size = 0, psize = 0, lsize = 0, blkptr_offset = 0;
326844cd46caSbillm 	zio_t *zio;
326944cd46caSbillm 	vdev_t *vd;
3270b24ab676SJeff Bonwick 	void *pbuf, *lbuf, *buf;
327107428bdfSVictor Latushkin 	char *s, *p, *dup, *vdev, *flagstr;
3272b24ab676SJeff Bonwick 	int i, error;
327344cd46caSbillm 
327444cd46caSbillm 	dup = strdup(thing);
327544cd46caSbillm 	s = strtok(dup, ":");
327644cd46caSbillm 	vdev = s ? s : "";
327744cd46caSbillm 	s = strtok(NULL, ":");
327844cd46caSbillm 	offset = strtoull(s ? s : "", NULL, 16);
327944cd46caSbillm 	s = strtok(NULL, ":");
328044cd46caSbillm 	size = strtoull(s ? s : "", NULL, 16);
328144cd46caSbillm 	s = strtok(NULL, ":");
328244cd46caSbillm 	flagstr = s ? s : "";
328344cd46caSbillm 
328444cd46caSbillm 	s = NULL;
328544cd46caSbillm 	if (size == 0)
328644cd46caSbillm 		s = "size must not be zero";
328744cd46caSbillm 	if (!IS_P2ALIGNED(size, DEV_BSIZE))
328844cd46caSbillm 		s = "size must be a multiple of sector size";
328944cd46caSbillm 	if (!IS_P2ALIGNED(offset, DEV_BSIZE))
329044cd46caSbillm 		s = "offset must be a multiple of sector size";
329144cd46caSbillm 	if (s) {
329244cd46caSbillm 		(void) printf("Invalid block specifier: %s  - %s\n", thing, s);
329344cd46caSbillm 		free(dup);
329444cd46caSbillm 		return;
329544cd46caSbillm 	}
329644cd46caSbillm 
329744cd46caSbillm 	for (s = strtok(flagstr, ":"); s; s = strtok(NULL, ":")) {
329844cd46caSbillm 		for (i = 0; flagstr[i]; i++) {
32995ad82045Snd 			int bit = flagbits[(uchar_t)flagstr[i]];
330044cd46caSbillm 
330144cd46caSbillm 			if (bit == 0) {
330244cd46caSbillm 				(void) printf("***Invalid flag: %c\n",
330344cd46caSbillm 				    flagstr[i]);
330444cd46caSbillm 				continue;
330544cd46caSbillm 			}
330644cd46caSbillm 			flags |= bit;
330744cd46caSbillm 
330844cd46caSbillm 			/* If it's not something with an argument, keep going */
3309b24ab676SJeff Bonwick 			if ((bit & (ZDB_FLAG_CHECKSUM |
331044cd46caSbillm 			    ZDB_FLAG_PRINT_BLKPTR)) == 0)
331144cd46caSbillm 				continue;
331244cd46caSbillm 
331344cd46caSbillm 			p = &flagstr[i + 1];
331444cd46caSbillm 			if (bit == ZDB_FLAG_PRINT_BLKPTR)
331544cd46caSbillm 				blkptr_offset = strtoull(p, &p, 16);
331644cd46caSbillm 			if (*p != ':' && *p != '\0') {
331744cd46caSbillm 				(void) printf("***Invalid flag arg: '%s'\n", s);
331844cd46caSbillm 				free(dup);
331944cd46caSbillm 				return;
332044cd46caSbillm 			}
332144cd46caSbillm 		}
332244cd46caSbillm 	}
332344cd46caSbillm 
332444cd46caSbillm 	vd = zdb_vdev_lookup(spa->spa_root_vdev, vdev);
332544cd46caSbillm 	if (vd == NULL) {
332644cd46caSbillm 		(void) printf("***Invalid vdev: %s\n", vdev);
332744cd46caSbillm 		free(dup);
332844cd46caSbillm 		return;
332944cd46caSbillm 	} else {
333044cd46caSbillm 		if (vd->vdev_path)
3331b24ab676SJeff Bonwick 			(void) fprintf(stderr, "Found vdev: %s\n",
3332b24ab676SJeff Bonwick 			    vd->vdev_path);
333344cd46caSbillm 		else
3334b24ab676SJeff Bonwick 			(void) fprintf(stderr, "Found vdev type: %s\n",
333544cd46caSbillm 			    vd->vdev_ops->vdev_op_type);
333644cd46caSbillm 	}
333744cd46caSbillm 
3338b24ab676SJeff Bonwick 	psize = size;
3339b24ab676SJeff Bonwick 	lsize = size;
334044cd46caSbillm 
3341b24ab676SJeff Bonwick 	pbuf = umem_alloc(SPA_MAXBLOCKSIZE, UMEM_NOFAIL);
3342b24ab676SJeff Bonwick 	lbuf = umem_alloc(SPA_MAXBLOCKSIZE, UMEM_NOFAIL);
3343b24ab676SJeff Bonwick 
3344b24ab676SJeff Bonwick 	BP_ZERO(bp);
3345b24ab676SJeff Bonwick 
3346b24ab676SJeff Bonwick 	DVA_SET_VDEV(&dva[0], vd->vdev_id);
3347b24ab676SJeff Bonwick 	DVA_SET_OFFSET(&dva[0], offset);
3348b24ab676SJeff Bonwick 	DVA_SET_GANG(&dva[0], !!(flags & ZDB_FLAG_GBH));
3349b24ab676SJeff Bonwick 	DVA_SET_ASIZE(&dva[0], vdev_psize_to_asize(vd, psize));
3350b24ab676SJeff Bonwick 
3351b24ab676SJeff Bonwick 	BP_SET_BIRTH(bp, TXG_INITIAL, TXG_INITIAL);
3352b24ab676SJeff Bonwick 
3353b24ab676SJeff Bonwick 	BP_SET_LSIZE(bp, lsize);
3354b24ab676SJeff Bonwick 	BP_SET_PSIZE(bp, psize);
3355b24ab676SJeff Bonwick 	BP_SET_COMPRESS(bp, ZIO_COMPRESS_OFF);
3356b24ab676SJeff Bonwick 	BP_SET_CHECKSUM(bp, ZIO_CHECKSUM_OFF);
3357b24ab676SJeff Bonwick 	BP_SET_TYPE(bp, DMU_OT_NONE);
3358b24ab676SJeff Bonwick 	BP_SET_LEVEL(bp, 0);
3359b24ab676SJeff Bonwick 	BP_SET_DEDUP(bp, 0);
3360b24ab676SJeff Bonwick 	BP_SET_BYTEORDER(bp, ZFS_HOST_BYTEORDER);
336144cd46caSbillm 
3362e14bb325SJeff Bonwick 	spa_config_enter(spa, SCL_STATE, FTAG, RW_READER);
336344cd46caSbillm 	zio = zio_root(spa, NULL, NULL, 0);
3364b24ab676SJeff Bonwick 
3365b24ab676SJeff Bonwick 	if (vd == vd->vdev_top) {
3366b24ab676SJeff Bonwick 		/*
3367b24ab676SJeff Bonwick 		 * Treat this as a normal block read.
3368b24ab676SJeff Bonwick 		 */
3369b24ab676SJeff Bonwick 		zio_nowait(zio_read(zio, spa, bp, pbuf, psize, NULL, NULL,
3370b24ab676SJeff Bonwick 		    ZIO_PRIORITY_SYNC_READ,
3371b24ab676SJeff Bonwick 		    ZIO_FLAG_CANFAIL | ZIO_FLAG_RAW, NULL));
3372b24ab676SJeff Bonwick 	} else {
3373b24ab676SJeff Bonwick 		/*
3374b24ab676SJeff Bonwick 		 * Treat this as a vdev child I/O.
3375b24ab676SJeff Bonwick 		 */
3376b24ab676SJeff Bonwick 		zio_nowait(zio_vdev_child_io(zio, bp, vd, offset, pbuf, psize,
3377b24ab676SJeff Bonwick 		    ZIO_TYPE_READ, ZIO_PRIORITY_SYNC_READ,
3378b24ab676SJeff Bonwick 		    ZIO_FLAG_DONT_CACHE | ZIO_FLAG_DONT_QUEUE |
3379b24ab676SJeff Bonwick 		    ZIO_FLAG_DONT_PROPAGATE | ZIO_FLAG_DONT_RETRY |
3380b24ab676SJeff Bonwick 		    ZIO_FLAG_CANFAIL | ZIO_FLAG_RAW, NULL, NULL));
3381b24ab676SJeff Bonwick 	}
3382b24ab676SJeff Bonwick 
338344cd46caSbillm 	error = zio_wait(zio);
3384e14bb325SJeff Bonwick 	spa_config_exit(spa, SCL_STATE, FTAG);
338544cd46caSbillm 
338644cd46caSbillm 	if (error) {
338744cd46caSbillm 		(void) printf("Read of %s failed, error: %d\n", thing, error);
338844cd46caSbillm 		goto out;
338944cd46caSbillm 	}
339044cd46caSbillm 
3391b24ab676SJeff Bonwick 	if (flags & ZDB_FLAG_DECOMPRESS) {
3392b24ab676SJeff Bonwick 		/*
3393b24ab676SJeff Bonwick 		 * We don't know how the data was compressed, so just try
3394b24ab676SJeff Bonwick 		 * every decompress function at every inflated blocksize.
3395b24ab676SJeff Bonwick 		 */
3396b24ab676SJeff Bonwick 		enum zio_compress c;
3397b24ab676SJeff Bonwick 		void *pbuf2 = umem_alloc(SPA_MAXBLOCKSIZE, UMEM_NOFAIL);
3398b24ab676SJeff Bonwick 		void *lbuf2 = umem_alloc(SPA_MAXBLOCKSIZE, UMEM_NOFAIL);
3399b24ab676SJeff Bonwick 
3400b24ab676SJeff Bonwick 		bcopy(pbuf, pbuf2, psize);
3401b24ab676SJeff Bonwick 
3402b24ab676SJeff Bonwick 		VERIFY(random_get_pseudo_bytes((uint8_t *)pbuf + psize,
3403b24ab676SJeff Bonwick 		    SPA_MAXBLOCKSIZE - psize) == 0);
3404b24ab676SJeff Bonwick 
3405b24ab676SJeff Bonwick 		VERIFY(random_get_pseudo_bytes((uint8_t *)pbuf2 + psize,
3406b24ab676SJeff Bonwick 		    SPA_MAXBLOCKSIZE - psize) == 0);
3407b24ab676SJeff Bonwick 
3408b24ab676SJeff Bonwick 		for (lsize = SPA_MAXBLOCKSIZE; lsize > psize;
3409b24ab676SJeff Bonwick 		    lsize -= SPA_MINBLOCKSIZE) {
3410b24ab676SJeff Bonwick 			for (c = 0; c < ZIO_COMPRESS_FUNCTIONS; c++) {
3411b24ab676SJeff Bonwick 				if (zio_decompress_data(c, pbuf, lbuf,
3412b24ab676SJeff Bonwick 				    psize, lsize) == 0 &&
3413b24ab676SJeff Bonwick 				    zio_decompress_data(c, pbuf2, lbuf2,
3414b24ab676SJeff Bonwick 				    psize, lsize) == 0 &&
3415b24ab676SJeff Bonwick 				    bcmp(lbuf, lbuf2, lsize) == 0)
3416b24ab676SJeff Bonwick 					break;
3417b24ab676SJeff Bonwick 			}
3418b24ab676SJeff Bonwick 			if (c != ZIO_COMPRESS_FUNCTIONS)
3419b24ab676SJeff Bonwick 				break;
3420b24ab676SJeff Bonwick 			lsize -= SPA_MINBLOCKSIZE;
3421b24ab676SJeff Bonwick 		}
3422b24ab676SJeff Bonwick 
3423b24ab676SJeff Bonwick 		umem_free(pbuf2, SPA_MAXBLOCKSIZE);
3424b24ab676SJeff Bonwick 		umem_free(lbuf2, SPA_MAXBLOCKSIZE);
3425b24ab676SJeff Bonwick 
3426b24ab676SJeff Bonwick 		if (lsize <= psize) {
3427b24ab676SJeff Bonwick 			(void) printf("Decompress of %s failed\n", thing);
3428b24ab676SJeff Bonwick 			goto out;
3429b24ab676SJeff Bonwick 		}
3430b24ab676SJeff Bonwick 		buf = lbuf;
3431b24ab676SJeff Bonwick 		size = lsize;
3432b24ab676SJeff Bonwick 	} else {
3433b24ab676SJeff Bonwick 		buf = pbuf;
3434b24ab676SJeff Bonwick 		size = psize;
3435b24ab676SJeff Bonwick 	}
3436b24ab676SJeff Bonwick 
343744cd46caSbillm 	if (flags & ZDB_FLAG_PRINT_BLKPTR)
343844cd46caSbillm 		zdb_print_blkptr((blkptr_t *)(void *)
343944cd46caSbillm 		    ((uintptr_t)buf + (uintptr_t)blkptr_offset), flags);
344044cd46caSbillm 	else if (flags & ZDB_FLAG_RAW)
344144cd46caSbillm 		zdb_dump_block_raw(buf, size, flags);
344244cd46caSbillm 	else if (flags & ZDB_FLAG_INDIRECT)
344344cd46caSbillm 		zdb_dump_indirect((blkptr_t *)buf, size / sizeof (blkptr_t),
344444cd46caSbillm 		    flags);
344544cd46caSbillm 	else if (flags & ZDB_FLAG_GBH)
344644cd46caSbillm 		zdb_dump_gbh(buf, flags);
344744cd46caSbillm 	else
344844cd46caSbillm 		zdb_dump_block(thing, buf, size, flags);
344944cd46caSbillm 
345044cd46caSbillm out:
3451b24ab676SJeff Bonwick 	umem_free(pbuf, SPA_MAXBLOCKSIZE);
3452b24ab676SJeff Bonwick 	umem_free(lbuf, SPA_MAXBLOCKSIZE);
345344cd46caSbillm 	free(dup);
345444cd46caSbillm }
345544cd46caSbillm 
3456de6628f0Sck static boolean_t
34573ad6c7f9SVictor Latushkin pool_match(nvlist_t *cfg, char *tgt)
3458de6628f0Sck {
34593ad6c7f9SVictor Latushkin 	uint64_t v, guid = strtoull(tgt, NULL, 0);
3460de6628f0Sck 	char *s;
3461de6628f0Sck 
3462de6628f0Sck 	if (guid != 0) {
34633ad6c7f9SVictor Latushkin 		if (nvlist_lookup_uint64(cfg, ZPOOL_CONFIG_POOL_GUID, &v) == 0)
34643ad6c7f9SVictor Latushkin 			return (v == guid);
3465de6628f0Sck 	} else {
34663ad6c7f9SVictor Latushkin 		if (nvlist_lookup_string(cfg, ZPOOL_CONFIG_POOL_NAME, &s) == 0)
34673ad6c7f9SVictor Latushkin 			return (strcmp(s, tgt) == 0);
3468de6628f0Sck 	}
34693ad6c7f9SVictor Latushkin 	return (B_FALSE);
3470de6628f0Sck }
3471de6628f0Sck 
34723ad6c7f9SVictor Latushkin static char *
34733ad6c7f9SVictor Latushkin find_zpool(char **target, nvlist_t **configp, int dirc, char **dirv)
3474de6628f0Sck {
3475de6628f0Sck 	nvlist_t *pools;
3476de6628f0Sck 	nvlist_t *match = NULL;
34773ad6c7f9SVictor Latushkin 	char *name = NULL;
34783ad6c7f9SVictor Latushkin 	char *sepp = NULL;
34799adfa60dSMatthew Ahrens 	char sep = '\0';
34803ad6c7f9SVictor Latushkin 	int count = 0;
3481d41c4376SMark J Musante 	importargs_t args = { 0 };
3482d41c4376SMark J Musante 
3483d41c4376SMark J Musante 	args.paths = dirc;
3484d41c4376SMark J Musante 	args.path = dirv;
3485d41c4376SMark J Musante 	args.can_be_active = B_TRUE;
3486de6628f0Sck 
34873ad6c7f9SVictor Latushkin 	if ((sepp = strpbrk(*target, "/@")) != NULL) {
34883ad6c7f9SVictor Latushkin 		sep = *sepp;
34893ad6c7f9SVictor Latushkin 		*sepp = '\0';
34903ad6c7f9SVictor Latushkin 	}
34913ad6c7f9SVictor Latushkin 
3492d41c4376SMark J Musante 	pools = zpool_search_import(g_zfs, &args);
3493de6628f0Sck 
3494de6628f0Sck 	if (pools != NULL) {
3495de6628f0Sck 		nvpair_t *elem = NULL;
3496de6628f0Sck 		while ((elem = nvlist_next_nvpair(pools, elem)) != NULL) {
3497de6628f0Sck 			verify(nvpair_value_nvlist(elem, configp) == 0);
34983ad6c7f9SVictor Latushkin 			if (pool_match(*configp, *target)) {
34993ad6c7f9SVictor Latushkin 				count++;
3500de6628f0Sck 				if (match != NULL) {
35013ad6c7f9SVictor Latushkin 					/* print previously found config */
35023ad6c7f9SVictor Latushkin 					if (name != NULL) {
35033ad6c7f9SVictor Latushkin 						(void) printf("%s\n", name);
35043ad6c7f9SVictor Latushkin 						dump_nvlist(match, 8);
35053ad6c7f9SVictor Latushkin 						name = NULL;
35063ad6c7f9SVictor Latushkin 					}
35073ad6c7f9SVictor Latushkin 					(void) printf("%s\n",
35083ad6c7f9SVictor Latushkin 					    nvpair_name(elem));
35093ad6c7f9SVictor Latushkin 					dump_nvlist(*configp, 8);
3510de6628f0Sck 				} else {
3511de6628f0Sck 					match = *configp;
35123ad6c7f9SVictor Latushkin 					name = nvpair_name(elem);
3513de6628f0Sck 				}
3514de6628f0Sck 			}
3515de6628f0Sck 		}
3516de6628f0Sck 	}
35173ad6c7f9SVictor Latushkin 	if (count > 1)
35183ad6c7f9SVictor Latushkin 		(void) fatal("\tMatched %d pools - use pool GUID "
35193ad6c7f9SVictor Latushkin 		    "instead of pool name or \n"
35203ad6c7f9SVictor Latushkin 		    "\tpool name part of a dataset name to select pool", count);
35213ad6c7f9SVictor Latushkin 
35223ad6c7f9SVictor Latushkin 	if (sepp)
35233ad6c7f9SVictor Latushkin 		*sepp = sep;
35243ad6c7f9SVictor Latushkin 	/*
35253ad6c7f9SVictor Latushkin 	 * If pool GUID was specified for pool id, replace it with pool name
35263ad6c7f9SVictor Latushkin 	 */
35273ad6c7f9SVictor Latushkin 	if (name && (strstr(*target, name) != *target)) {
35283ad6c7f9SVictor Latushkin 		int sz = 1 + strlen(name) + ((sepp) ? strlen(sepp) : 0);
35293ad6c7f9SVictor Latushkin 
35303ad6c7f9SVictor Latushkin 		*target = umem_alloc(sz, UMEM_NOFAIL);
35313ad6c7f9SVictor Latushkin 		(void) snprintf(*target, sz, "%s%s", name, sepp ? sepp : "");
35323ad6c7f9SVictor Latushkin 	}
3533de6628f0Sck 
35343ad6c7f9SVictor Latushkin 	*configp = name ? match : NULL;
3535de6628f0Sck 
35363ad6c7f9SVictor Latushkin 	return (name);
3537de6628f0Sck }
3538de6628f0Sck 
3539fa9e4066Sahrens int
3540fa9e4066Sahrens main(int argc, char **argv)
3541fa9e4066Sahrens {
3542fa9e4066Sahrens 	int i, c;
3543fa9e4066Sahrens 	struct rlimit rl = { 1024, 1024 };
35443ad6c7f9SVictor Latushkin 	spa_t *spa = NULL;
3545fa9e4066Sahrens 	objset_t *os = NULL;
3546fa9e4066Sahrens 	int dump_all = 1;
3547fa9e4066Sahrens 	int verbose = 0;
3548c8ee1847SVictor Latushkin 	int error = 0;
35493ad6c7f9SVictor Latushkin 	char **searchdirs = NULL;
35503ad6c7f9SVictor Latushkin 	int nsearch = 0;
35513ad6c7f9SVictor Latushkin 	char *target;
3552468c413aSTim Haley 	nvlist_t *policy = NULL;
3553468c413aSTim Haley 	uint64_t max_txg = UINT64_MAX;
3554c8ee1847SVictor Latushkin 	int rewind = ZPOOL_NEVER_REWIND;
3555ae24175bSCyril Plisko 	char *spa_config_path_env;
3556b702644aSTim Chase 	boolean_t target_is_spa = B_TRUE;
3557fa9e4066Sahrens 
3558fa9e4066Sahrens 	(void) setrlimit(RLIMIT_NOFILE, &rl);
3559004388ebScasper 	(void) enable_extended_FILE_stdio(-1, -1);
3560fa9e4066Sahrens 
3561fa9e4066Sahrens 	dprintf_setup(&argc, argv);
3562fa9e4066Sahrens 
3563ae24175bSCyril Plisko 	/*
3564ae24175bSCyril Plisko 	 * If there is an environment variable SPA_CONFIG_PATH it overrides
3565ae24175bSCyril Plisko 	 * default spa_config_path setting. If -U flag is specified it will
3566ae24175bSCyril Plisko 	 * override this environment variable settings once again.
3567ae24175bSCyril Plisko 	 */
3568ae24175bSCyril Plisko 	spa_config_path_env = getenv("SPA_CONFIG_PATH");
3569ae24175bSCyril Plisko 	if (spa_config_path_env != NULL)
3570ae24175bSCyril Plisko 		spa_config_path = spa_config_path_env;
3571ae24175bSCyril Plisko 
3572df15e419SMatthew Ahrens 	while ((c = getopt(argc, argv,
35730e60744cSPavel Zakharov 	    "bcdhilmMI:suCDRSAFLXx:evp:t:U:PGo:")) != -1) {
3574fa9e4066Sahrens 		switch (c) {
3575fa9e4066Sahrens 		case 'b':
3576fa9e4066Sahrens 		case 'c':
3577b24ab676SJeff Bonwick 		case 'd':
3578b24ab676SJeff Bonwick 		case 'h':
3579b24ab676SJeff Bonwick 		case 'i':
3580b24ab676SJeff Bonwick 		case 'l':
3581d6e555bdSGeorge Wilson 		case 'm':
3582fa9e4066Sahrens 		case 's':
3583b24ab676SJeff Bonwick 		case 'u':
3584fa9e4066Sahrens 		case 'C':
3585b24ab676SJeff Bonwick 		case 'D':
35862e4c9986SGeorge Wilson 		case 'M':
358744cd46caSbillm 		case 'R':
3588b24ab676SJeff Bonwick 		case 'S':
358929bdd2f9SPavel Zakharov 		case 'G':
3590fa9e4066Sahrens 			dump_opt[c]++;
3591fa9e4066Sahrens 			dump_all = 0;
3592fa9e4066Sahrens 			break;
3593feef89cfSVictor Latushkin 		case 'A':
3594c8ee1847SVictor Latushkin 		case 'F':
359582a0a985SVictor Latushkin 		case 'L':
3596c8ee1847SVictor Latushkin 		case 'X':
35973ad6c7f9SVictor Latushkin 		case 'e':
35983f9d6ad7SLin Ling 		case 'P':
359982a0a985SVictor Latushkin 			dump_opt[c]++;
360082a0a985SVictor Latushkin 			break;
36012e4c9986SGeorge Wilson 		case 'I':
360231d7e8faSGeorge Wilson 			max_inflight = strtoull(optarg, NULL, 0);
360331d7e8faSGeorge Wilson 			if (max_inflight == 0) {
360431d7e8faSGeorge Wilson 				(void) fprintf(stderr, "maximum number "
360531d7e8faSGeorge Wilson 				    "of inflight I/Os must be greater "
360631d7e8faSGeorge Wilson 				    "than 0\n");
360731d7e8faSGeorge Wilson 				usage();
360831d7e8faSGeorge Wilson 			}
360931d7e8faSGeorge Wilson 			break;
3610de6628f0Sck 		case 'p':
36113ad6c7f9SVictor Latushkin 			if (searchdirs == NULL) {
36123ad6c7f9SVictor Latushkin 				searchdirs = umem_alloc(sizeof (char *),
36133ad6c7f9SVictor Latushkin 				    UMEM_NOFAIL);
36143ad6c7f9SVictor Latushkin 			} else {
36153ad6c7f9SVictor Latushkin 				char **tmp = umem_alloc((nsearch + 1) *
36163ad6c7f9SVictor Latushkin 				    sizeof (char *), UMEM_NOFAIL);
36173ad6c7f9SVictor Latushkin 				bcopy(searchdirs, tmp, nsearch *
36183ad6c7f9SVictor Latushkin 				    sizeof (char *));
36193ad6c7f9SVictor Latushkin 				umem_free(searchdirs,
36203ad6c7f9SVictor Latushkin 				    nsearch * sizeof (char *));
36213ad6c7f9SVictor Latushkin 				searchdirs = tmp;
36223ad6c7f9SVictor Latushkin 			}
36233ad6c7f9SVictor Latushkin 			searchdirs[nsearch++] = optarg;
3624de6628f0Sck 			break;
36252e551927SVictor Latushkin 		case 't':
3626468c413aSTim Haley 			max_txg = strtoull(optarg, NULL, 0);
3627468c413aSTim Haley 			if (max_txg < TXG_INITIAL) {
36282e551927SVictor Latushkin 				(void) fprintf(stderr, "incorrect txg "
36292e551927SVictor Latushkin 				    "specified: %s\n", optarg);
36302e551927SVictor Latushkin 				usage();
36312e551927SVictor Latushkin 			}
36322e551927SVictor Latushkin 			break;
3633b24ab676SJeff Bonwick 		case 'U':
3634b24ab676SJeff Bonwick 			spa_config_path = optarg;
3635b24ab676SJeff Bonwick 			break;
36362e4c9986SGeorge Wilson 		case 'v':
36372e4c9986SGeorge Wilson 			verbose++;
36382e4c9986SGeorge Wilson 			break;
36392e4c9986SGeorge Wilson 		case 'x':
36402e4c9986SGeorge Wilson 			vn_dumpdir = optarg;
36412e4c9986SGeorge Wilson 			break;
36420e60744cSPavel Zakharov 		case 'o':
36430e60744cSPavel Zakharov 			error = set_global_var(optarg);
36440e60744cSPavel Zakharov 			if (error != 0)
36450e60744cSPavel Zakharov 				usage();
36460e60744cSPavel Zakharov 			break;
3647fa9e4066Sahrens 		default:
3648fa9e4066Sahrens 			usage();
3649fa9e4066Sahrens 			break;
3650fa9e4066Sahrens 		}
3651fa9e4066Sahrens 	}
3652fa9e4066Sahrens 
36533ad6c7f9SVictor Latushkin 	if (!dump_opt['e'] && searchdirs != NULL) {
365488b7b0f2SMatthew Ahrens 		(void) fprintf(stderr, "-p option requires use of -e\n");
365588b7b0f2SMatthew Ahrens 		usage();
365688b7b0f2SMatthew Ahrens 	}
3657de6628f0Sck 
365806be9802SMatthew Ahrens 	/*
365906be9802SMatthew Ahrens 	 * ZDB does not typically re-read blocks; therefore limit the ARC
366006be9802SMatthew Ahrens 	 * to 256 MB, which can be used entirely for metadata.
366106be9802SMatthew Ahrens 	 */
366206be9802SMatthew Ahrens 	zfs_arc_max = zfs_arc_meta_limit = 256 * 1024 * 1024;
366306be9802SMatthew Ahrens 
3664f7950bf1SMatthew Ahrens 	/*
3665f7950bf1SMatthew Ahrens 	 * "zdb -c" uses checksum-verifying scrub i/os which are async reads.
3666f7950bf1SMatthew Ahrens 	 * "zdb -b" uses traversal prefetch which uses async reads.
3667f7950bf1SMatthew Ahrens 	 * For good performance, let several of them be active at once.
3668f7950bf1SMatthew Ahrens 	 */
3669f7950bf1SMatthew Ahrens 	zfs_vdev_async_read_max_active = 10;
3670f7950bf1SMatthew Ahrens 
3671*4dd77f9eSMatthew Ahrens 	/*
3672*4dd77f9eSMatthew Ahrens 	 * Disable reference tracking for better performance.
3673*4dd77f9eSMatthew Ahrens 	 */
3674*4dd77f9eSMatthew Ahrens 	reference_tracking_enable = B_FALSE;
3675*4dd77f9eSMatthew Ahrens 
3676fa9e4066Sahrens 	kernel_init(FREAD);
3677de6628f0Sck 	g_zfs = libzfs_init();
367891ebeef5Sahrens 	ASSERT(g_zfs != NULL);
3679fa9e4066Sahrens 
3680b24ab676SJeff Bonwick 	if (dump_all)
3681b24ab676SJeff Bonwick 		verbose = MAX(verbose, 1);
3682b24ab676SJeff Bonwick 
3683fa9e4066Sahrens 	for (c = 0; c < 256; c++) {
36843f9d6ad7SLin Ling 		if (dump_all && !strchr("elAFLRSXP", c))
3685fa9e4066Sahrens 			dump_opt[c] = 1;
3686fa9e4066Sahrens 		if (dump_opt[c])
3687fa9e4066Sahrens 			dump_opt[c] += verbose;
3688fa9e4066Sahrens 	}
3689fa9e4066Sahrens 
3690feef89cfSVictor Latushkin 	aok = (dump_opt['A'] == 1) || (dump_opt['A'] > 2);
3691feef89cfSVictor Latushkin 	zfs_recover = (dump_opt['A'] > 1);
3692feef89cfSVictor Latushkin 
3693fa9e4066Sahrens 	argc -= optind;
3694fa9e4066Sahrens 	argv += optind;
3695fa9e4066Sahrens 
369607428bdfSVictor Latushkin 	if (argc < 2 && dump_opt['R'])
369707428bdfSVictor Latushkin 		usage();
3698fa9e4066Sahrens 	if (argc < 1) {
36993ad6c7f9SVictor Latushkin 		if (!dump_opt['e'] && dump_opt['C']) {
3700e829d913Sck 			dump_cachefile(spa_config_path);
3701fa9e4066Sahrens 			return (0);
3702fa9e4066Sahrens 		}
3703fa9e4066Sahrens 		usage();
3704fa9e4066Sahrens 	}
3705fa9e4066Sahrens 
3706fa9e4066Sahrens 	if (dump_opt['l']) {
3707fa9e4066Sahrens 		dump_label(argv[0]);
3708fa9e4066Sahrens 		return (0);
3709fa9e4066Sahrens 	}
3710fa9e4066Sahrens 
3711c8ee1847SVictor Latushkin 	if (dump_opt['X'] || dump_opt['F'])
3712c8ee1847SVictor Latushkin 		rewind = ZPOOL_DO_REWIND |
3713c8ee1847SVictor Latushkin 		    (dump_opt['X'] ? ZPOOL_EXTREME_REWIND : 0);
3714c8ee1847SVictor Latushkin 
3715c8ee1847SVictor Latushkin 	if (nvlist_alloc(&policy, NV_UNIQUE_NAME_TYPE, 0) != 0 ||
3716c8ee1847SVictor Latushkin 	    nvlist_add_uint64(policy, ZPOOL_REWIND_REQUEST_TXG, max_txg) != 0 ||
3717c8ee1847SVictor Latushkin 	    nvlist_add_uint32(policy, ZPOOL_REWIND_REQUEST, rewind) != 0)
3718c8ee1847SVictor Latushkin 		fatal("internal error: %s", strerror(ENOMEM));
3719c8ee1847SVictor Latushkin 
3720c5904d13Seschrock 	error = 0;
37213ad6c7f9SVictor Latushkin 	target = argv[0];
3722990b4856Slling 
37233ad6c7f9SVictor Latushkin 	if (dump_opt['e']) {
37243ad6c7f9SVictor Latushkin 		nvlist_t *cfg = NULL;
37253ad6c7f9SVictor Latushkin 		char *name = find_zpool(&target, &cfg, nsearch, searchdirs);
3726990b4856Slling 
37273ad6c7f9SVictor Latushkin 		error = ENOENT;
37283ad6c7f9SVictor Latushkin 		if (name) {
372907428bdfSVictor Latushkin 			if (dump_opt['C'] > 1) {
373007428bdfSVictor Latushkin 				(void) printf("\nConfiguration for import:\n");
373107428bdfSVictor Latushkin 				dump_nvlist(cfg, 8);
373207428bdfSVictor Latushkin 			}
3733c8ee1847SVictor Latushkin 			if (nvlist_add_nvlist(cfg,
3734468c413aSTim Haley 			    ZPOOL_REWIND_POLICY, policy) != 0) {
3735468c413aSTim Haley 				fatal("can't open '%s': %s",
3736468c413aSTim Haley 				    target, strerror(ENOMEM));
3737468c413aSTim Haley 			}
37384b964adaSGeorge Wilson 			if ((error = spa_import(name, cfg, NULL,
37394b964adaSGeorge Wilson 			    ZFS_IMPORT_MISSING_LOG)) != 0) {
37404b964adaSGeorge Wilson 				error = spa_import(name, cfg, NULL,
37414b964adaSGeorge Wilson 				    ZFS_IMPORT_VERBATIM);
37424b964adaSGeorge Wilson 			}
3743de6628f0Sck 		}
3744c5904d13Seschrock 	}
3745c5904d13Seschrock 
3746b702644aSTim Chase 	if (strpbrk(target, "/@") != NULL) {
3747b702644aSTim Chase 		size_t targetlen;
3748b702644aSTim Chase 
3749b702644aSTim Chase 		target_is_spa = B_FALSE;
3750b702644aSTim Chase 		/*
3751b702644aSTim Chase 		 * Remove any trailing slash.  Later code would get confused
3752b702644aSTim Chase 		 * by it, but we want to allow it so that "pool/" can
3753b702644aSTim Chase 		 * indicate that we want to dump the topmost filesystem,
3754b702644aSTim Chase 		 * rather than the whole pool.
3755b702644aSTim Chase 		 */
3756b702644aSTim Chase 		targetlen = strlen(target);
3757b702644aSTim Chase 		if (targetlen != 0 && target[targetlen - 1] == '/')
3758b702644aSTim Chase 			target[targetlen - 1] = '\0';
3759b702644aSTim Chase 	}
3760b702644aSTim Chase 
3761c5904d13Seschrock 	if (error == 0) {
3762b702644aSTim Chase 		if (target_is_spa || dump_opt['R']) {
376380eb36f2SGeorge Wilson 			error = spa_open_rewind(target, &spa, FTAG, policy,
376480eb36f2SGeorge Wilson 			    NULL);
37658f18d1faSGeorge Wilson 			if (error) {
37668f18d1faSGeorge Wilson 				/*
37678f18d1faSGeorge Wilson 				 * If we're missing the log device then
37688f18d1faSGeorge Wilson 				 * try opening the pool after clearing the
37698f18d1faSGeorge Wilson 				 * log state.
37708f18d1faSGeorge Wilson 				 */
37718f18d1faSGeorge Wilson 				mutex_enter(&spa_namespace_lock);
37723ad6c7f9SVictor Latushkin 				if ((spa = spa_lookup(target)) != NULL &&
37738f18d1faSGeorge Wilson 				    spa->spa_log_state == SPA_LOG_MISSING) {
37748f18d1faSGeorge Wilson 					spa->spa_log_state = SPA_LOG_CLEAR;
37758f18d1faSGeorge Wilson 					error = 0;
37768f18d1faSGeorge Wilson 				}
37778f18d1faSGeorge Wilson 				mutex_exit(&spa_namespace_lock);
37788f18d1faSGeorge Wilson 
377980eb36f2SGeorge Wilson 				if (!error) {
378080eb36f2SGeorge Wilson 					error = spa_open_rewind(target, &spa,
378180eb36f2SGeorge Wilson 					    FTAG, policy, NULL);
378280eb36f2SGeorge Wilson 				}
37838f18d1faSGeorge Wilson 			}
378407428bdfSVictor Latushkin 		} else {
378507428bdfSVictor Latushkin 			error = dmu_objset_own(target, DMU_OST_ANY,
378607428bdfSVictor Latushkin 			    B_TRUE, FTAG, &os);
3787c5904d13Seschrock 		}
3788fa9e4066Sahrens 	}
378980eb36f2SGeorge Wilson 	nvlist_free(policy);
379080eb36f2SGeorge Wilson 
3791fa9e4066Sahrens 	if (error)
37923ad6c7f9SVictor Latushkin 		fatal("can't open '%s': %s", target, strerror(error));
3793fa9e4066Sahrens 
3794fa9e4066Sahrens 	argv++;
379507428bdfSVictor Latushkin 	argc--;
379607428bdfSVictor Latushkin 	if (!dump_opt['R']) {
379707428bdfSVictor Latushkin 		if (argc > 0) {
379807428bdfSVictor Latushkin 			zopt_objects = argc;
379907428bdfSVictor Latushkin 			zopt_object = calloc(zopt_objects, sizeof (uint64_t));
380007428bdfSVictor Latushkin 			for (i = 0; i < zopt_objects; i++) {
380107428bdfSVictor Latushkin 				errno = 0;
380207428bdfSVictor Latushkin 				zopt_object[i] = strtoull(argv[i], NULL, 0);
380307428bdfSVictor Latushkin 				if (zopt_object[i] == 0 && errno != 0)
380487219db7SVictor Latushkin 					fatal("bad number %s: %s",
380507428bdfSVictor Latushkin 					    argv[i], strerror(errno));
380607428bdfSVictor Latushkin 			}
3807fa9e4066Sahrens 		}
3808e690fb27SChristopher Siden 		if (os != NULL) {
3809e690fb27SChristopher Siden 			dump_dir(os);
3810e690fb27SChristopher Siden 		} else if (zopt_objects > 0 && !dump_opt['m']) {
3811e690fb27SChristopher Siden 			dump_dir(spa->spa_meta_objset);
3812e690fb27SChristopher Siden 		} else {
3813e690fb27SChristopher Siden 			dump_zpool(spa);
3814e690fb27SChristopher Siden 		}
3815fa9e4066Sahrens 	} else {
381607428bdfSVictor Latushkin 		flagbits['b'] = ZDB_FLAG_PRINT_BLKPTR;
381707428bdfSVictor Latushkin 		flagbits['c'] = ZDB_FLAG_CHECKSUM;
381807428bdfSVictor Latushkin 		flagbits['d'] = ZDB_FLAG_DECOMPRESS;
381907428bdfSVictor Latushkin 		flagbits['e'] = ZDB_FLAG_BSWAP;
382007428bdfSVictor Latushkin 		flagbits['g'] = ZDB_FLAG_GBH;
382107428bdfSVictor Latushkin 		flagbits['i'] = ZDB_FLAG_INDIRECT;
382207428bdfSVictor Latushkin 		flagbits['p'] = ZDB_FLAG_PHYS;
382307428bdfSVictor Latushkin 		flagbits['r'] = ZDB_FLAG_RAW;
382407428bdfSVictor Latushkin 
382507428bdfSVictor Latushkin 		for (i = 0; i < argc; i++)
382607428bdfSVictor Latushkin 			zdb_read_block(argv[i], spa);
3827fa9e4066Sahrens 	}
3828fa9e4066Sahrens 
382907428bdfSVictor Latushkin 	(os != NULL) ? dmu_objset_disown(os, FTAG) : spa_close(spa, FTAG);
383007428bdfSVictor Latushkin 
3831e0d35c44Smarks 	fuid_table_destroy();
38320a586ceaSMark Shellenbaum 	sa_loaded = B_FALSE;
3833e0d35c44Smarks 
383429bdd2f9SPavel Zakharov 	dump_debug_buffer();
383529bdd2f9SPavel Zakharov 
3836de6628f0Sck 	libzfs_fini(g_zfs);
3837fa9e4066Sahrens 	kernel_fini();
3838fa9e4066Sahrens 
3839fa9e4066Sahrens 	return (0);
3840fa9e4066Sahrens }
3841