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.
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.
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]
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 * Copyright 2016 Nexenta Systems, Inc.
27 * Copyright (c) 2017 Lawrence Livermore National Security, LLC.
28 * Copyright (c) 2015, 2017, Intel Corporation.
33 #include <stdio_ext.h>
36 #include <sys/zfs_context.h>
38 #include <sys/spa_impl.h>
41 #include <sys/fs/zfs.h>
42 #include <sys/zfs_znode.h>
43 #include <sys/zfs_sa.h>
45 #include <sys/sa_impl.h>
47 #include <sys/vdev_impl.h>
48 #include <sys/metaslab_impl.h>
49 #include <sys/dmu_objset.h>
50 #include <sys/dsl_dir.h>
51 #include <sys/dsl_dataset.h>
52 #include <sys/dsl_pool.h>
55 #include <sys/zil_impl.h>
57 #include <sys/resource.h>
58 #include <sys/dmu_traverse.h>
59 #include <sys/zio_checksum.h>
60 #include <sys/zio_compress.h>
61 #include <sys/zfs_fuid.h>
64 #include <sys/zfeature.h>
66 #include <sys/blkptr.h>
67 #include <sys/dsl_crypt.h>
68 #include <zfs_comutil.h>
73 #define ZDB_COMPRESS_NAME(idx) ((idx) < ZIO_COMPRESS_FUNCTIONS ? \
74 zio_compress_table[(idx)].ci_name : "UNKNOWN")
75 #define ZDB_CHECKSUM_NAME(idx) ((idx) < ZIO_CHECKSUM_FUNCTIONS ? \
76 zio_checksum_table[(idx)].ci_name : "UNKNOWN")
77 #define ZDB_OT_TYPE(idx) ((idx) < DMU_OT_NUMTYPES ? (idx) : \
78 (((idx) == DMU_OTN_ZAP_DATA || (idx) == DMU_OTN_ZAP_METADATA) ? \
79 DMU_OT_ZAP_OTHER : DMU_OT_NUMTYPES))
82 zdb_ot_name(dmu_object_type_t type)
84 if (type < DMU_OT_NUMTYPES)
85 return (dmu_ot[type].ot_name);
86 else if ((type & DMU_OT_NEWTYPE) &&
87 ((type & DMU_OT_BYTESWAP_MASK) < DMU_BSWAP_NUMFUNCS))
88 return (dmu_ot_byteswap[type & DMU_OT_BYTESWAP_MASK].ob_name);
93 extern int reference_tracking_enable;
94 extern int zfs_recover;
95 extern uint64_t zfs_arc_max, zfs_arc_meta_limit;
96 extern int zfs_vdev_async_read_max_active;
98 static const char cmdname[] = "zdb";
99 uint8_t dump_opt[256];
101 typedef void object_viewer_t(objset_t *, uint64_t, void *data, size_t size);
103 uint64_t *zopt_object = NULL;
104 static unsigned zopt_objects = 0;
105 libzfs_handle_t *g_zfs;
106 uint64_t max_inflight = 1000;
108 static void snprintf_blkptr_compact(char *, size_t, const blkptr_t *);
111 * These libumem hooks provide a reasonable set of defaults for the allocator's
112 * debugging facilities.
115 _umem_debug_init(void)
117 return ("default,verbose"); /* $UMEM_DEBUG setting */
121 _umem_logging_init(void)
123 return ("fail,contents"); /* $UMEM_LOGGING setting */
129 (void) fprintf(stderr,
130 "Usage:\t%s [-AbcdDFGhiLMPsvX] [-e [-V] [-p <path> ...]] "
131 "[-I <inflight I/Os>]\n"
132 "\t\t[-o <var>=<value>]... [-t <txg>] [-U <cache>] [-x <dumpdir>]\n"
133 "\t\t[<poolname> [<object> ...]]\n"
134 "\t%s [-AdiPv] [-e [-V] [-p <path> ...]] [-U <cache>] <dataset>\n"
135 "\t\t[<object> ...]\n"
136 "\t%s -C [-A] [-U <cache>]\n"
137 "\t%s -l [-Aqu] <device>\n"
138 "\t%s -m [-AFLPX] [-e [-V] [-p <path> ...]] [-t <txg>] "
139 "[-U <cache>]\n\t\t<poolname> [<vdev> [<metaslab> ...]]\n"
140 "\t%s -O <dataset> <path>\n"
141 "\t%s -R [-A] [-e [-V] [-p <path> ...]] [-U <cache>]\n"
142 "\t\t<poolname> <vdev>:<offset>:<size>[:<flags>]\n"
143 "\t%s -E [-A] word0:word1:...:word15\n"
144 "\t%s -S [-AP] [-e [-V] [-p <path> ...]] [-U <cache>] "
146 cmdname, cmdname, cmdname, cmdname, cmdname, cmdname, cmdname,
149 (void) fprintf(stderr, " Dataset name must include at least one "
150 "separator character '/' or '@'\n");
151 (void) fprintf(stderr, " If dataset name is specified, only that "
152 "dataset is dumped\n");
153 (void) fprintf(stderr, " If object numbers are specified, only "
154 "those objects are dumped\n\n");
155 (void) fprintf(stderr, " Options to control amount of output:\n");
156 (void) fprintf(stderr, " -b block statistics\n");
157 (void) fprintf(stderr, " -c checksum all metadata (twice for "
158 "all data) blocks\n");
159 (void) fprintf(stderr, " -C config (or cachefile if alone)\n");
160 (void) fprintf(stderr, " -d dataset(s)\n");
161 (void) fprintf(stderr, " -D dedup statistics\n");
162 (void) fprintf(stderr, " -E decode and display block from an "
163 "embedded block pointer\n");
164 (void) fprintf(stderr, " -h pool history\n");
165 (void) fprintf(stderr, " -i intent logs\n");
166 (void) fprintf(stderr, " -l read label contents\n");
167 (void) fprintf(stderr, " -L disable leak tracking (do not "
168 "load spacemaps)\n");
169 (void) fprintf(stderr, " -m metaslabs\n");
170 (void) fprintf(stderr, " -M metaslab groups\n");
171 (void) fprintf(stderr, " -O perform object lookups by path\n");
172 (void) fprintf(stderr, " -R read and display block from a "
174 (void) fprintf(stderr, " -s report stats on zdb's I/O\n");
175 (void) fprintf(stderr, " -S simulate dedup to measure effect\n");
176 (void) fprintf(stderr, " -v verbose (applies to all "
178 (void) fprintf(stderr, " Below options are intended for use "
179 "with other options:\n");
180 (void) fprintf(stderr, " -A ignore assertions (-A), enable "
181 "panic recovery (-AA) or both (-AAA)\n");
182 (void) fprintf(stderr, " -e pool is exported/destroyed/"
183 "has altroot/not in a cachefile\n");
184 (void) fprintf(stderr, " -F attempt automatic rewind within "
185 "safe range of transaction groups\n");
186 (void) fprintf(stderr, " -G dump zfs_dbgmsg buffer before "
188 (void) fprintf(stderr, " -I <number of inflight I/Os> -- "
189 "specify the maximum number of\n "
190 "checksumming I/Os [default is 200]\n");
191 (void) fprintf(stderr, " -o <variable>=<value> set global "
192 "variable to an unsigned 32-bit integer\n");
193 (void) fprintf(stderr, " -p <path> -- use one or more with "
194 "-e to specify path to vdev dir\n");
195 (void) fprintf(stderr, " -P print numbers in parseable form\n");
196 (void) fprintf(stderr, " -q don't print label contents\n");
197 (void) fprintf(stderr, " -t <txg> -- highest txg to use when "
198 "searching for uberblocks\n");
199 (void) fprintf(stderr, " -u uberblock\n");
200 (void) fprintf(stderr, " -U <cachefile_path> -- use alternate "
202 (void) fprintf(stderr, " -V do verbatim import\n");
203 (void) fprintf(stderr, " -x <dumpdir> -- "
204 "dump all read blocks into specified directory\n");
205 (void) fprintf(stderr, " -X attempt extreme rewind (does not "
206 "work with dataset)\n");
207 (void) fprintf(stderr, "Specify an option more than once (e.g. -bb) "
208 "to make only that option verbose\n");
209 (void) fprintf(stderr, "Default is to dump everything non-verbosely\n");
214 dump_debug_buffer(void)
218 zfs_dbgmsg_print("zdb");
223 * Called for usage errors that are discovered after a call to spa_open(),
224 * dmu_bonus_hold(), or pool_match(). abort() is called for other errors.
228 fatal(const char *fmt, ...)
233 (void) fprintf(stderr, "%s: ", cmdname);
234 (void) vfprintf(stderr, fmt, ap);
236 (void) fprintf(stderr, "\n");
245 dump_packed_nvlist(objset_t *os, uint64_t object, void *data, size_t size)
248 size_t nvsize = *(uint64_t *)data;
249 char *packed = umem_alloc(nvsize, UMEM_NOFAIL);
251 VERIFY(0 == dmu_read(os, object, 0, nvsize, packed, DMU_READ_PREFETCH));
253 VERIFY(nvlist_unpack(packed, nvsize, &nv, 0) == 0);
255 umem_free(packed, nvsize);
264 dump_history_offsets(objset_t *os, uint64_t object, void *data, size_t size)
266 spa_history_phys_t *shp = data;
271 (void) printf("\t\tpool_create_len = %llu\n",
272 (u_longlong_t)shp->sh_pool_create_len);
273 (void) printf("\t\tphys_max_off = %llu\n",
274 (u_longlong_t)shp->sh_phys_max_off);
275 (void) printf("\t\tbof = %llu\n",
276 (u_longlong_t)shp->sh_bof);
277 (void) printf("\t\teof = %llu\n",
278 (u_longlong_t)shp->sh_eof);
279 (void) printf("\t\trecords_lost = %llu\n",
280 (u_longlong_t)shp->sh_records_lost);
284 zdb_nicenum(uint64_t num, char *buf, size_t buflen)
287 (void) snprintf(buf, buflen, "%llu", (longlong_t)num);
289 nicenum(num, buf, sizeof (buf));
292 static const char histo_stars[] = "****************************************";
293 static const uint64_t histo_width = sizeof (histo_stars) - 1;
296 dump_histogram(const uint64_t *histo, int size, int offset)
299 int minidx = size - 1;
303 for (i = 0; i < size; i++) {
306 if (histo[i] > 0 && i > maxidx)
308 if (histo[i] > 0 && i < minidx)
312 if (max < histo_width)
315 for (i = minidx; i <= maxidx; i++) {
316 (void) printf("\t\t\t%3u: %6llu %s\n",
317 i + offset, (u_longlong_t)histo[i],
318 &histo_stars[(max - histo[i]) * histo_width / max]);
323 dump_zap_stats(objset_t *os, uint64_t object)
328 error = zap_get_stats(os, object, &zs);
332 if (zs.zs_ptrtbl_len == 0) {
333 ASSERT(zs.zs_num_blocks == 1);
334 (void) printf("\tmicrozap: %llu bytes, %llu entries\n",
335 (u_longlong_t)zs.zs_blocksize,
336 (u_longlong_t)zs.zs_num_entries);
340 (void) printf("\tFat ZAP stats:\n");
342 (void) printf("\t\tPointer table:\n");
343 (void) printf("\t\t\t%llu elements\n",
344 (u_longlong_t)zs.zs_ptrtbl_len);
345 (void) printf("\t\t\tzt_blk: %llu\n",
346 (u_longlong_t)zs.zs_ptrtbl_zt_blk);
347 (void) printf("\t\t\tzt_numblks: %llu\n",
348 (u_longlong_t)zs.zs_ptrtbl_zt_numblks);
349 (void) printf("\t\t\tzt_shift: %llu\n",
350 (u_longlong_t)zs.zs_ptrtbl_zt_shift);
351 (void) printf("\t\t\tzt_blks_copied: %llu\n",
352 (u_longlong_t)zs.zs_ptrtbl_blks_copied);
353 (void) printf("\t\t\tzt_nextblk: %llu\n",
354 (u_longlong_t)zs.zs_ptrtbl_nextblk);
356 (void) printf("\t\tZAP entries: %llu\n",
357 (u_longlong_t)zs.zs_num_entries);
358 (void) printf("\t\tLeaf blocks: %llu\n",
359 (u_longlong_t)zs.zs_num_leafs);
360 (void) printf("\t\tTotal blocks: %llu\n",
361 (u_longlong_t)zs.zs_num_blocks);
362 (void) printf("\t\tzap_block_type: 0x%llx\n",
363 (u_longlong_t)zs.zs_block_type);
364 (void) printf("\t\tzap_magic: 0x%llx\n",
365 (u_longlong_t)zs.zs_magic);
366 (void) printf("\t\tzap_salt: 0x%llx\n",
367 (u_longlong_t)zs.zs_salt);
369 (void) printf("\t\tLeafs with 2^n pointers:\n");
370 dump_histogram(zs.zs_leafs_with_2n_pointers, ZAP_HISTOGRAM_SIZE, 0);
372 (void) printf("\t\tBlocks with n*5 entries:\n");
373 dump_histogram(zs.zs_blocks_with_n5_entries, ZAP_HISTOGRAM_SIZE, 0);
375 (void) printf("\t\tBlocks n/10 full:\n");
376 dump_histogram(zs.zs_blocks_n_tenths_full, ZAP_HISTOGRAM_SIZE, 0);
378 (void) printf("\t\tEntries with n chunks:\n");
379 dump_histogram(zs.zs_entries_using_n_chunks, ZAP_HISTOGRAM_SIZE, 0);
381 (void) printf("\t\tBuckets with n entries:\n");
382 dump_histogram(zs.zs_buckets_with_n_entries, ZAP_HISTOGRAM_SIZE, 0);
387 dump_none(objset_t *os, uint64_t object, void *data, size_t size)
393 dump_unknown(objset_t *os, uint64_t object, void *data, size_t size)
395 (void) printf("\tUNKNOWN OBJECT TYPE\n");
400 dump_uint8(objset_t *os, uint64_t object, void *data, size_t size)
406 dump_uint64(objset_t *os, uint64_t object, void *data, size_t size)
412 dump_zap(objset_t *os, uint64_t object, void *data, size_t size)
415 zap_attribute_t attr;
419 dump_zap_stats(os, object);
422 for (zap_cursor_init(&zc, os, object);
423 zap_cursor_retrieve(&zc, &attr) == 0;
424 zap_cursor_advance(&zc)) {
425 (void) printf("\t\t%s = ", attr.za_name);
426 if (attr.za_num_integers == 0) {
430 prop = umem_zalloc(attr.za_num_integers *
431 attr.za_integer_length, UMEM_NOFAIL);
432 (void) zap_lookup(os, object, attr.za_name,
433 attr.za_integer_length, attr.za_num_integers, prop);
434 if (attr.za_integer_length == 1) {
435 (void) printf("%s", (char *)prop);
437 for (i = 0; i < attr.za_num_integers; i++) {
438 switch (attr.za_integer_length) {
441 ((uint16_t *)prop)[i]);
445 ((uint32_t *)prop)[i]);
448 (void) printf("%lld ",
449 (u_longlong_t)((int64_t *)prop)[i]);
455 umem_free(prop, attr.za_num_integers * attr.za_integer_length);
457 zap_cursor_fini(&zc);
461 dump_bpobj(objset_t *os, uint64_t object, void *data, size_t size)
463 bpobj_phys_t *bpop = data;
465 char bytes[32], comp[32], uncomp[32];
467 /* make sure the output won't get truncated */
468 CTASSERT(sizeof (bytes) >= NN_NUMBUF_SZ);
469 CTASSERT(sizeof (comp) >= NN_NUMBUF_SZ);
470 CTASSERT(sizeof (uncomp) >= NN_NUMBUF_SZ);
475 zdb_nicenum(bpop->bpo_bytes, bytes, sizeof (bytes));
476 zdb_nicenum(bpop->bpo_comp, comp, sizeof (comp));
477 zdb_nicenum(bpop->bpo_uncomp, uncomp, sizeof (uncomp));
479 (void) printf("\t\tnum_blkptrs = %llu\n",
480 (u_longlong_t)bpop->bpo_num_blkptrs);
481 (void) printf("\t\tbytes = %s\n", bytes);
482 if (size >= BPOBJ_SIZE_V1) {
483 (void) printf("\t\tcomp = %s\n", comp);
484 (void) printf("\t\tuncomp = %s\n", uncomp);
486 if (size >= sizeof (*bpop)) {
487 (void) printf("\t\tsubobjs = %llu\n",
488 (u_longlong_t)bpop->bpo_subobjs);
489 (void) printf("\t\tnum_subobjs = %llu\n",
490 (u_longlong_t)bpop->bpo_num_subobjs);
493 if (dump_opt['d'] < 5)
496 for (i = 0; i < bpop->bpo_num_blkptrs; i++) {
497 char blkbuf[BP_SPRINTF_LEN];
500 int err = dmu_read(os, object,
501 i * sizeof (bp), sizeof (bp), &bp, 0);
503 (void) printf("got error %u from dmu_read\n", err);
506 snprintf_blkptr_compact(blkbuf, sizeof (blkbuf), &bp);
507 (void) printf("\t%s\n", blkbuf);
513 dump_bpobj_subobjs(objset_t *os, uint64_t object, void *data, size_t size)
515 dmu_object_info_t doi;
518 VERIFY0(dmu_object_info(os, object, &doi));
519 uint64_t *subobjs = kmem_alloc(doi.doi_max_offset, KM_SLEEP);
521 int err = dmu_read(os, object, 0, doi.doi_max_offset, subobjs, 0);
523 (void) printf("got error %u from dmu_read\n", err);
524 kmem_free(subobjs, doi.doi_max_offset);
528 int64_t last_nonzero = -1;
529 for (i = 0; i < doi.doi_max_offset / 8; i++) {
534 for (i = 0; i <= last_nonzero; i++) {
535 (void) printf("\t%llu\n", (u_longlong_t)subobjs[i]);
537 kmem_free(subobjs, doi.doi_max_offset);
542 dump_ddt_zap(objset_t *os, uint64_t object, void *data, size_t size)
544 dump_zap_stats(os, object);
545 /* contents are printed elsewhere, properly decoded */
550 dump_sa_attrs(objset_t *os, uint64_t object, void *data, size_t size)
553 zap_attribute_t attr;
555 dump_zap_stats(os, object);
558 for (zap_cursor_init(&zc, os, object);
559 zap_cursor_retrieve(&zc, &attr) == 0;
560 zap_cursor_advance(&zc)) {
561 (void) printf("\t\t%s = ", attr.za_name);
562 if (attr.za_num_integers == 0) {
566 (void) printf(" %llx : [%d:%d:%d]\n",
567 (u_longlong_t)attr.za_first_integer,
568 (int)ATTR_LENGTH(attr.za_first_integer),
569 (int)ATTR_BSWAP(attr.za_first_integer),
570 (int)ATTR_NUM(attr.za_first_integer));
572 zap_cursor_fini(&zc);
577 dump_sa_layouts(objset_t *os, uint64_t object, void *data, size_t size)
580 zap_attribute_t attr;
581 uint16_t *layout_attrs;
584 dump_zap_stats(os, object);
587 for (zap_cursor_init(&zc, os, object);
588 zap_cursor_retrieve(&zc, &attr) == 0;
589 zap_cursor_advance(&zc)) {
590 (void) printf("\t\t%s = [", attr.za_name);
591 if (attr.za_num_integers == 0) {
596 VERIFY(attr.za_integer_length == 2);
597 layout_attrs = umem_zalloc(attr.za_num_integers *
598 attr.za_integer_length, UMEM_NOFAIL);
600 VERIFY(zap_lookup(os, object, attr.za_name,
601 attr.za_integer_length,
602 attr.za_num_integers, layout_attrs) == 0);
604 for (i = 0; i != attr.za_num_integers; i++)
605 (void) printf(" %d ", (int)layout_attrs[i]);
606 (void) printf("]\n");
607 umem_free(layout_attrs,
608 attr.za_num_integers * attr.za_integer_length);
610 zap_cursor_fini(&zc);
615 dump_zpldir(objset_t *os, uint64_t object, void *data, size_t size)
618 zap_attribute_t attr;
619 const char *typenames[] = {
620 /* 0 */ "not specified",
622 /* 2 */ "Character Device",
623 /* 3 */ "3 (invalid)",
625 /* 5 */ "5 (invalid)",
626 /* 6 */ "Block Device",
627 /* 7 */ "7 (invalid)",
628 /* 8 */ "Regular File",
629 /* 9 */ "9 (invalid)",
630 /* 10 */ "Symbolic Link",
631 /* 11 */ "11 (invalid)",
634 /* 14 */ "Event Port",
635 /* 15 */ "15 (invalid)",
638 dump_zap_stats(os, object);
641 for (zap_cursor_init(&zc, os, object);
642 zap_cursor_retrieve(&zc, &attr) == 0;
643 zap_cursor_advance(&zc)) {
644 (void) printf("\t\t%s = %lld (type: %s)\n",
645 attr.za_name, ZFS_DIRENT_OBJ(attr.za_first_integer),
646 typenames[ZFS_DIRENT_TYPE(attr.za_first_integer)]);
648 zap_cursor_fini(&zc);
652 get_dtl_refcount(vdev_t *vd)
656 if (vd->vdev_ops->vdev_op_leaf) {
657 space_map_t *sm = vd->vdev_dtl_sm;
660 sm->sm_dbuf->db_size == sizeof (space_map_phys_t))
665 for (unsigned c = 0; c < vd->vdev_children; c++)
666 refcount += get_dtl_refcount(vd->vdev_child[c]);
671 get_metaslab_refcount(vdev_t *vd)
675 if (vd->vdev_top == vd && !vd->vdev_removing) {
676 for (unsigned m = 0; m < vd->vdev_ms_count; m++) {
677 space_map_t *sm = vd->vdev_ms[m]->ms_sm;
680 sm->sm_dbuf->db_size == sizeof (space_map_phys_t))
684 for (unsigned c = 0; c < vd->vdev_children; c++)
685 refcount += get_metaslab_refcount(vd->vdev_child[c]);
691 verify_spacemap_refcounts(spa_t *spa)
693 uint64_t expected_refcount = 0;
694 uint64_t actual_refcount;
696 (void) feature_get_refcount(spa,
697 &spa_feature_table[SPA_FEATURE_SPACEMAP_HISTOGRAM],
699 actual_refcount = get_dtl_refcount(spa->spa_root_vdev);
700 actual_refcount += get_metaslab_refcount(spa->spa_root_vdev);
702 if (expected_refcount != actual_refcount) {
703 (void) printf("space map refcount mismatch: expected %lld != "
705 (longlong_t)expected_refcount,
706 (longlong_t)actual_refcount);
713 dump_spacemap(objset_t *os, space_map_t *sm)
715 uint64_t alloc, offset, entry;
716 const char *ddata[] = { "ALLOC", "FREE", "CONDENSE", "INVALID",
717 "INVALID", "INVALID", "INVALID", "INVALID" };
723 * Print out the freelist entries in both encoded and decoded form.
726 for (offset = 0; offset < space_map_length(sm);
727 offset += sizeof (entry)) {
728 uint8_t mapshift = sm->sm_shift;
730 VERIFY0(dmu_read(os, space_map_object(sm), offset,
731 sizeof (entry), &entry, DMU_READ_PREFETCH));
732 if (SM_DEBUG_DECODE(entry)) {
734 (void) printf("\t [%6llu] %s: txg %llu, pass %llu\n",
735 (u_longlong_t)(offset / sizeof (entry)),
736 ddata[SM_DEBUG_ACTION_DECODE(entry)],
737 (u_longlong_t)SM_DEBUG_TXG_DECODE(entry),
738 (u_longlong_t)SM_DEBUG_SYNCPASS_DECODE(entry));
740 (void) printf("\t [%6llu] %c range:"
741 " %010llx-%010llx size: %06llx\n",
742 (u_longlong_t)(offset / sizeof (entry)),
743 SM_TYPE_DECODE(entry) == SM_ALLOC ? 'A' : 'F',
744 (u_longlong_t)((SM_OFFSET_DECODE(entry) <<
745 mapshift) + sm->sm_start),
746 (u_longlong_t)((SM_OFFSET_DECODE(entry) <<
747 mapshift) + sm->sm_start +
748 (SM_RUN_DECODE(entry) << mapshift)),
749 (u_longlong_t)(SM_RUN_DECODE(entry) << mapshift));
750 if (SM_TYPE_DECODE(entry) == SM_ALLOC)
751 alloc += SM_RUN_DECODE(entry) << mapshift;
753 alloc -= SM_RUN_DECODE(entry) << mapshift;
756 if (alloc != space_map_allocated(sm)) {
757 (void) printf("space_map_object alloc (%llu) INCONSISTENT "
758 "with space map summary (%llu)\n",
759 (u_longlong_t)space_map_allocated(sm), (u_longlong_t)alloc);
764 dump_metaslab_stats(metaslab_t *msp)
767 range_tree_t *rt = msp->ms_tree;
768 avl_tree_t *t = &msp->ms_size_tree;
769 int free_pct = range_tree_space(rt) * 100 / msp->ms_size;
771 /* max sure nicenum has enough space */
772 CTASSERT(sizeof (maxbuf) >= NN_NUMBUF_SZ);
774 zdb_nicenum(metaslab_block_maxsize(msp), maxbuf, sizeof (maxbuf));
776 (void) printf("\t %25s %10lu %7s %6s %4s %4d%%\n",
777 "segments", avl_numnodes(t), "maxsize", maxbuf,
778 "freepct", free_pct);
779 (void) printf("\tIn-memory histogram:\n");
780 dump_histogram(rt->rt_histogram, RANGE_TREE_HISTOGRAM_SIZE, 0);
784 dump_metaslab(metaslab_t *msp)
786 vdev_t *vd = msp->ms_group->mg_vd;
787 spa_t *spa = vd->vdev_spa;
788 space_map_t *sm = msp->ms_sm;
791 zdb_nicenum(msp->ms_size - space_map_allocated(sm), freebuf,
795 "\tmetaslab %6llu offset %12llx spacemap %6llu free %5s\n",
796 (u_longlong_t)msp->ms_id, (u_longlong_t)msp->ms_start,
797 (u_longlong_t)space_map_object(sm), freebuf);
799 if (dump_opt['m'] > 2 && !dump_opt['L']) {
800 mutex_enter(&msp->ms_lock);
801 metaslab_load_wait(msp);
802 if (!msp->ms_loaded) {
803 VERIFY0(metaslab_load(msp));
804 range_tree_stat_verify(msp->ms_tree);
806 dump_metaslab_stats(msp);
807 metaslab_unload(msp);
808 mutex_exit(&msp->ms_lock);
811 if (dump_opt['m'] > 1 && sm != NULL &&
812 spa_feature_is_active(spa, SPA_FEATURE_SPACEMAP_HISTOGRAM)) {
814 * The space map histogram represents free space in chunks
815 * of sm_shift (i.e. bucket 0 refers to 2^sm_shift).
817 (void) printf("\tOn-disk histogram:\t\tfragmentation %llu\n",
818 (u_longlong_t)msp->ms_fragmentation);
819 dump_histogram(sm->sm_phys->smp_histogram,
820 SPACE_MAP_HISTOGRAM_SIZE, sm->sm_shift);
823 if (dump_opt['d'] > 5 || dump_opt['m'] > 3) {
824 ASSERT(msp->ms_size == (1ULL << vd->vdev_ms_shift));
826 mutex_enter(&msp->ms_lock);
827 dump_spacemap(spa->spa_meta_objset, msp->ms_sm);
828 mutex_exit(&msp->ms_lock);
833 print_vdev_metaslab_header(vdev_t *vd)
835 (void) printf("\tvdev %10llu\n\t%-10s%5llu %-19s %-15s %-10s\n",
836 (u_longlong_t)vd->vdev_id,
837 "metaslabs", (u_longlong_t)vd->vdev_ms_count,
838 "offset", "spacemap", "free");
839 (void) printf("\t%15s %19s %15s %10s\n",
840 "---------------", "-------------------",
841 "---------------", "-------------");
845 dump_metaslab_groups(spa_t *spa)
847 vdev_t *rvd = spa->spa_root_vdev;
848 metaslab_class_t *mc = spa_normal_class(spa);
849 uint64_t fragmentation;
851 metaslab_class_histogram_verify(mc);
853 for (unsigned c = 0; c < rvd->vdev_children; c++) {
854 vdev_t *tvd = rvd->vdev_child[c];
855 metaslab_group_t *mg = tvd->vdev_mg;
857 if (mg->mg_class != mc)
860 metaslab_group_histogram_verify(mg);
861 mg->mg_fragmentation = metaslab_group_fragmentation(mg);
863 (void) printf("\tvdev %10llu\t\tmetaslabs%5llu\t\t"
865 (u_longlong_t)tvd->vdev_id,
866 (u_longlong_t)tvd->vdev_ms_count);
867 if (mg->mg_fragmentation == ZFS_FRAG_INVALID) {
868 (void) printf("%3s\n", "-");
870 (void) printf("%3llu%%\n",
871 (u_longlong_t)mg->mg_fragmentation);
873 dump_histogram(mg->mg_histogram, RANGE_TREE_HISTOGRAM_SIZE, 0);
876 (void) printf("\tpool %s\tfragmentation", spa_name(spa));
877 fragmentation = metaslab_class_fragmentation(mc);
878 if (fragmentation == ZFS_FRAG_INVALID)
879 (void) printf("\t%3s\n", "-");
881 (void) printf("\t%3llu%%\n", (u_longlong_t)fragmentation);
882 dump_histogram(mc->mc_histogram, RANGE_TREE_HISTOGRAM_SIZE, 0);
886 dump_metaslabs(spa_t *spa)
888 vdev_t *vd, *rvd = spa->spa_root_vdev;
889 uint64_t m, c = 0, children = rvd->vdev_children;
891 (void) printf("\nMetaslabs:\n");
893 if (!dump_opt['d'] && zopt_objects > 0) {
897 (void) fatal("bad vdev id: %llu", (u_longlong_t)c);
899 if (zopt_objects > 1) {
900 vd = rvd->vdev_child[c];
901 print_vdev_metaslab_header(vd);
903 for (m = 1; m < zopt_objects; m++) {
904 if (zopt_object[m] < vd->vdev_ms_count)
906 vd->vdev_ms[zopt_object[m]]);
908 (void) fprintf(stderr, "bad metaslab "
910 (u_longlong_t)zopt_object[m]);
917 for (; c < children; c++) {
918 vd = rvd->vdev_child[c];
919 print_vdev_metaslab_header(vd);
921 for (m = 0; m < vd->vdev_ms_count; m++)
922 dump_metaslab(vd->vdev_ms[m]);
928 dump_dde(const ddt_t *ddt, const ddt_entry_t *dde, uint64_t index)
930 const ddt_phys_t *ddp = dde->dde_phys;
931 const ddt_key_t *ddk = &dde->dde_key;
932 const char *types[4] = { "ditto", "single", "double", "triple" };
933 char blkbuf[BP_SPRINTF_LEN];
937 for (p = 0; p < DDT_PHYS_TYPES; p++, ddp++) {
938 if (ddp->ddp_phys_birth == 0)
940 ddt_bp_create(ddt->ddt_checksum, ddk, ddp, &blk);
941 snprintf_blkptr(blkbuf, sizeof (blkbuf), &blk);
942 (void) printf("index %llx refcnt %llu %s %s\n",
943 (u_longlong_t)index, (u_longlong_t)ddp->ddp_refcnt,
949 dump_dedup_ratio(const ddt_stat_t *dds)
951 double rL, rP, rD, D, dedup, compress, copies;
953 if (dds->dds_blocks == 0)
956 rL = (double)dds->dds_ref_lsize;
957 rP = (double)dds->dds_ref_psize;
958 rD = (double)dds->dds_ref_dsize;
959 D = (double)dds->dds_dsize;
965 (void) printf("dedup = %.2f, compress = %.2f, copies = %.2f, "
966 "dedup * compress / copies = %.2f\n\n",
967 dedup, compress, copies, dedup * compress / copies);
971 dump_ddt(ddt_t *ddt, enum ddt_type type, enum ddt_class class)
973 char name[DDT_NAMELEN];
976 dmu_object_info_t doi;
977 uint64_t count, dspace, mspace;
980 error = ddt_object_info(ddt, type, class, &doi);
986 error = ddt_object_count(ddt, type, class, &count);
991 dspace = doi.doi_physical_blocks_512 << 9;
992 mspace = doi.doi_fill_count * doi.doi_data_block_size;
994 ddt_object_name(ddt, type, class, name);
996 (void) printf("%s: %llu entries, size %llu on disk, %llu in core\n",
999 (u_longlong_t)(dspace / count),
1000 (u_longlong_t)(mspace / count));
1002 if (dump_opt['D'] < 3)
1005 zpool_dump_ddt(NULL, &ddt->ddt_histogram[type][class]);
1007 if (dump_opt['D'] < 4)
1010 if (dump_opt['D'] < 5 && class == DDT_CLASS_UNIQUE)
1013 (void) printf("%s contents:\n\n", name);
1015 while ((error = ddt_object_walk(ddt, type, class, &walk, &dde)) == 0)
1016 dump_dde(ddt, &dde, walk);
1018 ASSERT(error == ENOENT);
1020 (void) printf("\n");
1024 dump_all_ddts(spa_t *spa)
1026 ddt_histogram_t ddh_total;
1027 ddt_stat_t dds_total;
1029 bzero(&ddh_total, sizeof (ddh_total));
1030 bzero(&dds_total, sizeof (dds_total));
1032 for (enum zio_checksum c = 0; c < ZIO_CHECKSUM_FUNCTIONS; c++) {
1033 ddt_t *ddt = spa->spa_ddt[c];
1034 for (enum ddt_type type = 0; type < DDT_TYPES; type++) {
1035 for (enum ddt_class class = 0; class < DDT_CLASSES;
1037 dump_ddt(ddt, type, class);
1042 ddt_get_dedup_stats(spa, &dds_total);
1044 if (dds_total.dds_blocks == 0) {
1045 (void) printf("All DDTs are empty\n");
1049 (void) printf("\n");
1051 if (dump_opt['D'] > 1) {
1052 (void) printf("DDT histogram (aggregated over all DDTs):\n");
1053 ddt_get_dedup_histogram(spa, &ddh_total);
1054 zpool_dump_ddt(&dds_total, &ddh_total);
1057 dump_dedup_ratio(&dds_total);
1061 dump_dtl_seg(void *arg, uint64_t start, uint64_t size)
1065 (void) printf("%s [%llu,%llu) length %llu\n",
1067 (u_longlong_t)start,
1068 (u_longlong_t)(start + size),
1069 (u_longlong_t)(size));
1073 dump_dtl(vdev_t *vd, int indent)
1075 spa_t *spa = vd->vdev_spa;
1077 const char *name[DTL_TYPES] = { "missing", "partial", "scrub",
1081 spa_vdev_state_enter(spa, SCL_NONE);
1082 required = vdev_dtl_required(vd);
1083 (void) spa_vdev_state_exit(spa, NULL, 0);
1086 (void) printf("\nDirty time logs:\n\n");
1088 (void) printf("\t%*s%s [%s]\n", indent, "",
1089 vd->vdev_path ? vd->vdev_path :
1090 vd->vdev_parent ? vd->vdev_ops->vdev_op_type : spa_name(spa),
1091 required ? "DTL-required" : "DTL-expendable");
1093 for (int t = 0; t < DTL_TYPES; t++) {
1094 range_tree_t *rt = vd->vdev_dtl[t];
1095 if (range_tree_space(rt) == 0)
1097 (void) snprintf(prefix, sizeof (prefix), "\t%*s%s",
1098 indent + 2, "", name[t]);
1099 mutex_enter(rt->rt_lock);
1100 range_tree_walk(rt, dump_dtl_seg, prefix);
1101 mutex_exit(rt->rt_lock);
1102 if (dump_opt['d'] > 5 && vd->vdev_children == 0)
1103 dump_spacemap(spa->spa_meta_objset,
1107 for (unsigned c = 0; c < vd->vdev_children; c++)
1108 dump_dtl(vd->vdev_child[c], indent + 4);
1112 dump_history(spa_t *spa)
1114 nvlist_t **events = NULL;
1116 uint64_t resid, len, off = 0;
1122 char internalstr[MAXPATHLEN];
1124 if ((buf = malloc(SPA_OLD_MAXBLOCKSIZE)) == NULL) {
1125 (void) fprintf(stderr, "%s: unable to allocate I/O buffer\n",
1131 len = SPA_OLD_MAXBLOCKSIZE;
1133 if ((error = spa_history_get(spa, &off, &len, buf)) != 0) {
1134 (void) fprintf(stderr, "Unable to read history: "
1135 "error %d\n", error);
1140 if (zpool_history_unpack(buf, len, &resid, &events, &num) != 0)
1146 (void) printf("\nHistory:\n");
1147 for (unsigned i = 0; i < num; i++) {
1148 uint64_t time, txg, ievent;
1150 boolean_t printed = B_FALSE;
1152 if (nvlist_lookup_uint64(events[i], ZPOOL_HIST_TIME,
1155 if (nvlist_lookup_string(events[i], ZPOOL_HIST_CMD,
1157 if (nvlist_lookup_uint64(events[i],
1158 ZPOOL_HIST_INT_EVENT, &ievent) != 0)
1160 verify(nvlist_lookup_uint64(events[i],
1161 ZPOOL_HIST_TXG, &txg) == 0);
1162 verify(nvlist_lookup_string(events[i],
1163 ZPOOL_HIST_INT_STR, &intstr) == 0);
1164 if (ievent >= ZFS_NUM_LEGACY_HISTORY_EVENTS)
1167 (void) snprintf(internalstr,
1168 sizeof (internalstr),
1169 "[internal %s txg:%lld] %s",
1170 zfs_history_event_names[ievent],
1171 (longlong_t)txg, intstr);
1175 (void) localtime_r(&tsec, &t);
1176 (void) strftime(tbuf, sizeof (tbuf), "%F.%T", &t);
1177 (void) printf("%s %s\n", tbuf, cmd);
1181 if (dump_opt['h'] > 1) {
1183 (void) printf("unrecognized record:\n");
1184 dump_nvlist(events[i], 2);
1192 dump_dnode(objset_t *os, uint64_t object, void *data, size_t size)
1197 blkid2offset(const dnode_phys_t *dnp, const blkptr_t *bp,
1198 const zbookmark_phys_t *zb)
1201 ASSERT(zb->zb_level < 0);
1202 if (zb->zb_object == 0)
1203 return (zb->zb_blkid);
1204 return (zb->zb_blkid * BP_GET_LSIZE(bp));
1207 ASSERT(zb->zb_level >= 0);
1209 return ((zb->zb_blkid <<
1210 (zb->zb_level * (dnp->dn_indblkshift - SPA_BLKPTRSHIFT))) *
1211 dnp->dn_datablkszsec << SPA_MINBLOCKSHIFT);
1215 snprintf_blkptr_compact(char *blkbuf, size_t buflen, const blkptr_t *bp)
1217 const dva_t *dva = bp->blk_dva;
1218 int ndvas = dump_opt['d'] > 5 ? BP_GET_NDVAS(bp) : 1;
1221 if (dump_opt['b'] >= 6) {
1222 snprintf_blkptr(blkbuf, buflen, bp);
1226 if (BP_IS_EMBEDDED(bp)) {
1227 (void) sprintf(blkbuf,
1228 "EMBEDDED et=%u %llxL/%llxP B=%llu",
1229 (int)BPE_GET_ETYPE(bp),
1230 (u_longlong_t)BPE_GET_LSIZE(bp),
1231 (u_longlong_t)BPE_GET_PSIZE(bp),
1232 (u_longlong_t)bp->blk_birth);
1238 for (i = 0; i < ndvas; i++)
1239 (void) snprintf(blkbuf + strlen(blkbuf),
1240 buflen - strlen(blkbuf), "%llu:%llx:%llx ",
1241 (u_longlong_t)DVA_GET_VDEV(&dva[i]),
1242 (u_longlong_t)DVA_GET_OFFSET(&dva[i]),
1243 (u_longlong_t)DVA_GET_ASIZE(&dva[i]));
1245 if (BP_IS_HOLE(bp)) {
1246 (void) snprintf(blkbuf + strlen(blkbuf),
1247 buflen - strlen(blkbuf),
1249 (u_longlong_t)BP_GET_LSIZE(bp),
1250 (u_longlong_t)bp->blk_birth);
1252 (void) snprintf(blkbuf + strlen(blkbuf),
1253 buflen - strlen(blkbuf),
1254 "%llxL/%llxP F=%llu B=%llu/%llu",
1255 (u_longlong_t)BP_GET_LSIZE(bp),
1256 (u_longlong_t)BP_GET_PSIZE(bp),
1257 (u_longlong_t)BP_GET_FILL(bp),
1258 (u_longlong_t)bp->blk_birth,
1259 (u_longlong_t)BP_PHYSICAL_BIRTH(bp));
1264 print_indirect(blkptr_t *bp, const zbookmark_phys_t *zb,
1265 const dnode_phys_t *dnp)
1267 char blkbuf[BP_SPRINTF_LEN];
1270 if (!BP_IS_EMBEDDED(bp)) {
1271 ASSERT3U(BP_GET_TYPE(bp), ==, dnp->dn_type);
1272 ASSERT3U(BP_GET_LEVEL(bp), ==, zb->zb_level);
1275 (void) printf("%16llx ", (u_longlong_t)blkid2offset(dnp, bp, zb));
1277 ASSERT(zb->zb_level >= 0);
1279 for (l = dnp->dn_nlevels - 1; l >= -1; l--) {
1280 if (l == zb->zb_level) {
1281 (void) printf("L%llx", (u_longlong_t)zb->zb_level);
1287 snprintf_blkptr_compact(blkbuf, sizeof (blkbuf), bp);
1288 (void) printf("%s\n", blkbuf);
1292 visit_indirect(spa_t *spa, const dnode_phys_t *dnp,
1293 blkptr_t *bp, const zbookmark_phys_t *zb)
1297 if (bp->blk_birth == 0)
1300 print_indirect(bp, zb, dnp);
1302 if (BP_GET_LEVEL(bp) > 0 && !BP_IS_HOLE(bp)) {
1303 arc_flags_t flags = ARC_FLAG_WAIT;
1306 int epb = BP_GET_LSIZE(bp) >> SPA_BLKPTRSHIFT;
1310 err = arc_read(NULL, spa, bp, arc_getbuf_func, &buf,
1311 ZIO_PRIORITY_ASYNC_READ, ZIO_FLAG_CANFAIL, &flags, zb);
1314 ASSERT(buf->b_data);
1316 /* recursively visit blocks below this */
1318 for (i = 0; i < epb; i++, cbp++) {
1319 zbookmark_phys_t czb;
1321 SET_BOOKMARK(&czb, zb->zb_objset, zb->zb_object,
1323 zb->zb_blkid * epb + i);
1324 err = visit_indirect(spa, dnp, cbp, &czb);
1327 fill += BP_GET_FILL(cbp);
1330 ASSERT3U(fill, ==, BP_GET_FILL(bp));
1331 arc_buf_destroy(buf, &buf);
1339 dump_indirect(dnode_t *dn)
1341 dnode_phys_t *dnp = dn->dn_phys;
1343 zbookmark_phys_t czb;
1345 (void) printf("Indirect blocks:\n");
1347 SET_BOOKMARK(&czb, dmu_objset_id(dn->dn_objset),
1348 dn->dn_object, dnp->dn_nlevels - 1, 0);
1349 for (j = 0; j < dnp->dn_nblkptr; j++) {
1351 (void) visit_indirect(dmu_objset_spa(dn->dn_objset), dnp,
1352 &dnp->dn_blkptr[j], &czb);
1355 (void) printf("\n");
1360 dump_dsl_dir(objset_t *os, uint64_t object, void *data, size_t size)
1362 dsl_dir_phys_t *dd = data;
1366 /* make sure nicenum has enough space */
1367 CTASSERT(sizeof (nice) >= NN_NUMBUF_SZ);
1372 ASSERT3U(size, >=, sizeof (dsl_dir_phys_t));
1374 crtime = dd->dd_creation_time;
1375 (void) printf("\t\tcreation_time = %s", ctime(&crtime));
1376 (void) printf("\t\thead_dataset_obj = %llu\n",
1377 (u_longlong_t)dd->dd_head_dataset_obj);
1378 (void) printf("\t\tparent_dir_obj = %llu\n",
1379 (u_longlong_t)dd->dd_parent_obj);
1380 (void) printf("\t\torigin_obj = %llu\n",
1381 (u_longlong_t)dd->dd_origin_obj);
1382 (void) printf("\t\tchild_dir_zapobj = %llu\n",
1383 (u_longlong_t)dd->dd_child_dir_zapobj);
1384 zdb_nicenum(dd->dd_used_bytes, nice, sizeof (nice));
1385 (void) printf("\t\tused_bytes = %s\n", nice);
1386 zdb_nicenum(dd->dd_compressed_bytes, nice, sizeof (nice));
1387 (void) printf("\t\tcompressed_bytes = %s\n", nice);
1388 zdb_nicenum(dd->dd_uncompressed_bytes, nice, sizeof (nice));
1389 (void) printf("\t\tuncompressed_bytes = %s\n", nice);
1390 zdb_nicenum(dd->dd_quota, nice, sizeof (nice));
1391 (void) printf("\t\tquota = %s\n", nice);
1392 zdb_nicenum(dd->dd_reserved, nice, sizeof (nice));
1393 (void) printf("\t\treserved = %s\n", nice);
1394 (void) printf("\t\tprops_zapobj = %llu\n",
1395 (u_longlong_t)dd->dd_props_zapobj);
1396 (void) printf("\t\tdeleg_zapobj = %llu\n",
1397 (u_longlong_t)dd->dd_deleg_zapobj);
1398 (void) printf("\t\tflags = %llx\n",
1399 (u_longlong_t)dd->dd_flags);
1402 zdb_nicenum(dd->dd_used_breakdown[DD_USED_ ## which], nice, \
1404 (void) printf("\t\tused_breakdown[" #which "] = %s\n", nice)
1415 dump_dsl_dataset(objset_t *os, uint64_t object, void *data, size_t size)
1417 dsl_dataset_phys_t *ds = data;
1419 char used[32], compressed[32], uncompressed[32], unique[32];
1420 char blkbuf[BP_SPRINTF_LEN];
1422 /* make sure nicenum has enough space */
1423 CTASSERT(sizeof (used) >= NN_NUMBUF_SZ);
1424 CTASSERT(sizeof (compressed) >= NN_NUMBUF_SZ);
1425 CTASSERT(sizeof (uncompressed) >= NN_NUMBUF_SZ);
1426 CTASSERT(sizeof (unique) >= NN_NUMBUF_SZ);
1431 ASSERT(size == sizeof (*ds));
1432 crtime = ds->ds_creation_time;
1433 zdb_nicenum(ds->ds_referenced_bytes, used, sizeof (used));
1434 zdb_nicenum(ds->ds_compressed_bytes, compressed, sizeof (compressed));
1435 zdb_nicenum(ds->ds_uncompressed_bytes, uncompressed,
1436 sizeof (uncompressed));
1437 zdb_nicenum(ds->ds_unique_bytes, unique, sizeof (unique));
1438 snprintf_blkptr(blkbuf, sizeof (blkbuf), &ds->ds_bp);
1440 (void) printf("\t\tdir_obj = %llu\n",
1441 (u_longlong_t)ds->ds_dir_obj);
1442 (void) printf("\t\tprev_snap_obj = %llu\n",
1443 (u_longlong_t)ds->ds_prev_snap_obj);
1444 (void) printf("\t\tprev_snap_txg = %llu\n",
1445 (u_longlong_t)ds->ds_prev_snap_txg);
1446 (void) printf("\t\tnext_snap_obj = %llu\n",
1447 (u_longlong_t)ds->ds_next_snap_obj);
1448 (void) printf("\t\tsnapnames_zapobj = %llu\n",
1449 (u_longlong_t)ds->ds_snapnames_zapobj);
1450 (void) printf("\t\tnum_children = %llu\n",
1451 (u_longlong_t)ds->ds_num_children);
1452 (void) printf("\t\tuserrefs_obj = %llu\n",
1453 (u_longlong_t)ds->ds_userrefs_obj);
1454 (void) printf("\t\tcreation_time = %s", ctime(&crtime));
1455 (void) printf("\t\tcreation_txg = %llu\n",
1456 (u_longlong_t)ds->ds_creation_txg);
1457 (void) printf("\t\tdeadlist_obj = %llu\n",
1458 (u_longlong_t)ds->ds_deadlist_obj);
1459 (void) printf("\t\tused_bytes = %s\n", used);
1460 (void) printf("\t\tcompressed_bytes = %s\n", compressed);
1461 (void) printf("\t\tuncompressed_bytes = %s\n", uncompressed);
1462 (void) printf("\t\tunique = %s\n", unique);
1463 (void) printf("\t\tfsid_guid = %llu\n",
1464 (u_longlong_t)ds->ds_fsid_guid);
1465 (void) printf("\t\tguid = %llu\n",
1466 (u_longlong_t)ds->ds_guid);
1467 (void) printf("\t\tflags = %llx\n",
1468 (u_longlong_t)ds->ds_flags);
1469 (void) printf("\t\tnext_clones_obj = %llu\n",
1470 (u_longlong_t)ds->ds_next_clones_obj);
1471 (void) printf("\t\tprops_obj = %llu\n",
1472 (u_longlong_t)ds->ds_props_obj);
1473 (void) printf("\t\tbp = %s\n", blkbuf);
1478 dump_bptree_cb(void *arg, const blkptr_t *bp, dmu_tx_t *tx)
1480 char blkbuf[BP_SPRINTF_LEN];
1482 if (bp->blk_birth != 0) {
1483 snprintf_blkptr(blkbuf, sizeof (blkbuf), bp);
1484 (void) printf("\t%s\n", blkbuf);
1490 dump_bptree(objset_t *os, uint64_t obj, const char *name)
1496 /* make sure nicenum has enough space */
1497 CTASSERT(sizeof (bytes) >= NN_NUMBUF_SZ);
1499 if (dump_opt['d'] < 3)
1502 VERIFY3U(0, ==, dmu_bonus_hold(os, obj, FTAG, &db));
1504 zdb_nicenum(bt->bt_bytes, bytes, sizeof (bytes));
1505 (void) printf("\n %s: %llu datasets, %s\n",
1506 name, (unsigned long long)(bt->bt_end - bt->bt_begin), bytes);
1507 dmu_buf_rele(db, FTAG);
1509 if (dump_opt['d'] < 5)
1512 (void) printf("\n");
1514 (void) bptree_iterate(os, obj, B_FALSE, dump_bptree_cb, NULL, NULL);
1519 dump_bpobj_cb(void *arg, const blkptr_t *bp, dmu_tx_t *tx)
1521 char blkbuf[BP_SPRINTF_LEN];
1523 ASSERT(bp->blk_birth != 0);
1524 snprintf_blkptr_compact(blkbuf, sizeof (blkbuf), bp);
1525 (void) printf("\t%s\n", blkbuf);
1530 dump_full_bpobj(bpobj_t *bpo, const char *name, int indent)
1537 /* make sure nicenum has enough space */
1538 CTASSERT(sizeof (bytes) >= NN_NUMBUF_SZ);
1539 CTASSERT(sizeof (comp) >= NN_NUMBUF_SZ);
1540 CTASSERT(sizeof (uncomp) >= NN_NUMBUF_SZ);
1542 if (dump_opt['d'] < 3)
1545 zdb_nicenum(bpo->bpo_phys->bpo_bytes, bytes, sizeof (bytes));
1546 if (bpo->bpo_havesubobj && bpo->bpo_phys->bpo_subobjs != 0) {
1547 zdb_nicenum(bpo->bpo_phys->bpo_comp, comp, sizeof (comp));
1548 zdb_nicenum(bpo->bpo_phys->bpo_uncomp, uncomp, sizeof (uncomp));
1549 (void) printf(" %*s: object %llu, %llu local blkptrs, "
1550 "%llu subobjs in object, %llu, %s (%s/%s comp)\n",
1552 (u_longlong_t)bpo->bpo_object,
1553 (u_longlong_t)bpo->bpo_phys->bpo_num_blkptrs,
1554 (u_longlong_t)bpo->bpo_phys->bpo_num_subobjs,
1555 (u_longlong_t)bpo->bpo_phys->bpo_subobjs,
1556 bytes, comp, uncomp);
1558 for (i = 0; i < bpo->bpo_phys->bpo_num_subobjs; i++) {
1562 VERIFY0(dmu_read(bpo->bpo_os,
1563 bpo->bpo_phys->bpo_subobjs,
1564 i * sizeof (subobj), sizeof (subobj), &subobj, 0));
1565 error = bpobj_open(&subbpo, bpo->bpo_os, subobj);
1567 (void) printf("ERROR %u while trying to open "
1569 error, (u_longlong_t)subobj);
1572 dump_full_bpobj(&subbpo, "subobj", indent + 1);
1573 bpobj_close(&subbpo);
1576 (void) printf(" %*s: object %llu, %llu blkptrs, %s\n",
1578 (u_longlong_t)bpo->bpo_object,
1579 (u_longlong_t)bpo->bpo_phys->bpo_num_blkptrs,
1583 if (dump_opt['d'] < 5)
1588 (void) bpobj_iterate_nofree(bpo, dump_bpobj_cb, NULL, NULL);
1589 (void) printf("\n");
1594 dump_deadlist(dsl_deadlist_t *dl)
1596 dsl_deadlist_entry_t *dle;
1602 /* make sure nicenum has enough space */
1603 CTASSERT(sizeof (bytes) >= NN_NUMBUF_SZ);
1604 CTASSERT(sizeof (comp) >= NN_NUMBUF_SZ);
1605 CTASSERT(sizeof (uncomp) >= NN_NUMBUF_SZ);
1607 if (dump_opt['d'] < 3)
1610 if (dl->dl_oldfmt) {
1611 dump_full_bpobj(&dl->dl_bpobj, "old-format deadlist", 0);
1615 zdb_nicenum(dl->dl_phys->dl_used, bytes, sizeof (bytes));
1616 zdb_nicenum(dl->dl_phys->dl_comp, comp, sizeof (comp));
1617 zdb_nicenum(dl->dl_phys->dl_uncomp, uncomp, sizeof (uncomp));
1618 (void) printf("\n Deadlist: %s (%s/%s comp)\n",
1619 bytes, comp, uncomp);
1621 if (dump_opt['d'] < 4)
1624 (void) printf("\n");
1626 /* force the tree to be loaded */
1627 dsl_deadlist_space_range(dl, 0, UINT64_MAX, &unused, &unused, &unused);
1629 for (dle = avl_first(&dl->dl_tree); dle;
1630 dle = AVL_NEXT(&dl->dl_tree, dle)) {
1631 if (dump_opt['d'] >= 5) {
1633 (void) snprintf(buf, sizeof (buf),
1634 "mintxg %llu -> obj %llu",
1635 (longlong_t)dle->dle_mintxg,
1636 (longlong_t)dle->dle_bpobj.bpo_object);
1638 dump_full_bpobj(&dle->dle_bpobj, buf, 0);
1640 (void) printf("mintxg %llu -> obj %llu\n",
1641 (longlong_t)dle->dle_mintxg,
1642 (longlong_t)dle->dle_bpobj.bpo_object);
1648 static avl_tree_t idx_tree;
1649 static avl_tree_t domain_tree;
1650 static boolean_t fuid_table_loaded;
1651 static objset_t *sa_os = NULL;
1652 static sa_attr_type_t *sa_attr_table = NULL;
1655 open_objset(const char *path, dmu_objset_type_t type, void *tag, objset_t **osp)
1658 uint64_t sa_attrs = 0;
1659 uint64_t version = 0;
1661 VERIFY3P(sa_os, ==, NULL);
1662 err = dmu_objset_own(path, type, B_TRUE, B_FALSE, tag, osp);
1664 (void) fprintf(stderr, "failed to own dataset '%s': %s\n", path,
1669 if (dmu_objset_type(*osp) == DMU_OST_ZFS && !(*osp)->os_encrypted) {
1670 (void) zap_lookup(*osp, MASTER_NODE_OBJ, ZPL_VERSION_STR,
1672 if (version >= ZPL_VERSION_SA) {
1673 (void) zap_lookup(*osp, MASTER_NODE_OBJ, ZFS_SA_ATTRS,
1676 err = sa_setup(*osp, sa_attrs, zfs_attr_table, ZPL_END,
1679 (void) fprintf(stderr, "sa_setup failed: %s\n",
1681 dmu_objset_disown(*osp, B_FALSE, tag);
1691 close_objset(objset_t *os, void *tag)
1693 VERIFY3P(os, ==, sa_os);
1694 if (os->os_sa != NULL)
1696 dmu_objset_disown(os, B_FALSE, tag);
1697 sa_attr_table = NULL;
1702 fuid_table_destroy(void)
1704 if (fuid_table_loaded) {
1705 zfs_fuid_table_destroy(&idx_tree, &domain_tree);
1706 fuid_table_loaded = B_FALSE;
1711 * print uid or gid information.
1712 * For normal POSIX id just the id is printed in decimal format.
1713 * For CIFS files with FUID the fuid is printed in hex followed by
1714 * the domain-rid string.
1717 print_idstr(uint64_t id, const char *id_type)
1719 if (FUID_INDEX(id)) {
1722 domain = zfs_fuid_idx_domain(&idx_tree, FUID_INDEX(id));
1723 (void) printf("\t%s %llx [%s-%d]\n", id_type,
1724 (u_longlong_t)id, domain, (int)FUID_RID(id));
1726 (void) printf("\t%s %llu\n", id_type, (u_longlong_t)id);
1732 dump_uidgid(objset_t *os, uint64_t uid, uint64_t gid)
1734 uint32_t uid_idx, gid_idx;
1736 uid_idx = FUID_INDEX(uid);
1737 gid_idx = FUID_INDEX(gid);
1739 /* Load domain table, if not already loaded */
1740 if (!fuid_table_loaded && (uid_idx || gid_idx)) {
1743 /* first find the fuid object. It lives in the master node */
1744 VERIFY(zap_lookup(os, MASTER_NODE_OBJ, ZFS_FUID_TABLES,
1745 8, 1, &fuid_obj) == 0);
1746 zfs_fuid_avl_tree_create(&idx_tree, &domain_tree);
1747 (void) zfs_fuid_table_load(os, fuid_obj,
1748 &idx_tree, &domain_tree);
1749 fuid_table_loaded = B_TRUE;
1752 print_idstr(uid, "uid");
1753 print_idstr(gid, "gid");
1757 dump_znode_sa_xattr(sa_handle_t *hdl)
1760 nvpair_t *elem = NULL;
1761 int sa_xattr_size = 0;
1762 int sa_xattr_entries = 0;
1764 char *sa_xattr_packed;
1766 error = sa_size(hdl, sa_attr_table[ZPL_DXATTR], &sa_xattr_size);
1767 if (error || sa_xattr_size == 0)
1770 sa_xattr_packed = malloc(sa_xattr_size);
1771 if (sa_xattr_packed == NULL)
1774 error = sa_lookup(hdl, sa_attr_table[ZPL_DXATTR],
1775 sa_xattr_packed, sa_xattr_size);
1777 free(sa_xattr_packed);
1781 error = nvlist_unpack(sa_xattr_packed, sa_xattr_size, &sa_xattr, 0);
1783 free(sa_xattr_packed);
1787 while ((elem = nvlist_next_nvpair(sa_xattr, elem)) != NULL)
1790 (void) printf("\tSA xattrs: %d bytes, %d entries\n\n",
1791 sa_xattr_size, sa_xattr_entries);
1792 while ((elem = nvlist_next_nvpair(sa_xattr, elem)) != NULL) {
1796 (void) printf("\t\t%s = ", nvpair_name(elem));
1797 nvpair_value_byte_array(elem, &value, &cnt);
1798 for (idx = 0; idx < cnt; ++idx) {
1799 if (isprint(value[idx]))
1800 (void) putchar(value[idx]);
1802 (void) printf("\\%3.3o", value[idx]);
1804 (void) putchar('\n');
1807 nvlist_free(sa_xattr);
1808 free(sa_xattr_packed);
1813 dump_znode(objset_t *os, uint64_t object, void *data, size_t size)
1815 char path[MAXPATHLEN * 2]; /* allow for xattr and failure prefix */
1817 uint64_t xattr, rdev, gen;
1818 uint64_t uid, gid, mode, fsize, parent, links;
1820 uint64_t acctm[2], modtm[2], chgtm[2], crtm[2];
1821 time_t z_crtime, z_atime, z_mtime, z_ctime;
1822 sa_bulk_attr_t bulk[12];
1826 VERIFY3P(os, ==, sa_os);
1827 if (sa_handle_get(os, object, NULL, SA_HDL_PRIVATE, &hdl)) {
1828 (void) printf("Failed to get handle for SA znode\n");
1832 SA_ADD_BULK_ATTR(bulk, idx, sa_attr_table[ZPL_UID], NULL, &uid, 8);
1833 SA_ADD_BULK_ATTR(bulk, idx, sa_attr_table[ZPL_GID], NULL, &gid, 8);
1834 SA_ADD_BULK_ATTR(bulk, idx, sa_attr_table[ZPL_LINKS], NULL,
1836 SA_ADD_BULK_ATTR(bulk, idx, sa_attr_table[ZPL_GEN], NULL, &gen, 8);
1837 SA_ADD_BULK_ATTR(bulk, idx, sa_attr_table[ZPL_MODE], NULL,
1839 SA_ADD_BULK_ATTR(bulk, idx, sa_attr_table[ZPL_PARENT],
1841 SA_ADD_BULK_ATTR(bulk, idx, sa_attr_table[ZPL_SIZE], NULL,
1843 SA_ADD_BULK_ATTR(bulk, idx, sa_attr_table[ZPL_ATIME], NULL,
1845 SA_ADD_BULK_ATTR(bulk, idx, sa_attr_table[ZPL_MTIME], NULL,
1847 SA_ADD_BULK_ATTR(bulk, idx, sa_attr_table[ZPL_CRTIME], NULL,
1849 SA_ADD_BULK_ATTR(bulk, idx, sa_attr_table[ZPL_CTIME], NULL,
1851 SA_ADD_BULK_ATTR(bulk, idx, sa_attr_table[ZPL_FLAGS], NULL,
1854 if (sa_bulk_lookup(hdl, bulk, idx)) {
1855 (void) sa_handle_destroy(hdl);
1859 z_crtime = (time_t)crtm[0];
1860 z_atime = (time_t)acctm[0];
1861 z_mtime = (time_t)modtm[0];
1862 z_ctime = (time_t)chgtm[0];
1864 if (dump_opt['d'] > 4) {
1865 error = zfs_obj_to_path(os, object, path, sizeof (path));
1867 (void) snprintf(path, sizeof (path),
1868 "\?\?\?<object#%llu>", (u_longlong_t)object);
1870 (void) printf("\tpath %s\n", path);
1872 dump_uidgid(os, uid, gid);
1873 (void) printf("\tatime %s", ctime(&z_atime));
1874 (void) printf("\tmtime %s", ctime(&z_mtime));
1875 (void) printf("\tctime %s", ctime(&z_ctime));
1876 (void) printf("\tcrtime %s", ctime(&z_crtime));
1877 (void) printf("\tgen %llu\n", (u_longlong_t)gen);
1878 (void) printf("\tmode %llo\n", (u_longlong_t)mode);
1879 (void) printf("\tsize %llu\n", (u_longlong_t)fsize);
1880 (void) printf("\tparent %llu\n", (u_longlong_t)parent);
1881 (void) printf("\tlinks %llu\n", (u_longlong_t)links);
1882 (void) printf("\tpflags %llx\n", (u_longlong_t)pflags);
1883 if (dmu_objset_projectquota_enabled(os) && (pflags & ZFS_PROJID)) {
1886 if (sa_lookup(hdl, sa_attr_table[ZPL_PROJID], &projid,
1887 sizeof (uint64_t)) == 0)
1888 (void) printf("\tprojid %llu\n", (u_longlong_t)projid);
1890 if (sa_lookup(hdl, sa_attr_table[ZPL_XATTR], &xattr,
1891 sizeof (uint64_t)) == 0)
1892 (void) printf("\txattr %llu\n", (u_longlong_t)xattr);
1893 if (sa_lookup(hdl, sa_attr_table[ZPL_RDEV], &rdev,
1894 sizeof (uint64_t)) == 0)
1895 (void) printf("\trdev 0x%016llx\n", (u_longlong_t)rdev);
1896 dump_znode_sa_xattr(hdl);
1897 sa_handle_destroy(hdl);
1902 dump_acl(objset_t *os, uint64_t object, void *data, size_t size)
1908 dump_dmu_objset(objset_t *os, uint64_t object, void *data, size_t size)
1912 static object_viewer_t *object_viewer[DMU_OT_NUMTYPES + 1] = {
1913 dump_none, /* unallocated */
1914 dump_zap, /* object directory */
1915 dump_uint64, /* object array */
1916 dump_none, /* packed nvlist */
1917 dump_packed_nvlist, /* packed nvlist size */
1918 dump_none, /* bpobj */
1919 dump_bpobj, /* bpobj header */
1920 dump_none, /* SPA space map header */
1921 dump_none, /* SPA space map */
1922 dump_none, /* ZIL intent log */
1923 dump_dnode, /* DMU dnode */
1924 dump_dmu_objset, /* DMU objset */
1925 dump_dsl_dir, /* DSL directory */
1926 dump_zap, /* DSL directory child map */
1927 dump_zap, /* DSL dataset snap map */
1928 dump_zap, /* DSL props */
1929 dump_dsl_dataset, /* DSL dataset */
1930 dump_znode, /* ZFS znode */
1931 dump_acl, /* ZFS V0 ACL */
1932 dump_uint8, /* ZFS plain file */
1933 dump_zpldir, /* ZFS directory */
1934 dump_zap, /* ZFS master node */
1935 dump_zap, /* ZFS delete queue */
1936 dump_uint8, /* zvol object */
1937 dump_zap, /* zvol prop */
1938 dump_uint8, /* other uint8[] */
1939 dump_uint64, /* other uint64[] */
1940 dump_zap, /* other ZAP */
1941 dump_zap, /* persistent error log */
1942 dump_uint8, /* SPA history */
1943 dump_history_offsets, /* SPA history offsets */
1944 dump_zap, /* Pool properties */
1945 dump_zap, /* DSL permissions */
1946 dump_acl, /* ZFS ACL */
1947 dump_uint8, /* ZFS SYSACL */
1948 dump_none, /* FUID nvlist */
1949 dump_packed_nvlist, /* FUID nvlist size */
1950 dump_zap, /* DSL dataset next clones */
1951 dump_zap, /* DSL scrub queue */
1952 dump_zap, /* ZFS user/group/project used */
1953 dump_zap, /* ZFS user/group/project quota */
1954 dump_zap, /* snapshot refcount tags */
1955 dump_ddt_zap, /* DDT ZAP object */
1956 dump_zap, /* DDT statistics */
1957 dump_znode, /* SA object */
1958 dump_zap, /* SA Master Node */
1959 dump_sa_attrs, /* SA attribute registration */
1960 dump_sa_layouts, /* SA attribute layouts */
1961 dump_zap, /* DSL scrub translations */
1962 dump_none, /* fake dedup BP */
1963 dump_zap, /* deadlist */
1964 dump_none, /* deadlist hdr */
1965 dump_zap, /* dsl clones */
1966 dump_bpobj_subobjs, /* bpobj subobjs */
1967 dump_unknown, /* Unknown type, must be last */
1971 dump_object(objset_t *os, uint64_t object, int verbosity, int *print_header,
1972 uint64_t *dnode_slots_used)
1974 dmu_buf_t *db = NULL;
1975 dmu_object_info_t doi;
1977 boolean_t dnode_held = B_FALSE;
1980 char iblk[32], dblk[32], lsize[32], asize[32], fill[32], dnsize[32];
1981 char bonus_size[32];
1985 /* make sure nicenum has enough space */
1986 CTASSERT(sizeof (iblk) >= NN_NUMBUF_SZ);
1987 CTASSERT(sizeof (dblk) >= NN_NUMBUF_SZ);
1988 CTASSERT(sizeof (lsize) >= NN_NUMBUF_SZ);
1989 CTASSERT(sizeof (asize) >= NN_NUMBUF_SZ);
1990 CTASSERT(sizeof (bonus_size) >= NN_NUMBUF_SZ);
1992 if (*print_header) {
1993 (void) printf("\n%10s %3s %5s %5s %5s %6s %5s %6s %s\n",
1994 "Object", "lvl", "iblk", "dblk", "dsize", "dnsize",
1995 "lsize", "%full", "type");
2000 dn = DMU_META_DNODE(os);
2001 dmu_object_info_from_dnode(dn, &doi);
2004 * Encrypted datasets will have sensitive bonus buffers
2005 * encrypted. Therefore we cannot hold the bonus buffer and
2006 * must hold the dnode itself instead.
2008 error = dmu_object_info(os, object, &doi);
2010 fatal("dmu_object_info() failed, errno %u", error);
2012 if (os->os_encrypted &&
2013 DMU_OT_IS_ENCRYPTED(doi.doi_bonus_type)) {
2014 error = dnode_hold(os, object, FTAG, &dn);
2016 fatal("dnode_hold() failed, errno %u", error);
2017 dnode_held = B_TRUE;
2019 error = dmu_bonus_hold(os, object, FTAG, &db);
2021 fatal("dmu_bonus_hold(%llu) failed, errno %u",
2023 bonus = db->db_data;
2024 bsize = db->db_size;
2025 dn = DB_DNODE((dmu_buf_impl_t *)db);
2029 if (dnode_slots_used)
2030 *dnode_slots_used = doi.doi_dnodesize / DNODE_MIN_SIZE;
2032 zdb_nicenum(doi.doi_metadata_block_size, iblk, sizeof (iblk));
2033 zdb_nicenum(doi.doi_data_block_size, dblk, sizeof (dblk));
2034 zdb_nicenum(doi.doi_max_offset, lsize, sizeof (lsize));
2035 zdb_nicenum(doi.doi_physical_blocks_512 << 9, asize, sizeof (asize));
2036 zdb_nicenum(doi.doi_bonus_size, bonus_size, sizeof (bonus_size));
2037 zdb_nicenum(doi.doi_dnodesize, dnsize, sizeof (dnsize));
2038 (void) sprintf(fill, "%6.2f", 100.0 * doi.doi_fill_count *
2039 doi.doi_data_block_size / (object == 0 ? DNODES_PER_BLOCK : 1) /
2040 doi.doi_max_offset);
2044 if (doi.doi_checksum != ZIO_CHECKSUM_INHERIT || verbosity >= 6) {
2045 (void) snprintf(aux + strlen(aux), sizeof (aux) - strlen(aux),
2046 " (K=%s)", ZDB_CHECKSUM_NAME(doi.doi_checksum));
2049 if (doi.doi_compress != ZIO_COMPRESS_INHERIT || verbosity >= 6) {
2050 (void) snprintf(aux + strlen(aux), sizeof (aux) - strlen(aux),
2051 " (Z=%s)", ZDB_COMPRESS_NAME(doi.doi_compress));
2054 (void) printf("%10lld %3u %5s %5s %5s %6s %5s %6s %s%s\n",
2055 (u_longlong_t)object, doi.doi_indirection, iblk, dblk,
2056 asize, dnsize, lsize, fill, zdb_ot_name(doi.doi_type), aux);
2058 if (doi.doi_bonus_type != DMU_OT_NONE && verbosity > 3) {
2059 (void) printf("%10s %3s %5s %5s %5s %5s %5s %6s %s\n",
2060 "", "", "", "", "", "", bonus_size, "bonus",
2061 zdb_ot_name(doi.doi_bonus_type));
2064 if (verbosity >= 4) {
2065 (void) printf("\tdnode flags: %s%s%s%s\n",
2066 (dn->dn_phys->dn_flags & DNODE_FLAG_USED_BYTES) ?
2068 (dn->dn_phys->dn_flags & DNODE_FLAG_USERUSED_ACCOUNTED) ?
2069 "USERUSED_ACCOUNTED " : "",
2070 (dn->dn_phys->dn_flags & DNODE_FLAG_USEROBJUSED_ACCOUNTED) ?
2071 "USEROBJUSED_ACCOUNTED " : "",
2072 (dn->dn_phys->dn_flags & DNODE_FLAG_SPILL_BLKPTR) ?
2073 "SPILL_BLKPTR" : "");
2074 (void) printf("\tdnode maxblkid: %llu\n",
2075 (longlong_t)dn->dn_phys->dn_maxblkid);
2078 object_viewer[ZDB_OT_TYPE(doi.doi_bonus_type)](os,
2079 object, bonus, bsize);
2081 (void) printf("\t\t(bonus encrypted)\n");
2084 if (!os->os_encrypted || !DMU_OT_IS_ENCRYPTED(doi.doi_type)) {
2085 object_viewer[ZDB_OT_TYPE(doi.doi_type)](os, object,
2088 (void) printf("\t\t(object encrypted)\n");
2097 if (verbosity >= 5) {
2099 * Report the list of segments that comprise the object.
2103 uint64_t blkfill = 1;
2106 if (dn->dn_type == DMU_OT_DNODE) {
2108 blkfill = DNODES_PER_BLOCK;
2113 /* make sure nicenum has enough space */
2114 CTASSERT(sizeof (segsize) >= NN_NUMBUF_SZ);
2115 error = dnode_next_offset(dn,
2116 0, &start, minlvl, blkfill, 0);
2120 error = dnode_next_offset(dn,
2121 DNODE_FIND_HOLE, &end, minlvl, blkfill, 0);
2122 zdb_nicenum(end - start, segsize, sizeof (segsize));
2123 (void) printf("\t\tsegment [%016llx, %016llx)"
2124 " size %5s\n", (u_longlong_t)start,
2125 (u_longlong_t)end, segsize);
2133 dmu_buf_rele(db, FTAG);
2135 dnode_rele(dn, FTAG);
2138 static const char *objset_types[DMU_OST_NUMTYPES] = {
2139 "NONE", "META", "ZPL", "ZVOL", "OTHER", "ANY" };
2142 dump_dir(objset_t *os)
2144 dmu_objset_stats_t dds;
2145 uint64_t object, object_count;
2146 uint64_t refdbytes, usedobjs, scratch;
2148 char blkbuf[BP_SPRINTF_LEN + 20];
2149 char osname[ZFS_MAX_DATASET_NAME_LEN];
2150 const char *type = "UNKNOWN";
2151 int verbosity = dump_opt['d'];
2152 int print_header = 1;
2155 uint64_t total_slots_used = 0;
2156 uint64_t max_slot_used = 0;
2157 uint64_t dnode_slots;
2159 /* make sure nicenum has enough space */
2160 CTASSERT(sizeof (numbuf) >= NN_NUMBUF_SZ);
2162 dsl_pool_config_enter(dmu_objset_pool(os), FTAG);
2163 dmu_objset_fast_stat(os, &dds);
2164 dsl_pool_config_exit(dmu_objset_pool(os), FTAG);
2166 if (dds.dds_type < DMU_OST_NUMTYPES)
2167 type = objset_types[dds.dds_type];
2169 if (dds.dds_type == DMU_OST_META) {
2170 dds.dds_creation_txg = TXG_INITIAL;
2171 usedobjs = BP_GET_FILL(os->os_rootbp);
2172 refdbytes = dsl_dir_phys(os->os_spa->spa_dsl_pool->dp_mos_dir)->
2175 dmu_objset_space(os, &refdbytes, &scratch, &usedobjs, &scratch);
2178 ASSERT3U(usedobjs, ==, BP_GET_FILL(os->os_rootbp));
2180 zdb_nicenum(refdbytes, numbuf, sizeof (numbuf));
2182 if (verbosity >= 4) {
2183 (void) snprintf(blkbuf, sizeof (blkbuf), ", rootbp ");
2184 (void) snprintf_blkptr(blkbuf + strlen(blkbuf),
2185 sizeof (blkbuf) - strlen(blkbuf), os->os_rootbp);
2190 dmu_objset_name(os, osname);
2192 (void) printf("Dataset %s [%s], ID %llu, cr_txg %llu, "
2193 "%s, %llu objects%s\n",
2194 osname, type, (u_longlong_t)dmu_objset_id(os),
2195 (u_longlong_t)dds.dds_creation_txg,
2196 numbuf, (u_longlong_t)usedobjs, blkbuf);
2198 if (zopt_objects != 0) {
2199 for (i = 0; i < zopt_objects; i++)
2200 dump_object(os, zopt_object[i], verbosity,
2201 &print_header, NULL);
2202 (void) printf("\n");
2206 if (dump_opt['i'] != 0 || verbosity >= 2)
2207 dump_intent_log(dmu_objset_zil(os));
2209 if (dmu_objset_ds(os) != NULL)
2210 dump_deadlist(&dmu_objset_ds(os)->ds_deadlist);
2215 if (BP_IS_HOLE(os->os_rootbp))
2218 dump_object(os, 0, verbosity, &print_header, NULL);
2220 if (DMU_USERUSED_DNODE(os) != NULL &&
2221 DMU_USERUSED_DNODE(os)->dn_type != 0) {
2222 dump_object(os, DMU_USERUSED_OBJECT, verbosity, &print_header,
2224 dump_object(os, DMU_GROUPUSED_OBJECT, verbosity, &print_header,
2228 if (DMU_PROJECTUSED_DNODE(os) != NULL &&
2229 DMU_PROJECTUSED_DNODE(os)->dn_type != 0)
2230 dump_object(os, DMU_PROJECTUSED_OBJECT, verbosity,
2231 &print_header, NULL);
2234 while ((error = dmu_object_next(os, &object, B_FALSE, 0)) == 0) {
2235 dump_object(os, object, verbosity, &print_header, &dnode_slots);
2237 total_slots_used += dnode_slots;
2238 max_slot_used = object + dnode_slots - 1;
2241 (void) printf("\n");
2243 (void) printf(" Dnode slots:\n");
2244 (void) printf("\tTotal used: %10llu\n",
2245 (u_longlong_t)total_slots_used);
2246 (void) printf("\tMax used: %10llu\n",
2247 (u_longlong_t)max_slot_used);
2248 (void) printf("\tPercent empty: %10lf\n",
2249 (double)(max_slot_used - total_slots_used)*100 /
2250 (double)max_slot_used);
2252 (void) printf("\n");
2254 if (error != ESRCH) {
2255 (void) fprintf(stderr, "dmu_object_next() = %d\n", error);
2259 ASSERT3U(object_count, ==, usedobjs);
2263 dump_uberblock(uberblock_t *ub, const char *header, const char *footer)
2265 time_t timestamp = ub->ub_timestamp;
2267 (void) printf("%s", header ? header : "");
2268 (void) printf("\tmagic = %016llx\n", (u_longlong_t)ub->ub_magic);
2269 (void) printf("\tversion = %llu\n", (u_longlong_t)ub->ub_version);
2270 (void) printf("\ttxg = %llu\n", (u_longlong_t)ub->ub_txg);
2271 (void) printf("\tguid_sum = %llu\n", (u_longlong_t)ub->ub_guid_sum);
2272 (void) printf("\ttimestamp = %llu UTC = %s",
2273 (u_longlong_t)ub->ub_timestamp, asctime(localtime(×tamp)));
2275 (void) printf("\tmmp_magic = %016llx\n",
2276 (u_longlong_t)ub->ub_mmp_magic);
2277 if (ub->ub_mmp_magic == MMP_MAGIC)
2278 (void) printf("\tmmp_delay = %0llu\n",
2279 (u_longlong_t)ub->ub_mmp_delay);
2281 if (dump_opt['u'] >= 4) {
2282 char blkbuf[BP_SPRINTF_LEN];
2283 snprintf_blkptr(blkbuf, sizeof (blkbuf), &ub->ub_rootbp);
2284 (void) printf("\trootbp = %s\n", blkbuf);
2286 (void) printf("%s", footer ? footer : "");
2290 dump_config(spa_t *spa)
2297 error = dmu_bonus_hold(spa->spa_meta_objset,
2298 spa->spa_config_object, FTAG, &db);
2301 nvsize = *(uint64_t *)db->db_data;
2302 dmu_buf_rele(db, FTAG);
2304 (void) printf("\nMOS Configuration:\n");
2305 dump_packed_nvlist(spa->spa_meta_objset,
2306 spa->spa_config_object, (void *)&nvsize, 1);
2308 (void) fprintf(stderr, "dmu_bonus_hold(%llu) failed, errno %d",
2309 (u_longlong_t)spa->spa_config_object, error);
2314 dump_cachefile(const char *cachefile)
2317 struct stat64 statbuf;
2321 if ((fd = open64(cachefile, O_RDONLY)) < 0) {
2322 (void) printf("cannot open '%s': %s\n", cachefile,
2327 if (fstat64(fd, &statbuf) != 0) {
2328 (void) printf("failed to stat '%s': %s\n", cachefile,
2333 if ((buf = malloc(statbuf.st_size)) == NULL) {
2334 (void) fprintf(stderr, "failed to allocate %llu bytes\n",
2335 (u_longlong_t)statbuf.st_size);
2339 if (read(fd, buf, statbuf.st_size) != statbuf.st_size) {
2340 (void) fprintf(stderr, "failed to read %llu bytes\n",
2341 (u_longlong_t)statbuf.st_size);
2347 if (nvlist_unpack(buf, statbuf.st_size, &config, 0) != 0) {
2348 (void) fprintf(stderr, "failed to unpack nvlist\n");
2354 dump_nvlist(config, 0);
2356 nvlist_free(config);
2360 * ZFS label nvlist stats
2362 typedef struct zdb_nvl_stats {
2365 size_t zns_leaf_largest;
2366 size_t zns_leaf_total;
2367 nvlist_t *zns_string;
2368 nvlist_t *zns_uint64;
2369 nvlist_t *zns_boolean;
2373 collect_nvlist_stats(nvlist_t *nvl, zdb_nvl_stats_t *stats)
2375 nvlist_t *list, **array;
2376 nvpair_t *nvp = NULL;
2380 stats->zns_list_count++;
2382 while ((nvp = nvlist_next_nvpair(nvl, nvp)) != NULL) {
2383 name = nvpair_name(nvp);
2385 switch (nvpair_type(nvp)) {
2386 case DATA_TYPE_STRING:
2387 fnvlist_add_string(stats->zns_string, name,
2388 fnvpair_value_string(nvp));
2390 case DATA_TYPE_UINT64:
2391 fnvlist_add_uint64(stats->zns_uint64, name,
2392 fnvpair_value_uint64(nvp));
2394 case DATA_TYPE_BOOLEAN:
2395 fnvlist_add_boolean(stats->zns_boolean, name);
2397 case DATA_TYPE_NVLIST:
2398 if (nvpair_value_nvlist(nvp, &list) == 0)
2399 collect_nvlist_stats(list, stats);
2401 case DATA_TYPE_NVLIST_ARRAY:
2402 if (nvpair_value_nvlist_array(nvp, &array, &items) != 0)
2405 for (i = 0; i < items; i++) {
2406 collect_nvlist_stats(array[i], stats);
2408 /* collect stats on leaf vdev */
2409 if (strcmp(name, "children") == 0) {
2412 (void) nvlist_size(array[i], &size,
2414 stats->zns_leaf_total += size;
2415 if (size > stats->zns_leaf_largest)
2416 stats->zns_leaf_largest = size;
2417 stats->zns_leaf_count++;
2422 (void) printf("skip type %d!\n", (int)nvpair_type(nvp));
2428 dump_nvlist_stats(nvlist_t *nvl, size_t cap)
2430 zdb_nvl_stats_t stats = { 0 };
2431 size_t size, sum = 0, total;
2434 /* requires nvlist with non-unique names for stat collection */
2435 VERIFY0(nvlist_alloc(&stats.zns_string, 0, 0));
2436 VERIFY0(nvlist_alloc(&stats.zns_uint64, 0, 0));
2437 VERIFY0(nvlist_alloc(&stats.zns_boolean, 0, 0));
2438 VERIFY0(nvlist_size(stats.zns_boolean, &noise, NV_ENCODE_XDR));
2440 (void) printf("\n\nZFS Label NVList Config Stats:\n");
2442 VERIFY0(nvlist_size(nvl, &total, NV_ENCODE_XDR));
2443 (void) printf(" %d bytes used, %d bytes free (using %4.1f%%)\n\n",
2444 (int)total, (int)(cap - total), 100.0 * total / cap);
2446 collect_nvlist_stats(nvl, &stats);
2448 VERIFY0(nvlist_size(stats.zns_uint64, &size, NV_ENCODE_XDR));
2451 (void) printf("%12s %4d %6d bytes (%5.2f%%)\n", "integers:",
2452 (int)fnvlist_num_pairs(stats.zns_uint64),
2453 (int)size, 100.0 * size / total);
2455 VERIFY0(nvlist_size(stats.zns_string, &size, NV_ENCODE_XDR));
2458 (void) printf("%12s %4d %6d bytes (%5.2f%%)\n", "strings:",
2459 (int)fnvlist_num_pairs(stats.zns_string),
2460 (int)size, 100.0 * size / total);
2462 VERIFY0(nvlist_size(stats.zns_boolean, &size, NV_ENCODE_XDR));
2465 (void) printf("%12s %4d %6d bytes (%5.2f%%)\n", "booleans:",
2466 (int)fnvlist_num_pairs(stats.zns_boolean),
2467 (int)size, 100.0 * size / total);
2469 size = total - sum; /* treat remainder as nvlist overhead */
2470 (void) printf("%12s %4d %6d bytes (%5.2f%%)\n\n", "nvlists:",
2471 stats.zns_list_count, (int)size, 100.0 * size / total);
2473 if (stats.zns_leaf_count > 0) {
2474 size_t average = stats.zns_leaf_total / stats.zns_leaf_count;
2476 (void) printf("%12s %4d %6d bytes average\n", "leaf vdevs:",
2477 stats.zns_leaf_count, (int)average);
2478 (void) printf("%24d bytes largest\n",
2479 (int)stats.zns_leaf_largest);
2481 if (dump_opt['l'] >= 3 && average > 0)
2482 (void) printf(" space for %d additional leaf vdevs\n",
2483 (int)((cap - total) / average));
2485 (void) printf("\n");
2487 nvlist_free(stats.zns_string);
2488 nvlist_free(stats.zns_uint64);
2489 nvlist_free(stats.zns_boolean);
2492 typedef struct cksum_record {
2494 boolean_t labels[VDEV_LABELS];
2499 cksum_record_compare(const void *x1, const void *x2)
2501 const cksum_record_t *l = (cksum_record_t *)x1;
2502 const cksum_record_t *r = (cksum_record_t *)x2;
2503 int arraysize = ARRAY_SIZE(l->cksum.zc_word);
2506 for (int i = 0; i < arraysize; i++) {
2507 difference = AVL_CMP(l->cksum.zc_word[i], r->cksum.zc_word[i]);
2512 return (difference);
2515 static cksum_record_t *
2516 cksum_record_alloc(zio_cksum_t *cksum, int l)
2518 cksum_record_t *rec;
2520 rec = umem_zalloc(sizeof (*rec), UMEM_NOFAIL);
2521 rec->cksum = *cksum;
2522 rec->labels[l] = B_TRUE;
2527 static cksum_record_t *
2528 cksum_record_lookup(avl_tree_t *tree, zio_cksum_t *cksum)
2530 cksum_record_t lookup = { .cksum = *cksum };
2533 return (avl_find(tree, &lookup, &where));
2536 static cksum_record_t *
2537 cksum_record_insert(avl_tree_t *tree, zio_cksum_t *cksum, int l)
2539 cksum_record_t *rec;
2541 rec = cksum_record_lookup(tree, cksum);
2543 rec->labels[l] = B_TRUE;
2545 rec = cksum_record_alloc(cksum, l);
2553 first_label(cksum_record_t *rec)
2555 for (int i = 0; i < VDEV_LABELS; i++)
2563 print_label_numbers(char *prefix, cksum_record_t *rec)
2565 printf("%s", prefix);
2566 for (int i = 0; i < VDEV_LABELS; i++)
2567 if (rec->labels[i] == B_TRUE)
2572 #define MAX_UBERBLOCK_COUNT (VDEV_UBERBLOCK_RING >> UBERBLOCK_SHIFT)
2574 typedef struct label {
2576 nvlist_t *config_nv;
2577 cksum_record_t *config;
2578 cksum_record_t *uberblocks[MAX_UBERBLOCK_COUNT];
2579 boolean_t header_printed;
2580 boolean_t read_failed;
2584 print_label_header(label_t *label, int l)
2590 if (label->header_printed == B_TRUE)
2593 (void) printf("------------------------------------\n");
2594 (void) printf("LABEL %d\n", l);
2595 (void) printf("------------------------------------\n");
2597 label->header_printed = B_TRUE;
2601 dump_config_from_label(label_t *label, size_t buflen, int l)
2606 if ((dump_opt['l'] < 3) && (first_label(label->config) != l))
2609 print_label_header(label, l);
2610 dump_nvlist(label->config_nv, 4);
2611 print_label_numbers(" labels = ", label->config);
2613 if (dump_opt['l'] >= 2)
2614 dump_nvlist_stats(label->config_nv, buflen);
2617 #define ZDB_MAX_UB_HEADER_SIZE 32
2620 dump_label_uberblocks(label_t *label, uint64_t ashift, int label_num)
2624 char header[ZDB_MAX_UB_HEADER_SIZE];
2626 vd.vdev_ashift = ashift;
2629 for (int i = 0; i < VDEV_UBERBLOCK_COUNT(&vd); i++) {
2630 uint64_t uoff = VDEV_UBERBLOCK_OFFSET(&vd, i);
2631 uberblock_t *ub = (void *)((char *)&label->label + uoff);
2632 cksum_record_t *rec = label->uberblocks[i];
2635 if (dump_opt['u'] >= 2) {
2636 print_label_header(label, label_num);
2637 (void) printf(" Uberblock[%d] invalid\n", i);
2642 if ((dump_opt['u'] < 3) && (first_label(rec) != label_num))
2645 if ((dump_opt['u'] < 4) &&
2646 (ub->ub_mmp_magic == MMP_MAGIC) && ub->ub_mmp_delay &&
2647 (i >= VDEV_UBERBLOCK_COUNT(&vd) - MMP_BLOCKS_PER_LABEL))
2650 print_label_header(label, label_num);
2651 (void) snprintf(header, ZDB_MAX_UB_HEADER_SIZE,
2652 " Uberblock[%d]\n", i);
2653 dump_uberblock(ub, header, "");
2654 print_label_numbers(" labels = ", rec);
2658 static char curpath[PATH_MAX];
2661 * Iterate through the path components, recursively passing
2662 * current one's obj and remaining path until we find the obj
2666 dump_path_impl(objset_t *os, uint64_t obj, char *name)
2673 dmu_object_info_t doi;
2675 if ((s = strchr(name, '/')) != NULL)
2677 err = zap_lookup(os, obj, name, 8, 1, &child_obj);
2679 (void) strlcat(curpath, name, sizeof (curpath));
2682 (void) fprintf(stderr, "failed to lookup %s: %s\n",
2683 curpath, strerror(err));
2687 child_obj = ZFS_DIRENT_OBJ(child_obj);
2688 err = sa_buf_hold(os, child_obj, FTAG, &db);
2690 (void) fprintf(stderr,
2691 "failed to get SA dbuf for obj %llu: %s\n",
2692 (u_longlong_t)child_obj, strerror(err));
2695 dmu_object_info_from_db(db, &doi);
2696 sa_buf_rele(db, FTAG);
2698 if (doi.doi_bonus_type != DMU_OT_SA &&
2699 doi.doi_bonus_type != DMU_OT_ZNODE) {
2700 (void) fprintf(stderr, "invalid bonus type %d for obj %llu\n",
2701 doi.doi_bonus_type, (u_longlong_t)child_obj);
2705 if (dump_opt['v'] > 6) {
2706 (void) printf("obj=%llu %s type=%d bonustype=%d\n",
2707 (u_longlong_t)child_obj, curpath, doi.doi_type,
2708 doi.doi_bonus_type);
2711 (void) strlcat(curpath, "/", sizeof (curpath));
2713 switch (doi.doi_type) {
2714 case DMU_OT_DIRECTORY_CONTENTS:
2715 if (s != NULL && *(s + 1) != '\0')
2716 return (dump_path_impl(os, child_obj, s + 1));
2718 case DMU_OT_PLAIN_FILE_CONTENTS:
2719 dump_object(os, child_obj, dump_opt['v'], &header, NULL);
2722 (void) fprintf(stderr, "object %llu has non-file/directory "
2723 "type %d\n", (u_longlong_t)obj, doi.doi_type);
2731 * Dump the blocks for the object specified by path inside the dataset.
2734 dump_path(char *ds, char *path)
2740 err = open_objset(ds, DMU_OST_ZFS, FTAG, &os);
2744 err = zap_lookup(os, MASTER_NODE_OBJ, ZFS_ROOT_OBJ, 8, 1, &root_obj);
2746 (void) fprintf(stderr, "can't lookup root znode: %s\n",
2748 dmu_objset_disown(os, B_FALSE, FTAG);
2752 (void) snprintf(curpath, sizeof (curpath), "dataset=%s path=/", ds);
2754 err = dump_path_impl(os, root_obj, path);
2756 close_objset(os, FTAG);
2761 dump_label(const char *dev)
2763 char path[MAXPATHLEN];
2764 label_t labels[VDEV_LABELS];
2765 uint64_t psize, ashift;
2766 struct stat64 statbuf;
2767 boolean_t config_found = B_FALSE;
2768 boolean_t error = B_FALSE;
2769 avl_tree_t config_tree;
2770 avl_tree_t uberblock_tree;
2771 void *node, *cookie;
2774 bzero(labels, sizeof (labels));
2777 * Check if we were given absolute path and use it as is.
2778 * Otherwise if the provided vdev name doesn't point to a file,
2779 * try prepending expected disk paths and partition numbers.
2781 (void) strlcpy(path, dev, sizeof (path));
2782 if (dev[0] != '/' && stat64(path, &statbuf) != 0) {
2785 error = zfs_resolve_shortname(dev, path, MAXPATHLEN);
2786 if (error == 0 && zfs_dev_is_whole_disk(path)) {
2787 if (zfs_append_partition(path, MAXPATHLEN) == -1)
2791 if (error || (stat64(path, &statbuf) != 0)) {
2792 (void) printf("failed to find device %s, try "
2793 "specifying absolute path instead\n", dev);
2798 if ((fd = open64(path, O_RDONLY)) < 0) {
2799 (void) printf("cannot open '%s': %s\n", path, strerror(errno));
2803 if (fstat64_blk(fd, &statbuf) != 0) {
2804 (void) printf("failed to stat '%s': %s\n", path,
2810 if (S_ISBLK(statbuf.st_mode) && ioctl(fd, BLKFLSBUF) != 0)
2811 (void) printf("failed to invalidate cache '%s' : %s\n", path,
2814 avl_create(&config_tree, cksum_record_compare,
2815 sizeof (cksum_record_t), offsetof(cksum_record_t, link));
2816 avl_create(&uberblock_tree, cksum_record_compare,
2817 sizeof (cksum_record_t), offsetof(cksum_record_t, link));
2819 psize = statbuf.st_size;
2820 psize = P2ALIGN(psize, (uint64_t)sizeof (vdev_label_t));
2821 ashift = SPA_MINBLOCKSHIFT;
2824 * 1. Read the label from disk
2825 * 2. Unpack the configuration and insert in config tree.
2826 * 3. Traverse all uberblocks and insert in uberblock tree.
2828 for (int l = 0; l < VDEV_LABELS; l++) {
2829 label_t *label = &labels[l];
2830 char *buf = label->label.vl_vdev_phys.vp_nvlist;
2831 size_t buflen = sizeof (label->label.vl_vdev_phys.vp_nvlist);
2833 cksum_record_t *rec;
2837 if (pread64(fd, &label->label, sizeof (label->label),
2838 vdev_label_offset(psize, l, 0)) != sizeof (label->label)) {
2840 (void) printf("failed to read label %d\n", l);
2841 label->read_failed = B_TRUE;
2846 label->read_failed = B_FALSE;
2848 if (nvlist_unpack(buf, buflen, &config, 0) == 0) {
2849 nvlist_t *vdev_tree = NULL;
2852 if ((nvlist_lookup_nvlist(config,
2853 ZPOOL_CONFIG_VDEV_TREE, &vdev_tree) != 0) ||
2854 (nvlist_lookup_uint64(vdev_tree,
2855 ZPOOL_CONFIG_ASHIFT, &ashift) != 0))
2856 ashift = SPA_MINBLOCKSHIFT;
2858 if (nvlist_size(config, &size, NV_ENCODE_XDR) != 0)
2861 fletcher_4_native_varsize(buf, size, &cksum);
2862 rec = cksum_record_insert(&config_tree, &cksum, l);
2864 label->config = rec;
2865 label->config_nv = config;
2866 config_found = B_TRUE;
2871 vd.vdev_ashift = ashift;
2874 for (int i = 0; i < VDEV_UBERBLOCK_COUNT(&vd); i++) {
2875 uint64_t uoff = VDEV_UBERBLOCK_OFFSET(&vd, i);
2876 uberblock_t *ub = (void *)((char *)label + uoff);
2878 if (uberblock_verify(ub))
2881 fletcher_4_native_varsize(ub, sizeof (*ub), &cksum);
2882 rec = cksum_record_insert(&uberblock_tree, &cksum, l);
2884 label->uberblocks[i] = rec;
2889 * Dump the label and uberblocks.
2891 for (int l = 0; l < VDEV_LABELS; l++) {
2892 label_t *label = &labels[l];
2893 size_t buflen = sizeof (label->label.vl_vdev_phys.vp_nvlist);
2895 if (label->read_failed == B_TRUE)
2898 if (label->config_nv) {
2899 dump_config_from_label(label, buflen, l);
2902 (void) printf("failed to unpack label %d\n", l);
2906 dump_label_uberblocks(label, ashift, l);
2908 nvlist_free(label->config_nv);
2912 while ((node = avl_destroy_nodes(&config_tree, &cookie)) != NULL)
2913 umem_free(node, sizeof (cksum_record_t));
2916 while ((node = avl_destroy_nodes(&uberblock_tree, &cookie)) != NULL)
2917 umem_free(node, sizeof (cksum_record_t));
2919 avl_destroy(&config_tree);
2920 avl_destroy(&uberblock_tree);
2924 return (config_found == B_FALSE ? 2 :
2925 (error == B_TRUE ? 1 : 0));
2928 static uint64_t dataset_feature_count[SPA_FEATURES];
2932 dump_one_dir(const char *dsname, void *arg)
2938 error = open_objset(dsname, DMU_OST_ANY, FTAG, &os);
2942 for (f = 0; f < SPA_FEATURES; f++) {
2943 if (!dmu_objset_ds(os)->ds_feature_inuse[f])
2945 ASSERT(spa_feature_table[f].fi_flags &
2946 ZFEATURE_FLAG_PER_DATASET);
2947 dataset_feature_count[f]++;
2951 close_objset(os, FTAG);
2952 fuid_table_destroy();
2959 #define PSIZE_HISTO_SIZE (SPA_OLD_MAXBLOCKSIZE / SPA_MINBLOCKSIZE + 2)
2960 typedef struct zdb_blkstats {
2966 uint64_t zb_ditto_samevdev;
2967 uint64_t zb_psize_histogram[PSIZE_HISTO_SIZE];
2971 * Extended object types to report deferred frees and dedup auto-ditto blocks.
2973 #define ZDB_OT_DEFERRED (DMU_OT_NUMTYPES + 0)
2974 #define ZDB_OT_DITTO (DMU_OT_NUMTYPES + 1)
2975 #define ZDB_OT_OTHER (DMU_OT_NUMTYPES + 2)
2976 #define ZDB_OT_TOTAL (DMU_OT_NUMTYPES + 3)
2978 static const char *zdb_ot_extname[] = {
2985 #define ZB_TOTAL DN_MAX_LEVELS
2987 typedef struct zdb_cb {
2988 zdb_blkstats_t zcb_type[ZB_TOTAL + 1][ZDB_OT_TOTAL + 1];
2989 uint64_t zcb_dedup_asize;
2990 uint64_t zcb_dedup_blocks;
2991 uint64_t zcb_embedded_blocks[NUM_BP_EMBEDDED_TYPES];
2992 uint64_t zcb_embedded_histogram[NUM_BP_EMBEDDED_TYPES]
2993 [BPE_PAYLOAD_SIZE + 1];
2995 hrtime_t zcb_lastprint;
2996 uint64_t zcb_totalasize;
2997 uint64_t zcb_errors[256];
3004 zdb_count_block(zdb_cb_t *zcb, zilog_t *zilog, const blkptr_t *bp,
3005 dmu_object_type_t type)
3007 uint64_t refcnt = 0;
3010 ASSERT(type < ZDB_OT_TOTAL);
3012 if (zilog && zil_bp_tree_add(zilog, bp) != 0)
3015 for (i = 0; i < 4; i++) {
3016 int l = (i < 2) ? BP_GET_LEVEL(bp) : ZB_TOTAL;
3017 int t = (i & 1) ? type : ZDB_OT_TOTAL;
3019 zdb_blkstats_t *zb = &zcb->zcb_type[l][t];
3021 zb->zb_asize += BP_GET_ASIZE(bp);
3022 zb->zb_lsize += BP_GET_LSIZE(bp);
3023 zb->zb_psize += BP_GET_PSIZE(bp);
3027 * The histogram is only big enough to record blocks up to
3028 * SPA_OLD_MAXBLOCKSIZE; larger blocks go into the last,
3031 unsigned idx = BP_GET_PSIZE(bp) >> SPA_MINBLOCKSHIFT;
3032 idx = MIN(idx, SPA_OLD_MAXBLOCKSIZE / SPA_MINBLOCKSIZE + 1);
3033 zb->zb_psize_histogram[idx]++;
3035 zb->zb_gangs += BP_COUNT_GANG(bp);
3037 switch (BP_GET_NDVAS(bp)) {
3039 if (DVA_GET_VDEV(&bp->blk_dva[0]) ==
3040 DVA_GET_VDEV(&bp->blk_dva[1]))
3041 zb->zb_ditto_samevdev++;
3044 equal = (DVA_GET_VDEV(&bp->blk_dva[0]) ==
3045 DVA_GET_VDEV(&bp->blk_dva[1])) +
3046 (DVA_GET_VDEV(&bp->blk_dva[0]) ==
3047 DVA_GET_VDEV(&bp->blk_dva[2])) +
3048 (DVA_GET_VDEV(&bp->blk_dva[1]) ==
3049 DVA_GET_VDEV(&bp->blk_dva[2]));
3051 zb->zb_ditto_samevdev++;
3057 if (BP_IS_EMBEDDED(bp)) {
3058 zcb->zcb_embedded_blocks[BPE_GET_ETYPE(bp)]++;
3059 zcb->zcb_embedded_histogram[BPE_GET_ETYPE(bp)]
3060 [BPE_GET_PSIZE(bp)]++;
3067 if (BP_GET_DEDUP(bp)) {
3071 ddt = ddt_select(zcb->zcb_spa, bp);
3073 dde = ddt_lookup(ddt, bp, B_FALSE);
3078 ddt_phys_t *ddp = ddt_phys_select(dde, bp);
3079 ddt_phys_decref(ddp);
3080 refcnt = ddp->ddp_refcnt;
3081 if (ddt_phys_total_refcnt(dde) == 0)
3082 ddt_remove(ddt, dde);
3087 VERIFY3U(zio_wait(zio_claim(NULL, zcb->zcb_spa,
3088 refcnt ? 0 : spa_first_txg(zcb->zcb_spa),
3089 bp, NULL, NULL, ZIO_FLAG_CANFAIL)), ==, 0);
3093 zdb_blkptr_done(zio_t *zio)
3095 spa_t *spa = zio->io_spa;
3096 blkptr_t *bp = zio->io_bp;
3097 int ioerr = zio->io_error;
3098 zdb_cb_t *zcb = zio->io_private;
3099 zbookmark_phys_t *zb = &zio->io_bookmark;
3101 abd_free(zio->io_abd);
3103 mutex_enter(&spa->spa_scrub_lock);
3104 spa->spa_load_verify_ios--;
3105 cv_broadcast(&spa->spa_scrub_io_cv);
3107 if (ioerr && !(zio->io_flags & ZIO_FLAG_SPECULATIVE)) {
3108 char blkbuf[BP_SPRINTF_LEN];
3110 zcb->zcb_haderrors = 1;
3111 zcb->zcb_errors[ioerr]++;
3113 if (dump_opt['b'] >= 2)
3114 snprintf_blkptr(blkbuf, sizeof (blkbuf), bp);
3118 (void) printf("zdb_blkptr_cb: "
3119 "Got error %d reading "
3120 "<%llu, %llu, %lld, %llx> %s -- skipping\n",
3122 (u_longlong_t)zb->zb_objset,
3123 (u_longlong_t)zb->zb_object,
3124 (u_longlong_t)zb->zb_level,
3125 (u_longlong_t)zb->zb_blkid,
3128 mutex_exit(&spa->spa_scrub_lock);
3132 zdb_blkptr_cb(spa_t *spa, zilog_t *zilog, const blkptr_t *bp,
3133 const zbookmark_phys_t *zb, const dnode_phys_t *dnp, void *arg)
3135 zdb_cb_t *zcb = arg;
3136 dmu_object_type_t type;
3137 boolean_t is_metadata;
3142 if (dump_opt['b'] >= 5 && bp->blk_birth > 0) {
3143 char blkbuf[BP_SPRINTF_LEN];
3144 snprintf_blkptr(blkbuf, sizeof (blkbuf), bp);
3145 (void) printf("objset %llu object %llu "
3146 "level %lld offset 0x%llx %s\n",
3147 (u_longlong_t)zb->zb_objset,
3148 (u_longlong_t)zb->zb_object,
3149 (longlong_t)zb->zb_level,
3150 (u_longlong_t)blkid2offset(dnp, bp, zb),
3157 type = BP_GET_TYPE(bp);
3159 zdb_count_block(zcb, zilog, bp,
3160 (type & DMU_OT_NEWTYPE) ? ZDB_OT_OTHER : type);
3162 is_metadata = (BP_GET_LEVEL(bp) != 0 || DMU_OT_IS_METADATA(type));
3164 if (!BP_IS_EMBEDDED(bp) &&
3165 (dump_opt['c'] > 1 || (dump_opt['c'] && is_metadata))) {
3166 size_t size = BP_GET_PSIZE(bp);
3167 abd_t *abd = abd_alloc(size, B_FALSE);
3168 int flags = ZIO_FLAG_CANFAIL | ZIO_FLAG_SCRUB | ZIO_FLAG_RAW;
3170 /* If it's an intent log block, failure is expected. */
3171 if (zb->zb_level == ZB_ZIL_LEVEL)
3172 flags |= ZIO_FLAG_SPECULATIVE;
3174 mutex_enter(&spa->spa_scrub_lock);
3175 while (spa->spa_load_verify_ios > max_inflight)
3176 cv_wait(&spa->spa_scrub_io_cv, &spa->spa_scrub_lock);
3177 spa->spa_load_verify_ios++;
3178 mutex_exit(&spa->spa_scrub_lock);
3180 zio_nowait(zio_read(NULL, spa, bp, abd, size,
3181 zdb_blkptr_done, zcb, ZIO_PRIORITY_ASYNC_READ, flags, zb));
3184 zcb->zcb_readfails = 0;
3186 /* only call gethrtime() every 100 blocks */
3193 if (dump_opt['b'] < 5 && gethrtime() > zcb->zcb_lastprint + NANOSEC) {
3194 uint64_t now = gethrtime();
3196 uint64_t bytes = zcb->zcb_type[ZB_TOTAL][ZDB_OT_TOTAL].zb_asize;
3198 1 + bytes / (1 + ((now - zcb->zcb_start) / 1000 / 1000));
3200 (zcb->zcb_totalasize - bytes) / 1024 / kb_per_sec;
3202 /* make sure nicenum has enough space */
3203 CTASSERT(sizeof (buf) >= NN_NUMBUF_SZ);
3205 zfs_nicebytes(bytes, buf, sizeof (buf));
3206 (void) fprintf(stderr,
3207 "\r%5s completed (%4dMB/s) "
3208 "estimated time remaining: %uhr %02umin %02usec ",
3209 buf, kb_per_sec / 1024,
3210 sec_remaining / 60 / 60,
3211 sec_remaining / 60 % 60,
3212 sec_remaining % 60);
3214 zcb->zcb_lastprint = now;
3221 zdb_leak(void *arg, uint64_t start, uint64_t size)
3225 (void) printf("leaked space: vdev %llu, offset 0x%llx, size %llu\n",
3226 (u_longlong_t)vd->vdev_id, (u_longlong_t)start, (u_longlong_t)size);
3229 static metaslab_ops_t zdb_metaslab_ops = {
3234 zdb_ddt_leak_init(spa_t *spa, zdb_cb_t *zcb)
3241 bzero(&ddb, sizeof (ddb));
3242 while ((error = ddt_walk(spa, &ddb, &dde)) == 0) {
3244 ddt_phys_t *ddp = dde.dde_phys;
3246 if (ddb.ddb_class == DDT_CLASS_UNIQUE)
3249 ASSERT(ddt_phys_total_refcnt(&dde) > 1);
3251 for (p = 0; p < DDT_PHYS_TYPES; p++, ddp++) {
3252 if (ddp->ddp_phys_birth == 0)
3254 ddt_bp_create(ddb.ddb_checksum,
3255 &dde.dde_key, ddp, &blk);
3256 if (p == DDT_PHYS_DITTO) {
3257 zdb_count_block(zcb, NULL, &blk, ZDB_OT_DITTO);
3259 zcb->zcb_dedup_asize +=
3260 BP_GET_ASIZE(&blk) * (ddp->ddp_refcnt - 1);
3261 zcb->zcb_dedup_blocks++;
3264 if (!dump_opt['L']) {
3265 ddt_t *ddt = spa->spa_ddt[ddb.ddb_checksum];
3267 VERIFY(ddt_lookup(ddt, &blk, B_TRUE) != NULL);
3272 ASSERT(error == ENOENT);
3276 zdb_leak_init(spa_t *spa, zdb_cb_t *zcb)
3281 if (!dump_opt['L']) {
3282 vdev_t *rvd = spa->spa_root_vdev;
3285 * We are going to be changing the meaning of the metaslab's
3286 * ms_tree. Ensure that the allocator doesn't try to
3289 spa->spa_normal_class->mc_ops = &zdb_metaslab_ops;
3290 spa->spa_log_class->mc_ops = &zdb_metaslab_ops;
3292 for (c = 0; c < rvd->vdev_children; c++) {
3293 vdev_t *vd = rvd->vdev_child[c];
3294 ASSERTV(metaslab_group_t *mg = vd->vdev_mg);
3295 for (m = 0; m < vd->vdev_ms_count; m++) {
3296 metaslab_t *msp = vd->vdev_ms[m];
3297 ASSERT3P(msp->ms_group, ==, mg);
3298 mutex_enter(&msp->ms_lock);
3299 metaslab_unload(msp);
3302 * For leak detection, we overload the metaslab
3303 * ms_tree to contain allocated segments
3304 * instead of free segments. As a result,
3305 * we can't use the normal metaslab_load/unload
3308 if (msp->ms_sm != NULL) {
3309 (void) fprintf(stderr,
3310 "\rloading space map for "
3311 "vdev %llu of %llu, "
3312 "metaslab %llu of %llu ...",
3314 (longlong_t)rvd->vdev_children,
3316 (longlong_t)vd->vdev_ms_count);
3319 * We don't want to spend the CPU
3320 * manipulating the size-ordered
3321 * tree, so clear the range_tree
3324 msp->ms_tree->rt_ops = NULL;
3325 VERIFY0(space_map_load(msp->ms_sm,
3326 msp->ms_tree, SM_ALLOC));
3328 if (!msp->ms_loaded)
3329 msp->ms_loaded = B_TRUE;
3331 mutex_exit(&msp->ms_lock);
3334 (void) fprintf(stderr, "\n");
3337 spa_config_enter(spa, SCL_CONFIG, FTAG, RW_READER);
3339 zdb_ddt_leak_init(spa, zcb);
3341 spa_config_exit(spa, SCL_CONFIG, FTAG);
3345 zdb_leak_fini(spa_t *spa)
3347 if (!dump_opt['L']) {
3348 vdev_t *rvd = spa->spa_root_vdev;
3349 for (unsigned c = 0; c < rvd->vdev_children; c++) {
3350 vdev_t *vd = rvd->vdev_child[c];
3351 ASSERTV(metaslab_group_t *mg = vd->vdev_mg);
3352 for (unsigned m = 0; m < vd->vdev_ms_count; m++) {
3353 metaslab_t *msp = vd->vdev_ms[m];
3354 ASSERT3P(mg, ==, msp->ms_group);
3355 mutex_enter(&msp->ms_lock);
3358 * The ms_tree has been overloaded to
3359 * contain allocated segments. Now that we
3360 * finished traversing all blocks, any
3361 * block that remains in the ms_tree
3362 * represents an allocated block that we
3363 * did not claim during the traversal.
3364 * Claimed blocks would have been removed
3367 range_tree_vacate(msp->ms_tree, zdb_leak, vd);
3370 msp->ms_loaded = B_FALSE;
3372 mutex_exit(&msp->ms_lock);
3380 count_block_cb(void *arg, const blkptr_t *bp, dmu_tx_t *tx)
3382 zdb_cb_t *zcb = arg;
3384 if (dump_opt['b'] >= 5) {
3385 char blkbuf[BP_SPRINTF_LEN];
3386 snprintf_blkptr(blkbuf, sizeof (blkbuf), bp);
3387 (void) printf("[%s] %s\n",
3388 "deferred free", blkbuf);
3390 zdb_count_block(zcb, NULL, bp, ZDB_OT_DEFERRED);
3395 dump_block_stats(spa_t *spa)
3398 zdb_blkstats_t *zb, *tzb;
3399 uint64_t norm_alloc, norm_space, total_alloc, total_found;
3400 int flags = TRAVERSE_PRE | TRAVERSE_PREFETCH_METADATA |
3401 TRAVERSE_NO_DECRYPT | TRAVERSE_HARD;
3402 boolean_t leaks = B_FALSE;
3404 bp_embedded_type_t i;
3406 bzero(&zcb, sizeof (zcb));
3407 (void) printf("\nTraversing all blocks %s%s%s%s%s...\n\n",
3408 (dump_opt['c'] || !dump_opt['L']) ? "to verify " : "",
3409 (dump_opt['c'] == 1) ? "metadata " : "",
3410 dump_opt['c'] ? "checksums " : "",
3411 (dump_opt['c'] && !dump_opt['L']) ? "and verify " : "",
3412 !dump_opt['L'] ? "nothing leaked " : "");
3415 * Load all space maps as SM_ALLOC maps, then traverse the pool
3416 * claiming each block we discover. If the pool is perfectly
3417 * consistent, the space maps will be empty when we're done.
3418 * Anything left over is a leak; any block we can't claim (because
3419 * it's not part of any space map) is a double allocation,
3420 * reference to a freed block, or an unclaimed log block.
3422 bzero(&zcb, sizeof (zdb_cb_t));
3423 zdb_leak_init(spa, &zcb);
3426 * If there's a deferred-free bplist, process that first.
3428 (void) bpobj_iterate_nofree(&spa->spa_deferred_bpobj,
3429 count_block_cb, &zcb, NULL);
3430 if (spa_version(spa) >= SPA_VERSION_DEADLISTS) {
3431 (void) bpobj_iterate_nofree(&spa->spa_dsl_pool->dp_free_bpobj,
3432 count_block_cb, &zcb, NULL);
3434 if (spa_feature_is_active(spa, SPA_FEATURE_ASYNC_DESTROY)) {
3435 VERIFY3U(0, ==, bptree_iterate(spa->spa_meta_objset,
3436 spa->spa_dsl_pool->dp_bptree_obj, B_FALSE, count_block_cb,
3440 if (dump_opt['c'] > 1)
3441 flags |= TRAVERSE_PREFETCH_DATA;
3443 zcb.zcb_totalasize = metaslab_class_get_alloc(spa_normal_class(spa));
3444 zcb.zcb_start = zcb.zcb_lastprint = gethrtime();
3445 err = traverse_pool(spa, 0, flags, zdb_blkptr_cb, &zcb);
3448 * If we've traversed the data blocks then we need to wait for those
3449 * I/Os to complete. We leverage "The Godfather" zio to wait on
3450 * all async I/Os to complete.
3452 if (dump_opt['c']) {
3453 for (c = 0; c < max_ncpus; c++) {
3454 (void) zio_wait(spa->spa_async_zio_root[c]);
3455 spa->spa_async_zio_root[c] = zio_root(spa, NULL, NULL,
3456 ZIO_FLAG_CANFAIL | ZIO_FLAG_SPECULATIVE |
3457 ZIO_FLAG_GODFATHER);
3462 * Done after zio_wait() since zcb_haderrors is modified in
3465 zcb.zcb_haderrors |= err;
3467 if (zcb.zcb_haderrors) {
3468 (void) printf("\nError counts:\n\n");
3469 (void) printf("\t%5s %s\n", "errno", "count");
3470 for (e = 0; e < 256; e++) {
3471 if (zcb.zcb_errors[e] != 0) {
3472 (void) printf("\t%5d %llu\n",
3473 e, (u_longlong_t)zcb.zcb_errors[e]);
3479 * Report any leaked segments.
3483 tzb = &zcb.zcb_type[ZB_TOTAL][ZDB_OT_TOTAL];
3485 norm_alloc = metaslab_class_get_alloc(spa_normal_class(spa));
3486 norm_space = metaslab_class_get_space(spa_normal_class(spa));
3488 total_alloc = norm_alloc + metaslab_class_get_alloc(spa_log_class(spa));
3489 total_found = tzb->zb_asize - zcb.zcb_dedup_asize;
3491 if (total_found == total_alloc) {
3493 (void) printf("\n\tNo leaks (block sum matches space"
3494 " maps exactly)\n");
3496 (void) printf("block traversal size %llu != alloc %llu "
3498 (u_longlong_t)total_found,
3499 (u_longlong_t)total_alloc,
3500 (dump_opt['L']) ? "unreachable" : "leaked",
3501 (longlong_t)(total_alloc - total_found));
3505 if (tzb->zb_count == 0)
3508 (void) printf("\n");
3509 (void) printf("\tbp count: %10llu\n",
3510 (u_longlong_t)tzb->zb_count);
3511 (void) printf("\tganged count: %10llu\n",
3512 (longlong_t)tzb->zb_gangs);
3513 (void) printf("\tbp logical: %10llu avg: %6llu\n",
3514 (u_longlong_t)tzb->zb_lsize,
3515 (u_longlong_t)(tzb->zb_lsize / tzb->zb_count));
3516 (void) printf("\tbp physical: %10llu avg:"
3517 " %6llu compression: %6.2f\n",
3518 (u_longlong_t)tzb->zb_psize,
3519 (u_longlong_t)(tzb->zb_psize / tzb->zb_count),
3520 (double)tzb->zb_lsize / tzb->zb_psize);
3521 (void) printf("\tbp allocated: %10llu avg:"
3522 " %6llu compression: %6.2f\n",
3523 (u_longlong_t)tzb->zb_asize,
3524 (u_longlong_t)(tzb->zb_asize / tzb->zb_count),
3525 (double)tzb->zb_lsize / tzb->zb_asize);
3526 (void) printf("\tbp deduped: %10llu ref>1:"
3527 " %6llu deduplication: %6.2f\n",
3528 (u_longlong_t)zcb.zcb_dedup_asize,
3529 (u_longlong_t)zcb.zcb_dedup_blocks,
3530 (double)zcb.zcb_dedup_asize / tzb->zb_asize + 1.0);
3531 (void) printf("\tSPA allocated: %10llu used: %5.2f%%\n",
3532 (u_longlong_t)norm_alloc, 100.0 * norm_alloc / norm_space);
3534 for (i = 0; i < NUM_BP_EMBEDDED_TYPES; i++) {
3535 if (zcb.zcb_embedded_blocks[i] == 0)
3537 (void) printf("\n");
3538 (void) printf("\tadditional, non-pointer bps of type %u: "
3540 i, (u_longlong_t)zcb.zcb_embedded_blocks[i]);
3542 if (dump_opt['b'] >= 3) {
3543 (void) printf("\t number of (compressed) bytes: "
3545 dump_histogram(zcb.zcb_embedded_histogram[i],
3546 sizeof (zcb.zcb_embedded_histogram[i]) /
3547 sizeof (zcb.zcb_embedded_histogram[i][0]), 0);
3551 if (tzb->zb_ditto_samevdev != 0) {
3552 (void) printf("\tDittoed blocks on same vdev: %llu\n",
3553 (longlong_t)tzb->zb_ditto_samevdev);
3556 if (dump_opt['b'] >= 2) {
3558 (void) printf("\nBlocks\tLSIZE\tPSIZE\tASIZE"
3559 "\t avg\t comp\t%%Total\tType\n");
3561 for (t = 0; t <= ZDB_OT_TOTAL; t++) {
3562 char csize[32], lsize[32], psize[32], asize[32];
3563 char avg[32], gang[32];
3564 const char *typename;
3566 /* make sure nicenum has enough space */
3567 CTASSERT(sizeof (csize) >= NN_NUMBUF_SZ);
3568 CTASSERT(sizeof (lsize) >= NN_NUMBUF_SZ);
3569 CTASSERT(sizeof (psize) >= NN_NUMBUF_SZ);
3570 CTASSERT(sizeof (asize) >= NN_NUMBUF_SZ);
3571 CTASSERT(sizeof (avg) >= NN_NUMBUF_SZ);
3572 CTASSERT(sizeof (gang) >= NN_NUMBUF_SZ);
3574 if (t < DMU_OT_NUMTYPES)
3575 typename = dmu_ot[t].ot_name;
3577 typename = zdb_ot_extname[t - DMU_OT_NUMTYPES];
3579 if (zcb.zcb_type[ZB_TOTAL][t].zb_asize == 0) {
3580 (void) printf("%6s\t%5s\t%5s\t%5s"
3581 "\t%5s\t%5s\t%6s\t%s\n",
3593 for (l = ZB_TOTAL - 1; l >= -1; l--) {
3594 level = (l == -1 ? ZB_TOTAL : l);
3595 zb = &zcb.zcb_type[level][t];
3597 if (zb->zb_asize == 0)
3600 if (dump_opt['b'] < 3 && level != ZB_TOTAL)
3603 if (level == 0 && zb->zb_asize ==
3604 zcb.zcb_type[ZB_TOTAL][t].zb_asize)
3607 zdb_nicenum(zb->zb_count, csize,
3609 zdb_nicenum(zb->zb_lsize, lsize,
3611 zdb_nicenum(zb->zb_psize, psize,
3613 zdb_nicenum(zb->zb_asize, asize,
3615 zdb_nicenum(zb->zb_asize / zb->zb_count, avg,
3617 zdb_nicenum(zb->zb_gangs, gang, sizeof (gang));
3619 (void) printf("%6s\t%5s\t%5s\t%5s\t%5s"
3621 csize, lsize, psize, asize, avg,
3622 (double)zb->zb_lsize / zb->zb_psize,
3623 100.0 * zb->zb_asize / tzb->zb_asize);
3625 if (level == ZB_TOTAL)
3626 (void) printf("%s\n", typename);
3628 (void) printf(" L%d %s\n",
3631 if (dump_opt['b'] >= 3 && zb->zb_gangs > 0) {
3632 (void) printf("\t number of ganged "
3633 "blocks: %s\n", gang);
3636 if (dump_opt['b'] >= 4) {
3637 (void) printf("psize "
3638 "(in 512-byte sectors): "
3639 "number of blocks\n");
3640 dump_histogram(zb->zb_psize_histogram,
3641 PSIZE_HISTO_SIZE, 0);
3647 (void) printf("\n");
3652 if (zcb.zcb_haderrors)
3658 typedef struct zdb_ddt_entry {
3660 uint64_t zdde_ref_blocks;
3661 uint64_t zdde_ref_lsize;
3662 uint64_t zdde_ref_psize;
3663 uint64_t zdde_ref_dsize;
3664 avl_node_t zdde_node;
3669 zdb_ddt_add_cb(spa_t *spa, zilog_t *zilog, const blkptr_t *bp,
3670 const zbookmark_phys_t *zb, const dnode_phys_t *dnp, void *arg)
3672 avl_tree_t *t = arg;
3674 zdb_ddt_entry_t *zdde, zdde_search;
3676 if (bp == NULL || BP_IS_HOLE(bp) || BP_IS_EMBEDDED(bp))
3679 if (dump_opt['S'] > 1 && zb->zb_level == ZB_ROOT_LEVEL) {
3680 (void) printf("traversing objset %llu, %llu objects, "
3681 "%lu blocks so far\n",
3682 (u_longlong_t)zb->zb_objset,
3683 (u_longlong_t)BP_GET_FILL(bp),
3687 if (BP_IS_HOLE(bp) || BP_GET_CHECKSUM(bp) == ZIO_CHECKSUM_OFF ||
3688 BP_GET_LEVEL(bp) > 0 || DMU_OT_IS_METADATA(BP_GET_TYPE(bp)))
3691 ddt_key_fill(&zdde_search.zdde_key, bp);
3693 zdde = avl_find(t, &zdde_search, &where);
3696 zdde = umem_zalloc(sizeof (*zdde), UMEM_NOFAIL);
3697 zdde->zdde_key = zdde_search.zdde_key;
3698 avl_insert(t, zdde, where);
3701 zdde->zdde_ref_blocks += 1;
3702 zdde->zdde_ref_lsize += BP_GET_LSIZE(bp);
3703 zdde->zdde_ref_psize += BP_GET_PSIZE(bp);
3704 zdde->zdde_ref_dsize += bp_get_dsize_sync(spa, bp);
3710 dump_simulated_ddt(spa_t *spa)
3713 void *cookie = NULL;
3714 zdb_ddt_entry_t *zdde;
3715 ddt_histogram_t ddh_total;
3716 ddt_stat_t dds_total;
3718 bzero(&ddh_total, sizeof (ddh_total));
3719 bzero(&dds_total, sizeof (dds_total));
3720 avl_create(&t, ddt_entry_compare,
3721 sizeof (zdb_ddt_entry_t), offsetof(zdb_ddt_entry_t, zdde_node));
3723 spa_config_enter(spa, SCL_CONFIG, FTAG, RW_READER);
3725 (void) traverse_pool(spa, 0, TRAVERSE_PRE | TRAVERSE_PREFETCH_METADATA |
3726 TRAVERSE_NO_DECRYPT, zdb_ddt_add_cb, &t);
3728 spa_config_exit(spa, SCL_CONFIG, FTAG);
3730 while ((zdde = avl_destroy_nodes(&t, &cookie)) != NULL) {
3732 uint64_t refcnt = zdde->zdde_ref_blocks;
3733 ASSERT(refcnt != 0);
3735 dds.dds_blocks = zdde->zdde_ref_blocks / refcnt;
3736 dds.dds_lsize = zdde->zdde_ref_lsize / refcnt;
3737 dds.dds_psize = zdde->zdde_ref_psize / refcnt;
3738 dds.dds_dsize = zdde->zdde_ref_dsize / refcnt;
3740 dds.dds_ref_blocks = zdde->zdde_ref_blocks;
3741 dds.dds_ref_lsize = zdde->zdde_ref_lsize;
3742 dds.dds_ref_psize = zdde->zdde_ref_psize;
3743 dds.dds_ref_dsize = zdde->zdde_ref_dsize;
3745 ddt_stat_add(&ddh_total.ddh_stat[highbit64(refcnt) - 1],
3748 umem_free(zdde, sizeof (*zdde));
3753 ddt_histogram_stat(&dds_total, &ddh_total);
3755 (void) printf("Simulated DDT histogram:\n");
3757 zpool_dump_ddt(&dds_total, &ddh_total);
3759 dump_dedup_ratio(&dds_total);
3763 dump_zpool(spa_t *spa)
3765 dsl_pool_t *dp = spa_get_dsl(spa);
3768 if (dump_opt['S']) {
3769 dump_simulated_ddt(spa);
3773 if (!dump_opt['e'] && dump_opt['C'] > 1) {
3774 (void) printf("\nCached configuration:\n");
3775 dump_nvlist(spa->spa_config, 8);
3782 dump_uberblock(&spa->spa_uberblock, "\nUberblock:\n", "\n");
3787 if (dump_opt['d'] > 2 || dump_opt['m'])
3788 dump_metaslabs(spa);
3790 dump_metaslab_groups(spa);
3792 if (dump_opt['d'] || dump_opt['i']) {
3795 dump_dir(dp->dp_meta_objset);
3796 if (dump_opt['d'] >= 3) {
3797 dump_full_bpobj(&spa->spa_deferred_bpobj,
3798 "Deferred frees", 0);
3799 if (spa_version(spa) >= SPA_VERSION_DEADLISTS) {
3801 &spa->spa_dsl_pool->dp_free_bpobj,
3802 "Pool snapshot frees", 0);
3805 if (spa_feature_is_active(spa,
3806 SPA_FEATURE_ASYNC_DESTROY)) {
3807 dump_bptree(spa->spa_meta_objset,
3808 spa->spa_dsl_pool->dp_bptree_obj,
3809 "Pool dataset frees");
3811 dump_dtl(spa->spa_root_vdev, 0);
3813 (void) dmu_objset_find(spa_name(spa), dump_one_dir,
3814 NULL, DS_FIND_SNAPSHOTS | DS_FIND_CHILDREN);
3816 for (f = 0; f < SPA_FEATURES; f++) {
3819 if (!(spa_feature_table[f].fi_flags &
3820 ZFEATURE_FLAG_PER_DATASET) ||
3821 !spa_feature_is_enabled(spa, f)) {
3822 ASSERT0(dataset_feature_count[f]);
3825 if (feature_get_refcount(spa, &spa_feature_table[f],
3826 &refcount) == ENOTSUP)
3828 if (dataset_feature_count[f] != refcount) {
3829 (void) printf("%s feature refcount mismatch: "
3830 "%lld datasets != %lld refcount\n",
3831 spa_feature_table[f].fi_uname,
3832 (longlong_t)dataset_feature_count[f],
3833 (longlong_t)refcount);
3836 (void) printf("Verified %s feature refcount "
3837 "of %llu is correct\n",
3838 spa_feature_table[f].fi_uname,
3839 (longlong_t)refcount);
3843 if (rc == 0 && (dump_opt['b'] || dump_opt['c']))
3844 rc = dump_block_stats(spa);
3847 rc = verify_spacemap_refcounts(spa);
3850 show_pool_stats(spa);
3856 dump_debug_buffer();
3861 #define ZDB_FLAG_CHECKSUM 0x0001
3862 #define ZDB_FLAG_DECOMPRESS 0x0002
3863 #define ZDB_FLAG_BSWAP 0x0004
3864 #define ZDB_FLAG_GBH 0x0008
3865 #define ZDB_FLAG_INDIRECT 0x0010
3866 #define ZDB_FLAG_PHYS 0x0020
3867 #define ZDB_FLAG_RAW 0x0040
3868 #define ZDB_FLAG_PRINT_BLKPTR 0x0080
3870 static int flagbits[256];
3873 zdb_print_blkptr(blkptr_t *bp, int flags)
3875 char blkbuf[BP_SPRINTF_LEN];
3877 if (flags & ZDB_FLAG_BSWAP)
3878 byteswap_uint64_array((void *)bp, sizeof (blkptr_t));
3880 snprintf_blkptr(blkbuf, sizeof (blkbuf), bp);
3881 (void) printf("%s\n", blkbuf);
3885 zdb_dump_indirect(blkptr_t *bp, int nbps, int flags)
3889 for (i = 0; i < nbps; i++)
3890 zdb_print_blkptr(&bp[i], flags);
3894 zdb_dump_gbh(void *buf, int flags)
3896 zdb_dump_indirect((blkptr_t *)buf, SPA_GBH_NBLKPTRS, flags);
3900 zdb_dump_block_raw(void *buf, uint64_t size, int flags)
3902 if (flags & ZDB_FLAG_BSWAP)
3903 byteswap_uint64_array(buf, size);
3904 VERIFY(write(fileno(stdout), buf, size) == size);
3908 zdb_dump_block(char *label, void *buf, uint64_t size, int flags)
3910 uint64_t *d = (uint64_t *)buf;
3911 unsigned nwords = size / sizeof (uint64_t);
3912 int do_bswap = !!(flags & ZDB_FLAG_BSWAP);
3919 hdr = " 7 6 5 4 3 2 1 0 f e d c b a 9 8";
3921 hdr = " 0 1 2 3 4 5 6 7 8 9 a b c d e f";
3923 (void) printf("\n%s\n%6s %s 0123456789abcdef\n", label, "", hdr);
3925 #ifdef _LITTLE_ENDIAN
3926 /* correct the endianness */
3927 do_bswap = !do_bswap;
3929 for (i = 0; i < nwords; i += 2) {
3930 (void) printf("%06llx: %016llx %016llx ",
3931 (u_longlong_t)(i * sizeof (uint64_t)),
3932 (u_longlong_t)(do_bswap ? BSWAP_64(d[i]) : d[i]),
3933 (u_longlong_t)(do_bswap ? BSWAP_64(d[i + 1]) : d[i + 1]));
3936 for (j = 0; j < 2 * sizeof (uint64_t); j++)
3937 (void) printf("%c", isprint(c[j]) ? c[j] : '.');
3938 (void) printf("\n");
3943 * There are two acceptable formats:
3944 * leaf_name - For example: c1t0d0 or /tmp/ztest.0a
3945 * child[.child]* - For example: 0.1.1
3947 * The second form can be used to specify arbitrary vdevs anywhere
3948 * in the hierarchy. For example, in a pool with a mirror of
3949 * RAID-Zs, you can specify either RAID-Z vdev with 0.0 or 0.1 .
3952 zdb_vdev_lookup(vdev_t *vdev, const char *path)
3960 /* First, assume the x.x.x.x format */
3961 i = strtoul(path, &s, 10);
3962 if (s == path || (s && *s != '.' && *s != '\0'))
3964 if (i >= vdev->vdev_children)
3967 vdev = vdev->vdev_child[i];
3968 if (s && *s == '\0')
3970 return (zdb_vdev_lookup(vdev, s+1));
3973 for (i = 0; i < vdev->vdev_children; i++) {
3974 vdev_t *vc = vdev->vdev_child[i];
3976 if (vc->vdev_path == NULL) {
3977 vc = zdb_vdev_lookup(vc, path);
3984 p = strrchr(vc->vdev_path, '/');
3985 p = p ? p + 1 : vc->vdev_path;
3986 q = &vc->vdev_path[strlen(vc->vdev_path) - 2];
3988 if (strcmp(vc->vdev_path, path) == 0)
3990 if (strcmp(p, path) == 0)
3992 if (strcmp(q, "s0") == 0 && strncmp(p, path, q - p) == 0)
4000 * Read a block from a pool and print it out. The syntax of the
4001 * block descriptor is:
4003 * pool:vdev_specifier:offset:size[:flags]
4005 * pool - The name of the pool you wish to read from
4006 * vdev_specifier - Which vdev (see comment for zdb_vdev_lookup)
4007 * offset - offset, in hex, in bytes
4008 * size - Amount of data to read, in hex, in bytes
4009 * flags - A string of characters specifying options
4010 * b: Decode a blkptr at given offset within block
4011 * *c: Calculate and display checksums
4012 * d: Decompress data before dumping
4013 * e: Byteswap data before dumping
4014 * g: Display data as a gang block header
4015 * i: Display as an indirect block
4016 * p: Do I/O to physical offset
4017 * r: Dump raw data to stdout
4019 * * = not yet implemented
4022 zdb_read_block(char *thing, spa_t *spa)
4024 blkptr_t blk, *bp = &blk;
4025 dva_t *dva = bp->blk_dva;
4027 uint64_t offset = 0, size = 0, psize = 0, lsize = 0, blkptr_offset = 0;
4032 const char *s, *vdev;
4033 char *p, *dup, *flagstr;
4035 boolean_t borrowed = B_FALSE;
4037 dup = strdup(thing);
4038 s = strtok(dup, ":");
4040 s = strtok(NULL, ":");
4041 offset = strtoull(s ? s : "", NULL, 16);
4042 s = strtok(NULL, ":");
4043 size = strtoull(s ? s : "", NULL, 16);
4044 s = strtok(NULL, ":");
4046 flagstr = strdup(s);
4048 flagstr = strdup("");
4052 s = "size must not be zero";
4053 if (!IS_P2ALIGNED(size, DEV_BSIZE))
4054 s = "size must be a multiple of sector size";
4055 if (!IS_P2ALIGNED(offset, DEV_BSIZE))
4056 s = "offset must be a multiple of sector size";
4058 (void) printf("Invalid block specifier: %s - %s\n", thing, s);
4064 for (s = strtok(flagstr, ":"); s; s = strtok(NULL, ":")) {
4065 for (i = 0; flagstr[i]; i++) {
4066 int bit = flagbits[(uchar_t)flagstr[i]];
4069 (void) printf("***Invalid flag: %c\n",
4075 /* If it's not something with an argument, keep going */
4076 if ((bit & (ZDB_FLAG_CHECKSUM |
4077 ZDB_FLAG_PRINT_BLKPTR)) == 0)
4080 p = &flagstr[i + 1];
4081 if (bit == ZDB_FLAG_PRINT_BLKPTR) {
4082 blkptr_offset = strtoull(p, &p, 16);
4083 i = p - &flagstr[i + 1];
4085 if (*p != ':' && *p != '\0') {
4086 (void) printf("***Invalid flag arg: '%s'\n", s);
4095 vd = zdb_vdev_lookup(spa->spa_root_vdev, vdev);
4097 (void) printf("***Invalid vdev: %s\n", vdev);
4102 (void) fprintf(stderr, "Found vdev: %s\n",
4105 (void) fprintf(stderr, "Found vdev type: %s\n",
4106 vd->vdev_ops->vdev_op_type);
4112 pabd = abd_alloc_for_io(SPA_MAXBLOCKSIZE, B_FALSE);
4113 lbuf = umem_alloc(SPA_MAXBLOCKSIZE, UMEM_NOFAIL);
4117 DVA_SET_VDEV(&dva[0], vd->vdev_id);
4118 DVA_SET_OFFSET(&dva[0], offset);
4119 DVA_SET_GANG(&dva[0], !!(flags & ZDB_FLAG_GBH));
4120 DVA_SET_ASIZE(&dva[0], vdev_psize_to_asize(vd, psize));
4122 BP_SET_BIRTH(bp, TXG_INITIAL, TXG_INITIAL);
4124 BP_SET_LSIZE(bp, lsize);
4125 BP_SET_PSIZE(bp, psize);
4126 BP_SET_COMPRESS(bp, ZIO_COMPRESS_OFF);
4127 BP_SET_CHECKSUM(bp, ZIO_CHECKSUM_OFF);
4128 BP_SET_TYPE(bp, DMU_OT_NONE);
4129 BP_SET_LEVEL(bp, 0);
4130 BP_SET_DEDUP(bp, 0);
4131 BP_SET_BYTEORDER(bp, ZFS_HOST_BYTEORDER);
4133 spa_config_enter(spa, SCL_STATE, FTAG, RW_READER);
4134 zio = zio_root(spa, NULL, NULL, 0);
4136 if (vd == vd->vdev_top) {
4138 * Treat this as a normal block read.
4140 zio_nowait(zio_read(zio, spa, bp, pabd, psize, NULL, NULL,
4141 ZIO_PRIORITY_SYNC_READ,
4142 ZIO_FLAG_CANFAIL | ZIO_FLAG_RAW, NULL));
4145 * Treat this as a vdev child I/O.
4147 zio_nowait(zio_vdev_child_io(zio, bp, vd, offset, pabd,
4148 psize, ZIO_TYPE_READ, ZIO_PRIORITY_SYNC_READ,
4149 ZIO_FLAG_DONT_CACHE | ZIO_FLAG_DONT_QUEUE |
4150 ZIO_FLAG_DONT_PROPAGATE | ZIO_FLAG_DONT_RETRY |
4151 ZIO_FLAG_CANFAIL | ZIO_FLAG_RAW, NULL, NULL));
4154 error = zio_wait(zio);
4155 spa_config_exit(spa, SCL_STATE, FTAG);
4158 (void) printf("Read of %s failed, error: %d\n", thing, error);
4162 if (flags & ZDB_FLAG_DECOMPRESS) {
4164 * We don't know how the data was compressed, so just try
4165 * every decompress function at every inflated blocksize.
4167 enum zio_compress c;
4168 void *lbuf2 = umem_alloc(SPA_MAXBLOCKSIZE, UMEM_NOFAIL);
4171 * XXX - On the one hand, with SPA_MAXBLOCKSIZE at 16MB,
4172 * this could take a while and we should let the user know
4173 * we are not stuck. On the other hand, printing progress
4174 * info gets old after a while. What to do?
4176 for (lsize = psize + SPA_MINBLOCKSIZE;
4177 lsize <= SPA_MAXBLOCKSIZE; lsize += SPA_MINBLOCKSIZE) {
4178 for (c = 0; c < ZIO_COMPRESS_FUNCTIONS; c++) {
4180 * ZLE can easily decompress non zle stream.
4181 * So have an option to disable it.
4183 if (c == ZIO_COMPRESS_ZLE &&
4184 getenv("ZDB_NO_ZLE"))
4187 (void) fprintf(stderr,
4188 "Trying %05llx -> %05llx (%s)\n",
4189 (u_longlong_t)psize, (u_longlong_t)lsize,
4190 zio_compress_table[c].ci_name);
4193 * We randomize lbuf2, and decompress to both
4194 * lbuf and lbuf2. This way, we will know if
4195 * decompression fill exactly to lsize.
4197 VERIFY0(random_get_pseudo_bytes(lbuf2, lsize));
4199 if (zio_decompress_data(c, pabd,
4200 lbuf, psize, lsize) == 0 &&
4201 zio_decompress_data(c, pabd,
4202 lbuf2, psize, lsize) == 0 &&
4203 bcmp(lbuf, lbuf2, lsize) == 0)
4206 if (c != ZIO_COMPRESS_FUNCTIONS)
4209 umem_free(lbuf2, SPA_MAXBLOCKSIZE);
4211 if (lsize > SPA_MAXBLOCKSIZE) {
4212 (void) printf("Decompress of %s failed\n", thing);
4219 buf = abd_borrow_buf_copy(pabd, size);
4223 if (flags & ZDB_FLAG_PRINT_BLKPTR)
4224 zdb_print_blkptr((blkptr_t *)(void *)
4225 ((uintptr_t)buf + (uintptr_t)blkptr_offset), flags);
4226 else if (flags & ZDB_FLAG_RAW)
4227 zdb_dump_block_raw(buf, size, flags);
4228 else if (flags & ZDB_FLAG_INDIRECT)
4229 zdb_dump_indirect((blkptr_t *)buf, size / sizeof (blkptr_t),
4231 else if (flags & ZDB_FLAG_GBH)
4232 zdb_dump_gbh(buf, flags);
4234 zdb_dump_block(thing, buf, size, flags);
4237 abd_return_buf_copy(pabd, buf, size);
4241 umem_free(lbuf, SPA_MAXBLOCKSIZE);
4246 zdb_embedded_block(char *thing)
4249 unsigned long long *words = (void *)&bp;
4253 buf = umem_alloc(SPA_MAXBLOCKSIZE, UMEM_NOFAIL);
4255 bzero(&bp, sizeof (bp));
4256 err = sscanf(thing, "%llx:%llx:%llx:%llx:%llx:%llx:%llx:%llx:"
4257 "%llx:%llx:%llx:%llx:%llx:%llx:%llx:%llx",
4258 words + 0, words + 1, words + 2, words + 3,
4259 words + 4, words + 5, words + 6, words + 7,
4260 words + 8, words + 9, words + 10, words + 11,
4261 words + 12, words + 13, words + 14, words + 15);
4263 (void) printf("invalid input format\n");
4266 ASSERT3U(BPE_GET_LSIZE(&bp), <=, SPA_MAXBLOCKSIZE);
4267 err = decode_embedded_bp(&bp, buf, BPE_GET_LSIZE(&bp));
4269 (void) printf("decode failed: %u\n", err);
4272 zdb_dump_block_raw(buf, BPE_GET_LSIZE(&bp), 0);
4273 umem_free(buf, SPA_MAXBLOCKSIZE);
4277 main(int argc, char **argv)
4280 struct rlimit rl = { 1024, 1024 };
4282 objset_t *os = NULL;
4286 char **searchdirs = NULL;
4288 char *target, *target_pool;
4289 nvlist_t *policy = NULL;
4290 uint64_t max_txg = UINT64_MAX;
4291 int flags = ZFS_IMPORT_MISSING_LOG;
4292 int rewind = ZPOOL_NEVER_REWIND;
4293 char *spa_config_path_env;
4294 boolean_t target_is_spa = B_TRUE;
4296 (void) setrlimit(RLIMIT_NOFILE, &rl);
4297 (void) enable_extended_FILE_stdio(-1, -1);
4299 dprintf_setup(&argc, argv);
4302 * If there is an environment variable SPA_CONFIG_PATH it overrides
4303 * default spa_config_path setting. If -U flag is specified it will
4304 * override this environment variable settings once again.
4306 spa_config_path_env = getenv("SPA_CONFIG_PATH");
4307 if (spa_config_path_env != NULL)
4308 spa_config_path = spa_config_path_env;
4310 while ((c = getopt(argc, argv,
4311 "AbcCdDeEFGhiI:lLmMo:Op:PqRsSt:uU:vVx:X")) != -1) {
4342 /* NB: Sort single match options below. */
4344 max_inflight = strtoull(optarg, NULL, 0);
4345 if (max_inflight == 0) {
4346 (void) fprintf(stderr, "maximum number "
4347 "of inflight I/Os must be greater "
4353 error = set_global_var(optarg);
4358 if (searchdirs == NULL) {
4359 searchdirs = umem_alloc(sizeof (char *),
4362 char **tmp = umem_alloc((nsearch + 1) *
4363 sizeof (char *), UMEM_NOFAIL);
4364 bcopy(searchdirs, tmp, nsearch *
4366 umem_free(searchdirs,
4367 nsearch * sizeof (char *));
4370 searchdirs[nsearch++] = optarg;
4373 max_txg = strtoull(optarg, NULL, 0);
4374 if (max_txg < TXG_INITIAL) {
4375 (void) fprintf(stderr, "incorrect txg "
4376 "specified: %s\n", optarg);
4381 spa_config_path = optarg;
4382 if (spa_config_path[0] != '/') {
4383 (void) fprintf(stderr,
4384 "cachefile must be an absolute path "
4385 "(i.e. start with a slash)\n");
4393 flags = ZFS_IMPORT_VERBATIM;
4396 vn_dumpdir = optarg;
4404 if (!dump_opt['e'] && searchdirs != NULL) {
4405 (void) fprintf(stderr, "-p option requires use of -e\n");
4411 * ZDB does not typically re-read blocks; therefore limit the ARC
4412 * to 256 MB, which can be used entirely for metadata.
4414 zfs_arc_max = zfs_arc_meta_limit = 256 * 1024 * 1024;
4418 * "zdb -c" uses checksum-verifying scrub i/os which are async reads.
4419 * "zdb -b" uses traversal prefetch which uses async reads.
4420 * For good performance, let several of them be active at once.
4422 zfs_vdev_async_read_max_active = 10;
4425 * Disable reference tracking for better performance.
4427 reference_tracking_enable = B_FALSE;
4430 if ((g_zfs = libzfs_init()) == NULL) {
4431 (void) fprintf(stderr, "%s", libzfs_error_init(errno));
4436 verbose = MAX(verbose, 1);
4438 for (c = 0; c < 256; c++) {
4439 if (dump_all && strchr("AeEFlLOPRSX", c) == NULL)
4442 dump_opt[c] += verbose;
4445 aok = (dump_opt['A'] == 1) || (dump_opt['A'] > 2);
4446 zfs_recover = (dump_opt['A'] > 1);
4451 if (argc < 2 && dump_opt['R'])
4454 if (dump_opt['E']) {
4457 zdb_embedded_block(argv[0]);
4462 if (!dump_opt['e'] && dump_opt['C']) {
4463 dump_cachefile(spa_config_path);
4470 return (dump_label(argv[0]));
4472 if (dump_opt['O']) {
4475 dump_opt['v'] = verbose + 3;
4476 return (dump_path(argv[0], argv[1]));
4479 if (dump_opt['X'] || dump_opt['F'])
4480 rewind = ZPOOL_DO_REWIND |
4481 (dump_opt['X'] ? ZPOOL_EXTREME_REWIND : 0);
4483 if (nvlist_alloc(&policy, NV_UNIQUE_NAME_TYPE, 0) != 0 ||
4484 nvlist_add_uint64(policy, ZPOOL_REWIND_REQUEST_TXG, max_txg) != 0 ||
4485 nvlist_add_uint32(policy, ZPOOL_REWIND_REQUEST, rewind) != 0)
4486 fatal("internal error: %s", strerror(ENOMEM));
4491 if (strpbrk(target, "/@") != NULL) {
4494 target_pool = strdup(target);
4495 *strpbrk(target_pool, "/@") = '\0';
4497 target_is_spa = B_FALSE;
4498 targetlen = strlen(target);
4499 if (targetlen && target[targetlen - 1] == '/')
4500 target[targetlen - 1] = '\0';
4502 target_pool = target;
4505 if (dump_opt['e']) {
4506 importargs_t args = { 0 };
4507 nvlist_t *cfg = NULL;
4509 args.paths = nsearch;
4510 args.path = searchdirs;
4511 args.can_be_active = B_TRUE;
4513 error = zpool_tryimport(g_zfs, target_pool, &cfg, &args);
4517 if (nvlist_add_nvlist(cfg,
4518 ZPOOL_REWIND_POLICY, policy) != 0) {
4519 fatal("can't open '%s': %s",
4520 target, strerror(ENOMEM));
4524 * Disable the activity check to allow examination of
4527 if (dump_opt['C'] > 1) {
4528 (void) printf("\nConfiguration for import:\n");
4529 dump_nvlist(cfg, 8);
4531 error = spa_import(target_pool, cfg, NULL,
4532 flags | ZFS_IMPORT_SKIP_MMP);
4536 if (target_pool != target)
4540 if (target_is_spa || dump_opt['R']) {
4542 * Disable the activity check to allow examination of
4545 mutex_enter(&spa_namespace_lock);
4546 if ((spa = spa_lookup(target)) != NULL) {
4547 spa->spa_import_flags |= ZFS_IMPORT_SKIP_MMP;
4549 mutex_exit(&spa_namespace_lock);
4551 error = spa_open_rewind(target, &spa, FTAG, policy,
4555 * If we're missing the log device then
4556 * try opening the pool after clearing the
4559 mutex_enter(&spa_namespace_lock);
4560 if ((spa = spa_lookup(target)) != NULL &&
4561 spa->spa_log_state == SPA_LOG_MISSING) {
4562 spa->spa_log_state = SPA_LOG_CLEAR;
4565 mutex_exit(&spa_namespace_lock);
4568 error = spa_open_rewind(target, &spa,
4569 FTAG, policy, NULL);
4573 error = open_objset(target, DMU_OST_ANY, FTAG, &os);
4575 spa = dmu_objset_spa(os);
4578 nvlist_free(policy);
4581 fatal("can't open '%s': %s", target, strerror(error));
4584 * Set the pool failure mode to panic in order to prevent the pool
4585 * from suspending. A suspended I/O will have no way to resume and
4586 * can prevent the zdb(8) command from terminating as expected.
4589 spa->spa_failmode = ZIO_FAILURE_MODE_PANIC;
4593 if (!dump_opt['R']) {
4595 zopt_objects = argc;
4596 zopt_object = calloc(zopt_objects, sizeof (uint64_t));
4597 for (unsigned i = 0; i < zopt_objects; i++) {
4599 zopt_object[i] = strtoull(argv[i], NULL, 0);
4600 if (zopt_object[i] == 0 && errno != 0)
4601 fatal("bad number %s: %s",
4602 argv[i], strerror(errno));
4607 } else if (zopt_objects > 0 && !dump_opt['m']) {
4608 dump_dir(spa->spa_meta_objset);
4613 flagbits['b'] = ZDB_FLAG_PRINT_BLKPTR;
4614 flagbits['c'] = ZDB_FLAG_CHECKSUM;
4615 flagbits['d'] = ZDB_FLAG_DECOMPRESS;
4616 flagbits['e'] = ZDB_FLAG_BSWAP;
4617 flagbits['g'] = ZDB_FLAG_GBH;
4618 flagbits['i'] = ZDB_FLAG_INDIRECT;
4619 flagbits['p'] = ZDB_FLAG_PHYS;
4620 flagbits['r'] = ZDB_FLAG_RAW;
4622 for (int i = 0; i < argc; i++)
4623 zdb_read_block(argv[i], spa);
4627 close_objset(os, FTAG);
4629 spa_close(spa, FTAG);
4631 fuid_table_destroy();
4633 dump_debug_buffer();