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