xref: /illumos-gate/usr/src/cmd/zdb/zdb.c (revision ae24175b)
1fa9e4066Sahrens /*
2fa9e4066Sahrens  * CDDL HEADER START
3fa9e4066Sahrens  *
4fa9e4066Sahrens  * The contents of this file are subject to the terms of the
5ea8dc4b6Seschrock  * Common Development and Distribution License (the "License").
6ea8dc4b6Seschrock  * You may not use this file except in compliance with the License.
7fa9e4066Sahrens  *
8fa9e4066Sahrens  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9fa9e4066Sahrens  * or http://www.opensolaris.org/os/licensing.
10fa9e4066Sahrens  * See the License for the specific language governing permissions
11fa9e4066Sahrens  * and limitations under the License.
12fa9e4066Sahrens  *
13fa9e4066Sahrens  * When distributing Covered Code, include this CDDL HEADER in each
14fa9e4066Sahrens  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15fa9e4066Sahrens  * If applicable, add the following below this CDDL HEADER, with the
16fa9e4066Sahrens  * fields enclosed by brackets "[]" replaced with your own identifying
17fa9e4066Sahrens  * information: Portions Copyright [yyyy] [name of copyright owner]
18fa9e4066Sahrens  *
19fa9e4066Sahrens  * CDDL HEADER END
20fa9e4066Sahrens  */
21ad135b5dSChristopher Siden 
22fa9e4066Sahrens /*
238f2529deSMark Shellenbaum  * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
24ca0cc391SMatthew Ahrens  * Copyright (c) 2011, 2015 by Delphix. All rights reserved.
25fa9e4066Sahrens  */
26fa9e4066Sahrens 
27fa9e4066Sahrens #include <stdio.h>
28490d05b9SMatthew Ahrens #include <unistd.h>
29004388ebScasper #include <stdio_ext.h>
30fa9e4066Sahrens #include <stdlib.h>
3144cd46caSbillm #include <ctype.h>
32fa9e4066Sahrens #include <sys/zfs_context.h>
33fa9e4066Sahrens #include <sys/spa.h>
34fa9e4066Sahrens #include <sys/spa_impl.h>
35fa9e4066Sahrens #include <sys/dmu.h>
36fa9e4066Sahrens #include <sys/zap.h>
37fa9e4066Sahrens #include <sys/fs/zfs.h>
38fa9e4066Sahrens #include <sys/zfs_znode.h>
390a586ceaSMark Shellenbaum #include <sys/zfs_sa.h>
400a586ceaSMark Shellenbaum #include <sys/sa.h>
410a586ceaSMark Shellenbaum #include <sys/sa_impl.h>
42fa9e4066Sahrens #include <sys/vdev.h>
43fa9e4066Sahrens #include <sys/vdev_impl.h>
44fa9e4066Sahrens #include <sys/metaslab_impl.h>
45fa9e4066Sahrens #include <sys/dmu_objset.h>
46fa9e4066Sahrens #include <sys/dsl_dir.h>
47fa9e4066Sahrens #include <sys/dsl_dataset.h>
48fa9e4066Sahrens #include <sys/dsl_pool.h>
49fa9e4066Sahrens #include <sys/dbuf.h>
50fa9e4066Sahrens #include <sys/zil.h>
51fa9e4066Sahrens #include <sys/zil_impl.h>
52fa9e4066Sahrens #include <sys/stat.h>
53fa9e4066Sahrens #include <sys/resource.h>
54fa9e4066Sahrens #include <sys/dmu_traverse.h>
55fa9e4066Sahrens #include <sys/zio_checksum.h>
56fa9e4066Sahrens #include <sys/zio_compress.h>
57e0d35c44Smarks #include <sys/zfs_fuid.h>
5888b7b0f2SMatthew Ahrens #include <sys/arc.h>
59b24ab676SJeff Bonwick #include <sys/ddt.h>
60ad135b5dSChristopher Siden #include <sys/zfeature.h>
614445fffbSMatthew Ahrens #include <zfs_comutil.h>
62de6628f0Sck #undef ZFS_MAXNAMELEN
63de6628f0Sck #undef verify
64de6628f0Sck #include <libzfs.h>
65fa9e4066Sahrens 
66e690fb27SChristopher Siden #define	ZDB_COMPRESS_NAME(idx) ((idx) < ZIO_COMPRESS_FUNCTIONS ?	\
67e690fb27SChristopher Siden 	zio_compress_table[(idx)].ci_name : "UNKNOWN")
68e690fb27SChristopher Siden #define	ZDB_CHECKSUM_NAME(idx) ((idx) < ZIO_CHECKSUM_FUNCTIONS ?	\
69e690fb27SChristopher Siden 	zio_checksum_table[(idx)].ci_name : "UNKNOWN")
70e690fb27SChristopher Siden #define	ZDB_OT_NAME(idx) ((idx) < DMU_OT_NUMTYPES ?	\
71e690fb27SChristopher Siden 	dmu_ot[(idx)].ot_name : DMU_OT_IS_VALID(idx) ?	\
72e690fb27SChristopher Siden 	dmu_ot_byteswap[DMU_OT_BYTESWAP(idx)].ob_name : "UNKNOWN")
73e690fb27SChristopher Siden #define	ZDB_OT_TYPE(idx) ((idx) < DMU_OT_NUMTYPES ? (idx) :		\
74e690fb27SChristopher Siden 	(((idx) == DMU_OTN_ZAP_DATA || (idx) == DMU_OTN_ZAP_METADATA) ?	\
75e690fb27SChristopher Siden 	DMU_OT_ZAP_OTHER : DMU_OT_NUMTYPES))
766de8f417SVictor Latushkin 
77feef89cfSVictor Latushkin #ifndef lint
787fd05ac4SMatthew Ahrens extern boolean_t zfs_recover;
7906be9802SMatthew Ahrens extern uint64_t zfs_arc_max, zfs_arc_meta_limit;
80f7950bf1SMatthew Ahrens extern int zfs_vdev_async_read_max_active;
81feef89cfSVictor Latushkin #else
827fd05ac4SMatthew Ahrens boolean_t zfs_recover;
8306be9802SMatthew Ahrens uint64_t zfs_arc_max, zfs_arc_meta_limit;
84f7950bf1SMatthew Ahrens int zfs_vdev_async_read_max_active;
85feef89cfSVictor Latushkin #endif
86feef89cfSVictor Latushkin 
87fa9e4066Sahrens const char cmdname[] = "zdb";
88fa9e4066Sahrens uint8_t dump_opt[256];
89fa9e4066Sahrens 
90fa9e4066Sahrens typedef void object_viewer_t(objset_t *, uint64_t, void *data, size_t size);
91fa9e4066Sahrens 
92fa9e4066Sahrens extern void dump_intent_log(zilog_t *);
93fa9e4066Sahrens uint64_t *zopt_object = NULL;
94fa9e4066Sahrens int zopt_objects = 0;
95de6628f0Sck libzfs_handle_t *g_zfs;
9606be9802SMatthew Ahrens uint64_t max_inflight = 1000;
97fa9e4066Sahrens 
98732885fcSMatthew Ahrens static void snprintf_blkptr_compact(char *, size_t, const blkptr_t *);
99732885fcSMatthew Ahrens 
100fa9e4066Sahrens /*
101fa9e4066Sahrens  * These libumem hooks provide a reasonable set of defaults for the allocator's
102fa9e4066Sahrens  * debugging facilities.
103fa9e4066Sahrens  */
104fa9e4066Sahrens const char *
105fa9e4066Sahrens _umem_debug_init()
106fa9e4066Sahrens {
107fa9e4066Sahrens 	return ("default,verbose"); /* $UMEM_DEBUG setting */
108fa9e4066Sahrens }
109fa9e4066Sahrens 
110fa9e4066Sahrens const char *
111fa9e4066Sahrens _umem_logging_init(void)
112fa9e4066Sahrens {
113fa9e4066Sahrens 	return ("fail,contents"); /* $UMEM_LOGGING setting */
114fa9e4066Sahrens }
115fa9e4066Sahrens 
116fa9e4066Sahrens static void
117fa9e4066Sahrens usage(void)
118fa9e4066Sahrens {
119fa9e4066Sahrens 	(void) fprintf(stderr,
1202e4c9986SGeorge Wilson 	    "Usage: %s [-CumMdibcsDvhLXFPA] [-t txg] [-e [-p path...]] "
1212e4c9986SGeorge Wilson 	    "[-U config] [-I inflight I/Os] [-x dumpdir] poolname [object...]\n"
12231d7e8faSGeorge Wilson 	    "       %s [-divPA] [-e -p path...] [-U config] dataset "
12331d7e8faSGeorge Wilson 	    "[object...]\n"
1242e4c9986SGeorge Wilson 	    "       %s -mM [-LXFPA] [-t txg] [-e [-p path...]] [-U config] "
12590e894e2SRichard Lowe 	    "poolname [vdev [metaslab...]]\n"
12690e894e2SRichard Lowe 	    "       %s -R [-A] [-e [-p path...]] poolname "
12790e894e2SRichard Lowe 	    "vdev:offset:size[:flags]\n"
12831d7e8faSGeorge Wilson 	    "       %s -S [-PA] [-e [-p path...]] [-U config] poolname\n"
12990e894e2SRichard Lowe 	    "       %s -l [-uA] device\n"
13090e894e2SRichard Lowe 	    "       %s -C [-A] [-U config]\n\n",
131bbfd46c4SJeff Bonwick 	    cmdname, cmdname, cmdname, cmdname, cmdname, cmdname, cmdname);
1323ad6c7f9SVictor Latushkin 
1333ad6c7f9SVictor Latushkin 	(void) fprintf(stderr, "    Dataset name must include at least one "
1343ad6c7f9SVictor Latushkin 	    "separator character '/' or '@'\n");
1353ad6c7f9SVictor Latushkin 	(void) fprintf(stderr, "    If dataset name is specified, only that "
1363ad6c7f9SVictor Latushkin 	    "dataset is dumped\n");
1373ad6c7f9SVictor Latushkin 	(void) fprintf(stderr, "    If object numbers are specified, only "
1383ad6c7f9SVictor Latushkin 	    "those objects are dumped\n\n");
1393ad6c7f9SVictor Latushkin 	(void) fprintf(stderr, "    Options to control amount of output:\n");
1403ad6c7f9SVictor Latushkin 	(void) fprintf(stderr, "        -u uberblock\n");
1413ad6c7f9SVictor Latushkin 	(void) fprintf(stderr, "        -d dataset(s)\n");
1423ad6c7f9SVictor Latushkin 	(void) fprintf(stderr, "        -i intent logs\n");
14307428bdfSVictor Latushkin 	(void) fprintf(stderr, "        -C config (or cachefile if alone)\n");
1448f18d1faSGeorge Wilson 	(void) fprintf(stderr, "        -h pool history\n");
1453ad6c7f9SVictor Latushkin 	(void) fprintf(stderr, "        -b block statistics\n");
1463ad6c7f9SVictor Latushkin 	(void) fprintf(stderr, "        -m metaslabs\n");
1472e4c9986SGeorge Wilson 	(void) fprintf(stderr, "        -M metaslab groups\n");
1483ad6c7f9SVictor Latushkin 	(void) fprintf(stderr, "        -c checksum all metadata (twice for "
1496365109dSVictor Latushkin 	    "all data) blocks\n");
1503ad6c7f9SVictor Latushkin 	(void) fprintf(stderr, "        -s report stats on zdb's I/O\n");
151f0ba89beSJeff Bonwick 	(void) fprintf(stderr, "        -D dedup statistics\n");
152b24ab676SJeff Bonwick 	(void) fprintf(stderr, "        -S simulate dedup to measure effect\n");
1533ad6c7f9SVictor Latushkin 	(void) fprintf(stderr, "        -v verbose (applies to all others)\n");
154fa9e4066Sahrens 	(void) fprintf(stderr, "        -l dump label contents\n");
15582a0a985SVictor Latushkin 	(void) fprintf(stderr, "        -L disable leak tracking (do not "
15682a0a985SVictor Latushkin 	    "load spacemaps)\n");
157d41e7643Sek 	(void) fprintf(stderr, "        -R read and display block from a "
1583ad6c7f9SVictor Latushkin 	    "device\n\n");
1593ad6c7f9SVictor Latushkin 	(void) fprintf(stderr, "    Below options are intended for use "
160df15e419SMatthew Ahrens 	    "with other options:\n");
161feef89cfSVictor Latushkin 	(void) fprintf(stderr, "        -A ignore assertions (-A), enable "
162feef89cfSVictor Latushkin 	    "panic recovery (-AA) or both (-AAA)\n");
163c8ee1847SVictor Latushkin 	(void) fprintf(stderr, "        -F attempt automatic rewind within "
164c8ee1847SVictor Latushkin 	    "safe range of transaction groups\n");
1653ad6c7f9SVictor Latushkin 	(void) fprintf(stderr, "        -U <cachefile_path> -- use alternate "
1663ad6c7f9SVictor Latushkin 	    "cachefile\n");
167c8ee1847SVictor Latushkin 	(void) fprintf(stderr, "        -X attempt extreme rewind (does not "
168c8ee1847SVictor Latushkin 	    "work with dataset)\n");
1693ad6c7f9SVictor Latushkin 	(void) fprintf(stderr, "        -e pool is exported/destroyed/"
1703ad6c7f9SVictor Latushkin 	    "has altroot/not in a cachefile\n");
1713ad6c7f9SVictor Latushkin 	(void) fprintf(stderr, "        -p <path> -- use one or more with "
1723ad6c7f9SVictor Latushkin 	    "-e to specify path to vdev dir\n");
173df15e419SMatthew Ahrens 	(void) fprintf(stderr, "        -x <dumpdir> -- "
174df15e419SMatthew Ahrens 	    "dump all read blocks into specified directory\n");
17590e894e2SRichard Lowe 	(void) fprintf(stderr, "        -P print numbers in parseable form\n");
1763ad6c7f9SVictor Latushkin 	(void) fprintf(stderr, "        -t <txg> -- highest txg to use when "
1772e551927SVictor Latushkin 	    "searching for uberblocks\n");
1782e4c9986SGeorge Wilson 	(void) fprintf(stderr, "        -I <number of inflight I/Os> -- "
179df15e419SMatthew Ahrens 	    "specify the maximum number of "
180df15e419SMatthew Ahrens 	    "checksumming I/Os [default is 200]\n");
181fa9e4066Sahrens 	(void) fprintf(stderr, "Specify an option more than once (e.g. -bb) "
182fa9e4066Sahrens 	    "to make only that option verbose\n");
183fa9e4066Sahrens 	(void) fprintf(stderr, "Default is to dump everything non-verbosely\n");
184fa9e4066Sahrens 	exit(1);
185fa9e4066Sahrens }
186fa9e4066Sahrens 
187ccba0801SRich Morris /*
188ccba0801SRich Morris  * Called for usage errors that are discovered after a call to spa_open(),
189ccba0801SRich Morris  * dmu_bonus_hold(), or pool_match().  abort() is called for other errors.
190ccba0801SRich Morris  */
191ccba0801SRich Morris 
192fa9e4066Sahrens static void
193fa9e4066Sahrens fatal(const char *fmt, ...)
194fa9e4066Sahrens {
195fa9e4066Sahrens 	va_list ap;
196fa9e4066Sahrens 
197fa9e4066Sahrens 	va_start(ap, fmt);
198fa9e4066Sahrens 	(void) fprintf(stderr, "%s: ", cmdname);
199fa9e4066Sahrens 	(void) vfprintf(stderr, fmt, ap);
200fa9e4066Sahrens 	va_end(ap);
201fa9e4066Sahrens 	(void) fprintf(stderr, "\n");
202fa9e4066Sahrens 
203ccba0801SRich Morris 	exit(1);
204fa9e4066Sahrens }
205fa9e4066Sahrens 
206fa9e4066Sahrens /* ARGSUSED */
207fa9e4066Sahrens static void
208fa9e4066Sahrens dump_packed_nvlist(objset_t *os, uint64_t object, void *data, size_t size)
209fa9e4066Sahrens {
210fa9e4066Sahrens 	nvlist_t *nv;
211fa9e4066Sahrens 	size_t nvsize = *(uint64_t *)data;
212fa9e4066Sahrens 	char *packed = umem_alloc(nvsize, UMEM_NOFAIL);
213fa9e4066Sahrens 
2147bfdf011SNeil Perrin 	VERIFY(0 == dmu_read(os, object, 0, nvsize, packed, DMU_READ_PREFETCH));
215fa9e4066Sahrens 
216fa9e4066Sahrens 	VERIFY(nvlist_unpack(packed, nvsize, &nv, 0) == 0);
217fa9e4066Sahrens 
218fa9e4066Sahrens 	umem_free(packed, nvsize);
219fa9e4066Sahrens 
220fa9e4066Sahrens 	dump_nvlist(nv, 8);
221fa9e4066Sahrens 
222fa9e4066Sahrens 	nvlist_free(nv);
223fa9e4066Sahrens }
224fa9e4066Sahrens 
2254445fffbSMatthew Ahrens /* ARGSUSED */
2264445fffbSMatthew Ahrens static void
2274445fffbSMatthew Ahrens dump_history_offsets(objset_t *os, uint64_t object, void *data, size_t size)
2284445fffbSMatthew Ahrens {
2294445fffbSMatthew Ahrens 	spa_history_phys_t *shp = data;
2304445fffbSMatthew Ahrens 
2314445fffbSMatthew Ahrens 	if (shp == NULL)
2324445fffbSMatthew Ahrens 		return;
2334445fffbSMatthew Ahrens 
2344445fffbSMatthew Ahrens 	(void) printf("\t\tpool_create_len = %llu\n",
2354445fffbSMatthew Ahrens 	    (u_longlong_t)shp->sh_pool_create_len);
2364445fffbSMatthew Ahrens 	(void) printf("\t\tphys_max_off = %llu\n",
2374445fffbSMatthew Ahrens 	    (u_longlong_t)shp->sh_phys_max_off);
2384445fffbSMatthew Ahrens 	(void) printf("\t\tbof = %llu\n",
2394445fffbSMatthew Ahrens 	    (u_longlong_t)shp->sh_bof);
2404445fffbSMatthew Ahrens 	(void) printf("\t\teof = %llu\n",
2414445fffbSMatthew Ahrens 	    (u_longlong_t)shp->sh_eof);
2424445fffbSMatthew Ahrens 	(void) printf("\t\trecords_lost = %llu\n",
2434445fffbSMatthew Ahrens 	    (u_longlong_t)shp->sh_records_lost);
2444445fffbSMatthew Ahrens }
2454445fffbSMatthew Ahrens 
2463f9d6ad7SLin Ling static void
2473f9d6ad7SLin Ling zdb_nicenum(uint64_t num, char *buf)
2483f9d6ad7SLin Ling {
2493f9d6ad7SLin Ling 	if (dump_opt['P'])
2503f9d6ad7SLin Ling 		(void) sprintf(buf, "%llu", (longlong_t)num);
2513f9d6ad7SLin Ling 	else
2523f9d6ad7SLin Ling 		nicenum(num, buf);
2533f9d6ad7SLin Ling }
2543f9d6ad7SLin Ling 
255490d05b9SMatthew Ahrens const char histo_stars[] = "****************************************";
256490d05b9SMatthew Ahrens const int histo_width = sizeof (histo_stars) - 1;
257fa9e4066Sahrens 
258fa9e4066Sahrens static void
2590713e232SGeorge Wilson dump_histogram(const uint64_t *histo, int size, int offset)
260fa9e4066Sahrens {
261fa9e4066Sahrens 	int i;
262490d05b9SMatthew Ahrens 	int minidx = size - 1;
263fa9e4066Sahrens 	int maxidx = 0;
264fa9e4066Sahrens 	uint64_t max = 0;
265fa9e4066Sahrens 
266490d05b9SMatthew Ahrens 	for (i = 0; i < size; i++) {
267fa9e4066Sahrens 		if (histo[i] > max)
268fa9e4066Sahrens 			max = histo[i];
269fa9e4066Sahrens 		if (histo[i] > 0 && i > maxidx)
270fa9e4066Sahrens 			maxidx = i;
271fa9e4066Sahrens 		if (histo[i] > 0 && i < minidx)
272fa9e4066Sahrens 			minidx = i;
273fa9e4066Sahrens 	}
274fa9e4066Sahrens 
275490d05b9SMatthew Ahrens 	if (max < histo_width)
276490d05b9SMatthew Ahrens 		max = histo_width;
277fa9e4066Sahrens 
278490d05b9SMatthew Ahrens 	for (i = minidx; i <= maxidx; i++) {
279490d05b9SMatthew Ahrens 		(void) printf("\t\t\t%3u: %6llu %s\n",
2800713e232SGeorge Wilson 		    i + offset, (u_longlong_t)histo[i],
281490d05b9SMatthew Ahrens 		    &histo_stars[(max - histo[i]) * histo_width / max]);
282490d05b9SMatthew Ahrens 	}
283fa9e4066Sahrens }
284fa9e4066Sahrens 
285fa9e4066Sahrens static void
286fa9e4066Sahrens dump_zap_stats(objset_t *os, uint64_t object)
287fa9e4066Sahrens {
288fa9e4066Sahrens 	int error;
289fa9e4066Sahrens 	zap_stats_t zs;
290fa9e4066Sahrens 
291fa9e4066Sahrens 	error = zap_get_stats(os, object, &zs);
292fa9e4066Sahrens 	if (error)
293fa9e4066Sahrens 		return;
294fa9e4066Sahrens 
295fa9e4066Sahrens 	if (zs.zs_ptrtbl_len == 0) {
296fa9e4066Sahrens 		ASSERT(zs.zs_num_blocks == 1);
297fa9e4066Sahrens 		(void) printf("\tmicrozap: %llu bytes, %llu entries\n",
298fa9e4066Sahrens 		    (u_longlong_t)zs.zs_blocksize,
299fa9e4066Sahrens 		    (u_longlong_t)zs.zs_num_entries);
300fa9e4066Sahrens 		return;
301fa9e4066Sahrens 	}
302fa9e4066Sahrens 
303fa9e4066Sahrens 	(void) printf("\tFat ZAP stats:\n");
3048248818dSnd 
3058248818dSnd 	(void) printf("\t\tPointer table:\n");
3068248818dSnd 	(void) printf("\t\t\t%llu elements\n",
307fa9e4066Sahrens 	    (u_longlong_t)zs.zs_ptrtbl_len);
3088248818dSnd 	(void) printf("\t\t\tzt_blk: %llu\n",
3098248818dSnd 	    (u_longlong_t)zs.zs_ptrtbl_zt_blk);
3108248818dSnd 	(void) printf("\t\t\tzt_numblks: %llu\n",
3118248818dSnd 	    (u_longlong_t)zs.zs_ptrtbl_zt_numblks);
3128248818dSnd 	(void) printf("\t\t\tzt_shift: %llu\n",
3138248818dSnd 	    (u_longlong_t)zs.zs_ptrtbl_zt_shift);
3148248818dSnd 	(void) printf("\t\t\tzt_blks_copied: %llu\n",
3158248818dSnd 	    (u_longlong_t)zs.zs_ptrtbl_blks_copied);
3168248818dSnd 	(void) printf("\t\t\tzt_nextblk: %llu\n",
3178248818dSnd 	    (u_longlong_t)zs.zs_ptrtbl_nextblk);
3188248818dSnd 
319fa9e4066Sahrens 	(void) printf("\t\tZAP entries: %llu\n",
320fa9e4066Sahrens 	    (u_longlong_t)zs.zs_num_entries);
321fa9e4066Sahrens 	(void) printf("\t\tLeaf blocks: %llu\n",
322fa9e4066Sahrens 	    (u_longlong_t)zs.zs_num_leafs);
323fa9e4066Sahrens 	(void) printf("\t\tTotal blocks: %llu\n",
324fa9e4066Sahrens 	    (u_longlong_t)zs.zs_num_blocks);
3258248818dSnd 	(void) printf("\t\tzap_block_type: 0x%llx\n",
3268248818dSnd 	    (u_longlong_t)zs.zs_block_type);
3278248818dSnd 	(void) printf("\t\tzap_magic: 0x%llx\n",
3288248818dSnd 	    (u_longlong_t)zs.zs_magic);
3298248818dSnd 	(void) printf("\t\tzap_salt: 0x%llx\n",
3308248818dSnd 	    (u_longlong_t)zs.zs_salt);
331fa9e4066Sahrens 
332fa9e4066Sahrens 	(void) printf("\t\tLeafs with 2^n pointers:\n");
3330713e232SGeorge Wilson 	dump_histogram(zs.zs_leafs_with_2n_pointers, ZAP_HISTOGRAM_SIZE, 0);
334fa9e4066Sahrens 
335fa9e4066Sahrens 	(void) printf("\t\tBlocks with n*5 entries:\n");
3360713e232SGeorge Wilson 	dump_histogram(zs.zs_blocks_with_n5_entries, ZAP_HISTOGRAM_SIZE, 0);
337fa9e4066Sahrens 
338fa9e4066Sahrens 	(void) printf("\t\tBlocks n/10 full:\n");
3390713e232SGeorge Wilson 	dump_histogram(zs.zs_blocks_n_tenths_full, ZAP_HISTOGRAM_SIZE, 0);
340fa9e4066Sahrens 
341fa9e4066Sahrens 	(void) printf("\t\tEntries with n chunks:\n");
3420713e232SGeorge Wilson 	dump_histogram(zs.zs_entries_using_n_chunks, ZAP_HISTOGRAM_SIZE, 0);
343fa9e4066Sahrens 
344fa9e4066Sahrens 	(void) printf("\t\tBuckets with n entries:\n");
3450713e232SGeorge Wilson 	dump_histogram(zs.zs_buckets_with_n_entries, ZAP_HISTOGRAM_SIZE, 0);
346fa9e4066Sahrens }
347fa9e4066Sahrens 
348fa9e4066Sahrens /*ARGSUSED*/
349fa9e4066Sahrens static void
350fa9e4066Sahrens dump_none(objset_t *os, uint64_t object, void *data, size_t size)
351fa9e4066Sahrens {
352fa9e4066Sahrens }
353fa9e4066Sahrens 
3546de8f417SVictor Latushkin /*ARGSUSED*/
3556de8f417SVictor Latushkin static void
3566de8f417SVictor Latushkin dump_unknown(objset_t *os, uint64_t object, void *data, size_t size)
3576de8f417SVictor Latushkin {
3586de8f417SVictor Latushkin 	(void) printf("\tUNKNOWN OBJECT TYPE\n");
3596de8f417SVictor Latushkin }
3606de8f417SVictor Latushkin 
361fa9e4066Sahrens /*ARGSUSED*/
362fa9e4066Sahrens void
363fa9e4066Sahrens dump_uint8(objset_t *os, uint64_t object, void *data, size_t size)
364fa9e4066Sahrens {
365fa9e4066Sahrens }
366fa9e4066Sahrens 
367fa9e4066Sahrens /*ARGSUSED*/
368fa9e4066Sahrens static void
369fa9e4066Sahrens dump_uint64(objset_t *os, uint64_t object, void *data, size_t size)
370fa9e4066Sahrens {
371fa9e4066Sahrens }
372fa9e4066Sahrens 
373fa9e4066Sahrens /*ARGSUSED*/
374fa9e4066Sahrens static void
375fa9e4066Sahrens dump_zap(objset_t *os, uint64_t object, void *data, size_t size)
376fa9e4066Sahrens {
377fa9e4066Sahrens 	zap_cursor_t zc;
378fa9e4066Sahrens 	zap_attribute_t attr;
379fa9e4066Sahrens 	void *prop;
380fa9e4066Sahrens 	int i;
381fa9e4066Sahrens 
382fa9e4066Sahrens 	dump_zap_stats(os, object);
383fa9e4066Sahrens 	(void) printf("\n");
384fa9e4066Sahrens 
385fa9e4066Sahrens 	for (zap_cursor_init(&zc, os, object);
386fa9e4066Sahrens 	    zap_cursor_retrieve(&zc, &attr) == 0;
387fa9e4066Sahrens 	    zap_cursor_advance(&zc)) {
388fa9e4066Sahrens 		(void) printf("\t\t%s = ", attr.za_name);
389fa9e4066Sahrens 		if (attr.za_num_integers == 0) {
390fa9e4066Sahrens 			(void) printf("\n");
391fa9e4066Sahrens 			continue;
392fa9e4066Sahrens 		}
393fa9e4066Sahrens 		prop = umem_zalloc(attr.za_num_integers *
394fa9e4066Sahrens 		    attr.za_integer_length, UMEM_NOFAIL);
395fa9e4066Sahrens 		(void) zap_lookup(os, object, attr.za_name,
396fa9e4066Sahrens 		    attr.za_integer_length, attr.za_num_integers, prop);
397fa9e4066Sahrens 		if (attr.za_integer_length == 1) {
398fa9e4066Sahrens 			(void) printf("%s", (char *)prop);
399fa9e4066Sahrens 		} else {
400fa9e4066Sahrens 			for (i = 0; i < attr.za_num_integers; i++) {
401fa9e4066Sahrens 				switch (attr.za_integer_length) {
402fa9e4066Sahrens 				case 2:
403fa9e4066Sahrens 					(void) printf("%u ",
404fa9e4066Sahrens 					    ((uint16_t *)prop)[i]);
405fa9e4066Sahrens 					break;
406fa9e4066Sahrens 				case 4:
407fa9e4066Sahrens 					(void) printf("%u ",
408fa9e4066Sahrens 					    ((uint32_t *)prop)[i]);
409fa9e4066Sahrens 					break;
410fa9e4066Sahrens 				case 8:
411fa9e4066Sahrens 					(void) printf("%lld ",
412fa9e4066Sahrens 					    (u_longlong_t)((int64_t *)prop)[i]);
413fa9e4066Sahrens 					break;
414fa9e4066Sahrens 				}
415fa9e4066Sahrens 			}
416fa9e4066Sahrens 		}
417fa9e4066Sahrens 		(void) printf("\n");
418fa9e4066Sahrens 		umem_free(prop, attr.za_num_integers * attr.za_integer_length);
419fa9e4066Sahrens 	}
42087e5029aSahrens 	zap_cursor_fini(&zc);
421fa9e4066Sahrens }
422fa9e4066Sahrens 
423732885fcSMatthew Ahrens static void
424732885fcSMatthew Ahrens dump_bpobj(objset_t *os, uint64_t object, void *data, size_t size)
425732885fcSMatthew Ahrens {
426732885fcSMatthew Ahrens 	bpobj_phys_t *bpop = data;
427732885fcSMatthew Ahrens 	char bytes[32], comp[32], uncomp[32];
428732885fcSMatthew Ahrens 
429732885fcSMatthew Ahrens 	if (bpop == NULL)
430732885fcSMatthew Ahrens 		return;
431732885fcSMatthew Ahrens 
432732885fcSMatthew Ahrens 	zdb_nicenum(bpop->bpo_bytes, bytes);
433732885fcSMatthew Ahrens 	zdb_nicenum(bpop->bpo_comp, comp);
434732885fcSMatthew Ahrens 	zdb_nicenum(bpop->bpo_uncomp, uncomp);
435732885fcSMatthew Ahrens 
436732885fcSMatthew Ahrens 	(void) printf("\t\tnum_blkptrs = %llu\n",
437732885fcSMatthew Ahrens 	    (u_longlong_t)bpop->bpo_num_blkptrs);
438732885fcSMatthew Ahrens 	(void) printf("\t\tbytes = %s\n", bytes);
439732885fcSMatthew Ahrens 	if (size >= BPOBJ_SIZE_V1) {
440732885fcSMatthew Ahrens 		(void) printf("\t\tcomp = %s\n", comp);
441732885fcSMatthew Ahrens 		(void) printf("\t\tuncomp = %s\n", uncomp);
442732885fcSMatthew Ahrens 	}
443732885fcSMatthew Ahrens 	if (size >= sizeof (*bpop)) {
444732885fcSMatthew Ahrens 		(void) printf("\t\tsubobjs = %llu\n",
445732885fcSMatthew Ahrens 		    (u_longlong_t)bpop->bpo_subobjs);
446732885fcSMatthew Ahrens 		(void) printf("\t\tnum_subobjs = %llu\n",
447732885fcSMatthew Ahrens 		    (u_longlong_t)bpop->bpo_num_subobjs);
448732885fcSMatthew Ahrens 	}
449732885fcSMatthew Ahrens 
450732885fcSMatthew Ahrens 	if (dump_opt['d'] < 5)
451732885fcSMatthew Ahrens 		return;
452732885fcSMatthew Ahrens 
453732885fcSMatthew Ahrens 	for (uint64_t i = 0; i < bpop->bpo_num_blkptrs; i++) {
454732885fcSMatthew Ahrens 		char blkbuf[BP_SPRINTF_LEN];
455732885fcSMatthew Ahrens 		blkptr_t bp;
456732885fcSMatthew Ahrens 
457732885fcSMatthew Ahrens 		int err = dmu_read(os, object,
458732885fcSMatthew Ahrens 		    i * sizeof (bp), sizeof (bp), &bp, 0);
459732885fcSMatthew Ahrens 		if (err != 0) {
460732885fcSMatthew Ahrens 			(void) printf("got error %u from dmu_read\n", err);
461732885fcSMatthew Ahrens 			break;
462732885fcSMatthew Ahrens 		}
463732885fcSMatthew Ahrens 		snprintf_blkptr_compact(blkbuf, sizeof (blkbuf), &bp);
464732885fcSMatthew Ahrens 		(void) printf("\t%s\n", blkbuf);
465732885fcSMatthew Ahrens 	}
466732885fcSMatthew Ahrens }
467732885fcSMatthew Ahrens 
468732885fcSMatthew Ahrens /* ARGSUSED */
469732885fcSMatthew Ahrens static void
470732885fcSMatthew Ahrens dump_bpobj_subobjs(objset_t *os, uint64_t object, void *data, size_t size)
471732885fcSMatthew Ahrens {
472732885fcSMatthew Ahrens 	dmu_object_info_t doi;
473732885fcSMatthew Ahrens 
474732885fcSMatthew Ahrens 	VERIFY0(dmu_object_info(os, object, &doi));
475732885fcSMatthew Ahrens 	uint64_t *subobjs = kmem_alloc(doi.doi_max_offset, KM_SLEEP);
476732885fcSMatthew Ahrens 
477732885fcSMatthew Ahrens 	int err = dmu_read(os, object, 0, doi.doi_max_offset, subobjs, 0);
478732885fcSMatthew Ahrens 	if (err != 0) {
479732885fcSMatthew Ahrens 		(void) printf("got error %u from dmu_read\n", err);
480732885fcSMatthew Ahrens 		kmem_free(subobjs, doi.doi_max_offset);
481732885fcSMatthew Ahrens 		return;
482732885fcSMatthew Ahrens 	}
483732885fcSMatthew Ahrens 
484732885fcSMatthew Ahrens 	int64_t last_nonzero = -1;
485732885fcSMatthew Ahrens 	for (uint64_t i = 0; i < doi.doi_max_offset / 8; i++) {
486732885fcSMatthew Ahrens 		if (subobjs[i] != 0)
487732885fcSMatthew Ahrens 			last_nonzero = i;
488732885fcSMatthew Ahrens 	}
489732885fcSMatthew Ahrens 
490732885fcSMatthew Ahrens 	for (int64_t i = 0; i <= last_nonzero; i++) {
491732885fcSMatthew Ahrens 		(void) printf("\t%llu\n", (longlong_t)subobjs[i]);
492732885fcSMatthew Ahrens 	}
493732885fcSMatthew Ahrens 	kmem_free(subobjs, doi.doi_max_offset);
494732885fcSMatthew Ahrens }
495732885fcSMatthew Ahrens 
496486ae710SMatthew Ahrens /*ARGSUSED*/
497486ae710SMatthew Ahrens static void
498486ae710SMatthew Ahrens dump_ddt_zap(objset_t *os, uint64_t object, void *data, size_t size)
499486ae710SMatthew Ahrens {
500486ae710SMatthew Ahrens 	dump_zap_stats(os, object);
501486ae710SMatthew Ahrens 	/* contents are printed elsewhere, properly decoded */
502486ae710SMatthew Ahrens }
503486ae710SMatthew Ahrens 
5040a586ceaSMark Shellenbaum /*ARGSUSED*/
5050a586ceaSMark Shellenbaum static void
5060a586ceaSMark Shellenbaum dump_sa_attrs(objset_t *os, uint64_t object, void *data, size_t size)
5070a586ceaSMark Shellenbaum {
5080a586ceaSMark Shellenbaum 	zap_cursor_t zc;
5090a586ceaSMark Shellenbaum 	zap_attribute_t attr;
5100a586ceaSMark Shellenbaum 
5110a586ceaSMark Shellenbaum 	dump_zap_stats(os, object);
5120a586ceaSMark Shellenbaum 	(void) printf("\n");
5130a586ceaSMark Shellenbaum 
5140a586ceaSMark Shellenbaum 	for (zap_cursor_init(&zc, os, object);
5150a586ceaSMark Shellenbaum 	    zap_cursor_retrieve(&zc, &attr) == 0;
5160a586ceaSMark Shellenbaum 	    zap_cursor_advance(&zc)) {
5170a586ceaSMark Shellenbaum 		(void) printf("\t\t%s = ", attr.za_name);
5180a586ceaSMark Shellenbaum 		if (attr.za_num_integers == 0) {
5190a586ceaSMark Shellenbaum 			(void) printf("\n");
5200a586ceaSMark Shellenbaum 			continue;
5210a586ceaSMark Shellenbaum 		}
5220a586ceaSMark Shellenbaum 		(void) printf(" %llx : [%d:%d:%d]\n",
5230a586ceaSMark Shellenbaum 		    (u_longlong_t)attr.za_first_integer,
5240a586ceaSMark Shellenbaum 		    (int)ATTR_LENGTH(attr.za_first_integer),
5250a586ceaSMark Shellenbaum 		    (int)ATTR_BSWAP(attr.za_first_integer),
5260a586ceaSMark Shellenbaum 		    (int)ATTR_NUM(attr.za_first_integer));
5270a586ceaSMark Shellenbaum 	}
5280a586ceaSMark Shellenbaum 	zap_cursor_fini(&zc);
5290a586ceaSMark Shellenbaum }
5300a586ceaSMark Shellenbaum 
5310a586ceaSMark Shellenbaum /*ARGSUSED*/
5320a586ceaSMark Shellenbaum static void
5330a586ceaSMark Shellenbaum dump_sa_layouts(objset_t *os, uint64_t object, void *data, size_t size)
5340a586ceaSMark Shellenbaum {
5350a586ceaSMark Shellenbaum 	zap_cursor_t zc;
5360a586ceaSMark Shellenbaum 	zap_attribute_t attr;
5370a586ceaSMark Shellenbaum 	uint16_t *layout_attrs;
5380a586ceaSMark Shellenbaum 	int i;
5390a586ceaSMark Shellenbaum 
5400a586ceaSMark Shellenbaum 	dump_zap_stats(os, object);
5410a586ceaSMark Shellenbaum 	(void) printf("\n");
5420a586ceaSMark Shellenbaum 
5430a586ceaSMark Shellenbaum 	for (zap_cursor_init(&zc, os, object);
5440a586ceaSMark Shellenbaum 	    zap_cursor_retrieve(&zc, &attr) == 0;
5450a586ceaSMark Shellenbaum 	    zap_cursor_advance(&zc)) {
5460a586ceaSMark Shellenbaum 		(void) printf("\t\t%s = [", attr.za_name);
5470a586ceaSMark Shellenbaum 		if (attr.za_num_integers == 0) {
5480a586ceaSMark Shellenbaum 			(void) printf("\n");
5490a586ceaSMark Shellenbaum 			continue;
5500a586ceaSMark Shellenbaum 		}
5510a586ceaSMark Shellenbaum 
5520a586ceaSMark Shellenbaum 		VERIFY(attr.za_integer_length == 2);
5530a586ceaSMark Shellenbaum 		layout_attrs = umem_zalloc(attr.za_num_integers *
5540a586ceaSMark Shellenbaum 		    attr.za_integer_length, UMEM_NOFAIL);
5550a586ceaSMark Shellenbaum 
5560a586ceaSMark Shellenbaum 		VERIFY(zap_lookup(os, object, attr.za_name,
5570a586ceaSMark Shellenbaum 		    attr.za_integer_length,
5580a586ceaSMark Shellenbaum 		    attr.za_num_integers, layout_attrs) == 0);
5590a586ceaSMark Shellenbaum 
5600a586ceaSMark Shellenbaum 		for (i = 0; i != attr.za_num_integers; i++)
5610a586ceaSMark Shellenbaum 			(void) printf(" %d ", (int)layout_attrs[i]);
5620a586ceaSMark Shellenbaum 		(void) printf("]\n");
5630a586ceaSMark Shellenbaum 		umem_free(layout_attrs,
5640a586ceaSMark Shellenbaum 		    attr.za_num_integers * attr.za_integer_length);
5650a586ceaSMark Shellenbaum 	}
5660a586ceaSMark Shellenbaum 	zap_cursor_fini(&zc);
5670a586ceaSMark Shellenbaum }
5680a586ceaSMark Shellenbaum 
569e7437265Sahrens /*ARGSUSED*/
570e7437265Sahrens static void
571e7437265Sahrens dump_zpldir(objset_t *os, uint64_t object, void *data, size_t size)
572e7437265Sahrens {
573e7437265Sahrens 	zap_cursor_t zc;
574e7437265Sahrens 	zap_attribute_t attr;
575e7437265Sahrens 	const char *typenames[] = {
576e7437265Sahrens 		/* 0 */ "not specified",
577e7437265Sahrens 		/* 1 */ "FIFO",
578e7437265Sahrens 		/* 2 */ "Character Device",
579e7437265Sahrens 		/* 3 */ "3 (invalid)",
580e7437265Sahrens 		/* 4 */ "Directory",
581e7437265Sahrens 		/* 5 */ "5 (invalid)",
582e7437265Sahrens 		/* 6 */ "Block Device",
583e7437265Sahrens 		/* 7 */ "7 (invalid)",
584e7437265Sahrens 		/* 8 */ "Regular File",
585e7437265Sahrens 		/* 9 */ "9 (invalid)",
586e7437265Sahrens 		/* 10 */ "Symbolic Link",
587e7437265Sahrens 		/* 11 */ "11 (invalid)",
588e7437265Sahrens 		/* 12 */ "Socket",
589e7437265Sahrens 		/* 13 */ "Door",
590e7437265Sahrens 		/* 14 */ "Event Port",
591e7437265Sahrens 		/* 15 */ "15 (invalid)",
592e7437265Sahrens 	};
593e7437265Sahrens 
594e7437265Sahrens 	dump_zap_stats(os, object);
595e7437265Sahrens 	(void) printf("\n");
596e7437265Sahrens 
597e7437265Sahrens 	for (zap_cursor_init(&zc, os, object);
598e7437265Sahrens 	    zap_cursor_retrieve(&zc, &attr) == 0;
599e7437265Sahrens 	    zap_cursor_advance(&zc)) {
600e7437265Sahrens 		(void) printf("\t\t%s = %lld (type: %s)\n",
601e7437265Sahrens 		    attr.za_name, ZFS_DIRENT_OBJ(attr.za_first_integer),
602e7437265Sahrens 		    typenames[ZFS_DIRENT_TYPE(attr.za_first_integer)]);
603e7437265Sahrens 	}
604e7437265Sahrens 	zap_cursor_fini(&zc);
605e7437265Sahrens }
606e7437265Sahrens 
6070713e232SGeorge Wilson int
6080713e232SGeorge Wilson get_dtl_refcount(vdev_t *vd)
6090713e232SGeorge Wilson {
6100713e232SGeorge Wilson 	int refcount = 0;
6110713e232SGeorge Wilson 
6120713e232SGeorge Wilson 	if (vd->vdev_ops->vdev_op_leaf) {
6130713e232SGeorge Wilson 		space_map_t *sm = vd->vdev_dtl_sm;
6140713e232SGeorge Wilson 
6150713e232SGeorge Wilson 		if (sm != NULL &&
6160713e232SGeorge Wilson 		    sm->sm_dbuf->db_size == sizeof (space_map_phys_t))
6170713e232SGeorge Wilson 			return (1);
6180713e232SGeorge Wilson 		return (0);
6190713e232SGeorge Wilson 	}
6200713e232SGeorge Wilson 
6210713e232SGeorge Wilson 	for (int c = 0; c < vd->vdev_children; c++)
6220713e232SGeorge Wilson 		refcount += get_dtl_refcount(vd->vdev_child[c]);
6230713e232SGeorge Wilson 	return (refcount);
6240713e232SGeorge Wilson }
6250713e232SGeorge Wilson 
6260713e232SGeorge Wilson int
6270713e232SGeorge Wilson get_metaslab_refcount(vdev_t *vd)
6280713e232SGeorge Wilson {
6290713e232SGeorge Wilson 	int refcount = 0;
6300713e232SGeorge Wilson 
6312e4c9986SGeorge Wilson 	if (vd->vdev_top == vd && !vd->vdev_removing) {
6320713e232SGeorge Wilson 		for (int m = 0; m < vd->vdev_ms_count; m++) {
6330713e232SGeorge Wilson 			space_map_t *sm = vd->vdev_ms[m]->ms_sm;
6340713e232SGeorge Wilson 
6350713e232SGeorge Wilson 			if (sm != NULL &&
6360713e232SGeorge Wilson 			    sm->sm_dbuf->db_size == sizeof (space_map_phys_t))
6370713e232SGeorge Wilson 				refcount++;
6380713e232SGeorge Wilson 		}
6390713e232SGeorge Wilson 	}
6400713e232SGeorge Wilson 	for (int c = 0; c < vd->vdev_children; c++)
6410713e232SGeorge Wilson 		refcount += get_metaslab_refcount(vd->vdev_child[c]);
6420713e232SGeorge Wilson 
6430713e232SGeorge Wilson 	return (refcount);
6440713e232SGeorge Wilson }
6450713e232SGeorge Wilson 
6460713e232SGeorge Wilson static int
6470713e232SGeorge Wilson verify_spacemap_refcounts(spa_t *spa)
6480713e232SGeorge Wilson {
6492acef22dSMatthew Ahrens 	uint64_t expected_refcount = 0;
6502acef22dSMatthew Ahrens 	uint64_t actual_refcount;
6510713e232SGeorge Wilson 
6522acef22dSMatthew Ahrens 	(void) feature_get_refcount(spa,
6532acef22dSMatthew Ahrens 	    &spa_feature_table[SPA_FEATURE_SPACEMAP_HISTOGRAM],
6542acef22dSMatthew Ahrens 	    &expected_refcount);
6550713e232SGeorge Wilson 	actual_refcount = get_dtl_refcount(spa->spa_root_vdev);
6560713e232SGeorge Wilson 	actual_refcount += get_metaslab_refcount(spa->spa_root_vdev);
6570713e232SGeorge Wilson 
6580713e232SGeorge Wilson 	if (expected_refcount != actual_refcount) {
6592acef22dSMatthew Ahrens 		(void) printf("space map refcount mismatch: expected %lld != "
6602acef22dSMatthew Ahrens 		    "actual %lld\n",
6612acef22dSMatthew Ahrens 		    (longlong_t)expected_refcount,
6622acef22dSMatthew Ahrens 		    (longlong_t)actual_refcount);
6630713e232SGeorge Wilson 		return (2);
6640713e232SGeorge Wilson 	}
6650713e232SGeorge Wilson 	return (0);
6660713e232SGeorge Wilson }
6670713e232SGeorge Wilson 
668fa9e4066Sahrens static void
6690713e232SGeorge Wilson dump_spacemap(objset_t *os, space_map_t *sm)
670fa9e4066Sahrens {
671fa9e4066Sahrens 	uint64_t alloc, offset, entry;
6728053a263Sck 	char *ddata[] = { "ALLOC", "FREE", "CONDENSE", "INVALID",
6738053a263Sck 			    "INVALID", "INVALID", "INVALID", "INVALID" };
674fa9e4066Sahrens 
6750713e232SGeorge Wilson 	if (sm == NULL)
676fa9e4066Sahrens 		return;
677fa9e4066Sahrens 
678fa9e4066Sahrens 	/*
679fa9e4066Sahrens 	 * Print out the freelist entries in both encoded and decoded form.
680fa9e4066Sahrens 	 */
681fa9e4066Sahrens 	alloc = 0;
6820713e232SGeorge Wilson 	for (offset = 0; offset < space_map_length(sm);
6830713e232SGeorge Wilson 	    offset += sizeof (entry)) {
6840713e232SGeorge Wilson 		uint8_t mapshift = sm->sm_shift;
6850713e232SGeorge Wilson 
6860713e232SGeorge Wilson 		VERIFY0(dmu_read(os, space_map_object(sm), offset,
6877bfdf011SNeil Perrin 		    sizeof (entry), &entry, DMU_READ_PREFETCH));
688fa9e4066Sahrens 		if (SM_DEBUG_DECODE(entry)) {
6890713e232SGeorge Wilson 
69087219db7SVictor Latushkin 			(void) printf("\t    [%6llu] %s: txg %llu, pass %llu\n",
691fa9e4066Sahrens 			    (u_longlong_t)(offset / sizeof (entry)),
692fa9e4066Sahrens 			    ddata[SM_DEBUG_ACTION_DECODE(entry)],
6935ad82045Snd 			    (u_longlong_t)SM_DEBUG_TXG_DECODE(entry),
6945ad82045Snd 			    (u_longlong_t)SM_DEBUG_SYNCPASS_DECODE(entry));
695fa9e4066Sahrens 		} else {
69687219db7SVictor Latushkin 			(void) printf("\t    [%6llu]    %c  range:"
69787219db7SVictor Latushkin 			    " %010llx-%010llx  size: %06llx\n",
698fa9e4066Sahrens 			    (u_longlong_t)(offset / sizeof (entry)),
699fa9e4066Sahrens 			    SM_TYPE_DECODE(entry) == SM_ALLOC ? 'A' : 'F',
7005ad82045Snd 			    (u_longlong_t)((SM_OFFSET_DECODE(entry) <<
7010713e232SGeorge Wilson 			    mapshift) + sm->sm_start),
7025ad82045Snd 			    (u_longlong_t)((SM_OFFSET_DECODE(entry) <<
7030713e232SGeorge Wilson 			    mapshift) + sm->sm_start +
7040713e232SGeorge Wilson 			    (SM_RUN_DECODE(entry) << mapshift)),
7055ad82045Snd 			    (u_longlong_t)(SM_RUN_DECODE(entry) << mapshift));
706fa9e4066Sahrens 			if (SM_TYPE_DECODE(entry) == SM_ALLOC)
707fa9e4066Sahrens 				alloc += SM_RUN_DECODE(entry) << mapshift;
708fa9e4066Sahrens 			else
709fa9e4066Sahrens 				alloc -= SM_RUN_DECODE(entry) << mapshift;
710fa9e4066Sahrens 		}
711fa9e4066Sahrens 	}
7120713e232SGeorge Wilson 	if (alloc != space_map_allocated(sm)) {
713fa9e4066Sahrens 		(void) printf("space_map_object alloc (%llu) INCONSISTENT "
714fa9e4066Sahrens 		    "with space map summary (%llu)\n",
7150713e232SGeorge Wilson 		    (u_longlong_t)space_map_allocated(sm), (u_longlong_t)alloc);
716fa9e4066Sahrens 	}
717fa9e4066Sahrens }
718fa9e4066Sahrens 
719d6e555bdSGeorge Wilson static void
720d6e555bdSGeorge Wilson dump_metaslab_stats(metaslab_t *msp)
721d6e555bdSGeorge Wilson {
7223f9d6ad7SLin Ling 	char maxbuf[32];
7230713e232SGeorge Wilson 	range_tree_t *rt = msp->ms_tree;
7240713e232SGeorge Wilson 	avl_tree_t *t = &msp->ms_size_tree;
7250713e232SGeorge Wilson 	int free_pct = range_tree_space(rt) * 100 / msp->ms_size;
726d6e555bdSGeorge Wilson 
7270713e232SGeorge Wilson 	zdb_nicenum(metaslab_block_maxsize(msp), maxbuf);
728d6e555bdSGeorge Wilson 
72987219db7SVictor Latushkin 	(void) printf("\t %25s %10lu   %7s  %6s   %4s %4d%%\n",
730d6e555bdSGeorge Wilson 	    "segments", avl_numnodes(t), "maxsize", maxbuf,
731d6e555bdSGeorge Wilson 	    "freepct", free_pct);
7320713e232SGeorge Wilson 	(void) printf("\tIn-memory histogram:\n");
7330713e232SGeorge Wilson 	dump_histogram(rt->rt_histogram, RANGE_TREE_HISTOGRAM_SIZE, 0);
734d6e555bdSGeorge Wilson }
735d6e555bdSGeorge Wilson 
736fa9e4066Sahrens static void
737fa9e4066Sahrens dump_metaslab(metaslab_t *msp)
738fa9e4066Sahrens {
739fa9e4066Sahrens 	vdev_t *vd = msp->ms_group->mg_vd;
740fa9e4066Sahrens 	spa_t *spa = vd->vdev_spa;
7410713e232SGeorge Wilson 	space_map_t *sm = msp->ms_sm;
7423f9d6ad7SLin Ling 	char freebuf[32];
743fa9e4066Sahrens 
7440713e232SGeorge Wilson 	zdb_nicenum(msp->ms_size - space_map_allocated(sm), freebuf);
745fa9e4066Sahrens 
746fa9e4066Sahrens 	(void) printf(
74787219db7SVictor Latushkin 	    "\tmetaslab %6llu   offset %12llx   spacemap %6llu   free    %5s\n",
7480713e232SGeorge Wilson 	    (u_longlong_t)msp->ms_id, (u_longlong_t)msp->ms_start,
7490713e232SGeorge Wilson 	    (u_longlong_t)space_map_object(sm), freebuf);
750fa9e4066Sahrens 
7510713e232SGeorge Wilson 	if (dump_opt['m'] > 2 && !dump_opt['L']) {
752d6e555bdSGeorge Wilson 		mutex_enter(&msp->ms_lock);
7530713e232SGeorge Wilson 		metaslab_load_wait(msp);
7540713e232SGeorge Wilson 		if (!msp->ms_loaded) {
7550713e232SGeorge Wilson 			VERIFY0(metaslab_load(msp));
7560713e232SGeorge Wilson 			range_tree_stat_verify(msp->ms_tree);
7570713e232SGeorge Wilson 		}
758bc3975b5SVictor Latushkin 		dump_metaslab_stats(msp);
7590713e232SGeorge Wilson 		metaslab_unload(msp);
760d6e555bdSGeorge Wilson 		mutex_exit(&msp->ms_lock);
761d6e555bdSGeorge Wilson 	}
762d6e555bdSGeorge Wilson 
7630713e232SGeorge Wilson 	if (dump_opt['m'] > 1 && sm != NULL &&
7642acef22dSMatthew Ahrens 	    spa_feature_is_active(spa, SPA_FEATURE_SPACEMAP_HISTOGRAM)) {
7650713e232SGeorge Wilson 		/*
7660713e232SGeorge Wilson 		 * The space map histogram represents free space in chunks
7670713e232SGeorge Wilson 		 * of sm_shift (i.e. bucket 0 refers to 2^sm_shift).
7680713e232SGeorge Wilson 		 */
7692e4c9986SGeorge Wilson 		(void) printf("\tOn-disk histogram:\t\tfragmentation %llu\n",
7702e4c9986SGeorge Wilson 		    (u_longlong_t)msp->ms_fragmentation);
7710713e232SGeorge Wilson 		dump_histogram(sm->sm_phys->smp_histogram,
7722e4c9986SGeorge Wilson 		    SPACE_MAP_HISTOGRAM_SIZE, sm->sm_shift);
7730713e232SGeorge Wilson 	}
7740713e232SGeorge Wilson 
7750713e232SGeorge Wilson 	if (dump_opt['d'] > 5 || dump_opt['m'] > 3) {
7760713e232SGeorge Wilson 		ASSERT(msp->ms_size == (1ULL << vd->vdev_ms_shift));
777d6e555bdSGeorge Wilson 
778d6e555bdSGeorge Wilson 		mutex_enter(&msp->ms_lock);
7790713e232SGeorge Wilson 		dump_spacemap(spa->spa_meta_objset, msp->ms_sm);
780d6e555bdSGeorge Wilson 		mutex_exit(&msp->ms_lock);
781d6e555bdSGeorge Wilson 	}
78287219db7SVictor Latushkin }
783fa9e4066Sahrens 
78487219db7SVictor Latushkin static void
78587219db7SVictor Latushkin print_vdev_metaslab_header(vdev_t *vd)
78687219db7SVictor Latushkin {
78787219db7SVictor Latushkin 	(void) printf("\tvdev %10llu\n\t%-10s%5llu   %-19s   %-15s   %-10s\n",
78887219db7SVictor Latushkin 	    (u_longlong_t)vd->vdev_id,
78987219db7SVictor Latushkin 	    "metaslabs", (u_longlong_t)vd->vdev_ms_count,
79087219db7SVictor Latushkin 	    "offset", "spacemap", "free");
79187219db7SVictor Latushkin 	(void) printf("\t%15s   %19s   %15s   %10s\n",
79287219db7SVictor Latushkin 	    "---------------", "-------------------",
79387219db7SVictor Latushkin 	    "---------------", "-------------");
794fa9e4066Sahrens }
795fa9e4066Sahrens 
7962e4c9986SGeorge Wilson static void
7972e4c9986SGeorge Wilson dump_metaslab_groups(spa_t *spa)
7982e4c9986SGeorge Wilson {
7992e4c9986SGeorge Wilson 	vdev_t *rvd = spa->spa_root_vdev;
8002e4c9986SGeorge Wilson 	metaslab_class_t *mc = spa_normal_class(spa);
8012e4c9986SGeorge Wilson 	uint64_t fragmentation;
8022e4c9986SGeorge Wilson 
8032e4c9986SGeorge Wilson 	metaslab_class_histogram_verify(mc);
8042e4c9986SGeorge Wilson 
8052e4c9986SGeorge Wilson 	for (int c = 0; c < rvd->vdev_children; c++) {
8062e4c9986SGeorge Wilson 		vdev_t *tvd = rvd->vdev_child[c];
8072e4c9986SGeorge Wilson 		metaslab_group_t *mg = tvd->vdev_mg;
8082e4c9986SGeorge Wilson 
8092e4c9986SGeorge Wilson 		if (mg->mg_class != mc)
8102e4c9986SGeorge Wilson 			continue;
8112e4c9986SGeorge Wilson 
8122e4c9986SGeorge Wilson 		metaslab_group_histogram_verify(mg);
8132e4c9986SGeorge Wilson 		mg->mg_fragmentation = metaslab_group_fragmentation(mg);
8142e4c9986SGeorge Wilson 
8152e4c9986SGeorge Wilson 		(void) printf("\tvdev %10llu\t\tmetaslabs%5llu\t\t"
8162e4c9986SGeorge Wilson 		    "fragmentation",
8172e4c9986SGeorge Wilson 		    (u_longlong_t)tvd->vdev_id,
8182e4c9986SGeorge Wilson 		    (u_longlong_t)tvd->vdev_ms_count);
8192e4c9986SGeorge Wilson 		if (mg->mg_fragmentation == ZFS_FRAG_INVALID) {
8202e4c9986SGeorge Wilson 			(void) printf("%3s\n", "-");
8212e4c9986SGeorge Wilson 		} else {
8222e4c9986SGeorge Wilson 			(void) printf("%3llu%%\n",
8232e4c9986SGeorge Wilson 			    (u_longlong_t)mg->mg_fragmentation);
8242e4c9986SGeorge Wilson 		}
8252e4c9986SGeorge Wilson 		dump_histogram(mg->mg_histogram, RANGE_TREE_HISTOGRAM_SIZE, 0);
8262e4c9986SGeorge Wilson 	}
8272e4c9986SGeorge Wilson 
8282e4c9986SGeorge Wilson 	(void) printf("\tpool %s\tfragmentation", spa_name(spa));
8292e4c9986SGeorge Wilson 	fragmentation = metaslab_class_fragmentation(mc);
8302e4c9986SGeorge Wilson 	if (fragmentation == ZFS_FRAG_INVALID)
8312e4c9986SGeorge Wilson 		(void) printf("\t%3s\n", "-");
8322e4c9986SGeorge Wilson 	else
8332e4c9986SGeorge Wilson 		(void) printf("\t%3llu%%\n", (u_longlong_t)fragmentation);
8342e4c9986SGeorge Wilson 	dump_histogram(mc->mc_histogram, RANGE_TREE_HISTOGRAM_SIZE, 0);
8352e4c9986SGeorge Wilson }
8362e4c9986SGeorge Wilson 
837fa9e4066Sahrens static void
838fa9e4066Sahrens dump_metaslabs(spa_t *spa)
839fa9e4066Sahrens {
84087219db7SVictor Latushkin 	vdev_t *vd, *rvd = spa->spa_root_vdev;
84187219db7SVictor Latushkin 	uint64_t m, c = 0, children = rvd->vdev_children;
842fa9e4066Sahrens 
843fa9e4066Sahrens 	(void) printf("\nMetaslabs:\n");
844fa9e4066Sahrens 
84587219db7SVictor Latushkin 	if (!dump_opt['d'] && zopt_objects > 0) {
84687219db7SVictor Latushkin 		c = zopt_object[0];
84787219db7SVictor Latushkin 
84887219db7SVictor Latushkin 		if (c >= children)
84987219db7SVictor Latushkin 			(void) fatal("bad vdev id: %llu", (u_longlong_t)c);
850fa9e4066Sahrens 
85187219db7SVictor Latushkin 		if (zopt_objects > 1) {
85287219db7SVictor Latushkin 			vd = rvd->vdev_child[c];
85387219db7SVictor Latushkin 			print_vdev_metaslab_header(vd);
85487219db7SVictor Latushkin 
85587219db7SVictor Latushkin 			for (m = 1; m < zopt_objects; m++) {
85687219db7SVictor Latushkin 				if (zopt_object[m] < vd->vdev_ms_count)
85787219db7SVictor Latushkin 					dump_metaslab(
85887219db7SVictor Latushkin 					    vd->vdev_ms[zopt_object[m]]);
85987219db7SVictor Latushkin 				else
86087219db7SVictor Latushkin 					(void) fprintf(stderr, "bad metaslab "
86187219db7SVictor Latushkin 					    "number %llu\n",
86287219db7SVictor Latushkin 					    (u_longlong_t)zopt_object[m]);
86387219db7SVictor Latushkin 			}
86487219db7SVictor Latushkin 			(void) printf("\n");
86587219db7SVictor Latushkin 			return;
86687219db7SVictor Latushkin 		}
86787219db7SVictor Latushkin 		children = c + 1;
86887219db7SVictor Latushkin 	}
86987219db7SVictor Latushkin 	for (; c < children; c++) {
87087219db7SVictor Latushkin 		vd = rvd->vdev_child[c];
87187219db7SVictor Latushkin 		print_vdev_metaslab_header(vd);
872fa9e4066Sahrens 
873fa9e4066Sahrens 		for (m = 0; m < vd->vdev_ms_count; m++)
874fa9e4066Sahrens 			dump_metaslab(vd->vdev_ms[m]);
875fa9e4066Sahrens 		(void) printf("\n");
876fa9e4066Sahrens 	}
877fa9e4066Sahrens }
878fa9e4066Sahrens 
879b24ab676SJeff Bonwick static void
880b24ab676SJeff Bonwick dump_dde(const ddt_t *ddt, const ddt_entry_t *dde, uint64_t index)
881b24ab676SJeff Bonwick {
882b24ab676SJeff Bonwick 	const ddt_phys_t *ddp = dde->dde_phys;
883b24ab676SJeff Bonwick 	const ddt_key_t *ddk = &dde->dde_key;
884b24ab676SJeff Bonwick 	char *types[4] = { "ditto", "single", "double", "triple" };
885b24ab676SJeff Bonwick 	char blkbuf[BP_SPRINTF_LEN];
886b24ab676SJeff Bonwick 	blkptr_t blk;
887b24ab676SJeff Bonwick 
888b24ab676SJeff Bonwick 	for (int p = 0; p < DDT_PHYS_TYPES; p++, ddp++) {
889b24ab676SJeff Bonwick 		if (ddp->ddp_phys_birth == 0)
890b24ab676SJeff Bonwick 			continue;
891bbfd46c4SJeff Bonwick 		ddt_bp_create(ddt->ddt_checksum, ddk, ddp, &blk);
89243466aaeSMax Grossman 		snprintf_blkptr(blkbuf, sizeof (blkbuf), &blk);
893b24ab676SJeff Bonwick 		(void) printf("index %llx refcnt %llu %s %s\n",
894b24ab676SJeff Bonwick 		    (u_longlong_t)index, (u_longlong_t)ddp->ddp_refcnt,
895b24ab676SJeff Bonwick 		    types[p], blkbuf);
896b24ab676SJeff Bonwick 	}
897b24ab676SJeff Bonwick }
898b24ab676SJeff Bonwick 
899b24ab676SJeff Bonwick static void
900b24ab676SJeff Bonwick dump_dedup_ratio(const ddt_stat_t *dds)
901b24ab676SJeff Bonwick {
902b24ab676SJeff Bonwick 	double rL, rP, rD, D, dedup, compress, copies;
903b24ab676SJeff Bonwick 
904b24ab676SJeff Bonwick 	if (dds->dds_blocks == 0)
905b24ab676SJeff Bonwick 		return;
906b24ab676SJeff Bonwick 
907b24ab676SJeff Bonwick 	rL = (double)dds->dds_ref_lsize;
908b24ab676SJeff Bonwick 	rP = (double)dds->dds_ref_psize;
909b24ab676SJeff Bonwick 	rD = (double)dds->dds_ref_dsize;
910b24ab676SJeff Bonwick 	D = (double)dds->dds_dsize;
911b24ab676SJeff Bonwick 
912b24ab676SJeff Bonwick 	dedup = rD / D;
913b24ab676SJeff Bonwick 	compress = rL / rP;
914b24ab676SJeff Bonwick 	copies = rD / rP;
915b24ab676SJeff Bonwick 
916b24ab676SJeff Bonwick 	(void) printf("dedup = %.2f, compress = %.2f, copies = %.2f, "
917b24ab676SJeff Bonwick 	    "dedup * compress / copies = %.2f\n\n",
918b24ab676SJeff Bonwick 	    dedup, compress, copies, dedup * compress / copies);
919b24ab676SJeff Bonwick }
920b24ab676SJeff Bonwick 
921b24ab676SJeff Bonwick static void
922b24ab676SJeff Bonwick dump_ddt(ddt_t *ddt, enum ddt_type type, enum ddt_class class)
923b24ab676SJeff Bonwick {
924b24ab676SJeff Bonwick 	char name[DDT_NAMELEN];
925b24ab676SJeff Bonwick 	ddt_entry_t dde;
926b24ab676SJeff Bonwick 	uint64_t walk = 0;
927b24ab676SJeff Bonwick 	dmu_object_info_t doi;
928b24ab676SJeff Bonwick 	uint64_t count, dspace, mspace;
929b24ab676SJeff Bonwick 	int error;
930b24ab676SJeff Bonwick 
931b24ab676SJeff Bonwick 	error = ddt_object_info(ddt, type, class, &doi);
932b24ab676SJeff Bonwick 
933b24ab676SJeff Bonwick 	if (error == ENOENT)
934b24ab676SJeff Bonwick 		return;
935b24ab676SJeff Bonwick 	ASSERT(error == 0);
936b24ab676SJeff Bonwick 
9377448a079SGeorge Wilson 	if ((count = ddt_object_count(ddt, type, class)) == 0)
9387448a079SGeorge Wilson 		return;
9397448a079SGeorge Wilson 
940b24ab676SJeff Bonwick 	dspace = doi.doi_physical_blocks_512 << 9;
941b24ab676SJeff Bonwick 	mspace = doi.doi_fill_count * doi.doi_data_block_size;
942b24ab676SJeff Bonwick 
943b24ab676SJeff Bonwick 	ddt_object_name(ddt, type, class, name);
944b24ab676SJeff Bonwick 
945b24ab676SJeff Bonwick 	(void) printf("%s: %llu entries, size %llu on disk, %llu in core\n",
946b24ab676SJeff Bonwick 	    name,
947b24ab676SJeff Bonwick 	    (u_longlong_t)count,
948b24ab676SJeff Bonwick 	    (u_longlong_t)(dspace / count),
949b24ab676SJeff Bonwick 	    (u_longlong_t)(mspace / count));
950b24ab676SJeff Bonwick 
951b24ab676SJeff Bonwick 	if (dump_opt['D'] < 3)
952b24ab676SJeff Bonwick 		return;
953b24ab676SJeff Bonwick 
9549eb19f4dSGeorge Wilson 	zpool_dump_ddt(NULL, &ddt->ddt_histogram[type][class]);
955b24ab676SJeff Bonwick 
956b24ab676SJeff Bonwick 	if (dump_opt['D'] < 4)
957b24ab676SJeff Bonwick 		return;
958b24ab676SJeff Bonwick 
959b24ab676SJeff Bonwick 	if (dump_opt['D'] < 5 && class == DDT_CLASS_UNIQUE)
960b24ab676SJeff Bonwick 		return;
961b24ab676SJeff Bonwick 
962b24ab676SJeff Bonwick 	(void) printf("%s contents:\n\n", name);
963b24ab676SJeff Bonwick 
964bbfd46c4SJeff Bonwick 	while ((error = ddt_object_walk(ddt, type, class, &walk, &dde)) == 0)
965b24ab676SJeff Bonwick 		dump_dde(ddt, &dde, walk);
966b24ab676SJeff Bonwick 
967b24ab676SJeff Bonwick 	ASSERT(error == ENOENT);
968b24ab676SJeff Bonwick 
969b24ab676SJeff Bonwick 	(void) printf("\n");
970b24ab676SJeff Bonwick }
971b24ab676SJeff Bonwick 
972b24ab676SJeff Bonwick static void
973b24ab676SJeff Bonwick dump_all_ddts(spa_t *spa)
974b24ab676SJeff Bonwick {
975b24ab676SJeff Bonwick 	ddt_histogram_t ddh_total = { 0 };
976b24ab676SJeff Bonwick 	ddt_stat_t dds_total = { 0 };
977b24ab676SJeff Bonwick 
978b24ab676SJeff Bonwick 	for (enum zio_checksum c = 0; c < ZIO_CHECKSUM_FUNCTIONS; c++) {
979b24ab676SJeff Bonwick 		ddt_t *ddt = spa->spa_ddt[c];
980b24ab676SJeff Bonwick 		for (enum ddt_type type = 0; type < DDT_TYPES; type++) {
981b24ab676SJeff Bonwick 			for (enum ddt_class class = 0; class < DDT_CLASSES;
982b24ab676SJeff Bonwick 			    class++) {
983b24ab676SJeff Bonwick 				dump_ddt(ddt, type, class);
984b24ab676SJeff Bonwick 			}
985b24ab676SJeff Bonwick 		}
986b24ab676SJeff Bonwick 	}
987b24ab676SJeff Bonwick 
9889eb19f4dSGeorge Wilson 	ddt_get_dedup_stats(spa, &dds_total);
989b24ab676SJeff Bonwick 
990b24ab676SJeff Bonwick 	if (dds_total.dds_blocks == 0) {
991b24ab676SJeff Bonwick 		(void) printf("All DDTs are empty\n");
992b24ab676SJeff Bonwick 		return;
993b24ab676SJeff Bonwick 	}
994b24ab676SJeff Bonwick 
995b24ab676SJeff Bonwick 	(void) printf("\n");
996b24ab676SJeff Bonwick 
997b24ab676SJeff Bonwick 	if (dump_opt['D'] > 1) {
998b24ab676SJeff Bonwick 		(void) printf("DDT histogram (aggregated over all DDTs):\n");
9999eb19f4dSGeorge Wilson 		ddt_get_dedup_histogram(spa, &ddh_total);
10009eb19f4dSGeorge Wilson 		zpool_dump_ddt(&dds_total, &ddh_total);
1001b24ab676SJeff Bonwick 	}
1002b24ab676SJeff Bonwick 
1003b24ab676SJeff Bonwick 	dump_dedup_ratio(&dds_total);
1004b24ab676SJeff Bonwick }
1005b24ab676SJeff Bonwick 
10068ad4d6ddSJeff Bonwick static void
10070713e232SGeorge Wilson dump_dtl_seg(void *arg, uint64_t start, uint64_t size)
10088ad4d6ddSJeff Bonwick {
10090713e232SGeorge Wilson 	char *prefix = arg;
10108ad4d6ddSJeff Bonwick 
10118ad4d6ddSJeff Bonwick 	(void) printf("%s [%llu,%llu) length %llu\n",
10128ad4d6ddSJeff Bonwick 	    prefix,
10138ad4d6ddSJeff Bonwick 	    (u_longlong_t)start,
10148ad4d6ddSJeff Bonwick 	    (u_longlong_t)(start + size),
10158ad4d6ddSJeff Bonwick 	    (u_longlong_t)(size));
10168ad4d6ddSJeff Bonwick }
10178ad4d6ddSJeff Bonwick 
1018fa9e4066Sahrens static void
1019fa9e4066Sahrens dump_dtl(vdev_t *vd, int indent)
1020fa9e4066Sahrens {
10218ad4d6ddSJeff Bonwick 	spa_t *spa = vd->vdev_spa;
10228ad4d6ddSJeff Bonwick 	boolean_t required;
10238ad4d6ddSJeff Bonwick 	char *name[DTL_TYPES] = { "missing", "partial", "scrub", "outage" };
10248ad4d6ddSJeff Bonwick 	char prefix[256];
10258ad4d6ddSJeff Bonwick 
10268f18d1faSGeorge Wilson 	spa_vdev_state_enter(spa, SCL_NONE);
10278ad4d6ddSJeff Bonwick 	required = vdev_dtl_required(vd);
10288ad4d6ddSJeff Bonwick 	(void) spa_vdev_state_exit(spa, NULL, 0);
1029fa9e4066Sahrens 
1030fa9e4066Sahrens 	if (indent == 0)
1031fa9e4066Sahrens 		(void) printf("\nDirty time logs:\n\n");
1032fa9e4066Sahrens 
10338ad4d6ddSJeff Bonwick 	(void) printf("\t%*s%s [%s]\n", indent, "",
1034e14bb325SJeff Bonwick 	    vd->vdev_path ? vd->vdev_path :
10358ad4d6ddSJeff Bonwick 	    vd->vdev_parent ? vd->vdev_ops->vdev_op_type : spa_name(spa),
10368ad4d6ddSJeff Bonwick 	    required ? "DTL-required" : "DTL-expendable");
1037fa9e4066Sahrens 
10388ad4d6ddSJeff Bonwick 	for (int t = 0; t < DTL_TYPES; t++) {
10390713e232SGeorge Wilson 		range_tree_t *rt = vd->vdev_dtl[t];
10400713e232SGeorge Wilson 		if (range_tree_space(rt) == 0)
10418ad4d6ddSJeff Bonwick 			continue;
10428ad4d6ddSJeff Bonwick 		(void) snprintf(prefix, sizeof (prefix), "\t%*s%s",
10438ad4d6ddSJeff Bonwick 		    indent + 2, "", name[t]);
10440713e232SGeorge Wilson 		mutex_enter(rt->rt_lock);
10450713e232SGeorge Wilson 		range_tree_walk(rt, dump_dtl_seg, prefix);
10460713e232SGeorge Wilson 		mutex_exit(rt->rt_lock);
10478ad4d6ddSJeff Bonwick 		if (dump_opt['d'] > 5 && vd->vdev_children == 0)
10480713e232SGeorge Wilson 			dump_spacemap(spa->spa_meta_objset, vd->vdev_dtl_sm);
1049fa9e4066Sahrens 	}
1050fa9e4066Sahrens 
10518ad4d6ddSJeff Bonwick 	for (int c = 0; c < vd->vdev_children; c++)
1052fa9e4066Sahrens 		dump_dtl(vd->vdev_child[c], indent + 4);
1053fa9e4066Sahrens }
1054fa9e4066Sahrens 
10558f18d1faSGeorge Wilson static void
10568f18d1faSGeorge Wilson dump_history(spa_t *spa)
10578f18d1faSGeorge Wilson {
10588f18d1faSGeorge Wilson 	nvlist_t **events = NULL;
1059e4161df6SVictor Latushkin 	uint64_t resid, len, off = 0;
10608f18d1faSGeorge Wilson 	uint_t num = 0;
10618f18d1faSGeorge Wilson 	int error;
10628f18d1faSGeorge Wilson 	time_t tsec;
10638f18d1faSGeorge Wilson 	struct tm t;
10648f18d1faSGeorge Wilson 	char tbuf[30];
10658f18d1faSGeorge Wilson 	char internalstr[MAXPATHLEN];
10668f18d1faSGeorge Wilson 
10673f84190cSMatthew Ahrens 	char *buf = umem_alloc(SPA_MAXBLOCKSIZE, UMEM_NOFAIL);
10688f18d1faSGeorge Wilson 	do {
10693f84190cSMatthew Ahrens 		len = SPA_MAXBLOCKSIZE;
1070e4161df6SVictor Latushkin 
10718f18d1faSGeorge Wilson 		if ((error = spa_history_get(spa, &off, &len, buf)) != 0) {
10728f18d1faSGeorge Wilson 			(void) fprintf(stderr, "Unable to read history: "
10738f18d1faSGeorge Wilson 			    "error %d\n", error);
10743f84190cSMatthew Ahrens 			umem_free(buf, SPA_MAXBLOCKSIZE);
10758f18d1faSGeorge Wilson 			return;
10768f18d1faSGeorge Wilson 		}
10778f18d1faSGeorge Wilson 
10788f18d1faSGeorge Wilson 		if (zpool_history_unpack(buf, len, &resid, &events, &num) != 0)
10798f18d1faSGeorge Wilson 			break;
10808f18d1faSGeorge Wilson 
10818f18d1faSGeorge Wilson 		off -= resid;
10828f18d1faSGeorge Wilson 	} while (len != 0);
10833f84190cSMatthew Ahrens 	umem_free(buf, SPA_MAXBLOCKSIZE);
10848f18d1faSGeorge Wilson 
10858f18d1faSGeorge Wilson 	(void) printf("\nHistory:\n");
10868f18d1faSGeorge Wilson 	for (int i = 0; i < num; i++) {
10878f18d1faSGeorge Wilson 		uint64_t time, txg, ievent;
10888f18d1faSGeorge Wilson 		char *cmd, *intstr;
10894445fffbSMatthew Ahrens 		boolean_t printed = B_FALSE;
10908f18d1faSGeorge Wilson 
10918f18d1faSGeorge Wilson 		if (nvlist_lookup_uint64(events[i], ZPOOL_HIST_TIME,
10928f18d1faSGeorge Wilson 		    &time) != 0)
10934445fffbSMatthew Ahrens 			goto next;
10948f18d1faSGeorge Wilson 		if (nvlist_lookup_string(events[i], ZPOOL_HIST_CMD,
10958f18d1faSGeorge Wilson 		    &cmd) != 0) {
10968f18d1faSGeorge Wilson 			if (nvlist_lookup_uint64(events[i],
10978f18d1faSGeorge Wilson 			    ZPOOL_HIST_INT_EVENT, &ievent) != 0)
10984445fffbSMatthew Ahrens 				goto next;
10998f18d1faSGeorge Wilson 			verify(nvlist_lookup_uint64(events[i],
11008f18d1faSGeorge Wilson 			    ZPOOL_HIST_TXG, &txg) == 0);
11018f18d1faSGeorge Wilson 			verify(nvlist_lookup_string(events[i],
11028f18d1faSGeorge Wilson 			    ZPOOL_HIST_INT_STR, &intstr) == 0);
11034445fffbSMatthew Ahrens 			if (ievent >= ZFS_NUM_LEGACY_HISTORY_EVENTS)
11044445fffbSMatthew Ahrens 				goto next;
11058f18d1faSGeorge Wilson 
11068f18d1faSGeorge Wilson 			(void) snprintf(internalstr,
11078f18d1faSGeorge Wilson 			    sizeof (internalstr),
11088f18d1faSGeorge Wilson 			    "[internal %s txg:%lld] %s",
11093f9d6ad7SLin Ling 			    zfs_history_event_names[ievent], txg,
11108f18d1faSGeorge Wilson 			    intstr);
11118f18d1faSGeorge Wilson 			cmd = internalstr;
11128f18d1faSGeorge Wilson 		}
11138f18d1faSGeorge Wilson 		tsec = time;
11148f18d1faSGeorge Wilson 		(void) localtime_r(&tsec, &t);
11158f18d1faSGeorge Wilson 		(void) strftime(tbuf, sizeof (tbuf), "%F.%T", &t);
11168f18d1faSGeorge Wilson 		(void) printf("%s %s\n", tbuf, cmd);
11174445fffbSMatthew Ahrens 		printed = B_TRUE;
11184445fffbSMatthew Ahrens 
11194445fffbSMatthew Ahrens next:
11204445fffbSMatthew Ahrens 		if (dump_opt['h'] > 1) {
11214445fffbSMatthew Ahrens 			if (!printed)
11224445fffbSMatthew Ahrens 				(void) printf("unrecognized record:\n");
11234445fffbSMatthew Ahrens 			dump_nvlist(events[i], 2);
11244445fffbSMatthew Ahrens 		}
11258f18d1faSGeorge Wilson 	}
11268f18d1faSGeorge Wilson }
11278f18d1faSGeorge Wilson 
1128fa9e4066Sahrens /*ARGSUSED*/
1129fa9e4066Sahrens static void
1130fa9e4066Sahrens dump_dnode(objset_t *os, uint64_t object, void *data, size_t size)
1131fa9e4066Sahrens {
1132fa9e4066Sahrens }
1133fa9e4066Sahrens 
1134fa9e4066Sahrens static uint64_t
11357802d7bfSMatthew Ahrens blkid2offset(const dnode_phys_t *dnp, const blkptr_t *bp,
11367802d7bfSMatthew Ahrens     const zbookmark_phys_t *zb)
1137fa9e4066Sahrens {
1138b24ab676SJeff Bonwick 	if (dnp == NULL) {
1139b24ab676SJeff Bonwick 		ASSERT(zb->zb_level < 0);
1140b24ab676SJeff Bonwick 		if (zb->zb_object == 0)
1141b24ab676SJeff Bonwick 			return (zb->zb_blkid);
1142b24ab676SJeff Bonwick 		return (zb->zb_blkid * BP_GET_LSIZE(bp));
1143b24ab676SJeff Bonwick 	}
1144b24ab676SJeff Bonwick 
1145b24ab676SJeff Bonwick 	ASSERT(zb->zb_level >= 0);
1146fa9e4066Sahrens 
1147b24ab676SJeff Bonwick 	return ((zb->zb_blkid <<
1148b24ab676SJeff Bonwick 	    (zb->zb_level * (dnp->dn_indblkshift - SPA_BLKPTRSHIFT))) *
1149fa9e4066Sahrens 	    dnp->dn_datablkszsec << SPA_MINBLOCKSHIFT);
1150fa9e4066Sahrens }
1151fa9e4066Sahrens 
115244cd46caSbillm static void
115343466aaeSMax Grossman snprintf_blkptr_compact(char *blkbuf, size_t buflen, const blkptr_t *bp)
115444cd46caSbillm {
1155cde58dbcSMatthew Ahrens 	const dva_t *dva = bp->blk_dva;
1156b24ab676SJeff Bonwick 	int ndvas = dump_opt['d'] > 5 ? BP_GET_NDVAS(bp) : 1;
1157b24ab676SJeff Bonwick 
1158490d05b9SMatthew Ahrens 	if (dump_opt['b'] >= 6) {
115943466aaeSMax Grossman 		snprintf_blkptr(blkbuf, buflen, bp);
1160b24ab676SJeff Bonwick 		return;
1161b24ab676SJeff Bonwick 	}
116244cd46caSbillm 
11635d7b4d43SMatthew Ahrens 	if (BP_IS_EMBEDDED(bp)) {
11645d7b4d43SMatthew Ahrens 		(void) sprintf(blkbuf,
11655d7b4d43SMatthew Ahrens 		    "EMBEDDED et=%u %llxL/%llxP B=%llu",
11665d7b4d43SMatthew Ahrens 		    (int)BPE_GET_ETYPE(bp),
11675d7b4d43SMatthew Ahrens 		    (u_longlong_t)BPE_GET_LSIZE(bp),
11685d7b4d43SMatthew Ahrens 		    (u_longlong_t)BPE_GET_PSIZE(bp),
11695d7b4d43SMatthew Ahrens 		    (u_longlong_t)bp->blk_birth);
11705d7b4d43SMatthew Ahrens 		return;
11715d7b4d43SMatthew Ahrens 	}
117244cd46caSbillm 
11735d7b4d43SMatthew Ahrens 	blkbuf[0] = '\0';
1174b24ab676SJeff Bonwick 	for (int i = 0; i < ndvas; i++)
117543466aaeSMax Grossman 		(void) snprintf(blkbuf + strlen(blkbuf),
117643466aaeSMax Grossman 		    buflen - strlen(blkbuf), "%llu:%llx:%llx ",
117744cd46caSbillm 		    (u_longlong_t)DVA_GET_VDEV(&dva[i]),
117844cd46caSbillm 		    (u_longlong_t)DVA_GET_OFFSET(&dva[i]),
117944cd46caSbillm 		    (u_longlong_t)DVA_GET_ASIZE(&dva[i]));
118044cd46caSbillm 
118143466aaeSMax Grossman 	if (BP_IS_HOLE(bp)) {
118243466aaeSMax Grossman 		(void) snprintf(blkbuf + strlen(blkbuf),
118370163ac5SPrakash Surya 		    buflen - strlen(blkbuf),
118470163ac5SPrakash Surya 		    "%llxL B=%llu",
118570163ac5SPrakash Surya 		    (u_longlong_t)BP_GET_LSIZE(bp),
118643466aaeSMax Grossman 		    (u_longlong_t)bp->blk_birth);
118743466aaeSMax Grossman 	} else {
118843466aaeSMax Grossman 		(void) snprintf(blkbuf + strlen(blkbuf),
118943466aaeSMax Grossman 		    buflen - strlen(blkbuf),
119043466aaeSMax Grossman 		    "%llxL/%llxP F=%llu B=%llu/%llu",
119143466aaeSMax Grossman 		    (u_longlong_t)BP_GET_LSIZE(bp),
119243466aaeSMax Grossman 		    (u_longlong_t)BP_GET_PSIZE(bp),
11935d7b4d43SMatthew Ahrens 		    (u_longlong_t)BP_GET_FILL(bp),
119443466aaeSMax Grossman 		    (u_longlong_t)bp->blk_birth,
119543466aaeSMax Grossman 		    (u_longlong_t)BP_PHYSICAL_BIRTH(bp));
119643466aaeSMax Grossman 	}
119744cd46caSbillm }
119844cd46caSbillm 
119988b7b0f2SMatthew Ahrens static void
12007802d7bfSMatthew Ahrens print_indirect(blkptr_t *bp, const zbookmark_phys_t *zb,
120188b7b0f2SMatthew Ahrens     const dnode_phys_t *dnp)
1202fa9e4066Sahrens {
120388b7b0f2SMatthew Ahrens 	char blkbuf[BP_SPRINTF_LEN];
1204fa9e4066Sahrens 	int l;
1205fa9e4066Sahrens 
12065d7b4d43SMatthew Ahrens 	if (!BP_IS_EMBEDDED(bp)) {
12075d7b4d43SMatthew Ahrens 		ASSERT3U(BP_GET_TYPE(bp), ==, dnp->dn_type);
12085d7b4d43SMatthew Ahrens 		ASSERT3U(BP_GET_LEVEL(bp), ==, zb->zb_level);
12095d7b4d43SMatthew Ahrens 	}
1210fa9e4066Sahrens 
1211b24ab676SJeff Bonwick 	(void) printf("%16llx ", (u_longlong_t)blkid2offset(dnp, bp, zb));
1212fa9e4066Sahrens 
1213fa9e4066Sahrens 	ASSERT(zb->zb_level >= 0);
1214fa9e4066Sahrens 
1215fa9e4066Sahrens 	for (l = dnp->dn_nlevels - 1; l >= -1; l--) {
1216fa9e4066Sahrens 		if (l == zb->zb_level) {
121788b7b0f2SMatthew Ahrens 			(void) printf("L%llx", (u_longlong_t)zb->zb_level);
1218fa9e4066Sahrens 		} else {
121988b7b0f2SMatthew Ahrens 			(void) printf(" ");
1220fa9e4066Sahrens 		}
1221fa9e4066Sahrens 	}
1222fa9e4066Sahrens 
122343466aaeSMax Grossman 	snprintf_blkptr_compact(blkbuf, sizeof (blkbuf), bp);
122488b7b0f2SMatthew Ahrens 	(void) printf("%s\n", blkbuf);
122588b7b0f2SMatthew Ahrens }
122688b7b0f2SMatthew Ahrens 
122788b7b0f2SMatthew Ahrens static int
122888b7b0f2SMatthew Ahrens visit_indirect(spa_t *spa, const dnode_phys_t *dnp,
12297802d7bfSMatthew Ahrens     blkptr_t *bp, const zbookmark_phys_t *zb)
123088b7b0f2SMatthew Ahrens {
1231e4161df6SVictor Latushkin 	int err = 0;
123288b7b0f2SMatthew Ahrens 
123388b7b0f2SMatthew Ahrens 	if (bp->blk_birth == 0)
123488b7b0f2SMatthew Ahrens 		return (0);
123588b7b0f2SMatthew Ahrens 
123688b7b0f2SMatthew Ahrens 	print_indirect(bp, zb, dnp);
123788b7b0f2SMatthew Ahrens 
123843466aaeSMax Grossman 	if (BP_GET_LEVEL(bp) > 0 && !BP_IS_HOLE(bp)) {
12397adb730bSGeorge Wilson 		arc_flags_t flags = ARC_FLAG_WAIT;
124088b7b0f2SMatthew Ahrens 		int i;
124188b7b0f2SMatthew Ahrens 		blkptr_t *cbp;
124288b7b0f2SMatthew Ahrens 		int epb = BP_GET_LSIZE(bp) >> SPA_BLKPTRSHIFT;
124388b7b0f2SMatthew Ahrens 		arc_buf_t *buf;
124488b7b0f2SMatthew Ahrens 		uint64_t fill = 0;
124588b7b0f2SMatthew Ahrens 
12461b912ec7SGeorge Wilson 		err = arc_read(NULL, spa, bp, arc_getbuf_func, &buf,
124788b7b0f2SMatthew Ahrens 		    ZIO_PRIORITY_ASYNC_READ, ZIO_FLAG_CANFAIL, &flags, zb);
124888b7b0f2SMatthew Ahrens 		if (err)
124988b7b0f2SMatthew Ahrens 			return (err);
12503f9d6ad7SLin Ling 		ASSERT(buf->b_data);
125188b7b0f2SMatthew Ahrens 
125288b7b0f2SMatthew Ahrens 		/* recursively visit blocks below this */
125388b7b0f2SMatthew Ahrens 		cbp = buf->b_data;
125488b7b0f2SMatthew Ahrens 		for (i = 0; i < epb; i++, cbp++) {
12557802d7bfSMatthew Ahrens 			zbookmark_phys_t czb;
125688b7b0f2SMatthew Ahrens 
125788b7b0f2SMatthew Ahrens 			SET_BOOKMARK(&czb, zb->zb_objset, zb->zb_object,
125888b7b0f2SMatthew Ahrens 			    zb->zb_level - 1,
125988b7b0f2SMatthew Ahrens 			    zb->zb_blkid * epb + i);
126088b7b0f2SMatthew Ahrens 			err = visit_indirect(spa, dnp, cbp, &czb);
126188b7b0f2SMatthew Ahrens 			if (err)
126288b7b0f2SMatthew Ahrens 				break;
12635d7b4d43SMatthew Ahrens 			fill += BP_GET_FILL(cbp);
126488b7b0f2SMatthew Ahrens 		}
12658ad4d6ddSJeff Bonwick 		if (!err)
12665d7b4d43SMatthew Ahrens 			ASSERT3U(fill, ==, BP_GET_FILL(bp));
126788b7b0f2SMatthew Ahrens 		(void) arc_buf_remove_ref(buf, &buf);
1268fa9e4066Sahrens 	}
1269fa9e4066Sahrens 
127088b7b0f2SMatthew Ahrens 	return (err);
1271fa9e4066Sahrens }
1272fa9e4066Sahrens 
1273fa9e4066Sahrens /*ARGSUSED*/
1274fa9e4066Sahrens static void
127588b7b0f2SMatthew Ahrens dump_indirect(dnode_t *dn)
1276fa9e4066Sahrens {
127788b7b0f2SMatthew Ahrens 	dnode_phys_t *dnp = dn->dn_phys;
127888b7b0f2SMatthew Ahrens 	int j;
12797802d7bfSMatthew Ahrens 	zbookmark_phys_t czb;
1280fa9e4066Sahrens 
1281fa9e4066Sahrens 	(void) printf("Indirect blocks:\n");
1282fa9e4066Sahrens 
1283503ad85cSMatthew Ahrens 	SET_BOOKMARK(&czb, dmu_objset_id(dn->dn_objset),
128488b7b0f2SMatthew Ahrens 	    dn->dn_object, dnp->dn_nlevels - 1, 0);
128588b7b0f2SMatthew Ahrens 	for (j = 0; j < dnp->dn_nblkptr; j++) {
128688b7b0f2SMatthew Ahrens 		czb.zb_blkid = j;
1287503ad85cSMatthew Ahrens 		(void) visit_indirect(dmu_objset_spa(dn->dn_objset), dnp,
128888b7b0f2SMatthew Ahrens 		    &dnp->dn_blkptr[j], &czb);
128988b7b0f2SMatthew Ahrens 	}
1290fa9e4066Sahrens 
1291fa9e4066Sahrens 	(void) printf("\n");
1292fa9e4066Sahrens }
1293fa9e4066Sahrens 
1294fa9e4066Sahrens /*ARGSUSED*/
1295fa9e4066Sahrens static void
1296fa9e4066Sahrens dump_dsl_dir(objset_t *os, uint64_t object, void *data, size_t size)
1297fa9e4066Sahrens {
1298fa9e4066Sahrens 	dsl_dir_phys_t *dd = data;
1299fa9e4066Sahrens 	time_t crtime;
13003f9d6ad7SLin Ling 	char nice[32];
1301fa9e4066Sahrens 
1302fa9e4066Sahrens 	if (dd == NULL)
1303fa9e4066Sahrens 		return;
1304fa9e4066Sahrens 
1305da6c28aaSamw 	ASSERT3U(size, >=, sizeof (dsl_dir_phys_t));
1306fa9e4066Sahrens 
1307fa9e4066Sahrens 	crtime = dd->dd_creation_time;
1308fa9e4066Sahrens 	(void) printf("\t\tcreation_time = %s", ctime(&crtime));
1309fa9e4066Sahrens 	(void) printf("\t\thead_dataset_obj = %llu\n",
1310fa9e4066Sahrens 	    (u_longlong_t)dd->dd_head_dataset_obj);
1311fa9e4066Sahrens 	(void) printf("\t\tparent_dir_obj = %llu\n",
1312fa9e4066Sahrens 	    (u_longlong_t)dd->dd_parent_obj);
13133cb34c60Sahrens 	(void) printf("\t\torigin_obj = %llu\n",
13143cb34c60Sahrens 	    (u_longlong_t)dd->dd_origin_obj);
1315fa9e4066Sahrens 	(void) printf("\t\tchild_dir_zapobj = %llu\n",
1316fa9e4066Sahrens 	    (u_longlong_t)dd->dd_child_dir_zapobj);
13173f9d6ad7SLin Ling 	zdb_nicenum(dd->dd_used_bytes, nice);
131874e7dc98SMatthew Ahrens 	(void) printf("\t\tused_bytes = %s\n", nice);
13193f9d6ad7SLin Ling 	zdb_nicenum(dd->dd_compressed_bytes, nice);
132074e7dc98SMatthew Ahrens 	(void) printf("\t\tcompressed_bytes = %s\n", nice);
13213f9d6ad7SLin Ling 	zdb_nicenum(dd->dd_uncompressed_bytes, nice);
132274e7dc98SMatthew Ahrens 	(void) printf("\t\tuncompressed_bytes = %s\n", nice);
13233f9d6ad7SLin Ling 	zdb_nicenum(dd->dd_quota, nice);
132474e7dc98SMatthew Ahrens 	(void) printf("\t\tquota = %s\n", nice);
13253f9d6ad7SLin Ling 	zdb_nicenum(dd->dd_reserved, nice);
132674e7dc98SMatthew Ahrens 	(void) printf("\t\treserved = %s\n", nice);
1327fa9e4066Sahrens 	(void) printf("\t\tprops_zapobj = %llu\n",
1328fa9e4066Sahrens 	    (u_longlong_t)dd->dd_props_zapobj);
1329ecd6cf80Smarks 	(void) printf("\t\tdeleg_zapobj = %llu\n",
1330ecd6cf80Smarks 	    (u_longlong_t)dd->dd_deleg_zapobj);
133174e7dc98SMatthew Ahrens 	(void) printf("\t\tflags = %llx\n",
133274e7dc98SMatthew Ahrens 	    (u_longlong_t)dd->dd_flags);
133374e7dc98SMatthew Ahrens 
133474e7dc98SMatthew Ahrens #define	DO(which) \
13353f9d6ad7SLin Ling 	zdb_nicenum(dd->dd_used_breakdown[DD_USED_ ## which], nice); \
133674e7dc98SMatthew Ahrens 	(void) printf("\t\tused_breakdown[" #which "] = %s\n", nice)
133774e7dc98SMatthew Ahrens 	DO(HEAD);
133874e7dc98SMatthew Ahrens 	DO(SNAP);
133974e7dc98SMatthew Ahrens 	DO(CHILD);
134074e7dc98SMatthew Ahrens 	DO(CHILD_RSRV);
134174e7dc98SMatthew Ahrens 	DO(REFRSRV);
134274e7dc98SMatthew Ahrens #undef DO
1343fa9e4066Sahrens }
1344fa9e4066Sahrens 
1345fa9e4066Sahrens /*ARGSUSED*/
1346fa9e4066Sahrens static void
1347fa9e4066Sahrens dump_dsl_dataset(objset_t *os, uint64_t object, void *data, size_t size)
1348fa9e4066Sahrens {
1349fa9e4066Sahrens 	dsl_dataset_phys_t *ds = data;
1350fa9e4066Sahrens 	time_t crtime;
13513f9d6ad7SLin Ling 	char used[32], compressed[32], uncompressed[32], unique[32];
1352fbabab8fSmaybee 	char blkbuf[BP_SPRINTF_LEN];
1353fa9e4066Sahrens 
1354fa9e4066Sahrens 	if (ds == NULL)
1355fa9e4066Sahrens 		return;
1356fa9e4066Sahrens 
1357fa9e4066Sahrens 	ASSERT(size == sizeof (*ds));
1358fa9e4066Sahrens 	crtime = ds->ds_creation_time;
1359ad135b5dSChristopher Siden 	zdb_nicenum(ds->ds_referenced_bytes, used);
13603f9d6ad7SLin Ling 	zdb_nicenum(ds->ds_compressed_bytes, compressed);
13613f9d6ad7SLin Ling 	zdb_nicenum(ds->ds_uncompressed_bytes, uncompressed);
13623f9d6ad7SLin Ling 	zdb_nicenum(ds->ds_unique_bytes, unique);
136343466aaeSMax Grossman 	snprintf_blkptr(blkbuf, sizeof (blkbuf), &ds->ds_bp);
1364fa9e4066Sahrens 
1365088f3894Sahrens 	(void) printf("\t\tdir_obj = %llu\n",
1366fa9e4066Sahrens 	    (u_longlong_t)ds->ds_dir_obj);
1367fa9e4066Sahrens 	(void) printf("\t\tprev_snap_obj = %llu\n",
1368fa9e4066Sahrens 	    (u_longlong_t)ds->ds_prev_snap_obj);
1369fa9e4066Sahrens 	(void) printf("\t\tprev_snap_txg = %llu\n",
1370fa9e4066Sahrens 	    (u_longlong_t)ds->ds_prev_snap_txg);
1371fa9e4066Sahrens 	(void) printf("\t\tnext_snap_obj = %llu\n",
1372fa9e4066Sahrens 	    (u_longlong_t)ds->ds_next_snap_obj);
1373fa9e4066Sahrens 	(void) printf("\t\tsnapnames_zapobj = %llu\n",
1374fa9e4066Sahrens 	    (u_longlong_t)ds->ds_snapnames_zapobj);
1375fa9e4066Sahrens 	(void) printf("\t\tnum_children = %llu\n",
1376fa9e4066Sahrens 	    (u_longlong_t)ds->ds_num_children);
1377842727c2SChris Kirby 	(void) printf("\t\tuserrefs_obj = %llu\n",
1378842727c2SChris Kirby 	    (u_longlong_t)ds->ds_userrefs_obj);
1379fa9e4066Sahrens 	(void) printf("\t\tcreation_time = %s", ctime(&crtime));
1380fa9e4066Sahrens 	(void) printf("\t\tcreation_txg = %llu\n",
1381fa9e4066Sahrens 	    (u_longlong_t)ds->ds_creation_txg);
1382fa9e4066Sahrens 	(void) printf("\t\tdeadlist_obj = %llu\n",
1383fa9e4066Sahrens 	    (u_longlong_t)ds->ds_deadlist_obj);
1384fa9e4066Sahrens 	(void) printf("\t\tused_bytes = %s\n", used);
1385fa9e4066Sahrens 	(void) printf("\t\tcompressed_bytes = %s\n", compressed);
1386fa9e4066Sahrens 	(void) printf("\t\tuncompressed_bytes = %s\n", uncompressed);
1387fa9e4066Sahrens 	(void) printf("\t\tunique = %s\n", unique);
1388fa9e4066Sahrens 	(void) printf("\t\tfsid_guid = %llu\n",
1389fa9e4066Sahrens 	    (u_longlong_t)ds->ds_fsid_guid);
1390fa9e4066Sahrens 	(void) printf("\t\tguid = %llu\n",
1391fa9e4066Sahrens 	    (u_longlong_t)ds->ds_guid);
139299653d4eSeschrock 	(void) printf("\t\tflags = %llx\n",
139399653d4eSeschrock 	    (u_longlong_t)ds->ds_flags);
1394088f3894Sahrens 	(void) printf("\t\tnext_clones_obj = %llu\n",
1395088f3894Sahrens 	    (u_longlong_t)ds->ds_next_clones_obj);
1396bb0ade09Sahrens 	(void) printf("\t\tprops_obj = %llu\n",
1397bb0ade09Sahrens 	    (u_longlong_t)ds->ds_props_obj);
1398fa9e4066Sahrens 	(void) printf("\t\tbp = %s\n", blkbuf);
1399fa9e4066Sahrens }
1400fa9e4066Sahrens 
1401ad135b5dSChristopher Siden /* ARGSUSED */
1402ad135b5dSChristopher Siden static int
1403ad135b5dSChristopher Siden dump_bptree_cb(void *arg, const blkptr_t *bp, dmu_tx_t *tx)
1404ad135b5dSChristopher Siden {
1405ad135b5dSChristopher Siden 	char blkbuf[BP_SPRINTF_LEN];
1406ad135b5dSChristopher Siden 
1407ad135b5dSChristopher Siden 	if (bp->blk_birth != 0) {
140843466aaeSMax Grossman 		snprintf_blkptr(blkbuf, sizeof (blkbuf), bp);
1409ad135b5dSChristopher Siden 		(void) printf("\t%s\n", blkbuf);
1410ad135b5dSChristopher Siden 	}
1411ad135b5dSChristopher Siden 	return (0);
1412ad135b5dSChristopher Siden }
1413ad135b5dSChristopher Siden 
1414ad135b5dSChristopher Siden static void
1415ad135b5dSChristopher Siden dump_bptree(objset_t *os, uint64_t obj, char *name)
1416ad135b5dSChristopher Siden {
1417ad135b5dSChristopher Siden 	char bytes[32];
1418ad135b5dSChristopher Siden 	bptree_phys_t *bt;
1419ad135b5dSChristopher Siden 	dmu_buf_t *db;
1420ad135b5dSChristopher Siden 
1421ad135b5dSChristopher Siden 	if (dump_opt['d'] < 3)
1422ad135b5dSChristopher Siden 		return;
1423ad135b5dSChristopher Siden 
1424b420f3adSRichard Lowe 	VERIFY3U(0, ==, dmu_bonus_hold(os, obj, FTAG, &db));
1425ad135b5dSChristopher Siden 	bt = db->db_data;
1426ad135b5dSChristopher Siden 	zdb_nicenum(bt->bt_bytes, bytes);
1427ad135b5dSChristopher Siden 	(void) printf("\n    %s: %llu datasets, %s\n",
1428ad135b5dSChristopher Siden 	    name, (unsigned long long)(bt->bt_end - bt->bt_begin), bytes);
1429ad135b5dSChristopher Siden 	dmu_buf_rele(db, FTAG);
1430ad135b5dSChristopher Siden 
1431ad135b5dSChristopher Siden 	if (dump_opt['d'] < 5)
1432ad135b5dSChristopher Siden 		return;
1433ad135b5dSChristopher Siden 
1434ad135b5dSChristopher Siden 	(void) printf("\n");
1435ad135b5dSChristopher Siden 
1436ad135b5dSChristopher Siden 	(void) bptree_iterate(os, obj, B_FALSE, dump_bptree_cb, NULL, NULL);
1437ad135b5dSChristopher Siden }
1438ad135b5dSChristopher Siden 
1439cde58dbcSMatthew Ahrens /* ARGSUSED */
1440cde58dbcSMatthew Ahrens static int
1441cde58dbcSMatthew Ahrens dump_bpobj_cb(void *arg, const blkptr_t *bp, dmu_tx_t *tx)
1442cde58dbcSMatthew Ahrens {
1443cde58dbcSMatthew Ahrens 	char blkbuf[BP_SPRINTF_LEN];
1444cde58dbcSMatthew Ahrens 
1445cde58dbcSMatthew Ahrens 	ASSERT(bp->blk_birth != 0);
144643466aaeSMax Grossman 	snprintf_blkptr_compact(blkbuf, sizeof (blkbuf), bp);
1447cde58dbcSMatthew Ahrens 	(void) printf("\t%s\n", blkbuf);
1448cde58dbcSMatthew Ahrens 	return (0);
1449cde58dbcSMatthew Ahrens }
1450cde58dbcSMatthew Ahrens 
1451fa9e4066Sahrens static void
1452732885fcSMatthew Ahrens dump_full_bpobj(bpobj_t *bpo, char *name, int indent)
1453fa9e4066Sahrens {
14543f9d6ad7SLin Ling 	char bytes[32];
14553f9d6ad7SLin Ling 	char comp[32];
14563f9d6ad7SLin Ling 	char uncomp[32];
1457fa9e4066Sahrens 
1458fa9e4066Sahrens 	if (dump_opt['d'] < 3)
1459fa9e4066Sahrens 		return;
1460fa9e4066Sahrens 
1461cde58dbcSMatthew Ahrens 	zdb_nicenum(bpo->bpo_phys->bpo_bytes, bytes);
1462d0475637SMatthew Ahrens 	if (bpo->bpo_havesubobj && bpo->bpo_phys->bpo_subobjs != 0) {
1463cde58dbcSMatthew Ahrens 		zdb_nicenum(bpo->bpo_phys->bpo_comp, comp);
1464cde58dbcSMatthew Ahrens 		zdb_nicenum(bpo->bpo_phys->bpo_uncomp, uncomp);
1465d0475637SMatthew Ahrens 		(void) printf("    %*s: object %llu, %llu local blkptrs, "
1466732885fcSMatthew Ahrens 		    "%llu subobjs in object %llu, %s (%s/%s comp)\n",
1467d0475637SMatthew Ahrens 		    indent * 8, name,
1468d0475637SMatthew Ahrens 		    (u_longlong_t)bpo->bpo_object,
1469d0475637SMatthew Ahrens 		    (u_longlong_t)bpo->bpo_phys->bpo_num_blkptrs,
1470cde58dbcSMatthew Ahrens 		    (u_longlong_t)bpo->bpo_phys->bpo_num_subobjs,
1471732885fcSMatthew Ahrens 		    (u_longlong_t)bpo->bpo_phys->bpo_subobjs,
147299653d4eSeschrock 		    bytes, comp, uncomp);
1473d0475637SMatthew Ahrens 
1474d0475637SMatthew Ahrens 		for (uint64_t i = 0; i < bpo->bpo_phys->bpo_num_subobjs; i++) {
1475d0475637SMatthew Ahrens 			uint64_t subobj;
1476d0475637SMatthew Ahrens 			bpobj_t subbpo;
1477d0475637SMatthew Ahrens 			int error;
1478d0475637SMatthew Ahrens 			VERIFY0(dmu_read(bpo->bpo_os,
1479d0475637SMatthew Ahrens 			    bpo->bpo_phys->bpo_subobjs,
1480d0475637SMatthew Ahrens 			    i * sizeof (subobj), sizeof (subobj), &subobj, 0));
1481d0475637SMatthew Ahrens 			error = bpobj_open(&subbpo, bpo->bpo_os, subobj);
1482d0475637SMatthew Ahrens 			if (error != 0) {
1483d0475637SMatthew Ahrens 				(void) printf("ERROR %u while trying to open "
1484d0475637SMatthew Ahrens 				    "subobj id %llu\n",
1485d0475637SMatthew Ahrens 				    error, (u_longlong_t)subobj);
1486d0475637SMatthew Ahrens 				continue;
1487d0475637SMatthew Ahrens 			}
1488732885fcSMatthew Ahrens 			dump_full_bpobj(&subbpo, "subobj", indent + 1);
148977061867SMatthew Ahrens 			bpobj_close(&subbpo);
1490d0475637SMatthew Ahrens 		}
149199653d4eSeschrock 	} else {
1492d0475637SMatthew Ahrens 		(void) printf("    %*s: object %llu, %llu blkptrs, %s\n",
1493d0475637SMatthew Ahrens 		    indent * 8, name,
1494d0475637SMatthew Ahrens 		    (u_longlong_t)bpo->bpo_object,
1495d0475637SMatthew Ahrens 		    (u_longlong_t)bpo->bpo_phys->bpo_num_blkptrs,
1496d0475637SMatthew Ahrens 		    bytes);
149799653d4eSeschrock 	}
1498fa9e4066Sahrens 
1499cde58dbcSMatthew Ahrens 	if (dump_opt['d'] < 5)
1500fa9e4066Sahrens 		return;
1501fa9e4066Sahrens 
1502fa9e4066Sahrens 
1503d0475637SMatthew Ahrens 	if (indent == 0) {
1504d0475637SMatthew Ahrens 		(void) bpobj_iterate_nofree(bpo, dump_bpobj_cb, NULL, NULL);
1505d0475637SMatthew Ahrens 		(void) printf("\n");
1506d0475637SMatthew Ahrens 	}
1507cde58dbcSMatthew Ahrens }
150844cd46caSbillm 
1509cde58dbcSMatthew Ahrens static void
1510cde58dbcSMatthew Ahrens dump_deadlist(dsl_deadlist_t *dl)
1511cde58dbcSMatthew Ahrens {
1512cde58dbcSMatthew Ahrens 	dsl_deadlist_entry_t *dle;
1513d0475637SMatthew Ahrens 	uint64_t unused;
1514cde58dbcSMatthew Ahrens 	char bytes[32];
1515cde58dbcSMatthew Ahrens 	char comp[32];
1516cde58dbcSMatthew Ahrens 	char uncomp[32];
1517cde58dbcSMatthew Ahrens 
1518cde58dbcSMatthew Ahrens 	if (dump_opt['d'] < 3)
1519cde58dbcSMatthew Ahrens 		return;
1520cde58dbcSMatthew Ahrens 
152190c76c66SMatthew Ahrens 	if (dl->dl_oldfmt) {
1522732885fcSMatthew Ahrens 		dump_full_bpobj(&dl->dl_bpobj, "old-format deadlist", 0);
152390c76c66SMatthew Ahrens 		return;
152490c76c66SMatthew Ahrens 	}
152590c76c66SMatthew Ahrens 
1526cde58dbcSMatthew Ahrens 	zdb_nicenum(dl->dl_phys->dl_used, bytes);
1527cde58dbcSMatthew Ahrens 	zdb_nicenum(dl->dl_phys->dl_comp, comp);
1528cde58dbcSMatthew Ahrens 	zdb_nicenum(dl->dl_phys->dl_uncomp, uncomp);
1529cde58dbcSMatthew Ahrens 	(void) printf("\n    Deadlist: %s (%s/%s comp)\n",
1530cde58dbcSMatthew Ahrens 	    bytes, comp, uncomp);
1531cde58dbcSMatthew Ahrens 
1532cde58dbcSMatthew Ahrens 	if (dump_opt['d'] < 4)
1533cde58dbcSMatthew Ahrens 		return;
1534cde58dbcSMatthew Ahrens 
1535cde58dbcSMatthew Ahrens 	(void) printf("\n");
1536cde58dbcSMatthew Ahrens 
1537d0475637SMatthew Ahrens 	/* force the tree to be loaded */
1538d0475637SMatthew Ahrens 	dsl_deadlist_space_range(dl, 0, UINT64_MAX, &unused, &unused, &unused);
1539d0475637SMatthew Ahrens 
1540cde58dbcSMatthew Ahrens 	for (dle = avl_first(&dl->dl_tree); dle;
1541cde58dbcSMatthew Ahrens 	    dle = AVL_NEXT(&dl->dl_tree, dle)) {
1542d0475637SMatthew Ahrens 		if (dump_opt['d'] >= 5) {
1543d0475637SMatthew Ahrens 			char buf[128];
1544d0475637SMatthew Ahrens 			(void) snprintf(buf, sizeof (buf), "mintxg %llu -> ",
1545d0475637SMatthew Ahrens 			    (longlong_t)dle->dle_mintxg,
1546d0475637SMatthew Ahrens 			    (longlong_t)dle->dle_bpobj.bpo_object);
1547fa9e4066Sahrens 
1548732885fcSMatthew Ahrens 			dump_full_bpobj(&dle->dle_bpobj, buf, 0);
1549d0475637SMatthew Ahrens 		} else {
1550d0475637SMatthew Ahrens 			(void) printf("mintxg %llu -> obj %llu\n",
1551d0475637SMatthew Ahrens 			    (longlong_t)dle->dle_mintxg,
1552d0475637SMatthew Ahrens 			    (longlong_t)dle->dle_bpobj.bpo_object);
1553d0475637SMatthew Ahrens 
1554d0475637SMatthew Ahrens 		}
1555cde58dbcSMatthew Ahrens 	}
1556fa9e4066Sahrens }
1557fa9e4066Sahrens 
1558e0d35c44Smarks static avl_tree_t idx_tree;
1559e0d35c44Smarks static avl_tree_t domain_tree;
1560e0d35c44Smarks static boolean_t fuid_table_loaded;
15610a586ceaSMark Shellenbaum static boolean_t sa_loaded;
15620a586ceaSMark Shellenbaum sa_attr_type_t *sa_attr_table;
1563e0d35c44Smarks 
1564e0d35c44Smarks static void
1565e0d35c44Smarks fuid_table_destroy()
1566e0d35c44Smarks {
1567e0d35c44Smarks 	if (fuid_table_loaded) {
1568e0d35c44Smarks 		zfs_fuid_table_destroy(&idx_tree, &domain_tree);
1569e0d35c44Smarks 		fuid_table_loaded = B_FALSE;
1570e0d35c44Smarks 	}
1571e0d35c44Smarks }
1572e0d35c44Smarks 
1573e0d35c44Smarks /*
1574e0d35c44Smarks  * print uid or gid information.
1575e0d35c44Smarks  * For normal POSIX id just the id is printed in decimal format.
1576e0d35c44Smarks  * For CIFS files with FUID the fuid is printed in hex followed by
1577d0475637SMatthew Ahrens  * the domain-rid string.
1578e0d35c44Smarks  */
1579e0d35c44Smarks static void
1580e0d35c44Smarks print_idstr(uint64_t id, const char *id_type)
1581e0d35c44Smarks {
1582e0d35c44Smarks 	if (FUID_INDEX(id)) {
1583e0d35c44Smarks 		char *domain;
1584e0d35c44Smarks 
1585e0d35c44Smarks 		domain = zfs_fuid_idx_domain(&idx_tree, FUID_INDEX(id));
1586e0d35c44Smarks 		(void) printf("\t%s     %llx [%s-%d]\n", id_type,
1587e0d35c44Smarks 		    (u_longlong_t)id, domain, (int)FUID_RID(id));
1588e0d35c44Smarks 	} else {
1589e0d35c44Smarks 		(void) printf("\t%s     %llu\n", id_type, (u_longlong_t)id);
1590e0d35c44Smarks 	}
1591e0d35c44Smarks 
1592e0d35c44Smarks }
1593e0d35c44Smarks 
1594e0d35c44Smarks static void
15950a586ceaSMark Shellenbaum dump_uidgid(objset_t *os, uint64_t uid, uint64_t gid)
1596e0d35c44Smarks {
1597e0d35c44Smarks 	uint32_t uid_idx, gid_idx;
1598e0d35c44Smarks 
15990a586ceaSMark Shellenbaum 	uid_idx = FUID_INDEX(uid);
16000a586ceaSMark Shellenbaum 	gid_idx = FUID_INDEX(gid);
1601e0d35c44Smarks 
1602e0d35c44Smarks 	/* Load domain table, if not already loaded */
1603e0d35c44Smarks 	if (!fuid_table_loaded && (uid_idx || gid_idx)) {
1604e0d35c44Smarks 		uint64_t fuid_obj;
1605e0d35c44Smarks 
1606e0d35c44Smarks 		/* first find the fuid object.  It lives in the master node */
1607e0d35c44Smarks 		VERIFY(zap_lookup(os, MASTER_NODE_OBJ, ZFS_FUID_TABLES,
1608e0d35c44Smarks 		    8, 1, &fuid_obj) == 0);
160989459e17SMark Shellenbaum 		zfs_fuid_avl_tree_create(&idx_tree, &domain_tree);
1610e0d35c44Smarks 		(void) zfs_fuid_table_load(os, fuid_obj,
1611e0d35c44Smarks 		    &idx_tree, &domain_tree);
1612e0d35c44Smarks 		fuid_table_loaded = B_TRUE;
1613e0d35c44Smarks 	}
1614e0d35c44Smarks 
16150a586ceaSMark Shellenbaum 	print_idstr(uid, "uid");
16160a586ceaSMark Shellenbaum 	print_idstr(gid, "gid");
1617e0d35c44Smarks }
1618e0d35c44Smarks 
1619fa9e4066Sahrens /*ARGSUSED*/
1620fa9e4066Sahrens static void
1621fa9e4066Sahrens dump_znode(objset_t *os, uint64_t object, void *data, size_t size)
1622fa9e4066Sahrens {
1623fa9e4066Sahrens 	char path[MAXPATHLEN * 2];	/* allow for xattr and failure prefix */
16240a586ceaSMark Shellenbaum 	sa_handle_t *hdl;
16250a586ceaSMark Shellenbaum 	uint64_t xattr, rdev, gen;
16260a586ceaSMark Shellenbaum 	uint64_t uid, gid, mode, fsize, parent, links;
16278f2529deSMark Shellenbaum 	uint64_t pflags;
16280a586ceaSMark Shellenbaum 	uint64_t acctm[2], modtm[2], chgtm[2], crtm[2];
16290a586ceaSMark Shellenbaum 	time_t z_crtime, z_atime, z_mtime, z_ctime;
16308f2529deSMark Shellenbaum 	sa_bulk_attr_t bulk[12];
16310a586ceaSMark Shellenbaum 	int idx = 0;
163255434c77Sek 	int error;
1633fa9e4066Sahrens 
16340a586ceaSMark Shellenbaum 	if (!sa_loaded) {
16350a586ceaSMark Shellenbaum 		uint64_t sa_attrs = 0;
16360a586ceaSMark Shellenbaum 		uint64_t version;
16370a586ceaSMark Shellenbaum 
16380a586ceaSMark Shellenbaum 		VERIFY(zap_lookup(os, MASTER_NODE_OBJ, ZPL_VERSION_STR,
16390a586ceaSMark Shellenbaum 		    8, 1, &version) == 0);
16400a586ceaSMark Shellenbaum 		if (version >= ZPL_VERSION_SA) {
16410a586ceaSMark Shellenbaum 			VERIFY(zap_lookup(os, MASTER_NODE_OBJ, ZFS_SA_ATTRS,
16420a586ceaSMark Shellenbaum 			    8, 1, &sa_attrs) == 0);
16430a586ceaSMark Shellenbaum 		}
16441d8ccc7bSMark Shellenbaum 		if ((error = sa_setup(os, sa_attrs, zfs_attr_table,
16451d8ccc7bSMark Shellenbaum 		    ZPL_END, &sa_attr_table)) != 0) {
16461d8ccc7bSMark Shellenbaum 			(void) printf("sa_setup failed errno %d, can't "
16471d8ccc7bSMark Shellenbaum 			    "display znode contents\n", error);
16481d8ccc7bSMark Shellenbaum 			return;
16491d8ccc7bSMark Shellenbaum 		}
16500a586ceaSMark Shellenbaum 		sa_loaded = B_TRUE;
16510a586ceaSMark Shellenbaum 	}
16520a586ceaSMark Shellenbaum 
16530a586ceaSMark Shellenbaum 	if (sa_handle_get(os, object, NULL, SA_HDL_PRIVATE, &hdl)) {
16540a586ceaSMark Shellenbaum 		(void) printf("Failed to get handle for SA znode\n");
16550a586ceaSMark Shellenbaum 		return;
16560a586ceaSMark Shellenbaum 	}
16570a586ceaSMark Shellenbaum 
16580a586ceaSMark Shellenbaum 	SA_ADD_BULK_ATTR(bulk, idx, sa_attr_table[ZPL_UID], NULL, &uid, 8);
16590a586ceaSMark Shellenbaum 	SA_ADD_BULK_ATTR(bulk, idx, sa_attr_table[ZPL_GID], NULL, &gid, 8);
16600a586ceaSMark Shellenbaum 	SA_ADD_BULK_ATTR(bulk, idx, sa_attr_table[ZPL_LINKS], NULL,
16610a586ceaSMark Shellenbaum 	    &links, 8);
16620a586ceaSMark Shellenbaum 	SA_ADD_BULK_ATTR(bulk, idx, sa_attr_table[ZPL_GEN], NULL, &gen, 8);
16630a586ceaSMark Shellenbaum 	SA_ADD_BULK_ATTR(bulk, idx, sa_attr_table[ZPL_MODE], NULL,
16640a586ceaSMark Shellenbaum 	    &mode, 8);
16650a586ceaSMark Shellenbaum 	SA_ADD_BULK_ATTR(bulk, idx, sa_attr_table[ZPL_PARENT],
16660a586ceaSMark Shellenbaum 	    NULL, &parent, 8);
16670a586ceaSMark Shellenbaum 	SA_ADD_BULK_ATTR(bulk, idx, sa_attr_table[ZPL_SIZE], NULL,
16680a586ceaSMark Shellenbaum 	    &fsize, 8);
16690a586ceaSMark Shellenbaum 	SA_ADD_BULK_ATTR(bulk, idx, sa_attr_table[ZPL_ATIME], NULL,
16700a586ceaSMark Shellenbaum 	    acctm, 16);
16710a586ceaSMark Shellenbaum 	SA_ADD_BULK_ATTR(bulk, idx, sa_attr_table[ZPL_MTIME], NULL,
16720a586ceaSMark Shellenbaum 	    modtm, 16);
16730a586ceaSMark Shellenbaum 	SA_ADD_BULK_ATTR(bulk, idx, sa_attr_table[ZPL_CRTIME], NULL,
16740a586ceaSMark Shellenbaum 	    crtm, 16);
16750a586ceaSMark Shellenbaum 	SA_ADD_BULK_ATTR(bulk, idx, sa_attr_table[ZPL_CTIME], NULL,
16760a586ceaSMark Shellenbaum 	    chgtm, 16);
16778f2529deSMark Shellenbaum 	SA_ADD_BULK_ATTR(bulk, idx, sa_attr_table[ZPL_FLAGS], NULL,
16788f2529deSMark Shellenbaum 	    &pflags, 8);
16790a586ceaSMark Shellenbaum 
16800a586ceaSMark Shellenbaum 	if (sa_bulk_lookup(hdl, bulk, idx)) {
16810a586ceaSMark Shellenbaum 		(void) sa_handle_destroy(hdl);
16820a586ceaSMark Shellenbaum 		return;
16830a586ceaSMark Shellenbaum 	}
1684fa9e4066Sahrens 
168555434c77Sek 	error = zfs_obj_to_path(os, object, path, sizeof (path));
168655434c77Sek 	if (error != 0) {
168755434c77Sek 		(void) snprintf(path, sizeof (path), "\?\?\?<object#%llu>",
168855434c77Sek 		    (u_longlong_t)object);
168955434c77Sek 	}
1690fa9e4066Sahrens 	if (dump_opt['d'] < 3) {
169155434c77Sek 		(void) printf("\t%s\n", path);
16920a586ceaSMark Shellenbaum 		(void) sa_handle_destroy(hdl);
1693fa9e4066Sahrens 		return;
1694fa9e4066Sahrens 	}
1695fa9e4066Sahrens 
16960a586ceaSMark Shellenbaum 	z_crtime = (time_t)crtm[0];
16970a586ceaSMark Shellenbaum 	z_atime = (time_t)acctm[0];
16980a586ceaSMark Shellenbaum 	z_mtime = (time_t)modtm[0];
16990a586ceaSMark Shellenbaum 	z_ctime = (time_t)chgtm[0];
1700fa9e4066Sahrens 
170155434c77Sek 	(void) printf("\tpath	%s\n", path);
17020a586ceaSMark Shellenbaum 	dump_uidgid(os, uid, gid);
1703fa9e4066Sahrens 	(void) printf("\tatime	%s", ctime(&z_atime));
1704fa9e4066Sahrens 	(void) printf("\tmtime	%s", ctime(&z_mtime));
1705fa9e4066Sahrens 	(void) printf("\tctime	%s", ctime(&z_ctime));
1706fa9e4066Sahrens 	(void) printf("\tcrtime	%s", ctime(&z_crtime));
17070a586ceaSMark Shellenbaum 	(void) printf("\tgen	%llu\n", (u_longlong_t)gen);
17080a586ceaSMark Shellenbaum 	(void) printf("\tmode	%llo\n", (u_longlong_t)mode);
17090a586ceaSMark Shellenbaum 	(void) printf("\tsize	%llu\n", (u_longlong_t)fsize);
17100a586ceaSMark Shellenbaum 	(void) printf("\tparent	%llu\n", (u_longlong_t)parent);
17110a586ceaSMark Shellenbaum 	(void) printf("\tlinks	%llu\n", (u_longlong_t)links);
17128f2529deSMark Shellenbaum 	(void) printf("\tpflags	%llx\n", (u_longlong_t)pflags);
17130a586ceaSMark Shellenbaum 	if (sa_lookup(hdl, sa_attr_table[ZPL_XATTR], &xattr,
17140a586ceaSMark Shellenbaum 	    sizeof (uint64_t)) == 0)
17150a586ceaSMark Shellenbaum 		(void) printf("\txattr	%llu\n", (u_longlong_t)xattr);
17160a586ceaSMark Shellenbaum 	if (sa_lookup(hdl, sa_attr_table[ZPL_RDEV], &rdev,
17170a586ceaSMark Shellenbaum 	    sizeof (uint64_t)) == 0)
17180a586ceaSMark Shellenbaum 		(void) printf("\trdev	0x%016llx\n", (u_longlong_t)rdev);
17190a586ceaSMark Shellenbaum 	sa_handle_destroy(hdl);
1720fa9e4066Sahrens }
1721fa9e4066Sahrens 
1722fa9e4066Sahrens /*ARGSUSED*/
1723fa9e4066Sahrens static void
1724fa9e4066Sahrens dump_acl(objset_t *os, uint64_t object, void *data, size_t size)
1725fa9e4066Sahrens {
1726fa9e4066Sahrens }
1727fa9e4066Sahrens 
1728fa9e4066Sahrens /*ARGSUSED*/
1729fa9e4066Sahrens static void
1730fa9e4066Sahrens dump_dmu_objset(objset_t *os, uint64_t object, void *data, size_t size)
1731fa9e4066Sahrens {
1732fa9e4066Sahrens }
1733fa9e4066Sahrens 
17346de8f417SVictor Latushkin static object_viewer_t *object_viewer[DMU_OT_NUMTYPES + 1] = {
1735fa9e4066Sahrens 	dump_none,		/* unallocated			*/
1736fa9e4066Sahrens 	dump_zap,		/* object directory		*/
1737fa9e4066Sahrens 	dump_uint64,		/* object array			*/
1738fa9e4066Sahrens 	dump_none,		/* packed nvlist		*/
1739fa9e4066Sahrens 	dump_packed_nvlist,	/* packed nvlist size		*/
1740732885fcSMatthew Ahrens 	dump_none,		/* bpobj			*/
1741732885fcSMatthew Ahrens 	dump_bpobj,		/* bpobj header			*/
1742fa9e4066Sahrens 	dump_none,		/* SPA space map header		*/
1743fa9e4066Sahrens 	dump_none,		/* SPA space map		*/
1744fa9e4066Sahrens 	dump_none,		/* ZIL intent log		*/
1745fa9e4066Sahrens 	dump_dnode,		/* DMU dnode			*/
1746fa9e4066Sahrens 	dump_dmu_objset,	/* DMU objset			*/
1747ea8dc4b6Seschrock 	dump_dsl_dir,		/* DSL directory		*/
1748fa9e4066Sahrens 	dump_zap,		/* DSL directory child map	*/
1749fa9e4066Sahrens 	dump_zap,		/* DSL dataset snap map		*/
1750fa9e4066Sahrens 	dump_zap,		/* DSL props			*/
1751fa9e4066Sahrens 	dump_dsl_dataset,	/* DSL dataset			*/
1752fa9e4066Sahrens 	dump_znode,		/* ZFS znode			*/
1753da6c28aaSamw 	dump_acl,		/* ZFS V0 ACL			*/
1754fa9e4066Sahrens 	dump_uint8,		/* ZFS plain file		*/
1755e7437265Sahrens 	dump_zpldir,		/* ZFS directory		*/
1756fa9e4066Sahrens 	dump_zap,		/* ZFS master node		*/
1757fa9e4066Sahrens 	dump_zap,		/* ZFS delete queue		*/
1758fa9e4066Sahrens 	dump_uint8,		/* zvol object			*/
1759fa9e4066Sahrens 	dump_zap,		/* zvol prop			*/
1760fa9e4066Sahrens 	dump_uint8,		/* other uint8[]		*/
1761fa9e4066Sahrens 	dump_uint64,		/* other uint64[]		*/
1762fa9e4066Sahrens 	dump_zap,		/* other ZAP			*/
1763ea8dc4b6Seschrock 	dump_zap,		/* persistent error log		*/
176406eeb2adSek 	dump_uint8,		/* SPA history			*/
17654445fffbSMatthew Ahrens 	dump_history_offsets,	/* SPA history offsets		*/
1766b1b8ab34Slling 	dump_zap,		/* Pool properties		*/
1767ecd6cf80Smarks 	dump_zap,		/* DSL permissions		*/
1768da6c28aaSamw 	dump_acl,		/* ZFS ACL			*/
1769da6c28aaSamw 	dump_uint8,		/* ZFS SYSACL			*/
1770da6c28aaSamw 	dump_none,		/* FUID nvlist			*/
1771da6c28aaSamw 	dump_packed_nvlist,	/* FUID nvlist size		*/
1772088f3894Sahrens 	dump_zap,		/* DSL dataset next clones	*/
1773088f3894Sahrens 	dump_zap,		/* DSL scrub queue		*/
177414843421SMatthew Ahrens 	dump_zap,		/* ZFS user/group used		*/
177514843421SMatthew Ahrens 	dump_zap,		/* ZFS user/group quota		*/
1776842727c2SChris Kirby 	dump_zap,		/* snapshot refcount tags	*/
1777486ae710SMatthew Ahrens 	dump_ddt_zap,		/* DDT ZAP object		*/
1778b24ab676SJeff Bonwick 	dump_zap,		/* DDT statistics		*/
17790a586ceaSMark Shellenbaum 	dump_znode,		/* SA object			*/
17800a586ceaSMark Shellenbaum 	dump_zap,		/* SA Master Node		*/
17810a586ceaSMark Shellenbaum 	dump_sa_attrs,		/* SA attribute registration	*/
17820a586ceaSMark Shellenbaum 	dump_sa_layouts,	/* SA attribute layouts		*/
17833f9d6ad7SLin Ling 	dump_zap,		/* DSL scrub translations	*/
17843f9d6ad7SLin Ling 	dump_none,		/* fake dedup BP		*/
1785cde58dbcSMatthew Ahrens 	dump_zap,		/* deadlist			*/
1786cde58dbcSMatthew Ahrens 	dump_none,		/* deadlist hdr			*/
1787cde58dbcSMatthew Ahrens 	dump_zap,		/* dsl clones			*/
1788732885fcSMatthew Ahrens 	dump_bpobj_subobjs,	/* bpobj subobjs		*/
17890a586ceaSMark Shellenbaum 	dump_unknown,		/* Unknown type, must be last	*/
1790fa9e4066Sahrens };
1791fa9e4066Sahrens 
1792fa9e4066Sahrens static void
1793fa9e4066Sahrens dump_object(objset_t *os, uint64_t object, int verbosity, int *print_header)
1794fa9e4066Sahrens {
1795fa9e4066Sahrens 	dmu_buf_t *db = NULL;
1796fa9e4066Sahrens 	dmu_object_info_t doi;
1797fa9e4066Sahrens 	dnode_t *dn;
1798fa9e4066Sahrens 	void *bonus = NULL;
1799fa9e4066Sahrens 	size_t bsize = 0;
18003f9d6ad7SLin Ling 	char iblk[32], dblk[32], lsize[32], asize[32], fill[32];
18013f9d6ad7SLin Ling 	char bonus_size[32];
1802fa9e4066Sahrens 	char aux[50];
1803fa9e4066Sahrens 	int error;
1804fa9e4066Sahrens 
1805fa9e4066Sahrens 	if (*print_header) {
1806b24ab676SJeff Bonwick 		(void) printf("\n%10s  %3s  %5s  %5s  %5s  %5s  %6s  %s\n",
1807b24ab676SJeff Bonwick 		    "Object", "lvl", "iblk", "dblk", "dsize", "lsize",
1808b24ab676SJeff Bonwick 		    "%full", "type");
1809fa9e4066Sahrens 		*print_header = 0;
1810fa9e4066Sahrens 	}
1811fa9e4066Sahrens 
1812fa9e4066Sahrens 	if (object == 0) {
1813744947dcSTom Erickson 		dn = DMU_META_DNODE(os);
1814fa9e4066Sahrens 	} else {
1815ea8dc4b6Seschrock 		error = dmu_bonus_hold(os, object, FTAG, &db);
1816ea8dc4b6Seschrock 		if (error)
1817ea8dc4b6Seschrock 			fatal("dmu_bonus_hold(%llu) failed, errno %u",
1818ea8dc4b6Seschrock 			    object, error);
1819fa9e4066Sahrens 		bonus = db->db_data;
1820fa9e4066Sahrens 		bsize = db->db_size;
1821744947dcSTom Erickson 		dn = DB_DNODE((dmu_buf_impl_t *)db);
1822fa9e4066Sahrens 	}
1823fa9e4066Sahrens 	dmu_object_info_from_dnode(dn, &doi);
1824fa9e4066Sahrens 
18253f9d6ad7SLin Ling 	zdb_nicenum(doi.doi_metadata_block_size, iblk);
18263f9d6ad7SLin Ling 	zdb_nicenum(doi.doi_data_block_size, dblk);
18273f9d6ad7SLin Ling 	zdb_nicenum(doi.doi_max_offset, lsize);
18283f9d6ad7SLin Ling 	zdb_nicenum(doi.doi_physical_blocks_512 << 9, asize);
18293f9d6ad7SLin Ling 	zdb_nicenum(doi.doi_bonus_size, bonus_size);
1830b24ab676SJeff Bonwick 	(void) sprintf(fill, "%6.2f", 100.0 * doi.doi_fill_count *
1831bbfd46c4SJeff Bonwick 	    doi.doi_data_block_size / (object == 0 ? DNODES_PER_BLOCK : 1) /
1832bbfd46c4SJeff Bonwick 	    doi.doi_max_offset);
1833fa9e4066Sahrens 
1834fa9e4066Sahrens 	aux[0] = '\0';
1835fa9e4066Sahrens 
1836e7437265Sahrens 	if (doi.doi_checksum != ZIO_CHECKSUM_INHERIT || verbosity >= 6) {
1837fa9e4066Sahrens 		(void) snprintf(aux + strlen(aux), sizeof (aux), " (K=%s)",
18386de8f417SVictor Latushkin 		    ZDB_CHECKSUM_NAME(doi.doi_checksum));
1839e7437265Sahrens 	}
1840fa9e4066Sahrens 
1841e7437265Sahrens 	if (doi.doi_compress != ZIO_COMPRESS_INHERIT || verbosity >= 6) {
1842fa9e4066Sahrens 		(void) snprintf(aux + strlen(aux), sizeof (aux), " (Z=%s)",
18436de8f417SVictor Latushkin 		    ZDB_COMPRESS_NAME(doi.doi_compress));
1844e7437265Sahrens 	}
1845fa9e4066Sahrens 
1846b24ab676SJeff Bonwick 	(void) printf("%10lld  %3u  %5s  %5s  %5s  %5s  %6s  %s%s\n",
1847b24ab676SJeff Bonwick 	    (u_longlong_t)object, doi.doi_indirection, iblk, dblk,
1848b24ab676SJeff Bonwick 	    asize, lsize, fill, ZDB_OT_NAME(doi.doi_type), aux);
1849fa9e4066Sahrens 
1850fa9e4066Sahrens 	if (doi.doi_bonus_type != DMU_OT_NONE && verbosity > 3) {
1851b24ab676SJeff Bonwick 		(void) printf("%10s  %3s  %5s  %5s  %5s  %5s  %6s  %s\n",
1852b24ab676SJeff Bonwick 		    "", "", "", "", "", bonus_size, "bonus",
18536de8f417SVictor Latushkin 		    ZDB_OT_NAME(doi.doi_bonus_type));
1854fa9e4066Sahrens 	}
1855fa9e4066Sahrens 
1856fa9e4066Sahrens 	if (verbosity >= 4) {
18570a586ceaSMark Shellenbaum 		(void) printf("\tdnode flags: %s%s%s\n",
185814843421SMatthew Ahrens 		    (dn->dn_phys->dn_flags & DNODE_FLAG_USED_BYTES) ?
185914843421SMatthew Ahrens 		    "USED_BYTES " : "",
186014843421SMatthew Ahrens 		    (dn->dn_phys->dn_flags & DNODE_FLAG_USERUSED_ACCOUNTED) ?
18610a586ceaSMark Shellenbaum 		    "USERUSED_ACCOUNTED " : "",
18620a586ceaSMark Shellenbaum 		    (dn->dn_phys->dn_flags & DNODE_FLAG_SPILL_BLKPTR) ?
18630a586ceaSMark Shellenbaum 		    "SPILL_BLKPTR" : "");
186414843421SMatthew Ahrens 		(void) printf("\tdnode maxblkid: %llu\n",
186514843421SMatthew Ahrens 		    (longlong_t)dn->dn_phys->dn_maxblkid);
186614843421SMatthew Ahrens 
18676de8f417SVictor Latushkin 		object_viewer[ZDB_OT_TYPE(doi.doi_bonus_type)](os, object,
18686de8f417SVictor Latushkin 		    bonus, bsize);
18696de8f417SVictor Latushkin 		object_viewer[ZDB_OT_TYPE(doi.doi_type)](os, object, NULL, 0);
1870fa9e4066Sahrens 		*print_header = 1;
1871fa9e4066Sahrens 	}
1872fa9e4066Sahrens 
1873fa9e4066Sahrens 	if (verbosity >= 5)
187488b7b0f2SMatthew Ahrens 		dump_indirect(dn);
1875fa9e4066Sahrens 
1876fa9e4066Sahrens 	if (verbosity >= 5) {
1877fa9e4066Sahrens 		/*
1878fa9e4066Sahrens 		 * Report the list of segments that comprise the object.
1879fa9e4066Sahrens 		 */
1880fa9e4066Sahrens 		uint64_t start = 0;
1881fa9e4066Sahrens 		uint64_t end;
1882fa9e4066Sahrens 		uint64_t blkfill = 1;
1883fa9e4066Sahrens 		int minlvl = 1;
1884fa9e4066Sahrens 
1885fa9e4066Sahrens 		if (dn->dn_type == DMU_OT_DNODE) {
1886fa9e4066Sahrens 			minlvl = 0;
1887fa9e4066Sahrens 			blkfill = DNODES_PER_BLOCK;
1888fa9e4066Sahrens 		}
1889fa9e4066Sahrens 
1890fa9e4066Sahrens 		for (;;) {
18913f9d6ad7SLin Ling 			char segsize[32];
1892cdb0ab79Smaybee 			error = dnode_next_offset(dn,
1893cdb0ab79Smaybee 			    0, &start, minlvl, blkfill, 0);
1894fa9e4066Sahrens 			if (error)
1895fa9e4066Sahrens 				break;
1896fa9e4066Sahrens 			end = start;
1897cdb0ab79Smaybee 			error = dnode_next_offset(dn,
1898cdb0ab79Smaybee 			    DNODE_FIND_HOLE, &end, minlvl, blkfill, 0);
18993f9d6ad7SLin Ling 			zdb_nicenum(end - start, segsize);
1900fa9e4066Sahrens 			(void) printf("\t\tsegment [%016llx, %016llx)"
1901fa9e4066Sahrens 			    " size %5s\n", (u_longlong_t)start,
1902fa9e4066Sahrens 			    (u_longlong_t)end, segsize);
1903fa9e4066Sahrens 			if (error)
1904fa9e4066Sahrens 				break;
1905fa9e4066Sahrens 			start = end;
1906fa9e4066Sahrens 		}
1907fa9e4066Sahrens 	}
1908fa9e4066Sahrens 
1909fa9e4066Sahrens 	if (db != NULL)
1910ea8dc4b6Seschrock 		dmu_buf_rele(db, FTAG);
1911fa9e4066Sahrens }
1912fa9e4066Sahrens 
1913fa9e4066Sahrens static char *objset_types[DMU_OST_NUMTYPES] = {
1914fa9e4066Sahrens 	"NONE", "META", "ZPL", "ZVOL", "OTHER", "ANY" };
1915fa9e4066Sahrens 
1916fa9e4066Sahrens static void
1917fa9e4066Sahrens dump_dir(objset_t *os)
1918fa9e4066Sahrens {
1919fa9e4066Sahrens 	dmu_objset_stats_t dds;
1920fa9e4066Sahrens 	uint64_t object, object_count;
1921a2eea2e1Sahrens 	uint64_t refdbytes, usedobjs, scratch;
19223f9d6ad7SLin Ling 	char numbuf[32];
192314843421SMatthew Ahrens 	char blkbuf[BP_SPRINTF_LEN + 20];
1924fa9e4066Sahrens 	char osname[MAXNAMELEN];
1925fa9e4066Sahrens 	char *type = "UNKNOWN";
1926fa9e4066Sahrens 	int verbosity = dump_opt['d'];
1927fa9e4066Sahrens 	int print_header = 1;
1928fa9e4066Sahrens 	int i, error;
1929fa9e4066Sahrens 
19303b2aab18SMatthew Ahrens 	dsl_pool_config_enter(dmu_objset_pool(os), FTAG);
1931a2eea2e1Sahrens 	dmu_objset_fast_stat(os, &dds);
19323b2aab18SMatthew Ahrens 	dsl_pool_config_exit(dmu_objset_pool(os), FTAG);
1933fa9e4066Sahrens 
1934fa9e4066Sahrens 	if (dds.dds_type < DMU_OST_NUMTYPES)
1935fa9e4066Sahrens 		type = objset_types[dds.dds_type];
1936fa9e4066Sahrens 
1937fa9e4066Sahrens 	if (dds.dds_type == DMU_OST_META) {
1938fa9e4066Sahrens 		dds.dds_creation_txg = TXG_INITIAL;
19395d7b4d43SMatthew Ahrens 		usedobjs = BP_GET_FILL(os->os_rootbp);
1940c1379625SJustin T. Gibbs 		refdbytes = dsl_dir_phys(os->os_spa->spa_dsl_pool->dp_mos_dir)->
1941c1379625SJustin T. Gibbs 		    dd_used_bytes;
1942a2eea2e1Sahrens 	} else {
1943a2eea2e1Sahrens 		dmu_objset_space(os, &refdbytes, &scratch, &usedobjs, &scratch);
1944fa9e4066Sahrens 	}
1945fa9e4066Sahrens 
19465d7b4d43SMatthew Ahrens 	ASSERT3U(usedobjs, ==, BP_GET_FILL(os->os_rootbp));
1947fa9e4066Sahrens 
19483f9d6ad7SLin Ling 	zdb_nicenum(refdbytes, numbuf);
1949fa9e4066Sahrens 
1950fa9e4066Sahrens 	if (verbosity >= 4) {
195143466aaeSMax Grossman 		(void) snprintf(blkbuf, sizeof (blkbuf), ", rootbp ");
195243466aaeSMax Grossman 		(void) snprintf_blkptr(blkbuf + strlen(blkbuf),
195343466aaeSMax Grossman 		    sizeof (blkbuf) - strlen(blkbuf), os->os_rootbp);
1954fa9e4066Sahrens 	} else {
1955fa9e4066Sahrens 		blkbuf[0] = '\0';
1956fa9e4066Sahrens 	}
1957fa9e4066Sahrens 
1958fa9e4066Sahrens 	dmu_objset_name(os, osname);
1959fa9e4066Sahrens 
1960a2eea2e1Sahrens 	(void) printf("Dataset %s [%s], ID %llu, cr_txg %llu, "
1961fa9e4066Sahrens 	    "%s, %llu objects%s\n",
1962fa9e4066Sahrens 	    osname, type, (u_longlong_t)dmu_objset_id(os),
1963fa9e4066Sahrens 	    (u_longlong_t)dds.dds_creation_txg,
1964a2eea2e1Sahrens 	    numbuf, (u_longlong_t)usedobjs, blkbuf);
1965fa9e4066Sahrens 
1966b24ab676SJeff Bonwick 	if (zopt_objects != 0) {
1967b24ab676SJeff Bonwick 		for (i = 0; i < zopt_objects; i++)
1968b24ab676SJeff Bonwick 			dump_object(os, zopt_object[i], verbosity,
1969b24ab676SJeff Bonwick 			    &print_header);
1970b24ab676SJeff Bonwick 		(void) printf("\n");
1971b24ab676SJeff Bonwick 		return;
1972b24ab676SJeff Bonwick 	}
1973b24ab676SJeff Bonwick 
1974b24ab676SJeff Bonwick 	if (dump_opt['i'] != 0 || verbosity >= 2)
1975b24ab676SJeff Bonwick 		dump_intent_log(dmu_objset_zil(os));
1976fa9e4066Sahrens 
1977fa9e4066Sahrens 	if (dmu_objset_ds(os) != NULL)
1978cde58dbcSMatthew Ahrens 		dump_deadlist(&dmu_objset_ds(os)->ds_deadlist);
1979fa9e4066Sahrens 
1980fa9e4066Sahrens 	if (verbosity < 2)
1981fa9e4066Sahrens 		return;
1982fa9e4066Sahrens 
198343466aaeSMax Grossman 	if (BP_IS_HOLE(os->os_rootbp))
1984088f3894Sahrens 		return;
1985088f3894Sahrens 
1986fa9e4066Sahrens 	dump_object(os, 0, verbosity, &print_header);
198714843421SMatthew Ahrens 	object_count = 0;
1988744947dcSTom Erickson 	if (DMU_USERUSED_DNODE(os) != NULL &&
1989744947dcSTom Erickson 	    DMU_USERUSED_DNODE(os)->dn_type != 0) {
199014843421SMatthew Ahrens 		dump_object(os, DMU_USERUSED_OBJECT, verbosity, &print_header);
199114843421SMatthew Ahrens 		dump_object(os, DMU_GROUPUSED_OBJECT, verbosity, &print_header);
199214843421SMatthew Ahrens 	}
1993fa9e4066Sahrens 
1994fa9e4066Sahrens 	object = 0;
19956754306eSahrens 	while ((error = dmu_object_next(os, &object, B_FALSE, 0)) == 0) {
1996fa9e4066Sahrens 		dump_object(os, object, verbosity, &print_header);
1997fa9e4066Sahrens 		object_count++;
1998fa9e4066Sahrens 	}
1999fa9e4066Sahrens 
2000a2eea2e1Sahrens 	ASSERT3U(object_count, ==, usedobjs);
2001fa9e4066Sahrens 
2002fa9e4066Sahrens 	(void) printf("\n");
2003fa9e4066Sahrens 
2004ccba0801SRich Morris 	if (error != ESRCH) {
2005ccba0801SRich Morris 		(void) fprintf(stderr, "dmu_object_next() = %d\n", error);
2006ccba0801SRich Morris 		abort();
2007ccba0801SRich Morris 	}
2008fa9e4066Sahrens }
2009fa9e4066Sahrens 
2010fa9e4066Sahrens static void
201153b9a4a9SVictor Latushkin dump_uberblock(uberblock_t *ub, const char *header, const char *footer)
2012fa9e4066Sahrens {
2013fa9e4066Sahrens 	time_t timestamp = ub->ub_timestamp;
2014fa9e4066Sahrens 
201553b9a4a9SVictor Latushkin 	(void) printf(header ? header : "");
2016fa9e4066Sahrens 	(void) printf("\tmagic = %016llx\n", (u_longlong_t)ub->ub_magic);
2017fa9e4066Sahrens 	(void) printf("\tversion = %llu\n", (u_longlong_t)ub->ub_version);
2018fa9e4066Sahrens 	(void) printf("\ttxg = %llu\n", (u_longlong_t)ub->ub_txg);
2019fa9e4066Sahrens 	(void) printf("\tguid_sum = %llu\n", (u_longlong_t)ub->ub_guid_sum);
2020fa9e4066Sahrens 	(void) printf("\ttimestamp = %llu UTC = %s",
2021fa9e4066Sahrens 	    (u_longlong_t)ub->ub_timestamp, asctime(localtime(&timestamp)));
2022fa9e4066Sahrens 	if (dump_opt['u'] >= 3) {
2023fbabab8fSmaybee 		char blkbuf[BP_SPRINTF_LEN];
202443466aaeSMax Grossman 		snprintf_blkptr(blkbuf, sizeof (blkbuf), &ub->ub_rootbp);
2025fa9e4066Sahrens 		(void) printf("\trootbp = %s\n", blkbuf);
2026fa9e4066Sahrens 	}
202753b9a4a9SVictor Latushkin 	(void) printf(footer ? footer : "");
2028fa9e4066Sahrens }
2029fa9e4066Sahrens 
2030fa9e4066Sahrens static void
203107428bdfSVictor Latushkin dump_config(spa_t *spa)
2032fa9e4066Sahrens {
203307428bdfSVictor Latushkin 	dmu_buf_t *db;
203407428bdfSVictor Latushkin 	size_t nvsize = 0;
203507428bdfSVictor Latushkin 	int error = 0;
203607428bdfSVictor Latushkin 
2037fa9e4066Sahrens 
203807428bdfSVictor Latushkin 	error = dmu_bonus_hold(spa->spa_meta_objset,
203907428bdfSVictor Latushkin 	    spa->spa_config_object, FTAG, &db);
204007428bdfSVictor Latushkin 
204107428bdfSVictor Latushkin 	if (error == 0) {
204207428bdfSVictor Latushkin 		nvsize = *(uint64_t *)db->db_data;
204307428bdfSVictor Latushkin 		dmu_buf_rele(db, FTAG);
204407428bdfSVictor Latushkin 
204507428bdfSVictor Latushkin 		(void) printf("\nMOS Configuration:\n");
204607428bdfSVictor Latushkin 		dump_packed_nvlist(spa->spa_meta_objset,
204707428bdfSVictor Latushkin 		    spa->spa_config_object, (void *)&nvsize, 1);
204807428bdfSVictor Latushkin 	} else {
204907428bdfSVictor Latushkin 		(void) fprintf(stderr, "dmu_bonus_hold(%llu) failed, errno %d",
205007428bdfSVictor Latushkin 		    (u_longlong_t)spa->spa_config_object, error);
2051fa9e4066Sahrens 	}
2052fa9e4066Sahrens }
2053fa9e4066Sahrens 
2054c5904d13Seschrock static void
2055c5904d13Seschrock dump_cachefile(const char *cachefile)
2056c5904d13Seschrock {
2057c5904d13Seschrock 	int fd;
2058c5904d13Seschrock 	struct stat64 statbuf;
2059c5904d13Seschrock 	char *buf;
2060c5904d13Seschrock 	nvlist_t *config;
2061c5904d13Seschrock 
2062c5904d13Seschrock 	if ((fd = open64(cachefile, O_RDONLY)) < 0) {
2063c5904d13Seschrock 		(void) printf("cannot open '%s': %s\n", cachefile,
2064c5904d13Seschrock 		    strerror(errno));
2065c5904d13Seschrock 		exit(1);
2066c5904d13Seschrock 	}
2067c5904d13Seschrock 
2068c5904d13Seschrock 	if (fstat64(fd, &statbuf) != 0) {
2069c5904d13Seschrock 		(void) printf("failed to stat '%s': %s\n", cachefile,
2070c5904d13Seschrock 		    strerror(errno));
2071c5904d13Seschrock 		exit(1);
2072c5904d13Seschrock 	}
2073c5904d13Seschrock 
2074c5904d13Seschrock 	if ((buf = malloc(statbuf.st_size)) == NULL) {
2075c5904d13Seschrock 		(void) fprintf(stderr, "failed to allocate %llu bytes\n",
2076c5904d13Seschrock 		    (u_longlong_t)statbuf.st_size);
2077c5904d13Seschrock 		exit(1);
2078c5904d13Seschrock 	}
2079c5904d13Seschrock 
2080c5904d13Seschrock 	if (read(fd, buf, statbuf.st_size) != statbuf.st_size) {
2081c5904d13Seschrock 		(void) fprintf(stderr, "failed to read %llu bytes\n",
2082c5904d13Seschrock 		    (u_longlong_t)statbuf.st_size);
2083c5904d13Seschrock 		exit(1);
2084c5904d13Seschrock 	}
2085c5904d13Seschrock 
2086c5904d13Seschrock 	(void) close(fd);
2087c5904d13Seschrock 
2088c5904d13Seschrock 	if (nvlist_unpack(buf, statbuf.st_size, &config, 0) != 0) {
2089c5904d13Seschrock 		(void) fprintf(stderr, "failed to unpack nvlist\n");
2090c5904d13Seschrock 		exit(1);
2091c5904d13Seschrock 	}
2092c5904d13Seschrock 
2093c5904d13Seschrock 	free(buf);
2094c5904d13Seschrock 
2095c5904d13Seschrock 	dump_nvlist(config, 0);
2096c5904d13Seschrock 
2097c5904d13Seschrock 	nvlist_free(config);
2098c5904d13Seschrock }
2099c5904d13Seschrock 
210053b9a4a9SVictor Latushkin #define	ZDB_MAX_UB_HEADER_SIZE 32
210153b9a4a9SVictor Latushkin 
210253b9a4a9SVictor Latushkin static void
210353b9a4a9SVictor Latushkin dump_label_uberblocks(vdev_label_t *lbl, uint64_t ashift)
210453b9a4a9SVictor Latushkin {
210553b9a4a9SVictor Latushkin 	vdev_t vd;
210653b9a4a9SVictor Latushkin 	vdev_t *vdp = &vd;
210753b9a4a9SVictor Latushkin 	char header[ZDB_MAX_UB_HEADER_SIZE];
210853b9a4a9SVictor Latushkin 
210953b9a4a9SVictor Latushkin 	vd.vdev_ashift = ashift;
211053b9a4a9SVictor Latushkin 	vdp->vdev_top = vdp;
211153b9a4a9SVictor Latushkin 
211253b9a4a9SVictor Latushkin 	for (int i = 0; i < VDEV_UBERBLOCK_COUNT(vdp); i++) {
211353b9a4a9SVictor Latushkin 		uint64_t uoff = VDEV_UBERBLOCK_OFFSET(vdp, i);
211453b9a4a9SVictor Latushkin 		uberblock_t *ub = (void *)((char *)lbl + uoff);
211553b9a4a9SVictor Latushkin 
211653b9a4a9SVictor Latushkin 		if (uberblock_verify(ub))
211753b9a4a9SVictor Latushkin 			continue;
211853b9a4a9SVictor Latushkin 		(void) snprintf(header, ZDB_MAX_UB_HEADER_SIZE,
211953b9a4a9SVictor Latushkin 		    "Uberblock[%d]\n", i);
212053b9a4a9SVictor Latushkin 		dump_uberblock(ub, header, "");
212153b9a4a9SVictor Latushkin 	}
212253b9a4a9SVictor Latushkin }
212353b9a4a9SVictor Latushkin 
2124fa9e4066Sahrens static void
2125fa9e4066Sahrens dump_label(const char *dev)
2126fa9e4066Sahrens {
2127fa9e4066Sahrens 	int fd;
2128fa9e4066Sahrens 	vdev_label_t label;
2129c6065d0fSGeorge Wilson 	char *path, *buf = label.vl_vdev_phys.vp_nvlist;
2130fa9e4066Sahrens 	size_t buflen = sizeof (label.vl_vdev_phys.vp_nvlist);
2131fa9e4066Sahrens 	struct stat64 statbuf;
213253b9a4a9SVictor Latushkin 	uint64_t psize, ashift;
2133c6065d0fSGeorge Wilson 	int len = strlen(dev) + 1;
2134fa9e4066Sahrens 
2135c6065d0fSGeorge Wilson 	if (strncmp(dev, "/dev/dsk/", 9) == 0) {
2136c6065d0fSGeorge Wilson 		len++;
2137c6065d0fSGeorge Wilson 		path = malloc(len);
2138c6065d0fSGeorge Wilson 		(void) snprintf(path, len, "%s%s", "/dev/rdsk/", dev + 9);
2139c6065d0fSGeorge Wilson 	} else {
2140c6065d0fSGeorge Wilson 		path = strdup(dev);
2141c6065d0fSGeorge Wilson 	}
2142c6065d0fSGeorge Wilson 
2143c6065d0fSGeorge Wilson 	if ((fd = open64(path, O_RDONLY)) < 0) {
2144c6065d0fSGeorge Wilson 		(void) printf("cannot open '%s': %s\n", path, strerror(errno));
2145c6065d0fSGeorge Wilson 		free(path);
2146fa9e4066Sahrens 		exit(1);
2147fa9e4066Sahrens 	}
2148fa9e4066Sahrens 
2149fa9e4066Sahrens 	if (fstat64(fd, &statbuf) != 0) {
2150c6065d0fSGeorge Wilson 		(void) printf("failed to stat '%s': %s\n", path,
2151fa9e4066Sahrens 		    strerror(errno));
2152c6065d0fSGeorge Wilson 		free(path);
2153c6065d0fSGeorge Wilson 		(void) close(fd);
2154c6065d0fSGeorge Wilson 		exit(1);
2155c6065d0fSGeorge Wilson 	}
2156c6065d0fSGeorge Wilson 
2157c6065d0fSGeorge Wilson 	if (S_ISBLK(statbuf.st_mode)) {
2158c6065d0fSGeorge Wilson 		(void) printf("cannot use '%s': character device required\n",
2159c6065d0fSGeorge Wilson 		    path);
2160c6065d0fSGeorge Wilson 		free(path);
2161c6065d0fSGeorge Wilson 		(void) close(fd);
2162c6065d0fSGeorge Wilson 		exit(1);
2163fa9e4066Sahrens 	}
2164fa9e4066Sahrens 
2165fa9e4066Sahrens 	psize = statbuf.st_size;
2166fa9e4066Sahrens 	psize = P2ALIGN(psize, (uint64_t)sizeof (vdev_label_t));
2167fa9e4066Sahrens 
216853b9a4a9SVictor Latushkin 	for (int l = 0; l < VDEV_LABELS; l++) {
2169fa9e4066Sahrens 		nvlist_t *config = NULL;
2170fa9e4066Sahrens 
2171fa9e4066Sahrens 		(void) printf("--------------------------------------------\n");
2172fa9e4066Sahrens 		(void) printf("LABEL %d\n", l);
2173fa9e4066Sahrens 		(void) printf("--------------------------------------------\n");
2174fa9e4066Sahrens 
21750d981225Seschrock 		if (pread64(fd, &label, sizeof (label),
2176fa9e4066Sahrens 		    vdev_label_offset(psize, l, 0)) != sizeof (label)) {
2177fa9e4066Sahrens 			(void) printf("failed to read label %d\n", l);
2178fa9e4066Sahrens 			continue;
2179fa9e4066Sahrens 		}
2180fa9e4066Sahrens 
2181fa9e4066Sahrens 		if (nvlist_unpack(buf, buflen, &config, 0) != 0) {
2182fa9e4066Sahrens 			(void) printf("failed to unpack label %d\n", l);
218353b9a4a9SVictor Latushkin 			ashift = SPA_MINBLOCKSHIFT;
218453b9a4a9SVictor Latushkin 		} else {
218553b9a4a9SVictor Latushkin 			nvlist_t *vdev_tree = NULL;
218653b9a4a9SVictor Latushkin 
218753b9a4a9SVictor Latushkin 			dump_nvlist(config, 4);
218853b9a4a9SVictor Latushkin 			if ((nvlist_lookup_nvlist(config,
218953b9a4a9SVictor Latushkin 			    ZPOOL_CONFIG_VDEV_TREE, &vdev_tree) != 0) ||
219053b9a4a9SVictor Latushkin 			    (nvlist_lookup_uint64(vdev_tree,
219153b9a4a9SVictor Latushkin 			    ZPOOL_CONFIG_ASHIFT, &ashift) != 0))
219253b9a4a9SVictor Latushkin 				ashift = SPA_MINBLOCKSHIFT;
219353b9a4a9SVictor Latushkin 			nvlist_free(config);
2194fa9e4066Sahrens 		}
219553b9a4a9SVictor Latushkin 		if (dump_opt['u'])
219653b9a4a9SVictor Latushkin 			dump_label_uberblocks(&label, ashift);
2197fa9e4066Sahrens 	}
2198c6065d0fSGeorge Wilson 
2199c6065d0fSGeorge Wilson 	free(path);
2200c6065d0fSGeorge Wilson 	(void) close(fd);
2201fa9e4066Sahrens }
2202fa9e4066Sahrens 
2203ca0cc391SMatthew Ahrens static uint64_t dataset_feature_count[SPA_FEATURES];
2204b5152584SMatthew Ahrens 
2205fa9e4066Sahrens /*ARGSUSED*/
22061d452cf5Sahrens static int
2207fd136879SMatthew Ahrens dump_one_dir(const char *dsname, void *arg)
2208fa9e4066Sahrens {
2209fa9e4066Sahrens 	int error;
2210fa9e4066Sahrens 	objset_t *os;
2211fa9e4066Sahrens 
2212503ad85cSMatthew Ahrens 	error = dmu_objset_own(dsname, DMU_OST_ANY, B_TRUE, FTAG, &os);
2213fa9e4066Sahrens 	if (error) {
2214b24ab676SJeff Bonwick 		(void) printf("Could not open %s, error %d\n", dsname, error);
22151d452cf5Sahrens 		return (0);
2216fa9e4066Sahrens 	}
2217ca0cc391SMatthew Ahrens 
2218ca0cc391SMatthew Ahrens 	for (spa_feature_t f = 0; f < SPA_FEATURES; f++) {
2219ca0cc391SMatthew Ahrens 		if (!dmu_objset_ds(os)->ds_feature_inuse[f])
2220ca0cc391SMatthew Ahrens 			continue;
2221ca0cc391SMatthew Ahrens 		ASSERT(spa_feature_table[f].fi_flags &
2222ca0cc391SMatthew Ahrens 		    ZFEATURE_FLAG_PER_DATASET);
2223ca0cc391SMatthew Ahrens 		dataset_feature_count[f]++;
2224ca0cc391SMatthew Ahrens 	}
2225ca0cc391SMatthew Ahrens 
2226fa9e4066Sahrens 	dump_dir(os);
2227503ad85cSMatthew Ahrens 	dmu_objset_disown(os, FTAG);
2228e0d35c44Smarks 	fuid_table_destroy();
22290a586ceaSMark Shellenbaum 	sa_loaded = B_FALSE;
22301d452cf5Sahrens 	return (0);
2231fa9e4066Sahrens }
2232fa9e4066Sahrens 
2233fa9e4066Sahrens /*
2234b24ab676SJeff Bonwick  * Block statistics.
2235fa9e4066Sahrens  */
2236b5152584SMatthew Ahrens #define	PSIZE_HISTO_SIZE (SPA_OLD_MAXBLOCKSIZE / SPA_MINBLOCKSIZE + 2)
2237fa9e4066Sahrens typedef struct zdb_blkstats {
2238490d05b9SMatthew Ahrens 	uint64_t zb_asize;
2239490d05b9SMatthew Ahrens 	uint64_t zb_lsize;
2240490d05b9SMatthew Ahrens 	uint64_t zb_psize;
2241490d05b9SMatthew Ahrens 	uint64_t zb_count;
2242d5ee8a13SMatthew Ahrens 	uint64_t zb_gangs;
2243d5ee8a13SMatthew Ahrens 	uint64_t zb_ditto_samevdev;
2244490d05b9SMatthew Ahrens 	uint64_t zb_psize_histogram[PSIZE_HISTO_SIZE];
2245fa9e4066Sahrens } zdb_blkstats_t;
2246fa9e4066Sahrens 
2247b24ab676SJeff Bonwick /*
2248b24ab676SJeff Bonwick  * Extended object types to report deferred frees and dedup auto-ditto blocks.
2249b24ab676SJeff Bonwick  */
2250b24ab676SJeff Bonwick #define	ZDB_OT_DEFERRED	(DMU_OT_NUMTYPES + 0)
2251b24ab676SJeff Bonwick #define	ZDB_OT_DITTO	(DMU_OT_NUMTYPES + 1)
2252ad135b5dSChristopher Siden #define	ZDB_OT_OTHER	(DMU_OT_NUMTYPES + 2)
2253ad135b5dSChristopher Siden #define	ZDB_OT_TOTAL	(DMU_OT_NUMTYPES + 3)
2254b24ab676SJeff Bonwick 
2255b24ab676SJeff Bonwick static char *zdb_ot_extname[] = {
2256b24ab676SJeff Bonwick 	"deferred free",
2257b24ab676SJeff Bonwick 	"dedup ditto",
2258ad135b5dSChristopher Siden 	"other",
2259b24ab676SJeff Bonwick 	"Total",
2260b24ab676SJeff Bonwick };
2261fa9e4066Sahrens 
226288b7b0f2SMatthew Ahrens #define	ZB_TOTAL	DN_MAX_LEVELS
2263fa9e4066Sahrens 
2264fa9e4066Sahrens typedef struct zdb_cb {
2265b24ab676SJeff Bonwick 	zdb_blkstats_t	zcb_type[ZB_TOTAL + 1][ZDB_OT_TOTAL + 1];
2266b24ab676SJeff Bonwick 	uint64_t	zcb_dedup_asize;
2267b24ab676SJeff Bonwick 	uint64_t	zcb_dedup_blocks;
22685d7b4d43SMatthew Ahrens 	uint64_t	zcb_embedded_blocks[NUM_BP_EMBEDDED_TYPES];
22695d7b4d43SMatthew Ahrens 	uint64_t	zcb_embedded_histogram[NUM_BP_EMBEDDED_TYPES]
22705d7b4d43SMatthew Ahrens 	    [BPE_PAYLOAD_SIZE];
2271490d05b9SMatthew Ahrens 	uint64_t	zcb_start;
2272490d05b9SMatthew Ahrens 	uint64_t	zcb_lastprint;
2273490d05b9SMatthew Ahrens 	uint64_t	zcb_totalasize;
2274fa9e4066Sahrens 	uint64_t	zcb_errors[256];
2275fa9e4066Sahrens 	int		zcb_readfails;
2276fa9e4066Sahrens 	int		zcb_haderrors;
2277cde58dbcSMatthew Ahrens 	spa_t		*zcb_spa;
2278fa9e4066Sahrens } zdb_cb_t;
2279fa9e4066Sahrens 
2280fa9e4066Sahrens static void
2281cde58dbcSMatthew Ahrens zdb_count_block(zdb_cb_t *zcb, zilog_t *zilog, const blkptr_t *bp,
2282b24ab676SJeff Bonwick     dmu_object_type_t type)
2283fa9e4066Sahrens {
2284b24ab676SJeff Bonwick 	uint64_t refcnt = 0;
2285b24ab676SJeff Bonwick 
2286b24ab676SJeff Bonwick 	ASSERT(type < ZDB_OT_TOTAL);
2287b24ab676SJeff Bonwick 
2288b24ab676SJeff Bonwick 	if (zilog && zil_bp_tree_add(zilog, bp) != 0)
2289b24ab676SJeff Bonwick 		return;
2290b24ab676SJeff Bonwick 
2291e14bb325SJeff Bonwick 	for (int i = 0; i < 4; i++) {
2292fa9e4066Sahrens 		int l = (i < 2) ? BP_GET_LEVEL(bp) : ZB_TOTAL;
2293b24ab676SJeff Bonwick 		int t = (i & 1) ? type : ZDB_OT_TOTAL;
2294d5ee8a13SMatthew Ahrens 		int equal;
2295fa9e4066Sahrens 		zdb_blkstats_t *zb = &zcb->zcb_type[l][t];
2296fa9e4066Sahrens 
2297fa9e4066Sahrens 		zb->zb_asize += BP_GET_ASIZE(bp);
2298fa9e4066Sahrens 		zb->zb_lsize += BP_GET_LSIZE(bp);
2299fa9e4066Sahrens 		zb->zb_psize += BP_GET_PSIZE(bp);
2300fa9e4066Sahrens 		zb->zb_count++;
2301b5152584SMatthew Ahrens 
2302b5152584SMatthew Ahrens 		/*
2303b5152584SMatthew Ahrens 		 * The histogram is only big enough to record blocks up to
2304b5152584SMatthew Ahrens 		 * SPA_OLD_MAXBLOCKSIZE; larger blocks go into the last,
2305b5152584SMatthew Ahrens 		 * "other", bucket.
2306b5152584SMatthew Ahrens 		 */
2307b5152584SMatthew Ahrens 		int idx = BP_GET_PSIZE(bp) >> SPA_MINBLOCKSHIFT;
2308b5152584SMatthew Ahrens 		idx = MIN(idx, SPA_OLD_MAXBLOCKSIZE / SPA_MINBLOCKSIZE + 1);
2309b5152584SMatthew Ahrens 		zb->zb_psize_histogram[idx]++;
2310d5ee8a13SMatthew Ahrens 
2311d5ee8a13SMatthew Ahrens 		zb->zb_gangs += BP_COUNT_GANG(bp);
2312d5ee8a13SMatthew Ahrens 
2313d5ee8a13SMatthew Ahrens 		switch (BP_GET_NDVAS(bp)) {
2314d5ee8a13SMatthew Ahrens 		case 2:
2315d5ee8a13SMatthew Ahrens 			if (DVA_GET_VDEV(&bp->blk_dva[0]) ==
2316d5ee8a13SMatthew Ahrens 			    DVA_GET_VDEV(&bp->blk_dva[1]))
2317d5ee8a13SMatthew Ahrens 				zb->zb_ditto_samevdev++;
2318d5ee8a13SMatthew Ahrens 			break;
2319d5ee8a13SMatthew Ahrens 		case 3:
2320d5ee8a13SMatthew Ahrens 			equal = (DVA_GET_VDEV(&bp->blk_dva[0]) ==
2321d5ee8a13SMatthew Ahrens 			    DVA_GET_VDEV(&bp->blk_dva[1])) +
2322d5ee8a13SMatthew Ahrens 			    (DVA_GET_VDEV(&bp->blk_dva[0]) ==
2323d5ee8a13SMatthew Ahrens 			    DVA_GET_VDEV(&bp->blk_dva[2])) +
2324d5ee8a13SMatthew Ahrens 			    (DVA_GET_VDEV(&bp->blk_dva[1]) ==
2325d5ee8a13SMatthew Ahrens 			    DVA_GET_VDEV(&bp->blk_dva[2]));
2326d5ee8a13SMatthew Ahrens 			if (equal != 0)
2327d5ee8a13SMatthew Ahrens 				zb->zb_ditto_samevdev++;
2328d5ee8a13SMatthew Ahrens 			break;
2329d5ee8a13SMatthew Ahrens 		}
2330d5ee8a13SMatthew Ahrens 
2331fa9e4066Sahrens 	}
2332fa9e4066Sahrens 
23335d7b4d43SMatthew Ahrens 	if (BP_IS_EMBEDDED(bp)) {
23345d7b4d43SMatthew Ahrens 		zcb->zcb_embedded_blocks[BPE_GET_ETYPE(bp)]++;
23355d7b4d43SMatthew Ahrens 		zcb->zcb_embedded_histogram[BPE_GET_ETYPE(bp)]
23365d7b4d43SMatthew Ahrens 		    [BPE_GET_PSIZE(bp)]++;
23375d7b4d43SMatthew Ahrens 		return;
23385d7b4d43SMatthew Ahrens 	}
23395d7b4d43SMatthew Ahrens 
2340b24ab676SJeff Bonwick 	if (dump_opt['L'])
2341b24ab676SJeff Bonwick 		return;
2342b24ab676SJeff Bonwick 
2343b24ab676SJeff Bonwick 	if (BP_GET_DEDUP(bp)) {
2344b24ab676SJeff Bonwick 		ddt_t *ddt;
2345b24ab676SJeff Bonwick 		ddt_entry_t *dde;
2346b24ab676SJeff Bonwick 
2347cde58dbcSMatthew Ahrens 		ddt = ddt_select(zcb->zcb_spa, bp);
2348b24ab676SJeff Bonwick 		ddt_enter(ddt);
2349b24ab676SJeff Bonwick 		dde = ddt_lookup(ddt, bp, B_FALSE);
2350b24ab676SJeff Bonwick 
2351b24ab676SJeff Bonwick 		if (dde == NULL) {
2352b24ab676SJeff Bonwick 			refcnt = 0;
2353b24ab676SJeff Bonwick 		} else {
2354b24ab676SJeff Bonwick 			ddt_phys_t *ddp = ddt_phys_select(dde, bp);
2355b24ab676SJeff Bonwick 			ddt_phys_decref(ddp);
2356b24ab676SJeff Bonwick 			refcnt = ddp->ddp_refcnt;
2357b24ab676SJeff Bonwick 			if (ddt_phys_total_refcnt(dde) == 0)
2358b24ab676SJeff Bonwick 				ddt_remove(ddt, dde);
2359d41e7643Sek 		}
2360b24ab676SJeff Bonwick 		ddt_exit(ddt);
2361d41e7643Sek 	}
2362d41e7643Sek 
2363cde58dbcSMatthew Ahrens 	VERIFY3U(zio_wait(zio_claim(NULL, zcb->zcb_spa,
2364cde58dbcSMatthew Ahrens 	    refcnt ? 0 : spa_first_txg(zcb->zcb_spa),
2365b24ab676SJeff Bonwick 	    bp, NULL, NULL, ZIO_FLAG_CANFAIL)), ==, 0);
2366fa9e4066Sahrens }
2367fa9e4066Sahrens 
236831d7e8faSGeorge Wilson static void
236931d7e8faSGeorge Wilson zdb_blkptr_done(zio_t *zio)
237031d7e8faSGeorge Wilson {
237131d7e8faSGeorge Wilson 	spa_t *spa = zio->io_spa;
237231d7e8faSGeorge Wilson 	blkptr_t *bp = zio->io_bp;
237331d7e8faSGeorge Wilson 	int ioerr = zio->io_error;
237431d7e8faSGeorge Wilson 	zdb_cb_t *zcb = zio->io_private;
23757802d7bfSMatthew Ahrens 	zbookmark_phys_t *zb = &zio->io_bookmark;
237631d7e8faSGeorge Wilson 
237731d7e8faSGeorge Wilson 	zio_data_buf_free(zio->io_data, zio->io_size);
237831d7e8faSGeorge Wilson 
237931d7e8faSGeorge Wilson 	mutex_enter(&spa->spa_scrub_lock);
238031d7e8faSGeorge Wilson 	spa->spa_scrub_inflight--;
238131d7e8faSGeorge Wilson 	cv_broadcast(&spa->spa_scrub_io_cv);
238231d7e8faSGeorge Wilson 
238331d7e8faSGeorge Wilson 	if (ioerr && !(zio->io_flags & ZIO_FLAG_SPECULATIVE)) {
238431d7e8faSGeorge Wilson 		char blkbuf[BP_SPRINTF_LEN];
238531d7e8faSGeorge Wilson 
238631d7e8faSGeorge Wilson 		zcb->zcb_haderrors = 1;
238731d7e8faSGeorge Wilson 		zcb->zcb_errors[ioerr]++;
238831d7e8faSGeorge Wilson 
238931d7e8faSGeorge Wilson 		if (dump_opt['b'] >= 2)
239043466aaeSMax Grossman 			snprintf_blkptr(blkbuf, sizeof (blkbuf), bp);
239131d7e8faSGeorge Wilson 		else
239231d7e8faSGeorge Wilson 			blkbuf[0] = '\0';
239331d7e8faSGeorge Wilson 
239431d7e8faSGeorge Wilson 		(void) printf("zdb_blkptr_cb: "
239531d7e8faSGeorge Wilson 		    "Got error %d reading "
239631d7e8faSGeorge Wilson 		    "<%llu, %llu, %lld, %llx> %s -- skipping\n",
239731d7e8faSGeorge Wilson 		    ioerr,
239831d7e8faSGeorge Wilson 		    (u_longlong_t)zb->zb_objset,
239931d7e8faSGeorge Wilson 		    (u_longlong_t)zb->zb_object,
240031d7e8faSGeorge Wilson 		    (u_longlong_t)zb->zb_level,
240131d7e8faSGeorge Wilson 		    (u_longlong_t)zb->zb_blkid,
240231d7e8faSGeorge Wilson 		    blkbuf);
240331d7e8faSGeorge Wilson 	}
240431d7e8faSGeorge Wilson 	mutex_exit(&spa->spa_scrub_lock);
240531d7e8faSGeorge Wilson }
240631d7e8faSGeorge Wilson 
2407fa9e4066Sahrens static int
24081b912ec7SGeorge Wilson zdb_blkptr_cb(spa_t *spa, zilog_t *zilog, const blkptr_t *bp,
24097802d7bfSMatthew Ahrens     const zbookmark_phys_t *zb, const dnode_phys_t *dnp, void *arg)
2410fa9e4066Sahrens {
2411fa9e4066Sahrens 	zdb_cb_t *zcb = arg;
24126365109dSVictor Latushkin 	dmu_object_type_t type;
2413468c413aSTim Haley 	boolean_t is_metadata;
2414fa9e4066Sahrens 
2415a2cdcdd2SPaul Dagnelie 	if (bp == NULL)
2416a2cdcdd2SPaul Dagnelie 		return (0);
2417a2cdcdd2SPaul Dagnelie 
241843466aaeSMax Grossman 	if (dump_opt['b'] >= 5 && bp->blk_birth > 0) {
241943466aaeSMax Grossman 		char blkbuf[BP_SPRINTF_LEN];
242043466aaeSMax Grossman 		snprintf_blkptr(blkbuf, sizeof (blkbuf), bp);
242143466aaeSMax Grossman 		(void) printf("objset %llu object %llu "
242243466aaeSMax Grossman 		    "level %lld offset 0x%llx %s\n",
242343466aaeSMax Grossman 		    (u_longlong_t)zb->zb_objset,
242443466aaeSMax Grossman 		    (u_longlong_t)zb->zb_object,
242543466aaeSMax Grossman 		    (longlong_t)zb->zb_level,
242643466aaeSMax Grossman 		    (u_longlong_t)blkid2offset(dnp, bp, zb),
242743466aaeSMax Grossman 		    blkbuf);
242843466aaeSMax Grossman 	}
242943466aaeSMax Grossman 
243043466aaeSMax Grossman 	if (BP_IS_HOLE(bp))
243188b7b0f2SMatthew Ahrens 		return (0);
2432e14bb325SJeff Bonwick 
24336365109dSVictor Latushkin 	type = BP_GET_TYPE(bp);
2434e14bb325SJeff Bonwick 
2435ad135b5dSChristopher Siden 	zdb_count_block(zcb, zilog, bp,
2436ad135b5dSChristopher Siden 	    (type & DMU_OT_NEWTYPE) ? ZDB_OT_OTHER : type);
24376365109dSVictor Latushkin 
2438ad135b5dSChristopher Siden 	is_metadata = (BP_GET_LEVEL(bp) != 0 || DMU_OT_IS_METADATA(type));
24396365109dSVictor Latushkin 
24405d7b4d43SMatthew Ahrens 	if (!BP_IS_EMBEDDED(bp) &&
24415d7b4d43SMatthew Ahrens 	    (dump_opt['c'] > 1 || (dump_opt['c'] && is_metadata))) {
2442468c413aSTim Haley 		size_t size = BP_GET_PSIZE(bp);
244331d7e8faSGeorge Wilson 		void *data = zio_data_buf_alloc(size);
2444b24ab676SJeff Bonwick 		int flags = ZIO_FLAG_CANFAIL | ZIO_FLAG_SCRUB | ZIO_FLAG_RAW;
2445b24ab676SJeff Bonwick 
2446b24ab676SJeff Bonwick 		/* If it's an intent log block, failure is expected. */
2447b24ab676SJeff Bonwick 		if (zb->zb_level == ZB_ZIL_LEVEL)
2448b24ab676SJeff Bonwick 			flags |= ZIO_FLAG_SPECULATIVE;
2449b24ab676SJeff Bonwick 
245031d7e8faSGeorge Wilson 		mutex_enter(&spa->spa_scrub_lock);
245131d7e8faSGeorge Wilson 		while (spa->spa_scrub_inflight > max_inflight)
245231d7e8faSGeorge Wilson 			cv_wait(&spa->spa_scrub_io_cv, &spa->spa_scrub_lock);
245331d7e8faSGeorge Wilson 		spa->spa_scrub_inflight++;
245431d7e8faSGeorge Wilson 		mutex_exit(&spa->spa_scrub_lock);
2455b24ab676SJeff Bonwick 
245631d7e8faSGeorge Wilson 		zio_nowait(zio_read(NULL, spa, bp, data, size,
245731d7e8faSGeorge Wilson 		    zdb_blkptr_done, zcb, ZIO_PRIORITY_ASYNC_READ, flags, zb));
2458fa9e4066Sahrens 	}
2459fa9e4066Sahrens 
2460fa9e4066Sahrens 	zcb->zcb_readfails = 0;
2461fa9e4066Sahrens 
2462f7950bf1SMatthew Ahrens 	/* only call gethrtime() every 100 blocks */
2463f7950bf1SMatthew Ahrens 	static int iters;
2464f7950bf1SMatthew Ahrens 	if (++iters > 100)
2465f7950bf1SMatthew Ahrens 		iters = 0;
2466f7950bf1SMatthew Ahrens 	else
2467f7950bf1SMatthew Ahrens 		return (0);
2468f7950bf1SMatthew Ahrens 
2469f7950bf1SMatthew Ahrens 	if (dump_opt['b'] < 5 && gethrtime() > zcb->zcb_lastprint + NANOSEC) {
2470490d05b9SMatthew Ahrens 		uint64_t now = gethrtime();
2471490d05b9SMatthew Ahrens 		char buf[10];
2472490d05b9SMatthew Ahrens 		uint64_t bytes = zcb->zcb_type[ZB_TOTAL][ZDB_OT_TOTAL].zb_asize;
2473490d05b9SMatthew Ahrens 		int kb_per_sec =
2474490d05b9SMatthew Ahrens 		    1 + bytes / (1 + ((now - zcb->zcb_start) / 1000 / 1000));
2475490d05b9SMatthew Ahrens 		int sec_remaining =
2476490d05b9SMatthew Ahrens 		    (zcb->zcb_totalasize - bytes) / 1024 / kb_per_sec;
2477490d05b9SMatthew Ahrens 
2478490d05b9SMatthew Ahrens 		zfs_nicenum(bytes, buf, sizeof (buf));
2479490d05b9SMatthew Ahrens 		(void) fprintf(stderr,
2480490d05b9SMatthew Ahrens 		    "\r%5s completed (%4dMB/s) "
2481490d05b9SMatthew Ahrens 		    "estimated time remaining: %uhr %02umin %02usec        ",
2482490d05b9SMatthew Ahrens 		    buf, kb_per_sec / 1024,
2483490d05b9SMatthew Ahrens 		    sec_remaining / 60 / 60,
2484490d05b9SMatthew Ahrens 		    sec_remaining / 60 % 60,
2485490d05b9SMatthew Ahrens 		    sec_remaining % 60);
2486490d05b9SMatthew Ahrens 
2487490d05b9SMatthew Ahrens 		zcb->zcb_lastprint = now;
2488490d05b9SMatthew Ahrens 	}
2489490d05b9SMatthew Ahrens 
2490fa9e4066Sahrens 	return (0);
2491fa9e4066Sahrens }
2492fa9e4066Sahrens 
2493b24ab676SJeff Bonwick static void
24940713e232SGeorge Wilson zdb_leak(void *arg, uint64_t start, uint64_t size)
2495b24ab676SJeff Bonwick {
24960713e232SGeorge Wilson 	vdev_t *vd = arg;
2497b24ab676SJeff Bonwick 
2498b24ab676SJeff Bonwick 	(void) printf("leaked space: vdev %llu, offset 0x%llx, size %llu\n",
2499b24ab676SJeff Bonwick 	    (u_longlong_t)vd->vdev_id, (u_longlong_t)start, (u_longlong_t)size);
2500b24ab676SJeff Bonwick }
2501b24ab676SJeff Bonwick 
25020713e232SGeorge Wilson static metaslab_ops_t zdb_metaslab_ops = {
25032e4c9986SGeorge Wilson 	NULL	/* alloc */
2504b24ab676SJeff Bonwick };
2505b24ab676SJeff Bonwick 
2506b24ab676SJeff Bonwick static void
2507bbfd46c4SJeff Bonwick zdb_ddt_leak_init(spa_t *spa, zdb_cb_t *zcb)
2508b24ab676SJeff Bonwick {
2509bbfd46c4SJeff Bonwick 	ddt_bookmark_t ddb = { 0 };
2510b24ab676SJeff Bonwick 	ddt_entry_t dde;
2511b24ab676SJeff Bonwick 	int error;
2512b24ab676SJeff Bonwick 
2513bbfd46c4SJeff Bonwick 	while ((error = ddt_walk(spa, &ddb, &dde)) == 0) {
2514b24ab676SJeff Bonwick 		blkptr_t blk;
2515b24ab676SJeff Bonwick 		ddt_phys_t *ddp = dde.dde_phys;
2516bbfd46c4SJeff Bonwick 
2517bbfd46c4SJeff Bonwick 		if (ddb.ddb_class == DDT_CLASS_UNIQUE)
2518bbfd46c4SJeff Bonwick 			return;
2519bbfd46c4SJeff Bonwick 
2520b24ab676SJeff Bonwick 		ASSERT(ddt_phys_total_refcnt(&dde) > 1);
2521bbfd46c4SJeff Bonwick 
2522b24ab676SJeff Bonwick 		for (int p = 0; p < DDT_PHYS_TYPES; p++, ddp++) {
2523b24ab676SJeff Bonwick 			if (ddp->ddp_phys_birth == 0)
2524b24ab676SJeff Bonwick 				continue;
2525bbfd46c4SJeff Bonwick 			ddt_bp_create(ddb.ddb_checksum,
2526bbfd46c4SJeff Bonwick 			    &dde.dde_key, ddp, &blk);
2527b24ab676SJeff Bonwick 			if (p == DDT_PHYS_DITTO) {
2528cde58dbcSMatthew Ahrens 				zdb_count_block(zcb, NULL, &blk, ZDB_OT_DITTO);
2529b24ab676SJeff Bonwick 			} else {
2530b24ab676SJeff Bonwick 				zcb->zcb_dedup_asize +=
2531b24ab676SJeff Bonwick 				    BP_GET_ASIZE(&blk) * (ddp->ddp_refcnt - 1);
2532b24ab676SJeff Bonwick 				zcb->zcb_dedup_blocks++;
2533b24ab676SJeff Bonwick 			}
2534b24ab676SJeff Bonwick 		}
2535b24ab676SJeff Bonwick 		if (!dump_opt['L']) {
2536bbfd46c4SJeff Bonwick 			ddt_t *ddt = spa->spa_ddt[ddb.ddb_checksum];
2537b24ab676SJeff Bonwick 			ddt_enter(ddt);
2538b24ab676SJeff Bonwick 			VERIFY(ddt_lookup(ddt, &blk, B_TRUE) != NULL);
2539b24ab676SJeff Bonwick 			ddt_exit(ddt);
2540b24ab676SJeff Bonwick 		}
2541b24ab676SJeff Bonwick 	}
2542b24ab676SJeff Bonwick 
2543b24ab676SJeff Bonwick 	ASSERT(error == ENOENT);
2544b24ab676SJeff Bonwick }
2545b24ab676SJeff Bonwick 
2546b24ab676SJeff Bonwick static void
2547b24ab676SJeff Bonwick zdb_leak_init(spa_t *spa, zdb_cb_t *zcb)
2548b24ab676SJeff Bonwick {
2549cde58dbcSMatthew Ahrens 	zcb->zcb_spa = spa;
2550cde58dbcSMatthew Ahrens 
2551b24ab676SJeff Bonwick 	if (!dump_opt['L']) {
2552b24ab676SJeff Bonwick 		vdev_t *rvd = spa->spa_root_vdev;
255306be9802SMatthew Ahrens 		for (uint64_t c = 0; c < rvd->vdev_children; c++) {
2554b24ab676SJeff Bonwick 			vdev_t *vd = rvd->vdev_child[c];
255506be9802SMatthew Ahrens 			for (uint64_t m = 0; m < vd->vdev_ms_count; m++) {
2556b24ab676SJeff Bonwick 				metaslab_t *msp = vd->vdev_ms[m];
2557b24ab676SJeff Bonwick 				mutex_enter(&msp->ms_lock);
25580713e232SGeorge Wilson 				metaslab_unload(msp);
25590713e232SGeorge Wilson 
25600713e232SGeorge Wilson 				/*
25610713e232SGeorge Wilson 				 * For leak detection, we overload the metaslab
25620713e232SGeorge Wilson 				 * ms_tree to contain allocated segments
25630713e232SGeorge Wilson 				 * instead of free segments. As a result,
25640713e232SGeorge Wilson 				 * we can't use the normal metaslab_load/unload
25650713e232SGeorge Wilson 				 * interfaces.
25660713e232SGeorge Wilson 				 */
25670713e232SGeorge Wilson 				if (msp->ms_sm != NULL) {
256806be9802SMatthew Ahrens 					(void) fprintf(stderr,
256906be9802SMatthew Ahrens 					    "\rloading space map for "
257006be9802SMatthew Ahrens 					    "vdev %llu of %llu, "
257106be9802SMatthew Ahrens 					    "metaslab %llu of %llu ...",
257206be9802SMatthew Ahrens 					    (longlong_t)c,
257306be9802SMatthew Ahrens 					    (longlong_t)rvd->vdev_children,
257406be9802SMatthew Ahrens 					    (longlong_t)m,
257506be9802SMatthew Ahrens 					    (longlong_t)vd->vdev_ms_count);
257606be9802SMatthew Ahrens 
25770713e232SGeorge Wilson 					msp->ms_ops = &zdb_metaslab_ops;
2578f7950bf1SMatthew Ahrens 
2579f7950bf1SMatthew Ahrens 					/*
2580f7950bf1SMatthew Ahrens 					 * We don't want to spend the CPU
2581f7950bf1SMatthew Ahrens 					 * manipulating the size-ordered
2582f7950bf1SMatthew Ahrens 					 * tree, so clear the range_tree
2583f7950bf1SMatthew Ahrens 					 * ops.
2584f7950bf1SMatthew Ahrens 					 */
2585f7950bf1SMatthew Ahrens 					msp->ms_tree->rt_ops = NULL;
25860713e232SGeorge Wilson 					VERIFY0(space_map_load(msp->ms_sm,
25870713e232SGeorge Wilson 					    msp->ms_tree, SM_ALLOC));
25880713e232SGeorge Wilson 					msp->ms_loaded = B_TRUE;
25890713e232SGeorge Wilson 				}
2590b24ab676SJeff Bonwick 				mutex_exit(&msp->ms_lock);
2591b24ab676SJeff Bonwick 			}
2592b24ab676SJeff Bonwick 		}
259306be9802SMatthew Ahrens 		(void) fprintf(stderr, "\n");
2594b24ab676SJeff Bonwick 	}
2595b24ab676SJeff Bonwick 
2596b24ab676SJeff Bonwick 	spa_config_enter(spa, SCL_CONFIG, FTAG, RW_READER);
2597b24ab676SJeff Bonwick 
2598bbfd46c4SJeff Bonwick 	zdb_ddt_leak_init(spa, zcb);
2599b24ab676SJeff Bonwick 
2600b24ab676SJeff Bonwick 	spa_config_exit(spa, SCL_CONFIG, FTAG);
2601b24ab676SJeff Bonwick }
2602b24ab676SJeff Bonwick 
2603b24ab676SJeff Bonwick static void
2604b24ab676SJeff Bonwick zdb_leak_fini(spa_t *spa)
2605b24ab676SJeff Bonwick {
2606b24ab676SJeff Bonwick 	if (!dump_opt['L']) {
2607b24ab676SJeff Bonwick 		vdev_t *rvd = spa->spa_root_vdev;
2608b24ab676SJeff Bonwick 		for (int c = 0; c < rvd->vdev_children; c++) {
2609b24ab676SJeff Bonwick 			vdev_t *vd = rvd->vdev_child[c];
2610b24ab676SJeff Bonwick 			for (int m = 0; m < vd->vdev_ms_count; m++) {
2611b24ab676SJeff Bonwick 				metaslab_t *msp = vd->vdev_ms[m];
2612b24ab676SJeff Bonwick 				mutex_enter(&msp->ms_lock);
26130713e232SGeorge Wilson 
26140713e232SGeorge Wilson 				/*
26150713e232SGeorge Wilson 				 * The ms_tree has been overloaded to
26160713e232SGeorge Wilson 				 * contain allocated segments. Now that we
26170713e232SGeorge Wilson 				 * finished traversing all blocks, any
26180713e232SGeorge Wilson 				 * block that remains in the ms_tree
26190713e232SGeorge Wilson 				 * represents an allocated block that we
26200713e232SGeorge Wilson 				 * did not claim during the traversal.
26210713e232SGeorge Wilson 				 * Claimed blocks would have been removed
26220713e232SGeorge Wilson 				 * from the ms_tree.
26230713e232SGeorge Wilson 				 */
26240713e232SGeorge Wilson 				range_tree_vacate(msp->ms_tree, zdb_leak, vd);
26250713e232SGeorge Wilson 				msp->ms_loaded = B_FALSE;
26260713e232SGeorge Wilson 
2627b24ab676SJeff Bonwick 				mutex_exit(&msp->ms_lock);
2628b24ab676SJeff Bonwick 			}
2629b24ab676SJeff Bonwick 		}
2630b24ab676SJeff Bonwick 	}
2631b24ab676SJeff Bonwick }
2632b24ab676SJeff Bonwick 
2633cde58dbcSMatthew Ahrens /* ARGSUSED */
2634cde58dbcSMatthew Ahrens static int
2635cde58dbcSMatthew Ahrens count_block_cb(void *arg, const blkptr_t *bp, dmu_tx_t *tx)
2636cde58dbcSMatthew Ahrens {
2637cde58dbcSMatthew Ahrens 	zdb_cb_t *zcb = arg;
2638cde58dbcSMatthew Ahrens 
2639490d05b9SMatthew Ahrens 	if (dump_opt['b'] >= 5) {
2640cde58dbcSMatthew Ahrens 		char blkbuf[BP_SPRINTF_LEN];
264143466aaeSMax Grossman 		snprintf_blkptr(blkbuf, sizeof (blkbuf), bp);
2642cde58dbcSMatthew Ahrens 		(void) printf("[%s] %s\n",
2643cde58dbcSMatthew Ahrens 		    "deferred free", blkbuf);
2644cde58dbcSMatthew Ahrens 	}
2645cde58dbcSMatthew Ahrens 	zdb_count_block(zcb, NULL, bp, ZDB_OT_DEFERRED);
2646cde58dbcSMatthew Ahrens 	return (0);
2647cde58dbcSMatthew Ahrens }
2648cde58dbcSMatthew Ahrens 
2649fa9e4066Sahrens static int
2650fa9e4066Sahrens dump_block_stats(spa_t *spa)
2651fa9e4066Sahrens {
2652fa9e4066Sahrens 	zdb_cb_t zcb = { 0 };
2653fa9e4066Sahrens 	zdb_blkstats_t *zb, *tzb;
2654b24ab676SJeff Bonwick 	uint64_t norm_alloc, norm_space, total_alloc, total_found;
2655cd088ea4SVictor Latushkin 	int flags = TRAVERSE_PRE | TRAVERSE_PREFETCH_METADATA | TRAVERSE_HARD;
26565d7b4d43SMatthew Ahrens 	boolean_t leaks = B_FALSE;
2657fa9e4066Sahrens 
2658490d05b9SMatthew Ahrens 	(void) printf("\nTraversing all blocks %s%s%s%s%s...\n\n",
2659b24ab676SJeff Bonwick 	    (dump_opt['c'] || !dump_opt['L']) ? "to verify " : "",
2660b24ab676SJeff Bonwick 	    (dump_opt['c'] == 1) ? "metadata " : "",
2661b24ab676SJeff Bonwick 	    dump_opt['c'] ? "checksums " : "",
2662b24ab676SJeff Bonwick 	    (dump_opt['c'] && !dump_opt['L']) ? "and verify " : "",
2663b24ab676SJeff Bonwick 	    !dump_opt['L'] ? "nothing leaked " : "");
2664fa9e4066Sahrens 
2665fa9e4066Sahrens 	/*
2666e14bb325SJeff Bonwick 	 * Load all space maps as SM_ALLOC maps, then traverse the pool
2667e14bb325SJeff Bonwick 	 * claiming each block we discover.  If the pool is perfectly
2668e14bb325SJeff Bonwick 	 * consistent, the space maps will be empty when we're done.
2669e14bb325SJeff Bonwick 	 * Anything left over is a leak; any block we can't claim (because
2670e14bb325SJeff Bonwick 	 * it's not part of any space map) is a double allocation,
2671e14bb325SJeff Bonwick 	 * reference to a freed block, or an unclaimed log block.
2672fa9e4066Sahrens 	 */
2673b24ab676SJeff Bonwick 	zdb_leak_init(spa, &zcb);
2674fa9e4066Sahrens 
2675fa9e4066Sahrens 	/*
2676fa9e4066Sahrens 	 * If there's a deferred-free bplist, process that first.
2677fa9e4066Sahrens 	 */
2678cde58dbcSMatthew Ahrens 	(void) bpobj_iterate_nofree(&spa->spa_deferred_bpobj,
2679cde58dbcSMatthew Ahrens 	    count_block_cb, &zcb, NULL);
26803b2aab18SMatthew Ahrens 	if (spa_version(spa) >= SPA_VERSION_DEADLISTS) {
26813b2aab18SMatthew Ahrens 		(void) bpobj_iterate_nofree(&spa->spa_dsl_pool->dp_free_bpobj,
26823b2aab18SMatthew Ahrens 		    count_block_cb, &zcb, NULL);
26833b2aab18SMatthew Ahrens 	}
26842acef22dSMatthew Ahrens 	if (spa_feature_is_active(spa, SPA_FEATURE_ASYNC_DESTROY)) {
2685b420f3adSRichard Lowe 		VERIFY3U(0, ==, bptree_iterate(spa->spa_meta_objset,
2686ad135b5dSChristopher Siden 		    spa->spa_dsl_pool->dp_bptree_obj, B_FALSE, count_block_cb,
2687ad135b5dSChristopher Siden 		    &zcb, NULL));
2688ad135b5dSChristopher Siden 	}
2689fa9e4066Sahrens 
2690bbfd46c4SJeff Bonwick 	if (dump_opt['c'] > 1)
2691bbfd46c4SJeff Bonwick 		flags |= TRAVERSE_PREFETCH_DATA;
2692bbfd46c4SJeff Bonwick 
2693490d05b9SMatthew Ahrens 	zcb.zcb_totalasize = metaslab_class_get_alloc(spa_normal_class(spa));
2694490d05b9SMatthew Ahrens 	zcb.zcb_start = zcb.zcb_lastprint = gethrtime();
2695bbfd46c4SJeff Bonwick 	zcb.zcb_haderrors |= traverse_pool(spa, 0, flags, zdb_blkptr_cb, &zcb);
2696fa9e4066Sahrens 
269731d7e8faSGeorge Wilson 	/*
269831d7e8faSGeorge Wilson 	 * If we've traversed the data blocks then we need to wait for those
269931d7e8faSGeorge Wilson 	 * I/Os to complete. We leverage "The Godfather" zio to wait on
270031d7e8faSGeorge Wilson 	 * all async I/Os to complete.
270131d7e8faSGeorge Wilson 	 */
270231d7e8faSGeorge Wilson 	if (dump_opt['c']) {
27036f834bc1SMatthew Ahrens 		for (int i = 0; i < max_ncpus; i++) {
27046f834bc1SMatthew Ahrens 			(void) zio_wait(spa->spa_async_zio_root[i]);
27056f834bc1SMatthew Ahrens 			spa->spa_async_zio_root[i] = zio_root(spa, NULL, NULL,
27066f834bc1SMatthew Ahrens 			    ZIO_FLAG_CANFAIL | ZIO_FLAG_SPECULATIVE |
27076f834bc1SMatthew Ahrens 			    ZIO_FLAG_GODFATHER);
27086f834bc1SMatthew Ahrens 		}
270931d7e8faSGeorge Wilson 	}
271031d7e8faSGeorge Wilson 
2711b24ab676SJeff Bonwick 	if (zcb.zcb_haderrors) {
2712fa9e4066Sahrens 		(void) printf("\nError counts:\n\n");
2713fa9e4066Sahrens 		(void) printf("\t%5s  %s\n", "errno", "count");
2714b24ab676SJeff Bonwick 		for (int e = 0; e < 256; e++) {
2715fa9e4066Sahrens 			if (zcb.zcb_errors[e] != 0) {
2716fa9e4066Sahrens 				(void) printf("\t%5d  %llu\n",
2717fa9e4066Sahrens 				    e, (u_longlong_t)zcb.zcb_errors[e]);
2718fa9e4066Sahrens 			}
2719fa9e4066Sahrens 		}
2720fa9e4066Sahrens 	}
2721fa9e4066Sahrens 
2722fa9e4066Sahrens 	/*
2723fa9e4066Sahrens 	 * Report any leaked segments.
2724fa9e4066Sahrens 	 */
2725b24ab676SJeff Bonwick 	zdb_leak_fini(spa);
2726fa9e4066Sahrens 
2727b24ab676SJeff Bonwick 	tzb = &zcb.zcb_type[ZB_TOTAL][ZDB_OT_TOTAL];
27288654d025Sperrin 
2729b24ab676SJeff Bonwick 	norm_alloc = metaslab_class_get_alloc(spa_normal_class(spa));
2730b24ab676SJeff Bonwick 	norm_space = metaslab_class_get_space(spa_normal_class(spa));
27318654d025Sperrin 
2732b24ab676SJeff Bonwick 	total_alloc = norm_alloc + metaslab_class_get_alloc(spa_log_class(spa));
2733b24ab676SJeff Bonwick 	total_found = tzb->zb_asize - zcb.zcb_dedup_asize;
2734fa9e4066Sahrens 
2735b24ab676SJeff Bonwick 	if (total_found == total_alloc) {
273682a0a985SVictor Latushkin 		if (!dump_opt['L'])
273782a0a985SVictor Latushkin 			(void) printf("\n\tNo leaks (block sum matches space"
273882a0a985SVictor Latushkin 			    " maps exactly)\n");
2739fa9e4066Sahrens 	} else {
2740fa9e4066Sahrens 		(void) printf("block traversal size %llu != alloc %llu "
274182a0a985SVictor Latushkin 		    "(%s %lld)\n",
2742b24ab676SJeff Bonwick 		    (u_longlong_t)total_found,
2743b24ab676SJeff Bonwick 		    (u_longlong_t)total_alloc,
274482a0a985SVictor Latushkin 		    (dump_opt['L']) ? "unreachable" : "leaked",
2745b24ab676SJeff Bonwick 		    (longlong_t)(total_alloc - total_found));
27465d7b4d43SMatthew Ahrens 		leaks = B_TRUE;
2747fa9e4066Sahrens 	}
2748fa9e4066Sahrens 
2749fa9e4066Sahrens 	if (tzb->zb_count == 0)
2750fa9e4066Sahrens 		return (2);
2751fa9e4066Sahrens 
2752fa9e4066Sahrens 	(void) printf("\n");
2753fa9e4066Sahrens 	(void) printf("\tbp count:      %10llu\n",
2754fa9e4066Sahrens 	    (u_longlong_t)tzb->zb_count);
2755d5ee8a13SMatthew Ahrens 	(void) printf("\tganged count:  %10llu\n",
2756d5ee8a13SMatthew Ahrens 	    (longlong_t)tzb->zb_gangs);
2757b24ab676SJeff Bonwick 	(void) printf("\tbp logical:    %10llu      avg: %6llu\n",
2758fa9e4066Sahrens 	    (u_longlong_t)tzb->zb_lsize,
2759fa9e4066Sahrens 	    (u_longlong_t)(tzb->zb_lsize / tzb->zb_count));
2760b24ab676SJeff Bonwick 	(void) printf("\tbp physical:   %10llu      avg:"
2761b24ab676SJeff Bonwick 	    " %6llu     compression: %6.2f\n",
2762fa9e4066Sahrens 	    (u_longlong_t)tzb->zb_psize,
2763fa9e4066Sahrens 	    (u_longlong_t)(tzb->zb_psize / tzb->zb_count),
2764fa9e4066Sahrens 	    (double)tzb->zb_lsize / tzb->zb_psize);
2765b24ab676SJeff Bonwick 	(void) printf("\tbp allocated:  %10llu      avg:"
2766b24ab676SJeff Bonwick 	    " %6llu     compression: %6.2f\n",
2767fa9e4066Sahrens 	    (u_longlong_t)tzb->zb_asize,
2768fa9e4066Sahrens 	    (u_longlong_t)(tzb->zb_asize / tzb->zb_count),
2769fa9e4066Sahrens 	    (double)tzb->zb_lsize / tzb->zb_asize);
2770b24ab676SJeff Bonwick 	(void) printf("\tbp deduped:    %10llu    ref>1:"
2771b24ab676SJeff Bonwick 	    " %6llu   deduplication: %6.2f\n",
2772b24ab676SJeff Bonwick 	    (u_longlong_t)zcb.zcb_dedup_asize,
2773b24ab676SJeff Bonwick 	    (u_longlong_t)zcb.zcb_dedup_blocks,
2774b24ab676SJeff Bonwick 	    (double)zcb.zcb_dedup_asize / tzb->zb_asize + 1.0);
2775b24ab676SJeff Bonwick 	(void) printf("\tSPA allocated: %10llu     used: %5.2f%%\n",
2776b24ab676SJeff Bonwick 	    (u_longlong_t)norm_alloc, 100.0 * norm_alloc / norm_space);
2777fa9e4066Sahrens 
27785d7b4d43SMatthew Ahrens 	for (bp_embedded_type_t i = 0; i < NUM_BP_EMBEDDED_TYPES; i++) {
27795d7b4d43SMatthew Ahrens 		if (zcb.zcb_embedded_blocks[i] == 0)
27805d7b4d43SMatthew Ahrens 			continue;
27815d7b4d43SMatthew Ahrens 		(void) printf("\n");
27825d7b4d43SMatthew Ahrens 		(void) printf("\tadditional, non-pointer bps of type %u: "
27835d7b4d43SMatthew Ahrens 		    "%10llu\n",
27845d7b4d43SMatthew Ahrens 		    i, (u_longlong_t)zcb.zcb_embedded_blocks[i]);
27855d7b4d43SMatthew Ahrens 
27865d7b4d43SMatthew Ahrens 		if (dump_opt['b'] >= 3) {
27875d7b4d43SMatthew Ahrens 			(void) printf("\t number of (compressed) bytes:  "
27885d7b4d43SMatthew Ahrens 			    "number of bps\n");
27895d7b4d43SMatthew Ahrens 			dump_histogram(zcb.zcb_embedded_histogram[i],
27905d7b4d43SMatthew Ahrens 			    sizeof (zcb.zcb_embedded_histogram[i]) /
27915d7b4d43SMatthew Ahrens 			    sizeof (zcb.zcb_embedded_histogram[i][0]), 0);
27925d7b4d43SMatthew Ahrens 		}
27935d7b4d43SMatthew Ahrens 	}
27945d7b4d43SMatthew Ahrens 
2795d5ee8a13SMatthew Ahrens 	if (tzb->zb_ditto_samevdev != 0) {
2796d5ee8a13SMatthew Ahrens 		(void) printf("\tDittoed blocks on same vdev: %llu\n",
2797d5ee8a13SMatthew Ahrens 		    (longlong_t)tzb->zb_ditto_samevdev);
2798d5ee8a13SMatthew Ahrens 	}
2799d5ee8a13SMatthew Ahrens 
2800fa9e4066Sahrens 	if (dump_opt['b'] >= 2) {
2801fa9e4066Sahrens 		int l, t, level;
2802fa9e4066Sahrens 		(void) printf("\nBlocks\tLSIZE\tPSIZE\tASIZE"
2803fa9e4066Sahrens 		    "\t  avg\t comp\t%%Total\tType\n");
2804fa9e4066Sahrens 
2805b24ab676SJeff Bonwick 		for (t = 0; t <= ZDB_OT_TOTAL; t++) {
28063f9d6ad7SLin Ling 			char csize[32], lsize[32], psize[32], asize[32];
2807d5ee8a13SMatthew Ahrens 			char avg[32], gang[32];
2808fa9e4066Sahrens 			char *typename;
2809fa9e4066Sahrens 
2810b24ab676SJeff Bonwick 			if (t < DMU_OT_NUMTYPES)
2811b24ab676SJeff Bonwick 				typename = dmu_ot[t].ot_name;
2812b24ab676SJeff Bonwick 			else
2813b24ab676SJeff Bonwick 				typename = zdb_ot_extname[t - DMU_OT_NUMTYPES];
2814fa9e4066Sahrens 
2815fa9e4066Sahrens 			if (zcb.zcb_type[ZB_TOTAL][t].zb_asize == 0) {
2816fa9e4066Sahrens 				(void) printf("%6s\t%5s\t%5s\t%5s"
2817fa9e4066Sahrens 				    "\t%5s\t%5s\t%6s\t%s\n",
2818fa9e4066Sahrens 				    "-",
2819fa9e4066Sahrens 				    "-",
2820fa9e4066Sahrens 				    "-",
2821fa9e4066Sahrens 				    "-",
2822fa9e4066Sahrens 				    "-",
2823fa9e4066Sahrens 				    "-",
2824fa9e4066Sahrens 				    "-",
2825fa9e4066Sahrens 				    typename);
2826fa9e4066Sahrens 				continue;
2827fa9e4066Sahrens 			}
2828fa9e4066Sahrens 
2829fa9e4066Sahrens 			for (l = ZB_TOTAL - 1; l >= -1; l--) {
2830fa9e4066Sahrens 				level = (l == -1 ? ZB_TOTAL : l);
2831fa9e4066Sahrens 				zb = &zcb.zcb_type[level][t];
2832fa9e4066Sahrens 
2833fa9e4066Sahrens 				if (zb->zb_asize == 0)
2834fa9e4066Sahrens 					continue;
2835fa9e4066Sahrens 
2836fa9e4066Sahrens 				if (dump_opt['b'] < 3 && level != ZB_TOTAL)
2837fa9e4066Sahrens 					continue;
2838fa9e4066Sahrens 
2839fa9e4066Sahrens 				if (level == 0 && zb->zb_asize ==
2840fa9e4066Sahrens 				    zcb.zcb_type[ZB_TOTAL][t].zb_asize)
2841fa9e4066Sahrens 					continue;
2842fa9e4066Sahrens 
28433f9d6ad7SLin Ling 				zdb_nicenum(zb->zb_count, csize);
28443f9d6ad7SLin Ling 				zdb_nicenum(zb->zb_lsize, lsize);
28453f9d6ad7SLin Ling 				zdb_nicenum(zb->zb_psize, psize);
28463f9d6ad7SLin Ling 				zdb_nicenum(zb->zb_asize, asize);
28473f9d6ad7SLin Ling 				zdb_nicenum(zb->zb_asize / zb->zb_count, avg);
2848d5ee8a13SMatthew Ahrens 				zdb_nicenum(zb->zb_gangs, gang);
2849fa9e4066Sahrens 
2850fa9e4066Sahrens 				(void) printf("%6s\t%5s\t%5s\t%5s\t%5s"
2851fa9e4066Sahrens 				    "\t%5.2f\t%6.2f\t",
2852fa9e4066Sahrens 				    csize, lsize, psize, asize, avg,
2853fa9e4066Sahrens 				    (double)zb->zb_lsize / zb->zb_psize,
2854fa9e4066Sahrens 				    100.0 * zb->zb_asize / tzb->zb_asize);
2855fa9e4066Sahrens 
2856fa9e4066Sahrens 				if (level == ZB_TOTAL)
2857fa9e4066Sahrens 					(void) printf("%s\n", typename);
2858fa9e4066Sahrens 				else
2859fa9e4066Sahrens 					(void) printf("    L%d %s\n",
2860fa9e4066Sahrens 					    level, typename);
2861490d05b9SMatthew Ahrens 
2862d5ee8a13SMatthew Ahrens 				if (dump_opt['b'] >= 3 && zb->zb_gangs > 0) {
2863d5ee8a13SMatthew Ahrens 					(void) printf("\t number of ganged "
2864d5ee8a13SMatthew Ahrens 					    "blocks: %s\n", gang);
2865d5ee8a13SMatthew Ahrens 				}
2866d5ee8a13SMatthew Ahrens 
2867490d05b9SMatthew Ahrens 				if (dump_opt['b'] >= 4) {
2868490d05b9SMatthew Ahrens 					(void) printf("psize "
2869490d05b9SMatthew Ahrens 					    "(in 512-byte sectors): "
2870490d05b9SMatthew Ahrens 					    "number of blocks\n");
2871490d05b9SMatthew Ahrens 					dump_histogram(zb->zb_psize_histogram,
28720713e232SGeorge Wilson 					    PSIZE_HISTO_SIZE, 0);
2873490d05b9SMatthew Ahrens 				}
2874fa9e4066Sahrens 			}
2875fa9e4066Sahrens 		}
2876fa9e4066Sahrens 	}
2877fa9e4066Sahrens 
2878fa9e4066Sahrens 	(void) printf("\n");
2879fa9e4066Sahrens 
2880fa9e4066Sahrens 	if (leaks)
2881fa9e4066Sahrens 		return (2);
2882fa9e4066Sahrens 
2883fa9e4066Sahrens 	if (zcb.zcb_haderrors)
2884fa9e4066Sahrens 		return (3);
2885fa9e4066Sahrens 
2886fa9e4066Sahrens 	return (0);
2887fa9e4066Sahrens }
2888fa9e4066Sahrens 
2889b24ab676SJeff Bonwick typedef struct zdb_ddt_entry {
2890b24ab676SJeff Bonwick 	ddt_key_t	zdde_key;
2891b24ab676SJeff Bonwick 	uint64_t	zdde_ref_blocks;
2892b24ab676SJeff Bonwick 	uint64_t	zdde_ref_lsize;
2893b24ab676SJeff Bonwick 	uint64_t	zdde_ref_psize;
2894b24ab676SJeff Bonwick 	uint64_t	zdde_ref_dsize;
2895b24ab676SJeff Bonwick 	avl_node_t	zdde_node;
2896b24ab676SJeff Bonwick } zdb_ddt_entry_t;
2897b24ab676SJeff Bonwick 
2898b24ab676SJeff Bonwick /* ARGSUSED */
2899b24ab676SJeff Bonwick static int
2900b24ab676SJeff Bonwick zdb_ddt_add_cb(spa_t *spa, zilog_t *zilog, const blkptr_t *bp,
29017802d7bfSMatthew Ahrens     const zbookmark_phys_t *zb, const dnode_phys_t *dnp, void *arg)
2902b24ab676SJeff Bonwick {
2903b24ab676SJeff Bonwick 	avl_tree_t *t = arg;
2904b24ab676SJeff Bonwick 	avl_index_t where;
2905b24ab676SJeff Bonwick 	zdb_ddt_entry_t *zdde, zdde_search;
2906b24ab676SJeff Bonwick 
2907a2cdcdd2SPaul Dagnelie 	if (bp == NULL || BP_IS_HOLE(bp) || BP_IS_EMBEDDED(bp))
2908b24ab676SJeff Bonwick 		return (0);
2909b24ab676SJeff Bonwick 
2910b24ab676SJeff Bonwick 	if (dump_opt['S'] > 1 && zb->zb_level == ZB_ROOT_LEVEL) {
2911b24ab676SJeff Bonwick 		(void) printf("traversing objset %llu, %llu objects, "
2912b24ab676SJeff Bonwick 		    "%lu blocks so far\n",
2913b24ab676SJeff Bonwick 		    (u_longlong_t)zb->zb_objset,
29145d7b4d43SMatthew Ahrens 		    (u_longlong_t)BP_GET_FILL(bp),
2915b24ab676SJeff Bonwick 		    avl_numnodes(t));
2916b24ab676SJeff Bonwick 	}
2917b24ab676SJeff Bonwick 
2918bbfd46c4SJeff Bonwick 	if (BP_IS_HOLE(bp) || BP_GET_CHECKSUM(bp) == ZIO_CHECKSUM_OFF ||
2919ad135b5dSChristopher Siden 	    BP_GET_LEVEL(bp) > 0 || DMU_OT_IS_METADATA(BP_GET_TYPE(bp)))
2920b24ab676SJeff Bonwick 		return (0);
2921b24ab676SJeff Bonwick 
2922b24ab676SJeff Bonwick 	ddt_key_fill(&zdde_search.zdde_key, bp);
2923b24ab676SJeff Bonwick 
2924b24ab676SJeff Bonwick 	zdde = avl_find(t, &zdde_search, &where);
2925b24ab676SJeff Bonwick 
2926b24ab676SJeff Bonwick 	if (zdde == NULL) {
2927b24ab676SJeff Bonwick 		zdde = umem_zalloc(sizeof (*zdde), UMEM_NOFAIL);
2928b24ab676SJeff Bonwick 		zdde->zdde_key = zdde_search.zdde_key;
2929b24ab676SJeff Bonwick 		avl_insert(t, zdde, where);
2930b24ab676SJeff Bonwick 	}
2931b24ab676SJeff Bonwick 
2932b24ab676SJeff Bonwick 	zdde->zdde_ref_blocks += 1;
2933b24ab676SJeff Bonwick 	zdde->zdde_ref_lsize += BP_GET_LSIZE(bp);
2934b24ab676SJeff Bonwick 	zdde->zdde_ref_psize += BP_GET_PSIZE(bp);
2935b24ab676SJeff Bonwick 	zdde->zdde_ref_dsize += bp_get_dsize_sync(spa, bp);
2936b24ab676SJeff Bonwick 
2937b24ab676SJeff Bonwick 	return (0);
2938b24ab676SJeff Bonwick }
2939b24ab676SJeff Bonwick 
2940b24ab676SJeff Bonwick static void
2941b24ab676SJeff Bonwick dump_simulated_ddt(spa_t *spa)
2942b24ab676SJeff Bonwick {
2943b24ab676SJeff Bonwick 	avl_tree_t t;
2944b24ab676SJeff Bonwick 	void *cookie = NULL;
2945b24ab676SJeff Bonwick 	zdb_ddt_entry_t *zdde;
2946b24ab676SJeff Bonwick 	ddt_histogram_t ddh_total = { 0 };
2947b24ab676SJeff Bonwick 	ddt_stat_t dds_total = { 0 };
2948b24ab676SJeff Bonwick 
2949b24ab676SJeff Bonwick 	avl_create(&t, ddt_entry_compare,
2950b24ab676SJeff Bonwick 	    sizeof (zdb_ddt_entry_t), offsetof(zdb_ddt_entry_t, zdde_node));
2951b24ab676SJeff Bonwick 
2952b24ab676SJeff Bonwick 	spa_config_enter(spa, SCL_CONFIG, FTAG, RW_READER);
2953b24ab676SJeff Bonwick 
2954bbfd46c4SJeff Bonwick 	(void) traverse_pool(spa, 0, TRAVERSE_PRE | TRAVERSE_PREFETCH_METADATA,
2955bbfd46c4SJeff Bonwick 	    zdb_ddt_add_cb, &t);
2956b24ab676SJeff Bonwick 
2957b24ab676SJeff Bonwick 	spa_config_exit(spa, SCL_CONFIG, FTAG);
2958b24ab676SJeff Bonwick 
2959b24ab676SJeff Bonwick 	while ((zdde = avl_destroy_nodes(&t, &cookie)) != NULL) {
2960b24ab676SJeff Bonwick 		ddt_stat_t dds;
2961b24ab676SJeff Bonwick 		uint64_t refcnt = zdde->zdde_ref_blocks;
2962b24ab676SJeff Bonwick 		ASSERT(refcnt != 0);
2963b24ab676SJeff Bonwick 
2964b24ab676SJeff Bonwick 		dds.dds_blocks = zdde->zdde_ref_blocks / refcnt;
2965b24ab676SJeff Bonwick 		dds.dds_lsize = zdde->zdde_ref_lsize / refcnt;
2966b24ab676SJeff Bonwick 		dds.dds_psize = zdde->zdde_ref_psize / refcnt;
2967b24ab676SJeff Bonwick 		dds.dds_dsize = zdde->zdde_ref_dsize / refcnt;
2968b24ab676SJeff Bonwick 
2969b24ab676SJeff Bonwick 		dds.dds_ref_blocks = zdde->zdde_ref_blocks;
2970b24ab676SJeff Bonwick 		dds.dds_ref_lsize = zdde->zdde_ref_lsize;
2971b24ab676SJeff Bonwick 		dds.dds_ref_psize = zdde->zdde_ref_psize;
2972b24ab676SJeff Bonwick 		dds.dds_ref_dsize = zdde->zdde_ref_dsize;
2973b24ab676SJeff Bonwick 
2974bf16b11eSMatthew Ahrens 		ddt_stat_add(&ddh_total.ddh_stat[highbit64(refcnt) - 1],
2975bf16b11eSMatthew Ahrens 		    &dds, 0);
2976b24ab676SJeff Bonwick 
2977b24ab676SJeff Bonwick 		umem_free(zdde, sizeof (*zdde));
2978b24ab676SJeff Bonwick 	}
2979b24ab676SJeff Bonwick 
2980b24ab676SJeff Bonwick 	avl_destroy(&t);
2981b24ab676SJeff Bonwick 
2982b24ab676SJeff Bonwick 	ddt_histogram_stat(&dds_total, &ddh_total);
2983b24ab676SJeff Bonwick 
2984b24ab676SJeff Bonwick 	(void) printf("Simulated DDT histogram:\n");
2985b24ab676SJeff Bonwick 
29869eb19f4dSGeorge Wilson 	zpool_dump_ddt(&dds_total, &ddh_total);
2987b24ab676SJeff Bonwick 
2988b24ab676SJeff Bonwick 	dump_dedup_ratio(&dds_total);
2989b24ab676SJeff Bonwick }
2990b24ab676SJeff Bonwick 
2991fa9e4066Sahrens static void
2992fa9e4066Sahrens dump_zpool(spa_t *spa)
2993fa9e4066Sahrens {
2994fa9e4066Sahrens 	dsl_pool_t *dp = spa_get_dsl(spa);
2995fa9e4066Sahrens 	int rc = 0;
2996fa9e4066Sahrens 
2997b24ab676SJeff Bonwick 	if (dump_opt['S']) {
2998b24ab676SJeff Bonwick 		dump_simulated_ddt(spa);
2999b24ab676SJeff Bonwick 		return;
3000b24ab676SJeff Bonwick 	}
3001b24ab676SJeff Bonwick 
300207428bdfSVictor Latushkin 	if (!dump_opt['e'] && dump_opt['C'] > 1) {
300307428bdfSVictor Latushkin 		(void) printf("\nCached configuration:\n");
300407428bdfSVictor Latushkin 		dump_nvlist(spa->spa_config, 8);
300507428bdfSVictor Latushkin 	}
300607428bdfSVictor Latushkin 
300707428bdfSVictor Latushkin 	if (dump_opt['C'])
300807428bdfSVictor Latushkin 		dump_config(spa);
300907428bdfSVictor Latushkin 
3010fa9e4066Sahrens 	if (dump_opt['u'])
301153b9a4a9SVictor Latushkin 		dump_uberblock(&spa->spa_uberblock, "\nUberblock:\n", "\n");
3012fa9e4066Sahrens 
3013b24ab676SJeff Bonwick 	if (dump_opt['D'])
3014b24ab676SJeff Bonwick 		dump_all_ddts(spa);
3015b24ab676SJeff Bonwick 
301687219db7SVictor Latushkin 	if (dump_opt['d'] > 2 || dump_opt['m'])
301787219db7SVictor Latushkin 		dump_metaslabs(spa);
30182e4c9986SGeorge Wilson 	if (dump_opt['M'])
30192e4c9986SGeorge Wilson 		dump_metaslab_groups(spa);
302087219db7SVictor Latushkin 
302187219db7SVictor Latushkin 	if (dump_opt['d'] || dump_opt['i']) {
3022fa9e4066Sahrens 		dump_dir(dp->dp_meta_objset);
3023fa9e4066Sahrens 		if (dump_opt['d'] >= 3) {
3024732885fcSMatthew Ahrens 			dump_full_bpobj(&spa->spa_deferred_bpobj,
3025d0475637SMatthew Ahrens 			    "Deferred frees", 0);
3026cde58dbcSMatthew Ahrens 			if (spa_version(spa) >= SPA_VERSION_DEADLISTS) {
3027732885fcSMatthew Ahrens 				dump_full_bpobj(
3028732885fcSMatthew Ahrens 				    &spa->spa_dsl_pool->dp_free_bpobj,
3029d0475637SMatthew Ahrens 				    "Pool snapshot frees", 0);
3030ad135b5dSChristopher Siden 			}
3031ad135b5dSChristopher Siden 
3032ad135b5dSChristopher Siden 			if (spa_feature_is_active(spa,
30332acef22dSMatthew Ahrens 			    SPA_FEATURE_ASYNC_DESTROY)) {
3034ad135b5dSChristopher Siden 				dump_bptree(spa->spa_meta_objset,
3035ad135b5dSChristopher Siden 				    spa->spa_dsl_pool->dp_bptree_obj,
3036ad135b5dSChristopher Siden 				    "Pool dataset frees");
3037cde58dbcSMatthew Ahrens 			}
3038fa9e4066Sahrens 			dump_dtl(spa->spa_root_vdev, 0);
3039fa9e4066Sahrens 		}
304007428bdfSVictor Latushkin 		(void) dmu_objset_find(spa_name(spa), dump_one_dir,
304107428bdfSVictor Latushkin 		    NULL, DS_FIND_SNAPSHOTS | DS_FIND_CHILDREN);
3042b5152584SMatthew Ahrens 
3043ca0cc391SMatthew Ahrens 		for (spa_feature_t f = 0; f < SPA_FEATURES; f++) {
3044ca0cc391SMatthew Ahrens 			uint64_t refcount;
3045ca0cc391SMatthew Ahrens 
3046ca0cc391SMatthew Ahrens 			if (!(spa_feature_table[f].fi_flags &
3047ca0cc391SMatthew Ahrens 			    ZFEATURE_FLAG_PER_DATASET)) {
3048ca0cc391SMatthew Ahrens 				ASSERT0(dataset_feature_count[f]);
3049ca0cc391SMatthew Ahrens 				continue;
3050ca0cc391SMatthew Ahrens 			}
3051ca0cc391SMatthew Ahrens 			(void) feature_get_refcount(spa,
3052ca0cc391SMatthew Ahrens 			    &spa_feature_table[f], &refcount);
3053ca0cc391SMatthew Ahrens 			if (dataset_feature_count[f] != refcount) {
3054ca0cc391SMatthew Ahrens 				(void) printf("%s feature refcount mismatch: "
3055ca0cc391SMatthew Ahrens 				    "%lld datasets != %lld refcount\n",
3056ca0cc391SMatthew Ahrens 				    spa_feature_table[f].fi_uname,
3057ca0cc391SMatthew Ahrens 				    (longlong_t)dataset_feature_count[f],
3058ca0cc391SMatthew Ahrens 				    (longlong_t)refcount);
3059ca0cc391SMatthew Ahrens 				rc = 2;
3060ca0cc391SMatthew Ahrens 			} else {
3061ca0cc391SMatthew Ahrens 				(void) printf("Verified %s feature refcount "
3062ca0cc391SMatthew Ahrens 				    "of %llu is correct\n",
3063ca0cc391SMatthew Ahrens 				    spa_feature_table[f].fi_uname,
3064ca0cc391SMatthew Ahrens 				    (longlong_t)refcount);
3065ca0cc391SMatthew Ahrens 			}
3066b5152584SMatthew Ahrens 		}
3067fa9e4066Sahrens 	}
3068b5152584SMatthew Ahrens 	if (rc == 0 && (dump_opt['b'] || dump_opt['c']))
3069fa9e4066Sahrens 		rc = dump_block_stats(spa);
3070fa9e4066Sahrens 
30710713e232SGeorge Wilson 	if (rc == 0)
30720713e232SGeorge Wilson 		rc = verify_spacemap_refcounts(spa);
30730713e232SGeorge Wilson 
3074fa9e4066Sahrens 	if (dump_opt['s'])
3075fa9e4066Sahrens 		show_pool_stats(spa);
3076fa9e4066Sahrens 
30778f18d1faSGeorge Wilson 	if (dump_opt['h'])
30788f18d1faSGeorge Wilson 		dump_history(spa);
30798f18d1faSGeorge Wilson 
3080fa9e4066Sahrens 	if (rc != 0)
3081fa9e4066Sahrens 		exit(rc);
3082fa9e4066Sahrens }
3083fa9e4066Sahrens 
308444cd46caSbillm #define	ZDB_FLAG_CHECKSUM	0x0001
308544cd46caSbillm #define	ZDB_FLAG_DECOMPRESS	0x0002
308644cd46caSbillm #define	ZDB_FLAG_BSWAP		0x0004
308744cd46caSbillm #define	ZDB_FLAG_GBH		0x0008
308844cd46caSbillm #define	ZDB_FLAG_INDIRECT	0x0010
308944cd46caSbillm #define	ZDB_FLAG_PHYS		0x0020
309044cd46caSbillm #define	ZDB_FLAG_RAW		0x0040
309144cd46caSbillm #define	ZDB_FLAG_PRINT_BLKPTR	0x0080
309244cd46caSbillm 
309344cd46caSbillm int flagbits[256];
309444cd46caSbillm 
309544cd46caSbillm static void
309644cd46caSbillm zdb_print_blkptr(blkptr_t *bp, int flags)
309744cd46caSbillm {
3098b24ab676SJeff Bonwick 	char blkbuf[BP_SPRINTF_LEN];
309944cd46caSbillm 
310044cd46caSbillm 	if (flags & ZDB_FLAG_BSWAP)
310144cd46caSbillm 		byteswap_uint64_array((void *)bp, sizeof (blkptr_t));
3102b24ab676SJeff Bonwick 
310343466aaeSMax Grossman 	snprintf_blkptr(blkbuf, sizeof (blkbuf), bp);
3104b24ab676SJeff Bonwick 	(void) printf("%s\n", blkbuf);
310544cd46caSbillm }
310644cd46caSbillm 
310744cd46caSbillm static void
310844cd46caSbillm zdb_dump_indirect(blkptr_t *bp, int nbps, int flags)
310944cd46caSbillm {
311044cd46caSbillm 	int i;
311144cd46caSbillm 
311244cd46caSbillm 	for (i = 0; i < nbps; i++)
311344cd46caSbillm 		zdb_print_blkptr(&bp[i], flags);
311444cd46caSbillm }
311544cd46caSbillm 
311644cd46caSbillm static void
311744cd46caSbillm zdb_dump_gbh(void *buf, int flags)
311844cd46caSbillm {
311944cd46caSbillm 	zdb_dump_indirect((blkptr_t *)buf, SPA_GBH_NBLKPTRS, flags);
312044cd46caSbillm }
312144cd46caSbillm 
312244cd46caSbillm static void
312344cd46caSbillm zdb_dump_block_raw(void *buf, uint64_t size, int flags)
312444cd46caSbillm {
312544cd46caSbillm 	if (flags & ZDB_FLAG_BSWAP)
312644cd46caSbillm 		byteswap_uint64_array(buf, size);
3127b24ab676SJeff Bonwick 	(void) write(1, buf, size);
312844cd46caSbillm }
312944cd46caSbillm 
313044cd46caSbillm static void
313144cd46caSbillm zdb_dump_block(char *label, void *buf, uint64_t size, int flags)
313244cd46caSbillm {
313344cd46caSbillm 	uint64_t *d = (uint64_t *)buf;
313444cd46caSbillm 	int nwords = size / sizeof (uint64_t);
313544cd46caSbillm 	int do_bswap = !!(flags & ZDB_FLAG_BSWAP);
313644cd46caSbillm 	int i, j;
313744cd46caSbillm 	char *hdr, *c;
313844cd46caSbillm 
313944cd46caSbillm 
314044cd46caSbillm 	if (do_bswap)
314144cd46caSbillm 		hdr = " 7 6 5 4 3 2 1 0   f e d c b a 9 8";
314244cd46caSbillm 	else
314344cd46caSbillm 		hdr = " 0 1 2 3 4 5 6 7   8 9 a b c d e f";
314444cd46caSbillm 
314544cd46caSbillm 	(void) printf("\n%s\n%6s   %s  0123456789abcdef\n", label, "", hdr);
314644cd46caSbillm 
314744cd46caSbillm 	for (i = 0; i < nwords; i += 2) {
314844cd46caSbillm 		(void) printf("%06llx:  %016llx  %016llx  ",
314944cd46caSbillm 		    (u_longlong_t)(i * sizeof (uint64_t)),
315044cd46caSbillm 		    (u_longlong_t)(do_bswap ? BSWAP_64(d[i]) : d[i]),
315144cd46caSbillm 		    (u_longlong_t)(do_bswap ? BSWAP_64(d[i + 1]) : d[i + 1]));
315244cd46caSbillm 
315344cd46caSbillm 		c = (char *)&d[i];
315444cd46caSbillm 		for (j = 0; j < 2 * sizeof (uint64_t); j++)
315544cd46caSbillm 			(void) printf("%c", isprint(c[j]) ? c[j] : '.');
315644cd46caSbillm 		(void) printf("\n");
315744cd46caSbillm 	}
315844cd46caSbillm }
315944cd46caSbillm 
316044cd46caSbillm /*
316144cd46caSbillm  * There are two acceptable formats:
316244cd46caSbillm  *	leaf_name	  - For example: c1t0d0 or /tmp/ztest.0a
316344cd46caSbillm  *	child[.child]*    - For example: 0.1.1
316444cd46caSbillm  *
316544cd46caSbillm  * The second form can be used to specify arbitrary vdevs anywhere
316644cd46caSbillm  * in the heirarchy.  For example, in a pool with a mirror of
316744cd46caSbillm  * RAID-Zs, you can specify either RAID-Z vdev with 0.0 or 0.1 .
316844cd46caSbillm  */
316944cd46caSbillm static vdev_t *
317044cd46caSbillm zdb_vdev_lookup(vdev_t *vdev, char *path)
317144cd46caSbillm {
317244cd46caSbillm 	char *s, *p, *q;
317344cd46caSbillm 	int i;
317444cd46caSbillm 
317544cd46caSbillm 	if (vdev == NULL)
317644cd46caSbillm 		return (NULL);
317744cd46caSbillm 
317844cd46caSbillm 	/* First, assume the x.x.x.x format */
317944cd46caSbillm 	i = (int)strtoul(path, &s, 10);
318044cd46caSbillm 	if (s == path || (s && *s != '.' && *s != '\0'))
318144cd46caSbillm 		goto name;
318244cd46caSbillm 	if (i < 0 || i >= vdev->vdev_children)
318344cd46caSbillm 		return (NULL);
318444cd46caSbillm 
318544cd46caSbillm 	vdev = vdev->vdev_child[i];
318644cd46caSbillm 	if (*s == '\0')
318744cd46caSbillm 		return (vdev);
318844cd46caSbillm 	return (zdb_vdev_lookup(vdev, s+1));
318944cd46caSbillm 
319044cd46caSbillm name:
319144cd46caSbillm 	for (i = 0; i < vdev->vdev_children; i++) {
319244cd46caSbillm 		vdev_t *vc = vdev->vdev_child[i];
319344cd46caSbillm 
319444cd46caSbillm 		if (vc->vdev_path == NULL) {
319544cd46caSbillm 			vc = zdb_vdev_lookup(vc, path);
319644cd46caSbillm 			if (vc == NULL)
319744cd46caSbillm 				continue;
319844cd46caSbillm 			else
319944cd46caSbillm 				return (vc);
320044cd46caSbillm 		}
320144cd46caSbillm 
320244cd46caSbillm 		p = strrchr(vc->vdev_path, '/');
320344cd46caSbillm 		p = p ? p + 1 : vc->vdev_path;
320444cd46caSbillm 		q = &vc->vdev_path[strlen(vc->vdev_path) - 2];
320544cd46caSbillm 
320644cd46caSbillm 		if (strcmp(vc->vdev_path, path) == 0)
320744cd46caSbillm 			return (vc);
320844cd46caSbillm 		if (strcmp(p, path) == 0)
320944cd46caSbillm 			return (vc);
321044cd46caSbillm 		if (strcmp(q, "s0") == 0 && strncmp(p, path, q - p) == 0)
321144cd46caSbillm 			return (vc);
321244cd46caSbillm 	}
321344cd46caSbillm 
321444cd46caSbillm 	return (NULL);
321544cd46caSbillm }
321644cd46caSbillm 
321744cd46caSbillm /*
321844cd46caSbillm  * Read a block from a pool and print it out.  The syntax of the
321944cd46caSbillm  * block descriptor is:
322044cd46caSbillm  *
322144cd46caSbillm  *	pool:vdev_specifier:offset:size[:flags]
322244cd46caSbillm  *
322344cd46caSbillm  *	pool           - The name of the pool you wish to read from
322444cd46caSbillm  *	vdev_specifier - Which vdev (see comment for zdb_vdev_lookup)
322544cd46caSbillm  *	offset         - offset, in hex, in bytes
322644cd46caSbillm  *	size           - Amount of data to read, in hex, in bytes
322744cd46caSbillm  *	flags          - A string of characters specifying options
322844cd46caSbillm  *		 b: Decode a blkptr at given offset within block
322944cd46caSbillm  *		*c: Calculate and display checksums
3230b24ab676SJeff Bonwick  *		 d: Decompress data before dumping
323144cd46caSbillm  *		 e: Byteswap data before dumping
3232b24ab676SJeff Bonwick  *		 g: Display data as a gang block header
3233b24ab676SJeff Bonwick  *		 i: Display as an indirect block
323444cd46caSbillm  *		 p: Do I/O to physical offset
323544cd46caSbillm  *		 r: Dump raw data to stdout
323644cd46caSbillm  *
323744cd46caSbillm  *              * = not yet implemented
323844cd46caSbillm  */
323944cd46caSbillm static void
324007428bdfSVictor Latushkin zdb_read_block(char *thing, spa_t *spa)
324144cd46caSbillm {
3242b24ab676SJeff Bonwick 	blkptr_t blk, *bp = &blk;
3243b24ab676SJeff Bonwick 	dva_t *dva = bp->blk_dva;
324444cd46caSbillm 	int flags = 0;
3245b24ab676SJeff Bonwick 	uint64_t offset = 0, size = 0, psize = 0, lsize = 0, blkptr_offset = 0;
324644cd46caSbillm 	zio_t *zio;
324744cd46caSbillm 	vdev_t *vd;
3248b24ab676SJeff Bonwick 	void *pbuf, *lbuf, *buf;
324907428bdfSVictor Latushkin 	char *s, *p, *dup, *vdev, *flagstr;
3250b24ab676SJeff Bonwick 	int i, error;
325144cd46caSbillm 
325244cd46caSbillm 	dup = strdup(thing);
325344cd46caSbillm 	s = strtok(dup, ":");
325444cd46caSbillm 	vdev = s ? s : "";
325544cd46caSbillm 	s = strtok(NULL, ":");
325644cd46caSbillm 	offset = strtoull(s ? s : "", NULL, 16);
325744cd46caSbillm 	s = strtok(NULL, ":");
325844cd46caSbillm 	size = strtoull(s ? s : "", NULL, 16);
325944cd46caSbillm 	s = strtok(NULL, ":");
326044cd46caSbillm 	flagstr = s ? s : "";
326144cd46caSbillm 
326244cd46caSbillm 	s = NULL;
326344cd46caSbillm 	if (size == 0)
326444cd46caSbillm 		s = "size must not be zero";
326544cd46caSbillm 	if (!IS_P2ALIGNED(size, DEV_BSIZE))
326644cd46caSbillm 		s = "size must be a multiple of sector size";
326744cd46caSbillm 	if (!IS_P2ALIGNED(offset, DEV_BSIZE))
326844cd46caSbillm 		s = "offset must be a multiple of sector size";
326944cd46caSbillm 	if (s) {
327044cd46caSbillm 		(void) printf("Invalid block specifier: %s  - %s\n", thing, s);
327144cd46caSbillm 		free(dup);
327244cd46caSbillm 		return;
327344cd46caSbillm 	}
327444cd46caSbillm 
327544cd46caSbillm 	for (s = strtok(flagstr, ":"); s; s = strtok(NULL, ":")) {
327644cd46caSbillm 		for (i = 0; flagstr[i]; i++) {
32775ad82045Snd 			int bit = flagbits[(uchar_t)flagstr[i]];
327844cd46caSbillm 
327944cd46caSbillm 			if (bit == 0) {
328044cd46caSbillm 				(void) printf("***Invalid flag: %c\n",
328144cd46caSbillm 				    flagstr[i]);
328244cd46caSbillm 				continue;
328344cd46caSbillm 			}
328444cd46caSbillm 			flags |= bit;
328544cd46caSbillm 
328644cd46caSbillm 			/* If it's not something with an argument, keep going */
3287b24ab676SJeff Bonwick 			if ((bit & (ZDB_FLAG_CHECKSUM |
328844cd46caSbillm 			    ZDB_FLAG_PRINT_BLKPTR)) == 0)
328944cd46caSbillm 				continue;
329044cd46caSbillm 
329144cd46caSbillm 			p = &flagstr[i + 1];
329244cd46caSbillm 			if (bit == ZDB_FLAG_PRINT_BLKPTR)
329344cd46caSbillm 				blkptr_offset = strtoull(p, &p, 16);
329444cd46caSbillm 			if (*p != ':' && *p != '\0') {
329544cd46caSbillm 				(void) printf("***Invalid flag arg: '%s'\n", s);
329644cd46caSbillm 				free(dup);
329744cd46caSbillm 				return;
329844cd46caSbillm 			}
329944cd46caSbillm 		}
330044cd46caSbillm 	}
330144cd46caSbillm 
330244cd46caSbillm 	vd = zdb_vdev_lookup(spa->spa_root_vdev, vdev);
330344cd46caSbillm 	if (vd == NULL) {
330444cd46caSbillm 		(void) printf("***Invalid vdev: %s\n", vdev);
330544cd46caSbillm 		free(dup);
330644cd46caSbillm 		return;
330744cd46caSbillm 	} else {
330844cd46caSbillm 		if (vd->vdev_path)
3309b24ab676SJeff Bonwick 			(void) fprintf(stderr, "Found vdev: %s\n",
3310b24ab676SJeff Bonwick 			    vd->vdev_path);
331144cd46caSbillm 		else
3312b24ab676SJeff Bonwick 			(void) fprintf(stderr, "Found vdev type: %s\n",
331344cd46caSbillm 			    vd->vdev_ops->vdev_op_type);
331444cd46caSbillm 	}
331544cd46caSbillm 
3316b24ab676SJeff Bonwick 	psize = size;
3317b24ab676SJeff Bonwick 	lsize = size;
331844cd46caSbillm 
3319b24ab676SJeff Bonwick 	pbuf = umem_alloc(SPA_MAXBLOCKSIZE, UMEM_NOFAIL);
3320b24ab676SJeff Bonwick 	lbuf = umem_alloc(SPA_MAXBLOCKSIZE, UMEM_NOFAIL);
3321b24ab676SJeff Bonwick 
3322b24ab676SJeff Bonwick 	BP_ZERO(bp);
3323b24ab676SJeff Bonwick 
3324b24ab676SJeff Bonwick 	DVA_SET_VDEV(&dva[0], vd->vdev_id);
3325b24ab676SJeff Bonwick 	DVA_SET_OFFSET(&dva[0], offset);
3326b24ab676SJeff Bonwick 	DVA_SET_GANG(&dva[0], !!(flags & ZDB_FLAG_GBH));
3327b24ab676SJeff Bonwick 	DVA_SET_ASIZE(&dva[0], vdev_psize_to_asize(vd, psize));
3328b24ab676SJeff Bonwick 
3329b24ab676SJeff Bonwick 	BP_SET_BIRTH(bp, TXG_INITIAL, TXG_INITIAL);
3330b24ab676SJeff Bonwick 
3331b24ab676SJeff Bonwick 	BP_SET_LSIZE(bp, lsize);
3332b24ab676SJeff Bonwick 	BP_SET_PSIZE(bp, psize);
3333b24ab676SJeff Bonwick 	BP_SET_COMPRESS(bp, ZIO_COMPRESS_OFF);
3334b24ab676SJeff Bonwick 	BP_SET_CHECKSUM(bp, ZIO_CHECKSUM_OFF);
3335b24ab676SJeff Bonwick 	BP_SET_TYPE(bp, DMU_OT_NONE);
3336b24ab676SJeff Bonwick 	BP_SET_LEVEL(bp, 0);
3337b24ab676SJeff Bonwick 	BP_SET_DEDUP(bp, 0);
3338b24ab676SJeff Bonwick 	BP_SET_BYTEORDER(bp, ZFS_HOST_BYTEORDER);
333944cd46caSbillm 
3340e14bb325SJeff Bonwick 	spa_config_enter(spa, SCL_STATE, FTAG, RW_READER);
334144cd46caSbillm 	zio = zio_root(spa, NULL, NULL, 0);
3342b24ab676SJeff Bonwick 
3343b24ab676SJeff Bonwick 	if (vd == vd->vdev_top) {
3344b24ab676SJeff Bonwick 		/*
3345b24ab676SJeff Bonwick 		 * Treat this as a normal block read.
3346b24ab676SJeff Bonwick 		 */
3347b24ab676SJeff Bonwick 		zio_nowait(zio_read(zio, spa, bp, pbuf, psize, NULL, NULL,
3348b24ab676SJeff Bonwick 		    ZIO_PRIORITY_SYNC_READ,
3349b24ab676SJeff Bonwick 		    ZIO_FLAG_CANFAIL | ZIO_FLAG_RAW, NULL));
3350b24ab676SJeff Bonwick 	} else {
3351b24ab676SJeff Bonwick 		/*
3352b24ab676SJeff Bonwick 		 * Treat this as a vdev child I/O.
3353b24ab676SJeff Bonwick 		 */
3354b24ab676SJeff Bonwick 		zio_nowait(zio_vdev_child_io(zio, bp, vd, offset, pbuf, psize,
3355b24ab676SJeff Bonwick 		    ZIO_TYPE_READ, ZIO_PRIORITY_SYNC_READ,
3356b24ab676SJeff Bonwick 		    ZIO_FLAG_DONT_CACHE | ZIO_FLAG_DONT_QUEUE |
3357b24ab676SJeff Bonwick 		    ZIO_FLAG_DONT_PROPAGATE | ZIO_FLAG_DONT_RETRY |
3358b24ab676SJeff Bonwick 		    ZIO_FLAG_CANFAIL | ZIO_FLAG_RAW, NULL, NULL));
3359b24ab676SJeff Bonwick 	}
3360b24ab676SJeff Bonwick 
336144cd46caSbillm 	error = zio_wait(zio);
3362e14bb325SJeff Bonwick 	spa_config_exit(spa, SCL_STATE, FTAG);
336344cd46caSbillm 
336444cd46caSbillm 	if (error) {
336544cd46caSbillm 		(void) printf("Read of %s failed, error: %d\n", thing, error);
336644cd46caSbillm 		goto out;
336744cd46caSbillm 	}
336844cd46caSbillm 
3369b24ab676SJeff Bonwick 	if (flags & ZDB_FLAG_DECOMPRESS) {
3370b24ab676SJeff Bonwick 		/*
3371b24ab676SJeff Bonwick 		 * We don't know how the data was compressed, so just try
3372b24ab676SJeff Bonwick 		 * every decompress function at every inflated blocksize.
3373b24ab676SJeff Bonwick 		 */
3374b24ab676SJeff Bonwick 		enum zio_compress c;
3375b24ab676SJeff Bonwick 		void *pbuf2 = umem_alloc(SPA_MAXBLOCKSIZE, UMEM_NOFAIL);
3376b24ab676SJeff Bonwick 		void *lbuf2 = umem_alloc(SPA_MAXBLOCKSIZE, UMEM_NOFAIL);
3377b24ab676SJeff Bonwick 
3378b24ab676SJeff Bonwick 		bcopy(pbuf, pbuf2, psize);
3379b24ab676SJeff Bonwick 
3380b24ab676SJeff Bonwick 		VERIFY(random_get_pseudo_bytes((uint8_t *)pbuf + psize,
3381b24ab676SJeff Bonwick 		    SPA_MAXBLOCKSIZE - psize) == 0);
3382b24ab676SJeff Bonwick 
3383b24ab676SJeff Bonwick 		VERIFY(random_get_pseudo_bytes((uint8_t *)pbuf2 + psize,
3384b24ab676SJeff Bonwick 		    SPA_MAXBLOCKSIZE - psize) == 0);
3385b24ab676SJeff Bonwick 
3386b24ab676SJeff Bonwick 		for (lsize = SPA_MAXBLOCKSIZE; lsize > psize;
3387b24ab676SJeff Bonwick 		    lsize -= SPA_MINBLOCKSIZE) {
3388b24ab676SJeff Bonwick 			for (c = 0; c < ZIO_COMPRESS_FUNCTIONS; c++) {
3389b24ab676SJeff Bonwick 				if (zio_decompress_data(c, pbuf, lbuf,
3390b24ab676SJeff Bonwick 				    psize, lsize) == 0 &&
3391b24ab676SJeff Bonwick 				    zio_decompress_data(c, pbuf2, lbuf2,
3392b24ab676SJeff Bonwick 				    psize, lsize) == 0 &&
3393b24ab676SJeff Bonwick 				    bcmp(lbuf, lbuf2, lsize) == 0)
3394b24ab676SJeff Bonwick 					break;
3395b24ab676SJeff Bonwick 			}
3396b24ab676SJeff Bonwick 			if (c != ZIO_COMPRESS_FUNCTIONS)
3397b24ab676SJeff Bonwick 				break;
3398b24ab676SJeff Bonwick 			lsize -= SPA_MINBLOCKSIZE;
3399b24ab676SJeff Bonwick 		}
3400b24ab676SJeff Bonwick 
3401b24ab676SJeff Bonwick 		umem_free(pbuf2, SPA_MAXBLOCKSIZE);
3402b24ab676SJeff Bonwick 		umem_free(lbuf2, SPA_MAXBLOCKSIZE);
3403b24ab676SJeff Bonwick 
3404b24ab676SJeff Bonwick 		if (lsize <= psize) {
3405b24ab676SJeff Bonwick 			(void) printf("Decompress of %s failed\n", thing);
3406b24ab676SJeff Bonwick 			goto out;
3407b24ab676SJeff Bonwick 		}
3408b24ab676SJeff Bonwick 		buf = lbuf;
3409b24ab676SJeff Bonwick 		size = lsize;
3410b24ab676SJeff Bonwick 	} else {
3411b24ab676SJeff Bonwick 		buf = pbuf;
3412b24ab676SJeff Bonwick 		size = psize;
3413b24ab676SJeff Bonwick 	}
3414b24ab676SJeff Bonwick 
341544cd46caSbillm 	if (flags & ZDB_FLAG_PRINT_BLKPTR)
341644cd46caSbillm 		zdb_print_blkptr((blkptr_t *)(void *)
341744cd46caSbillm 		    ((uintptr_t)buf + (uintptr_t)blkptr_offset), flags);
341844cd46caSbillm 	else if (flags & ZDB_FLAG_RAW)
341944cd46caSbillm 		zdb_dump_block_raw(buf, size, flags);
342044cd46caSbillm 	else if (flags & ZDB_FLAG_INDIRECT)
342144cd46caSbillm 		zdb_dump_indirect((blkptr_t *)buf, size / sizeof (blkptr_t),
342244cd46caSbillm 		    flags);
342344cd46caSbillm 	else if (flags & ZDB_FLAG_GBH)
342444cd46caSbillm 		zdb_dump_gbh(buf, flags);
342544cd46caSbillm 	else
342644cd46caSbillm 		zdb_dump_block(thing, buf, size, flags);
342744cd46caSbillm 
342844cd46caSbillm out:
3429b24ab676SJeff Bonwick 	umem_free(pbuf, SPA_MAXBLOCKSIZE);
3430b24ab676SJeff Bonwick 	umem_free(lbuf, SPA_MAXBLOCKSIZE);
343144cd46caSbillm 	free(dup);
343244cd46caSbillm }
343344cd46caSbillm 
3434de6628f0Sck static boolean_t
34353ad6c7f9SVictor Latushkin pool_match(nvlist_t *cfg, char *tgt)
3436de6628f0Sck {
34373ad6c7f9SVictor Latushkin 	uint64_t v, guid = strtoull(tgt, NULL, 0);
3438de6628f0Sck 	char *s;
3439de6628f0Sck 
3440de6628f0Sck 	if (guid != 0) {
34413ad6c7f9SVictor Latushkin 		if (nvlist_lookup_uint64(cfg, ZPOOL_CONFIG_POOL_GUID, &v) == 0)
34423ad6c7f9SVictor Latushkin 			return (v == guid);
3443de6628f0Sck 	} else {
34443ad6c7f9SVictor Latushkin 		if (nvlist_lookup_string(cfg, ZPOOL_CONFIG_POOL_NAME, &s) == 0)
34453ad6c7f9SVictor Latushkin 			return (strcmp(s, tgt) == 0);
3446de6628f0Sck 	}
34473ad6c7f9SVictor Latushkin 	return (B_FALSE);
3448de6628f0Sck }
3449de6628f0Sck 
34503ad6c7f9SVictor Latushkin static char *
34513ad6c7f9SVictor Latushkin find_zpool(char **target, nvlist_t **configp, int dirc, char **dirv)
3452de6628f0Sck {
3453de6628f0Sck 	nvlist_t *pools;
3454de6628f0Sck 	nvlist_t *match = NULL;
34553ad6c7f9SVictor Latushkin 	char *name = NULL;
34563ad6c7f9SVictor Latushkin 	char *sepp = NULL;
34573ad6c7f9SVictor Latushkin 	char sep;
34583ad6c7f9SVictor Latushkin 	int count = 0;
3459d41c4376SMark J Musante 	importargs_t args = { 0 };
3460d41c4376SMark J Musante 
3461d41c4376SMark J Musante 	args.paths = dirc;
3462d41c4376SMark J Musante 	args.path = dirv;
3463d41c4376SMark J Musante 	args.can_be_active = B_TRUE;
3464de6628f0Sck 
34653ad6c7f9SVictor Latushkin 	if ((sepp = strpbrk(*target, "/@")) != NULL) {
34663ad6c7f9SVictor Latushkin 		sep = *sepp;
34673ad6c7f9SVictor Latushkin 		*sepp = '\0';
34683ad6c7f9SVictor Latushkin 	}
34693ad6c7f9SVictor Latushkin 
3470d41c4376SMark J Musante 	pools = zpool_search_import(g_zfs, &args);
3471de6628f0Sck 
3472de6628f0Sck 	if (pools != NULL) {
3473de6628f0Sck 		nvpair_t *elem = NULL;
3474de6628f0Sck 		while ((elem = nvlist_next_nvpair(pools, elem)) != NULL) {
3475de6628f0Sck 			verify(nvpair_value_nvlist(elem, configp) == 0);
34763ad6c7f9SVictor Latushkin 			if (pool_match(*configp, *target)) {
34773ad6c7f9SVictor Latushkin 				count++;
3478de6628f0Sck 				if (match != NULL) {
34793ad6c7f9SVictor Latushkin 					/* print previously found config */
34803ad6c7f9SVictor Latushkin 					if (name != NULL) {
34813ad6c7f9SVictor Latushkin 						(void) printf("%s\n", name);
34823ad6c7f9SVictor Latushkin 						dump_nvlist(match, 8);
34833ad6c7f9SVictor Latushkin 						name = NULL;
34843ad6c7f9SVictor Latushkin 					}
34853ad6c7f9SVictor Latushkin 					(void) printf("%s\n",
34863ad6c7f9SVictor Latushkin 					    nvpair_name(elem));
34873ad6c7f9SVictor Latushkin 					dump_nvlist(*configp, 8);
3488de6628f0Sck 				} else {
3489de6628f0Sck 					match = *configp;
34903ad6c7f9SVictor Latushkin 					name = nvpair_name(elem);
3491de6628f0Sck 				}
3492de6628f0Sck 			}
3493de6628f0Sck 		}
3494de6628f0Sck 	}
34953ad6c7f9SVictor Latushkin 	if (count > 1)
34963ad6c7f9SVictor Latushkin 		(void) fatal("\tMatched %d pools - use pool GUID "
34973ad6c7f9SVictor Latushkin 		    "instead of pool name or \n"
34983ad6c7f9SVictor Latushkin 		    "\tpool name part of a dataset name to select pool", count);
34993ad6c7f9SVictor Latushkin 
35003ad6c7f9SVictor Latushkin 	if (sepp)
35013ad6c7f9SVictor Latushkin 		*sepp = sep;
35023ad6c7f9SVictor Latushkin 	/*
35033ad6c7f9SVictor Latushkin 	 * If pool GUID was specified for pool id, replace it with pool name
35043ad6c7f9SVictor Latushkin 	 */
35053ad6c7f9SVictor Latushkin 	if (name && (strstr(*target, name) != *target)) {
35063ad6c7f9SVictor Latushkin 		int sz = 1 + strlen(name) + ((sepp) ? strlen(sepp) : 0);
35073ad6c7f9SVictor Latushkin 
35083ad6c7f9SVictor Latushkin 		*target = umem_alloc(sz, UMEM_NOFAIL);
35093ad6c7f9SVictor Latushkin 		(void) snprintf(*target, sz, "%s%s", name, sepp ? sepp : "");
35103ad6c7f9SVictor Latushkin 	}
3511de6628f0Sck 
35123ad6c7f9SVictor Latushkin 	*configp = name ? match : NULL;
3513de6628f0Sck 
35143ad6c7f9SVictor Latushkin 	return (name);
3515de6628f0Sck }
3516de6628f0Sck 
3517fa9e4066Sahrens int
3518fa9e4066Sahrens main(int argc, char **argv)
3519fa9e4066Sahrens {
3520fa9e4066Sahrens 	int i, c;
3521fa9e4066Sahrens 	struct rlimit rl = { 1024, 1024 };
35223ad6c7f9SVictor Latushkin 	spa_t *spa = NULL;
3523fa9e4066Sahrens 	objset_t *os = NULL;
3524fa9e4066Sahrens 	int dump_all = 1;
3525fa9e4066Sahrens 	int verbose = 0;
3526c8ee1847SVictor Latushkin 	int error = 0;
35273ad6c7f9SVictor Latushkin 	char **searchdirs = NULL;
35283ad6c7f9SVictor Latushkin 	int nsearch = 0;
35293ad6c7f9SVictor Latushkin 	char *target;
3530468c413aSTim Haley 	nvlist_t *policy = NULL;
3531468c413aSTim Haley 	uint64_t max_txg = UINT64_MAX;
3532c8ee1847SVictor Latushkin 	int rewind = ZPOOL_NEVER_REWIND;
3533*ae24175bSCyril Plisko 	char *spa_config_path_env;
3534fa9e4066Sahrens 
3535fa9e4066Sahrens 	(void) setrlimit(RLIMIT_NOFILE, &rl);
3536004388ebScasper 	(void) enable_extended_FILE_stdio(-1, -1);
3537fa9e4066Sahrens 
3538fa9e4066Sahrens 	dprintf_setup(&argc, argv);
3539fa9e4066Sahrens 
3540*ae24175bSCyril Plisko 	/*
3541*ae24175bSCyril Plisko 	 * If there is an environment variable SPA_CONFIG_PATH it overrides
3542*ae24175bSCyril Plisko 	 * default spa_config_path setting. If -U flag is specified it will
3543*ae24175bSCyril Plisko 	 * override this environment variable settings once again.
3544*ae24175bSCyril Plisko 	 */
3545*ae24175bSCyril Plisko 	spa_config_path_env = getenv("SPA_CONFIG_PATH");
3546*ae24175bSCyril Plisko 	if (spa_config_path_env != NULL)
3547*ae24175bSCyril Plisko 		spa_config_path = spa_config_path_env;
3548*ae24175bSCyril Plisko 
3549df15e419SMatthew Ahrens 	while ((c = getopt(argc, argv,
35502e4c9986SGeorge Wilson 	    "bcdhilmMI:suCDRSAFLXx:evp:t:U:P")) != -1) {
3551fa9e4066Sahrens 		switch (c) {
3552fa9e4066Sahrens 		case 'b':
3553fa9e4066Sahrens 		case 'c':
3554b24ab676SJeff Bonwick 		case 'd':
3555b24ab676SJeff Bonwick 		case 'h':
3556b24ab676SJeff Bonwick 		case 'i':
3557b24ab676SJeff Bonwick 		case 'l':
3558d6e555bdSGeorge Wilson 		case 'm':
3559fa9e4066Sahrens 		case 's':
3560b24ab676SJeff Bonwick 		case 'u':
3561fa9e4066Sahrens 		case 'C':
3562b24ab676SJeff Bonwick 		case 'D':
35632e4c9986SGeorge Wilson 		case 'M':
356444cd46caSbillm 		case 'R':
3565b24ab676SJeff Bonwick 		case 'S':
3566fa9e4066Sahrens 			dump_opt[c]++;
3567fa9e4066Sahrens 			dump_all = 0;
3568fa9e4066Sahrens 			break;
3569feef89cfSVictor Latushkin 		case 'A':
3570c8ee1847SVictor Latushkin 		case 'F':
357182a0a985SVictor Latushkin 		case 'L':
3572c8ee1847SVictor Latushkin 		case 'X':
35733ad6c7f9SVictor Latushkin 		case 'e':
35743f9d6ad7SLin Ling 		case 'P':
357582a0a985SVictor Latushkin 			dump_opt[c]++;
357682a0a985SVictor Latushkin 			break;
35772e4c9986SGeorge Wilson 		case 'I':
357831d7e8faSGeorge Wilson 			max_inflight = strtoull(optarg, NULL, 0);
357931d7e8faSGeorge Wilson 			if (max_inflight == 0) {
358031d7e8faSGeorge Wilson 				(void) fprintf(stderr, "maximum number "
358131d7e8faSGeorge Wilson 				    "of inflight I/Os must be greater "
358231d7e8faSGeorge Wilson 				    "than 0\n");
358331d7e8faSGeorge Wilson 				usage();
358431d7e8faSGeorge Wilson 			}
358531d7e8faSGeorge Wilson 			break;
3586de6628f0Sck 		case 'p':
35873ad6c7f9SVictor Latushkin 			if (searchdirs == NULL) {
35883ad6c7f9SVictor Latushkin 				searchdirs = umem_alloc(sizeof (char *),
35893ad6c7f9SVictor Latushkin 				    UMEM_NOFAIL);
35903ad6c7f9SVictor Latushkin 			} else {
35913ad6c7f9SVictor Latushkin 				char **tmp = umem_alloc((nsearch + 1) *
35923ad6c7f9SVictor Latushkin 				    sizeof (char *), UMEM_NOFAIL);
35933ad6c7f9SVictor Latushkin 				bcopy(searchdirs, tmp, nsearch *
35943ad6c7f9SVictor Latushkin 				    sizeof (char *));
35953ad6c7f9SVictor Latushkin 				umem_free(searchdirs,
35963ad6c7f9SVictor Latushkin 				    nsearch * sizeof (char *));
35973ad6c7f9SVictor Latushkin 				searchdirs = tmp;
35983ad6c7f9SVictor Latushkin 			}
35993ad6c7f9SVictor Latushkin 			searchdirs[nsearch++] = optarg;
3600de6628f0Sck 			break;
36012e551927SVictor Latushkin 		case 't':
3602468c413aSTim Haley 			max_txg = strtoull(optarg, NULL, 0);
3603468c413aSTim Haley 			if (max_txg < TXG_INITIAL) {
36042e551927SVictor Latushkin 				(void) fprintf(stderr, "incorrect txg "
36052e551927SVictor Latushkin 				    "specified: %s\n", optarg);
36062e551927SVictor Latushkin 				usage();
36072e551927SVictor Latushkin 			}
36082e551927SVictor Latushkin 			break;
3609b24ab676SJeff Bonwick 		case 'U':
3610b24ab676SJeff Bonwick 			spa_config_path = optarg;
3611b24ab676SJeff Bonwick 			break;
36122e4c9986SGeorge Wilson 		case 'v':
36132e4c9986SGeorge Wilson 			verbose++;
36142e4c9986SGeorge Wilson 			break;
36152e4c9986SGeorge Wilson 		case 'x':
36162e4c9986SGeorge Wilson 			vn_dumpdir = optarg;
36172e4c9986SGeorge Wilson 			break;
3618fa9e4066Sahrens 		default:
3619fa9e4066Sahrens 			usage();
3620fa9e4066Sahrens 			break;
3621fa9e4066Sahrens 		}
3622fa9e4066Sahrens 	}
3623fa9e4066Sahrens 
36243ad6c7f9SVictor Latushkin 	if (!dump_opt['e'] && searchdirs != NULL) {
362588b7b0f2SMatthew Ahrens 		(void) fprintf(stderr, "-p option requires use of -e\n");
362688b7b0f2SMatthew Ahrens 		usage();
362788b7b0f2SMatthew Ahrens 	}
3628de6628f0Sck 
362906be9802SMatthew Ahrens 	/*
363006be9802SMatthew Ahrens 	 * ZDB does not typically re-read blocks; therefore limit the ARC
363106be9802SMatthew Ahrens 	 * to 256 MB, which can be used entirely for metadata.
363206be9802SMatthew Ahrens 	 */
363306be9802SMatthew Ahrens 	zfs_arc_max = zfs_arc_meta_limit = 256 * 1024 * 1024;
363406be9802SMatthew Ahrens 
3635f7950bf1SMatthew Ahrens 	/*
3636f7950bf1SMatthew Ahrens 	 * "zdb -c" uses checksum-verifying scrub i/os which are async reads.
3637f7950bf1SMatthew Ahrens 	 * "zdb -b" uses traversal prefetch which uses async reads.
3638f7950bf1SMatthew Ahrens 	 * For good performance, let several of them be active at once.
3639f7950bf1SMatthew Ahrens 	 */
3640f7950bf1SMatthew Ahrens 	zfs_vdev_async_read_max_active = 10;
3641f7950bf1SMatthew Ahrens 
3642fa9e4066Sahrens 	kernel_init(FREAD);
3643de6628f0Sck 	g_zfs = libzfs_init();
364491ebeef5Sahrens 	ASSERT(g_zfs != NULL);
3645fa9e4066Sahrens 
3646b24ab676SJeff Bonwick 	if (dump_all)
3647b24ab676SJeff Bonwick 		verbose = MAX(verbose, 1);
3648b24ab676SJeff Bonwick 
3649fa9e4066Sahrens 	for (c = 0; c < 256; c++) {
36503f9d6ad7SLin Ling 		if (dump_all && !strchr("elAFLRSXP", c))
3651fa9e4066Sahrens 			dump_opt[c] = 1;
3652fa9e4066Sahrens 		if (dump_opt[c])
3653fa9e4066Sahrens 			dump_opt[c] += verbose;
3654fa9e4066Sahrens 	}
3655fa9e4066Sahrens 
3656feef89cfSVictor Latushkin 	aok = (dump_opt['A'] == 1) || (dump_opt['A'] > 2);
3657feef89cfSVictor Latushkin 	zfs_recover = (dump_opt['A'] > 1);
3658feef89cfSVictor Latushkin 
3659fa9e4066Sahrens 	argc -= optind;
3660fa9e4066Sahrens 	argv += optind;
3661fa9e4066Sahrens 
366207428bdfSVictor Latushkin 	if (argc < 2 && dump_opt['R'])
366307428bdfSVictor Latushkin 		usage();
3664fa9e4066Sahrens 	if (argc < 1) {
36653ad6c7f9SVictor Latushkin 		if (!dump_opt['e'] && dump_opt['C']) {
3666e829d913Sck 			dump_cachefile(spa_config_path);
3667fa9e4066Sahrens 			return (0);
3668fa9e4066Sahrens 		}
3669fa9e4066Sahrens 		usage();
3670fa9e4066Sahrens 	}
3671fa9e4066Sahrens 
3672fa9e4066Sahrens 	if (dump_opt['l']) {
3673fa9e4066Sahrens 		dump_label(argv[0]);
3674fa9e4066Sahrens 		return (0);
3675fa9e4066Sahrens 	}
3676fa9e4066Sahrens 
3677c8ee1847SVictor Latushkin 	if (dump_opt['X'] || dump_opt['F'])
3678c8ee1847SVictor Latushkin 		rewind = ZPOOL_DO_REWIND |
3679c8ee1847SVictor Latushkin 		    (dump_opt['X'] ? ZPOOL_EXTREME_REWIND : 0);
3680c8ee1847SVictor Latushkin 
3681c8ee1847SVictor Latushkin 	if (nvlist_alloc(&policy, NV_UNIQUE_NAME_TYPE, 0) != 0 ||
3682c8ee1847SVictor Latushkin 	    nvlist_add_uint64(policy, ZPOOL_REWIND_REQUEST_TXG, max_txg) != 0 ||
3683c8ee1847SVictor Latushkin 	    nvlist_add_uint32(policy, ZPOOL_REWIND_REQUEST, rewind) != 0)
3684c8ee1847SVictor Latushkin 		fatal("internal error: %s", strerror(ENOMEM));
3685c8ee1847SVictor Latushkin 
3686c5904d13Seschrock 	error = 0;
36873ad6c7f9SVictor Latushkin 	target = argv[0];
3688990b4856Slling 
36893ad6c7f9SVictor Latushkin 	if (dump_opt['e']) {
36903ad6c7f9SVictor Latushkin 		nvlist_t *cfg = NULL;
36913ad6c7f9SVictor Latushkin 		char *name = find_zpool(&target, &cfg, nsearch, searchdirs);
3692990b4856Slling 
36933ad6c7f9SVictor Latushkin 		error = ENOENT;
36943ad6c7f9SVictor Latushkin 		if (name) {
369507428bdfSVictor Latushkin 			if (dump_opt['C'] > 1) {
369607428bdfSVictor Latushkin 				(void) printf("\nConfiguration for import:\n");
369707428bdfSVictor Latushkin 				dump_nvlist(cfg, 8);
369807428bdfSVictor Latushkin 			}
3699c8ee1847SVictor Latushkin 			if (nvlist_add_nvlist(cfg,
3700468c413aSTim Haley 			    ZPOOL_REWIND_POLICY, policy) != 0) {
3701468c413aSTim Haley 				fatal("can't open '%s': %s",
3702468c413aSTim Haley 				    target, strerror(ENOMEM));
3703468c413aSTim Haley 			}
37044b964adaSGeorge Wilson 			if ((error = spa_import(name, cfg, NULL,
37054b964adaSGeorge Wilson 			    ZFS_IMPORT_MISSING_LOG)) != 0) {
37064b964adaSGeorge Wilson 				error = spa_import(name, cfg, NULL,
37074b964adaSGeorge Wilson 				    ZFS_IMPORT_VERBATIM);
37084b964adaSGeorge Wilson 			}
3709de6628f0Sck 		}
3710c5904d13Seschrock 	}
3711c5904d13Seschrock 
3712c5904d13Seschrock 	if (error == 0) {
371307428bdfSVictor Latushkin 		if (strpbrk(target, "/@") == NULL || dump_opt['R']) {
371480eb36f2SGeorge Wilson 			error = spa_open_rewind(target, &spa, FTAG, policy,
371580eb36f2SGeorge Wilson 			    NULL);
37168f18d1faSGeorge Wilson 			if (error) {
37178f18d1faSGeorge Wilson 				/*
37188f18d1faSGeorge Wilson 				 * If we're missing the log device then
37198f18d1faSGeorge Wilson 				 * try opening the pool after clearing the
37208f18d1faSGeorge Wilson 				 * log state.
37218f18d1faSGeorge Wilson 				 */
37228f18d1faSGeorge Wilson 				mutex_enter(&spa_namespace_lock);
37233ad6c7f9SVictor Latushkin 				if ((spa = spa_lookup(target)) != NULL &&
37248f18d1faSGeorge Wilson 				    spa->spa_log_state == SPA_LOG_MISSING) {
37258f18d1faSGeorge Wilson 					spa->spa_log_state = SPA_LOG_CLEAR;
37268f18d1faSGeorge Wilson 					error = 0;
37278f18d1faSGeorge Wilson 				}
37288f18d1faSGeorge Wilson 				mutex_exit(&spa_namespace_lock);
37298f18d1faSGeorge Wilson 
373080eb36f2SGeorge Wilson 				if (!error) {
373180eb36f2SGeorge Wilson 					error = spa_open_rewind(target, &spa,
373280eb36f2SGeorge Wilson 					    FTAG, policy, NULL);
373380eb36f2SGeorge Wilson 				}
37348f18d1faSGeorge Wilson 			}
373507428bdfSVictor Latushkin 		} else {
373607428bdfSVictor Latushkin 			error = dmu_objset_own(target, DMU_OST_ANY,
373707428bdfSVictor Latushkin 			    B_TRUE, FTAG, &os);
3738c5904d13Seschrock 		}
3739fa9e4066Sahrens 	}
374080eb36f2SGeorge Wilson 	nvlist_free(policy);
374180eb36f2SGeorge Wilson 
3742fa9e4066Sahrens 	if (error)
37433ad6c7f9SVictor Latushkin 		fatal("can't open '%s': %s", target, strerror(error));
3744fa9e4066Sahrens 
3745fa9e4066Sahrens 	argv++;
374607428bdfSVictor Latushkin 	argc--;
374707428bdfSVictor Latushkin 	if (!dump_opt['R']) {
374807428bdfSVictor Latushkin 		if (argc > 0) {
374907428bdfSVictor Latushkin 			zopt_objects = argc;
375007428bdfSVictor Latushkin 			zopt_object = calloc(zopt_objects, sizeof (uint64_t));
375107428bdfSVictor Latushkin 			for (i = 0; i < zopt_objects; i++) {
375207428bdfSVictor Latushkin 				errno = 0;
375307428bdfSVictor Latushkin 				zopt_object[i] = strtoull(argv[i], NULL, 0);
375407428bdfSVictor Latushkin 				if (zopt_object[i] == 0 && errno != 0)
375587219db7SVictor Latushkin 					fatal("bad number %s: %s",
375607428bdfSVictor Latushkin 					    argv[i], strerror(errno));
375707428bdfSVictor Latushkin 			}
3758fa9e4066Sahrens 		}
3759e690fb27SChristopher Siden 		if (os != NULL) {
3760e690fb27SChristopher Siden 			dump_dir(os);
3761e690fb27SChristopher Siden 		} else if (zopt_objects > 0 && !dump_opt['m']) {
3762e690fb27SChristopher Siden 			dump_dir(spa->spa_meta_objset);
3763e690fb27SChristopher Siden 		} else {
3764e690fb27SChristopher Siden 			dump_zpool(spa);
3765e690fb27SChristopher Siden 		}
3766fa9e4066Sahrens 	} else {
376707428bdfSVictor Latushkin 		flagbits['b'] = ZDB_FLAG_PRINT_BLKPTR;
376807428bdfSVictor Latushkin 		flagbits['c'] = ZDB_FLAG_CHECKSUM;
376907428bdfSVictor Latushkin 		flagbits['d'] = ZDB_FLAG_DECOMPRESS;
377007428bdfSVictor Latushkin 		flagbits['e'] = ZDB_FLAG_BSWAP;
377107428bdfSVictor Latushkin 		flagbits['g'] = ZDB_FLAG_GBH;
377207428bdfSVictor Latushkin 		flagbits['i'] = ZDB_FLAG_INDIRECT;
377307428bdfSVictor Latushkin 		flagbits['p'] = ZDB_FLAG_PHYS;
377407428bdfSVictor Latushkin 		flagbits['r'] = ZDB_FLAG_RAW;
377507428bdfSVictor Latushkin 
377607428bdfSVictor Latushkin 		for (i = 0; i < argc; i++)
377707428bdfSVictor Latushkin 			zdb_read_block(argv[i], spa);
3778fa9e4066Sahrens 	}
3779fa9e4066Sahrens 
378007428bdfSVictor Latushkin 	(os != NULL) ? dmu_objset_disown(os, FTAG) : spa_close(spa, FTAG);
378107428bdfSVictor Latushkin 
3782e0d35c44Smarks 	fuid_table_destroy();
37830a586ceaSMark Shellenbaum 	sa_loaded = B_FALSE;
3784e0d35c44Smarks 
3785de6628f0Sck 	libzfs_fini(g_zfs);
3786fa9e4066Sahrens 	kernel_fini();
3787fa9e4066Sahrens 
3788fa9e4066Sahrens 	return (0);
3789fa9e4066Sahrens }
3790