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