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