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] [-o var=value] "
123 "poolname [object...]\n"
124 " %s [-divPA] [-e -p path...] [-U config] dataset "
126 " %s -mM [-LXFPA] [-t txg] [-e [-p path...]] [-U config] "
127 "poolname [vdev [metaslab...]]\n"
128 " %s -R [-A] [-e [-p path...]] poolname "
129 "vdev:offset:size[:flags]\n"
130 " %s -S [-PA] [-e [-p path...]] [-U config] poolname\n"
131 " %s -l [-uA] device\n"
132 " %s -C [-A] [-U config]\n\n",
133 cmdname, cmdname, cmdname, cmdname, cmdname, cmdname, cmdname);
135 (void) fprintf(stderr, " Dataset name must include at least one "
136 "separator character '/' or '@'\n");
137 (void) fprintf(stderr, " If dataset name is specified, only that "
138 "dataset is dumped\n");
139 (void) fprintf(stderr, " If object numbers are specified, only "
140 "those objects are dumped\n\n");
141 (void) fprintf(stderr, " Options to control amount of output:\n");
142 (void) fprintf(stderr, " -u uberblock\n");
143 (void) fprintf(stderr, " -d dataset(s)\n");
144 (void) fprintf(stderr, " -i intent logs\n");
145 (void) fprintf(stderr, " -C config (or cachefile if alone)\n");
146 (void) fprintf(stderr, " -h pool history\n");
147 (void) fprintf(stderr, " -b block statistics\n");
148 (void) fprintf(stderr, " -m metaslabs\n");
149 (void) fprintf(stderr, " -M metaslab groups\n");
150 (void) fprintf(stderr, " -c checksum all metadata (twice for "
151 "all data) blocks\n");
152 (void) fprintf(stderr, " -s report stats on zdb's I/O\n");
153 (void) fprintf(stderr, " -D dedup statistics\n");
154 (void) fprintf(stderr, " -S simulate dedup to measure effect\n");
155 (void) fprintf(stderr, " -v verbose (applies to all others)\n");
156 (void) fprintf(stderr, " -l dump label contents\n");
157 (void) fprintf(stderr, " -L disable leak tracking (do not "
158 "load spacemaps)\n");
159 (void) fprintf(stderr, " -R read and display block from a "
161 (void) fprintf(stderr, " Below options are intended for use "
162 "with other options:\n");
163 (void) fprintf(stderr, " -A ignore assertions (-A), enable "
164 "panic recovery (-AA) or both (-AAA)\n");
165 (void) fprintf(stderr, " -F attempt automatic rewind within "
166 "safe range of transaction groups\n");
167 (void) fprintf(stderr, " -U <cachefile_path> -- use alternate "
169 (void) fprintf(stderr, " -X attempt extreme rewind (does not "
170 "work with dataset)\n");
171 (void) fprintf(stderr, " -e pool is exported/destroyed/"
172 "has altroot/not in a cachefile\n");
173 (void) fprintf(stderr, " -p <path> -- use one or more with "
174 "-e to specify path to vdev dir\n");
175 (void) fprintf(stderr, " -x <dumpdir> -- "
176 "dump all read blocks into specified directory\n");
177 (void) fprintf(stderr, " -P print numbers in parseable form\n");
178 (void) fprintf(stderr, " -t <txg> -- highest txg to use when "
179 "searching for uberblocks\n");
180 (void) fprintf(stderr, " -I <number of inflight I/Os> -- "
181 "specify the maximum number of "
182 "checksumming I/Os [default is 200]\n");
183 (void) fprintf(stderr, " -G dump zfs_dbgmsg buffer before "
185 (void) fprintf(stderr, " -o <variable>=<value> set global "
186 "variable to an unsigned 32-bit integer value\n");
187 (void) fprintf(stderr, "Specify an option more than once (e.g. -bb) "
188 "to make only that option verbose\n");
189 (void) fprintf(stderr, "Default is to dump everything non-verbosely\n");
198 zfs_dbgmsg_print("zdb");
203 * Called for usage errors that are discovered after a call to spa_open(),
204 * dmu_bonus_hold(), or pool_match(). abort() is called for other errors.
208 fatal(const char *fmt, ...)
213 (void) fprintf(stderr, "%s: ", cmdname);
214 (void) vfprintf(stderr, fmt, ap);
216 (void) fprintf(stderr, "\n");
225 dump_packed_nvlist(objset_t *os, uint64_t object, void *data, size_t size)
228 size_t nvsize = *(uint64_t *)data;
229 char *packed = umem_alloc(nvsize, UMEM_NOFAIL);
231 VERIFY(0 == dmu_read(os, object, 0, nvsize, packed, DMU_READ_PREFETCH));
233 VERIFY(nvlist_unpack(packed, nvsize, &nv, 0) == 0);
235 umem_free(packed, nvsize);
244 dump_history_offsets(objset_t *os, uint64_t object, void *data, size_t size)
246 spa_history_phys_t *shp = data;
251 (void) printf("\t\tpool_create_len = %llu\n",
252 (u_longlong_t)shp->sh_pool_create_len);
253 (void) printf("\t\tphys_max_off = %llu\n",
254 (u_longlong_t)shp->sh_phys_max_off);
255 (void) printf("\t\tbof = %llu\n",
256 (u_longlong_t)shp->sh_bof);
257 (void) printf("\t\teof = %llu\n",
258 (u_longlong_t)shp->sh_eof);
259 (void) printf("\t\trecords_lost = %llu\n",
260 (u_longlong_t)shp->sh_records_lost);
264 zdb_nicenum(uint64_t num, char *buf, size_t buflen)
267 (void) snprintf(buf, buflen, "%llu", (longlong_t)num);
269 nicenum(num, buf, sizeof (buf));
272 const char histo_stars[] = "****************************************";
273 const int histo_width = sizeof (histo_stars) - 1;
276 dump_histogram(const uint64_t *histo, int size, int offset)
279 int minidx = size - 1;
283 for (i = 0; i < size; i++) {
286 if (histo[i] > 0 && i > maxidx)
288 if (histo[i] > 0 && i < minidx)
292 if (max < histo_width)
295 for (i = minidx; i <= maxidx; i++) {
296 (void) printf("\t\t\t%3u: %6llu %s\n",
297 i + offset, (u_longlong_t)histo[i],
298 &histo_stars[(max - histo[i]) * histo_width / max]);
303 dump_zap_stats(objset_t *os, uint64_t object)
308 error = zap_get_stats(os, object, &zs);
312 if (zs.zs_ptrtbl_len == 0) {
313 ASSERT(zs.zs_num_blocks == 1);
314 (void) printf("\tmicrozap: %llu bytes, %llu entries\n",
315 (u_longlong_t)zs.zs_blocksize,
316 (u_longlong_t)zs.zs_num_entries);
320 (void) printf("\tFat ZAP stats:\n");
322 (void) printf("\t\tPointer table:\n");
323 (void) printf("\t\t\t%llu elements\n",
324 (u_longlong_t)zs.zs_ptrtbl_len);
325 (void) printf("\t\t\tzt_blk: %llu\n",
326 (u_longlong_t)zs.zs_ptrtbl_zt_blk);
327 (void) printf("\t\t\tzt_numblks: %llu\n",
328 (u_longlong_t)zs.zs_ptrtbl_zt_numblks);
329 (void) printf("\t\t\tzt_shift: %llu\n",
330 (u_longlong_t)zs.zs_ptrtbl_zt_shift);
331 (void) printf("\t\t\tzt_blks_copied: %llu\n",
332 (u_longlong_t)zs.zs_ptrtbl_blks_copied);
333 (void) printf("\t\t\tzt_nextblk: %llu\n",
334 (u_longlong_t)zs.zs_ptrtbl_nextblk);
336 (void) printf("\t\tZAP entries: %llu\n",
337 (u_longlong_t)zs.zs_num_entries);
338 (void) printf("\t\tLeaf blocks: %llu\n",
339 (u_longlong_t)zs.zs_num_leafs);
340 (void) printf("\t\tTotal blocks: %llu\n",
341 (u_longlong_t)zs.zs_num_blocks);
342 (void) printf("\t\tzap_block_type: 0x%llx\n",
343 (u_longlong_t)zs.zs_block_type);
344 (void) printf("\t\tzap_magic: 0x%llx\n",
345 (u_longlong_t)zs.zs_magic);
346 (void) printf("\t\tzap_salt: 0x%llx\n",
347 (u_longlong_t)zs.zs_salt);
349 (void) printf("\t\tLeafs with 2^n pointers:\n");
350 dump_histogram(zs.zs_leafs_with_2n_pointers, ZAP_HISTOGRAM_SIZE, 0);
352 (void) printf("\t\tBlocks with n*5 entries:\n");
353 dump_histogram(zs.zs_blocks_with_n5_entries, ZAP_HISTOGRAM_SIZE, 0);
355 (void) printf("\t\tBlocks n/10 full:\n");
356 dump_histogram(zs.zs_blocks_n_tenths_full, ZAP_HISTOGRAM_SIZE, 0);
358 (void) printf("\t\tEntries with n chunks:\n");
359 dump_histogram(zs.zs_entries_using_n_chunks, ZAP_HISTOGRAM_SIZE, 0);
361 (void) printf("\t\tBuckets with n entries:\n");
362 dump_histogram(zs.zs_buckets_with_n_entries, ZAP_HISTOGRAM_SIZE, 0);
367 dump_none(objset_t *os, uint64_t object, void *data, size_t size)
373 dump_unknown(objset_t *os, uint64_t object, void *data, size_t size)
375 (void) printf("\tUNKNOWN OBJECT TYPE\n");
380 dump_uint8(objset_t *os, uint64_t object, void *data, size_t size)
386 dump_uint64(objset_t *os, uint64_t object, void *data, size_t size)
392 dump_zap(objset_t *os, uint64_t object, void *data, size_t size)
395 zap_attribute_t attr;
399 dump_zap_stats(os, object);
402 for (zap_cursor_init(&zc, os, object);
403 zap_cursor_retrieve(&zc, &attr) == 0;
404 zap_cursor_advance(&zc)) {
405 (void) printf("\t\t%s = ", attr.za_name);
406 if (attr.za_num_integers == 0) {
410 prop = umem_zalloc(attr.za_num_integers *
411 attr.za_integer_length, UMEM_NOFAIL);
412 (void) zap_lookup(os, object, attr.za_name,
413 attr.za_integer_length, attr.za_num_integers, prop);
414 if (attr.za_integer_length == 1) {
415 (void) printf("%s", (char *)prop);
417 for (i = 0; i < attr.za_num_integers; i++) {
418 switch (attr.za_integer_length) {
421 ((uint16_t *)prop)[i]);
425 ((uint32_t *)prop)[i]);
428 (void) printf("%lld ",
429 (u_longlong_t)((int64_t *)prop)[i]);
435 umem_free(prop, attr.za_num_integers * attr.za_integer_length);
437 zap_cursor_fini(&zc);
441 dump_bpobj(objset_t *os, uint64_t object, void *data, size_t size)
443 bpobj_phys_t *bpop = data;
444 char bytes[32], comp[32], uncomp[32];
446 /* make sure the output won't get truncated */
447 CTASSERT(sizeof (bytes) >= NN_NUMBUF_SZ);
448 CTASSERT(sizeof (comp) >= NN_NUMBUF_SZ);
449 CTASSERT(sizeof (uncomp) >= NN_NUMBUF_SZ);
454 zdb_nicenum(bpop->bpo_bytes, bytes, sizeof (bytes));
455 zdb_nicenum(bpop->bpo_comp, comp, sizeof (comp));
456 zdb_nicenum(bpop->bpo_uncomp, uncomp, sizeof (uncomp));
458 (void) printf("\t\tnum_blkptrs = %llu\n",
459 (u_longlong_t)bpop->bpo_num_blkptrs);
460 (void) printf("\t\tbytes = %s\n", bytes);
461 if (size >= BPOBJ_SIZE_V1) {
462 (void) printf("\t\tcomp = %s\n", comp);
463 (void) printf("\t\tuncomp = %s\n", uncomp);
465 if (size >= sizeof (*bpop)) {
466 (void) printf("\t\tsubobjs = %llu\n",
467 (u_longlong_t)bpop->bpo_subobjs);
468 (void) printf("\t\tnum_subobjs = %llu\n",
469 (u_longlong_t)bpop->bpo_num_subobjs);
472 if (dump_opt['d'] < 5)
475 for (uint64_t i = 0; i < bpop->bpo_num_blkptrs; i++) {
476 char blkbuf[BP_SPRINTF_LEN];
479 int err = dmu_read(os, object,
480 i * sizeof (bp), sizeof (bp), &bp, 0);
482 (void) printf("got error %u from dmu_read\n", err);
485 snprintf_blkptr_compact(blkbuf, sizeof (blkbuf), &bp);
486 (void) printf("\t%s\n", blkbuf);
492 dump_bpobj_subobjs(objset_t *os, uint64_t object, void *data, size_t size)
494 dmu_object_info_t doi;
496 VERIFY0(dmu_object_info(os, object, &doi));
497 uint64_t *subobjs = kmem_alloc(doi.doi_max_offset, KM_SLEEP);
499 int err = dmu_read(os, object, 0, doi.doi_max_offset, subobjs, 0);
501 (void) printf("got error %u from dmu_read\n", err);
502 kmem_free(subobjs, doi.doi_max_offset);
506 int64_t last_nonzero = -1;
507 for (uint64_t i = 0; i < doi.doi_max_offset / 8; i++) {
512 for (int64_t i = 0; i <= last_nonzero; i++) {
513 (void) printf("\t%llu\n", (longlong_t)subobjs[i]);
515 kmem_free(subobjs, doi.doi_max_offset);
520 dump_ddt_zap(objset_t *os, uint64_t object, void *data, size_t size)
522 dump_zap_stats(os, object);
523 /* contents are printed elsewhere, properly decoded */
528 dump_sa_attrs(objset_t *os, uint64_t object, void *data, size_t size)
531 zap_attribute_t attr;
533 dump_zap_stats(os, object);
536 for (zap_cursor_init(&zc, os, object);
537 zap_cursor_retrieve(&zc, &attr) == 0;
538 zap_cursor_advance(&zc)) {
539 (void) printf("\t\t%s = ", attr.za_name);
540 if (attr.za_num_integers == 0) {
544 (void) printf(" %llx : [%d:%d:%d]\n",
545 (u_longlong_t)attr.za_first_integer,
546 (int)ATTR_LENGTH(attr.za_first_integer),
547 (int)ATTR_BSWAP(attr.za_first_integer),
548 (int)ATTR_NUM(attr.za_first_integer));
550 zap_cursor_fini(&zc);
555 dump_sa_layouts(objset_t *os, uint64_t object, void *data, size_t size)
558 zap_attribute_t attr;
559 uint16_t *layout_attrs;
562 dump_zap_stats(os, object);
565 for (zap_cursor_init(&zc, os, object);
566 zap_cursor_retrieve(&zc, &attr) == 0;
567 zap_cursor_advance(&zc)) {
568 (void) printf("\t\t%s = [", attr.za_name);
569 if (attr.za_num_integers == 0) {
574 VERIFY(attr.za_integer_length == 2);
575 layout_attrs = umem_zalloc(attr.za_num_integers *
576 attr.za_integer_length, UMEM_NOFAIL);
578 VERIFY(zap_lookup(os, object, attr.za_name,
579 attr.za_integer_length,
580 attr.za_num_integers, layout_attrs) == 0);
582 for (i = 0; i != attr.za_num_integers; i++)
583 (void) printf(" %d ", (int)layout_attrs[i]);
584 (void) printf("]\n");
585 umem_free(layout_attrs,
586 attr.za_num_integers * attr.za_integer_length);
588 zap_cursor_fini(&zc);
593 dump_zpldir(objset_t *os, uint64_t object, void *data, size_t size)
596 zap_attribute_t attr;
597 const char *typenames[] = {
598 /* 0 */ "not specified",
600 /* 2 */ "Character Device",
601 /* 3 */ "3 (invalid)",
603 /* 5 */ "5 (invalid)",
604 /* 6 */ "Block Device",
605 /* 7 */ "7 (invalid)",
606 /* 8 */ "Regular File",
607 /* 9 */ "9 (invalid)",
608 /* 10 */ "Symbolic Link",
609 /* 11 */ "11 (invalid)",
612 /* 14 */ "Event Port",
613 /* 15 */ "15 (invalid)",
616 dump_zap_stats(os, object);
619 for (zap_cursor_init(&zc, os, object);
620 zap_cursor_retrieve(&zc, &attr) == 0;
621 zap_cursor_advance(&zc)) {
622 (void) printf("\t\t%s = %lld (type: %s)\n",
623 attr.za_name, ZFS_DIRENT_OBJ(attr.za_first_integer),
624 typenames[ZFS_DIRENT_TYPE(attr.za_first_integer)]);
626 zap_cursor_fini(&zc);
630 get_dtl_refcount(vdev_t *vd)
634 if (vd->vdev_ops->vdev_op_leaf) {
635 space_map_t *sm = vd->vdev_dtl_sm;
638 sm->sm_dbuf->db_size == sizeof (space_map_phys_t))
643 for (int c = 0; c < vd->vdev_children; c++)
644 refcount += get_dtl_refcount(vd->vdev_child[c]);
649 get_metaslab_refcount(vdev_t *vd)
653 if (vd->vdev_top == vd && !vd->vdev_removing) {
654 for (int m = 0; m < vd->vdev_ms_count; m++) {
655 space_map_t *sm = vd->vdev_ms[m]->ms_sm;
658 sm->sm_dbuf->db_size == sizeof (space_map_phys_t))
662 for (int c = 0; c < vd->vdev_children; c++)
663 refcount += get_metaslab_refcount(vd->vdev_child[c]);
669 verify_spacemap_refcounts(spa_t *spa)
671 uint64_t expected_refcount = 0;
672 uint64_t actual_refcount;
674 (void) feature_get_refcount(spa,
675 &spa_feature_table[SPA_FEATURE_SPACEMAP_HISTOGRAM],
677 actual_refcount = get_dtl_refcount(spa->spa_root_vdev);
678 actual_refcount += get_metaslab_refcount(spa->spa_root_vdev);
680 if (expected_refcount != actual_refcount) {
681 (void) printf("space map refcount mismatch: expected %lld != "
683 (longlong_t)expected_refcount,
684 (longlong_t)actual_refcount);
691 dump_spacemap(objset_t *os, space_map_t *sm)
693 uint64_t alloc, offset, entry;
694 char *ddata[] = { "ALLOC", "FREE", "CONDENSE", "INVALID",
695 "INVALID", "INVALID", "INVALID", "INVALID" };
701 * Print out the freelist entries in both encoded and decoded form.
704 for (offset = 0; offset < space_map_length(sm);
705 offset += sizeof (entry)) {
706 uint8_t mapshift = sm->sm_shift;
708 VERIFY0(dmu_read(os, space_map_object(sm), offset,
709 sizeof (entry), &entry, DMU_READ_PREFETCH));
710 if (SM_DEBUG_DECODE(entry)) {
712 (void) printf("\t [%6llu] %s: txg %llu, pass %llu\n",
713 (u_longlong_t)(offset / sizeof (entry)),
714 ddata[SM_DEBUG_ACTION_DECODE(entry)],
715 (u_longlong_t)SM_DEBUG_TXG_DECODE(entry),
716 (u_longlong_t)SM_DEBUG_SYNCPASS_DECODE(entry));
718 (void) printf("\t [%6llu] %c range:"
719 " %010llx-%010llx size: %06llx\n",
720 (u_longlong_t)(offset / sizeof (entry)),
721 SM_TYPE_DECODE(entry) == SM_ALLOC ? 'A' : 'F',
722 (u_longlong_t)((SM_OFFSET_DECODE(entry) <<
723 mapshift) + sm->sm_start),
724 (u_longlong_t)((SM_OFFSET_DECODE(entry) <<
725 mapshift) + sm->sm_start +
726 (SM_RUN_DECODE(entry) << mapshift)),
727 (u_longlong_t)(SM_RUN_DECODE(entry) << mapshift));
728 if (SM_TYPE_DECODE(entry) == SM_ALLOC)
729 alloc += SM_RUN_DECODE(entry) << mapshift;
731 alloc -= SM_RUN_DECODE(entry) << mapshift;
734 if (alloc != space_map_allocated(sm)) {
735 (void) printf("space_map_object alloc (%llu) INCONSISTENT "
736 "with space map summary (%llu)\n",
737 (u_longlong_t)space_map_allocated(sm), (u_longlong_t)alloc);
742 dump_metaslab_stats(metaslab_t *msp)
745 range_tree_t *rt = msp->ms_tree;
746 avl_tree_t *t = &msp->ms_size_tree;
747 int free_pct = range_tree_space(rt) * 100 / msp->ms_size;
749 /* max sure nicenum has enough space */
750 CTASSERT(sizeof (maxbuf) >= NN_NUMBUF_SZ);
752 zdb_nicenum(metaslab_block_maxsize(msp), maxbuf, sizeof (maxbuf));
754 (void) printf("\t %25s %10lu %7s %6s %4s %4d%%\n",
755 "segments", avl_numnodes(t), "maxsize", maxbuf,
756 "freepct", free_pct);
757 (void) printf("\tIn-memory histogram:\n");
758 dump_histogram(rt->rt_histogram, RANGE_TREE_HISTOGRAM_SIZE, 0);
762 dump_metaslab(metaslab_t *msp)
764 vdev_t *vd = msp->ms_group->mg_vd;
765 spa_t *spa = vd->vdev_spa;
766 space_map_t *sm = msp->ms_sm;
769 zdb_nicenum(msp->ms_size - space_map_allocated(sm), freebuf,
773 "\tmetaslab %6llu offset %12llx spacemap %6llu free %5s\n",
774 (u_longlong_t)msp->ms_id, (u_longlong_t)msp->ms_start,
775 (u_longlong_t)space_map_object(sm), freebuf);
777 if (dump_opt['m'] > 2 && !dump_opt['L']) {
778 mutex_enter(&msp->ms_lock);
779 metaslab_load_wait(msp);
780 if (!msp->ms_loaded) {
781 VERIFY0(metaslab_load(msp));
782 range_tree_stat_verify(msp->ms_tree);
784 dump_metaslab_stats(msp);
785 metaslab_unload(msp);
786 mutex_exit(&msp->ms_lock);
789 if (dump_opt['m'] > 1 && sm != NULL &&
790 spa_feature_is_active(spa, SPA_FEATURE_SPACEMAP_HISTOGRAM)) {
792 * The space map histogram represents free space in chunks
793 * of sm_shift (i.e. bucket 0 refers to 2^sm_shift).
795 (void) printf("\tOn-disk histogram:\t\tfragmentation %llu\n",
796 (u_longlong_t)msp->ms_fragmentation);
797 dump_histogram(sm->sm_phys->smp_histogram,
798 SPACE_MAP_HISTOGRAM_SIZE, sm->sm_shift);
801 if (dump_opt['d'] > 5 || dump_opt['m'] > 3) {
802 ASSERT(msp->ms_size == (1ULL << vd->vdev_ms_shift));
804 mutex_enter(&msp->ms_lock);
805 dump_spacemap(spa->spa_meta_objset, msp->ms_sm);
806 mutex_exit(&msp->ms_lock);
811 print_vdev_metaslab_header(vdev_t *vd)
813 (void) printf("\tvdev %10llu\n\t%-10s%5llu %-19s %-15s %-10s\n",
814 (u_longlong_t)vd->vdev_id,
815 "metaslabs", (u_longlong_t)vd->vdev_ms_count,
816 "offset", "spacemap", "free");
817 (void) printf("\t%15s %19s %15s %10s\n",
818 "---------------", "-------------------",
819 "---------------", "-------------");
823 dump_metaslab_groups(spa_t *spa)
825 vdev_t *rvd = spa->spa_root_vdev;
826 metaslab_class_t *mc = spa_normal_class(spa);
827 uint64_t fragmentation;
829 metaslab_class_histogram_verify(mc);
831 for (int c = 0; c < rvd->vdev_children; c++) {
832 vdev_t *tvd = rvd->vdev_child[c];
833 metaslab_group_t *mg = tvd->vdev_mg;
835 if (mg->mg_class != mc)
838 metaslab_group_histogram_verify(mg);
839 mg->mg_fragmentation = metaslab_group_fragmentation(mg);
841 (void) printf("\tvdev %10llu\t\tmetaslabs%5llu\t\t"
843 (u_longlong_t)tvd->vdev_id,
844 (u_longlong_t)tvd->vdev_ms_count);
845 if (mg->mg_fragmentation == ZFS_FRAG_INVALID) {
846 (void) printf("%3s\n", "-");
848 (void) printf("%3llu%%\n",
849 (u_longlong_t)mg->mg_fragmentation);
851 dump_histogram(mg->mg_histogram, RANGE_TREE_HISTOGRAM_SIZE, 0);
854 (void) printf("\tpool %s\tfragmentation", spa_name(spa));
855 fragmentation = metaslab_class_fragmentation(mc);
856 if (fragmentation == ZFS_FRAG_INVALID)
857 (void) printf("\t%3s\n", "-");
859 (void) printf("\t%3llu%%\n", (u_longlong_t)fragmentation);
860 dump_histogram(mc->mc_histogram, RANGE_TREE_HISTOGRAM_SIZE, 0);
864 dump_metaslabs(spa_t *spa)
866 vdev_t *vd, *rvd = spa->spa_root_vdev;
867 uint64_t m, c = 0, children = rvd->vdev_children;
869 (void) printf("\nMetaslabs:\n");
871 if (!dump_opt['d'] && zopt_objects > 0) {
875 (void) fatal("bad vdev id: %llu", (u_longlong_t)c);
877 if (zopt_objects > 1) {
878 vd = rvd->vdev_child[c];
879 print_vdev_metaslab_header(vd);
881 for (m = 1; m < zopt_objects; m++) {
882 if (zopt_object[m] < vd->vdev_ms_count)
884 vd->vdev_ms[zopt_object[m]]);
886 (void) fprintf(stderr, "bad metaslab "
888 (u_longlong_t)zopt_object[m]);
895 for (; c < children; c++) {
896 vd = rvd->vdev_child[c];
897 print_vdev_metaslab_header(vd);
899 for (m = 0; m < vd->vdev_ms_count; m++)
900 dump_metaslab(vd->vdev_ms[m]);
906 dump_dde(const ddt_t *ddt, const ddt_entry_t *dde, uint64_t index)
908 const ddt_phys_t *ddp = dde->dde_phys;
909 const ddt_key_t *ddk = &dde->dde_key;
910 char *types[4] = { "ditto", "single", "double", "triple" };
911 char blkbuf[BP_SPRINTF_LEN];
914 for (int p = 0; p < DDT_PHYS_TYPES; p++, ddp++) {
915 if (ddp->ddp_phys_birth == 0)
917 ddt_bp_create(ddt->ddt_checksum, ddk, ddp, &blk);
918 snprintf_blkptr(blkbuf, sizeof (blkbuf), &blk);
919 (void) printf("index %llx refcnt %llu %s %s\n",
920 (u_longlong_t)index, (u_longlong_t)ddp->ddp_refcnt,
926 dump_dedup_ratio(const ddt_stat_t *dds)
928 double rL, rP, rD, D, dedup, compress, copies;
930 if (dds->dds_blocks == 0)
933 rL = (double)dds->dds_ref_lsize;
934 rP = (double)dds->dds_ref_psize;
935 rD = (double)dds->dds_ref_dsize;
936 D = (double)dds->dds_dsize;
942 (void) printf("dedup = %.2f, compress = %.2f, copies = %.2f, "
943 "dedup * compress / copies = %.2f\n\n",
944 dedup, compress, copies, dedup * compress / copies);
948 dump_ddt(ddt_t *ddt, enum ddt_type type, enum ddt_class class)
950 char name[DDT_NAMELEN];
953 dmu_object_info_t doi;
954 uint64_t count, dspace, mspace;
957 error = ddt_object_info(ddt, type, class, &doi);
963 error = ddt_object_count(ddt, type, class, &count);
968 dspace = doi.doi_physical_blocks_512 << 9;
969 mspace = doi.doi_fill_count * doi.doi_data_block_size;
971 ddt_object_name(ddt, type, class, name);
973 (void) printf("%s: %llu entries, size %llu on disk, %llu in core\n",
976 (u_longlong_t)(dspace / count),
977 (u_longlong_t)(mspace / count));
979 if (dump_opt['D'] < 3)
982 zpool_dump_ddt(NULL, &ddt->ddt_histogram[type][class]);
984 if (dump_opt['D'] < 4)
987 if (dump_opt['D'] < 5 && class == DDT_CLASS_UNIQUE)
990 (void) printf("%s contents:\n\n", name);
992 while ((error = ddt_object_walk(ddt, type, class, &walk, &dde)) == 0)
993 dump_dde(ddt, &dde, walk);
995 ASSERT(error == ENOENT);
1001 dump_all_ddts(spa_t *spa)
1003 ddt_histogram_t ddh_total = { 0 };
1004 ddt_stat_t dds_total = { 0 };
1006 for (enum zio_checksum c = 0; c < ZIO_CHECKSUM_FUNCTIONS; c++) {
1007 ddt_t *ddt = spa->spa_ddt[c];
1008 for (enum ddt_type type = 0; type < DDT_TYPES; type++) {
1009 for (enum ddt_class class = 0; class < DDT_CLASSES;
1011 dump_ddt(ddt, type, class);
1016 ddt_get_dedup_stats(spa, &dds_total);
1018 if (dds_total.dds_blocks == 0) {
1019 (void) printf("All DDTs are empty\n");
1023 (void) printf("\n");
1025 if (dump_opt['D'] > 1) {
1026 (void) printf("DDT histogram (aggregated over all DDTs):\n");
1027 ddt_get_dedup_histogram(spa, &ddh_total);
1028 zpool_dump_ddt(&dds_total, &ddh_total);
1031 dump_dedup_ratio(&dds_total);
1035 dump_dtl_seg(void *arg, uint64_t start, uint64_t size)
1039 (void) printf("%s [%llu,%llu) length %llu\n",
1041 (u_longlong_t)start,
1042 (u_longlong_t)(start + size),
1043 (u_longlong_t)(size));
1047 dump_dtl(vdev_t *vd, int indent)
1049 spa_t *spa = vd->vdev_spa;
1051 char *name[DTL_TYPES] = { "missing", "partial", "scrub", "outage" };
1054 spa_vdev_state_enter(spa, SCL_NONE);
1055 required = vdev_dtl_required(vd);
1056 (void) spa_vdev_state_exit(spa, NULL, 0);
1059 (void) printf("\nDirty time logs:\n\n");
1061 (void) printf("\t%*s%s [%s]\n", indent, "",
1062 vd->vdev_path ? vd->vdev_path :
1063 vd->vdev_parent ? vd->vdev_ops->vdev_op_type : spa_name(spa),
1064 required ? "DTL-required" : "DTL-expendable");
1066 for (int t = 0; t < DTL_TYPES; t++) {
1067 range_tree_t *rt = vd->vdev_dtl[t];
1068 if (range_tree_space(rt) == 0)
1070 (void) snprintf(prefix, sizeof (prefix), "\t%*s%s",
1071 indent + 2, "", name[t]);
1072 mutex_enter(rt->rt_lock);
1073 range_tree_walk(rt, dump_dtl_seg, prefix);
1074 mutex_exit(rt->rt_lock);
1075 if (dump_opt['d'] > 5 && vd->vdev_children == 0)
1076 dump_spacemap(spa->spa_meta_objset, vd->vdev_dtl_sm);
1079 for (int c = 0; c < vd->vdev_children; c++)
1080 dump_dtl(vd->vdev_child[c], indent + 4);
1083 /* from spa_history.c: spa_history_create_obj() */
1084 #define HIS_BUF_LEN_DEF (128 << 10)
1085 #define HIS_BUF_LEN_MAX (1 << 30)
1088 dump_history(spa_t *spa)
1090 nvlist_t **events = NULL;
1092 uint64_t bufsize = HIS_BUF_LEN_DEF;
1093 uint64_t resid, len, off = 0;
1099 char internalstr[MAXPATHLEN];
1101 if ((buf = malloc(bufsize)) == NULL)
1102 (void) fprintf(stderr, "Unable to read history: "
1107 if ((error = spa_history_get(spa, &off, &len, buf)) != 0) {
1108 (void) fprintf(stderr, "Unable to read history: "
1109 "error %d\n", error);
1113 if (zpool_history_unpack(buf, len, &resid, &events, &num) != 0)
1118 * If the history block is too big, double the buffer
1119 * size and try again.
1126 if ((bufsize >= HIS_BUF_LEN_MAX) ||
1127 ((buf = malloc(bufsize)) == NULL)) {
1128 (void) fprintf(stderr, "Unable to read history: "
1136 (void) printf("\nHistory:\n");
1137 for (int i = 0; i < num; i++) {
1138 uint64_t time, txg, ievent;
1140 boolean_t printed = B_FALSE;
1142 if (nvlist_lookup_uint64(events[i], ZPOOL_HIST_TIME,
1145 if (nvlist_lookup_string(events[i], ZPOOL_HIST_CMD,
1147 if (nvlist_lookup_uint64(events[i],
1148 ZPOOL_HIST_INT_EVENT, &ievent) != 0)
1150 verify(nvlist_lookup_uint64(events[i],
1151 ZPOOL_HIST_TXG, &txg) == 0);
1152 verify(nvlist_lookup_string(events[i],
1153 ZPOOL_HIST_INT_STR, &intstr) == 0);
1154 if (ievent >= ZFS_NUM_LEGACY_HISTORY_EVENTS)
1157 (void) snprintf(internalstr,
1158 sizeof (internalstr),
1159 "[internal %s txg:%lld] %s",
1160 zfs_history_event_names[ievent], txg,
1165 (void) localtime_r(&tsec, &t);
1166 (void) strftime(tbuf, sizeof (tbuf), "%F.%T", &t);
1167 (void) printf("%s %s\n", tbuf, cmd);
1171 if (dump_opt['h'] > 1) {
1173 (void) printf("unrecognized record:\n");
1174 dump_nvlist(events[i], 2);
1181 dump_dnode(objset_t *os, uint64_t object, void *data, size_t size)
1186 blkid2offset(const dnode_phys_t *dnp, const blkptr_t *bp,
1187 const zbookmark_phys_t *zb)
1190 ASSERT(zb->zb_level < 0);
1191 if (zb->zb_object == 0)
1192 return (zb->zb_blkid);
1193 return (zb->zb_blkid * BP_GET_LSIZE(bp));
1196 ASSERT(zb->zb_level >= 0);
1198 return ((zb->zb_blkid <<
1199 (zb->zb_level * (dnp->dn_indblkshift - SPA_BLKPTRSHIFT))) *
1200 dnp->dn_datablkszsec << SPA_MINBLOCKSHIFT);
1204 snprintf_blkptr_compact(char *blkbuf, size_t buflen, const blkptr_t *bp)
1206 const dva_t *dva = bp->blk_dva;
1207 int ndvas = dump_opt['d'] > 5 ? BP_GET_NDVAS(bp) : 1;
1209 if (dump_opt['b'] >= 6) {
1210 snprintf_blkptr(blkbuf, buflen, bp);
1214 if (BP_IS_EMBEDDED(bp)) {
1215 (void) sprintf(blkbuf,
1216 "EMBEDDED et=%u %llxL/%llxP B=%llu",
1217 (int)BPE_GET_ETYPE(bp),
1218 (u_longlong_t)BPE_GET_LSIZE(bp),
1219 (u_longlong_t)BPE_GET_PSIZE(bp),
1220 (u_longlong_t)bp->blk_birth);
1225 for (int i = 0; i < ndvas; i++)
1226 (void) snprintf(blkbuf + strlen(blkbuf),
1227 buflen - strlen(blkbuf), "%llu:%llx:%llx ",
1228 (u_longlong_t)DVA_GET_VDEV(&dva[i]),
1229 (u_longlong_t)DVA_GET_OFFSET(&dva[i]),
1230 (u_longlong_t)DVA_GET_ASIZE(&dva[i]));
1232 if (BP_IS_HOLE(bp)) {
1233 (void) snprintf(blkbuf + strlen(blkbuf),
1234 buflen - strlen(blkbuf),
1236 (u_longlong_t)BP_GET_LSIZE(bp),
1237 (u_longlong_t)bp->blk_birth);
1239 (void) snprintf(blkbuf + strlen(blkbuf),
1240 buflen - strlen(blkbuf),
1241 "%llxL/%llxP F=%llu B=%llu/%llu",
1242 (u_longlong_t)BP_GET_LSIZE(bp),
1243 (u_longlong_t)BP_GET_PSIZE(bp),
1244 (u_longlong_t)BP_GET_FILL(bp),
1245 (u_longlong_t)bp->blk_birth,
1246 (u_longlong_t)BP_PHYSICAL_BIRTH(bp));
1251 print_indirect(blkptr_t *bp, const zbookmark_phys_t *zb,
1252 const dnode_phys_t *dnp)
1254 char blkbuf[BP_SPRINTF_LEN];
1257 if (!BP_IS_EMBEDDED(bp)) {
1258 ASSERT3U(BP_GET_TYPE(bp), ==, dnp->dn_type);
1259 ASSERT3U(BP_GET_LEVEL(bp), ==, zb->zb_level);
1262 (void) printf("%16llx ", (u_longlong_t)blkid2offset(dnp, bp, zb));
1264 ASSERT(zb->zb_level >= 0);
1266 for (l = dnp->dn_nlevels - 1; l >= -1; l--) {
1267 if (l == zb->zb_level) {
1268 (void) printf("L%llx", (u_longlong_t)zb->zb_level);
1274 snprintf_blkptr_compact(blkbuf, sizeof (blkbuf), bp);
1275 (void) printf("%s\n", blkbuf);
1279 visit_indirect(spa_t *spa, const dnode_phys_t *dnp,
1280 blkptr_t *bp, const zbookmark_phys_t *zb)
1284 if (bp->blk_birth == 0)
1287 print_indirect(bp, zb, dnp);
1289 if (BP_GET_LEVEL(bp) > 0 && !BP_IS_HOLE(bp)) {
1290 arc_flags_t flags = ARC_FLAG_WAIT;
1293 int epb = BP_GET_LSIZE(bp) >> SPA_BLKPTRSHIFT;
1297 err = arc_read(NULL, spa, bp, arc_getbuf_func, &buf,
1298 ZIO_PRIORITY_ASYNC_READ, ZIO_FLAG_CANFAIL, &flags, zb);
1301 ASSERT(buf->b_data);
1303 /* recursively visit blocks below this */
1305 for (i = 0; i < epb; i++, cbp++) {
1306 zbookmark_phys_t czb;
1308 SET_BOOKMARK(&czb, zb->zb_objset, zb->zb_object,
1310 zb->zb_blkid * epb + i);
1311 err = visit_indirect(spa, dnp, cbp, &czb);
1314 fill += BP_GET_FILL(cbp);
1317 ASSERT3U(fill, ==, BP_GET_FILL(bp));
1318 arc_buf_destroy(buf, &buf);
1326 dump_indirect(dnode_t *dn)
1328 dnode_phys_t *dnp = dn->dn_phys;
1330 zbookmark_phys_t czb;
1332 (void) printf("Indirect blocks:\n");
1334 SET_BOOKMARK(&czb, dmu_objset_id(dn->dn_objset),
1335 dn->dn_object, dnp->dn_nlevels - 1, 0);
1336 for (j = 0; j < dnp->dn_nblkptr; j++) {
1338 (void) visit_indirect(dmu_objset_spa(dn->dn_objset), dnp,
1339 &dnp->dn_blkptr[j], &czb);
1342 (void) printf("\n");
1347 dump_dsl_dir(objset_t *os, uint64_t object, void *data, size_t size)
1349 dsl_dir_phys_t *dd = data;
1353 /* make sure nicenum has enough space */
1354 CTASSERT(sizeof (nice) >= NN_NUMBUF_SZ);
1359 ASSERT3U(size, >=, sizeof (dsl_dir_phys_t));
1361 crtime = dd->dd_creation_time;
1362 (void) printf("\t\tcreation_time = %s", ctime(&crtime));
1363 (void) printf("\t\thead_dataset_obj = %llu\n",
1364 (u_longlong_t)dd->dd_head_dataset_obj);
1365 (void) printf("\t\tparent_dir_obj = %llu\n",
1366 (u_longlong_t)dd->dd_parent_obj);
1367 (void) printf("\t\torigin_obj = %llu\n",
1368 (u_longlong_t)dd->dd_origin_obj);
1369 (void) printf("\t\tchild_dir_zapobj = %llu\n",
1370 (u_longlong_t)dd->dd_child_dir_zapobj);
1371 zdb_nicenum(dd->dd_used_bytes, nice, sizeof (nice));
1372 (void) printf("\t\tused_bytes = %s\n", nice);
1373 zdb_nicenum(dd->dd_compressed_bytes, nice, sizeof (nice));
1374 (void) printf("\t\tcompressed_bytes = %s\n", nice);
1375 zdb_nicenum(dd->dd_uncompressed_bytes, nice, sizeof (nice));
1376 (void) printf("\t\tuncompressed_bytes = %s\n", nice);
1377 zdb_nicenum(dd->dd_quota, nice, sizeof (nice));
1378 (void) printf("\t\tquota = %s\n", nice);
1379 zdb_nicenum(dd->dd_reserved, nice, sizeof (nice));
1380 (void) printf("\t\treserved = %s\n", nice);
1381 (void) printf("\t\tprops_zapobj = %llu\n",
1382 (u_longlong_t)dd->dd_props_zapobj);
1383 (void) printf("\t\tdeleg_zapobj = %llu\n",
1384 (u_longlong_t)dd->dd_deleg_zapobj);
1385 (void) printf("\t\tflags = %llx\n",
1386 (u_longlong_t)dd->dd_flags);
1389 zdb_nicenum(dd->dd_used_breakdown[DD_USED_ ## which], nice, \
1391 (void) printf("\t\tused_breakdown[" #which "] = %s\n", nice)
1402 dump_dsl_dataset(objset_t *os, uint64_t object, void *data, size_t size)
1404 dsl_dataset_phys_t *ds = data;
1406 char used[32], compressed[32], uncompressed[32], unique[32];
1407 char blkbuf[BP_SPRINTF_LEN];
1409 /* make sure nicenum has enough space */
1410 CTASSERT(sizeof (used) >= NN_NUMBUF_SZ);
1411 CTASSERT(sizeof (compressed) >= NN_NUMBUF_SZ);
1412 CTASSERT(sizeof (uncompressed) >= NN_NUMBUF_SZ);
1413 CTASSERT(sizeof (unique) >= NN_NUMBUF_SZ);
1418 ASSERT(size == sizeof (*ds));
1419 crtime = ds->ds_creation_time;
1420 zdb_nicenum(ds->ds_referenced_bytes, used, sizeof (used));
1421 zdb_nicenum(ds->ds_compressed_bytes, compressed, sizeof (compressed));
1422 zdb_nicenum(ds->ds_uncompressed_bytes, uncompressed,
1423 sizeof (uncompressed));
1424 zdb_nicenum(ds->ds_unique_bytes, unique, sizeof (unique));
1425 snprintf_blkptr(blkbuf, sizeof (blkbuf), &ds->ds_bp);
1427 (void) printf("\t\tdir_obj = %llu\n",
1428 (u_longlong_t)ds->ds_dir_obj);
1429 (void) printf("\t\tprev_snap_obj = %llu\n",
1430 (u_longlong_t)ds->ds_prev_snap_obj);
1431 (void) printf("\t\tprev_snap_txg = %llu\n",
1432 (u_longlong_t)ds->ds_prev_snap_txg);
1433 (void) printf("\t\tnext_snap_obj = %llu\n",
1434 (u_longlong_t)ds->ds_next_snap_obj);
1435 (void) printf("\t\tsnapnames_zapobj = %llu\n",
1436 (u_longlong_t)ds->ds_snapnames_zapobj);
1437 (void) printf("\t\tnum_children = %llu\n",
1438 (u_longlong_t)ds->ds_num_children);
1439 (void) printf("\t\tuserrefs_obj = %llu\n",
1440 (u_longlong_t)ds->ds_userrefs_obj);
1441 (void) printf("\t\tcreation_time = %s", ctime(&crtime));
1442 (void) printf("\t\tcreation_txg = %llu\n",
1443 (u_longlong_t)ds->ds_creation_txg);
1444 (void) printf("\t\tdeadlist_obj = %llu\n",
1445 (u_longlong_t)ds->ds_deadlist_obj);
1446 (void) printf("\t\tused_bytes = %s\n", used);
1447 (void) printf("\t\tcompressed_bytes = %s\n", compressed);
1448 (void) printf("\t\tuncompressed_bytes = %s\n", uncompressed);
1449 (void) printf("\t\tunique = %s\n", unique);
1450 (void) printf("\t\tfsid_guid = %llu\n",
1451 (u_longlong_t)ds->ds_fsid_guid);
1452 (void) printf("\t\tguid = %llu\n",
1453 (u_longlong_t)ds->ds_guid);
1454 (void) printf("\t\tflags = %llx\n",
1455 (u_longlong_t)ds->ds_flags);
1456 (void) printf("\t\tnext_clones_obj = %llu\n",
1457 (u_longlong_t)ds->ds_next_clones_obj);
1458 (void) printf("\t\tprops_obj = %llu\n",
1459 (u_longlong_t)ds->ds_props_obj);
1460 (void) printf("\t\tbp = %s\n", blkbuf);
1465 dump_bptree_cb(void *arg, const blkptr_t *bp, dmu_tx_t *tx)
1467 char blkbuf[BP_SPRINTF_LEN];
1469 if (bp->blk_birth != 0) {
1470 snprintf_blkptr(blkbuf, sizeof (blkbuf), bp);
1471 (void) printf("\t%s\n", blkbuf);
1477 dump_bptree(objset_t *os, uint64_t obj, char *name)
1483 /* make sure nicenum has enough space */
1484 CTASSERT(sizeof (bytes) >= NN_NUMBUF_SZ);
1486 if (dump_opt['d'] < 3)
1489 VERIFY3U(0, ==, dmu_bonus_hold(os, obj, FTAG, &db));
1491 zdb_nicenum(bt->bt_bytes, bytes, sizeof (bytes));
1492 (void) printf("\n %s: %llu datasets, %s\n",
1493 name, (unsigned long long)(bt->bt_end - bt->bt_begin), bytes);
1494 dmu_buf_rele(db, FTAG);
1496 if (dump_opt['d'] < 5)
1499 (void) printf("\n");
1501 (void) bptree_iterate(os, obj, B_FALSE, dump_bptree_cb, NULL, NULL);
1506 dump_bpobj_cb(void *arg, const blkptr_t *bp, dmu_tx_t *tx)
1508 char blkbuf[BP_SPRINTF_LEN];
1510 ASSERT(bp->blk_birth != 0);
1511 snprintf_blkptr_compact(blkbuf, sizeof (blkbuf), bp);
1512 (void) printf("\t%s\n", blkbuf);
1517 dump_full_bpobj(bpobj_t *bpo, char *name, int indent)
1523 /* make sure nicenum has enough space */
1524 CTASSERT(sizeof (bytes) >= NN_NUMBUF_SZ);
1525 CTASSERT(sizeof (comp) >= NN_NUMBUF_SZ);
1526 CTASSERT(sizeof (uncomp) >= NN_NUMBUF_SZ);
1528 if (dump_opt['d'] < 3)
1531 zdb_nicenum(bpo->bpo_phys->bpo_bytes, bytes, sizeof (bytes));
1532 if (bpo->bpo_havesubobj && bpo->bpo_phys->bpo_subobjs != 0) {
1533 zdb_nicenum(bpo->bpo_phys->bpo_comp, comp, sizeof (comp));
1534 zdb_nicenum(bpo->bpo_phys->bpo_uncomp, uncomp, sizeof (uncomp));
1535 (void) printf(" %*s: object %llu, %llu local blkptrs, "
1536 "%llu subobjs in object %llu, %s (%s/%s comp)\n",
1538 (u_longlong_t)bpo->bpo_object,
1539 (u_longlong_t)bpo->bpo_phys->bpo_num_blkptrs,
1540 (u_longlong_t)bpo->bpo_phys->bpo_num_subobjs,
1541 (u_longlong_t)bpo->bpo_phys->bpo_subobjs,
1542 bytes, comp, uncomp);
1544 for (uint64_t i = 0; i < bpo->bpo_phys->bpo_num_subobjs; i++) {
1548 VERIFY0(dmu_read(bpo->bpo_os,
1549 bpo->bpo_phys->bpo_subobjs,
1550 i * sizeof (subobj), sizeof (subobj), &subobj, 0));
1551 error = bpobj_open(&subbpo, bpo->bpo_os, subobj);
1553 (void) printf("ERROR %u while trying to open "
1555 error, (u_longlong_t)subobj);
1558 dump_full_bpobj(&subbpo, "subobj", indent + 1);
1559 bpobj_close(&subbpo);
1562 (void) printf(" %*s: object %llu, %llu blkptrs, %s\n",
1564 (u_longlong_t)bpo->bpo_object,
1565 (u_longlong_t)bpo->bpo_phys->bpo_num_blkptrs,
1569 if (dump_opt['d'] < 5)
1574 (void) bpobj_iterate_nofree(bpo, dump_bpobj_cb, NULL, NULL);
1575 (void) printf("\n");
1580 dump_deadlist(dsl_deadlist_t *dl)
1582 dsl_deadlist_entry_t *dle;
1588 /* make sure nicenum has enough space */
1589 CTASSERT(sizeof (bytes) >= NN_NUMBUF_SZ);
1590 CTASSERT(sizeof (comp) >= NN_NUMBUF_SZ);
1591 CTASSERT(sizeof (uncomp) >= NN_NUMBUF_SZ);
1593 if (dump_opt['d'] < 3)
1596 if (dl->dl_oldfmt) {
1597 dump_full_bpobj(&dl->dl_bpobj, "old-format deadlist", 0);
1601 zdb_nicenum(dl->dl_phys->dl_used, bytes, sizeof (bytes));
1602 zdb_nicenum(dl->dl_phys->dl_comp, comp, sizeof (comp));
1603 zdb_nicenum(dl->dl_phys->dl_uncomp, uncomp, sizeof (uncomp));
1604 (void) printf("\n Deadlist: %s (%s/%s comp)\n",
1605 bytes, comp, uncomp);
1607 if (dump_opt['d'] < 4)
1610 (void) printf("\n");
1612 /* force the tree to be loaded */
1613 dsl_deadlist_space_range(dl, 0, UINT64_MAX, &unused, &unused, &unused);
1615 for (dle = avl_first(&dl->dl_tree); dle;
1616 dle = AVL_NEXT(&dl->dl_tree, dle)) {
1617 if (dump_opt['d'] >= 5) {
1619 (void) snprintf(buf, sizeof (buf), "mintxg %llu -> ",
1620 (longlong_t)dle->dle_mintxg,
1621 (longlong_t)dle->dle_bpobj.bpo_object);
1622 dump_full_bpobj(&dle->dle_bpobj, buf, 0);
1624 (void) printf("mintxg %llu -> obj %llu\n",
1625 (longlong_t)dle->dle_mintxg,
1626 (longlong_t)dle->dle_bpobj.bpo_object);
1632 static avl_tree_t idx_tree;
1633 static avl_tree_t domain_tree;
1634 static boolean_t fuid_table_loaded;
1635 static boolean_t sa_loaded;
1636 sa_attr_type_t *sa_attr_table;
1639 fuid_table_destroy()
1641 if (fuid_table_loaded) {
1642 zfs_fuid_table_destroy(&idx_tree, &domain_tree);
1643 fuid_table_loaded = B_FALSE;
1648 * print uid or gid information.
1649 * For normal POSIX id just the id is printed in decimal format.
1650 * For CIFS files with FUID the fuid is printed in hex followed by
1651 * the domain-rid string.
1654 print_idstr(uint64_t id, const char *id_type)
1656 if (FUID_INDEX(id)) {
1659 domain = zfs_fuid_idx_domain(&idx_tree, FUID_INDEX(id));
1660 (void) printf("\t%s %llx [%s-%d]\n", id_type,
1661 (u_longlong_t)id, domain, (int)FUID_RID(id));
1663 (void) printf("\t%s %llu\n", id_type, (u_longlong_t)id);
1669 dump_uidgid(objset_t *os, uint64_t uid, uint64_t gid)
1671 uint32_t uid_idx, gid_idx;
1673 uid_idx = FUID_INDEX(uid);
1674 gid_idx = FUID_INDEX(gid);
1676 /* Load domain table, if not already loaded */
1677 if (!fuid_table_loaded && (uid_idx || gid_idx)) {
1680 /* first find the fuid object. It lives in the master node */
1681 VERIFY(zap_lookup(os, MASTER_NODE_OBJ, ZFS_FUID_TABLES,
1682 8, 1, &fuid_obj) == 0);
1683 zfs_fuid_avl_tree_create(&idx_tree, &domain_tree);
1684 (void) zfs_fuid_table_load(os, fuid_obj,
1685 &idx_tree, &domain_tree);
1686 fuid_table_loaded = B_TRUE;
1689 print_idstr(uid, "uid");
1690 print_idstr(gid, "gid");
1695 dump_znode(objset_t *os, uint64_t object, void *data, size_t size)
1697 char path[MAXPATHLEN * 2]; /* allow for xattr and failure prefix */
1699 uint64_t xattr, rdev, gen;
1700 uint64_t uid, gid, mode, fsize, parent, links;
1702 uint64_t acctm[2], modtm[2], chgtm[2], crtm[2];
1703 time_t z_crtime, z_atime, z_mtime, z_ctime;
1704 sa_bulk_attr_t bulk[12];
1709 uint64_t sa_attrs = 0;
1712 VERIFY(zap_lookup(os, MASTER_NODE_OBJ, ZPL_VERSION_STR,
1713 8, 1, &version) == 0);
1714 if (version >= ZPL_VERSION_SA) {
1715 VERIFY(zap_lookup(os, MASTER_NODE_OBJ, ZFS_SA_ATTRS,
1716 8, 1, &sa_attrs) == 0);
1718 if ((error = sa_setup(os, sa_attrs, zfs_attr_table,
1719 ZPL_END, &sa_attr_table)) != 0) {
1720 (void) printf("sa_setup failed errno %d, can't "
1721 "display znode contents\n", error);
1727 if (sa_handle_get(os, object, NULL, SA_HDL_PRIVATE, &hdl)) {
1728 (void) printf("Failed to get handle for SA znode\n");
1732 SA_ADD_BULK_ATTR(bulk, idx, sa_attr_table[ZPL_UID], NULL, &uid, 8);
1733 SA_ADD_BULK_ATTR(bulk, idx, sa_attr_table[ZPL_GID], NULL, &gid, 8);
1734 SA_ADD_BULK_ATTR(bulk, idx, sa_attr_table[ZPL_LINKS], NULL,
1736 SA_ADD_BULK_ATTR(bulk, idx, sa_attr_table[ZPL_GEN], NULL, &gen, 8);
1737 SA_ADD_BULK_ATTR(bulk, idx, sa_attr_table[ZPL_MODE], NULL,
1739 SA_ADD_BULK_ATTR(bulk, idx, sa_attr_table[ZPL_PARENT],
1741 SA_ADD_BULK_ATTR(bulk, idx, sa_attr_table[ZPL_SIZE], NULL,
1743 SA_ADD_BULK_ATTR(bulk, idx, sa_attr_table[ZPL_ATIME], NULL,
1745 SA_ADD_BULK_ATTR(bulk, idx, sa_attr_table[ZPL_MTIME], NULL,
1747 SA_ADD_BULK_ATTR(bulk, idx, sa_attr_table[ZPL_CRTIME], NULL,
1749 SA_ADD_BULK_ATTR(bulk, idx, sa_attr_table[ZPL_CTIME], NULL,
1751 SA_ADD_BULK_ATTR(bulk, idx, sa_attr_table[ZPL_FLAGS], NULL,
1754 if (sa_bulk_lookup(hdl, bulk, idx)) {
1755 (void) sa_handle_destroy(hdl);
1759 z_crtime = (time_t)crtm[0];
1760 z_atime = (time_t)acctm[0];
1761 z_mtime = (time_t)modtm[0];
1762 z_ctime = (time_t)chgtm[0];
1764 if (dump_opt['d'] > 4) {
1765 error = zfs_obj_to_path(os, object, path, sizeof (path));
1767 (void) snprintf(path, sizeof (path),
1768 "\?\?\?<object#%llu>", (u_longlong_t)object);
1770 (void) printf("\tpath %s\n", path);
1772 dump_uidgid(os, uid, gid);
1773 (void) printf("\tatime %s", ctime(&z_atime));
1774 (void) printf("\tmtime %s", ctime(&z_mtime));
1775 (void) printf("\tctime %s", ctime(&z_ctime));
1776 (void) printf("\tcrtime %s", ctime(&z_crtime));
1777 (void) printf("\tgen %llu\n", (u_longlong_t)gen);
1778 (void) printf("\tmode %llo\n", (u_longlong_t)mode);
1779 (void) printf("\tsize %llu\n", (u_longlong_t)fsize);
1780 (void) printf("\tparent %llu\n", (u_longlong_t)parent);
1781 (void) printf("\tlinks %llu\n", (u_longlong_t)links);
1782 (void) printf("\tpflags %llx\n", (u_longlong_t)pflags);
1783 if (sa_lookup(hdl, sa_attr_table[ZPL_XATTR], &xattr,
1784 sizeof (uint64_t)) == 0)
1785 (void) printf("\txattr %llu\n", (u_longlong_t)xattr);
1786 if (sa_lookup(hdl, sa_attr_table[ZPL_RDEV], &rdev,
1787 sizeof (uint64_t)) == 0)
1788 (void) printf("\trdev 0x%016llx\n", (u_longlong_t)rdev);
1789 sa_handle_destroy(hdl);
1794 dump_acl(objset_t *os, uint64_t object, void *data, size_t size)
1800 dump_dmu_objset(objset_t *os, uint64_t object, void *data, size_t size)
1804 static object_viewer_t *object_viewer[DMU_OT_NUMTYPES + 1] = {
1805 dump_none, /* unallocated */
1806 dump_zap, /* object directory */
1807 dump_uint64, /* object array */
1808 dump_none, /* packed nvlist */
1809 dump_packed_nvlist, /* packed nvlist size */
1810 dump_none, /* bpobj */
1811 dump_bpobj, /* bpobj header */
1812 dump_none, /* SPA space map header */
1813 dump_none, /* SPA space map */
1814 dump_none, /* ZIL intent log */
1815 dump_dnode, /* DMU dnode */
1816 dump_dmu_objset, /* DMU objset */
1817 dump_dsl_dir, /* DSL directory */
1818 dump_zap, /* DSL directory child map */
1819 dump_zap, /* DSL dataset snap map */
1820 dump_zap, /* DSL props */
1821 dump_dsl_dataset, /* DSL dataset */
1822 dump_znode, /* ZFS znode */
1823 dump_acl, /* ZFS V0 ACL */
1824 dump_uint8, /* ZFS plain file */
1825 dump_zpldir, /* ZFS directory */
1826 dump_zap, /* ZFS master node */
1827 dump_zap, /* ZFS delete queue */
1828 dump_uint8, /* zvol object */
1829 dump_zap, /* zvol prop */
1830 dump_uint8, /* other uint8[] */
1831 dump_uint64, /* other uint64[] */
1832 dump_zap, /* other ZAP */
1833 dump_zap, /* persistent error log */
1834 dump_uint8, /* SPA history */
1835 dump_history_offsets, /* SPA history offsets */
1836 dump_zap, /* Pool properties */
1837 dump_zap, /* DSL permissions */
1838 dump_acl, /* ZFS ACL */
1839 dump_uint8, /* ZFS SYSACL */
1840 dump_none, /* FUID nvlist */
1841 dump_packed_nvlist, /* FUID nvlist size */
1842 dump_zap, /* DSL dataset next clones */
1843 dump_zap, /* DSL scrub queue */
1844 dump_zap, /* ZFS user/group used */
1845 dump_zap, /* ZFS user/group quota */
1846 dump_zap, /* snapshot refcount tags */
1847 dump_ddt_zap, /* DDT ZAP object */
1848 dump_zap, /* DDT statistics */
1849 dump_znode, /* SA object */
1850 dump_zap, /* SA Master Node */
1851 dump_sa_attrs, /* SA attribute registration */
1852 dump_sa_layouts, /* SA attribute layouts */
1853 dump_zap, /* DSL scrub translations */
1854 dump_none, /* fake dedup BP */
1855 dump_zap, /* deadlist */
1856 dump_none, /* deadlist hdr */
1857 dump_zap, /* dsl clones */
1858 dump_bpobj_subobjs, /* bpobj subobjs */
1859 dump_unknown, /* Unknown type, must be last */
1863 dump_object(objset_t *os, uint64_t object, int verbosity, int *print_header)
1865 dmu_buf_t *db = NULL;
1866 dmu_object_info_t doi;
1870 char iblk[32], dblk[32], lsize[32], asize[32], fill[32];
1871 char bonus_size[32];
1875 /* make sure nicenum has enough space */
1876 CTASSERT(sizeof (iblk) >= NN_NUMBUF_SZ);
1877 CTASSERT(sizeof (dblk) >= NN_NUMBUF_SZ);
1878 CTASSERT(sizeof (lsize) >= NN_NUMBUF_SZ);
1879 CTASSERT(sizeof (asize) >= NN_NUMBUF_SZ);
1880 CTASSERT(sizeof (bonus_size) >= NN_NUMBUF_SZ);
1882 if (*print_header) {
1883 (void) printf("\n%10s %3s %5s %5s %5s %5s %6s %s\n",
1884 "Object", "lvl", "iblk", "dblk", "dsize", "lsize",
1890 dn = DMU_META_DNODE(os);
1892 error = dmu_bonus_hold(os, object, FTAG, &db);
1894 fatal("dmu_bonus_hold(%llu) failed, errno %u",
1896 bonus = db->db_data;
1897 bsize = db->db_size;
1898 dn = DB_DNODE((dmu_buf_impl_t *)db);
1900 dmu_object_info_from_dnode(dn, &doi);
1902 zdb_nicenum(doi.doi_metadata_block_size, iblk, sizeof (iblk));
1903 zdb_nicenum(doi.doi_data_block_size, dblk, sizeof (dblk));
1904 zdb_nicenum(doi.doi_max_offset, lsize, sizeof (lsize));
1905 zdb_nicenum(doi.doi_physical_blocks_512 << 9, asize, sizeof (asize));
1906 zdb_nicenum(doi.doi_bonus_size, bonus_size, sizeof (bonus_size));
1907 (void) sprintf(fill, "%6.2f", 100.0 * doi.doi_fill_count *
1908 doi.doi_data_block_size / (object == 0 ? DNODES_PER_BLOCK : 1) /
1909 doi.doi_max_offset);
1913 if (doi.doi_checksum != ZIO_CHECKSUM_INHERIT || verbosity >= 6) {
1914 (void) snprintf(aux + strlen(aux), sizeof (aux), " (K=%s)",
1915 ZDB_CHECKSUM_NAME(doi.doi_checksum));
1918 if (doi.doi_compress != ZIO_COMPRESS_INHERIT || verbosity >= 6) {
1919 (void) snprintf(aux + strlen(aux), sizeof (aux), " (Z=%s)",
1920 ZDB_COMPRESS_NAME(doi.doi_compress));
1923 (void) printf("%10lld %3u %5s %5s %5s %5s %6s %s%s\n",
1924 (u_longlong_t)object, doi.doi_indirection, iblk, dblk,
1925 asize, lsize, fill, ZDB_OT_NAME(doi.doi_type), aux);
1927 if (doi.doi_bonus_type != DMU_OT_NONE && verbosity > 3) {
1928 (void) printf("%10s %3s %5s %5s %5s %5s %6s %s\n",
1929 "", "", "", "", "", bonus_size, "bonus",
1930 ZDB_OT_NAME(doi.doi_bonus_type));
1933 if (verbosity >= 4) {
1934 (void) printf("\tdnode flags: %s%s%s\n",
1935 (dn->dn_phys->dn_flags & DNODE_FLAG_USED_BYTES) ?
1937 (dn->dn_phys->dn_flags & DNODE_FLAG_USERUSED_ACCOUNTED) ?
1938 "USERUSED_ACCOUNTED " : "",
1939 (dn->dn_phys->dn_flags & DNODE_FLAG_SPILL_BLKPTR) ?
1940 "SPILL_BLKPTR" : "");
1941 (void) printf("\tdnode maxblkid: %llu\n",
1942 (longlong_t)dn->dn_phys->dn_maxblkid);
1944 object_viewer[ZDB_OT_TYPE(doi.doi_bonus_type)](os, object,
1946 object_viewer[ZDB_OT_TYPE(doi.doi_type)](os, object, NULL, 0);
1953 if (verbosity >= 5) {
1955 * Report the list of segments that comprise the object.
1959 uint64_t blkfill = 1;
1962 if (dn->dn_type == DMU_OT_DNODE) {
1964 blkfill = DNODES_PER_BLOCK;
1969 /* make sure nicenum has enough space */
1970 CTASSERT(sizeof (segsize) >= NN_NUMBUF_SZ);
1971 error = dnode_next_offset(dn,
1972 0, &start, minlvl, blkfill, 0);
1976 error = dnode_next_offset(dn,
1977 DNODE_FIND_HOLE, &end, minlvl, blkfill, 0);
1978 zdb_nicenum(end - start, segsize, sizeof (segsize));
1979 (void) printf("\t\tsegment [%016llx, %016llx)"
1980 " size %5s\n", (u_longlong_t)start,
1981 (u_longlong_t)end, segsize);
1989 dmu_buf_rele(db, FTAG);
1992 static char *objset_types[DMU_OST_NUMTYPES] = {
1993 "NONE", "META", "ZPL", "ZVOL", "OTHER", "ANY" };
1996 dump_dir(objset_t *os)
1998 dmu_objset_stats_t dds;
1999 uint64_t object, object_count;
2000 uint64_t refdbytes, usedobjs, scratch;
2002 char blkbuf[BP_SPRINTF_LEN + 20];
2003 char osname[ZFS_MAX_DATASET_NAME_LEN];
2004 char *type = "UNKNOWN";
2005 int verbosity = dump_opt['d'];
2006 int print_header = 1;
2009 /* make sure nicenum has enough space */
2010 CTASSERT(sizeof (numbuf) >= NN_NUMBUF_SZ);
2012 dsl_pool_config_enter(dmu_objset_pool(os), FTAG);
2013 dmu_objset_fast_stat(os, &dds);
2014 dsl_pool_config_exit(dmu_objset_pool(os), FTAG);
2016 if (dds.dds_type < DMU_OST_NUMTYPES)
2017 type = objset_types[dds.dds_type];
2019 if (dds.dds_type == DMU_OST_META) {
2020 dds.dds_creation_txg = TXG_INITIAL;
2021 usedobjs = BP_GET_FILL(os->os_rootbp);
2022 refdbytes = dsl_dir_phys(os->os_spa->spa_dsl_pool->dp_mos_dir)->
2025 dmu_objset_space(os, &refdbytes, &scratch, &usedobjs, &scratch);
2028 ASSERT3U(usedobjs, ==, BP_GET_FILL(os->os_rootbp));
2030 zdb_nicenum(refdbytes, numbuf, sizeof (numbuf));
2032 if (verbosity >= 4) {
2033 (void) snprintf(blkbuf, sizeof (blkbuf), ", rootbp ");
2034 (void) snprintf_blkptr(blkbuf + strlen(blkbuf),
2035 sizeof (blkbuf) - strlen(blkbuf), os->os_rootbp);
2040 dmu_objset_name(os, osname);
2042 (void) printf("Dataset %s [%s], ID %llu, cr_txg %llu, "
2043 "%s, %llu objects%s\n",
2044 osname, type, (u_longlong_t)dmu_objset_id(os),
2045 (u_longlong_t)dds.dds_creation_txg,
2046 numbuf, (u_longlong_t)usedobjs, blkbuf);
2048 if (zopt_objects != 0) {
2049 for (i = 0; i < zopt_objects; i++)
2050 dump_object(os, zopt_object[i], verbosity,
2052 (void) printf("\n");
2056 if (dump_opt['i'] != 0 || verbosity >= 2)
2057 dump_intent_log(dmu_objset_zil(os));
2059 if (dmu_objset_ds(os) != NULL)
2060 dump_deadlist(&dmu_objset_ds(os)->ds_deadlist);
2065 if (BP_IS_HOLE(os->os_rootbp))
2068 dump_object(os, 0, verbosity, &print_header);
2070 if (DMU_USERUSED_DNODE(os) != NULL &&
2071 DMU_USERUSED_DNODE(os)->dn_type != 0) {
2072 dump_object(os, DMU_USERUSED_OBJECT, verbosity, &print_header);
2073 dump_object(os, DMU_GROUPUSED_OBJECT, verbosity, &print_header);
2077 while ((error = dmu_object_next(os, &object, B_FALSE, 0)) == 0) {
2078 dump_object(os, object, verbosity, &print_header);
2082 ASSERT3U(object_count, ==, usedobjs);
2084 (void) printf("\n");
2086 if (error != ESRCH) {
2087 (void) fprintf(stderr, "dmu_object_next() = %d\n", error);
2093 dump_uberblock(uberblock_t *ub, const char *header, const char *footer)
2095 time_t timestamp = ub->ub_timestamp;
2097 (void) printf(header ? header : "");
2098 (void) printf("\tmagic = %016llx\n", (u_longlong_t)ub->ub_magic);
2099 (void) printf("\tversion = %llu\n", (u_longlong_t)ub->ub_version);
2100 (void) printf("\ttxg = %llu\n", (u_longlong_t)ub->ub_txg);
2101 (void) printf("\tguid_sum = %llu\n", (u_longlong_t)ub->ub_guid_sum);
2102 (void) printf("\ttimestamp = %llu UTC = %s",
2103 (u_longlong_t)ub->ub_timestamp, asctime(localtime(×tamp)));
2104 if (dump_opt['u'] >= 3) {
2105 char blkbuf[BP_SPRINTF_LEN];
2106 snprintf_blkptr(blkbuf, sizeof (blkbuf), &ub->ub_rootbp);
2107 (void) printf("\trootbp = %s\n", blkbuf);
2109 (void) printf(footer ? footer : "");
2113 dump_config(spa_t *spa)
2120 error = dmu_bonus_hold(spa->spa_meta_objset,
2121 spa->spa_config_object, FTAG, &db);
2124 nvsize = *(uint64_t *)db->db_data;
2125 dmu_buf_rele(db, FTAG);
2127 (void) printf("\nMOS Configuration:\n");
2128 dump_packed_nvlist(spa->spa_meta_objset,
2129 spa->spa_config_object, (void *)&nvsize, 1);
2131 (void) fprintf(stderr, "dmu_bonus_hold(%llu) failed, errno %d",
2132 (u_longlong_t)spa->spa_config_object, error);
2137 dump_cachefile(const char *cachefile)
2140 struct stat64 statbuf;
2144 if ((fd = open64(cachefile, O_RDONLY)) < 0) {
2145 (void) printf("cannot open '%s': %s\n", cachefile,
2150 if (fstat64(fd, &statbuf) != 0) {
2151 (void) printf("failed to stat '%s': %s\n", cachefile,
2156 if ((buf = malloc(statbuf.st_size)) == NULL) {
2157 (void) fprintf(stderr, "failed to allocate %llu bytes\n",
2158 (u_longlong_t)statbuf.st_size);
2162 if (read(fd, buf, statbuf.st_size) != statbuf.st_size) {
2163 (void) fprintf(stderr, "failed to read %llu bytes\n",
2164 (u_longlong_t)statbuf.st_size);
2170 if (nvlist_unpack(buf, statbuf.st_size, &config, 0) != 0) {
2171 (void) fprintf(stderr, "failed to unpack nvlist\n");
2177 dump_nvlist(config, 0);
2179 nvlist_free(config);
2182 #define ZDB_MAX_UB_HEADER_SIZE 32
2185 dump_label_uberblocks(vdev_label_t *lbl, uint64_t ashift)
2189 char header[ZDB_MAX_UB_HEADER_SIZE];
2191 vd.vdev_ashift = ashift;
2192 vdp->vdev_top = vdp;
2194 for (int i = 0; i < VDEV_UBERBLOCK_COUNT(vdp); i++) {
2195 uint64_t uoff = VDEV_UBERBLOCK_OFFSET(vdp, i);
2196 uberblock_t *ub = (void *)((char *)lbl + uoff);
2198 if (uberblock_verify(ub))
2200 (void) snprintf(header, ZDB_MAX_UB_HEADER_SIZE,
2201 "Uberblock[%d]\n", i);
2202 dump_uberblock(ub, header, "");
2207 dump_label(const char *dev)
2211 char *path, *buf = label.vl_vdev_phys.vp_nvlist;
2212 size_t buflen = sizeof (label.vl_vdev_phys.vp_nvlist);
2213 struct stat64 statbuf;
2214 uint64_t psize, ashift;
2215 int len = strlen(dev) + 1;
2217 if (strncmp(dev, ZFS_DISK_ROOTD, strlen(ZFS_DISK_ROOTD)) == 0) {
2220 (void) snprintf(path, len, "%s%s", ZFS_RDISK_ROOTD,
2221 dev + strlen(ZFS_DISK_ROOTD));
2226 if ((fd = open64(path, O_RDONLY)) < 0) {
2227 (void) printf("cannot open '%s': %s\n", path, strerror(errno));
2232 if (fstat64(fd, &statbuf) != 0) {
2233 (void) printf("failed to stat '%s': %s\n", path,
2240 if (S_ISBLK(statbuf.st_mode)) {
2241 (void) printf("cannot use '%s': character device required\n",
2248 psize = statbuf.st_size;
2249 psize = P2ALIGN(psize, (uint64_t)sizeof (vdev_label_t));
2251 for (int l = 0; l < VDEV_LABELS; l++) {
2252 nvlist_t *config = NULL;
2254 (void) printf("--------------------------------------------\n");
2255 (void) printf("LABEL %d\n", l);
2256 (void) printf("--------------------------------------------\n");
2258 if (pread64(fd, &label, sizeof (label),
2259 vdev_label_offset(psize, l, 0)) != sizeof (label)) {
2260 (void) printf("failed to read label %d\n", l);
2264 if (nvlist_unpack(buf, buflen, &config, 0) != 0) {
2265 (void) printf("failed to unpack label %d\n", l);
2266 ashift = SPA_MINBLOCKSHIFT;
2268 nvlist_t *vdev_tree = NULL;
2270 dump_nvlist(config, 4);
2271 if ((nvlist_lookup_nvlist(config,
2272 ZPOOL_CONFIG_VDEV_TREE, &vdev_tree) != 0) ||
2273 (nvlist_lookup_uint64(vdev_tree,
2274 ZPOOL_CONFIG_ASHIFT, &ashift) != 0))
2275 ashift = SPA_MINBLOCKSHIFT;
2276 nvlist_free(config);
2279 dump_label_uberblocks(&label, ashift);
2286 static uint64_t dataset_feature_count[SPA_FEATURES];
2290 dump_one_dir(const char *dsname, void *arg)
2295 error = dmu_objset_own(dsname, DMU_OST_ANY, B_TRUE, FTAG, &os);
2297 (void) printf("Could not open %s, error %d\n", dsname, error);
2301 for (spa_feature_t f = 0; f < SPA_FEATURES; f++) {
2302 if (!dmu_objset_ds(os)->ds_feature_inuse[f])
2304 ASSERT(spa_feature_table[f].fi_flags &
2305 ZFEATURE_FLAG_PER_DATASET);
2306 dataset_feature_count[f]++;
2310 dmu_objset_disown(os, FTAG);
2311 fuid_table_destroy();
2312 sa_loaded = B_FALSE;
2319 #define PSIZE_HISTO_SIZE (SPA_OLD_MAXBLOCKSIZE / SPA_MINBLOCKSIZE + 2)
2320 typedef struct zdb_blkstats {
2326 uint64_t zb_ditto_samevdev;
2327 uint64_t zb_psize_histogram[PSIZE_HISTO_SIZE];
2331 * Extended object types to report deferred frees and dedup auto-ditto blocks.
2333 #define ZDB_OT_DEFERRED (DMU_OT_NUMTYPES + 0)
2334 #define ZDB_OT_DITTO (DMU_OT_NUMTYPES + 1)
2335 #define ZDB_OT_OTHER (DMU_OT_NUMTYPES + 2)
2336 #define ZDB_OT_TOTAL (DMU_OT_NUMTYPES + 3)
2338 static char *zdb_ot_extname[] = {
2345 #define ZB_TOTAL DN_MAX_LEVELS
2347 typedef struct zdb_cb {
2348 zdb_blkstats_t zcb_type[ZB_TOTAL + 1][ZDB_OT_TOTAL + 1];
2349 uint64_t zcb_dedup_asize;
2350 uint64_t zcb_dedup_blocks;
2351 uint64_t zcb_embedded_blocks[NUM_BP_EMBEDDED_TYPES];
2352 uint64_t zcb_embedded_histogram[NUM_BP_EMBEDDED_TYPES]
2355 uint64_t zcb_lastprint;
2356 uint64_t zcb_totalasize;
2357 uint64_t zcb_errors[256];
2364 zdb_count_block(zdb_cb_t *zcb, zilog_t *zilog, const blkptr_t *bp,
2365 dmu_object_type_t type)
2367 uint64_t refcnt = 0;
2369 ASSERT(type < ZDB_OT_TOTAL);
2371 if (zilog && zil_bp_tree_add(zilog, bp) != 0)
2374 for (int i = 0; i < 4; i++) {
2375 int l = (i < 2) ? BP_GET_LEVEL(bp) : ZB_TOTAL;
2376 int t = (i & 1) ? type : ZDB_OT_TOTAL;
2378 zdb_blkstats_t *zb = &zcb->zcb_type[l][t];
2380 zb->zb_asize += BP_GET_ASIZE(bp);
2381 zb->zb_lsize += BP_GET_LSIZE(bp);
2382 zb->zb_psize += BP_GET_PSIZE(bp);
2386 * The histogram is only big enough to record blocks up to
2387 * SPA_OLD_MAXBLOCKSIZE; larger blocks go into the last,
2390 int idx = BP_GET_PSIZE(bp) >> SPA_MINBLOCKSHIFT;
2391 idx = MIN(idx, SPA_OLD_MAXBLOCKSIZE / SPA_MINBLOCKSIZE + 1);
2392 zb->zb_psize_histogram[idx]++;
2394 zb->zb_gangs += BP_COUNT_GANG(bp);
2396 switch (BP_GET_NDVAS(bp)) {
2398 if (DVA_GET_VDEV(&bp->blk_dva[0]) ==
2399 DVA_GET_VDEV(&bp->blk_dva[1]))
2400 zb->zb_ditto_samevdev++;
2403 equal = (DVA_GET_VDEV(&bp->blk_dva[0]) ==
2404 DVA_GET_VDEV(&bp->blk_dva[1])) +
2405 (DVA_GET_VDEV(&bp->blk_dva[0]) ==
2406 DVA_GET_VDEV(&bp->blk_dva[2])) +
2407 (DVA_GET_VDEV(&bp->blk_dva[1]) ==
2408 DVA_GET_VDEV(&bp->blk_dva[2]));
2410 zb->zb_ditto_samevdev++;
2416 if (BP_IS_EMBEDDED(bp)) {
2417 zcb->zcb_embedded_blocks[BPE_GET_ETYPE(bp)]++;
2418 zcb->zcb_embedded_histogram[BPE_GET_ETYPE(bp)]
2419 [BPE_GET_PSIZE(bp)]++;
2426 if (BP_GET_DEDUP(bp)) {
2430 ddt = ddt_select(zcb->zcb_spa, bp);
2432 dde = ddt_lookup(ddt, bp, B_FALSE);
2437 ddt_phys_t *ddp = ddt_phys_select(dde, bp);
2438 ddt_phys_decref(ddp);
2439 refcnt = ddp->ddp_refcnt;
2440 if (ddt_phys_total_refcnt(dde) == 0)
2441 ddt_remove(ddt, dde);
2446 VERIFY3U(zio_wait(zio_claim(NULL, zcb->zcb_spa,
2447 refcnt ? 0 : spa_first_txg(zcb->zcb_spa),
2448 bp, NULL, NULL, ZIO_FLAG_CANFAIL)), ==, 0);
2453 zdb_blkptr_done(zio_t *zio)
2455 spa_t *spa = zio->io_spa;
2456 blkptr_t *bp = zio->io_bp;
2457 int ioerr = zio->io_error;
2458 zdb_cb_t *zcb = zio->io_private;
2459 zbookmark_phys_t *zb = &zio->io_bookmark;
2461 zio_data_buf_free(zio->io_data, zio->io_size);
2463 mutex_enter(&spa->spa_scrub_lock);
2464 spa->spa_scrub_inflight--;
2465 cv_broadcast(&spa->spa_scrub_io_cv);
2467 if (ioerr && !(zio->io_flags & ZIO_FLAG_SPECULATIVE)) {
2468 char blkbuf[BP_SPRINTF_LEN];
2470 zcb->zcb_haderrors = 1;
2471 zcb->zcb_errors[ioerr]++;
2473 if (dump_opt['b'] >= 2)
2474 snprintf_blkptr(blkbuf, sizeof (blkbuf), bp);
2478 (void) printf("zdb_blkptr_cb: "
2479 "Got error %d reading "
2480 "<%llu, %llu, %lld, %llx> %s -- skipping\n",
2482 (u_longlong_t)zb->zb_objset,
2483 (u_longlong_t)zb->zb_object,
2484 (u_longlong_t)zb->zb_level,
2485 (u_longlong_t)zb->zb_blkid,
2488 mutex_exit(&spa->spa_scrub_lock);
2493 zdb_blkptr_cb(spa_t *spa, zilog_t *zilog, const blkptr_t *bp,
2494 const zbookmark_phys_t *zb, const dnode_phys_t *dnp, void *arg)
2496 zdb_cb_t *zcb = arg;
2497 dmu_object_type_t type;
2498 boolean_t is_metadata;
2503 if (dump_opt['b'] >= 5 && bp->blk_birth > 0) {
2504 char blkbuf[BP_SPRINTF_LEN];
2505 snprintf_blkptr(blkbuf, sizeof (blkbuf), bp);
2506 (void) printf("objset %llu object %llu "
2507 "level %lld offset 0x%llx %s\n",
2508 (u_longlong_t)zb->zb_objset,
2509 (u_longlong_t)zb->zb_object,
2510 (longlong_t)zb->zb_level,
2511 (u_longlong_t)blkid2offset(dnp, bp, zb),
2518 type = BP_GET_TYPE(bp);
2520 zdb_count_block(zcb, zilog, bp,
2521 (type & DMU_OT_NEWTYPE) ? ZDB_OT_OTHER : type);
2523 is_metadata = (BP_GET_LEVEL(bp) != 0 || DMU_OT_IS_METADATA(type));
2525 if (!BP_IS_EMBEDDED(bp) &&
2526 (dump_opt['c'] > 1 || (dump_opt['c'] && is_metadata))) {
2527 size_t size = BP_GET_PSIZE(bp);
2528 void *data = zio_data_buf_alloc(size);
2529 int flags = ZIO_FLAG_CANFAIL | ZIO_FLAG_SCRUB | ZIO_FLAG_RAW;
2531 /* If it's an intent log block, failure is expected. */
2532 if (zb->zb_level == ZB_ZIL_LEVEL)
2533 flags |= ZIO_FLAG_SPECULATIVE;
2535 mutex_enter(&spa->spa_scrub_lock);
2536 while (spa->spa_scrub_inflight > max_inflight)
2537 cv_wait(&spa->spa_scrub_io_cv, &spa->spa_scrub_lock);
2538 spa->spa_scrub_inflight++;
2539 mutex_exit(&spa->spa_scrub_lock);
2541 zio_nowait(zio_read(NULL, spa, bp, data, size,
2542 zdb_blkptr_done, zcb, ZIO_PRIORITY_ASYNC_READ, flags, zb));
2545 zcb->zcb_readfails = 0;
2547 /* only call gethrtime() every 100 blocks */
2554 if (dump_opt['b'] < 5 && gethrtime() > zcb->zcb_lastprint + NANOSEC) {
2555 uint64_t now = gethrtime();
2557 uint64_t bytes = zcb->zcb_type[ZB_TOTAL][ZDB_OT_TOTAL].zb_asize;
2559 1 + bytes / (1 + ((now - zcb->zcb_start) / 1000 / 1000));
2561 (zcb->zcb_totalasize - bytes) / 1024 / kb_per_sec;
2563 /* make sure nicenum has enough space */
2564 CTASSERT(sizeof (buf) >= NN_NUMBUF_SZ);
2566 zfs_nicenum(bytes, buf, sizeof (buf));
2567 (void) fprintf(stderr,
2568 "\r%5s completed (%4dMB/s) "
2569 "estimated time remaining: %uhr %02umin %02usec ",
2570 buf, kb_per_sec / 1024,
2571 sec_remaining / 60 / 60,
2572 sec_remaining / 60 % 60,
2573 sec_remaining % 60);
2575 zcb->zcb_lastprint = now;
2582 zdb_leak(void *arg, uint64_t start, uint64_t size)
2586 (void) printf("leaked space: vdev %llu, offset 0x%llx, size %llu\n",
2587 (u_longlong_t)vd->vdev_id, (u_longlong_t)start, (u_longlong_t)size);
2590 static metaslab_ops_t zdb_metaslab_ops = {
2595 zdb_ddt_leak_init(spa_t *spa, zdb_cb_t *zcb)
2597 ddt_bookmark_t ddb = { 0 };
2601 while ((error = ddt_walk(spa, &ddb, &dde)) == 0) {
2603 ddt_phys_t *ddp = dde.dde_phys;
2605 if (ddb.ddb_class == DDT_CLASS_UNIQUE)
2608 ASSERT(ddt_phys_total_refcnt(&dde) > 1);
2610 for (int p = 0; p < DDT_PHYS_TYPES; p++, ddp++) {
2611 if (ddp->ddp_phys_birth == 0)
2613 ddt_bp_create(ddb.ddb_checksum,
2614 &dde.dde_key, ddp, &blk);
2615 if (p == DDT_PHYS_DITTO) {
2616 zdb_count_block(zcb, NULL, &blk, ZDB_OT_DITTO);
2618 zcb->zcb_dedup_asize +=
2619 BP_GET_ASIZE(&blk) * (ddp->ddp_refcnt - 1);
2620 zcb->zcb_dedup_blocks++;
2623 if (!dump_opt['L']) {
2624 ddt_t *ddt = spa->spa_ddt[ddb.ddb_checksum];
2626 VERIFY(ddt_lookup(ddt, &blk, B_TRUE) != NULL);
2631 ASSERT(error == ENOENT);
2635 zdb_leak_init(spa_t *spa, zdb_cb_t *zcb)
2639 if (!dump_opt['L']) {
2640 vdev_t *rvd = spa->spa_root_vdev;
2641 for (uint64_t c = 0; c < rvd->vdev_children; c++) {
2642 vdev_t *vd = rvd->vdev_child[c];
2643 for (uint64_t m = 0; m < vd->vdev_ms_count; m++) {
2644 metaslab_t *msp = vd->vdev_ms[m];
2645 mutex_enter(&msp->ms_lock);
2646 metaslab_unload(msp);
2649 * For leak detection, we overload the metaslab
2650 * ms_tree to contain allocated segments
2651 * instead of free segments. As a result,
2652 * we can't use the normal metaslab_load/unload
2655 if (msp->ms_sm != NULL) {
2656 (void) fprintf(stderr,
2657 "\rloading space map for "
2658 "vdev %llu of %llu, "
2659 "metaslab %llu of %llu ...",
2661 (longlong_t)rvd->vdev_children,
2663 (longlong_t)vd->vdev_ms_count);
2665 msp->ms_ops = &zdb_metaslab_ops;
2668 * We don't want to spend the CPU
2669 * manipulating the size-ordered
2670 * tree, so clear the range_tree
2673 msp->ms_tree->rt_ops = NULL;
2674 VERIFY0(space_map_load(msp->ms_sm,
2675 msp->ms_tree, SM_ALLOC));
2676 msp->ms_loaded = B_TRUE;
2678 mutex_exit(&msp->ms_lock);
2681 (void) fprintf(stderr, "\n");
2684 spa_config_enter(spa, SCL_CONFIG, FTAG, RW_READER);
2686 zdb_ddt_leak_init(spa, zcb);
2688 spa_config_exit(spa, SCL_CONFIG, FTAG);
2692 zdb_leak_fini(spa_t *spa)
2694 if (!dump_opt['L']) {
2695 vdev_t *rvd = spa->spa_root_vdev;
2696 for (int c = 0; c < rvd->vdev_children; c++) {
2697 vdev_t *vd = rvd->vdev_child[c];
2698 for (int m = 0; m < vd->vdev_ms_count; m++) {
2699 metaslab_t *msp = vd->vdev_ms[m];
2700 mutex_enter(&msp->ms_lock);
2703 * The ms_tree has been overloaded to
2704 * contain allocated segments. Now that we
2705 * finished traversing all blocks, any
2706 * block that remains in the ms_tree
2707 * represents an allocated block that we
2708 * did not claim during the traversal.
2709 * Claimed blocks would have been removed
2712 range_tree_vacate(msp->ms_tree, zdb_leak, vd);
2713 msp->ms_loaded = B_FALSE;
2715 mutex_exit(&msp->ms_lock);
2723 count_block_cb(void *arg, const blkptr_t *bp, dmu_tx_t *tx)
2725 zdb_cb_t *zcb = arg;
2727 if (dump_opt['b'] >= 5) {
2728 char blkbuf[BP_SPRINTF_LEN];
2729 snprintf_blkptr(blkbuf, sizeof (blkbuf), bp);
2730 (void) printf("[%s] %s\n",
2731 "deferred free", blkbuf);
2733 zdb_count_block(zcb, NULL, bp, ZDB_OT_DEFERRED);
2738 dump_block_stats(spa_t *spa)
2740 zdb_cb_t zcb = { 0 };
2741 zdb_blkstats_t *zb, *tzb;
2742 uint64_t norm_alloc, norm_space, total_alloc, total_found;
2743 int flags = TRAVERSE_PRE | TRAVERSE_PREFETCH_METADATA | TRAVERSE_HARD;
2744 boolean_t leaks = B_FALSE;
2746 (void) printf("\nTraversing all blocks %s%s%s%s%s...\n\n",
2747 (dump_opt['c'] || !dump_opt['L']) ? "to verify " : "",
2748 (dump_opt['c'] == 1) ? "metadata " : "",
2749 dump_opt['c'] ? "checksums " : "",
2750 (dump_opt['c'] && !dump_opt['L']) ? "and verify " : "",
2751 !dump_opt['L'] ? "nothing leaked " : "");
2754 * Load all space maps as SM_ALLOC maps, then traverse the pool
2755 * claiming each block we discover. If the pool is perfectly
2756 * consistent, the space maps will be empty when we're done.
2757 * Anything left over is a leak; any block we can't claim (because
2758 * it's not part of any space map) is a double allocation,
2759 * reference to a freed block, or an unclaimed log block.
2761 zdb_leak_init(spa, &zcb);
2764 * If there's a deferred-free bplist, process that first.
2766 (void) bpobj_iterate_nofree(&spa->spa_deferred_bpobj,
2767 count_block_cb, &zcb, NULL);
2768 if (spa_version(spa) >= SPA_VERSION_DEADLISTS) {
2769 (void) bpobj_iterate_nofree(&spa->spa_dsl_pool->dp_free_bpobj,
2770 count_block_cb, &zcb, NULL);
2772 if (spa_feature_is_active(spa, SPA_FEATURE_ASYNC_DESTROY)) {
2773 VERIFY3U(0, ==, bptree_iterate(spa->spa_meta_objset,
2774 spa->spa_dsl_pool->dp_bptree_obj, B_FALSE, count_block_cb,
2778 if (dump_opt['c'] > 1)
2779 flags |= TRAVERSE_PREFETCH_DATA;
2781 zcb.zcb_totalasize = metaslab_class_get_alloc(spa_normal_class(spa));
2782 zcb.zcb_start = zcb.zcb_lastprint = gethrtime();
2783 zcb.zcb_haderrors |= traverse_pool(spa, 0, flags, zdb_blkptr_cb, &zcb);
2786 * If we've traversed the data blocks then we need to wait for those
2787 * I/Os to complete. We leverage "The Godfather" zio to wait on
2788 * all async I/Os to complete.
2790 if (dump_opt['c']) {
2791 for (int i = 0; i < max_ncpus; i++) {
2792 (void) zio_wait(spa->spa_async_zio_root[i]);
2793 spa->spa_async_zio_root[i] = zio_root(spa, NULL, NULL,
2794 ZIO_FLAG_CANFAIL | ZIO_FLAG_SPECULATIVE |
2795 ZIO_FLAG_GODFATHER);
2799 if (zcb.zcb_haderrors) {
2800 (void) printf("\nError counts:\n\n");
2801 (void) printf("\t%5s %s\n", "errno", "count");
2802 for (int e = 0; e < 256; e++) {
2803 if (zcb.zcb_errors[e] != 0) {
2804 (void) printf("\t%5d %llu\n",
2805 e, (u_longlong_t)zcb.zcb_errors[e]);
2811 * Report any leaked segments.
2815 tzb = &zcb.zcb_type[ZB_TOTAL][ZDB_OT_TOTAL];
2817 norm_alloc = metaslab_class_get_alloc(spa_normal_class(spa));
2818 norm_space = metaslab_class_get_space(spa_normal_class(spa));
2820 total_alloc = norm_alloc + metaslab_class_get_alloc(spa_log_class(spa));
2821 total_found = tzb->zb_asize - zcb.zcb_dedup_asize;
2823 if (total_found == total_alloc) {
2825 (void) printf("\n\tNo leaks (block sum matches space"
2826 " maps exactly)\n");
2828 (void) printf("block traversal size %llu != alloc %llu "
2830 (u_longlong_t)total_found,
2831 (u_longlong_t)total_alloc,
2832 (dump_opt['L']) ? "unreachable" : "leaked",
2833 (longlong_t)(total_alloc - total_found));
2837 if (tzb->zb_count == 0)
2840 (void) printf("\n");
2841 (void) printf("\tbp count: %10llu\n",
2842 (u_longlong_t)tzb->zb_count);
2843 (void) printf("\tganged count: %10llu\n",
2844 (longlong_t)tzb->zb_gangs);
2845 (void) printf("\tbp logical: %10llu avg: %6llu\n",
2846 (u_longlong_t)tzb->zb_lsize,
2847 (u_longlong_t)(tzb->zb_lsize / tzb->zb_count));
2848 (void) printf("\tbp physical: %10llu avg:"
2849 " %6llu compression: %6.2f\n",
2850 (u_longlong_t)tzb->zb_psize,
2851 (u_longlong_t)(tzb->zb_psize / tzb->zb_count),
2852 (double)tzb->zb_lsize / tzb->zb_psize);
2853 (void) printf("\tbp allocated: %10llu avg:"
2854 " %6llu compression: %6.2f\n",
2855 (u_longlong_t)tzb->zb_asize,
2856 (u_longlong_t)(tzb->zb_asize / tzb->zb_count),
2857 (double)tzb->zb_lsize / tzb->zb_asize);
2858 (void) printf("\tbp deduped: %10llu ref>1:"
2859 " %6llu deduplication: %6.2f\n",
2860 (u_longlong_t)zcb.zcb_dedup_asize,
2861 (u_longlong_t)zcb.zcb_dedup_blocks,
2862 (double)zcb.zcb_dedup_asize / tzb->zb_asize + 1.0);
2863 (void) printf("\tSPA allocated: %10llu used: %5.2f%%\n",
2864 (u_longlong_t)norm_alloc, 100.0 * norm_alloc / norm_space);
2866 for (bp_embedded_type_t i = 0; i < NUM_BP_EMBEDDED_TYPES; i++) {
2867 if (zcb.zcb_embedded_blocks[i] == 0)
2869 (void) printf("\n");
2870 (void) printf("\tadditional, non-pointer bps of type %u: "
2872 i, (u_longlong_t)zcb.zcb_embedded_blocks[i]);
2874 if (dump_opt['b'] >= 3) {
2875 (void) printf("\t number of (compressed) bytes: "
2877 dump_histogram(zcb.zcb_embedded_histogram[i],
2878 sizeof (zcb.zcb_embedded_histogram[i]) /
2879 sizeof (zcb.zcb_embedded_histogram[i][0]), 0);
2883 if (tzb->zb_ditto_samevdev != 0) {
2884 (void) printf("\tDittoed blocks on same vdev: %llu\n",
2885 (longlong_t)tzb->zb_ditto_samevdev);
2888 if (dump_opt['b'] >= 2) {
2890 (void) printf("\nBlocks\tLSIZE\tPSIZE\tASIZE"
2891 "\t avg\t comp\t%%Total\tType\n");
2893 for (t = 0; t <= ZDB_OT_TOTAL; t++) {
2894 char csize[32], lsize[32], psize[32], asize[32];
2895 char avg[32], gang[32];
2898 /* make sure nicenum has enough space */
2899 CTASSERT(sizeof (csize) >= NN_NUMBUF_SZ);
2900 CTASSERT(sizeof (lsize) >= NN_NUMBUF_SZ);
2901 CTASSERT(sizeof (psize) >= NN_NUMBUF_SZ);
2902 CTASSERT(sizeof (asize) >= NN_NUMBUF_SZ);
2903 CTASSERT(sizeof (avg) >= NN_NUMBUF_SZ);
2904 CTASSERT(sizeof (gang) >= NN_NUMBUF_SZ);
2906 if (t < DMU_OT_NUMTYPES)
2907 typename = dmu_ot[t].ot_name;
2909 typename = zdb_ot_extname[t - DMU_OT_NUMTYPES];
2911 if (zcb.zcb_type[ZB_TOTAL][t].zb_asize == 0) {
2912 (void) printf("%6s\t%5s\t%5s\t%5s"
2913 "\t%5s\t%5s\t%6s\t%s\n",
2925 for (l = ZB_TOTAL - 1; l >= -1; l--) {
2926 level = (l == -1 ? ZB_TOTAL : l);
2927 zb = &zcb.zcb_type[level][t];
2929 if (zb->zb_asize == 0)
2932 if (dump_opt['b'] < 3 && level != ZB_TOTAL)
2935 if (level == 0 && zb->zb_asize ==
2936 zcb.zcb_type[ZB_TOTAL][t].zb_asize)
2939 zdb_nicenum(zb->zb_count, csize,
2941 zdb_nicenum(zb->zb_lsize, lsize,
2943 zdb_nicenum(zb->zb_psize, psize,
2945 zdb_nicenum(zb->zb_asize, asize,
2947 zdb_nicenum(zb->zb_asize / zb->zb_count, avg,
2949 zdb_nicenum(zb->zb_gangs, gang, sizeof (gang));
2951 (void) printf("%6s\t%5s\t%5s\t%5s\t%5s"
2953 csize, lsize, psize, asize, avg,
2954 (double)zb->zb_lsize / zb->zb_psize,
2955 100.0 * zb->zb_asize / tzb->zb_asize);
2957 if (level == ZB_TOTAL)
2958 (void) printf("%s\n", typename);
2960 (void) printf(" L%d %s\n",
2963 if (dump_opt['b'] >= 3 && zb->zb_gangs > 0) {
2964 (void) printf("\t number of ganged "
2965 "blocks: %s\n", gang);
2968 if (dump_opt['b'] >= 4) {
2969 (void) printf("psize "
2970 "(in 512-byte sectors): "
2971 "number of blocks\n");
2972 dump_histogram(zb->zb_psize_histogram,
2973 PSIZE_HISTO_SIZE, 0);
2979 (void) printf("\n");
2984 if (zcb.zcb_haderrors)
2990 typedef struct zdb_ddt_entry {
2992 uint64_t zdde_ref_blocks;
2993 uint64_t zdde_ref_lsize;
2994 uint64_t zdde_ref_psize;
2995 uint64_t zdde_ref_dsize;
2996 avl_node_t zdde_node;
3001 zdb_ddt_add_cb(spa_t *spa, zilog_t *zilog, const blkptr_t *bp,
3002 const zbookmark_phys_t *zb, const dnode_phys_t *dnp, void *arg)
3004 avl_tree_t *t = arg;
3006 zdb_ddt_entry_t *zdde, zdde_search;
3008 if (bp == NULL || BP_IS_HOLE(bp) || BP_IS_EMBEDDED(bp))
3011 if (dump_opt['S'] > 1 && zb->zb_level == ZB_ROOT_LEVEL) {
3012 (void) printf("traversing objset %llu, %llu objects, "
3013 "%lu blocks so far\n",
3014 (u_longlong_t)zb->zb_objset,
3015 (u_longlong_t)BP_GET_FILL(bp),
3019 if (BP_IS_HOLE(bp) || BP_GET_CHECKSUM(bp) == ZIO_CHECKSUM_OFF ||
3020 BP_GET_LEVEL(bp) > 0 || DMU_OT_IS_METADATA(BP_GET_TYPE(bp)))
3023 ddt_key_fill(&zdde_search.zdde_key, bp);
3025 zdde = avl_find(t, &zdde_search, &where);
3028 zdde = umem_zalloc(sizeof (*zdde), UMEM_NOFAIL);
3029 zdde->zdde_key = zdde_search.zdde_key;
3030 avl_insert(t, zdde, where);
3033 zdde->zdde_ref_blocks += 1;
3034 zdde->zdde_ref_lsize += BP_GET_LSIZE(bp);
3035 zdde->zdde_ref_psize += BP_GET_PSIZE(bp);
3036 zdde->zdde_ref_dsize += bp_get_dsize_sync(spa, bp);
3042 dump_simulated_ddt(spa_t *spa)
3045 void *cookie = NULL;
3046 zdb_ddt_entry_t *zdde;
3047 ddt_histogram_t ddh_total = { 0 };
3048 ddt_stat_t dds_total = { 0 };
3050 avl_create(&t, ddt_entry_compare,
3051 sizeof (zdb_ddt_entry_t), offsetof(zdb_ddt_entry_t, zdde_node));
3053 spa_config_enter(spa, SCL_CONFIG, FTAG, RW_READER);
3055 (void) traverse_pool(spa, 0, TRAVERSE_PRE | TRAVERSE_PREFETCH_METADATA,
3056 zdb_ddt_add_cb, &t);
3058 spa_config_exit(spa, SCL_CONFIG, FTAG);
3060 while ((zdde = avl_destroy_nodes(&t, &cookie)) != NULL) {
3062 uint64_t refcnt = zdde->zdde_ref_blocks;
3063 ASSERT(refcnt != 0);
3065 dds.dds_blocks = zdde->zdde_ref_blocks / refcnt;
3066 dds.dds_lsize = zdde->zdde_ref_lsize / refcnt;
3067 dds.dds_psize = zdde->zdde_ref_psize / refcnt;
3068 dds.dds_dsize = zdde->zdde_ref_dsize / refcnt;
3070 dds.dds_ref_blocks = zdde->zdde_ref_blocks;
3071 dds.dds_ref_lsize = zdde->zdde_ref_lsize;
3072 dds.dds_ref_psize = zdde->zdde_ref_psize;
3073 dds.dds_ref_dsize = zdde->zdde_ref_dsize;
3075 ddt_stat_add(&ddh_total.ddh_stat[highbit64(refcnt) - 1],
3078 umem_free(zdde, sizeof (*zdde));
3083 ddt_histogram_stat(&dds_total, &ddh_total);
3085 (void) printf("Simulated DDT histogram:\n");
3087 zpool_dump_ddt(&dds_total, &ddh_total);
3089 dump_dedup_ratio(&dds_total);
3093 dump_zpool(spa_t *spa)
3095 dsl_pool_t *dp = spa_get_dsl(spa);
3098 if (dump_opt['S']) {
3099 dump_simulated_ddt(spa);
3103 if (!dump_opt['e'] && dump_opt['C'] > 1) {
3104 (void) printf("\nCached configuration:\n");
3105 dump_nvlist(spa->spa_config, 8);
3112 dump_uberblock(&spa->spa_uberblock, "\nUberblock:\n", "\n");
3117 if (dump_opt['d'] > 2 || dump_opt['m'])
3118 dump_metaslabs(spa);
3120 dump_metaslab_groups(spa);
3122 if (dump_opt['d'] || dump_opt['i']) {
3123 dump_dir(dp->dp_meta_objset);
3124 if (dump_opt['d'] >= 3) {
3125 dump_full_bpobj(&spa->spa_deferred_bpobj,
3126 "Deferred frees", 0);
3127 if (spa_version(spa) >= SPA_VERSION_DEADLISTS) {
3129 &spa->spa_dsl_pool->dp_free_bpobj,
3130 "Pool snapshot frees", 0);
3133 if (spa_feature_is_active(spa,
3134 SPA_FEATURE_ASYNC_DESTROY)) {
3135 dump_bptree(spa->spa_meta_objset,
3136 spa->spa_dsl_pool->dp_bptree_obj,
3137 "Pool dataset frees");
3139 dump_dtl(spa->spa_root_vdev, 0);
3141 (void) dmu_objset_find(spa_name(spa), dump_one_dir,
3142 NULL, DS_FIND_SNAPSHOTS | DS_FIND_CHILDREN);
3144 for (spa_feature_t f = 0; f < SPA_FEATURES; f++) {
3147 if (!(spa_feature_table[f].fi_flags &
3148 ZFEATURE_FLAG_PER_DATASET)) {
3149 ASSERT0(dataset_feature_count[f]);
3152 (void) feature_get_refcount(spa,
3153 &spa_feature_table[f], &refcount);
3154 if (dataset_feature_count[f] != refcount) {
3155 (void) printf("%s feature refcount mismatch: "
3156 "%lld datasets != %lld refcount\n",
3157 spa_feature_table[f].fi_uname,
3158 (longlong_t)dataset_feature_count[f],
3159 (longlong_t)refcount);
3162 (void) printf("Verified %s feature refcount "
3163 "of %llu is correct\n",
3164 spa_feature_table[f].fi_uname,
3165 (longlong_t)refcount);
3169 if (rc == 0 && (dump_opt['b'] || dump_opt['c']))
3170 rc = dump_block_stats(spa);
3173 rc = verify_spacemap_refcounts(spa);
3176 show_pool_stats(spa);
3182 dump_debug_buffer();
3187 #define ZDB_FLAG_CHECKSUM 0x0001
3188 #define ZDB_FLAG_DECOMPRESS 0x0002
3189 #define ZDB_FLAG_BSWAP 0x0004
3190 #define ZDB_FLAG_GBH 0x0008
3191 #define ZDB_FLAG_INDIRECT 0x0010
3192 #define ZDB_FLAG_PHYS 0x0020
3193 #define ZDB_FLAG_RAW 0x0040
3194 #define ZDB_FLAG_PRINT_BLKPTR 0x0080
3199 zdb_print_blkptr(blkptr_t *bp, int flags)
3201 char blkbuf[BP_SPRINTF_LEN];
3203 if (flags & ZDB_FLAG_BSWAP)
3204 byteswap_uint64_array((void *)bp, sizeof (blkptr_t));
3206 snprintf_blkptr(blkbuf, sizeof (blkbuf), bp);
3207 (void) printf("%s\n", blkbuf);
3211 zdb_dump_indirect(blkptr_t *bp, int nbps, int flags)
3215 for (i = 0; i < nbps; i++)
3216 zdb_print_blkptr(&bp[i], flags);
3220 zdb_dump_gbh(void *buf, int flags)
3222 zdb_dump_indirect((blkptr_t *)buf, SPA_GBH_NBLKPTRS, flags);
3226 zdb_dump_block_raw(void *buf, uint64_t size, int flags)
3228 if (flags & ZDB_FLAG_BSWAP)
3229 byteswap_uint64_array(buf, size);
3230 (void) write(1, buf, size);
3234 zdb_dump_block(char *label, void *buf, uint64_t size, int flags)
3236 uint64_t *d = (uint64_t *)buf;
3237 int nwords = size / sizeof (uint64_t);
3238 int do_bswap = !!(flags & ZDB_FLAG_BSWAP);
3244 hdr = " 7 6 5 4 3 2 1 0 f e d c b a 9 8";
3246 hdr = " 0 1 2 3 4 5 6 7 8 9 a b c d e f";
3248 (void) printf("\n%s\n%6s %s 0123456789abcdef\n", label, "", hdr);
3250 for (i = 0; i < nwords; i += 2) {
3251 (void) printf("%06llx: %016llx %016llx ",
3252 (u_longlong_t)(i * sizeof (uint64_t)),
3253 (u_longlong_t)(do_bswap ? BSWAP_64(d[i]) : d[i]),
3254 (u_longlong_t)(do_bswap ? BSWAP_64(d[i + 1]) : d[i + 1]));
3257 for (j = 0; j < 2 * sizeof (uint64_t); j++)
3258 (void) printf("%c", isprint(c[j]) ? c[j] : '.');
3259 (void) printf("\n");
3264 * There are two acceptable formats:
3265 * leaf_name - For example: c1t0d0 or /tmp/ztest.0a
3266 * child[.child]* - For example: 0.1.1
3268 * The second form can be used to specify arbitrary vdevs anywhere
3269 * in the heirarchy. For example, in a pool with a mirror of
3270 * RAID-Zs, you can specify either RAID-Z vdev with 0.0 or 0.1 .
3273 zdb_vdev_lookup(vdev_t *vdev, char *path)
3281 /* First, assume the x.x.x.x format */
3282 i = (int)strtoul(path, &s, 10);
3283 if (s == path || (s && *s != '.' && *s != '\0'))
3285 if (i < 0 || i >= vdev->vdev_children)
3288 vdev = vdev->vdev_child[i];
3291 return (zdb_vdev_lookup(vdev, s+1));
3294 for (i = 0; i < vdev->vdev_children; i++) {
3295 vdev_t *vc = vdev->vdev_child[i];
3297 if (vc->vdev_path == NULL) {
3298 vc = zdb_vdev_lookup(vc, path);
3305 p = strrchr(vc->vdev_path, '/');
3306 p = p ? p + 1 : vc->vdev_path;
3307 q = &vc->vdev_path[strlen(vc->vdev_path) - 2];
3309 if (strcmp(vc->vdev_path, path) == 0)
3311 if (strcmp(p, path) == 0)
3313 if (strcmp(q, "s0") == 0 && strncmp(p, path, q - p) == 0)
3321 * Read a block from a pool and print it out. The syntax of the
3322 * block descriptor is:
3324 * pool:vdev_specifier:offset:size[:flags]
3326 * pool - The name of the pool you wish to read from
3327 * vdev_specifier - Which vdev (see comment for zdb_vdev_lookup)
3328 * offset - offset, in hex, in bytes
3329 * size - Amount of data to read, in hex, in bytes
3330 * flags - A string of characters specifying options
3331 * b: Decode a blkptr at given offset within block
3332 * *c: Calculate and display checksums
3333 * d: Decompress data before dumping
3334 * e: Byteswap data before dumping
3335 * g: Display data as a gang block header
3336 * i: Display as an indirect block
3337 * p: Do I/O to physical offset
3338 * r: Dump raw data to stdout
3340 * * = not yet implemented
3343 zdb_read_block(char *thing, spa_t *spa)
3345 blkptr_t blk, *bp = &blk;
3346 dva_t *dva = bp->blk_dva;
3348 uint64_t offset = 0, size = 0, psize = 0, lsize = 0, blkptr_offset = 0;
3351 void *pbuf, *lbuf, *buf;
3352 char *s, *p, *dup, *vdev, *flagstr;
3355 dup = strdup(thing);
3356 s = strtok(dup, ":");
3358 s = strtok(NULL, ":");
3359 offset = strtoull(s ? s : "", NULL, 16);
3360 s = strtok(NULL, ":");
3361 size = strtoull(s ? s : "", NULL, 16);
3362 s = strtok(NULL, ":");
3363 flagstr = s ? s : "";
3367 s = "size must not be zero";
3368 if (!IS_P2ALIGNED(size, DEV_BSIZE))
3369 s = "size must be a multiple of sector size";
3370 if (!IS_P2ALIGNED(offset, DEV_BSIZE))
3371 s = "offset must be a multiple of sector size";
3373 (void) printf("Invalid block specifier: %s - %s\n", thing, s);
3378 for (s = strtok(flagstr, ":"); s; s = strtok(NULL, ":")) {
3379 for (i = 0; flagstr[i]; i++) {
3380 int bit = flagbits[(uchar_t)flagstr[i]];
3383 (void) printf("***Invalid flag: %c\n",
3389 /* If it's not something with an argument, keep going */
3390 if ((bit & (ZDB_FLAG_CHECKSUM |
3391 ZDB_FLAG_PRINT_BLKPTR)) == 0)
3394 p = &flagstr[i + 1];
3395 if (bit == ZDB_FLAG_PRINT_BLKPTR)
3396 blkptr_offset = strtoull(p, &p, 16);
3397 if (*p != ':' && *p != '\0') {
3398 (void) printf("***Invalid flag arg: '%s'\n", s);
3402 i += p - &flagstr[i + 1]; /* skip over the number */
3406 vd = zdb_vdev_lookup(spa->spa_root_vdev, vdev);
3408 (void) printf("***Invalid vdev: %s\n", vdev);
3413 (void) fprintf(stderr, "Found vdev: %s\n",
3416 (void) fprintf(stderr, "Found vdev type: %s\n",
3417 vd->vdev_ops->vdev_op_type);
3423 pbuf = umem_alloc(SPA_MAXBLOCKSIZE, UMEM_NOFAIL);
3424 lbuf = umem_alloc(SPA_MAXBLOCKSIZE, UMEM_NOFAIL);
3428 DVA_SET_VDEV(&dva[0], vd->vdev_id);
3429 DVA_SET_OFFSET(&dva[0], offset);
3430 DVA_SET_GANG(&dva[0], !!(flags & ZDB_FLAG_GBH));
3431 DVA_SET_ASIZE(&dva[0], vdev_psize_to_asize(vd, psize));
3433 BP_SET_BIRTH(bp, TXG_INITIAL, TXG_INITIAL);
3435 BP_SET_LSIZE(bp, lsize);
3436 BP_SET_PSIZE(bp, psize);
3437 BP_SET_COMPRESS(bp, ZIO_COMPRESS_OFF);
3438 BP_SET_CHECKSUM(bp, ZIO_CHECKSUM_OFF);
3439 BP_SET_TYPE(bp, DMU_OT_NONE);
3440 BP_SET_LEVEL(bp, 0);
3441 BP_SET_DEDUP(bp, 0);
3442 BP_SET_BYTEORDER(bp, ZFS_HOST_BYTEORDER);
3444 spa_config_enter(spa, SCL_STATE, FTAG, RW_READER);
3445 zio = zio_root(spa, NULL, NULL, 0);
3447 if (vd == vd->vdev_top) {
3449 * Treat this as a normal block read.
3451 zio_nowait(zio_read(zio, spa, bp, pbuf, psize, NULL, NULL,
3452 ZIO_PRIORITY_SYNC_READ,
3453 ZIO_FLAG_CANFAIL | ZIO_FLAG_RAW, NULL));
3456 * Treat this as a vdev child I/O.
3458 zio_nowait(zio_vdev_child_io(zio, bp, vd, offset, pbuf, psize,
3459 ZIO_TYPE_READ, ZIO_PRIORITY_SYNC_READ,
3460 ZIO_FLAG_DONT_CACHE | ZIO_FLAG_DONT_QUEUE |
3461 ZIO_FLAG_DONT_PROPAGATE | ZIO_FLAG_DONT_RETRY |
3462 ZIO_FLAG_CANFAIL | ZIO_FLAG_RAW, NULL, NULL));
3465 error = zio_wait(zio);
3466 spa_config_exit(spa, SCL_STATE, FTAG);
3469 (void) printf("Read of %s failed, error: %d\n", thing, error);
3473 if (flags & ZDB_FLAG_DECOMPRESS) {
3475 * We don't know how the data was compressed, so just try
3476 * every decompress function at every inflated blocksize.
3478 enum zio_compress c;
3479 void *pbuf2 = umem_alloc(SPA_MAXBLOCKSIZE, UMEM_NOFAIL);
3480 void *lbuf2 = umem_alloc(SPA_MAXBLOCKSIZE, UMEM_NOFAIL);
3482 bcopy(pbuf, pbuf2, psize);
3484 VERIFY(random_get_pseudo_bytes((uint8_t *)pbuf + psize,
3485 SPA_MAXBLOCKSIZE - psize) == 0);
3487 VERIFY(random_get_pseudo_bytes((uint8_t *)pbuf2 + psize,
3488 SPA_MAXBLOCKSIZE - psize) == 0);
3490 for (lsize = SPA_MAXBLOCKSIZE; lsize > psize;
3491 lsize -= SPA_MINBLOCKSIZE) {
3492 for (c = 0; c < ZIO_COMPRESS_FUNCTIONS; c++) {
3493 if (zio_decompress_data(c, pbuf, lbuf,
3494 psize, lsize) == 0 &&
3495 zio_decompress_data(c, pbuf2, lbuf2,
3496 psize, lsize) == 0 &&
3497 bcmp(lbuf, lbuf2, lsize) == 0)
3500 if (c != ZIO_COMPRESS_FUNCTIONS)
3502 lsize -= SPA_MINBLOCKSIZE;
3505 umem_free(pbuf2, SPA_MAXBLOCKSIZE);
3506 umem_free(lbuf2, SPA_MAXBLOCKSIZE);
3508 if (lsize <= psize) {
3509 (void) printf("Decompress of %s failed\n", thing);
3519 if (flags & ZDB_FLAG_PRINT_BLKPTR)
3520 zdb_print_blkptr((blkptr_t *)(void *)
3521 ((uintptr_t)buf + (uintptr_t)blkptr_offset), flags);
3522 else if (flags & ZDB_FLAG_RAW)
3523 zdb_dump_block_raw(buf, size, flags);
3524 else if (flags & ZDB_FLAG_INDIRECT)
3525 zdb_dump_indirect((blkptr_t *)buf, size / sizeof (blkptr_t),
3527 else if (flags & ZDB_FLAG_GBH)
3528 zdb_dump_gbh(buf, flags);
3530 zdb_dump_block(thing, buf, size, flags);
3533 umem_free(pbuf, SPA_MAXBLOCKSIZE);
3534 umem_free(lbuf, SPA_MAXBLOCKSIZE);
3539 pool_match(nvlist_t *cfg, char *tgt)
3541 uint64_t v, guid = strtoull(tgt, NULL, 0);
3545 if (nvlist_lookup_uint64(cfg, ZPOOL_CONFIG_POOL_GUID, &v) == 0)
3548 if (nvlist_lookup_string(cfg, ZPOOL_CONFIG_POOL_NAME, &s) == 0)
3549 return (strcmp(s, tgt) == 0);
3555 find_zpool(char **target, nvlist_t **configp, int dirc, char **dirv)
3558 nvlist_t *match = NULL;
3563 importargs_t args = { 0 };
3567 args.can_be_active = B_TRUE;
3569 if ((sepp = strpbrk(*target, "/@")) != NULL) {
3574 pools = zpool_search_import(g_zfs, &args);
3576 if (pools != NULL) {
3577 nvpair_t *elem = NULL;
3578 while ((elem = nvlist_next_nvpair(pools, elem)) != NULL) {
3579 verify(nvpair_value_nvlist(elem, configp) == 0);
3580 if (pool_match(*configp, *target)) {
3582 if (match != NULL) {
3583 /* print previously found config */
3585 (void) printf("%s\n", name);
3586 dump_nvlist(match, 8);
3589 (void) printf("%s\n",
3591 dump_nvlist(*configp, 8);
3594 name = nvpair_name(elem);
3600 (void) fatal("\tMatched %d pools - use pool GUID "
3601 "instead of pool name or \n"
3602 "\tpool name part of a dataset name to select pool", count);
3607 * If pool GUID was specified for pool id, replace it with pool name
3609 if (name && (strstr(*target, name) != *target)) {
3610 int sz = 1 + strlen(name) + ((sepp) ? strlen(sepp) : 0);
3612 *target = umem_alloc(sz, UMEM_NOFAIL);
3613 (void) snprintf(*target, sz, "%s%s", name, sepp ? sepp : "");
3616 *configp = name ? match : NULL;
3622 main(int argc, char **argv)
3625 struct rlimit rl = { 1024, 1024 };
3627 objset_t *os = NULL;
3631 char **searchdirs = NULL;
3634 nvlist_t *policy = NULL;
3635 uint64_t max_txg = UINT64_MAX;
3636 int rewind = ZPOOL_NEVER_REWIND;
3637 char *spa_config_path_env;
3638 boolean_t target_is_spa = B_TRUE;
3640 (void) setrlimit(RLIMIT_NOFILE, &rl);
3641 (void) enable_extended_FILE_stdio(-1, -1);
3643 dprintf_setup(&argc, argv);
3646 * If there is an environment variable SPA_CONFIG_PATH it overrides
3647 * default spa_config_path setting. If -U flag is specified it will
3648 * override this environment variable settings once again.
3650 spa_config_path_env = getenv("SPA_CONFIG_PATH");
3651 if (spa_config_path_env != NULL)
3652 spa_config_path = spa_config_path_env;
3654 while ((c = getopt(argc, argv,
3655 "bcdhilmMI:suCDRSAFLXx:evp:t:U:PGo:")) != -1) {
3684 max_inflight = strtoull(optarg, NULL, 0);
3685 if (max_inflight == 0) {
3686 (void) fprintf(stderr, "maximum number "
3687 "of inflight I/Os must be greater "
3693 if (searchdirs == NULL) {
3694 searchdirs = umem_alloc(sizeof (char *),
3697 char **tmp = umem_alloc((nsearch + 1) *
3698 sizeof (char *), UMEM_NOFAIL);
3699 bcopy(searchdirs, tmp, nsearch *
3701 umem_free(searchdirs,
3702 nsearch * sizeof (char *));
3705 searchdirs[nsearch++] = optarg;
3708 max_txg = strtoull(optarg, NULL, 0);
3709 if (max_txg < TXG_INITIAL) {
3710 (void) fprintf(stderr, "incorrect txg "
3711 "specified: %s\n", optarg);
3716 spa_config_path = optarg;
3722 vn_dumpdir = optarg;
3725 error = set_global_var(optarg);
3735 if (!dump_opt['e'] && searchdirs != NULL) {
3736 (void) fprintf(stderr, "-p option requires use of -e\n");
3741 * ZDB does not typically re-read blocks; therefore limit the ARC
3742 * to 256 MB, which can be used entirely for metadata.
3744 zfs_arc_max = zfs_arc_meta_limit = 256 * 1024 * 1024;
3747 * "zdb -c" uses checksum-verifying scrub i/os which are async reads.
3748 * "zdb -b" uses traversal prefetch which uses async reads.
3749 * For good performance, let several of them be active at once.
3751 zfs_vdev_async_read_max_active = 10;
3754 g_zfs = libzfs_init();
3756 fatal("Fail to initialize zfs");
3759 verbose = MAX(verbose, 1);
3761 for (c = 0; c < 256; c++) {
3762 if (dump_all && !strchr("elAFLRSXP", c))
3765 dump_opt[c] += verbose;
3768 aok = (dump_opt['A'] == 1) || (dump_opt['A'] > 2);
3769 zfs_recover = (dump_opt['A'] > 1);
3774 if (argc < 2 && dump_opt['R'])
3777 if (!dump_opt['e'] && dump_opt['C']) {
3778 dump_cachefile(spa_config_path);
3784 if (dump_opt['l']) {
3785 dump_label(argv[0]);
3789 if (dump_opt['X'] || dump_opt['F'])
3790 rewind = ZPOOL_DO_REWIND |
3791 (dump_opt['X'] ? ZPOOL_EXTREME_REWIND : 0);
3793 if (nvlist_alloc(&policy, NV_UNIQUE_NAME_TYPE, 0) != 0 ||
3794 nvlist_add_uint64(policy, ZPOOL_REWIND_REQUEST_TXG, max_txg) != 0 ||
3795 nvlist_add_uint32(policy, ZPOOL_REWIND_REQUEST, rewind) != 0)
3796 fatal("internal error: %s", strerror(ENOMEM));
3801 if (dump_opt['e']) {
3802 nvlist_t *cfg = NULL;
3803 char *name = find_zpool(&target, &cfg, nsearch, searchdirs);
3807 if (dump_opt['C'] > 1) {
3808 (void) printf("\nConfiguration for import:\n");
3809 dump_nvlist(cfg, 8);
3811 if (nvlist_add_nvlist(cfg,
3812 ZPOOL_REWIND_POLICY, policy) != 0) {
3813 fatal("can't open '%s': %s",
3814 target, strerror(ENOMEM));
3816 if ((error = spa_import(name, cfg, NULL,
3817 ZFS_IMPORT_MISSING_LOG)) != 0) {
3818 error = spa_import(name, cfg, NULL,
3819 ZFS_IMPORT_VERBATIM);
3824 if (strpbrk(target, "/@") != NULL) {
3827 target_is_spa = B_FALSE;
3829 * Remove any trailing slash. Later code would get confused
3830 * by it, but we want to allow it so that "pool/" can
3831 * indicate that we want to dump the topmost filesystem,
3832 * rather than the whole pool.
3834 targetlen = strlen(target);
3835 if (targetlen != 0 && target[targetlen - 1] == '/')
3836 target[targetlen - 1] = '\0';
3840 if (target_is_spa || dump_opt['R']) {
3841 error = spa_open_rewind(target, &spa, FTAG, policy,
3845 * If we're missing the log device then
3846 * try opening the pool after clearing the
3849 mutex_enter(&spa_namespace_lock);
3850 if ((spa = spa_lookup(target)) != NULL &&
3851 spa->spa_log_state == SPA_LOG_MISSING) {
3852 spa->spa_log_state = SPA_LOG_CLEAR;
3855 mutex_exit(&spa_namespace_lock);
3858 error = spa_open_rewind(target, &spa,
3859 FTAG, policy, NULL);
3863 error = dmu_objset_own(target, DMU_OST_ANY,
3867 nvlist_free(policy);
3870 fatal("can't open '%s': %s", target, strerror(error));
3874 if (!dump_opt['R']) {
3876 zopt_objects = argc;
3877 zopt_object = calloc(zopt_objects, sizeof (uint64_t));
3878 for (i = 0; i < zopt_objects; i++) {
3880 zopt_object[i] = strtoull(argv[i], NULL, 0);
3881 if (zopt_object[i] == 0 && errno != 0)
3882 fatal("bad number %s: %s",
3883 argv[i], strerror(errno));
3888 } else if (zopt_objects > 0 && !dump_opt['m']) {
3889 dump_dir(spa->spa_meta_objset);
3894 flagbits['b'] = ZDB_FLAG_PRINT_BLKPTR;
3895 flagbits['c'] = ZDB_FLAG_CHECKSUM;
3896 flagbits['d'] = ZDB_FLAG_DECOMPRESS;
3897 flagbits['e'] = ZDB_FLAG_BSWAP;
3898 flagbits['g'] = ZDB_FLAG_GBH;
3899 flagbits['i'] = ZDB_FLAG_INDIRECT;
3900 flagbits['p'] = ZDB_FLAG_PHYS;
3901 flagbits['r'] = ZDB_FLAG_RAW;
3903 for (i = 0; i < argc; i++)
3904 zdb_read_block(argv[i], spa);
3907 (os != NULL) ? dmu_objset_disown(os, FTAG) : spa_close(spa, FTAG);
3909 fuid_table_destroy();
3910 sa_loaded = B_FALSE;
3912 dump_debug_buffer();