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]
30 #include <stdio_ext.h>
33 #include <sys/zfs_context.h>
35 #include <sys/spa_impl.h>
38 #include <sys/fs/zfs.h>
39 #include <sys/zfs_znode.h>
40 #include <sys/zfs_sa.h>
42 #include <sys/sa_impl.h>
44 #include <sys/vdev_impl.h>
45 #include <sys/metaslab_impl.h>
46 #include <sys/dmu_objset.h>
47 #include <sys/dsl_dir.h>
48 #include <sys/dsl_dataset.h>
49 #include <sys/dsl_pool.h>
52 #include <sys/zil_impl.h>
54 #include <sys/resource.h>
55 #include <sys/dmu_traverse.h>
56 #include <sys/zio_checksum.h>
57 #include <sys/zio_compress.h>
58 #include <sys/zfs_fuid.h>
61 #include <sys/zfeature.h>
62 #include <zfs_comutil.h>
63 #include <libcmdutils.h>
67 #define ZDB_COMPRESS_NAME(idx) ((idx) < ZIO_COMPRESS_FUNCTIONS ? \
68 zio_compress_table[(idx)].ci_name : "UNKNOWN")
69 #define ZDB_CHECKSUM_NAME(idx) ((idx) < ZIO_CHECKSUM_FUNCTIONS ? \
70 zio_checksum_table[(idx)].ci_name : "UNKNOWN")
71 #define ZDB_OT_NAME(idx) ((idx) < DMU_OT_NUMTYPES ? \
72 dmu_ot[(idx)].ot_name : DMU_OT_IS_VALID(idx) ? \
73 dmu_ot_byteswap[DMU_OT_BYTESWAP(idx)].ob_name : "UNKNOWN")
74 #define ZDB_OT_TYPE(idx) ((idx) < DMU_OT_NUMTYPES ? (idx) : \
75 (((idx) == DMU_OTN_ZAP_DATA || (idx) == DMU_OTN_ZAP_METADATA) ? \
76 DMU_OT_ZAP_OTHER : DMU_OT_NUMTYPES))
79 extern boolean_t zfs_recover;
80 extern uint64_t zfs_arc_max, zfs_arc_meta_limit;
81 extern int zfs_vdev_async_read_max_active;
83 boolean_t zfs_recover;
84 uint64_t zfs_arc_max, zfs_arc_meta_limit;
85 int zfs_vdev_async_read_max_active;
88 const char cmdname[] = "zdb";
89 uint8_t dump_opt[256];
91 typedef void object_viewer_t(objset_t *, uint64_t, void *data, size_t size);
93 extern void dump_intent_log(zilog_t *);
94 uint64_t *zopt_object = NULL;
96 libzfs_handle_t *g_zfs;
97 uint64_t max_inflight = 1000;
99 static void snprintf_blkptr_compact(char *, size_t, const blkptr_t *);
102 * These libumem hooks provide a reasonable set of defaults for the allocator's
103 * debugging facilities.
108 return ("default,verbose"); /* $UMEM_DEBUG setting */
112 _umem_logging_init(void)
114 return ("fail,contents"); /* $UMEM_LOGGING setting */
120 (void) fprintf(stderr,
121 "Usage: %s [-CumMdibcsDvhLXFPAG] [-t txg] [-e [-p path...]] "
122 "[-U config] [-I inflight I/Os] [-x dumpdir] poolname [object...]\n"
123 " %s [-divPA] [-e -p path...] [-U config] dataset "
125 " %s -mM [-LXFPA] [-t txg] [-e [-p path...]] [-U config] "
126 "poolname [vdev [metaslab...]]\n"
127 " %s -R [-A] [-e [-p path...]] poolname "
128 "vdev:offset:size[:flags]\n"
129 " %s -S [-PA] [-e [-p path...]] [-U config] poolname\n"
130 " %s -l [-uA] device\n"
131 " %s -C [-A] [-U config]\n\n",
132 cmdname, cmdname, cmdname, cmdname, cmdname, cmdname, cmdname);
134 (void) fprintf(stderr, " Dataset name must include at least one "
135 "separator character '/' or '@'\n");
136 (void) fprintf(stderr, " If dataset name is specified, only that "
137 "dataset is dumped\n");
138 (void) fprintf(stderr, " If object numbers are specified, only "
139 "those objects are dumped\n\n");
140 (void) fprintf(stderr, " Options to control amount of output:\n");
141 (void) fprintf(stderr, " -u uberblock\n");
142 (void) fprintf(stderr, " -d dataset(s)\n");
143 (void) fprintf(stderr, " -i intent logs\n");
144 (void) fprintf(stderr, " -C config (or cachefile if alone)\n");
145 (void) fprintf(stderr, " -h pool history\n");
146 (void) fprintf(stderr, " -b block statistics\n");
147 (void) fprintf(stderr, " -m metaslabs\n");
148 (void) fprintf(stderr, " -M metaslab groups\n");
149 (void) fprintf(stderr, " -c checksum all metadata (twice for "
150 "all data) blocks\n");
151 (void) fprintf(stderr, " -s report stats on zdb's I/O\n");
152 (void) fprintf(stderr, " -D dedup statistics\n");
153 (void) fprintf(stderr, " -S simulate dedup to measure effect\n");
154 (void) fprintf(stderr, " -v verbose (applies to all others)\n");
155 (void) fprintf(stderr, " -l dump label contents\n");
156 (void) fprintf(stderr, " -L disable leak tracking (do not "
157 "load spacemaps)\n");
158 (void) fprintf(stderr, " -R read and display block from a "
160 (void) fprintf(stderr, " Below options are intended for use "
161 "with other options:\n");
162 (void) fprintf(stderr, " -A ignore assertions (-A), enable "
163 "panic recovery (-AA) or both (-AAA)\n");
164 (void) fprintf(stderr, " -F attempt automatic rewind within "
165 "safe range of transaction groups\n");
166 (void) fprintf(stderr, " -U <cachefile_path> -- use alternate "
168 (void) fprintf(stderr, " -X attempt extreme rewind (does not "
169 "work with dataset)\n");
170 (void) fprintf(stderr, " -e pool is exported/destroyed/"
171 "has altroot/not in a cachefile\n");
172 (void) fprintf(stderr, " -p <path> -- use one or more with "
173 "-e to specify path to vdev dir\n");
174 (void) fprintf(stderr, " -x <dumpdir> -- "
175 "dump all read blocks into specified directory\n");
176 (void) fprintf(stderr, " -P print numbers in parseable form\n");
177 (void) fprintf(stderr, " -t <txg> -- highest txg to use when "
178 "searching for uberblocks\n");
179 (void) fprintf(stderr, " -I <number of inflight I/Os> -- "
180 "specify the maximum number of "
181 "checksumming I/Os [default is 200]\n");
182 (void) fprintf(stderr, " -G dump zfs_dbgmsg buffer before "
184 (void) fprintf(stderr, "Specify an option more than once (e.g. -bb) "
185 "to make only that option verbose\n");
186 (void) fprintf(stderr, "Default is to dump everything non-verbosely\n");
195 zfs_dbgmsg_print("zdb");
200 * Called for usage errors that are discovered after a call to spa_open(),
201 * dmu_bonus_hold(), or pool_match(). abort() is called for other errors.
205 fatal(const char *fmt, ...)
210 (void) fprintf(stderr, "%s: ", cmdname);
211 (void) vfprintf(stderr, fmt, ap);
213 (void) fprintf(stderr, "\n");
222 dump_packed_nvlist(objset_t *os, uint64_t object, void *data, size_t size)
225 size_t nvsize = *(uint64_t *)data;
226 char *packed = umem_alloc(nvsize, UMEM_NOFAIL);
228 VERIFY(0 == dmu_read(os, object, 0, nvsize, packed, DMU_READ_PREFETCH));
230 VERIFY(nvlist_unpack(packed, nvsize, &nv, 0) == 0);
232 umem_free(packed, nvsize);
241 dump_history_offsets(objset_t *os, uint64_t object, void *data, size_t size)
243 spa_history_phys_t *shp = data;
248 (void) printf("\t\tpool_create_len = %llu\n",
249 (u_longlong_t)shp->sh_pool_create_len);
250 (void) printf("\t\tphys_max_off = %llu\n",
251 (u_longlong_t)shp->sh_phys_max_off);
252 (void) printf("\t\tbof = %llu\n",
253 (u_longlong_t)shp->sh_bof);
254 (void) printf("\t\teof = %llu\n",
255 (u_longlong_t)shp->sh_eof);
256 (void) printf("\t\trecords_lost = %llu\n",
257 (u_longlong_t)shp->sh_records_lost);
261 zdb_nicenum(uint64_t num, char *buf, size_t buflen)
264 (void) snprintf(buf, buflen, "%llu", (longlong_t)num);
266 nicenum(num, buf, sizeof (buf));
269 const char histo_stars[] = "****************************************";
270 const int histo_width = sizeof (histo_stars) - 1;
273 dump_histogram(const uint64_t *histo, int size, int offset)
276 int minidx = size - 1;
280 for (i = 0; i < size; i++) {
283 if (histo[i] > 0 && i > maxidx)
285 if (histo[i] > 0 && i < minidx)
289 if (max < histo_width)
292 for (i = minidx; i <= maxidx; i++) {
293 (void) printf("\t\t\t%3u: %6llu %s\n",
294 i + offset, (u_longlong_t)histo[i],
295 &histo_stars[(max - histo[i]) * histo_width / max]);
300 dump_zap_stats(objset_t *os, uint64_t object)
305 error = zap_get_stats(os, object, &zs);
309 if (zs.zs_ptrtbl_len == 0) {
310 ASSERT(zs.zs_num_blocks == 1);
311 (void) printf("\tmicrozap: %llu bytes, %llu entries\n",
312 (u_longlong_t)zs.zs_blocksize,
313 (u_longlong_t)zs.zs_num_entries);
317 (void) printf("\tFat ZAP stats:\n");
319 (void) printf("\t\tPointer table:\n");
320 (void) printf("\t\t\t%llu elements\n",
321 (u_longlong_t)zs.zs_ptrtbl_len);
322 (void) printf("\t\t\tzt_blk: %llu\n",
323 (u_longlong_t)zs.zs_ptrtbl_zt_blk);
324 (void) printf("\t\t\tzt_numblks: %llu\n",
325 (u_longlong_t)zs.zs_ptrtbl_zt_numblks);
326 (void) printf("\t\t\tzt_shift: %llu\n",
327 (u_longlong_t)zs.zs_ptrtbl_zt_shift);
328 (void) printf("\t\t\tzt_blks_copied: %llu\n",
329 (u_longlong_t)zs.zs_ptrtbl_blks_copied);
330 (void) printf("\t\t\tzt_nextblk: %llu\n",
331 (u_longlong_t)zs.zs_ptrtbl_nextblk);
333 (void) printf("\t\tZAP entries: %llu\n",
334 (u_longlong_t)zs.zs_num_entries);
335 (void) printf("\t\tLeaf blocks: %llu\n",
336 (u_longlong_t)zs.zs_num_leafs);
337 (void) printf("\t\tTotal blocks: %llu\n",
338 (u_longlong_t)zs.zs_num_blocks);
339 (void) printf("\t\tzap_block_type: 0x%llx\n",
340 (u_longlong_t)zs.zs_block_type);
341 (void) printf("\t\tzap_magic: 0x%llx\n",
342 (u_longlong_t)zs.zs_magic);
343 (void) printf("\t\tzap_salt: 0x%llx\n",
344 (u_longlong_t)zs.zs_salt);
346 (void) printf("\t\tLeafs with 2^n pointers:\n");
347 dump_histogram(zs.zs_leafs_with_2n_pointers, ZAP_HISTOGRAM_SIZE, 0);
349 (void) printf("\t\tBlocks with n*5 entries:\n");
350 dump_histogram(zs.zs_blocks_with_n5_entries, ZAP_HISTOGRAM_SIZE, 0);
352 (void) printf("\t\tBlocks n/10 full:\n");
353 dump_histogram(zs.zs_blocks_n_tenths_full, ZAP_HISTOGRAM_SIZE, 0);
355 (void) printf("\t\tEntries with n chunks:\n");
356 dump_histogram(zs.zs_entries_using_n_chunks, ZAP_HISTOGRAM_SIZE, 0);
358 (void) printf("\t\tBuckets with n entries:\n");
359 dump_histogram(zs.zs_buckets_with_n_entries, ZAP_HISTOGRAM_SIZE, 0);
364 dump_none(objset_t *os, uint64_t object, void *data, size_t size)
370 dump_unknown(objset_t *os, uint64_t object, void *data, size_t size)
372 (void) printf("\tUNKNOWN OBJECT TYPE\n");
377 dump_uint8(objset_t *os, uint64_t object, void *data, size_t size)
383 dump_uint64(objset_t *os, uint64_t object, void *data, size_t size)
389 dump_zap(objset_t *os, uint64_t object, void *data, size_t size)
392 zap_attribute_t attr;
396 dump_zap_stats(os, object);
399 for (zap_cursor_init(&zc, os, object);
400 zap_cursor_retrieve(&zc, &attr) == 0;
401 zap_cursor_advance(&zc)) {
402 (void) printf("\t\t%s = ", attr.za_name);
403 if (attr.za_num_integers == 0) {
407 prop = umem_zalloc(attr.za_num_integers *
408 attr.za_integer_length, UMEM_NOFAIL);
409 (void) zap_lookup(os, object, attr.za_name,
410 attr.za_integer_length, attr.za_num_integers, prop);
411 if (attr.za_integer_length == 1) {
412 (void) printf("%s", (char *)prop);
414 for (i = 0; i < attr.za_num_integers; i++) {
415 switch (attr.za_integer_length) {
418 ((uint16_t *)prop)[i]);
422 ((uint32_t *)prop)[i]);
425 (void) printf("%lld ",
426 (u_longlong_t)((int64_t *)prop)[i]);
432 umem_free(prop, attr.za_num_integers * attr.za_integer_length);
434 zap_cursor_fini(&zc);
438 dump_bpobj(objset_t *os, uint64_t object, void *data, size_t size)
440 bpobj_phys_t *bpop = data;
441 char bytes[32], comp[32], uncomp[32];
443 /* make sure the output won't get truncated */
444 CTASSERT(sizeof (bytes) >= NN_NUMBUF_SZ);
445 CTASSERT(sizeof (comp) >= NN_NUMBUF_SZ);
446 CTASSERT(sizeof (uncomp) >= NN_NUMBUF_SZ);
451 zdb_nicenum(bpop->bpo_bytes, bytes, sizeof (bytes));
452 zdb_nicenum(bpop->bpo_comp, comp, sizeof (comp));
453 zdb_nicenum(bpop->bpo_uncomp, uncomp, sizeof (uncomp));
455 (void) printf("\t\tnum_blkptrs = %llu\n",
456 (u_longlong_t)bpop->bpo_num_blkptrs);
457 (void) printf("\t\tbytes = %s\n", bytes);
458 if (size >= BPOBJ_SIZE_V1) {
459 (void) printf("\t\tcomp = %s\n", comp);
460 (void) printf("\t\tuncomp = %s\n", uncomp);
462 if (size >= sizeof (*bpop)) {
463 (void) printf("\t\tsubobjs = %llu\n",
464 (u_longlong_t)bpop->bpo_subobjs);
465 (void) printf("\t\tnum_subobjs = %llu\n",
466 (u_longlong_t)bpop->bpo_num_subobjs);
469 if (dump_opt['d'] < 5)
472 for (uint64_t i = 0; i < bpop->bpo_num_blkptrs; i++) {
473 char blkbuf[BP_SPRINTF_LEN];
476 int err = dmu_read(os, object,
477 i * sizeof (bp), sizeof (bp), &bp, 0);
479 (void) printf("got error %u from dmu_read\n", err);
482 snprintf_blkptr_compact(blkbuf, sizeof (blkbuf), &bp);
483 (void) printf("\t%s\n", blkbuf);
489 dump_bpobj_subobjs(objset_t *os, uint64_t object, void *data, size_t size)
491 dmu_object_info_t doi;
493 VERIFY0(dmu_object_info(os, object, &doi));
494 uint64_t *subobjs = kmem_alloc(doi.doi_max_offset, KM_SLEEP);
496 int err = dmu_read(os, object, 0, doi.doi_max_offset, subobjs, 0);
498 (void) printf("got error %u from dmu_read\n", err);
499 kmem_free(subobjs, doi.doi_max_offset);
503 int64_t last_nonzero = -1;
504 for (uint64_t i = 0; i < doi.doi_max_offset / 8; i++) {
509 for (int64_t i = 0; i <= last_nonzero; i++) {
510 (void) printf("\t%llu\n", (longlong_t)subobjs[i]);
512 kmem_free(subobjs, doi.doi_max_offset);
517 dump_ddt_zap(objset_t *os, uint64_t object, void *data, size_t size)
519 dump_zap_stats(os, object);
520 /* contents are printed elsewhere, properly decoded */
525 dump_sa_attrs(objset_t *os, uint64_t object, void *data, size_t size)
528 zap_attribute_t attr;
530 dump_zap_stats(os, object);
533 for (zap_cursor_init(&zc, os, object);
534 zap_cursor_retrieve(&zc, &attr) == 0;
535 zap_cursor_advance(&zc)) {
536 (void) printf("\t\t%s = ", attr.za_name);
537 if (attr.za_num_integers == 0) {
541 (void) printf(" %llx : [%d:%d:%d]\n",
542 (u_longlong_t)attr.za_first_integer,
543 (int)ATTR_LENGTH(attr.za_first_integer),
544 (int)ATTR_BSWAP(attr.za_first_integer),
545 (int)ATTR_NUM(attr.za_first_integer));
547 zap_cursor_fini(&zc);
552 dump_sa_layouts(objset_t *os, uint64_t object, void *data, size_t size)
555 zap_attribute_t attr;
556 uint16_t *layout_attrs;
559 dump_zap_stats(os, object);
562 for (zap_cursor_init(&zc, os, object);
563 zap_cursor_retrieve(&zc, &attr) == 0;
564 zap_cursor_advance(&zc)) {
565 (void) printf("\t\t%s = [", attr.za_name);
566 if (attr.za_num_integers == 0) {
571 VERIFY(attr.za_integer_length == 2);
572 layout_attrs = umem_zalloc(attr.za_num_integers *
573 attr.za_integer_length, UMEM_NOFAIL);
575 VERIFY(zap_lookup(os, object, attr.za_name,
576 attr.za_integer_length,
577 attr.za_num_integers, layout_attrs) == 0);
579 for (i = 0; i != attr.za_num_integers; i++)
580 (void) printf(" %d ", (int)layout_attrs[i]);
581 (void) printf("]\n");
582 umem_free(layout_attrs,
583 attr.za_num_integers * attr.za_integer_length);
585 zap_cursor_fini(&zc);
590 dump_zpldir(objset_t *os, uint64_t object, void *data, size_t size)
593 zap_attribute_t attr;
594 const char *typenames[] = {
595 /* 0 */ "not specified",
597 /* 2 */ "Character Device",
598 /* 3 */ "3 (invalid)",
600 /* 5 */ "5 (invalid)",
601 /* 6 */ "Block Device",
602 /* 7 */ "7 (invalid)",
603 /* 8 */ "Regular File",
604 /* 9 */ "9 (invalid)",
605 /* 10 */ "Symbolic Link",
606 /* 11 */ "11 (invalid)",
609 /* 14 */ "Event Port",
610 /* 15 */ "15 (invalid)",
613 dump_zap_stats(os, object);
616 for (zap_cursor_init(&zc, os, object);
617 zap_cursor_retrieve(&zc, &attr) == 0;
618 zap_cursor_advance(&zc)) {
619 (void) printf("\t\t%s = %lld (type: %s)\n",
620 attr.za_name, ZFS_DIRENT_OBJ(attr.za_first_integer),
621 typenames[ZFS_DIRENT_TYPE(attr.za_first_integer)]);
623 zap_cursor_fini(&zc);
627 get_dtl_refcount(vdev_t *vd)
631 if (vd->vdev_ops->vdev_op_leaf) {
632 space_map_t *sm = vd->vdev_dtl_sm;
635 sm->sm_dbuf->db_size == sizeof (space_map_phys_t))
640 for (int c = 0; c < vd->vdev_children; c++)
641 refcount += get_dtl_refcount(vd->vdev_child[c]);
646 get_metaslab_refcount(vdev_t *vd)
650 if (vd->vdev_top == vd && !vd->vdev_removing) {
651 for (int m = 0; m < vd->vdev_ms_count; m++) {
652 space_map_t *sm = vd->vdev_ms[m]->ms_sm;
655 sm->sm_dbuf->db_size == sizeof (space_map_phys_t))
659 for (int c = 0; c < vd->vdev_children; c++)
660 refcount += get_metaslab_refcount(vd->vdev_child[c]);
666 verify_spacemap_refcounts(spa_t *spa)
668 uint64_t expected_refcount = 0;
669 uint64_t actual_refcount;
671 (void) feature_get_refcount(spa,
672 &spa_feature_table[SPA_FEATURE_SPACEMAP_HISTOGRAM],
674 actual_refcount = get_dtl_refcount(spa->spa_root_vdev);
675 actual_refcount += get_metaslab_refcount(spa->spa_root_vdev);
677 if (expected_refcount != actual_refcount) {
678 (void) printf("space map refcount mismatch: expected %lld != "
680 (longlong_t)expected_refcount,
681 (longlong_t)actual_refcount);
688 dump_spacemap(objset_t *os, space_map_t *sm)
690 uint64_t alloc, offset, entry;
691 char *ddata[] = { "ALLOC", "FREE", "CONDENSE", "INVALID",
692 "INVALID", "INVALID", "INVALID", "INVALID" };
698 * Print out the freelist entries in both encoded and decoded form.
701 for (offset = 0; offset < space_map_length(sm);
702 offset += sizeof (entry)) {
703 uint8_t mapshift = sm->sm_shift;
705 VERIFY0(dmu_read(os, space_map_object(sm), offset,
706 sizeof (entry), &entry, DMU_READ_PREFETCH));
707 if (SM_DEBUG_DECODE(entry)) {
709 (void) printf("\t [%6llu] %s: txg %llu, pass %llu\n",
710 (u_longlong_t)(offset / sizeof (entry)),
711 ddata[SM_DEBUG_ACTION_DECODE(entry)],
712 (u_longlong_t)SM_DEBUG_TXG_DECODE(entry),
713 (u_longlong_t)SM_DEBUG_SYNCPASS_DECODE(entry));
715 (void) printf("\t [%6llu] %c range:"
716 " %010llx-%010llx size: %06llx\n",
717 (u_longlong_t)(offset / sizeof (entry)),
718 SM_TYPE_DECODE(entry) == SM_ALLOC ? 'A' : 'F',
719 (u_longlong_t)((SM_OFFSET_DECODE(entry) <<
720 mapshift) + sm->sm_start),
721 (u_longlong_t)((SM_OFFSET_DECODE(entry) <<
722 mapshift) + sm->sm_start +
723 (SM_RUN_DECODE(entry) << mapshift)),
724 (u_longlong_t)(SM_RUN_DECODE(entry) << mapshift));
725 if (SM_TYPE_DECODE(entry) == SM_ALLOC)
726 alloc += SM_RUN_DECODE(entry) << mapshift;
728 alloc -= SM_RUN_DECODE(entry) << mapshift;
731 if (alloc != space_map_allocated(sm)) {
732 (void) printf("space_map_object alloc (%llu) INCONSISTENT "
733 "with space map summary (%llu)\n",
734 (u_longlong_t)space_map_allocated(sm), (u_longlong_t)alloc);
739 dump_metaslab_stats(metaslab_t *msp)
742 range_tree_t *rt = msp->ms_tree;
743 avl_tree_t *t = &msp->ms_size_tree;
744 int free_pct = range_tree_space(rt) * 100 / msp->ms_size;
746 /* max sure nicenum has enough space */
747 CTASSERT(sizeof (maxbuf) >= NN_NUMBUF_SZ);
749 zdb_nicenum(metaslab_block_maxsize(msp), maxbuf, sizeof (maxbuf));
751 (void) printf("\t %25s %10lu %7s %6s %4s %4d%%\n",
752 "segments", avl_numnodes(t), "maxsize", maxbuf,
753 "freepct", free_pct);
754 (void) printf("\tIn-memory histogram:\n");
755 dump_histogram(rt->rt_histogram, RANGE_TREE_HISTOGRAM_SIZE, 0);
759 dump_metaslab(metaslab_t *msp)
761 vdev_t *vd = msp->ms_group->mg_vd;
762 spa_t *spa = vd->vdev_spa;
763 space_map_t *sm = msp->ms_sm;
766 zdb_nicenum(msp->ms_size - space_map_allocated(sm), freebuf,
770 "\tmetaslab %6llu offset %12llx spacemap %6llu free %5s\n",
771 (u_longlong_t)msp->ms_id, (u_longlong_t)msp->ms_start,
772 (u_longlong_t)space_map_object(sm), freebuf);
774 if (dump_opt['m'] > 2 && !dump_opt['L']) {
775 mutex_enter(&msp->ms_lock);
776 metaslab_load_wait(msp);
777 if (!msp->ms_loaded) {
778 VERIFY0(metaslab_load(msp));
779 range_tree_stat_verify(msp->ms_tree);
781 dump_metaslab_stats(msp);
782 metaslab_unload(msp);
783 mutex_exit(&msp->ms_lock);
786 if (dump_opt['m'] > 1 && sm != NULL &&
787 spa_feature_is_active(spa, SPA_FEATURE_SPACEMAP_HISTOGRAM)) {
789 * The space map histogram represents free space in chunks
790 * of sm_shift (i.e. bucket 0 refers to 2^sm_shift).
792 (void) printf("\tOn-disk histogram:\t\tfragmentation %llu\n",
793 (u_longlong_t)msp->ms_fragmentation);
794 dump_histogram(sm->sm_phys->smp_histogram,
795 SPACE_MAP_HISTOGRAM_SIZE, sm->sm_shift);
798 if (dump_opt['d'] > 5 || dump_opt['m'] > 3) {
799 ASSERT(msp->ms_size == (1ULL << vd->vdev_ms_shift));
801 mutex_enter(&msp->ms_lock);
802 dump_spacemap(spa->spa_meta_objset, msp->ms_sm);
803 mutex_exit(&msp->ms_lock);
808 print_vdev_metaslab_header(vdev_t *vd)
810 (void) printf("\tvdev %10llu\n\t%-10s%5llu %-19s %-15s %-10s\n",
811 (u_longlong_t)vd->vdev_id,
812 "metaslabs", (u_longlong_t)vd->vdev_ms_count,
813 "offset", "spacemap", "free");
814 (void) printf("\t%15s %19s %15s %10s\n",
815 "---------------", "-------------------",
816 "---------------", "-------------");
820 dump_metaslab_groups(spa_t *spa)
822 vdev_t *rvd = spa->spa_root_vdev;
823 metaslab_class_t *mc = spa_normal_class(spa);
824 uint64_t fragmentation;
826 metaslab_class_histogram_verify(mc);
828 for (int c = 0; c < rvd->vdev_children; c++) {
829 vdev_t *tvd = rvd->vdev_child[c];
830 metaslab_group_t *mg = tvd->vdev_mg;
832 if (mg->mg_class != mc)
835 metaslab_group_histogram_verify(mg);
836 mg->mg_fragmentation = metaslab_group_fragmentation(mg);
838 (void) printf("\tvdev %10llu\t\tmetaslabs%5llu\t\t"
840 (u_longlong_t)tvd->vdev_id,
841 (u_longlong_t)tvd->vdev_ms_count);
842 if (mg->mg_fragmentation == ZFS_FRAG_INVALID) {
843 (void) printf("%3s\n", "-");
845 (void) printf("%3llu%%\n",
846 (u_longlong_t)mg->mg_fragmentation);
848 dump_histogram(mg->mg_histogram, RANGE_TREE_HISTOGRAM_SIZE, 0);
851 (void) printf("\tpool %s\tfragmentation", spa_name(spa));
852 fragmentation = metaslab_class_fragmentation(mc);
853 if (fragmentation == ZFS_FRAG_INVALID)
854 (void) printf("\t%3s\n", "-");
856 (void) printf("\t%3llu%%\n", (u_longlong_t)fragmentation);
857 dump_histogram(mc->mc_histogram, RANGE_TREE_HISTOGRAM_SIZE, 0);
861 dump_metaslabs(spa_t *spa)
863 vdev_t *vd, *rvd = spa->spa_root_vdev;
864 uint64_t m, c = 0, children = rvd->vdev_children;
866 (void) printf("\nMetaslabs:\n");
868 if (!dump_opt['d'] && zopt_objects > 0) {
872 (void) fatal("bad vdev id: %llu", (u_longlong_t)c);
874 if (zopt_objects > 1) {
875 vd = rvd->vdev_child[c];
876 print_vdev_metaslab_header(vd);
878 for (m = 1; m < zopt_objects; m++) {
879 if (zopt_object[m] < vd->vdev_ms_count)
881 vd->vdev_ms[zopt_object[m]]);
883 (void) fprintf(stderr, "bad metaslab "
885 (u_longlong_t)zopt_object[m]);
892 for (; c < children; c++) {
893 vd = rvd->vdev_child[c];
894 print_vdev_metaslab_header(vd);
896 for (m = 0; m < vd->vdev_ms_count; m++)
897 dump_metaslab(vd->vdev_ms[m]);
903 dump_dde(const ddt_t *ddt, const ddt_entry_t *dde, uint64_t index)
905 const ddt_phys_t *ddp = dde->dde_phys;
906 const ddt_key_t *ddk = &dde->dde_key;
907 char *types[4] = { "ditto", "single", "double", "triple" };
908 char blkbuf[BP_SPRINTF_LEN];
911 for (int p = 0; p < DDT_PHYS_TYPES; p++, ddp++) {
912 if (ddp->ddp_phys_birth == 0)
914 ddt_bp_create(ddt->ddt_checksum, ddk, ddp, &blk);
915 snprintf_blkptr(blkbuf, sizeof (blkbuf), &blk);
916 (void) printf("index %llx refcnt %llu %s %s\n",
917 (u_longlong_t)index, (u_longlong_t)ddp->ddp_refcnt,
923 dump_dedup_ratio(const ddt_stat_t *dds)
925 double rL, rP, rD, D, dedup, compress, copies;
927 if (dds->dds_blocks == 0)
930 rL = (double)dds->dds_ref_lsize;
931 rP = (double)dds->dds_ref_psize;
932 rD = (double)dds->dds_ref_dsize;
933 D = (double)dds->dds_dsize;
939 (void) printf("dedup = %.2f, compress = %.2f, copies = %.2f, "
940 "dedup * compress / copies = %.2f\n\n",
941 dedup, compress, copies, dedup * compress / copies);
945 dump_ddt(ddt_t *ddt, enum ddt_type type, enum ddt_class class)
947 char name[DDT_NAMELEN];
950 dmu_object_info_t doi;
951 uint64_t count, dspace, mspace;
954 error = ddt_object_info(ddt, type, class, &doi);
960 error = ddt_object_count(ddt, type, class, &count);
965 dspace = doi.doi_physical_blocks_512 << 9;
966 mspace = doi.doi_fill_count * doi.doi_data_block_size;
968 ddt_object_name(ddt, type, class, name);
970 (void) printf("%s: %llu entries, size %llu on disk, %llu in core\n",
973 (u_longlong_t)(dspace / count),
974 (u_longlong_t)(mspace / count));
976 if (dump_opt['D'] < 3)
979 zpool_dump_ddt(NULL, &ddt->ddt_histogram[type][class]);
981 if (dump_opt['D'] < 4)
984 if (dump_opt['D'] < 5 && class == DDT_CLASS_UNIQUE)
987 (void) printf("%s contents:\n\n", name);
989 while ((error = ddt_object_walk(ddt, type, class, &walk, &dde)) == 0)
990 dump_dde(ddt, &dde, walk);
992 ASSERT(error == ENOENT);
998 dump_all_ddts(spa_t *spa)
1000 ddt_histogram_t ddh_total = { 0 };
1001 ddt_stat_t dds_total = { 0 };
1003 for (enum zio_checksum c = 0; c < ZIO_CHECKSUM_FUNCTIONS; c++) {
1004 ddt_t *ddt = spa->spa_ddt[c];
1005 for (enum ddt_type type = 0; type < DDT_TYPES; type++) {
1006 for (enum ddt_class class = 0; class < DDT_CLASSES;
1008 dump_ddt(ddt, type, class);
1013 ddt_get_dedup_stats(spa, &dds_total);
1015 if (dds_total.dds_blocks == 0) {
1016 (void) printf("All DDTs are empty\n");
1020 (void) printf("\n");
1022 if (dump_opt['D'] > 1) {
1023 (void) printf("DDT histogram (aggregated over all DDTs):\n");
1024 ddt_get_dedup_histogram(spa, &ddh_total);
1025 zpool_dump_ddt(&dds_total, &ddh_total);
1028 dump_dedup_ratio(&dds_total);
1032 dump_dtl_seg(void *arg, uint64_t start, uint64_t size)
1036 (void) printf("%s [%llu,%llu) length %llu\n",
1038 (u_longlong_t)start,
1039 (u_longlong_t)(start + size),
1040 (u_longlong_t)(size));
1044 dump_dtl(vdev_t *vd, int indent)
1046 spa_t *spa = vd->vdev_spa;
1048 char *name[DTL_TYPES] = { "missing", "partial", "scrub", "outage" };
1051 spa_vdev_state_enter(spa, SCL_NONE);
1052 required = vdev_dtl_required(vd);
1053 (void) spa_vdev_state_exit(spa, NULL, 0);
1056 (void) printf("\nDirty time logs:\n\n");
1058 (void) printf("\t%*s%s [%s]\n", indent, "",
1059 vd->vdev_path ? vd->vdev_path :
1060 vd->vdev_parent ? vd->vdev_ops->vdev_op_type : spa_name(spa),
1061 required ? "DTL-required" : "DTL-expendable");
1063 for (int t = 0; t < DTL_TYPES; t++) {
1064 range_tree_t *rt = vd->vdev_dtl[t];
1065 if (range_tree_space(rt) == 0)
1067 (void) snprintf(prefix, sizeof (prefix), "\t%*s%s",
1068 indent + 2, "", name[t]);
1069 mutex_enter(rt->rt_lock);
1070 range_tree_walk(rt, dump_dtl_seg, prefix);
1071 mutex_exit(rt->rt_lock);
1072 if (dump_opt['d'] > 5 && vd->vdev_children == 0)
1073 dump_spacemap(spa->spa_meta_objset, vd->vdev_dtl_sm);
1076 for (int c = 0; c < vd->vdev_children; c++)
1077 dump_dtl(vd->vdev_child[c], indent + 4);
1080 /* from spa_history.c: spa_history_create_obj() */
1081 #define HIS_BUF_LEN_DEF (128 << 10)
1082 #define HIS_BUF_LEN_MAX (1 << 30)
1085 dump_history(spa_t *spa)
1087 nvlist_t **events = NULL;
1089 uint64_t bufsize = HIS_BUF_LEN_DEF;
1090 uint64_t resid, len, off = 0;
1096 char internalstr[MAXPATHLEN];
1098 if ((buf = malloc(bufsize)) == NULL)
1099 (void) fprintf(stderr, "Unable to read history: "
1104 if ((error = spa_history_get(spa, &off, &len, buf)) != 0) {
1105 (void) fprintf(stderr, "Unable to read history: "
1106 "error %d\n", error);
1110 if (zpool_history_unpack(buf, len, &resid, &events, &num) != 0)
1115 * If the history block is too big, double the buffer
1116 * size and try again.
1123 if ((bufsize >= HIS_BUF_LEN_MAX) ||
1124 ((buf = malloc(bufsize)) == NULL)) {
1125 (void) fprintf(stderr, "Unable to read history: "
1133 (void) printf("\nHistory:\n");
1134 for (int i = 0; i < num; i++) {
1135 uint64_t time, txg, ievent;
1137 boolean_t printed = B_FALSE;
1139 if (nvlist_lookup_uint64(events[i], ZPOOL_HIST_TIME,
1142 if (nvlist_lookup_string(events[i], ZPOOL_HIST_CMD,
1144 if (nvlist_lookup_uint64(events[i],
1145 ZPOOL_HIST_INT_EVENT, &ievent) != 0)
1147 verify(nvlist_lookup_uint64(events[i],
1148 ZPOOL_HIST_TXG, &txg) == 0);
1149 verify(nvlist_lookup_string(events[i],
1150 ZPOOL_HIST_INT_STR, &intstr) == 0);
1151 if (ievent >= ZFS_NUM_LEGACY_HISTORY_EVENTS)
1154 (void) snprintf(internalstr,
1155 sizeof (internalstr),
1156 "[internal %s txg:%lld] %s",
1157 zfs_history_event_names[ievent], txg,
1162 (void) localtime_r(&tsec, &t);
1163 (void) strftime(tbuf, sizeof (tbuf), "%F.%T", &t);
1164 (void) printf("%s %s\n", tbuf, cmd);
1168 if (dump_opt['h'] > 1) {
1170 (void) printf("unrecognized record:\n");
1171 dump_nvlist(events[i], 2);
1178 dump_dnode(objset_t *os, uint64_t object, void *data, size_t size)
1183 blkid2offset(const dnode_phys_t *dnp, const blkptr_t *bp,
1184 const zbookmark_phys_t *zb)
1187 ASSERT(zb->zb_level < 0);
1188 if (zb->zb_object == 0)
1189 return (zb->zb_blkid);
1190 return (zb->zb_blkid * BP_GET_LSIZE(bp));
1193 ASSERT(zb->zb_level >= 0);
1195 return ((zb->zb_blkid <<
1196 (zb->zb_level * (dnp->dn_indblkshift - SPA_BLKPTRSHIFT))) *
1197 dnp->dn_datablkszsec << SPA_MINBLOCKSHIFT);
1201 snprintf_blkptr_compact(char *blkbuf, size_t buflen, const blkptr_t *bp)
1203 const dva_t *dva = bp->blk_dva;
1204 int ndvas = dump_opt['d'] > 5 ? BP_GET_NDVAS(bp) : 1;
1206 if (dump_opt['b'] >= 6) {
1207 snprintf_blkptr(blkbuf, buflen, bp);
1211 if (BP_IS_EMBEDDED(bp)) {
1212 (void) sprintf(blkbuf,
1213 "EMBEDDED et=%u %llxL/%llxP B=%llu",
1214 (int)BPE_GET_ETYPE(bp),
1215 (u_longlong_t)BPE_GET_LSIZE(bp),
1216 (u_longlong_t)BPE_GET_PSIZE(bp),
1217 (u_longlong_t)bp->blk_birth);
1222 for (int i = 0; i < ndvas; i++)
1223 (void) snprintf(blkbuf + strlen(blkbuf),
1224 buflen - strlen(blkbuf), "%llu:%llx:%llx ",
1225 (u_longlong_t)DVA_GET_VDEV(&dva[i]),
1226 (u_longlong_t)DVA_GET_OFFSET(&dva[i]),
1227 (u_longlong_t)DVA_GET_ASIZE(&dva[i]));
1229 if (BP_IS_HOLE(bp)) {
1230 (void) snprintf(blkbuf + strlen(blkbuf),
1231 buflen - strlen(blkbuf),
1233 (u_longlong_t)BP_GET_LSIZE(bp),
1234 (u_longlong_t)bp->blk_birth);
1236 (void) snprintf(blkbuf + strlen(blkbuf),
1237 buflen - strlen(blkbuf),
1238 "%llxL/%llxP F=%llu B=%llu/%llu",
1239 (u_longlong_t)BP_GET_LSIZE(bp),
1240 (u_longlong_t)BP_GET_PSIZE(bp),
1241 (u_longlong_t)BP_GET_FILL(bp),
1242 (u_longlong_t)bp->blk_birth,
1243 (u_longlong_t)BP_PHYSICAL_BIRTH(bp));
1248 print_indirect(blkptr_t *bp, const zbookmark_phys_t *zb,
1249 const dnode_phys_t *dnp)
1251 char blkbuf[BP_SPRINTF_LEN];
1254 if (!BP_IS_EMBEDDED(bp)) {
1255 ASSERT3U(BP_GET_TYPE(bp), ==, dnp->dn_type);
1256 ASSERT3U(BP_GET_LEVEL(bp), ==, zb->zb_level);
1259 (void) printf("%16llx ", (u_longlong_t)blkid2offset(dnp, bp, zb));
1261 ASSERT(zb->zb_level >= 0);
1263 for (l = dnp->dn_nlevels - 1; l >= -1; l--) {
1264 if (l == zb->zb_level) {
1265 (void) printf("L%llx", (u_longlong_t)zb->zb_level);
1271 snprintf_blkptr_compact(blkbuf, sizeof (blkbuf), bp);
1272 (void) printf("%s\n", blkbuf);
1276 visit_indirect(spa_t *spa, const dnode_phys_t *dnp,
1277 blkptr_t *bp, const zbookmark_phys_t *zb)
1281 if (bp->blk_birth == 0)
1284 print_indirect(bp, zb, dnp);
1286 if (BP_GET_LEVEL(bp) > 0 && !BP_IS_HOLE(bp)) {
1287 arc_flags_t flags = ARC_FLAG_WAIT;
1290 int epb = BP_GET_LSIZE(bp) >> SPA_BLKPTRSHIFT;
1294 err = arc_read(NULL, spa, bp, arc_getbuf_func, &buf,
1295 ZIO_PRIORITY_ASYNC_READ, ZIO_FLAG_CANFAIL, &flags, zb);
1298 ASSERT(buf->b_data);
1300 /* recursively visit blocks below this */
1302 for (i = 0; i < epb; i++, cbp++) {
1303 zbookmark_phys_t czb;
1305 SET_BOOKMARK(&czb, zb->zb_objset, zb->zb_object,
1307 zb->zb_blkid * epb + i);
1308 err = visit_indirect(spa, dnp, cbp, &czb);
1311 fill += BP_GET_FILL(cbp);
1314 ASSERT3U(fill, ==, BP_GET_FILL(bp));
1315 arc_buf_destroy(buf, &buf);
1323 dump_indirect(dnode_t *dn)
1325 dnode_phys_t *dnp = dn->dn_phys;
1327 zbookmark_phys_t czb;
1329 (void) printf("Indirect blocks:\n");
1331 SET_BOOKMARK(&czb, dmu_objset_id(dn->dn_objset),
1332 dn->dn_object, dnp->dn_nlevels - 1, 0);
1333 for (j = 0; j < dnp->dn_nblkptr; j++) {
1335 (void) visit_indirect(dmu_objset_spa(dn->dn_objset), dnp,
1336 &dnp->dn_blkptr[j], &czb);
1339 (void) printf("\n");
1344 dump_dsl_dir(objset_t *os, uint64_t object, void *data, size_t size)
1346 dsl_dir_phys_t *dd = data;
1350 /* make sure nicenum has enough space */
1351 CTASSERT(sizeof (nice) >= NN_NUMBUF_SZ);
1356 ASSERT3U(size, >=, sizeof (dsl_dir_phys_t));
1358 crtime = dd->dd_creation_time;
1359 (void) printf("\t\tcreation_time = %s", ctime(&crtime));
1360 (void) printf("\t\thead_dataset_obj = %llu\n",
1361 (u_longlong_t)dd->dd_head_dataset_obj);
1362 (void) printf("\t\tparent_dir_obj = %llu\n",
1363 (u_longlong_t)dd->dd_parent_obj);
1364 (void) printf("\t\torigin_obj = %llu\n",
1365 (u_longlong_t)dd->dd_origin_obj);
1366 (void) printf("\t\tchild_dir_zapobj = %llu\n",
1367 (u_longlong_t)dd->dd_child_dir_zapobj);
1368 zdb_nicenum(dd->dd_used_bytes, nice, sizeof (nice));
1369 (void) printf("\t\tused_bytes = %s\n", nice);
1370 zdb_nicenum(dd->dd_compressed_bytes, nice, sizeof (nice));
1371 (void) printf("\t\tcompressed_bytes = %s\n", nice);
1372 zdb_nicenum(dd->dd_uncompressed_bytes, nice, sizeof (nice));
1373 (void) printf("\t\tuncompressed_bytes = %s\n", nice);
1374 zdb_nicenum(dd->dd_quota, nice, sizeof (nice));
1375 (void) printf("\t\tquota = %s\n", nice);
1376 zdb_nicenum(dd->dd_reserved, nice, sizeof (nice));
1377 (void) printf("\t\treserved = %s\n", nice);
1378 (void) printf("\t\tprops_zapobj = %llu\n",
1379 (u_longlong_t)dd->dd_props_zapobj);
1380 (void) printf("\t\tdeleg_zapobj = %llu\n",
1381 (u_longlong_t)dd->dd_deleg_zapobj);
1382 (void) printf("\t\tflags = %llx\n",
1383 (u_longlong_t)dd->dd_flags);
1386 zdb_nicenum(dd->dd_used_breakdown[DD_USED_ ## which], nice, \
1388 (void) printf("\t\tused_breakdown[" #which "] = %s\n", nice)
1399 dump_dsl_dataset(objset_t *os, uint64_t object, void *data, size_t size)
1401 dsl_dataset_phys_t *ds = data;
1403 char used[32], compressed[32], uncompressed[32], unique[32];
1404 char blkbuf[BP_SPRINTF_LEN];
1406 /* make sure nicenum has enough space */
1407 CTASSERT(sizeof (used) >= NN_NUMBUF_SZ);
1408 CTASSERT(sizeof (compressed) >= NN_NUMBUF_SZ);
1409 CTASSERT(sizeof (uncompressed) >= NN_NUMBUF_SZ);
1410 CTASSERT(sizeof (unique) >= NN_NUMBUF_SZ);
1415 ASSERT(size == sizeof (*ds));
1416 crtime = ds->ds_creation_time;
1417 zdb_nicenum(ds->ds_referenced_bytes, used, sizeof (used));
1418 zdb_nicenum(ds->ds_compressed_bytes, compressed, sizeof (compressed));
1419 zdb_nicenum(ds->ds_uncompressed_bytes, uncompressed,
1420 sizeof (uncompressed));
1421 zdb_nicenum(ds->ds_unique_bytes, unique, sizeof (unique));
1422 snprintf_blkptr(blkbuf, sizeof (blkbuf), &ds->ds_bp);
1424 (void) printf("\t\tdir_obj = %llu\n",
1425 (u_longlong_t)ds->ds_dir_obj);
1426 (void) printf("\t\tprev_snap_obj = %llu\n",
1427 (u_longlong_t)ds->ds_prev_snap_obj);
1428 (void) printf("\t\tprev_snap_txg = %llu\n",
1429 (u_longlong_t)ds->ds_prev_snap_txg);
1430 (void) printf("\t\tnext_snap_obj = %llu\n",
1431 (u_longlong_t)ds->ds_next_snap_obj);
1432 (void) printf("\t\tsnapnames_zapobj = %llu\n",
1433 (u_longlong_t)ds->ds_snapnames_zapobj);
1434 (void) printf("\t\tnum_children = %llu\n",
1435 (u_longlong_t)ds->ds_num_children);
1436 (void) printf("\t\tuserrefs_obj = %llu\n",
1437 (u_longlong_t)ds->ds_userrefs_obj);
1438 (void) printf("\t\tcreation_time = %s", ctime(&crtime));
1439 (void) printf("\t\tcreation_txg = %llu\n",
1440 (u_longlong_t)ds->ds_creation_txg);
1441 (void) printf("\t\tdeadlist_obj = %llu\n",
1442 (u_longlong_t)ds->ds_deadlist_obj);
1443 (void) printf("\t\tused_bytes = %s\n", used);
1444 (void) printf("\t\tcompressed_bytes = %s\n", compressed);
1445 (void) printf("\t\tuncompressed_bytes = %s\n", uncompressed);
1446 (void) printf("\t\tunique = %s\n", unique);
1447 (void) printf("\t\tfsid_guid = %llu\n",
1448 (u_longlong_t)ds->ds_fsid_guid);
1449 (void) printf("\t\tguid = %llu\n",
1450 (u_longlong_t)ds->ds_guid);
1451 (void) printf("\t\tflags = %llx\n",
1452 (u_longlong_t)ds->ds_flags);
1453 (void) printf("\t\tnext_clones_obj = %llu\n",
1454 (u_longlong_t)ds->ds_next_clones_obj);
1455 (void) printf("\t\tprops_obj = %llu\n",
1456 (u_longlong_t)ds->ds_props_obj);
1457 (void) printf("\t\tbp = %s\n", blkbuf);
1462 dump_bptree_cb(void *arg, const blkptr_t *bp, dmu_tx_t *tx)
1464 char blkbuf[BP_SPRINTF_LEN];
1466 if (bp->blk_birth != 0) {
1467 snprintf_blkptr(blkbuf, sizeof (blkbuf), bp);
1468 (void) printf("\t%s\n", blkbuf);
1474 dump_bptree(objset_t *os, uint64_t obj, char *name)
1480 /* make sure nicenum has enough space */
1481 CTASSERT(sizeof (bytes) >= NN_NUMBUF_SZ);
1483 if (dump_opt['d'] < 3)
1486 VERIFY3U(0, ==, dmu_bonus_hold(os, obj, FTAG, &db));
1488 zdb_nicenum(bt->bt_bytes, bytes, sizeof (bytes));
1489 (void) printf("\n %s: %llu datasets, %s\n",
1490 name, (unsigned long long)(bt->bt_end - bt->bt_begin), bytes);
1491 dmu_buf_rele(db, FTAG);
1493 if (dump_opt['d'] < 5)
1496 (void) printf("\n");
1498 (void) bptree_iterate(os, obj, B_FALSE, dump_bptree_cb, NULL, NULL);
1503 dump_bpobj_cb(void *arg, const blkptr_t *bp, dmu_tx_t *tx)
1505 char blkbuf[BP_SPRINTF_LEN];
1507 ASSERT(bp->blk_birth != 0);
1508 snprintf_blkptr_compact(blkbuf, sizeof (blkbuf), bp);
1509 (void) printf("\t%s\n", blkbuf);
1514 dump_full_bpobj(bpobj_t *bpo, char *name, int indent)
1520 /* make sure nicenum has enough space */
1521 CTASSERT(sizeof (bytes) >= NN_NUMBUF_SZ);
1522 CTASSERT(sizeof (comp) >= NN_NUMBUF_SZ);
1523 CTASSERT(sizeof (uncomp) >= NN_NUMBUF_SZ);
1525 if (dump_opt['d'] < 3)
1528 zdb_nicenum(bpo->bpo_phys->bpo_bytes, bytes, sizeof (bytes));
1529 if (bpo->bpo_havesubobj && bpo->bpo_phys->bpo_subobjs != 0) {
1530 zdb_nicenum(bpo->bpo_phys->bpo_comp, comp, sizeof (comp));
1531 zdb_nicenum(bpo->bpo_phys->bpo_uncomp, uncomp, sizeof (uncomp));
1532 (void) printf(" %*s: object %llu, %llu local blkptrs, "
1533 "%llu subobjs in object %llu, %s (%s/%s comp)\n",
1535 (u_longlong_t)bpo->bpo_object,
1536 (u_longlong_t)bpo->bpo_phys->bpo_num_blkptrs,
1537 (u_longlong_t)bpo->bpo_phys->bpo_num_subobjs,
1538 (u_longlong_t)bpo->bpo_phys->bpo_subobjs,
1539 bytes, comp, uncomp);
1541 for (uint64_t i = 0; i < bpo->bpo_phys->bpo_num_subobjs; i++) {
1545 VERIFY0(dmu_read(bpo->bpo_os,
1546 bpo->bpo_phys->bpo_subobjs,
1547 i * sizeof (subobj), sizeof (subobj), &subobj, 0));
1548 error = bpobj_open(&subbpo, bpo->bpo_os, subobj);
1550 (void) printf("ERROR %u while trying to open "
1552 error, (u_longlong_t)subobj);
1555 dump_full_bpobj(&subbpo, "subobj", indent + 1);
1556 bpobj_close(&subbpo);
1559 (void) printf(" %*s: object %llu, %llu blkptrs, %s\n",
1561 (u_longlong_t)bpo->bpo_object,
1562 (u_longlong_t)bpo->bpo_phys->bpo_num_blkptrs,
1566 if (dump_opt['d'] < 5)
1571 (void) bpobj_iterate_nofree(bpo, dump_bpobj_cb, NULL, NULL);
1572 (void) printf("\n");
1577 dump_deadlist(dsl_deadlist_t *dl)
1579 dsl_deadlist_entry_t *dle;
1585 /* make sure nicenum has enough space */
1586 CTASSERT(sizeof (bytes) >= NN_NUMBUF_SZ);
1587 CTASSERT(sizeof (comp) >= NN_NUMBUF_SZ);
1588 CTASSERT(sizeof (uncomp) >= NN_NUMBUF_SZ);
1590 if (dump_opt['d'] < 3)
1593 if (dl->dl_oldfmt) {
1594 dump_full_bpobj(&dl->dl_bpobj, "old-format deadlist", 0);
1598 zdb_nicenum(dl->dl_phys->dl_used, bytes, sizeof (bytes));
1599 zdb_nicenum(dl->dl_phys->dl_comp, comp, sizeof (comp));
1600 zdb_nicenum(dl->dl_phys->dl_uncomp, uncomp, sizeof (uncomp));
1601 (void) printf("\n Deadlist: %s (%s/%s comp)\n",
1602 bytes, comp, uncomp);
1604 if (dump_opt['d'] < 4)
1607 (void) printf("\n");
1609 /* force the tree to be loaded */
1610 dsl_deadlist_space_range(dl, 0, UINT64_MAX, &unused, &unused, &unused);
1612 for (dle = avl_first(&dl->dl_tree); dle;
1613 dle = AVL_NEXT(&dl->dl_tree, dle)) {
1614 if (dump_opt['d'] >= 5) {
1616 (void) snprintf(buf, sizeof (buf), "mintxg %llu -> ",
1617 (longlong_t)dle->dle_mintxg,
1618 (longlong_t)dle->dle_bpobj.bpo_object);
1619 dump_full_bpobj(&dle->dle_bpobj, buf, 0);
1621 (void) printf("mintxg %llu -> obj %llu\n",
1622 (longlong_t)dle->dle_mintxg,
1623 (longlong_t)dle->dle_bpobj.bpo_object);
1629 static avl_tree_t idx_tree;
1630 static avl_tree_t domain_tree;
1631 static boolean_t fuid_table_loaded;
1632 static boolean_t sa_loaded;
1633 sa_attr_type_t *sa_attr_table;
1636 fuid_table_destroy()
1638 if (fuid_table_loaded) {
1639 zfs_fuid_table_destroy(&idx_tree, &domain_tree);
1640 fuid_table_loaded = B_FALSE;
1645 * print uid or gid information.
1646 * For normal POSIX id just the id is printed in decimal format.
1647 * For CIFS files with FUID the fuid is printed in hex followed by
1648 * the domain-rid string.
1651 print_idstr(uint64_t id, const char *id_type)
1653 if (FUID_INDEX(id)) {
1656 domain = zfs_fuid_idx_domain(&idx_tree, FUID_INDEX(id));
1657 (void) printf("\t%s %llx [%s-%d]\n", id_type,
1658 (u_longlong_t)id, domain, (int)FUID_RID(id));
1660 (void) printf("\t%s %llu\n", id_type, (u_longlong_t)id);
1666 dump_uidgid(objset_t *os, uint64_t uid, uint64_t gid)
1668 uint32_t uid_idx, gid_idx;
1670 uid_idx = FUID_INDEX(uid);
1671 gid_idx = FUID_INDEX(gid);
1673 /* Load domain table, if not already loaded */
1674 if (!fuid_table_loaded && (uid_idx || gid_idx)) {
1677 /* first find the fuid object. It lives in the master node */
1678 VERIFY(zap_lookup(os, MASTER_NODE_OBJ, ZFS_FUID_TABLES,
1679 8, 1, &fuid_obj) == 0);
1680 zfs_fuid_avl_tree_create(&idx_tree, &domain_tree);
1681 (void) zfs_fuid_table_load(os, fuid_obj,
1682 &idx_tree, &domain_tree);
1683 fuid_table_loaded = B_TRUE;
1686 print_idstr(uid, "uid");
1687 print_idstr(gid, "gid");
1692 dump_znode(objset_t *os, uint64_t object, void *data, size_t size)
1694 char path[MAXPATHLEN * 2]; /* allow for xattr and failure prefix */
1696 uint64_t xattr, rdev, gen;
1697 uint64_t uid, gid, mode, fsize, parent, links;
1699 uint64_t acctm[2], modtm[2], chgtm[2], crtm[2];
1700 time_t z_crtime, z_atime, z_mtime, z_ctime;
1701 sa_bulk_attr_t bulk[12];
1706 uint64_t sa_attrs = 0;
1709 VERIFY(zap_lookup(os, MASTER_NODE_OBJ, ZPL_VERSION_STR,
1710 8, 1, &version) == 0);
1711 if (version >= ZPL_VERSION_SA) {
1712 VERIFY(zap_lookup(os, MASTER_NODE_OBJ, ZFS_SA_ATTRS,
1713 8, 1, &sa_attrs) == 0);
1715 if ((error = sa_setup(os, sa_attrs, zfs_attr_table,
1716 ZPL_END, &sa_attr_table)) != 0) {
1717 (void) printf("sa_setup failed errno %d, can't "
1718 "display znode contents\n", error);
1724 if (sa_handle_get(os, object, NULL, SA_HDL_PRIVATE, &hdl)) {
1725 (void) printf("Failed to get handle for SA znode\n");
1729 SA_ADD_BULK_ATTR(bulk, idx, sa_attr_table[ZPL_UID], NULL, &uid, 8);
1730 SA_ADD_BULK_ATTR(bulk, idx, sa_attr_table[ZPL_GID], NULL, &gid, 8);
1731 SA_ADD_BULK_ATTR(bulk, idx, sa_attr_table[ZPL_LINKS], NULL,
1733 SA_ADD_BULK_ATTR(bulk, idx, sa_attr_table[ZPL_GEN], NULL, &gen, 8);
1734 SA_ADD_BULK_ATTR(bulk, idx, sa_attr_table[ZPL_MODE], NULL,
1736 SA_ADD_BULK_ATTR(bulk, idx, sa_attr_table[ZPL_PARENT],
1738 SA_ADD_BULK_ATTR(bulk, idx, sa_attr_table[ZPL_SIZE], NULL,
1740 SA_ADD_BULK_ATTR(bulk, idx, sa_attr_table[ZPL_ATIME], NULL,
1742 SA_ADD_BULK_ATTR(bulk, idx, sa_attr_table[ZPL_MTIME], NULL,
1744 SA_ADD_BULK_ATTR(bulk, idx, sa_attr_table[ZPL_CRTIME], NULL,
1746 SA_ADD_BULK_ATTR(bulk, idx, sa_attr_table[ZPL_CTIME], NULL,
1748 SA_ADD_BULK_ATTR(bulk, idx, sa_attr_table[ZPL_FLAGS], NULL,
1751 if (sa_bulk_lookup(hdl, bulk, idx)) {
1752 (void) sa_handle_destroy(hdl);
1756 z_crtime = (time_t)crtm[0];
1757 z_atime = (time_t)acctm[0];
1758 z_mtime = (time_t)modtm[0];
1759 z_ctime = (time_t)chgtm[0];
1761 if (dump_opt['d'] > 4) {
1762 error = zfs_obj_to_path(os, object, path, sizeof (path));
1764 (void) snprintf(path, sizeof (path),
1765 "\?\?\?<object#%llu>", (u_longlong_t)object);
1767 (void) printf("\tpath %s\n", path);
1769 dump_uidgid(os, uid, gid);
1770 (void) printf("\tatime %s", ctime(&z_atime));
1771 (void) printf("\tmtime %s", ctime(&z_mtime));
1772 (void) printf("\tctime %s", ctime(&z_ctime));
1773 (void) printf("\tcrtime %s", ctime(&z_crtime));
1774 (void) printf("\tgen %llu\n", (u_longlong_t)gen);
1775 (void) printf("\tmode %llo\n", (u_longlong_t)mode);
1776 (void) printf("\tsize %llu\n", (u_longlong_t)fsize);
1777 (void) printf("\tparent %llu\n", (u_longlong_t)parent);
1778 (void) printf("\tlinks %llu\n", (u_longlong_t)links);
1779 (void) printf("\tpflags %llx\n", (u_longlong_t)pflags);
1780 if (sa_lookup(hdl, sa_attr_table[ZPL_XATTR], &xattr,
1781 sizeof (uint64_t)) == 0)
1782 (void) printf("\txattr %llu\n", (u_longlong_t)xattr);
1783 if (sa_lookup(hdl, sa_attr_table[ZPL_RDEV], &rdev,
1784 sizeof (uint64_t)) == 0)
1785 (void) printf("\trdev 0x%016llx\n", (u_longlong_t)rdev);
1786 sa_handle_destroy(hdl);
1791 dump_acl(objset_t *os, uint64_t object, void *data, size_t size)
1797 dump_dmu_objset(objset_t *os, uint64_t object, void *data, size_t size)
1801 static object_viewer_t *object_viewer[DMU_OT_NUMTYPES + 1] = {
1802 dump_none, /* unallocated */
1803 dump_zap, /* object directory */
1804 dump_uint64, /* object array */
1805 dump_none, /* packed nvlist */
1806 dump_packed_nvlist, /* packed nvlist size */
1807 dump_none, /* bpobj */
1808 dump_bpobj, /* bpobj header */
1809 dump_none, /* SPA space map header */
1810 dump_none, /* SPA space map */
1811 dump_none, /* ZIL intent log */
1812 dump_dnode, /* DMU dnode */
1813 dump_dmu_objset, /* DMU objset */
1814 dump_dsl_dir, /* DSL directory */
1815 dump_zap, /* DSL directory child map */
1816 dump_zap, /* DSL dataset snap map */
1817 dump_zap, /* DSL props */
1818 dump_dsl_dataset, /* DSL dataset */
1819 dump_znode, /* ZFS znode */
1820 dump_acl, /* ZFS V0 ACL */
1821 dump_uint8, /* ZFS plain file */
1822 dump_zpldir, /* ZFS directory */
1823 dump_zap, /* ZFS master node */
1824 dump_zap, /* ZFS delete queue */
1825 dump_uint8, /* zvol object */
1826 dump_zap, /* zvol prop */
1827 dump_uint8, /* other uint8[] */
1828 dump_uint64, /* other uint64[] */
1829 dump_zap, /* other ZAP */
1830 dump_zap, /* persistent error log */
1831 dump_uint8, /* SPA history */
1832 dump_history_offsets, /* SPA history offsets */
1833 dump_zap, /* Pool properties */
1834 dump_zap, /* DSL permissions */
1835 dump_acl, /* ZFS ACL */
1836 dump_uint8, /* ZFS SYSACL */
1837 dump_none, /* FUID nvlist */
1838 dump_packed_nvlist, /* FUID nvlist size */
1839 dump_zap, /* DSL dataset next clones */
1840 dump_zap, /* DSL scrub queue */
1841 dump_zap, /* ZFS user/group used */
1842 dump_zap, /* ZFS user/group quota */
1843 dump_zap, /* snapshot refcount tags */
1844 dump_ddt_zap, /* DDT ZAP object */
1845 dump_zap, /* DDT statistics */
1846 dump_znode, /* SA object */
1847 dump_zap, /* SA Master Node */
1848 dump_sa_attrs, /* SA attribute registration */
1849 dump_sa_layouts, /* SA attribute layouts */
1850 dump_zap, /* DSL scrub translations */
1851 dump_none, /* fake dedup BP */
1852 dump_zap, /* deadlist */
1853 dump_none, /* deadlist hdr */
1854 dump_zap, /* dsl clones */
1855 dump_bpobj_subobjs, /* bpobj subobjs */
1856 dump_unknown, /* Unknown type, must be last */
1860 dump_object(objset_t *os, uint64_t object, int verbosity, int *print_header)
1862 dmu_buf_t *db = NULL;
1863 dmu_object_info_t doi;
1867 char iblk[32], dblk[32], lsize[32], asize[32], fill[32];
1868 char bonus_size[32];
1872 /* make sure nicenum has enough space */
1873 CTASSERT(sizeof (iblk) >= NN_NUMBUF_SZ);
1874 CTASSERT(sizeof (dblk) >= NN_NUMBUF_SZ);
1875 CTASSERT(sizeof (lsize) >= NN_NUMBUF_SZ);
1876 CTASSERT(sizeof (asize) >= NN_NUMBUF_SZ);
1877 CTASSERT(sizeof (bonus_size) >= NN_NUMBUF_SZ);
1879 if (*print_header) {
1880 (void) printf("\n%10s %3s %5s %5s %5s %5s %6s %s\n",
1881 "Object", "lvl", "iblk", "dblk", "dsize", "lsize",
1887 dn = DMU_META_DNODE(os);
1889 error = dmu_bonus_hold(os, object, FTAG, &db);
1891 fatal("dmu_bonus_hold(%llu) failed, errno %u",
1893 bonus = db->db_data;
1894 bsize = db->db_size;
1895 dn = DB_DNODE((dmu_buf_impl_t *)db);
1897 dmu_object_info_from_dnode(dn, &doi);
1899 zdb_nicenum(doi.doi_metadata_block_size, iblk, sizeof (iblk));
1900 zdb_nicenum(doi.doi_data_block_size, dblk, sizeof (dblk));
1901 zdb_nicenum(doi.doi_max_offset, lsize, sizeof (lsize));
1902 zdb_nicenum(doi.doi_physical_blocks_512 << 9, asize, sizeof (asize));
1903 zdb_nicenum(doi.doi_bonus_size, bonus_size, sizeof (bonus_size));
1904 (void) sprintf(fill, "%6.2f", 100.0 * doi.doi_fill_count *
1905 doi.doi_data_block_size / (object == 0 ? DNODES_PER_BLOCK : 1) /
1906 doi.doi_max_offset);
1910 if (doi.doi_checksum != ZIO_CHECKSUM_INHERIT || verbosity >= 6) {
1911 (void) snprintf(aux + strlen(aux), sizeof (aux), " (K=%s)",
1912 ZDB_CHECKSUM_NAME(doi.doi_checksum));
1915 if (doi.doi_compress != ZIO_COMPRESS_INHERIT || verbosity >= 6) {
1916 (void) snprintf(aux + strlen(aux), sizeof (aux), " (Z=%s)",
1917 ZDB_COMPRESS_NAME(doi.doi_compress));
1920 (void) printf("%10lld %3u %5s %5s %5s %5s %6s %s%s\n",
1921 (u_longlong_t)object, doi.doi_indirection, iblk, dblk,
1922 asize, lsize, fill, ZDB_OT_NAME(doi.doi_type), aux);
1924 if (doi.doi_bonus_type != DMU_OT_NONE && verbosity > 3) {
1925 (void) printf("%10s %3s %5s %5s %5s %5s %6s %s\n",
1926 "", "", "", "", "", bonus_size, "bonus",
1927 ZDB_OT_NAME(doi.doi_bonus_type));
1930 if (verbosity >= 4) {
1931 (void) printf("\tdnode flags: %s%s%s\n",
1932 (dn->dn_phys->dn_flags & DNODE_FLAG_USED_BYTES) ?
1934 (dn->dn_phys->dn_flags & DNODE_FLAG_USERUSED_ACCOUNTED) ?
1935 "USERUSED_ACCOUNTED " : "",
1936 (dn->dn_phys->dn_flags & DNODE_FLAG_SPILL_BLKPTR) ?
1937 "SPILL_BLKPTR" : "");
1938 (void) printf("\tdnode maxblkid: %llu\n",
1939 (longlong_t)dn->dn_phys->dn_maxblkid);
1941 object_viewer[ZDB_OT_TYPE(doi.doi_bonus_type)](os, object,
1943 object_viewer[ZDB_OT_TYPE(doi.doi_type)](os, object, NULL, 0);
1950 if (verbosity >= 5) {
1952 * Report the list of segments that comprise the object.
1956 uint64_t blkfill = 1;
1959 if (dn->dn_type == DMU_OT_DNODE) {
1961 blkfill = DNODES_PER_BLOCK;
1966 /* make sure nicenum has enough space */
1967 CTASSERT(sizeof (segsize) >= NN_NUMBUF_SZ);
1968 error = dnode_next_offset(dn,
1969 0, &start, minlvl, blkfill, 0);
1973 error = dnode_next_offset(dn,
1974 DNODE_FIND_HOLE, &end, minlvl, blkfill, 0);
1975 zdb_nicenum(end - start, segsize, sizeof (segsize));
1976 (void) printf("\t\tsegment [%016llx, %016llx)"
1977 " size %5s\n", (u_longlong_t)start,
1978 (u_longlong_t)end, segsize);
1986 dmu_buf_rele(db, FTAG);
1989 static char *objset_types[DMU_OST_NUMTYPES] = {
1990 "NONE", "META", "ZPL", "ZVOL", "OTHER", "ANY" };
1993 dump_dir(objset_t *os)
1995 dmu_objset_stats_t dds;
1996 uint64_t object, object_count;
1997 uint64_t refdbytes, usedobjs, scratch;
1999 char blkbuf[BP_SPRINTF_LEN + 20];
2000 char osname[ZFS_MAX_DATASET_NAME_LEN];
2001 char *type = "UNKNOWN";
2002 int verbosity = dump_opt['d'];
2003 int print_header = 1;
2006 /* make sure nicenum has enough space */
2007 CTASSERT(sizeof (numbuf) >= NN_NUMBUF_SZ);
2009 dsl_pool_config_enter(dmu_objset_pool(os), FTAG);
2010 dmu_objset_fast_stat(os, &dds);
2011 dsl_pool_config_exit(dmu_objset_pool(os), FTAG);
2013 if (dds.dds_type < DMU_OST_NUMTYPES)
2014 type = objset_types[dds.dds_type];
2016 if (dds.dds_type == DMU_OST_META) {
2017 dds.dds_creation_txg = TXG_INITIAL;
2018 usedobjs = BP_GET_FILL(os->os_rootbp);
2019 refdbytes = dsl_dir_phys(os->os_spa->spa_dsl_pool->dp_mos_dir)->
2022 dmu_objset_space(os, &refdbytes, &scratch, &usedobjs, &scratch);
2025 ASSERT3U(usedobjs, ==, BP_GET_FILL(os->os_rootbp));
2027 zdb_nicenum(refdbytes, numbuf, sizeof (numbuf));
2029 if (verbosity >= 4) {
2030 (void) snprintf(blkbuf, sizeof (blkbuf), ", rootbp ");
2031 (void) snprintf_blkptr(blkbuf + strlen(blkbuf),
2032 sizeof (blkbuf) - strlen(blkbuf), os->os_rootbp);
2037 dmu_objset_name(os, osname);
2039 (void) printf("Dataset %s [%s], ID %llu, cr_txg %llu, "
2040 "%s, %llu objects%s\n",
2041 osname, type, (u_longlong_t)dmu_objset_id(os),
2042 (u_longlong_t)dds.dds_creation_txg,
2043 numbuf, (u_longlong_t)usedobjs, blkbuf);
2045 if (zopt_objects != 0) {
2046 for (i = 0; i < zopt_objects; i++)
2047 dump_object(os, zopt_object[i], verbosity,
2049 (void) printf("\n");
2053 if (dump_opt['i'] != 0 || verbosity >= 2)
2054 dump_intent_log(dmu_objset_zil(os));
2056 if (dmu_objset_ds(os) != NULL)
2057 dump_deadlist(&dmu_objset_ds(os)->ds_deadlist);
2062 if (BP_IS_HOLE(os->os_rootbp))
2065 dump_object(os, 0, verbosity, &print_header);
2067 if (DMU_USERUSED_DNODE(os) != NULL &&
2068 DMU_USERUSED_DNODE(os)->dn_type != 0) {
2069 dump_object(os, DMU_USERUSED_OBJECT, verbosity, &print_header);
2070 dump_object(os, DMU_GROUPUSED_OBJECT, verbosity, &print_header);
2074 while ((error = dmu_object_next(os, &object, B_FALSE, 0)) == 0) {
2075 dump_object(os, object, verbosity, &print_header);
2079 ASSERT3U(object_count, ==, usedobjs);
2081 (void) printf("\n");
2083 if (error != ESRCH) {
2084 (void) fprintf(stderr, "dmu_object_next() = %d\n", error);
2090 dump_uberblock(uberblock_t *ub, const char *header, const char *footer)
2092 time_t timestamp = ub->ub_timestamp;
2094 (void) printf(header ? header : "");
2095 (void) printf("\tmagic = %016llx\n", (u_longlong_t)ub->ub_magic);
2096 (void) printf("\tversion = %llu\n", (u_longlong_t)ub->ub_version);
2097 (void) printf("\ttxg = %llu\n", (u_longlong_t)ub->ub_txg);
2098 (void) printf("\tguid_sum = %llu\n", (u_longlong_t)ub->ub_guid_sum);
2099 (void) printf("\ttimestamp = %llu UTC = %s",
2100 (u_longlong_t)ub->ub_timestamp, asctime(localtime(×tamp)));
2101 if (dump_opt['u'] >= 3) {
2102 char blkbuf[BP_SPRINTF_LEN];
2103 snprintf_blkptr(blkbuf, sizeof (blkbuf), &ub->ub_rootbp);
2104 (void) printf("\trootbp = %s\n", blkbuf);
2106 (void) printf(footer ? footer : "");
2110 dump_config(spa_t *spa)
2117 error = dmu_bonus_hold(spa->spa_meta_objset,
2118 spa->spa_config_object, FTAG, &db);
2121 nvsize = *(uint64_t *)db->db_data;
2122 dmu_buf_rele(db, FTAG);
2124 (void) printf("\nMOS Configuration:\n");
2125 dump_packed_nvlist(spa->spa_meta_objset,
2126 spa->spa_config_object, (void *)&nvsize, 1);
2128 (void) fprintf(stderr, "dmu_bonus_hold(%llu) failed, errno %d",
2129 (u_longlong_t)spa->spa_config_object, error);
2134 dump_cachefile(const char *cachefile)
2137 struct stat64 statbuf;
2141 if ((fd = open64(cachefile, O_RDONLY)) < 0) {
2142 (void) printf("cannot open '%s': %s\n", cachefile,
2147 if (fstat64(fd, &statbuf) != 0) {
2148 (void) printf("failed to stat '%s': %s\n", cachefile,
2153 if ((buf = malloc(statbuf.st_size)) == NULL) {
2154 (void) fprintf(stderr, "failed to allocate %llu bytes\n",
2155 (u_longlong_t)statbuf.st_size);
2159 if (read(fd, buf, statbuf.st_size) != statbuf.st_size) {
2160 (void) fprintf(stderr, "failed to read %llu bytes\n",
2161 (u_longlong_t)statbuf.st_size);
2167 if (nvlist_unpack(buf, statbuf.st_size, &config, 0) != 0) {
2168 (void) fprintf(stderr, "failed to unpack nvlist\n");
2174 dump_nvlist(config, 0);
2176 nvlist_free(config);
2179 #define ZDB_MAX_UB_HEADER_SIZE 32
2182 dump_label_uberblocks(vdev_label_t *lbl, uint64_t ashift)
2186 char header[ZDB_MAX_UB_HEADER_SIZE];
2188 vd.vdev_ashift = ashift;
2189 vdp->vdev_top = vdp;
2191 for (int i = 0; i < VDEV_UBERBLOCK_COUNT(vdp); i++) {
2192 uint64_t uoff = VDEV_UBERBLOCK_OFFSET(vdp, i);
2193 uberblock_t *ub = (void *)((char *)lbl + uoff);
2195 if (uberblock_verify(ub))
2197 (void) snprintf(header, ZDB_MAX_UB_HEADER_SIZE,
2198 "Uberblock[%d]\n", i);
2199 dump_uberblock(ub, header, "");
2204 dump_label(const char *dev)
2208 char *path, *buf = label.vl_vdev_phys.vp_nvlist;
2209 size_t buflen = sizeof (label.vl_vdev_phys.vp_nvlist);
2210 struct stat64 statbuf;
2211 uint64_t psize, ashift;
2212 int len = strlen(dev) + 1;
2214 if (strncmp(dev, ZFS_DISK_ROOTD, strlen(ZFS_DISK_ROOTD)) == 0) {
2217 (void) snprintf(path, len, "%s%s", ZFS_RDISK_ROOTD,
2218 dev + strlen(ZFS_DISK_ROOTD));
2223 if ((fd = open64(path, O_RDONLY)) < 0) {
2224 (void) printf("cannot open '%s': %s\n", path, strerror(errno));
2229 if (fstat64(fd, &statbuf) != 0) {
2230 (void) printf("failed to stat '%s': %s\n", path,
2237 if (S_ISBLK(statbuf.st_mode)) {
2238 (void) printf("cannot use '%s': character device required\n",
2245 psize = statbuf.st_size;
2246 psize = P2ALIGN(psize, (uint64_t)sizeof (vdev_label_t));
2248 for (int l = 0; l < VDEV_LABELS; l++) {
2249 nvlist_t *config = NULL;
2251 (void) printf("--------------------------------------------\n");
2252 (void) printf("LABEL %d\n", l);
2253 (void) printf("--------------------------------------------\n");
2255 if (pread64(fd, &label, sizeof (label),
2256 vdev_label_offset(psize, l, 0)) != sizeof (label)) {
2257 (void) printf("failed to read label %d\n", l);
2261 if (nvlist_unpack(buf, buflen, &config, 0) != 0) {
2262 (void) printf("failed to unpack label %d\n", l);
2263 ashift = SPA_MINBLOCKSHIFT;
2265 nvlist_t *vdev_tree = NULL;
2267 dump_nvlist(config, 4);
2268 if ((nvlist_lookup_nvlist(config,
2269 ZPOOL_CONFIG_VDEV_TREE, &vdev_tree) != 0) ||
2270 (nvlist_lookup_uint64(vdev_tree,
2271 ZPOOL_CONFIG_ASHIFT, &ashift) != 0))
2272 ashift = SPA_MINBLOCKSHIFT;
2273 nvlist_free(config);
2276 dump_label_uberblocks(&label, ashift);
2283 static uint64_t dataset_feature_count[SPA_FEATURES];
2287 dump_one_dir(const char *dsname, void *arg)
2292 error = dmu_objset_own(dsname, DMU_OST_ANY, B_TRUE, FTAG, &os);
2294 (void) printf("Could not open %s, error %d\n", dsname, error);
2298 for (spa_feature_t f = 0; f < SPA_FEATURES; f++) {
2299 if (!dmu_objset_ds(os)->ds_feature_inuse[f])
2301 ASSERT(spa_feature_table[f].fi_flags &
2302 ZFEATURE_FLAG_PER_DATASET);
2303 dataset_feature_count[f]++;
2307 dmu_objset_disown(os, FTAG);
2308 fuid_table_destroy();
2309 sa_loaded = B_FALSE;
2316 #define PSIZE_HISTO_SIZE (SPA_OLD_MAXBLOCKSIZE / SPA_MINBLOCKSIZE + 2)
2317 typedef struct zdb_blkstats {
2323 uint64_t zb_ditto_samevdev;
2324 uint64_t zb_psize_histogram[PSIZE_HISTO_SIZE];
2328 * Extended object types to report deferred frees and dedup auto-ditto blocks.
2330 #define ZDB_OT_DEFERRED (DMU_OT_NUMTYPES + 0)
2331 #define ZDB_OT_DITTO (DMU_OT_NUMTYPES + 1)
2332 #define ZDB_OT_OTHER (DMU_OT_NUMTYPES + 2)
2333 #define ZDB_OT_TOTAL (DMU_OT_NUMTYPES + 3)
2335 static char *zdb_ot_extname[] = {
2342 #define ZB_TOTAL DN_MAX_LEVELS
2344 typedef struct zdb_cb {
2345 zdb_blkstats_t zcb_type[ZB_TOTAL + 1][ZDB_OT_TOTAL + 1];
2346 uint64_t zcb_dedup_asize;
2347 uint64_t zcb_dedup_blocks;
2348 uint64_t zcb_embedded_blocks[NUM_BP_EMBEDDED_TYPES];
2349 uint64_t zcb_embedded_histogram[NUM_BP_EMBEDDED_TYPES]
2352 uint64_t zcb_lastprint;
2353 uint64_t zcb_totalasize;
2354 uint64_t zcb_errors[256];
2361 zdb_count_block(zdb_cb_t *zcb, zilog_t *zilog, const blkptr_t *bp,
2362 dmu_object_type_t type)
2364 uint64_t refcnt = 0;
2366 ASSERT(type < ZDB_OT_TOTAL);
2368 if (zilog && zil_bp_tree_add(zilog, bp) != 0)
2371 for (int i = 0; i < 4; i++) {
2372 int l = (i < 2) ? BP_GET_LEVEL(bp) : ZB_TOTAL;
2373 int t = (i & 1) ? type : ZDB_OT_TOTAL;
2375 zdb_blkstats_t *zb = &zcb->zcb_type[l][t];
2377 zb->zb_asize += BP_GET_ASIZE(bp);
2378 zb->zb_lsize += BP_GET_LSIZE(bp);
2379 zb->zb_psize += BP_GET_PSIZE(bp);
2383 * The histogram is only big enough to record blocks up to
2384 * SPA_OLD_MAXBLOCKSIZE; larger blocks go into the last,
2387 int idx = BP_GET_PSIZE(bp) >> SPA_MINBLOCKSHIFT;
2388 idx = MIN(idx, SPA_OLD_MAXBLOCKSIZE / SPA_MINBLOCKSIZE + 1);
2389 zb->zb_psize_histogram[idx]++;
2391 zb->zb_gangs += BP_COUNT_GANG(bp);
2393 switch (BP_GET_NDVAS(bp)) {
2395 if (DVA_GET_VDEV(&bp->blk_dva[0]) ==
2396 DVA_GET_VDEV(&bp->blk_dva[1]))
2397 zb->zb_ditto_samevdev++;
2400 equal = (DVA_GET_VDEV(&bp->blk_dva[0]) ==
2401 DVA_GET_VDEV(&bp->blk_dva[1])) +
2402 (DVA_GET_VDEV(&bp->blk_dva[0]) ==
2403 DVA_GET_VDEV(&bp->blk_dva[2])) +
2404 (DVA_GET_VDEV(&bp->blk_dva[1]) ==
2405 DVA_GET_VDEV(&bp->blk_dva[2]));
2407 zb->zb_ditto_samevdev++;
2413 if (BP_IS_EMBEDDED(bp)) {
2414 zcb->zcb_embedded_blocks[BPE_GET_ETYPE(bp)]++;
2415 zcb->zcb_embedded_histogram[BPE_GET_ETYPE(bp)]
2416 [BPE_GET_PSIZE(bp)]++;
2423 if (BP_GET_DEDUP(bp)) {
2427 ddt = ddt_select(zcb->zcb_spa, bp);
2429 dde = ddt_lookup(ddt, bp, B_FALSE);
2434 ddt_phys_t *ddp = ddt_phys_select(dde, bp);
2435 ddt_phys_decref(ddp);
2436 refcnt = ddp->ddp_refcnt;
2437 if (ddt_phys_total_refcnt(dde) == 0)
2438 ddt_remove(ddt, dde);
2443 VERIFY3U(zio_wait(zio_claim(NULL, zcb->zcb_spa,
2444 refcnt ? 0 : spa_first_txg(zcb->zcb_spa),
2445 bp, NULL, NULL, ZIO_FLAG_CANFAIL)), ==, 0);
2450 zdb_blkptr_done(zio_t *zio)
2452 spa_t *spa = zio->io_spa;
2453 blkptr_t *bp = zio->io_bp;
2454 int ioerr = zio->io_error;
2455 zdb_cb_t *zcb = zio->io_private;
2456 zbookmark_phys_t *zb = &zio->io_bookmark;
2458 zio_data_buf_free(zio->io_data, zio->io_size);
2460 mutex_enter(&spa->spa_scrub_lock);
2461 spa->spa_scrub_inflight--;
2462 cv_broadcast(&spa->spa_scrub_io_cv);
2464 if (ioerr && !(zio->io_flags & ZIO_FLAG_SPECULATIVE)) {
2465 char blkbuf[BP_SPRINTF_LEN];
2467 zcb->zcb_haderrors = 1;
2468 zcb->zcb_errors[ioerr]++;
2470 if (dump_opt['b'] >= 2)
2471 snprintf_blkptr(blkbuf, sizeof (blkbuf), bp);
2475 (void) printf("zdb_blkptr_cb: "
2476 "Got error %d reading "
2477 "<%llu, %llu, %lld, %llx> %s -- skipping\n",
2479 (u_longlong_t)zb->zb_objset,
2480 (u_longlong_t)zb->zb_object,
2481 (u_longlong_t)zb->zb_level,
2482 (u_longlong_t)zb->zb_blkid,
2485 mutex_exit(&spa->spa_scrub_lock);
2490 zdb_blkptr_cb(spa_t *spa, zilog_t *zilog, const blkptr_t *bp,
2491 const zbookmark_phys_t *zb, const dnode_phys_t *dnp, void *arg)
2493 zdb_cb_t *zcb = arg;
2494 dmu_object_type_t type;
2495 boolean_t is_metadata;
2500 if (dump_opt['b'] >= 5 && bp->blk_birth > 0) {
2501 char blkbuf[BP_SPRINTF_LEN];
2502 snprintf_blkptr(blkbuf, sizeof (blkbuf), bp);
2503 (void) printf("objset %llu object %llu "
2504 "level %lld offset 0x%llx %s\n",
2505 (u_longlong_t)zb->zb_objset,
2506 (u_longlong_t)zb->zb_object,
2507 (longlong_t)zb->zb_level,
2508 (u_longlong_t)blkid2offset(dnp, bp, zb),
2515 type = BP_GET_TYPE(bp);
2517 zdb_count_block(zcb, zilog, bp,
2518 (type & DMU_OT_NEWTYPE) ? ZDB_OT_OTHER : type);
2520 is_metadata = (BP_GET_LEVEL(bp) != 0 || DMU_OT_IS_METADATA(type));
2522 if (!BP_IS_EMBEDDED(bp) &&
2523 (dump_opt['c'] > 1 || (dump_opt['c'] && is_metadata))) {
2524 size_t size = BP_GET_PSIZE(bp);
2525 void *data = zio_data_buf_alloc(size);
2526 int flags = ZIO_FLAG_CANFAIL | ZIO_FLAG_SCRUB | ZIO_FLAG_RAW;
2528 /* If it's an intent log block, failure is expected. */
2529 if (zb->zb_level == ZB_ZIL_LEVEL)
2530 flags |= ZIO_FLAG_SPECULATIVE;
2532 mutex_enter(&spa->spa_scrub_lock);
2533 while (spa->spa_scrub_inflight > max_inflight)
2534 cv_wait(&spa->spa_scrub_io_cv, &spa->spa_scrub_lock);
2535 spa->spa_scrub_inflight++;
2536 mutex_exit(&spa->spa_scrub_lock);
2538 zio_nowait(zio_read(NULL, spa, bp, data, size,
2539 zdb_blkptr_done, zcb, ZIO_PRIORITY_ASYNC_READ, flags, zb));
2542 zcb->zcb_readfails = 0;
2544 /* only call gethrtime() every 100 blocks */
2551 if (dump_opt['b'] < 5 && gethrtime() > zcb->zcb_lastprint + NANOSEC) {
2552 uint64_t now = gethrtime();
2554 uint64_t bytes = zcb->zcb_type[ZB_TOTAL][ZDB_OT_TOTAL].zb_asize;
2556 1 + bytes / (1 + ((now - zcb->zcb_start) / 1000 / 1000));
2558 (zcb->zcb_totalasize - bytes) / 1024 / kb_per_sec;
2560 /* make sure nicenum has enough space */
2561 CTASSERT(sizeof (buf) >= NN_NUMBUF_SZ);
2563 zfs_nicenum(bytes, buf, sizeof (buf));
2564 (void) fprintf(stderr,
2565 "\r%5s completed (%4dMB/s) "
2566 "estimated time remaining: %uhr %02umin %02usec ",
2567 buf, kb_per_sec / 1024,
2568 sec_remaining / 60 / 60,
2569 sec_remaining / 60 % 60,
2570 sec_remaining % 60);
2572 zcb->zcb_lastprint = now;
2579 zdb_leak(void *arg, uint64_t start, uint64_t size)
2583 (void) printf("leaked space: vdev %llu, offset 0x%llx, size %llu\n",
2584 (u_longlong_t)vd->vdev_id, (u_longlong_t)start, (u_longlong_t)size);
2587 static metaslab_ops_t zdb_metaslab_ops = {
2592 zdb_ddt_leak_init(spa_t *spa, zdb_cb_t *zcb)
2594 ddt_bookmark_t ddb = { 0 };
2598 while ((error = ddt_walk(spa, &ddb, &dde)) == 0) {
2600 ddt_phys_t *ddp = dde.dde_phys;
2602 if (ddb.ddb_class == DDT_CLASS_UNIQUE)
2605 ASSERT(ddt_phys_total_refcnt(&dde) > 1);
2607 for (int p = 0; p < DDT_PHYS_TYPES; p++, ddp++) {
2608 if (ddp->ddp_phys_birth == 0)
2610 ddt_bp_create(ddb.ddb_checksum,
2611 &dde.dde_key, ddp, &blk);
2612 if (p == DDT_PHYS_DITTO) {
2613 zdb_count_block(zcb, NULL, &blk, ZDB_OT_DITTO);
2615 zcb->zcb_dedup_asize +=
2616 BP_GET_ASIZE(&blk) * (ddp->ddp_refcnt - 1);
2617 zcb->zcb_dedup_blocks++;
2620 if (!dump_opt['L']) {
2621 ddt_t *ddt = spa->spa_ddt[ddb.ddb_checksum];
2623 VERIFY(ddt_lookup(ddt, &blk, B_TRUE) != NULL);
2628 ASSERT(error == ENOENT);
2632 zdb_leak_init(spa_t *spa, zdb_cb_t *zcb)
2636 if (!dump_opt['L']) {
2637 vdev_t *rvd = spa->spa_root_vdev;
2638 for (uint64_t c = 0; c < rvd->vdev_children; c++) {
2639 vdev_t *vd = rvd->vdev_child[c];
2640 for (uint64_t m = 0; m < vd->vdev_ms_count; m++) {
2641 metaslab_t *msp = vd->vdev_ms[m];
2642 mutex_enter(&msp->ms_lock);
2643 metaslab_unload(msp);
2646 * For leak detection, we overload the metaslab
2647 * ms_tree to contain allocated segments
2648 * instead of free segments. As a result,
2649 * we can't use the normal metaslab_load/unload
2652 if (msp->ms_sm != NULL) {
2653 (void) fprintf(stderr,
2654 "\rloading space map for "
2655 "vdev %llu of %llu, "
2656 "metaslab %llu of %llu ...",
2658 (longlong_t)rvd->vdev_children,
2660 (longlong_t)vd->vdev_ms_count);
2662 msp->ms_ops = &zdb_metaslab_ops;
2665 * We don't want to spend the CPU
2666 * manipulating the size-ordered
2667 * tree, so clear the range_tree
2670 msp->ms_tree->rt_ops = NULL;
2671 VERIFY0(space_map_load(msp->ms_sm,
2672 msp->ms_tree, SM_ALLOC));
2673 msp->ms_loaded = B_TRUE;
2675 mutex_exit(&msp->ms_lock);
2678 (void) fprintf(stderr, "\n");
2681 spa_config_enter(spa, SCL_CONFIG, FTAG, RW_READER);
2683 zdb_ddt_leak_init(spa, zcb);
2685 spa_config_exit(spa, SCL_CONFIG, FTAG);
2689 zdb_leak_fini(spa_t *spa)
2691 if (!dump_opt['L']) {
2692 vdev_t *rvd = spa->spa_root_vdev;
2693 for (int c = 0; c < rvd->vdev_children; c++) {
2694 vdev_t *vd = rvd->vdev_child[c];
2695 for (int m = 0; m < vd->vdev_ms_count; m++) {
2696 metaslab_t *msp = vd->vdev_ms[m];
2697 mutex_enter(&msp->ms_lock);
2700 * The ms_tree has been overloaded to
2701 * contain allocated segments. Now that we
2702 * finished traversing all blocks, any
2703 * block that remains in the ms_tree
2704 * represents an allocated block that we
2705 * did not claim during the traversal.
2706 * Claimed blocks would have been removed
2709 range_tree_vacate(msp->ms_tree, zdb_leak, vd);
2710 msp->ms_loaded = B_FALSE;
2712 mutex_exit(&msp->ms_lock);
2720 count_block_cb(void *arg, const blkptr_t *bp, dmu_tx_t *tx)
2722 zdb_cb_t *zcb = arg;
2724 if (dump_opt['b'] >= 5) {
2725 char blkbuf[BP_SPRINTF_LEN];
2726 snprintf_blkptr(blkbuf, sizeof (blkbuf), bp);
2727 (void) printf("[%s] %s\n",
2728 "deferred free", blkbuf);
2730 zdb_count_block(zcb, NULL, bp, ZDB_OT_DEFERRED);
2735 dump_block_stats(spa_t *spa)
2737 zdb_cb_t zcb = { 0 };
2738 zdb_blkstats_t *zb, *tzb;
2739 uint64_t norm_alloc, norm_space, total_alloc, total_found;
2740 int flags = TRAVERSE_PRE | TRAVERSE_PREFETCH_METADATA | TRAVERSE_HARD;
2741 boolean_t leaks = B_FALSE;
2743 (void) printf("\nTraversing all blocks %s%s%s%s%s...\n\n",
2744 (dump_opt['c'] || !dump_opt['L']) ? "to verify " : "",
2745 (dump_opt['c'] == 1) ? "metadata " : "",
2746 dump_opt['c'] ? "checksums " : "",
2747 (dump_opt['c'] && !dump_opt['L']) ? "and verify " : "",
2748 !dump_opt['L'] ? "nothing leaked " : "");
2751 * Load all space maps as SM_ALLOC maps, then traverse the pool
2752 * claiming each block we discover. If the pool is perfectly
2753 * consistent, the space maps will be empty when we're done.
2754 * Anything left over is a leak; any block we can't claim (because
2755 * it's not part of any space map) is a double allocation,
2756 * reference to a freed block, or an unclaimed log block.
2758 zdb_leak_init(spa, &zcb);
2761 * If there's a deferred-free bplist, process that first.
2763 (void) bpobj_iterate_nofree(&spa->spa_deferred_bpobj,
2764 count_block_cb, &zcb, NULL);
2765 if (spa_version(spa) >= SPA_VERSION_DEADLISTS) {
2766 (void) bpobj_iterate_nofree(&spa->spa_dsl_pool->dp_free_bpobj,
2767 count_block_cb, &zcb, NULL);
2769 if (spa_feature_is_active(spa, SPA_FEATURE_ASYNC_DESTROY)) {
2770 VERIFY3U(0, ==, bptree_iterate(spa->spa_meta_objset,
2771 spa->spa_dsl_pool->dp_bptree_obj, B_FALSE, count_block_cb,
2775 if (dump_opt['c'] > 1)
2776 flags |= TRAVERSE_PREFETCH_DATA;
2778 zcb.zcb_totalasize = metaslab_class_get_alloc(spa_normal_class(spa));
2779 zcb.zcb_start = zcb.zcb_lastprint = gethrtime();
2780 zcb.zcb_haderrors |= traverse_pool(spa, 0, flags, zdb_blkptr_cb, &zcb);
2783 * If we've traversed the data blocks then we need to wait for those
2784 * I/Os to complete. We leverage "The Godfather" zio to wait on
2785 * all async I/Os to complete.
2787 if (dump_opt['c']) {
2788 for (int i = 0; i < max_ncpus; i++) {
2789 (void) zio_wait(spa->spa_async_zio_root[i]);
2790 spa->spa_async_zio_root[i] = zio_root(spa, NULL, NULL,
2791 ZIO_FLAG_CANFAIL | ZIO_FLAG_SPECULATIVE |
2792 ZIO_FLAG_GODFATHER);
2796 if (zcb.zcb_haderrors) {
2797 (void) printf("\nError counts:\n\n");
2798 (void) printf("\t%5s %s\n", "errno", "count");
2799 for (int e = 0; e < 256; e++) {
2800 if (zcb.zcb_errors[e] != 0) {
2801 (void) printf("\t%5d %llu\n",
2802 e, (u_longlong_t)zcb.zcb_errors[e]);
2808 * Report any leaked segments.
2812 tzb = &zcb.zcb_type[ZB_TOTAL][ZDB_OT_TOTAL];
2814 norm_alloc = metaslab_class_get_alloc(spa_normal_class(spa));
2815 norm_space = metaslab_class_get_space(spa_normal_class(spa));
2817 total_alloc = norm_alloc + metaslab_class_get_alloc(spa_log_class(spa));
2818 total_found = tzb->zb_asize - zcb.zcb_dedup_asize;
2820 if (total_found == total_alloc) {
2822 (void) printf("\n\tNo leaks (block sum matches space"
2823 " maps exactly)\n");
2825 (void) printf("block traversal size %llu != alloc %llu "
2827 (u_longlong_t)total_found,
2828 (u_longlong_t)total_alloc,
2829 (dump_opt['L']) ? "unreachable" : "leaked",
2830 (longlong_t)(total_alloc - total_found));
2834 if (tzb->zb_count == 0)
2837 (void) printf("\n");
2838 (void) printf("\tbp count: %10llu\n",
2839 (u_longlong_t)tzb->zb_count);
2840 (void) printf("\tganged count: %10llu\n",
2841 (longlong_t)tzb->zb_gangs);
2842 (void) printf("\tbp logical: %10llu avg: %6llu\n",
2843 (u_longlong_t)tzb->zb_lsize,
2844 (u_longlong_t)(tzb->zb_lsize / tzb->zb_count));
2845 (void) printf("\tbp physical: %10llu avg:"
2846 " %6llu compression: %6.2f\n",
2847 (u_longlong_t)tzb->zb_psize,
2848 (u_longlong_t)(tzb->zb_psize / tzb->zb_count),
2849 (double)tzb->zb_lsize / tzb->zb_psize);
2850 (void) printf("\tbp allocated: %10llu avg:"
2851 " %6llu compression: %6.2f\n",
2852 (u_longlong_t)tzb->zb_asize,
2853 (u_longlong_t)(tzb->zb_asize / tzb->zb_count),
2854 (double)tzb->zb_lsize / tzb->zb_asize);
2855 (void) printf("\tbp deduped: %10llu ref>1:"
2856 " %6llu deduplication: %6.2f\n",
2857 (u_longlong_t)zcb.zcb_dedup_asize,
2858 (u_longlong_t)zcb.zcb_dedup_blocks,
2859 (double)zcb.zcb_dedup_asize / tzb->zb_asize + 1.0);
2860 (void) printf("\tSPA allocated: %10llu used: %5.2f%%\n",
2861 (u_longlong_t)norm_alloc, 100.0 * norm_alloc / norm_space);
2863 for (bp_embedded_type_t i = 0; i < NUM_BP_EMBEDDED_TYPES; i++) {
2864 if (zcb.zcb_embedded_blocks[i] == 0)
2866 (void) printf("\n");
2867 (void) printf("\tadditional, non-pointer bps of type %u: "
2869 i, (u_longlong_t)zcb.zcb_embedded_blocks[i]);
2871 if (dump_opt['b'] >= 3) {
2872 (void) printf("\t number of (compressed) bytes: "
2874 dump_histogram(zcb.zcb_embedded_histogram[i],
2875 sizeof (zcb.zcb_embedded_histogram[i]) /
2876 sizeof (zcb.zcb_embedded_histogram[i][0]), 0);
2880 if (tzb->zb_ditto_samevdev != 0) {
2881 (void) printf("\tDittoed blocks on same vdev: %llu\n",
2882 (longlong_t)tzb->zb_ditto_samevdev);
2885 if (dump_opt['b'] >= 2) {
2887 (void) printf("\nBlocks\tLSIZE\tPSIZE\tASIZE"
2888 "\t avg\t comp\t%%Total\tType\n");
2890 for (t = 0; t <= ZDB_OT_TOTAL; t++) {
2891 char csize[32], lsize[32], psize[32], asize[32];
2892 char avg[32], gang[32];
2895 /* make sure nicenum has enough space */
2896 CTASSERT(sizeof (csize) >= NN_NUMBUF_SZ);
2897 CTASSERT(sizeof (lsize) >= NN_NUMBUF_SZ);
2898 CTASSERT(sizeof (psize) >= NN_NUMBUF_SZ);
2899 CTASSERT(sizeof (asize) >= NN_NUMBUF_SZ);
2900 CTASSERT(sizeof (avg) >= NN_NUMBUF_SZ);
2901 CTASSERT(sizeof (gang) >= NN_NUMBUF_SZ);
2903 if (t < DMU_OT_NUMTYPES)
2904 typename = dmu_ot[t].ot_name;
2906 typename = zdb_ot_extname[t - DMU_OT_NUMTYPES];
2908 if (zcb.zcb_type[ZB_TOTAL][t].zb_asize == 0) {
2909 (void) printf("%6s\t%5s\t%5s\t%5s"
2910 "\t%5s\t%5s\t%6s\t%s\n",
2922 for (l = ZB_TOTAL - 1; l >= -1; l--) {
2923 level = (l == -1 ? ZB_TOTAL : l);
2924 zb = &zcb.zcb_type[level][t];
2926 if (zb->zb_asize == 0)
2929 if (dump_opt['b'] < 3 && level != ZB_TOTAL)
2932 if (level == 0 && zb->zb_asize ==
2933 zcb.zcb_type[ZB_TOTAL][t].zb_asize)
2936 zdb_nicenum(zb->zb_count, csize,
2938 zdb_nicenum(zb->zb_lsize, lsize,
2940 zdb_nicenum(zb->zb_psize, psize,
2942 zdb_nicenum(zb->zb_asize, asize,
2944 zdb_nicenum(zb->zb_asize / zb->zb_count, avg,
2946 zdb_nicenum(zb->zb_gangs, gang, sizeof (gang));
2948 (void) printf("%6s\t%5s\t%5s\t%5s\t%5s"
2950 csize, lsize, psize, asize, avg,
2951 (double)zb->zb_lsize / zb->zb_psize,
2952 100.0 * zb->zb_asize / tzb->zb_asize);
2954 if (level == ZB_TOTAL)
2955 (void) printf("%s\n", typename);
2957 (void) printf(" L%d %s\n",
2960 if (dump_opt['b'] >= 3 && zb->zb_gangs > 0) {
2961 (void) printf("\t number of ganged "
2962 "blocks: %s\n", gang);
2965 if (dump_opt['b'] >= 4) {
2966 (void) printf("psize "
2967 "(in 512-byte sectors): "
2968 "number of blocks\n");
2969 dump_histogram(zb->zb_psize_histogram,
2970 PSIZE_HISTO_SIZE, 0);
2976 (void) printf("\n");
2981 if (zcb.zcb_haderrors)
2987 typedef struct zdb_ddt_entry {
2989 uint64_t zdde_ref_blocks;
2990 uint64_t zdde_ref_lsize;
2991 uint64_t zdde_ref_psize;
2992 uint64_t zdde_ref_dsize;
2993 avl_node_t zdde_node;
2998 zdb_ddt_add_cb(spa_t *spa, zilog_t *zilog, const blkptr_t *bp,
2999 const zbookmark_phys_t *zb, const dnode_phys_t *dnp, void *arg)
3001 avl_tree_t *t = arg;
3003 zdb_ddt_entry_t *zdde, zdde_search;
3005 if (bp == NULL || BP_IS_HOLE(bp) || BP_IS_EMBEDDED(bp))
3008 if (dump_opt['S'] > 1 && zb->zb_level == ZB_ROOT_LEVEL) {
3009 (void) printf("traversing objset %llu, %llu objects, "
3010 "%lu blocks so far\n",
3011 (u_longlong_t)zb->zb_objset,
3012 (u_longlong_t)BP_GET_FILL(bp),
3016 if (BP_IS_HOLE(bp) || BP_GET_CHECKSUM(bp) == ZIO_CHECKSUM_OFF ||
3017 BP_GET_LEVEL(bp) > 0 || DMU_OT_IS_METADATA(BP_GET_TYPE(bp)))
3020 ddt_key_fill(&zdde_search.zdde_key, bp);
3022 zdde = avl_find(t, &zdde_search, &where);
3025 zdde = umem_zalloc(sizeof (*zdde), UMEM_NOFAIL);
3026 zdde->zdde_key = zdde_search.zdde_key;
3027 avl_insert(t, zdde, where);
3030 zdde->zdde_ref_blocks += 1;
3031 zdde->zdde_ref_lsize += BP_GET_LSIZE(bp);
3032 zdde->zdde_ref_psize += BP_GET_PSIZE(bp);
3033 zdde->zdde_ref_dsize += bp_get_dsize_sync(spa, bp);
3039 dump_simulated_ddt(spa_t *spa)
3042 void *cookie = NULL;
3043 zdb_ddt_entry_t *zdde;
3044 ddt_histogram_t ddh_total = { 0 };
3045 ddt_stat_t dds_total = { 0 };
3047 avl_create(&t, ddt_entry_compare,
3048 sizeof (zdb_ddt_entry_t), offsetof(zdb_ddt_entry_t, zdde_node));
3050 spa_config_enter(spa, SCL_CONFIG, FTAG, RW_READER);
3052 (void) traverse_pool(spa, 0, TRAVERSE_PRE | TRAVERSE_PREFETCH_METADATA,
3053 zdb_ddt_add_cb, &t);
3055 spa_config_exit(spa, SCL_CONFIG, FTAG);
3057 while ((zdde = avl_destroy_nodes(&t, &cookie)) != NULL) {
3059 uint64_t refcnt = zdde->zdde_ref_blocks;
3060 ASSERT(refcnt != 0);
3062 dds.dds_blocks = zdde->zdde_ref_blocks / refcnt;
3063 dds.dds_lsize = zdde->zdde_ref_lsize / refcnt;
3064 dds.dds_psize = zdde->zdde_ref_psize / refcnt;
3065 dds.dds_dsize = zdde->zdde_ref_dsize / refcnt;
3067 dds.dds_ref_blocks = zdde->zdde_ref_blocks;
3068 dds.dds_ref_lsize = zdde->zdde_ref_lsize;
3069 dds.dds_ref_psize = zdde->zdde_ref_psize;
3070 dds.dds_ref_dsize = zdde->zdde_ref_dsize;
3072 ddt_stat_add(&ddh_total.ddh_stat[highbit64(refcnt) - 1],
3075 umem_free(zdde, sizeof (*zdde));
3080 ddt_histogram_stat(&dds_total, &ddh_total);
3082 (void) printf("Simulated DDT histogram:\n");
3084 zpool_dump_ddt(&dds_total, &ddh_total);
3086 dump_dedup_ratio(&dds_total);
3090 dump_zpool(spa_t *spa)
3092 dsl_pool_t *dp = spa_get_dsl(spa);
3095 if (dump_opt['S']) {
3096 dump_simulated_ddt(spa);
3100 if (!dump_opt['e'] && dump_opt['C'] > 1) {
3101 (void) printf("\nCached configuration:\n");
3102 dump_nvlist(spa->spa_config, 8);
3109 dump_uberblock(&spa->spa_uberblock, "\nUberblock:\n", "\n");
3114 if (dump_opt['d'] > 2 || dump_opt['m'])
3115 dump_metaslabs(spa);
3117 dump_metaslab_groups(spa);
3119 if (dump_opt['d'] || dump_opt['i']) {
3120 dump_dir(dp->dp_meta_objset);
3121 if (dump_opt['d'] >= 3) {
3122 dump_full_bpobj(&spa->spa_deferred_bpobj,
3123 "Deferred frees", 0);
3124 if (spa_version(spa) >= SPA_VERSION_DEADLISTS) {
3126 &spa->spa_dsl_pool->dp_free_bpobj,
3127 "Pool snapshot frees", 0);
3130 if (spa_feature_is_active(spa,
3131 SPA_FEATURE_ASYNC_DESTROY)) {
3132 dump_bptree(spa->spa_meta_objset,
3133 spa->spa_dsl_pool->dp_bptree_obj,
3134 "Pool dataset frees");
3136 dump_dtl(spa->spa_root_vdev, 0);
3138 (void) dmu_objset_find(spa_name(spa), dump_one_dir,
3139 NULL, DS_FIND_SNAPSHOTS | DS_FIND_CHILDREN);
3141 for (spa_feature_t f = 0; f < SPA_FEATURES; f++) {
3144 if (!(spa_feature_table[f].fi_flags &
3145 ZFEATURE_FLAG_PER_DATASET)) {
3146 ASSERT0(dataset_feature_count[f]);
3149 (void) feature_get_refcount(spa,
3150 &spa_feature_table[f], &refcount);
3151 if (dataset_feature_count[f] != refcount) {
3152 (void) printf("%s feature refcount mismatch: "
3153 "%lld datasets != %lld refcount\n",
3154 spa_feature_table[f].fi_uname,
3155 (longlong_t)dataset_feature_count[f],
3156 (longlong_t)refcount);
3159 (void) printf("Verified %s feature refcount "
3160 "of %llu is correct\n",
3161 spa_feature_table[f].fi_uname,
3162 (longlong_t)refcount);
3166 if (rc == 0 && (dump_opt['b'] || dump_opt['c']))
3167 rc = dump_block_stats(spa);
3170 rc = verify_spacemap_refcounts(spa);
3173 show_pool_stats(spa);
3179 dump_debug_buffer();
3184 #define ZDB_FLAG_CHECKSUM 0x0001
3185 #define ZDB_FLAG_DECOMPRESS 0x0002
3186 #define ZDB_FLAG_BSWAP 0x0004
3187 #define ZDB_FLAG_GBH 0x0008
3188 #define ZDB_FLAG_INDIRECT 0x0010
3189 #define ZDB_FLAG_PHYS 0x0020
3190 #define ZDB_FLAG_RAW 0x0040
3191 #define ZDB_FLAG_PRINT_BLKPTR 0x0080
3196 zdb_print_blkptr(blkptr_t *bp, int flags)
3198 char blkbuf[BP_SPRINTF_LEN];
3200 if (flags & ZDB_FLAG_BSWAP)
3201 byteswap_uint64_array((void *)bp, sizeof (blkptr_t));
3203 snprintf_blkptr(blkbuf, sizeof (blkbuf), bp);
3204 (void) printf("%s\n", blkbuf);
3208 zdb_dump_indirect(blkptr_t *bp, int nbps, int flags)
3212 for (i = 0; i < nbps; i++)
3213 zdb_print_blkptr(&bp[i], flags);
3217 zdb_dump_gbh(void *buf, int flags)
3219 zdb_dump_indirect((blkptr_t *)buf, SPA_GBH_NBLKPTRS, flags);
3223 zdb_dump_block_raw(void *buf, uint64_t size, int flags)
3225 if (flags & ZDB_FLAG_BSWAP)
3226 byteswap_uint64_array(buf, size);
3227 (void) write(1, buf, size);
3231 zdb_dump_block(char *label, void *buf, uint64_t size, int flags)
3233 uint64_t *d = (uint64_t *)buf;
3234 int nwords = size / sizeof (uint64_t);
3235 int do_bswap = !!(flags & ZDB_FLAG_BSWAP);
3241 hdr = " 7 6 5 4 3 2 1 0 f e d c b a 9 8";
3243 hdr = " 0 1 2 3 4 5 6 7 8 9 a b c d e f";
3245 (void) printf("\n%s\n%6s %s 0123456789abcdef\n", label, "", hdr);
3247 for (i = 0; i < nwords; i += 2) {
3248 (void) printf("%06llx: %016llx %016llx ",
3249 (u_longlong_t)(i * sizeof (uint64_t)),
3250 (u_longlong_t)(do_bswap ? BSWAP_64(d[i]) : d[i]),
3251 (u_longlong_t)(do_bswap ? BSWAP_64(d[i + 1]) : d[i + 1]));
3254 for (j = 0; j < 2 * sizeof (uint64_t); j++)
3255 (void) printf("%c", isprint(c[j]) ? c[j] : '.');
3256 (void) printf("\n");
3261 * There are two acceptable formats:
3262 * leaf_name - For example: c1t0d0 or /tmp/ztest.0a
3263 * child[.child]* - For example: 0.1.1
3265 * The second form can be used to specify arbitrary vdevs anywhere
3266 * in the heirarchy. For example, in a pool with a mirror of
3267 * RAID-Zs, you can specify either RAID-Z vdev with 0.0 or 0.1 .
3270 zdb_vdev_lookup(vdev_t *vdev, char *path)
3278 /* First, assume the x.x.x.x format */
3279 i = (int)strtoul(path, &s, 10);
3280 if (s == path || (s && *s != '.' && *s != '\0'))
3282 if (i < 0 || i >= vdev->vdev_children)
3285 vdev = vdev->vdev_child[i];
3288 return (zdb_vdev_lookup(vdev, s+1));
3291 for (i = 0; i < vdev->vdev_children; i++) {
3292 vdev_t *vc = vdev->vdev_child[i];
3294 if (vc->vdev_path == NULL) {
3295 vc = zdb_vdev_lookup(vc, path);
3302 p = strrchr(vc->vdev_path, '/');
3303 p = p ? p + 1 : vc->vdev_path;
3304 q = &vc->vdev_path[strlen(vc->vdev_path) - 2];
3306 if (strcmp(vc->vdev_path, path) == 0)
3308 if (strcmp(p, path) == 0)
3310 if (strcmp(q, "s0") == 0 && strncmp(p, path, q - p) == 0)
3318 * Read a block from a pool and print it out. The syntax of the
3319 * block descriptor is:
3321 * pool:vdev_specifier:offset:size[:flags]
3323 * pool - The name of the pool you wish to read from
3324 * vdev_specifier - Which vdev (see comment for zdb_vdev_lookup)
3325 * offset - offset, in hex, in bytes
3326 * size - Amount of data to read, in hex, in bytes
3327 * flags - A string of characters specifying options
3328 * b: Decode a blkptr at given offset within block
3329 * *c: Calculate and display checksums
3330 * d: Decompress data before dumping
3331 * e: Byteswap data before dumping
3332 * g: Display data as a gang block header
3333 * i: Display as an indirect block
3334 * p: Do I/O to physical offset
3335 * r: Dump raw data to stdout
3337 * * = not yet implemented
3340 zdb_read_block(char *thing, spa_t *spa)
3342 blkptr_t blk, *bp = &blk;
3343 dva_t *dva = bp->blk_dva;
3345 uint64_t offset = 0, size = 0, psize = 0, lsize = 0, blkptr_offset = 0;
3348 void *pbuf, *lbuf, *buf;
3349 char *s, *p, *dup, *vdev, *flagstr;
3352 dup = strdup(thing);
3353 s = strtok(dup, ":");
3355 s = strtok(NULL, ":");
3356 offset = strtoull(s ? s : "", NULL, 16);
3357 s = strtok(NULL, ":");
3358 size = strtoull(s ? s : "", NULL, 16);
3359 s = strtok(NULL, ":");
3360 flagstr = s ? s : "";
3364 s = "size must not be zero";
3365 if (!IS_P2ALIGNED(size, DEV_BSIZE))
3366 s = "size must be a multiple of sector size";
3367 if (!IS_P2ALIGNED(offset, DEV_BSIZE))
3368 s = "offset must be a multiple of sector size";
3370 (void) printf("Invalid block specifier: %s - %s\n", thing, s);
3375 for (s = strtok(flagstr, ":"); s; s = strtok(NULL, ":")) {
3376 for (i = 0; flagstr[i]; i++) {
3377 int bit = flagbits[(uchar_t)flagstr[i]];
3380 (void) printf("***Invalid flag: %c\n",
3386 /* If it's not something with an argument, keep going */
3387 if ((bit & (ZDB_FLAG_CHECKSUM |
3388 ZDB_FLAG_PRINT_BLKPTR)) == 0)
3391 p = &flagstr[i + 1];
3392 if (bit == ZDB_FLAG_PRINT_BLKPTR)
3393 blkptr_offset = strtoull(p, &p, 16);
3394 if (*p != ':' && *p != '\0') {
3395 (void) printf("***Invalid flag arg: '%s'\n", s);
3399 i += p - &flagstr[i + 1]; /* skip over the number */
3403 vd = zdb_vdev_lookup(spa->spa_root_vdev, vdev);
3405 (void) printf("***Invalid vdev: %s\n", vdev);
3410 (void) fprintf(stderr, "Found vdev: %s\n",
3413 (void) fprintf(stderr, "Found vdev type: %s\n",
3414 vd->vdev_ops->vdev_op_type);
3420 pbuf = umem_alloc(SPA_MAXBLOCKSIZE, UMEM_NOFAIL);
3421 lbuf = umem_alloc(SPA_MAXBLOCKSIZE, UMEM_NOFAIL);
3425 DVA_SET_VDEV(&dva[0], vd->vdev_id);
3426 DVA_SET_OFFSET(&dva[0], offset);
3427 DVA_SET_GANG(&dva[0], !!(flags & ZDB_FLAG_GBH));
3428 DVA_SET_ASIZE(&dva[0], vdev_psize_to_asize(vd, psize));
3430 BP_SET_BIRTH(bp, TXG_INITIAL, TXG_INITIAL);
3432 BP_SET_LSIZE(bp, lsize);
3433 BP_SET_PSIZE(bp, psize);
3434 BP_SET_COMPRESS(bp, ZIO_COMPRESS_OFF);
3435 BP_SET_CHECKSUM(bp, ZIO_CHECKSUM_OFF);
3436 BP_SET_TYPE(bp, DMU_OT_NONE);
3437 BP_SET_LEVEL(bp, 0);
3438 BP_SET_DEDUP(bp, 0);
3439 BP_SET_BYTEORDER(bp, ZFS_HOST_BYTEORDER);
3441 spa_config_enter(spa, SCL_STATE, FTAG, RW_READER);
3442 zio = zio_root(spa, NULL, NULL, 0);
3444 if (vd == vd->vdev_top) {
3446 * Treat this as a normal block read.
3448 zio_nowait(zio_read(zio, spa, bp, pbuf, psize, NULL, NULL,
3449 ZIO_PRIORITY_SYNC_READ,
3450 ZIO_FLAG_CANFAIL | ZIO_FLAG_RAW, NULL));
3453 * Treat this as a vdev child I/O.
3455 zio_nowait(zio_vdev_child_io(zio, bp, vd, offset, pbuf, psize,
3456 ZIO_TYPE_READ, ZIO_PRIORITY_SYNC_READ,
3457 ZIO_FLAG_DONT_CACHE | ZIO_FLAG_DONT_QUEUE |
3458 ZIO_FLAG_DONT_PROPAGATE | ZIO_FLAG_DONT_RETRY |
3459 ZIO_FLAG_CANFAIL | ZIO_FLAG_RAW, NULL, NULL));
3462 error = zio_wait(zio);
3463 spa_config_exit(spa, SCL_STATE, FTAG);
3466 (void) printf("Read of %s failed, error: %d\n", thing, error);
3470 if (flags & ZDB_FLAG_DECOMPRESS) {
3472 * We don't know how the data was compressed, so just try
3473 * every decompress function at every inflated blocksize.
3475 enum zio_compress c;
3476 void *pbuf2 = umem_alloc(SPA_MAXBLOCKSIZE, UMEM_NOFAIL);
3477 void *lbuf2 = umem_alloc(SPA_MAXBLOCKSIZE, UMEM_NOFAIL);
3479 bcopy(pbuf, pbuf2, psize);
3481 VERIFY(random_get_pseudo_bytes((uint8_t *)pbuf + psize,
3482 SPA_MAXBLOCKSIZE - psize) == 0);
3484 VERIFY(random_get_pseudo_bytes((uint8_t *)pbuf2 + psize,
3485 SPA_MAXBLOCKSIZE - psize) == 0);
3487 for (lsize = SPA_MAXBLOCKSIZE; lsize > psize;
3488 lsize -= SPA_MINBLOCKSIZE) {
3489 for (c = 0; c < ZIO_COMPRESS_FUNCTIONS; c++) {
3490 if (zio_decompress_data(c, pbuf, lbuf,
3491 psize, lsize) == 0 &&
3492 zio_decompress_data(c, pbuf2, lbuf2,
3493 psize, lsize) == 0 &&
3494 bcmp(lbuf, lbuf2, lsize) == 0)
3497 if (c != ZIO_COMPRESS_FUNCTIONS)
3499 lsize -= SPA_MINBLOCKSIZE;
3502 umem_free(pbuf2, SPA_MAXBLOCKSIZE);
3503 umem_free(lbuf2, SPA_MAXBLOCKSIZE);
3505 if (lsize <= psize) {
3506 (void) printf("Decompress of %s failed\n", thing);
3516 if (flags & ZDB_FLAG_PRINT_BLKPTR)
3517 zdb_print_blkptr((blkptr_t *)(void *)
3518 ((uintptr_t)buf + (uintptr_t)blkptr_offset), flags);
3519 else if (flags & ZDB_FLAG_RAW)
3520 zdb_dump_block_raw(buf, size, flags);
3521 else if (flags & ZDB_FLAG_INDIRECT)
3522 zdb_dump_indirect((blkptr_t *)buf, size / sizeof (blkptr_t),
3524 else if (flags & ZDB_FLAG_GBH)
3525 zdb_dump_gbh(buf, flags);
3527 zdb_dump_block(thing, buf, size, flags);
3530 umem_free(pbuf, SPA_MAXBLOCKSIZE);
3531 umem_free(lbuf, SPA_MAXBLOCKSIZE);
3536 pool_match(nvlist_t *cfg, char *tgt)
3538 uint64_t v, guid = strtoull(tgt, NULL, 0);
3542 if (nvlist_lookup_uint64(cfg, ZPOOL_CONFIG_POOL_GUID, &v) == 0)
3545 if (nvlist_lookup_string(cfg, ZPOOL_CONFIG_POOL_NAME, &s) == 0)
3546 return (strcmp(s, tgt) == 0);
3552 find_zpool(char **target, nvlist_t **configp, int dirc, char **dirv)
3555 nvlist_t *match = NULL;
3560 importargs_t args = { 0 };
3564 args.can_be_active = B_TRUE;
3566 if ((sepp = strpbrk(*target, "/@")) != NULL) {
3571 pools = zpool_search_import(g_zfs, &args);
3573 if (pools != NULL) {
3574 nvpair_t *elem = NULL;
3575 while ((elem = nvlist_next_nvpair(pools, elem)) != NULL) {
3576 verify(nvpair_value_nvlist(elem, configp) == 0);
3577 if (pool_match(*configp, *target)) {
3579 if (match != NULL) {
3580 /* print previously found config */
3582 (void) printf("%s\n", name);
3583 dump_nvlist(match, 8);
3586 (void) printf("%s\n",
3588 dump_nvlist(*configp, 8);
3591 name = nvpair_name(elem);
3597 (void) fatal("\tMatched %d pools - use pool GUID "
3598 "instead of pool name or \n"
3599 "\tpool name part of a dataset name to select pool", count);
3604 * If pool GUID was specified for pool id, replace it with pool name
3606 if (name && (strstr(*target, name) != *target)) {
3607 int sz = 1 + strlen(name) + ((sepp) ? strlen(sepp) : 0);
3609 *target = umem_alloc(sz, UMEM_NOFAIL);
3610 (void) snprintf(*target, sz, "%s%s", name, sepp ? sepp : "");
3613 *configp = name ? match : NULL;
3619 main(int argc, char **argv)
3622 struct rlimit rl = { 1024, 1024 };
3624 objset_t *os = NULL;
3628 char **searchdirs = NULL;
3631 nvlist_t *policy = NULL;
3632 uint64_t max_txg = UINT64_MAX;
3633 int rewind = ZPOOL_NEVER_REWIND;
3634 char *spa_config_path_env;
3635 boolean_t target_is_spa = B_TRUE;
3637 (void) setrlimit(RLIMIT_NOFILE, &rl);
3638 (void) enable_extended_FILE_stdio(-1, -1);
3640 dprintf_setup(&argc, argv);
3643 * If there is an environment variable SPA_CONFIG_PATH it overrides
3644 * default spa_config_path setting. If -U flag is specified it will
3645 * override this environment variable settings once again.
3647 spa_config_path_env = getenv("SPA_CONFIG_PATH");
3648 if (spa_config_path_env != NULL)
3649 spa_config_path = spa_config_path_env;
3651 while ((c = getopt(argc, argv,
3652 "bcdhilmMI:suCDRSAFLXx:evp:t:U:PG")) != -1) {
3681 max_inflight = strtoull(optarg, NULL, 0);
3682 if (max_inflight == 0) {
3683 (void) fprintf(stderr, "maximum number "
3684 "of inflight I/Os must be greater "
3690 if (searchdirs == NULL) {
3691 searchdirs = umem_alloc(sizeof (char *),
3694 char **tmp = umem_alloc((nsearch + 1) *
3695 sizeof (char *), UMEM_NOFAIL);
3696 bcopy(searchdirs, tmp, nsearch *
3698 umem_free(searchdirs,
3699 nsearch * sizeof (char *));
3702 searchdirs[nsearch++] = optarg;
3705 max_txg = strtoull(optarg, NULL, 0);
3706 if (max_txg < TXG_INITIAL) {
3707 (void) fprintf(stderr, "incorrect txg "
3708 "specified: %s\n", optarg);
3713 spa_config_path = optarg;
3719 vn_dumpdir = optarg;
3727 if (!dump_opt['e'] && searchdirs != NULL) {
3728 (void) fprintf(stderr, "-p option requires use of -e\n");
3733 * ZDB does not typically re-read blocks; therefore limit the ARC
3734 * to 256 MB, which can be used entirely for metadata.
3736 zfs_arc_max = zfs_arc_meta_limit = 256 * 1024 * 1024;
3739 * "zdb -c" uses checksum-verifying scrub i/os which are async reads.
3740 * "zdb -b" uses traversal prefetch which uses async reads.
3741 * For good performance, let several of them be active at once.
3743 zfs_vdev_async_read_max_active = 10;
3746 g_zfs = libzfs_init();
3748 fatal("Fail to initialize zfs");
3751 verbose = MAX(verbose, 1);
3753 for (c = 0; c < 256; c++) {
3754 if (dump_all && !strchr("elAFLRSXP", c))
3757 dump_opt[c] += verbose;
3760 aok = (dump_opt['A'] == 1) || (dump_opt['A'] > 2);
3761 zfs_recover = (dump_opt['A'] > 1);
3766 if (argc < 2 && dump_opt['R'])
3769 if (!dump_opt['e'] && dump_opt['C']) {
3770 dump_cachefile(spa_config_path);
3776 if (dump_opt['l']) {
3777 dump_label(argv[0]);
3781 if (dump_opt['X'] || dump_opt['F'])
3782 rewind = ZPOOL_DO_REWIND |
3783 (dump_opt['X'] ? ZPOOL_EXTREME_REWIND : 0);
3785 if (nvlist_alloc(&policy, NV_UNIQUE_NAME_TYPE, 0) != 0 ||
3786 nvlist_add_uint64(policy, ZPOOL_REWIND_REQUEST_TXG, max_txg) != 0 ||
3787 nvlist_add_uint32(policy, ZPOOL_REWIND_REQUEST, rewind) != 0)
3788 fatal("internal error: %s", strerror(ENOMEM));
3793 if (dump_opt['e']) {
3794 nvlist_t *cfg = NULL;
3795 char *name = find_zpool(&target, &cfg, nsearch, searchdirs);
3799 if (dump_opt['C'] > 1) {
3800 (void) printf("\nConfiguration for import:\n");
3801 dump_nvlist(cfg, 8);
3803 if (nvlist_add_nvlist(cfg,
3804 ZPOOL_REWIND_POLICY, policy) != 0) {
3805 fatal("can't open '%s': %s",
3806 target, strerror(ENOMEM));
3808 if ((error = spa_import(name, cfg, NULL,
3809 ZFS_IMPORT_MISSING_LOG)) != 0) {
3810 error = spa_import(name, cfg, NULL,
3811 ZFS_IMPORT_VERBATIM);
3816 if (strpbrk(target, "/@") != NULL) {
3819 target_is_spa = B_FALSE;
3821 * Remove any trailing slash. Later code would get confused
3822 * by it, but we want to allow it so that "pool/" can
3823 * indicate that we want to dump the topmost filesystem,
3824 * rather than the whole pool.
3826 targetlen = strlen(target);
3827 if (targetlen != 0 && target[targetlen - 1] == '/')
3828 target[targetlen - 1] = '\0';
3832 if (target_is_spa || dump_opt['R']) {
3833 error = spa_open_rewind(target, &spa, FTAG, policy,
3837 * If we're missing the log device then
3838 * try opening the pool after clearing the
3841 mutex_enter(&spa_namespace_lock);
3842 if ((spa = spa_lookup(target)) != NULL &&
3843 spa->spa_log_state == SPA_LOG_MISSING) {
3844 spa->spa_log_state = SPA_LOG_CLEAR;
3847 mutex_exit(&spa_namespace_lock);
3850 error = spa_open_rewind(target, &spa,
3851 FTAG, policy, NULL);
3855 error = dmu_objset_own(target, DMU_OST_ANY,
3859 nvlist_free(policy);
3862 fatal("can't open '%s': %s", target, strerror(error));
3866 if (!dump_opt['R']) {
3868 zopt_objects = argc;
3869 zopt_object = calloc(zopt_objects, sizeof (uint64_t));
3870 for (i = 0; i < zopt_objects; i++) {
3872 zopt_object[i] = strtoull(argv[i], NULL, 0);
3873 if (zopt_object[i] == 0 && errno != 0)
3874 fatal("bad number %s: %s",
3875 argv[i], strerror(errno));
3880 } else if (zopt_objects > 0 && !dump_opt['m']) {
3881 dump_dir(spa->spa_meta_objset);
3886 flagbits['b'] = ZDB_FLAG_PRINT_BLKPTR;
3887 flagbits['c'] = ZDB_FLAG_CHECKSUM;
3888 flagbits['d'] = ZDB_FLAG_DECOMPRESS;
3889 flagbits['e'] = ZDB_FLAG_BSWAP;
3890 flagbits['g'] = ZDB_FLAG_GBH;
3891 flagbits['i'] = ZDB_FLAG_INDIRECT;
3892 flagbits['p'] = ZDB_FLAG_PHYS;
3893 flagbits['r'] = ZDB_FLAG_RAW;
3895 for (i = 0; i < argc; i++)
3896 zdb_read_block(argv[i], spa);
3899 (os != NULL) ? dmu_objset_disown(os, FTAG) : spa_close(spa, FTAG);
3901 fuid_table_destroy();
3902 sa_loaded = B_FALSE;
3904 dump_debug_buffer();