4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License (the "License").
6 * You may not use this file except in compliance with the License.
8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 * or http://www.opensolaris.org/os/licensing.
10 * See the License for the specific language governing permissions
11 * and limitations under the License.
13 * When distributing Covered Code, include this CDDL HEADER in each
14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 * If applicable, add the following below this CDDL HEADER, with the
16 * fields enclosed by brackets "[]" replaced with your own identifying
17 * information: Portions Copyright [yyyy] [name of copyright owner]
23 * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
24 * Copyright (c) 2011, 2016 by Delphix. All rights reserved.
25 * Copyright (c) 2014 Integros [integros.com]
26 * Copyright 2016 Nexenta Systems, Inc.
27 * Copyright (c) 2017 Lawrence Livermore National Security, LLC.
28 * Copyright (c) 2015, 2017, Intel Corporation.
33 #include <stdio_ext.h>
36 #include <sys/zfs_context.h>
38 #include <sys/spa_impl.h>
41 #include <sys/fs/zfs.h>
42 #include <sys/zfs_znode.h>
43 #include <sys/zfs_sa.h>
45 #include <sys/sa_impl.h>
47 #include <sys/vdev_impl.h>
48 #include <sys/metaslab_impl.h>
49 #include <sys/dmu_objset.h>
50 #include <sys/dsl_dir.h>
51 #include <sys/dsl_dataset.h>
52 #include <sys/dsl_pool.h>
55 #include <sys/zil_impl.h>
57 #include <sys/resource.h>
58 #include <sys/dmu_traverse.h>
59 #include <sys/zio_checksum.h>
60 #include <sys/zio_compress.h>
61 #include <sys/zfs_fuid.h>
64 #include <sys/zfeature.h>
66 #include <sys/blkptr.h>
67 #include <zfs_comutil.h>
70 #define ZDB_COMPRESS_NAME(idx) ((idx) < ZIO_COMPRESS_FUNCTIONS ? \
71 zio_compress_table[(idx)].ci_name : "UNKNOWN")
72 #define ZDB_CHECKSUM_NAME(idx) ((idx) < ZIO_CHECKSUM_FUNCTIONS ? \
73 zio_checksum_table[(idx)].ci_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 zdb_ot_name(dmu_object_type_t type)
81 if (type < DMU_OT_NUMTYPES)
82 return (dmu_ot[type].ot_name);
83 else if ((type & DMU_OT_NEWTYPE) &&
84 ((type & DMU_OT_BYTESWAP_MASK) < DMU_BSWAP_NUMFUNCS))
85 return (dmu_ot_byteswap[type & DMU_OT_BYTESWAP_MASK].ob_name);
90 extern int reference_tracking_enable;
91 extern int zfs_recover;
92 extern uint64_t zfs_arc_max, zfs_arc_meta_limit;
93 extern int zfs_vdev_async_read_max_active;
95 const char cmdname[] = "zdb";
96 uint8_t dump_opt[256];
98 typedef void object_viewer_t(objset_t *, uint64_t, void *data, size_t size);
100 extern void dump_intent_log(zilog_t *);
101 uint64_t *zopt_object = NULL;
102 int zopt_objects = 0;
103 libzfs_handle_t *g_zfs;
104 uint64_t max_inflight = 1000;
106 static void snprintf_blkptr_compact(char *, size_t, const blkptr_t *);
109 * These libumem hooks provide a reasonable set of defaults for the allocator's
110 * debugging facilities.
113 _umem_debug_init(void)
115 return ("default,verbose"); /* $UMEM_DEBUG setting */
119 _umem_logging_init(void)
121 return ("fail,contents"); /* $UMEM_LOGGING setting */
127 (void) fprintf(stderr,
128 "Usage:\t%s [-AbcdDFGhiLMPsvX] [-e [-V] [-p <path> ...]] "
129 "[-I <inflight I/Os>]\n"
130 "\t\t[-o <var>=<value>]... [-t <txg>] [-U <cache>] [-x <dumpdir>]\n"
131 "\t\t[<poolname> [<object> ...]]\n"
132 "\t%s [-AdiPv] [-e [-V] [-p <path> ...]] [-U <cache>] <dataset> "
134 "\t%s -C [-A] [-U <cache>]\n"
135 "\t%s -l [-Aqu] <device>\n"
136 "\t%s -m [-AFLPX] [-e [-V] [-p <path> ...]] [-t <txg>] "
137 "[-U <cache>]\n\t\t<poolname> [<vdev> [<metaslab> ...]]\n"
138 "\t%s -O <dataset> <path>\n"
139 "\t%s -R [-A] [-e [-V] [-p <path> ...]] [-U <cache>]\n"
140 "\t\t<poolname> <vdev>:<offset>:<size>[:<flags>]\n"
141 "\t%s -E [-A] word0:word1:...:word15\n"
142 "\t%s -S [-AP] [-e [-V] [-p <path> ...]] [-U <cache>] "
144 cmdname, cmdname, cmdname, cmdname, cmdname, cmdname, cmdname,
147 (void) fprintf(stderr, " Dataset name must include at least one "
148 "separator character '/' or '@'\n");
149 (void) fprintf(stderr, " If dataset name is specified, only that "
150 "dataset is dumped\n");
151 (void) fprintf(stderr, " If object numbers are specified, only "
152 "those objects are dumped\n\n");
153 (void) fprintf(stderr, " Options to control amount of output:\n");
154 (void) fprintf(stderr, " -b block statistics\n");
155 (void) fprintf(stderr, " -c checksum all metadata (twice for "
156 "all data) blocks\n");
157 (void) fprintf(stderr, " -C config (or cachefile if alone)\n");
158 (void) fprintf(stderr, " -d dataset(s)\n");
159 (void) fprintf(stderr, " -D dedup statistics\n");
160 (void) fprintf(stderr, " -E decode and display block from an "
161 "embedded block pointer\n");
162 (void) fprintf(stderr, " -h pool history\n");
163 (void) fprintf(stderr, " -i intent logs\n");
164 (void) fprintf(stderr, " -l read label contents\n");
165 (void) fprintf(stderr, " -L disable leak tracking (do not "
166 "load spacemaps)\n");
167 (void) fprintf(stderr, " -m metaslabs\n");
168 (void) fprintf(stderr, " -M metaslab groups\n");
169 (void) fprintf(stderr, " -O perform object lookups by path\n");
170 (void) fprintf(stderr, " -R read and display block from a "
172 (void) fprintf(stderr, " -s report stats on zdb's I/O\n");
173 (void) fprintf(stderr, " -S simulate dedup to measure effect\n");
174 (void) fprintf(stderr, " -v verbose (applies to all "
176 (void) fprintf(stderr, " Below options are intended for use "
177 "with other options:\n");
178 (void) fprintf(stderr, " -A ignore assertions (-A), enable "
179 "panic recovery (-AA) or both (-AAA)\n");
180 (void) fprintf(stderr, " -e pool is exported/destroyed/"
181 "has altroot/not in a cachefile\n");
182 (void) fprintf(stderr, " -F attempt automatic rewind within "
183 "safe range of transaction groups\n");
184 (void) fprintf(stderr, " -G dump zfs_dbgmsg buffer before "
186 (void) fprintf(stderr, " -I <number of inflight I/Os> -- "
187 "specify the maximum number of\n "
188 "checksumming I/Os [default is 200]\n");
189 (void) fprintf(stderr, " -o <variable>=<value> set global "
190 "variable to an unsigned 32-bit integer\n");
191 (void) fprintf(stderr, " -p <path> -- use one or more with "
192 "-e to specify path to vdev dir\n");
193 (void) fprintf(stderr, " -P print numbers in parseable form\n");
194 (void) fprintf(stderr, " -q don't print label contents\n");
195 (void) fprintf(stderr, " -t <txg> -- highest txg to use when "
196 "searching for uberblocks\n");
197 (void) fprintf(stderr, " -u uberblock\n");
198 (void) fprintf(stderr, " -U <cachefile_path> -- use alternate "
200 (void) fprintf(stderr, " -V do verbatim import\n");
201 (void) fprintf(stderr, " -x <dumpdir> -- "
202 "dump all read blocks into specified directory\n");
203 (void) fprintf(stderr, " -X attempt extreme rewind (does not "
204 "work with dataset)\n");
205 (void) fprintf(stderr, "Specify an option more than once (e.g. -bb) "
206 "to make only that option verbose\n");
207 (void) fprintf(stderr, "Default is to dump everything non-verbosely\n");
212 dump_debug_buffer(void)
216 zfs_dbgmsg_print("zdb");
221 * Called for usage errors that are discovered after a call to spa_open(),
222 * dmu_bonus_hold(), or pool_match(). abort() is called for other errors.
226 fatal(const char *fmt, ...)
231 (void) fprintf(stderr, "%s: ", cmdname);
232 (void) vfprintf(stderr, fmt, ap);
234 (void) fprintf(stderr, "\n");
243 dump_packed_nvlist(objset_t *os, uint64_t object, void *data, size_t size)
246 size_t nvsize = *(uint64_t *)data;
247 char *packed = umem_alloc(nvsize, UMEM_NOFAIL);
249 VERIFY(0 == dmu_read(os, object, 0, nvsize, packed, DMU_READ_PREFETCH));
251 VERIFY(nvlist_unpack(packed, nvsize, &nv, 0) == 0);
253 umem_free(packed, nvsize);
262 dump_history_offsets(objset_t *os, uint64_t object, void *data, size_t size)
264 spa_history_phys_t *shp = data;
269 (void) printf("\t\tpool_create_len = %llu\n",
270 (u_longlong_t)shp->sh_pool_create_len);
271 (void) printf("\t\tphys_max_off = %llu\n",
272 (u_longlong_t)shp->sh_phys_max_off);
273 (void) printf("\t\tbof = %llu\n",
274 (u_longlong_t)shp->sh_bof);
275 (void) printf("\t\teof = %llu\n",
276 (u_longlong_t)shp->sh_eof);
277 (void) printf("\t\trecords_lost = %llu\n",
278 (u_longlong_t)shp->sh_records_lost);
282 zdb_nicenum(uint64_t num, char *buf)
285 (void) sprintf(buf, "%llu", (longlong_t)num);
290 const char histo_stars[] = "****************************************";
291 const int histo_width = sizeof (histo_stars) - 1;
294 dump_histogram(const uint64_t *histo, int size, int offset)
297 int minidx = size - 1;
301 for (i = 0; i < size; i++) {
304 if (histo[i] > 0 && i > maxidx)
306 if (histo[i] > 0 && i < minidx)
310 if (max < histo_width)
313 for (i = minidx; i <= maxidx; i++) {
314 (void) printf("\t\t\t%3u: %6llu %s\n",
315 i + offset, (u_longlong_t)histo[i],
316 &histo_stars[(max - histo[i]) * histo_width / max]);
321 dump_zap_stats(objset_t *os, uint64_t object)
326 error = zap_get_stats(os, object, &zs);
330 if (zs.zs_ptrtbl_len == 0) {
331 ASSERT(zs.zs_num_blocks == 1);
332 (void) printf("\tmicrozap: %llu bytes, %llu entries\n",
333 (u_longlong_t)zs.zs_blocksize,
334 (u_longlong_t)zs.zs_num_entries);
338 (void) printf("\tFat ZAP stats:\n");
340 (void) printf("\t\tPointer table:\n");
341 (void) printf("\t\t\t%llu elements\n",
342 (u_longlong_t)zs.zs_ptrtbl_len);
343 (void) printf("\t\t\tzt_blk: %llu\n",
344 (u_longlong_t)zs.zs_ptrtbl_zt_blk);
345 (void) printf("\t\t\tzt_numblks: %llu\n",
346 (u_longlong_t)zs.zs_ptrtbl_zt_numblks);
347 (void) printf("\t\t\tzt_shift: %llu\n",
348 (u_longlong_t)zs.zs_ptrtbl_zt_shift);
349 (void) printf("\t\t\tzt_blks_copied: %llu\n",
350 (u_longlong_t)zs.zs_ptrtbl_blks_copied);
351 (void) printf("\t\t\tzt_nextblk: %llu\n",
352 (u_longlong_t)zs.zs_ptrtbl_nextblk);
354 (void) printf("\t\tZAP entries: %llu\n",
355 (u_longlong_t)zs.zs_num_entries);
356 (void) printf("\t\tLeaf blocks: %llu\n",
357 (u_longlong_t)zs.zs_num_leafs);
358 (void) printf("\t\tTotal blocks: %llu\n",
359 (u_longlong_t)zs.zs_num_blocks);
360 (void) printf("\t\tzap_block_type: 0x%llx\n",
361 (u_longlong_t)zs.zs_block_type);
362 (void) printf("\t\tzap_magic: 0x%llx\n",
363 (u_longlong_t)zs.zs_magic);
364 (void) printf("\t\tzap_salt: 0x%llx\n",
365 (u_longlong_t)zs.zs_salt);
367 (void) printf("\t\tLeafs with 2^n pointers:\n");
368 dump_histogram(zs.zs_leafs_with_2n_pointers, ZAP_HISTOGRAM_SIZE, 0);
370 (void) printf("\t\tBlocks with n*5 entries:\n");
371 dump_histogram(zs.zs_blocks_with_n5_entries, ZAP_HISTOGRAM_SIZE, 0);
373 (void) printf("\t\tBlocks n/10 full:\n");
374 dump_histogram(zs.zs_blocks_n_tenths_full, ZAP_HISTOGRAM_SIZE, 0);
376 (void) printf("\t\tEntries with n chunks:\n");
377 dump_histogram(zs.zs_entries_using_n_chunks, ZAP_HISTOGRAM_SIZE, 0);
379 (void) printf("\t\tBuckets with n entries:\n");
380 dump_histogram(zs.zs_buckets_with_n_entries, ZAP_HISTOGRAM_SIZE, 0);
385 dump_none(objset_t *os, uint64_t object, void *data, size_t size)
391 dump_unknown(objset_t *os, uint64_t object, void *data, size_t size)
393 (void) printf("\tUNKNOWN OBJECT TYPE\n");
398 dump_uint8(objset_t *os, uint64_t object, void *data, size_t size)
404 dump_uint64(objset_t *os, uint64_t object, void *data, size_t size)
410 dump_zap(objset_t *os, uint64_t object, void *data, size_t size)
413 zap_attribute_t attr;
417 dump_zap_stats(os, object);
420 for (zap_cursor_init(&zc, os, object);
421 zap_cursor_retrieve(&zc, &attr) == 0;
422 zap_cursor_advance(&zc)) {
423 (void) printf("\t\t%s = ", attr.za_name);
424 if (attr.za_num_integers == 0) {
428 prop = umem_zalloc(attr.za_num_integers *
429 attr.za_integer_length, UMEM_NOFAIL);
430 (void) zap_lookup(os, object, attr.za_name,
431 attr.za_integer_length, attr.za_num_integers, prop);
432 if (attr.za_integer_length == 1) {
433 (void) printf("%s", (char *)prop);
435 for (i = 0; i < attr.za_num_integers; i++) {
436 switch (attr.za_integer_length) {
439 ((uint16_t *)prop)[i]);
443 ((uint32_t *)prop)[i]);
446 (void) printf("%lld ",
447 (u_longlong_t)((int64_t *)prop)[i]);
453 umem_free(prop, attr.za_num_integers * attr.za_integer_length);
455 zap_cursor_fini(&zc);
459 dump_bpobj(objset_t *os, uint64_t object, void *data, size_t size)
461 bpobj_phys_t *bpop = data;
463 char bytes[32], comp[32], uncomp[32];
468 zdb_nicenum(bpop->bpo_bytes, bytes);
469 zdb_nicenum(bpop->bpo_comp, comp);
470 zdb_nicenum(bpop->bpo_uncomp, uncomp);
472 (void) printf("\t\tnum_blkptrs = %llu\n",
473 (u_longlong_t)bpop->bpo_num_blkptrs);
474 (void) printf("\t\tbytes = %s\n", bytes);
475 if (size >= BPOBJ_SIZE_V1) {
476 (void) printf("\t\tcomp = %s\n", comp);
477 (void) printf("\t\tuncomp = %s\n", uncomp);
479 if (size >= sizeof (*bpop)) {
480 (void) printf("\t\tsubobjs = %llu\n",
481 (u_longlong_t)bpop->bpo_subobjs);
482 (void) printf("\t\tnum_subobjs = %llu\n",
483 (u_longlong_t)bpop->bpo_num_subobjs);
486 if (dump_opt['d'] < 5)
489 for (i = 0; i < bpop->bpo_num_blkptrs; i++) {
490 char blkbuf[BP_SPRINTF_LEN];
493 int err = dmu_read(os, object,
494 i * sizeof (bp), sizeof (bp), &bp, 0);
496 (void) printf("got error %u from dmu_read\n", err);
499 snprintf_blkptr_compact(blkbuf, sizeof (blkbuf), &bp);
500 (void) printf("\t%s\n", blkbuf);
506 dump_bpobj_subobjs(objset_t *os, uint64_t object, void *data, size_t size)
508 dmu_object_info_t doi;
511 VERIFY0(dmu_object_info(os, object, &doi));
512 uint64_t *subobjs = kmem_alloc(doi.doi_max_offset, KM_SLEEP);
514 int err = dmu_read(os, object, 0, doi.doi_max_offset, subobjs, 0);
516 (void) printf("got error %u from dmu_read\n", err);
517 kmem_free(subobjs, doi.doi_max_offset);
521 int64_t last_nonzero = -1;
522 for (i = 0; i < doi.doi_max_offset / 8; i++) {
527 for (i = 0; i <= last_nonzero; i++) {
528 (void) printf("\t%llu\n", (u_longlong_t)subobjs[i]);
530 kmem_free(subobjs, doi.doi_max_offset);
535 dump_ddt_zap(objset_t *os, uint64_t object, void *data, size_t size)
537 dump_zap_stats(os, object);
538 /* contents are printed elsewhere, properly decoded */
543 dump_sa_attrs(objset_t *os, uint64_t object, void *data, size_t size)
546 zap_attribute_t attr;
548 dump_zap_stats(os, object);
551 for (zap_cursor_init(&zc, os, object);
552 zap_cursor_retrieve(&zc, &attr) == 0;
553 zap_cursor_advance(&zc)) {
554 (void) printf("\t\t%s = ", attr.za_name);
555 if (attr.za_num_integers == 0) {
559 (void) printf(" %llx : [%d:%d:%d]\n",
560 (u_longlong_t)attr.za_first_integer,
561 (int)ATTR_LENGTH(attr.za_first_integer),
562 (int)ATTR_BSWAP(attr.za_first_integer),
563 (int)ATTR_NUM(attr.za_first_integer));
565 zap_cursor_fini(&zc);
570 dump_sa_layouts(objset_t *os, uint64_t object, void *data, size_t size)
573 zap_attribute_t attr;
574 uint16_t *layout_attrs;
577 dump_zap_stats(os, object);
580 for (zap_cursor_init(&zc, os, object);
581 zap_cursor_retrieve(&zc, &attr) == 0;
582 zap_cursor_advance(&zc)) {
583 (void) printf("\t\t%s = [", attr.za_name);
584 if (attr.za_num_integers == 0) {
589 VERIFY(attr.za_integer_length == 2);
590 layout_attrs = umem_zalloc(attr.za_num_integers *
591 attr.za_integer_length, UMEM_NOFAIL);
593 VERIFY(zap_lookup(os, object, attr.za_name,
594 attr.za_integer_length,
595 attr.za_num_integers, layout_attrs) == 0);
597 for (i = 0; i != attr.za_num_integers; i++)
598 (void) printf(" %d ", (int)layout_attrs[i]);
599 (void) printf("]\n");
600 umem_free(layout_attrs,
601 attr.za_num_integers * attr.za_integer_length);
603 zap_cursor_fini(&zc);
608 dump_zpldir(objset_t *os, uint64_t object, void *data, size_t size)
611 zap_attribute_t attr;
612 const char *typenames[] = {
613 /* 0 */ "not specified",
615 /* 2 */ "Character Device",
616 /* 3 */ "3 (invalid)",
618 /* 5 */ "5 (invalid)",
619 /* 6 */ "Block Device",
620 /* 7 */ "7 (invalid)",
621 /* 8 */ "Regular File",
622 /* 9 */ "9 (invalid)",
623 /* 10 */ "Symbolic Link",
624 /* 11 */ "11 (invalid)",
627 /* 14 */ "Event Port",
628 /* 15 */ "15 (invalid)",
631 dump_zap_stats(os, object);
634 for (zap_cursor_init(&zc, os, object);
635 zap_cursor_retrieve(&zc, &attr) == 0;
636 zap_cursor_advance(&zc)) {
637 (void) printf("\t\t%s = %lld (type: %s)\n",
638 attr.za_name, ZFS_DIRENT_OBJ(attr.za_first_integer),
639 typenames[ZFS_DIRENT_TYPE(attr.za_first_integer)]);
641 zap_cursor_fini(&zc);
645 get_dtl_refcount(vdev_t *vd)
650 if (vd->vdev_ops->vdev_op_leaf) {
651 space_map_t *sm = vd->vdev_dtl_sm;
654 sm->sm_dbuf->db_size == sizeof (space_map_phys_t))
659 for (c = 0; c < vd->vdev_children; c++)
660 refcount += get_dtl_refcount(vd->vdev_child[c]);
665 get_metaslab_refcount(vdev_t *vd)
670 if (vd->vdev_top == vd && !vd->vdev_removing) {
671 for (m = 0; m < vd->vdev_ms_count; m++) {
672 space_map_t *sm = vd->vdev_ms[m]->ms_sm;
675 sm->sm_dbuf->db_size == sizeof (space_map_phys_t))
679 for (c = 0; c < vd->vdev_children; c++)
680 refcount += get_metaslab_refcount(vd->vdev_child[c]);
686 verify_spacemap_refcounts(spa_t *spa)
688 uint64_t expected_refcount = 0;
689 uint64_t actual_refcount;
691 (void) feature_get_refcount(spa,
692 &spa_feature_table[SPA_FEATURE_SPACEMAP_HISTOGRAM],
694 actual_refcount = get_dtl_refcount(spa->spa_root_vdev);
695 actual_refcount += get_metaslab_refcount(spa->spa_root_vdev);
697 if (expected_refcount != actual_refcount) {
698 (void) printf("space map refcount mismatch: expected %lld != "
700 (longlong_t)expected_refcount,
701 (longlong_t)actual_refcount);
708 dump_spacemap(objset_t *os, space_map_t *sm)
710 uint64_t alloc, offset, entry;
711 char *ddata[] = { "ALLOC", "FREE", "CONDENSE", "INVALID",
712 "INVALID", "INVALID", "INVALID", "INVALID" };
718 * Print out the freelist entries in both encoded and decoded form.
721 for (offset = 0; offset < space_map_length(sm);
722 offset += sizeof (entry)) {
723 uint8_t mapshift = sm->sm_shift;
725 VERIFY0(dmu_read(os, space_map_object(sm), offset,
726 sizeof (entry), &entry, DMU_READ_PREFETCH));
727 if (SM_DEBUG_DECODE(entry)) {
729 (void) printf("\t [%6llu] %s: txg %llu, pass %llu\n",
730 (u_longlong_t)(offset / sizeof (entry)),
731 ddata[SM_DEBUG_ACTION_DECODE(entry)],
732 (u_longlong_t)SM_DEBUG_TXG_DECODE(entry),
733 (u_longlong_t)SM_DEBUG_SYNCPASS_DECODE(entry));
735 (void) printf("\t [%6llu] %c range:"
736 " %010llx-%010llx size: %06llx\n",
737 (u_longlong_t)(offset / sizeof (entry)),
738 SM_TYPE_DECODE(entry) == SM_ALLOC ? 'A' : 'F',
739 (u_longlong_t)((SM_OFFSET_DECODE(entry) <<
740 mapshift) + sm->sm_start),
741 (u_longlong_t)((SM_OFFSET_DECODE(entry) <<
742 mapshift) + sm->sm_start +
743 (SM_RUN_DECODE(entry) << mapshift)),
744 (u_longlong_t)(SM_RUN_DECODE(entry) << mapshift));
745 if (SM_TYPE_DECODE(entry) == SM_ALLOC)
746 alloc += SM_RUN_DECODE(entry) << mapshift;
748 alloc -= SM_RUN_DECODE(entry) << mapshift;
751 if (alloc != space_map_allocated(sm)) {
752 (void) printf("space_map_object alloc (%llu) INCONSISTENT "
753 "with space map summary (%llu)\n",
754 (u_longlong_t)space_map_allocated(sm), (u_longlong_t)alloc);
759 dump_metaslab_stats(metaslab_t *msp)
762 range_tree_t *rt = msp->ms_tree;
763 avl_tree_t *t = &msp->ms_size_tree;
764 int free_pct = range_tree_space(rt) * 100 / msp->ms_size;
766 zdb_nicenum(metaslab_block_maxsize(msp), maxbuf);
768 (void) printf("\t %25s %10lu %7s %6s %4s %4d%%\n",
769 "segments", avl_numnodes(t), "maxsize", maxbuf,
770 "freepct", free_pct);
771 (void) printf("\tIn-memory histogram:\n");
772 dump_histogram(rt->rt_histogram, RANGE_TREE_HISTOGRAM_SIZE, 0);
776 dump_metaslab(metaslab_t *msp)
778 vdev_t *vd = msp->ms_group->mg_vd;
779 spa_t *spa = vd->vdev_spa;
780 space_map_t *sm = msp->ms_sm;
783 zdb_nicenum(msp->ms_size - space_map_allocated(sm), freebuf);
786 "\tmetaslab %6llu offset %12llx spacemap %6llu free %5s\n",
787 (u_longlong_t)msp->ms_id, (u_longlong_t)msp->ms_start,
788 (u_longlong_t)space_map_object(sm), freebuf);
790 if (dump_opt['m'] > 2 && !dump_opt['L']) {
791 mutex_enter(&msp->ms_lock);
792 metaslab_load_wait(msp);
793 if (!msp->ms_loaded) {
794 VERIFY0(metaslab_load(msp));
795 range_tree_stat_verify(msp->ms_tree);
797 dump_metaslab_stats(msp);
798 metaslab_unload(msp);
799 mutex_exit(&msp->ms_lock);
802 if (dump_opt['m'] > 1 && sm != NULL &&
803 spa_feature_is_active(spa, SPA_FEATURE_SPACEMAP_HISTOGRAM)) {
805 * The space map histogram represents free space in chunks
806 * of sm_shift (i.e. bucket 0 refers to 2^sm_shift).
808 (void) printf("\tOn-disk histogram:\t\tfragmentation %llu\n",
809 (u_longlong_t)msp->ms_fragmentation);
810 dump_histogram(sm->sm_phys->smp_histogram,
811 SPACE_MAP_HISTOGRAM_SIZE, sm->sm_shift);
814 if (dump_opt['d'] > 5 || dump_opt['m'] > 3) {
815 ASSERT(msp->ms_size == (1ULL << vd->vdev_ms_shift));
817 mutex_enter(&msp->ms_lock);
818 dump_spacemap(spa->spa_meta_objset, msp->ms_sm);
819 mutex_exit(&msp->ms_lock);
824 print_vdev_metaslab_header(vdev_t *vd)
826 (void) printf("\tvdev %10llu\n\t%-10s%5llu %-19s %-15s %-10s\n",
827 (u_longlong_t)vd->vdev_id,
828 "metaslabs", (u_longlong_t)vd->vdev_ms_count,
829 "offset", "spacemap", "free");
830 (void) printf("\t%15s %19s %15s %10s\n",
831 "---------------", "-------------------",
832 "---------------", "-------------");
836 dump_metaslab_groups(spa_t *spa)
838 vdev_t *rvd = spa->spa_root_vdev;
839 metaslab_class_t *mc = spa_normal_class(spa);
840 uint64_t fragmentation;
843 metaslab_class_histogram_verify(mc);
845 for (c = 0; c < rvd->vdev_children; c++) {
846 vdev_t *tvd = rvd->vdev_child[c];
847 metaslab_group_t *mg = tvd->vdev_mg;
849 if (mg->mg_class != mc)
852 metaslab_group_histogram_verify(mg);
853 mg->mg_fragmentation = metaslab_group_fragmentation(mg);
855 (void) printf("\tvdev %10llu\t\tmetaslabs%5llu\t\t"
857 (u_longlong_t)tvd->vdev_id,
858 (u_longlong_t)tvd->vdev_ms_count);
859 if (mg->mg_fragmentation == ZFS_FRAG_INVALID) {
860 (void) printf("%3s\n", "-");
862 (void) printf("%3llu%%\n",
863 (u_longlong_t)mg->mg_fragmentation);
865 dump_histogram(mg->mg_histogram, RANGE_TREE_HISTOGRAM_SIZE, 0);
868 (void) printf("\tpool %s\tfragmentation", spa_name(spa));
869 fragmentation = metaslab_class_fragmentation(mc);
870 if (fragmentation == ZFS_FRAG_INVALID)
871 (void) printf("\t%3s\n", "-");
873 (void) printf("\t%3llu%%\n", (u_longlong_t)fragmentation);
874 dump_histogram(mc->mc_histogram, RANGE_TREE_HISTOGRAM_SIZE, 0);
878 dump_metaslabs(spa_t *spa)
880 vdev_t *vd, *rvd = spa->spa_root_vdev;
881 uint64_t m, c = 0, children = rvd->vdev_children;
883 (void) printf("\nMetaslabs:\n");
885 if (!dump_opt['d'] && zopt_objects > 0) {
889 (void) fatal("bad vdev id: %llu", (u_longlong_t)c);
891 if (zopt_objects > 1) {
892 vd = rvd->vdev_child[c];
893 print_vdev_metaslab_header(vd);
895 for (m = 1; m < zopt_objects; m++) {
896 if (zopt_object[m] < vd->vdev_ms_count)
898 vd->vdev_ms[zopt_object[m]]);
900 (void) fprintf(stderr, "bad metaslab "
902 (u_longlong_t)zopt_object[m]);
909 for (; c < children; c++) {
910 vd = rvd->vdev_child[c];
911 print_vdev_metaslab_header(vd);
913 for (m = 0; m < vd->vdev_ms_count; m++)
914 dump_metaslab(vd->vdev_ms[m]);
920 dump_dde(const ddt_t *ddt, const ddt_entry_t *dde, uint64_t index)
922 const ddt_phys_t *ddp = dde->dde_phys;
923 const ddt_key_t *ddk = &dde->dde_key;
924 char *types[4] = { "ditto", "single", "double", "triple" };
925 char blkbuf[BP_SPRINTF_LEN];
929 for (p = 0; p < DDT_PHYS_TYPES; p++, ddp++) {
930 if (ddp->ddp_phys_birth == 0)
932 ddt_bp_create(ddt->ddt_checksum, ddk, ddp, &blk);
933 snprintf_blkptr(blkbuf, sizeof (blkbuf), &blk);
934 (void) printf("index %llx refcnt %llu %s %s\n",
935 (u_longlong_t)index, (u_longlong_t)ddp->ddp_refcnt,
941 dump_dedup_ratio(const ddt_stat_t *dds)
943 double rL, rP, rD, D, dedup, compress, copies;
945 if (dds->dds_blocks == 0)
948 rL = (double)dds->dds_ref_lsize;
949 rP = (double)dds->dds_ref_psize;
950 rD = (double)dds->dds_ref_dsize;
951 D = (double)dds->dds_dsize;
957 (void) printf("dedup = %.2f, compress = %.2f, copies = %.2f, "
958 "dedup * compress / copies = %.2f\n\n",
959 dedup, compress, copies, dedup * compress / copies);
963 dump_ddt(ddt_t *ddt, enum ddt_type type, enum ddt_class class)
965 char name[DDT_NAMELEN];
968 dmu_object_info_t doi;
969 uint64_t count, dspace, mspace;
972 error = ddt_object_info(ddt, type, class, &doi);
978 error = ddt_object_count(ddt, type, class, &count);
983 dspace = doi.doi_physical_blocks_512 << 9;
984 mspace = doi.doi_fill_count * doi.doi_data_block_size;
986 ddt_object_name(ddt, type, class, name);
988 (void) printf("%s: %llu entries, size %llu on disk, %llu in core\n",
991 (u_longlong_t)(dspace / count),
992 (u_longlong_t)(mspace / count));
994 if (dump_opt['D'] < 3)
997 zpool_dump_ddt(NULL, &ddt->ddt_histogram[type][class]);
999 if (dump_opt['D'] < 4)
1002 if (dump_opt['D'] < 5 && class == DDT_CLASS_UNIQUE)
1005 (void) printf("%s contents:\n\n", name);
1007 while ((error = ddt_object_walk(ddt, type, class, &walk, &dde)) == 0)
1008 dump_dde(ddt, &dde, walk);
1010 ASSERT(error == ENOENT);
1012 (void) printf("\n");
1016 dump_all_ddts(spa_t *spa)
1018 ddt_histogram_t ddh_total;
1019 ddt_stat_t dds_total;
1020 enum zio_checksum c;
1022 enum ddt_class class;
1024 bzero(&ddh_total, sizeof (ddt_histogram_t));
1025 bzero(&dds_total, sizeof (ddt_stat_t));
1027 for (c = 0; c < ZIO_CHECKSUM_FUNCTIONS; c++) {
1028 ddt_t *ddt = spa->spa_ddt[c];
1029 for (type = 0; type < DDT_TYPES; type++) {
1030 for (class = 0; class < DDT_CLASSES;
1032 dump_ddt(ddt, type, class);
1037 ddt_get_dedup_stats(spa, &dds_total);
1039 if (dds_total.dds_blocks == 0) {
1040 (void) printf("All DDTs are empty\n");
1044 (void) printf("\n");
1046 if (dump_opt['D'] > 1) {
1047 (void) printf("DDT histogram (aggregated over all DDTs):\n");
1048 ddt_get_dedup_histogram(spa, &ddh_total);
1049 zpool_dump_ddt(&dds_total, &ddh_total);
1052 dump_dedup_ratio(&dds_total);
1056 dump_dtl_seg(void *arg, uint64_t start, uint64_t size)
1060 (void) printf("%s [%llu,%llu) length %llu\n",
1062 (u_longlong_t)start,
1063 (u_longlong_t)(start + size),
1064 (u_longlong_t)(size));
1068 dump_dtl(vdev_t *vd, int indent)
1070 spa_t *spa = vd->vdev_spa;
1072 char *name[DTL_TYPES] = { "missing", "partial", "scrub", "outage" };
1076 spa_vdev_state_enter(spa, SCL_NONE);
1077 required = vdev_dtl_required(vd);
1078 (void) spa_vdev_state_exit(spa, NULL, 0);
1081 (void) printf("\nDirty time logs:\n\n");
1083 (void) printf("\t%*s%s [%s]\n", indent, "",
1084 vd->vdev_path ? vd->vdev_path :
1085 vd->vdev_parent ? vd->vdev_ops->vdev_op_type : spa_name(spa),
1086 required ? "DTL-required" : "DTL-expendable");
1088 for (t = 0; t < DTL_TYPES; t++) {
1089 range_tree_t *rt = vd->vdev_dtl[t];
1090 if (range_tree_space(rt) == 0)
1092 (void) snprintf(prefix, sizeof (prefix), "\t%*s%s",
1093 indent + 2, "", name[t]);
1094 mutex_enter(rt->rt_lock);
1095 range_tree_walk(rt, dump_dtl_seg, prefix);
1096 mutex_exit(rt->rt_lock);
1097 if (dump_opt['d'] > 5 && vd->vdev_children == 0)
1098 dump_spacemap(spa->spa_meta_objset,
1102 for (c = 0; c < vd->vdev_children; c++)
1103 dump_dtl(vd->vdev_child[c], indent + 4);
1107 dump_history(spa_t *spa)
1109 nvlist_t **events = NULL;
1111 uint64_t resid, len, off = 0;
1117 char internalstr[MAXPATHLEN];
1120 if ((buf = malloc(SPA_OLD_MAXBLOCKSIZE)) == NULL) {
1121 (void) fprintf(stderr, "%s: unable to allocate I/O buffer\n",
1127 len = SPA_OLD_MAXBLOCKSIZE;
1129 if ((error = spa_history_get(spa, &off, &len, buf)) != 0) {
1130 (void) fprintf(stderr, "Unable to read history: "
1131 "error %d\n", error);
1136 if (zpool_history_unpack(buf, len, &resid, &events, &num) != 0)
1142 (void) printf("\nHistory:\n");
1143 for (i = 0; i < num; i++) {
1144 uint64_t time, txg, ievent;
1146 boolean_t printed = B_FALSE;
1148 if (nvlist_lookup_uint64(events[i], ZPOOL_HIST_TIME,
1151 if (nvlist_lookup_string(events[i], ZPOOL_HIST_CMD,
1153 if (nvlist_lookup_uint64(events[i],
1154 ZPOOL_HIST_INT_EVENT, &ievent) != 0)
1156 verify(nvlist_lookup_uint64(events[i],
1157 ZPOOL_HIST_TXG, &txg) == 0);
1158 verify(nvlist_lookup_string(events[i],
1159 ZPOOL_HIST_INT_STR, &intstr) == 0);
1160 if (ievent >= ZFS_NUM_LEGACY_HISTORY_EVENTS)
1163 (void) snprintf(internalstr,
1164 sizeof (internalstr),
1165 "[internal %s txg:%lld] %s",
1166 zfs_history_event_names[ievent],
1167 (longlong_t)txg, intstr);
1171 (void) localtime_r(&tsec, &t);
1172 (void) strftime(tbuf, sizeof (tbuf), "%F.%T", &t);
1173 (void) printf("%s %s\n", tbuf, cmd);
1177 if (dump_opt['h'] > 1) {
1179 (void) printf("unrecognized record:\n");
1180 dump_nvlist(events[i], 2);
1188 dump_dnode(objset_t *os, uint64_t object, void *data, size_t size)
1193 blkid2offset(const dnode_phys_t *dnp, const blkptr_t *bp,
1194 const zbookmark_phys_t *zb)
1197 ASSERT(zb->zb_level < 0);
1198 if (zb->zb_object == 0)
1199 return (zb->zb_blkid);
1200 return (zb->zb_blkid * BP_GET_LSIZE(bp));
1203 ASSERT(zb->zb_level >= 0);
1205 return ((zb->zb_blkid <<
1206 (zb->zb_level * (dnp->dn_indblkshift - SPA_BLKPTRSHIFT))) *
1207 dnp->dn_datablkszsec << SPA_MINBLOCKSHIFT);
1211 snprintf_blkptr_compact(char *blkbuf, size_t buflen, const blkptr_t *bp)
1213 const dva_t *dva = bp->blk_dva;
1214 int ndvas = dump_opt['d'] > 5 ? BP_GET_NDVAS(bp) : 1;
1217 if (dump_opt['b'] >= 6) {
1218 snprintf_blkptr(blkbuf, buflen, bp);
1222 if (BP_IS_EMBEDDED(bp)) {
1223 (void) sprintf(blkbuf,
1224 "EMBEDDED et=%u %llxL/%llxP B=%llu",
1225 (int)BPE_GET_ETYPE(bp),
1226 (u_longlong_t)BPE_GET_LSIZE(bp),
1227 (u_longlong_t)BPE_GET_PSIZE(bp),
1228 (u_longlong_t)bp->blk_birth);
1234 for (i = 0; i < ndvas; i++)
1235 (void) snprintf(blkbuf + strlen(blkbuf),
1236 buflen - strlen(blkbuf), "%llu:%llx:%llx ",
1237 (u_longlong_t)DVA_GET_VDEV(&dva[i]),
1238 (u_longlong_t)DVA_GET_OFFSET(&dva[i]),
1239 (u_longlong_t)DVA_GET_ASIZE(&dva[i]));
1241 if (BP_IS_HOLE(bp)) {
1242 (void) snprintf(blkbuf + strlen(blkbuf),
1243 buflen - strlen(blkbuf),
1245 (u_longlong_t)BP_GET_LSIZE(bp),
1246 (u_longlong_t)bp->blk_birth);
1248 (void) snprintf(blkbuf + strlen(blkbuf),
1249 buflen - strlen(blkbuf),
1250 "%llxL/%llxP F=%llu B=%llu/%llu",
1251 (u_longlong_t)BP_GET_LSIZE(bp),
1252 (u_longlong_t)BP_GET_PSIZE(bp),
1253 (u_longlong_t)BP_GET_FILL(bp),
1254 (u_longlong_t)bp->blk_birth,
1255 (u_longlong_t)BP_PHYSICAL_BIRTH(bp));
1260 print_indirect(blkptr_t *bp, const zbookmark_phys_t *zb,
1261 const dnode_phys_t *dnp)
1263 char blkbuf[BP_SPRINTF_LEN];
1266 if (!BP_IS_EMBEDDED(bp)) {
1267 ASSERT3U(BP_GET_TYPE(bp), ==, dnp->dn_type);
1268 ASSERT3U(BP_GET_LEVEL(bp), ==, zb->zb_level);
1271 (void) printf("%16llx ", (u_longlong_t)blkid2offset(dnp, bp, zb));
1273 ASSERT(zb->zb_level >= 0);
1275 for (l = dnp->dn_nlevels - 1; l >= -1; l--) {
1276 if (l == zb->zb_level) {
1277 (void) printf("L%llx", (u_longlong_t)zb->zb_level);
1283 snprintf_blkptr_compact(blkbuf, sizeof (blkbuf), bp);
1284 (void) printf("%s\n", blkbuf);
1288 visit_indirect(spa_t *spa, const dnode_phys_t *dnp,
1289 blkptr_t *bp, const zbookmark_phys_t *zb)
1293 if (bp->blk_birth == 0)
1296 print_indirect(bp, zb, dnp);
1298 if (BP_GET_LEVEL(bp) > 0 && !BP_IS_HOLE(bp)) {
1299 arc_flags_t flags = ARC_FLAG_WAIT;
1302 int epb = BP_GET_LSIZE(bp) >> SPA_BLKPTRSHIFT;
1306 err = arc_read(NULL, spa, bp, arc_getbuf_func, &buf,
1307 ZIO_PRIORITY_ASYNC_READ, ZIO_FLAG_CANFAIL, &flags, zb);
1310 ASSERT(buf->b_data);
1312 /* recursively visit blocks below this */
1314 for (i = 0; i < epb; i++, cbp++) {
1315 zbookmark_phys_t czb;
1317 SET_BOOKMARK(&czb, zb->zb_objset, zb->zb_object,
1319 zb->zb_blkid * epb + i);
1320 err = visit_indirect(spa, dnp, cbp, &czb);
1323 fill += BP_GET_FILL(cbp);
1326 ASSERT3U(fill, ==, BP_GET_FILL(bp));
1327 arc_buf_destroy(buf, &buf);
1335 dump_indirect(dnode_t *dn)
1337 dnode_phys_t *dnp = dn->dn_phys;
1339 zbookmark_phys_t czb;
1341 (void) printf("Indirect blocks:\n");
1343 SET_BOOKMARK(&czb, dmu_objset_id(dn->dn_objset),
1344 dn->dn_object, dnp->dn_nlevels - 1, 0);
1345 for (j = 0; j < dnp->dn_nblkptr; j++) {
1347 (void) visit_indirect(dmu_objset_spa(dn->dn_objset), dnp,
1348 &dnp->dn_blkptr[j], &czb);
1351 (void) printf("\n");
1356 dump_dsl_dir(objset_t *os, uint64_t object, void *data, size_t size)
1358 dsl_dir_phys_t *dd = data;
1365 ASSERT3U(size, >=, sizeof (dsl_dir_phys_t));
1367 crtime = dd->dd_creation_time;
1368 (void) printf("\t\tcreation_time = %s", ctime(&crtime));
1369 (void) printf("\t\thead_dataset_obj = %llu\n",
1370 (u_longlong_t)dd->dd_head_dataset_obj);
1371 (void) printf("\t\tparent_dir_obj = %llu\n",
1372 (u_longlong_t)dd->dd_parent_obj);
1373 (void) printf("\t\torigin_obj = %llu\n",
1374 (u_longlong_t)dd->dd_origin_obj);
1375 (void) printf("\t\tchild_dir_zapobj = %llu\n",
1376 (u_longlong_t)dd->dd_child_dir_zapobj);
1377 zdb_nicenum(dd->dd_used_bytes, nice);
1378 (void) printf("\t\tused_bytes = %s\n", nice);
1379 zdb_nicenum(dd->dd_compressed_bytes, nice);
1380 (void) printf("\t\tcompressed_bytes = %s\n", nice);
1381 zdb_nicenum(dd->dd_uncompressed_bytes, nice);
1382 (void) printf("\t\tuncompressed_bytes = %s\n", nice);
1383 zdb_nicenum(dd->dd_quota, nice);
1384 (void) printf("\t\tquota = %s\n", nice);
1385 zdb_nicenum(dd->dd_reserved, nice);
1386 (void) printf("\t\treserved = %s\n", nice);
1387 (void) printf("\t\tprops_zapobj = %llu\n",
1388 (u_longlong_t)dd->dd_props_zapobj);
1389 (void) printf("\t\tdeleg_zapobj = %llu\n",
1390 (u_longlong_t)dd->dd_deleg_zapobj);
1391 (void) printf("\t\tflags = %llx\n",
1392 (u_longlong_t)dd->dd_flags);
1395 zdb_nicenum(dd->dd_used_breakdown[DD_USED_ ## which], nice); \
1396 (void) printf("\t\tused_breakdown[" #which "] = %s\n", nice)
1407 dump_dsl_dataset(objset_t *os, uint64_t object, void *data, size_t size)
1409 dsl_dataset_phys_t *ds = data;
1411 char used[32], compressed[32], uncompressed[32], unique[32];
1412 char blkbuf[BP_SPRINTF_LEN];
1417 ASSERT(size == sizeof (*ds));
1418 crtime = ds->ds_creation_time;
1419 zdb_nicenum(ds->ds_referenced_bytes, used);
1420 zdb_nicenum(ds->ds_compressed_bytes, compressed);
1421 zdb_nicenum(ds->ds_uncompressed_bytes, uncompressed);
1422 zdb_nicenum(ds->ds_unique_bytes, unique);
1423 snprintf_blkptr(blkbuf, sizeof (blkbuf), &ds->ds_bp);
1425 (void) printf("\t\tdir_obj = %llu\n",
1426 (u_longlong_t)ds->ds_dir_obj);
1427 (void) printf("\t\tprev_snap_obj = %llu\n",
1428 (u_longlong_t)ds->ds_prev_snap_obj);
1429 (void) printf("\t\tprev_snap_txg = %llu\n",
1430 (u_longlong_t)ds->ds_prev_snap_txg);
1431 (void) printf("\t\tnext_snap_obj = %llu\n",
1432 (u_longlong_t)ds->ds_next_snap_obj);
1433 (void) printf("\t\tsnapnames_zapobj = %llu\n",
1434 (u_longlong_t)ds->ds_snapnames_zapobj);
1435 (void) printf("\t\tnum_children = %llu\n",
1436 (u_longlong_t)ds->ds_num_children);
1437 (void) printf("\t\tuserrefs_obj = %llu\n",
1438 (u_longlong_t)ds->ds_userrefs_obj);
1439 (void) printf("\t\tcreation_time = %s", ctime(&crtime));
1440 (void) printf("\t\tcreation_txg = %llu\n",
1441 (u_longlong_t)ds->ds_creation_txg);
1442 (void) printf("\t\tdeadlist_obj = %llu\n",
1443 (u_longlong_t)ds->ds_deadlist_obj);
1444 (void) printf("\t\tused_bytes = %s\n", used);
1445 (void) printf("\t\tcompressed_bytes = %s\n", compressed);
1446 (void) printf("\t\tuncompressed_bytes = %s\n", uncompressed);
1447 (void) printf("\t\tunique = %s\n", unique);
1448 (void) printf("\t\tfsid_guid = %llu\n",
1449 (u_longlong_t)ds->ds_fsid_guid);
1450 (void) printf("\t\tguid = %llu\n",
1451 (u_longlong_t)ds->ds_guid);
1452 (void) printf("\t\tflags = %llx\n",
1453 (u_longlong_t)ds->ds_flags);
1454 (void) printf("\t\tnext_clones_obj = %llu\n",
1455 (u_longlong_t)ds->ds_next_clones_obj);
1456 (void) printf("\t\tprops_obj = %llu\n",
1457 (u_longlong_t)ds->ds_props_obj);
1458 (void) printf("\t\tbp = %s\n", blkbuf);
1463 dump_bptree_cb(void *arg, const blkptr_t *bp, dmu_tx_t *tx)
1465 char blkbuf[BP_SPRINTF_LEN];
1467 if (bp->blk_birth != 0) {
1468 snprintf_blkptr(blkbuf, sizeof (blkbuf), bp);
1469 (void) printf("\t%s\n", blkbuf);
1475 dump_bptree(objset_t *os, uint64_t obj, char *name)
1481 if (dump_opt['d'] < 3)
1484 VERIFY3U(0, ==, dmu_bonus_hold(os, obj, FTAG, &db));
1486 zdb_nicenum(bt->bt_bytes, bytes);
1487 (void) printf("\n %s: %llu datasets, %s\n",
1488 name, (unsigned long long)(bt->bt_end - bt->bt_begin), bytes);
1489 dmu_buf_rele(db, FTAG);
1491 if (dump_opt['d'] < 5)
1494 (void) printf("\n");
1496 (void) bptree_iterate(os, obj, B_FALSE, dump_bptree_cb, NULL, NULL);
1501 dump_bpobj_cb(void *arg, const blkptr_t *bp, dmu_tx_t *tx)
1503 char blkbuf[BP_SPRINTF_LEN];
1505 ASSERT(bp->blk_birth != 0);
1506 snprintf_blkptr_compact(blkbuf, sizeof (blkbuf), bp);
1507 (void) printf("\t%s\n", blkbuf);
1512 dump_full_bpobj(bpobj_t *bpo, char *name, int indent)
1519 if (dump_opt['d'] < 3)
1522 zdb_nicenum(bpo->bpo_phys->bpo_bytes, bytes);
1523 if (bpo->bpo_havesubobj && bpo->bpo_phys->bpo_subobjs != 0) {
1524 zdb_nicenum(bpo->bpo_phys->bpo_comp, comp);
1525 zdb_nicenum(bpo->bpo_phys->bpo_uncomp, uncomp);
1526 (void) printf(" %*s: object %llu, %llu local blkptrs, "
1527 "%llu subobjs in object, %llu, %s (%s/%s comp)\n",
1529 (u_longlong_t)bpo->bpo_object,
1530 (u_longlong_t)bpo->bpo_phys->bpo_num_blkptrs,
1531 (u_longlong_t)bpo->bpo_phys->bpo_num_subobjs,
1532 (u_longlong_t)bpo->bpo_phys->bpo_subobjs,
1533 bytes, comp, uncomp);
1535 for (i = 0; i < bpo->bpo_phys->bpo_num_subobjs; i++) {
1539 VERIFY0(dmu_read(bpo->bpo_os,
1540 bpo->bpo_phys->bpo_subobjs,
1541 i * sizeof (subobj), sizeof (subobj), &subobj, 0));
1542 error = bpobj_open(&subbpo, bpo->bpo_os, subobj);
1544 (void) printf("ERROR %u while trying to open "
1546 error, (u_longlong_t)subobj);
1549 dump_full_bpobj(&subbpo, "subobj", indent + 1);
1550 bpobj_close(&subbpo);
1553 (void) printf(" %*s: object %llu, %llu blkptrs, %s\n",
1555 (u_longlong_t)bpo->bpo_object,
1556 (u_longlong_t)bpo->bpo_phys->bpo_num_blkptrs,
1560 if (dump_opt['d'] < 5)
1565 (void) bpobj_iterate_nofree(bpo, dump_bpobj_cb, NULL, NULL);
1566 (void) printf("\n");
1571 dump_deadlist(dsl_deadlist_t *dl)
1573 dsl_deadlist_entry_t *dle;
1579 if (dump_opt['d'] < 3)
1582 if (dl->dl_oldfmt) {
1583 dump_full_bpobj(&dl->dl_bpobj, "old-format deadlist", 0);
1587 zdb_nicenum(dl->dl_phys->dl_used, bytes);
1588 zdb_nicenum(dl->dl_phys->dl_comp, comp);
1589 zdb_nicenum(dl->dl_phys->dl_uncomp, uncomp);
1590 (void) printf("\n Deadlist: %s (%s/%s comp)\n",
1591 bytes, comp, uncomp);
1593 if (dump_opt['d'] < 4)
1596 (void) printf("\n");
1598 /* force the tree to be loaded */
1599 dsl_deadlist_space_range(dl, 0, UINT64_MAX, &unused, &unused, &unused);
1601 for (dle = avl_first(&dl->dl_tree); dle;
1602 dle = AVL_NEXT(&dl->dl_tree, dle)) {
1603 if (dump_opt['d'] >= 5) {
1605 (void) snprintf(buf, sizeof (buf),
1606 "mintxg %llu -> obj %llu",
1607 (longlong_t)dle->dle_mintxg,
1608 (longlong_t)dle->dle_bpobj.bpo_object);
1610 dump_full_bpobj(&dle->dle_bpobj, buf, 0);
1612 (void) printf("mintxg %llu -> obj %llu\n",
1613 (longlong_t)dle->dle_mintxg,
1614 (longlong_t)dle->dle_bpobj.bpo_object);
1620 static avl_tree_t idx_tree;
1621 static avl_tree_t domain_tree;
1622 static boolean_t fuid_table_loaded;
1623 static objset_t *sa_os = NULL;
1624 static sa_attr_type_t *sa_attr_table = NULL;
1627 open_objset(const char *path, dmu_objset_type_t type, void *tag, objset_t **osp)
1630 uint64_t sa_attrs = 0;
1631 uint64_t version = 0;
1633 VERIFY3P(sa_os, ==, NULL);
1634 err = dmu_objset_own(path, type, B_TRUE, tag, osp);
1636 (void) fprintf(stderr, "failed to own dataset '%s': %s\n", path,
1641 if (dmu_objset_type(*osp) == DMU_OST_ZFS) {
1642 (void) zap_lookup(*osp, MASTER_NODE_OBJ, ZPL_VERSION_STR,
1644 if (version >= ZPL_VERSION_SA) {
1645 (void) zap_lookup(*osp, MASTER_NODE_OBJ, ZFS_SA_ATTRS,
1648 err = sa_setup(*osp, sa_attrs, zfs_attr_table, ZPL_END,
1651 (void) fprintf(stderr, "sa_setup failed: %s\n",
1653 dmu_objset_disown(*osp, tag);
1663 close_objset(objset_t *os, void *tag)
1665 VERIFY3P(os, ==, sa_os);
1666 if (os->os_sa != NULL)
1668 dmu_objset_disown(os, tag);
1669 sa_attr_table = NULL;
1674 fuid_table_destroy(void)
1676 if (fuid_table_loaded) {
1677 zfs_fuid_table_destroy(&idx_tree, &domain_tree);
1678 fuid_table_loaded = B_FALSE;
1683 * print uid or gid information.
1684 * For normal POSIX id just the id is printed in decimal format.
1685 * For CIFS files with FUID the fuid is printed in hex followed by
1686 * the domain-rid string.
1689 print_idstr(uint64_t id, const char *id_type)
1691 if (FUID_INDEX(id)) {
1694 domain = zfs_fuid_idx_domain(&idx_tree, FUID_INDEX(id));
1695 (void) printf("\t%s %llx [%s-%d]\n", id_type,
1696 (u_longlong_t)id, domain, (int)FUID_RID(id));
1698 (void) printf("\t%s %llu\n", id_type, (u_longlong_t)id);
1704 dump_uidgid(objset_t *os, uint64_t uid, uint64_t gid)
1706 uint32_t uid_idx, gid_idx;
1708 uid_idx = FUID_INDEX(uid);
1709 gid_idx = FUID_INDEX(gid);
1711 /* Load domain table, if not already loaded */
1712 if (!fuid_table_loaded && (uid_idx || gid_idx)) {
1715 /* first find the fuid object. It lives in the master node */
1716 VERIFY(zap_lookup(os, MASTER_NODE_OBJ, ZFS_FUID_TABLES,
1717 8, 1, &fuid_obj) == 0);
1718 zfs_fuid_avl_tree_create(&idx_tree, &domain_tree);
1719 (void) zfs_fuid_table_load(os, fuid_obj,
1720 &idx_tree, &domain_tree);
1721 fuid_table_loaded = B_TRUE;
1724 print_idstr(uid, "uid");
1725 print_idstr(gid, "gid");
1729 dump_znode_sa_xattr(sa_handle_t *hdl)
1732 nvpair_t *elem = NULL;
1733 int sa_xattr_size = 0;
1734 int sa_xattr_entries = 0;
1736 char *sa_xattr_packed;
1738 error = sa_size(hdl, sa_attr_table[ZPL_DXATTR], &sa_xattr_size);
1739 if (error || sa_xattr_size == 0)
1742 sa_xattr_packed = malloc(sa_xattr_size);
1743 if (sa_xattr_packed == NULL)
1746 error = sa_lookup(hdl, sa_attr_table[ZPL_DXATTR],
1747 sa_xattr_packed, sa_xattr_size);
1749 free(sa_xattr_packed);
1753 error = nvlist_unpack(sa_xattr_packed, sa_xattr_size, &sa_xattr, 0);
1755 free(sa_xattr_packed);
1759 while ((elem = nvlist_next_nvpair(sa_xattr, elem)) != NULL)
1762 (void) printf("\tSA xattrs: %d bytes, %d entries\n\n",
1763 sa_xattr_size, sa_xattr_entries);
1764 while ((elem = nvlist_next_nvpair(sa_xattr, elem)) != NULL) {
1768 (void) printf("\t\t%s = ", nvpair_name(elem));
1769 nvpair_value_byte_array(elem, &value, &cnt);
1770 for (idx = 0; idx < cnt; ++idx) {
1771 if (isprint(value[idx]))
1772 (void) putchar(value[idx]);
1774 (void) printf("\\%3.3o", value[idx]);
1776 (void) putchar('\n');
1779 nvlist_free(sa_xattr);
1780 free(sa_xattr_packed);
1785 dump_znode(objset_t *os, uint64_t object, void *data, size_t size)
1787 char path[MAXPATHLEN * 2]; /* allow for xattr and failure prefix */
1789 uint64_t xattr, rdev, gen;
1790 uint64_t uid, gid, mode, fsize, parent, links;
1792 uint64_t acctm[2], modtm[2], chgtm[2], crtm[2];
1793 time_t z_crtime, z_atime, z_mtime, z_ctime;
1794 sa_bulk_attr_t bulk[12];
1798 VERIFY3P(os, ==, sa_os);
1799 if (sa_handle_get(os, object, NULL, SA_HDL_PRIVATE, &hdl)) {
1800 (void) printf("Failed to get handle for SA znode\n");
1804 SA_ADD_BULK_ATTR(bulk, idx, sa_attr_table[ZPL_UID], NULL, &uid, 8);
1805 SA_ADD_BULK_ATTR(bulk, idx, sa_attr_table[ZPL_GID], NULL, &gid, 8);
1806 SA_ADD_BULK_ATTR(bulk, idx, sa_attr_table[ZPL_LINKS], NULL,
1808 SA_ADD_BULK_ATTR(bulk, idx, sa_attr_table[ZPL_GEN], NULL, &gen, 8);
1809 SA_ADD_BULK_ATTR(bulk, idx, sa_attr_table[ZPL_MODE], NULL,
1811 SA_ADD_BULK_ATTR(bulk, idx, sa_attr_table[ZPL_PARENT],
1813 SA_ADD_BULK_ATTR(bulk, idx, sa_attr_table[ZPL_SIZE], NULL,
1815 SA_ADD_BULK_ATTR(bulk, idx, sa_attr_table[ZPL_ATIME], NULL,
1817 SA_ADD_BULK_ATTR(bulk, idx, sa_attr_table[ZPL_MTIME], NULL,
1819 SA_ADD_BULK_ATTR(bulk, idx, sa_attr_table[ZPL_CRTIME], NULL,
1821 SA_ADD_BULK_ATTR(bulk, idx, sa_attr_table[ZPL_CTIME], NULL,
1823 SA_ADD_BULK_ATTR(bulk, idx, sa_attr_table[ZPL_FLAGS], NULL,
1826 if (sa_bulk_lookup(hdl, bulk, idx)) {
1827 (void) sa_handle_destroy(hdl);
1831 z_crtime = (time_t)crtm[0];
1832 z_atime = (time_t)acctm[0];
1833 z_mtime = (time_t)modtm[0];
1834 z_ctime = (time_t)chgtm[0];
1836 if (dump_opt['d'] > 4) {
1837 error = zfs_obj_to_path(os, object, path, sizeof (path));
1839 (void) snprintf(path, sizeof (path),
1840 "\?\?\?<object#%llu>", (u_longlong_t)object);
1842 (void) printf("\tpath %s\n", path);
1844 dump_uidgid(os, uid, gid);
1845 (void) printf("\tatime %s", ctime(&z_atime));
1846 (void) printf("\tmtime %s", ctime(&z_mtime));
1847 (void) printf("\tctime %s", ctime(&z_ctime));
1848 (void) printf("\tcrtime %s", ctime(&z_crtime));
1849 (void) printf("\tgen %llu\n", (u_longlong_t)gen);
1850 (void) printf("\tmode %llo\n", (u_longlong_t)mode);
1851 (void) printf("\tsize %llu\n", (u_longlong_t)fsize);
1852 (void) printf("\tparent %llu\n", (u_longlong_t)parent);
1853 (void) printf("\tlinks %llu\n", (u_longlong_t)links);
1854 (void) printf("\tpflags %llx\n", (u_longlong_t)pflags);
1855 if (sa_lookup(hdl, sa_attr_table[ZPL_XATTR], &xattr,
1856 sizeof (uint64_t)) == 0)
1857 (void) printf("\txattr %llu\n", (u_longlong_t)xattr);
1858 if (sa_lookup(hdl, sa_attr_table[ZPL_RDEV], &rdev,
1859 sizeof (uint64_t)) == 0)
1860 (void) printf("\trdev 0x%016llx\n", (u_longlong_t)rdev);
1861 dump_znode_sa_xattr(hdl);
1862 sa_handle_destroy(hdl);
1867 dump_acl(objset_t *os, uint64_t object, void *data, size_t size)
1873 dump_dmu_objset(objset_t *os, uint64_t object, void *data, size_t size)
1877 static object_viewer_t *object_viewer[DMU_OT_NUMTYPES + 1] = {
1878 dump_none, /* unallocated */
1879 dump_zap, /* object directory */
1880 dump_uint64, /* object array */
1881 dump_none, /* packed nvlist */
1882 dump_packed_nvlist, /* packed nvlist size */
1883 dump_none, /* bpobj */
1884 dump_bpobj, /* bpobj header */
1885 dump_none, /* SPA space map header */
1886 dump_none, /* SPA space map */
1887 dump_none, /* ZIL intent log */
1888 dump_dnode, /* DMU dnode */
1889 dump_dmu_objset, /* DMU objset */
1890 dump_dsl_dir, /* DSL directory */
1891 dump_zap, /* DSL directory child map */
1892 dump_zap, /* DSL dataset snap map */
1893 dump_zap, /* DSL props */
1894 dump_dsl_dataset, /* DSL dataset */
1895 dump_znode, /* ZFS znode */
1896 dump_acl, /* ZFS V0 ACL */
1897 dump_uint8, /* ZFS plain file */
1898 dump_zpldir, /* ZFS directory */
1899 dump_zap, /* ZFS master node */
1900 dump_zap, /* ZFS delete queue */
1901 dump_uint8, /* zvol object */
1902 dump_zap, /* zvol prop */
1903 dump_uint8, /* other uint8[] */
1904 dump_uint64, /* other uint64[] */
1905 dump_zap, /* other ZAP */
1906 dump_zap, /* persistent error log */
1907 dump_uint8, /* SPA history */
1908 dump_history_offsets, /* SPA history offsets */
1909 dump_zap, /* Pool properties */
1910 dump_zap, /* DSL permissions */
1911 dump_acl, /* ZFS ACL */
1912 dump_uint8, /* ZFS SYSACL */
1913 dump_none, /* FUID nvlist */
1914 dump_packed_nvlist, /* FUID nvlist size */
1915 dump_zap, /* DSL dataset next clones */
1916 dump_zap, /* DSL scrub queue */
1917 dump_zap, /* ZFS user/group used */
1918 dump_zap, /* ZFS user/group quota */
1919 dump_zap, /* snapshot refcount tags */
1920 dump_ddt_zap, /* DDT ZAP object */
1921 dump_zap, /* DDT statistics */
1922 dump_znode, /* SA object */
1923 dump_zap, /* SA Master Node */
1924 dump_sa_attrs, /* SA attribute registration */
1925 dump_sa_layouts, /* SA attribute layouts */
1926 dump_zap, /* DSL scrub translations */
1927 dump_none, /* fake dedup BP */
1928 dump_zap, /* deadlist */
1929 dump_none, /* deadlist hdr */
1930 dump_zap, /* dsl clones */
1931 dump_bpobj_subobjs, /* bpobj subobjs */
1932 dump_unknown, /* Unknown type, must be last */
1936 dump_object(objset_t *os, uint64_t object, int verbosity, int *print_header,
1937 uint64_t *dnode_slots_used)
1939 dmu_buf_t *db = NULL;
1940 dmu_object_info_t doi;
1944 char iblk[32], dblk[32], lsize[32], asize[32], fill[32], dnsize[32];
1945 char bonus_size[32];
1949 if (*print_header) {
1950 (void) printf("\n%10s %3s %5s %5s %5s %6s %5s %6s %s\n",
1951 "Object", "lvl", "iblk", "dblk", "dsize", "dnsize",
1952 "lsize", "%full", "type");
1957 dn = DMU_META_DNODE(os);
1959 error = dmu_bonus_hold(os, object, FTAG, &db);
1961 fatal("dmu_bonus_hold(%llu) failed, errno %u",
1963 bonus = db->db_data;
1964 bsize = db->db_size;
1965 dn = DB_DNODE((dmu_buf_impl_t *)db);
1967 dmu_object_info_from_dnode(dn, &doi);
1969 if (dnode_slots_used)
1970 *dnode_slots_used = doi.doi_dnodesize / DNODE_MIN_SIZE;
1972 zdb_nicenum(doi.doi_metadata_block_size, iblk);
1973 zdb_nicenum(doi.doi_data_block_size, dblk);
1974 zdb_nicenum(doi.doi_max_offset, lsize);
1975 zdb_nicenum(doi.doi_physical_blocks_512 << 9, asize);
1976 zdb_nicenum(doi.doi_bonus_size, bonus_size);
1977 zdb_nicenum(doi.doi_dnodesize, dnsize);
1978 (void) sprintf(fill, "%6.2f", 100.0 * doi.doi_fill_count *
1979 doi.doi_data_block_size / (object == 0 ? DNODES_PER_BLOCK : 1) /
1980 doi.doi_max_offset);
1984 if (doi.doi_checksum != ZIO_CHECKSUM_INHERIT || verbosity >= 6) {
1985 (void) snprintf(aux + strlen(aux), sizeof (aux), " (K=%s)",
1986 ZDB_CHECKSUM_NAME(doi.doi_checksum));
1989 if (doi.doi_compress != ZIO_COMPRESS_INHERIT || verbosity >= 6) {
1990 (void) snprintf(aux + strlen(aux), sizeof (aux), " (Z=%s)",
1991 ZDB_COMPRESS_NAME(doi.doi_compress));
1994 (void) printf("%10lld %3u %5s %5s %5s %6s %5s %6s %s%s\n",
1995 (u_longlong_t)object, doi.doi_indirection, iblk, dblk,
1996 asize, dnsize, lsize, fill, zdb_ot_name(doi.doi_type), aux);
1998 if (doi.doi_bonus_type != DMU_OT_NONE && verbosity > 3) {
1999 (void) printf("%10s %3s %5s %5s %5s %5s %5s %6s %s\n",
2000 "", "", "", "", "", "", bonus_size, "bonus",
2001 zdb_ot_name(doi.doi_bonus_type));
2004 if (verbosity >= 4) {
2005 (void) printf("\tdnode flags: %s%s%s%s\n",
2006 (dn->dn_phys->dn_flags & DNODE_FLAG_USED_BYTES) ?
2008 (dn->dn_phys->dn_flags & DNODE_FLAG_USERUSED_ACCOUNTED) ?
2009 "USERUSED_ACCOUNTED " : "",
2010 (dn->dn_phys->dn_flags & DNODE_FLAG_USEROBJUSED_ACCOUNTED) ?
2011 "USEROBJUSED_ACCOUNTED " : "",
2012 (dn->dn_phys->dn_flags & DNODE_FLAG_SPILL_BLKPTR) ?
2013 "SPILL_BLKPTR" : "");
2014 (void) printf("\tdnode maxblkid: %llu\n",
2015 (longlong_t)dn->dn_phys->dn_maxblkid);
2017 object_viewer[ZDB_OT_TYPE(doi.doi_bonus_type)](os, object,
2019 object_viewer[ZDB_OT_TYPE(doi.doi_type)](os, object, NULL, 0);
2026 if (verbosity >= 5) {
2028 * Report the list of segments that comprise the object.
2032 uint64_t blkfill = 1;
2035 if (dn->dn_type == DMU_OT_DNODE) {
2037 blkfill = DNODES_PER_BLOCK;
2042 error = dnode_next_offset(dn,
2043 0, &start, minlvl, blkfill, 0);
2047 error = dnode_next_offset(dn,
2048 DNODE_FIND_HOLE, &end, minlvl, blkfill, 0);
2049 zdb_nicenum(end - start, segsize);
2050 (void) printf("\t\tsegment [%016llx, %016llx)"
2051 " size %5s\n", (u_longlong_t)start,
2052 (u_longlong_t)end, segsize);
2060 dmu_buf_rele(db, FTAG);
2063 static char *objset_types[DMU_OST_NUMTYPES] = {
2064 "NONE", "META", "ZPL", "ZVOL", "OTHER", "ANY" };
2067 dump_dir(objset_t *os)
2069 dmu_objset_stats_t dds;
2070 uint64_t object, object_count;
2071 uint64_t refdbytes, usedobjs, scratch;
2073 char blkbuf[BP_SPRINTF_LEN + 20];
2074 char osname[ZFS_MAX_DATASET_NAME_LEN];
2075 char *type = "UNKNOWN";
2076 int verbosity = dump_opt['d'];
2077 int print_header = 1;
2079 uint64_t total_slots_used = 0;
2080 uint64_t max_slot_used = 0;
2081 uint64_t dnode_slots;
2083 dsl_pool_config_enter(dmu_objset_pool(os), FTAG);
2084 dmu_objset_fast_stat(os, &dds);
2085 dsl_pool_config_exit(dmu_objset_pool(os), FTAG);
2087 if (dds.dds_type < DMU_OST_NUMTYPES)
2088 type = objset_types[dds.dds_type];
2090 if (dds.dds_type == DMU_OST_META) {
2091 dds.dds_creation_txg = TXG_INITIAL;
2092 usedobjs = BP_GET_FILL(os->os_rootbp);
2093 refdbytes = dsl_dir_phys(os->os_spa->spa_dsl_pool->dp_mos_dir)->
2096 dmu_objset_space(os, &refdbytes, &scratch, &usedobjs, &scratch);
2099 ASSERT3U(usedobjs, ==, BP_GET_FILL(os->os_rootbp));
2101 zdb_nicenum(refdbytes, numbuf);
2103 if (verbosity >= 4) {
2104 (void) snprintf(blkbuf, sizeof (blkbuf), ", rootbp ");
2105 (void) snprintf_blkptr(blkbuf + strlen(blkbuf),
2106 sizeof (blkbuf) - strlen(blkbuf), os->os_rootbp);
2111 dmu_objset_name(os, osname);
2113 (void) printf("Dataset %s [%s], ID %llu, cr_txg %llu, "
2114 "%s, %llu objects%s\n",
2115 osname, type, (u_longlong_t)dmu_objset_id(os),
2116 (u_longlong_t)dds.dds_creation_txg,
2117 numbuf, (u_longlong_t)usedobjs, blkbuf);
2119 if (zopt_objects != 0) {
2120 for (i = 0; i < zopt_objects; i++)
2121 dump_object(os, zopt_object[i], verbosity,
2122 &print_header, NULL);
2123 (void) printf("\n");
2127 if (dump_opt['i'] != 0 || verbosity >= 2)
2128 dump_intent_log(dmu_objset_zil(os));
2130 if (dmu_objset_ds(os) != NULL)
2131 dump_deadlist(&dmu_objset_ds(os)->ds_deadlist);
2136 if (BP_IS_HOLE(os->os_rootbp))
2139 dump_object(os, 0, verbosity, &print_header, NULL);
2141 if (DMU_USERUSED_DNODE(os) != NULL &&
2142 DMU_USERUSED_DNODE(os)->dn_type != 0) {
2143 dump_object(os, DMU_USERUSED_OBJECT, verbosity, &print_header,
2145 dump_object(os, DMU_GROUPUSED_OBJECT, verbosity, &print_header,
2150 while ((error = dmu_object_next(os, &object, B_FALSE, 0)) == 0) {
2151 dump_object(os, object, verbosity, &print_header, &dnode_slots);
2153 total_slots_used += dnode_slots;
2154 max_slot_used = object + dnode_slots - 1;
2157 ASSERT3U(object_count, ==, usedobjs);
2159 (void) printf("\n");
2161 (void) printf(" Dnode slots:\n");
2162 (void) printf("\tTotal used: %10llu\n",
2163 (u_longlong_t)total_slots_used);
2164 (void) printf("\tMax used: %10llu\n",
2165 (u_longlong_t)max_slot_used);
2166 (void) printf("\tPercent empty: %10lf\n",
2167 (double)(max_slot_used - total_slots_used)*100 /
2168 (double)max_slot_used);
2170 (void) printf("\n");
2172 if (error != ESRCH) {
2173 (void) fprintf(stderr, "dmu_object_next() = %d\n", error);
2179 dump_uberblock(uberblock_t *ub, const char *header, const char *footer)
2181 time_t timestamp = ub->ub_timestamp;
2183 (void) printf("%s", header ? header : "");
2184 (void) printf("\tmagic = %016llx\n", (u_longlong_t)ub->ub_magic);
2185 (void) printf("\tversion = %llu\n", (u_longlong_t)ub->ub_version);
2186 (void) printf("\ttxg = %llu\n", (u_longlong_t)ub->ub_txg);
2187 (void) printf("\tguid_sum = %llu\n", (u_longlong_t)ub->ub_guid_sum);
2188 (void) printf("\ttimestamp = %llu UTC = %s",
2189 (u_longlong_t)ub->ub_timestamp, asctime(localtime(×tamp)));
2191 (void) printf("\tmmp_magic = %016llx\n",
2192 (u_longlong_t)ub->ub_mmp_magic);
2193 if (ub->ub_mmp_magic == MMP_MAGIC)
2194 (void) printf("\tmmp_delay = %0llu\n",
2195 (u_longlong_t)ub->ub_mmp_delay);
2197 if (dump_opt['u'] >= 4) {
2198 char blkbuf[BP_SPRINTF_LEN];
2199 snprintf_blkptr(blkbuf, sizeof (blkbuf), &ub->ub_rootbp);
2200 (void) printf("\trootbp = %s\n", blkbuf);
2202 (void) printf("%s", footer ? footer : "");
2206 dump_config(spa_t *spa)
2213 error = dmu_bonus_hold(spa->spa_meta_objset,
2214 spa->spa_config_object, FTAG, &db);
2217 nvsize = *(uint64_t *)db->db_data;
2218 dmu_buf_rele(db, FTAG);
2220 (void) printf("\nMOS Configuration:\n");
2221 dump_packed_nvlist(spa->spa_meta_objset,
2222 spa->spa_config_object, (void *)&nvsize, 1);
2224 (void) fprintf(stderr, "dmu_bonus_hold(%llu) failed, errno %d",
2225 (u_longlong_t)spa->spa_config_object, error);
2230 dump_cachefile(const char *cachefile)
2233 struct stat64 statbuf;
2237 if ((fd = open64(cachefile, O_RDONLY)) < 0) {
2238 (void) printf("cannot open '%s': %s\n", cachefile,
2243 if (fstat64(fd, &statbuf) != 0) {
2244 (void) printf("failed to stat '%s': %s\n", cachefile,
2249 if ((buf = malloc(statbuf.st_size)) == NULL) {
2250 (void) fprintf(stderr, "failed to allocate %llu bytes\n",
2251 (u_longlong_t)statbuf.st_size);
2255 if (read(fd, buf, statbuf.st_size) != statbuf.st_size) {
2256 (void) fprintf(stderr, "failed to read %llu bytes\n",
2257 (u_longlong_t)statbuf.st_size);
2263 if (nvlist_unpack(buf, statbuf.st_size, &config, 0) != 0) {
2264 (void) fprintf(stderr, "failed to unpack nvlist\n");
2270 dump_nvlist(config, 0);
2272 nvlist_free(config);
2276 * ZFS label nvlist stats
2278 typedef struct zdb_nvl_stats {
2281 size_t zns_leaf_largest;
2282 size_t zns_leaf_total;
2283 nvlist_t *zns_string;
2284 nvlist_t *zns_uint64;
2285 nvlist_t *zns_boolean;
2289 collect_nvlist_stats(nvlist_t *nvl, zdb_nvl_stats_t *stats)
2291 nvlist_t *list, **array;
2292 nvpair_t *nvp = NULL;
2296 stats->zns_list_count++;
2298 while ((nvp = nvlist_next_nvpair(nvl, nvp)) != NULL) {
2299 name = nvpair_name(nvp);
2301 switch (nvpair_type(nvp)) {
2302 case DATA_TYPE_STRING:
2303 fnvlist_add_string(stats->zns_string, name,
2304 fnvpair_value_string(nvp));
2306 case DATA_TYPE_UINT64:
2307 fnvlist_add_uint64(stats->zns_uint64, name,
2308 fnvpair_value_uint64(nvp));
2310 case DATA_TYPE_BOOLEAN:
2311 fnvlist_add_boolean(stats->zns_boolean, name);
2313 case DATA_TYPE_NVLIST:
2314 if (nvpair_value_nvlist(nvp, &list) == 0)
2315 collect_nvlist_stats(list, stats);
2317 case DATA_TYPE_NVLIST_ARRAY:
2318 if (nvpair_value_nvlist_array(nvp, &array, &items) != 0)
2321 for (i = 0; i < items; i++) {
2322 collect_nvlist_stats(array[i], stats);
2324 /* collect stats on leaf vdev */
2325 if (strcmp(name, "children") == 0) {
2328 (void) nvlist_size(array[i], &size,
2330 stats->zns_leaf_total += size;
2331 if (size > stats->zns_leaf_largest)
2332 stats->zns_leaf_largest = size;
2333 stats->zns_leaf_count++;
2338 (void) printf("skip type %d!\n", (int)nvpair_type(nvp));
2344 dump_nvlist_stats(nvlist_t *nvl, size_t cap)
2346 zdb_nvl_stats_t stats = { 0 };
2347 size_t size, sum = 0, total;
2350 /* requires nvlist with non-unique names for stat collection */
2351 VERIFY0(nvlist_alloc(&stats.zns_string, 0, 0));
2352 VERIFY0(nvlist_alloc(&stats.zns_uint64, 0, 0));
2353 VERIFY0(nvlist_alloc(&stats.zns_boolean, 0, 0));
2354 VERIFY0(nvlist_size(stats.zns_boolean, &noise, NV_ENCODE_XDR));
2356 (void) printf("\n\nZFS Label NVList Config Stats:\n");
2358 VERIFY0(nvlist_size(nvl, &total, NV_ENCODE_XDR));
2359 (void) printf(" %d bytes used, %d bytes free (using %4.1f%%)\n\n",
2360 (int)total, (int)(cap - total), 100.0 * total / cap);
2362 collect_nvlist_stats(nvl, &stats);
2364 VERIFY0(nvlist_size(stats.zns_uint64, &size, NV_ENCODE_XDR));
2367 (void) printf("%12s %4d %6d bytes (%5.2f%%)\n", "integers:",
2368 (int)fnvlist_num_pairs(stats.zns_uint64),
2369 (int)size, 100.0 * size / total);
2371 VERIFY0(nvlist_size(stats.zns_string, &size, NV_ENCODE_XDR));
2374 (void) printf("%12s %4d %6d bytes (%5.2f%%)\n", "strings:",
2375 (int)fnvlist_num_pairs(stats.zns_string),
2376 (int)size, 100.0 * size / total);
2378 VERIFY0(nvlist_size(stats.zns_boolean, &size, NV_ENCODE_XDR));
2381 (void) printf("%12s %4d %6d bytes (%5.2f%%)\n", "booleans:",
2382 (int)fnvlist_num_pairs(stats.zns_boolean),
2383 (int)size, 100.0 * size / total);
2385 size = total - sum; /* treat remainder as nvlist overhead */
2386 (void) printf("%12s %4d %6d bytes (%5.2f%%)\n\n", "nvlists:",
2387 stats.zns_list_count, (int)size, 100.0 * size / total);
2389 if (stats.zns_leaf_count > 0) {
2390 size_t average = stats.zns_leaf_total / stats.zns_leaf_count;
2392 (void) printf("%12s %4d %6d bytes average\n", "leaf vdevs:",
2393 stats.zns_leaf_count, (int)average);
2394 (void) printf("%24d bytes largest\n",
2395 (int)stats.zns_leaf_largest);
2397 if (dump_opt['l'] >= 3 && average > 0)
2398 (void) printf(" space for %d additional leaf vdevs\n",
2399 (int)((cap - total) / average));
2401 (void) printf("\n");
2403 nvlist_free(stats.zns_string);
2404 nvlist_free(stats.zns_uint64);
2405 nvlist_free(stats.zns_boolean);
2408 typedef struct cksum_record {
2410 boolean_t labels[VDEV_LABELS];
2415 cksum_record_compare(const void *x1, const void *x2)
2417 const cksum_record_t *l = (cksum_record_t *)x1;
2418 const cksum_record_t *r = (cksum_record_t *)x2;
2419 int arraysize = ARRAY_SIZE(l->cksum.zc_word);
2422 for (int i = 0; i < arraysize; i++) {
2423 difference = AVL_CMP(l->cksum.zc_word[i], r->cksum.zc_word[i]);
2428 return (difference);
2431 static cksum_record_t *
2432 cksum_record_alloc(zio_cksum_t *cksum, int l)
2434 cksum_record_t *rec;
2436 rec = umem_zalloc(sizeof (*rec), UMEM_NOFAIL);
2437 rec->cksum = *cksum;
2438 rec->labels[l] = B_TRUE;
2443 static cksum_record_t *
2444 cksum_record_lookup(avl_tree_t *tree, zio_cksum_t *cksum)
2446 cksum_record_t lookup = { .cksum = *cksum };
2449 return (avl_find(tree, &lookup, &where));
2452 static cksum_record_t *
2453 cksum_record_insert(avl_tree_t *tree, zio_cksum_t *cksum, int l)
2455 cksum_record_t *rec;
2457 rec = cksum_record_lookup(tree, cksum);
2459 rec->labels[l] = B_TRUE;
2461 rec = cksum_record_alloc(cksum, l);
2469 first_label(cksum_record_t *rec)
2471 for (int i = 0; i < VDEV_LABELS; i++)
2479 print_label_numbers(char *prefix, cksum_record_t *rec)
2481 printf("%s", prefix);
2482 for (int i = 0; i < VDEV_LABELS; i++)
2483 if (rec->labels[i] == B_TRUE)
2488 #define MAX_UBERBLOCK_COUNT (VDEV_UBERBLOCK_RING >> UBERBLOCK_SHIFT)
2490 typedef struct label {
2492 nvlist_t *config_nv;
2493 cksum_record_t *config;
2494 cksum_record_t *uberblocks[MAX_UBERBLOCK_COUNT];
2495 boolean_t header_printed;
2496 boolean_t read_failed;
2500 print_label_header(label_t *label, int l)
2506 if (label->header_printed == B_TRUE)
2509 (void) printf("------------------------------------\n");
2510 (void) printf("LABEL %d\n", l);
2511 (void) printf("------------------------------------\n");
2513 label->header_printed = B_TRUE;
2517 dump_config_from_label(label_t *label, size_t buflen, int l)
2522 if ((dump_opt['l'] < 3) && (first_label(label->config) != l))
2525 print_label_header(label, l);
2526 dump_nvlist(label->config_nv, 4);
2527 print_label_numbers(" labels = ", label->config);
2529 if (dump_opt['l'] >= 2)
2530 dump_nvlist_stats(label->config_nv, buflen);
2533 #define ZDB_MAX_UB_HEADER_SIZE 32
2536 dump_label_uberblocks(label_t *label, uint64_t ashift, int label_num)
2540 char header[ZDB_MAX_UB_HEADER_SIZE];
2542 vd.vdev_ashift = ashift;
2545 for (int i = 0; i < VDEV_UBERBLOCK_COUNT(&vd); i++) {
2546 uint64_t uoff = VDEV_UBERBLOCK_OFFSET(&vd, i);
2547 uberblock_t *ub = (void *)((char *)&label->label + uoff);
2548 cksum_record_t *rec = label->uberblocks[i];
2551 if (dump_opt['u'] >= 2) {
2552 print_label_header(label, label_num);
2553 (void) printf(" Uberblock[%d] invalid\n", i);
2558 if ((dump_opt['u'] < 3) && (first_label(rec) != label_num))
2561 if ((dump_opt['u'] < 4) &&
2562 (ub->ub_mmp_magic == MMP_MAGIC) && ub->ub_mmp_delay &&
2563 (i >= VDEV_UBERBLOCK_COUNT(&vd) - MMP_BLOCKS_PER_LABEL))
2566 print_label_header(label, label_num);
2567 (void) snprintf(header, ZDB_MAX_UB_HEADER_SIZE,
2568 " Uberblock[%d]\n", i);
2569 dump_uberblock(ub, header, "");
2570 print_label_numbers(" labels = ", rec);
2574 static char curpath[PATH_MAX];
2577 * Iterate through the path components, recursively passing
2578 * current one's obj and remaining path until we find the obj
2582 dump_path_impl(objset_t *os, uint64_t obj, char *name)
2589 dmu_object_info_t doi;
2591 if ((s = strchr(name, '/')) != NULL)
2593 err = zap_lookup(os, obj, name, 8, 1, &child_obj);
2595 (void) strlcat(curpath, name, sizeof (curpath));
2598 (void) fprintf(stderr, "failed to lookup %s: %s\n",
2599 curpath, strerror(err));
2603 child_obj = ZFS_DIRENT_OBJ(child_obj);
2604 err = sa_buf_hold(os, child_obj, FTAG, &db);
2606 (void) fprintf(stderr,
2607 "failed to get SA dbuf for obj %llu: %s\n",
2608 (u_longlong_t)child_obj, strerror(err));
2611 dmu_object_info_from_db(db, &doi);
2612 sa_buf_rele(db, FTAG);
2614 if (doi.doi_bonus_type != DMU_OT_SA &&
2615 doi.doi_bonus_type != DMU_OT_ZNODE) {
2616 (void) fprintf(stderr, "invalid bonus type %d for obj %llu\n",
2617 doi.doi_bonus_type, (u_longlong_t)child_obj);
2621 if (dump_opt['v'] > 6) {
2622 (void) printf("obj=%llu %s type=%d bonustype=%d\n",
2623 (u_longlong_t)child_obj, curpath, doi.doi_type,
2624 doi.doi_bonus_type);
2627 (void) strlcat(curpath, "/", sizeof (curpath));
2629 switch (doi.doi_type) {
2630 case DMU_OT_DIRECTORY_CONTENTS:
2631 if (s != NULL && *(s + 1) != '\0')
2632 return (dump_path_impl(os, child_obj, s + 1));
2634 case DMU_OT_PLAIN_FILE_CONTENTS:
2635 dump_object(os, child_obj, dump_opt['v'], &header, NULL);
2638 (void) fprintf(stderr, "object %llu has non-file/directory "
2639 "type %d\n", (u_longlong_t)obj, doi.doi_type);
2647 * Dump the blocks for the object specified by path inside the dataset.
2650 dump_path(char *ds, char *path)
2656 err = open_objset(ds, DMU_OST_ZFS, FTAG, &os);
2660 err = zap_lookup(os, MASTER_NODE_OBJ, ZFS_ROOT_OBJ, 8, 1, &root_obj);
2662 (void) fprintf(stderr, "can't lookup root znode: %s\n",
2664 dmu_objset_disown(os, FTAG);
2668 (void) snprintf(curpath, sizeof (curpath), "dataset=%s path=/", ds);
2670 err = dump_path_impl(os, root_obj, path);
2672 close_objset(os, FTAG);
2677 dump_label(const char *dev)
2679 char path[MAXPATHLEN];
2680 label_t labels[VDEV_LABELS];
2681 uint64_t psize, ashift;
2682 struct stat64 statbuf;
2683 boolean_t config_found = B_FALSE;
2684 boolean_t error = B_FALSE;
2685 avl_tree_t config_tree;
2686 avl_tree_t uberblock_tree;
2687 void *node, *cookie;
2690 bzero(labels, sizeof (labels));
2693 * Check if we were given absolute path and use it as is.
2694 * Otherwise if the provided vdev name doesn't point to a file,
2695 * try prepending expected disk paths and partition numbers.
2697 (void) strlcpy(path, dev, sizeof (path));
2698 if (dev[0] != '/' && stat64(path, &statbuf) != 0) {
2701 error = zfs_resolve_shortname(dev, path, MAXPATHLEN);
2702 if (error == 0 && zfs_dev_is_whole_disk(path)) {
2703 if (zfs_append_partition(path, MAXPATHLEN) == -1)
2707 if (error || (stat64(path, &statbuf) != 0)) {
2708 (void) printf("failed to find device %s, try "
2709 "specifying absolute path instead\n", dev);
2714 if ((fd = open64(path, O_RDONLY)) < 0) {
2715 (void) printf("cannot open '%s': %s\n", path, strerror(errno));
2719 if (fstat64_blk(fd, &statbuf) != 0) {
2720 (void) printf("failed to stat '%s': %s\n", path,
2726 if (S_ISBLK(statbuf.st_mode) && ioctl(fd, BLKFLSBUF) != 0)
2727 (void) printf("failed to invalidate cache '%s' : %s\n", path,
2730 avl_create(&config_tree, cksum_record_compare,
2731 sizeof (cksum_record_t), offsetof(cksum_record_t, link));
2732 avl_create(&uberblock_tree, cksum_record_compare,
2733 sizeof (cksum_record_t), offsetof(cksum_record_t, link));
2735 psize = statbuf.st_size;
2736 psize = P2ALIGN(psize, (uint64_t)sizeof (vdev_label_t));
2737 ashift = SPA_MINBLOCKSHIFT;
2740 * 1. Read the label from disk
2741 * 2. Unpack the configuration and insert in config tree.
2742 * 3. Traverse all uberblocks and insert in uberblock tree.
2744 for (int l = 0; l < VDEV_LABELS; l++) {
2745 label_t *label = &labels[l];
2746 char *buf = label->label.vl_vdev_phys.vp_nvlist;
2747 size_t buflen = sizeof (label->label.vl_vdev_phys.vp_nvlist);
2749 cksum_record_t *rec;
2753 if (pread64(fd, &label->label, sizeof (label->label),
2754 vdev_label_offset(psize, l, 0)) != sizeof (label->label)) {
2756 (void) printf("failed to read label %d\n", l);
2757 label->read_failed = B_TRUE;
2762 label->read_failed = B_FALSE;
2764 if (nvlist_unpack(buf, buflen, &config, 0) == 0) {
2765 nvlist_t *vdev_tree = NULL;
2768 if ((nvlist_lookup_nvlist(config,
2769 ZPOOL_CONFIG_VDEV_TREE, &vdev_tree) != 0) ||
2770 (nvlist_lookup_uint64(vdev_tree,
2771 ZPOOL_CONFIG_ASHIFT, &ashift) != 0))
2772 ashift = SPA_MINBLOCKSHIFT;
2774 if (nvlist_size(config, &size, NV_ENCODE_XDR) != 0)
2777 fletcher_4_native_varsize(buf, size, &cksum);
2778 rec = cksum_record_insert(&config_tree, &cksum, l);
2780 label->config = rec;
2781 label->config_nv = config;
2782 config_found = B_TRUE;
2787 vd.vdev_ashift = ashift;
2790 for (int i = 0; i < VDEV_UBERBLOCK_COUNT(&vd); i++) {
2791 uint64_t uoff = VDEV_UBERBLOCK_OFFSET(&vd, i);
2792 uberblock_t *ub = (void *)((char *)label + uoff);
2794 if (uberblock_verify(ub))
2797 fletcher_4_native_varsize(ub, sizeof (*ub), &cksum);
2798 rec = cksum_record_insert(&uberblock_tree, &cksum, l);
2800 label->uberblocks[i] = rec;
2805 * Dump the label and uberblocks.
2807 for (int l = 0; l < VDEV_LABELS; l++) {
2808 label_t *label = &labels[l];
2809 size_t buflen = sizeof (label->label.vl_vdev_phys.vp_nvlist);
2811 if (label->read_failed == B_TRUE)
2814 if (label->config_nv) {
2815 dump_config_from_label(label, buflen, l);
2818 (void) printf("failed to unpack label %d\n", l);
2822 dump_label_uberblocks(label, ashift, l);
2824 nvlist_free(label->config_nv);
2828 while ((node = avl_destroy_nodes(&config_tree, &cookie)) != NULL)
2829 umem_free(node, sizeof (cksum_record_t));
2832 while ((node = avl_destroy_nodes(&uberblock_tree, &cookie)) != NULL)
2833 umem_free(node, sizeof (cksum_record_t));
2835 avl_destroy(&config_tree);
2836 avl_destroy(&uberblock_tree);
2840 return (config_found == B_FALSE ? 2 :
2841 (error == B_TRUE ? 1 : 0));
2844 static uint64_t dataset_feature_count[SPA_FEATURES];
2848 dump_one_dir(const char *dsname, void *arg)
2854 error = open_objset(dsname, DMU_OST_ANY, FTAG, &os);
2858 for (f = 0; f < SPA_FEATURES; f++) {
2859 if (!dmu_objset_ds(os)->ds_feature_inuse[f])
2861 ASSERT(spa_feature_table[f].fi_flags &
2862 ZFEATURE_FLAG_PER_DATASET);
2863 dataset_feature_count[f]++;
2867 close_objset(os, FTAG);
2868 fuid_table_destroy();
2875 #define PSIZE_HISTO_SIZE (SPA_OLD_MAXBLOCKSIZE / SPA_MINBLOCKSIZE + 2)
2876 typedef struct zdb_blkstats {
2882 uint64_t zb_ditto_samevdev;
2883 uint64_t zb_psize_histogram[PSIZE_HISTO_SIZE];
2887 * Extended object types to report deferred frees and dedup auto-ditto blocks.
2889 #define ZDB_OT_DEFERRED (DMU_OT_NUMTYPES + 0)
2890 #define ZDB_OT_DITTO (DMU_OT_NUMTYPES + 1)
2891 #define ZDB_OT_OTHER (DMU_OT_NUMTYPES + 2)
2892 #define ZDB_OT_TOTAL (DMU_OT_NUMTYPES + 3)
2894 static char *zdb_ot_extname[] = {
2901 #define ZB_TOTAL DN_MAX_LEVELS
2903 typedef struct zdb_cb {
2904 zdb_blkstats_t zcb_type[ZB_TOTAL + 1][ZDB_OT_TOTAL + 1];
2905 uint64_t zcb_dedup_asize;
2906 uint64_t zcb_dedup_blocks;
2907 uint64_t zcb_embedded_blocks[NUM_BP_EMBEDDED_TYPES];
2908 uint64_t zcb_embedded_histogram[NUM_BP_EMBEDDED_TYPES]
2909 [BPE_PAYLOAD_SIZE + 1];
2911 uint64_t zcb_lastprint;
2912 uint64_t zcb_totalasize;
2913 uint64_t zcb_errors[256];
2920 zdb_count_block(zdb_cb_t *zcb, zilog_t *zilog, const blkptr_t *bp,
2921 dmu_object_type_t type)
2923 uint64_t refcnt = 0;
2926 ASSERT(type < ZDB_OT_TOTAL);
2928 if (zilog && zil_bp_tree_add(zilog, bp) != 0)
2931 for (i = 0; i < 4; i++) {
2932 int l = (i < 2) ? BP_GET_LEVEL(bp) : ZB_TOTAL;
2933 int t = (i & 1) ? type : ZDB_OT_TOTAL;
2935 zdb_blkstats_t *zb = &zcb->zcb_type[l][t];
2937 zb->zb_asize += BP_GET_ASIZE(bp);
2938 zb->zb_lsize += BP_GET_LSIZE(bp);
2939 zb->zb_psize += BP_GET_PSIZE(bp);
2943 * The histogram is only big enough to record blocks up to
2944 * SPA_OLD_MAXBLOCKSIZE; larger blocks go into the last,
2947 int idx = BP_GET_PSIZE(bp) >> SPA_MINBLOCKSHIFT;
2948 idx = MIN(idx, SPA_OLD_MAXBLOCKSIZE / SPA_MINBLOCKSIZE + 1);
2949 zb->zb_psize_histogram[idx]++;
2951 zb->zb_gangs += BP_COUNT_GANG(bp);
2953 switch (BP_GET_NDVAS(bp)) {
2955 if (DVA_GET_VDEV(&bp->blk_dva[0]) ==
2956 DVA_GET_VDEV(&bp->blk_dva[1]))
2957 zb->zb_ditto_samevdev++;
2960 equal = (DVA_GET_VDEV(&bp->blk_dva[0]) ==
2961 DVA_GET_VDEV(&bp->blk_dva[1])) +
2962 (DVA_GET_VDEV(&bp->blk_dva[0]) ==
2963 DVA_GET_VDEV(&bp->blk_dva[2])) +
2964 (DVA_GET_VDEV(&bp->blk_dva[1]) ==
2965 DVA_GET_VDEV(&bp->blk_dva[2]));
2967 zb->zb_ditto_samevdev++;
2973 if (BP_IS_EMBEDDED(bp)) {
2974 zcb->zcb_embedded_blocks[BPE_GET_ETYPE(bp)]++;
2975 zcb->zcb_embedded_histogram[BPE_GET_ETYPE(bp)]
2976 [BPE_GET_PSIZE(bp)]++;
2983 if (BP_GET_DEDUP(bp)) {
2987 ddt = ddt_select(zcb->zcb_spa, bp);
2989 dde = ddt_lookup(ddt, bp, B_FALSE);
2994 ddt_phys_t *ddp = ddt_phys_select(dde, bp);
2995 ddt_phys_decref(ddp);
2996 refcnt = ddp->ddp_refcnt;
2997 if (ddt_phys_total_refcnt(dde) == 0)
2998 ddt_remove(ddt, dde);
3003 VERIFY3U(zio_wait(zio_claim(NULL, zcb->zcb_spa,
3004 refcnt ? 0 : spa_first_txg(zcb->zcb_spa),
3005 bp, NULL, NULL, ZIO_FLAG_CANFAIL)), ==, 0);
3009 zdb_blkptr_done(zio_t *zio)
3011 spa_t *spa = zio->io_spa;
3012 blkptr_t *bp = zio->io_bp;
3013 int ioerr = zio->io_error;
3014 zdb_cb_t *zcb = zio->io_private;
3015 zbookmark_phys_t *zb = &zio->io_bookmark;
3017 abd_free(zio->io_abd);
3019 mutex_enter(&spa->spa_scrub_lock);
3020 spa->spa_scrub_inflight--;
3021 cv_broadcast(&spa->spa_scrub_io_cv);
3023 if (ioerr && !(zio->io_flags & ZIO_FLAG_SPECULATIVE)) {
3024 char blkbuf[BP_SPRINTF_LEN];
3026 zcb->zcb_haderrors = 1;
3027 zcb->zcb_errors[ioerr]++;
3029 if (dump_opt['b'] >= 2)
3030 snprintf_blkptr(blkbuf, sizeof (blkbuf), bp);
3034 (void) printf("zdb_blkptr_cb: "
3035 "Got error %d reading "
3036 "<%llu, %llu, %lld, %llx> %s -- skipping\n",
3038 (u_longlong_t)zb->zb_objset,
3039 (u_longlong_t)zb->zb_object,
3040 (u_longlong_t)zb->zb_level,
3041 (u_longlong_t)zb->zb_blkid,
3044 mutex_exit(&spa->spa_scrub_lock);
3048 zdb_blkptr_cb(spa_t *spa, zilog_t *zilog, const blkptr_t *bp,
3049 const zbookmark_phys_t *zb, const dnode_phys_t *dnp, void *arg)
3051 zdb_cb_t *zcb = arg;
3052 dmu_object_type_t type;
3053 boolean_t is_metadata;
3058 if (dump_opt['b'] >= 5 && bp->blk_birth > 0) {
3059 char blkbuf[BP_SPRINTF_LEN];
3060 snprintf_blkptr(blkbuf, sizeof (blkbuf), bp);
3061 (void) printf("objset %llu object %llu "
3062 "level %lld offset 0x%llx %s\n",
3063 (u_longlong_t)zb->zb_objset,
3064 (u_longlong_t)zb->zb_object,
3065 (longlong_t)zb->zb_level,
3066 (u_longlong_t)blkid2offset(dnp, bp, zb),
3073 type = BP_GET_TYPE(bp);
3075 zdb_count_block(zcb, zilog, bp,
3076 (type & DMU_OT_NEWTYPE) ? ZDB_OT_OTHER : type);
3078 is_metadata = (BP_GET_LEVEL(bp) != 0 || DMU_OT_IS_METADATA(type));
3080 if (!BP_IS_EMBEDDED(bp) &&
3081 (dump_opt['c'] > 1 || (dump_opt['c'] && is_metadata))) {
3082 size_t size = BP_GET_PSIZE(bp);
3083 abd_t *abd = abd_alloc(size, B_FALSE);
3084 int flags = ZIO_FLAG_CANFAIL | ZIO_FLAG_SCRUB | ZIO_FLAG_RAW;
3086 /* If it's an intent log block, failure is expected. */
3087 if (zb->zb_level == ZB_ZIL_LEVEL)
3088 flags |= ZIO_FLAG_SPECULATIVE;
3090 mutex_enter(&spa->spa_scrub_lock);
3091 while (spa->spa_scrub_inflight > max_inflight)
3092 cv_wait(&spa->spa_scrub_io_cv, &spa->spa_scrub_lock);
3093 spa->spa_scrub_inflight++;
3094 mutex_exit(&spa->spa_scrub_lock);
3096 zio_nowait(zio_read(NULL, spa, bp, abd, size,
3097 zdb_blkptr_done, zcb, ZIO_PRIORITY_ASYNC_READ, flags, zb));
3100 zcb->zcb_readfails = 0;
3102 /* only call gethrtime() every 100 blocks */
3109 if (dump_opt['b'] < 5 && gethrtime() > zcb->zcb_lastprint + NANOSEC) {
3110 uint64_t now = gethrtime();
3112 uint64_t bytes = zcb->zcb_type[ZB_TOTAL][ZDB_OT_TOTAL].zb_asize;
3114 1 + bytes / (1 + ((now - zcb->zcb_start) / 1000 / 1000));
3116 (zcb->zcb_totalasize - bytes) / 1024 / kb_per_sec;
3118 zfs_nicebytes(bytes, buf, sizeof (buf));
3119 (void) fprintf(stderr,
3120 "\r%5s completed (%4dMB/s) "
3121 "estimated time remaining: %uhr %02umin %02usec ",
3122 buf, kb_per_sec / 1024,
3123 sec_remaining / 60 / 60,
3124 sec_remaining / 60 % 60,
3125 sec_remaining % 60);
3127 zcb->zcb_lastprint = now;
3134 zdb_leak(void *arg, uint64_t start, uint64_t size)
3138 (void) printf("leaked space: vdev %llu, offset 0x%llx, size %llu\n",
3139 (u_longlong_t)vd->vdev_id, (u_longlong_t)start, (u_longlong_t)size);
3142 static metaslab_ops_t zdb_metaslab_ops = {
3147 zdb_ddt_leak_init(spa_t *spa, zdb_cb_t *zcb)
3149 ddt_bookmark_t ddb = { 0 };
3154 while ((error = ddt_walk(spa, &ddb, &dde)) == 0) {
3156 ddt_phys_t *ddp = dde.dde_phys;
3158 if (ddb.ddb_class == DDT_CLASS_UNIQUE)
3161 ASSERT(ddt_phys_total_refcnt(&dde) > 1);
3163 for (p = 0; p < DDT_PHYS_TYPES; p++, ddp++) {
3164 if (ddp->ddp_phys_birth == 0)
3166 ddt_bp_create(ddb.ddb_checksum,
3167 &dde.dde_key, ddp, &blk);
3168 if (p == DDT_PHYS_DITTO) {
3169 zdb_count_block(zcb, NULL, &blk, ZDB_OT_DITTO);
3171 zcb->zcb_dedup_asize +=
3172 BP_GET_ASIZE(&blk) * (ddp->ddp_refcnt - 1);
3173 zcb->zcb_dedup_blocks++;
3176 if (!dump_opt['L']) {
3177 ddt_t *ddt = spa->spa_ddt[ddb.ddb_checksum];
3179 VERIFY(ddt_lookup(ddt, &blk, B_TRUE) != NULL);
3184 ASSERT(error == ENOENT);
3188 zdb_leak_init(spa_t *spa, zdb_cb_t *zcb)
3193 if (!dump_opt['L']) {
3194 vdev_t *rvd = spa->spa_root_vdev;
3197 * We are going to be changing the meaning of the metaslab's
3198 * ms_tree. Ensure that the allocator doesn't try to
3201 spa->spa_normal_class->mc_ops = &zdb_metaslab_ops;
3202 spa->spa_log_class->mc_ops = &zdb_metaslab_ops;
3204 for (c = 0; c < rvd->vdev_children; c++) {
3205 vdev_t *vd = rvd->vdev_child[c];
3206 ASSERTV(metaslab_group_t *mg = vd->vdev_mg);
3207 for (m = 0; m < vd->vdev_ms_count; m++) {
3208 metaslab_t *msp = vd->vdev_ms[m];
3209 ASSERT3P(msp->ms_group, ==, mg);
3210 mutex_enter(&msp->ms_lock);
3211 metaslab_unload(msp);
3214 * For leak detection, we overload the metaslab
3215 * ms_tree to contain allocated segments
3216 * instead of free segments. As a result,
3217 * we can't use the normal metaslab_load/unload
3220 if (msp->ms_sm != NULL) {
3221 (void) fprintf(stderr,
3222 "\rloading space map for "
3223 "vdev %llu of %llu, "
3224 "metaslab %llu of %llu ...",
3226 (longlong_t)rvd->vdev_children,
3228 (longlong_t)vd->vdev_ms_count);
3231 * We don't want to spend the CPU
3232 * manipulating the size-ordered
3233 * tree, so clear the range_tree
3236 msp->ms_tree->rt_ops = NULL;
3237 VERIFY0(space_map_load(msp->ms_sm,
3238 msp->ms_tree, SM_ALLOC));
3240 if (!msp->ms_loaded)
3241 msp->ms_loaded = B_TRUE;
3243 mutex_exit(&msp->ms_lock);
3246 (void) fprintf(stderr, "\n");
3249 spa_config_enter(spa, SCL_CONFIG, FTAG, RW_READER);
3251 zdb_ddt_leak_init(spa, zcb);
3253 spa_config_exit(spa, SCL_CONFIG, FTAG);
3257 zdb_leak_fini(spa_t *spa)
3261 if (!dump_opt['L']) {
3262 vdev_t *rvd = spa->spa_root_vdev;
3263 for (c = 0; c < rvd->vdev_children; c++) {
3264 vdev_t *vd = rvd->vdev_child[c];
3265 ASSERTV(metaslab_group_t *mg = vd->vdev_mg);
3266 for (m = 0; m < vd->vdev_ms_count; m++) {
3267 metaslab_t *msp = vd->vdev_ms[m];
3268 ASSERT3P(mg, ==, msp->ms_group);
3269 mutex_enter(&msp->ms_lock);
3272 * The ms_tree has been overloaded to
3273 * contain allocated segments. Now that we
3274 * finished traversing all blocks, any
3275 * block that remains in the ms_tree
3276 * represents an allocated block that we
3277 * did not claim during the traversal.
3278 * Claimed blocks would have been removed
3281 range_tree_vacate(msp->ms_tree, zdb_leak, vd);
3284 msp->ms_loaded = B_FALSE;
3286 mutex_exit(&msp->ms_lock);
3294 count_block_cb(void *arg, const blkptr_t *bp, dmu_tx_t *tx)
3296 zdb_cb_t *zcb = arg;
3298 if (dump_opt['b'] >= 5) {
3299 char blkbuf[BP_SPRINTF_LEN];
3300 snprintf_blkptr(blkbuf, sizeof (blkbuf), bp);
3301 (void) printf("[%s] %s\n",
3302 "deferred free", blkbuf);
3304 zdb_count_block(zcb, NULL, bp, ZDB_OT_DEFERRED);
3309 dump_block_stats(spa_t *spa)
3312 zdb_blkstats_t *zb, *tzb;
3313 uint64_t norm_alloc, norm_space, total_alloc, total_found;
3314 int flags = TRAVERSE_PRE | TRAVERSE_PREFETCH_METADATA | TRAVERSE_HARD;
3315 boolean_t leaks = B_FALSE;
3317 bp_embedded_type_t i;
3319 (void) printf("\nTraversing all blocks %s%s%s%s%s...\n\n",
3320 (dump_opt['c'] || !dump_opt['L']) ? "to verify " : "",
3321 (dump_opt['c'] == 1) ? "metadata " : "",
3322 dump_opt['c'] ? "checksums " : "",
3323 (dump_opt['c'] && !dump_opt['L']) ? "and verify " : "",
3324 !dump_opt['L'] ? "nothing leaked " : "");
3327 * Load all space maps as SM_ALLOC maps, then traverse the pool
3328 * claiming each block we discover. If the pool is perfectly
3329 * consistent, the space maps will be empty when we're done.
3330 * Anything left over is a leak; any block we can't claim (because
3331 * it's not part of any space map) is a double allocation,
3332 * reference to a freed block, or an unclaimed log block.
3334 bzero(&zcb, sizeof (zdb_cb_t));
3335 zdb_leak_init(spa, &zcb);
3338 * If there's a deferred-free bplist, process that first.
3340 (void) bpobj_iterate_nofree(&spa->spa_deferred_bpobj,
3341 count_block_cb, &zcb, NULL);
3342 if (spa_version(spa) >= SPA_VERSION_DEADLISTS) {
3343 (void) bpobj_iterate_nofree(&spa->spa_dsl_pool->dp_free_bpobj,
3344 count_block_cb, &zcb, NULL);
3346 if (spa_feature_is_active(spa, SPA_FEATURE_ASYNC_DESTROY)) {
3347 VERIFY3U(0, ==, bptree_iterate(spa->spa_meta_objset,
3348 spa->spa_dsl_pool->dp_bptree_obj, B_FALSE, count_block_cb,
3352 if (dump_opt['c'] > 1)
3353 flags |= TRAVERSE_PREFETCH_DATA;
3355 zcb.zcb_totalasize = metaslab_class_get_alloc(spa_normal_class(spa));
3356 zcb.zcb_start = zcb.zcb_lastprint = gethrtime();
3357 err = traverse_pool(spa, 0, flags, zdb_blkptr_cb, &zcb);
3360 * If we've traversed the data blocks then we need to wait for those
3361 * I/Os to complete. We leverage "The Godfather" zio to wait on
3362 * all async I/Os to complete.
3364 if (dump_opt['c']) {
3365 for (c = 0; c < max_ncpus; c++) {
3366 (void) zio_wait(spa->spa_async_zio_root[c]);
3367 spa->spa_async_zio_root[c] = zio_root(spa, NULL, NULL,
3368 ZIO_FLAG_CANFAIL | ZIO_FLAG_SPECULATIVE |
3369 ZIO_FLAG_GODFATHER);
3374 * Done after zio_wait() since zcb_haderrors is modified in
3377 zcb.zcb_haderrors |= err;
3379 if (zcb.zcb_haderrors) {
3380 (void) printf("\nError counts:\n\n");
3381 (void) printf("\t%5s %s\n", "errno", "count");
3382 for (e = 0; e < 256; e++) {
3383 if (zcb.zcb_errors[e] != 0) {
3384 (void) printf("\t%5d %llu\n",
3385 e, (u_longlong_t)zcb.zcb_errors[e]);
3391 * Report any leaked segments.
3395 tzb = &zcb.zcb_type[ZB_TOTAL][ZDB_OT_TOTAL];
3397 norm_alloc = metaslab_class_get_alloc(spa_normal_class(spa));
3398 norm_space = metaslab_class_get_space(spa_normal_class(spa));
3400 total_alloc = norm_alloc + metaslab_class_get_alloc(spa_log_class(spa));
3401 total_found = tzb->zb_asize - zcb.zcb_dedup_asize;
3403 if (total_found == total_alloc) {
3405 (void) printf("\n\tNo leaks (block sum matches space"
3406 " maps exactly)\n");
3408 (void) printf("block traversal size %llu != alloc %llu "
3410 (u_longlong_t)total_found,
3411 (u_longlong_t)total_alloc,
3412 (dump_opt['L']) ? "unreachable" : "leaked",
3413 (longlong_t)(total_alloc - total_found));
3417 if (tzb->zb_count == 0)
3420 (void) printf("\n");
3421 (void) printf("\tbp count: %10llu\n",
3422 (u_longlong_t)tzb->zb_count);
3423 (void) printf("\tganged count: %10llu\n",
3424 (longlong_t)tzb->zb_gangs);
3425 (void) printf("\tbp logical: %10llu avg: %6llu\n",
3426 (u_longlong_t)tzb->zb_lsize,
3427 (u_longlong_t)(tzb->zb_lsize / tzb->zb_count));
3428 (void) printf("\tbp physical: %10llu avg:"
3429 " %6llu compression: %6.2f\n",
3430 (u_longlong_t)tzb->zb_psize,
3431 (u_longlong_t)(tzb->zb_psize / tzb->zb_count),
3432 (double)tzb->zb_lsize / tzb->zb_psize);
3433 (void) printf("\tbp allocated: %10llu avg:"
3434 " %6llu compression: %6.2f\n",
3435 (u_longlong_t)tzb->zb_asize,
3436 (u_longlong_t)(tzb->zb_asize / tzb->zb_count),
3437 (double)tzb->zb_lsize / tzb->zb_asize);
3438 (void) printf("\tbp deduped: %10llu ref>1:"
3439 " %6llu deduplication: %6.2f\n",
3440 (u_longlong_t)zcb.zcb_dedup_asize,
3441 (u_longlong_t)zcb.zcb_dedup_blocks,
3442 (double)zcb.zcb_dedup_asize / tzb->zb_asize + 1.0);
3443 (void) printf("\tSPA allocated: %10llu used: %5.2f%%\n",
3444 (u_longlong_t)norm_alloc, 100.0 * norm_alloc / norm_space);
3446 for (i = 0; i < NUM_BP_EMBEDDED_TYPES; i++) {
3447 if (zcb.zcb_embedded_blocks[i] == 0)
3449 (void) printf("\n");
3450 (void) printf("\tadditional, non-pointer bps of type %u: "
3452 i, (u_longlong_t)zcb.zcb_embedded_blocks[i]);
3454 if (dump_opt['b'] >= 3) {
3455 (void) printf("\t number of (compressed) bytes: "
3457 dump_histogram(zcb.zcb_embedded_histogram[i],
3458 sizeof (zcb.zcb_embedded_histogram[i]) /
3459 sizeof (zcb.zcb_embedded_histogram[i][0]), 0);
3463 if (tzb->zb_ditto_samevdev != 0) {
3464 (void) printf("\tDittoed blocks on same vdev: %llu\n",
3465 (longlong_t)tzb->zb_ditto_samevdev);
3468 if (dump_opt['b'] >= 2) {
3470 (void) printf("\nBlocks\tLSIZE\tPSIZE\tASIZE"
3471 "\t avg\t comp\t%%Total\tType\n");
3473 for (t = 0; t <= ZDB_OT_TOTAL; t++) {
3474 char csize[32], lsize[32], psize[32], asize[32];
3475 char avg[32], gang[32];
3478 if (t < DMU_OT_NUMTYPES)
3479 typename = dmu_ot[t].ot_name;
3481 typename = zdb_ot_extname[t - DMU_OT_NUMTYPES];
3483 if (zcb.zcb_type[ZB_TOTAL][t].zb_asize == 0) {
3484 (void) printf("%6s\t%5s\t%5s\t%5s"
3485 "\t%5s\t%5s\t%6s\t%s\n",
3497 for (l = ZB_TOTAL - 1; l >= -1; l--) {
3498 level = (l == -1 ? ZB_TOTAL : l);
3499 zb = &zcb.zcb_type[level][t];
3501 if (zb->zb_asize == 0)
3504 if (dump_opt['b'] < 3 && level != ZB_TOTAL)
3507 if (level == 0 && zb->zb_asize ==
3508 zcb.zcb_type[ZB_TOTAL][t].zb_asize)
3511 zdb_nicenum(zb->zb_count, csize);
3512 zdb_nicenum(zb->zb_lsize, lsize);
3513 zdb_nicenum(zb->zb_psize, psize);
3514 zdb_nicenum(zb->zb_asize, asize);
3515 zdb_nicenum(zb->zb_asize / zb->zb_count, avg);
3516 zdb_nicenum(zb->zb_gangs, gang);
3518 (void) printf("%6s\t%5s\t%5s\t%5s\t%5s"
3520 csize, lsize, psize, asize, avg,
3521 (double)zb->zb_lsize / zb->zb_psize,
3522 100.0 * zb->zb_asize / tzb->zb_asize);
3524 if (level == ZB_TOTAL)
3525 (void) printf("%s\n", typename);
3527 (void) printf(" L%d %s\n",
3530 if (dump_opt['b'] >= 3 && zb->zb_gangs > 0) {
3531 (void) printf("\t number of ganged "
3532 "blocks: %s\n", gang);
3535 if (dump_opt['b'] >= 4) {
3536 (void) printf("psize "
3537 "(in 512-byte sectors): "
3538 "number of blocks\n");
3539 dump_histogram(zb->zb_psize_histogram,
3540 PSIZE_HISTO_SIZE, 0);
3546 (void) printf("\n");
3551 if (zcb.zcb_haderrors)
3557 typedef struct zdb_ddt_entry {
3559 uint64_t zdde_ref_blocks;
3560 uint64_t zdde_ref_lsize;
3561 uint64_t zdde_ref_psize;
3562 uint64_t zdde_ref_dsize;
3563 avl_node_t zdde_node;
3568 zdb_ddt_add_cb(spa_t *spa, zilog_t *zilog, const blkptr_t *bp,
3569 const zbookmark_phys_t *zb, const dnode_phys_t *dnp, void *arg)
3571 avl_tree_t *t = arg;
3573 zdb_ddt_entry_t *zdde, zdde_search;
3575 if (bp == NULL || BP_IS_HOLE(bp) || BP_IS_EMBEDDED(bp))
3578 if (dump_opt['S'] > 1 && zb->zb_level == ZB_ROOT_LEVEL) {
3579 (void) printf("traversing objset %llu, %llu objects, "
3580 "%lu blocks so far\n",
3581 (u_longlong_t)zb->zb_objset,
3582 (u_longlong_t)BP_GET_FILL(bp),
3586 if (BP_IS_HOLE(bp) || BP_GET_CHECKSUM(bp) == ZIO_CHECKSUM_OFF ||
3587 BP_GET_LEVEL(bp) > 0 || DMU_OT_IS_METADATA(BP_GET_TYPE(bp)))
3590 ddt_key_fill(&zdde_search.zdde_key, bp);
3592 zdde = avl_find(t, &zdde_search, &where);
3595 zdde = umem_zalloc(sizeof (*zdde), UMEM_NOFAIL);
3596 zdde->zdde_key = zdde_search.zdde_key;
3597 avl_insert(t, zdde, where);
3600 zdde->zdde_ref_blocks += 1;
3601 zdde->zdde_ref_lsize += BP_GET_LSIZE(bp);
3602 zdde->zdde_ref_psize += BP_GET_PSIZE(bp);
3603 zdde->zdde_ref_dsize += bp_get_dsize_sync(spa, bp);
3609 dump_simulated_ddt(spa_t *spa)
3612 void *cookie = NULL;
3613 zdb_ddt_entry_t *zdde;
3614 ddt_histogram_t ddh_total;
3615 ddt_stat_t dds_total;
3617 bzero(&ddh_total, sizeof (ddt_histogram_t));
3618 bzero(&dds_total, sizeof (ddt_stat_t));
3620 avl_create(&t, ddt_entry_compare,
3621 sizeof (zdb_ddt_entry_t), offsetof(zdb_ddt_entry_t, zdde_node));
3623 spa_config_enter(spa, SCL_CONFIG, FTAG, RW_READER);
3625 (void) traverse_pool(spa, 0, TRAVERSE_PRE | TRAVERSE_PREFETCH_METADATA,
3626 zdb_ddt_add_cb, &t);
3628 spa_config_exit(spa, SCL_CONFIG, FTAG);
3630 while ((zdde = avl_destroy_nodes(&t, &cookie)) != NULL) {
3632 uint64_t refcnt = zdde->zdde_ref_blocks;
3633 ASSERT(refcnt != 0);
3635 dds.dds_blocks = zdde->zdde_ref_blocks / refcnt;
3636 dds.dds_lsize = zdde->zdde_ref_lsize / refcnt;
3637 dds.dds_psize = zdde->zdde_ref_psize / refcnt;
3638 dds.dds_dsize = zdde->zdde_ref_dsize / refcnt;
3640 dds.dds_ref_blocks = zdde->zdde_ref_blocks;
3641 dds.dds_ref_lsize = zdde->zdde_ref_lsize;
3642 dds.dds_ref_psize = zdde->zdde_ref_psize;
3643 dds.dds_ref_dsize = zdde->zdde_ref_dsize;
3645 ddt_stat_add(&ddh_total.ddh_stat[highbit64(refcnt) - 1],
3648 umem_free(zdde, sizeof (*zdde));
3653 ddt_histogram_stat(&dds_total, &ddh_total);
3655 (void) printf("Simulated DDT histogram:\n");
3657 zpool_dump_ddt(&dds_total, &ddh_total);
3659 dump_dedup_ratio(&dds_total);
3663 dump_zpool(spa_t *spa)
3665 dsl_pool_t *dp = spa_get_dsl(spa);
3668 if (dump_opt['S']) {
3669 dump_simulated_ddt(spa);
3673 if (!dump_opt['e'] && dump_opt['C'] > 1) {
3674 (void) printf("\nCached configuration:\n");
3675 dump_nvlist(spa->spa_config, 8);
3682 dump_uberblock(&spa->spa_uberblock, "\nUberblock:\n", "\n");
3687 if (dump_opt['d'] > 2 || dump_opt['m'])
3688 dump_metaslabs(spa);
3690 dump_metaslab_groups(spa);
3692 if (dump_opt['d'] || dump_opt['i']) {
3695 dump_dir(dp->dp_meta_objset);
3696 if (dump_opt['d'] >= 3) {
3697 dump_full_bpobj(&spa->spa_deferred_bpobj,
3698 "Deferred frees", 0);
3699 if (spa_version(spa) >= SPA_VERSION_DEADLISTS) {
3701 &spa->spa_dsl_pool->dp_free_bpobj,
3702 "Pool snapshot frees", 0);
3705 if (spa_feature_is_active(spa,
3706 SPA_FEATURE_ASYNC_DESTROY)) {
3707 dump_bptree(spa->spa_meta_objset,
3708 spa->spa_dsl_pool->dp_bptree_obj,
3709 "Pool dataset frees");
3711 dump_dtl(spa->spa_root_vdev, 0);
3713 (void) dmu_objset_find(spa_name(spa), dump_one_dir,
3714 NULL, DS_FIND_SNAPSHOTS | DS_FIND_CHILDREN);
3716 for (f = 0; f < SPA_FEATURES; f++) {
3719 if (!(spa_feature_table[f].fi_flags &
3720 ZFEATURE_FLAG_PER_DATASET) ||
3721 !spa_feature_is_enabled(spa, f)) {
3722 ASSERT0(dataset_feature_count[f]);
3725 if (feature_get_refcount(spa, &spa_feature_table[f],
3726 &refcount) == ENOTSUP)
3728 if (dataset_feature_count[f] != refcount) {
3729 (void) printf("%s feature refcount mismatch: "
3730 "%lld datasets != %lld refcount\n",
3731 spa_feature_table[f].fi_uname,
3732 (longlong_t)dataset_feature_count[f],
3733 (longlong_t)refcount);
3736 (void) printf("Verified %s feature refcount "
3737 "of %llu is correct\n",
3738 spa_feature_table[f].fi_uname,
3739 (longlong_t)refcount);
3743 if (rc == 0 && (dump_opt['b'] || dump_opt['c']))
3744 rc = dump_block_stats(spa);
3747 rc = verify_spacemap_refcounts(spa);
3750 show_pool_stats(spa);
3756 dump_debug_buffer();
3761 #define ZDB_FLAG_CHECKSUM 0x0001
3762 #define ZDB_FLAG_DECOMPRESS 0x0002
3763 #define ZDB_FLAG_BSWAP 0x0004
3764 #define ZDB_FLAG_GBH 0x0008
3765 #define ZDB_FLAG_INDIRECT 0x0010
3766 #define ZDB_FLAG_PHYS 0x0020
3767 #define ZDB_FLAG_RAW 0x0040
3768 #define ZDB_FLAG_PRINT_BLKPTR 0x0080
3773 zdb_print_blkptr(blkptr_t *bp, int flags)
3775 char blkbuf[BP_SPRINTF_LEN];
3777 if (flags & ZDB_FLAG_BSWAP)
3778 byteswap_uint64_array((void *)bp, sizeof (blkptr_t));
3780 snprintf_blkptr(blkbuf, sizeof (blkbuf), bp);
3781 (void) printf("%s\n", blkbuf);
3785 zdb_dump_indirect(blkptr_t *bp, int nbps, int flags)
3789 for (i = 0; i < nbps; i++)
3790 zdb_print_blkptr(&bp[i], flags);
3794 zdb_dump_gbh(void *buf, int flags)
3796 zdb_dump_indirect((blkptr_t *)buf, SPA_GBH_NBLKPTRS, flags);
3800 zdb_dump_block_raw(void *buf, uint64_t size, int flags)
3802 if (flags & ZDB_FLAG_BSWAP)
3803 byteswap_uint64_array(buf, size);
3804 VERIFY(write(fileno(stdout), buf, size) == size);
3808 zdb_dump_block(char *label, void *buf, uint64_t size, int flags)
3810 uint64_t *d = (uint64_t *)buf;
3811 int nwords = size / sizeof (uint64_t);
3812 int do_bswap = !!(flags & ZDB_FLAG_BSWAP);
3818 hdr = " 7 6 5 4 3 2 1 0 f e d c b a 9 8";
3820 hdr = " 0 1 2 3 4 5 6 7 8 9 a b c d e f";
3822 (void) printf("\n%s\n%6s %s 0123456789abcdef\n", label, "", hdr);
3824 #ifdef _LITTLE_ENDIAN
3825 /* correct the endianness */
3826 do_bswap = !do_bswap;
3828 for (i = 0; i < nwords; i += 2) {
3829 (void) printf("%06llx: %016llx %016llx ",
3830 (u_longlong_t)(i * sizeof (uint64_t)),
3831 (u_longlong_t)(do_bswap ? BSWAP_64(d[i]) : d[i]),
3832 (u_longlong_t)(do_bswap ? BSWAP_64(d[i + 1]) : d[i + 1]));
3835 for (j = 0; j < 2 * sizeof (uint64_t); j++)
3836 (void) printf("%c", isprint(c[j]) ? c[j] : '.');
3837 (void) printf("\n");
3842 * There are two acceptable formats:
3843 * leaf_name - For example: c1t0d0 or /tmp/ztest.0a
3844 * child[.child]* - For example: 0.1.1
3846 * The second form can be used to specify arbitrary vdevs anywhere
3847 * in the hierarchy. For example, in a pool with a mirror of
3848 * RAID-Zs, you can specify either RAID-Z vdev with 0.0 or 0.1 .
3851 zdb_vdev_lookup(vdev_t *vdev, char *path)
3859 /* First, assume the x.x.x.x format */
3860 i = (int)strtoul(path, &s, 10);
3861 if (s == path || (s && *s != '.' && *s != '\0'))
3863 if (i < 0 || i >= vdev->vdev_children)
3866 vdev = vdev->vdev_child[i];
3867 if (s && *s == '\0')
3869 return (zdb_vdev_lookup(vdev, s+1));
3872 for (i = 0; i < vdev->vdev_children; i++) {
3873 vdev_t *vc = vdev->vdev_child[i];
3875 if (vc->vdev_path == NULL) {
3876 vc = zdb_vdev_lookup(vc, path);
3883 p = strrchr(vc->vdev_path, '/');
3884 p = p ? p + 1 : vc->vdev_path;
3885 q = &vc->vdev_path[strlen(vc->vdev_path) - 2];
3887 if (strcmp(vc->vdev_path, path) == 0)
3889 if (strcmp(p, path) == 0)
3891 if (strcmp(q, "s0") == 0 && strncmp(p, path, q - p) == 0)
3899 * Read a block from a pool and print it out. The syntax of the
3900 * block descriptor is:
3902 * pool:vdev_specifier:offset:size[:flags]
3904 * pool - The name of the pool you wish to read from
3905 * vdev_specifier - Which vdev (see comment for zdb_vdev_lookup)
3906 * offset - offset, in hex, in bytes
3907 * size - Amount of data to read, in hex, in bytes
3908 * flags - A string of characters specifying options
3909 * b: Decode a blkptr at given offset within block
3910 * *c: Calculate and display checksums
3911 * d: Decompress data before dumping
3912 * e: Byteswap data before dumping
3913 * g: Display data as a gang block header
3914 * i: Display as an indirect block
3915 * p: Do I/O to physical offset
3916 * r: Dump raw data to stdout
3918 * * = not yet implemented
3921 zdb_read_block(char *thing, spa_t *spa)
3923 blkptr_t blk, *bp = &blk;
3924 dva_t *dva = bp->blk_dva;
3926 uint64_t offset = 0, size = 0, psize = 0, lsize = 0, blkptr_offset = 0;
3931 char *s, *p, *dup, *vdev, *flagstr;
3933 boolean_t borrowed = B_FALSE;
3935 dup = strdup(thing);
3936 s = strtok(dup, ":");
3938 s = strtok(NULL, ":");
3939 offset = strtoull(s ? s : "", NULL, 16);
3940 s = strtok(NULL, ":");
3941 size = strtoull(s ? s : "", NULL, 16);
3942 s = strtok(NULL, ":");
3943 flagstr = s ? s : "";
3947 s = "size must not be zero";
3948 if (!IS_P2ALIGNED(size, DEV_BSIZE))
3949 s = "size must be a multiple of sector size";
3950 if (!IS_P2ALIGNED(offset, DEV_BSIZE))
3951 s = "offset must be a multiple of sector size";
3953 (void) printf("Invalid block specifier: %s - %s\n", thing, s);
3958 for (s = strtok(flagstr, ":"); s; s = strtok(NULL, ":")) {
3959 for (i = 0; flagstr[i]; i++) {
3960 int bit = flagbits[(uchar_t)flagstr[i]];
3963 (void) printf("***Invalid flag: %c\n",
3969 /* If it's not something with an argument, keep going */
3970 if ((bit & (ZDB_FLAG_CHECKSUM |
3971 ZDB_FLAG_PRINT_BLKPTR)) == 0)
3974 p = &flagstr[i + 1];
3975 if (bit == ZDB_FLAG_PRINT_BLKPTR) {
3976 blkptr_offset = strtoull(p, &p, 16);
3977 i = p - &flagstr[i + 1];
3979 if (*p != ':' && *p != '\0') {
3980 (void) printf("***Invalid flag arg: '%s'\n", s);
3987 vd = zdb_vdev_lookup(spa->spa_root_vdev, vdev);
3989 (void) printf("***Invalid vdev: %s\n", vdev);
3994 (void) fprintf(stderr, "Found vdev: %s\n",
3997 (void) fprintf(stderr, "Found vdev type: %s\n",
3998 vd->vdev_ops->vdev_op_type);
4004 pabd = abd_alloc_for_io(SPA_MAXBLOCKSIZE, B_FALSE);
4005 lbuf = umem_alloc(SPA_MAXBLOCKSIZE, UMEM_NOFAIL);
4009 DVA_SET_VDEV(&dva[0], vd->vdev_id);
4010 DVA_SET_OFFSET(&dva[0], offset);
4011 DVA_SET_GANG(&dva[0], !!(flags & ZDB_FLAG_GBH));
4012 DVA_SET_ASIZE(&dva[0], vdev_psize_to_asize(vd, psize));
4014 BP_SET_BIRTH(bp, TXG_INITIAL, TXG_INITIAL);
4016 BP_SET_LSIZE(bp, lsize);
4017 BP_SET_PSIZE(bp, psize);
4018 BP_SET_COMPRESS(bp, ZIO_COMPRESS_OFF);
4019 BP_SET_CHECKSUM(bp, ZIO_CHECKSUM_OFF);
4020 BP_SET_TYPE(bp, DMU_OT_NONE);
4021 BP_SET_LEVEL(bp, 0);
4022 BP_SET_DEDUP(bp, 0);
4023 BP_SET_BYTEORDER(bp, ZFS_HOST_BYTEORDER);
4025 spa_config_enter(spa, SCL_STATE, FTAG, RW_READER);
4026 zio = zio_root(spa, NULL, NULL, 0);
4028 if (vd == vd->vdev_top) {
4030 * Treat this as a normal block read.
4032 zio_nowait(zio_read(zio, spa, bp, pabd, psize, NULL, NULL,
4033 ZIO_PRIORITY_SYNC_READ,
4034 ZIO_FLAG_CANFAIL | ZIO_FLAG_RAW, NULL));
4037 * Treat this as a vdev child I/O.
4039 zio_nowait(zio_vdev_child_io(zio, bp, vd, offset, pabd,
4040 psize, ZIO_TYPE_READ, ZIO_PRIORITY_SYNC_READ,
4041 ZIO_FLAG_DONT_CACHE | ZIO_FLAG_DONT_QUEUE |
4042 ZIO_FLAG_DONT_PROPAGATE | ZIO_FLAG_DONT_RETRY |
4043 ZIO_FLAG_CANFAIL | ZIO_FLAG_RAW, NULL, NULL));
4046 error = zio_wait(zio);
4047 spa_config_exit(spa, SCL_STATE, FTAG);
4050 (void) printf("Read of %s failed, error: %d\n", thing, error);
4054 if (flags & ZDB_FLAG_DECOMPRESS) {
4056 * We don't know how the data was compressed, so just try
4057 * every decompress function at every inflated blocksize.
4059 enum zio_compress c;
4060 void *lbuf2 = umem_alloc(SPA_MAXBLOCKSIZE, UMEM_NOFAIL);
4063 * XXX - On the one hand, with SPA_MAXBLOCKSIZE at 16MB,
4064 * this could take a while and we should let the user know
4065 * we are not stuck. On the other hand, printing progress
4066 * info gets old after a while. What to do?
4068 for (lsize = psize + SPA_MINBLOCKSIZE;
4069 lsize <= SPA_MAXBLOCKSIZE; lsize += SPA_MINBLOCKSIZE) {
4070 for (c = 0; c < ZIO_COMPRESS_FUNCTIONS; c++) {
4072 * ZLE can easily decompress non zle stream.
4073 * So have an option to disable it.
4075 if (c == ZIO_COMPRESS_ZLE &&
4076 getenv("ZDB_NO_ZLE"))
4079 (void) fprintf(stderr,
4080 "Trying %05llx -> %05llx (%s)\n",
4081 (u_longlong_t)psize, (u_longlong_t)lsize,
4082 zio_compress_table[c].ci_name);
4085 * We randomize lbuf2, and decompress to both
4086 * lbuf and lbuf2. This way, we will know if
4087 * decompression fill exactly to lsize.
4089 VERIFY0(random_get_pseudo_bytes(lbuf2, lsize));
4091 if (zio_decompress_data(c, pabd,
4092 lbuf, psize, lsize) == 0 &&
4093 zio_decompress_data(c, pabd,
4094 lbuf2, psize, lsize) == 0 &&
4095 bcmp(lbuf, lbuf2, lsize) == 0)
4098 if (c != ZIO_COMPRESS_FUNCTIONS)
4101 umem_free(lbuf2, SPA_MAXBLOCKSIZE);
4103 if (lsize > SPA_MAXBLOCKSIZE) {
4104 (void) printf("Decompress of %s failed\n", thing);
4111 buf = abd_borrow_buf_copy(pabd, size);
4115 if (flags & ZDB_FLAG_PRINT_BLKPTR)
4116 zdb_print_blkptr((blkptr_t *)(void *)
4117 ((uintptr_t)buf + (uintptr_t)blkptr_offset), flags);
4118 else if (flags & ZDB_FLAG_RAW)
4119 zdb_dump_block_raw(buf, size, flags);
4120 else if (flags & ZDB_FLAG_INDIRECT)
4121 zdb_dump_indirect((blkptr_t *)buf, size / sizeof (blkptr_t),
4123 else if (flags & ZDB_FLAG_GBH)
4124 zdb_dump_gbh(buf, flags);
4126 zdb_dump_block(thing, buf, size, flags);
4129 abd_return_buf_copy(pabd, buf, size);
4133 umem_free(lbuf, SPA_MAXBLOCKSIZE);
4138 zdb_embedded_block(char *thing)
4141 unsigned long long *words = (void *)&bp;
4145 buf = umem_alloc(SPA_MAXBLOCKSIZE, UMEM_NOFAIL);
4147 bzero(&bp, sizeof (bp));
4148 err = sscanf(thing, "%llx:%llx:%llx:%llx:%llx:%llx:%llx:%llx:"
4149 "%llx:%llx:%llx:%llx:%llx:%llx:%llx:%llx",
4150 words + 0, words + 1, words + 2, words + 3,
4151 words + 4, words + 5, words + 6, words + 7,
4152 words + 8, words + 9, words + 10, words + 11,
4153 words + 12, words + 13, words + 14, words + 15);
4155 (void) printf("invalid input format\n");
4158 ASSERT3U(BPE_GET_LSIZE(&bp), <=, SPA_MAXBLOCKSIZE);
4159 err = decode_embedded_bp(&bp, buf, BPE_GET_LSIZE(&bp));
4161 (void) printf("decode failed: %u\n", err);
4164 zdb_dump_block_raw(buf, BPE_GET_LSIZE(&bp), 0);
4165 umem_free(buf, SPA_MAXBLOCKSIZE);
4169 main(int argc, char **argv)
4172 struct rlimit rl = { 1024, 1024 };
4174 objset_t *os = NULL;
4178 char **searchdirs = NULL;
4181 nvlist_t *policy = NULL;
4182 uint64_t max_txg = UINT64_MAX;
4183 int flags = ZFS_IMPORT_MISSING_LOG;
4184 int rewind = ZPOOL_NEVER_REWIND;
4185 char *spa_config_path_env;
4186 boolean_t target_is_spa = B_TRUE;
4188 (void) setrlimit(RLIMIT_NOFILE, &rl);
4189 (void) enable_extended_FILE_stdio(-1, -1);
4191 dprintf_setup(&argc, argv);
4194 * If there is an environment variable SPA_CONFIG_PATH it overrides
4195 * default spa_config_path setting. If -U flag is specified it will
4196 * override this environment variable settings once again.
4198 spa_config_path_env = getenv("SPA_CONFIG_PATH");
4199 if (spa_config_path_env != NULL)
4200 spa_config_path = spa_config_path_env;
4202 while ((c = getopt(argc, argv,
4203 "AbcCdDeEFGhiI:lLmMo:Op:PqRsSt:uU:vVx:X")) != -1) {
4234 /* NB: Sort single match options below. */
4236 max_inflight = strtoull(optarg, NULL, 0);
4237 if (max_inflight == 0) {
4238 (void) fprintf(stderr, "maximum number "
4239 "of inflight I/Os must be greater "
4245 error = set_global_var(optarg);
4250 if (searchdirs == NULL) {
4251 searchdirs = umem_alloc(sizeof (char *),
4254 char **tmp = umem_alloc((nsearch + 1) *
4255 sizeof (char *), UMEM_NOFAIL);
4256 bcopy(searchdirs, tmp, nsearch *
4258 umem_free(searchdirs,
4259 nsearch * sizeof (char *));
4262 searchdirs[nsearch++] = optarg;
4265 max_txg = strtoull(optarg, NULL, 0);
4266 if (max_txg < TXG_INITIAL) {
4267 (void) fprintf(stderr, "incorrect txg "
4268 "specified: %s\n", optarg);
4273 spa_config_path = optarg;
4274 if (spa_config_path[0] != '/') {
4275 (void) fprintf(stderr,
4276 "cachefile must be an absolute path "
4277 "(i.e. start with a slash)\n");
4285 flags = ZFS_IMPORT_VERBATIM;
4288 vn_dumpdir = optarg;
4296 if (!dump_opt['e'] && searchdirs != NULL) {
4297 (void) fprintf(stderr, "-p option requires use of -e\n");
4303 * ZDB does not typically re-read blocks; therefore limit the ARC
4304 * to 256 MB, which can be used entirely for metadata.
4306 zfs_arc_max = zfs_arc_meta_limit = 256 * 1024 * 1024;
4310 * "zdb -c" uses checksum-verifying scrub i/os which are async reads.
4311 * "zdb -b" uses traversal prefetch which uses async reads.
4312 * For good performance, let several of them be active at once.
4314 zfs_vdev_async_read_max_active = 10;
4317 * Disable reference tracking for better performance.
4319 reference_tracking_enable = B_FALSE;
4322 if ((g_zfs = libzfs_init()) == NULL) {
4323 (void) fprintf(stderr, "%s", libzfs_error_init(errno));
4328 verbose = MAX(verbose, 1);
4330 for (c = 0; c < 256; c++) {
4331 if (dump_all && strchr("AeEFlLOPRSX", c) == NULL)
4334 dump_opt[c] += verbose;
4337 aok = (dump_opt['A'] == 1) || (dump_opt['A'] > 2);
4338 zfs_recover = (dump_opt['A'] > 1);
4343 if (argc < 2 && dump_opt['R'])
4346 if (dump_opt['E']) {
4349 zdb_embedded_block(argv[0]);
4354 if (!dump_opt['e'] && dump_opt['C']) {
4355 dump_cachefile(spa_config_path);
4362 return (dump_label(argv[0]));
4364 if (dump_opt['O']) {
4367 dump_opt['v'] = verbose + 3;
4368 return (dump_path(argv[0], argv[1]));
4371 if (dump_opt['X'] || dump_opt['F'])
4372 rewind = ZPOOL_DO_REWIND |
4373 (dump_opt['X'] ? ZPOOL_EXTREME_REWIND : 0);
4375 if (nvlist_alloc(&policy, NV_UNIQUE_NAME_TYPE, 0) != 0 ||
4376 nvlist_add_uint64(policy, ZPOOL_REWIND_REQUEST_TXG, max_txg) != 0 ||
4377 nvlist_add_uint32(policy, ZPOOL_REWIND_REQUEST, rewind) != 0)
4378 fatal("internal error: %s", strerror(ENOMEM));
4383 if (dump_opt['e']) {
4384 importargs_t args = { 0 };
4385 nvlist_t *cfg = NULL;
4387 args.paths = nsearch;
4388 args.path = searchdirs;
4389 args.can_be_active = B_TRUE;
4391 error = zpool_tryimport(g_zfs, target, &cfg, &args);
4393 if (nvlist_add_nvlist(cfg,
4394 ZPOOL_REWIND_POLICY, policy) != 0) {
4395 fatal("can't open '%s': %s",
4396 target, strerror(ENOMEM));
4400 * Disable the activity check to allow examination of
4403 if (dump_opt['C'] > 1) {
4404 (void) printf("\nConfiguration for import:\n");
4405 dump_nvlist(cfg, 8);
4407 error = spa_import(target, cfg, NULL,
4408 flags | ZFS_IMPORT_SKIP_MMP);
4412 if (strpbrk(target, "/@") != NULL) {
4415 target_is_spa = B_FALSE;
4416 targetlen = strlen(target);
4417 if (targetlen && target[targetlen - 1] == '/')
4418 target[targetlen - 1] = '\0';
4422 if (target_is_spa || dump_opt['R']) {
4424 * Disable the activity check to allow examination of
4427 mutex_enter(&spa_namespace_lock);
4428 if ((spa = spa_lookup(target)) != NULL) {
4429 spa->spa_import_flags |= ZFS_IMPORT_SKIP_MMP;
4431 mutex_exit(&spa_namespace_lock);
4433 error = spa_open_rewind(target, &spa, FTAG, policy,
4437 * If we're missing the log device then
4438 * try opening the pool after clearing the
4441 mutex_enter(&spa_namespace_lock);
4442 if ((spa = spa_lookup(target)) != NULL &&
4443 spa->spa_log_state == SPA_LOG_MISSING) {
4444 spa->spa_log_state = SPA_LOG_CLEAR;
4447 mutex_exit(&spa_namespace_lock);
4450 error = spa_open_rewind(target, &spa,
4451 FTAG, policy, NULL);
4455 error = open_objset(target, DMU_OST_ANY, FTAG, &os);
4458 nvlist_free(policy);
4461 fatal("can't open '%s': %s", target, strerror(error));
4465 if (!dump_opt['R']) {
4467 zopt_objects = argc;
4468 zopt_object = calloc(zopt_objects, sizeof (uint64_t));
4469 for (i = 0; i < zopt_objects; i++) {
4471 zopt_object[i] = strtoull(argv[i], NULL, 0);
4472 if (zopt_object[i] == 0 && errno != 0)
4473 fatal("bad number %s: %s",
4474 argv[i], strerror(errno));
4479 } else if (zopt_objects > 0 && !dump_opt['m']) {
4480 dump_dir(spa->spa_meta_objset);
4485 flagbits['b'] = ZDB_FLAG_PRINT_BLKPTR;
4486 flagbits['c'] = ZDB_FLAG_CHECKSUM;
4487 flagbits['d'] = ZDB_FLAG_DECOMPRESS;
4488 flagbits['e'] = ZDB_FLAG_BSWAP;
4489 flagbits['g'] = ZDB_FLAG_GBH;
4490 flagbits['i'] = ZDB_FLAG_INDIRECT;
4491 flagbits['p'] = ZDB_FLAG_PHYS;
4492 flagbits['r'] = ZDB_FLAG_RAW;
4494 for (i = 0; i < argc; i++)
4495 zdb_read_block(argv[i], spa);
4499 close_objset(os, FTAG);
4501 spa_close(spa, FTAG);
4503 fuid_table_destroy();
4505 dump_debug_buffer();