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) 2012, 2014 by Delphix. All rights reserved.
29 #include <stdio_ext.h>
32 #include <sys/zfs_context.h>
34 #include <sys/spa_impl.h>
37 #include <sys/fs/zfs.h>
38 #include <sys/zfs_znode.h>
39 #include <sys/zfs_sa.h>
41 #include <sys/sa_impl.h>
43 #include <sys/vdev_impl.h>
44 #include <sys/metaslab_impl.h>
45 #include <sys/dmu_objset.h>
46 #include <sys/dsl_dir.h>
47 #include <sys/dsl_dataset.h>
48 #include <sys/dsl_pool.h>
51 #include <sys/zil_impl.h>
53 #include <sys/resource.h>
54 #include <sys/dmu_traverse.h>
55 #include <sys/zio_checksum.h>
56 #include <sys/zio_compress.h>
57 #include <sys/zfs_fuid.h>
60 #include <sys/zfeature.h>
61 #include <zfs_comutil.h>
66 #define ZDB_COMPRESS_NAME(idx) ((idx) < ZIO_COMPRESS_FUNCTIONS ? \
67 zio_compress_table[(idx)].ci_name : "UNKNOWN")
68 #define ZDB_CHECKSUM_NAME(idx) ((idx) < ZIO_CHECKSUM_FUNCTIONS ? \
69 zio_checksum_table[(idx)].ci_name : "UNKNOWN")
70 #define ZDB_OT_NAME(idx) ((idx) < DMU_OT_NUMTYPES ? \
71 dmu_ot[(idx)].ot_name : DMU_OT_IS_VALID(idx) ? \
72 dmu_ot_byteswap[DMU_OT_BYTESWAP(idx)].ob_name : "UNKNOWN")
73 #define ZDB_OT_TYPE(idx) ((idx) < DMU_OT_NUMTYPES ? (idx) : \
74 (((idx) == DMU_OTN_ZAP_DATA || (idx) == DMU_OTN_ZAP_METADATA) ? \
75 DMU_OT_ZAP_OTHER : DMU_OT_NUMTYPES))
78 extern int zfs_recover;
83 const char cmdname[] = "zdb";
84 uint8_t dump_opt[256];
86 typedef void object_viewer_t(objset_t *, uint64_t, void *data, size_t size);
88 extern void dump_intent_log(zilog_t *);
89 uint64_t *zopt_object = NULL;
91 libzfs_handle_t *g_zfs;
92 uint64_t max_inflight = 200;
95 * These libumem hooks provide a reasonable set of defaults for the allocator's
96 * debugging facilities.
101 return ("default,verbose"); /* $UMEM_DEBUG setting */
105 _umem_logging_init(void)
107 return ("fail,contents"); /* $UMEM_LOGGING setting */
113 (void) fprintf(stderr,
114 "Usage: %s [-CumdibcsDvhLXFPA] [-t txg] [-e [-p path...]] "
115 "[-U config] [-M inflight I/Os] poolname [object...]\n"
116 " %s [-divPA] [-e -p path...] [-U config] dataset "
118 " %s -m [-LXFPA] [-t txg] [-e [-p path...]] [-U config] "
119 "poolname [vdev [metaslab...]]\n"
120 " %s -R [-A] [-e [-p path...]] poolname "
121 "vdev:offset:size[:flags]\n"
122 " %s -S [-PA] [-e [-p path...]] [-U config] poolname\n"
123 " %s -l [-uA] device\n"
124 " %s -C [-A] [-U config]\n\n",
125 cmdname, cmdname, cmdname, cmdname, cmdname, cmdname, cmdname);
127 (void) fprintf(stderr, " Dataset name must include at least one "
128 "separator character '/' or '@'\n");
129 (void) fprintf(stderr, " If dataset name is specified, only that "
130 "dataset is dumped\n");
131 (void) fprintf(stderr, " If object numbers are specified, only "
132 "those objects are dumped\n\n");
133 (void) fprintf(stderr, " Options to control amount of output:\n");
134 (void) fprintf(stderr, " -u uberblock\n");
135 (void) fprintf(stderr, " -d dataset(s)\n");
136 (void) fprintf(stderr, " -i intent logs\n");
137 (void) fprintf(stderr, " -C config (or cachefile if alone)\n");
138 (void) fprintf(stderr, " -h pool history\n");
139 (void) fprintf(stderr, " -b block statistics\n");
140 (void) fprintf(stderr, " -m metaslabs\n");
141 (void) fprintf(stderr, " -c checksum all metadata (twice for "
142 "all data) blocks\n");
143 (void) fprintf(stderr, " -s report stats on zdb's I/O\n");
144 (void) fprintf(stderr, " -D dedup statistics\n");
145 (void) fprintf(stderr, " -S simulate dedup to measure effect\n");
146 (void) fprintf(stderr, " -v verbose (applies to all others)\n");
147 (void) fprintf(stderr, " -l dump label contents\n");
148 (void) fprintf(stderr, " -L disable leak tracking (do not "
149 "load spacemaps)\n");
150 (void) fprintf(stderr, " -R read and display block from a "
152 (void) fprintf(stderr, " Below options are intended for use "
153 "with other options (except -l):\n");
154 (void) fprintf(stderr, " -A ignore assertions (-A), enable "
155 "panic recovery (-AA) or both (-AAA)\n");
156 (void) fprintf(stderr, " -F attempt automatic rewind within "
157 "safe range of transaction groups\n");
158 (void) fprintf(stderr, " -U <cachefile_path> -- use alternate "
160 (void) fprintf(stderr, " -X attempt extreme rewind (does not "
161 "work with dataset)\n");
162 (void) fprintf(stderr, " -e pool is exported/destroyed/"
163 "has altroot/not in a cachefile\n");
164 (void) fprintf(stderr, " -p <path> -- use one or more with "
165 "-e to specify path to vdev dir\n");
166 (void) fprintf(stderr, " -P print numbers in parseable form\n");
167 (void) fprintf(stderr, " -t <txg> -- highest txg to use when "
168 "searching for uberblocks\n");
169 (void) fprintf(stderr, " -M <number of inflight I/Os> -- "
170 "specify the maximum number of checksumming I/Os [default is 200]");
171 (void) fprintf(stderr, "Specify an option more than once (e.g. -bb) "
172 "to make only that option verbose\n");
173 (void) fprintf(stderr, "Default is to dump everything non-verbosely\n");
178 * Called for usage errors that are discovered after a call to spa_open(),
179 * dmu_bonus_hold(), or pool_match(). abort() is called for other errors.
183 fatal(const char *fmt, ...)
188 (void) fprintf(stderr, "%s: ", cmdname);
189 (void) vfprintf(stderr, fmt, ap);
191 (void) fprintf(stderr, "\n");
198 dump_packed_nvlist(objset_t *os, uint64_t object, void *data, size_t size)
201 size_t nvsize = *(uint64_t *)data;
202 char *packed = umem_alloc(nvsize, UMEM_NOFAIL);
204 VERIFY(0 == dmu_read(os, object, 0, nvsize, packed, DMU_READ_PREFETCH));
206 VERIFY(nvlist_unpack(packed, nvsize, &nv, 0) == 0);
208 umem_free(packed, nvsize);
217 dump_history_offsets(objset_t *os, uint64_t object, void *data, size_t size)
219 spa_history_phys_t *shp = data;
224 (void) printf("\t\tpool_create_len = %llu\n",
225 (u_longlong_t)shp->sh_pool_create_len);
226 (void) printf("\t\tphys_max_off = %llu\n",
227 (u_longlong_t)shp->sh_phys_max_off);
228 (void) printf("\t\tbof = %llu\n",
229 (u_longlong_t)shp->sh_bof);
230 (void) printf("\t\teof = %llu\n",
231 (u_longlong_t)shp->sh_eof);
232 (void) printf("\t\trecords_lost = %llu\n",
233 (u_longlong_t)shp->sh_records_lost);
237 zdb_nicenum(uint64_t num, char *buf)
240 (void) sprintf(buf, "%llu", (longlong_t)num);
245 const char histo_stars[] = "****************************************";
246 const int histo_width = sizeof (histo_stars) - 1;
249 dump_histogram(const uint64_t *histo, int size, int offset)
252 int minidx = size - 1;
256 for (i = 0; i < size; i++) {
259 if (histo[i] > 0 && i > maxidx)
261 if (histo[i] > 0 && i < minidx)
265 if (max < histo_width)
268 for (i = minidx; i <= maxidx; i++) {
269 (void) printf("\t\t\t%3u: %6llu %s\n",
270 i + offset, (u_longlong_t)histo[i],
271 &histo_stars[(max - histo[i]) * histo_width / max]);
276 dump_zap_stats(objset_t *os, uint64_t object)
281 error = zap_get_stats(os, object, &zs);
285 if (zs.zs_ptrtbl_len == 0) {
286 ASSERT(zs.zs_num_blocks == 1);
287 (void) printf("\tmicrozap: %llu bytes, %llu entries\n",
288 (u_longlong_t)zs.zs_blocksize,
289 (u_longlong_t)zs.zs_num_entries);
293 (void) printf("\tFat ZAP stats:\n");
295 (void) printf("\t\tPointer table:\n");
296 (void) printf("\t\t\t%llu elements\n",
297 (u_longlong_t)zs.zs_ptrtbl_len);
298 (void) printf("\t\t\tzt_blk: %llu\n",
299 (u_longlong_t)zs.zs_ptrtbl_zt_blk);
300 (void) printf("\t\t\tzt_numblks: %llu\n",
301 (u_longlong_t)zs.zs_ptrtbl_zt_numblks);
302 (void) printf("\t\t\tzt_shift: %llu\n",
303 (u_longlong_t)zs.zs_ptrtbl_zt_shift);
304 (void) printf("\t\t\tzt_blks_copied: %llu\n",
305 (u_longlong_t)zs.zs_ptrtbl_blks_copied);
306 (void) printf("\t\t\tzt_nextblk: %llu\n",
307 (u_longlong_t)zs.zs_ptrtbl_nextblk);
309 (void) printf("\t\tZAP entries: %llu\n",
310 (u_longlong_t)zs.zs_num_entries);
311 (void) printf("\t\tLeaf blocks: %llu\n",
312 (u_longlong_t)zs.zs_num_leafs);
313 (void) printf("\t\tTotal blocks: %llu\n",
314 (u_longlong_t)zs.zs_num_blocks);
315 (void) printf("\t\tzap_block_type: 0x%llx\n",
316 (u_longlong_t)zs.zs_block_type);
317 (void) printf("\t\tzap_magic: 0x%llx\n",
318 (u_longlong_t)zs.zs_magic);
319 (void) printf("\t\tzap_salt: 0x%llx\n",
320 (u_longlong_t)zs.zs_salt);
322 (void) printf("\t\tLeafs with 2^n pointers:\n");
323 dump_histogram(zs.zs_leafs_with_2n_pointers, ZAP_HISTOGRAM_SIZE, 0);
325 (void) printf("\t\tBlocks with n*5 entries:\n");
326 dump_histogram(zs.zs_blocks_with_n5_entries, ZAP_HISTOGRAM_SIZE, 0);
328 (void) printf("\t\tBlocks n/10 full:\n");
329 dump_histogram(zs.zs_blocks_n_tenths_full, ZAP_HISTOGRAM_SIZE, 0);
331 (void) printf("\t\tEntries with n chunks:\n");
332 dump_histogram(zs.zs_entries_using_n_chunks, ZAP_HISTOGRAM_SIZE, 0);
334 (void) printf("\t\tBuckets with n entries:\n");
335 dump_histogram(zs.zs_buckets_with_n_entries, ZAP_HISTOGRAM_SIZE, 0);
340 dump_none(objset_t *os, uint64_t object, void *data, size_t size)
346 dump_unknown(objset_t *os, uint64_t object, void *data, size_t size)
348 (void) printf("\tUNKNOWN OBJECT TYPE\n");
353 dump_uint8(objset_t *os, uint64_t object, void *data, size_t size)
359 dump_uint64(objset_t *os, uint64_t object, void *data, size_t size)
365 dump_zap(objset_t *os, uint64_t object, void *data, size_t size)
368 zap_attribute_t attr;
372 dump_zap_stats(os, object);
375 for (zap_cursor_init(&zc, os, object);
376 zap_cursor_retrieve(&zc, &attr) == 0;
377 zap_cursor_advance(&zc)) {
378 (void) printf("\t\t%s = ", attr.za_name);
379 if (attr.za_num_integers == 0) {
383 prop = umem_zalloc(attr.za_num_integers *
384 attr.za_integer_length, UMEM_NOFAIL);
385 (void) zap_lookup(os, object, attr.za_name,
386 attr.za_integer_length, attr.za_num_integers, prop);
387 if (attr.za_integer_length == 1) {
388 (void) printf("%s", (char *)prop);
390 for (i = 0; i < attr.za_num_integers; i++) {
391 switch (attr.za_integer_length) {
394 ((uint16_t *)prop)[i]);
398 ((uint32_t *)prop)[i]);
401 (void) printf("%lld ",
402 (u_longlong_t)((int64_t *)prop)[i]);
408 umem_free(prop, attr.za_num_integers * attr.za_integer_length);
410 zap_cursor_fini(&zc);
415 dump_ddt_zap(objset_t *os, uint64_t object, void *data, size_t size)
417 dump_zap_stats(os, object);
418 /* contents are printed elsewhere, properly decoded */
423 dump_sa_attrs(objset_t *os, uint64_t object, void *data, size_t size)
426 zap_attribute_t attr;
428 dump_zap_stats(os, object);
431 for (zap_cursor_init(&zc, os, object);
432 zap_cursor_retrieve(&zc, &attr) == 0;
433 zap_cursor_advance(&zc)) {
434 (void) printf("\t\t%s = ", attr.za_name);
435 if (attr.za_num_integers == 0) {
439 (void) printf(" %llx : [%d:%d:%d]\n",
440 (u_longlong_t)attr.za_first_integer,
441 (int)ATTR_LENGTH(attr.za_first_integer),
442 (int)ATTR_BSWAP(attr.za_first_integer),
443 (int)ATTR_NUM(attr.za_first_integer));
445 zap_cursor_fini(&zc);
450 dump_sa_layouts(objset_t *os, uint64_t object, void *data, size_t size)
453 zap_attribute_t attr;
454 uint16_t *layout_attrs;
457 dump_zap_stats(os, object);
460 for (zap_cursor_init(&zc, os, object);
461 zap_cursor_retrieve(&zc, &attr) == 0;
462 zap_cursor_advance(&zc)) {
463 (void) printf("\t\t%s = [", attr.za_name);
464 if (attr.za_num_integers == 0) {
469 VERIFY(attr.za_integer_length == 2);
470 layout_attrs = umem_zalloc(attr.za_num_integers *
471 attr.za_integer_length, UMEM_NOFAIL);
473 VERIFY(zap_lookup(os, object, attr.za_name,
474 attr.za_integer_length,
475 attr.za_num_integers, layout_attrs) == 0);
477 for (i = 0; i != attr.za_num_integers; i++)
478 (void) printf(" %d ", (int)layout_attrs[i]);
479 (void) printf("]\n");
480 umem_free(layout_attrs,
481 attr.za_num_integers * attr.za_integer_length);
483 zap_cursor_fini(&zc);
488 dump_zpldir(objset_t *os, uint64_t object, void *data, size_t size)
491 zap_attribute_t attr;
492 const char *typenames[] = {
493 /* 0 */ "not specified",
495 /* 2 */ "Character Device",
496 /* 3 */ "3 (invalid)",
498 /* 5 */ "5 (invalid)",
499 /* 6 */ "Block Device",
500 /* 7 */ "7 (invalid)",
501 /* 8 */ "Regular File",
502 /* 9 */ "9 (invalid)",
503 /* 10 */ "Symbolic Link",
504 /* 11 */ "11 (invalid)",
507 /* 14 */ "Event Port",
508 /* 15 */ "15 (invalid)",
511 dump_zap_stats(os, object);
514 for (zap_cursor_init(&zc, os, object);
515 zap_cursor_retrieve(&zc, &attr) == 0;
516 zap_cursor_advance(&zc)) {
517 (void) printf("\t\t%s = %lld (type: %s)\n",
518 attr.za_name, ZFS_DIRENT_OBJ(attr.za_first_integer),
519 typenames[ZFS_DIRENT_TYPE(attr.za_first_integer)]);
521 zap_cursor_fini(&zc);
525 get_dtl_refcount(vdev_t *vd)
529 if (vd->vdev_ops->vdev_op_leaf) {
530 space_map_t *sm = vd->vdev_dtl_sm;
533 sm->sm_dbuf->db_size == sizeof (space_map_phys_t))
538 for (int c = 0; c < vd->vdev_children; c++)
539 refcount += get_dtl_refcount(vd->vdev_child[c]);
544 get_metaslab_refcount(vdev_t *vd)
548 if (vd->vdev_top == vd) {
549 for (int m = 0; m < vd->vdev_ms_count; m++) {
550 space_map_t *sm = vd->vdev_ms[m]->ms_sm;
553 sm->sm_dbuf->db_size == sizeof (space_map_phys_t))
557 for (int c = 0; c < vd->vdev_children; c++)
558 refcount += get_metaslab_refcount(vd->vdev_child[c]);
564 verify_spacemap_refcounts(spa_t *spa)
566 uint64_t expected_refcount = 0;
567 uint64_t actual_refcount;
569 (void) feature_get_refcount(spa,
570 &spa_feature_table[SPA_FEATURE_SPACEMAP_HISTOGRAM],
572 actual_refcount = get_dtl_refcount(spa->spa_root_vdev);
573 actual_refcount += get_metaslab_refcount(spa->spa_root_vdev);
575 if (expected_refcount != actual_refcount) {
576 (void) printf("space map refcount mismatch: expected %lld != "
578 (longlong_t)expected_refcount,
579 (longlong_t)actual_refcount);
586 dump_spacemap(objset_t *os, space_map_t *sm)
588 uint64_t alloc, offset, entry;
589 char *ddata[] = { "ALLOC", "FREE", "CONDENSE", "INVALID",
590 "INVALID", "INVALID", "INVALID", "INVALID" };
596 * Print out the freelist entries in both encoded and decoded form.
599 for (offset = 0; offset < space_map_length(sm);
600 offset += sizeof (entry)) {
601 uint8_t mapshift = sm->sm_shift;
603 VERIFY0(dmu_read(os, space_map_object(sm), offset,
604 sizeof (entry), &entry, DMU_READ_PREFETCH));
605 if (SM_DEBUG_DECODE(entry)) {
607 (void) printf("\t [%6llu] %s: txg %llu, pass %llu\n",
608 (u_longlong_t)(offset / sizeof (entry)),
609 ddata[SM_DEBUG_ACTION_DECODE(entry)],
610 (u_longlong_t)SM_DEBUG_TXG_DECODE(entry),
611 (u_longlong_t)SM_DEBUG_SYNCPASS_DECODE(entry));
613 (void) printf("\t [%6llu] %c range:"
614 " %010llx-%010llx size: %06llx\n",
615 (u_longlong_t)(offset / sizeof (entry)),
616 SM_TYPE_DECODE(entry) == SM_ALLOC ? 'A' : 'F',
617 (u_longlong_t)((SM_OFFSET_DECODE(entry) <<
618 mapshift) + sm->sm_start),
619 (u_longlong_t)((SM_OFFSET_DECODE(entry) <<
620 mapshift) + sm->sm_start +
621 (SM_RUN_DECODE(entry) << mapshift)),
622 (u_longlong_t)(SM_RUN_DECODE(entry) << mapshift));
623 if (SM_TYPE_DECODE(entry) == SM_ALLOC)
624 alloc += SM_RUN_DECODE(entry) << mapshift;
626 alloc -= SM_RUN_DECODE(entry) << mapshift;
629 if (alloc != space_map_allocated(sm)) {
630 (void) printf("space_map_object alloc (%llu) INCONSISTENT "
631 "with space map summary (%llu)\n",
632 (u_longlong_t)space_map_allocated(sm), (u_longlong_t)alloc);
637 dump_metaslab_stats(metaslab_t *msp)
640 range_tree_t *rt = msp->ms_tree;
641 avl_tree_t *t = &msp->ms_size_tree;
642 int free_pct = range_tree_space(rt) * 100 / msp->ms_size;
644 zdb_nicenum(metaslab_block_maxsize(msp), maxbuf);
646 (void) printf("\t %25s %10lu %7s %6s %4s %4d%%\n",
647 "segments", avl_numnodes(t), "maxsize", maxbuf,
648 "freepct", free_pct);
649 (void) printf("\tIn-memory histogram:\n");
650 dump_histogram(rt->rt_histogram, RANGE_TREE_HISTOGRAM_SIZE, 0);
654 dump_metaslab(metaslab_t *msp)
656 vdev_t *vd = msp->ms_group->mg_vd;
657 spa_t *spa = vd->vdev_spa;
658 space_map_t *sm = msp->ms_sm;
661 zdb_nicenum(msp->ms_size - space_map_allocated(sm), freebuf);
664 "\tmetaslab %6llu offset %12llx spacemap %6llu free %5s\n",
665 (u_longlong_t)msp->ms_id, (u_longlong_t)msp->ms_start,
666 (u_longlong_t)space_map_object(sm), freebuf);
668 if (dump_opt['m'] > 2 && !dump_opt['L']) {
669 mutex_enter(&msp->ms_lock);
670 metaslab_load_wait(msp);
671 if (!msp->ms_loaded) {
672 VERIFY0(metaslab_load(msp));
673 range_tree_stat_verify(msp->ms_tree);
675 dump_metaslab_stats(msp);
676 metaslab_unload(msp);
677 mutex_exit(&msp->ms_lock);
680 if (dump_opt['m'] > 1 && sm != NULL &&
681 spa_feature_is_active(spa, SPA_FEATURE_SPACEMAP_HISTOGRAM)) {
683 * The space map histogram represents free space in chunks
684 * of sm_shift (i.e. bucket 0 refers to 2^sm_shift).
686 (void) printf("\tOn-disk histogram:\n");
687 dump_histogram(sm->sm_phys->smp_histogram,
688 SPACE_MAP_HISTOGRAM_SIZE(sm), sm->sm_shift);
691 if (dump_opt['d'] > 5 || dump_opt['m'] > 3) {
692 ASSERT(msp->ms_size == (1ULL << vd->vdev_ms_shift));
694 mutex_enter(&msp->ms_lock);
695 dump_spacemap(spa->spa_meta_objset, msp->ms_sm);
696 mutex_exit(&msp->ms_lock);
701 print_vdev_metaslab_header(vdev_t *vd)
703 (void) printf("\tvdev %10llu\n\t%-10s%5llu %-19s %-15s %-10s\n",
704 (u_longlong_t)vd->vdev_id,
705 "metaslabs", (u_longlong_t)vd->vdev_ms_count,
706 "offset", "spacemap", "free");
707 (void) printf("\t%15s %19s %15s %10s\n",
708 "---------------", "-------------------",
709 "---------------", "-------------");
713 dump_metaslabs(spa_t *spa)
715 vdev_t *vd, *rvd = spa->spa_root_vdev;
716 uint64_t m, c = 0, children = rvd->vdev_children;
718 (void) printf("\nMetaslabs:\n");
720 if (!dump_opt['d'] && zopt_objects > 0) {
724 (void) fatal("bad vdev id: %llu", (u_longlong_t)c);
726 if (zopt_objects > 1) {
727 vd = rvd->vdev_child[c];
728 print_vdev_metaslab_header(vd);
730 for (m = 1; m < zopt_objects; m++) {
731 if (zopt_object[m] < vd->vdev_ms_count)
733 vd->vdev_ms[zopt_object[m]]);
735 (void) fprintf(stderr, "bad metaslab "
737 (u_longlong_t)zopt_object[m]);
744 for (; c < children; c++) {
745 vd = rvd->vdev_child[c];
746 print_vdev_metaslab_header(vd);
748 for (m = 0; m < vd->vdev_ms_count; m++)
749 dump_metaslab(vd->vdev_ms[m]);
755 dump_dde(const ddt_t *ddt, const ddt_entry_t *dde, uint64_t index)
757 const ddt_phys_t *ddp = dde->dde_phys;
758 const ddt_key_t *ddk = &dde->dde_key;
759 char *types[4] = { "ditto", "single", "double", "triple" };
760 char blkbuf[BP_SPRINTF_LEN];
763 for (int p = 0; p < DDT_PHYS_TYPES; p++, ddp++) {
764 if (ddp->ddp_phys_birth == 0)
766 ddt_bp_create(ddt->ddt_checksum, ddk, ddp, &blk);
767 snprintf_blkptr(blkbuf, sizeof (blkbuf), &blk);
768 (void) printf("index %llx refcnt %llu %s %s\n",
769 (u_longlong_t)index, (u_longlong_t)ddp->ddp_refcnt,
775 dump_dedup_ratio(const ddt_stat_t *dds)
777 double rL, rP, rD, D, dedup, compress, copies;
779 if (dds->dds_blocks == 0)
782 rL = (double)dds->dds_ref_lsize;
783 rP = (double)dds->dds_ref_psize;
784 rD = (double)dds->dds_ref_dsize;
785 D = (double)dds->dds_dsize;
791 (void) printf("dedup = %.2f, compress = %.2f, copies = %.2f, "
792 "dedup * compress / copies = %.2f\n\n",
793 dedup, compress, copies, dedup * compress / copies);
797 dump_ddt(ddt_t *ddt, enum ddt_type type, enum ddt_class class)
799 char name[DDT_NAMELEN];
802 dmu_object_info_t doi;
803 uint64_t count, dspace, mspace;
806 error = ddt_object_info(ddt, type, class, &doi);
812 error = ddt_object_count(ddt, type, class, &count);
817 dspace = doi.doi_physical_blocks_512 << 9;
818 mspace = doi.doi_fill_count * doi.doi_data_block_size;
820 ddt_object_name(ddt, type, class, name);
822 (void) printf("%s: %llu entries, size %llu on disk, %llu in core\n",
825 (u_longlong_t)(dspace / count),
826 (u_longlong_t)(mspace / count));
828 if (dump_opt['D'] < 3)
831 zpool_dump_ddt(NULL, &ddt->ddt_histogram[type][class]);
833 if (dump_opt['D'] < 4)
836 if (dump_opt['D'] < 5 && class == DDT_CLASS_UNIQUE)
839 (void) printf("%s contents:\n\n", name);
841 while ((error = ddt_object_walk(ddt, type, class, &walk, &dde)) == 0)
842 dump_dde(ddt, &dde, walk);
844 ASSERT(error == ENOENT);
850 dump_all_ddts(spa_t *spa)
852 ddt_histogram_t ddh_total = { 0 };
853 ddt_stat_t dds_total = { 0 };
855 for (enum zio_checksum c = 0; c < ZIO_CHECKSUM_FUNCTIONS; c++) {
856 ddt_t *ddt = spa->spa_ddt[c];
857 for (enum ddt_type type = 0; type < DDT_TYPES; type++) {
858 for (enum ddt_class class = 0; class < DDT_CLASSES;
860 dump_ddt(ddt, type, class);
865 ddt_get_dedup_stats(spa, &dds_total);
867 if (dds_total.dds_blocks == 0) {
868 (void) printf("All DDTs are empty\n");
874 if (dump_opt['D'] > 1) {
875 (void) printf("DDT histogram (aggregated over all DDTs):\n");
876 ddt_get_dedup_histogram(spa, &ddh_total);
877 zpool_dump_ddt(&dds_total, &ddh_total);
880 dump_dedup_ratio(&dds_total);
884 dump_dtl_seg(void *arg, uint64_t start, uint64_t size)
888 (void) printf("%s [%llu,%llu) length %llu\n",
891 (u_longlong_t)(start + size),
892 (u_longlong_t)(size));
896 dump_dtl(vdev_t *vd, int indent)
898 spa_t *spa = vd->vdev_spa;
900 char *name[DTL_TYPES] = { "missing", "partial", "scrub", "outage" };
903 spa_vdev_state_enter(spa, SCL_NONE);
904 required = vdev_dtl_required(vd);
905 (void) spa_vdev_state_exit(spa, NULL, 0);
908 (void) printf("\nDirty time logs:\n\n");
910 (void) printf("\t%*s%s [%s]\n", indent, "",
911 vd->vdev_path ? vd->vdev_path :
912 vd->vdev_parent ? vd->vdev_ops->vdev_op_type : spa_name(spa),
913 required ? "DTL-required" : "DTL-expendable");
915 for (int t = 0; t < DTL_TYPES; t++) {
916 range_tree_t *rt = vd->vdev_dtl[t];
917 if (range_tree_space(rt) == 0)
919 (void) snprintf(prefix, sizeof (prefix), "\t%*s%s",
920 indent + 2, "", name[t]);
921 mutex_enter(rt->rt_lock);
922 range_tree_walk(rt, dump_dtl_seg, prefix);
923 mutex_exit(rt->rt_lock);
924 if (dump_opt['d'] > 5 && vd->vdev_children == 0)
925 dump_spacemap(spa->spa_meta_objset, vd->vdev_dtl_sm);
928 for (int c = 0; c < vd->vdev_children; c++)
929 dump_dtl(vd->vdev_child[c], indent + 4);
932 /* from spa_history.c: spa_history_create_obj() */
933 #define HIS_BUF_LEN_DEF (128 << 10)
934 #define HIS_BUF_LEN_MAX (1 << 30)
937 dump_history(spa_t *spa)
939 nvlist_t **events = NULL;
941 uint64_t bufsize = HIS_BUF_LEN_DEF;
942 uint64_t resid, len, off = 0;
948 char internalstr[MAXPATHLEN];
950 if ((buf = malloc(bufsize)) == NULL)
951 (void) fprintf(stderr, "Unable to read history: "
956 if ((error = spa_history_get(spa, &off, &len, buf)) != 0) {
957 (void) fprintf(stderr, "Unable to read history: "
958 "error %d\n", error);
962 if (zpool_history_unpack(buf, len, &resid, &events, &num) != 0)
967 * If the history block is too big, double the buffer
968 * size and try again.
975 if ((bufsize >= HIS_BUF_LEN_MAX) ||
976 ((buf = malloc(bufsize)) == NULL)) {
977 (void) fprintf(stderr, "Unable to read history: "
985 (void) printf("\nHistory:\n");
986 for (int i = 0; i < num; i++) {
987 uint64_t time, txg, ievent;
989 boolean_t printed = B_FALSE;
991 if (nvlist_lookup_uint64(events[i], ZPOOL_HIST_TIME,
994 if (nvlist_lookup_string(events[i], ZPOOL_HIST_CMD,
996 if (nvlist_lookup_uint64(events[i],
997 ZPOOL_HIST_INT_EVENT, &ievent) != 0)
999 verify(nvlist_lookup_uint64(events[i],
1000 ZPOOL_HIST_TXG, &txg) == 0);
1001 verify(nvlist_lookup_string(events[i],
1002 ZPOOL_HIST_INT_STR, &intstr) == 0);
1003 if (ievent >= ZFS_NUM_LEGACY_HISTORY_EVENTS)
1006 (void) snprintf(internalstr,
1007 sizeof (internalstr),
1008 "[internal %s txg:%lld] %s",
1009 zfs_history_event_names[ievent], txg,
1014 (void) localtime_r(&tsec, &t);
1015 (void) strftime(tbuf, sizeof (tbuf), "%F.%T", &t);
1016 (void) printf("%s %s\n", tbuf, cmd);
1020 if (dump_opt['h'] > 1) {
1022 (void) printf("unrecognized record:\n");
1023 dump_nvlist(events[i], 2);
1030 dump_dnode(objset_t *os, uint64_t object, void *data, size_t size)
1035 blkid2offset(const dnode_phys_t *dnp, const blkptr_t *bp, const zbookmark_t *zb)
1038 ASSERT(zb->zb_level < 0);
1039 if (zb->zb_object == 0)
1040 return (zb->zb_blkid);
1041 return (zb->zb_blkid * BP_GET_LSIZE(bp));
1044 ASSERT(zb->zb_level >= 0);
1046 return ((zb->zb_blkid <<
1047 (zb->zb_level * (dnp->dn_indblkshift - SPA_BLKPTRSHIFT))) *
1048 dnp->dn_datablkszsec << SPA_MINBLOCKSHIFT);
1052 snprintf_blkptr_compact(char *blkbuf, size_t buflen, const blkptr_t *bp)
1054 const dva_t *dva = bp->blk_dva;
1055 int ndvas = dump_opt['d'] > 5 ? BP_GET_NDVAS(bp) : 1;
1057 if (dump_opt['b'] >= 6) {
1058 snprintf_blkptr(blkbuf, buflen, bp);
1064 for (int i = 0; i < ndvas; i++)
1065 (void) snprintf(blkbuf + strlen(blkbuf),
1066 buflen - strlen(blkbuf), "%llu:%llx:%llx ",
1067 (u_longlong_t)DVA_GET_VDEV(&dva[i]),
1068 (u_longlong_t)DVA_GET_OFFSET(&dva[i]),
1069 (u_longlong_t)DVA_GET_ASIZE(&dva[i]));
1071 if (BP_IS_HOLE(bp)) {
1072 (void) snprintf(blkbuf + strlen(blkbuf),
1073 buflen - strlen(blkbuf), "B=%llu",
1074 (u_longlong_t)bp->blk_birth);
1076 (void) snprintf(blkbuf + strlen(blkbuf),
1077 buflen - strlen(blkbuf),
1078 "%llxL/%llxP F=%llu B=%llu/%llu",
1079 (u_longlong_t)BP_GET_LSIZE(bp),
1080 (u_longlong_t)BP_GET_PSIZE(bp),
1081 (u_longlong_t)bp->blk_fill,
1082 (u_longlong_t)bp->blk_birth,
1083 (u_longlong_t)BP_PHYSICAL_BIRTH(bp));
1088 print_indirect(blkptr_t *bp, const zbookmark_t *zb,
1089 const dnode_phys_t *dnp)
1091 char blkbuf[BP_SPRINTF_LEN];
1094 ASSERT3U(BP_GET_TYPE(bp), ==, dnp->dn_type);
1095 ASSERT3U(BP_GET_LEVEL(bp), ==, zb->zb_level);
1097 (void) printf("%16llx ", (u_longlong_t)blkid2offset(dnp, bp, zb));
1099 ASSERT(zb->zb_level >= 0);
1101 for (l = dnp->dn_nlevels - 1; l >= -1; l--) {
1102 if (l == zb->zb_level) {
1103 (void) printf("L%llx", (u_longlong_t)zb->zb_level);
1109 snprintf_blkptr_compact(blkbuf, sizeof (blkbuf), bp);
1110 (void) printf("%s\n", blkbuf);
1114 visit_indirect(spa_t *spa, const dnode_phys_t *dnp,
1115 blkptr_t *bp, const zbookmark_t *zb)
1119 if (bp->blk_birth == 0)
1122 print_indirect(bp, zb, dnp);
1124 if (BP_GET_LEVEL(bp) > 0 && !BP_IS_HOLE(bp)) {
1125 uint32_t flags = ARC_WAIT;
1128 int epb = BP_GET_LSIZE(bp) >> SPA_BLKPTRSHIFT;
1132 err = arc_read(NULL, spa, bp, arc_getbuf_func, &buf,
1133 ZIO_PRIORITY_ASYNC_READ, ZIO_FLAG_CANFAIL, &flags, zb);
1136 ASSERT(buf->b_data);
1138 /* recursively visit blocks below this */
1140 for (i = 0; i < epb; i++, cbp++) {
1143 SET_BOOKMARK(&czb, zb->zb_objset, zb->zb_object,
1145 zb->zb_blkid * epb + i);
1146 err = visit_indirect(spa, dnp, cbp, &czb);
1149 fill += cbp->blk_fill;
1152 ASSERT3U(fill, ==, bp->blk_fill);
1153 (void) arc_buf_remove_ref(buf, &buf);
1161 dump_indirect(dnode_t *dn)
1163 dnode_phys_t *dnp = dn->dn_phys;
1167 (void) printf("Indirect blocks:\n");
1169 SET_BOOKMARK(&czb, dmu_objset_id(dn->dn_objset),
1170 dn->dn_object, dnp->dn_nlevels - 1, 0);
1171 for (j = 0; j < dnp->dn_nblkptr; j++) {
1173 (void) visit_indirect(dmu_objset_spa(dn->dn_objset), dnp,
1174 &dnp->dn_blkptr[j], &czb);
1177 (void) printf("\n");
1182 dump_dsl_dir(objset_t *os, uint64_t object, void *data, size_t size)
1184 dsl_dir_phys_t *dd = data;
1191 ASSERT3U(size, >=, sizeof (dsl_dir_phys_t));
1193 crtime = dd->dd_creation_time;
1194 (void) printf("\t\tcreation_time = %s", ctime(&crtime));
1195 (void) printf("\t\thead_dataset_obj = %llu\n",
1196 (u_longlong_t)dd->dd_head_dataset_obj);
1197 (void) printf("\t\tparent_dir_obj = %llu\n",
1198 (u_longlong_t)dd->dd_parent_obj);
1199 (void) printf("\t\torigin_obj = %llu\n",
1200 (u_longlong_t)dd->dd_origin_obj);
1201 (void) printf("\t\tchild_dir_zapobj = %llu\n",
1202 (u_longlong_t)dd->dd_child_dir_zapobj);
1203 zdb_nicenum(dd->dd_used_bytes, nice);
1204 (void) printf("\t\tused_bytes = %s\n", nice);
1205 zdb_nicenum(dd->dd_compressed_bytes, nice);
1206 (void) printf("\t\tcompressed_bytes = %s\n", nice);
1207 zdb_nicenum(dd->dd_uncompressed_bytes, nice);
1208 (void) printf("\t\tuncompressed_bytes = %s\n", nice);
1209 zdb_nicenum(dd->dd_quota, nice);
1210 (void) printf("\t\tquota = %s\n", nice);
1211 zdb_nicenum(dd->dd_reserved, nice);
1212 (void) printf("\t\treserved = %s\n", nice);
1213 (void) printf("\t\tprops_zapobj = %llu\n",
1214 (u_longlong_t)dd->dd_props_zapobj);
1215 (void) printf("\t\tdeleg_zapobj = %llu\n",
1216 (u_longlong_t)dd->dd_deleg_zapobj);
1217 (void) printf("\t\tflags = %llx\n",
1218 (u_longlong_t)dd->dd_flags);
1221 zdb_nicenum(dd->dd_used_breakdown[DD_USED_ ## which], nice); \
1222 (void) printf("\t\tused_breakdown[" #which "] = %s\n", nice)
1233 dump_dsl_dataset(objset_t *os, uint64_t object, void *data, size_t size)
1235 dsl_dataset_phys_t *ds = data;
1237 char used[32], compressed[32], uncompressed[32], unique[32];
1238 char blkbuf[BP_SPRINTF_LEN];
1243 ASSERT(size == sizeof (*ds));
1244 crtime = ds->ds_creation_time;
1245 zdb_nicenum(ds->ds_referenced_bytes, used);
1246 zdb_nicenum(ds->ds_compressed_bytes, compressed);
1247 zdb_nicenum(ds->ds_uncompressed_bytes, uncompressed);
1248 zdb_nicenum(ds->ds_unique_bytes, unique);
1249 snprintf_blkptr(blkbuf, sizeof (blkbuf), &ds->ds_bp);
1251 (void) printf("\t\tdir_obj = %llu\n",
1252 (u_longlong_t)ds->ds_dir_obj);
1253 (void) printf("\t\tprev_snap_obj = %llu\n",
1254 (u_longlong_t)ds->ds_prev_snap_obj);
1255 (void) printf("\t\tprev_snap_txg = %llu\n",
1256 (u_longlong_t)ds->ds_prev_snap_txg);
1257 (void) printf("\t\tnext_snap_obj = %llu\n",
1258 (u_longlong_t)ds->ds_next_snap_obj);
1259 (void) printf("\t\tsnapnames_zapobj = %llu\n",
1260 (u_longlong_t)ds->ds_snapnames_zapobj);
1261 (void) printf("\t\tnum_children = %llu\n",
1262 (u_longlong_t)ds->ds_num_children);
1263 (void) printf("\t\tuserrefs_obj = %llu\n",
1264 (u_longlong_t)ds->ds_userrefs_obj);
1265 (void) printf("\t\tcreation_time = %s", ctime(&crtime));
1266 (void) printf("\t\tcreation_txg = %llu\n",
1267 (u_longlong_t)ds->ds_creation_txg);
1268 (void) printf("\t\tdeadlist_obj = %llu\n",
1269 (u_longlong_t)ds->ds_deadlist_obj);
1270 (void) printf("\t\tused_bytes = %s\n", used);
1271 (void) printf("\t\tcompressed_bytes = %s\n", compressed);
1272 (void) printf("\t\tuncompressed_bytes = %s\n", uncompressed);
1273 (void) printf("\t\tunique = %s\n", unique);
1274 (void) printf("\t\tfsid_guid = %llu\n",
1275 (u_longlong_t)ds->ds_fsid_guid);
1276 (void) printf("\t\tguid = %llu\n",
1277 (u_longlong_t)ds->ds_guid);
1278 (void) printf("\t\tflags = %llx\n",
1279 (u_longlong_t)ds->ds_flags);
1280 (void) printf("\t\tnext_clones_obj = %llu\n",
1281 (u_longlong_t)ds->ds_next_clones_obj);
1282 (void) printf("\t\tprops_obj = %llu\n",
1283 (u_longlong_t)ds->ds_props_obj);
1284 (void) printf("\t\tbp = %s\n", blkbuf);
1289 dump_bptree_cb(void *arg, const blkptr_t *bp, dmu_tx_t *tx)
1291 char blkbuf[BP_SPRINTF_LEN];
1293 if (bp->blk_birth != 0) {
1294 snprintf_blkptr(blkbuf, sizeof (blkbuf), bp);
1295 (void) printf("\t%s\n", blkbuf);
1301 dump_bptree(objset_t *os, uint64_t obj, char *name)
1307 if (dump_opt['d'] < 3)
1310 VERIFY3U(0, ==, dmu_bonus_hold(os, obj, FTAG, &db));
1312 zdb_nicenum(bt->bt_bytes, bytes);
1313 (void) printf("\n %s: %llu datasets, %s\n",
1314 name, (unsigned long long)(bt->bt_end - bt->bt_begin), bytes);
1315 dmu_buf_rele(db, FTAG);
1317 if (dump_opt['d'] < 5)
1320 (void) printf("\n");
1322 (void) bptree_iterate(os, obj, B_FALSE, dump_bptree_cb, NULL, NULL);
1327 dump_bpobj_cb(void *arg, const blkptr_t *bp, dmu_tx_t *tx)
1329 char blkbuf[BP_SPRINTF_LEN];
1331 ASSERT(bp->blk_birth != 0);
1332 snprintf_blkptr_compact(blkbuf, sizeof (blkbuf), bp);
1333 (void) printf("\t%s\n", blkbuf);
1338 dump_bpobj(bpobj_t *bpo, char *name, int indent)
1344 if (dump_opt['d'] < 3)
1347 zdb_nicenum(bpo->bpo_phys->bpo_bytes, bytes);
1348 if (bpo->bpo_havesubobj && bpo->bpo_phys->bpo_subobjs != 0) {
1349 zdb_nicenum(bpo->bpo_phys->bpo_comp, comp);
1350 zdb_nicenum(bpo->bpo_phys->bpo_uncomp, uncomp);
1351 (void) printf(" %*s: object %llu, %llu local blkptrs, "
1352 "%llu subobjs, %s (%s/%s comp)\n",
1354 (u_longlong_t)bpo->bpo_object,
1355 (u_longlong_t)bpo->bpo_phys->bpo_num_blkptrs,
1356 (u_longlong_t)bpo->bpo_phys->bpo_num_subobjs,
1357 bytes, comp, uncomp);
1359 for (uint64_t i = 0; i < bpo->bpo_phys->bpo_num_subobjs; i++) {
1363 VERIFY0(dmu_read(bpo->bpo_os,
1364 bpo->bpo_phys->bpo_subobjs,
1365 i * sizeof (subobj), sizeof (subobj), &subobj, 0));
1366 error = bpobj_open(&subbpo, bpo->bpo_os, subobj);
1368 (void) printf("ERROR %u while trying to open "
1370 error, (u_longlong_t)subobj);
1373 dump_bpobj(&subbpo, "subobj", indent + 1);
1374 bpobj_close(&subbpo);
1377 (void) printf(" %*s: object %llu, %llu blkptrs, %s\n",
1379 (u_longlong_t)bpo->bpo_object,
1380 (u_longlong_t)bpo->bpo_phys->bpo_num_blkptrs,
1384 if (dump_opt['d'] < 5)
1389 (void) bpobj_iterate_nofree(bpo, dump_bpobj_cb, NULL, NULL);
1390 (void) printf("\n");
1395 dump_deadlist(dsl_deadlist_t *dl)
1397 dsl_deadlist_entry_t *dle;
1403 if (dump_opt['d'] < 3)
1406 zdb_nicenum(dl->dl_phys->dl_used, bytes);
1407 zdb_nicenum(dl->dl_phys->dl_comp, comp);
1408 zdb_nicenum(dl->dl_phys->dl_uncomp, uncomp);
1409 (void) printf("\n Deadlist: %s (%s/%s comp)\n",
1410 bytes, comp, uncomp);
1412 if (dump_opt['d'] < 4)
1415 (void) printf("\n");
1417 /* force the tree to be loaded */
1418 dsl_deadlist_space_range(dl, 0, UINT64_MAX, &unused, &unused, &unused);
1420 for (dle = avl_first(&dl->dl_tree); dle;
1421 dle = AVL_NEXT(&dl->dl_tree, dle)) {
1422 if (dump_opt['d'] >= 5) {
1424 (void) snprintf(buf, sizeof (buf), "mintxg %llu -> ",
1425 (longlong_t)dle->dle_mintxg,
1426 (longlong_t)dle->dle_bpobj.bpo_object);
1428 dump_bpobj(&dle->dle_bpobj, buf, 0);
1430 (void) printf("mintxg %llu -> obj %llu\n",
1431 (longlong_t)dle->dle_mintxg,
1432 (longlong_t)dle->dle_bpobj.bpo_object);
1438 static avl_tree_t idx_tree;
1439 static avl_tree_t domain_tree;
1440 static boolean_t fuid_table_loaded;
1441 static boolean_t sa_loaded;
1442 sa_attr_type_t *sa_attr_table;
1445 fuid_table_destroy()
1447 if (fuid_table_loaded) {
1448 zfs_fuid_table_destroy(&idx_tree, &domain_tree);
1449 fuid_table_loaded = B_FALSE;
1454 * print uid or gid information.
1455 * For normal POSIX id just the id is printed in decimal format.
1456 * For CIFS files with FUID the fuid is printed in hex followed by
1457 * the domain-rid string.
1460 print_idstr(uint64_t id, const char *id_type)
1462 if (FUID_INDEX(id)) {
1465 domain = zfs_fuid_idx_domain(&idx_tree, FUID_INDEX(id));
1466 (void) printf("\t%s %llx [%s-%d]\n", id_type,
1467 (u_longlong_t)id, domain, (int)FUID_RID(id));
1469 (void) printf("\t%s %llu\n", id_type, (u_longlong_t)id);
1475 dump_uidgid(objset_t *os, uint64_t uid, uint64_t gid)
1477 uint32_t uid_idx, gid_idx;
1479 uid_idx = FUID_INDEX(uid);
1480 gid_idx = FUID_INDEX(gid);
1482 /* Load domain table, if not already loaded */
1483 if (!fuid_table_loaded && (uid_idx || gid_idx)) {
1486 /* first find the fuid object. It lives in the master node */
1487 VERIFY(zap_lookup(os, MASTER_NODE_OBJ, ZFS_FUID_TABLES,
1488 8, 1, &fuid_obj) == 0);
1489 zfs_fuid_avl_tree_create(&idx_tree, &domain_tree);
1490 (void) zfs_fuid_table_load(os, fuid_obj,
1491 &idx_tree, &domain_tree);
1492 fuid_table_loaded = B_TRUE;
1495 print_idstr(uid, "uid");
1496 print_idstr(gid, "gid");
1501 dump_znode(objset_t *os, uint64_t object, void *data, size_t size)
1503 char path[MAXPATHLEN * 2]; /* allow for xattr and failure prefix */
1505 uint64_t xattr, rdev, gen;
1506 uint64_t uid, gid, mode, fsize, parent, links;
1508 uint64_t acctm[2], modtm[2], chgtm[2], crtm[2];
1509 time_t z_crtime, z_atime, z_mtime, z_ctime;
1510 sa_bulk_attr_t bulk[12];
1515 uint64_t sa_attrs = 0;
1518 VERIFY(zap_lookup(os, MASTER_NODE_OBJ, ZPL_VERSION_STR,
1519 8, 1, &version) == 0);
1520 if (version >= ZPL_VERSION_SA) {
1521 VERIFY(zap_lookup(os, MASTER_NODE_OBJ, ZFS_SA_ATTRS,
1522 8, 1, &sa_attrs) == 0);
1524 if ((error = sa_setup(os, sa_attrs, zfs_attr_table,
1525 ZPL_END, &sa_attr_table)) != 0) {
1526 (void) printf("sa_setup failed errno %d, can't "
1527 "display znode contents\n", error);
1533 if (sa_handle_get(os, object, NULL, SA_HDL_PRIVATE, &hdl)) {
1534 (void) printf("Failed to get handle for SA znode\n");
1538 SA_ADD_BULK_ATTR(bulk, idx, sa_attr_table[ZPL_UID], NULL, &uid, 8);
1539 SA_ADD_BULK_ATTR(bulk, idx, sa_attr_table[ZPL_GID], NULL, &gid, 8);
1540 SA_ADD_BULK_ATTR(bulk, idx, sa_attr_table[ZPL_LINKS], NULL,
1542 SA_ADD_BULK_ATTR(bulk, idx, sa_attr_table[ZPL_GEN], NULL, &gen, 8);
1543 SA_ADD_BULK_ATTR(bulk, idx, sa_attr_table[ZPL_MODE], NULL,
1545 SA_ADD_BULK_ATTR(bulk, idx, sa_attr_table[ZPL_PARENT],
1547 SA_ADD_BULK_ATTR(bulk, idx, sa_attr_table[ZPL_SIZE], NULL,
1549 SA_ADD_BULK_ATTR(bulk, idx, sa_attr_table[ZPL_ATIME], NULL,
1551 SA_ADD_BULK_ATTR(bulk, idx, sa_attr_table[ZPL_MTIME], NULL,
1553 SA_ADD_BULK_ATTR(bulk, idx, sa_attr_table[ZPL_CRTIME], NULL,
1555 SA_ADD_BULK_ATTR(bulk, idx, sa_attr_table[ZPL_CTIME], NULL,
1557 SA_ADD_BULK_ATTR(bulk, idx, sa_attr_table[ZPL_FLAGS], NULL,
1560 if (sa_bulk_lookup(hdl, bulk, idx)) {
1561 (void) sa_handle_destroy(hdl);
1565 error = zfs_obj_to_path(os, object, path, sizeof (path));
1567 (void) snprintf(path, sizeof (path), "\?\?\?<object#%llu>",
1568 (u_longlong_t)object);
1570 if (dump_opt['d'] < 3) {
1571 (void) printf("\t%s\n", path);
1572 (void) sa_handle_destroy(hdl);
1576 z_crtime = (time_t)crtm[0];
1577 z_atime = (time_t)acctm[0];
1578 z_mtime = (time_t)modtm[0];
1579 z_ctime = (time_t)chgtm[0];
1581 (void) printf("\tpath %s\n", path);
1582 dump_uidgid(os, uid, gid);
1583 (void) printf("\tatime %s", ctime(&z_atime));
1584 (void) printf("\tmtime %s", ctime(&z_mtime));
1585 (void) printf("\tctime %s", ctime(&z_ctime));
1586 (void) printf("\tcrtime %s", ctime(&z_crtime));
1587 (void) printf("\tgen %llu\n", (u_longlong_t)gen);
1588 (void) printf("\tmode %llo\n", (u_longlong_t)mode);
1589 (void) printf("\tsize %llu\n", (u_longlong_t)fsize);
1590 (void) printf("\tparent %llu\n", (u_longlong_t)parent);
1591 (void) printf("\tlinks %llu\n", (u_longlong_t)links);
1592 (void) printf("\tpflags %llx\n", (u_longlong_t)pflags);
1593 if (sa_lookup(hdl, sa_attr_table[ZPL_XATTR], &xattr,
1594 sizeof (uint64_t)) == 0)
1595 (void) printf("\txattr %llu\n", (u_longlong_t)xattr);
1596 if (sa_lookup(hdl, sa_attr_table[ZPL_RDEV], &rdev,
1597 sizeof (uint64_t)) == 0)
1598 (void) printf("\trdev 0x%016llx\n", (u_longlong_t)rdev);
1599 sa_handle_destroy(hdl);
1604 dump_acl(objset_t *os, uint64_t object, void *data, size_t size)
1610 dump_dmu_objset(objset_t *os, uint64_t object, void *data, size_t size)
1614 static object_viewer_t *object_viewer[DMU_OT_NUMTYPES + 1] = {
1615 dump_none, /* unallocated */
1616 dump_zap, /* object directory */
1617 dump_uint64, /* object array */
1618 dump_none, /* packed nvlist */
1619 dump_packed_nvlist, /* packed nvlist size */
1620 dump_none, /* bplist */
1621 dump_none, /* bplist header */
1622 dump_none, /* SPA space map header */
1623 dump_none, /* SPA space map */
1624 dump_none, /* ZIL intent log */
1625 dump_dnode, /* DMU dnode */
1626 dump_dmu_objset, /* DMU objset */
1627 dump_dsl_dir, /* DSL directory */
1628 dump_zap, /* DSL directory child map */
1629 dump_zap, /* DSL dataset snap map */
1630 dump_zap, /* DSL props */
1631 dump_dsl_dataset, /* DSL dataset */
1632 dump_znode, /* ZFS znode */
1633 dump_acl, /* ZFS V0 ACL */
1634 dump_uint8, /* ZFS plain file */
1635 dump_zpldir, /* ZFS directory */
1636 dump_zap, /* ZFS master node */
1637 dump_zap, /* ZFS delete queue */
1638 dump_uint8, /* zvol object */
1639 dump_zap, /* zvol prop */
1640 dump_uint8, /* other uint8[] */
1641 dump_uint64, /* other uint64[] */
1642 dump_zap, /* other ZAP */
1643 dump_zap, /* persistent error log */
1644 dump_uint8, /* SPA history */
1645 dump_history_offsets, /* SPA history offsets */
1646 dump_zap, /* Pool properties */
1647 dump_zap, /* DSL permissions */
1648 dump_acl, /* ZFS ACL */
1649 dump_uint8, /* ZFS SYSACL */
1650 dump_none, /* FUID nvlist */
1651 dump_packed_nvlist, /* FUID nvlist size */
1652 dump_zap, /* DSL dataset next clones */
1653 dump_zap, /* DSL scrub queue */
1654 dump_zap, /* ZFS user/group used */
1655 dump_zap, /* ZFS user/group quota */
1656 dump_zap, /* snapshot refcount tags */
1657 dump_ddt_zap, /* DDT ZAP object */
1658 dump_zap, /* DDT statistics */
1659 dump_znode, /* SA object */
1660 dump_zap, /* SA Master Node */
1661 dump_sa_attrs, /* SA attribute registration */
1662 dump_sa_layouts, /* SA attribute layouts */
1663 dump_zap, /* DSL scrub translations */
1664 dump_none, /* fake dedup BP */
1665 dump_zap, /* deadlist */
1666 dump_none, /* deadlist hdr */
1667 dump_zap, /* dsl clones */
1668 dump_none, /* bpobj subobjs */
1669 dump_unknown, /* Unknown type, must be last */
1673 dump_object(objset_t *os, uint64_t object, int verbosity, int *print_header)
1675 dmu_buf_t *db = NULL;
1676 dmu_object_info_t doi;
1680 char iblk[32], dblk[32], lsize[32], asize[32], fill[32];
1681 char bonus_size[32];
1685 if (*print_header) {
1686 (void) printf("\n%10s %3s %5s %5s %5s %5s %6s %s\n",
1687 "Object", "lvl", "iblk", "dblk", "dsize", "lsize",
1693 dn = DMU_META_DNODE(os);
1695 error = dmu_bonus_hold(os, object, FTAG, &db);
1697 fatal("dmu_bonus_hold(%llu) failed, errno %u",
1699 bonus = db->db_data;
1700 bsize = db->db_size;
1701 dn = DB_DNODE((dmu_buf_impl_t *)db);
1703 dmu_object_info_from_dnode(dn, &doi);
1705 zdb_nicenum(doi.doi_metadata_block_size, iblk);
1706 zdb_nicenum(doi.doi_data_block_size, dblk);
1707 zdb_nicenum(doi.doi_max_offset, lsize);
1708 zdb_nicenum(doi.doi_physical_blocks_512 << 9, asize);
1709 zdb_nicenum(doi.doi_bonus_size, bonus_size);
1710 (void) sprintf(fill, "%6.2f", 100.0 * doi.doi_fill_count *
1711 doi.doi_data_block_size / (object == 0 ? DNODES_PER_BLOCK : 1) /
1712 doi.doi_max_offset);
1716 if (doi.doi_checksum != ZIO_CHECKSUM_INHERIT || verbosity >= 6) {
1717 (void) snprintf(aux + strlen(aux), sizeof (aux), " (K=%s)",
1718 ZDB_CHECKSUM_NAME(doi.doi_checksum));
1721 if (doi.doi_compress != ZIO_COMPRESS_INHERIT || verbosity >= 6) {
1722 (void) snprintf(aux + strlen(aux), sizeof (aux), " (Z=%s)",
1723 ZDB_COMPRESS_NAME(doi.doi_compress));
1726 (void) printf("%10lld %3u %5s %5s %5s %5s %6s %s%s\n",
1727 (u_longlong_t)object, doi.doi_indirection, iblk, dblk,
1728 asize, lsize, fill, ZDB_OT_NAME(doi.doi_type), aux);
1730 if (doi.doi_bonus_type != DMU_OT_NONE && verbosity > 3) {
1731 (void) printf("%10s %3s %5s %5s %5s %5s %6s %s\n",
1732 "", "", "", "", "", bonus_size, "bonus",
1733 ZDB_OT_NAME(doi.doi_bonus_type));
1736 if (verbosity >= 4) {
1737 (void) printf("\tdnode flags: %s%s%s\n",
1738 (dn->dn_phys->dn_flags & DNODE_FLAG_USED_BYTES) ?
1740 (dn->dn_phys->dn_flags & DNODE_FLAG_USERUSED_ACCOUNTED) ?
1741 "USERUSED_ACCOUNTED " : "",
1742 (dn->dn_phys->dn_flags & DNODE_FLAG_SPILL_BLKPTR) ?
1743 "SPILL_BLKPTR" : "");
1744 (void) printf("\tdnode maxblkid: %llu\n",
1745 (longlong_t)dn->dn_phys->dn_maxblkid);
1747 object_viewer[ZDB_OT_TYPE(doi.doi_bonus_type)](os, object,
1749 object_viewer[ZDB_OT_TYPE(doi.doi_type)](os, object, NULL, 0);
1756 if (verbosity >= 5) {
1758 * Report the list of segments that comprise the object.
1762 uint64_t blkfill = 1;
1765 if (dn->dn_type == DMU_OT_DNODE) {
1767 blkfill = DNODES_PER_BLOCK;
1772 error = dnode_next_offset(dn,
1773 0, &start, minlvl, blkfill, 0);
1777 error = dnode_next_offset(dn,
1778 DNODE_FIND_HOLE, &end, minlvl, blkfill, 0);
1779 zdb_nicenum(end - start, segsize);
1780 (void) printf("\t\tsegment [%016llx, %016llx)"
1781 " size %5s\n", (u_longlong_t)start,
1782 (u_longlong_t)end, segsize);
1790 dmu_buf_rele(db, FTAG);
1793 static char *objset_types[DMU_OST_NUMTYPES] = {
1794 "NONE", "META", "ZPL", "ZVOL", "OTHER", "ANY" };
1797 dump_dir(objset_t *os)
1799 dmu_objset_stats_t dds;
1800 uint64_t object, object_count;
1801 uint64_t refdbytes, usedobjs, scratch;
1803 char blkbuf[BP_SPRINTF_LEN + 20];
1804 char osname[MAXNAMELEN];
1805 char *type = "UNKNOWN";
1806 int verbosity = dump_opt['d'];
1807 int print_header = 1;
1810 dsl_pool_config_enter(dmu_objset_pool(os), FTAG);
1811 dmu_objset_fast_stat(os, &dds);
1812 dsl_pool_config_exit(dmu_objset_pool(os), FTAG);
1814 if (dds.dds_type < DMU_OST_NUMTYPES)
1815 type = objset_types[dds.dds_type];
1817 if (dds.dds_type == DMU_OST_META) {
1818 dds.dds_creation_txg = TXG_INITIAL;
1819 usedobjs = os->os_rootbp->blk_fill;
1820 refdbytes = os->os_spa->spa_dsl_pool->
1821 dp_mos_dir->dd_phys->dd_used_bytes;
1823 dmu_objset_space(os, &refdbytes, &scratch, &usedobjs, &scratch);
1826 ASSERT3U(usedobjs, ==, os->os_rootbp->blk_fill);
1828 zdb_nicenum(refdbytes, numbuf);
1830 if (verbosity >= 4) {
1831 (void) snprintf(blkbuf, sizeof (blkbuf), ", rootbp ");
1832 (void) snprintf_blkptr(blkbuf + strlen(blkbuf),
1833 sizeof (blkbuf) - strlen(blkbuf), os->os_rootbp);
1838 dmu_objset_name(os, osname);
1840 (void) printf("Dataset %s [%s], ID %llu, cr_txg %llu, "
1841 "%s, %llu objects%s\n",
1842 osname, type, (u_longlong_t)dmu_objset_id(os),
1843 (u_longlong_t)dds.dds_creation_txg,
1844 numbuf, (u_longlong_t)usedobjs, blkbuf);
1846 if (zopt_objects != 0) {
1847 for (i = 0; i < zopt_objects; i++)
1848 dump_object(os, zopt_object[i], verbosity,
1850 (void) printf("\n");
1854 if (dump_opt['i'] != 0 || verbosity >= 2)
1855 dump_intent_log(dmu_objset_zil(os));
1857 if (dmu_objset_ds(os) != NULL)
1858 dump_deadlist(&dmu_objset_ds(os)->ds_deadlist);
1863 if (BP_IS_HOLE(os->os_rootbp))
1866 dump_object(os, 0, verbosity, &print_header);
1868 if (DMU_USERUSED_DNODE(os) != NULL &&
1869 DMU_USERUSED_DNODE(os)->dn_type != 0) {
1870 dump_object(os, DMU_USERUSED_OBJECT, verbosity, &print_header);
1871 dump_object(os, DMU_GROUPUSED_OBJECT, verbosity, &print_header);
1875 while ((error = dmu_object_next(os, &object, B_FALSE, 0)) == 0) {
1876 dump_object(os, object, verbosity, &print_header);
1880 ASSERT3U(object_count, ==, usedobjs);
1882 (void) printf("\n");
1884 if (error != ESRCH) {
1885 (void) fprintf(stderr, "dmu_object_next() = %d\n", error);
1891 dump_uberblock(uberblock_t *ub, const char *header, const char *footer)
1893 time_t timestamp = ub->ub_timestamp;
1895 (void) printf(header ? header : "");
1896 (void) printf("\tmagic = %016llx\n", (u_longlong_t)ub->ub_magic);
1897 (void) printf("\tversion = %llu\n", (u_longlong_t)ub->ub_version);
1898 (void) printf("\ttxg = %llu\n", (u_longlong_t)ub->ub_txg);
1899 (void) printf("\tguid_sum = %llu\n", (u_longlong_t)ub->ub_guid_sum);
1900 (void) printf("\ttimestamp = %llu UTC = %s",
1901 (u_longlong_t)ub->ub_timestamp, asctime(localtime(×tamp)));
1902 if (dump_opt['u'] >= 3) {
1903 char blkbuf[BP_SPRINTF_LEN];
1904 snprintf_blkptr(blkbuf, sizeof (blkbuf), &ub->ub_rootbp);
1905 (void) printf("\trootbp = %s\n", blkbuf);
1907 (void) printf(footer ? footer : "");
1911 dump_config(spa_t *spa)
1918 error = dmu_bonus_hold(spa->spa_meta_objset,
1919 spa->spa_config_object, FTAG, &db);
1922 nvsize = *(uint64_t *)db->db_data;
1923 dmu_buf_rele(db, FTAG);
1925 (void) printf("\nMOS Configuration:\n");
1926 dump_packed_nvlist(spa->spa_meta_objset,
1927 spa->spa_config_object, (void *)&nvsize, 1);
1929 (void) fprintf(stderr, "dmu_bonus_hold(%llu) failed, errno %d",
1930 (u_longlong_t)spa->spa_config_object, error);
1935 dump_cachefile(const char *cachefile)
1938 struct stat64 statbuf;
1942 if ((fd = open64(cachefile, O_RDONLY)) < 0) {
1943 (void) printf("cannot open '%s': %s\n", cachefile,
1948 if (fstat64(fd, &statbuf) != 0) {
1949 (void) printf("failed to stat '%s': %s\n", cachefile,
1954 if ((buf = malloc(statbuf.st_size)) == NULL) {
1955 (void) fprintf(stderr, "failed to allocate %llu bytes\n",
1956 (u_longlong_t)statbuf.st_size);
1960 if (read(fd, buf, statbuf.st_size) != statbuf.st_size) {
1961 (void) fprintf(stderr, "failed to read %llu bytes\n",
1962 (u_longlong_t)statbuf.st_size);
1968 if (nvlist_unpack(buf, statbuf.st_size, &config, 0) != 0) {
1969 (void) fprintf(stderr, "failed to unpack nvlist\n");
1975 dump_nvlist(config, 0);
1977 nvlist_free(config);
1980 #define ZDB_MAX_UB_HEADER_SIZE 32
1983 dump_label_uberblocks(vdev_label_t *lbl, uint64_t ashift)
1987 char header[ZDB_MAX_UB_HEADER_SIZE];
1989 vd.vdev_ashift = ashift;
1990 vdp->vdev_top = vdp;
1992 for (int i = 0; i < VDEV_UBERBLOCK_COUNT(vdp); i++) {
1993 uint64_t uoff = VDEV_UBERBLOCK_OFFSET(vdp, i);
1994 uberblock_t *ub = (void *)((char *)lbl + uoff);
1996 if (uberblock_verify(ub))
1998 (void) snprintf(header, ZDB_MAX_UB_HEADER_SIZE,
1999 "Uberblock[%d]\n", i);
2000 dump_uberblock(ub, header, "");
2005 dump_label(const char *dev)
2009 char *path, *buf = label.vl_vdev_phys.vp_nvlist;
2010 size_t buflen = sizeof (label.vl_vdev_phys.vp_nvlist);
2011 struct stat64 statbuf;
2012 uint64_t psize, ashift;
2013 int len = strlen(dev) + 1;
2015 if (strncmp(dev, "/dev/dsk/", 9) == 0) {
2018 (void) snprintf(path, len, "%s%s", "/dev/rdsk/", dev + 9);
2023 if ((fd = open64(path, O_RDONLY)) < 0) {
2024 (void) printf("cannot open '%s': %s\n", path, strerror(errno));
2029 if (fstat64(fd, &statbuf) != 0) {
2030 (void) printf("failed to stat '%s': %s\n", path,
2037 if (S_ISBLK(statbuf.st_mode)) {
2038 (void) printf("cannot use '%s': character device required\n",
2045 psize = statbuf.st_size;
2046 psize = P2ALIGN(psize, (uint64_t)sizeof (vdev_label_t));
2048 for (int l = 0; l < VDEV_LABELS; l++) {
2049 nvlist_t *config = NULL;
2051 (void) printf("--------------------------------------------\n");
2052 (void) printf("LABEL %d\n", l);
2053 (void) printf("--------------------------------------------\n");
2055 if (pread64(fd, &label, sizeof (label),
2056 vdev_label_offset(psize, l, 0)) != sizeof (label)) {
2057 (void) printf("failed to read label %d\n", l);
2061 if (nvlist_unpack(buf, buflen, &config, 0) != 0) {
2062 (void) printf("failed to unpack label %d\n", l);
2063 ashift = SPA_MINBLOCKSHIFT;
2065 nvlist_t *vdev_tree = NULL;
2067 dump_nvlist(config, 4);
2068 if ((nvlist_lookup_nvlist(config,
2069 ZPOOL_CONFIG_VDEV_TREE, &vdev_tree) != 0) ||
2070 (nvlist_lookup_uint64(vdev_tree,
2071 ZPOOL_CONFIG_ASHIFT, &ashift) != 0))
2072 ashift = SPA_MINBLOCKSHIFT;
2073 nvlist_free(config);
2076 dump_label_uberblocks(&label, ashift);
2085 dump_one_dir(const char *dsname, void *arg)
2090 error = dmu_objset_own(dsname, DMU_OST_ANY, B_TRUE, FTAG, &os);
2092 (void) printf("Could not open %s, error %d\n", dsname, error);
2096 dmu_objset_disown(os, FTAG);
2097 fuid_table_destroy();
2098 sa_loaded = B_FALSE;
2105 #define PSIZE_HISTO_SIZE (SPA_MAXBLOCKSIZE / SPA_MINBLOCKSIZE + 1)
2106 typedef struct zdb_blkstats {
2111 uint64_t zb_psize_histogram[PSIZE_HISTO_SIZE];
2115 * Extended object types to report deferred frees and dedup auto-ditto blocks.
2117 #define ZDB_OT_DEFERRED (DMU_OT_NUMTYPES + 0)
2118 #define ZDB_OT_DITTO (DMU_OT_NUMTYPES + 1)
2119 #define ZDB_OT_OTHER (DMU_OT_NUMTYPES + 2)
2120 #define ZDB_OT_TOTAL (DMU_OT_NUMTYPES + 3)
2122 static char *zdb_ot_extname[] = {
2129 #define ZB_TOTAL DN_MAX_LEVELS
2131 typedef struct zdb_cb {
2132 zdb_blkstats_t zcb_type[ZB_TOTAL + 1][ZDB_OT_TOTAL + 1];
2133 uint64_t zcb_dedup_asize;
2134 uint64_t zcb_dedup_blocks;
2136 uint64_t zcb_lastprint;
2137 uint64_t zcb_totalasize;
2138 uint64_t zcb_errors[256];
2145 zdb_count_block(zdb_cb_t *zcb, zilog_t *zilog, const blkptr_t *bp,
2146 dmu_object_type_t type)
2148 uint64_t refcnt = 0;
2150 ASSERT(type < ZDB_OT_TOTAL);
2152 if (zilog && zil_bp_tree_add(zilog, bp) != 0)
2155 for (int i = 0; i < 4; i++) {
2156 int l = (i < 2) ? BP_GET_LEVEL(bp) : ZB_TOTAL;
2157 int t = (i & 1) ? type : ZDB_OT_TOTAL;
2158 zdb_blkstats_t *zb = &zcb->zcb_type[l][t];
2160 zb->zb_asize += BP_GET_ASIZE(bp);
2161 zb->zb_lsize += BP_GET_LSIZE(bp);
2162 zb->zb_psize += BP_GET_PSIZE(bp);
2164 zb->zb_psize_histogram[BP_GET_PSIZE(bp) >> SPA_MINBLOCKSHIFT]++;
2170 if (BP_GET_DEDUP(bp)) {
2174 ddt = ddt_select(zcb->zcb_spa, bp);
2176 dde = ddt_lookup(ddt, bp, B_FALSE);
2181 ddt_phys_t *ddp = ddt_phys_select(dde, bp);
2182 ddt_phys_decref(ddp);
2183 refcnt = ddp->ddp_refcnt;
2184 if (ddt_phys_total_refcnt(dde) == 0)
2185 ddt_remove(ddt, dde);
2190 VERIFY3U(zio_wait(zio_claim(NULL, zcb->zcb_spa,
2191 refcnt ? 0 : spa_first_txg(zcb->zcb_spa),
2192 bp, NULL, NULL, ZIO_FLAG_CANFAIL)), ==, 0);
2197 zdb_blkptr_done(zio_t *zio)
2199 spa_t *spa = zio->io_spa;
2200 blkptr_t *bp = zio->io_bp;
2201 int ioerr = zio->io_error;
2202 zdb_cb_t *zcb = zio->io_private;
2203 zbookmark_t *zb = &zio->io_bookmark;
2205 zio_data_buf_free(zio->io_data, zio->io_size);
2207 mutex_enter(&spa->spa_scrub_lock);
2208 spa->spa_scrub_inflight--;
2209 cv_broadcast(&spa->spa_scrub_io_cv);
2211 if (ioerr && !(zio->io_flags & ZIO_FLAG_SPECULATIVE)) {
2212 char blkbuf[BP_SPRINTF_LEN];
2214 zcb->zcb_haderrors = 1;
2215 zcb->zcb_errors[ioerr]++;
2217 if (dump_opt['b'] >= 2)
2218 snprintf_blkptr(blkbuf, sizeof (blkbuf), bp);
2222 (void) printf("zdb_blkptr_cb: "
2223 "Got error %d reading "
2224 "<%llu, %llu, %lld, %llx> %s -- skipping\n",
2226 (u_longlong_t)zb->zb_objset,
2227 (u_longlong_t)zb->zb_object,
2228 (u_longlong_t)zb->zb_level,
2229 (u_longlong_t)zb->zb_blkid,
2232 mutex_exit(&spa->spa_scrub_lock);
2237 zdb_blkptr_cb(spa_t *spa, zilog_t *zilog, const blkptr_t *bp,
2238 const zbookmark_t *zb, const dnode_phys_t *dnp, void *arg)
2240 zdb_cb_t *zcb = arg;
2241 dmu_object_type_t type;
2242 boolean_t is_metadata;
2244 if (dump_opt['b'] >= 5 && bp->blk_birth > 0) {
2245 char blkbuf[BP_SPRINTF_LEN];
2246 snprintf_blkptr(blkbuf, sizeof (blkbuf), bp);
2247 (void) printf("objset %llu object %llu "
2248 "level %lld offset 0x%llx %s\n",
2249 (u_longlong_t)zb->zb_objset,
2250 (u_longlong_t)zb->zb_object,
2251 (longlong_t)zb->zb_level,
2252 (u_longlong_t)blkid2offset(dnp, bp, zb),
2259 type = BP_GET_TYPE(bp);
2261 zdb_count_block(zcb, zilog, bp,
2262 (type & DMU_OT_NEWTYPE) ? ZDB_OT_OTHER : type);
2264 is_metadata = (BP_GET_LEVEL(bp) != 0 || DMU_OT_IS_METADATA(type));
2266 if (dump_opt['c'] > 1 || (dump_opt['c'] && is_metadata)) {
2267 size_t size = BP_GET_PSIZE(bp);
2268 void *data = zio_data_buf_alloc(size);
2269 int flags = ZIO_FLAG_CANFAIL | ZIO_FLAG_SCRUB | ZIO_FLAG_RAW;
2271 /* If it's an intent log block, failure is expected. */
2272 if (zb->zb_level == ZB_ZIL_LEVEL)
2273 flags |= ZIO_FLAG_SPECULATIVE;
2275 mutex_enter(&spa->spa_scrub_lock);
2276 while (spa->spa_scrub_inflight > max_inflight)
2277 cv_wait(&spa->spa_scrub_io_cv, &spa->spa_scrub_lock);
2278 spa->spa_scrub_inflight++;
2279 mutex_exit(&spa->spa_scrub_lock);
2281 zio_nowait(zio_read(NULL, spa, bp, data, size,
2282 zdb_blkptr_done, zcb, ZIO_PRIORITY_ASYNC_READ, flags, zb));
2285 zcb->zcb_readfails = 0;
2287 if (dump_opt['b'] < 5 && isatty(STDERR_FILENO) &&
2288 gethrtime() > zcb->zcb_lastprint + NANOSEC) {
2289 uint64_t now = gethrtime();
2291 uint64_t bytes = zcb->zcb_type[ZB_TOTAL][ZDB_OT_TOTAL].zb_asize;
2293 1 + bytes / (1 + ((now - zcb->zcb_start) / 1000 / 1000));
2295 (zcb->zcb_totalasize - bytes) / 1024 / kb_per_sec;
2297 zfs_nicenum(bytes, buf, sizeof (buf));
2298 (void) fprintf(stderr,
2299 "\r%5s completed (%4dMB/s) "
2300 "estimated time remaining: %uhr %02umin %02usec ",
2301 buf, kb_per_sec / 1024,
2302 sec_remaining / 60 / 60,
2303 sec_remaining / 60 % 60,
2304 sec_remaining % 60);
2306 zcb->zcb_lastprint = now;
2313 zdb_leak(void *arg, uint64_t start, uint64_t size)
2317 (void) printf("leaked space: vdev %llu, offset 0x%llx, size %llu\n",
2318 (u_longlong_t)vd->vdev_id, (u_longlong_t)start, (u_longlong_t)size);
2321 static metaslab_ops_t zdb_metaslab_ops = {
2323 NULL /* fragmented */
2327 zdb_ddt_leak_init(spa_t *spa, zdb_cb_t *zcb)
2329 ddt_bookmark_t ddb = { 0 };
2333 while ((error = ddt_walk(spa, &ddb, &dde)) == 0) {
2335 ddt_phys_t *ddp = dde.dde_phys;
2337 if (ddb.ddb_class == DDT_CLASS_UNIQUE)
2340 ASSERT(ddt_phys_total_refcnt(&dde) > 1);
2342 for (int p = 0; p < DDT_PHYS_TYPES; p++, ddp++) {
2343 if (ddp->ddp_phys_birth == 0)
2345 ddt_bp_create(ddb.ddb_checksum,
2346 &dde.dde_key, ddp, &blk);
2347 if (p == DDT_PHYS_DITTO) {
2348 zdb_count_block(zcb, NULL, &blk, ZDB_OT_DITTO);
2350 zcb->zcb_dedup_asize +=
2351 BP_GET_ASIZE(&blk) * (ddp->ddp_refcnt - 1);
2352 zcb->zcb_dedup_blocks++;
2355 if (!dump_opt['L']) {
2356 ddt_t *ddt = spa->spa_ddt[ddb.ddb_checksum];
2358 VERIFY(ddt_lookup(ddt, &blk, B_TRUE) != NULL);
2363 ASSERT(error == ENOENT);
2367 zdb_leak_init(spa_t *spa, zdb_cb_t *zcb)
2371 if (!dump_opt['L']) {
2372 vdev_t *rvd = spa->spa_root_vdev;
2373 for (int c = 0; c < rvd->vdev_children; c++) {
2374 vdev_t *vd = rvd->vdev_child[c];
2375 for (int m = 0; m < vd->vdev_ms_count; m++) {
2376 metaslab_t *msp = vd->vdev_ms[m];
2377 mutex_enter(&msp->ms_lock);
2378 metaslab_unload(msp);
2381 * For leak detection, we overload the metaslab
2382 * ms_tree to contain allocated segments
2383 * instead of free segments. As a result,
2384 * we can't use the normal metaslab_load/unload
2387 if (msp->ms_sm != NULL) {
2388 msp->ms_ops = &zdb_metaslab_ops;
2389 VERIFY0(space_map_load(msp->ms_sm,
2390 msp->ms_tree, SM_ALLOC));
2391 msp->ms_loaded = B_TRUE;
2393 mutex_exit(&msp->ms_lock);
2398 spa_config_enter(spa, SCL_CONFIG, FTAG, RW_READER);
2400 zdb_ddt_leak_init(spa, zcb);
2402 spa_config_exit(spa, SCL_CONFIG, FTAG);
2406 zdb_leak_fini(spa_t *spa)
2408 if (!dump_opt['L']) {
2409 vdev_t *rvd = spa->spa_root_vdev;
2410 for (int c = 0; c < rvd->vdev_children; c++) {
2411 vdev_t *vd = rvd->vdev_child[c];
2412 for (int m = 0; m < vd->vdev_ms_count; m++) {
2413 metaslab_t *msp = vd->vdev_ms[m];
2414 mutex_enter(&msp->ms_lock);
2417 * The ms_tree has been overloaded to
2418 * contain allocated segments. Now that we
2419 * finished traversing all blocks, any
2420 * block that remains in the ms_tree
2421 * represents an allocated block that we
2422 * did not claim during the traversal.
2423 * Claimed blocks would have been removed
2426 range_tree_vacate(msp->ms_tree, zdb_leak, vd);
2427 msp->ms_loaded = B_FALSE;
2429 mutex_exit(&msp->ms_lock);
2437 count_block_cb(void *arg, const blkptr_t *bp, dmu_tx_t *tx)
2439 zdb_cb_t *zcb = arg;
2441 if (dump_opt['b'] >= 5) {
2442 char blkbuf[BP_SPRINTF_LEN];
2443 snprintf_blkptr(blkbuf, sizeof (blkbuf), bp);
2444 (void) printf("[%s] %s\n",
2445 "deferred free", blkbuf);
2447 zdb_count_block(zcb, NULL, bp, ZDB_OT_DEFERRED);
2452 dump_block_stats(spa_t *spa)
2454 zdb_cb_t zcb = { 0 };
2455 zdb_blkstats_t *zb, *tzb;
2456 uint64_t norm_alloc, norm_space, total_alloc, total_found;
2457 int flags = TRAVERSE_PRE | TRAVERSE_PREFETCH_METADATA | TRAVERSE_HARD;
2460 (void) printf("\nTraversing all blocks %s%s%s%s%s...\n\n",
2461 (dump_opt['c'] || !dump_opt['L']) ? "to verify " : "",
2462 (dump_opt['c'] == 1) ? "metadata " : "",
2463 dump_opt['c'] ? "checksums " : "",
2464 (dump_opt['c'] && !dump_opt['L']) ? "and verify " : "",
2465 !dump_opt['L'] ? "nothing leaked " : "");
2468 * Load all space maps as SM_ALLOC maps, then traverse the pool
2469 * claiming each block we discover. If the pool is perfectly
2470 * consistent, the space maps will be empty when we're done.
2471 * Anything left over is a leak; any block we can't claim (because
2472 * it's not part of any space map) is a double allocation,
2473 * reference to a freed block, or an unclaimed log block.
2475 zdb_leak_init(spa, &zcb);
2478 * If there's a deferred-free bplist, process that first.
2480 (void) bpobj_iterate_nofree(&spa->spa_deferred_bpobj,
2481 count_block_cb, &zcb, NULL);
2482 if (spa_version(spa) >= SPA_VERSION_DEADLISTS) {
2483 (void) bpobj_iterate_nofree(&spa->spa_dsl_pool->dp_free_bpobj,
2484 count_block_cb, &zcb, NULL);
2486 if (spa_feature_is_active(spa, SPA_FEATURE_ASYNC_DESTROY)) {
2487 VERIFY3U(0, ==, bptree_iterate(spa->spa_meta_objset,
2488 spa->spa_dsl_pool->dp_bptree_obj, B_FALSE, count_block_cb,
2492 if (dump_opt['c'] > 1)
2493 flags |= TRAVERSE_PREFETCH_DATA;
2495 zcb.zcb_totalasize = metaslab_class_get_alloc(spa_normal_class(spa));
2496 zcb.zcb_start = zcb.zcb_lastprint = gethrtime();
2497 zcb.zcb_haderrors |= traverse_pool(spa, 0, flags, zdb_blkptr_cb, &zcb);
2500 * If we've traversed the data blocks then we need to wait for those
2501 * I/Os to complete. We leverage "The Godfather" zio to wait on
2502 * all async I/Os to complete.
2504 if (dump_opt['c']) {
2505 (void) zio_wait(spa->spa_async_zio_root);
2506 spa->spa_async_zio_root = zio_root(spa, NULL, NULL,
2507 ZIO_FLAG_CANFAIL | ZIO_FLAG_SPECULATIVE |
2508 ZIO_FLAG_GODFATHER);
2511 if (zcb.zcb_haderrors) {
2512 (void) printf("\nError counts:\n\n");
2513 (void) printf("\t%5s %s\n", "errno", "count");
2514 for (int e = 0; e < 256; e++) {
2515 if (zcb.zcb_errors[e] != 0) {
2516 (void) printf("\t%5d %llu\n",
2517 e, (u_longlong_t)zcb.zcb_errors[e]);
2523 * Report any leaked segments.
2527 tzb = &zcb.zcb_type[ZB_TOTAL][ZDB_OT_TOTAL];
2529 norm_alloc = metaslab_class_get_alloc(spa_normal_class(spa));
2530 norm_space = metaslab_class_get_space(spa_normal_class(spa));
2532 total_alloc = norm_alloc + metaslab_class_get_alloc(spa_log_class(spa));
2533 total_found = tzb->zb_asize - zcb.zcb_dedup_asize;
2535 if (total_found == total_alloc) {
2537 (void) printf("\n\tNo leaks (block sum matches space"
2538 " maps exactly)\n");
2540 (void) printf("block traversal size %llu != alloc %llu "
2542 (u_longlong_t)total_found,
2543 (u_longlong_t)total_alloc,
2544 (dump_opt['L']) ? "unreachable" : "leaked",
2545 (longlong_t)(total_alloc - total_found));
2549 if (tzb->zb_count == 0)
2552 (void) printf("\n");
2553 (void) printf("\tbp count: %10llu\n",
2554 (u_longlong_t)tzb->zb_count);
2555 (void) printf("\tbp logical: %10llu avg: %6llu\n",
2556 (u_longlong_t)tzb->zb_lsize,
2557 (u_longlong_t)(tzb->zb_lsize / tzb->zb_count));
2558 (void) printf("\tbp physical: %10llu avg:"
2559 " %6llu compression: %6.2f\n",
2560 (u_longlong_t)tzb->zb_psize,
2561 (u_longlong_t)(tzb->zb_psize / tzb->zb_count),
2562 (double)tzb->zb_lsize / tzb->zb_psize);
2563 (void) printf("\tbp allocated: %10llu avg:"
2564 " %6llu compression: %6.2f\n",
2565 (u_longlong_t)tzb->zb_asize,
2566 (u_longlong_t)(tzb->zb_asize / tzb->zb_count),
2567 (double)tzb->zb_lsize / tzb->zb_asize);
2568 (void) printf("\tbp deduped: %10llu ref>1:"
2569 " %6llu deduplication: %6.2f\n",
2570 (u_longlong_t)zcb.zcb_dedup_asize,
2571 (u_longlong_t)zcb.zcb_dedup_blocks,
2572 (double)zcb.zcb_dedup_asize / tzb->zb_asize + 1.0);
2573 (void) printf("\tSPA allocated: %10llu used: %5.2f%%\n",
2574 (u_longlong_t)norm_alloc, 100.0 * norm_alloc / norm_space);
2576 if (dump_opt['b'] >= 2) {
2578 (void) printf("\nBlocks\tLSIZE\tPSIZE\tASIZE"
2579 "\t avg\t comp\t%%Total\tType\n");
2581 for (t = 0; t <= ZDB_OT_TOTAL; t++) {
2582 char csize[32], lsize[32], psize[32], asize[32];
2586 if (t < DMU_OT_NUMTYPES)
2587 typename = dmu_ot[t].ot_name;
2589 typename = zdb_ot_extname[t - DMU_OT_NUMTYPES];
2591 if (zcb.zcb_type[ZB_TOTAL][t].zb_asize == 0) {
2592 (void) printf("%6s\t%5s\t%5s\t%5s"
2593 "\t%5s\t%5s\t%6s\t%s\n",
2605 for (l = ZB_TOTAL - 1; l >= -1; l--) {
2606 level = (l == -1 ? ZB_TOTAL : l);
2607 zb = &zcb.zcb_type[level][t];
2609 if (zb->zb_asize == 0)
2612 if (dump_opt['b'] < 3 && level != ZB_TOTAL)
2615 if (level == 0 && zb->zb_asize ==
2616 zcb.zcb_type[ZB_TOTAL][t].zb_asize)
2619 zdb_nicenum(zb->zb_count, csize);
2620 zdb_nicenum(zb->zb_lsize, lsize);
2621 zdb_nicenum(zb->zb_psize, psize);
2622 zdb_nicenum(zb->zb_asize, asize);
2623 zdb_nicenum(zb->zb_asize / zb->zb_count, avg);
2625 (void) printf("%6s\t%5s\t%5s\t%5s\t%5s"
2627 csize, lsize, psize, asize, avg,
2628 (double)zb->zb_lsize / zb->zb_psize,
2629 100.0 * zb->zb_asize / tzb->zb_asize);
2631 if (level == ZB_TOTAL)
2632 (void) printf("%s\n", typename);
2634 (void) printf(" L%d %s\n",
2637 if (dump_opt['b'] >= 4) {
2638 (void) printf("psize "
2639 "(in 512-byte sectors): "
2640 "number of blocks\n");
2641 dump_histogram(zb->zb_psize_histogram,
2642 PSIZE_HISTO_SIZE, 0);
2648 (void) printf("\n");
2653 if (zcb.zcb_haderrors)
2659 typedef struct zdb_ddt_entry {
2661 uint64_t zdde_ref_blocks;
2662 uint64_t zdde_ref_lsize;
2663 uint64_t zdde_ref_psize;
2664 uint64_t zdde_ref_dsize;
2665 avl_node_t zdde_node;
2670 zdb_ddt_add_cb(spa_t *spa, zilog_t *zilog, const blkptr_t *bp,
2671 const zbookmark_t *zb, const dnode_phys_t *dnp, void *arg)
2673 avl_tree_t *t = arg;
2675 zdb_ddt_entry_t *zdde, zdde_search;
2680 if (dump_opt['S'] > 1 && zb->zb_level == ZB_ROOT_LEVEL) {
2681 (void) printf("traversing objset %llu, %llu objects, "
2682 "%lu blocks so far\n",
2683 (u_longlong_t)zb->zb_objset,
2684 (u_longlong_t)bp->blk_fill,
2688 if (BP_IS_HOLE(bp) || BP_GET_CHECKSUM(bp) == ZIO_CHECKSUM_OFF ||
2689 BP_GET_LEVEL(bp) > 0 || DMU_OT_IS_METADATA(BP_GET_TYPE(bp)))
2692 ddt_key_fill(&zdde_search.zdde_key, bp);
2694 zdde = avl_find(t, &zdde_search, &where);
2697 zdde = umem_zalloc(sizeof (*zdde), UMEM_NOFAIL);
2698 zdde->zdde_key = zdde_search.zdde_key;
2699 avl_insert(t, zdde, where);
2702 zdde->zdde_ref_blocks += 1;
2703 zdde->zdde_ref_lsize += BP_GET_LSIZE(bp);
2704 zdde->zdde_ref_psize += BP_GET_PSIZE(bp);
2705 zdde->zdde_ref_dsize += bp_get_dsize_sync(spa, bp);
2711 dump_simulated_ddt(spa_t *spa)
2714 void *cookie = NULL;
2715 zdb_ddt_entry_t *zdde;
2716 ddt_histogram_t ddh_total = { 0 };
2717 ddt_stat_t dds_total = { 0 };
2719 avl_create(&t, ddt_entry_compare,
2720 sizeof (zdb_ddt_entry_t), offsetof(zdb_ddt_entry_t, zdde_node));
2722 spa_config_enter(spa, SCL_CONFIG, FTAG, RW_READER);
2724 (void) traverse_pool(spa, 0, TRAVERSE_PRE | TRAVERSE_PREFETCH_METADATA,
2725 zdb_ddt_add_cb, &t);
2727 spa_config_exit(spa, SCL_CONFIG, FTAG);
2729 while ((zdde = avl_destroy_nodes(&t, &cookie)) != NULL) {
2731 uint64_t refcnt = zdde->zdde_ref_blocks;
2732 ASSERT(refcnt != 0);
2734 dds.dds_blocks = zdde->zdde_ref_blocks / refcnt;
2735 dds.dds_lsize = zdde->zdde_ref_lsize / refcnt;
2736 dds.dds_psize = zdde->zdde_ref_psize / refcnt;
2737 dds.dds_dsize = zdde->zdde_ref_dsize / refcnt;
2739 dds.dds_ref_blocks = zdde->zdde_ref_blocks;
2740 dds.dds_ref_lsize = zdde->zdde_ref_lsize;
2741 dds.dds_ref_psize = zdde->zdde_ref_psize;
2742 dds.dds_ref_dsize = zdde->zdde_ref_dsize;
2744 ddt_stat_add(&ddh_total.ddh_stat[highbit64(refcnt) - 1],
2747 umem_free(zdde, sizeof (*zdde));
2752 ddt_histogram_stat(&dds_total, &ddh_total);
2754 (void) printf("Simulated DDT histogram:\n");
2756 zpool_dump_ddt(&dds_total, &ddh_total);
2758 dump_dedup_ratio(&dds_total);
2762 dump_zpool(spa_t *spa)
2764 dsl_pool_t *dp = spa_get_dsl(spa);
2767 if (dump_opt['S']) {
2768 dump_simulated_ddt(spa);
2772 if (!dump_opt['e'] && dump_opt['C'] > 1) {
2773 (void) printf("\nCached configuration:\n");
2774 dump_nvlist(spa->spa_config, 8);
2781 dump_uberblock(&spa->spa_uberblock, "\nUberblock:\n", "\n");
2786 if (dump_opt['d'] > 2 || dump_opt['m'])
2787 dump_metaslabs(spa);
2789 if (dump_opt['d'] || dump_opt['i']) {
2790 dump_dir(dp->dp_meta_objset);
2791 if (dump_opt['d'] >= 3) {
2792 dump_bpobj(&spa->spa_deferred_bpobj,
2793 "Deferred frees", 0);
2794 if (spa_version(spa) >= SPA_VERSION_DEADLISTS) {
2795 dump_bpobj(&spa->spa_dsl_pool->dp_free_bpobj,
2796 "Pool snapshot frees", 0);
2799 if (spa_feature_is_active(spa,
2800 SPA_FEATURE_ASYNC_DESTROY)) {
2801 dump_bptree(spa->spa_meta_objset,
2802 spa->spa_dsl_pool->dp_bptree_obj,
2803 "Pool dataset frees");
2805 dump_dtl(spa->spa_root_vdev, 0);
2807 (void) dmu_objset_find(spa_name(spa), dump_one_dir,
2808 NULL, DS_FIND_SNAPSHOTS | DS_FIND_CHILDREN);
2810 if (dump_opt['b'] || dump_opt['c'])
2811 rc = dump_block_stats(spa);
2814 rc = verify_spacemap_refcounts(spa);
2817 show_pool_stats(spa);
2826 #define ZDB_FLAG_CHECKSUM 0x0001
2827 #define ZDB_FLAG_DECOMPRESS 0x0002
2828 #define ZDB_FLAG_BSWAP 0x0004
2829 #define ZDB_FLAG_GBH 0x0008
2830 #define ZDB_FLAG_INDIRECT 0x0010
2831 #define ZDB_FLAG_PHYS 0x0020
2832 #define ZDB_FLAG_RAW 0x0040
2833 #define ZDB_FLAG_PRINT_BLKPTR 0x0080
2838 zdb_print_blkptr(blkptr_t *bp, int flags)
2840 char blkbuf[BP_SPRINTF_LEN];
2842 if (flags & ZDB_FLAG_BSWAP)
2843 byteswap_uint64_array((void *)bp, sizeof (blkptr_t));
2845 snprintf_blkptr(blkbuf, sizeof (blkbuf), bp);
2846 (void) printf("%s\n", blkbuf);
2850 zdb_dump_indirect(blkptr_t *bp, int nbps, int flags)
2854 for (i = 0; i < nbps; i++)
2855 zdb_print_blkptr(&bp[i], flags);
2859 zdb_dump_gbh(void *buf, int flags)
2861 zdb_dump_indirect((blkptr_t *)buf, SPA_GBH_NBLKPTRS, flags);
2865 zdb_dump_block_raw(void *buf, uint64_t size, int flags)
2867 if (flags & ZDB_FLAG_BSWAP)
2868 byteswap_uint64_array(buf, size);
2869 (void) write(1, buf, size);
2873 zdb_dump_block(char *label, void *buf, uint64_t size, int flags)
2875 uint64_t *d = (uint64_t *)buf;
2876 int nwords = size / sizeof (uint64_t);
2877 int do_bswap = !!(flags & ZDB_FLAG_BSWAP);
2883 hdr = " 7 6 5 4 3 2 1 0 f e d c b a 9 8";
2885 hdr = " 0 1 2 3 4 5 6 7 8 9 a b c d e f";
2887 (void) printf("\n%s\n%6s %s 0123456789abcdef\n", label, "", hdr);
2889 for (i = 0; i < nwords; i += 2) {
2890 (void) printf("%06llx: %016llx %016llx ",
2891 (u_longlong_t)(i * sizeof (uint64_t)),
2892 (u_longlong_t)(do_bswap ? BSWAP_64(d[i]) : d[i]),
2893 (u_longlong_t)(do_bswap ? BSWAP_64(d[i + 1]) : d[i + 1]));
2896 for (j = 0; j < 2 * sizeof (uint64_t); j++)
2897 (void) printf("%c", isprint(c[j]) ? c[j] : '.');
2898 (void) printf("\n");
2903 * There are two acceptable formats:
2904 * leaf_name - For example: c1t0d0 or /tmp/ztest.0a
2905 * child[.child]* - For example: 0.1.1
2907 * The second form can be used to specify arbitrary vdevs anywhere
2908 * in the heirarchy. For example, in a pool with a mirror of
2909 * RAID-Zs, you can specify either RAID-Z vdev with 0.0 or 0.1 .
2912 zdb_vdev_lookup(vdev_t *vdev, char *path)
2920 /* First, assume the x.x.x.x format */
2921 i = (int)strtoul(path, &s, 10);
2922 if (s == path || (s && *s != '.' && *s != '\0'))
2924 if (i < 0 || i >= vdev->vdev_children)
2927 vdev = vdev->vdev_child[i];
2930 return (zdb_vdev_lookup(vdev, s+1));
2933 for (i = 0; i < vdev->vdev_children; i++) {
2934 vdev_t *vc = vdev->vdev_child[i];
2936 if (vc->vdev_path == NULL) {
2937 vc = zdb_vdev_lookup(vc, path);
2944 p = strrchr(vc->vdev_path, '/');
2945 p = p ? p + 1 : vc->vdev_path;
2946 q = &vc->vdev_path[strlen(vc->vdev_path) - 2];
2948 if (strcmp(vc->vdev_path, path) == 0)
2950 if (strcmp(p, path) == 0)
2952 if (strcmp(q, "s0") == 0 && strncmp(p, path, q - p) == 0)
2960 * Read a block from a pool and print it out. The syntax of the
2961 * block descriptor is:
2963 * pool:vdev_specifier:offset:size[:flags]
2965 * pool - The name of the pool you wish to read from
2966 * vdev_specifier - Which vdev (see comment for zdb_vdev_lookup)
2967 * offset - offset, in hex, in bytes
2968 * size - Amount of data to read, in hex, in bytes
2969 * flags - A string of characters specifying options
2970 * b: Decode a blkptr at given offset within block
2971 * *c: Calculate and display checksums
2972 * d: Decompress data before dumping
2973 * e: Byteswap data before dumping
2974 * g: Display data as a gang block header
2975 * i: Display as an indirect block
2976 * p: Do I/O to physical offset
2977 * r: Dump raw data to stdout
2979 * * = not yet implemented
2982 zdb_read_block(char *thing, spa_t *spa)
2984 blkptr_t blk, *bp = &blk;
2985 dva_t *dva = bp->blk_dva;
2987 uint64_t offset = 0, size = 0, psize = 0, lsize = 0, blkptr_offset = 0;
2990 void *pbuf, *lbuf, *buf;
2991 char *s, *p, *dup, *vdev, *flagstr;
2994 dup = strdup(thing);
2995 s = strtok(dup, ":");
2997 s = strtok(NULL, ":");
2998 offset = strtoull(s ? s : "", NULL, 16);
2999 s = strtok(NULL, ":");
3000 size = strtoull(s ? s : "", NULL, 16);
3001 s = strtok(NULL, ":");
3002 flagstr = s ? s : "";
3006 s = "size must not be zero";
3007 if (!IS_P2ALIGNED(size, DEV_BSIZE))
3008 s = "size must be a multiple of sector size";
3009 if (!IS_P2ALIGNED(offset, DEV_BSIZE))
3010 s = "offset must be a multiple of sector size";
3012 (void) printf("Invalid block specifier: %s - %s\n", thing, s);
3017 for (s = strtok(flagstr, ":"); s; s = strtok(NULL, ":")) {
3018 for (i = 0; flagstr[i]; i++) {
3019 int bit = flagbits[(uchar_t)flagstr[i]];
3022 (void) printf("***Invalid flag: %c\n",
3028 /* If it's not something with an argument, keep going */
3029 if ((bit & (ZDB_FLAG_CHECKSUM |
3030 ZDB_FLAG_PRINT_BLKPTR)) == 0)
3033 p = &flagstr[i + 1];
3034 if (bit == ZDB_FLAG_PRINT_BLKPTR)
3035 blkptr_offset = strtoull(p, &p, 16);
3036 if (*p != ':' && *p != '\0') {
3037 (void) printf("***Invalid flag arg: '%s'\n", s);
3041 i += p - &flagstr[i + 1]; /* skip over the number */
3045 vd = zdb_vdev_lookup(spa->spa_root_vdev, vdev);
3047 (void) printf("***Invalid vdev: %s\n", vdev);
3052 (void) fprintf(stderr, "Found vdev: %s\n",
3055 (void) fprintf(stderr, "Found vdev type: %s\n",
3056 vd->vdev_ops->vdev_op_type);
3062 pbuf = umem_alloc(SPA_MAXBLOCKSIZE, UMEM_NOFAIL);
3063 lbuf = umem_alloc(SPA_MAXBLOCKSIZE, UMEM_NOFAIL);
3067 DVA_SET_VDEV(&dva[0], vd->vdev_id);
3068 DVA_SET_OFFSET(&dva[0], offset);
3069 DVA_SET_GANG(&dva[0], !!(flags & ZDB_FLAG_GBH));
3070 DVA_SET_ASIZE(&dva[0], vdev_psize_to_asize(vd, psize));
3072 BP_SET_BIRTH(bp, TXG_INITIAL, TXG_INITIAL);
3074 BP_SET_LSIZE(bp, lsize);
3075 BP_SET_PSIZE(bp, psize);
3076 BP_SET_COMPRESS(bp, ZIO_COMPRESS_OFF);
3077 BP_SET_CHECKSUM(bp, ZIO_CHECKSUM_OFF);
3078 BP_SET_TYPE(bp, DMU_OT_NONE);
3079 BP_SET_LEVEL(bp, 0);
3080 BP_SET_DEDUP(bp, 0);
3081 BP_SET_BYTEORDER(bp, ZFS_HOST_BYTEORDER);
3083 spa_config_enter(spa, SCL_STATE, FTAG, RW_READER);
3084 zio = zio_root(spa, NULL, NULL, 0);
3086 if (vd == vd->vdev_top) {
3088 * Treat this as a normal block read.
3090 zio_nowait(zio_read(zio, spa, bp, pbuf, psize, NULL, NULL,
3091 ZIO_PRIORITY_SYNC_READ,
3092 ZIO_FLAG_CANFAIL | ZIO_FLAG_RAW, NULL));
3095 * Treat this as a vdev child I/O.
3097 zio_nowait(zio_vdev_child_io(zio, bp, vd, offset, pbuf, psize,
3098 ZIO_TYPE_READ, ZIO_PRIORITY_SYNC_READ,
3099 ZIO_FLAG_DONT_CACHE | ZIO_FLAG_DONT_QUEUE |
3100 ZIO_FLAG_DONT_PROPAGATE | ZIO_FLAG_DONT_RETRY |
3101 ZIO_FLAG_CANFAIL | ZIO_FLAG_RAW, NULL, NULL));
3104 error = zio_wait(zio);
3105 spa_config_exit(spa, SCL_STATE, FTAG);
3108 (void) printf("Read of %s failed, error: %d\n", thing, error);
3112 if (flags & ZDB_FLAG_DECOMPRESS) {
3114 * We don't know how the data was compressed, so just try
3115 * every decompress function at every inflated blocksize.
3117 enum zio_compress c;
3118 void *pbuf2 = umem_alloc(SPA_MAXBLOCKSIZE, UMEM_NOFAIL);
3119 void *lbuf2 = umem_alloc(SPA_MAXBLOCKSIZE, UMEM_NOFAIL);
3121 bcopy(pbuf, pbuf2, psize);
3123 VERIFY(random_get_pseudo_bytes((uint8_t *)pbuf + psize,
3124 SPA_MAXBLOCKSIZE - psize) == 0);
3126 VERIFY(random_get_pseudo_bytes((uint8_t *)pbuf2 + psize,
3127 SPA_MAXBLOCKSIZE - psize) == 0);
3129 for (lsize = SPA_MAXBLOCKSIZE; lsize > psize;
3130 lsize -= SPA_MINBLOCKSIZE) {
3131 for (c = 0; c < ZIO_COMPRESS_FUNCTIONS; c++) {
3132 if (zio_decompress_data(c, pbuf, lbuf,
3133 psize, lsize) == 0 &&
3134 zio_decompress_data(c, pbuf2, lbuf2,
3135 psize, lsize) == 0 &&
3136 bcmp(lbuf, lbuf2, lsize) == 0)
3139 if (c != ZIO_COMPRESS_FUNCTIONS)
3141 lsize -= SPA_MINBLOCKSIZE;
3144 umem_free(pbuf2, SPA_MAXBLOCKSIZE);
3145 umem_free(lbuf2, SPA_MAXBLOCKSIZE);
3147 if (lsize <= psize) {
3148 (void) printf("Decompress of %s failed\n", thing);
3158 if (flags & ZDB_FLAG_PRINT_BLKPTR)
3159 zdb_print_blkptr((blkptr_t *)(void *)
3160 ((uintptr_t)buf + (uintptr_t)blkptr_offset), flags);
3161 else if (flags & ZDB_FLAG_RAW)
3162 zdb_dump_block_raw(buf, size, flags);
3163 else if (flags & ZDB_FLAG_INDIRECT)
3164 zdb_dump_indirect((blkptr_t *)buf, size / sizeof (blkptr_t),
3166 else if (flags & ZDB_FLAG_GBH)
3167 zdb_dump_gbh(buf, flags);
3169 zdb_dump_block(thing, buf, size, flags);
3172 umem_free(pbuf, SPA_MAXBLOCKSIZE);
3173 umem_free(lbuf, SPA_MAXBLOCKSIZE);
3178 pool_match(nvlist_t *cfg, char *tgt)
3180 uint64_t v, guid = strtoull(tgt, NULL, 0);
3184 if (nvlist_lookup_uint64(cfg, ZPOOL_CONFIG_POOL_GUID, &v) == 0)
3187 if (nvlist_lookup_string(cfg, ZPOOL_CONFIG_POOL_NAME, &s) == 0)
3188 return (strcmp(s, tgt) == 0);
3194 find_zpool(char **target, nvlist_t **configp, int dirc, char **dirv)
3197 nvlist_t *match = NULL;
3202 importargs_t args = { 0 };
3206 args.can_be_active = B_TRUE;
3208 if ((sepp = strpbrk(*target, "/@")) != NULL) {
3213 pools = zpool_search_import(g_zfs, &args);
3215 if (pools != NULL) {
3216 nvpair_t *elem = NULL;
3217 while ((elem = nvlist_next_nvpair(pools, elem)) != NULL) {
3218 verify(nvpair_value_nvlist(elem, configp) == 0);
3219 if (pool_match(*configp, *target)) {
3221 if (match != NULL) {
3222 /* print previously found config */
3224 (void) printf("%s\n", name);
3225 dump_nvlist(match, 8);
3228 (void) printf("%s\n",
3230 dump_nvlist(*configp, 8);
3233 name = nvpair_name(elem);
3239 (void) fatal("\tMatched %d pools - use pool GUID "
3240 "instead of pool name or \n"
3241 "\tpool name part of a dataset name to select pool", count);
3246 * If pool GUID was specified for pool id, replace it with pool name
3248 if (name && (strstr(*target, name) != *target)) {
3249 int sz = 1 + strlen(name) + ((sepp) ? strlen(sepp) : 0);
3251 *target = umem_alloc(sz, UMEM_NOFAIL);
3252 (void) snprintf(*target, sz, "%s%s", name, sepp ? sepp : "");
3255 *configp = name ? match : NULL;
3261 main(int argc, char **argv)
3264 struct rlimit rl = { 1024, 1024 };
3266 objset_t *os = NULL;
3270 char **searchdirs = NULL;
3273 nvlist_t *policy = NULL;
3274 uint64_t max_txg = UINT64_MAX;
3275 int rewind = ZPOOL_NEVER_REWIND;
3277 (void) setrlimit(RLIMIT_NOFILE, &rl);
3278 (void) enable_extended_FILE_stdio(-1, -1);
3280 dprintf_setup(&argc, argv);
3282 while ((c = getopt(argc, argv, "bcdhilmM:suCDRSAFLXevp:t:U:P")) != -1) {
3312 max_inflight = strtoull(optarg, NULL, 0);
3313 if (max_inflight == 0) {
3314 (void) fprintf(stderr, "maximum number "
3315 "of inflight I/Os must be greater "
3321 if (searchdirs == NULL) {
3322 searchdirs = umem_alloc(sizeof (char *),
3325 char **tmp = umem_alloc((nsearch + 1) *
3326 sizeof (char *), UMEM_NOFAIL);
3327 bcopy(searchdirs, tmp, nsearch *
3329 umem_free(searchdirs,
3330 nsearch * sizeof (char *));
3333 searchdirs[nsearch++] = optarg;
3336 max_txg = strtoull(optarg, NULL, 0);
3337 if (max_txg < TXG_INITIAL) {
3338 (void) fprintf(stderr, "incorrect txg "
3339 "specified: %s\n", optarg);
3344 spa_config_path = optarg;
3352 if (!dump_opt['e'] && searchdirs != NULL) {
3353 (void) fprintf(stderr, "-p option requires use of -e\n");
3358 g_zfs = libzfs_init();
3359 ASSERT(g_zfs != NULL);
3362 verbose = MAX(verbose, 1);
3364 for (c = 0; c < 256; c++) {
3365 if (dump_all && !strchr("elAFLRSXP", c))
3368 dump_opt[c] += verbose;
3371 aok = (dump_opt['A'] == 1) || (dump_opt['A'] > 2);
3372 zfs_recover = (dump_opt['A'] > 1);
3377 if (argc < 2 && dump_opt['R'])
3380 if (!dump_opt['e'] && dump_opt['C']) {
3381 dump_cachefile(spa_config_path);
3387 if (dump_opt['l']) {
3388 dump_label(argv[0]);
3392 if (dump_opt['X'] || dump_opt['F'])
3393 rewind = ZPOOL_DO_REWIND |
3394 (dump_opt['X'] ? ZPOOL_EXTREME_REWIND : 0);
3396 if (nvlist_alloc(&policy, NV_UNIQUE_NAME_TYPE, 0) != 0 ||
3397 nvlist_add_uint64(policy, ZPOOL_REWIND_REQUEST_TXG, max_txg) != 0 ||
3398 nvlist_add_uint32(policy, ZPOOL_REWIND_REQUEST, rewind) != 0)
3399 fatal("internal error: %s", strerror(ENOMEM));
3404 if (dump_opt['e']) {
3405 nvlist_t *cfg = NULL;
3406 char *name = find_zpool(&target, &cfg, nsearch, searchdirs);
3410 if (dump_opt['C'] > 1) {
3411 (void) printf("\nConfiguration for import:\n");
3412 dump_nvlist(cfg, 8);
3414 if (nvlist_add_nvlist(cfg,
3415 ZPOOL_REWIND_POLICY, policy) != 0) {
3416 fatal("can't open '%s': %s",
3417 target, strerror(ENOMEM));
3419 if ((error = spa_import(name, cfg, NULL,
3420 ZFS_IMPORT_MISSING_LOG)) != 0) {
3421 error = spa_import(name, cfg, NULL,
3422 ZFS_IMPORT_VERBATIM);
3428 if (strpbrk(target, "/@") == NULL || dump_opt['R']) {
3429 error = spa_open_rewind(target, &spa, FTAG, policy,
3433 * If we're missing the log device then
3434 * try opening the pool after clearing the
3437 mutex_enter(&spa_namespace_lock);
3438 if ((spa = spa_lookup(target)) != NULL &&
3439 spa->spa_log_state == SPA_LOG_MISSING) {
3440 spa->spa_log_state = SPA_LOG_CLEAR;
3443 mutex_exit(&spa_namespace_lock);
3446 error = spa_open_rewind(target, &spa,
3447 FTAG, policy, NULL);
3451 error = dmu_objset_own(target, DMU_OST_ANY,
3455 nvlist_free(policy);
3458 fatal("can't open '%s': %s", target, strerror(error));
3462 if (!dump_opt['R']) {
3464 zopt_objects = argc;
3465 zopt_object = calloc(zopt_objects, sizeof (uint64_t));
3466 for (i = 0; i < zopt_objects; i++) {
3468 zopt_object[i] = strtoull(argv[i], NULL, 0);
3469 if (zopt_object[i] == 0 && errno != 0)
3470 fatal("bad number %s: %s",
3471 argv[i], strerror(errno));
3476 } else if (zopt_objects > 0 && !dump_opt['m']) {
3477 dump_dir(spa->spa_meta_objset);
3482 flagbits['b'] = ZDB_FLAG_PRINT_BLKPTR;
3483 flagbits['c'] = ZDB_FLAG_CHECKSUM;
3484 flagbits['d'] = ZDB_FLAG_DECOMPRESS;
3485 flagbits['e'] = ZDB_FLAG_BSWAP;
3486 flagbits['g'] = ZDB_FLAG_GBH;
3487 flagbits['i'] = ZDB_FLAG_INDIRECT;
3488 flagbits['p'] = ZDB_FLAG_PHYS;
3489 flagbits['r'] = ZDB_FLAG_RAW;
3491 for (i = 0; i < argc; i++)
3492 zdb_read_block(argv[i], spa);
3495 (os != NULL) ? dmu_objset_disown(os, FTAG) : spa_close(spa, FTAG);
3497 fuid_table_destroy();
3498 sa_loaded = B_FALSE;