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