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]
22 * Copyright 2008 Sun Microsystems, Inc. All rights reserved.
23 * Use is subject to license terms.
27 #include <stdio_ext.h>
30 #include <sys/zfs_context.h>
32 #include <sys/spa_impl.h>
35 #include <sys/fs/zfs.h>
36 #include <sys/zfs_znode.h>
38 #include <sys/vdev_impl.h>
39 #include <sys/metaslab_impl.h>
40 #include <sys/dmu_objset.h>
41 #include <sys/dsl_dir.h>
42 #include <sys/dsl_dataset.h>
43 #include <sys/dsl_pool.h>
46 #include <sys/zil_impl.h>
48 #include <sys/resource.h>
49 #include <sys/dmu_traverse.h>
50 #include <sys/zio_checksum.h>
51 #include <sys/zio_compress.h>
52 #include <sys/zfs_fuid.h>
58 const char cmdname
[] = "zdb";
59 uint8_t dump_opt
[256];
61 typedef void object_viewer_t(objset_t
*, uint64_t, void *data
, size_t size
);
63 extern void dump_intent_log(zilog_t
*);
64 uint64_t *zopt_object
= NULL
;
66 libzfs_handle_t
*g_zfs
;
67 boolean_t zdb_sig_user_data
= B_TRUE
;
68 int zdb_sig_cksumalg
= ZIO_CHECKSUM_SHA256
;
71 * These libumem hooks provide a reasonable set of defaults for the allocator's
72 * debugging facilities.
77 return ("default,verbose"); /* $UMEM_DEBUG setting */
81 _umem_logging_init(void)
83 return ("fail,contents"); /* $UMEM_LOGGING setting */
89 (void) fprintf(stderr
,
90 "Usage: %s [-udibcsvL] [-U cachefile_path] "
92 "dataset [object...]\n"
95 " %s -R pool:vdev:offset:size:flags\n"
96 " %s [-p path_to_vdev_dir]\n"
97 " %s -e pool | GUID | devid ...\n",
98 cmdname
, cmdname
, cmdname
, cmdname
, cmdname
, cmdname
);
100 (void) fprintf(stderr
, " -u uberblock\n");
101 (void) fprintf(stderr
, " -d datasets\n");
102 (void) fprintf(stderr
, " -C cached pool configuration\n");
103 (void) fprintf(stderr
, " -i intent logs\n");
104 (void) fprintf(stderr
, " -b block statistics\n");
105 (void) fprintf(stderr
, " -c checksum all data blocks\n");
106 (void) fprintf(stderr
, " -s report stats on zdb's I/O\n");
107 (void) fprintf(stderr
, " -S <user|all>:<cksum_alg|all> -- "
108 "dump blkptr signatures\n");
109 (void) fprintf(stderr
, " -v verbose (applies to all others)\n");
110 (void) fprintf(stderr
, " -l dump label contents\n");
111 (void) fprintf(stderr
, " -L disable leak tracking (do not "
112 "load spacemaps)\n");
113 (void) fprintf(stderr
, " -U cachefile_path -- use alternate "
115 (void) fprintf(stderr
, " -R read and display block from a "
117 (void) fprintf(stderr
, " -e Pool is exported/destroyed/"
119 (void) fprintf(stderr
, " -p <Path to vdev dir> (use with -e)\n");
120 (void) fprintf(stderr
, "Specify an option more than once (e.g. -bb) "
121 "to make only that option verbose\n");
122 (void) fprintf(stderr
, "Default is to dump everything non-verbosely\n");
127 fatal(const char *fmt
, ...)
132 (void) fprintf(stderr
, "%s: ", cmdname
);
133 (void) vfprintf(stderr
, fmt
, ap
);
135 (void) fprintf(stderr
, "\n");
141 dump_nvlist(nvlist_t
*list
, int indent
)
143 nvpair_t
*elem
= NULL
;
145 while ((elem
= nvlist_next_nvpair(list
, elem
)) != NULL
) {
146 switch (nvpair_type(elem
)) {
147 case DATA_TYPE_STRING
:
151 VERIFY(nvpair_value_string(elem
, &value
) == 0);
152 (void) printf("%*s%s='%s'\n", indent
, "",
153 nvpair_name(elem
), value
);
157 case DATA_TYPE_UINT64
:
161 VERIFY(nvpair_value_uint64(elem
, &value
) == 0);
162 (void) printf("%*s%s=%llu\n", indent
, "",
163 nvpair_name(elem
), (u_longlong_t
)value
);
167 case DATA_TYPE_NVLIST
:
171 VERIFY(nvpair_value_nvlist(elem
, &value
) == 0);
172 (void) printf("%*s%s\n", indent
, "",
174 dump_nvlist(value
, indent
+ 4);
178 case DATA_TYPE_NVLIST_ARRAY
:
183 VERIFY(nvpair_value_nvlist_array(elem
, &value
,
186 for (c
= 0; c
< count
; c
++) {
187 (void) printf("%*s%s[%u]\n", indent
, "",
188 nvpair_name(elem
), c
);
189 dump_nvlist(value
[c
], indent
+ 8);
196 (void) printf("bad config type %d for %s\n",
197 nvpair_type(elem
), nvpair_name(elem
));
204 dump_packed_nvlist(objset_t
*os
, uint64_t object
, void *data
, size_t size
)
207 size_t nvsize
= *(uint64_t *)data
;
208 char *packed
= umem_alloc(nvsize
, UMEM_NOFAIL
);
210 VERIFY(0 == dmu_read(os
, object
, 0, nvsize
, packed
));
212 VERIFY(nvlist_unpack(packed
, nvsize
, &nv
, 0) == 0);
214 umem_free(packed
, nvsize
);
221 const char dump_zap_stars
[] = "****************************************";
222 const int dump_zap_width
= sizeof (dump_zap_stars
) - 1;
225 dump_zap_histogram(uint64_t histo
[ZAP_HISTOGRAM_SIZE
])
228 int minidx
= ZAP_HISTOGRAM_SIZE
- 1;
232 for (i
= 0; i
< ZAP_HISTOGRAM_SIZE
; i
++) {
235 if (histo
[i
] > 0 && i
> maxidx
)
237 if (histo
[i
] > 0 && i
< minidx
)
241 if (max
< dump_zap_width
)
242 max
= dump_zap_width
;
244 for (i
= minidx
; i
<= maxidx
; i
++)
245 (void) printf("\t\t\t%u: %6llu %s\n", i
, (u_longlong_t
)histo
[i
],
246 &dump_zap_stars
[(max
- histo
[i
]) * dump_zap_width
/ max
]);
250 dump_zap_stats(objset_t
*os
, uint64_t object
)
255 error
= zap_get_stats(os
, object
, &zs
);
259 if (zs
.zs_ptrtbl_len
== 0) {
260 ASSERT(zs
.zs_num_blocks
== 1);
261 (void) printf("\tmicrozap: %llu bytes, %llu entries\n",
262 (u_longlong_t
)zs
.zs_blocksize
,
263 (u_longlong_t
)zs
.zs_num_entries
);
267 (void) printf("\tFat ZAP stats:\n");
269 (void) printf("\t\tPointer table:\n");
270 (void) printf("\t\t\t%llu elements\n",
271 (u_longlong_t
)zs
.zs_ptrtbl_len
);
272 (void) printf("\t\t\tzt_blk: %llu\n",
273 (u_longlong_t
)zs
.zs_ptrtbl_zt_blk
);
274 (void) printf("\t\t\tzt_numblks: %llu\n",
275 (u_longlong_t
)zs
.zs_ptrtbl_zt_numblks
);
276 (void) printf("\t\t\tzt_shift: %llu\n",
277 (u_longlong_t
)zs
.zs_ptrtbl_zt_shift
);
278 (void) printf("\t\t\tzt_blks_copied: %llu\n",
279 (u_longlong_t
)zs
.zs_ptrtbl_blks_copied
);
280 (void) printf("\t\t\tzt_nextblk: %llu\n",
281 (u_longlong_t
)zs
.zs_ptrtbl_nextblk
);
283 (void) printf("\t\tZAP entries: %llu\n",
284 (u_longlong_t
)zs
.zs_num_entries
);
285 (void) printf("\t\tLeaf blocks: %llu\n",
286 (u_longlong_t
)zs
.zs_num_leafs
);
287 (void) printf("\t\tTotal blocks: %llu\n",
288 (u_longlong_t
)zs
.zs_num_blocks
);
289 (void) printf("\t\tzap_block_type: 0x%llx\n",
290 (u_longlong_t
)zs
.zs_block_type
);
291 (void) printf("\t\tzap_magic: 0x%llx\n",
292 (u_longlong_t
)zs
.zs_magic
);
293 (void) printf("\t\tzap_salt: 0x%llx\n",
294 (u_longlong_t
)zs
.zs_salt
);
296 (void) printf("\t\tLeafs with 2^n pointers:\n");
297 dump_zap_histogram(zs
.zs_leafs_with_2n_pointers
);
299 (void) printf("\t\tBlocks with n*5 entries:\n");
300 dump_zap_histogram(zs
.zs_blocks_with_n5_entries
);
302 (void) printf("\t\tBlocks n/10 full:\n");
303 dump_zap_histogram(zs
.zs_blocks_n_tenths_full
);
305 (void) printf("\t\tEntries with n chunks:\n");
306 dump_zap_histogram(zs
.zs_entries_using_n_chunks
);
308 (void) printf("\t\tBuckets with n entries:\n");
309 dump_zap_histogram(zs
.zs_buckets_with_n_entries
);
314 dump_none(objset_t
*os
, uint64_t object
, void *data
, size_t size
)
320 dump_uint8(objset_t
*os
, uint64_t object
, void *data
, size_t size
)
326 dump_uint64(objset_t
*os
, uint64_t object
, void *data
, size_t size
)
332 dump_zap(objset_t
*os
, uint64_t object
, void *data
, size_t size
)
335 zap_attribute_t attr
;
339 dump_zap_stats(os
, object
);
342 for (zap_cursor_init(&zc
, os
, object
);
343 zap_cursor_retrieve(&zc
, &attr
) == 0;
344 zap_cursor_advance(&zc
)) {
345 (void) printf("\t\t%s = ", attr
.za_name
);
346 if (attr
.za_num_integers
== 0) {
350 prop
= umem_zalloc(attr
.za_num_integers
*
351 attr
.za_integer_length
, UMEM_NOFAIL
);
352 (void) zap_lookup(os
, object
, attr
.za_name
,
353 attr
.za_integer_length
, attr
.za_num_integers
, prop
);
354 if (attr
.za_integer_length
== 1) {
355 (void) printf("%s", (char *)prop
);
357 for (i
= 0; i
< attr
.za_num_integers
; i
++) {
358 switch (attr
.za_integer_length
) {
361 ((uint16_t *)prop
)[i
]);
365 ((uint32_t *)prop
)[i
]);
368 (void) printf("%lld ",
369 (u_longlong_t
)((int64_t *)prop
)[i
]);
375 umem_free(prop
, attr
.za_num_integers
* attr
.za_integer_length
);
377 zap_cursor_fini(&zc
);
382 dump_zpldir(objset_t
*os
, uint64_t object
, void *data
, size_t size
)
385 zap_attribute_t attr
;
386 const char *typenames
[] = {
387 /* 0 */ "not specified",
389 /* 2 */ "Character Device",
390 /* 3 */ "3 (invalid)",
392 /* 5 */ "5 (invalid)",
393 /* 6 */ "Block Device",
394 /* 7 */ "7 (invalid)",
395 /* 8 */ "Regular File",
396 /* 9 */ "9 (invalid)",
397 /* 10 */ "Symbolic Link",
398 /* 11 */ "11 (invalid)",
401 /* 14 */ "Event Port",
402 /* 15 */ "15 (invalid)",
405 dump_zap_stats(os
, object
);
408 for (zap_cursor_init(&zc
, os
, object
);
409 zap_cursor_retrieve(&zc
, &attr
) == 0;
410 zap_cursor_advance(&zc
)) {
411 (void) printf("\t\t%s = %lld (type: %s)\n",
412 attr
.za_name
, ZFS_DIRENT_OBJ(attr
.za_first_integer
),
413 typenames
[ZFS_DIRENT_TYPE(attr
.za_first_integer
)]);
415 zap_cursor_fini(&zc
);
419 dump_spacemap(objset_t
*os
, space_map_obj_t
*smo
, space_map_t
*sm
)
421 uint64_t alloc
, offset
, entry
;
422 uint8_t mapshift
= sm
->sm_shift
;
423 uint64_t mapstart
= sm
->sm_start
;
424 char *ddata
[] = { "ALLOC", "FREE", "CONDENSE", "INVALID",
425 "INVALID", "INVALID", "INVALID", "INVALID" };
427 if (smo
->smo_object
== 0)
431 * Print out the freelist entries in both encoded and decoded form.
434 for (offset
= 0; offset
< smo
->smo_objsize
; offset
+= sizeof (entry
)) {
435 VERIFY(0 == dmu_read(os
, smo
->smo_object
, offset
,
436 sizeof (entry
), &entry
));
437 if (SM_DEBUG_DECODE(entry
)) {
438 (void) printf("\t\t[%4llu] %s: txg %llu, pass %llu\n",
439 (u_longlong_t
)(offset
/ sizeof (entry
)),
440 ddata
[SM_DEBUG_ACTION_DECODE(entry
)],
441 (u_longlong_t
)SM_DEBUG_TXG_DECODE(entry
),
442 (u_longlong_t
)SM_DEBUG_SYNCPASS_DECODE(entry
));
444 (void) printf("\t\t[%4llu] %c range:"
445 " %08llx-%08llx size: %06llx\n",
446 (u_longlong_t
)(offset
/ sizeof (entry
)),
447 SM_TYPE_DECODE(entry
) == SM_ALLOC
? 'A' : 'F',
448 (u_longlong_t
)((SM_OFFSET_DECODE(entry
) <<
449 mapshift
) + mapstart
),
450 (u_longlong_t
)((SM_OFFSET_DECODE(entry
) <<
451 mapshift
) + mapstart
+ (SM_RUN_DECODE(entry
) <<
453 (u_longlong_t
)(SM_RUN_DECODE(entry
) << mapshift
));
454 if (SM_TYPE_DECODE(entry
) == SM_ALLOC
)
455 alloc
+= SM_RUN_DECODE(entry
) << mapshift
;
457 alloc
-= SM_RUN_DECODE(entry
) << mapshift
;
460 if (alloc
!= smo
->smo_alloc
) {
461 (void) printf("space_map_object alloc (%llu) INCONSISTENT "
462 "with space map summary (%llu)\n",
463 (u_longlong_t
)smo
->smo_alloc
, (u_longlong_t
)alloc
);
468 dump_metaslab(metaslab_t
*msp
)
471 space_map_obj_t
*smo
= &msp
->ms_smo
;
472 vdev_t
*vd
= msp
->ms_group
->mg_vd
;
473 spa_t
*spa
= vd
->vdev_spa
;
475 nicenum(msp
->ms_map
.sm_size
- smo
->smo_alloc
, freebuf
);
477 if (dump_opt
['d'] <= 5) {
478 (void) printf("\t%10llx %10llu %5s\n",
479 (u_longlong_t
)msp
->ms_map
.sm_start
,
480 (u_longlong_t
)smo
->smo_object
,
486 "\tvdev %llu offset %08llx spacemap %4llu free %5s\n",
487 (u_longlong_t
)vd
->vdev_id
, (u_longlong_t
)msp
->ms_map
.sm_start
,
488 (u_longlong_t
)smo
->smo_object
, freebuf
);
490 ASSERT(msp
->ms_map
.sm_size
== (1ULL << vd
->vdev_ms_shift
));
492 dump_spacemap(spa
->spa_meta_objset
, smo
, &msp
->ms_map
);
496 dump_metaslabs(spa_t
*spa
)
498 vdev_t
*rvd
= spa
->spa_root_vdev
;
502 (void) printf("\nMetaslabs:\n");
504 for (c
= 0; c
< rvd
->vdev_children
; c
++) {
505 vd
= rvd
->vdev_child
[c
];
507 (void) printf("\n vdev %llu\n\n", (u_longlong_t
)vd
->vdev_id
);
509 if (dump_opt
['d'] <= 5) {
510 (void) printf("\t%10s %10s %5s\n",
511 "offset", "spacemap", "free");
512 (void) printf("\t%10s %10s %5s\n",
513 "------", "--------", "----");
515 for (m
= 0; m
< vd
->vdev_ms_count
; m
++)
516 dump_metaslab(vd
->vdev_ms
[m
]);
522 dump_dtl(vdev_t
*vd
, int indent
)
524 avl_tree_t
*t
= &vd
->vdev_dtl_map
.sm_root
;
530 (void) printf("\nDirty time logs:\n\n");
532 (void) printf("\t%*s%s\n", indent
, "",
533 vd
->vdev_path
? vd
->vdev_path
:
534 vd
->vdev_parent
? vd
->vdev_ops
->vdev_op_type
:
535 spa_name(vd
->vdev_spa
));
537 for (ss
= avl_first(t
); ss
; ss
= AVL_NEXT(t
, ss
)) {
539 * Everything in this DTL must appear in all parent DTL unions.
541 for (pvd
= vd
; pvd
; pvd
= pvd
->vdev_parent
)
542 ASSERT(vdev_dtl_contains(&pvd
->vdev_dtl_map
,
543 ss
->ss_start
, ss
->ss_end
- ss
->ss_start
));
544 (void) printf("\t%*soutage [%llu,%llu] length %llu\n",
546 (u_longlong_t
)ss
->ss_start
,
547 (u_longlong_t
)ss
->ss_end
- 1,
548 (u_longlong_t
)(ss
->ss_end
- ss
->ss_start
));
553 if (dump_opt
['d'] > 5 && vd
->vdev_children
== 0) {
554 dump_spacemap(vd
->vdev_spa
->spa_meta_objset
, &vd
->vdev_dtl
,
559 for (c
= 0; c
< vd
->vdev_children
; c
++)
560 dump_dtl(vd
->vdev_child
[c
], indent
+ 4);
565 dump_dnode(objset_t
*os
, uint64_t object
, void *data
, size_t size
)
570 blkid2offset(const dnode_phys_t
*dnp
, int level
, uint64_t blkid
)
575 return ((blkid
<< (level
* (dnp
->dn_indblkshift
- SPA_BLKPTRSHIFT
))) *
576 dnp
->dn_datablkszsec
<< SPA_MINBLOCKSHIFT
);
580 sprintf_blkptr_compact(char *blkbuf
, blkptr_t
*bp
, int alldvas
)
582 dva_t
*dva
= bp
->blk_dva
;
583 int ndvas
= alldvas
? BP_GET_NDVAS(bp
) : 1;
588 for (i
= 0; i
< ndvas
; i
++)
589 (void) sprintf(blkbuf
+ strlen(blkbuf
), "%llu:%llx:%llx ",
590 (u_longlong_t
)DVA_GET_VDEV(&dva
[i
]),
591 (u_longlong_t
)DVA_GET_OFFSET(&dva
[i
]),
592 (u_longlong_t
)DVA_GET_ASIZE(&dva
[i
]));
594 (void) sprintf(blkbuf
+ strlen(blkbuf
), "%llxL/%llxP F=%llu B=%llu",
595 (u_longlong_t
)BP_GET_LSIZE(bp
),
596 (u_longlong_t
)BP_GET_PSIZE(bp
),
597 (u_longlong_t
)bp
->blk_fill
,
598 (u_longlong_t
)bp
->blk_birth
);
602 print_indirect(blkptr_t
*bp
, const zbookmark_t
*zb
,
603 const dnode_phys_t
*dnp
)
605 char blkbuf
[BP_SPRINTF_LEN
];
608 ASSERT3U(BP_GET_TYPE(bp
), ==, dnp
->dn_type
);
609 ASSERT3U(BP_GET_LEVEL(bp
), ==, zb
->zb_level
);
611 (void) printf("%16llx ",
612 (u_longlong_t
)blkid2offset(dnp
, zb
->zb_level
, zb
->zb_blkid
));
614 ASSERT(zb
->zb_level
>= 0);
616 for (l
= dnp
->dn_nlevels
- 1; l
>= -1; l
--) {
617 if (l
== zb
->zb_level
) {
618 (void) printf("L%llx", (u_longlong_t
)zb
->zb_level
);
624 sprintf_blkptr_compact(blkbuf
, bp
, dump_opt
['d'] > 5 ? 1 : 0);
625 (void) printf("%s\n", blkbuf
);
628 #define SET_BOOKMARK(zb, objset, object, level, blkid) \
630 (zb)->zb_objset = objset; \
631 (zb)->zb_object = object; \
632 (zb)->zb_level = level; \
633 (zb)->zb_blkid = blkid; \
637 visit_indirect(spa_t
*spa
, const dnode_phys_t
*dnp
,
638 blkptr_t
*bp
, const zbookmark_t
*zb
)
642 if (bp
->blk_birth
== 0)
645 print_indirect(bp
, zb
, dnp
);
647 if (BP_GET_LEVEL(bp
) > 0) {
648 uint32_t flags
= ARC_WAIT
;
651 int epb
= BP_GET_LSIZE(bp
) >> SPA_BLKPTRSHIFT
;
655 err
= arc_read_nolock(NULL
, spa
, bp
, arc_getbuf_func
, &buf
,
656 ZIO_PRIORITY_ASYNC_READ
, ZIO_FLAG_CANFAIL
, &flags
, zb
);
660 /* recursively visit blocks below this */
662 for (i
= 0; i
< epb
; i
++, cbp
++) {
665 SET_BOOKMARK(&czb
, zb
->zb_objset
, zb
->zb_object
,
667 zb
->zb_blkid
* epb
+ i
);
668 err
= visit_indirect(spa
, dnp
, cbp
, &czb
);
671 fill
+= cbp
->blk_fill
;
673 ASSERT3U(fill
, ==, bp
->blk_fill
);
674 (void) arc_buf_remove_ref(buf
, &buf
);
682 dump_indirect(dnode_t
*dn
)
684 dnode_phys_t
*dnp
= dn
->dn_phys
;
688 (void) printf("Indirect blocks:\n");
690 SET_BOOKMARK(&czb
, dmu_objset_id(&dn
->dn_objset
->os
),
691 dn
->dn_object
, dnp
->dn_nlevels
- 1, 0);
692 for (j
= 0; j
< dnp
->dn_nblkptr
; j
++) {
694 (void) visit_indirect(dmu_objset_spa(&dn
->dn_objset
->os
), dnp
,
695 &dnp
->dn_blkptr
[j
], &czb
);
703 dump_dsl_dir(objset_t
*os
, uint64_t object
, void *data
, size_t size
)
705 dsl_dir_phys_t
*dd
= data
;
712 ASSERT3U(size
, >=, sizeof (dsl_dir_phys_t
));
714 crtime
= dd
->dd_creation_time
;
715 (void) printf("\t\tcreation_time = %s", ctime(&crtime
));
716 (void) printf("\t\thead_dataset_obj = %llu\n",
717 (u_longlong_t
)dd
->dd_head_dataset_obj
);
718 (void) printf("\t\tparent_dir_obj = %llu\n",
719 (u_longlong_t
)dd
->dd_parent_obj
);
720 (void) printf("\t\torigin_obj = %llu\n",
721 (u_longlong_t
)dd
->dd_origin_obj
);
722 (void) printf("\t\tchild_dir_zapobj = %llu\n",
723 (u_longlong_t
)dd
->dd_child_dir_zapobj
);
724 nicenum(dd
->dd_used_bytes
, nice
);
725 (void) printf("\t\tused_bytes = %s\n", nice
);
726 nicenum(dd
->dd_compressed_bytes
, nice
);
727 (void) printf("\t\tcompressed_bytes = %s\n", nice
);
728 nicenum(dd
->dd_uncompressed_bytes
, nice
);
729 (void) printf("\t\tuncompressed_bytes = %s\n", nice
);
730 nicenum(dd
->dd_quota
, nice
);
731 (void) printf("\t\tquota = %s\n", nice
);
732 nicenum(dd
->dd_reserved
, nice
);
733 (void) printf("\t\treserved = %s\n", nice
);
734 (void) printf("\t\tprops_zapobj = %llu\n",
735 (u_longlong_t
)dd
->dd_props_zapobj
);
736 (void) printf("\t\tdeleg_zapobj = %llu\n",
737 (u_longlong_t
)dd
->dd_deleg_zapobj
);
738 (void) printf("\t\tflags = %llx\n",
739 (u_longlong_t
)dd
->dd_flags
);
742 nicenum(dd->dd_used_breakdown[DD_USED_ ## which], nice); \
743 (void) printf("\t\tused_breakdown[" #which "] = %s\n", nice)
754 dump_dsl_dataset(objset_t
*os
, uint64_t object
, void *data
, size_t size
)
756 dsl_dataset_phys_t
*ds
= data
;
758 char used
[6], compressed
[6], uncompressed
[6], unique
[6];
759 char blkbuf
[BP_SPRINTF_LEN
];
764 ASSERT(size
== sizeof (*ds
));
765 crtime
= ds
->ds_creation_time
;
766 nicenum(ds
->ds_used_bytes
, used
);
767 nicenum(ds
->ds_compressed_bytes
, compressed
);
768 nicenum(ds
->ds_uncompressed_bytes
, uncompressed
);
769 nicenum(ds
->ds_unique_bytes
, unique
);
770 sprintf_blkptr(blkbuf
, BP_SPRINTF_LEN
, &ds
->ds_bp
);
772 (void) printf("\t\tdir_obj = %llu\n",
773 (u_longlong_t
)ds
->ds_dir_obj
);
774 (void) printf("\t\tprev_snap_obj = %llu\n",
775 (u_longlong_t
)ds
->ds_prev_snap_obj
);
776 (void) printf("\t\tprev_snap_txg = %llu\n",
777 (u_longlong_t
)ds
->ds_prev_snap_txg
);
778 (void) printf("\t\tnext_snap_obj = %llu\n",
779 (u_longlong_t
)ds
->ds_next_snap_obj
);
780 (void) printf("\t\tsnapnames_zapobj = %llu\n",
781 (u_longlong_t
)ds
->ds_snapnames_zapobj
);
782 (void) printf("\t\tnum_children = %llu\n",
783 (u_longlong_t
)ds
->ds_num_children
);
784 (void) printf("\t\tcreation_time = %s", ctime(&crtime
));
785 (void) printf("\t\tcreation_txg = %llu\n",
786 (u_longlong_t
)ds
->ds_creation_txg
);
787 (void) printf("\t\tdeadlist_obj = %llu\n",
788 (u_longlong_t
)ds
->ds_deadlist_obj
);
789 (void) printf("\t\tused_bytes = %s\n", used
);
790 (void) printf("\t\tcompressed_bytes = %s\n", compressed
);
791 (void) printf("\t\tuncompressed_bytes = %s\n", uncompressed
);
792 (void) printf("\t\tunique = %s\n", unique
);
793 (void) printf("\t\tfsid_guid = %llu\n",
794 (u_longlong_t
)ds
->ds_fsid_guid
);
795 (void) printf("\t\tguid = %llu\n",
796 (u_longlong_t
)ds
->ds_guid
);
797 (void) printf("\t\tflags = %llx\n",
798 (u_longlong_t
)ds
->ds_flags
);
799 (void) printf("\t\tnext_clones_obj = %llu\n",
800 (u_longlong_t
)ds
->ds_next_clones_obj
);
801 (void) printf("\t\tprops_obj = %llu\n",
802 (u_longlong_t
)ds
->ds_props_obj
);
803 (void) printf("\t\tbp = %s\n", blkbuf
);
807 dump_bplist(objset_t
*mos
, uint64_t object
, char *name
)
809 bplist_t bpl
= { 0 };
810 blkptr_t blk
, *bp
= &blk
;
816 if (dump_opt
['d'] < 3)
819 mutex_init(&bpl
.bpl_lock
, NULL
, MUTEX_DEFAULT
, NULL
);
820 VERIFY(0 == bplist_open(&bpl
, mos
, object
));
821 if (bplist_empty(&bpl
)) {
823 mutex_destroy(&bpl
.bpl_lock
);
827 nicenum(bpl
.bpl_phys
->bpl_bytes
, bytes
);
828 if (bpl
.bpl_dbuf
->db_size
== sizeof (bplist_phys_t
)) {
829 nicenum(bpl
.bpl_phys
->bpl_comp
, comp
);
830 nicenum(bpl
.bpl_phys
->bpl_uncomp
, uncomp
);
831 (void) printf("\n %s: %llu entries, %s (%s/%s comp)\n",
832 name
, (u_longlong_t
)bpl
.bpl_phys
->bpl_entries
,
833 bytes
, comp
, uncomp
);
835 (void) printf("\n %s: %llu entries, %s\n",
836 name
, (u_longlong_t
)bpl
.bpl_phys
->bpl_entries
, bytes
);
839 if (dump_opt
['d'] < 5) {
841 mutex_destroy(&bpl
.bpl_lock
);
847 while (bplist_iterate(&bpl
, &itor
, bp
) == 0) {
848 char blkbuf
[BP_SPRINTF_LEN
];
850 ASSERT(bp
->blk_birth
!= 0);
851 sprintf_blkptr_compact(blkbuf
, bp
, dump_opt
['d'] > 5 ? 1 : 0);
852 (void) printf("\tItem %3llu: %s\n",
853 (u_longlong_t
)itor
- 1, blkbuf
);
857 mutex_destroy(&bpl
.bpl_lock
);
860 static avl_tree_t idx_tree
;
861 static avl_tree_t domain_tree
;
862 static boolean_t fuid_table_loaded
;
867 if (fuid_table_loaded
) {
868 zfs_fuid_table_destroy(&idx_tree
, &domain_tree
);
869 fuid_table_loaded
= B_FALSE
;
874 * print uid or gid information.
875 * For normal POSIX id just the id is printed in decimal format.
876 * For CIFS files with FUID the fuid is printed in hex followed by
877 * the doman-rid string.
880 print_idstr(uint64_t id
, const char *id_type
)
882 if (FUID_INDEX(id
)) {
885 domain
= zfs_fuid_idx_domain(&idx_tree
, FUID_INDEX(id
));
886 (void) printf("\t%s %llx [%s-%d]\n", id_type
,
887 (u_longlong_t
)id
, domain
, (int)FUID_RID(id
));
889 (void) printf("\t%s %llu\n", id_type
, (u_longlong_t
)id
);
895 dump_uidgid(objset_t
*os
, znode_phys_t
*zp
)
897 uint32_t uid_idx
, gid_idx
;
899 uid_idx
= FUID_INDEX(zp
->zp_uid
);
900 gid_idx
= FUID_INDEX(zp
->zp_gid
);
902 /* Load domain table, if not already loaded */
903 if (!fuid_table_loaded
&& (uid_idx
|| gid_idx
)) {
906 /* first find the fuid object. It lives in the master node */
907 VERIFY(zap_lookup(os
, MASTER_NODE_OBJ
, ZFS_FUID_TABLES
,
908 8, 1, &fuid_obj
) == 0);
909 (void) zfs_fuid_table_load(os
, fuid_obj
,
910 &idx_tree
, &domain_tree
);
911 fuid_table_loaded
= B_TRUE
;
914 print_idstr(zp
->zp_uid
, "uid");
915 print_idstr(zp
->zp_gid
, "gid");
920 dump_znode(objset_t
*os
, uint64_t object
, void *data
, size_t size
)
922 znode_phys_t
*zp
= data
;
923 time_t z_crtime
, z_atime
, z_mtime
, z_ctime
;
924 char path
[MAXPATHLEN
* 2]; /* allow for xattr and failure prefix */
927 ASSERT(size
>= sizeof (znode_phys_t
));
929 error
= zfs_obj_to_path(os
, object
, path
, sizeof (path
));
931 (void) snprintf(path
, sizeof (path
), "\?\?\?<object#%llu>",
932 (u_longlong_t
)object
);
935 if (dump_opt
['d'] < 3) {
936 (void) printf("\t%s\n", path
);
940 z_crtime
= (time_t)zp
->zp_crtime
[0];
941 z_atime
= (time_t)zp
->zp_atime
[0];
942 z_mtime
= (time_t)zp
->zp_mtime
[0];
943 z_ctime
= (time_t)zp
->zp_ctime
[0];
945 (void) printf("\tpath %s\n", path
);
947 (void) printf("\tatime %s", ctime(&z_atime
));
948 (void) printf("\tmtime %s", ctime(&z_mtime
));
949 (void) printf("\tctime %s", ctime(&z_ctime
));
950 (void) printf("\tcrtime %s", ctime(&z_crtime
));
951 (void) printf("\tgen %llu\n", (u_longlong_t
)zp
->zp_gen
);
952 (void) printf("\tmode %llo\n", (u_longlong_t
)zp
->zp_mode
);
953 (void) printf("\tsize %llu\n", (u_longlong_t
)zp
->zp_size
);
954 (void) printf("\tparent %llu\n", (u_longlong_t
)zp
->zp_parent
);
955 (void) printf("\tlinks %llu\n", (u_longlong_t
)zp
->zp_links
);
956 (void) printf("\txattr %llu\n", (u_longlong_t
)zp
->zp_xattr
);
957 (void) printf("\trdev 0x%016llx\n", (u_longlong_t
)zp
->zp_rdev
);
962 dump_acl(objset_t
*os
, uint64_t object
, void *data
, size_t size
)
968 dump_dmu_objset(objset_t
*os
, uint64_t object
, void *data
, size_t size
)
972 static object_viewer_t
*object_viewer
[DMU_OT_NUMTYPES
] = {
973 dump_none
, /* unallocated */
974 dump_zap
, /* object directory */
975 dump_uint64
, /* object array */
976 dump_none
, /* packed nvlist */
977 dump_packed_nvlist
, /* packed nvlist size */
978 dump_none
, /* bplist */
979 dump_none
, /* bplist header */
980 dump_none
, /* SPA space map header */
981 dump_none
, /* SPA space map */
982 dump_none
, /* ZIL intent log */
983 dump_dnode
, /* DMU dnode */
984 dump_dmu_objset
, /* DMU objset */
985 dump_dsl_dir
, /* DSL directory */
986 dump_zap
, /* DSL directory child map */
987 dump_zap
, /* DSL dataset snap map */
988 dump_zap
, /* DSL props */
989 dump_dsl_dataset
, /* DSL dataset */
990 dump_znode
, /* ZFS znode */
991 dump_acl
, /* ZFS V0 ACL */
992 dump_uint8
, /* ZFS plain file */
993 dump_zpldir
, /* ZFS directory */
994 dump_zap
, /* ZFS master node */
995 dump_zap
, /* ZFS delete queue */
996 dump_uint8
, /* zvol object */
997 dump_zap
, /* zvol prop */
998 dump_uint8
, /* other uint8[] */
999 dump_uint64
, /* other uint64[] */
1000 dump_zap
, /* other ZAP */
1001 dump_zap
, /* persistent error log */
1002 dump_uint8
, /* SPA history */
1003 dump_uint64
, /* SPA history offsets */
1004 dump_zap
, /* Pool properties */
1005 dump_zap
, /* DSL permissions */
1006 dump_acl
, /* ZFS ACL */
1007 dump_uint8
, /* ZFS SYSACL */
1008 dump_none
, /* FUID nvlist */
1009 dump_packed_nvlist
, /* FUID nvlist size */
1010 dump_zap
, /* DSL dataset next clones */
1011 dump_zap
, /* DSL scrub queue */
1015 dump_object(objset_t
*os
, uint64_t object
, int verbosity
, int *print_header
)
1017 dmu_buf_t
*db
= NULL
;
1018 dmu_object_info_t doi
;
1022 char iblk
[6], dblk
[6], lsize
[6], asize
[6], bonus_size
[6], segsize
[6];
1026 if (*print_header
) {
1027 (void) printf("\n Object lvl iblk dblk lsize"
1033 dn
= os
->os
->os_meta_dnode
;
1035 error
= dmu_bonus_hold(os
, object
, FTAG
, &db
);
1037 fatal("dmu_bonus_hold(%llu) failed, errno %u",
1039 bonus
= db
->db_data
;
1040 bsize
= db
->db_size
;
1041 dn
= ((dmu_buf_impl_t
*)db
)->db_dnode
;
1043 dmu_object_info_from_dnode(dn
, &doi
);
1045 nicenum(doi
.doi_metadata_block_size
, iblk
);
1046 nicenum(doi
.doi_data_block_size
, dblk
);
1047 nicenum(doi
.doi_data_block_size
* (doi
.doi_max_block_offset
+ 1),
1049 nicenum(doi
.doi_physical_blks
<< 9, asize
);
1050 nicenum(doi
.doi_bonus_size
, bonus_size
);
1054 if (doi
.doi_checksum
!= ZIO_CHECKSUM_INHERIT
|| verbosity
>= 6) {
1055 (void) snprintf(aux
+ strlen(aux
), sizeof (aux
), " (K=%s)",
1056 zio_checksum_table
[doi
.doi_checksum
].ci_name
);
1059 if (doi
.doi_compress
!= ZIO_COMPRESS_INHERIT
|| verbosity
>= 6) {
1060 (void) snprintf(aux
+ strlen(aux
), sizeof (aux
), " (Z=%s)",
1061 zio_compress_table
[doi
.doi_compress
].ci_name
);
1064 (void) printf("%10lld %3u %5s %5s %5s %5s %s%s\n",
1065 (u_longlong_t
)object
, doi
.doi_indirection
, iblk
, dblk
, lsize
,
1066 asize
, dmu_ot
[doi
.doi_type
].ot_name
, aux
);
1068 if (doi
.doi_bonus_type
!= DMU_OT_NONE
&& verbosity
> 3) {
1069 (void) printf("%10s %3s %5s %5s %5s %5s %s\n",
1070 "", "", "", "", bonus_size
, "bonus",
1071 dmu_ot
[doi
.doi_bonus_type
].ot_name
);
1074 if (verbosity
>= 4) {
1075 object_viewer
[doi
.doi_bonus_type
](os
, object
, bonus
, bsize
);
1076 object_viewer
[doi
.doi_type
](os
, object
, NULL
, 0);
1083 if (verbosity
>= 5) {
1085 * Report the list of segments that comprise the object.
1089 uint64_t blkfill
= 1;
1092 if (dn
->dn_type
== DMU_OT_DNODE
) {
1094 blkfill
= DNODES_PER_BLOCK
;
1098 error
= dnode_next_offset(dn
,
1099 0, &start
, minlvl
, blkfill
, 0);
1103 error
= dnode_next_offset(dn
,
1104 DNODE_FIND_HOLE
, &end
, minlvl
, blkfill
, 0);
1105 nicenum(end
- start
, segsize
);
1106 (void) printf("\t\tsegment [%016llx, %016llx)"
1107 " size %5s\n", (u_longlong_t
)start
,
1108 (u_longlong_t
)end
, segsize
);
1116 dmu_buf_rele(db
, FTAG
);
1119 static char *objset_types
[DMU_OST_NUMTYPES
] = {
1120 "NONE", "META", "ZPL", "ZVOL", "OTHER", "ANY" };
1123 dump_dir(objset_t
*os
)
1125 dmu_objset_stats_t dds
;
1126 uint64_t object
, object_count
;
1127 uint64_t refdbytes
, usedobjs
, scratch
;
1129 char blkbuf
[BP_SPRINTF_LEN
];
1130 char osname
[MAXNAMELEN
];
1131 char *type
= "UNKNOWN";
1132 int verbosity
= dump_opt
['d'];
1133 int print_header
= 1;
1136 dmu_objset_fast_stat(os
, &dds
);
1138 if (dds
.dds_type
< DMU_OST_NUMTYPES
)
1139 type
= objset_types
[dds
.dds_type
];
1141 if (dds
.dds_type
== DMU_OST_META
) {
1142 dds
.dds_creation_txg
= TXG_INITIAL
;
1143 usedobjs
= os
->os
->os_rootbp
->blk_fill
;
1144 refdbytes
= os
->os
->os_spa
->spa_dsl_pool
->
1145 dp_mos_dir
->dd_phys
->dd_used_bytes
;
1147 dmu_objset_space(os
, &refdbytes
, &scratch
, &usedobjs
, &scratch
);
1150 ASSERT3U(usedobjs
, ==, os
->os
->os_rootbp
->blk_fill
);
1152 nicenum(refdbytes
, numbuf
);
1154 if (verbosity
>= 4) {
1155 (void) strcpy(blkbuf
, ", rootbp ");
1156 sprintf_blkptr(blkbuf
+ strlen(blkbuf
),
1157 BP_SPRINTF_LEN
- strlen(blkbuf
), os
->os
->os_rootbp
);
1162 dmu_objset_name(os
, osname
);
1164 (void) printf("Dataset %s [%s], ID %llu, cr_txg %llu, "
1165 "%s, %llu objects%s\n",
1166 osname
, type
, (u_longlong_t
)dmu_objset_id(os
),
1167 (u_longlong_t
)dds
.dds_creation_txg
,
1168 numbuf
, (u_longlong_t
)usedobjs
, blkbuf
);
1170 dump_intent_log(dmu_objset_zil(os
));
1172 if (dmu_objset_ds(os
) != NULL
)
1173 dump_bplist(dmu_objset_pool(os
)->dp_meta_objset
,
1174 dmu_objset_ds(os
)->ds_phys
->ds_deadlist_obj
, "Deadlist");
1179 if (os
->os
->os_rootbp
->blk_birth
== 0)
1182 if (zopt_objects
!= 0) {
1183 for (i
= 0; i
< zopt_objects
; i
++)
1184 dump_object(os
, zopt_object
[i
], verbosity
,
1186 (void) printf("\n");
1190 dump_object(os
, 0, verbosity
, &print_header
);
1194 while ((error
= dmu_object_next(os
, &object
, B_FALSE
, 0)) == 0) {
1195 dump_object(os
, object
, verbosity
, &print_header
);
1199 ASSERT3U(object_count
, ==, usedobjs
);
1201 (void) printf("\n");
1204 fatal("dmu_object_next() = %d", error
);
1208 dump_uberblock(uberblock_t
*ub
)
1210 time_t timestamp
= ub
->ub_timestamp
;
1212 (void) printf("Uberblock\n\n");
1213 (void) printf("\tmagic = %016llx\n", (u_longlong_t
)ub
->ub_magic
);
1214 (void) printf("\tversion = %llu\n", (u_longlong_t
)ub
->ub_version
);
1215 (void) printf("\ttxg = %llu\n", (u_longlong_t
)ub
->ub_txg
);
1216 (void) printf("\tguid_sum = %llu\n", (u_longlong_t
)ub
->ub_guid_sum
);
1217 (void) printf("\ttimestamp = %llu UTC = %s",
1218 (u_longlong_t
)ub
->ub_timestamp
, asctime(localtime(×tamp
)));
1219 if (dump_opt
['u'] >= 3) {
1220 char blkbuf
[BP_SPRINTF_LEN
];
1221 sprintf_blkptr(blkbuf
, BP_SPRINTF_LEN
, &ub
->ub_rootbp
);
1222 (void) printf("\trootbp = %s\n", blkbuf
);
1224 (void) printf("\n");
1228 dump_config(const char *pool
)
1232 mutex_enter(&spa_namespace_lock
);
1233 while ((spa
= spa_next(spa
)) != NULL
) {
1235 (void) printf("%s\n", spa_name(spa
));
1236 if (pool
== NULL
|| strcmp(pool
, spa_name(spa
)) == 0)
1237 dump_nvlist(spa
->spa_config
, 4);
1239 mutex_exit(&spa_namespace_lock
);
1243 dump_cachefile(const char *cachefile
)
1246 struct stat64 statbuf
;
1250 if ((fd
= open64(cachefile
, O_RDONLY
)) < 0) {
1251 (void) printf("cannot open '%s': %s\n", cachefile
,
1256 if (fstat64(fd
, &statbuf
) != 0) {
1257 (void) printf("failed to stat '%s': %s\n", cachefile
,
1262 if ((buf
= malloc(statbuf
.st_size
)) == NULL
) {
1263 (void) fprintf(stderr
, "failed to allocate %llu bytes\n",
1264 (u_longlong_t
)statbuf
.st_size
);
1268 if (read(fd
, buf
, statbuf
.st_size
) != statbuf
.st_size
) {
1269 (void) fprintf(stderr
, "failed to read %llu bytes\n",
1270 (u_longlong_t
)statbuf
.st_size
);
1276 if (nvlist_unpack(buf
, statbuf
.st_size
, &config
, 0) != 0) {
1277 (void) fprintf(stderr
, "failed to unpack nvlist\n");
1283 dump_nvlist(config
, 0);
1285 nvlist_free(config
);
1289 dump_label(const char *dev
)
1293 char *buf
= label
.vl_vdev_phys
.vp_nvlist
;
1294 size_t buflen
= sizeof (label
.vl_vdev_phys
.vp_nvlist
);
1295 struct stat64 statbuf
;
1299 if ((fd
= open64(dev
, O_RDONLY
)) < 0) {
1300 (void) printf("cannot open '%s': %s\n", dev
, strerror(errno
));
1304 if (fstat64(fd
, &statbuf
) != 0) {
1305 (void) printf("failed to stat '%s': %s\n", dev
,
1310 psize
= statbuf
.st_size
;
1311 psize
= P2ALIGN(psize
, (uint64_t)sizeof (vdev_label_t
));
1313 for (l
= 0; l
< VDEV_LABELS
; l
++) {
1315 nvlist_t
*config
= NULL
;
1317 (void) printf("--------------------------------------------\n");
1318 (void) printf("LABEL %d\n", l
);
1319 (void) printf("--------------------------------------------\n");
1321 if (pread64(fd
, &label
, sizeof (label
),
1322 vdev_label_offset(psize
, l
, 0)) != sizeof (label
)) {
1323 (void) printf("failed to read label %d\n", l
);
1327 if (nvlist_unpack(buf
, buflen
, &config
, 0) != 0) {
1328 (void) printf("failed to unpack label %d\n", l
);
1331 dump_nvlist(config
, 4);
1332 nvlist_free(config
);
1338 dump_one_dir(char *dsname
, void *arg
)
1343 error
= dmu_objset_open(dsname
, DMU_OST_ANY
,
1344 DS_MODE_USER
| DS_MODE_READONLY
, &os
);
1346 (void) printf("Could not open %s\n", dsname
);
1350 dmu_objset_close(os
);
1351 fuid_table_destroy();
1356 zdb_leak(space_map_t
*sm
, uint64_t start
, uint64_t size
)
1358 vdev_t
*vd
= sm
->sm_ppd
;
1360 (void) printf("leaked space: vdev %llu, offset 0x%llx, size %llu\n",
1361 (u_longlong_t
)vd
->vdev_id
, (u_longlong_t
)start
, (u_longlong_t
)size
);
1366 zdb_space_map_load(space_map_t
*sm
)
1371 zdb_space_map_unload(space_map_t
*sm
)
1373 space_map_vacate(sm
, zdb_leak
, sm
);
1378 zdb_space_map_claim(space_map_t
*sm
, uint64_t start
, uint64_t size
)
1382 static space_map_ops_t zdb_space_map_ops
= {
1384 zdb_space_map_unload
,
1386 zdb_space_map_claim
,
1391 zdb_leak_init(spa_t
*spa
)
1393 vdev_t
*rvd
= spa
->spa_root_vdev
;
1395 for (int c
= 0; c
< rvd
->vdev_children
; c
++) {
1396 vdev_t
*vd
= rvd
->vdev_child
[c
];
1397 for (int m
= 0; m
< vd
->vdev_ms_count
; m
++) {
1398 metaslab_t
*msp
= vd
->vdev_ms
[m
];
1399 mutex_enter(&msp
->ms_lock
);
1400 VERIFY(space_map_load(&msp
->ms_map
, &zdb_space_map_ops
,
1401 SM_ALLOC
, &msp
->ms_smo
, spa
->spa_meta_objset
) == 0);
1402 msp
->ms_map
.sm_ppd
= vd
;
1403 mutex_exit(&msp
->ms_lock
);
1409 zdb_leak_fini(spa_t
*spa
)
1411 vdev_t
*rvd
= spa
->spa_root_vdev
;
1413 for (int c
= 0; c
< rvd
->vdev_children
; c
++) {
1414 vdev_t
*vd
= rvd
->vdev_child
[c
];
1415 for (int m
= 0; m
< vd
->vdev_ms_count
; m
++) {
1416 metaslab_t
*msp
= vd
->vdev_ms
[m
];
1417 mutex_enter(&msp
->ms_lock
);
1418 space_map_unload(&msp
->ms_map
);
1419 mutex_exit(&msp
->ms_lock
);
1425 * Verify that the sum of the sizes of all blocks in the pool adds up
1426 * to the SPA's sa_alloc total.
1428 typedef struct zdb_blkstats
{
1435 #define DMU_OT_DEFERRED DMU_OT_NONE
1436 #define DMU_OT_TOTAL DMU_OT_NUMTYPES
1438 #define ZB_TOTAL DN_MAX_LEVELS
1440 typedef struct zdb_cb
{
1441 zdb_blkstats_t zcb_type
[ZB_TOTAL
+ 1][DMU_OT_TOTAL
+ 1];
1442 uint64_t zcb_errors
[256];
1448 zdb_count_block(spa_t
*spa
, zdb_cb_t
*zcb
, blkptr_t
*bp
, dmu_object_type_t type
)
1450 for (int i
= 0; i
< 4; i
++) {
1451 int l
= (i
< 2) ? BP_GET_LEVEL(bp
) : ZB_TOTAL
;
1452 int t
= (i
& 1) ? type
: DMU_OT_TOTAL
;
1453 zdb_blkstats_t
*zb
= &zcb
->zcb_type
[l
][t
];
1455 zb
->zb_asize
+= BP_GET_ASIZE(bp
);
1456 zb
->zb_lsize
+= BP_GET_LSIZE(bp
);
1457 zb
->zb_psize
+= BP_GET_PSIZE(bp
);
1461 if (dump_opt
['S']) {
1462 boolean_t print_sig
;
1464 print_sig
= !zdb_sig_user_data
|| (BP_GET_LEVEL(bp
) == 0 &&
1465 BP_GET_TYPE(bp
) == DMU_OT_PLAIN_FILE_CONTENTS
);
1467 if (BP_GET_CHECKSUM(bp
) < zdb_sig_cksumalg
)
1468 print_sig
= B_FALSE
;
1471 (void) printf("%llu\t%lld\t%lld\t%s\t%s\t%s\t"
1472 "%llx:%llx:%llx:%llx\n",
1473 (u_longlong_t
)BP_GET_LEVEL(bp
),
1474 (longlong_t
)BP_GET_PSIZE(bp
),
1475 (longlong_t
)BP_GET_NDVAS(bp
),
1476 dmu_ot
[BP_GET_TYPE(bp
)].ot_name
,
1477 zio_checksum_table
[BP_GET_CHECKSUM(bp
)].ci_name
,
1478 zio_compress_table
[BP_GET_COMPRESS(bp
)].ci_name
,
1479 (u_longlong_t
)bp
->blk_cksum
.zc_word
[0],
1480 (u_longlong_t
)bp
->blk_cksum
.zc_word
[1],
1481 (u_longlong_t
)bp
->blk_cksum
.zc_word
[2],
1482 (u_longlong_t
)bp
->blk_cksum
.zc_word
[3]);
1487 VERIFY(zio_wait(zio_claim(NULL
, spa
, spa_first_txg(spa
), bp
,
1488 NULL
, NULL
, ZIO_FLAG_MUSTSUCCEED
)) == 0);
1492 zdb_blkptr_cb(spa_t
*spa
, blkptr_t
*bp
, const zbookmark_t
*zb
,
1493 const dnode_phys_t
*dnp
, void *arg
)
1495 zdb_cb_t
*zcb
= arg
;
1496 char blkbuf
[BP_SPRINTF_LEN
];
1501 zdb_count_block(spa
, zcb
, bp
, BP_GET_TYPE(bp
));
1503 if (dump_opt
['c'] || dump_opt
['S']) {
1507 size
= BP_GET_LSIZE(bp
);
1508 data
= malloc(size
);
1509 ioerr
= zio_wait(zio_read(NULL
, spa
, bp
, data
, size
,
1510 NULL
, NULL
, ZIO_PRIORITY_ASYNC_READ
,
1511 ZIO_FLAG_CANFAIL
| ZIO_FLAG_SCRUB
, zb
));
1514 /* We expect io errors on intent log */
1515 if (ioerr
&& BP_GET_TYPE(bp
) != DMU_OT_INTENT_LOG
) {
1516 zcb
->zcb_haderrors
= 1;
1517 zcb
->zcb_errors
[ioerr
]++;
1519 if (dump_opt
['b'] >= 2)
1520 sprintf_blkptr(blkbuf
, BP_SPRINTF_LEN
, bp
);
1524 if (!dump_opt
['S']) {
1525 (void) printf("zdb_blkptr_cb: "
1526 "Got error %d reading "
1527 "<%llu, %llu, %lld, %llx> %s -- skipping\n",
1529 (u_longlong_t
)zb
->zb_objset
,
1530 (u_longlong_t
)zb
->zb_object
,
1531 (u_longlong_t
)zb
->zb_level
,
1532 (u_longlong_t
)zb
->zb_blkid
,
1538 zcb
->zcb_readfails
= 0;
1540 if (dump_opt
['b'] >= 4) {
1541 sprintf_blkptr(blkbuf
, BP_SPRINTF_LEN
, bp
);
1542 (void) printf("objset %llu object %llu offset 0x%llx %s\n",
1543 (u_longlong_t
)zb
->zb_objset
,
1544 (u_longlong_t
)zb
->zb_object
,
1545 (u_longlong_t
)blkid2offset(dnp
, zb
->zb_level
, zb
->zb_blkid
),
1553 dump_block_stats(spa_t
*spa
)
1555 zdb_cb_t zcb
= { 0 };
1556 zdb_blkstats_t
*zb
, *tzb
;
1557 uint64_t alloc
, space
, logalloc
;
1558 vdev_t
*rvd
= spa
->spa_root_vdev
;
1562 if (!dump_opt
['S']) {
1563 (void) printf("\nTraversing all blocks %s%s%s%s...\n",
1564 (dump_opt
['c'] || !dump_opt
['L']) ? "to verify " : "",
1565 dump_opt
['c'] ? "checksums " : "",
1566 (dump_opt
['c'] && !dump_opt
['L']) ? "and verify " : "",
1567 !dump_opt
['L'] ? "nothing leaked " : "");
1571 * Load all space maps as SM_ALLOC maps, then traverse the pool
1572 * claiming each block we discover. If the pool is perfectly
1573 * consistent, the space maps will be empty when we're done.
1574 * Anything left over is a leak; any block we can't claim (because
1575 * it's not part of any space map) is a double allocation,
1576 * reference to a freed block, or an unclaimed log block.
1582 * If there's a deferred-free bplist, process that first.
1584 if (spa
->spa_sync_bplist_obj
!= 0) {
1585 bplist_t
*bpl
= &spa
->spa_sync_bplist
;
1589 VERIFY(0 == bplist_open(bpl
, spa
->spa_meta_objset
,
1590 spa
->spa_sync_bplist_obj
));
1592 while (bplist_iterate(bpl
, &itor
, &blk
) == 0) {
1593 if (dump_opt
['b'] >= 4) {
1594 char blkbuf
[BP_SPRINTF_LEN
];
1595 sprintf_blkptr(blkbuf
, BP_SPRINTF_LEN
, &blk
);
1596 (void) printf("[%s] %s\n",
1597 "deferred free", blkbuf
);
1599 zdb_count_block(spa
, &zcb
, &blk
, DMU_OT_DEFERRED
);
1605 zcb
.zcb_haderrors
|= traverse_pool(spa
, zdb_blkptr_cb
, &zcb
);
1607 if (zcb
.zcb_haderrors
&& !dump_opt
['S']) {
1608 (void) printf("\nError counts:\n\n");
1609 (void) printf("\t%5s %s\n", "errno", "count");
1610 for (e
= 0; e
< 256; e
++) {
1611 if (zcb
.zcb_errors
[e
] != 0) {
1612 (void) printf("\t%5d %llu\n",
1613 e
, (u_longlong_t
)zcb
.zcb_errors
[e
]);
1619 * Report any leaked segments.
1625 * If we're interested in printing out the blkptr signatures,
1626 * return now as we don't print out anything else (including
1627 * errors and leaks).
1630 return (zcb
.zcb_haderrors
? 3 : 0);
1632 alloc
= spa_get_alloc(spa
);
1633 space
= spa_get_space(spa
);
1636 * Log blocks allocated from a separate log device don't count
1637 * as part of the normal pool space; factor them in here.
1641 for (c
= 0; c
< rvd
->vdev_children
; c
++)
1642 if (rvd
->vdev_child
[c
]->vdev_islog
)
1643 logalloc
+= rvd
->vdev_child
[c
]->vdev_stat
.vs_alloc
;
1645 tzb
= &zcb
.zcb_type
[ZB_TOTAL
][DMU_OT_TOTAL
];
1647 if (tzb
->zb_asize
== alloc
+ logalloc
) {
1649 (void) printf("\n\tNo leaks (block sum matches space"
1650 " maps exactly)\n");
1652 (void) printf("block traversal size %llu != alloc %llu "
1654 (u_longlong_t
)tzb
->zb_asize
,
1655 (u_longlong_t
)alloc
+ logalloc
,
1656 (dump_opt
['L']) ? "unreachable" : "leaked",
1657 (longlong_t
)(alloc
+ logalloc
- tzb
->zb_asize
));
1661 if (tzb
->zb_count
== 0)
1664 (void) printf("\n");
1665 (void) printf("\tbp count: %10llu\n",
1666 (u_longlong_t
)tzb
->zb_count
);
1667 (void) printf("\tbp logical: %10llu\t avg: %6llu\n",
1668 (u_longlong_t
)tzb
->zb_lsize
,
1669 (u_longlong_t
)(tzb
->zb_lsize
/ tzb
->zb_count
));
1670 (void) printf("\tbp physical: %10llu\t avg:"
1671 " %6llu\tcompression: %6.2f\n",
1672 (u_longlong_t
)tzb
->zb_psize
,
1673 (u_longlong_t
)(tzb
->zb_psize
/ tzb
->zb_count
),
1674 (double)tzb
->zb_lsize
/ tzb
->zb_psize
);
1675 (void) printf("\tbp allocated: %10llu\t avg:"
1676 " %6llu\tcompression: %6.2f\n",
1677 (u_longlong_t
)tzb
->zb_asize
,
1678 (u_longlong_t
)(tzb
->zb_asize
/ tzb
->zb_count
),
1679 (double)tzb
->zb_lsize
/ tzb
->zb_asize
);
1680 (void) printf("\tSPA allocated: %10llu\tused: %5.2f%%\n",
1681 (u_longlong_t
)alloc
, 100.0 * alloc
/ space
);
1683 if (dump_opt
['b'] >= 2) {
1685 (void) printf("\nBlocks\tLSIZE\tPSIZE\tASIZE"
1686 "\t avg\t comp\t%%Total\tType\n");
1688 for (t
= 0; t
<= DMU_OT_NUMTYPES
; t
++) {
1689 char csize
[6], lsize
[6], psize
[6], asize
[6], avg
[6];
1692 typename
= t
== DMU_OT_DEFERRED
? "deferred free" :
1693 t
== DMU_OT_TOTAL
? "Total" : dmu_ot
[t
].ot_name
;
1695 if (zcb
.zcb_type
[ZB_TOTAL
][t
].zb_asize
== 0) {
1696 (void) printf("%6s\t%5s\t%5s\t%5s"
1697 "\t%5s\t%5s\t%6s\t%s\n",
1709 for (l
= ZB_TOTAL
- 1; l
>= -1; l
--) {
1710 level
= (l
== -1 ? ZB_TOTAL
: l
);
1711 zb
= &zcb
.zcb_type
[level
][t
];
1713 if (zb
->zb_asize
== 0)
1716 if (dump_opt
['b'] < 3 && level
!= ZB_TOTAL
)
1719 if (level
== 0 && zb
->zb_asize
==
1720 zcb
.zcb_type
[ZB_TOTAL
][t
].zb_asize
)
1723 nicenum(zb
->zb_count
, csize
);
1724 nicenum(zb
->zb_lsize
, lsize
);
1725 nicenum(zb
->zb_psize
, psize
);
1726 nicenum(zb
->zb_asize
, asize
);
1727 nicenum(zb
->zb_asize
/ zb
->zb_count
, avg
);
1729 (void) printf("%6s\t%5s\t%5s\t%5s\t%5s"
1731 csize
, lsize
, psize
, asize
, avg
,
1732 (double)zb
->zb_lsize
/ zb
->zb_psize
,
1733 100.0 * zb
->zb_asize
/ tzb
->zb_asize
);
1735 if (level
== ZB_TOTAL
)
1736 (void) printf("%s\n", typename
);
1738 (void) printf(" L%d %s\n",
1744 (void) printf("\n");
1749 if (zcb
.zcb_haderrors
)
1756 dump_zpool(spa_t
*spa
)
1758 dsl_pool_t
*dp
= spa_get_dsl(spa
);
1762 dump_uberblock(&spa
->spa_uberblock
);
1764 if (dump_opt
['d'] || dump_opt
['i']) {
1765 dump_dir(dp
->dp_meta_objset
);
1766 if (dump_opt
['d'] >= 3) {
1767 dump_bplist(dp
->dp_meta_objset
,
1768 spa
->spa_sync_bplist_obj
, "Deferred frees");
1769 dump_dtl(spa
->spa_root_vdev
, 0);
1770 dump_metaslabs(spa
);
1772 (void) dmu_objset_find(spa_name(spa
), dump_one_dir
, NULL
,
1773 DS_FIND_SNAPSHOTS
| DS_FIND_CHILDREN
);
1776 if (dump_opt
['b'] || dump_opt
['c'] || dump_opt
['S'])
1777 rc
= dump_block_stats(spa
);
1780 show_pool_stats(spa
);
1786 #define ZDB_FLAG_CHECKSUM 0x0001
1787 #define ZDB_FLAG_DECOMPRESS 0x0002
1788 #define ZDB_FLAG_BSWAP 0x0004
1789 #define ZDB_FLAG_GBH 0x0008
1790 #define ZDB_FLAG_INDIRECT 0x0010
1791 #define ZDB_FLAG_PHYS 0x0020
1792 #define ZDB_FLAG_RAW 0x0040
1793 #define ZDB_FLAG_PRINT_BLKPTR 0x0080
1798 zdb_print_blkptr(blkptr_t
*bp
, int flags
)
1800 dva_t
*dva
= bp
->blk_dva
;
1803 if (flags
& ZDB_FLAG_BSWAP
)
1804 byteswap_uint64_array((void *)bp
, sizeof (blkptr_t
));
1806 * Super-ick warning: This code is also duplicated in
1807 * cmd/mdb/common/modules/zfs/zfs.c . Yeah, I hate code
1810 for (d
= 0; d
< BP_GET_NDVAS(bp
); d
++) {
1811 (void) printf("\tDVA[%d]: vdev_id %lld / %llx\n", d
,
1812 (longlong_t
)DVA_GET_VDEV(&dva
[d
]),
1813 (longlong_t
)DVA_GET_OFFSET(&dva
[d
]));
1814 (void) printf("\tDVA[%d]: GANG: %-5s GRID: %04llx\t"
1816 DVA_GET_GANG(&dva
[d
]) ? "TRUE" : "FALSE",
1817 (longlong_t
)DVA_GET_GRID(&dva
[d
]),
1818 (longlong_t
)DVA_GET_ASIZE(&dva
[d
]));
1819 (void) printf("\tDVA[%d]: :%llu:%llx:%llx:%s%s%s%s\n", d
,
1820 (u_longlong_t
)DVA_GET_VDEV(&dva
[d
]),
1821 (longlong_t
)DVA_GET_OFFSET(&dva
[d
]),
1822 (longlong_t
)BP_GET_PSIZE(bp
),
1823 BP_SHOULD_BYTESWAP(bp
) ? "e" : "",
1824 !DVA_GET_GANG(&dva
[d
]) && BP_GET_LEVEL(bp
) != 0 ?
1826 DVA_GET_GANG(&dva
[d
]) ? "g" : "",
1827 BP_GET_COMPRESS(bp
) != 0 ? "d" : "");
1829 (void) printf("\tLSIZE: %-16llx\t\tPSIZE: %llx\n",
1830 (longlong_t
)BP_GET_LSIZE(bp
), (longlong_t
)BP_GET_PSIZE(bp
));
1831 (void) printf("\tENDIAN: %6s\t\t\t\t\tTYPE: %s\n",
1832 BP_GET_BYTEORDER(bp
) ? "LITTLE" : "BIG",
1833 dmu_ot
[BP_GET_TYPE(bp
)].ot_name
);
1834 (void) printf("\tBIRTH: %-16llx LEVEL: %-2llu\tFILL: %llx\n",
1835 (u_longlong_t
)bp
->blk_birth
, (u_longlong_t
)BP_GET_LEVEL(bp
),
1836 (u_longlong_t
)bp
->blk_fill
);
1837 (void) printf("\tCKFUNC: %-16s\t\tCOMP: %s\n",
1838 zio_checksum_table
[BP_GET_CHECKSUM(bp
)].ci_name
,
1839 zio_compress_table
[BP_GET_COMPRESS(bp
)].ci_name
);
1840 (void) printf("\tCKSUM: %llx:%llx:%llx:%llx\n",
1841 (u_longlong_t
)bp
->blk_cksum
.zc_word
[0],
1842 (u_longlong_t
)bp
->blk_cksum
.zc_word
[1],
1843 (u_longlong_t
)bp
->blk_cksum
.zc_word
[2],
1844 (u_longlong_t
)bp
->blk_cksum
.zc_word
[3]);
1848 zdb_dump_indirect(blkptr_t
*bp
, int nbps
, int flags
)
1852 for (i
= 0; i
< nbps
; i
++)
1853 zdb_print_blkptr(&bp
[i
], flags
);
1857 zdb_dump_gbh(void *buf
, int flags
)
1859 zdb_dump_indirect((blkptr_t
*)buf
, SPA_GBH_NBLKPTRS
, flags
);
1863 zdb_dump_block_raw(void *buf
, uint64_t size
, int flags
)
1865 if (flags
& ZDB_FLAG_BSWAP
)
1866 byteswap_uint64_array(buf
, size
);
1867 (void) write(2, buf
, size
);
1871 zdb_dump_block(char *label
, void *buf
, uint64_t size
, int flags
)
1873 uint64_t *d
= (uint64_t *)buf
;
1874 int nwords
= size
/ sizeof (uint64_t);
1875 int do_bswap
= !!(flags
& ZDB_FLAG_BSWAP
);
1881 hdr
= " 7 6 5 4 3 2 1 0 f e d c b a 9 8";
1883 hdr
= " 0 1 2 3 4 5 6 7 8 9 a b c d e f";
1885 (void) printf("\n%s\n%6s %s 0123456789abcdef\n", label
, "", hdr
);
1887 for (i
= 0; i
< nwords
; i
+= 2) {
1888 (void) printf("%06llx: %016llx %016llx ",
1889 (u_longlong_t
)(i
* sizeof (uint64_t)),
1890 (u_longlong_t
)(do_bswap
? BSWAP_64(d
[i
]) : d
[i
]),
1891 (u_longlong_t
)(do_bswap
? BSWAP_64(d
[i
+ 1]) : d
[i
+ 1]));
1894 for (j
= 0; j
< 2 * sizeof (uint64_t); j
++)
1895 (void) printf("%c", isprint(c
[j
]) ? c
[j
] : '.');
1896 (void) printf("\n");
1901 * There are two acceptable formats:
1902 * leaf_name - For example: c1t0d0 or /tmp/ztest.0a
1903 * child[.child]* - For example: 0.1.1
1905 * The second form can be used to specify arbitrary vdevs anywhere
1906 * in the heirarchy. For example, in a pool with a mirror of
1907 * RAID-Zs, you can specify either RAID-Z vdev with 0.0 or 0.1 .
1910 zdb_vdev_lookup(vdev_t
*vdev
, char *path
)
1918 /* First, assume the x.x.x.x format */
1919 i
= (int)strtoul(path
, &s
, 10);
1920 if (s
== path
|| (s
&& *s
!= '.' && *s
!= '\0'))
1922 if (i
< 0 || i
>= vdev
->vdev_children
)
1925 vdev
= vdev
->vdev_child
[i
];
1928 return (zdb_vdev_lookup(vdev
, s
+1));
1931 for (i
= 0; i
< vdev
->vdev_children
; i
++) {
1932 vdev_t
*vc
= vdev
->vdev_child
[i
];
1934 if (vc
->vdev_path
== NULL
) {
1935 vc
= zdb_vdev_lookup(vc
, path
);
1942 p
= strrchr(vc
->vdev_path
, '/');
1943 p
= p
? p
+ 1 : vc
->vdev_path
;
1944 q
= &vc
->vdev_path
[strlen(vc
->vdev_path
) - 2];
1946 if (strcmp(vc
->vdev_path
, path
) == 0)
1948 if (strcmp(p
, path
) == 0)
1950 if (strcmp(q
, "s0") == 0 && strncmp(p
, path
, q
- p
) == 0)
1958 * Read a block from a pool and print it out. The syntax of the
1959 * block descriptor is:
1961 * pool:vdev_specifier:offset:size[:flags]
1963 * pool - The name of the pool you wish to read from
1964 * vdev_specifier - Which vdev (see comment for zdb_vdev_lookup)
1965 * offset - offset, in hex, in bytes
1966 * size - Amount of data to read, in hex, in bytes
1967 * flags - A string of characters specifying options
1968 * b: Decode a blkptr at given offset within block
1969 * *c: Calculate and display checksums
1970 * *d: Decompress data before dumping
1971 * e: Byteswap data before dumping
1972 * *g: Display data as a gang block header
1973 * *i: Display as an indirect block
1974 * p: Do I/O to physical offset
1975 * r: Dump raw data to stdout
1977 * * = not yet implemented
1980 zdb_read_block(char *thing
, spa_t
**spap
)
1984 uint64_t offset
= 0, size
= 0, blkptr_offset
= 0;
1988 char *s
, *p
, *dup
, *pool
, *vdev
, *flagstr
;
1989 int i
, error
, zio_flags
;
1991 dup
= strdup(thing
);
1992 s
= strtok(dup
, ":");
1994 s
= strtok(NULL
, ":");
1996 s
= strtok(NULL
, ":");
1997 offset
= strtoull(s
? s
: "", NULL
, 16);
1998 s
= strtok(NULL
, ":");
1999 size
= strtoull(s
? s
: "", NULL
, 16);
2000 s
= strtok(NULL
, ":");
2001 flagstr
= s
? s
: "";
2005 s
= "size must not be zero";
2006 if (!IS_P2ALIGNED(size
, DEV_BSIZE
))
2007 s
= "size must be a multiple of sector size";
2008 if (!IS_P2ALIGNED(offset
, DEV_BSIZE
))
2009 s
= "offset must be a multiple of sector size";
2011 (void) printf("Invalid block specifier: %s - %s\n", thing
, s
);
2016 for (s
= strtok(flagstr
, ":"); s
; s
= strtok(NULL
, ":")) {
2017 for (i
= 0; flagstr
[i
]; i
++) {
2018 int bit
= flagbits
[(uchar_t
)flagstr
[i
]];
2021 (void) printf("***Invalid flag: %c\n",
2027 /* If it's not something with an argument, keep going */
2028 if ((bit
& (ZDB_FLAG_CHECKSUM
| ZDB_FLAG_DECOMPRESS
|
2029 ZDB_FLAG_PRINT_BLKPTR
)) == 0)
2032 p
= &flagstr
[i
+ 1];
2033 if (bit
== ZDB_FLAG_PRINT_BLKPTR
)
2034 blkptr_offset
= strtoull(p
, &p
, 16);
2035 if (*p
!= ':' && *p
!= '\0') {
2036 (void) printf("***Invalid flag arg: '%s'\n", s
);
2043 if (spa
== NULL
|| strcmp(spa_name(spa
), pool
) != 0) {
2045 spa_close(spa
, (void *)zdb_read_block
);
2046 error
= spa_open(pool
, spap
, (void *)zdb_read_block
);
2048 fatal("Failed to open pool '%s': %s",
2049 pool
, strerror(error
));
2053 vd
= zdb_vdev_lookup(spa
->spa_root_vdev
, vdev
);
2055 (void) printf("***Invalid vdev: %s\n", vdev
);
2060 (void) printf("Found vdev: %s\n", vd
->vdev_path
);
2062 (void) printf("Found vdev type: %s\n",
2063 vd
->vdev_ops
->vdev_op_type
);
2066 buf
= umem_alloc(size
, UMEM_NOFAIL
);
2068 zio_flags
= ZIO_FLAG_DONT_CACHE
| ZIO_FLAG_DONT_QUEUE
|
2069 ZIO_FLAG_DONT_PROPAGATE
| ZIO_FLAG_DONT_RETRY
;
2071 spa_config_enter(spa
, SCL_STATE
, FTAG
, RW_READER
);
2072 zio
= zio_root(spa
, NULL
, NULL
, 0);
2073 /* XXX todo - cons up a BP so RAID-Z will be happy */
2074 zio_nowait(zio_vdev_child_io(zio
, NULL
, vd
, offset
, buf
, size
,
2075 ZIO_TYPE_READ
, ZIO_PRIORITY_SYNC_READ
, zio_flags
, NULL
, NULL
));
2076 error
= zio_wait(zio
);
2077 spa_config_exit(spa
, SCL_STATE
, FTAG
);
2080 (void) printf("Read of %s failed, error: %d\n", thing
, error
);
2084 if (flags
& ZDB_FLAG_PRINT_BLKPTR
)
2085 zdb_print_blkptr((blkptr_t
*)(void *)
2086 ((uintptr_t)buf
+ (uintptr_t)blkptr_offset
), flags
);
2087 else if (flags
& ZDB_FLAG_RAW
)
2088 zdb_dump_block_raw(buf
, size
, flags
);
2089 else if (flags
& ZDB_FLAG_INDIRECT
)
2090 zdb_dump_indirect((blkptr_t
*)buf
, size
/ sizeof (blkptr_t
),
2092 else if (flags
& ZDB_FLAG_GBH
)
2093 zdb_dump_gbh(buf
, flags
);
2095 zdb_dump_block(thing
, buf
, size
, flags
);
2098 umem_free(buf
, size
);
2103 nvlist_string_match(nvlist_t
*config
, char *name
, char *tgt
)
2107 if (nvlist_lookup_string(config
, name
, &s
) != 0)
2110 return (strcmp(s
, tgt
) == 0);
2114 nvlist_uint64_match(nvlist_t
*config
, char *name
, uint64_t tgt
)
2118 if (nvlist_lookup_uint64(config
, name
, &val
) != 0)
2121 return (val
== tgt
);
2125 vdev_child_guid_match(nvlist_t
*vdev
, uint64_t guid
)
2130 verify(nvlist_lookup_nvlist_array(vdev
, ZPOOL_CONFIG_CHILDREN
,
2131 &child
, &children
) == 0);
2132 for (c
= 0; c
< children
; ++c
)
2133 if (nvlist_uint64_match(child
[c
], ZPOOL_CONFIG_GUID
, guid
))
2139 vdev_child_string_match(nvlist_t
*vdev
, char *tgt
)
2144 verify(nvlist_lookup_nvlist_array(vdev
, ZPOOL_CONFIG_CHILDREN
,
2145 &child
, &children
) == 0);
2146 for (c
= 0; c
< children
; ++c
) {
2147 if (nvlist_string_match(child
[c
], ZPOOL_CONFIG_PATH
, tgt
) ||
2148 nvlist_string_match(child
[c
], ZPOOL_CONFIG_DEVID
, tgt
))
2155 vdev_guid_match(nvlist_t
*config
, uint64_t guid
)
2159 verify(nvlist_lookup_nvlist(config
, ZPOOL_CONFIG_VDEV_TREE
,
2162 return (nvlist_uint64_match(nvroot
, ZPOOL_CONFIG_GUID
, guid
) ||
2163 vdev_child_guid_match(nvroot
, guid
));
2167 vdev_string_match(nvlist_t
*config
, char *tgt
)
2171 verify(nvlist_lookup_nvlist(config
, ZPOOL_CONFIG_VDEV_TREE
,
2174 return (vdev_child_string_match(nvroot
, tgt
));
2178 pool_match(nvlist_t
*config
, char *tgt
)
2180 uint64_t guid
= strtoull(tgt
, NULL
, 0);
2184 nvlist_uint64_match(config
, ZPOOL_CONFIG_POOL_GUID
, guid
) ||
2185 vdev_guid_match(config
, guid
));
2188 nvlist_string_match(config
, ZPOOL_CONFIG_POOL_NAME
, tgt
) ||
2189 vdev_string_match(config
, tgt
));
2194 find_exported_zpool(char *pool_id
, nvlist_t
**configp
, char *vdev_dir
)
2198 nvlist_t
*match
= NULL
;
2200 if (vdev_dir
!= NULL
)
2201 pools
= zpool_find_import_activeok(g_zfs
, 1, &vdev_dir
);
2203 pools
= zpool_find_import_activeok(g_zfs
, 0, NULL
);
2205 if (pools
!= NULL
) {
2206 nvpair_t
*elem
= NULL
;
2208 while ((elem
= nvlist_next_nvpair(pools
, elem
)) != NULL
) {
2209 verify(nvpair_value_nvlist(elem
, configp
) == 0);
2210 if (pool_match(*configp
, pool_id
)) {
2211 if (match
!= NULL
) {
2213 "More than one matching pool - "
2214 "specify guid/devid/device path.");
2223 *configp
= error
? NULL
: match
;
2229 main(int argc
, char **argv
)
2232 struct rlimit rl
= { 1024, 1024 };
2234 objset_t
*os
= NULL
;
2240 char *vdev_dir
= NULL
;
2242 (void) setrlimit(RLIMIT_NOFILE
, &rl
);
2243 (void) enable_extended_FILE_stdio(-1, -1);
2245 dprintf_setup(&argc
, argv
);
2247 while ((c
= getopt(argc
, argv
, "udibcsvCLS:U:lRep:")) != -1) {
2268 spa_config_path
= optarg
;
2279 zdb_sig_user_data
= (strncmp(optarg
, "user:", 5) == 0);
2280 if (!zdb_sig_user_data
&& strncmp(optarg
, "all:", 4))
2282 endstr
= strchr(optarg
, ':') + 1;
2283 if (strcmp(endstr
, "fletcher2") == 0)
2284 zdb_sig_cksumalg
= ZIO_CHECKSUM_FLETCHER_2
;
2285 else if (strcmp(endstr
, "fletcher4") == 0)
2286 zdb_sig_cksumalg
= ZIO_CHECKSUM_FLETCHER_4
;
2287 else if (strcmp(endstr
, "sha256") == 0)
2288 zdb_sig_cksumalg
= ZIO_CHECKSUM_SHA256
;
2289 else if (strcmp(endstr
, "all") == 0)
2290 zdb_sig_cksumalg
= ZIO_CHECKSUM_FLETCHER_2
;
2300 if (vdev_dir
!= NULL
&& exported
== 0) {
2301 (void) fprintf(stderr
, "-p option requires use of -e\n");
2306 g_zfs
= libzfs_init();
2307 ASSERT(g_zfs
!= NULL
);
2309 for (c
= 0; c
< 256; c
++) {
2310 if (dump_all
&& c
!= 'l' && c
!= 'R')
2313 dump_opt
[c
] += verbose
;
2320 if (dump_opt
['C']) {
2321 dump_cachefile(spa_config_path
);
2327 if (dump_opt
['l']) {
2328 dump_label(argv
[0]);
2332 if (dump_opt
['R']) {
2333 flagbits
['b'] = ZDB_FLAG_PRINT_BLKPTR
;
2334 flagbits
['c'] = ZDB_FLAG_CHECKSUM
;
2335 flagbits
['d'] = ZDB_FLAG_DECOMPRESS
;
2336 flagbits
['e'] = ZDB_FLAG_BSWAP
;
2337 flagbits
['g'] = ZDB_FLAG_GBH
;
2338 flagbits
['i'] = ZDB_FLAG_INDIRECT
;
2339 flagbits
['p'] = ZDB_FLAG_PHYS
;
2340 flagbits
['r'] = ZDB_FLAG_RAW
;
2344 zdb_read_block(argv
[0], &spa
);
2349 spa_close(spa
, (void *)zdb_read_block
);
2354 dump_config(argv
[0]);
2359 * Check to see if the name refers to an exported zpool
2362 nvlist_t
*exported_conf
= NULL
;
2364 if ((slash
= strchr(argv
[0], '/')) != NULL
)
2367 error
= find_exported_zpool(argv
[0], &exported_conf
, vdev_dir
);
2369 nvlist_t
*nvl
= NULL
;
2371 if (vdev_dir
!= NULL
) {
2372 if (nvlist_alloc(&nvl
, NV_UNIQUE_NAME
, 0) != 0)
2374 else if (nvlist_add_string(nvl
,
2375 zpool_prop_to_name(ZPOOL_PROP_ALTROOT
),
2381 error
= spa_import_faulted(argv
[0],
2382 exported_conf
, nvl
);
2392 if (strchr(argv
[0], '/') != NULL
) {
2393 error
= dmu_objset_open(argv
[0], DMU_OST_ANY
,
2394 DS_MODE_USER
| DS_MODE_READONLY
, &os
);
2396 error
= spa_open(argv
[0], &spa
, FTAG
);
2401 fatal("can't open %s: %s", argv
[0], strerror(error
));
2405 zopt_objects
= argc
;
2406 zopt_object
= calloc(zopt_objects
, sizeof (uint64_t));
2407 for (i
= 0; i
< zopt_objects
; i
++) {
2409 zopt_object
[i
] = strtoull(argv
[i
], NULL
, 0);
2410 if (zopt_object
[i
] == 0 && errno
!= 0)
2411 fatal("bad object number %s: %s",
2412 argv
[i
], strerror(errno
));
2418 dmu_objset_close(os
);
2421 spa_close(spa
, FTAG
);
2424 fuid_table_destroy();