zdb.c revision 87e5029a3226958edab1512d6182bc74d8d80c9a
1fa9e406ahrens/*
2fa9e406ahrens * CDDL HEADER START
3fa9e406ahrens *
4fa9e406ahrens * The contents of this file are subject to the terms of the
5fa9e406ahrens * Common Development and Distribution License, Version 1.0 only
6fa9e406ahrens * (the "License").  You may not use this file except in compliance
7fa9e406ahrens * with the License.
8fa9e406ahrens *
9fa9e406ahrens * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10fa9e406ahrens * or http://www.opensolaris.org/os/licensing.
11fa9e406ahrens * See the License for the specific language governing permissions
12fa9e406ahrens * and limitations under the License.
13fa9e406ahrens *
14fa9e406ahrens * When distributing Covered Code, include this CDDL HEADER in each
15fa9e406ahrens * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16fa9e406ahrens * If applicable, add the following below this CDDL HEADER, with the
17fa9e406ahrens * fields enclosed by brackets "[]" replaced with your own identifying
18fa9e406ahrens * information: Portions Copyright [yyyy] [name of copyright owner]
19fa9e406ahrens *
20fa9e406ahrens * CDDL HEADER END
21fa9e406ahrens */
22fa9e406ahrens/*
23fa9e406ahrens * Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
24fa9e406ahrens * Use is subject to license terms.
25fa9e406ahrens */
26fa9e406ahrens
27fa9e406ahrens#pragma ident	"%Z%%M%	%I%	%E% SMI"
28fa9e406ahrens
29fa9e406ahrens#include <stdio.h>
30fa9e406ahrens#include <stdlib.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>
38fa9e406ahrens#include <sys/vdev.h>
39fa9e406ahrens#include <sys/vdev_impl.h>
40fa9e406ahrens#include <sys/metaslab_impl.h>
41fa9e406ahrens#include <sys/dmu_objset.h>
42fa9e406ahrens#include <sys/dsl_dir.h>
43fa9e406ahrens#include <sys/dsl_dataset.h>
44fa9e406ahrens#include <sys/dsl_pool.h>
45fa9e406ahrens#include <sys/dbuf.h>
46fa9e406ahrens#include <sys/zil.h>
47fa9e406ahrens#include <sys/zil_impl.h>
48fa9e406ahrens#include <sys/stat.h>
49fa9e406ahrens#include <sys/resource.h>
50fa9e406ahrens#include <sys/dmu_traverse.h>
51fa9e406ahrens#include <sys/zio_checksum.h>
52fa9e406ahrens#include <sys/zio_compress.h>
53fa9e406ahrens
54fa9e406ahrensconst char cmdname[] = "zdb";
55fa9e406ahrensuint8_t dump_opt[256];
56fa9e406ahrens
57fa9e406ahrenstypedef void object_viewer_t(objset_t *, uint64_t, void *data, size_t size);
58fa9e406ahrens
59fa9e406ahrensextern void dump_intent_log(zilog_t *);
60fa9e406ahrensuint64_t *zopt_object = NULL;
61fa9e406ahrensint zopt_objects = 0;
62fa9e406ahrensint zdb_advance = ADVANCE_PRE;
63fa9e406ahrenszbookmark_t zdb_noread = { 0, 0, ZB_NO_LEVEL, 0 };
64fa9e406ahrens
65fa9e406ahrens/*
66fa9e406ahrens * These libumem hooks provide a reasonable set of defaults for the allocator's
67fa9e406ahrens * debugging facilities.
68fa9e406ahrens */
69fa9e406ahrensconst char *
70fa9e406ahrens_umem_debug_init()
71fa9e406ahrens{
72fa9e406ahrens	return ("default,verbose"); /* $UMEM_DEBUG setting */
73fa9e406ahrens}
74fa9e406ahrens
75fa9e406ahrensconst char *
76fa9e406ahrens_umem_logging_init(void)
77fa9e406ahrens{
78fa9e406ahrens	return ("fail,contents"); /* $UMEM_LOGGING setting */
79fa9e406ahrens}
80fa9e406ahrens
81fa9e406ahrensstatic void
82fa9e406ahrensusage(void)
83fa9e406ahrens{
84fa9e406ahrens	(void) fprintf(stderr,
85fa9e406ahrens	    "Usage: %s [-udibcsvLU] [-O order] [-B os:obj:level:blkid] "
86fa9e406ahrens	    "dataset [object...]\n"
87fa9e406ahrens	    "       %s -C [pool]\n"
88fa9e406ahrens	    "       %s -l dev\n",
89fa9e406ahrens	    cmdname, cmdname, cmdname);
90fa9e406ahrens
91fa9e406ahrens	(void) fprintf(stderr, "	-u uberblock\n");
92fa9e406ahrens	(void) fprintf(stderr, "	-d datasets\n");
93fa9e406ahrens	(void) fprintf(stderr, "        -C cached pool configuration\n");
94fa9e406ahrens	(void) fprintf(stderr, "	-i intent logs\n");
95fa9e406ahrens	(void) fprintf(stderr, "	-b block statistics\n");
96fa9e406ahrens	(void) fprintf(stderr, "	-c checksum all data blocks\n");
97fa9e406ahrens	(void) fprintf(stderr, "	-s report stats on zdb's I/O\n");
98fa9e406ahrens	(void) fprintf(stderr, "	-v verbose (applies to all others)\n");
99fa9e406ahrens	(void) fprintf(stderr, "        -l dump label contents\n");
100fa9e406ahrens	(void) fprintf(stderr, "	-L live pool (allows some errors)\n");
101fa9e406ahrens	(void) fprintf(stderr, "	-O [!]<pre|post|prune|data|holes> "
102fa9e406ahrens	    "visitation order\n");
103fa9e406ahrens	(void) fprintf(stderr, "	-U use zpool.cache in /tmp\n");
104fa9e406ahrens	(void) fprintf(stderr, "	-B objset:object:level:blkid -- "
105fa9e406ahrens	    "simulate bad block\n");
106fa9e406ahrens	(void) fprintf(stderr, "Specify an option more than once (e.g. -bb) "
107fa9e406ahrens	    "to make only that option verbose\n");
108fa9e406ahrens	(void) fprintf(stderr, "Default is to dump everything non-verbosely\n");
109fa9e406ahrens	exit(1);
110fa9e406ahrens}
111fa9e406ahrens
112fa9e406ahrensstatic void
113fa9e406ahrensfatal(const char *fmt, ...)
114fa9e406ahrens{
115fa9e406ahrens	va_list ap;
116fa9e406ahrens
117fa9e406ahrens	va_start(ap, fmt);
118fa9e406ahrens	(void) fprintf(stderr, "%s: ", cmdname);
119fa9e406ahrens	(void) vfprintf(stderr, fmt, ap);
120fa9e406ahrens	va_end(ap);
121fa9e406ahrens	(void) fprintf(stderr, "\n");
122fa9e406ahrens
123fa9e406ahrens	exit(1);
124fa9e406ahrens}
125fa9e406ahrens
126fa9e406ahrensstatic void
127fa9e406ahrensdump_nvlist(nvlist_t *list, int indent)
128fa9e406ahrens{
129fa9e406ahrens	nvpair_t *elem = NULL;
130fa9e406ahrens
131fa9e406ahrens	while ((elem = nvlist_next_nvpair(list, elem)) != NULL) {
132fa9e406ahrens		switch (nvpair_type(elem)) {
133fa9e406ahrens		case DATA_TYPE_STRING:
134fa9e406ahrens			{
135fa9e406ahrens				char *value;
136fa9e406ahrens
137fa9e406ahrens				VERIFY(nvpair_value_string(elem, &value) == 0);
138fa9e406ahrens				(void) printf("%*s%s='%s'\n", indent, "",
139fa9e406ahrens				    nvpair_name(elem), value);
140fa9e406ahrens			}
141fa9e406ahrens			break;
142fa9e406ahrens
143fa9e406ahrens		case DATA_TYPE_UINT64:
144fa9e406ahrens			{
145fa9e406ahrens				uint64_t value;
146fa9e406ahrens
147fa9e406ahrens				VERIFY(nvpair_value_uint64(elem, &value) == 0);
148fa9e406ahrens				(void) printf("%*s%s=%llu\n", indent, "",
149fa9e406ahrens				    nvpair_name(elem), (u_longlong_t)value);
150fa9e406ahrens			}
151fa9e406ahrens			break;
152fa9e406ahrens
153fa9e406ahrens		case DATA_TYPE_NVLIST:
154fa9e406ahrens			{
155fa9e406ahrens				nvlist_t *value;
156fa9e406ahrens
157fa9e406ahrens				VERIFY(nvpair_value_nvlist(elem, &value) == 0);
158fa9e406ahrens				(void) printf("%*s%s\n", indent, "",
159fa9e406ahrens				    nvpair_name(elem));
160fa9e406ahrens				dump_nvlist(value, indent + 4);
161fa9e406ahrens			}
162fa9e406ahrens			break;
163fa9e406ahrens
164fa9e406ahrens		case DATA_TYPE_NVLIST_ARRAY:
165fa9e406ahrens			{
166fa9e406ahrens				nvlist_t **value;
167fa9e406ahrens				uint_t c, count;
168fa9e406ahrens
169fa9e406ahrens				VERIFY(nvpair_value_nvlist_array(elem, &value,
170fa9e406ahrens				    &count) == 0);
171fa9e406ahrens
172fa9e406ahrens				for (c = 0; c < count; c++) {
173fa9e406ahrens					(void) printf("%*s%s[%u]\n", indent, "",
174fa9e406ahrens					    nvpair_name(elem), c);
175fa9e406ahrens					dump_nvlist(value[c], indent + 8);
176fa9e406ahrens				}
177fa9e406ahrens			}
178fa9e406ahrens			break;
179fa9e406ahrens
180fa9e406ahrens		default:
181fa9e406ahrens
182fa9e406ahrens			(void) printf("bad config type %d for %s\n",
183fa9e406ahrens			    nvpair_type(elem), nvpair_name(elem));
184fa9e406ahrens		}
185fa9e406ahrens	}
186fa9e406ahrens}
187fa9e406ahrens
188fa9e406ahrens/* ARGSUSED */
189fa9e406ahrensstatic void
190fa9e406ahrensdump_packed_nvlist(objset_t *os, uint64_t object, void *data, size_t size)
191fa9e406ahrens{
192fa9e406ahrens	nvlist_t *nv;
193fa9e406ahrens	size_t nvsize = *(uint64_t *)data;
194fa9e406ahrens	char *packed = umem_alloc(nvsize, UMEM_NOFAIL);
195fa9e406ahrens
196fa9e406ahrens	dmu_read(os, object, 0, nvsize, packed);
197fa9e406ahrens
198fa9e406ahrens	VERIFY(nvlist_unpack(packed, nvsize, &nv, 0) == 0);
199fa9e406ahrens
200fa9e406ahrens	umem_free(packed, nvsize);
201fa9e406ahrens
202fa9e406ahrens	dump_nvlist(nv, 8);
203fa9e406ahrens
204fa9e406ahrens	nvlist_free(nv);
205fa9e406ahrens}
206fa9e406ahrens
207fa9e406ahrensconst char dump_zap_stars[] = "****************************************";
208fa9e406ahrensconst int dump_zap_width = sizeof (dump_zap_stars) - 1;
209fa9e406ahrens
210fa9e406ahrensstatic void
211fa9e406ahrensdump_zap_histogram(uint64_t histo[ZAP_HISTOGRAM_SIZE])
212fa9e406ahrens{
213fa9e406ahrens	int i;
214fa9e406ahrens	int minidx = ZAP_HISTOGRAM_SIZE - 1;
215fa9e406ahrens	int maxidx = 0;
216fa9e406ahrens	uint64_t max = 0;
217fa9e406ahrens
218fa9e406ahrens	for (i = 0; i < ZAP_HISTOGRAM_SIZE; i++) {
219fa9e406ahrens		if (histo[i] > max)
220fa9e406ahrens			max = histo[i];
221fa9e406ahrens		if (histo[i] > 0 && i > maxidx)
222fa9e406ahrens			maxidx = i;
223fa9e406ahrens		if (histo[i] > 0 && i < minidx)
224fa9e406ahrens			minidx = i;
225fa9e406ahrens	}
226fa9e406ahrens
227fa9e406ahrens	if (max < dump_zap_width)
228fa9e406ahrens		max = dump_zap_width;
229fa9e406ahrens
230fa9e406ahrens	for (i = minidx; i <= maxidx; i++)
231fa9e406ahrens		(void) printf("\t\t\t%u: %6llu %s\n", i, (u_longlong_t)histo[i],
232fa9e406ahrens		    &dump_zap_stars[(max - histo[i]) * dump_zap_width / max]);
233fa9e406ahrens}
234fa9e406ahrens
235fa9e406ahrensstatic void
236fa9e406ahrensdump_zap_stats(objset_t *os, uint64_t object)
237fa9e406ahrens{
238fa9e406ahrens	int error;
239fa9e406ahrens	zap_stats_t zs;
240fa9e406ahrens
241fa9e406ahrens	error = zap_get_stats(os, object, &zs);
242fa9e406ahrens	if (error)
243fa9e406ahrens		return;
244fa9e406ahrens
245fa9e406ahrens	if (zs.zs_ptrtbl_len == 0) {
246fa9e406ahrens		ASSERT(zs.zs_num_blocks == 1);
247fa9e406ahrens		(void) printf("\tmicrozap: %llu bytes, %llu entries\n",
248fa9e406ahrens		    (u_longlong_t)zs.zs_blocksize,
249fa9e406ahrens		    (u_longlong_t)zs.zs_num_entries);
250fa9e406ahrens		return;
251fa9e406ahrens	}
252fa9e406ahrens
253fa9e406ahrens	(void) printf("\tFat ZAP stats:\n");
254fa9e406ahrens	(void) printf("\t\tPointer table: %llu elements\n",
255fa9e406ahrens	    (u_longlong_t)zs.zs_ptrtbl_len);
256fa9e406ahrens	(void) printf("\t\tZAP entries: %llu\n",
257fa9e406ahrens	    (u_longlong_t)zs.zs_num_entries);
258fa9e406ahrens	(void) printf("\t\tLeaf blocks: %llu\n",
259fa9e406ahrens	    (u_longlong_t)zs.zs_num_leafs);
260fa9e406ahrens	(void) printf("\t\tTotal blocks: %llu\n",
261fa9e406ahrens	    (u_longlong_t)zs.zs_num_blocks);
262fa9e406ahrens	(void) printf("\t\tOversize blocks: %llu\n",
263fa9e406ahrens	    (u_longlong_t)zs.zs_num_blocks_large);
264fa9e406ahrens
265fa9e406ahrens	(void) printf("\t\tLeafs with 2^n pointers:\n");
266fa9e406ahrens	dump_zap_histogram(zs.zs_leafs_with_2n_pointers);
267fa9e406ahrens
268fa9e406ahrens	(void) printf("\t\tLeafs with n chained:\n");
269fa9e406ahrens	dump_zap_histogram(zs.zs_leafs_with_n_chained);
270fa9e406ahrens
271fa9e406ahrens	(void) printf("\t\tBlocks with n*5 entries:\n");
272fa9e406ahrens	dump_zap_histogram(zs.zs_blocks_with_n5_entries);
273fa9e406ahrens
274fa9e406ahrens	(void) printf("\t\tBlocks n/10 full:\n");
275fa9e406ahrens	dump_zap_histogram(zs.zs_blocks_n_tenths_full);
276fa9e406ahrens
277fa9e406ahrens	(void) printf("\t\tEntries with n chunks:\n");
278fa9e406ahrens	dump_zap_histogram(zs.zs_entries_using_n_chunks);
279fa9e406ahrens
280fa9e406ahrens	(void) printf("\t\tBuckets with n entries:\n");
281fa9e406ahrens	dump_zap_histogram(zs.zs_buckets_with_n_entries);
282fa9e406ahrens}
283fa9e406ahrens
284fa9e406ahrens/*ARGSUSED*/
285fa9e406ahrensstatic void
286fa9e406ahrensdump_none(objset_t *os, uint64_t object, void *data, size_t size)
287fa9e406ahrens{
288fa9e406ahrens}
289fa9e406ahrens
290fa9e406ahrens/*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
352fa9e406ahrensstatic void
353fa9e406ahrensdump_spacemap(objset_t *os, space_map_obj_t *smo, space_map_t *sm)
354fa9e406ahrens{
355fa9e406ahrens	uint64_t alloc, offset, entry;
356fa9e406ahrens	int mapshift = sm->sm_shift;
357fa9e406ahrens	uint64_t mapstart = sm->sm_start;
358fa9e406ahrens	char *ddata[] = { "ALLOC", "FREE", "CONDENSE", "INVALID" };
359fa9e406ahrens
360fa9e406ahrens	if (smo->smo_object == 0)
361fa9e406ahrens		return;
362fa9e406ahrens
363fa9e406ahrens	/*
364fa9e406ahrens	 * Print out the freelist entries in both encoded and decoded form.
365fa9e406ahrens	 */
366fa9e406ahrens	alloc = 0;
367fa9e406ahrens	for (offset = 0; offset < smo->smo_objsize; offset += sizeof (entry)) {
368fa9e406ahrens		dmu_read(os, smo->smo_object, offset, sizeof (entry), &entry);
369fa9e406ahrens		if (SM_DEBUG_DECODE(entry)) {
370fa9e406ahrens			(void) printf("\t\t[%4llu] %s: txg %llu, pass %llu\n",
371fa9e406ahrens			    (u_longlong_t)(offset / sizeof (entry)),
372fa9e406ahrens			    ddata[SM_DEBUG_ACTION_DECODE(entry)],
373fa9e406ahrens			    SM_DEBUG_TXG_DECODE(entry),
374fa9e406ahrens			    SM_DEBUG_SYNCPASS_DECODE(entry));
375fa9e406ahrens		} else {
376fa9e406ahrens			(void) printf("\t\t[%4llu]    %c  range:"
377fa9e406ahrens			    " %08llx-%08llx  size: %06llx\n",
378fa9e406ahrens			    (u_longlong_t)(offset / sizeof (entry)),
379fa9e406ahrens			    SM_TYPE_DECODE(entry) == SM_ALLOC ? 'A' : 'F',
380fa9e406ahrens			    (SM_OFFSET_DECODE(entry) << mapshift) + mapstart,
381fa9e406ahrens			    (SM_OFFSET_DECODE(entry) << mapshift) + mapstart +
382fa9e406ahrens			    (SM_RUN_DECODE(entry) << mapshift),
383fa9e406ahrens			    (SM_RUN_DECODE(entry) << mapshift));
384fa9e406ahrens			if (SM_TYPE_DECODE(entry) == SM_ALLOC)
385fa9e406ahrens				alloc += SM_RUN_DECODE(entry) << mapshift;
386fa9e406ahrens			else
387fa9e406ahrens				alloc -= SM_RUN_DECODE(entry) << mapshift;
388fa9e406ahrens		}
389fa9e406ahrens	}
390fa9e406ahrens	if (alloc != smo->smo_alloc) {
391fa9e406ahrens		(void) printf("space_map_object alloc (%llu) INCONSISTENT "
392fa9e406ahrens		    "with space map summary (%llu)\n",
393fa9e406ahrens		    (u_longlong_t)smo->smo_alloc, (u_longlong_t)alloc);
394fa9e406ahrens	}
395fa9e406ahrens}
396fa9e406ahrens
397fa9e406ahrensstatic void
398fa9e406ahrensdump_metaslab(metaslab_t *msp)
399fa9e406ahrens{
400fa9e406ahrens	char freebuf[5];
401fa9e406ahrens	space_map_obj_t *smo = msp->ms_smo;
402fa9e406ahrens	vdev_t *vd = msp->ms_group->mg_vd;
403fa9e406ahrens	spa_t *spa = vd->vdev_spa;
404fa9e406ahrens
405fa9e406ahrens	nicenum(msp->ms_map.sm_size - smo->smo_alloc, freebuf);
406fa9e406ahrens
407fa9e406ahrens	if (dump_opt['d'] <= 5) {
408fa9e406ahrens		(void) printf("\t%10llx   %10llu   %5s\n",
409fa9e406ahrens		    (u_longlong_t)msp->ms_map.sm_start,
410fa9e406ahrens		    (u_longlong_t)smo->smo_object,
411fa9e406ahrens		    freebuf);
412fa9e406ahrens		return;
413fa9e406ahrens	}
414fa9e406ahrens
415fa9e406ahrens	(void) printf(
416fa9e406ahrens	    "\tvdev %llu   offset %08llx   spacemap %4llu   free %5s\n",
417fa9e406ahrens	    (u_longlong_t)vd->vdev_id, (u_longlong_t)msp->ms_map.sm_start,
418fa9e406ahrens	    (u_longlong_t)smo->smo_object, freebuf);
419fa9e406ahrens
420fa9e406ahrens	ASSERT(msp->ms_map.sm_size == (1ULL << vd->vdev_ms_shift));
421fa9e406ahrens
422fa9e406ahrens	dump_spacemap(spa->spa_meta_objset, smo, &msp->ms_map);
423fa9e406ahrens}
424fa9e406ahrens
425fa9e406ahrensstatic void
426fa9e406ahrensdump_metaslabs(spa_t *spa)
427fa9e406ahrens{
428fa9e406ahrens	vdev_t *rvd = spa->spa_root_vdev;
429fa9e406ahrens	vdev_t *vd;
430fa9e406ahrens	int c, m;
431fa9e406ahrens
432fa9e406ahrens	(void) printf("\nMetaslabs:\n");
433fa9e406ahrens
434fa9e406ahrens	for (c = 0; c < rvd->vdev_children; c++) {
435fa9e406ahrens		vd = rvd->vdev_child[c];
436fa9e406ahrens
437fa9e406ahrens		spa_config_enter(spa, RW_READER);
438fa9e406ahrens		(void) printf("\n    vdev %llu = %s\n\n",
439fa9e406ahrens		    (u_longlong_t)vd->vdev_id, vdev_description(vd));
440fa9e406ahrens		spa_config_exit(spa);
441fa9e406ahrens
442fa9e406ahrens		if (dump_opt['d'] <= 5) {
443fa9e406ahrens			(void) printf("\t%10s   %10s   %5s\n",
444fa9e406ahrens			    "offset", "spacemap", "free");
445fa9e406ahrens			(void) printf("\t%10s   %10s   %5s\n",
446fa9e406ahrens			    "------", "--------", "----");
447fa9e406ahrens		}
448fa9e406ahrens		for (m = 0; m < vd->vdev_ms_count; m++)
449fa9e406ahrens			dump_metaslab(vd->vdev_ms[m]);
450fa9e406ahrens		(void) printf("\n");
451fa9e406ahrens	}
452fa9e406ahrens}
453fa9e406ahrens
454fa9e406ahrensstatic void
455fa9e406ahrensdump_dtl(vdev_t *vd, int indent)
456fa9e406ahrens{
457fa9e406ahrens	avl_tree_t *t = &vd->vdev_dtl_map.sm_root;
458fa9e406ahrens	spa_t *spa = vd->vdev_spa;
459fa9e406ahrens	space_seg_t *ss;
460fa9e406ahrens	vdev_t *pvd;
461fa9e406ahrens	int c;
462fa9e406ahrens
463fa9e406ahrens	if (indent == 0)
464fa9e406ahrens		(void) printf("\nDirty time logs:\n\n");
465fa9e406ahrens
466fa9e406ahrens	spa_config_enter(spa, RW_READER);
467fa9e406ahrens	(void) printf("\t%*s%s\n", indent, "", vdev_description(vd));
468fa9e406ahrens	spa_config_exit(spa);
469fa9e406ahrens
470fa9e406ahrens	for (ss = avl_first(t); ss; ss = AVL_NEXT(t, ss)) {
471fa9e406ahrens		/*
472fa9e406ahrens		 * Everything in this DTL must appear in all parent DTL unions.
473fa9e406ahrens		 */
474fa9e406ahrens		for (pvd = vd; pvd; pvd = pvd->vdev_parent)
475fa9e406ahrens			ASSERT(vdev_dtl_contains(&pvd->vdev_dtl_map,
476fa9e406ahrens			    ss->ss_start, ss->ss_end - ss->ss_start));
477fa9e406ahrens		(void) printf("\t%*soutage [%llu,%llu] length %llu\n",
478fa9e406ahrens		    indent, "",
479fa9e406ahrens		    (u_longlong_t)ss->ss_start,
480fa9e406ahrens		    (u_longlong_t)ss->ss_end - 1,
481fa9e406ahrens		    (u_longlong_t)ss->ss_end - ss->ss_start);
482fa9e406ahrens	}
483fa9e406ahrens
484fa9e406ahrens	(void) printf("\n");
485fa9e406ahrens
486fa9e406ahrens	if (dump_opt['d'] > 5 && vd->vdev_children == 0) {
487fa9e406ahrens		dump_spacemap(vd->vdev_spa->spa_meta_objset, &vd->vdev_dtl,
488fa9e406ahrens		    &vd->vdev_dtl_map);
489fa9e406ahrens		(void) printf("\n");
490fa9e406ahrens	}
491fa9e406ahrens
492fa9e406ahrens	for (c = 0; c < vd->vdev_children; c++)
493fa9e406ahrens		dump_dtl(vd->vdev_child[c], indent + 4);
494fa9e406ahrens}
495fa9e406ahrens
496fa9e406ahrens/*ARGSUSED*/
497fa9e406ahrensstatic void
498fa9e406ahrensdump_dnode(objset_t *os, uint64_t object, void *data, size_t size)
499fa9e406ahrens{
500fa9e406ahrens}
501fa9e406ahrens
502fa9e406ahrensstatic uint64_t
503fa9e406ahrensblkid2offset(dnode_phys_t *dnp, int level, uint64_t blkid)
504fa9e406ahrens{
505fa9e406ahrens	if (level < 0)
506fa9e406ahrens		return (blkid);
507fa9e406ahrens
508fa9e406ahrens	return ((blkid << (level * (dnp->dn_indblkshift - SPA_BLKPTRSHIFT))) *
509fa9e406ahrens	    dnp->dn_datablkszsec << SPA_MINBLOCKSHIFT);
510fa9e406ahrens}
511fa9e406ahrens
512fa9e406ahrens/* ARGSUSED */
513fa9e406ahrensstatic int
514fa9e406ahrenszdb_indirect_cb(traverse_blk_cache_t *bc, spa_t *spa, void *a)
515fa9e406ahrens{
516fa9e406ahrens	zbookmark_t *zb = &bc->bc_bookmark;
517fa9e406ahrens	blkptr_t *bp = &bc->bc_blkptr;
518fa9e406ahrens	dva_t *dva = &bp->blk_dva[0];
519fa9e406ahrens	void *data = bc->bc_data;
520fa9e406ahrens	dnode_phys_t *dnp = bc->bc_dnode;
521fa9e406ahrens	char buffer[300];
522fa9e406ahrens	int l;
523fa9e406ahrens
524fa9e406ahrens	if (bc->bc_errno) {
525fa9e406ahrens		(void) sprintf(buffer,
526fa9e406ahrens		    "Error %d reading <%llu, %llu, %d, %llu>: ",
527fa9e406ahrens		    bc->bc_errno,
528fa9e406ahrens		    (u_longlong_t)zb->zb_objset,
529fa9e406ahrens		    (u_longlong_t)zb->zb_object,
530fa9e406ahrens		    zb->zb_level,
531fa9e406ahrens		    (u_longlong_t)zb->zb_blkid);
532fa9e406ahrens		goto out;
533fa9e406ahrens	}
534fa9e406ahrens
535fa9e406ahrens	if (zb->zb_level == -1) {
536fa9e406ahrens		ASSERT3U(BP_GET_TYPE(bp), ==, DMU_OT_OBJSET);
537fa9e406ahrens		ASSERT3U(BP_GET_LEVEL(bp), ==, 0);
538fa9e406ahrens	} else {
539fa9e406ahrens		ASSERT3U(BP_GET_TYPE(bp), ==, dnp->dn_type);
540fa9e406ahrens		ASSERT3U(BP_GET_LEVEL(bp), ==, zb->zb_level);
541fa9e406ahrens	}
542fa9e406ahrens
543fa9e406ahrens	if (zb->zb_level > 0) {
544fa9e406ahrens		uint64_t fill = 0;
545fa9e406ahrens		blkptr_t *bpx, *bpend;
546fa9e406ahrens
547fa9e406ahrens		for (bpx = data, bpend = bpx + BP_GET_LSIZE(bp) / sizeof (*bpx);
548fa9e406ahrens		    bpx < bpend; bpx++) {
549fa9e406ahrens			if (bpx->blk_birth != 0) {
550fa9e406ahrens				ASSERT(bpx->blk_fill > 0);
551fa9e406ahrens				fill += bpx->blk_fill;
552fa9e406ahrens			} else {
553fa9e406ahrens				ASSERT(bpx->blk_fill == 0);
554fa9e406ahrens			}
555fa9e406ahrens		}
556fa9e406ahrens		ASSERT3U(fill, ==, bp->blk_fill);
557fa9e406ahrens	}
558fa9e406ahrens
559fa9e406ahrens	if (zb->zb_level == 0 && dnp->dn_type == DMU_OT_DNODE) {
560fa9e406ahrens		uint64_t fill = 0;
561fa9e406ahrens		dnode_phys_t *dnx, *dnend;
562fa9e406ahrens
563fa9e406ahrens		for (dnx = data, dnend = dnx + (BP_GET_LSIZE(bp)>>DNODE_SHIFT);
564fa9e406ahrens		    dnx < dnend; dnx++) {
565fa9e406ahrens			if (dnx->dn_type != DMU_OT_NONE)
566fa9e406ahrens				fill++;
567fa9e406ahrens		}
568fa9e406ahrens		ASSERT3U(fill, ==, bp->blk_fill);
569fa9e406ahrens	}
570fa9e406ahrens
571fa9e406ahrens	(void) sprintf(buffer, "%16llx ",
572fa9e406ahrens	    (u_longlong_t)blkid2offset(dnp, zb->zb_level, zb->zb_blkid));
573fa9e406ahrens
574fa9e406ahrens	ASSERT(zb->zb_level >= 0);
575fa9e406ahrens
576fa9e406ahrens	for (l = dnp->dn_nlevels - 1; l >= -1; l--) {
577fa9e406ahrens		if (l == zb->zb_level) {
578fa9e406ahrens			(void) sprintf(buffer + strlen(buffer), "L%x",
579fa9e406ahrens			    zb->zb_level);
580fa9e406ahrens		} else {
581fa9e406ahrens			(void) sprintf(buffer + strlen(buffer), " ");
582fa9e406ahrens		}
583fa9e406ahrens	}
584fa9e406ahrens
585fa9e406ahrensout:
586fa9e406ahrens	if (bp->blk_birth == 0) {
587fa9e406ahrens		(void) sprintf(buffer + strlen(buffer), "<hole>");
588fa9e406ahrens		(void) printf("%s\n", buffer);
589fa9e406ahrens	} else {
590fa9e406ahrens		// XXBP - Need to print number of active BPs here
591fa9e406ahrens		(void) sprintf(buffer + strlen(buffer),
592fa9e406ahrens		    "vdev=%llu off=%llx %llxL/%llxP/%llxA F=%llu B=%llu",
593fa9e406ahrens		    (u_longlong_t)DVA_GET_VDEV(dva),
594fa9e406ahrens		    (u_longlong_t)DVA_GET_OFFSET(dva),
595fa9e406ahrens		    (u_longlong_t)BP_GET_LSIZE(bp),
596fa9e406ahrens		    (u_longlong_t)BP_GET_PSIZE(bp),
597fa9e406ahrens		    (u_longlong_t)DVA_GET_ASIZE(dva),
598fa9e406ahrens		    (u_longlong_t)bp->blk_fill,
599fa9e406ahrens		    (u_longlong_t)bp->blk_birth);
600fa9e406ahrens
601fa9e406ahrens		(void) printf("%s\n", buffer);
602fa9e406ahrens	}
603fa9e406ahrens
604fa9e406ahrens	return (bc->bc_errno ? ERESTART : 0);
605fa9e406ahrens}
606fa9e406ahrens
607fa9e406ahrens/*ARGSUSED*/
608fa9e406ahrensstatic void
609fa9e406ahrensdump_indirect(objset_t *os, uint64_t object, void *data, size_t size)
610fa9e406ahrens{
611fa9e406ahrens	traverse_handle_t *th;
612fa9e406ahrens	uint64_t objset = dmu_objset_id(os);
613fa9e406ahrens	int advance = zdb_advance;
614fa9e406ahrens
615fa9e406ahrens	(void) printf("Indirect blocks:\n");
616fa9e406ahrens
617fa9e406ahrens	if (object == 0)
618fa9e406ahrens		advance |= ADVANCE_DATA;
619fa9e406ahrens
620fa9e406ahrens	th = traverse_init(dmu_objset_spa(os), zdb_indirect_cb, NULL, advance,
621fa9e406ahrens	    ZIO_FLAG_CANFAIL);
622fa9e406ahrens	th->th_noread = zdb_noread;
623fa9e406ahrens
624fa9e406ahrens	traverse_add_dnode(th, 0, -1ULL, objset, object);
625fa9e406ahrens
626fa9e406ahrens	while (traverse_more(th) == EAGAIN)
627fa9e406ahrens		continue;
628fa9e406ahrens
629fa9e406ahrens	(void) printf("\n");
630fa9e406ahrens
631fa9e406ahrens	traverse_fini(th);
632fa9e406ahrens}
633fa9e406ahrens
634fa9e406ahrens/*ARGSUSED*/
635fa9e406ahrensstatic void
636fa9e406ahrensdump_dsl_dir(objset_t *os, uint64_t object, void *data, size_t size)
637fa9e406ahrens{
638fa9e406ahrens	dsl_dir_phys_t *dd = data;
639fa9e406ahrens	time_t crtime;
640fa9e406ahrens	char used[6], compressed[6], uncompressed[6], quota[6], resv[6];
641fa9e406ahrens
642fa9e406ahrens	if (dd == NULL)
643fa9e406ahrens		return;
644fa9e406ahrens
645fa9e406ahrens	ASSERT(size == sizeof (*dd));
646fa9e406ahrens
647fa9e406ahrens	crtime = dd->dd_creation_time;
648fa9e406ahrens	nicenum(dd->dd_used_bytes, used);
649fa9e406ahrens	nicenum(dd->dd_compressed_bytes, compressed);
650fa9e406ahrens	nicenum(dd->dd_uncompressed_bytes, uncompressed);
651fa9e406ahrens	nicenum(dd->dd_quota, quota);
652fa9e406ahrens	nicenum(dd->dd_reserved, resv);
653fa9e406ahrens
654fa9e406ahrens	(void) printf("\t\tcreation_time = %s", ctime(&crtime));
655fa9e406ahrens	(void) printf("\t\thead_dataset_obj = %llu\n",
656fa9e406ahrens	    (u_longlong_t)dd->dd_head_dataset_obj);
657fa9e406ahrens	(void) printf("\t\tparent_dir_obj = %llu\n",
658fa9e406ahrens	    (u_longlong_t)dd->dd_parent_obj);
659fa9e406ahrens	(void) printf("\t\tclone_parent_obj = %llu\n",
660fa9e406ahrens	    (u_longlong_t)dd->dd_clone_parent_obj);
661fa9e406ahrens	(void) printf("\t\tchild_dir_zapobj = %llu\n",
662fa9e406ahrens	    (u_longlong_t)dd->dd_child_dir_zapobj);
663fa9e406ahrens	(void) printf("\t\tused_bytes = %s\n", used);
664fa9e406ahrens	(void) printf("\t\tcompressed_bytes = %s\n", compressed);
665fa9e406ahrens	(void) printf("\t\tuncompressed_bytes = %s\n", uncompressed);
666fa9e406ahrens	(void) printf("\t\tquota = %s\n", quota);
667fa9e406ahrens	(void) printf("\t\treserved = %s\n", resv);
668fa9e406ahrens	(void) printf("\t\tprops_zapobj = %llu\n",
669fa9e406ahrens	    (u_longlong_t)dd->dd_props_zapobj);
670fa9e406ahrens}
671fa9e406ahrens
672fa9e406ahrens/*ARGSUSED*/
673fa9e406ahrensstatic void
674fa9e406ahrensdump_dsl_dataset(objset_t *os, uint64_t object, void *data, size_t size)
675fa9e406ahrens{
676fa9e406ahrens	dsl_dataset_phys_t *ds = data;
677fa9e406ahrens	time_t crtime;
678fa9e406ahrens	char used[6], compressed[6], uncompressed[6], unique[6], blkbuf[300];
679fa9e406ahrens
680fa9e406ahrens	if (ds == NULL)
681fa9e406ahrens		return;
682fa9e406ahrens
683fa9e406ahrens	ASSERT(size == sizeof (*ds));
684fa9e406ahrens	crtime = ds->ds_creation_time;
685fa9e406ahrens	nicenum(ds->ds_used_bytes, used);
686fa9e406ahrens	nicenum(ds->ds_compressed_bytes, compressed);
687fa9e406ahrens	nicenum(ds->ds_uncompressed_bytes, uncompressed);
688fa9e406ahrens	nicenum(ds->ds_unique_bytes, unique);
689fa9e406ahrens	sprintf_blkptr(blkbuf, &ds->ds_bp);
690fa9e406ahrens
691fa9e406ahrens	(void) printf("\t\tdataset_obj = %llu\n",
692fa9e406ahrens	    (u_longlong_t)ds->ds_dir_obj);
693fa9e406ahrens	(void) printf("\t\tprev_snap_obj = %llu\n",
694fa9e406ahrens	    (u_longlong_t)ds->ds_prev_snap_obj);
695fa9e406ahrens	(void) printf("\t\tprev_snap_txg = %llu\n",
696fa9e406ahrens	    (u_longlong_t)ds->ds_prev_snap_txg);
697fa9e406ahrens	(void) printf("\t\tnext_snap_obj = %llu\n",
698fa9e406ahrens	    (u_longlong_t)ds->ds_next_snap_obj);
699fa9e406ahrens	(void) printf("\t\tsnapnames_zapobj = %llu\n",
700fa9e406ahrens	    (u_longlong_t)ds->ds_snapnames_zapobj);
701fa9e406ahrens	(void) printf("\t\tnum_children = %llu\n",
702fa9e406ahrens	    (u_longlong_t)ds->ds_num_children);
703fa9e406ahrens	(void) printf("\t\tcreation_time = %s", ctime(&crtime));
704fa9e406ahrens	(void) printf("\t\tcreation_txg = %llu\n",
705fa9e406ahrens	    (u_longlong_t)ds->ds_creation_txg);
706fa9e406ahrens	(void) printf("\t\tdeadlist_obj = %llu\n",
707fa9e406ahrens	    (u_longlong_t)ds->ds_deadlist_obj);
708fa9e406ahrens	(void) printf("\t\tused_bytes = %s\n", used);
709fa9e406ahrens	(void) printf("\t\tcompressed_bytes = %s\n", compressed);
710fa9e406ahrens	(void) printf("\t\tuncompressed_bytes = %s\n", uncompressed);
711fa9e406ahrens	(void) printf("\t\tunique = %s\n", unique);
712fa9e406ahrens	(void) printf("\t\tfsid_guid = %llu\n",
713fa9e406ahrens	    (u_longlong_t)ds->ds_fsid_guid);
714fa9e406ahrens	(void) printf("\t\tguid = %llu\n",
715fa9e406ahrens	    (u_longlong_t)ds->ds_guid);
716fa9e406ahrens	(void) printf("\t\trestoring = %llu\n",
717fa9e406ahrens	    (u_longlong_t)ds->ds_restoring);
718fa9e406ahrens	(void) printf("\t\tbp = %s\n", blkbuf);
719fa9e406ahrens}
720fa9e406ahrens
721fa9e406ahrensstatic void
722fa9e406ahrensdump_bplist(objset_t *mos, uint64_t object, char *name)
723fa9e406ahrens{
724fa9e406ahrens	bplist_t bpl = { 0 };
725fa9e406ahrens	blkptr_t blk, *bp = &blk;
726fa9e406ahrens	uint64_t itor = 0;
727fa9e406ahrens	char numbuf[6];
728fa9e406ahrens
729fa9e406ahrens	if (dump_opt['d'] < 3)
730fa9e406ahrens		return;
731fa9e406ahrens
732fa9e406ahrens	bplist_open(&bpl, mos, object);
733fa9e406ahrens	if (bplist_empty(&bpl)) {
734fa9e406ahrens		bplist_close(&bpl);
735fa9e406ahrens		return;
736fa9e406ahrens	}
737fa9e406ahrens
738fa9e406ahrens	nicenum(bpl.bpl_phys->bpl_bytes, numbuf);
739fa9e406ahrens
740fa9e406ahrens	(void) printf("\n    %s: %llu entries, %s\n",
741fa9e406ahrens	    name, (u_longlong_t)bpl.bpl_phys->bpl_entries, numbuf);
742fa9e406ahrens
743fa9e406ahrens	if (dump_opt['d'] < 5) {
744fa9e406ahrens		bplist_close(&bpl);
745fa9e406ahrens		return;
746fa9e406ahrens	}
747fa9e406ahrens
748fa9e406ahrens	(void) printf("\n");
749fa9e406ahrens
750fa9e406ahrens	while (bplist_iterate(&bpl, &itor, bp) == 0) {
751fa9e406ahrens		ASSERT(bp->blk_birth != 0);
752fa9e406ahrens		// XXBP - Do we want to see all DVAs, or just one?
753fa9e406ahrens		(void) printf("\tItem %3llu: vdev=%llu off=%llx "
754fa9e406ahrens		    "%llxL/%llxP/%llxA F=%llu B=%llu\n",
755fa9e406ahrens		    (u_longlong_t)itor - 1,
756fa9e406ahrens		    (u_longlong_t)DVA_GET_VDEV(&bp->blk_dva[0]),
757fa9e406ahrens		    (u_longlong_t)DVA_GET_OFFSET(&bp->blk_dva[0]),
758fa9e406ahrens		    (u_longlong_t)BP_GET_LSIZE(bp),
759fa9e406ahrens		    (u_longlong_t)BP_GET_PSIZE(bp),
760fa9e406ahrens		    (u_longlong_t)DVA_GET_ASIZE(&bp->blk_dva[0]),
761fa9e406ahrens		    (u_longlong_t)bp->blk_fill,
762fa9e406ahrens		    (u_longlong_t)bp->blk_birth);
763fa9e406ahrens	}
764fa9e406ahrens
765fa9e406ahrens	bplist_close(&bpl);
766fa9e406ahrens}
767fa9e406ahrens
768fa9e406ahrensstatic char *
769fa9e406ahrensznode_path(objset_t *os, uint64_t object, char *pathbuf, size_t size)
770fa9e406ahrens{
771fa9e406ahrens	dmu_buf_t *db;
772fa9e406ahrens	dmu_object_info_t doi;
773fa9e406ahrens	znode_phys_t *zp;
774fa9e406ahrens	uint64_t parent = 0;
775fa9e406ahrens	size_t complen;
776fa9e406ahrens	char component[MAXNAMELEN + 1];
777fa9e406ahrens	char *path;
778fa9e406ahrens
779fa9e406ahrens	path = pathbuf + size;
780fa9e406ahrens	*--path = '\0';
781fa9e406ahrens
782fa9e406ahrens	for (;;) {
783fa9e406ahrens		db = dmu_bonus_hold(os, object);
784fa9e406ahrens		if (db == NULL)
785fa9e406ahrens			break;
786fa9e406ahrens
787fa9e406ahrens		dmu_buf_read(db);
788fa9e406ahrens		dmu_object_info_from_db(db, &doi);
789fa9e406ahrens		zp = db->db_data;
790fa9e406ahrens		parent = zp->zp_parent;
791fa9e406ahrens		dmu_buf_rele(db);
792fa9e406ahrens
793fa9e406ahrens		if (doi.doi_bonus_type != DMU_OT_ZNODE)
794fa9e406ahrens			break;
795fa9e406ahrens
796fa9e406ahrens		if (parent == object) {
797fa9e406ahrens			if (path[0] != '/')
798fa9e406ahrens				*--path = '/';
799fa9e406ahrens			return (path);
800fa9e406ahrens		}
801fa9e406ahrens
802fa9e406ahrens		if (zap_value_search(os, parent, object, component) != 0)
803fa9e406ahrens			break;
804fa9e406ahrens
805fa9e406ahrens		complen = strlen(component);
806fa9e406ahrens		path -= complen;
807fa9e406ahrens		bcopy(component, path, complen);
808fa9e406ahrens		*--path = '/';
809fa9e406ahrens
810fa9e406ahrens		object = parent;
811fa9e406ahrens	}
812fa9e406ahrens
813fa9e406ahrens	(void) sprintf(component, "???<object#%llu>", (u_longlong_t)object);
814fa9e406ahrens
815fa9e406ahrens	complen = strlen(component);
816fa9e406ahrens	path -= complen;
817fa9e406ahrens	bcopy(component, path, complen);
818fa9e406ahrens
819fa9e406ahrens	return (path);
820fa9e406ahrens}
821fa9e406ahrens
822fa9e406ahrens/*ARGSUSED*/
823fa9e406ahrensstatic void
824fa9e406ahrensdump_znode(objset_t *os, uint64_t object, void *data, size_t size)
825fa9e406ahrens{
826fa9e406ahrens	znode_phys_t *zp = data;
827fa9e406ahrens	time_t z_crtime, z_atime, z_mtime, z_ctime;
828fa9e406ahrens	char path[MAXPATHLEN * 2];	/* allow for xattr and failure prefix */
829fa9e406ahrens
830fa9e406ahrens	ASSERT(size >= sizeof (znode_phys_t));
831fa9e406ahrens
832fa9e406ahrens	if (dump_opt['d'] < 3) {
833fa9e406ahrens		(void) printf("\t%s\n",
834fa9e406ahrens		    znode_path(os, object, path, sizeof (path)));
835fa9e406ahrens		return;
836fa9e406ahrens	}
837fa9e406ahrens
838fa9e406ahrens	z_crtime = (time_t)zp->zp_crtime[0];
839fa9e406ahrens	z_atime = (time_t)zp->zp_atime[0];
840fa9e406ahrens	z_mtime = (time_t)zp->zp_mtime[0];
841fa9e406ahrens	z_ctime = (time_t)zp->zp_ctime[0];
842fa9e406ahrens
843fa9e406ahrens	(void) printf("\tpath	%s\n",
844fa9e406ahrens	    znode_path(os, object, path, sizeof (path)));
845fa9e406ahrens	(void) printf("\tatime	%s", ctime(&z_atime));
846fa9e406ahrens	(void) printf("\tmtime	%s", ctime(&z_mtime));
847fa9e406ahrens	(void) printf("\tctime	%s", ctime(&z_ctime));
848fa9e406ahrens	(void) printf("\tcrtime	%s", ctime(&z_crtime));
849fa9e406ahrens	(void) printf("\tgen	%llu\n", (u_longlong_t)zp->zp_gen);
850fa9e406ahrens	(void) printf("\tmode	%llo\n", (u_longlong_t)zp->zp_mode);
851fa9e406ahrens	(void) printf("\tsize	%llu\n", (u_longlong_t)zp->zp_size);
852fa9e406ahrens	(void) printf("\tparent	%llu\n", (u_longlong_t)zp->zp_parent);
853fa9e406ahrens	(void) printf("\tlinks	%llu\n", (u_longlong_t)zp->zp_links);
854fa9e406ahrens	(void) printf("\txattr	%llu\n", (u_longlong_t)zp->zp_xattr);
855fa9e406ahrens	(void) printf("\trdev	0x%016llx\n", (u_longlong_t)zp->zp_rdev);
856fa9e406ahrens}
857fa9e406ahrens
858fa9e406ahrens/*ARGSUSED*/
859fa9e406ahrensstatic void
860fa9e406ahrensdump_acl(objset_t *os, uint64_t object, void *data, size_t size)
861fa9e406ahrens{
862fa9e406ahrens}
863fa9e406ahrens
864fa9e406ahrens/*ARGSUSED*/
865fa9e406ahrensstatic void
866fa9e406ahrensdump_dmu_objset(objset_t *os, uint64_t object, void *data, size_t size)
867fa9e406ahrens{
868fa9e406ahrens}
869fa9e406ahrens
870fa9e406ahrensstatic object_viewer_t *object_viewer[DMU_OT_NUMTYPES] = {
871fa9e406ahrens	dump_none,		/* unallocated			*/
872fa9e406ahrens	dump_zap,		/* object directory		*/
873fa9e406ahrens	dump_uint64,		/* object array			*/
874fa9e406ahrens	dump_none,		/* packed nvlist		*/
875fa9e406ahrens	dump_packed_nvlist,	/* packed nvlist size		*/
876fa9e406ahrens	dump_none,		/* bplist			*/
877fa9e406ahrens	dump_none,		/* bplist header		*/
878fa9e406ahrens	dump_none,		/* SPA space map header		*/
879fa9e406ahrens	dump_none,		/* SPA space map		*/
880fa9e406ahrens	dump_none,		/* ZIL intent log		*/
881fa9e406ahrens	dump_dnode,		/* DMU dnode			*/
882fa9e406ahrens	dump_dmu_objset,	/* DMU objset			*/
883fa9e406ahrens	dump_dsl_dir,	/* DSL directory			*/
884fa9e406ahrens	dump_zap,		/* DSL directory child map	*/
885fa9e406ahrens	dump_zap,		/* DSL dataset snap map		*/
886fa9e406ahrens	dump_zap,		/* DSL props			*/
887fa9e406ahrens	dump_dsl_dataset,	/* DSL dataset			*/
888fa9e406ahrens	dump_znode,		/* ZFS znode			*/
889fa9e406ahrens	dump_acl,		/* ZFS ACL			*/
890fa9e406ahrens	dump_uint8,		/* ZFS plain file		*/
891fa9e406ahrens	dump_zap,		/* ZFS directory		*/
892fa9e406ahrens	dump_zap,		/* ZFS master node		*/
893fa9e406ahrens	dump_zap,		/* ZFS delete queue		*/
894fa9e406ahrens	dump_uint8,		/* zvol object			*/
895fa9e406ahrens	dump_zap,		/* zvol prop			*/
896fa9e406ahrens	dump_uint8,		/* other uint8[]		*/
897fa9e406ahrens	dump_uint64,		/* other uint64[]		*/
898fa9e406ahrens	dump_zap,		/* other ZAP			*/
899fa9e406ahrens};
900fa9e406ahrens
901fa9e406ahrensstatic void
902fa9e406ahrensdump_object(objset_t *os, uint64_t object, int verbosity, int *print_header)
903fa9e406ahrens{
904fa9e406ahrens	dmu_buf_t *db = NULL;
905fa9e406ahrens	dmu_object_info_t doi;
906fa9e406ahrens	dnode_t *dn;
907fa9e406ahrens	void *bonus = NULL;
908fa9e406ahrens	size_t bsize = 0;
909fa9e406ahrens	char iblk[6], dblk[6], lsize[6], psize[6], bonus_size[6], segsize[6];
910fa9e406ahrens	char aux[50];
911fa9e406ahrens	int error;
912fa9e406ahrens
913fa9e406ahrens	if (*print_header) {
914fa9e406ahrens		(void) printf("\n    Object  lvl   iblk   dblk  lsize"
915fa9e406ahrens		    "  psize  type\n");
916fa9e406ahrens		*print_header = 0;
917fa9e406ahrens	}
918fa9e406ahrens
919fa9e406ahrens	if (object == 0) {
920fa9e406ahrens		dn = os->os->os_meta_dnode;
921fa9e406ahrens	} else {
922fa9e406ahrens		db = dmu_bonus_hold(os, object);
923fa9e406ahrens		if (db == NULL)
924fa9e406ahrens			fatal("dmu_bonus_hold(%llu) failed", object);
925fa9e406ahrens		dmu_buf_read(db);
926fa9e406ahrens		bonus = db->db_data;
927fa9e406ahrens		bsize = db->db_size;
928fa9e406ahrens		dn = ((dmu_buf_impl_t *)db)->db_dnode;
929fa9e406ahrens	}
930fa9e406ahrens	dmu_object_info_from_dnode(dn, &doi);
931fa9e406ahrens
932fa9e406ahrens	nicenum(doi.doi_metadata_block_size, iblk);
933fa9e406ahrens	nicenum(doi.doi_data_block_size, dblk);
934fa9e406ahrens	nicenum(doi.doi_data_block_size * (doi.doi_max_block_offset + 1),
935fa9e406ahrens	    lsize);
936fa9e406ahrens	nicenum(doi.doi_physical_blks << 9, psize);
937fa9e406ahrens	nicenum(doi.doi_bonus_size, bonus_size);
938fa9e406ahrens
939fa9e406ahrens	aux[0] = '\0';
940fa9e406ahrens
941fa9e406ahrens	if (doi.doi_checksum != ZIO_CHECKSUM_INHERIT || verbosity >= 6)
942fa9e406ahrens		(void) snprintf(aux + strlen(aux), sizeof (aux), " (K=%s)",
943fa9e406ahrens		zio_checksum_table[doi.doi_checksum].ci_name);
944fa9e406ahrens
945fa9e406ahrens	if (doi.doi_compress != ZIO_COMPRESS_INHERIT || verbosity >= 6)
946fa9e406ahrens		(void) snprintf(aux + strlen(aux), sizeof (aux), " (Z=%s)",
947fa9e406ahrens		zio_compress_table[doi.doi_compress].ci_name);
948fa9e406ahrens
949fa9e406ahrens	(void) printf("%10lld  %3u  %5s  %5s  %5s  %5s  %s%s\n",
950fa9e406ahrens	    (u_longlong_t)object, doi.doi_indirection, iblk, dblk, lsize,
951fa9e406ahrens	    psize, dmu_ot[doi.doi_type].ot_name, aux);
952fa9e406ahrens
953fa9e406ahrens	if (doi.doi_bonus_type != DMU_OT_NONE && verbosity > 3) {
954fa9e406ahrens		(void) printf("%10s  %3s  %5s  %5s  %5s  %5s  %s\n",
955fa9e406ahrens		    "", "", "", "", bonus_size, "bonus",
956fa9e406ahrens		    dmu_ot[doi.doi_bonus_type].ot_name);
957fa9e406ahrens	}
958fa9e406ahrens
959fa9e406ahrens	if (verbosity >= 4) {
960fa9e406ahrens		object_viewer[doi.doi_bonus_type](os, object, bonus, bsize);
961fa9e406ahrens		object_viewer[doi.doi_type](os, object, NULL, 0);
962fa9e406ahrens		*print_header = 1;
963fa9e406ahrens	}
964fa9e406ahrens
965fa9e406ahrens	if (verbosity >= 5)
966fa9e406ahrens		dump_indirect(os, object, NULL, 0);
967fa9e406ahrens
968fa9e406ahrens	if (verbosity >= 5) {
969fa9e406ahrens		/*
970fa9e406ahrens		 * Report the list of segments that comprise the object.
971fa9e406ahrens		 */
972fa9e406ahrens		uint64_t start = 0;
973fa9e406ahrens		uint64_t end;
974fa9e406ahrens		uint64_t blkfill = 1;
975fa9e406ahrens		int minlvl = 1;
976fa9e406ahrens
977fa9e406ahrens		if (dn->dn_type == DMU_OT_DNODE) {
978fa9e406ahrens			minlvl = 0;
979fa9e406ahrens			blkfill = DNODES_PER_BLOCK;
980fa9e406ahrens		}
981fa9e406ahrens
982fa9e406ahrens		for (;;) {
983fa9e406ahrens			error = dnode_next_offset(dn, B_FALSE, &start, minlvl,
984fa9e406ahrens			    blkfill);
985fa9e406ahrens			if (error)
986fa9e406ahrens				break;
987fa9e406ahrens			end = start;
988fa9e406ahrens			error = dnode_next_offset(dn, B_TRUE, &end, minlvl,
989fa9e406ahrens			    blkfill);
990fa9e406ahrens			nicenum(end - start, segsize);
991fa9e406ahrens			(void) printf("\t\tsegment [%016llx, %016llx)"
992fa9e406ahrens			    " size %5s\n", (u_longlong_t)start,
993fa9e406ahrens			    (u_longlong_t)end, segsize);
994fa9e406ahrens			if (error)
995fa9e406ahrens				break;
996fa9e406ahrens			start = end;
997fa9e406ahrens		}
998fa9e406ahrens	}
999fa9e406ahrens
1000fa9e406ahrens	if (db != NULL)
1001fa9e406ahrens		dmu_buf_rele(db);
1002fa9e406ahrens}
1003fa9e406ahrens
1004fa9e406ahrensstatic char *objset_types[DMU_OST_NUMTYPES] = {
1005fa9e406ahrens	"NONE", "META", "ZPL", "ZVOL", "OTHER", "ANY" };
1006fa9e406ahrens
1007fa9e406ahrens/*ARGSUSED*/
1008fa9e406ahrensstatic void
1009fa9e406ahrensdump_dir(objset_t *os)
1010fa9e406ahrens{
1011fa9e406ahrens	dmu_objset_stats_t dds;
1012fa9e406ahrens	uint64_t object, object_count;
1013fa9e406ahrens	char numbuf[8];
1014fa9e406ahrens	char blkbuf[300];
1015fa9e406ahrens	char osname[MAXNAMELEN];
1016fa9e406ahrens	char *type = "UNKNOWN";
1017fa9e406ahrens	int verbosity = dump_opt['d'];
1018fa9e406ahrens	int print_header = 1;
1019fa9e406ahrens	int i, error;
1020fa9e406ahrens
1021fa9e406ahrens	dmu_objset_stats(os, &dds);
1022fa9e406ahrens
1023fa9e406ahrens	if (dds.dds_type < DMU_OST_NUMTYPES)
1024fa9e406ahrens		type = objset_types[dds.dds_type];
1025fa9e406ahrens
1026fa9e406ahrens	if (dds.dds_type == DMU_OST_META) {
1027fa9e406ahrens		dds.dds_creation_txg = TXG_INITIAL;
1028fa9e406ahrens		dds.dds_last_txg = os->os->os_rootbp.blk_birth;
1029fa9e406ahrens		dds.dds_objects_used = os->os->os_rootbp.blk_fill;
1030fa9e406ahrens		dds.dds_space_refd =
1031fa9e406ahrens		    os->os->os_spa->spa_dsl_pool->dp_mos_dir->dd_used_bytes;
1032fa9e406ahrens	}
1033fa9e406ahrens
1034fa9e406ahrens	ASSERT3U(dds.dds_objects_used, ==, os->os->os_rootbp.blk_fill);
1035fa9e406ahrens
1036fa9e406ahrens	nicenum(dds.dds_space_refd, numbuf);
1037fa9e406ahrens
1038fa9e406ahrens	if (verbosity >= 4) {
1039fa9e406ahrens		(void) strcpy(blkbuf, ", rootbp ");
1040fa9e406ahrens		sprintf_blkptr(blkbuf + strlen(blkbuf), &os->os->os_rootbp);
1041fa9e406ahrens	} else {
1042fa9e406ahrens		blkbuf[0] = '\0';
1043fa9e406ahrens	}
1044fa9e406ahrens
1045fa9e406ahrens	dmu_objset_name(os, osname);
1046fa9e406ahrens
1047fa9e406ahrens	(void) printf("Dataset %s [%s], ID %llu, cr_txg %llu, last_txg %llu, "
1048fa9e406ahrens	    "%s, %llu objects%s\n",
1049fa9e406ahrens	    osname, type, (u_longlong_t)dmu_objset_id(os),
1050fa9e406ahrens	    (u_longlong_t)dds.dds_creation_txg,
1051fa9e406ahrens	    (u_longlong_t)dds.dds_last_txg,
1052fa9e406ahrens	    numbuf,
1053fa9e406ahrens	    (u_longlong_t)dds.dds_objects_used,
1054fa9e406ahrens	    blkbuf);
1055fa9e406ahrens
1056fa9e406ahrens	dump_intent_log(dmu_objset_zil(os));
1057fa9e406ahrens
1058fa9e406ahrens	if (dmu_objset_ds(os) != NULL)
1059fa9e406ahrens		dump_bplist(dmu_objset_pool(os)->dp_meta_objset,
1060fa9e406ahrens		    dmu_objset_ds(os)->ds_phys->ds_deadlist_obj, "Deadlist");
1061fa9e406ahrens
1062fa9e406ahrens	if (verbosity < 2)
1063fa9e406ahrens		return;
1064fa9e406ahrens
1065fa9e406ahrens	if (zopt_objects != 0) {
1066fa9e406ahrens		for (i = 0; i < zopt_objects; i++)
1067fa9e406ahrens			dump_object(os, zopt_object[i], verbosity,
1068fa9e406ahrens			    &print_header);
1069fa9e406ahrens		(void) printf("\n");
1070fa9e406ahrens		return;
1071fa9e406ahrens	}
1072fa9e406ahrens
1073fa9e406ahrens	dump_object(os, 0, verbosity, &print_header);
1074fa9e406ahrens	object_count = 1;
1075fa9e406ahrens
1076fa9e406ahrens	object = 0;
1077fa9e406ahrens	while ((error = dmu_object_next(os, &object, B_FALSE)) == 0) {
1078fa9e406ahrens		dump_object(os, object, verbosity, &print_header);
1079fa9e406ahrens		object_count++;
1080fa9e406ahrens	}
1081fa9e406ahrens
1082fa9e406ahrens	ASSERT3U(object_count, ==, dds.dds_objects_used);
1083fa9e406ahrens
1084fa9e406ahrens	(void) printf("\n");
1085fa9e406ahrens
1086fa9e406ahrens	if (error != ESRCH)
1087fa9e406ahrens		fatal("dmu_object_next() = %d", error);
1088fa9e406ahrens}
1089fa9e406ahrens
1090fa9e406ahrensstatic void
1091fa9e406ahrensdump_uberblock(uberblock_t *ub)
1092fa9e406ahrens{
1093fa9e406ahrens	time_t timestamp = ub->ub_timestamp;
1094fa9e406ahrens
1095fa9e406ahrens	(void) printf("Uberblock\n\n");
1096fa9e406ahrens	(void) printf("\tmagic = %016llx\n", (u_longlong_t)ub->ub_magic);
1097fa9e406ahrens	(void) printf("\tversion = %llu\n", (u_longlong_t)ub->ub_version);
1098fa9e406ahrens	(void) printf("\ttxg = %llu\n", (u_longlong_t)ub->ub_txg);
1099fa9e406ahrens	(void) printf("\tguid_sum = %llu\n", (u_longlong_t)ub->ub_guid_sum);
1100fa9e406ahrens	(void) printf("\ttimestamp = %llu UTC = %s",
1101fa9e406ahrens	    (u_longlong_t)ub->ub_timestamp, asctime(localtime(&timestamp)));
1102fa9e406ahrens	if (dump_opt['u'] >= 3) {
1103fa9e406ahrens		char blkbuf[300];
1104fa9e406ahrens		sprintf_blkptr(blkbuf, &ub->ub_rootbp);
1105fa9e406ahrens		(void) printf("\trootbp = %s\n", blkbuf);
1106fa9e406ahrens	}
1107fa9e406ahrens	(void) printf("\n");
1108fa9e406ahrens}
1109fa9e406ahrens
1110