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