zdb.c revision 87219db7f64d5002b5a8311fd76f7900fda2c4e8
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/*
22ccba080Rich Morris * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
23fa9e406ahrens * Use is subject to license terms.
24fa9e406ahrens */
25fa9e406ahrens
26fa9e406ahrens#include <stdio.h>
27004388ecasper#include <stdio_ext.h>
28fa9e406ahrens#include <stdlib.h>
2944cd46cbillm#include <ctype.h>
30fa9e406ahrens#include <sys/zfs_context.h>
31fa9e406ahrens#include <sys/spa.h>
32fa9e406ahrens#include <sys/spa_impl.h>
33fa9e406ahrens#include <sys/dmu.h>
34fa9e406ahrens#include <sys/zap.h>
35fa9e406ahrens#include <sys/fs/zfs.h>
36fa9e406ahrens#include <sys/zfs_znode.h>
37fa9e406ahrens#include <sys/vdev.h>
38fa9e406ahrens#include <sys/vdev_impl.h>
39fa9e406ahrens#include <sys/metaslab_impl.h>
40fa9e406ahrens#include <sys/dmu_objset.h>
41fa9e406ahrens#include <sys/dsl_dir.h>
42fa9e406ahrens#include <sys/dsl_dataset.h>
43fa9e406ahrens#include <sys/dsl_pool.h>
44fa9e406ahrens#include <sys/dbuf.h>
45fa9e406ahrens#include <sys/zil.h>
46fa9e406ahrens#include <sys/zil_impl.h>
47fa9e406ahrens#include <sys/stat.h>
48fa9e406ahrens#include <sys/resource.h>
49fa9e406ahrens#include <sys/dmu_traverse.h>
50fa9e406ahrens#include <sys/zio_checksum.h>
51fa9e406ahrens#include <sys/zio_compress.h>
52e0d35c4marks#include <sys/zfs_fuid.h>
5388b7b0fMatthew Ahrens#include <sys/arc.h>
54de6628fck#undef ZFS_MAXNAMELEN
55de6628fck#undef verify
56de6628fck#include <libzfs.h>
57fa9e406ahrens
586de8f41Victor Latushkin#define	ZDB_COMPRESS_NAME(idx) ((idx) < ZIO_COMPRESS_FUNCTIONS ? \
596de8f41Victor Latushkin    zio_compress_table[(idx)].ci_name : "UNKNOWN")
606de8f41Victor Latushkin#define	ZDB_CHECKSUM_NAME(idx) ((idx) < ZIO_CHECKSUM_FUNCTIONS ? \
616de8f41Victor Latushkin    zio_checksum_table[(idx)].ci_name : "UNKNOWN")
626de8f41Victor Latushkin#define	ZDB_OT_NAME(idx) ((idx) < DMU_OT_NUMTYPES ? \
636de8f41Victor Latushkin    dmu_ot[(idx)].ot_name : "UNKNOWN")
646de8f41Victor Latushkin#define	ZDB_OT_TYPE(idx) ((idx) < DMU_OT_NUMTYPES ? (idx) : DMU_OT_NUMTYPES)
656de8f41Victor Latushkin
66fa9e406ahrensconst char cmdname[] = "zdb";
67fa9e406ahrensuint8_t dump_opt[256];
68fa9e406ahrens
69fa9e406ahrenstypedef void object_viewer_t(objset_t *, uint64_t, void *data, size_t size);
70fa9e406ahrens
71fa9e406ahrensextern void dump_intent_log(zilog_t *);
72fa9e406ahrensuint64_t *zopt_object = NULL;
73fa9e406ahrensint zopt_objects = 0;
74de6628fcklibzfs_handle_t *g_zfs;
75d41e764ekboolean_t zdb_sig_user_data = B_TRUE;
76d41e764ekint zdb_sig_cksumalg = ZIO_CHECKSUM_SHA256;
77fa9e406ahrens
78fa9e406ahrens/*
79fa9e406ahrens * These libumem hooks provide a reasonable set of defaults for the allocator's
80fa9e406ahrens * debugging facilities.
81fa9e406ahrens */
82fa9e406ahrensconst char *
83fa9e406ahrens_umem_debug_init()
84fa9e406ahrens{
85fa9e406ahrens	return ("default,verbose"); /* $UMEM_DEBUG setting */
86fa9e406ahrens}
87fa9e406ahrens
88fa9e406ahrensconst char *
89fa9e406ahrens_umem_logging_init(void)
90fa9e406ahrens{
91fa9e406ahrens	return ("fail,contents"); /* $UMEM_LOGGING setting */
92fa9e406ahrens}
93fa9e406ahrens
94fa9e406ahrensstatic void
95fa9e406ahrensusage(void)
96fa9e406ahrens{
97fa9e406ahrens	(void) fprintf(stderr,
983ad6c7fVictor Latushkin	    "Usage: %s [-CumdibcsvhL] [-S user:cksumalg] "
993ad6c7fVictor Latushkin	    "poolname [object...]\n"
1003ad6c7fVictor Latushkin	    "       %s [-div] dataset [object...]\n"
10187219dbVictor Latushkin	    "       %s -m [-L] poolname [vdev [metaslab...]]\n"
10207428bdVictor Latushkin	    "       %s -R poolname vdev:offset:size[:flags]\n"
10307428bdVictor Latushkin	    "       %s -l device\n"
10407428bdVictor Latushkin	    "       %s -C\n\n",
10587219dbVictor Latushkin	    cmdname, cmdname, cmdname, cmdname, cmdname, cmdname);
1063ad6c7fVictor Latushkin
1073ad6c7fVictor Latushkin	(void) fprintf(stderr, "    Dataset name must include at least one "
1083ad6c7fVictor Latushkin	    "separator character '/' or '@'\n");
1093ad6c7fVictor Latushkin	(void) fprintf(stderr, "    If dataset name is specified, only that "
1103ad6c7fVictor Latushkin	    "dataset is dumped\n");
1113ad6c7fVictor Latushkin	(void) fprintf(stderr, "    If object numbers are specified, only "
1123ad6c7fVictor Latushkin	    "those objects are dumped\n\n");
1133ad6c7fVictor Latushkin	(void) fprintf(stderr, "    Options to control amount of output:\n");
1143ad6c7fVictor Latushkin	(void) fprintf(stderr, "        -u uberblock\n");
1153ad6c7fVictor Latushkin	(void) fprintf(stderr, "        -d dataset(s)\n");
1163ad6c7fVictor Latushkin	(void) fprintf(stderr, "        -i intent logs\n");
11707428bdVictor Latushkin	(void) fprintf(stderr, "        -C config (or cachefile if alone)\n");
1188f18d1fGeorge Wilson	(void) fprintf(stderr, "        -h pool history\n");
1193ad6c7fVictor Latushkin	(void) fprintf(stderr, "        -b block statistics\n");
1203ad6c7fVictor Latushkin	(void) fprintf(stderr, "        -m metaslabs\n");
1213ad6c7fVictor Latushkin	(void) fprintf(stderr, "        -c checksum all metadata (twice for "
1226365109Victor Latushkin	    "all data) blocks\n");
1233ad6c7fVictor Latushkin	(void) fprintf(stderr, "        -s report stats on zdb's I/O\n");
1243ad6c7fVictor Latushkin	(void) fprintf(stderr, "        -S <user|all>:<cksum_alg|all> -- "
125d41e764ek	    "dump blkptr signatures\n");
1263ad6c7fVictor Latushkin	(void) fprintf(stderr, "        -v verbose (applies to all others)\n");
127fa9e406ahrens	(void) fprintf(stderr, "        -l dump label contents\n");
12882a0a98Victor Latushkin	(void) fprintf(stderr, "        -L disable leak tracking (do not "
12982a0a98Victor Latushkin	    "load spacemaps)\n");
130d41e764ek	(void) fprintf(stderr, "        -R read and display block from a "
1313ad6c7fVictor Latushkin	    "device\n\n");
1323ad6c7fVictor Latushkin	(void) fprintf(stderr, "    Below options are intended for use "
1333ad6c7fVictor Latushkin	    "with other options (except -l):\n");
1343ad6c7fVictor Latushkin	(void) fprintf(stderr, "        -U <cachefile_path> -- use alternate "
1353ad6c7fVictor Latushkin	    "cachefile\n");
1363ad6c7fVictor Latushkin	(void) fprintf(stderr, "        -e pool is exported/destroyed/"
1373ad6c7fVictor Latushkin	    "has altroot/not in a cachefile\n");
1383ad6c7fVictor Latushkin	(void) fprintf(stderr, "        -p <path> -- use one or more with "
1393ad6c7fVictor Latushkin	    "-e to specify path to vdev dir\n");
1403ad6c7fVictor Latushkin	(void) fprintf(stderr, "        -t <txg> -- highest txg to use when "
1412e55192Victor Latushkin	    "searching for uberblocks\n");
142fa9e406ahrens	(void) fprintf(stderr, "Specify an option more than once (e.g. -bb) "
143fa9e406ahrens	    "to make only that option verbose\n");
144fa9e406ahrens	(void) fprintf(stderr, "Default is to dump everything non-verbosely\n");
145fa9e406ahrens	exit(1);
146fa9e406ahrens}
147fa9e406ahrens
148ccba080Rich Morris/*
149ccba080Rich Morris * Called for usage errors that are discovered after a call to spa_open(),
150ccba080Rich Morris * dmu_bonus_hold(), or pool_match().  abort() is called for other errors.
151ccba080Rich Morris */
152ccba080Rich Morris
153fa9e406ahrensstatic void
154fa9e406ahrensfatal(const char *fmt, ...)
155fa9e406ahrens{
156fa9e406ahrens	va_list ap;
157fa9e406ahrens
158fa9e406ahrens	va_start(ap, fmt);
159fa9e406ahrens	(void) fprintf(stderr, "%s: ", cmdname);
160fa9e406ahrens	(void) vfprintf(stderr, fmt, ap);
161fa9e406ahrens	va_end(ap);
162fa9e406ahrens	(void) fprintf(stderr, "\n");
163fa9e406ahrens
164ccba080Rich Morris	exit(1);
165fa9e406ahrens}
166fa9e406ahrens
167fa9e406ahrens/* ARGSUSED */
168fa9e406ahrensstatic void
169fa9e406ahrensdump_packed_nvlist(objset_t *os, uint64_t object, void *data, size_t size)
170fa9e406ahrens{
171fa9e406ahrens	nvlist_t *nv;
172fa9e406ahrens	size_t nvsize = *(uint64_t *)data;
173fa9e406ahrens	char *packed = umem_alloc(nvsize, UMEM_NOFAIL);
174fa9e406ahrens
1757bfdf01Neil Perrin	VERIFY(0 == dmu_read(os, object, 0, nvsize, packed, DMU_READ_PREFETCH));
176fa9e406ahrens
177fa9e406ahrens	VERIFY(nvlist_unpack(packed, nvsize, &nv, 0) == 0);
178fa9e406ahrens
179fa9e406ahrens	umem_free(packed, nvsize);
180fa9e406ahrens
181fa9e406ahrens	dump_nvlist(nv, 8);
182fa9e406ahrens
183fa9e406ahrens	nvlist_free(nv);
184fa9e406ahrens}
185fa9e406ahrens
186fa9e406ahrensconst char dump_zap_stars[] = "****************************************";
187fa9e406ahrensconst int dump_zap_width = sizeof (dump_zap_stars) - 1;
188fa9e406ahrens
189fa9e406ahrensstatic void
190fa9e406ahrensdump_zap_histogram(uint64_t histo[ZAP_HISTOGRAM_SIZE])
191fa9e406ahrens{
192fa9e406ahrens	int i;
193fa9e406ahrens	int minidx = ZAP_HISTOGRAM_SIZE - 1;
194fa9e406ahrens	int maxidx = 0;
195fa9e406ahrens	uint64_t max = 0;
196fa9e406ahrens
197fa9e406ahrens	for (i = 0; i < ZAP_HISTOGRAM_SIZE; i++) {
198fa9e406ahrens		if (histo[i] > max)
199fa9e406ahrens			max = histo[i];
200fa9e406ahrens		if (histo[i] > 0 && i > maxidx)
201fa9e406ahrens			maxidx = i;
202fa9e406ahrens		if (histo[i] > 0 && i < minidx)
203fa9e406ahrens			minidx = i;
204fa9e406ahrens	}
205fa9e406ahrens
206fa9e406ahrens	if (max < dump_zap_width)
207fa9e406ahrens		max = dump_zap_width;
208fa9e406ahrens
209fa9e406ahrens	for (i = minidx; i <= maxidx; i++)
210fa9e406ahrens		(void) printf("\t\t\t%u: %6llu %s\n", i, (u_longlong_t)histo[i],
211fa9e406ahrens		    &dump_zap_stars[(max - histo[i]) * dump_zap_width / max]);
212fa9e406ahrens}
213fa9e406ahrens
214fa9e406ahrensstatic void
215fa9e406ahrensdump_zap_stats(objset_t *os, uint64_t object)
216fa9e406ahrens{
217fa9e406ahrens	int error;
218fa9e406ahrens	zap_stats_t zs;
219fa9e406ahrens
220fa9e406ahrens	error = zap_get_stats(os, object, &zs);
221fa9e406ahrens	if (error)
222fa9e406ahrens		return;
223fa9e406ahrens
224fa9e406ahrens	if (zs.zs_ptrtbl_len == 0) {
225fa9e406ahrens		ASSERT(zs.zs_num_blocks == 1);
226fa9e406ahrens		(void) printf("\tmicrozap: %llu bytes, %llu entries\n",
227fa9e406ahrens		    (u_longlong_t)zs.zs_blocksize,
228fa9e406ahrens		    (u_longlong_t)zs.zs_num_entries);
229fa9e406ahrens		return;
230fa9e406ahrens	}
231fa9e406ahrens
232fa9e406ahrens	(void) printf("\tFat ZAP stats:\n");
2338248818nd
2348248818nd	(void) printf("\t\tPointer table:\n");
2358248818nd	(void) printf("\t\t\t%llu elements\n",
236fa9e406ahrens	    (u_longlong_t)zs.zs_ptrtbl_len);
2378248818nd	(void) printf("\t\t\tzt_blk: %llu\n",
2388248818nd	    (u_longlong_t)zs.zs_ptrtbl_zt_blk);
2398248818nd	(void) printf("\t\t\tzt_numblks: %llu\n",
2408248818nd	    (u_longlong_t)zs.zs_ptrtbl_zt_numblks);
2418248818nd	(void) printf("\t\t\tzt_shift: %llu\n",
2428248818nd	    (u_longlong_t)zs.zs_ptrtbl_zt_shift);
2438248818nd	(void) printf("\t\t\tzt_blks_copied: %llu\n",
2448248818nd	    (u_longlong_t)zs.zs_ptrtbl_blks_copied);
2458248818nd	(void) printf("\t\t\tzt_nextblk: %llu\n",
2468248818nd	    (u_longlong_t)zs.zs_ptrtbl_nextblk);
2478248818nd
248fa9e406ahrens	(void) printf("\t\tZAP entries: %llu\n",
249fa9e406ahrens	    (u_longlong_t)zs.zs_num_entries);
250fa9e406ahrens	(void) printf("\t\tLeaf blocks: %llu\n",
251fa9e406ahrens	    (u_longlong_t)zs.zs_num_leafs);
252fa9e406ahrens	(void) printf("\t\tTotal blocks: %llu\n",
253fa9e406ahrens	    (u_longlong_t)zs.zs_num_blocks);
2548248818nd	(void) printf("\t\tzap_block_type: 0x%llx\n",
2558248818nd	    (u_longlong_t)zs.zs_block_type);
2568248818nd	(void) printf("\t\tzap_magic: 0x%llx\n",
2578248818nd	    (u_longlong_t)zs.zs_magic);
2588248818nd	(void) printf("\t\tzap_salt: 0x%llx\n",
2598248818nd	    (u_longlong_t)zs.zs_salt);
260fa9e406ahrens
261fa9e406ahrens	(void) printf("\t\tLeafs with 2^n pointers:\n");
262fa9e406ahrens	dump_zap_histogram(zs.zs_leafs_with_2n_pointers);
263fa9e406ahrens
264fa9e406ahrens	(void) printf("\t\tBlocks with n*5 entries:\n");
265fa9e406ahrens	dump_zap_histogram(zs.zs_blocks_with_n5_entries);
266fa9e406ahrens
267fa9e406ahrens	(void) printf("\t\tBlocks n/10 full:\n");
268fa9e406ahrens	dump_zap_histogram(zs.zs_blocks_n_tenths_full);
269fa9e406ahrens
270fa9e406ahrens	(void) printf("\t\tEntries with n chunks:\n");
271fa9e406ahrens	dump_zap_histogram(zs.zs_entries_using_n_chunks);
272fa9e406ahrens
273fa9e406ahrens	(void) printf("\t\tBuckets with n entries:\n");
274fa9e406ahrens	dump_zap_histogram(zs.zs_buckets_with_n_entries);
275fa9e406ahrens}
276fa9e406ahrens
277fa9e406ahrens/*ARGSUSED*/
278fa9e406ahrensstatic void
279fa9e406ahrensdump_none(objset_t *os, uint64_t object, void *data, size_t size)
280fa9e406ahrens{
281fa9e406ahrens}
282fa9e406ahrens
283fa9e406ahrens/*ARGSUSED*/
2846de8f41Victor Latushkinstatic void
2856de8f41Victor Latushkindump_unknown(objset_t *os, uint64_t object, void *data, size_t size)
2866de8f41Victor Latushkin{
2876de8f41Victor Latushkin	(void) printf("\tUNKNOWN OBJECT TYPE\n");
2886de8f41Victor Latushkin}
2896de8f41Victor Latushkin
2906de8f41Victor Latushkin/*ARGSUSED*/
291fa9e406ahrensvoid
292fa9e406ahrensdump_uint8(objset_t *os, uint64_t object, void *data, size_t size)
293fa9e406ahrens{
294fa9e406ahrens}
295fa9e406ahrens
296fa9e406ahrens/*ARGSUSED*/
297fa9e406ahrensstatic void
298fa9e406ahrensdump_uint64(objset_t *os, uint64_t object, void *data, size_t size)
299fa9e406ahrens{
300fa9e406ahrens}
301fa9e406ahrens
302fa9e406ahrens/*ARGSUSED*/
303fa9e406ahrensstatic void
304fa9e406ahrensdump_zap(objset_t *os, uint64_t object, void *data, size_t size)
305fa9e406ahrens{
306fa9e406ahrens	zap_cursor_t zc;
307fa9e406ahrens	zap_attribute_t attr;
308fa9e406ahrens	void *prop;
309fa9e406ahrens	int i;
310fa9e406ahrens
311fa9e406ahrens	dump_zap_stats(os, object);
312fa9e406ahrens	(void) printf("\n");
313fa9e406ahrens
314fa9e406ahrens	for (zap_cursor_init(&zc, os, object);
315fa9e406ahrens	    zap_cursor_retrieve(&zc, &attr) == 0;
316fa9e406ahrens	    zap_cursor_advance(&zc)) {
317fa9e406ahrens		(void) printf("\t\t%s = ", attr.za_name);
318fa9e406ahrens		if (attr.za_num_integers == 0) {
319fa9e406ahrens			(void) printf("\n");
320fa9e406ahrens			continue;
321fa9e406ahrens		}
322fa9e406ahrens		prop = umem_zalloc(attr.za_num_integers *
323fa9e406ahrens		    attr.za_integer_length, UMEM_NOFAIL);
324fa9e406ahrens		(void) zap_lookup(os, object, attr.za_name,
325fa9e406ahrens		    attr.za_integer_length, attr.za_num_integers, prop);
326fa9e406ahrens		if (attr.za_integer_length == 1) {
327fa9e406ahrens			(void) printf("%s", (char *)prop);
328fa9e406ahrens		} else {
329fa9e406ahrens			for (i = 0; i < attr.za_num_integers; i++) {
330fa9e406ahrens				switch (attr.za_integer_length) {
331fa9e406ahrens				case 2:
332fa9e406ahrens					(void) printf("%u ",
333fa9e406ahrens					    ((uint16_t *)prop)[i]);
334fa9e406ahrens					break;
335fa9e406ahrens				case 4:
336fa9e406ahrens					(void) printf("%u ",
337fa9e406ahrens					    ((uint32_t *)prop)[i]);
338fa9e406ahrens					break;
339fa9e406ahrens				case 8:
340fa9e406ahrens					(void) printf("%lld ",
341fa9e406ahrens					    (u_longlong_t)((int64_t *)prop)[i]);
342fa9e406ahrens					break;
343fa9e406ahrens				}
344fa9e406ahrens			}
345fa9e406ahrens		}
346fa9e406ahrens		(void) printf("\n");
347fa9e406ahrens		umem_free(prop, attr.za_num_integers * attr.za_integer_length);
348fa9e406ahrens	}
34987e5029ahrens	zap_cursor_fini(&zc);
350fa9e406ahrens}
351fa9e406ahrens
352e743726ahrens/*ARGSUSED*/
353e743726ahrensstatic void
354e743726ahrensdump_zpldir(objset_t *os, uint64_t object, void *data, size_t size)
355e743726ahrens{
356e743726ahrens	zap_cursor_t zc;
357e743726ahrens	zap_attribute_t attr;
358e743726ahrens	const char *typenames[] = {
359e743726ahrens		/* 0 */ "not specified",
360e743726ahrens		/* 1 */ "FIFO",
361e743726ahrens		/* 2 */ "Character Device",
362e743726ahrens		/* 3 */ "3 (invalid)",
363e743726ahrens		/* 4 */ "Directory",
364e743726ahrens		/* 5 */ "5 (invalid)",
365e743726ahrens		/* 6 */ "Block Device",
366e743726ahrens		/* 7 */ "7 (invalid)",
367e743726ahrens		/* 8 */ "Regular File",
368e743726ahrens		/* 9 */ "9 (invalid)",
369e743726ahrens		/* 10 */ "Symbolic Link",
370e743726ahrens		/* 11 */ "11 (invalid)",
371e743726ahrens		/* 12 */ "Socket",
372e743726ahrens		/* 13 */ "Door",
373e743726ahrens		/* 14 */ "Event Port",
374e743726ahrens		/* 15 */ "15 (invalid)",
375e743726ahrens	};
376e743726ahrens
377e743726ahrens	dump_zap_stats(os, object);
378e743726ahrens	(void) printf("\n");
379e743726ahrens
380e743726ahrens	for (zap_cursor_init(&zc, os, object);
381e743726ahrens	    zap_cursor_retrieve(&zc, &attr) == 0;
382e743726ahrens	    zap_cursor_advance(&zc)) {
383e743726ahrens		(void) printf("\t\t%s = %lld (type: %s)\n",
384e743726ahrens		    attr.za_name, ZFS_DIRENT_OBJ(attr.za_first_integer),
385e743726ahrens		    typenames[ZFS_DIRENT_TYPE(attr.za_first_integer)]);
386e743726ahrens	}
387e743726ahrens	zap_cursor_fini(&zc);
388e743726ahrens}
389e743726ahrens
390fa9e406ahrensstatic void
391fa9e406ahrensdump_spacemap(objset_t *os, space_map_obj_t *smo, space_map_t *sm)
392fa9e406ahrens{
393fa9e406ahrens	uint64_t alloc, offset, entry;
394ecc2d60bonwick	uint8_t mapshift = sm->sm_shift;
395fa9e406ahrens	uint64_t mapstart = sm->sm_start;
3968053a26ck	char *ddata[] = { "ALLOC", "FREE", "CONDENSE", "INVALID",
3978053a26ck			    "INVALID", "INVALID", "INVALID", "INVALID" };
398fa9e406ahrens
399fa9e406ahrens	if (smo->smo_object == 0)
400fa9e406ahrens		return;
401fa9e406ahrens
402fa9e406ahrens	/*
403fa9e406ahrens	 * Print out the freelist entries in both encoded and decoded form.
404fa9e406ahrens	 */
405fa9e406ahrens	alloc = 0;
406fa9e406ahrens	for (offset = 0; offset < smo->smo_objsize; offset += sizeof (entry)) {
407ea8dc4beschrock		VERIFY(0 == dmu_read(os, smo->smo_object, offset,
4087bfdf01Neil Perrin		    sizeof (entry), &entry, DMU_READ_PREFETCH));
409fa9e406ahrens		if (SM_DEBUG_DECODE(entry)) {
41087219dbVictor Latushkin			(void) printf("\t    [%6llu] %s: txg %llu, pass %llu\n",
411fa9e406ahrens			    (u_longlong_t)(offset / sizeof (entry)),
412fa9e406ahrens			    ddata[SM_DEBUG_ACTION_DECODE(entry)],
4135ad8204nd			    (u_longlong_t)SM_DEBUG_TXG_DECODE(entry),
4145ad8204nd			    (u_longlong_t)SM_DEBUG_SYNCPASS_DECODE(entry));
415fa9e406ahrens		} else {
41687219dbVictor Latushkin			(void) printf("\t    [%6llu]    %c  range:"
41787219dbVictor Latushkin			    " %010llx-%010llx  size: %06llx\n",
418fa9e406ahrens			    (u_longlong_t)(offset / sizeof (entry)),
419fa9e406ahrens			    SM_TYPE_DECODE(entry) == SM_ALLOC ? 'A' : 'F',
4205ad8204nd			    (u_longlong_t)((SM_OFFSET_DECODE(entry) <<
4215ad8204nd			    mapshift) + mapstart),
4225ad8204nd			    (u_longlong_t)((SM_OFFSET_DECODE(entry) <<
4235ad8204nd			    mapshift) + mapstart + (SM_RUN_DECODE(entry) <<
4245ad8204nd			    mapshift)),
4255ad8204nd			    (u_longlong_t)(SM_RUN_DECODE(entry) << mapshift));
426fa9e406ahrens			if (SM_TYPE_DECODE(entry) == SM_ALLOC)
427fa9e406ahrens				alloc += SM_RUN_DECODE(entry) << mapshift;
428fa9e406ahrens			else
429fa9e406ahrens				alloc -= SM_RUN_DECODE(entry) << mapshift;
430fa9e406ahrens		}
431fa9e406ahrens	}
432fa9e406ahrens	if (alloc != smo->smo_alloc) {
433fa9e406ahrens		(void) printf("space_map_object alloc (%llu) INCONSISTENT "
434fa9e406ahrens		    "with space map summary (%llu)\n",
435fa9e406ahrens		    (u_longlong_t)smo->smo_alloc, (u_longlong_t)alloc);
436fa9e406ahrens	}
437fa9e406ahrens}
438fa9e406ahrens
439fa9e406ahrensstatic void
440d6e555bGeorge Wilsondump_metaslab_stats(metaslab_t *msp)
441d6e555bGeorge Wilson{
442d6e555bGeorge Wilson	char maxbuf[5];
443d6e555bGeorge Wilson	space_map_t *sm = &msp->ms_map;
444d6e555bGeorge Wilson	avl_tree_t *t = sm->sm_pp_root;
445d6e555bGeorge Wilson	int free_pct = sm->sm_space * 100 / sm->sm_size;
446d6e555bGeorge Wilson
447d6e555bGeorge Wilson	nicenum(space_map_maxsize(sm), maxbuf);
448d6e555bGeorge Wilson
44987219dbVictor Latushkin	(void) printf("\t %25s %10lu   %7s  %6s   %4s %4d%%\n",
450d6e555bGeorge Wilson	    "segments", avl_numnodes(t), "maxsize", maxbuf,
451d6e555bGeorge Wilson	    "freepct", free_pct);
452d6e555bGeorge Wilson}
453d6e555bGeorge Wilson
454d6e555bGeorge Wilsonstatic void
455fa9e406ahrensdump_metaslab(metaslab_t *msp)
456fa9e406ahrens{
457fa9e406ahrens	char freebuf[5];
458ecc2d60bonwick	space_map_obj_t *smo = &msp->ms_smo;
459fa9e406ahrens	vdev_t *vd = msp->ms_group->mg_vd;
460fa9e406ahrens	spa_t *spa = vd->vdev_spa;
461fa9e406ahrens
462fa9e406ahrens	nicenum(msp->ms_map.sm_size - smo->smo_alloc, freebuf);
463fa9e406ahrens
464fa9e406ahrens	(void) printf(
46587219dbVictor Latushkin	    "\tmetaslab %6llu   offset %12llx   spacemap %6llu   free    %5s\n",
46687219dbVictor Latushkin	    (u_longlong_t)(msp->ms_map.sm_start / msp->ms_map.sm_size),
46787219dbVictor Latushkin	    (u_longlong_t)msp->ms_map.sm_start, (u_longlong_t)smo->smo_object,
46887219dbVictor Latushkin	    freebuf);
469fa9e406ahrens
47087219dbVictor Latushkin	if (dump_opt['m'] > 1 && !dump_opt['L']) {
471d6e555bGeorge Wilson		mutex_enter(&msp->ms_lock);
472d6e555bGeorge Wilson		VERIFY(space_map_load(&msp->ms_map, zfs_metaslab_ops,
473d6e555bGeorge Wilson		    SM_FREE, &msp->ms_smo, spa->spa_meta_objset) == 0);
474d6e555bGeorge Wilson		dump_metaslab_stats(msp);
475d6e555bGeorge Wilson		space_map_unload(&msp->ms_map);
476d6e555bGeorge Wilson		mutex_exit(&msp->ms_lock);
477d6e555bGeorge Wilson	}
478d6e555bGeorge Wilson
479d6e555bGeorge Wilson	if (dump_opt['d'] > 5 || dump_opt['m'] > 2) {
480d6e555bGeorge Wilson		ASSERT(msp->ms_map.sm_size == (1ULL << vd->vdev_ms_shift));
481d6e555bGeorge Wilson
482d6e555bGeorge Wilson		mutex_enter(&msp->ms_lock);
483d6e555bGeorge Wilson		dump_spacemap(spa->spa_meta_objset, smo, &msp->ms_map);
484d6e555bGeorge Wilson		mutex_exit(&msp->ms_lock);
485d6e555bGeorge Wilson	}
48687219dbVictor Latushkin}
487fa9e406ahrens
48887219dbVictor Latushkinstatic void
48987219dbVictor Latushkinprint_vdev_metaslab_header(vdev_t *vd)
49087219dbVictor Latushkin{
49187219dbVictor Latushkin	(void) printf("\tvdev %10llu\n\t%-10s%5llu   %-19s   %-15s   %-10s\n",
49287219dbVictor Latushkin	    (u_longlong_t)vd->vdev_id,
49387219dbVictor Latushkin	    "metaslabs", (u_longlong_t)vd->vdev_ms_count,
49487219dbVictor Latushkin	    "offset", "spacemap", "free");
49587219dbVictor Latushkin	(void) printf("\t%15s   %19s   %15s   %10s\n",
49687219dbVictor Latushkin	    "---------------", "-------------------",
49787219dbVictor Latushkin	    "---------------", "-------------");
498fa9e406ahrens}
499fa9e406ahrens
500fa9e406ahrensstatic void
501fa9e406ahrensdump_metaslabs(spa_t *spa)
502fa9e406ahrens{
50387219dbVictor Latushkin	vdev_t *vd, *rvd = spa->spa_root_vdev;
50487219dbVictor Latushkin	uint64_t m, c = 0, children = rvd->vdev_children;
505fa9e406ahrens
506fa9e406ahrens	(void) printf("\nMetaslabs:\n");
507fa9e406ahrens
50887219dbVictor Latushkin	if (!dump_opt['d'] && zopt_objects > 0) {
50987219dbVictor Latushkin		c = zopt_object[0];
51087219dbVictor Latushkin
51187219dbVictor Latushkin		if (c >= children)
51287219dbVictor Latushkin			(void) fatal("bad vdev id: %llu", (u_longlong_t)c);
513fa9e406ahrens
51487219dbVictor Latushkin		if (zopt_objects > 1) {
51587219dbVictor Latushkin			vd = rvd->vdev_child[c];
51687219dbVictor Latushkin			print_vdev_metaslab_header(vd);
51787219dbVictor Latushkin
51887219dbVictor Latushkin			for (m = 1; m < zopt_objects; m++) {
51987219dbVictor Latushkin				if (zopt_object[m] < vd->vdev_ms_count)
52087219dbVictor Latushkin					dump_metaslab(
52187219dbVictor Latushkin					    vd->vdev_ms[zopt_object[m]]);
52287219dbVictor Latushkin				else
52387219dbVictor Latushkin					(void) fprintf(stderr, "bad metaslab "
52487219dbVictor Latushkin					    "number %llu\n",
52587219dbVictor Latushkin					    (u_longlong_t)zopt_object[m]);
52687219dbVictor Latushkin			}
52787219dbVictor Latushkin			(void) printf("\n");
52887219dbVictor Latushkin			return;
52987219dbVictor Latushkin		}
53087219dbVictor Latushkin		children = c + 1;
53187219dbVictor Latushkin	}
53287219dbVictor Latushkin	for (; c < children; c++) {
53387219dbVictor Latushkin		vd = rvd->vdev_child[c];
53487219dbVictor Latushkin		print_vdev_metaslab_header(vd);
535fa9e406ahrens
536fa9e406ahrens		for (m = 0; m < vd->vdev_ms_count; m++)
537fa9e406ahrens			dump_metaslab(vd->vdev_ms[m]);
538fa9e406ahrens		(void) printf("\n");
539fa9e406ahrens	}
540fa9e406ahrens}
541fa9e406ahrens
542fa9e406ahrensstatic void
5438ad4d6dJeff Bonwickdump_dtl_seg(space_map_t *sm, uint64_t start, uint64_t size)
5448ad4d6dJeff Bonwick{
5458ad4d6dJeff Bonwick	char *prefix = (void *)sm;
5468ad4d6dJeff Bonwick
5478ad4d6dJeff Bonwick	(void) printf("%s [%llu,%llu) length %llu\n",
5488ad4d6dJeff Bonwick	    prefix,
5498ad4d6dJeff Bonwick	    (u_longlong_t)start,
5508ad4d6dJeff Bonwick	    (u_longlong_t)(start + size),
5518ad4d6dJeff Bonwick	    (u_longlong_t)(size));
5528ad4d6dJeff Bonwick}
5538ad4d6dJeff Bonwick
5548ad4d6dJeff Bonwickstatic void
555fa9e406ahrensdump_dtl(vdev_t *vd, int indent)
556fa9e406ahrens{
5578ad4d6dJeff Bonwick	spa_t *spa = vd->vdev_spa;
5588ad4d6dJeff Bonwick	boolean_t required;
5598ad4d6dJeff Bonwick	char *name[DTL_TYPES] = { "missing", "partial", "scrub", "outage" };
5608ad4d6dJeff Bonwick	char prefix[256];
5618ad4d6dJeff Bonwick
5628f18d1fGeorge Wilson	spa_vdev_state_enter(spa, SCL_NONE);
5638ad4d6dJeff Bonwick	required = vdev_dtl_required(vd);
5648ad4d6dJeff Bonwick	(void) spa_vdev_state_exit(spa, NULL, 0);
565fa9e406ahrens
566fa9e406ahrens	if (indent == 0)
567fa9e406ahrens		(void) printf("\nDirty time logs:\n\n");
568fa9e406ahrens
5698ad4d6dJeff Bonwick	(void) printf("\t%*s%s [%s]\n", indent, "",
570e14bb32Jeff Bonwick	    vd->vdev_path ? vd->vdev_path :
5718ad4d6dJeff Bonwick	    vd->vdev_parent ? vd->vdev_ops->vdev_op_type : spa_name(spa),
5728ad4d6dJeff Bonwick	    required ? "DTL-required" : "DTL-expendable");
573fa9e406ahrens
5748ad4d6dJeff Bonwick	for (int t = 0; t < DTL_TYPES; t++) {
5758ad4d6dJeff Bonwick		space_map_t *sm = &vd->vdev_dtl[t];
5768ad4d6dJeff Bonwick		if (sm->sm_space == 0)
5778ad4d6dJeff Bonwick			continue;
5788ad4d6dJeff Bonwick		(void) snprintf(prefix, sizeof (prefix), "\t%*s%s",
5798ad4d6dJeff Bonwick		    indent + 2, "", name[t]);
5808ad4d6dJeff Bonwick		mutex_enter(sm->sm_lock);
5818ad4d6dJeff Bonwick		space_map_walk(sm, dump_dtl_seg, (void *)prefix);
5828ad4d6dJeff Bonwick		mutex_exit(sm->sm_lock);
5838ad4d6dJeff Bonwick		if (dump_opt['d'] > 5 && vd->vdev_children == 0)
5848ad4d6dJeff Bonwick			dump_spacemap(spa->spa_meta_objset,
5858ad4d6dJeff Bonwick			    &vd->vdev_dtl_smo, sm);
586fa9e406ahrens	}
587fa9e406ahrens
5888ad4d6dJeff Bonwick	for (int c = 0; c < vd->vdev_children; c++)
589fa9e406ahrens		dump_dtl(vd->vdev_child[c], indent + 4);
590fa9e406ahrens}
591fa9e406ahrens
5928f18d1fGeorge Wilsonstatic void
5938f18d1fGeorge Wilsondump_history(spa_t *spa)
5948f18d1fGeorge Wilson{
5958f18d1fGeorge Wilson	nvlist_t **events = NULL;
5968f18d1fGeorge Wilson	char buf[SPA_MAXBLOCKSIZE];
597e4161dfVictor Latushkin	uint64_t resid, len, off = 0;
5988f18d1fGeorge Wilson	uint_t num = 0;
5998f18d1fGeorge Wilson	int error;
6008f18d1fGeorge Wilson	time_t tsec;
6018f18d1fGeorge Wilson	struct tm t;
6028f18d1fGeorge Wilson	char tbuf[30];
6038f18d1fGeorge Wilson	char internalstr[MAXPATHLEN];
6048f18d1fGeorge Wilson
6058f18d1fGeorge Wilson	do {
606e4161dfVictor Latushkin		len = sizeof (buf);
607e4161dfVictor Latushkin
6088f18d1fGeorge Wilson		if ((error = spa_history_get(spa, &off, &len, buf)) != 0) {
6098f18d1fGeorge Wilson			(void) fprintf(stderr, "Unable to read history: "
6108f18d1fGeorge Wilson			    "error %d\n", error);
6118f18d1fGeorge Wilson			return;
6128f18d1fGeorge Wilson		}
6138f18d1fGeorge Wilson
6148f18d1fGeorge Wilson		if (zpool_history_unpack(buf, len, &resid, &events, &num) != 0)
6158f18d1fGeorge Wilson			break;
6168f18d1fGeorge Wilson
6178f18d1fGeorge Wilson		off -= resid;
6188f18d1fGeorge Wilson	} while (len != 0);
6198f18d1fGeorge Wilson
6208f18d1fGeorge Wilson	(void) printf("\nHistory:\n");
6218f18d1fGeorge Wilson	for (int i = 0; i < num; i++) {
6228f18d1fGeorge Wilson		uint64_t time, txg, ievent;
6238f18d1fGeorge Wilson		char *cmd, *intstr;
6248f18d1fGeorge Wilson
6258f18d1fGeorge Wilson		if (nvlist_lookup_uint64(events[i], ZPOOL_HIST_TIME,
6268f18d1fGeorge Wilson		    &time) != 0)
6278f18d1fGeorge Wilson			continue;
6288f18d1fGeorge Wilson		if (nvlist_lookup_string(events[i], ZPOOL_HIST_CMD,
6298f18d1fGeorge Wilson		    &cmd) != 0) {
6308f18d1fGeorge Wilson			if (nvlist_lookup_uint64(events[i],
6318f18d1fGeorge Wilson			    ZPOOL_HIST_INT_EVENT, &ievent) != 0)
6328f18d1fGeorge Wilson				continue;
6338f18d1fGeorge Wilson			verify(nvlist_lookup_uint64(events[i],
6348f18d1fGeorge Wilson			    ZPOOL_HIST_TXG, &txg) == 0);
6358f18d1fGeorge Wilson			verify(nvlist_lookup_string(events[i],
6368f18d1fGeorge Wilson			    ZPOOL_HIST_INT_STR, &intstr) == 0);
6378f18d1fGeorge Wilson			if (ievent >= LOG_END)
6388f18d1fGeorge Wilson				continue;
6398f18d1fGeorge Wilson
6408f18d1fGeorge Wilson			(void) snprintf(internalstr,
6418f18d1fGeorge Wilson			    sizeof (internalstr),
6428f18d1fGeorge Wilson			    "[internal %s txg:%lld] %s",
6438f18d1fGeorge Wilson			    hist_event_table[ievent], txg,
6448f18d1fGeorge Wilson			    intstr);
6458f18d1fGeorge Wilson			cmd = internalstr;
6468f18d1fGeorge Wilson		}
6478f18d1fGeorge Wilson		tsec = time;
6488f18d1fGeorge Wilson		(void) localtime_r(&tsec, &t);
6498f18d1fGeorge Wilson		(void) strftime(tbuf, sizeof (tbuf), "%F.%T", &t);
6508f18d1fGeorge Wilson		(void) printf("%s %s\n", tbuf, cmd);
6518f18d1fGeorge Wilson	}
6528f18d1fGeorge Wilson}
6538f18d1fGeorge Wilson
654fa9e406ahrens/*ARGSUSED*/
655fa9e406ahrensstatic void
656fa9e406ahrensdump_dnode(objset_t *os, uint64_t object, void *data, size_t size)
657fa9e406ahrens{
658fa9e406ahrens}
659fa9e406ahrens
660fa9e406ahrensstatic uint64_t
66188b7b0fMatthew Ahrensblkid2offset(const dnode_phys_t *dnp, int level, uint64_t blkid)
662fa9e406ahrens{
663fa9e406ahrens	if (level < 0)
664fa9e406ahrens		return (blkid);
665fa9e406ahrens
666fa9e406ahrens	return ((blkid << (level * (dnp->dn_indblkshift - SPA_BLKPTRSHIFT))) *
667fa9e406ahrens	    dnp->dn_datablkszsec << SPA_MINBLOCKSHIFT);
668fa9e406ahrens}
669fa9e406ahrens
67044cd46cbillmstatic void
67144cd46cbillmsprintf_blkptr_compact(char *blkbuf, blkptr_t *bp, int alldvas)
67244cd46cbillm{
67344cd46cbillm	dva_t *dva = bp->blk_dva;
67444cd46cbillm	int ndvas = alldvas ? BP_GET_NDVAS(bp) : 1;
67544cd46cbillm	int i;
67644cd46cbillm
67744cd46cbillm	blkbuf[0] = '\0';
67844cd46cbillm
67944cd46cbillm	for (i = 0; i < ndvas; i++)
68044cd46cbillm		(void) sprintf(blkbuf + strlen(blkbuf), "%llu:%llx:%llx ",
68144cd46cbillm		    (u_longlong_t)DVA_GET_VDEV(&dva[i]),
68244cd46cbillm		    (u_longlong_t)DVA_GET_OFFSET(&dva[i]),
68344cd46cbillm		    (u_longlong_t)DVA_GET_ASIZE(&dva[i]));
68444cd46cbillm
68544cd46cbillm	(void) sprintf(blkbuf + strlen(blkbuf), "%llxL/%llxP F=%llu B=%llu",
68644cd46cbillm	    (u_longlong_t)BP_GET_LSIZE(bp),
68744cd46cbillm	    (u_longlong_t)BP_GET_PSIZE(bp),
68844cd46cbillm	    (u_longlong_t)bp->blk_fill,
68944cd46cbillm	    (u_longlong_t)bp->blk_birth);
69044cd46cbillm}
69144cd46cbillm
69288b7b0fMatthew Ahrensstatic void
69388b7b0fMatthew Ahrensprint_indirect(blkptr_t *bp, const zbookmark_t *zb,
69488b7b0fMatthew Ahrens    const dnode_phys_t *dnp)
695fa9e406ahrens{
69688b7b0fMatthew Ahrens	char blkbuf[BP_SPRINTF_LEN];
697fa9e406ahrens	int l;
698fa9e406ahrens
69988b7b0fMatthew Ahrens	ASSERT3U(BP_GET_TYPE(bp), ==, dnp->dn_type);
70088b7b0fMatthew Ahrens	ASSERT3U(BP_GET_LEVEL(bp), ==, zb->zb_level);
701fa9e406ahrens
70288b7b0fMatthew Ahrens	(void) printf("%16llx ",
703fa9e406ahrens	    (u_longlong_t)blkid2offset(dnp, zb->zb_level, zb->zb_blkid));
704fa9e406ahrens
705fa9e406ahrens	ASSERT(zb->zb_level >= 0);
706fa9e406ahrens
707fa9e406ahrens	for (l = dnp->dn_nlevels - 1; l >= -1; l--) {
708fa9e406ahrens		if (l == zb->zb_level) {
70988b7b0fMatthew Ahrens			(void) printf("L%llx", (u_longlong_t)zb->zb_level);
710fa9e406ahrens		} else {
71188b7b0fMatthew Ahrens			(void) printf(" ");
712fa9e406ahrens		}
713fa9e406ahrens	}
714fa9e406ahrens
71588b7b0fMatthew Ahrens	sprintf_blkptr_compact(blkbuf, bp, dump_opt['d'] > 5 ? 1 : 0);
71688b7b0fMatthew Ahrens	(void) printf("%s\n", blkbuf);
71788b7b0fMatthew Ahrens}
71888b7b0fMatthew Ahrens
71988b7b0fMatthew Ahrens#define	SET_BOOKMARK(zb, objset, object, level, blkid)  \
72088b7b0fMatthew Ahrens{                                                       \
72188b7b0fMatthew Ahrens	(zb)->zb_objset = objset;                       \
72288b7b0fMatthew Ahrens	(zb)->zb_object = object;                       \
72388b7b0fMatthew Ahrens	(zb)->zb_level = level;                         \
72488b7b0fMatthew Ahrens	(zb)->zb_blkid = blkid;                         \
72588b7b0fMatthew Ahrens}
72688b7b0fMatthew Ahrens
72788b7b0fMatthew Ahrensstatic int
72888b7b0fMatthew Ahrensvisit_indirect(spa_t *spa, const dnode_phys_t *dnp,
72988b7b0fMatthew Ahrens    blkptr_t *bp, const zbookmark_t *zb)
73088b7b0fMatthew Ahrens{
731e4161dfVictor Latushkin	int err = 0;
73288b7b0fMatthew Ahrens
73388b7b0fMatthew Ahrens	if (bp->blk_birth == 0)
73488b7b0fMatthew Ahrens		return (0);
73588b7b0fMatthew Ahrens
73688b7b0fMatthew Ahrens	print_indirect(bp, zb, dnp);
73788b7b0fMatthew Ahrens
73888b7b0fMatthew Ahrens	if (BP_GET_LEVEL(bp) > 0) {
73988b7b0fMatthew Ahrens		uint32_t flags = ARC_WAIT;
74088b7b0fMatthew Ahrens		int i;
74188b7b0fMatthew Ahrens		blkptr_t *cbp;
74288b7b0fMatthew Ahrens		int epb = BP_GET_LSIZE(bp) >> SPA_BLKPTRSHIFT;
74388b7b0fMatthew Ahrens		arc_buf_t *buf;
74488b7b0fMatthew Ahrens		uint64_t fill = 0;
74588b7b0fMatthew Ahrens
74688b7b0fMatthew Ahrens		err = arc_read_nolock(NULL, spa, bp, arc_getbuf_func, &buf,
74788b7b0fMatthew Ahrens		    ZIO_PRIORITY_ASYNC_READ, ZIO_FLAG_CANFAIL, &flags, zb);
74888b7b0fMatthew Ahrens		if (err)
74988b7b0fMatthew Ahrens			return (err);
75088b7b0fMatthew Ahrens
75188b7b0fMatthew Ahrens		/* recursively visit blocks below this */
75288b7b0fMatthew Ahrens		cbp = buf->b_data;
75388b7b0fMatthew Ahrens		for (i = 0; i < epb; i++, cbp++) {
75488b7b0fMatthew Ahrens			zbookmark_t czb;
75588b7b0fMatthew Ahrens
75688b7b0fMatthew Ahrens			SET_BOOKMARK(&czb, zb->zb_objset, zb->zb_object,
75788b7b0fMatthew Ahrens			    zb->zb_level - 1,
75888b7b0fMatthew Ahrens			    zb->zb_blkid * epb + i);
75988b7b0fMatthew Ahrens			err = visit_indirect(spa, dnp, cbp, &czb);
76088b7b0fMatthew Ahrens			if (err)
76188b7b0fMatthew Ahrens				break;
76288b7b0fMatthew Ahrens			fill += cbp->blk_fill;
76388b7b0fMatthew Ahrens		}
7648ad4d6dJeff Bonwick		if (!err)
7658ad4d6dJeff Bonwick			ASSERT3U(fill, ==, bp->blk_fill);
76688b7b0fMatthew Ahrens		(void) arc_buf_remove_ref(buf, &buf);
767fa9e406ahrens	}
768fa9e406ahrens
76988b7b0fMatthew Ahrens	return (err);
770fa9e406ahrens}
771fa9e406ahrens
772fa9e406ahrens/*ARGSUSED*/
773fa9e406ahrensstatic void
77488b7b0fMatthew Ahrensdump_indirect(dnode_t *dn)
775fa9e406ahrens{
77688b7b0fMatthew Ahrens	dnode_phys_t *dnp = dn->dn_phys;
77788b7b0fMatthew Ahrens	int j;
77888b7b0fMatthew Ahrens	zbookmark_t czb;
779fa9e406ahrens
780fa9e406ahrens	(void) printf("Indirect blocks:\n");
781fa9e406ahrens
782503ad85Matthew Ahrens	SET_BOOKMARK(&czb, dmu_objset_id(dn->dn_objset),
78388b7b0fMatthew Ahrens	    dn->dn_object, dnp->dn_nlevels - 1, 0);
78488b7b0fMatthew Ahrens	for (j = 0; j < dnp->dn_nblkptr; j++) {
78588b7b0fMatthew Ahrens		czb.zb_blkid = j;
786503ad85Matthew Ahrens		(void) visit_indirect(dmu_objset_spa(dn->dn_objset), dnp,
78788b7b0fMatthew Ahrens		    &dnp->dn_blkptr[j], &czb);
78888b7b0fMatthew Ahrens	}
789fa9e406ahrens
790fa9e406ahrens	(void) printf("\n");
791fa9e406ahrens}
792fa9e406ahrens
793fa9e406ahrens/*ARGSUSED*/
794fa9e406ahrensstatic void
795fa9e406ahrensdump_dsl_dir(objset_t *os, uint64_t object, void *data, size_t size)
796fa9e406ahrens{
797fa9e406ahrens	dsl_dir_phys_t *dd = data;
798fa9e406ahrens	time_t crtime;
79974e7dc9Matthew Ahrens	char nice[6];
800fa9e406ahrens
801fa9e406ahrens	if (dd == NULL)
802fa9e406ahrens		return;
803fa9e406ahrens
804da6c28aamw	ASSERT3U(size, >=, sizeof (dsl_dir_phys_t));
805fa9e406ahrens
806fa9e406ahrens	crtime = dd->dd_creation_time;
807fa9e406ahrens	(void) printf("\t\tcreation_time = %s", ctime(&crtime));
808fa9e406ahrens	(void) printf("\t\thead_dataset_obj = %llu\n",
809fa9e406ahrens	    (u_longlong_t)dd->dd_head_dataset_obj);
810fa9e406ahrens	(void) printf("\t\tparent_dir_obj = %llu\n",
811fa9e406ahrens	    (u_longlong_t)dd->dd_parent_obj);
8123cb34c6ahrens	(void) printf("\t\torigin_obj = %llu\n",
8133cb34c6ahrens	    (u_longlong_t)dd->dd_origin_obj);
814fa9e406ahrens	(void) printf("\t\tchild_dir_zapobj = %llu\n",
815fa9e406ahrens	    (u_longlong_t)dd->dd_child_dir_zapobj);
81674e7dc9Matthew Ahrens	nicenum(dd->dd_used_bytes, nice);
81774e7dc9Matthew Ahrens	(void) printf("\t\tused_bytes = %s\n", nice);
81874e7dc9Matthew Ahrens	nicenum(dd->dd_compressed_bytes, nice);
81974e7dc9Matthew Ahrens	(void) printf("\t\tcompressed_bytes = %s\n", nice);
82074e7dc9Matthew Ahrens	nicenum(dd->dd_uncompressed_bytes, nice);
82174e7dc9Matthew Ahrens	(void) printf("\t\tuncompressed_bytes = %s\n", nice);
82274e7dc9Matthew Ahrens	nicenum(dd->dd_quota, nice);
82374e7dc9Matthew Ahrens	(void) printf("\t\tquota = %s\n", nice);
82474e7dc9Matthew Ahrens	nicenum(dd->dd_reserved, nice);
82574e7dc9Matthew Ahrens	(void) printf("\t\treserved = %s\n", nice);
826fa9e406ahrens	(void) printf("\t\tprops_zapobj = %llu\n",
827fa9e406ahrens	    (u_longlong_t)dd->dd_props_zapobj);
828ecd6cf8marks	(void) printf("\t\tdeleg_zapobj = %llu\n",
829ecd6cf8marks	    (u_longlong_t)dd->dd_deleg_zapobj);
83074e7dc9Matthew Ahrens	(void) printf("\t\tflags = %llx\n",
83174e7dc9Matthew Ahrens	    (u_longlong_t)dd->dd_flags);
83274e7dc9Matthew Ahrens
83374e7dc9Matthew Ahrens#define	DO(which) \
83474e7dc9Matthew Ahrens	nicenum(dd->dd_used_breakdown[DD_USED_ ## which], nice); \
83574e7dc9Matthew Ahrens	(void) printf("\t\tused_breakdown[" #which "] = %s\n", nice)
83674e7dc9Matthew Ahrens	DO(HEAD);
83774e7dc9Matthew Ahrens	DO(SNAP);
83874e7dc9Matthew Ahrens	DO(CHILD);
83974e7dc9Matthew Ahrens	DO(CHILD_RSRV);
84074e7dc9Matthew Ahrens	DO(REFRSRV);
84174e7dc9Matthew Ahrens#undef DO
842fa9e406ahrens}
843fa9e406ahrens
844fa9e406ahrens/*ARGSUSED*/
845fa9e406ahrensstatic void
846fa9e406ahrensdump_dsl_dataset(objset_t *os, uint64_t object, void *data, size_t size)
847fa9e406ahrens{
848fa9e406ahrens	dsl_dataset_phys_t *ds = data;
849fa9e406ahrens	time_t crtime;
850fbabab8maybee	char used[6], compressed[6], uncompressed[6], unique[6];
851fbabab8maybee	char blkbuf[BP_SPRINTF_LEN];
852fa9e406ahrens
853fa9e406ahrens	if (ds == NULL)
854fa9e406ahrens		return;
855fa9e406ahrens
856fa9e406ahrens	ASSERT(size == sizeof (*ds));
857fa9e406ahrens	crtime = ds->ds_creation_time;
858fa9e406ahrens	nicenum(ds->ds_used_bytes, used);
859fa9e406ahrens	nicenum(ds->ds_compressed_bytes, compressed);
860fa9e406ahrens	nicenum(ds->ds_uncompressed_bytes, uncompressed);
861fa9e406ahrens	nicenum(ds->ds_unique_bytes, unique);
862fbabab8maybee	sprintf_blkptr(blkbuf, BP_SPRINTF_LEN, &ds->ds_bp);
863fa9e406ahrens
864088f389ahrens	(void) printf("\t\tdir_obj = %llu\n",
865fa9e406ahrens	    (u_longlong_t)ds->ds_dir_obj);
866fa9e406ahrens	(void) printf("\t\tprev_snap_obj = %llu\n",
867fa9e406ahrens	    (u_longlong_t)ds->ds_prev_snap_obj);
868fa9e406ahrens	(void) printf("\t\tprev_snap_txg = %llu\n",
869fa9e406ahrens	    (u_longlong_t)ds->ds_prev_snap_txg);
870fa9e406ahrens	(void) printf("\t\tnext_snap_obj = %llu\n",
871fa9e406ahrens	    (u_longlong_t)ds->ds_next_snap_obj);
872fa9e406ahrens	(void) printf("\t\tsnapnames_zapobj = %llu\n",
873fa9e406ahrens	    (u_longlong_t)ds->ds_snapnames_zapobj);
874fa9e406ahrens	(void) printf("\t\tnum_children = %llu\n",
875fa9e406ahrens	    (u_longlong_t)ds->ds_num_children);
876842727cChris Kirby	(void) printf("\t\tuserrefs_obj = %llu\n",
877842727cChris Kirby	    (u_longlong_t)ds->ds_userrefs_obj);
878fa9e406ahrens	(void) printf("\t\tcreation_time = %s", ctime(&crtime));
879fa9e406ahrens	(void) printf("\t\tcreation_txg = %llu\n",
880fa9e406ahrens	    (u_longlong_t)ds->ds_creation_txg);
881fa9e406ahrens	(void) printf("\t\tdeadlist_obj = %llu\n",
882fa9e406ahrens	    (u_longlong_t)ds->ds_deadlist_obj);
883fa9e406ahrens	(void) printf("\t\tused_bytes = %s\n", used);
884fa9e406ahrens	(void) printf("\t\tcompressed_bytes = %s\n", compressed);
885fa9e406ahrens	(void) printf("\t\tuncompressed_bytes = %s\n", uncompressed);
886fa9e406ahrens	(void) printf("\t\tunique = %s\n", unique);
887fa9e406ahrens	(void) printf("\t\tfsid_guid = %llu\n",
888fa9e406ahrens	    (u_longlong_t)ds->ds_fsid_guid);
889fa9e406ahrens	(void) printf("\t\tguid = %llu\n",
890fa9e406ahrens	    (u_longlong_t)ds->ds_guid);
89199653d4eschrock	(void) printf("\t\tflags = %llx\n",
89299653d4eschrock	    (u_longlong_t)ds->ds_flags);
893088f389ahrens	(void) printf("\t\tnext_clones_obj = %llu\n",
894088f389ahrens	    (u_longlong_t)ds->ds_next_clones_obj);
895bb0ade0ahrens	(void) printf("\t\tprops_obj = %llu\n",
896bb0ade0ahrens	    (u_longlong_t)ds->ds_props_obj);
897fa9e406ahrens	(void) printf("\t\tbp = %s\n", blkbuf);
898fa9e406ahrens}
899fa9e406ahrens
900fa9e406ahrensstatic void
901fa9e406ahrensdump_bplist(objset_t *mos, uint64_t object, char *name)
902fa9e406ahrens{
903fa9e406ahrens	bplist_t bpl = { 0 };
904fa9e406ahrens	blkptr_t blk, *bp = &blk;
905fa9e406ahrens	uint64_t itor = 0;
90699653d4eschrock	char bytes[6];
90799653d4eschrock	char comp[6];
90899653d4eschrock	char uncomp[6];
909fa9e406ahrens
910fa9e406ahrens	if (dump_opt['d'] < 3)
911fa9e406ahrens		return;
912fa9e406ahrens
913c25056dgw	mutex_init(&bpl.bpl_lock, NULL, MUTEX_DEFAULT, NULL);
914ea8dc4beschrock	VERIFY(0 == bplist_open(&bpl, mos, object));
915fa9e406ahrens	if (bplist_empty(&bpl)) {
916fa9e406ahrens		bplist_close(&bpl);
917c25056dgw		mutex_destroy(&bpl.bpl_lock);
918fa9e406ahrens		return;
919fa9e406ahrens	}
920