zdb.c revision 2acef22db7808606888f8f92715629ff3ba555b9
1fa9e406ahrens/*
2fa9e406ahrens * CDDL HEADER START
3fa9e406ahrens *
4fa9e406ahrens * The contents of this file are subject to the terms of the
5ea8dc4beschrock * Common Development and Distribution License (the "License").
6ea8dc4beschrock * You may not use this file except in compliance with the License.
7fa9e406ahrens *
8fa9e406ahrens * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9fa9e406ahrens * or http://www.opensolaris.org/os/licensing.
10fa9e406ahrens * See the License for the specific language governing permissions
11fa9e406ahrens * and limitations under the License.
12fa9e406ahrens *
13fa9e406ahrens * When distributing Covered Code, include this CDDL HEADER in each
14fa9e406ahrens * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15fa9e406ahrens * If applicable, add the following below this CDDL HEADER, with the
16fa9e406ahrens * fields enclosed by brackets "[]" replaced with your own identifying
17fa9e406ahrens * information: Portions Copyright [yyyy] [name of copyright owner]
18fa9e406ahrens *
19fa9e406ahrens * CDDL HEADER END
20fa9e406ahrens */
21ad135b5Christopher Siden
22fa9e406ahrens/*
238f2529dMark Shellenbaum * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
24490d05bMatthew Ahrens * Copyright (c) 2013 by Delphix. All rights reserved.
25fa9e406ahrens */
26fa9e406ahrens
27fa9e406ahrens#include <stdio.h>
28490d05bMatthew Ahrens#include <unistd.h>
29004388ecasper#include <stdio_ext.h>
30fa9e406ahrens#include <stdlib.h>
3144cd46cbillm#include <ctype.h>
32fa9e406ahrens#include <sys/zfs_context.h>
33fa9e406ahrens#include <sys/spa.h>
34fa9e406ahrens#include <sys/spa_impl.h>
35fa9e406ahrens#include <sys/dmu.h>
36fa9e406ahrens#include <sys/zap.h>
37fa9e406ahrens#include <sys/fs/zfs.h>
38fa9e406ahrens#include <sys/zfs_znode.h>
390a586ceMark Shellenbaum#include <sys/zfs_sa.h>
400a586ceMark Shellenbaum#include <sys/sa.h>
410a586ceMark Shellenbaum#include <sys/sa_impl.h>
42fa9e406ahrens#include <sys/vdev.h>
43fa9e406ahrens#include <sys/vdev_impl.h>
44fa9e406ahrens#include <sys/metaslab_impl.h>
45fa9e406ahrens#include <sys/dmu_objset.h>
46fa9e406ahrens#include <sys/dsl_dir.h>
47fa9e406ahrens#include <sys/dsl_dataset.h>
48fa9e406ahrens#include <sys/dsl_pool.h>
49fa9e406ahrens#include <sys/dbuf.h>
50fa9e406ahrens#include <sys/zil.h>
51fa9e406ahrens#include <sys/zil_impl.h>
52fa9e406ahrens#include <sys/stat.h>
53fa9e406ahrens#include <sys/resource.h>
54fa9e406ahrens#include <sys/dmu_traverse.h>
55fa9e406ahrens#include <sys/zio_checksum.h>
56fa9e406ahrens#include <sys/zio_compress.h>
57e0d35c4marks#include <sys/zfs_fuid.h>
5888b7b0fMatthew Ahrens#include <sys/arc.h>
59b24ab67Jeff Bonwick#include <sys/ddt.h>
60ad135b5Christopher Siden#include <sys/zfeature.h>
614445fffMatthew Ahrens#include <zfs_comutil.h>
62de6628fck#undef ZFS_MAXNAMELEN
63de6628fck#undef verify
64de6628fck#include <libzfs.h>
65fa9e406ahrens
66e690fb2Christopher Siden#define	ZDB_COMPRESS_NAME(idx) ((idx) < ZIO_COMPRESS_FUNCTIONS ?	\
67e690fb2Christopher Siden	zio_compress_table[(idx)].ci_name : "UNKNOWN")
68e690fb2Christopher Siden#define	ZDB_CHECKSUM_NAME(idx) ((idx) < ZIO_CHECKSUM_FUNCTIONS ?	\
69e690fb2Christopher Siden	zio_checksum_table[(idx)].ci_name : "UNKNOWN")
70e690fb2Christopher Siden#define	ZDB_OT_NAME(idx) ((idx) < DMU_OT_NUMTYPES ?	\
71e690fb2Christopher Siden	dmu_ot[(idx)].ot_name : DMU_OT_IS_VALID(idx) ?	\
72e690fb2Christopher Siden	dmu_ot_byteswap[DMU_OT_BYTESWAP(idx)].ob_name : "UNKNOWN")
73e690fb2Christopher Siden#define	ZDB_OT_TYPE(idx) ((idx) < DMU_OT_NUMTYPES ? (idx) :		\
74e690fb2Christopher Siden	(((idx) == DMU_OTN_ZAP_DATA || (idx) == DMU_OTN_ZAP_METADATA) ?	\
75e690fb2Christopher Siden	DMU_OT_ZAP_OTHER : DMU_OT_NUMTYPES))
766de8f41Victor Latushkin
77feef89cVictor Latushkin#ifndef lint
78feef89cVictor Latushkinextern int zfs_recover;
79feef89cVictor Latushkin#else
80feef89cVictor Latushkinint zfs_recover;
81feef89cVictor Latushkin#endif
82feef89cVictor Latushkin
83fa9e406ahrensconst char cmdname[] = "zdb";
84fa9e406ahrensuint8_t dump_opt[256];
85fa9e406ahrens
86fa9e406ahrenstypedef void object_viewer_t(objset_t *, uint64_t, void *data, size_t size);
87fa9e406ahrens
88fa9e406ahrensextern void dump_intent_log(zilog_t *);
89fa9e406ahrensuint64_t *zopt_object = NULL;
90fa9e406ahrensint zopt_objects = 0;
91de6628fcklibzfs_handle_t *g_zfs;
9231d7e8fGeorge Wilsonuint64_t max_inflight = 200;
93fa9e406ahrens
94fa9e406ahrens/*
95fa9e406ahrens * These libumem hooks provide a reasonable set of defaults for the allocator's
96fa9e406ahrens * debugging facilities.
97fa9e406ahrens */
98fa9e406ahrensconst char *
99fa9e406ahrens_umem_debug_init()
100fa9e406ahrens{
101fa9e406ahrens	return ("default,verbose"); /* $UMEM_DEBUG setting */
102fa9e406ahrens}
103fa9e406ahrens
104fa9e406ahrensconst char *
105fa9e406ahrens_umem_logging_init(void)
106fa9e406ahrens{
107fa9e406ahrens	return ("fail,contents"); /* $UMEM_LOGGING setting */
108fa9e406ahrens}
109fa9e406ahrens
110fa9e406ahrensstatic void
111fa9e406ahrensusage(void)
112fa9e406ahrens{
113fa9e406ahrens	(void) fprintf(stderr,
11490e894eRichard Lowe	    "Usage: %s [-CumdibcsDvhLXFPA] [-t txg] [-e [-p path...]] "
11531d7e8fGeorge Wilson	    "[-U config] [-M inflight I/Os] poolname [object...]\n"
11631d7e8fGeorge Wilson	    "       %s [-divPA] [-e -p path...] [-U config] dataset "
11731d7e8fGeorge Wilson	    "[object...]\n"
11831d7e8fGeorge Wilson	    "       %s -m [-LXFPA] [-t txg] [-e [-p path...]] [-U config] "
11990e894eRichard Lowe	    "poolname [vdev [metaslab...]]\n"
12090e894eRichard Lowe	    "       %s -R [-A] [-e [-p path...]] poolname "
12190e894eRichard Lowe	    "vdev:offset:size[:flags]\n"
12231d7e8fGeorge Wilson	    "       %s -S [-PA] [-e [-p path...]] [-U config] poolname\n"
12390e894eRichard Lowe	    "       %s -l [-uA] device\n"
12490e894eRichard Lowe	    "       %s -C [-A] [-U config]\n\n",
125bbfd46cJeff Bonwick	    cmdname, cmdname, cmdname, cmdname, cmdname, cmdname, cmdname);
1263ad6c7fVictor Latushkin
1273ad6c7fVictor Latushkin	(void) fprintf(stderr, "    Dataset name must include at least one "
1283ad6c7fVictor Latushkin	    "separator character '/' or '@'\n");
1293ad6c7fVictor Latushkin	(void) fprintf(stderr, "    If dataset name is specified, only that "
1303ad6c7fVictor Latushkin	    "dataset is dumped\n");
1313ad6c7fVictor Latushkin	(void) fprintf(stderr, "    If object numbers are specified, only "
1323ad6c7fVictor Latushkin	    "those objects are dumped\n\n");
1333ad6c7fVictor Latushkin	(void) fprintf(stderr, "    Options to control amount of output:\n");
1343ad6c7fVictor Latushkin	(void) fprintf(stderr, "        -u uberblock\n");
1353ad6c7fVictor Latushkin	(void) fprintf(stderr, "        -d dataset(s)\n");
1363ad6c7fVictor Latushkin	(void) fprintf(stderr, "        -i intent logs\n");
13707428bdVictor Latushkin	(void) fprintf(stderr, "        -C config (or cachefile if alone)\n");
1388f18d1fGeorge Wilson	(void) fprintf(stderr, "        -h pool history\n");
1393ad6c7fVictor Latushkin	(void) fprintf(stderr, "        -b block statistics\n");
1403ad6c7fVictor Latushkin	(void) fprintf(stderr, "        -m metaslabs\n");
1413ad6c7fVictor Latushkin	(void) fprintf(stderr, "        -c checksum all metadata (twice for "
1426365109Victor Latushkin	    "all data) blocks\n");
1433ad6c7fVictor Latushkin	(void) fprintf(stderr, "        -s report stats on zdb's I/O\n");
144f0ba89bJeff Bonwick	(void) fprintf(stderr, "        -D dedup statistics\n");
145b24ab67Jeff Bonwick	(void) fprintf(stderr, "        -S simulate dedup to measure effect\n");
1463ad6c7fVictor Latushkin	(void) fprintf(stderr, "        -v verbose (applies to all others)\n");
147fa9e406ahrens	(void) fprintf(stderr, "        -l dump label contents\n");
14882a0a98Victor Latushkin	(void) fprintf(stderr, "        -L disable leak tracking (do not "
14982a0a98Victor Latushkin	    "load spacemaps)\n");
150d41e764ek	(void) fprintf(stderr, "        -R read and display block from a "
1513ad6c7fVictor Latushkin	    "device\n\n");
1523ad6c7fVictor Latushkin	(void) fprintf(stderr, "    Below options are intended for use "
1533ad6c7fVictor Latushkin	    "with other options (except -l):\n");
154feef89cVictor Latushkin	(void) fprintf(stderr, "        -A ignore assertions (-A), enable "
155feef89cVictor Latushkin	    "panic recovery (-AA) or both (-AAA)\n");
156c8ee184Victor Latushkin	(void) fprintf(stderr, "        -F attempt automatic rewind within "
157c8ee184Victor Latushkin	    "safe range of transaction groups\n");
1583ad6c7fVictor Latushkin	(void) fprintf(stderr, "        -U <cachefile_path> -- use alternate "
1593ad6c7fVictor Latushkin	    "cachefile\n");
160c8ee184Victor Latushkin	(void) fprintf(stderr, "        -X attempt extreme rewind (does not "
161c8ee184Victor Latushkin	    "work with dataset)\n");
1623ad6c7fVictor Latushkin	(void) fprintf(stderr, "        -e pool is exported/destroyed/"
1633ad6c7fVictor Latushkin	    "has altroot/not in a cachefile\n");
1643ad6c7fVictor Latushkin	(void) fprintf(stderr, "        -p <path> -- use one or more with "
1653ad6c7fVictor Latushkin	    "-e to specify path to vdev dir\n");
16690e894eRichard Lowe	(void) fprintf(stderr, "        -P print numbers in parseable form\n");
1673ad6c7fVictor Latushkin	(void) fprintf(stderr, "        -t <txg> -- highest txg to use when "
1682e55192Victor Latushkin	    "searching for uberblocks\n");
16931d7e8fGeorge Wilson	(void) fprintf(stderr, "        -M <number of inflight I/Os> -- "
17031d7e8fGeorge Wilson	    "specify the maximum number of checksumming I/Os [default is 200]");
171fa9e406ahrens	(void) fprintf(stderr, "Specify an option more than once (e.g. -bb) "
172fa9e406ahrens	    "to make only that option verbose\n");
173fa9e406ahrens	(void) fprintf(stderr, "Default is to dump everything non-verbosely\n");
174fa9e406ahrens	exit(1);
175fa9e406ahrens}
176fa9e406ahrens
177ccba080Rich Morris/*
178ccba080Rich Morris * Called for usage errors that are discovered after a call to spa_open(),
179ccba080Rich Morris * dmu_bonus_hold(), or pool_match().  abort() is called for other errors.
180ccba080Rich Morris */
181ccba080Rich Morris
182fa9e406ahrensstatic void
183fa9e406ahrensfatal(const char *fmt, ...)
184fa9e406ahrens{
185fa9e406ahrens	va_list ap;
186fa9e406ahrens
187fa9e406ahrens	va_start(ap, fmt);
188fa9e406ahrens	(void) fprintf(stderr, "%s: ", cmdname);
189fa9e406ahrens	(void) vfprintf(stderr, fmt, ap);
190fa9e406ahrens	va_end(ap);
191fa9e406ahrens	(void) fprintf(stderr, "\n");
192fa9e406ahrens
193ccba080Rich Morris	exit(1);
194fa9e406ahrens}
195fa9e406ahrens
196fa9e406ahrens/* ARGSUSED */
197fa9e406ahrensstatic void
198fa9e406ahrensdump_packed_nvlist(objset_t *os, uint64_t object, void *data, size_t size)
199fa9e406ahrens{
200fa9e406ahrens	nvlist_t *nv;
201fa9e406ahrens	size_t nvsize = *(uint64_t *)data;
202fa9e406ahrens	char *packed = umem_alloc(nvsize, UMEM_NOFAIL);
203fa9e406ahrens
2047bfdf01Neil Perrin	VERIFY(0 == dmu_read(os, object, 0, nvsize, packed, DMU_READ_PREFETCH));
205fa9e406ahrens
206fa9e406ahrens	VERIFY(nvlist_unpack(packed, nvsize, &nv, 0) == 0);
207fa9e406ahrens
208fa9e406ahrens	umem_free(packed, nvsize);
209fa9e406ahrens
210fa9e406ahrens	dump_nvlist(nv, 8);
211fa9e406ahrens
212fa9e406ahrens	nvlist_free(nv);
213fa9e406ahrens}
214fa9e406ahrens
2154445fffMatthew Ahrens/* ARGSUSED */
2164445fffMatthew Ahrensstatic void
2174445fffMatthew Ahrensdump_history_offsets(objset_t *os, uint64_t object, void *data, size_t size)
2184445fffMatthew Ahrens{
2194445fffMatthew Ahrens	spa_history_phys_t *shp = data;
2204445fffMatthew Ahrens
2214445fffMatthew Ahrens	if (shp == NULL)
2224445fffMatthew Ahrens		return;
2234445fffMatthew Ahrens
2244445fffMatthew Ahrens	(void) printf("\t\tpool_create_len = %llu\n",
2254445fffMatthew Ahrens	    (u_longlong_t)shp->sh_pool_create_len);
2264445fffMatthew Ahrens	(void) printf("\t\tphys_max_off = %llu\n",
2274445fffMatthew Ahrens	    (u_longlong_t)shp->sh_phys_max_off);
2284445fffMatthew Ahrens	(void) printf("\t\tbof = %llu\n",
2294445fffMatthew Ahrens	    (u_longlong_t)shp->sh_bof);
2304445fffMatthew Ahrens	(void) printf("\t\teof = %llu\n",
2314445fffMatthew Ahrens	    (u_longlong_t)shp->sh_eof);
2324445fffMatthew Ahrens	(void) printf("\t\trecords_lost = %llu\n",
2334445fffMatthew Ahrens	    (u_longlong_t)shp->sh_records_lost);
2344445fffMatthew Ahrens}
2354445fffMatthew Ahrens
2363f9d6adLin Lingstatic void
2373f9d6adLin Lingzdb_nicenum(uint64_t num, char *buf)
2383f9d6adLin Ling{
2393f9d6adLin Ling	if (dump_opt['P'])
2403f9d6adLin Ling		(void) sprintf(buf, "%llu", (longlong_t)num);
2413f9d6adLin Ling	else
2423f9d6adLin Ling		nicenum(num, buf);
2433f9d6adLin Ling}
2443f9d6adLin Ling
245490d05bMatthew Ahrensconst char histo_stars[] = "****************************************";
246490d05bMatthew Ahrensconst int histo_width = sizeof (histo_stars) - 1;
247fa9e406ahrens
248fa9e406ahrensstatic void
2490713e23George Wilsondump_histogram(const uint64_t *histo, int size, int offset)
250fa9e406ahrens{
251fa9e406ahrens	int i;
252490d05bMatthew Ahrens	int minidx = size - 1;
253fa9e406ahrens	int maxidx = 0;
254fa9e406ahrens	uint64_t max = 0;
255fa9e406ahrens
256490d05bMatthew Ahrens	for (i = 0; i < size; i++) {
257fa9e406ahrens		if (histo[i] > max)
258fa9e406ahrens			max = histo[i];
259fa9e406ahrens		if (histo[i] > 0 && i > maxidx)
260fa9e406ahrens			maxidx = i;
261fa9e406ahrens		if (histo[i] > 0 && i < minidx)
262fa9e406ahrens			minidx = i;
263fa9e406ahrens	}
264fa9e406ahrens
265490d05bMatthew Ahrens	if (max < histo_width)
266490d05bMatthew Ahrens		max = histo_width;
267fa9e406ahrens
268490d05bMatthew Ahrens	for (i = minidx; i <= maxidx; i++) {
269490d05bMatthew Ahrens		(void) printf("\t\t\t%3u: %6llu %s\n",
2700713e23George Wilson		    i + offset, (u_longlong_t)histo[i],
271490d05bMatthew Ahrens		    &histo_stars[(max - histo[i]) * histo_width / max]);
272490d05bMatthew Ahrens	}
273fa9e406ahrens}
274fa9e406ahrens
275fa9e406ahrensstatic void
276fa9e406ahrensdump_zap_stats(objset_t *os, uint64_t object)
277fa9e406ahrens{
278fa9e406ahrens	int error;
279fa9e406ahrens	zap_stats_t zs;
280fa9e406ahrens
281fa9e406ahrens	error = zap_get_stats(os, object, &zs);
282fa9e406ahrens	if (error)
283fa9e406ahrens		return;
284fa9e406ahrens
285fa9e406ahrens	if (zs.zs_ptrtbl_len == 0) {
286fa9e406ahrens		ASSERT(zs.zs_num_blocks == 1);
287fa9e406ahrens		(void) printf("\tmicrozap: %llu bytes, %llu entries\n",
288fa9e406ahrens		    (u_longlong_t)zs.zs_blocksize,
289fa9e406ahrens		    (u_longlong_t)zs.zs_num_entries);
290fa9e406ahrens		return;
291fa9e406ahrens	}
292fa9e406ahrens
293fa9e406ahrens	(void) printf("\tFat ZAP stats:\n");
2948248818nd
2958248818nd	(void) printf("\t\tPointer table:\n");
2968248818nd	(void) printf("\t\t\t%llu elements\n",
297fa9e406ahrens	    (u_longlong_t)zs.zs_ptrtbl_len);
2988248818nd	(void) printf("\t\t\tzt_blk: %llu\n",
2998248818nd	    (u_longlong_t)zs.zs_ptrtbl_zt_blk);
3008248818nd	(void) printf("\t\t\tzt_numblks: %llu\n",
3018248818nd	    (u_longlong_t)zs.zs_ptrtbl_zt_numblks);
3028248818nd	(void) printf("\t\t\tzt_shift: %llu\n",
3038248818nd	    (u_longlong_t)zs.zs_ptrtbl_zt_shift);
3048248818nd	(void) printf("\t\t\tzt_blks_copied: %llu\n",
3058248818nd	    (u_longlong_t)zs.zs_ptrtbl_blks_copied);
3068248818nd	(void) printf("\t\t\tzt_nextblk: %llu\n",
3078248818nd	    (u_longlong_t)zs.zs_ptrtbl_nextblk);
3088248818nd
309fa9e406ahrens	(void) printf("\t\tZAP entries: %llu\n",
310fa9e406ahrens	    (u_longlong_t)zs.zs_num_entries);
311fa9e406ahrens	(void) printf("\t\tLeaf blocks: %llu\n",
312fa9e406ahrens	    (u_longlong_t)zs.zs_num_leafs);
313fa9e406ahrens	(void) printf("\t\tTotal blocks: %llu\n",
314fa9e406ahrens	    (u_longlong_t)zs.zs_num_blocks);
3158248818nd	(void) printf("\t\tzap_block_type: 0x%llx\n",
3168248818nd	    (u_longlong_t)zs.zs_block_type);
3178248818nd	(void) printf("\t\tzap_magic: 0x%llx\n",
3188248818nd	    (u_longlong_t)zs.zs_magic);
3198248818nd	(void) printf("\t\tzap_salt: 0x%llx\n",
3208248818nd	    (u_longlong_t)zs.zs_salt);
321fa9e406ahrens
322fa9e406ahrens	(void) printf("\t\tLeafs with 2^n pointers:\n");
3230713e23George Wilson	dump_histogram(zs.zs_leafs_with_2n_pointers, ZAP_HISTOGRAM_SIZE, 0);
324fa9e406ahrens
325fa9e406ahrens	(void) printf("\t\tBlocks with n*5 entries:\n");
3260713e23George Wilson	dump_histogram(zs.zs_blocks_with_n5_entries, ZAP_HISTOGRAM_SIZE, 0);
327fa9e406ahrens
328fa9e406ahrens	(void) printf("\t\tBlocks n/10 full:\n");
3290713e23George Wilson	dump_histogram(zs.zs_blocks_n_tenths_full, ZAP_HISTOGRAM_SIZE, 0);
330fa9e406ahrens
331fa9e406ahrens	(void) printf("\t\tEntries with n chunks:\n");
3320713e23George Wilson	dump_histogram(zs.zs_entries_using_n_chunks, ZAP_HISTOGRAM_SIZE, 0);
333fa9e406ahrens
334fa9e406ahrens	(void) printf("\t\tBuckets with n entries:\n");
3350713e23George Wilson	dump_histogram(zs.zs_buckets_with_n_entries, ZAP_HISTOGRAM_SIZE, 0);
336fa9e406ahrens}
337fa9e406ahrens
338fa9e406ahrens/*ARGSUSED*/
339fa9e406ahrensstatic void
340fa9e406ahrensdump_none(objset_t *os, uint64_t object, void *data, size_t size)
341fa9e406ahrens{
342fa9e406ahrens}
343fa9e406ahrens
344fa9e406ahrens/*ARGSUSED*/
3456de8f41Victor Latushkinstatic void
3466de8f41Victor Latushkindump_unknown(objset_t *os, uint64_t object, void *data, size_t size)
3476de8f41Victor Latushkin{
3486de8f41Victor Latushkin	(void) printf("\tUNKNOWN OBJECT TYPE\n");
3496de8f41Victor Latushkin}
3506de8f41Victor Latushkin
3516de8f41Victor Latushkin/*ARGSUSED*/
352fa9e406ahrensvoid
353fa9e406ahrensdump_uint8(objset_t *os, uint64_t object, void *data, size_t size)
354fa9e406ahrens{
355fa9e406ahrens}
356fa9e406ahrens
357fa9e406ahrens/*ARGSUSED*/
358fa9e406ahrensstatic void
359fa9e406ahrensdump_uint64(objset_t *os, uint64_t object, void *data, size_t size)
360fa9e406ahrens{
361fa9e406ahrens}
362fa9e406ahrens
363fa9e406ahrens/*ARGSUSED*/
364fa9e406ahrensstatic void
365fa9e406ahrensdump_zap(objset_t *os, uint64_t object, void *data, size_t size)
366fa9e406ahrens{
367fa9e406ahrens	zap_cursor_t zc;
368fa9e406ahrens	zap_attribute_t attr;
369fa9e406ahrens	void *prop;
370fa9e406ahrens	int i;
371fa9e406ahrens
372fa9e406ahrens	dump_zap_stats(os, object);
373fa9e406ahrens	(void) printf("\n");
374fa9e406ahrens
375fa9e406ahrens	for (zap_cursor_init(&zc, os, object);
376fa9e406ahrens	    zap_cursor_retrieve(&zc, &attr) == 0;
377fa9e406ahrens	    zap_cursor_advance(&zc)) {
378fa9e406ahrens		(void) printf("\t\t%s = ", attr.za_name);
379fa9e406ahrens		if (attr.za_num_integers == 0) {
380fa9e406ahrens			(void) printf("\n");
381fa9e406ahrens			continue;
382fa9e406ahrens		}
383fa9e406ahrens		prop = umem_zalloc(attr.za_num_integers *
384fa9e406ahrens		    attr.za_integer_length, UMEM_NOFAIL);
385fa9e406ahrens		(void) zap_lookup(os, object, attr.za_name,
386fa9e406ahrens		    attr.za_integer_length, attr.za_num_integers, prop);
387fa9e406ahrens		if (attr.za_integer_length == 1) {
388fa9e406ahrens			(void) printf("%s", (char *)prop);
389fa9e406ahrens		} else {
390fa9e406ahrens			for (i = 0; i < attr.za_num_integers; i++) {
391fa9e406ahrens				switch (attr.za_integer_length) {
392fa9e406ahrens				case 2:
393fa9e406ahrens					(void) printf("%u ",
394fa9e406ahrens					    ((uint16_t *)prop)[i]);
395fa9e406ahrens					break;
396fa9e406ahrens				case 4:
397fa9e406ahrens					(void) printf("%u ",
398fa9e406ahrens					    ((uint32_t *)prop)[i]);
399fa9e406ahrens					break;
400fa9e406ahrens				case 8:
401fa9e406ahrens					(void) printf("%lld ",
402fa9e406ahrens					    (u_longlong_t)((int64_t *)prop)[i]);
403fa9e406ahrens					break;
404fa9e406ahrens				}
405fa9e406ahrens			}
406fa9e406ahrens		}
407fa9e406ahrens		(void) printf("\n");
408fa9e406ahrens		umem_free(prop, attr.za_num_integers * attr.za_integer_length);
409fa9e406ahrens	}
41087e5029ahrens	zap_cursor_fini(&zc);
411fa9e406ahrens}
412fa9e406ahrens
413e743726ahrens/*ARGSUSED*/
414e743726ahrensstatic void
415486ae71Matthew Ahrensdump_ddt_zap(objset_t *os, uint64_t object, void *data, size_t size)
416486ae71Matthew Ahrens{
417486ae71Matthew Ahrens	dump_zap_stats(os, object);
418486ae71Matthew Ahrens	/* contents are printed elsewhere, properly decoded */
419486ae71Matthew Ahrens}
420486ae71Matthew Ahrens
421486ae71Matthew Ahrens/*ARGSUSED*/
422486ae71Matthew Ahrensstatic void
4230a586ceMark Shellenbaumdump_sa_attrs(objset_t *os, uint64_t object, void *data, size_t size)
4240a586ceMark Shellenbaum{
4250a586ceMark Shellenbaum	zap_cursor_t zc;
4260a586ceMark Shellenbaum	zap_attribute_t attr;
4270a586ceMark Shellenbaum
4280a586ceMark Shellenbaum	dump_zap_stats(os, object);
4290a586ceMark Shellenbaum	(void) printf("\n");
4300a586ceMark Shellenbaum
4310a586ceMark Shellenbaum	for (zap_cursor_init(&zc, os, object);
4320a586ceMark Shellenbaum	    zap_cursor_retrieve(&zc, &attr) == 0;
4330a586ceMark Shellenbaum	    zap_cursor_advance(&zc)) {
4340a586ceMark Shellenbaum		(void) printf("\t\t%s = ", attr.za_name);
4350a586ceMark Shellenbaum		if (attr.za_num_integers == 0) {
4360a586ceMark Shellenbaum			(void) printf("\n");
4370a586ceMark Shellenbaum			continue;
4380a586ceMark Shellenbaum		}
4390a586ceMark Shellenbaum		(void) printf(" %llx : [%d:%d:%d]\n",
4400a586ceMark Shellenbaum		    (u_longlong_t)attr.za_first_integer,
4410a586ceMark Shellenbaum		    (int)ATTR_LENGTH(attr.za_first_integer),
4420a586ceMark Shellenbaum		    (int)ATTR_BSWAP(attr.za_first_integer),
4430a586ceMark Shellenbaum		    (int)ATTR_NUM(attr.za_first_integer));
4440a586ceMark Shellenbaum	}
4450a586ceMark Shellenbaum	zap_cursor_fini(&zc);
4460a586ceMark Shellenbaum}
4470a586ceMark Shellenbaum
4480a586ceMark Shellenbaum/*ARGSUSED*/
4490a586ceMark Shellenbaumstatic void
4500a586ceMark Shellenbaumdump_sa_layouts(objset_t *os, uint64_t object, void *data, size_t size)
4510a586ceMark Shellenbaum{
4520a586ceMark Shellenbaum	zap_cursor_t zc;
4530a586ceMark Shellenbaum	zap_attribute_t attr;
4540a586ceMark Shellenbaum	uint16_t *layout_attrs;
4550a586ceMark Shellenbaum	int i;
4560a586ceMark Shellenbaum
4570a586ceMark Shellenbaum	dump_zap_stats(os, object);
4580a586ceMark Shellenbaum	(void) printf("\n");
4590a586ceMark Shellenbaum
4600a586ceMark Shellenbaum	for (zap_cursor_init(&zc, os, object);
4610a586ceMark Shellenbaum	    zap_cursor_retrieve(&zc, &attr) == 0;
4620a586ceMark Shellenbaum	    zap_cursor_advance(&zc)) {
4630a586ceMark Shellenbaum		(void) printf("\t\t%s = [", attr.za_name);
4640a586ceMark Shellenbaum		if (attr.za_num_integers == 0) {
4650a586ceMark Shellenbaum			(void) printf("\n");
4660a586ceMark Shellenbaum			continue;
4670a586ceMark Shellenbaum		}
4680a586ceMark Shellenbaum
4690a586ceMark Shellenbaum		VERIFY(attr.za_integer_length == 2);
4700a586ceMark Shellenbaum		layout_attrs = umem_zalloc(attr.za_num_integers *
4710a586ceMark Shellenbaum		    attr.za_integer_length, UMEM_NOFAIL);
4720a586ceMark Shellenbaum
4730a586ceMark Shellenbaum		VERIFY(zap_lookup(os, object, attr.za_name,
4740a586ceMark Shellenbaum		    attr.za_integer_length,
4750a586ceMark Shellenbaum		    attr.za_num_integers, layout_attrs) == 0);
4760a586ceMark Shellenbaum
4770a586ceMark Shellenbaum		for (i = 0; i != attr.za_num_integers; i++)
4780a586ceMark Shellenbaum			(void) printf(" %d ", (int)layout_attrs[i]);
4790a586ceMark Shellenbaum		(void) printf("]\n");
4800a586ceMark Shellenbaum		umem_free(layout_attrs,
4810a586ceMark Shellenbaum		    attr.za_num_integers * attr.za_integer_length);
4820a586ceMark Shellenbaum	}
4830a586ceMark Shellenbaum	zap_cursor_fini(&zc);
4840a586ceMark Shellenbaum}
4850a586ceMark Shellenbaum
4860a586ceMark Shellenbaum/*ARGSUSED*/
4870a586ceMark Shellenbaumstatic void
488e743726ahrensdump_zpldir(objset_t *os, uint64_t object, void *data, size_t size)
489e743726ahrens{
490e743726ahrens	zap_cursor_t zc;
491e743726ahrens	zap_attribute_t attr;
492e743726ahrens	const char *typenames[] = {
493e743726ahrens		/* 0 */ "not specified",
494e743726ahrens		/* 1 */ "FIFO",
495e743726ahrens		/* 2 */ "Character Device",
496e743726ahrens		/* 3 */ "3 (invalid)",
497e743726ahrens		/* 4 */ "Directory",
498e743726ahrens		/* 5 */ "5 (invalid)",
499e743726ahrens		/* 6 */ "Block Device",
500e743726ahrens		/* 7 */ "7 (invalid)",
501e743726ahrens		/* 8 */ "Regular File",
502e743726ahrens		/* 9 */ "9 (invalid)",
503e743726ahrens		/* 10 */ "Symbolic Link",
504e743726ahrens		/* 11 */ "11 (invalid)",
505e743726ahrens		/* 12 */ "Socket",
506e743726ahrens		/* 13 */ "Door",
507e743726ahrens		/* 14 */ "Event Port",
508e743726ahrens		/* 15 */ "15 (invalid)",
509e743726ahrens	};
510e743726ahrens
511e743726ahrens	dump_zap_stats(os, object);
512e743726ahrens	(void) printf("\n");
513e743726ahrens
514e743726ahrens	for (zap_cursor_init(&zc, os, object);
515e743726ahrens	    zap_cursor_retrieve(&zc, &attr) == 0;
516e743726ahrens	    zap_cursor_advance(&zc)) {
517e743726ahrens		(void) printf("\t\t%s = %lld (type: %s)\n",
518e743726ahrens		    attr.za_name, ZFS_DIRENT_OBJ(attr.za_first_integer),
519e743726ahrens		    typenames[ZFS_DIRENT_TYPE(attr.za_first_integer)]);
520e743726ahrens	}
521e743726ahrens	zap_cursor_fini(&zc);
522e743726ahrens}
523e743726ahrens
5240713e23George Wilsonint
5250713e23George Wilsonget_dtl_refcount(vdev_t *vd)
5260713e23George Wilson{
5270713e23George Wilson	int refcount = 0;
5280713e23George Wilson
5290713e23George Wilson	if (vd->vdev_ops->vdev_op_leaf) {
5300713e23George Wilson		space_map_t *sm = vd->vdev_dtl_sm;
5310713e23George Wilson
5320713e23George Wilson		if (sm != NULL &&
5330713e23George Wilson		    sm->sm_dbuf->db_size == sizeof (space_map_phys_t))
5340713e23George Wilson			return (1);
5350713e23George Wilson		return (0);
5360713e23George Wilson	}
5370713e23George Wilson
5380713e23George Wilson	for (int c = 0; c < vd->vdev_children; c++)
5390713e23George Wilson		refcount += get_dtl_refcount(vd->vdev_child[c]);
5400713e23George Wilson	return (refcount);
5410713e23George Wilson}
5420713e23George Wilson
5430713e23George Wilsonint
5440713e23George Wilsonget_metaslab_refcount(vdev_t *vd)
5450713e23George Wilson{
5460713e23George Wilson	int refcount = 0;
5470713e23George Wilson
5480713e23George Wilson	if (vd->vdev_top == vd) {
5490713e23George Wilson		for (int m = 0; m < vd->vdev_ms_count; m++) {
5500713e23George Wilson			space_map_t *sm = vd->vdev_ms[m]->ms_sm;
5510713e23George Wilson
5520713e23George Wilson			if (sm != NULL &&
5530713e23George Wilson			    sm->sm_dbuf->db_size == sizeof (space_map_phys_t))
5540713e23George Wilson				refcount++;
5550713e23George Wilson		}
5560713e23George Wilson	}
5570713e23George Wilson	for (int c = 0; c < vd->vdev_children; c++)
5580713e23George Wilson		refcount += get_metaslab_refcount(vd->vdev_child[c]);
5590713e23George Wilson
5600713e23George Wilson	return (refcount);
5610713e23George Wilson}
5620713e23George Wilson
5630713e23George Wilsonstatic int
5640713e23George Wilsonverify_spacemap_refcounts(spa_t *spa)
5650713e23George Wilson{
5662acef22Matthew Ahrens	uint64_t expected_refcount = 0;
5672acef22Matthew Ahrens	uint64_t actual_refcount;
5680713e23George Wilson
5692acef22Matthew Ahrens	(void) feature_get_refcount(spa,
5702acef22Matthew Ahrens	    &spa_feature_table[SPA_FEATURE_SPACEMAP_HISTOGRAM],
5712acef22Matthew Ahrens	    &expected_refcount);
5720713e23George Wilson	actual_refcount = get_dtl_refcount(spa->spa_root_vdev);
5730713e23George Wilson	actual_refcount += get_metaslab_refcount(spa->spa_root_vdev);
5740713e23George Wilson
5750713e23George Wilson	if (expected_refcount != actual_refcount) {
5762acef22Matthew Ahrens		(void) printf("space map refcount mismatch: expected %lld != "
5772acef22Matthew Ahrens		    "actual %lld\n",
5782acef22Matthew Ahrens		    (longlong_t)expected_refcount,
5792acef22Matthew Ahrens		    (longlong_t)actual_refcount);
5800713e23George Wilson		return (2);
5810713e23George Wilson	}
5820713e23George Wilson	return (0);
5830713e23George Wilson}
5840713e23George Wilson
585fa9e406ahrensstatic void
5860713e23George Wilsondump_spacemap(objset_t *os, space_map_t *sm)
587fa9e406ahrens{
588fa9e406ahrens	uint64_t alloc, offset, entry;
5898053a26ck	char *ddata[] = { "ALLOC", "FREE", "CONDENSE", "INVALID",
5908053a26ck			    "INVALID", "INVALID", "INVALID", "INVALID" };
591fa9e406ahrens
5920713e23George Wilson	if (sm == NULL)
593fa9e406ahrens		return;
594fa9e406ahrens
595fa9e406ahrens	/*
596fa9e406ahrens	 * Print out the freelist entries in both encoded and decoded form.
597fa9e406ahrens	 */
598fa9e406ahrens	alloc = 0;
5990713e23George Wilson	for (offset = 0; offset < space_map_length(sm);
6000713e23George Wilson	    offset += sizeof (entry)) {
6010713e23George Wilson		uint8_t mapshift = sm->sm_shift;
6020713e23George Wilson
6030713e23George Wilson		VERIFY0(dmu_read(os, space_map_object(sm), offset,
6047bfdf01Neil Perrin		    sizeof (entry), &entry, DMU_READ_PREFETCH));
605fa9e406ahrens		if (SM_DEBUG_DECODE(entry)) {
6060713e23George Wilson
60787219dbVictor Latushkin			(void) printf("\t    [%6llu] %s: txg %llu, pass %llu\n",
608fa9e406ahrens			    (u_longlong_t)(offset / sizeof (entry)),
609fa9e406ahrens			    ddata[SM_DEBUG_ACTION_DECODE(entry)],
6105ad8204nd			    (u_longlong_t)SM_DEBUG_TXG_DECODE(entry),
6115ad8204nd			    (u_longlong_t)SM_DEBUG_SYNCPASS_DECODE(entry));
612fa9e406ahrens		} else {
61387219dbVictor Latushkin			(void) printf("\t    [%6llu]    %c  range:"
61487219dbVictor Latushkin			    " %010llx-%010llx  size: %06llx\n",
615fa9e406ahrens			    (u_longlong_t)(offset / sizeof (entry)),
616fa9e406ahrens			    SM_TYPE_DECODE(entry) == SM_ALLOC ? 'A' : 'F',
6175ad8204nd			    (u_longlong_t)((SM_OFFSET_DECODE(entry) <<
6180713e23George Wilson			    mapshift) + sm->sm_start),
6195ad8204nd			    (u_longlong_t)((SM_OFFSET_DECODE(entry) <<
6200713e23George Wilson			    mapshift) + sm->sm_start +
6210713e23George Wilson			    (SM_RUN_DECODE(entry) << mapshift)),
6225ad8204nd			    (u_longlong_t)(SM_RUN_DECODE(entry) << mapshift));
623fa9e406ahrens			if (SM_TYPE_DECODE(entry) == SM_ALLOC)
624fa9e406ahrens				alloc += SM_RUN_DECODE(entry) << mapshift;
625fa9e406ahrens			else
626fa9e406ahrens				alloc -= SM_RUN_DECODE(entry) << mapshift;
627fa9e406ahrens		}
628fa9e406ahrens	}
6290713e23George Wilson	if (alloc != space_map_allocated(sm)) {
630fa9e406ahrens		(void) printf("space_map_object alloc (%llu) INCONSISTENT "
631fa9e406ahrens		    "with space map summary (%llu)\n",
6320713e23George Wilson		    (u_longlong_t)space_map_allocated(sm), (u_longlong_t)alloc);
633fa9e406ahrens	}
634fa9e406ahrens}
635fa9e406ahrens
636fa9e406ahrensstatic void
637d6e555bGeorge Wilsondump_metaslab_stats(metaslab_t *msp)
638d6e555bGeorge Wilson{
6393f9d6adLin Ling	char maxbuf[32];
6400713e23George Wilson	range_tree_t *rt = msp->ms_tree;
6410713e23George Wilson	avl_tree_t *t = &msp->ms_size_tree;
6420713e23George Wilson	int free_pct = range_tree_space(rt) * 100 / msp->ms_size;
643d6e555bGeorge Wilson
6440713e23George Wilson	zdb_nicenum(metaslab_block_maxsize(msp), maxbuf);
645d6e555bGeorge Wilson
64687219dbVictor Latushkin	(void) printf("\t %25s %10lu   %7s  %6s   %4s %4d%%\n",
647d6e555bGeorge Wilson	    "segments", avl_numnodes(t), "maxsize", maxbuf,
648d6e555bGeorge Wilson	    "freepct", free_pct);
6490713e23George Wilson	(void) printf("\tIn-memory histogram:\n");
6500713e23George Wilson	dump_histogram(rt->rt_histogram, RANGE_TREE_HISTOGRAM_SIZE, 0);
651d6e555bGeorge Wilson}
652d6e555bGeorge Wilson
653d6e555bGeorge Wilsonstatic void
654fa9e406ahrensdump_metaslab(metaslab_t *msp)
655fa9e406ahrens{
656fa9e406ahrens	vdev_t *vd = msp->ms_group->mg_vd;
657fa9e406ahrens	spa_t *spa = vd->vdev_spa;
6580713e23George Wilson	space_map_t *sm = msp->ms_sm;
6593f9d6adLin Ling	char freebuf[32];
660fa9e406ahrens
6610713e23George Wilson	zdb_nicenum(msp->ms_size - space_map_allocated(sm), freebuf);
662fa9e406ahrens
663fa9e406ahrens	(void) printf(
66487219dbVictor Latushkin	    "\tmetaslab %6llu   offset %12llx   spacemap %6llu   free    %5s\n",
6650713e23George Wilson	    (u_longlong_t)msp->ms_id, (u_longlong_t)msp->ms_start,
6660713e23George Wilson	    (u_longlong_t)space_map_object(sm), freebuf);
667fa9e406ahrens
6680713e23George Wilson	if (dump_opt['m'] > 2 && !dump_opt['L']) {
669d6e555bGeorge Wilson		mutex_enter(&msp->ms_lock);
6700713e23George Wilson		metaslab_load_wait(msp);
6710713e23George Wilson		if (!msp->ms_loaded) {
6720713e23George Wilson			VERIFY0(metaslab_load(msp));
6730713e23George Wilson			range_tree_stat_verify(msp->ms_tree);
6740713e23George Wilson		}
675bc3975bVictor Latushkin		dump_metaslab_stats(msp);
6760713e23George Wilson		metaslab_unload(msp);
677d6e555bGeorge Wilson		mutex_exit(&msp->ms_lock);
678d6e555bGeorge Wilson	}
679d6e555bGeorge Wilson
6800713e23George Wilson	if (dump_opt['m'] > 1 && sm != NULL &&
6812acef22Matthew Ahrens	    spa_feature_is_active(spa, SPA_FEATURE_SPACEMAP_HISTOGRAM)) {
6820713e23George Wilson		/*
6830713e23George Wilson		 * The space map histogram represents free space in chunks
6840713e23George Wilson		 * of sm_shift (i.e. bucket 0 refers to 2^sm_shift).
6850713e23George Wilson		 */
6860713e23George Wilson		(void) printf("\tOn-disk histogram:\n");
6870713e23George Wilson		dump_histogram(sm->sm_phys->smp_histogram,
6880713e23George Wilson		    SPACE_MAP_HISTOGRAM_SIZE(sm), sm->sm_shift);
6890713e23George Wilson	}
6900713e23George Wilson
6910713e23George Wilson	if (dump_opt['d'] > 5 || dump_opt['m'] > 3) {
6920713e23George Wilson		ASSERT(msp->ms_size == (1ULL << vd->vdev_ms_shift));
693d6e555bGeorge Wilson
694d6e555bGeorge Wilson		mutex_enter(&msp->ms_lock);
6950713e23George Wilson		dump_spacemap(spa->spa_meta_objset, msp->ms_sm);
696d6e555bGeorge Wilson		mutex_exit(&msp->ms_lock);
697d6e555bGeorge Wilson	}
69887219dbVictor Latushkin}
699fa9e406ahrens
70087219dbVictor Latushkinstatic void
70187219dbVictor Latushkinprint_vdev_metaslab_header(vdev_t *vd)
70287219dbVictor Latushkin{
70387219dbVictor Latushkin	(void) printf("\tvdev %10llu\n\t%-10s%5llu   %-19s   %-15s   %-10s\n",
70487219dbVictor Latushkin	    (u_longlong_t)vd->vdev_id,
70587219dbVictor Latushkin	    "metaslabs", (u_longlong_t)vd->vdev_ms_count,
70687219dbVictor Latushkin	    "offset", "spacemap", "free");
70787219dbVictor Latushkin	(void) printf("\t%15s   %19s   %15s   %10s\n",
70887219dbVictor Latushkin	    "---------------", "-------------------",
70987219dbVictor Latushkin	    "---------------", "-------------");
710fa9e406ahrens}
711fa9e406ahrens
712fa9e406ahrensstatic void
713fa9e406ahrensdump_metaslabs(spa_t *spa)
714fa9e406ahrens{
71587219dbVictor Latushkin	vdev_t *vd, *rvd = spa->spa_root_vdev;
71687219dbVictor Latushkin	uint64_t m, c = 0, children = rvd->vdev_children;
717fa9e406ahrens
718fa9e406ahrens	(void) printf("\nMetaslabs:\n");
719fa9e406ahrens
72087219dbVictor Latushkin	if (!dump_opt['d'] && zopt_objects > 0) {
72187219dbVictor Latushkin		c = zopt_object[0];
72287219dbVictor Latushkin
72387219dbVictor Latushkin		if (c >= children)
72487219dbVictor Latushkin			(void) fatal("bad vdev id: %llu", (u_longlong_t)c);
725fa9e406ahrens
72687219dbVictor Latushkin		if (zopt_objects > 1) {
72787219dbVictor Latushkin			vd = rvd->vdev_child[c];
72887219dbVictor Latushkin			print_vdev_metaslab_header(vd);
72987219dbVictor Latushkin
73087219dbVictor Latushkin			for (m = 1; m < zopt_objects; m++) {
73187219dbVictor Latushkin				if (zopt_object[m] < vd->vdev_ms_count)
73287219dbVictor Latushkin					dump_metaslab(
73387219dbVictor Latushkin					    vd->vdev_ms[zopt_object[m]]);
73487219dbVictor Latushkin				else
73587219dbVictor Latushkin					(void) fprintf(stderr, "bad metaslab "
73687219dbVictor Latushkin					    "number %llu\n",
73787219dbVictor Latushkin					    (u_longlong_t)zopt_object[m]);
73887219dbVictor Latushkin			}
73987219dbVictor Latushkin			(void) printf("\n");
74087219dbVictor Latushkin			return;
74187219dbVictor Latushkin		}
74287219dbVictor Latushkin		children = c + 1;
74387219dbVictor Latushkin	}
74487219dbVictor Latushkin	for (; c < children; c++) {
74587219dbVictor Latushkin		vd = rvd->vdev_child[c];
74687219dbVictor Latushkin		print_vdev_metaslab_header(vd);
747fa9e406ahrens
748fa9e406ahrens		for (m = 0; m < vd->vdev_ms_count; m++)
749fa9e406ahrens			dump_metaslab(vd->vdev_ms[m]);
750fa9e406ahrens		(void) printf("\n");
751fa9e406ahrens	}
752fa9e406ahrens}
753fa9e406ahrens
754fa9e406ahrensstatic void
755b24ab67Jeff Bonwickdump_dde(const ddt_t *ddt, const ddt_entry_t *dde, uint64_t index)
756b24ab67Jeff Bonwick{
757b24ab67Jeff Bonwick	const ddt_phys_t *ddp = dde->dde_phys;
758b24ab67Jeff Bonwick	const ddt_key_t *ddk = &dde->dde_key;
759b24ab67Jeff Bonwick	char *types[4] = { "ditto", "single", "double", "triple" };
760b24ab67Jeff Bonwick	char blkbuf[BP_SPRINTF_LEN];
761b24ab67Jeff Bonwick	blkptr_t blk;
762b24ab67Jeff Bonwick
763b24ab67Jeff Bonwick	for (int p = 0; p < DDT_PHYS_TYPES; p++, ddp++) {
764b24ab67Jeff Bonwick		if (ddp->ddp_phys_birth == 0)
765b24ab67Jeff Bonwick			continue;
766bbfd46cJeff Bonwick		ddt_bp_create(ddt->ddt_checksum, ddk, ddp, &blk);
767b24ab67Jeff Bonwick		sprintf_blkptr(blkbuf, &blk);
768b24ab67Jeff Bonwick		(void) printf("index %llx refcnt %llu %s %s\n",
769b24ab67Jeff Bonwick		    (u_longlong_t)index, (u_longlong_t)ddp->ddp_refcnt,
770b24ab67Jeff Bonwick		    types[p], blkbuf);
771b24ab67Jeff Bonwick	}
772b24ab67Jeff Bonwick}
773b24ab67Jeff Bonwick
774b24ab67Jeff Bonwickstatic void
775b24ab67Jeff Bonwickdump_dedup_ratio(const ddt_stat_t *dds)
776b24ab67Jeff Bonwick{
777b24ab67Jeff Bonwick	double rL, rP, rD, D, dedup, compress, copies;
778b24ab67Jeff Bonwick
779b24ab67Jeff Bonwick	if (dds->dds_blocks == 0)
780b24ab67Jeff Bonwick		return;
781b24ab67Jeff Bonwick
782b24ab67Jeff Bonwick	rL = (double)dds->dds_ref_lsize;
783b24ab67Jeff Bonwick	rP = (double)dds->dds_ref_psize;
784b24ab67Jeff Bonwick	rD = (double)dds->dds_ref_dsize;
785b24ab67Jeff Bonwick	D = (double)dds->dds_dsize;
786b24ab67Jeff Bonwick
787b24ab67Jeff Bonwick	dedup = rD / D;
788b24ab67Jeff Bonwick	compress = rL / rP;
789b24ab67Jeff Bonwick	copies = rD / rP;
790b24ab67Jeff Bonwick
791b24ab67Jeff Bonwick	(void) printf("dedup = %.2f, compress = %.2f, copies = %.2f, "
792b24ab67Jeff Bonwick	    "dedup * compress / copies = %.2f\n\n",
793b24ab67Jeff Bonwick	    dedup, compress, copies, dedup * compress / copies);
794b24ab67Jeff Bonwick}
795b24ab67Jeff Bonwick
796b24ab67Jeff Bonwickstatic void
797b24ab67Jeff Bonwickdump_ddt(ddt_t *ddt, enum ddt_type type, enum ddt_class class)
798b24ab67Jeff Bonwick{
799b24ab67Jeff Bonwick	char name[DDT_NAMELEN];
800b24ab67Jeff Bonwick	ddt_entry_t dde;
801b24ab67Jeff Bonwick	uint64_t walk = 0;
802b24ab67Jeff Bonwick	dmu_object_info_t doi;
803b24ab67Jeff Bonwick	uint64_t count, dspace, mspace;
804b24ab67Jeff Bonwick	int error;
805b24ab67Jeff Bonwick
806b24ab67Jeff Bonwick	error = ddt_object_info(ddt, type, class, &doi);
807b24ab67Jeff Bonwick
808b24ab67Jeff Bonwick	if (error == ENOENT)
809b24ab67Jeff Bonwick		return;
810b24ab67Jeff Bonwick	ASSERT(error == 0);
811b24ab67Jeff Bonwick
8127448a07George Wilson	if ((count = ddt_object_count(ddt, type, class)) == 0)
8137448a07George Wilson		return;
8147448a07George Wilson
815b24ab67Jeff Bonwick	dspace = doi.doi_physical_blocks_512 << 9;
816b24ab67Jeff Bonwick	mspace = doi.doi_fill_count * doi.doi_data_block_size;
817b24ab67Jeff Bonwick
818b24ab67Jeff Bonwick	ddt_object_name(ddt, type, class, name);
819b24ab67Jeff Bonwick
820b24ab67Jeff Bonwick	(void) printf("%s: %llu entries, size %llu on disk, %llu in core\n",
821b24ab67Jeff Bonwick	    name,
822b24ab67Jeff Bonwick	    (u_longlong_t)count,
823b24ab67Jeff Bonwick	    (u_longlong_t)(dspace / count),
824b24ab67Jeff Bonwick	    (u_longlong_t)(mspace / count));
825b24ab67Jeff Bonwick
826b24ab67Jeff Bonwick	if (dump_opt['D'] < 3)
827b24ab67Jeff Bonwick		return;
828b24ab67Jeff Bonwick
8299eb19f4George Wilson	zpool_dump_ddt(NULL, &ddt->ddt_histogram[type][class]);
830b24ab67Jeff Bonwick
831b24ab67Jeff Bonwick	if (dump_opt['D'] < 4)
832b24ab67Jeff Bonwick		return;
833b24ab67Jeff Bonwick
834b24ab67Jeff Bonwick	if (dump_opt['D'] < 5 && class == DDT_CLASS_UNIQUE)
835b24ab67Jeff Bonwick		return;
836b24ab67Jeff Bonwick
837b24ab67Jeff Bonwick	(void) printf("%s contents:\n\n", name);
838b24ab67Jeff Bonwick
839bbfd46cJeff Bonwick	while ((error = ddt_object_walk(ddt, type, class, &walk, &dde)) == 0)
840b24ab67Jeff Bonwick		dump_dde(ddt, &dde, walk);
841b24ab67Jeff Bonwick
842b24ab67Jeff Bonwick	ASSERT(error == ENOENT);
843b24ab67Jeff Bonwick
844b24ab67Jeff Bonwick	(void) printf("\n");
845b24ab67Jeff Bonwick}
846b24ab67Jeff Bonwick
847b24ab67Jeff Bonwickstatic void
848b24ab67Jeff Bonwickdump_all_ddts(spa_t *spa)
849b24ab67Jeff Bonwick{
850b24ab67Jeff Bonwick	ddt_histogram_t ddh_total = { 0 };
851b24ab67Jeff Bonwick	ddt_stat_t dds_total = { 0 };
852b24ab67Jeff Bonwick
853b24ab67Jeff Bonwick	for (enum zio_checksum c = 0; c < ZIO_CHECKSUM_FUNCTIONS; c++) {
854b24ab67Jeff Bonwick		ddt_t *ddt = spa->spa_ddt[c];
855b24ab67Jeff Bonwick		for (enum ddt_type type = 0; type < DDT_TYPES; type++) {
856b24ab67Jeff Bonwick			for (enum ddt_class class = 0; class < DDT_CLASSES;
857b24ab67Jeff Bonwick			    class++) {
858b24ab67Jeff Bonwick				dump_ddt(ddt, type, class);
859b24ab67Jeff Bonwick			}
860b24ab67Jeff Bonwick		}
861b24ab67Jeff Bonwick	}
862b24ab67Jeff Bonwick
8639eb19f4George Wilson	ddt_get_dedup_stats(spa, &dds_total);
864b24ab67Jeff Bonwick
865b24ab67Jeff Bonwick	if (dds_total.dds_blocks == 0) {
866b24ab67Jeff Bonwick		(void) printf("All DDTs are empty\n");
867b24ab67Jeff Bonwick		return;
868b24ab67Jeff Bonwick	}
869b24ab67Jeff Bonwick
870b24ab67Jeff Bonwick	(void) printf("\n");
871b24ab67Jeff Bonwick
872b24ab67Jeff Bonwick	if (dump_opt['D'] > 1) {
873b24ab67Jeff Bonwick		(void) printf("DDT histogram (aggregated over all DDTs):\n");
8749eb19f4George Wilson		ddt_get_dedup_histogram(spa, &ddh_total);
8759eb19f4George Wilson		zpool_dump_ddt(&dds_total, &ddh_total);
876b24ab67Jeff Bonwick	}
877b24ab67Jeff Bonwick
878b24ab67Jeff Bonwick	dump_dedup_ratio(&dds_total);
879b24ab67Jeff Bonwick}
880b24ab67Jeff Bonwick
881b24ab67Jeff Bonwickstatic void
8820713e23George Wilsondump_dtl_seg(void *arg, uint64_t start, uint64_t size)
8838ad4d6dJeff Bonwick{
8840713e23George Wilson	char *prefix = arg;
8858ad4d6dJeff Bonwick
8868ad4d6dJeff Bonwick	(void) printf("%s [%llu,%llu) length %llu\n",
8878ad4d6dJeff Bonwick	    prefix,
8888ad4d6dJeff Bonwick	    (u_longlong_t)start,
8898ad4d6dJeff Bonwick	    (u_longlong_t)(start + size),
8908ad4d6dJeff Bonwick	    (u_longlong_t)(size));
8918ad4d6dJeff Bonwick}
8928ad4d6dJeff Bonwick
8938ad4d6dJeff Bonwickstatic void
894fa9e406ahrensdump_dtl(vdev_t *vd, int indent)
895fa9e406ahrens{
8968ad4d6dJeff Bonwick	spa_t *spa = vd->vdev_spa;
8978ad4d6dJeff Bonwick	boolean_t required;
8988ad4d6dJeff Bonwick	char *name[DTL_TYPES] = { "missing", "partial", "scrub", "outage" };
8998ad4d6dJeff Bonwick	char prefix[256];
9008ad4d6dJeff Bonwick
9018f18d1fGeorge Wilson	spa_vdev_state_enter(spa, SCL_NONE);
9028ad4d6dJeff Bonwick	required = vdev_dtl_required(vd);
9038ad4d6dJeff Bonwick	(void) spa_vdev_state_exit(spa, NULL, 0);
904fa9e406ahrens
905fa9e406ahrens	if (indent == 0)
906fa9e406ahrens		(void) printf("\nDirty time logs:\n\n");
907fa9e406ahrens
9088ad4d6dJeff Bonwick	(void) printf("\t%*s%s [%s]\n", indent, "",
909e14bb32Jeff Bonwick	    vd->vdev_path ? vd->vdev_path :
9108ad4d6dJeff Bonwick	    vd->vdev_parent ? vd->vdev_ops->vdev_op_type : spa_name(spa),
9118ad4d6dJeff Bonwick	    required ? "DTL-required" : "DTL-expendable");
912fa9e406ahrens
9138ad4d6dJeff Bonwick	for (int t = 0; t < DTL_TYPES; t++) {
9140713e23George Wilson		range_tree_t *rt = vd->vdev_dtl[t];
9150713e23George Wilson		if (range_tree_space(rt) == 0)
9168ad4d6dJeff Bonwick			continue;
9178ad4d6dJeff Bonwick		(void) snprintf(prefix, sizeof (prefix), "\t%*s%s",
9188ad4d6dJeff Bonwick		    indent + 2, "", name[t]);
9190713e23George Wilson		mutex_enter(rt->rt_lock);
9200713e23George Wilson		range_tree_walk(rt, dump_dtl_seg, prefix);
9210713e23George Wilson		mutex_exit(rt->rt_lock);
9228ad4d6dJeff Bonwick		if (dump_opt['d'] > 5 && vd->vdev_children == 0)
9230713e23George Wilson			dump_spacemap(spa->spa_meta_objset, vd->vdev_dtl_sm);
924fa9e406ahrens	}
925fa9e406ahrens
9268ad4d6dJeff Bonwick	for (int c = 0; c < vd->vdev_children; c++)
927fa9e406ahrens		dump_dtl(vd->vdev_child[c], indent + 4);
928fa9e406ahrens}
929fa9e406ahrens
9308f18d1fGeorge Wilsonstatic void
9318f18d1fGeorge Wilsondump_history(spa_t *spa)
9328f18d1fGeorge Wilson{
9338f18d1fGeorge Wilson	nvlist_t **events = NULL;
9348f18d1fGeorge Wilson	char buf[SPA_MAXBLOCKSIZE];
935e4161dfVictor Latushkin	uint64_t resid, len, off = 0;
9368f18d1fGeorge Wilson	uint_t num = 0;
9378f18d1fGeorge Wilson	int error;
9388f18d1fGeorge Wilson	time_t tsec;
9398f18d1fGeorge Wilson	struct tm t;
9408f18d1fGeorge Wilson	char tbuf[30];
9418f18d1fGeorge Wilson	char internalstr[MAXPATHLEN];
9428f18d1fGeorge Wilson
9438f18d1fGeorge Wilson	do {
944e4161dfVictor Latushkin		len = sizeof (buf);
945e4161dfVictor Latushkin
9468f18d1fGeorge Wilson		if ((error = spa_history_get(spa, &off, &len, buf)) != 0) {
947