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