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