zdb.c revision e690fb27a7d1483f052505e1ff373d205f9dee99
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
238f2529dMark Shellenbaum * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
24ad135b5Christopher Siden * Copyright (c) 2012 by Delphix. All rights reserved.
25fa9e406ahrens */
27fa9e406ahrens#include <stdio.h>
28004388ecasper#include <stdio_ext.h>
29fa9e406ahrens#include <stdlib.h>
3044cd46cbillm#include <ctype.h>
31fa9e406ahrens#include <sys/zfs_context.h>
32fa9e406ahrens#include <sys/spa.h>
33fa9e406ahrens#include <sys/spa_impl.h>
34fa9e406ahrens#include <sys/dmu.h>
35fa9e406ahrens#include <sys/zap.h>
36fa9e406ahrens#include <sys/fs/zfs.h>
37fa9e406ahrens#include <sys/zfs_znode.h>
380a586ceMark Shellenbaum#include <sys/zfs_sa.h>
390a586ceMark Shellenbaum#include <sys/sa.h>
400a586ceMark Shellenbaum#include <sys/sa_impl.h>
41fa9e406ahrens#include <sys/vdev.h>
42fa9e406ahrens#include <sys/vdev_impl.h>
43fa9e406ahrens#include <sys/metaslab_impl.h>
44fa9e406ahrens#include <sys/dmu_objset.h>
45fa9e406ahrens#include <sys/dsl_dir.h>
46fa9e406ahrens#include <sys/dsl_dataset.h>
47fa9e406ahrens#include <sys/dsl_pool.h>
48fa9e406ahrens#include <sys/dbuf.h>
49fa9e406ahrens#include <sys/zil.h>
50fa9e406ahrens#include <sys/zil_impl.h>
51fa9e406ahrens#include <sys/stat.h>
52fa9e406ahrens#include <sys/resource.h>
53fa9e406ahrens#include <sys/dmu_traverse.h>
54fa9e406ahrens#include <sys/zio_checksum.h>
55fa9e406ahrens#include <sys/zio_compress.h>
56e0d35c4marks#include <sys/zfs_fuid.h>
5788b7b0fMatthew Ahrens#include <sys/arc.h>
58b24ab67Jeff Bonwick#include <sys/ddt.h>
59ad135b5Christopher Siden#include <sys/zfeature.h>
604445fffMatthew Ahrens#include <zfs_comutil.h>
61de6628fck#undef ZFS_MAXNAMELEN
62de6628fck#undef verify
63de6628fck#include <libzfs.h>
65e690fb2Christopher Siden#define	ZDB_COMPRESS_NAME(idx) ((idx) < ZIO_COMPRESS_FUNCTIONS ?	\
66e690fb2Christopher Siden	zio_compress_table[(idx)].ci_name : "UNKNOWN")
67e690fb2Christopher Siden#define	ZDB_CHECKSUM_NAME(idx) ((idx) < ZIO_CHECKSUM_FUNCTIONS ?	\
68e690fb2Christopher Siden	zio_checksum_table[(idx)].ci_name : "UNKNOWN")
69e690fb2Christopher Siden#define	ZDB_OT_NAME(idx) ((idx) < DMU_OT_NUMTYPES ?	\
70e690fb2Christopher Siden	dmu_ot[(idx)].ot_name : DMU_OT_IS_VALID(idx) ?	\
71e690fb2Christopher Siden	dmu_ot_byteswap[DMU_OT_BYTESWAP(idx)].ob_name : "UNKNOWN")
72e690fb2Christopher Siden#define	ZDB_OT_TYPE(idx) ((idx) < DMU_OT_NUMTYPES ? (idx) :		\
73e690fb2Christopher Siden	(((idx) == DMU_OTN_ZAP_DATA || (idx) == DMU_OTN_ZAP_METADATA) ?	\
74e690fb2Christopher Siden	DMU_OT_ZAP_OTHER : DMU_OT_NUMTYPES))
756de8f41Victor Latushkin
76feef89cVictor Latushkin#ifndef lint
77feef89cVictor Latushkinextern int zfs_recover;
78feef89cVictor Latushkin#else
79feef89cVictor Latushkinint zfs_recover;
80feef89cVictor Latushkin#endif
81feef89cVictor Latushkin
82fa9e406ahrensconst char cmdname[] = "zdb";
83fa9e406ahrensuint8_t dump_opt[256];
85fa9e406ahrenstypedef void object_viewer_t(objset_t *, uint64_t, void *data, size_t size);
87fa9e406ahrensextern void dump_intent_log(zilog_t *);
88fa9e406ahrensuint64_t *zopt_object = NULL;
89fa9e406ahrensint zopt_objects = 0;
90de6628fcklibzfs_handle_t *g_zfs;
9131d7e8fGeorge Wilsonuint64_t max_inflight = 200;
94fa9e406ahrens * These libumem hooks provide a reasonable set of defaults for the allocator's
95fa9e406ahrens * debugging facilities.
96fa9e406ahrens */
97fa9e406ahrensconst char *
100fa9e406ahrens	return ("default,verbose"); /* $UMEM_DEBUG setting */
103fa9e406ahrensconst char *
106fa9e406ahrens	return ("fail,contents"); /* $UMEM_LOGGING setting */
109fa9e406ahrensstatic void
112fa9e406ahrens	(void) fprintf(stderr,
11390e894eRichard Lowe	    "Usage: %s [-CumdibcsDvhLXFPA] [-t txg] [-e [-p path...]] "
11431d7e8fGeorge Wilson	    "[-U config] [-M inflight I/Os] poolname [object...]\n"
11531d7e8fGeorge Wilson	    "       %s [-divPA] [-e -p path...] [-U config] dataset "
11631d7e8fGeorge Wilson	    "[object...]\n"
11731d7e8fGeorge Wilson	    "       %s -m [-LXFPA] [-t txg] [-e [-p path...]] [-U config] "
11890e894eRichard Lowe	    "poolname [vdev [metaslab...]]\n"
11990e894eRichard Lowe	    "       %s -R [-A] [-e [-p path...]] poolname "
12090e894eRichard Lowe	    "vdev:offset:size[:flags]\n"
12131d7e8fGeorge Wilson	    "       %s -S [-PA] [-e [-p path...]] [-U config] poolname\n"
12290e894eRichard Lowe	    "       %s -l [-uA] device\n"
12390e894eRichard Lowe	    "       %s -C [-A] [-U config]\n\n",
124bbfd46cJeff Bonwick	    cmdname, cmdname, cmdname, cmdname, cmdname, cmdname, cmdname);
1253ad6c7fVictor Latushkin
1263ad6c7fVictor Latushkin	(void) fprintf(stderr, "    Dataset name must include at least one "
1273ad6c7fVictor Latushkin	    "separator character '/' or '@'\n");
1283ad6c7fVictor Latushkin	(void) fprintf(stderr, "    If dataset name is specified, only that "
1293ad6c7fVictor Latushkin	    "dataset is dumped\n");
1303ad6c7fVictor Latushkin	(void) fprintf(stderr, "    If object numbers are specified, only "
1313ad6c7fVictor Latushkin	    "those objects are dumped\n\n");
1323ad6c7fVictor Latushkin	(void) fprintf(stderr, "    Options to control amount of output:\n");
1333ad6c7fVictor Latushkin	(void) fprintf(stderr, "        -u uberblock\n");
1343ad6c7fVictor Latushkin	(void) fprintf(stderr, "        -d dataset(s)\n");
1353ad6c7fVictor Latushkin	(void) fprintf(stderr, "        -i intent logs\n");
13607428bdVictor Latushkin	(void) fprintf(stderr, "        -C config (or cachefile if alone)\n");
1378f18d1fGeorge Wilson	(void) fprintf(stderr, "        -h pool history\n");
1383ad6c7fVictor Latushkin	(void) fprintf(stderr, "        -b block statistics\n");
1393ad6c7fVictor Latushkin	(void) fprintf(stderr, "        -m metaslabs\n");
1403ad6c7fVictor Latushkin	(void) fprintf(stderr, "        -c checksum all metadata (twice for "
1416365109Victor Latushkin	    "all data) blocks\n");
1423ad6c7fVictor Latushkin	(void) fprintf(stderr, "        -s report stats on zdb's I/O\n");
143f0ba89bJeff Bonwick	(void) fprintf(stderr, "        -D dedup statistics\n");
144b24ab67Jeff Bonwick	(void) fprintf(stderr, "        -S simulate dedup to measure effect\n");
1453ad6c7fVictor Latushkin	(void) fprintf(stderr, "        -v verbose (applies to all others)\n");
146fa9e406ahrens	(void) fprintf(stderr, "        -l dump label contents\n");
14782a0a98Victor Latushkin	(void) fprintf(stderr, "        -L disable leak tracking (do not "
14882a0a98Victor Latushkin	    "load spacemaps)\n");
149d41e764ek	(void) fprintf(stderr, "        -R read and display block from a "
1503ad6c7fVictor Latushkin	    "device\n\n");
1513ad6c7fVictor Latushkin	(void) fprintf(stderr, "    Below options are intended for use "
1523ad6c7fVictor Latushkin	    "with other options (except -l):\n");
153feef89cVictor Latushkin	(void) fprintf(stderr, "        -A ignore assertions (-A), enable "
154feef89cVictor Latushkin	    "panic recovery (-AA) or both (-AAA)\n");
155c8ee184Victor Latushkin	(void) fprintf(stderr, "        -F attempt automatic rewind within "
156c8ee184Victor Latushkin	    "safe range of transaction groups\n");
1573ad6c7fVictor Latushkin	(void) fprintf(stderr, "        -U <cachefile_path> -- use alternate "
1583ad6c7fVictor Latushkin	    "cachefile\n");
159c8ee184Victor Latushkin	(void) fprintf(stderr, "        -X attempt extreme rewind (does not "
160c8ee184Victor Latushkin	    "work with dataset)\n");
1613ad6c7fVictor Latushkin	(void) fprintf(stderr, "        -e pool is exported/destroyed/"
1623ad6c7fVictor Latushkin	    "has altroot/not in a cachefile\n");
1633ad6c7fVictor Latushkin	(void) fprintf(stderr, "        -p <path> -- use one or more with "
1643ad6c7fVictor Latushkin	    "-e to specify path to vdev dir\n");
16590e894eRichard Lowe	(void) fprintf(stderr, "        -P print numbers in parseable form\n");
1663ad6c7fVictor Latushkin	(void) fprintf(stderr, "        -t <txg> -- highest txg to use when "
1672e55192Victor Latushkin	    "searching for uberblocks\n");
16831d7e8fGeorge Wilson	(void) fprintf(stderr, "        -M <number of inflight I/Os> -- "
16931d7e8fGeorge Wilson	    "specify the maximum number of checksumming I/Os [default is 200]");
170fa9e406ahrens	(void) fprintf(stderr, "Specify an option more than once (e.g. -bb) "
171fa9e406ahrens	    "to make only that option verbose\n");
172fa9e406ahrens	(void) fprintf(stderr, "Default is to dump everything non-verbosely\n");
173fa9e406ahrens	exit(1);
176ccba080Rich Morris/*
177ccba080Rich Morris * Called for usage errors that are discovered after a call to spa_open(),
178ccba080Rich Morris * dmu_bonus_hold(), or pool_match().  abort() is called for other errors.
179ccba080Rich Morris */
180ccba080Rich Morris
181fa9e406ahrensstatic void
182fa9e406ahrensfatal(const char *fmt, ...)
184fa9e406ahrens	va_list ap;
186fa9e406ahrens	va_start(ap, fmt);
187fa9e406ahrens	(void) fprintf(stderr, "%s: ", cmdname);
188fa9e406ahrens	(void) vfprintf(stderr, fmt, ap);
189fa9e406ahrens	va_end(ap);
190fa9e406ahrens	(void) fprintf(stderr, "\n");
192ccba080Rich Morris	exit(1);
195fa9e406ahrens/* ARGSUSED */
196fa9e406ahrensstatic void
197fa9e406ahrensdump_packed_nvlist(objset_t *os, uint64_t object, void *data, size_t size)
199fa9e406ahrens	nvlist_t *nv;
200fa9e406ahrens	size_t nvsize = *(uint64_t *)data;
201fa9e406ahrens	char *packed = umem_alloc(nvsize, UMEM_NOFAIL);
2037bfdf01Neil Perrin	VERIFY(0 == dmu_read(os, object, 0, nvsize, packed, DMU_READ_PREFETCH));
205fa9e406ahrens	VERIFY(nvlist_unpack(packed, nvsize, &nv, 0) == 0);
207fa9e406ahrens	umem_free(packed, nvsize);
209fa9e406ahrens	dump_nvlist(nv, 8);
211fa9e406ahrens	nvlist_free(nv);
2144445fffMatthew Ahrens/* ARGSUSED */
2154445fffMatthew Ahrensstatic void
2164445fffMatthew Ahrensdump_history_offsets(objset_t *os, uint64_t object, void *data, size_t size)
2174445fffMatthew Ahrens{
2184445fffMatthew Ahrens	spa_history_phys_t *shp = data;
2194445fffMatthew Ahrens
2204445fffMatthew Ahrens	if (shp == NULL)
2214445fffMatthew Ahrens		return;
2224445fffMatthew Ahrens
2234445fffMatthew Ahrens	(void) printf("\t\tpool_create_len = %llu\n",
2244445fffMatthew Ahrens	    (u_longlong_t)shp->sh_pool_create_len);
2254445fffMatthew Ahrens	(void) printf("\t\tphys_max_off = %llu\n",
2264445fffMatthew Ahrens	    (u_longlong_t)shp->sh_phys_max_off);
2274445fffMatthew Ahrens	(void) printf("\t\tbof = %llu\n",
2284445fffMatthew Ahrens	    (u_longlong_t)shp->sh_bof);
2294445fffMatthew Ahrens	(void) printf("\t\teof = %llu\n",
2304445fffMatthew Ahrens	    (u_longlong_t)shp->sh_eof);
2314445fffMatthew Ahrens	(void) printf("\t\trecords_lost = %llu\n",
2324445fffMatthew Ahrens	    (u_longlong_t)shp->sh_records_lost);
2334445fffMatthew Ahrens}
2344445fffMatthew Ahrens
2353f9d6adLin Lingstatic void
2363f9d6adLin Lingzdb_nicenum(uint64_t num, char *buf)
2373f9d6adLin Ling{
2383f9d6adLin Ling	if (dump_opt['P'])
2393f9d6adLin Ling		(void) sprintf(buf, "%llu", (longlong_t)num);
2403f9d6adLin Ling	else
2413f9d6adLin Ling		nicenum(num, buf);
2423f9d6adLin Ling}
2433f9d6adLin Ling
244fa9e406ahrensconst char dump_zap_stars[] = "****************************************";
245fa9e406ahrensconst int dump_zap_width = sizeof (dump_zap_stars) - 1;
247fa9e406ahrensstatic void
248fa9e406ahrensdump_zap_histogram(uint64_t histo[ZAP_HISTOGRAM_SIZE])
250fa9e406ahrens	int i;
251fa9e406ahrens	int minidx = ZAP_HISTOGRAM_SIZE - 1;
252fa9e406ahrens	int maxidx = 0;
253fa9e406ahrens	uint64_t max = 0;
255fa9e406ahrens	for (i = 0; i < ZAP_HISTOGRAM_SIZE; i++) {
256fa9e406ahrens		if (histo[i] > max)
257fa9e406ahrens			max = histo[i];
258fa9e406ahrens		if (histo[i] > 0 && i > maxidx)
259fa9e406ahrens			maxidx = i;
260fa9e406ahrens		if (histo[i] > 0 && i < minidx)
261fa9e406ahrens			minidx = i;
262fa9e406ahrens	}
264fa9e406ahrens	if (max < dump_zap_width)
265fa9e406ahrens		max = dump_zap_width;
267fa9e406ahrens	for (i = minidx; i <= maxidx; i++)
268fa9e406ahrens		(void) printf("\t\t\t%u: %6llu %s\n", i, (u_longlong_t)histo[i],
269fa9e406ahrens		    &dump_zap_stars[(max - histo[i]) * dump_zap_width / max]);
272fa9e406ahrensstatic void
273fa9e406ahrensdump_zap_stats(objset_t *os, uint64_t object)
275fa9e406ahrens	int error;
276fa9e406ahrens	zap_stats_t zs;
278fa9e406ahrens	error = zap_get_stats(os, object, &zs);
279fa9e406ahrens	if (error)
280fa9e406ahrens		return;
282fa9e406ahrens	if (zs.zs_ptrtbl_len == 0) {
283fa9e406ahrens		ASSERT(zs.zs_num_blocks == 1);
284fa9e406ahrens		(void) printf("\tmicrozap: %llu bytes, %llu entries\n",
285fa9e406ahrens		    (u_longlong_t)zs.zs_blocksize,
286fa9e406ahrens		    (u_longlong_t)zs.zs_num_entries);
287fa9e406ahrens		return;
288fa9e406ahrens	}
290fa9e406ahrens	(void) printf("\tFat ZAP stats:\n");
2928248818nd	(void) printf("\t\tPointer table:\n");
2938248818nd	(void) printf("\t\t\t%llu elements\n",
294fa9e406ahrens	    (u_longlong_t)zs.zs_ptrtbl_len);
2958248818nd	(void) printf("\t\t\tzt_blk: %llu\n",
2968248818nd	    (u_longlong_t)zs.zs_ptrtbl_zt_blk);
2978248818nd	(void) printf("\t\t\tzt_numblks: %llu\n",
2988248818nd	    (u_longlong_t)zs.zs_ptrtbl_zt_numblks);
2998248818nd	(void) printf("\t\t\tzt_shift: %llu\n",
3008248818nd	    (u_longlong_t)zs.zs_ptrtbl_zt_shift);
3018248818nd	(void) printf("\t\t\tzt_blks_copied: %llu\n",
3028248818nd	    (u_longlong_t)zs.zs_ptrtbl_blks_copied);
3038248818nd	(void) printf("\t\t\tzt_nextblk: %llu\n",
3048248818nd	    (u_longlong_t)zs.zs_ptrtbl_nextblk);
306fa9e406ahrens	(void) printf("\t\tZAP entries: %llu\n",
307fa9e406ahrens	    (u_longlong_t)zs.zs_num_entries);
308fa9e406ahrens	(void) printf("\t\tLeaf blocks: %llu\n",
309fa9e406ahrens	    (u_longlong_t)zs.zs_num_leafs);
310fa9e406ahrens	(void) printf("\t\tTotal blocks: %llu\n",
311fa9e406ahrens	    (u_longlong_t)zs.zs_num_blocks);
3128248818nd	(void) printf("\t\tzap_block_type: 0x%llx\n",
3138248818nd	    (u_longlong_t)zs.zs_block_type);
3148248818nd	(void) printf("\t\tzap_magic: 0x%llx\n",
3158248818nd	    (u_longlong_t)zs.zs_magic);
3168248818nd	(void) printf("\t\tzap_salt: 0x%llx\n",
3178248818nd	    (u_longlong_t)zs.zs_salt);
319fa9e406ahrens	(void) printf("\t\tLeafs with 2^n pointers:\n");
320fa9e406ahrens	dump_zap_histogram(zs.zs_leafs_with_2n_pointers);
322fa9e406ahrens	(void) printf("\t\tBlocks with n*5 entries:\n");
323fa9e406ahrens	dump_zap_histogram(zs.zs_blocks_with_n5_entries);
325fa9e406ahrens	(void) printf("\t\tBlocks n/10 full:\n");
326fa9e406ahrens	dump_zap_histogram(zs.zs_blocks_n_tenths_full);
328fa9e406ahrens	(void) printf("\t\tEntries with n chunks:\n");
329fa9e406ahrens	dump_zap_histogram(zs.zs_entries_using_n_chunks);
331fa9e406ahrens	(void) printf("\t\tBuckets with n entries:\n");
332fa9e406ahrens	dump_zap_histogram(zs.zs_buckets_with_n_entries);
336fa9e406ahrensstatic void
337fa9e406ahrensdump_none(objset_t *os, uint64_t object, void *data, size_t size)
3426de8f41Victor Latushkinstatic void
3436de8f41Victor Latushkindump_unknown(objset_t *os, uint64_t object, void *data, size_t size)
3446de8f41Victor Latushkin{
3456de8f41Victor Latushkin	(void) printf("\tUNKNOWN OBJECT TYPE\n");
3466de8f41Victor Latushkin}
3476de8f41Victor Latushkin
3486de8f41Victor Latushkin/*ARGSUSED*/
350fa9e406ahrensdump_uint8(objset_t *os, uint64_t object, void *data, size_t size)
355fa9e406ahrensstatic void
356fa9e406ahrensdump_uint64(objset_t *os, uint64_t object, void *data, size_t size)
361fa9e406ahrensstatic void
362fa9e406ahrensdump_zap(objset_t *os, uint64_t object, void *data, size_t size)
364fa9e406ahrens	zap_cursor_t zc;
365fa9e406ahrens	zap_attribute_t attr;
366fa9e406ahrens	void *prop;
367fa9e406ahrens	int i;
369fa9e406ahrens	dump_zap_stats(os, object);
370fa9e406ahrens	(void) printf("\n");
372fa9e406ahrens	for (zap_cursor_init(&zc, os, object);
373fa9e406ahrens	    zap_cursor_retrieve(&zc, &attr) == 0;
374fa9e406ahrens	    zap_cursor_advance(&zc)) {
375fa9e406ahrens		(void) printf("\t\t%s = ", attr.za_name);
376fa9e406ahrens		if (attr.za_num_integers == 0) {
377fa9e406ahrens			(void) printf("\n");
378fa9e406ahrens			continue;
379fa9e406ahrens		}
380fa9e406ahrens		prop = umem_zalloc(attr.za_num_integers *
381fa9e406ahrens		    attr.za_integer_length, UMEM_NOFAIL);
382fa9e406ahrens		(void) zap_lookup(os, object, attr.za_name,
383fa9e406ahrens		    attr.za_integer_length, attr.za_num_integers, prop);
384fa9e406ahrens		if (attr.za_integer_length == 1) {
385fa9e406ahrens			(void) printf("%s", (char *)prop);
386fa9e406ahrens		} else {
387fa9e406ahrens			for (i = 0; i < attr.za_num_integers; i++) {
388fa9e406ahrens				switch (attr.za_integer_length) {
389fa9e406ahrens				case 2:
390fa9e406ahrens					(void) printf("%u ",
391fa9e406ahrens					    ((uint16_t *)prop)[i]);
392fa9e406ahrens					break;
393fa9e406ahrens				case 4:
394fa9e406ahrens					(void) printf("%u ",
395fa9e406ahrens					    ((uint32_t *)prop)[i]);
396fa9e406ahrens					break;
397fa9e406ahrens				case 8:
398fa9e406ahrens					(void) printf("%lld ",
399fa9e406ahrens					    (u_longlong_t)((int64_t *)prop)[i]);
400fa9e406ahrens					break;
401fa9e406ahrens				}
402fa9e406ahrens			}
403fa9e406ahrens		}
404fa9e406ahrens		(void) printf("\n");
405fa9e406ahrens		umem_free(prop, attr.za_num_integers * attr.za_integer_length);
406fa9e406ahrens	}
40787e5029ahrens	zap_cursor_fini(&zc);
411e743726ahrensstatic void
412486ae71Matthew Ahrensdump_ddt_zap(objset_t *os, uint64_t object, void *data, size_t size)
413486ae71Matthew Ahrens{
414486ae71Matthew Ahrens	dump_zap_stats(os, object);
415486ae71Matthew Ahrens	/* contents are printed elsewhere, properly decoded */
416486ae71Matthew Ahrens}
417486ae71Matthew Ahrens
418486ae71Matthew Ahrens/*ARGSUSED*/
419486ae71Matthew Ahrensstatic void
4200a586ceMark Shellenbaumdump_sa_attrs(objset_t *os, uint64_t object, void *data, size_t size)
4210a586ceMark Shellenbaum{
4220a586ceMark Shellenbaum	zap_cursor_t zc;
4230a586ceMark Shellenbaum	zap_attribute_t attr;
4240a586ceMark Shellenbaum
4250a586ceMark Shellenbaum	dump_zap_stats(os, object);
4260a586ceMark Shellenbaum	(void) printf("\n");
4270a586ceMark Shellenbaum
4280a586ceMark Shellenbaum	for (zap_cursor_init(&zc, os, object);
4290a586ceMark Shellenbaum	    zap_cursor_retrieve(&zc, &attr) == 0;
4300a586ceMark Shellenbaum	    zap_cursor_advance(&zc)) {
4310a586ceMark Shellenbaum		(void) printf("\t\t%s = ", attr.za_name);
4320a586ceMark Shellenbaum		if (attr.za_num_integers == 0) {
4330a586ceMark Shellenbaum			(void) printf("\n");
4340a586ceMark Shellenbaum			continue;
4350a586ceMark Shellenbaum		}
4360a586ceMark Shellenbaum		(void) printf(" %llx : [%d:%d:%d]\n",
4370a586ceMark Shellenbaum		    (u_longlong_t)attr.za_first_integer,
4380a586ceMark Shellenbaum		    (int)ATTR_LENGTH(attr.za_first_integer),
4390a586ceMark Shellenbaum		    (int)ATTR_BSWAP(attr.za_first_integer),
4400a586ceMark Shellenbaum		    (int)ATTR_NUM(attr.za_first_integer));
4410a586ceMark Shellenbaum	}
4420a586ceMark Shellenbaum	zap_cursor_fini(&zc);
4430a586ceMark Shellenbaum}
4440a586ceMark Shellenbaum
4450a586ceMark Shellenbaum/*ARGSUSED*/
4460a586ceMark Shellenbaumstatic void
4470a586ceMark Shellenbaumdump_sa_layouts(objset_t *os, uint64_t object, void *data, size_t size)
4480a586ceMark Shellenbaum{
4490a586ceMark Shellenbaum	zap_cursor_t zc;
4500a586ceMark Shellenbaum	zap_attribute_t attr;
4510a586ceMark Shellenbaum	uint16_t *layout_attrs;
4520a586ceMark Shellenbaum	int i;
4530a586ceMark Shellenbaum
4540a586ceMark Shellenbaum	dump_zap_stats(os, object);
4550a586ceMark Shellenbaum	(void) printf("\n");
4560a586ceMark Shellenbaum
4570a586ceMark Shellenbaum	for (zap_cursor_init(&zc, os, object);
4580a586ceMark Shellenbaum	    zap_cursor_retrieve(&zc, &attr) == 0;
4590a586ceMark Shellenbaum	    zap_cursor_advance(&zc)) {
4600a586ceMark Shellenbaum		(void) printf("\t\t%s = [", attr.za_name);
4610a586ceMark Shellenbaum		if (attr.za_num_integers == 0) {
4620a586ceMark Shellenbaum			(void) printf("\n");
4630a586ceMark Shellenbaum			continue;
4640a586ceMark Shellenbaum		}
4650a586ceMark Shellenbaum
4660a586ceMark Shellenbaum		VERIFY(attr.za_integer_length == 2);
4670a586ceMark Shellenbaum		layout_attrs = umem_zalloc(attr.za_num_integers *
4680a586ceMark Shellenbaum		    attr.za_integer_length, UMEM_NOFAIL);
4690a586ceMark Shellenbaum
4700a586ceMark Shellenbaum		VERIFY(zap_lookup(os, object, attr.za_name,
4710a586ceMark Shellenbaum		    attr.za_integer_length,
4720a586ceMark Shellenbaum		    attr.za_num_integers, layout_attrs) == 0);
4730a586ceMark Shellenbaum
4740a586ceMark Shellenbaum		for (i = 0; i != attr.za_num_integers; i++)
4750a586ceMark Shellenbaum			(void) printf(" %d ", (int)layout_attrs[i]);
4760a586ceMark Shellenbaum		(void) printf("]\n");
4770a586ceMark Shellenbaum		umem_free(layout_attrs,
4780a586ceMark Shellenbaum		    attr.za_num_integers * attr.za_integer_length);
4790a586ceMark Shellenbaum	}
4800a586ceMark Shellenbaum	zap_cursor_fini(&zc);
4810a586ceMark Shellenbaum}
4820a586ceMark Shellenbaum
4830a586ceMark Shellenbaum/*ARGSUSED*/
4840a586ceMark Shellenbaumstatic void
485e743726ahrensdump_zpldir(objset_t *os, uint64_t object, void *data, size_t size)
487e743726ahrens	zap_cursor_t zc;
488e743726ahrens	zap_attribute_t attr;
489e743726ahrens	const char *typenames[] = {
490e743726ahrens		/* 0 */ "not specified",
491e743726ahrens		/* 1 */ "FIFO",
492e743726ahrens		/* 2 */ "Character Device",
493e743726ahrens		/* 3 */ "3 (invalid)",
494e743726ahrens		/* 4 */ "Directory",
495e743726ahrens		/* 5 */ "5 (invalid)",
496e743726ahrens		/* 6 */ "Block Device",
497e743726ahrens		/* 7 */ "7 (invalid)",
498e743726ahrens		/* 8 */ "Regular File",
499e743726ahrens		/* 9 */ "9 (invalid)",
500e743726ahrens		/* 10 */ "Symbolic Link",
501e743726ahrens		/* 11 */ "11 (invalid)",
502e743726ahrens		/* 12 */ "Socket",
503e743726ahrens		/* 13 */ "Door",
504e743726ahrens		/* 14 */ "Event Port",
505e743726ahrens		/* 15 */ "15 (invalid)",
506e743726ahrens	};
508e743726ahrens	dump_zap_stats(os, object);
509e743726ahrens	(void) printf("\n");
511e743726ahrens	for (zap_cursor_init(&zc, os, object);
512e743726ahrens	    zap_cursor_retrieve(&zc, &attr) == 0;
513e743726ahrens	    zap_cursor_advance(&zc)) {
514e743726ahrens		(void) printf("\t\t%s = %lld (type: %s)\n",
515e743726ahrens		    attr.za_name, ZFS_DIRENT_OBJ(attr.za_first_integer),
516e743726ahrens		    typenames[ZFS_DIRENT_TYPE(attr.za_first_integer)]);
517e743726ahrens	}
518e743726ahrens	zap_cursor_fini(&zc);
521fa9e406ahrensstatic void
522fa9e406ahrensdump_spacemap(objset_t *os, space_map_obj_t *smo, space_map_t *sm)
524fa9e406ahrens	uint64_t alloc, offset, entry;
525ecc2d60bonwick	uint8_t mapshift = sm->sm_shift;
526fa9e406ahrens	uint64_t mapstart = sm->sm_start;
5278053a26ck	char *ddata[] = { "ALLOC", "FREE", "CONDENSE", "INVALID",
5288053a26ck			    "INVALID", "INVALID", "INVALID", "INVALID" };
530fa9e406ahrens	if (smo->smo_object == 0)
531fa9e406ahrens		return;
533fa9e406ahrens	/*
534fa9e406ahrens	 * Print out the freelist entries in both encoded and decoded form.
535fa9e406ahrens	 */
536fa9e406ahrens	alloc = 0;
537fa9e406ahrens	for (offset = 0; offset < smo->smo_objsize; offset += sizeof (entry)) {
538b420f3aRichard Lowe		VERIFY3U(0, ==, dmu_read(os, smo->smo_object, offset,
5397bfdf01Neil Perrin		    sizeof (entry), &entry, DMU_READ_PREFETCH));
540fa9e406ahrens		if (SM_DEBUG_DECODE(entry)) {
54187219dbVictor Latushkin			(void) printf("\t    [%6llu] %s: txg %llu, pass %llu\n",
542fa9e406ahrens			    (u_longlong_t)(offset / sizeof (entry)),
543fa9e406ahrens			    ddata[SM_DEBUG_ACTION_DECODE(entry)],
5445ad8204nd			    (u_longlong_t)SM_DEBUG_TXG_DECODE(entry),
5455ad8204nd			    (u_longlong_t)SM_DEBUG_SYNCPASS_DECODE(entry));
546fa9e406ahrens		} else {
54787219dbVictor Latushkin			(void) printf("\t    [%6llu]    %c  range:"
54887219dbVictor Latushkin			    " %010llx-%010llx  size: %06llx\n",
549fa9e406ahrens			    (u_longlong_t)(offset / sizeof (entry)),
550fa9e406ahrens			    SM_TYPE_DECODE(entry) == SM_ALLOC ? 'A' : 'F',
5515ad8204nd			    (u_longlong_t)((SM_OFFSET_DECODE(entry) <<
5525ad8204nd			    mapshift) + mapstart),
5535ad8204nd			    (u_longlong_t)((SM_OFFSET_DECODE(entry) <<
5545ad8204nd			    mapshift) + mapstart + (SM_RUN_DECODE(entry) <<
5555ad8204nd			    mapshift)),
5565ad8204nd			    (u_longlong_t)(SM_RUN_DECODE(entry) << mapshift));
557fa9e406ahrens			if (SM_TYPE_DECODE(entry) == SM_ALLOC)
558fa9e406ahrens				alloc += SM_RUN_DECODE(entry) << mapshift;
559fa9e406ahrens			else
560fa9e406ahrens				alloc -= SM_RUN_DECODE(entry) << mapshift;
561fa9e406ahrens		}
562fa9e406ahrens	}
563fa9e406ahrens	if (alloc != smo->smo_alloc) {
564fa9e406ahrens		(void) printf("space_map_object alloc (%llu) INCONSISTENT "
565fa9e406ahrens		    "with space map summary (%llu)\n",
566fa9e406ahrens		    (u_longlong_t)smo->smo_alloc, (u_longlong_t)alloc);
567fa9e406ahrens	}
570fa9e406ahrensstatic void
571d6e555bGeorge Wilsondump_metaslab_stats(metaslab_t *msp)
572d6e555bGeorge Wilson{
5733f9d6adLin Ling	char maxbuf[32];
574d6e555bGeorge Wilson	space_map_t *sm = &msp->ms_map;
575d6e555bGeorge Wilson	avl_tree_t *t = sm->sm_pp_root;
576d6e555bGeorge Wilson	int free_pct = sm->sm_space * 100 / sm->sm_size;
577d6e555bGeorge Wilson
5783f9d6adLin Ling	zdb_nicenum(space_map_maxsize(sm), maxbuf);
579d6e555bGeorge Wilson
58087219dbVictor Latushkin	(void) printf("\t %25s %10lu   %7s  %6s   %4s %4d%%\n",
581d6e555bGeorge Wilson	    "segments", avl_numnodes(t), "maxsize", maxbuf,
582d6e555bGeorge Wilson	    "freepct", free_pct);
583d6e555bGeorge Wilson}
584d6e555bGeorge Wilson
585d6e555bGeorge Wilsonstatic void
586fa9e406ahrensdump_metaslab(metaslab_t *msp)
588fa9e406ahrens	vdev_t *vd = msp->ms_group->mg_vd;
589fa9e406ahrens	spa_t *spa = vd->vdev_spa;
59080eb36fGeorge Wilson	space_map_t *sm = &msp->ms_map;
59180eb36fGeorge Wilson	space_map_obj_t *smo = &msp->ms_smo;
5923f9d6adLin Ling	char freebuf[32];
5943f9d6adLin Ling	zdb_nicenum(sm->sm_size - smo->smo_alloc, freebuf);
596fa9e406ahrens	(void) printf(
59787219dbVictor Latushkin	    "\tmetaslab %6llu   offset %12llx   spacemap %6llu   free    %5s\n",
59880eb36fGeorge Wilson	    (u_longlong_t)(sm->sm_start / sm->sm_size),
59980eb36fGeorge Wilson	    (u_longlong_t)sm->sm_start, (u_longlong_t)smo->smo_object, freebuf);
60187219dbVictor Latushkin	if (dump_opt['m'] > 1 && !dump_opt['L']) {
602d6e555bGeorge Wilson		mutex_enter(&msp->ms_lock);
60380eb36fGeorge Wilson		space_map_load_wait(sm);
604bc3975bVictor Latushkin		if (!sm->sm_loaded)
60580eb36fGeorge Wilson			VERIFY(space_map_load(sm, zfs_metaslab_ops,
60680eb36fGeorge Wilson			    SM_FREE, smo, spa->spa_meta_objset) == 0);
607bc3975bVictor Latushkin		dump_metaslab_stats(msp);
608bc3975bVictor Latushkin		space_map_unload(sm);
609d6e555bGeorge Wilson		mutex_exit(&msp->ms_lock);
610d6e555bGeorge Wilson	}
611d6e555bGeorge Wilson
612d6e555bGeorge Wilson	if (dump_opt['d'] > 5 || dump_opt['m'] > 2) {
61380eb36fGeorge Wilson		ASSERT(sm->sm_size == (1ULL << vd->vdev_ms_shift));
614d6e555bGeorge Wilson
615d6e555bGeorge Wilson		mutex_enter(&msp->ms_lock);
61680eb36fGeorge Wilson		dump_spacemap(spa->spa_meta_objset, smo, sm);
617d6e555bGeorge Wilson		mutex_exit(&msp->ms_lock);
618d6e555bGeorge Wilson	}
61987219dbVictor Latushkin}
62187219dbVictor Latushkinstatic void
62287219dbVictor Latushkinprint_vdev_metaslab_header(vdev_t *vd)
62387219dbVictor Latushkin{
62487219dbVictor Latushkin	(void) printf("\tvdev %10llu\n\t%-10s%5llu   %-19s   %-15s   %-10s\n",
62587219dbVictor Latushkin	    (u_longlong_t)vd->vdev_id,
62687219dbVictor Latushkin	    "metaslabs", (u_longlong_t)vd->vdev_ms_count,
62787219dbVictor Latushkin	    "offset", "spacemap", "free");
62887219dbVictor Latushkin	(void) printf("\t%15s   %19s   %15s   %10s\n",
62987219dbVictor Latushkin	    "---------------", "-------------------",
63087219dbVictor Latushkin	    "---------------", "-------------");
633fa9e406ahrensstatic void
634fa9e406ahrensdump_metaslabs(spa_t *spa)
63687219dbVictor Latushkin	vdev_t *vd, *rvd = spa->spa_root_vdev;
63787219dbVictor Latushkin	uint64_t m, c = 0, children = rvd->vdev_children;
639fa9e406ahrens	(void) printf("\nMetaslabs:\n");
64187219dbVictor Latushkin	if (!dump_opt['d'] && zopt_objects > 0) {
64287219dbVictor Latushkin		c = zopt_object[0];
64387219dbVictor Latushkin
64487219dbVictor Latushkin		if (c >= children)
64587219dbVictor Latushkin			(void) fatal("bad vdev id: %llu", (u_longlong_t)c);
64787219dbVictor Latushkin		if (zopt_objects > 1) {
64887219dbVictor Latushkin			vd = rvd->vdev_child[c];
64987219dbVictor Latushkin			print_vdev_metaslab_header(vd);
65087219dbVictor Latushkin
65187219dbVictor Latushkin			for (m = 1; m < zopt_objects; m++) {
65287219dbVictor Latushkin				if (zopt_object[m] < vd->vdev_ms_count)
65387219dbVictor Latushkin					dump_metaslab(
65487219dbVictor Latushkin					    vd->vdev_ms[zopt_object[m]]);
65587219dbVictor Latushkin				else
65687219dbVictor Latushkin					(void) fprintf(stderr, "bad metaslab "
65787219dbVictor Latushkin					    "number %llu\n",
65887219dbVictor Latushkin					    (u_longlong_t)zopt_object[m]);
65987219dbVictor Latushkin			}
66087219dbVictor Latushkin			(void) printf("\n");
66187219dbVictor Latushkin			return;
66287219dbVictor Latushkin		}
66387219dbVictor Latushkin		children = c + 1;
66487219dbVictor Latushkin	}
66587219dbVictor Latushkin	for (; c < children; c++) {
66687219dbVictor Latushkin		vd = rvd->vdev_child[c];
66787219dbVictor Latushkin		print_vdev_metaslab_header(vd);
669fa9e406ahrens		for (m = 0; m < vd->vdev_ms_count; m++)
670fa9e406ahrens			dump_metaslab(vd->vdev_ms[m]);
671fa9e406ahrens		(void) printf("\n");
672fa9e406ahrens	}
675fa9e406ahrensstatic void
676b24ab67Jeff Bonwickdump_dde(const ddt_t *ddt, const ddt_entry_t *dde, uint64_t index)
677b24ab67Jeff Bonwick{
678b24ab67Jeff Bonwick	const ddt_phys_t *ddp = dde->dde_phys;
679b24ab67Jeff Bonwick	const ddt_key_t *ddk = &dde->dde_key;
680b24ab67Jeff Bonwick	char *types[4] = { "ditto", "single", "double", "triple" };
681b24ab67Jeff Bonwick	char blkbuf[BP_SPRINTF_LEN];
682b24ab67Jeff Bonwick	blkptr_t blk;
683b24ab67Jeff Bonwick
684b24ab67Jeff Bonwick	for (int p = 0; p < DDT_PHYS_TYPES; p++, ddp++) {
685b24ab67Jeff Bonwick		if (ddp->ddp_phys_birth == 0)
686b24ab67Jeff Bonwick			continue;
687bbfd46cJeff Bonwick		ddt_bp_create(ddt->ddt_checksum, ddk, ddp, &blk);
688b24ab67Jeff Bonwick		sprintf_blkptr(blkbuf, &blk);
689b24ab67Jeff Bonwick		(void) printf("index %llx refcnt %llu %s %s\n",
690b24ab67Jeff Bonwick		    (u_longlong_t)index, (u_longlong_t)ddp->ddp_refcnt,
691b24ab67Jeff Bonwick		    types[p], blkbuf);
692b24ab67Jeff Bonwick	}
693b24ab67Jeff Bonwick}
694b24ab67Jeff Bonwick
695b24ab67Jeff Bonwickstatic void
696b24ab67Jeff Bonwickdump_dedup_ratio(const ddt_stat_t *dds)
697b24ab67Jeff Bonwick{
698b24ab67Jeff Bonwick	double rL, rP, rD, D, dedup, compress, copies;
699b24ab67Jeff Bonwick
700b24ab67Jeff Bonwick	if (dds->dds_blocks == 0)
701b24ab67Jeff Bonwick		return;
702b24ab67Jeff Bonwick
703b24ab67Jeff Bonwick	rL = (double)dds->dds_ref_lsize;
704b24ab67Jeff Bonwick	rP = (double)dds->dds_ref_psize;
705b24ab67Jeff Bonwick	rD = (double)dds->dds_ref_dsize;
706b24ab67Jeff Bonwick	D = (double)dds->dds_dsize;
707b24ab67Jeff Bonwick
708b24ab67Jeff Bonwick	dedup = rD / D;
709b24ab67Jeff Bonwick	compress = rL / rP;
710b24ab67Jeff Bonwick	copies = rD / rP;
711b24ab67Jeff Bonwick
712b24ab67Jeff Bonwick	(void) printf("dedup = %.2f, compress = %.2f, copies = %.2f, "
713b24ab67Jeff Bonwick	    "dedup * compress / copies = %.2f\n\n",
714b24ab67Jeff Bonwick	    dedup, compress, copies, dedup * compress / copies);
715b24ab67Jeff Bonwick}
716b24ab67Jeff Bonwick
717b24ab67Jeff Bonwickstatic void
718b24ab67Jeff Bonwickdump_ddt(ddt_t *ddt, enum ddt_type type, enum ddt_class class)
719b24ab67Jeff Bonwick{
720b24ab67Jeff Bonwick	char name[DDT_NAMELEN];
721b24ab67Jeff Bonwick	ddt_entry_t dde;
722b24ab67Jeff Bonwick	uint64_t walk = 0;
723b24ab67Jeff Bonwick	dmu_object_info_t doi;
724b24ab67Jeff Bonwick	uint64_t count, dspace, mspace;
725b24ab67Jeff Bonwick	int error;
726b24ab67Jeff Bonwick
727b24ab67Jeff Bonwick	error = ddt_object_info(ddt, type, class, &doi);
728b24ab67Jeff Bonwick
729b24ab67Jeff Bonwick	if (error == ENOENT)
730b24ab67Jeff Bonwick		return;
731b24ab67Jeff Bonwick	ASSERT(error == 0);
732b24ab67Jeff Bonwick
7337448a07George Wilson	if ((count = ddt_object_count(ddt, type, class)) == 0)
7347448a07George Wilson		return;
7357448a07George Wilson
736b24ab67Jeff Bonwick	dspace = doi.doi_physical_blocks_512 << 9;
737b24ab67Jeff Bonwick	mspace = doi.doi_fill_count * doi.doi_data_block_size;
738b24ab67Jeff Bonwick
739b24ab67Jeff Bonwick	ddt_object_name(ddt, type, class, name);
740b24ab67Jeff Bonwick
741b24ab67Jeff Bonwick	(void) printf("%s: %llu entries, size %llu on disk, %llu in core\n",
742b24ab67Jeff Bonwick	    name,
743b24ab67Jeff Bonwick	    (u_longlong_t)count,
744b24ab67Jeff Bonwick	    (u_longlong_t)(dspace / count),
745b24ab67Jeff Bonwick	    (u_longlong_t)(mspace / count));
746b24ab67Jeff Bonwick
747b24ab67Jeff Bonwick	if (dump_opt['D'] < 3)
748b24ab67Jeff Bonwick		return;
749b24ab67Jeff Bonwick
7509eb19f4George Wilson	zpool_dump_ddt(NULL, &ddt->ddt_histogram[type][class]);
751b24ab67Jeff Bonwick
752b24ab67Jeff Bonwick	if (dump_opt['D'] < 4)
753b24ab67Jeff Bonwick		return;
754b24ab67Jeff Bonwick
755b24ab67Jeff Bonwick	if (dump_opt['D'] < 5 && class == DDT_CLASS_UNIQUE)
756b24ab67Jeff Bonwick		return;
757b24ab67Jeff Bonwick
758b24ab67Jeff Bonwick	(void) printf("%s contents:\n\n", name);
759b24ab67Jeff Bonwick
760bbfd46cJeff Bonwick	while ((error = ddt_object_walk(ddt, type, class, &walk, &dde)) == 0)
761b24ab67Jeff Bonwick		dump_dde(ddt, &dde, walk);
762b24ab67Jeff Bonwick
763b24ab67Jeff Bonwick	ASSERT(error == ENOENT);
764b24ab67Jeff Bonwick
765b24ab67Jeff Bonwick	(void) printf("\n");
766b24ab67Jeff Bonwick}
767b24ab67Jeff Bonwick
768b24ab67Jeff Bonwickstatic void
769b24ab67Jeff Bonwickdump_all_ddts(spa_t *spa)
770b24ab67Jeff Bonwick{
771b24ab67Jeff Bonwick	ddt_histogram_t ddh_total = { 0 };
772b24ab67Jeff Bonwick	ddt_stat_t dds_total = { 0 };
773b24ab67Jeff Bonwick
774b24ab67Jeff Bonwick	for (enum zio_checksum c = 0; c < ZIO_CHECKSUM_FUNCTIONS; c++) {
775b24ab67Jeff Bonwick		ddt_t *ddt = spa->spa_ddt[c];
776b24ab67Jeff Bonwick		for (enum ddt_type type = 0; type < DDT_TYPES; type++) {
777b24ab67Jeff Bonwick			for (enum ddt_class class = 0; class < DDT_CLASSES;
778b24ab67Jeff Bonwick			    class++) {
779b24ab67Jeff Bonwick				dump_ddt(ddt, type, class);
780b24ab67Jeff Bonwick			}
781b24ab67Jeff Bonwick		}
782b24ab67Jeff Bonwick	}
783b24ab67Jeff Bonwick
7849eb19f4George Wilson	ddt_get_dedup_stats(spa, &dds_total);
785b24ab67Jeff Bonwick
786b24ab67Jeff Bonwick	if (dds_total.dds_blocks == 0) {
787b24ab67Jeff Bonwick		(void) printf("All DDTs are empty\n");
788b24ab67Jeff Bonwick		return;
789b24ab67Jeff Bonwick	}
790b24ab67Jeff Bonwick
791b24ab67Jeff Bonwick	(void) printf("\n");
792b24ab67Jeff Bonwick
793b24ab67Jeff Bonwick	if (dump_opt['D'] > 1) {
794b24ab67Jeff Bonwick		(void) printf("DDT histogram (aggregated over all DDTs):\n");
7959eb19f4George Wilson		ddt_get_dedup_histogram(spa, &ddh_total);
7969eb19f4George Wilson		zpool_dump_ddt(&dds_total, &ddh_total);
797b24ab67Jeff Bonwick	}
798b24ab67Jeff Bonwick
799b24ab67Jeff Bonwick	dump_dedup_ratio(&dds_total);
800b24ab67Jeff Bonwick}
801b24ab67Jeff Bonwick
802b24ab67Jeff Bonwickstatic void
8038ad4d6dJeff Bonwickdump_dtl_seg(space_map_t *sm, uint64_t start, uint64_t size)
8048ad4d6dJeff Bonwick{
8058ad4d6dJeff Bonwick	char *prefix = (void *)sm;
8068ad4d6dJeff Bonwick
8078ad4d6dJeff Bonwick	(void) printf("%s [%llu,%llu) length %llu\n",
8088ad4d6dJeff Bonwick	    prefix,
8098ad4d6dJeff Bonwick	    (u_longlong_t)start,
8108ad4d6dJeff Bonwick	    (u_longlong_t)(start + size),
8118ad4d6dJeff Bonwick	    (u_longlong_t)(size));
8128ad4d6dJeff Bonwick}
8138ad4d6dJeff Bonwick
8148ad4d6dJeff Bonwickstatic void
815fa9e406ahrensdump_dtl(vdev_t *vd, int indent)
8178ad4d6dJeff Bonwick	spa_t *spa = vd->vdev_spa;
8188ad4d6dJeff Bonwick	boolean_t required;
8198ad4d6dJeff Bonwick	char *name[DTL_TYPES] = { "missing", "partial", "scrub", "outage" };
8208ad4d6dJeff Bonwick	char prefix[256];
8218ad4d6dJeff Bonwick
8228f18d1fGeorge Wilson	spa_vdev_state_enter(spa, SCL_NONE);
8238ad4d6dJeff Bonwick	required = vdev_dtl_required(vd);
8248ad4d6dJeff Bonwick	(void) spa_vdev_state_exit(spa, NULL, 0);
826fa9e406ahrens	if (indent == 0)
827fa9e406ahrens		(void) printf("\nDirty time logs:\n\n");
8298ad4d6dJeff Bonwick	(void) printf("\t%*s%s [%s]\n", indent, "",
830e14bb32Jeff Bonwick	    vd->vdev_path ? vd->vdev_path :
8318ad4d6dJeff Bonwick	    vd->vdev_parent ? vd->vdev_ops->vdev_op_type : spa_name(spa),
8328ad4d6dJeff Bonwick	    required ? "DTL-required" : "DTL-expendable");
8348ad4d6dJeff Bonwick	for (int t = 0; t < DTL_TYPES; t++) {
8358ad4d6dJeff Bonwick		space_map_t *sm = &vd->vdev_dtl[t];
8368ad4d6dJeff Bonwick		if (sm->sm_space == 0)
8378ad4d6dJeff Bonwick			continue;
8388ad4d6dJeff Bonwick		(void) snprintf(prefix, sizeof (prefix), "\t%*s%s",
8398ad4d6dJeff Bonwick		    indent + 2, "", name[t]);
8408ad4d6dJeff Bonwick		mutex_enter(sm->sm_lock);
8418ad4d6dJeff Bonwick		space_map_walk(sm, dump_dtl_seg, (void *)prefix);
8428ad4d6dJeff Bonwick		mutex_exit(sm->sm_lock);
8438ad4d6dJeff Bonwick		if (dump_opt['d'] > 5 && vd->vdev_children == 0)
8448ad4d6dJeff Bonwick			dump_spacemap(spa->spa_meta_objset,
8458ad4d6dJeff Bonwick			    &vd->vdev_dtl_smo, sm);
846fa9e406ahrens	}
8488ad4d6dJeff Bonwick	for (int c = 0; c < vd->vdev_children; c++)
849fa9e406ahrens		dump_dtl(vd->vdev_child[c], indent + 4);
8528f18d1fGeorge Wilsonstatic void
8538f18d1fGeorge Wilsondump_history(spa_t *spa)
8548f18d1fGeorge Wilson{
8558f18d1fGeorge Wilson	nvlist_t **events = NULL;
8568f18d1fGeorge Wilson	char buf[SPA_MAXBLOCKSIZE];
857e4161dfVictor Latushkin	uint64_t resid, len, off = 0;
8588f18d1fGeorge Wilson	uint_t num = 0;
8598f18d1fGeorge Wilson	int error;
8608f18d1fGeorge Wilson	time_t tsec;
8618f18d1fGeorge Wilson	struct tm t;
8628f18d1fGeorge Wilson	char tbuf[30];
8638f18d1fGeorge Wilson	char internalstr[MAXPATHLEN];
8648f18d1fGeorge Wilson
8658f18d1fGeorge Wilson	do {
866e4161dfVictor Latushkin		len = sizeof (buf);
867e4161dfVictor Latushkin
8688f18d1fGeorge Wilson		if ((error = spa_history_get(spa, &off, &len, buf)) != 0) {
8698f18d1fGeorge Wilson			(void) fprintf(stderr, "Unable to read history: "
8708f18d1fGeorge Wilson			    "error %d\n", error);
8718f18d1fGeorge Wilson			return;
8728f18d1fGeorge Wilson		}
8738f18d1fGeorge Wilson
8748f18d1fGeorge Wilson		if (zpool_history_unpack(buf, len, &resid, &events, &num) != 0)
8758f18d1fGeorge Wilson			break;
8768f18d1fGeorge Wilson
8778f18d1fGeorge Wilson		off -= resid;
8788f18d1fGeorge Wilson	} while (len != 0);
8798f18d1fGeorge Wilson
8808f18d1fGeorge Wilson	(void) printf("\nHistory:\n");
8818f18d1fGeorge Wilson	for (int i = 0; i < num; i++) {
8828f18d1fGeorge Wilson		uint64_t time, txg, ievent;
8838f18d1fGeorge Wilson		char *cmd, *intstr;
8844445fffMatthew Ahrens		boolean_t printed = B_FALSE;
8858f18d1fGeorge Wilson
8868f18d1fGeorge Wilson		if (nvlist_lookup_uint64(events[i], ZPOOL_HIST_TIME,
8878f18d1fGeorge Wilson		    &time) != 0)
8884445fffMatthew Ahrens			goto next;
8898f18d1fGeorge Wilson		if (nvlist_lookup_string(events[i], ZPOOL_HIST_CMD,
8908f18d1fGeorge Wilson		    &cmd) != 0) {
8918f18d1fGeorge Wilson			if (nvlist_lookup_uint64(events[i],
8928f18d1fGeorge Wilson			    ZPOOL_HIST_INT_EVENT, &ievent) != 0)
8934445fffMatthew Ahrens				goto next;
8948f18d1fGeorge Wilson			verify(nvlist_lookup_uint64(events[i],
8958f18d1fGeorge Wilson			    ZPOOL_HIST_TXG, &txg) == 0);
8968f18d1fGeorge Wilson			verify(nvlist_lookup_string(events[i],
8978f18d1fGeorge Wilson			    ZPOOL_HIST_INT_STR, &intstr) == 0);
8984445fffMatthew Ahrens			if (ievent >= ZFS_NUM_LEGACY_HISTORY_EVENTS)
8994445fffMatthew Ahrens				goto next;
9008f18d1fGeorge Wilson
9018f18d1fGeorge Wilson			(void) snprintf(internalstr,
9028f18d1fGeorge Wilson			    sizeof (internalstr),
9038f18d1fGeorge Wilson			    "[internal %s txg:%lld] %s",
9043f9d6adLin Ling			    zfs_history_event_names[ievent], txg,
9058f18d1fGeorge Wilson			    intstr);
9068f18d1fGeorge Wilson			cmd = internalstr;
9078f18d1fGeorge Wilson		}
9088f18d1fGeorge Wilson		tsec = time;
9098f18d1fGeorge Wilson		(void) localtime_r(&tsec, &t);
9108f18d1fGeorge Wilson		(void) strftime(tbuf, sizeof (tbuf), "%F.%T", &t);
9118f18d1fGeorge Wilson		(void) printf("%s %s\n", tbuf, cmd);
9124445fffMatthew Ahrens		printed = B_TRUE;
9134445fffMatthew Ahrens
9144445fffMatthew Ahrensnext:
9154445fffMatthew Ahrens		if (dump_opt['h'] > 1) {
9164445fffMatthew Ahrens			if (!printed)
9174445fffMatthew Ahrens				(void) printf("unrecognized record:\n");
9184445fffMatthew Ahrens			dump_nvlist(events[i], 2);
9194445fffMatthew Ahrens		}
9208f18d1fGeorge Wilson	}
9218f18d1fGeorge Wilson}
9228f18d1fGeorge Wilson
924fa9e406ahrensstatic void
925fa9e406ahrensdump_dnode(objset_t *os, uint64_t object, void *data, size_t size)
929fa9e406ahrensstatic uint64_t
930b24ab67Jeff Bonwickblkid2offset(const dnode_phys_t *dnp, const blkptr_t *bp, const zbookmark_t *zb)
932b24ab67Jeff Bonwick	if (dnp == NULL) {
933b24ab67Jeff Bonwick		ASSERT(zb->zb_level < 0);
934b24ab67Jeff Bonwick		if (zb->zb_object == 0)
935b24ab67Jeff Bonwick			return (zb->zb_blkid);
936b24ab67Jeff Bonwick		return (zb->zb_blkid * BP_GET_LSIZE(bp));
937b24ab67Jeff Bonwick	}
938b24ab67Jeff Bonwick
939b24ab67Jeff Bonwick	ASSERT(zb->zb_level >= 0);
941b24ab67Jeff Bonwick	return ((zb->zb_blkid <<
942b24ab67Jeff Bonwick	    (zb->zb_level * (dnp->dn_indblkshift - SPA_BLKPTRSHIFT))) *
943fa9e406ahrens	    dnp->dn_datablkszsec << SPA_MINBLOCKSHIFT);
94644cd46cbillmstatic void
947cde58dbMatthew Ahrenssprintf_blkptr_compact(char *blkbuf, const blkptr_t *bp)
949cde58dbMatthew Ahrens	const dva_t *dva = bp->blk_dva;
950b24ab67Jeff Bonwick	int ndvas = dump_opt['d'] > 5 ? BP_GET_NDVAS(bp) : 1;
951b24ab67Jeff Bonwick
952b24ab67Jeff Bonwick	if (dump_opt['b'] >= 5) {
953b24ab67Jeff Bonwick		sprintf_blkptr(blkbuf, bp);
954b24ab67Jeff Bonwick		return;
955b24ab67Jeff Bonwick	}
95744cd46cbillm	blkbuf[0] = '\0';
959b24ab67Jeff Bonwick	for (int i = 0; i < ndvas; i++)
96044cd46cbillm		(void) sprintf(blkbuf + strlen(blkbuf), "%llu:%llx:%llx ",
96144cd46cbillm		    (u_longlong_t)DVA_GET_VDEV(&dva[i]),
96244cd46cbillm		    (u_longlong_t)DVA_GET_OFFSET(&dva[i]),
96344cd46cbillm		    (u_longlong_t)DVA_GET_ASIZE(&dva[i]));
965b24ab67Jeff Bonwick	(void) sprintf(blkbuf + strlen(blkbuf),
966b24ab67Jeff Bonwick	    "%llxL/%llxP F=%llu B=%llu/%llu",
96744cd46cbillm	    (u_longlong_t)BP_GET_LSIZE(bp),
96844cd46cbillm	    (u_longlong_t)BP_GET_PSIZE(bp),
96944cd46cbillm	    (u_longlong_t)bp->blk_fill,
970b24ab67Jeff Bonwick	    (u_longlong_t)bp->blk_birth,
971b24ab67Jeff Bonwick	    (u_longlong_t)BP_PHYSICAL_BIRTH(bp));
97488b7b0fMatthew Ahrensstatic void
97588b7b0fMatthew Ahrensprint_indirect(blkptr_t *bp, const zbookmark_t *zb,
97688b7b0fMatthew Ahrens    const dnode_phys_t *dnp)
97888b7b0fMatthew Ahrens	char blkbuf[BP_SPRINTF_LEN];
979fa9e406ahrens	int l;
98188b7b0fMatthew Ahrens	ASSERT3U(BP_GET_TYPE(bp), ==, dnp->dn_type);
98288b7b0fMatthew Ahrens	ASSERT3U(BP_GET_LEVEL(bp), ==, zb->zb_level);
984b24ab67Jeff Bonwick	(void) printf("%16llx ", (u_longlong_t)blkid2offset(dnp, bp, zb));
986fa9e406ahrens	ASSERT(zb->zb_level >= 0);
988fa9e406ahrens	for (l = dnp->dn_nlevels - 1; l >= -1; l--) {
989fa9e406ahrens		if (l == zb->zb_level) {
99088b7b0fMatthew Ahrens			(void) printf("L%llx", (u_longlong_t)zb->zb_level);
991fa9e406ahrens		} else {
99288b7b0fMatthew Ahrens			(void) printf(" ");
993fa9e406ahrens		}
994fa9e406ahrens	}
996b24ab67Jeff Bonwick	sprintf_blkptr_compact(blkbuf, bp);