1 /* 2 * CDDL HEADER START 3 * 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. 7 * 8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 9 * or https://opensource.org/licenses/CDDL-1.0. 10 * See the License for the specific language governing permissions 11 * and limitations under the License. 12 * 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] 18 * 19 * CDDL HEADER END 20 */ 21 22 /* 23 * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved. 24 * Copyright (c) 2011, 2019 by Delphix. All rights reserved. 25 * Copyright (c) 2014 Integros [integros.com] 26 * Copyright 2016 Nexenta Systems, Inc. 27 * Copyright (c) 2017, 2018 Lawrence Livermore National Security, LLC. 28 * Copyright (c) 2015, 2017, Intel Corporation. 29 * Copyright (c) 2020 Datto Inc. 30 * Copyright (c) 2020, The FreeBSD Foundation [1] 31 * 32 * [1] Portions of this software were developed by Allan Jude 33 * under sponsorship from the FreeBSD Foundation. 34 * Copyright (c) 2021 Allan Jude 35 * Copyright (c) 2021 Toomas Soome <tsoome@me.com> 36 */ 37 38 #include <stdio.h> 39 #include <unistd.h> 40 #include <stdlib.h> 41 #include <ctype.h> 42 #include <getopt.h> 43 #include <sys/zfs_context.h> 44 #include <sys/spa.h> 45 #include <sys/spa_impl.h> 46 #include <sys/dmu.h> 47 #include <sys/zap.h> 48 #include <sys/fs/zfs.h> 49 #include <sys/zfs_znode.h> 50 #include <sys/zfs_sa.h> 51 #include <sys/sa.h> 52 #include <sys/sa_impl.h> 53 #include <sys/vdev.h> 54 #include <sys/vdev_impl.h> 55 #include <sys/metaslab_impl.h> 56 #include <sys/dmu_objset.h> 57 #include <sys/dsl_dir.h> 58 #include <sys/dsl_dataset.h> 59 #include <sys/dsl_pool.h> 60 #include <sys/dsl_bookmark.h> 61 #include <sys/dbuf.h> 62 #include <sys/zil.h> 63 #include <sys/zil_impl.h> 64 #include <sys/stat.h> 65 #include <sys/resource.h> 66 #include <sys/dmu_send.h> 67 #include <sys/dmu_traverse.h> 68 #include <sys/zio_checksum.h> 69 #include <sys/zio_compress.h> 70 #include <sys/zfs_fuid.h> 71 #include <sys/arc.h> 72 #include <sys/arc_impl.h> 73 #include <sys/ddt.h> 74 #include <sys/zfeature.h> 75 #include <sys/abd.h> 76 #include <sys/blkptr.h> 77 #include <sys/dsl_crypt.h> 78 #include <sys/dsl_scan.h> 79 #include <sys/btree.h> 80 #include <zfs_comutil.h> 81 #include <sys/zstd/zstd.h> 82 83 #include <libnvpair.h> 84 #include <libzutil.h> 85 86 #include "zdb.h" 87 88 #define ZDB_COMPRESS_NAME(idx) ((idx) < ZIO_COMPRESS_FUNCTIONS ? \ 89 zio_compress_table[(idx)].ci_name : "UNKNOWN") 90 #define ZDB_CHECKSUM_NAME(idx) ((idx) < ZIO_CHECKSUM_FUNCTIONS ? \ 91 zio_checksum_table[(idx)].ci_name : "UNKNOWN") 92 #define ZDB_OT_TYPE(idx) ((idx) < DMU_OT_NUMTYPES ? (idx) : \ 93 (idx) == DMU_OTN_ZAP_DATA || (idx) == DMU_OTN_ZAP_METADATA ? \ 94 DMU_OT_ZAP_OTHER : \ 95 (idx) == DMU_OTN_UINT64_DATA || (idx) == DMU_OTN_UINT64_METADATA ? \ 96 DMU_OT_UINT64_OTHER : DMU_OT_NUMTYPES) 97 98 /* Some platforms require part of inode IDs to be remapped */ 99 #ifdef __APPLE__ 100 #define ZDB_MAP_OBJECT_ID(obj) INO_XNUTOZFS(obj, 2) 101 #else 102 #define ZDB_MAP_OBJECT_ID(obj) (obj) 103 #endif 104 105 static const char * 106 zdb_ot_name(dmu_object_type_t type) 107 { 108 if (type < DMU_OT_NUMTYPES) 109 return (dmu_ot[type].ot_name); 110 else if ((type & DMU_OT_NEWTYPE) && 111 ((type & DMU_OT_BYTESWAP_MASK) < DMU_BSWAP_NUMFUNCS)) 112 return (dmu_ot_byteswap[type & DMU_OT_BYTESWAP_MASK].ob_name); 113 else 114 return ("UNKNOWN"); 115 } 116 117 extern int reference_tracking_enable; 118 extern int zfs_recover; 119 extern unsigned long zfs_arc_meta_min, zfs_arc_meta_limit; 120 extern uint_t zfs_vdev_async_read_max_active; 121 extern boolean_t spa_load_verify_dryrun; 122 extern boolean_t spa_mode_readable_spacemaps; 123 extern uint_t zfs_reconstruct_indirect_combinations_max; 124 extern uint_t zfs_btree_verify_intensity; 125 126 static const char cmdname[] = "zdb"; 127 uint8_t dump_opt[256]; 128 129 typedef void object_viewer_t(objset_t *, uint64_t, void *data, size_t size); 130 131 uint64_t *zopt_metaslab = NULL; 132 static unsigned zopt_metaslab_args = 0; 133 134 typedef struct zopt_object_range { 135 uint64_t zor_obj_start; 136 uint64_t zor_obj_end; 137 uint64_t zor_flags; 138 } zopt_object_range_t; 139 zopt_object_range_t *zopt_object_ranges = NULL; 140 static unsigned zopt_object_args = 0; 141 142 static int flagbits[256]; 143 144 #define ZOR_FLAG_PLAIN_FILE 0x0001 145 #define ZOR_FLAG_DIRECTORY 0x0002 146 #define ZOR_FLAG_SPACE_MAP 0x0004 147 #define ZOR_FLAG_ZAP 0x0008 148 #define ZOR_FLAG_ALL_TYPES -1 149 #define ZOR_SUPPORTED_FLAGS (ZOR_FLAG_PLAIN_FILE | \ 150 ZOR_FLAG_DIRECTORY | \ 151 ZOR_FLAG_SPACE_MAP | \ 152 ZOR_FLAG_ZAP) 153 154 #define ZDB_FLAG_CHECKSUM 0x0001 155 #define ZDB_FLAG_DECOMPRESS 0x0002 156 #define ZDB_FLAG_BSWAP 0x0004 157 #define ZDB_FLAG_GBH 0x0008 158 #define ZDB_FLAG_INDIRECT 0x0010 159 #define ZDB_FLAG_RAW 0x0020 160 #define ZDB_FLAG_PRINT_BLKPTR 0x0040 161 #define ZDB_FLAG_VERBOSE 0x0080 162 163 uint64_t max_inflight_bytes = 256 * 1024 * 1024; /* 256MB */ 164 static int leaked_objects = 0; 165 static range_tree_t *mos_refd_objs; 166 167 static void snprintf_blkptr_compact(char *, size_t, const blkptr_t *, 168 boolean_t); 169 static void mos_obj_refd(uint64_t); 170 static void mos_obj_refd_multiple(uint64_t); 171 static int dump_bpobj_cb(void *arg, const blkptr_t *bp, boolean_t free, 172 dmu_tx_t *tx); 173 174 typedef struct sublivelist_verify { 175 /* FREE's that haven't yet matched to an ALLOC, in one sub-livelist */ 176 zfs_btree_t sv_pair; 177 178 /* ALLOC's without a matching FREE, accumulates across sub-livelists */ 179 zfs_btree_t sv_leftover; 180 } sublivelist_verify_t; 181 182 static int 183 livelist_compare(const void *larg, const void *rarg) 184 { 185 const blkptr_t *l = larg; 186 const blkptr_t *r = rarg; 187 188 /* Sort them according to dva[0] */ 189 uint64_t l_dva0_vdev, r_dva0_vdev; 190 l_dva0_vdev = DVA_GET_VDEV(&l->blk_dva[0]); 191 r_dva0_vdev = DVA_GET_VDEV(&r->blk_dva[0]); 192 if (l_dva0_vdev < r_dva0_vdev) 193 return (-1); 194 else if (l_dva0_vdev > r_dva0_vdev) 195 return (+1); 196 197 /* if vdevs are equal, sort by offsets. */ 198 uint64_t l_dva0_offset; 199 uint64_t r_dva0_offset; 200 l_dva0_offset = DVA_GET_OFFSET(&l->blk_dva[0]); 201 r_dva0_offset = DVA_GET_OFFSET(&r->blk_dva[0]); 202 if (l_dva0_offset < r_dva0_offset) { 203 return (-1); 204 } else if (l_dva0_offset > r_dva0_offset) { 205 return (+1); 206 } 207 208 /* 209 * Since we're storing blkptrs without cancelling FREE/ALLOC pairs, 210 * it's possible the offsets are equal. In that case, sort by txg 211 */ 212 if (l->blk_birth < r->blk_birth) { 213 return (-1); 214 } else if (l->blk_birth > r->blk_birth) { 215 return (+1); 216 } 217 return (0); 218 } 219 220 typedef struct sublivelist_verify_block { 221 dva_t svb_dva; 222 223 /* 224 * We need this to check if the block marked as allocated 225 * in the livelist was freed (and potentially reallocated) 226 * in the metaslab spacemaps at a later TXG. 227 */ 228 uint64_t svb_allocated_txg; 229 } sublivelist_verify_block_t; 230 231 static void zdb_print_blkptr(const blkptr_t *bp, int flags); 232 233 typedef struct sublivelist_verify_block_refcnt { 234 /* block pointer entry in livelist being verified */ 235 blkptr_t svbr_blk; 236 237 /* 238 * Refcount gets incremented to 1 when we encounter the first 239 * FREE entry for the svfbr block pointer and a node for it 240 * is created in our ZDB verification/tracking metadata. 241 * 242 * As we encounter more FREE entries we increment this counter 243 * and similarly decrement it whenever we find the respective 244 * ALLOC entries for this block. 245 * 246 * When the refcount gets to 0 it means that all the FREE and 247 * ALLOC entries of this block have paired up and we no longer 248 * need to track it in our verification logic (e.g. the node 249 * containing this struct in our verification data structure 250 * should be freed). 251 * 252 * [refer to sublivelist_verify_blkptr() for the actual code] 253 */ 254 uint32_t svbr_refcnt; 255 } sublivelist_verify_block_refcnt_t; 256 257 static int 258 sublivelist_block_refcnt_compare(const void *larg, const void *rarg) 259 { 260 const sublivelist_verify_block_refcnt_t *l = larg; 261 const sublivelist_verify_block_refcnt_t *r = rarg; 262 return (livelist_compare(&l->svbr_blk, &r->svbr_blk)); 263 } 264 265 static int 266 sublivelist_verify_blkptr(void *arg, const blkptr_t *bp, boolean_t free, 267 dmu_tx_t *tx) 268 { 269 ASSERT3P(tx, ==, NULL); 270 struct sublivelist_verify *sv = arg; 271 sublivelist_verify_block_refcnt_t current = { 272 .svbr_blk = *bp, 273 274 /* 275 * Start with 1 in case this is the first free entry. 276 * This field is not used for our B-Tree comparisons 277 * anyway. 278 */ 279 .svbr_refcnt = 1, 280 }; 281 282 zfs_btree_index_t where; 283 sublivelist_verify_block_refcnt_t *pair = 284 zfs_btree_find(&sv->sv_pair, ¤t, &where); 285 if (free) { 286 if (pair == NULL) { 287 /* first free entry for this block pointer */ 288 zfs_btree_add(&sv->sv_pair, ¤t); 289 } else { 290 pair->svbr_refcnt++; 291 } 292 } else { 293 if (pair == NULL) { 294 /* block that is currently marked as allocated */ 295 for (int i = 0; i < SPA_DVAS_PER_BP; i++) { 296 if (DVA_IS_EMPTY(&bp->blk_dva[i])) 297 break; 298 sublivelist_verify_block_t svb = { 299 .svb_dva = bp->blk_dva[i], 300 .svb_allocated_txg = bp->blk_birth 301 }; 302 303 if (zfs_btree_find(&sv->sv_leftover, &svb, 304 &where) == NULL) { 305 zfs_btree_add_idx(&sv->sv_leftover, 306 &svb, &where); 307 } 308 } 309 } else { 310 /* alloc matches a free entry */ 311 pair->svbr_refcnt--; 312 if (pair->svbr_refcnt == 0) { 313 /* all allocs and frees have been matched */ 314 zfs_btree_remove_idx(&sv->sv_pair, &where); 315 } 316 } 317 } 318 319 return (0); 320 } 321 322 static int 323 sublivelist_verify_func(void *args, dsl_deadlist_entry_t *dle) 324 { 325 int err; 326 struct sublivelist_verify *sv = args; 327 328 zfs_btree_create(&sv->sv_pair, sublivelist_block_refcnt_compare, 329 sizeof (sublivelist_verify_block_refcnt_t)); 330 331 err = bpobj_iterate_nofree(&dle->dle_bpobj, sublivelist_verify_blkptr, 332 sv, NULL); 333 334 sublivelist_verify_block_refcnt_t *e; 335 zfs_btree_index_t *cookie = NULL; 336 while ((e = zfs_btree_destroy_nodes(&sv->sv_pair, &cookie)) != NULL) { 337 char blkbuf[BP_SPRINTF_LEN]; 338 snprintf_blkptr_compact(blkbuf, sizeof (blkbuf), 339 &e->svbr_blk, B_TRUE); 340 (void) printf("\tERROR: %d unmatched FREE(s): %s\n", 341 e->svbr_refcnt, blkbuf); 342 } 343 zfs_btree_destroy(&sv->sv_pair); 344 345 return (err); 346 } 347 348 static int 349 livelist_block_compare(const void *larg, const void *rarg) 350 { 351 const sublivelist_verify_block_t *l = larg; 352 const sublivelist_verify_block_t *r = rarg; 353 354 if (DVA_GET_VDEV(&l->svb_dva) < DVA_GET_VDEV(&r->svb_dva)) 355 return (-1); 356 else if (DVA_GET_VDEV(&l->svb_dva) > DVA_GET_VDEV(&r->svb_dva)) 357 return (+1); 358 359 if (DVA_GET_OFFSET(&l->svb_dva) < DVA_GET_OFFSET(&r->svb_dva)) 360 return (-1); 361 else if (DVA_GET_OFFSET(&l->svb_dva) > DVA_GET_OFFSET(&r->svb_dva)) 362 return (+1); 363 364 if (DVA_GET_ASIZE(&l->svb_dva) < DVA_GET_ASIZE(&r->svb_dva)) 365 return (-1); 366 else if (DVA_GET_ASIZE(&l->svb_dva) > DVA_GET_ASIZE(&r->svb_dva)) 367 return (+1); 368 369 return (0); 370 } 371 372 /* 373 * Check for errors in a livelist while tracking all unfreed ALLOCs in the 374 * sublivelist_verify_t: sv->sv_leftover 375 */ 376 static void 377 livelist_verify(dsl_deadlist_t *dl, void *arg) 378 { 379 sublivelist_verify_t *sv = arg; 380 dsl_deadlist_iterate(dl, sublivelist_verify_func, sv); 381 } 382 383 /* 384 * Check for errors in the livelist entry and discard the intermediary 385 * data structures 386 */ 387 static int 388 sublivelist_verify_lightweight(void *args, dsl_deadlist_entry_t *dle) 389 { 390 (void) args; 391 sublivelist_verify_t sv; 392 zfs_btree_create(&sv.sv_leftover, livelist_block_compare, 393 sizeof (sublivelist_verify_block_t)); 394 int err = sublivelist_verify_func(&sv, dle); 395 zfs_btree_clear(&sv.sv_leftover); 396 zfs_btree_destroy(&sv.sv_leftover); 397 return (err); 398 } 399 400 typedef struct metaslab_verify { 401 /* 402 * Tree containing all the leftover ALLOCs from the livelists 403 * that are part of this metaslab. 404 */ 405 zfs_btree_t mv_livelist_allocs; 406 407 /* 408 * Metaslab information. 409 */ 410 uint64_t mv_vdid; 411 uint64_t mv_msid; 412 uint64_t mv_start; 413 uint64_t mv_end; 414 415 /* 416 * What's currently allocated for this metaslab. 417 */ 418 range_tree_t *mv_allocated; 419 } metaslab_verify_t; 420 421 typedef void ll_iter_t(dsl_deadlist_t *ll, void *arg); 422 423 typedef int (*zdb_log_sm_cb_t)(spa_t *spa, space_map_entry_t *sme, uint64_t txg, 424 void *arg); 425 426 typedef struct unflushed_iter_cb_arg { 427 spa_t *uic_spa; 428 uint64_t uic_txg; 429 void *uic_arg; 430 zdb_log_sm_cb_t uic_cb; 431 } unflushed_iter_cb_arg_t; 432 433 static int 434 iterate_through_spacemap_logs_cb(space_map_entry_t *sme, void *arg) 435 { 436 unflushed_iter_cb_arg_t *uic = arg; 437 return (uic->uic_cb(uic->uic_spa, sme, uic->uic_txg, uic->uic_arg)); 438 } 439 440 static void 441 iterate_through_spacemap_logs(spa_t *spa, zdb_log_sm_cb_t cb, void *arg) 442 { 443 if (!spa_feature_is_active(spa, SPA_FEATURE_LOG_SPACEMAP)) 444 return; 445 446 spa_config_enter(spa, SCL_CONFIG, FTAG, RW_READER); 447 for (spa_log_sm_t *sls = avl_first(&spa->spa_sm_logs_by_txg); 448 sls; sls = AVL_NEXT(&spa->spa_sm_logs_by_txg, sls)) { 449 space_map_t *sm = NULL; 450 VERIFY0(space_map_open(&sm, spa_meta_objset(spa), 451 sls->sls_sm_obj, 0, UINT64_MAX, SPA_MINBLOCKSHIFT)); 452 453 unflushed_iter_cb_arg_t uic = { 454 .uic_spa = spa, 455 .uic_txg = sls->sls_txg, 456 .uic_arg = arg, 457 .uic_cb = cb 458 }; 459 VERIFY0(space_map_iterate(sm, space_map_length(sm), 460 iterate_through_spacemap_logs_cb, &uic)); 461 space_map_close(sm); 462 } 463 spa_config_exit(spa, SCL_CONFIG, FTAG); 464 } 465 466 static void 467 verify_livelist_allocs(metaslab_verify_t *mv, uint64_t txg, 468 uint64_t offset, uint64_t size) 469 { 470 sublivelist_verify_block_t svb; 471 DVA_SET_VDEV(&svb.svb_dva, mv->mv_vdid); 472 DVA_SET_OFFSET(&svb.svb_dva, offset); 473 DVA_SET_ASIZE(&svb.svb_dva, size); 474 zfs_btree_index_t where; 475 uint64_t end_offset = offset + size; 476 477 /* 478 * Look for an exact match for spacemap entry in the livelist entries. 479 * Then, look for other livelist entries that fall within the range 480 * of the spacemap entry as it may have been condensed 481 */ 482 sublivelist_verify_block_t *found = 483 zfs_btree_find(&mv->mv_livelist_allocs, &svb, &where); 484 if (found == NULL) { 485 found = zfs_btree_next(&mv->mv_livelist_allocs, &where, &where); 486 } 487 for (; found != NULL && DVA_GET_VDEV(&found->svb_dva) == mv->mv_vdid && 488 DVA_GET_OFFSET(&found->svb_dva) < end_offset; 489 found = zfs_btree_next(&mv->mv_livelist_allocs, &where, &where)) { 490 if (found->svb_allocated_txg <= txg) { 491 (void) printf("ERROR: Livelist ALLOC [%llx:%llx] " 492 "from TXG %llx FREED at TXG %llx\n", 493 (u_longlong_t)DVA_GET_OFFSET(&found->svb_dva), 494 (u_longlong_t)DVA_GET_ASIZE(&found->svb_dva), 495 (u_longlong_t)found->svb_allocated_txg, 496 (u_longlong_t)txg); 497 } 498 } 499 } 500 501 static int 502 metaslab_spacemap_validation_cb(space_map_entry_t *sme, void *arg) 503 { 504 metaslab_verify_t *mv = arg; 505 uint64_t offset = sme->sme_offset; 506 uint64_t size = sme->sme_run; 507 uint64_t txg = sme->sme_txg; 508 509 if (sme->sme_type == SM_ALLOC) { 510 if (range_tree_contains(mv->mv_allocated, 511 offset, size)) { 512 (void) printf("ERROR: DOUBLE ALLOC: " 513 "%llu [%llx:%llx] " 514 "%llu:%llu LOG_SM\n", 515 (u_longlong_t)txg, (u_longlong_t)offset, 516 (u_longlong_t)size, (u_longlong_t)mv->mv_vdid, 517 (u_longlong_t)mv->mv_msid); 518 } else { 519 range_tree_add(mv->mv_allocated, 520 offset, size); 521 } 522 } else { 523 if (!range_tree_contains(mv->mv_allocated, 524 offset, size)) { 525 (void) printf("ERROR: DOUBLE FREE: " 526 "%llu [%llx:%llx] " 527 "%llu:%llu LOG_SM\n", 528 (u_longlong_t)txg, (u_longlong_t)offset, 529 (u_longlong_t)size, (u_longlong_t)mv->mv_vdid, 530 (u_longlong_t)mv->mv_msid); 531 } else { 532 range_tree_remove(mv->mv_allocated, 533 offset, size); 534 } 535 } 536 537 if (sme->sme_type != SM_ALLOC) { 538 /* 539 * If something is freed in the spacemap, verify that 540 * it is not listed as allocated in the livelist. 541 */ 542 verify_livelist_allocs(mv, txg, offset, size); 543 } 544 return (0); 545 } 546 547 static int 548 spacemap_check_sm_log_cb(spa_t *spa, space_map_entry_t *sme, 549 uint64_t txg, void *arg) 550 { 551 metaslab_verify_t *mv = arg; 552 uint64_t offset = sme->sme_offset; 553 uint64_t vdev_id = sme->sme_vdev; 554 555 vdev_t *vd = vdev_lookup_top(spa, vdev_id); 556 557 /* skip indirect vdevs */ 558 if (!vdev_is_concrete(vd)) 559 return (0); 560 561 if (vdev_id != mv->mv_vdid) 562 return (0); 563 564 metaslab_t *ms = vd->vdev_ms[offset >> vd->vdev_ms_shift]; 565 if (ms->ms_id != mv->mv_msid) 566 return (0); 567 568 if (txg < metaslab_unflushed_txg(ms)) 569 return (0); 570 571 572 ASSERT3U(txg, ==, sme->sme_txg); 573 return (metaslab_spacemap_validation_cb(sme, mv)); 574 } 575 576 static void 577 spacemap_check_sm_log(spa_t *spa, metaslab_verify_t *mv) 578 { 579 iterate_through_spacemap_logs(spa, spacemap_check_sm_log_cb, mv); 580 } 581 582 static void 583 spacemap_check_ms_sm(space_map_t *sm, metaslab_verify_t *mv) 584 { 585 if (sm == NULL) 586 return; 587 588 VERIFY0(space_map_iterate(sm, space_map_length(sm), 589 metaslab_spacemap_validation_cb, mv)); 590 } 591 592 static void iterate_deleted_livelists(spa_t *spa, ll_iter_t func, void *arg); 593 594 /* 595 * Transfer blocks from sv_leftover tree to the mv_livelist_allocs if 596 * they are part of that metaslab (mv_msid). 597 */ 598 static void 599 mv_populate_livelist_allocs(metaslab_verify_t *mv, sublivelist_verify_t *sv) 600 { 601 zfs_btree_index_t where; 602 sublivelist_verify_block_t *svb; 603 ASSERT3U(zfs_btree_numnodes(&mv->mv_livelist_allocs), ==, 0); 604 for (svb = zfs_btree_first(&sv->sv_leftover, &where); 605 svb != NULL; 606 svb = zfs_btree_next(&sv->sv_leftover, &where, &where)) { 607 if (DVA_GET_VDEV(&svb->svb_dva) != mv->mv_vdid) 608 continue; 609 610 if (DVA_GET_OFFSET(&svb->svb_dva) < mv->mv_start && 611 (DVA_GET_OFFSET(&svb->svb_dva) + 612 DVA_GET_ASIZE(&svb->svb_dva)) > mv->mv_start) { 613 (void) printf("ERROR: Found block that crosses " 614 "metaslab boundary: <%llu:%llx:%llx>\n", 615 (u_longlong_t)DVA_GET_VDEV(&svb->svb_dva), 616 (u_longlong_t)DVA_GET_OFFSET(&svb->svb_dva), 617 (u_longlong_t)DVA_GET_ASIZE(&svb->svb_dva)); 618 continue; 619 } 620 621 if (DVA_GET_OFFSET(&svb->svb_dva) < mv->mv_start) 622 continue; 623 624 if (DVA_GET_OFFSET(&svb->svb_dva) >= mv->mv_end) 625 continue; 626 627 if ((DVA_GET_OFFSET(&svb->svb_dva) + 628 DVA_GET_ASIZE(&svb->svb_dva)) > mv->mv_end) { 629 (void) printf("ERROR: Found block that crosses " 630 "metaslab boundary: <%llu:%llx:%llx>\n", 631 (u_longlong_t)DVA_GET_VDEV(&svb->svb_dva), 632 (u_longlong_t)DVA_GET_OFFSET(&svb->svb_dva), 633 (u_longlong_t)DVA_GET_ASIZE(&svb->svb_dva)); 634 continue; 635 } 636 637 zfs_btree_add(&mv->mv_livelist_allocs, svb); 638 } 639 640 for (svb = zfs_btree_first(&mv->mv_livelist_allocs, &where); 641 svb != NULL; 642 svb = zfs_btree_next(&mv->mv_livelist_allocs, &where, &where)) { 643 zfs_btree_remove(&sv->sv_leftover, svb); 644 } 645 } 646 647 /* 648 * [Livelist Check] 649 * Iterate through all the sublivelists and: 650 * - report leftover frees (**) 651 * - record leftover ALLOCs together with their TXG [see Cross Check] 652 * 653 * (**) Note: Double ALLOCs are valid in datasets that have dedup 654 * enabled. Similarly double FREEs are allowed as well but 655 * only if they pair up with a corresponding ALLOC entry once 656 * we our done with our sublivelist iteration. 657 * 658 * [Spacemap Check] 659 * for each metaslab: 660 * - iterate over spacemap and then the metaslab's entries in the 661 * spacemap log, then report any double FREEs and ALLOCs (do not 662 * blow up). 663 * 664 * [Cross Check] 665 * After finishing the Livelist Check phase and while being in the 666 * Spacemap Check phase, we find all the recorded leftover ALLOCs 667 * of the livelist check that are part of the metaslab that we are 668 * currently looking at in the Spacemap Check. We report any entries 669 * that are marked as ALLOCs in the livelists but have been actually 670 * freed (and potentially allocated again) after their TXG stamp in 671 * the spacemaps. Also report any ALLOCs from the livelists that 672 * belong to indirect vdevs (e.g. their vdev completed removal). 673 * 674 * Note that this will miss Log Spacemap entries that cancelled each other 675 * out before being flushed to the metaslab, so we are not guaranteed 676 * to match all erroneous ALLOCs. 677 */ 678 static void 679 livelist_metaslab_validate(spa_t *spa) 680 { 681 (void) printf("Verifying deleted livelist entries\n"); 682 683 sublivelist_verify_t sv; 684 zfs_btree_create(&sv.sv_leftover, livelist_block_compare, 685 sizeof (sublivelist_verify_block_t)); 686 iterate_deleted_livelists(spa, livelist_verify, &sv); 687 688 (void) printf("Verifying metaslab entries\n"); 689 vdev_t *rvd = spa->spa_root_vdev; 690 for (uint64_t c = 0; c < rvd->vdev_children; c++) { 691 vdev_t *vd = rvd->vdev_child[c]; 692 693 if (!vdev_is_concrete(vd)) 694 continue; 695 696 for (uint64_t mid = 0; mid < vd->vdev_ms_count; mid++) { 697 metaslab_t *m = vd->vdev_ms[mid]; 698 699 (void) fprintf(stderr, 700 "\rverifying concrete vdev %llu, " 701 "metaslab %llu of %llu ...", 702 (longlong_t)vd->vdev_id, 703 (longlong_t)mid, 704 (longlong_t)vd->vdev_ms_count); 705 706 uint64_t shift, start; 707 range_seg_type_t type = 708 metaslab_calculate_range_tree_type(vd, m, 709 &start, &shift); 710 metaslab_verify_t mv; 711 mv.mv_allocated = range_tree_create(NULL, 712 type, NULL, start, shift); 713 mv.mv_vdid = vd->vdev_id; 714 mv.mv_msid = m->ms_id; 715 mv.mv_start = m->ms_start; 716 mv.mv_end = m->ms_start + m->ms_size; 717 zfs_btree_create(&mv.mv_livelist_allocs, 718 livelist_block_compare, 719 sizeof (sublivelist_verify_block_t)); 720 721 mv_populate_livelist_allocs(&mv, &sv); 722 723 spacemap_check_ms_sm(m->ms_sm, &mv); 724 spacemap_check_sm_log(spa, &mv); 725 726 range_tree_vacate(mv.mv_allocated, NULL, NULL); 727 range_tree_destroy(mv.mv_allocated); 728 zfs_btree_clear(&mv.mv_livelist_allocs); 729 zfs_btree_destroy(&mv.mv_livelist_allocs); 730 } 731 } 732 (void) fprintf(stderr, "\n"); 733 734 /* 735 * If there are any segments in the leftover tree after we walked 736 * through all the metaslabs in the concrete vdevs then this means 737 * that we have segments in the livelists that belong to indirect 738 * vdevs and are marked as allocated. 739 */ 740 if (zfs_btree_numnodes(&sv.sv_leftover) == 0) { 741 zfs_btree_destroy(&sv.sv_leftover); 742 return; 743 } 744 (void) printf("ERROR: Found livelist blocks marked as allocated " 745 "for indirect vdevs:\n"); 746 747 zfs_btree_index_t *where = NULL; 748 sublivelist_verify_block_t *svb; 749 while ((svb = zfs_btree_destroy_nodes(&sv.sv_leftover, &where)) != 750 NULL) { 751 int vdev_id = DVA_GET_VDEV(&svb->svb_dva); 752 ASSERT3U(vdev_id, <, rvd->vdev_children); 753 vdev_t *vd = rvd->vdev_child[vdev_id]; 754 ASSERT(!vdev_is_concrete(vd)); 755 (void) printf("<%d:%llx:%llx> TXG %llx\n", 756 vdev_id, (u_longlong_t)DVA_GET_OFFSET(&svb->svb_dva), 757 (u_longlong_t)DVA_GET_ASIZE(&svb->svb_dva), 758 (u_longlong_t)svb->svb_allocated_txg); 759 } 760 (void) printf("\n"); 761 zfs_btree_destroy(&sv.sv_leftover); 762 } 763 764 /* 765 * These libumem hooks provide a reasonable set of defaults for the allocator's 766 * debugging facilities. 767 */ 768 const char * 769 _umem_debug_init(void) 770 { 771 return ("default,verbose"); /* $UMEM_DEBUG setting */ 772 } 773 774 const char * 775 _umem_logging_init(void) 776 { 777 return ("fail,contents"); /* $UMEM_LOGGING setting */ 778 } 779 780 static void 781 usage(void) 782 { 783 (void) fprintf(stderr, 784 "Usage:\t%s [-AbcdDFGhikLMPsvXy] [-e [-V] [-p <path> ...]] " 785 "[-I <inflight I/Os>]\n" 786 "\t\t[-o <var>=<value>]... [-t <txg>] [-U <cache>] [-x <dumpdir>]\n" 787 "\t\t[<poolname>[/<dataset | objset id>] [<object | range> ...]]\n" 788 "\t%s [-AdiPv] [-e [-V] [-p <path> ...]] [-U <cache>]\n" 789 "\t\t[<poolname>[/<dataset | objset id>] [<object | range> ...]\n" 790 "\t%s [-v] <bookmark>\n" 791 "\t%s -C [-A] [-U <cache>]\n" 792 "\t%s -l [-Aqu] <device>\n" 793 "\t%s -m [-AFLPX] [-e [-V] [-p <path> ...]] [-t <txg>] " 794 "[-U <cache>]\n\t\t<poolname> [<vdev> [<metaslab> ...]]\n" 795 "\t%s -O <dataset> <path>\n" 796 "\t%s -r <dataset> <path> <destination>\n" 797 "\t%s -R [-A] [-e [-V] [-p <path> ...]] [-U <cache>]\n" 798 "\t\t<poolname> <vdev>:<offset>:<size>[:<flags>]\n" 799 "\t%s -E [-A] word0:word1:...:word15\n" 800 "\t%s -S [-AP] [-e [-V] [-p <path> ...]] [-U <cache>] " 801 "<poolname>\n\n", 802 cmdname, cmdname, cmdname, cmdname, cmdname, cmdname, cmdname, 803 cmdname, cmdname, cmdname, cmdname); 804 805 (void) fprintf(stderr, " Dataset name must include at least one " 806 "separator character '/' or '@'\n"); 807 (void) fprintf(stderr, " If dataset name is specified, only that " 808 "dataset is dumped\n"); 809 (void) fprintf(stderr, " If object numbers or object number " 810 "ranges are specified, only those\n" 811 " objects or ranges are dumped.\n\n"); 812 (void) fprintf(stderr, 813 " Object ranges take the form <start>:<end>[:<flags>]\n" 814 " start Starting object number\n" 815 " end Ending object number, or -1 for no upper bound\n" 816 " flags Optional flags to select object types:\n" 817 " A All objects (this is the default)\n" 818 " d ZFS directories\n" 819 " f ZFS files \n" 820 " m SPA space maps\n" 821 " z ZAPs\n" 822 " - Negate effect of next flag\n\n"); 823 (void) fprintf(stderr, " Options to control amount of output:\n"); 824 (void) fprintf(stderr, " -b --block-stats " 825 "block statistics\n"); 826 (void) fprintf(stderr, " -c --checksum " 827 "checksum all metadata (twice for all data) blocks\n"); 828 (void) fprintf(stderr, " -C --config " 829 "config (or cachefile if alone)\n"); 830 (void) fprintf(stderr, " -d --datasets " 831 "dataset(s)\n"); 832 (void) fprintf(stderr, " -D --dedup-stats " 833 "dedup statistics\n"); 834 (void) fprintf(stderr, " -E --embedded-block-pointer=INTEGER\n" 835 " decode and display block " 836 "from an embedded block pointer\n"); 837 (void) fprintf(stderr, " -h --history " 838 "pool history\n"); 839 (void) fprintf(stderr, " -i --intent-logs " 840 "intent logs\n"); 841 (void) fprintf(stderr, " -l --label " 842 "read label contents\n"); 843 (void) fprintf(stderr, " -k --checkpointed-state " 844 "examine the checkpointed state of the pool\n"); 845 (void) fprintf(stderr, " -L --disable-leak-tracking " 846 "disable leak tracking (do not load spacemaps)\n"); 847 (void) fprintf(stderr, " -m --metaslabs " 848 "metaslabs\n"); 849 (void) fprintf(stderr, " -M --metaslab-groups " 850 "metaslab groups\n"); 851 (void) fprintf(stderr, " -O --object-lookups " 852 "perform object lookups by path\n"); 853 (void) fprintf(stderr, " -r --copy-object " 854 "copy an object by path to file\n"); 855 (void) fprintf(stderr, " -R --read-block " 856 "read and display block from a device\n"); 857 (void) fprintf(stderr, " -s --io-stats " 858 "report stats on zdb's I/O\n"); 859 (void) fprintf(stderr, " -S --simulate-dedup " 860 "simulate dedup to measure effect\n"); 861 (void) fprintf(stderr, " -v --verbose " 862 "verbose (applies to all others)\n"); 863 (void) fprintf(stderr, " -y --livelist " 864 "perform livelist and metaslab validation on any livelists being " 865 "deleted\n\n"); 866 (void) fprintf(stderr, " Below options are intended for use " 867 "with other options:\n"); 868 (void) fprintf(stderr, " -A --ignore-assertions " 869 "ignore assertions (-A), enable panic recovery (-AA) or both " 870 "(-AAA)\n"); 871 (void) fprintf(stderr, " -e --exported " 872 "pool is exported/destroyed/has altroot/not in a cachefile\n"); 873 (void) fprintf(stderr, " -F --automatic-rewind " 874 "attempt automatic rewind within safe range of transaction " 875 "groups\n"); 876 (void) fprintf(stderr, " -G --dump-debug-msg " 877 "dump zfs_dbgmsg buffer before exiting\n"); 878 (void) fprintf(stderr, " -I --inflight=INTEGER " 879 "specify the maximum number of checksumming I/Os " 880 "[default is 200]\n"); 881 (void) fprintf(stderr, " -o --option=\"OPTION=INTEGER\" " 882 "set global variable to an unsigned 32-bit integer\n"); 883 (void) fprintf(stderr, " -p --path==PATH " 884 "use one or more with -e to specify path to vdev dir\n"); 885 (void) fprintf(stderr, " -P --parseable " 886 "print numbers in parseable form\n"); 887 (void) fprintf(stderr, " -q --skip-label " 888 "don't print label contents\n"); 889 (void) fprintf(stderr, " -t --txg=INTEGER " 890 "highest txg to use when searching for uberblocks\n"); 891 (void) fprintf(stderr, " -u --uberblock " 892 "uberblock\n"); 893 (void) fprintf(stderr, " -U --cachefile=PATH " 894 "use alternate cachefile\n"); 895 (void) fprintf(stderr, " -V --verbatim " 896 "do verbatim import\n"); 897 (void) fprintf(stderr, " -x --dump-blocks=PATH " 898 "dump all read blocks into specified directory\n"); 899 (void) fprintf(stderr, " -X --extreme-rewind " 900 "attempt extreme rewind (does not work with dataset)\n"); 901 (void) fprintf(stderr, " -Y --all-reconstruction " 902 "attempt all reconstruction combinations for split blocks\n"); 903 (void) fprintf(stderr, " -Z --zstd-headers " 904 "show ZSTD headers \n"); 905 (void) fprintf(stderr, "Specify an option more than once (e.g. -bb) " 906 "to make only that option verbose\n"); 907 (void) fprintf(stderr, "Default is to dump everything non-verbosely\n"); 908 exit(1); 909 } 910 911 static void 912 dump_debug_buffer(void) 913 { 914 if (dump_opt['G']) { 915 (void) printf("\n"); 916 (void) fflush(stdout); 917 zfs_dbgmsg_print("zdb"); 918 } 919 } 920 921 /* 922 * Called for usage errors that are discovered after a call to spa_open(), 923 * dmu_bonus_hold(), or pool_match(). abort() is called for other errors. 924 */ 925 926 static void 927 fatal(const char *fmt, ...) 928 { 929 va_list ap; 930 931 va_start(ap, fmt); 932 (void) fprintf(stderr, "%s: ", cmdname); 933 (void) vfprintf(stderr, fmt, ap); 934 va_end(ap); 935 (void) fprintf(stderr, "\n"); 936 937 dump_debug_buffer(); 938 939 exit(1); 940 } 941 942 static void 943 dump_packed_nvlist(objset_t *os, uint64_t object, void *data, size_t size) 944 { 945 (void) size; 946 nvlist_t *nv; 947 size_t nvsize = *(uint64_t *)data; 948 char *packed = umem_alloc(nvsize, UMEM_NOFAIL); 949 950 VERIFY(0 == dmu_read(os, object, 0, nvsize, packed, DMU_READ_PREFETCH)); 951 952 VERIFY(nvlist_unpack(packed, nvsize, &nv, 0) == 0); 953 954 umem_free(packed, nvsize); 955 956 dump_nvlist(nv, 8); 957 958 nvlist_free(nv); 959 } 960 961 static void 962 dump_history_offsets(objset_t *os, uint64_t object, void *data, size_t size) 963 { 964 (void) os, (void) object, (void) size; 965 spa_history_phys_t *shp = data; 966 967 if (shp == NULL) 968 return; 969 970 (void) printf("\t\tpool_create_len = %llu\n", 971 (u_longlong_t)shp->sh_pool_create_len); 972 (void) printf("\t\tphys_max_off = %llu\n", 973 (u_longlong_t)shp->sh_phys_max_off); 974 (void) printf("\t\tbof = %llu\n", 975 (u_longlong_t)shp->sh_bof); 976 (void) printf("\t\teof = %llu\n", 977 (u_longlong_t)shp->sh_eof); 978 (void) printf("\t\trecords_lost = %llu\n", 979 (u_longlong_t)shp->sh_records_lost); 980 } 981 982 static void 983 zdb_nicenum(uint64_t num, char *buf, size_t buflen) 984 { 985 if (dump_opt['P']) 986 (void) snprintf(buf, buflen, "%llu", (longlong_t)num); 987 else 988 nicenum(num, buf, buflen); 989 } 990 991 static const char histo_stars[] = "****************************************"; 992 static const uint64_t histo_width = sizeof (histo_stars) - 1; 993 994 static void 995 dump_histogram(const uint64_t *histo, int size, int offset) 996 { 997 int i; 998 int minidx = size - 1; 999 int maxidx = 0; 1000 uint64_t max = 0; 1001 1002 for (i = 0; i < size; i++) { 1003 if (histo[i] > max) 1004 max = histo[i]; 1005 if (histo[i] > 0 && i > maxidx) 1006 maxidx = i; 1007 if (histo[i] > 0 && i < minidx) 1008 minidx = i; 1009 } 1010 1011 if (max < histo_width) 1012 max = histo_width; 1013 1014 for (i = minidx; i <= maxidx; i++) { 1015 (void) printf("\t\t\t%3u: %6llu %s\n", 1016 i + offset, (u_longlong_t)histo[i], 1017 &histo_stars[(max - histo[i]) * histo_width / max]); 1018 } 1019 } 1020 1021 static void 1022 dump_zap_stats(objset_t *os, uint64_t object) 1023 { 1024 int error; 1025 zap_stats_t zs; 1026 1027 error = zap_get_stats(os, object, &zs); 1028 if (error) 1029 return; 1030 1031 if (zs.zs_ptrtbl_len == 0) { 1032 ASSERT(zs.zs_num_blocks == 1); 1033 (void) printf("\tmicrozap: %llu bytes, %llu entries\n", 1034 (u_longlong_t)zs.zs_blocksize, 1035 (u_longlong_t)zs.zs_num_entries); 1036 return; 1037 } 1038 1039 (void) printf("\tFat ZAP stats:\n"); 1040 1041 (void) printf("\t\tPointer table:\n"); 1042 (void) printf("\t\t\t%llu elements\n", 1043 (u_longlong_t)zs.zs_ptrtbl_len); 1044 (void) printf("\t\t\tzt_blk: %llu\n", 1045 (u_longlong_t)zs.zs_ptrtbl_zt_blk); 1046 (void) printf("\t\t\tzt_numblks: %llu\n", 1047 (u_longlong_t)zs.zs_ptrtbl_zt_numblks); 1048 (void) printf("\t\t\tzt_shift: %llu\n", 1049 (u_longlong_t)zs.zs_ptrtbl_zt_shift); 1050 (void) printf("\t\t\tzt_blks_copied: %llu\n", 1051 (u_longlong_t)zs.zs_ptrtbl_blks_copied); 1052 (void) printf("\t\t\tzt_nextblk: %llu\n", 1053 (u_longlong_t)zs.zs_ptrtbl_nextblk); 1054 1055 (void) printf("\t\tZAP entries: %llu\n", 1056 (u_longlong_t)zs.zs_num_entries); 1057 (void) printf("\t\tLeaf blocks: %llu\n", 1058 (u_longlong_t)zs.zs_num_leafs); 1059 (void) printf("\t\tTotal blocks: %llu\n", 1060 (u_longlong_t)zs.zs_num_blocks); 1061 (void) printf("\t\tzap_block_type: 0x%llx\n", 1062 (u_longlong_t)zs.zs_block_type); 1063 (void) printf("\t\tzap_magic: 0x%llx\n", 1064 (u_longlong_t)zs.zs_magic); 1065 (void) printf("\t\tzap_salt: 0x%llx\n", 1066 (u_longlong_t)zs.zs_salt); 1067 1068 (void) printf("\t\tLeafs with 2^n pointers:\n"); 1069 dump_histogram(zs.zs_leafs_with_2n_pointers, ZAP_HISTOGRAM_SIZE, 0); 1070 1071 (void) printf("\t\tBlocks with n*5 entries:\n"); 1072 dump_histogram(zs.zs_blocks_with_n5_entries, ZAP_HISTOGRAM_SIZE, 0); 1073 1074 (void) printf("\t\tBlocks n/10 full:\n"); 1075 dump_histogram(zs.zs_blocks_n_tenths_full, ZAP_HISTOGRAM_SIZE, 0); 1076 1077 (void) printf("\t\tEntries with n chunks:\n"); 1078 dump_histogram(zs.zs_entries_using_n_chunks, ZAP_HISTOGRAM_SIZE, 0); 1079 1080 (void) printf("\t\tBuckets with n entries:\n"); 1081 dump_histogram(zs.zs_buckets_with_n_entries, ZAP_HISTOGRAM_SIZE, 0); 1082 } 1083 1084 static void 1085 dump_none(objset_t *os, uint64_t object, void *data, size_t size) 1086 { 1087 (void) os, (void) object, (void) data, (void) size; 1088 } 1089 1090 static void 1091 dump_unknown(objset_t *os, uint64_t object, void *data, size_t size) 1092 { 1093 (void) os, (void) object, (void) data, (void) size; 1094 (void) printf("\tUNKNOWN OBJECT TYPE\n"); 1095 } 1096 1097 static void 1098 dump_uint8(objset_t *os, uint64_t object, void *data, size_t size) 1099 { 1100 (void) os, (void) object, (void) data, (void) size; 1101 } 1102 1103 static void 1104 dump_uint64(objset_t *os, uint64_t object, void *data, size_t size) 1105 { 1106 uint64_t *arr; 1107 uint64_t oursize; 1108 if (dump_opt['d'] < 6) 1109 return; 1110 1111 if (data == NULL) { 1112 dmu_object_info_t doi; 1113 1114 VERIFY0(dmu_object_info(os, object, &doi)); 1115 size = doi.doi_max_offset; 1116 /* 1117 * We cap the size at 1 mebibyte here to prevent 1118 * allocation failures and nigh-infinite printing if the 1119 * object is extremely large. 1120 */ 1121 oursize = MIN(size, 1 << 20); 1122 arr = kmem_alloc(oursize, KM_SLEEP); 1123 1124 int err = dmu_read(os, object, 0, oursize, arr, 0); 1125 if (err != 0) { 1126 (void) printf("got error %u from dmu_read\n", err); 1127 kmem_free(arr, oursize); 1128 return; 1129 } 1130 } else { 1131 /* 1132 * Even though the allocation is already done in this code path, 1133 * we still cap the size to prevent excessive printing. 1134 */ 1135 oursize = MIN(size, 1 << 20); 1136 arr = data; 1137 } 1138 1139 if (size == 0) { 1140 if (data == NULL) 1141 kmem_free(arr, oursize); 1142 (void) printf("\t\t[]\n"); 1143 return; 1144 } 1145 1146 (void) printf("\t\t[%0llx", (u_longlong_t)arr[0]); 1147 for (size_t i = 1; i * sizeof (uint64_t) < oursize; i++) { 1148 if (i % 4 != 0) 1149 (void) printf(", %0llx", (u_longlong_t)arr[i]); 1150 else 1151 (void) printf(",\n\t\t%0llx", (u_longlong_t)arr[i]); 1152 } 1153 if (oursize != size) 1154 (void) printf(", ... "); 1155 (void) printf("]\n"); 1156 1157 if (data == NULL) 1158 kmem_free(arr, oursize); 1159 } 1160 1161 static void 1162 dump_zap(objset_t *os, uint64_t object, void *data, size_t size) 1163 { 1164 (void) data, (void) size; 1165 zap_cursor_t zc; 1166 zap_attribute_t attr; 1167 void *prop; 1168 unsigned i; 1169 1170 dump_zap_stats(os, object); 1171 (void) printf("\n"); 1172 1173 for (zap_cursor_init(&zc, os, object); 1174 zap_cursor_retrieve(&zc, &attr) == 0; 1175 zap_cursor_advance(&zc)) { 1176 (void) printf("\t\t%s = ", attr.za_name); 1177 if (attr.za_num_integers == 0) { 1178 (void) printf("\n"); 1179 continue; 1180 } 1181 prop = umem_zalloc(attr.za_num_integers * 1182 attr.za_integer_length, UMEM_NOFAIL); 1183 (void) zap_lookup(os, object, attr.za_name, 1184 attr.za_integer_length, attr.za_num_integers, prop); 1185 if (attr.za_integer_length == 1) { 1186 if (strcmp(attr.za_name, 1187 DSL_CRYPTO_KEY_MASTER_KEY) == 0 || 1188 strcmp(attr.za_name, 1189 DSL_CRYPTO_KEY_HMAC_KEY) == 0 || 1190 strcmp(attr.za_name, DSL_CRYPTO_KEY_IV) == 0 || 1191 strcmp(attr.za_name, DSL_CRYPTO_KEY_MAC) == 0 || 1192 strcmp(attr.za_name, DMU_POOL_CHECKSUM_SALT) == 0) { 1193 uint8_t *u8 = prop; 1194 1195 for (i = 0; i < attr.za_num_integers; i++) { 1196 (void) printf("%02x", u8[i]); 1197 } 1198 } else { 1199 (void) printf("%s", (char *)prop); 1200 } 1201 } else { 1202 for (i = 0; i < attr.za_num_integers; i++) { 1203 switch (attr.za_integer_length) { 1204 case 2: 1205 (void) printf("%u ", 1206 ((uint16_t *)prop)[i]); 1207 break; 1208 case 4: 1209 (void) printf("%u ", 1210 ((uint32_t *)prop)[i]); 1211 break; 1212 case 8: 1213 (void) printf("%lld ", 1214 (u_longlong_t)((int64_t *)prop)[i]); 1215 break; 1216 } 1217 } 1218 } 1219 (void) printf("\n"); 1220 umem_free(prop, attr.za_num_integers * attr.za_integer_length); 1221 } 1222 zap_cursor_fini(&zc); 1223 } 1224 1225 static void 1226 dump_bpobj(objset_t *os, uint64_t object, void *data, size_t size) 1227 { 1228 bpobj_phys_t *bpop = data; 1229 uint64_t i; 1230 char bytes[32], comp[32], uncomp[32]; 1231 1232 /* make sure the output won't get truncated */ 1233 _Static_assert(sizeof (bytes) >= NN_NUMBUF_SZ, "bytes truncated"); 1234 _Static_assert(sizeof (comp) >= NN_NUMBUF_SZ, "comp truncated"); 1235 _Static_assert(sizeof (uncomp) >= NN_NUMBUF_SZ, "uncomp truncated"); 1236 1237 if (bpop == NULL) 1238 return; 1239 1240 zdb_nicenum(bpop->bpo_bytes, bytes, sizeof (bytes)); 1241 zdb_nicenum(bpop->bpo_comp, comp, sizeof (comp)); 1242 zdb_nicenum(bpop->bpo_uncomp, uncomp, sizeof (uncomp)); 1243 1244 (void) printf("\t\tnum_blkptrs = %llu\n", 1245 (u_longlong_t)bpop->bpo_num_blkptrs); 1246 (void) printf("\t\tbytes = %s\n", bytes); 1247 if (size >= BPOBJ_SIZE_V1) { 1248 (void) printf("\t\tcomp = %s\n", comp); 1249 (void) printf("\t\tuncomp = %s\n", uncomp); 1250 } 1251 if (size >= BPOBJ_SIZE_V2) { 1252 (void) printf("\t\tsubobjs = %llu\n", 1253 (u_longlong_t)bpop->bpo_subobjs); 1254 (void) printf("\t\tnum_subobjs = %llu\n", 1255 (u_longlong_t)bpop->bpo_num_subobjs); 1256 } 1257 if (size >= sizeof (*bpop)) { 1258 (void) printf("\t\tnum_freed = %llu\n", 1259 (u_longlong_t)bpop->bpo_num_freed); 1260 } 1261 1262 if (dump_opt['d'] < 5) 1263 return; 1264 1265 for (i = 0; i < bpop->bpo_num_blkptrs; i++) { 1266 char blkbuf[BP_SPRINTF_LEN]; 1267 blkptr_t bp; 1268 1269 int err = dmu_read(os, object, 1270 i * sizeof (bp), sizeof (bp), &bp, 0); 1271 if (err != 0) { 1272 (void) printf("got error %u from dmu_read\n", err); 1273 break; 1274 } 1275 snprintf_blkptr_compact(blkbuf, sizeof (blkbuf), &bp, 1276 BP_GET_FREE(&bp)); 1277 (void) printf("\t%s\n", blkbuf); 1278 } 1279 } 1280 1281 static void 1282 dump_bpobj_subobjs(objset_t *os, uint64_t object, void *data, size_t size) 1283 { 1284 (void) data, (void) size; 1285 dmu_object_info_t doi; 1286 int64_t i; 1287 1288 VERIFY0(dmu_object_info(os, object, &doi)); 1289 uint64_t *subobjs = kmem_alloc(doi.doi_max_offset, KM_SLEEP); 1290 1291 int err = dmu_read(os, object, 0, doi.doi_max_offset, subobjs, 0); 1292 if (err != 0) { 1293 (void) printf("got error %u from dmu_read\n", err); 1294 kmem_free(subobjs, doi.doi_max_offset); 1295 return; 1296 } 1297 1298 int64_t last_nonzero = -1; 1299 for (i = 0; i < doi.doi_max_offset / 8; i++) { 1300 if (subobjs[i] != 0) 1301 last_nonzero = i; 1302 } 1303 1304 for (i = 0; i <= last_nonzero; i++) { 1305 (void) printf("\t%llu\n", (u_longlong_t)subobjs[i]); 1306 } 1307 kmem_free(subobjs, doi.doi_max_offset); 1308 } 1309 1310 static void 1311 dump_ddt_zap(objset_t *os, uint64_t object, void *data, size_t size) 1312 { 1313 (void) data, (void) size; 1314 dump_zap_stats(os, object); 1315 /* contents are printed elsewhere, properly decoded */ 1316 } 1317 1318 static void 1319 dump_sa_attrs(objset_t *os, uint64_t object, void *data, size_t size) 1320 { 1321 (void) data, (void) size; 1322 zap_cursor_t zc; 1323 zap_attribute_t attr; 1324 1325 dump_zap_stats(os, object); 1326 (void) printf("\n"); 1327 1328 for (zap_cursor_init(&zc, os, object); 1329 zap_cursor_retrieve(&zc, &attr) == 0; 1330 zap_cursor_advance(&zc)) { 1331 (void) printf("\t\t%s = ", attr.za_name); 1332 if (attr.za_num_integers == 0) { 1333 (void) printf("\n"); 1334 continue; 1335 } 1336 (void) printf(" %llx : [%d:%d:%d]\n", 1337 (u_longlong_t)attr.za_first_integer, 1338 (int)ATTR_LENGTH(attr.za_first_integer), 1339 (int)ATTR_BSWAP(attr.za_first_integer), 1340 (int)ATTR_NUM(attr.za_first_integer)); 1341 } 1342 zap_cursor_fini(&zc); 1343 } 1344 1345 static void 1346 dump_sa_layouts(objset_t *os, uint64_t object, void *data, size_t size) 1347 { 1348 (void) data, (void) size; 1349 zap_cursor_t zc; 1350 zap_attribute_t attr; 1351 uint16_t *layout_attrs; 1352 unsigned i; 1353 1354 dump_zap_stats(os, object); 1355 (void) printf("\n"); 1356 1357 for (zap_cursor_init(&zc, os, object); 1358 zap_cursor_retrieve(&zc, &attr) == 0; 1359 zap_cursor_advance(&zc)) { 1360 (void) printf("\t\t%s = [", attr.za_name); 1361 if (attr.za_num_integers == 0) { 1362 (void) printf("\n"); 1363 continue; 1364 } 1365 1366 VERIFY(attr.za_integer_length == 2); 1367 layout_attrs = umem_zalloc(attr.za_num_integers * 1368 attr.za_integer_length, UMEM_NOFAIL); 1369 1370 VERIFY(zap_lookup(os, object, attr.za_name, 1371 attr.za_integer_length, 1372 attr.za_num_integers, layout_attrs) == 0); 1373 1374 for (i = 0; i != attr.za_num_integers; i++) 1375 (void) printf(" %d ", (int)layout_attrs[i]); 1376 (void) printf("]\n"); 1377 umem_free(layout_attrs, 1378 attr.za_num_integers * attr.za_integer_length); 1379 } 1380 zap_cursor_fini(&zc); 1381 } 1382 1383 static void 1384 dump_zpldir(objset_t *os, uint64_t object, void *data, size_t size) 1385 { 1386 (void) data, (void) size; 1387 zap_cursor_t zc; 1388 zap_attribute_t attr; 1389 const char *typenames[] = { 1390 /* 0 */ "not specified", 1391 /* 1 */ "FIFO", 1392 /* 2 */ "Character Device", 1393 /* 3 */ "3 (invalid)", 1394 /* 4 */ "Directory", 1395 /* 5 */ "5 (invalid)", 1396 /* 6 */ "Block Device", 1397 /* 7 */ "7 (invalid)", 1398 /* 8 */ "Regular File", 1399 /* 9 */ "9 (invalid)", 1400 /* 10 */ "Symbolic Link", 1401 /* 11 */ "11 (invalid)", 1402 /* 12 */ "Socket", 1403 /* 13 */ "Door", 1404 /* 14 */ "Event Port", 1405 /* 15 */ "15 (invalid)", 1406 }; 1407 1408 dump_zap_stats(os, object); 1409 (void) printf("\n"); 1410 1411 for (zap_cursor_init(&zc, os, object); 1412 zap_cursor_retrieve(&zc, &attr) == 0; 1413 zap_cursor_advance(&zc)) { 1414 (void) printf("\t\t%s = %lld (type: %s)\n", 1415 attr.za_name, ZFS_DIRENT_OBJ(attr.za_first_integer), 1416 typenames[ZFS_DIRENT_TYPE(attr.za_first_integer)]); 1417 } 1418 zap_cursor_fini(&zc); 1419 } 1420 1421 static int 1422 get_dtl_refcount(vdev_t *vd) 1423 { 1424 int refcount = 0; 1425 1426 if (vd->vdev_ops->vdev_op_leaf) { 1427 space_map_t *sm = vd->vdev_dtl_sm; 1428 1429 if (sm != NULL && 1430 sm->sm_dbuf->db_size == sizeof (space_map_phys_t)) 1431 return (1); 1432 return (0); 1433 } 1434 1435 for (unsigned c = 0; c < vd->vdev_children; c++) 1436 refcount += get_dtl_refcount(vd->vdev_child[c]); 1437 return (refcount); 1438 } 1439 1440 static int 1441 get_metaslab_refcount(vdev_t *vd) 1442 { 1443 int refcount = 0; 1444 1445 if (vd->vdev_top == vd) { 1446 for (uint64_t m = 0; m < vd->vdev_ms_count; m++) { 1447 space_map_t *sm = vd->vdev_ms[m]->ms_sm; 1448 1449 if (sm != NULL && 1450 sm->sm_dbuf->db_size == sizeof (space_map_phys_t)) 1451 refcount++; 1452 } 1453 } 1454 for (unsigned c = 0; c < vd->vdev_children; c++) 1455 refcount += get_metaslab_refcount(vd->vdev_child[c]); 1456 1457 return (refcount); 1458 } 1459 1460 static int 1461 get_obsolete_refcount(vdev_t *vd) 1462 { 1463 uint64_t obsolete_sm_object; 1464 int refcount = 0; 1465 1466 VERIFY0(vdev_obsolete_sm_object(vd, &obsolete_sm_object)); 1467 if (vd->vdev_top == vd && obsolete_sm_object != 0) { 1468 dmu_object_info_t doi; 1469 VERIFY0(dmu_object_info(vd->vdev_spa->spa_meta_objset, 1470 obsolete_sm_object, &doi)); 1471 if (doi.doi_bonus_size == sizeof (space_map_phys_t)) { 1472 refcount++; 1473 } 1474 } else { 1475 ASSERT3P(vd->vdev_obsolete_sm, ==, NULL); 1476 ASSERT3U(obsolete_sm_object, ==, 0); 1477 } 1478 for (unsigned c = 0; c < vd->vdev_children; c++) { 1479 refcount += get_obsolete_refcount(vd->vdev_child[c]); 1480 } 1481 1482 return (refcount); 1483 } 1484 1485 static int 1486 get_prev_obsolete_spacemap_refcount(spa_t *spa) 1487 { 1488 uint64_t prev_obj = 1489 spa->spa_condensing_indirect_phys.scip_prev_obsolete_sm_object; 1490 if (prev_obj != 0) { 1491 dmu_object_info_t doi; 1492 VERIFY0(dmu_object_info(spa->spa_meta_objset, prev_obj, &doi)); 1493 if (doi.doi_bonus_size == sizeof (space_map_phys_t)) { 1494 return (1); 1495 } 1496 } 1497 return (0); 1498 } 1499 1500 static int 1501 get_checkpoint_refcount(vdev_t *vd) 1502 { 1503 int refcount = 0; 1504 1505 if (vd->vdev_top == vd && vd->vdev_top_zap != 0 && 1506 zap_contains(spa_meta_objset(vd->vdev_spa), 1507 vd->vdev_top_zap, VDEV_TOP_ZAP_POOL_CHECKPOINT_SM) == 0) 1508 refcount++; 1509 1510 for (uint64_t c = 0; c < vd->vdev_children; c++) 1511 refcount += get_checkpoint_refcount(vd->vdev_child[c]); 1512 1513 return (refcount); 1514 } 1515 1516 static int 1517 get_log_spacemap_refcount(spa_t *spa) 1518 { 1519 return (avl_numnodes(&spa->spa_sm_logs_by_txg)); 1520 } 1521 1522 static int 1523 verify_spacemap_refcounts(spa_t *spa) 1524 { 1525 uint64_t expected_refcount = 0; 1526 uint64_t actual_refcount; 1527 1528 (void) feature_get_refcount(spa, 1529 &spa_feature_table[SPA_FEATURE_SPACEMAP_HISTOGRAM], 1530 &expected_refcount); 1531 actual_refcount = get_dtl_refcount(spa->spa_root_vdev); 1532 actual_refcount += get_metaslab_refcount(spa->spa_root_vdev); 1533 actual_refcount += get_obsolete_refcount(spa->spa_root_vdev); 1534 actual_refcount += get_prev_obsolete_spacemap_refcount(spa); 1535 actual_refcount += get_checkpoint_refcount(spa->spa_root_vdev); 1536 actual_refcount += get_log_spacemap_refcount(spa); 1537 1538 if (expected_refcount != actual_refcount) { 1539 (void) printf("space map refcount mismatch: expected %lld != " 1540 "actual %lld\n", 1541 (longlong_t)expected_refcount, 1542 (longlong_t)actual_refcount); 1543 return (2); 1544 } 1545 return (0); 1546 } 1547 1548 static void 1549 dump_spacemap(objset_t *os, space_map_t *sm) 1550 { 1551 const char *ddata[] = { "ALLOC", "FREE", "CONDENSE", "INVALID", 1552 "INVALID", "INVALID", "INVALID", "INVALID" }; 1553 1554 if (sm == NULL) 1555 return; 1556 1557 (void) printf("space map object %llu:\n", 1558 (longlong_t)sm->sm_object); 1559 (void) printf(" smp_length = 0x%llx\n", 1560 (longlong_t)sm->sm_phys->smp_length); 1561 (void) printf(" smp_alloc = 0x%llx\n", 1562 (longlong_t)sm->sm_phys->smp_alloc); 1563 1564 if (dump_opt['d'] < 6 && dump_opt['m'] < 4) 1565 return; 1566 1567 /* 1568 * Print out the freelist entries in both encoded and decoded form. 1569 */ 1570 uint8_t mapshift = sm->sm_shift; 1571 int64_t alloc = 0; 1572 uint64_t word, entry_id = 0; 1573 for (uint64_t offset = 0; offset < space_map_length(sm); 1574 offset += sizeof (word)) { 1575 1576 VERIFY0(dmu_read(os, space_map_object(sm), offset, 1577 sizeof (word), &word, DMU_READ_PREFETCH)); 1578 1579 if (sm_entry_is_debug(word)) { 1580 uint64_t de_txg = SM_DEBUG_TXG_DECODE(word); 1581 uint64_t de_sync_pass = SM_DEBUG_SYNCPASS_DECODE(word); 1582 if (de_txg == 0) { 1583 (void) printf( 1584 "\t [%6llu] PADDING\n", 1585 (u_longlong_t)entry_id); 1586 } else { 1587 (void) printf( 1588 "\t [%6llu] %s: txg %llu pass %llu\n", 1589 (u_longlong_t)entry_id, 1590 ddata[SM_DEBUG_ACTION_DECODE(word)], 1591 (u_longlong_t)de_txg, 1592 (u_longlong_t)de_sync_pass); 1593 } 1594 entry_id++; 1595 continue; 1596 } 1597 1598 uint8_t words; 1599 char entry_type; 1600 uint64_t entry_off, entry_run, entry_vdev = SM_NO_VDEVID; 1601 1602 if (sm_entry_is_single_word(word)) { 1603 entry_type = (SM_TYPE_DECODE(word) == SM_ALLOC) ? 1604 'A' : 'F'; 1605 entry_off = (SM_OFFSET_DECODE(word) << mapshift) + 1606 sm->sm_start; 1607 entry_run = SM_RUN_DECODE(word) << mapshift; 1608 words = 1; 1609 } else { 1610 /* it is a two-word entry so we read another word */ 1611 ASSERT(sm_entry_is_double_word(word)); 1612 1613 uint64_t extra_word; 1614 offset += sizeof (extra_word); 1615 VERIFY0(dmu_read(os, space_map_object(sm), offset, 1616 sizeof (extra_word), &extra_word, 1617 DMU_READ_PREFETCH)); 1618 1619 ASSERT3U(offset, <=, space_map_length(sm)); 1620 1621 entry_run = SM2_RUN_DECODE(word) << mapshift; 1622 entry_vdev = SM2_VDEV_DECODE(word); 1623 entry_type = (SM2_TYPE_DECODE(extra_word) == SM_ALLOC) ? 1624 'A' : 'F'; 1625 entry_off = (SM2_OFFSET_DECODE(extra_word) << 1626 mapshift) + sm->sm_start; 1627 words = 2; 1628 } 1629 1630 (void) printf("\t [%6llu] %c range:" 1631 " %010llx-%010llx size: %06llx vdev: %06llu words: %u\n", 1632 (u_longlong_t)entry_id, 1633 entry_type, (u_longlong_t)entry_off, 1634 (u_longlong_t)(entry_off + entry_run), 1635 (u_longlong_t)entry_run, 1636 (u_longlong_t)entry_vdev, words); 1637 1638 if (entry_type == 'A') 1639 alloc += entry_run; 1640 else 1641 alloc -= entry_run; 1642 entry_id++; 1643 } 1644 if (alloc != space_map_allocated(sm)) { 1645 (void) printf("space_map_object alloc (%lld) INCONSISTENT " 1646 "with space map summary (%lld)\n", 1647 (longlong_t)space_map_allocated(sm), (longlong_t)alloc); 1648 } 1649 } 1650 1651 static void 1652 dump_metaslab_stats(metaslab_t *msp) 1653 { 1654 char maxbuf[32]; 1655 range_tree_t *rt = msp->ms_allocatable; 1656 zfs_btree_t *t = &msp->ms_allocatable_by_size; 1657 int free_pct = range_tree_space(rt) * 100 / msp->ms_size; 1658 1659 /* max sure nicenum has enough space */ 1660 _Static_assert(sizeof (maxbuf) >= NN_NUMBUF_SZ, "maxbuf truncated"); 1661 1662 zdb_nicenum(metaslab_largest_allocatable(msp), maxbuf, sizeof (maxbuf)); 1663 1664 (void) printf("\t %25s %10lu %7s %6s %4s %4d%%\n", 1665 "segments", zfs_btree_numnodes(t), "maxsize", maxbuf, 1666 "freepct", free_pct); 1667 (void) printf("\tIn-memory histogram:\n"); 1668 dump_histogram(rt->rt_histogram, RANGE_TREE_HISTOGRAM_SIZE, 0); 1669 } 1670 1671 static void 1672 dump_metaslab(metaslab_t *msp) 1673 { 1674 vdev_t *vd = msp->ms_group->mg_vd; 1675 spa_t *spa = vd->vdev_spa; 1676 space_map_t *sm = msp->ms_sm; 1677 char freebuf[32]; 1678 1679 zdb_nicenum(msp->ms_size - space_map_allocated(sm), freebuf, 1680 sizeof (freebuf)); 1681 1682 (void) printf( 1683 "\tmetaslab %6llu offset %12llx spacemap %6llu free %5s\n", 1684 (u_longlong_t)msp->ms_id, (u_longlong_t)msp->ms_start, 1685 (u_longlong_t)space_map_object(sm), freebuf); 1686 1687 if (dump_opt['m'] > 2 && !dump_opt['L']) { 1688 mutex_enter(&msp->ms_lock); 1689 VERIFY0(metaslab_load(msp)); 1690 range_tree_stat_verify(msp->ms_allocatable); 1691 dump_metaslab_stats(msp); 1692 metaslab_unload(msp); 1693 mutex_exit(&msp->ms_lock); 1694 } 1695 1696 if (dump_opt['m'] > 1 && sm != NULL && 1697 spa_feature_is_active(spa, SPA_FEATURE_SPACEMAP_HISTOGRAM)) { 1698 /* 1699 * The space map histogram represents free space in chunks 1700 * of sm_shift (i.e. bucket 0 refers to 2^sm_shift). 1701 */ 1702 (void) printf("\tOn-disk histogram:\t\tfragmentation %llu\n", 1703 (u_longlong_t)msp->ms_fragmentation); 1704 dump_histogram(sm->sm_phys->smp_histogram, 1705 SPACE_MAP_HISTOGRAM_SIZE, sm->sm_shift); 1706 } 1707 1708 if (vd->vdev_ops == &vdev_draid_ops) 1709 ASSERT3U(msp->ms_size, <=, 1ULL << vd->vdev_ms_shift); 1710 else 1711 ASSERT3U(msp->ms_size, ==, 1ULL << vd->vdev_ms_shift); 1712 1713 dump_spacemap(spa->spa_meta_objset, msp->ms_sm); 1714 1715 if (spa_feature_is_active(spa, SPA_FEATURE_LOG_SPACEMAP)) { 1716 (void) printf("\tFlush data:\n\tunflushed txg=%llu\n\n", 1717 (u_longlong_t)metaslab_unflushed_txg(msp)); 1718 } 1719 } 1720 1721 static void 1722 print_vdev_metaslab_header(vdev_t *vd) 1723 { 1724 vdev_alloc_bias_t alloc_bias = vd->vdev_alloc_bias; 1725 const char *bias_str = ""; 1726 if (alloc_bias == VDEV_BIAS_LOG || vd->vdev_islog) { 1727 bias_str = VDEV_ALLOC_BIAS_LOG; 1728 } else if (alloc_bias == VDEV_BIAS_SPECIAL) { 1729 bias_str = VDEV_ALLOC_BIAS_SPECIAL; 1730 } else if (alloc_bias == VDEV_BIAS_DEDUP) { 1731 bias_str = VDEV_ALLOC_BIAS_DEDUP; 1732 } 1733 1734 uint64_t ms_flush_data_obj = 0; 1735 if (vd->vdev_top_zap != 0) { 1736 int error = zap_lookup(spa_meta_objset(vd->vdev_spa), 1737 vd->vdev_top_zap, VDEV_TOP_ZAP_MS_UNFLUSHED_PHYS_TXGS, 1738 sizeof (uint64_t), 1, &ms_flush_data_obj); 1739 if (error != ENOENT) { 1740 ASSERT0(error); 1741 } 1742 } 1743 1744 (void) printf("\tvdev %10llu %s", 1745 (u_longlong_t)vd->vdev_id, bias_str); 1746 1747 if (ms_flush_data_obj != 0) { 1748 (void) printf(" ms_unflushed_phys object %llu", 1749 (u_longlong_t)ms_flush_data_obj); 1750 } 1751 1752 (void) printf("\n\t%-10s%5llu %-19s %-15s %-12s\n", 1753 "metaslabs", (u_longlong_t)vd->vdev_ms_count, 1754 "offset", "spacemap", "free"); 1755 (void) printf("\t%15s %19s %15s %12s\n", 1756 "---------------", "-------------------", 1757 "---------------", "------------"); 1758 } 1759 1760 static void 1761 dump_metaslab_groups(spa_t *spa, boolean_t show_special) 1762 { 1763 vdev_t *rvd = spa->spa_root_vdev; 1764 metaslab_class_t *mc = spa_normal_class(spa); 1765 metaslab_class_t *smc = spa_special_class(spa); 1766 uint64_t fragmentation; 1767 1768 metaslab_class_histogram_verify(mc); 1769 1770 for (unsigned c = 0; c < rvd->vdev_children; c++) { 1771 vdev_t *tvd = rvd->vdev_child[c]; 1772 metaslab_group_t *mg = tvd->vdev_mg; 1773 1774 if (mg == NULL || (mg->mg_class != mc && 1775 (!show_special || mg->mg_class != smc))) 1776 continue; 1777 1778 metaslab_group_histogram_verify(mg); 1779 mg->mg_fragmentation = metaslab_group_fragmentation(mg); 1780 1781 (void) printf("\tvdev %10llu\t\tmetaslabs%5llu\t\t" 1782 "fragmentation", 1783 (u_longlong_t)tvd->vdev_id, 1784 (u_longlong_t)tvd->vdev_ms_count); 1785 if (mg->mg_fragmentation == ZFS_FRAG_INVALID) { 1786 (void) printf("%3s\n", "-"); 1787 } else { 1788 (void) printf("%3llu%%\n", 1789 (u_longlong_t)mg->mg_fragmentation); 1790 } 1791 dump_histogram(mg->mg_histogram, RANGE_TREE_HISTOGRAM_SIZE, 0); 1792 } 1793 1794 (void) printf("\tpool %s\tfragmentation", spa_name(spa)); 1795 fragmentation = metaslab_class_fragmentation(mc); 1796 if (fragmentation == ZFS_FRAG_INVALID) 1797 (void) printf("\t%3s\n", "-"); 1798 else 1799 (void) printf("\t%3llu%%\n", (u_longlong_t)fragmentation); 1800 dump_histogram(mc->mc_histogram, RANGE_TREE_HISTOGRAM_SIZE, 0); 1801 } 1802 1803 static void 1804 print_vdev_indirect(vdev_t *vd) 1805 { 1806 vdev_indirect_config_t *vic = &vd->vdev_indirect_config; 1807 vdev_indirect_mapping_t *vim = vd->vdev_indirect_mapping; 1808 vdev_indirect_births_t *vib = vd->vdev_indirect_births; 1809 1810 if (vim == NULL) { 1811 ASSERT3P(vib, ==, NULL); 1812 return; 1813 } 1814 1815 ASSERT3U(vdev_indirect_mapping_object(vim), ==, 1816 vic->vic_mapping_object); 1817 ASSERT3U(vdev_indirect_births_object(vib), ==, 1818 vic->vic_births_object); 1819 1820 (void) printf("indirect births obj %llu:\n", 1821 (longlong_t)vic->vic_births_object); 1822 (void) printf(" vib_count = %llu\n", 1823 (longlong_t)vdev_indirect_births_count(vib)); 1824 for (uint64_t i = 0; i < vdev_indirect_births_count(vib); i++) { 1825 vdev_indirect_birth_entry_phys_t *cur_vibe = 1826 &vib->vib_entries[i]; 1827 (void) printf("\toffset %llx -> txg %llu\n", 1828 (longlong_t)cur_vibe->vibe_offset, 1829 (longlong_t)cur_vibe->vibe_phys_birth_txg); 1830 } 1831 (void) printf("\n"); 1832 1833 (void) printf("indirect mapping obj %llu:\n", 1834 (longlong_t)vic->vic_mapping_object); 1835 (void) printf(" vim_max_offset = 0x%llx\n", 1836 (longlong_t)vdev_indirect_mapping_max_offset(vim)); 1837 (void) printf(" vim_bytes_mapped = 0x%llx\n", 1838 (longlong_t)vdev_indirect_mapping_bytes_mapped(vim)); 1839 (void) printf(" vim_count = %llu\n", 1840 (longlong_t)vdev_indirect_mapping_num_entries(vim)); 1841 1842 if (dump_opt['d'] <= 5 && dump_opt['m'] <= 3) 1843 return; 1844 1845 uint32_t *counts = vdev_indirect_mapping_load_obsolete_counts(vim); 1846 1847 for (uint64_t i = 0; i < vdev_indirect_mapping_num_entries(vim); i++) { 1848 vdev_indirect_mapping_entry_phys_t *vimep = 1849 &vim->vim_entries[i]; 1850 (void) printf("\t<%llx:%llx:%llx> -> " 1851 "<%llx:%llx:%llx> (%x obsolete)\n", 1852 (longlong_t)vd->vdev_id, 1853 (longlong_t)DVA_MAPPING_GET_SRC_OFFSET(vimep), 1854 (longlong_t)DVA_GET_ASIZE(&vimep->vimep_dst), 1855 (longlong_t)DVA_GET_VDEV(&vimep->vimep_dst), 1856 (longlong_t)DVA_GET_OFFSET(&vimep->vimep_dst), 1857 (longlong_t)DVA_GET_ASIZE(&vimep->vimep_dst), 1858 counts[i]); 1859 } 1860 (void) printf("\n"); 1861 1862 uint64_t obsolete_sm_object; 1863 VERIFY0(vdev_obsolete_sm_object(vd, &obsolete_sm_object)); 1864 if (obsolete_sm_object != 0) { 1865 objset_t *mos = vd->vdev_spa->spa_meta_objset; 1866 (void) printf("obsolete space map object %llu:\n", 1867 (u_longlong_t)obsolete_sm_object); 1868 ASSERT(vd->vdev_obsolete_sm != NULL); 1869 ASSERT3U(space_map_object(vd->vdev_obsolete_sm), ==, 1870 obsolete_sm_object); 1871 dump_spacemap(mos, vd->vdev_obsolete_sm); 1872 (void) printf("\n"); 1873 } 1874 } 1875 1876 static void 1877 dump_metaslabs(spa_t *spa) 1878 { 1879 vdev_t *vd, *rvd = spa->spa_root_vdev; 1880 uint64_t m, c = 0, children = rvd->vdev_children; 1881 1882 (void) printf("\nMetaslabs:\n"); 1883 1884 if (!dump_opt['d'] && zopt_metaslab_args > 0) { 1885 c = zopt_metaslab[0]; 1886 1887 if (c >= children) 1888 (void) fatal("bad vdev id: %llu", (u_longlong_t)c); 1889 1890 if (zopt_metaslab_args > 1) { 1891 vd = rvd->vdev_child[c]; 1892 print_vdev_metaslab_header(vd); 1893 1894 for (m = 1; m < zopt_metaslab_args; m++) { 1895 if (zopt_metaslab[m] < vd->vdev_ms_count) 1896 dump_metaslab( 1897 vd->vdev_ms[zopt_metaslab[m]]); 1898 else 1899 (void) fprintf(stderr, "bad metaslab " 1900 "number %llu\n", 1901 (u_longlong_t)zopt_metaslab[m]); 1902 } 1903 (void) printf("\n"); 1904 return; 1905 } 1906 children = c + 1; 1907 } 1908 for (; c < children; c++) { 1909 vd = rvd->vdev_child[c]; 1910 print_vdev_metaslab_header(vd); 1911 1912 print_vdev_indirect(vd); 1913 1914 for (m = 0; m < vd->vdev_ms_count; m++) 1915 dump_metaslab(vd->vdev_ms[m]); 1916 (void) printf("\n"); 1917 } 1918 } 1919 1920 static void 1921 dump_log_spacemaps(spa_t *spa) 1922 { 1923 if (!spa_feature_is_active(spa, SPA_FEATURE_LOG_SPACEMAP)) 1924 return; 1925 1926 (void) printf("\nLog Space Maps in Pool:\n"); 1927 for (spa_log_sm_t *sls = avl_first(&spa->spa_sm_logs_by_txg); 1928 sls; sls = AVL_NEXT(&spa->spa_sm_logs_by_txg, sls)) { 1929 space_map_t *sm = NULL; 1930 VERIFY0(space_map_open(&sm, spa_meta_objset(spa), 1931 sls->sls_sm_obj, 0, UINT64_MAX, SPA_MINBLOCKSHIFT)); 1932 1933 (void) printf("Log Spacemap object %llu txg %llu\n", 1934 (u_longlong_t)sls->sls_sm_obj, (u_longlong_t)sls->sls_txg); 1935 dump_spacemap(spa->spa_meta_objset, sm); 1936 space_map_close(sm); 1937 } 1938 (void) printf("\n"); 1939 } 1940 1941 static void 1942 dump_dde(const ddt_t *ddt, const ddt_entry_t *dde, uint64_t index) 1943 { 1944 const ddt_phys_t *ddp = dde->dde_phys; 1945 const ddt_key_t *ddk = &dde->dde_key; 1946 const char *types[4] = { "ditto", "single", "double", "triple" }; 1947 char blkbuf[BP_SPRINTF_LEN]; 1948 blkptr_t blk; 1949 int p; 1950 1951 for (p = 0; p < DDT_PHYS_TYPES; p++, ddp++) { 1952 if (ddp->ddp_phys_birth == 0) 1953 continue; 1954 ddt_bp_create(ddt->ddt_checksum, ddk, ddp, &blk); 1955 snprintf_blkptr(blkbuf, sizeof (blkbuf), &blk); 1956 (void) printf("index %llx refcnt %llu %s %s\n", 1957 (u_longlong_t)index, (u_longlong_t)ddp->ddp_refcnt, 1958 types[p], blkbuf); 1959 } 1960 } 1961 1962 static void 1963 dump_dedup_ratio(const ddt_stat_t *dds) 1964 { 1965 double rL, rP, rD, D, dedup, compress, copies; 1966 1967 if (dds->dds_blocks == 0) 1968 return; 1969 1970 rL = (double)dds->dds_ref_lsize; 1971 rP = (double)dds->dds_ref_psize; 1972 rD = (double)dds->dds_ref_dsize; 1973 D = (double)dds->dds_dsize; 1974 1975 dedup = rD / D; 1976 compress = rL / rP; 1977 copies = rD / rP; 1978 1979 (void) printf("dedup = %.2f, compress = %.2f, copies = %.2f, " 1980 "dedup * compress / copies = %.2f\n\n", 1981 dedup, compress, copies, dedup * compress / copies); 1982 } 1983 1984 static void 1985 dump_ddt(ddt_t *ddt, enum ddt_type type, enum ddt_class class) 1986 { 1987 char name[DDT_NAMELEN]; 1988 ddt_entry_t dde; 1989 uint64_t walk = 0; 1990 dmu_object_info_t doi; 1991 uint64_t count, dspace, mspace; 1992 int error; 1993 1994 error = ddt_object_info(ddt, type, class, &doi); 1995 1996 if (error == ENOENT) 1997 return; 1998 ASSERT(error == 0); 1999 2000 error = ddt_object_count(ddt, type, class, &count); 2001 ASSERT(error == 0); 2002 if (count == 0) 2003 return; 2004 2005 dspace = doi.doi_physical_blocks_512 << 9; 2006 mspace = doi.doi_fill_count * doi.doi_data_block_size; 2007 2008 ddt_object_name(ddt, type, class, name); 2009 2010 (void) printf("%s: %llu entries, size %llu on disk, %llu in core\n", 2011 name, 2012 (u_longlong_t)count, 2013 (u_longlong_t)(dspace / count), 2014 (u_longlong_t)(mspace / count)); 2015 2016 if (dump_opt['D'] < 3) 2017 return; 2018 2019 zpool_dump_ddt(NULL, &ddt->ddt_histogram[type][class]); 2020 2021 if (dump_opt['D'] < 4) 2022 return; 2023 2024 if (dump_opt['D'] < 5 && class == DDT_CLASS_UNIQUE) 2025 return; 2026 2027 (void) printf("%s contents:\n\n", name); 2028 2029 while ((error = ddt_object_walk(ddt, type, class, &walk, &dde)) == 0) 2030 dump_dde(ddt, &dde, walk); 2031 2032 ASSERT3U(error, ==, ENOENT); 2033 2034 (void) printf("\n"); 2035 } 2036 2037 static void 2038 dump_all_ddts(spa_t *spa) 2039 { 2040 ddt_histogram_t ddh_total = {{{0}}}; 2041 ddt_stat_t dds_total = {0}; 2042 2043 for (enum zio_checksum c = 0; c < ZIO_CHECKSUM_FUNCTIONS; c++) { 2044 ddt_t *ddt = spa->spa_ddt[c]; 2045 for (enum ddt_type type = 0; type < DDT_TYPES; type++) { 2046 for (enum ddt_class class = 0; class < DDT_CLASSES; 2047 class++) { 2048 dump_ddt(ddt, type, class); 2049 } 2050 } 2051 } 2052 2053 ddt_get_dedup_stats(spa, &dds_total); 2054 2055 if (dds_total.dds_blocks == 0) { 2056 (void) printf("All DDTs are empty\n"); 2057 return; 2058 } 2059 2060 (void) printf("\n"); 2061 2062 if (dump_opt['D'] > 1) { 2063 (void) printf("DDT histogram (aggregated over all DDTs):\n"); 2064 ddt_get_dedup_histogram(spa, &ddh_total); 2065 zpool_dump_ddt(&dds_total, &ddh_total); 2066 } 2067 2068 dump_dedup_ratio(&dds_total); 2069 } 2070 2071 static void 2072 dump_dtl_seg(void *arg, uint64_t start, uint64_t size) 2073 { 2074 char *prefix = arg; 2075 2076 (void) printf("%s [%llu,%llu) length %llu\n", 2077 prefix, 2078 (u_longlong_t)start, 2079 (u_longlong_t)(start + size), 2080 (u_longlong_t)(size)); 2081 } 2082 2083 static void 2084 dump_dtl(vdev_t *vd, int indent) 2085 { 2086 spa_t *spa = vd->vdev_spa; 2087 boolean_t required; 2088 const char *name[DTL_TYPES] = { "missing", "partial", "scrub", 2089 "outage" }; 2090 char prefix[256]; 2091 2092 spa_vdev_state_enter(spa, SCL_NONE); 2093 required = vdev_dtl_required(vd); 2094 (void) spa_vdev_state_exit(spa, NULL, 0); 2095 2096 if (indent == 0) 2097 (void) printf("\nDirty time logs:\n\n"); 2098 2099 (void) printf("\t%*s%s [%s]\n", indent, "", 2100 vd->vdev_path ? vd->vdev_path : 2101 vd->vdev_parent ? vd->vdev_ops->vdev_op_type : spa_name(spa), 2102 required ? "DTL-required" : "DTL-expendable"); 2103 2104 for (int t = 0; t < DTL_TYPES; t++) { 2105 range_tree_t *rt = vd->vdev_dtl[t]; 2106 if (range_tree_space(rt) == 0) 2107 continue; 2108 (void) snprintf(prefix, sizeof (prefix), "\t%*s%s", 2109 indent + 2, "", name[t]); 2110 range_tree_walk(rt, dump_dtl_seg, prefix); 2111 if (dump_opt['d'] > 5 && vd->vdev_children == 0) 2112 dump_spacemap(spa->spa_meta_objset, 2113 vd->vdev_dtl_sm); 2114 } 2115 2116 for (unsigned c = 0; c < vd->vdev_children; c++) 2117 dump_dtl(vd->vdev_child[c], indent + 4); 2118 } 2119 2120 static void 2121 dump_history(spa_t *spa) 2122 { 2123 nvlist_t **events = NULL; 2124 char *buf; 2125 uint64_t resid, len, off = 0; 2126 uint_t num = 0; 2127 int error; 2128 char tbuf[30]; 2129 2130 if ((buf = malloc(SPA_OLD_MAXBLOCKSIZE)) == NULL) { 2131 (void) fprintf(stderr, "%s: unable to allocate I/O buffer\n", 2132 __func__); 2133 return; 2134 } 2135 2136 do { 2137 len = SPA_OLD_MAXBLOCKSIZE; 2138 2139 if ((error = spa_history_get(spa, &off, &len, buf)) != 0) { 2140 (void) fprintf(stderr, "Unable to read history: " 2141 "error %d\n", error); 2142 free(buf); 2143 return; 2144 } 2145 2146 if (zpool_history_unpack(buf, len, &resid, &events, &num) != 0) 2147 break; 2148 2149 off -= resid; 2150 } while (len != 0); 2151 2152 (void) printf("\nHistory:\n"); 2153 for (unsigned i = 0; i < num; i++) { 2154 boolean_t printed = B_FALSE; 2155 2156 if (nvlist_exists(events[i], ZPOOL_HIST_TIME)) { 2157 time_t tsec; 2158 struct tm t; 2159 2160 tsec = fnvlist_lookup_uint64(events[i], 2161 ZPOOL_HIST_TIME); 2162 (void) localtime_r(&tsec, &t); 2163 (void) strftime(tbuf, sizeof (tbuf), "%F.%T", &t); 2164 } else { 2165 tbuf[0] = '\0'; 2166 } 2167 2168 if (nvlist_exists(events[i], ZPOOL_HIST_CMD)) { 2169 (void) printf("%s %s\n", tbuf, 2170 fnvlist_lookup_string(events[i], ZPOOL_HIST_CMD)); 2171 } else if (nvlist_exists(events[i], ZPOOL_HIST_INT_EVENT)) { 2172 uint64_t ievent; 2173 2174 ievent = fnvlist_lookup_uint64(events[i], 2175 ZPOOL_HIST_INT_EVENT); 2176 if (ievent >= ZFS_NUM_LEGACY_HISTORY_EVENTS) 2177 goto next; 2178 2179 (void) printf(" %s [internal %s txg:%ju] %s\n", 2180 tbuf, 2181 zfs_history_event_names[ievent], 2182 fnvlist_lookup_uint64(events[i], 2183 ZPOOL_HIST_TXG), 2184 fnvlist_lookup_string(events[i], 2185 ZPOOL_HIST_INT_STR)); 2186 } else if (nvlist_exists(events[i], ZPOOL_HIST_INT_NAME)) { 2187 (void) printf("%s [txg:%ju] %s", tbuf, 2188 fnvlist_lookup_uint64(events[i], 2189 ZPOOL_HIST_TXG), 2190 fnvlist_lookup_string(events[i], 2191 ZPOOL_HIST_INT_NAME)); 2192 2193 if (nvlist_exists(events[i], ZPOOL_HIST_DSNAME)) { 2194 (void) printf(" %s (%llu)", 2195 fnvlist_lookup_string(events[i], 2196 ZPOOL_HIST_DSNAME), 2197 (u_longlong_t)fnvlist_lookup_uint64( 2198 events[i], 2199 ZPOOL_HIST_DSID)); 2200 } 2201 2202 (void) printf(" %s\n", fnvlist_lookup_string(events[i], 2203 ZPOOL_HIST_INT_STR)); 2204 } else if (nvlist_exists(events[i], ZPOOL_HIST_IOCTL)) { 2205 (void) printf("%s ioctl %s\n", tbuf, 2206 fnvlist_lookup_string(events[i], 2207 ZPOOL_HIST_IOCTL)); 2208 2209 if (nvlist_exists(events[i], ZPOOL_HIST_INPUT_NVL)) { 2210 (void) printf(" input:\n"); 2211 dump_nvlist(fnvlist_lookup_nvlist(events[i], 2212 ZPOOL_HIST_INPUT_NVL), 8); 2213 } 2214 if (nvlist_exists(events[i], ZPOOL_HIST_OUTPUT_NVL)) { 2215 (void) printf(" output:\n"); 2216 dump_nvlist(fnvlist_lookup_nvlist(events[i], 2217 ZPOOL_HIST_OUTPUT_NVL), 8); 2218 } 2219 if (nvlist_exists(events[i], ZPOOL_HIST_ERRNO)) { 2220 (void) printf(" errno: %lld\n", 2221 (longlong_t)fnvlist_lookup_int64(events[i], 2222 ZPOOL_HIST_ERRNO)); 2223 } 2224 } else { 2225 goto next; 2226 } 2227 2228 printed = B_TRUE; 2229 next: 2230 if (dump_opt['h'] > 1) { 2231 if (!printed) 2232 (void) printf("unrecognized record:\n"); 2233 dump_nvlist(events[i], 2); 2234 } 2235 } 2236 free(buf); 2237 } 2238 2239 static void 2240 dump_dnode(objset_t *os, uint64_t object, void *data, size_t size) 2241 { 2242 (void) os, (void) object, (void) data, (void) size; 2243 } 2244 2245 static uint64_t 2246 blkid2offset(const dnode_phys_t *dnp, const blkptr_t *bp, 2247 const zbookmark_phys_t *zb) 2248 { 2249 if (dnp == NULL) { 2250 ASSERT(zb->zb_level < 0); 2251 if (zb->zb_object == 0) 2252 return (zb->zb_blkid); 2253 return (zb->zb_blkid * BP_GET_LSIZE(bp)); 2254 } 2255 2256 ASSERT(zb->zb_level >= 0); 2257 2258 return ((zb->zb_blkid << 2259 (zb->zb_level * (dnp->dn_indblkshift - SPA_BLKPTRSHIFT))) * 2260 dnp->dn_datablkszsec << SPA_MINBLOCKSHIFT); 2261 } 2262 2263 static void 2264 snprintf_zstd_header(spa_t *spa, char *blkbuf, size_t buflen, 2265 const blkptr_t *bp) 2266 { 2267 abd_t *pabd; 2268 void *buf; 2269 zio_t *zio; 2270 zfs_zstdhdr_t zstd_hdr; 2271 int error; 2272 2273 if (BP_GET_COMPRESS(bp) != ZIO_COMPRESS_ZSTD) 2274 return; 2275 2276 if (BP_IS_HOLE(bp)) 2277 return; 2278 2279 if (BP_IS_EMBEDDED(bp)) { 2280 buf = malloc(SPA_MAXBLOCKSIZE); 2281 if (buf == NULL) { 2282 (void) fprintf(stderr, "out of memory\n"); 2283 exit(1); 2284 } 2285 decode_embedded_bp_compressed(bp, buf); 2286 memcpy(&zstd_hdr, buf, sizeof (zstd_hdr)); 2287 free(buf); 2288 zstd_hdr.c_len = BE_32(zstd_hdr.c_len); 2289 zstd_hdr.raw_version_level = BE_32(zstd_hdr.raw_version_level); 2290 (void) snprintf(blkbuf + strlen(blkbuf), 2291 buflen - strlen(blkbuf), 2292 " ZSTD:size=%u:version=%u:level=%u:EMBEDDED", 2293 zstd_hdr.c_len, zfs_get_hdrversion(&zstd_hdr), 2294 zfs_get_hdrlevel(&zstd_hdr)); 2295 return; 2296 } 2297 2298 pabd = abd_alloc_for_io(SPA_MAXBLOCKSIZE, B_FALSE); 2299 zio = zio_root(spa, NULL, NULL, 0); 2300 2301 /* Decrypt but don't decompress so we can read the compression header */ 2302 zio_nowait(zio_read(zio, spa, bp, pabd, BP_GET_PSIZE(bp), NULL, NULL, 2303 ZIO_PRIORITY_SYNC_READ, ZIO_FLAG_CANFAIL | ZIO_FLAG_RAW_COMPRESS, 2304 NULL)); 2305 error = zio_wait(zio); 2306 if (error) { 2307 (void) fprintf(stderr, "read failed: %d\n", error); 2308 return; 2309 } 2310 buf = abd_borrow_buf_copy(pabd, BP_GET_LSIZE(bp)); 2311 memcpy(&zstd_hdr, buf, sizeof (zstd_hdr)); 2312 zstd_hdr.c_len = BE_32(zstd_hdr.c_len); 2313 zstd_hdr.raw_version_level = BE_32(zstd_hdr.raw_version_level); 2314 2315 (void) snprintf(blkbuf + strlen(blkbuf), 2316 buflen - strlen(blkbuf), 2317 " ZSTD:size=%u:version=%u:level=%u:NORMAL", 2318 zstd_hdr.c_len, zfs_get_hdrversion(&zstd_hdr), 2319 zfs_get_hdrlevel(&zstd_hdr)); 2320 2321 abd_return_buf_copy(pabd, buf, BP_GET_LSIZE(bp)); 2322 } 2323 2324 static void 2325 snprintf_blkptr_compact(char *blkbuf, size_t buflen, const blkptr_t *bp, 2326 boolean_t bp_freed) 2327 { 2328 const dva_t *dva = bp->blk_dva; 2329 int ndvas = dump_opt['d'] > 5 ? BP_GET_NDVAS(bp) : 1; 2330 int i; 2331 2332 if (dump_opt['b'] >= 6) { 2333 snprintf_blkptr(blkbuf, buflen, bp); 2334 if (bp_freed) { 2335 (void) snprintf(blkbuf + strlen(blkbuf), 2336 buflen - strlen(blkbuf), " %s", "FREE"); 2337 } 2338 return; 2339 } 2340 2341 if (BP_IS_EMBEDDED(bp)) { 2342 (void) sprintf(blkbuf, 2343 "EMBEDDED et=%u %llxL/%llxP B=%llu", 2344 (int)BPE_GET_ETYPE(bp), 2345 (u_longlong_t)BPE_GET_LSIZE(bp), 2346 (u_longlong_t)BPE_GET_PSIZE(bp), 2347 (u_longlong_t)bp->blk_birth); 2348 return; 2349 } 2350 2351 blkbuf[0] = '\0'; 2352 2353 for (i = 0; i < ndvas; i++) 2354 (void) snprintf(blkbuf + strlen(blkbuf), 2355 buflen - strlen(blkbuf), "%llu:%llx:%llx ", 2356 (u_longlong_t)DVA_GET_VDEV(&dva[i]), 2357 (u_longlong_t)DVA_GET_OFFSET(&dva[i]), 2358 (u_longlong_t)DVA_GET_ASIZE(&dva[i])); 2359 2360 if (BP_IS_HOLE(bp)) { 2361 (void) snprintf(blkbuf + strlen(blkbuf), 2362 buflen - strlen(blkbuf), 2363 "%llxL B=%llu", 2364 (u_longlong_t)BP_GET_LSIZE(bp), 2365 (u_longlong_t)bp->blk_birth); 2366 } else { 2367 (void) snprintf(blkbuf + strlen(blkbuf), 2368 buflen - strlen(blkbuf), 2369 "%llxL/%llxP F=%llu B=%llu/%llu", 2370 (u_longlong_t)BP_GET_LSIZE(bp), 2371 (u_longlong_t)BP_GET_PSIZE(bp), 2372 (u_longlong_t)BP_GET_FILL(bp), 2373 (u_longlong_t)bp->blk_birth, 2374 (u_longlong_t)BP_PHYSICAL_BIRTH(bp)); 2375 if (bp_freed) 2376 (void) snprintf(blkbuf + strlen(blkbuf), 2377 buflen - strlen(blkbuf), " %s", "FREE"); 2378 (void) snprintf(blkbuf + strlen(blkbuf), 2379 buflen - strlen(blkbuf), " cksum=%llx:%llx:%llx:%llx", 2380 (u_longlong_t)bp->blk_cksum.zc_word[0], 2381 (u_longlong_t)bp->blk_cksum.zc_word[1], 2382 (u_longlong_t)bp->blk_cksum.zc_word[2], 2383 (u_longlong_t)bp->blk_cksum.zc_word[3]); 2384 } 2385 } 2386 2387 static void 2388 print_indirect(spa_t *spa, blkptr_t *bp, const zbookmark_phys_t *zb, 2389 const dnode_phys_t *dnp) 2390 { 2391 char blkbuf[BP_SPRINTF_LEN]; 2392 int l; 2393 2394 if (!BP_IS_EMBEDDED(bp)) { 2395 ASSERT3U(BP_GET_TYPE(bp), ==, dnp->dn_type); 2396 ASSERT3U(BP_GET_LEVEL(bp), ==, zb->zb_level); 2397 } 2398 2399 (void) printf("%16llx ", (u_longlong_t)blkid2offset(dnp, bp, zb)); 2400 2401 ASSERT(zb->zb_level >= 0); 2402 2403 for (l = dnp->dn_nlevels - 1; l >= -1; l--) { 2404 if (l == zb->zb_level) { 2405 (void) printf("L%llx", (u_longlong_t)zb->zb_level); 2406 } else { 2407 (void) printf(" "); 2408 } 2409 } 2410 2411 snprintf_blkptr_compact(blkbuf, sizeof (blkbuf), bp, B_FALSE); 2412 if (dump_opt['Z'] && BP_GET_COMPRESS(bp) == ZIO_COMPRESS_ZSTD) 2413 snprintf_zstd_header(spa, blkbuf, sizeof (blkbuf), bp); 2414 (void) printf("%s\n", blkbuf); 2415 } 2416 2417 static int 2418 visit_indirect(spa_t *spa, const dnode_phys_t *dnp, 2419 blkptr_t *bp, const zbookmark_phys_t *zb) 2420 { 2421 int err = 0; 2422 2423 if (bp->blk_birth == 0) 2424 return (0); 2425 2426 print_indirect(spa, bp, zb, dnp); 2427 2428 if (BP_GET_LEVEL(bp) > 0 && !BP_IS_HOLE(bp)) { 2429 arc_flags_t flags = ARC_FLAG_WAIT; 2430 int i; 2431 blkptr_t *cbp; 2432 int epb = BP_GET_LSIZE(bp) >> SPA_BLKPTRSHIFT; 2433 arc_buf_t *buf; 2434 uint64_t fill = 0; 2435 ASSERT(!BP_IS_REDACTED(bp)); 2436 2437 err = arc_read(NULL, spa, bp, arc_getbuf_func, &buf, 2438 ZIO_PRIORITY_ASYNC_READ, ZIO_FLAG_CANFAIL, &flags, zb); 2439 if (err) 2440 return (err); 2441 ASSERT(buf->b_data); 2442 2443 /* recursively visit blocks below this */ 2444 cbp = buf->b_data; 2445 for (i = 0; i < epb; i++, cbp++) { 2446 zbookmark_phys_t czb; 2447 2448 SET_BOOKMARK(&czb, zb->zb_objset, zb->zb_object, 2449 zb->zb_level - 1, 2450 zb->zb_blkid * epb + i); 2451 err = visit_indirect(spa, dnp, cbp, &czb); 2452 if (err) 2453 break; 2454 fill += BP_GET_FILL(cbp); 2455 } 2456 if (!err) 2457 ASSERT3U(fill, ==, BP_GET_FILL(bp)); 2458 arc_buf_destroy(buf, &buf); 2459 } 2460 2461 return (err); 2462 } 2463 2464 static void 2465 dump_indirect(dnode_t *dn) 2466 { 2467 dnode_phys_t *dnp = dn->dn_phys; 2468 zbookmark_phys_t czb; 2469 2470 (void) printf("Indirect blocks:\n"); 2471 2472 SET_BOOKMARK(&czb, dmu_objset_id(dn->dn_objset), 2473 dn->dn_object, dnp->dn_nlevels - 1, 0); 2474 for (int j = 0; j < dnp->dn_nblkptr; j++) { 2475 czb.zb_blkid = j; 2476 (void) visit_indirect(dmu_objset_spa(dn->dn_objset), dnp, 2477 &dnp->dn_blkptr[j], &czb); 2478 } 2479 2480 (void) printf("\n"); 2481 } 2482 2483 static void 2484 dump_dsl_dir(objset_t *os, uint64_t object, void *data, size_t size) 2485 { 2486 (void) os, (void) object; 2487 dsl_dir_phys_t *dd = data; 2488 time_t crtime; 2489 char nice[32]; 2490 2491 /* make sure nicenum has enough space */ 2492 _Static_assert(sizeof (nice) >= NN_NUMBUF_SZ, "nice truncated"); 2493 2494 if (dd == NULL) 2495 return; 2496 2497 ASSERT3U(size, >=, sizeof (dsl_dir_phys_t)); 2498 2499 crtime = dd->dd_creation_time; 2500 (void) printf("\t\tcreation_time = %s", ctime(&crtime)); 2501 (void) printf("\t\thead_dataset_obj = %llu\n", 2502 (u_longlong_t)dd->dd_head_dataset_obj); 2503 (void) printf("\t\tparent_dir_obj = %llu\n", 2504 (u_longlong_t)dd->dd_parent_obj); 2505 (void) printf("\t\torigin_obj = %llu\n", 2506 (u_longlong_t)dd->dd_origin_obj); 2507 (void) printf("\t\tchild_dir_zapobj = %llu\n", 2508 (u_longlong_t)dd->dd_child_dir_zapobj); 2509 zdb_nicenum(dd->dd_used_bytes, nice, sizeof (nice)); 2510 (void) printf("\t\tused_bytes = %s\n", nice); 2511 zdb_nicenum(dd->dd_compressed_bytes, nice, sizeof (nice)); 2512 (void) printf("\t\tcompressed_bytes = %s\n", nice); 2513 zdb_nicenum(dd->dd_uncompressed_bytes, nice, sizeof (nice)); 2514 (void) printf("\t\tuncompressed_bytes = %s\n", nice); 2515 zdb_nicenum(dd->dd_quota, nice, sizeof (nice)); 2516 (void) printf("\t\tquota = %s\n", nice); 2517 zdb_nicenum(dd->dd_reserved, nice, sizeof (nice)); 2518 (void) printf("\t\treserved = %s\n", nice); 2519 (void) printf("\t\tprops_zapobj = %llu\n", 2520 (u_longlong_t)dd->dd_props_zapobj); 2521 (void) printf("\t\tdeleg_zapobj = %llu\n", 2522 (u_longlong_t)dd->dd_deleg_zapobj); 2523 (void) printf("\t\tflags = %llx\n", 2524 (u_longlong_t)dd->dd_flags); 2525 2526 #define DO(which) \ 2527 zdb_nicenum(dd->dd_used_breakdown[DD_USED_ ## which], nice, \ 2528 sizeof (nice)); \ 2529 (void) printf("\t\tused_breakdown[" #which "] = %s\n", nice) 2530 DO(HEAD); 2531 DO(SNAP); 2532 DO(CHILD); 2533 DO(CHILD_RSRV); 2534 DO(REFRSRV); 2535 #undef DO 2536 (void) printf("\t\tclones = %llu\n", 2537 (u_longlong_t)dd->dd_clones); 2538 } 2539 2540 static void 2541 dump_dsl_dataset(objset_t *os, uint64_t object, void *data, size_t size) 2542 { 2543 (void) os, (void) object; 2544 dsl_dataset_phys_t *ds = data; 2545 time_t crtime; 2546 char used[32], compressed[32], uncompressed[32], unique[32]; 2547 char blkbuf[BP_SPRINTF_LEN]; 2548 2549 /* make sure nicenum has enough space */ 2550 _Static_assert(sizeof (used) >= NN_NUMBUF_SZ, "used truncated"); 2551 _Static_assert(sizeof (compressed) >= NN_NUMBUF_SZ, 2552 "compressed truncated"); 2553 _Static_assert(sizeof (uncompressed) >= NN_NUMBUF_SZ, 2554 "uncompressed truncated"); 2555 _Static_assert(sizeof (unique) >= NN_NUMBUF_SZ, "unique truncated"); 2556 2557 if (ds == NULL) 2558 return; 2559 2560 ASSERT(size == sizeof (*ds)); 2561 crtime = ds->ds_creation_time; 2562 zdb_nicenum(ds->ds_referenced_bytes, used, sizeof (used)); 2563 zdb_nicenum(ds->ds_compressed_bytes, compressed, sizeof (compressed)); 2564 zdb_nicenum(ds->ds_uncompressed_bytes, uncompressed, 2565 sizeof (uncompressed)); 2566 zdb_nicenum(ds->ds_unique_bytes, unique, sizeof (unique)); 2567 snprintf_blkptr(blkbuf, sizeof (blkbuf), &ds->ds_bp); 2568 2569 (void) printf("\t\tdir_obj = %llu\n", 2570 (u_longlong_t)ds->ds_dir_obj); 2571 (void) printf("\t\tprev_snap_obj = %llu\n", 2572 (u_longlong_t)ds->ds_prev_snap_obj); 2573 (void) printf("\t\tprev_snap_txg = %llu\n", 2574 (u_longlong_t)ds->ds_prev_snap_txg); 2575 (void) printf("\t\tnext_snap_obj = %llu\n", 2576 (u_longlong_t)ds->ds_next_snap_obj); 2577 (void) printf("\t\tsnapnames_zapobj = %llu\n", 2578 (u_longlong_t)ds->ds_snapnames_zapobj); 2579 (void) printf("\t\tnum_children = %llu\n", 2580 (u_longlong_t)ds->ds_num_children); 2581 (void) printf("\t\tuserrefs_obj = %llu\n", 2582 (u_longlong_t)ds->ds_userrefs_obj); 2583 (void) printf("\t\tcreation_time = %s", ctime(&crtime)); 2584 (void) printf("\t\tcreation_txg = %llu\n", 2585 (u_longlong_t)ds->ds_creation_txg); 2586 (void) printf("\t\tdeadlist_obj = %llu\n", 2587 (u_longlong_t)ds->ds_deadlist_obj); 2588 (void) printf("\t\tused_bytes = %s\n", used); 2589 (void) printf("\t\tcompressed_bytes = %s\n", compressed); 2590 (void) printf("\t\tuncompressed_bytes = %s\n", uncompressed); 2591 (void) printf("\t\tunique = %s\n", unique); 2592 (void) printf("\t\tfsid_guid = %llu\n", 2593 (u_longlong_t)ds->ds_fsid_guid); 2594 (void) printf("\t\tguid = %llu\n", 2595 (u_longlong_t)ds->ds_guid); 2596 (void) printf("\t\tflags = %llx\n", 2597 (u_longlong_t)ds->ds_flags); 2598 (void) printf("\t\tnext_clones_obj = %llu\n", 2599 (u_longlong_t)ds->ds_next_clones_obj); 2600 (void) printf("\t\tprops_obj = %llu\n", 2601 (u_longlong_t)ds->ds_props_obj); 2602 (void) printf("\t\tbp = %s\n", blkbuf); 2603 } 2604 2605 static int 2606 dump_bptree_cb(void *arg, const blkptr_t *bp, dmu_tx_t *tx) 2607 { 2608 (void) arg, (void) tx; 2609 char blkbuf[BP_SPRINTF_LEN]; 2610 2611 if (bp->blk_birth != 0) { 2612 snprintf_blkptr(blkbuf, sizeof (blkbuf), bp); 2613 (void) printf("\t%s\n", blkbuf); 2614 } 2615 return (0); 2616 } 2617 2618 static void 2619 dump_bptree(objset_t *os, uint64_t obj, const char *name) 2620 { 2621 char bytes[32]; 2622 bptree_phys_t *bt; 2623 dmu_buf_t *db; 2624 2625 /* make sure nicenum has enough space */ 2626 _Static_assert(sizeof (bytes) >= NN_NUMBUF_SZ, "bytes truncated"); 2627 2628 if (dump_opt['d'] < 3) 2629 return; 2630 2631 VERIFY3U(0, ==, dmu_bonus_hold(os, obj, FTAG, &db)); 2632 bt = db->db_data; 2633 zdb_nicenum(bt->bt_bytes, bytes, sizeof (bytes)); 2634 (void) printf("\n %s: %llu datasets, %s\n", 2635 name, (unsigned long long)(bt->bt_end - bt->bt_begin), bytes); 2636 dmu_buf_rele(db, FTAG); 2637 2638 if (dump_opt['d'] < 5) 2639 return; 2640 2641 (void) printf("\n"); 2642 2643 (void) bptree_iterate(os, obj, B_FALSE, dump_bptree_cb, NULL, NULL); 2644 } 2645 2646 static int 2647 dump_bpobj_cb(void *arg, const blkptr_t *bp, boolean_t bp_freed, dmu_tx_t *tx) 2648 { 2649 (void) arg, (void) tx; 2650 char blkbuf[BP_SPRINTF_LEN]; 2651 2652 ASSERT(bp->blk_birth != 0); 2653 snprintf_blkptr_compact(blkbuf, sizeof (blkbuf), bp, bp_freed); 2654 (void) printf("\t%s\n", blkbuf); 2655 return (0); 2656 } 2657 2658 static void 2659 dump_full_bpobj(bpobj_t *bpo, const char *name, int indent) 2660 { 2661 char bytes[32]; 2662 char comp[32]; 2663 char uncomp[32]; 2664 uint64_t i; 2665 2666 /* make sure nicenum has enough space */ 2667 _Static_assert(sizeof (bytes) >= NN_NUMBUF_SZ, "bytes truncated"); 2668 _Static_assert(sizeof (comp) >= NN_NUMBUF_SZ, "comp truncated"); 2669 _Static_assert(sizeof (uncomp) >= NN_NUMBUF_SZ, "uncomp truncated"); 2670 2671 if (dump_opt['d'] < 3) 2672 return; 2673 2674 zdb_nicenum(bpo->bpo_phys->bpo_bytes, bytes, sizeof (bytes)); 2675 if (bpo->bpo_havesubobj && bpo->bpo_phys->bpo_subobjs != 0) { 2676 zdb_nicenum(bpo->bpo_phys->bpo_comp, comp, sizeof (comp)); 2677 zdb_nicenum(bpo->bpo_phys->bpo_uncomp, uncomp, sizeof (uncomp)); 2678 if (bpo->bpo_havefreed) { 2679 (void) printf(" %*s: object %llu, %llu local " 2680 "blkptrs, %llu freed, %llu subobjs in object %llu, " 2681 "%s (%s/%s comp)\n", 2682 indent * 8, name, 2683 (u_longlong_t)bpo->bpo_object, 2684 (u_longlong_t)bpo->bpo_phys->bpo_num_blkptrs, 2685 (u_longlong_t)bpo->bpo_phys->bpo_num_freed, 2686 (u_longlong_t)bpo->bpo_phys->bpo_num_subobjs, 2687 (u_longlong_t)bpo->bpo_phys->bpo_subobjs, 2688 bytes, comp, uncomp); 2689 } else { 2690 (void) printf(" %*s: object %llu, %llu local " 2691 "blkptrs, %llu subobjs in object %llu, " 2692 "%s (%s/%s comp)\n", 2693 indent * 8, name, 2694 (u_longlong_t)bpo->bpo_object, 2695 (u_longlong_t)bpo->bpo_phys->bpo_num_blkptrs, 2696 (u_longlong_t)bpo->bpo_phys->bpo_num_subobjs, 2697 (u_longlong_t)bpo->bpo_phys->bpo_subobjs, 2698 bytes, comp, uncomp); 2699 } 2700 2701 for (i = 0; i < bpo->bpo_phys->bpo_num_subobjs; i++) { 2702 uint64_t subobj; 2703 bpobj_t subbpo; 2704 int error; 2705 VERIFY0(dmu_read(bpo->bpo_os, 2706 bpo->bpo_phys->bpo_subobjs, 2707 i * sizeof (subobj), sizeof (subobj), &subobj, 0)); 2708 error = bpobj_open(&subbpo, bpo->bpo_os, subobj); 2709 if (error != 0) { 2710 (void) printf("ERROR %u while trying to open " 2711 "subobj id %llu\n", 2712 error, (u_longlong_t)subobj); 2713 continue; 2714 } 2715 dump_full_bpobj(&subbpo, "subobj", indent + 1); 2716 bpobj_close(&subbpo); 2717 } 2718 } else { 2719 if (bpo->bpo_havefreed) { 2720 (void) printf(" %*s: object %llu, %llu blkptrs, " 2721 "%llu freed, %s\n", 2722 indent * 8, name, 2723 (u_longlong_t)bpo->bpo_object, 2724 (u_longlong_t)bpo->bpo_phys->bpo_num_blkptrs, 2725 (u_longlong_t)bpo->bpo_phys->bpo_num_freed, 2726 bytes); 2727 } else { 2728 (void) printf(" %*s: object %llu, %llu blkptrs, " 2729 "%s\n", 2730 indent * 8, name, 2731 (u_longlong_t)bpo->bpo_object, 2732 (u_longlong_t)bpo->bpo_phys->bpo_num_blkptrs, 2733 bytes); 2734 } 2735 } 2736 2737 if (dump_opt['d'] < 5) 2738 return; 2739 2740 2741 if (indent == 0) { 2742 (void) bpobj_iterate_nofree(bpo, dump_bpobj_cb, NULL, NULL); 2743 (void) printf("\n"); 2744 } 2745 } 2746 2747 static int 2748 dump_bookmark(dsl_pool_t *dp, char *name, boolean_t print_redact, 2749 boolean_t print_list) 2750 { 2751 int err = 0; 2752 zfs_bookmark_phys_t prop; 2753 objset_t *mos = dp->dp_spa->spa_meta_objset; 2754 err = dsl_bookmark_lookup(dp, name, NULL, &prop); 2755 2756 if (err != 0) { 2757 return (err); 2758 } 2759 2760 (void) printf("\t#%s: ", strchr(name, '#') + 1); 2761 (void) printf("{guid: %llx creation_txg: %llu creation_time: " 2762 "%llu redaction_obj: %llu}\n", (u_longlong_t)prop.zbm_guid, 2763 (u_longlong_t)prop.zbm_creation_txg, 2764 (u_longlong_t)prop.zbm_creation_time, 2765 (u_longlong_t)prop.zbm_redaction_obj); 2766 2767 IMPLY(print_list, print_redact); 2768 if (!print_redact || prop.zbm_redaction_obj == 0) 2769 return (0); 2770 2771 redaction_list_t *rl; 2772 VERIFY0(dsl_redaction_list_hold_obj(dp, 2773 prop.zbm_redaction_obj, FTAG, &rl)); 2774 2775 redaction_list_phys_t *rlp = rl->rl_phys; 2776 (void) printf("\tRedacted:\n\t\tProgress: "); 2777 if (rlp->rlp_last_object != UINT64_MAX || 2778 rlp->rlp_last_blkid != UINT64_MAX) { 2779 (void) printf("%llu %llu (incomplete)\n", 2780 (u_longlong_t)rlp->rlp_last_object, 2781 (u_longlong_t)rlp->rlp_last_blkid); 2782 } else { 2783 (void) printf("complete\n"); 2784 } 2785 (void) printf("\t\tSnapshots: ["); 2786 for (unsigned int i = 0; i < rlp->rlp_num_snaps; i++) { 2787 if (i > 0) 2788 (void) printf(", "); 2789 (void) printf("%0llu", 2790 (u_longlong_t)rlp->rlp_snaps[i]); 2791 } 2792 (void) printf("]\n\t\tLength: %llu\n", 2793 (u_longlong_t)rlp->rlp_num_entries); 2794 2795 if (!print_list) { 2796 dsl_redaction_list_rele(rl, FTAG); 2797 return (0); 2798 } 2799 2800 if (rlp->rlp_num_entries == 0) { 2801 dsl_redaction_list_rele(rl, FTAG); 2802 (void) printf("\t\tRedaction List: []\n\n"); 2803 return (0); 2804 } 2805 2806 redact_block_phys_t *rbp_buf; 2807 uint64_t size; 2808 dmu_object_info_t doi; 2809 2810 VERIFY0(dmu_object_info(mos, prop.zbm_redaction_obj, &doi)); 2811 size = doi.doi_max_offset; 2812 rbp_buf = kmem_alloc(size, KM_SLEEP); 2813 2814 err = dmu_read(mos, prop.zbm_redaction_obj, 0, size, 2815 rbp_buf, 0); 2816 if (err != 0) { 2817 dsl_redaction_list_rele(rl, FTAG); 2818 kmem_free(rbp_buf, size); 2819 return (err); 2820 } 2821 2822 (void) printf("\t\tRedaction List: [{object: %llx, offset: " 2823 "%llx, blksz: %x, count: %llx}", 2824 (u_longlong_t)rbp_buf[0].rbp_object, 2825 (u_longlong_t)rbp_buf[0].rbp_blkid, 2826 (uint_t)(redact_block_get_size(&rbp_buf[0])), 2827 (u_longlong_t)redact_block_get_count(&rbp_buf[0])); 2828 2829 for (size_t i = 1; i < rlp->rlp_num_entries; i++) { 2830 (void) printf(",\n\t\t{object: %llx, offset: %llx, " 2831 "blksz: %x, count: %llx}", 2832 (u_longlong_t)rbp_buf[i].rbp_object, 2833 (u_longlong_t)rbp_buf[i].rbp_blkid, 2834 (uint_t)(redact_block_get_size(&rbp_buf[i])), 2835 (u_longlong_t)redact_block_get_count(&rbp_buf[i])); 2836 } 2837 dsl_redaction_list_rele(rl, FTAG); 2838 kmem_free(rbp_buf, size); 2839 (void) printf("]\n\n"); 2840 return (0); 2841 } 2842 2843 static void 2844 dump_bookmarks(objset_t *os, int verbosity) 2845 { 2846 zap_cursor_t zc; 2847 zap_attribute_t attr; 2848 dsl_dataset_t *ds = dmu_objset_ds(os); 2849 dsl_pool_t *dp = spa_get_dsl(os->os_spa); 2850 objset_t *mos = os->os_spa->spa_meta_objset; 2851 if (verbosity < 4) 2852 return; 2853 dsl_pool_config_enter(dp, FTAG); 2854 2855 for (zap_cursor_init(&zc, mos, ds->ds_bookmarks_obj); 2856 zap_cursor_retrieve(&zc, &attr) == 0; 2857 zap_cursor_advance(&zc)) { 2858 char osname[ZFS_MAX_DATASET_NAME_LEN]; 2859 char buf[ZFS_MAX_DATASET_NAME_LEN]; 2860 dmu_objset_name(os, osname); 2861 VERIFY3S(0, <=, snprintf(buf, sizeof (buf), "%s#%s", osname, 2862 attr.za_name)); 2863 (void) dump_bookmark(dp, buf, verbosity >= 5, verbosity >= 6); 2864 } 2865 zap_cursor_fini(&zc); 2866 dsl_pool_config_exit(dp, FTAG); 2867 } 2868 2869 static void 2870 bpobj_count_refd(bpobj_t *bpo) 2871 { 2872 mos_obj_refd(bpo->bpo_object); 2873 2874 if (bpo->bpo_havesubobj && bpo->bpo_phys->bpo_subobjs != 0) { 2875 mos_obj_refd(bpo->bpo_phys->bpo_subobjs); 2876 for (uint64_t i = 0; i < bpo->bpo_phys->bpo_num_subobjs; i++) { 2877 uint64_t subobj; 2878 bpobj_t subbpo; 2879 int error; 2880 VERIFY0(dmu_read(bpo->bpo_os, 2881 bpo->bpo_phys->bpo_subobjs, 2882 i * sizeof (subobj), sizeof (subobj), &subobj, 0)); 2883 error = bpobj_open(&subbpo, bpo->bpo_os, subobj); 2884 if (error != 0) { 2885 (void) printf("ERROR %u while trying to open " 2886 "subobj id %llu\n", 2887 error, (u_longlong_t)subobj); 2888 continue; 2889 } 2890 bpobj_count_refd(&subbpo); 2891 bpobj_close(&subbpo); 2892 } 2893 } 2894 } 2895 2896 static int 2897 dsl_deadlist_entry_count_refd(void *arg, dsl_deadlist_entry_t *dle) 2898 { 2899 spa_t *spa = arg; 2900 uint64_t empty_bpobj = spa->spa_dsl_pool->dp_empty_bpobj; 2901 if (dle->dle_bpobj.bpo_object != empty_bpobj) 2902 bpobj_count_refd(&dle->dle_bpobj); 2903 return (0); 2904 } 2905 2906 static int 2907 dsl_deadlist_entry_dump(void *arg, dsl_deadlist_entry_t *dle) 2908 { 2909 ASSERT(arg == NULL); 2910 if (dump_opt['d'] >= 5) { 2911 char buf[128]; 2912 (void) snprintf(buf, sizeof (buf), 2913 "mintxg %llu -> obj %llu", 2914 (longlong_t)dle->dle_mintxg, 2915 (longlong_t)dle->dle_bpobj.bpo_object); 2916 2917 dump_full_bpobj(&dle->dle_bpobj, buf, 0); 2918 } else { 2919 (void) printf("mintxg %llu -> obj %llu\n", 2920 (longlong_t)dle->dle_mintxg, 2921 (longlong_t)dle->dle_bpobj.bpo_object); 2922 } 2923 return (0); 2924 } 2925 2926 static void 2927 dump_blkptr_list(dsl_deadlist_t *dl, const char *name) 2928 { 2929 char bytes[32]; 2930 char comp[32]; 2931 char uncomp[32]; 2932 char entries[32]; 2933 spa_t *spa = dmu_objset_spa(dl->dl_os); 2934 uint64_t empty_bpobj = spa->spa_dsl_pool->dp_empty_bpobj; 2935 2936 if (dl->dl_oldfmt) { 2937 if (dl->dl_bpobj.bpo_object != empty_bpobj) 2938 bpobj_count_refd(&dl->dl_bpobj); 2939 } else { 2940 mos_obj_refd(dl->dl_object); 2941 dsl_deadlist_iterate(dl, dsl_deadlist_entry_count_refd, spa); 2942 } 2943 2944 /* make sure nicenum has enough space */ 2945 _Static_assert(sizeof (bytes) >= NN_NUMBUF_SZ, "bytes truncated"); 2946 _Static_assert(sizeof (comp) >= NN_NUMBUF_SZ, "comp truncated"); 2947 _Static_assert(sizeof (uncomp) >= NN_NUMBUF_SZ, "uncomp truncated"); 2948 _Static_assert(sizeof (entries) >= NN_NUMBUF_SZ, "entries truncated"); 2949 2950 if (dump_opt['d'] < 3) 2951 return; 2952 2953 if (dl->dl_oldfmt) { 2954 dump_full_bpobj(&dl->dl_bpobj, "old-format deadlist", 0); 2955 return; 2956 } 2957 2958 zdb_nicenum(dl->dl_phys->dl_used, bytes, sizeof (bytes)); 2959 zdb_nicenum(dl->dl_phys->dl_comp, comp, sizeof (comp)); 2960 zdb_nicenum(dl->dl_phys->dl_uncomp, uncomp, sizeof (uncomp)); 2961 zdb_nicenum(avl_numnodes(&dl->dl_tree), entries, sizeof (entries)); 2962 (void) printf("\n %s: %s (%s/%s comp), %s entries\n", 2963 name, bytes, comp, uncomp, entries); 2964 2965 if (dump_opt['d'] < 4) 2966 return; 2967 2968 (void) putchar('\n'); 2969 2970 dsl_deadlist_iterate(dl, dsl_deadlist_entry_dump, NULL); 2971 } 2972 2973 static int 2974 verify_dd_livelist(objset_t *os) 2975 { 2976 uint64_t ll_used, used, ll_comp, comp, ll_uncomp, uncomp; 2977 dsl_pool_t *dp = spa_get_dsl(os->os_spa); 2978 dsl_dir_t *dd = os->os_dsl_dataset->ds_dir; 2979 2980 ASSERT(!dmu_objset_is_snapshot(os)); 2981 if (!dsl_deadlist_is_open(&dd->dd_livelist)) 2982 return (0); 2983 2984 /* Iterate through the livelist to check for duplicates */ 2985 dsl_deadlist_iterate(&dd->dd_livelist, sublivelist_verify_lightweight, 2986 NULL); 2987 2988 dsl_pool_config_enter(dp, FTAG); 2989 dsl_deadlist_space(&dd->dd_livelist, &ll_used, 2990 &ll_comp, &ll_uncomp); 2991 2992 dsl_dataset_t *origin_ds; 2993 ASSERT(dsl_pool_config_held(dp)); 2994 VERIFY0(dsl_dataset_hold_obj(dp, 2995 dsl_dir_phys(dd)->dd_origin_obj, FTAG, &origin_ds)); 2996 VERIFY0(dsl_dataset_space_written(origin_ds, os->os_dsl_dataset, 2997 &used, &comp, &uncomp)); 2998 dsl_dataset_rele(origin_ds, FTAG); 2999 dsl_pool_config_exit(dp, FTAG); 3000 /* 3001 * It's possible that the dataset's uncomp space is larger than the 3002 * livelist's because livelists do not track embedded block pointers 3003 */ 3004 if (used != ll_used || comp != ll_comp || uncomp < ll_uncomp) { 3005 char nice_used[32], nice_comp[32], nice_uncomp[32]; 3006 (void) printf("Discrepancy in space accounting:\n"); 3007 zdb_nicenum(used, nice_used, sizeof (nice_used)); 3008 zdb_nicenum(comp, nice_comp, sizeof (nice_comp)); 3009 zdb_nicenum(uncomp, nice_uncomp, sizeof (nice_uncomp)); 3010 (void) printf("dir: used %s, comp %s, uncomp %s\n", 3011 nice_used, nice_comp, nice_uncomp); 3012 zdb_nicenum(ll_used, nice_used, sizeof (nice_used)); 3013 zdb_nicenum(ll_comp, nice_comp, sizeof (nice_comp)); 3014 zdb_nicenum(ll_uncomp, nice_uncomp, sizeof (nice_uncomp)); 3015 (void) printf("livelist: used %s, comp %s, uncomp %s\n", 3016 nice_used, nice_comp, nice_uncomp); 3017 return (1); 3018 } 3019 return (0); 3020 } 3021 3022 static avl_tree_t idx_tree; 3023 static avl_tree_t domain_tree; 3024 static boolean_t fuid_table_loaded; 3025 static objset_t *sa_os = NULL; 3026 static sa_attr_type_t *sa_attr_table = NULL; 3027 3028 static int 3029 open_objset(const char *path, const void *tag, objset_t **osp) 3030 { 3031 int err; 3032 uint64_t sa_attrs = 0; 3033 uint64_t version = 0; 3034 3035 VERIFY3P(sa_os, ==, NULL); 3036 /* 3037 * We can't own an objset if it's redacted. Therefore, we do this 3038 * dance: hold the objset, then acquire a long hold on its dataset, then 3039 * release the pool (which is held as part of holding the objset). 3040 */ 3041 err = dmu_objset_hold(path, tag, osp); 3042 if (err != 0) { 3043 (void) fprintf(stderr, "failed to hold dataset '%s': %s\n", 3044 path, strerror(err)); 3045 return (err); 3046 } 3047 dsl_dataset_long_hold(dmu_objset_ds(*osp), tag); 3048 dsl_pool_rele(dmu_objset_pool(*osp), tag); 3049 3050 if (dmu_objset_type(*osp) == DMU_OST_ZFS && !(*osp)->os_encrypted) { 3051 (void) zap_lookup(*osp, MASTER_NODE_OBJ, ZPL_VERSION_STR, 3052 8, 1, &version); 3053 if (version >= ZPL_VERSION_SA) { 3054 (void) zap_lookup(*osp, MASTER_NODE_OBJ, ZFS_SA_ATTRS, 3055 8, 1, &sa_attrs); 3056 } 3057 err = sa_setup(*osp, sa_attrs, zfs_attr_table, ZPL_END, 3058 &sa_attr_table); 3059 if (err != 0) { 3060 (void) fprintf(stderr, "sa_setup failed: %s\n", 3061 strerror(err)); 3062 dsl_dataset_long_rele(dmu_objset_ds(*osp), tag); 3063 dsl_dataset_rele(dmu_objset_ds(*osp), tag); 3064 *osp = NULL; 3065 } 3066 } 3067 sa_os = *osp; 3068 3069 return (0); 3070 } 3071 3072 static void 3073 close_objset(objset_t *os, const void *tag) 3074 { 3075 VERIFY3P(os, ==, sa_os); 3076 if (os->os_sa != NULL) 3077 sa_tear_down(os); 3078 dsl_dataset_long_rele(dmu_objset_ds(os), tag); 3079 dsl_dataset_rele(dmu_objset_ds(os), tag); 3080 sa_attr_table = NULL; 3081 sa_os = NULL; 3082 } 3083 3084 static void 3085 fuid_table_destroy(void) 3086 { 3087 if (fuid_table_loaded) { 3088 zfs_fuid_table_destroy(&idx_tree, &domain_tree); 3089 fuid_table_loaded = B_FALSE; 3090 } 3091 } 3092 3093 /* 3094 * print uid or gid information. 3095 * For normal POSIX id just the id is printed in decimal format. 3096 * For CIFS files with FUID the fuid is printed in hex followed by 3097 * the domain-rid string. 3098 */ 3099 static void 3100 print_idstr(uint64_t id, const char *id_type) 3101 { 3102 if (FUID_INDEX(id)) { 3103 const char *domain = 3104 zfs_fuid_idx_domain(&idx_tree, FUID_INDEX(id)); 3105 (void) printf("\t%s %llx [%s-%d]\n", id_type, 3106 (u_longlong_t)id, domain, (int)FUID_RID(id)); 3107 } else { 3108 (void) printf("\t%s %llu\n", id_type, (u_longlong_t)id); 3109 } 3110 3111 } 3112 3113 static void 3114 dump_uidgid(objset_t *os, uint64_t uid, uint64_t gid) 3115 { 3116 uint32_t uid_idx, gid_idx; 3117 3118 uid_idx = FUID_INDEX(uid); 3119 gid_idx = FUID_INDEX(gid); 3120 3121 /* Load domain table, if not already loaded */ 3122 if (!fuid_table_loaded && (uid_idx || gid_idx)) { 3123 uint64_t fuid_obj; 3124 3125 /* first find the fuid object. It lives in the master node */ 3126 VERIFY(zap_lookup(os, MASTER_NODE_OBJ, ZFS_FUID_TABLES, 3127 8, 1, &fuid_obj) == 0); 3128 zfs_fuid_avl_tree_create(&idx_tree, &domain_tree); 3129 (void) zfs_fuid_table_load(os, fuid_obj, 3130 &idx_tree, &domain_tree); 3131 fuid_table_loaded = B_TRUE; 3132 } 3133 3134 print_idstr(uid, "uid"); 3135 print_idstr(gid, "gid"); 3136 } 3137 3138 static void 3139 dump_znode_sa_xattr(sa_handle_t *hdl) 3140 { 3141 nvlist_t *sa_xattr; 3142 nvpair_t *elem = NULL; 3143 int sa_xattr_size = 0; 3144 int sa_xattr_entries = 0; 3145 int error; 3146 char *sa_xattr_packed; 3147 3148 error = sa_size(hdl, sa_attr_table[ZPL_DXATTR], &sa_xattr_size); 3149 if (error || sa_xattr_size == 0) 3150 return; 3151 3152 sa_xattr_packed = malloc(sa_xattr_size); 3153 if (sa_xattr_packed == NULL) 3154 return; 3155 3156 error = sa_lookup(hdl, sa_attr_table[ZPL_DXATTR], 3157 sa_xattr_packed, sa_xattr_size); 3158 if (error) { 3159 free(sa_xattr_packed); 3160 return; 3161 } 3162 3163 error = nvlist_unpack(sa_xattr_packed, sa_xattr_size, &sa_xattr, 0); 3164 if (error) { 3165 free(sa_xattr_packed); 3166 return; 3167 } 3168 3169 while ((elem = nvlist_next_nvpair(sa_xattr, elem)) != NULL) 3170 sa_xattr_entries++; 3171 3172 (void) printf("\tSA xattrs: %d bytes, %d entries\n\n", 3173 sa_xattr_size, sa_xattr_entries); 3174 while ((elem = nvlist_next_nvpair(sa_xattr, elem)) != NULL) { 3175 uchar_t *value; 3176 uint_t cnt, idx; 3177 3178 (void) printf("\t\t%s = ", nvpair_name(elem)); 3179 nvpair_value_byte_array(elem, &value, &cnt); 3180 for (idx = 0; idx < cnt; ++idx) { 3181 if (isprint(value[idx])) 3182 (void) putchar(value[idx]); 3183 else 3184 (void) printf("\\%3.3o", value[idx]); 3185 } 3186 (void) putchar('\n'); 3187 } 3188 3189 nvlist_free(sa_xattr); 3190 free(sa_xattr_packed); 3191 } 3192 3193 static void 3194 dump_znode_symlink(sa_handle_t *hdl) 3195 { 3196 int sa_symlink_size = 0; 3197 char linktarget[MAXPATHLEN]; 3198 int error; 3199 3200 error = sa_size(hdl, sa_attr_table[ZPL_SYMLINK], &sa_symlink_size); 3201 if (error || sa_symlink_size == 0) { 3202 return; 3203 } 3204 if (sa_symlink_size >= sizeof (linktarget)) { 3205 (void) printf("symlink size %d is too large\n", 3206 sa_symlink_size); 3207 return; 3208 } 3209 linktarget[sa_symlink_size] = '\0'; 3210 if (sa_lookup(hdl, sa_attr_table[ZPL_SYMLINK], 3211 &linktarget, sa_symlink_size) == 0) 3212 (void) printf("\ttarget %s\n", linktarget); 3213 } 3214 3215 static void 3216 dump_znode(objset_t *os, uint64_t object, void *data, size_t size) 3217 { 3218 (void) data, (void) size; 3219 char path[MAXPATHLEN * 2]; /* allow for xattr and failure prefix */ 3220 sa_handle_t *hdl; 3221 uint64_t xattr, rdev, gen; 3222 uint64_t uid, gid, mode, fsize, parent, links; 3223 uint64_t pflags; 3224 uint64_t acctm[2], modtm[2], chgtm[2], crtm[2]; 3225 time_t z_crtime, z_atime, z_mtime, z_ctime; 3226 sa_bulk_attr_t bulk[12]; 3227 int idx = 0; 3228 int error; 3229 3230 VERIFY3P(os, ==, sa_os); 3231 if (sa_handle_get(os, object, NULL, SA_HDL_PRIVATE, &hdl)) { 3232 (void) printf("Failed to get handle for SA znode\n"); 3233 return; 3234 } 3235 3236 SA_ADD_BULK_ATTR(bulk, idx, sa_attr_table[ZPL_UID], NULL, &uid, 8); 3237 SA_ADD_BULK_ATTR(bulk, idx, sa_attr_table[ZPL_GID], NULL, &gid, 8); 3238 SA_ADD_BULK_ATTR(bulk, idx, sa_attr_table[ZPL_LINKS], NULL, 3239 &links, 8); 3240 SA_ADD_BULK_ATTR(bulk, idx, sa_attr_table[ZPL_GEN], NULL, &gen, 8); 3241 SA_ADD_BULK_ATTR(bulk, idx, sa_attr_table[ZPL_MODE], NULL, 3242 &mode, 8); 3243 SA_ADD_BULK_ATTR(bulk, idx, sa_attr_table[ZPL_PARENT], 3244 NULL, &parent, 8); 3245 SA_ADD_BULK_ATTR(bulk, idx, sa_attr_table[ZPL_SIZE], NULL, 3246 &fsize, 8); 3247 SA_ADD_BULK_ATTR(bulk, idx, sa_attr_table[ZPL_ATIME], NULL, 3248 acctm, 16); 3249 SA_ADD_BULK_ATTR(bulk, idx, sa_attr_table[ZPL_MTIME], NULL, 3250 modtm, 16); 3251 SA_ADD_BULK_ATTR(bulk, idx, sa_attr_table[ZPL_CRTIME], NULL, 3252 crtm, 16); 3253 SA_ADD_BULK_ATTR(bulk, idx, sa_attr_table[ZPL_CTIME], NULL, 3254 chgtm, 16); 3255 SA_ADD_BULK_ATTR(bulk, idx, sa_attr_table[ZPL_FLAGS], NULL, 3256 &pflags, 8); 3257 3258 if (sa_bulk_lookup(hdl, bulk, idx)) { 3259 (void) sa_handle_destroy(hdl); 3260 return; 3261 } 3262 3263 z_crtime = (time_t)crtm[0]; 3264 z_atime = (time_t)acctm[0]; 3265 z_mtime = (time_t)modtm[0]; 3266 z_ctime = (time_t)chgtm[0]; 3267 3268 if (dump_opt['d'] > 4) { 3269 error = zfs_obj_to_path(os, object, path, sizeof (path)); 3270 if (error == ESTALE) { 3271 (void) snprintf(path, sizeof (path), "on delete queue"); 3272 } else if (error != 0) { 3273 leaked_objects++; 3274 (void) snprintf(path, sizeof (path), 3275 "path not found, possibly leaked"); 3276 } 3277 (void) printf("\tpath %s\n", path); 3278 } 3279 3280 if (S_ISLNK(mode)) 3281 dump_znode_symlink(hdl); 3282 dump_uidgid(os, uid, gid); 3283 (void) printf("\tatime %s", ctime(&z_atime)); 3284 (void) printf("\tmtime %s", ctime(&z_mtime)); 3285 (void) printf("\tctime %s", ctime(&z_ctime)); 3286 (void) printf("\tcrtime %s", ctime(&z_crtime)); 3287 (void) printf("\tgen %llu\n", (u_longlong_t)gen); 3288 (void) printf("\tmode %llo\n", (u_longlong_t)mode); 3289 (void) printf("\tsize %llu\n", (u_longlong_t)fsize); 3290 (void) printf("\tparent %llu\n", (u_longlong_t)parent); 3291 (void) printf("\tlinks %llu\n", (u_longlong_t)links); 3292 (void) printf("\tpflags %llx\n", (u_longlong_t)pflags); 3293 if (dmu_objset_projectquota_enabled(os) && (pflags & ZFS_PROJID)) { 3294 uint64_t projid; 3295 3296 if (sa_lookup(hdl, sa_attr_table[ZPL_PROJID], &projid, 3297 sizeof (uint64_t)) == 0) 3298 (void) printf("\tprojid %llu\n", (u_longlong_t)projid); 3299 } 3300 if (sa_lookup(hdl, sa_attr_table[ZPL_XATTR], &xattr, 3301 sizeof (uint64_t)) == 0) 3302 (void) printf("\txattr %llu\n", (u_longlong_t)xattr); 3303 if (sa_lookup(hdl, sa_attr_table[ZPL_RDEV], &rdev, 3304 sizeof (uint64_t)) == 0) 3305 (void) printf("\trdev 0x%016llx\n", (u_longlong_t)rdev); 3306 dump_znode_sa_xattr(hdl); 3307 sa_handle_destroy(hdl); 3308 } 3309 3310 static void 3311 dump_acl(objset_t *os, uint64_t object, void *data, size_t size) 3312 { 3313 (void) os, (void) object, (void) data, (void) size; 3314 } 3315 3316 static void 3317 dump_dmu_objset(objset_t *os, uint64_t object, void *data, size_t size) 3318 { 3319 (void) os, (void) object, (void) data, (void) size; 3320 } 3321 3322 static object_viewer_t *object_viewer[DMU_OT_NUMTYPES + 1] = { 3323 dump_none, /* unallocated */ 3324 dump_zap, /* object directory */ 3325 dump_uint64, /* object array */ 3326 dump_none, /* packed nvlist */ 3327 dump_packed_nvlist, /* packed nvlist size */ 3328 dump_none, /* bpobj */ 3329 dump_bpobj, /* bpobj header */ 3330 dump_none, /* SPA space map header */ 3331 dump_none, /* SPA space map */ 3332 dump_none, /* ZIL intent log */ 3333 dump_dnode, /* DMU dnode */ 3334 dump_dmu_objset, /* DMU objset */ 3335 dump_dsl_dir, /* DSL directory */ 3336 dump_zap, /* DSL directory child map */ 3337 dump_zap, /* DSL dataset snap map */ 3338 dump_zap, /* DSL props */ 3339 dump_dsl_dataset, /* DSL dataset */ 3340 dump_znode, /* ZFS znode */ 3341 dump_acl, /* ZFS V0 ACL */ 3342 dump_uint8, /* ZFS plain file */ 3343 dump_zpldir, /* ZFS directory */ 3344 dump_zap, /* ZFS master node */ 3345 dump_zap, /* ZFS delete queue */ 3346 dump_uint8, /* zvol object */ 3347 dump_zap, /* zvol prop */ 3348 dump_uint8, /* other uint8[] */ 3349 dump_uint64, /* other uint64[] */ 3350 dump_zap, /* other ZAP */ 3351 dump_zap, /* persistent error log */ 3352 dump_uint8, /* SPA history */ 3353 dump_history_offsets, /* SPA history offsets */ 3354 dump_zap, /* Pool properties */ 3355 dump_zap, /* DSL permissions */ 3356 dump_acl, /* ZFS ACL */ 3357 dump_uint8, /* ZFS SYSACL */ 3358 dump_none, /* FUID nvlist */ 3359 dump_packed_nvlist, /* FUID nvlist size */ 3360 dump_zap, /* DSL dataset next clones */ 3361 dump_zap, /* DSL scrub queue */ 3362 dump_zap, /* ZFS user/group/project used */ 3363 dump_zap, /* ZFS user/group/project quota */ 3364 dump_zap, /* snapshot refcount tags */ 3365 dump_ddt_zap, /* DDT ZAP object */ 3366 dump_zap, /* DDT statistics */ 3367 dump_znode, /* SA object */ 3368 dump_zap, /* SA Master Node */ 3369 dump_sa_attrs, /* SA attribute registration */ 3370 dump_sa_layouts, /* SA attribute layouts */ 3371 dump_zap, /* DSL scrub translations */ 3372 dump_none, /* fake dedup BP */ 3373 dump_zap, /* deadlist */ 3374 dump_none, /* deadlist hdr */ 3375 dump_zap, /* dsl clones */ 3376 dump_bpobj_subobjs, /* bpobj subobjs */ 3377 dump_unknown, /* Unknown type, must be last */ 3378 }; 3379 3380 static boolean_t 3381 match_object_type(dmu_object_type_t obj_type, uint64_t flags) 3382 { 3383 boolean_t match = B_TRUE; 3384 3385 switch (obj_type) { 3386 case DMU_OT_DIRECTORY_CONTENTS: 3387 if (!(flags & ZOR_FLAG_DIRECTORY)) 3388 match = B_FALSE; 3389 break; 3390 case DMU_OT_PLAIN_FILE_CONTENTS: 3391 if (!(flags & ZOR_FLAG_PLAIN_FILE)) 3392 match = B_FALSE; 3393 break; 3394 case DMU_OT_SPACE_MAP: 3395 if (!(flags & ZOR_FLAG_SPACE_MAP)) 3396 match = B_FALSE; 3397 break; 3398 default: 3399 if (strcmp(zdb_ot_name(obj_type), "zap") == 0) { 3400 if (!(flags & ZOR_FLAG_ZAP)) 3401 match = B_FALSE; 3402 break; 3403 } 3404 3405 /* 3406 * If all bits except some of the supported flags are 3407 * set, the user combined the all-types flag (A) with 3408 * a negated flag to exclude some types (e.g. A-f to 3409 * show all object types except plain files). 3410 */ 3411 if ((flags | ZOR_SUPPORTED_FLAGS) != ZOR_FLAG_ALL_TYPES) 3412 match = B_FALSE; 3413 3414 break; 3415 } 3416 3417 return (match); 3418 } 3419 3420 static void 3421 dump_object(objset_t *os, uint64_t object, int verbosity, 3422 boolean_t *print_header, uint64_t *dnode_slots_used, uint64_t flags) 3423 { 3424 dmu_buf_t *db = NULL; 3425 dmu_object_info_t doi; 3426 dnode_t *dn; 3427 boolean_t dnode_held = B_FALSE; 3428 void *bonus = NULL; 3429 size_t bsize = 0; 3430 char iblk[32], dblk[32], lsize[32], asize[32], fill[32], dnsize[32]; 3431 char bonus_size[32]; 3432 char aux[50]; 3433 int error; 3434 3435 /* make sure nicenum has enough space */ 3436 _Static_assert(sizeof (iblk) >= NN_NUMBUF_SZ, "iblk truncated"); 3437 _Static_assert(sizeof (dblk) >= NN_NUMBUF_SZ, "dblk truncated"); 3438 _Static_assert(sizeof (lsize) >= NN_NUMBUF_SZ, "lsize truncated"); 3439 _Static_assert(sizeof (asize) >= NN_NUMBUF_SZ, "asize truncated"); 3440 _Static_assert(sizeof (bonus_size) >= NN_NUMBUF_SZ, 3441 "bonus_size truncated"); 3442 3443 if (*print_header) { 3444 (void) printf("\n%10s %3s %5s %5s %5s %6s %5s %6s %s\n", 3445 "Object", "lvl", "iblk", "dblk", "dsize", "dnsize", 3446 "lsize", "%full", "type"); 3447 *print_header = 0; 3448 } 3449 3450 if (object == 0) { 3451 dn = DMU_META_DNODE(os); 3452 dmu_object_info_from_dnode(dn, &doi); 3453 } else { 3454 /* 3455 * Encrypted datasets will have sensitive bonus buffers 3456 * encrypted. Therefore we cannot hold the bonus buffer and 3457 * must hold the dnode itself instead. 3458 */ 3459 error = dmu_object_info(os, object, &doi); 3460 if (error) 3461 fatal("dmu_object_info() failed, errno %u", error); 3462 3463 if (os->os_encrypted && 3464 DMU_OT_IS_ENCRYPTED(doi.doi_bonus_type)) { 3465 error = dnode_hold(os, object, FTAG, &dn); 3466 if (error) 3467 fatal("dnode_hold() failed, errno %u", error); 3468 dnode_held = B_TRUE; 3469 } else { 3470 error = dmu_bonus_hold(os, object, FTAG, &db); 3471 if (error) 3472 fatal("dmu_bonus_hold(%llu) failed, errno %u", 3473 object, error); 3474 bonus = db->db_data; 3475 bsize = db->db_size; 3476 dn = DB_DNODE((dmu_buf_impl_t *)db); 3477 } 3478 } 3479 3480 /* 3481 * Default to showing all object types if no flags were specified. 3482 */ 3483 if (flags != 0 && flags != ZOR_FLAG_ALL_TYPES && 3484 !match_object_type(doi.doi_type, flags)) 3485 goto out; 3486 3487 if (dnode_slots_used) 3488 *dnode_slots_used = doi.doi_dnodesize / DNODE_MIN_SIZE; 3489 3490 zdb_nicenum(doi.doi_metadata_block_size, iblk, sizeof (iblk)); 3491 zdb_nicenum(doi.doi_data_block_size, dblk, sizeof (dblk)); 3492 zdb_nicenum(doi.doi_max_offset, lsize, sizeof (lsize)); 3493 zdb_nicenum(doi.doi_physical_blocks_512 << 9, asize, sizeof (asize)); 3494 zdb_nicenum(doi.doi_bonus_size, bonus_size, sizeof (bonus_size)); 3495 zdb_nicenum(doi.doi_dnodesize, dnsize, sizeof (dnsize)); 3496 (void) sprintf(fill, "%6.2f", 100.0 * doi.doi_fill_count * 3497 doi.doi_data_block_size / (object == 0 ? DNODES_PER_BLOCK : 1) / 3498 doi.doi_max_offset); 3499 3500 aux[0] = '\0'; 3501 3502 if (doi.doi_checksum != ZIO_CHECKSUM_INHERIT || verbosity >= 6) { 3503 (void) snprintf(aux + strlen(aux), sizeof (aux) - strlen(aux), 3504 " (K=%s)", ZDB_CHECKSUM_NAME(doi.doi_checksum)); 3505 } 3506 3507 if (doi.doi_compress == ZIO_COMPRESS_INHERIT && 3508 ZIO_COMPRESS_HASLEVEL(os->os_compress) && verbosity >= 6) { 3509 const char *compname = NULL; 3510 if (zfs_prop_index_to_string(ZFS_PROP_COMPRESSION, 3511 ZIO_COMPRESS_RAW(os->os_compress, os->os_complevel), 3512 &compname) == 0) { 3513 (void) snprintf(aux + strlen(aux), 3514 sizeof (aux) - strlen(aux), " (Z=inherit=%s)", 3515 compname); 3516 } else { 3517 (void) snprintf(aux + strlen(aux), 3518 sizeof (aux) - strlen(aux), 3519 " (Z=inherit=%s-unknown)", 3520 ZDB_COMPRESS_NAME(os->os_compress)); 3521 } 3522 } else if (doi.doi_compress == ZIO_COMPRESS_INHERIT && verbosity >= 6) { 3523 (void) snprintf(aux + strlen(aux), sizeof (aux) - strlen(aux), 3524 " (Z=inherit=%s)", ZDB_COMPRESS_NAME(os->os_compress)); 3525 } else if (doi.doi_compress != ZIO_COMPRESS_INHERIT || verbosity >= 6) { 3526 (void) snprintf(aux + strlen(aux), sizeof (aux) - strlen(aux), 3527 " (Z=%s)", ZDB_COMPRESS_NAME(doi.doi_compress)); 3528 } 3529 3530 (void) printf("%10lld %3u %5s %5s %5s %6s %5s %6s %s%s\n", 3531 (u_longlong_t)object, doi.doi_indirection, iblk, dblk, 3532 asize, dnsize, lsize, fill, zdb_ot_name(doi.doi_type), aux); 3533 3534 if (doi.doi_bonus_type != DMU_OT_NONE && verbosity > 3) { 3535 (void) printf("%10s %3s %5s %5s %5s %5s %5s %6s %s\n", 3536 "", "", "", "", "", "", bonus_size, "bonus", 3537 zdb_ot_name(doi.doi_bonus_type)); 3538 } 3539 3540 if (verbosity >= 4) { 3541 (void) printf("\tdnode flags: %s%s%s%s\n", 3542 (dn->dn_phys->dn_flags & DNODE_FLAG_USED_BYTES) ? 3543 "USED_BYTES " : "", 3544 (dn->dn_phys->dn_flags & DNODE_FLAG_USERUSED_ACCOUNTED) ? 3545 "USERUSED_ACCOUNTED " : "", 3546 (dn->dn_phys->dn_flags & DNODE_FLAG_USEROBJUSED_ACCOUNTED) ? 3547 "USEROBJUSED_ACCOUNTED " : "", 3548 (dn->dn_phys->dn_flags & DNODE_FLAG_SPILL_BLKPTR) ? 3549 "SPILL_BLKPTR" : ""); 3550 (void) printf("\tdnode maxblkid: %llu\n", 3551 (longlong_t)dn->dn_phys->dn_maxblkid); 3552 3553 if (!dnode_held) { 3554 object_viewer[ZDB_OT_TYPE(doi.doi_bonus_type)](os, 3555 object, bonus, bsize); 3556 } else { 3557 (void) printf("\t\t(bonus encrypted)\n"); 3558 } 3559 3560 if (!os->os_encrypted || !DMU_OT_IS_ENCRYPTED(doi.doi_type)) { 3561 object_viewer[ZDB_OT_TYPE(doi.doi_type)](os, object, 3562 NULL, 0); 3563 } else { 3564 (void) printf("\t\t(object encrypted)\n"); 3565 } 3566 3567 *print_header = B_TRUE; 3568 } 3569 3570 if (verbosity >= 5) { 3571 if (dn->dn_phys->dn_flags & DNODE_FLAG_SPILL_BLKPTR) { 3572 char blkbuf[BP_SPRINTF_LEN]; 3573 snprintf_blkptr_compact(blkbuf, sizeof (blkbuf), 3574 DN_SPILL_BLKPTR(dn->dn_phys), B_FALSE); 3575 (void) printf("\nSpill block: %s\n", blkbuf); 3576 } 3577 dump_indirect(dn); 3578 } 3579 3580 if (verbosity >= 5) { 3581 /* 3582 * Report the list of segments that comprise the object. 3583 */ 3584 uint64_t start = 0; 3585 uint64_t end; 3586 uint64_t blkfill = 1; 3587 int minlvl = 1; 3588 3589 if (dn->dn_type == DMU_OT_DNODE) { 3590 minlvl = 0; 3591 blkfill = DNODES_PER_BLOCK; 3592 } 3593 3594 for (;;) { 3595 char segsize[32]; 3596 /* make sure nicenum has enough space */ 3597 _Static_assert(sizeof (segsize) >= NN_NUMBUF_SZ, 3598 "segsize truncated"); 3599 error = dnode_next_offset(dn, 3600 0, &start, minlvl, blkfill, 0); 3601 if (error) 3602 break; 3603 end = start; 3604 error = dnode_next_offset(dn, 3605 DNODE_FIND_HOLE, &end, minlvl, blkfill, 0); 3606 zdb_nicenum(end - start, segsize, sizeof (segsize)); 3607 (void) printf("\t\tsegment [%016llx, %016llx)" 3608 " size %5s\n", (u_longlong_t)start, 3609 (u_longlong_t)end, segsize); 3610 if (error) 3611 break; 3612 start = end; 3613 } 3614 } 3615 3616 out: 3617 if (db != NULL) 3618 dmu_buf_rele(db, FTAG); 3619 if (dnode_held) 3620 dnode_rele(dn, FTAG); 3621 } 3622 3623 static void 3624 count_dir_mos_objects(dsl_dir_t *dd) 3625 { 3626 mos_obj_refd(dd->dd_object); 3627 mos_obj_refd(dsl_dir_phys(dd)->dd_child_dir_zapobj); 3628 mos_obj_refd(dsl_dir_phys(dd)->dd_deleg_zapobj); 3629 mos_obj_refd(dsl_dir_phys(dd)->dd_props_zapobj); 3630 mos_obj_refd(dsl_dir_phys(dd)->dd_clones); 3631 3632 /* 3633 * The dd_crypto_obj can be referenced by multiple dsl_dir's. 3634 * Ignore the references after the first one. 3635 */ 3636 mos_obj_refd_multiple(dd->dd_crypto_obj); 3637 } 3638 3639 static void 3640 count_ds_mos_objects(dsl_dataset_t *ds) 3641 { 3642 mos_obj_refd(ds->ds_object); 3643 mos_obj_refd(dsl_dataset_phys(ds)->ds_next_clones_obj); 3644 mos_obj_refd(dsl_dataset_phys(ds)->ds_props_obj); 3645 mos_obj_refd(dsl_dataset_phys(ds)->ds_userrefs_obj); 3646 mos_obj_refd(dsl_dataset_phys(ds)->ds_snapnames_zapobj); 3647 mos_obj_refd(ds->ds_bookmarks_obj); 3648 3649 if (!dsl_dataset_is_snapshot(ds)) { 3650 count_dir_mos_objects(ds->ds_dir); 3651 } 3652 } 3653 3654 static const char *const objset_types[DMU_OST_NUMTYPES] = { 3655 "NONE", "META", "ZPL", "ZVOL", "OTHER", "ANY" }; 3656 3657 /* 3658 * Parse a string denoting a range of object IDs of the form 3659 * <start>[:<end>[:flags]], and store the results in zor. 3660 * Return 0 on success. On error, return 1 and update the msg 3661 * pointer to point to a descriptive error message. 3662 */ 3663 static int 3664 parse_object_range(char *range, zopt_object_range_t *zor, const char **msg) 3665 { 3666 uint64_t flags = 0; 3667 char *p, *s, *dup, *flagstr, *tmp = NULL; 3668 size_t len; 3669 int i; 3670 int rc = 0; 3671 3672 if (strchr(range, ':') == NULL) { 3673 zor->zor_obj_start = strtoull(range, &p, 0); 3674 if (*p != '\0') { 3675 *msg = "Invalid characters in object ID"; 3676 rc = 1; 3677 } 3678 zor->zor_obj_start = ZDB_MAP_OBJECT_ID(zor->zor_obj_start); 3679 zor->zor_obj_end = zor->zor_obj_start; 3680 return (rc); 3681 } 3682 3683 if (strchr(range, ':') == range) { 3684 *msg = "Invalid leading colon"; 3685 rc = 1; 3686 return (rc); 3687 } 3688 3689 len = strlen(range); 3690 if (range[len - 1] == ':') { 3691 *msg = "Invalid trailing colon"; 3692 rc = 1; 3693 return (rc); 3694 } 3695 3696 dup = strdup(range); 3697 s = strtok_r(dup, ":", &tmp); 3698 zor->zor_obj_start = strtoull(s, &p, 0); 3699 3700 if (*p != '\0') { 3701 *msg = "Invalid characters in start object ID"; 3702 rc = 1; 3703 goto out; 3704 } 3705 3706 s = strtok_r(NULL, ":", &tmp); 3707 zor->zor_obj_end = strtoull(s, &p, 0); 3708 3709 if (*p != '\0') { 3710 *msg = "Invalid characters in end object ID"; 3711 rc = 1; 3712 goto out; 3713 } 3714 3715 if (zor->zor_obj_start > zor->zor_obj_end) { 3716 *msg = "Start object ID may not exceed end object ID"; 3717 rc = 1; 3718 goto out; 3719 } 3720 3721 s = strtok_r(NULL, ":", &tmp); 3722 if (s == NULL) { 3723 zor->zor_flags = ZOR_FLAG_ALL_TYPES; 3724 goto out; 3725 } else if (strtok_r(NULL, ":", &tmp) != NULL) { 3726 *msg = "Invalid colon-delimited field after flags"; 3727 rc = 1; 3728 goto out; 3729 } 3730 3731 flagstr = s; 3732 for (i = 0; flagstr[i]; i++) { 3733 int bit; 3734 boolean_t negation = (flagstr[i] == '-'); 3735 3736 if (negation) { 3737 i++; 3738 if (flagstr[i] == '\0') { 3739 *msg = "Invalid trailing negation operator"; 3740 rc = 1; 3741 goto out; 3742 } 3743 } 3744 bit = flagbits[(uchar_t)flagstr[i]]; 3745 if (bit == 0) { 3746 *msg = "Invalid flag"; 3747 rc = 1; 3748 goto out; 3749 } 3750 if (negation) 3751 flags &= ~bit; 3752 else 3753 flags |= bit; 3754 } 3755 zor->zor_flags = flags; 3756 3757 zor->zor_obj_start = ZDB_MAP_OBJECT_ID(zor->zor_obj_start); 3758 zor->zor_obj_end = ZDB_MAP_OBJECT_ID(zor->zor_obj_end); 3759 3760 out: 3761 free(dup); 3762 return (rc); 3763 } 3764 3765 static void 3766 dump_objset(objset_t *os) 3767 { 3768 dmu_objset_stats_t dds = { 0 }; 3769 uint64_t object, object_count; 3770 uint64_t refdbytes, usedobjs, scratch; 3771 char numbuf[32]; 3772 char blkbuf[BP_SPRINTF_LEN + 20]; 3773 char osname[ZFS_MAX_DATASET_NAME_LEN]; 3774 const char *type = "UNKNOWN"; 3775 int verbosity = dump_opt['d']; 3776 boolean_t print_header; 3777 unsigned i; 3778 int error; 3779 uint64_t total_slots_used = 0; 3780 uint64_t max_slot_used = 0; 3781 uint64_t dnode_slots; 3782 uint64_t obj_start; 3783 uint64_t obj_end; 3784 uint64_t flags; 3785 3786 /* make sure nicenum has enough space */ 3787 _Static_assert(sizeof (numbuf) >= NN_NUMBUF_SZ, "numbuf truncated"); 3788 3789 dsl_pool_config_enter(dmu_objset_pool(os), FTAG); 3790 dmu_objset_fast_stat(os, &dds); 3791 dsl_pool_config_exit(dmu_objset_pool(os), FTAG); 3792 3793 print_header = B_TRUE; 3794 3795 if (dds.dds_type < DMU_OST_NUMTYPES) 3796 type = objset_types[dds.dds_type]; 3797 3798 if (dds.dds_type == DMU_OST_META) { 3799 dds.dds_creation_txg = TXG_INITIAL; 3800 usedobjs = BP_GET_FILL(os->os_rootbp); 3801 refdbytes = dsl_dir_phys(os->os_spa->spa_dsl_pool->dp_mos_dir)-> 3802 dd_used_bytes; 3803 } else { 3804 dmu_objset_space(os, &refdbytes, &scratch, &usedobjs, &scratch); 3805 } 3806 3807 ASSERT3U(usedobjs, ==, BP_GET_FILL(os->os_rootbp)); 3808 3809 zdb_nicenum(refdbytes, numbuf, sizeof (numbuf)); 3810 3811 if (verbosity >= 4) { 3812 (void) snprintf(blkbuf, sizeof (blkbuf), ", rootbp "); 3813 (void) snprintf_blkptr(blkbuf + strlen(blkbuf), 3814 sizeof (blkbuf) - strlen(blkbuf), os->os_rootbp); 3815 } else { 3816 blkbuf[0] = '\0'; 3817 } 3818 3819 dmu_objset_name(os, osname); 3820 3821 (void) printf("Dataset %s [%s], ID %llu, cr_txg %llu, " 3822 "%s, %llu objects%s%s\n", 3823 osname, type, (u_longlong_t)dmu_objset_id(os), 3824 (u_longlong_t)dds.dds_creation_txg, 3825 numbuf, (u_longlong_t)usedobjs, blkbuf, 3826 (dds.dds_inconsistent) ? " (inconsistent)" : ""); 3827 3828 for (i = 0; i < zopt_object_args; i++) { 3829 obj_start = zopt_object_ranges[i].zor_obj_start; 3830 obj_end = zopt_object_ranges[i].zor_obj_end; 3831 flags = zopt_object_ranges[i].zor_flags; 3832 3833 object = obj_start; 3834 if (object == 0 || obj_start == obj_end) 3835 dump_object(os, object, verbosity, &print_header, NULL, 3836 flags); 3837 else 3838 object--; 3839 3840 while ((dmu_object_next(os, &object, B_FALSE, 0) == 0) && 3841 object <= obj_end) { 3842 dump_object(os, object, verbosity, &print_header, NULL, 3843 flags); 3844 } 3845 } 3846 3847 if (zopt_object_args > 0) { 3848 (void) printf("\n"); 3849 return; 3850 } 3851 3852 if (dump_opt['i'] != 0 || verbosity >= 2) 3853 dump_intent_log(dmu_objset_zil(os)); 3854 3855 if (dmu_objset_ds(os) != NULL) { 3856 dsl_dataset_t *ds = dmu_objset_ds(os); 3857 dump_blkptr_list(&ds->ds_deadlist, "Deadlist"); 3858 if (dsl_deadlist_is_open(&ds->ds_dir->dd_livelist) && 3859 !dmu_objset_is_snapshot(os)) { 3860 dump_blkptr_list(&ds->ds_dir->dd_livelist, "Livelist"); 3861 if (verify_dd_livelist(os) != 0) 3862 fatal("livelist is incorrect"); 3863 } 3864 3865 if (dsl_dataset_remap_deadlist_exists(ds)) { 3866 (void) printf("ds_remap_deadlist:\n"); 3867 dump_blkptr_list(&ds->ds_remap_deadlist, "Deadlist"); 3868 } 3869 count_ds_mos_objects(ds); 3870 } 3871 3872 if (dmu_objset_ds(os) != NULL) 3873 dump_bookmarks(os, verbosity); 3874 3875 if (verbosity < 2) 3876 return; 3877 3878 if (BP_IS_HOLE(os->os_rootbp)) 3879 return; 3880 3881 dump_object(os, 0, verbosity, &print_header, NULL, 0); 3882 object_count = 0; 3883 if (DMU_USERUSED_DNODE(os) != NULL && 3884 DMU_USERUSED_DNODE(os)->dn_type != 0) { 3885 dump_object(os, DMU_USERUSED_OBJECT, verbosity, &print_header, 3886 NULL, 0); 3887 dump_object(os, DMU_GROUPUSED_OBJECT, verbosity, &print_header, 3888 NULL, 0); 3889 } 3890 3891 if (DMU_PROJECTUSED_DNODE(os) != NULL && 3892 DMU_PROJECTUSED_DNODE(os)->dn_type != 0) 3893 dump_object(os, DMU_PROJECTUSED_OBJECT, verbosity, 3894 &print_header, NULL, 0); 3895 3896 object = 0; 3897 while ((error = dmu_object_next(os, &object, B_FALSE, 0)) == 0) { 3898 dump_object(os, object, verbosity, &print_header, &dnode_slots, 3899 0); 3900 object_count++; 3901 total_slots_used += dnode_slots; 3902 max_slot_used = object + dnode_slots - 1; 3903 } 3904 3905 (void) printf("\n"); 3906 3907 (void) printf(" Dnode slots:\n"); 3908 (void) printf("\tTotal used: %10llu\n", 3909 (u_longlong_t)total_slots_used); 3910 (void) printf("\tMax used: %10llu\n", 3911 (u_longlong_t)max_slot_used); 3912 (void) printf("\tPercent empty: %10lf\n", 3913 (double)(max_slot_used - total_slots_used)*100 / 3914 (double)max_slot_used); 3915 (void) printf("\n"); 3916 3917 if (error != ESRCH) { 3918 (void) fprintf(stderr, "dmu_object_next() = %d\n", error); 3919 abort(); 3920 } 3921 3922 ASSERT3U(object_count, ==, usedobjs); 3923 3924 if (leaked_objects != 0) { 3925 (void) printf("%d potentially leaked objects detected\n", 3926 leaked_objects); 3927 leaked_objects = 0; 3928 } 3929 } 3930 3931 static void 3932 dump_uberblock(uberblock_t *ub, const char *header, const char *footer) 3933 { 3934 time_t timestamp = ub->ub_timestamp; 3935 3936 (void) printf("%s", header ? header : ""); 3937 (void) printf("\tmagic = %016llx\n", (u_longlong_t)ub->ub_magic); 3938 (void) printf("\tversion = %llu\n", (u_longlong_t)ub->ub_version); 3939 (void) printf("\ttxg = %llu\n", (u_longlong_t)ub->ub_txg); 3940 (void) printf("\tguid_sum = %llu\n", (u_longlong_t)ub->ub_guid_sum); 3941 (void) printf("\ttimestamp = %llu UTC = %s", 3942 (u_longlong_t)ub->ub_timestamp, ctime(×tamp)); 3943 3944 (void) printf("\tmmp_magic = %016llx\n", 3945 (u_longlong_t)ub->ub_mmp_magic); 3946 if (MMP_VALID(ub)) { 3947 (void) printf("\tmmp_delay = %0llu\n", 3948 (u_longlong_t)ub->ub_mmp_delay); 3949 if (MMP_SEQ_VALID(ub)) 3950 (void) printf("\tmmp_seq = %u\n", 3951 (unsigned int) MMP_SEQ(ub)); 3952 if (MMP_FAIL_INT_VALID(ub)) 3953 (void) printf("\tmmp_fail = %u\n", 3954 (unsigned int) MMP_FAIL_INT(ub)); 3955 if (MMP_INTERVAL_VALID(ub)) 3956 (void) printf("\tmmp_write = %u\n", 3957 (unsigned int) MMP_INTERVAL(ub)); 3958 /* After MMP_* to make summarize_uberblock_mmp cleaner */ 3959 (void) printf("\tmmp_valid = %x\n", 3960 (unsigned int) ub->ub_mmp_config & 0xFF); 3961 } 3962 3963 if (dump_opt['u'] >= 4) { 3964 char blkbuf[BP_SPRINTF_LEN]; 3965 snprintf_blkptr(blkbuf, sizeof (blkbuf), &ub->ub_rootbp); 3966 (void) printf("\trootbp = %s\n", blkbuf); 3967 } 3968 (void) printf("\tcheckpoint_txg = %llu\n", 3969 (u_longlong_t)ub->ub_checkpoint_txg); 3970 (void) printf("%s", footer ? footer : ""); 3971 } 3972 3973 static void 3974 dump_config(spa_t *spa) 3975 { 3976 dmu_buf_t *db; 3977 size_t nvsize = 0; 3978 int error = 0; 3979 3980 3981 error = dmu_bonus_hold(spa->spa_meta_objset, 3982 spa->spa_config_object, FTAG, &db); 3983 3984 if (error == 0) { 3985 nvsize = *(uint64_t *)db->db_data; 3986 dmu_buf_rele(db, FTAG); 3987 3988 (void) printf("\nMOS Configuration:\n"); 3989 dump_packed_nvlist(spa->spa_meta_objset, 3990 spa->spa_config_object, (void *)&nvsize, 1); 3991 } else { 3992 (void) fprintf(stderr, "dmu_bonus_hold(%llu) failed, errno %d", 3993 (u_longlong_t)spa->spa_config_object, error); 3994 } 3995 } 3996 3997 static void 3998 dump_cachefile(const char *cachefile) 3999 { 4000 int fd; 4001 struct stat64 statbuf; 4002 char *buf; 4003 nvlist_t *config; 4004 4005 if ((fd = open64(cachefile, O_RDONLY)) < 0) { 4006 (void) printf("cannot open '%s': %s\n", cachefile, 4007 strerror(errno)); 4008 exit(1); 4009 } 4010 4011 if (fstat64(fd, &statbuf) != 0) { 4012 (void) printf("failed to stat '%s': %s\n", cachefile, 4013 strerror(errno)); 4014 exit(1); 4015 } 4016 4017 if ((buf = malloc(statbuf.st_size)) == NULL) { 4018 (void) fprintf(stderr, "failed to allocate %llu bytes\n", 4019 (u_longlong_t)statbuf.st_size); 4020 exit(1); 4021 } 4022 4023 if (read(fd, buf, statbuf.st_size) != statbuf.st_size) { 4024 (void) fprintf(stderr, "failed to read %llu bytes\n", 4025 (u_longlong_t)statbuf.st_size); 4026 exit(1); 4027 } 4028 4029 (void) close(fd); 4030 4031 if (nvlist_unpack(buf, statbuf.st_size, &config, 0) != 0) { 4032 (void) fprintf(stderr, "failed to unpack nvlist\n"); 4033 exit(1); 4034 } 4035 4036 free(buf); 4037 4038 dump_nvlist(config, 0); 4039 4040 nvlist_free(config); 4041 } 4042 4043 /* 4044 * ZFS label nvlist stats 4045 */ 4046 typedef struct zdb_nvl_stats { 4047 int zns_list_count; 4048 int zns_leaf_count; 4049 size_t zns_leaf_largest; 4050 size_t zns_leaf_total; 4051 nvlist_t *zns_string; 4052 nvlist_t *zns_uint64; 4053 nvlist_t *zns_boolean; 4054 } zdb_nvl_stats_t; 4055 4056 static void 4057 collect_nvlist_stats(nvlist_t *nvl, zdb_nvl_stats_t *stats) 4058 { 4059 nvlist_t *list, **array; 4060 nvpair_t *nvp = NULL; 4061 char *name; 4062 uint_t i, items; 4063 4064 stats->zns_list_count++; 4065 4066 while ((nvp = nvlist_next_nvpair(nvl, nvp)) != NULL) { 4067 name = nvpair_name(nvp); 4068 4069 switch (nvpair_type(nvp)) { 4070 case DATA_TYPE_STRING: 4071 fnvlist_add_string(stats->zns_string, name, 4072 fnvpair_value_string(nvp)); 4073 break; 4074 case DATA_TYPE_UINT64: 4075 fnvlist_add_uint64(stats->zns_uint64, name, 4076 fnvpair_value_uint64(nvp)); 4077 break; 4078 case DATA_TYPE_BOOLEAN: 4079 fnvlist_add_boolean(stats->zns_boolean, name); 4080 break; 4081 case DATA_TYPE_NVLIST: 4082 if (nvpair_value_nvlist(nvp, &list) == 0) 4083 collect_nvlist_stats(list, stats); 4084 break; 4085 case DATA_TYPE_NVLIST_ARRAY: 4086 if (nvpair_value_nvlist_array(nvp, &array, &items) != 0) 4087 break; 4088 4089 for (i = 0; i < items; i++) { 4090 collect_nvlist_stats(array[i], stats); 4091 4092 /* collect stats on leaf vdev */ 4093 if (strcmp(name, "children") == 0) { 4094 size_t size; 4095 4096 (void) nvlist_size(array[i], &size, 4097 NV_ENCODE_XDR); 4098 stats->zns_leaf_total += size; 4099 if (size > stats->zns_leaf_largest) 4100 stats->zns_leaf_largest = size; 4101 stats->zns_leaf_count++; 4102 } 4103 } 4104 break; 4105 default: 4106 (void) printf("skip type %d!\n", (int)nvpair_type(nvp)); 4107 } 4108 } 4109 } 4110 4111 static void 4112 dump_nvlist_stats(nvlist_t *nvl, size_t cap) 4113 { 4114 zdb_nvl_stats_t stats = { 0 }; 4115 size_t size, sum = 0, total; 4116 size_t noise; 4117 4118 /* requires nvlist with non-unique names for stat collection */ 4119 VERIFY0(nvlist_alloc(&stats.zns_string, 0, 0)); 4120 VERIFY0(nvlist_alloc(&stats.zns_uint64, 0, 0)); 4121 VERIFY0(nvlist_alloc(&stats.zns_boolean, 0, 0)); 4122 VERIFY0(nvlist_size(stats.zns_boolean, &noise, NV_ENCODE_XDR)); 4123 4124 (void) printf("\n\nZFS Label NVList Config Stats:\n"); 4125 4126 VERIFY0(nvlist_size(nvl, &total, NV_ENCODE_XDR)); 4127 (void) printf(" %d bytes used, %d bytes free (using %4.1f%%)\n\n", 4128 (int)total, (int)(cap - total), 100.0 * total / cap); 4129 4130 collect_nvlist_stats(nvl, &stats); 4131 4132 VERIFY0(nvlist_size(stats.zns_uint64, &size, NV_ENCODE_XDR)); 4133 size -= noise; 4134 sum += size; 4135 (void) printf("%12s %4d %6d bytes (%5.2f%%)\n", "integers:", 4136 (int)fnvlist_num_pairs(stats.zns_uint64), 4137 (int)size, 100.0 * size / total); 4138 4139 VERIFY0(nvlist_size(stats.zns_string, &size, NV_ENCODE_XDR)); 4140 size -= noise; 4141 sum += size; 4142 (void) printf("%12s %4d %6d bytes (%5.2f%%)\n", "strings:", 4143 (int)fnvlist_num_pairs(stats.zns_string), 4144 (int)size, 100.0 * size / total); 4145 4146 VERIFY0(nvlist_size(stats.zns_boolean, &size, NV_ENCODE_XDR)); 4147 size -= noise; 4148 sum += size; 4149 (void) printf("%12s %4d %6d bytes (%5.2f%%)\n", "booleans:", 4150 (int)fnvlist_num_pairs(stats.zns_boolean), 4151 (int)size, 100.0 * size / total); 4152 4153 size = total - sum; /* treat remainder as nvlist overhead */ 4154 (void) printf("%12s %4d %6d bytes (%5.2f%%)\n\n", "nvlists:", 4155 stats.zns_list_count, (int)size, 100.0 * size / total); 4156 4157 if (stats.zns_leaf_count > 0) { 4158 size_t average = stats.zns_leaf_total / stats.zns_leaf_count; 4159 4160 (void) printf("%12s %4d %6d bytes average\n", "leaf vdevs:", 4161 stats.zns_leaf_count, (int)average); 4162 (void) printf("%24d bytes largest\n", 4163 (int)stats.zns_leaf_largest); 4164 4165 if (dump_opt['l'] >= 3 && average > 0) 4166 (void) printf(" space for %d additional leaf vdevs\n", 4167 (int)((cap - total) / average)); 4168 } 4169 (void) printf("\n"); 4170 4171 nvlist_free(stats.zns_string); 4172 nvlist_free(stats.zns_uint64); 4173 nvlist_free(stats.zns_boolean); 4174 } 4175 4176 typedef struct cksum_record { 4177 zio_cksum_t cksum; 4178 boolean_t labels[VDEV_LABELS]; 4179 avl_node_t link; 4180 } cksum_record_t; 4181 4182 static int 4183 cksum_record_compare(const void *x1, const void *x2) 4184 { 4185 const cksum_record_t *l = (cksum_record_t *)x1; 4186 const cksum_record_t *r = (cksum_record_t *)x2; 4187 int arraysize = ARRAY_SIZE(l->cksum.zc_word); 4188 int difference = 0; 4189 4190 for (int i = 0; i < arraysize; i++) { 4191 difference = TREE_CMP(l->cksum.zc_word[i], r->cksum.zc_word[i]); 4192 if (difference) 4193 break; 4194 } 4195 4196 return (difference); 4197 } 4198 4199 static cksum_record_t * 4200 cksum_record_alloc(zio_cksum_t *cksum, int l) 4201 { 4202 cksum_record_t *rec; 4203 4204 rec = umem_zalloc(sizeof (*rec), UMEM_NOFAIL); 4205 rec->cksum = *cksum; 4206 rec->labels[l] = B_TRUE; 4207 4208 return (rec); 4209 } 4210 4211 static cksum_record_t * 4212 cksum_record_lookup(avl_tree_t *tree, zio_cksum_t *cksum) 4213 { 4214 cksum_record_t lookup = { .cksum = *cksum }; 4215 avl_index_t where; 4216 4217 return (avl_find(tree, &lookup, &where)); 4218 } 4219 4220 static cksum_record_t * 4221 cksum_record_insert(avl_tree_t *tree, zio_cksum_t *cksum, int l) 4222 { 4223 cksum_record_t *rec; 4224 4225 rec = cksum_record_lookup(tree, cksum); 4226 if (rec) { 4227 rec->labels[l] = B_TRUE; 4228 } else { 4229 rec = cksum_record_alloc(cksum, l); 4230 avl_add(tree, rec); 4231 } 4232 4233 return (rec); 4234 } 4235 4236 static int 4237 first_label(cksum_record_t *rec) 4238 { 4239 for (int i = 0; i < VDEV_LABELS; i++) 4240 if (rec->labels[i]) 4241 return (i); 4242 4243 return (-1); 4244 } 4245 4246 static void 4247 print_label_numbers(const char *prefix, const cksum_record_t *rec) 4248 { 4249 fputs(prefix, stdout); 4250 for (int i = 0; i < VDEV_LABELS; i++) 4251 if (rec->labels[i] == B_TRUE) 4252 printf("%d ", i); 4253 putchar('\n'); 4254 } 4255 4256 #define MAX_UBERBLOCK_COUNT (VDEV_UBERBLOCK_RING >> UBERBLOCK_SHIFT) 4257 4258 typedef struct zdb_label { 4259 vdev_label_t label; 4260 uint64_t label_offset; 4261 nvlist_t *config_nv; 4262 cksum_record_t *config; 4263 cksum_record_t *uberblocks[MAX_UBERBLOCK_COUNT]; 4264 boolean_t header_printed; 4265 boolean_t read_failed; 4266 boolean_t cksum_valid; 4267 } zdb_label_t; 4268 4269 static void 4270 print_label_header(zdb_label_t *label, int l) 4271 { 4272 4273 if (dump_opt['q']) 4274 return; 4275 4276 if (label->header_printed == B_TRUE) 4277 return; 4278 4279 (void) printf("------------------------------------\n"); 4280 (void) printf("LABEL %d %s\n", l, 4281 label->cksum_valid ? "" : "(Bad label cksum)"); 4282 (void) printf("------------------------------------\n"); 4283 4284 label->header_printed = B_TRUE; 4285 } 4286 4287 static void 4288 print_l2arc_header(void) 4289 { 4290 (void) printf("------------------------------------\n"); 4291 (void) printf("L2ARC device header\n"); 4292 (void) printf("------------------------------------\n"); 4293 } 4294 4295 static void 4296 print_l2arc_log_blocks(void) 4297 { 4298 (void) printf("------------------------------------\n"); 4299 (void) printf("L2ARC device log blocks\n"); 4300 (void) printf("------------------------------------\n"); 4301 } 4302 4303 static void 4304 dump_l2arc_log_entries(uint64_t log_entries, 4305 l2arc_log_ent_phys_t *le, uint64_t i) 4306 { 4307 for (int j = 0; j < log_entries; j++) { 4308 dva_t dva = le[j].le_dva; 4309 (void) printf("lb[%4llu]\tle[%4d]\tDVA asize: %llu, " 4310 "vdev: %llu, offset: %llu\n", 4311 (u_longlong_t)i, j + 1, 4312 (u_longlong_t)DVA_GET_ASIZE(&dva), 4313 (u_longlong_t)DVA_GET_VDEV(&dva), 4314 (u_longlong_t)DVA_GET_OFFSET(&dva)); 4315 (void) printf("|\t\t\t\tbirth: %llu\n", 4316 (u_longlong_t)le[j].le_birth); 4317 (void) printf("|\t\t\t\tlsize: %llu\n", 4318 (u_longlong_t)L2BLK_GET_LSIZE((&le[j])->le_prop)); 4319 (void) printf("|\t\t\t\tpsize: %llu\n", 4320 (u_longlong_t)L2BLK_GET_PSIZE((&le[j])->le_prop)); 4321 (void) printf("|\t\t\t\tcompr: %llu\n", 4322 (u_longlong_t)L2BLK_GET_COMPRESS((&le[j])->le_prop)); 4323 (void) printf("|\t\t\t\tcomplevel: %llu\n", 4324 (u_longlong_t)(&le[j])->le_complevel); 4325 (void) printf("|\t\t\t\ttype: %llu\n", 4326 (u_longlong_t)L2BLK_GET_TYPE((&le[j])->le_prop)); 4327 (void) printf("|\t\t\t\tprotected: %llu\n", 4328 (u_longlong_t)L2BLK_GET_PROTECTED((&le[j])->le_prop)); 4329 (void) printf("|\t\t\t\tprefetch: %llu\n", 4330 (u_longlong_t)L2BLK_GET_PREFETCH((&le[j])->le_prop)); 4331 (void) printf("|\t\t\t\taddress: %llu\n", 4332 (u_longlong_t)le[j].le_daddr); 4333 (void) printf("|\t\t\t\tARC state: %llu\n", 4334 (u_longlong_t)L2BLK_GET_STATE((&le[j])->le_prop)); 4335 (void) printf("|\n"); 4336 } 4337 (void) printf("\n"); 4338 } 4339 4340 static void 4341 dump_l2arc_log_blkptr(l2arc_log_blkptr_t lbps) 4342 { 4343 (void) printf("|\t\tdaddr: %llu\n", (u_longlong_t)lbps.lbp_daddr); 4344 (void) printf("|\t\tpayload_asize: %llu\n", 4345 (u_longlong_t)lbps.lbp_payload_asize); 4346 (void) printf("|\t\tpayload_start: %llu\n", 4347 (u_longlong_t)lbps.lbp_payload_start); 4348 (void) printf("|\t\tlsize: %llu\n", 4349 (u_longlong_t)L2BLK_GET_LSIZE((&lbps)->lbp_prop)); 4350 (void) printf("|\t\tasize: %llu\n", 4351 (u_longlong_t)L2BLK_GET_PSIZE((&lbps)->lbp_prop)); 4352 (void) printf("|\t\tcompralgo: %llu\n", 4353 (u_longlong_t)L2BLK_GET_COMPRESS((&lbps)->lbp_prop)); 4354 (void) printf("|\t\tcksumalgo: %llu\n", 4355 (u_longlong_t)L2BLK_GET_CHECKSUM((&lbps)->lbp_prop)); 4356 (void) printf("|\n\n"); 4357 } 4358 4359 static void 4360 dump_l2arc_log_blocks(int fd, l2arc_dev_hdr_phys_t l2dhdr, 4361 l2arc_dev_hdr_phys_t *rebuild) 4362 { 4363 l2arc_log_blk_phys_t this_lb; 4364 uint64_t asize; 4365 l2arc_log_blkptr_t lbps[2]; 4366 abd_t *abd; 4367 zio_cksum_t cksum; 4368 int failed = 0; 4369 l2arc_dev_t dev; 4370 4371 if (!dump_opt['q']) 4372 print_l2arc_log_blocks(); 4373 memcpy(lbps, l2dhdr.dh_start_lbps, sizeof (lbps)); 4374 4375 dev.l2ad_evict = l2dhdr.dh_evict; 4376 dev.l2ad_start = l2dhdr.dh_start; 4377 dev.l2ad_end = l2dhdr.dh_end; 4378 4379 if (l2dhdr.dh_start_lbps[0].lbp_daddr == 0) { 4380 /* no log blocks to read */ 4381 if (!dump_opt['q']) { 4382 (void) printf("No log blocks to read\n"); 4383 (void) printf("\n"); 4384 } 4385 return; 4386 } else { 4387 dev.l2ad_hand = lbps[0].lbp_daddr + 4388 L2BLK_GET_PSIZE((&lbps[0])->lbp_prop); 4389 } 4390 4391 dev.l2ad_first = !!(l2dhdr.dh_flags & L2ARC_DEV_HDR_EVICT_FIRST); 4392 4393 for (;;) { 4394 if (!l2arc_log_blkptr_valid(&dev, &lbps[0])) 4395 break; 4396 4397 /* L2BLK_GET_PSIZE returns aligned size for log blocks */ 4398 asize = L2BLK_GET_PSIZE((&lbps[0])->lbp_prop); 4399 if (pread64(fd, &this_lb, asize, lbps[0].lbp_daddr) != asize) { 4400 if (!dump_opt['q']) { 4401 (void) printf("Error while reading next log " 4402 "block\n\n"); 4403 } 4404 break; 4405 } 4406 4407 fletcher_4_native_varsize(&this_lb, asize, &cksum); 4408 if (!ZIO_CHECKSUM_EQUAL(cksum, lbps[0].lbp_cksum)) { 4409 failed++; 4410 if (!dump_opt['q']) { 4411 (void) printf("Invalid cksum\n"); 4412 dump_l2arc_log_blkptr(lbps[0]); 4413 } 4414 break; 4415 } 4416 4417 switch (L2BLK_GET_COMPRESS((&lbps[0])->lbp_prop)) { 4418 case ZIO_COMPRESS_OFF: 4419 break; 4420 default: 4421 abd = abd_alloc_for_io(asize, B_TRUE); 4422 abd_copy_from_buf_off(abd, &this_lb, 0, asize); 4423 if (zio_decompress_data(L2BLK_GET_COMPRESS( 4424 (&lbps[0])->lbp_prop), abd, &this_lb, 4425 asize, sizeof (this_lb), NULL) != 0) { 4426 (void) printf("L2ARC block decompression " 4427 "failed\n"); 4428 abd_free(abd); 4429 goto out; 4430 } 4431 abd_free(abd); 4432 break; 4433 } 4434 4435 if (this_lb.lb_magic == BSWAP_64(L2ARC_LOG_BLK_MAGIC)) 4436 byteswap_uint64_array(&this_lb, sizeof (this_lb)); 4437 if (this_lb.lb_magic != L2ARC_LOG_BLK_MAGIC) { 4438 if (!dump_opt['q']) 4439 (void) printf("Invalid log block magic\n\n"); 4440 break; 4441 } 4442 4443 rebuild->dh_lb_count++; 4444 rebuild->dh_lb_asize += asize; 4445 if (dump_opt['l'] > 1 && !dump_opt['q']) { 4446 (void) printf("lb[%4llu]\tmagic: %llu\n", 4447 (u_longlong_t)rebuild->dh_lb_count, 4448 (u_longlong_t)this_lb.lb_magic); 4449 dump_l2arc_log_blkptr(lbps[0]); 4450 } 4451 4452 if (dump_opt['l'] > 2 && !dump_opt['q']) 4453 dump_l2arc_log_entries(l2dhdr.dh_log_entries, 4454 this_lb.lb_entries, 4455 rebuild->dh_lb_count); 4456 4457 if (l2arc_range_check_overlap(lbps[1].lbp_payload_start, 4458 lbps[0].lbp_payload_start, dev.l2ad_evict) && 4459 !dev.l2ad_first) 4460 break; 4461 4462 lbps[0] = lbps[1]; 4463 lbps[1] = this_lb.lb_prev_lbp; 4464 } 4465 out: 4466 if (!dump_opt['q']) { 4467 (void) printf("log_blk_count:\t %llu with valid cksum\n", 4468 (u_longlong_t)rebuild->dh_lb_count); 4469 (void) printf("\t\t %d with invalid cksum\n", failed); 4470 (void) printf("log_blk_asize:\t %llu\n\n", 4471 (u_longlong_t)rebuild->dh_lb_asize); 4472 } 4473 } 4474 4475 static int 4476 dump_l2arc_header(int fd) 4477 { 4478 l2arc_dev_hdr_phys_t l2dhdr = {0}, rebuild = {0}; 4479 int error = B_FALSE; 4480 4481 if (pread64(fd, &l2dhdr, sizeof (l2dhdr), 4482 VDEV_LABEL_START_SIZE) != sizeof (l2dhdr)) { 4483 error = B_TRUE; 4484 } else { 4485 if (l2dhdr.dh_magic == BSWAP_64(L2ARC_DEV_HDR_MAGIC)) 4486 byteswap_uint64_array(&l2dhdr, sizeof (l2dhdr)); 4487 4488 if (l2dhdr.dh_magic != L2ARC_DEV_HDR_MAGIC) 4489 error = B_TRUE; 4490 } 4491 4492 if (error) { 4493 (void) printf("L2ARC device header not found\n\n"); 4494 /* Do not return an error here for backward compatibility */ 4495 return (0); 4496 } else if (!dump_opt['q']) { 4497 print_l2arc_header(); 4498 4499 (void) printf(" magic: %llu\n", 4500 (u_longlong_t)l2dhdr.dh_magic); 4501 (void) printf(" version: %llu\n", 4502 (u_longlong_t)l2dhdr.dh_version); 4503 (void) printf(" pool_guid: %llu\n", 4504 (u_longlong_t)l2dhdr.dh_spa_guid); 4505 (void) printf(" flags: %llu\n", 4506 (u_longlong_t)l2dhdr.dh_flags); 4507 (void) printf(" start_lbps[0]: %llu\n", 4508 (u_longlong_t) 4509 l2dhdr.dh_start_lbps[0].lbp_daddr); 4510 (void) printf(" start_lbps[1]: %llu\n", 4511 (u_longlong_t) 4512 l2dhdr.dh_start_lbps[1].lbp_daddr); 4513 (void) printf(" log_blk_ent: %llu\n", 4514 (u_longlong_t)l2dhdr.dh_log_entries); 4515 (void) printf(" start: %llu\n", 4516 (u_longlong_t)l2dhdr.dh_start); 4517 (void) printf(" end: %llu\n", 4518 (u_longlong_t)l2dhdr.dh_end); 4519 (void) printf(" evict: %llu\n", 4520 (u_longlong_t)l2dhdr.dh_evict); 4521 (void) printf(" lb_asize_refcount: %llu\n", 4522 (u_longlong_t)l2dhdr.dh_lb_asize); 4523 (void) printf(" lb_count_refcount: %llu\n", 4524 (u_longlong_t)l2dhdr.dh_lb_count); 4525 (void) printf(" trim_action_time: %llu\n", 4526 (u_longlong_t)l2dhdr.dh_trim_action_time); 4527 (void) printf(" trim_state: %llu\n\n", 4528 (u_longlong_t)l2dhdr.dh_trim_state); 4529 } 4530 4531 dump_l2arc_log_blocks(fd, l2dhdr, &rebuild); 4532 /* 4533 * The total aligned size of log blocks and the number of log blocks 4534 * reported in the header of the device may be less than what zdb 4535 * reports by dump_l2arc_log_blocks() which emulates l2arc_rebuild(). 4536 * This happens because dump_l2arc_log_blocks() lacks the memory 4537 * pressure valve that l2arc_rebuild() has. Thus, if we are on a system 4538 * with low memory, l2arc_rebuild will exit prematurely and dh_lb_asize 4539 * and dh_lb_count will be lower to begin with than what exists on the 4540 * device. This is normal and zdb should not exit with an error. The 4541 * opposite case should never happen though, the values reported in the 4542 * header should never be higher than what dump_l2arc_log_blocks() and 4543 * l2arc_rebuild() report. If this happens there is a leak in the 4544 * accounting of log blocks. 4545 */ 4546 if (l2dhdr.dh_lb_asize > rebuild.dh_lb_asize || 4547 l2dhdr.dh_lb_count > rebuild.dh_lb_count) 4548 return (1); 4549 4550 return (0); 4551 } 4552 4553 static void 4554 dump_config_from_label(zdb_label_t *label, size_t buflen, int l) 4555 { 4556 if (dump_opt['q']) 4557 return; 4558 4559 if ((dump_opt['l'] < 3) && (first_label(label->config) != l)) 4560 return; 4561 4562 print_label_header(label, l); 4563 dump_nvlist(label->config_nv, 4); 4564 print_label_numbers(" labels = ", label->config); 4565 4566 if (dump_opt['l'] >= 2) 4567 dump_nvlist_stats(label->config_nv, buflen); 4568 } 4569 4570 #define ZDB_MAX_UB_HEADER_SIZE 32 4571 4572 static void 4573 dump_label_uberblocks(zdb_label_t *label, uint64_t ashift, int label_num) 4574 { 4575 4576 vdev_t vd; 4577 char header[ZDB_MAX_UB_HEADER_SIZE]; 4578 4579 vd.vdev_ashift = ashift; 4580 vd.vdev_top = &vd; 4581 4582 for (int i = 0; i < VDEV_UBERBLOCK_COUNT(&vd); i++) { 4583 uint64_t uoff = VDEV_UBERBLOCK_OFFSET(&vd, i); 4584 uberblock_t *ub = (void *)((char *)&label->label + uoff); 4585 cksum_record_t *rec = label->uberblocks[i]; 4586 4587 if (rec == NULL) { 4588 if (dump_opt['u'] >= 2) { 4589 print_label_header(label, label_num); 4590 (void) printf(" Uberblock[%d] invalid\n", i); 4591 } 4592 continue; 4593 } 4594 4595 if ((dump_opt['u'] < 3) && (first_label(rec) != label_num)) 4596 continue; 4597 4598 if ((dump_opt['u'] < 4) && 4599 (ub->ub_mmp_magic == MMP_MAGIC) && ub->ub_mmp_delay && 4600 (i >= VDEV_UBERBLOCK_COUNT(&vd) - MMP_BLOCKS_PER_LABEL)) 4601 continue; 4602 4603 print_label_header(label, label_num); 4604 (void) snprintf(header, ZDB_MAX_UB_HEADER_SIZE, 4605 " Uberblock[%d]\n", i); 4606 dump_uberblock(ub, header, ""); 4607 print_label_numbers(" labels = ", rec); 4608 } 4609 } 4610 4611 static char curpath[PATH_MAX]; 4612 4613 /* 4614 * Iterate through the path components, recursively passing 4615 * current one's obj and remaining path until we find the obj 4616 * for the last one. 4617 */ 4618 static int 4619 dump_path_impl(objset_t *os, uint64_t obj, char *name, uint64_t *retobj) 4620 { 4621 int err; 4622 boolean_t header = B_TRUE; 4623 uint64_t child_obj; 4624 char *s; 4625 dmu_buf_t *db; 4626 dmu_object_info_t doi; 4627 4628 if ((s = strchr(name, '/')) != NULL) 4629 *s = '\0'; 4630 err = zap_lookup(os, obj, name, 8, 1, &child_obj); 4631 4632 (void) strlcat(curpath, name, sizeof (curpath)); 4633 4634 if (err != 0) { 4635 (void) fprintf(stderr, "failed to lookup %s: %s\n", 4636 curpath, strerror(err)); 4637 return (err); 4638 } 4639 4640 child_obj = ZFS_DIRENT_OBJ(child_obj); 4641 err = sa_buf_hold(os, child_obj, FTAG, &db); 4642 if (err != 0) { 4643 (void) fprintf(stderr, 4644 "failed to get SA dbuf for obj %llu: %s\n", 4645 (u_longlong_t)child_obj, strerror(err)); 4646 return (EINVAL); 4647 } 4648 dmu_object_info_from_db(db, &doi); 4649 sa_buf_rele(db, FTAG); 4650 4651 if (doi.doi_bonus_type != DMU_OT_SA && 4652 doi.doi_bonus_type != DMU_OT_ZNODE) { 4653 (void) fprintf(stderr, "invalid bonus type %d for obj %llu\n", 4654 doi.doi_bonus_type, (u_longlong_t)child_obj); 4655 return (EINVAL); 4656 } 4657 4658 if (dump_opt['v'] > 6) { 4659 (void) printf("obj=%llu %s type=%d bonustype=%d\n", 4660 (u_longlong_t)child_obj, curpath, doi.doi_type, 4661 doi.doi_bonus_type); 4662 } 4663 4664 (void) strlcat(curpath, "/", sizeof (curpath)); 4665 4666 switch (doi.doi_type) { 4667 case DMU_OT_DIRECTORY_CONTENTS: 4668 if (s != NULL && *(s + 1) != '\0') 4669 return (dump_path_impl(os, child_obj, s + 1, retobj)); 4670 zfs_fallthrough; 4671 case DMU_OT_PLAIN_FILE_CONTENTS: 4672 if (retobj != NULL) { 4673 *retobj = child_obj; 4674 } else { 4675 dump_object(os, child_obj, dump_opt['v'], &header, 4676 NULL, 0); 4677 } 4678 return (0); 4679 default: 4680 (void) fprintf(stderr, "object %llu has non-file/directory " 4681 "type %d\n", (u_longlong_t)obj, doi.doi_type); 4682 break; 4683 } 4684 4685 return (EINVAL); 4686 } 4687 4688 /* 4689 * Dump the blocks for the object specified by path inside the dataset. 4690 */ 4691 static int 4692 dump_path(char *ds, char *path, uint64_t *retobj) 4693 { 4694 int err; 4695 objset_t *os; 4696 uint64_t root_obj; 4697 4698 err = open_objset(ds, FTAG, &os); 4699 if (err != 0) 4700 return (err); 4701 4702 err = zap_lookup(os, MASTER_NODE_OBJ, ZFS_ROOT_OBJ, 8, 1, &root_obj); 4703 if (err != 0) { 4704 (void) fprintf(stderr, "can't lookup root znode: %s\n", 4705 strerror(err)); 4706 close_objset(os, FTAG); 4707 return (EINVAL); 4708 } 4709 4710 (void) snprintf(curpath, sizeof (curpath), "dataset=%s path=/", ds); 4711 4712 err = dump_path_impl(os, root_obj, path, retobj); 4713 4714 close_objset(os, FTAG); 4715 return (err); 4716 } 4717 4718 static int 4719 zdb_copy_object(objset_t *os, uint64_t srcobj, char *destfile) 4720 { 4721 int err = 0; 4722 uint64_t size, readsize, oursize, offset; 4723 ssize_t writesize; 4724 sa_handle_t *hdl; 4725 4726 (void) printf("Copying object %" PRIu64 " to file %s\n", srcobj, 4727 destfile); 4728 4729 VERIFY3P(os, ==, sa_os); 4730 if ((err = sa_handle_get(os, srcobj, NULL, SA_HDL_PRIVATE, &hdl))) { 4731 (void) printf("Failed to get handle for SA znode\n"); 4732 return (err); 4733 } 4734 if ((err = sa_lookup(hdl, sa_attr_table[ZPL_SIZE], &size, 8))) { 4735 (void) sa_handle_destroy(hdl); 4736 return (err); 4737 } 4738 (void) sa_handle_destroy(hdl); 4739 4740 (void) printf("Object %" PRIu64 " is %" PRIu64 " bytes\n", srcobj, 4741 size); 4742 if (size == 0) { 4743 return (EINVAL); 4744 } 4745 4746 int fd = open(destfile, O_WRONLY | O_CREAT | O_TRUNC, 0644); 4747 if (fd == -1) 4748 return (errno); 4749 /* 4750 * We cap the size at 1 mebibyte here to prevent 4751 * allocation failures and nigh-infinite printing if the 4752 * object is extremely large. 4753 */ 4754 oursize = MIN(size, 1 << 20); 4755 offset = 0; 4756 char *buf = kmem_alloc(oursize, KM_NOSLEEP); 4757 if (buf == NULL) { 4758 (void) close(fd); 4759 return (ENOMEM); 4760 } 4761 4762 while (offset < size) { 4763 readsize = MIN(size - offset, 1 << 20); 4764 err = dmu_read(os, srcobj, offset, readsize, buf, 0); 4765 if (err != 0) { 4766 (void) printf("got error %u from dmu_read\n", err); 4767 kmem_free(buf, oursize); 4768 (void) close(fd); 4769 return (err); 4770 } 4771 if (dump_opt['v'] > 3) { 4772 (void) printf("Read offset=%" PRIu64 " size=%" PRIu64 4773 " error=%d\n", offset, readsize, err); 4774 } 4775 4776 writesize = write(fd, buf, readsize); 4777 if (writesize < 0) { 4778 err = errno; 4779 break; 4780 } else if (writesize != readsize) { 4781 /* Incomplete write */ 4782 (void) fprintf(stderr, "Short write, only wrote %llu of" 4783 " %" PRIu64 " bytes, exiting...\n", 4784 (u_longlong_t)writesize, readsize); 4785 break; 4786 } 4787 4788 offset += readsize; 4789 } 4790 4791 (void) close(fd); 4792 4793 if (buf != NULL) 4794 kmem_free(buf, oursize); 4795 4796 return (err); 4797 } 4798 4799 static boolean_t 4800 label_cksum_valid(vdev_label_t *label, uint64_t offset) 4801 { 4802 zio_checksum_info_t *ci = &zio_checksum_table[ZIO_CHECKSUM_LABEL]; 4803 zio_cksum_t expected_cksum; 4804 zio_cksum_t actual_cksum; 4805 zio_cksum_t verifier; 4806 zio_eck_t *eck; 4807 int byteswap; 4808 4809 void *data = (char *)label + offsetof(vdev_label_t, vl_vdev_phys); 4810 eck = (zio_eck_t *)((char *)(data) + VDEV_PHYS_SIZE) - 1; 4811 4812 offset += offsetof(vdev_label_t, vl_vdev_phys); 4813 ZIO_SET_CHECKSUM(&verifier, offset, 0, 0, 0); 4814 4815 byteswap = (eck->zec_magic == BSWAP_64(ZEC_MAGIC)); 4816 if (byteswap) 4817 byteswap_uint64_array(&verifier, sizeof (zio_cksum_t)); 4818 4819 expected_cksum = eck->zec_cksum; 4820 eck->zec_cksum = verifier; 4821 4822 abd_t *abd = abd_get_from_buf(data, VDEV_PHYS_SIZE); 4823 ci->ci_func[byteswap](abd, VDEV_PHYS_SIZE, NULL, &actual_cksum); 4824 abd_free(abd); 4825 4826 if (byteswap) 4827 byteswap_uint64_array(&expected_cksum, sizeof (zio_cksum_t)); 4828 4829 if (ZIO_CHECKSUM_EQUAL(actual_cksum, expected_cksum)) 4830 return (B_TRUE); 4831 4832 return (B_FALSE); 4833 } 4834 4835 static int 4836 dump_label(const char *dev) 4837 { 4838 char path[MAXPATHLEN]; 4839 zdb_label_t labels[VDEV_LABELS] = {{{{0}}}}; 4840 uint64_t psize, ashift, l2cache; 4841 struct stat64 statbuf; 4842 boolean_t config_found = B_FALSE; 4843 boolean_t error = B_FALSE; 4844 boolean_t read_l2arc_header = B_FALSE; 4845 avl_tree_t config_tree; 4846 avl_tree_t uberblock_tree; 4847 void *node, *cookie; 4848 int fd; 4849 4850 /* 4851 * Check if we were given absolute path and use it as is. 4852 * Otherwise if the provided vdev name doesn't point to a file, 4853 * try prepending expected disk paths and partition numbers. 4854 */ 4855 (void) strlcpy(path, dev, sizeof (path)); 4856 if (dev[0] != '/' && stat64(path, &statbuf) != 0) { 4857 int error; 4858 4859 error = zfs_resolve_shortname(dev, path, MAXPATHLEN); 4860 if (error == 0 && zfs_dev_is_whole_disk(path)) { 4861 if (zfs_append_partition(path, MAXPATHLEN) == -1) 4862 error = ENOENT; 4863 } 4864 4865 if (error || (stat64(path, &statbuf) != 0)) { 4866 (void) printf("failed to find device %s, try " 4867 "specifying absolute path instead\n", dev); 4868 return (1); 4869 } 4870 } 4871 4872 if ((fd = open64(path, O_RDONLY)) < 0) { 4873 (void) printf("cannot open '%s': %s\n", path, strerror(errno)); 4874 exit(1); 4875 } 4876 4877 if (fstat64_blk(fd, &statbuf) != 0) { 4878 (void) printf("failed to stat '%s': %s\n", path, 4879 strerror(errno)); 4880 (void) close(fd); 4881 exit(1); 4882 } 4883 4884 if (S_ISBLK(statbuf.st_mode) && zfs_dev_flush(fd) != 0) 4885 (void) printf("failed to invalidate cache '%s' : %s\n", path, 4886 strerror(errno)); 4887 4888 avl_create(&config_tree, cksum_record_compare, 4889 sizeof (cksum_record_t), offsetof(cksum_record_t, link)); 4890 avl_create(&uberblock_tree, cksum_record_compare, 4891 sizeof (cksum_record_t), offsetof(cksum_record_t, link)); 4892 4893 psize = statbuf.st_size; 4894 psize = P2ALIGN(psize, (uint64_t)sizeof (vdev_label_t)); 4895 ashift = SPA_MINBLOCKSHIFT; 4896 4897 /* 4898 * 1. Read the label from disk 4899 * 2. Verify label cksum 4900 * 3. Unpack the configuration and insert in config tree. 4901 * 4. Traverse all uberblocks and insert in uberblock tree. 4902 */ 4903 for (int l = 0; l < VDEV_LABELS; l++) { 4904 zdb_label_t *label = &labels[l]; 4905 char *buf = label->label.vl_vdev_phys.vp_nvlist; 4906 size_t buflen = sizeof (label->label.vl_vdev_phys.vp_nvlist); 4907 nvlist_t *config; 4908 cksum_record_t *rec; 4909 zio_cksum_t cksum; 4910 vdev_t vd; 4911 4912 label->label_offset = vdev_label_offset(psize, l, 0); 4913 4914 if (pread64(fd, &label->label, sizeof (label->label), 4915 label->label_offset) != sizeof (label->label)) { 4916 if (!dump_opt['q']) 4917 (void) printf("failed to read label %d\n", l); 4918 label->read_failed = B_TRUE; 4919 error = B_TRUE; 4920 continue; 4921 } 4922 4923 label->read_failed = B_FALSE; 4924 label->cksum_valid = label_cksum_valid(&label->label, 4925 label->label_offset); 4926 4927 if (nvlist_unpack(buf, buflen, &config, 0) == 0) { 4928 nvlist_t *vdev_tree = NULL; 4929 size_t size; 4930 4931 if ((nvlist_lookup_nvlist(config, 4932 ZPOOL_CONFIG_VDEV_TREE, &vdev_tree) != 0) || 4933 (nvlist_lookup_uint64(vdev_tree, 4934 ZPOOL_CONFIG_ASHIFT, &ashift) != 0)) 4935 ashift = SPA_MINBLOCKSHIFT; 4936 4937 if (nvlist_size(config, &size, NV_ENCODE_XDR) != 0) 4938 size = buflen; 4939 4940 /* If the device is a cache device clear the header. */ 4941 if (!read_l2arc_header) { 4942 if (nvlist_lookup_uint64(config, 4943 ZPOOL_CONFIG_POOL_STATE, &l2cache) == 0 && 4944 l2cache == POOL_STATE_L2CACHE) { 4945 read_l2arc_header = B_TRUE; 4946 } 4947 } 4948 4949 fletcher_4_native_varsize(buf, size, &cksum); 4950 rec = cksum_record_insert(&config_tree, &cksum, l); 4951 4952 label->config = rec; 4953 label->config_nv = config; 4954 config_found = B_TRUE; 4955 } else { 4956 error = B_TRUE; 4957 } 4958 4959 vd.vdev_ashift = ashift; 4960 vd.vdev_top = &vd; 4961 4962 for (int i = 0; i < VDEV_UBERBLOCK_COUNT(&vd); i++) { 4963 uint64_t uoff = VDEV_UBERBLOCK_OFFSET(&vd, i); 4964 uberblock_t *ub = (void *)((char *)label + uoff); 4965 4966 if (uberblock_verify(ub)) 4967 continue; 4968 4969 fletcher_4_native_varsize(ub, sizeof (*ub), &cksum); 4970 rec = cksum_record_insert(&uberblock_tree, &cksum, l); 4971 4972 label->uberblocks[i] = rec; 4973 } 4974 } 4975 4976 /* 4977 * Dump the label and uberblocks. 4978 */ 4979 for (int l = 0; l < VDEV_LABELS; l++) { 4980 zdb_label_t *label = &labels[l]; 4981 size_t buflen = sizeof (label->label.vl_vdev_phys.vp_nvlist); 4982 4983 if (label->read_failed == B_TRUE) 4984 continue; 4985 4986 if (label->config_nv) { 4987 dump_config_from_label(label, buflen, l); 4988 } else { 4989 if (!dump_opt['q']) 4990 (void) printf("failed to unpack label %d\n", l); 4991 } 4992 4993 if (dump_opt['u']) 4994 dump_label_uberblocks(label, ashift, l); 4995 4996 nvlist_free(label->config_nv); 4997 } 4998 4999 /* 5000 * Dump the L2ARC header, if existent. 5001 */ 5002 if (read_l2arc_header) 5003 error |= dump_l2arc_header(fd); 5004 5005 cookie = NULL; 5006 while ((node = avl_destroy_nodes(&config_tree, &cookie)) != NULL) 5007 umem_free(node, sizeof (cksum_record_t)); 5008 5009 cookie = NULL; 5010 while ((node = avl_destroy_nodes(&uberblock_tree, &cookie)) != NULL) 5011 umem_free(node, sizeof (cksum_record_t)); 5012 5013 avl_destroy(&config_tree); 5014 avl_destroy(&uberblock_tree); 5015 5016 (void) close(fd); 5017 5018 return (config_found == B_FALSE ? 2 : 5019 (error == B_TRUE ? 1 : 0)); 5020 } 5021 5022 static uint64_t dataset_feature_count[SPA_FEATURES]; 5023 static uint64_t global_feature_count[SPA_FEATURES]; 5024 static uint64_t remap_deadlist_count = 0; 5025 5026 static int 5027 dump_one_objset(const char *dsname, void *arg) 5028 { 5029 (void) arg; 5030 int error; 5031 objset_t *os; 5032 spa_feature_t f; 5033 5034 error = open_objset(dsname, FTAG, &os); 5035 if (error != 0) 5036 return (0); 5037 5038 for (f = 0; f < SPA_FEATURES; f++) { 5039 if (!dsl_dataset_feature_is_active(dmu_objset_ds(os), f)) 5040 continue; 5041 ASSERT(spa_feature_table[f].fi_flags & 5042 ZFEATURE_FLAG_PER_DATASET); 5043 dataset_feature_count[f]++; 5044 } 5045 5046 if (dsl_dataset_remap_deadlist_exists(dmu_objset_ds(os))) { 5047 remap_deadlist_count++; 5048 } 5049 5050 for (dsl_bookmark_node_t *dbn = 5051 avl_first(&dmu_objset_ds(os)->ds_bookmarks); dbn != NULL; 5052 dbn = AVL_NEXT(&dmu_objset_ds(os)->ds_bookmarks, dbn)) { 5053 mos_obj_refd(dbn->dbn_phys.zbm_redaction_obj); 5054 if (dbn->dbn_phys.zbm_redaction_obj != 0) 5055 global_feature_count[SPA_FEATURE_REDACTION_BOOKMARKS]++; 5056 if (dbn->dbn_phys.zbm_flags & ZBM_FLAG_HAS_FBN) 5057 global_feature_count[SPA_FEATURE_BOOKMARK_WRITTEN]++; 5058 } 5059 5060 if (dsl_deadlist_is_open(&dmu_objset_ds(os)->ds_dir->dd_livelist) && 5061 !dmu_objset_is_snapshot(os)) { 5062 global_feature_count[SPA_FEATURE_LIVELIST]++; 5063 } 5064 5065 dump_objset(os); 5066 close_objset(os, FTAG); 5067 fuid_table_destroy(); 5068 return (0); 5069 } 5070 5071 /* 5072 * Block statistics. 5073 */ 5074 #define PSIZE_HISTO_SIZE (SPA_OLD_MAXBLOCKSIZE / SPA_MINBLOCKSIZE + 2) 5075 typedef struct zdb_blkstats { 5076 uint64_t zb_asize; 5077 uint64_t zb_lsize; 5078 uint64_t zb_psize; 5079 uint64_t zb_count; 5080 uint64_t zb_gangs; 5081 uint64_t zb_ditto_samevdev; 5082 uint64_t zb_ditto_same_ms; 5083 uint64_t zb_psize_histogram[PSIZE_HISTO_SIZE]; 5084 } zdb_blkstats_t; 5085 5086 /* 5087 * Extended object types to report deferred frees and dedup auto-ditto blocks. 5088 */ 5089 #define ZDB_OT_DEFERRED (DMU_OT_NUMTYPES + 0) 5090 #define ZDB_OT_DITTO (DMU_OT_NUMTYPES + 1) 5091 #define ZDB_OT_OTHER (DMU_OT_NUMTYPES + 2) 5092 #define ZDB_OT_TOTAL (DMU_OT_NUMTYPES + 3) 5093 5094 static const char *zdb_ot_extname[] = { 5095 "deferred free", 5096 "dedup ditto", 5097 "other", 5098 "Total", 5099 }; 5100 5101 #define ZB_TOTAL DN_MAX_LEVELS 5102 #define SPA_MAX_FOR_16M (SPA_MAXBLOCKSHIFT+1) 5103 5104 typedef struct zdb_cb { 5105 zdb_blkstats_t zcb_type[ZB_TOTAL + 1][ZDB_OT_TOTAL + 1]; 5106 uint64_t zcb_removing_size; 5107 uint64_t zcb_checkpoint_size; 5108 uint64_t zcb_dedup_asize; 5109 uint64_t zcb_dedup_blocks; 5110 uint64_t zcb_psize_count[SPA_MAX_FOR_16M]; 5111 uint64_t zcb_lsize_count[SPA_MAX_FOR_16M]; 5112 uint64_t zcb_asize_count[SPA_MAX_FOR_16M]; 5113 uint64_t zcb_psize_len[SPA_MAX_FOR_16M]; 5114 uint64_t zcb_lsize_len[SPA_MAX_FOR_16M]; 5115 uint64_t zcb_asize_len[SPA_MAX_FOR_16M]; 5116 uint64_t zcb_psize_total; 5117 uint64_t zcb_lsize_total; 5118 uint64_t zcb_asize_total; 5119 uint64_t zcb_embedded_blocks[NUM_BP_EMBEDDED_TYPES]; 5120 uint64_t zcb_embedded_histogram[NUM_BP_EMBEDDED_TYPES] 5121 [BPE_PAYLOAD_SIZE + 1]; 5122 uint64_t zcb_start; 5123 hrtime_t zcb_lastprint; 5124 uint64_t zcb_totalasize; 5125 uint64_t zcb_errors[256]; 5126 int zcb_readfails; 5127 int zcb_haderrors; 5128 spa_t *zcb_spa; 5129 uint32_t **zcb_vd_obsolete_counts; 5130 } zdb_cb_t; 5131 5132 /* test if two DVA offsets from same vdev are within the same metaslab */ 5133 static boolean_t 5134 same_metaslab(spa_t *spa, uint64_t vdev, uint64_t off1, uint64_t off2) 5135 { 5136 vdev_t *vd = vdev_lookup_top(spa, vdev); 5137 uint64_t ms_shift = vd->vdev_ms_shift; 5138 5139 return ((off1 >> ms_shift) == (off2 >> ms_shift)); 5140 } 5141 5142 /* 5143 * Used to simplify reporting of the histogram data. 5144 */ 5145 typedef struct one_histo { 5146 const char *name; 5147 uint64_t *count; 5148 uint64_t *len; 5149 uint64_t cumulative; 5150 } one_histo_t; 5151 5152 /* 5153 * The number of separate histograms processed for psize, lsize and asize. 5154 */ 5155 #define NUM_HISTO 3 5156 5157 /* 5158 * This routine will create a fixed column size output of three different 5159 * histograms showing by blocksize of 512 - 2^ SPA_MAX_FOR_16M 5160 * the count, length and cumulative length of the psize, lsize and 5161 * asize blocks. 5162 * 5163 * All three types of blocks are listed on a single line 5164 * 5165 * By default the table is printed in nicenumber format (e.g. 123K) but 5166 * if the '-P' parameter is specified then the full raw number (parseable) 5167 * is printed out. 5168 */ 5169 static void 5170 dump_size_histograms(zdb_cb_t *zcb) 5171 { 5172 /* 5173 * A temporary buffer that allows us to convert a number into 5174 * a string using zdb_nicenumber to allow either raw or human 5175 * readable numbers to be output. 5176 */ 5177 char numbuf[32]; 5178 5179 /* 5180 * Define titles which are used in the headers of the tables 5181 * printed by this routine. 5182 */ 5183 const char blocksize_title1[] = "block"; 5184 const char blocksize_title2[] = "size"; 5185 const char count_title[] = "Count"; 5186 const char length_title[] = "Size"; 5187 const char cumulative_title[] = "Cum."; 5188 5189 /* 5190 * Setup the histogram arrays (psize, lsize, and asize). 5191 */ 5192 one_histo_t parm_histo[NUM_HISTO]; 5193 5194 parm_histo[0].name = "psize"; 5195 parm_histo[0].count = zcb->zcb_psize_count; 5196 parm_histo[0].len = zcb->zcb_psize_len; 5197 parm_histo[0].cumulative = 0; 5198 5199 parm_histo[1].name = "lsize"; 5200 parm_histo[1].count = zcb->zcb_lsize_count; 5201 parm_histo[1].len = zcb->zcb_lsize_len; 5202 parm_histo[1].cumulative = 0; 5203 5204 parm_histo[2].name = "asize"; 5205 parm_histo[2].count = zcb->zcb_asize_count; 5206 parm_histo[2].len = zcb->zcb_asize_len; 5207 parm_histo[2].cumulative = 0; 5208 5209 5210 (void) printf("\nBlock Size Histogram\n"); 5211 /* 5212 * Print the first line titles 5213 */ 5214 if (dump_opt['P']) 5215 (void) printf("\n%s\t", blocksize_title1); 5216 else 5217 (void) printf("\n%7s ", blocksize_title1); 5218 5219 for (int j = 0; j < NUM_HISTO; j++) { 5220 if (dump_opt['P']) { 5221 if (j < NUM_HISTO - 1) { 5222 (void) printf("%s\t\t\t", parm_histo[j].name); 5223 } else { 5224 /* Don't print trailing spaces */ 5225 (void) printf(" %s", parm_histo[j].name); 5226 } 5227 } else { 5228 if (j < NUM_HISTO - 1) { 5229 /* Left aligned strings in the output */ 5230 (void) printf("%-7s ", 5231 parm_histo[j].name); 5232 } else { 5233 /* Don't print trailing spaces */ 5234 (void) printf("%s", parm_histo[j].name); 5235 } 5236 } 5237 } 5238 (void) printf("\n"); 5239 5240 /* 5241 * Print the second line titles 5242 */ 5243 if (dump_opt['P']) { 5244 (void) printf("%s\t", blocksize_title2); 5245 } else { 5246 (void) printf("%7s ", blocksize_title2); 5247 } 5248 5249 for (int i = 0; i < NUM_HISTO; i++) { 5250 if (dump_opt['P']) { 5251 (void) printf("%s\t%s\t%s\t", 5252 count_title, length_title, cumulative_title); 5253 } else { 5254 (void) printf("%7s%7s%7s", 5255 count_title, length_title, cumulative_title); 5256 } 5257 } 5258 (void) printf("\n"); 5259 5260 /* 5261 * Print the rows 5262 */ 5263 for (int i = SPA_MINBLOCKSHIFT; i < SPA_MAX_FOR_16M; i++) { 5264 5265 /* 5266 * Print the first column showing the blocksize 5267 */ 5268 zdb_nicenum((1ULL << i), numbuf, sizeof (numbuf)); 5269 5270 if (dump_opt['P']) { 5271 printf("%s", numbuf); 5272 } else { 5273 printf("%7s:", numbuf); 5274 } 5275 5276 /* 5277 * Print the remaining set of 3 columns per size: 5278 * for psize, lsize and asize 5279 */ 5280 for (int j = 0; j < NUM_HISTO; j++) { 5281 parm_histo[j].cumulative += parm_histo[j].len[i]; 5282 5283 zdb_nicenum(parm_histo[j].count[i], 5284 numbuf, sizeof (numbuf)); 5285 if (dump_opt['P']) 5286 (void) printf("\t%s", numbuf); 5287 else 5288 (void) printf("%7s", numbuf); 5289 5290 zdb_nicenum(parm_histo[j].len[i], 5291 numbuf, sizeof (numbuf)); 5292 if (dump_opt['P']) 5293 (void) printf("\t%s", numbuf); 5294 else 5295 (void) printf("%7s", numbuf); 5296 5297 zdb_nicenum(parm_histo[j].cumulative, 5298 numbuf, sizeof (numbuf)); 5299 if (dump_opt['P']) 5300 (void) printf("\t%s", numbuf); 5301 else 5302 (void) printf("%7s", numbuf); 5303 } 5304 (void) printf("\n"); 5305 } 5306 } 5307 5308 static void 5309 zdb_count_block(zdb_cb_t *zcb, zilog_t *zilog, const blkptr_t *bp, 5310 dmu_object_type_t type) 5311 { 5312 uint64_t refcnt = 0; 5313 int i; 5314 5315 ASSERT(type < ZDB_OT_TOTAL); 5316 5317 if (zilog && zil_bp_tree_add(zilog, bp) != 0) 5318 return; 5319 5320 spa_config_enter(zcb->zcb_spa, SCL_CONFIG, FTAG, RW_READER); 5321 5322 for (i = 0; i < 4; i++) { 5323 int l = (i < 2) ? BP_GET_LEVEL(bp) : ZB_TOTAL; 5324 int t = (i & 1) ? type : ZDB_OT_TOTAL; 5325 int equal; 5326 zdb_blkstats_t *zb = &zcb->zcb_type[l][t]; 5327 5328 zb->zb_asize += BP_GET_ASIZE(bp); 5329 zb->zb_lsize += BP_GET_LSIZE(bp); 5330 zb->zb_psize += BP_GET_PSIZE(bp); 5331 zb->zb_count++; 5332 5333 /* 5334 * The histogram is only big enough to record blocks up to 5335 * SPA_OLD_MAXBLOCKSIZE; larger blocks go into the last, 5336 * "other", bucket. 5337 */ 5338 unsigned idx = BP_GET_PSIZE(bp) >> SPA_MINBLOCKSHIFT; 5339 idx = MIN(idx, SPA_OLD_MAXBLOCKSIZE / SPA_MINBLOCKSIZE + 1); 5340 zb->zb_psize_histogram[idx]++; 5341 5342 zb->zb_gangs += BP_COUNT_GANG(bp); 5343 5344 switch (BP_GET_NDVAS(bp)) { 5345 case 2: 5346 if (DVA_GET_VDEV(&bp->blk_dva[0]) == 5347 DVA_GET_VDEV(&bp->blk_dva[1])) { 5348 zb->zb_ditto_samevdev++; 5349 5350 if (same_metaslab(zcb->zcb_spa, 5351 DVA_GET_VDEV(&bp->blk_dva[0]), 5352 DVA_GET_OFFSET(&bp->blk_dva[0]), 5353 DVA_GET_OFFSET(&bp->blk_dva[1]))) 5354 zb->zb_ditto_same_ms++; 5355 } 5356 break; 5357 case 3: 5358 equal = (DVA_GET_VDEV(&bp->blk_dva[0]) == 5359 DVA_GET_VDEV(&bp->blk_dva[1])) + 5360 (DVA_GET_VDEV(&bp->blk_dva[0]) == 5361 DVA_GET_VDEV(&bp->blk_dva[2])) + 5362 (DVA_GET_VDEV(&bp->blk_dva[1]) == 5363 DVA_GET_VDEV(&bp->blk_dva[2])); 5364 if (equal != 0) { 5365 zb->zb_ditto_samevdev++; 5366 5367 if (DVA_GET_VDEV(&bp->blk_dva[0]) == 5368 DVA_GET_VDEV(&bp->blk_dva[1]) && 5369 same_metaslab(zcb->zcb_spa, 5370 DVA_GET_VDEV(&bp->blk_dva[0]), 5371 DVA_GET_OFFSET(&bp->blk_dva[0]), 5372 DVA_GET_OFFSET(&bp->blk_dva[1]))) 5373 zb->zb_ditto_same_ms++; 5374 else if (DVA_GET_VDEV(&bp->blk_dva[0]) == 5375 DVA_GET_VDEV(&bp->blk_dva[2]) && 5376 same_metaslab(zcb->zcb_spa, 5377 DVA_GET_VDEV(&bp->blk_dva[0]), 5378 DVA_GET_OFFSET(&bp->blk_dva[0]), 5379 DVA_GET_OFFSET(&bp->blk_dva[2]))) 5380 zb->zb_ditto_same_ms++; 5381 else if (DVA_GET_VDEV(&bp->blk_dva[1]) == 5382 DVA_GET_VDEV(&bp->blk_dva[2]) && 5383 same_metaslab(zcb->zcb_spa, 5384 DVA_GET_VDEV(&bp->blk_dva[1]), 5385 DVA_GET_OFFSET(&bp->blk_dva[1]), 5386 DVA_GET_OFFSET(&bp->blk_dva[2]))) 5387 zb->zb_ditto_same_ms++; 5388 } 5389 break; 5390 } 5391 } 5392 5393 spa_config_exit(zcb->zcb_spa, SCL_CONFIG, FTAG); 5394 5395 if (BP_IS_EMBEDDED(bp)) { 5396 zcb->zcb_embedded_blocks[BPE_GET_ETYPE(bp)]++; 5397 zcb->zcb_embedded_histogram[BPE_GET_ETYPE(bp)] 5398 [BPE_GET_PSIZE(bp)]++; 5399 return; 5400 } 5401 /* 5402 * The binning histogram bins by powers of two up to 5403 * SPA_MAXBLOCKSIZE rather than creating bins for 5404 * every possible blocksize found in the pool. 5405 */ 5406 int bin = highbit64(BP_GET_PSIZE(bp)) - 1; 5407 5408 zcb->zcb_psize_count[bin]++; 5409 zcb->zcb_psize_len[bin] += BP_GET_PSIZE(bp); 5410 zcb->zcb_psize_total += BP_GET_PSIZE(bp); 5411 5412 bin = highbit64(BP_GET_LSIZE(bp)) - 1; 5413 5414 zcb->zcb_lsize_count[bin]++; 5415 zcb->zcb_lsize_len[bin] += BP_GET_LSIZE(bp); 5416 zcb->zcb_lsize_total += BP_GET_LSIZE(bp); 5417 5418 bin = highbit64(BP_GET_ASIZE(bp)) - 1; 5419 5420 zcb->zcb_asize_count[bin]++; 5421 zcb->zcb_asize_len[bin] += BP_GET_ASIZE(bp); 5422 zcb->zcb_asize_total += BP_GET_ASIZE(bp); 5423 5424 if (dump_opt['L']) 5425 return; 5426 5427 if (BP_GET_DEDUP(bp)) { 5428 ddt_t *ddt; 5429 ddt_entry_t *dde; 5430 5431 ddt = ddt_select(zcb->zcb_spa, bp); 5432 ddt_enter(ddt); 5433 dde = ddt_lookup(ddt, bp, B_FALSE); 5434 5435 if (dde == NULL) { 5436 refcnt = 0; 5437 } else { 5438 ddt_phys_t *ddp = ddt_phys_select(dde, bp); 5439 ddt_phys_decref(ddp); 5440 refcnt = ddp->ddp_refcnt; 5441 if (ddt_phys_total_refcnt(dde) == 0) 5442 ddt_remove(ddt, dde); 5443 } 5444 ddt_exit(ddt); 5445 } 5446 5447 VERIFY3U(zio_wait(zio_claim(NULL, zcb->zcb_spa, 5448 refcnt ? 0 : spa_min_claim_txg(zcb->zcb_spa), 5449 bp, NULL, NULL, ZIO_FLAG_CANFAIL)), ==, 0); 5450 } 5451 5452 static void 5453 zdb_blkptr_done(zio_t *zio) 5454 { 5455 spa_t *spa = zio->io_spa; 5456 blkptr_t *bp = zio->io_bp; 5457 int ioerr = zio->io_error; 5458 zdb_cb_t *zcb = zio->io_private; 5459 zbookmark_phys_t *zb = &zio->io_bookmark; 5460 5461 mutex_enter(&spa->spa_scrub_lock); 5462 spa->spa_load_verify_bytes -= BP_GET_PSIZE(bp); 5463 cv_broadcast(&spa->spa_scrub_io_cv); 5464 5465 if (ioerr && !(zio->io_flags & ZIO_FLAG_SPECULATIVE)) { 5466 char blkbuf[BP_SPRINTF_LEN]; 5467 5468 zcb->zcb_haderrors = 1; 5469 zcb->zcb_errors[ioerr]++; 5470 5471 if (dump_opt['b'] >= 2) 5472 snprintf_blkptr(blkbuf, sizeof (blkbuf), bp); 5473 else 5474 blkbuf[0] = '\0'; 5475 5476 (void) printf("zdb_blkptr_cb: " 5477 "Got error %d reading " 5478 "<%llu, %llu, %lld, %llx> %s -- skipping\n", 5479 ioerr, 5480 (u_longlong_t)zb->zb_objset, 5481 (u_longlong_t)zb->zb_object, 5482 (u_longlong_t)zb->zb_level, 5483 (u_longlong_t)zb->zb_blkid, 5484 blkbuf); 5485 } 5486 mutex_exit(&spa->spa_scrub_lock); 5487 5488 abd_free(zio->io_abd); 5489 } 5490 5491 static int 5492 zdb_blkptr_cb(spa_t *spa, zilog_t *zilog, const blkptr_t *bp, 5493 const zbookmark_phys_t *zb, const dnode_phys_t *dnp, void *arg) 5494 { 5495 zdb_cb_t *zcb = arg; 5496 dmu_object_type_t type; 5497 boolean_t is_metadata; 5498 5499 if (zb->zb_level == ZB_DNODE_LEVEL) 5500 return (0); 5501 5502 if (dump_opt['b'] >= 5 && bp->blk_birth > 0) { 5503 char blkbuf[BP_SPRINTF_LEN]; 5504 snprintf_blkptr(blkbuf, sizeof (blkbuf), bp); 5505 (void) printf("objset %llu object %llu " 5506 "level %lld offset 0x%llx %s\n", 5507 (u_longlong_t)zb->zb_objset, 5508 (u_longlong_t)zb->zb_object, 5509 (longlong_t)zb->zb_level, 5510 (u_longlong_t)blkid2offset(dnp, bp, zb), 5511 blkbuf); 5512 } 5513 5514 if (BP_IS_HOLE(bp) || BP_IS_REDACTED(bp)) 5515 return (0); 5516 5517 type = BP_GET_TYPE(bp); 5518 5519 zdb_count_block(zcb, zilog, bp, 5520 (type & DMU_OT_NEWTYPE) ? ZDB_OT_OTHER : type); 5521 5522 is_metadata = (BP_GET_LEVEL(bp) != 0 || DMU_OT_IS_METADATA(type)); 5523 5524 if (!BP_IS_EMBEDDED(bp) && 5525 (dump_opt['c'] > 1 || (dump_opt['c'] && is_metadata))) { 5526 size_t size = BP_GET_PSIZE(bp); 5527 abd_t *abd = abd_alloc(size, B_FALSE); 5528 int flags = ZIO_FLAG_CANFAIL | ZIO_FLAG_SCRUB | ZIO_FLAG_RAW; 5529 5530 /* If it's an intent log block, failure is expected. */ 5531 if (zb->zb_level == ZB_ZIL_LEVEL) 5532 flags |= ZIO_FLAG_SPECULATIVE; 5533 5534 mutex_enter(&spa->spa_scrub_lock); 5535 while (spa->spa_load_verify_bytes > max_inflight_bytes) 5536 cv_wait(&spa->spa_scrub_io_cv, &spa->spa_scrub_lock); 5537 spa->spa_load_verify_bytes += size; 5538 mutex_exit(&spa->spa_scrub_lock); 5539 5540 zio_nowait(zio_read(NULL, spa, bp, abd, size, 5541 zdb_blkptr_done, zcb, ZIO_PRIORITY_ASYNC_READ, flags, zb)); 5542 } 5543 5544 zcb->zcb_readfails = 0; 5545 5546 /* only call gethrtime() every 100 blocks */ 5547 static int iters; 5548 if (++iters > 100) 5549 iters = 0; 5550 else 5551 return (0); 5552 5553 if (dump_opt['b'] < 5 && gethrtime() > zcb->zcb_lastprint + NANOSEC) { 5554 uint64_t now = gethrtime(); 5555 char buf[10]; 5556 uint64_t bytes = zcb->zcb_type[ZB_TOTAL][ZDB_OT_TOTAL].zb_asize; 5557 uint64_t kb_per_sec = 5558 1 + bytes / (1 + ((now - zcb->zcb_start) / 1000 / 1000)); 5559 uint64_t sec_remaining = 5560 (zcb->zcb_totalasize - bytes) / 1024 / kb_per_sec; 5561 5562 /* make sure nicenum has enough space */ 5563 _Static_assert(sizeof (buf) >= NN_NUMBUF_SZ, "buf truncated"); 5564 5565 zfs_nicebytes(bytes, buf, sizeof (buf)); 5566 (void) fprintf(stderr, 5567 "\r%5s completed (%4"PRIu64"MB/s) " 5568 "estimated time remaining: " 5569 "%"PRIu64"hr %02"PRIu64"min %02"PRIu64"sec ", 5570 buf, kb_per_sec / 1024, 5571 sec_remaining / 60 / 60, 5572 sec_remaining / 60 % 60, 5573 sec_remaining % 60); 5574 5575 zcb->zcb_lastprint = now; 5576 } 5577 5578 return (0); 5579 } 5580 5581 static void 5582 zdb_leak(void *arg, uint64_t start, uint64_t size) 5583 { 5584 vdev_t *vd = arg; 5585 5586 (void) printf("leaked space: vdev %llu, offset 0x%llx, size %llu\n", 5587 (u_longlong_t)vd->vdev_id, (u_longlong_t)start, (u_longlong_t)size); 5588 } 5589 5590 static metaslab_ops_t zdb_metaslab_ops = { 5591 NULL /* alloc */ 5592 }; 5593 5594 static int 5595 load_unflushed_svr_segs_cb(spa_t *spa, space_map_entry_t *sme, 5596 uint64_t txg, void *arg) 5597 { 5598 spa_vdev_removal_t *svr = arg; 5599 5600 uint64_t offset = sme->sme_offset; 5601 uint64_t size = sme->sme_run; 5602 5603 /* skip vdevs we don't care about */ 5604 if (sme->sme_vdev != svr->svr_vdev_id) 5605 return (0); 5606 5607 vdev_t *vd = vdev_lookup_top(spa, sme->sme_vdev); 5608 metaslab_t *ms = vd->vdev_ms[offset >> vd->vdev_ms_shift]; 5609 ASSERT(sme->sme_type == SM_ALLOC || sme->sme_type == SM_FREE); 5610 5611 if (txg < metaslab_unflushed_txg(ms)) 5612 return (0); 5613 5614 if (sme->sme_type == SM_ALLOC) 5615 range_tree_add(svr->svr_allocd_segs, offset, size); 5616 else 5617 range_tree_remove(svr->svr_allocd_segs, offset, size); 5618 5619 return (0); 5620 } 5621 5622 static void 5623 claim_segment_impl_cb(uint64_t inner_offset, vdev_t *vd, uint64_t offset, 5624 uint64_t size, void *arg) 5625 { 5626 (void) inner_offset, (void) arg; 5627 5628 /* 5629 * This callback was called through a remap from 5630 * a device being removed. Therefore, the vdev that 5631 * this callback is applied to is a concrete 5632 * vdev. 5633 */ 5634 ASSERT(vdev_is_concrete(vd)); 5635 5636 VERIFY0(metaslab_claim_impl(vd, offset, size, 5637 spa_min_claim_txg(vd->vdev_spa))); 5638 } 5639 5640 static void 5641 claim_segment_cb(void *arg, uint64_t offset, uint64_t size) 5642 { 5643 vdev_t *vd = arg; 5644 5645 vdev_indirect_ops.vdev_op_remap(vd, offset, size, 5646 claim_segment_impl_cb, NULL); 5647 } 5648 5649 /* 5650 * After accounting for all allocated blocks that are directly referenced, 5651 * we might have missed a reference to a block from a partially complete 5652 * (and thus unused) indirect mapping object. We perform a secondary pass 5653 * through the metaslabs we have already mapped and claim the destination 5654 * blocks. 5655 */ 5656 static void 5657 zdb_claim_removing(spa_t *spa, zdb_cb_t *zcb) 5658 { 5659 if (dump_opt['L']) 5660 return; 5661 5662 if (spa->spa_vdev_removal == NULL) 5663 return; 5664 5665 spa_config_enter(spa, SCL_CONFIG, FTAG, RW_READER); 5666 5667 spa_vdev_removal_t *svr = spa->spa_vdev_removal; 5668 vdev_t *vd = vdev_lookup_top(spa, svr->svr_vdev_id); 5669 vdev_indirect_mapping_t *vim = vd->vdev_indirect_mapping; 5670 5671 ASSERT0(range_tree_space(svr->svr_allocd_segs)); 5672 5673 range_tree_t *allocs = range_tree_create(NULL, RANGE_SEG64, NULL, 0, 0); 5674 for (uint64_t msi = 0; msi < vd->vdev_ms_count; msi++) { 5675 metaslab_t *msp = vd->vdev_ms[msi]; 5676 5677 ASSERT0(range_tree_space(allocs)); 5678 if (msp->ms_sm != NULL) 5679 VERIFY0(space_map_load(msp->ms_sm, allocs, SM_ALLOC)); 5680 range_tree_vacate(allocs, range_tree_add, svr->svr_allocd_segs); 5681 } 5682 range_tree_destroy(allocs); 5683 5684 iterate_through_spacemap_logs(spa, load_unflushed_svr_segs_cb, svr); 5685 5686 /* 5687 * Clear everything past what has been synced, 5688 * because we have not allocated mappings for 5689 * it yet. 5690 */ 5691 range_tree_clear(svr->svr_allocd_segs, 5692 vdev_indirect_mapping_max_offset(vim), 5693 vd->vdev_asize - vdev_indirect_mapping_max_offset(vim)); 5694 5695 zcb->zcb_removing_size += range_tree_space(svr->svr_allocd_segs); 5696 range_tree_vacate(svr->svr_allocd_segs, claim_segment_cb, vd); 5697 5698 spa_config_exit(spa, SCL_CONFIG, FTAG); 5699 } 5700 5701 static int 5702 increment_indirect_mapping_cb(void *arg, const blkptr_t *bp, boolean_t bp_freed, 5703 dmu_tx_t *tx) 5704 { 5705 (void) tx; 5706 zdb_cb_t *zcb = arg; 5707 spa_t *spa = zcb->zcb_spa; 5708 vdev_t *vd; 5709 const dva_t *dva = &bp->blk_dva[0]; 5710 5711 ASSERT(!bp_freed); 5712 ASSERT(!dump_opt['L']); 5713 ASSERT3U(BP_GET_NDVAS(bp), ==, 1); 5714 5715 spa_config_enter(spa, SCL_VDEV, FTAG, RW_READER); 5716 vd = vdev_lookup_top(zcb->zcb_spa, DVA_GET_VDEV(dva)); 5717 ASSERT3P(vd, !=, NULL); 5718 spa_config_exit(spa, SCL_VDEV, FTAG); 5719 5720 ASSERT(vd->vdev_indirect_config.vic_mapping_object != 0); 5721 ASSERT3P(zcb->zcb_vd_obsolete_counts[vd->vdev_id], !=, NULL); 5722 5723 vdev_indirect_mapping_increment_obsolete_count( 5724 vd->vdev_indirect_mapping, 5725 DVA_GET_OFFSET(dva), DVA_GET_ASIZE(dva), 5726 zcb->zcb_vd_obsolete_counts[vd->vdev_id]); 5727 5728 return (0); 5729 } 5730 5731 static uint32_t * 5732 zdb_load_obsolete_counts(vdev_t *vd) 5733 { 5734 vdev_indirect_mapping_t *vim = vd->vdev_indirect_mapping; 5735 spa_t *spa = vd->vdev_spa; 5736 spa_condensing_indirect_phys_t *scip = 5737 &spa->spa_condensing_indirect_phys; 5738 uint64_t obsolete_sm_object; 5739 uint32_t *counts; 5740 5741 VERIFY0(vdev_obsolete_sm_object(vd, &obsolete_sm_object)); 5742 EQUIV(obsolete_sm_object != 0, vd->vdev_obsolete_sm != NULL); 5743 counts = vdev_indirect_mapping_load_obsolete_counts(vim); 5744 if (vd->vdev_obsolete_sm != NULL) { 5745 vdev_indirect_mapping_load_obsolete_spacemap(vim, counts, 5746 vd->vdev_obsolete_sm); 5747 } 5748 if (scip->scip_vdev == vd->vdev_id && 5749 scip->scip_prev_obsolete_sm_object != 0) { 5750 space_map_t *prev_obsolete_sm = NULL; 5751 VERIFY0(space_map_open(&prev_obsolete_sm, spa->spa_meta_objset, 5752 scip->scip_prev_obsolete_sm_object, 0, vd->vdev_asize, 0)); 5753 vdev_indirect_mapping_load_obsolete_spacemap(vim, counts, 5754 prev_obsolete_sm); 5755 space_map_close(prev_obsolete_sm); 5756 } 5757 return (counts); 5758 } 5759 5760 static void 5761 zdb_ddt_leak_init(spa_t *spa, zdb_cb_t *zcb) 5762 { 5763 ddt_bookmark_t ddb = {0}; 5764 ddt_entry_t dde; 5765 int error; 5766 int p; 5767 5768 ASSERT(!dump_opt['L']); 5769 5770 while ((error = ddt_walk(spa, &ddb, &dde)) == 0) { 5771 blkptr_t blk; 5772 ddt_phys_t *ddp = dde.dde_phys; 5773 5774 if (ddb.ddb_class == DDT_CLASS_UNIQUE) 5775 return; 5776 5777 ASSERT(ddt_phys_total_refcnt(&dde) > 1); 5778 5779 for (p = 0; p < DDT_PHYS_TYPES; p++, ddp++) { 5780 if (ddp->ddp_phys_birth == 0) 5781 continue; 5782 ddt_bp_create(ddb.ddb_checksum, 5783 &dde.dde_key, ddp, &blk); 5784 if (p == DDT_PHYS_DITTO) { 5785 zdb_count_block(zcb, NULL, &blk, ZDB_OT_DITTO); 5786 } else { 5787 zcb->zcb_dedup_asize += 5788 BP_GET_ASIZE(&blk) * (ddp->ddp_refcnt - 1); 5789 zcb->zcb_dedup_blocks++; 5790 } 5791 } 5792 ddt_t *ddt = spa->spa_ddt[ddb.ddb_checksum]; 5793 ddt_enter(ddt); 5794 VERIFY(ddt_lookup(ddt, &blk, B_TRUE) != NULL); 5795 ddt_exit(ddt); 5796 } 5797 5798 ASSERT(error == ENOENT); 5799 } 5800 5801 typedef struct checkpoint_sm_exclude_entry_arg { 5802 vdev_t *cseea_vd; 5803 uint64_t cseea_checkpoint_size; 5804 } checkpoint_sm_exclude_entry_arg_t; 5805 5806 static int 5807 checkpoint_sm_exclude_entry_cb(space_map_entry_t *sme, void *arg) 5808 { 5809 checkpoint_sm_exclude_entry_arg_t *cseea = arg; 5810 vdev_t *vd = cseea->cseea_vd; 5811 metaslab_t *ms = vd->vdev_ms[sme->sme_offset >> vd->vdev_ms_shift]; 5812 uint64_t end = sme->sme_offset + sme->sme_run; 5813 5814 ASSERT(sme->sme_type == SM_FREE); 5815 5816 /* 5817 * Since the vdev_checkpoint_sm exists in the vdev level 5818 * and the ms_sm space maps exist in the metaslab level, 5819 * an entry in the checkpoint space map could theoretically 5820 * cross the boundaries of the metaslab that it belongs. 5821 * 5822 * In reality, because of the way that we populate and 5823 * manipulate the checkpoint's space maps currently, 5824 * there shouldn't be any entries that cross metaslabs. 5825 * Hence the assertion below. 5826 * 5827 * That said, there is no fundamental requirement that 5828 * the checkpoint's space map entries should not cross 5829 * metaslab boundaries. So if needed we could add code 5830 * that handles metaslab-crossing segments in the future. 5831 */ 5832 VERIFY3U(sme->sme_offset, >=, ms->ms_start); 5833 VERIFY3U(end, <=, ms->ms_start + ms->ms_size); 5834 5835 /* 5836 * By removing the entry from the allocated segments we 5837 * also verify that the entry is there to begin with. 5838 */ 5839 mutex_enter(&ms->ms_lock); 5840 range_tree_remove(ms->ms_allocatable, sme->sme_offset, sme->sme_run); 5841 mutex_exit(&ms->ms_lock); 5842 5843 cseea->cseea_checkpoint_size += sme->sme_run; 5844 return (0); 5845 } 5846 5847 static void 5848 zdb_leak_init_vdev_exclude_checkpoint(vdev_t *vd, zdb_cb_t *zcb) 5849 { 5850 spa_t *spa = vd->vdev_spa; 5851 space_map_t *checkpoint_sm = NULL; 5852 uint64_t checkpoint_sm_obj; 5853 5854 /* 5855 * If there is no vdev_top_zap, we are in a pool whose 5856 * version predates the pool checkpoint feature. 5857 */ 5858 if (vd->vdev_top_zap == 0) 5859 return; 5860 5861 /* 5862 * If there is no reference of the vdev_checkpoint_sm in 5863 * the vdev_top_zap, then one of the following scenarios 5864 * is true: 5865 * 5866 * 1] There is no checkpoint 5867 * 2] There is a checkpoint, but no checkpointed blocks 5868 * have been freed yet 5869 * 3] The current vdev is indirect 5870 * 5871 * In these cases we return immediately. 5872 */ 5873 if (zap_contains(spa_meta_objset(spa), vd->vdev_top_zap, 5874 VDEV_TOP_ZAP_POOL_CHECKPOINT_SM) != 0) 5875 return; 5876 5877 VERIFY0(zap_lookup(spa_meta_objset(spa), vd->vdev_top_zap, 5878 VDEV_TOP_ZAP_POOL_CHECKPOINT_SM, sizeof (uint64_t), 1, 5879 &checkpoint_sm_obj)); 5880 5881 checkpoint_sm_exclude_entry_arg_t cseea; 5882 cseea.cseea_vd = vd; 5883 cseea.cseea_checkpoint_size = 0; 5884 5885 VERIFY0(space_map_open(&checkpoint_sm, spa_meta_objset(spa), 5886 checkpoint_sm_obj, 0, vd->vdev_asize, vd->vdev_ashift)); 5887 5888 VERIFY0(space_map_iterate(checkpoint_sm, 5889 space_map_length(checkpoint_sm), 5890 checkpoint_sm_exclude_entry_cb, &cseea)); 5891 space_map_close(checkpoint_sm); 5892 5893 zcb->zcb_checkpoint_size += cseea.cseea_checkpoint_size; 5894 } 5895 5896 static void 5897 zdb_leak_init_exclude_checkpoint(spa_t *spa, zdb_cb_t *zcb) 5898 { 5899 ASSERT(!dump_opt['L']); 5900 5901 vdev_t *rvd = spa->spa_root_vdev; 5902 for (uint64_t c = 0; c < rvd->vdev_children; c++) { 5903 ASSERT3U(c, ==, rvd->vdev_child[c]->vdev_id); 5904 zdb_leak_init_vdev_exclude_checkpoint(rvd->vdev_child[c], zcb); 5905 } 5906 } 5907 5908 static int 5909 count_unflushed_space_cb(spa_t *spa, space_map_entry_t *sme, 5910 uint64_t txg, void *arg) 5911 { 5912 int64_t *ualloc_space = arg; 5913 5914 uint64_t offset = sme->sme_offset; 5915 uint64_t vdev_id = sme->sme_vdev; 5916 5917 vdev_t *vd = vdev_lookup_top(spa, vdev_id); 5918 if (!vdev_is_concrete(vd)) 5919 return (0); 5920 5921 metaslab_t *ms = vd->vdev_ms[offset >> vd->vdev_ms_shift]; 5922 ASSERT(sme->sme_type == SM_ALLOC || sme->sme_type == SM_FREE); 5923 5924 if (txg < metaslab_unflushed_txg(ms)) 5925 return (0); 5926 5927 if (sme->sme_type == SM_ALLOC) 5928 *ualloc_space += sme->sme_run; 5929 else 5930 *ualloc_space -= sme->sme_run; 5931 5932 return (0); 5933 } 5934 5935 static int64_t 5936 get_unflushed_alloc_space(spa_t *spa) 5937 { 5938 if (dump_opt['L']) 5939 return (0); 5940 5941 int64_t ualloc_space = 0; 5942 iterate_through_spacemap_logs(spa, count_unflushed_space_cb, 5943 &ualloc_space); 5944 return (ualloc_space); 5945 } 5946 5947 static int 5948 load_unflushed_cb(spa_t *spa, space_map_entry_t *sme, uint64_t txg, void *arg) 5949 { 5950 maptype_t *uic_maptype = arg; 5951 5952 uint64_t offset = sme->sme_offset; 5953 uint64_t size = sme->sme_run; 5954 uint64_t vdev_id = sme->sme_vdev; 5955 5956 vdev_t *vd = vdev_lookup_top(spa, vdev_id); 5957 5958 /* skip indirect vdevs */ 5959 if (!vdev_is_concrete(vd)) 5960 return (0); 5961 5962 metaslab_t *ms = vd->vdev_ms[offset >> vd->vdev_ms_shift]; 5963 5964 ASSERT(sme->sme_type == SM_ALLOC || sme->sme_type == SM_FREE); 5965 ASSERT(*uic_maptype == SM_ALLOC || *uic_maptype == SM_FREE); 5966 5967 if (txg < metaslab_unflushed_txg(ms)) 5968 return (0); 5969 5970 if (*uic_maptype == sme->sme_type) 5971 range_tree_add(ms->ms_allocatable, offset, size); 5972 else 5973 range_tree_remove(ms->ms_allocatable, offset, size); 5974 5975 return (0); 5976 } 5977 5978 static void 5979 load_unflushed_to_ms_allocatables(spa_t *spa, maptype_t maptype) 5980 { 5981 iterate_through_spacemap_logs(spa, load_unflushed_cb, &maptype); 5982 } 5983 5984 static void 5985 load_concrete_ms_allocatable_trees(spa_t *spa, maptype_t maptype) 5986 { 5987 vdev_t *rvd = spa->spa_root_vdev; 5988 for (uint64_t i = 0; i < rvd->vdev_children; i++) { 5989 vdev_t *vd = rvd->vdev_child[i]; 5990 5991 ASSERT3U(i, ==, vd->vdev_id); 5992 5993 if (vd->vdev_ops == &vdev_indirect_ops) 5994 continue; 5995 5996 for (uint64_t m = 0; m < vd->vdev_ms_count; m++) { 5997 metaslab_t *msp = vd->vdev_ms[m]; 5998 5999 (void) fprintf(stderr, 6000 "\rloading concrete vdev %llu, " 6001 "metaslab %llu of %llu ...", 6002 (longlong_t)vd->vdev_id, 6003 (longlong_t)msp->ms_id, 6004 (longlong_t)vd->vdev_ms_count); 6005 6006 mutex_enter(&msp->ms_lock); 6007 range_tree_vacate(msp->ms_allocatable, NULL, NULL); 6008 6009 /* 6010 * We don't want to spend the CPU manipulating the 6011 * size-ordered tree, so clear the range_tree ops. 6012 */ 6013 msp->ms_allocatable->rt_ops = NULL; 6014 6015 if (msp->ms_sm != NULL) { 6016 VERIFY0(space_map_load(msp->ms_sm, 6017 msp->ms_allocatable, maptype)); 6018 } 6019 if (!msp->ms_loaded) 6020 msp->ms_loaded = B_TRUE; 6021 mutex_exit(&msp->ms_lock); 6022 } 6023 } 6024 6025 load_unflushed_to_ms_allocatables(spa, maptype); 6026 } 6027 6028 /* 6029 * vm_idxp is an in-out parameter which (for indirect vdevs) is the 6030 * index in vim_entries that has the first entry in this metaslab. 6031 * On return, it will be set to the first entry after this metaslab. 6032 */ 6033 static void 6034 load_indirect_ms_allocatable_tree(vdev_t *vd, metaslab_t *msp, 6035 uint64_t *vim_idxp) 6036 { 6037 vdev_indirect_mapping_t *vim = vd->vdev_indirect_mapping; 6038 6039 mutex_enter(&msp->ms_lock); 6040 range_tree_vacate(msp->ms_allocatable, NULL, NULL); 6041 6042 /* 6043 * We don't want to spend the CPU manipulating the 6044 * size-ordered tree, so clear the range_tree ops. 6045 */ 6046 msp->ms_allocatable->rt_ops = NULL; 6047 6048 for (; *vim_idxp < vdev_indirect_mapping_num_entries(vim); 6049 (*vim_idxp)++) { 6050 vdev_indirect_mapping_entry_phys_t *vimep = 6051 &vim->vim_entries[*vim_idxp]; 6052 uint64_t ent_offset = DVA_MAPPING_GET_SRC_OFFSET(vimep); 6053 uint64_t ent_len = DVA_GET_ASIZE(&vimep->vimep_dst); 6054 ASSERT3U(ent_offset, >=, msp->ms_start); 6055 if (ent_offset >= msp->ms_start + msp->ms_size) 6056 break; 6057 6058 /* 6059 * Mappings do not cross metaslab boundaries, 6060 * because we create them by walking the metaslabs. 6061 */ 6062 ASSERT3U(ent_offset + ent_len, <=, 6063 msp->ms_start + msp->ms_size); 6064 range_tree_add(msp->ms_allocatable, ent_offset, ent_len); 6065 } 6066 6067 if (!msp->ms_loaded) 6068 msp->ms_loaded = B_TRUE; 6069 mutex_exit(&msp->ms_lock); 6070 } 6071 6072 static void 6073 zdb_leak_init_prepare_indirect_vdevs(spa_t *spa, zdb_cb_t *zcb) 6074 { 6075 ASSERT(!dump_opt['L']); 6076 6077 vdev_t *rvd = spa->spa_root_vdev; 6078 for (uint64_t c = 0; c < rvd->vdev_children; c++) { 6079 vdev_t *vd = rvd->vdev_child[c]; 6080 6081 ASSERT3U(c, ==, vd->vdev_id); 6082 6083 if (vd->vdev_ops != &vdev_indirect_ops) 6084 continue; 6085 6086 /* 6087 * Note: we don't check for mapping leaks on 6088 * removing vdevs because their ms_allocatable's 6089 * are used to look for leaks in allocated space. 6090 */ 6091 zcb->zcb_vd_obsolete_counts[c] = zdb_load_obsolete_counts(vd); 6092 6093 /* 6094 * Normally, indirect vdevs don't have any 6095 * metaslabs. We want to set them up for 6096 * zio_claim(). 6097 */ 6098 vdev_metaslab_group_create(vd); 6099 VERIFY0(vdev_metaslab_init(vd, 0)); 6100 6101 vdev_indirect_mapping_t *vim __maybe_unused = 6102 vd->vdev_indirect_mapping; 6103 uint64_t vim_idx = 0; 6104 for (uint64_t m = 0; m < vd->vdev_ms_count; m++) { 6105 6106 (void) fprintf(stderr, 6107 "\rloading indirect vdev %llu, " 6108 "metaslab %llu of %llu ...", 6109 (longlong_t)vd->vdev_id, 6110 (longlong_t)vd->vdev_ms[m]->ms_id, 6111 (longlong_t)vd->vdev_ms_count); 6112 6113 load_indirect_ms_allocatable_tree(vd, vd->vdev_ms[m], 6114 &vim_idx); 6115 } 6116 ASSERT3U(vim_idx, ==, vdev_indirect_mapping_num_entries(vim)); 6117 } 6118 } 6119 6120 static void 6121 zdb_leak_init(spa_t *spa, zdb_cb_t *zcb) 6122 { 6123 zcb->zcb_spa = spa; 6124 6125 if (dump_opt['L']) 6126 return; 6127 6128 dsl_pool_t *dp = spa->spa_dsl_pool; 6129 vdev_t *rvd = spa->spa_root_vdev; 6130 6131 /* 6132 * We are going to be changing the meaning of the metaslab's 6133 * ms_allocatable. Ensure that the allocator doesn't try to 6134 * use the tree. 6135 */ 6136 spa->spa_normal_class->mc_ops = &zdb_metaslab_ops; 6137 spa->spa_log_class->mc_ops = &zdb_metaslab_ops; 6138 spa->spa_embedded_log_class->mc_ops = &zdb_metaslab_ops; 6139 6140 zcb->zcb_vd_obsolete_counts = 6141 umem_zalloc(rvd->vdev_children * sizeof (uint32_t *), 6142 UMEM_NOFAIL); 6143 6144 /* 6145 * For leak detection, we overload the ms_allocatable trees 6146 * to contain allocated segments instead of free segments. 6147 * As a result, we can't use the normal metaslab_load/unload 6148 * interfaces. 6149 */ 6150 zdb_leak_init_prepare_indirect_vdevs(spa, zcb); 6151 load_concrete_ms_allocatable_trees(spa, SM_ALLOC); 6152 6153 /* 6154 * On load_concrete_ms_allocatable_trees() we loaded all the 6155 * allocated entries from the ms_sm to the ms_allocatable for 6156 * each metaslab. If the pool has a checkpoint or is in the 6157 * middle of discarding a checkpoint, some of these blocks 6158 * may have been freed but their ms_sm may not have been 6159 * updated because they are referenced by the checkpoint. In 6160 * order to avoid false-positives during leak-detection, we 6161 * go through the vdev's checkpoint space map and exclude all 6162 * its entries from their relevant ms_allocatable. 6163 * 6164 * We also aggregate the space held by the checkpoint and add 6165 * it to zcb_checkpoint_size. 6166 * 6167 * Note that at this point we are also verifying that all the 6168 * entries on the checkpoint_sm are marked as allocated in 6169 * the ms_sm of their relevant metaslab. 6170 * [see comment in checkpoint_sm_exclude_entry_cb()] 6171 */ 6172 zdb_leak_init_exclude_checkpoint(spa, zcb); 6173 ASSERT3U(zcb->zcb_checkpoint_size, ==, spa_get_checkpoint_space(spa)); 6174 6175 /* for cleaner progress output */ 6176 (void) fprintf(stderr, "\n"); 6177 6178 if (bpobj_is_open(&dp->dp_obsolete_bpobj)) { 6179 ASSERT(spa_feature_is_enabled(spa, 6180 SPA_FEATURE_DEVICE_REMOVAL)); 6181 (void) bpobj_iterate_nofree(&dp->dp_obsolete_bpobj, 6182 increment_indirect_mapping_cb, zcb, NULL); 6183 } 6184 6185 spa_config_enter(spa, SCL_CONFIG, FTAG, RW_READER); 6186 zdb_ddt_leak_init(spa, zcb); 6187 spa_config_exit(spa, SCL_CONFIG, FTAG); 6188 } 6189 6190 static boolean_t 6191 zdb_check_for_obsolete_leaks(vdev_t *vd, zdb_cb_t *zcb) 6192 { 6193 boolean_t leaks = B_FALSE; 6194 vdev_indirect_mapping_t *vim = vd->vdev_indirect_mapping; 6195 uint64_t total_leaked = 0; 6196 boolean_t are_precise = B_FALSE; 6197 6198 ASSERT(vim != NULL); 6199 6200 for (uint64_t i = 0; i < vdev_indirect_mapping_num_entries(vim); i++) { 6201 vdev_indirect_mapping_entry_phys_t *vimep = 6202 &vim->vim_entries[i]; 6203 uint64_t obsolete_bytes = 0; 6204 uint64_t offset = DVA_MAPPING_GET_SRC_OFFSET(vimep); 6205 metaslab_t *msp = vd->vdev_ms[offset >> vd->vdev_ms_shift]; 6206 6207 /* 6208 * This is not very efficient but it's easy to 6209 * verify correctness. 6210 */ 6211 for (uint64_t inner_offset = 0; 6212 inner_offset < DVA_GET_ASIZE(&vimep->vimep_dst); 6213 inner_offset += 1ULL << vd->vdev_ashift) { 6214 if (range_tree_contains(msp->ms_allocatable, 6215 offset + inner_offset, 1ULL << vd->vdev_ashift)) { 6216 obsolete_bytes += 1ULL << vd->vdev_ashift; 6217 } 6218 } 6219 6220 int64_t bytes_leaked = obsolete_bytes - 6221 zcb->zcb_vd_obsolete_counts[vd->vdev_id][i]; 6222 ASSERT3U(DVA_GET_ASIZE(&vimep->vimep_dst), >=, 6223 zcb->zcb_vd_obsolete_counts[vd->vdev_id][i]); 6224 6225 VERIFY0(vdev_obsolete_counts_are_precise(vd, &are_precise)); 6226 if (bytes_leaked != 0 && (are_precise || dump_opt['d'] >= 5)) { 6227 (void) printf("obsolete indirect mapping count " 6228 "mismatch on %llu:%llx:%llx : %llx bytes leaked\n", 6229 (u_longlong_t)vd->vdev_id, 6230 (u_longlong_t)DVA_MAPPING_GET_SRC_OFFSET(vimep), 6231 (u_longlong_t)DVA_GET_ASIZE(&vimep->vimep_dst), 6232 (u_longlong_t)bytes_leaked); 6233 } 6234 total_leaked += ABS(bytes_leaked); 6235 } 6236 6237 VERIFY0(vdev_obsolete_counts_are_precise(vd, &are_precise)); 6238 if (!are_precise && total_leaked > 0) { 6239 int pct_leaked = total_leaked * 100 / 6240 vdev_indirect_mapping_bytes_mapped(vim); 6241 (void) printf("cannot verify obsolete indirect mapping " 6242 "counts of vdev %llu because precise feature was not " 6243 "enabled when it was removed: %d%% (%llx bytes) of mapping" 6244 "unreferenced\n", 6245 (u_longlong_t)vd->vdev_id, pct_leaked, 6246 (u_longlong_t)total_leaked); 6247 } else if (total_leaked > 0) { 6248 (void) printf("obsolete indirect mapping count mismatch " 6249 "for vdev %llu -- %llx total bytes mismatched\n", 6250 (u_longlong_t)vd->vdev_id, 6251 (u_longlong_t)total_leaked); 6252 leaks |= B_TRUE; 6253 } 6254 6255 vdev_indirect_mapping_free_obsolete_counts(vim, 6256 zcb->zcb_vd_obsolete_counts[vd->vdev_id]); 6257 zcb->zcb_vd_obsolete_counts[vd->vdev_id] = NULL; 6258 6259 return (leaks); 6260 } 6261 6262 static boolean_t 6263 zdb_leak_fini(spa_t *spa, zdb_cb_t *zcb) 6264 { 6265 if (dump_opt['L']) 6266 return (B_FALSE); 6267 6268 boolean_t leaks = B_FALSE; 6269 vdev_t *rvd = spa->spa_root_vdev; 6270 for (unsigned c = 0; c < rvd->vdev_children; c++) { 6271 vdev_t *vd = rvd->vdev_child[c]; 6272 6273 if (zcb->zcb_vd_obsolete_counts[c] != NULL) { 6274 leaks |= zdb_check_for_obsolete_leaks(vd, zcb); 6275 } 6276 6277 for (uint64_t m = 0; m < vd->vdev_ms_count; m++) { 6278 metaslab_t *msp = vd->vdev_ms[m]; 6279 ASSERT3P(msp->ms_group, ==, (msp->ms_group->mg_class == 6280 spa_embedded_log_class(spa)) ? 6281 vd->vdev_log_mg : vd->vdev_mg); 6282 6283 /* 6284 * ms_allocatable has been overloaded 6285 * to contain allocated segments. Now that 6286 * we finished traversing all blocks, any 6287 * block that remains in the ms_allocatable 6288 * represents an allocated block that we 6289 * did not claim during the traversal. 6290 * Claimed blocks would have been removed 6291 * from the ms_allocatable. For indirect 6292 * vdevs, space remaining in the tree 6293 * represents parts of the mapping that are 6294 * not referenced, which is not a bug. 6295 */ 6296 if (vd->vdev_ops == &vdev_indirect_ops) { 6297 range_tree_vacate(msp->ms_allocatable, 6298 NULL, NULL); 6299 } else { 6300 range_tree_vacate(msp->ms_allocatable, 6301 zdb_leak, vd); 6302 } 6303 if (msp->ms_loaded) { 6304 msp->ms_loaded = B_FALSE; 6305 } 6306 } 6307 } 6308 6309 umem_free(zcb->zcb_vd_obsolete_counts, 6310 rvd->vdev_children * sizeof (uint32_t *)); 6311 zcb->zcb_vd_obsolete_counts = NULL; 6312 6313 return (leaks); 6314 } 6315 6316 static int 6317 count_block_cb(void *arg, const blkptr_t *bp, dmu_tx_t *tx) 6318 { 6319 (void) tx; 6320 zdb_cb_t *zcb = arg; 6321 6322 if (dump_opt['b'] >= 5) { 6323 char blkbuf[BP_SPRINTF_LEN]; 6324 snprintf_blkptr(blkbuf, sizeof (blkbuf), bp); 6325 (void) printf("[%s] %s\n", 6326 "deferred free", blkbuf); 6327 } 6328 zdb_count_block(zcb, NULL, bp, ZDB_OT_DEFERRED); 6329 return (0); 6330 } 6331 6332 /* 6333 * Iterate over livelists which have been destroyed by the user but 6334 * are still present in the MOS, waiting to be freed 6335 */ 6336 static void 6337 iterate_deleted_livelists(spa_t *spa, ll_iter_t func, void *arg) 6338 { 6339 objset_t *mos = spa->spa_meta_objset; 6340 uint64_t zap_obj; 6341 int err = zap_lookup(mos, DMU_POOL_DIRECTORY_OBJECT, 6342 DMU_POOL_DELETED_CLONES, sizeof (uint64_t), 1, &zap_obj); 6343 if (err == ENOENT) 6344 return; 6345 ASSERT0(err); 6346 6347 zap_cursor_t zc; 6348 zap_attribute_t attr; 6349 dsl_deadlist_t ll; 6350 /* NULL out os prior to dsl_deadlist_open in case it's garbage */ 6351 ll.dl_os = NULL; 6352 for (zap_cursor_init(&zc, mos, zap_obj); 6353 zap_cursor_retrieve(&zc, &attr) == 0; 6354 (void) zap_cursor_advance(&zc)) { 6355 dsl_deadlist_open(&ll, mos, attr.za_first_integer); 6356 func(&ll, arg); 6357 dsl_deadlist_close(&ll); 6358 } 6359 zap_cursor_fini(&zc); 6360 } 6361 6362 static int 6363 bpobj_count_block_cb(void *arg, const blkptr_t *bp, boolean_t bp_freed, 6364 dmu_tx_t *tx) 6365 { 6366 ASSERT(!bp_freed); 6367 return (count_block_cb(arg, bp, tx)); 6368 } 6369 6370 static int 6371 livelist_entry_count_blocks_cb(void *args, dsl_deadlist_entry_t *dle) 6372 { 6373 zdb_cb_t *zbc = args; 6374 bplist_t blks; 6375 bplist_create(&blks); 6376 /* determine which blocks have been alloc'd but not freed */ 6377 VERIFY0(dsl_process_sub_livelist(&dle->dle_bpobj, &blks, NULL, NULL)); 6378 /* count those blocks */ 6379 (void) bplist_iterate(&blks, count_block_cb, zbc, NULL); 6380 bplist_destroy(&blks); 6381 return (0); 6382 } 6383 6384 static void 6385 livelist_count_blocks(dsl_deadlist_t *ll, void *arg) 6386 { 6387 dsl_deadlist_iterate(ll, livelist_entry_count_blocks_cb, arg); 6388 } 6389 6390 /* 6391 * Count the blocks in the livelists that have been destroyed by the user 6392 * but haven't yet been freed. 6393 */ 6394 static void 6395 deleted_livelists_count_blocks(spa_t *spa, zdb_cb_t *zbc) 6396 { 6397 iterate_deleted_livelists(spa, livelist_count_blocks, zbc); 6398 } 6399 6400 static void 6401 dump_livelist_cb(dsl_deadlist_t *ll, void *arg) 6402 { 6403 ASSERT3P(arg, ==, NULL); 6404 global_feature_count[SPA_FEATURE_LIVELIST]++; 6405 dump_blkptr_list(ll, "Deleted Livelist"); 6406 dsl_deadlist_iterate(ll, sublivelist_verify_lightweight, NULL); 6407 } 6408 6409 /* 6410 * Print out, register object references to, and increment feature counts for 6411 * livelists that have been destroyed by the user but haven't yet been freed. 6412 */ 6413 static void 6414 deleted_livelists_dump_mos(spa_t *spa) 6415 { 6416 uint64_t zap_obj; 6417 objset_t *mos = spa->spa_meta_objset; 6418 int err = zap_lookup(mos, DMU_POOL_DIRECTORY_OBJECT, 6419 DMU_POOL_DELETED_CLONES, sizeof (uint64_t), 1, &zap_obj); 6420 if (err == ENOENT) 6421 return; 6422 mos_obj_refd(zap_obj); 6423 iterate_deleted_livelists(spa, dump_livelist_cb, NULL); 6424 } 6425 6426 static int 6427 dump_block_stats(spa_t *spa) 6428 { 6429 zdb_cb_t *zcb; 6430 zdb_blkstats_t *zb, *tzb; 6431 uint64_t norm_alloc, norm_space, total_alloc, total_found; 6432 int flags = TRAVERSE_PRE | TRAVERSE_PREFETCH_METADATA | 6433 TRAVERSE_NO_DECRYPT | TRAVERSE_HARD; 6434 boolean_t leaks = B_FALSE; 6435 int e, c, err; 6436 bp_embedded_type_t i; 6437 6438 zcb = umem_zalloc(sizeof (zdb_cb_t), UMEM_NOFAIL); 6439 6440 (void) printf("\nTraversing all blocks %s%s%s%s%s...\n\n", 6441 (dump_opt['c'] || !dump_opt['L']) ? "to verify " : "", 6442 (dump_opt['c'] == 1) ? "metadata " : "", 6443 dump_opt['c'] ? "checksums " : "", 6444 (dump_opt['c'] && !dump_opt['L']) ? "and verify " : "", 6445 !dump_opt['L'] ? "nothing leaked " : ""); 6446 6447 /* 6448 * When leak detection is enabled we load all space maps as SM_ALLOC 6449 * maps, then traverse the pool claiming each block we discover. If 6450 * the pool is perfectly consistent, the segment trees will be empty 6451 * when we're done. Anything left over is a leak; any block we can't 6452 * claim (because it's not part of any space map) is a double 6453 * allocation, reference to a freed block, or an unclaimed log block. 6454 * 6455 * When leak detection is disabled (-L option) we still traverse the 6456 * pool claiming each block we discover, but we skip opening any space 6457 * maps. 6458 */ 6459 zdb_leak_init(spa, zcb); 6460 6461 /* 6462 * If there's a deferred-free bplist, process that first. 6463 */ 6464 (void) bpobj_iterate_nofree(&spa->spa_deferred_bpobj, 6465 bpobj_count_block_cb, zcb, NULL); 6466 6467 if (spa_version(spa) >= SPA_VERSION_DEADLISTS) { 6468 (void) bpobj_iterate_nofree(&spa->spa_dsl_pool->dp_free_bpobj, 6469 bpobj_count_block_cb, zcb, NULL); 6470 } 6471 6472 zdb_claim_removing(spa, zcb); 6473 6474 if (spa_feature_is_active(spa, SPA_FEATURE_ASYNC_DESTROY)) { 6475 VERIFY3U(0, ==, bptree_iterate(spa->spa_meta_objset, 6476 spa->spa_dsl_pool->dp_bptree_obj, B_FALSE, count_block_cb, 6477 zcb, NULL)); 6478 } 6479 6480 deleted_livelists_count_blocks(spa, zcb); 6481 6482 if (dump_opt['c'] > 1) 6483 flags |= TRAVERSE_PREFETCH_DATA; 6484 6485 zcb->zcb_totalasize = metaslab_class_get_alloc(spa_normal_class(spa)); 6486 zcb->zcb_totalasize += metaslab_class_get_alloc(spa_special_class(spa)); 6487 zcb->zcb_totalasize += metaslab_class_get_alloc(spa_dedup_class(spa)); 6488 zcb->zcb_totalasize += 6489 metaslab_class_get_alloc(spa_embedded_log_class(spa)); 6490 zcb->zcb_start = zcb->zcb_lastprint = gethrtime(); 6491 err = traverse_pool(spa, 0, flags, zdb_blkptr_cb, zcb); 6492 6493 /* 6494 * If we've traversed the data blocks then we need to wait for those 6495 * I/Os to complete. We leverage "The Godfather" zio to wait on 6496 * all async I/Os to complete. 6497 */ 6498 if (dump_opt['c']) { 6499 for (c = 0; c < max_ncpus; c++) { 6500 (void) zio_wait(spa->spa_async_zio_root[c]); 6501 spa->spa_async_zio_root[c] = zio_root(spa, NULL, NULL, 6502 ZIO_FLAG_CANFAIL | ZIO_FLAG_SPECULATIVE | 6503 ZIO_FLAG_GODFATHER); 6504 } 6505 } 6506 ASSERT0(spa->spa_load_verify_bytes); 6507 6508 /* 6509 * Done after zio_wait() since zcb_haderrors is modified in 6510 * zdb_blkptr_done() 6511 */ 6512 zcb->zcb_haderrors |= err; 6513 6514 if (zcb->zcb_haderrors) { 6515 (void) printf("\nError counts:\n\n"); 6516 (void) printf("\t%5s %s\n", "errno", "count"); 6517 for (e = 0; e < 256; e++) { 6518 if (zcb->zcb_errors[e] != 0) { 6519 (void) printf("\t%5d %llu\n", 6520 e, (u_longlong_t)zcb->zcb_errors[e]); 6521 } 6522 } 6523 } 6524 6525 /* 6526 * Report any leaked segments. 6527 */ 6528 leaks |= zdb_leak_fini(spa, zcb); 6529 6530 tzb = &zcb->zcb_type[ZB_TOTAL][ZDB_OT_TOTAL]; 6531 6532 norm_alloc = metaslab_class_get_alloc(spa_normal_class(spa)); 6533 norm_space = metaslab_class_get_space(spa_normal_class(spa)); 6534 6535 total_alloc = norm_alloc + 6536 metaslab_class_get_alloc(spa_log_class(spa)) + 6537 metaslab_class_get_alloc(spa_embedded_log_class(spa)) + 6538 metaslab_class_get_alloc(spa_special_class(spa)) + 6539 metaslab_class_get_alloc(spa_dedup_class(spa)) + 6540 get_unflushed_alloc_space(spa); 6541 total_found = tzb->zb_asize - zcb->zcb_dedup_asize + 6542 zcb->zcb_removing_size + zcb->zcb_checkpoint_size; 6543 6544 if (total_found == total_alloc && !dump_opt['L']) { 6545 (void) printf("\n\tNo leaks (block sum matches space" 6546 " maps exactly)\n"); 6547 } else if (!dump_opt['L']) { 6548 (void) printf("block traversal size %llu != alloc %llu " 6549 "(%s %lld)\n", 6550 (u_longlong_t)total_found, 6551 (u_longlong_t)total_alloc, 6552 (dump_opt['L']) ? "unreachable" : "leaked", 6553 (longlong_t)(total_alloc - total_found)); 6554 leaks = B_TRUE; 6555 } 6556 6557 if (tzb->zb_count == 0) { 6558 umem_free(zcb, sizeof (zdb_cb_t)); 6559 return (2); 6560 } 6561 6562 (void) printf("\n"); 6563 (void) printf("\t%-16s %14llu\n", "bp count:", 6564 (u_longlong_t)tzb->zb_count); 6565 (void) printf("\t%-16s %14llu\n", "ganged count:", 6566 (longlong_t)tzb->zb_gangs); 6567 (void) printf("\t%-16s %14llu avg: %6llu\n", "bp logical:", 6568 (u_longlong_t)tzb->zb_lsize, 6569 (u_longlong_t)(tzb->zb_lsize / tzb->zb_count)); 6570 (void) printf("\t%-16s %14llu avg: %6llu compression: %6.2f\n", 6571 "bp physical:", (u_longlong_t)tzb->zb_psize, 6572 (u_longlong_t)(tzb->zb_psize / tzb->zb_count), 6573 (double)tzb->zb_lsize / tzb->zb_psize); 6574 (void) printf("\t%-16s %14llu avg: %6llu compression: %6.2f\n", 6575 "bp allocated:", (u_longlong_t)tzb->zb_asize, 6576 (u_longlong_t)(tzb->zb_asize / tzb->zb_count), 6577 (double)tzb->zb_lsize / tzb->zb_asize); 6578 (void) printf("\t%-16s %14llu ref>1: %6llu deduplication: %6.2f\n", 6579 "bp deduped:", (u_longlong_t)zcb->zcb_dedup_asize, 6580 (u_longlong_t)zcb->zcb_dedup_blocks, 6581 (double)zcb->zcb_dedup_asize / tzb->zb_asize + 1.0); 6582 (void) printf("\t%-16s %14llu used: %5.2f%%\n", "Normal class:", 6583 (u_longlong_t)norm_alloc, 100.0 * norm_alloc / norm_space); 6584 6585 if (spa_special_class(spa)->mc_allocator[0].mca_rotor != NULL) { 6586 uint64_t alloc = metaslab_class_get_alloc( 6587 spa_special_class(spa)); 6588 uint64_t space = metaslab_class_get_space( 6589 spa_special_class(spa)); 6590 6591 (void) printf("\t%-16s %14llu used: %5.2f%%\n", 6592 "Special class", (u_longlong_t)alloc, 6593 100.0 * alloc / space); 6594 } 6595 6596 if (spa_dedup_class(spa)->mc_allocator[0].mca_rotor != NULL) { 6597 uint64_t alloc = metaslab_class_get_alloc( 6598 spa_dedup_class(spa)); 6599 uint64_t space = metaslab_class_get_space( 6600 spa_dedup_class(spa)); 6601 6602 (void) printf("\t%-16s %14llu used: %5.2f%%\n", 6603 "Dedup class", (u_longlong_t)alloc, 6604 100.0 * alloc / space); 6605 } 6606 6607 if (spa_embedded_log_class(spa)->mc_allocator[0].mca_rotor != NULL) { 6608 uint64_t alloc = metaslab_class_get_alloc( 6609 spa_embedded_log_class(spa)); 6610 uint64_t space = metaslab_class_get_space( 6611 spa_embedded_log_class(spa)); 6612 6613 (void) printf("\t%-16s %14llu used: %5.2f%%\n", 6614 "Embedded log class", (u_longlong_t)alloc, 6615 100.0 * alloc / space); 6616 } 6617 6618 for (i = 0; i < NUM_BP_EMBEDDED_TYPES; i++) { 6619 if (zcb->zcb_embedded_blocks[i] == 0) 6620 continue; 6621 (void) printf("\n"); 6622 (void) printf("\tadditional, non-pointer bps of type %u: " 6623 "%10llu\n", 6624 i, (u_longlong_t)zcb->zcb_embedded_blocks[i]); 6625 6626 if (dump_opt['b'] >= 3) { 6627 (void) printf("\t number of (compressed) bytes: " 6628 "number of bps\n"); 6629 dump_histogram(zcb->zcb_embedded_histogram[i], 6630 sizeof (zcb->zcb_embedded_histogram[i]) / 6631 sizeof (zcb->zcb_embedded_histogram[i][0]), 0); 6632 } 6633 } 6634 6635 if (tzb->zb_ditto_samevdev != 0) { 6636 (void) printf("\tDittoed blocks on same vdev: %llu\n", 6637 (longlong_t)tzb->zb_ditto_samevdev); 6638 } 6639 if (tzb->zb_ditto_same_ms != 0) { 6640 (void) printf("\tDittoed blocks in same metaslab: %llu\n", 6641 (longlong_t)tzb->zb_ditto_same_ms); 6642 } 6643 6644 for (uint64_t v = 0; v < spa->spa_root_vdev->vdev_children; v++) { 6645 vdev_t *vd = spa->spa_root_vdev->vdev_child[v]; 6646 vdev_indirect_mapping_t *vim = vd->vdev_indirect_mapping; 6647 6648 if (vim == NULL) { 6649 continue; 6650 } 6651 6652 char mem[32]; 6653 zdb_nicenum(vdev_indirect_mapping_num_entries(vim), 6654 mem, vdev_indirect_mapping_size(vim)); 6655 6656 (void) printf("\tindirect vdev id %llu has %llu segments " 6657 "(%s in memory)\n", 6658 (longlong_t)vd->vdev_id, 6659 (longlong_t)vdev_indirect_mapping_num_entries(vim), mem); 6660 } 6661 6662 if (dump_opt['b'] >= 2) { 6663 int l, t, level; 6664 (void) printf("\nBlocks\tLSIZE\tPSIZE\tASIZE" 6665 "\t avg\t comp\t%%Total\tType\n"); 6666 6667 for (t = 0; t <= ZDB_OT_TOTAL; t++) { 6668 char csize[32], lsize[32], psize[32], asize[32]; 6669 char avg[32], gang[32]; 6670 const char *typename; 6671 6672 /* make sure nicenum has enough space */ 6673 _Static_assert(sizeof (csize) >= NN_NUMBUF_SZ, 6674 "csize truncated"); 6675 _Static_assert(sizeof (lsize) >= NN_NUMBUF_SZ, 6676 "lsize truncated"); 6677 _Static_assert(sizeof (psize) >= NN_NUMBUF_SZ, 6678 "psize truncated"); 6679 _Static_assert(sizeof (asize) >= NN_NUMBUF_SZ, 6680 "asize truncated"); 6681 _Static_assert(sizeof (avg) >= NN_NUMBUF_SZ, 6682 "avg truncated"); 6683 _Static_assert(sizeof (gang) >= NN_NUMBUF_SZ, 6684 "gang truncated"); 6685 6686 if (t < DMU_OT_NUMTYPES) 6687 typename = dmu_ot[t].ot_name; 6688 else 6689 typename = zdb_ot_extname[t - DMU_OT_NUMTYPES]; 6690 6691 if (zcb->zcb_type[ZB_TOTAL][t].zb_asize == 0) { 6692 (void) printf("%6s\t%5s\t%5s\t%5s" 6693 "\t%5s\t%5s\t%6s\t%s\n", 6694 "-", 6695 "-", 6696 "-", 6697 "-", 6698 "-", 6699 "-", 6700 "-", 6701 typename); 6702 continue; 6703 } 6704 6705 for (l = ZB_TOTAL - 1; l >= -1; l--) { 6706 level = (l == -1 ? ZB_TOTAL : l); 6707 zb = &zcb->zcb_type[level][t]; 6708 6709 if (zb->zb_asize == 0) 6710 continue; 6711 6712 if (dump_opt['b'] < 3 && level != ZB_TOTAL) 6713 continue; 6714 6715 if (level == 0 && zb->zb_asize == 6716 zcb->zcb_type[ZB_TOTAL][t].zb_asize) 6717 continue; 6718 6719 zdb_nicenum(zb->zb_count, csize, 6720 sizeof (csize)); 6721 zdb_nicenum(zb->zb_lsize, lsize, 6722 sizeof (lsize)); 6723 zdb_nicenum(zb->zb_psize, psize, 6724 sizeof (psize)); 6725 zdb_nicenum(zb->zb_asize, asize, 6726 sizeof (asize)); 6727 zdb_nicenum(zb->zb_asize / zb->zb_count, avg, 6728 sizeof (avg)); 6729 zdb_nicenum(zb->zb_gangs, gang, sizeof (gang)); 6730 6731 (void) printf("%6s\t%5s\t%5s\t%5s\t%5s" 6732 "\t%5.2f\t%6.2f\t", 6733 csize, lsize, psize, asize, avg, 6734 (double)zb->zb_lsize / zb->zb_psize, 6735 100.0 * zb->zb_asize / tzb->zb_asize); 6736 6737 if (level == ZB_TOTAL) 6738 (void) printf("%s\n", typename); 6739 else 6740 (void) printf(" L%d %s\n", 6741 level, typename); 6742 6743 if (dump_opt['b'] >= 3 && zb->zb_gangs > 0) { 6744 (void) printf("\t number of ganged " 6745 "blocks: %s\n", gang); 6746 } 6747 6748 if (dump_opt['b'] >= 4) { 6749 (void) printf("psize " 6750 "(in 512-byte sectors): " 6751 "number of blocks\n"); 6752 dump_histogram(zb->zb_psize_histogram, 6753 PSIZE_HISTO_SIZE, 0); 6754 } 6755 } 6756 } 6757 6758 /* Output a table summarizing block sizes in the pool */ 6759 if (dump_opt['b'] >= 2) { 6760 dump_size_histograms(zcb); 6761 } 6762 } 6763 6764 (void) printf("\n"); 6765 6766 if (leaks) { 6767 umem_free(zcb, sizeof (zdb_cb_t)); 6768 return (2); 6769 } 6770 6771 if (zcb->zcb_haderrors) { 6772 umem_free(zcb, sizeof (zdb_cb_t)); 6773 return (3); 6774 } 6775 6776 umem_free(zcb, sizeof (zdb_cb_t)); 6777 return (0); 6778 } 6779 6780 typedef struct zdb_ddt_entry { 6781 ddt_key_t zdde_key; 6782 uint64_t zdde_ref_blocks; 6783 uint64_t zdde_ref_lsize; 6784 uint64_t zdde_ref_psize; 6785 uint64_t zdde_ref_dsize; 6786 avl_node_t zdde_node; 6787 } zdb_ddt_entry_t; 6788 6789 static int 6790 zdb_ddt_add_cb(spa_t *spa, zilog_t *zilog, const blkptr_t *bp, 6791 const zbookmark_phys_t *zb, const dnode_phys_t *dnp, void *arg) 6792 { 6793 (void) zilog, (void) dnp; 6794 avl_tree_t *t = arg; 6795 avl_index_t where; 6796 zdb_ddt_entry_t *zdde, zdde_search; 6797 6798 if (zb->zb_level == ZB_DNODE_LEVEL || BP_IS_HOLE(bp) || 6799 BP_IS_EMBEDDED(bp)) 6800 return (0); 6801 6802 if (dump_opt['S'] > 1 && zb->zb_level == ZB_ROOT_LEVEL) { 6803 (void) printf("traversing objset %llu, %llu objects, " 6804 "%lu blocks so far\n", 6805 (u_longlong_t)zb->zb_objset, 6806 (u_longlong_t)BP_GET_FILL(bp), 6807 avl_numnodes(t)); 6808 } 6809 6810 if (BP_IS_HOLE(bp) || BP_GET_CHECKSUM(bp) == ZIO_CHECKSUM_OFF || 6811 BP_GET_LEVEL(bp) > 0 || DMU_OT_IS_METADATA(BP_GET_TYPE(bp))) 6812 return (0); 6813 6814 ddt_key_fill(&zdde_search.zdde_key, bp); 6815 6816 zdde = avl_find(t, &zdde_search, &where); 6817 6818 if (zdde == NULL) { 6819 zdde = umem_zalloc(sizeof (*zdde), UMEM_NOFAIL); 6820 zdde->zdde_key = zdde_search.zdde_key; 6821 avl_insert(t, zdde, where); 6822 } 6823 6824 zdde->zdde_ref_blocks += 1; 6825 zdde->zdde_ref_lsize += BP_GET_LSIZE(bp); 6826 zdde->zdde_ref_psize += BP_GET_PSIZE(bp); 6827 zdde->zdde_ref_dsize += bp_get_dsize_sync(spa, bp); 6828 6829 return (0); 6830 } 6831 6832 static void 6833 dump_simulated_ddt(spa_t *spa) 6834 { 6835 avl_tree_t t; 6836 void *cookie = NULL; 6837 zdb_ddt_entry_t *zdde; 6838 ddt_histogram_t ddh_total = {{{0}}}; 6839 ddt_stat_t dds_total = {0}; 6840 6841 avl_create(&t, ddt_entry_compare, 6842 sizeof (zdb_ddt_entry_t), offsetof(zdb_ddt_entry_t, zdde_node)); 6843 6844 spa_config_enter(spa, SCL_CONFIG, FTAG, RW_READER); 6845 6846 (void) traverse_pool(spa, 0, TRAVERSE_PRE | TRAVERSE_PREFETCH_METADATA | 6847 TRAVERSE_NO_DECRYPT, zdb_ddt_add_cb, &t); 6848 6849 spa_config_exit(spa, SCL_CONFIG, FTAG); 6850 6851 while ((zdde = avl_destroy_nodes(&t, &cookie)) != NULL) { 6852 ddt_stat_t dds; 6853 uint64_t refcnt = zdde->zdde_ref_blocks; 6854 ASSERT(refcnt != 0); 6855 6856 dds.dds_blocks = zdde->zdde_ref_blocks / refcnt; 6857 dds.dds_lsize = zdde->zdde_ref_lsize / refcnt; 6858 dds.dds_psize = zdde->zdde_ref_psize / refcnt; 6859 dds.dds_dsize = zdde->zdde_ref_dsize / refcnt; 6860 6861 dds.dds_ref_blocks = zdde->zdde_ref_blocks; 6862 dds.dds_ref_lsize = zdde->zdde_ref_lsize; 6863 dds.dds_ref_psize = zdde->zdde_ref_psize; 6864 dds.dds_ref_dsize = zdde->zdde_ref_dsize; 6865 6866 ddt_stat_add(&ddh_total.ddh_stat[highbit64(refcnt) - 1], 6867 &dds, 0); 6868 6869 umem_free(zdde, sizeof (*zdde)); 6870 } 6871 6872 avl_destroy(&t); 6873 6874 ddt_histogram_stat(&dds_total, &ddh_total); 6875 6876 (void) printf("Simulated DDT histogram:\n"); 6877 6878 zpool_dump_ddt(&dds_total, &ddh_total); 6879 6880 dump_dedup_ratio(&dds_total); 6881 } 6882 6883 static int 6884 verify_device_removal_feature_counts(spa_t *spa) 6885 { 6886 uint64_t dr_feature_refcount = 0; 6887 uint64_t oc_feature_refcount = 0; 6888 uint64_t indirect_vdev_count = 0; 6889 uint64_t precise_vdev_count = 0; 6890 uint64_t obsolete_counts_object_count = 0; 6891 uint64_t obsolete_sm_count = 0; 6892 uint64_t obsolete_counts_count = 0; 6893 uint64_t scip_count = 0; 6894 uint64_t obsolete_bpobj_count = 0; 6895 int ret = 0; 6896 6897 spa_condensing_indirect_phys_t *scip = 6898 &spa->spa_condensing_indirect_phys; 6899 if (scip->scip_next_mapping_object != 0) { 6900 vdev_t *vd = spa->spa_root_vdev->vdev_child[scip->scip_vdev]; 6901 ASSERT(scip->scip_prev_obsolete_sm_object != 0); 6902 ASSERT3P(vd->vdev_ops, ==, &vdev_indirect_ops); 6903 6904 (void) printf("Condensing indirect vdev %llu: new mapping " 6905 "object %llu, prev obsolete sm %llu\n", 6906 (u_longlong_t)scip->scip_vdev, 6907 (u_longlong_t)scip->scip_next_mapping_object, 6908 (u_longlong_t)scip->scip_prev_obsolete_sm_object); 6909 if (scip->scip_prev_obsolete_sm_object != 0) { 6910 space_map_t *prev_obsolete_sm = NULL; 6911 VERIFY0(space_map_open(&prev_obsolete_sm, 6912 spa->spa_meta_objset, 6913 scip->scip_prev_obsolete_sm_object, 6914 0, vd->vdev_asize, 0)); 6915 dump_spacemap(spa->spa_meta_objset, prev_obsolete_sm); 6916 (void) printf("\n"); 6917 space_map_close(prev_obsolete_sm); 6918 } 6919 6920 scip_count += 2; 6921 } 6922 6923 for (uint64_t i = 0; i < spa->spa_root_vdev->vdev_children; i++) { 6924 vdev_t *vd = spa->spa_root_vdev->vdev_child[i]; 6925 vdev_indirect_config_t *vic = &vd->vdev_indirect_config; 6926 6927 if (vic->vic_mapping_object != 0) { 6928 ASSERT(vd->vdev_ops == &vdev_indirect_ops || 6929 vd->vdev_removing); 6930 indirect_vdev_count++; 6931 6932 if (vd->vdev_indirect_mapping->vim_havecounts) { 6933 obsolete_counts_count++; 6934 } 6935 } 6936 6937 boolean_t are_precise; 6938 VERIFY0(vdev_obsolete_counts_are_precise(vd, &are_precise)); 6939 if (are_precise) { 6940 ASSERT(vic->vic_mapping_object != 0); 6941 precise_vdev_count++; 6942 } 6943 6944 uint64_t obsolete_sm_object; 6945 VERIFY0(vdev_obsolete_sm_object(vd, &obsolete_sm_object)); 6946 if (obsolete_sm_object != 0) { 6947 ASSERT(vic->vic_mapping_object != 0); 6948 obsolete_sm_count++; 6949 } 6950 } 6951 6952 (void) feature_get_refcount(spa, 6953 &spa_feature_table[SPA_FEATURE_DEVICE_REMOVAL], 6954 &dr_feature_refcount); 6955 (void) feature_get_refcount(spa, 6956 &spa_feature_table[SPA_FEATURE_OBSOLETE_COUNTS], 6957 &oc_feature_refcount); 6958 6959 if (dr_feature_refcount != indirect_vdev_count) { 6960 ret = 1; 6961 (void) printf("Number of indirect vdevs (%llu) " \ 6962 "does not match feature count (%llu)\n", 6963 (u_longlong_t)indirect_vdev_count, 6964 (u_longlong_t)dr_feature_refcount); 6965 } else { 6966 (void) printf("Verified device_removal feature refcount " \ 6967 "of %llu is correct\n", 6968 (u_longlong_t)dr_feature_refcount); 6969 } 6970 6971 if (zap_contains(spa_meta_objset(spa), DMU_POOL_DIRECTORY_OBJECT, 6972 DMU_POOL_OBSOLETE_BPOBJ) == 0) { 6973 obsolete_bpobj_count++; 6974 } 6975 6976 6977 obsolete_counts_object_count = precise_vdev_count; 6978 obsolete_counts_object_count += obsolete_sm_count; 6979 obsolete_counts_object_count += obsolete_counts_count; 6980 obsolete_counts_object_count += scip_count; 6981 obsolete_counts_object_count += obsolete_bpobj_count; 6982 obsolete_counts_object_count += remap_deadlist_count; 6983 6984 if (oc_feature_refcount != obsolete_counts_object_count) { 6985 ret = 1; 6986 (void) printf("Number of obsolete counts objects (%llu) " \ 6987 "does not match feature count (%llu)\n", 6988 (u_longlong_t)obsolete_counts_object_count, 6989 (u_longlong_t)oc_feature_refcount); 6990 (void) printf("pv:%llu os:%llu oc:%llu sc:%llu " 6991 "ob:%llu rd:%llu\n", 6992 (u_longlong_t)precise_vdev_count, 6993 (u_longlong_t)obsolete_sm_count, 6994 (u_longlong_t)obsolete_counts_count, 6995 (u_longlong_t)scip_count, 6996 (u_longlong_t)obsolete_bpobj_count, 6997 (u_longlong_t)remap_deadlist_count); 6998 } else { 6999 (void) printf("Verified indirect_refcount feature refcount " \ 7000 "of %llu is correct\n", 7001 (u_longlong_t)oc_feature_refcount); 7002 } 7003 return (ret); 7004 } 7005 7006 static void 7007 zdb_set_skip_mmp(char *target) 7008 { 7009 spa_t *spa; 7010 7011 /* 7012 * Disable the activity check to allow examination of 7013 * active pools. 7014 */ 7015 mutex_enter(&spa_namespace_lock); 7016 if ((spa = spa_lookup(target)) != NULL) { 7017 spa->spa_import_flags |= ZFS_IMPORT_SKIP_MMP; 7018 } 7019 mutex_exit(&spa_namespace_lock); 7020 } 7021 7022 #define BOGUS_SUFFIX "_CHECKPOINTED_UNIVERSE" 7023 /* 7024 * Import the checkpointed state of the pool specified by the target 7025 * parameter as readonly. The function also accepts a pool config 7026 * as an optional parameter, else it attempts to infer the config by 7027 * the name of the target pool. 7028 * 7029 * Note that the checkpointed state's pool name will be the name of 7030 * the original pool with the above suffix appended to it. In addition, 7031 * if the target is not a pool name (e.g. a path to a dataset) then 7032 * the new_path parameter is populated with the updated path to 7033 * reflect the fact that we are looking into the checkpointed state. 7034 * 7035 * The function returns a newly-allocated copy of the name of the 7036 * pool containing the checkpointed state. When this copy is no 7037 * longer needed it should be freed with free(3C). Same thing 7038 * applies to the new_path parameter if allocated. 7039 */ 7040 static char * 7041 import_checkpointed_state(char *target, nvlist_t *cfg, char **new_path) 7042 { 7043 int error = 0; 7044 char *poolname, *bogus_name = NULL; 7045 boolean_t freecfg = B_FALSE; 7046 7047 /* If the target is not a pool, the extract the pool name */ 7048 char *path_start = strchr(target, '/'); 7049 if (path_start != NULL) { 7050 size_t poolname_len = path_start - target; 7051 poolname = strndup(target, poolname_len); 7052 } else { 7053 poolname = target; 7054 } 7055 7056 if (cfg == NULL) { 7057 zdb_set_skip_mmp(poolname); 7058 error = spa_get_stats(poolname, &cfg, NULL, 0); 7059 if (error != 0) { 7060 fatal("Tried to read config of pool \"%s\" but " 7061 "spa_get_stats() failed with error %d\n", 7062 poolname, error); 7063 } 7064 freecfg = B_TRUE; 7065 } 7066 7067 if (asprintf(&bogus_name, "%s%s", poolname, BOGUS_SUFFIX) == -1) { 7068 if (target != poolname) 7069 free(poolname); 7070 return (NULL); 7071 } 7072 fnvlist_add_string(cfg, ZPOOL_CONFIG_POOL_NAME, bogus_name); 7073 7074 error = spa_import(bogus_name, cfg, NULL, 7075 ZFS_IMPORT_MISSING_LOG | ZFS_IMPORT_CHECKPOINT | 7076 ZFS_IMPORT_SKIP_MMP); 7077 if (freecfg) 7078 nvlist_free(cfg); 7079 if (error != 0) { 7080 fatal("Tried to import pool \"%s\" but spa_import() failed " 7081 "with error %d\n", bogus_name, error); 7082 } 7083 7084 if (new_path != NULL && path_start != NULL) { 7085 if (asprintf(new_path, "%s%s", bogus_name, path_start) == -1) { 7086 free(bogus_name); 7087 if (path_start != NULL) 7088 free(poolname); 7089 return (NULL); 7090 } 7091 } 7092 7093 if (target != poolname) 7094 free(poolname); 7095 7096 return (bogus_name); 7097 } 7098 7099 typedef struct verify_checkpoint_sm_entry_cb_arg { 7100 vdev_t *vcsec_vd; 7101 7102 /* the following fields are only used for printing progress */ 7103 uint64_t vcsec_entryid; 7104 uint64_t vcsec_num_entries; 7105 } verify_checkpoint_sm_entry_cb_arg_t; 7106 7107 #define ENTRIES_PER_PROGRESS_UPDATE 10000 7108 7109 static int 7110 verify_checkpoint_sm_entry_cb(space_map_entry_t *sme, void *arg) 7111 { 7112 verify_checkpoint_sm_entry_cb_arg_t *vcsec = arg; 7113 vdev_t *vd = vcsec->vcsec_vd; 7114 metaslab_t *ms = vd->vdev_ms[sme->sme_offset >> vd->vdev_ms_shift]; 7115 uint64_t end = sme->sme_offset + sme->sme_run; 7116 7117 ASSERT(sme->sme_type == SM_FREE); 7118 7119 if ((vcsec->vcsec_entryid % ENTRIES_PER_PROGRESS_UPDATE) == 0) { 7120 (void) fprintf(stderr, 7121 "\rverifying vdev %llu, space map entry %llu of %llu ...", 7122 (longlong_t)vd->vdev_id, 7123 (longlong_t)vcsec->vcsec_entryid, 7124 (longlong_t)vcsec->vcsec_num_entries); 7125 } 7126 vcsec->vcsec_entryid++; 7127 7128 /* 7129 * See comment in checkpoint_sm_exclude_entry_cb() 7130 */ 7131 VERIFY3U(sme->sme_offset, >=, ms->ms_start); 7132 VERIFY3U(end, <=, ms->ms_start + ms->ms_size); 7133 7134 /* 7135 * The entries in the vdev_checkpoint_sm should be marked as 7136 * allocated in the checkpointed state of the pool, therefore 7137 * their respective ms_allocateable trees should not contain them. 7138 */ 7139 mutex_enter(&ms->ms_lock); 7140 range_tree_verify_not_present(ms->ms_allocatable, 7141 sme->sme_offset, sme->sme_run); 7142 mutex_exit(&ms->ms_lock); 7143 7144 return (0); 7145 } 7146 7147 /* 7148 * Verify that all segments in the vdev_checkpoint_sm are allocated 7149 * according to the checkpoint's ms_sm (i.e. are not in the checkpoint's 7150 * ms_allocatable). 7151 * 7152 * Do so by comparing the checkpoint space maps (vdev_checkpoint_sm) of 7153 * each vdev in the current state of the pool to the metaslab space maps 7154 * (ms_sm) of the checkpointed state of the pool. 7155 * 7156 * Note that the function changes the state of the ms_allocatable 7157 * trees of the current spa_t. The entries of these ms_allocatable 7158 * trees are cleared out and then repopulated from with the free 7159 * entries of their respective ms_sm space maps. 7160 */ 7161 static void 7162 verify_checkpoint_vdev_spacemaps(spa_t *checkpoint, spa_t *current) 7163 { 7164 vdev_t *ckpoint_rvd = checkpoint->spa_root_vdev; 7165 vdev_t *current_rvd = current->spa_root_vdev; 7166 7167 load_concrete_ms_allocatable_trees(checkpoint, SM_FREE); 7168 7169 for (uint64_t c = 0; c < ckpoint_rvd->vdev_children; c++) { 7170 vdev_t *ckpoint_vd = ckpoint_rvd->vdev_child[c]; 7171 vdev_t *current_vd = current_rvd->vdev_child[c]; 7172 7173 space_map_t *checkpoint_sm = NULL; 7174 uint64_t checkpoint_sm_obj; 7175 7176 if (ckpoint_vd->vdev_ops == &vdev_indirect_ops) { 7177 /* 7178 * Since we don't allow device removal in a pool 7179 * that has a checkpoint, we expect that all removed 7180 * vdevs were removed from the pool before the 7181 * checkpoint. 7182 */ 7183 ASSERT3P(current_vd->vdev_ops, ==, &vdev_indirect_ops); 7184 continue; 7185 } 7186 7187 /* 7188 * If the checkpoint space map doesn't exist, then nothing 7189 * here is checkpointed so there's nothing to verify. 7190 */ 7191 if (current_vd->vdev_top_zap == 0 || 7192 zap_contains(spa_meta_objset(current), 7193 current_vd->vdev_top_zap, 7194 VDEV_TOP_ZAP_POOL_CHECKPOINT_SM) != 0) 7195 continue; 7196 7197 VERIFY0(zap_lookup(spa_meta_objset(current), 7198 current_vd->vdev_top_zap, VDEV_TOP_ZAP_POOL_CHECKPOINT_SM, 7199 sizeof (uint64_t), 1, &checkpoint_sm_obj)); 7200 7201 VERIFY0(space_map_open(&checkpoint_sm, spa_meta_objset(current), 7202 checkpoint_sm_obj, 0, current_vd->vdev_asize, 7203 current_vd->vdev_ashift)); 7204 7205 verify_checkpoint_sm_entry_cb_arg_t vcsec; 7206 vcsec.vcsec_vd = ckpoint_vd; 7207 vcsec.vcsec_entryid = 0; 7208 vcsec.vcsec_num_entries = 7209 space_map_length(checkpoint_sm) / sizeof (uint64_t); 7210 VERIFY0(space_map_iterate(checkpoint_sm, 7211 space_map_length(checkpoint_sm), 7212 verify_checkpoint_sm_entry_cb, &vcsec)); 7213 if (dump_opt['m'] > 3) 7214 dump_spacemap(current->spa_meta_objset, checkpoint_sm); 7215 space_map_close(checkpoint_sm); 7216 } 7217 7218 /* 7219 * If we've added vdevs since we took the checkpoint, ensure 7220 * that their checkpoint space maps are empty. 7221 */ 7222 if (ckpoint_rvd->vdev_children < current_rvd->vdev_children) { 7223 for (uint64_t c = ckpoint_rvd->vdev_children; 7224 c < current_rvd->vdev_children; c++) { 7225 vdev_t *current_vd = current_rvd->vdev_child[c]; 7226 VERIFY3P(current_vd->vdev_checkpoint_sm, ==, NULL); 7227 } 7228 } 7229 7230 /* for cleaner progress output */ 7231 (void) fprintf(stderr, "\n"); 7232 } 7233 7234 /* 7235 * Verifies that all space that's allocated in the checkpoint is 7236 * still allocated in the current version, by checking that everything 7237 * in checkpoint's ms_allocatable (which is actually allocated, not 7238 * allocatable/free) is not present in current's ms_allocatable. 7239 * 7240 * Note that the function changes the state of the ms_allocatable 7241 * trees of both spas when called. The entries of all ms_allocatable 7242 * trees are cleared out and then repopulated from their respective 7243 * ms_sm space maps. In the checkpointed state we load the allocated 7244 * entries, and in the current state we load the free entries. 7245 */ 7246 static void 7247 verify_checkpoint_ms_spacemaps(spa_t *checkpoint, spa_t *current) 7248 { 7249 vdev_t *ckpoint_rvd = checkpoint->spa_root_vdev; 7250 vdev_t *current_rvd = current->spa_root_vdev; 7251 7252 load_concrete_ms_allocatable_trees(checkpoint, SM_ALLOC); 7253 load_concrete_ms_allocatable_trees(current, SM_FREE); 7254 7255 for (uint64_t i = 0; i < ckpoint_rvd->vdev_children; i++) { 7256 vdev_t *ckpoint_vd = ckpoint_rvd->vdev_child[i]; 7257 vdev_t *current_vd = current_rvd->vdev_child[i]; 7258 7259 if (ckpoint_vd->vdev_ops == &vdev_indirect_ops) { 7260 /* 7261 * See comment in verify_checkpoint_vdev_spacemaps() 7262 */ 7263 ASSERT3P(current_vd->vdev_ops, ==, &vdev_indirect_ops); 7264 continue; 7265 } 7266 7267 for (uint64_t m = 0; m < ckpoint_vd->vdev_ms_count; m++) { 7268 metaslab_t *ckpoint_msp = ckpoint_vd->vdev_ms[m]; 7269 metaslab_t *current_msp = current_vd->vdev_ms[m]; 7270 7271 (void) fprintf(stderr, 7272 "\rverifying vdev %llu of %llu, " 7273 "metaslab %llu of %llu ...", 7274 (longlong_t)current_vd->vdev_id, 7275 (longlong_t)current_rvd->vdev_children, 7276 (longlong_t)current_vd->vdev_ms[m]->ms_id, 7277 (longlong_t)current_vd->vdev_ms_count); 7278 7279 /* 7280 * We walk through the ms_allocatable trees that 7281 * are loaded with the allocated blocks from the 7282 * ms_sm spacemaps of the checkpoint. For each 7283 * one of these ranges we ensure that none of them 7284 * exists in the ms_allocatable trees of the 7285 * current state which are loaded with the ranges 7286 * that are currently free. 7287 * 7288 * This way we ensure that none of the blocks that 7289 * are part of the checkpoint were freed by mistake. 7290 */ 7291 range_tree_walk(ckpoint_msp->ms_allocatable, 7292 (range_tree_func_t *)range_tree_verify_not_present, 7293 current_msp->ms_allocatable); 7294 } 7295 } 7296 7297 /* for cleaner progress output */ 7298 (void) fprintf(stderr, "\n"); 7299 } 7300 7301 static void 7302 verify_checkpoint_blocks(spa_t *spa) 7303 { 7304 ASSERT(!dump_opt['L']); 7305 7306 spa_t *checkpoint_spa; 7307 char *checkpoint_pool; 7308 int error = 0; 7309 7310 /* 7311 * We import the checkpointed state of the pool (under a different 7312 * name) so we can do verification on it against the current state 7313 * of the pool. 7314 */ 7315 checkpoint_pool = import_checkpointed_state(spa->spa_name, NULL, 7316 NULL); 7317 ASSERT(strcmp(spa->spa_name, checkpoint_pool) != 0); 7318 7319 error = spa_open(checkpoint_pool, &checkpoint_spa, FTAG); 7320 if (error != 0) { 7321 fatal("Tried to open pool \"%s\" but spa_open() failed with " 7322 "error %d\n", checkpoint_pool, error); 7323 } 7324 7325 /* 7326 * Ensure that ranges in the checkpoint space maps of each vdev 7327 * are allocated according to the checkpointed state's metaslab 7328 * space maps. 7329 */ 7330 verify_checkpoint_vdev_spacemaps(checkpoint_spa, spa); 7331 7332 /* 7333 * Ensure that allocated ranges in the checkpoint's metaslab 7334 * space maps remain allocated in the metaslab space maps of 7335 * the current state. 7336 */ 7337 verify_checkpoint_ms_spacemaps(checkpoint_spa, spa); 7338 7339 /* 7340 * Once we are done, we get rid of the checkpointed state. 7341 */ 7342 spa_close(checkpoint_spa, FTAG); 7343 free(checkpoint_pool); 7344 } 7345 7346 static void 7347 dump_leftover_checkpoint_blocks(spa_t *spa) 7348 { 7349 vdev_t *rvd = spa->spa_root_vdev; 7350 7351 for (uint64_t i = 0; i < rvd->vdev_children; i++) { 7352 vdev_t *vd = rvd->vdev_child[i]; 7353 7354 space_map_t *checkpoint_sm = NULL; 7355 uint64_t checkpoint_sm_obj; 7356 7357 if (vd->vdev_top_zap == 0) 7358 continue; 7359 7360 if (zap_contains(spa_meta_objset(spa), vd->vdev_top_zap, 7361 VDEV_TOP_ZAP_POOL_CHECKPOINT_SM) != 0) 7362 continue; 7363 7364 VERIFY0(zap_lookup(spa_meta_objset(spa), vd->vdev_top_zap, 7365 VDEV_TOP_ZAP_POOL_CHECKPOINT_SM, 7366 sizeof (uint64_t), 1, &checkpoint_sm_obj)); 7367 7368 VERIFY0(space_map_open(&checkpoint_sm, spa_meta_objset(spa), 7369 checkpoint_sm_obj, 0, vd->vdev_asize, vd->vdev_ashift)); 7370 dump_spacemap(spa->spa_meta_objset, checkpoint_sm); 7371 space_map_close(checkpoint_sm); 7372 } 7373 } 7374 7375 static int 7376 verify_checkpoint(spa_t *spa) 7377 { 7378 uberblock_t checkpoint; 7379 int error; 7380 7381 if (!spa_feature_is_active(spa, SPA_FEATURE_POOL_CHECKPOINT)) 7382 return (0); 7383 7384 error = zap_lookup(spa->spa_meta_objset, DMU_POOL_DIRECTORY_OBJECT, 7385 DMU_POOL_ZPOOL_CHECKPOINT, sizeof (uint64_t), 7386 sizeof (uberblock_t) / sizeof (uint64_t), &checkpoint); 7387 7388 if (error == ENOENT && !dump_opt['L']) { 7389 /* 7390 * If the feature is active but the uberblock is missing 7391 * then we must be in the middle of discarding the 7392 * checkpoint. 7393 */ 7394 (void) printf("\nPartially discarded checkpoint " 7395 "state found:\n"); 7396 if (dump_opt['m'] > 3) 7397 dump_leftover_checkpoint_blocks(spa); 7398 return (0); 7399 } else if (error != 0) { 7400 (void) printf("lookup error %d when looking for " 7401 "checkpointed uberblock in MOS\n", error); 7402 return (error); 7403 } 7404 dump_uberblock(&checkpoint, "\nCheckpointed uberblock found:\n", "\n"); 7405 7406 if (checkpoint.ub_checkpoint_txg == 0) { 7407 (void) printf("\nub_checkpoint_txg not set in checkpointed " 7408 "uberblock\n"); 7409 error = 3; 7410 } 7411 7412 if (error == 0 && !dump_opt['L']) 7413 verify_checkpoint_blocks(spa); 7414 7415 return (error); 7416 } 7417 7418 static void 7419 mos_leaks_cb(void *arg, uint64_t start, uint64_t size) 7420 { 7421 (void) arg; 7422 for (uint64_t i = start; i < size; i++) { 7423 (void) printf("MOS object %llu referenced but not allocated\n", 7424 (u_longlong_t)i); 7425 } 7426 } 7427 7428 static void 7429 mos_obj_refd(uint64_t obj) 7430 { 7431 if (obj != 0 && mos_refd_objs != NULL) 7432 range_tree_add(mos_refd_objs, obj, 1); 7433 } 7434 7435 /* 7436 * Call on a MOS object that may already have been referenced. 7437 */ 7438 static void 7439 mos_obj_refd_multiple(uint64_t obj) 7440 { 7441 if (obj != 0 && mos_refd_objs != NULL && 7442 !range_tree_contains(mos_refd_objs, obj, 1)) 7443 range_tree_add(mos_refd_objs, obj, 1); 7444 } 7445 7446 static void 7447 mos_leak_vdev_top_zap(vdev_t *vd) 7448 { 7449 uint64_t ms_flush_data_obj; 7450 int error = zap_lookup(spa_meta_objset(vd->vdev_spa), 7451 vd->vdev_top_zap, VDEV_TOP_ZAP_MS_UNFLUSHED_PHYS_TXGS, 7452 sizeof (ms_flush_data_obj), 1, &ms_flush_data_obj); 7453 if (error == ENOENT) 7454 return; 7455 ASSERT0(error); 7456 7457 mos_obj_refd(ms_flush_data_obj); 7458 } 7459 7460 static void 7461 mos_leak_vdev(vdev_t *vd) 7462 { 7463 mos_obj_refd(vd->vdev_dtl_object); 7464 mos_obj_refd(vd->vdev_ms_array); 7465 mos_obj_refd(vd->vdev_indirect_config.vic_births_object); 7466 mos_obj_refd(vd->vdev_indirect_config.vic_mapping_object); 7467 mos_obj_refd(vd->vdev_leaf_zap); 7468 if (vd->vdev_checkpoint_sm != NULL) 7469 mos_obj_refd(vd->vdev_checkpoint_sm->sm_object); 7470 if (vd->vdev_indirect_mapping != NULL) { 7471 mos_obj_refd(vd->vdev_indirect_mapping-> 7472 vim_phys->vimp_counts_object); 7473 } 7474 if (vd->vdev_obsolete_sm != NULL) 7475 mos_obj_refd(vd->vdev_obsolete_sm->sm_object); 7476 7477 for (uint64_t m = 0; m < vd->vdev_ms_count; m++) { 7478 metaslab_t *ms = vd->vdev_ms[m]; 7479 mos_obj_refd(space_map_object(ms->ms_sm)); 7480 } 7481 7482 if (vd->vdev_top_zap != 0) { 7483 mos_obj_refd(vd->vdev_top_zap); 7484 mos_leak_vdev_top_zap(vd); 7485 } 7486 7487 for (uint64_t c = 0; c < vd->vdev_children; c++) { 7488 mos_leak_vdev(vd->vdev_child[c]); 7489 } 7490 } 7491 7492 static void 7493 mos_leak_log_spacemaps(spa_t *spa) 7494 { 7495 uint64_t spacemap_zap; 7496 int error = zap_lookup(spa_meta_objset(spa), 7497 DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_LOG_SPACEMAP_ZAP, 7498 sizeof (spacemap_zap), 1, &spacemap_zap); 7499 if (error == ENOENT) 7500 return; 7501 ASSERT0(error); 7502 7503 mos_obj_refd(spacemap_zap); 7504 for (spa_log_sm_t *sls = avl_first(&spa->spa_sm_logs_by_txg); 7505 sls; sls = AVL_NEXT(&spa->spa_sm_logs_by_txg, sls)) 7506 mos_obj_refd(sls->sls_sm_obj); 7507 } 7508 7509 static int 7510 dump_mos_leaks(spa_t *spa) 7511 { 7512 int rv = 0; 7513 objset_t *mos = spa->spa_meta_objset; 7514 dsl_pool_t *dp = spa->spa_dsl_pool; 7515 7516 /* Visit and mark all referenced objects in the MOS */ 7517 7518 mos_obj_refd(DMU_POOL_DIRECTORY_OBJECT); 7519 mos_obj_refd(spa->spa_pool_props_object); 7520 mos_obj_refd(spa->spa_config_object); 7521 mos_obj_refd(spa->spa_ddt_stat_object); 7522 mos_obj_refd(spa->spa_feat_desc_obj); 7523 mos_obj_refd(spa->spa_feat_enabled_txg_obj); 7524 mos_obj_refd(spa->spa_feat_for_read_obj); 7525 mos_obj_refd(spa->spa_feat_for_write_obj); 7526 mos_obj_refd(spa->spa_history); 7527 mos_obj_refd(spa->spa_errlog_last); 7528 mos_obj_refd(spa->spa_errlog_scrub); 7529 mos_obj_refd(spa->spa_all_vdev_zaps); 7530 mos_obj_refd(spa->spa_dsl_pool->dp_bptree_obj); 7531 mos_obj_refd(spa->spa_dsl_pool->dp_tmp_userrefs_obj); 7532 mos_obj_refd(spa->spa_dsl_pool->dp_scan->scn_phys.scn_queue_obj); 7533 bpobj_count_refd(&spa->spa_deferred_bpobj); 7534 mos_obj_refd(dp->dp_empty_bpobj); 7535 bpobj_count_refd(&dp->dp_obsolete_bpobj); 7536 bpobj_count_refd(&dp->dp_free_bpobj); 7537 mos_obj_refd(spa->spa_l2cache.sav_object); 7538 mos_obj_refd(spa->spa_spares.sav_object); 7539 7540 if (spa->spa_syncing_log_sm != NULL) 7541 mos_obj_refd(spa->spa_syncing_log_sm->sm_object); 7542 mos_leak_log_spacemaps(spa); 7543 7544 mos_obj_refd(spa->spa_condensing_indirect_phys. 7545 scip_next_mapping_object); 7546 mos_obj_refd(spa->spa_condensing_indirect_phys. 7547 scip_prev_obsolete_sm_object); 7548 if (spa->spa_condensing_indirect_phys.scip_next_mapping_object != 0) { 7549 vdev_indirect_mapping_t *vim = 7550 vdev_indirect_mapping_open(mos, 7551 spa->spa_condensing_indirect_phys.scip_next_mapping_object); 7552 mos_obj_refd(vim->vim_phys->vimp_counts_object); 7553 vdev_indirect_mapping_close(vim); 7554 } 7555 deleted_livelists_dump_mos(spa); 7556 7557 if (dp->dp_origin_snap != NULL) { 7558 dsl_dataset_t *ds; 7559 7560 dsl_pool_config_enter(dp, FTAG); 7561 VERIFY0(dsl_dataset_hold_obj(dp, 7562 dsl_dataset_phys(dp->dp_origin_snap)->ds_next_snap_obj, 7563 FTAG, &ds)); 7564 count_ds_mos_objects(ds); 7565 dump_blkptr_list(&ds->ds_deadlist, "Deadlist"); 7566 dsl_dataset_rele(ds, FTAG); 7567 dsl_pool_config_exit(dp, FTAG); 7568 7569 count_ds_mos_objects(dp->dp_origin_snap); 7570 dump_blkptr_list(&dp->dp_origin_snap->ds_deadlist, "Deadlist"); 7571 } 7572 count_dir_mos_objects(dp->dp_mos_dir); 7573 if (dp->dp_free_dir != NULL) 7574 count_dir_mos_objects(dp->dp_free_dir); 7575 if (dp->dp_leak_dir != NULL) 7576 count_dir_mos_objects(dp->dp_leak_dir); 7577 7578 mos_leak_vdev(spa->spa_root_vdev); 7579 7580 for (uint64_t class = 0; class < DDT_CLASSES; class++) { 7581 for (uint64_t type = 0; type < DDT_TYPES; type++) { 7582 for (uint64_t cksum = 0; 7583 cksum < ZIO_CHECKSUM_FUNCTIONS; cksum++) { 7584 ddt_t *ddt = spa->spa_ddt[cksum]; 7585 mos_obj_refd(ddt->ddt_object[type][class]); 7586 } 7587 } 7588 } 7589 7590 /* 7591 * Visit all allocated objects and make sure they are referenced. 7592 */ 7593 uint64_t object = 0; 7594 while (dmu_object_next(mos, &object, B_FALSE, 0) == 0) { 7595 if (range_tree_contains(mos_refd_objs, object, 1)) { 7596 range_tree_remove(mos_refd_objs, object, 1); 7597 } else { 7598 dmu_object_info_t doi; 7599 const char *name; 7600 VERIFY0(dmu_object_info(mos, object, &doi)); 7601 if (doi.doi_type & DMU_OT_NEWTYPE) { 7602 dmu_object_byteswap_t bswap = 7603 DMU_OT_BYTESWAP(doi.doi_type); 7604 name = dmu_ot_byteswap[bswap].ob_name; 7605 } else { 7606 name = dmu_ot[doi.doi_type].ot_name; 7607 } 7608 7609 (void) printf("MOS object %llu (%s) leaked\n", 7610 (u_longlong_t)object, name); 7611 rv = 2; 7612 } 7613 } 7614 (void) range_tree_walk(mos_refd_objs, mos_leaks_cb, NULL); 7615 if (!range_tree_is_empty(mos_refd_objs)) 7616 rv = 2; 7617 range_tree_vacate(mos_refd_objs, NULL, NULL); 7618 range_tree_destroy(mos_refd_objs); 7619 return (rv); 7620 } 7621 7622 typedef struct log_sm_obsolete_stats_arg { 7623 uint64_t lsos_current_txg; 7624 7625 uint64_t lsos_total_entries; 7626 uint64_t lsos_valid_entries; 7627 7628 uint64_t lsos_sm_entries; 7629 uint64_t lsos_valid_sm_entries; 7630 } log_sm_obsolete_stats_arg_t; 7631 7632 static int 7633 log_spacemap_obsolete_stats_cb(spa_t *spa, space_map_entry_t *sme, 7634 uint64_t txg, void *arg) 7635 { 7636 log_sm_obsolete_stats_arg_t *lsos = arg; 7637 7638 uint64_t offset = sme->sme_offset; 7639 uint64_t vdev_id = sme->sme_vdev; 7640 7641 if (lsos->lsos_current_txg == 0) { 7642 /* this is the first log */ 7643 lsos->lsos_current_txg = txg; 7644 } else if (lsos->lsos_current_txg < txg) { 7645 /* we just changed log - print stats and reset */ 7646 (void) printf("%-8llu valid entries out of %-8llu - txg %llu\n", 7647 (u_longlong_t)lsos->lsos_valid_sm_entries, 7648 (u_longlong_t)lsos->lsos_sm_entries, 7649 (u_longlong_t)lsos->lsos_current_txg); 7650 lsos->lsos_valid_sm_entries = 0; 7651 lsos->lsos_sm_entries = 0; 7652 lsos->lsos_current_txg = txg; 7653 } 7654 ASSERT3U(lsos->lsos_current_txg, ==, txg); 7655 7656 lsos->lsos_sm_entries++; 7657 lsos->lsos_total_entries++; 7658 7659 vdev_t *vd = vdev_lookup_top(spa, vdev_id); 7660 if (!vdev_is_concrete(vd)) 7661 return (0); 7662 7663 metaslab_t *ms = vd->vdev_ms[offset >> vd->vdev_ms_shift]; 7664 ASSERT(sme->sme_type == SM_ALLOC || sme->sme_type == SM_FREE); 7665 7666 if (txg < metaslab_unflushed_txg(ms)) 7667 return (0); 7668 lsos->lsos_valid_sm_entries++; 7669 lsos->lsos_valid_entries++; 7670 return (0); 7671 } 7672 7673 static void 7674 dump_log_spacemap_obsolete_stats(spa_t *spa) 7675 { 7676 if (!spa_feature_is_active(spa, SPA_FEATURE_LOG_SPACEMAP)) 7677 return; 7678 7679 log_sm_obsolete_stats_arg_t lsos = {0}; 7680 7681 (void) printf("Log Space Map Obsolete Entry Statistics:\n"); 7682 7683 iterate_through_spacemap_logs(spa, 7684 log_spacemap_obsolete_stats_cb, &lsos); 7685 7686 /* print stats for latest log */ 7687 (void) printf("%-8llu valid entries out of %-8llu - txg %llu\n", 7688 (u_longlong_t)lsos.lsos_valid_sm_entries, 7689 (u_longlong_t)lsos.lsos_sm_entries, 7690 (u_longlong_t)lsos.lsos_current_txg); 7691 7692 (void) printf("%-8llu valid entries out of %-8llu - total\n\n", 7693 (u_longlong_t)lsos.lsos_valid_entries, 7694 (u_longlong_t)lsos.lsos_total_entries); 7695 } 7696 7697 static void 7698 dump_zpool(spa_t *spa) 7699 { 7700 dsl_pool_t *dp = spa_get_dsl(spa); 7701 int rc = 0; 7702 7703 if (dump_opt['y']) { 7704 livelist_metaslab_validate(spa); 7705 } 7706 7707 if (dump_opt['S']) { 7708 dump_simulated_ddt(spa); 7709 return; 7710 } 7711 7712 if (!dump_opt['e'] && dump_opt['C'] > 1) { 7713 (void) printf("\nCached configuration:\n"); 7714 dump_nvlist(spa->spa_config, 8); 7715 } 7716 7717 if (dump_opt['C']) 7718 dump_config(spa); 7719 7720 if (dump_opt['u']) 7721 dump_uberblock(&spa->spa_uberblock, "\nUberblock:\n", "\n"); 7722 7723 if (dump_opt['D']) 7724 dump_all_ddts(spa); 7725 7726 if (dump_opt['d'] > 2 || dump_opt['m']) 7727 dump_metaslabs(spa); 7728 if (dump_opt['M']) 7729 dump_metaslab_groups(spa, dump_opt['M'] > 1); 7730 if (dump_opt['d'] > 2 || dump_opt['m']) { 7731 dump_log_spacemaps(spa); 7732 dump_log_spacemap_obsolete_stats(spa); 7733 } 7734 7735 if (dump_opt['d'] || dump_opt['i']) { 7736 spa_feature_t f; 7737 mos_refd_objs = range_tree_create(NULL, RANGE_SEG64, NULL, 0, 7738 0); 7739 dump_objset(dp->dp_meta_objset); 7740 7741 if (dump_opt['d'] >= 3) { 7742 dsl_pool_t *dp = spa->spa_dsl_pool; 7743 dump_full_bpobj(&spa->spa_deferred_bpobj, 7744 "Deferred frees", 0); 7745 if (spa_version(spa) >= SPA_VERSION_DEADLISTS) { 7746 dump_full_bpobj(&dp->dp_free_bpobj, 7747 "Pool snapshot frees", 0); 7748 } 7749 if (bpobj_is_open(&dp->dp_obsolete_bpobj)) { 7750 ASSERT(spa_feature_is_enabled(spa, 7751 SPA_FEATURE_DEVICE_REMOVAL)); 7752 dump_full_bpobj(&dp->dp_obsolete_bpobj, 7753 "Pool obsolete blocks", 0); 7754 } 7755 7756 if (spa_feature_is_active(spa, 7757 SPA_FEATURE_ASYNC_DESTROY)) { 7758 dump_bptree(spa->spa_meta_objset, 7759 dp->dp_bptree_obj, 7760 "Pool dataset frees"); 7761 } 7762 dump_dtl(spa->spa_root_vdev, 0); 7763 } 7764 7765 for (spa_feature_t f = 0; f < SPA_FEATURES; f++) 7766 global_feature_count[f] = UINT64_MAX; 7767 global_feature_count[SPA_FEATURE_REDACTION_BOOKMARKS] = 0; 7768 global_feature_count[SPA_FEATURE_BOOKMARK_WRITTEN] = 0; 7769 global_feature_count[SPA_FEATURE_LIVELIST] = 0; 7770 7771 (void) dmu_objset_find(spa_name(spa), dump_one_objset, 7772 NULL, DS_FIND_SNAPSHOTS | DS_FIND_CHILDREN); 7773 7774 if (rc == 0 && !dump_opt['L']) 7775 rc = dump_mos_leaks(spa); 7776 7777 for (f = 0; f < SPA_FEATURES; f++) { 7778 uint64_t refcount; 7779 7780 uint64_t *arr; 7781 if (!(spa_feature_table[f].fi_flags & 7782 ZFEATURE_FLAG_PER_DATASET)) { 7783 if (global_feature_count[f] == UINT64_MAX) 7784 continue; 7785 if (!spa_feature_is_enabled(spa, f)) { 7786 ASSERT0(global_feature_count[f]); 7787 continue; 7788 } 7789 arr = global_feature_count; 7790 } else { 7791 if (!spa_feature_is_enabled(spa, f)) { 7792 ASSERT0(dataset_feature_count[f]); 7793 continue; 7794 } 7795 arr = dataset_feature_count; 7796 } 7797 if (feature_get_refcount(spa, &spa_feature_table[f], 7798 &refcount) == ENOTSUP) 7799 continue; 7800 if (arr[f] != refcount) { 7801 (void) printf("%s feature refcount mismatch: " 7802 "%lld consumers != %lld refcount\n", 7803 spa_feature_table[f].fi_uname, 7804 (longlong_t)arr[f], (longlong_t)refcount); 7805 rc = 2; 7806 } else { 7807 (void) printf("Verified %s feature refcount " 7808 "of %llu is correct\n", 7809 spa_feature_table[f].fi_uname, 7810 (longlong_t)refcount); 7811 } 7812 } 7813 7814 if (rc == 0) 7815 rc = verify_device_removal_feature_counts(spa); 7816 } 7817 7818 if (rc == 0 && (dump_opt['b'] || dump_opt['c'])) 7819 rc = dump_block_stats(spa); 7820 7821 if (rc == 0) 7822 rc = verify_spacemap_refcounts(spa); 7823 7824 if (dump_opt['s']) 7825 show_pool_stats(spa); 7826 7827 if (dump_opt['h']) 7828 dump_history(spa); 7829 7830 if (rc == 0) 7831 rc = verify_checkpoint(spa); 7832 7833 if (rc != 0) { 7834 dump_debug_buffer(); 7835 exit(rc); 7836 } 7837 } 7838 7839 #define ZDB_FLAG_CHECKSUM 0x0001 7840 #define ZDB_FLAG_DECOMPRESS 0x0002 7841 #define ZDB_FLAG_BSWAP 0x0004 7842 #define ZDB_FLAG_GBH 0x0008 7843 #define ZDB_FLAG_INDIRECT 0x0010 7844 #define ZDB_FLAG_RAW 0x0020 7845 #define ZDB_FLAG_PRINT_BLKPTR 0x0040 7846 #define ZDB_FLAG_VERBOSE 0x0080 7847 7848 static int flagbits[256]; 7849 static char flagbitstr[16]; 7850 7851 static void 7852 zdb_print_blkptr(const blkptr_t *bp, int flags) 7853 { 7854 char blkbuf[BP_SPRINTF_LEN]; 7855 7856 if (flags & ZDB_FLAG_BSWAP) 7857 byteswap_uint64_array((void *)bp, sizeof (blkptr_t)); 7858 7859 snprintf_blkptr(blkbuf, sizeof (blkbuf), bp); 7860 (void) printf("%s\n", blkbuf); 7861 } 7862 7863 static void 7864 zdb_dump_indirect(blkptr_t *bp, int nbps, int flags) 7865 { 7866 int i; 7867 7868 for (i = 0; i < nbps; i++) 7869 zdb_print_blkptr(&bp[i], flags); 7870 } 7871 7872 static void 7873 zdb_dump_gbh(void *buf, int flags) 7874 { 7875 zdb_dump_indirect((blkptr_t *)buf, SPA_GBH_NBLKPTRS, flags); 7876 } 7877 7878 static void 7879 zdb_dump_block_raw(void *buf, uint64_t size, int flags) 7880 { 7881 if (flags & ZDB_FLAG_BSWAP) 7882 byteswap_uint64_array(buf, size); 7883 VERIFY(write(fileno(stdout), buf, size) == size); 7884 } 7885 7886 static void 7887 zdb_dump_block(char *label, void *buf, uint64_t size, int flags) 7888 { 7889 uint64_t *d = (uint64_t *)buf; 7890 unsigned nwords = size / sizeof (uint64_t); 7891 int do_bswap = !!(flags & ZDB_FLAG_BSWAP); 7892 unsigned i, j; 7893 const char *hdr; 7894 char *c; 7895 7896 7897 if (do_bswap) 7898 hdr = " 7 6 5 4 3 2 1 0 f e d c b a 9 8"; 7899 else 7900 hdr = " 0 1 2 3 4 5 6 7 8 9 a b c d e f"; 7901 7902 (void) printf("\n%s\n%6s %s 0123456789abcdef\n", label, "", hdr); 7903 7904 #ifdef _LITTLE_ENDIAN 7905 /* correct the endianness */ 7906 do_bswap = !do_bswap; 7907 #endif 7908 for (i = 0; i < nwords; i += 2) { 7909 (void) printf("%06llx: %016llx %016llx ", 7910 (u_longlong_t)(i * sizeof (uint64_t)), 7911 (u_longlong_t)(do_bswap ? BSWAP_64(d[i]) : d[i]), 7912 (u_longlong_t)(do_bswap ? BSWAP_64(d[i + 1]) : d[i + 1])); 7913 7914 c = (char *)&d[i]; 7915 for (j = 0; j < 2 * sizeof (uint64_t); j++) 7916 (void) printf("%c", isprint(c[j]) ? c[j] : '.'); 7917 (void) printf("\n"); 7918 } 7919 } 7920 7921 /* 7922 * There are two acceptable formats: 7923 * leaf_name - For example: c1t0d0 or /tmp/ztest.0a 7924 * child[.child]* - For example: 0.1.1 7925 * 7926 * The second form can be used to specify arbitrary vdevs anywhere 7927 * in the hierarchy. For example, in a pool with a mirror of 7928 * RAID-Zs, you can specify either RAID-Z vdev with 0.0 or 0.1 . 7929 */ 7930 static vdev_t * 7931 zdb_vdev_lookup(vdev_t *vdev, const char *path) 7932 { 7933 char *s, *p, *q; 7934 unsigned i; 7935 7936 if (vdev == NULL) 7937 return (NULL); 7938 7939 /* First, assume the x.x.x.x format */ 7940 i = strtoul(path, &s, 10); 7941 if (s == path || (s && *s != '.' && *s != '\0')) 7942 goto name; 7943 if (i >= vdev->vdev_children) 7944 return (NULL); 7945 7946 vdev = vdev->vdev_child[i]; 7947 if (s && *s == '\0') 7948 return (vdev); 7949 return (zdb_vdev_lookup(vdev, s+1)); 7950 7951 name: 7952 for (i = 0; i < vdev->vdev_children; i++) { 7953 vdev_t *vc = vdev->vdev_child[i]; 7954 7955 if (vc->vdev_path == NULL) { 7956 vc = zdb_vdev_lookup(vc, path); 7957 if (vc == NULL) 7958 continue; 7959 else 7960 return (vc); 7961 } 7962 7963 p = strrchr(vc->vdev_path, '/'); 7964 p = p ? p + 1 : vc->vdev_path; 7965 q = &vc->vdev_path[strlen(vc->vdev_path) - 2]; 7966 7967 if (strcmp(vc->vdev_path, path) == 0) 7968 return (vc); 7969 if (strcmp(p, path) == 0) 7970 return (vc); 7971 if (strcmp(q, "s0") == 0 && strncmp(p, path, q - p) == 0) 7972 return (vc); 7973 } 7974 7975 return (NULL); 7976 } 7977 7978 static int 7979 name_from_objset_id(spa_t *spa, uint64_t objset_id, char *outstr) 7980 { 7981 dsl_dataset_t *ds; 7982 7983 dsl_pool_config_enter(spa->spa_dsl_pool, FTAG); 7984 int error = dsl_dataset_hold_obj(spa->spa_dsl_pool, objset_id, 7985 NULL, &ds); 7986 if (error != 0) { 7987 (void) fprintf(stderr, "failed to hold objset %llu: %s\n", 7988 (u_longlong_t)objset_id, strerror(error)); 7989 dsl_pool_config_exit(spa->spa_dsl_pool, FTAG); 7990 return (error); 7991 } 7992 dsl_dataset_name(ds, outstr); 7993 dsl_dataset_rele(ds, NULL); 7994 dsl_pool_config_exit(spa->spa_dsl_pool, FTAG); 7995 return (0); 7996 } 7997 7998 static boolean_t 7999 zdb_parse_block_sizes(char *sizes, uint64_t *lsize, uint64_t *psize) 8000 { 8001 char *s0, *s1, *tmp = NULL; 8002 8003 if (sizes == NULL) 8004 return (B_FALSE); 8005 8006 s0 = strtok_r(sizes, "/", &tmp); 8007 if (s0 == NULL) 8008 return (B_FALSE); 8009 s1 = strtok_r(NULL, "/", &tmp); 8010 *lsize = strtoull(s0, NULL, 16); 8011 *psize = s1 ? strtoull(s1, NULL, 16) : *lsize; 8012 return (*lsize >= *psize && *psize > 0); 8013 } 8014 8015 #define ZIO_COMPRESS_MASK(alg) (1ULL << (ZIO_COMPRESS_##alg)) 8016 8017 static boolean_t 8018 zdb_decompress_block(abd_t *pabd, void *buf, void *lbuf, uint64_t lsize, 8019 uint64_t psize, int flags) 8020 { 8021 (void) buf; 8022 boolean_t exceeded = B_FALSE; 8023 /* 8024 * We don't know how the data was compressed, so just try 8025 * every decompress function at every inflated blocksize. 8026 */ 8027 void *lbuf2 = umem_alloc(SPA_MAXBLOCKSIZE, UMEM_NOFAIL); 8028 int cfuncs[ZIO_COMPRESS_FUNCTIONS] = { 0 }; 8029 int *cfuncp = cfuncs; 8030 uint64_t maxlsize = SPA_MAXBLOCKSIZE; 8031 uint64_t mask = ZIO_COMPRESS_MASK(ON) | ZIO_COMPRESS_MASK(OFF) | 8032 ZIO_COMPRESS_MASK(INHERIT) | ZIO_COMPRESS_MASK(EMPTY) | 8033 (getenv("ZDB_NO_ZLE") ? ZIO_COMPRESS_MASK(ZLE) : 0); 8034 *cfuncp++ = ZIO_COMPRESS_LZ4; 8035 *cfuncp++ = ZIO_COMPRESS_LZJB; 8036 mask |= ZIO_COMPRESS_MASK(LZ4) | ZIO_COMPRESS_MASK(LZJB); 8037 for (int c = 0; c < ZIO_COMPRESS_FUNCTIONS; c++) 8038 if (((1ULL << c) & mask) == 0) 8039 *cfuncp++ = c; 8040 8041 /* 8042 * On the one hand, with SPA_MAXBLOCKSIZE at 16MB, this 8043 * could take a while and we should let the user know 8044 * we are not stuck. On the other hand, printing progress 8045 * info gets old after a while. User can specify 'v' flag 8046 * to see the progression. 8047 */ 8048 if (lsize == psize) 8049 lsize += SPA_MINBLOCKSIZE; 8050 else 8051 maxlsize = lsize; 8052 for (; lsize <= maxlsize; lsize += SPA_MINBLOCKSIZE) { 8053 for (cfuncp = cfuncs; *cfuncp; cfuncp++) { 8054 if (flags & ZDB_FLAG_VERBOSE) { 8055 (void) fprintf(stderr, 8056 "Trying %05llx -> %05llx (%s)\n", 8057 (u_longlong_t)psize, 8058 (u_longlong_t)lsize, 8059 zio_compress_table[*cfuncp].\ 8060 ci_name); 8061 } 8062 8063 /* 8064 * We randomize lbuf2, and decompress to both 8065 * lbuf and lbuf2. This way, we will know if 8066 * decompression fill exactly to lsize. 8067 */ 8068 VERIFY0(random_get_pseudo_bytes(lbuf2, lsize)); 8069 8070 if (zio_decompress_data(*cfuncp, pabd, 8071 lbuf, psize, lsize, NULL) == 0 && 8072 zio_decompress_data(*cfuncp, pabd, 8073 lbuf2, psize, lsize, NULL) == 0 && 8074 memcmp(lbuf, lbuf2, lsize) == 0) 8075 break; 8076 } 8077 if (*cfuncp != 0) 8078 break; 8079 } 8080 umem_free(lbuf2, SPA_MAXBLOCKSIZE); 8081 8082 if (lsize > maxlsize) { 8083 exceeded = B_TRUE; 8084 } 8085 if (*cfuncp == ZIO_COMPRESS_ZLE) { 8086 printf("\nZLE decompression was selected. If you " 8087 "suspect the results are wrong,\ntry avoiding ZLE " 8088 "by setting and exporting ZDB_NO_ZLE=\"true\"\n"); 8089 } 8090 8091 return (exceeded); 8092 } 8093 8094 /* 8095 * Read a block from a pool and print it out. The syntax of the 8096 * block descriptor is: 8097 * 8098 * pool:vdev_specifier:offset:[lsize/]psize[:flags] 8099 * 8100 * pool - The name of the pool you wish to read from 8101 * vdev_specifier - Which vdev (see comment for zdb_vdev_lookup) 8102 * offset - offset, in hex, in bytes 8103 * size - Amount of data to read, in hex, in bytes 8104 * flags - A string of characters specifying options 8105 * b: Decode a blkptr at given offset within block 8106 * c: Calculate and display checksums 8107 * d: Decompress data before dumping 8108 * e: Byteswap data before dumping 8109 * g: Display data as a gang block header 8110 * i: Display as an indirect block 8111 * r: Dump raw data to stdout 8112 * v: Verbose 8113 * 8114 */ 8115 static void 8116 zdb_read_block(char *thing, spa_t *spa) 8117 { 8118 blkptr_t blk, *bp = &blk; 8119 dva_t *dva = bp->blk_dva; 8120 int flags = 0; 8121 uint64_t offset = 0, psize = 0, lsize = 0, blkptr_offset = 0; 8122 zio_t *zio; 8123 vdev_t *vd; 8124 abd_t *pabd; 8125 void *lbuf, *buf; 8126 char *s, *p, *dup, *flagstr, *sizes, *tmp = NULL; 8127 const char *vdev, *errmsg = NULL; 8128 int i, error; 8129 boolean_t borrowed = B_FALSE, found = B_FALSE; 8130 8131 dup = strdup(thing); 8132 s = strtok_r(dup, ":", &tmp); 8133 vdev = s ?: ""; 8134 s = strtok_r(NULL, ":", &tmp); 8135 offset = strtoull(s ? s : "", NULL, 16); 8136 sizes = strtok_r(NULL, ":", &tmp); 8137 s = strtok_r(NULL, ":", &tmp); 8138 flagstr = strdup(s ?: ""); 8139 8140 if (!zdb_parse_block_sizes(sizes, &lsize, &psize)) 8141 errmsg = "invalid size(s)"; 8142 if (!IS_P2ALIGNED(psize, DEV_BSIZE) || !IS_P2ALIGNED(lsize, DEV_BSIZE)) 8143 errmsg = "size must be a multiple of sector size"; 8144 if (!IS_P2ALIGNED(offset, DEV_BSIZE)) 8145 errmsg = "offset must be a multiple of sector size"; 8146 if (errmsg) { 8147 (void) printf("Invalid block specifier: %s - %s\n", 8148 thing, errmsg); 8149 goto done; 8150 } 8151 8152 tmp = NULL; 8153 for (s = strtok_r(flagstr, ":", &tmp); 8154 s != NULL; 8155 s = strtok_r(NULL, ":", &tmp)) { 8156 for (i = 0; i < strlen(flagstr); i++) { 8157 int bit = flagbits[(uchar_t)flagstr[i]]; 8158 8159 if (bit == 0) { 8160 (void) printf("***Ignoring flag: %c\n", 8161 (uchar_t)flagstr[i]); 8162 continue; 8163 } 8164 found = B_TRUE; 8165 flags |= bit; 8166 8167 p = &flagstr[i + 1]; 8168 if (*p != ':' && *p != '\0') { 8169 int j = 0, nextbit = flagbits[(uchar_t)*p]; 8170 char *end, offstr[8] = { 0 }; 8171 if ((bit == ZDB_FLAG_PRINT_BLKPTR) && 8172 (nextbit == 0)) { 8173 /* look ahead to isolate the offset */ 8174 while (nextbit == 0 && 8175 strchr(flagbitstr, *p) == NULL) { 8176 offstr[j] = *p; 8177 j++; 8178 if (i + j > strlen(flagstr)) 8179 break; 8180 p++; 8181 nextbit = flagbits[(uchar_t)*p]; 8182 } 8183 blkptr_offset = strtoull(offstr, &end, 8184 16); 8185 i += j; 8186 } else if (nextbit == 0) { 8187 (void) printf("***Ignoring flag arg:" 8188 " '%c'\n", (uchar_t)*p); 8189 } 8190 } 8191 } 8192 } 8193 if (blkptr_offset % sizeof (blkptr_t)) { 8194 printf("Block pointer offset 0x%llx " 8195 "must be divisible by 0x%x\n", 8196 (longlong_t)blkptr_offset, (int)sizeof (blkptr_t)); 8197 goto done; 8198 } 8199 if (found == B_FALSE && strlen(flagstr) > 0) { 8200 printf("Invalid flag arg: '%s'\n", flagstr); 8201 goto done; 8202 } 8203 8204 vd = zdb_vdev_lookup(spa->spa_root_vdev, vdev); 8205 if (vd == NULL) { 8206 (void) printf("***Invalid vdev: %s\n", vdev); 8207 goto done; 8208 } else { 8209 if (vd->vdev_path) 8210 (void) fprintf(stderr, "Found vdev: %s\n", 8211 vd->vdev_path); 8212 else 8213 (void) fprintf(stderr, "Found vdev type: %s\n", 8214 vd->vdev_ops->vdev_op_type); 8215 } 8216 8217 pabd = abd_alloc_for_io(SPA_MAXBLOCKSIZE, B_FALSE); 8218 lbuf = umem_alloc(SPA_MAXBLOCKSIZE, UMEM_NOFAIL); 8219 8220 BP_ZERO(bp); 8221 8222 DVA_SET_VDEV(&dva[0], vd->vdev_id); 8223 DVA_SET_OFFSET(&dva[0], offset); 8224 DVA_SET_GANG(&dva[0], !!(flags & ZDB_FLAG_GBH)); 8225 DVA_SET_ASIZE(&dva[0], vdev_psize_to_asize(vd, psize)); 8226 8227 BP_SET_BIRTH(bp, TXG_INITIAL, TXG_INITIAL); 8228 8229 BP_SET_LSIZE(bp, lsize); 8230 BP_SET_PSIZE(bp, psize); 8231 BP_SET_COMPRESS(bp, ZIO_COMPRESS_OFF); 8232 BP_SET_CHECKSUM(bp, ZIO_CHECKSUM_OFF); 8233 BP_SET_TYPE(bp, DMU_OT_NONE); 8234 BP_SET_LEVEL(bp, 0); 8235 BP_SET_DEDUP(bp, 0); 8236 BP_SET_BYTEORDER(bp, ZFS_HOST_BYTEORDER); 8237 8238 spa_config_enter(spa, SCL_STATE, FTAG, RW_READER); 8239 zio = zio_root(spa, NULL, NULL, 0); 8240 8241 if (vd == vd->vdev_top) { 8242 /* 8243 * Treat this as a normal block read. 8244 */ 8245 zio_nowait(zio_read(zio, spa, bp, pabd, psize, NULL, NULL, 8246 ZIO_PRIORITY_SYNC_READ, 8247 ZIO_FLAG_CANFAIL | ZIO_FLAG_RAW, NULL)); 8248 } else { 8249 /* 8250 * Treat this as a vdev child I/O. 8251 */ 8252 zio_nowait(zio_vdev_child_io(zio, bp, vd, offset, pabd, 8253 psize, ZIO_TYPE_READ, ZIO_PRIORITY_SYNC_READ, 8254 ZIO_FLAG_DONT_CACHE | ZIO_FLAG_DONT_PROPAGATE | 8255 ZIO_FLAG_DONT_RETRY | ZIO_FLAG_CANFAIL | ZIO_FLAG_RAW | 8256 ZIO_FLAG_OPTIONAL, NULL, NULL)); 8257 } 8258 8259 error = zio_wait(zio); 8260 spa_config_exit(spa, SCL_STATE, FTAG); 8261 8262 if (error) { 8263 (void) printf("Read of %s failed, error: %d\n", thing, error); 8264 goto out; 8265 } 8266 8267 uint64_t orig_lsize = lsize; 8268 buf = lbuf; 8269 if (flags & ZDB_FLAG_DECOMPRESS) { 8270 boolean_t failed = zdb_decompress_block(pabd, buf, lbuf, 8271 lsize, psize, flags); 8272 if (failed) { 8273 (void) printf("Decompress of %s failed\n", thing); 8274 goto out; 8275 } 8276 } else { 8277 buf = abd_borrow_buf_copy(pabd, lsize); 8278 borrowed = B_TRUE; 8279 } 8280 /* 8281 * Try to detect invalid block pointer. If invalid, try 8282 * decompressing. 8283 */ 8284 if ((flags & ZDB_FLAG_PRINT_BLKPTR || flags & ZDB_FLAG_INDIRECT) && 8285 !(flags & ZDB_FLAG_DECOMPRESS)) { 8286 const blkptr_t *b = (const blkptr_t *)(void *) 8287 ((uintptr_t)buf + (uintptr_t)blkptr_offset); 8288 if (zfs_blkptr_verify(spa, b, B_FALSE, BLK_VERIFY_ONLY) == 8289 B_FALSE) { 8290 abd_return_buf_copy(pabd, buf, lsize); 8291 borrowed = B_FALSE; 8292 buf = lbuf; 8293 boolean_t failed = zdb_decompress_block(pabd, buf, 8294 lbuf, lsize, psize, flags); 8295 b = (const blkptr_t *)(void *) 8296 ((uintptr_t)buf + (uintptr_t)blkptr_offset); 8297 if (failed || zfs_blkptr_verify(spa, b, B_FALSE, 8298 BLK_VERIFY_LOG) == B_FALSE) { 8299 printf("invalid block pointer at this DVA\n"); 8300 goto out; 8301 } 8302 } 8303 } 8304 8305 if (flags & ZDB_FLAG_PRINT_BLKPTR) 8306 zdb_print_blkptr((blkptr_t *)(void *) 8307 ((uintptr_t)buf + (uintptr_t)blkptr_offset), flags); 8308 else if (flags & ZDB_FLAG_RAW) 8309 zdb_dump_block_raw(buf, lsize, flags); 8310 else if (flags & ZDB_FLAG_INDIRECT) 8311 zdb_dump_indirect((blkptr_t *)buf, 8312 orig_lsize / sizeof (blkptr_t), flags); 8313 else if (flags & ZDB_FLAG_GBH) 8314 zdb_dump_gbh(buf, flags); 8315 else 8316 zdb_dump_block(thing, buf, lsize, flags); 8317 8318 /* 8319 * If :c was specified, iterate through the checksum table to 8320 * calculate and display each checksum for our specified 8321 * DVA and length. 8322 */ 8323 if ((flags & ZDB_FLAG_CHECKSUM) && !(flags & ZDB_FLAG_RAW) && 8324 !(flags & ZDB_FLAG_GBH)) { 8325 zio_t *czio; 8326 (void) printf("\n"); 8327 for (enum zio_checksum ck = ZIO_CHECKSUM_LABEL; 8328 ck < ZIO_CHECKSUM_FUNCTIONS; ck++) { 8329 8330 if ((zio_checksum_table[ck].ci_flags & 8331 ZCHECKSUM_FLAG_EMBEDDED) || 8332 ck == ZIO_CHECKSUM_NOPARITY) { 8333 continue; 8334 } 8335 BP_SET_CHECKSUM(bp, ck); 8336 spa_config_enter(spa, SCL_STATE, FTAG, RW_READER); 8337 czio = zio_root(spa, NULL, NULL, ZIO_FLAG_CANFAIL); 8338 czio->io_bp = bp; 8339 8340 if (vd == vd->vdev_top) { 8341 zio_nowait(zio_read(czio, spa, bp, pabd, psize, 8342 NULL, NULL, 8343 ZIO_PRIORITY_SYNC_READ, 8344 ZIO_FLAG_CANFAIL | ZIO_FLAG_RAW | 8345 ZIO_FLAG_DONT_RETRY, NULL)); 8346 } else { 8347 zio_nowait(zio_vdev_child_io(czio, bp, vd, 8348 offset, pabd, psize, ZIO_TYPE_READ, 8349 ZIO_PRIORITY_SYNC_READ, 8350 ZIO_FLAG_DONT_CACHE | 8351 ZIO_FLAG_DONT_PROPAGATE | 8352 ZIO_FLAG_DONT_RETRY | 8353 ZIO_FLAG_CANFAIL | ZIO_FLAG_RAW | 8354 ZIO_FLAG_SPECULATIVE | 8355 ZIO_FLAG_OPTIONAL, NULL, NULL)); 8356 } 8357 error = zio_wait(czio); 8358 if (error == 0 || error == ECKSUM) { 8359 zio_t *ck_zio = zio_root(spa, NULL, NULL, 0); 8360 ck_zio->io_offset = 8361 DVA_GET_OFFSET(&bp->blk_dva[0]); 8362 ck_zio->io_bp = bp; 8363 zio_checksum_compute(ck_zio, ck, pabd, lsize); 8364 printf("%12s\tcksum=%llx:%llx:%llx:%llx\n", 8365 zio_checksum_table[ck].ci_name, 8366 (u_longlong_t)bp->blk_cksum.zc_word[0], 8367 (u_longlong_t)bp->blk_cksum.zc_word[1], 8368 (u_longlong_t)bp->blk_cksum.zc_word[2], 8369 (u_longlong_t)bp->blk_cksum.zc_word[3]); 8370 zio_wait(ck_zio); 8371 } else { 8372 printf("error %d reading block\n", error); 8373 } 8374 spa_config_exit(spa, SCL_STATE, FTAG); 8375 } 8376 } 8377 8378 if (borrowed) 8379 abd_return_buf_copy(pabd, buf, lsize); 8380 8381 out: 8382 abd_free(pabd); 8383 umem_free(lbuf, SPA_MAXBLOCKSIZE); 8384 done: 8385 free(flagstr); 8386 free(dup); 8387 } 8388 8389 static void 8390 zdb_embedded_block(char *thing) 8391 { 8392 blkptr_t bp = {{{{0}}}}; 8393 unsigned long long *words = (void *)&bp; 8394 char *buf; 8395 int err; 8396 8397 err = sscanf(thing, "%llx:%llx:%llx:%llx:%llx:%llx:%llx:%llx:" 8398 "%llx:%llx:%llx:%llx:%llx:%llx:%llx:%llx", 8399 words + 0, words + 1, words + 2, words + 3, 8400 words + 4, words + 5, words + 6, words + 7, 8401 words + 8, words + 9, words + 10, words + 11, 8402 words + 12, words + 13, words + 14, words + 15); 8403 if (err != 16) { 8404 (void) fprintf(stderr, "invalid input format\n"); 8405 exit(1); 8406 } 8407 ASSERT3U(BPE_GET_LSIZE(&bp), <=, SPA_MAXBLOCKSIZE); 8408 buf = malloc(SPA_MAXBLOCKSIZE); 8409 if (buf == NULL) { 8410 (void) fprintf(stderr, "out of memory\n"); 8411 exit(1); 8412 } 8413 err = decode_embedded_bp(&bp, buf, BPE_GET_LSIZE(&bp)); 8414 if (err != 0) { 8415 (void) fprintf(stderr, "decode failed: %u\n", err); 8416 exit(1); 8417 } 8418 zdb_dump_block_raw(buf, BPE_GET_LSIZE(&bp), 0); 8419 free(buf); 8420 } 8421 8422 /* check for valid hex or decimal numeric string */ 8423 static boolean_t 8424 zdb_numeric(char *str) 8425 { 8426 int i = 0; 8427 8428 if (strlen(str) == 0) 8429 return (B_FALSE); 8430 if (strncmp(str, "0x", 2) == 0 || strncmp(str, "0X", 2) == 0) 8431 i = 2; 8432 for (; i < strlen(str); i++) { 8433 if (!isxdigit(str[i])) 8434 return (B_FALSE); 8435 } 8436 return (B_TRUE); 8437 } 8438 8439 int 8440 main(int argc, char **argv) 8441 { 8442 int c; 8443 spa_t *spa = NULL; 8444 objset_t *os = NULL; 8445 int dump_all = 1; 8446 int verbose = 0; 8447 int error = 0; 8448 char **searchdirs = NULL; 8449 int nsearch = 0; 8450 char *target, *target_pool, dsname[ZFS_MAX_DATASET_NAME_LEN]; 8451 nvlist_t *policy = NULL; 8452 uint64_t max_txg = UINT64_MAX; 8453 int64_t objset_id = -1; 8454 uint64_t object; 8455 int flags = ZFS_IMPORT_MISSING_LOG; 8456 int rewind = ZPOOL_NEVER_REWIND; 8457 char *spa_config_path_env, *objset_str; 8458 boolean_t target_is_spa = B_TRUE, dataset_lookup = B_FALSE; 8459 nvlist_t *cfg = NULL; 8460 8461 dprintf_setup(&argc, argv); 8462 8463 /* 8464 * If there is an environment variable SPA_CONFIG_PATH it overrides 8465 * default spa_config_path setting. If -U flag is specified it will 8466 * override this environment variable settings once again. 8467 */ 8468 spa_config_path_env = getenv("SPA_CONFIG_PATH"); 8469 if (spa_config_path_env != NULL) 8470 spa_config_path = spa_config_path_env; 8471 8472 /* 8473 * For performance reasons, we set this tunable down. We do so before 8474 * the arg parsing section so that the user can override this value if 8475 * they choose. 8476 */ 8477 zfs_btree_verify_intensity = 3; 8478 8479 struct option long_options[] = { 8480 {"ignore-assertions", no_argument, NULL, 'A'}, 8481 {"block-stats", no_argument, NULL, 'b'}, 8482 {"checksum", no_argument, NULL, 'c'}, 8483 {"config", no_argument, NULL, 'C'}, 8484 {"datasets", no_argument, NULL, 'd'}, 8485 {"dedup-stats", no_argument, NULL, 'D'}, 8486 {"exported", no_argument, NULL, 'e'}, 8487 {"embedded-block-pointer", no_argument, NULL, 'E'}, 8488 {"automatic-rewind", no_argument, NULL, 'F'}, 8489 {"dump-debug-msg", no_argument, NULL, 'G'}, 8490 {"history", no_argument, NULL, 'h'}, 8491 {"intent-logs", no_argument, NULL, 'i'}, 8492 {"inflight", required_argument, NULL, 'I'}, 8493 {"checkpointed-state", no_argument, NULL, 'k'}, 8494 {"label", no_argument, NULL, 'l'}, 8495 {"disable-leak-tracking", no_argument, NULL, 'L'}, 8496 {"metaslabs", no_argument, NULL, 'm'}, 8497 {"metaslab-groups", no_argument, NULL, 'M'}, 8498 {"numeric", no_argument, NULL, 'N'}, 8499 {"option", required_argument, NULL, 'o'}, 8500 {"object-lookups", no_argument, NULL, 'O'}, 8501 {"path", required_argument, NULL, 'p'}, 8502 {"parseable", no_argument, NULL, 'P'}, 8503 {"skip-label", no_argument, NULL, 'q'}, 8504 {"copy-object", no_argument, NULL, 'r'}, 8505 {"read-block", no_argument, NULL, 'R'}, 8506 {"io-stats", no_argument, NULL, 's'}, 8507 {"simulate-dedup", no_argument, NULL, 'S'}, 8508 {"txg", required_argument, NULL, 't'}, 8509 {"uberblock", no_argument, NULL, 'u'}, 8510 {"cachefile", required_argument, NULL, 'U'}, 8511 {"verbose", no_argument, NULL, 'v'}, 8512 {"verbatim", no_argument, NULL, 'V'}, 8513 {"dump-blocks", required_argument, NULL, 'x'}, 8514 {"extreme-rewind", no_argument, NULL, 'X'}, 8515 {"all-reconstruction", no_argument, NULL, 'Y'}, 8516 {"livelist", no_argument, NULL, 'y'}, 8517 {"zstd-headers", no_argument, NULL, 'Z'}, 8518 {0, 0, 0, 0} 8519 }; 8520 8521 while ((c = getopt_long(argc, argv, 8522 "AbcCdDeEFGhiI:klLmMNo:Op:PqrRsSt:uU:vVx:XYyZ", 8523 long_options, NULL)) != -1) { 8524 switch (c) { 8525 case 'b': 8526 case 'c': 8527 case 'C': 8528 case 'd': 8529 case 'D': 8530 case 'E': 8531 case 'G': 8532 case 'h': 8533 case 'i': 8534 case 'l': 8535 case 'm': 8536 case 'M': 8537 case 'N': 8538 case 'O': 8539 case 'r': 8540 case 'R': 8541 case 's': 8542 case 'S': 8543 case 'u': 8544 case 'y': 8545 case 'Z': 8546 dump_opt[c]++; 8547 dump_all = 0; 8548 break; 8549 case 'A': 8550 case 'e': 8551 case 'F': 8552 case 'k': 8553 case 'L': 8554 case 'P': 8555 case 'q': 8556 case 'X': 8557 dump_opt[c]++; 8558 break; 8559 case 'Y': 8560 zfs_reconstruct_indirect_combinations_max = INT_MAX; 8561 zfs_deadman_enabled = 0; 8562 break; 8563 /* NB: Sort single match options below. */ 8564 case 'I': 8565 max_inflight_bytes = strtoull(optarg, NULL, 0); 8566 if (max_inflight_bytes == 0) { 8567 (void) fprintf(stderr, "maximum number " 8568 "of inflight bytes must be greater " 8569 "than 0\n"); 8570 usage(); 8571 } 8572 break; 8573 case 'o': 8574 error = set_global_var(optarg); 8575 if (error != 0) 8576 usage(); 8577 break; 8578 case 'p': 8579 if (searchdirs == NULL) { 8580 searchdirs = umem_alloc(sizeof (char *), 8581 UMEM_NOFAIL); 8582 } else { 8583 char **tmp = umem_alloc((nsearch + 1) * 8584 sizeof (char *), UMEM_NOFAIL); 8585 memcpy(tmp, searchdirs, nsearch * 8586 sizeof (char *)); 8587 umem_free(searchdirs, 8588 nsearch * sizeof (char *)); 8589 searchdirs = tmp; 8590 } 8591 searchdirs[nsearch++] = optarg; 8592 break; 8593 case 't': 8594 max_txg = strtoull(optarg, NULL, 0); 8595 if (max_txg < TXG_INITIAL) { 8596 (void) fprintf(stderr, "incorrect txg " 8597 "specified: %s\n", optarg); 8598 usage(); 8599 } 8600 break; 8601 case 'U': 8602 spa_config_path = optarg; 8603 if (spa_config_path[0] != '/') { 8604 (void) fprintf(stderr, 8605 "cachefile must be an absolute path " 8606 "(i.e. start with a slash)\n"); 8607 usage(); 8608 } 8609 break; 8610 case 'v': 8611 verbose++; 8612 break; 8613 case 'V': 8614 flags = ZFS_IMPORT_VERBATIM; 8615 break; 8616 case 'x': 8617 vn_dumpdir = optarg; 8618 break; 8619 default: 8620 usage(); 8621 break; 8622 } 8623 } 8624 8625 if (!dump_opt['e'] && searchdirs != NULL) { 8626 (void) fprintf(stderr, "-p option requires use of -e\n"); 8627 usage(); 8628 } 8629 #if defined(_LP64) 8630 /* 8631 * ZDB does not typically re-read blocks; therefore limit the ARC 8632 * to 256 MB, which can be used entirely for metadata. 8633 */ 8634 zfs_arc_min = zfs_arc_meta_min = 2ULL << SPA_MAXBLOCKSHIFT; 8635 zfs_arc_max = zfs_arc_meta_limit = 256 * 1024 * 1024; 8636 #endif 8637 8638 /* 8639 * "zdb -c" uses checksum-verifying scrub i/os which are async reads. 8640 * "zdb -b" uses traversal prefetch which uses async reads. 8641 * For good performance, let several of them be active at once. 8642 */ 8643 zfs_vdev_async_read_max_active = 10; 8644 8645 /* 8646 * Disable reference tracking for better performance. 8647 */ 8648 reference_tracking_enable = B_FALSE; 8649 8650 /* 8651 * Do not fail spa_load when spa_load_verify fails. This is needed 8652 * to load non-idle pools. 8653 */ 8654 spa_load_verify_dryrun = B_TRUE; 8655 8656 /* 8657 * ZDB should have ability to read spacemaps. 8658 */ 8659 spa_mode_readable_spacemaps = B_TRUE; 8660 8661 kernel_init(SPA_MODE_READ); 8662 8663 if (dump_all) 8664 verbose = MAX(verbose, 1); 8665 8666 for (c = 0; c < 256; c++) { 8667 if (dump_all && strchr("AeEFklLNOPrRSXy", c) == NULL) 8668 dump_opt[c] = 1; 8669 if (dump_opt[c]) 8670 dump_opt[c] += verbose; 8671 } 8672 8673 libspl_set_assert_ok((dump_opt['A'] == 1) || (dump_opt['A'] > 2)); 8674 zfs_recover = (dump_opt['A'] > 1); 8675 8676 argc -= optind; 8677 argv += optind; 8678 if (argc < 2 && dump_opt['R']) 8679 usage(); 8680 8681 if (dump_opt['E']) { 8682 if (argc != 1) 8683 usage(); 8684 zdb_embedded_block(argv[0]); 8685 return (0); 8686 } 8687 8688 if (argc < 1) { 8689 if (!dump_opt['e'] && dump_opt['C']) { 8690 dump_cachefile(spa_config_path); 8691 return (0); 8692 } 8693 usage(); 8694 } 8695 8696 if (dump_opt['l']) 8697 return (dump_label(argv[0])); 8698 8699 if (dump_opt['O']) { 8700 if (argc != 2) 8701 usage(); 8702 dump_opt['v'] = verbose + 3; 8703 return (dump_path(argv[0], argv[1], NULL)); 8704 } 8705 if (dump_opt['r']) { 8706 target_is_spa = B_FALSE; 8707 if (argc != 3) 8708 usage(); 8709 dump_opt['v'] = verbose; 8710 error = dump_path(argv[0], argv[1], &object); 8711 if (error != 0) 8712 fatal("internal error: %s", strerror(error)); 8713 } 8714 8715 if (dump_opt['X'] || dump_opt['F']) 8716 rewind = ZPOOL_DO_REWIND | 8717 (dump_opt['X'] ? ZPOOL_EXTREME_REWIND : 0); 8718 8719 /* -N implies -d */ 8720 if (dump_opt['N'] && dump_opt['d'] == 0) 8721 dump_opt['d'] = dump_opt['N']; 8722 8723 if (nvlist_alloc(&policy, NV_UNIQUE_NAME_TYPE, 0) != 0 || 8724 nvlist_add_uint64(policy, ZPOOL_LOAD_REQUEST_TXG, max_txg) != 0 || 8725 nvlist_add_uint32(policy, ZPOOL_LOAD_REWIND_POLICY, rewind) != 0) 8726 fatal("internal error: %s", strerror(ENOMEM)); 8727 8728 error = 0; 8729 target = argv[0]; 8730 8731 if (strpbrk(target, "/@") != NULL) { 8732 size_t targetlen; 8733 8734 target_pool = strdup(target); 8735 *strpbrk(target_pool, "/@") = '\0'; 8736 8737 target_is_spa = B_FALSE; 8738 targetlen = strlen(target); 8739 if (targetlen && target[targetlen - 1] == '/') 8740 target[targetlen - 1] = '\0'; 8741 8742 /* 8743 * See if an objset ID was supplied (-d <pool>/<objset ID>). 8744 * To disambiguate tank/100, consider the 100 as objsetID 8745 * if -N was given, otherwise 100 is an objsetID iff 8746 * tank/100 as a named dataset fails on lookup. 8747 */ 8748 objset_str = strchr(target, '/'); 8749 if (objset_str && strlen(objset_str) > 1 && 8750 zdb_numeric(objset_str + 1)) { 8751 char *endptr; 8752 errno = 0; 8753 objset_str++; 8754 objset_id = strtoull(objset_str, &endptr, 0); 8755 /* dataset 0 is the same as opening the pool */ 8756 if (errno == 0 && endptr != objset_str && 8757 objset_id != 0) { 8758 if (dump_opt['N']) 8759 dataset_lookup = B_TRUE; 8760 } 8761 /* normal dataset name not an objset ID */ 8762 if (endptr == objset_str) { 8763 objset_id = -1; 8764 } 8765 } else if (objset_str && !zdb_numeric(objset_str + 1) && 8766 dump_opt['N']) { 8767 printf("Supply a numeric objset ID with -N\n"); 8768 exit(1); 8769 } 8770 } else { 8771 target_pool = target; 8772 } 8773 8774 if (dump_opt['e']) { 8775 importargs_t args = { 0 }; 8776 8777 args.paths = nsearch; 8778 args.path = searchdirs; 8779 args.can_be_active = B_TRUE; 8780 8781 error = zpool_find_config(NULL, target_pool, &cfg, &args, 8782 &libzpool_config_ops); 8783 8784 if (error == 0) { 8785 8786 if (nvlist_add_nvlist(cfg, 8787 ZPOOL_LOAD_POLICY, policy) != 0) { 8788 fatal("can't open '%s': %s", 8789 target, strerror(ENOMEM)); 8790 } 8791 8792 if (dump_opt['C'] > 1) { 8793 (void) printf("\nConfiguration for import:\n"); 8794 dump_nvlist(cfg, 8); 8795 } 8796 8797 /* 8798 * Disable the activity check to allow examination of 8799 * active pools. 8800 */ 8801 error = spa_import(target_pool, cfg, NULL, 8802 flags | ZFS_IMPORT_SKIP_MMP); 8803 } 8804 } 8805 8806 if (searchdirs != NULL) { 8807 umem_free(searchdirs, nsearch * sizeof (char *)); 8808 searchdirs = NULL; 8809 } 8810 8811 /* 8812 * import_checkpointed_state makes the assumption that the 8813 * target pool that we pass it is already part of the spa 8814 * namespace. Because of that we need to make sure to call 8815 * it always after the -e option has been processed, which 8816 * imports the pool to the namespace if it's not in the 8817 * cachefile. 8818 */ 8819 char *checkpoint_pool = NULL; 8820 char *checkpoint_target = NULL; 8821 if (dump_opt['k']) { 8822 checkpoint_pool = import_checkpointed_state(target, cfg, 8823 &checkpoint_target); 8824 8825 if (checkpoint_target != NULL) 8826 target = checkpoint_target; 8827 } 8828 8829 if (cfg != NULL) { 8830 nvlist_free(cfg); 8831 cfg = NULL; 8832 } 8833 8834 if (target_pool != target) 8835 free(target_pool); 8836 8837 if (error == 0) { 8838 if (dump_opt['k'] && (target_is_spa || dump_opt['R'])) { 8839 ASSERT(checkpoint_pool != NULL); 8840 ASSERT(checkpoint_target == NULL); 8841 8842 error = spa_open(checkpoint_pool, &spa, FTAG); 8843 if (error != 0) { 8844 fatal("Tried to open pool \"%s\" but " 8845 "spa_open() failed with error %d\n", 8846 checkpoint_pool, error); 8847 } 8848 8849 } else if (target_is_spa || dump_opt['R'] || objset_id == 0) { 8850 zdb_set_skip_mmp(target); 8851 error = spa_open_rewind(target, &spa, FTAG, policy, 8852 NULL); 8853 if (error) { 8854 /* 8855 * If we're missing the log device then 8856 * try opening the pool after clearing the 8857 * log state. 8858 */ 8859 mutex_enter(&spa_namespace_lock); 8860 if ((spa = spa_lookup(target)) != NULL && 8861 spa->spa_log_state == SPA_LOG_MISSING) { 8862 spa->spa_log_state = SPA_LOG_CLEAR; 8863 error = 0; 8864 } 8865 mutex_exit(&spa_namespace_lock); 8866 8867 if (!error) { 8868 error = spa_open_rewind(target, &spa, 8869 FTAG, policy, NULL); 8870 } 8871 } 8872 } else if (strpbrk(target, "#") != NULL) { 8873 dsl_pool_t *dp; 8874 error = dsl_pool_hold(target, FTAG, &dp); 8875 if (error != 0) { 8876 fatal("can't dump '%s': %s", target, 8877 strerror(error)); 8878 } 8879 error = dump_bookmark(dp, target, B_TRUE, verbose > 1); 8880 dsl_pool_rele(dp, FTAG); 8881 if (error != 0) { 8882 fatal("can't dump '%s': %s", target, 8883 strerror(error)); 8884 } 8885 return (error); 8886 } else { 8887 target_pool = strdup(target); 8888 if (strpbrk(target, "/@") != NULL) 8889 *strpbrk(target_pool, "/@") = '\0'; 8890 8891 zdb_set_skip_mmp(target); 8892 /* 8893 * If -N was supplied, the user has indicated that 8894 * zdb -d <pool>/<objsetID> is in effect. Otherwise 8895 * we first assume that the dataset string is the 8896 * dataset name. If dmu_objset_hold fails with the 8897 * dataset string, and we have an objset_id, retry the 8898 * lookup with the objsetID. 8899 */ 8900 boolean_t retry = B_TRUE; 8901 retry_lookup: 8902 if (dataset_lookup == B_TRUE) { 8903 /* 8904 * Use the supplied id to get the name 8905 * for open_objset. 8906 */ 8907 error = spa_open(target_pool, &spa, FTAG); 8908 if (error == 0) { 8909 error = name_from_objset_id(spa, 8910 objset_id, dsname); 8911 spa_close(spa, FTAG); 8912 if (error == 0) 8913 target = dsname; 8914 } 8915 } 8916 if (error == 0) { 8917 if (objset_id > 0 && retry) { 8918 int err = dmu_objset_hold(target, FTAG, 8919 &os); 8920 if (err) { 8921 dataset_lookup = B_TRUE; 8922 retry = B_FALSE; 8923 goto retry_lookup; 8924 } else { 8925 dmu_objset_rele(os, FTAG); 8926 } 8927 } 8928 error = open_objset(target, FTAG, &os); 8929 } 8930 if (error == 0) 8931 spa = dmu_objset_spa(os); 8932 free(target_pool); 8933 } 8934 } 8935 nvlist_free(policy); 8936 8937 if (error) 8938 fatal("can't open '%s': %s", target, strerror(error)); 8939 8940 /* 8941 * Set the pool failure mode to panic in order to prevent the pool 8942 * from suspending. A suspended I/O will have no way to resume and 8943 * can prevent the zdb(8) command from terminating as expected. 8944 */ 8945 if (spa != NULL) 8946 spa->spa_failmode = ZIO_FAILURE_MODE_PANIC; 8947 8948 argv++; 8949 argc--; 8950 if (dump_opt['r']) { 8951 error = zdb_copy_object(os, object, argv[1]); 8952 } else if (!dump_opt['R']) { 8953 flagbits['d'] = ZOR_FLAG_DIRECTORY; 8954 flagbits['f'] = ZOR_FLAG_PLAIN_FILE; 8955 flagbits['m'] = ZOR_FLAG_SPACE_MAP; 8956 flagbits['z'] = ZOR_FLAG_ZAP; 8957 flagbits['A'] = ZOR_FLAG_ALL_TYPES; 8958 8959 if (argc > 0 && dump_opt['d']) { 8960 zopt_object_args = argc; 8961 zopt_object_ranges = calloc(zopt_object_args, 8962 sizeof (zopt_object_range_t)); 8963 for (unsigned i = 0; i < zopt_object_args; i++) { 8964 int err; 8965 const char *msg = NULL; 8966 8967 err = parse_object_range(argv[i], 8968 &zopt_object_ranges[i], &msg); 8969 if (err != 0) 8970 fatal("Bad object or range: '%s': %s\n", 8971 argv[i], msg ?: ""); 8972 } 8973 } else if (argc > 0 && dump_opt['m']) { 8974 zopt_metaslab_args = argc; 8975 zopt_metaslab = calloc(zopt_metaslab_args, 8976 sizeof (uint64_t)); 8977 for (unsigned i = 0; i < zopt_metaslab_args; i++) { 8978 errno = 0; 8979 zopt_metaslab[i] = strtoull(argv[i], NULL, 0); 8980 if (zopt_metaslab[i] == 0 && errno != 0) 8981 fatal("bad number %s: %s", argv[i], 8982 strerror(errno)); 8983 } 8984 } 8985 if (os != NULL) { 8986 dump_objset(os); 8987 } else if (zopt_object_args > 0 && !dump_opt['m']) { 8988 dump_objset(spa->spa_meta_objset); 8989 } else { 8990 dump_zpool(spa); 8991 } 8992 } else { 8993 flagbits['b'] = ZDB_FLAG_PRINT_BLKPTR; 8994 flagbits['c'] = ZDB_FLAG_CHECKSUM; 8995 flagbits['d'] = ZDB_FLAG_DECOMPRESS; 8996 flagbits['e'] = ZDB_FLAG_BSWAP; 8997 flagbits['g'] = ZDB_FLAG_GBH; 8998 flagbits['i'] = ZDB_FLAG_INDIRECT; 8999 flagbits['r'] = ZDB_FLAG_RAW; 9000 flagbits['v'] = ZDB_FLAG_VERBOSE; 9001 9002 for (int i = 0; i < argc; i++) 9003 zdb_read_block(argv[i], spa); 9004 } 9005 9006 if (dump_opt['k']) { 9007 free(checkpoint_pool); 9008 if (!target_is_spa) 9009 free(checkpoint_target); 9010 } 9011 9012 if (os != NULL) { 9013 close_objset(os, FTAG); 9014 } else { 9015 spa_close(spa, FTAG); 9016 } 9017 9018 fuid_table_destroy(); 9019 9020 dump_debug_buffer(); 9021 9022 kernel_fini(); 9023 9024 return (error); 9025 } 9026