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.
24af1d63aPaul Dagnelie * Copyright (c) 2011, 2019 by Delphix. All rights reserved.
25c3d26abMatthew Ahrens * Copyright (c) 2014 Integros [integros.com]
26ed61ec1Yuri Pankov * Copyright 2017 Nexenta Systems, Inc.
27e0f1c0aOlaf Faaland * Copyright (c) 2017, 2018 Lawrence Livermore National Security, LLC.
28f06dce2Andrew Stormont * Copyright 2017 RackTop Systems.
29fa9e406ahrens */
30fa9e406ahrens
31fa9e406ahrens#include <stdio.h>
32490d05bMatthew Ahrens#include <unistd.h>
33004388ecasper#include <stdio_ext.h>
34fa9e406ahrens#include <stdlib.h>
3544cd46cbillm#include <ctype.h>
36fa9e406ahrens#include <sys/zfs_context.h>
37fa9e406ahrens#include <sys/spa.h>
38fa9e406ahrens#include <sys/spa_impl.h>
39fa9e406ahrens#include <sys/dmu.h>
40fa9e406ahrens#include <sys/zap.h>
41fa9e406ahrens#include <sys/fs/zfs.h>
42fa9e406ahrens#include <sys/zfs_znode.h>
430a586ceMark Shellenbaum#include <sys/zfs_sa.h>
440a586ceMark Shellenbaum#include <sys/sa.h>
450a586ceMark Shellenbaum#include <sys/sa_impl.h>
46fa9e406ahrens#include <sys/vdev.h>
47fa9e406ahrens#include <sys/vdev_impl.h>
48fa9e406ahrens#include <sys/metaslab_impl.h>
49fa9e406ahrens#include <sys/dmu_objset.h>
50fa9e406ahrens#include <sys/dsl_dir.h>
51fa9e406ahrens#include <sys/dsl_dataset.h>
52fa9e406ahrens#include <sys/dsl_pool.h>
53fa9e406ahrens#include <sys/dbuf.h>
54fa9e406ahrens#include <sys/zil.h>
55fa9e406ahrens#include <sys/zil_impl.h>
56fa9e406ahrens#include <sys/stat.h>
57fa9e406ahrens#include <sys/resource.h>
58fa9e406ahrens#include <sys/dmu_traverse.h>
59fa9e406ahrens#include <sys/zio_checksum.h>
60fa9e406ahrens#include <sys/zio_compress.h>
61e0d35c4marks#include <sys/zfs_fuid.h>
6288b7b0fMatthew Ahrens#include <sys/arc.h>
63b24ab67Jeff Bonwick#include <sys/ddt.h>
64ad135b5Christopher Siden#include <sys/zfeature.h>
65770499eDan Kimmel#include <sys/abd.h>
664923c69Matthew Ahrens#include <sys/blkptr.h>
6717fb938Matthew Ahrens#include <sys/dsl_scan.h>
68eb63303Tom Caputi#include <sys/dsl_crypt.h>
694445fffMatthew Ahrens#include <zfs_comutil.h>
700a05512Jason King#include <libcmdutils.h>
71de6628fck#undef verify
72de6628fck#include <libzfs.h>
73fa9e406ahrens
743f7978dAlan Somers#include "zdb.h"
753f7978dAlan Somers
76e690fb2Christopher Siden#define	ZDB_COMPRESS_NAME(idx) ((idx) < ZIO_COMPRESS_FUNCTIONS ?	\
77e690fb2Christopher Siden	zio_compress_table[(idx)].ci_name : "UNKNOWN")
78e690fb2Christopher Siden#define	ZDB_CHECKSUM_NAME(idx) ((idx) < ZIO_CHECKSUM_FUNCTIONS ?	\
79e690fb2Christopher Siden	zio_checksum_table[(idx)].ci_name : "UNKNOWN")
80e690fb2Christopher Siden#define	ZDB_OT_NAME(idx) ((idx) < DMU_OT_NUMTYPES ?	\
81e690fb2Christopher Siden	dmu_ot[(idx)].ot_name : DMU_OT_IS_VALID(idx) ?	\
82e690fb2Christopher Siden	dmu_ot_byteswap[DMU_OT_BYTESWAP(idx)].ob_name : "UNKNOWN")
83e690fb2Christopher Siden#define	ZDB_OT_TYPE(idx) ((idx) < DMU_OT_NUMTYPES ? (idx) :		\
845cabbc6Prashanth Sreenivasa	(idx) == DMU_OTN_ZAP_DATA || (idx) == DMU_OTN_ZAP_METADATA ?	\
855cabbc6Prashanth Sreenivasa	DMU_OT_ZAP_OTHER : \
865cabbc6Prashanth Sreenivasa	(idx) == DMU_OTN_UINT64_DATA || (idx) == DMU_OTN_UINT64_METADATA ? \
875cabbc6Prashanth Sreenivasa	DMU_OT_UINT64_OTHER : DMU_OT_NUMTYPES)
886de8f41Victor Latushkin
894dd77f9Matthew Ahrensextern int reference_tracking_enable;
907fd05acMatthew Ahrensextern boolean_t zfs_recover;
9106be980Matthew Ahrensextern uint64_t zfs_arc_max, zfs_arc_meta_limit;
92f7950bfMatthew Ahrensextern int zfs_vdev_async_read_max_active;
93f06dce2Andrew Stormontextern int aok;
94e144c4ePavel Zakharovextern boolean_t spa_load_verify_dryrun;
954d7988dPaul Dagnelieextern int zfs_btree_verify_intensity;
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;
10620b5dafPaul Dagneliestatic int leaked_objects = 0;
107fa9e406ahrens
108732885fMatthew Ahrensstatic void snprintf_blkptr_compact(char *, size_t, const blkptr_t *);
10917fb938Matthew Ahrensstatic void mos_obj_refd(uint64_t);
110732885fMatthew Ahrens
111fa9e406ahrens/*
112fa9e406ahrens * These libumem hooks provide a reasonable set of defaults for the allocator's
113fa9e406ahrens * debugging facilities.
114fa9e406ahrens */
115fa9e406ahrensconst char *
116fa9e406ahrens_umem_debug_init()
117fa9e406ahrens{
118fa9e406ahrens	return ("default,verbose"); /* $UMEM_DEBUG setting */
119fa9e406ahrens}
120fa9e406ahrens
121fa9e406ahrensconst char *
122fa9e406ahrens_umem_logging_init(void)
123fa9e406ahrens{
124fa9e406ahrens	return ("fail,contents"); /* $UMEM_LOGGING setting */
125fa9e406ahrens}
126fa9e406ahrens
127fa9e406ahrensstatic void
128fa9e406ahrensusage(void)
129fa9e406ahrens{
130fa9e406ahrens	(void) fprintf(stderr,
1318671400Serapheim Dimitropoulos	    "Usage:\t%s [-AbcdDFGhikLMPsvX] [-e [-V] [-p <path> ...]] "
132ed61ec1Yuri Pankov	    "[-I <inflight I/Os>]\n"
133ed61ec1Yuri Pankov	    "\t\t[-o <var>=<value>]... [-t <txg>] [-U <cache>] [-x <dumpdir>]\n"
134ed61ec1Yuri Pankov	    "\t\t[<poolname> [<object> ...]]\n"
135dfd5965Richard Yao	    "\t%s [-AdiPv] [-e [-V] [-p <path> ...]] [-U <cache>] <dataset> "
136ed61ec1Yuri Pankov	    "[<object> ...]\n"
137ed61ec1Yuri Pankov	    "\t%s -C [-A] [-U <cache>]\n"
138ed61ec1Yuri Pankov	    "\t%s -l [-Aqu] <device>\n"
139dfd5965Richard Yao	    "\t%s -m [-AFLPX] [-e [-V] [-p <path> ...]] [-t <txg>] "
140dfd5965Richard Yao	    "[-U <cache>]\n\t\t<poolname> [<vdev> [<metaslab> ...]]\n"
141ed61ec1Yuri Pankov	    "\t%s -O <dataset> <path>\n"
142dfd5965Richard Yao	    "\t%s -R [-A] [-e [-V] [-p <path> ...]] [-U <cache>]\n"
143ed61ec1Yuri Pankov	    "\t\t<poolname> <vdev>:<offset>:<size>[:<flags>]\n"
1444923c69Matthew Ahrens	    "\t%s -E [-A] word0:word1:...:word15\n"
145dfd5965Richard Yao	    "\t%s -S [-AP] [-e [-V] [-p <path> ...]] [-U <cache>] "
146dfd5965Richard Yao	    "<poolname>\n\n",
147ed61ec1Yuri Pankov	    cmdname, cmdname, cmdname, cmdname, cmdname, cmdname, cmdname,
1484923c69Matthew Ahrens	    cmdname, cmdname);
1493ad6c7fVictor Latushkin
1503ad6c7fVictor Latushkin	(void) fprintf(stderr, "    Dataset name must include at least one "
1513ad6c7fVictor Latushkin	    "separator character '/' or '@'\n");
1523ad6c7fVictor Latushkin	(void) fprintf(stderr, "    If dataset name is specified, only that "
1533ad6c7fVictor Latushkin	    "dataset is dumped\n");
1543ad6c7fVictor Latushkin	(void) fprintf(stderr, "    If object numbers are specified, only "
1553ad6c7fVictor Latushkin	    "those objects are dumped\n\n");
1563ad6c7fVictor Latushkin	(void) fprintf(stderr, "    Options to control amount of output:\n");
1573ad6c7fVictor Latushkin	(void) fprintf(stderr, "        -b block statistics\n");
1583ad6c7fVictor Latushkin	(void) fprintf(stderr, "        -c checksum all metadata (twice for "
1596365109Victor Latushkin	    "all data) blocks\n");
160ed61ec1Yuri Pankov	(void) fprintf(stderr, "        -C config (or cachefile if alone)\n");
161ed61ec1Yuri Pankov	(void) fprintf(stderr, "        -d dataset(s)\n");
162f0ba89bJeff Bonwick	(void) fprintf(stderr, "        -D dedup statistics\n");
1634923c69Matthew Ahrens	(void) fprintf(stderr, "        -E decode and display block from an "
1644923c69Matthew Ahrens	    "embedded block pointer\n");
165ed61ec1Yuri Pankov	(void) fprintf(stderr, "        -h pool history\n");
166ed61ec1Yuri Pankov	(void) fprintf(stderr, "        -i intent logs\n");
16764723e3Yuri Pankov	(void) fprintf(stderr, "        -l read label contents\n");
1688671400Serapheim Dimitropoulos	(void) fprintf(stderr, "        -k examine the checkpointed state "
1698671400Serapheim Dimitropoulos	    "of the pool\n");
17082a0a98Victor Latushkin	(void) fprintf(stderr, "        -L disable leak tracking (do not "
17182a0a98Victor Latushkin	    "load spacemaps)\n");
172ed61ec1Yuri Pankov	(void) fprintf(stderr, "        -m metaslabs\n");
173ed61ec1Yuri Pankov	(void) fprintf(stderr, "        -M metaslab groups\n");
174ed61ec1Yuri Pankov	(void) fprintf(stderr, "        -O perform object lookups by path\n");
175d41e764ek	(void) fprintf(stderr, "        -R read and display block from a "
176ed61ec1Yuri Pankov	    "device\n");
177ed61ec1Yuri Pankov	(void) fprintf(stderr, "        -s report stats on zdb's I/O\n");
178ed61ec1Yuri Pankov	(void) fprintf(stderr, "        -S simulate dedup to measure effect\n");
179ed61ec1Yuri Pankov	(void) fprintf(stderr, "        -v verbose (applies to all "
180ed61ec1Yuri Pankov	    "others)\n\n");
1813ad6c7fVictor Latushkin	(void) fprintf(stderr, "    Below options are intended for use "
182df15e41Matthew Ahrens	    "with other options:\n");
183feef89cVictor Latushkin	(void) fprintf(stderr, "        -A ignore assertions (-A), enable "
184feef89cVictor Latushkin	    "panic recovery (-AA) or both (-AAA)\n");
1853ad6c7fVictor Latushkin	(void) fprintf(stderr, "        -e pool is exported/destroyed/"
1863ad6c7fVictor Latushkin	    "has altroot/not in a cachefile\n");
187ed61ec1Yuri Pankov	(void) fprintf(stderr, "        -F attempt automatic rewind within "
188ed61ec1Yuri Pankov	    "safe range of transaction groups\n");
189ed61ec1Yuri Pankov	(void) fprintf(stderr, "        -G dump zfs_dbgmsg buffer before "
190ed61ec1Yuri Pankov	    "exiting\n");
1912e4c998George Wilson	(void) fprintf(stderr, "        -I <number of inflight I/Os> -- "
192df15e41Matthew Ahrens	    "specify the maximum number of "
193df15e41Matthew Ahrens	    "checksumming I/Os [default is 200]\n");
1940e60744Pavel Zakharov	(void) fprintf(stderr, "        -o <variable>=<value> set global "
1950e60744Pavel Zakharov	    "variable to an unsigned 32-bit integer value\n");
196ed61ec1Yuri Pankov	(void) fprintf(stderr, "        -p <path> -- use one or more with "
197ed61ec1Yuri Pankov	    "-e to specify path to vdev dir\n");
198ed61ec1Yuri Pankov	(void) fprintf(stderr, "        -P print numbers in parseable form\n");
19964723e3Yuri Pankov	(void) fprintf(stderr, "        -q don't print label contents\n");
200ed61ec1Yuri Pankov	(void) fprintf(stderr, "        -t <txg> -- highest txg to use when "
201ed61ec1Yuri Pankov	    "searching for uberblocks\n");
20264723e3Yuri Pankov	(void) fprintf(stderr, "        -u uberblock\n");
203ed61ec1Yuri Pankov	(void) fprintf(stderr, "        -U <cachefile_path> -- use alternate "
204ed61ec1Yuri Pankov	    "cachefile\n");
205dfd5965Richard Yao	(void) fprintf(stderr, "        -V do verbatim import\n");
206ed61ec1Yuri Pankov	(void) fprintf(stderr, "        -x <dumpdir> -- "
207ed61ec1Yuri Pankov	    "dump all read blocks into specified directory\n");
208ed61ec1Yuri Pankov	(void) fprintf(stderr, "        -X attempt extreme rewind (does not "
209ed61ec1Yuri Pankov	    "work with dataset)\n\n");
210fa9e406ahrens	(void) fprintf(stderr, "Specify an option more than once (e.g. -bb) "
211fa9e406ahrens	    "to make only that option verbose\n");
212fa9e406ahrens	(void) fprintf(stderr, "Default is to dump everything non-verbosely\n");
213fa9e406ahrens	exit(1);
214fa9e406ahrens}
215fa9e406ahrens
21629bdd2fPavel Zakharovstatic void
21729bdd2fPavel Zakharovdump_debug_buffer()
21829bdd2fPavel Zakharov{
21929bdd2fPavel Zakharov	if (dump_opt['G']) {
22029bdd2fPavel Zakharov		(void) printf("\n");
22129bdd2fPavel Zakharov		zfs_dbgmsg_print("zdb");
22229bdd2fPavel Zakharov	}
22329bdd2fPavel Zakharov}
22429bdd2fPavel Zakharov
225ccba080Rich Morris/*
226ccba080Rich Morris * Called for usage errors that are discovered after a call to spa_open(),
227ccba080Rich Morris * dmu_bonus_hold(), or pool_match().  abort() is called for other errors.
228ccba080Rich Morris */
229ccba080Rich Morris
230fa9e406ahrensstatic void
231fa9e406ahrensfatal(const char *fmt, ...)
232fa9e406ahrens{
233fa9e406ahrens	va_list ap;
234fa9e406ahrens
235fa9e406ahrens	va_start(ap, fmt);
236fa9e406ahrens	(void) fprintf(stderr, "%s: ", cmdname);
237fa9e406ahrens	(void) vfprintf(stderr, fmt, ap);
238fa9e406ahrens	va_end(ap);
239fa9e406ahrens	(void) fprintf(stderr, "\n");
240fa9e406ahrens
24129bdd2fPavel Zakharov	dump_debug_buffer();
24229bdd2fPavel Zakharov
243ccba080Rich Morris	exit(1);
244fa9e406ahrens}
245fa9e406ahrens
246fa9e406ahrens/* ARGSUSED */
247fa9e406ahrensstatic void
248fa9e406ahrensdump_packed_nvlist(objset_t *os, uint64_t object, void *data, size_t size)
249fa9e406ahrens{
250fa9e406ahrens	nvlist_t *nv;
251fa9e406ahrens	size_t nvsize = *(uint64_t *)data;
252fa9e406ahrens	char *packed = umem_alloc(nvsize, UMEM_NOFAIL);
253fa9e406ahrens
2547bfdf01Neil Perrin	VERIFY(0 == dmu_read(os, object, 0, nvsize, packed, DMU_READ_PREFETCH));
255fa9e406ahrens
256fa9e406ahrens	VERIFY(nvlist_unpack(packed, nvsize, &nv, 0) == 0);
257fa9e406ahrens
258fa9e406ahrens	umem_free(packed, nvsize);
259fa9e406ahrens
260fa9e406ahrens	dump_nvlist(nv, 8);
261fa9e406ahrens
262fa9e406ahrens	nvlist_free(nv);
263fa9e406ahrens}
264fa9e406ahrens
2654445fffMatthew Ahrens/* ARGSUSED */
2664445fffMatthew Ahrensstatic void
2674445fffMatthew Ahrensdump_history_offsets(objset_t *os, uint64_t object, void *data, size_t size)
2684445fffMatthew Ahrens{
2694445fffMatthew Ahrens	spa_history_phys_t *shp = data;
2704445fffMatthew Ahrens
2714445fffMatthew Ahrens	if (shp == NULL)
2724445fffMatthew Ahrens		return;
2734445fffMatthew Ahrens
2744445fffMatthew Ahrens	(void) printf("\t\tpool_create_len = %llu\n",
2754445fffMatthew Ahrens	    (u_longlong_t)shp->sh_pool_create_len);
2764445fffMatthew Ahrens	(void) printf("\t\tphys_max_off = %llu\n",
2774445fffMatthew Ahrens	    (u_longlong_t)shp->sh_phys_max_off);
2784445fffMatthew Ahrens	(void) printf("\t\tbof = %llu\n",
2794445fffMatthew Ahrens	    (u_longlong_t)shp->sh_bof);
2804445fffMatthew Ahrens	(void) printf("\t\teof = %llu\n",
2814445fffMatthew Ahrens	    (u_longlong_t)shp->sh_eof);
2824445fffMatthew Ahrens	(void) printf("\t\trecords_lost = %llu\n",
2834445fffMatthew Ahrens	    (u_longlong_t)shp->sh_records_lost);
2844445fffMatthew Ahrens}
2854445fffMatthew Ahrens
2863f9d6adLin Lingstatic void
2870a05512Jason Kingzdb_nicenum(uint64_t num, char *buf, size_t buflen)
2883f9d6adLin Ling{
2893f9d6adLin Ling	if (dump_opt['P'])
2900a05512Jason King		(void) snprintf(buf, buflen, "%llu", (longlong_t)num);
2913f9d6adLin Ling	else
2920a05512Jason King		nicenum(num, buf, sizeof (buf));
2933f9d6adLin Ling}
2943f9d6adLin Ling
2953f7978dAlan Somersstatic const char histo_stars[] = "****************************************";
2963f7978dAlan Somersstatic const uint64_t histo_width = sizeof (histo_stars) - 1;
297fa9e406ahrens
298fa9e406ahrensstatic void
2990713e23George Wilsondump_histogram(const uint64_t *histo, int size, int offset)
300fa9e406ahrens{
301fa9e406ahrens	int i;
302490d05bMatthew Ahrens	int minidx = size - 1;
303fa9e406ahrens	int maxidx = 0;
304fa9e406ahrens	uint64_t max = 0;
305fa9e406ahrens
306490d05bMatthew Ahrens	for (i = 0; i < size; i++) {
307fa9e406ahrens		if (histo[i] > max)
308fa9e406ahrens			max = histo[i];
309fa9e406ahrens		if (histo[i] > 0 && i > maxidx)
310fa9e406ahrens			maxidx = i;
311fa9e406ahrens		if (histo[i] > 0 && i < minidx)
312fa9e406ahrens			minidx = i;
313fa9e406ahrens	}
314fa9e406ahrens
315490d05bMatthew Ahrens	if (max < histo_width)
316490d05bMatthew Ahrens		max = histo_width;
317fa9e406ahrens
318490d05bMatthew Ahrens	for (i = minidx; i <= maxidx; i++) {
319490d05bMatthew Ahrens		(void) printf("\t\t\t%3u: %6llu %s\n",
3200713e23George Wilson		    i + offset, (u_longlong_t)histo[i],
321490d05bMatthew Ahrens		    &histo_stars[(max - histo[i]) * histo_width / max]);
322490d05bMatthew Ahrens	}
323fa9e406ahrens}
324fa9e406ahrens
325fa9e406ahrensstatic void
326fa9e406ahrensdump_zap_stats(objset_t *os, uint64_t object)
327fa9e406ahrens{
328fa9e406ahrens	int error;
329fa9e406ahrens	zap_stats_t zs;
330fa9e406ahrens
331fa9e406ahrens	error = zap_get_stats(os, object, &zs);
332fa9e406ahrens	if (error)
333fa9e406ahrens		return;
334fa9e406ahrens
335fa9e406ahrens	if (zs.zs_ptrtbl_len == 0) {
336fa9e406ahrens		ASSERT(zs.zs_num_blocks == 1);
337fa9e406ahrens		(void) printf("\tmicrozap: %llu bytes, %llu entries\n",
338fa9e406ahrens		    (u_longlong_t)zs.zs_blocksize,
339fa9e406ahrens		    (u_longlong_t)zs.zs_num_entries);
340fa9e406ahrens		return;
341fa9e406ahrens	}
342fa9e406ahrens
343fa9e406ahrens	(void) printf("\tFat ZAP stats:\n");
3448248818nd
3458248818nd	(void) printf("\t\tPointer table:\n");
3468248818nd	(void) printf("\t\t\t%llu elements\n",
347fa9e406ahrens	    (u_longlong_t)zs.zs_ptrtbl_len);
3488248818nd	(void) printf("\t\t\tzt_blk: %llu\n",
3498248818nd	    (u_longlong_t)zs.zs_ptrtbl_zt_blk);
3508248818nd	(void) printf("\t\t\tzt_numblks: %llu\n",
3518248818nd	    (u_longlong_t)zs.zs_ptrtbl_zt_numblks);
3528248818nd	(void) printf("\t\t\tzt_shift: %llu\n",
3538248818nd	    (u_longlong_t)zs.zs_ptrtbl_zt_shift);
3548248818nd	(void) printf("\t\t\tzt_blks_copied: %llu\n",
3558248818nd	    (u_longlong_t)zs.zs_ptrtbl_blks_copied);
3568248818nd	(void) printf("\t\t\tzt_nextblk: %llu\n",
3578248818nd	    (u_longlong_t)zs.zs_ptrtbl_nextblk);
3588248818nd
359fa9e406ahrens	(void) printf("\t\tZAP entries: %llu\n",
360fa9e406ahrens	    (u_longlong_t)zs.zs_num_entries);
361fa9e406ahrens	(void) printf("\t\tLeaf blocks: %llu\n",
362fa9e406ahrens	    (u_longlong_t)zs.zs_num_leafs);
363fa9e406ahrens	(void) printf("\t\tTotal blocks: %llu\n",
364fa9e406ahrens	    (u_longlong_t)zs.zs_num_blocks);
3658248818nd	(void) printf("\t\tzap_block_type: 0x%llx\n",
3668248818nd	    (u_longlong_t)zs.zs_block_type);
3678248818nd	(void) printf("\t\tzap_magic: 0x%llx\n",
3688248818nd	    (u_longlong_t)zs.zs_magic);
3698248818nd	(void) printf("\t\tzap_salt: 0x%llx\n",
3708248818nd	    (u_longlong_t)zs.zs_salt);
371fa9e406ahrens
372fa9e406ahrens	(void) printf("\t\tLeafs with 2^n pointers:\n");
3730713e23George Wilson	dump_histogram(zs.zs_leafs_with_2n_pointers, ZAP_HISTOGRAM_SIZE, 0);
374fa9e406ahrens
375fa9e406ahrens	(void) printf("\t\tBlocks with n*5 entries:\n");
3760713e23George Wilson	dump_histogram(zs.zs_blocks_with_n5_entries, ZAP_HISTOGRAM_SIZE, 0);
377fa9e406ahrens
378fa9e406ahrens	(void) printf("\t\tBlocks n/10 full:\n");
3790713e23George Wilson	dump_histogram(zs.zs_blocks_n_tenths_full, ZAP_HISTOGRAM_SIZE, 0);
380fa9e406ahrens
381fa9e406ahrens	(void) printf("\t\tEntries with n chunks:\n");
3820713e23George Wilson	dump_histogram(zs.zs_entries_using_n_chunks, ZAP_HISTOGRAM_SIZE, 0);
383fa9e406ahrens
384fa9e406ahrens	(void) printf("\t\tBuckets with n entries:\n");
3850713e23George Wilson	dump_histogram(zs.zs_buckets_with_n_entries, ZAP_HISTOGRAM_SIZE, 0);
386fa9e406ahrens}
387fa9e406ahrens
388fa9e406ahrens/*ARGSUSED*/
389fa9e406ahrensstatic void
390fa9e406ahrensdump_none(objset_t *os, uint64_t object, void *data, size_t size)
391fa9e406ahrens{
392fa9e406ahrens}
393fa9e406ahrens
394fa9e406ahrens/*ARGSUSED*/
3956de8f41Victor Latushkinstatic void
3966de8f41Victor Latushkindump_unknown(objset_t *os, uint64_t object, void *data, size_t size)
3976de8f41Victor Latushkin{
3986de8f41Victor Latushkin	(void) printf("\tUNKNOWN OBJECT TYPE\n");
3996de8f41Victor Latushkin}
4006de8f41Victor Latushkin
4016de8f41Victor Latushkin/*ARGSUSED*/
4023f7978dAlan Somersstatic void
403fa9e406ahrensdump_uint8(objset_t *os, uint64_t object, void *data, size_t size)
404fa9e406ahrens{
405fa9e406ahrens}
406fa9e406ahrens
407fa9e406ahrens/*ARGSUSED*/
408fa9e406ahrensstatic void
409fa9e406ahrensdump_uint64(objset_t *os, uint64_t object, void *data, size_t size)
410fa9e406ahrens{
411fa9e406ahrens}
412fa9e406ahrens
413fa9e406ahrens/*ARGSUSED*/
414fa9e406ahrensstatic void
415fa9e406ahrensdump_zap(objset_t *os, uint64_t object, void *data, size_t size)
416fa9e406ahrens{
417fa9e406ahrens	zap_cursor_t zc;
418fa9e406ahrens	zap_attribute_t attr;
419fa9e406ahrens	void *prop;
4203f7978dAlan Somers	unsigned i;
421fa9e406ahrens
422fa9e406ahrens	dump_zap_stats(os, object);
423fa9e406ahrens	(void) printf("\n");
424fa9e406ahrens
425fa9e406ahrens	for (zap_cursor_init(&zc, os, object);
426fa9e406ahrens	    zap_cursor_retrieve(&zc, &attr) == 0;
427fa9e406ahrens	    zap_cursor_advance(&zc)) {
428fa9e406ahrens		(void) printf("\t\t%s = ", attr.za_name);
429fa9e406ahrens		if (attr.za_num_integers == 0) {
430fa9e406ahrens			(void) printf("\n");
431fa9e406ahrens			continue;
432fa9e406ahrens		}
433fa9e406ahrens		prop = umem_zalloc(attr.za_num_integers *
434fa9e406ahrens		    attr.za_integer_length, UMEM_NOFAIL);
435fa9e406ahrens		(void) zap_lookup(os, object, attr.za_name,
436fa9e406ahrens		    attr.za_integer_length, attr.za_num_integers, prop);
437fa9e406ahrens		if (attr.za_integer_length == 1) {
4382d37a1aToomas Soome			if (strcmp(attr.za_name,
4392d37a1aToomas Soome			    DSL_CRYPTO_KEY_MASTER_KEY) == 0 ||
4402d37a1aToomas Soome			    strcmp(attr.za_name,
4412d37a1aToomas Soome			    DSL_CRYPTO_KEY_HMAC_KEY) == 0 ||
4422d37a1aToomas Soome			    strcmp(attr.za_name, DSL_CRYPTO_KEY_IV) == 0 ||
44332114f8Toomas Soome			    strcmp(attr.za_name, DSL_CRYPTO_KEY_MAC) == 0 ||
44432114f8Toomas Soome			    strcmp(attr.za_name, DMU_POOL_CHECKSUM_SALT) == 0) {
4452d37a1aToomas Soome				uint8_t *u8 = prop;
4462d37a1aToomas Soome
4472d37a1aToomas Soome				for (i = 0; i < attr.za_num_integers; i++) {
4482d37a1aToomas Soome					(void) printf("%02x", u8[i]);
4492d37a1aToomas Soome				}
4502d37a1aToomas Soome			} else {
4512d37a1aToomas Soome				(void) printf("%s", (char *)prop);
4522d37a1aToomas Soome			}
453fa9e406ahrens		} else {
454fa9e406ahrens			for (i = 0; i < attr.za_num_integers; i++) {
455fa9e406ahrens				switch (attr.za_integer_length) {
456fa9e406ahrens				case 2:
457fa9e406ahrens					(void) printf("%u ",
458fa9e406ahrens					    ((uint16_t *)prop)[i]);
459fa9e406ahrens					break;
460fa9e406ahrens				case 4:
461fa9e406ahrens					(void) printf("%u ",
462fa9e406ahrens					    ((uint32_t *)prop)[i]);
463fa9e406ahrens					break;
464fa9e406ahrens				case 8:
465fa9e406ahrens					(void) printf("%lld ",
466fa9e406ahrens					    (u_longlong_t)((int64_t *)prop)[i]);
467fa9e406ahrens					break;
468fa9e406ahrens				}
469fa9e406ahrens			}
470fa9e406ahrens		}
471fa9e406ahrens		(void) printf("\n");
472fa9e406ahrens		umem_free(prop, attr.za_num_integers * attr.za_integer_length);
473fa9e406ahrens	}
47487e5029ahrens	zap_cursor_fini(&zc);
475fa9e406ahrens}
476fa9e406ahrens
477732885fMatthew Ahrensstatic void
478732885fMatthew Ahrensdump_bpobj(objset_t *os, uint64_t object, void *data, size_t size)
479732885fMatthew Ahrens{
480732885fMatthew Ahrens	bpobj_phys_t *bpop = data;
481732885fMatthew Ahrens	char bytes[32], comp[32], uncomp[32];
482732885fMatthew Ahrens
4830a05512Jason King	/* make sure the output won't get truncated */
4840a05512Jason King	CTASSERT(sizeof (bytes) >= NN_NUMBUF_SZ);
4850a05512Jason King	CTASSERT(sizeof (comp) >= NN_NUMBUF_SZ);
4860a05512Jason King	CTASSERT(sizeof (uncomp) >= NN_NUMBUF_SZ);
4870a05512Jason King
488732885fMatthew Ahrens	if (bpop == NULL)
489732885fMatthew Ahrens		return;
490732885fMatthew Ahrens
4910a05512Jason King	zdb_nicenum(bpop->bpo_bytes, bytes, sizeof (bytes));
4920a05512Jason King	zdb_nicenum(bpop->bpo_comp, comp, sizeof (comp));
4930a05512Jason King	zdb_nicenum(bpop->bpo_uncomp, uncomp, sizeof (uncomp));
494732885fMatthew Ahrens
495732885fMatthew Ahrens	(void) printf("\t\tnum_blkptrs = %llu\n",
496732885fMatthew Ahrens	    (u_longlong_t)bpop->bpo_num_blkptrs);
497732885fMatthew Ahrens	(void) printf("\t\tbytes = %s\n", bytes);
498732885fMatthew Ahrens	if (size >= BPOBJ_SIZE_V1) {
499732885fMatthew Ahrens		(void) printf("\t\tcomp = %s\n", comp);
500732885fMatthew Ahrens		(void) printf("\t\tuncomp = %s\n", uncomp);
501732885fMatthew Ahrens	}
502732885fMatthew Ahrens	if (size >= sizeof (*bpop)) {
503732885fMatthew Ahrens		(void) printf("\t\tsubobjs = %llu\n",
504732885fMatthew Ahrens		    (u_longlong_t)bpop->bpo_subobjs);
505732885fMatthew Ahrens		(void) printf("\t\tnum_subobjs = %llu\n",
506732885fMatthew Ahrens		    (u_longlong_t)bpop->bpo_num_subobjs);
507732885fMatthew Ahrens	}
508732885fMatthew Ahrens
509732885fMatthew Ahrens	if (dump_opt['d'] < 5)
510732885fMatthew Ahrens		return;
511732885fMatthew Ahrens
512732885fMatthew Ahrens	for (uint64_t i = 0; i < bpop->bpo_num_blkptrs; i++) {
513732885fMatthew Ahrens		char blkbuf[BP_SPRINTF_LEN];
514732885fMatthew Ahrens		blkptr_t bp;
515732885fMatthew Ahrens
516732885fMatthew Ahrens		int err = dmu_read(os, object,
517732885fMatthew Ahrens		    i * sizeof (bp), sizeof (bp), &bp, 0);
518732885fMatthew Ahrens		if (err != 0) {
519732885fMatthew Ahrens			(void) printf("got error %u from dmu_read\n", err);
520732885fMatthew Ahrens			break;
521732885fMatthew Ahrens		}
522732885fMatthew Ahrens		snprintf_blkptr_compact(blkbuf, sizeof (blkbuf), &bp);
523732885fMatthew Ahrens		(void) printf("\t%s\n", blkbuf);
524732885fMatthew Ahrens	}
525732885fMatthew Ahrens}
526732885fMatthew Ahrens
527732885fMatthew Ahrens/* ARGSUSED */
528732885fMatthew Ahrensstatic void
529732885fMatthew Ahrensdump_bpobj_subobjs(objset_t *os, uint64_t object, void *data, size_t size)
530732885fMatthew Ahrens{
531732885fMatthew Ahrens	dmu_object_info_t doi;
532732885fMatthew Ahrens
533732885fMatthew Ahrens	VERIFY0(dmu_object_info(os, object, &doi));
534732885fMatthew Ahrens	uint64_t *subobjs = kmem_alloc(doi.doi_max_offset, KM_SLEEP);
535732885fMatthew Ahrens
536732885fMatthew Ahrens	int err = dmu_read(os, object, 0, doi.doi_max_offset, subobjs, 0);
537732885fMatthew Ahrens	if (err != 0) {
538732885fMatthew Ahrens		(void) printf("got error %u from dmu_read\n", err);
539732885fMatthew Ahrens		kmem_free(subobjs, doi.doi_max_offset);
540732885fMatthew Ahrens		return;
541732885fMatthew Ahrens	}
542732885fMatthew Ahrens
543732885fMatthew Ahrens	int64_t last_nonzero = -1;
544732885fMatthew Ahrens	for (uint64_t i = 0; i < doi.doi_max_offset / 8; i++) {
545732885fMatthew Ahrens		if (subobjs[i] != 0)
546732885fMatthew Ahrens			last_nonzero = i;
547732885fMatthew Ahrens	}
548732885fMatthew Ahrens
549732885fMatthew Ahrens	for (int64_t i = 0; i <= last_nonzero; i++) {
550732885fMatthew Ahrens		(void) printf("\t%llu\n", (longlong_t)subobjs[i]);
551732885fMatthew Ahrens	}
552732885fMatthew Ahrens	kmem_free(subobjs, doi.doi_max_offset);
553732885fMatthew Ahrens}
554732885fMatthew Ahrens
555e743726ahrens/*ARGSUSED*/
556e743726ahrensstatic void
557486ae71Matthew Ahrensdump_ddt_zap(objset_t *os, uint64_t object, void *data, size_t size)
558486ae71Matthew Ahrens{
559486ae71Matthew Ahrens	dump_zap_stats(os, object);
560486ae71Matthew Ahrens	/* contents are printed elsewhere, properly decoded */
561486ae71Matthew Ahrens}
562486ae71Matthew Ahrens
563486ae71Matthew Ahrens/*ARGSUSED*/
564486ae71Matthew Ahrensstatic void
5650a586ceMark Shellenbaumdump_sa_attrs(objset_t *os, uint64_t object, void *data, size_t size)
5660a586ceMark Shellenbaum{
5670a586ceMark Shellenbaum	zap_cursor_t zc;
5680a586ceMark Shellenbaum	zap_attribute_t attr;
5690a586ceMark Shellenbaum
5700a586ceMark Shellenbaum	dump_zap_stats(os, object);
5710a586ceMark Shellenbaum	(void) printf("\n");
5720a586ceMark Shellenbaum
5730a586ceMark Shellenbaum	for (zap_cursor_init(&zc, os, object);
5740a586ceMark Shellenbaum	    zap_cursor_retrieve(&zc, &attr) == 0;
5750a586ceMark Shellenbaum	    zap_cursor_advance(&zc)) {
5760a586ceMark Shellenbaum		(void) printf("\t\t%s = ", attr.za_name);
5770a586ceMark Shellenbaum		if (attr.za_num_integers == 0) {
5780a586ceMark Shellenbaum			(void) printf("\n");
5790a586ceMark Shellenbaum			continue;
5800a586ceMark Shellenbaum		}
5810a586ceMark Shellenbaum		(void) printf(" %llx : [%d:%d:%d]\n",
5820a586ceMark Shellenbaum		    (u_longlong_t)attr.za_first_integer,
5830a586ceMark Shellenbaum		    (int)ATTR_LENGTH(attr.za_first_integer),
5840a586ceMark Shellenbaum		    (int)ATTR_BSWAP(attr.za_first_integer),
5850a586ceMark Shellenbaum		    (int)ATTR_NUM(attr.za_first_integer));
5860a586ceMark Shellenbaum	}
5870a586ceMark Shellenbaum	zap_cursor_fini(&zc);
5880a586ceMark Shellenbaum}
5890a586ceMark Shellenbaum
5900a586ceMark Shellenbaum/*ARGSUSED*/
5910a586ceMark Shellenbaumstatic void
5920a586ceMark Shellenbaumdump_sa_layouts(objset_t *os, uint64_t object, void *data, size_t size)
5930a586ceMark Shellenbaum{
5940a586ceMark Shellenbaum	zap_cursor_t zc;
5950a586ceMark Shellenbaum	zap_attribute_t attr;
5960a586ceMark Shellenbaum	uint16_t *layout_attrs;
5973f7978dAlan Somers	unsigned i;
5980a586ceMark Shellenbaum
5990a586ceMark Shellenbaum	dump_zap_stats(os, object);
6000a586ceMark Shellenbaum	(void) printf("\n");
6010a586ceMark Shellenbaum
6020a586ceMark Shellenbaum	for (zap_cursor_init(&zc, os, object);
6030a586ceMark Shellenbaum	    zap_cursor_retrieve(&zc, &attr) == 0;
6040a586ceMark Shellenbaum	    zap_cursor_advance(&zc)) {
6050a586ceMark Shellenbaum		(void) printf("\t\t%s = [", attr.za_name);
6060a586ceMark Shellenbaum		if (attr.za_num_integers == 0) {
6070a586ceMark Shellenbaum			(void) printf("\n");
6080a586ceMark Shellenbaum			continue;
6090a586ceMark Shellenbaum		}
6100a586ceMark Shellenbaum
6110a586ceMark Shellenbaum		VERIFY(attr.za_integer_length == 2);
6120a586ceMark Shellenbaum		layout_attrs = umem_zalloc(attr.za_num_integers *
6130a586ceMark Shellenbaum		    attr.za_integer_length, UMEM_NOFAIL);
6140a586ceMark Shellenbaum
6150a586ceMark Shellenbaum		VERIFY(zap_lookup(os, object, attr.za_name,
6160a586ceMark Shellenbaum		    attr.za_integer_length,
6170a586ceMark Shellenbaum		    attr.za_num_integers, layout_attrs) == 0);
6180a586ceMark Shellenbaum
6190a586ceMark Shellenbaum		for (i = 0; i != attr.za_num_integers; i++)
6200a586ceMark Shellenbaum			(void) printf(" %d ", (int)layout_attrs[i]);
6210a586ceMark Shellenbaum		(void) printf("]\n");
6220a586ceMark Shellenbaum		umem_free(layout_attrs,
6230a586ceMark Shellenbaum		    attr.za_num_integers * attr.za_integer_length);
6240a586ceMark Shellenbaum	}
6250a586ceMark Shellenbaum	zap_cursor_fini(&zc);
6260a586ceMark Shellenbaum}
6270a586ceMark Shellenbaum
6280a586ceMark Shellenbaum/*ARGSUSED*/
6290a586ceMark Shellenbaumstatic void
630e743726ahrensdump_zpldir(objset_t *os, uint64_t object, void *data, size_t size)
631e743726ahrens{
632e743726ahrens	zap_cursor_t zc;
633e743726ahrens	zap_attribute_t attr;
634e743726ahrens	const char *typenames[] = {
635e743726ahrens		/* 0 */ "not specified",
636e743726ahrens		/* 1 */ "FIFO",
637e743726ahrens		/* 2 */ "Character Device",
638e743726ahrens		/* 3 */ "3 (invalid)",
639e743726ahrens		/* 4 */ "Directory",
640e743726ahrens		/* 5 */ "5 (invalid)",
641e743726ahrens		/* 6 */ "Block Device",
642e743726ahrens		/* 7 */ "7 (invalid)",
643e743726ahrens		/* 8 */ "Regular File",
644e743726ahrens		/* 9 */ "9 (invalid)",
645e743726ahrens		/* 10 */ "Symbolic Link",
646e743726ahrens		/* 11 */ "11 (invalid)",
647e743726ahrens		/* 12 */ "Socket",
648e743726ahrens		/* 13 */ "Door",
649e743726ahrens		/* 14 */ "Event Port",
650e743726ahrens		/* 15 */ "15 (invalid)",
651e743726ahrens	};
652e743726ahrens
653e743726ahrens	dump_zap_stats(os, object);
654e743726ahrens	(void) printf("\n");
655e743726ahrens
656e743726ahrens	for (zap_cursor_init(&zc, os, object);
657e743726ahrens	    zap_cursor_retrieve(&zc, &attr) == 0;
658e743726ahrens	    zap_cursor_advance(&zc)) {
659e743726ahrens		(void) printf("\t\t%s = %lld (type: %s)\n",
660e743726ahrens		    attr.za_name, ZFS_DIRENT_OBJ(attr.za_first_integer),
661e743726ahrens		    typenames[ZFS_DIRENT_TYPE(attr.za_first_integer)]);
662e743726ahrens	}
663e743726ahrens	zap_cursor_fini(&zc);
664e743726ahrens}
665e743726ahrens
6663f7978dAlan Somersstatic int
6670713e23George Wilsonget_dtl_refcount(vdev_t *vd)
6680713e23George Wilson{
6690713e23George Wilson	int refcount = 0;
6700713e23George Wilson
6710713e23George Wilson	if (vd->vdev_ops->vdev_op_leaf) {
6720713e23George Wilson		space_map_t *sm = vd->vdev_dtl_sm;
6730713e23George Wilson
6740713e23George Wilson		if (sm != NULL &&
6750713e23George Wilson		    sm->sm_dbuf->db_size == sizeof (space_map_phys_t))
6760713e23George Wilson			return (1);
6770713e23George Wilson		return (0);
6780713e23George Wilson	}
6790713e23George Wilson
6803f7978dAlan Somers	for (unsigned c = 0; c < vd->vdev_children; c++)
6810713e23George Wilson		refcount += get_dtl_refcount(vd->vdev_child[c]);
6820713e23George Wilson	return (refcount);
6830713e23George Wilson}
6840713e23George Wilson
6853f7978dAlan Somersstatic int
6860713e23George Wilsonget_metaslab_refcount(vdev_t *vd)
6870713e23George Wilson{
6880713e23George Wilson	int refcount = 0;
6890713e23George Wilson
6905cabbc6Prashanth Sreenivasa	if (vd->vdev_top == vd) {
6915cabbc6Prashanth Sreenivasa		for (uint64_t m = 0; m < vd->vdev_ms_count; m++) {
6920713e23George Wilson			space_map_t *sm = vd->vdev_ms[m]->ms_sm;
6930713e23George Wilson
6940713e23George Wilson			if (sm != NULL &&
6950713e23George Wilson			    sm->sm_dbuf->db_size == sizeof (space_map_phys_t))
6960713e23George Wilson				refcount++;
6970713e23George Wilson		}
6980713e23George Wilson	}
6993f7978dAlan Somers	for (unsigned c = 0; c < vd->vdev_children; c++)
7000713e23George Wilson		refcount += get_metaslab_refcount(vd->vdev_child[c]);
7010713e23George Wilson
7020713e23George Wilson	return (refcount);
7030713e23George Wilson}
7040713e23George Wilson
7050713e23George Wilsonstatic int
7065cabbc6Prashanth Sreenivasaget_obsolete_refcount(vdev_t *vd)
7075cabbc6Prashanth Sreenivasa{
7085cabbc6Prashanth Sreenivasa	int refcount = 0;
7095cabbc6Prashanth Sreenivasa
7105cabbc6Prashanth Sreenivasa	uint64_t obsolete_sm_obj = vdev_obsolete_sm_object(vd);
7115cabbc6Prashanth Sreenivasa	if (vd->vdev_top == vd && obsolete_sm_obj != 0) {
7125cabbc6Prashanth Sreenivasa		dmu_object_info_t doi;
7135cabbc6Prashanth Sreenivasa		VERIFY0(dmu_object_info(vd->vdev_spa->spa_meta_objset,
7145cabbc6Prashanth Sreenivasa		    obsolete_sm_obj, &doi));
7155cabbc6Prashanth Sreenivasa		if (doi.doi_bonus_size == sizeof (space_map_phys_t)) {
7165cabbc6Prashanth Sreenivasa			refcount++;
7175cabbc6Prashanth Sreenivasa		}
7185cabbc6Prashanth Sreenivasa	} else {
7195cabbc6Prashanth Sreenivasa		ASSERT3P(vd->vdev_obsolete_sm, ==, NULL);
7205cabbc6Prashanth Sreenivasa		ASSERT3U(obsolete_sm_obj, ==, 0);
7215cabbc6Prashanth Sreenivasa	}
7225cabbc6Prashanth Sreenivasa	for (unsigned c = 0; c < vd->vdev_children; c++) {
7235cabbc6Prashanth Sreenivasa		refcount += get_obsolete_refcount(vd->vdev_child[c]);
7245cabbc6Prashanth Sreenivasa	}
7255cabbc6Prashanth Sreenivasa
7265cabbc6Prashanth Sreenivasa	return (refcount);
7275cabbc6Prashanth Sreenivasa}
7285cabbc6Prashanth Sreenivasa
7295cabbc6Prashanth Sreenivasastatic int
7305cabbc6Prashanth Sreenivasaget_prev_obsolete_spacemap_refcount(spa_t *spa)
7315cabbc6Prashanth Sreenivasa{
7325cabbc6Prashanth Sreenivasa	uint64_t prev_obj =
7335cabbc6Prashanth Sreenivasa	    spa->spa_condensing_indirect_phys.scip_prev_obsolete_sm_object;
7345cabbc6Prashanth Sreenivasa	if (prev_obj != 0) {
7355cabbc6Prashanth Sreenivasa		dmu_object_info_t doi;
7365cabbc6Prashanth Sreenivasa		VERIFY0(dmu_object_info(spa->spa_meta_objset, prev_obj, &doi));
7375cabbc6Prashanth Sreenivasa		if (doi.doi_bonus_size == sizeof (space_map_phys_t)) {
7385cabbc6Prashanth Sreenivasa			return (1);
7395cabbc6Prashanth Sreenivasa		}
7405cabbc6Prashanth Sreenivasa	}
7415cabbc6Prashanth Sreenivasa	return (0);
7425cabbc6Prashanth Sreenivasa}
7435cabbc6Prashanth Sreenivasa
7445cabbc6Prashanth Sreenivasastatic int
7458671400Serapheim Dimitropoulosget_checkpoint_refcount(vdev_t *vd)
7468671400Serapheim Dimitropoulos{
7478671400Serapheim Dimitropoulos	int refcount = 0;
7488671400Serapheim Dimitropoulos
7498671400Serapheim Dimitropoulos	if (vd->vdev_top == vd && vd->vdev_top_zap != 0 &&
7508671400Serapheim Dimitropoulos	    zap_contains(spa_meta_objset(vd->vdev_spa),
7518671400Serapheim Dimitropoulos	    vd->vdev_top_zap, VDEV_TOP_ZAP_POOL_CHECKPOINT_SM) == 0)
7528671400Serapheim Dimitropoulos		refcount++;
7538671400Serapheim Dimitropoulos
7548671400Serapheim Dimitropoulos	for (uint64_t c = 0; c < vd->vdev_children; c++)
7558671400Serapheim Dimitropoulos		refcount += get_checkpoint_refcount(vd->vdev_child[c]);
7568671400Serapheim Dimitropoulos
7578671400Serapheim Dimitropoulos	return (refcount);
7588671400Serapheim Dimitropoulos}
7598671400Serapheim Dimitropoulos
7608671400Serapheim Dimitropoulosstatic int
761814dcd4Serapheim Dimitropoulosget_log_spacemap_refcount(spa_t *spa)
762814dcd4Serapheim Dimitropoulos{
763814dcd4Serapheim Dimitropoulos	return (avl_numnodes(&spa->spa_sm_logs_by_txg));
764814dcd4Serapheim Dimitropoulos}
765814dcd4Serapheim Dimitropoulos
766814dcd4Serapheim Dimitropoulosstatic int
7670713e23George Wilsonverify_spacemap_refcounts(spa_t *spa)
7680713e23George Wilson{
7692acef22Matthew Ahrens	uint64_t expected_refcount = 0;
7702acef22Matthew Ahrens	uint64_t actual_refcount;
7710713e23George Wilson
7722acef22Matthew Ahrens	(void) feature_get_refcount(spa,
7732acef22Matthew Ahrens	    &spa_feature_table[SPA_FEATURE_SPACEMAP_HISTOGRAM],
7742acef22Matthew Ahrens	    &expected_refcount);
7750713e23George Wilson	actual_refcount = get_dtl_refcount(spa->spa_root_vdev);
7760713e23George Wilson	actual_refcount += get_metaslab_refcount(spa->spa_root_vdev);
7775cabbc6Prashanth Sreenivasa	actual_refcount += get_obsolete_refcount(spa->spa_root_vdev);
7785cabbc6Prashanth Sreenivasa	actual_refcount += get_prev_obsolete_spacemap_refcount(spa);
7798671400Serapheim Dimitropoulos	actual_refcount += get_checkpoint_refcount(spa->spa_root_vdev);
780814dcd4Serapheim Dimitropoulos	actual_refcount += get_log_spacemap_refcount(spa);
7810713e23George Wilson
7820713e23George Wilson	if (expected_refcount != actual_refcount) {
7832acef22Matthew Ahrens		(void) printf("space map refcount mismatch: expected %lld != "
7842acef22Matthew Ahrens		    "actual %lld\n",
7852acef22Matthew Ahrens		    (longlong_t)expected_refcount,
7862acef22Matthew Ahrens		    (longlong_t)actual_refcount);
7870713e23George Wilson		return (2);
7880713e23George Wilson	}
7890713e23George Wilson	return (0);
7900713e23George Wilson}
7910713e23George Wilson
792fa9e406ahrensstatic void
7930713e23George Wilsondump_spacemap(objset_t *os, space_map_t *sm)
794fa9e406ahrens{
7955cabbc6Prashanth Sreenivasa	char *ddata[] = { "ALLOC", "FREE", "CONDENSE", "INVALID",
7965cabbc6Prashanth Sreenivasa	    "INVALID", "INVALID", "INVALID", "INVALID" };
797fa9e406ahrens
7980713e23George Wilson	if (sm == NULL)
799fa9e406ahrens		return;
800fa9e406ahrens
8015cabbc6Prashanth Sreenivasa	(void) printf("space map object %llu:\n",
802555d674Serapheim Dimitropoulos	    (longlong_t)sm->sm_object);
803555d674Serapheim Dimitropoulos	(void) printf("  smp_length = 0x%llx\n",
804555d674Serapheim Dimitropoulos	    (longlong_t)sm->sm_phys->smp_length);
8055cabbc6Prashanth Sreenivasa	(void) printf("  smp_alloc = 0x%llx\n",
8065cabbc6Prashanth Sreenivasa	    (longlong_t)sm->sm_phys->smp_alloc);
8075cabbc6Prashanth Sreenivasa
808555d674Serapheim Dimitropoulos	if (dump_opt['d'] < 6 && dump_opt['m'] < 4)
809555d674Serapheim Dimitropoulos		return;
810555d674Serapheim Dimitropoulos
811fa9e406ahrens	/*
812fa9e406ahrens	 * Print out the freelist entries in both encoded and decoded form.
813fa9e406ahrens	 */
81417f1128Serapheim Dimitropoulos	uint8_t mapshift = sm->sm_shift;
81517f1128Serapheim Dimitropoulos	int64_t alloc = 0;
816555d674Serapheim Dimitropoulos	uint64_t word, entry_id = 0;
81717f1128Serapheim Dimitropoulos	for (uint64_t offset = 0; offset < space_map_length(sm);
81817f1128Serapheim Dimitropoulos	    offset += sizeof (word)) {
8190713e23George Wilson
8200713e23George Wilson		VERIFY0(dmu_read(os, space_map_object(sm), offset,
82117f1128Serapheim Dimitropoulos		    sizeof (word), &word, DMU_READ_PREFETCH));
8220713e23George Wilson
82317f1128Serapheim Dimitropoulos		if (sm_entry_is_debug(word)) {
824555d674Serapheim Dimitropoulos			(void) printf("\t    [%6llu] %s: txg %llu pass %llu\n",
825555d674Serapheim Dimitropoulos			    (u_longlong_t)entry_id,
82617f1128Serapheim Dimitropoulos			    ddata[SM_DEBUG_ACTION_DECODE(word)],
82717f1128Serapheim Dimitropoulos			    (u_longlong_t)SM_DEBUG_TXG_DECODE(word),
82817f1128Serapheim Dimitropoulos			    (u_longlong_t)SM_DEBUG_SYNCPASS_DECODE(word));
829555d674Serapheim Dimitropoulos			entry_id++;
83017f1128Serapheim Dimitropoulos			continue;
83117f1128Serapheim Dimitropoulos		}
83217f1128Serapheim Dimitropoulos
83317f1128Serapheim Dimitropoulos		uint8_t words;
83417f1128Serapheim Dimitropoulos		char entry_type;
83517f1128Serapheim Dimitropoulos		uint64_t entry_off, entry_run, entry_vdev = SM_NO_VDEVID;
83617f1128Serapheim Dimitropoulos
83717f1128Serapheim Dimitropoulos		if (sm_entry_is_single_word(word)) {
83817f1128Serapheim Dimitropoulos			entry_type = (SM_TYPE_DECODE(word) == SM_ALLOC) ?
83917f1128Serapheim Dimitropoulos			    'A' : 'F';
84017f1128Serapheim Dimitropoulos			entry_off = (SM_OFFSET_DECODE(word) << mapshift) +
84117f1128Serapheim Dimitropoulos			    sm->sm_start;
84217f1128Serapheim Dimitropoulos			entry_run = SM_RUN_DECODE(word) << mapshift;
84317f1128Serapheim Dimitropoulos			words = 1;
844fa9e406ahrens		} else {
84517f1128Serapheim Dimitropoulos			/* it is a two-word entry so we read another word */
84617f1128Serapheim Dimitropoulos			ASSERT(sm_entry_is_double_word(word));
84717f1128Serapheim Dimitropoulos
84817f1128Serapheim Dimitropoulos			uint64_t extra_word;
84917f1128Serapheim Dimitropoulos			offset += sizeof (extra_word);
85017f1128Serapheim Dimitropoulos			VERIFY0(dmu_read(os, space_map_object(sm), offset,
85117f1128Serapheim Dimitropoulos			    sizeof (extra_word), &extra_word,
85217f1128Serapheim Dimitropoulos			    DMU_READ_PREFETCH));
85317f1128Serapheim Dimitropoulos
85417f1128Serapheim Dimitropoulos			ASSERT3U(offset, <=, space_map_length(sm));
85517f1128Serapheim Dimitropoulos
85617f1128Serapheim Dimitropoulos			entry_run = SM2_RUN_DECODE(word) << mapshift;
85717f1128Serapheim Dimitropoulos			entry_vdev = SM2_VDEV_DECODE(word);
85817f1128Serapheim Dimitropoulos			entry_type = (SM2_TYPE_DECODE(extra_word) == SM_ALLOC) ?
85917f1128Serapheim Dimitropoulos			    'A' : 'F';
86017f1128Serapheim Dimitropoulos			entry_off = (SM2_OFFSET_DECODE(extra_word) <<
86117f1128Serapheim Dimitropoulos			    mapshift) + sm->sm_start;
86217f1128Serapheim Dimitropoulos			words = 2;
863fa9e406ahrens		}
86417f1128Serapheim Dimitropoulos
865