xref: /titanic_44/usr/src/cmd/zdb/zdb.c (revision a66b28a398b570821116edfa8ced7b2bd4aeeab8)
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.
24f22efbdaSMatthew 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>
62*a66b28a3SJason King #include <libcmdutils.h>
63de6628f0Sck153898 #undef verify
64de6628f0Sck153898 #include <libzfs.h>
65fa9e4066Sahrens 
666de8f417SVictor Latushkin #define	ZDB_COMPRESS_NAME(idx) ((idx) < ZIO_COMPRESS_FUNCTIONS ?	\
676de8f417SVictor Latushkin 	zio_compress_table[(idx)].ci_name : "UNKNOWN")
686de8f417SVictor Latushkin #define	ZDB_CHECKSUM_NAME(idx) ((idx) < ZIO_CHECKSUM_FUNCTIONS ?	\
696de8f417SVictor Latushkin 	zio_checksum_table[(idx)].ci_name : "UNKNOWN")
706de8f417SVictor Latushkin #define	ZDB_OT_NAME(idx) ((idx) < DMU_OT_NUMTYPES ?	\
71ad135b5dSChristopher Siden 	dmu_ot[(idx)].ot_name : DMU_OT_IS_VALID(idx) ?	\
72ad135b5dSChristopher Siden 	dmu_ot_byteswap[DMU_OT_BYTESWAP(idx)].ob_name : "UNKNOWN")
73e690fb27SChristopher Siden #define	ZDB_OT_TYPE(idx) ((idx) < DMU_OT_NUMTYPES ? (idx) :		\
74e690fb27SChristopher Siden 	(((idx) == DMU_OTN_ZAP_DATA || (idx) == DMU_OTN_ZAP_METADATA) ?	\
75e690fb27SChristopher Siden 	DMU_OT_ZAP_OTHER : DMU_OT_NUMTYPES))
766de8f417SVictor Latushkin 
77feef89cfSVictor Latushkin #ifndef lint
787fd05ac4SMatthew Ahrens extern boolean_t zfs_recover;
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;
95de6628f0Sck153898 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 *
_umem_debug_init()105fa9e4066Sahrens _umem_debug_init()
106fa9e4066Sahrens {
107fa9e4066Sahrens 	return ("default,verbose"); /* $UMEM_DEBUG setting */
108fa9e4066Sahrens }
109fa9e4066Sahrens 
110fa9e4066Sahrens const char *
_umem_logging_init(void)111fa9e4066Sahrens _umem_logging_init(void)
112fa9e4066Sahrens {
113fa9e4066Sahrens 	return ("fail,contents"); /* $UMEM_LOGGING setting */
114fa9e4066Sahrens }
115fa9e4066Sahrens 
116fa9e4066Sahrens static void
usage(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);
132fa9e4066Sahrens 
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");
140fa9e4066Sahrens 	(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");
145fa9e4066Sahrens 	(void) fprintf(stderr, "        -b block statistics\n");
146d6e555bdSGeorge Wilson 	(void) fprintf(stderr, "        -m metaslabs\n");
1472e4c9986SGeorge Wilson 	(void) fprintf(stderr, "        -M metaslab groups\n");
1486365109dSVictor Latushkin 	(void) fprintf(stderr, "        -c checksum all metadata (twice for "
1496365109dSVictor Latushkin 	    "all data) blocks\n");
150fa9e4066Sahrens 	(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");
153fa9e4066Sahrens 	(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");
15744cd46caSbillm 	(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
fatal(const char * fmt,...)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
dump_packed_nvlist(objset_t * os,uint64_t object,void * data,size_t size)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
dump_history_offsets(objset_t * os,uint64_t object,void * data,size_t size)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
zdb_nicenum(uint64_t num,char * buf,size_t buflen)247*a66b28a3SJason King zdb_nicenum(uint64_t num, char *buf, size_t buflen)
2483f9d6ad7SLin Ling {
2493f9d6ad7SLin Ling 	if (dump_opt['P'])
250*a66b28a3SJason King 		(void) snprintf(buf, buflen, "%llu", (longlong_t)num);
2513f9d6ad7SLin Ling 	else
252*a66b28a3SJason King 		nicenum(num, buf, sizeof (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
dump_histogram(const uint64_t * histo,int size,int offset)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
dump_zap_stats(objset_t * os,uint64_t object)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");
3048248818dSnd150628 
3058248818dSnd150628 	(void) printf("\t\tPointer table:\n");
3068248818dSnd150628 	(void) printf("\t\t\t%llu elements\n",
307fa9e4066Sahrens 	    (u_longlong_t)zs.zs_ptrtbl_len);
3088248818dSnd150628 	(void) printf("\t\t\tzt_blk: %llu\n",
3098248818dSnd150628 	    (u_longlong_t)zs.zs_ptrtbl_zt_blk);
3108248818dSnd150628 	(void) printf("\t\t\tzt_numblks: %llu\n",
3118248818dSnd150628 	    (u_longlong_t)zs.zs_ptrtbl_zt_numblks);
3128248818dSnd150628 	(void) printf("\t\t\tzt_shift: %llu\n",
3138248818dSnd150628 	    (u_longlong_t)zs.zs_ptrtbl_zt_shift);
3148248818dSnd150628 	(void) printf("\t\t\tzt_blks_copied: %llu\n",
3158248818dSnd150628 	    (u_longlong_t)zs.zs_ptrtbl_blks_copied);
3168248818dSnd150628 	(void) printf("\t\t\tzt_nextblk: %llu\n",
3178248818dSnd150628 	    (u_longlong_t)zs.zs_ptrtbl_nextblk);
3188248818dSnd150628 
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);
3258248818dSnd150628 	(void) printf("\t\tzap_block_type: 0x%llx\n",
3268248818dSnd150628 	    (u_longlong_t)zs.zs_block_type);
3278248818dSnd150628 	(void) printf("\t\tzap_magic: 0x%llx\n",
3288248818dSnd150628 	    (u_longlong_t)zs.zs_magic);
3298248818dSnd150628 	(void) printf("\t\tzap_salt: 0x%llx\n",
3308248818dSnd150628 	    (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
dump_none(objset_t * os,uint64_t object,void * data,size_t size)350fa9e4066Sahrens dump_none(objset_t *os, uint64_t object, void *data, size_t size)
351fa9e4066Sahrens {
352fa9e4066Sahrens }
353fa9e4066Sahrens 
354fa9e4066Sahrens /*ARGSUSED*/
3556de8f417SVictor Latushkin static void
dump_unknown(objset_t * os,uint64_t object,void * data,size_t size)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 
3616de8f417SVictor Latushkin /*ARGSUSED*/
362fa9e4066Sahrens void
dump_uint8(objset_t * os,uint64_t object,void * data,size_t size)363fa9e4066Sahrens dump_uint8(objset_t *os, uint64_t object, void *data, size_t size)
364fa9e4066Sahrens {
365fa9e4066Sahrens }
366fa9e4066Sahrens 
367fa9e4066Sahrens /*ARGSUSED*/
368fa9e4066Sahrens static void
dump_uint64(objset_t * os,uint64_t object,void * data,size_t size)369fa9e4066Sahrens dump_uint64(objset_t *os, uint64_t object, void *data, size_t size)
370fa9e4066Sahrens {
371fa9e4066Sahrens }
372fa9e4066Sahrens 
373fa9e4066Sahrens /*ARGSUSED*/
374fa9e4066Sahrens static void
dump_zap(objset_t * os,uint64_t object,void * data,size_t size)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
dump_bpobj(objset_t * os,uint64_t object,void * data,size_t size)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 
429*a66b28a3SJason King 	/* make sure the output won't get truncated */
430*a66b28a3SJason King 	CTASSERT(sizeof (bytes) >= NN_NUMBUF_SZ);
431*a66b28a3SJason King 	CTASSERT(sizeof (comp) >= NN_NUMBUF_SZ);
432*a66b28a3SJason King 	CTASSERT(sizeof (uncomp) >= NN_NUMBUF_SZ);
433*a66b28a3SJason King 
434732885fcSMatthew Ahrens 	if (bpop == NULL)
435732885fcSMatthew Ahrens 		return;
436732885fcSMatthew Ahrens 
437*a66b28a3SJason King 	zdb_nicenum(bpop->bpo_bytes, bytes, sizeof (bytes));
438*a66b28a3SJason King 	zdb_nicenum(bpop->bpo_comp, comp, sizeof (comp));
439*a66b28a3SJason King 	zdb_nicenum(bpop->bpo_uncomp, uncomp, sizeof (uncomp));
440732885fcSMatthew Ahrens 
441732885fcSMatthew Ahrens 	(void) printf("\t\tnum_blkptrs = %llu\n",
442732885fcSMatthew Ahrens 	    (u_longlong_t)bpop->bpo_num_blkptrs);
443732885fcSMatthew Ahrens 	(void) printf("\t\tbytes = %s\n", bytes);
444732885fcSMatthew Ahrens 	if (size >= BPOBJ_SIZE_V1) {
445732885fcSMatthew Ahrens 		(void) printf("\t\tcomp = %s\n", comp);
446732885fcSMatthew Ahrens 		(void) printf("\t\tuncomp = %s\n", uncomp);
447732885fcSMatthew Ahrens 	}
448732885fcSMatthew Ahrens 	if (size >= sizeof (*bpop)) {
449732885fcSMatthew Ahrens 		(void) printf("\t\tsubobjs = %llu\n",
450732885fcSMatthew Ahrens 		    (u_longlong_t)bpop->bpo_subobjs);
451732885fcSMatthew Ahrens 		(void) printf("\t\tnum_subobjs = %llu\n",
452732885fcSMatthew Ahrens 		    (u_longlong_t)bpop->bpo_num_subobjs);
453732885fcSMatthew Ahrens 	}
454732885fcSMatthew Ahrens 
455732885fcSMatthew Ahrens 	if (dump_opt['d'] < 5)
456732885fcSMatthew Ahrens 		return;
457732885fcSMatthew Ahrens 
458732885fcSMatthew Ahrens 	for (uint64_t i = 0; i < bpop->bpo_num_blkptrs; i++) {
459732885fcSMatthew Ahrens 		char blkbuf[BP_SPRINTF_LEN];
460732885fcSMatthew Ahrens 		blkptr_t bp;
461732885fcSMatthew Ahrens 
462732885fcSMatthew Ahrens 		int err = dmu_read(os, object,
463732885fcSMatthew Ahrens 		    i * sizeof (bp), sizeof (bp), &bp, 0);
464732885fcSMatthew Ahrens 		if (err != 0) {
465732885fcSMatthew Ahrens 			(void) printf("got error %u from dmu_read\n", err);
466732885fcSMatthew Ahrens 			break;
467732885fcSMatthew Ahrens 		}
468732885fcSMatthew Ahrens 		snprintf_blkptr_compact(blkbuf, sizeof (blkbuf), &bp);
469732885fcSMatthew Ahrens 		(void) printf("\t%s\n", blkbuf);
470732885fcSMatthew Ahrens 	}
471732885fcSMatthew Ahrens }
472732885fcSMatthew Ahrens 
473732885fcSMatthew Ahrens /* ARGSUSED */
474732885fcSMatthew Ahrens static void
dump_bpobj_subobjs(objset_t * os,uint64_t object,void * data,size_t size)475732885fcSMatthew Ahrens dump_bpobj_subobjs(objset_t *os, uint64_t object, void *data, size_t size)
476732885fcSMatthew Ahrens {
477732885fcSMatthew Ahrens 	dmu_object_info_t doi;
478732885fcSMatthew Ahrens 
479732885fcSMatthew Ahrens 	VERIFY0(dmu_object_info(os, object, &doi));
480732885fcSMatthew Ahrens 	uint64_t *subobjs = kmem_alloc(doi.doi_max_offset, KM_SLEEP);
481732885fcSMatthew Ahrens 
482732885fcSMatthew Ahrens 	int err = dmu_read(os, object, 0, doi.doi_max_offset, subobjs, 0);
483732885fcSMatthew Ahrens 	if (err != 0) {
484732885fcSMatthew Ahrens 		(void) printf("got error %u from dmu_read\n", err);
485732885fcSMatthew Ahrens 		kmem_free(subobjs, doi.doi_max_offset);
486732885fcSMatthew Ahrens 		return;
487732885fcSMatthew Ahrens 	}
488732885fcSMatthew Ahrens 
489732885fcSMatthew Ahrens 	int64_t last_nonzero = -1;
490732885fcSMatthew Ahrens 	for (uint64_t i = 0; i < doi.doi_max_offset / 8; i++) {
491732885fcSMatthew Ahrens 		if (subobjs[i] != 0)
492732885fcSMatthew Ahrens 			last_nonzero = i;
493732885fcSMatthew Ahrens 	}
494732885fcSMatthew Ahrens 
495732885fcSMatthew Ahrens 	for (int64_t i = 0; i <= last_nonzero; i++) {
496732885fcSMatthew Ahrens 		(void) printf("\t%llu\n", (longlong_t)subobjs[i]);
497732885fcSMatthew Ahrens 	}
498732885fcSMatthew Ahrens 	kmem_free(subobjs, doi.doi_max_offset);
499732885fcSMatthew Ahrens }
500732885fcSMatthew Ahrens 
501e7437265Sahrens /*ARGSUSED*/
502e7437265Sahrens static void
dump_ddt_zap(objset_t * os,uint64_t object,void * data,size_t size)503486ae710SMatthew Ahrens dump_ddt_zap(objset_t *os, uint64_t object, void *data, size_t size)
504486ae710SMatthew Ahrens {
505486ae710SMatthew Ahrens 	dump_zap_stats(os, object);
506486ae710SMatthew Ahrens 	/* contents are printed elsewhere, properly decoded */
507486ae710SMatthew Ahrens }
508486ae710SMatthew Ahrens 
509486ae710SMatthew Ahrens /*ARGSUSED*/
510486ae710SMatthew Ahrens static void
dump_sa_attrs(objset_t * os,uint64_t object,void * data,size_t size)5110a586ceaSMark Shellenbaum dump_sa_attrs(objset_t *os, uint64_t object, void *data, size_t size)
5120a586ceaSMark Shellenbaum {
5130a586ceaSMark Shellenbaum 	zap_cursor_t zc;
5140a586ceaSMark Shellenbaum 	zap_attribute_t attr;
5150a586ceaSMark Shellenbaum 
5160a586ceaSMark Shellenbaum 	dump_zap_stats(os, object);
5170a586ceaSMark Shellenbaum 	(void) printf("\n");
5180a586ceaSMark Shellenbaum 
5190a586ceaSMark Shellenbaum 	for (zap_cursor_init(&zc, os, object);
5200a586ceaSMark Shellenbaum 	    zap_cursor_retrieve(&zc, &attr) == 0;
5210a586ceaSMark Shellenbaum 	    zap_cursor_advance(&zc)) {
5220a586ceaSMark Shellenbaum 		(void) printf("\t\t%s = ", attr.za_name);
5230a586ceaSMark Shellenbaum 		if (attr.za_num_integers == 0) {
5240a586ceaSMark Shellenbaum 			(void) printf("\n");
5250a586ceaSMark Shellenbaum 			continue;
5260a586ceaSMark Shellenbaum 		}
5270a586ceaSMark Shellenbaum 		(void) printf(" %llx : [%d:%d:%d]\n",
5280a586ceaSMark Shellenbaum 		    (u_longlong_t)attr.za_first_integer,
5290a586ceaSMark Shellenbaum 		    (int)ATTR_LENGTH(attr.za_first_integer),
5300a586ceaSMark Shellenbaum 		    (int)ATTR_BSWAP(attr.za_first_integer),
5310a586ceaSMark Shellenbaum 		    (int)ATTR_NUM(attr.za_first_integer));
5320a586ceaSMark Shellenbaum 	}
5330a586ceaSMark Shellenbaum 	zap_cursor_fini(&zc);
5340a586ceaSMark Shellenbaum }
5350a586ceaSMark Shellenbaum 
5360a586ceaSMark Shellenbaum /*ARGSUSED*/
5370a586ceaSMark Shellenbaum static void
dump_sa_layouts(objset_t * os,uint64_t object,void * data,size_t size)5380a586ceaSMark Shellenbaum dump_sa_layouts(objset_t *os, uint64_t object, void *data, size_t size)
5390a586ceaSMark Shellenbaum {
5400a586ceaSMark Shellenbaum 	zap_cursor_t zc;
5410a586ceaSMark Shellenbaum 	zap_attribute_t attr;
5420a586ceaSMark Shellenbaum 	uint16_t *layout_attrs;
5430a586ceaSMark Shellenbaum 	int i;
5440a586ceaSMark Shellenbaum 
5450a586ceaSMark Shellenbaum 	dump_zap_stats(os, object);
5460a586ceaSMark Shellenbaum 	(void) printf("\n");
5470a586ceaSMark Shellenbaum 
5480a586ceaSMark Shellenbaum 	for (zap_cursor_init(&zc, os, object);
5490a586ceaSMark Shellenbaum 	    zap_cursor_retrieve(&zc, &attr) == 0;
5500a586ceaSMark Shellenbaum 	    zap_cursor_advance(&zc)) {
5510a586ceaSMark Shellenbaum 		(void) printf("\t\t%s = [", attr.za_name);
5520a586ceaSMark Shellenbaum 		if (attr.za_num_integers == 0) {
5530a586ceaSMark Shellenbaum 			(void) printf("\n");
5540a586ceaSMark Shellenbaum 			continue;
5550a586ceaSMark Shellenbaum 		}
5560a586ceaSMark Shellenbaum 
5570a586ceaSMark Shellenbaum 		VERIFY(attr.za_integer_length == 2);
5580a586ceaSMark Shellenbaum 		layout_attrs = umem_zalloc(attr.za_num_integers *
5590a586ceaSMark Shellenbaum 		    attr.za_integer_length, UMEM_NOFAIL);
5600a586ceaSMark Shellenbaum 
5610a586ceaSMark Shellenbaum 		VERIFY(zap_lookup(os, object, attr.za_name,
5620a586ceaSMark Shellenbaum 		    attr.za_integer_length,
5630a586ceaSMark Shellenbaum 		    attr.za_num_integers, layout_attrs) == 0);
5640a586ceaSMark Shellenbaum 
5650a586ceaSMark Shellenbaum 		for (i = 0; i != attr.za_num_integers; i++)
5660a586ceaSMark Shellenbaum 			(void) printf(" %d ", (int)layout_attrs[i]);
5670a586ceaSMark Shellenbaum 		(void) printf("]\n");
5680a586ceaSMark Shellenbaum 		umem_free(layout_attrs,
5690a586ceaSMark Shellenbaum 		    attr.za_num_integers * attr.za_integer_length);
5700a586ceaSMark Shellenbaum 	}
5710a586ceaSMark Shellenbaum 	zap_cursor_fini(&zc);
5720a586ceaSMark Shellenbaum }
5730a586ceaSMark Shellenbaum 
5740a586ceaSMark Shellenbaum /*ARGSUSED*/
5750a586ceaSMark Shellenbaum static void
dump_zpldir(objset_t * os,uint64_t object,void * data,size_t size)576e7437265Sahrens dump_zpldir(objset_t *os, uint64_t object, void *data, size_t size)
577e7437265Sahrens {
578e7437265Sahrens 	zap_cursor_t zc;
579e7437265Sahrens 	zap_attribute_t attr;
580e7437265Sahrens 	const char *typenames[] = {
581e7437265Sahrens 		/* 0 */ "not specified",
582e7437265Sahrens 		/* 1 */ "FIFO",
583e7437265Sahrens 		/* 2 */ "Character Device",
584e7437265Sahrens 		/* 3 */ "3 (invalid)",
585e7437265Sahrens 		/* 4 */ "Directory",
586e7437265Sahrens 		/* 5 */ "5 (invalid)",
587e7437265Sahrens 		/* 6 */ "Block Device",
588e7437265Sahrens 		/* 7 */ "7 (invalid)",
589e7437265Sahrens 		/* 8 */ "Regular File",
590e7437265Sahrens 		/* 9 */ "9 (invalid)",
591e7437265Sahrens 		/* 10 */ "Symbolic Link",
592e7437265Sahrens 		/* 11 */ "11 (invalid)",
593e7437265Sahrens 		/* 12 */ "Socket",
594e7437265Sahrens 		/* 13 */ "Door",
595e7437265Sahrens 		/* 14 */ "Event Port",
596e7437265Sahrens 		/* 15 */ "15 (invalid)",
597e7437265Sahrens 	};
598e7437265Sahrens 
599e7437265Sahrens 	dump_zap_stats(os, object);
600e7437265Sahrens 	(void) printf("\n");
601e7437265Sahrens 
602e7437265Sahrens 	for (zap_cursor_init(&zc, os, object);
603e7437265Sahrens 	    zap_cursor_retrieve(&zc, &attr) == 0;
604e7437265Sahrens 	    zap_cursor_advance(&zc)) {
605e7437265Sahrens 		(void) printf("\t\t%s = %lld (type: %s)\n",
606e7437265Sahrens 		    attr.za_name, ZFS_DIRENT_OBJ(attr.za_first_integer),
607e7437265Sahrens 		    typenames[ZFS_DIRENT_TYPE(attr.za_first_integer)]);
608e7437265Sahrens 	}
609e7437265Sahrens 	zap_cursor_fini(&zc);
610e7437265Sahrens }
611e7437265Sahrens 
6120713e232SGeorge Wilson int
get_dtl_refcount(vdev_t * vd)6130713e232SGeorge Wilson get_dtl_refcount(vdev_t *vd)
6140713e232SGeorge Wilson {
6150713e232SGeorge Wilson 	int refcount = 0;
6160713e232SGeorge Wilson 
6170713e232SGeorge Wilson 	if (vd->vdev_ops->vdev_op_leaf) {
6180713e232SGeorge Wilson 		space_map_t *sm = vd->vdev_dtl_sm;
6190713e232SGeorge Wilson 
6200713e232SGeorge Wilson 		if (sm != NULL &&
6210713e232SGeorge Wilson 		    sm->sm_dbuf->db_size == sizeof (space_map_phys_t))
6220713e232SGeorge Wilson 			return (1);
6230713e232SGeorge Wilson 		return (0);
6240713e232SGeorge Wilson 	}
6250713e232SGeorge Wilson 
6260713e232SGeorge Wilson 	for (int c = 0; c < vd->vdev_children; c++)
6270713e232SGeorge Wilson 		refcount += get_dtl_refcount(vd->vdev_child[c]);
6280713e232SGeorge Wilson 	return (refcount);
6290713e232SGeorge Wilson }
6300713e232SGeorge Wilson 
6310713e232SGeorge Wilson int
get_metaslab_refcount(vdev_t * vd)6320713e232SGeorge Wilson get_metaslab_refcount(vdev_t *vd)
6330713e232SGeorge Wilson {
6340713e232SGeorge Wilson 	int refcount = 0;
6350713e232SGeorge Wilson 
6362e4c9986SGeorge Wilson 	if (vd->vdev_top == vd && !vd->vdev_removing) {
6370713e232SGeorge Wilson 		for (int m = 0; m < vd->vdev_ms_count; m++) {
6380713e232SGeorge Wilson 			space_map_t *sm = vd->vdev_ms[m]->ms_sm;
6390713e232SGeorge Wilson 
6400713e232SGeorge Wilson 			if (sm != NULL &&
6410713e232SGeorge Wilson 			    sm->sm_dbuf->db_size == sizeof (space_map_phys_t))
6420713e232SGeorge Wilson 				refcount++;
6430713e232SGeorge Wilson 		}
6440713e232SGeorge Wilson 	}
6450713e232SGeorge Wilson 	for (int c = 0; c < vd->vdev_children; c++)
6460713e232SGeorge Wilson 		refcount += get_metaslab_refcount(vd->vdev_child[c]);
6470713e232SGeorge Wilson 
6480713e232SGeorge Wilson 	return (refcount);
6490713e232SGeorge Wilson }
6500713e232SGeorge Wilson 
6510713e232SGeorge Wilson static int
verify_spacemap_refcounts(spa_t * spa)6520713e232SGeorge Wilson verify_spacemap_refcounts(spa_t *spa)
6530713e232SGeorge Wilson {
6542acef22dSMatthew Ahrens 	uint64_t expected_refcount = 0;
6552acef22dSMatthew Ahrens 	uint64_t actual_refcount;
6560713e232SGeorge Wilson 
6572acef22dSMatthew Ahrens 	(void) feature_get_refcount(spa,
6582acef22dSMatthew Ahrens 	    &spa_feature_table[SPA_FEATURE_SPACEMAP_HISTOGRAM],
6592acef22dSMatthew Ahrens 	    &expected_refcount);
6600713e232SGeorge Wilson 	actual_refcount = get_dtl_refcount(spa->spa_root_vdev);
6610713e232SGeorge Wilson 	actual_refcount += get_metaslab_refcount(spa->spa_root_vdev);
6620713e232SGeorge Wilson 
6630713e232SGeorge Wilson 	if (expected_refcount != actual_refcount) {
6642acef22dSMatthew Ahrens 		(void) printf("space map refcount mismatch: expected %lld != "
6652acef22dSMatthew Ahrens 		    "actual %lld\n",
6662acef22dSMatthew Ahrens 		    (longlong_t)expected_refcount,
6672acef22dSMatthew Ahrens 		    (longlong_t)actual_refcount);
6680713e232SGeorge Wilson 		return (2);
6690713e232SGeorge Wilson 	}
6700713e232SGeorge Wilson 	return (0);
6710713e232SGeorge Wilson }
6720713e232SGeorge Wilson 
673fa9e4066Sahrens static void
dump_spacemap(objset_t * os,space_map_t * sm)6740713e232SGeorge Wilson dump_spacemap(objset_t *os, space_map_t *sm)
675fa9e4066Sahrens {
676fa9e4066Sahrens 	uint64_t alloc, offset, entry;
6778053a263Sck153898 	char *ddata[] = { "ALLOC", "FREE", "CONDENSE", "INVALID",
6788053a263Sck153898 			    "INVALID", "INVALID", "INVALID", "INVALID" };
679fa9e4066Sahrens 
6800713e232SGeorge Wilson 	if (sm == NULL)
681fa9e4066Sahrens 		return;
682fa9e4066Sahrens 
683fa9e4066Sahrens 	/*
684fa9e4066Sahrens 	 * Print out the freelist entries in both encoded and decoded form.
685fa9e4066Sahrens 	 */
686fa9e4066Sahrens 	alloc = 0;
6870713e232SGeorge Wilson 	for (offset = 0; offset < space_map_length(sm);
6880713e232SGeorge Wilson 	    offset += sizeof (entry)) {
6890713e232SGeorge Wilson 		uint8_t mapshift = sm->sm_shift;
6900713e232SGeorge Wilson 
6910713e232SGeorge Wilson 		VERIFY0(dmu_read(os, space_map_object(sm), offset,
6927bfdf011SNeil Perrin 		    sizeof (entry), &entry, DMU_READ_PREFETCH));
693fa9e4066Sahrens 		if (SM_DEBUG_DECODE(entry)) {
6940713e232SGeorge Wilson 
69587219db7SVictor Latushkin 			(void) printf("\t    [%6llu] %s: txg %llu, pass %llu\n",
696fa9e4066Sahrens 			    (u_longlong_t)(offset / sizeof (entry)),
697fa9e4066Sahrens 			    ddata[SM_DEBUG_ACTION_DECODE(entry)],
6985ad82045Snd150628 			    (u_longlong_t)SM_DEBUG_TXG_DECODE(entry),
6995ad82045Snd150628 			    (u_longlong_t)SM_DEBUG_SYNCPASS_DECODE(entry));
700fa9e4066Sahrens 		} else {
70187219db7SVictor Latushkin 			(void) printf("\t    [%6llu]    %c  range:"
70287219db7SVictor Latushkin 			    " %010llx-%010llx  size: %06llx\n",
703fa9e4066Sahrens 			    (u_longlong_t)(offset / sizeof (entry)),
704fa9e4066Sahrens 			    SM_TYPE_DECODE(entry) == SM_ALLOC ? 'A' : 'F',
7055ad82045Snd150628 			    (u_longlong_t)((SM_OFFSET_DECODE(entry) <<
7060713e232SGeorge Wilson 			    mapshift) + sm->sm_start),
7075ad82045Snd150628 			    (u_longlong_t)((SM_OFFSET_DECODE(entry) <<
7080713e232SGeorge Wilson 			    mapshift) + sm->sm_start +
7090713e232SGeorge Wilson 			    (SM_RUN_DECODE(entry) << mapshift)),
7105ad82045Snd150628 			    (u_longlong_t)(SM_RUN_DECODE(entry) << mapshift));
711fa9e4066Sahrens 			if (SM_TYPE_DECODE(entry) == SM_ALLOC)
712fa9e4066Sahrens 				alloc += SM_RUN_DECODE(entry) << mapshift;
713fa9e4066Sahrens 			else
714fa9e4066Sahrens 				alloc -= SM_RUN_DECODE(entry) << mapshift;
715fa9e4066Sahrens 		}
716fa9e4066Sahrens 	}
7170713e232SGeorge Wilson 	if (alloc != space_map_allocated(sm)) {
718fa9e4066Sahrens 		(void) printf("space_map_object alloc (%llu) INCONSISTENT "
719fa9e4066Sahrens 		    "with space map summary (%llu)\n",
7200713e232SGeorge Wilson 		    (u_longlong_t)space_map_allocated(sm), (u_longlong_t)alloc);
721fa9e4066Sahrens 	}
722fa9e4066Sahrens }
723fa9e4066Sahrens 
724fa9e4066Sahrens static void
dump_metaslab_stats(metaslab_t * msp)725d6e555bdSGeorge Wilson dump_metaslab_stats(metaslab_t *msp)
726d6e555bdSGeorge Wilson {
7273f9d6ad7SLin Ling 	char maxbuf[32];
7280713e232SGeorge Wilson 	range_tree_t *rt = msp->ms_tree;
7290713e232SGeorge Wilson 	avl_tree_t *t = &msp->ms_size_tree;
7300713e232SGeorge Wilson 	int free_pct = range_tree_space(rt) * 100 / msp->ms_size;
731d6e555bdSGeorge Wilson 
732*a66b28a3SJason King 	/* max sure nicenum has enough space */
733*a66b28a3SJason King 	CTASSERT(sizeof (maxbuf) >= NN_NUMBUF_SZ);
734*a66b28a3SJason King 
735*a66b28a3SJason King 	zdb_nicenum(metaslab_block_maxsize(msp), maxbuf, sizeof (maxbuf));
736d6e555bdSGeorge Wilson 
73787219db7SVictor Latushkin 	(void) printf("\t %25s %10lu   %7s  %6s   %4s %4d%%\n",
738d6e555bdSGeorge Wilson 	    "segments", avl_numnodes(t), "maxsize", maxbuf,
739d6e555bdSGeorge Wilson 	    "freepct", free_pct);
7400713e232SGeorge Wilson 	(void) printf("\tIn-memory histogram:\n");
7410713e232SGeorge Wilson 	dump_histogram(rt->rt_histogram, RANGE_TREE_HISTOGRAM_SIZE, 0);
742d6e555bdSGeorge Wilson }
743d6e555bdSGeorge Wilson 
744d6e555bdSGeorge Wilson static void
dump_metaslab(metaslab_t * msp)745fa9e4066Sahrens dump_metaslab(metaslab_t *msp)
746fa9e4066Sahrens {
747fa9e4066Sahrens 	vdev_t *vd = msp->ms_group->mg_vd;
748fa9e4066Sahrens 	spa_t *spa = vd->vdev_spa;
7490713e232SGeorge Wilson 	space_map_t *sm = msp->ms_sm;
7503f9d6ad7SLin Ling 	char freebuf[32];
751fa9e4066Sahrens 
752*a66b28a3SJason King 	zdb_nicenum(msp->ms_size - space_map_allocated(sm), freebuf,
753*a66b28a3SJason King 	    sizeof (freebuf));
754fa9e4066Sahrens 
755fa9e4066Sahrens 	(void) printf(
75687219db7SVictor Latushkin 	    "\tmetaslab %6llu   offset %12llx   spacemap %6llu   free    %5s\n",
7570713e232SGeorge Wilson 	    (u_longlong_t)msp->ms_id, (u_longlong_t)msp->ms_start,
7580713e232SGeorge Wilson 	    (u_longlong_t)space_map_object(sm), freebuf);
759fa9e4066Sahrens 
7600713e232SGeorge Wilson 	if (dump_opt['m'] > 2 && !dump_opt['L']) {
761d6e555bdSGeorge Wilson 		mutex_enter(&msp->ms_lock);
7620713e232SGeorge Wilson 		metaslab_load_wait(msp);
7630713e232SGeorge Wilson 		if (!msp->ms_loaded) {
7640713e232SGeorge Wilson 			VERIFY0(metaslab_load(msp));
7650713e232SGeorge Wilson 			range_tree_stat_verify(msp->ms_tree);
7660713e232SGeorge Wilson 		}
767d6e555bdSGeorge Wilson 		dump_metaslab_stats(msp);
7680713e232SGeorge Wilson 		metaslab_unload(msp);
769d6e555bdSGeorge Wilson 		mutex_exit(&msp->ms_lock);
770d6e555bdSGeorge Wilson 	}
771d6e555bdSGeorge Wilson 
7720713e232SGeorge Wilson 	if (dump_opt['m'] > 1 && sm != NULL &&
7732acef22dSMatthew Ahrens 	    spa_feature_is_active(spa, SPA_FEATURE_SPACEMAP_HISTOGRAM)) {
7740713e232SGeorge Wilson 		/*
7750713e232SGeorge Wilson 		 * The space map histogram represents free space in chunks
7760713e232SGeorge Wilson 		 * of sm_shift (i.e. bucket 0 refers to 2^sm_shift).
7770713e232SGeorge Wilson 		 */
7782e4c9986SGeorge Wilson 		(void) printf("\tOn-disk histogram:\t\tfragmentation %llu\n",
7792e4c9986SGeorge Wilson 		    (u_longlong_t)msp->ms_fragmentation);
7800713e232SGeorge Wilson 		dump_histogram(sm->sm_phys->smp_histogram,
7812e4c9986SGeorge Wilson 		    SPACE_MAP_HISTOGRAM_SIZE, sm->sm_shift);
7820713e232SGeorge Wilson 	}
7830713e232SGeorge Wilson 
7840713e232SGeorge Wilson 	if (dump_opt['d'] > 5 || dump_opt['m'] > 3) {
7850713e232SGeorge Wilson 		ASSERT(msp->ms_size == (1ULL << vd->vdev_ms_shift));
786fa9e4066Sahrens 
787d6e555bdSGeorge Wilson 		mutex_enter(&msp->ms_lock);
7880713e232SGeorge Wilson 		dump_spacemap(spa->spa_meta_objset, msp->ms_sm);
789d6e555bdSGeorge Wilson 		mutex_exit(&msp->ms_lock);
790d6e555bdSGeorge Wilson 	}
79187219db7SVictor Latushkin }
792d6e555bdSGeorge Wilson 
79387219db7SVictor Latushkin static void
print_vdev_metaslab_header(vdev_t * vd)79487219db7SVictor Latushkin print_vdev_metaslab_header(vdev_t *vd)
79587219db7SVictor Latushkin {
79687219db7SVictor Latushkin 	(void) printf("\tvdev %10llu\n\t%-10s%5llu   %-19s   %-15s   %-10s\n",
79787219db7SVictor Latushkin 	    (u_longlong_t)vd->vdev_id,
79887219db7SVictor Latushkin 	    "metaslabs", (u_longlong_t)vd->vdev_ms_count,
79987219db7SVictor Latushkin 	    "offset", "spacemap", "free");
80087219db7SVictor Latushkin 	(void) printf("\t%15s   %19s   %15s   %10s\n",
80187219db7SVictor Latushkin 	    "---------------", "-------------------",
80287219db7SVictor Latushkin 	    "---------------", "-------------");
803fa9e4066Sahrens }
804fa9e4066Sahrens 
805fa9e4066Sahrens static void
dump_metaslab_groups(spa_t * spa)8062e4c9986SGeorge Wilson dump_metaslab_groups(spa_t *spa)
8072e4c9986SGeorge Wilson {
8082e4c9986SGeorge Wilson 	vdev_t *rvd = spa->spa_root_vdev;
8092e4c9986SGeorge Wilson 	metaslab_class_t *mc = spa_normal_class(spa);
8102e4c9986SGeorge Wilson 	uint64_t fragmentation;
8112e4c9986SGeorge Wilson 
8122e4c9986SGeorge Wilson 	metaslab_class_histogram_verify(mc);
8132e4c9986SGeorge Wilson 
8142e4c9986SGeorge Wilson 	for (int c = 0; c < rvd->vdev_children; c++) {
8152e4c9986SGeorge Wilson 		vdev_t *tvd = rvd->vdev_child[c];
8162e4c9986SGeorge Wilson 		metaslab_group_t *mg = tvd->vdev_mg;
8172e4c9986SGeorge Wilson 
8182e4c9986SGeorge Wilson 		if (mg->mg_class != mc)
8192e4c9986SGeorge Wilson 			continue;
8202e4c9986SGeorge Wilson 
8212e4c9986SGeorge Wilson 		metaslab_group_histogram_verify(mg);
8222e4c9986SGeorge Wilson 		mg->mg_fragmentation = metaslab_group_fragmentation(mg);
8232e4c9986SGeorge Wilson 
8242e4c9986SGeorge Wilson 		(void) printf("\tvdev %10llu\t\tmetaslabs%5llu\t\t"
8252e4c9986SGeorge Wilson 		    "fragmentation",
8262e4c9986SGeorge Wilson 		    (u_longlong_t)tvd->vdev_id,
8272e4c9986SGeorge Wilson 		    (u_longlong_t)tvd->vdev_ms_count);
8282e4c9986SGeorge Wilson 		if (mg->mg_fragmentation == ZFS_FRAG_INVALID) {
8292e4c9986SGeorge Wilson 			(void) printf("%3s\n", "-");
8302e4c9986SGeorge Wilson 		} else {
8312e4c9986SGeorge Wilson 			(void) printf("%3llu%%\n",
8322e4c9986SGeorge Wilson 			    (u_longlong_t)mg->mg_fragmentation);
8332e4c9986SGeorge Wilson 		}
8342e4c9986SGeorge Wilson 		dump_histogram(mg->mg_histogram, RANGE_TREE_HISTOGRAM_SIZE, 0);
8352e4c9986SGeorge Wilson 	}
8362e4c9986SGeorge Wilson 
8372e4c9986SGeorge Wilson 	(void) printf("\tpool %s\tfragmentation", spa_name(spa));
8382e4c9986SGeorge Wilson 	fragmentation = metaslab_class_fragmentation(mc);
8392e4c9986SGeorge Wilson 	if (fragmentation == ZFS_FRAG_INVALID)
8402e4c9986SGeorge Wilson 		(void) printf("\t%3s\n", "-");
8412e4c9986SGeorge Wilson 	else
8422e4c9986SGeorge Wilson 		(void) printf("\t%3llu%%\n", (u_longlong_t)fragmentation);
8432e4c9986SGeorge Wilson 	dump_histogram(mc->mc_histogram, RANGE_TREE_HISTOGRAM_SIZE, 0);
8442e4c9986SGeorge Wilson }
8452e4c9986SGeorge Wilson 
8462e4c9986SGeorge Wilson static void
dump_metaslabs(spa_t * spa)847fa9e4066Sahrens dump_metaslabs(spa_t *spa)
848fa9e4066Sahrens {
84987219db7SVictor Latushkin 	vdev_t *vd, *rvd = spa->spa_root_vdev;
85087219db7SVictor Latushkin 	uint64_t m, c = 0, children = rvd->vdev_children;
851fa9e4066Sahrens 
852fa9e4066Sahrens 	(void) printf("\nMetaslabs:\n");
853fa9e4066Sahrens 
85487219db7SVictor Latushkin 	if (!dump_opt['d'] && zopt_objects > 0) {
85587219db7SVictor Latushkin 		c = zopt_object[0];
856fa9e4066Sahrens 
85787219db7SVictor Latushkin 		if (c >= children)
85887219db7SVictor Latushkin 			(void) fatal("bad vdev id: %llu", (u_longlong_t)c);
85987219db7SVictor Latushkin 
86087219db7SVictor Latushkin 		if (zopt_objects > 1) {
86187219db7SVictor Latushkin 			vd = rvd->vdev_child[c];
86287219db7SVictor Latushkin 			print_vdev_metaslab_header(vd);
86387219db7SVictor Latushkin 
86487219db7SVictor Latushkin 			for (m = 1; m < zopt_objects; m++) {
86587219db7SVictor Latushkin 				if (zopt_object[m] < vd->vdev_ms_count)
86687219db7SVictor Latushkin 					dump_metaslab(
86787219db7SVictor Latushkin 					    vd->vdev_ms[zopt_object[m]]);
86887219db7SVictor Latushkin 				else
86987219db7SVictor Latushkin 					(void) fprintf(stderr, "bad metaslab "
87087219db7SVictor Latushkin 					    "number %llu\n",
87187219db7SVictor Latushkin 					    (u_longlong_t)zopt_object[m]);
87287219db7SVictor Latushkin 			}
87387219db7SVictor Latushkin 			(void) printf("\n");
87487219db7SVictor Latushkin 			return;
87587219db7SVictor Latushkin 		}
87687219db7SVictor Latushkin 		children = c + 1;
87787219db7SVictor Latushkin 	}
87887219db7SVictor Latushkin 	for (; c < children; c++) {
87987219db7SVictor Latushkin 		vd = rvd->vdev_child[c];
88087219db7SVictor Latushkin 		print_vdev_metaslab_header(vd);
881fa9e4066Sahrens 
882fa9e4066Sahrens 		for (m = 0; m < vd->vdev_ms_count; m++)
883fa9e4066Sahrens 			dump_metaslab(vd->vdev_ms[m]);
884fa9e4066Sahrens 		(void) printf("\n");
885fa9e4066Sahrens 	}
886fa9e4066Sahrens }
887fa9e4066Sahrens 
888fa9e4066Sahrens static void
dump_dde(const ddt_t * ddt,const ddt_entry_t * dde,uint64_t index)889b24ab676SJeff Bonwick dump_dde(const ddt_t *ddt, const ddt_entry_t *dde, uint64_t index)
890b24ab676SJeff Bonwick {
891b24ab676SJeff Bonwick 	const ddt_phys_t *ddp = dde->dde_phys;
892b24ab676SJeff Bonwick 	const ddt_key_t *ddk = &dde->dde_key;
893b24ab676SJeff Bonwick 	char *types[4] = { "ditto", "single", "double", "triple" };
894b24ab676SJeff Bonwick 	char blkbuf[BP_SPRINTF_LEN];
895b24ab676SJeff Bonwick 	blkptr_t blk;
896b24ab676SJeff Bonwick 
897b24ab676SJeff Bonwick 	for (int p = 0; p < DDT_PHYS_TYPES; p++, ddp++) {
898b24ab676SJeff Bonwick 		if (ddp->ddp_phys_birth == 0)
899b24ab676SJeff Bonwick 			continue;
900bbfd46c4SJeff Bonwick 		ddt_bp_create(ddt->ddt_checksum, ddk, ddp, &blk);
90143466aaeSMax Grossman 		snprintf_blkptr(blkbuf, sizeof (blkbuf), &blk);
902b24ab676SJeff Bonwick 		(void) printf("index %llx refcnt %llu %s %s\n",
903b24ab676SJeff Bonwick 		    (u_longlong_t)index, (u_longlong_t)ddp->ddp_refcnt,
904b24ab676SJeff Bonwick 		    types[p], blkbuf);
905b24ab676SJeff Bonwick 	}
906b24ab676SJeff Bonwick }
907b24ab676SJeff Bonwick 
908b24ab676SJeff Bonwick static void
dump_dedup_ratio(const ddt_stat_t * dds)909b24ab676SJeff Bonwick dump_dedup_ratio(const ddt_stat_t *dds)
910b24ab676SJeff Bonwick {
911b24ab676SJeff Bonwick 	double rL, rP, rD, D, dedup, compress, copies;
912b24ab676SJeff Bonwick 
913b24ab676SJeff Bonwick 	if (dds->dds_blocks == 0)
914b24ab676SJeff Bonwick 		return;
915b24ab676SJeff Bonwick 
916b24ab676SJeff Bonwick 	rL = (double)dds->dds_ref_lsize;
917b24ab676SJeff Bonwick 	rP = (double)dds->dds_ref_psize;
918b24ab676SJeff Bonwick 	rD = (double)dds->dds_ref_dsize;
919b24ab676SJeff Bonwick 	D = (double)dds->dds_dsize;
920b24ab676SJeff Bonwick 
921b24ab676SJeff Bonwick 	dedup = rD / D;
922b24ab676SJeff Bonwick 	compress = rL / rP;
923b24ab676SJeff Bonwick 	copies = rD / rP;
924b24ab676SJeff Bonwick 
925b24ab676SJeff Bonwick 	(void) printf("dedup = %.2f, compress = %.2f, copies = %.2f, "
926b24ab676SJeff Bonwick 	    "dedup * compress / copies = %.2f\n\n",
927b24ab676SJeff Bonwick 	    dedup, compress, copies, dedup * compress / copies);
928b24ab676SJeff Bonwick }
929b24ab676SJeff Bonwick 
930b24ab676SJeff Bonwick static void
dump_ddt(ddt_t * ddt,enum ddt_type type,enum ddt_class class)931b24ab676SJeff Bonwick dump_ddt(ddt_t *ddt, enum ddt_type type, enum ddt_class class)
932b24ab676SJeff Bonwick {
933b24ab676SJeff Bonwick 	char name[DDT_NAMELEN];
934b24ab676SJeff Bonwick 	ddt_entry_t dde;
935b24ab676SJeff Bonwick 	uint64_t walk = 0;
936b24ab676SJeff Bonwick 	dmu_object_info_t doi;
937b24ab676SJeff Bonwick 	uint64_t count, dspace, mspace;
938b24ab676SJeff Bonwick 	int error;
939b24ab676SJeff Bonwick 
940b24ab676SJeff Bonwick 	error = ddt_object_info(ddt, type, class, &doi);
941b24ab676SJeff Bonwick 
942b24ab676SJeff Bonwick 	if (error == ENOENT)
943b24ab676SJeff Bonwick 		return;
944b24ab676SJeff Bonwick 	ASSERT(error == 0);
945b24ab676SJeff Bonwick 
9467448a079SGeorge Wilson 	if ((count = ddt_object_count(ddt, type, class)) == 0)
9477448a079SGeorge Wilson 		return;
9487448a079SGeorge Wilson 
949b24ab676SJeff Bonwick 	dspace = doi.doi_physical_blocks_512 << 9;
950b24ab676SJeff Bonwick 	mspace = doi.doi_fill_count * doi.doi_data_block_size;
951b24ab676SJeff Bonwick 
952b24ab676SJeff Bonwick 	ddt_object_name(ddt, type, class, name);
953b24ab676SJeff Bonwick 
954b24ab676SJeff Bonwick 	(void) printf("%s: %llu entries, size %llu on disk, %llu in core\n",
955b24ab676SJeff Bonwick 	    name,
956b24ab676SJeff Bonwick 	    (u_longlong_t)count,
957b24ab676SJeff Bonwick 	    (u_longlong_t)(dspace / count),
958b24ab676SJeff Bonwick 	    (u_longlong_t)(mspace / count));
959b24ab676SJeff Bonwick 
960b24ab676SJeff Bonwick 	if (dump_opt['D'] < 3)
961b24ab676SJeff Bonwick 		return;
962b24ab676SJeff Bonwick 
9639eb19f4dSGeorge Wilson 	zpool_dump_ddt(NULL, &ddt->ddt_histogram[type][class]);
964b24ab676SJeff Bonwick 
965b24ab676SJeff Bonwick 	if (dump_opt['D'] < 4)
966b24ab676SJeff Bonwick 		return;
967b24ab676SJeff Bonwick 
968b24ab676SJeff Bonwick 	if (dump_opt['D'] < 5 && class == DDT_CLASS_UNIQUE)
969b24ab676SJeff Bonwick 		return;
970b24ab676SJeff Bonwick 
971b24ab676SJeff Bonwick 	(void) printf("%s contents:\n\n", name);
972b24ab676SJeff Bonwick 
973bbfd46c4SJeff Bonwick 	while ((error = ddt_object_walk(ddt, type, class, &walk, &dde)) == 0)
974b24ab676SJeff Bonwick 		dump_dde(ddt, &dde, walk);
975b24ab676SJeff Bonwick 
976b24ab676SJeff Bonwick 	ASSERT(error == ENOENT);
977b24ab676SJeff Bonwick 
978b24ab676SJeff Bonwick 	(void) printf("\n");
979b24ab676SJeff Bonwick }
980b24ab676SJeff Bonwick 
981b24ab676SJeff Bonwick static void
dump_all_ddts(spa_t * spa)982b24ab676SJeff Bonwick dump_all_ddts(spa_t *spa)
983b24ab676SJeff Bonwick {
984b24ab676SJeff Bonwick 	ddt_histogram_t ddh_total = { 0 };
985b24ab676SJeff Bonwick 	ddt_stat_t dds_total = { 0 };
986b24ab676SJeff Bonwick 
987b24ab676SJeff Bonwick 	for (enum zio_checksum c = 0; c < ZIO_CHECKSUM_FUNCTIONS; c++) {
988b24ab676SJeff Bonwick 		ddt_t *ddt = spa->spa_ddt[c];
989b24ab676SJeff Bonwick 		for (enum ddt_type type = 0; type < DDT_TYPES; type++) {
990b24ab676SJeff Bonwick 			for (enum ddt_class class = 0; class < DDT_CLASSES;
991b24ab676SJeff Bonwick 			    class++) {
992b24ab676SJeff Bonwick 				dump_ddt(ddt, type, class);
993b24ab676SJeff Bonwick 			}
994b24ab676SJeff Bonwick 		}
995b24ab676SJeff Bonwick 	}
996b24ab676SJeff Bonwick 
9979eb19f4dSGeorge Wilson 	ddt_get_dedup_stats(spa, &dds_total);
998b24ab676SJeff Bonwick 
999b24ab676SJeff Bonwick 	if (dds_total.dds_blocks == 0) {
1000b24ab676SJeff Bonwick 		(void) printf("All DDTs are empty\n");
1001b24ab676SJeff Bonwick 		return;
1002b24ab676SJeff Bonwick 	}
1003b24ab676SJeff Bonwick 
1004b24ab676SJeff Bonwick 	(void) printf("\n");
1005b24ab676SJeff Bonwick 
1006b24ab676SJeff Bonwick 	if (dump_opt['D'] > 1) {
1007b24ab676SJeff Bonwick 		(void) printf("DDT histogram (aggregated over all DDTs):\n");
10089eb19f4dSGeorge Wilson 		ddt_get_dedup_histogram(spa, &ddh_total);
10099eb19f4dSGeorge Wilson 		zpool_dump_ddt(&dds_total, &ddh_total);
1010b24ab676SJeff Bonwick 	}
1011b24ab676SJeff Bonwick 
1012b24ab676SJeff Bonwick 	dump_dedup_ratio(&dds_total);
1013b24ab676SJeff Bonwick }
1014b24ab676SJeff Bonwick 
1015b24ab676SJeff Bonwick static void
dump_dtl_seg(void * arg,uint64_t start,uint64_t size)10160713e232SGeorge Wilson dump_dtl_seg(void *arg, uint64_t start, uint64_t size)
10178ad4d6ddSJeff Bonwick {
10180713e232SGeorge Wilson 	char *prefix = arg;
10198ad4d6ddSJeff Bonwick 
10208ad4d6ddSJeff Bonwick 	(void) printf("%s [%llu,%llu) length %llu\n",
10218ad4d6ddSJeff Bonwick 	    prefix,
10228ad4d6ddSJeff Bonwick 	    (u_longlong_t)start,
10238ad4d6ddSJeff Bonwick 	    (u_longlong_t)(start + size),
10248ad4d6ddSJeff Bonwick 	    (u_longlong_t)(size));
10258ad4d6ddSJeff Bonwick }
10268ad4d6ddSJeff Bonwick 
10278ad4d6ddSJeff Bonwick static void
dump_dtl(vdev_t * vd,int indent)1028fa9e4066Sahrens dump_dtl(vdev_t *vd, int indent)
1029fa9e4066Sahrens {
10308ad4d6ddSJeff Bonwick 	spa_t *spa = vd->vdev_spa;
10318ad4d6ddSJeff Bonwick 	boolean_t required;
10328ad4d6ddSJeff Bonwick 	char *name[DTL_TYPES] = { "missing", "partial", "scrub", "outage" };
10338ad4d6ddSJeff Bonwick 	char prefix[256];
10348ad4d6ddSJeff Bonwick 
10358f18d1faSGeorge Wilson 	spa_vdev_state_enter(spa, SCL_NONE);
10368ad4d6ddSJeff Bonwick 	required = vdev_dtl_required(vd);
10378ad4d6ddSJeff Bonwick 	(void) spa_vdev_state_exit(spa, NULL, 0);
1038fa9e4066Sahrens 
1039fa9e4066Sahrens 	if (indent == 0)
1040fa9e4066Sahrens 		(void) printf("\nDirty time logs:\n\n");
1041fa9e4066Sahrens 
10428ad4d6ddSJeff Bonwick 	(void) printf("\t%*s%s [%s]\n", indent, "",
1043e14bb325SJeff Bonwick 	    vd->vdev_path ? vd->vdev_path :
10448ad4d6ddSJeff Bonwick 	    vd->vdev_parent ? vd->vdev_ops->vdev_op_type : spa_name(spa),
10458ad4d6ddSJeff Bonwick 	    required ? "DTL-required" : "DTL-expendable");
1046fa9e4066Sahrens 
10478ad4d6ddSJeff Bonwick 	for (int t = 0; t < DTL_TYPES; t++) {
10480713e232SGeorge Wilson 		range_tree_t *rt = vd->vdev_dtl[t];
10490713e232SGeorge Wilson 		if (range_tree_space(rt) == 0)
10508ad4d6ddSJeff Bonwick 			continue;
10518ad4d6ddSJeff Bonwick 		(void) snprintf(prefix, sizeof (prefix), "\t%*s%s",
10528ad4d6ddSJeff Bonwick 		    indent + 2, "", name[t]);
10530713e232SGeorge Wilson 		mutex_enter(rt->rt_lock);
10540713e232SGeorge Wilson 		range_tree_walk(rt, dump_dtl_seg, prefix);
10550713e232SGeorge Wilson 		mutex_exit(rt->rt_lock);
10568ad4d6ddSJeff Bonwick 		if (dump_opt['d'] > 5 && vd->vdev_children == 0)
10570713e232SGeorge Wilson 			dump_spacemap(spa->spa_meta_objset, vd->vdev_dtl_sm);
1058fa9e4066Sahrens 	}
1059fa9e4066Sahrens 
10608ad4d6ddSJeff Bonwick 	for (int c = 0; c < vd->vdev_children; c++)
1061fa9e4066Sahrens 		dump_dtl(vd->vdev_child[c], indent + 4);
1062fa9e4066Sahrens }
1063fa9e4066Sahrens 
10648f18d1faSGeorge Wilson static void
dump_history(spa_t * spa)10658f18d1faSGeorge Wilson dump_history(spa_t *spa)
10668f18d1faSGeorge Wilson {
10678f18d1faSGeorge Wilson 	nvlist_t **events = NULL;
10688f18d1faSGeorge Wilson 	char buf[SPA_MAXBLOCKSIZE];
1069e4161df6SVictor Latushkin 	uint64_t resid, len, off = 0;
10708f18d1faSGeorge Wilson 	uint_t num = 0;
10718f18d1faSGeorge Wilson 	int error;
10728f18d1faSGeorge Wilson 	time_t tsec;
10738f18d1faSGeorge Wilson 	struct tm t;
10748f18d1faSGeorge Wilson 	char tbuf[30];
10758f18d1faSGeorge Wilson 	char internalstr[MAXPATHLEN];
10768f18d1faSGeorge Wilson 
10778f18d1faSGeorge Wilson 	do {
1078e4161df6SVictor Latushkin 		len = sizeof (buf);
1079e4161df6SVictor Latushkin 
10808f18d1faSGeorge Wilson 		if ((error = spa_history_get(spa, &off, &len, buf)) != 0) {
10818f18d1faSGeorge Wilson 			(void) fprintf(stderr, "Unable to read history: "
10828f18d1faSGeorge Wilson 			    "error %d\n", error);
10838f18d1faSGeorge Wilson 			return;
10848f18d1faSGeorge Wilson 		}
10858f18d1faSGeorge Wilson 
10868f18d1faSGeorge Wilson 		if (zpool_history_unpack(buf, len, &resid, &events, &num) != 0)
10878f18d1faSGeorge Wilson 			break;
10888f18d1faSGeorge Wilson 
10898f18d1faSGeorge Wilson 		off -= resid;
10908f18d1faSGeorge Wilson 	} while (len != 0);
10918f18d1faSGeorge Wilson 
10928f18d1faSGeorge Wilson 	(void) printf("\nHistory:\n");
10938f18d1faSGeorge Wilson 	for (int i = 0; i < num; i++) {
10948f18d1faSGeorge Wilson 		uint64_t time, txg, ievent;
10958f18d1faSGeorge Wilson 		char *cmd, *intstr;
10964445fffbSMatthew Ahrens 		boolean_t printed = B_FALSE;
10978f18d1faSGeorge Wilson 
10988f18d1faSGeorge Wilson 		if (nvlist_lookup_uint64(events[i], ZPOOL_HIST_TIME,
10998f18d1faSGeorge Wilson 		    &time) != 0)
11004445fffbSMatthew Ahrens 			goto next;
11018f18d1faSGeorge Wilson 		if (nvlist_lookup_string(events[i], ZPOOL_HIST_CMD,
11028f18d1faSGeorge Wilson 		    &cmd) != 0) {
11038f18d1faSGeorge Wilson 			if (nvlist_lookup_uint64(events[i],
11048f18d1faSGeorge Wilson 			    ZPOOL_HIST_INT_EVENT, &ievent) != 0)
11054445fffbSMatthew Ahrens 				goto next;
11068f18d1faSGeorge Wilson 			verify(nvlist_lookup_uint64(events[i],
11078f18d1faSGeorge Wilson 			    ZPOOL_HIST_TXG, &txg) == 0);
11088f18d1faSGeorge Wilson 			verify(nvlist_lookup_string(events[i],
11098f18d1faSGeorge Wilson 			    ZPOOL_HIST_INT_STR, &intstr) == 0);
11104445fffbSMatthew Ahrens 			if (ievent >= ZFS_NUM_LEGACY_HISTORY_EVENTS)
11114445fffbSMatthew Ahrens 				goto next;
11128f18d1faSGeorge Wilson 
11138f18d1faSGeorge Wilson 			(void) snprintf(internalstr,
11148f18d1faSGeorge Wilson 			    sizeof (internalstr),
11158f18d1faSGeorge Wilson 			    "[internal %s txg:%lld] %s",
11163f9d6ad7SLin Ling 			    zfs_history_event_names[ievent], txg,
11178f18d1faSGeorge Wilson 			    intstr);
11188f18d1faSGeorge Wilson 			cmd = internalstr;
11198f18d1faSGeorge Wilson 		}
11208f18d1faSGeorge Wilson 		tsec = time;
11218f18d1faSGeorge Wilson 		(void) localtime_r(&tsec, &t);
11228f18d1faSGeorge Wilson 		(void) strftime(tbuf, sizeof (tbuf), "%F.%T", &t);
11238f18d1faSGeorge Wilson 		(void) printf("%s %s\n", tbuf, cmd);
11244445fffbSMatthew Ahrens 		printed = B_TRUE;
11254445fffbSMatthew Ahrens 
11264445fffbSMatthew Ahrens next:
11274445fffbSMatthew Ahrens 		if (dump_opt['h'] > 1) {
11284445fffbSMatthew Ahrens 			if (!printed)
11294445fffbSMatthew Ahrens 				(void) printf("unrecognized record:\n");
11304445fffbSMatthew Ahrens 			dump_nvlist(events[i], 2);
11314445fffbSMatthew Ahrens 		}
11328f18d1faSGeorge Wilson 	}
11338f18d1faSGeorge Wilson }
11348f18d1faSGeorge Wilson 
1135fa9e4066Sahrens /*ARGSUSED*/
1136fa9e4066Sahrens static void
dump_dnode(objset_t * os,uint64_t object,void * data,size_t size)1137fa9e4066Sahrens dump_dnode(objset_t *os, uint64_t object, void *data, size_t size)
1138fa9e4066Sahrens {
1139fa9e4066Sahrens }
1140fa9e4066Sahrens 
1141fa9e4066Sahrens static uint64_t
blkid2offset(const dnode_phys_t * dnp,const blkptr_t * bp,const zbookmark_phys_t * zb)11427802d7bfSMatthew Ahrens blkid2offset(const dnode_phys_t *dnp, const blkptr_t *bp,
11437802d7bfSMatthew Ahrens     const zbookmark_phys_t *zb)
1144fa9e4066Sahrens {
1145b24ab676SJeff Bonwick 	if (dnp == NULL) {
1146b24ab676SJeff Bonwick 		ASSERT(zb->zb_level < 0);
1147b24ab676SJeff Bonwick 		if (zb->zb_object == 0)
1148b24ab676SJeff Bonwick 			return (zb->zb_blkid);
1149b24ab676SJeff Bonwick 		return (zb->zb_blkid * BP_GET_LSIZE(bp));
1150b24ab676SJeff Bonwick 	}
1151fa9e4066Sahrens 
1152b24ab676SJeff Bonwick 	ASSERT(zb->zb_level >= 0);
1153b24ab676SJeff Bonwick 
1154b24ab676SJeff Bonwick 	return ((zb->zb_blkid <<
1155b24ab676SJeff Bonwick 	    (zb->zb_level * (dnp->dn_indblkshift - SPA_BLKPTRSHIFT))) *
1156fa9e4066Sahrens 	    dnp->dn_datablkszsec << SPA_MINBLOCKSHIFT);
1157fa9e4066Sahrens }
1158fa9e4066Sahrens 
115944cd46caSbillm static void
snprintf_blkptr_compact(char * blkbuf,size_t buflen,const blkptr_t * bp)116043466aaeSMax Grossman snprintf_blkptr_compact(char *blkbuf, size_t buflen, const blkptr_t *bp)
116144cd46caSbillm {
1162cde58dbcSMatthew Ahrens 	const dva_t *dva = bp->blk_dva;
1163b24ab676SJeff Bonwick 	int ndvas = dump_opt['d'] > 5 ? BP_GET_NDVAS(bp) : 1;
1164b24ab676SJeff Bonwick 
1165490d05b9SMatthew Ahrens 	if (dump_opt['b'] >= 6) {
116643466aaeSMax Grossman 		snprintf_blkptr(blkbuf, buflen, bp);
1167b24ab676SJeff Bonwick 		return;
1168b24ab676SJeff Bonwick 	}
116944cd46caSbillm 
11705d7b4d43SMatthew Ahrens 	if (BP_IS_EMBEDDED(bp)) {
11715d7b4d43SMatthew Ahrens 		(void) sprintf(blkbuf,
11725d7b4d43SMatthew Ahrens 		    "EMBEDDED et=%u %llxL/%llxP B=%llu",
11735d7b4d43SMatthew Ahrens 		    (int)BPE_GET_ETYPE(bp),
11745d7b4d43SMatthew Ahrens 		    (u_longlong_t)BPE_GET_LSIZE(bp),
11755d7b4d43SMatthew Ahrens 		    (u_longlong_t)BPE_GET_PSIZE(bp),
11765d7b4d43SMatthew Ahrens 		    (u_longlong_t)bp->blk_birth);
11775d7b4d43SMatthew Ahrens 		return;
11785d7b4d43SMatthew Ahrens 	}
117944cd46caSbillm 
11805d7b4d43SMatthew Ahrens 	blkbuf[0] = '\0';
1181b24ab676SJeff Bonwick 	for (int i = 0; i < ndvas; i++)
118243466aaeSMax Grossman 		(void) snprintf(blkbuf + strlen(blkbuf),
118343466aaeSMax Grossman 		    buflen - strlen(blkbuf), "%llu:%llx:%llx ",
118444cd46caSbillm 		    (u_longlong_t)DVA_GET_VDEV(&dva[i]),
118544cd46caSbillm 		    (u_longlong_t)DVA_GET_OFFSET(&dva[i]),
118644cd46caSbillm 		    (u_longlong_t)DVA_GET_ASIZE(&dva[i]));
118744cd46caSbillm 
118843466aaeSMax Grossman 	if (BP_IS_HOLE(bp)) {
118943466aaeSMax Grossman 		(void) snprintf(blkbuf + strlen(blkbuf),
119070163ac5SPrakash Surya 		    buflen - strlen(blkbuf),
119170163ac5SPrakash Surya 		    "%llxL B=%llu",
119270163ac5SPrakash Surya 		    (u_longlong_t)BP_GET_LSIZE(bp),
119343466aaeSMax Grossman 		    (u_longlong_t)bp->blk_birth);
119443466aaeSMax Grossman 	} else {
119543466aaeSMax Grossman 		(void) snprintf(blkbuf + strlen(blkbuf),
119643466aaeSMax Grossman 		    buflen - strlen(blkbuf),
1197b24ab676SJeff Bonwick 		    "%llxL/%llxP F=%llu B=%llu/%llu",
119844cd46caSbillm 		    (u_longlong_t)BP_GET_LSIZE(bp),
119944cd46caSbillm 		    (u_longlong_t)BP_GET_PSIZE(bp),
12005d7b4d43SMatthew Ahrens 		    (u_longlong_t)BP_GET_FILL(bp),
1201b24ab676SJeff Bonwick 		    (u_longlong_t)bp->blk_birth,
1202b24ab676SJeff Bonwick 		    (u_longlong_t)BP_PHYSICAL_BIRTH(bp));
120344cd46caSbillm 	}
120443466aaeSMax Grossman }
120544cd46caSbillm 
120688b7b0f2SMatthew Ahrens static void
print_indirect(blkptr_t * bp,const zbookmark_phys_t * zb,const dnode_phys_t * dnp)12077802d7bfSMatthew Ahrens print_indirect(blkptr_t *bp, const zbookmark_phys_t *zb,
120888b7b0f2SMatthew Ahrens     const dnode_phys_t *dnp)
1209fa9e4066Sahrens {
121088b7b0f2SMatthew Ahrens 	char blkbuf[BP_SPRINTF_LEN];
1211fa9e4066Sahrens 	int l;
1212fa9e4066Sahrens 
12135d7b4d43SMatthew Ahrens 	if (!BP_IS_EMBEDDED(bp)) {
1214fa9e4066Sahrens 		ASSERT3U(BP_GET_TYPE(bp), ==, dnp->dn_type);
1215fa9e4066Sahrens 		ASSERT3U(BP_GET_LEVEL(bp), ==, zb->zb_level);
12165d7b4d43SMatthew Ahrens 	}
1217fa9e4066Sahrens 
1218b24ab676SJeff Bonwick 	(void) printf("%16llx ", (u_longlong_t)blkid2offset(dnp, bp, zb));
1219fa9e4066Sahrens 
1220fa9e4066Sahrens 	ASSERT(zb->zb_level >= 0);
1221fa9e4066Sahrens 
1222fa9e4066Sahrens 	for (l = dnp->dn_nlevels - 1; l >= -1; l--) {
1223fa9e4066Sahrens 		if (l == zb->zb_level) {
122488b7b0f2SMatthew Ahrens 			(void) printf("L%llx", (u_longlong_t)zb->zb_level);
1225fa9e4066Sahrens 		} else {
122688b7b0f2SMatthew Ahrens 			(void) printf(" ");
1227fa9e4066Sahrens 		}
1228fa9e4066Sahrens 	}
1229fa9e4066Sahrens 
123043466aaeSMax Grossman 	snprintf_blkptr_compact(blkbuf, sizeof (blkbuf), bp);
123144cd46caSbillm 	(void) printf("%s\n", blkbuf);
1232fa9e4066Sahrens }
1233fa9e4066Sahrens 
123488b7b0f2SMatthew Ahrens static int
visit_indirect(spa_t * spa,const dnode_phys_t * dnp,blkptr_t * bp,const zbookmark_phys_t * zb)123588b7b0f2SMatthew Ahrens visit_indirect(spa_t *spa, const dnode_phys_t *dnp,
12367802d7bfSMatthew Ahrens     blkptr_t *bp, const zbookmark_phys_t *zb)
123788b7b0f2SMatthew Ahrens {
1238e4161df6SVictor Latushkin 	int err = 0;
123988b7b0f2SMatthew Ahrens 
124088b7b0f2SMatthew Ahrens 	if (bp->blk_birth == 0)
124188b7b0f2SMatthew Ahrens 		return (0);
124288b7b0f2SMatthew Ahrens 
124388b7b0f2SMatthew Ahrens 	print_indirect(bp, zb, dnp);
124488b7b0f2SMatthew Ahrens 
124543466aaeSMax Grossman 	if (BP_GET_LEVEL(bp) > 0 && !BP_IS_HOLE(bp)) {
12467adb730bSGeorge Wilson 		arc_flags_t flags = ARC_FLAG_WAIT;
124788b7b0f2SMatthew Ahrens 		int i;
124888b7b0f2SMatthew Ahrens 		blkptr_t *cbp;
124988b7b0f2SMatthew Ahrens 		int epb = BP_GET_LSIZE(bp) >> SPA_BLKPTRSHIFT;
125088b7b0f2SMatthew Ahrens 		arc_buf_t *buf;
125188b7b0f2SMatthew Ahrens 		uint64_t fill = 0;
125288b7b0f2SMatthew Ahrens 
12531b912ec7SGeorge Wilson 		err = arc_read(NULL, spa, bp, arc_getbuf_func, &buf,
125488b7b0f2SMatthew Ahrens 		    ZIO_PRIORITY_ASYNC_READ, ZIO_FLAG_CANFAIL, &flags, zb);
125588b7b0f2SMatthew Ahrens 		if (err)
125688b7b0f2SMatthew Ahrens 			return (err);
12573f9d6ad7SLin Ling 		ASSERT(buf->b_data);
125888b7b0f2SMatthew Ahrens 
125988b7b0f2SMatthew Ahrens 		/* recursively visit blocks below this */
126088b7b0f2SMatthew Ahrens 		cbp = buf->b_data;
126188b7b0f2SMatthew Ahrens 		for (i = 0; i < epb; i++, cbp++) {
12627802d7bfSMatthew Ahrens 			zbookmark_phys_t czb;
126388b7b0f2SMatthew Ahrens 
126488b7b0f2SMatthew Ahrens 			SET_BOOKMARK(&czb, zb->zb_objset, zb->zb_object,
126588b7b0f2SMatthew Ahrens 			    zb->zb_level - 1,
126688b7b0f2SMatthew Ahrens 			    zb->zb_blkid * epb + i);
126788b7b0f2SMatthew Ahrens 			err = visit_indirect(spa, dnp, cbp, &czb);
126888b7b0f2SMatthew Ahrens 			if (err)
126988b7b0f2SMatthew Ahrens 				break;
12705d7b4d43SMatthew Ahrens 			fill += BP_GET_FILL(cbp);
127188b7b0f2SMatthew Ahrens 		}
12728ad4d6ddSJeff Bonwick 		if (!err)
12735d7b4d43SMatthew Ahrens 			ASSERT3U(fill, ==, BP_GET_FILL(bp));
127488b7b0f2SMatthew Ahrens 		(void) arc_buf_remove_ref(buf, &buf);
127588b7b0f2SMatthew Ahrens 	}
127688b7b0f2SMatthew Ahrens 
127788b7b0f2SMatthew Ahrens 	return (err);
1278fa9e4066Sahrens }
1279fa9e4066Sahrens 
1280fa9e4066Sahrens /*ARGSUSED*/
1281fa9e4066Sahrens static void
dump_indirect(dnode_t * dn)128288b7b0f2SMatthew Ahrens dump_indirect(dnode_t *dn)
1283fa9e4066Sahrens {
128488b7b0f2SMatthew Ahrens 	dnode_phys_t *dnp = dn->dn_phys;
128588b7b0f2SMatthew Ahrens 	int j;
12867802d7bfSMatthew Ahrens 	zbookmark_phys_t czb;
1287fa9e4066Sahrens 
1288fa9e4066Sahrens 	(void) printf("Indirect blocks:\n");
1289fa9e4066Sahrens 
1290503ad85cSMatthew Ahrens 	SET_BOOKMARK(&czb, dmu_objset_id(dn->dn_objset),
129188b7b0f2SMatthew Ahrens 	    dn->dn_object, dnp->dn_nlevels - 1, 0);
129288b7b0f2SMatthew Ahrens 	for (j = 0; j < dnp->dn_nblkptr; j++) {
129388b7b0f2SMatthew Ahrens 		czb.zb_blkid = j;
1294503ad85cSMatthew Ahrens 		(void) visit_indirect(dmu_objset_spa(dn->dn_objset), dnp,
129588b7b0f2SMatthew Ahrens 		    &dnp->dn_blkptr[j], &czb);
129688b7b0f2SMatthew Ahrens 	}
1297fa9e4066Sahrens 
1298fa9e4066Sahrens 	(void) printf("\n");
1299fa9e4066Sahrens }
1300fa9e4066Sahrens 
1301fa9e4066Sahrens /*ARGSUSED*/
1302fa9e4066Sahrens static void
dump_dsl_dir(objset_t * os,uint64_t object,void * data,size_t size)1303fa9e4066Sahrens dump_dsl_dir(objset_t *os, uint64_t object, void *data, size_t size)
1304fa9e4066Sahrens {
1305fa9e4066Sahrens 	dsl_dir_phys_t *dd = data;
1306fa9e4066Sahrens 	time_t crtime;
13073f9d6ad7SLin Ling 	char nice[32];
1308fa9e4066Sahrens 
1309*a66b28a3SJason King 	/* make sure nicenum has enough space */
1310*a66b28a3SJason King 	CTASSERT(sizeof (nice) >= NN_NUMBUF_SZ);
1311*a66b28a3SJason King 
1312fa9e4066Sahrens 	if (dd == NULL)
1313fa9e4066Sahrens 		return;
1314fa9e4066Sahrens 
1315da6c28aaSamw 	ASSERT3U(size, >=, sizeof (dsl_dir_phys_t));
1316fa9e4066Sahrens 
1317fa9e4066Sahrens 	crtime = dd->dd_creation_time;
1318fa9e4066Sahrens 	(void) printf("\t\tcreation_time = %s", ctime(&crtime));
1319fa9e4066Sahrens 	(void) printf("\t\thead_dataset_obj = %llu\n",
1320fa9e4066Sahrens 	    (u_longlong_t)dd->dd_head_dataset_obj);
1321fa9e4066Sahrens 	(void) printf("\t\tparent_dir_obj = %llu\n",
1322fa9e4066Sahrens 	    (u_longlong_t)dd->dd_parent_obj);
13233cb34c60Sahrens 	(void) printf("\t\torigin_obj = %llu\n",
13243cb34c60Sahrens 	    (u_longlong_t)dd->dd_origin_obj);
1325fa9e4066Sahrens 	(void) printf("\t\tchild_dir_zapobj = %llu\n",
1326fa9e4066Sahrens 	    (u_longlong_t)dd->dd_child_dir_zapobj);
1327*a66b28a3SJason King 	zdb_nicenum(dd->dd_used_bytes, nice, sizeof (nice));
132874e7dc98SMatthew Ahrens 	(void) printf("\t\tused_bytes = %s\n", nice);
1329*a66b28a3SJason King 	zdb_nicenum(dd->dd_compressed_bytes, nice, sizeof (nice));
133074e7dc98SMatthew Ahrens 	(void) printf("\t\tcompressed_bytes = %s\n", nice);
1331*a66b28a3SJason King 	zdb_nicenum(dd->dd_uncompressed_bytes, nice, sizeof (nice));
133274e7dc98SMatthew Ahrens 	(void) printf("\t\tuncompressed_bytes = %s\n", nice);
1333*a66b28a3SJason King 	zdb_nicenum(dd->dd_quota, nice, sizeof (nice));
133474e7dc98SMatthew Ahrens 	(void) printf("\t\tquota = %s\n", nice);
1335*a66b28a3SJason King 	zdb_nicenum(dd->dd_reserved, nice, sizeof (nice));
133674e7dc98SMatthew Ahrens 	(void) printf("\t\treserved = %s\n", nice);
1337fa9e4066Sahrens 	(void) printf("\t\tprops_zapobj = %llu\n",
1338fa9e4066Sahrens 	    (u_longlong_t)dd->dd_props_zapobj);
1339ecd6cf80Smarks 	(void) printf("\t\tdeleg_zapobj = %llu\n",
1340ecd6cf80Smarks 	    (u_longlong_t)dd->dd_deleg_zapobj);
134174e7dc98SMatthew Ahrens 	(void) printf("\t\tflags = %llx\n",
134274e7dc98SMatthew Ahrens 	    (u_longlong_t)dd->dd_flags);
134374e7dc98SMatthew Ahrens 
134474e7dc98SMatthew Ahrens #define	DO(which) \
1345*a66b28a3SJason King 	zdb_nicenum(dd->dd_used_breakdown[DD_USED_ ## which], nice, \
1346*a66b28a3SJason King 	    sizeof (nice)); \
134774e7dc98SMatthew Ahrens 	(void) printf("\t\tused_breakdown[" #which "] = %s\n", nice)
134874e7dc98SMatthew Ahrens 	DO(HEAD);
134974e7dc98SMatthew Ahrens 	DO(SNAP);
135074e7dc98SMatthew Ahrens 	DO(CHILD);
135174e7dc98SMatthew Ahrens 	DO(CHILD_RSRV);
135274e7dc98SMatthew Ahrens 	DO(REFRSRV);
135374e7dc98SMatthew Ahrens #undef DO
1354fa9e4066Sahrens }
1355fa9e4066Sahrens 
1356fa9e4066Sahrens /*ARGSUSED*/
1357fa9e4066Sahrens static void
dump_dsl_dataset(objset_t * os,uint64_t object,void * data,size_t size)1358fa9e4066Sahrens dump_dsl_dataset(objset_t *os, uint64_t object, void *data, size_t size)
1359fa9e4066Sahrens {
1360fa9e4066Sahrens 	dsl_dataset_phys_t *ds = data;
1361fa9e4066Sahrens 	time_t crtime;
13623f9d6ad7SLin Ling 	char used[32], compressed[32], uncompressed[32], unique[32];
1363fbabab8fSmaybee 	char blkbuf[BP_SPRINTF_LEN];
1364fa9e4066Sahrens 
1365*a66b28a3SJason King 	/* make sure nicenum has enough space */
1366*a66b28a3SJason King 	CTASSERT(sizeof (used) >= NN_NUMBUF_SZ);
1367*a66b28a3SJason King 	CTASSERT(sizeof (compressed) >= NN_NUMBUF_SZ);
1368*a66b28a3SJason King 	CTASSERT(sizeof (uncompressed) >= NN_NUMBUF_SZ);
1369*a66b28a3SJason King 	CTASSERT(sizeof (unique) >= NN_NUMBUF_SZ);
1370*a66b28a3SJason King 
1371fa9e4066Sahrens 	if (ds == NULL)
1372fa9e4066Sahrens 		return;
1373fa9e4066Sahrens 
1374fa9e4066Sahrens 	ASSERT(size == sizeof (*ds));
1375fa9e4066Sahrens 	crtime = ds->ds_creation_time;
1376*a66b28a3SJason King 	zdb_nicenum(ds->ds_referenced_bytes, used, sizeof (used));
1377*a66b28a3SJason King 	zdb_nicenum(ds->ds_compressed_bytes, compressed, sizeof (compressed));
1378*a66b28a3SJason King 	zdb_nicenum(ds->ds_uncompressed_bytes, uncompressed,
1379*a66b28a3SJason King 	    sizeof (uncompressed));
1380*a66b28a3SJason King 	zdb_nicenum(ds->ds_unique_bytes, unique, sizeof (unique));
138143466aaeSMax Grossman 	snprintf_blkptr(blkbuf, sizeof (blkbuf), &ds->ds_bp);
1382fa9e4066Sahrens 
1383088f3894Sahrens 	(void) printf("\t\tdir_obj = %llu\n",
1384fa9e4066Sahrens 	    (u_longlong_t)ds->ds_dir_obj);
1385fa9e4066Sahrens 	(void) printf("\t\tprev_snap_obj = %llu\n",
1386fa9e4066Sahrens 	    (u_longlong_t)ds->ds_prev_snap_obj);
1387fa9e4066Sahrens 	(void) printf("\t\tprev_snap_txg = %llu\n",
1388fa9e4066Sahrens 	    (u_longlong_t)ds->ds_prev_snap_txg);
1389fa9e4066Sahrens 	(void) printf("\t\tnext_snap_obj = %llu\n",
1390fa9e4066Sahrens 	    (u_longlong_t)ds->ds_next_snap_obj);
1391fa9e4066Sahrens 	(void) printf("\t\tsnapnames_zapobj = %llu\n",
1392fa9e4066Sahrens 	    (u_longlong_t)ds->ds_snapnames_zapobj);
1393fa9e4066Sahrens 	(void) printf("\t\tnum_children = %llu\n",
1394fa9e4066Sahrens 	    (u_longlong_t)ds->ds_num_children);
1395842727c2SChris Kirby 	(void) printf("\t\tuserrefs_obj = %llu\n",
1396842727c2SChris Kirby 	    (u_longlong_t)ds->ds_userrefs_obj);
1397fa9e4066Sahrens 	(void) printf("\t\tcreation_time = %s", ctime(&crtime));
1398fa9e4066Sahrens 	(void) printf("\t\tcreation_txg = %llu\n",
1399fa9e4066Sahrens 	    (u_longlong_t)ds->ds_creation_txg);
1400fa9e4066Sahrens 	(void) printf("\t\tdeadlist_obj = %llu\n",
1401fa9e4066Sahrens 	    (u_longlong_t)ds->ds_deadlist_obj);
1402fa9e4066Sahrens 	(void) printf("\t\tused_bytes = %s\n", used);
1403fa9e4066Sahrens 	(void) printf("\t\tcompressed_bytes = %s\n", compressed);
1404fa9e4066Sahrens 	(void) printf("\t\tuncompressed_bytes = %s\n", uncompressed);
1405fa9e4066Sahrens 	(void) printf("\t\tunique = %s\n", unique);
1406fa9e4066Sahrens 	(void) printf("\t\tfsid_guid = %llu\n",
1407fa9e4066Sahrens 	    (u_longlong_t)ds->ds_fsid_guid);
1408fa9e4066Sahrens 	(void) printf("\t\tguid = %llu\n",
1409fa9e4066Sahrens 	    (u_longlong_t)ds->ds_guid);
141099653d4eSeschrock 	(void) printf("\t\tflags = %llx\n",
141199653d4eSeschrock 	    (u_longlong_t)ds->ds_flags);
1412088f3894Sahrens 	(void) printf("\t\tnext_clones_obj = %llu\n",
1413088f3894Sahrens 	    (u_longlong_t)ds->ds_next_clones_obj);
1414bb0ade09Sahrens 	(void) printf("\t\tprops_obj = %llu\n",
1415bb0ade09Sahrens 	    (u_longlong_t)ds->ds_props_obj);
1416fa9e4066Sahrens 	(void) printf("\t\tbp = %s\n", blkbuf);
1417fa9e4066Sahrens }
1418fa9e4066Sahrens 
1419cde58dbcSMatthew Ahrens /* ARGSUSED */
1420cde58dbcSMatthew Ahrens static int
dump_bptree_cb(void * arg,const blkptr_t * bp,dmu_tx_t * tx)1421ad135b5dSChristopher Siden dump_bptree_cb(void *arg, const blkptr_t *bp, dmu_tx_t *tx)
1422ad135b5dSChristopher Siden {
1423ad135b5dSChristopher Siden 	char blkbuf[BP_SPRINTF_LEN];
1424ad135b5dSChristopher Siden 
1425ad135b5dSChristopher Siden 	if (bp->blk_birth != 0) {
142643466aaeSMax Grossman 		snprintf_blkptr(blkbuf, sizeof (blkbuf), bp);
1427ad135b5dSChristopher Siden 		(void) printf("\t%s\n", blkbuf);
1428ad135b5dSChristopher Siden 	}
1429ad135b5dSChristopher Siden 	return (0);
1430ad135b5dSChristopher Siden }
1431ad135b5dSChristopher Siden 
1432ad135b5dSChristopher Siden static void
dump_bptree(objset_t * os,uint64_t obj,char * name)1433ad135b5dSChristopher Siden dump_bptree(objset_t *os, uint64_t obj, char *name)
1434ad135b5dSChristopher Siden {
1435ad135b5dSChristopher Siden 	char bytes[32];
1436ad135b5dSChristopher Siden 	bptree_phys_t *bt;
1437ad135b5dSChristopher Siden 	dmu_buf_t *db;
1438ad135b5dSChristopher Siden 
1439*a66b28a3SJason King 	/* make sure nicenum has enough space */
1440*a66b28a3SJason King 	CTASSERT(sizeof (bytes) >= NN_NUMBUF_SZ);
1441*a66b28a3SJason King 
1442ad135b5dSChristopher Siden 	if (dump_opt['d'] < 3)
1443ad135b5dSChristopher Siden 		return;
1444ad135b5dSChristopher Siden 
1445b420f3adSRichard Lowe 	VERIFY3U(0, ==, dmu_bonus_hold(os, obj, FTAG, &db));
1446ad135b5dSChristopher Siden 	bt = db->db_data;
1447*a66b28a3SJason King 	zdb_nicenum(bt->bt_bytes, bytes, sizeof (bytes));
1448ad135b5dSChristopher Siden 	(void) printf("\n    %s: %llu datasets, %s\n",
1449ad135b5dSChristopher Siden 	    name, (unsigned long long)(bt->bt_end - bt->bt_begin), bytes);
1450ad135b5dSChristopher Siden 	dmu_buf_rele(db, FTAG);
1451ad135b5dSChristopher Siden 
1452ad135b5dSChristopher Siden 	if (dump_opt['d'] < 5)
1453ad135b5dSChristopher Siden 		return;
1454ad135b5dSChristopher Siden 
1455ad135b5dSChristopher Siden 	(void) printf("\n");
1456ad135b5dSChristopher Siden 
1457ad135b5dSChristopher Siden 	(void) bptree_iterate(os, obj, B_FALSE, dump_bptree_cb, NULL, NULL);
1458ad135b5dSChristopher Siden }
1459ad135b5dSChristopher Siden 
1460ad135b5dSChristopher Siden /* ARGSUSED */
1461ad135b5dSChristopher Siden static int
dump_bpobj_cb(void * arg,const blkptr_t * bp,dmu_tx_t * tx)1462cde58dbcSMatthew Ahrens dump_bpobj_cb(void *arg, const blkptr_t *bp, dmu_tx_t *tx)
1463fa9e4066Sahrens {
1464cde58dbcSMatthew Ahrens 	char blkbuf[BP_SPRINTF_LEN];
1465cde58dbcSMatthew Ahrens 
1466cde58dbcSMatthew Ahrens 	ASSERT(bp->blk_birth != 0);
146743466aaeSMax Grossman 	snprintf_blkptr_compact(blkbuf, sizeof (blkbuf), bp);
1468cde58dbcSMatthew Ahrens 	(void) printf("\t%s\n", blkbuf);
1469cde58dbcSMatthew Ahrens 	return (0);
1470cde58dbcSMatthew Ahrens }
1471cde58dbcSMatthew Ahrens 
1472cde58dbcSMatthew Ahrens static void
dump_full_bpobj(bpobj_t * bpo,char * name,int indent)1473732885fcSMatthew Ahrens dump_full_bpobj(bpobj_t *bpo, char *name, int indent)
1474cde58dbcSMatthew Ahrens {
14753f9d6ad7SLin Ling 	char bytes[32];
14763f9d6ad7SLin Ling 	char comp[32];
14773f9d6ad7SLin Ling 	char uncomp[32];
1478fa9e4066Sahrens 
1479*a66b28a3SJason King 	/* make sure nicenum has enough space */
1480*a66b28a3SJason King 	CTASSERT(sizeof (bytes) >= NN_NUMBUF_SZ);
1481*a66b28a3SJason King 	CTASSERT(sizeof (comp) >= NN_NUMBUF_SZ);
1482*a66b28a3SJason King 	CTASSERT(sizeof (uncomp) >= NN_NUMBUF_SZ);
1483*a66b28a3SJason King 
1484fa9e4066Sahrens 	if (dump_opt['d'] < 3)
1485fa9e4066Sahrens 		return;
1486fa9e4066Sahrens 
1487*a66b28a3SJason King 	zdb_nicenum(bpo->bpo_phys->bpo_bytes, bytes, sizeof (bytes));
1488d0475637SMatthew Ahrens 	if (bpo->bpo_havesubobj && bpo->bpo_phys->bpo_subobjs != 0) {
1489*a66b28a3SJason King 		zdb_nicenum(bpo->bpo_phys->bpo_comp, comp, sizeof (comp));
1490*a66b28a3SJason King 		zdb_nicenum(bpo->bpo_phys->bpo_uncomp, uncomp, sizeof (uncomp));
1491d0475637SMatthew Ahrens 		(void) printf("    %*s: object %llu, %llu local blkptrs, "
1492732885fcSMatthew Ahrens 		    "%llu subobjs in object %llu, %s (%s/%s comp)\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,
1496cde58dbcSMatthew Ahrens 		    (u_longlong_t)bpo->bpo_phys->bpo_num_subobjs,
1497732885fcSMatthew Ahrens 		    (u_longlong_t)bpo->bpo_phys->bpo_subobjs,
149899653d4eSeschrock 		    bytes, comp, uncomp);
1499d0475637SMatthew Ahrens 
1500d0475637SMatthew Ahrens 		for (uint64_t i = 0; i < bpo->bpo_phys->bpo_num_subobjs; i++) {
1501d0475637SMatthew Ahrens 			uint64_t subobj;
1502d0475637SMatthew Ahrens 			bpobj_t subbpo;
1503d0475637SMatthew Ahrens 			int error;
1504d0475637SMatthew Ahrens 			VERIFY0(dmu_read(bpo->bpo_os,
1505d0475637SMatthew Ahrens 			    bpo->bpo_phys->bpo_subobjs,
1506d0475637SMatthew Ahrens 			    i * sizeof (subobj), sizeof (subobj), &subobj, 0));
1507d0475637SMatthew Ahrens 			error = bpobj_open(&subbpo, bpo->bpo_os, subobj);
1508d0475637SMatthew Ahrens 			if (error != 0) {
1509d0475637SMatthew Ahrens 				(void) printf("ERROR %u while trying to open "
1510d0475637SMatthew Ahrens 				    "subobj id %llu\n",
1511d0475637SMatthew Ahrens 				    error, (u_longlong_t)subobj);
1512d0475637SMatthew Ahrens 				continue;
1513d0475637SMatthew Ahrens 			}
1514732885fcSMatthew Ahrens 			dump_full_bpobj(&subbpo, "subobj", indent + 1);
151577061867SMatthew Ahrens 			bpobj_close(&subbpo);
1516d0475637SMatthew Ahrens 		}
151799653d4eSeschrock 	} else {
1518d0475637SMatthew Ahrens 		(void) printf("    %*s: object %llu, %llu blkptrs, %s\n",
1519d0475637SMatthew Ahrens 		    indent * 8, name,
1520d0475637SMatthew Ahrens 		    (u_longlong_t)bpo->bpo_object,
1521d0475637SMatthew Ahrens 		    (u_longlong_t)bpo->bpo_phys->bpo_num_blkptrs,
1522d0475637SMatthew Ahrens 		    bytes);
152399653d4eSeschrock 	}
1524fa9e4066Sahrens 
1525cde58dbcSMatthew Ahrens 	if (dump_opt['d'] < 5)
1526fa9e4066Sahrens 		return;
1527fa9e4066Sahrens 
1528fa9e4066Sahrens 
1529d0475637SMatthew Ahrens 	if (indent == 0) {
1530cde58dbcSMatthew Ahrens 		(void) bpobj_iterate_nofree(bpo, dump_bpobj_cb, NULL, NULL);
1531d0475637SMatthew Ahrens 		(void) printf("\n");
1532d0475637SMatthew Ahrens 	}
1533fa9e4066Sahrens }
1534fa9e4066Sahrens 
1535cde58dbcSMatthew Ahrens static void
dump_deadlist(dsl_deadlist_t * dl)1536cde58dbcSMatthew Ahrens dump_deadlist(dsl_deadlist_t *dl)
1537cde58dbcSMatthew Ahrens {
1538cde58dbcSMatthew Ahrens 	dsl_deadlist_entry_t *dle;
1539d0475637SMatthew Ahrens 	uint64_t unused;
1540cde58dbcSMatthew Ahrens 	char bytes[32];
1541cde58dbcSMatthew Ahrens 	char comp[32];
1542cde58dbcSMatthew Ahrens 	char uncomp[32];
1543cde58dbcSMatthew Ahrens 
1544*a66b28a3SJason King 	/* make sure nicenum has enough space */
1545*a66b28a3SJason King 	CTASSERT(sizeof (bytes) >= NN_NUMBUF_SZ);
1546*a66b28a3SJason King 	CTASSERT(sizeof (comp) >= NN_NUMBUF_SZ);
1547*a66b28a3SJason King 	CTASSERT(sizeof (uncomp) >= NN_NUMBUF_SZ);
1548*a66b28a3SJason King 
1549cde58dbcSMatthew Ahrens 	if (dump_opt['d'] < 3)
1550cde58dbcSMatthew Ahrens 		return;
1551cde58dbcSMatthew Ahrens 
155290c76c66SMatthew Ahrens 	if (dl->dl_oldfmt) {
1553732885fcSMatthew Ahrens 		dump_full_bpobj(&dl->dl_bpobj, "old-format deadlist", 0);
155490c76c66SMatthew Ahrens 		return;
155590c76c66SMatthew Ahrens 	}
155690c76c66SMatthew Ahrens 
1557*a66b28a3SJason King 	zdb_nicenum(dl->dl_phys->dl_used, bytes, sizeof (bytes));
1558*a66b28a3SJason King 	zdb_nicenum(dl->dl_phys->dl_comp, comp, sizeof (comp));
1559*a66b28a3SJason King 	zdb_nicenum(dl->dl_phys->dl_uncomp, uncomp, sizeof (uncomp));
1560cde58dbcSMatthew Ahrens 	(void) printf("\n    Deadlist: %s (%s/%s comp)\n",
1561cde58dbcSMatthew Ahrens 	    bytes, comp, uncomp);
1562cde58dbcSMatthew Ahrens 
1563cde58dbcSMatthew Ahrens 	if (dump_opt['d'] < 4)
1564cde58dbcSMatthew Ahrens 		return;
1565cde58dbcSMatthew Ahrens 
1566cde58dbcSMatthew Ahrens 	(void) printf("\n");
1567cde58dbcSMatthew Ahrens 
1568d0475637SMatthew Ahrens 	/* force the tree to be loaded */
1569d0475637SMatthew Ahrens 	dsl_deadlist_space_range(dl, 0, UINT64_MAX, &unused, &unused, &unused);
1570d0475637SMatthew Ahrens 
1571cde58dbcSMatthew Ahrens 	for (dle = avl_first(&dl->dl_tree); dle;
1572cde58dbcSMatthew Ahrens 	    dle = AVL_NEXT(&dl->dl_tree, dle)) {
1573d0475637SMatthew Ahrens 		if (dump_opt['d'] >= 5) {
1574d0475637SMatthew Ahrens 			char buf[128];
1575d0475637SMatthew Ahrens 			(void) snprintf(buf, sizeof (buf), "mintxg %llu -> ",
1576d0475637SMatthew Ahrens 			    (longlong_t)dle->dle_mintxg,
1577d0475637SMatthew Ahrens 			    (longlong_t)dle->dle_bpobj.bpo_object);
1578d0475637SMatthew Ahrens 
1579732885fcSMatthew Ahrens 			dump_full_bpobj(&dle->dle_bpobj, buf, 0);
1580d0475637SMatthew Ahrens 		} else {
1581cde58dbcSMatthew Ahrens 			(void) printf("mintxg %llu -> obj %llu\n",
1582cde58dbcSMatthew Ahrens 			    (longlong_t)dle->dle_mintxg,
1583cde58dbcSMatthew Ahrens 			    (longlong_t)dle->dle_bpobj.bpo_object);
1584cde58dbcSMatthew Ahrens 
1585d0475637SMatthew Ahrens 		}
1586cde58dbcSMatthew Ahrens 	}
1587fa9e4066Sahrens }
1588fa9e4066Sahrens 
1589e0d35c44Smarks static avl_tree_t idx_tree;
1590e0d35c44Smarks static avl_tree_t domain_tree;
1591e0d35c44Smarks static boolean_t fuid_table_loaded;
15920a586ceaSMark Shellenbaum static boolean_t sa_loaded;
15930a586ceaSMark Shellenbaum sa_attr_type_t *sa_attr_table;
1594e0d35c44Smarks 
1595e0d35c44Smarks static void
fuid_table_destroy()1596e0d35c44Smarks fuid_table_destroy()
1597e0d35c44Smarks {
1598e0d35c44Smarks 	if (fuid_table_loaded) {
1599e0d35c44Smarks 		zfs_fuid_table_destroy(&idx_tree, &domain_tree);
1600e0d35c44Smarks 		fuid_table_loaded = B_FALSE;
1601e0d35c44Smarks 	}
1602e0d35c44Smarks }
1603e0d35c44Smarks 
1604e0d35c44Smarks /*
1605e0d35c44Smarks  * print uid or gid information.
1606e0d35c44Smarks  * For normal POSIX id just the id is printed in decimal format.
1607e0d35c44Smarks  * For CIFS files with FUID the fuid is printed in hex followed by
1608d0475637SMatthew Ahrens  * the domain-rid string.
1609e0d35c44Smarks  */
1610e0d35c44Smarks static void
print_idstr(uint64_t id,const char * id_type)1611e0d35c44Smarks print_idstr(uint64_t id, const char *id_type)
1612e0d35c44Smarks {
1613e0d35c44Smarks 	if (FUID_INDEX(id)) {
1614e0d35c44Smarks 		char *domain;
1615e0d35c44Smarks 
1616e0d35c44Smarks 		domain = zfs_fuid_idx_domain(&idx_tree, FUID_INDEX(id));
1617e0d35c44Smarks 		(void) printf("\t%s     %llx [%s-%d]\n", id_type,
1618e0d35c44Smarks 		    (u_longlong_t)id, domain, (int)FUID_RID(id));
1619e0d35c44Smarks 	} else {
1620e0d35c44Smarks 		(void) printf("\t%s     %llu\n", id_type, (u_longlong_t)id);
1621e0d35c44Smarks 	}
1622e0d35c44Smarks 
1623e0d35c44Smarks }
1624e0d35c44Smarks 
1625e0d35c44Smarks static void
dump_uidgid(objset_t * os,uint64_t uid,uint64_t gid)16260a586ceaSMark Shellenbaum dump_uidgid(objset_t *os, uint64_t uid, uint64_t gid)
1627e0d35c44Smarks {
1628e0d35c44Smarks 	uint32_t uid_idx, gid_idx;
1629e0d35c44Smarks 
16300a586ceaSMark Shellenbaum 	uid_idx = FUID_INDEX(uid);
16310a586ceaSMark Shellenbaum 	gid_idx = FUID_INDEX(gid);
1632e0d35c44Smarks 
1633e0d35c44Smarks 	/* Load domain table, if not already loaded */
1634e0d35c44Smarks 	if (!fuid_table_loaded && (uid_idx || gid_idx)) {
1635e0d35c44Smarks 		uint64_t fuid_obj;
1636e0d35c44Smarks 
1637e0d35c44Smarks 		/* first find the fuid object.  It lives in the master node */
1638e0d35c44Smarks 		VERIFY(zap_lookup(os, MASTER_NODE_OBJ, ZFS_FUID_TABLES,
1639e0d35c44Smarks 		    8, 1, &fuid_obj) == 0);
164089459e17SMark Shellenbaum 		zfs_fuid_avl_tree_create(&idx_tree, &domain_tree);
1641e0d35c44Smarks 		(void) zfs_fuid_table_load(os, fuid_obj,
1642e0d35c44Smarks 		    &idx_tree, &domain_tree);
1643e0d35c44Smarks 		fuid_table_loaded = B_TRUE;
1644e0d35c44Smarks 	}
1645e0d35c44Smarks 
16460a586ceaSMark Shellenbaum 	print_idstr(uid, "uid");
16470a586ceaSMark Shellenbaum 	print_idstr(gid, "gid");
1648e0d35c44Smarks }
1649e0d35c44Smarks 
1650fa9e4066Sahrens /*ARGSUSED*/
1651fa9e4066Sahrens static void
dump_znode(objset_t * os,uint64_t object,void * data,size_t size)1652fa9e4066Sahrens dump_znode(objset_t *os, uint64_t object, void *data, size_t size)
1653fa9e4066Sahrens {
1654fa9e4066Sahrens 	char path[MAXPATHLEN * 2];	/* allow for xattr and failure prefix */
16550a586ceaSMark Shellenbaum 	sa_handle_t *hdl;
16560a586ceaSMark Shellenbaum 	uint64_t xattr, rdev, gen;
16570a586ceaSMark Shellenbaum 	uint64_t uid, gid, mode, fsize, parent, links;
16588f2529deSMark Shellenbaum 	uint64_t pflags;
16590a586ceaSMark Shellenbaum 	uint64_t acctm[2], modtm[2], chgtm[2], crtm[2];
16600a586ceaSMark Shellenbaum 	time_t z_crtime, z_atime, z_mtime, z_ctime;
16618f2529deSMark Shellenbaum 	sa_bulk_attr_t bulk[12];
16620a586ceaSMark Shellenbaum 	int idx = 0;
166355434c77Sek110237 	int error;
1664fa9e4066Sahrens 
16650a586ceaSMark Shellenbaum 	if (!sa_loaded) {
16660a586ceaSMark Shellenbaum 		uint64_t sa_attrs = 0;
16670a586ceaSMark Shellenbaum 		uint64_t version;
16680a586ceaSMark Shellenbaum 
16690a586ceaSMark Shellenbaum 		VERIFY(zap_lookup(os, MASTER_NODE_OBJ, ZPL_VERSION_STR,
16700a586ceaSMark Shellenbaum 		    8, 1, &version) == 0);
16710a586ceaSMark Shellenbaum 		if (version >= ZPL_VERSION_SA) {
16720a586ceaSMark Shellenbaum 			VERIFY(zap_lookup(os, MASTER_NODE_OBJ, ZFS_SA_ATTRS,
16730a586ceaSMark Shellenbaum 			    8, 1, &sa_attrs) == 0);
16740a586ceaSMark Shellenbaum 		}
16751d8ccc7bSMark Shellenbaum 		if ((error = sa_setup(os, sa_attrs, zfs_attr_table,
16761d8ccc7bSMark Shellenbaum 		    ZPL_END, &sa_attr_table)) != 0) {
16771d8ccc7bSMark Shellenbaum 			(void) printf("sa_setup failed errno %d, can't "
16781d8ccc7bSMark Shellenbaum 			    "display znode contents\n", error);
16791d8ccc7bSMark Shellenbaum 			return;
16801d8ccc7bSMark Shellenbaum 		}
16810a586ceaSMark Shellenbaum 		sa_loaded = B_TRUE;
16820a586ceaSMark Shellenbaum 	}
16830a586ceaSMark Shellenbaum 
16840a586ceaSMark Shellenbaum 	if (sa_handle_get(os, object, NULL, SA_HDL_PRIVATE, &hdl)) {
16850a586ceaSMark Shellenbaum 		(void) printf("Failed to get handle for SA znode\n");
16860a586ceaSMark Shellenbaum 		return;
16870a586ceaSMark Shellenbaum 	}
16880a586ceaSMark Shellenbaum 
16890a586ceaSMark Shellenbaum 	SA_ADD_BULK_ATTR(bulk, idx, sa_attr_table[ZPL_UID], NULL, &uid, 8);
16900a586ceaSMark Shellenbaum 	SA_ADD_BULK_ATTR(bulk, idx, sa_attr_table[ZPL_GID], NULL, &gid, 8);
16910a586ceaSMark Shellenbaum 	SA_ADD_BULK_ATTR(bulk, idx, sa_attr_table[ZPL_LINKS], NULL,
16920a586ceaSMark Shellenbaum 	    &links, 8);
16930a586ceaSMark Shellenbaum 	SA_ADD_BULK_ATTR(bulk, idx, sa_attr_table[ZPL_GEN], NULL, &gen, 8);
16940a586ceaSMark Shellenbaum 	SA_ADD_BULK_ATTR(bulk, idx, sa_attr_table[ZPL_MODE], NULL,
16950a586ceaSMark Shellenbaum 	    &mode, 8);
16960a586ceaSMark Shellenbaum 	SA_ADD_BULK_ATTR(bulk, idx, sa_attr_table[ZPL_PARENT],
16970a586ceaSMark Shellenbaum 	    NULL, &parent, 8);
16980a586ceaSMark Shellenbaum 	SA_ADD_BULK_ATTR(bulk, idx, sa_attr_table[ZPL_SIZE], NULL,
16990a586ceaSMark Shellenbaum 	    &fsize, 8);
17000a586ceaSMark Shellenbaum 	SA_ADD_BULK_ATTR(bulk, idx, sa_attr_table[ZPL_ATIME], NULL,
17010a586ceaSMark Shellenbaum 	    acctm, 16);
17020a586ceaSMark Shellenbaum 	SA_ADD_BULK_ATTR(bulk, idx, sa_attr_table[ZPL_MTIME], NULL,
17030a586ceaSMark Shellenbaum 	    modtm, 16);
17040a586ceaSMark Shellenbaum 	SA_ADD_BULK_ATTR(bulk, idx, sa_attr_table[ZPL_CRTIME], NULL,
17050a586ceaSMark Shellenbaum 	    crtm, 16);
17060a586ceaSMark Shellenbaum 	SA_ADD_BULK_ATTR(bulk, idx, sa_attr_table[ZPL_CTIME], NULL,
17070a586ceaSMark Shellenbaum 	    chgtm, 16);
17088f2529deSMark Shellenbaum 	SA_ADD_BULK_ATTR(bulk, idx, sa_attr_table[ZPL_FLAGS], NULL,
17098f2529deSMark Shellenbaum 	    &pflags, 8);
17100a586ceaSMark Shellenbaum 
17110a586ceaSMark Shellenbaum 	if (sa_bulk_lookup(hdl, bulk, idx)) {
17120a586ceaSMark Shellenbaum 		(void) sa_handle_destroy(hdl);
17130a586ceaSMark Shellenbaum 		return;
17140a586ceaSMark Shellenbaum 	}
1715fa9e4066Sahrens 
171655434c77Sek110237 	error = zfs_obj_to_path(os, object, path, sizeof (path));
171755434c77Sek110237 	if (error != 0) {
171855434c77Sek110237 		(void) snprintf(path, sizeof (path), "\?\?\?<object#%llu>",
171955434c77Sek110237 		    (u_longlong_t)object);
172055434c77Sek110237 	}
1721fa9e4066Sahrens 	if (dump_opt['d'] < 3) {
172255434c77Sek110237 		(void) printf("\t%s\n", path);
17230a586ceaSMark Shellenbaum 		(void) sa_handle_destroy(hdl);
1724fa9e4066Sahrens 		return;
1725fa9e4066Sahrens 	}
1726fa9e4066Sahrens 
17270a586ceaSMark Shellenbaum 	z_crtime = (time_t)crtm[0];
17280a586ceaSMark Shellenbaum 	z_atime = (time_t)acctm[0];
17290a586ceaSMark Shellenbaum 	z_mtime = (time_t)modtm[0];
17300a586ceaSMark Shellenbaum 	z_ctime = (time_t)chgtm[0];
1731fa9e4066Sahrens 
173255434c77Sek110237 	(void) printf("\tpath	%s\n", path);
17330a586ceaSMark Shellenbaum 	dump_uidgid(os, uid, gid);
1734fa9e4066Sahrens 	(void) printf("\tatime	%s", ctime(&z_atime));
1735fa9e4066Sahrens 	(void) printf("\tmtime	%s", ctime(&z_mtime));
1736fa9e4066Sahrens 	(void) printf("\tctime	%s", ctime(&z_ctime));
1737fa9e4066Sahrens 	(void) printf("\tcrtime	%s", ctime(&z_crtime));
17380a586ceaSMark Shellenbaum 	(void) printf("\tgen	%llu\n", (u_longlong_t)gen);
17390a586ceaSMark Shellenbaum 	(void) printf("\tmode	%llo\n", (u_longlong_t)mode);
17400a586ceaSMark Shellenbaum 	(void) printf("\tsize	%llu\n", (u_longlong_t)fsize);
17410a586ceaSMark Shellenbaum 	(void) printf("\tparent	%llu\n", (u_longlong_t)parent);
17420a586ceaSMark Shellenbaum 	(void) printf("\tlinks	%llu\n", (u_longlong_t)links);
17438f2529deSMark Shellenbaum 	(void) printf("\tpflags	%llx\n", (u_longlong_t)pflags);
17440a586ceaSMark Shellenbaum 	if (sa_lookup(hdl, sa_attr_table[ZPL_XATTR], &xattr,
17450a586ceaSMark Shellenbaum 	    sizeof (uint64_t)) == 0)
17460a586ceaSMark Shellenbaum 		(void) printf("\txattr	%llu\n", (u_longlong_t)xattr);
17470a586ceaSMark Shellenbaum 	if (sa_lookup(hdl, sa_attr_table[ZPL_RDEV], &rdev,
17480a586ceaSMark Shellenbaum 	    sizeof (uint64_t)) == 0)
17490a586ceaSMark Shellenbaum 		(void) printf("\trdev	0x%016llx\n", (u_longlong_t)rdev);
17500a586ceaSMark Shellenbaum 	sa_handle_destroy(hdl);
1751fa9e4066Sahrens }
1752fa9e4066Sahrens 
1753fa9e4066Sahrens /*ARGSUSED*/
1754fa9e4066Sahrens static void
dump_acl(objset_t * os,uint64_t object,void * data,size_t size)1755fa9e4066Sahrens dump_acl(objset_t *os, uint64_t object, void *data, size_t size)
1756fa9e4066Sahrens {
1757fa9e4066Sahrens }
1758fa9e4066Sahrens 
1759fa9e4066Sahrens /*ARGSUSED*/
1760fa9e4066Sahrens static void
dump_dmu_objset(objset_t * os,uint64_t object,void * data,size_t size)1761fa9e4066Sahrens dump_dmu_objset(objset_t *os, uint64_t object, void *data, size_t size)
1762fa9e4066Sahrens {
1763fa9e4066Sahrens }
1764fa9e4066Sahrens 
17656de8f417SVictor Latushkin static object_viewer_t *object_viewer[DMU_OT_NUMTYPES + 1] = {
1766fa9e4066Sahrens 	dump_none,		/* unallocated			*/
1767fa9e4066Sahrens 	dump_zap,		/* object directory		*/
1768fa9e4066Sahrens 	dump_uint64,		/* object array			*/
1769fa9e4066Sahrens 	dump_none,		/* packed nvlist		*/
1770fa9e4066Sahrens 	dump_packed_nvlist,	/* packed nvlist size		*/
1771732885fcSMatthew Ahrens 	dump_none,		/* bpobj			*/
1772732885fcSMatthew Ahrens 	dump_bpobj,		/* bpobj header			*/
1773fa9e4066Sahrens 	dump_none,		/* SPA space map header		*/
1774fa9e4066Sahrens 	dump_none,		/* SPA space map		*/
1775fa9e4066Sahrens 	dump_none,		/* ZIL intent log		*/
1776fa9e4066Sahrens 	dump_dnode,		/* DMU dnode			*/
1777fa9e4066Sahrens 	dump_dmu_objset,	/* DMU objset			*/
1778fa9e4066Sahrens 	dump_dsl_dir,		/* DSL directory		*/
1779fa9e4066Sahrens 	dump_zap,		/* DSL directory child map	*/
1780fa9e4066Sahrens 	dump_zap,		/* DSL dataset snap map		*/
1781fa9e4066Sahrens 	dump_zap,		/* DSL props			*/
1782fa9e4066Sahrens 	dump_dsl_dataset,	/* DSL dataset			*/
1783fa9e4066Sahrens 	dump_znode,		/* ZFS znode			*/
1784da6c28aaSamw 	dump_acl,		/* ZFS V0 ACL			*/
1785fa9e4066Sahrens 	dump_uint8,		/* ZFS plain file		*/
1786e7437265Sahrens 	dump_zpldir,		/* ZFS directory		*/
1787fa9e4066Sahrens 	dump_zap,		/* ZFS master node		*/
1788fa9e4066Sahrens 	dump_zap,		/* ZFS delete queue		*/
1789fa9e4066Sahrens 	dump_uint8,		/* zvol object			*/
1790fa9e4066Sahrens 	dump_zap,		/* zvol prop			*/
1791fa9e4066Sahrens 	dump_uint8,		/* other uint8[]		*/
1792fa9e4066Sahrens 	dump_uint64,		/* other uint64[]		*/
1793fa9e4066Sahrens 	dump_zap,		/* other ZAP			*/
1794ea8dc4b6Seschrock 	dump_zap,		/* persistent error log		*/
179506eeb2adSek110237 	dump_uint8,		/* SPA history			*/
17964445fffbSMatthew Ahrens 	dump_history_offsets,	/* SPA history offsets		*/
1797b1b8ab34Slling 	dump_zap,		/* Pool properties		*/
1798ecd6cf80Smarks 	dump_zap,		/* DSL permissions		*/
1799da6c28aaSamw 	dump_acl,		/* ZFS ACL			*/
1800da6c28aaSamw 	dump_uint8,		/* ZFS SYSACL			*/
1801da6c28aaSamw 	dump_none,		/* FUID nvlist			*/
1802da6c28aaSamw 	dump_packed_nvlist,	/* FUID nvlist size		*/
1803088f3894Sahrens 	dump_zap,		/* DSL dataset next clones	*/
1804088f3894Sahrens 	dump_zap,		/* DSL scrub queue		*/
180514843421SMatthew Ahrens 	dump_zap,		/* ZFS user/group used		*/
180614843421SMatthew Ahrens 	dump_zap,		/* ZFS user/group quota		*/
1807842727c2SChris Kirby 	dump_zap,		/* snapshot refcount tags	*/
1808486ae710SMatthew Ahrens 	dump_ddt_zap,		/* DDT ZAP object		*/
1809b24ab676SJeff Bonwick 	dump_zap,		/* DDT statistics		*/
18100a586ceaSMark Shellenbaum 	dump_znode,		/* SA object			*/
18110a586ceaSMark Shellenbaum 	dump_zap,		/* SA Master Node		*/
18120a586ceaSMark Shellenbaum 	dump_sa_attrs,		/* SA attribute registration	*/
18130a586ceaSMark Shellenbaum 	dump_sa_layouts,	/* SA attribute layouts		*/
18143f9d6ad7SLin Ling 	dump_zap,		/* DSL scrub translations	*/
18153f9d6ad7SLin Ling 	dump_none,		/* fake dedup BP		*/
1816cde58dbcSMatthew Ahrens 	dump_zap,		/* deadlist			*/
1817cde58dbcSMatthew Ahrens 	dump_none,		/* deadlist hdr			*/
1818cde58dbcSMatthew Ahrens 	dump_zap,		/* dsl clones			*/
1819732885fcSMatthew Ahrens 	dump_bpobj_subobjs,	/* bpobj subobjs		*/
18200a586ceaSMark Shellenbaum 	dump_unknown,		/* Unknown type, must be last	*/
1821fa9e4066Sahrens };
1822fa9e4066Sahrens 
1823fa9e4066Sahrens static void
dump_object(objset_t * os,uint64_t object,int verbosity,int * print_header)1824fa9e4066Sahrens dump_object(objset_t *os, uint64_t object, int verbosity, int *print_header)
1825fa9e4066Sahrens {
1826fa9e4066Sahrens 	dmu_buf_t *db = NULL;
1827fa9e4066Sahrens 	dmu_object_info_t doi;
1828fa9e4066Sahrens 	dnode_t *dn;
1829fa9e4066Sahrens 	void *bonus = NULL;
1830fa9e4066Sahrens 	size_t bsize = 0;
18313f9d6ad7SLin Ling 	char iblk[32], dblk[32], lsize[32], asize[32], fill[32];
18323f9d6ad7SLin Ling 	char bonus_size[32];
1833fa9e4066Sahrens 	char aux[50];
1834fa9e4066Sahrens 	int error;
1835fa9e4066Sahrens 
1836*a66b28a3SJason King 	/* make sure nicenum has enough space */
1837*a66b28a3SJason King 	CTASSERT(sizeof (iblk) >= NN_NUMBUF_SZ);
1838*a66b28a3SJason King 	CTASSERT(sizeof (dblk) >= NN_NUMBUF_SZ);
1839*a66b28a3SJason King 	CTASSERT(sizeof (lsize) >= NN_NUMBUF_SZ);
1840*a66b28a3SJason King 	CTASSERT(sizeof (asize) >= NN_NUMBUF_SZ);
1841*a66b28a3SJason King 	CTASSERT(sizeof (bonus_size) >= NN_NUMBUF_SZ);
1842*a66b28a3SJason King 
1843fa9e4066Sahrens 	if (*print_header) {
1844b24ab676SJeff Bonwick 		(void) printf("\n%10s  %3s  %5s  %5s  %5s  %5s  %6s  %s\n",
1845b24ab676SJeff Bonwick 		    "Object", "lvl", "iblk", "dblk", "dsize", "lsize",
1846b24ab676SJeff Bonwick 		    "%full", "type");
1847fa9e4066Sahrens 		*print_header = 0;
1848fa9e4066Sahrens 	}
1849fa9e4066Sahrens 
1850fa9e4066Sahrens 	if (object == 0) {
1851744947dcSTom Erickson 		dn = DMU_META_DNODE(os);
1852fa9e4066Sahrens 	} else {
1853ea8dc4b6Seschrock 		error = dmu_bonus_hold(os, object, FTAG, &db);
1854ea8dc4b6Seschrock 		if (error)
1855ea8dc4b6Seschrock 			fatal("dmu_bonus_hold(%llu) failed, errno %u",
1856ea8dc4b6Seschrock 			    object, error);
1857fa9e4066Sahrens 		bonus = db->db_data;
1858fa9e4066Sahrens 		bsize = db->db_size;
1859744947dcSTom Erickson 		dn = DB_DNODE((dmu_buf_impl_t *)db);
1860fa9e4066Sahrens 	}
1861fa9e4066Sahrens 	dmu_object_info_from_dnode(dn, &doi);
1862fa9e4066Sahrens 
1863*a66b28a3SJason King 	zdb_nicenum(doi.doi_metadata_block_size, iblk, sizeof (iblk));
1864*a66b28a3SJason King 	zdb_nicenum(doi.doi_data_block_size, dblk, sizeof (dblk));
1865*a66b28a3SJason King 	zdb_nicenum(doi.doi_max_offset, lsize, sizeof (lsize));
1866*a66b28a3SJason King 	zdb_nicenum(doi.doi_physical_blocks_512 << 9, asize, sizeof (asize));
1867*a66b28a3SJason King 	zdb_nicenum(doi.doi_bonus_size, bonus_size, sizeof (bonus_size));
1868b24ab676SJeff Bonwick 	(void) sprintf(fill, "%6.2f", 100.0 * doi.doi_fill_count *
1869bbfd46c4SJeff Bonwick 	    doi.doi_data_block_size / (object == 0 ? DNODES_PER_BLOCK : 1) /
1870bbfd46c4SJeff Bonwick 	    doi.doi_max_offset);
1871fa9e4066Sahrens 
1872fa9e4066Sahrens 	aux[0] = '\0';
1873fa9e4066Sahrens 
1874e7437265Sahrens 	if (doi.doi_checksum != ZIO_CHECKSUM_INHERIT || verbosity >= 6) {
1875fa9e4066Sahrens 		(void) snprintf(aux + strlen(aux), sizeof (aux), " (K=%s)",
18766de8f417SVictor Latushkin 		    ZDB_CHECKSUM_NAME(doi.doi_checksum));
1877e7437265Sahrens 	}
1878fa9e4066Sahrens 
1879e7437265Sahrens 	if (doi.doi_compress != ZIO_COMPRESS_INHERIT || verbosity >= 6) {
1880fa9e4066Sahrens 		(void) snprintf(aux + strlen(aux), sizeof (aux), " (Z=%s)",
18816de8f417SVictor Latushkin 		    ZDB_COMPRESS_NAME(doi.doi_compress));
1882e7437265Sahrens 	}
1883fa9e4066Sahrens 
1884b24ab676SJeff Bonwick 	(void) printf("%10lld  %3u  %5s  %5s  %5s  %5s  %6s  %s%s\n",
1885b24ab676SJeff Bonwick 	    (u_longlong_t)object, doi.doi_indirection, iblk, dblk,
1886b24ab676SJeff Bonwick 	    asize, lsize, fill, ZDB_OT_NAME(doi.doi_type), aux);
1887fa9e4066Sahrens 
1888fa9e4066Sahrens 	if (doi.doi_bonus_type != DMU_OT_NONE && verbosity > 3) {
1889b24ab676SJeff Bonwick 		(void) printf("%10s  %3s  %5s  %5s  %5s  %5s  %6s  %s\n",
1890b24ab676SJeff Bonwick 		    "", "", "", "", "", bonus_size, "bonus",
18916de8f417SVictor Latushkin 		    ZDB_OT_NAME(doi.doi_bonus_type));
1892fa9e4066Sahrens 	}
1893fa9e4066Sahrens 
1894fa9e4066Sahrens 	if (verbosity >= 4) {
18950a586ceaSMark Shellenbaum 		(void) printf("\tdnode flags: %s%s%s\n",
189614843421SMatthew Ahrens 		    (dn->dn_phys->dn_flags & DNODE_FLAG_USED_BYTES) ?
189714843421SMatthew Ahrens 		    "USED_BYTES " : "",
189814843421SMatthew Ahrens 		    (dn->dn_phys->dn_flags & DNODE_FLAG_USERUSED_ACCOUNTED) ?
18990a586ceaSMark Shellenbaum 		    "USERUSED_ACCOUNTED " : "",
19000a586ceaSMark Shellenbaum 		    (dn->dn_phys->dn_flags & DNODE_FLAG_SPILL_BLKPTR) ?
19010a586ceaSMark Shellenbaum 		    "SPILL_BLKPTR" : "");
190214843421SMatthew Ahrens 		(void) printf("\tdnode maxblkid: %llu\n",
190314843421SMatthew Ahrens 		    (longlong_t)dn->dn_phys->dn_maxblkid);
190414843421SMatthew Ahrens 
19056de8f417SVictor Latushkin 		object_viewer[ZDB_OT_TYPE(doi.doi_bonus_type)](os, object,
19066de8f417SVictor Latushkin 		    bonus, bsize);
19076de8f417SVictor Latushkin 		object_viewer[ZDB_OT_TYPE(doi.doi_type)](os, object, NULL, 0);
1908fa9e4066Sahrens 		*print_header = 1;
1909fa9e4066Sahrens 	}
1910fa9e4066Sahrens 
1911fa9e4066Sahrens 	if (verbosity >= 5)
191288b7b0f2SMatthew Ahrens 		dump_indirect(dn);
1913fa9e4066Sahrens 
1914fa9e4066Sahrens 	if (verbosity >= 5) {
1915fa9e4066Sahrens 		/*
1916fa9e4066Sahrens 		 * Report the list of segments that comprise the object.
1917fa9e4066Sahrens 		 */
1918fa9e4066Sahrens 		uint64_t start = 0;
1919fa9e4066Sahrens 		uint64_t end;
1920fa9e4066Sahrens 		uint64_t blkfill = 1;
1921fa9e4066Sahrens 		int minlvl = 1;
1922fa9e4066Sahrens 
1923fa9e4066Sahrens 		if (dn->dn_type == DMU_OT_DNODE) {
1924fa9e4066Sahrens 			minlvl = 0;
1925fa9e4066Sahrens 			blkfill = DNODES_PER_BLOCK;
1926fa9e4066Sahrens 		}
1927fa9e4066Sahrens 
1928fa9e4066Sahrens 		for (;;) {
19293f9d6ad7SLin Ling 			char segsize[32];
1930*a66b28a3SJason King 			/* make sure nicenum has enough space */
1931*a66b28a3SJason King 			CTASSERT(sizeof (segsize) >= NN_NUMBUF_SZ);
1932cdb0ab79Smaybee 			error = dnode_next_offset(dn,
1933cdb0ab79Smaybee 			    0, &start, minlvl, blkfill, 0);
1934fa9e4066Sahrens 			if (error)
1935fa9e4066Sahrens 				break;
1936fa9e4066Sahrens 			end = start;
1937cdb0ab79Smaybee 			error = dnode_next_offset(dn,
1938cdb0ab79Smaybee 			    DNODE_FIND_HOLE, &end, minlvl, blkfill, 0);
1939*a66b28a3SJason King 			zdb_nicenum(end - start, segsize, sizeof (segsize));
1940fa9e4066Sahrens 			(void) printf("\t\tsegment [%016llx, %016llx)"
1941fa9e4066Sahrens 			    " size %5s\n", (u_longlong_t)start,
1942fa9e4066Sahrens 			    (u_longlong_t)end, segsize);
1943fa9e4066Sahrens 			if (error)
1944fa9e4066Sahrens 				break;
1945fa9e4066Sahrens 			start = end;
1946fa9e4066Sahrens 		}
1947fa9e4066Sahrens 	}
1948fa9e4066Sahrens 
1949fa9e4066Sahrens 	if (db != NULL)
1950ea8dc4b6Seschrock 		dmu_buf_rele(db, FTAG);
1951fa9e4066Sahrens }
1952fa9e4066Sahrens 
1953fa9e4066Sahrens static char *objset_types[DMU_OST_NUMTYPES] = {
1954fa9e4066Sahrens 	"NONE", "META", "ZPL", "ZVOL", "OTHER", "ANY" };
1955fa9e4066Sahrens 
1956fa9e4066Sahrens static void
dump_dir(objset_t * os)1957fa9e4066Sahrens dump_dir(objset_t *os)
1958fa9e4066Sahrens {
1959fa9e4066Sahrens 	dmu_objset_stats_t dds;
1960fa9e4066Sahrens 	uint64_t object, object_count;
1961a2eea2e1Sahrens 	uint64_t refdbytes, usedobjs, scratch;
19623f9d6ad7SLin Ling 	char numbuf[32];
196314843421SMatthew Ahrens 	char blkbuf[BP_SPRINTF_LEN + 20];
1964a1988827SMatthew Ahrens 	char osname[ZFS_MAX_DATASET_NAME_LEN];
1965fa9e4066Sahrens 	char *type = "UNKNOWN";
1966fa9e4066Sahrens 	int verbosity = dump_opt['d'];
1967fa9e4066Sahrens 	int print_header = 1;
1968fa9e4066Sahrens 	int i, error;
1969fa9e4066Sahrens 
1970*a66b28a3SJason King 	/* make sure nicenum has enough space */
1971*a66b28a3SJason King 	CTASSERT(sizeof (numbuf) >= NN_NUMBUF_SZ);
1972*a66b28a3SJason King 
19733b2aab18SMatthew Ahrens 	dsl_pool_config_enter(dmu_objset_pool(os), FTAG);
1974a2eea2e1Sahrens 	dmu_objset_fast_stat(os, &dds);
19753b2aab18SMatthew Ahrens 	dsl_pool_config_exit(dmu_objset_pool(os), FTAG);
1976fa9e4066Sahrens 
1977fa9e4066Sahrens 	if (dds.dds_type < DMU_OST_NUMTYPES)
1978fa9e4066Sahrens 		type = objset_types[dds.dds_type];
1979fa9e4066Sahrens 
1980fa9e4066Sahrens 	if (dds.dds_type == DMU_OST_META) {
1981fa9e4066Sahrens 		dds.dds_creation_txg = TXG_INITIAL;
19825d7b4d43SMatthew Ahrens 		usedobjs = BP_GET_FILL(os->os_rootbp);
1983c1379625SJustin T. Gibbs 		refdbytes = dsl_dir_phys(os->os_spa->spa_dsl_pool->dp_mos_dir)->
1984c1379625SJustin T. Gibbs 		    dd_used_bytes;
1985a2eea2e1Sahrens 	} else {
1986a2eea2e1Sahrens 		dmu_objset_space(os, &refdbytes, &scratch, &usedobjs, &scratch);
1987fa9e4066Sahrens 	}
1988fa9e4066Sahrens 
19895d7b4d43SMatthew Ahrens 	ASSERT3U(usedobjs, ==, BP_GET_FILL(os->os_rootbp));
1990fa9e4066Sahrens 
1991*a66b28a3SJason King 	zdb_nicenum(refdbytes, numbuf, sizeof (numbuf));
1992fa9e4066Sahrens 
1993fa9e4066Sahrens 	if (verbosity >= 4) {
199443466aaeSMax Grossman 		(void) snprintf(blkbuf, sizeof (blkbuf), ", rootbp ");
199543466aaeSMax Grossman 		(void) snprintf_blkptr(blkbuf + strlen(blkbuf),
199643466aaeSMax Grossman 		    sizeof (blkbuf) - strlen(blkbuf), os->os_rootbp);
1997fa9e4066Sahrens 	} else {
1998fa9e4066Sahrens 		blkbuf[0] = '\0';
1999fa9e4066Sahrens 	}
2000fa9e4066Sahrens 
2001fa9e4066Sahrens 	dmu_objset_name(os, osname);
2002fa9e4066Sahrens 
2003a2eea2e1Sahrens 	(void) printf("Dataset %s [%s], ID %llu, cr_txg %llu, "
2004fa9e4066Sahrens 	    "%s, %llu objects%s\n",
2005fa9e4066Sahrens 	    osname, type, (u_longlong_t)dmu_objset_id(os),
2006fa9e4066Sahrens 	    (u_longlong_t)dds.dds_creation_txg,
2007a2eea2e1Sahrens 	    numbuf, (u_longlong_t)usedobjs, blkbuf);
2008fa9e4066Sahrens 
2009b24ab676SJeff Bonwick 	if (zopt_objects != 0) {
2010b24ab676SJeff Bonwick 		for (i = 0; i < zopt_objects; i++)
2011b24ab676SJeff Bonwick 			dump_object(os, zopt_object[i], verbosity,
2012b24ab676SJeff Bonwick 			    &print_header);
2013b24ab676SJeff Bonwick 		(void) printf("\n");
2014b24ab676SJeff Bonwick 		return;
2015b24ab676SJeff Bonwick 	}
2016b24ab676SJeff Bonwick 
2017b24ab676SJeff Bonwick 	if (dump_opt['i'] != 0 || verbosity >= 2)
2018fa9e4066Sahrens 		dump_intent_log(dmu_objset_zil(os));
2019fa9e4066Sahrens 
2020fa9e4066Sahrens 	if (dmu_objset_ds(os) != NULL)
2021cde58dbcSMatthew Ahrens 		dump_deadlist(&dmu_objset_ds(os)->ds_deadlist);
2022fa9e4066Sahrens 
2023fa9e4066Sahrens 	if (verbosity < 2)
2024fa9e4066Sahrens 		return;
2025fa9e4066Sahrens 
202643466aaeSMax Grossman 	if (BP_IS_HOLE(os->os_rootbp))
2027088f3894Sahrens 		return;
2028088f3894Sahrens 
2029fa9e4066Sahrens 	dump_object(os, 0, verbosity, &print_header);
203014843421SMatthew Ahrens 	object_count = 0;
2031744947dcSTom Erickson 	if (DMU_USERUSED_DNODE(os) != NULL &&
2032744947dcSTom Erickson 	    DMU_USERUSED_DNODE(os)->dn_type != 0) {
203314843421SMatthew Ahrens 		dump_object(os, DMU_USERUSED_OBJECT, verbosity, &print_header);
203414843421SMatthew Ahrens 		dump_object(os, DMU_GROUPUSED_OBJECT, verbosity, &print_header);
203514843421SMatthew Ahrens 	}
2036fa9e4066Sahrens 
2037fa9e4066Sahrens 	object = 0;
20386754306eSahrens 	while ((error = dmu_object_next(os, &object, B_FALSE, 0)) == 0) {
2039fa9e4066Sahrens 		dump_object(os, object, verbosity, &print_header);
2040fa9e4066Sahrens 		object_count++;
2041fa9e4066Sahrens 	}
2042fa9e4066Sahrens 
2043a2eea2e1Sahrens 	ASSERT3U(object_count, ==, usedobjs);
2044fa9e4066Sahrens 
2045fa9e4066Sahrens 	(void) printf("\n");
2046fa9e4066Sahrens 
2047ccba0801SRich Morris 	if (error != ESRCH) {
2048ccba0801SRich Morris 		(void) fprintf(stderr, "dmu_object_next() = %d\n", error);
2049ccba0801SRich Morris 		abort();
2050ccba0801SRich Morris 	}
2051fa9e4066Sahrens }
2052fa9e4066Sahrens 
2053fa9e4066Sahrens static void
dump_uberblock(uberblock_t * ub,const char * header,const char * footer)205453b9a4a9SVictor Latushkin dump_uberblock(uberblock_t *ub, const char *header, const char *footer)
2055fa9e4066Sahrens {
2056fa9e4066Sahrens 	time_t timestamp = ub->ub_timestamp;
2057fa9e4066Sahrens 
205853b9a4a9SVictor Latushkin 	(void) printf(header ? header : "");
2059fa9e4066Sahrens 	(void) printf("\tmagic = %016llx\n", (u_longlong_t)ub->ub_magic);
2060fa9e4066Sahrens 	(void) printf("\tversion = %llu\n", (u_longlong_t)ub->ub_version);
2061fa9e4066Sahrens 	(void) printf("\ttxg = %llu\n", (u_longlong_t)ub->ub_txg);
2062fa9e4066Sahrens 	(void) printf("\tguid_sum = %llu\n", (u_longlong_t)ub->ub_guid_sum);
2063fa9e4066Sahrens 	(void) printf("\ttimestamp = %llu UTC = %s",
2064fa9e4066Sahrens 	    (u_longlong_t)ub->ub_timestamp, asctime(localtime(&timestamp)));
2065fa9e4066Sahrens 	if (dump_opt['u'] >= 3) {
2066fbabab8fSmaybee 		char blkbuf[BP_SPRINTF_LEN];
206743466aaeSMax Grossman 		snprintf_blkptr(blkbuf, sizeof (blkbuf), &ub->ub_rootbp);
2068fa9e4066Sahrens 		(void) printf("\trootbp = %s\n", blkbuf);
2069fa9e4066Sahrens 	}
207053b9a4a9SVictor Latushkin 	(void) printf(footer ? footer : "");
2071fa9e4066Sahrens }
2072fa9e4066Sahrens 
2073fa9e4066Sahrens static void
dump_config(spa_t * spa)207407428bdfSVictor Latushkin dump_config(spa_t *spa)
2075fa9e4066Sahrens {
207607428bdfSVictor Latushkin 	dmu_buf_t *db;
207707428bdfSVictor Latushkin 	size_t nvsize = 0;
207807428bdfSVictor Latushkin 	int error = 0;
2079fa9e4066Sahrens 
208007428bdfSVictor Latushkin 
208107428bdfSVictor Latushkin 	error = dmu_bonus_hold(spa->spa_meta_objset,
208207428bdfSVictor Latushkin 	    spa->spa_config_object, FTAG, &db);
208307428bdfSVictor Latushkin 
208407428bdfSVictor Latushkin 	if (error == 0) {
208507428bdfSVictor Latushkin 		nvsize = *(uint64_t *)db->db_data;
208607428bdfSVictor Latushkin 		dmu_buf_rele(db, FTAG);
208707428bdfSVictor Latushkin 
208807428bdfSVictor Latushkin 		(void) printf("\nMOS Configuration:\n");
208907428bdfSVictor Latushkin 		dump_packed_nvlist(spa->spa_meta_objset,
209007428bdfSVictor Latushkin 		    spa->spa_config_object, (void *)&nvsize, 1);
209107428bdfSVictor Latushkin 	} else {
209207428bdfSVictor Latushkin 		(void) fprintf(stderr, "dmu_bonus_hold(%llu) failed, errno %d",
209307428bdfSVictor Latushkin 		    (u_longlong_t)spa->spa_config_object, error);
2094fa9e4066Sahrens 	}
2095fa9e4066Sahrens }
2096fa9e4066Sahrens 
2097fa9e4066Sahrens static void
dump_cachefile(const char * cachefile)2098c5904d13Seschrock dump_cachefile(const char *cachefile)
2099c5904d13Seschrock {
2100c5904d13Seschrock 	int fd;
2101c5904d13Seschrock 	struct stat64 statbuf;
2102c5904d13Seschrock 	char *buf;
2103c5904d13Seschrock 	nvlist_t *config;
2104c5904d13Seschrock 
2105c5904d13Seschrock 	if ((fd = open64(cachefile, O_RDONLY)) < 0) {
2106c5904d13Seschrock 		(void) printf("cannot open '%s': %s\n", cachefile,
2107c5904d13Seschrock 		    strerror(errno));
2108c5904d13Seschrock 		exit(1);
2109c5904d13Seschrock 	}
2110c5904d13Seschrock 
2111c5904d13Seschrock 	if (fstat64(fd, &statbuf) != 0) {
2112c5904d13Seschrock 		(void) printf("failed to stat '%s': %s\n", cachefile,
2113c5904d13Seschrock 		    strerror(errno));
2114c5904d13Seschrock 		exit(1);
2115c5904d13Seschrock 	}
2116c5904d13Seschrock 
2117c5904d13Seschrock 	if ((buf = malloc(statbuf.st_size)) == NULL) {
2118c5904d13Seschrock 		(void) fprintf(stderr, "failed to allocate %llu bytes\n",
2119c5904d13Seschrock 		    (u_longlong_t)statbuf.st_size);
2120c5904d13Seschrock 		exit(1);
2121c5904d13Seschrock 	}
2122c5904d13Seschrock 
2123c5904d13Seschrock 	if (read(fd, buf, statbuf.st_size) != statbuf.st_size) {
2124c5904d13Seschrock 		(void) fprintf(stderr, "failed to read %llu bytes\n",
2125c5904d13Seschrock 		    (u_longlong_t)statbuf.st_size);
2126c5904d13Seschrock 		exit(1);
2127c5904d13Seschrock 	}
2128c5904d13Seschrock 
2129c5904d13Seschrock 	(void) close(fd);
2130c5904d13Seschrock 
2131c5904d13Seschrock 	if (nvlist_unpack(buf, statbuf.st_size, &config, 0) != 0) {
2132c5904d13Seschrock 		(void) fprintf(stderr, "failed to unpack nvlist\n");
2133c5904d13Seschrock 		exit(1);
2134c5904d13Seschrock 	}
2135c5904d13Seschrock 
2136c5904d13Seschrock 	free(buf);
2137c5904d13Seschrock 
2138c5904d13Seschrock 	dump_nvlist(config, 0);
2139c5904d13Seschrock 
2140c5904d13Seschrock 	nvlist_free(config);
2141c5904d13Seschrock }
2142c5904d13Seschrock 
214353b9a4a9SVictor Latushkin #define	ZDB_MAX_UB_HEADER_SIZE 32
214453b9a4a9SVictor Latushkin 
214553b9a4a9SVictor Latushkin static void
dump_label_uberblocks(vdev_label_t * lbl,uint64_t ashift)214653b9a4a9SVictor Latushkin dump_label_uberblocks(vdev_label_t *lbl, uint64_t ashift)
214753b9a4a9SVictor Latushkin {
214853b9a4a9SVictor Latushkin 	vdev_t vd;
214953b9a4a9SVictor Latushkin 	vdev_t *vdp = &vd;
215053b9a4a9SVictor Latushkin 	char header[ZDB_MAX_UB_HEADER_SIZE];
215153b9a4a9SVictor Latushkin 
215253b9a4a9SVictor Latushkin 	vd.vdev_ashift = ashift;
215353b9a4a9SVictor Latushkin 	vdp->vdev_top = vdp;
215453b9a4a9SVictor Latushkin 
215553b9a4a9SVictor Latushkin 	for (int i = 0; i < VDEV_UBERBLOCK_COUNT(vdp); i++) {
215653b9a4a9SVictor Latushkin 		uint64_t uoff = VDEV_UBERBLOCK_OFFSET(vdp, i);
215753b9a4a9SVictor Latushkin 		uberblock_t *ub = (void *)((char *)lbl + uoff);
215853b9a4a9SVictor Latushkin 
215953b9a4a9SVictor Latushkin 		if (uberblock_verify(ub))
216053b9a4a9SVictor Latushkin 			continue;
216153b9a4a9SVictor Latushkin 		(void) snprintf(header, ZDB_MAX_UB_HEADER_SIZE,
216253b9a4a9SVictor Latushkin 		    "Uberblock[%d]\n", i);
216353b9a4a9SVictor Latushkin 		dump_uberblock(ub, header, "");
216453b9a4a9SVictor Latushkin 	}
216553b9a4a9SVictor Latushkin }
216653b9a4a9SVictor Latushkin 
2167c5904d13Seschrock static void
dump_label(const char * dev)2168fa9e4066Sahrens dump_label(const char *dev)
2169fa9e4066Sahrens {
2170fa9e4066Sahrens 	int fd;
2171fa9e4066Sahrens 	vdev_label_t label;
2172c6065d0fSGeorge Wilson 	char *path, *buf = label.vl_vdev_phys.vp_nvlist;
2173fa9e4066Sahrens 	size_t buflen = sizeof (label.vl_vdev_phys.vp_nvlist);
2174fa9e4066Sahrens 	struct stat64 statbuf;
217553b9a4a9SVictor Latushkin 	uint64_t psize, ashift;
2176c6065d0fSGeorge Wilson 	int len = strlen(dev) + 1;
2177fa9e4066Sahrens 
2178c6065d0fSGeorge Wilson 	if (strncmp(dev, "/dev/dsk/", 9) == 0) {
2179c6065d0fSGeorge Wilson 		len++;
2180c6065d0fSGeorge Wilson 		path = malloc(len);
2181c6065d0fSGeorge Wilson 		(void) snprintf(path, len, "%s%s", "/dev/rdsk/", dev + 9);
2182c6065d0fSGeorge Wilson 	} else {
2183c6065d0fSGeorge Wilson 		path = strdup(dev);
2184c6065d0fSGeorge Wilson 	}
2185c6065d0fSGeorge Wilson 
2186c6065d0fSGeorge Wilson 	if ((fd = open64(path, O_RDONLY)) < 0) {
2187c6065d0fSGeorge Wilson 		(void) printf("cannot open '%s': %s\n", path, strerror(errno));
2188c6065d0fSGeorge Wilson 		free(path);
2189fa9e4066Sahrens 		exit(1);
2190fa9e4066Sahrens 	}
2191fa9e4066Sahrens 
2192fa9e4066Sahrens 	if (fstat64(fd, &statbuf) != 0) {
2193c6065d0fSGeorge Wilson 		(void) printf("failed to stat '%s': %s\n", path,
2194fa9e4066Sahrens 		    strerror(errno));
2195c6065d0fSGeorge Wilson 		free(path);
2196c6065d0fSGeorge Wilson 		(void) close(fd);
2197c6065d0fSGeorge Wilson 		exit(1);
2198c6065d0fSGeorge Wilson 	}
2199c6065d0fSGeorge Wilson 
2200c6065d0fSGeorge Wilson 	if (S_ISBLK(statbuf.st_mode)) {
2201c6065d0fSGeorge Wilson 		(void) printf("cannot use '%s': character device required\n",
2202c6065d0fSGeorge Wilson 		    path);
2203c6065d0fSGeorge Wilson 		free(path);
2204c6065d0fSGeorge Wilson 		(void) close(fd);
2205c6065d0fSGeorge Wilson 		exit(1);
2206fa9e4066Sahrens 	}
2207fa9e4066Sahrens 
2208fa9e4066Sahrens 	psize = statbuf.st_size;
2209fa9e4066Sahrens 	psize = P2ALIGN(psize, (uint64_t)sizeof (vdev_label_t));
2210fa9e4066Sahrens 
221153b9a4a9SVictor Latushkin 	for (int l = 0; l < VDEV_LABELS; l++) {
2212fa9e4066Sahrens 		nvlist_t *config = NULL;
2213fa9e4066Sahrens 
2214fa9e4066Sahrens 		(void) printf("--------------------------------------------\n");
2215fa9e4066Sahrens 		(void) printf("LABEL %d\n", l);
2216fa9e4066Sahrens 		(void) printf("--------------------------------------------\n");
2217fa9e4066Sahrens 
22180d981225Seschrock 		if (pread64(fd, &label, sizeof (label),
2219fa9e4066Sahrens 		    vdev_label_offset(psize, l, 0)) != sizeof (label)) {
2220fa9e4066Sahrens 			(void) printf("failed to read label %d\n", l);
2221fa9e4066Sahrens 			continue;
2222fa9e4066Sahrens 		}
2223fa9e4066Sahrens 
2224fa9e4066Sahrens 		if (nvlist_unpack(buf, buflen, &config, 0) != 0) {
2225fa9e4066Sahrens 			(void) printf("failed to unpack label %d\n", l);
222653b9a4a9SVictor Latushkin 			ashift = SPA_MINBLOCKSHIFT;
222753b9a4a9SVictor Latushkin 		} else {
222853b9a4a9SVictor Latushkin 			nvlist_t *vdev_tree = NULL;
222953b9a4a9SVictor Latushkin 
2230fa9e4066Sahrens 			dump_nvlist(config, 4);
223153b9a4a9SVictor Latushkin 			if ((nvlist_lookup_nvlist(config,
223253b9a4a9SVictor Latushkin 			    ZPOOL_CONFIG_VDEV_TREE, &vdev_tree) != 0) ||
223353b9a4a9SVictor Latushkin 			    (nvlist_lookup_uint64(vdev_tree,
223453b9a4a9SVictor Latushkin 			    ZPOOL_CONFIG_ASHIFT, &ashift) != 0))
223553b9a4a9SVictor Latushkin 				ashift = SPA_MINBLOCKSHIFT;
2236fa9e4066Sahrens 			nvlist_free(config);
2237fa9e4066Sahrens 		}
223853b9a4a9SVictor Latushkin 		if (dump_opt['u'])
223953b9a4a9SVictor Latushkin 			dump_label_uberblocks(&label, ashift);
224053b9a4a9SVictor Latushkin 	}
2241c6065d0fSGeorge Wilson 
2242c6065d0fSGeorge Wilson 	free(path);
2243c6065d0fSGeorge Wilson 	(void) close(fd);
2244fa9e4066Sahrens }
2245fa9e4066Sahrens 
2246f22efbdaSMatthew Ahrens static uint64_t dataset_feature_count[SPA_FEATURES];
2247b5152584SMatthew Ahrens 
2248fa9e4066Sahrens /*ARGSUSED*/
22491d452cf5Sahrens static int
dump_one_dir(const char * dsname,void * arg)2250fd136879SMatthew Ahrens dump_one_dir(const char *dsname, void *arg)
2251fa9e4066Sahrens {
2252fa9e4066Sahrens 	int error;
2253fa9e4066Sahrens 	objset_t *os;
2254fa9e4066Sahrens 
2255503ad85cSMatthew Ahrens 	error = dmu_objset_own(dsname, DMU_OST_ANY, B_TRUE, FTAG, &os);
2256fa9e4066Sahrens 	if (error) {
2257b24ab676SJeff Bonwick 		(void) printf("Could not open %s, error %d\n", dsname, error);
22581d452cf5Sahrens 		return (0);
2259fa9e4066Sahrens 	}
2260f22efbdaSMatthew Ahrens 
2261f22efbdaSMatthew Ahrens 	for (spa_feature_t f = 0; f < SPA_FEATURES; f++) {
2262f22efbdaSMatthew Ahrens 		if (!dmu_objset_ds(os)->ds_feature_inuse[f])
2263f22efbdaSMatthew Ahrens 			continue;
2264f22efbdaSMatthew Ahrens 		ASSERT(spa_feature_table[f].fi_flags &
2265f22efbdaSMatthew Ahrens 		    ZFEATURE_FLAG_PER_DATASET);
2266f22efbdaSMatthew Ahrens 		dataset_feature_count[f]++;
2267f22efbdaSMatthew Ahrens 	}
2268f22efbdaSMatthew Ahrens 
2269fa9e4066Sahrens 	dump_dir(os);
2270503ad85cSMatthew Ahrens 	dmu_objset_disown(os, FTAG);
2271e0d35c44Smarks 	fuid_table_destroy();
22720a586ceaSMark Shellenbaum 	sa_loaded = B_FALSE;
22731d452cf5Sahrens 	return (0);
2274fa9e4066Sahrens }
2275fa9e4066Sahrens 
2276b24ab676SJeff Bonwick /*
2277b24ab676SJeff Bonwick  * Block statistics.
2278b24ab676SJeff Bonwick  */
2279b5152584SMatthew Ahrens #define	PSIZE_HISTO_SIZE (SPA_OLD_MAXBLOCKSIZE / SPA_MINBLOCKSIZE + 2)
2280b24ab676SJeff Bonwick typedef struct zdb_blkstats {
2281b24ab676SJeff Bonwick 	uint64_t zb_asize;
2282b24ab676SJeff Bonwick 	uint64_t zb_lsize;
2283b24ab676SJeff Bonwick 	uint64_t zb_psize;
2284b24ab676SJeff Bonwick 	uint64_t zb_count;
2285d5ee8a13SMatthew Ahrens 	uint64_t zb_gangs;
2286d5ee8a13SMatthew Ahrens 	uint64_t zb_ditto_samevdev;
2287490d05b9SMatthew Ahrens 	uint64_t zb_psize_histogram[PSIZE_HISTO_SIZE];
2288b24ab676SJeff Bonwick } zdb_blkstats_t;
2289b24ab676SJeff Bonwick 
2290b24ab676SJeff Bonwick /*
2291b24ab676SJeff Bonwick  * Extended object types to report deferred frees and dedup auto-ditto blocks.
2292b24ab676SJeff Bonwick  */
2293b24ab676SJeff Bonwick #define	ZDB_OT_DEFERRED	(DMU_OT_NUMTYPES + 0)
2294b24ab676SJeff Bonwick #define	ZDB_OT_DITTO	(DMU_OT_NUMTYPES + 1)
2295ad135b5dSChristopher Siden #define	ZDB_OT_OTHER	(DMU_OT_NUMTYPES + 2)
2296ad135b5dSChristopher Siden #define	ZDB_OT_TOTAL	(DMU_OT_NUMTYPES + 3)
2297b24ab676SJeff Bonwick 
2298b24ab676SJeff Bonwick static char *zdb_ot_extname[] = {
2299b24ab676SJeff Bonwick 	"deferred free",
2300b24ab676SJeff Bonwick 	"dedup ditto",
2301ad135b5dSChristopher Siden 	"other",
2302b24ab676SJeff Bonwick 	"Total",
2303b24ab676SJeff Bonwick };
2304b24ab676SJeff Bonwick 
2305b24ab676SJeff Bonwick #define	ZB_TOTAL	DN_MAX_LEVELS
2306b24ab676SJeff Bonwick 
2307b24ab676SJeff Bonwick typedef struct zdb_cb {
2308b24ab676SJeff Bonwick 	zdb_blkstats_t	zcb_type[ZB_TOTAL + 1][ZDB_OT_TOTAL + 1];
2309b24ab676SJeff Bonwick 	uint64_t	zcb_dedup_asize;
2310b24ab676SJeff Bonwick 	uint64_t	zcb_dedup_blocks;
23115d7b4d43SMatthew Ahrens 	uint64_t	zcb_embedded_blocks[NUM_BP_EMBEDDED_TYPES];
23125d7b4d43SMatthew Ahrens 	uint64_t	zcb_embedded_histogram[NUM_BP_EMBEDDED_TYPES]
23135d7b4d43SMatthew Ahrens 	    [BPE_PAYLOAD_SIZE];
2314490d05b9SMatthew Ahrens 	uint64_t	zcb_start;
2315490d05b9SMatthew Ahrens 	uint64_t	zcb_lastprint;
2316490d05b9SMatthew Ahrens 	uint64_t	zcb_totalasize;
2317b24ab676SJeff Bonwick 	uint64_t	zcb_errors[256];
2318b24ab676SJeff Bonwick 	int		zcb_readfails;
2319b24ab676SJeff Bonwick 	int		zcb_haderrors;
2320cde58dbcSMatthew Ahrens 	spa_t		*zcb_spa;
2321b24ab676SJeff Bonwick } zdb_cb_t;
2322b24ab676SJeff Bonwick 
2323b24ab676SJeff Bonwick static void
zdb_count_block(zdb_cb_t * zcb,zilog_t * zilog,const blkptr_t * bp,dmu_object_type_t type)2324cde58dbcSMatthew Ahrens zdb_count_block(zdb_cb_t *zcb, zilog_t *zilog, const blkptr_t *bp,
2325b24ab676SJeff Bonwick     dmu_object_type_t type)
2326b24ab676SJeff Bonwick {
2327b24ab676SJeff Bonwick 	uint64_t refcnt = 0;
2328b24ab676SJeff Bonwick 
2329b24ab676SJeff Bonwick 	ASSERT(type < ZDB_OT_TOTAL);
2330b24ab676SJeff Bonwick 
2331b24ab676SJeff Bonwick 	if (zilog && zil_bp_tree_add(zilog, bp) != 0)
2332b24ab676SJeff Bonwick 		return;
2333b24ab676SJeff Bonwick 
2334b24ab676SJeff Bonwick 	for (int i = 0; i < 4; i++) {
2335b24ab676SJeff Bonwick 		int l = (i < 2) ? BP_GET_LEVEL(bp) : ZB_TOTAL;
2336b24ab676SJeff Bonwick 		int t = (i & 1) ? type : ZDB_OT_TOTAL;
2337d5ee8a13SMatthew Ahrens 		int equal;
2338b24ab676SJeff Bonwick 		zdb_blkstats_t *zb = &zcb->zcb_type[l][t];
2339b24ab676SJeff Bonwick 
2340b24ab676SJeff Bonwick 		zb->zb_asize += BP_GET_ASIZE(bp);
2341b24ab676SJeff Bonwick 		zb->zb_lsize += BP_GET_LSIZE(bp);
2342b24ab676SJeff Bonwick 		zb->zb_psize += BP_GET_PSIZE(bp);
2343b24ab676SJeff Bonwick 		zb->zb_count++;
2344b5152584SMatthew Ahrens 
2345b5152584SMatthew Ahrens 		/*
2346b5152584SMatthew Ahrens 		 * The histogram is only big enough to record blocks up to
2347b5152584SMatthew Ahrens 		 * SPA_OLD_MAXBLOCKSIZE; larger blocks go into the last,
2348b5152584SMatthew Ahrens 		 * "other", bucket.
2349b5152584SMatthew Ahrens 		 */
2350b5152584SMatthew Ahrens 		int idx = BP_GET_PSIZE(bp) >> SPA_MINBLOCKSHIFT;
2351b5152584SMatthew Ahrens 		idx = MIN(idx, SPA_OLD_MAXBLOCKSIZE / SPA_MINBLOCKSIZE + 1);
2352b5152584SMatthew Ahrens 		zb->zb_psize_histogram[idx]++;
2353d5ee8a13SMatthew Ahrens 
2354d5ee8a13SMatthew Ahrens 		zb->zb_gangs += BP_COUNT_GANG(bp);
2355d5ee8a13SMatthew Ahrens 
2356d5ee8a13SMatthew Ahrens 		switch (BP_GET_NDVAS(bp)) {
2357d5ee8a13SMatthew Ahrens 		case 2:
2358d5ee8a13SMatthew Ahrens 			if (DVA_GET_VDEV(&bp->blk_dva[0]) ==
2359d5ee8a13SMatthew Ahrens 			    DVA_GET_VDEV(&bp->blk_dva[1]))
2360d5ee8a13SMatthew Ahrens 				zb->zb_ditto_samevdev++;
2361d5ee8a13SMatthew Ahrens 			break;
2362d5ee8a13SMatthew Ahrens 		case 3:
2363d5ee8a13SMatthew Ahrens 			equal = (DVA_GET_VDEV(&bp->blk_dva[0]) ==
2364d5ee8a13SMatthew Ahrens 			    DVA_GET_VDEV(&bp->blk_dva[1])) +
2365d5ee8a13SMatthew Ahrens 			    (DVA_GET_VDEV(&bp->blk_dva[0]) ==
2366d5ee8a13SMatthew Ahrens 			    DVA_GET_VDEV(&bp->blk_dva[2])) +
2367d5ee8a13SMatthew Ahrens 			    (DVA_GET_VDEV(&bp->blk_dva[1]) ==
2368d5ee8a13SMatthew Ahrens 			    DVA_GET_VDEV(&bp->blk_dva[2]));
2369d5ee8a13SMatthew Ahrens 			if (equal != 0)
2370d5ee8a13SMatthew Ahrens 				zb->zb_ditto_samevdev++;
2371d5ee8a13SMatthew Ahrens 			break;
2372d5ee8a13SMatthew Ahrens 		}
2373d5ee8a13SMatthew Ahrens 
2374b24ab676SJeff Bonwick 	}
2375b24ab676SJeff Bonwick 
23765d7b4d43SMatthew Ahrens 	if (BP_IS_EMBEDDED(bp)) {
23775d7b4d43SMatthew Ahrens 		zcb->zcb_embedded_blocks[BPE_GET_ETYPE(bp)]++;
23785d7b4d43SMatthew Ahrens 		zcb->zcb_embedded_histogram[BPE_GET_ETYPE(bp)]
23795d7b4d43SMatthew Ahrens 		    [BPE_GET_PSIZE(bp)]++;
23805d7b4d43SMatthew Ahrens 		return;
23815d7b4d43SMatthew Ahrens 	}
23825d7b4d43SMatthew Ahrens 
2383b24ab676SJeff Bonwick 	if (dump_opt['L'])
2384b24ab676SJeff Bonwick 		return;
2385b24ab676SJeff Bonwick 
2386b24ab676SJeff Bonwick 	if (BP_GET_DEDUP(bp)) {
2387b24ab676SJeff Bonwick 		ddt_t *ddt;
2388b24ab676SJeff Bonwick 		ddt_entry_t *dde;
2389b24ab676SJeff Bonwick 
2390cde58dbcSMatthew Ahrens 		ddt = ddt_select(zcb->zcb_spa, bp);
2391b24ab676SJeff Bonwick 		ddt_enter(ddt);
2392b24ab676SJeff Bonwick 		dde = ddt_lookup(ddt, bp, B_FALSE);
2393b24ab676SJeff Bonwick 
2394b24ab676SJeff Bonwick 		if (dde == NULL) {
2395b24ab676SJeff Bonwick 			refcnt = 0;
2396b24ab676SJeff Bonwick 		} else {
2397b24ab676SJeff Bonwick 			ddt_phys_t *ddp = ddt_phys_select(dde, bp);
2398b24ab676SJeff Bonwick 			ddt_phys_decref(ddp);
2399b24ab676SJeff Bonwick 			refcnt = ddp->ddp_refcnt;
2400b24ab676SJeff Bonwick 			if (ddt_phys_total_refcnt(dde) == 0)
2401b24ab676SJeff Bonwick 				ddt_remove(ddt, dde);
2402b24ab676SJeff Bonwick 		}
2403b24ab676SJeff Bonwick 		ddt_exit(ddt);
2404b24ab676SJeff Bonwick 	}
2405b24ab676SJeff Bonwick 
2406cde58dbcSMatthew Ahrens 	VERIFY3U(zio_wait(zio_claim(NULL, zcb->zcb_spa,
2407cde58dbcSMatthew Ahrens 	    refcnt ? 0 : spa_first_txg(zcb->zcb_spa),
2408b24ab676SJeff Bonwick 	    bp, NULL, NULL, ZIO_FLAG_CANFAIL)), ==, 0);
2409b24ab676SJeff Bonwick }
2410b24ab676SJeff Bonwick 
241131d7e8faSGeorge Wilson static void
zdb_blkptr_done(zio_t * zio)241231d7e8faSGeorge Wilson zdb_blkptr_done(zio_t *zio)
241331d7e8faSGeorge Wilson {
241431d7e8faSGeorge Wilson 	spa_t *spa = zio->io_spa;
241531d7e8faSGeorge Wilson 	blkptr_t *bp = zio->io_bp;
241631d7e8faSGeorge Wilson 	int ioerr = zio->io_error;
241731d7e8faSGeorge Wilson 	zdb_cb_t *zcb = zio->io_private;
24187802d7bfSMatthew Ahrens 	zbookmark_phys_t *zb = &zio->io_bookmark;
241931d7e8faSGeorge Wilson 
242031d7e8faSGeorge Wilson 	zio_data_buf_free(zio->io_data, zio->io_size);
242131d7e8faSGeorge Wilson 
242231d7e8faSGeorge Wilson 	mutex_enter(&spa->spa_scrub_lock);
242331d7e8faSGeorge Wilson 	spa->spa_scrub_inflight--;
242431d7e8faSGeorge Wilson 	cv_broadcast(&spa->spa_scrub_io_cv);
242531d7e8faSGeorge Wilson 
242631d7e8faSGeorge Wilson 	if (ioerr && !(zio->io_flags & ZIO_FLAG_SPECULATIVE)) {
242731d7e8faSGeorge Wilson 		char blkbuf[BP_SPRINTF_LEN];
242831d7e8faSGeorge Wilson 
242931d7e8faSGeorge Wilson 		zcb->zcb_haderrors = 1;
243031d7e8faSGeorge Wilson 		zcb->zcb_errors[ioerr]++;
243131d7e8faSGeorge Wilson 
243231d7e8faSGeorge Wilson 		if (dump_opt['b'] >= 2)
243343466aaeSMax Grossman 			snprintf_blkptr(blkbuf, sizeof (blkbuf), bp);
243431d7e8faSGeorge Wilson 		else
243531d7e8faSGeorge Wilson 			blkbuf[0] = '\0';
243631d7e8faSGeorge Wilson 
243731d7e8faSGeorge Wilson 		(void) printf("zdb_blkptr_cb: "
243831d7e8faSGeorge Wilson 		    "Got error %d reading "
243931d7e8faSGeorge Wilson 		    "<%llu, %llu, %lld, %llx> %s -- skipping\n",
244031d7e8faSGeorge Wilson 		    ioerr,
244131d7e8faSGeorge Wilson 		    (u_longlong_t)zb->zb_objset,
244231d7e8faSGeorge Wilson 		    (u_longlong_t)zb->zb_object,
244331d7e8faSGeorge Wilson 		    (u_longlong_t)zb->zb_level,
244431d7e8faSGeorge Wilson 		    (u_longlong_t)zb->zb_blkid,
244531d7e8faSGeorge Wilson 		    blkbuf);
244631d7e8faSGeorge Wilson 	}
244731d7e8faSGeorge Wilson 	mutex_exit(&spa->spa_scrub_lock);
244831d7e8faSGeorge Wilson }
244931d7e8faSGeorge Wilson 
2450b24ab676SJeff Bonwick static int
zdb_blkptr_cb(spa_t * spa,zilog_t * zilog,const blkptr_t * bp,const zbookmark_phys_t * zb,const dnode_phys_t * dnp,void * arg)24511b912ec7SGeorge Wilson zdb_blkptr_cb(spa_t *spa, zilog_t *zilog, const blkptr_t *bp,
24527802d7bfSMatthew Ahrens     const zbookmark_phys_t *zb, const dnode_phys_t *dnp, void *arg)
2453b24ab676SJeff Bonwick {
2454b24ab676SJeff Bonwick 	zdb_cb_t *zcb = arg;
2455b24ab676SJeff Bonwick 	dmu_object_type_t type;
2456b24ab676SJeff Bonwick 	boolean_t is_metadata;
2457b24ab676SJeff Bonwick 
2458a2cdcdd2SPaul Dagnelie 	if (bp == NULL)
2459a2cdcdd2SPaul Dagnelie 		return (0);
2460a2cdcdd2SPaul Dagnelie 
246143466aaeSMax Grossman 	if (dump_opt['b'] >= 5 && bp->blk_birth > 0) {
246243466aaeSMax Grossman 		char blkbuf[BP_SPRINTF_LEN];
246343466aaeSMax Grossman 		snprintf_blkptr(blkbuf, sizeof (blkbuf), bp);
246443466aaeSMax Grossman 		(void) printf("objset %llu object %llu "
246543466aaeSMax Grossman 		    "level %lld offset 0x%llx %s\n",
246643466aaeSMax Grossman 		    (u_longlong_t)zb->zb_objset,
246743466aaeSMax Grossman 		    (u_longlong_t)zb->zb_object,
246843466aaeSMax Grossman 		    (longlong_t)zb->zb_level,
246943466aaeSMax Grossman 		    (u_longlong_t)blkid2offset(dnp, bp, zb),
247043466aaeSMax Grossman 		    blkbuf);
247143466aaeSMax Grossman 	}
247243466aaeSMax Grossman 
247343466aaeSMax Grossman 	if (BP_IS_HOLE(bp))
2474b24ab676SJeff Bonwick 		return (0);
2475b24ab676SJeff Bonwick 
2476b24ab676SJeff Bonwick 	type = BP_GET_TYPE(bp);
2477b24ab676SJeff Bonwick 
2478ad135b5dSChristopher Siden 	zdb_count_block(zcb, zilog, bp,
2479ad135b5dSChristopher Siden 	    (type & DMU_OT_NEWTYPE) ? ZDB_OT_OTHER : type);
2480b24ab676SJeff Bonwick 
2481ad135b5dSChristopher Siden 	is_metadata = (BP_GET_LEVEL(bp) != 0 || DMU_OT_IS_METADATA(type));
2482b24ab676SJeff Bonwick 
24835d7b4d43SMatthew Ahrens 	if (!BP_IS_EMBEDDED(bp) &&
24845d7b4d43SMatthew Ahrens 	    (dump_opt['c'] > 1 || (dump_opt['c'] && is_metadata))) {
2485b24ab676SJeff Bonwick 		size_t size = BP_GET_PSIZE(bp);
248631d7e8faSGeorge Wilson 		void *data = zio_data_buf_alloc(size);
2487b24ab676SJeff Bonwick 		int flags = ZIO_FLAG_CANFAIL | ZIO_FLAG_SCRUB | ZIO_FLAG_RAW;
2488b24ab676SJeff Bonwick 
2489b24ab676SJeff Bonwick 		/* If it's an intent log block, failure is expected. */
2490b24ab676SJeff Bonwick 		if (zb->zb_level == ZB_ZIL_LEVEL)
2491b24ab676SJeff Bonwick 			flags |= ZIO_FLAG_SPECULATIVE;
2492b24ab676SJeff Bonwick 
249331d7e8faSGeorge Wilson 		mutex_enter(&spa->spa_scrub_lock);
249431d7e8faSGeorge Wilson 		while (spa->spa_scrub_inflight > max_inflight)
249531d7e8faSGeorge Wilson 			cv_wait(&spa->spa_scrub_io_cv, &spa->spa_scrub_lock);
249631d7e8faSGeorge Wilson 		spa->spa_scrub_inflight++;
249731d7e8faSGeorge Wilson 		mutex_exit(&spa->spa_scrub_lock);
2498b24ab676SJeff Bonwick 
249931d7e8faSGeorge Wilson 		zio_nowait(zio_read(NULL, spa, bp, data, size,
250031d7e8faSGeorge Wilson 		    zdb_blkptr_done, zcb, ZIO_PRIORITY_ASYNC_READ, flags, zb));
2501b24ab676SJeff Bonwick 	}
2502b24ab676SJeff Bonwick 
2503b24ab676SJeff Bonwick 	zcb->zcb_readfails = 0;
2504b24ab676SJeff Bonwick 
2505f7950bf1SMatthew Ahrens 	/* only call gethrtime() every 100 blocks */
2506f7950bf1SMatthew Ahrens 	static int iters;
2507f7950bf1SMatthew Ahrens 	if (++iters > 100)
2508f7950bf1SMatthew Ahrens 		iters = 0;
2509f7950bf1SMatthew Ahrens 	else
2510f7950bf1SMatthew Ahrens 		return (0);
2511f7950bf1SMatthew Ahrens 
2512f7950bf1SMatthew Ahrens 	if (dump_opt['b'] < 5 && gethrtime() > zcb->zcb_lastprint + NANOSEC) {
2513490d05b9SMatthew Ahrens 		uint64_t now = gethrtime();
2514490d05b9SMatthew Ahrens 		char buf[10];
2515490d05b9SMatthew Ahrens 		uint64_t bytes = zcb->zcb_type[ZB_TOTAL][ZDB_OT_TOTAL].zb_asize;
2516490d05b9SMatthew Ahrens 		int kb_per_sec =
2517490d05b9SMatthew Ahrens 		    1 + bytes / (1 + ((now - zcb->zcb_start) / 1000 / 1000));
2518490d05b9SMatthew Ahrens 		int sec_remaining =
2519490d05b9SMatthew Ahrens 		    (zcb->zcb_totalasize - bytes) / 1024 / kb_per_sec;
2520490d05b9SMatthew Ahrens 
2521*a66b28a3SJason King 		/* make sure nicenum has enough space */
2522*a66b28a3SJason King 		CTASSERT(sizeof (buf) >= NN_NUMBUF_SZ);
2523*a66b28a3SJason King 
2524490d05b9SMatthew Ahrens 		zfs_nicenum(bytes, buf, sizeof (buf));
2525490d05b9SMatthew Ahrens 		(void) fprintf(stderr,
2526490d05b9SMatthew Ahrens 		    "\r%5s completed (%4dMB/s) "
2527490d05b9SMatthew Ahrens 		    "estimated time remaining: %uhr %02umin %02usec        ",
2528490d05b9SMatthew Ahrens 		    buf, kb_per_sec / 1024,
2529490d05b9SMatthew Ahrens 		    sec_remaining / 60 / 60,
2530490d05b9SMatthew Ahrens 		    sec_remaining / 60 % 60,
2531490d05b9SMatthew Ahrens 		    sec_remaining % 60);
2532490d05b9SMatthew Ahrens 
2533490d05b9SMatthew Ahrens 		zcb->zcb_lastprint = now;
2534490d05b9SMatthew Ahrens 	}
2535490d05b9SMatthew Ahrens 
2536b24ab676SJeff Bonwick 	return (0);
2537b24ab676SJeff Bonwick }
2538b24ab676SJeff Bonwick 
2539fa9e4066Sahrens static void
zdb_leak(void * arg,uint64_t start,uint64_t size)25400713e232SGeorge Wilson zdb_leak(void *arg, uint64_t start, uint64_t size)
2541fa9e4066Sahrens {
25420713e232SGeorge Wilson 	vdev_t *vd = arg;
2543fa9e4066Sahrens 
2544fa9e4066Sahrens 	(void) printf("leaked space: vdev %llu, offset 0x%llx, size %llu\n",
2545e14bb325SJeff Bonwick 	    (u_longlong_t)vd->vdev_id, (u_longlong_t)start, (u_longlong_t)size);
2546e14bb325SJeff Bonwick }
2547e14bb325SJeff Bonwick 
25480713e232SGeorge Wilson static metaslab_ops_t zdb_metaslab_ops = {
25492e4c9986SGeorge Wilson 	NULL	/* alloc */
2550e14bb325SJeff Bonwick };
2551e14bb325SJeff Bonwick 
2552e14bb325SJeff Bonwick static void
zdb_ddt_leak_init(spa_t * spa,zdb_cb_t * zcb)2553bbfd46c4SJeff Bonwick zdb_ddt_leak_init(spa_t *spa, zdb_cb_t *zcb)
2554fa9e4066Sahrens {
2555bbfd46c4SJeff Bonwick 	ddt_bookmark_t ddb = { 0 };
2556b24ab676SJeff Bonwick 	ddt_entry_t dde;
2557b24ab676SJeff Bonwick 	int error;
2558fa9e4066Sahrens 
2559bbfd46c4SJeff Bonwick 	while ((error = ddt_walk(spa, &ddb, &dde)) == 0) {
2560b24ab676SJeff Bonwick 		blkptr_t blk;
2561b24ab676SJeff Bonwick 		ddt_phys_t *ddp = dde.dde_phys;
2562bbfd46c4SJeff Bonwick 
2563bbfd46c4SJeff Bonwick 		if (ddb.ddb_class == DDT_CLASS_UNIQUE)
2564bbfd46c4SJeff Bonwick 			return;
2565bbfd46c4SJeff Bonwick 
2566b24ab676SJeff Bonwick 		ASSERT(ddt_phys_total_refcnt(&dde) > 1);
2567bbfd46c4SJeff Bonwick 
2568b24ab676SJeff Bonwick 		for (int p = 0; p < DDT_PHYS_TYPES; p++, ddp++) {
2569b24ab676SJeff Bonwick 			if (ddp->ddp_phys_birth == 0)
2570b24ab676SJeff Bonwick 				continue;
2571bbfd46c4SJeff Bonwick 			ddt_bp_create(ddb.ddb_checksum,
2572bbfd46c4SJeff Bonwick 			    &dde.dde_key, ddp, &blk);
2573b24ab676SJeff Bonwick 			if (p == DDT_PHYS_DITTO) {
2574cde58dbcSMatthew Ahrens 				zdb_count_block(zcb, NULL, &blk, ZDB_OT_DITTO);
2575b24ab676SJeff Bonwick 			} else {
2576b24ab676SJeff Bonwick 				zcb->zcb_dedup_asize +=
2577b24ab676SJeff Bonwick 				    BP_GET_ASIZE(&blk) * (ddp->ddp_refcnt - 1);
2578b24ab676SJeff Bonwick 				zcb->zcb_dedup_blocks++;
2579b24ab676SJeff Bonwick 			}
2580b24ab676SJeff Bonwick 		}
2581b24ab676SJeff Bonwick 		if (!dump_opt['L']) {
2582bbfd46c4SJeff Bonwick 			ddt_t *ddt = spa->spa_ddt[ddb.ddb_checksum];
2583b24ab676SJeff Bonwick 			ddt_enter(ddt);
2584b24ab676SJeff Bonwick 			VERIFY(ddt_lookup(ddt, &blk, B_TRUE) != NULL);
2585b24ab676SJeff Bonwick 			ddt_exit(ddt);
2586b24ab676SJeff Bonwick 		}
2587b24ab676SJeff Bonwick 	}
2588b24ab676SJeff Bonwick 
2589b24ab676SJeff Bonwick 	ASSERT(error == ENOENT);
2590b24ab676SJeff Bonwick }
2591b24ab676SJeff Bonwick 
2592b24ab676SJeff Bonwick static void
zdb_leak_init(spa_t * spa,zdb_cb_t * zcb)2593b24ab676SJeff Bonwick zdb_leak_init(spa_t *spa, zdb_cb_t *zcb)
2594b24ab676SJeff Bonwick {
2595cde58dbcSMatthew Ahrens 	zcb->zcb_spa = spa;
2596cde58dbcSMatthew Ahrens 
2597b24ab676SJeff Bonwick 	if (!dump_opt['L']) {
2598b24ab676SJeff Bonwick 		vdev_t *rvd = spa->spa_root_vdev;
259906be9802SMatthew Ahrens 		for (uint64_t c = 0; c < rvd->vdev_children; c++) {
2600e14bb325SJeff Bonwick 			vdev_t *vd = rvd->vdev_child[c];
260106be9802SMatthew Ahrens 			for (uint64_t m = 0; m < vd->vdev_ms_count; m++) {
2602fa9e4066Sahrens 				metaslab_t *msp = vd->vdev_ms[m];
2603fa9e4066Sahrens 				mutex_enter(&msp->ms_lock);
26040713e232SGeorge Wilson 				metaslab_unload(msp);
26050713e232SGeorge Wilson 
26060713e232SGeorge Wilson 				/*
26070713e232SGeorge Wilson 				 * For leak detection, we overload the metaslab
26080713e232SGeorge Wilson 				 * ms_tree to contain allocated segments
26090713e232SGeorge Wilson 				 * instead of free segments. As a result,
26100713e232SGeorge Wilson 				 * we can't use the normal metaslab_load/unload
26110713e232SGeorge Wilson 				 * interfaces.
26120713e232SGeorge Wilson 				 */
26130713e232SGeorge Wilson 				if (msp->ms_sm != NULL) {
261406be9802SMatthew Ahrens 					(void) fprintf(stderr,
261506be9802SMatthew Ahrens 					    "\rloading space map for "
261606be9802SMatthew Ahrens 					    "vdev %llu of %llu, "
261706be9802SMatthew Ahrens 					    "metaslab %llu of %llu ...",
261806be9802SMatthew Ahrens 					    (longlong_t)c,
261906be9802SMatthew Ahrens 					    (longlong_t)rvd->vdev_children,
262006be9802SMatthew Ahrens 					    (longlong_t)m,
262106be9802SMatthew Ahrens 					    (longlong_t)vd->vdev_ms_count);
262206be9802SMatthew Ahrens 
26230713e232SGeorge Wilson 					msp->ms_ops = &zdb_metaslab_ops;
2624f7950bf1SMatthew Ahrens 
2625f7950bf1SMatthew Ahrens 					/*
2626f7950bf1SMatthew Ahrens 					 * We don't want to spend the CPU
2627f7950bf1SMatthew Ahrens 					 * manipulating the size-ordered
2628f7950bf1SMatthew Ahrens 					 * tree, so clear the range_tree
2629f7950bf1SMatthew Ahrens 					 * ops.
2630f7950bf1SMatthew Ahrens 					 */
2631f7950bf1SMatthew Ahrens 					msp->ms_tree->rt_ops = NULL;
26320713e232SGeorge Wilson 					VERIFY0(space_map_load(msp->ms_sm,
26330713e232SGeorge Wilson 					    msp->ms_tree, SM_ALLOC));
26340713e232SGeorge Wilson 					msp->ms_loaded = B_TRUE;
26350713e232SGeorge Wilson 				}
2636fa9e4066Sahrens 				mutex_exit(&msp->ms_lock);
2637fa9e4066Sahrens 			}
2638fa9e4066Sahrens 		}
263906be9802SMatthew Ahrens 		(void) fprintf(stderr, "\n");
2640fa9e4066Sahrens 	}
2641fa9e4066Sahrens 
2642b24ab676SJeff Bonwick 	spa_config_enter(spa, SCL_CONFIG, FTAG, RW_READER);
2643b24ab676SJeff Bonwick 
2644bbfd46c4SJeff Bonwick 	zdb_ddt_leak_init(spa, zcb);
2645b24ab676SJeff Bonwick 
2646b24ab676SJeff Bonwick 	spa_config_exit(spa, SCL_CONFIG, FTAG);
2647b24ab676SJeff Bonwick }
2648b24ab676SJeff Bonwick 
2649fa9e4066Sahrens static void
zdb_leak_fini(spa_t * spa)2650e14bb325SJeff Bonwick zdb_leak_fini(spa_t *spa)
2651fa9e4066Sahrens {
2652b24ab676SJeff Bonwick 	if (!dump_opt['L']) {
2653fa9e4066Sahrens 		vdev_t *rvd = spa->spa_root_vdev;
2654e14bb325SJeff Bonwick 		for (int c = 0; c < rvd->vdev_children; c++) {
2655e14bb325SJeff Bonwick 			vdev_t *vd = rvd->vdev_child[c];
2656e14bb325SJeff Bonwick 			for (int m = 0; m < vd->vdev_ms_count; m++) {
2657e14bb325SJeff Bonwick 				metaslab_t *msp = vd->vdev_ms[m];
2658e14bb325SJeff Bonwick 				mutex_enter(&msp->ms_lock);
26590713e232SGeorge Wilson 
26600713e232SGeorge Wilson 				/*
26610713e232SGeorge Wilson 				 * The ms_tree has been overloaded to
26620713e232SGeorge Wilson 				 * contain allocated segments. Now that we
26630713e232SGeorge Wilson 				 * finished traversing all blocks, any
26640713e232SGeorge Wilson 				 * block that remains in the ms_tree
26650713e232SGeorge Wilson 				 * represents an allocated block that we
26660713e232SGeorge Wilson 				 * did not claim during the traversal.
26670713e232SGeorge Wilson 				 * Claimed blocks would have been removed
26680713e232SGeorge Wilson 				 * from the ms_tree.
26690713e232SGeorge Wilson 				 */
26700713e232SGeorge Wilson 				range_tree_vacate(msp->ms_tree, zdb_leak, vd);
26710713e232SGeorge Wilson 				msp->ms_loaded = B_FALSE;
26720713e232SGeorge Wilson 
2673e14bb325SJeff Bonwick 				mutex_exit(&msp->ms_lock);
2674e14bb325SJeff Bonwick 			}
2675e14bb325SJeff Bonwick 		}
2676fa9e4066Sahrens 	}
2677fa9e4066Sahrens }
2678fa9e4066Sahrens 
2679cde58dbcSMatthew Ahrens /* ARGSUSED */
2680cde58dbcSMatthew Ahrens static int
count_block_cb(void * arg,const blkptr_t * bp,dmu_tx_t * tx)2681cde58dbcSMatthew Ahrens count_block_cb(void *arg, const blkptr_t *bp, dmu_tx_t *tx)
2682cde58dbcSMatthew Ahrens {
2683cde58dbcSMatthew Ahrens 	zdb_cb_t *zcb = arg;
2684cde58dbcSMatthew Ahrens 
2685490d05b9SMatthew Ahrens 	if (dump_opt['b'] >= 5) {
2686cde58dbcSMatthew Ahrens 		char blkbuf[BP_SPRINTF_LEN];
268743466aaeSMax Grossman 		snprintf_blkptr(blkbuf, sizeof (blkbuf), bp);
2688cde58dbcSMatthew Ahrens 		(void) printf("[%s] %s\n",
2689cde58dbcSMatthew Ahrens 		    "deferred free", blkbuf);
2690cde58dbcSMatthew Ahrens 	}
2691cde58dbcSMatthew Ahrens 	zdb_count_block(zcb, NULL, bp, ZDB_OT_DEFERRED);
2692cde58dbcSMatthew Ahrens 	return (0);
2693cde58dbcSMatthew Ahrens }
2694cde58dbcSMatthew Ahrens 
2695fa9e4066Sahrens static int
dump_block_stats(spa_t * spa)2696fa9e4066Sahrens dump_block_stats(spa_t *spa)
2697fa9e4066Sahrens {
2698fa9e4066Sahrens 	zdb_cb_t zcb = { 0 };
2699fa9e4066Sahrens 	zdb_blkstats_t *zb, *tzb;
2700b24ab676SJeff Bonwick 	uint64_t norm_alloc, norm_space, total_alloc, total_found;
2701cd088ea4SVictor Latushkin 	int flags = TRAVERSE_PRE | TRAVERSE_PREFETCH_METADATA | TRAVERSE_HARD;
27025d7b4d43SMatthew Ahrens 	boolean_t leaks = B_FALSE;
2703fa9e4066Sahrens 
2704490d05b9SMatthew Ahrens 	(void) printf("\nTraversing all blocks %s%s%s%s%s...\n\n",
270582a0a985SVictor Latushkin 	    (dump_opt['c'] || !dump_opt['L']) ? "to verify " : "",
27066365109dSVictor Latushkin 	    (dump_opt['c'] == 1) ? "metadata " : "",
270782a0a985SVictor Latushkin 	    dump_opt['c'] ? "checksums " : "",
270882a0a985SVictor Latushkin 	    (dump_opt['c'] && !dump_opt['L']) ? "and verify " : "",
270982a0a985SVictor Latushkin 	    !dump_opt['L'] ? "nothing leaked " : "");
2710fa9e4066Sahrens 
2711fa9e4066Sahrens 	/*
2712e14bb325SJeff Bonwick 	 * Load all space maps as SM_ALLOC maps, then traverse the pool
2713e14bb325SJeff Bonwick 	 * claiming each block we discover.  If the pool is perfectly
2714e14bb325SJeff Bonwick 	 * consistent, the space maps will be empty when we're done.
2715e14bb325SJeff Bonwick 	 * Anything left over is a leak; any block we can't claim (because
2716e14bb325SJeff Bonwick 	 * it's not part of any space map) is a double allocation,
2717e14bb325SJeff Bonwick 	 * reference to a freed block, or an unclaimed log block.
2718fa9e4066Sahrens 	 */
2719b24ab676SJeff Bonwick 	zdb_leak_init(spa, &zcb);
2720fa9e4066Sahrens 
2721fa9e4066Sahrens 	/*
2722fa9e4066Sahrens 	 * If there's a deferred-free bplist, process that first.
2723fa9e4066Sahrens 	 */
2724cde58dbcSMatthew Ahrens 	(void) bpobj_iterate_nofree(&spa->spa_deferred_bpobj,
2725cde58dbcSMatthew Ahrens 	    count_block_cb, &zcb, NULL);
27263b2aab18SMatthew Ahrens 	if (spa_version(spa) >= SPA_VERSION_DEADLISTS) {
2727cde58dbcSMatthew Ahrens 		(void) bpobj_iterate_nofree(&spa->spa_dsl_pool->dp_free_bpobj,
2728cde58dbcSMatthew Ahrens 		    count_block_cb, &zcb, NULL);
27293b2aab18SMatthew Ahrens 	}
27302acef22dSMatthew Ahrens 	if (spa_feature_is_active(spa, SPA_FEATURE_ASYNC_DESTROY)) {
2731b420f3adSRichard Lowe 		VERIFY3U(0, ==, bptree_iterate(spa->spa_meta_objset,
2732ad135b5dSChristopher Siden 		    spa->spa_dsl_pool->dp_bptree_obj, B_FALSE, count_block_cb,
2733ad135b5dSChristopher Siden 		    &zcb, NULL));
2734ad135b5dSChristopher Siden 	}
2735fa9e4066Sahrens 
2736bbfd46c4SJeff Bonwick 	if (dump_opt['c'] > 1)
2737bbfd46c4SJeff Bonwick 		flags |= TRAVERSE_PREFETCH_DATA;
2738bbfd46c4SJeff Bonwick 
2739490d05b9SMatthew Ahrens 	zcb.zcb_totalasize = metaslab_class_get_alloc(spa_normal_class(spa));
2740490d05b9SMatthew Ahrens 	zcb.zcb_start = zcb.zcb_lastprint = gethrtime();
2741bbfd46c4SJeff Bonwick 	zcb.zcb_haderrors |= traverse_pool(spa, 0, flags, zdb_blkptr_cb, &zcb);
2742fa9e4066Sahrens 
274331d7e8faSGeorge Wilson 	/*
274431d7e8faSGeorge Wilson 	 * If we've traversed the data blocks then we need to wait for those
274531d7e8faSGeorge Wilson 	 * I/Os to complete. We leverage "The Godfather" zio to wait on
274631d7e8faSGeorge Wilson 	 * all async I/Os to complete.
274731d7e8faSGeorge Wilson 	 */
274831d7e8faSGeorge Wilson 	if (dump_opt['c']) {
27496f834bc1SMatthew Ahrens 		for (int i = 0; i < max_ncpus; i++) {
27506f834bc1SMatthew Ahrens 			(void) zio_wait(spa->spa_async_zio_root[i]);
27516f834bc1SMatthew Ahrens 			spa->spa_async_zio_root[i] = zio_root(spa, NULL, NULL,
275231d7e8faSGeorge Wilson 			    ZIO_FLAG_CANFAIL | ZIO_FLAG_SPECULATIVE |
275331d7e8faSGeorge Wilson 			    ZIO_FLAG_GODFATHER);
275431d7e8faSGeorge Wilson 		}
27556f834bc1SMatthew Ahrens 	}
275631d7e8faSGeorge Wilson 
2757b24ab676SJeff Bonwick 	if (zcb.zcb_haderrors) {
2758fa9e4066Sahrens 		(void) printf("\nError counts:\n\n");
2759fa9e4066Sahrens 		(void) printf("\t%5s  %s\n", "errno", "count");
2760b24ab676SJeff Bonwick 		for (int e = 0; e < 256; e++) {
2761fa9e4066Sahrens 			if (zcb.zcb_errors[e] != 0) {
2762fa9e4066Sahrens 				(void) printf("\t%5d  %llu\n",
2763fa9e4066Sahrens 				    e, (u_longlong_t)zcb.zcb_errors[e]);
2764fa9e4066Sahrens 			}
2765fa9e4066Sahrens 		}
2766fa9e4066Sahrens 	}
2767fa9e4066Sahrens 
2768fa9e4066Sahrens 	/*
2769fa9e4066Sahrens 	 * Report any leaked segments.
2770fa9e4066Sahrens 	 */
2771e14bb325SJeff Bonwick 	zdb_leak_fini(spa);
2772fa9e4066Sahrens 
2773b24ab676SJeff Bonwick 	tzb = &zcb.zcb_type[ZB_TOTAL][ZDB_OT_TOTAL];
2774d41e7643Sek110237 
2775b24ab676SJeff Bonwick 	norm_alloc = metaslab_class_get_alloc(spa_normal_class(spa));
2776b24ab676SJeff Bonwick 	norm_space = metaslab_class_get_space(spa_normal_class(spa));
2777fa9e4066Sahrens 
2778b24ab676SJeff Bonwick 	total_alloc = norm_alloc + metaslab_class_get_alloc(spa_log_class(spa));
2779b24ab676SJeff Bonwick 	total_found = tzb->zb_asize - zcb.zcb_dedup_asize;
27808654d025Sperrin 
2781b24ab676SJeff Bonwick 	if (total_found == total_alloc) {
278282a0a985SVictor Latushkin 		if (!dump_opt['L'])
2783fa9e4066Sahrens 			(void) printf("\n\tNo leaks (block sum matches space"
2784fa9e4066Sahrens 			    " maps exactly)\n");
2785fa9e4066Sahrens 	} else {
2786fa9e4066Sahrens 		(void) printf("block traversal size %llu != alloc %llu "
278782a0a985SVictor Latushkin 		    "(%s %lld)\n",
2788b24ab676SJeff Bonwick 		    (u_longlong_t)total_found,
2789b24ab676SJeff Bonwick 		    (u_longlong_t)total_alloc,
279082a0a985SVictor Latushkin 		    (dump_opt['L']) ? "unreachable" : "leaked",
2791b24ab676SJeff Bonwick 		    (longlong_t)(total_alloc - total_found));
27925d7b4d43SMatthew Ahrens 		leaks = B_TRUE;
2793fa9e4066Sahrens 	}
2794fa9e4066Sahrens 
2795fa9e4066Sahrens 	if (tzb->zb_count == 0)
2796fa9e4066Sahrens 		return (2);
2797fa9e4066Sahrens 
2798fa9e4066Sahrens 	(void) printf("\n");
2799fa9e4066Sahrens 	(void) printf("\tbp count:      %10llu\n",
2800fa9e4066Sahrens 	    (u_longlong_t)tzb->zb_count);
2801d5ee8a13SMatthew Ahrens 	(void) printf("\tganged count:  %10llu\n",
2802d5ee8a13SMatthew Ahrens 	    (longlong_t)tzb->zb_gangs);
2803b24ab676SJeff Bonwick 	(void) printf("\tbp logical:    %10llu      avg: %6llu\n",
2804fa9e4066Sahrens 	    (u_longlong_t)tzb->zb_lsize,
2805fa9e4066Sahrens 	    (u_longlong_t)(tzb->zb_lsize / tzb->zb_count));
2806b24ab676SJeff Bonwick 	(void) printf("\tbp physical:   %10llu      avg:"
2807b24ab676SJeff Bonwick 	    " %6llu     compression: %6.2f\n",
2808fa9e4066Sahrens 	    (u_longlong_t)tzb->zb_psize,
2809fa9e4066Sahrens 	    (u_longlong_t)(tzb->zb_psize / tzb->zb_count),
2810fa9e4066Sahrens 	    (double)tzb->zb_lsize / tzb->zb_psize);
2811b24ab676SJeff Bonwick 	(void) printf("\tbp allocated:  %10llu      avg:"
2812b24ab676SJeff Bonwick 	    " %6llu     compression: %6.2f\n",
2813fa9e4066Sahrens 	    (u_longlong_t)tzb->zb_asize,
2814fa9e4066Sahrens 	    (u_longlong_t)(tzb->zb_asize / tzb->zb_count),
2815fa9e4066Sahrens 	    (double)tzb->zb_lsize / tzb->zb_asize);
2816b24ab676SJeff Bonwick 	(void) printf("\tbp deduped:    %10llu    ref>1:"
2817b24ab676SJeff Bonwick 	    " %6llu   deduplication: %6.2f\n",
2818b24ab676SJeff Bonwick 	    (u_longlong_t)zcb.zcb_dedup_asize,
2819b24ab676SJeff Bonwick 	    (u_longlong_t)zcb.zcb_dedup_blocks,
2820b24ab676SJeff Bonwick 	    (double)zcb.zcb_dedup_asize / tzb->zb_asize + 1.0);
2821b24ab676SJeff Bonwick 	(void) printf("\tSPA allocated: %10llu     used: %5.2f%%\n",
2822b24ab676SJeff Bonwick 	    (u_longlong_t)norm_alloc, 100.0 * norm_alloc / norm_space);
2823fa9e4066Sahrens 
28245d7b4d43SMatthew Ahrens 	for (bp_embedded_type_t i = 0; i < NUM_BP_EMBEDDED_TYPES; i++) {
28255d7b4d43SMatthew Ahrens 		if (zcb.zcb_embedded_blocks[i] == 0)
28265d7b4d43SMatthew Ahrens 			continue;
28275d7b4d43SMatthew Ahrens 		(void) printf("\n");
28285d7b4d43SMatthew Ahrens 		(void) printf("\tadditional, non-pointer bps of type %u: "
28295d7b4d43SMatthew Ahrens 		    "%10llu\n",
28305d7b4d43SMatthew Ahrens 		    i, (u_longlong_t)zcb.zcb_embedded_blocks[i]);
28315d7b4d43SMatthew Ahrens 
28325d7b4d43SMatthew Ahrens 		if (dump_opt['b'] >= 3) {
28335d7b4d43SMatthew Ahrens 			(void) printf("\t number of (compressed) bytes:  "
28345d7b4d43SMatthew Ahrens 			    "number of bps\n");
28355d7b4d43SMatthew Ahrens 			dump_histogram(zcb.zcb_embedded_histogram[i],
28365d7b4d43SMatthew Ahrens 			    sizeof (zcb.zcb_embedded_histogram[i]) /
28375d7b4d43SMatthew Ahrens 			    sizeof (zcb.zcb_embedded_histogram[i][0]), 0);
28385d7b4d43SMatthew Ahrens 		}
28395d7b4d43SMatthew Ahrens 	}
28405d7b4d43SMatthew Ahrens 
2841d5ee8a13SMatthew Ahrens 	if (tzb->zb_ditto_samevdev != 0) {
2842d5ee8a13SMatthew Ahrens 		(void) printf("\tDittoed blocks on same vdev: %llu\n",
2843d5ee8a13SMatthew Ahrens 		    (longlong_t)tzb->zb_ditto_samevdev);
2844d5ee8a13SMatthew Ahrens 	}
2845d5ee8a13SMatthew Ahrens 
2846fa9e4066Sahrens 	if (dump_opt['b'] >= 2) {
2847fa9e4066Sahrens 		int l, t, level;
2848fa9e4066Sahrens 		(void) printf("\nBlocks\tLSIZE\tPSIZE\tASIZE"
2849fa9e4066Sahrens 		    "\t  avg\t comp\t%%Total\tType\n");
2850fa9e4066Sahrens 
2851b24ab676SJeff Bonwick 		for (t = 0; t <= ZDB_OT_TOTAL; t++) {
28523f9d6ad7SLin Ling 			char csize[32], lsize[32], psize[32], asize[32];
2853d5ee8a13SMatthew Ahrens 			char avg[32], gang[32];
2854fa9e4066Sahrens 			char *typename;
2855fa9e4066Sahrens 
2856*a66b28a3SJason King 			/* make sure nicenum has enough space */
2857*a66b28a3SJason King 			CTASSERT(sizeof (csize) >= NN_NUMBUF_SZ);
2858*a66b28a3SJason King 			CTASSERT(sizeof (lsize) >= NN_NUMBUF_SZ);
2859*a66b28a3SJason King 			CTASSERT(sizeof (psize) >= NN_NUMBUF_SZ);
2860*a66b28a3SJason King 			CTASSERT(sizeof (asize) >= NN_NUMBUF_SZ);
2861*a66b28a3SJason King 			CTASSERT(sizeof (avg) >= NN_NUMBUF_SZ);
2862*a66b28a3SJason King 			CTASSERT(sizeof (gang) >= NN_NUMBUF_SZ);
2863*a66b28a3SJason King 
2864b24ab676SJeff Bonwick 			if (t < DMU_OT_NUMTYPES)
2865b24ab676SJeff Bonwick 				typename = dmu_ot[t].ot_name;
2866b24ab676SJeff Bonwick 			else
2867b24ab676SJeff Bonwick 				typename = zdb_ot_extname[t - DMU_OT_NUMTYPES];
2868fa9e4066Sahrens 
2869fa9e4066Sahrens 			if (zcb.zcb_type[ZB_TOTAL][t].zb_asize == 0) {
2870fa9e4066Sahrens 				(void) printf("%6s\t%5s\t%5s\t%5s"
2871fa9e4066Sahrens 				    "\t%5s\t%5s\t%6s\t%s\n",
2872fa9e4066Sahrens 				    "-",
2873fa9e4066Sahrens 				    "-",
2874fa9e4066Sahrens 				    "-",
2875fa9e4066Sahrens 				    "-",
2876fa9e4066Sahrens 				    "-",
2877fa9e4066Sahrens 				    "-",
2878fa9e4066Sahrens 				    "-",
2879fa9e4066Sahrens 				    typename);
2880fa9e4066Sahrens 				continue;
2881fa9e4066Sahrens 			}
2882fa9e4066Sahrens 
2883fa9e4066Sahrens 			for (l = ZB_TOTAL - 1; l >= -1; l--) {
2884fa9e4066Sahrens 				level = (l == -1 ? ZB_TOTAL : l);
2885fa9e4066Sahrens 				zb = &zcb.zcb_type[level][t];
2886fa9e4066Sahrens 
2887fa9e4066Sahrens 				if (zb->zb_asize == 0)
2888fa9e4066Sahrens 					continue;
2889fa9e4066Sahrens 
2890fa9e4066Sahrens 				if (dump_opt['b'] < 3 && level != ZB_TOTAL)
2891fa9e4066Sahrens 					continue;
2892fa9e4066Sahrens 
2893fa9e4066Sahrens 				if (level == 0 && zb->zb_asize ==
2894fa9e4066Sahrens 				    zcb.zcb_type[ZB_TOTAL][t].zb_asize)
2895fa9e4066Sahrens 					continue;
2896fa9e4066Sahrens 
2897*a66b28a3SJason King 				zdb_nicenum(zb->zb_count, csize,
2898*a66b28a3SJason King 				    sizeof (csize));
2899*a66b28a3SJason King 				zdb_nicenum(zb->zb_lsize, lsize,
2900*a66b28a3SJason King 				    sizeof (lsize));
2901*a66b28a3SJason King 				zdb_nicenum(zb->zb_psize, psize,
2902*a66b28a3SJason King 				    sizeof (psize));
2903*a66b28a3SJason King 				zdb_nicenum(zb->zb_asize, asize,
2904*a66b28a3SJason King 				    sizeof (asize));
2905*a66b28a3SJason King 				zdb_nicenum(zb->zb_asize / zb->zb_count, avg,
2906*a66b28a3SJason King 				    sizeof (avg));
2907*a66b28a3SJason King 				zdb_nicenum(zb->zb_gangs, gang, sizeof (gang));
2908fa9e4066Sahrens 
2909fa9e4066Sahrens 				(void) printf("%6s\t%5s\t%5s\t%5s\t%5s"
2910fa9e4066Sahrens 				    "\t%5.2f\t%6.2f\t",
2911fa9e4066Sahrens 				    csize, lsize, psize, asize, avg,
2912fa9e4066Sahrens 				    (double)zb->zb_lsize / zb->zb_psize,
2913fa9e4066Sahrens 				    100.0 * zb->zb_asize / tzb->zb_asize);
2914fa9e4066Sahrens 
2915fa9e4066Sahrens 				if (level == ZB_TOTAL)
2916fa9e4066Sahrens 					(void) printf("%s\n", typename);
2917fa9e4066Sahrens 				else
2918fa9e4066Sahrens 					(void) printf("    L%d %s\n",
2919fa9e4066Sahrens 					    level, typename);
2920490d05b9SMatthew Ahrens 
2921d5ee8a13SMatthew Ahrens 				if (dump_opt['b'] >= 3 && zb->zb_gangs > 0) {
2922d5ee8a13SMatthew Ahrens 					(void) printf("\t number of ganged "
2923d5ee8a13SMatthew Ahrens 					    "blocks: %s\n", gang);
2924d5ee8a13SMatthew Ahrens 				}
2925d5ee8a13SMatthew Ahrens 
2926490d05b9SMatthew Ahrens 				if (dump_opt['b'] >= 4) {
2927490d05b9SMatthew Ahrens 					(void) printf("psize "
2928490d05b9SMatthew Ahrens 					    "(in 512-byte sectors): "
2929490d05b9SMatthew Ahrens 					    "number of blocks\n");
2930490d05b9SMatthew Ahrens 					dump_histogram(zb->zb_psize_histogram,
29310713e232SGeorge Wilson 					    PSIZE_HISTO_SIZE, 0);
2932490d05b9SMatthew Ahrens 				}
2933fa9e4066Sahrens 			}
2934fa9e4066Sahrens 		}
2935fa9e4066Sahrens 	}
2936fa9e4066Sahrens 
2937fa9e4066Sahrens 	(void) printf("\n");
2938fa9e4066Sahrens 
2939fa9e4066Sahrens 	if (leaks)
2940fa9e4066Sahrens 		return (2);
2941fa9e4066Sahrens 
2942fa9e4066Sahrens 	if (zcb.zcb_haderrors)
2943fa9e4066Sahrens 		return (3);
2944fa9e4066Sahrens 
2945fa9e4066Sahrens 	return (0);
2946fa9e4066Sahrens }
2947fa9e4066Sahrens 
2948b24ab676SJeff Bonwick typedef struct zdb_ddt_entry {
2949b24ab676SJeff Bonwick 	ddt_key_t	zdde_key;
2950b24ab676SJeff Bonwick 	uint64_t	zdde_ref_blocks;
2951b24ab676SJeff Bonwick 	uint64_t	zdde_ref_lsize;
2952b24ab676SJeff Bonwick 	uint64_t	zdde_ref_psize;
2953b24ab676SJeff Bonwick 	uint64_t	zdde_ref_dsize;
2954b24ab676SJeff Bonwick 	avl_node_t	zdde_node;
2955b24ab676SJeff Bonwick } zdb_ddt_entry_t;
2956b24ab676SJeff Bonwick 
2957b24ab676SJeff Bonwick /* ARGSUSED */
2958b24ab676SJeff Bonwick static int
zdb_ddt_add_cb(spa_t * spa,zilog_t * zilog,const blkptr_t * bp,const zbookmark_phys_t * zb,const dnode_phys_t * dnp,void * arg)2959b24ab676SJeff Bonwick zdb_ddt_add_cb(spa_t *spa, zilog_t *zilog, const blkptr_t *bp,
29607802d7bfSMatthew Ahrens     const zbookmark_phys_t *zb, const dnode_phys_t *dnp, void *arg)
2961b24ab676SJeff Bonwick {
2962b24ab676SJeff Bonwick 	avl_tree_t *t = arg;
2963b24ab676SJeff Bonwick 	avl_index_t where;
2964b24ab676SJeff Bonwick 	zdb_ddt_entry_t *zdde, zdde_search;
2965b24ab676SJeff Bonwick 
2966a2cdcdd2SPaul Dagnelie 	if (bp == NULL || BP_IS_HOLE(bp) || BP_IS_EMBEDDED(bp))
2967b24ab676SJeff Bonwick 		return (0);
2968b24ab676SJeff Bonwick 
2969b24ab676SJeff Bonwick 	if (dump_opt['S'] > 1 && zb->zb_level == ZB_ROOT_LEVEL) {
2970b24ab676SJeff Bonwick 		(void) printf("traversing objset %llu, %llu objects, "
2971b24ab676SJeff Bonwick 		    "%lu blocks so far\n",
2972b24ab676SJeff Bonwick 		    (u_longlong_t)zb->zb_objset,
29735d7b4d43SMatthew Ahrens 		    (u_longlong_t)BP_GET_FILL(bp),
2974b24ab676SJeff Bonwick 		    avl_numnodes(t));
2975b24ab676SJeff Bonwick 	}
2976b24ab676SJeff Bonwick 
2977bbfd46c4SJeff Bonwick 	if (BP_IS_HOLE(bp) || BP_GET_CHECKSUM(bp) == ZIO_CHECKSUM_OFF ||
2978ad135b5dSChristopher Siden 	    BP_GET_LEVEL(bp) > 0 || DMU_OT_IS_METADATA(BP_GET_TYPE(bp)))
2979b24ab676SJeff Bonwick 		return (0);
2980b24ab676SJeff Bonwick 
2981b24ab676SJeff Bonwick 	ddt_key_fill(&zdde_search.zdde_key, bp);
2982b24ab676SJeff Bonwick 
2983b24ab676SJeff Bonwick 	zdde = avl_find(t, &zdde_search, &where);
2984b24ab676SJeff Bonwick 
2985b24ab676SJeff Bonwick 	if (zdde == NULL) {
2986b24ab676SJeff Bonwick 		zdde = umem_zalloc(sizeof (*zdde), UMEM_NOFAIL);
2987b24ab676SJeff Bonwick 		zdde->zdde_key = zdde_search.zdde_key;
2988b24ab676SJeff Bonwick 		avl_insert(t, zdde, where);
2989b24ab676SJeff Bonwick 	}
2990b24ab676SJeff Bonwick 
2991b24ab676SJeff Bonwick 	zdde->zdde_ref_blocks += 1;
2992b24ab676SJeff Bonwick 	zdde->zdde_ref_lsize += BP_GET_LSIZE(bp);
2993b24ab676SJeff Bonwick 	zdde->zdde_ref_psize += BP_GET_PSIZE(bp);
2994b24ab676SJeff Bonwick 	zdde->zdde_ref_dsize += bp_get_dsize_sync(spa, bp);
2995b24ab676SJeff Bonwick 
2996b24ab676SJeff Bonwick 	return (0);
2997b24ab676SJeff Bonwick }
2998b24ab676SJeff Bonwick 
2999b24ab676SJeff Bonwick static void
dump_simulated_ddt(spa_t * spa)3000b24ab676SJeff Bonwick dump_simulated_ddt(spa_t *spa)
3001b24ab676SJeff Bonwick {
3002b24ab676SJeff Bonwick 	avl_tree_t t;
3003b24ab676SJeff Bonwick 	void *cookie = NULL;
3004b24ab676SJeff Bonwick 	zdb_ddt_entry_t *zdde;
3005b24ab676SJeff Bonwick 	ddt_histogram_t ddh_total = { 0 };
3006b24ab676SJeff Bonwick 	ddt_stat_t dds_total = { 0 };
3007b24ab676SJeff Bonwick 
3008b24ab676SJeff Bonwick 	avl_create(&t, ddt_entry_compare,
3009b24ab676SJeff Bonwick 	    sizeof (zdb_ddt_entry_t), offsetof(zdb_ddt_entry_t, zdde_node));
3010b24ab676SJeff Bonwick 
3011b24ab676SJeff Bonwick 	spa_config_enter(spa, SCL_CONFIG, FTAG, RW_READER);
3012b24ab676SJeff Bonwick 
3013bbfd46c4SJeff Bonwick 	(void) traverse_pool(spa, 0, TRAVERSE_PRE | TRAVERSE_PREFETCH_METADATA,
3014bbfd46c4SJeff Bonwick 	    zdb_ddt_add_cb, &t);
3015b24ab676SJeff Bonwick 
3016b24ab676SJeff Bonwick 	spa_config_exit(spa, SCL_CONFIG, FTAG);
3017b24ab676SJeff Bonwick 
3018b24ab676SJeff Bonwick 	while ((zdde = avl_destroy_nodes(&t, &cookie)) != NULL) {
3019b24ab676SJeff Bonwick 		ddt_stat_t dds;
3020b24ab676SJeff Bonwick 		uint64_t refcnt = zdde->zdde_ref_blocks;
3021b24ab676SJeff Bonwick 		ASSERT(refcnt != 0);
3022b24ab676SJeff Bonwick 
3023b24ab676SJeff Bonwick 		dds.dds_blocks = zdde->zdde_ref_blocks / refcnt;
3024b24ab676SJeff Bonwick 		dds.dds_lsize = zdde->zdde_ref_lsize / refcnt;
3025b24ab676SJeff Bonwick 		dds.dds_psize = zdde->zdde_ref_psize / refcnt;
3026b24ab676SJeff Bonwick 		dds.dds_dsize = zdde->zdde_ref_dsize / refcnt;
3027b24ab676SJeff Bonwick 
3028b24ab676SJeff Bonwick 		dds.dds_ref_blocks = zdde->zdde_ref_blocks;
3029b24ab676SJeff Bonwick 		dds.dds_ref_lsize = zdde->zdde_ref_lsize;
3030b24ab676SJeff Bonwick 		dds.dds_ref_psize = zdde->zdde_ref_psize;
3031b24ab676SJeff Bonwick 		dds.dds_ref_dsize = zdde->zdde_ref_dsize;
3032b24ab676SJeff Bonwick 
3033bf16b11eSMatthew Ahrens 		ddt_stat_add(&ddh_total.ddh_stat[highbit64(refcnt) - 1],
3034bf16b11eSMatthew Ahrens 		    &dds, 0);
3035b24ab676SJeff Bonwick 
3036b24ab676SJeff Bonwick 		umem_free(zdde, sizeof (*zdde));
3037b24ab676SJeff Bonwick 	}
3038b24ab676SJeff Bonwick 
3039b24ab676SJeff Bonwick 	avl_destroy(&t);
3040b24ab676SJeff Bonwick 
3041b24ab676SJeff Bonwick 	ddt_histogram_stat(&dds_total, &ddh_total);
3042b24ab676SJeff Bonwick 
3043b24ab676SJeff Bonwick 	(void) printf("Simulated DDT histogram:\n");
3044b24ab676SJeff Bonwick 
30459eb19f4dSGeorge Wilson 	zpool_dump_ddt(&dds_total, &ddh_total);
3046b24ab676SJeff Bonwick 
3047b24ab676SJeff Bonwick 	dump_dedup_ratio(&dds_total);
3048b24ab676SJeff Bonwick }
3049b24ab676SJeff Bonwick 
3050fa9e4066Sahrens static void
dump_zpool(spa_t * spa)3051fa9e4066Sahrens dump_zpool(spa_t *spa)
3052fa9e4066Sahrens {
3053fa9e4066Sahrens 	dsl_pool_t *dp = spa_get_dsl(spa);
3054fa9e4066Sahrens 	int rc = 0;
3055fa9e4066Sahrens 
3056b24ab676SJeff Bonwick 	if (dump_opt['S']) {
3057b24ab676SJeff Bonwick 		dump_simulated_ddt(spa);
3058b24ab676SJeff Bonwick 		return;
3059b24ab676SJeff Bonwick 	}
3060b24ab676SJeff Bonwick 
306107428bdfSVictor Latushkin 	if (!dump_opt['e'] && dump_opt['C'] > 1) {
306207428bdfSVictor Latushkin 		(void) printf("\nCached configuration:\n");
306307428bdfSVictor Latushkin 		dump_nvlist(spa->spa_config, 8);
306407428bdfSVictor Latushkin 	}
306507428bdfSVictor Latushkin 
306607428bdfSVictor Latushkin 	if (dump_opt['C'])
306707428bdfSVictor Latushkin 		dump_config(spa);
306807428bdfSVictor Latushkin 
3069fa9e4066Sahrens 	if (dump_opt['u'])
307053b9a4a9SVictor Latushkin 		dump_uberblock(&spa->spa_uberblock, "\nUberblock:\n", "\n");
3071fa9e4066Sahrens 
3072b24ab676SJeff Bonwick 	if (dump_opt['D'])
3073b24ab676SJeff Bonwick 		dump_all_ddts(spa);
3074b24ab676SJeff Bonwick 
307587219db7SVictor Latushkin 	if (dump_opt['d'] > 2 || dump_opt['m'])
307687219db7SVictor Latushkin 		dump_metaslabs(spa);
30772e4c9986SGeorge Wilson 	if (dump_opt['M'])
30782e4c9986SGeorge Wilson 		dump_metaslab_groups(spa);
307987219db7SVictor Latushkin 
308087219db7SVictor Latushkin 	if (dump_opt['d'] || dump_opt['i']) {
3081fa9e4066Sahrens 		dump_dir(dp->dp_meta_objset);
3082fa9e4066Sahrens 		if (dump_opt['d'] >= 3) {
3083732885fcSMatthew Ahrens 			dump_full_bpobj(&spa->spa_deferred_bpobj,
3084d0475637SMatthew Ahrens 			    "Deferred frees", 0);
3085cde58dbcSMatthew Ahrens 			if (spa_version(spa) >= SPA_VERSION_DEADLISTS) {
3086732885fcSMatthew Ahrens 				dump_full_bpobj(
3087732885fcSMatthew Ahrens 				    &spa->spa_dsl_pool->dp_free_bpobj,
3088d0475637SMatthew Ahrens 				    "Pool snapshot frees", 0);
3089ad135b5dSChristopher Siden 			}
3090ad135b5dSChristopher Siden 
3091ad135b5dSChristopher Siden 			if (spa_feature_is_active(spa,
30922acef22dSMatthew Ahrens 			    SPA_FEATURE_ASYNC_DESTROY)) {
3093ad135b5dSChristopher Siden 				dump_bptree(spa->spa_meta_objset,
3094ad135b5dSChristopher Siden 				    spa->spa_dsl_pool->dp_bptree_obj,
3095ad135b5dSChristopher Siden 				    "Pool dataset frees");
3096cde58dbcSMatthew Ahrens 			}
3097fa9e4066Sahrens 			dump_dtl(spa->spa_root_vdev, 0);
3098fa9e4066Sahrens 		}
309907428bdfSVictor Latushkin 		(void) dmu_objset_find(spa_name(spa), dump_one_dir,
310007428bdfSVictor Latushkin 		    NULL, DS_FIND_SNAPSHOTS | DS_FIND_CHILDREN);
3101b5152584SMatthew Ahrens 
3102f22efbdaSMatthew Ahrens 		for (spa_feature_t f = 0; f < SPA_FEATURES; f++) {
3103f22efbdaSMatthew Ahrens 			uint64_t refcount;
3104f22efbdaSMatthew Ahrens 
3105f22efbdaSMatthew Ahrens 			if (!(spa_feature_table[f].fi_flags &
3106f22efbdaSMatthew Ahrens 			    ZFEATURE_FLAG_PER_DATASET)) {
3107f22efbdaSMatthew Ahrens 				ASSERT0(dataset_feature_count[f]);
3108f22efbdaSMatthew Ahrens 				continue;
3109f22efbdaSMatthew Ahrens 			}
3110b5152584SMatthew Ahrens 			(void) feature_get_refcount(spa,
3111f22efbdaSMatthew Ahrens 			    &spa_feature_table[f], &refcount);
3112f22efbdaSMatthew Ahrens 			if (dataset_feature_count[f] != refcount) {
3113f22efbdaSMatthew Ahrens 				(void) printf("%s feature refcount mismatch: "
3114f22efbdaSMatthew Ahrens 				    "%lld datasets != %lld refcount\n",
3115f22efbdaSMatthew Ahrens 				    spa_feature_table[f].fi_uname,
3116f22efbdaSMatthew Ahrens 				    (longlong_t)dataset_feature_count[f],
3117b5152584SMatthew Ahrens 				    (longlong_t)refcount);
3118b5152584SMatthew Ahrens 				rc = 2;
3119b5152584SMatthew Ahrens 			} else {
3120f22efbdaSMatthew Ahrens 				(void) printf("Verified %s feature refcount "
3121f22efbdaSMatthew Ahrens 				    "of %llu is correct\n",
3122f22efbdaSMatthew Ahrens 				    spa_feature_table[f].fi_uname,
3123f22efbdaSMatthew Ahrens 				    (longlong_t)refcount);
3124f22efbdaSMatthew Ahrens 			}
3125fa9e4066Sahrens 		}
3126b5152584SMatthew Ahrens 	}
3127b5152584SMatthew Ahrens 	if (rc == 0 && (dump_opt['b'] || dump_opt['c']))
3128fa9e4066Sahrens 		rc = dump_block_stats(spa);
3129fa9e4066Sahrens 
31300713e232SGeorge Wilson 	if (rc == 0)
31310713e232SGeorge Wilson 		rc = verify_spacemap_refcounts(spa);
31320713e232SGeorge Wilson 
3133fa9e4066Sahrens 	if (dump_opt['s'])
3134fa9e4066Sahrens 		show_pool_stats(spa);
3135fa9e4066Sahrens 
31368f18d1faSGeorge Wilson 	if (dump_opt['h'])
31378f18d1faSGeorge Wilson 		dump_history(spa);
31388f18d1faSGeorge Wilson 
3139fa9e4066Sahrens 	if (rc != 0)
3140fa9e4066Sahrens 		exit(rc);
3141fa9e4066Sahrens }
3142fa9e4066Sahrens 
314344cd46caSbillm #define	ZDB_FLAG_CHECKSUM	0x0001
314444cd46caSbillm #define	ZDB_FLAG_DECOMPRESS	0x0002
314544cd46caSbillm #define	ZDB_FLAG_BSWAP		0x0004
314644cd46caSbillm #define	ZDB_FLAG_GBH		0x0008
314744cd46caSbillm #define	ZDB_FLAG_INDIRECT	0x0010
314844cd46caSbillm #define	ZDB_FLAG_PHYS		0x0020
314944cd46caSbillm #define	ZDB_FLAG_RAW		0x0040
315044cd46caSbillm #define	ZDB_FLAG_PRINT_BLKPTR	0x0080
315144cd46caSbillm 
315244cd46caSbillm int flagbits[256];
315344cd46caSbillm 
315444cd46caSbillm static void
zdb_print_blkptr(blkptr_t * bp,int flags)315544cd46caSbillm zdb_print_blkptr(blkptr_t *bp, int flags)
315644cd46caSbillm {
3157b24ab676SJeff Bonwick 	char blkbuf[BP_SPRINTF_LEN];
315844cd46caSbillm 
315944cd46caSbillm 	if (flags & ZDB_FLAG_BSWAP)
316044cd46caSbillm 		byteswap_uint64_array((void *)bp, sizeof (blkptr_t));
3161b24ab676SJeff Bonwick 
316243466aaeSMax Grossman 	snprintf_blkptr(blkbuf, sizeof (blkbuf), bp);
3163b24ab676SJeff Bonwick 	(void) printf("%s\n", blkbuf);
316444cd46caSbillm }
316544cd46caSbillm 
316644cd46caSbillm static void
zdb_dump_indirect(blkptr_t * bp,int nbps,int flags)316744cd46caSbillm zdb_dump_indirect(blkptr_t *bp, int nbps, int flags)
316844cd46caSbillm {
316944cd46caSbillm 	int i;
317044cd46caSbillm 
317144cd46caSbillm 	for (i = 0; i < nbps; i++)
317244cd46caSbillm 		zdb_print_blkptr(&bp[i], flags);
317344cd46caSbillm }
317444cd46caSbillm 
317544cd46caSbillm static void
zdb_dump_gbh(void * buf,int flags)317644cd46caSbillm zdb_dump_gbh(void *buf, int flags)
317744cd46caSbillm {
317844cd46caSbillm 	zdb_dump_indirect((blkptr_t *)buf, SPA_GBH_NBLKPTRS, flags);
317944cd46caSbillm }
318044cd46caSbillm 
318144cd46caSbillm static void
zdb_dump_block_raw(void * buf,uint64_t size,int flags)318244cd46caSbillm zdb_dump_block_raw(void *buf, uint64_t size, int flags)
318344cd46caSbillm {
318444cd46caSbillm 	if (flags & ZDB_FLAG_BSWAP)
318544cd46caSbillm 		byteswap_uint64_array(buf, size);
3186b24ab676SJeff Bonwick 	(void) write(1, buf, size);
318744cd46caSbillm }
318844cd46caSbillm 
318944cd46caSbillm static void
zdb_dump_block(char * label,void * buf,uint64_t size,int flags)319044cd46caSbillm zdb_dump_block(char *label, void *buf, uint64_t size, int flags)
319144cd46caSbillm {
319244cd46caSbillm 	uint64_t *d = (uint64_t *)buf;
319344cd46caSbillm 	int nwords = size / sizeof (uint64_t);
319444cd46caSbillm 	int do_bswap = !!(flags & ZDB_FLAG_BSWAP);
319544cd46caSbillm 	int i, j;
319644cd46caSbillm 	char *hdr, *c;
319744cd46caSbillm 
319844cd46caSbillm 
319944cd46caSbillm 	if (do_bswap)
320044cd46caSbillm 		hdr = " 7 6 5 4 3 2 1 0   f e d c b a 9 8";
320144cd46caSbillm 	else
320244cd46caSbillm 		hdr = " 0 1 2 3 4 5 6 7   8 9 a b c d e f";
320344cd46caSbillm 
320444cd46caSbillm 	(void) printf("\n%s\n%6s   %s  0123456789abcdef\n", label, "", hdr);
320544cd46caSbillm 
320644cd46caSbillm 	for (i = 0; i < nwords; i += 2) {
320744cd46caSbillm 		(void) printf("%06llx:  %016llx  %016llx  ",
320844cd46caSbillm 		    (u_longlong_t)(i * sizeof (uint64_t)),
320944cd46caSbillm 		    (u_longlong_t)(do_bswap ? BSWAP_64(d[i]) : d[i]),
321044cd46caSbillm 		    (u_longlong_t)(do_bswap ? BSWAP_64(d[i + 1]) : d[i + 1]));
321144cd46caSbillm 
321244cd46caSbillm 		c = (char *)&d[i];
321344cd46caSbillm 		for (j = 0; j < 2 * sizeof (uint64_t); j++)
321444cd46caSbillm 			(void) printf("%c", isprint(c[j]) ? c[j] : '.');
321544cd46caSbillm 		(void) printf("\n");
321644cd46caSbillm 	}
321744cd46caSbillm }
321844cd46caSbillm 
321944cd46caSbillm /*
322044cd46caSbillm  * There are two acceptable formats:
322144cd46caSbillm  *	leaf_name	  - For example: c1t0d0 or /tmp/ztest.0a
322244cd46caSbillm  *	child[.child]*    - For example: 0.1.1
322344cd46caSbillm  *
322444cd46caSbillm  * The second form can be used to specify arbitrary vdevs anywhere
322544cd46caSbillm  * in the heirarchy.  For example, in a pool with a mirror of
322644cd46caSbillm  * RAID-Zs, you can specify either RAID-Z vdev with 0.0 or 0.1 .
322744cd46caSbillm  */
322844cd46caSbillm static vdev_t *
zdb_vdev_lookup(vdev_t * vdev,char * path)322944cd46caSbillm zdb_vdev_lookup(vdev_t *vdev, char *path)
323044cd46caSbillm {
323144cd46caSbillm 	char *s, *p, *q;
323244cd46caSbillm 	int i;
323344cd46caSbillm 
323444cd46caSbillm 	if (vdev == NULL)
323544cd46caSbillm 		return (NULL);
323644cd46caSbillm 
323744cd46caSbillm 	/* First, assume the x.x.x.x format */
323844cd46caSbillm 	i = (int)strtoul(path, &s, 10);
323944cd46caSbillm 	if (s == path || (s && *s != '.' && *s != '\0'))
324044cd46caSbillm 		goto name;
324144cd46caSbillm 	if (i < 0 || i >= vdev->vdev_children)
324244cd46caSbillm 		return (NULL);
324344cd46caSbillm 
324444cd46caSbillm 	vdev = vdev->vdev_child[i];
324544cd46caSbillm 	if (*s == '\0')
324644cd46caSbillm 		return (vdev);
324744cd46caSbillm 	return (zdb_vdev_lookup(vdev, s+1));
324844cd46caSbillm 
324944cd46caSbillm name:
325044cd46caSbillm 	for (i = 0; i < vdev->vdev_children; i++) {
325144cd46caSbillm 		vdev_t *vc = vdev->vdev_child[i];
325244cd46caSbillm 
325344cd46caSbillm 		if (vc->vdev_path == NULL) {
325444cd46caSbillm 			vc = zdb_vdev_lookup(vc, path);
325544cd46caSbillm 			if (vc == NULL)
325644cd46caSbillm 				continue;
325744cd46caSbillm 			else
325844cd46caSbillm 				return (vc);
325944cd46caSbillm 		}
326044cd46caSbillm 
326144cd46caSbillm 		p = strrchr(vc->vdev_path, '/');
326244cd46caSbillm 		p = p ? p + 1 : vc->vdev_path;
326344cd46caSbillm 		q = &vc->vdev_path[strlen(vc->vdev_path) - 2];
326444cd46caSbillm 
326544cd46caSbillm 		if (strcmp(vc->vdev_path, path) == 0)
326644cd46caSbillm 			return (vc);
326744cd46caSbillm 		if (strcmp(p, path) == 0)
326844cd46caSbillm 			return (vc);
326944cd46caSbillm 		if (strcmp(q, "s0") == 0 && strncmp(p, path, q - p) == 0)
327044cd46caSbillm 			return (vc);
327144cd46caSbillm 	}
327244cd46caSbillm 
327344cd46caSbillm 	return (NULL);
327444cd46caSbillm }
327544cd46caSbillm 
327644cd46caSbillm /*
327744cd46caSbillm  * Read a block from a pool and print it out.  The syntax of the
327844cd46caSbillm  * block descriptor is:
327944cd46caSbillm  *
328044cd46caSbillm  *	pool:vdev_specifier:offset:size[:flags]
328144cd46caSbillm  *
328244cd46caSbillm  *	pool           - The name of the pool you wish to read from
328344cd46caSbillm  *	vdev_specifier - Which vdev (see comment for zdb_vdev_lookup)
328444cd46caSbillm  *	offset         - offset, in hex, in bytes
328544cd46caSbillm  *	size           - Amount of data to read, in hex, in bytes
328644cd46caSbillm  *	flags          - A string of characters specifying options
328744cd46caSbillm  *		 b: Decode a blkptr at given offset within block
328844cd46caSbillm  *		*c: Calculate and display checksums
3289b24ab676SJeff Bonwick  *		 d: Decompress data before dumping
329044cd46caSbillm  *		 e: Byteswap data before dumping
3291b24ab676SJeff Bonwick  *		 g: Display data as a gang block header
3292b24ab676SJeff Bonwick  *		 i: Display as an indirect block
329344cd46caSbillm  *		 p: Do I/O to physical offset
329444cd46caSbillm  *		 r: Dump raw data to stdout
329544cd46caSbillm  *
329644cd46caSbillm  *              * = not yet implemented
329744cd46caSbillm  */
329844cd46caSbillm static void
zdb_read_block(char * thing,spa_t * spa)329907428bdfSVictor Latushkin zdb_read_block(char *thing, spa_t *spa)
330044cd46caSbillm {
3301b24ab676SJeff Bonwick 	blkptr_t blk, *bp = &blk;
3302b24ab676SJeff Bonwick 	dva_t *dva = bp->blk_dva;
330344cd46caSbillm 	int flags = 0;
3304b24ab676SJeff Bonwick 	uint64_t offset = 0, size = 0, psize = 0, lsize = 0, blkptr_offset = 0;
330544cd46caSbillm 	zio_t *zio;
330644cd46caSbillm 	vdev_t *vd;
3307b24ab676SJeff Bonwick 	void *pbuf, *lbuf, *buf;
330807428bdfSVictor Latushkin 	char *s, *p, *dup, *vdev, *flagstr;
3309b24ab676SJeff Bonwick 	int i, error;
331044cd46caSbillm 
331144cd46caSbillm 	dup = strdup(thing);
331244cd46caSbillm 	s = strtok(dup, ":");
331344cd46caSbillm 	vdev = s ? s : "";
331444cd46caSbillm 	s = strtok(NULL, ":");
331544cd46caSbillm 	offset = strtoull(s ? s : "", NULL, 16);
331644cd46caSbillm 	s = strtok(NULL, ":");
331744cd46caSbillm 	size = strtoull(s ? s : "", NULL, 16);
331844cd46caSbillm 	s = strtok(NULL, ":");
331944cd46caSbillm 	flagstr = s ? s : "";
332044cd46caSbillm 
332144cd46caSbillm 	s = NULL;
332244cd46caSbillm 	if (size == 0)
332344cd46caSbillm 		s = "size must not be zero";
332444cd46caSbillm 	if (!IS_P2ALIGNED(size, DEV_BSIZE))
332544cd46caSbillm 		s = "size must be a multiple of sector size";
332644cd46caSbillm 	if (!IS_P2ALIGNED(offset, DEV_BSIZE))
332744cd46caSbillm 		s = "offset must be a multiple of sector size";
332844cd46caSbillm 	if (s) {
332944cd46caSbillm 		(void) printf("Invalid block specifier: %s  - %s\n", thing, s);
333044cd46caSbillm 		free(dup);
333144cd46caSbillm 		return;
333244cd46caSbillm 	}
333344cd46caSbillm 
333444cd46caSbillm 	for (s = strtok(flagstr, ":"); s; s = strtok(NULL, ":")) {
333544cd46caSbillm 		for (i = 0; flagstr[i]; i++) {
33365ad82045Snd150628 			int bit = flagbits[(uchar_t)flagstr[i]];
333744cd46caSbillm 
333844cd46caSbillm 			if (bit == 0) {
333944cd46caSbillm 				(void) printf("***Invalid flag: %c\n",
334044cd46caSbillm 				    flagstr[i]);
334144cd46caSbillm 				continue;
334244cd46caSbillm 			}
334344cd46caSbillm 			flags |= bit;
334444cd46caSbillm 
334544cd46caSbillm 			/* If it's not something with an argument, keep going */
3346b24ab676SJeff Bonwick 			if ((bit & (ZDB_FLAG_CHECKSUM |
334744cd46caSbillm 			    ZDB_FLAG_PRINT_BLKPTR)) == 0)
334844cd46caSbillm 				continue;
334944cd46caSbillm 
335044cd46caSbillm 			p = &flagstr[i + 1];
335144cd46caSbillm 			if (bit == ZDB_FLAG_PRINT_BLKPTR)
335244cd46caSbillm 				blkptr_offset = strtoull(p, &p, 16);
335344cd46caSbillm 			if (*p != ':' && *p != '\0') {
335444cd46caSbillm 				(void) printf("***Invalid flag arg: '%s'\n", s);
335544cd46caSbillm 				free(dup);
335644cd46caSbillm 				return;
335744cd46caSbillm 			}
335844cd46caSbillm 		}
335944cd46caSbillm 	}
336044cd46caSbillm 
336144cd46caSbillm 	vd = zdb_vdev_lookup(spa->spa_root_vdev, vdev);
336244cd46caSbillm 	if (vd == NULL) {
336344cd46caSbillm 		(void) printf("***Invalid vdev: %s\n", vdev);
336444cd46caSbillm 		free(dup);
336544cd46caSbillm 		return;
336644cd46caSbillm 	} else {
336744cd46caSbillm 		if (vd->vdev_path)
3368b24ab676SJeff Bonwick 			(void) fprintf(stderr, "Found vdev: %s\n",
3369b24ab676SJeff Bonwick 			    vd->vdev_path);
337044cd46caSbillm 		else
3371b24ab676SJeff Bonwick 			(void) fprintf(stderr, "Found vdev type: %s\n",
337244cd46caSbillm 			    vd->vdev_ops->vdev_op_type);
337344cd46caSbillm 	}
337444cd46caSbillm 
3375b24ab676SJeff Bonwick 	psize = size;
3376b24ab676SJeff Bonwick 	lsize = size;
337744cd46caSbillm 
3378b24ab676SJeff Bonwick 	pbuf = umem_alloc(SPA_MAXBLOCKSIZE, UMEM_NOFAIL);
3379b24ab676SJeff Bonwick 	lbuf = umem_alloc(SPA_MAXBLOCKSIZE, UMEM_NOFAIL);
3380b24ab676SJeff Bonwick 
3381b24ab676SJeff Bonwick 	BP_ZERO(bp);
3382b24ab676SJeff Bonwick 
3383b24ab676SJeff Bonwick 	DVA_SET_VDEV(&dva[0], vd->vdev_id);
3384b24ab676SJeff Bonwick 	DVA_SET_OFFSET(&dva[0], offset);
3385b24ab676SJeff Bonwick 	DVA_SET_GANG(&dva[0], !!(flags & ZDB_FLAG_GBH));
3386b24ab676SJeff Bonwick 	DVA_SET_ASIZE(&dva[0], vdev_psize_to_asize(vd, psize));
3387b24ab676SJeff Bonwick 
3388b24ab676SJeff Bonwick 	BP_SET_BIRTH(bp, TXG_INITIAL, TXG_INITIAL);
3389b24ab676SJeff Bonwick 
3390b24ab676SJeff Bonwick 	BP_SET_LSIZE(bp, lsize);
3391b24ab676SJeff Bonwick 	BP_SET_PSIZE(bp, psize);
3392b24ab676SJeff Bonwick 	BP_SET_COMPRESS(bp, ZIO_COMPRESS_OFF);
3393b24ab676SJeff Bonwick 	BP_SET_CHECKSUM(bp, ZIO_CHECKSUM_OFF);
3394b24ab676SJeff Bonwick 	BP_SET_TYPE(bp, DMU_OT_NONE);
3395b24ab676SJeff Bonwick 	BP_SET_LEVEL(bp, 0);
3396b24ab676SJeff Bonwick 	BP_SET_DEDUP(bp, 0);
3397b24ab676SJeff Bonwick 	BP_SET_BYTEORDER(bp, ZFS_HOST_BYTEORDER);
339844cd46caSbillm 
3399e14bb325SJeff Bonwick 	spa_config_enter(spa, SCL_STATE, FTAG, RW_READER);
340044cd46caSbillm 	zio = zio_root(spa, NULL, NULL, 0);
3401b24ab676SJeff Bonwick 
3402b24ab676SJeff Bonwick 	if (vd == vd->vdev_top) {
3403b24ab676SJeff Bonwick 		/*
3404b24ab676SJeff Bonwick 		 * Treat this as a normal block read.
3405b24ab676SJeff Bonwick 		 */
3406b24ab676SJeff Bonwick 		zio_nowait(zio_read(zio, spa, bp, pbuf, psize, NULL, NULL,
3407b24ab676SJeff Bonwick 		    ZIO_PRIORITY_SYNC_READ,
3408b24ab676SJeff Bonwick 		    ZIO_FLAG_CANFAIL | ZIO_FLAG_RAW, NULL));
3409b24ab676SJeff Bonwick 	} else {
3410b24ab676SJeff Bonwick 		/*
3411b24ab676SJeff Bonwick 		 * Treat this as a vdev child I/O.
3412b24ab676SJeff Bonwick 		 */
3413b24ab676SJeff Bonwick 		zio_nowait(zio_vdev_child_io(zio, bp, vd, offset, pbuf, psize,
3414b24ab676SJeff Bonwick 		    ZIO_TYPE_READ, ZIO_PRIORITY_SYNC_READ,
3415b24ab676SJeff Bonwick 		    ZIO_FLAG_DONT_CACHE | ZIO_FLAG_DONT_QUEUE |
3416b24ab676SJeff Bonwick 		    ZIO_FLAG_DONT_PROPAGATE | ZIO_FLAG_DONT_RETRY |
3417b24ab676SJeff Bonwick 		    ZIO_FLAG_CANFAIL | ZIO_FLAG_RAW, NULL, NULL));
3418b24ab676SJeff Bonwick 	}
3419b24ab676SJeff Bonwick 
342044cd46caSbillm 	error = zio_wait(zio);
3421e14bb325SJeff Bonwick 	spa_config_exit(spa, SCL_STATE, FTAG);
342244cd46caSbillm 
342344cd46caSbillm 	if (error) {
342444cd46caSbillm 		(void) printf("Read of %s failed, error: %d\n", thing, error);
342544cd46caSbillm 		goto out;
342644cd46caSbillm 	}
342744cd46caSbillm 
3428b24ab676SJeff Bonwick 	if (flags & ZDB_FLAG_DECOMPRESS) {
3429b24ab676SJeff Bonwick 		/*
3430b24ab676SJeff Bonwick 		 * We don't know how the data was compressed, so just try
3431b24ab676SJeff Bonwick 		 * every decompress function at every inflated blocksize.
3432b24ab676SJeff Bonwick 		 */
3433b24ab676SJeff Bonwick 		enum zio_compress c;
3434b24ab676SJeff Bonwick 		void *pbuf2 = umem_alloc(SPA_MAXBLOCKSIZE, UMEM_NOFAIL);
3435b24ab676SJeff Bonwick 		void *lbuf2 = umem_alloc(SPA_MAXBLOCKSIZE, UMEM_NOFAIL);
3436b24ab676SJeff Bonwick 
3437b24ab676SJeff Bonwick 		bcopy(pbuf, pbuf2, psize);
3438b24ab676SJeff Bonwick 
3439b24ab676SJeff Bonwick 		VERIFY(random_get_pseudo_bytes((uint8_t *)pbuf + psize,
3440b24ab676SJeff Bonwick 		    SPA_MAXBLOCKSIZE - psize) == 0);
3441b24ab676SJeff Bonwick 
3442b24ab676SJeff Bonwick 		VERIFY(random_get_pseudo_bytes((uint8_t *)pbuf2 + psize,
3443b24ab676SJeff Bonwick 		    SPA_MAXBLOCKSIZE - psize) == 0);
3444b24ab676SJeff Bonwick 
3445b24ab676SJeff Bonwick 		for (lsize = SPA_MAXBLOCKSIZE; lsize > psize;
3446b24ab676SJeff Bonwick 		    lsize -= SPA_MINBLOCKSIZE) {
3447b24ab676SJeff Bonwick 			for (c = 0; c < ZIO_COMPRESS_FUNCTIONS; c++) {
3448b24ab676SJeff Bonwick 				if (zio_decompress_data(c, pbuf, lbuf,
3449b24ab676SJeff Bonwick 				    psize, lsize) == 0 &&
3450b24ab676SJeff Bonwick 				    zio_decompress_data(c, pbuf2, lbuf2,
3451b24ab676SJeff Bonwick 				    psize, lsize) == 0 &&
3452b24ab676SJeff Bonwick 				    bcmp(lbuf, lbuf2, lsize) == 0)
3453b24ab676SJeff Bonwick 					break;
3454b24ab676SJeff Bonwick 			}
3455b24ab676SJeff Bonwick 			if (c != ZIO_COMPRESS_FUNCTIONS)
3456b24ab676SJeff Bonwick 				break;
3457b24ab676SJeff Bonwick 			lsize -= SPA_MINBLOCKSIZE;
3458b24ab676SJeff Bonwick 		}
3459b24ab676SJeff Bonwick 
3460b24ab676SJeff Bonwick 		umem_free(pbuf2, SPA_MAXBLOCKSIZE);
3461b24ab676SJeff Bonwick 		umem_free(lbuf2, SPA_MAXBLOCKSIZE);
3462b24ab676SJeff Bonwick 
3463b24ab676SJeff Bonwick 		if (lsize <= psize) {
3464b24ab676SJeff Bonwick 			(void) printf("Decompress of %s failed\n", thing);
3465b24ab676SJeff Bonwick 			goto out;
3466b24ab676SJeff Bonwick 		}
3467b24ab676SJeff Bonwick 		buf = lbuf;
3468b24ab676SJeff Bonwick 		size = lsize;
3469b24ab676SJeff Bonwick 	} else {
3470b24ab676SJeff Bonwick 		buf = pbuf;
3471b24ab676SJeff Bonwick 		size = psize;
3472b24ab676SJeff Bonwick 	}
3473b24ab676SJeff Bonwick 
347444cd46caSbillm 	if (flags & ZDB_FLAG_PRINT_BLKPTR)
347544cd46caSbillm 		zdb_print_blkptr((blkptr_t *)(void *)
347644cd46caSbillm 		    ((uintptr_t)buf + (uintptr_t)blkptr_offset), flags);
347744cd46caSbillm 	else if (flags & ZDB_FLAG_RAW)
347844cd46caSbillm 		zdb_dump_block_raw(buf, size, flags);
347944cd46caSbillm 	else if (flags & ZDB_FLAG_INDIRECT)
348044cd46caSbillm 		zdb_dump_indirect((blkptr_t *)buf, size / sizeof (blkptr_t),
348144cd46caSbillm 		    flags);
348244cd46caSbillm 	else if (flags & ZDB_FLAG_GBH)
348344cd46caSbillm 		zdb_dump_gbh(buf, flags);
348444cd46caSbillm 	else
348544cd46caSbillm 		zdb_dump_block(thing, buf, size, flags);
348644cd46caSbillm 
348744cd46caSbillm out:
3488b24ab676SJeff Bonwick 	umem_free(pbuf, SPA_MAXBLOCKSIZE);
3489b24ab676SJeff Bonwick 	umem_free(lbuf, SPA_MAXBLOCKSIZE);
349044cd46caSbillm 	free(dup);
349144cd46caSbillm }
349244cd46caSbillm 
3493de6628f0Sck153898 static boolean_t
pool_match(nvlist_t * cfg,char * tgt)34943ad6c7f9SVictor Latushkin pool_match(nvlist_t *cfg, char *tgt)
3495de6628f0Sck153898 {
34963ad6c7f9SVictor Latushkin 	uint64_t v, guid = strtoull(tgt, NULL, 0);
3497de6628f0Sck153898 	char *s;
3498de6628f0Sck153898 
34993ad6c7f9SVictor Latushkin 	if (guid != 0) {
35003ad6c7f9SVictor Latushkin 		if (nvlist_lookup_uint64(cfg, ZPOOL_CONFIG_POOL_GUID, &v) == 0)
35013ad6c7f9SVictor Latushkin 			return (v == guid);
35023ad6c7f9SVictor Latushkin 	} else {
35033ad6c7f9SVictor Latushkin 		if (nvlist_lookup_string(cfg, ZPOOL_CONFIG_POOL_NAME, &s) == 0)
3504de6628f0Sck153898 			return (strcmp(s, tgt) == 0);
3505de6628f0Sck153898 	}
3506de6628f0Sck153898 	return (B_FALSE);
3507de6628f0Sck153898 }
3508de6628f0Sck153898 
35093ad6c7f9SVictor Latushkin static char *
find_zpool(char ** target,nvlist_t ** configp,int dirc,char ** dirv)35103ad6c7f9SVictor Latushkin find_zpool(char **target, nvlist_t **configp, int dirc, char **dirv)
3511de6628f0Sck153898 {
3512de6628f0Sck153898 	nvlist_t *pools;
3513de6628f0Sck153898 	nvlist_t *match = NULL;
35143ad6c7f9SVictor Latushkin 	char *name = NULL;
35153ad6c7f9SVictor Latushkin 	char *sepp = NULL;
3516a1988827SMatthew Ahrens 	char sep = '\0';
35173ad6c7f9SVictor Latushkin 	int count = 0;
3518d41c4376SMark J Musante 	importargs_t args = { 0 };
3519d41c4376SMark J Musante 
3520d41c4376SMark J Musante 	args.paths = dirc;
3521d41c4376SMark J Musante 	args.path = dirv;
3522d41c4376SMark J Musante 	args.can_be_active = B_TRUE;
3523de6628f0Sck153898 
35243ad6c7f9SVictor Latushkin 	if ((sepp = strpbrk(*target, "/@")) != NULL) {
35253ad6c7f9SVictor Latushkin 		sep = *sepp;
35263ad6c7f9SVictor Latushkin 		*sepp = '\0';
35273ad6c7f9SVictor Latushkin 	}
35283ad6c7f9SVictor Latushkin 
3529d41c4376SMark J Musante 	pools = zpool_search_import(g_zfs, &args);
3530de6628f0Sck153898 
3531de6628f0Sck153898 	if (pools != NULL) {
3532de6628f0Sck153898 		nvpair_t *elem = NULL;
3533de6628f0Sck153898 		while ((elem = nvlist_next_nvpair(pools, elem)) != NULL) {
3534de6628f0Sck153898 			verify(nvpair_value_nvlist(elem, configp) == 0);
35353ad6c7f9SVictor Latushkin 			if (pool_match(*configp, *target)) {
35363ad6c7f9SVictor Latushkin 				count++;
3537de6628f0Sck153898 				if (match != NULL) {
35383ad6c7f9SVictor Latushkin 					/* print previously found config */
35393ad6c7f9SVictor Latushkin 					if (name != NULL) {
35403ad6c7f9SVictor Latushkin 						(void) printf("%s\n", name);
35413ad6c7f9SVictor Latushkin 						dump_nvlist(match, 8);
35423ad6c7f9SVictor Latushkin 						name = NULL;
35433ad6c7f9SVictor Latushkin 					}
35443ad6c7f9SVictor Latushkin 					(void) printf("%s\n",
35453ad6c7f9SVictor Latushkin 					    nvpair_name(elem));
35463ad6c7f9SVictor Latushkin 					dump_nvlist(*configp, 8);
3547de6628f0Sck153898 				} else {
3548de6628f0Sck153898 					match = *configp;
35493ad6c7f9SVictor Latushkin 					name = nvpair_name(elem);
3550de6628f0Sck153898 				}
3551de6628f0Sck153898 			}
3552de6628f0Sck153898 		}
3553de6628f0Sck153898 	}
35543ad6c7f9SVictor Latushkin 	if (count > 1)
35553ad6c7f9SVictor Latushkin 		(void) fatal("\tMatched %d pools - use pool GUID "
35563ad6c7f9SVictor Latushkin 		    "instead of pool name or \n"
35573ad6c7f9SVictor Latushkin 		    "\tpool name part of a dataset name to select pool", count);
35583ad6c7f9SVictor Latushkin 
35593ad6c7f9SVictor Latushkin 	if (sepp)
35603ad6c7f9SVictor Latushkin 		*sepp = sep;
35613ad6c7f9SVictor Latushkin 	/*
35623ad6c7f9SVictor Latushkin 	 * If pool GUID was specified for pool id, replace it with pool name
35633ad6c7f9SVictor Latushkin 	 */
35643ad6c7f9SVictor Latushkin 	if (name && (strstr(*target, name) != *target)) {
35653ad6c7f9SVictor Latushkin 		int sz = 1 + strlen(name) + ((sepp) ? strlen(sepp) : 0);
35663ad6c7f9SVictor Latushkin 
35673ad6c7f9SVictor Latushkin 		*target = umem_alloc(sz, UMEM_NOFAIL);
35683ad6c7f9SVictor Latushkin 		(void) snprintf(*target, sz, "%s%s", name, sepp ? sepp : "");
35693ad6c7f9SVictor Latushkin 	}
3570de6628f0Sck153898 
35713ad6c7f9SVictor Latushkin 	*configp = name ? match : NULL;
3572de6628f0Sck153898 
35733ad6c7f9SVictor Latushkin 	return (name);
3574de6628f0Sck153898 }
3575de6628f0Sck153898 
3576fa9e4066Sahrens int
main(int argc,char ** argv)3577fa9e4066Sahrens main(int argc, char **argv)
3578fa9e4066Sahrens {
3579fa9e4066Sahrens 	int i, c;
3580fa9e4066Sahrens 	struct rlimit rl = { 1024, 1024 };
35813ad6c7f9SVictor Latushkin 	spa_t *spa = NULL;
3582fa9e4066Sahrens 	objset_t *os = NULL;
3583fa9e4066Sahrens 	int dump_all = 1;
3584fa9e4066Sahrens 	int verbose = 0;
3585c8ee1847SVictor Latushkin 	int error = 0;
35863ad6c7f9SVictor Latushkin 	char **searchdirs = NULL;
35873ad6c7f9SVictor Latushkin 	int nsearch = 0;
35883ad6c7f9SVictor Latushkin 	char *target;
3589468c413aSTim Haley 	nvlist_t *policy = NULL;
3590468c413aSTim Haley 	uint64_t max_txg = UINT64_MAX;
3591c8ee1847SVictor Latushkin 	int rewind = ZPOOL_NEVER_REWIND;
3592fa9e4066Sahrens 
3593fa9e4066Sahrens 	(void) setrlimit(RLIMIT_NOFILE, &rl);
3594004388ebScasper 	(void) enable_extended_FILE_stdio(-1, -1);
3595fa9e4066Sahrens 
3596fa9e4066Sahrens 	dprintf_setup(&argc, argv);
3597fa9e4066Sahrens 
3598df15e419SMatthew Ahrens 	while ((c = getopt(argc, argv,
35992e4c9986SGeorge Wilson 	    "bcdhilmMI:suCDRSAFLXx:evp:t:U:P")) != -1) {
3600fa9e4066Sahrens 		switch (c) {
3601fa9e4066Sahrens 		case 'b':
3602fa9e4066Sahrens 		case 'c':
3603b24ab676SJeff Bonwick 		case 'd':
3604b24ab676SJeff Bonwick 		case 'h':
3605b24ab676SJeff Bonwick 		case 'i':
3606b24ab676SJeff Bonwick 		case 'l':
3607d6e555bdSGeorge Wilson 		case 'm':
3608fa9e4066Sahrens 		case 's':
3609b24ab676SJeff Bonwick 		case 'u':
3610fa9e4066Sahrens 		case 'C':
3611b24ab676SJeff Bonwick 		case 'D':
36122e4c9986SGeorge Wilson 		case 'M':
361344cd46caSbillm 		case 'R':
3614b24ab676SJeff Bonwick 		case 'S':
3615fa9e4066Sahrens 			dump_opt[c]++;
3616fa9e4066Sahrens 			dump_all = 0;
3617fa9e4066Sahrens 			break;
3618feef89cfSVictor Latushkin 		case 'A':
3619c8ee1847SVictor Latushkin 		case 'F':
362082a0a985SVictor Latushkin 		case 'L':
3621c8ee1847SVictor Latushkin 		case 'X':
36223ad6c7f9SVictor Latushkin 		case 'e':
36233f9d6ad7SLin Ling 		case 'P':
362482a0a985SVictor Latushkin 			dump_opt[c]++;
362582a0a985SVictor Latushkin 			break;
36262e4c9986SGeorge Wilson 		case 'I':
362731d7e8faSGeorge Wilson 			max_inflight = strtoull(optarg, NULL, 0);
362831d7e8faSGeorge Wilson 			if (max_inflight == 0) {
362931d7e8faSGeorge Wilson 				(void) fprintf(stderr, "maximum number "
363031d7e8faSGeorge Wilson 				    "of inflight I/Os must be greater "
363131d7e8faSGeorge Wilson 				    "than 0\n");
363231d7e8faSGeorge Wilson 				usage();
363331d7e8faSGeorge Wilson 			}
363431d7e8faSGeorge Wilson 			break;
3635de6628f0Sck153898 		case 'p':
36363ad6c7f9SVictor Latushkin 			if (searchdirs == NULL) {
36373ad6c7f9SVictor Latushkin 				searchdirs = umem_alloc(sizeof (char *),
36383ad6c7f9SVictor Latushkin 				    UMEM_NOFAIL);
36393ad6c7f9SVictor Latushkin 			} else {
36403ad6c7f9SVictor Latushkin 				char **tmp = umem_alloc((nsearch + 1) *
36413ad6c7f9SVictor Latushkin 				    sizeof (char *), UMEM_NOFAIL);
36423ad6c7f9SVictor Latushkin 				bcopy(searchdirs, tmp, nsearch *
36433ad6c7f9SVictor Latushkin 				    sizeof (char *));
36443ad6c7f9SVictor Latushkin 				umem_free(searchdirs,
36453ad6c7f9SVictor Latushkin 				    nsearch * sizeof (char *));
36463ad6c7f9SVictor Latushkin 				searchdirs = tmp;
36473ad6c7f9SVictor Latushkin 			}
36483ad6c7f9SVictor Latushkin 			searchdirs[nsearch++] = optarg;
3649de6628f0Sck153898 			break;
36502e551927SVictor Latushkin 		case 't':
3651468c413aSTim Haley 			max_txg = strtoull(optarg, NULL, 0);
3652468c413aSTim Haley 			if (max_txg < TXG_INITIAL) {
36532e551927SVictor Latushkin 				(void) fprintf(stderr, "incorrect txg "
36542e551927SVictor Latushkin 				    "specified: %s\n", optarg);
36552e551927SVictor Latushkin 				usage();
36562e551927SVictor Latushkin 			}
36572e551927SVictor Latushkin 			break;
3658b24ab676SJeff Bonwick 		case 'U':
3659b24ab676SJeff Bonwick 			spa_config_path = optarg;
3660b24ab676SJeff Bonwick 			break;
36612e4c9986SGeorge Wilson 		case 'v':
36622e4c9986SGeorge Wilson 			verbose++;
36632e4c9986SGeorge Wilson 			break;
36642e4c9986SGeorge Wilson 		case 'x':
36652e4c9986SGeorge Wilson 			vn_dumpdir = optarg;
36662e4c9986SGeorge Wilson 			break;
3667fa9e4066Sahrens 		default:
3668fa9e4066Sahrens 			usage();
3669fa9e4066Sahrens 			break;
3670fa9e4066Sahrens 		}
3671fa9e4066Sahrens 	}
3672fa9e4066Sahrens 
36733ad6c7f9SVictor Latushkin 	if (!dump_opt['e'] && searchdirs != NULL) {
367488b7b0f2SMatthew Ahrens 		(void) fprintf(stderr, "-p option requires use of -e\n");
367588b7b0f2SMatthew Ahrens 		usage();
367688b7b0f2SMatthew Ahrens 	}
3677de6628f0Sck153898 
367806be9802SMatthew Ahrens 	/*
367906be9802SMatthew Ahrens 	 * ZDB does not typically re-read blocks; therefore limit the ARC
368006be9802SMatthew Ahrens 	 * to 256 MB, which can be used entirely for metadata.
368106be9802SMatthew Ahrens 	 */
368206be9802SMatthew Ahrens 	zfs_arc_max = zfs_arc_meta_limit = 256 * 1024 * 1024;
368306be9802SMatthew Ahrens 
3684f7950bf1SMatthew Ahrens 	/*
3685f7950bf1SMatthew Ahrens 	 * "zdb -c" uses checksum-verifying scrub i/os which are async reads.
3686f7950bf1SMatthew Ahrens 	 * "zdb -b" uses traversal prefetch which uses async reads.
3687f7950bf1SMatthew Ahrens 	 * For good performance, let several of them be active at once.
3688f7950bf1SMatthew Ahrens 	 */
3689f7950bf1SMatthew Ahrens 	zfs_vdev_async_read_max_active = 10;
3690f7950bf1SMatthew Ahrens 
3691fa9e4066Sahrens 	kernel_init(FREAD);
3692de6628f0Sck153898 	g_zfs = libzfs_init();
369391ebeef5Sahrens 	ASSERT(g_zfs != NULL);
3694fa9e4066Sahrens 
3695b24ab676SJeff Bonwick 	if (dump_all)
3696b24ab676SJeff Bonwick 		verbose = MAX(verbose, 1);
3697b24ab676SJeff Bonwick 
3698fa9e4066Sahrens 	for (c = 0; c < 256; c++) {
36993f9d6ad7SLin Ling 		if (dump_all && !strchr("elAFLRSXP", c))
3700fa9e4066Sahrens 			dump_opt[c] = 1;
3701fa9e4066Sahrens 		if (dump_opt[c])
3702fa9e4066Sahrens 			dump_opt[c] += verbose;
3703fa9e4066Sahrens 	}
3704fa9e4066Sahrens 
3705feef89cfSVictor Latushkin 	aok = (dump_opt['A'] == 1) || (dump_opt['A'] > 2);
3706feef89cfSVictor Latushkin 	zfs_recover = (dump_opt['A'] > 1);
3707feef89cfSVictor Latushkin 
3708fa9e4066Sahrens 	argc -= optind;
3709fa9e4066Sahrens 	argv += optind;
3710fa9e4066Sahrens 
371107428bdfSVictor Latushkin 	if (argc < 2 && dump_opt['R'])
371207428bdfSVictor Latushkin 		usage();
3713fa9e4066Sahrens 	if (argc < 1) {
37143ad6c7f9SVictor Latushkin 		if (!dump_opt['e'] && dump_opt['C']) {
3715e829d913Sck153898 			dump_cachefile(spa_config_path);
3716fa9e4066Sahrens 			return (0);
3717fa9e4066Sahrens 		}
3718fa9e4066Sahrens 		usage();
3719fa9e4066Sahrens 	}
3720fa9e4066Sahrens 
3721fa9e4066Sahrens 	if (dump_opt['l']) {
3722fa9e4066Sahrens 		dump_label(argv[0]);
3723fa9e4066Sahrens 		return (0);
3724fa9e4066Sahrens 	}
3725fa9e4066Sahrens 
3726c8ee1847SVictor Latushkin 	if (dump_opt['X'] || dump_opt['F'])
3727c8ee1847SVictor Latushkin 		rewind = ZPOOL_DO_REWIND |
3728c8ee1847SVictor Latushkin 		    (dump_opt['X'] ? ZPOOL_EXTREME_REWIND : 0);
3729c8ee1847SVictor Latushkin 
3730c8ee1847SVictor Latushkin 	if (nvlist_alloc(&policy, NV_UNIQUE_NAME_TYPE, 0) != 0 ||
3731c8ee1847SVictor Latushkin 	    nvlist_add_uint64(policy, ZPOOL_REWIND_REQUEST_TXG, max_txg) != 0 ||
3732c8ee1847SVictor Latushkin 	    nvlist_add_uint32(policy, ZPOOL_REWIND_REQUEST, rewind) != 0)
3733c8ee1847SVictor Latushkin 		fatal("internal error: %s", strerror(ENOMEM));
3734c8ee1847SVictor Latushkin 
3735c5904d13Seschrock 	error = 0;
37363ad6c7f9SVictor Latushkin 	target = argv[0];
3737de6628f0Sck153898 
37383ad6c7f9SVictor Latushkin 	if (dump_opt['e']) {
37393ad6c7f9SVictor Latushkin 		nvlist_t *cfg = NULL;
37403ad6c7f9SVictor Latushkin 		char *name = find_zpool(&target, &cfg, nsearch, searchdirs);
3741c5904d13Seschrock 
37423ad6c7f9SVictor Latushkin 		error = ENOENT;
37433ad6c7f9SVictor Latushkin 		if (name) {
374407428bdfSVictor Latushkin 			if (dump_opt['C'] > 1) {
374507428bdfSVictor Latushkin 				(void) printf("\nConfiguration for import:\n");
374607428bdfSVictor Latushkin 				dump_nvlist(cfg, 8);
374707428bdfSVictor Latushkin 			}
3748c8ee1847SVictor Latushkin 			if (nvlist_add_nvlist(cfg,
3749468c413aSTim Haley 			    ZPOOL_REWIND_POLICY, policy) != 0) {
3750468c413aSTim Haley 				fatal("can't open '%s': %s",
3751468c413aSTim Haley 				    target, strerror(ENOMEM));
3752468c413aSTim Haley 			}
37534b964adaSGeorge Wilson 			if ((error = spa_import(name, cfg, NULL,
37544b964adaSGeorge Wilson 			    ZFS_IMPORT_MISSING_LOG)) != 0) {
37554b964adaSGeorge Wilson 				error = spa_import(name, cfg, NULL,
37564b964adaSGeorge Wilson 				    ZFS_IMPORT_VERBATIM);
37574b964adaSGeorge Wilson 			}
3758990b4856Slling 		}
3759c5904d13Seschrock 	}
3760c5904d13Seschrock 
3761c5904d13Seschrock 	if (error == 0) {
376207428bdfSVictor Latushkin 		if (strpbrk(target, "/@") == NULL || dump_opt['R']) {
376380eb36f2SGeorge Wilson 			error = spa_open_rewind(target, &spa, FTAG, policy,
376480eb36f2SGeorge Wilson 			    NULL);
37658f18d1faSGeorge Wilson 			if (error) {
37668f18d1faSGeorge Wilson 				/*
37678f18d1faSGeorge Wilson 				 * If we're missing the log device then
37688f18d1faSGeorge Wilson 				 * try opening the pool after clearing the
37698f18d1faSGeorge Wilson 				 * log state.
37708f18d1faSGeorge Wilson 				 */
37718f18d1faSGeorge Wilson 				mutex_enter(&spa_namespace_lock);
37723ad6c7f9SVictor Latushkin 				if ((spa = spa_lookup(target)) != NULL &&
37738f18d1faSGeorge Wilson 				    spa->spa_log_state == SPA_LOG_MISSING) {
37748f18d1faSGeorge Wilson 					spa->spa_log_state = SPA_LOG_CLEAR;
37758f18d1faSGeorge Wilson 					error = 0;
37768f18d1faSGeorge Wilson 				}
37778f18d1faSGeorge Wilson 				mutex_exit(&spa_namespace_lock);
37788f18d1faSGeorge Wilson 
377980eb36f2SGeorge Wilson 				if (!error) {
378080eb36f2SGeorge Wilson 					error = spa_open_rewind(target, &spa,
378180eb36f2SGeorge Wilson 					    FTAG, policy, NULL);
378280eb36f2SGeorge Wilson 				}
37838f18d1faSGeorge Wilson 			}
378407428bdfSVictor Latushkin 		} else {
378507428bdfSVictor Latushkin 			error = dmu_objset_own(target, DMU_OST_ANY,
378607428bdfSVictor Latushkin 			    B_TRUE, FTAG, &os);
3787c5904d13Seschrock 		}
3788de6628f0Sck153898 	}
378980eb36f2SGeorge Wilson 	nvlist_free(policy);
379080eb36f2SGeorge Wilson 
3791fa9e4066Sahrens 	if (error)
37923ad6c7f9SVictor Latushkin 		fatal("can't open '%s': %s", target, strerror(error));
3793fa9e4066Sahrens 
3794fa9e4066Sahrens 	argv++;
379507428bdfSVictor Latushkin 	argc--;
379607428bdfSVictor Latushkin 	if (!dump_opt['R']) {
379707428bdfSVictor Latushkin 		if (argc > 0) {
3798fa9e4066Sahrens 			zopt_objects = argc;
3799fa9e4066Sahrens 			zopt_object = calloc(zopt_objects, sizeof (uint64_t));
3800fa9e4066Sahrens 			for (i = 0; i < zopt_objects; i++) {
3801fa9e4066Sahrens 				errno = 0;
3802fa9e4066Sahrens 				zopt_object[i] = strtoull(argv[i], NULL, 0);
3803fa9e4066Sahrens 				if (zopt_object[i] == 0 && errno != 0)
380487219db7SVictor Latushkin 					fatal("bad number %s: %s",
3805fa9e4066Sahrens 					    argv[i], strerror(errno));
3806fa9e4066Sahrens 			}
3807fa9e4066Sahrens 		}
3808e690fb27SChristopher Siden 		if (os != NULL) {
3809e690fb27SChristopher Siden 			dump_dir(os);
3810e690fb27SChristopher Siden 		} else if (zopt_objects > 0 && !dump_opt['m']) {
3811e690fb27SChristopher Siden 			dump_dir(spa->spa_meta_objset);
3812e690fb27SChristopher Siden 		} else {
3813e690fb27SChristopher Siden 			dump_zpool(spa);
3814e690fb27SChristopher Siden 		}
3815fa9e4066Sahrens 	} else {
381607428bdfSVictor Latushkin 		flagbits['b'] = ZDB_FLAG_PRINT_BLKPTR;
381707428bdfSVictor Latushkin 		flagbits['c'] = ZDB_FLAG_CHECKSUM;
381807428bdfSVictor Latushkin 		flagbits['d'] = ZDB_FLAG_DECOMPRESS;
381907428bdfSVictor Latushkin 		flagbits['e'] = ZDB_FLAG_BSWAP;
382007428bdfSVictor Latushkin 		flagbits['g'] = ZDB_FLAG_GBH;
382107428bdfSVictor Latushkin 		flagbits['i'] = ZDB_FLAG_INDIRECT;
382207428bdfSVictor Latushkin 		flagbits['p'] = ZDB_FLAG_PHYS;
382307428bdfSVictor Latushkin 		flagbits['r'] = ZDB_FLAG_RAW;
382407428bdfSVictor Latushkin 
382507428bdfSVictor Latushkin 		for (i = 0; i < argc; i++)
382607428bdfSVictor Latushkin 			zdb_read_block(argv[i], spa);
3827fa9e4066Sahrens 	}
3828fa9e4066Sahrens 
382907428bdfSVictor Latushkin 	(os != NULL) ? dmu_objset_disown(os, FTAG) : spa_close(spa, FTAG);
383007428bdfSVictor Latushkin 
3831e0d35c44Smarks 	fuid_table_destroy();
38320a586ceaSMark Shellenbaum 	sa_loaded = B_FALSE;
3833e0d35c44Smarks 
3834de6628f0Sck153898 	libzfs_fini(g_zfs);
3835fa9e4066Sahrens 	kernel_fini();
3836fa9e4066Sahrens 
3837fa9e4066Sahrens 	return (0);
3838fa9e4066Sahrens }
3839