zdb.c revision 8f2529de2d60d4fff8eb4d86f71b51584342934e
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 */
21fa9e406ahrens/*
228f2529dMark Shellenbaum * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
23fa9e406ahrens */
24fa9e406ahrens
25fa9e406ahrens#include <stdio.h>
26004388ecasper#include <stdio_ext.h>
27fa9e406ahrens#include <stdlib.h>
2844cd46cbillm#include <ctype.h>
29fa9e406ahrens#include <sys/zfs_context.h>
30fa9e406ahrens#include <sys/spa.h>
31fa9e406ahrens#include <sys/spa_impl.h>
32fa9e406ahrens#include <sys/dmu.h>
33fa9e406ahrens#include <sys/zap.h>
34fa9e406ahrens#include <sys/fs/zfs.h>
35fa9e406ahrens#include <sys/zfs_znode.h>
360a586ceMark Shellenbaum#include <sys/zfs_sa.h>
370a586ceMark Shellenbaum#include <sys/sa.h>
380a586ceMark Shellenbaum#include <sys/sa_impl.h>
39fa9e406ahrens#include <sys/vdev.h>
40fa9e406ahrens#include <sys/vdev_impl.h>
41fa9e406ahrens#include <sys/metaslab_impl.h>
42fa9e406ahrens#include <sys/dmu_objset.h>
43fa9e406ahrens#include <sys/dsl_dir.h>
44fa9e406ahrens#include <sys/dsl_dataset.h>
45fa9e406ahrens#include <sys/dsl_pool.h>
46fa9e406ahrens#include <sys/dbuf.h>
47fa9e406ahrens#include <sys/zil.h>
48fa9e406ahrens#include <sys/zil_impl.h>
49fa9e406ahrens#include <sys/stat.h>
50fa9e406ahrens#include <sys/resource.h>
51fa9e406ahrens#include <sys/dmu_traverse.h>
52fa9e406ahrens#include <sys/zio_checksum.h>
53fa9e406ahrens#include <sys/zio_compress.h>
54e0d35c4marks#include <sys/zfs_fuid.h>
5588b7b0fMatthew Ahrens#include <sys/arc.h>
56b24ab67Jeff Bonwick#include <sys/ddt.h>
57de6628fck#undef ZFS_MAXNAMELEN
58de6628fck#undef verify
59de6628fck#include <libzfs.h>
60fa9e406ahrens
616de8f41Victor Latushkin#define	ZDB_COMPRESS_NAME(idx) ((idx) < ZIO_COMPRESS_FUNCTIONS ? \
626de8f41Victor Latushkin    zio_compress_table[(idx)].ci_name : "UNKNOWN")
636de8f41Victor Latushkin#define	ZDB_CHECKSUM_NAME(idx) ((idx) < ZIO_CHECKSUM_FUNCTIONS ? \
646de8f41Victor Latushkin    zio_checksum_table[(idx)].ci_name : "UNKNOWN")
656de8f41Victor Latushkin#define	ZDB_OT_NAME(idx) ((idx) < DMU_OT_NUMTYPES ? \
666de8f41Victor Latushkin    dmu_ot[(idx)].ot_name : "UNKNOWN")
676de8f41Victor Latushkin#define	ZDB_OT_TYPE(idx) ((idx) < DMU_OT_NUMTYPES ? (idx) : DMU_OT_NUMTYPES)
686de8f41Victor Latushkin
69feef89cVictor Latushkin#ifndef lint
70feef89cVictor Latushkinextern int zfs_recover;
71feef89cVictor Latushkin#else
72feef89cVictor Latushkinint zfs_recover;
73feef89cVictor Latushkin#endif
74feef89cVictor Latushkin
75fa9e406ahrensconst char cmdname[] = "zdb";
76fa9e406ahrensuint8_t dump_opt[256];
77fa9e406ahrens
78fa9e406ahrenstypedef void object_viewer_t(objset_t *, uint64_t, void *data, size_t size);
79fa9e406ahrens
80fa9e406ahrensextern void dump_intent_log(zilog_t *);
81fa9e406ahrensuint64_t *zopt_object = NULL;
82fa9e406ahrensint zopt_objects = 0;
83de6628fcklibzfs_handle_t *g_zfs;
84fa9e406ahrens
85fa9e406ahrens/*
86fa9e406ahrens * These libumem hooks provide a reasonable set of defaults for the allocator's
87fa9e406ahrens * debugging facilities.
88fa9e406ahrens */
89fa9e406ahrensconst char *
90fa9e406ahrens_umem_debug_init()
91fa9e406ahrens{
92fa9e406ahrens	return ("default,verbose"); /* $UMEM_DEBUG setting */
93fa9e406ahrens}
94fa9e406ahrens
95fa9e406ahrensconst char *
96fa9e406ahrens_umem_logging_init(void)
97fa9e406ahrens{
98fa9e406ahrens	return ("fail,contents"); /* $UMEM_LOGGING setting */
99fa9e406ahrens}
100fa9e406ahrens
101fa9e406ahrensstatic void
102fa9e406ahrensusage(void)
103fa9e406ahrens{
104fa9e406ahrens	(void) fprintf(stderr,
105f0ba89bJeff Bonwick	    "Usage: %s [-CumdibcsDvhL] poolname [object...]\n"
1063ad6c7fVictor Latushkin	    "       %s [-div] dataset [object...]\n"
10787219dbVictor Latushkin	    "       %s -m [-L] poolname [vdev [metaslab...]]\n"
10807428bdVictor Latushkin	    "       %s -R poolname vdev:offset:size[:flags]\n"
109bbfd46cJeff Bonwick	    "       %s -S poolname\n"
11053b9a4aVictor Latushkin	    "       %s -l [-u] device\n"
11107428bdVictor Latushkin	    "       %s -C\n\n",
112bbfd46cJeff Bonwick	    cmdname, cmdname, cmdname, cmdname, cmdname, cmdname, cmdname);
1133ad6c7fVictor Latushkin
1143ad6c7fVictor Latushkin	(void) fprintf(stderr, "    Dataset name must include at least one "
1153ad6c7fVictor Latushkin	    "separator character '/' or '@'\n");
1163ad6c7fVictor Latushkin	(void) fprintf(stderr, "    If dataset name is specified, only that "
1173ad6c7fVictor Latushkin	    "dataset is dumped\n");
1183ad6c7fVictor Latushkin	(void) fprintf(stderr, "    If object numbers are specified, only "
1193ad6c7fVictor Latushkin	    "those objects are dumped\n\n");
1203ad6c7fVictor Latushkin	(void) fprintf(stderr, "    Options to control amount of output:\n");
1213ad6c7fVictor Latushkin	(void) fprintf(stderr, "        -u uberblock\n");
1223ad6c7fVictor Latushkin	(void) fprintf(stderr, "        -d dataset(s)\n");
1233ad6c7fVictor Latushkin	(void) fprintf(stderr, "        -i intent logs\n");
12407428bdVictor Latushkin	(void) fprintf(stderr, "        -C config (or cachefile if alone)\n");
1258f18d1fGeorge Wilson	(void) fprintf(stderr, "        -h pool history\n");
1263ad6c7fVictor Latushkin	(void) fprintf(stderr, "        -b block statistics\n");
1273ad6c7fVictor Latushkin	(void) fprintf(stderr, "        -m metaslabs\n");
1283ad6c7fVictor Latushkin	(void) fprintf(stderr, "        -c checksum all metadata (twice for "
1296365109Victor Latushkin	    "all data) blocks\n");
1303ad6c7fVictor Latushkin	(void) fprintf(stderr, "        -s report stats on zdb's I/O\n");
131f0ba89bJeff Bonwick	(void) fprintf(stderr, "        -D dedup statistics\n");
132b24ab67Jeff Bonwick	(void) fprintf(stderr, "        -S simulate dedup to measure effect\n");
1333ad6c7fVictor Latushkin	(void) fprintf(stderr, "        -v verbose (applies to all others)\n");
134fa9e406ahrens	(void) fprintf(stderr, "        -l dump label contents\n");
13582a0a98Victor Latushkin	(void) fprintf(stderr, "        -L disable leak tracking (do not "
13682a0a98Victor Latushkin	    "load spacemaps)\n");
137d41e764ek	(void) fprintf(stderr, "        -R read and display block from a "
1383ad6c7fVictor Latushkin	    "device\n\n");
1393ad6c7fVictor Latushkin	(void) fprintf(stderr, "    Below options are intended for use "
1403ad6c7fVictor Latushkin	    "with other options (except -l):\n");
141feef89cVictor Latushkin	(void) fprintf(stderr, "        -A ignore assertions (-A), enable "
142feef89cVictor Latushkin	    "panic recovery (-AA) or both (-AAA)\n");
143c8ee184Victor Latushkin	(void) fprintf(stderr, "        -F attempt automatic rewind within "
144c8ee184Victor Latushkin	    "safe range of transaction groups\n");
1453ad6c7fVictor Latushkin	(void) fprintf(stderr, "        -U <cachefile_path> -- use alternate "
1463ad6c7fVictor Latushkin	    "cachefile\n");
147c8ee184Victor Latushkin	(void) fprintf(stderr, "        -X attempt extreme rewind (does not "
148c8ee184Victor Latushkin	    "work with dataset)\n");
1493ad6c7fVictor Latushkin	(void) fprintf(stderr, "        -e pool is exported/destroyed/"
1503ad6c7fVictor Latushkin	    "has altroot/not in a cachefile\n");
1513ad6c7fVictor Latushkin	(void) fprintf(stderr, "        -p <path> -- use one or more with "
1523ad6c7fVictor Latushkin	    "-e to specify path to vdev dir\n");
1533ad6c7fVictor Latushkin	(void) fprintf(stderr, "        -t <txg> -- highest txg to use when "
1542e55192Victor Latushkin	    "searching for uberblocks\n");
155fa9e406ahrens	(void) fprintf(stderr, "Specify an option more than once (e.g. -bb) "
156fa9e406ahrens	    "to make only that option verbose\n");
157fa9e406ahrens	(void) fprintf(stderr, "Default is to dump everything non-verbosely\n");
158fa9e406ahrens	exit(1);
159fa9e406ahrens}
160fa9e406ahrens
161ccba080Rich Morris/*
162ccba080Rich Morris * Called for usage errors that are discovered after a call to spa_open(),
163ccba080Rich Morris * dmu_bonus_hold(), or pool_match().  abort() is called for other errors.
164ccba080Rich Morris */
165ccba080Rich Morris
166fa9e406ahrensstatic void
167fa9e406ahrensfatal(const char *fmt, ...)
168fa9e406ahrens{
169fa9e406ahrens	va_list ap;
170fa9e406ahrens
171fa9e406ahrens	va_start(ap, fmt);
172fa9e406ahrens	(void) fprintf(stderr, "%s: ", cmdname);
173fa9e406ahrens	(void) vfprintf(stderr, fmt, ap);
174fa9e406ahrens	va_end(ap);
175fa9e406ahrens	(void) fprintf(stderr, "\n");
176fa9e406ahrens
177ccba080Rich Morris	exit(1);
178fa9e406ahrens}
179fa9e406ahrens
180fa9e406ahrens/* ARGSUSED */
181fa9e406ahrensstatic void
182fa9e406ahrensdump_packed_nvlist(objset_t *os, uint64_t object, void *data, size_t size)
183fa9e406ahrens{
184fa9e406ahrens	nvlist_t *nv;
185fa9e406ahrens	size_t nvsize = *(uint64_t *)data;
186fa9e406ahrens	char *packed = umem_alloc(nvsize, UMEM_NOFAIL);
187fa9e406ahrens
1887bfdf01Neil Perrin	VERIFY(0 == dmu_read(os, object, 0, nvsize, packed, DMU_READ_PREFETCH));
189fa9e406ahrens
190fa9e406ahrens	VERIFY(nvlist_unpack(packed, nvsize, &nv, 0) == 0);
191fa9e406ahrens
192fa9e406ahrens	umem_free(packed, nvsize);
193fa9e406ahrens
194fa9e406ahrens	dump_nvlist(nv, 8);
195fa9e406ahrens
196fa9e406ahrens	nvlist_free(nv);
197fa9e406ahrens}
198fa9e406ahrens
199fa9e406ahrensconst char dump_zap_stars[] = "****************************************";
200fa9e406ahrensconst int dump_zap_width = sizeof (dump_zap_stars) - 1;
201fa9e406ahrens
202fa9e406ahrensstatic void
203fa9e406ahrensdump_zap_histogram(uint64_t histo[ZAP_HISTOGRAM_SIZE])
204fa9e406ahrens{
205fa9e406ahrens	int i;
206fa9e406ahrens	int minidx = ZAP_HISTOGRAM_SIZE - 1;
207fa9e406ahrens	int maxidx = 0;
208fa9e406ahrens	uint64_t max = 0;
209fa9e406ahrens
210fa9e406ahrens	for (i = 0; i < ZAP_HISTOGRAM_SIZE; i++) {
211fa9e406ahrens		if (histo[i] > max)
212fa9e406ahrens			max = histo[i];
213fa9e406ahrens		if (histo[i] > 0 && i > maxidx)
214fa9e406ahrens			maxidx = i;
215fa9e406ahrens		if (histo[i] > 0 && i < minidx)
216fa9e406ahrens			minidx = i;
217fa9e406ahrens	}
218fa9e406ahrens
219fa9e406ahrens	if (max < dump_zap_width)
220fa9e406ahrens		max = dump_zap_width;
221fa9e406ahrens
222fa9e406ahrens	for (i = minidx; i <= maxidx; i++)
223fa9e406ahrens		(void) printf("\t\t\t%u: %6llu %s\n", i, (u_longlong_t)histo[i],
224fa9e406ahrens		    &dump_zap_stars[(max - histo[i]) * dump_zap_width / max]);
225fa9e406ahrens}
226fa9e406ahrens
227fa9e406ahrensstatic void
228fa9e406ahrensdump_zap_stats(objset_t *os, uint64_t object)
229fa9e406ahrens{
230fa9e406ahrens	int error;
231fa9e406ahrens	zap_stats_t zs;
232fa9e406ahrens
233fa9e406ahrens	error = zap_get_stats(os, object, &zs);
234fa9e406ahrens	if (error)
235fa9e406ahrens		return;
236fa9e406ahrens
237fa9e406ahrens	if (zs.zs_ptrtbl_len == 0) {
238fa9e406ahrens		ASSERT(zs.zs_num_blocks == 1);
239fa9e406ahrens		(void) printf("\tmicrozap: %llu bytes, %llu entries\n",
240fa9e406ahrens		    (u_longlong_t)zs.zs_blocksize,
241fa9e406ahrens		    (u_longlong_t)zs.zs_num_entries);
242fa9e406ahrens		return;
243fa9e406ahrens	}
244fa9e406ahrens
245fa9e406ahrens	(void) printf("\tFat ZAP stats:\n");
2468248818nd
2478248818nd	(void) printf("\t\tPointer table:\n");
2488248818nd	(void) printf("\t\t\t%llu elements\n",
249fa9e406ahrens	    (u_longlong_t)zs.zs_ptrtbl_len);
2508248818nd	(void) printf("\t\t\tzt_blk: %llu\n",
2518248818nd	    (u_longlong_t)zs.zs_ptrtbl_zt_blk);
2528248818nd	(void) printf("\t\t\tzt_numblks: %llu\n",
2538248818nd	    (u_longlong_t)zs.zs_ptrtbl_zt_numblks);
2548248818nd	(void) printf("\t\t\tzt_shift: %llu\n",
2558248818nd	    (u_longlong_t)zs.zs_ptrtbl_zt_shift);
2568248818nd	(void) printf("\t\t\tzt_blks_copied: %llu\n",
2578248818nd	    (u_longlong_t)zs.zs_ptrtbl_blks_copied);
2588248818nd	(void) printf("\t\t\tzt_nextblk: %llu\n",
2598248818nd	    (u_longlong_t)zs.zs_ptrtbl_nextblk);
2608248818nd
261fa9e406ahrens	(void) printf("\t\tZAP entries: %llu\n",
262fa9e406ahrens	    (u_longlong_t)zs.zs_num_entries);
263fa9e406ahrens	(void) printf("\t\tLeaf blocks: %llu\n",
264fa9e406ahrens	    (u_longlong_t)zs.zs_num_leafs);
265fa9e406ahrens	(void) printf("\t\tTotal blocks: %llu\n",
266fa9e406ahrens	    (u_longlong_t)zs.zs_num_blocks);
2678248818nd	(void) printf("\t\tzap_block_type: 0x%llx\n",
2688248818nd	    (u_longlong_t)zs.zs_block_type);
2698248818nd	(void) printf("\t\tzap_magic: 0x%llx\n",
2708248818nd	    (u_longlong_t)zs.zs_magic);
2718248818nd	(void) printf("\t\tzap_salt: 0x%llx\n",
2728248818nd	    (u_longlong_t)zs.zs_salt);
273fa9e406ahrens
274fa9e406ahrens	(void) printf("\t\tLeafs with 2^n pointers:\n");
275fa9e406ahrens	dump_zap_histogram(zs.zs_leafs_with_2n_pointers);
276fa9e406ahrens
277fa9e406ahrens	(void) printf("\t\tBlocks with n*5 entries:\n");
278fa9e406ahrens	dump_zap_histogram(zs.zs_blocks_with_n5_entries);
279fa9e406ahrens
280fa9e406ahrens	(void) printf("\t\tBlocks n/10 full:\n");
281fa9e406ahrens	dump_zap_histogram(zs.zs_blocks_n_tenths_full);
282fa9e406ahrens
283fa9e406ahrens	(void) printf("\t\tEntries with n chunks:\n");
284fa9e406ahrens	dump_zap_histogram(zs.zs_entries_using_n_chunks);
285fa9e406ahrens
286fa9e406ahrens	(void) printf("\t\tBuckets with n entries:\n");
287fa9e406ahrens	dump_zap_histogram(zs.zs_buckets_with_n_entries);
288fa9e406ahrens}
289fa9e406ahrens
290fa9e406ahrens/*ARGSUSED*/
291fa9e406ahrensstatic void
292fa9e406ahrensdump_none(objset_t *os, uint64_t object, void *data, size_t size)
293fa9e406ahrens{
294fa9e406ahrens}
295fa9e406ahrens
296fa9e406ahrens/*ARGSUSED*/
2976de8f41Victor Latushkinstatic void
2986de8f41Victor Latushkindump_unknown(objset_t *os, uint64_t object, void *data, size_t size)
2996de8f41Victor Latushkin{
3006de8f41Victor Latushkin	(void) printf("\tUNKNOWN OBJECT TYPE\n");
3016de8f41Victor Latushkin}
3026de8f41Victor Latushkin
3036de8f41Victor Latushkin/*ARGSUSED*/
304fa9e406ahrensvoid
305fa9e406ahrensdump_uint8(objset_t *os, uint64_t object, void *data, size_t size)
306fa9e406ahrens{
307fa9e406ahrens}
308fa9e406ahrens
309fa9e406ahrens/*ARGSUSED*/
310fa9e406ahrensstatic void
311fa9e406ahrensdump_uint64(objset_t *os, uint64_t object, void *data, size_t size)
312fa9e406ahrens{
313fa9e406ahrens}
314fa9e406ahrens
315fa9e406ahrens/*ARGSUSED*/
316fa9e406ahrensstatic void
317fa9e406ahrensdump_zap(objset_t *os, uint64_t object, void *data, size_t size)
318fa9e406ahrens{
319fa9e406ahrens	zap_cursor_t zc;
320fa9e406ahrens	zap_attribute_t attr;
321fa9e406ahrens	void *prop;
322fa9e406ahrens	int i;
323fa9e406ahrens
324fa9e406ahrens	dump_zap_stats(os, object);
325fa9e406ahrens	(void) printf("\n");
326fa9e406ahrens
327fa9e406ahrens	for (zap_cursor_init(&zc, os, object);
328fa9e406ahrens	    zap_cursor_retrieve(&zc, &attr) == 0;
329fa9e406ahrens	    zap_cursor_advance(&zc)) {
330fa9e406ahrens		(void) printf("\t\t%s = ", attr.za_name);
331fa9e406ahrens		if (attr.za_num_integers == 0) {
332fa9e406ahrens			(void) printf("\n");
333fa9e406ahrens			continue;
334fa9e406ahrens		}
335fa9e406ahrens		prop = umem_zalloc(attr.za_num_integers *
336fa9e406ahrens		    attr.za_integer_length, UMEM_NOFAIL);
337fa9e406ahrens		(void) zap_lookup(os, object, attr.za_name,
338fa9e406ahrens		    attr.za_integer_length, attr.za_num_integers, prop);
339fa9e406ahrens		if (attr.za_integer_length == 1) {
340fa9e406ahrens			(void) printf("%s", (char *)prop);
341fa9e406ahrens		} else {
342fa9e406ahrens			for (i = 0; i < attr.za_num_integers; i++) {
343fa9e406ahrens				switch (attr.za_integer_length) {
344fa9e406ahrens				case 2:
345fa9e406ahrens					(void) printf("%u ",
346fa9e406ahrens					    ((uint16_t *)prop)[i]);
347fa9e406ahrens					break;
348fa9e406ahrens				case 4:
349fa9e406ahrens					(void) printf("%u ",
350fa9e406ahrens					    ((uint32_t *)prop)[i]);
351fa9e406ahrens					break;
352fa9e406ahrens				case 8:
353fa9e406ahrens					(void) printf("%lld ",
354fa9e406ahrens					    (u_longlong_t)((int64_t *)prop)[i]);
355fa9e406ahrens					break;
356fa9e406ahrens				}
357fa9e406ahrens			}
358fa9e406ahrens		}
359fa9e406ahrens		(void) printf("\n");
360fa9e406ahrens		umem_free(prop, attr.za_num_integers * attr.za_integer_length);
361fa9e406ahrens	}
36287e5029ahrens	zap_cursor_fini(&zc);
363fa9e406ahrens}
364fa9e406ahrens
365e743726ahrens/*ARGSUSED*/
366e743726ahrensstatic void
367486ae71Matthew Ahrensdump_ddt_zap(objset_t *os, uint64_t object, void *data, size_t size)
368486ae71Matthew Ahrens{
369486ae71Matthew Ahrens	dump_zap_stats(os, object);
370486ae71Matthew Ahrens	/* contents are printed elsewhere, properly decoded */
371486ae71Matthew Ahrens}
372486ae71Matthew Ahrens
373486ae71Matthew Ahrens/*ARGSUSED*/
374486ae71Matthew Ahrensstatic void
3750a586ceMark Shellenbaumdump_sa_attrs(objset_t *os, uint64_t object, void *data, size_t size)
3760a586ceMark Shellenbaum{
3770a586ceMark Shellenbaum	zap_cursor_t zc;
3780a586ceMark Shellenbaum	zap_attribute_t attr;
3790a586ceMark Shellenbaum
3800a586ceMark Shellenbaum	dump_zap_stats(os, object);
3810a586ceMark Shellenbaum	(void) printf("\n");
3820a586ceMark Shellenbaum
3830a586ceMark Shellenbaum	for (zap_cursor_init(&zc, os, object);
3840a586ceMark Shellenbaum	    zap_cursor_retrieve(&zc, &attr) == 0;
3850a586ceMark Shellenbaum	    zap_cursor_advance(&zc)) {
3860a586ceMark Shellenbaum		(void) printf("\t\t%s = ", attr.za_name);
3870a586ceMark Shellenbaum		if (attr.za_num_integers == 0) {
3880a586ceMark Shellenbaum			(void) printf("\n");
3890a586ceMark Shellenbaum			continue;
3900a586ceMark Shellenbaum		}
3910a586ceMark Shellenbaum		(void) printf(" %llx : [%d:%d:%d]\n",
3920a586ceMark Shellenbaum		    (u_longlong_t)attr.za_first_integer,
3930a586ceMark Shellenbaum		    (int)ATTR_LENGTH(attr.za_first_integer),
3940a586ceMark Shellenbaum		    (int)ATTR_BSWAP(attr.za_first_integer),
3950a586ceMark Shellenbaum		    (int)ATTR_NUM(attr.za_first_integer));
3960a586ceMark Shellenbaum	}
3970a586ceMark Shellenbaum	zap_cursor_fini(&zc);
3980a586ceMark Shellenbaum}
3990a586ceMark Shellenbaum
4000a586ceMark Shellenbaum/*ARGSUSED*/
4010a586ceMark Shellenbaumstatic void
4020a586ceMark Shellenbaumdump_sa_layouts(objset_t *os, uint64_t object, void *data, size_t size)
4030a586ceMark Shellenbaum{
4040a586ceMark Shellenbaum	zap_cursor_t zc;
4050a586ceMark Shellenbaum	zap_attribute_t attr;
4060a586ceMark Shellenbaum	uint16_t *layout_attrs;
4070a586ceMark Shellenbaum	int i;
4080a586ceMark Shellenbaum
4090a586ceMark Shellenbaum	dump_zap_stats(os, object);
4100a586ceMark Shellenbaum	(void) printf("\n");
4110a586ceMark Shellenbaum
4120a586ceMark Shellenbaum	for (zap_cursor_init(&zc, os, object);
4130a586ceMark Shellenbaum	    zap_cursor_retrieve(&zc, &attr) == 0;
4140a586ceMark Shellenbaum	    zap_cursor_advance(&zc)) {
4150a586ceMark Shellenbaum		(void) printf("\t\t%s = [", attr.za_name);
4160a586ceMark Shellenbaum		if (attr.za_num_integers == 0) {
4170a586ceMark Shellenbaum			(void) printf("\n");
4180a586ceMark Shellenbaum			continue;
4190a586ceMark Shellenbaum		}
4200a586ceMark Shellenbaum
4210a586ceMark Shellenbaum		VERIFY(attr.za_integer_length == 2);
4220a586ceMark Shellenbaum		layout_attrs = umem_zalloc(attr.za_num_integers *
4230a586ceMark Shellenbaum		    attr.za_integer_length, UMEM_NOFAIL);
4240a586ceMark Shellenbaum
4250a586ceMark Shellenbaum		VERIFY(zap_lookup(os, object, attr.za_name,
4260a586ceMark Shellenbaum		    attr.za_integer_length,
4270a586ceMark Shellenbaum		    attr.za_num_integers, layout_attrs) == 0);
4280a586ceMark Shellenbaum
4290a586ceMark Shellenbaum		for (i = 0; i != attr.za_num_integers; i++)
4300a586ceMark Shellenbaum			(void) printf(" %d ", (int)layout_attrs[i]);
4310a586ceMark Shellenbaum		(void) printf("]\n");
4320a586ceMark Shellenbaum		umem_free(layout_attrs,
4330a586ceMark Shellenbaum		    attr.za_num_integers * attr.za_integer_length);
4340a586ceMark Shellenbaum	}
4350a586ceMark Shellenbaum	zap_cursor_fini(&zc);
4360a586ceMark Shellenbaum}
4370a586ceMark Shellenbaum
4380a586ceMark Shellenbaum/*ARGSUSED*/
4390a586ceMark Shellenbaumstatic void
440e743726ahrensdump_zpldir(objset_t *os, uint64_t object, void *data, size_t size)
441e743726ahrens{
442e743726ahrens	zap_cursor_t zc;
443e743726ahrens	zap_attribute_t attr;
444e743726ahrens	const char *typenames[] = {
445e743726ahrens		/* 0 */ "not specified",
446e743726ahrens		/* 1 */ "FIFO",
447e743726ahrens		/* 2 */ "Character Device",
448e743726ahrens		/* 3 */ "3 (invalid)",
449e743726ahrens		/* 4 */ "Directory",
450e743726ahrens		/* 5 */ "5 (invalid)",
451e743726ahrens		/* 6 */ "Block Device",
452e743726ahrens		/* 7 */ "7 (invalid)",
453e743726ahrens		/* 8 */ "Regular File",
454e743726ahrens		/* 9 */ "9 (invalid)",
455e743726ahrens		/* 10 */ "Symbolic Link",
456e743726ahrens		/* 11 */ "11 (invalid)",
457e743726ahrens		/* 12 */ "Socket",
458e743726ahrens		/* 13 */ "Door",
459e743726ahrens		/* 14 */ "Event Port",
460e743726ahrens		/* 15 */ "15 (invalid)",
461e743726ahrens	};
462e743726ahrens
463e743726ahrens	dump_zap_stats(os, object);
464e743726ahrens	(void) printf("\n");
465e743726ahrens
466e743726ahrens	for (zap_cursor_init(&zc, os, object);
467e743726ahrens	    zap_cursor_retrieve(&zc, &attr) == 0;
468e743726ahrens	    zap_cursor_advance(&zc)) {
469e743726ahrens		(void) printf("\t\t%s = %lld (type: %s)\n",
470e743726ahrens		    attr.za_name, ZFS_DIRENT_OBJ(attr.za_first_integer),
471e743726ahrens		    typenames[ZFS_DIRENT_TYPE(attr.za_first_integer)]);
472e743726ahrens	}
473e743726ahrens	zap_cursor_fini(&zc);
474e743726ahrens}
475e743726ahrens
476fa9e406ahrensstatic void
477fa9e406ahrensdump_spacemap(objset_t *os, space_map_obj_t *smo, space_map_t *sm)
478fa9e406ahrens{
479fa9e406ahrens	uint64_t alloc, offset, entry;
480ecc2d60bonwick	uint8_t mapshift = sm->sm_shift;
481fa9e406ahrens	uint64_t mapstart = sm->sm_start;
4828053a26ck	char *ddata[] = { "ALLOC", "FREE", "CONDENSE", "INVALID",
4838053a26ck			    "INVALID", "INVALID", "INVALID", "INVALID" };
484fa9e406ahrens
485fa9e406ahrens	if (smo->smo_object == 0)
486fa9e406ahrens		return;
487fa9e406ahrens
488fa9e406ahrens	/*
489fa9e406ahrens	 * Print out the freelist entries in both encoded and decoded form.
490fa9e406ahrens	 */
491fa9e406ahrens	alloc = 0;
492fa9e406ahrens	for (offset = 0; offset < smo->smo_objsize; offset += sizeof (entry)) {
493ea8dc4beschrock		VERIFY(0 == dmu_read(os, smo->smo_object, offset,
4947bfdf01Neil Perrin		    sizeof (entry), &entry, DMU_READ_PREFETCH));
495fa9e406ahrens		if (SM_DEBUG_DECODE(entry)) {
49687219dbVictor Latushkin			(void) printf("\t    [%6llu] %s: txg %llu, pass %llu\n",
497fa9e406ahrens			    (u_longlong_t)(offset / sizeof (entry)),
498fa9e406ahrens			    ddata[SM_DEBUG_ACTION_DECODE(entry)],
4995ad8204nd			    (u_longlong_t)SM_DEBUG_TXG_DECODE(entry),
5005ad8204nd			    (u_longlong_t)SM_DEBUG_SYNCPASS_DECODE(entry));
501fa9e406ahrens		} else {
50287219dbVictor Latushkin			(void) printf("\t    [%6llu]    %c  range:"
50387219dbVictor Latushkin			    " %010llx-%010llx  size: %06llx\n",
504fa9e406ahrens			    (u_longlong_t)(offset / sizeof (entry)),
505fa9e406ahrens			    SM_TYPE_DECODE(entry) == SM_ALLOC ? 'A' : 'F',
5065ad8204nd			    (u_longlong_t)((SM_OFFSET_DECODE(entry) <<
5075ad8204nd			    mapshift) + mapstart),
5085ad8204nd			    (u_longlong_t)((SM_OFFSET_DECODE(entry) <<
5095ad8204nd			    mapshift) + mapstart + (SM_RUN_DECODE(entry) <<
5105ad8204nd			    mapshift)),
5115ad8204nd			    (u_longlong_t)(SM_RUN_DECODE(entry) << mapshift));
512fa9e406ahrens			if (SM_TYPE_DECODE(entry) == SM_ALLOC)
513fa9e406ahrens				alloc += SM_RUN_DECODE(entry) << mapshift;
514fa9e406ahrens			else
515fa9e406ahrens				alloc -= SM_RUN_DECODE(entry) << mapshift;
516fa9e406ahrens		}
517fa9e406ahrens	}
518fa9e406ahrens	if (alloc != smo->smo_alloc) {
519fa9e406ahrens		(void) printf("space_map_object alloc (%llu) INCONSISTENT "
520fa9e406ahrens		    "with space map summary (%llu)\n",
521fa9e406ahrens		    (u_longlong_t)smo->smo_alloc, (u_longlong_t)alloc);
522fa9e406ahrens	}
523fa9e406ahrens}
524fa9e406ahrens
525fa9e406ahrensstatic void
526d6e555bGeorge Wilsondump_metaslab_stats(metaslab_t *msp)
527d6e555bGeorge Wilson{
528d6e555bGeorge Wilson	char maxbuf[5];
529d6e555bGeorge Wilson	space_map_t *sm = &msp->ms_map;
530d6e555bGeorge Wilson	avl_tree_t *t = sm->sm_pp_root;
531d6e555bGeorge Wilson	int free_pct = sm->sm_space * 100 / sm->sm_size;
532d6e555bGeorge Wilson
533d6e555bGeorge Wilson	nicenum(space_map_maxsize(sm), maxbuf);
534d6e555bGeorge Wilson
53587219dbVictor Latushkin	(void) printf("\t %25s %10lu   %7s  %6s   %4s %4d%%\n",
536d6e555bGeorge Wilson	    "segments", avl_numnodes(t), "maxsize", maxbuf,
537d6e555bGeorge Wilson	    "freepct", free_pct);
538d6e555bGeorge Wilson}
539d6e555bGeorge Wilson
540d6e555bGeorge Wilsonstatic void
541fa9e406ahrensdump_metaslab(metaslab_t *msp)
542fa9e406ahrens{
543fa9e406ahrens	vdev_t *vd = msp->ms_group->mg_vd;
544fa9e406ahrens	spa_t *spa = vd->vdev_spa;
54580eb36fGeorge Wilson	space_map_t *sm = &msp->ms_map;
54680eb36fGeorge Wilson	space_map_obj_t *smo = &msp->ms_smo;
54780eb36fGeorge Wilson	char freebuf[5];
548fa9e406ahrens
54980eb36fGeorge Wilson	nicenum(sm->sm_size - smo->smo_alloc, freebuf);
550fa9e406ahrens
551fa9e406ahrens	(void) printf(
55287219dbVictor Latushkin	    "\tmetaslab %6llu   offset %12llx   spacemap %6llu   free    %5s\n",
55380eb36fGeorge Wilson	    (u_longlong_t)(sm->sm_start / sm->sm_size),
55480eb36fGeorge Wilson	    (u_longlong_t)sm->sm_start, (u_longlong_t)smo->smo_object, freebuf);
555fa9e406ahrens
55687219dbVictor Latushkin	if (dump_opt['m'] > 1 && !dump_opt['L']) {
557d6e555bGeorge Wilson		mutex_enter(&msp->ms_lock);
55880eb36fGeorge Wilson		space_map_load_wait(sm);
559bc3975bVictor Latushkin		if (!sm->sm_loaded)
56080eb36fGeorge Wilson			VERIFY(space_map_load(sm, zfs_metaslab_ops,
56180eb36fGeorge Wilson			    SM_FREE, smo, spa->spa_meta_objset) == 0);
562bc3975bVictor Latushkin		dump_metaslab_stats(msp);
563bc3975bVictor Latushkin		space_map_unload(sm);
564d6e555bGeorge Wilson		mutex_exit(&msp->ms_lock);
565d6e555bGeorge Wilson	}
566d6e555bGeorge Wilson
567d6e555bGeorge Wilson	if (dump_opt['d'] > 5 || dump_opt['m'] > 2) {
56880eb36fGeorge Wilson		ASSERT(sm->sm_size == (1ULL << vd->vdev_ms_shift));
569d6e555bGeorge Wilson
570d6e555bGeorge Wilson		mutex_enter(&msp->ms_lock);
57180eb36fGeorge Wilson		dump_spacemap(spa->spa_meta_objset, smo, sm);
572d6e555bGeorge Wilson		mutex_exit(&msp->ms_lock);
573d6e555bGeorge Wilson	}
57487219dbVictor Latushkin}
575fa9e406ahrens
57687219dbVictor Latushkinstatic void
57787219dbVictor Latushkinprint_vdev_metaslab_header(vdev_t *vd)
57887219dbVictor Latushkin{
57987219dbVictor Latushkin	(void) printf("\tvdev %10llu\n\t%-10s%5llu   %-19s   %-15s   %-10s\n",
58087219dbVictor Latushkin	    (u_longlong_t)vd->vdev_id,
58187219dbVictor Latushkin	    "metaslabs", (u_longlong_t)vd->vdev_ms_count,
58287219dbVictor Latushkin	    "offset", "spacemap", "free");
58387219dbVictor Latushkin	(void) printf("\t%15s   %19s   %15s   %10s\n",
58487219dbVictor Latushkin	    "---------------", "-------------------",
58587219dbVictor Latushkin	    "---------------", "-------------");
586fa9e406ahrens}
587fa9e406ahrens
588fa9e406ahrensstatic void
589fa9e406ahrensdump_metaslabs(spa_t *spa)
590fa9e406ahrens{
59187219dbVictor Latushkin	vdev_t *vd, *rvd = spa->spa_root_vdev;
59287219dbVictor Latushkin	uint64_t m, c = 0, children = rvd->vdev_children;
593fa9e406ahrens
594fa9e406ahrens	(void) printf("\nMetaslabs:\n");
595fa9e406ahrens
59687219dbVictor Latushkin	if (!dump_opt['d'] && zopt_objects > 0) {
59787219dbVictor Latushkin		c = zopt_object[0];
59887219dbVictor Latushkin
59987219dbVictor Latushkin		if (c >= children)
60087219dbVictor Latushkin			(void) fatal("bad vdev id: %llu", (u_longlong_t)c);
601fa9e406ahrens
60287219dbVictor Latushkin		if (zopt_objects > 1) {
60387219dbVictor Latushkin			vd = rvd->vdev_child[c];
60487219dbVictor Latushkin			print_vdev_metaslab_header(vd);
60587219dbVictor Latushkin
60687219dbVictor Latushkin			for (m = 1; m < zopt_objects; m++) {
60787219dbVictor Latushkin				if (zopt_object[m] < vd->vdev_ms_count)
60887219dbVictor Latushkin					dump_metaslab(
60987219dbVictor Latushkin					    vd->vdev_ms[zopt_object[m]]);
61087219dbVictor Latushkin				else
61187219dbVictor Latushkin					(void) fprintf(stderr, "bad metaslab "
61287219dbVictor Latushkin					    "number %llu\n",
61387219dbVictor Latushkin					    (u_longlong_t)zopt_object[m]);
61487219dbVictor Latushkin			}
61587219dbVictor Latushkin			(void) printf("\n");
61687219dbVictor Latushkin			return;
61787219dbVictor Latushkin		}
61887219dbVictor Latushkin		children = c + 1;
61987219dbVictor Latushkin	}
62087219dbVictor Latushkin	for (; c < children; c++) {
62187219dbVictor Latushkin		vd = rvd->vdev_child[c];
62287219dbVictor Latushkin		print_vdev_metaslab_header(vd);
623fa9e406ahrens
624fa9e406ahrens		for (m = 0; m < vd->vdev_ms_count; m++)
625fa9e406ahrens			dump_metaslab(vd->vdev_ms[m]);
626fa9e406ahrens		(void) printf("\n");
627fa9e406ahrens	}
628fa9e406ahrens}
629fa9e406ahrens
630fa9e406ahrensstatic void
631b24ab67Jeff Bonwickdump_dde(const ddt_t *ddt, const ddt_entry_t *dde, uint64_t index)
632b24ab67Jeff Bonwick{
633b24ab67Jeff Bonwick	const ddt_phys_t *ddp = dde->dde_phys;
634b24ab67Jeff Bonwick	const ddt_key_t *ddk = &dde->dde_key;
635b24ab67Jeff Bonwick	char *types[4] = { "ditto", "single", "double", "triple" };
636b24ab67Jeff Bonwick	char blkbuf[BP_SPRINTF_LEN];
637b24ab67Jeff Bonwick	blkptr_t blk;
638b24ab67Jeff Bonwick
639b24ab67Jeff Bonwick	for (int p = 0; p < DDT_PHYS_TYPES; p++, ddp++) {
640b24ab67Jeff Bonwick		if (ddp->ddp_phys_birth == 0)
641b24ab67Jeff Bonwick			continue;
642bbfd46cJeff Bonwick		ddt_bp_create(ddt->ddt_checksum, ddk, ddp, &blk);
643b24ab67Jeff Bonwick		sprintf_blkptr(blkbuf, &blk);
644b24ab67Jeff Bonwick		(void) printf("index %llx refcnt %llu %s %s\n",
645b24ab67Jeff Bonwick		    (u_longlong_t)index, (u_longlong_t)ddp->ddp_refcnt,
646b24ab67Jeff Bonwick		    types[p], blkbuf);
647b24ab67Jeff Bonwick	}
648b24ab67Jeff Bonwick}
649b24ab67Jeff Bonwick
650b24ab67Jeff Bonwickstatic void
651b24ab67Jeff Bonwickdump_dedup_ratio(const ddt_stat_t *dds)
652b24ab67Jeff Bonwick{
653b24ab67Jeff Bonwick	double rL, rP, rD, D, dedup, compress, copies;
654b24ab67Jeff Bonwick
655b24ab67Jeff Bonwick	if (dds->dds_blocks == 0)
656b24ab67Jeff Bonwick		return;
657b24ab67Jeff Bonwick
658b24ab67Jeff Bonwick	rL = (double)dds->dds_ref_lsize;
659b24ab67Jeff Bonwick	rP = (double)dds->dds_ref_psize;
660b24ab67Jeff Bonwick	rD = (double)dds->dds_ref_dsize;
661b24ab67Jeff Bonwick	D = (double)dds->dds_dsize;
662b24ab67Jeff Bonwick
663b24ab67Jeff Bonwick	dedup = rD / D;
664b24ab67Jeff Bonwick	compress = rL / rP;
665b24ab67Jeff Bonwick	copies = rD / rP;
666b24ab67Jeff Bonwick
667b24ab67Jeff Bonwick	(void) printf("dedup = %.2f, compress = %.2f, copies = %.2f, "
668b24ab67Jeff Bonwick	    "dedup * compress / copies = %.2f\n\n",
669b24ab67Jeff Bonwick	    dedup, compress, copies, dedup * compress / copies);
670b24ab67Jeff Bonwick}
671b24ab67Jeff Bonwick
672b24ab67Jeff Bonwickstatic void
673b24ab67Jeff Bonwickdump_ddt(ddt_t *ddt, enum ddt_type type, enum ddt_class class)
674b24ab67Jeff Bonwick{
675b24ab67Jeff Bonwick	char name[DDT_NAMELEN];
676b24ab67Jeff Bonwick	ddt_entry_t dde;
677b24ab67Jeff Bonwick	uint64_t walk = 0;
678b24ab67Jeff Bonwick	dmu_object_info_t doi;
679b24ab67Jeff Bonwick	uint64_t count, dspace, mspace;
680b24ab67Jeff Bonwick	int error;
681b24ab67Jeff Bonwick
682b24ab67Jeff Bonwick	error = ddt_object_info(ddt, type, class, &doi);
683b24ab67Jeff Bonwick
684b24ab67Jeff Bonwick	if (error == ENOENT)
685b24ab67Jeff Bonwick		return;
686b24ab67Jeff Bonwick	ASSERT(error == 0);
687b24ab67Jeff Bonwick
688b24ab67Jeff Bonwick	count = ddt_object_count(ddt, type, class);
689b24ab67Jeff Bonwick	dspace = doi.doi_physical_blocks_512 << 9;
690b24ab67Jeff Bonwick	mspace = doi.doi_fill_count * doi.doi_data_block_size;
691b24ab67Jeff Bonwick
692b24ab67Jeff Bonwick	ASSERT(count != 0);	/* we should have destroyed it */
693b24ab67Jeff Bonwick
694b24ab67Jeff Bonwick	ddt_object_name(ddt, type, class, name);
695b24ab67Jeff Bonwick
696b24ab67Jeff Bonwick	(void) printf("%s: %llu entries, size %llu on disk, %llu in core\n",
697b24ab67Jeff Bonwick	    name,
698b24ab67Jeff Bonwick	    (u_longlong_t)count,
699b24ab67Jeff Bonwick	    (u_longlong_t)(dspace / count),
700b24ab67Jeff Bonwick	    (u_longlong_t)(mspace / count));
701b24ab67Jeff Bonwick
702b24ab67Jeff Bonwick	if (dump_opt['D'] < 3)
703b24ab67Jeff Bonwick		return;
704b24ab67Jeff Bonwick
7059eb19f4George Wilson	zpool_dump_ddt(NULL, &ddt->ddt_histogram[type][class]);
706b24ab67Jeff Bonwick
707b24ab67Jeff Bonwick	if (dump_opt['D'] < 4)
708b24ab67Jeff Bonwick		return;
709b24ab67Jeff Bonwick
710b24ab67Jeff Bonwick	if (dump_opt['D'] < 5 && class == DDT_CLASS_UNIQUE)
711b24ab67Jeff Bonwick		return;
712b24ab67Jeff Bonwick
713b24ab67Jeff Bonwick	(void) printf("%s contents:\n\n", name);
714b24ab67Jeff Bonwick
715bbfd46cJeff Bonwick	while ((error = ddt_object_walk(ddt, type, class, &walk, &dde)) == 0)
716b24ab67Jeff Bonwick		dump_dde(ddt, &dde, walk);
717b24ab67Jeff Bonwick
718b24ab67Jeff Bonwick	ASSERT(error == ENOENT);
719b24ab67Jeff Bonwick
720b24ab67Jeff Bonwick	(void) printf("\n");
721b24ab67Jeff Bonwick}
722b24ab67Jeff Bonwick
723b24ab67Jeff Bonwickstatic void
724b24ab67Jeff Bonwickdump_all_ddts(spa_t *spa)
725b24ab67Jeff Bonwick{
726b24ab67Jeff Bonwick	ddt_histogram_t ddh_total = { 0 };
727b24ab67Jeff Bonwick	ddt_stat_t dds_total = { 0 };
728b24ab67Jeff Bonwick
729b24ab67Jeff Bonwick	for (enum zio_checksum c = 0; c < ZIO_CHECKSUM_FUNCTIONS; c++) {
730b24ab67Jeff Bonwick		ddt_t *ddt = spa->spa_ddt[c];
731b24ab67Jeff Bonwick		for (enum ddt_type type = 0; type < DDT_TYPES; type++) {
732b24ab67Jeff Bonwick			for (enum ddt_class class = 0; class < DDT_CLASSES;
733b24ab67Jeff Bonwick			    class++) {
734b24ab67Jeff Bonwick				dump_ddt(ddt, type, class);
735b24ab67Jeff Bonwick			}
736b24ab67Jeff Bonwick		}
737b24ab67Jeff Bonwick	}
738b24ab67Jeff Bonwick
7399eb19f4George Wilson	ddt_get_dedup_stats(spa, &dds_total);
740b24ab67Jeff Bonwick
741b24ab67Jeff Bonwick	if (dds_total.dds_blocks == 0) {
742b24ab67Jeff Bonwick		(void) printf("All DDTs are empty\n");
743b24ab67Jeff Bonwick		return;
744b24ab67Jeff Bonwick	}
745b24ab67Jeff Bonwick
746b24ab67Jeff Bonwick	(void) printf("\n");
747b24ab67Jeff Bonwick
748b24ab67Jeff Bonwick	if (dump_opt['D'] > 1) {
749b24ab67Jeff Bonwick		(void) printf("DDT histogram (aggregated over all DDTs):\n");
7509eb19f4George Wilson		ddt_get_dedup_histogram(spa, &ddh_total);
7519eb19f4George Wilson		zpool_dump_ddt(&dds_total, &ddh_total);
752b24ab67Jeff Bonwick	}
753b24ab67Jeff Bonwick
754b24ab67Jeff Bonwick	dump_dedup_ratio(&dds_total);
755b24ab67Jeff Bonwick}
756b24ab67Jeff Bonwick
757b24ab67Jeff Bonwickstatic void
7588ad4d6dJeff Bonwickdump_dtl_seg(space_map_t *sm, uint64_t start, uint64_t size)
7598ad4d6dJeff Bonwick{
7608ad4d6dJeff Bonwick	char *prefix = (void *)sm;
7618ad4d6dJeff Bonwick
7628ad4d6dJeff Bonwick	(void) printf("%s [%llu,%llu) length %llu\n",
7638ad4d6dJeff Bonwick	    prefix,
7648ad4d6dJeff Bonwick	    (u_longlong_t)start,
7658ad4d6dJeff Bonwick	    (u_longlong_t)(start + size),
7668ad4d6dJeff Bonwick	    (u_longlong_t)(size));
7678ad4d6dJeff Bonwick}
7688ad4d6dJeff Bonwick
7698ad4d6dJeff Bonwickstatic void
770fa9e406ahrensdump_dtl(vdev_t *vd, int indent)
771fa9e406ahrens{
7728ad4d6dJeff Bonwick	spa_t *spa = vd->vdev_spa;
7738ad4d6dJeff Bonwick	boolean_t required;
7748ad4d6dJeff Bonwick	char *name[DTL_TYPES] = { "missing", "partial", "scrub", "outage" };
7758ad4d6dJeff Bonwick	char prefix[256];
7768ad4d6dJeff Bonwick
7778f18d1fGeorge Wilson	spa_vdev_state_enter(spa, SCL_NONE);
7788ad4d6dJeff Bonwick	required = vdev_dtl_required(vd);
7798ad4d6dJeff Bonwick	(void) spa_vdev_state_exit(spa, NULL, 0);
780fa9e406ahrens
781fa9e406ahrens	if (indent == 0)
782fa9e406ahrens		(void) printf("\nDirty time logs:\n\n");
783fa9e406ahrens
7848ad4d6dJeff Bonwick	(void) printf("\t%*s%s [%s]\n", indent, "",
785e14bb32Jeff Bonwick	    vd->vdev_path ? vd->vdev_path :
7868ad4d6dJeff Bonwick	    vd->vdev_parent ? vd->vdev_ops->vdev_op_type : spa_name(spa),
7878ad4d6dJeff Bonwick	    required ? "DTL-required" : "DTL-expendable");
788fa9e406ahrens
7898ad4d6dJeff Bonwick	for (int t = 0; t < DTL_TYPES; t++) {
7908ad4d6dJeff Bonwick		space_map_t *sm = &vd->vdev_dtl[t];
7918ad4d6dJeff Bonwick		if (sm->sm_space == 0)
7928ad4d6dJeff Bonwick			continue;
7938ad4d6dJeff Bonwick		(void) snprintf(prefix, sizeof (prefix), "\t%*s%s",
7948ad4d6dJeff Bonwick		    indent + 2, "", name[t]);
7958ad4d6dJeff Bonwick		mutex_enter(sm->sm_lock);
7968ad4d6dJeff Bonwick		space_map_walk(sm, dump_dtl_seg, (void *)prefix);
7978ad4d6dJeff Bonwick		mutex_exit(sm->sm_lock);
7988ad4d6dJeff Bonwick		if (dump_opt['d'] > 5 && vd->vdev_children == 0)
7998ad4d6dJeff Bonwick			dump_spacemap(spa->spa_meta_objset,
8008ad4d6dJeff Bonwick			    &vd->vdev_dtl_smo, sm);
801fa9e406ahrens	}
802fa9e406ahrens
8038ad4d6dJeff Bonwick	for (int c = 0; c < vd->vdev_children; c++)
804fa9e406ahrens		dump_dtl(vd->vdev_child[c], indent + 4);
805fa9e406ahrens}
806fa9e406ahrens
8078f18d1fGeorge Wilsonstatic void
8088f18d1fGeorge Wilsondump_history(spa_t *spa)
8098f18d1fGeorge Wilson{
8108f18d1fGeorge Wilson	nvlist_t **events = NULL;
8118f18d1fGeorge Wilson	char buf[SPA_MAXBLOCKSIZE];
812e4161dfVictor Latushkin	uint64_t resid, len, off = 0;
8138f18d1fGeorge Wilson	uint_t num = 0;
8148f18d1fGeorge Wilson	int error;
8158f18d1fGeorge Wilson	time_t tsec;
8168f18d1fGeorge Wilson	struct tm t;
8178f18d1fGeorge Wilson	char tbuf[30];
8188f18d1fGeorge Wilson	char internalstr[MAXPATHLEN];
8198f18d1fGeorge Wilson
8208f18d1fGeorge Wilson	do {
821e4161dfVictor Latushkin		len = sizeof (buf);
822e4161dfVictor Latushkin
8238f18d1fGeorge Wilson		if ((error = spa_history_get(spa, &off, &len, buf)) != 0) {
8248f18d1fGeorge Wilson			(void) fprintf(stderr, "Unable to read history: "
8258f18d1fGeorge Wilson			    "error %d\n", error);
8268f18d1fGeorge Wilson			return;
8278f18d1fGeorge Wilson		}
8288f18d1fGeorge Wilson
8298f18d1fGeorge Wilson		if (zpool_history_unpack(buf, len, &resid, &events, &num) != 0)
8308f18d1fGeorge Wilson			break;
8318f18d1fGeorge Wilson
8328f18d1fGeorge Wilson		off -= resid;
8338f18d1fGeorge Wilson	} while (len != 0);
8348f18d1fGeorge Wilson
8358f18d1fGeorge Wilson	(void) printf("\nHistory:\n");
8368f18d1fGeorge Wilson	for (int i = 0; i < num; i++) {
8378f18d1fGeorge Wilson		uint64_t time, txg, ievent;
8388f18d1fGeorge Wilson		char *cmd, *intstr;
8398f18d1fGeorge Wilson
8408f18d1fGeorge Wilson		if (nvlist_lookup_uint64(events[i], ZPOOL_HIST_TIME,
8418f18d1fGeorge Wilson		    &time) != 0)
8428f18d1fGeorge Wilson			continue;
8438f18d1fGeorge Wilson		if (nvlist_lookup_string(events[i], ZPOOL_HIST_CMD,
8448f18d1fGeorge Wilson		    &cmd) != 0) {
8458f18d1fGeorge Wilson			if (nvlist_lookup_uint64(events[i],
8468f18d1fGeorge Wilson			    ZPOOL_HIST_INT_EVENT, &ievent) != 0)
8478f18d1fGeorge Wilson				continue;
8488f18d1fGeorge Wilson			verify(nvlist_lookup_uint64(events[i],
8498f18d1fGeorge Wilson			    ZPOOL_HIST_TXG, &txg) == 0);
8508f18d1fGeorge Wilson			verify(nvlist_lookup_string(events[i],
8518f18d1fGeorge Wilson			    ZPOOL_HIST_INT_STR, &intstr) == 0);
8528f18d1fGeorge Wilson			if (ievent >= LOG_END)
8538f18d1fGeorge Wilson				continue;
8548f18d1fGeorge Wilson
8558f18d1fGeorge Wilson			(void) snprintf(internalstr,
8568f18d1fGeorge Wilson			    sizeof (internalstr),
8578f18d1fGeorge Wilson			    "[internal %s txg:%lld] %s",
8588f18d1fGeorge Wilson			    hist_event_table[ievent], txg,
8598f18d1fGeorge Wilson			    intstr);
8608f18d1fGeorge Wilson			cmd = internalstr;
8618f18d1fGeorge Wilson		}
8628f18d1fGeorge Wilson		tsec = time;
8638f18d1fGeorge Wilson		(void) localtime_r(&tsec, &t);
8648f18d1fGeorge Wilson		(void) strftime(tbuf, sizeof (tbuf), "%F.%T", &t);
8658f18d1fGeorge Wilson		(void) printf("%s %s\n", tbuf, cmd);
8668f18d1fGeorge Wilson	}
8678f18d1fGeorge Wilson}
8688f18d1fGeorge Wilson
869fa9e406ahrens/*ARGSUSED*/
870fa9e406ahrensstatic void
871fa9e406ahrensdump_dnode(objset_t *os, uint64_t object, void *data, size_t size)
872fa9e406ahrens{
873fa9e406ahrens}
874fa9e406ahrens
875fa9e406ahrensstatic uint64_t
876b24ab67Jeff Bonwickblkid2offset(const dnode_phys_t *dnp, const blkptr_t *bp, const zbookmark_t *zb)
877fa9e406ahrens{
878b24ab67Jeff Bonwick	if (dnp == NULL) {
879b24ab67Jeff Bonwick		ASSERT(zb->zb_level < 0);
880b24ab67Jeff Bonwick		if (zb->zb_object == 0)
881b24ab67Jeff Bonwick			return (zb->zb_blkid);
882b24ab67Jeff Bonwick		return (zb->zb_blkid * BP_GET_LSIZE(bp));
883b24ab67Jeff Bonwick	}
884b24ab67Jeff Bonwick
885b24ab67Jeff Bonwick	ASSERT(zb->zb_level >= 0);
886fa9e406ahrens
887b24ab67Jeff Bonwick	return ((zb->zb_blkid <<
888b24ab67Jeff Bonwick	    (zb->zb_level * (dnp->dn_indblkshift - SPA_BLKPTRSHIFT))) *
889fa9e406ahrens	    dnp->dn_datablkszsec << SPA_MINBLOCKSHIFT);
890fa9e406ahrens}
891fa9e406ahrens
89244cd46cbillmstatic void
893b24ab67Jeff Bonwicksprintf_blkptr_compact(char *blkbuf, blkptr_t *bp)
89444cd46cbillm{
89544cd46cbillm	dva_t *dva = bp->blk_dva;
896b24ab67Jeff Bonwick	int ndvas = dump_opt['d'] > 5 ? BP_GET_NDVAS(bp) : 1;
897b24ab67Jeff Bonwick
898b24ab67Jeff Bonwick	if (dump_opt['b'] >= 5) {
899b24ab67Jeff Bonwick		sprintf_blkptr(blkbuf, bp);
900b24ab67Jeff Bonwick		return;
901b24ab67Jeff Bonwick	}
90244cd46cbillm
90344cd46cbillm	blkbuf[0] = '\0';
90444cd46cbillm
905b24ab67Jeff Bonwick	for (int i = 0; i < ndvas; i++)
90644cd46cbillm		(void) sprintf(blkbuf + strlen(blkbuf), "%llu:%llx:%llx ",
90744cd46cbillm		    (u_longlong_t)DVA_GET_VDEV(&dva[i]),
90844cd46cbillm		    (u_longlong_t)DVA_GET_OFFSET(&dva[i]),
90944cd46cbillm		    (u_longlong_t)DVA_GET_ASIZE(&dva[i]));
91044cd46cbillm
911b24ab67Jeff Bonwick	(void) sprintf(blkbuf + strlen(blkbuf),
912b24ab67Jeff Bonwick	    "%llxL/%llxP F=%llu B=%llu/%llu",
91344cd46cbillm	    (u_longlong_t)BP_GET_LSIZE(bp),
91444cd46cbillm	    (u_longlong_t)BP_GET_PSIZE(bp),
91544cd46cbillm	    (u_longlong_t)bp->blk_fill,
916b24ab67Jeff Bonwick	    (u_longlong_t)bp->blk_birth,
917b24ab67Jeff Bonwick	    (u_longlong_t)BP_PHYSICAL_BIRTH(bp));
91844cd46cbillm}
91944cd46cbillm
92088b7b0fMatthew Ahrensstatic void
92188b7b0fMatthew Ahrensprint_indirect(blkptr_t *bp, const zbookmark_t *zb,
92288b7b0fMatthew Ahrens    const dnode_phys_t *dnp)
923fa9e406ahrens{
92488b7b0fMatthew Ahrens	char blkbuf[BP_SPRINTF_LEN];
925fa9e406ahrens	int l;
926fa9e406ahrens
92788b7b0fMatthew Ahrens	ASSERT3U(BP_GET_TYPE(bp), ==, dnp->dn_type);
92888b7b0fMatthew Ahrens	ASSERT3U(BP_GET_LEVEL(bp), ==, zb->zb_level);
929fa9e406ahrens
930b24ab67Jeff Bonwick	(void) printf("%16llx ", (u_longlong_t)blkid2offset(dnp, bp, zb));
931fa9e406ahrens
932fa9e406ahrens	ASSERT(zb->zb_level >= 0);
933fa9e406ahrens
934fa9e406ahrens	for (l = dnp->dn_nlevels - 1; l >= -1; l--) {
935fa9e406ahrens		if (l == zb->zb_level) {
93688b7b0fMatthew Ahrens			(void) printf("L%llx", (u_longlong_t)zb->zb_level);
937fa9e406ahrens		} else {
93888b7b0fMatthew Ahrens			(void) printf(" ");
939fa9e406ahrens		}
940fa9e406ahrens	}
941fa9e406ahrens
942b24ab67Jeff Bonwick	sprintf_blkptr_compact(blkbuf, bp);
94388b7b0fMatthew Ahrens	(void) printf("%s\n", blkbuf);
94488b7b0fMatthew Ahrens}
94588b7b0fMatthew Ahrens
94688b7b0fMatthew Ahrensstatic int
94788b7b0fMatthew Ahrensvisit_indirect(spa_t *spa, const dnode_phys_t *dnp,
94888b7b0fMatthew Ahrens    blkptr_t *bp, const zbookmark_t *zb)
94988b7b0fMatthew Ahrens{
950e4161dfVictor Latushkin	int err = 0;
95188b7b0fMatthew Ahrens
95288b7b0fMatthew Ahrens	if (bp->blk_birth == 0)
95388b7b0fMatthew Ahrens		return (0);
95488b7b0fMatthew Ahrens
95588b7b0fMatthew Ahrens	print_indirect(bp, zb, dnp);
95688b7b0fMatthew Ahrens
95788b7b0fMatthew Ahrens	if (BP_GET_LEVEL(bp) > 0) {
95888b7b0fMatthew Ahrens		uint32_t flags = ARC_WAIT;
95988b7b0fMatthew Ahrens		int i;
96088b7b0fMatthew Ahrens		blkptr_t *cbp;
96188b7b0fMatthew Ahrens		int epb = BP_GET_LSIZE(bp) >> SPA_BLKPTRSHIFT;
96288b7b0fMatthew Ahrens		arc_buf_t *buf;
96388b7b0fMatthew Ahrens		uint64_t fill = 0;
96488b7b0fMatthew Ahrens
96588b7b0fMatthew Ahrens		err = arc_read_nolock(NULL, spa, bp, arc_getbuf_func, &buf,
96688b7b0fMatthew Ahrens		    ZIO_PRIORITY_ASYNC_READ, ZIO_FLAG_CANFAIL, &flags, zb);
96788b7b0fMatthew Ahrens		if (err)
96888b7b0fMatthew Ahrens			return (err);
96988b7b0fMatthew Ahrens
97088b7b0fMatthew Ahrens		/* recursively visit blocks below this */
97188b7b0fMatthew Ahrens		cbp = buf->b_data;
97288b7b0fMatthew Ahrens		for (i = 0; i < epb; i++, cbp++) {
97388b7b0fMatthew Ahrens			zbookmark_t czb;
97488b7b0fMatthew Ahrens
97588b7b0fMatthew Ahrens			SET_BOOKMARK(&czb, zb->zb_objset, zb->zb_object,
97688b7b0fMatthew Ahrens			    zb->zb_level - 1,
97788b7b0fMatthew Ahrens			    zb->zb_blkid * epb + i);
97888b7b0fMatthew Ahrens			err = visit_indirect(spa, dnp, cbp, &czb);
97988b7b0fMatthew Ahrens			if (err)
98088b7b0fMatthew Ahrens				break;
98188b7b0fMatthew Ahrens			fill += cbp->blk_fill;
98288b7b0fMatthew Ahrens		}
9838ad4d6dJeff Bonwick		if (!err)
9848ad4d6dJeff Bonwick			ASSERT3U(fill, ==, bp->blk_fill);
98588b7b0fMatthew Ahrens		(void) arc_buf_remove_ref(buf, &buf);
986fa9e406ahrens	}
987fa9e406ahrens
98888b7b0fMatthew Ahrens	return (err);
989fa9e406ahrens}
990fa9e406ahrens
991fa9e406ahrens/*ARGSUSED*/
992fa9e406ahrensstatic void
99388b7b0fMatthew Ahrensdump_indirect(dnode_t *dn)
994fa9e406ahrens{
99588b7b0fMatthew Ahrens	dnode_phys_t *dnp = dn->dn_phys;
99688b7b0fMatthew Ahrens	int j;
99788b7b0fMatthew Ahrens	zbookmark_t czb;
998fa9e406ahrens
999fa9e406ahrens	(void) printf("Indirect blocks:\n");
1000fa9e406ahrens
1001503ad85Matthew Ahrens	SET_BOOKMARK(&czb, dmu_objset_id(dn->dn_objset),
1002