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) 2013 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>
65 #define ZDB_COMPRESS_NAME(idx) ((idx) < ZIO_COMPRESS_FUNCTIONS ? \
66 zio_compress_table[(idx)].ci_name : "UNKNOWN")
67 #define ZDB_CHECKSUM_NAME(idx) ((idx) < ZIO_CHECKSUM_FUNCTIONS ? \
68 zio_checksum_table[(idx)].ci_name : "UNKNOWN")
69 #define ZDB_OT_NAME(idx) ((idx) < DMU_OT_NUMTYPES ? \
70 dmu_ot[(idx)].ot_name : DMU_OT_IS_VALID(idx) ? \
71 dmu_ot_byteswap[DMU_OT_BYTESWAP(idx)].ob_name : "UNKNOWN")
72 #define ZDB_OT_TYPE(idx) ((idx) < DMU_OT_NUMTYPES ? (idx) : \
73 (((idx) == DMU_OTN_ZAP_DATA || (idx) == DMU_OTN_ZAP_METADATA) ? \
74 DMU_OT_ZAP_OTHER : DMU_OT_NUMTYPES))
77 extern int zfs_recover;
82 const char cmdname[] = "zdb";
83 uint8_t dump_opt[256];
85 typedef void object_viewer_t(objset_t *, uint64_t, void *data, size_t size);
87 extern void dump_intent_log(zilog_t *);
88 uint64_t *zopt_object = NULL;
90 libzfs_handle_t *g_zfs;
91 uint64_t max_inflight = 200;
94 * These libumem hooks provide a reasonable set of defaults for the allocator's
95 * debugging facilities.
98 _umem_debug_init(void)
100 return ("default,verbose"); /* $UMEM_DEBUG setting */
104 _umem_logging_init(void)
106 return ("fail,contents"); /* $UMEM_LOGGING setting */
112 (void) fprintf(stderr,
113 "Usage: %s [-CumdibcsDvhLXFPA] [-t txg] [-e [-p path...]] "
114 "[-U config] [-M inflight I/Os] poolname [object...]\n"
115 " %s [-divPA] [-e -p path...] [-U config] dataset "
117 " %s -m [-LXFPA] [-t txg] [-e [-p path...]] [-U config] "
118 "poolname [vdev [metaslab...]]\n"
119 " %s -R [-A] [-e [-p path...]] poolname "
120 "vdev:offset:size[:flags]\n"
121 " %s -S [-PA] [-e [-p path...]] [-U config] poolname\n"
122 " %s -l [-uA] device\n"
123 " %s -C [-A] [-U config]\n\n",
124 cmdname, cmdname, cmdname, cmdname, cmdname, cmdname, cmdname);
126 (void) fprintf(stderr, " Dataset name must include at least one "
127 "separator character '/' or '@'\n");
128 (void) fprintf(stderr, " If dataset name is specified, only that "
129 "dataset is dumped\n");
130 (void) fprintf(stderr, " If object numbers are specified, only "
131 "those objects are dumped\n\n");
132 (void) fprintf(stderr, " Options to control amount of output:\n");
133 (void) fprintf(stderr, " -u uberblock\n");
134 (void) fprintf(stderr, " -d dataset(s)\n");
135 (void) fprintf(stderr, " -i intent logs\n");
136 (void) fprintf(stderr, " -C config (or cachefile if alone)\n");
137 (void) fprintf(stderr, " -h pool history\n");
138 (void) fprintf(stderr, " -b block statistics\n");
139 (void) fprintf(stderr, " -m metaslabs\n");
140 (void) fprintf(stderr, " -c checksum all metadata (twice for "
141 "all data) blocks\n");
142 (void) fprintf(stderr, " -s report stats on zdb's I/O\n");
143 (void) fprintf(stderr, " -D dedup statistics\n");
144 (void) fprintf(stderr, " -S simulate dedup to measure effect\n");
145 (void) fprintf(stderr, " -v verbose (applies to all others)\n");
146 (void) fprintf(stderr, " -l dump label contents\n");
147 (void) fprintf(stderr, " -L disable leak tracking (do not "
148 "load spacemaps)\n");
149 (void) fprintf(stderr, " -R read and display block from a "
151 (void) fprintf(stderr, " Below options are intended for use "
152 "with other options (except -l):\n");
153 (void) fprintf(stderr, " -A ignore assertions (-A), enable "
154 "panic recovery (-AA) or both (-AAA)\n");
155 (void) fprintf(stderr, " -F attempt automatic rewind within "
156 "safe range of transaction groups\n");
157 (void) fprintf(stderr, " -U <cachefile_path> -- use alternate "
159 (void) fprintf(stderr, " -X attempt extreme rewind (does not "
160 "work with dataset)\n");
161 (void) fprintf(stderr, " -e pool is exported/destroyed/"
162 "has altroot/not in a cachefile\n");
163 (void) fprintf(stderr, " -p <path> -- use one or more with "
164 "-e to specify path to vdev dir\n");
165 (void) fprintf(stderr, " -P print numbers in parseable form\n");
166 (void) fprintf(stderr, " -t <txg> -- highest txg to use when "
167 "searching for uberblocks\n");
168 (void) fprintf(stderr, " -M <number of inflight I/Os> -- "
169 "specify the maximum number of checksumming I/Os "
170 "[default is 200]\n");
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)
530 if (vd->vdev_ops->vdev_op_leaf) {
531 space_map_t *sm = vd->vdev_dtl_sm;
534 sm->sm_dbuf->db_size == sizeof (space_map_phys_t))
539 for (c = 0; c < vd->vdev_children; c++)
540 refcount += get_dtl_refcount(vd->vdev_child[c]);
545 get_metaslab_refcount(vdev_t *vd)
550 if (vd->vdev_top == vd) {
551 for (m = 0; m < vd->vdev_ms_count; m++) {
552 space_map_t *sm = vd->vdev_ms[m]->ms_sm;
555 sm->sm_dbuf->db_size == sizeof (space_map_phys_t))
559 for (c = 0; c < vd->vdev_children; c++)
560 refcount += get_metaslab_refcount(vd->vdev_child[c]);
566 verify_spacemap_refcounts(spa_t *spa)
568 uint64_t expected_refcount = 0;
569 uint64_t actual_refcount;
571 (void) feature_get_refcount(spa,
572 &spa_feature_table[SPA_FEATURE_SPACEMAP_HISTOGRAM],
574 actual_refcount = get_dtl_refcount(spa->spa_root_vdev);
575 actual_refcount += get_metaslab_refcount(spa->spa_root_vdev);
577 if (expected_refcount != actual_refcount) {
578 (void) printf("space map refcount mismatch: expected %lld != "
580 (longlong_t)expected_refcount,
581 (longlong_t)actual_refcount);
588 dump_spacemap(objset_t *os, space_map_t *sm)
590 uint64_t alloc, offset, entry;
591 char *ddata[] = { "ALLOC", "FREE", "CONDENSE", "INVALID",
592 "INVALID", "INVALID", "INVALID", "INVALID" };
598 * Print out the freelist entries in both encoded and decoded form.
601 for (offset = 0; offset < space_map_length(sm);
602 offset += sizeof (entry)) {
603 uint8_t mapshift = sm->sm_shift;
605 VERIFY0(dmu_read(os, space_map_object(sm), offset,
606 sizeof (entry), &entry, DMU_READ_PREFETCH));
607 if (SM_DEBUG_DECODE(entry)) {
609 (void) printf("\t [%6llu] %s: txg %llu, pass %llu\n",
610 (u_longlong_t)(offset / sizeof (entry)),
611 ddata[SM_DEBUG_ACTION_DECODE(entry)],
612 (u_longlong_t)SM_DEBUG_TXG_DECODE(entry),
613 (u_longlong_t)SM_DEBUG_SYNCPASS_DECODE(entry));
615 (void) printf("\t [%6llu] %c range:"
616 " %010llx-%010llx size: %06llx\n",
617 (u_longlong_t)(offset / sizeof (entry)),
618 SM_TYPE_DECODE(entry) == SM_ALLOC ? 'A' : 'F',
619 (u_longlong_t)((SM_OFFSET_DECODE(entry) <<
620 mapshift) + sm->sm_start),
621 (u_longlong_t)((SM_OFFSET_DECODE(entry) <<
622 mapshift) + sm->sm_start +
623 (SM_RUN_DECODE(entry) << mapshift)),
624 (u_longlong_t)(SM_RUN_DECODE(entry) << mapshift));
625 if (SM_TYPE_DECODE(entry) == SM_ALLOC)
626 alloc += SM_RUN_DECODE(entry) << mapshift;
628 alloc -= SM_RUN_DECODE(entry) << mapshift;
631 if (alloc != space_map_allocated(sm)) {
632 (void) printf("space_map_object alloc (%llu) INCONSISTENT "
633 "with space map summary (%llu)\n",
634 (u_longlong_t)space_map_allocated(sm), (u_longlong_t)alloc);
639 dump_metaslab_stats(metaslab_t *msp)
642 range_tree_t *rt = msp->ms_tree;
643 avl_tree_t *t = &msp->ms_size_tree;
644 int free_pct = range_tree_space(rt) * 100 / msp->ms_size;
646 zdb_nicenum(metaslab_block_maxsize(msp), maxbuf);
648 (void) printf("\t %25s %10lu %7s %6s %4s %4d%%\n",
649 "segments", avl_numnodes(t), "maxsize", maxbuf,
650 "freepct", free_pct);
651 (void) printf("\tIn-memory histogram:\n");
652 dump_histogram(rt->rt_histogram, RANGE_TREE_HISTOGRAM_SIZE, 0);
656 dump_metaslab(metaslab_t *msp)
658 vdev_t *vd = msp->ms_group->mg_vd;
659 spa_t *spa = vd->vdev_spa;
660 space_map_t *sm = msp->ms_sm;
663 zdb_nicenum(msp->ms_size - space_map_allocated(sm), freebuf);
666 "\tmetaslab %6llu offset %12llx spacemap %6llu free %5s\n",
667 (u_longlong_t)msp->ms_id, (u_longlong_t)msp->ms_start,
668 (u_longlong_t)space_map_object(sm), freebuf);
670 if (dump_opt['m'] > 2 && !dump_opt['L']) {
671 mutex_enter(&msp->ms_lock);
672 metaslab_load_wait(msp);
673 if (!msp->ms_loaded) {
674 VERIFY0(metaslab_load(msp));
675 range_tree_stat_verify(msp->ms_tree);
677 dump_metaslab_stats(msp);
678 metaslab_unload(msp);
679 mutex_exit(&msp->ms_lock);
682 if (dump_opt['m'] > 1 && sm != NULL &&
683 spa_feature_is_active(spa, SPA_FEATURE_SPACEMAP_HISTOGRAM)) {
685 * The space map histogram represents free space in chunks
686 * of sm_shift (i.e. bucket 0 refers to 2^sm_shift).
688 (void) printf("\tOn-disk histogram:\n");
689 dump_histogram(sm->sm_phys->smp_histogram,
690 SPACE_MAP_HISTOGRAM_SIZE(sm), sm->sm_shift);
693 if (dump_opt['d'] > 5 || dump_opt['m'] > 3) {
694 ASSERT(msp->ms_size == (1ULL << vd->vdev_ms_shift));
696 mutex_enter(&msp->ms_lock);
697 dump_spacemap(spa->spa_meta_objset, msp->ms_sm);
698 mutex_exit(&msp->ms_lock);
703 print_vdev_metaslab_header(vdev_t *vd)
705 (void) printf("\tvdev %10llu\n\t%-10s%5llu %-19s %-15s %-10s\n",
706 (u_longlong_t)vd->vdev_id,
707 "metaslabs", (u_longlong_t)vd->vdev_ms_count,
708 "offset", "spacemap", "free");
709 (void) printf("\t%15s %19s %15s %10s\n",
710 "---------------", "-------------------",
711 "---------------", "-------------");
715 dump_metaslabs(spa_t *spa)
717 vdev_t *vd, *rvd = spa->spa_root_vdev;
718 uint64_t m, c = 0, children = rvd->vdev_children;
720 (void) printf("\nMetaslabs:\n");
722 if (!dump_opt['d'] && zopt_objects > 0) {
726 (void) fatal("bad vdev id: %llu", (u_longlong_t)c);
728 if (zopt_objects > 1) {
729 vd = rvd->vdev_child[c];
730 print_vdev_metaslab_header(vd);
732 for (m = 1; m < zopt_objects; m++) {
733 if (zopt_object[m] < vd->vdev_ms_count)
735 vd->vdev_ms[zopt_object[m]]);
737 (void) fprintf(stderr, "bad metaslab "
739 (u_longlong_t)zopt_object[m]);
746 for (; c < children; c++) {
747 vd = rvd->vdev_child[c];
748 print_vdev_metaslab_header(vd);
750 for (m = 0; m < vd->vdev_ms_count; m++)
751 dump_metaslab(vd->vdev_ms[m]);
757 dump_dde(const ddt_t *ddt, const ddt_entry_t *dde, uint64_t index)
759 const ddt_phys_t *ddp = dde->dde_phys;
760 const ddt_key_t *ddk = &dde->dde_key;
761 char *types[4] = { "ditto", "single", "double", "triple" };
762 char blkbuf[BP_SPRINTF_LEN];
766 for (p = 0; p < DDT_PHYS_TYPES; p++, ddp++) {
767 if (ddp->ddp_phys_birth == 0)
769 ddt_bp_create(ddt->ddt_checksum, ddk, ddp, &blk);
770 snprintf_blkptr(blkbuf, sizeof (blkbuf), &blk);
771 (void) printf("index %llx refcnt %llu %s %s\n",
772 (u_longlong_t)index, (u_longlong_t)ddp->ddp_refcnt,
778 dump_dedup_ratio(const ddt_stat_t *dds)
780 double rL, rP, rD, D, dedup, compress, copies;
782 if (dds->dds_blocks == 0)
785 rL = (double)dds->dds_ref_lsize;
786 rP = (double)dds->dds_ref_psize;
787 rD = (double)dds->dds_ref_dsize;
788 D = (double)dds->dds_dsize;
794 (void) printf("dedup = %.2f, compress = %.2f, copies = %.2f, "
795 "dedup * compress / copies = %.2f\n\n",
796 dedup, compress, copies, dedup * compress / copies);
800 dump_ddt(ddt_t *ddt, enum ddt_type type, enum ddt_class class)
802 char name[DDT_NAMELEN];
805 dmu_object_info_t doi;
806 uint64_t count, dspace, mspace;
809 error = ddt_object_info(ddt, type, class, &doi);
815 error = ddt_object_count(ddt, type, class, &count);
820 dspace = doi.doi_physical_blocks_512 << 9;
821 mspace = doi.doi_fill_count * doi.doi_data_block_size;
823 ddt_object_name(ddt, type, class, name);
825 (void) printf("%s: %llu entries, size %llu on disk, %llu in core\n",
828 (u_longlong_t)(dspace / count),
829 (u_longlong_t)(mspace / count));
831 if (dump_opt['D'] < 3)
834 zpool_dump_ddt(NULL, &ddt->ddt_histogram[type][class]);
836 if (dump_opt['D'] < 4)
839 if (dump_opt['D'] < 5 && class == DDT_CLASS_UNIQUE)
842 (void) printf("%s contents:\n\n", name);
844 while ((error = ddt_object_walk(ddt, type, class, &walk, &dde)) == 0)
845 dump_dde(ddt, &dde, walk);
847 ASSERT(error == ENOENT);
853 dump_all_ddts(spa_t *spa)
855 ddt_histogram_t ddh_total;
856 ddt_stat_t dds_total;
859 enum ddt_class class;
861 bzero(&ddh_total, sizeof (ddt_histogram_t));
862 bzero(&dds_total, sizeof (ddt_stat_t));
864 for (c = 0; c < ZIO_CHECKSUM_FUNCTIONS; c++) {
865 ddt_t *ddt = spa->spa_ddt[c];
866 for (type = 0; type < DDT_TYPES; type++) {
867 for (class = 0; class < DDT_CLASSES;
869 dump_ddt(ddt, type, class);
874 ddt_get_dedup_stats(spa, &dds_total);
876 if (dds_total.dds_blocks == 0) {
877 (void) printf("All DDTs are empty\n");
883 if (dump_opt['D'] > 1) {
884 (void) printf("DDT histogram (aggregated over all DDTs):\n");
885 ddt_get_dedup_histogram(spa, &ddh_total);
886 zpool_dump_ddt(&dds_total, &ddh_total);
889 dump_dedup_ratio(&dds_total);
893 dump_dtl_seg(void *arg, uint64_t start, uint64_t size)
897 (void) printf("%s [%llu,%llu) length %llu\n",
900 (u_longlong_t)(start + size),
901 (u_longlong_t)(size));
905 dump_dtl(vdev_t *vd, int indent)
907 spa_t *spa = vd->vdev_spa;
909 char *name[DTL_TYPES] = { "missing", "partial", "scrub", "outage" };
913 spa_vdev_state_enter(spa, SCL_NONE);
914 required = vdev_dtl_required(vd);
915 (void) spa_vdev_state_exit(spa, NULL, 0);
918 (void) printf("\nDirty time logs:\n\n");
920 (void) printf("\t%*s%s [%s]\n", indent, "",
921 vd->vdev_path ? vd->vdev_path :
922 vd->vdev_parent ? vd->vdev_ops->vdev_op_type : spa_name(spa),
923 required ? "DTL-required" : "DTL-expendable");
925 for (t = 0; t < DTL_TYPES; t++) {
926 range_tree_t *rt = vd->vdev_dtl[t];
927 if (range_tree_space(rt) == 0)
929 (void) snprintf(prefix, sizeof (prefix), "\t%*s%s",
930 indent + 2, "", name[t]);
931 mutex_enter(rt->rt_lock);
932 range_tree_walk(rt, dump_dtl_seg, prefix);
933 mutex_exit(rt->rt_lock);
934 if (dump_opt['d'] > 5 && vd->vdev_children == 0)
935 dump_spacemap(spa->spa_meta_objset,
939 for (c = 0; c < vd->vdev_children; c++)
940 dump_dtl(vd->vdev_child[c], indent + 4);
944 dump_history(spa_t *spa)
946 nvlist_t **events = NULL;
947 char buf[SPA_MAXBLOCKSIZE];
948 uint64_t resid, len, off = 0;
954 char internalstr[MAXPATHLEN];
960 if ((error = spa_history_get(spa, &off, &len, buf)) != 0) {
961 (void) fprintf(stderr, "Unable to read history: "
962 "error %d\n", error);
966 if (zpool_history_unpack(buf, len, &resid, &events, &num) != 0)
972 (void) printf("\nHistory:\n");
973 for (i = 0; i < num; i++) {
974 uint64_t time, txg, ievent;
976 boolean_t printed = B_FALSE;
978 if (nvlist_lookup_uint64(events[i], ZPOOL_HIST_TIME,
981 if (nvlist_lookup_string(events[i], ZPOOL_HIST_CMD,
983 if (nvlist_lookup_uint64(events[i],
984 ZPOOL_HIST_INT_EVENT, &ievent) != 0)
986 verify(nvlist_lookup_uint64(events[i],
987 ZPOOL_HIST_TXG, &txg) == 0);
988 verify(nvlist_lookup_string(events[i],
989 ZPOOL_HIST_INT_STR, &intstr) == 0);
990 if (ievent >= ZFS_NUM_LEGACY_HISTORY_EVENTS)
993 (void) snprintf(internalstr,
994 sizeof (internalstr),
995 "[internal %s txg:%lld] %s",
996 zfs_history_event_names[ievent],
997 (longlong_t)txg, intstr);
1001 (void) localtime_r(&tsec, &t);
1002 (void) strftime(tbuf, sizeof (tbuf), "%F.%T", &t);
1003 (void) printf("%s %s\n", tbuf, cmd);
1007 if (dump_opt['h'] > 1) {
1009 (void) printf("unrecognized record:\n");
1010 dump_nvlist(events[i], 2);
1017 dump_dnode(objset_t *os, uint64_t object, void *data, size_t size)
1022 blkid2offset(const dnode_phys_t *dnp, const blkptr_t *bp, const zbookmark_t *zb)
1025 ASSERT(zb->zb_level < 0);
1026 if (zb->zb_object == 0)
1027 return (zb->zb_blkid);
1028 return (zb->zb_blkid * BP_GET_LSIZE(bp));
1031 ASSERT(zb->zb_level >= 0);
1033 return ((zb->zb_blkid <<
1034 (zb->zb_level * (dnp->dn_indblkshift - SPA_BLKPTRSHIFT))) *
1035 dnp->dn_datablkszsec << SPA_MINBLOCKSHIFT);
1039 snprintf_blkptr_compact(char *blkbuf, size_t buflen, const blkptr_t *bp)
1041 const dva_t *dva = bp->blk_dva;
1042 int ndvas = dump_opt['d'] > 5 ? BP_GET_NDVAS(bp) : 1;
1045 if (dump_opt['b'] >= 6) {
1046 snprintf_blkptr(blkbuf, buflen, bp);
1052 for (i = 0; i < ndvas; i++)
1053 (void) snprintf(blkbuf + strlen(blkbuf),
1054 buflen - strlen(blkbuf), "%llu:%llx:%llx ",
1055 (u_longlong_t)DVA_GET_VDEV(&dva[i]),
1056 (u_longlong_t)DVA_GET_OFFSET(&dva[i]),
1057 (u_longlong_t)DVA_GET_ASIZE(&dva[i]));
1059 if (BP_IS_HOLE(bp)) {
1060 (void) snprintf(blkbuf + strlen(blkbuf),
1061 buflen - strlen(blkbuf), "B=%llu",
1062 (u_longlong_t)bp->blk_birth);
1064 (void) snprintf(blkbuf + strlen(blkbuf),
1065 buflen - strlen(blkbuf),
1066 "%llxL/%llxP F=%llu B=%llu/%llu",
1067 (u_longlong_t)BP_GET_LSIZE(bp),
1068 (u_longlong_t)BP_GET_PSIZE(bp),
1069 (u_longlong_t)bp->blk_fill,
1070 (u_longlong_t)bp->blk_birth,
1071 (u_longlong_t)BP_PHYSICAL_BIRTH(bp));
1076 print_indirect(blkptr_t *bp, const zbookmark_t *zb,
1077 const dnode_phys_t *dnp)
1079 char blkbuf[BP_SPRINTF_LEN];
1082 ASSERT3U(BP_GET_TYPE(bp), ==, dnp->dn_type);
1083 ASSERT3U(BP_GET_LEVEL(bp), ==, zb->zb_level);
1085 (void) printf("%16llx ", (u_longlong_t)blkid2offset(dnp, bp, zb));
1087 ASSERT(zb->zb_level >= 0);
1089 for (l = dnp->dn_nlevels - 1; l >= -1; l--) {
1090 if (l == zb->zb_level) {
1091 (void) printf("L%llx", (u_longlong_t)zb->zb_level);
1097 snprintf_blkptr_compact(blkbuf, sizeof (blkbuf), bp);
1098 (void) printf("%s\n", blkbuf);
1102 visit_indirect(spa_t *spa, const dnode_phys_t *dnp,
1103 blkptr_t *bp, const zbookmark_t *zb)
1107 if (bp->blk_birth == 0)
1110 print_indirect(bp, zb, dnp);
1112 if (BP_GET_LEVEL(bp) > 0 && !BP_IS_HOLE(bp)) {
1113 uint32_t flags = ARC_WAIT;
1116 int epb = BP_GET_LSIZE(bp) >> SPA_BLKPTRSHIFT;
1120 err = arc_read(NULL, spa, bp, arc_getbuf_func, &buf,
1121 ZIO_PRIORITY_ASYNC_READ, ZIO_FLAG_CANFAIL, &flags, zb);
1124 ASSERT(buf->b_data);
1126 /* recursively visit blocks below this */
1128 for (i = 0; i < epb; i++, cbp++) {
1131 SET_BOOKMARK(&czb, zb->zb_objset, zb->zb_object,
1133 zb->zb_blkid * epb + i);
1134 err = visit_indirect(spa, dnp, cbp, &czb);
1137 fill += cbp->blk_fill;
1140 ASSERT3U(fill, ==, bp->blk_fill);
1141 (void) arc_buf_remove_ref(buf, &buf);
1149 dump_indirect(dnode_t *dn)
1151 dnode_phys_t *dnp = dn->dn_phys;
1155 (void) printf("Indirect blocks:\n");
1157 SET_BOOKMARK(&czb, dmu_objset_id(dn->dn_objset),
1158 dn->dn_object, dnp->dn_nlevels - 1, 0);
1159 for (j = 0; j < dnp->dn_nblkptr; j++) {
1161 (void) visit_indirect(dmu_objset_spa(dn->dn_objset), dnp,
1162 &dnp->dn_blkptr[j], &czb);
1165 (void) printf("\n");
1170 dump_dsl_dir(objset_t *os, uint64_t object, void *data, size_t size)
1172 dsl_dir_phys_t *dd = data;
1179 ASSERT3U(size, >=, sizeof (dsl_dir_phys_t));
1181 crtime = dd->dd_creation_time;
1182 (void) printf("\t\tcreation_time = %s", ctime(&crtime));
1183 (void) printf("\t\thead_dataset_obj = %llu\n",
1184 (u_longlong_t)dd->dd_head_dataset_obj);
1185 (void) printf("\t\tparent_dir_obj = %llu\n",
1186 (u_longlong_t)dd->dd_parent_obj);
1187 (void) printf("\t\torigin_obj = %llu\n",
1188 (u_longlong_t)dd->dd_origin_obj);
1189 (void) printf("\t\tchild_dir_zapobj = %llu\n",
1190 (u_longlong_t)dd->dd_child_dir_zapobj);
1191 zdb_nicenum(dd->dd_used_bytes, nice);
1192 (void) printf("\t\tused_bytes = %s\n", nice);
1193 zdb_nicenum(dd->dd_compressed_bytes, nice);
1194 (void) printf("\t\tcompressed_bytes = %s\n", nice);
1195 zdb_nicenum(dd->dd_uncompressed_bytes, nice);
1196 (void) printf("\t\tuncompressed_bytes = %s\n", nice);
1197 zdb_nicenum(dd->dd_quota, nice);
1198 (void) printf("\t\tquota = %s\n", nice);
1199 zdb_nicenum(dd->dd_reserved, nice);
1200 (void) printf("\t\treserved = %s\n", nice);
1201 (void) printf("\t\tprops_zapobj = %llu\n",
1202 (u_longlong_t)dd->dd_props_zapobj);
1203 (void) printf("\t\tdeleg_zapobj = %llu\n",
1204 (u_longlong_t)dd->dd_deleg_zapobj);
1205 (void) printf("\t\tflags = %llx\n",
1206 (u_longlong_t)dd->dd_flags);
1209 zdb_nicenum(dd->dd_used_breakdown[DD_USED_ ## which], nice); \
1210 (void) printf("\t\tused_breakdown[" #which "] = %s\n", nice)
1221 dump_dsl_dataset(objset_t *os, uint64_t object, void *data, size_t size)
1223 dsl_dataset_phys_t *ds = data;
1225 char used[32], compressed[32], uncompressed[32], unique[32];
1226 char blkbuf[BP_SPRINTF_LEN];
1231 ASSERT(size == sizeof (*ds));
1232 crtime = ds->ds_creation_time;
1233 zdb_nicenum(ds->ds_referenced_bytes, used);
1234 zdb_nicenum(ds->ds_compressed_bytes, compressed);
1235 zdb_nicenum(ds->ds_uncompressed_bytes, uncompressed);
1236 zdb_nicenum(ds->ds_unique_bytes, unique);
1237 snprintf_blkptr(blkbuf, sizeof (blkbuf), &ds->ds_bp);
1239 (void) printf("\t\tdir_obj = %llu\n",
1240 (u_longlong_t)ds->ds_dir_obj);
1241 (void) printf("\t\tprev_snap_obj = %llu\n",
1242 (u_longlong_t)ds->ds_prev_snap_obj);
1243 (void) printf("\t\tprev_snap_txg = %llu\n",
1244 (u_longlong_t)ds->ds_prev_snap_txg);
1245 (void) printf("\t\tnext_snap_obj = %llu\n",
1246 (u_longlong_t)ds->ds_next_snap_obj);
1247 (void) printf("\t\tsnapnames_zapobj = %llu\n",
1248 (u_longlong_t)ds->ds_snapnames_zapobj);
1249 (void) printf("\t\tnum_children = %llu\n",
1250 (u_longlong_t)ds->ds_num_children);
1251 (void) printf("\t\tuserrefs_obj = %llu\n",
1252 (u_longlong_t)ds->ds_userrefs_obj);
1253 (void) printf("\t\tcreation_time = %s", ctime(&crtime));
1254 (void) printf("\t\tcreation_txg = %llu\n",
1255 (u_longlong_t)ds->ds_creation_txg);
1256 (void) printf("\t\tdeadlist_obj = %llu\n",
1257 (u_longlong_t)ds->ds_deadlist_obj);
1258 (void) printf("\t\tused_bytes = %s\n", used);
1259 (void) printf("\t\tcompressed_bytes = %s\n", compressed);
1260 (void) printf("\t\tuncompressed_bytes = %s\n", uncompressed);
1261 (void) printf("\t\tunique = %s\n", unique);
1262 (void) printf("\t\tfsid_guid = %llu\n",
1263 (u_longlong_t)ds->ds_fsid_guid);
1264 (void) printf("\t\tguid = %llu\n",
1265 (u_longlong_t)ds->ds_guid);
1266 (void) printf("\t\tflags = %llx\n",
1267 (u_longlong_t)ds->ds_flags);
1268 (void) printf("\t\tnext_clones_obj = %llu\n",
1269 (u_longlong_t)ds->ds_next_clones_obj);
1270 (void) printf("\t\tprops_obj = %llu\n",
1271 (u_longlong_t)ds->ds_props_obj);
1272 (void) printf("\t\tbp = %s\n", blkbuf);
1277 dump_bptree_cb(void *arg, const blkptr_t *bp, dmu_tx_t *tx)
1279 char blkbuf[BP_SPRINTF_LEN];
1281 if (bp->blk_birth != 0) {
1282 snprintf_blkptr(blkbuf, sizeof (blkbuf), bp);
1283 (void) printf("\t%s\n", blkbuf);
1289 dump_bptree(objset_t *os, uint64_t obj, char *name)
1295 if (dump_opt['d'] < 3)
1298 VERIFY3U(0, ==, dmu_bonus_hold(os, obj, FTAG, &db));
1300 zdb_nicenum(bt->bt_bytes, bytes);
1301 (void) printf("\n %s: %llu datasets, %s\n",
1302 name, (unsigned long long)(bt->bt_end - bt->bt_begin), bytes);
1303 dmu_buf_rele(db, FTAG);
1305 if (dump_opt['d'] < 5)
1308 (void) printf("\n");
1310 (void) bptree_iterate(os, obj, B_FALSE, dump_bptree_cb, NULL, NULL);
1315 dump_bpobj_cb(void *arg, const blkptr_t *bp, dmu_tx_t *tx)
1317 char blkbuf[BP_SPRINTF_LEN];
1319 ASSERT(bp->blk_birth != 0);
1320 snprintf_blkptr_compact(blkbuf, sizeof (blkbuf), bp);
1321 (void) printf("\t%s\n", blkbuf);
1326 dump_bpobj(bpobj_t *bpo, char *name, int indent)
1333 if (dump_opt['d'] < 3)
1336 zdb_nicenum(bpo->bpo_phys->bpo_bytes, bytes);
1337 if (bpo->bpo_havesubobj && bpo->bpo_phys->bpo_subobjs != 0) {
1338 zdb_nicenum(bpo->bpo_phys->bpo_comp, comp);
1339 zdb_nicenum(bpo->bpo_phys->bpo_uncomp, uncomp);
1340 (void) printf(" %*s: object %llu, %llu local blkptrs, "
1341 "%llu subobjs, %s (%s/%s comp)\n",
1343 (u_longlong_t)bpo->bpo_object,
1344 (u_longlong_t)bpo->bpo_phys->bpo_num_blkptrs,
1345 (u_longlong_t)bpo->bpo_phys->bpo_num_subobjs,
1346 bytes, comp, uncomp);
1348 for (i = 0; i < bpo->bpo_phys->bpo_num_subobjs; i++) {
1352 VERIFY0(dmu_read(bpo->bpo_os,
1353 bpo->bpo_phys->bpo_subobjs,
1354 i * sizeof (subobj), sizeof (subobj), &subobj, 0));
1355 error = bpobj_open(&subbpo, bpo->bpo_os, subobj);
1357 (void) printf("ERROR %u while trying to open "
1359 error, (u_longlong_t)subobj);
1362 dump_bpobj(&subbpo, "subobj", indent + 1);
1365 (void) printf(" %*s: object %llu, %llu blkptrs, %s\n",
1367 (u_longlong_t)bpo->bpo_object,
1368 (u_longlong_t)bpo->bpo_phys->bpo_num_blkptrs,
1372 if (dump_opt['d'] < 5)
1377 (void) bpobj_iterate_nofree(bpo, dump_bpobj_cb, NULL, NULL);
1378 (void) printf("\n");
1383 dump_deadlist(dsl_deadlist_t *dl)
1385 dsl_deadlist_entry_t *dle;
1391 if (dump_opt['d'] < 3)
1394 zdb_nicenum(dl->dl_phys->dl_used, bytes);
1395 zdb_nicenum(dl->dl_phys->dl_comp, comp);
1396 zdb_nicenum(dl->dl_phys->dl_uncomp, uncomp);
1397 (void) printf("\n Deadlist: %s (%s/%s comp)\n",
1398 bytes, comp, uncomp);
1400 if (dump_opt['d'] < 4)
1403 (void) printf("\n");
1405 /* force the tree to be loaded */
1406 dsl_deadlist_space_range(dl, 0, UINT64_MAX, &unused, &unused, &unused);
1408 for (dle = avl_first(&dl->dl_tree); dle;
1409 dle = AVL_NEXT(&dl->dl_tree, dle)) {
1410 if (dump_opt['d'] >= 5) {
1412 (void) snprintf(buf, sizeof (buf),
1413 "mintxg %llu -> obj %llu",
1414 (longlong_t)dle->dle_mintxg,
1415 (longlong_t)dle->dle_bpobj.bpo_object);
1417 dump_bpobj(&dle->dle_bpobj, buf, 0);
1419 (void) printf("mintxg %llu -> obj %llu\n",
1420 (longlong_t)dle->dle_mintxg,
1421 (longlong_t)dle->dle_bpobj.bpo_object);
1427 static avl_tree_t idx_tree;
1428 static avl_tree_t domain_tree;
1429 static boolean_t fuid_table_loaded;
1430 static boolean_t sa_loaded;
1431 sa_attr_type_t *sa_attr_table;
1434 fuid_table_destroy(void)
1436 if (fuid_table_loaded) {
1437 zfs_fuid_table_destroy(&idx_tree, &domain_tree);
1438 fuid_table_loaded = B_FALSE;
1443 * print uid or gid information.
1444 * For normal POSIX id just the id is printed in decimal format.
1445 * For CIFS files with FUID the fuid is printed in hex followed by
1446 * the domain-rid string.
1449 print_idstr(uint64_t id, const char *id_type)
1451 if (FUID_INDEX(id)) {
1454 domain = zfs_fuid_idx_domain(&idx_tree, FUID_INDEX(id));
1455 (void) printf("\t%s %llx [%s-%d]\n", id_type,
1456 (u_longlong_t)id, domain, (int)FUID_RID(id));
1458 (void) printf("\t%s %llu\n", id_type, (u_longlong_t)id);
1464 dump_uidgid(objset_t *os, uint64_t uid, uint64_t gid)
1466 uint32_t uid_idx, gid_idx;
1468 uid_idx = FUID_INDEX(uid);
1469 gid_idx = FUID_INDEX(gid);
1471 /* Load domain table, if not already loaded */
1472 if (!fuid_table_loaded && (uid_idx || gid_idx)) {
1475 /* first find the fuid object. It lives in the master node */
1476 VERIFY(zap_lookup(os, MASTER_NODE_OBJ, ZFS_FUID_TABLES,
1477 8, 1, &fuid_obj) == 0);
1478 zfs_fuid_avl_tree_create(&idx_tree, &domain_tree);
1479 (void) zfs_fuid_table_load(os, fuid_obj,
1480 &idx_tree, &domain_tree);
1481 fuid_table_loaded = B_TRUE;
1484 print_idstr(uid, "uid");
1485 print_idstr(gid, "gid");
1489 dump_znode_sa_xattr(sa_handle_t *hdl)
1492 nvpair_t *elem = NULL;
1493 int sa_xattr_size = 0;
1494 int sa_xattr_entries = 0;
1496 char *sa_xattr_packed;
1498 error = sa_size(hdl, sa_attr_table[ZPL_DXATTR], &sa_xattr_size);
1499 if (error || sa_xattr_size == 0)
1502 sa_xattr_packed = malloc(sa_xattr_size);
1503 if (sa_xattr_packed == NULL)
1506 error = sa_lookup(hdl, sa_attr_table[ZPL_DXATTR],
1507 sa_xattr_packed, sa_xattr_size);
1509 free(sa_xattr_packed);
1513 error = nvlist_unpack(sa_xattr_packed, sa_xattr_size, &sa_xattr, 0);
1515 free(sa_xattr_packed);
1519 while ((elem = nvlist_next_nvpair(sa_xattr, elem)) != NULL)
1522 (void) printf("\tSA xattrs: %d bytes, %d entries\n\n",
1523 sa_xattr_size, sa_xattr_entries);
1524 while ((elem = nvlist_next_nvpair(sa_xattr, elem)) != NULL) {
1528 (void) printf("\t\t%s = ", nvpair_name(elem));
1529 nvpair_value_byte_array(elem, &value, &cnt);
1530 for (idx = 0; idx < cnt; ++idx) {
1531 if (isprint(value[idx]))
1532 (void) putchar(value[idx]);
1534 (void) printf("\\%3.3o", value[idx]);
1536 (void) putchar('\n');
1539 nvlist_free(sa_xattr);
1540 free(sa_xattr_packed);
1545 dump_znode(objset_t *os, uint64_t object, void *data, size_t size)
1547 char path[MAXPATHLEN * 2]; /* allow for xattr and failure prefix */
1549 uint64_t xattr, rdev, gen;
1550 uint64_t uid, gid, mode, fsize, parent, links;
1552 uint64_t acctm[2], modtm[2], chgtm[2], crtm[2];
1553 time_t z_crtime, z_atime, z_mtime, z_ctime;
1554 sa_bulk_attr_t bulk[12];
1559 uint64_t sa_attrs = 0;
1562 VERIFY(zap_lookup(os, MASTER_NODE_OBJ, ZPL_VERSION_STR,
1563 8, 1, &version) == 0);
1564 if (version >= ZPL_VERSION_SA) {
1565 VERIFY(zap_lookup(os, MASTER_NODE_OBJ, ZFS_SA_ATTRS,
1566 8, 1, &sa_attrs) == 0);
1568 if ((error = sa_setup(os, sa_attrs, zfs_attr_table,
1569 ZPL_END, &sa_attr_table)) != 0) {
1570 (void) printf("sa_setup failed errno %d, can't "
1571 "display znode contents\n", error);
1577 if (sa_handle_get(os, object, NULL, SA_HDL_PRIVATE, &hdl)) {
1578 (void) printf("Failed to get handle for SA znode\n");
1582 SA_ADD_BULK_ATTR(bulk, idx, sa_attr_table[ZPL_UID], NULL, &uid, 8);
1583 SA_ADD_BULK_ATTR(bulk, idx, sa_attr_table[ZPL_GID], NULL, &gid, 8);
1584 SA_ADD_BULK_ATTR(bulk, idx, sa_attr_table[ZPL_LINKS], NULL,
1586 SA_ADD_BULK_ATTR(bulk, idx, sa_attr_table[ZPL_GEN], NULL, &gen, 8);
1587 SA_ADD_BULK_ATTR(bulk, idx, sa_attr_table[ZPL_MODE], NULL,
1589 SA_ADD_BULK_ATTR(bulk, idx, sa_attr_table[ZPL_PARENT],
1591 SA_ADD_BULK_ATTR(bulk, idx, sa_attr_table[ZPL_SIZE], NULL,
1593 SA_ADD_BULK_ATTR(bulk, idx, sa_attr_table[ZPL_ATIME], NULL,
1595 SA_ADD_BULK_ATTR(bulk, idx, sa_attr_table[ZPL_MTIME], NULL,
1597 SA_ADD_BULK_ATTR(bulk, idx, sa_attr_table[ZPL_CRTIME], NULL,
1599 SA_ADD_BULK_ATTR(bulk, idx, sa_attr_table[ZPL_CTIME], NULL,
1601 SA_ADD_BULK_ATTR(bulk, idx, sa_attr_table[ZPL_FLAGS], NULL,
1604 if (sa_bulk_lookup(hdl, bulk, idx)) {
1605 (void) sa_handle_destroy(hdl);
1609 error = zfs_obj_to_path(os, object, path, sizeof (path));
1611 (void) snprintf(path, sizeof (path), "\?\?\?<object#%llu>",
1612 (u_longlong_t)object);
1614 if (dump_opt['d'] < 3) {
1615 (void) printf("\t%s\n", path);
1616 (void) sa_handle_destroy(hdl);
1620 z_crtime = (time_t)crtm[0];
1621 z_atime = (time_t)acctm[0];
1622 z_mtime = (time_t)modtm[0];
1623 z_ctime = (time_t)chgtm[0];
1625 (void) printf("\tpath %s\n", path);
1626 dump_uidgid(os, uid, gid);
1627 (void) printf("\tatime %s", ctime(&z_atime));
1628 (void) printf("\tmtime %s", ctime(&z_mtime));
1629 (void) printf("\tctime %s", ctime(&z_ctime));
1630 (void) printf("\tcrtime %s", ctime(&z_crtime));
1631 (void) printf("\tgen %llu\n", (u_longlong_t)gen);
1632 (void) printf("\tmode %llo\n", (u_longlong_t)mode);
1633 (void) printf("\tsize %llu\n", (u_longlong_t)fsize);
1634 (void) printf("\tparent %llu\n", (u_longlong_t)parent);
1635 (void) printf("\tlinks %llu\n", (u_longlong_t)links);
1636 (void) printf("\tpflags %llx\n", (u_longlong_t)pflags);
1637 if (sa_lookup(hdl, sa_attr_table[ZPL_XATTR], &xattr,
1638 sizeof (uint64_t)) == 0)
1639 (void) printf("\txattr %llu\n", (u_longlong_t)xattr);
1640 if (sa_lookup(hdl, sa_attr_table[ZPL_RDEV], &rdev,
1641 sizeof (uint64_t)) == 0)
1642 (void) printf("\trdev 0x%016llx\n", (u_longlong_t)rdev);
1643 dump_znode_sa_xattr(hdl);
1644 sa_handle_destroy(hdl);
1649 dump_acl(objset_t *os, uint64_t object, void *data, size_t size)
1655 dump_dmu_objset(objset_t *os, uint64_t object, void *data, size_t size)
1659 static object_viewer_t *object_viewer[DMU_OT_NUMTYPES + 1] = {
1660 dump_none, /* unallocated */
1661 dump_zap, /* object directory */
1662 dump_uint64, /* object array */
1663 dump_none, /* packed nvlist */
1664 dump_packed_nvlist, /* packed nvlist size */
1665 dump_none, /* bplist */
1666 dump_none, /* bplist header */
1667 dump_none, /* SPA space map header */
1668 dump_none, /* SPA space map */
1669 dump_none, /* ZIL intent log */
1670 dump_dnode, /* DMU dnode */
1671 dump_dmu_objset, /* DMU objset */
1672 dump_dsl_dir, /* DSL directory */
1673 dump_zap, /* DSL directory child map */
1674 dump_zap, /* DSL dataset snap map */
1675 dump_zap, /* DSL props */
1676 dump_dsl_dataset, /* DSL dataset */
1677 dump_znode, /* ZFS znode */
1678 dump_acl, /* ZFS V0 ACL */
1679 dump_uint8, /* ZFS plain file */
1680 dump_zpldir, /* ZFS directory */
1681 dump_zap, /* ZFS master node */
1682 dump_zap, /* ZFS delete queue */
1683 dump_uint8, /* zvol object */
1684 dump_zap, /* zvol prop */
1685 dump_uint8, /* other uint8[] */
1686 dump_uint64, /* other uint64[] */
1687 dump_zap, /* other ZAP */
1688 dump_zap, /* persistent error log */
1689 dump_uint8, /* SPA history */
1690 dump_history_offsets, /* SPA history offsets */
1691 dump_zap, /* Pool properties */
1692 dump_zap, /* DSL permissions */
1693 dump_acl, /* ZFS ACL */
1694 dump_uint8, /* ZFS SYSACL */
1695 dump_none, /* FUID nvlist */
1696 dump_packed_nvlist, /* FUID nvlist size */
1697 dump_zap, /* DSL dataset next clones */
1698 dump_zap, /* DSL scrub queue */
1699 dump_zap, /* ZFS user/group used */
1700 dump_zap, /* ZFS user/group quota */
1701 dump_zap, /* snapshot refcount tags */
1702 dump_ddt_zap, /* DDT ZAP object */
1703 dump_zap, /* DDT statistics */
1704 dump_znode, /* SA object */
1705 dump_zap, /* SA Master Node */
1706 dump_sa_attrs, /* SA attribute registration */
1707 dump_sa_layouts, /* SA attribute layouts */
1708 dump_zap, /* DSL scrub translations */
1709 dump_none, /* fake dedup BP */
1710 dump_zap, /* deadlist */
1711 dump_none, /* deadlist hdr */
1712 dump_zap, /* dsl clones */
1713 dump_none, /* bpobj subobjs */
1714 dump_unknown, /* Unknown type, must be last */
1718 dump_object(objset_t *os, uint64_t object, int verbosity, int *print_header)
1720 dmu_buf_t *db = NULL;
1721 dmu_object_info_t doi;
1725 char iblk[32], dblk[32], lsize[32], asize[32], fill[32];
1726 char bonus_size[32];
1730 if (*print_header) {
1731 (void) printf("\n%10s %3s %5s %5s %5s %5s %6s %s\n",
1732 "Object", "lvl", "iblk", "dblk", "dsize", "lsize",
1738 dn = DMU_META_DNODE(os);
1740 error = dmu_bonus_hold(os, object, FTAG, &db);
1742 fatal("dmu_bonus_hold(%llu) failed, errno %u",
1744 bonus = db->db_data;
1745 bsize = db->db_size;
1746 dn = DB_DNODE((dmu_buf_impl_t *)db);
1748 dmu_object_info_from_dnode(dn, &doi);
1750 zdb_nicenum(doi.doi_metadata_block_size, iblk);
1751 zdb_nicenum(doi.doi_data_block_size, dblk);
1752 zdb_nicenum(doi.doi_max_offset, lsize);
1753 zdb_nicenum(doi.doi_physical_blocks_512 << 9, asize);
1754 zdb_nicenum(doi.doi_bonus_size, bonus_size);
1755 (void) sprintf(fill, "%6.2f", 100.0 * doi.doi_fill_count *
1756 doi.doi_data_block_size / (object == 0 ? DNODES_PER_BLOCK : 1) /
1757 doi.doi_max_offset);
1761 if (doi.doi_checksum != ZIO_CHECKSUM_INHERIT || verbosity >= 6) {
1762 (void) snprintf(aux + strlen(aux), sizeof (aux), " (K=%s)",
1763 ZDB_CHECKSUM_NAME(doi.doi_checksum));
1766 if (doi.doi_compress != ZIO_COMPRESS_INHERIT || verbosity >= 6) {
1767 (void) snprintf(aux + strlen(aux), sizeof (aux), " (Z=%s)",
1768 ZDB_COMPRESS_NAME(doi.doi_compress));
1771 (void) printf("%10lld %3u %5s %5s %5s %5s %6s %s%s\n",
1772 (u_longlong_t)object, doi.doi_indirection, iblk, dblk,
1773 asize, lsize, fill, ZDB_OT_NAME(doi.doi_type), aux);
1775 if (doi.doi_bonus_type != DMU_OT_NONE && verbosity > 3) {
1776 (void) printf("%10s %3s %5s %5s %5s %5s %6s %s\n",
1777 "", "", "", "", "", bonus_size, "bonus",
1778 ZDB_OT_NAME(doi.doi_bonus_type));
1781 if (verbosity >= 4) {
1782 (void) printf("\tdnode flags: %s%s%s\n",
1783 (dn->dn_phys->dn_flags & DNODE_FLAG_USED_BYTES) ?
1785 (dn->dn_phys->dn_flags & DNODE_FLAG_USERUSED_ACCOUNTED) ?
1786 "USERUSED_ACCOUNTED " : "",
1787 (dn->dn_phys->dn_flags & DNODE_FLAG_SPILL_BLKPTR) ?
1788 "SPILL_BLKPTR" : "");
1789 (void) printf("\tdnode maxblkid: %llu\n",
1790 (longlong_t)dn->dn_phys->dn_maxblkid);
1792 object_viewer[ZDB_OT_TYPE(doi.doi_bonus_type)](os, object,
1794 object_viewer[ZDB_OT_TYPE(doi.doi_type)](os, object, NULL, 0);
1801 if (verbosity >= 5) {
1803 * Report the list of segments that comprise the object.
1807 uint64_t blkfill = 1;
1810 if (dn->dn_type == DMU_OT_DNODE) {
1812 blkfill = DNODES_PER_BLOCK;
1817 error = dnode_next_offset(dn,
1818 0, &start, minlvl, blkfill, 0);
1822 error = dnode_next_offset(dn,
1823 DNODE_FIND_HOLE, &end, minlvl, blkfill, 0);
1824 zdb_nicenum(end - start, segsize);
1825 (void) printf("\t\tsegment [%016llx, %016llx)"
1826 " size %5s\n", (u_longlong_t)start,
1827 (u_longlong_t)end, segsize);
1835 dmu_buf_rele(db, FTAG);
1838 static char *objset_types[DMU_OST_NUMTYPES] = {
1839 "NONE", "META", "ZPL", "ZVOL", "OTHER", "ANY" };
1842 dump_dir(objset_t *os)
1844 dmu_objset_stats_t dds;
1845 uint64_t object, object_count;
1846 uint64_t refdbytes, usedobjs, scratch;
1848 char blkbuf[BP_SPRINTF_LEN + 20];
1849 char osname[MAXNAMELEN];
1850 char *type = "UNKNOWN";
1851 int verbosity = dump_opt['d'];
1852 int print_header = 1;
1855 dsl_pool_config_enter(dmu_objset_pool(os), FTAG);
1856 dmu_objset_fast_stat(os, &dds);
1857 dsl_pool_config_exit(dmu_objset_pool(os), FTAG);
1859 if (dds.dds_type < DMU_OST_NUMTYPES)
1860 type = objset_types[dds.dds_type];
1862 if (dds.dds_type == DMU_OST_META) {
1863 dds.dds_creation_txg = TXG_INITIAL;
1864 usedobjs = os->os_rootbp->blk_fill;
1865 refdbytes = os->os_spa->spa_dsl_pool->
1866 dp_mos_dir->dd_phys->dd_used_bytes;
1868 dmu_objset_space(os, &refdbytes, &scratch, &usedobjs, &scratch);
1871 ASSERT3U(usedobjs, ==, os->os_rootbp->blk_fill);
1873 zdb_nicenum(refdbytes, numbuf);
1875 if (verbosity >= 4) {
1876 (void) snprintf(blkbuf, sizeof (blkbuf), ", rootbp ");
1877 (void) snprintf_blkptr(blkbuf + strlen(blkbuf),
1878 sizeof (blkbuf) - strlen(blkbuf), os->os_rootbp);
1883 dmu_objset_name(os, osname);
1885 (void) printf("Dataset %s [%s], ID %llu, cr_txg %llu, "
1886 "%s, %llu objects%s\n",
1887 osname, type, (u_longlong_t)dmu_objset_id(os),
1888 (u_longlong_t)dds.dds_creation_txg,
1889 numbuf, (u_longlong_t)usedobjs, blkbuf);
1891 if (zopt_objects != 0) {
1892 for (i = 0; i < zopt_objects; i++)
1893 dump_object(os, zopt_object[i], verbosity,
1895 (void) printf("\n");
1899 if (dump_opt['i'] != 0 || verbosity >= 2)
1900 dump_intent_log(dmu_objset_zil(os));
1902 if (dmu_objset_ds(os) != NULL)
1903 dump_deadlist(&dmu_objset_ds(os)->ds_deadlist);
1908 if (BP_IS_HOLE(os->os_rootbp))
1911 dump_object(os, 0, verbosity, &print_header);
1913 if (DMU_USERUSED_DNODE(os) != NULL &&
1914 DMU_USERUSED_DNODE(os)->dn_type != 0) {
1915 dump_object(os, DMU_USERUSED_OBJECT, verbosity, &print_header);
1916 dump_object(os, DMU_GROUPUSED_OBJECT, verbosity, &print_header);
1920 while ((error = dmu_object_next(os, &object, B_FALSE, 0)) == 0) {
1921 dump_object(os, object, verbosity, &print_header);
1925 ASSERT3U(object_count, ==, usedobjs);
1927 (void) printf("\n");
1929 if (error != ESRCH) {
1930 (void) fprintf(stderr, "dmu_object_next() = %d\n", error);
1936 dump_uberblock(uberblock_t *ub, const char *header, const char *footer)
1938 time_t timestamp = ub->ub_timestamp;
1940 (void) printf("%s", header ? header : "");
1941 (void) printf("\tmagic = %016llx\n", (u_longlong_t)ub->ub_magic);
1942 (void) printf("\tversion = %llu\n", (u_longlong_t)ub->ub_version);
1943 (void) printf("\ttxg = %llu\n", (u_longlong_t)ub->ub_txg);
1944 (void) printf("\tguid_sum = %llu\n", (u_longlong_t)ub->ub_guid_sum);
1945 (void) printf("\ttimestamp = %llu UTC = %s",
1946 (u_longlong_t)ub->ub_timestamp, asctime(localtime(×tamp)));
1947 if (dump_opt['u'] >= 3) {
1948 char blkbuf[BP_SPRINTF_LEN];
1949 snprintf_blkptr(blkbuf, sizeof (blkbuf), &ub->ub_rootbp);
1950 (void) printf("\trootbp = %s\n", blkbuf);
1952 (void) printf("%s", footer ? footer : "");
1956 dump_config(spa_t *spa)
1963 error = dmu_bonus_hold(spa->spa_meta_objset,
1964 spa->spa_config_object, FTAG, &db);
1967 nvsize = *(uint64_t *)db->db_data;
1968 dmu_buf_rele(db, FTAG);
1970 (void) printf("\nMOS Configuration:\n");
1971 dump_packed_nvlist(spa->spa_meta_objset,
1972 spa->spa_config_object, (void *)&nvsize, 1);
1974 (void) fprintf(stderr, "dmu_bonus_hold(%llu) failed, errno %d",
1975 (u_longlong_t)spa->spa_config_object, error);
1980 dump_cachefile(const char *cachefile)
1983 struct stat64 statbuf;
1987 if ((fd = open64(cachefile, O_RDONLY)) < 0) {
1988 (void) printf("cannot open '%s': %s\n", cachefile,
1993 if (fstat64(fd, &statbuf) != 0) {
1994 (void) printf("failed to stat '%s': %s\n", cachefile,
1999 if ((buf = malloc(statbuf.st_size)) == NULL) {
2000 (void) fprintf(stderr, "failed to allocate %llu bytes\n",
2001 (u_longlong_t)statbuf.st_size);
2005 if (read(fd, buf, statbuf.st_size) != statbuf.st_size) {
2006 (void) fprintf(stderr, "failed to read %llu bytes\n",
2007 (u_longlong_t)statbuf.st_size);
2013 if (nvlist_unpack(buf, statbuf.st_size, &config, 0) != 0) {
2014 (void) fprintf(stderr, "failed to unpack nvlist\n");
2020 dump_nvlist(config, 0);
2022 nvlist_free(config);
2025 #define ZDB_MAX_UB_HEADER_SIZE 32
2028 dump_label_uberblocks(vdev_label_t *lbl, uint64_t ashift)
2032 char header[ZDB_MAX_UB_HEADER_SIZE];
2035 vd.vdev_ashift = ashift;
2036 vdp->vdev_top = vdp;
2038 for (i = 0; i < VDEV_UBERBLOCK_COUNT(vdp); i++) {
2039 uint64_t uoff = VDEV_UBERBLOCK_OFFSET(vdp, i);
2040 uberblock_t *ub = (void *)((char *)lbl + uoff);
2042 if (uberblock_verify(ub))
2044 (void) snprintf(header, ZDB_MAX_UB_HEADER_SIZE,
2045 "Uberblock[%d]\n", i);
2046 dump_uberblock(ub, header, "");
2051 dump_label(const char *dev)
2055 char *path, *buf = label.vl_vdev_phys.vp_nvlist;
2056 size_t buflen = sizeof (label.vl_vdev_phys.vp_nvlist);
2057 struct stat64 statbuf;
2058 uint64_t psize, ashift;
2059 int len = strlen(dev) + 1;
2062 if (strncmp(dev, "/dev/dsk/", 9) == 0) {
2065 (void) snprintf(path, len, "%s%s", "/dev/rdsk/", dev + 9);
2070 if ((fd = open64(path, O_RDONLY)) < 0) {
2071 (void) printf("cannot open '%s': %s\n", path, strerror(errno));
2076 if (fstat64_blk(fd, &statbuf) != 0) {
2077 (void) printf("failed to stat '%s': %s\n", path,
2084 psize = statbuf.st_size;
2085 psize = P2ALIGN(psize, (uint64_t)sizeof (vdev_label_t));
2087 for (l = 0; l < VDEV_LABELS; l++) {
2088 nvlist_t *config = NULL;
2090 (void) printf("--------------------------------------------\n");
2091 (void) printf("LABEL %d\n", l);
2092 (void) printf("--------------------------------------------\n");
2094 if (pread64(fd, &label, sizeof (label),
2095 vdev_label_offset(psize, l, 0)) != sizeof (label)) {
2096 (void) printf("failed to read label %d\n", l);
2100 if (nvlist_unpack(buf, buflen, &config, 0) != 0) {
2101 (void) printf("failed to unpack label %d\n", l);
2102 ashift = SPA_MINBLOCKSHIFT;
2104 nvlist_t *vdev_tree = NULL;
2106 dump_nvlist(config, 4);
2107 if ((nvlist_lookup_nvlist(config,
2108 ZPOOL_CONFIG_VDEV_TREE, &vdev_tree) != 0) ||
2109 (nvlist_lookup_uint64(vdev_tree,
2110 ZPOOL_CONFIG_ASHIFT, &ashift) != 0))
2111 ashift = SPA_MINBLOCKSHIFT;
2112 nvlist_free(config);
2115 dump_label_uberblocks(&label, ashift);
2124 dump_one_dir(const char *dsname, void *arg)
2129 error = dmu_objset_own(dsname, DMU_OST_ANY, B_TRUE, FTAG, &os);
2131 (void) printf("Could not open %s, error %d\n", dsname, error);
2135 dmu_objset_disown(os, FTAG);
2136 fuid_table_destroy();
2137 sa_loaded = B_FALSE;
2144 #define PSIZE_HISTO_SIZE (SPA_MAXBLOCKSIZE / SPA_MINBLOCKSIZE + 1)
2145 typedef struct zdb_blkstats {
2150 uint64_t zb_psize_histogram[PSIZE_HISTO_SIZE];
2154 * Extended object types to report deferred frees and dedup auto-ditto blocks.
2156 #define ZDB_OT_DEFERRED (DMU_OT_NUMTYPES + 0)
2157 #define ZDB_OT_DITTO (DMU_OT_NUMTYPES + 1)
2158 #define ZDB_OT_OTHER (DMU_OT_NUMTYPES + 2)
2159 #define ZDB_OT_TOTAL (DMU_OT_NUMTYPES + 3)
2161 static char *zdb_ot_extname[] = {
2168 #define ZB_TOTAL DN_MAX_LEVELS
2170 typedef struct zdb_cb {
2171 zdb_blkstats_t zcb_type[ZB_TOTAL + 1][ZDB_OT_TOTAL + 1];
2172 uint64_t zcb_dedup_asize;
2173 uint64_t zcb_dedup_blocks;
2175 uint64_t zcb_lastprint;
2176 uint64_t zcb_totalasize;
2177 uint64_t zcb_errors[256];
2184 zdb_count_block(zdb_cb_t *zcb, zilog_t *zilog, const blkptr_t *bp,
2185 dmu_object_type_t type)
2187 uint64_t refcnt = 0;
2190 ASSERT(type < ZDB_OT_TOTAL);
2192 if (zilog && zil_bp_tree_add(zilog, bp) != 0)
2195 for (i = 0; i < 4; i++) {
2196 int l = (i < 2) ? BP_GET_LEVEL(bp) : ZB_TOTAL;
2197 int t = (i & 1) ? type : ZDB_OT_TOTAL;
2198 zdb_blkstats_t *zb = &zcb->zcb_type[l][t];
2200 zb->zb_asize += BP_GET_ASIZE(bp);
2201 zb->zb_lsize += BP_GET_LSIZE(bp);
2202 zb->zb_psize += BP_GET_PSIZE(bp);
2204 zb->zb_psize_histogram[BP_GET_PSIZE(bp) >> SPA_MINBLOCKSHIFT]++;
2210 if (BP_GET_DEDUP(bp)) {
2214 ddt = ddt_select(zcb->zcb_spa, bp);
2216 dde = ddt_lookup(ddt, bp, B_FALSE);
2221 ddt_phys_t *ddp = ddt_phys_select(dde, bp);
2222 ddt_phys_decref(ddp);
2223 refcnt = ddp->ddp_refcnt;
2224 if (ddt_phys_total_refcnt(dde) == 0)
2225 ddt_remove(ddt, dde);
2230 VERIFY3U(zio_wait(zio_claim(NULL, zcb->zcb_spa,
2231 refcnt ? 0 : spa_first_txg(zcb->zcb_spa),
2232 bp, NULL, NULL, ZIO_FLAG_CANFAIL)), ==, 0);
2236 zdb_blkptr_done(zio_t *zio)
2238 spa_t *spa = zio->io_spa;
2239 blkptr_t *bp = zio->io_bp;
2240 int ioerr = zio->io_error;
2241 zdb_cb_t *zcb = zio->io_private;
2242 zbookmark_t *zb = &zio->io_bookmark;
2244 zio_data_buf_free(zio->io_data, zio->io_size);
2246 mutex_enter(&spa->spa_scrub_lock);
2247 spa->spa_scrub_inflight--;
2248 cv_broadcast(&spa->spa_scrub_io_cv);
2250 if (ioerr && !(zio->io_flags & ZIO_FLAG_SPECULATIVE)) {
2251 char blkbuf[BP_SPRINTF_LEN];
2253 zcb->zcb_haderrors = 1;
2254 zcb->zcb_errors[ioerr]++;
2256 if (dump_opt['b'] >= 2)
2257 snprintf_blkptr(blkbuf, sizeof (blkbuf), bp);
2261 (void) printf("zdb_blkptr_cb: "
2262 "Got error %d reading "
2263 "<%llu, %llu, %lld, %llx> %s -- skipping\n",
2265 (u_longlong_t)zb->zb_objset,
2266 (u_longlong_t)zb->zb_object,
2267 (u_longlong_t)zb->zb_level,
2268 (u_longlong_t)zb->zb_blkid,
2271 mutex_exit(&spa->spa_scrub_lock);
2275 zdb_blkptr_cb(spa_t *spa, zilog_t *zilog, const blkptr_t *bp,
2276 const zbookmark_t *zb, const dnode_phys_t *dnp, void *arg)
2278 zdb_cb_t *zcb = arg;
2279 dmu_object_type_t type;
2280 boolean_t is_metadata;
2282 if (dump_opt['b'] >= 5 && bp->blk_birth > 0) {
2283 char blkbuf[BP_SPRINTF_LEN];
2284 snprintf_blkptr(blkbuf, sizeof (blkbuf), bp);
2285 (void) printf("objset %llu object %llu "
2286 "level %lld offset 0x%llx %s\n",
2287 (u_longlong_t)zb->zb_objset,
2288 (u_longlong_t)zb->zb_object,
2289 (longlong_t)zb->zb_level,
2290 (u_longlong_t)blkid2offset(dnp, bp, zb),
2297 type = BP_GET_TYPE(bp);
2299 zdb_count_block(zcb, zilog, bp,
2300 (type & DMU_OT_NEWTYPE) ? ZDB_OT_OTHER : type);
2302 is_metadata = (BP_GET_LEVEL(bp) != 0 || DMU_OT_IS_METADATA(type));
2304 if (dump_opt['c'] > 1 || (dump_opt['c'] && is_metadata)) {
2305 size_t size = BP_GET_PSIZE(bp);
2306 void *data = zio_data_buf_alloc(size);
2307 int flags = ZIO_FLAG_CANFAIL | ZIO_FLAG_SCRUB | ZIO_FLAG_RAW;
2309 /* If it's an intent log block, failure is expected. */
2310 if (zb->zb_level == ZB_ZIL_LEVEL)
2311 flags |= ZIO_FLAG_SPECULATIVE;
2313 mutex_enter(&spa->spa_scrub_lock);
2314 while (spa->spa_scrub_inflight > max_inflight)
2315 cv_wait(&spa->spa_scrub_io_cv, &spa->spa_scrub_lock);
2316 spa->spa_scrub_inflight++;
2317 mutex_exit(&spa->spa_scrub_lock);
2319 zio_nowait(zio_read(NULL, spa, bp, data, size,
2320 zdb_blkptr_done, zcb, ZIO_PRIORITY_ASYNC_READ, flags, zb));
2323 zcb->zcb_readfails = 0;
2325 if (dump_opt['b'] < 5 && isatty(STDERR_FILENO) &&
2326 gethrtime() > zcb->zcb_lastprint + NANOSEC) {
2327 uint64_t now = gethrtime();
2329 uint64_t bytes = zcb->zcb_type[ZB_TOTAL][ZDB_OT_TOTAL].zb_asize;
2331 1 + bytes / (1 + ((now - zcb->zcb_start) / 1000 / 1000));
2333 (zcb->zcb_totalasize - bytes) / 1024 / kb_per_sec;
2335 zfs_nicenum(bytes, buf, sizeof (buf));
2336 (void) fprintf(stderr,
2337 "\r%5s completed (%4dMB/s) "
2338 "estimated time remaining: %uhr %02umin %02usec ",
2339 buf, kb_per_sec / 1024,
2340 sec_remaining / 60 / 60,
2341 sec_remaining / 60 % 60,
2342 sec_remaining % 60);
2344 zcb->zcb_lastprint = now;
2351 zdb_leak(void *arg, uint64_t start, uint64_t size)
2355 (void) printf("leaked space: vdev %llu, offset 0x%llx, size %llu\n",
2356 (u_longlong_t)vd->vdev_id, (u_longlong_t)start, (u_longlong_t)size);
2359 static metaslab_ops_t zdb_metaslab_ops = {
2361 NULL /* fragmented */
2365 zdb_ddt_leak_init(spa_t *spa, zdb_cb_t *zcb)
2367 ddt_bookmark_t ddb = { 0 };
2372 while ((error = ddt_walk(spa, &ddb, &dde)) == 0) {
2374 ddt_phys_t *ddp = dde.dde_phys;
2376 if (ddb.ddb_class == DDT_CLASS_UNIQUE)
2379 ASSERT(ddt_phys_total_refcnt(&dde) > 1);
2381 for (p = 0; p < DDT_PHYS_TYPES; p++, ddp++) {
2382 if (ddp->ddp_phys_birth == 0)
2384 ddt_bp_create(ddb.ddb_checksum,
2385 &dde.dde_key, ddp, &blk);
2386 if (p == DDT_PHYS_DITTO) {
2387 zdb_count_block(zcb, NULL, &blk, ZDB_OT_DITTO);
2389 zcb->zcb_dedup_asize +=
2390 BP_GET_ASIZE(&blk) * (ddp->ddp_refcnt - 1);
2391 zcb->zcb_dedup_blocks++;
2394 if (!dump_opt['L']) {
2395 ddt_t *ddt = spa->spa_ddt[ddb.ddb_checksum];
2397 VERIFY(ddt_lookup(ddt, &blk, B_TRUE) != NULL);
2402 ASSERT(error == ENOENT);
2406 zdb_leak_init(spa_t *spa, zdb_cb_t *zcb)
2411 if (!dump_opt['L']) {
2412 vdev_t *rvd = spa->spa_root_vdev;
2413 for (c = 0; c < rvd->vdev_children; c++) {
2414 vdev_t *vd = rvd->vdev_child[c];
2415 for (m = 0; m < vd->vdev_ms_count; m++) {
2416 metaslab_t *msp = vd->vdev_ms[m];
2417 mutex_enter(&msp->ms_lock);
2418 metaslab_unload(msp);
2421 * For leak detection, we overload the metaslab
2422 * ms_tree to contain allocated segments
2423 * instead of free segments. As a result,
2424 * we can't use the normal metaslab_load/unload
2427 if (msp->ms_sm != NULL) {
2428 msp->ms_ops = &zdb_metaslab_ops;
2429 VERIFY0(space_map_load(msp->ms_sm,
2430 msp->ms_tree, SM_ALLOC));
2431 msp->ms_loaded = B_TRUE;
2433 mutex_exit(&msp->ms_lock);
2438 spa_config_enter(spa, SCL_CONFIG, FTAG, RW_READER);
2440 zdb_ddt_leak_init(spa, zcb);
2442 spa_config_exit(spa, SCL_CONFIG, FTAG);
2446 zdb_leak_fini(spa_t *spa)
2450 if (!dump_opt['L']) {
2451 vdev_t *rvd = spa->spa_root_vdev;
2452 for (c = 0; c < rvd->vdev_children; c++) {
2453 vdev_t *vd = rvd->vdev_child[c];
2454 for (m = 0; m < vd->vdev_ms_count; m++) {
2455 metaslab_t *msp = vd->vdev_ms[m];
2456 mutex_enter(&msp->ms_lock);
2459 * The ms_tree has been overloaded to
2460 * contain allocated segments. Now that we
2461 * finished traversing all blocks, any
2462 * block that remains in the ms_tree
2463 * represents an allocated block that we
2464 * did not claim during the traversal.
2465 * Claimed blocks would have been removed
2468 range_tree_vacate(msp->ms_tree, zdb_leak, vd);
2469 msp->ms_loaded = B_FALSE;
2471 mutex_exit(&msp->ms_lock);
2479 count_block_cb(void *arg, const blkptr_t *bp, dmu_tx_t *tx)
2481 zdb_cb_t *zcb = arg;
2483 if (dump_opt['b'] >= 5) {
2484 char blkbuf[BP_SPRINTF_LEN];
2485 snprintf_blkptr(blkbuf, sizeof (blkbuf), bp);
2486 (void) printf("[%s] %s\n",
2487 "deferred free", blkbuf);
2489 zdb_count_block(zcb, NULL, bp, ZDB_OT_DEFERRED);
2494 dump_block_stats(spa_t *spa)
2497 zdb_blkstats_t *zb, *tzb;
2498 uint64_t norm_alloc, norm_space, total_alloc, total_found;
2499 int flags = TRAVERSE_PRE | TRAVERSE_PREFETCH_METADATA | TRAVERSE_HARD;
2503 (void) printf("\nTraversing all blocks %s%s%s%s%s...\n\n",
2504 (dump_opt['c'] || !dump_opt['L']) ? "to verify " : "",
2505 (dump_opt['c'] == 1) ? "metadata " : "",
2506 dump_opt['c'] ? "checksums " : "",
2507 (dump_opt['c'] && !dump_opt['L']) ? "and verify " : "",
2508 !dump_opt['L'] ? "nothing leaked " : "");
2511 * Load all space maps as SM_ALLOC maps, then traverse the pool
2512 * claiming each block we discover. If the pool is perfectly
2513 * consistent, the space maps will be empty when we're done.
2514 * Anything left over is a leak; any block we can't claim (because
2515 * it's not part of any space map) is a double allocation,
2516 * reference to a freed block, or an unclaimed log block.
2518 bzero(&zcb, sizeof (zdb_cb_t));
2519 zdb_leak_init(spa, &zcb);
2522 * If there's a deferred-free bplist, process that first.
2524 (void) bpobj_iterate_nofree(&spa->spa_deferred_bpobj,
2525 count_block_cb, &zcb, NULL);
2526 if (spa_version(spa) >= SPA_VERSION_DEADLISTS) {
2527 (void) bpobj_iterate_nofree(&spa->spa_dsl_pool->dp_free_bpobj,
2528 count_block_cb, &zcb, NULL);
2530 if (spa_feature_is_active(spa, SPA_FEATURE_ASYNC_DESTROY)) {
2531 VERIFY3U(0, ==, bptree_iterate(spa->spa_meta_objset,
2532 spa->spa_dsl_pool->dp_bptree_obj, B_FALSE, count_block_cb,
2536 if (dump_opt['c'] > 1)
2537 flags |= TRAVERSE_PREFETCH_DATA;
2539 zcb.zcb_totalasize = metaslab_class_get_alloc(spa_normal_class(spa));
2540 zcb.zcb_start = zcb.zcb_lastprint = gethrtime();
2541 zcb.zcb_haderrors |= traverse_pool(spa, 0, flags, zdb_blkptr_cb, &zcb);
2544 * If we've traversed the data blocks then we need to wait for those
2545 * I/Os to complete. We leverage "The Godfather" zio to wait on
2546 * all async I/Os to complete.
2548 if (dump_opt['c']) {
2549 (void) zio_wait(spa->spa_async_zio_root);
2550 spa->spa_async_zio_root = zio_root(spa, NULL, NULL,
2551 ZIO_FLAG_CANFAIL | ZIO_FLAG_SPECULATIVE |
2552 ZIO_FLAG_GODFATHER);
2555 if (zcb.zcb_haderrors) {
2556 (void) printf("\nError counts:\n\n");
2557 (void) printf("\t%5s %s\n", "errno", "count");
2558 for (e = 0; e < 256; e++) {
2559 if (zcb.zcb_errors[e] != 0) {
2560 (void) printf("\t%5d %llu\n",
2561 e, (u_longlong_t)zcb.zcb_errors[e]);
2567 * Report any leaked segments.
2571 tzb = &zcb.zcb_type[ZB_TOTAL][ZDB_OT_TOTAL];
2573 norm_alloc = metaslab_class_get_alloc(spa_normal_class(spa));
2574 norm_space = metaslab_class_get_space(spa_normal_class(spa));
2576 total_alloc = norm_alloc + metaslab_class_get_alloc(spa_log_class(spa));
2577 total_found = tzb->zb_asize - zcb.zcb_dedup_asize;
2579 if (total_found == total_alloc) {
2581 (void) printf("\n\tNo leaks (block sum matches space"
2582 " maps exactly)\n");
2584 (void) printf("block traversal size %llu != alloc %llu "
2586 (u_longlong_t)total_found,
2587 (u_longlong_t)total_alloc,
2588 (dump_opt['L']) ? "unreachable" : "leaked",
2589 (longlong_t)(total_alloc - total_found));
2593 if (tzb->zb_count == 0)
2596 (void) printf("\n");
2597 (void) printf("\tbp count: %10llu\n",
2598 (u_longlong_t)tzb->zb_count);
2599 (void) printf("\tbp logical: %10llu avg: %6llu\n",
2600 (u_longlong_t)tzb->zb_lsize,
2601 (u_longlong_t)(tzb->zb_lsize / tzb->zb_count));
2602 (void) printf("\tbp physical: %10llu avg:"
2603 " %6llu compression: %6.2f\n",
2604 (u_longlong_t)tzb->zb_psize,
2605 (u_longlong_t)(tzb->zb_psize / tzb->zb_count),
2606 (double)tzb->zb_lsize / tzb->zb_psize);
2607 (void) printf("\tbp allocated: %10llu avg:"
2608 " %6llu compression: %6.2f\n",
2609 (u_longlong_t)tzb->zb_asize,
2610 (u_longlong_t)(tzb->zb_asize / tzb->zb_count),
2611 (double)tzb->zb_lsize / tzb->zb_asize);
2612 (void) printf("\tbp deduped: %10llu ref>1:"
2613 " %6llu deduplication: %6.2f\n",
2614 (u_longlong_t)zcb.zcb_dedup_asize,
2615 (u_longlong_t)zcb.zcb_dedup_blocks,
2616 (double)zcb.zcb_dedup_asize / tzb->zb_asize + 1.0);
2617 (void) printf("\tSPA allocated: %10llu used: %5.2f%%\n",
2618 (u_longlong_t)norm_alloc, 100.0 * norm_alloc / norm_space);
2620 if (dump_opt['b'] >= 2) {
2622 (void) printf("\nBlocks\tLSIZE\tPSIZE\tASIZE"
2623 "\t avg\t comp\t%%Total\tType\n");
2625 for (t = 0; t <= ZDB_OT_TOTAL; t++) {
2626 char csize[32], lsize[32], psize[32], asize[32];
2630 if (t < DMU_OT_NUMTYPES)
2631 typename = dmu_ot[t].ot_name;
2633 typename = zdb_ot_extname[t - DMU_OT_NUMTYPES];
2635 if (zcb.zcb_type[ZB_TOTAL][t].zb_asize == 0) {
2636 (void) printf("%6s\t%5s\t%5s\t%5s"
2637 "\t%5s\t%5s\t%6s\t%s\n",
2649 for (l = ZB_TOTAL - 1; l >= -1; l--) {
2650 level = (l == -1 ? ZB_TOTAL : l);
2651 zb = &zcb.zcb_type[level][t];
2653 if (zb->zb_asize == 0)
2656 if (dump_opt['b'] < 3 && level != ZB_TOTAL)
2659 if (level == 0 && zb->zb_asize ==
2660 zcb.zcb_type[ZB_TOTAL][t].zb_asize)
2663 zdb_nicenum(zb->zb_count, csize);
2664 zdb_nicenum(zb->zb_lsize, lsize);
2665 zdb_nicenum(zb->zb_psize, psize);
2666 zdb_nicenum(zb->zb_asize, asize);
2667 zdb_nicenum(zb->zb_asize / zb->zb_count, avg);
2669 (void) printf("%6s\t%5s\t%5s\t%5s\t%5s"
2671 csize, lsize, psize, asize, avg,
2672 (double)zb->zb_lsize / zb->zb_psize,
2673 100.0 * zb->zb_asize / tzb->zb_asize);
2675 if (level == ZB_TOTAL)
2676 (void) printf("%s\n", typename);
2678 (void) printf(" L%d %s\n",
2681 if (dump_opt['b'] >= 4) {
2682 (void) printf("psize "
2683 "(in 512-byte sectors): "
2684 "number of blocks\n");
2685 dump_histogram(zb->zb_psize_histogram,
2686 PSIZE_HISTO_SIZE, 0);
2692 (void) printf("\n");
2697 if (zcb.zcb_haderrors)
2703 typedef struct zdb_ddt_entry {
2705 uint64_t zdde_ref_blocks;
2706 uint64_t zdde_ref_lsize;
2707 uint64_t zdde_ref_psize;
2708 uint64_t zdde_ref_dsize;
2709 avl_node_t zdde_node;
2714 zdb_ddt_add_cb(spa_t *spa, zilog_t *zilog, const blkptr_t *bp,
2715 const zbookmark_t *zb, const dnode_phys_t *dnp, void *arg)
2717 avl_tree_t *t = arg;
2719 zdb_ddt_entry_t *zdde, zdde_search;
2724 if (dump_opt['S'] > 1 && zb->zb_level == ZB_ROOT_LEVEL) {
2725 (void) printf("traversing objset %llu, %llu objects, "
2726 "%lu blocks so far\n",
2727 (u_longlong_t)zb->zb_objset,
2728 (u_longlong_t)bp->blk_fill,
2732 if (BP_IS_HOLE(bp) || BP_GET_CHECKSUM(bp) == ZIO_CHECKSUM_OFF ||
2733 BP_GET_LEVEL(bp) > 0 || DMU_OT_IS_METADATA(BP_GET_TYPE(bp)))
2736 ddt_key_fill(&zdde_search.zdde_key, bp);
2738 zdde = avl_find(t, &zdde_search, &where);
2741 zdde = umem_zalloc(sizeof (*zdde), UMEM_NOFAIL);
2742 zdde->zdde_key = zdde_search.zdde_key;
2743 avl_insert(t, zdde, where);
2746 zdde->zdde_ref_blocks += 1;
2747 zdde->zdde_ref_lsize += BP_GET_LSIZE(bp);
2748 zdde->zdde_ref_psize += BP_GET_PSIZE(bp);
2749 zdde->zdde_ref_dsize += bp_get_dsize_sync(spa, bp);
2755 dump_simulated_ddt(spa_t *spa)
2758 void *cookie = NULL;
2759 zdb_ddt_entry_t *zdde;
2760 ddt_histogram_t ddh_total;
2761 ddt_stat_t dds_total;
2763 bzero(&ddh_total, sizeof (ddt_histogram_t));
2764 bzero(&dds_total, sizeof (ddt_stat_t));
2766 avl_create(&t, ddt_entry_compare,
2767 sizeof (zdb_ddt_entry_t), offsetof(zdb_ddt_entry_t, zdde_node));
2769 spa_config_enter(spa, SCL_CONFIG, FTAG, RW_READER);
2771 (void) traverse_pool(spa, 0, TRAVERSE_PRE | TRAVERSE_PREFETCH_METADATA,
2772 zdb_ddt_add_cb, &t);
2774 spa_config_exit(spa, SCL_CONFIG, FTAG);
2776 while ((zdde = avl_destroy_nodes(&t, &cookie)) != NULL) {
2778 uint64_t refcnt = zdde->zdde_ref_blocks;
2779 ASSERT(refcnt != 0);
2781 dds.dds_blocks = zdde->zdde_ref_blocks / refcnt;
2782 dds.dds_lsize = zdde->zdde_ref_lsize / refcnt;
2783 dds.dds_psize = zdde->zdde_ref_psize / refcnt;
2784 dds.dds_dsize = zdde->zdde_ref_dsize / refcnt;
2786 dds.dds_ref_blocks = zdde->zdde_ref_blocks;
2787 dds.dds_ref_lsize = zdde->zdde_ref_lsize;
2788 dds.dds_ref_psize = zdde->zdde_ref_psize;
2789 dds.dds_ref_dsize = zdde->zdde_ref_dsize;
2791 ddt_stat_add(&ddh_total.ddh_stat[highbit(refcnt) - 1], &dds, 0);
2793 umem_free(zdde, sizeof (*zdde));
2798 ddt_histogram_stat(&dds_total, &ddh_total);
2800 (void) printf("Simulated DDT histogram:\n");
2802 zpool_dump_ddt(&dds_total, &ddh_total);
2804 dump_dedup_ratio(&dds_total);
2808 dump_zpool(spa_t *spa)
2810 dsl_pool_t *dp = spa_get_dsl(spa);
2813 if (dump_opt['S']) {
2814 dump_simulated_ddt(spa);
2818 if (!dump_opt['e'] && dump_opt['C'] > 1) {
2819 (void) printf("\nCached configuration:\n");
2820 dump_nvlist(spa->spa_config, 8);
2827 dump_uberblock(&spa->spa_uberblock, "\nUberblock:\n", "\n");
2832 if (dump_opt['d'] > 2 || dump_opt['m'])
2833 dump_metaslabs(spa);
2835 if (dump_opt['d'] || dump_opt['i']) {
2836 dump_dir(dp->dp_meta_objset);
2837 if (dump_opt['d'] >= 3) {
2838 dump_bpobj(&spa->spa_deferred_bpobj,
2839 "Deferred frees", 0);
2840 if (spa_version(spa) >= SPA_VERSION_DEADLISTS) {
2841 dump_bpobj(&spa->spa_dsl_pool->dp_free_bpobj,
2842 "Pool snapshot frees", 0);
2845 if (spa_feature_is_active(spa,
2846 SPA_FEATURE_ASYNC_DESTROY)) {
2847 dump_bptree(spa->spa_meta_objset,
2848 spa->spa_dsl_pool->dp_bptree_obj,
2849 "Pool dataset frees");
2851 dump_dtl(spa->spa_root_vdev, 0);
2853 (void) dmu_objset_find(spa_name(spa), dump_one_dir,
2854 NULL, DS_FIND_SNAPSHOTS | DS_FIND_CHILDREN);
2856 if (dump_opt['b'] || dump_opt['c'])
2857 rc = dump_block_stats(spa);
2860 rc = verify_spacemap_refcounts(spa);
2863 show_pool_stats(spa);
2872 #define ZDB_FLAG_CHECKSUM 0x0001
2873 #define ZDB_FLAG_DECOMPRESS 0x0002
2874 #define ZDB_FLAG_BSWAP 0x0004
2875 #define ZDB_FLAG_GBH 0x0008
2876 #define ZDB_FLAG_INDIRECT 0x0010
2877 #define ZDB_FLAG_PHYS 0x0020
2878 #define ZDB_FLAG_RAW 0x0040
2879 #define ZDB_FLAG_PRINT_BLKPTR 0x0080
2884 zdb_print_blkptr(blkptr_t *bp, int flags)
2886 char blkbuf[BP_SPRINTF_LEN];
2888 if (flags & ZDB_FLAG_BSWAP)
2889 byteswap_uint64_array((void *)bp, sizeof (blkptr_t));
2891 snprintf_blkptr(blkbuf, sizeof (blkbuf), bp);
2892 (void) printf("%s\n", blkbuf);
2896 zdb_dump_indirect(blkptr_t *bp, int nbps, int flags)
2900 for (i = 0; i < nbps; i++)
2901 zdb_print_blkptr(&bp[i], flags);
2905 zdb_dump_gbh(void *buf, int flags)
2907 zdb_dump_indirect((blkptr_t *)buf, SPA_GBH_NBLKPTRS, flags);
2911 zdb_dump_block_raw(void *buf, uint64_t size, int flags)
2913 if (flags & ZDB_FLAG_BSWAP)
2914 byteswap_uint64_array(buf, size);
2915 VERIFY(write(fileno(stdout), buf, size) == size);
2919 zdb_dump_block(char *label, void *buf, uint64_t size, int flags)
2921 uint64_t *d = (uint64_t *)buf;
2922 int nwords = size / sizeof (uint64_t);
2923 int do_bswap = !!(flags & ZDB_FLAG_BSWAP);
2929 hdr = " 7 6 5 4 3 2 1 0 f e d c b a 9 8";
2931 hdr = " 0 1 2 3 4 5 6 7 8 9 a b c d e f";
2933 (void) printf("\n%s\n%6s %s 0123456789abcdef\n", label, "", hdr);
2935 for (i = 0; i < nwords; i += 2) {
2936 (void) printf("%06llx: %016llx %016llx ",
2937 (u_longlong_t)(i * sizeof (uint64_t)),
2938 (u_longlong_t)(do_bswap ? BSWAP_64(d[i]) : d[i]),
2939 (u_longlong_t)(do_bswap ? BSWAP_64(d[i + 1]) : d[i + 1]));
2942 for (j = 0; j < 2 * sizeof (uint64_t); j++)
2943 (void) printf("%c", isprint(c[j]) ? c[j] : '.');
2944 (void) printf("\n");
2949 * There are two acceptable formats:
2950 * leaf_name - For example: c1t0d0 or /tmp/ztest.0a
2951 * child[.child]* - For example: 0.1.1
2953 * The second form can be used to specify arbitrary vdevs anywhere
2954 * in the heirarchy. For example, in a pool with a mirror of
2955 * RAID-Zs, you can specify either RAID-Z vdev with 0.0 or 0.1 .
2958 zdb_vdev_lookup(vdev_t *vdev, char *path)
2966 /* First, assume the x.x.x.x format */
2967 i = (int)strtoul(path, &s, 10);
2968 if (s == path || (s && *s != '.' && *s != '\0'))
2970 if (i < 0 || i >= vdev->vdev_children)
2973 vdev = vdev->vdev_child[i];
2976 return (zdb_vdev_lookup(vdev, s+1));
2979 for (i = 0; i < vdev->vdev_children; i++) {
2980 vdev_t *vc = vdev->vdev_child[i];
2982 if (vc->vdev_path == NULL) {
2983 vc = zdb_vdev_lookup(vc, path);
2990 p = strrchr(vc->vdev_path, '/');
2991 p = p ? p + 1 : vc->vdev_path;
2992 q = &vc->vdev_path[strlen(vc->vdev_path) - 2];
2994 if (strcmp(vc->vdev_path, path) == 0)
2996 if (strcmp(p, path) == 0)
2998 if (strcmp(q, "s0") == 0 && strncmp(p, path, q - p) == 0)
3006 * Read a block from a pool and print it out. The syntax of the
3007 * block descriptor is:
3009 * pool:vdev_specifier:offset:size[:flags]
3011 * pool - The name of the pool you wish to read from
3012 * vdev_specifier - Which vdev (see comment for zdb_vdev_lookup)
3013 * offset - offset, in hex, in bytes
3014 * size - Amount of data to read, in hex, in bytes
3015 * flags - A string of characters specifying options
3016 * b: Decode a blkptr at given offset within block
3017 * *c: Calculate and display checksums
3018 * d: Decompress data before dumping
3019 * e: Byteswap data before dumping
3020 * g: Display data as a gang block header
3021 * i: Display as an indirect block
3022 * p: Do I/O to physical offset
3023 * r: Dump raw data to stdout
3025 * * = not yet implemented
3028 zdb_read_block(char *thing, spa_t *spa)
3030 blkptr_t blk, *bp = &blk;
3031 dva_t *dva = bp->blk_dva;
3033 uint64_t offset = 0, size = 0, psize = 0, lsize = 0, blkptr_offset = 0;
3036 void *pbuf, *lbuf, *buf;
3037 char *s, *p, *dup, *vdev, *flagstr;
3040 dup = strdup(thing);
3041 s = strtok(dup, ":");
3043 s = strtok(NULL, ":");
3044 offset = strtoull(s ? s : "", NULL, 16);
3045 s = strtok(NULL, ":");
3046 size = strtoull(s ? s : "", NULL, 16);
3047 s = strtok(NULL, ":");
3048 flagstr = s ? s : "";
3052 s = "size must not be zero";
3053 if (!IS_P2ALIGNED(size, DEV_BSIZE))
3054 s = "size must be a multiple of sector size";
3055 if (!IS_P2ALIGNED(offset, DEV_BSIZE))
3056 s = "offset must be a multiple of sector size";
3058 (void) printf("Invalid block specifier: %s - %s\n", thing, s);
3063 for (s = strtok(flagstr, ":"); s; s = strtok(NULL, ":")) {
3064 for (i = 0; flagstr[i]; i++) {
3065 int bit = flagbits[(uchar_t)flagstr[i]];
3068 (void) printf("***Invalid flag: %c\n",
3074 /* If it's not something with an argument, keep going */
3075 if ((bit & (ZDB_FLAG_CHECKSUM |
3076 ZDB_FLAG_PRINT_BLKPTR)) == 0)
3079 p = &flagstr[i + 1];
3080 if (bit == ZDB_FLAG_PRINT_BLKPTR)
3081 blkptr_offset = strtoull(p, &p, 16);
3082 if (*p != ':' && *p != '\0') {
3083 (void) printf("***Invalid flag arg: '%s'\n", s);
3090 vd = zdb_vdev_lookup(spa->spa_root_vdev, vdev);
3092 (void) printf("***Invalid vdev: %s\n", vdev);
3097 (void) fprintf(stderr, "Found vdev: %s\n",
3100 (void) fprintf(stderr, "Found vdev type: %s\n",
3101 vd->vdev_ops->vdev_op_type);
3107 pbuf = umem_alloc_aligned(SPA_MAXBLOCKSIZE, 512, UMEM_NOFAIL);
3108 lbuf = umem_alloc(SPA_MAXBLOCKSIZE, UMEM_NOFAIL);
3112 DVA_SET_VDEV(&dva[0], vd->vdev_id);
3113 DVA_SET_OFFSET(&dva[0], offset);
3114 DVA_SET_GANG(&dva[0], !!(flags & ZDB_FLAG_GBH));
3115 DVA_SET_ASIZE(&dva[0], vdev_psize_to_asize(vd, psize));
3117 BP_SET_BIRTH(bp, TXG_INITIAL, TXG_INITIAL);
3119 BP_SET_LSIZE(bp, lsize);
3120 BP_SET_PSIZE(bp, psize);
3121 BP_SET_COMPRESS(bp, ZIO_COMPRESS_OFF);
3122 BP_SET_CHECKSUM(bp, ZIO_CHECKSUM_OFF);
3123 BP_SET_TYPE(bp, DMU_OT_NONE);
3124 BP_SET_LEVEL(bp, 0);
3125 BP_SET_DEDUP(bp, 0);
3126 BP_SET_BYTEORDER(bp, ZFS_HOST_BYTEORDER);
3128 spa_config_enter(spa, SCL_STATE, FTAG, RW_READER);
3129 zio = zio_root(spa, NULL, NULL, 0);
3131 if (vd == vd->vdev_top) {
3133 * Treat this as a normal block read.
3135 zio_nowait(zio_read(zio, spa, bp, pbuf, psize, NULL, NULL,
3136 ZIO_PRIORITY_SYNC_READ,
3137 ZIO_FLAG_CANFAIL | ZIO_FLAG_RAW, NULL));
3140 * Treat this as a vdev child I/O.
3142 zio_nowait(zio_vdev_child_io(zio, bp, vd, offset, pbuf, psize,
3143 ZIO_TYPE_READ, ZIO_PRIORITY_SYNC_READ,
3144 ZIO_FLAG_DONT_CACHE | ZIO_FLAG_DONT_QUEUE |
3145 ZIO_FLAG_DONT_PROPAGATE | ZIO_FLAG_DONT_RETRY |
3146 ZIO_FLAG_CANFAIL | ZIO_FLAG_RAW, NULL, NULL));
3149 error = zio_wait(zio);
3150 spa_config_exit(spa, SCL_STATE, FTAG);
3153 (void) printf("Read of %s failed, error: %d\n", thing, error);
3157 if (flags & ZDB_FLAG_DECOMPRESS) {
3159 * We don't know how the data was compressed, so just try
3160 * every decompress function at every inflated blocksize.
3162 enum zio_compress c;
3163 void *pbuf2 = umem_alloc(SPA_MAXBLOCKSIZE, UMEM_NOFAIL);
3164 void *lbuf2 = umem_alloc(SPA_MAXBLOCKSIZE, UMEM_NOFAIL);
3166 bcopy(pbuf, pbuf2, psize);
3168 VERIFY(random_get_pseudo_bytes((uint8_t *)pbuf + psize,
3169 SPA_MAXBLOCKSIZE - psize) == 0);
3171 VERIFY(random_get_pseudo_bytes((uint8_t *)pbuf2 + psize,
3172 SPA_MAXBLOCKSIZE - psize) == 0);
3174 for (lsize = SPA_MAXBLOCKSIZE; lsize > psize;
3175 lsize -= SPA_MINBLOCKSIZE) {
3176 for (c = 0; c < ZIO_COMPRESS_FUNCTIONS; c++) {
3177 if (zio_decompress_data(c, pbuf, lbuf,
3178 psize, lsize) == 0 &&
3179 zio_decompress_data(c, pbuf2, lbuf2,
3180 psize, lsize) == 0 &&
3181 bcmp(lbuf, lbuf2, lsize) == 0)
3184 if (c != ZIO_COMPRESS_FUNCTIONS)
3186 lsize -= SPA_MINBLOCKSIZE;
3189 umem_free(pbuf2, SPA_MAXBLOCKSIZE);
3190 umem_free(lbuf2, SPA_MAXBLOCKSIZE);
3192 if (lsize <= psize) {
3193 (void) printf("Decompress of %s failed\n", thing);
3203 if (flags & ZDB_FLAG_PRINT_BLKPTR)
3204 zdb_print_blkptr((blkptr_t *)(void *)
3205 ((uintptr_t)buf + (uintptr_t)blkptr_offset), flags);
3206 else if (flags & ZDB_FLAG_RAW)
3207 zdb_dump_block_raw(buf, size, flags);
3208 else if (flags & ZDB_FLAG_INDIRECT)
3209 zdb_dump_indirect((blkptr_t *)buf, size / sizeof (blkptr_t),
3211 else if (flags & ZDB_FLAG_GBH)
3212 zdb_dump_gbh(buf, flags);
3214 zdb_dump_block(thing, buf, size, flags);
3217 umem_free(pbuf, SPA_MAXBLOCKSIZE);
3218 umem_free(lbuf, SPA_MAXBLOCKSIZE);
3223 pool_match(nvlist_t *cfg, char *tgt)
3225 uint64_t v, guid = strtoull(tgt, NULL, 0);
3229 if (nvlist_lookup_uint64(cfg, ZPOOL_CONFIG_POOL_GUID, &v) == 0)
3232 if (nvlist_lookup_string(cfg, ZPOOL_CONFIG_POOL_NAME, &s) == 0)
3233 return (strcmp(s, tgt) == 0);
3239 find_zpool(char **target, nvlist_t **configp, int dirc, char **dirv)
3242 nvlist_t *match = NULL;
3247 importargs_t args = { 0 };
3251 args.can_be_active = B_TRUE;
3253 if ((sepp = strpbrk(*target, "/@")) != NULL) {
3258 pools = zpool_search_import(g_zfs, &args);
3260 if (pools != NULL) {
3261 nvpair_t *elem = NULL;
3262 while ((elem = nvlist_next_nvpair(pools, elem)) != NULL) {
3263 verify(nvpair_value_nvlist(elem, configp) == 0);
3264 if (pool_match(*configp, *target)) {
3266 if (match != NULL) {
3267 /* print previously found config */
3269 (void) printf("%s\n", name);
3270 dump_nvlist(match, 8);
3273 (void) printf("%s\n",
3275 dump_nvlist(*configp, 8);
3278 name = nvpair_name(elem);
3284 (void) fatal("\tMatched %d pools - use pool GUID "
3285 "instead of pool name or \n"
3286 "\tpool name part of a dataset name to select pool", count);
3291 * If pool GUID was specified for pool id, replace it with pool name
3293 if (name && (strstr(*target, name) != *target)) {
3294 int sz = 1 + strlen(name) + ((sepp) ? strlen(sepp) : 0);
3296 *target = umem_alloc(sz, UMEM_NOFAIL);
3297 (void) snprintf(*target, sz, "%s%s", name, sepp ? sepp : "");
3300 *configp = name ? match : NULL;
3306 main(int argc, char **argv)
3309 struct rlimit rl = { 1024, 1024 };
3311 objset_t *os = NULL;
3315 char **searchdirs = NULL;
3318 nvlist_t *policy = NULL;
3319 uint64_t max_txg = UINT64_MAX;
3320 int flags = ZFS_IMPORT_MISSING_LOG;
3321 int rewind = ZPOOL_NEVER_REWIND;
3322 char *spa_config_path_env;
3323 const char *opts = "bcdhilmM:suCDRSAFLVXevp:t:U:P";
3325 (void) setrlimit(RLIMIT_NOFILE, &rl);
3326 (void) enable_extended_FILE_stdio(-1, -1);
3328 dprintf_setup(&argc, argv);
3331 * If there is an environment variable SPA_CONFIG_PATH it overrides
3332 * default spa_config_path setting. If -U flag is specified it will
3333 * override this environment variable settings once again.
3335 spa_config_path_env = getenv("SPA_CONFIG_PATH");
3336 if (spa_config_path_env != NULL)
3337 spa_config_path = spa_config_path_env;
3339 while ((c = getopt(argc, argv, opts)) != -1) {
3366 flags = ZFS_IMPORT_VERBATIM;
3372 max_inflight = strtoull(optarg, NULL, 0);
3373 if (max_inflight == 0) {
3374 (void) fprintf(stderr, "maximum number "
3375 "of inflight I/Os must be greater "
3381 if (searchdirs == NULL) {
3382 searchdirs = umem_alloc(sizeof (char *),
3385 char **tmp = umem_alloc((nsearch + 1) *
3386 sizeof (char *), UMEM_NOFAIL);
3387 bcopy(searchdirs, tmp, nsearch *
3389 umem_free(searchdirs,
3390 nsearch * sizeof (char *));
3393 searchdirs[nsearch++] = optarg;
3396 max_txg = strtoull(optarg, NULL, 0);
3397 if (max_txg < TXG_INITIAL) {
3398 (void) fprintf(stderr, "incorrect txg "
3399 "specified: %s\n", optarg);
3404 spa_config_path = optarg;
3412 if (!dump_opt['e'] && searchdirs != NULL) {
3413 (void) fprintf(stderr, "-p option requires use of -e\n");
3418 if ((g_zfs = libzfs_init()) == NULL)
3422 verbose = MAX(verbose, 1);
3424 for (c = 0; c < 256; c++) {
3425 if (dump_all && !strchr("elAFLRSXP", c))
3428 dump_opt[c] += verbose;
3431 aok = (dump_opt['A'] == 1) || (dump_opt['A'] > 2);
3432 zfs_recover = (dump_opt['A'] > 1);
3437 if (argc < 2 && dump_opt['R'])
3440 if (!dump_opt['e'] && dump_opt['C']) {
3441 dump_cachefile(spa_config_path);
3447 if (dump_opt['l']) {
3448 dump_label(argv[0]);
3452 if (dump_opt['X'] || dump_opt['F'])
3453 rewind = ZPOOL_DO_REWIND |
3454 (dump_opt['X'] ? ZPOOL_EXTREME_REWIND : 0);
3456 if (nvlist_alloc(&policy, NV_UNIQUE_NAME_TYPE, 0) != 0 ||
3457 nvlist_add_uint64(policy, ZPOOL_REWIND_REQUEST_TXG, max_txg) != 0 ||
3458 nvlist_add_uint32(policy, ZPOOL_REWIND_REQUEST, rewind) != 0)
3459 fatal("internal error: %s", strerror(ENOMEM));
3464 if (dump_opt['e']) {
3465 nvlist_t *cfg = NULL;
3466 char *name = find_zpool(&target, &cfg, nsearch, searchdirs);
3470 if (dump_opt['C'] > 1) {
3471 (void) printf("\nConfiguration for import:\n");
3472 dump_nvlist(cfg, 8);
3474 if (nvlist_add_nvlist(cfg,
3475 ZPOOL_REWIND_POLICY, policy) != 0) {
3476 fatal("can't open '%s': %s",
3477 target, strerror(ENOMEM));
3479 error = spa_import(name, cfg, NULL, flags);
3484 if (strpbrk(target, "/@") == NULL || dump_opt['R']) {
3485 error = spa_open_rewind(target, &spa, FTAG, policy,
3489 * If we're missing the log device then
3490 * try opening the pool after clearing the
3493 mutex_enter(&spa_namespace_lock);
3494 if ((spa = spa_lookup(target)) != NULL &&
3495 spa->spa_log_state == SPA_LOG_MISSING) {
3496 spa->spa_log_state = SPA_LOG_CLEAR;
3499 mutex_exit(&spa_namespace_lock);
3502 error = spa_open_rewind(target, &spa,
3503 FTAG, policy, NULL);
3507 error = dmu_objset_own(target, DMU_OST_ANY,
3511 nvlist_free(policy);
3514 fatal("can't open '%s': %s", target, strerror(error));
3518 if (!dump_opt['R']) {
3520 zopt_objects = argc;
3521 zopt_object = calloc(zopt_objects, sizeof (uint64_t));
3522 for (i = 0; i < zopt_objects; i++) {
3524 zopt_object[i] = strtoull(argv[i], NULL, 0);
3525 if (zopt_object[i] == 0 && errno != 0)
3526 fatal("bad number %s: %s",
3527 argv[i], strerror(errno));
3532 } else if (zopt_objects > 0 && !dump_opt['m']) {
3533 dump_dir(spa->spa_meta_objset);
3538 flagbits['b'] = ZDB_FLAG_PRINT_BLKPTR;
3539 flagbits['c'] = ZDB_FLAG_CHECKSUM;
3540 flagbits['d'] = ZDB_FLAG_DECOMPRESS;
3541 flagbits['e'] = ZDB_FLAG_BSWAP;
3542 flagbits['g'] = ZDB_FLAG_GBH;
3543 flagbits['i'] = ZDB_FLAG_INDIRECT;
3544 flagbits['p'] = ZDB_FLAG_PHYS;
3545 flagbits['r'] = ZDB_FLAG_RAW;
3547 for (i = 0; i < argc; i++)
3548 zdb_read_block(argv[i], spa);
3551 (os != NULL) ? dmu_objset_disown(os, FTAG) : spa_close(spa, FTAG);
3553 fuid_table_destroy();
3554 sa_loaded = B_FALSE;