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