xref: /titanic_50/usr/src/cmd/zdb/zdb.c (revision 799823bbed51a695d01e13511bbb1369980bb714)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License (the "License").
6  * You may not use this file except in compliance with the License.
7  *
8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9  * or http://www.opensolaris.org/os/licensing.
10  * See the License for the specific language governing permissions
11  * and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL HEADER in each
14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15  * If applicable, add the following below this CDDL HEADER, with the
16  * fields enclosed by brackets "[]" replaced with your own identifying
17  * information: Portions Copyright [yyyy] [name of copyright owner]
18  *
19  * CDDL HEADER END
20  */
21 
22 /*
23  * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
24  * Copyright (c) 2011, 2014 by Delphix. All rights reserved.
25  */
26 
27 #include <stdio.h>
28 #include <unistd.h>
29 #include <stdio_ext.h>
30 #include <stdlib.h>
31 #include <ctype.h>
32 #include <sys/zfs_context.h>
33 #include <sys/spa.h>
34 #include <sys/spa_impl.h>
35 #include <sys/dmu.h>
36 #include <sys/zap.h>
37 #include <sys/fs/zfs.h>
38 #include <sys/zfs_znode.h>
39 #include <sys/zfs_sa.h>
40 #include <sys/sa.h>
41 #include <sys/sa_impl.h>
42 #include <sys/vdev.h>
43 #include <sys/vdev_impl.h>
44 #include <sys/metaslab_impl.h>
45 #include <sys/dmu_objset.h>
46 #include <sys/dsl_dir.h>
47 #include <sys/dsl_dataset.h>
48 #include <sys/dsl_pool.h>
49 #include <sys/dbuf.h>
50 #include <sys/zil.h>
51 #include <sys/zil_impl.h>
52 #include <sys/stat.h>
53 #include <sys/resource.h>
54 #include <sys/dmu_traverse.h>
55 #include <sys/zio_checksum.h>
56 #include <sys/zio_compress.h>
57 #include <sys/zfs_fuid.h>
58 #include <sys/arc.h>
59 #include <sys/ddt.h>
60 #include <sys/zfeature.h>
61 #include <zfs_comutil.h>
62 #undef ZFS_MAXNAMELEN
63 #undef verify
64 #include <libzfs.h>
65 
66 #define	ZDB_COMPRESS_NAME(idx) ((idx) < ZIO_COMPRESS_FUNCTIONS ?	\
67 	zio_compress_table[(idx)].ci_name : "UNKNOWN")
68 #define	ZDB_CHECKSUM_NAME(idx) ((idx) < ZIO_CHECKSUM_FUNCTIONS ?	\
69 	zio_checksum_table[(idx)].ci_name : "UNKNOWN")
70 #define	ZDB_OT_NAME(idx) ((idx) < DMU_OT_NUMTYPES ?	\
71 	dmu_ot[(idx)].ot_name : DMU_OT_IS_VALID(idx) ?	\
72 	dmu_ot_byteswap[DMU_OT_BYTESWAP(idx)].ob_name : "UNKNOWN")
73 #define	ZDB_OT_TYPE(idx) ((idx) < DMU_OT_NUMTYPES ? (idx) :		\
74 	(((idx) == DMU_OTN_ZAP_DATA || (idx) == DMU_OTN_ZAP_METADATA) ?	\
75 	DMU_OT_ZAP_OTHER : DMU_OT_NUMTYPES))
76 
77 #ifndef lint
78 extern boolean_t zfs_recover;
79 extern uint64_t zfs_arc_max, zfs_arc_meta_limit;
80 extern int zfs_vdev_async_read_max_active;
81 #else
82 boolean_t zfs_recover;
83 uint64_t zfs_arc_max, zfs_arc_meta_limit;
84 int zfs_vdev_async_read_max_active;
85 #endif
86 
87 const char cmdname[] = "zdb";
88 uint8_t dump_opt[256];
89 
90 typedef void object_viewer_t(objset_t *, uint64_t, void *data, size_t size);
91 
92 extern void dump_intent_log(zilog_t *);
93 uint64_t *zopt_object = NULL;
94 int zopt_objects = 0;
95 libzfs_handle_t *g_zfs;
96 uint64_t max_inflight = 1000;
97 
98 static void snprintf_blkptr_compact(char *, size_t, const blkptr_t *);
99 
100 /*
101  * These libumem hooks provide a reasonable set of defaults for the allocator's
102  * debugging facilities.
103  */
104 const char *
105 _umem_debug_init()
106 {
107 	return ("default,verbose"); /* $UMEM_DEBUG setting */
108 }
109 
110 const char *
111 _umem_logging_init(void)
112 {
113 	return ("fail,contents"); /* $UMEM_LOGGING setting */
114 }
115 
116 static void
117 usage(void)
118 {
119 	(void) fprintf(stderr,
120 	    "Usage: %s [-CumMdibcsDvhLXFPA] [-t txg] [-e [-p path...]] "
121 	    "[-U config] [-I inflight I/Os] [-x dumpdir] poolname [object...]\n"
122 	    "       %s [-divPA] [-e -p path...] [-U config] dataset "
123 	    "[object...]\n"
124 	    "       %s -mM [-LXFPA] [-t txg] [-e [-p path...]] [-U config] "
125 	    "poolname [vdev [metaslab...]]\n"
126 	    "       %s -R [-A] [-e [-p path...]] poolname "
127 	    "vdev:offset:size[:flags]\n"
128 	    "       %s -S [-PA] [-e [-p path...]] [-U config] poolname\n"
129 	    "       %s -l [-uA] device\n"
130 	    "       %s -C [-A] [-U config]\n\n",
131 	    cmdname, cmdname, cmdname, cmdname, cmdname, cmdname, cmdname);
132 
133 	(void) fprintf(stderr, "    Dataset name must include at least one "
134 	    "separator character '/' or '@'\n");
135 	(void) fprintf(stderr, "    If dataset name is specified, only that "
136 	    "dataset is dumped\n");
137 	(void) fprintf(stderr, "    If object numbers are specified, only "
138 	    "those objects are dumped\n\n");
139 	(void) fprintf(stderr, "    Options to control amount of output:\n");
140 	(void) fprintf(stderr, "        -u uberblock\n");
141 	(void) fprintf(stderr, "        -d dataset(s)\n");
142 	(void) fprintf(stderr, "        -i intent logs\n");
143 	(void) fprintf(stderr, "        -C config (or cachefile if alone)\n");
144 	(void) fprintf(stderr, "        -h pool history\n");
145 	(void) fprintf(stderr, "        -b block statistics\n");
146 	(void) fprintf(stderr, "        -m metaslabs\n");
147 	(void) fprintf(stderr, "        -M metaslab groups\n");
148 	(void) fprintf(stderr, "        -c checksum all metadata (twice for "
149 	    "all data) blocks\n");
150 	(void) fprintf(stderr, "        -s report stats on zdb's I/O\n");
151 	(void) fprintf(stderr, "        -D dedup statistics\n");
152 	(void) fprintf(stderr, "        -S simulate dedup to measure effect\n");
153 	(void) fprintf(stderr, "        -v verbose (applies to all others)\n");
154 	(void) fprintf(stderr, "        -l dump label contents\n");
155 	(void) fprintf(stderr, "        -L disable leak tracking (do not "
156 	    "load spacemaps)\n");
157 	(void) fprintf(stderr, "        -R read and display block from a "
158 	    "device\n\n");
159 	(void) fprintf(stderr, "    Below options are intended for use "
160 	    "with other options:\n");
161 	(void) fprintf(stderr, "        -A ignore assertions (-A), enable "
162 	    "panic recovery (-AA) or both (-AAA)\n");
163 	(void) fprintf(stderr, "        -F attempt automatic rewind within "
164 	    "safe range of transaction groups\n");
165 	(void) fprintf(stderr, "        -U <cachefile_path> -- use alternate "
166 	    "cachefile\n");
167 	(void) fprintf(stderr, "        -X attempt extreme rewind (does not "
168 	    "work with dataset)\n");
169 	(void) fprintf(stderr, "        -e pool is exported/destroyed/"
170 	    "has altroot/not in a cachefile\n");
171 	(void) fprintf(stderr, "        -p <path> -- use one or more with "
172 	    "-e to specify path to vdev dir\n");
173 	(void) fprintf(stderr, "        -x <dumpdir> -- "
174 	    "dump all read blocks into specified directory\n");
175 	(void) fprintf(stderr, "        -P print numbers in parseable form\n");
176 	(void) fprintf(stderr, "        -t <txg> -- highest txg to use when "
177 	    "searching for uberblocks\n");
178 	(void) fprintf(stderr, "        -I <number of inflight I/Os> -- "
179 	    "specify the maximum number of "
180 	    "checksumming I/Os [default is 200]\n");
181 	(void) fprintf(stderr, "Specify an option more than once (e.g. -bb) "
182 	    "to make only that option verbose\n");
183 	(void) fprintf(stderr, "Default is to dump everything non-verbosely\n");
184 	exit(1);
185 }
186 
187 /*
188  * Called for usage errors that are discovered after a call to spa_open(),
189  * dmu_bonus_hold(), or pool_match().  abort() is called for other errors.
190  */
191 
192 static void
193 fatal(const char *fmt, ...)
194 {
195 	va_list ap;
196 
197 	va_start(ap, fmt);
198 	(void) fprintf(stderr, "%s: ", cmdname);
199 	(void) vfprintf(stderr, fmt, ap);
200 	va_end(ap);
201 	(void) fprintf(stderr, "\n");
202 
203 	exit(1);
204 }
205 
206 /* ARGSUSED */
207 static void
208 dump_packed_nvlist(objset_t *os, uint64_t object, void *data, size_t size)
209 {
210 	nvlist_t *nv;
211 	size_t nvsize = *(uint64_t *)data;
212 	char *packed = umem_alloc(nvsize, UMEM_NOFAIL);
213 
214 	VERIFY(0 == dmu_read(os, object, 0, nvsize, packed, DMU_READ_PREFETCH));
215 
216 	VERIFY(nvlist_unpack(packed, nvsize, &nv, 0) == 0);
217 
218 	umem_free(packed, nvsize);
219 
220 	dump_nvlist(nv, 8);
221 
222 	nvlist_free(nv);
223 }
224 
225 /* ARGSUSED */
226 static void
227 dump_history_offsets(objset_t *os, uint64_t object, void *data, size_t size)
228 {
229 	spa_history_phys_t *shp = data;
230 
231 	if (shp == NULL)
232 		return;
233 
234 	(void) printf("\t\tpool_create_len = %llu\n",
235 	    (u_longlong_t)shp->sh_pool_create_len);
236 	(void) printf("\t\tphys_max_off = %llu\n",
237 	    (u_longlong_t)shp->sh_phys_max_off);
238 	(void) printf("\t\tbof = %llu\n",
239 	    (u_longlong_t)shp->sh_bof);
240 	(void) printf("\t\teof = %llu\n",
241 	    (u_longlong_t)shp->sh_eof);
242 	(void) printf("\t\trecords_lost = %llu\n",
243 	    (u_longlong_t)shp->sh_records_lost);
244 }
245 
246 static void
247 zdb_nicenum(uint64_t num, char *buf)
248 {
249 	if (dump_opt['P'])
250 		(void) sprintf(buf, "%llu", (longlong_t)num);
251 	else
252 		nicenum(num, buf);
253 }
254 
255 const char histo_stars[] = "****************************************";
256 const int histo_width = sizeof (histo_stars) - 1;
257 
258 static void
259 dump_histogram(const uint64_t *histo, int size, int offset)
260 {
261 	int i;
262 	int minidx = size - 1;
263 	int maxidx = 0;
264 	uint64_t max = 0;
265 
266 	for (i = 0; i < size; i++) {
267 		if (histo[i] > max)
268 			max = histo[i];
269 		if (histo[i] > 0 && i > maxidx)
270 			maxidx = i;
271 		if (histo[i] > 0 && i < minidx)
272 			minidx = i;
273 	}
274 
275 	if (max < histo_width)
276 		max = histo_width;
277 
278 	for (i = minidx; i <= maxidx; i++) {
279 		(void) printf("\t\t\t%3u: %6llu %s\n",
280 		    i + offset, (u_longlong_t)histo[i],
281 		    &histo_stars[(max - histo[i]) * histo_width / max]);
282 	}
283 }
284 
285 static void
286 dump_zap_stats(objset_t *os, uint64_t object)
287 {
288 	int error;
289 	zap_stats_t zs;
290 
291 	error = zap_get_stats(os, object, &zs);
292 	if (error)
293 		return;
294 
295 	if (zs.zs_ptrtbl_len == 0) {
296 		ASSERT(zs.zs_num_blocks == 1);
297 		(void) printf("\tmicrozap: %llu bytes, %llu entries\n",
298 		    (u_longlong_t)zs.zs_blocksize,
299 		    (u_longlong_t)zs.zs_num_entries);
300 		return;
301 	}
302 
303 	(void) printf("\tFat ZAP stats:\n");
304 
305 	(void) printf("\t\tPointer table:\n");
306 	(void) printf("\t\t\t%llu elements\n",
307 	    (u_longlong_t)zs.zs_ptrtbl_len);
308 	(void) printf("\t\t\tzt_blk: %llu\n",
309 	    (u_longlong_t)zs.zs_ptrtbl_zt_blk);
310 	(void) printf("\t\t\tzt_numblks: %llu\n",
311 	    (u_longlong_t)zs.zs_ptrtbl_zt_numblks);
312 	(void) printf("\t\t\tzt_shift: %llu\n",
313 	    (u_longlong_t)zs.zs_ptrtbl_zt_shift);
314 	(void) printf("\t\t\tzt_blks_copied: %llu\n",
315 	    (u_longlong_t)zs.zs_ptrtbl_blks_copied);
316 	(void) printf("\t\t\tzt_nextblk: %llu\n",
317 	    (u_longlong_t)zs.zs_ptrtbl_nextblk);
318 
319 	(void) printf("\t\tZAP entries: %llu\n",
320 	    (u_longlong_t)zs.zs_num_entries);
321 	(void) printf("\t\tLeaf blocks: %llu\n",
322 	    (u_longlong_t)zs.zs_num_leafs);
323 	(void) printf("\t\tTotal blocks: %llu\n",
324 	    (u_longlong_t)zs.zs_num_blocks);
325 	(void) printf("\t\tzap_block_type: 0x%llx\n",
326 	    (u_longlong_t)zs.zs_block_type);
327 	(void) printf("\t\tzap_magic: 0x%llx\n",
328 	    (u_longlong_t)zs.zs_magic);
329 	(void) printf("\t\tzap_salt: 0x%llx\n",
330 	    (u_longlong_t)zs.zs_salt);
331 
332 	(void) printf("\t\tLeafs with 2^n pointers:\n");
333 	dump_histogram(zs.zs_leafs_with_2n_pointers, ZAP_HISTOGRAM_SIZE, 0);
334 
335 	(void) printf("\t\tBlocks with n*5 entries:\n");
336 	dump_histogram(zs.zs_blocks_with_n5_entries, ZAP_HISTOGRAM_SIZE, 0);
337 
338 	(void) printf("\t\tBlocks n/10 full:\n");
339 	dump_histogram(zs.zs_blocks_n_tenths_full, ZAP_HISTOGRAM_SIZE, 0);
340 
341 	(void) printf("\t\tEntries with n chunks:\n");
342 	dump_histogram(zs.zs_entries_using_n_chunks, ZAP_HISTOGRAM_SIZE, 0);
343 
344 	(void) printf("\t\tBuckets with n entries:\n");
345 	dump_histogram(zs.zs_buckets_with_n_entries, ZAP_HISTOGRAM_SIZE, 0);
346 }
347 
348 /*ARGSUSED*/
349 static void
350 dump_none(objset_t *os, uint64_t object, void *data, size_t size)
351 {
352 }
353 
354 /*ARGSUSED*/
355 static void
356 dump_unknown(objset_t *os, uint64_t object, void *data, size_t size)
357 {
358 	(void) printf("\tUNKNOWN OBJECT TYPE\n");
359 }
360 
361 /*ARGSUSED*/
362 void
363 dump_uint8(objset_t *os, uint64_t object, void *data, size_t size)
364 {
365 }
366 
367 /*ARGSUSED*/
368 static void
369 dump_uint64(objset_t *os, uint64_t object, void *data, size_t size)
370 {
371 }
372 
373 /*ARGSUSED*/
374 static void
375 dump_zap(objset_t *os, uint64_t object, void *data, size_t size)
376 {
377 	zap_cursor_t zc;
378 	zap_attribute_t attr;
379 	void *prop;
380 	int i;
381 
382 	dump_zap_stats(os, object);
383 	(void) printf("\n");
384 
385 	for (zap_cursor_init(&zc, os, object);
386 	    zap_cursor_retrieve(&zc, &attr) == 0;
387 	    zap_cursor_advance(&zc)) {
388 		(void) printf("\t\t%s = ", attr.za_name);
389 		if (attr.za_num_integers == 0) {
390 			(void) printf("\n");
391 			continue;
392 		}
393 		prop = umem_zalloc(attr.za_num_integers *
394 		    attr.za_integer_length, UMEM_NOFAIL);
395 		(void) zap_lookup(os, object, attr.za_name,
396 		    attr.za_integer_length, attr.za_num_integers, prop);
397 		if (attr.za_integer_length == 1) {
398 			(void) printf("%s", (char *)prop);
399 		} else {
400 			for (i = 0; i < attr.za_num_integers; i++) {
401 				switch (attr.za_integer_length) {
402 				case 2:
403 					(void) printf("%u ",
404 					    ((uint16_t *)prop)[i]);
405 					break;
406 				case 4:
407 					(void) printf("%u ",
408 					    ((uint32_t *)prop)[i]);
409 					break;
410 				case 8:
411 					(void) printf("%lld ",
412 					    (u_longlong_t)((int64_t *)prop)[i]);
413 					break;
414 				}
415 			}
416 		}
417 		(void) printf("\n");
418 		umem_free(prop, attr.za_num_integers * attr.za_integer_length);
419 	}
420 	zap_cursor_fini(&zc);
421 }
422 
423 static void
424 dump_bpobj(objset_t *os, uint64_t object, void *data, size_t size)
425 {
426 	bpobj_phys_t *bpop = data;
427 	char bytes[32], comp[32], uncomp[32];
428 
429 	if (bpop == NULL)
430 		return;
431 
432 	zdb_nicenum(bpop->bpo_bytes, bytes);
433 	zdb_nicenum(bpop->bpo_comp, comp);
434 	zdb_nicenum(bpop->bpo_uncomp, uncomp);
435 
436 	(void) printf("\t\tnum_blkptrs = %llu\n",
437 	    (u_longlong_t)bpop->bpo_num_blkptrs);
438 	(void) printf("\t\tbytes = %s\n", bytes);
439 	if (size >= BPOBJ_SIZE_V1) {
440 		(void) printf("\t\tcomp = %s\n", comp);
441 		(void) printf("\t\tuncomp = %s\n", uncomp);
442 	}
443 	if (size >= sizeof (*bpop)) {
444 		(void) printf("\t\tsubobjs = %llu\n",
445 		    (u_longlong_t)bpop->bpo_subobjs);
446 		(void) printf("\t\tnum_subobjs = %llu\n",
447 		    (u_longlong_t)bpop->bpo_num_subobjs);
448 	}
449 
450 	if (dump_opt['d'] < 5)
451 		return;
452 
453 	for (uint64_t i = 0; i < bpop->bpo_num_blkptrs; i++) {
454 		char blkbuf[BP_SPRINTF_LEN];
455 		blkptr_t bp;
456 
457 		int err = dmu_read(os, object,
458 		    i * sizeof (bp), sizeof (bp), &bp, 0);
459 		if (err != 0) {
460 			(void) printf("got error %u from dmu_read\n", err);
461 			break;
462 		}
463 		snprintf_blkptr_compact(blkbuf, sizeof (blkbuf), &bp);
464 		(void) printf("\t%s\n", blkbuf);
465 	}
466 }
467 
468 /* ARGSUSED */
469 static void
470 dump_bpobj_subobjs(objset_t *os, uint64_t object, void *data, size_t size)
471 {
472 	dmu_object_info_t doi;
473 
474 	VERIFY0(dmu_object_info(os, object, &doi));
475 	uint64_t *subobjs = kmem_alloc(doi.doi_max_offset, KM_SLEEP);
476 
477 	int err = dmu_read(os, object, 0, doi.doi_max_offset, subobjs, 0);
478 	if (err != 0) {
479 		(void) printf("got error %u from dmu_read\n", err);
480 		kmem_free(subobjs, doi.doi_max_offset);
481 		return;
482 	}
483 
484 	int64_t last_nonzero = -1;
485 	for (uint64_t i = 0; i < doi.doi_max_offset / 8; i++) {
486 		if (subobjs[i] != 0)
487 			last_nonzero = i;
488 	}
489 
490 	for (int64_t i = 0; i <= last_nonzero; i++) {
491 		(void) printf("\t%llu\n", (longlong_t)subobjs[i]);
492 	}
493 	kmem_free(subobjs, doi.doi_max_offset);
494 }
495 
496 /*ARGSUSED*/
497 static void
498 dump_ddt_zap(objset_t *os, uint64_t object, void *data, size_t size)
499 {
500 	dump_zap_stats(os, object);
501 	/* contents are printed elsewhere, properly decoded */
502 }
503 
504 /*ARGSUSED*/
505 static void
506 dump_sa_attrs(objset_t *os, uint64_t object, void *data, size_t size)
507 {
508 	zap_cursor_t zc;
509 	zap_attribute_t attr;
510 
511 	dump_zap_stats(os, object);
512 	(void) printf("\n");
513 
514 	for (zap_cursor_init(&zc, os, object);
515 	    zap_cursor_retrieve(&zc, &attr) == 0;
516 	    zap_cursor_advance(&zc)) {
517 		(void) printf("\t\t%s = ", attr.za_name);
518 		if (attr.za_num_integers == 0) {
519 			(void) printf("\n");
520 			continue;
521 		}
522 		(void) printf(" %llx : [%d:%d:%d]\n",
523 		    (u_longlong_t)attr.za_first_integer,
524 		    (int)ATTR_LENGTH(attr.za_first_integer),
525 		    (int)ATTR_BSWAP(attr.za_first_integer),
526 		    (int)ATTR_NUM(attr.za_first_integer));
527 	}
528 	zap_cursor_fini(&zc);
529 }
530 
531 /*ARGSUSED*/
532 static void
533 dump_sa_layouts(objset_t *os, uint64_t object, void *data, size_t size)
534 {
535 	zap_cursor_t zc;
536 	zap_attribute_t attr;
537 	uint16_t *layout_attrs;
538 	int i;
539 
540 	dump_zap_stats(os, object);
541 	(void) printf("\n");
542 
543 	for (zap_cursor_init(&zc, os, object);
544 	    zap_cursor_retrieve(&zc, &attr) == 0;
545 	    zap_cursor_advance(&zc)) {
546 		(void) printf("\t\t%s = [", attr.za_name);
547 		if (attr.za_num_integers == 0) {
548 			(void) printf("\n");
549 			continue;
550 		}
551 
552 		VERIFY(attr.za_integer_length == 2);
553 		layout_attrs = umem_zalloc(attr.za_num_integers *
554 		    attr.za_integer_length, UMEM_NOFAIL);
555 
556 		VERIFY(zap_lookup(os, object, attr.za_name,
557 		    attr.za_integer_length,
558 		    attr.za_num_integers, layout_attrs) == 0);
559 
560 		for (i = 0; i != attr.za_num_integers; i++)
561 			(void) printf(" %d ", (int)layout_attrs[i]);
562 		(void) printf("]\n");
563 		umem_free(layout_attrs,
564 		    attr.za_num_integers * attr.za_integer_length);
565 	}
566 	zap_cursor_fini(&zc);
567 }
568 
569 /*ARGSUSED*/
570 static void
571 dump_zpldir(objset_t *os, uint64_t object, void *data, size_t size)
572 {
573 	zap_cursor_t zc;
574 	zap_attribute_t attr;
575 	const char *typenames[] = {
576 		/* 0 */ "not specified",
577 		/* 1 */ "FIFO",
578 		/* 2 */ "Character Device",
579 		/* 3 */ "3 (invalid)",
580 		/* 4 */ "Directory",
581 		/* 5 */ "5 (invalid)",
582 		/* 6 */ "Block Device",
583 		/* 7 */ "7 (invalid)",
584 		/* 8 */ "Regular File",
585 		/* 9 */ "9 (invalid)",
586 		/* 10 */ "Symbolic Link",
587 		/* 11 */ "11 (invalid)",
588 		/* 12 */ "Socket",
589 		/* 13 */ "Door",
590 		/* 14 */ "Event Port",
591 		/* 15 */ "15 (invalid)",
592 	};
593 
594 	dump_zap_stats(os, object);
595 	(void) printf("\n");
596 
597 	for (zap_cursor_init(&zc, os, object);
598 	    zap_cursor_retrieve(&zc, &attr) == 0;
599 	    zap_cursor_advance(&zc)) {
600 		(void) printf("\t\t%s = %lld (type: %s)\n",
601 		    attr.za_name, ZFS_DIRENT_OBJ(attr.za_first_integer),
602 		    typenames[ZFS_DIRENT_TYPE(attr.za_first_integer)]);
603 	}
604 	zap_cursor_fini(&zc);
605 }
606 
607 int
608 get_dtl_refcount(vdev_t *vd)
609 {
610 	int refcount = 0;
611 
612 	if (vd->vdev_ops->vdev_op_leaf) {
613 		space_map_t *sm = vd->vdev_dtl_sm;
614 
615 		if (sm != NULL &&
616 		    sm->sm_dbuf->db_size == sizeof (space_map_phys_t))
617 			return (1);
618 		return (0);
619 	}
620 
621 	for (int c = 0; c < vd->vdev_children; c++)
622 		refcount += get_dtl_refcount(vd->vdev_child[c]);
623 	return (refcount);
624 }
625 
626 int
627 get_metaslab_refcount(vdev_t *vd)
628 {
629 	int refcount = 0;
630 
631 	if (vd->vdev_top == vd && !vd->vdev_removing) {
632 		for (int m = 0; m < vd->vdev_ms_count; m++) {
633 			space_map_t *sm = vd->vdev_ms[m]->ms_sm;
634 
635 			if (sm != NULL &&
636 			    sm->sm_dbuf->db_size == sizeof (space_map_phys_t))
637 				refcount++;
638 		}
639 	}
640 	for (int c = 0; c < vd->vdev_children; c++)
641 		refcount += get_metaslab_refcount(vd->vdev_child[c]);
642 
643 	return (refcount);
644 }
645 
646 static int
647 verify_spacemap_refcounts(spa_t *spa)
648 {
649 	uint64_t expected_refcount = 0;
650 	uint64_t actual_refcount;
651 
652 	(void) feature_get_refcount(spa,
653 	    &spa_feature_table[SPA_FEATURE_SPACEMAP_HISTOGRAM],
654 	    &expected_refcount);
655 	actual_refcount = get_dtl_refcount(spa->spa_root_vdev);
656 	actual_refcount += get_metaslab_refcount(spa->spa_root_vdev);
657 
658 	if (expected_refcount != actual_refcount) {
659 		(void) printf("space map refcount mismatch: expected %lld != "
660 		    "actual %lld\n",
661 		    (longlong_t)expected_refcount,
662 		    (longlong_t)actual_refcount);
663 		return (2);
664 	}
665 	return (0);
666 }
667 
668 static void
669 dump_spacemap(objset_t *os, space_map_t *sm)
670 {
671 	uint64_t alloc, offset, entry;
672 	char *ddata[] = { "ALLOC", "FREE", "CONDENSE", "INVALID",
673 			    "INVALID", "INVALID", "INVALID", "INVALID" };
674 
675 	if (sm == NULL)
676 		return;
677 
678 	/*
679 	 * Print out the freelist entries in both encoded and decoded form.
680 	 */
681 	alloc = 0;
682 	for (offset = 0; offset < space_map_length(sm);
683 	    offset += sizeof (entry)) {
684 		uint8_t mapshift = sm->sm_shift;
685 
686 		VERIFY0(dmu_read(os, space_map_object(sm), offset,
687 		    sizeof (entry), &entry, DMU_READ_PREFETCH));
688 		if (SM_DEBUG_DECODE(entry)) {
689 
690 			(void) printf("\t    [%6llu] %s: txg %llu, pass %llu\n",
691 			    (u_longlong_t)(offset / sizeof (entry)),
692 			    ddata[SM_DEBUG_ACTION_DECODE(entry)],
693 			    (u_longlong_t)SM_DEBUG_TXG_DECODE(entry),
694 			    (u_longlong_t)SM_DEBUG_SYNCPASS_DECODE(entry));
695 		} else {
696 			(void) printf("\t    [%6llu]    %c  range:"
697 			    " %010llx-%010llx  size: %06llx\n",
698 			    (u_longlong_t)(offset / sizeof (entry)),
699 			    SM_TYPE_DECODE(entry) == SM_ALLOC ? 'A' : 'F',
700 			    (u_longlong_t)((SM_OFFSET_DECODE(entry) <<
701 			    mapshift) + sm->sm_start),
702 			    (u_longlong_t)((SM_OFFSET_DECODE(entry) <<
703 			    mapshift) + sm->sm_start +
704 			    (SM_RUN_DECODE(entry) << mapshift)),
705 			    (u_longlong_t)(SM_RUN_DECODE(entry) << mapshift));
706 			if (SM_TYPE_DECODE(entry) == SM_ALLOC)
707 				alloc += SM_RUN_DECODE(entry) << mapshift;
708 			else
709 				alloc -= SM_RUN_DECODE(entry) << mapshift;
710 		}
711 	}
712 	if (alloc != space_map_allocated(sm)) {
713 		(void) printf("space_map_object alloc (%llu) INCONSISTENT "
714 		    "with space map summary (%llu)\n",
715 		    (u_longlong_t)space_map_allocated(sm), (u_longlong_t)alloc);
716 	}
717 }
718 
719 static void
720 dump_metaslab_stats(metaslab_t *msp)
721 {
722 	char maxbuf[32];
723 	range_tree_t *rt = msp->ms_tree;
724 	avl_tree_t *t = &msp->ms_size_tree;
725 	int free_pct = range_tree_space(rt) * 100 / msp->ms_size;
726 
727 	zdb_nicenum(metaslab_block_maxsize(msp), maxbuf);
728 
729 	(void) printf("\t %25s %10lu   %7s  %6s   %4s %4d%%\n",
730 	    "segments", avl_numnodes(t), "maxsize", maxbuf,
731 	    "freepct", free_pct);
732 	(void) printf("\tIn-memory histogram:\n");
733 	dump_histogram(rt->rt_histogram, RANGE_TREE_HISTOGRAM_SIZE, 0);
734 }
735 
736 static void
737 dump_metaslab(metaslab_t *msp)
738 {
739 	vdev_t *vd = msp->ms_group->mg_vd;
740 	spa_t *spa = vd->vdev_spa;
741 	space_map_t *sm = msp->ms_sm;
742 	char freebuf[32];
743 
744 	zdb_nicenum(msp->ms_size - space_map_allocated(sm), freebuf);
745 
746 	(void) printf(
747 	    "\tmetaslab %6llu   offset %12llx   spacemap %6llu   free    %5s\n",
748 	    (u_longlong_t)msp->ms_id, (u_longlong_t)msp->ms_start,
749 	    (u_longlong_t)space_map_object(sm), freebuf);
750 
751 	if (dump_opt['m'] > 2 && !dump_opt['L']) {
752 		mutex_enter(&msp->ms_lock);
753 		metaslab_load_wait(msp);
754 		if (!msp->ms_loaded) {
755 			VERIFY0(metaslab_load(msp));
756 			range_tree_stat_verify(msp->ms_tree);
757 		}
758 		dump_metaslab_stats(msp);
759 		metaslab_unload(msp);
760 		mutex_exit(&msp->ms_lock);
761 	}
762 
763 	if (dump_opt['m'] > 1 && sm != NULL &&
764 	    spa_feature_is_active(spa, SPA_FEATURE_SPACEMAP_HISTOGRAM)) {
765 		/*
766 		 * The space map histogram represents free space in chunks
767 		 * of sm_shift (i.e. bucket 0 refers to 2^sm_shift).
768 		 */
769 		(void) printf("\tOn-disk histogram:\t\tfragmentation %llu\n",
770 		    (u_longlong_t)msp->ms_fragmentation);
771 		dump_histogram(sm->sm_phys->smp_histogram,
772 		    SPACE_MAP_HISTOGRAM_SIZE, sm->sm_shift);
773 	}
774 
775 	if (dump_opt['d'] > 5 || dump_opt['m'] > 3) {
776 		ASSERT(msp->ms_size == (1ULL << vd->vdev_ms_shift));
777 
778 		mutex_enter(&msp->ms_lock);
779 		dump_spacemap(spa->spa_meta_objset, msp->ms_sm);
780 		mutex_exit(&msp->ms_lock);
781 	}
782 }
783 
784 static void
785 print_vdev_metaslab_header(vdev_t *vd)
786 {
787 	(void) printf("\tvdev %10llu\n\t%-10s%5llu   %-19s   %-15s   %-10s\n",
788 	    (u_longlong_t)vd->vdev_id,
789 	    "metaslabs", (u_longlong_t)vd->vdev_ms_count,
790 	    "offset", "spacemap", "free");
791 	(void) printf("\t%15s   %19s   %15s   %10s\n",
792 	    "---------------", "-------------------",
793 	    "---------------", "-------------");
794 }
795 
796 static void
797 dump_metaslab_groups(spa_t *spa)
798 {
799 	vdev_t *rvd = spa->spa_root_vdev;
800 	metaslab_class_t *mc = spa_normal_class(spa);
801 	uint64_t fragmentation;
802 
803 	metaslab_class_histogram_verify(mc);
804 
805 	for (int c = 0; c < rvd->vdev_children; c++) {
806 		vdev_t *tvd = rvd->vdev_child[c];
807 		metaslab_group_t *mg = tvd->vdev_mg;
808 
809 		if (mg->mg_class != mc)
810 			continue;
811 
812 		metaslab_group_histogram_verify(mg);
813 		mg->mg_fragmentation = metaslab_group_fragmentation(mg);
814 
815 		(void) printf("\tvdev %10llu\t\tmetaslabs%5llu\t\t"
816 		    "fragmentation",
817 		    (u_longlong_t)tvd->vdev_id,
818 		    (u_longlong_t)tvd->vdev_ms_count);
819 		if (mg->mg_fragmentation == ZFS_FRAG_INVALID) {
820 			(void) printf("%3s\n", "-");
821 		} else {
822 			(void) printf("%3llu%%\n",
823 			    (u_longlong_t)mg->mg_fragmentation);
824 		}
825 		dump_histogram(mg->mg_histogram, RANGE_TREE_HISTOGRAM_SIZE, 0);
826 	}
827 
828 	(void) printf("\tpool %s\tfragmentation", spa_name(spa));
829 	fragmentation = metaslab_class_fragmentation(mc);
830 	if (fragmentation == ZFS_FRAG_INVALID)
831 		(void) printf("\t%3s\n", "-");
832 	else
833 		(void) printf("\t%3llu%%\n", (u_longlong_t)fragmentation);
834 	dump_histogram(mc->mc_histogram, RANGE_TREE_HISTOGRAM_SIZE, 0);
835 }
836 
837 static void
838 dump_metaslabs(spa_t *spa)
839 {
840 	vdev_t *vd, *rvd = spa->spa_root_vdev;
841 	uint64_t m, c = 0, children = rvd->vdev_children;
842 
843 	(void) printf("\nMetaslabs:\n");
844 
845 	if (!dump_opt['d'] && zopt_objects > 0) {
846 		c = zopt_object[0];
847 
848 		if (c >= children)
849 			(void) fatal("bad vdev id: %llu", (u_longlong_t)c);
850 
851 		if (zopt_objects > 1) {
852 			vd = rvd->vdev_child[c];
853 			print_vdev_metaslab_header(vd);
854 
855 			for (m = 1; m < zopt_objects; m++) {
856 				if (zopt_object[m] < vd->vdev_ms_count)
857 					dump_metaslab(
858 					    vd->vdev_ms[zopt_object[m]]);
859 				else
860 					(void) fprintf(stderr, "bad metaslab "
861 					    "number %llu\n",
862 					    (u_longlong_t)zopt_object[m]);
863 			}
864 			(void) printf("\n");
865 			return;
866 		}
867 		children = c + 1;
868 	}
869 	for (; c < children; c++) {
870 		vd = rvd->vdev_child[c];
871 		print_vdev_metaslab_header(vd);
872 
873 		for (m = 0; m < vd->vdev_ms_count; m++)
874 			dump_metaslab(vd->vdev_ms[m]);
875 		(void) printf("\n");
876 	}
877 }
878 
879 static void
880 dump_dde(const ddt_t *ddt, const ddt_entry_t *dde, uint64_t index)
881 {
882 	const ddt_phys_t *ddp = dde->dde_phys;
883 	const ddt_key_t *ddk = &dde->dde_key;
884 	char *types[4] = { "ditto", "single", "double", "triple" };
885 	char blkbuf[BP_SPRINTF_LEN];
886 	blkptr_t blk;
887 
888 	for (int p = 0; p < DDT_PHYS_TYPES; p++, ddp++) {
889 		if (ddp->ddp_phys_birth == 0)
890 			continue;
891 		ddt_bp_create(ddt->ddt_checksum, ddk, ddp, &blk);
892 		snprintf_blkptr(blkbuf, sizeof (blkbuf), &blk);
893 		(void) printf("index %llx refcnt %llu %s %s\n",
894 		    (u_longlong_t)index, (u_longlong_t)ddp->ddp_refcnt,
895 		    types[p], blkbuf);
896 	}
897 }
898 
899 static void
900 dump_dedup_ratio(const ddt_stat_t *dds)
901 {
902 	double rL, rP, rD, D, dedup, compress, copies;
903 
904 	if (dds->dds_blocks == 0)
905 		return;
906 
907 	rL = (double)dds->dds_ref_lsize;
908 	rP = (double)dds->dds_ref_psize;
909 	rD = (double)dds->dds_ref_dsize;
910 	D = (double)dds->dds_dsize;
911 
912 	dedup = rD / D;
913 	compress = rL / rP;
914 	copies = rD / rP;
915 
916 	(void) printf("dedup = %.2f, compress = %.2f, copies = %.2f, "
917 	    "dedup * compress / copies = %.2f\n\n",
918 	    dedup, compress, copies, dedup * compress / copies);
919 }
920 
921 static void
922 dump_ddt(ddt_t *ddt, enum ddt_type type, enum ddt_class class)
923 {
924 	char name[DDT_NAMELEN];
925 	ddt_entry_t dde;
926 	uint64_t walk = 0;
927 	dmu_object_info_t doi;
928 	uint64_t count, dspace, mspace;
929 	int error;
930 
931 	error = ddt_object_info(ddt, type, class, &doi);
932 
933 	if (error == ENOENT)
934 		return;
935 	ASSERT(error == 0);
936 
937 	if ((count = ddt_object_count(ddt, type, class)) == 0)
938 		return;
939 
940 	dspace = doi.doi_physical_blocks_512 << 9;
941 	mspace = doi.doi_fill_count * doi.doi_data_block_size;
942 
943 	ddt_object_name(ddt, type, class, name);
944 
945 	(void) printf("%s: %llu entries, size %llu on disk, %llu in core\n",
946 	    name,
947 	    (u_longlong_t)count,
948 	    (u_longlong_t)(dspace / count),
949 	    (u_longlong_t)(mspace / count));
950 
951 	if (dump_opt['D'] < 3)
952 		return;
953 
954 	zpool_dump_ddt(NULL, &ddt->ddt_histogram[type][class]);
955 
956 	if (dump_opt['D'] < 4)
957 		return;
958 
959 	if (dump_opt['D'] < 5 && class == DDT_CLASS_UNIQUE)
960 		return;
961 
962 	(void) printf("%s contents:\n\n", name);
963 
964 	while ((error = ddt_object_walk(ddt, type, class, &walk, &dde)) == 0)
965 		dump_dde(ddt, &dde, walk);
966 
967 	ASSERT(error == ENOENT);
968 
969 	(void) printf("\n");
970 }
971 
972 static void
973 dump_all_ddts(spa_t *spa)
974 {
975 	ddt_histogram_t ddh_total = { 0 };
976 	ddt_stat_t dds_total = { 0 };
977 
978 	for (enum zio_checksum c = 0; c < ZIO_CHECKSUM_FUNCTIONS; c++) {
979 		ddt_t *ddt = spa->spa_ddt[c];
980 		for (enum ddt_type type = 0; type < DDT_TYPES; type++) {
981 			for (enum ddt_class class = 0; class < DDT_CLASSES;
982 			    class++) {
983 				dump_ddt(ddt, type, class);
984 			}
985 		}
986 	}
987 
988 	ddt_get_dedup_stats(spa, &dds_total);
989 
990 	if (dds_total.dds_blocks == 0) {
991 		(void) printf("All DDTs are empty\n");
992 		return;
993 	}
994 
995 	(void) printf("\n");
996 
997 	if (dump_opt['D'] > 1) {
998 		(void) printf("DDT histogram (aggregated over all DDTs):\n");
999 		ddt_get_dedup_histogram(spa, &ddh_total);
1000 		zpool_dump_ddt(&dds_total, &ddh_total);
1001 	}
1002 
1003 	dump_dedup_ratio(&dds_total);
1004 }
1005 
1006 static void
1007 dump_dtl_seg(void *arg, uint64_t start, uint64_t size)
1008 {
1009 	char *prefix = arg;
1010 
1011 	(void) printf("%s [%llu,%llu) length %llu\n",
1012 	    prefix,
1013 	    (u_longlong_t)start,
1014 	    (u_longlong_t)(start + size),
1015 	    (u_longlong_t)(size));
1016 }
1017 
1018 static void
1019 dump_dtl(vdev_t *vd, int indent)
1020 {
1021 	spa_t *spa = vd->vdev_spa;
1022 	boolean_t required;
1023 	char *name[DTL_TYPES] = { "missing", "partial", "scrub", "outage" };
1024 	char prefix[256];
1025 
1026 	spa_vdev_state_enter(spa, SCL_NONE);
1027 	required = vdev_dtl_required(vd);
1028 	(void) spa_vdev_state_exit(spa, NULL, 0);
1029 
1030 	if (indent == 0)
1031 		(void) printf("\nDirty time logs:\n\n");
1032 
1033 	(void) printf("\t%*s%s [%s]\n", indent, "",
1034 	    vd->vdev_path ? vd->vdev_path :
1035 	    vd->vdev_parent ? vd->vdev_ops->vdev_op_type : spa_name(spa),
1036 	    required ? "DTL-required" : "DTL-expendable");
1037 
1038 	for (int t = 0; t < DTL_TYPES; t++) {
1039 		range_tree_t *rt = vd->vdev_dtl[t];
1040 		if (range_tree_space(rt) == 0)
1041 			continue;
1042 		(void) snprintf(prefix, sizeof (prefix), "\t%*s%s",
1043 		    indent + 2, "", name[t]);
1044 		mutex_enter(rt->rt_lock);
1045 		range_tree_walk(rt, dump_dtl_seg, prefix);
1046 		mutex_exit(rt->rt_lock);
1047 		if (dump_opt['d'] > 5 && vd->vdev_children == 0)
1048 			dump_spacemap(spa->spa_meta_objset, vd->vdev_dtl_sm);
1049 	}
1050 
1051 	for (int c = 0; c < vd->vdev_children; c++)
1052 		dump_dtl(vd->vdev_child[c], indent + 4);
1053 }
1054 
1055 static void
1056 dump_history(spa_t *spa)
1057 {
1058 	nvlist_t **events = NULL;
1059 	char buf[SPA_MAXBLOCKSIZE];
1060 	uint64_t resid, len, off = 0;
1061 	uint_t num = 0;
1062 	int error;
1063 	time_t tsec;
1064 	struct tm t;
1065 	char tbuf[30];
1066 	char internalstr[MAXPATHLEN];
1067 
1068 	do {
1069 		len = sizeof (buf);
1070 
1071 		if ((error = spa_history_get(spa, &off, &len, buf)) != 0) {
1072 			(void) fprintf(stderr, "Unable to read history: "
1073 			    "error %d\n", error);
1074 			return;
1075 		}
1076 
1077 		if (zpool_history_unpack(buf, len, &resid, &events, &num) != 0)
1078 			break;
1079 
1080 		off -= resid;
1081 	} while (len != 0);
1082 
1083 	(void) printf("\nHistory:\n");
1084 	for (int i = 0; i < num; i++) {
1085 		uint64_t time, txg, ievent;
1086 		char *cmd, *intstr;
1087 		boolean_t printed = B_FALSE;
1088 
1089 		if (nvlist_lookup_uint64(events[i], ZPOOL_HIST_TIME,
1090 		    &time) != 0)
1091 			goto next;
1092 		if (nvlist_lookup_string(events[i], ZPOOL_HIST_CMD,
1093 		    &cmd) != 0) {
1094 			if (nvlist_lookup_uint64(events[i],
1095 			    ZPOOL_HIST_INT_EVENT, &ievent) != 0)
1096 				goto next;
1097 			verify(nvlist_lookup_uint64(events[i],
1098 			    ZPOOL_HIST_TXG, &txg) == 0);
1099 			verify(nvlist_lookup_string(events[i],
1100 			    ZPOOL_HIST_INT_STR, &intstr) == 0);
1101 			if (ievent >= ZFS_NUM_LEGACY_HISTORY_EVENTS)
1102 				goto next;
1103 
1104 			(void) snprintf(internalstr,
1105 			    sizeof (internalstr),
1106 			    "[internal %s txg:%lld] %s",
1107 			    zfs_history_event_names[ievent], txg,
1108 			    intstr);
1109 			cmd = internalstr;
1110 		}
1111 		tsec = time;
1112 		(void) localtime_r(&tsec, &t);
1113 		(void) strftime(tbuf, sizeof (tbuf), "%F.%T", &t);
1114 		(void) printf("%s %s\n", tbuf, cmd);
1115 		printed = B_TRUE;
1116 
1117 next:
1118 		if (dump_opt['h'] > 1) {
1119 			if (!printed)
1120 				(void) printf("unrecognized record:\n");
1121 			dump_nvlist(events[i], 2);
1122 		}
1123 	}
1124 }
1125 
1126 /*ARGSUSED*/
1127 static void
1128 dump_dnode(objset_t *os, uint64_t object, void *data, size_t size)
1129 {
1130 }
1131 
1132 static uint64_t
1133 blkid2offset(const dnode_phys_t *dnp, const blkptr_t *bp,
1134     const zbookmark_phys_t *zb)
1135 {
1136 	if (dnp == NULL) {
1137 		ASSERT(zb->zb_level < 0);
1138 		if (zb->zb_object == 0)
1139 			return (zb->zb_blkid);
1140 		return (zb->zb_blkid * BP_GET_LSIZE(bp));
1141 	}
1142 
1143 	ASSERT(zb->zb_level >= 0);
1144 
1145 	return ((zb->zb_blkid <<
1146 	    (zb->zb_level * (dnp->dn_indblkshift - SPA_BLKPTRSHIFT))) *
1147 	    dnp->dn_datablkszsec << SPA_MINBLOCKSHIFT);
1148 }
1149 
1150 static void
1151 snprintf_blkptr_compact(char *blkbuf, size_t buflen, const blkptr_t *bp)
1152 {
1153 	const dva_t *dva = bp->blk_dva;
1154 	int ndvas = dump_opt['d'] > 5 ? BP_GET_NDVAS(bp) : 1;
1155 
1156 	if (dump_opt['b'] >= 6) {
1157 		snprintf_blkptr(blkbuf, buflen, bp);
1158 		return;
1159 	}
1160 
1161 	if (BP_IS_EMBEDDED(bp)) {
1162 		(void) sprintf(blkbuf,
1163 		    "EMBEDDED et=%u %llxL/%llxP B=%llu",
1164 		    (int)BPE_GET_ETYPE(bp),
1165 		    (u_longlong_t)BPE_GET_LSIZE(bp),
1166 		    (u_longlong_t)BPE_GET_PSIZE(bp),
1167 		    (u_longlong_t)bp->blk_birth);
1168 		return;
1169 	}
1170 
1171 	blkbuf[0] = '\0';
1172 	for (int i = 0; i < ndvas; i++)
1173 		(void) snprintf(blkbuf + strlen(blkbuf),
1174 		    buflen - strlen(blkbuf), "%llu:%llx:%llx ",
1175 		    (u_longlong_t)DVA_GET_VDEV(&dva[i]),
1176 		    (u_longlong_t)DVA_GET_OFFSET(&dva[i]),
1177 		    (u_longlong_t)DVA_GET_ASIZE(&dva[i]));
1178 
1179 	if (BP_IS_HOLE(bp)) {
1180 		(void) snprintf(blkbuf + strlen(blkbuf),
1181 		    buflen - strlen(blkbuf),
1182 		    "%llxL B=%llu",
1183 		    (u_longlong_t)BP_GET_LSIZE(bp),
1184 		    (u_longlong_t)bp->blk_birth);
1185 	} else {
1186 		(void) snprintf(blkbuf + strlen(blkbuf),
1187 		    buflen - strlen(blkbuf),
1188 		    "%llxL/%llxP F=%llu B=%llu/%llu",
1189 		    (u_longlong_t)BP_GET_LSIZE(bp),
1190 		    (u_longlong_t)BP_GET_PSIZE(bp),
1191 		    (u_longlong_t)BP_GET_FILL(bp),
1192 		    (u_longlong_t)bp->blk_birth,
1193 		    (u_longlong_t)BP_PHYSICAL_BIRTH(bp));
1194 	}
1195 }
1196 
1197 static void
1198 print_indirect(blkptr_t *bp, const zbookmark_phys_t *zb,
1199     const dnode_phys_t *dnp)
1200 {
1201 	char blkbuf[BP_SPRINTF_LEN];
1202 	int l;
1203 
1204 	if (!BP_IS_EMBEDDED(bp)) {
1205 		ASSERT3U(BP_GET_TYPE(bp), ==, dnp->dn_type);
1206 		ASSERT3U(BP_GET_LEVEL(bp), ==, zb->zb_level);
1207 	}
1208 
1209 	(void) printf("%16llx ", (u_longlong_t)blkid2offset(dnp, bp, zb));
1210 
1211 	ASSERT(zb->zb_level >= 0);
1212 
1213 	for (l = dnp->dn_nlevels - 1; l >= -1; l--) {
1214 		if (l == zb->zb_level) {
1215 			(void) printf("L%llx", (u_longlong_t)zb->zb_level);
1216 		} else {
1217 			(void) printf(" ");
1218 		}
1219 	}
1220 
1221 	snprintf_blkptr_compact(blkbuf, sizeof (blkbuf), bp);
1222 	(void) printf("%s\n", blkbuf);
1223 }
1224 
1225 static int
1226 visit_indirect(spa_t *spa, const dnode_phys_t *dnp,
1227     blkptr_t *bp, const zbookmark_phys_t *zb)
1228 {
1229 	int err = 0;
1230 
1231 	if (bp->blk_birth == 0)
1232 		return (0);
1233 
1234 	print_indirect(bp, zb, dnp);
1235 
1236 	if (BP_GET_LEVEL(bp) > 0 && !BP_IS_HOLE(bp)) {
1237 		arc_flags_t flags = ARC_FLAG_WAIT;
1238 		int i;
1239 		blkptr_t *cbp;
1240 		int epb = BP_GET_LSIZE(bp) >> SPA_BLKPTRSHIFT;
1241 		arc_buf_t *buf;
1242 		uint64_t fill = 0;
1243 
1244 		err = arc_read(NULL, spa, bp, arc_getbuf_func, &buf,
1245 		    ZIO_PRIORITY_ASYNC_READ, ZIO_FLAG_CANFAIL, &flags, zb);
1246 		if (err)
1247 			return (err);
1248 		ASSERT(buf->b_data);
1249 
1250 		/* recursively visit blocks below this */
1251 		cbp = buf->b_data;
1252 		for (i = 0; i < epb; i++, cbp++) {
1253 			zbookmark_phys_t czb;
1254 
1255 			SET_BOOKMARK(&czb, zb->zb_objset, zb->zb_object,
1256 			    zb->zb_level - 1,
1257 			    zb->zb_blkid * epb + i);
1258 			err = visit_indirect(spa, dnp, cbp, &czb);
1259 			if (err)
1260 				break;
1261 			fill += BP_GET_FILL(cbp);
1262 		}
1263 		if (!err)
1264 			ASSERT3U(fill, ==, BP_GET_FILL(bp));
1265 		(void) arc_buf_remove_ref(buf, &buf);
1266 	}
1267 
1268 	return (err);
1269 }
1270 
1271 /*ARGSUSED*/
1272 static void
1273 dump_indirect(dnode_t *dn)
1274 {
1275 	dnode_phys_t *dnp = dn->dn_phys;
1276 	int j;
1277 	zbookmark_phys_t czb;
1278 
1279 	(void) printf("Indirect blocks:\n");
1280 
1281 	SET_BOOKMARK(&czb, dmu_objset_id(dn->dn_objset),
1282 	    dn->dn_object, dnp->dn_nlevels - 1, 0);
1283 	for (j = 0; j < dnp->dn_nblkptr; j++) {
1284 		czb.zb_blkid = j;
1285 		(void) visit_indirect(dmu_objset_spa(dn->dn_objset), dnp,
1286 		    &dnp->dn_blkptr[j], &czb);
1287 	}
1288 
1289 	(void) printf("\n");
1290 }
1291 
1292 /*ARGSUSED*/
1293 static void
1294 dump_dsl_dir(objset_t *os, uint64_t object, void *data, size_t size)
1295 {
1296 	dsl_dir_phys_t *dd = data;
1297 	time_t crtime;
1298 	char nice[32];
1299 
1300 	if (dd == NULL)
1301 		return;
1302 
1303 	ASSERT3U(size, >=, sizeof (dsl_dir_phys_t));
1304 
1305 	crtime = dd->dd_creation_time;
1306 	(void) printf("\t\tcreation_time = %s", ctime(&crtime));
1307 	(void) printf("\t\thead_dataset_obj = %llu\n",
1308 	    (u_longlong_t)dd->dd_head_dataset_obj);
1309 	(void) printf("\t\tparent_dir_obj = %llu\n",
1310 	    (u_longlong_t)dd->dd_parent_obj);
1311 	(void) printf("\t\torigin_obj = %llu\n",
1312 	    (u_longlong_t)dd->dd_origin_obj);
1313 	(void) printf("\t\tchild_dir_zapobj = %llu\n",
1314 	    (u_longlong_t)dd->dd_child_dir_zapobj);
1315 	zdb_nicenum(dd->dd_used_bytes, nice);
1316 	(void) printf("\t\tused_bytes = %s\n", nice);
1317 	zdb_nicenum(dd->dd_compressed_bytes, nice);
1318 	(void) printf("\t\tcompressed_bytes = %s\n", nice);
1319 	zdb_nicenum(dd->dd_uncompressed_bytes, nice);
1320 	(void) printf("\t\tuncompressed_bytes = %s\n", nice);
1321 	zdb_nicenum(dd->dd_quota, nice);
1322 	(void) printf("\t\tquota = %s\n", nice);
1323 	zdb_nicenum(dd->dd_reserved, nice);
1324 	(void) printf("\t\treserved = %s\n", nice);
1325 	(void) printf("\t\tprops_zapobj = %llu\n",
1326 	    (u_longlong_t)dd->dd_props_zapobj);
1327 	(void) printf("\t\tdeleg_zapobj = %llu\n",
1328 	    (u_longlong_t)dd->dd_deleg_zapobj);
1329 	(void) printf("\t\tflags = %llx\n",
1330 	    (u_longlong_t)dd->dd_flags);
1331 
1332 #define	DO(which) \
1333 	zdb_nicenum(dd->dd_used_breakdown[DD_USED_ ## which], nice); \
1334 	(void) printf("\t\tused_breakdown[" #which "] = %s\n", nice)
1335 	DO(HEAD);
1336 	DO(SNAP);
1337 	DO(CHILD);
1338 	DO(CHILD_RSRV);
1339 	DO(REFRSRV);
1340 #undef DO
1341 }
1342 
1343 /*ARGSUSED*/
1344 static void
1345 dump_dsl_dataset(objset_t *os, uint64_t object, void *data, size_t size)
1346 {
1347 	dsl_dataset_phys_t *ds = data;
1348 	time_t crtime;
1349 	char used[32], compressed[32], uncompressed[32], unique[32];
1350 	char blkbuf[BP_SPRINTF_LEN];
1351 
1352 	if (ds == NULL)
1353 		return;
1354 
1355 	ASSERT(size == sizeof (*ds));
1356 	crtime = ds->ds_creation_time;
1357 	zdb_nicenum(ds->ds_referenced_bytes, used);
1358 	zdb_nicenum(ds->ds_compressed_bytes, compressed);
1359 	zdb_nicenum(ds->ds_uncompressed_bytes, uncompressed);
1360 	zdb_nicenum(ds->ds_unique_bytes, unique);
1361 	snprintf_blkptr(blkbuf, sizeof (blkbuf), &ds->ds_bp);
1362 
1363 	(void) printf("\t\tdir_obj = %llu\n",
1364 	    (u_longlong_t)ds->ds_dir_obj);
1365 	(void) printf("\t\tprev_snap_obj = %llu\n",
1366 	    (u_longlong_t)ds->ds_prev_snap_obj);
1367 	(void) printf("\t\tprev_snap_txg = %llu\n",
1368 	    (u_longlong_t)ds->ds_prev_snap_txg);
1369 	(void) printf("\t\tnext_snap_obj = %llu\n",
1370 	    (u_longlong_t)ds->ds_next_snap_obj);
1371 	(void) printf("\t\tsnapnames_zapobj = %llu\n",
1372 	    (u_longlong_t)ds->ds_snapnames_zapobj);
1373 	(void) printf("\t\tnum_children = %llu\n",
1374 	    (u_longlong_t)ds->ds_num_children);
1375 	(void) printf("\t\tuserrefs_obj = %llu\n",
1376 	    (u_longlong_t)ds->ds_userrefs_obj);
1377 	(void) printf("\t\tcreation_time = %s", ctime(&crtime));
1378 	(void) printf("\t\tcreation_txg = %llu\n",
1379 	    (u_longlong_t)ds->ds_creation_txg);
1380 	(void) printf("\t\tdeadlist_obj = %llu\n",
1381 	    (u_longlong_t)ds->ds_deadlist_obj);
1382 	(void) printf("\t\tused_bytes = %s\n", used);
1383 	(void) printf("\t\tcompressed_bytes = %s\n", compressed);
1384 	(void) printf("\t\tuncompressed_bytes = %s\n", uncompressed);
1385 	(void) printf("\t\tunique = %s\n", unique);
1386 	(void) printf("\t\tfsid_guid = %llu\n",
1387 	    (u_longlong_t)ds->ds_fsid_guid);
1388 	(void) printf("\t\tguid = %llu\n",
1389 	    (u_longlong_t)ds->ds_guid);
1390 	(void) printf("\t\tflags = %llx\n",
1391 	    (u_longlong_t)ds->ds_flags);
1392 	(void) printf("\t\tnext_clones_obj = %llu\n",
1393 	    (u_longlong_t)ds->ds_next_clones_obj);
1394 	(void) printf("\t\tprops_obj = %llu\n",
1395 	    (u_longlong_t)ds->ds_props_obj);
1396 	(void) printf("\t\tbp = %s\n", blkbuf);
1397 }
1398 
1399 /* ARGSUSED */
1400 static int
1401 dump_bptree_cb(void *arg, const blkptr_t *bp, dmu_tx_t *tx)
1402 {
1403 	char blkbuf[BP_SPRINTF_LEN];
1404 
1405 	if (bp->blk_birth != 0) {
1406 		snprintf_blkptr(blkbuf, sizeof (blkbuf), bp);
1407 		(void) printf("\t%s\n", blkbuf);
1408 	}
1409 	return (0);
1410 }
1411 
1412 static void
1413 dump_bptree(objset_t *os, uint64_t obj, char *name)
1414 {
1415 	char bytes[32];
1416 	bptree_phys_t *bt;
1417 	dmu_buf_t *db;
1418 
1419 	if (dump_opt['d'] < 3)
1420 		return;
1421 
1422 	VERIFY3U(0, ==, dmu_bonus_hold(os, obj, FTAG, &db));
1423 	bt = db->db_data;
1424 	zdb_nicenum(bt->bt_bytes, bytes);
1425 	(void) printf("\n    %s: %llu datasets, %s\n",
1426 	    name, (unsigned long long)(bt->bt_end - bt->bt_begin), bytes);
1427 	dmu_buf_rele(db, FTAG);
1428 
1429 	if (dump_opt['d'] < 5)
1430 		return;
1431 
1432 	(void) printf("\n");
1433 
1434 	(void) bptree_iterate(os, obj, B_FALSE, dump_bptree_cb, NULL, NULL);
1435 }
1436 
1437 /* ARGSUSED */
1438 static int
1439 dump_bpobj_cb(void *arg, const blkptr_t *bp, dmu_tx_t *tx)
1440 {
1441 	char blkbuf[BP_SPRINTF_LEN];
1442 
1443 	ASSERT(bp->blk_birth != 0);
1444 	snprintf_blkptr_compact(blkbuf, sizeof (blkbuf), bp);
1445 	(void) printf("\t%s\n", blkbuf);
1446 	return (0);
1447 }
1448 
1449 static void
1450 dump_full_bpobj(bpobj_t *bpo, char *name, int indent)
1451 {
1452 	char bytes[32];
1453 	char comp[32];
1454 	char uncomp[32];
1455 
1456 	if (dump_opt['d'] < 3)
1457 		return;
1458 
1459 	zdb_nicenum(bpo->bpo_phys->bpo_bytes, bytes);
1460 	if (bpo->bpo_havesubobj && bpo->bpo_phys->bpo_subobjs != 0) {
1461 		zdb_nicenum(bpo->bpo_phys->bpo_comp, comp);
1462 		zdb_nicenum(bpo->bpo_phys->bpo_uncomp, uncomp);
1463 		(void) printf("    %*s: object %llu, %llu local blkptrs, "
1464 		    "%llu subobjs in object %llu, %s (%s/%s comp)\n",
1465 		    indent * 8, name,
1466 		    (u_longlong_t)bpo->bpo_object,
1467 		    (u_longlong_t)bpo->bpo_phys->bpo_num_blkptrs,
1468 		    (u_longlong_t)bpo->bpo_phys->bpo_num_subobjs,
1469 		    (u_longlong_t)bpo->bpo_phys->bpo_subobjs,
1470 		    bytes, comp, uncomp);
1471 
1472 		for (uint64_t i = 0; i < bpo->bpo_phys->bpo_num_subobjs; i++) {
1473 			uint64_t subobj;
1474 			bpobj_t subbpo;
1475 			int error;
1476 			VERIFY0(dmu_read(bpo->bpo_os,
1477 			    bpo->bpo_phys->bpo_subobjs,
1478 			    i * sizeof (subobj), sizeof (subobj), &subobj, 0));
1479 			error = bpobj_open(&subbpo, bpo->bpo_os, subobj);
1480 			if (error != 0) {
1481 				(void) printf("ERROR %u while trying to open "
1482 				    "subobj id %llu\n",
1483 				    error, (u_longlong_t)subobj);
1484 				continue;
1485 			}
1486 			dump_full_bpobj(&subbpo, "subobj", indent + 1);
1487 			bpobj_close(&subbpo);
1488 		}
1489 	} else {
1490 		(void) printf("    %*s: object %llu, %llu blkptrs, %s\n",
1491 		    indent * 8, name,
1492 		    (u_longlong_t)bpo->bpo_object,
1493 		    (u_longlong_t)bpo->bpo_phys->bpo_num_blkptrs,
1494 		    bytes);
1495 	}
1496 
1497 	if (dump_opt['d'] < 5)
1498 		return;
1499 
1500 
1501 	if (indent == 0) {
1502 		(void) bpobj_iterate_nofree(bpo, dump_bpobj_cb, NULL, NULL);
1503 		(void) printf("\n");
1504 	}
1505 }
1506 
1507 static void
1508 dump_deadlist(dsl_deadlist_t *dl)
1509 {
1510 	dsl_deadlist_entry_t *dle;
1511 	uint64_t unused;
1512 	char bytes[32];
1513 	char comp[32];
1514 	char uncomp[32];
1515 
1516 	if (dump_opt['d'] < 3)
1517 		return;
1518 
1519 	if (dl->dl_oldfmt) {
1520 		dump_full_bpobj(&dl->dl_bpobj, "old-format deadlist", 0);
1521 		return;
1522 	}
1523 
1524 	zdb_nicenum(dl->dl_phys->dl_used, bytes);
1525 	zdb_nicenum(dl->dl_phys->dl_comp, comp);
1526 	zdb_nicenum(dl->dl_phys->dl_uncomp, uncomp);
1527 	(void) printf("\n    Deadlist: %s (%s/%s comp)\n",
1528 	    bytes, comp, uncomp);
1529 
1530 	if (dump_opt['d'] < 4)
1531 		return;
1532 
1533 	(void) printf("\n");
1534 
1535 	/* force the tree to be loaded */
1536 	dsl_deadlist_space_range(dl, 0, UINT64_MAX, &unused, &unused, &unused);
1537 
1538 	for (dle = avl_first(&dl->dl_tree); dle;
1539 	    dle = AVL_NEXT(&dl->dl_tree, dle)) {
1540 		if (dump_opt['d'] >= 5) {
1541 			char buf[128];
1542 			(void) snprintf(buf, sizeof (buf), "mintxg %llu -> ",
1543 			    (longlong_t)dle->dle_mintxg,
1544 			    (longlong_t)dle->dle_bpobj.bpo_object);
1545 
1546 			dump_full_bpobj(&dle->dle_bpobj, buf, 0);
1547 		} else {
1548 			(void) printf("mintxg %llu -> obj %llu\n",
1549 			    (longlong_t)dle->dle_mintxg,
1550 			    (longlong_t)dle->dle_bpobj.bpo_object);
1551 
1552 		}
1553 	}
1554 }
1555 
1556 static avl_tree_t idx_tree;
1557 static avl_tree_t domain_tree;
1558 static boolean_t fuid_table_loaded;
1559 static boolean_t sa_loaded;
1560 sa_attr_type_t *sa_attr_table;
1561 
1562 static void
1563 fuid_table_destroy()
1564 {
1565 	if (fuid_table_loaded) {
1566 		zfs_fuid_table_destroy(&idx_tree, &domain_tree);
1567 		fuid_table_loaded = B_FALSE;
1568 	}
1569 }
1570 
1571 /*
1572  * print uid or gid information.
1573  * For normal POSIX id just the id is printed in decimal format.
1574  * For CIFS files with FUID the fuid is printed in hex followed by
1575  * the domain-rid string.
1576  */
1577 static void
1578 print_idstr(uint64_t id, const char *id_type)
1579 {
1580 	if (FUID_INDEX(id)) {
1581 		char *domain;
1582 
1583 		domain = zfs_fuid_idx_domain(&idx_tree, FUID_INDEX(id));
1584 		(void) printf("\t%s     %llx [%s-%d]\n", id_type,
1585 		    (u_longlong_t)id, domain, (int)FUID_RID(id));
1586 	} else {
1587 		(void) printf("\t%s     %llu\n", id_type, (u_longlong_t)id);
1588 	}
1589 
1590 }
1591 
1592 static void
1593 dump_uidgid(objset_t *os, uint64_t uid, uint64_t gid)
1594 {
1595 	uint32_t uid_idx, gid_idx;
1596 
1597 	uid_idx = FUID_INDEX(uid);
1598 	gid_idx = FUID_INDEX(gid);
1599 
1600 	/* Load domain table, if not already loaded */
1601 	if (!fuid_table_loaded && (uid_idx || gid_idx)) {
1602 		uint64_t fuid_obj;
1603 
1604 		/* first find the fuid object.  It lives in the master node */
1605 		VERIFY(zap_lookup(os, MASTER_NODE_OBJ, ZFS_FUID_TABLES,
1606 		    8, 1, &fuid_obj) == 0);
1607 		zfs_fuid_avl_tree_create(&idx_tree, &domain_tree);
1608 		(void) zfs_fuid_table_load(os, fuid_obj,
1609 		    &idx_tree, &domain_tree);
1610 		fuid_table_loaded = B_TRUE;
1611 	}
1612 
1613 	print_idstr(uid, "uid");
1614 	print_idstr(gid, "gid");
1615 }
1616 
1617 /*ARGSUSED*/
1618 static void
1619 dump_znode(objset_t *os, uint64_t object, void *data, size_t size)
1620 {
1621 	char path[MAXPATHLEN * 2];	/* allow for xattr and failure prefix */
1622 	sa_handle_t *hdl;
1623 	uint64_t xattr, rdev, gen;
1624 	uint64_t uid, gid, mode, fsize, parent, links;
1625 	uint64_t pflags;
1626 	uint64_t acctm[2], modtm[2], chgtm[2], crtm[2];
1627 	time_t z_crtime, z_atime, z_mtime, z_ctime;
1628 	sa_bulk_attr_t bulk[12];
1629 	int idx = 0;
1630 	int error;
1631 
1632 	if (!sa_loaded) {
1633 		uint64_t sa_attrs = 0;
1634 		uint64_t version;
1635 
1636 		VERIFY(zap_lookup(os, MASTER_NODE_OBJ, ZPL_VERSION_STR,
1637 		    8, 1, &version) == 0);
1638 		if (version >= ZPL_VERSION_SA) {
1639 			VERIFY(zap_lookup(os, MASTER_NODE_OBJ, ZFS_SA_ATTRS,
1640 			    8, 1, &sa_attrs) == 0);
1641 		}
1642 		if ((error = sa_setup(os, sa_attrs, zfs_attr_table,
1643 		    ZPL_END, &sa_attr_table)) != 0) {
1644 			(void) printf("sa_setup failed errno %d, can't "
1645 			    "display znode contents\n", error);
1646 			return;
1647 		}
1648 		sa_loaded = B_TRUE;
1649 	}
1650 
1651 	if (sa_handle_get(os, object, NULL, SA_HDL_PRIVATE, &hdl)) {
1652 		(void) printf("Failed to get handle for SA znode\n");
1653 		return;
1654 	}
1655 
1656 	SA_ADD_BULK_ATTR(bulk, idx, sa_attr_table[ZPL_UID], NULL, &uid, 8);
1657 	SA_ADD_BULK_ATTR(bulk, idx, sa_attr_table[ZPL_GID], NULL, &gid, 8);
1658 	SA_ADD_BULK_ATTR(bulk, idx, sa_attr_table[ZPL_LINKS], NULL,
1659 	    &links, 8);
1660 	SA_ADD_BULK_ATTR(bulk, idx, sa_attr_table[ZPL_GEN], NULL, &gen, 8);
1661 	SA_ADD_BULK_ATTR(bulk, idx, sa_attr_table[ZPL_MODE], NULL,
1662 	    &mode, 8);
1663 	SA_ADD_BULK_ATTR(bulk, idx, sa_attr_table[ZPL_PARENT],
1664 	    NULL, &parent, 8);
1665 	SA_ADD_BULK_ATTR(bulk, idx, sa_attr_table[ZPL_SIZE], NULL,
1666 	    &fsize, 8);
1667 	SA_ADD_BULK_ATTR(bulk, idx, sa_attr_table[ZPL_ATIME], NULL,
1668 	    acctm, 16);
1669 	SA_ADD_BULK_ATTR(bulk, idx, sa_attr_table[ZPL_MTIME], NULL,
1670 	    modtm, 16);
1671 	SA_ADD_BULK_ATTR(bulk, idx, sa_attr_table[ZPL_CRTIME], NULL,
1672 	    crtm, 16);
1673 	SA_ADD_BULK_ATTR(bulk, idx, sa_attr_table[ZPL_CTIME], NULL,
1674 	    chgtm, 16);
1675 	SA_ADD_BULK_ATTR(bulk, idx, sa_attr_table[ZPL_FLAGS], NULL,
1676 	    &pflags, 8);
1677 
1678 	if (sa_bulk_lookup(hdl, bulk, idx)) {
1679 		(void) sa_handle_destroy(hdl);
1680 		return;
1681 	}
1682 
1683 	error = zfs_obj_to_path(os, object, path, sizeof (path));
1684 	if (error != 0) {
1685 		(void) snprintf(path, sizeof (path), "\?\?\?<object#%llu>",
1686 		    (u_longlong_t)object);
1687 	}
1688 	if (dump_opt['d'] < 3) {
1689 		(void) printf("\t%s\n", path);
1690 		(void) sa_handle_destroy(hdl);
1691 		return;
1692 	}
1693 
1694 	z_crtime = (time_t)crtm[0];
1695 	z_atime = (time_t)acctm[0];
1696 	z_mtime = (time_t)modtm[0];
1697 	z_ctime = (time_t)chgtm[0];
1698 
1699 	(void) printf("\tpath	%s\n", path);
1700 	dump_uidgid(os, uid, gid);
1701 	(void) printf("\tatime	%s", ctime(&z_atime));
1702 	(void) printf("\tmtime	%s", ctime(&z_mtime));
1703 	(void) printf("\tctime	%s", ctime(&z_ctime));
1704 	(void) printf("\tcrtime	%s", ctime(&z_crtime));
1705 	(void) printf("\tgen	%llu\n", (u_longlong_t)gen);
1706 	(void) printf("\tmode	%llo\n", (u_longlong_t)mode);
1707 	(void) printf("\tsize	%llu\n", (u_longlong_t)fsize);
1708 	(void) printf("\tparent	%llu\n", (u_longlong_t)parent);
1709 	(void) printf("\tlinks	%llu\n", (u_longlong_t)links);
1710 	(void) printf("\tpflags	%llx\n", (u_longlong_t)pflags);
1711 	if (sa_lookup(hdl, sa_attr_table[ZPL_XATTR], &xattr,
1712 	    sizeof (uint64_t)) == 0)
1713 		(void) printf("\txattr	%llu\n", (u_longlong_t)xattr);
1714 	if (sa_lookup(hdl, sa_attr_table[ZPL_RDEV], &rdev,
1715 	    sizeof (uint64_t)) == 0)
1716 		(void) printf("\trdev	0x%016llx\n", (u_longlong_t)rdev);
1717 	sa_handle_destroy(hdl);
1718 }
1719 
1720 /*ARGSUSED*/
1721 static void
1722 dump_acl(objset_t *os, uint64_t object, void *data, size_t size)
1723 {
1724 }
1725 
1726 /*ARGSUSED*/
1727 static void
1728 dump_dmu_objset(objset_t *os, uint64_t object, void *data, size_t size)
1729 {
1730 }
1731 
1732 static object_viewer_t *object_viewer[DMU_OT_NUMTYPES + 1] = {
1733 	dump_none,		/* unallocated			*/
1734 	dump_zap,		/* object directory		*/
1735 	dump_uint64,		/* object array			*/
1736 	dump_none,		/* packed nvlist		*/
1737 	dump_packed_nvlist,	/* packed nvlist size		*/
1738 	dump_none,		/* bpobj			*/
1739 	dump_bpobj,		/* bpobj header			*/
1740 	dump_none,		/* SPA space map header		*/
1741 	dump_none,		/* SPA space map		*/
1742 	dump_none,		/* ZIL intent log		*/
1743 	dump_dnode,		/* DMU dnode			*/
1744 	dump_dmu_objset,	/* DMU objset			*/
1745 	dump_dsl_dir,		/* DSL directory		*/
1746 	dump_zap,		/* DSL directory child map	*/
1747 	dump_zap,		/* DSL dataset snap map		*/
1748 	dump_zap,		/* DSL props			*/
1749 	dump_dsl_dataset,	/* DSL dataset			*/
1750 	dump_znode,		/* ZFS znode			*/
1751 	dump_acl,		/* ZFS V0 ACL			*/
1752 	dump_uint8,		/* ZFS plain file		*/
1753 	dump_zpldir,		/* ZFS directory		*/
1754 	dump_zap,		/* ZFS master node		*/
1755 	dump_zap,		/* ZFS delete queue		*/
1756 	dump_uint8,		/* zvol object			*/
1757 	dump_zap,		/* zvol prop			*/
1758 	dump_uint8,		/* other uint8[]		*/
1759 	dump_uint64,		/* other uint64[]		*/
1760 	dump_zap,		/* other ZAP			*/
1761 	dump_zap,		/* persistent error log		*/
1762 	dump_uint8,		/* SPA history			*/
1763 	dump_history_offsets,	/* SPA history offsets		*/
1764 	dump_zap,		/* Pool properties		*/
1765 	dump_zap,		/* DSL permissions		*/
1766 	dump_acl,		/* ZFS ACL			*/
1767 	dump_uint8,		/* ZFS SYSACL			*/
1768 	dump_none,		/* FUID nvlist			*/
1769 	dump_packed_nvlist,	/* FUID nvlist size		*/
1770 	dump_zap,		/* DSL dataset next clones	*/
1771 	dump_zap,		/* DSL scrub queue		*/
1772 	dump_zap,		/* ZFS user/group used		*/
1773 	dump_zap,		/* ZFS user/group quota		*/
1774 	dump_zap,		/* snapshot refcount tags	*/
1775 	dump_ddt_zap,		/* DDT ZAP object		*/
1776 	dump_zap,		/* DDT statistics		*/
1777 	dump_znode,		/* SA object			*/
1778 	dump_zap,		/* SA Master Node		*/
1779 	dump_sa_attrs,		/* SA attribute registration	*/
1780 	dump_sa_layouts,	/* SA attribute layouts		*/
1781 	dump_zap,		/* DSL scrub translations	*/
1782 	dump_none,		/* fake dedup BP		*/
1783 	dump_zap,		/* deadlist			*/
1784 	dump_none,		/* deadlist hdr			*/
1785 	dump_zap,		/* dsl clones			*/
1786 	dump_bpobj_subobjs,	/* bpobj subobjs		*/
1787 	dump_unknown,		/* Unknown type, must be last	*/
1788 };
1789 
1790 static void
1791 dump_object(objset_t *os, uint64_t object, int verbosity, int *print_header)
1792 {
1793 	dmu_buf_t *db = NULL;
1794 	dmu_object_info_t doi;
1795 	dnode_t *dn;
1796 	void *bonus = NULL;
1797 	size_t bsize = 0;
1798 	char iblk[32], dblk[32], lsize[32], asize[32], fill[32];
1799 	char bonus_size[32];
1800 	char aux[50];
1801 	int error;
1802 
1803 	if (*print_header) {
1804 		(void) printf("\n%10s  %3s  %5s  %5s  %5s  %5s  %6s  %s\n",
1805 		    "Object", "lvl", "iblk", "dblk", "dsize", "lsize",
1806 		    "%full", "type");
1807 		*print_header = 0;
1808 	}
1809 
1810 	if (object == 0) {
1811 		dn = DMU_META_DNODE(os);
1812 	} else {
1813 		error = dmu_bonus_hold(os, object, FTAG, &db);
1814 		if (error)
1815 			fatal("dmu_bonus_hold(%llu) failed, errno %u",
1816 			    object, error);
1817 		bonus = db->db_data;
1818 		bsize = db->db_size;
1819 		dn = DB_DNODE((dmu_buf_impl_t *)db);
1820 	}
1821 	dmu_object_info_from_dnode(dn, &doi);
1822 
1823 	zdb_nicenum(doi.doi_metadata_block_size, iblk);
1824 	zdb_nicenum(doi.doi_data_block_size, dblk);
1825 	zdb_nicenum(doi.doi_max_offset, lsize);
1826 	zdb_nicenum(doi.doi_physical_blocks_512 << 9, asize);
1827 	zdb_nicenum(doi.doi_bonus_size, bonus_size);
1828 	(void) sprintf(fill, "%6.2f", 100.0 * doi.doi_fill_count *
1829 	    doi.doi_data_block_size / (object == 0 ? DNODES_PER_BLOCK : 1) /
1830 	    doi.doi_max_offset);
1831 
1832 	aux[0] = '\0';
1833 
1834 	if (doi.doi_checksum != ZIO_CHECKSUM_INHERIT || verbosity >= 6) {
1835 		(void) snprintf(aux + strlen(aux), sizeof (aux), " (K=%s)",
1836 		    ZDB_CHECKSUM_NAME(doi.doi_checksum));
1837 	}
1838 
1839 	if (doi.doi_compress != ZIO_COMPRESS_INHERIT || verbosity >= 6) {
1840 		(void) snprintf(aux + strlen(aux), sizeof (aux), " (Z=%s)",
1841 		    ZDB_COMPRESS_NAME(doi.doi_compress));
1842 	}
1843 
1844 	(void) printf("%10lld  %3u  %5s  %5s  %5s  %5s  %6s  %s%s\n",
1845 	    (u_longlong_t)object, doi.doi_indirection, iblk, dblk,
1846 	    asize, lsize, fill, ZDB_OT_NAME(doi.doi_type), aux);
1847 
1848 	if (doi.doi_bonus_type != DMU_OT_NONE && verbosity > 3) {
1849 		(void) printf("%10s  %3s  %5s  %5s  %5s  %5s  %6s  %s\n",
1850 		    "", "", "", "", "", bonus_size, "bonus",
1851 		    ZDB_OT_NAME(doi.doi_bonus_type));
1852 	}
1853 
1854 	if (verbosity >= 4) {
1855 		(void) printf("\tdnode flags: %s%s%s\n",
1856 		    (dn->dn_phys->dn_flags & DNODE_FLAG_USED_BYTES) ?
1857 		    "USED_BYTES " : "",
1858 		    (dn->dn_phys->dn_flags & DNODE_FLAG_USERUSED_ACCOUNTED) ?
1859 		    "USERUSED_ACCOUNTED " : "",
1860 		    (dn->dn_phys->dn_flags & DNODE_FLAG_SPILL_BLKPTR) ?
1861 		    "SPILL_BLKPTR" : "");
1862 		(void) printf("\tdnode maxblkid: %llu\n",
1863 		    (longlong_t)dn->dn_phys->dn_maxblkid);
1864 
1865 		object_viewer[ZDB_OT_TYPE(doi.doi_bonus_type)](os, object,
1866 		    bonus, bsize);
1867 		object_viewer[ZDB_OT_TYPE(doi.doi_type)](os, object, NULL, 0);
1868 		*print_header = 1;
1869 	}
1870 
1871 	if (verbosity >= 5)
1872 		dump_indirect(dn);
1873 
1874 	if (verbosity >= 5) {
1875 		/*
1876 		 * Report the list of segments that comprise the object.
1877 		 */
1878 		uint64_t start = 0;
1879 		uint64_t end;
1880 		uint64_t blkfill = 1;
1881 		int minlvl = 1;
1882 
1883 		if (dn->dn_type == DMU_OT_DNODE) {
1884 			minlvl = 0;
1885 			blkfill = DNODES_PER_BLOCK;
1886 		}
1887 
1888 		for (;;) {
1889 			char segsize[32];
1890 			error = dnode_next_offset(dn,
1891 			    0, &start, minlvl, blkfill, 0);
1892 			if (error)
1893 				break;
1894 			end = start;
1895 			error = dnode_next_offset(dn,
1896 			    DNODE_FIND_HOLE, &end, minlvl, blkfill, 0);
1897 			zdb_nicenum(end - start, segsize);
1898 			(void) printf("\t\tsegment [%016llx, %016llx)"
1899 			    " size %5s\n", (u_longlong_t)start,
1900 			    (u_longlong_t)end, segsize);
1901 			if (error)
1902 				break;
1903 			start = end;
1904 		}
1905 	}
1906 
1907 	if (db != NULL)
1908 		dmu_buf_rele(db, FTAG);
1909 }
1910 
1911 static char *objset_types[DMU_OST_NUMTYPES] = {
1912 	"NONE", "META", "ZPL", "ZVOL", "OTHER", "ANY" };
1913 
1914 static void
1915 dump_dir(objset_t *os)
1916 {
1917 	dmu_objset_stats_t dds;
1918 	uint64_t object, object_count;
1919 	uint64_t refdbytes, usedobjs, scratch;
1920 	char numbuf[32];
1921 	char blkbuf[BP_SPRINTF_LEN + 20];
1922 	char osname[MAXNAMELEN];
1923 	char *type = "UNKNOWN";
1924 	int verbosity = dump_opt['d'];
1925 	int print_header = 1;
1926 	int i, error;
1927 
1928 	dsl_pool_config_enter(dmu_objset_pool(os), FTAG);
1929 	dmu_objset_fast_stat(os, &dds);
1930 	dsl_pool_config_exit(dmu_objset_pool(os), FTAG);
1931 
1932 	if (dds.dds_type < DMU_OST_NUMTYPES)
1933 		type = objset_types[dds.dds_type];
1934 
1935 	if (dds.dds_type == DMU_OST_META) {
1936 		dds.dds_creation_txg = TXG_INITIAL;
1937 		usedobjs = BP_GET_FILL(os->os_rootbp);
1938 		refdbytes = dsl_dir_phys(os->os_spa->spa_dsl_pool->dp_mos_dir)->
1939 		    dd_used_bytes;
1940 	} else {
1941 		dmu_objset_space(os, &refdbytes, &scratch, &usedobjs, &scratch);
1942 	}
1943 
1944 	ASSERT3U(usedobjs, ==, BP_GET_FILL(os->os_rootbp));
1945 
1946 	zdb_nicenum(refdbytes, numbuf);
1947 
1948 	if (verbosity >= 4) {
1949 		(void) snprintf(blkbuf, sizeof (blkbuf), ", rootbp ");
1950 		(void) snprintf_blkptr(blkbuf + strlen(blkbuf),
1951 		    sizeof (blkbuf) - strlen(blkbuf), os->os_rootbp);
1952 	} else {
1953 		blkbuf[0] = '\0';
1954 	}
1955 
1956 	dmu_objset_name(os, osname);
1957 
1958 	(void) printf("Dataset %s [%s], ID %llu, cr_txg %llu, "
1959 	    "%s, %llu objects%s\n",
1960 	    osname, type, (u_longlong_t)dmu_objset_id(os),
1961 	    (u_longlong_t)dds.dds_creation_txg,
1962 	    numbuf, (u_longlong_t)usedobjs, blkbuf);
1963 
1964 	if (zopt_objects != 0) {
1965 		for (i = 0; i < zopt_objects; i++)
1966 			dump_object(os, zopt_object[i], verbosity,
1967 			    &print_header);
1968 		(void) printf("\n");
1969 		return;
1970 	}
1971 
1972 	if (dump_opt['i'] != 0 || verbosity >= 2)
1973 		dump_intent_log(dmu_objset_zil(os));
1974 
1975 	if (dmu_objset_ds(os) != NULL)
1976 		dump_deadlist(&dmu_objset_ds(os)->ds_deadlist);
1977 
1978 	if (verbosity < 2)
1979 		return;
1980 
1981 	if (BP_IS_HOLE(os->os_rootbp))
1982 		return;
1983 
1984 	dump_object(os, 0, verbosity, &print_header);
1985 	object_count = 0;
1986 	if (DMU_USERUSED_DNODE(os) != NULL &&
1987 	    DMU_USERUSED_DNODE(os)->dn_type != 0) {
1988 		dump_object(os, DMU_USERUSED_OBJECT, verbosity, &print_header);
1989 		dump_object(os, DMU_GROUPUSED_OBJECT, verbosity, &print_header);
1990 	}
1991 
1992 	object = 0;
1993 	while ((error = dmu_object_next(os, &object, B_FALSE, 0)) == 0) {
1994 		dump_object(os, object, verbosity, &print_header);
1995 		object_count++;
1996 	}
1997 
1998 	ASSERT3U(object_count, ==, usedobjs);
1999 
2000 	(void) printf("\n");
2001 
2002 	if (error != ESRCH) {
2003 		(void) fprintf(stderr, "dmu_object_next() = %d\n", error);
2004 		abort();
2005 	}
2006 }
2007 
2008 static void
2009 dump_uberblock(uberblock_t *ub, const char *header, const char *footer)
2010 {
2011 	time_t timestamp = ub->ub_timestamp;
2012 
2013 	(void) printf(header ? header : "");
2014 	(void) printf("\tmagic = %016llx\n", (u_longlong_t)ub->ub_magic);
2015 	(void) printf("\tversion = %llu\n", (u_longlong_t)ub->ub_version);
2016 	(void) printf("\ttxg = %llu\n", (u_longlong_t)ub->ub_txg);
2017 	(void) printf("\tguid_sum = %llu\n", (u_longlong_t)ub->ub_guid_sum);
2018 	(void) printf("\ttimestamp = %llu UTC = %s",
2019 	    (u_longlong_t)ub->ub_timestamp, asctime(localtime(&timestamp)));
2020 	if (dump_opt['u'] >= 3) {
2021 		char blkbuf[BP_SPRINTF_LEN];
2022 		snprintf_blkptr(blkbuf, sizeof (blkbuf), &ub->ub_rootbp);
2023 		(void) printf("\trootbp = %s\n", blkbuf);
2024 	}
2025 	(void) printf(footer ? footer : "");
2026 }
2027 
2028 static void
2029 dump_config(spa_t *spa)
2030 {
2031 	dmu_buf_t *db;
2032 	size_t nvsize = 0;
2033 	int error = 0;
2034 
2035 
2036 	error = dmu_bonus_hold(spa->spa_meta_objset,
2037 	    spa->spa_config_object, FTAG, &db);
2038 
2039 	if (error == 0) {
2040 		nvsize = *(uint64_t *)db->db_data;
2041 		dmu_buf_rele(db, FTAG);
2042 
2043 		(void) printf("\nMOS Configuration:\n");
2044 		dump_packed_nvlist(spa->spa_meta_objset,
2045 		    spa->spa_config_object, (void *)&nvsize, 1);
2046 	} else {
2047 		(void) fprintf(stderr, "dmu_bonus_hold(%llu) failed, errno %d",
2048 		    (u_longlong_t)spa->spa_config_object, error);
2049 	}
2050 }
2051 
2052 static void
2053 dump_cachefile(const char *cachefile)
2054 {
2055 	int fd;
2056 	struct stat64 statbuf;
2057 	char *buf;
2058 	nvlist_t *config;
2059 
2060 	if ((fd = open64(cachefile, O_RDONLY)) < 0) {
2061 		(void) printf("cannot open '%s': %s\n", cachefile,
2062 		    strerror(errno));
2063 		exit(1);
2064 	}
2065 
2066 	if (fstat64(fd, &statbuf) != 0) {
2067 		(void) printf("failed to stat '%s': %s\n", cachefile,
2068 		    strerror(errno));
2069 		exit(1);
2070 	}
2071 
2072 	if ((buf = malloc(statbuf.st_size)) == NULL) {
2073 		(void) fprintf(stderr, "failed to allocate %llu bytes\n",
2074 		    (u_longlong_t)statbuf.st_size);
2075 		exit(1);
2076 	}
2077 
2078 	if (read(fd, buf, statbuf.st_size) != statbuf.st_size) {
2079 		(void) fprintf(stderr, "failed to read %llu bytes\n",
2080 		    (u_longlong_t)statbuf.st_size);
2081 		exit(1);
2082 	}
2083 
2084 	(void) close(fd);
2085 
2086 	if (nvlist_unpack(buf, statbuf.st_size, &config, 0) != 0) {
2087 		(void) fprintf(stderr, "failed to unpack nvlist\n");
2088 		exit(1);
2089 	}
2090 
2091 	free(buf);
2092 
2093 	dump_nvlist(config, 0);
2094 
2095 	nvlist_free(config);
2096 }
2097 
2098 #define	ZDB_MAX_UB_HEADER_SIZE 32
2099 
2100 static void
2101 dump_label_uberblocks(vdev_label_t *lbl, uint64_t ashift)
2102 {
2103 	vdev_t vd;
2104 	vdev_t *vdp = &vd;
2105 	char header[ZDB_MAX_UB_HEADER_SIZE];
2106 
2107 	vd.vdev_ashift = ashift;
2108 	vdp->vdev_top = vdp;
2109 
2110 	for (int i = 0; i < VDEV_UBERBLOCK_COUNT(vdp); i++) {
2111 		uint64_t uoff = VDEV_UBERBLOCK_OFFSET(vdp, i);
2112 		uberblock_t *ub = (void *)((char *)lbl + uoff);
2113 
2114 		if (uberblock_verify(ub))
2115 			continue;
2116 		(void) snprintf(header, ZDB_MAX_UB_HEADER_SIZE,
2117 		    "Uberblock[%d]\n", i);
2118 		dump_uberblock(ub, header, "");
2119 	}
2120 }
2121 
2122 static void
2123 dump_label(const char *dev)
2124 {
2125 	int fd;
2126 	vdev_label_t label;
2127 	char *path, *buf = label.vl_vdev_phys.vp_nvlist;
2128 	size_t buflen = sizeof (label.vl_vdev_phys.vp_nvlist);
2129 	struct stat64 statbuf;
2130 	uint64_t psize, ashift;
2131 	int len = strlen(dev) + 1;
2132 
2133 	if (strncmp(dev, "/dev/dsk/", 9) == 0) {
2134 		len++;
2135 		path = malloc(len);
2136 		(void) snprintf(path, len, "%s%s", "/dev/rdsk/", dev + 9);
2137 	} else {
2138 		path = strdup(dev);
2139 	}
2140 
2141 	if ((fd = open64(path, O_RDONLY)) < 0) {
2142 		(void) printf("cannot open '%s': %s\n", path, strerror(errno));
2143 		free(path);
2144 		exit(1);
2145 	}
2146 
2147 	if (fstat64(fd, &statbuf) != 0) {
2148 		(void) printf("failed to stat '%s': %s\n", path,
2149 		    strerror(errno));
2150 		free(path);
2151 		(void) close(fd);
2152 		exit(1);
2153 	}
2154 
2155 	if (S_ISBLK(statbuf.st_mode)) {
2156 		(void) printf("cannot use '%s': character device required\n",
2157 		    path);
2158 		free(path);
2159 		(void) close(fd);
2160 		exit(1);
2161 	}
2162 
2163 	psize = statbuf.st_size;
2164 	psize = P2ALIGN(psize, (uint64_t)sizeof (vdev_label_t));
2165 
2166 	for (int l = 0; l < VDEV_LABELS; l++) {
2167 		nvlist_t *config = NULL;
2168 
2169 		(void) printf("--------------------------------------------\n");
2170 		(void) printf("LABEL %d\n", l);
2171 		(void) printf("--------------------------------------------\n");
2172 
2173 		if (pread64(fd, &label, sizeof (label),
2174 		    vdev_label_offset(psize, l, 0)) != sizeof (label)) {
2175 			(void) printf("failed to read label %d\n", l);
2176 			continue;
2177 		}
2178 
2179 		if (nvlist_unpack(buf, buflen, &config, 0) != 0) {
2180 			(void) printf("failed to unpack label %d\n", l);
2181 			ashift = SPA_MINBLOCKSHIFT;
2182 		} else {
2183 			nvlist_t *vdev_tree = NULL;
2184 
2185 			dump_nvlist(config, 4);
2186 			if ((nvlist_lookup_nvlist(config,
2187 			    ZPOOL_CONFIG_VDEV_TREE, &vdev_tree) != 0) ||
2188 			    (nvlist_lookup_uint64(vdev_tree,
2189 			    ZPOOL_CONFIG_ASHIFT, &ashift) != 0))
2190 				ashift = SPA_MINBLOCKSHIFT;
2191 			nvlist_free(config);
2192 		}
2193 		if (dump_opt['u'])
2194 			dump_label_uberblocks(&label, ashift);
2195 	}
2196 
2197 	free(path);
2198 	(void) close(fd);
2199 }
2200 
2201 static uint64_t num_large_blocks;
2202 
2203 /*ARGSUSED*/
2204 static int
2205 dump_one_dir(const char *dsname, void *arg)
2206 {
2207 	int error;
2208 	objset_t *os;
2209 
2210 	error = dmu_objset_own(dsname, DMU_OST_ANY, B_TRUE, FTAG, &os);
2211 	if (error) {
2212 		(void) printf("Could not open %s, error %d\n", dsname, error);
2213 		return (0);
2214 	}
2215 	if (dmu_objset_ds(os)->ds_large_blocks)
2216 		num_large_blocks++;
2217 	dump_dir(os);
2218 	dmu_objset_disown(os, FTAG);
2219 	fuid_table_destroy();
2220 	sa_loaded = B_FALSE;
2221 	return (0);
2222 }
2223 
2224 /*
2225  * Block statistics.
2226  */
2227 #define	PSIZE_HISTO_SIZE (SPA_OLD_MAXBLOCKSIZE / SPA_MINBLOCKSIZE + 2)
2228 typedef struct zdb_blkstats {
2229 	uint64_t zb_asize;
2230 	uint64_t zb_lsize;
2231 	uint64_t zb_psize;
2232 	uint64_t zb_count;
2233 	uint64_t zb_gangs;
2234 	uint64_t zb_ditto_samevdev;
2235 	uint64_t zb_psize_histogram[PSIZE_HISTO_SIZE];
2236 } zdb_blkstats_t;
2237 
2238 /*
2239  * Extended object types to report deferred frees and dedup auto-ditto blocks.
2240  */
2241 #define	ZDB_OT_DEFERRED	(DMU_OT_NUMTYPES + 0)
2242 #define	ZDB_OT_DITTO	(DMU_OT_NUMTYPES + 1)
2243 #define	ZDB_OT_OTHER	(DMU_OT_NUMTYPES + 2)
2244 #define	ZDB_OT_TOTAL	(DMU_OT_NUMTYPES + 3)
2245 
2246 static char *zdb_ot_extname[] = {
2247 	"deferred free",
2248 	"dedup ditto",
2249 	"other",
2250 	"Total",
2251 };
2252 
2253 #define	ZB_TOTAL	DN_MAX_LEVELS
2254 
2255 typedef struct zdb_cb {
2256 	zdb_blkstats_t	zcb_type[ZB_TOTAL + 1][ZDB_OT_TOTAL + 1];
2257 	uint64_t	zcb_dedup_asize;
2258 	uint64_t	zcb_dedup_blocks;
2259 	uint64_t	zcb_embedded_blocks[NUM_BP_EMBEDDED_TYPES];
2260 	uint64_t	zcb_embedded_histogram[NUM_BP_EMBEDDED_TYPES]
2261 	    [BPE_PAYLOAD_SIZE];
2262 	uint64_t	zcb_start;
2263 	uint64_t	zcb_lastprint;
2264 	uint64_t	zcb_totalasize;
2265 	uint64_t	zcb_errors[256];
2266 	int		zcb_readfails;
2267 	int		zcb_haderrors;
2268 	spa_t		*zcb_spa;
2269 } zdb_cb_t;
2270 
2271 static void
2272 zdb_count_block(zdb_cb_t *zcb, zilog_t *zilog, const blkptr_t *bp,
2273     dmu_object_type_t type)
2274 {
2275 	uint64_t refcnt = 0;
2276 
2277 	ASSERT(type < ZDB_OT_TOTAL);
2278 
2279 	if (zilog && zil_bp_tree_add(zilog, bp) != 0)
2280 		return;
2281 
2282 	for (int i = 0; i < 4; i++) {
2283 		int l = (i < 2) ? BP_GET_LEVEL(bp) : ZB_TOTAL;
2284 		int t = (i & 1) ? type : ZDB_OT_TOTAL;
2285 		int equal;
2286 		zdb_blkstats_t *zb = &zcb->zcb_type[l][t];
2287 
2288 		zb->zb_asize += BP_GET_ASIZE(bp);
2289 		zb->zb_lsize += BP_GET_LSIZE(bp);
2290 		zb->zb_psize += BP_GET_PSIZE(bp);
2291 		zb->zb_count++;
2292 
2293 		/*
2294 		 * The histogram is only big enough to record blocks up to
2295 		 * SPA_OLD_MAXBLOCKSIZE; larger blocks go into the last,
2296 		 * "other", bucket.
2297 		 */
2298 		int idx = BP_GET_PSIZE(bp) >> SPA_MINBLOCKSHIFT;
2299 		idx = MIN(idx, SPA_OLD_MAXBLOCKSIZE / SPA_MINBLOCKSIZE + 1);
2300 		zb->zb_psize_histogram[idx]++;
2301 
2302 		zb->zb_gangs += BP_COUNT_GANG(bp);
2303 
2304 		switch (BP_GET_NDVAS(bp)) {
2305 		case 2:
2306 			if (DVA_GET_VDEV(&bp->blk_dva[0]) ==
2307 			    DVA_GET_VDEV(&bp->blk_dva[1]))
2308 				zb->zb_ditto_samevdev++;
2309 			break;
2310 		case 3:
2311 			equal = (DVA_GET_VDEV(&bp->blk_dva[0]) ==
2312 			    DVA_GET_VDEV(&bp->blk_dva[1])) +
2313 			    (DVA_GET_VDEV(&bp->blk_dva[0]) ==
2314 			    DVA_GET_VDEV(&bp->blk_dva[2])) +
2315 			    (DVA_GET_VDEV(&bp->blk_dva[1]) ==
2316 			    DVA_GET_VDEV(&bp->blk_dva[2]));
2317 			if (equal != 0)
2318 				zb->zb_ditto_samevdev++;
2319 			break;
2320 		}
2321 
2322 	}
2323 
2324 	if (BP_IS_EMBEDDED(bp)) {
2325 		zcb->zcb_embedded_blocks[BPE_GET_ETYPE(bp)]++;
2326 		zcb->zcb_embedded_histogram[BPE_GET_ETYPE(bp)]
2327 		    [BPE_GET_PSIZE(bp)]++;
2328 		return;
2329 	}
2330 
2331 	if (dump_opt['L'])
2332 		return;
2333 
2334 	if (BP_GET_DEDUP(bp)) {
2335 		ddt_t *ddt;
2336 		ddt_entry_t *dde;
2337 
2338 		ddt = ddt_select(zcb->zcb_spa, bp);
2339 		ddt_enter(ddt);
2340 		dde = ddt_lookup(ddt, bp, B_FALSE);
2341 
2342 		if (dde == NULL) {
2343 			refcnt = 0;
2344 		} else {
2345 			ddt_phys_t *ddp = ddt_phys_select(dde, bp);
2346 			ddt_phys_decref(ddp);
2347 			refcnt = ddp->ddp_refcnt;
2348 			if (ddt_phys_total_refcnt(dde) == 0)
2349 				ddt_remove(ddt, dde);
2350 		}
2351 		ddt_exit(ddt);
2352 	}
2353 
2354 	VERIFY3U(zio_wait(zio_claim(NULL, zcb->zcb_spa,
2355 	    refcnt ? 0 : spa_first_txg(zcb->zcb_spa),
2356 	    bp, NULL, NULL, ZIO_FLAG_CANFAIL)), ==, 0);
2357 }
2358 
2359 static void
2360 zdb_blkptr_done(zio_t *zio)
2361 {
2362 	spa_t *spa = zio->io_spa;
2363 	blkptr_t *bp = zio->io_bp;
2364 	int ioerr = zio->io_error;
2365 	zdb_cb_t *zcb = zio->io_private;
2366 	zbookmark_phys_t *zb = &zio->io_bookmark;
2367 
2368 	zio_data_buf_free(zio->io_data, zio->io_size);
2369 
2370 	mutex_enter(&spa->spa_scrub_lock);
2371 	spa->spa_scrub_inflight--;
2372 	cv_broadcast(&spa->spa_scrub_io_cv);
2373 
2374 	if (ioerr && !(zio->io_flags & ZIO_FLAG_SPECULATIVE)) {
2375 		char blkbuf[BP_SPRINTF_LEN];
2376 
2377 		zcb->zcb_haderrors = 1;
2378 		zcb->zcb_errors[ioerr]++;
2379 
2380 		if (dump_opt['b'] >= 2)
2381 			snprintf_blkptr(blkbuf, sizeof (blkbuf), bp);
2382 		else
2383 			blkbuf[0] = '\0';
2384 
2385 		(void) printf("zdb_blkptr_cb: "
2386 		    "Got error %d reading "
2387 		    "<%llu, %llu, %lld, %llx> %s -- skipping\n",
2388 		    ioerr,
2389 		    (u_longlong_t)zb->zb_objset,
2390 		    (u_longlong_t)zb->zb_object,
2391 		    (u_longlong_t)zb->zb_level,
2392 		    (u_longlong_t)zb->zb_blkid,
2393 		    blkbuf);
2394 	}
2395 	mutex_exit(&spa->spa_scrub_lock);
2396 }
2397 
2398 static int
2399 zdb_blkptr_cb(spa_t *spa, zilog_t *zilog, const blkptr_t *bp,
2400     const zbookmark_phys_t *zb, const dnode_phys_t *dnp, void *arg)
2401 {
2402 	zdb_cb_t *zcb = arg;
2403 	dmu_object_type_t type;
2404 	boolean_t is_metadata;
2405 
2406 	if (dump_opt['b'] >= 5 && bp->blk_birth > 0) {
2407 		char blkbuf[BP_SPRINTF_LEN];
2408 		snprintf_blkptr(blkbuf, sizeof (blkbuf), bp);
2409 		(void) printf("objset %llu object %llu "
2410 		    "level %lld offset 0x%llx %s\n",
2411 		    (u_longlong_t)zb->zb_objset,
2412 		    (u_longlong_t)zb->zb_object,
2413 		    (longlong_t)zb->zb_level,
2414 		    (u_longlong_t)blkid2offset(dnp, bp, zb),
2415 		    blkbuf);
2416 	}
2417 
2418 	if (BP_IS_HOLE(bp))
2419 		return (0);
2420 
2421 	type = BP_GET_TYPE(bp);
2422 
2423 	zdb_count_block(zcb, zilog, bp,
2424 	    (type & DMU_OT_NEWTYPE) ? ZDB_OT_OTHER : type);
2425 
2426 	is_metadata = (BP_GET_LEVEL(bp) != 0 || DMU_OT_IS_METADATA(type));
2427 
2428 	if (!BP_IS_EMBEDDED(bp) &&
2429 	    (dump_opt['c'] > 1 || (dump_opt['c'] && is_metadata))) {
2430 		size_t size = BP_GET_PSIZE(bp);
2431 		void *data = zio_data_buf_alloc(size);
2432 		int flags = ZIO_FLAG_CANFAIL | ZIO_FLAG_SCRUB | ZIO_FLAG_RAW;
2433 
2434 		/* If it's an intent log block, failure is expected. */
2435 		if (zb->zb_level == ZB_ZIL_LEVEL)
2436 			flags |= ZIO_FLAG_SPECULATIVE;
2437 
2438 		mutex_enter(&spa->spa_scrub_lock);
2439 		while (spa->spa_scrub_inflight > max_inflight)
2440 			cv_wait(&spa->spa_scrub_io_cv, &spa->spa_scrub_lock);
2441 		spa->spa_scrub_inflight++;
2442 		mutex_exit(&spa->spa_scrub_lock);
2443 
2444 		zio_nowait(zio_read(NULL, spa, bp, data, size,
2445 		    zdb_blkptr_done, zcb, ZIO_PRIORITY_ASYNC_READ, flags, zb));
2446 	}
2447 
2448 	zcb->zcb_readfails = 0;
2449 
2450 	/* only call gethrtime() every 100 blocks */
2451 	static int iters;
2452 	if (++iters > 100)
2453 		iters = 0;
2454 	else
2455 		return (0);
2456 
2457 	if (dump_opt['b'] < 5 && gethrtime() > zcb->zcb_lastprint + NANOSEC) {
2458 		uint64_t now = gethrtime();
2459 		char buf[10];
2460 		uint64_t bytes = zcb->zcb_type[ZB_TOTAL][ZDB_OT_TOTAL].zb_asize;
2461 		int kb_per_sec =
2462 		    1 + bytes / (1 + ((now - zcb->zcb_start) / 1000 / 1000));
2463 		int sec_remaining =
2464 		    (zcb->zcb_totalasize - bytes) / 1024 / kb_per_sec;
2465 
2466 		zfs_nicenum(bytes, buf, sizeof (buf));
2467 		(void) fprintf(stderr,
2468 		    "\r%5s completed (%4dMB/s) "
2469 		    "estimated time remaining: %uhr %02umin %02usec        ",
2470 		    buf, kb_per_sec / 1024,
2471 		    sec_remaining / 60 / 60,
2472 		    sec_remaining / 60 % 60,
2473 		    sec_remaining % 60);
2474 
2475 		zcb->zcb_lastprint = now;
2476 	}
2477 
2478 	return (0);
2479 }
2480 
2481 static void
2482 zdb_leak(void *arg, uint64_t start, uint64_t size)
2483 {
2484 	vdev_t *vd = arg;
2485 
2486 	(void) printf("leaked space: vdev %llu, offset 0x%llx, size %llu\n",
2487 	    (u_longlong_t)vd->vdev_id, (u_longlong_t)start, (u_longlong_t)size);
2488 }
2489 
2490 static metaslab_ops_t zdb_metaslab_ops = {
2491 	NULL	/* alloc */
2492 };
2493 
2494 static void
2495 zdb_ddt_leak_init(spa_t *spa, zdb_cb_t *zcb)
2496 {
2497 	ddt_bookmark_t ddb = { 0 };
2498 	ddt_entry_t dde;
2499 	int error;
2500 
2501 	while ((error = ddt_walk(spa, &ddb, &dde)) == 0) {
2502 		blkptr_t blk;
2503 		ddt_phys_t *ddp = dde.dde_phys;
2504 
2505 		if (ddb.ddb_class == DDT_CLASS_UNIQUE)
2506 			return;
2507 
2508 		ASSERT(ddt_phys_total_refcnt(&dde) > 1);
2509 
2510 		for (int p = 0; p < DDT_PHYS_TYPES; p++, ddp++) {
2511 			if (ddp->ddp_phys_birth == 0)
2512 				continue;
2513 			ddt_bp_create(ddb.ddb_checksum,
2514 			    &dde.dde_key, ddp, &blk);
2515 			if (p == DDT_PHYS_DITTO) {
2516 				zdb_count_block(zcb, NULL, &blk, ZDB_OT_DITTO);
2517 			} else {
2518 				zcb->zcb_dedup_asize +=
2519 				    BP_GET_ASIZE(&blk) * (ddp->ddp_refcnt - 1);
2520 				zcb->zcb_dedup_blocks++;
2521 			}
2522 		}
2523 		if (!dump_opt['L']) {
2524 			ddt_t *ddt = spa->spa_ddt[ddb.ddb_checksum];
2525 			ddt_enter(ddt);
2526 			VERIFY(ddt_lookup(ddt, &blk, B_TRUE) != NULL);
2527 			ddt_exit(ddt);
2528 		}
2529 	}
2530 
2531 	ASSERT(error == ENOENT);
2532 }
2533 
2534 static void
2535 zdb_leak_init(spa_t *spa, zdb_cb_t *zcb)
2536 {
2537 	zcb->zcb_spa = spa;
2538 
2539 	if (!dump_opt['L']) {
2540 		vdev_t *rvd = spa->spa_root_vdev;
2541 		for (uint64_t c = 0; c < rvd->vdev_children; c++) {
2542 			vdev_t *vd = rvd->vdev_child[c];
2543 			for (uint64_t m = 0; m < vd->vdev_ms_count; m++) {
2544 				metaslab_t *msp = vd->vdev_ms[m];
2545 				mutex_enter(&msp->ms_lock);
2546 				metaslab_unload(msp);
2547 
2548 				/*
2549 				 * For leak detection, we overload the metaslab
2550 				 * ms_tree to contain allocated segments
2551 				 * instead of free segments. As a result,
2552 				 * we can't use the normal metaslab_load/unload
2553 				 * interfaces.
2554 				 */
2555 				if (msp->ms_sm != NULL) {
2556 					(void) fprintf(stderr,
2557 					    "\rloading space map for "
2558 					    "vdev %llu of %llu, "
2559 					    "metaslab %llu of %llu ...",
2560 					    (longlong_t)c,
2561 					    (longlong_t)rvd->vdev_children,
2562 					    (longlong_t)m,
2563 					    (longlong_t)vd->vdev_ms_count);
2564 
2565 					msp->ms_ops = &zdb_metaslab_ops;
2566 
2567 					/*
2568 					 * We don't want to spend the CPU
2569 					 * manipulating the size-ordered
2570 					 * tree, so clear the range_tree
2571 					 * ops.
2572 					 */
2573 					msp->ms_tree->rt_ops = NULL;
2574 					VERIFY0(space_map_load(msp->ms_sm,
2575 					    msp->ms_tree, SM_ALLOC));
2576 					msp->ms_loaded = B_TRUE;
2577 				}
2578 				mutex_exit(&msp->ms_lock);
2579 			}
2580 		}
2581 		(void) fprintf(stderr, "\n");
2582 	}
2583 
2584 	spa_config_enter(spa, SCL_CONFIG, FTAG, RW_READER);
2585 
2586 	zdb_ddt_leak_init(spa, zcb);
2587 
2588 	spa_config_exit(spa, SCL_CONFIG, FTAG);
2589 }
2590 
2591 static void
2592 zdb_leak_fini(spa_t *spa)
2593 {
2594 	if (!dump_opt['L']) {
2595 		vdev_t *rvd = spa->spa_root_vdev;
2596 		for (int c = 0; c < rvd->vdev_children; c++) {
2597 			vdev_t *vd = rvd->vdev_child[c];
2598 			for (int m = 0; m < vd->vdev_ms_count; m++) {
2599 				metaslab_t *msp = vd->vdev_ms[m];
2600 				mutex_enter(&msp->ms_lock);
2601 
2602 				/*
2603 				 * The ms_tree has been overloaded to
2604 				 * contain allocated segments. Now that we
2605 				 * finished traversing all blocks, any
2606 				 * block that remains in the ms_tree
2607 				 * represents an allocated block that we
2608 				 * did not claim during the traversal.
2609 				 * Claimed blocks would have been removed
2610 				 * from the ms_tree.
2611 				 */
2612 				range_tree_vacate(msp->ms_tree, zdb_leak, vd);
2613 				msp->ms_loaded = B_FALSE;
2614 
2615 				mutex_exit(&msp->ms_lock);
2616 			}
2617 		}
2618 	}
2619 }
2620 
2621 /* ARGSUSED */
2622 static int
2623 count_block_cb(void *arg, const blkptr_t *bp, dmu_tx_t *tx)
2624 {
2625 	zdb_cb_t *zcb = arg;
2626 
2627 	if (dump_opt['b'] >= 5) {
2628 		char blkbuf[BP_SPRINTF_LEN];
2629 		snprintf_blkptr(blkbuf, sizeof (blkbuf), bp);
2630 		(void) printf("[%s] %s\n",
2631 		    "deferred free", blkbuf);
2632 	}
2633 	zdb_count_block(zcb, NULL, bp, ZDB_OT_DEFERRED);
2634 	return (0);
2635 }
2636 
2637 static int
2638 dump_block_stats(spa_t *spa)
2639 {
2640 	zdb_cb_t zcb = { 0 };
2641 	zdb_blkstats_t *zb, *tzb;
2642 	uint64_t norm_alloc, norm_space, total_alloc, total_found;
2643 	int flags = TRAVERSE_PRE | TRAVERSE_PREFETCH_METADATA | TRAVERSE_HARD;
2644 	boolean_t leaks = B_FALSE;
2645 
2646 	(void) printf("\nTraversing all blocks %s%s%s%s%s...\n\n",
2647 	    (dump_opt['c'] || !dump_opt['L']) ? "to verify " : "",
2648 	    (dump_opt['c'] == 1) ? "metadata " : "",
2649 	    dump_opt['c'] ? "checksums " : "",
2650 	    (dump_opt['c'] && !dump_opt['L']) ? "and verify " : "",
2651 	    !dump_opt['L'] ? "nothing leaked " : "");
2652 
2653 	/*
2654 	 * Load all space maps as SM_ALLOC maps, then traverse the pool
2655 	 * claiming each block we discover.  If the pool is perfectly
2656 	 * consistent, the space maps will be empty when we're done.
2657 	 * Anything left over is a leak; any block we can't claim (because
2658 	 * it's not part of any space map) is a double allocation,
2659 	 * reference to a freed block, or an unclaimed log block.
2660 	 */
2661 	zdb_leak_init(spa, &zcb);
2662 
2663 	/*
2664 	 * If there's a deferred-free bplist, process that first.
2665 	 */
2666 	(void) bpobj_iterate_nofree(&spa->spa_deferred_bpobj,
2667 	    count_block_cb, &zcb, NULL);
2668 	if (spa_version(spa) >= SPA_VERSION_DEADLISTS) {
2669 		(void) bpobj_iterate_nofree(&spa->spa_dsl_pool->dp_free_bpobj,
2670 		    count_block_cb, &zcb, NULL);
2671 	}
2672 	if (spa_feature_is_active(spa, SPA_FEATURE_ASYNC_DESTROY)) {
2673 		VERIFY3U(0, ==, bptree_iterate(spa->spa_meta_objset,
2674 		    spa->spa_dsl_pool->dp_bptree_obj, B_FALSE, count_block_cb,
2675 		    &zcb, NULL));
2676 	}
2677 
2678 	if (dump_opt['c'] > 1)
2679 		flags |= TRAVERSE_PREFETCH_DATA;
2680 
2681 	zcb.zcb_totalasize = metaslab_class_get_alloc(spa_normal_class(spa));
2682 	zcb.zcb_start = zcb.zcb_lastprint = gethrtime();
2683 	zcb.zcb_haderrors |= traverse_pool(spa, 0, flags, zdb_blkptr_cb, &zcb);
2684 
2685 	/*
2686 	 * If we've traversed the data blocks then we need to wait for those
2687 	 * I/Os to complete. We leverage "The Godfather" zio to wait on
2688 	 * all async I/Os to complete.
2689 	 */
2690 	if (dump_opt['c']) {
2691 		for (int i = 0; i < max_ncpus; i++) {
2692 			(void) zio_wait(spa->spa_async_zio_root[i]);
2693 			spa->spa_async_zio_root[i] = zio_root(spa, NULL, NULL,
2694 			    ZIO_FLAG_CANFAIL | ZIO_FLAG_SPECULATIVE |
2695 			    ZIO_FLAG_GODFATHER);
2696 		}
2697 	}
2698 
2699 	if (zcb.zcb_haderrors) {
2700 		(void) printf("\nError counts:\n\n");
2701 		(void) printf("\t%5s  %s\n", "errno", "count");
2702 		for (int e = 0; e < 256; e++) {
2703 			if (zcb.zcb_errors[e] != 0) {
2704 				(void) printf("\t%5d  %llu\n",
2705 				    e, (u_longlong_t)zcb.zcb_errors[e]);
2706 			}
2707 		}
2708 	}
2709 
2710 	/*
2711 	 * Report any leaked segments.
2712 	 */
2713 	zdb_leak_fini(spa);
2714 
2715 	tzb = &zcb.zcb_type[ZB_TOTAL][ZDB_OT_TOTAL];
2716 
2717 	norm_alloc = metaslab_class_get_alloc(spa_normal_class(spa));
2718 	norm_space = metaslab_class_get_space(spa_normal_class(spa));
2719 
2720 	total_alloc = norm_alloc + metaslab_class_get_alloc(spa_log_class(spa));
2721 	total_found = tzb->zb_asize - zcb.zcb_dedup_asize;
2722 
2723 	if (total_found == total_alloc) {
2724 		if (!dump_opt['L'])
2725 			(void) printf("\n\tNo leaks (block sum matches space"
2726 			    " maps exactly)\n");
2727 	} else {
2728 		(void) printf("block traversal size %llu != alloc %llu "
2729 		    "(%s %lld)\n",
2730 		    (u_longlong_t)total_found,
2731 		    (u_longlong_t)total_alloc,
2732 		    (dump_opt['L']) ? "unreachable" : "leaked",
2733 		    (longlong_t)(total_alloc - total_found));
2734 		leaks = B_TRUE;
2735 	}
2736 
2737 	if (tzb->zb_count == 0)
2738 		return (2);
2739 
2740 	(void) printf("\n");
2741 	(void) printf("\tbp count:      %10llu\n",
2742 	    (u_longlong_t)tzb->zb_count);
2743 	(void) printf("\tganged count:  %10llu\n",
2744 	    (longlong_t)tzb->zb_gangs);
2745 	(void) printf("\tbp logical:    %10llu      avg: %6llu\n",
2746 	    (u_longlong_t)tzb->zb_lsize,
2747 	    (u_longlong_t)(tzb->zb_lsize / tzb->zb_count));
2748 	(void) printf("\tbp physical:   %10llu      avg:"
2749 	    " %6llu     compression: %6.2f\n",
2750 	    (u_longlong_t)tzb->zb_psize,
2751 	    (u_longlong_t)(tzb->zb_psize / tzb->zb_count),
2752 	    (double)tzb->zb_lsize / tzb->zb_psize);
2753 	(void) printf("\tbp allocated:  %10llu      avg:"
2754 	    " %6llu     compression: %6.2f\n",
2755 	    (u_longlong_t)tzb->zb_asize,
2756 	    (u_longlong_t)(tzb->zb_asize / tzb->zb_count),
2757 	    (double)tzb->zb_lsize / tzb->zb_asize);
2758 	(void) printf("\tbp deduped:    %10llu    ref>1:"
2759 	    " %6llu   deduplication: %6.2f\n",
2760 	    (u_longlong_t)zcb.zcb_dedup_asize,
2761 	    (u_longlong_t)zcb.zcb_dedup_blocks,
2762 	    (double)zcb.zcb_dedup_asize / tzb->zb_asize + 1.0);
2763 	(void) printf("\tSPA allocated: %10llu     used: %5.2f%%\n",
2764 	    (u_longlong_t)norm_alloc, 100.0 * norm_alloc / norm_space);
2765 
2766 	for (bp_embedded_type_t i = 0; i < NUM_BP_EMBEDDED_TYPES; i++) {
2767 		if (zcb.zcb_embedded_blocks[i] == 0)
2768 			continue;
2769 		(void) printf("\n");
2770 		(void) printf("\tadditional, non-pointer bps of type %u: "
2771 		    "%10llu\n",
2772 		    i, (u_longlong_t)zcb.zcb_embedded_blocks[i]);
2773 
2774 		if (dump_opt['b'] >= 3) {
2775 			(void) printf("\t number of (compressed) bytes:  "
2776 			    "number of bps\n");
2777 			dump_histogram(zcb.zcb_embedded_histogram[i],
2778 			    sizeof (zcb.zcb_embedded_histogram[i]) /
2779 			    sizeof (zcb.zcb_embedded_histogram[i][0]), 0);
2780 		}
2781 	}
2782 
2783 	if (tzb->zb_ditto_samevdev != 0) {
2784 		(void) printf("\tDittoed blocks on same vdev: %llu\n",
2785 		    (longlong_t)tzb->zb_ditto_samevdev);
2786 	}
2787 
2788 	if (dump_opt['b'] >= 2) {
2789 		int l, t, level;
2790 		(void) printf("\nBlocks\tLSIZE\tPSIZE\tASIZE"
2791 		    "\t  avg\t comp\t%%Total\tType\n");
2792 
2793 		for (t = 0; t <= ZDB_OT_TOTAL; t++) {
2794 			char csize[32], lsize[32], psize[32], asize[32];
2795 			char avg[32], gang[32];
2796 			char *typename;
2797 
2798 			if (t < DMU_OT_NUMTYPES)
2799 				typename = dmu_ot[t].ot_name;
2800 			else
2801 				typename = zdb_ot_extname[t - DMU_OT_NUMTYPES];
2802 
2803 			if (zcb.zcb_type[ZB_TOTAL][t].zb_asize == 0) {
2804 				(void) printf("%6s\t%5s\t%5s\t%5s"
2805 				    "\t%5s\t%5s\t%6s\t%s\n",
2806 				    "-",
2807 				    "-",
2808 				    "-",
2809 				    "-",
2810 				    "-",
2811 				    "-",
2812 				    "-",
2813 				    typename);
2814 				continue;
2815 			}
2816 
2817 			for (l = ZB_TOTAL - 1; l >= -1; l--) {
2818 				level = (l == -1 ? ZB_TOTAL : l);
2819 				zb = &zcb.zcb_type[level][t];
2820 
2821 				if (zb->zb_asize == 0)
2822 					continue;
2823 
2824 				if (dump_opt['b'] < 3 && level != ZB_TOTAL)
2825 					continue;
2826 
2827 				if (level == 0 && zb->zb_asize ==
2828 				    zcb.zcb_type[ZB_TOTAL][t].zb_asize)
2829 					continue;
2830 
2831 				zdb_nicenum(zb->zb_count, csize);
2832 				zdb_nicenum(zb->zb_lsize, lsize);
2833 				zdb_nicenum(zb->zb_psize, psize);
2834 				zdb_nicenum(zb->zb_asize, asize);
2835 				zdb_nicenum(zb->zb_asize / zb->zb_count, avg);
2836 				zdb_nicenum(zb->zb_gangs, gang);
2837 
2838 				(void) printf("%6s\t%5s\t%5s\t%5s\t%5s"
2839 				    "\t%5.2f\t%6.2f\t",
2840 				    csize, lsize, psize, asize, avg,
2841 				    (double)zb->zb_lsize / zb->zb_psize,
2842 				    100.0 * zb->zb_asize / tzb->zb_asize);
2843 
2844 				if (level == ZB_TOTAL)
2845 					(void) printf("%s\n", typename);
2846 				else
2847 					(void) printf("    L%d %s\n",
2848 					    level, typename);
2849 
2850 				if (dump_opt['b'] >= 3 && zb->zb_gangs > 0) {
2851 					(void) printf("\t number of ganged "
2852 					    "blocks: %s\n", gang);
2853 				}
2854 
2855 				if (dump_opt['b'] >= 4) {
2856 					(void) printf("psize "
2857 					    "(in 512-byte sectors): "
2858 					    "number of blocks\n");
2859 					dump_histogram(zb->zb_psize_histogram,
2860 					    PSIZE_HISTO_SIZE, 0);
2861 				}
2862 			}
2863 		}
2864 	}
2865 
2866 	(void) printf("\n");
2867 
2868 	if (leaks)
2869 		return (2);
2870 
2871 	if (zcb.zcb_haderrors)
2872 		return (3);
2873 
2874 	return (0);
2875 }
2876 
2877 typedef struct zdb_ddt_entry {
2878 	ddt_key_t	zdde_key;
2879 	uint64_t	zdde_ref_blocks;
2880 	uint64_t	zdde_ref_lsize;
2881 	uint64_t	zdde_ref_psize;
2882 	uint64_t	zdde_ref_dsize;
2883 	avl_node_t	zdde_node;
2884 } zdb_ddt_entry_t;
2885 
2886 /* ARGSUSED */
2887 static int
2888 zdb_ddt_add_cb(spa_t *spa, zilog_t *zilog, const blkptr_t *bp,
2889     const zbookmark_phys_t *zb, const dnode_phys_t *dnp, void *arg)
2890 {
2891 	avl_tree_t *t = arg;
2892 	avl_index_t where;
2893 	zdb_ddt_entry_t *zdde, zdde_search;
2894 
2895 	if (BP_IS_HOLE(bp) || BP_IS_EMBEDDED(bp))
2896 		return (0);
2897 
2898 	if (dump_opt['S'] > 1 && zb->zb_level == ZB_ROOT_LEVEL) {
2899 		(void) printf("traversing objset %llu, %llu objects, "
2900 		    "%lu blocks so far\n",
2901 		    (u_longlong_t)zb->zb_objset,
2902 		    (u_longlong_t)BP_GET_FILL(bp),
2903 		    avl_numnodes(t));
2904 	}
2905 
2906 	if (BP_IS_HOLE(bp) || BP_GET_CHECKSUM(bp) == ZIO_CHECKSUM_OFF ||
2907 	    BP_GET_LEVEL(bp) > 0 || DMU_OT_IS_METADATA(BP_GET_TYPE(bp)))
2908 		return (0);
2909 
2910 	ddt_key_fill(&zdde_search.zdde_key, bp);
2911 
2912 	zdde = avl_find(t, &zdde_search, &where);
2913 
2914 	if (zdde == NULL) {
2915 		zdde = umem_zalloc(sizeof (*zdde), UMEM_NOFAIL);
2916 		zdde->zdde_key = zdde_search.zdde_key;
2917 		avl_insert(t, zdde, where);
2918 	}
2919 
2920 	zdde->zdde_ref_blocks += 1;
2921 	zdde->zdde_ref_lsize += BP_GET_LSIZE(bp);
2922 	zdde->zdde_ref_psize += BP_GET_PSIZE(bp);
2923 	zdde->zdde_ref_dsize += bp_get_dsize_sync(spa, bp);
2924 
2925 	return (0);
2926 }
2927 
2928 static void
2929 dump_simulated_ddt(spa_t *spa)
2930 {
2931 	avl_tree_t t;
2932 	void *cookie = NULL;
2933 	zdb_ddt_entry_t *zdde;
2934 	ddt_histogram_t ddh_total = { 0 };
2935 	ddt_stat_t dds_total = { 0 };
2936 
2937 	avl_create(&t, ddt_entry_compare,
2938 	    sizeof (zdb_ddt_entry_t), offsetof(zdb_ddt_entry_t, zdde_node));
2939 
2940 	spa_config_enter(spa, SCL_CONFIG, FTAG, RW_READER);
2941 
2942 	(void) traverse_pool(spa, 0, TRAVERSE_PRE | TRAVERSE_PREFETCH_METADATA,
2943 	    zdb_ddt_add_cb, &t);
2944 
2945 	spa_config_exit(spa, SCL_CONFIG, FTAG);
2946 
2947 	while ((zdde = avl_destroy_nodes(&t, &cookie)) != NULL) {
2948 		ddt_stat_t dds;
2949 		uint64_t refcnt = zdde->zdde_ref_blocks;
2950 		ASSERT(refcnt != 0);
2951 
2952 		dds.dds_blocks = zdde->zdde_ref_blocks / refcnt;
2953 		dds.dds_lsize = zdde->zdde_ref_lsize / refcnt;
2954 		dds.dds_psize = zdde->zdde_ref_psize / refcnt;
2955 		dds.dds_dsize = zdde->zdde_ref_dsize / refcnt;
2956 
2957 		dds.dds_ref_blocks = zdde->zdde_ref_blocks;
2958 		dds.dds_ref_lsize = zdde->zdde_ref_lsize;
2959 		dds.dds_ref_psize = zdde->zdde_ref_psize;
2960 		dds.dds_ref_dsize = zdde->zdde_ref_dsize;
2961 
2962 		ddt_stat_add(&ddh_total.ddh_stat[highbit64(refcnt) - 1],
2963 		    &dds, 0);
2964 
2965 		umem_free(zdde, sizeof (*zdde));
2966 	}
2967 
2968 	avl_destroy(&t);
2969 
2970 	ddt_histogram_stat(&dds_total, &ddh_total);
2971 
2972 	(void) printf("Simulated DDT histogram:\n");
2973 
2974 	zpool_dump_ddt(&dds_total, &ddh_total);
2975 
2976 	dump_dedup_ratio(&dds_total);
2977 }
2978 
2979 static void
2980 dump_zpool(spa_t *spa)
2981 {
2982 	dsl_pool_t *dp = spa_get_dsl(spa);
2983 	int rc = 0;
2984 
2985 	if (dump_opt['S']) {
2986 		dump_simulated_ddt(spa);
2987 		return;
2988 	}
2989 
2990 	if (!dump_opt['e'] && dump_opt['C'] > 1) {
2991 		(void) printf("\nCached configuration:\n");
2992 		dump_nvlist(spa->spa_config, 8);
2993 	}
2994 
2995 	if (dump_opt['C'])
2996 		dump_config(spa);
2997 
2998 	if (dump_opt['u'])
2999 		dump_uberblock(&spa->spa_uberblock, "\nUberblock:\n", "\n");
3000 
3001 	if (dump_opt['D'])
3002 		dump_all_ddts(spa);
3003 
3004 	if (dump_opt['d'] > 2 || dump_opt['m'])
3005 		dump_metaslabs(spa);
3006 	if (dump_opt['M'])
3007 		dump_metaslab_groups(spa);
3008 
3009 	if (dump_opt['d'] || dump_opt['i']) {
3010 		uint64_t refcount;
3011 		dump_dir(dp->dp_meta_objset);
3012 		if (dump_opt['d'] >= 3) {
3013 			dump_full_bpobj(&spa->spa_deferred_bpobj,
3014 			    "Deferred frees", 0);
3015 			if (spa_version(spa) >= SPA_VERSION_DEADLISTS) {
3016 				dump_full_bpobj(
3017 				    &spa->spa_dsl_pool->dp_free_bpobj,
3018 				    "Pool snapshot frees", 0);
3019 			}
3020 
3021 			if (spa_feature_is_active(spa,
3022 			    SPA_FEATURE_ASYNC_DESTROY)) {
3023 				dump_bptree(spa->spa_meta_objset,
3024 				    spa->spa_dsl_pool->dp_bptree_obj,
3025 				    "Pool dataset frees");
3026 			}
3027 			dump_dtl(spa->spa_root_vdev, 0);
3028 		}
3029 		(void) dmu_objset_find(spa_name(spa), dump_one_dir,
3030 		    NULL, DS_FIND_SNAPSHOTS | DS_FIND_CHILDREN);
3031 
3032 		(void) feature_get_refcount(spa,
3033 		    &spa_feature_table[SPA_FEATURE_LARGE_BLOCKS], &refcount);
3034 		if (num_large_blocks != refcount) {
3035 			(void) printf("large_blocks feature refcount mismatch: "
3036 			    "expected %lld != actual %lld\n",
3037 			    (longlong_t)num_large_blocks,
3038 			    (longlong_t)refcount);
3039 			rc = 2;
3040 		} else {
3041 			(void) printf("Verified large_blocks feature refcount "
3042 			    "is correct (%llu)\n", (longlong_t)refcount);
3043 		}
3044 	}
3045 	if (rc == 0 && (dump_opt['b'] || dump_opt['c']))
3046 		rc = dump_block_stats(spa);
3047 
3048 	if (rc == 0)
3049 		rc = verify_spacemap_refcounts(spa);
3050 
3051 	if (dump_opt['s'])
3052 		show_pool_stats(spa);
3053 
3054 	if (dump_opt['h'])
3055 		dump_history(spa);
3056 
3057 	if (rc != 0)
3058 		exit(rc);
3059 }
3060 
3061 #define	ZDB_FLAG_CHECKSUM	0x0001
3062 #define	ZDB_FLAG_DECOMPRESS	0x0002
3063 #define	ZDB_FLAG_BSWAP		0x0004
3064 #define	ZDB_FLAG_GBH		0x0008
3065 #define	ZDB_FLAG_INDIRECT	0x0010
3066 #define	ZDB_FLAG_PHYS		0x0020
3067 #define	ZDB_FLAG_RAW		0x0040
3068 #define	ZDB_FLAG_PRINT_BLKPTR	0x0080
3069 
3070 int flagbits[256];
3071 
3072 static void
3073 zdb_print_blkptr(blkptr_t *bp, int flags)
3074 {
3075 	char blkbuf[BP_SPRINTF_LEN];
3076 
3077 	if (flags & ZDB_FLAG_BSWAP)
3078 		byteswap_uint64_array((void *)bp, sizeof (blkptr_t));
3079 
3080 	snprintf_blkptr(blkbuf, sizeof (blkbuf), bp);
3081 	(void) printf("%s\n", blkbuf);
3082 }
3083 
3084 static void
3085 zdb_dump_indirect(blkptr_t *bp, int nbps, int flags)
3086 {
3087 	int i;
3088 
3089 	for (i = 0; i < nbps; i++)
3090 		zdb_print_blkptr(&bp[i], flags);
3091 }
3092 
3093 static void
3094 zdb_dump_gbh(void *buf, int flags)
3095 {
3096 	zdb_dump_indirect((blkptr_t *)buf, SPA_GBH_NBLKPTRS, flags);
3097 }
3098 
3099 static void
3100 zdb_dump_block_raw(void *buf, uint64_t size, int flags)
3101 {
3102 	if (flags & ZDB_FLAG_BSWAP)
3103 		byteswap_uint64_array(buf, size);
3104 	(void) write(1, buf, size);
3105 }
3106 
3107 static void
3108 zdb_dump_block(char *label, void *buf, uint64_t size, int flags)
3109 {
3110 	uint64_t *d = (uint64_t *)buf;
3111 	int nwords = size / sizeof (uint64_t);
3112 	int do_bswap = !!(flags & ZDB_FLAG_BSWAP);
3113 	int i, j;
3114 	char *hdr, *c;
3115 
3116 
3117 	if (do_bswap)
3118 		hdr = " 7 6 5 4 3 2 1 0   f e d c b a 9 8";
3119 	else
3120 		hdr = " 0 1 2 3 4 5 6 7   8 9 a b c d e f";
3121 
3122 	(void) printf("\n%s\n%6s   %s  0123456789abcdef\n", label, "", hdr);
3123 
3124 	for (i = 0; i < nwords; i += 2) {
3125 		(void) printf("%06llx:  %016llx  %016llx  ",
3126 		    (u_longlong_t)(i * sizeof (uint64_t)),
3127 		    (u_longlong_t)(do_bswap ? BSWAP_64(d[i]) : d[i]),
3128 		    (u_longlong_t)(do_bswap ? BSWAP_64(d[i + 1]) : d[i + 1]));
3129 
3130 		c = (char *)&d[i];
3131 		for (j = 0; j < 2 * sizeof (uint64_t); j++)
3132 			(void) printf("%c", isprint(c[j]) ? c[j] : '.');
3133 		(void) printf("\n");
3134 	}
3135 }
3136 
3137 /*
3138  * There are two acceptable formats:
3139  *	leaf_name	  - For example: c1t0d0 or /tmp/ztest.0a
3140  *	child[.child]*    - For example: 0.1.1
3141  *
3142  * The second form can be used to specify arbitrary vdevs anywhere
3143  * in the heirarchy.  For example, in a pool with a mirror of
3144  * RAID-Zs, you can specify either RAID-Z vdev with 0.0 or 0.1 .
3145  */
3146 static vdev_t *
3147 zdb_vdev_lookup(vdev_t *vdev, char *path)
3148 {
3149 	char *s, *p, *q;
3150 	int i;
3151 
3152 	if (vdev == NULL)
3153 		return (NULL);
3154 
3155 	/* First, assume the x.x.x.x format */
3156 	i = (int)strtoul(path, &s, 10);
3157 	if (s == path || (s && *s != '.' && *s != '\0'))
3158 		goto name;
3159 	if (i < 0 || i >= vdev->vdev_children)
3160 		return (NULL);
3161 
3162 	vdev = vdev->vdev_child[i];
3163 	if (*s == '\0')
3164 		return (vdev);
3165 	return (zdb_vdev_lookup(vdev, s+1));
3166 
3167 name:
3168 	for (i = 0; i < vdev->vdev_children; i++) {
3169 		vdev_t *vc = vdev->vdev_child[i];
3170 
3171 		if (vc->vdev_path == NULL) {
3172 			vc = zdb_vdev_lookup(vc, path);
3173 			if (vc == NULL)
3174 				continue;
3175 			else
3176 				return (vc);
3177 		}
3178 
3179 		p = strrchr(vc->vdev_path, '/');
3180 		p = p ? p + 1 : vc->vdev_path;
3181 		q = &vc->vdev_path[strlen(vc->vdev_path) - 2];
3182 
3183 		if (strcmp(vc->vdev_path, path) == 0)
3184 			return (vc);
3185 		if (strcmp(p, path) == 0)
3186 			return (vc);
3187 		if (strcmp(q, "s0") == 0 && strncmp(p, path, q - p) == 0)
3188 			return (vc);
3189 	}
3190 
3191 	return (NULL);
3192 }
3193 
3194 /*
3195  * Read a block from a pool and print it out.  The syntax of the
3196  * block descriptor is:
3197  *
3198  *	pool:vdev_specifier:offset:size[:flags]
3199  *
3200  *	pool           - The name of the pool you wish to read from
3201  *	vdev_specifier - Which vdev (see comment for zdb_vdev_lookup)
3202  *	offset         - offset, in hex, in bytes
3203  *	size           - Amount of data to read, in hex, in bytes
3204  *	flags          - A string of characters specifying options
3205  *		 b: Decode a blkptr at given offset within block
3206  *		*c: Calculate and display checksums
3207  *		 d: Decompress data before dumping
3208  *		 e: Byteswap data before dumping
3209  *		 g: Display data as a gang block header
3210  *		 i: Display as an indirect block
3211  *		 p: Do I/O to physical offset
3212  *		 r: Dump raw data to stdout
3213  *
3214  *              * = not yet implemented
3215  */
3216 static void
3217 zdb_read_block(char *thing, spa_t *spa)
3218 {
3219 	blkptr_t blk, *bp = &blk;
3220 	dva_t *dva = bp->blk_dva;
3221 	int flags = 0;
3222 	uint64_t offset = 0, size = 0, psize = 0, lsize = 0, blkptr_offset = 0;
3223 	zio_t *zio;
3224 	vdev_t *vd;
3225 	void *pbuf, *lbuf, *buf;
3226 	char *s, *p, *dup, *vdev, *flagstr;
3227 	int i, error;
3228 
3229 	dup = strdup(thing);
3230 	s = strtok(dup, ":");
3231 	vdev = s ? s : "";
3232 	s = strtok(NULL, ":");
3233 	offset = strtoull(s ? s : "", NULL, 16);
3234 	s = strtok(NULL, ":");
3235 	size = strtoull(s ? s : "", NULL, 16);
3236 	s = strtok(NULL, ":");
3237 	flagstr = s ? s : "";
3238 
3239 	s = NULL;
3240 	if (size == 0)
3241 		s = "size must not be zero";
3242 	if (!IS_P2ALIGNED(size, DEV_BSIZE))
3243 		s = "size must be a multiple of sector size";
3244 	if (!IS_P2ALIGNED(offset, DEV_BSIZE))
3245 		s = "offset must be a multiple of sector size";
3246 	if (s) {
3247 		(void) printf("Invalid block specifier: %s  - %s\n", thing, s);
3248 		free(dup);
3249 		return;
3250 	}
3251 
3252 	for (s = strtok(flagstr, ":"); s; s = strtok(NULL, ":")) {
3253 		for (i = 0; flagstr[i]; i++) {
3254 			int bit = flagbits[(uchar_t)flagstr[i]];
3255 
3256 			if (bit == 0) {
3257 				(void) printf("***Invalid flag: %c\n",
3258 				    flagstr[i]);
3259 				continue;
3260 			}
3261 			flags |= bit;
3262 
3263 			/* If it's not something with an argument, keep going */
3264 			if ((bit & (ZDB_FLAG_CHECKSUM |
3265 			    ZDB_FLAG_PRINT_BLKPTR)) == 0)
3266 				continue;
3267 
3268 			p = &flagstr[i + 1];
3269 			if (bit == ZDB_FLAG_PRINT_BLKPTR)
3270 				blkptr_offset = strtoull(p, &p, 16);
3271 			if (*p != ':' && *p != '\0') {
3272 				(void) printf("***Invalid flag arg: '%s'\n", s);
3273 				free(dup);
3274 				return;
3275 			}
3276 		}
3277 	}
3278 
3279 	vd = zdb_vdev_lookup(spa->spa_root_vdev, vdev);
3280 	if (vd == NULL) {
3281 		(void) printf("***Invalid vdev: %s\n", vdev);
3282 		free(dup);
3283 		return;
3284 	} else {
3285 		if (vd->vdev_path)
3286 			(void) fprintf(stderr, "Found vdev: %s\n",
3287 			    vd->vdev_path);
3288 		else
3289 			(void) fprintf(stderr, "Found vdev type: %s\n",
3290 			    vd->vdev_ops->vdev_op_type);
3291 	}
3292 
3293 	psize = size;
3294 	lsize = size;
3295 
3296 	pbuf = umem_alloc(SPA_MAXBLOCKSIZE, UMEM_NOFAIL);
3297 	lbuf = umem_alloc(SPA_MAXBLOCKSIZE, UMEM_NOFAIL);
3298 
3299 	BP_ZERO(bp);
3300 
3301 	DVA_SET_VDEV(&dva[0], vd->vdev_id);
3302 	DVA_SET_OFFSET(&dva[0], offset);
3303 	DVA_SET_GANG(&dva[0], !!(flags & ZDB_FLAG_GBH));
3304 	DVA_SET_ASIZE(&dva[0], vdev_psize_to_asize(vd, psize));
3305 
3306 	BP_SET_BIRTH(bp, TXG_INITIAL, TXG_INITIAL);
3307 
3308 	BP_SET_LSIZE(bp, lsize);
3309 	BP_SET_PSIZE(bp, psize);
3310 	BP_SET_COMPRESS(bp, ZIO_COMPRESS_OFF);
3311 	BP_SET_CHECKSUM(bp, ZIO_CHECKSUM_OFF);
3312 	BP_SET_TYPE(bp, DMU_OT_NONE);
3313 	BP_SET_LEVEL(bp, 0);
3314 	BP_SET_DEDUP(bp, 0);
3315 	BP_SET_BYTEORDER(bp, ZFS_HOST_BYTEORDER);
3316 
3317 	spa_config_enter(spa, SCL_STATE, FTAG, RW_READER);
3318 	zio = zio_root(spa, NULL, NULL, 0);
3319 
3320 	if (vd == vd->vdev_top) {
3321 		/*
3322 		 * Treat this as a normal block read.
3323 		 */
3324 		zio_nowait(zio_read(zio, spa, bp, pbuf, psize, NULL, NULL,
3325 		    ZIO_PRIORITY_SYNC_READ,
3326 		    ZIO_FLAG_CANFAIL | ZIO_FLAG_RAW, NULL));
3327 	} else {
3328 		/*
3329 		 * Treat this as a vdev child I/O.
3330 		 */
3331 		zio_nowait(zio_vdev_child_io(zio, bp, vd, offset, pbuf, psize,
3332 		    ZIO_TYPE_READ, ZIO_PRIORITY_SYNC_READ,
3333 		    ZIO_FLAG_DONT_CACHE | ZIO_FLAG_DONT_QUEUE |
3334 		    ZIO_FLAG_DONT_PROPAGATE | ZIO_FLAG_DONT_RETRY |
3335 		    ZIO_FLAG_CANFAIL | ZIO_FLAG_RAW, NULL, NULL));
3336 	}
3337 
3338 	error = zio_wait(zio);
3339 	spa_config_exit(spa, SCL_STATE, FTAG);
3340 
3341 	if (error) {
3342 		(void) printf("Read of %s failed, error: %d\n", thing, error);
3343 		goto out;
3344 	}
3345 
3346 	if (flags & ZDB_FLAG_DECOMPRESS) {
3347 		/*
3348 		 * We don't know how the data was compressed, so just try
3349 		 * every decompress function at every inflated blocksize.
3350 		 */
3351 		enum zio_compress c;
3352 		void *pbuf2 = umem_alloc(SPA_MAXBLOCKSIZE, UMEM_NOFAIL);
3353 		void *lbuf2 = umem_alloc(SPA_MAXBLOCKSIZE, UMEM_NOFAIL);
3354 
3355 		bcopy(pbuf, pbuf2, psize);
3356 
3357 		VERIFY(random_get_pseudo_bytes((uint8_t *)pbuf + psize,
3358 		    SPA_MAXBLOCKSIZE - psize) == 0);
3359 
3360 		VERIFY(random_get_pseudo_bytes((uint8_t *)pbuf2 + psize,
3361 		    SPA_MAXBLOCKSIZE - psize) == 0);
3362 
3363 		for (lsize = SPA_MAXBLOCKSIZE; lsize > psize;
3364 		    lsize -= SPA_MINBLOCKSIZE) {
3365 			for (c = 0; c < ZIO_COMPRESS_FUNCTIONS; c++) {
3366 				if (zio_decompress_data(c, pbuf, lbuf,
3367 				    psize, lsize) == 0 &&
3368 				    zio_decompress_data(c, pbuf2, lbuf2,
3369 				    psize, lsize) == 0 &&
3370 				    bcmp(lbuf, lbuf2, lsize) == 0)
3371 					break;
3372 			}
3373 			if (c != ZIO_COMPRESS_FUNCTIONS)
3374 				break;
3375 			lsize -= SPA_MINBLOCKSIZE;
3376 		}
3377 
3378 		umem_free(pbuf2, SPA_MAXBLOCKSIZE);
3379 		umem_free(lbuf2, SPA_MAXBLOCKSIZE);
3380 
3381 		if (lsize <= psize) {
3382 			(void) printf("Decompress of %s failed\n", thing);
3383 			goto out;
3384 		}
3385 		buf = lbuf;
3386 		size = lsize;
3387 	} else {
3388 		buf = pbuf;
3389 		size = psize;
3390 	}
3391 
3392 	if (flags & ZDB_FLAG_PRINT_BLKPTR)
3393 		zdb_print_blkptr((blkptr_t *)(void *)
3394 		    ((uintptr_t)buf + (uintptr_t)blkptr_offset), flags);
3395 	else if (flags & ZDB_FLAG_RAW)
3396 		zdb_dump_block_raw(buf, size, flags);
3397 	else if (flags & ZDB_FLAG_INDIRECT)
3398 		zdb_dump_indirect((blkptr_t *)buf, size / sizeof (blkptr_t),
3399 		    flags);
3400 	else if (flags & ZDB_FLAG_GBH)
3401 		zdb_dump_gbh(buf, flags);
3402 	else
3403 		zdb_dump_block(thing, buf, size, flags);
3404 
3405 out:
3406 	umem_free(pbuf, SPA_MAXBLOCKSIZE);
3407 	umem_free(lbuf, SPA_MAXBLOCKSIZE);
3408 	free(dup);
3409 }
3410 
3411 static boolean_t
3412 pool_match(nvlist_t *cfg, char *tgt)
3413 {
3414 	uint64_t v, guid = strtoull(tgt, NULL, 0);
3415 	char *s;
3416 
3417 	if (guid != 0) {
3418 		if (nvlist_lookup_uint64(cfg, ZPOOL_CONFIG_POOL_GUID, &v) == 0)
3419 			return (v == guid);
3420 	} else {
3421 		if (nvlist_lookup_string(cfg, ZPOOL_CONFIG_POOL_NAME, &s) == 0)
3422 			return (strcmp(s, tgt) == 0);
3423 	}
3424 	return (B_FALSE);
3425 }
3426 
3427 static char *
3428 find_zpool(char **target, nvlist_t **configp, int dirc, char **dirv)
3429 {
3430 	nvlist_t *pools;
3431 	nvlist_t *match = NULL;
3432 	char *name = NULL;
3433 	char *sepp = NULL;
3434 	char sep;
3435 	int count = 0;
3436 	importargs_t args = { 0 };
3437 
3438 	args.paths = dirc;
3439 	args.path = dirv;
3440 	args.can_be_active = B_TRUE;
3441 
3442 	if ((sepp = strpbrk(*target, "/@")) != NULL) {
3443 		sep = *sepp;
3444 		*sepp = '\0';
3445 	}
3446 
3447 	pools = zpool_search_import(g_zfs, &args);
3448 
3449 	if (pools != NULL) {
3450 		nvpair_t *elem = NULL;
3451 		while ((elem = nvlist_next_nvpair(pools, elem)) != NULL) {
3452 			verify(nvpair_value_nvlist(elem, configp) == 0);
3453 			if (pool_match(*configp, *target)) {
3454 				count++;
3455 				if (match != NULL) {
3456 					/* print previously found config */
3457 					if (name != NULL) {
3458 						(void) printf("%s\n", name);
3459 						dump_nvlist(match, 8);
3460 						name = NULL;
3461 					}
3462 					(void) printf("%s\n",
3463 					    nvpair_name(elem));
3464 					dump_nvlist(*configp, 8);
3465 				} else {
3466 					match = *configp;
3467 					name = nvpair_name(elem);
3468 				}
3469 			}
3470 		}
3471 	}
3472 	if (count > 1)
3473 		(void) fatal("\tMatched %d pools - use pool GUID "
3474 		    "instead of pool name or \n"
3475 		    "\tpool name part of a dataset name to select pool", count);
3476 
3477 	if (sepp)
3478 		*sepp = sep;
3479 	/*
3480 	 * If pool GUID was specified for pool id, replace it with pool name
3481 	 */
3482 	if (name && (strstr(*target, name) != *target)) {
3483 		int sz = 1 + strlen(name) + ((sepp) ? strlen(sepp) : 0);
3484 
3485 		*target = umem_alloc(sz, UMEM_NOFAIL);
3486 		(void) snprintf(*target, sz, "%s%s", name, sepp ? sepp : "");
3487 	}
3488 
3489 	*configp = name ? match : NULL;
3490 
3491 	return (name);
3492 }
3493 
3494 int
3495 main(int argc, char **argv)
3496 {
3497 	int i, c;
3498 	struct rlimit rl = { 1024, 1024 };
3499 	spa_t *spa = NULL;
3500 	objset_t *os = NULL;
3501 	int dump_all = 1;
3502 	int verbose = 0;
3503 	int error = 0;
3504 	char **searchdirs = NULL;
3505 	int nsearch = 0;
3506 	char *target;
3507 	nvlist_t *policy = NULL;
3508 	uint64_t max_txg = UINT64_MAX;
3509 	int rewind = ZPOOL_NEVER_REWIND;
3510 
3511 	(void) setrlimit(RLIMIT_NOFILE, &rl);
3512 	(void) enable_extended_FILE_stdio(-1, -1);
3513 
3514 	dprintf_setup(&argc, argv);
3515 
3516 	while ((c = getopt(argc, argv,
3517 	    "bcdhilmMI:suCDRSAFLXx:evp:t:U:P")) != -1) {
3518 		switch (c) {
3519 		case 'b':
3520 		case 'c':
3521 		case 'd':
3522 		case 'h':
3523 		case 'i':
3524 		case 'l':
3525 		case 'm':
3526 		case 's':
3527 		case 'u':
3528 		case 'C':
3529 		case 'D':
3530 		case 'M':
3531 		case 'R':
3532 		case 'S':
3533 			dump_opt[c]++;
3534 			dump_all = 0;
3535 			break;
3536 		case 'A':
3537 		case 'F':
3538 		case 'L':
3539 		case 'X':
3540 		case 'e':
3541 		case 'P':
3542 			dump_opt[c]++;
3543 			break;
3544 		case 'I':
3545 			max_inflight = strtoull(optarg, NULL, 0);
3546 			if (max_inflight == 0) {
3547 				(void) fprintf(stderr, "maximum number "
3548 				    "of inflight I/Os must be greater "
3549 				    "than 0\n");
3550 				usage();
3551 			}
3552 			break;
3553 		case 'p':
3554 			if (searchdirs == NULL) {
3555 				searchdirs = umem_alloc(sizeof (char *),
3556 				    UMEM_NOFAIL);
3557 			} else {
3558 				char **tmp = umem_alloc((nsearch + 1) *
3559 				    sizeof (char *), UMEM_NOFAIL);
3560 				bcopy(searchdirs, tmp, nsearch *
3561 				    sizeof (char *));
3562 				umem_free(searchdirs,
3563 				    nsearch * sizeof (char *));
3564 				searchdirs = tmp;
3565 			}
3566 			searchdirs[nsearch++] = optarg;
3567 			break;
3568 		case 't':
3569 			max_txg = strtoull(optarg, NULL, 0);
3570 			if (max_txg < TXG_INITIAL) {
3571 				(void) fprintf(stderr, "incorrect txg "
3572 				    "specified: %s\n", optarg);
3573 				usage();
3574 			}
3575 			break;
3576 		case 'U':
3577 			spa_config_path = optarg;
3578 			break;
3579 		case 'v':
3580 			verbose++;
3581 			break;
3582 		case 'x':
3583 			vn_dumpdir = optarg;
3584 			break;
3585 		default:
3586 			usage();
3587 			break;
3588 		}
3589 	}
3590 
3591 	if (!dump_opt['e'] && searchdirs != NULL) {
3592 		(void) fprintf(stderr, "-p option requires use of -e\n");
3593 		usage();
3594 	}
3595 
3596 	/*
3597 	 * ZDB does not typically re-read blocks; therefore limit the ARC
3598 	 * to 256 MB, which can be used entirely for metadata.
3599 	 */
3600 	zfs_arc_max = zfs_arc_meta_limit = 256 * 1024 * 1024;
3601 
3602 	/*
3603 	 * "zdb -c" uses checksum-verifying scrub i/os which are async reads.
3604 	 * "zdb -b" uses traversal prefetch which uses async reads.
3605 	 * For good performance, let several of them be active at once.
3606 	 */
3607 	zfs_vdev_async_read_max_active = 10;
3608 
3609 	kernel_init(FREAD);
3610 	g_zfs = libzfs_init();
3611 	ASSERT(g_zfs != NULL);
3612 
3613 	if (dump_all)
3614 		verbose = MAX(verbose, 1);
3615 
3616 	for (c = 0; c < 256; c++) {
3617 		if (dump_all && !strchr("elAFLRSXP", c))
3618 			dump_opt[c] = 1;
3619 		if (dump_opt[c])
3620 			dump_opt[c] += verbose;
3621 	}
3622 
3623 	aok = (dump_opt['A'] == 1) || (dump_opt['A'] > 2);
3624 	zfs_recover = (dump_opt['A'] > 1);
3625 
3626 	argc -= optind;
3627 	argv += optind;
3628 
3629 	if (argc < 2 && dump_opt['R'])
3630 		usage();
3631 	if (argc < 1) {
3632 		if (!dump_opt['e'] && dump_opt['C']) {
3633 			dump_cachefile(spa_config_path);
3634 			return (0);
3635 		}
3636 		usage();
3637 	}
3638 
3639 	if (dump_opt['l']) {
3640 		dump_label(argv[0]);
3641 		return (0);
3642 	}
3643 
3644 	if (dump_opt['X'] || dump_opt['F'])
3645 		rewind = ZPOOL_DO_REWIND |
3646 		    (dump_opt['X'] ? ZPOOL_EXTREME_REWIND : 0);
3647 
3648 	if (nvlist_alloc(&policy, NV_UNIQUE_NAME_TYPE, 0) != 0 ||
3649 	    nvlist_add_uint64(policy, ZPOOL_REWIND_REQUEST_TXG, max_txg) != 0 ||
3650 	    nvlist_add_uint32(policy, ZPOOL_REWIND_REQUEST, rewind) != 0)
3651 		fatal("internal error: %s", strerror(ENOMEM));
3652 
3653 	error = 0;
3654 	target = argv[0];
3655 
3656 	if (dump_opt['e']) {
3657 		nvlist_t *cfg = NULL;
3658 		char *name = find_zpool(&target, &cfg, nsearch, searchdirs);
3659 
3660 		error = ENOENT;
3661 		if (name) {
3662 			if (dump_opt['C'] > 1) {
3663 				(void) printf("\nConfiguration for import:\n");
3664 				dump_nvlist(cfg, 8);
3665 			}
3666 			if (nvlist_add_nvlist(cfg,
3667 			    ZPOOL_REWIND_POLICY, policy) != 0) {
3668 				fatal("can't open '%s': %s",
3669 				    target, strerror(ENOMEM));
3670 			}
3671 			if ((error = spa_import(name, cfg, NULL,
3672 			    ZFS_IMPORT_MISSING_LOG)) != 0) {
3673 				error = spa_import(name, cfg, NULL,
3674 				    ZFS_IMPORT_VERBATIM);
3675 			}
3676 		}
3677 	}
3678 
3679 	if (error == 0) {
3680 		if (strpbrk(target, "/@") == NULL || dump_opt['R']) {
3681 			error = spa_open_rewind(target, &spa, FTAG, policy,
3682 			    NULL);
3683 			if (error) {
3684 				/*
3685 				 * If we're missing the log device then
3686 				 * try opening the pool after clearing the
3687 				 * log state.
3688 				 */
3689 				mutex_enter(&spa_namespace_lock);
3690 				if ((spa = spa_lookup(target)) != NULL &&
3691 				    spa->spa_log_state == SPA_LOG_MISSING) {
3692 					spa->spa_log_state = SPA_LOG_CLEAR;
3693 					error = 0;
3694 				}
3695 				mutex_exit(&spa_namespace_lock);
3696 
3697 				if (!error) {
3698 					error = spa_open_rewind(target, &spa,
3699 					    FTAG, policy, NULL);
3700 				}
3701 			}
3702 		} else {
3703 			error = dmu_objset_own(target, DMU_OST_ANY,
3704 			    B_TRUE, FTAG, &os);
3705 		}
3706 	}
3707 	nvlist_free(policy);
3708 
3709 	if (error)
3710 		fatal("can't open '%s': %s", target, strerror(error));
3711 
3712 	argv++;
3713 	argc--;
3714 	if (!dump_opt['R']) {
3715 		if (argc > 0) {
3716 			zopt_objects = argc;
3717 			zopt_object = calloc(zopt_objects, sizeof (uint64_t));
3718 			for (i = 0; i < zopt_objects; i++) {
3719 				errno = 0;
3720 				zopt_object[i] = strtoull(argv[i], NULL, 0);
3721 				if (zopt_object[i] == 0 && errno != 0)
3722 					fatal("bad number %s: %s",
3723 					    argv[i], strerror(errno));
3724 			}
3725 		}
3726 		if (os != NULL) {
3727 			dump_dir(os);
3728 		} else if (zopt_objects > 0 && !dump_opt['m']) {
3729 			dump_dir(spa->spa_meta_objset);
3730 		} else {
3731 			dump_zpool(spa);
3732 		}
3733 	} else {
3734 		flagbits['b'] = ZDB_FLAG_PRINT_BLKPTR;
3735 		flagbits['c'] = ZDB_FLAG_CHECKSUM;
3736 		flagbits['d'] = ZDB_FLAG_DECOMPRESS;
3737 		flagbits['e'] = ZDB_FLAG_BSWAP;
3738 		flagbits['g'] = ZDB_FLAG_GBH;
3739 		flagbits['i'] = ZDB_FLAG_INDIRECT;
3740 		flagbits['p'] = ZDB_FLAG_PHYS;
3741 		flagbits['r'] = ZDB_FLAG_RAW;
3742 
3743 		for (i = 0; i < argc; i++)
3744 			zdb_read_block(argv[i], spa);
3745 	}
3746 
3747 	(os != NULL) ? dmu_objset_disown(os, FTAG) : spa_close(spa, FTAG);
3748 
3749 	fuid_table_destroy();
3750 	sa_loaded = B_FALSE;
3751 
3752 	libzfs_fini(g_zfs);
3753 	kernel_fini();
3754 
3755 	return (0);
3756 }
3757