zdb.c revision ed61ec1da9132e570b0853386d0f78a32f852cd2
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.
2429bdd2fPavel Zakharov * Copyright (c) 2011, 2016 by Delphix. All rights reserved.
25c3d26abMatthew Ahrens * Copyright (c) 2014 Integros [integros.com]
26ed61ec1Yuri Pankov * Copyright 2017 Nexenta Systems, Inc.
27fa9e406ahrens */
28fa9e406ahrens
29fa9e406ahrens#include <stdio.h>
30490d05bMatthew Ahrens#include <unistd.h>
31004388ecasper#include <stdio_ext.h>
32fa9e406ahrens#include <stdlib.h>
3344cd46cbillm#include <ctype.h>
34fa9e406ahrens#include <sys/zfs_context.h>
35fa9e406ahrens#include <sys/spa.h>
36fa9e406ahrens#include <sys/spa_impl.h>
37fa9e406ahrens#include <sys/dmu.h>
38fa9e406ahrens#include <sys/zap.h>
39fa9e406ahrens#include <sys/fs/zfs.h>
40fa9e406ahrens#include <sys/zfs_znode.h>
410a586ceMark Shellenbaum#include <sys/zfs_sa.h>
420a586ceMark Shellenbaum#include <sys/sa.h>
430a586ceMark Shellenbaum#include <sys/sa_impl.h>
44fa9e406ahrens#include <sys/vdev.h>
45fa9e406ahrens#include <sys/vdev_impl.h>
46fa9e406ahrens#include <sys/metaslab_impl.h>
47fa9e406ahrens#include <sys/dmu_objset.h>
48fa9e406ahrens#include <sys/dsl_dir.h>
49fa9e406ahrens#include <sys/dsl_dataset.h>
50fa9e406ahrens#include <sys/dsl_pool.h>
51fa9e406ahrens#include <sys/dbuf.h>
52fa9e406ahrens#include <sys/zil.h>
53fa9e406ahrens#include <sys/zil_impl.h>
54fa9e406ahrens#include <sys/stat.h>
55fa9e406ahrens#include <sys/resource.h>
56fa9e406ahrens#include <sys/dmu_traverse.h>
57fa9e406ahrens#include <sys/zio_checksum.h>
58fa9e406ahrens#include <sys/zio_compress.h>
59e0d35c4marks#include <sys/zfs_fuid.h>
6088b7b0fMatthew Ahrens#include <sys/arc.h>
61b24ab67Jeff Bonwick#include <sys/ddt.h>
62ad135b5Christopher Siden#include <sys/zfeature.h>
634445fffMatthew Ahrens#include <zfs_comutil.h>
64de6628fck#undef verify
65de6628fck#include <libzfs.h>
66fa9e406ahrens
67e690fb2Christopher Siden#define	ZDB_COMPRESS_NAME(idx) ((idx) < ZIO_COMPRESS_FUNCTIONS ?	\
68e690fb2Christopher Siden	zio_compress_table[(idx)].ci_name : "UNKNOWN")
69e690fb2Christopher Siden#define	ZDB_CHECKSUM_NAME(idx) ((idx) < ZIO_CHECKSUM_FUNCTIONS ?	\
70e690fb2Christopher Siden	zio_checksum_table[(idx)].ci_name : "UNKNOWN")
71e690fb2Christopher Siden#define	ZDB_OT_NAME(idx) ((idx) < DMU_OT_NUMTYPES ?	\
72e690fb2Christopher Siden	dmu_ot[(idx)].ot_name : DMU_OT_IS_VALID(idx) ?	\
73e690fb2Christopher Siden	dmu_ot_byteswap[DMU_OT_BYTESWAP(idx)].ob_name : "UNKNOWN")
74e690fb2Christopher Siden#define	ZDB_OT_TYPE(idx) ((idx) < DMU_OT_NUMTYPES ? (idx) :		\
75e690fb2Christopher Siden	(((idx) == DMU_OTN_ZAP_DATA || (idx) == DMU_OTN_ZAP_METADATA) ?	\
76e690fb2Christopher Siden	DMU_OT_ZAP_OTHER : DMU_OT_NUMTYPES))
776de8f41Victor Latushkin
78feef89cVictor Latushkin#ifndef lint
794dd77f9Matthew Ahrensextern int reference_tracking_enable;
807fd05acMatthew Ahrensextern boolean_t zfs_recover;
8106be980Matthew Ahrensextern uint64_t zfs_arc_max, zfs_arc_meta_limit;
82f7950bfMatthew Ahrensextern int zfs_vdev_async_read_max_active;
83feef89cVictor Latushkin#else
844dd77f9Matthew Ahrensint reference_tracking_enable;
857fd05acMatthew Ahrensboolean_t zfs_recover;
8606be980Matthew Ahrensuint64_t zfs_arc_max, zfs_arc_meta_limit;
87f7950bfMatthew Ahrensint zfs_vdev_async_read_max_active;
88feef89cVictor Latushkin#endif
89feef89cVictor Latushkin
90fa9e406ahrensconst char cmdname[] = "zdb";
91fa9e406ahrensuint8_t dump_opt[256];
92fa9e406ahrens
93fa9e406ahrenstypedef void object_viewer_t(objset_t *, uint64_t, void *data, size_t size);
94fa9e406ahrens
95fa9e406ahrensextern void dump_intent_log(zilog_t *);
96fa9e406ahrensuint64_t *zopt_object = NULL;
97fa9e406ahrensint zopt_objects = 0;
98de6628fcklibzfs_handle_t *g_zfs;
9906be980Matthew Ahrensuint64_t max_inflight = 1000;
100fa9e406ahrens
101732885fMatthew Ahrensstatic void snprintf_blkptr_compact(char *, size_t, const blkptr_t *);
102732885fMatthew Ahrens
103fa9e406ahrens/*
104fa9e406ahrens * These libumem hooks provide a reasonable set of defaults for the allocator's
105fa9e406ahrens * debugging facilities.
106fa9e406ahrens */
107fa9e406ahrensconst char *
108fa9e406ahrens_umem_debug_init()
109fa9e406ahrens{
110fa9e406ahrens	return ("default,verbose"); /* $UMEM_DEBUG setting */
111fa9e406ahrens}
112fa9e406ahrens
113fa9e406ahrensconst char *
114fa9e406ahrens_umem_logging_init(void)
115fa9e406ahrens{
116fa9e406ahrens	return ("fail,contents"); /* $UMEM_LOGGING setting */
117fa9e406ahrens}
118fa9e406ahrens
119fa9e406ahrensstatic void
120fa9e406ahrensusage(void)
121fa9e406ahrens{
122fa9e406ahrens	(void) fprintf(stderr,
123ed61ec1Yuri Pankov	    "Usage:\t%s [-AbcdDFGhiLMPsvX] [-e [-p <path> ...]] "
124ed61ec1Yuri Pankov	    "[-I <inflight I/Os>]\n"
125ed61ec1Yuri Pankov	    "\t\t[-o <var>=<value>]... [-t <txg>] [-U <cache>] [-x <dumpdir>]\n"
126ed61ec1Yuri Pankov	    "\t\t[<poolname> [<object> ...]]\n"
127ed61ec1Yuri Pankov	    "\t%s [-AdiPv] [-e [-p <path> ...]] [-U <cache>] <dataset> "
128ed61ec1Yuri Pankov	    "[<object> ...]\n"
129ed61ec1Yuri Pankov	    "\t%s -C [-A] [-U <cache>]\n"
130ed61ec1Yuri Pankov	    "\t%s -l [-Aqu] <device>\n"
131ed61ec1Yuri Pankov	    "\t%s -m [-AFLPX] [-e [-p <path> ...]] [-t <txg>] [-U <cache>]\n"
132ed61ec1Yuri Pankov	    "\t\t<poolname> [<vdev> [<metaslab> ...]]\n"
133ed61ec1Yuri Pankov	    "\t%s -O <dataset> <path>\n"
134ed61ec1Yuri Pankov	    "\t%s -R [-A] [-e [-p <path> ...]] [-U <cache>]\n"
135ed61ec1Yuri Pankov	    "\t\t<poolname> <vdev>:<offset>:<size>[:<flags>]\n"
136ed61ec1Yuri Pankov	    "\t%s -S [-AP] [-e [-p <path> ...]] [-U <cache>] <poolname>\n\n",
137ed61ec1Yuri Pankov	    cmdname, cmdname, cmdname, cmdname, cmdname, cmdname, cmdname,
138ed61ec1Yuri Pankov	    cmdname);
1393ad6c7fVictor Latushkin
1403ad6c7fVictor Latushkin	(void) fprintf(stderr, "    Dataset name must include at least one "
1413ad6c7fVictor Latushkin	    "separator character '/' or '@'\n");
1423ad6c7fVictor Latushkin	(void) fprintf(stderr, "    If dataset name is specified, only that "
1433ad6c7fVictor Latushkin	    "dataset is dumped\n");
1443ad6c7fVictor Latushkin	(void) fprintf(stderr, "    If object numbers are specified, only "
1453ad6c7fVictor Latushkin	    "those objects are dumped\n\n");
1463ad6c7fVictor Latushkin	(void) fprintf(stderr, "    Options to control amount of output:\n");
1473ad6c7fVictor Latushkin	(void) fprintf(stderr, "        -b block statistics\n");
1483ad6c7fVictor Latushkin	(void) fprintf(stderr, "        -c checksum all metadata (twice for "
1496365109Victor Latushkin	    "all data) blocks\n");
150ed61ec1Yuri Pankov	(void) fprintf(stderr, "        -C config (or cachefile if alone)\n");
151ed61ec1Yuri Pankov	(void) fprintf(stderr, "        -d dataset(s)\n");
152f0ba89bJeff Bonwick	(void) fprintf(stderr, "        -D dedup statistics\n");
153ed61ec1Yuri Pankov	(void) fprintf(stderr, "        -h pool history\n");
154ed61ec1Yuri Pankov	(void) fprintf(stderr, "        -i intent logs\n");
15564723e3Yuri Pankov	(void) fprintf(stderr, "        -l read label contents\n");
15682a0a98Victor Latushkin	(void) fprintf(stderr, "        -L disable leak tracking (do not "
15782a0a98Victor Latushkin	    "load spacemaps)\n");
158ed61ec1Yuri Pankov	(void) fprintf(stderr, "        -m metaslabs\n");
159ed61ec1Yuri Pankov	(void) fprintf(stderr, "        -M metaslab groups\n");
160ed61ec1Yuri Pankov	(void) fprintf(stderr, "        -O perform object lookups by path\n");
161d41e764ek	(void) fprintf(stderr, "        -R read and display block from a "
162ed61ec1Yuri Pankov	    "device\n");
163ed61ec1Yuri Pankov	(void) fprintf(stderr, "        -s report stats on zdb's I/O\n");
164ed61ec1Yuri Pankov	(void) fprintf(stderr, "        -S simulate dedup to measure effect\n");
165ed61ec1Yuri Pankov	(void) fprintf(stderr, "        -v verbose (applies to all "
166ed61ec1Yuri Pankov	    "others)\n\n");
1673ad6c7fVictor Latushkin	(void) fprintf(stderr, "    Below options are intended for use "
168df15e41Matthew Ahrens	    "with other options:\n");
169feef89cVictor Latushkin	(void) fprintf(stderr, "        -A ignore assertions (-A), enable "
170feef89cVictor Latushkin	    "panic recovery (-AA) or both (-AAA)\n");
1713ad6c7fVictor Latushkin	(void) fprintf(stderr, "        -e pool is exported/destroyed/"
1723ad6c7fVictor Latushkin	    "has altroot/not in a cachefile\n");
173ed61ec1Yuri Pankov	(void) fprintf(stderr, "        -F attempt automatic rewind within "
174ed61ec1Yuri Pankov	    "safe range of transaction groups\n");
175ed61ec1Yuri Pankov	(void) fprintf(stderr, "        -G dump zfs_dbgmsg buffer before "
176ed61ec1Yuri Pankov	    "exiting\n");
1772e4c998George Wilson	(void) fprintf(stderr, "        -I <number of inflight I/Os> -- "
178df15e41Matthew Ahrens	    "specify the maximum number of "
179df15e41Matthew Ahrens	    "checksumming I/Os [default is 200]\n");
1800e60744Pavel Zakharov	(void) fprintf(stderr, "        -o <variable>=<value> set global "
1810e60744Pavel Zakharov	    "variable to an unsigned 32-bit integer value\n");
182ed61ec1Yuri Pankov	(void) fprintf(stderr, "        -p <path> -- use one or more with "
183ed61ec1Yuri Pankov	    "-e to specify path to vdev dir\n");
184ed61ec1Yuri Pankov	(void) fprintf(stderr, "        -P print numbers in parseable form\n");
18564723e3Yuri Pankov	(void) fprintf(stderr, "        -q don't print label contents\n");
186ed61ec1Yuri Pankov	(void) fprintf(stderr, "        -t <txg> -- highest txg to use when "
187ed61ec1Yuri Pankov	    "searching for uberblocks\n");
18864723e3Yuri Pankov	(void) fprintf(stderr, "        -u uberblock\n");
189ed61ec1Yuri Pankov	(void) fprintf(stderr, "        -U <cachefile_path> -- use alternate "
190ed61ec1Yuri Pankov	    "cachefile\n");
191ed61ec1Yuri Pankov	(void) fprintf(stderr, "        -x <dumpdir> -- "
192ed61ec1Yuri Pankov	    "dump all read blocks into specified directory\n");
193ed61ec1Yuri Pankov	(void) fprintf(stderr, "        -X attempt extreme rewind (does not "
194ed61ec1Yuri Pankov	    "work with dataset)\n\n");
195fa9e406ahrens	(void) fprintf(stderr, "Specify an option more than once (e.g. -bb) "
196fa9e406ahrens	    "to make only that option verbose\n");
197fa9e406ahrens	(void) fprintf(stderr, "Default is to dump everything non-verbosely\n");
198fa9e406ahrens	exit(1);
199fa9e406ahrens}
200fa9e406ahrens
20129bdd2fPavel Zakharovstatic void
20229bdd2fPavel Zakharovdump_debug_buffer()
20329bdd2fPavel Zakharov{
20429bdd2fPavel Zakharov	if (dump_opt['G']) {
20529bdd2fPavel Zakharov		(void) printf("\n");
20629bdd2fPavel Zakharov		zfs_dbgmsg_print("zdb");
20729bdd2fPavel Zakharov	}
20829bdd2fPavel Zakharov}
20929bdd2fPavel Zakharov
210ccba080Rich Morris/*
211ccba080Rich Morris * Called for usage errors that are discovered after a call to spa_open(),
212ccba080Rich Morris * dmu_bonus_hold(), or pool_match().  abort() is called for other errors.
213ccba080Rich Morris */
214ccba080Rich Morris
215fa9e406ahrensstatic void
216fa9e406ahrensfatal(const char *fmt, ...)
217fa9e406ahrens{
218fa9e406ahrens	va_list ap;
219fa9e406ahrens
220fa9e406ahrens	va_start(ap, fmt);
221fa9e406ahrens	(void) fprintf(stderr, "%s: ", cmdname);
222fa9e406ahrens	(void) vfprintf(stderr, fmt, ap);
223fa9e406ahrens	va_end(ap);
224fa9e406ahrens	(void) fprintf(stderr, "\n");
225fa9e406ahrens
22629bdd2fPavel Zakharov	dump_debug_buffer();
22729bdd2fPavel Zakharov
228ccba080Rich Morris	exit(1);
229fa9e406ahrens}
230fa9e406ahrens
231fa9e406ahrens/* ARGSUSED */
232fa9e406ahrensstatic void
233fa9e406ahrensdump_packed_nvlist(objset_t *os, uint64_t object, void *data, size_t size)
234fa9e406ahrens{
235fa9e406ahrens	nvlist_t *nv;
236fa9e406ahrens	size_t nvsize = *(uint64_t *)data;
237fa9e406ahrens	char *packed = umem_alloc(nvsize, UMEM_NOFAIL);
238fa9e406ahrens
2397bfdf01Neil Perrin	VERIFY(0 == dmu_read(os, object, 0, nvsize, packed, DMU_READ_PREFETCH));
240fa9e406ahrens
241fa9e406ahrens	VERIFY(nvlist_unpack(packed, nvsize, &nv, 0) == 0);
242fa9e406ahrens
243fa9e406ahrens	umem_free(packed, nvsize);
244fa9e406ahrens
245fa9e406ahrens	dump_nvlist(nv, 8);
246fa9e406ahrens
247fa9e406ahrens	nvlist_free(nv);
248fa9e406ahrens}
249fa9e406ahrens
2504445fffMatthew Ahrens/* ARGSUSED */
2514445fffMatthew Ahrensstatic void
2524445fffMatthew Ahrensdump_history_offsets(objset_t *os, uint64_t object, void *data, size_t size)
2534445fffMatthew Ahrens{
2544445fffMatthew Ahrens	spa_history_phys_t *shp = data;
2554445fffMatthew Ahrens
2564445fffMatthew Ahrens	if (shp == NULL)
2574445fffMatthew Ahrens		return;
2584445fffMatthew Ahrens
2594445fffMatthew Ahrens	(void) printf("\t\tpool_create_len = %llu\n",
2604445fffMatthew Ahrens	    (u_longlong_t)shp->sh_pool_create_len);
2614445fffMatthew Ahrens	(void) printf("\t\tphys_max_off = %llu\n",
2624445fffMatthew Ahrens	    (u_longlong_t)shp->sh_phys_max_off);
2634445fffMatthew Ahrens	(void) printf("\t\tbof = %llu\n",
2644445fffMatthew Ahrens	    (u_longlong_t)shp->sh_bof);
2654445fffMatthew Ahrens	(void) printf("\t\teof = %llu\n",
2664445fffMatthew Ahrens	    (u_longlong_t)shp->sh_eof);
2674445fffMatthew Ahrens	(void) printf("\t\trecords_lost = %llu\n",
2684445fffMatthew Ahrens	    (u_longlong_t)shp->sh_records_lost);
2694445fffMatthew Ahrens}
2704445fffMatthew Ahrens
2713f9d6adLin Lingstatic void
2723f9d6adLin Lingzdb_nicenum(uint64_t num, char *buf)
2733f9d6adLin Ling{
2743f9d6adLin Ling	if (dump_opt['P'])
2753f9d6adLin Ling		(void) sprintf(buf, "%llu", (longlong_t)num);
2763f9d6adLin Ling	else
2773f9d6adLin Ling		nicenum(num, buf);
2783f9d6adLin Ling}
2793f9d6adLin Ling
280490d05bMatthew Ahrensconst char histo_stars[] = "****************************************";
281490d05bMatthew Ahrensconst int histo_width = sizeof (histo_stars) - 1;
282fa9e406ahrens
283fa9e406ahrensstatic void
2840713e23George Wilsondump_histogram(const uint64_t *histo, int size, int offset)
285fa9e406ahrens{
286fa9e406ahrens	int i;
287490d05bMatthew Ahrens	int minidx = size - 1;
288fa9e406ahrens	int maxidx = 0;
289fa9e406ahrens	uint64_t max = 0;
290fa9e406ahrens
291490d05bMatthew Ahrens	for (i = 0; i < size; i++) {
292fa9e406ahrens		if (histo[i] > max)
293fa9e406ahrens			max = histo[i];
294fa9e406ahrens		if (histo[i] > 0 && i > maxidx)
295fa9e406ahrens			maxidx = i;
296fa9e406ahrens		if (histo[i] > 0 && i < minidx)
297fa9e406ahrens			minidx = i;
298fa9e406ahrens	}
299fa9e406ahrens
300490d05bMatthew Ahrens	if (max < histo_width)
301490d05bMatthew Ahrens		max = histo_width;
302fa9e406ahrens
303490d05bMatthew Ahrens	for (i = minidx; i <= maxidx; i++) {
304490d05bMatthew Ahrens		(void) printf("\t\t\t%3u: %6llu %s\n",
3050713e23George Wilson		    i + offset, (u_longlong_t)histo[i],
306490d05bMatthew Ahrens		    &histo_stars[(max - histo[i]) * histo_width / max]);
307490d05bMatthew Ahrens	}
308fa9e406ahrens}
309fa9e406ahrens
310fa9e406ahrensstatic void
311fa9e406ahrensdump_zap_stats(objset_t *os, uint64_t object)
312fa9e406ahrens{
313fa9e406ahrens	int error;
314fa9e406ahrens	zap_stats_t zs;
315fa9e406ahrens
316fa9e406ahrens	error = zap_get_stats(os, object, &zs);
317fa9e406ahrens	if (error)
318fa9e406ahrens		return;
319fa9e406ahrens
320fa9e406ahrens	if (zs.zs_ptrtbl_len == 0) {
321fa9e406ahrens		ASSERT(zs.zs_num_blocks == 1);
322fa9e406ahrens		(void) printf("\tmicrozap: %llu bytes, %llu entries\n",
323fa9e406ahrens		    (u_longlong_t)zs.zs_blocksize,
324fa9e406ahrens		    (u_longlong_t)zs.zs_num_entries);
325fa9e406ahrens		return;
326fa9e406ahrens	}
327fa9e406ahrens
328fa9e406ahrens	(void) printf("\tFat ZAP stats:\n");
3298248818nd
3308248818nd	(void) printf("\t\tPointer table:\n");
3318248818nd	(void) printf("\t\t\t%llu elements\n",
332fa9e406ahrens	    (u_longlong_t)zs.zs_ptrtbl_len);
3338248818nd	(void) printf("\t\t\tzt_blk: %llu\n",
3348248818nd	    (u_longlong_t)zs.zs_ptrtbl_zt_blk);
3358248818nd	(void) printf("\t\t\tzt_numblks: %llu\n",
3368248818nd	    (u_longlong_t)zs.zs_ptrtbl_zt_numblks);
3378248818nd	(void) printf("\t\t\tzt_shift: %llu\n",
3388248818nd	    (u_longlong_t)zs.zs_ptrtbl_zt_shift);
3398248818nd	(void) printf("\t\t\tzt_blks_copied: %llu\n",
3408248818nd	    (u_longlong_t)zs.zs_ptrtbl_blks_copied);
3418248818nd	(void) printf("\t\t\tzt_nextblk: %llu\n",
3428248818nd	    (u_longlong_t)zs.zs_ptrtbl_nextblk);
3438248818nd
344fa9e406ahrens	(void) printf("\t\tZAP entries: %llu\n",
345fa9e406ahrens	    (u_longlong_t)zs.zs_num_entries);
346fa9e406ahrens	(void) printf("\t\tLeaf blocks: %llu\n",
347fa9e406ahrens	    (u_longlong_t)zs.zs_num_leafs);
348fa9e406ahrens	(void) printf("\t\tTotal blocks: %llu\n",
349fa9e406ahrens	    (u_longlong_t)zs.zs_num_blocks);
3508248818nd	(void) printf("\t\tzap_block_type: 0x%llx\n",
3518248818nd	    (u_longlong_t)zs.zs_block_type);
3528248818nd	(void) printf("\t\tzap_magic: 0x%llx\n",
3538248818nd	    (u_longlong_t)zs.zs_magic);
3548248818nd	(void) printf("\t\tzap_salt: 0x%llx\n",
3558248818nd	    (u_longlong_t)zs.zs_salt);
356fa9e406ahrens
357fa9e406ahrens	(void) printf("\t\tLeafs with 2^n pointers:\n");
3580713e23George Wilson	dump_histogram(zs.zs_leafs_with_2n_pointers, ZAP_HISTOGRAM_SIZE, 0);
359fa9e406ahrens
360fa9e406ahrens	(void) printf("\t\tBlocks with n*5 entries:\n");
3610713e23George Wilson	dump_histogram(zs.zs_blocks_with_n5_entries, ZAP_HISTOGRAM_SIZE, 0);
362fa9e406ahrens
363fa9e406ahrens	(void) printf("\t\tBlocks n/10 full:\n");
3640713e23George Wilson	dump_histogram(zs.zs_blocks_n_tenths_full, ZAP_HISTOGRAM_SIZE, 0);
365fa9e406ahrens
366fa9e406ahrens	(void) printf("\t\tEntries with n chunks:\n");
3670713e23George Wilson	dump_histogram(zs.zs_entries_using_n_chunks, ZAP_HISTOGRAM_SIZE, 0);
368fa9e406ahrens
369fa9e406ahrens	(void) printf("\t\tBuckets with n entries:\n");
3700713e23George Wilson	dump_histogram(zs.zs_buckets_with_n_entries, ZAP_HISTOGRAM_SIZE, 0);
371fa9e406ahrens}
372fa9e406ahrens
373fa9e406ahrens/*ARGSUSED*/
374fa9e406ahrensstatic void
375fa9e406ahrensdump_none(objset_t *os, uint64_t object, void *data, size_t size)
376fa9e406ahrens{
377fa9e406ahrens}
378fa9e406ahrens
379fa9e406ahrens/*ARGSUSED*/
3806de8f41Victor Latushkinstatic void
3816de8f41Victor Latushkindump_unknown(objset_t *os, uint64_t object, void *data, size_t size)
3826de8f41Victor Latushkin{
3836de8f41Victor Latushkin	(void) printf("\tUNKNOWN OBJECT TYPE\n");
3846de8f41Victor Latushkin}
3856de8f41Victor Latushkin
3866de8f41Victor Latushkin/*ARGSUSED*/
387fa9e406ahrensvoid
388fa9e406ahrensdump_uint8(objset_t *os, uint64_t object, void *data, size_t size)
389fa9e406ahrens{
390fa9e406ahrens}
391fa9e406ahrens
392fa9e406ahrens/*ARGSUSED*/
393fa9e406ahrensstatic void
394fa9e406ahrensdump_uint64(objset_t *os, uint64_t object, void *data, size_t size)
395fa9e406ahrens{
396fa9e406ahrens}
397fa9e406ahrens
398fa9e406ahrens/*ARGSUSED*/
399fa9e406ahrensstatic void
400fa9e406ahrensdump_zap(objset_t *os, uint64_t object, void *data, size_t size)
401fa9e406ahrens{
402fa9e406ahrens	zap_cursor_t zc;
403fa9e406ahrens	zap_attribute_t attr;
404fa9e406ahrens	void *prop;
405fa9e406ahrens	int i;
406fa9e406ahrens
407fa9e406ahrens	dump_zap_stats(os, object);
408fa9e406ahrens	(void) printf("\n");
409fa9e406ahrens
410fa9e406ahrens	for (zap_cursor_init(&zc, os, object);
411fa9e406ahrens	    zap_cursor_retrieve(&zc, &attr) == 0;
412fa9e406ahrens	    zap_cursor_advance(&zc)) {
413fa9e406ahrens		(void) printf("\t\t%s = ", attr.za_name);
414fa9e406ahrens		if (attr.za_num_integers == 0) {
415fa9e406ahrens			(void) printf("\n");
416fa9e406ahrens			continue;
417fa9e406ahrens		}
418fa9e406ahrens		prop = umem_zalloc(attr.za_num_integers *
419fa9e406ahrens		    attr.za_integer_length, UMEM_NOFAIL);
420fa9e406ahrens		(void) zap_lookup(os, object, attr.za_name,
421fa9e406ahrens		    attr.za_integer_length, attr.za_num_integers, prop);
422fa9e406ahrens		if (attr.za_integer_length == 1) {
423fa9e406ahrens			(void) printf("%s", (char *)prop);
424fa9e406ahrens		} else {
425fa9e406ahrens			for (i = 0; i < attr.za_num_integers; i++) {
426fa9e406ahrens				switch (attr.za_integer_length) {
427fa9e406ahrens				case 2:
428fa9e406ahrens					(void) printf("%u ",
429fa9e406ahrens					    ((uint16_t *)prop)[i]);
430fa9e406ahrens					break;
431fa9e406ahrens				case 4:
432fa9e406ahrens					(void) printf("%u ",
433fa9e406ahrens					    ((uint32_t *)prop)[i]);
434fa9e406ahrens					break;
435fa9e406ahrens				case 8:
436fa9e406ahrens					(void) printf("%lld ",
437fa9e406ahrens					    (u_longlong_t)((int64_t *)prop)[i]);
438fa9e406ahrens					break;
439fa9e406ahrens				}
440fa9e406ahrens			}
441fa9e406ahrens		}
442fa9e406ahrens		(void) printf("\n");
443fa9e406ahrens		umem_free(prop, attr.za_num_integers * attr.za_integer_length);
444fa9e406ahrens	}
44587e5029ahrens	zap_cursor_fini(&zc);
446fa9e406ahrens}
447fa9e406ahrens
448732885fMatthew Ahrensstatic void
449732885fMatthew Ahrensdump_bpobj(objset_t *os, uint64_t object, void *data, size_t size)
450732885fMatthew Ahrens{
451732885fMatthew Ahrens	bpobj_phys_t *bpop = data;
452732885fMatthew Ahrens	char bytes[32], comp[32], uncomp[32];
453732885fMatthew Ahrens
454732885fMatthew Ahrens	if (bpop == NULL)
455732885fMatthew Ahrens		return;
456732885fMatthew Ahrens
457732885fMatthew Ahrens	zdb_nicenum(bpop->bpo_bytes, bytes);
458732885fMatthew Ahrens	zdb_nicenum(bpop->bpo_comp, comp);
459732885fMatthew Ahrens	zdb_nicenum(bpop->bpo_uncomp, uncomp);
460732885fMatthew Ahrens
461732885fMatthew Ahrens	(void) printf("\t\tnum_blkptrs = %llu\n",
462732885fMatthew Ahrens	    (u_longlong_t)bpop->bpo_num_blkptrs);
463732885fMatthew Ahrens	(void) printf("\t\tbytes = %s\n", bytes);
464732885fMatthew Ahrens	if (size >= BPOBJ_SIZE_V1) {
465732885fMatthew Ahrens		(void) printf("\t\tcomp = %s\n", comp);
466732885fMatthew Ahrens		(void) printf("\t\tuncomp = %s\n", uncomp);
467732885fMatthew Ahrens	}
468732885fMatthew Ahrens	if (size >= sizeof (*bpop)) {
469732885fMatthew Ahrens		(void) printf("\t\tsubobjs = %llu\n",
470732885fMatthew Ahrens		    (u_longlong_t)bpop->bpo_subobjs);
471732885fMatthew Ahrens		(void) printf("\t\tnum_subobjs = %llu\n",
472732885fMatthew Ahrens		    (u_longlong_t)bpop->bpo_num_subobjs);
473732885fMatthew Ahrens	}
474732885fMatthew Ahrens
475732885fMatthew Ahrens	if (dump_opt['d'] < 5)
476732885fMatthew Ahrens		return;
477732885fMatthew Ahrens
478732885fMatthew Ahrens	for (uint64_t i = 0; i < bpop->bpo_num_blkptrs; i++) {
479732885fMatthew Ahrens		char blkbuf[BP_SPRINTF_LEN];
480732885fMatthew Ahrens		blkptr_t bp;
481732885fMatthew Ahrens
482732885fMatthew Ahrens		int err = dmu_read(os, object,
483732885fMatthew Ahrens		    i * sizeof (bp), sizeof (bp), &bp, 0);
484732885fMatthew Ahrens		if (err != 0) {
485732885fMatthew Ahrens			(void) printf("got error %u from dmu_read\n", err);
486732885fMatthew Ahrens			break;
487732885fMatthew Ahrens		}
488732885fMatthew Ahrens		snprintf_blkptr_compact(blkbuf, sizeof (blkbuf), &bp);
489732885fMatthew Ahrens		(void) printf("\t%s\n", blkbuf);
490732885fMatthew Ahrens	}
491732885fMatthew Ahrens}
492732885fMatthew Ahrens
493732885fMatthew Ahrens/* ARGSUSED */
494732885fMatthew Ahrensstatic void
495732885fMatthew Ahrensdump_bpobj_subobjs(objset_t *os, uint64_t object, void *data, size_t size)
496732885fMatthew Ahrens{
497732885fMatthew Ahrens	dmu_object_info_t doi;
498732885fMatthew Ahrens
499732885fMatthew Ahrens	VERIFY0(dmu_object_info(os, object, &doi));
500732885fMatthew Ahrens	uint64_t *subobjs = kmem_alloc(doi.doi_max_offset, KM_SLEEP);
501732885fMatthew Ahrens
502732885fMatthew Ahrens	int err = dmu_read(os, object, 0, doi.doi_max_offset, subobjs, 0);
503732885fMatthew Ahrens	if (err != 0) {
504732885fMatthew Ahrens		(void) printf("got error %u from dmu_read\n", err);
505732885fMatthew Ahrens		kmem_free(subobjs, doi.doi_max_offset);
506732885fMatthew Ahrens		return;
507732885fMatthew Ahrens	}
508732885fMatthew Ahrens
509732885fMatthew Ahrens	int64_t last_nonzero = -1;
510732885fMatthew Ahrens	for (uint64_t i = 0; i < doi.doi_max_offset / 8; i++) {
511732885fMatthew Ahrens		if (subobjs[i] != 0)
512732885fMatthew Ahrens			last_nonzero = i;
513732885fMatthew Ahrens	}
514732885fMatthew Ahrens
515732885fMatthew Ahrens	for (int64_t i = 0; i <= last_nonzero; i++) {
516732885fMatthew Ahrens		(void) printf("\t%llu\n", (longlong_t)subobjs[i]);
517732885fMatthew Ahrens	}
518732885fMatthew Ahrens	kmem_free(subobjs, doi.doi_max_offset);
519732885fMatthew Ahrens}
520732885fMatthew Ahrens
521e743726ahrens/*ARGSUSED*/
522e743726ahrensstatic void
523486ae71Matthew Ahrensdump_ddt_zap(objset_t *os, uint64_t object, void *data, size_t size)
524486ae71Matthew Ahrens{
525486ae71Matthew Ahrens	dump_zap_stats(os, object);
526486ae71Matthew Ahrens	/* contents are printed elsewhere, properly decoded */
527486ae71Matthew Ahrens}
528486ae71Matthew Ahrens
529486ae71Matthew Ahrens/*ARGSUSED*/
530486ae71Matthew Ahrensstatic void
5310a586ceMark Shellenbaumdump_sa_attrs(objset_t *os, uint64_t object, void *data, size_t size)
5320a586ceMark Shellenbaum{
5330a586ceMark Shellenbaum	zap_cursor_t zc;
5340a586ceMark Shellenbaum	zap_attribute_t attr;
5350a586ceMark Shellenbaum
5360a586ceMark Shellenbaum	dump_zap_stats(os, object);
5370a586ceMark Shellenbaum	(void) printf("\n");
5380a586ceMark Shellenbaum
5390a586ceMark Shellenbaum	for (zap_cursor_init(&zc, os, object);
5400a586ceMark Shellenbaum	    zap_cursor_retrieve(&zc, &attr) == 0;
5410a586ceMark Shellenbaum	    zap_cursor_advance(&zc)) {
5420a586ceMark Shellenbaum		(void) printf("\t\t%s = ", attr.za_name);
5430a586ceMark Shellenbaum		if (attr.za_num_integers == 0) {
5440a586ceMark Shellenbaum			(void) printf("\n");
5450a586ceMark Shellenbaum			continue;
5460a586ceMark Shellenbaum		}
5470a586ceMark Shellenbaum		(void) printf(" %llx : [%d:%d:%d]\n",
5480a586ceMark Shellenbaum		    (u_longlong_t)attr.za_first_integer,
5490a586ceMark Shellenbaum		    (int)ATTR_LENGTH(attr.za_first_integer),
5500a586ceMark Shellenbaum		    (int)ATTR_BSWAP(attr.za_first_integer),
5510a586ceMark Shellenbaum		    (int)ATTR_NUM(attr.za_first_integer));
5520a586ceMark Shellenbaum	}
5530a586ceMark Shellenbaum	zap_cursor_fini(&zc);
5540a586ceMark Shellenbaum}
5550a586ceMark Shellenbaum
5560a586ceMark Shellenbaum/*ARGSUSED*/
5570a586ceMark Shellenbaumstatic void
5580a586ceMark Shellenbaumdump_sa_layouts(objset_t *os, uint64_t object, void *data, size_t size)
5590a586ceMark Shellenbaum{
5600a586ceMark Shellenbaum	zap_cursor_t zc;
5610a586ceMark Shellenbaum	zap_attribute_t attr;
5620a586ceMark Shellenbaum	uint16_t *layout_attrs;
5630a586ceMark Shellenbaum	int i;
5640a586ceMark Shellenbaum
5650a586ceMark Shellenbaum	dump_zap_stats(os, object);
5660a586ceMark Shellenbaum	(void) printf("\n");
5670a586ceMark Shellenbaum
5680a586ceMark Shellenbaum	for (zap_cursor_init(&zc, os, object);
5690a586ceMark Shellenbaum	    zap_cursor_retrieve(&zc, &attr) == 0;
5700a586ceMark Shellenbaum	    zap_cursor_advance(&zc)) {
5710a586ceMark Shellenbaum		(void) printf("\t\t%s = [", attr.za_name);
5720a586ceMark Shellenbaum		if (attr.za_num_integers == 0) {
5730a586ceMark Shellenbaum			(void) printf("\n");
5740a586ceMark Shellenbaum			continue;
5750a586ceMark Shellenbaum		}
5760a586ceMark Shellenbaum
5770a586ceMark Shellenbaum		VERIFY(attr.za_integer_length == 2);
5780a586ceMark Shellenbaum		layout_attrs = umem_zalloc(attr.za_num_integers *
5790a586ceMark Shellenbaum		    attr.za_integer_length, UMEM_NOFAIL);
5800a586ceMark Shellenbaum
5810a586ceMark Shellenbaum		VERIFY(zap_lookup(os, object, attr.za_name,
5820a586ceMark Shellenbaum		    attr.za_integer_length,
5830a586ceMark Shellenbaum		    attr.za_num_integers, layout_attrs) == 0);
5840a586ceMark Shellenbaum
5850a586ceMark Shellenbaum		for (i = 0; i != attr.za_num_integers; i++)
5860a586ceMark Shellenbaum			(void) printf(" %d ", (int)layout_attrs[i]);
5870a586ceMark Shellenbaum		(void) printf("]\n");
5880a586ceMark Shellenbaum		umem_free(layout_attrs,
5890a586ceMark Shellenbaum		    attr.za_num_integers * attr.za_integer_length);
5900a586ceMark Shellenbaum	}
5910a586ceMark Shellenbaum	zap_cursor_fini(&zc);
5920a586ceMark Shellenbaum}
5930a586ceMark Shellenbaum
5940a586ceMark Shellenbaum/*ARGSUSED*/
5950a586ceMark Shellenbaumstatic void
596e743726ahrensdump_zpldir(objset_t *os, uint64_t object, void *data, size_t size)
597e743726ahrens{
598e743726ahrens	zap_cursor_t zc;
599e743726ahrens	zap_attribute_t attr;
600e743726ahrens	const char *typenames[] = {
601e743726ahrens		/* 0 */ "not specified",
602e743726ahrens		/* 1 */ "FIFO",
603e743726ahrens		/* 2 */ "Character Device",
604e743726ahrens		/* 3 */ "3 (invalid)",
605e743726ahrens		/* 4 */ "Directory",
606e743726ahrens		/* 5 */ "5 (invalid)",
607e743726ahrens		/* 6 */ "Block Device",
608e743726ahrens		/* 7 */ "7 (invalid)",
609e743726ahrens		/* 8 */ "Regular File",
610e743726ahrens		/* 9 */ "9 (invalid)",
611e743726ahrens		/* 10 */ "Symbolic Link",
612e743726ahrens		/* 11 */ "11 (invalid)",
613e743726ahrens		/* 12 */ "Socket",
614e743726ahrens		/* 13 */ "Door",
615e743726ahrens		/* 14 */ "Event Port",
616e743726ahrens		/* 15 */ "15 (invalid)",
617e743726ahrens	};
618e743726ahrens
619e743726ahrens	dump_zap_stats(os, object);
620e743726ahrens	(void) printf("\n");
621e743726ahrens
622e743726ahrens	for (zap_cursor_init(&zc, os, object);
623e743726ahrens	    zap_cursor_retrieve(&zc, &attr) == 0;
624e743726ahrens	    zap_cursor_advance(&zc)) {
625e743726ahrens		(void) printf("\t\t%s = %lld (type: %s)\n",
626e743726ahrens		    attr.za_name, ZFS_DIRENT_OBJ(attr.za_first_integer),
627e743726ahrens		    typenames[ZFS_DIRENT_TYPE(attr.za_first_integer)]);
628e743726ahrens	}
629e743726ahrens	zap_cursor_fini(&zc);
630e743726ahrens}
631e743726ahrens
6320713e23George Wilsonint
6330713e23George Wilsonget_dtl_refcount(vdev_t *vd)
6340713e23George Wilson{
6350713e23George Wilson	int refcount = 0;
6360713e23George Wilson
6370713e23George Wilson	if (vd->vdev_ops->vdev_op_leaf) {
6380713e23George Wilson		space_map_t *sm = vd->vdev_dtl_sm;
6390713e23George Wilson
6400713e23George Wilson		if (sm != NULL &&
6410713e23George Wilson		    sm->sm_dbuf->db_size == sizeof (space_map_phys_t))
6420713e23George Wilson			return (1);
6430713e23George Wilson		return (0);
6440713e23George Wilson	}
6450713e23George Wilson
6460713e23George Wilson	for (int c = 0; c < vd->vdev_children; c++)
6470713e23George Wilson		refcount += get_dtl_refcount(vd->vdev_child[c]);
6480713e23George Wilson	return (refcount);
6490713e23George Wilson}
6500713e23George Wilson
6510713e23George Wilsonint
6520713e23George Wilsonget_metaslab_refcount(vdev_t *vd)
6530713e23George Wilson{
6540713e23George Wilson	int refcount = 0;
6550713e23George Wilson
6562e4c998George Wilson	if (vd->vdev_top == vd && !vd->vdev_removing) {
6570713e23George Wilson		for (int m = 0; m < vd->vdev_ms_count; m++) {
6580713e23George Wilson			space_map_t *sm = vd->vdev_ms[m]->ms_sm;
6590713e23George Wilson
6600713e23George Wilson			if (sm != NULL &&
6610713e23George Wilson			    sm->sm_dbuf->db_size == sizeof (space_map_phys_t))
6620713e23George Wilson				refcount++;
6630713e23George Wilson		}
6640713e23George Wilson	}
6650713e23George Wilson	for (int c = 0; c < vd->vdev_children; c++)
6660713e23George Wilson		refcount += get_metaslab_refcount(vd->vdev_child[c]);
6670713e23George Wilson
6680713e23George Wilson	return (refcount);
6690713e23George Wilson}
6700713e23George Wilson
6710713e23George Wilsonstatic int
6720713e23George Wilsonverify_spacemap_refcounts(spa_t *spa)
6730713e23George Wilson{
6742acef22Matthew Ahrens	uint64_t expected_refcount = 0;
6752acef22Matthew Ahrens	uint64_t actual_refcount;
6760713e23George Wilson
6772acef22Matthew Ahrens	(void) feature_get_refcount(spa,
6782acef22Matthew Ahrens	    &spa_feature_table[SPA_FEATURE_SPACEMAP_HISTOGRAM],
6792acef22Matthew Ahrens	    &expected_refcount);
6800713e23George Wilson	actual_refcount = get_dtl_refcount(spa->spa_root_vdev);
6810713e23George Wilson	actual_refcount += get_metaslab_refcount(spa->spa_root_vdev);
6820713e23George Wilson
6830713e23George Wilson	if (expected_refcount != actual_refcount) {
6842acef22Matthew Ahrens		(void) printf("space map refcount mismatch: expected %lld != "
6852acef22Matthew Ahrens		    "actual %lld\n",
6862acef22Matthew Ahrens		    (longlong_t)expected_refcount,
6872acef22Matthew Ahrens		    (longlong_t)actual_refcount);
6880713e23George Wilson		return (2);
6890713e23George Wilson	}
6900713e23George Wilson	return (0);
6910713e23George Wilson}
6920713e23George Wilson
693fa9e406ahrensstatic void
6940713e23George Wilsondump_spacemap(objset_t *os, space_map_t *sm)
695fa9e406ahrens{
696fa9e406ahrens	uint64_t alloc, offset, entry;
6978053a26ck	char *ddata[] = { "ALLOC", "FREE", "CONDENSE", "INVALID",
6988053a26ck			    "INVALID", "INVALID", "INVALID", "INVALID" };
699fa9e406ahrens
7000713e23George Wilson	if (sm == NULL)
701fa9e406ahrens		return;
702fa9e406ahrens
703fa9e406ahrens	/*
704fa9e406ahrens	 * Print out the freelist entries in both encoded and decoded form.
705fa9e406ahrens	 */
706fa9e406ahrens	alloc = 0;
7070713e23George Wilson	for (offset = 0; offset < space_map_length(sm);
7080713e23George Wilson	    offset += sizeof (entry)) {
7090713e23George Wilson		uint8_t mapshift = sm->sm_shift;
7100713e23George Wilson
7110713e23George Wilson		VERIFY0(dmu_read(os, space_map_object(sm), offset,
7127bfdf01Neil Perrin		    sizeof (entry), &entry, DMU_READ_PREFETCH));
713fa9e406ahrens		if (SM_DEBUG_DECODE(entry)) {
7140713e23George Wilson
71587219dbVictor Latushkin			(void) printf("\t    [%6llu] %s: txg %llu, pass %llu\n",
716fa9e406ahrens			    (u_longlong_t)(offset / sizeof (entry)),
717fa9e406ahrens			    ddata[SM_DEBUG_ACTION_DECODE(entry)],
7185ad8204nd			    (u_longlong_t)SM_DEBUG_TXG_DECODE(entry),
7195ad8204nd			    (u_longlong_t)SM_DEBUG_SYNCPASS_DECODE(entry));
720fa9e406ahrens		} else {
72187219dbVictor Latushkin			(void) printf("\t    [%6llu]    %c  range:"
72287219dbVictor Latushkin			    " %010llx-%010llx  size: %06llx\n",
723fa9e406ahrens			    (u_longlong_t)(offset / sizeof (entry)),
724fa9e406ahrens			    SM_TYPE_DECODE(entry) == SM_ALLOC ? 'A' : 'F',
7255ad8204nd			    (u_longlong_t)((SM_OFFSET_DECODE(entry) <<
7260713e23George Wilson			    mapshift) + sm->sm_start),
7275ad8204nd			    (u_longlong_t)((SM_OFFSET_DECODE(entry) <<
7280713e23George Wilson			    mapshift) + sm->sm_start +
7290713e23George Wilson			    (SM_RUN_DECODE(entry) << mapshift)),
7305ad8204nd			    (u_longlong_t)(SM_RUN_DECODE(entry) << mapshift));
731fa9e406ahrens			if (SM_TYPE_DECODE(entry) == SM_ALLOC)
732fa9e406ahrens				alloc += SM_RUN_DECODE(entry) << mapshift;
733fa9e406ahrens			else
734fa9e406ahrens				alloc -= SM_RUN_DECODE(entry) << mapshift;
735fa9e406ahrens		}
736fa9e406ahrens	}
7370713e23George Wilson	if (alloc != space_map_allocated(sm)) {
738fa9e406ahrens		(void) printf("space_map_object alloc (%llu) INCONSISTENT "
739fa9e406ahrens		    "with space map summary (%llu)\n",
7400713e23George Wilson		    (u_longlong_t)space_map_allocated(sm), (u_longlong_t)alloc);
741fa9e406ahrens	}
742fa9e406ahrens}
743fa9e406ahrens
744fa9e406ahrensstatic void
745d6e555bGeorge Wilsondump_metaslab_stats(metaslab_t *msp)
746d6e555bGeorge Wilson{
7473f9d6adLin Ling	char maxbuf[32];
7480713e23George Wilson	range_tree_t *rt = msp->ms_tree;
7490713e23George Wilson	avl_tree_t *t = &msp->ms_size_tree;
7500713e23George Wilson	int free_pct = range_tree_space(rt) * 100 / msp->ms_size;
751d6e555bGeorge Wilson
7520713e23George Wilson	zdb_nicenum(metaslab_block_maxsize(msp), maxbuf);
753d6e555bGeorge Wilson
75487219dbVictor Latushkin	(void) printf("\t %25s %10lu   %7s  %6s   %4s %4d%%\n",
755d6e555bGeorge Wilson	    "segments", avl_numnodes(t), "maxsize", maxbuf,
756d6e555bGeorge Wilson	    "freepct", free_pct);
7570713e23George Wilson	(void) printf("\tIn-memory histogram:\n");
7580713e23George Wilson	dump_histogram(rt->rt_histogram, RANGE_TREE_HISTOGRAM_SIZE, 0);
759d6e555bGeorge Wilson}
760d6e555bGeorge Wilson
761d6e555bGeorge Wilsonstatic void
762fa9e406ahrensdump_metaslab(metaslab_t *msp)
763fa9e406ahrens{
764fa9e406ahrens	vdev_t *vd = msp->ms_group->mg_vd;
765fa9e406ahrens	spa_t *spa = vd->vdev_spa;
7660713e23George Wilson	space_map_t *sm = msp->ms_sm;
7673f9d6adLin Ling	char freebuf[32];
768fa9e406ahrens
7690713e23George Wilson	zdb_nicenum(msp->ms_size - space_map_allocated(sm), freebuf);
770fa9e406ahrens
771fa9e406ahrens	(void) printf(
77287219dbVictor Latushkin	    "\tmetaslab %6llu   offset %12llx   spacemap %6llu   free    %5s\n",
7730713e23George Wilson	    (u_longlong_t)msp->ms_id, (u_longlong_t)msp->ms_start,
7740713e23George Wilson	    (u_longlong_t)space_map_object(sm), freebuf);
775fa9e406ahrens
7760713e23George Wilson	if (dump_opt['m'] > 2 && !dump_opt['L']) {
777d6e555bGeorge Wilson		mutex_enter(&msp->ms_lock);
7780713e23George Wilson		metaslab_load_wait(msp);
7790713e23George Wilson		if (!msp->ms_loaded) {
7800713e23George Wilson			VERIFY0(metaslab_load(msp));
7810713e23George Wilson			range_tree_stat_verify(msp->ms_tree);
7820713e23George Wilson		}
783bc3975bVictor Latushkin		dump_metaslab_stats(msp);
7840713e23George Wilson		metaslab_unload(msp);
785d6e555bGeorge Wilson		mutex_exit(&msp->ms_lock);
786d6e555bGeorge Wilson	}
787d6e555bGeorge Wilson
7880713e23George Wilson	if (dump_opt['m'] > 1 && sm != NULL &&
7892acef22Matthew Ahrens	    spa_feature_is_active(spa, SPA_FEATURE_SPACEMAP_HISTOGRAM)) {
7900713e23George Wilson		/*
7910713e23George Wilson		 * The space map histogram represents free space in chunks
7920713e23George Wilson		 * of sm_shift (i.e. bucket 0 refers to 2^sm_shift).
7930713e23George Wilson		 */
7942e4c998George Wilson		(void) printf("\tOn-disk histogram:\t\tfragmentation %llu\n",
7952e4c998George Wilson		    (u_longlong_t)msp->ms_fragmentation);
7960713e23George Wilson		dump_histogram(sm->sm_phys->smp_histogram,
7972e4c998George Wilson		    SPACE_MAP_HISTOGRAM_SIZE, sm->sm_shift);
7980713e23George Wilson	}
7990713e23George Wilson
8000713e23George Wilson	if (dump_opt['d'] > 5 || dump_opt['m'] > 3) {
8010713e23George Wilson		ASSERT(msp->ms_size == (1ULL << vd->vdev_ms_shift));
802d6e555bGeorge Wilson
803d6e555bGeorge Wilson		mutex_enter(&msp->ms_lock);
8040713e23George Wilson		dump_spacemap(spa->spa_meta_objset, msp->ms_sm);
805d6e555bGeorge Wilson		mutex_exit(&msp->ms_lock);
806d6e555bGeorge Wilson	}
80787219dbVictor Latushkin}
808fa9e406ahrens
80987219dbVictor Latushkinstatic void
81087219dbVictor Latushkinprint_vdev_metaslab_header(vdev_t *vd)
81187219dbVictor Latushkin{
81287219dbVictor Latushkin	(void) printf("\tvdev %10llu\n\t%-10s%5llu   %-19s   %-15s   %-10s\n",
81387219dbVictor Latushkin	    (u_longlong_t)vd->vdev_id,
81487219dbVictor Latushkin	    "metaslabs", (u_longlong_t)vd->vdev_ms_count,
81587219dbVictor Latushkin	    "offset", "spacemap", "free");
81687219dbVictor Latushkin	(void) printf("\t%15s   %19s   %15s   %10s\n",
81787219dbVictor Latushkin	    "---------------", "-------------------",
81887219dbVictor Latushkin	    "---------------", "-------------");
819fa9e406ahrens}
820fa9e406ahrens
821fa9e406ahrensstatic void
8222e4c998George Wilsondump_metaslab_groups(spa_t *spa)
8232e4c998George Wilson{
8242e4c998George Wilson	vdev_t *rvd = spa->spa_root_vdev;
8252e4c998George Wilson	metaslab_class_t *mc = spa_normal_class(spa);
8262e4c998George Wilson	uint64_t fragmentation;
8272e4c998George Wilson
8282e4c998George Wilson	metaslab_class_histogram_verify(mc);
8292e4c998George Wilson
8302e4c998George Wilson	for (int c = 0; c < rvd->vdev_children; c++) {
8312e4c998George Wilson		vdev_t *tvd = rvd->vdev_child[c];
8322e4c998George Wilson		metaslab_group_t *mg = tvd->vdev_mg;
8332e4c998George Wilson
8342e4c998George Wilson		if (mg->mg_class != mc)
8352e4c998George Wilson			continue;
8362e4c998George Wilson
8372e4c998George Wilson		metaslab_group_histogram_verify(mg);
8382e4c998George Wilson		mg->mg_fragmentation = metaslab_group_fragmentation(mg);
8392e4c998George Wilson
8402e4c998George Wilson		(void) printf("\tvdev %10llu\t\tmetaslabs%5llu\t\t"
8412e4c998George Wilson		    "fragmentation",
8422e4c998George Wilson		    (u_longlong_t)tvd->vdev_id,
8432e4c998George Wilson		    (u_longlong_t)tvd->vdev_ms_count);
8442e4c998George Wilson		if (mg->mg_fragmentation == ZFS_FRAG_INVALID) {
8452e4c998George Wilson			(void) printf("%3s\n", "-");
8462e4c998George Wilson		} else {
8472e4c998George Wilson			(void) printf("%3llu%%\n",
8482e4c998George Wilson			    (u_longlong_t)mg->mg_fragmentation);
8492e4c998George Wilson		}
8502e4c998George Wilson		dump_histogram(mg->mg_histogram, RANGE_TREE_HISTOGRAM_SIZE, 0);
8512e4c998George Wilson	}
8522e4c998George Wilson
8532e4c998George Wilson	(void) printf("\tpool %s\tfragmentation", spa_name(spa));
8542e4c998George Wilson	fragmentation = metaslab_class_fragmentation(mc);
8552e4c998George Wilson	if (fragmentation == ZFS_FRAG_INVALID)
8562e4c998George Wilson		(void) printf("\t%3s\n", "-");
8572e4c998George Wilson	else
858