zdb.c revision 5cabbc6b49070407fb9610cfe73d4c0e0dea3e77
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.
245cabbc6Prashanth Sreenivasa * Copyright (c) 2011, 2017 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>
63770499eDan Kimmel#include <sys/abd.h>
644923c69Matthew Ahrens#include <sys/blkptr.h>
654445fffMatthew Ahrens#include <zfs_comutil.h>
660a05512Jason King#include <libcmdutils.h>
67de6628fck#undef verify
68de6628fck#include <libzfs.h>
69fa9e406ahrens
703f7978dAlan Somers#include "zdb.h"
713f7978dAlan Somers
72e690fb2Christopher Siden#define	ZDB_COMPRESS_NAME(idx) ((idx) < ZIO_COMPRESS_FUNCTIONS ?	\
73e690fb2Christopher Siden	zio_compress_table[(idx)].ci_name : "UNKNOWN")
74e690fb2Christopher Siden#define	ZDB_CHECKSUM_NAME(idx) ((idx) < ZIO_CHECKSUM_FUNCTIONS ?	\
75e690fb2Christopher Siden	zio_checksum_table[(idx)].ci_name : "UNKNOWN")
76e690fb2Christopher Siden#define	ZDB_OT_NAME(idx) ((idx) < DMU_OT_NUMTYPES ?	\
77e690fb2Christopher Siden	dmu_ot[(idx)].ot_name : DMU_OT_IS_VALID(idx) ?	\
78e690fb2Christopher Siden	dmu_ot_byteswap[DMU_OT_BYTESWAP(idx)].ob_name : "UNKNOWN")
79e690fb2Christopher Siden#define	ZDB_OT_TYPE(idx) ((idx) < DMU_OT_NUMTYPES ? (idx) :		\
805cabbc6Prashanth Sreenivasa	(idx) == DMU_OTN_ZAP_DATA || (idx) == DMU_OTN_ZAP_METADATA ?	\
815cabbc6Prashanth Sreenivasa	DMU_OT_ZAP_OTHER : \
825cabbc6Prashanth Sreenivasa	(idx) == DMU_OTN_UINT64_DATA || (idx) == DMU_OTN_UINT64_METADATA ? \
835cabbc6Prashanth Sreenivasa	DMU_OT_UINT64_OTHER : DMU_OT_NUMTYPES)
846de8f41Victor Latushkin
85feef89cVictor Latushkin#ifndef lint
864dd77f9Matthew Ahrensextern int reference_tracking_enable;
877fd05acMatthew Ahrensextern boolean_t zfs_recover;
8806be980Matthew Ahrensextern uint64_t zfs_arc_max, zfs_arc_meta_limit;
89f7950bfMatthew Ahrensextern int zfs_vdev_async_read_max_active;
90feef89cVictor Latushkin#else
914dd77f9Matthew Ahrensint reference_tracking_enable;
927fd05acMatthew Ahrensboolean_t zfs_recover;
9306be980Matthew Ahrensuint64_t zfs_arc_max, zfs_arc_meta_limit;
94f7950bfMatthew Ahrensint zfs_vdev_async_read_max_active;
95feef89cVictor Latushkin#endif
96feef89cVictor Latushkin
973f7978dAlan Somersstatic const char cmdname[] = "zdb";
98fa9e406ahrensuint8_t dump_opt[256];
99fa9e406ahrens
100fa9e406ahrenstypedef void object_viewer_t(objset_t *, uint64_t, void *data, size_t size);
101fa9e406ahrens
102fa9e406ahrensuint64_t *zopt_object = NULL;
1033f7978dAlan Somersstatic unsigned zopt_objects = 0;
104de6628fcklibzfs_handle_t *g_zfs;
10506be980Matthew Ahrensuint64_t max_inflight = 1000;
106fa9e406ahrens
107732885fMatthew Ahrensstatic void snprintf_blkptr_compact(char *, size_t, const blkptr_t *);
108732885fMatthew Ahrens
109fa9e406ahrens/*
110fa9e406ahrens * These libumem hooks provide a reasonable set of defaults for the allocator's
111fa9e406ahrens * debugging facilities.
112fa9e406ahrens */
113fa9e406ahrensconst char *
114fa9e406ahrens_umem_debug_init()
115fa9e406ahrens{
116fa9e406ahrens	return ("default,verbose"); /* $UMEM_DEBUG setting */
117fa9e406ahrens}
118fa9e406ahrens
119fa9e406ahrensconst char *
120fa9e406ahrens_umem_logging_init(void)
121fa9e406ahrens{
122fa9e406ahrens	return ("fail,contents"); /* $UMEM_LOGGING setting */
123fa9e406ahrens}
124fa9e406ahrens
125fa9e406ahrensstatic void
126fa9e406ahrensusage(void)
127fa9e406ahrens{
128fa9e406ahrens	(void) fprintf(stderr,
129dfd5965Richard Yao	    "Usage:\t%s [-AbcdDFGhiLMPsvX] [-e [-V] [-p <path> ...]] "
130ed61ec1Yuri Pankov	    "[-I <inflight I/Os>]\n"
131ed61ec1Yuri Pankov	    "\t\t[-o <var>=<value>]... [-t <txg>] [-U <cache>] [-x <dumpdir>]\n"
132ed61ec1Yuri Pankov	    "\t\t[<poolname> [<object> ...]]\n"
133dfd5965Richard Yao	    "\t%s [-AdiPv] [-e [-V] [-p <path> ...]] [-U <cache>] <dataset> "
134ed61ec1Yuri Pankov	    "[<object> ...]\n"
135ed61ec1Yuri Pankov	    "\t%s -C [-A] [-U <cache>]\n"
136ed61ec1Yuri Pankov	    "\t%s -l [-Aqu] <device>\n"
137dfd5965Richard Yao	    "\t%s -m [-AFLPX] [-e [-V] [-p <path> ...]] [-t <txg>] "
138dfd5965Richard Yao	    "[-U <cache>]\n\t\t<poolname> [<vdev> [<metaslab> ...]]\n"
139ed61ec1Yuri Pankov	    "\t%s -O <dataset> <path>\n"
140dfd5965Richard Yao	    "\t%s -R [-A] [-e [-V] [-p <path> ...]] [-U <cache>]\n"
141ed61ec1Yuri Pankov	    "\t\t<poolname> <vdev>:<offset>:<size>[:<flags>]\n"
1424923c69Matthew Ahrens	    "\t%s -E [-A] word0:word1:...:word15\n"
143dfd5965Richard Yao	    "\t%s -S [-AP] [-e [-V] [-p <path> ...]] [-U <cache>] "
144dfd5965Richard Yao	    "<poolname>\n\n",
145ed61ec1Yuri Pankov	    cmdname, cmdname, cmdname, cmdname, cmdname, cmdname, cmdname,
1464923c69Matthew Ahrens	    cmdname, cmdname);
1473ad6c7fVictor Latushkin
1483ad6c7fVictor Latushkin	(void) fprintf(stderr, "    Dataset name must include at least one "
1493ad6c7fVictor Latushkin	    "separator character '/' or '@'\n");
1503ad6c7fVictor Latushkin	(void) fprintf(stderr, "    If dataset name is specified, only that "
1513ad6c7fVictor Latushkin	    "dataset is dumped\n");
1523ad6c7fVictor Latushkin	(void) fprintf(stderr, "    If object numbers are specified, only "
1533ad6c7fVictor Latushkin	    "those objects are dumped\n\n");
1543ad6c7fVictor Latushkin	(void) fprintf(stderr, "    Options to control amount of output:\n");
1553ad6c7fVictor Latushkin	(void) fprintf(stderr, "        -b block statistics\n");
1563ad6c7fVictor Latushkin	(void) fprintf(stderr, "        -c checksum all metadata (twice for "
1576365109Victor Latushkin	    "all data) blocks\n");
158ed61ec1Yuri Pankov	(void) fprintf(stderr, "        -C config (or cachefile if alone)\n");
159ed61ec1Yuri Pankov	(void) fprintf(stderr, "        -d dataset(s)\n");
160f0ba89bJeff Bonwick	(void) fprintf(stderr, "        -D dedup statistics\n");
1614923c69Matthew Ahrens	(void) fprintf(stderr, "        -E decode and display block from an "
1624923c69Matthew Ahrens	    "embedded block pointer\n");
163ed61ec1Yuri Pankov	(void) fprintf(stderr, "        -h pool history\n");
164ed61ec1Yuri Pankov	(void) fprintf(stderr, "        -i intent logs\n");
16564723e3Yuri Pankov	(void) fprintf(stderr, "        -l read label contents\n");
16682a0a98Victor Latushkin	(void) fprintf(stderr, "        -L disable leak tracking (do not "
16782a0a98Victor Latushkin	    "load spacemaps)\n");
168ed61ec1Yuri Pankov	(void) fprintf(stderr, "        -m metaslabs\n");
169ed61ec1Yuri Pankov	(void) fprintf(stderr, "        -M metaslab groups\n");
170ed61ec1Yuri Pankov	(void) fprintf(stderr, "        -O perform object lookups by path\n");
171d41e764ek	(void) fprintf(stderr, "        -R read and display block from a "
172ed61ec1Yuri Pankov	    "device\n");
173ed61ec1Yuri Pankov	(void) fprintf(stderr, "        -s report stats on zdb's I/O\n");
174ed61ec1Yuri Pankov	(void) fprintf(stderr, "        -S simulate dedup to measure effect\n");
175ed61ec1Yuri Pankov	(void) fprintf(stderr, "        -v verbose (applies to all "
176ed61ec1Yuri Pankov	    "others)\n\n");
1773ad6c7fVictor Latushkin	(void) fprintf(stderr, "    Below options are intended for use "
178df15e41Matthew Ahrens	    "with other options:\n");
179feef89cVictor Latushkin	(void) fprintf(stderr, "        -A ignore assertions (-A), enable "
180feef89cVictor Latushkin	    "panic recovery (-AA) or both (-AAA)\n");
1813ad6c7fVictor Latushkin	(void) fprintf(stderr, "        -e pool is exported/destroyed/"
1823ad6c7fVictor Latushkin	    "has altroot/not in a cachefile\n");
183ed61ec1Yuri Pankov	(void) fprintf(stderr, "        -F attempt automatic rewind within "
184ed61ec1Yuri Pankov	    "safe range of transaction groups\n");
185ed61ec1Yuri Pankov	(void) fprintf(stderr, "        -G dump zfs_dbgmsg buffer before "
186ed61ec1Yuri Pankov	    "exiting\n");
1872e4c998George Wilson	(void) fprintf(stderr, "        -I <number of inflight I/Os> -- "
188df15e41Matthew Ahrens	    "specify the maximum number of "
189df15e41Matthew Ahrens	    "checksumming I/Os [default is 200]\n");
1900e60744Pavel Zakharov	(void) fprintf(stderr, "        -o <variable>=<value> set global "
1910e60744Pavel Zakharov	    "variable to an unsigned 32-bit integer value\n");
192ed61ec1Yuri Pankov	(void) fprintf(stderr, "        -p <path> -- use one or more with "
193ed61ec1Yuri Pankov	    "-e to specify path to vdev dir\n");
194ed61ec1Yuri Pankov	(void) fprintf(stderr, "        -P print numbers in parseable form\n");
19564723e3Yuri Pankov	(void) fprintf(stderr, "        -q don't print label contents\n");
196ed61ec1Yuri Pankov	(void) fprintf(stderr, "        -t <txg> -- highest txg to use when "
197ed61ec1Yuri Pankov	    "searching for uberblocks\n");
19864723e3Yuri Pankov	(void) fprintf(stderr, "        -u uberblock\n");
199ed61ec1Yuri Pankov	(void) fprintf(stderr, "        -U <cachefile_path> -- use alternate "
200ed61ec1Yuri Pankov	    "cachefile\n");
201dfd5965Richard Yao	(void) fprintf(stderr, "        -V do verbatim import\n");
202ed61ec1Yuri Pankov	(void) fprintf(stderr, "        -x <dumpdir> -- "
203ed61ec1Yuri Pankov	    "dump all read blocks into specified directory\n");
204ed61ec1Yuri Pankov	(void) fprintf(stderr, "        -X attempt extreme rewind (does not "
205ed61ec1Yuri Pankov	    "work with dataset)\n\n");
206fa9e406ahrens	(void) fprintf(stderr, "Specify an option more than once (e.g. -bb) "
207fa9e406ahrens	    "to make only that option verbose\n");
208fa9e406ahrens	(void) fprintf(stderr, "Default is to dump everything non-verbosely\n");
209fa9e406ahrens	exit(1);
210fa9e406ahrens}
211fa9e406ahrens
21229bdd2fPavel Zakharovstatic void
21329bdd2fPavel Zakharovdump_debug_buffer()
21429bdd2fPavel Zakharov{
21529bdd2fPavel Zakharov	if (dump_opt['G']) {
21629bdd2fPavel Zakharov		(void) printf("\n");
21729bdd2fPavel Zakharov		zfs_dbgmsg_print("zdb");
21829bdd2fPavel Zakharov	}
21929bdd2fPavel Zakharov}
22029bdd2fPavel Zakharov
221ccba080Rich Morris/*
222ccba080Rich Morris * Called for usage errors that are discovered after a call to spa_open(),
223ccba080Rich Morris * dmu_bonus_hold(), or pool_match().  abort() is called for other errors.
224ccba080Rich Morris */
225ccba080Rich Morris
226fa9e406ahrensstatic void
227fa9e406ahrensfatal(const char *fmt, ...)
228fa9e406ahrens{
229fa9e406ahrens	va_list ap;
230fa9e406ahrens
231fa9e406ahrens	va_start(ap, fmt);
232fa9e406ahrens	(void) fprintf(stderr, "%s: ", cmdname);
233fa9e406ahrens	(void) vfprintf(stderr, fmt, ap);
234fa9e406ahrens	va_end(ap);
235fa9e406ahrens	(void) fprintf(stderr, "\n");
236fa9e406ahrens
23729bdd2fPavel Zakharov	dump_debug_buffer();
23829bdd2fPavel Zakharov
239ccba080Rich Morris	exit(1);
240fa9e406ahrens}
241fa9e406ahrens
242fa9e406ahrens/* ARGSUSED */
243fa9e406ahrensstatic void
244fa9e406ahrensdump_packed_nvlist(objset_t *os, uint64_t object, void *data, size_t size)
245fa9e406ahrens{
246fa9e406ahrens	nvlist_t *nv;
247fa9e406ahrens	size_t nvsize = *(uint64_t *)data;
248fa9e406ahrens	char *packed = umem_alloc(nvsize, UMEM_NOFAIL);
249fa9e406ahrens
2507bfdf01Neil Perrin	VERIFY(0 == dmu_read(os, object, 0, nvsize, packed, DMU_READ_PREFETCH));
251fa9e406ahrens
252fa9e406ahrens	VERIFY(nvlist_unpack(packed, nvsize, &nv, 0) == 0);
253fa9e406ahrens
254fa9e406ahrens	umem_free(packed, nvsize);
255fa9e406ahrens
256fa9e406ahrens	dump_nvlist(nv, 8);
257fa9e406ahrens
258fa9e406ahrens	nvlist_free(nv);
259fa9e406ahrens}
260fa9e406ahrens
2614445fffMatthew Ahrens/* ARGSUSED */
2624445fffMatthew Ahrensstatic void
2634445fffMatthew Ahrensdump_history_offsets(objset_t *os, uint64_t object, void *data, size_t size)
2644445fffMatthew Ahrens{
2654445fffMatthew Ahrens	spa_history_phys_t *shp = data;
2664445fffMatthew Ahrens
2674445fffMatthew Ahrens	if (shp == NULL)
2684445fffMatthew Ahrens		return;
2694445fffMatthew Ahrens
2704445fffMatthew Ahrens	(void) printf("\t\tpool_create_len = %llu\n",
2714445fffMatthew Ahrens	    (u_longlong_t)shp->sh_pool_create_len);
2724445fffMatthew Ahrens	(void) printf("\t\tphys_max_off = %llu\n",
2734445fffMatthew Ahrens	    (u_longlong_t)shp->sh_phys_max_off);
2744445fffMatthew Ahrens	(void) printf("\t\tbof = %llu\n",
2754445fffMatthew Ahrens	    (u_longlong_t)shp->sh_bof);
2764445fffMatthew Ahrens	(void) printf("\t\teof = %llu\n",
2774445fffMatthew Ahrens	    (u_longlong_t)shp->sh_eof);
2784445fffMatthew Ahrens	(void) printf("\t\trecords_lost = %llu\n",
2794445fffMatthew Ahrens	    (u_longlong_t)shp->sh_records_lost);
2804445fffMatthew Ahrens}
2814445fffMatthew Ahrens
2823f9d6adLin Lingstatic void
2830a05512Jason Kingzdb_nicenum(uint64_t num, char *buf, size_t buflen)
2843f9d6adLin Ling{
2853f9d6adLin Ling	if (dump_opt['P'])
2860a05512Jason King		(void) snprintf(buf, buflen, "%llu", (longlong_t)num);
2873f9d6adLin Ling	else
2880a05512Jason King		nicenum(num, buf, sizeof (buf));
2893f9d6adLin Ling}
2903f9d6adLin Ling
2913f7978dAlan Somersstatic const char histo_stars[] = "****************************************";
2923f7978dAlan Somersstatic const uint64_t histo_width = sizeof (histo_stars) - 1;
293fa9e406ahrens
294fa9e406ahrensstatic void
2950713e23George Wilsondump_histogram(const uint64_t *histo, int size, int offset)
296fa9e406ahrens{
297fa9e406ahrens	int i;
298490d05bMatthew Ahrens	int minidx = size - 1;
299fa9e406ahrens	int maxidx = 0;
300fa9e406ahrens	uint64_t max = 0;
301fa9e406ahrens
302490d05bMatthew Ahrens	for (i = 0; i < size; i++) {
303fa9e406ahrens		if (histo[i] > max)
304fa9e406ahrens			max = histo[i];
305fa9e406ahrens		if (histo[i] > 0 && i > maxidx)
306fa9e406ahrens			maxidx = i;
307fa9e406ahrens		if (histo[i] > 0 && i < minidx)
308fa9e406ahrens			minidx = i;
309fa9e406ahrens	}
310fa9e406ahrens
311490d05bMatthew Ahrens	if (max < histo_width)
312490d05bMatthew Ahrens		max = histo_width;
313fa9e406ahrens
314490d05bMatthew Ahrens	for (i = minidx; i <= maxidx; i++) {
315490d05bMatthew Ahrens		(void) printf("\t\t\t%3u: %6llu %s\n",
3160713e23George Wilson		    i + offset, (u_longlong_t)histo[i],
317490d05bMatthew Ahrens		    &histo_stars[(max - histo[i]) * histo_width / max]);
318490d05bMatthew Ahrens	}
319fa9e406ahrens}
320fa9e406ahrens
321fa9e406ahrensstatic void
322fa9e406ahrensdump_zap_stats(objset_t *os, uint64_t object)
323fa9e406ahrens{
324fa9e406ahrens	int error;
325fa9e406ahrens	zap_stats_t zs;
326fa9e406ahrens
327fa9e406ahrens	error = zap_get_stats(os, object, &zs);
328fa9e406ahrens	if (error)
329fa9e406ahrens		return;
330fa9e406ahrens
331fa9e406ahrens	if (zs.zs_ptrtbl_len == 0) {
332fa9e406ahrens		ASSERT(zs.zs_num_blocks == 1);
333fa9e406ahrens		(void) printf("\tmicrozap: %llu bytes, %llu entries\n",
334fa9e406ahrens		    (u_longlong_t)zs.zs_blocksize,
335fa9e406ahrens		    (u_longlong_t)zs.zs_num_entries);
336fa9e406ahrens		return;
337fa9e406ahrens	}
338fa9e406ahrens
339fa9e406ahrens	(void) printf("\tFat ZAP stats:\n");
3408248818nd
3418248818nd	(void) printf("\t\tPointer table:\n");
3428248818nd	(void) printf("\t\t\t%llu elements\n",
343fa9e406ahrens	    (u_longlong_t)zs.zs_ptrtbl_len);
3448248818nd	(void) printf("\t\t\tzt_blk: %llu\n",
3458248818nd	    (u_longlong_t)zs.zs_ptrtbl_zt_blk);
3468248818nd	(void) printf("\t\t\tzt_numblks: %llu\n",
3478248818nd	    (u_longlong_t)zs.zs_ptrtbl_zt_numblks);
3488248818nd	(void) printf("\t\t\tzt_shift: %llu\n",
3498248818nd	    (u_longlong_t)zs.zs_ptrtbl_zt_shift);
3508248818nd	(void) printf("\t\t\tzt_blks_copied: %llu\n",
3518248818nd	    (u_longlong_t)zs.zs_ptrtbl_blks_copied);
3528248818nd	(void) printf("\t\t\tzt_nextblk: %llu\n",
3538248818nd	    (u_longlong_t)zs.zs_ptrtbl_nextblk);
3548248818nd
355fa9e406ahrens	(void) printf("\t\tZAP entries: %llu\n",
356fa9e406ahrens	    (u_longlong_t)zs.zs_num_entries);
357fa9e406ahrens	(void) printf("\t\tLeaf blocks: %llu\n",
358fa9e406ahrens	    (u_longlong_t)zs.zs_num_leafs);
359fa9e406ahrens	(void) printf("\t\tTotal blocks: %llu\n",
360fa9e406ahrens	    (u_longlong_t)zs.zs_num_blocks);
3618248818nd	(void) printf("\t\tzap_block_type: 0x%llx\n",
3628248818nd	    (u_longlong_t)zs.zs_block_type);
3638248818nd	(void) printf("\t\tzap_magic: 0x%llx\n",
3648248818nd	    (u_longlong_t)zs.zs_magic);
3658248818nd	(void) printf("\t\tzap_salt: 0x%llx\n",
3668248818nd	    (u_longlong_t)zs.zs_salt);
367fa9e406ahrens
368fa9e406ahrens	(void) printf("\t\tLeafs with 2^n pointers:\n");
3690713e23George Wilson	dump_histogram(zs.zs_leafs_with_2n_pointers, ZAP_HISTOGRAM_SIZE, 0);
370fa9e406ahrens
371fa9e406ahrens	(void) printf("\t\tBlocks with n*5 entries:\n");
3720713e23George Wilson	dump_histogram(zs.zs_blocks_with_n5_entries, ZAP_HISTOGRAM_SIZE, 0);
373fa9e406ahrens
374fa9e406ahrens	(void) printf("\t\tBlocks n/10 full:\n");
3750713e23George Wilson	dump_histogram(zs.zs_blocks_n_tenths_full, ZAP_HISTOGRAM_SIZE, 0);
376fa9e406ahrens
377fa9e406ahrens	(void) printf("\t\tEntries with n chunks:\n");
3780713e23George Wilson	dump_histogram(zs.zs_entries_using_n_chunks, ZAP_HISTOGRAM_SIZE, 0);
379fa9e406ahrens
380fa9e406ahrens	(void) printf("\t\tBuckets with n entries:\n");
3810713e23George Wilson	dump_histogram(zs.zs_buckets_with_n_entries, ZAP_HISTOGRAM_SIZE, 0);
382fa9e406ahrens}
383fa9e406ahrens
384fa9e406ahrens/*ARGSUSED*/
385fa9e406ahrensstatic void
386fa9e406ahrensdump_none(objset_t *os, uint64_t object, void *data, size_t size)
387fa9e406ahrens{
388fa9e406ahrens}
389fa9e406ahrens
390fa9e406ahrens/*ARGSUSED*/
3916de8f41Victor Latushkinstatic void
3926de8f41Victor Latushkindump_unknown(objset_t *os, uint64_t object, void *data, size_t size)
3936de8f41Victor Latushkin{
3946de8f41Victor Latushkin	(void) printf("\tUNKNOWN OBJECT TYPE\n");
3956de8f41Victor Latushkin}
3966de8f41Victor Latushkin
3976de8f41Victor Latushkin/*ARGSUSED*/
3983f7978dAlan Somersstatic void
399fa9e406ahrensdump_uint8(objset_t *os, uint64_t object, void *data, size_t size)
400fa9e406ahrens{
401fa9e406ahrens}
402fa9e406ahrens
403fa9e406ahrens/*ARGSUSED*/
404fa9e406ahrensstatic void
405fa9e406ahrensdump_uint64(objset_t *os, uint64_t object, void *data, size_t size)
406fa9e406ahrens{
407fa9e406ahrens}
408fa9e406ahrens
409fa9e406ahrens/*ARGSUSED*/
410fa9e406ahrensstatic void
411fa9e406ahrensdump_zap(objset_t *os, uint64_t object, void *data, size_t size)
412fa9e406ahrens{
413fa9e406ahrens	zap_cursor_t zc;
414fa9e406ahrens	zap_attribute_t attr;
415fa9e406ahrens	void *prop;
4163f7978dAlan Somers	unsigned i;
417fa9e406ahrens
418fa9e406ahrens	dump_zap_stats(os, object);
419fa9e406ahrens	(void) printf("\n");
420fa9e406ahrens
421fa9e406ahrens	for (zap_cursor_init(&zc, os, object);
422fa9e406ahrens	    zap_cursor_retrieve(&zc, &attr) == 0;
423fa9e406ahrens	    zap_cursor_advance(&zc)) {
424fa9e406ahrens		(void) printf("\t\t%s = ", attr.za_name);
425fa9e406ahrens		if (attr.za_num_integers == 0) {
426fa9e406ahrens			(void) printf("\n");
427fa9e406ahrens			continue;
428fa9e406ahrens		}
429fa9e406ahrens		prop = umem_zalloc(attr.za_num_integers *
430fa9e406ahrens		    attr.za_integer_length, UMEM_NOFAIL);
431fa9e406ahrens		(void) zap_lookup(os, object, attr.za_name,
432fa9e406ahrens		    attr.za_integer_length, attr.za_num_integers, prop);
433fa9e406ahrens		if (attr.za_integer_length == 1) {
434fa9e406ahrens			(void) printf("%s", (char *)prop);
435fa9e406ahrens		} else {
436fa9e406ahrens			for (i = 0; i < attr.za_num_integers; i++) {
437fa9e406ahrens				switch (attr.za_integer_length) {
438fa9e406ahrens				case 2:
439fa9e406ahrens					(void) printf("%u ",
440fa9e406ahrens					    ((uint16_t *)prop)[i]);
441fa9e406ahrens					break;
442fa9e406ahrens				case 4:
443fa9e406ahrens					(void) printf("%u ",
444fa9e406ahrens					    ((uint32_t *)prop)[i]);
445fa9e406ahrens					break;
446fa9e406ahrens				case 8:
447fa9e406ahrens					(void) printf("%lld ",
448fa9e406ahrens					    (u_longlong_t)((int64_t *)prop)[i]);
449fa9e406ahrens					break;
450fa9e406ahrens				}
451fa9e406ahrens			}
452fa9e406ahrens		}
453fa9e406ahrens		(void) printf("\n");
454fa9e406ahrens		umem_free(prop, attr.za_num_integers * attr.za_integer_length);
455fa9e406ahrens	}
45687e5029ahrens	zap_cursor_fini(&zc);
457fa9e406ahrens}
458fa9e406ahrens
459732885fMatthew Ahrensstatic void
460732885fMatthew Ahrensdump_bpobj(objset_t *os, uint64_t object, void *data, size_t size)
461732885fMatthew Ahrens{
462732885fMatthew Ahrens	bpobj_phys_t *bpop = data;
463732885fMatthew Ahrens	char bytes[32], comp[32], uncomp[32];
464732885fMatthew Ahrens
4650a05512Jason King	/* make sure the output won't get truncated */
4660a05512Jason King	CTASSERT(sizeof (bytes) >= NN_NUMBUF_SZ);
4670a05512Jason King	CTASSERT(sizeof (comp) >= NN_NUMBUF_SZ);
4680a05512Jason King	CTASSERT(sizeof (uncomp) >= NN_NUMBUF_SZ);
4690a05512Jason King
470732885fMatthew Ahrens	if (bpop == NULL)
471732885fMatthew Ahrens		return;
472732885fMatthew Ahrens
4730a05512Jason King	zdb_nicenum(bpop->bpo_bytes, bytes, sizeof (bytes));
4740a05512Jason King	zdb_nicenum(bpop->bpo_comp, comp, sizeof (comp));
4750a05512Jason King	zdb_nicenum(bpop->bpo_uncomp, uncomp, sizeof (uncomp));
476732885fMatthew Ahrens
477732885fMatthew Ahrens	(void) printf("\t\tnum_blkptrs = %llu\n",
478732885fMatthew Ahrens	    (u_longlong_t)bpop->bpo_num_blkptrs);
479732885fMatthew Ahrens	(void) printf("\t\tbytes = %s\n", bytes);
480732885fMatthew Ahrens	if (size >= BPOBJ_SIZE_V1) {
481732885fMatthew Ahrens		(void) printf("\t\tcomp = %s\n", comp);
482732885fMatthew Ahrens		(void) printf("\t\tuncomp = %s\n", uncomp);
483732885fMatthew Ahrens	}
484732885fMatthew Ahrens	if (size >= sizeof (*bpop)) {
485732885fMatthew Ahrens		(void) printf("\t\tsubobjs = %llu\n",
486732885fMatthew Ahrens		    (u_longlong_t)bpop->bpo_subobjs);
487732885fMatthew Ahrens		(void) printf("\t\tnum_subobjs = %llu\n",
488732885fMatthew Ahrens		    (u_longlong_t)bpop->bpo_num_subobjs);
489732885fMatthew Ahrens	}
490732885fMatthew Ahrens
491732885fMatthew Ahrens	if (dump_opt['d'] < 5)
492732885fMatthew Ahrens		return;
493732885fMatthew Ahrens
494732885fMatthew Ahrens	for (uint64_t i = 0; i < bpop->bpo_num_blkptrs; i++) {
495732885fMatthew Ahrens		char blkbuf[BP_SPRINTF_LEN];
496732885fMatthew Ahrens		blkptr_t bp;
497732885fMatthew Ahrens
498732885fMatthew Ahrens		int err = dmu_read(os, object,
499732885fMatthew Ahrens		    i * sizeof (bp), sizeof (bp), &bp, 0);
500732885fMatthew Ahrens		if (err != 0) {
501732885fMatthew Ahrens			(void) printf("got error %u from dmu_read\n", err);
502732885fMatthew Ahrens			break;
503732885fMatthew Ahrens		}
504732885fMatthew Ahrens		snprintf_blkptr_compact(blkbuf, sizeof (blkbuf), &bp);
505732885fMatthew Ahrens		(void) printf("\t%s\n", blkbuf);
506732885fMatthew Ahrens	}
507732885fMatthew Ahrens}
508732885fMatthew Ahrens
509732885fMatthew Ahrens/* ARGSUSED */
510732885fMatthew Ahrensstatic void
511732885fMatthew Ahrensdump_bpobj_subobjs(objset_t *os, uint64_t object, void *data, size_t size)
512732885fMatthew Ahrens{
513732885fMatthew Ahrens	dmu_object_info_t doi;
514732885fMatthew Ahrens
515732885fMatthew Ahrens	VERIFY0(dmu_object_info(os, object, &doi));
516732885fMatthew Ahrens	uint64_t *subobjs = kmem_alloc(doi.doi_max_offset, KM_SLEEP);
517732885fMatthew Ahrens
518732885fMatthew Ahrens	int err = dmu_read(os, object, 0, doi.doi_max_offset, subobjs, 0);
519732885fMatthew Ahrens	if (err != 0) {
520732885fMatthew Ahrens		(void) printf("got error %u from dmu_read\n", err);
521732885fMatthew Ahrens		kmem_free(subobjs, doi.doi_max_offset);
522732885fMatthew Ahrens		return;
523732885fMatthew Ahrens	}
524732885fMatthew Ahrens
525732885fMatthew Ahrens	int64_t last_nonzero = -1;
526732885fMatthew Ahrens	for (uint64_t i = 0; i < doi.doi_max_offset / 8; i++) {
527732885fMatthew Ahrens		if (subobjs[i] != 0)
528732885fMatthew Ahrens			last_nonzero = i;
529732885fMatthew Ahrens	}
530732885fMatthew Ahrens
531732885fMatthew Ahrens	for (int64_t i = 0; i <= last_nonzero; i++) {
532732885fMatthew Ahrens		(void) printf("\t%llu\n", (longlong_t)subobjs[i]);
533732885fMatthew Ahrens	}
534732885fMatthew Ahrens	kmem_free(subobjs, doi.doi_max_offset);
535732885fMatthew Ahrens}
536732885fMatthew Ahrens
537e743726ahrens/*ARGSUSED*/
538e743726ahrensstatic void
539486ae71Matthew Ahrensdump_ddt_zap(objset_t *os, uint64_t object, void *data, size_t size)
540486ae71Matthew Ahrens{
541486ae71Matthew Ahrens	dump_zap_stats(os, object);
542486ae71Matthew Ahrens	/* contents are printed elsewhere, properly decoded */
543486ae71Matthew Ahrens}
544486ae71Matthew Ahrens
545486ae71Matthew Ahrens/*ARGSUSED*/
546486ae71Matthew Ahrensstatic void
5470a586ceMark Shellenbaumdump_sa_attrs(objset_t *os, uint64_t object, void *data, size_t size)
5480a586ceMark Shellenbaum{
5490a586ceMark Shellenbaum	zap_cursor_t zc;
5500a586ceMark Shellenbaum	zap_attribute_t attr;
5510a586ceMark Shellenbaum
5520a586ceMark Shellenbaum	dump_zap_stats(os, object);
5530a586ceMark Shellenbaum	(void) printf("\n");
5540a586ceMark Shellenbaum
5550a586ceMark Shellenbaum	for (zap_cursor_init(&zc, os, object);
5560a586ceMark Shellenbaum	    zap_cursor_retrieve(&zc, &attr) == 0;
5570a586ceMark Shellenbaum	    zap_cursor_advance(&zc)) {
5580a586ceMark Shellenbaum		(void) printf("\t\t%s = ", attr.za_name);
5590a586ceMark Shellenbaum		if (attr.za_num_integers == 0) {
5600a586ceMark Shellenbaum			(void) printf("\n");
5610a586ceMark Shellenbaum			continue;
5620a586ceMark Shellenbaum		}
5630a586ceMark Shellenbaum		(void) printf(" %llx : [%d:%d:%d]\n",
5640a586ceMark Shellenbaum		    (u_longlong_t)attr.za_first_integer,
5650a586ceMark Shellenbaum		    (int)ATTR_LENGTH(attr.za_first_integer),
5660a586ceMark Shellenbaum		    (int)ATTR_BSWAP(attr.za_first_integer),
5670a586ceMark Shellenbaum		    (int)ATTR_NUM(attr.za_first_integer));
5680a586ceMark Shellenbaum	}
5690a586ceMark Shellenbaum	zap_cursor_fini(&zc);
5700a586ceMark Shellenbaum}
5710a586ceMark Shellenbaum
5720a586ceMark Shellenbaum/*ARGSUSED*/
5730a586ceMark Shellenbaumstatic void
5740a586ceMark Shellenbaumdump_sa_layouts(objset_t *os, uint64_t object, void *data, size_t size)
5750a586ceMark Shellenbaum{
5760a586ceMark Shellenbaum	zap_cursor_t zc;
5770a586ceMark Shellenbaum	zap_attribute_t attr;
5780a586ceMark Shellenbaum	uint16_t *layout_attrs;
5793f7978dAlan Somers	unsigned i;
5800a586ceMark Shellenbaum
5810a586ceMark Shellenbaum	dump_zap_stats(os, object);
5820a586ceMark Shellenbaum	(void) printf("\n");
5830a586ceMark Shellenbaum
5840a586ceMark Shellenbaum	for (zap_cursor_init(&zc, os, object);
5850a586ceMark Shellenbaum	    zap_cursor_retrieve(&zc, &attr) == 0;
5860a586ceMark Shellenbaum	    zap_cursor_advance(&zc)) {
5870a586ceMark Shellenbaum		(void) printf("\t\t%s = [", attr.za_name);
5880a586ceMark Shellenbaum		if (attr.za_num_integers == 0) {
5890a586ceMark Shellenbaum			(void) printf("\n");
5900a586ceMark Shellenbaum			continue;
5910a586ceMark Shellenbaum		}
5920a586ceMark Shellenbaum
5930a586ceMark Shellenbaum		VERIFY(attr.za_integer_length == 2);
5940a586ceMark Shellenbaum		layout_attrs = umem_zalloc(attr.za_num_integers *
5950a586ceMark Shellenbaum		    attr.za_integer_length, UMEM_NOFAIL);
5960a586ceMark Shellenbaum
5970a586ceMark Shellenbaum		VERIFY(zap_lookup(os, object, attr.za_name,
5980a586ceMark Shellenbaum		    attr.za_integer_length,
5990a586ceMark Shellenbaum		    attr.za_num_integers, layout_attrs) == 0);
6000a586ceMark Shellenbaum
6010a586ceMark Shellenbaum		for (i = 0; i != attr.za_num_integers; i++)
6020a586ceMark Shellenbaum			(void) printf(" %d ", (int)layout_attrs[i]);
6030a586ceMark Shellenbaum		(void) printf("]\n");
6040a586ceMark Shellenbaum		umem_free(layout_attrs,
6050a586ceMark Shellenbaum		    attr.za_num_integers * attr.za_integer_length);
6060a586ceMark Shellenbaum	}
6070a586ceMark Shellenbaum	zap_cursor_fini(&zc);
6080a586ceMark Shellenbaum}
6090a586ceMark Shellenbaum
6100a586ceMark Shellenbaum/*ARGSUSED*/
6110a586ceMark Shellenbaumstatic void
612e743726ahrensdump_zpldir(objset_t *os, uint64_t object, void *data, size_t size)
613e743726ahrens{
614e743726ahrens	zap_cursor_t zc;
615e743726ahrens	zap_attribute_t attr;
616e743726ahrens	const char *typenames[] = {
617e743726ahrens		/* 0 */ "not specified",
618e743726ahrens		/* 1 */ "FIFO",
619e743726ahrens		/* 2 */ "Character Device",
620e743726ahrens		/* 3 */ "3 (invalid)",
621e743726ahrens		/* 4 */ "Directory",
622e743726ahrens		/* 5 */ "5 (invalid)",
623e743726ahrens		/* 6 */ "Block Device",
624e743726ahrens		/* 7 */ "7 (invalid)",
625e743726ahrens		/* 8 */ "Regular File",
626e743726ahrens		/* 9 */ "9 (invalid)",
627e743726ahrens		/* 10 */ "Symbolic Link",
628e743726ahrens		/* 11 */ "11 (invalid)",
629e743726ahrens		/* 12 */ "Socket",
630e743726ahrens		/* 13 */ "Door",
631e743726ahrens		/* 14 */ "Event Port",
632e743726ahrens		/* 15 */ "15 (invalid)",
633e743726ahrens	};
634e743726ahrens
635e743726ahrens	dump_zap_stats(os, object);
636e743726ahrens	(void) printf("\n");
637e743726ahrens
638e743726ahrens	for (zap_cursor_init(&zc, os, object);
639e743726ahrens	    zap_cursor_retrieve(&zc, &attr) == 0;
640e743726ahrens	    zap_cursor_advance(&zc)) {
641e743726ahrens		(void) printf("\t\t%s = %lld (type: %s)\n",
642e743726ahrens		    attr.za_name, ZFS_DIRENT_OBJ(attr.za_first_integer),
643e743726ahrens		    typenames[ZFS_DIRENT_TYPE(attr.za_first_integer)]);
644e743726ahrens	}
645e743726ahrens	zap_cursor_fini(&zc);
646e743726ahrens}
647e743726ahrens
6483f7978dAlan Somersstatic int
6490713e23George Wilsonget_dtl_refcount(vdev_t *vd)
6500713e23George Wilson{
6510713e23George Wilson	int refcount = 0;
6520713e23George Wilson
6530713e23George Wilson	if (vd->vdev_ops->vdev_op_leaf) {
6540713e23George Wilson		space_map_t *sm = vd->vdev_dtl_sm;
6550713e23George Wilson
6560713e23George Wilson		if (sm != NULL &&
6570713e23George Wilson		    sm->sm_dbuf->db_size == sizeof (space_map_phys_t))
6580713e23George Wilson			return (1);
6590713e23George Wilson		return (0);
6600713e23George Wilson	}
6610713e23George Wilson
6623f7978dAlan Somers	for (unsigned c = 0; c < vd->vdev_children; c++)
6630713e23George Wilson		refcount += get_dtl_refcount(vd->vdev_child[c]);
6640713e23George Wilson	return (refcount);
6650713e23George Wilson}
6660713e23George Wilson
6673f7978dAlan Somersstatic int
6680713e23George Wilsonget_metaslab_refcount(vdev_t *vd)
6690713e23George Wilson{
6700713e23George Wilson	int refcount = 0;
6710713e23George Wilson
6725cabbc6Prashanth Sreenivasa	if (vd->vdev_top == vd) {
6735cabbc6Prashanth Sreenivasa		for (uint64_t m = 0; m < vd->vdev_ms_count; m++) {
6740713e23George Wilson			space_map_t *sm = vd->vdev_ms[m]->ms_sm;
6750713e23George Wilson
6760713e23George Wilson			if (sm != NULL &&
6770713e23George Wilson			    sm->sm_dbuf->db_size == sizeof (space_map_phys_t))
6780713e23George Wilson				refcount++;
6790713e23George Wilson		}
6800713e23George Wilson	}
6813f7978dAlan Somers	for (unsigned c = 0; c < vd->vdev_children; c++)
6820713e23George Wilson		refcount += get_metaslab_refcount(vd->vdev_child[c]);
6830713e23George Wilson
6840713e23George Wilson	return (refcount);
6850713e23George Wilson}
6860713e23George Wilson
6870713e23George Wilsonstatic int
6885cabbc6Prashanth Sreenivasaget_obsolete_refcount(vdev_t *vd)
6895cabbc6Prashanth Sreenivasa{
6905cabbc6Prashanth Sreenivasa	int refcount = 0;
6915cabbc6Prashanth Sreenivasa
6925cabbc6Prashanth Sreenivasa	uint64_t obsolete_sm_obj = vdev_obsolete_sm_object(vd);
6935cabbc6Prashanth Sreenivasa	if (vd->vdev_top == vd && obsolete_sm_obj != 0) {
6945cabbc6Prashanth Sreenivasa		dmu_object_info_t doi;
6955cabbc6Prashanth Sreenivasa		VERIFY0(dmu_object_info(vd->vdev_spa->spa_meta_objset,
6965cabbc6Prashanth Sreenivasa		    obsolete_sm_obj, &doi));
6975cabbc6Prashanth Sreenivasa		if (doi.doi_bonus_size == sizeof (space_map_phys_t)) {
6985cabbc6Prashanth Sreenivasa			refcount++;
6995cabbc6Prashanth Sreenivasa		}
7005cabbc6Prashanth Sreenivasa	} else {
7015cabbc6Prashanth Sreenivasa		ASSERT3P(vd->vdev_obsolete_sm, ==, NULL);
7025cabbc6Prashanth Sreenivasa		ASSERT3U(obsolete_sm_obj, ==, 0);
7035cabbc6Prashanth Sreenivasa	}
7045cabbc6Prashanth Sreenivasa	for (unsigned c = 0; c < vd->vdev_children; c++) {
7055cabbc6Prashanth Sreenivasa		refcount += get_obsolete_refcount(vd->vdev_child[c]);
7065cabbc6Prashanth Sreenivasa	}
7075cabbc6Prashanth Sreenivasa
7085cabbc6Prashanth Sreenivasa	return (refcount);
7095cabbc6Prashanth Sreenivasa}
7105cabbc6Prashanth Sreenivasa
7115cabbc6Prashanth Sreenivasastatic int
7125cabbc6Prashanth Sreenivasaget_prev_obsolete_spacemap_refcount(spa_t *spa)
7135cabbc6Prashanth Sreenivasa{
7145cabbc6Prashanth Sreenivasa	uint64_t prev_obj =
7155cabbc6Prashanth Sreenivasa	    spa->spa_condensing_indirect_phys.scip_prev_obsolete_sm_object;
7165cabbc6Prashanth Sreenivasa	if (prev_obj != 0) {
7175cabbc6Prashanth Sreenivasa		dmu_object_info_t doi;
7185cabbc6Prashanth Sreenivasa		VERIFY0(dmu_object_info(spa->spa_meta_objset, prev_obj, &doi));
7195cabbc6Prashanth Sreenivasa		if (doi.doi_bonus_size == sizeof (space_map_phys_t)) {
7205cabbc6Prashanth Sreenivasa			return (1);
7215cabbc6Prashanth Sreenivasa		}
7225cabbc6Prashanth Sreenivasa	}
7235cabbc6Prashanth Sreenivasa	return (0);
7245cabbc6Prashanth Sreenivasa}
7255cabbc6Prashanth Sreenivasa
7265cabbc6Prashanth Sreenivasastatic int
7270713e23George Wilsonverify_spacemap_refcounts(spa_t *spa)
7280713e23George Wilson{
7292acef22Matthew Ahrens	uint64_t expected_refcount = 0;
7302acef22Matthew Ahrens	uint64_t actual_refcount;
7310713e23George Wilson
7322acef22Matthew Ahrens	(void) feature_get_refcount(spa,
7332acef22Matthew Ahrens	    &spa_feature_table[SPA_FEATURE_SPACEMAP_HISTOGRAM],
7342acef22Matthew Ahrens	    &expected_refcount);
7350713e23George Wilson	actual_refcount = get_dtl_refcount(spa->spa_root_vdev);
7360713e23George Wilson	actual_refcount += get_metaslab_refcount(spa->spa_root_vdev);
7375cabbc6Prashanth Sreenivasa	actual_refcount += get_obsolete_refcount(spa->spa_root_vdev);
7385cabbc6Prashanth Sreenivasa	actual_refcount += get_prev_obsolete_spacemap_refcount(spa);
7390713e23George Wilson
7400713e23George Wilson	if (expected_refcount != actual_refcount) {
7412acef22Matthew Ahrens		(void) printf("space map refcount mismatch: expected %lld != "
7422acef22Matthew Ahrens		    "actual %lld\n",
7432acef22Matthew Ahrens		    (longlong_t)expected_refcount,
7442acef22Matthew Ahrens		    (longlong_t)actual_refcount);
7450713e23George Wilson		return (2);
7460713e23George Wilson	}
7470713e23George Wilson	return (0);
7480713e23George Wilson}
7490713e23George Wilson
750fa9e406ahrensstatic void
7510713e23George Wilsondump_spacemap(objset_t *os, space_map_t *sm)
752fa9e406ahrens{
753fa9e406ahrens	uint64_t alloc, offset, entry;
7545cabbc6Prashanth Sreenivasa	char *ddata[] = { "ALLOC", "FREE", "CONDENSE", "INVALID",
7555cabbc6Prashanth Sreenivasa	    "INVALID", "INVALID", "INVALID", "INVALID" };
756fa9e406ahrens
7570713e23George Wilson	if (sm == NULL)
758fa9e406ahrens		return;
759fa9e406ahrens
7605cabbc6Prashanth Sreenivasa	(void) printf("space map object %llu:\n",
7615cabbc6Prashanth Sreenivasa	    (longlong_t)sm->sm_phys->smp_object);
7625cabbc6Prashanth Sreenivasa	(void) printf("  smp_objsize = 0x%llx\n",
7635cabbc6Prashanth Sreenivasa	    (longlong_t)sm->sm_phys->smp_objsize);
7645cabbc6Prashanth Sreenivasa	(void) printf("  smp_alloc = 0x%llx\n",
7655cabbc6Prashanth Sreenivasa	    (longlong_t)sm->sm_phys->smp_alloc);
7665cabbc6Prashanth Sreenivasa
767fa9e406ahrens	/*
768fa9e406ahrens	 * Print out the freelist entries in both encoded and decoded form.
769fa9e406ahrens	 */
770fa9e406ahrens	alloc = 0;
7710713e23George Wilson	for (offset = 0; offset < space_map_length(sm);
7720713e23George Wilson	    offset += sizeof (entry)) {
7730713e23George Wilson		uint8_t mapshift = sm->sm_shift;
7740713e23George Wilson
7750713e23George Wilson		VERIFY0(dmu_read(os, space_map_object(sm), offset,
7767bfdf01Neil Perrin		    sizeof (entry), &entry, DMU_READ_PREFETCH));
777fa9e406ahrens		if (SM_DEBUG_DECODE(entry)) {
7780713e23George Wilson
77987219dbVictor Latushkin			(void) printf("\t    [%6llu] %s: txg %llu, pass %llu\n",
780fa9e406ahrens			    (u_longlong_t)(offset / sizeof (entry)),
781fa9e406ahrens			    ddata[SM_DEBUG_ACTION_DECODE(entry)],
7825ad8204nd			    (u_longlong_t)SM_DEBUG_TXG_DECODE(entry),
7835ad8204nd			    (u_longlong_t)SM_DEBUG_SYNCPASS_DECODE(entry));
784fa9e406ahrens		} else {
78587219dbVictor Latushkin			(void) printf("\t    [%6llu]    %c  range:"
78687219dbVictor Latushkin			    " %010llx-%010llx  size: %06llx\n",
787fa9e406ahrens			    (u_longlong_t)(offset / sizeof (entry)),
788fa9e406ahrens			    SM_TYPE_DECODE(entry) == SM_ALLOC ? 'A' : 'F',
7895ad8204nd			    (u_longlong_t)((SM_OFFSET_DECODE(entry) <<
7900713e23George Wilson			    mapshift) + sm->sm_start),
7915ad8204nd			    (u_longlong_t)((SM_OFFSET_DECODE(entry) <<
7920713e23George Wilson			    mapshift) + sm->sm_start +
7930713e23George Wilson			    (SM_RUN_DECODE(entry) << mapshift)),
7945ad8204nd			    (u_longlong_t)(SM_RUN_DECODE(entry) << mapshift));
795fa9e406ahrens			if (SM_TYPE_DECODE(entry) == SM_ALLOC)
796fa9e406ahrens				alloc += SM_RUN_DECODE(entry) << mapshift;
797fa9e406ahrens			else
798fa9e406ahrens				alloc -= SM_RUN_DECODE(entry) << mapshift;
799fa9e406ahrens		}
800fa9e406ahrens	}
8010713e23George Wilson	if (alloc != space_map_allocated(sm)) {
802fa9e406ahrens		(void) printf("space_map_object alloc (%llu) INCONSISTENT "
803fa9e406ahrens		    "with space map summary (%llu)\n",
8040713e23George Wilson		    (u_longlong_t)space_map_allocated(sm), (u_longlong_t)alloc);
805fa9e406ahrens	}
806fa9e406ahrens}
807fa9e406ahrens
808fa9e406ahrensstatic void
809d6e555bGeorge Wilsondump_metaslab_stats(metaslab_t *msp)
810d6e555bGeorge Wilson{
8113f9d6adLin Ling	char maxbuf[32];
8120713e23George Wilson	range_tree_t *rt = msp->ms_tree;
8130713e23George Wilson	avl_tree_t *t = &msp->ms_size_tree;
8140713e23George Wilson	int free_pct = range_tree_space(rt) * 100 / msp->ms_size;
815d6e555bGeorge Wilson
8160a05512Jason King	/* max sure nicenum has enough space */
8170a05512Jason King	CTASSERT(sizeof (maxbuf) >= NN_NUMBUF_SZ);
8180a05512Jason King
8190a05512Jason King	zdb_nicenum(metaslab_block_maxsize(msp), maxbuf, sizeof (maxbuf));
820d6e555bGeorge Wilson
82187219dbVictor Latushkin	(void) printf("\t %25s %10lu   %7s  %6s   %4s %4d%%\n",
822d6e555bGeorge Wilson	    "segments", avl_numnodes(t), "maxsize", maxbuf,
823d6e555bGeorge Wilson	    "freepct", free_pct);
8240713e23George Wilson	(void) printf("\tIn-memory histogram:\n");
8250713e23George Wilson	dump_histogram(rt->rt_histogram, RANGE_TREE_HISTOGRAM_SIZE, 0);
826d6e555bGeorge Wilson}
827d6e555bGeorge Wilson
828d6e555bGeorge Wilsonstatic void
829fa9e406ahrensdump_metaslab(metaslab_t *msp)
830fa9e406ahrens{
831fa9e406ahrens	vdev_t *vd = msp->ms_group->mg_vd;
832fa9e406ahrens	spa_t *spa = vd->vdev_spa;
8330713e23George Wilson	space_map_t *sm = msp->ms_sm;
8343f9d6adLin Ling	char freebuf[32];
835fa9e406ahrens
8360a05512Jason King	zdb_nicenum(msp->ms_size - space_map_allocated(sm), freebuf,
8370a05512Jason King	    sizeof (freebuf));
838fa9e406ahrens
839fa9e406ahrens	(void) printf(
84087219dbVictor Latushkin	    "\tmetaslab %6llu   offset %12llx   spacemap %6llu   free    %5s\n",
8410713e23George Wilson	    (u_longlong_t)msp->ms_id, (u_longlong_t)msp->ms_start,
8420713e23George Wilson	    (u_longlong_t)space_map_object(sm), freebuf);
843fa9e406ahrens
8440713e23George Wilson	if (dump_opt['m'] > 2 && !dump_opt['L']) {
845d6e555bGeorge Wilson		mutex_enter(&msp->ms_lock);
8460713e23George Wilson		metaslab_load_wait(msp);
8470713e23George Wilson		if (!msp->ms_loaded) {
8480713e23George Wilson			VERIFY0(metaslab_load(msp));
8490713e23George Wilson			range_tree_stat_verify(msp->ms_tree);
8500713e23George Wilson		}
851bc3975bVictor Latushkin		dump_metaslab_stats(msp);
8520713e23George Wilson		metaslab_unload(msp);
853d6e555bGeorge Wilson		mutex_exit(&msp->ms_lock);
854d6e555bGeorge Wilson	}
855d6e555bGeorge Wilson
8560713e23George Wilson	if (dump_opt['m'] > 1 && sm != NULL &&
8572acef22Matthew Ahrens	    spa_feature_is_active(spa, SPA_FEATURE_SPACEMAP_HISTOGRAM)) {
8580713e23George Wilson		/*
8590713e23George Wilson		 * The space map histogram represents free space in chunks
8600713e23George Wilson		 * of sm_shift (i.e. bucket 0 refers to 2^sm_shift).
8610713e23George Wilson		 */
8622e4c998George Wilson		(void) printf("\tOn-disk histogram:\t\tfragmentation %llu\n",
8632e4c998George Wilson		    (u_longlong_t)msp->ms_fragmentation);
8640713e23George Wilson		dump_histogram(sm->sm_phys->smp_histogram,
8652e4c998George Wilson		    SPACE_MAP_HISTOGRAM_SIZE, sm->sm_shift);
8660713e23George Wilson	}
8670713e23George Wilson
8680713e23George Wilson	if (dump_opt['d'] > 5 || dump_opt['m'] > 3) {
8690713e23George Wilson		ASSERT(msp->ms_size == (1ULL << vd->vdev_ms_shift));
870d6e555bGeorge Wilson
8710713e23George Wilson		dump_spacemap(spa->spa_meta_objset, msp->ms_sm);
872d6e555bGeorge Wilson	}
87387219dbVictor Latushkin}
874fa9e406