xref: /titanic_41/usr/src/cmd/zdb/zdb.c (revision ad135b5d644628e791c3188a6ecbd9c257961ef8)
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  */
21*ad135b5dSChristopher Siden 
22fa9e4066Sahrens /*
238f2529deSMark Shellenbaum  * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
24*ad135b5dSChristopher Siden  * Copyright (c) 2012 by Delphix. All rights reserved.
25fa9e4066Sahrens  */
26fa9e4066Sahrens 
27fa9e4066Sahrens #include <stdio.h>
28004388ebScasper #include <stdio_ext.h>
29fa9e4066Sahrens #include <stdlib.h>
3044cd46caSbillm #include <ctype.h>
31fa9e4066Sahrens #include <sys/zfs_context.h>
32fa9e4066Sahrens #include <sys/spa.h>
33fa9e4066Sahrens #include <sys/spa_impl.h>
34fa9e4066Sahrens #include <sys/dmu.h>
35fa9e4066Sahrens #include <sys/zap.h>
36fa9e4066Sahrens #include <sys/fs/zfs.h>
37fa9e4066Sahrens #include <sys/zfs_znode.h>
380a586ceaSMark Shellenbaum #include <sys/zfs_sa.h>
390a586ceaSMark Shellenbaum #include <sys/sa.h>
400a586ceaSMark Shellenbaum #include <sys/sa_impl.h>
41fa9e4066Sahrens #include <sys/vdev.h>
42fa9e4066Sahrens #include <sys/vdev_impl.h>
43fa9e4066Sahrens #include <sys/metaslab_impl.h>
44fa9e4066Sahrens #include <sys/dmu_objset.h>
45fa9e4066Sahrens #include <sys/dsl_dir.h>
46fa9e4066Sahrens #include <sys/dsl_dataset.h>
47fa9e4066Sahrens #include <sys/dsl_pool.h>
48fa9e4066Sahrens #include <sys/dbuf.h>
49fa9e4066Sahrens #include <sys/zil.h>
50fa9e4066Sahrens #include <sys/zil_impl.h>
51fa9e4066Sahrens #include <sys/stat.h>
52fa9e4066Sahrens #include <sys/resource.h>
53fa9e4066Sahrens #include <sys/dmu_traverse.h>
54fa9e4066Sahrens #include <sys/zio_checksum.h>
55fa9e4066Sahrens #include <sys/zio_compress.h>
56e0d35c44Smarks #include <sys/zfs_fuid.h>
5788b7b0f2SMatthew Ahrens #include <sys/arc.h>
58b24ab676SJeff Bonwick #include <sys/ddt.h>
59*ad135b5dSChristopher Siden #include <sys/zfeature.h>
60de6628f0Sck153898 #undef ZFS_MAXNAMELEN
61de6628f0Sck153898 #undef verify
62de6628f0Sck153898 #include <libzfs.h>
63fa9e4066Sahrens 
646de8f417SVictor Latushkin #define	ZDB_COMPRESS_NAME(idx) ((idx) < ZIO_COMPRESS_FUNCTIONS ? \
656de8f417SVictor Latushkin     zio_compress_table[(idx)].ci_name : "UNKNOWN")
666de8f417SVictor Latushkin #define	ZDB_CHECKSUM_NAME(idx) ((idx) < ZIO_CHECKSUM_FUNCTIONS ? \
676de8f417SVictor Latushkin     zio_checksum_table[(idx)].ci_name : "UNKNOWN")
686de8f417SVictor Latushkin #define	ZDB_OT_NAME(idx) ((idx) < DMU_OT_NUMTYPES ? \
69*ad135b5dSChristopher Siden     dmu_ot[(idx)].ot_name : DMU_OT_IS_VALID(idx) ? \
70*ad135b5dSChristopher Siden     dmu_ot_byteswap[DMU_OT_BYTESWAP(idx)].ob_name : "UNKNOWN")
716de8f417SVictor Latushkin #define	ZDB_OT_TYPE(idx) ((idx) < DMU_OT_NUMTYPES ? (idx) : DMU_OT_NUMTYPES)
726de8f417SVictor Latushkin 
73feef89cfSVictor Latushkin #ifndef lint
74feef89cfSVictor Latushkin extern int zfs_recover;
75feef89cfSVictor Latushkin #else
76feef89cfSVictor Latushkin int zfs_recover;
77feef89cfSVictor Latushkin #endif
78feef89cfSVictor Latushkin 
79fa9e4066Sahrens const char cmdname[] = "zdb";
80fa9e4066Sahrens uint8_t dump_opt[256];
81fa9e4066Sahrens 
82fa9e4066Sahrens typedef void object_viewer_t(objset_t *, uint64_t, void *data, size_t size);
83fa9e4066Sahrens 
84fa9e4066Sahrens extern void dump_intent_log(zilog_t *);
85fa9e4066Sahrens uint64_t *zopt_object = NULL;
86fa9e4066Sahrens int zopt_objects = 0;
87de6628f0Sck153898 libzfs_handle_t *g_zfs;
88fa9e4066Sahrens 
89fa9e4066Sahrens /*
90fa9e4066Sahrens  * These libumem hooks provide a reasonable set of defaults for the allocator's
91fa9e4066Sahrens  * debugging facilities.
92fa9e4066Sahrens  */
93fa9e4066Sahrens const char *
94fa9e4066Sahrens _umem_debug_init()
95fa9e4066Sahrens {
96fa9e4066Sahrens 	return ("default,verbose"); /* $UMEM_DEBUG setting */
97fa9e4066Sahrens }
98fa9e4066Sahrens 
99fa9e4066Sahrens const char *
100fa9e4066Sahrens _umem_logging_init(void)
101fa9e4066Sahrens {
102fa9e4066Sahrens 	return ("fail,contents"); /* $UMEM_LOGGING setting */
103fa9e4066Sahrens }
104fa9e4066Sahrens 
105fa9e4066Sahrens static void
106fa9e4066Sahrens usage(void)
107fa9e4066Sahrens {
108fa9e4066Sahrens 	(void) fprintf(stderr,
10990e894e2SRichard Lowe 	    "Usage: %s [-CumdibcsDvhLXFPA] [-t txg] [-e [-p path...]] "
11090e894e2SRichard Lowe 	    "poolname [object...]\n"
11190e894e2SRichard Lowe 	    "       %s [-divPA] [-e -p path...] dataset [object...]\n"
11290e894e2SRichard Lowe 	    "       %s -m [-LXFPA] [-t txg] [-e [-p path...]] "
11390e894e2SRichard Lowe 	    "poolname [vdev [metaslab...]]\n"
11490e894e2SRichard Lowe 	    "       %s -R [-A] [-e [-p path...]] poolname "
11590e894e2SRichard Lowe 	    "vdev:offset:size[:flags]\n"
11690e894e2SRichard Lowe 	    "       %s -S [-PA] [-e [-p path...]] poolname\n"
11790e894e2SRichard Lowe 	    "       %s -l [-uA] device\n"
11890e894e2SRichard Lowe 	    "       %s -C [-A] [-U config]\n\n",
119bbfd46c4SJeff Bonwick 	    cmdname, cmdname, cmdname, cmdname, cmdname, cmdname, cmdname);
120fa9e4066Sahrens 
1213ad6c7f9SVictor Latushkin 	(void) fprintf(stderr, "    Dataset name must include at least one "
1223ad6c7f9SVictor Latushkin 	    "separator character '/' or '@'\n");
1233ad6c7f9SVictor Latushkin 	(void) fprintf(stderr, "    If dataset name is specified, only that "
1243ad6c7f9SVictor Latushkin 	    "dataset is dumped\n");
1253ad6c7f9SVictor Latushkin 	(void) fprintf(stderr, "    If object numbers are specified, only "
1263ad6c7f9SVictor Latushkin 	    "those objects are dumped\n\n");
1273ad6c7f9SVictor Latushkin 	(void) fprintf(stderr, "    Options to control amount of output:\n");
128fa9e4066Sahrens 	(void) fprintf(stderr, "        -u uberblock\n");
1293ad6c7f9SVictor Latushkin 	(void) fprintf(stderr, "        -d dataset(s)\n");
1303ad6c7f9SVictor Latushkin 	(void) fprintf(stderr, "        -i intent logs\n");
13107428bdfSVictor Latushkin 	(void) fprintf(stderr, "        -C config (or cachefile if alone)\n");
1328f18d1faSGeorge Wilson 	(void) fprintf(stderr, "        -h pool history\n");
133fa9e4066Sahrens 	(void) fprintf(stderr, "        -b block statistics\n");
134d6e555bdSGeorge Wilson 	(void) fprintf(stderr, "        -m metaslabs\n");
1356365109dSVictor Latushkin 	(void) fprintf(stderr, "        -c checksum all metadata (twice for "
1366365109dSVictor Latushkin 	    "all data) blocks\n");
137fa9e4066Sahrens 	(void) fprintf(stderr, "        -s report stats on zdb's I/O\n");
138f0ba89beSJeff Bonwick 	(void) fprintf(stderr, "        -D dedup statistics\n");
139b24ab676SJeff Bonwick 	(void) fprintf(stderr, "        -S simulate dedup to measure effect\n");
140fa9e4066Sahrens 	(void) fprintf(stderr, "        -v verbose (applies to all others)\n");
141fa9e4066Sahrens 	(void) fprintf(stderr, "        -l dump label contents\n");
14282a0a985SVictor Latushkin 	(void) fprintf(stderr, "        -L disable leak tracking (do not "
14382a0a985SVictor Latushkin 	    "load spacemaps)\n");
14444cd46caSbillm 	(void) fprintf(stderr, "        -R read and display block from a "
1453ad6c7f9SVictor Latushkin 	    "device\n\n");
1463ad6c7f9SVictor Latushkin 	(void) fprintf(stderr, "    Below options are intended for use "
1473ad6c7f9SVictor Latushkin 	    "with other options (except -l):\n");
148feef89cfSVictor Latushkin 	(void) fprintf(stderr, "        -A ignore assertions (-A), enable "
149feef89cfSVictor Latushkin 	    "panic recovery (-AA) or both (-AAA)\n");
150c8ee1847SVictor Latushkin 	(void) fprintf(stderr, "        -F attempt automatic rewind within "
151c8ee1847SVictor Latushkin 	    "safe range of transaction groups\n");
1523ad6c7f9SVictor Latushkin 	(void) fprintf(stderr, "        -U <cachefile_path> -- use alternate "
1533ad6c7f9SVictor Latushkin 	    "cachefile\n");
154c8ee1847SVictor Latushkin 	(void) fprintf(stderr, "        -X attempt extreme rewind (does not "
155c8ee1847SVictor Latushkin 	    "work with dataset)\n");
1563ad6c7f9SVictor Latushkin 	(void) fprintf(stderr, "        -e pool is exported/destroyed/"
1573ad6c7f9SVictor Latushkin 	    "has altroot/not in a cachefile\n");
1583ad6c7f9SVictor Latushkin 	(void) fprintf(stderr, "        -p <path> -- use one or more with "
1593ad6c7f9SVictor Latushkin 	    "-e to specify path to vdev dir\n");
16090e894e2SRichard Lowe 	(void) fprintf(stderr, "        -P print numbers in parseable form\n");
1613ad6c7f9SVictor Latushkin 	(void) fprintf(stderr, "        -t <txg> -- highest txg to use when "
1622e551927SVictor Latushkin 	    "searching for uberblocks\n");
163fa9e4066Sahrens 	(void) fprintf(stderr, "Specify an option more than once (e.g. -bb) "
164fa9e4066Sahrens 	    "to make only that option verbose\n");
165fa9e4066Sahrens 	(void) fprintf(stderr, "Default is to dump everything non-verbosely\n");
166fa9e4066Sahrens 	exit(1);
167fa9e4066Sahrens }
168fa9e4066Sahrens 
169ccba0801SRich Morris /*
170ccba0801SRich Morris  * Called for usage errors that are discovered after a call to spa_open(),
171ccba0801SRich Morris  * dmu_bonus_hold(), or pool_match().  abort() is called for other errors.
172ccba0801SRich Morris  */
173ccba0801SRich Morris 
174fa9e4066Sahrens static void
175fa9e4066Sahrens fatal(const char *fmt, ...)
176fa9e4066Sahrens {
177fa9e4066Sahrens 	va_list ap;
178fa9e4066Sahrens 
179fa9e4066Sahrens 	va_start(ap, fmt);
180fa9e4066Sahrens 	(void) fprintf(stderr, "%s: ", cmdname);
181fa9e4066Sahrens 	(void) vfprintf(stderr, fmt, ap);
182fa9e4066Sahrens 	va_end(ap);
183fa9e4066Sahrens 	(void) fprintf(stderr, "\n");
184fa9e4066Sahrens 
185ccba0801SRich Morris 	exit(1);
186fa9e4066Sahrens }
187fa9e4066Sahrens 
188fa9e4066Sahrens /* ARGSUSED */
189fa9e4066Sahrens static void
190fa9e4066Sahrens dump_packed_nvlist(objset_t *os, uint64_t object, void *data, size_t size)
191fa9e4066Sahrens {
192fa9e4066Sahrens 	nvlist_t *nv;
193fa9e4066Sahrens 	size_t nvsize = *(uint64_t *)data;
194fa9e4066Sahrens 	char *packed = umem_alloc(nvsize, UMEM_NOFAIL);
195fa9e4066Sahrens 
1967bfdf011SNeil Perrin 	VERIFY(0 == dmu_read(os, object, 0, nvsize, packed, DMU_READ_PREFETCH));
197fa9e4066Sahrens 
198fa9e4066Sahrens 	VERIFY(nvlist_unpack(packed, nvsize, &nv, 0) == 0);
199fa9e4066Sahrens 
200fa9e4066Sahrens 	umem_free(packed, nvsize);
201fa9e4066Sahrens 
202fa9e4066Sahrens 	dump_nvlist(nv, 8);
203fa9e4066Sahrens 
204fa9e4066Sahrens 	nvlist_free(nv);
205fa9e4066Sahrens }
206fa9e4066Sahrens 
2073f9d6ad7SLin Ling static void
2083f9d6ad7SLin Ling zdb_nicenum(uint64_t num, char *buf)
2093f9d6ad7SLin Ling {
2103f9d6ad7SLin Ling 	if (dump_opt['P'])
2113f9d6ad7SLin Ling 		(void) sprintf(buf, "%llu", (longlong_t)num);
2123f9d6ad7SLin Ling 	else
2133f9d6ad7SLin Ling 		nicenum(num, buf);
2143f9d6ad7SLin Ling }
2153f9d6ad7SLin Ling 
216fa9e4066Sahrens const char dump_zap_stars[] = "****************************************";
217fa9e4066Sahrens const int dump_zap_width = sizeof (dump_zap_stars) - 1;
218fa9e4066Sahrens 
219fa9e4066Sahrens static void
220fa9e4066Sahrens dump_zap_histogram(uint64_t histo[ZAP_HISTOGRAM_SIZE])
221fa9e4066Sahrens {
222fa9e4066Sahrens 	int i;
223fa9e4066Sahrens 	int minidx = ZAP_HISTOGRAM_SIZE - 1;
224fa9e4066Sahrens 	int maxidx = 0;
225fa9e4066Sahrens 	uint64_t max = 0;
226fa9e4066Sahrens 
227fa9e4066Sahrens 	for (i = 0; i < ZAP_HISTOGRAM_SIZE; i++) {
228fa9e4066Sahrens 		if (histo[i] > max)
229fa9e4066Sahrens 			max = histo[i];
230fa9e4066Sahrens 		if (histo[i] > 0 && i > maxidx)
231fa9e4066Sahrens 			maxidx = i;
232fa9e4066Sahrens 		if (histo[i] > 0 && i < minidx)
233fa9e4066Sahrens 			minidx = i;
234fa9e4066Sahrens 	}
235fa9e4066Sahrens 
236fa9e4066Sahrens 	if (max < dump_zap_width)
237fa9e4066Sahrens 		max = dump_zap_width;
238fa9e4066Sahrens 
239fa9e4066Sahrens 	for (i = minidx; i <= maxidx; i++)
240fa9e4066Sahrens 		(void) printf("\t\t\t%u: %6llu %s\n", i, (u_longlong_t)histo[i],
241fa9e4066Sahrens 		    &dump_zap_stars[(max - histo[i]) * dump_zap_width / max]);
242fa9e4066Sahrens }
243fa9e4066Sahrens 
244fa9e4066Sahrens static void
245fa9e4066Sahrens dump_zap_stats(objset_t *os, uint64_t object)
246fa9e4066Sahrens {
247fa9e4066Sahrens 	int error;
248fa9e4066Sahrens 	zap_stats_t zs;
249fa9e4066Sahrens 
250fa9e4066Sahrens 	error = zap_get_stats(os, object, &zs);
251fa9e4066Sahrens 	if (error)
252fa9e4066Sahrens 		return;
253fa9e4066Sahrens 
254fa9e4066Sahrens 	if (zs.zs_ptrtbl_len == 0) {
255fa9e4066Sahrens 		ASSERT(zs.zs_num_blocks == 1);
256fa9e4066Sahrens 		(void) printf("\tmicrozap: %llu bytes, %llu entries\n",
257fa9e4066Sahrens 		    (u_longlong_t)zs.zs_blocksize,
258fa9e4066Sahrens 		    (u_longlong_t)zs.zs_num_entries);
259fa9e4066Sahrens 		return;
260fa9e4066Sahrens 	}
261fa9e4066Sahrens 
262fa9e4066Sahrens 	(void) printf("\tFat ZAP stats:\n");
2638248818dSnd150628 
2648248818dSnd150628 	(void) printf("\t\tPointer table:\n");
2658248818dSnd150628 	(void) printf("\t\t\t%llu elements\n",
266fa9e4066Sahrens 	    (u_longlong_t)zs.zs_ptrtbl_len);
2678248818dSnd150628 	(void) printf("\t\t\tzt_blk: %llu\n",
2688248818dSnd150628 	    (u_longlong_t)zs.zs_ptrtbl_zt_blk);
2698248818dSnd150628 	(void) printf("\t\t\tzt_numblks: %llu\n",
2708248818dSnd150628 	    (u_longlong_t)zs.zs_ptrtbl_zt_numblks);
2718248818dSnd150628 	(void) printf("\t\t\tzt_shift: %llu\n",
2728248818dSnd150628 	    (u_longlong_t)zs.zs_ptrtbl_zt_shift);
2738248818dSnd150628 	(void) printf("\t\t\tzt_blks_copied: %llu\n",
2748248818dSnd150628 	    (u_longlong_t)zs.zs_ptrtbl_blks_copied);
2758248818dSnd150628 	(void) printf("\t\t\tzt_nextblk: %llu\n",
2768248818dSnd150628 	    (u_longlong_t)zs.zs_ptrtbl_nextblk);
2778248818dSnd150628 
278fa9e4066Sahrens 	(void) printf("\t\tZAP entries: %llu\n",
279fa9e4066Sahrens 	    (u_longlong_t)zs.zs_num_entries);
280fa9e4066Sahrens 	(void) printf("\t\tLeaf blocks: %llu\n",
281fa9e4066Sahrens 	    (u_longlong_t)zs.zs_num_leafs);
282fa9e4066Sahrens 	(void) printf("\t\tTotal blocks: %llu\n",
283fa9e4066Sahrens 	    (u_longlong_t)zs.zs_num_blocks);
2848248818dSnd150628 	(void) printf("\t\tzap_block_type: 0x%llx\n",
2858248818dSnd150628 	    (u_longlong_t)zs.zs_block_type);
2868248818dSnd150628 	(void) printf("\t\tzap_magic: 0x%llx\n",
2878248818dSnd150628 	    (u_longlong_t)zs.zs_magic);
2888248818dSnd150628 	(void) printf("\t\tzap_salt: 0x%llx\n",
2898248818dSnd150628 	    (u_longlong_t)zs.zs_salt);
290fa9e4066Sahrens 
291fa9e4066Sahrens 	(void) printf("\t\tLeafs with 2^n pointers:\n");
292fa9e4066Sahrens 	dump_zap_histogram(zs.zs_leafs_with_2n_pointers);
293fa9e4066Sahrens 
294fa9e4066Sahrens 	(void) printf("\t\tBlocks with n*5 entries:\n");
295fa9e4066Sahrens 	dump_zap_histogram(zs.zs_blocks_with_n5_entries);
296fa9e4066Sahrens 
297fa9e4066Sahrens 	(void) printf("\t\tBlocks n/10 full:\n");
298fa9e4066Sahrens 	dump_zap_histogram(zs.zs_blocks_n_tenths_full);
299fa9e4066Sahrens 
300fa9e4066Sahrens 	(void) printf("\t\tEntries with n chunks:\n");
301fa9e4066Sahrens 	dump_zap_histogram(zs.zs_entries_using_n_chunks);
302fa9e4066Sahrens 
303fa9e4066Sahrens 	(void) printf("\t\tBuckets with n entries:\n");
304fa9e4066Sahrens 	dump_zap_histogram(zs.zs_buckets_with_n_entries);
305fa9e4066Sahrens }
306fa9e4066Sahrens 
307fa9e4066Sahrens /*ARGSUSED*/
308fa9e4066Sahrens static void
309fa9e4066Sahrens dump_none(objset_t *os, uint64_t object, void *data, size_t size)
310fa9e4066Sahrens {
311fa9e4066Sahrens }
312fa9e4066Sahrens 
313fa9e4066Sahrens /*ARGSUSED*/
3146de8f417SVictor Latushkin static void
3156de8f417SVictor Latushkin dump_unknown(objset_t *os, uint64_t object, void *data, size_t size)
3166de8f417SVictor Latushkin {
3176de8f417SVictor Latushkin 	(void) printf("\tUNKNOWN OBJECT TYPE\n");
3186de8f417SVictor Latushkin }
3196de8f417SVictor Latushkin 
3206de8f417SVictor Latushkin /*ARGSUSED*/
321fa9e4066Sahrens void
322fa9e4066Sahrens dump_uint8(objset_t *os, uint64_t object, void *data, size_t size)
323fa9e4066Sahrens {
324fa9e4066Sahrens }
325fa9e4066Sahrens 
326fa9e4066Sahrens /*ARGSUSED*/
327fa9e4066Sahrens static void
328fa9e4066Sahrens dump_uint64(objset_t *os, uint64_t object, void *data, size_t size)
329fa9e4066Sahrens {
330fa9e4066Sahrens }
331fa9e4066Sahrens 
332fa9e4066Sahrens /*ARGSUSED*/
333fa9e4066Sahrens static void
334fa9e4066Sahrens dump_zap(objset_t *os, uint64_t object, void *data, size_t size)
335fa9e4066Sahrens {
336fa9e4066Sahrens 	zap_cursor_t zc;
337fa9e4066Sahrens 	zap_attribute_t attr;
338fa9e4066Sahrens 	void *prop;
339fa9e4066Sahrens 	int i;
340fa9e4066Sahrens 
341fa9e4066Sahrens 	dump_zap_stats(os, object);
342fa9e4066Sahrens 	(void) printf("\n");
343fa9e4066Sahrens 
344fa9e4066Sahrens 	for (zap_cursor_init(&zc, os, object);
345fa9e4066Sahrens 	    zap_cursor_retrieve(&zc, &attr) == 0;
346fa9e4066Sahrens 	    zap_cursor_advance(&zc)) {
347fa9e4066Sahrens 		(void) printf("\t\t%s = ", attr.za_name);
348fa9e4066Sahrens 		if (attr.za_num_integers == 0) {
349fa9e4066Sahrens 			(void) printf("\n");
350fa9e4066Sahrens 			continue;
351fa9e4066Sahrens 		}
352fa9e4066Sahrens 		prop = umem_zalloc(attr.za_num_integers *
353fa9e4066Sahrens 		    attr.za_integer_length, UMEM_NOFAIL);
354fa9e4066Sahrens 		(void) zap_lookup(os, object, attr.za_name,
355fa9e4066Sahrens 		    attr.za_integer_length, attr.za_num_integers, prop);
356fa9e4066Sahrens 		if (attr.za_integer_length == 1) {
357fa9e4066Sahrens 			(void) printf("%s", (char *)prop);
358fa9e4066Sahrens 		} else {
359fa9e4066Sahrens 			for (i = 0; i < attr.za_num_integers; i++) {
360fa9e4066Sahrens 				switch (attr.za_integer_length) {
361fa9e4066Sahrens 				case 2:
362fa9e4066Sahrens 					(void) printf("%u ",
363fa9e4066Sahrens 					    ((uint16_t *)prop)[i]);
364fa9e4066Sahrens 					break;
365fa9e4066Sahrens 				case 4:
366fa9e4066Sahrens 					(void) printf("%u ",
367fa9e4066Sahrens 					    ((uint32_t *)prop)[i]);
368fa9e4066Sahrens 					break;
369fa9e4066Sahrens 				case 8:
370fa9e4066Sahrens 					(void) printf("%lld ",
371fa9e4066Sahrens 					    (u_longlong_t)((int64_t *)prop)[i]);
372fa9e4066Sahrens 					break;
373fa9e4066Sahrens 				}
374fa9e4066Sahrens 			}
375fa9e4066Sahrens 		}
376fa9e4066Sahrens 		(void) printf("\n");
377fa9e4066Sahrens 		umem_free(prop, attr.za_num_integers * attr.za_integer_length);
378fa9e4066Sahrens 	}
37987e5029aSahrens 	zap_cursor_fini(&zc);
380fa9e4066Sahrens }
381fa9e4066Sahrens 
382e7437265Sahrens /*ARGSUSED*/
383e7437265Sahrens static void
384486ae710SMatthew Ahrens dump_ddt_zap(objset_t *os, uint64_t object, void *data, size_t size)
385486ae710SMatthew Ahrens {
386486ae710SMatthew Ahrens 	dump_zap_stats(os, object);
387486ae710SMatthew Ahrens 	/* contents are printed elsewhere, properly decoded */
388486ae710SMatthew Ahrens }
389486ae710SMatthew Ahrens 
390486ae710SMatthew Ahrens /*ARGSUSED*/
391486ae710SMatthew Ahrens static void
3920a586ceaSMark Shellenbaum dump_sa_attrs(objset_t *os, uint64_t object, void *data, size_t size)
3930a586ceaSMark Shellenbaum {
3940a586ceaSMark Shellenbaum 	zap_cursor_t zc;
3950a586ceaSMark Shellenbaum 	zap_attribute_t attr;
3960a586ceaSMark Shellenbaum 
3970a586ceaSMark Shellenbaum 	dump_zap_stats(os, object);
3980a586ceaSMark Shellenbaum 	(void) printf("\n");
3990a586ceaSMark Shellenbaum 
4000a586ceaSMark Shellenbaum 	for (zap_cursor_init(&zc, os, object);
4010a586ceaSMark Shellenbaum 	    zap_cursor_retrieve(&zc, &attr) == 0;
4020a586ceaSMark Shellenbaum 	    zap_cursor_advance(&zc)) {
4030a586ceaSMark Shellenbaum 		(void) printf("\t\t%s = ", attr.za_name);
4040a586ceaSMark Shellenbaum 		if (attr.za_num_integers == 0) {
4050a586ceaSMark Shellenbaum 			(void) printf("\n");
4060a586ceaSMark Shellenbaum 			continue;
4070a586ceaSMark Shellenbaum 		}
4080a586ceaSMark Shellenbaum 		(void) printf(" %llx : [%d:%d:%d]\n",
4090a586ceaSMark Shellenbaum 		    (u_longlong_t)attr.za_first_integer,
4100a586ceaSMark Shellenbaum 		    (int)ATTR_LENGTH(attr.za_first_integer),
4110a586ceaSMark Shellenbaum 		    (int)ATTR_BSWAP(attr.za_first_integer),
4120a586ceaSMark Shellenbaum 		    (int)ATTR_NUM(attr.za_first_integer));
4130a586ceaSMark Shellenbaum 	}
4140a586ceaSMark Shellenbaum 	zap_cursor_fini(&zc);
4150a586ceaSMark Shellenbaum }
4160a586ceaSMark Shellenbaum 
4170a586ceaSMark Shellenbaum /*ARGSUSED*/
4180a586ceaSMark Shellenbaum static void
4190a586ceaSMark Shellenbaum dump_sa_layouts(objset_t *os, uint64_t object, void *data, size_t size)
4200a586ceaSMark Shellenbaum {
4210a586ceaSMark Shellenbaum 	zap_cursor_t zc;
4220a586ceaSMark Shellenbaum 	zap_attribute_t attr;
4230a586ceaSMark Shellenbaum 	uint16_t *layout_attrs;
4240a586ceaSMark Shellenbaum 	int i;
4250a586ceaSMark Shellenbaum 
4260a586ceaSMark Shellenbaum 	dump_zap_stats(os, object);
4270a586ceaSMark Shellenbaum 	(void) printf("\n");
4280a586ceaSMark Shellenbaum 
4290a586ceaSMark Shellenbaum 	for (zap_cursor_init(&zc, os, object);
4300a586ceaSMark Shellenbaum 	    zap_cursor_retrieve(&zc, &attr) == 0;
4310a586ceaSMark Shellenbaum 	    zap_cursor_advance(&zc)) {
4320a586ceaSMark Shellenbaum 		(void) printf("\t\t%s = [", attr.za_name);
4330a586ceaSMark Shellenbaum 		if (attr.za_num_integers == 0) {
4340a586ceaSMark Shellenbaum 			(void) printf("\n");
4350a586ceaSMark Shellenbaum 			continue;
4360a586ceaSMark Shellenbaum 		}
4370a586ceaSMark Shellenbaum 
4380a586ceaSMark Shellenbaum 		VERIFY(attr.za_integer_length == 2);
4390a586ceaSMark Shellenbaum 		layout_attrs = umem_zalloc(attr.za_num_integers *
4400a586ceaSMark Shellenbaum 		    attr.za_integer_length, UMEM_NOFAIL);
4410a586ceaSMark Shellenbaum 
4420a586ceaSMark Shellenbaum 		VERIFY(zap_lookup(os, object, attr.za_name,
4430a586ceaSMark Shellenbaum 		    attr.za_integer_length,
4440a586ceaSMark Shellenbaum 		    attr.za_num_integers, layout_attrs) == 0);
4450a586ceaSMark Shellenbaum 
4460a586ceaSMark Shellenbaum 		for (i = 0; i != attr.za_num_integers; i++)
4470a586ceaSMark Shellenbaum 			(void) printf(" %d ", (int)layout_attrs[i]);
4480a586ceaSMark Shellenbaum 		(void) printf("]\n");
4490a586ceaSMark Shellenbaum 		umem_free(layout_attrs,
4500a586ceaSMark Shellenbaum 		    attr.za_num_integers * attr.za_integer_length);
4510a586ceaSMark Shellenbaum 	}
4520a586ceaSMark Shellenbaum 	zap_cursor_fini(&zc);
4530a586ceaSMark Shellenbaum }
4540a586ceaSMark Shellenbaum 
4550a586ceaSMark Shellenbaum /*ARGSUSED*/
4560a586ceaSMark Shellenbaum static void
457e7437265Sahrens dump_zpldir(objset_t *os, uint64_t object, void *data, size_t size)
458e7437265Sahrens {
459e7437265Sahrens 	zap_cursor_t zc;
460e7437265Sahrens 	zap_attribute_t attr;
461e7437265Sahrens 	const char *typenames[] = {
462e7437265Sahrens 		/* 0 */ "not specified",
463e7437265Sahrens 		/* 1 */ "FIFO",
464e7437265Sahrens 		/* 2 */ "Character Device",
465e7437265Sahrens 		/* 3 */ "3 (invalid)",
466e7437265Sahrens 		/* 4 */ "Directory",
467e7437265Sahrens 		/* 5 */ "5 (invalid)",
468e7437265Sahrens 		/* 6 */ "Block Device",
469e7437265Sahrens 		/* 7 */ "7 (invalid)",
470e7437265Sahrens 		/* 8 */ "Regular File",
471e7437265Sahrens 		/* 9 */ "9 (invalid)",
472e7437265Sahrens 		/* 10 */ "Symbolic Link",
473e7437265Sahrens 		/* 11 */ "11 (invalid)",
474e7437265Sahrens 		/* 12 */ "Socket",
475e7437265Sahrens 		/* 13 */ "Door",
476e7437265Sahrens 		/* 14 */ "Event Port",
477e7437265Sahrens 		/* 15 */ "15 (invalid)",
478e7437265Sahrens 	};
479e7437265Sahrens 
480e7437265Sahrens 	dump_zap_stats(os, object);
481e7437265Sahrens 	(void) printf("\n");
482e7437265Sahrens 
483e7437265Sahrens 	for (zap_cursor_init(&zc, os, object);
484e7437265Sahrens 	    zap_cursor_retrieve(&zc, &attr) == 0;
485e7437265Sahrens 	    zap_cursor_advance(&zc)) {
486e7437265Sahrens 		(void) printf("\t\t%s = %lld (type: %s)\n",
487e7437265Sahrens 		    attr.za_name, ZFS_DIRENT_OBJ(attr.za_first_integer),
488e7437265Sahrens 		    typenames[ZFS_DIRENT_TYPE(attr.za_first_integer)]);
489e7437265Sahrens 	}
490e7437265Sahrens 	zap_cursor_fini(&zc);
491e7437265Sahrens }
492e7437265Sahrens 
493fa9e4066Sahrens static void
494fa9e4066Sahrens dump_spacemap(objset_t *os, space_map_obj_t *smo, space_map_t *sm)
495fa9e4066Sahrens {
496fa9e4066Sahrens 	uint64_t alloc, offset, entry;
497ecc2d604Sbonwick 	uint8_t mapshift = sm->sm_shift;
498fa9e4066Sahrens 	uint64_t mapstart = sm->sm_start;
4998053a263Sck153898 	char *ddata[] = { "ALLOC", "FREE", "CONDENSE", "INVALID",
5008053a263Sck153898 			    "INVALID", "INVALID", "INVALID", "INVALID" };
501fa9e4066Sahrens 
502fa9e4066Sahrens 	if (smo->smo_object == 0)
503fa9e4066Sahrens 		return;
504fa9e4066Sahrens 
505fa9e4066Sahrens 	/*
506fa9e4066Sahrens 	 * Print out the freelist entries in both encoded and decoded form.
507fa9e4066Sahrens 	 */
508fa9e4066Sahrens 	alloc = 0;
509fa9e4066Sahrens 	for (offset = 0; offset < smo->smo_objsize; offset += sizeof (entry)) {
5103f9d6ad7SLin Ling 		VERIFY3U(0, ==, dmu_read(os, smo->smo_object, offset,
5117bfdf011SNeil Perrin 		    sizeof (entry), &entry, DMU_READ_PREFETCH));
512fa9e4066Sahrens 		if (SM_DEBUG_DECODE(entry)) {
51387219db7SVictor Latushkin 			(void) printf("\t    [%6llu] %s: txg %llu, pass %llu\n",
514fa9e4066Sahrens 			    (u_longlong_t)(offset / sizeof (entry)),
515fa9e4066Sahrens 			    ddata[SM_DEBUG_ACTION_DECODE(entry)],
5165ad82045Snd150628 			    (u_longlong_t)SM_DEBUG_TXG_DECODE(entry),
5175ad82045Snd150628 			    (u_longlong_t)SM_DEBUG_SYNCPASS_DECODE(entry));
518fa9e4066Sahrens 		} else {
51987219db7SVictor Latushkin 			(void) printf("\t    [%6llu]    %c  range:"
52087219db7SVictor Latushkin 			    " %010llx-%010llx  size: %06llx\n",
521fa9e4066Sahrens 			    (u_longlong_t)(offset / sizeof (entry)),
522fa9e4066Sahrens 			    SM_TYPE_DECODE(entry) == SM_ALLOC ? 'A' : 'F',
5235ad82045Snd150628 			    (u_longlong_t)((SM_OFFSET_DECODE(entry) <<
5245ad82045Snd150628 			    mapshift) + mapstart),
5255ad82045Snd150628 			    (u_longlong_t)((SM_OFFSET_DECODE(entry) <<
5265ad82045Snd150628 			    mapshift) + mapstart + (SM_RUN_DECODE(entry) <<
5275ad82045Snd150628 			    mapshift)),
5285ad82045Snd150628 			    (u_longlong_t)(SM_RUN_DECODE(entry) << mapshift));
529fa9e4066Sahrens 			if (SM_TYPE_DECODE(entry) == SM_ALLOC)
530fa9e4066Sahrens 				alloc += SM_RUN_DECODE(entry) << mapshift;
531fa9e4066Sahrens 			else
532fa9e4066Sahrens 				alloc -= SM_RUN_DECODE(entry) << mapshift;
533fa9e4066Sahrens 		}
534fa9e4066Sahrens 	}
535fa9e4066Sahrens 	if (alloc != smo->smo_alloc) {
536fa9e4066Sahrens 		(void) printf("space_map_object alloc (%llu) INCONSISTENT "
537fa9e4066Sahrens 		    "with space map summary (%llu)\n",
538fa9e4066Sahrens 		    (u_longlong_t)smo->smo_alloc, (u_longlong_t)alloc);
539fa9e4066Sahrens 	}
540fa9e4066Sahrens }
541fa9e4066Sahrens 
542fa9e4066Sahrens static void
543d6e555bdSGeorge Wilson dump_metaslab_stats(metaslab_t *msp)
544d6e555bdSGeorge Wilson {
5453f9d6ad7SLin Ling 	char maxbuf[32];
546d6e555bdSGeorge Wilson 	space_map_t *sm = &msp->ms_map;
547d6e555bdSGeorge Wilson 	avl_tree_t *t = sm->sm_pp_root;
548d6e555bdSGeorge Wilson 	int free_pct = sm->sm_space * 100 / sm->sm_size;
549d6e555bdSGeorge Wilson 
5503f9d6ad7SLin Ling 	zdb_nicenum(space_map_maxsize(sm), maxbuf);
551d6e555bdSGeorge Wilson 
55287219db7SVictor Latushkin 	(void) printf("\t %25s %10lu   %7s  %6s   %4s %4d%%\n",
553d6e555bdSGeorge Wilson 	    "segments", avl_numnodes(t), "maxsize", maxbuf,
554d6e555bdSGeorge Wilson 	    "freepct", free_pct);
555d6e555bdSGeorge Wilson }
556d6e555bdSGeorge Wilson 
557d6e555bdSGeorge Wilson static void
558fa9e4066Sahrens dump_metaslab(metaslab_t *msp)
559fa9e4066Sahrens {
560fa9e4066Sahrens 	vdev_t *vd = msp->ms_group->mg_vd;
561fa9e4066Sahrens 	spa_t *spa = vd->vdev_spa;
56280eb36f2SGeorge Wilson 	space_map_t *sm = &msp->ms_map;
56380eb36f2SGeorge Wilson 	space_map_obj_t *smo = &msp->ms_smo;
5643f9d6ad7SLin Ling 	char freebuf[32];
565fa9e4066Sahrens 
5663f9d6ad7SLin Ling 	zdb_nicenum(sm->sm_size - smo->smo_alloc, freebuf);
567fa9e4066Sahrens 
568fa9e4066Sahrens 	(void) printf(
56987219db7SVictor Latushkin 	    "\tmetaslab %6llu   offset %12llx   spacemap %6llu   free    %5s\n",
57080eb36f2SGeorge Wilson 	    (u_longlong_t)(sm->sm_start / sm->sm_size),
57180eb36f2SGeorge Wilson 	    (u_longlong_t)sm->sm_start, (u_longlong_t)smo->smo_object, freebuf);
572fa9e4066Sahrens 
57387219db7SVictor Latushkin 	if (dump_opt['m'] > 1 && !dump_opt['L']) {
574d6e555bdSGeorge Wilson 		mutex_enter(&msp->ms_lock);
57580eb36f2SGeorge Wilson 		space_map_load_wait(sm);
576bc3975b5SVictor Latushkin 		if (!sm->sm_loaded)
57780eb36f2SGeorge Wilson 			VERIFY(space_map_load(sm, zfs_metaslab_ops,
57880eb36f2SGeorge Wilson 			    SM_FREE, smo, spa->spa_meta_objset) == 0);
579d6e555bdSGeorge Wilson 		dump_metaslab_stats(msp);
58080eb36f2SGeorge Wilson 		space_map_unload(sm);
581d6e555bdSGeorge Wilson 		mutex_exit(&msp->ms_lock);
582d6e555bdSGeorge Wilson 	}
583d6e555bdSGeorge Wilson 
584d6e555bdSGeorge Wilson 	if (dump_opt['d'] > 5 || dump_opt['m'] > 2) {
58580eb36f2SGeorge Wilson 		ASSERT(sm->sm_size == (1ULL << vd->vdev_ms_shift));
586fa9e4066Sahrens 
587d6e555bdSGeorge Wilson 		mutex_enter(&msp->ms_lock);
58880eb36f2SGeorge Wilson 		dump_spacemap(spa->spa_meta_objset, smo, sm);
589d6e555bdSGeorge Wilson 		mutex_exit(&msp->ms_lock);
590d6e555bdSGeorge Wilson 	}
59187219db7SVictor Latushkin }
592d6e555bdSGeorge Wilson 
59387219db7SVictor Latushkin static void
59487219db7SVictor Latushkin print_vdev_metaslab_header(vdev_t *vd)
59587219db7SVictor Latushkin {
59687219db7SVictor Latushkin 	(void) printf("\tvdev %10llu\n\t%-10s%5llu   %-19s   %-15s   %-10s\n",
59787219db7SVictor Latushkin 	    (u_longlong_t)vd->vdev_id,
59887219db7SVictor Latushkin 	    "metaslabs", (u_longlong_t)vd->vdev_ms_count,
59987219db7SVictor Latushkin 	    "offset", "spacemap", "free");
60087219db7SVictor Latushkin 	(void) printf("\t%15s   %19s   %15s   %10s\n",
60187219db7SVictor Latushkin 	    "---------------", "-------------------",
60287219db7SVictor Latushkin 	    "---------------", "-------------");
603fa9e4066Sahrens }
604fa9e4066Sahrens 
605fa9e4066Sahrens static void
606fa9e4066Sahrens dump_metaslabs(spa_t *spa)
607fa9e4066Sahrens {
60887219db7SVictor Latushkin 	vdev_t *vd, *rvd = spa->spa_root_vdev;
60987219db7SVictor Latushkin 	uint64_t m, c = 0, children = rvd->vdev_children;
610fa9e4066Sahrens 
611fa9e4066Sahrens 	(void) printf("\nMetaslabs:\n");
612fa9e4066Sahrens 
61387219db7SVictor Latushkin 	if (!dump_opt['d'] && zopt_objects > 0) {
61487219db7SVictor Latushkin 		c = zopt_object[0];
615fa9e4066Sahrens 
61687219db7SVictor Latushkin 		if (c >= children)
61787219db7SVictor Latushkin 			(void) fatal("bad vdev id: %llu", (u_longlong_t)c);
61887219db7SVictor Latushkin 
61987219db7SVictor Latushkin 		if (zopt_objects > 1) {
62087219db7SVictor Latushkin 			vd = rvd->vdev_child[c];
62187219db7SVictor Latushkin 			print_vdev_metaslab_header(vd);
62287219db7SVictor Latushkin 
62387219db7SVictor Latushkin 			for (m = 1; m < zopt_objects; m++) {
62487219db7SVictor Latushkin 				if (zopt_object[m] < vd->vdev_ms_count)
62587219db7SVictor Latushkin 					dump_metaslab(
62687219db7SVictor Latushkin 					    vd->vdev_ms[zopt_object[m]]);
62787219db7SVictor Latushkin 				else
62887219db7SVictor Latushkin 					(void) fprintf(stderr, "bad metaslab "
62987219db7SVictor Latushkin 					    "number %llu\n",
63087219db7SVictor Latushkin 					    (u_longlong_t)zopt_object[m]);
63187219db7SVictor Latushkin 			}
63287219db7SVictor Latushkin 			(void) printf("\n");
63387219db7SVictor Latushkin 			return;
63487219db7SVictor Latushkin 		}
63587219db7SVictor Latushkin 		children = c + 1;
63687219db7SVictor Latushkin 	}
63787219db7SVictor Latushkin 	for (; c < children; c++) {
63887219db7SVictor Latushkin 		vd = rvd->vdev_child[c];
63987219db7SVictor Latushkin 		print_vdev_metaslab_header(vd);
640fa9e4066Sahrens 
641fa9e4066Sahrens 		for (m = 0; m < vd->vdev_ms_count; m++)
642fa9e4066Sahrens 			dump_metaslab(vd->vdev_ms[m]);
643fa9e4066Sahrens 		(void) printf("\n");
644fa9e4066Sahrens 	}
645fa9e4066Sahrens }
646fa9e4066Sahrens 
647fa9e4066Sahrens static void
648b24ab676SJeff Bonwick dump_dde(const ddt_t *ddt, const ddt_entry_t *dde, uint64_t index)
649b24ab676SJeff Bonwick {
650b24ab676SJeff Bonwick 	const ddt_phys_t *ddp = dde->dde_phys;
651b24ab676SJeff Bonwick 	const ddt_key_t *ddk = &dde->dde_key;
652b24ab676SJeff Bonwick 	char *types[4] = { "ditto", "single", "double", "triple" };
653b24ab676SJeff Bonwick 	char blkbuf[BP_SPRINTF_LEN];
654b24ab676SJeff Bonwick 	blkptr_t blk;
655b24ab676SJeff Bonwick 
656b24ab676SJeff Bonwick 	for (int p = 0; p < DDT_PHYS_TYPES; p++, ddp++) {
657b24ab676SJeff Bonwick 		if (ddp->ddp_phys_birth == 0)
658b24ab676SJeff Bonwick 			continue;
659bbfd46c4SJeff Bonwick 		ddt_bp_create(ddt->ddt_checksum, ddk, ddp, &blk);
660b24ab676SJeff Bonwick 		sprintf_blkptr(blkbuf, &blk);
661b24ab676SJeff Bonwick 		(void) printf("index %llx refcnt %llu %s %s\n",
662b24ab676SJeff Bonwick 		    (u_longlong_t)index, (u_longlong_t)ddp->ddp_refcnt,
663b24ab676SJeff Bonwick 		    types[p], blkbuf);
664b24ab676SJeff Bonwick 	}
665b24ab676SJeff Bonwick }
666b24ab676SJeff Bonwick 
667b24ab676SJeff Bonwick static void
668b24ab676SJeff Bonwick dump_dedup_ratio(const ddt_stat_t *dds)
669b24ab676SJeff Bonwick {
670b24ab676SJeff Bonwick 	double rL, rP, rD, D, dedup, compress, copies;
671b24ab676SJeff Bonwick 
672b24ab676SJeff Bonwick 	if (dds->dds_blocks == 0)
673b24ab676SJeff Bonwick 		return;
674b24ab676SJeff Bonwick 
675b24ab676SJeff Bonwick 	rL = (double)dds->dds_ref_lsize;
676b24ab676SJeff Bonwick 	rP = (double)dds->dds_ref_psize;
677b24ab676SJeff Bonwick 	rD = (double)dds->dds_ref_dsize;
678b24ab676SJeff Bonwick 	D = (double)dds->dds_dsize;
679b24ab676SJeff Bonwick 
680b24ab676SJeff Bonwick 	dedup = rD / D;
681b24ab676SJeff Bonwick 	compress = rL / rP;
682b24ab676SJeff Bonwick 	copies = rD / rP;
683b24ab676SJeff Bonwick 
684b24ab676SJeff Bonwick 	(void) printf("dedup = %.2f, compress = %.2f, copies = %.2f, "
685b24ab676SJeff Bonwick 	    "dedup * compress / copies = %.2f\n\n",
686b24ab676SJeff Bonwick 	    dedup, compress, copies, dedup * compress / copies);
687b24ab676SJeff Bonwick }
688b24ab676SJeff Bonwick 
689b24ab676SJeff Bonwick static void
690b24ab676SJeff Bonwick dump_ddt(ddt_t *ddt, enum ddt_type type, enum ddt_class class)
691b24ab676SJeff Bonwick {
692b24ab676SJeff Bonwick 	char name[DDT_NAMELEN];
693b24ab676SJeff Bonwick 	ddt_entry_t dde;
694b24ab676SJeff Bonwick 	uint64_t walk = 0;
695b24ab676SJeff Bonwick 	dmu_object_info_t doi;
696b24ab676SJeff Bonwick 	uint64_t count, dspace, mspace;
697b24ab676SJeff Bonwick 	int error;
698b24ab676SJeff Bonwick 
699b24ab676SJeff Bonwick 	error = ddt_object_info(ddt, type, class, &doi);
700b24ab676SJeff Bonwick 
701b24ab676SJeff Bonwick 	if (error == ENOENT)
702b24ab676SJeff Bonwick 		return;
703b24ab676SJeff Bonwick 	ASSERT(error == 0);
704b24ab676SJeff Bonwick 
7057448a079SGeorge Wilson 	if ((count = ddt_object_count(ddt, type, class)) == 0)
7067448a079SGeorge Wilson 		return;
7077448a079SGeorge Wilson 
708b24ab676SJeff Bonwick 	dspace = doi.doi_physical_blocks_512 << 9;
709b24ab676SJeff Bonwick 	mspace = doi.doi_fill_count * doi.doi_data_block_size;
710b24ab676SJeff Bonwick 
711b24ab676SJeff Bonwick 	ddt_object_name(ddt, type, class, name);
712b24ab676SJeff Bonwick 
713b24ab676SJeff Bonwick 	(void) printf("%s: %llu entries, size %llu on disk, %llu in core\n",
714b24ab676SJeff Bonwick 	    name,
715b24ab676SJeff Bonwick 	    (u_longlong_t)count,
716b24ab676SJeff Bonwick 	    (u_longlong_t)(dspace / count),
717b24ab676SJeff Bonwick 	    (u_longlong_t)(mspace / count));
718b24ab676SJeff Bonwick 
719b24ab676SJeff Bonwick 	if (dump_opt['D'] < 3)
720b24ab676SJeff Bonwick 		return;
721b24ab676SJeff Bonwick 
7229eb19f4dSGeorge Wilson 	zpool_dump_ddt(NULL, &ddt->ddt_histogram[type][class]);
723b24ab676SJeff Bonwick 
724b24ab676SJeff Bonwick 	if (dump_opt['D'] < 4)
725b24ab676SJeff Bonwick 		return;
726b24ab676SJeff Bonwick 
727b24ab676SJeff Bonwick 	if (dump_opt['D'] < 5 && class == DDT_CLASS_UNIQUE)
728b24ab676SJeff Bonwick 		return;
729b24ab676SJeff Bonwick 
730b24ab676SJeff Bonwick 	(void) printf("%s contents:\n\n", name);
731b24ab676SJeff Bonwick 
732bbfd46c4SJeff Bonwick 	while ((error = ddt_object_walk(ddt, type, class, &walk, &dde)) == 0)
733b24ab676SJeff Bonwick 		dump_dde(ddt, &dde, walk);
734b24ab676SJeff Bonwick 
735b24ab676SJeff Bonwick 	ASSERT(error == ENOENT);
736b24ab676SJeff Bonwick 
737b24ab676SJeff Bonwick 	(void) printf("\n");
738b24ab676SJeff Bonwick }
739b24ab676SJeff Bonwick 
740b24ab676SJeff Bonwick static void
741b24ab676SJeff Bonwick dump_all_ddts(spa_t *spa)
742b24ab676SJeff Bonwick {
743b24ab676SJeff Bonwick 	ddt_histogram_t ddh_total = { 0 };
744b24ab676SJeff Bonwick 	ddt_stat_t dds_total = { 0 };
745b24ab676SJeff Bonwick 
746b24ab676SJeff Bonwick 	for (enum zio_checksum c = 0; c < ZIO_CHECKSUM_FUNCTIONS; c++) {
747b24ab676SJeff Bonwick 		ddt_t *ddt = spa->spa_ddt[c];
748b24ab676SJeff Bonwick 		for (enum ddt_type type = 0; type < DDT_TYPES; type++) {
749b24ab676SJeff Bonwick 			for (enum ddt_class class = 0; class < DDT_CLASSES;
750b24ab676SJeff Bonwick 			    class++) {
751b24ab676SJeff Bonwick 				dump_ddt(ddt, type, class);
752b24ab676SJeff Bonwick 			}
753b24ab676SJeff Bonwick 		}
754b24ab676SJeff Bonwick 	}
755b24ab676SJeff Bonwick 
7569eb19f4dSGeorge Wilson 	ddt_get_dedup_stats(spa, &dds_total);
757b24ab676SJeff Bonwick 
758b24ab676SJeff Bonwick 	if (dds_total.dds_blocks == 0) {
759b24ab676SJeff Bonwick 		(void) printf("All DDTs are empty\n");
760b24ab676SJeff Bonwick 		return;
761b24ab676SJeff Bonwick 	}
762b24ab676SJeff Bonwick 
763b24ab676SJeff Bonwick 	(void) printf("\n");
764b24ab676SJeff Bonwick 
765b24ab676SJeff Bonwick 	if (dump_opt['D'] > 1) {
766b24ab676SJeff Bonwick 		(void) printf("DDT histogram (aggregated over all DDTs):\n");
7679eb19f4dSGeorge Wilson 		ddt_get_dedup_histogram(spa, &ddh_total);
7689eb19f4dSGeorge Wilson 		zpool_dump_ddt(&dds_total, &ddh_total);
769b24ab676SJeff Bonwick 	}
770b24ab676SJeff Bonwick 
771b24ab676SJeff Bonwick 	dump_dedup_ratio(&dds_total);
772b24ab676SJeff Bonwick }
773b24ab676SJeff Bonwick 
774b24ab676SJeff Bonwick static void
7758ad4d6ddSJeff Bonwick dump_dtl_seg(space_map_t *sm, uint64_t start, uint64_t size)
7768ad4d6ddSJeff Bonwick {
7778ad4d6ddSJeff Bonwick 	char *prefix = (void *)sm;
7788ad4d6ddSJeff Bonwick 
7798ad4d6ddSJeff Bonwick 	(void) printf("%s [%llu,%llu) length %llu\n",
7808ad4d6ddSJeff Bonwick 	    prefix,
7818ad4d6ddSJeff Bonwick 	    (u_longlong_t)start,
7828ad4d6ddSJeff Bonwick 	    (u_longlong_t)(start + size),
7838ad4d6ddSJeff Bonwick 	    (u_longlong_t)(size));
7848ad4d6ddSJeff Bonwick }
7858ad4d6ddSJeff Bonwick 
7868ad4d6ddSJeff Bonwick static void
787fa9e4066Sahrens dump_dtl(vdev_t *vd, int indent)
788fa9e4066Sahrens {
7898ad4d6ddSJeff Bonwick 	spa_t *spa = vd->vdev_spa;
7908ad4d6ddSJeff Bonwick 	boolean_t required;
7918ad4d6ddSJeff Bonwick 	char *name[DTL_TYPES] = { "missing", "partial", "scrub", "outage" };
7928ad4d6ddSJeff Bonwick 	char prefix[256];
7938ad4d6ddSJeff Bonwick 
7948f18d1faSGeorge Wilson 	spa_vdev_state_enter(spa, SCL_NONE);
7958ad4d6ddSJeff Bonwick 	required = vdev_dtl_required(vd);
7968ad4d6ddSJeff Bonwick 	(void) spa_vdev_state_exit(spa, NULL, 0);
797fa9e4066Sahrens 
798fa9e4066Sahrens 	if (indent == 0)
799fa9e4066Sahrens 		(void) printf("\nDirty time logs:\n\n");
800fa9e4066Sahrens 
8018ad4d6ddSJeff Bonwick 	(void) printf("\t%*s%s [%s]\n", indent, "",
802e14bb325SJeff Bonwick 	    vd->vdev_path ? vd->vdev_path :
8038ad4d6ddSJeff Bonwick 	    vd->vdev_parent ? vd->vdev_ops->vdev_op_type : spa_name(spa),
8048ad4d6ddSJeff Bonwick 	    required ? "DTL-required" : "DTL-expendable");
805fa9e4066Sahrens 
8068ad4d6ddSJeff Bonwick 	for (int t = 0; t < DTL_TYPES; t++) {
8078ad4d6ddSJeff Bonwick 		space_map_t *sm = &vd->vdev_dtl[t];
8088ad4d6ddSJeff Bonwick 		if (sm->sm_space == 0)
8098ad4d6ddSJeff Bonwick 			continue;
8108ad4d6ddSJeff Bonwick 		(void) snprintf(prefix, sizeof (prefix), "\t%*s%s",
8118ad4d6ddSJeff Bonwick 		    indent + 2, "", name[t]);
8128ad4d6ddSJeff Bonwick 		mutex_enter(sm->sm_lock);
8138ad4d6ddSJeff Bonwick 		space_map_walk(sm, dump_dtl_seg, (void *)prefix);
8148ad4d6ddSJeff Bonwick 		mutex_exit(sm->sm_lock);
8158ad4d6ddSJeff Bonwick 		if (dump_opt['d'] > 5 && vd->vdev_children == 0)
8168ad4d6ddSJeff Bonwick 			dump_spacemap(spa->spa_meta_objset,
8178ad4d6ddSJeff Bonwick 			    &vd->vdev_dtl_smo, sm);
818fa9e4066Sahrens 	}
819fa9e4066Sahrens 
8208ad4d6ddSJeff Bonwick 	for (int c = 0; c < vd->vdev_children; c++)
821fa9e4066Sahrens 		dump_dtl(vd->vdev_child[c], indent + 4);
822fa9e4066Sahrens }
823fa9e4066Sahrens 
8248f18d1faSGeorge Wilson static void
8258f18d1faSGeorge Wilson dump_history(spa_t *spa)
8268f18d1faSGeorge Wilson {
8278f18d1faSGeorge Wilson 	nvlist_t **events = NULL;
8288f18d1faSGeorge Wilson 	char buf[SPA_MAXBLOCKSIZE];
829e4161df6SVictor Latushkin 	uint64_t resid, len, off = 0;
8308f18d1faSGeorge Wilson 	uint_t num = 0;
8318f18d1faSGeorge Wilson 	int error;
8328f18d1faSGeorge Wilson 	time_t tsec;
8338f18d1faSGeorge Wilson 	struct tm t;
8348f18d1faSGeorge Wilson 	char tbuf[30];
8358f18d1faSGeorge Wilson 	char internalstr[MAXPATHLEN];
8368f18d1faSGeorge Wilson 
8378f18d1faSGeorge Wilson 	do {
838e4161df6SVictor Latushkin 		len = sizeof (buf);
839e4161df6SVictor Latushkin 
8408f18d1faSGeorge Wilson 		if ((error = spa_history_get(spa, &off, &len, buf)) != 0) {
8418f18d1faSGeorge Wilson 			(void) fprintf(stderr, "Unable to read history: "
8428f18d1faSGeorge Wilson 			    "error %d\n", error);
8438f18d1faSGeorge Wilson 			return;
8448f18d1faSGeorge Wilson 		}
8458f18d1faSGeorge Wilson 
8468f18d1faSGeorge Wilson 		if (zpool_history_unpack(buf, len, &resid, &events, &num) != 0)
8478f18d1faSGeorge Wilson 			break;
8488f18d1faSGeorge Wilson 
8498f18d1faSGeorge Wilson 		off -= resid;
8508f18d1faSGeorge Wilson 	} while (len != 0);
8518f18d1faSGeorge Wilson 
8528f18d1faSGeorge Wilson 	(void) printf("\nHistory:\n");
8538f18d1faSGeorge Wilson 	for (int i = 0; i < num; i++) {
8548f18d1faSGeorge Wilson 		uint64_t time, txg, ievent;
8558f18d1faSGeorge Wilson 		char *cmd, *intstr;
8568f18d1faSGeorge Wilson 
8578f18d1faSGeorge Wilson 		if (nvlist_lookup_uint64(events[i], ZPOOL_HIST_TIME,
8588f18d1faSGeorge Wilson 		    &time) != 0)
8598f18d1faSGeorge Wilson 			continue;
8608f18d1faSGeorge Wilson 		if (nvlist_lookup_string(events[i], ZPOOL_HIST_CMD,
8618f18d1faSGeorge Wilson 		    &cmd) != 0) {
8628f18d1faSGeorge Wilson 			if (nvlist_lookup_uint64(events[i],
8638f18d1faSGeorge Wilson 			    ZPOOL_HIST_INT_EVENT, &ievent) != 0)
8648f18d1faSGeorge Wilson 				continue;
8658f18d1faSGeorge Wilson 			verify(nvlist_lookup_uint64(events[i],
8668f18d1faSGeorge Wilson 			    ZPOOL_HIST_TXG, &txg) == 0);
8678f18d1faSGeorge Wilson 			verify(nvlist_lookup_string(events[i],
8688f18d1faSGeorge Wilson 			    ZPOOL_HIST_INT_STR, &intstr) == 0);
8698f18d1faSGeorge Wilson 			if (ievent >= LOG_END)
8708f18d1faSGeorge Wilson 				continue;
8718f18d1faSGeorge Wilson 
8728f18d1faSGeorge Wilson 			(void) snprintf(internalstr,
8738f18d1faSGeorge Wilson 			    sizeof (internalstr),
8748f18d1faSGeorge Wilson 			    "[internal %s txg:%lld] %s",
8753f9d6ad7SLin Ling 			    zfs_history_event_names[ievent], txg,
8768f18d1faSGeorge Wilson 			    intstr);
8778f18d1faSGeorge Wilson 			cmd = internalstr;
8788f18d1faSGeorge Wilson 		}
8798f18d1faSGeorge Wilson 		tsec = time;
8808f18d1faSGeorge Wilson 		(void) localtime_r(&tsec, &t);
8818f18d1faSGeorge Wilson 		(void) strftime(tbuf, sizeof (tbuf), "%F.%T", &t);
8828f18d1faSGeorge Wilson 		(void) printf("%s %s\n", tbuf, cmd);
8838f18d1faSGeorge Wilson 	}
8848f18d1faSGeorge Wilson }
8858f18d1faSGeorge Wilson 
886fa9e4066Sahrens /*ARGSUSED*/
887fa9e4066Sahrens static void
888fa9e4066Sahrens dump_dnode(objset_t *os, uint64_t object, void *data, size_t size)
889fa9e4066Sahrens {
890fa9e4066Sahrens }
891fa9e4066Sahrens 
892fa9e4066Sahrens static uint64_t
893b24ab676SJeff Bonwick blkid2offset(const dnode_phys_t *dnp, const blkptr_t *bp, const zbookmark_t *zb)
894fa9e4066Sahrens {
895b24ab676SJeff Bonwick 	if (dnp == NULL) {
896b24ab676SJeff Bonwick 		ASSERT(zb->zb_level < 0);
897b24ab676SJeff Bonwick 		if (zb->zb_object == 0)
898b24ab676SJeff Bonwick 			return (zb->zb_blkid);
899b24ab676SJeff Bonwick 		return (zb->zb_blkid * BP_GET_LSIZE(bp));
900b24ab676SJeff Bonwick 	}
901fa9e4066Sahrens 
902b24ab676SJeff Bonwick 	ASSERT(zb->zb_level >= 0);
903b24ab676SJeff Bonwick 
904b24ab676SJeff Bonwick 	return ((zb->zb_blkid <<
905b24ab676SJeff Bonwick 	    (zb->zb_level * (dnp->dn_indblkshift - SPA_BLKPTRSHIFT))) *
906fa9e4066Sahrens 	    dnp->dn_datablkszsec << SPA_MINBLOCKSHIFT);
907fa9e4066Sahrens }
908fa9e4066Sahrens 
90944cd46caSbillm static void
910cde58dbcSMatthew Ahrens sprintf_blkptr_compact(char *blkbuf, const blkptr_t *bp)
91144cd46caSbillm {
912cde58dbcSMatthew Ahrens 	const dva_t *dva = bp->blk_dva;
913b24ab676SJeff Bonwick 	int ndvas = dump_opt['d'] > 5 ? BP_GET_NDVAS(bp) : 1;
914b24ab676SJeff Bonwick 
915b24ab676SJeff Bonwick 	if (dump_opt['b'] >= 5) {
916b24ab676SJeff Bonwick 		sprintf_blkptr(blkbuf, bp);
917b24ab676SJeff Bonwick 		return;
918b24ab676SJeff Bonwick 	}
91944cd46caSbillm 
92044cd46caSbillm 	blkbuf[0] = '\0';
92144cd46caSbillm 
922b24ab676SJeff Bonwick 	for (int i = 0; i < ndvas; i++)
92344cd46caSbillm 		(void) sprintf(blkbuf + strlen(blkbuf), "%llu:%llx:%llx ",
92444cd46caSbillm 		    (u_longlong_t)DVA_GET_VDEV(&dva[i]),
92544cd46caSbillm 		    (u_longlong_t)DVA_GET_OFFSET(&dva[i]),
92644cd46caSbillm 		    (u_longlong_t)DVA_GET_ASIZE(&dva[i]));
92744cd46caSbillm 
928b24ab676SJeff Bonwick 	(void) sprintf(blkbuf + strlen(blkbuf),
929b24ab676SJeff Bonwick 	    "%llxL/%llxP F=%llu B=%llu/%llu",
93044cd46caSbillm 	    (u_longlong_t)BP_GET_LSIZE(bp),
93144cd46caSbillm 	    (u_longlong_t)BP_GET_PSIZE(bp),
93244cd46caSbillm 	    (u_longlong_t)bp->blk_fill,
933b24ab676SJeff Bonwick 	    (u_longlong_t)bp->blk_birth,
934b24ab676SJeff Bonwick 	    (u_longlong_t)BP_PHYSICAL_BIRTH(bp));
93544cd46caSbillm }
93644cd46caSbillm 
93788b7b0f2SMatthew Ahrens static void
93888b7b0f2SMatthew Ahrens print_indirect(blkptr_t *bp, const zbookmark_t *zb,
93988b7b0f2SMatthew Ahrens     const dnode_phys_t *dnp)
940fa9e4066Sahrens {
94188b7b0f2SMatthew Ahrens 	char blkbuf[BP_SPRINTF_LEN];
942fa9e4066Sahrens 	int l;
943fa9e4066Sahrens 
944fa9e4066Sahrens 	ASSERT3U(BP_GET_TYPE(bp), ==, dnp->dn_type);
945fa9e4066Sahrens 	ASSERT3U(BP_GET_LEVEL(bp), ==, zb->zb_level);
946fa9e4066Sahrens 
947b24ab676SJeff Bonwick 	(void) printf("%16llx ", (u_longlong_t)blkid2offset(dnp, bp, zb));
948fa9e4066Sahrens 
949fa9e4066Sahrens 	ASSERT(zb->zb_level >= 0);
950fa9e4066Sahrens 
951fa9e4066Sahrens 	for (l = dnp->dn_nlevels - 1; l >= -1; l--) {
952fa9e4066Sahrens 		if (l == zb->zb_level) {
95388b7b0f2SMatthew Ahrens 			(void) printf("L%llx", (u_longlong_t)zb->zb_level);
954fa9e4066Sahrens 		} else {
95588b7b0f2SMatthew Ahrens 			(void) printf(" ");
956fa9e4066Sahrens 		}
957fa9e4066Sahrens 	}
958fa9e4066Sahrens 
959b24ab676SJeff Bonwick 	sprintf_blkptr_compact(blkbuf, bp);
96044cd46caSbillm 	(void) printf("%s\n", blkbuf);
961fa9e4066Sahrens }
962fa9e4066Sahrens 
96388b7b0f2SMatthew Ahrens static int
96488b7b0f2SMatthew Ahrens visit_indirect(spa_t *spa, const dnode_phys_t *dnp,
96588b7b0f2SMatthew Ahrens     blkptr_t *bp, const zbookmark_t *zb)
96688b7b0f2SMatthew Ahrens {
967e4161df6SVictor Latushkin 	int err = 0;
96888b7b0f2SMatthew Ahrens 
96988b7b0f2SMatthew Ahrens 	if (bp->blk_birth == 0)
97088b7b0f2SMatthew Ahrens 		return (0);
97188b7b0f2SMatthew Ahrens 
97288b7b0f2SMatthew Ahrens 	print_indirect(bp, zb, dnp);
97388b7b0f2SMatthew Ahrens 
97488b7b0f2SMatthew Ahrens 	if (BP_GET_LEVEL(bp) > 0) {
97588b7b0f2SMatthew Ahrens 		uint32_t flags = ARC_WAIT;
97688b7b0f2SMatthew Ahrens 		int i;
97788b7b0f2SMatthew Ahrens 		blkptr_t *cbp;
97888b7b0f2SMatthew Ahrens 		int epb = BP_GET_LSIZE(bp) >> SPA_BLKPTRSHIFT;
97988b7b0f2SMatthew Ahrens 		arc_buf_t *buf;
98088b7b0f2SMatthew Ahrens 		uint64_t fill = 0;
98188b7b0f2SMatthew Ahrens 
98288b7b0f2SMatthew Ahrens 		err = arc_read_nolock(NULL, spa, bp, arc_getbuf_func, &buf,
98388b7b0f2SMatthew Ahrens 		    ZIO_PRIORITY_ASYNC_READ, ZIO_FLAG_CANFAIL, &flags, zb);
98488b7b0f2SMatthew Ahrens 		if (err)
98588b7b0f2SMatthew Ahrens 			return (err);
9863f9d6ad7SLin Ling 		ASSERT(buf->b_data);
98788b7b0f2SMatthew Ahrens 
98888b7b0f2SMatthew Ahrens 		/* recursively visit blocks below this */
98988b7b0f2SMatthew Ahrens 		cbp = buf->b_data;
99088b7b0f2SMatthew Ahrens 		for (i = 0; i < epb; i++, cbp++) {
99188b7b0f2SMatthew Ahrens 			zbookmark_t czb;
99288b7b0f2SMatthew Ahrens 
99388b7b0f2SMatthew Ahrens 			SET_BOOKMARK(&czb, zb->zb_objset, zb->zb_object,
99488b7b0f2SMatthew Ahrens 			    zb->zb_level - 1,
99588b7b0f2SMatthew Ahrens 			    zb->zb_blkid * epb + i);
99688b7b0f2SMatthew Ahrens 			err = visit_indirect(spa, dnp, cbp, &czb);
99788b7b0f2SMatthew Ahrens 			if (err)
99888b7b0f2SMatthew Ahrens 				break;
99988b7b0f2SMatthew Ahrens 			fill += cbp->blk_fill;
100088b7b0f2SMatthew Ahrens 		}
10018ad4d6ddSJeff Bonwick 		if (!err)
100288b7b0f2SMatthew Ahrens 			ASSERT3U(fill, ==, bp->blk_fill);
100388b7b0f2SMatthew Ahrens 		(void) arc_buf_remove_ref(buf, &buf);
100488b7b0f2SMatthew Ahrens 	}
100588b7b0f2SMatthew Ahrens 
100688b7b0f2SMatthew Ahrens 	return (err);
1007fa9e4066Sahrens }
1008fa9e4066Sahrens 
1009fa9e4066Sahrens /*ARGSUSED*/
1010fa9e4066Sahrens static void
101188b7b0f2SMatthew Ahrens dump_indirect(dnode_t *dn)
1012fa9e4066Sahrens {
101388b7b0f2SMatthew Ahrens 	dnode_phys_t *dnp = dn->dn_phys;
101488b7b0f2SMatthew Ahrens 	int j;
101588b7b0f2SMatthew Ahrens 	zbookmark_t czb;
1016fa9e4066Sahrens 
1017fa9e4066Sahrens 	(void) printf("Indirect blocks:\n");
1018fa9e4066Sahrens 
1019503ad85cSMatthew Ahrens 	SET_BOOKMARK(&czb, dmu_objset_id(dn->dn_objset),
102088b7b0f2SMatthew Ahrens 	    dn->dn_object, dnp->dn_nlevels - 1, 0);
102188b7b0f2SMatthew Ahrens 	for (j = 0; j < dnp->dn_nblkptr; j++) {
102288b7b0f2SMatthew Ahrens 		czb.zb_blkid = j;
1023503ad85cSMatthew Ahrens 		(void) visit_indirect(dmu_objset_spa(dn->dn_objset), dnp,
102488b7b0f2SMatthew Ahrens 		    &dnp->dn_blkptr[j], &czb);
102588b7b0f2SMatthew Ahrens 	}
1026fa9e4066Sahrens 
1027fa9e4066Sahrens 	(void) printf("\n");
1028fa9e4066Sahrens }
1029fa9e4066Sahrens 
1030fa9e4066Sahrens /*ARGSUSED*/
1031fa9e4066Sahrens static void
1032fa9e4066Sahrens dump_dsl_dir(objset_t *os, uint64_t object, void *data, size_t size)
1033fa9e4066Sahrens {
1034fa9e4066Sahrens 	dsl_dir_phys_t *dd = data;
1035fa9e4066Sahrens 	time_t crtime;
10363f9d6ad7SLin Ling 	char nice[32];
1037fa9e4066Sahrens 
1038fa9e4066Sahrens 	if (dd == NULL)
1039fa9e4066Sahrens 		return;
1040fa9e4066Sahrens 
1041da6c28aaSamw 	ASSERT3U(size, >=, sizeof (dsl_dir_phys_t));
1042fa9e4066Sahrens 
1043fa9e4066Sahrens 	crtime = dd->dd_creation_time;
1044fa9e4066Sahrens 	(void) printf("\t\tcreation_time = %s", ctime(&crtime));
1045fa9e4066Sahrens 	(void) printf("\t\thead_dataset_obj = %llu\n",
1046fa9e4066Sahrens 	    (u_longlong_t)dd->dd_head_dataset_obj);
1047fa9e4066Sahrens 	(void) printf("\t\tparent_dir_obj = %llu\n",
1048fa9e4066Sahrens 	    (u_longlong_t)dd->dd_parent_obj);
10493cb34c60Sahrens 	(void) printf("\t\torigin_obj = %llu\n",
10503cb34c60Sahrens 	    (u_longlong_t)dd->dd_origin_obj);
1051fa9e4066Sahrens 	(void) printf("\t\tchild_dir_zapobj = %llu\n",
1052fa9e4066Sahrens 	    (u_longlong_t)dd->dd_child_dir_zapobj);
10533f9d6ad7SLin Ling 	zdb_nicenum(dd->dd_used_bytes, nice);
105474e7dc98SMatthew Ahrens 	(void) printf("\t\tused_bytes = %s\n", nice);
10553f9d6ad7SLin Ling 	zdb_nicenum(dd->dd_compressed_bytes, nice);
105674e7dc98SMatthew Ahrens 	(void) printf("\t\tcompressed_bytes = %s\n", nice);
10573f9d6ad7SLin Ling 	zdb_nicenum(dd->dd_uncompressed_bytes, nice);
105874e7dc98SMatthew Ahrens 	(void) printf("\t\tuncompressed_bytes = %s\n", nice);
10593f9d6ad7SLin Ling 	zdb_nicenum(dd->dd_quota, nice);
106074e7dc98SMatthew Ahrens 	(void) printf("\t\tquota = %s\n", nice);
10613f9d6ad7SLin Ling 	zdb_nicenum(dd->dd_reserved, nice);
106274e7dc98SMatthew Ahrens 	(void) printf("\t\treserved = %s\n", nice);
1063fa9e4066Sahrens 	(void) printf("\t\tprops_zapobj = %llu\n",
1064fa9e4066Sahrens 	    (u_longlong_t)dd->dd_props_zapobj);
1065ecd6cf80Smarks 	(void) printf("\t\tdeleg_zapobj = %llu\n",
1066ecd6cf80Smarks 	    (u_longlong_t)dd->dd_deleg_zapobj);
106774e7dc98SMatthew Ahrens 	(void) printf("\t\tflags = %llx\n",
106874e7dc98SMatthew Ahrens 	    (u_longlong_t)dd->dd_flags);
106974e7dc98SMatthew Ahrens 
107074e7dc98SMatthew Ahrens #define	DO(which) \
10713f9d6ad7SLin Ling 	zdb_nicenum(dd->dd_used_breakdown[DD_USED_ ## which], nice); \
107274e7dc98SMatthew Ahrens 	(void) printf("\t\tused_breakdown[" #which "] = %s\n", nice)
107374e7dc98SMatthew Ahrens 	DO(HEAD);
107474e7dc98SMatthew Ahrens 	DO(SNAP);
107574e7dc98SMatthew Ahrens 	DO(CHILD);
107674e7dc98SMatthew Ahrens 	DO(CHILD_RSRV);
107774e7dc98SMatthew Ahrens 	DO(REFRSRV);
107874e7dc98SMatthew Ahrens #undef DO
1079fa9e4066Sahrens }
1080fa9e4066Sahrens 
1081fa9e4066Sahrens /*ARGSUSED*/
1082fa9e4066Sahrens static void
1083fa9e4066Sahrens dump_dsl_dataset(objset_t *os, uint64_t object, void *data, size_t size)
1084fa9e4066Sahrens {
1085fa9e4066Sahrens 	dsl_dataset_phys_t *ds = data;
1086fa9e4066Sahrens 	time_t crtime;
10873f9d6ad7SLin Ling 	char used[32], compressed[32], uncompressed[32], unique[32];
1088fbabab8fSmaybee 	char blkbuf[BP_SPRINTF_LEN];
1089fa9e4066Sahrens 
1090fa9e4066Sahrens 	if (ds == NULL)
1091fa9e4066Sahrens 		return;
1092fa9e4066Sahrens 
1093fa9e4066Sahrens 	ASSERT(size == sizeof (*ds));
1094fa9e4066Sahrens 	crtime = ds->ds_creation_time;
1095*ad135b5dSChristopher Siden 	zdb_nicenum(ds->ds_referenced_bytes, used);
10963f9d6ad7SLin Ling 	zdb_nicenum(ds->ds_compressed_bytes, compressed);
10973f9d6ad7SLin Ling 	zdb_nicenum(ds->ds_uncompressed_bytes, uncompressed);
10983f9d6ad7SLin Ling 	zdb_nicenum(ds->ds_unique_bytes, unique);
1099b24ab676SJeff Bonwick 	sprintf_blkptr(blkbuf, &ds->ds_bp);
1100fa9e4066Sahrens 
1101088f3894Sahrens 	(void) printf("\t\tdir_obj = %llu\n",
1102fa9e4066Sahrens 	    (u_longlong_t)ds->ds_dir_obj);
1103fa9e4066Sahrens 	(void) printf("\t\tprev_snap_obj = %llu\n",
1104fa9e4066Sahrens 	    (u_longlong_t)ds->ds_prev_snap_obj);
1105fa9e4066Sahrens 	(void) printf("\t\tprev_snap_txg = %llu\n",
1106fa9e4066Sahrens 	    (u_longlong_t)ds->ds_prev_snap_txg);
1107fa9e4066Sahrens 	(void) printf("\t\tnext_snap_obj = %llu\n",
1108fa9e4066Sahrens 	    (u_longlong_t)ds->ds_next_snap_obj);
1109fa9e4066Sahrens 	(void) printf("\t\tsnapnames_zapobj = %llu\n",
1110fa9e4066Sahrens 	    (u_longlong_t)ds->ds_snapnames_zapobj);
1111fa9e4066Sahrens 	(void) printf("\t\tnum_children = %llu\n",
1112fa9e4066Sahrens 	    (u_longlong_t)ds->ds_num_children);
1113842727c2SChris Kirby 	(void) printf("\t\tuserrefs_obj = %llu\n",
1114842727c2SChris Kirby 	    (u_longlong_t)ds->ds_userrefs_obj);
1115fa9e4066Sahrens 	(void) printf("\t\tcreation_time = %s", ctime(&crtime));
1116fa9e4066Sahrens 	(void) printf("\t\tcreation_txg = %llu\n",
1117fa9e4066Sahrens 	    (u_longlong_t)ds->ds_creation_txg);
1118fa9e4066Sahrens 	(void) printf("\t\tdeadlist_obj = %llu\n",
1119fa9e4066Sahrens 	    (u_longlong_t)ds->ds_deadlist_obj);
1120fa9e4066Sahrens 	(void) printf("\t\tused_bytes = %s\n", used);
1121fa9e4066Sahrens 	(void) printf("\t\tcompressed_bytes = %s\n", compressed);
1122fa9e4066Sahrens 	(void) printf("\t\tuncompressed_bytes = %s\n", uncompressed);
1123fa9e4066Sahrens 	(void) printf("\t\tunique = %s\n", unique);
1124fa9e4066Sahrens 	(void) printf("\t\tfsid_guid = %llu\n",
1125fa9e4066Sahrens 	    (u_longlong_t)ds->ds_fsid_guid);
1126fa9e4066Sahrens 	(void) printf("\t\tguid = %llu\n",
1127fa9e4066Sahrens 	    (u_longlong_t)ds->ds_guid);
112899653d4eSeschrock 	(void) printf("\t\tflags = %llx\n",
112999653d4eSeschrock 	    (u_longlong_t)ds->ds_flags);
1130088f3894Sahrens 	(void) printf("\t\tnext_clones_obj = %llu\n",
1131088f3894Sahrens 	    (u_longlong_t)ds->ds_next_clones_obj);
1132bb0ade09Sahrens 	(void) printf("\t\tprops_obj = %llu\n",
1133bb0ade09Sahrens 	    (u_longlong_t)ds->ds_props_obj);
1134fa9e4066Sahrens 	(void) printf("\t\tbp = %s\n", blkbuf);
1135fa9e4066Sahrens }
1136fa9e4066Sahrens 
1137cde58dbcSMatthew Ahrens /* ARGSUSED */
1138cde58dbcSMatthew Ahrens static int
1139*ad135b5dSChristopher Siden dump_bptree_cb(void *arg, const blkptr_t *bp, dmu_tx_t *tx)
1140*ad135b5dSChristopher Siden {
1141*ad135b5dSChristopher Siden 	char blkbuf[BP_SPRINTF_LEN];
1142*ad135b5dSChristopher Siden 
1143*ad135b5dSChristopher Siden 	if (bp->blk_birth != 0) {
1144*ad135b5dSChristopher Siden 		sprintf_blkptr(blkbuf, bp);
1145*ad135b5dSChristopher Siden 		(void) printf("\t%s\n", blkbuf);
1146*ad135b5dSChristopher Siden 	}
1147*ad135b5dSChristopher Siden 	return (0);
1148*ad135b5dSChristopher Siden }
1149*ad135b5dSChristopher Siden 
1150*ad135b5dSChristopher Siden static void
1151*ad135b5dSChristopher Siden dump_bptree(objset_t *os, uint64_t obj, char *name)
1152*ad135b5dSChristopher Siden {
1153*ad135b5dSChristopher Siden 	char bytes[32];
1154*ad135b5dSChristopher Siden 	bptree_phys_t *bt;
1155*ad135b5dSChristopher Siden 	dmu_buf_t *db;
1156*ad135b5dSChristopher Siden 
1157*ad135b5dSChristopher Siden 	if (dump_opt['d'] < 3)
1158*ad135b5dSChristopher Siden 		return;
1159*ad135b5dSChristopher Siden 
1160*ad135b5dSChristopher Siden 	VERIFY3U(0, ==, dmu_bonus_hold(os, obj, FTAG, &db));
1161*ad135b5dSChristopher Siden 	bt = db->db_data;
1162*ad135b5dSChristopher Siden 	zdb_nicenum(bt->bt_bytes, bytes);
1163*ad135b5dSChristopher Siden 	(void) printf("\n    %s: %llu datasets, %s\n",
1164*ad135b5dSChristopher Siden 	    name, (unsigned long long)(bt->bt_end - bt->bt_begin), bytes);
1165*ad135b5dSChristopher Siden 	dmu_buf_rele(db, FTAG);
1166*ad135b5dSChristopher Siden 
1167*ad135b5dSChristopher Siden 	if (dump_opt['d'] < 5)
1168*ad135b5dSChristopher Siden 		return;
1169*ad135b5dSChristopher Siden 
1170*ad135b5dSChristopher Siden 	(void) printf("\n");
1171*ad135b5dSChristopher Siden 
1172*ad135b5dSChristopher Siden 	(void) bptree_iterate(os, obj, B_FALSE, dump_bptree_cb, NULL, NULL);
1173*ad135b5dSChristopher Siden }
1174*ad135b5dSChristopher Siden 
1175*ad135b5dSChristopher Siden /* ARGSUSED */
1176*ad135b5dSChristopher Siden static int
1177cde58dbcSMatthew Ahrens dump_bpobj_cb(void *arg, const blkptr_t *bp, dmu_tx_t *tx)
1178fa9e4066Sahrens {
1179cde58dbcSMatthew Ahrens 	char blkbuf[BP_SPRINTF_LEN];
1180cde58dbcSMatthew Ahrens 
1181cde58dbcSMatthew Ahrens 	ASSERT(bp->blk_birth != 0);
1182cde58dbcSMatthew Ahrens 	sprintf_blkptr_compact(blkbuf, bp);
1183cde58dbcSMatthew Ahrens 	(void) printf("\t%s\n", blkbuf);
1184cde58dbcSMatthew Ahrens 	return (0);
1185cde58dbcSMatthew Ahrens }
1186cde58dbcSMatthew Ahrens 
1187cde58dbcSMatthew Ahrens static void
1188cde58dbcSMatthew Ahrens dump_bpobj(bpobj_t *bpo, char *name)
1189cde58dbcSMatthew Ahrens {
11903f9d6ad7SLin Ling 	char bytes[32];
11913f9d6ad7SLin Ling 	char comp[32];
11923f9d6ad7SLin Ling 	char uncomp[32];
1193fa9e4066Sahrens 
1194fa9e4066Sahrens 	if (dump_opt['d'] < 3)
1195fa9e4066Sahrens 		return;
1196fa9e4066Sahrens 
1197cde58dbcSMatthew Ahrens 	zdb_nicenum(bpo->bpo_phys->bpo_bytes, bytes);
1198cde58dbcSMatthew Ahrens 	if (bpo->bpo_havesubobj) {
1199cde58dbcSMatthew Ahrens 		zdb_nicenum(bpo->bpo_phys->bpo_comp, comp);
1200cde58dbcSMatthew Ahrens 		zdb_nicenum(bpo->bpo_phys->bpo_uncomp, uncomp);
1201cde58dbcSMatthew Ahrens 		(void) printf("\n    %s: %llu local blkptrs, %llu subobjs, "
1202cde58dbcSMatthew Ahrens 		    "%s (%s/%s comp)\n",
1203cde58dbcSMatthew Ahrens 		    name, (u_longlong_t)bpo->bpo_phys->bpo_num_blkptrs,
1204cde58dbcSMatthew Ahrens 		    (u_longlong_t)bpo->bpo_phys->bpo_num_subobjs,
120599653d4eSeschrock 		    bytes, comp, uncomp);
120699653d4eSeschrock 	} else {
1207cde58dbcSMatthew Ahrens 		(void) printf("\n    %s: %llu blkptrs, %s\n",
1208cde58dbcSMatthew Ahrens 		    name, (u_longlong_t)bpo->bpo_phys->bpo_num_blkptrs, bytes);
120999653d4eSeschrock 	}
1210fa9e4066Sahrens 
1211cde58dbcSMatthew Ahrens 	if (dump_opt['d'] < 5)
1212fa9e4066Sahrens 		return;
1213fa9e4066Sahrens 
1214fa9e4066Sahrens 	(void) printf("\n");
1215fa9e4066Sahrens 
1216cde58dbcSMatthew Ahrens 	(void) bpobj_iterate_nofree(bpo, dump_bpobj_cb, NULL, NULL);
1217fa9e4066Sahrens }
1218fa9e4066Sahrens 
1219cde58dbcSMatthew Ahrens static void
1220cde58dbcSMatthew Ahrens dump_deadlist(dsl_deadlist_t *dl)
1221cde58dbcSMatthew Ahrens {
1222cde58dbcSMatthew Ahrens 	dsl_deadlist_entry_t *dle;
1223cde58dbcSMatthew Ahrens 	char bytes[32];
1224cde58dbcSMatthew Ahrens 	char comp[32];
1225cde58dbcSMatthew Ahrens 	char uncomp[32];
1226cde58dbcSMatthew Ahrens 
1227cde58dbcSMatthew Ahrens 	if (dump_opt['d'] < 3)
1228cde58dbcSMatthew Ahrens 		return;
1229cde58dbcSMatthew Ahrens 
1230cde58dbcSMatthew Ahrens 	zdb_nicenum(dl->dl_phys->dl_used, bytes);
1231cde58dbcSMatthew Ahrens 	zdb_nicenum(dl->dl_phys->dl_comp, comp);
1232cde58dbcSMatthew Ahrens 	zdb_nicenum(dl->dl_phys->dl_uncomp, uncomp);
1233cde58dbcSMatthew Ahrens 	(void) printf("\n    Deadlist: %s (%s/%s comp)\n",
1234cde58dbcSMatthew Ahrens 	    bytes, comp, uncomp);
1235cde58dbcSMatthew Ahrens 
1236cde58dbcSMatthew Ahrens 	if (dump_opt['d'] < 4)
1237cde58dbcSMatthew Ahrens 		return;
1238cde58dbcSMatthew Ahrens 
1239cde58dbcSMatthew Ahrens 	(void) printf("\n");
1240cde58dbcSMatthew Ahrens 
1241cde58dbcSMatthew Ahrens 	for (dle = avl_first(&dl->dl_tree); dle;
1242cde58dbcSMatthew Ahrens 	    dle = AVL_NEXT(&dl->dl_tree, dle)) {
1243cde58dbcSMatthew Ahrens 		(void) printf("      mintxg %llu -> obj %llu\n",
1244cde58dbcSMatthew Ahrens 		    (longlong_t)dle->dle_mintxg,
1245cde58dbcSMatthew Ahrens 		    (longlong_t)dle->dle_bpobj.bpo_object);
1246cde58dbcSMatthew Ahrens 
1247cde58dbcSMatthew Ahrens 		if (dump_opt['d'] >= 5)
1248cde58dbcSMatthew Ahrens 			dump_bpobj(&dle->dle_bpobj, "");
1249cde58dbcSMatthew Ahrens 	}
1250fa9e4066Sahrens }
1251fa9e4066Sahrens 
1252e0d35c44Smarks static avl_tree_t idx_tree;
1253e0d35c44Smarks static avl_tree_t domain_tree;
1254e0d35c44Smarks static boolean_t fuid_table_loaded;
12550a586ceaSMark Shellenbaum static boolean_t sa_loaded;
12560a586ceaSMark Shellenbaum sa_attr_type_t *sa_attr_table;
1257e0d35c44Smarks 
1258e0d35c44Smarks static void
1259e0d35c44Smarks fuid_table_destroy()
1260e0d35c44Smarks {
1261e0d35c44Smarks 	if (fuid_table_loaded) {
1262e0d35c44Smarks 		zfs_fuid_table_destroy(&idx_tree, &domain_tree);
1263e0d35c44Smarks 		fuid_table_loaded = B_FALSE;
1264e0d35c44Smarks 	}
1265e0d35c44Smarks }
1266e0d35c44Smarks 
1267e0d35c44Smarks /*
1268e0d35c44Smarks  * print uid or gid information.
1269e0d35c44Smarks  * For normal POSIX id just the id is printed in decimal format.
1270e0d35c44Smarks  * For CIFS files with FUID the fuid is printed in hex followed by
1271e0d35c44Smarks  * the doman-rid string.
1272e0d35c44Smarks  */
1273e0d35c44Smarks static void
1274e0d35c44Smarks print_idstr(uint64_t id, const char *id_type)
1275e0d35c44Smarks {
1276e0d35c44Smarks 	if (FUID_INDEX(id)) {
1277e0d35c44Smarks 		char *domain;
1278e0d35c44Smarks 
1279e0d35c44Smarks 		domain = zfs_fuid_idx_domain(&idx_tree, FUID_INDEX(id));
1280e0d35c44Smarks 		(void) printf("\t%s     %llx [%s-%d]\n", id_type,
1281e0d35c44Smarks 		    (u_longlong_t)id, domain, (int)FUID_RID(id));
1282e0d35c44Smarks 	} else {
1283e0d35c44Smarks 		(void) printf("\t%s     %llu\n", id_type, (u_longlong_t)id);
1284e0d35c44Smarks 	}
1285e0d35c44Smarks 
1286e0d35c44Smarks }
1287e0d35c44Smarks 
1288e0d35c44Smarks static void
12890a586ceaSMark Shellenbaum dump_uidgid(objset_t *os, uint64_t uid, uint64_t gid)
1290e0d35c44Smarks {
1291e0d35c44Smarks 	uint32_t uid_idx, gid_idx;
1292e0d35c44Smarks 
12930a586ceaSMark Shellenbaum 	uid_idx = FUID_INDEX(uid);
12940a586ceaSMark Shellenbaum 	gid_idx = FUID_INDEX(gid);
1295e0d35c44Smarks 
1296e0d35c44Smarks 	/* Load domain table, if not already loaded */
1297e0d35c44Smarks 	if (!fuid_table_loaded && (uid_idx || gid_idx)) {
1298e0d35c44Smarks 		uint64_t fuid_obj;
1299e0d35c44Smarks 
1300e0d35c44Smarks 		/* first find the fuid object.  It lives in the master node */
1301e0d35c44Smarks 		VERIFY(zap_lookup(os, MASTER_NODE_OBJ, ZFS_FUID_TABLES,
1302e0d35c44Smarks 		    8, 1, &fuid_obj) == 0);
130389459e17SMark Shellenbaum 		zfs_fuid_avl_tree_create(&idx_tree, &domain_tree);
1304e0d35c44Smarks 		(void) zfs_fuid_table_load(os, fuid_obj,
1305e0d35c44Smarks 		    &idx_tree, &domain_tree);
1306e0d35c44Smarks 		fuid_table_loaded = B_TRUE;
1307e0d35c44Smarks 	}
1308e0d35c44Smarks 
13090a586ceaSMark Shellenbaum 	print_idstr(uid, "uid");
13100a586ceaSMark Shellenbaum 	print_idstr(gid, "gid");
1311e0d35c44Smarks }
1312e0d35c44Smarks 
1313fa9e4066Sahrens /*ARGSUSED*/
1314fa9e4066Sahrens static void
1315fa9e4066Sahrens dump_znode(objset_t *os, uint64_t object, void *data, size_t size)
1316fa9e4066Sahrens {
1317fa9e4066Sahrens 	char path[MAXPATHLEN * 2];	/* allow for xattr and failure prefix */
13180a586ceaSMark Shellenbaum 	sa_handle_t *hdl;
13190a586ceaSMark Shellenbaum 	uint64_t xattr, rdev, gen;
13200a586ceaSMark Shellenbaum 	uint64_t uid, gid, mode, fsize, parent, links;
13218f2529deSMark Shellenbaum 	uint64_t pflags;
13220a586ceaSMark Shellenbaum 	uint64_t acctm[2], modtm[2], chgtm[2], crtm[2];
13230a586ceaSMark Shellenbaum 	time_t z_crtime, z_atime, z_mtime, z_ctime;
13248f2529deSMark Shellenbaum 	sa_bulk_attr_t bulk[12];
13250a586ceaSMark Shellenbaum 	int idx = 0;
132655434c77Sek110237 	int error;
1327fa9e4066Sahrens 
13280a586ceaSMark Shellenbaum 	if (!sa_loaded) {
13290a586ceaSMark Shellenbaum 		uint64_t sa_attrs = 0;
13300a586ceaSMark Shellenbaum 		uint64_t version;
13310a586ceaSMark Shellenbaum 
13320a586ceaSMark Shellenbaum 		VERIFY(zap_lookup(os, MASTER_NODE_OBJ, ZPL_VERSION_STR,
13330a586ceaSMark Shellenbaum 		    8, 1, &version) == 0);
13340a586ceaSMark Shellenbaum 		if (version >= ZPL_VERSION_SA) {
13350a586ceaSMark Shellenbaum 			VERIFY(zap_lookup(os, MASTER_NODE_OBJ, ZFS_SA_ATTRS,
13360a586ceaSMark Shellenbaum 			    8, 1, &sa_attrs) == 0);
13370a586ceaSMark Shellenbaum 		}
13381d8ccc7bSMark Shellenbaum 		if ((error = sa_setup(os, sa_attrs, zfs_attr_table,
13391d8ccc7bSMark Shellenbaum 		    ZPL_END, &sa_attr_table)) != 0) {
13401d8ccc7bSMark Shellenbaum 			(void) printf("sa_setup failed errno %d, can't "
13411d8ccc7bSMark Shellenbaum 			    "display znode contents\n", error);
13421d8ccc7bSMark Shellenbaum 			return;
13431d8ccc7bSMark Shellenbaum 		}
13440a586ceaSMark Shellenbaum 		sa_loaded = B_TRUE;
13450a586ceaSMark Shellenbaum 	}
13460a586ceaSMark Shellenbaum 
13470a586ceaSMark Shellenbaum 	if (sa_handle_get(os, object, NULL, SA_HDL_PRIVATE, &hdl)) {
13480a586ceaSMark Shellenbaum 		(void) printf("Failed to get handle for SA znode\n");
13490a586ceaSMark Shellenbaum 		return;
13500a586ceaSMark Shellenbaum 	}
13510a586ceaSMark Shellenbaum 
13520a586ceaSMark Shellenbaum 	SA_ADD_BULK_ATTR(bulk, idx, sa_attr_table[ZPL_UID], NULL, &uid, 8);
13530a586ceaSMark Shellenbaum 	SA_ADD_BULK_ATTR(bulk, idx, sa_attr_table[ZPL_GID], NULL, &gid, 8);
13540a586ceaSMark Shellenbaum 	SA_ADD_BULK_ATTR(bulk, idx, sa_attr_table[ZPL_LINKS], NULL,
13550a586ceaSMark Shellenbaum 	    &links, 8);
13560a586ceaSMark Shellenbaum 	SA_ADD_BULK_ATTR(bulk, idx, sa_attr_table[ZPL_GEN], NULL, &gen, 8);
13570a586ceaSMark Shellenbaum 	SA_ADD_BULK_ATTR(bulk, idx, sa_attr_table[ZPL_MODE], NULL,
13580a586ceaSMark Shellenbaum 	    &mode, 8);
13590a586ceaSMark Shellenbaum 	SA_ADD_BULK_ATTR(bulk, idx, sa_attr_table[ZPL_PARENT],
13600a586ceaSMark Shellenbaum 	    NULL, &parent, 8);
13610a586ceaSMark Shellenbaum 	SA_ADD_BULK_ATTR(bulk, idx, sa_attr_table[ZPL_SIZE], NULL,
13620a586ceaSMark Shellenbaum 	    &fsize, 8);
13630a586ceaSMark Shellenbaum 	SA_ADD_BULK_ATTR(bulk, idx, sa_attr_table[ZPL_ATIME], NULL,
13640a586ceaSMark Shellenbaum 	    acctm, 16);
13650a586ceaSMark Shellenbaum 	SA_ADD_BULK_ATTR(bulk, idx, sa_attr_table[ZPL_MTIME], NULL,
13660a586ceaSMark Shellenbaum 	    modtm, 16);
13670a586ceaSMark Shellenbaum 	SA_ADD_BULK_ATTR(bulk, idx, sa_attr_table[ZPL_CRTIME], NULL,
13680a586ceaSMark Shellenbaum 	    crtm, 16);
13690a586ceaSMark Shellenbaum 	SA_ADD_BULK_ATTR(bulk, idx, sa_attr_table[ZPL_CTIME], NULL,
13700a586ceaSMark Shellenbaum 	    chgtm, 16);
13718f2529deSMark Shellenbaum 	SA_ADD_BULK_ATTR(bulk, idx, sa_attr_table[ZPL_FLAGS], NULL,
13728f2529deSMark Shellenbaum 	    &pflags, 8);
13730a586ceaSMark Shellenbaum 
13740a586ceaSMark Shellenbaum 	if (sa_bulk_lookup(hdl, bulk, idx)) {
13750a586ceaSMark Shellenbaum 		(void) sa_handle_destroy(hdl);
13760a586ceaSMark Shellenbaum 		return;
13770a586ceaSMark Shellenbaum 	}
1378fa9e4066Sahrens 
137955434c77Sek110237 	error = zfs_obj_to_path(os, object, path, sizeof (path));
138055434c77Sek110237 	if (error != 0) {
138155434c77Sek110237 		(void) snprintf(path, sizeof (path), "\?\?\?<object#%llu>",
138255434c77Sek110237 		    (u_longlong_t)object);
138355434c77Sek110237 	}
1384fa9e4066Sahrens 	if (dump_opt['d'] < 3) {
138555434c77Sek110237 		(void) printf("\t%s\n", path);
13860a586ceaSMark Shellenbaum 		(void) sa_handle_destroy(hdl);
1387fa9e4066Sahrens 		return;
1388fa9e4066Sahrens 	}
1389fa9e4066Sahrens 
13900a586ceaSMark Shellenbaum 	z_crtime = (time_t)crtm[0];
13910a586ceaSMark Shellenbaum 	z_atime = (time_t)acctm[0];
13920a586ceaSMark Shellenbaum 	z_mtime = (time_t)modtm[0];
13930a586ceaSMark Shellenbaum 	z_ctime = (time_t)chgtm[0];
1394fa9e4066Sahrens 
139555434c77Sek110237 	(void) printf("\tpath	%s\n", path);
13960a586ceaSMark Shellenbaum 	dump_uidgid(os, uid, gid);
1397fa9e4066Sahrens 	(void) printf("\tatime	%s", ctime(&z_atime));
1398fa9e4066Sahrens 	(void) printf("\tmtime	%s", ctime(&z_mtime));
1399fa9e4066Sahrens 	(void) printf("\tctime	%s", ctime(&z_ctime));
1400fa9e4066Sahrens 	(void) printf("\tcrtime	%s", ctime(&z_crtime));
14010a586ceaSMark Shellenbaum 	(void) printf("\tgen	%llu\n", (u_longlong_t)gen);
14020a586ceaSMark Shellenbaum 	(void) printf("\tmode	%llo\n", (u_longlong_t)mode);
14030a586ceaSMark Shellenbaum 	(void) printf("\tsize	%llu\n", (u_longlong_t)fsize);
14040a586ceaSMark Shellenbaum 	(void) printf("\tparent	%llu\n", (u_longlong_t)parent);
14050a586ceaSMark Shellenbaum 	(void) printf("\tlinks	%llu\n", (u_longlong_t)links);
14068f2529deSMark Shellenbaum 	(void) printf("\tpflags	%llx\n", (u_longlong_t)pflags);
14070a586ceaSMark Shellenbaum 	if (sa_lookup(hdl, sa_attr_table[ZPL_XATTR], &xattr,
14080a586ceaSMark Shellenbaum 	    sizeof (uint64_t)) == 0)
14090a586ceaSMark Shellenbaum 		(void) printf("\txattr	%llu\n", (u_longlong_t)xattr);
14100a586ceaSMark Shellenbaum 	if (sa_lookup(hdl, sa_attr_table[ZPL_RDEV], &rdev,
14110a586ceaSMark Shellenbaum 	    sizeof (uint64_t)) == 0)
14120a586ceaSMark Shellenbaum 		(void) printf("\trdev	0x%016llx\n", (u_longlong_t)rdev);
14130a586ceaSMark Shellenbaum 	sa_handle_destroy(hdl);
1414fa9e4066Sahrens }
1415fa9e4066Sahrens 
1416fa9e4066Sahrens /*ARGSUSED*/
1417fa9e4066Sahrens static void
1418fa9e4066Sahrens dump_acl(objset_t *os, uint64_t object, void *data, size_t size)
1419fa9e4066Sahrens {
1420fa9e4066Sahrens }
1421fa9e4066Sahrens 
1422fa9e4066Sahrens /*ARGSUSED*/
1423fa9e4066Sahrens static void
1424fa9e4066Sahrens dump_dmu_objset(objset_t *os, uint64_t object, void *data, size_t size)
1425fa9e4066Sahrens {
1426fa9e4066Sahrens }
1427fa9e4066Sahrens 
14286de8f417SVictor Latushkin static object_viewer_t *object_viewer[DMU_OT_NUMTYPES + 1] = {
1429fa9e4066Sahrens 	dump_none,		/* unallocated			*/
1430fa9e4066Sahrens 	dump_zap,		/* object directory		*/
1431fa9e4066Sahrens 	dump_uint64,		/* object array			*/
1432fa9e4066Sahrens 	dump_none,		/* packed nvlist		*/
1433fa9e4066Sahrens 	dump_packed_nvlist,	/* packed nvlist size		*/
1434fa9e4066Sahrens 	dump_none,		/* bplist			*/
1435fa9e4066Sahrens 	dump_none,		/* bplist header		*/
1436fa9e4066Sahrens 	dump_none,		/* SPA space map header		*/
1437fa9e4066Sahrens 	dump_none,		/* SPA space map		*/
1438fa9e4066Sahrens 	dump_none,		/* ZIL intent log		*/
1439fa9e4066Sahrens 	dump_dnode,		/* DMU dnode			*/
1440fa9e4066Sahrens 	dump_dmu_objset,	/* DMU objset			*/
1441fa9e4066Sahrens 	dump_dsl_dir,		/* DSL directory		*/
1442fa9e4066Sahrens 	dump_zap,		/* DSL directory child map	*/
1443fa9e4066Sahrens 	dump_zap,		/* DSL dataset snap map		*/
1444fa9e4066Sahrens 	dump_zap,		/* DSL props			*/
1445fa9e4066Sahrens 	dump_dsl_dataset,	/* DSL dataset			*/
1446fa9e4066Sahrens 	dump_znode,		/* ZFS znode			*/
1447da6c28aaSamw 	dump_acl,		/* ZFS V0 ACL			*/
1448fa9e4066Sahrens 	dump_uint8,		/* ZFS plain file		*/
1449e7437265Sahrens 	dump_zpldir,		/* ZFS directory		*/
1450fa9e4066Sahrens 	dump_zap,		/* ZFS master node		*/
1451fa9e4066Sahrens 	dump_zap,		/* ZFS delete queue		*/
1452fa9e4066Sahrens 	dump_uint8,		/* zvol object			*/
1453fa9e4066Sahrens 	dump_zap,		/* zvol prop			*/
1454fa9e4066Sahrens 	dump_uint8,		/* other uint8[]		*/
1455fa9e4066Sahrens 	dump_uint64,		/* other uint64[]		*/
1456fa9e4066Sahrens 	dump_zap,		/* other ZAP			*/
1457ea8dc4b6Seschrock 	dump_zap,		/* persistent error log		*/
145806eeb2adSek110237 	dump_uint8,		/* SPA history			*/
145906eeb2adSek110237 	dump_uint64,		/* SPA history offsets		*/
1460b1b8ab34Slling 	dump_zap,		/* Pool properties		*/
1461ecd6cf80Smarks 	dump_zap,		/* DSL permissions		*/
1462da6c28aaSamw 	dump_acl,		/* ZFS ACL			*/
1463da6c28aaSamw 	dump_uint8,		/* ZFS SYSACL			*/
1464da6c28aaSamw 	dump_none,		/* FUID nvlist			*/
1465da6c28aaSamw 	dump_packed_nvlist,	/* FUID nvlist size		*/
1466088f3894Sahrens 	dump_zap,		/* DSL dataset next clones	*/
1467088f3894Sahrens 	dump_zap,		/* DSL scrub queue		*/
146814843421SMatthew Ahrens 	dump_zap,		/* ZFS user/group used		*/
146914843421SMatthew Ahrens 	dump_zap,		/* ZFS user/group quota		*/
1470842727c2SChris Kirby 	dump_zap,		/* snapshot refcount tags	*/
1471486ae710SMatthew Ahrens 	dump_ddt_zap,		/* DDT ZAP object		*/
1472b24ab676SJeff Bonwick 	dump_zap,		/* DDT statistics		*/
14730a586ceaSMark Shellenbaum 	dump_znode,		/* SA object			*/
14740a586ceaSMark Shellenbaum 	dump_zap,		/* SA Master Node		*/
14750a586ceaSMark Shellenbaum 	dump_sa_attrs,		/* SA attribute registration	*/
14760a586ceaSMark Shellenbaum 	dump_sa_layouts,	/* SA attribute layouts		*/
14773f9d6ad7SLin Ling 	dump_zap,		/* DSL scrub translations	*/
14783f9d6ad7SLin Ling 	dump_none,		/* fake dedup BP		*/
1479cde58dbcSMatthew Ahrens 	dump_zap,		/* deadlist			*/
1480cde58dbcSMatthew Ahrens 	dump_none,		/* deadlist hdr			*/
1481cde58dbcSMatthew Ahrens 	dump_zap,		/* dsl clones			*/
1482cde58dbcSMatthew Ahrens 	dump_none,		/* bpobj subobjs		*/
14830a586ceaSMark Shellenbaum 	dump_unknown,		/* Unknown type, must be last	*/
1484fa9e4066Sahrens };
1485fa9e4066Sahrens 
1486fa9e4066Sahrens static void
1487fa9e4066Sahrens dump_object(objset_t *os, uint64_t object, int verbosity, int *print_header)
1488fa9e4066Sahrens {
1489fa9e4066Sahrens 	dmu_buf_t *db = NULL;
1490fa9e4066Sahrens 	dmu_object_info_t doi;
1491fa9e4066Sahrens 	dnode_t *dn;
1492fa9e4066Sahrens 	void *bonus = NULL;
1493fa9e4066Sahrens 	size_t bsize = 0;
14943f9d6ad7SLin Ling 	char iblk[32], dblk[32], lsize[32], asize[32], fill[32];
14953f9d6ad7SLin Ling 	char bonus_size[32];
1496fa9e4066Sahrens 	char aux[50];
1497fa9e4066Sahrens 	int error;
1498fa9e4066Sahrens 
1499fa9e4066Sahrens 	if (*print_header) {
1500b24ab676SJeff Bonwick 		(void) printf("\n%10s  %3s  %5s  %5s  %5s  %5s  %6s  %s\n",
1501b24ab676SJeff Bonwick 		    "Object", "lvl", "iblk", "dblk", "dsize", "lsize",
1502b24ab676SJeff Bonwick 		    "%full", "type");
1503fa9e4066Sahrens 		*print_header = 0;
1504fa9e4066Sahrens 	}
1505fa9e4066Sahrens 
1506fa9e4066Sahrens 	if (object == 0) {
1507744947dcSTom Erickson 		dn = DMU_META_DNODE(os);
1508fa9e4066Sahrens 	} else {
1509ea8dc4b6Seschrock 		error = dmu_bonus_hold(os, object, FTAG, &db);
1510ea8dc4b6Seschrock 		if (error)
1511ea8dc4b6Seschrock 			fatal("dmu_bonus_hold(%llu) failed, errno %u",
1512ea8dc4b6Seschrock 			    object, error);
1513fa9e4066Sahrens 		bonus = db->db_data;
1514fa9e4066Sahrens 		bsize = db->db_size;
1515744947dcSTom Erickson 		dn = DB_DNODE((dmu_buf_impl_t *)db);
1516fa9e4066Sahrens 	}
1517fa9e4066Sahrens 	dmu_object_info_from_dnode(dn, &doi);
1518fa9e4066Sahrens 
15193f9d6ad7SLin Ling 	zdb_nicenum(doi.doi_metadata_block_size, iblk);
15203f9d6ad7SLin Ling 	zdb_nicenum(doi.doi_data_block_size, dblk);
15213f9d6ad7SLin Ling 	zdb_nicenum(doi.doi_max_offset, lsize);
15223f9d6ad7SLin Ling 	zdb_nicenum(doi.doi_physical_blocks_512 << 9, asize);
15233f9d6ad7SLin Ling 	zdb_nicenum(doi.doi_bonus_size, bonus_size);
1524b24ab676SJeff Bonwick 	(void) sprintf(fill, "%6.2f", 100.0 * doi.doi_fill_count *
1525bbfd46c4SJeff Bonwick 	    doi.doi_data_block_size / (object == 0 ? DNODES_PER_BLOCK : 1) /
1526bbfd46c4SJeff Bonwick 	    doi.doi_max_offset);
1527fa9e4066Sahrens 
1528fa9e4066Sahrens 	aux[0] = '\0';
1529fa9e4066Sahrens 
1530e7437265Sahrens 	if (doi.doi_checksum != ZIO_CHECKSUM_INHERIT || verbosity >= 6) {
1531fa9e4066Sahrens 		(void) snprintf(aux + strlen(aux), sizeof (aux), " (K=%s)",
15326de8f417SVictor Latushkin 		    ZDB_CHECKSUM_NAME(doi.doi_checksum));
1533e7437265Sahrens 	}
1534fa9e4066Sahrens 
1535e7437265Sahrens 	if (doi.doi_compress != ZIO_COMPRESS_INHERIT || verbosity >= 6) {
1536fa9e4066Sahrens 		(void) snprintf(aux + strlen(aux), sizeof (aux), " (Z=%s)",
15376de8f417SVictor Latushkin 		    ZDB_COMPRESS_NAME(doi.doi_compress));
1538e7437265Sahrens 	}
1539fa9e4066Sahrens 
1540b24ab676SJeff Bonwick 	(void) printf("%10lld  %3u  %5s  %5s  %5s  %5s  %6s  %s%s\n",
1541b24ab676SJeff Bonwick 	    (u_longlong_t)object, doi.doi_indirection, iblk, dblk,
1542b24ab676SJeff Bonwick 	    asize, lsize, fill, ZDB_OT_NAME(doi.doi_type), aux);
1543fa9e4066Sahrens 
1544fa9e4066Sahrens 	if (doi.doi_bonus_type != DMU_OT_NONE && verbosity > 3) {
1545b24ab676SJeff Bonwick 		(void) printf("%10s  %3s  %5s  %5s  %5s  %5s  %6s  %s\n",
1546b24ab676SJeff Bonwick 		    "", "", "", "", "", bonus_size, "bonus",
15476de8f417SVictor Latushkin 		    ZDB_OT_NAME(doi.doi_bonus_type));
1548fa9e4066Sahrens 	}
1549fa9e4066Sahrens 
1550fa9e4066Sahrens 	if (verbosity >= 4) {
15510a586ceaSMark Shellenbaum 		(void) printf("\tdnode flags: %s%s%s\n",
155214843421SMatthew Ahrens 		    (dn->dn_phys->dn_flags & DNODE_FLAG_USED_BYTES) ?
155314843421SMatthew Ahrens 		    "USED_BYTES " : "",
155414843421SMatthew Ahrens 		    (dn->dn_phys->dn_flags & DNODE_FLAG_USERUSED_ACCOUNTED) ?
15550a586ceaSMark Shellenbaum 		    "USERUSED_ACCOUNTED " : "",
15560a586ceaSMark Shellenbaum 		    (dn->dn_phys->dn_flags & DNODE_FLAG_SPILL_BLKPTR) ?
15570a586ceaSMark Shellenbaum 		    "SPILL_BLKPTR" : "");
155814843421SMatthew Ahrens 		(void) printf("\tdnode maxblkid: %llu\n",
155914843421SMatthew Ahrens 		    (longlong_t)dn->dn_phys->dn_maxblkid);
156014843421SMatthew Ahrens 
15616de8f417SVictor Latushkin 		object_viewer[ZDB_OT_TYPE(doi.doi_bonus_type)](os, object,
15626de8f417SVictor Latushkin 		    bonus, bsize);
15636de8f417SVictor Latushkin 		object_viewer[ZDB_OT_TYPE(doi.doi_type)](os, object, NULL, 0);
1564fa9e4066Sahrens 		*print_header = 1;
1565fa9e4066Sahrens 	}
1566fa9e4066Sahrens 
1567fa9e4066Sahrens 	if (verbosity >= 5)
156888b7b0f2SMatthew Ahrens 		dump_indirect(dn);
1569fa9e4066Sahrens 
1570fa9e4066Sahrens 	if (verbosity >= 5) {
1571fa9e4066Sahrens 		/*
1572fa9e4066Sahrens 		 * Report the list of segments that comprise the object.
1573fa9e4066Sahrens 		 */
1574fa9e4066Sahrens 		uint64_t start = 0;
1575fa9e4066Sahrens 		uint64_t end;
1576fa9e4066Sahrens 		uint64_t blkfill = 1;
1577fa9e4066Sahrens 		int minlvl = 1;
1578fa9e4066Sahrens 
1579fa9e4066Sahrens 		if (dn->dn_type == DMU_OT_DNODE) {
1580fa9e4066Sahrens 			minlvl = 0;
1581fa9e4066Sahrens 			blkfill = DNODES_PER_BLOCK;
1582fa9e4066Sahrens 		}
1583fa9e4066Sahrens 
1584fa9e4066Sahrens 		for (;;) {
15853f9d6ad7SLin Ling 			char segsize[32];
1586cdb0ab79Smaybee 			error = dnode_next_offset(dn,
1587cdb0ab79Smaybee 			    0, &start, minlvl, blkfill, 0);
1588fa9e4066Sahrens 			if (error)
1589fa9e4066Sahrens 				break;
1590fa9e4066Sahrens 			end = start;
1591cdb0ab79Smaybee 			error = dnode_next_offset(dn,
1592cdb0ab79Smaybee 			    DNODE_FIND_HOLE, &end, minlvl, blkfill, 0);
15933f9d6ad7SLin Ling 			zdb_nicenum(end - start, segsize);
1594fa9e4066Sahrens 			(void) printf("\t\tsegment [%016llx, %016llx)"
1595fa9e4066Sahrens 			    " size %5s\n", (u_longlong_t)start,
1596fa9e4066Sahrens 			    (u_longlong_t)end, segsize);
1597fa9e4066Sahrens 			if (error)
1598fa9e4066Sahrens 				break;
1599fa9e4066Sahrens 			start = end;
1600fa9e4066Sahrens 		}
1601fa9e4066Sahrens 	}
1602fa9e4066Sahrens 
1603fa9e4066Sahrens 	if (db != NULL)
1604ea8dc4b6Seschrock 		dmu_buf_rele(db, FTAG);
1605fa9e4066Sahrens }
1606fa9e4066Sahrens 
1607fa9e4066Sahrens static char *objset_types[DMU_OST_NUMTYPES] = {
1608fa9e4066Sahrens 	"NONE", "META", "ZPL", "ZVOL", "OTHER", "ANY" };
1609fa9e4066Sahrens 
1610fa9e4066Sahrens static void
1611fa9e4066Sahrens dump_dir(objset_t *os)
1612fa9e4066Sahrens {
1613fa9e4066Sahrens 	dmu_objset_stats_t dds;
1614fa9e4066Sahrens 	uint64_t object, object_count;
1615a2eea2e1Sahrens 	uint64_t refdbytes, usedobjs, scratch;
16163f9d6ad7SLin Ling 	char numbuf[32];
161714843421SMatthew Ahrens 	char blkbuf[BP_SPRINTF_LEN + 20];
1618fa9e4066Sahrens 	char osname[MAXNAMELEN];
1619fa9e4066Sahrens 	char *type = "UNKNOWN";
1620fa9e4066Sahrens 	int verbosity = dump_opt['d'];
1621fa9e4066Sahrens 	int print_header = 1;
1622fa9e4066Sahrens 	int i, error;
1623fa9e4066Sahrens 
1624a2eea2e1Sahrens 	dmu_objset_fast_stat(os, &dds);
1625fa9e4066Sahrens 
1626fa9e4066Sahrens 	if (dds.dds_type < DMU_OST_NUMTYPES)
1627fa9e4066Sahrens 		type = objset_types[dds.dds_type];
1628fa9e4066Sahrens 
1629fa9e4066Sahrens 	if (dds.dds_type == DMU_OST_META) {
1630fa9e4066Sahrens 		dds.dds_creation_txg = TXG_INITIAL;
1631503ad85cSMatthew Ahrens 		usedobjs = os->os_rootbp->blk_fill;
1632503ad85cSMatthew Ahrens 		refdbytes = os->os_spa->spa_dsl_pool->
163374e7dc98SMatthew Ahrens 		    dp_mos_dir->dd_phys->dd_used_bytes;
1634a2eea2e1Sahrens 	} else {
1635a2eea2e1Sahrens 		dmu_objset_space(os, &refdbytes, &scratch, &usedobjs, &scratch);
1636fa9e4066Sahrens 	}
1637fa9e4066Sahrens 
1638503ad85cSMatthew Ahrens 	ASSERT3U(usedobjs, ==, os->os_rootbp->blk_fill);
1639fa9e4066Sahrens 
16403f9d6ad7SLin Ling 	zdb_nicenum(refdbytes, numbuf);
1641fa9e4066Sahrens 
1642fa9e4066Sahrens 	if (verbosity >= 4) {
1643e4161df6SVictor Latushkin 		(void) sprintf(blkbuf, ", rootbp ");
1644b24ab676SJeff Bonwick 		(void) sprintf_blkptr(blkbuf + strlen(blkbuf), os->os_rootbp);
1645fa9e4066Sahrens 	} else {
1646fa9e4066Sahrens 		blkbuf[0] = '\0';
1647fa9e4066Sahrens 	}
1648fa9e4066Sahrens 
1649fa9e4066Sahrens 	dmu_objset_name(os, osname);
1650fa9e4066Sahrens 
1651a2eea2e1Sahrens 	(void) printf("Dataset %s [%s], ID %llu, cr_txg %llu, "
1652fa9e4066Sahrens 	    "%s, %llu objects%s\n",
1653fa9e4066Sahrens 	    osname, type, (u_longlong_t)dmu_objset_id(os),
1654fa9e4066Sahrens 	    (u_longlong_t)dds.dds_creation_txg,
1655a2eea2e1Sahrens 	    numbuf, (u_longlong_t)usedobjs, blkbuf);
1656fa9e4066Sahrens 
1657b24ab676SJeff Bonwick 	if (zopt_objects != 0) {
1658b24ab676SJeff Bonwick 		for (i = 0; i < zopt_objects; i++)
1659b24ab676SJeff Bonwick 			dump_object(os, zopt_object[i], verbosity,
1660b24ab676SJeff Bonwick 			    &print_header);
1661b24ab676SJeff Bonwick 		(void) printf("\n");
1662b24ab676SJeff Bonwick 		return;
1663b24ab676SJeff Bonwick 	}
1664b24ab676SJeff Bonwick 
1665b24ab676SJeff Bonwick 	if (dump_opt['i'] != 0 || verbosity >= 2)
1666fa9e4066Sahrens 		dump_intent_log(dmu_objset_zil(os));
1667fa9e4066Sahrens 
1668fa9e4066Sahrens 	if (dmu_objset_ds(os) != NULL)
1669cde58dbcSMatthew Ahrens 		dump_deadlist(&dmu_objset_ds(os)->ds_deadlist);
1670fa9e4066Sahrens 
1671fa9e4066Sahrens 	if (verbosity < 2)
1672fa9e4066Sahrens 		return;
1673fa9e4066Sahrens 
1674503ad85cSMatthew Ahrens 	if (os->os_rootbp->blk_birth == 0)
1675088f3894Sahrens 		return;
1676088f3894Sahrens 
1677fa9e4066Sahrens 	dump_object(os, 0, verbosity, &print_header);
167814843421SMatthew Ahrens 	object_count = 0;
1679744947dcSTom Erickson 	if (DMU_USERUSED_DNODE(os) != NULL &&
1680744947dcSTom Erickson 	    DMU_USERUSED_DNODE(os)->dn_type != 0) {
168114843421SMatthew Ahrens 		dump_object(os, DMU_USERUSED_OBJECT, verbosity, &print_header);
168214843421SMatthew Ahrens 		dump_object(os, DMU_GROUPUSED_OBJECT, verbosity, &print_header);
168314843421SMatthew Ahrens 	}
1684fa9e4066Sahrens 
1685fa9e4066Sahrens 	object = 0;
16866754306eSahrens 	while ((error = dmu_object_next(os, &object, B_FALSE, 0)) == 0) {
1687fa9e4066Sahrens 		dump_object(os, object, verbosity, &print_header);
1688fa9e4066Sahrens 		object_count++;
1689fa9e4066Sahrens 	}
1690fa9e4066Sahrens 
1691a2eea2e1Sahrens 	ASSERT3U(object_count, ==, usedobjs);
1692fa9e4066Sahrens 
1693fa9e4066Sahrens 	(void) printf("\n");
1694fa9e4066Sahrens 
1695ccba0801SRich Morris 	if (error != ESRCH) {
1696ccba0801SRich Morris 		(void) fprintf(stderr, "dmu_object_next() = %d\n", error);
1697ccba0801SRich Morris 		abort();
1698ccba0801SRich Morris 	}
1699fa9e4066Sahrens }
1700fa9e4066Sahrens 
1701fa9e4066Sahrens static void
170253b9a4a9SVictor Latushkin dump_uberblock(uberblock_t *ub, const char *header, const char *footer)
1703fa9e4066Sahrens {
1704fa9e4066Sahrens 	time_t timestamp = ub->ub_timestamp;
1705fa9e4066Sahrens 
170653b9a4a9SVictor Latushkin 	(void) printf(header ? header : "");
1707fa9e4066Sahrens 	(void) printf("\tmagic = %016llx\n", (u_longlong_t)ub->ub_magic);
1708fa9e4066Sahrens 	(void) printf("\tversion = %llu\n", (u_longlong_t)ub->ub_version);
1709fa9e4066Sahrens 	(void) printf("\ttxg = %llu\n", (u_longlong_t)ub->ub_txg);
1710fa9e4066Sahrens 	(void) printf("\tguid_sum = %llu\n", (u_longlong_t)ub->ub_guid_sum);
1711fa9e4066Sahrens 	(void) printf("\ttimestamp = %llu UTC = %s",
1712fa9e4066Sahrens 	    (u_longlong_t)ub->ub_timestamp, asctime(localtime(&timestamp)));
1713fa9e4066Sahrens 	if (dump_opt['u'] >= 3) {
1714fbabab8fSmaybee 		char blkbuf[BP_SPRINTF_LEN];
1715b24ab676SJeff Bonwick 		sprintf_blkptr(blkbuf, &ub->ub_rootbp);
1716fa9e4066Sahrens 		(void) printf("\trootbp = %s\n", blkbuf);
1717fa9e4066Sahrens 	}
171853b9a4a9SVictor Latushkin 	(void) printf(footer ? footer : "");
1719fa9e4066Sahrens }
1720fa9e4066Sahrens 
1721fa9e4066Sahrens static void
172207428bdfSVictor Latushkin dump_config(spa_t *spa)
1723fa9e4066Sahrens {
172407428bdfSVictor Latushkin 	dmu_buf_t *db;
172507428bdfSVictor Latushkin 	size_t nvsize = 0;
172607428bdfSVictor Latushkin 	int error = 0;
1727fa9e4066Sahrens 
172807428bdfSVictor Latushkin 
172907428bdfSVictor Latushkin 	error = dmu_bonus_hold(spa->spa_meta_objset,
173007428bdfSVictor Latushkin 	    spa->spa_config_object, FTAG, &db);
173107428bdfSVictor Latushkin 
173207428bdfSVictor Latushkin 	if (error == 0) {
173307428bdfSVictor Latushkin 		nvsize = *(uint64_t *)db->db_data;
173407428bdfSVictor Latushkin 		dmu_buf_rele(db, FTAG);
173507428bdfSVictor Latushkin 
173607428bdfSVictor Latushkin 		(void) printf("\nMOS Configuration:\n");
173707428bdfSVictor Latushkin 		dump_packed_nvlist(spa->spa_meta_objset,
173807428bdfSVictor Latushkin 		    spa->spa_config_object, (void *)&nvsize, 1);
173907428bdfSVictor Latushkin 	} else {
174007428bdfSVictor Latushkin 		(void) fprintf(stderr, "dmu_bonus_hold(%llu) failed, errno %d",
174107428bdfSVictor Latushkin 		    (u_longlong_t)spa->spa_config_object, error);
1742fa9e4066Sahrens 	}
1743fa9e4066Sahrens }
1744fa9e4066Sahrens 
1745fa9e4066Sahrens static void
1746c5904d13Seschrock dump_cachefile(const char *cachefile)
1747c5904d13Seschrock {
1748c5904d13Seschrock 	int fd;
1749c5904d13Seschrock 	struct stat64 statbuf;
1750c5904d13Seschrock 	char *buf;
1751c5904d13Seschrock 	nvlist_t *config;
1752c5904d13Seschrock 
1753c5904d13Seschrock 	if ((fd = open64(cachefile, O_RDONLY)) < 0) {
1754c5904d13Seschrock 		(void) printf("cannot open '%s': %s\n", cachefile,
1755c5904d13Seschrock 		    strerror(errno));
1756c5904d13Seschrock 		exit(1);
1757c5904d13Seschrock 	}
1758c5904d13Seschrock 
1759c5904d13Seschrock 	if (fstat64(fd, &statbuf) != 0) {
1760c5904d13Seschrock 		(void) printf("failed to stat '%s': %s\n", cachefile,
1761c5904d13Seschrock 		    strerror(errno));
1762c5904d13Seschrock 		exit(1);
1763c5904d13Seschrock 	}
1764c5904d13Seschrock 
1765c5904d13Seschrock 	if ((buf = malloc(statbuf.st_size)) == NULL) {
1766c5904d13Seschrock 		(void) fprintf(stderr, "failed to allocate %llu bytes\n",
1767c5904d13Seschrock 		    (u_longlong_t)statbuf.st_size);
1768c5904d13Seschrock 		exit(1);
1769c5904d13Seschrock 	}
1770c5904d13Seschrock 
1771c5904d13Seschrock 	if (read(fd, buf, statbuf.st_size) != statbuf.st_size) {
1772c5904d13Seschrock 		(void) fprintf(stderr, "failed to read %llu bytes\n",
1773c5904d13Seschrock 		    (u_longlong_t)statbuf.st_size);
1774c5904d13Seschrock 		exit(1);
1775c5904d13Seschrock 	}
1776c5904d13Seschrock 
1777c5904d13Seschrock 	(void) close(fd);
1778c5904d13Seschrock 
1779c5904d13Seschrock 	if (nvlist_unpack(buf, statbuf.st_size, &config, 0) != 0) {
1780c5904d13Seschrock 		(void) fprintf(stderr, "failed to unpack nvlist\n");
1781c5904d13Seschrock 		exit(1);
1782c5904d13Seschrock 	}
1783c5904d13Seschrock 
1784c5904d13Seschrock 	free(buf);
1785c5904d13Seschrock 
1786c5904d13Seschrock 	dump_nvlist(config, 0);
1787c5904d13Seschrock 
1788c5904d13Seschrock 	nvlist_free(config);
1789c5904d13Seschrock }
1790c5904d13Seschrock 
179153b9a4a9SVictor Latushkin #define	ZDB_MAX_UB_HEADER_SIZE 32
179253b9a4a9SVictor Latushkin 
179353b9a4a9SVictor Latushkin static void
179453b9a4a9SVictor Latushkin dump_label_uberblocks(vdev_label_t *lbl, uint64_t ashift)
179553b9a4a9SVictor Latushkin {
179653b9a4a9SVictor Latushkin 	vdev_t vd;
179753b9a4a9SVictor Latushkin 	vdev_t *vdp = &vd;
179853b9a4a9SVictor Latushkin 	char header[ZDB_MAX_UB_HEADER_SIZE];
179953b9a4a9SVictor Latushkin 
180053b9a4a9SVictor Latushkin 	vd.vdev_ashift = ashift;
180153b9a4a9SVictor Latushkin 	vdp->vdev_top = vdp;
180253b9a4a9SVictor Latushkin 
180353b9a4a9SVictor Latushkin 	for (int i = 0; i < VDEV_UBERBLOCK_COUNT(vdp); i++) {
180453b9a4a9SVictor Latushkin 		uint64_t uoff = VDEV_UBERBLOCK_OFFSET(vdp, i);
180553b9a4a9SVictor Latushkin 		uberblock_t *ub = (void *)((char *)lbl + uoff);
180653b9a4a9SVictor Latushkin 
180753b9a4a9SVictor Latushkin 		if (uberblock_verify(ub))
180853b9a4a9SVictor Latushkin 			continue;
180953b9a4a9SVictor Latushkin 		(void) snprintf(header, ZDB_MAX_UB_HEADER_SIZE,
181053b9a4a9SVictor Latushkin 		    "Uberblock[%d]\n", i);
181153b9a4a9SVictor Latushkin 		dump_uberblock(ub, header, "");
181253b9a4a9SVictor Latushkin 	}
181353b9a4a9SVictor Latushkin }
181453b9a4a9SVictor Latushkin 
1815c5904d13Seschrock static void
1816fa9e4066Sahrens dump_label(const char *dev)
1817fa9e4066Sahrens {
1818fa9e4066Sahrens 	int fd;
1819fa9e4066Sahrens 	vdev_label_t label;
1820c6065d0fSGeorge Wilson 	char *path, *buf = label.vl_vdev_phys.vp_nvlist;
1821fa9e4066Sahrens 	size_t buflen = sizeof (label.vl_vdev_phys.vp_nvlist);
1822fa9e4066Sahrens 	struct stat64 statbuf;
182353b9a4a9SVictor Latushkin 	uint64_t psize, ashift;
1824c6065d0fSGeorge Wilson 	int len = strlen(dev) + 1;
1825fa9e4066Sahrens 
1826c6065d0fSGeorge Wilson 	if (strncmp(dev, "/dev/dsk/", 9) == 0) {
1827c6065d0fSGeorge Wilson 		len++;
1828c6065d0fSGeorge Wilson 		path = malloc(len);
1829c6065d0fSGeorge Wilson 		(void) snprintf(path, len, "%s%s", "/dev/rdsk/", dev + 9);
1830c6065d0fSGeorge Wilson 	} else {
1831c6065d0fSGeorge Wilson 		path = strdup(dev);
1832c6065d0fSGeorge Wilson 	}
1833c6065d0fSGeorge Wilson 
1834c6065d0fSGeorge Wilson 	if ((fd = open64(path, O_RDONLY)) < 0) {
1835c6065d0fSGeorge Wilson 		(void) printf("cannot open '%s': %s\n", path, strerror(errno));
1836c6065d0fSGeorge Wilson 		free(path);
1837fa9e4066Sahrens 		exit(1);
1838fa9e4066Sahrens 	}
1839fa9e4066Sahrens 
1840fa9e4066Sahrens 	if (fstat64(fd, &statbuf) != 0) {
1841c6065d0fSGeorge Wilson 		(void) printf("failed to stat '%s': %s\n", path,
1842fa9e4066Sahrens 		    strerror(errno));
1843c6065d0fSGeorge Wilson 		free(path);
1844c6065d0fSGeorge Wilson 		(void) close(fd);
1845c6065d0fSGeorge Wilson 		exit(1);
1846c6065d0fSGeorge Wilson 	}
1847c6065d0fSGeorge Wilson 
1848c6065d0fSGeorge Wilson 	if (S_ISBLK(statbuf.st_mode)) {
1849c6065d0fSGeorge Wilson 		(void) printf("cannot use '%s': character device required\n",
1850c6065d0fSGeorge Wilson 		    path);
1851c6065d0fSGeorge Wilson 		free(path);
1852c6065d0fSGeorge Wilson 		(void) close(fd);
1853c6065d0fSGeorge Wilson 		exit(1);
1854fa9e4066Sahrens 	}
1855fa9e4066Sahrens 
1856fa9e4066Sahrens 	psize = statbuf.st_size;
1857fa9e4066Sahrens 	psize = P2ALIGN(psize, (uint64_t)sizeof (vdev_label_t));
1858fa9e4066Sahrens 
185953b9a4a9SVictor Latushkin 	for (int l = 0; l < VDEV_LABELS; l++) {
1860fa9e4066Sahrens 		nvlist_t *config = NULL;
1861fa9e4066Sahrens 
1862fa9e4066Sahrens 		(void) printf("--------------------------------------------\n");
1863fa9e4066Sahrens 		(void) printf("LABEL %d\n", l);
1864fa9e4066Sahrens 		(void) printf("--------------------------------------------\n");
1865fa9e4066Sahrens 
18660d981225Seschrock 		if (pread64(fd, &label, sizeof (label),
1867fa9e4066Sahrens 		    vdev_label_offset(psize, l, 0)) != sizeof (label)) {
1868fa9e4066Sahrens 			(void) printf("failed to read label %d\n", l);
1869fa9e4066Sahrens 			continue;
1870fa9e4066Sahrens 		}
1871fa9e4066Sahrens 
1872fa9e4066Sahrens 		if (nvlist_unpack(buf, buflen, &config, 0) != 0) {
1873fa9e4066Sahrens 			(void) printf("failed to unpack label %d\n", l);
187453b9a4a9SVictor Latushkin 			ashift = SPA_MINBLOCKSHIFT;
187553b9a4a9SVictor Latushkin 		} else {
187653b9a4a9SVictor Latushkin 			nvlist_t *vdev_tree = NULL;
187753b9a4a9SVictor Latushkin 
1878fa9e4066Sahrens 			dump_nvlist(config, 4);
187953b9a4a9SVictor Latushkin 			if ((nvlist_lookup_nvlist(config,
188053b9a4a9SVictor Latushkin 			    ZPOOL_CONFIG_VDEV_TREE, &vdev_tree) != 0) ||
188153b9a4a9SVictor Latushkin 			    (nvlist_lookup_uint64(vdev_tree,
188253b9a4a9SVictor Latushkin 			    ZPOOL_CONFIG_ASHIFT, &ashift) != 0))
188353b9a4a9SVictor Latushkin 				ashift = SPA_MINBLOCKSHIFT;
1884fa9e4066Sahrens 			nvlist_free(config);
1885fa9e4066Sahrens 		}
188653b9a4a9SVictor Latushkin 		if (dump_opt['u'])
188753b9a4a9SVictor Latushkin 			dump_label_uberblocks(&label, ashift);
188853b9a4a9SVictor Latushkin 	}
1889c6065d0fSGeorge Wilson 
1890c6065d0fSGeorge Wilson 	free(path);
1891c6065d0fSGeorge Wilson 	(void) close(fd);
1892fa9e4066Sahrens }
1893fa9e4066Sahrens 
1894fa9e4066Sahrens /*ARGSUSED*/
18951d452cf5Sahrens static int
1896fd136879SMatthew Ahrens dump_one_dir(const char *dsname, void *arg)
1897fa9e4066Sahrens {
1898fa9e4066Sahrens 	int error;
1899fa9e4066Sahrens 	objset_t *os;
1900fa9e4066Sahrens 
1901503ad85cSMatthew Ahrens 	error = dmu_objset_own(dsname, DMU_OST_ANY, B_TRUE, FTAG, &os);
1902fa9e4066Sahrens 	if (error) {
1903b24ab676SJeff Bonwick 		(void) printf("Could not open %s, error %d\n", dsname, error);
19041d452cf5Sahrens 		return (0);
1905fa9e4066Sahrens 	}
1906fa9e4066Sahrens 	dump_dir(os);
1907503ad85cSMatthew Ahrens 	dmu_objset_disown(os, FTAG);
1908e0d35c44Smarks 	fuid_table_destroy();
19090a586ceaSMark Shellenbaum 	sa_loaded = B_FALSE;
19101d452cf5Sahrens 	return (0);
1911fa9e4066Sahrens }
1912fa9e4066Sahrens 
1913b24ab676SJeff Bonwick /*
1914b24ab676SJeff Bonwick  * Block statistics.
1915b24ab676SJeff Bonwick  */
1916b24ab676SJeff Bonwick typedef struct zdb_blkstats {
1917b24ab676SJeff Bonwick 	uint64_t	zb_asize;
1918b24ab676SJeff Bonwick 	uint64_t	zb_lsize;
1919b24ab676SJeff Bonwick 	uint64_t	zb_psize;
1920b24ab676SJeff Bonwick 	uint64_t	zb_count;
1921b24ab676SJeff Bonwick } zdb_blkstats_t;
1922b24ab676SJeff Bonwick 
1923b24ab676SJeff Bonwick /*
1924b24ab676SJeff Bonwick  * Extended object types to report deferred frees and dedup auto-ditto blocks.
1925b24ab676SJeff Bonwick  */
1926b24ab676SJeff Bonwick #define	ZDB_OT_DEFERRED	(DMU_OT_NUMTYPES + 0)
1927b24ab676SJeff Bonwick #define	ZDB_OT_DITTO	(DMU_OT_NUMTYPES + 1)
1928*ad135b5dSChristopher Siden #define	ZDB_OT_OTHER	(DMU_OT_NUMTYPES + 2)
1929*ad135b5dSChristopher Siden #define	ZDB_OT_TOTAL	(DMU_OT_NUMTYPES + 3)
1930b24ab676SJeff Bonwick 
1931b24ab676SJeff Bonwick static char *zdb_ot_extname[] = {
1932b24ab676SJeff Bonwick 	"deferred free",
1933b24ab676SJeff Bonwick 	"dedup ditto",
1934*ad135b5dSChristopher Siden 	"other",
1935b24ab676SJeff Bonwick 	"Total",
1936b24ab676SJeff Bonwick };
1937b24ab676SJeff Bonwick 
1938b24ab676SJeff Bonwick #define	ZB_TOTAL	DN_MAX_LEVELS
1939b24ab676SJeff Bonwick 
1940b24ab676SJeff Bonwick typedef struct zdb_cb {
1941b24ab676SJeff Bonwick 	zdb_blkstats_t	zcb_type[ZB_TOTAL + 1][ZDB_OT_TOTAL + 1];
1942b24ab676SJeff Bonwick 	uint64_t	zcb_dedup_asize;
1943b24ab676SJeff Bonwick 	uint64_t	zcb_dedup_blocks;
1944b24ab676SJeff Bonwick 	uint64_t	zcb_errors[256];
1945b24ab676SJeff Bonwick 	int		zcb_readfails;
1946b24ab676SJeff Bonwick 	int		zcb_haderrors;
1947cde58dbcSMatthew Ahrens 	spa_t		*zcb_spa;
1948b24ab676SJeff Bonwick } zdb_cb_t;
1949b24ab676SJeff Bonwick 
1950b24ab676SJeff Bonwick static void
1951cde58dbcSMatthew Ahrens zdb_count_block(zdb_cb_t *zcb, zilog_t *zilog, const blkptr_t *bp,
1952b24ab676SJeff Bonwick     dmu_object_type_t type)
1953b24ab676SJeff Bonwick {
1954b24ab676SJeff Bonwick 	uint64_t refcnt = 0;
1955b24ab676SJeff Bonwick 
1956b24ab676SJeff Bonwick 	ASSERT(type < ZDB_OT_TOTAL);
1957b24ab676SJeff Bonwick 
1958b24ab676SJeff Bonwick 	if (zilog && zil_bp_tree_add(zilog, bp) != 0)
1959b24ab676SJeff Bonwick 		return;
1960b24ab676SJeff Bonwick 
1961b24ab676SJeff Bonwick 	for (int i = 0; i < 4; i++) {
1962b24ab676SJeff Bonwick 		int l = (i < 2) ? BP_GET_LEVEL(bp) : ZB_TOTAL;
1963b24ab676SJeff Bonwick 		int t = (i & 1) ? type : ZDB_OT_TOTAL;
1964b24ab676SJeff Bonwick 		zdb_blkstats_t *zb = &zcb->zcb_type[l][t];
1965b24ab676SJeff Bonwick 
1966b24ab676SJeff Bonwick 		zb->zb_asize += BP_GET_ASIZE(bp);
1967b24ab676SJeff Bonwick 		zb->zb_lsize += BP_GET_LSIZE(bp);
1968b24ab676SJeff Bonwick 		zb->zb_psize += BP_GET_PSIZE(bp);
1969b24ab676SJeff Bonwick 		zb->zb_count++;
1970b24ab676SJeff Bonwick 	}
1971b24ab676SJeff Bonwick 
1972b24ab676SJeff Bonwick 	if (dump_opt['L'])
1973b24ab676SJeff Bonwick 		return;
1974b24ab676SJeff Bonwick 
1975b24ab676SJeff Bonwick 	if (BP_GET_DEDUP(bp)) {
1976b24ab676SJeff Bonwick 		ddt_t *ddt;
1977b24ab676SJeff Bonwick 		ddt_entry_t *dde;
1978b24ab676SJeff Bonwick 
1979cde58dbcSMatthew Ahrens 		ddt = ddt_select(zcb->zcb_spa, bp);
1980b24ab676SJeff Bonwick 		ddt_enter(ddt);
1981b24ab676SJeff Bonwick 		dde = ddt_lookup(ddt, bp, B_FALSE);
1982b24ab676SJeff Bonwick 
1983b24ab676SJeff Bonwick 		if (dde == NULL) {
1984b24ab676SJeff Bonwick 			refcnt = 0;
1985b24ab676SJeff Bonwick 		} else {
1986b24ab676SJeff Bonwick 			ddt_phys_t *ddp = ddt_phys_select(dde, bp);
1987b24ab676SJeff Bonwick 			ddt_phys_decref(ddp);
1988b24ab676SJeff Bonwick 			refcnt = ddp->ddp_refcnt;
1989b24ab676SJeff Bonwick 			if (ddt_phys_total_refcnt(dde) == 0)
1990b24ab676SJeff Bonwick 				ddt_remove(ddt, dde);
1991b24ab676SJeff Bonwick 		}
1992b24ab676SJeff Bonwick 		ddt_exit(ddt);
1993b24ab676SJeff Bonwick 	}
1994b24ab676SJeff Bonwick 
1995cde58dbcSMatthew Ahrens 	VERIFY3U(zio_wait(zio_claim(NULL, zcb->zcb_spa,
1996cde58dbcSMatthew Ahrens 	    refcnt ? 0 : spa_first_txg(zcb->zcb_spa),
1997b24ab676SJeff Bonwick 	    bp, NULL, NULL, ZIO_FLAG_CANFAIL)), ==, 0);
1998b24ab676SJeff Bonwick }
1999b24ab676SJeff Bonwick 
20003f9d6ad7SLin Ling /* ARGSUSED */
2001b24ab676SJeff Bonwick static int
20023f9d6ad7SLin Ling zdb_blkptr_cb(spa_t *spa, zilog_t *zilog, const blkptr_t *bp, arc_buf_t *pbuf,
2003b24ab676SJeff Bonwick     const zbookmark_t *zb, const dnode_phys_t *dnp, void *arg)
2004b24ab676SJeff Bonwick {
2005b24ab676SJeff Bonwick 	zdb_cb_t *zcb = arg;
2006b24ab676SJeff Bonwick 	char blkbuf[BP_SPRINTF_LEN];
2007b24ab676SJeff Bonwick 	dmu_object_type_t type;
2008b24ab676SJeff Bonwick 	boolean_t is_metadata;
2009b24ab676SJeff Bonwick 
2010b24ab676SJeff Bonwick 	if (bp == NULL)
2011b24ab676SJeff Bonwick 		return (0);
2012b24ab676SJeff Bonwick 
2013b24ab676SJeff Bonwick 	type = BP_GET_TYPE(bp);
2014b24ab676SJeff Bonwick 
2015*ad135b5dSChristopher Siden 	zdb_count_block(zcb, zilog, bp,
2016*ad135b5dSChristopher Siden 	    (type & DMU_OT_NEWTYPE) ? ZDB_OT_OTHER : type);
2017b24ab676SJeff Bonwick 
2018*ad135b5dSChristopher Siden 	is_metadata = (BP_GET_LEVEL(bp) != 0 || DMU_OT_IS_METADATA(type));
2019b24ab676SJeff Bonwick 
2020b24ab676SJeff Bonwick 	if (dump_opt['c'] > 1 || (dump_opt['c'] && is_metadata)) {
2021b24ab676SJeff Bonwick 		int ioerr;
2022b24ab676SJeff Bonwick 		size_t size = BP_GET_PSIZE(bp);
2023b24ab676SJeff Bonwick 		void *data = malloc(size);
2024b24ab676SJeff Bonwick 		int flags = ZIO_FLAG_CANFAIL | ZIO_FLAG_SCRUB | ZIO_FLAG_RAW;
2025b24ab676SJeff Bonwick 
2026b24ab676SJeff Bonwick 		/* If it's an intent log block, failure is expected. */
2027b24ab676SJeff Bonwick 		if (zb->zb_level == ZB_ZIL_LEVEL)
2028b24ab676SJeff Bonwick 			flags |= ZIO_FLAG_SPECULATIVE;
2029b24ab676SJeff Bonwick 
2030b24ab676SJeff Bonwick 		ioerr = zio_wait(zio_read(NULL, spa, bp, data, size,
2031b24ab676SJeff Bonwick 		    NULL, NULL, ZIO_PRIORITY_ASYNC_READ, flags, zb));
2032b24ab676SJeff Bonwick 
2033b24ab676SJeff Bonwick 		free(data);
2034b24ab676SJeff Bonwick 
2035b24ab676SJeff Bonwick 		if (ioerr && !(flags & ZIO_FLAG_SPECULATIVE)) {
2036b24ab676SJeff Bonwick 			zcb->zcb_haderrors = 1;
2037b24ab676SJeff Bonwick 			zcb->zcb_errors[ioerr]++;
2038b24ab676SJeff Bonwick 
2039b24ab676SJeff Bonwick 			if (dump_opt['b'] >= 2)
2040b24ab676SJeff Bonwick 				sprintf_blkptr(blkbuf, bp);
2041b24ab676SJeff Bonwick 			else
2042b24ab676SJeff Bonwick 				blkbuf[0] = '\0';
2043b24ab676SJeff Bonwick 
2044b24ab676SJeff Bonwick 			(void) printf("zdb_blkptr_cb: "
2045b24ab676SJeff Bonwick 			    "Got error %d reading "
2046b24ab676SJeff Bonwick 			    "<%llu, %llu, %lld, %llx> %s -- skipping\n",
2047b24ab676SJeff Bonwick 			    ioerr,
2048b24ab676SJeff Bonwick 			    (u_longlong_t)zb->zb_objset,
2049b24ab676SJeff Bonwick 			    (u_longlong_t)zb->zb_object,
2050b24ab676SJeff Bonwick 			    (u_longlong_t)zb->zb_level,
2051b24ab676SJeff Bonwick 			    (u_longlong_t)zb->zb_blkid,
2052b24ab676SJeff Bonwick 			    blkbuf);
2053b24ab676SJeff Bonwick 		}
2054b24ab676SJeff Bonwick 	}
2055b24ab676SJeff Bonwick 
2056b24ab676SJeff Bonwick 	zcb->zcb_readfails = 0;
2057b24ab676SJeff Bonwick 
2058b24ab676SJeff Bonwick 	if (dump_opt['b'] >= 4) {
2059b24ab676SJeff Bonwick 		sprintf_blkptr(blkbuf, bp);
2060b24ab676SJeff Bonwick 		(void) printf("objset %llu object %llu "
2061b24ab676SJeff Bonwick 		    "level %lld offset 0x%llx %s\n",
2062b24ab676SJeff Bonwick 		    (u_longlong_t)zb->zb_objset,
2063b24ab676SJeff Bonwick 		    (u_longlong_t)zb->zb_object,
2064b24ab676SJeff Bonwick 		    (longlong_t)zb->zb_level,
2065b24ab676SJeff Bonwick 		    (u_longlong_t)blkid2offset(dnp, bp, zb),
2066b24ab676SJeff Bonwick 		    blkbuf);
2067b24ab676SJeff Bonwick 	}
2068b24ab676SJeff Bonwick 
2069b24ab676SJeff Bonwick 	return (0);
2070b24ab676SJeff Bonwick }
2071b24ab676SJeff Bonwick 
2072fa9e4066Sahrens static void
2073fa9e4066Sahrens zdb_leak(space_map_t *sm, uint64_t start, uint64_t size)
2074fa9e4066Sahrens {
2075e14bb325SJeff Bonwick 	vdev_t *vd = sm->sm_ppd;
2076fa9e4066Sahrens 
2077fa9e4066Sahrens 	(void) printf("leaked space: vdev %llu, offset 0x%llx, size %llu\n",
2078e14bb325SJeff Bonwick 	    (u_longlong_t)vd->vdev_id, (u_longlong_t)start, (u_longlong_t)size);
2079e14bb325SJeff Bonwick }
2080e14bb325SJeff Bonwick 
2081e14bb325SJeff Bonwick /* ARGSUSED */
2082e14bb325SJeff Bonwick static void
2083e14bb325SJeff Bonwick zdb_space_map_load(space_map_t *sm)
2084e14bb325SJeff Bonwick {
2085fa9e4066Sahrens }
2086fa9e4066Sahrens 
2087fa9e4066Sahrens static void
2088e14bb325SJeff Bonwick zdb_space_map_unload(space_map_t *sm)
2089e14bb325SJeff Bonwick {
2090e14bb325SJeff Bonwick 	space_map_vacate(sm, zdb_leak, sm);
2091e14bb325SJeff Bonwick }
2092e14bb325SJeff Bonwick 
2093e14bb325SJeff Bonwick /* ARGSUSED */
2094e14bb325SJeff Bonwick static void
2095e14bb325SJeff Bonwick zdb_space_map_claim(space_map_t *sm, uint64_t start, uint64_t size)
2096e14bb325SJeff Bonwick {
2097e14bb325SJeff Bonwick }
2098e14bb325SJeff Bonwick 
2099e14bb325SJeff Bonwick static space_map_ops_t zdb_space_map_ops = {
2100e14bb325SJeff Bonwick 	zdb_space_map_load,
2101e14bb325SJeff Bonwick 	zdb_space_map_unload,
2102e14bb325SJeff Bonwick 	NULL,	/* alloc */
2103e14bb325SJeff Bonwick 	zdb_space_map_claim,
2104d6e555bdSGeorge Wilson 	NULL,	/* free */
2105d6e555bdSGeorge Wilson 	NULL	/* maxsize */
2106e14bb325SJeff Bonwick };
2107e14bb325SJeff Bonwick 
2108e14bb325SJeff Bonwick static void
2109bbfd46c4SJeff Bonwick zdb_ddt_leak_init(spa_t *spa, zdb_cb_t *zcb)
2110fa9e4066Sahrens {
2111bbfd46c4SJeff Bonwick 	ddt_bookmark_t ddb = { 0 };
2112b24ab676SJeff Bonwick 	ddt_entry_t dde;
2113b24ab676SJeff Bonwick 	int error;
2114fa9e4066Sahrens 
2115bbfd46c4SJeff Bonwick 	while ((error = ddt_walk(spa, &ddb, &dde)) == 0) {
2116b24ab676SJeff Bonwick 		blkptr_t blk;
2117b24ab676SJeff Bonwick 		ddt_phys_t *ddp = dde.dde_phys;
2118bbfd46c4SJeff Bonwick 
2119bbfd46c4SJeff Bonwick 		if (ddb.ddb_class == DDT_CLASS_UNIQUE)
2120bbfd46c4SJeff Bonwick 			return;
2121bbfd46c4SJeff Bonwick 
2122b24ab676SJeff Bonwick 		ASSERT(ddt_phys_total_refcnt(&dde) > 1);
2123bbfd46c4SJeff Bonwick 
2124b24ab676SJeff Bonwick 		for (int p = 0; p < DDT_PHYS_TYPES; p++, ddp++) {
2125b24ab676SJeff Bonwick 			if (ddp->ddp_phys_birth == 0)
2126b24ab676SJeff Bonwick 				continue;
2127bbfd46c4SJeff Bonwick 			ddt_bp_create(ddb.ddb_checksum,
2128bbfd46c4SJeff Bonwick 			    &dde.dde_key, ddp, &blk);
2129b24ab676SJeff Bonwick 			if (p == DDT_PHYS_DITTO) {
2130cde58dbcSMatthew Ahrens 				zdb_count_block(zcb, NULL, &blk, ZDB_OT_DITTO);
2131b24ab676SJeff Bonwick 			} else {
2132b24ab676SJeff Bonwick 				zcb->zcb_dedup_asize +=
2133b24ab676SJeff Bonwick 				    BP_GET_ASIZE(&blk) * (ddp->ddp_refcnt - 1);
2134b24ab676SJeff Bonwick 				zcb->zcb_dedup_blocks++;
2135b24ab676SJeff Bonwick 			}
2136b24ab676SJeff Bonwick 		}
2137b24ab676SJeff Bonwick 		if (!dump_opt['L']) {
2138bbfd46c4SJeff Bonwick 			ddt_t *ddt = spa->spa_ddt[ddb.ddb_checksum];
2139b24ab676SJeff Bonwick 			ddt_enter(ddt);
2140b24ab676SJeff Bonwick 			VERIFY(ddt_lookup(ddt, &blk, B_TRUE) != NULL);
2141b24ab676SJeff Bonwick 			ddt_exit(ddt);
2142b24ab676SJeff Bonwick 		}
2143b24ab676SJeff Bonwick 	}
2144b24ab676SJeff Bonwick 
2145b24ab676SJeff Bonwick 	ASSERT(error == ENOENT);
2146b24ab676SJeff Bonwick }
2147b24ab676SJeff Bonwick 
2148b24ab676SJeff Bonwick static void
2149b24ab676SJeff Bonwick zdb_leak_init(spa_t *spa, zdb_cb_t *zcb)
2150b24ab676SJeff Bonwick {
2151cde58dbcSMatthew Ahrens 	zcb->zcb_spa = spa;
2152cde58dbcSMatthew Ahrens 
2153b24ab676SJeff Bonwick 	if (!dump_opt['L']) {
2154b24ab676SJeff Bonwick 		vdev_t *rvd = spa->spa_root_vdev;
2155e14bb325SJeff Bonwick 		for (int c = 0; c < rvd->vdev_children; c++) {
2156e14bb325SJeff Bonwick 			vdev_t *vd = rvd->vdev_child[c];
2157e14bb325SJeff Bonwick 			for (int m = 0; m < vd->vdev_ms_count; m++) {
2158fa9e4066Sahrens 				metaslab_t *msp = vd->vdev_ms[m];
2159fa9e4066Sahrens 				mutex_enter(&msp->ms_lock);
2160b24ab676SJeff Bonwick 				space_map_unload(&msp->ms_map);
2161b24ab676SJeff Bonwick 				VERIFY(space_map_load(&msp->ms_map,
2162b24ab676SJeff Bonwick 				    &zdb_space_map_ops, SM_ALLOC, &msp->ms_smo,
2163b24ab676SJeff Bonwick 				    spa->spa_meta_objset) == 0);
2164e14bb325SJeff Bonwick 				msp->ms_map.sm_ppd = vd;
2165fa9e4066Sahrens 				mutex_exit(&msp->ms_lock);
2166fa9e4066Sahrens 			}
2167fa9e4066Sahrens 		}
2168fa9e4066Sahrens 	}
2169fa9e4066Sahrens 
2170b24ab676SJeff Bonwick 	spa_config_enter(spa, SCL_CONFIG, FTAG, RW_READER);
2171b24ab676SJeff Bonwick 
2172bbfd46c4SJeff Bonwick 	zdb_ddt_leak_init(spa, zcb);
2173b24ab676SJeff Bonwick 
2174b24ab676SJeff Bonwick 	spa_config_exit(spa, SCL_CONFIG, FTAG);
2175b24ab676SJeff Bonwick }
2176b24ab676SJeff Bonwick 
2177fa9e4066Sahrens static void
2178e14bb325SJeff Bonwick zdb_leak_fini(spa_t *spa)
2179fa9e4066Sahrens {
2180b24ab676SJeff Bonwick 	if (!dump_opt['L']) {
2181fa9e4066Sahrens 		vdev_t *rvd = spa->spa_root_vdev;
2182e14bb325SJeff Bonwick 		for (int c = 0; c < rvd->vdev_children; c++) {
2183e14bb325SJeff Bonwick 			vdev_t *vd = rvd->vdev_child[c];
2184e14bb325SJeff Bonwick 			for (int m = 0; m < vd->vdev_ms_count; m++) {
2185e14bb325SJeff Bonwick 				metaslab_t *msp = vd->vdev_ms[m];
2186e14bb325SJeff Bonwick 				mutex_enter(&msp->ms_lock);
2187e14bb325SJeff Bonwick 				space_map_unload(&msp->ms_map);
2188e14bb325SJeff Bonwick 				mutex_exit(&msp->ms_lock);
2189e14bb325SJeff Bonwick 			}
2190e14bb325SJeff Bonwick 		}
2191fa9e4066Sahrens 	}
2192fa9e4066Sahrens }
2193fa9e4066Sahrens 
2194cde58dbcSMatthew Ahrens /* ARGSUSED */
2195cde58dbcSMatthew Ahrens static int
2196cde58dbcSMatthew Ahrens count_block_cb(void *arg, const blkptr_t *bp, dmu_tx_t *tx)
2197cde58dbcSMatthew Ahrens {
2198cde58dbcSMatthew Ahrens 	zdb_cb_t *zcb = arg;
2199cde58dbcSMatthew Ahrens 
2200cde58dbcSMatthew Ahrens 	if (dump_opt['b'] >= 4) {
2201cde58dbcSMatthew Ahrens 		char blkbuf[BP_SPRINTF_LEN];
2202cde58dbcSMatthew Ahrens 		sprintf_blkptr(blkbuf, bp);
2203cde58dbcSMatthew Ahrens 		(void) printf("[%s] %s\n",
2204cde58dbcSMatthew Ahrens 		    "deferred free", blkbuf);
2205cde58dbcSMatthew Ahrens 	}
2206cde58dbcSMatthew Ahrens 	zdb_count_block(zcb, NULL, bp, ZDB_OT_DEFERRED);
2207cde58dbcSMatthew Ahrens 	return (0);
2208cde58dbcSMatthew Ahrens }
2209cde58dbcSMatthew Ahrens 
2210fa9e4066Sahrens static int
2211fa9e4066Sahrens dump_block_stats(spa_t *spa)
2212fa9e4066Sahrens {
2213fa9e4066Sahrens 	zdb_cb_t zcb = { 0 };
2214fa9e4066Sahrens 	zdb_blkstats_t *zb, *tzb;
2215b24ab676SJeff Bonwick 	uint64_t norm_alloc, norm_space, total_alloc, total_found;
2216cd088ea4SVictor Latushkin 	int flags = TRAVERSE_PRE | TRAVERSE_PREFETCH_METADATA | TRAVERSE_HARD;
2217fa9e4066Sahrens 	int leaks = 0;
2218fa9e4066Sahrens 
22196365109dSVictor Latushkin 	(void) printf("\nTraversing all blocks %s%s%s%s%s...\n",
222082a0a985SVictor Latushkin 	    (dump_opt['c'] || !dump_opt['L']) ? "to verify " : "",
22216365109dSVictor Latushkin 	    (dump_opt['c'] == 1) ? "metadata " : "",
222282a0a985SVictor Latushkin 	    dump_opt['c'] ? "checksums " : "",
222382a0a985SVictor Latushkin 	    (dump_opt['c'] && !dump_opt['L']) ? "and verify " : "",
222482a0a985SVictor Latushkin 	    !dump_opt['L'] ? "nothing leaked " : "");
2225fa9e4066Sahrens 
2226fa9e4066Sahrens 	/*
2227e14bb325SJeff Bonwick 	 * Load all space maps as SM_ALLOC maps, then traverse the pool
2228e14bb325SJeff Bonwick 	 * claiming each block we discover.  If the pool is perfectly
2229e14bb325SJeff Bonwick 	 * consistent, the space maps will be empty when we're done.
2230e14bb325SJeff Bonwick 	 * Anything left over is a leak; any block we can't claim (because
2231e14bb325SJeff Bonwick 	 * it's not part of any space map) is a double allocation,
2232e14bb325SJeff Bonwick 	 * reference to a freed block, or an unclaimed log block.
2233fa9e4066Sahrens 	 */
2234b24ab676SJeff Bonwick 	zdb_leak_init(spa, &zcb);
2235fa9e4066Sahrens 
2236fa9e4066Sahrens 	/*
2237fa9e4066Sahrens 	 * If there's a deferred-free bplist, process that first.
2238fa9e4066Sahrens 	 */
2239cde58dbcSMatthew Ahrens 	(void) bpobj_iterate_nofree(&spa->spa_deferred_bpobj,
2240cde58dbcSMatthew Ahrens 	    count_block_cb, &zcb, NULL);
2241cde58dbcSMatthew Ahrens 	(void) bpobj_iterate_nofree(&spa->spa_dsl_pool->dp_free_bpobj,
2242cde58dbcSMatthew Ahrens 	    count_block_cb, &zcb, NULL);
2243*ad135b5dSChristopher Siden 	if (spa_feature_is_active(spa,
2244*ad135b5dSChristopher Siden 	    &spa_feature_table[SPA_FEATURE_ASYNC_DESTROY])) {
2245*ad135b5dSChristopher Siden 		VERIFY3U(0, ==, bptree_iterate(spa->spa_meta_objset,
2246*ad135b5dSChristopher Siden 		    spa->spa_dsl_pool->dp_bptree_obj, B_FALSE, count_block_cb,
2247*ad135b5dSChristopher Siden 		    &zcb, NULL));
2248*ad135b5dSChristopher Siden 	}
2249fa9e4066Sahrens 
2250bbfd46c4SJeff Bonwick 	if (dump_opt['c'] > 1)
2251bbfd46c4SJeff Bonwick 		flags |= TRAVERSE_PREFETCH_DATA;
2252bbfd46c4SJeff Bonwick 
2253bbfd46c4SJeff Bonwick 	zcb.zcb_haderrors |= traverse_pool(spa, 0, flags, zdb_blkptr_cb, &zcb);
2254fa9e4066Sahrens 
2255b24ab676SJeff Bonwick 	if (zcb.zcb_haderrors) {
2256fa9e4066Sahrens 		(void) printf("\nError counts:\n\n");
2257fa9e4066Sahrens 		(void) printf("\t%5s  %s\n", "errno", "count");
2258b24ab676SJeff Bonwick 		for (int e = 0; e < 256; e++) {
2259fa9e4066Sahrens 			if (zcb.zcb_errors[e] != 0) {
2260fa9e4066Sahrens 				(void) printf("\t%5d  %llu\n",
2261fa9e4066Sahrens 				    e, (u_longlong_t)zcb.zcb_errors[e]);
2262fa9e4066Sahrens 			}
2263fa9e4066Sahrens 		}
2264fa9e4066Sahrens 	}
2265fa9e4066Sahrens 
2266fa9e4066Sahrens 	/*
2267fa9e4066Sahrens 	 * Report any leaked segments.
2268fa9e4066Sahrens 	 */
2269e14bb325SJeff Bonwick 	zdb_leak_fini(spa);
2270fa9e4066Sahrens 
2271b24ab676SJeff Bonwick 	tzb = &zcb.zcb_type[ZB_TOTAL][ZDB_OT_TOTAL];
2272d41e7643Sek110237 
2273b24ab676SJeff Bonwick 	norm_alloc = metaslab_class_get_alloc(spa_normal_class(spa));
2274b24ab676SJeff Bonwick 	norm_space = metaslab_class_get_space(spa_normal_class(spa));
2275fa9e4066Sahrens 
2276b24ab676SJeff Bonwick 	total_alloc = norm_alloc + metaslab_class_get_alloc(spa_log_class(spa));
2277b24ab676SJeff Bonwick 	total_found = tzb->zb_asize - zcb.zcb_dedup_asize;
22788654d025Sperrin 
2279b24ab676SJeff Bonwick 	if (total_found == total_alloc) {
228082a0a985SVictor Latushkin 		if (!dump_opt['L'])
2281fa9e4066Sahrens 			(void) printf("\n\tNo leaks (block sum matches space"
2282fa9e4066Sahrens 			    " maps exactly)\n");
2283fa9e4066Sahrens 	} else {
2284fa9e4066Sahrens 		(void) printf("block traversal size %llu != alloc %llu "
228582a0a985SVictor Latushkin 		    "(%s %lld)\n",
2286b24ab676SJeff Bonwick 		    (u_longlong_t)total_found,
2287b24ab676SJeff Bonwick 		    (u_longlong_t)total_alloc,
228882a0a985SVictor Latushkin 		    (dump_opt['L']) ? "unreachable" : "leaked",
2289b24ab676SJeff Bonwick 		    (longlong_t)(total_alloc - total_found));
2290fa9e4066Sahrens 		leaks = 1;
2291fa9e4066Sahrens 	}
2292fa9e4066Sahrens 
2293fa9e4066Sahrens 	if (tzb->zb_count == 0)
2294fa9e4066Sahrens 		return (2);
2295fa9e4066Sahrens 
2296fa9e4066Sahrens 	(void) printf("\n");
2297fa9e4066Sahrens 	(void) printf("\tbp count:      %10llu\n",
2298fa9e4066Sahrens 	    (u_longlong_t)tzb->zb_count);
2299b24ab676SJeff Bonwick 	(void) printf("\tbp logical:    %10llu      avg: %6llu\n",
2300fa9e4066Sahrens 	    (u_longlong_t)tzb->zb_lsize,
2301fa9e4066Sahrens 	    (u_longlong_t)(tzb->zb_lsize / tzb->zb_count));
2302b24ab676SJeff Bonwick 	(void) printf("\tbp physical:   %10llu      avg:"
2303b24ab676SJeff Bonwick 	    " %6llu     compression: %6.2f\n",
2304fa9e4066Sahrens 	    (u_longlong_t)tzb->zb_psize,
2305fa9e4066Sahrens 	    (u_longlong_t)(tzb->zb_psize / tzb->zb_count),
2306fa9e4066Sahrens 	    (double)tzb->zb_lsize / tzb->zb_psize);
2307b24ab676SJeff Bonwick 	(void) printf("\tbp allocated:  %10llu      avg:"
2308b24ab676SJeff Bonwick 	    " %6llu     compression: %6.2f\n",
2309fa9e4066Sahrens 	    (u_longlong_t)tzb->zb_asize,
2310fa9e4066Sahrens 	    (u_longlong_t)(tzb->zb_asize / tzb->zb_count),
2311fa9e4066Sahrens 	    (double)tzb->zb_lsize / tzb->zb_asize);
2312b24ab676SJeff Bonwick 	(void) printf("\tbp deduped:    %10llu    ref>1:"
2313b24ab676SJeff Bonwick 	    " %6llu   deduplication: %6.2f\n",
2314b24ab676SJeff Bonwick 	    (u_longlong_t)zcb.zcb_dedup_asize,
2315b24ab676SJeff Bonwick 	    (u_longlong_t)zcb.zcb_dedup_blocks,
2316b24ab676SJeff Bonwick 	    (double)zcb.zcb_dedup_asize / tzb->zb_asize + 1.0);
2317b24ab676SJeff Bonwick 	(void) printf("\tSPA allocated: %10llu     used: %5.2f%%\n",
2318b24ab676SJeff Bonwick 	    (u_longlong_t)norm_alloc, 100.0 * norm_alloc / norm_space);
2319fa9e4066Sahrens 
2320fa9e4066Sahrens 	if (dump_opt['b'] >= 2) {
2321fa9e4066Sahrens 		int l, t, level;
2322fa9e4066Sahrens 		(void) printf("\nBlocks\tLSIZE\tPSIZE\tASIZE"
2323fa9e4066Sahrens 		    "\t  avg\t comp\t%%Total\tType\n");
2324fa9e4066Sahrens 
2325b24ab676SJeff Bonwick 		for (t = 0; t <= ZDB_OT_TOTAL; t++) {
23263f9d6ad7SLin Ling 			char csize[32], lsize[32], psize[32], asize[32];
23273f9d6ad7SLin Ling 			char avg[32];
2328fa9e4066Sahrens 			char *typename;
2329fa9e4066Sahrens 
2330b24ab676SJeff Bonwick 			if (t < DMU_OT_NUMTYPES)
2331b24ab676SJeff Bonwick 				typename = dmu_ot[t].ot_name;
2332b24ab676SJeff Bonwick 			else
2333b24ab676SJeff Bonwick 				typename = zdb_ot_extname[t - DMU_OT_NUMTYPES];
2334fa9e4066Sahrens 
2335fa9e4066Sahrens 			if (zcb.zcb_type[ZB_TOTAL][t].zb_asize == 0) {
2336fa9e4066Sahrens 				(void) printf("%6s\t%5s\t%5s\t%5s"
2337fa9e4066Sahrens 				    "\t%5s\t%5s\t%6s\t%s\n",
2338fa9e4066Sahrens 				    "-",
2339fa9e4066Sahrens 				    "-",
2340fa9e4066Sahrens 				    "-",
2341fa9e4066Sahrens 				    "-",
2342fa9e4066Sahrens 				    "-",
2343fa9e4066Sahrens 				    "-",
2344fa9e4066Sahrens 				    "-",
2345fa9e4066Sahrens 				    typename);
2346fa9e4066Sahrens 				continue;
2347fa9e4066Sahrens 			}
2348fa9e4066Sahrens 
2349fa9e4066Sahrens 			for (l = ZB_TOTAL - 1; l >= -1; l--) {
2350fa9e4066Sahrens 				level = (l == -1 ? ZB_TOTAL : l);
2351fa9e4066Sahrens 				zb = &zcb.zcb_type[level][t];
2352fa9e4066Sahrens 
2353fa9e4066Sahrens 				if (zb->zb_asize == 0)
2354fa9e4066Sahrens 					continue;
2355fa9e4066Sahrens 
2356fa9e4066Sahrens 				if (dump_opt['b'] < 3 && level != ZB_TOTAL)
2357fa9e4066Sahrens 					continue;
2358fa9e4066Sahrens 
2359fa9e4066Sahrens 				if (level == 0 && zb->zb_asize ==
2360fa9e4066Sahrens 				    zcb.zcb_type[ZB_TOTAL][t].zb_asize)
2361fa9e4066Sahrens 					continue;
2362fa9e4066Sahrens 
23633f9d6ad7SLin Ling 				zdb_nicenum(zb->zb_count, csize);
23643f9d6ad7SLin Ling 				zdb_nicenum(zb->zb_lsize, lsize);
23653f9d6ad7SLin Ling 				zdb_nicenum(zb->zb_psize, psize);
23663f9d6ad7SLin Ling 				zdb_nicenum(zb->zb_asize, asize);
23673f9d6ad7SLin Ling 				zdb_nicenum(zb->zb_asize / zb->zb_count, avg);
2368fa9e4066Sahrens 
2369fa9e4066Sahrens 				(void) printf("%6s\t%5s\t%5s\t%5s\t%5s"
2370fa9e4066Sahrens 				    "\t%5.2f\t%6.2f\t",
2371fa9e4066Sahrens 				    csize, lsize, psize, asize, avg,
2372fa9e4066Sahrens 				    (double)zb->zb_lsize / zb->zb_psize,
2373fa9e4066Sahrens 				    100.0 * zb->zb_asize / tzb->zb_asize);
2374fa9e4066Sahrens 
2375fa9e4066Sahrens 				if (level == ZB_TOTAL)
2376fa9e4066Sahrens 					(void) printf("%s\n", typename);
2377fa9e4066Sahrens 				else
2378fa9e4066Sahrens 					(void) printf("    L%d %s\n",
2379fa9e4066Sahrens 					    level, typename);
2380fa9e4066Sahrens 			}
2381fa9e4066Sahrens 		}
2382fa9e4066Sahrens 	}
2383fa9e4066Sahrens 
2384fa9e4066Sahrens 	(void) printf("\n");
2385fa9e4066Sahrens 
2386fa9e4066Sahrens 	if (leaks)
2387fa9e4066Sahrens 		return (2);
2388fa9e4066Sahrens 
2389fa9e4066Sahrens 	if (zcb.zcb_haderrors)
2390fa9e4066Sahrens 		return (3);
2391fa9e4066Sahrens 
2392fa9e4066Sahrens 	return (0);
2393fa9e4066Sahrens }
2394fa9e4066Sahrens 
2395b24ab676SJeff Bonwick typedef struct zdb_ddt_entry {
2396b24ab676SJeff Bonwick 	ddt_key_t	zdde_key;
2397b24ab676SJeff Bonwick 	uint64_t	zdde_ref_blocks;
2398b24ab676SJeff Bonwick 	uint64_t	zdde_ref_lsize;
2399b24ab676SJeff Bonwick 	uint64_t	zdde_ref_psize;
2400b24ab676SJeff Bonwick 	uint64_t	zdde_ref_dsize;
2401b24ab676SJeff Bonwick 	avl_node_t	zdde_node;
2402b24ab676SJeff Bonwick } zdb_ddt_entry_t;
2403b24ab676SJeff Bonwick 
2404b24ab676SJeff Bonwick /* ARGSUSED */
2405b24ab676SJeff Bonwick static int
2406b24ab676SJeff Bonwick zdb_ddt_add_cb(spa_t *spa, zilog_t *zilog, const blkptr_t *bp,
24073f9d6ad7SLin Ling     arc_buf_t *pbuf, const zbookmark_t *zb, const dnode_phys_t *dnp, void *arg)
2408b24ab676SJeff Bonwick {
2409b24ab676SJeff Bonwick 	avl_tree_t *t = arg;
2410b24ab676SJeff Bonwick 	avl_index_t where;
2411b24ab676SJeff Bonwick 	zdb_ddt_entry_t *zdde, zdde_search;
2412b24ab676SJeff Bonwick 
2413b24ab676SJeff Bonwick 	if (bp == NULL)
2414b24ab676SJeff Bonwick 		return (0);
2415b24ab676SJeff Bonwick 
2416b24ab676SJeff Bonwick 	if (dump_opt['S'] > 1 && zb->zb_level == ZB_ROOT_LEVEL) {
2417b24ab676SJeff Bonwick 		(void) printf("traversing objset %llu, %llu objects, "
2418b24ab676SJeff Bonwick 		    "%lu blocks so far\n",
2419b24ab676SJeff Bonwick 		    (u_longlong_t)zb->zb_objset,
2420b24ab676SJeff Bonwick 		    (u_longlong_t)bp->blk_fill,
2421b24ab676SJeff Bonwick 		    avl_numnodes(t));
2422b24ab676SJeff Bonwick 	}
2423b24ab676SJeff Bonwick 
2424bbfd46c4SJeff Bonwick 	if (BP_IS_HOLE(bp) || BP_GET_CHECKSUM(bp) == ZIO_CHECKSUM_OFF ||
2425*ad135b5dSChristopher Siden 	    BP_GET_LEVEL(bp) > 0 || DMU_OT_IS_METADATA(BP_GET_TYPE(bp)))
2426b24ab676SJeff Bonwick 		return (0);
2427b24ab676SJeff Bonwick 
2428b24ab676SJeff Bonwick 	ddt_key_fill(&zdde_search.zdde_key, bp);
2429b24ab676SJeff Bonwick 
2430b24ab676SJeff Bonwick 	zdde = avl_find(t, &zdde_search, &where);
2431b24ab676SJeff Bonwick 
2432b24ab676SJeff Bonwick 	if (zdde == NULL) {
2433b24ab676SJeff Bonwick 		zdde = umem_zalloc(sizeof (*zdde), UMEM_NOFAIL);
2434b24ab676SJeff Bonwick 		zdde->zdde_key = zdde_search.zdde_key;
2435b24ab676SJeff Bonwick 		avl_insert(t, zdde, where);
2436b24ab676SJeff Bonwick 	}
2437b24ab676SJeff Bonwick 
2438b24ab676SJeff Bonwick 	zdde->zdde_ref_blocks += 1;
2439b24ab676SJeff Bonwick 	zdde->zdde_ref_lsize += BP_GET_LSIZE(bp);
2440b24ab676SJeff Bonwick 	zdde->zdde_ref_psize += BP_GET_PSIZE(bp);
2441b24ab676SJeff Bonwick 	zdde->zdde_ref_dsize += bp_get_dsize_sync(spa, bp);
2442b24ab676SJeff Bonwick 
2443b24ab676SJeff Bonwick 	return (0);
2444b24ab676SJeff Bonwick }
2445b24ab676SJeff Bonwick 
2446b24ab676SJeff Bonwick static void
2447b24ab676SJeff Bonwick dump_simulated_ddt(spa_t *spa)
2448b24ab676SJeff Bonwick {
2449b24ab676SJeff Bonwick 	avl_tree_t t;
2450b24ab676SJeff Bonwick 	void *cookie = NULL;
2451b24ab676SJeff Bonwick 	zdb_ddt_entry_t *zdde;
2452b24ab676SJeff Bonwick 	ddt_histogram_t ddh_total = { 0 };
2453b24ab676SJeff Bonwick 	ddt_stat_t dds_total = { 0 };
2454b24ab676SJeff Bonwick 
2455b24ab676SJeff Bonwick 	avl_create(&t, ddt_entry_compare,
2456b24ab676SJeff Bonwick 	    sizeof (zdb_ddt_entry_t), offsetof(zdb_ddt_entry_t, zdde_node));
2457b24ab676SJeff Bonwick 
2458b24ab676SJeff Bonwick 	spa_config_enter(spa, SCL_CONFIG, FTAG, RW_READER);
2459b24ab676SJeff Bonwick 
2460bbfd46c4SJeff Bonwick 	(void) traverse_pool(spa, 0, TRAVERSE_PRE | TRAVERSE_PREFETCH_METADATA,
2461bbfd46c4SJeff Bonwick 	    zdb_ddt_add_cb, &t);
2462b24ab676SJeff Bonwick 
2463b24ab676SJeff Bonwick 	spa_config_exit(spa, SCL_CONFIG, FTAG);
2464b24ab676SJeff Bonwick 
2465b24ab676SJeff Bonwick 	while ((zdde = avl_destroy_nodes(&t, &cookie)) != NULL) {
2466b24ab676SJeff Bonwick 		ddt_stat_t dds;
2467b24ab676SJeff Bonwick 		uint64_t refcnt = zdde->zdde_ref_blocks;
2468b24ab676SJeff Bonwick 		ASSERT(refcnt != 0);
2469b24ab676SJeff Bonwick 
2470b24ab676SJeff Bonwick 		dds.dds_blocks = zdde->zdde_ref_blocks / refcnt;
2471b24ab676SJeff Bonwick 		dds.dds_lsize = zdde->zdde_ref_lsize / refcnt;
2472b24ab676SJeff Bonwick 		dds.dds_psize = zdde->zdde_ref_psize / refcnt;
2473b24ab676SJeff Bonwick 		dds.dds_dsize = zdde->zdde_ref_dsize / refcnt;
2474b24ab676SJeff Bonwick 
2475b24ab676SJeff Bonwick 		dds.dds_ref_blocks = zdde->zdde_ref_blocks;
2476b24ab676SJeff Bonwick 		dds.dds_ref_lsize = zdde->zdde_ref_lsize;
2477b24ab676SJeff Bonwick 		dds.dds_ref_psize = zdde->zdde_ref_psize;
2478b24ab676SJeff Bonwick 		dds.dds_ref_dsize = zdde->zdde_ref_dsize;
2479b24ab676SJeff Bonwick 
2480b24ab676SJeff Bonwick 		ddt_stat_add(&ddh_total.ddh_stat[highbit(refcnt) - 1], &dds, 0);
2481b24ab676SJeff Bonwick 
2482b24ab676SJeff Bonwick 		umem_free(zdde, sizeof (*zdde));
2483b24ab676SJeff Bonwick 	}
2484b24ab676SJeff Bonwick 
2485b24ab676SJeff Bonwick 	avl_destroy(&t);
2486b24ab676SJeff Bonwick 
2487b24ab676SJeff Bonwick 	ddt_histogram_stat(&dds_total, &ddh_total);
2488b24ab676SJeff Bonwick 
2489b24ab676SJeff Bonwick 	(void) printf("Simulated DDT histogram:\n");
2490b24ab676SJeff Bonwick 
24919eb19f4dSGeorge Wilson 	zpool_dump_ddt(&dds_total, &ddh_total);
2492b24ab676SJeff Bonwick 
2493b24ab676SJeff Bonwick 	dump_dedup_ratio(&dds_total);
2494b24ab676SJeff Bonwick }
2495b24ab676SJeff Bonwick 
2496fa9e4066Sahrens static void
2497fa9e4066Sahrens dump_zpool(spa_t *spa)
2498fa9e4066Sahrens {
2499fa9e4066Sahrens 	dsl_pool_t *dp = spa_get_dsl(spa);
2500fa9e4066Sahrens 	int rc = 0;
2501fa9e4066Sahrens 
2502b24ab676SJeff Bonwick 	if (dump_opt['S']) {
2503b24ab676SJeff Bonwick 		dump_simulated_ddt(spa);
2504b24ab676SJeff Bonwick 		return;
2505b24ab676SJeff Bonwick 	}
2506b24ab676SJeff Bonwick 
250707428bdfSVictor Latushkin 	if (!dump_opt['e'] && dump_opt['C'] > 1) {
250807428bdfSVictor Latushkin 		(void) printf("\nCached configuration:\n");
250907428bdfSVictor Latushkin 		dump_nvlist(spa->spa_config, 8);
251007428bdfSVictor Latushkin 	}
251107428bdfSVictor Latushkin 
251207428bdfSVictor Latushkin 	if (dump_opt['C'])
251307428bdfSVictor Latushkin 		dump_config(spa);
251407428bdfSVictor Latushkin 
2515fa9e4066Sahrens 	if (dump_opt['u'])
251653b9a4a9SVictor Latushkin 		dump_uberblock(&spa->spa_uberblock, "\nUberblock:\n", "\n");
2517fa9e4066Sahrens 
2518b24ab676SJeff Bonwick 	if (dump_opt['D'])
2519b24ab676SJeff Bonwick 		dump_all_ddts(spa);
2520b24ab676SJeff Bonwick 
252187219db7SVictor Latushkin 	if (dump_opt['d'] > 2 || dump_opt['m'])
252287219db7SVictor Latushkin 		dump_metaslabs(spa);
252387219db7SVictor Latushkin 
252487219db7SVictor Latushkin 	if (dump_opt['d'] || dump_opt['i']) {
2525fa9e4066Sahrens 		dump_dir(dp->dp_meta_objset);
2526fa9e4066Sahrens 		if (dump_opt['d'] >= 3) {
2527cde58dbcSMatthew Ahrens 			dump_bpobj(&spa->spa_deferred_bpobj, "Deferred frees");
2528cde58dbcSMatthew Ahrens 			if (spa_version(spa) >= SPA_VERSION_DEADLISTS) {
2529cde58dbcSMatthew Ahrens 				dump_bpobj(&spa->spa_dsl_pool->dp_free_bpobj,
2530*ad135b5dSChristopher Siden 				    "Pool snapshot frees");
2531*ad135b5dSChristopher Siden 			}
2532*ad135b5dSChristopher Siden 
2533*ad135b5dSChristopher Siden 			if (spa_feature_is_active(spa,
2534*ad135b5dSChristopher Siden 			    &spa_feature_table[SPA_FEATURE_ASYNC_DESTROY])) {
2535*ad135b5dSChristopher Siden 				dump_bptree(spa->spa_meta_objset,
2536*ad135b5dSChristopher Siden 				    spa->spa_dsl_pool->dp_bptree_obj,
2537*ad135b5dSChristopher Siden 				    "Pool dataset frees");
2538cde58dbcSMatthew Ahrens 			}
2539fa9e4066Sahrens 			dump_dtl(spa->spa_root_vdev, 0);
2540fa9e4066Sahrens 		}
254107428bdfSVictor Latushkin 		(void) dmu_objset_find(spa_name(spa), dump_one_dir,
254207428bdfSVictor Latushkin 		    NULL, DS_FIND_SNAPSHOTS | DS_FIND_CHILDREN);
2543fa9e4066Sahrens 	}
2544b24ab676SJeff Bonwick 	if (dump_opt['b'] || dump_opt['c'])
2545fa9e4066Sahrens 		rc = dump_block_stats(spa);
2546fa9e4066Sahrens 
2547fa9e4066Sahrens 	if (dump_opt['s'])
2548fa9e4066Sahrens 		show_pool_stats(spa);
2549fa9e4066Sahrens 
25508f18d1faSGeorge Wilson 	if (dump_opt['h'])
25518f18d1faSGeorge Wilson 		dump_history(spa);
25528f18d1faSGeorge Wilson 
2553fa9e4066Sahrens 	if (rc != 0)
2554fa9e4066Sahrens 		exit(rc);
2555fa9e4066Sahrens }
2556fa9e4066Sahrens 
255744cd46caSbillm #define	ZDB_FLAG_CHECKSUM	0x0001
255844cd46caSbillm #define	ZDB_FLAG_DECOMPRESS	0x0002
255944cd46caSbillm #define	ZDB_FLAG_BSWAP		0x0004
256044cd46caSbillm #define	ZDB_FLAG_GBH		0x0008
256144cd46caSbillm #define	ZDB_FLAG_INDIRECT	0x0010
256244cd46caSbillm #define	ZDB_FLAG_PHYS		0x0020
256344cd46caSbillm #define	ZDB_FLAG_RAW		0x0040
256444cd46caSbillm #define	ZDB_FLAG_PRINT_BLKPTR	0x0080
256544cd46caSbillm 
256644cd46caSbillm int flagbits[256];
256744cd46caSbillm 
256844cd46caSbillm static void
256944cd46caSbillm zdb_print_blkptr(blkptr_t *bp, int flags)
257044cd46caSbillm {
2571b24ab676SJeff Bonwick 	char blkbuf[BP_SPRINTF_LEN];
257244cd46caSbillm 
257344cd46caSbillm 	if (flags & ZDB_FLAG_BSWAP)
257444cd46caSbillm 		byteswap_uint64_array((void *)bp, sizeof (blkptr_t));
2575b24ab676SJeff Bonwick 
2576b24ab676SJeff Bonwick 	sprintf_blkptr(blkbuf, bp);
2577b24ab676SJeff Bonwick 	(void) printf("%s\n", blkbuf);
257844cd46caSbillm }
257944cd46caSbillm 
258044cd46caSbillm static void
258144cd46caSbillm zdb_dump_indirect(blkptr_t *bp, int nbps, int flags)
258244cd46caSbillm {
258344cd46caSbillm 	int i;
258444cd46caSbillm 
258544cd46caSbillm 	for (i = 0; i < nbps; i++)
258644cd46caSbillm 		zdb_print_blkptr(&bp[i], flags);
258744cd46caSbillm }
258844cd46caSbillm 
258944cd46caSbillm static void
259044cd46caSbillm zdb_dump_gbh(void *buf, int flags)
259144cd46caSbillm {
259244cd46caSbillm 	zdb_dump_indirect((blkptr_t *)buf, SPA_GBH_NBLKPTRS, flags);
259344cd46caSbillm }
259444cd46caSbillm 
259544cd46caSbillm static void
259644cd46caSbillm zdb_dump_block_raw(void *buf, uint64_t size, int flags)
259744cd46caSbillm {
259844cd46caSbillm 	if (flags & ZDB_FLAG_BSWAP)
259944cd46caSbillm 		byteswap_uint64_array(buf, size);
2600b24ab676SJeff Bonwick 	(void) write(1, buf, size);
260144cd46caSbillm }
260244cd46caSbillm 
260344cd46caSbillm static void
260444cd46caSbillm zdb_dump_block(char *label, void *buf, uint64_t size, int flags)
260544cd46caSbillm {
260644cd46caSbillm 	uint64_t *d = (uint64_t *)buf;
260744cd46caSbillm 	int nwords = size / sizeof (uint64_t);
260844cd46caSbillm 	int do_bswap = !!(flags & ZDB_FLAG_BSWAP);
260944cd46caSbillm 	int i, j;
261044cd46caSbillm 	char *hdr, *c;
261144cd46caSbillm 
261244cd46caSbillm 
261344cd46caSbillm 	if (do_bswap)
261444cd46caSbillm 		hdr = " 7 6 5 4 3 2 1 0   f e d c b a 9 8";
261544cd46caSbillm 	else
261644cd46caSbillm 		hdr = " 0 1 2 3 4 5 6 7   8 9 a b c d e f";
261744cd46caSbillm 
261844cd46caSbillm 	(void) printf("\n%s\n%6s   %s  0123456789abcdef\n", label, "", hdr);
261944cd46caSbillm 
262044cd46caSbillm 	for (i = 0; i < nwords; i += 2) {
262144cd46caSbillm 		(void) printf("%06llx:  %016llx  %016llx  ",
262244cd46caSbillm 		    (u_longlong_t)(i * sizeof (uint64_t)),
262344cd46caSbillm 		    (u_longlong_t)(do_bswap ? BSWAP_64(d[i]) : d[i]),
262444cd46caSbillm 		    (u_longlong_t)(do_bswap ? BSWAP_64(d[i + 1]) : d[i + 1]));
262544cd46caSbillm 
262644cd46caSbillm 		c = (char *)&d[i];
262744cd46caSbillm 		for (j = 0; j < 2 * sizeof (uint64_t); j++)
262844cd46caSbillm 			(void) printf("%c", isprint(c[j]) ? c[j] : '.');
262944cd46caSbillm 		(void) printf("\n");
263044cd46caSbillm 	}
263144cd46caSbillm }
263244cd46caSbillm 
263344cd46caSbillm /*
263444cd46caSbillm  * There are two acceptable formats:
263544cd46caSbillm  *	leaf_name	  - For example: c1t0d0 or /tmp/ztest.0a
263644cd46caSbillm  *	child[.child]*    - For example: 0.1.1
263744cd46caSbillm  *
263844cd46caSbillm  * The second form can be used to specify arbitrary vdevs anywhere
263944cd46caSbillm  * in the heirarchy.  For example, in a pool with a mirror of
264044cd46caSbillm  * RAID-Zs, you can specify either RAID-Z vdev with 0.0 or 0.1 .
264144cd46caSbillm  */
264244cd46caSbillm static vdev_t *
264344cd46caSbillm zdb_vdev_lookup(vdev_t *vdev, char *path)
264444cd46caSbillm {
264544cd46caSbillm 	char *s, *p, *q;
264644cd46caSbillm 	int i;
264744cd46caSbillm 
264844cd46caSbillm 	if (vdev == NULL)
264944cd46caSbillm 		return (NULL);
265044cd46caSbillm 
265144cd46caSbillm 	/* First, assume the x.x.x.x format */
265244cd46caSbillm 	i = (int)strtoul(path, &s, 10);
265344cd46caSbillm 	if (s == path || (s && *s != '.' && *s != '\0'))
265444cd46caSbillm 		goto name;
265544cd46caSbillm 	if (i < 0 || i >= vdev->vdev_children)
265644cd46caSbillm 		return (NULL);
265744cd46caSbillm 
265844cd46caSbillm 	vdev = vdev->vdev_child[i];
265944cd46caSbillm 	if (*s == '\0')
266044cd46caSbillm 		return (vdev);
266144cd46caSbillm 	return (zdb_vdev_lookup(vdev, s+1));
266244cd46caSbillm 
266344cd46caSbillm name:
266444cd46caSbillm 	for (i = 0; i < vdev->vdev_children; i++) {
266544cd46caSbillm 		vdev_t *vc = vdev->vdev_child[i];
266644cd46caSbillm 
266744cd46caSbillm 		if (vc->vdev_path == NULL) {
266844cd46caSbillm 			vc = zdb_vdev_lookup(vc, path);
266944cd46caSbillm 			if (vc == NULL)
267044cd46caSbillm 				continue;
267144cd46caSbillm 			else
267244cd46caSbillm 				return (vc);
267344cd46caSbillm 		}
267444cd46caSbillm 
267544cd46caSbillm 		p = strrchr(vc->vdev_path, '/');
267644cd46caSbillm 		p = p ? p + 1 : vc->vdev_path;
267744cd46caSbillm 		q = &vc->vdev_path[strlen(vc->vdev_path) - 2];
267844cd46caSbillm 
267944cd46caSbillm 		if (strcmp(vc->vdev_path, path) == 0)
268044cd46caSbillm 			return (vc);
268144cd46caSbillm 		if (strcmp(p, path) == 0)
268244cd46caSbillm 			return (vc);
268344cd46caSbillm 		if (strcmp(q, "s0") == 0 && strncmp(p, path, q - p) == 0)
268444cd46caSbillm 			return (vc);
268544cd46caSbillm 	}
268644cd46caSbillm 
268744cd46caSbillm 	return (NULL);
268844cd46caSbillm }
268944cd46caSbillm 
269044cd46caSbillm /*
269144cd46caSbillm  * Read a block from a pool and print it out.  The syntax of the
269244cd46caSbillm  * block descriptor is:
269344cd46caSbillm  *
269444cd46caSbillm  *	pool:vdev_specifier:offset:size[:flags]
269544cd46caSbillm  *
269644cd46caSbillm  *	pool           - The name of the pool you wish to read from
269744cd46caSbillm  *	vdev_specifier - Which vdev (see comment for zdb_vdev_lookup)
269844cd46caSbillm  *	offset         - offset, in hex, in bytes
269944cd46caSbillm  *	size           - Amount of data to read, in hex, in bytes
270044cd46caSbillm  *	flags          - A string of characters specifying options
270144cd46caSbillm  *		 b: Decode a blkptr at given offset within block
270244cd46caSbillm  *		*c: Calculate and display checksums
2703b24ab676SJeff Bonwick  *		 d: Decompress data before dumping
270444cd46caSbillm  *		 e: Byteswap data before dumping
2705b24ab676SJeff Bonwick  *		 g: Display data as a gang block header
2706b24ab676SJeff Bonwick  *		 i: Display as an indirect block
270744cd46caSbillm  *		 p: Do I/O to physical offset
270844cd46caSbillm  *		 r: Dump raw data to stdout
270944cd46caSbillm  *
271044cd46caSbillm  *              * = not yet implemented
271144cd46caSbillm  */
271244cd46caSbillm static void
271307428bdfSVictor Latushkin zdb_read_block(char *thing, spa_t *spa)
271444cd46caSbillm {
2715b24ab676SJeff Bonwick 	blkptr_t blk, *bp = &blk;
2716b24ab676SJeff Bonwick 	dva_t *dva = bp->blk_dva;
271744cd46caSbillm 	int flags = 0;
2718b24ab676SJeff Bonwick 	uint64_t offset = 0, size = 0, psize = 0, lsize = 0, blkptr_offset = 0;
271944cd46caSbillm 	zio_t *zio;
272044cd46caSbillm 	vdev_t *vd;
2721b24ab676SJeff Bonwick 	void *pbuf, *lbuf, *buf;
272207428bdfSVictor Latushkin 	char *s, *p, *dup, *vdev, *flagstr;
2723b24ab676SJeff Bonwick 	int i, error;
272444cd46caSbillm 
272544cd46caSbillm 	dup = strdup(thing);
272644cd46caSbillm 	s = strtok(dup, ":");
272744cd46caSbillm 	vdev = s ? s : "";
272844cd46caSbillm 	s = strtok(NULL, ":");
272944cd46caSbillm 	offset = strtoull(s ? s : "", NULL, 16);
273044cd46caSbillm 	s = strtok(NULL, ":");
273144cd46caSbillm 	size = strtoull(s ? s : "", NULL, 16);
273244cd46caSbillm 	s = strtok(NULL, ":");
273344cd46caSbillm 	flagstr = s ? s : "";
273444cd46caSbillm 
273544cd46caSbillm 	s = NULL;
273644cd46caSbillm 	if (size == 0)
273744cd46caSbillm 		s = "size must not be zero";
273844cd46caSbillm 	if (!IS_P2ALIGNED(size, DEV_BSIZE))
273944cd46caSbillm 		s = "size must be a multiple of sector size";
274044cd46caSbillm 	if (!IS_P2ALIGNED(offset, DEV_BSIZE))
274144cd46caSbillm 		s = "offset must be a multiple of sector size";
274244cd46caSbillm 	if (s) {
274344cd46caSbillm 		(void) printf("Invalid block specifier: %s  - %s\n", thing, s);
274444cd46caSbillm 		free(dup);
274544cd46caSbillm 		return;
274644cd46caSbillm 	}
274744cd46caSbillm 
274844cd46caSbillm 	for (s = strtok(flagstr, ":"); s; s = strtok(NULL, ":")) {
274944cd46caSbillm 		for (i = 0; flagstr[i]; i++) {
27505ad82045Snd150628 			int bit = flagbits[(uchar_t)flagstr[i]];
275144cd46caSbillm 
275244cd46caSbillm 			if (bit == 0) {
275344cd46caSbillm 				(void) printf("***Invalid flag: %c\n",
275444cd46caSbillm 				    flagstr[i]);
275544cd46caSbillm 				continue;
275644cd46caSbillm 			}
275744cd46caSbillm 			flags |= bit;
275844cd46caSbillm 
275944cd46caSbillm 			/* If it's not something with an argument, keep going */
2760b24ab676SJeff Bonwick 			if ((bit & (ZDB_FLAG_CHECKSUM |
276144cd46caSbillm 			    ZDB_FLAG_PRINT_BLKPTR)) == 0)
276244cd46caSbillm 				continue;
276344cd46caSbillm 
276444cd46caSbillm 			p = &flagstr[i + 1];
276544cd46caSbillm 			if (bit == ZDB_FLAG_PRINT_BLKPTR)
276644cd46caSbillm 				blkptr_offset = strtoull(p, &p, 16);
276744cd46caSbillm 			if (*p != ':' && *p != '\0') {
276844cd46caSbillm 				(void) printf("***Invalid flag arg: '%s'\n", s);
276944cd46caSbillm 				free(dup);
277044cd46caSbillm 				return;
277144cd46caSbillm 			}
277244cd46caSbillm 		}
277344cd46caSbillm 	}
277444cd46caSbillm 
277544cd46caSbillm 	vd = zdb_vdev_lookup(spa->spa_root_vdev, vdev);
277644cd46caSbillm 	if (vd == NULL) {
277744cd46caSbillm 		(void) printf("***Invalid vdev: %s\n", vdev);
277844cd46caSbillm 		free(dup);
277944cd46caSbillm 		return;
278044cd46caSbillm 	} else {
278144cd46caSbillm 		if (vd->vdev_path)
2782b24ab676SJeff Bonwick 			(void) fprintf(stderr, "Found vdev: %s\n",
2783b24ab676SJeff Bonwick 			    vd->vdev_path);
278444cd46caSbillm 		else
2785b24ab676SJeff Bonwick 			(void) fprintf(stderr, "Found vdev type: %s\n",
278644cd46caSbillm 			    vd->vdev_ops->vdev_op_type);
278744cd46caSbillm 	}
278844cd46caSbillm 
2789b24ab676SJeff Bonwick 	psize = size;
2790b24ab676SJeff Bonwick 	lsize = size;
279144cd46caSbillm 
2792b24ab676SJeff Bonwick 	pbuf = umem_alloc(SPA_MAXBLOCKSIZE, UMEM_NOFAIL);
2793b24ab676SJeff Bonwick 	lbuf = umem_alloc(SPA_MAXBLOCKSIZE, UMEM_NOFAIL);
2794b24ab676SJeff Bonwick 
2795b24ab676SJeff Bonwick 	BP_ZERO(bp);
2796b24ab676SJeff Bonwick 
2797b24ab676SJeff Bonwick 	DVA_SET_VDEV(&dva[0], vd->vdev_id);
2798b24ab676SJeff Bonwick 	DVA_SET_OFFSET(&dva[0], offset);
2799b24ab676SJeff Bonwick 	DVA_SET_GANG(&dva[0], !!(flags & ZDB_FLAG_GBH));
2800b24ab676SJeff Bonwick 	DVA_SET_ASIZE(&dva[0], vdev_psize_to_asize(vd, psize));
2801b24ab676SJeff Bonwick 
2802b24ab676SJeff Bonwick 	BP_SET_BIRTH(bp, TXG_INITIAL, TXG_INITIAL);
2803b24ab676SJeff Bonwick 
2804b24ab676SJeff Bonwick 	BP_SET_LSIZE(bp, lsize);
2805b24ab676SJeff Bonwick 	BP_SET_PSIZE(bp, psize);
2806b24ab676SJeff Bonwick 	BP_SET_COMPRESS(bp, ZIO_COMPRESS_OFF);
2807b24ab676SJeff Bonwick 	BP_SET_CHECKSUM(bp, ZIO_CHECKSUM_OFF);
2808b24ab676SJeff Bonwick 	BP_SET_TYPE(bp, DMU_OT_NONE);
2809b24ab676SJeff Bonwick 	BP_SET_LEVEL(bp, 0);
2810b24ab676SJeff Bonwick 	BP_SET_DEDUP(bp, 0);
2811b24ab676SJeff Bonwick 	BP_SET_BYTEORDER(bp, ZFS_HOST_BYTEORDER);
281244cd46caSbillm 
2813e14bb325SJeff Bonwick 	spa_config_enter(spa, SCL_STATE, FTAG, RW_READER);
281444cd46caSbillm 	zio = zio_root(spa, NULL, NULL, 0);
2815b24ab676SJeff Bonwick 
2816b24ab676SJeff Bonwick 	if (vd == vd->vdev_top) {
2817b24ab676SJeff Bonwick 		/*
2818b24ab676SJeff Bonwick 		 * Treat this as a normal block read.
2819b24ab676SJeff Bonwick 		 */
2820b24ab676SJeff Bonwick 		zio_nowait(zio_read(zio, spa, bp, pbuf, psize, NULL, NULL,
2821b24ab676SJeff Bonwick 		    ZIO_PRIORITY_SYNC_READ,
2822b24ab676SJeff Bonwick 		    ZIO_FLAG_CANFAIL | ZIO_FLAG_RAW, NULL));
2823b24ab676SJeff Bonwick 	} else {
2824b24ab676SJeff Bonwick 		/*
2825b24ab676SJeff Bonwick 		 * Treat this as a vdev child I/O.
2826b24ab676SJeff Bonwick 		 */
2827b24ab676SJeff Bonwick 		zio_nowait(zio_vdev_child_io(zio, bp, vd, offset, pbuf, psize,
2828b24ab676SJeff Bonwick 		    ZIO_TYPE_READ, ZIO_PRIORITY_SYNC_READ,
2829b24ab676SJeff Bonwick 		    ZIO_FLAG_DONT_CACHE | ZIO_FLAG_DONT_QUEUE |
2830b24ab676SJeff Bonwick 		    ZIO_FLAG_DONT_PROPAGATE | ZIO_FLAG_DONT_RETRY |
2831b24ab676SJeff Bonwick 		    ZIO_FLAG_CANFAIL | ZIO_FLAG_RAW, NULL, NULL));
2832b24ab676SJeff Bonwick 	}
2833b24ab676SJeff Bonwick 
283444cd46caSbillm 	error = zio_wait(zio);
2835e14bb325SJeff Bonwick 	spa_config_exit(spa, SCL_STATE, FTAG);
283644cd46caSbillm 
283744cd46caSbillm 	if (error) {
283844cd46caSbillm 		(void) printf("Read of %s failed, error: %d\n", thing, error);
283944cd46caSbillm 		goto out;
284044cd46caSbillm 	}
284144cd46caSbillm 
2842b24ab676SJeff Bonwick 	if (flags & ZDB_FLAG_DECOMPRESS) {
2843b24ab676SJeff Bonwick 		/*
2844b24ab676SJeff Bonwick 		 * We don't know how the data was compressed, so just try
2845b24ab676SJeff Bonwick 		 * every decompress function at every inflated blocksize.
2846b24ab676SJeff Bonwick 		 */
2847b24ab676SJeff Bonwick 		enum zio_compress c;
2848b24ab676SJeff Bonwick 		void *pbuf2 = umem_alloc(SPA_MAXBLOCKSIZE, UMEM_NOFAIL);
2849b24ab676SJeff Bonwick 		void *lbuf2 = umem_alloc(SPA_MAXBLOCKSIZE, UMEM_NOFAIL);
2850b24ab676SJeff Bonwick 
2851b24ab676SJeff Bonwick 		bcopy(pbuf, pbuf2, psize);
2852b24ab676SJeff Bonwick 
2853b24ab676SJeff Bonwick 		VERIFY(random_get_pseudo_bytes((uint8_t *)pbuf + psize,
2854b24ab676SJeff Bonwick 		    SPA_MAXBLOCKSIZE - psize) == 0);
2855b24ab676SJeff Bonwick 
2856b24ab676SJeff Bonwick 		VERIFY(random_get_pseudo_bytes((uint8_t *)pbuf2 + psize,
2857b24ab676SJeff Bonwick 		    SPA_MAXBLOCKSIZE - psize) == 0);
2858b24ab676SJeff Bonwick 
2859b24ab676SJeff Bonwick 		for (lsize = SPA_MAXBLOCKSIZE; lsize > psize;
2860b24ab676SJeff Bonwick 		    lsize -= SPA_MINBLOCKSIZE) {
2861b24ab676SJeff Bonwick 			for (c = 0; c < ZIO_COMPRESS_FUNCTIONS; c++) {
2862b24ab676SJeff Bonwick 				if (zio_decompress_data(c, pbuf, lbuf,
2863b24ab676SJeff Bonwick 				    psize, lsize) == 0 &&
2864b24ab676SJeff Bonwick 				    zio_decompress_data(c, pbuf2, lbuf2,
2865b24ab676SJeff Bonwick 				    psize, lsize) == 0 &&
2866b24ab676SJeff Bonwick 				    bcmp(lbuf, lbuf2, lsize) == 0)
2867b24ab676SJeff Bonwick 					break;
2868b24ab676SJeff Bonwick 			}
2869b24ab676SJeff Bonwick 			if (c != ZIO_COMPRESS_FUNCTIONS)
2870b24ab676SJeff Bonwick 				break;
2871b24ab676SJeff Bonwick 			lsize -= SPA_MINBLOCKSIZE;
2872b24ab676SJeff Bonwick 		}
2873b24ab676SJeff Bonwick 
2874b24ab676SJeff Bonwick 		umem_free(pbuf2, SPA_MAXBLOCKSIZE);
2875b24ab676SJeff Bonwick 		umem_free(lbuf2, SPA_MAXBLOCKSIZE);
2876b24ab676SJeff Bonwick 
2877b24ab676SJeff Bonwick 		if (lsize <= psize) {
2878b24ab676SJeff Bonwick 			(void) printf("Decompress of %s failed\n", thing);
2879b24ab676SJeff Bonwick 			goto out;
2880b24ab676SJeff Bonwick 		}
2881b24ab676SJeff Bonwick 		buf = lbuf;
2882b24ab676SJeff Bonwick 		size = lsize;
2883b24ab676SJeff Bonwick 	} else {
2884b24ab676SJeff Bonwick 		buf = pbuf;
2885b24ab676SJeff Bonwick 		size = psize;
2886b24ab676SJeff Bonwick 	}
2887b24ab676SJeff Bonwick 
288844cd46caSbillm 	if (flags & ZDB_FLAG_PRINT_BLKPTR)
288944cd46caSbillm 		zdb_print_blkptr((blkptr_t *)(void *)
289044cd46caSbillm 		    ((uintptr_t)buf + (uintptr_t)blkptr_offset), flags);
289144cd46caSbillm 	else if (flags & ZDB_FLAG_RAW)
289244cd46caSbillm 		zdb_dump_block_raw(buf, size, flags);
289344cd46caSbillm 	else if (flags & ZDB_FLAG_INDIRECT)
289444cd46caSbillm 		zdb_dump_indirect((blkptr_t *)buf, size / sizeof (blkptr_t),
289544cd46caSbillm 		    flags);
289644cd46caSbillm 	else if (flags & ZDB_FLAG_GBH)
289744cd46caSbillm 		zdb_dump_gbh(buf, flags);
289844cd46caSbillm 	else
289944cd46caSbillm 		zdb_dump_block(thing, buf, size, flags);
290044cd46caSbillm 
290144cd46caSbillm out:
2902b24ab676SJeff Bonwick 	umem_free(pbuf, SPA_MAXBLOCKSIZE);
2903b24ab676SJeff Bonwick 	umem_free(lbuf, SPA_MAXBLOCKSIZE);
290444cd46caSbillm 	free(dup);
290544cd46caSbillm }
290644cd46caSbillm 
2907de6628f0Sck153898 static boolean_t
29083ad6c7f9SVictor Latushkin pool_match(nvlist_t *cfg, char *tgt)
2909de6628f0Sck153898 {
29103ad6c7f9SVictor Latushkin 	uint64_t v, guid = strtoull(tgt, NULL, 0);
2911de6628f0Sck153898 	char *s;
2912de6628f0Sck153898 
29133ad6c7f9SVictor Latushkin 	if (guid != 0) {
29143ad6c7f9SVictor Latushkin 		if (nvlist_lookup_uint64(cfg, ZPOOL_CONFIG_POOL_GUID, &v) == 0)
29153ad6c7f9SVictor Latushkin 			return (v == guid);
29163ad6c7f9SVictor Latushkin 	} else {
29173ad6c7f9SVictor Latushkin 		if (nvlist_lookup_string(cfg, ZPOOL_CONFIG_POOL_NAME, &s) == 0)
2918de6628f0Sck153898 			return (strcmp(s, tgt) == 0);
2919de6628f0Sck153898 	}
2920de6628f0Sck153898 	return (B_FALSE);
2921de6628f0Sck153898 }
2922de6628f0Sck153898 
29233ad6c7f9SVictor Latushkin static char *
29243ad6c7f9SVictor Latushkin find_zpool(char **target, nvlist_t **configp, int dirc, char **dirv)
2925de6628f0Sck153898 {
2926de6628f0Sck153898 	nvlist_t *pools;
2927de6628f0Sck153898 	nvlist_t *match = NULL;
29283ad6c7f9SVictor Latushkin 	char *name = NULL;
29293ad6c7f9SVictor Latushkin 	char *sepp = NULL;
29303ad6c7f9SVictor Latushkin 	char sep;
29313ad6c7f9SVictor Latushkin 	int count = 0;
2932d41c4376SMark J Musante 	importargs_t args = { 0 };
2933d41c4376SMark J Musante 
2934d41c4376SMark J Musante 	args.paths = dirc;
2935d41c4376SMark J Musante 	args.path = dirv;
2936d41c4376SMark J Musante 	args.can_be_active = B_TRUE;
2937de6628f0Sck153898 
29383ad6c7f9SVictor Latushkin 	if ((sepp = strpbrk(*target, "/@")) != NULL) {
29393ad6c7f9SVictor Latushkin 		sep = *sepp;
29403ad6c7f9SVictor Latushkin 		*sepp = '\0';
29413ad6c7f9SVictor Latushkin 	}
29423ad6c7f9SVictor Latushkin 
2943d41c4376SMark J Musante 	pools = zpool_search_import(g_zfs, &args);
2944de6628f0Sck153898 
2945de6628f0Sck153898 	if (pools != NULL) {
2946de6628f0Sck153898 		nvpair_t *elem = NULL;
2947de6628f0Sck153898 		while ((elem = nvlist_next_nvpair(pools, elem)) != NULL) {
2948de6628f0Sck153898 			verify(nvpair_value_nvlist(elem, configp) == 0);
29493ad6c7f9SVictor Latushkin 			if (pool_match(*configp, *target)) {
29503ad6c7f9SVictor Latushkin 				count++;
2951de6628f0Sck153898 				if (match != NULL) {
29523ad6c7f9SVictor Latushkin 					/* print previously found config */
29533ad6c7f9SVictor Latushkin 					if (name != NULL) {
29543ad6c7f9SVictor Latushkin 						(void) printf("%s\n", name);
29553ad6c7f9SVictor Latushkin 						dump_nvlist(match, 8);
29563ad6c7f9SVictor Latushkin 						name = NULL;
29573ad6c7f9SVictor Latushkin 					}
29583ad6c7f9SVictor Latushkin 					(void) printf("%s\n",
29593ad6c7f9SVictor Latushkin 					    nvpair_name(elem));
29603ad6c7f9SVictor Latushkin 					dump_nvlist(*configp, 8);
2961de6628f0Sck153898 				} else {
2962de6628f0Sck153898 					match = *configp;
29633ad6c7f9SVictor Latushkin 					name = nvpair_name(elem);
2964de6628f0Sck153898 				}
2965de6628f0Sck153898 			}
2966de6628f0Sck153898 		}
2967de6628f0Sck153898 	}
29683ad6c7f9SVictor Latushkin 	if (count > 1)
29693ad6c7f9SVictor Latushkin 		(void) fatal("\tMatched %d pools - use pool GUID "
29703ad6c7f9SVictor Latushkin 		    "instead of pool name or \n"
29713ad6c7f9SVictor Latushkin 		    "\tpool name part of a dataset name to select pool", count);
29723ad6c7f9SVictor Latushkin 
29733ad6c7f9SVictor Latushkin 	if (sepp)
29743ad6c7f9SVictor Latushkin 		*sepp = sep;
29753ad6c7f9SVictor Latushkin 	/*
29763ad6c7f9SVictor Latushkin 	 * If pool GUID was specified for pool id, replace it with pool name
29773ad6c7f9SVictor Latushkin 	 */
29783ad6c7f9SVictor Latushkin 	if (name && (strstr(*target, name) != *target)) {
29793ad6c7f9SVictor Latushkin 		int sz = 1 + strlen(name) + ((sepp) ? strlen(sepp) : 0);
29803ad6c7f9SVictor Latushkin 
29813ad6c7f9SVictor Latushkin 		*target = umem_alloc(sz, UMEM_NOFAIL);
29823ad6c7f9SVictor Latushkin 		(void) snprintf(*target, sz, "%s%s", name, sepp ? sepp : "");
29833ad6c7f9SVictor Latushkin 	}
2984de6628f0Sck153898 
29853ad6c7f9SVictor Latushkin 	*configp = name ? match : NULL;
2986de6628f0Sck153898 
29873ad6c7f9SVictor Latushkin 	return (name);
2988de6628f0Sck153898 }
2989de6628f0Sck153898 
2990fa9e4066Sahrens int
2991fa9e4066Sahrens main(int argc, char **argv)
2992fa9e4066Sahrens {
2993fa9e4066Sahrens 	int i, c;
2994fa9e4066Sahrens 	struct rlimit rl = { 1024, 1024 };
29953ad6c7f9SVictor Latushkin 	spa_t *spa = NULL;
2996fa9e4066Sahrens 	objset_t *os = NULL;
2997fa9e4066Sahrens 	int dump_all = 1;
2998fa9e4066Sahrens 	int verbose = 0;
2999c8ee1847SVictor Latushkin 	int error = 0;
30003ad6c7f9SVictor Latushkin 	char **searchdirs = NULL;
30013ad6c7f9SVictor Latushkin 	int nsearch = 0;
30023ad6c7f9SVictor Latushkin 	char *target;
3003468c413aSTim Haley 	nvlist_t *policy = NULL;
3004468c413aSTim Haley 	uint64_t max_txg = UINT64_MAX;
3005c8ee1847SVictor Latushkin 	int rewind = ZPOOL_NEVER_REWIND;
3006fa9e4066Sahrens 
3007fa9e4066Sahrens 	(void) setrlimit(RLIMIT_NOFILE, &rl);
3008004388ebScasper 	(void) enable_extended_FILE_stdio(-1, -1);
3009fa9e4066Sahrens 
3010fa9e4066Sahrens 	dprintf_setup(&argc, argv);
3011fa9e4066Sahrens 
30123f9d6ad7SLin Ling 	while ((c = getopt(argc, argv, "bcdhilmsuCDRSAFLXevp:t:U:P")) != -1) {
3013fa9e4066Sahrens 		switch (c) {
3014fa9e4066Sahrens 		case 'b':
3015fa9e4066Sahrens 		case 'c':
3016b24ab676SJeff Bonwick 		case 'd':
3017b24ab676SJeff Bonwick 		case 'h':
3018b24ab676SJeff Bonwick 		case 'i':
3019b24ab676SJeff Bonwick 		case 'l':
3020d6e555bdSGeorge Wilson 		case 'm':
3021fa9e4066Sahrens 		case 's':
3022b24ab676SJeff Bonwick 		case 'u':
3023fa9e4066Sahrens 		case 'C':
3024b24ab676SJeff Bonwick 		case 'D':
302544cd46caSbillm 		case 'R':
3026b24ab676SJeff Bonwick 		case 'S':
3027fa9e4066Sahrens 			dump_opt[c]++;
3028fa9e4066Sahrens 			dump_all = 0;
3029fa9e4066Sahrens 			break;
3030feef89cfSVictor Latushkin 		case 'A':
3031c8ee1847SVictor Latushkin 		case 'F':
303282a0a985SVictor Latushkin 		case 'L':
3033c8ee1847SVictor Latushkin 		case 'X':
30343ad6c7f9SVictor Latushkin 		case 'e':
30353f9d6ad7SLin Ling 		case 'P':
303682a0a985SVictor Latushkin 			dump_opt[c]++;
303782a0a985SVictor Latushkin 			break;
3038fa9e4066Sahrens 		case 'v':
3039fa9e4066Sahrens 			verbose++;
3040fa9e4066Sahrens 			break;
3041de6628f0Sck153898 		case 'p':
30423ad6c7f9SVictor Latushkin 			if (searchdirs == NULL) {
30433ad6c7f9SVictor Latushkin 				searchdirs = umem_alloc(sizeof (char *),
30443ad6c7f9SVictor Latushkin 				    UMEM_NOFAIL);
30453ad6c7f9SVictor Latushkin 			} else {
30463ad6c7f9SVictor Latushkin 				char **tmp = umem_alloc((nsearch + 1) *
30473ad6c7f9SVictor Latushkin 				    sizeof (char *), UMEM_NOFAIL);
30483ad6c7f9SVictor Latushkin 				bcopy(searchdirs, tmp, nsearch *
30493ad6c7f9SVictor Latushkin 				    sizeof (char *));
30503ad6c7f9SVictor Latushkin 				umem_free(searchdirs,
30513ad6c7f9SVictor Latushkin 				    nsearch * sizeof (char *));
30523ad6c7f9SVictor Latushkin 				searchdirs = tmp;
30533ad6c7f9SVictor Latushkin 			}
30543ad6c7f9SVictor Latushkin 			searchdirs[nsearch++] = optarg;
3055de6628f0Sck153898 			break;
30562e551927SVictor Latushkin 		case 't':
3057468c413aSTim Haley 			max_txg = strtoull(optarg, NULL, 0);
3058468c413aSTim Haley 			if (max_txg < TXG_INITIAL) {
30592e551927SVictor Latushkin 				(void) fprintf(stderr, "incorrect txg "
30602e551927SVictor Latushkin 				    "specified: %s\n", optarg);
30612e551927SVictor Latushkin 				usage();
30622e551927SVictor Latushkin 			}
30632e551927SVictor Latushkin 			break;
3064b24ab676SJeff Bonwick 		case 'U':
3065b24ab676SJeff Bonwick 			spa_config_path = optarg;
3066b24ab676SJeff Bonwick 			break;
3067fa9e4066Sahrens 		default:
3068fa9e4066Sahrens 			usage();
3069fa9e4066Sahrens 			break;
3070fa9e4066Sahrens 		}
3071fa9e4066Sahrens 	}
3072fa9e4066Sahrens 
30733ad6c7f9SVictor Latushkin 	if (!dump_opt['e'] && searchdirs != NULL) {
307488b7b0f2SMatthew Ahrens 		(void) fprintf(stderr, "-p option requires use of -e\n");
307588b7b0f2SMatthew Ahrens 		usage();
307688b7b0f2SMatthew Ahrens 	}
3077de6628f0Sck153898 
3078fa9e4066Sahrens 	kernel_init(FREAD);
3079de6628f0Sck153898 	g_zfs = libzfs_init();
308091ebeef5Sahrens 	ASSERT(g_zfs != NULL);
3081fa9e4066Sahrens 
3082b24ab676SJeff Bonwick 	if (dump_all)
3083b24ab676SJeff Bonwick 		verbose = MAX(verbose, 1);
3084b24ab676SJeff Bonwick 
3085fa9e4066Sahrens 	for (c = 0; c < 256; c++) {
30863f9d6ad7SLin Ling 		if (dump_all && !strchr("elAFLRSXP", c))
3087fa9e4066Sahrens 			dump_opt[c] = 1;
3088fa9e4066Sahrens 		if (dump_opt[c])
3089fa9e4066Sahrens 			dump_opt[c] += verbose;
3090fa9e4066Sahrens 	}
3091fa9e4066Sahrens 
3092feef89cfSVictor Latushkin 	aok = (dump_opt['A'] == 1) || (dump_opt['A'] > 2);
3093feef89cfSVictor Latushkin 	zfs_recover = (dump_opt['A'] > 1);
3094feef89cfSVictor Latushkin 
3095fa9e4066Sahrens 	argc -= optind;
3096fa9e4066Sahrens 	argv += optind;
3097fa9e4066Sahrens 
309807428bdfSVictor Latushkin 	if (argc < 2 && dump_opt['R'])
309907428bdfSVictor Latushkin 		usage();
3100fa9e4066Sahrens 	if (argc < 1) {
31013ad6c7f9SVictor Latushkin 		if (!dump_opt['e'] && dump_opt['C']) {
3102e829d913Sck153898 			dump_cachefile(spa_config_path);
3103fa9e4066Sahrens 			return (0);
3104fa9e4066Sahrens 		}
3105fa9e4066Sahrens 		usage();
3106fa9e4066Sahrens 	}
3107fa9e4066Sahrens 
3108fa9e4066Sahrens 	if (dump_opt['l']) {
3109fa9e4066Sahrens 		dump_label(argv[0]);
3110fa9e4066Sahrens 		return (0);
3111fa9e4066Sahrens 	}
3112fa9e4066Sahrens 
3113c8ee1847SVictor Latushkin 	if (dump_opt['X'] || dump_opt['F'])
3114c8ee1847SVictor Latushkin 		rewind = ZPOOL_DO_REWIND |
3115c8ee1847SVictor Latushkin 		    (dump_opt['X'] ? ZPOOL_EXTREME_REWIND : 0);
3116c8ee1847SVictor Latushkin 
3117c8ee1847SVictor Latushkin 	if (nvlist_alloc(&policy, NV_UNIQUE_NAME_TYPE, 0) != 0 ||
3118c8ee1847SVictor Latushkin 	    nvlist_add_uint64(policy, ZPOOL_REWIND_REQUEST_TXG, max_txg) != 0 ||
3119c8ee1847SVictor Latushkin 	    nvlist_add_uint32(policy, ZPOOL_REWIND_REQUEST, rewind) != 0)
3120c8ee1847SVictor Latushkin 		fatal("internal error: %s", strerror(ENOMEM));
3121c8ee1847SVictor Latushkin 
3122c5904d13Seschrock 	error = 0;
31233ad6c7f9SVictor Latushkin 	target = argv[0];
3124de6628f0Sck153898 
31253ad6c7f9SVictor Latushkin 	if (dump_opt['e']) {
31263ad6c7f9SVictor Latushkin 		nvlist_t *cfg = NULL;
31273ad6c7f9SVictor Latushkin 		char *name = find_zpool(&target, &cfg, nsearch, searchdirs);
3128c5904d13Seschrock 
31293ad6c7f9SVictor Latushkin 		error = ENOENT;
31303ad6c7f9SVictor Latushkin 		if (name) {
313107428bdfSVictor Latushkin 			if (dump_opt['C'] > 1) {
313207428bdfSVictor Latushkin 				(void) printf("\nConfiguration for import:\n");
313307428bdfSVictor Latushkin 				dump_nvlist(cfg, 8);
313407428bdfSVictor Latushkin 			}
3135c8ee1847SVictor Latushkin 			if (nvlist_add_nvlist(cfg,
3136468c413aSTim Haley 			    ZPOOL_REWIND_POLICY, policy) != 0) {
3137468c413aSTim Haley 				fatal("can't open '%s': %s",
3138468c413aSTim Haley 				    target, strerror(ENOMEM));
3139468c413aSTim Haley 			}
31404b964adaSGeorge Wilson 			if ((error = spa_import(name, cfg, NULL,
31414b964adaSGeorge Wilson 			    ZFS_IMPORT_MISSING_LOG)) != 0) {
31424b964adaSGeorge Wilson 				error = spa_import(name, cfg, NULL,
31434b964adaSGeorge Wilson 				    ZFS_IMPORT_VERBATIM);
31444b964adaSGeorge Wilson 			}
3145990b4856Slling 		}
3146c5904d13Seschrock 	}
3147c5904d13Seschrock 
3148c5904d13Seschrock 	if (error == 0) {
314907428bdfSVictor Latushkin 		if (strpbrk(target, "/@") == NULL || dump_opt['R']) {
315080eb36f2SGeorge Wilson 			error = spa_open_rewind(target, &spa, FTAG, policy,
315180eb36f2SGeorge Wilson 			    NULL);
31528f18d1faSGeorge Wilson 			if (error) {
31538f18d1faSGeorge Wilson 				/*
31548f18d1faSGeorge Wilson 				 * If we're missing the log device then
31558f18d1faSGeorge Wilson 				 * try opening the pool after clearing the
31568f18d1faSGeorge Wilson 				 * log state.
31578f18d1faSGeorge Wilson 				 */
31588f18d1faSGeorge Wilson 				mutex_enter(&spa_namespace_lock);
31593ad6c7f9SVictor Latushkin 				if ((spa = spa_lookup(target)) != NULL &&
31608f18d1faSGeorge Wilson 				    spa->spa_log_state == SPA_LOG_MISSING) {
31618f18d1faSGeorge Wilson 					spa->spa_log_state = SPA_LOG_CLEAR;
31628f18d1faSGeorge Wilson 					error = 0;
31638f18d1faSGeorge Wilson 				}
31648f18d1faSGeorge Wilson 				mutex_exit(&spa_namespace_lock);
31658f18d1faSGeorge Wilson 
316680eb36f2SGeorge Wilson 				if (!error) {
316780eb36f2SGeorge Wilson 					error = spa_open_rewind(target, &spa,
316880eb36f2SGeorge Wilson 					    FTAG, policy, NULL);
316980eb36f2SGeorge Wilson 				}
31708f18d1faSGeorge Wilson 			}
317107428bdfSVictor Latushkin 		} else {
317207428bdfSVictor Latushkin 			error = dmu_objset_own(target, DMU_OST_ANY,
317307428bdfSVictor Latushkin 			    B_TRUE, FTAG, &os);
3174c5904d13Seschrock 		}
3175de6628f0Sck153898 	}
317680eb36f2SGeorge Wilson 	nvlist_free(policy);
317780eb36f2SGeorge Wilson 
3178fa9e4066Sahrens 	if (error)
31793ad6c7f9SVictor Latushkin 		fatal("can't open '%s': %s", target, strerror(error));
3180fa9e4066Sahrens 
3181fa9e4066Sahrens 	argv++;
318207428bdfSVictor Latushkin 	argc--;
318307428bdfSVictor Latushkin 	if (!dump_opt['R']) {
318407428bdfSVictor Latushkin 		if (argc > 0) {
3185fa9e4066Sahrens 			zopt_objects = argc;
3186fa9e4066Sahrens 			zopt_object = calloc(zopt_objects, sizeof (uint64_t));
3187fa9e4066Sahrens 			for (i = 0; i < zopt_objects; i++) {
3188fa9e4066Sahrens 				errno = 0;
3189fa9e4066Sahrens 				zopt_object[i] = strtoull(argv[i], NULL, 0);
3190fa9e4066Sahrens 				if (zopt_object[i] == 0 && errno != 0)
319187219db7SVictor Latushkin 					fatal("bad number %s: %s",
3192fa9e4066Sahrens 					    argv[i], strerror(errno));
3193fa9e4066Sahrens 			}
3194fa9e4066Sahrens 		}
319507428bdfSVictor Latushkin 		(os != NULL) ? dump_dir(os) : dump_zpool(spa);
3196fa9e4066Sahrens 	} else {
319707428bdfSVictor Latushkin 		flagbits['b'] = ZDB_FLAG_PRINT_BLKPTR;
319807428bdfSVictor Latushkin 		flagbits['c'] = ZDB_FLAG_CHECKSUM;
319907428bdfSVictor Latushkin 		flagbits['d'] = ZDB_FLAG_DECOMPRESS;
320007428bdfSVictor Latushkin 		flagbits['e'] = ZDB_FLAG_BSWAP;
320107428bdfSVictor Latushkin 		flagbits['g'] = ZDB_FLAG_GBH;
320207428bdfSVictor Latushkin 		flagbits['i'] = ZDB_FLAG_INDIRECT;
320307428bdfSVictor Latushkin 		flagbits['p'] = ZDB_FLAG_PHYS;
320407428bdfSVictor Latushkin 		flagbits['r'] = ZDB_FLAG_RAW;
320507428bdfSVictor Latushkin 
320607428bdfSVictor Latushkin 		for (i = 0; i < argc; i++)
320707428bdfSVictor Latushkin 			zdb_read_block(argv[i], spa);
3208fa9e4066Sahrens 	}
3209fa9e4066Sahrens 
321007428bdfSVictor Latushkin 	(os != NULL) ? dmu_objset_disown(os, FTAG) : spa_close(spa, FTAG);
321107428bdfSVictor Latushkin 
3212e0d35c44Smarks 	fuid_table_destroy();
32130a586ceaSMark Shellenbaum 	sa_loaded = B_FALSE;
3214e0d35c44Smarks 
3215de6628f0Sck153898 	libzfs_fini(g_zfs);
3216fa9e4066Sahrens 	kernel_fini();
3217fa9e4066Sahrens 
3218fa9e4066Sahrens 	return (0);
3219fa9e4066Sahrens }
3220