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, 2018 by Delphix. All rights reserved. 25 * Copyright (c) 2014 Integros [integros.com] 26 * Copyright 2017 Nexenta Systems, Inc. 27 * Copyright (c) 2017, 2018 Lawrence Livermore National Security, LLC. 28 * Copyright 2017 RackTop Systems. 29 */ 30 31 #include <stdio.h> 32 #include <unistd.h> 33 #include <stdio_ext.h> 34 #include <stdlib.h> 35 #include <ctype.h> 36 #include <sys/zfs_context.h> 37 #include <sys/spa.h> 38 #include <sys/spa_impl.h> 39 #include <sys/dmu.h> 40 #include <sys/zap.h> 41 #include <sys/fs/zfs.h> 42 #include <sys/zfs_znode.h> 43 #include <sys/zfs_sa.h> 44 #include <sys/sa.h> 45 #include <sys/sa_impl.h> 46 #include <sys/vdev.h> 47 #include <sys/vdev_impl.h> 48 #include <sys/metaslab_impl.h> 49 #include <sys/dmu_objset.h> 50 #include <sys/dsl_dir.h> 51 #include <sys/dsl_dataset.h> 52 #include <sys/dsl_pool.h> 53 #include <sys/dbuf.h> 54 #include <sys/zil.h> 55 #include <sys/zil_impl.h> 56 #include <sys/stat.h> 57 #include <sys/resource.h> 58 #include <sys/dmu_traverse.h> 59 #include <sys/zio_checksum.h> 60 #include <sys/zio_compress.h> 61 #include <sys/zfs_fuid.h> 62 #include <sys/arc.h> 63 #include <sys/ddt.h> 64 #include <sys/zfeature.h> 65 #include <sys/abd.h> 66 #include <sys/blkptr.h> 67 #include <sys/dsl_scan.h> 68 #include <sys/dsl_crypt.h> 69 #include <zfs_comutil.h> 70 #include <libcmdutils.h> 71 #undef verify 72 #include <libzfs.h> 73 74 #include "zdb.h" 75 76 #define ZDB_COMPRESS_NAME(idx) ((idx) < ZIO_COMPRESS_FUNCTIONS ? \ 77 zio_compress_table[(idx)].ci_name : "UNKNOWN") 78 #define ZDB_CHECKSUM_NAME(idx) ((idx) < ZIO_CHECKSUM_FUNCTIONS ? \ 79 zio_checksum_table[(idx)].ci_name : "UNKNOWN") 80 #define ZDB_OT_NAME(idx) ((idx) < DMU_OT_NUMTYPES ? \ 81 dmu_ot[(idx)].ot_name : DMU_OT_IS_VALID(idx) ? \ 82 dmu_ot_byteswap[DMU_OT_BYTESWAP(idx)].ob_name : "UNKNOWN") 83 #define ZDB_OT_TYPE(idx) ((idx) < DMU_OT_NUMTYPES ? (idx) : \ 84 (idx) == DMU_OTN_ZAP_DATA || (idx) == DMU_OTN_ZAP_METADATA ? \ 85 DMU_OT_ZAP_OTHER : \ 86 (idx) == DMU_OTN_UINT64_DATA || (idx) == DMU_OTN_UINT64_METADATA ? \ 87 DMU_OT_UINT64_OTHER : DMU_OT_NUMTYPES) 88 89 #ifndef lint 90 extern int reference_tracking_enable; 91 extern boolean_t zfs_recover; 92 extern uint64_t zfs_arc_max, zfs_arc_meta_limit; 93 extern int zfs_vdev_async_read_max_active; 94 extern int aok; 95 extern boolean_t spa_load_verify_dryrun; 96 #else 97 int reference_tracking_enable; 98 boolean_t zfs_recover; 99 uint64_t zfs_arc_max, zfs_arc_meta_limit; 100 int zfs_vdev_async_read_max_active; 101 int aok; 102 boolean_t spa_load_verify_dryrun; 103 #endif 104 105 static const char cmdname[] = "zdb"; 106 uint8_t dump_opt[256]; 107 108 typedef void object_viewer_t(objset_t *, uint64_t, void *data, size_t size); 109 110 uint64_t *zopt_object = NULL; 111 static unsigned zopt_objects = 0; 112 libzfs_handle_t *g_zfs; 113 uint64_t max_inflight = 1000; 114 static int leaked_objects = 0; 115 116 static void snprintf_blkptr_compact(char *, size_t, const blkptr_t *); 117 static void mos_obj_refd(uint64_t); 118 119 /* 120 * These libumem hooks provide a reasonable set of defaults for the allocator's 121 * debugging facilities. 122 */ 123 const char * 124 _umem_debug_init() 125 { 126 return ("default,verbose"); /* $UMEM_DEBUG setting */ 127 } 128 129 const char * 130 _umem_logging_init(void) 131 { 132 return ("fail,contents"); /* $UMEM_LOGGING setting */ 133 } 134 135 static void 136 usage(void) 137 { 138 (void) fprintf(stderr, 139 "Usage:\t%s [-AbcdDFGhikLMPsvX] [-e [-V] [-p <path> ...]] " 140 "[-I <inflight I/Os>]\n" 141 "\t\t[-o <var>=<value>]... [-t <txg>] [-U <cache>] [-x <dumpdir>]\n" 142 "\t\t[<poolname> [<object> ...]]\n" 143 "\t%s [-AdiPv] [-e [-V] [-p <path> ...]] [-U <cache>] <dataset> " 144 "[<object> ...]\n" 145 "\t%s -C [-A] [-U <cache>]\n" 146 "\t%s -l [-Aqu] <device>\n" 147 "\t%s -m [-AFLPX] [-e [-V] [-p <path> ...]] [-t <txg>] " 148 "[-U <cache>]\n\t\t<poolname> [<vdev> [<metaslab> ...]]\n" 149 "\t%s -O <dataset> <path>\n" 150 "\t%s -R [-A] [-e [-V] [-p <path> ...]] [-U <cache>]\n" 151 "\t\t<poolname> <vdev>:<offset>:<size>[:<flags>]\n" 152 "\t%s -E [-A] word0:word1:...:word15\n" 153 "\t%s -S [-AP] [-e [-V] [-p <path> ...]] [-U <cache>] " 154 "<poolname>\n\n", 155 cmdname, cmdname, cmdname, cmdname, cmdname, cmdname, cmdname, 156 cmdname, cmdname); 157 158 (void) fprintf(stderr, " Dataset name must include at least one " 159 "separator character '/' or '@'\n"); 160 (void) fprintf(stderr, " If dataset name is specified, only that " 161 "dataset is dumped\n"); 162 (void) fprintf(stderr, " If object numbers are specified, only " 163 "those objects are dumped\n\n"); 164 (void) fprintf(stderr, " Options to control amount of output:\n"); 165 (void) fprintf(stderr, " -b block statistics\n"); 166 (void) fprintf(stderr, " -c checksum all metadata (twice for " 167 "all data) blocks\n"); 168 (void) fprintf(stderr, " -C config (or cachefile if alone)\n"); 169 (void) fprintf(stderr, " -d dataset(s)\n"); 170 (void) fprintf(stderr, " -D dedup statistics\n"); 171 (void) fprintf(stderr, " -E decode and display block from an " 172 "embedded block pointer\n"); 173 (void) fprintf(stderr, " -h pool history\n"); 174 (void) fprintf(stderr, " -i intent logs\n"); 175 (void) fprintf(stderr, " -l read label contents\n"); 176 (void) fprintf(stderr, " -k examine the checkpointed state " 177 "of the pool\n"); 178 (void) fprintf(stderr, " -L disable leak tracking (do not " 179 "load spacemaps)\n"); 180 (void) fprintf(stderr, " -m metaslabs\n"); 181 (void) fprintf(stderr, " -M metaslab groups\n"); 182 (void) fprintf(stderr, " -O perform object lookups by path\n"); 183 (void) fprintf(stderr, " -R read and display block from a " 184 "device\n"); 185 (void) fprintf(stderr, " -s report stats on zdb's I/O\n"); 186 (void) fprintf(stderr, " -S simulate dedup to measure effect\n"); 187 (void) fprintf(stderr, " -v verbose (applies to all " 188 "others)\n\n"); 189 (void) fprintf(stderr, " Below options are intended for use " 190 "with other options:\n"); 191 (void) fprintf(stderr, " -A ignore assertions (-A), enable " 192 "panic recovery (-AA) or both (-AAA)\n"); 193 (void) fprintf(stderr, " -e pool is exported/destroyed/" 194 "has altroot/not in a cachefile\n"); 195 (void) fprintf(stderr, " -F attempt automatic rewind within " 196 "safe range of transaction groups\n"); 197 (void) fprintf(stderr, " -G dump zfs_dbgmsg buffer before " 198 "exiting\n"); 199 (void) fprintf(stderr, " -I <number of inflight I/Os> -- " 200 "specify the maximum number of " 201 "checksumming I/Os [default is 200]\n"); 202 (void) fprintf(stderr, " -o <variable>=<value> set global " 203 "variable to an unsigned 32-bit integer value\n"); 204 (void) fprintf(stderr, " -p <path> -- use one or more with " 205 "-e to specify path to vdev dir\n"); 206 (void) fprintf(stderr, " -P print numbers in parseable form\n"); 207 (void) fprintf(stderr, " -q don't print label contents\n"); 208 (void) fprintf(stderr, " -t <txg> -- highest txg to use when " 209 "searching for uberblocks\n"); 210 (void) fprintf(stderr, " -u uberblock\n"); 211 (void) fprintf(stderr, " -U <cachefile_path> -- use alternate " 212 "cachefile\n"); 213 (void) fprintf(stderr, " -V do verbatim import\n"); 214 (void) fprintf(stderr, " -x <dumpdir> -- " 215 "dump all read blocks into specified directory\n"); 216 (void) fprintf(stderr, " -X attempt extreme rewind (does not " 217 "work with dataset)\n\n"); 218 (void) fprintf(stderr, "Specify an option more than once (e.g. -bb) " 219 "to make only that option verbose\n"); 220 (void) fprintf(stderr, "Default is to dump everything non-verbosely\n"); 221 exit(1); 222 } 223 224 static void 225 dump_debug_buffer() 226 { 227 if (dump_opt['G']) { 228 (void) printf("\n"); 229 zfs_dbgmsg_print("zdb"); 230 } 231 } 232 233 /* 234 * Called for usage errors that are discovered after a call to spa_open(), 235 * dmu_bonus_hold(), or pool_match(). abort() is called for other errors. 236 */ 237 238 static void 239 fatal(const char *fmt, ...) 240 { 241 va_list ap; 242 243 va_start(ap, fmt); 244 (void) fprintf(stderr, "%s: ", cmdname); 245 (void) vfprintf(stderr, fmt, ap); 246 va_end(ap); 247 (void) fprintf(stderr, "\n"); 248 249 dump_debug_buffer(); 250 251 exit(1); 252 } 253 254 /* ARGSUSED */ 255 static void 256 dump_packed_nvlist(objset_t *os, uint64_t object, void *data, size_t size) 257 { 258 nvlist_t *nv; 259 size_t nvsize = *(uint64_t *)data; 260 char *packed = umem_alloc(nvsize, UMEM_NOFAIL); 261 262 VERIFY(0 == dmu_read(os, object, 0, nvsize, packed, DMU_READ_PREFETCH)); 263 264 VERIFY(nvlist_unpack(packed, nvsize, &nv, 0) == 0); 265 266 umem_free(packed, nvsize); 267 268 dump_nvlist(nv, 8); 269 270 nvlist_free(nv); 271 } 272 273 /* ARGSUSED */ 274 static void 275 dump_history_offsets(objset_t *os, uint64_t object, void *data, size_t size) 276 { 277 spa_history_phys_t *shp = data; 278 279 if (shp == NULL) 280 return; 281 282 (void) printf("\t\tpool_create_len = %llu\n", 283 (u_longlong_t)shp->sh_pool_create_len); 284 (void) printf("\t\tphys_max_off = %llu\n", 285 (u_longlong_t)shp->sh_phys_max_off); 286 (void) printf("\t\tbof = %llu\n", 287 (u_longlong_t)shp->sh_bof); 288 (void) printf("\t\teof = %llu\n", 289 (u_longlong_t)shp->sh_eof); 290 (void) printf("\t\trecords_lost = %llu\n", 291 (u_longlong_t)shp->sh_records_lost); 292 } 293 294 static void 295 zdb_nicenum(uint64_t num, char *buf, size_t buflen) 296 { 297 if (dump_opt['P']) 298 (void) snprintf(buf, buflen, "%llu", (longlong_t)num); 299 else 300 nicenum(num, buf, sizeof (buf)); 301 } 302 303 static const char histo_stars[] = "****************************************"; 304 static const uint64_t histo_width = sizeof (histo_stars) - 1; 305 306 static void 307 dump_histogram(const uint64_t *histo, int size, int offset) 308 { 309 int i; 310 int minidx = size - 1; 311 int maxidx = 0; 312 uint64_t max = 0; 313 314 for (i = 0; i < size; i++) { 315 if (histo[i] > max) 316 max = histo[i]; 317 if (histo[i] > 0 && i > maxidx) 318 maxidx = i; 319 if (histo[i] > 0 && i < minidx) 320 minidx = i; 321 } 322 323 if (max < histo_width) 324 max = histo_width; 325 326 for (i = minidx; i <= maxidx; i++) { 327 (void) printf("\t\t\t%3u: %6llu %s\n", 328 i + offset, (u_longlong_t)histo[i], 329 &histo_stars[(max - histo[i]) * histo_width / max]); 330 } 331 } 332 333 static void 334 dump_zap_stats(objset_t *os, uint64_t object) 335 { 336 int error; 337 zap_stats_t zs; 338 339 error = zap_get_stats(os, object, &zs); 340 if (error) 341 return; 342 343 if (zs.zs_ptrtbl_len == 0) { 344 ASSERT(zs.zs_num_blocks == 1); 345 (void) printf("\tmicrozap: %llu bytes, %llu entries\n", 346 (u_longlong_t)zs.zs_blocksize, 347 (u_longlong_t)zs.zs_num_entries); 348 return; 349 } 350 351 (void) printf("\tFat ZAP stats:\n"); 352 353 (void) printf("\t\tPointer table:\n"); 354 (void) printf("\t\t\t%llu elements\n", 355 (u_longlong_t)zs.zs_ptrtbl_len); 356 (void) printf("\t\t\tzt_blk: %llu\n", 357 (u_longlong_t)zs.zs_ptrtbl_zt_blk); 358 (void) printf("\t\t\tzt_numblks: %llu\n", 359 (u_longlong_t)zs.zs_ptrtbl_zt_numblks); 360 (void) printf("\t\t\tzt_shift: %llu\n", 361 (u_longlong_t)zs.zs_ptrtbl_zt_shift); 362 (void) printf("\t\t\tzt_blks_copied: %llu\n", 363 (u_longlong_t)zs.zs_ptrtbl_blks_copied); 364 (void) printf("\t\t\tzt_nextblk: %llu\n", 365 (u_longlong_t)zs.zs_ptrtbl_nextblk); 366 367 (void) printf("\t\tZAP entries: %llu\n", 368 (u_longlong_t)zs.zs_num_entries); 369 (void) printf("\t\tLeaf blocks: %llu\n", 370 (u_longlong_t)zs.zs_num_leafs); 371 (void) printf("\t\tTotal blocks: %llu\n", 372 (u_longlong_t)zs.zs_num_blocks); 373 (void) printf("\t\tzap_block_type: 0x%llx\n", 374 (u_longlong_t)zs.zs_block_type); 375 (void) printf("\t\tzap_magic: 0x%llx\n", 376 (u_longlong_t)zs.zs_magic); 377 (void) printf("\t\tzap_salt: 0x%llx\n", 378 (u_longlong_t)zs.zs_salt); 379 380 (void) printf("\t\tLeafs with 2^n pointers:\n"); 381 dump_histogram(zs.zs_leafs_with_2n_pointers, ZAP_HISTOGRAM_SIZE, 0); 382 383 (void) printf("\t\tBlocks with n*5 entries:\n"); 384 dump_histogram(zs.zs_blocks_with_n5_entries, ZAP_HISTOGRAM_SIZE, 0); 385 386 (void) printf("\t\tBlocks n/10 full:\n"); 387 dump_histogram(zs.zs_blocks_n_tenths_full, ZAP_HISTOGRAM_SIZE, 0); 388 389 (void) printf("\t\tEntries with n chunks:\n"); 390 dump_histogram(zs.zs_entries_using_n_chunks, ZAP_HISTOGRAM_SIZE, 0); 391 392 (void) printf("\t\tBuckets with n entries:\n"); 393 dump_histogram(zs.zs_buckets_with_n_entries, ZAP_HISTOGRAM_SIZE, 0); 394 } 395 396 /*ARGSUSED*/ 397 static void 398 dump_none(objset_t *os, uint64_t object, void *data, size_t size) 399 { 400 } 401 402 /*ARGSUSED*/ 403 static void 404 dump_unknown(objset_t *os, uint64_t object, void *data, size_t size) 405 { 406 (void) printf("\tUNKNOWN OBJECT TYPE\n"); 407 } 408 409 /*ARGSUSED*/ 410 static void 411 dump_uint8(objset_t *os, uint64_t object, void *data, size_t size) 412 { 413 } 414 415 /*ARGSUSED*/ 416 static void 417 dump_uint64(objset_t *os, uint64_t object, void *data, size_t size) 418 { 419 } 420 421 /*ARGSUSED*/ 422 static void 423 dump_zap(objset_t *os, uint64_t object, void *data, size_t size) 424 { 425 zap_cursor_t zc; 426 zap_attribute_t attr; 427 void *prop; 428 unsigned i; 429 430 dump_zap_stats(os, object); 431 (void) printf("\n"); 432 433 for (zap_cursor_init(&zc, os, object); 434 zap_cursor_retrieve(&zc, &attr) == 0; 435 zap_cursor_advance(&zc)) { 436 (void) printf("\t\t%s = ", attr.za_name); 437 if (attr.za_num_integers == 0) { 438 (void) printf("\n"); 439 continue; 440 } 441 prop = umem_zalloc(attr.za_num_integers * 442 attr.za_integer_length, UMEM_NOFAIL); 443 (void) zap_lookup(os, object, attr.za_name, 444 attr.za_integer_length, attr.za_num_integers, prop); 445 if (attr.za_integer_length == 1) { 446 (void) printf("%s", (char *)prop); 447 } else { 448 for (i = 0; i < attr.za_num_integers; i++) { 449 switch (attr.za_integer_length) { 450 case 2: 451 (void) printf("%u ", 452 ((uint16_t *)prop)[i]); 453 break; 454 case 4: 455 (void) printf("%u ", 456 ((uint32_t *)prop)[i]); 457 break; 458 case 8: 459 (void) printf("%lld ", 460 (u_longlong_t)((int64_t *)prop)[i]); 461 break; 462 } 463 } 464 } 465 (void) printf("\n"); 466 umem_free(prop, attr.za_num_integers * attr.za_integer_length); 467 } 468 zap_cursor_fini(&zc); 469 } 470 471 static void 472 dump_bpobj(objset_t *os, uint64_t object, void *data, size_t size) 473 { 474 bpobj_phys_t *bpop = data; 475 char bytes[32], comp[32], uncomp[32]; 476 477 /* make sure the output won't get truncated */ 478 CTASSERT(sizeof (bytes) >= NN_NUMBUF_SZ); 479 CTASSERT(sizeof (comp) >= NN_NUMBUF_SZ); 480 CTASSERT(sizeof (uncomp) >= NN_NUMBUF_SZ); 481 482 if (bpop == NULL) 483 return; 484 485 zdb_nicenum(bpop->bpo_bytes, bytes, sizeof (bytes)); 486 zdb_nicenum(bpop->bpo_comp, comp, sizeof (comp)); 487 zdb_nicenum(bpop->bpo_uncomp, uncomp, sizeof (uncomp)); 488 489 (void) printf("\t\tnum_blkptrs = %llu\n", 490 (u_longlong_t)bpop->bpo_num_blkptrs); 491 (void) printf("\t\tbytes = %s\n", bytes); 492 if (size >= BPOBJ_SIZE_V1) { 493 (void) printf("\t\tcomp = %s\n", comp); 494 (void) printf("\t\tuncomp = %s\n", uncomp); 495 } 496 if (size >= sizeof (*bpop)) { 497 (void) printf("\t\tsubobjs = %llu\n", 498 (u_longlong_t)bpop->bpo_subobjs); 499 (void) printf("\t\tnum_subobjs = %llu\n", 500 (u_longlong_t)bpop->bpo_num_subobjs); 501 } 502 503 if (dump_opt['d'] < 5) 504 return; 505 506 for (uint64_t i = 0; i < bpop->bpo_num_blkptrs; i++) { 507 char blkbuf[BP_SPRINTF_LEN]; 508 blkptr_t bp; 509 510 int err = dmu_read(os, object, 511 i * sizeof (bp), sizeof (bp), &bp, 0); 512 if (err != 0) { 513 (void) printf("got error %u from dmu_read\n", err); 514 break; 515 } 516 snprintf_blkptr_compact(blkbuf, sizeof (blkbuf), &bp); 517 (void) printf("\t%s\n", blkbuf); 518 } 519 } 520 521 /* ARGSUSED */ 522 static void 523 dump_bpobj_subobjs(objset_t *os, uint64_t object, void *data, size_t size) 524 { 525 dmu_object_info_t doi; 526 527 VERIFY0(dmu_object_info(os, object, &doi)); 528 uint64_t *subobjs = kmem_alloc(doi.doi_max_offset, KM_SLEEP); 529 530 int err = dmu_read(os, object, 0, doi.doi_max_offset, subobjs, 0); 531 if (err != 0) { 532 (void) printf("got error %u from dmu_read\n", err); 533 kmem_free(subobjs, doi.doi_max_offset); 534 return; 535 } 536 537 int64_t last_nonzero = -1; 538 for (uint64_t i = 0; i < doi.doi_max_offset / 8; i++) { 539 if (subobjs[i] != 0) 540 last_nonzero = i; 541 } 542 543 for (int64_t i = 0; i <= last_nonzero; i++) { 544 (void) printf("\t%llu\n", (longlong_t)subobjs[i]); 545 } 546 kmem_free(subobjs, doi.doi_max_offset); 547 } 548 549 /*ARGSUSED*/ 550 static void 551 dump_ddt_zap(objset_t *os, uint64_t object, void *data, size_t size) 552 { 553 dump_zap_stats(os, object); 554 /* contents are printed elsewhere, properly decoded */ 555 } 556 557 /*ARGSUSED*/ 558 static void 559 dump_sa_attrs(objset_t *os, uint64_t object, void *data, size_t size) 560 { 561 zap_cursor_t zc; 562 zap_attribute_t attr; 563 564 dump_zap_stats(os, object); 565 (void) printf("\n"); 566 567 for (zap_cursor_init(&zc, os, object); 568 zap_cursor_retrieve(&zc, &attr) == 0; 569 zap_cursor_advance(&zc)) { 570 (void) printf("\t\t%s = ", attr.za_name); 571 if (attr.za_num_integers == 0) { 572 (void) printf("\n"); 573 continue; 574 } 575 (void) printf(" %llx : [%d:%d:%d]\n", 576 (u_longlong_t)attr.za_first_integer, 577 (int)ATTR_LENGTH(attr.za_first_integer), 578 (int)ATTR_BSWAP(attr.za_first_integer), 579 (int)ATTR_NUM(attr.za_first_integer)); 580 } 581 zap_cursor_fini(&zc); 582 } 583 584 /*ARGSUSED*/ 585 static void 586 dump_sa_layouts(objset_t *os, uint64_t object, void *data, size_t size) 587 { 588 zap_cursor_t zc; 589 zap_attribute_t attr; 590 uint16_t *layout_attrs; 591 unsigned i; 592 593 dump_zap_stats(os, object); 594 (void) printf("\n"); 595 596 for (zap_cursor_init(&zc, os, object); 597 zap_cursor_retrieve(&zc, &attr) == 0; 598 zap_cursor_advance(&zc)) { 599 (void) printf("\t\t%s = [", attr.za_name); 600 if (attr.za_num_integers == 0) { 601 (void) printf("\n"); 602 continue; 603 } 604 605 VERIFY(attr.za_integer_length == 2); 606 layout_attrs = umem_zalloc(attr.za_num_integers * 607 attr.za_integer_length, UMEM_NOFAIL); 608 609 VERIFY(zap_lookup(os, object, attr.za_name, 610 attr.za_integer_length, 611 attr.za_num_integers, layout_attrs) == 0); 612 613 for (i = 0; i != attr.za_num_integers; i++) 614 (void) printf(" %d ", (int)layout_attrs[i]); 615 (void) printf("]\n"); 616 umem_free(layout_attrs, 617 attr.za_num_integers * attr.za_integer_length); 618 } 619 zap_cursor_fini(&zc); 620 } 621 622 /*ARGSUSED*/ 623 static void 624 dump_zpldir(objset_t *os, uint64_t object, void *data, size_t size) 625 { 626 zap_cursor_t zc; 627 zap_attribute_t attr; 628 const char *typenames[] = { 629 /* 0 */ "not specified", 630 /* 1 */ "FIFO", 631 /* 2 */ "Character Device", 632 /* 3 */ "3 (invalid)", 633 /* 4 */ "Directory", 634 /* 5 */ "5 (invalid)", 635 /* 6 */ "Block Device", 636 /* 7 */ "7 (invalid)", 637 /* 8 */ "Regular File", 638 /* 9 */ "9 (invalid)", 639 /* 10 */ "Symbolic Link", 640 /* 11 */ "11 (invalid)", 641 /* 12 */ "Socket", 642 /* 13 */ "Door", 643 /* 14 */ "Event Port", 644 /* 15 */ "15 (invalid)", 645 }; 646 647 dump_zap_stats(os, object); 648 (void) printf("\n"); 649 650 for (zap_cursor_init(&zc, os, object); 651 zap_cursor_retrieve(&zc, &attr) == 0; 652 zap_cursor_advance(&zc)) { 653 (void) printf("\t\t%s = %lld (type: %s)\n", 654 attr.za_name, ZFS_DIRENT_OBJ(attr.za_first_integer), 655 typenames[ZFS_DIRENT_TYPE(attr.za_first_integer)]); 656 } 657 zap_cursor_fini(&zc); 658 } 659 660 static int 661 get_dtl_refcount(vdev_t *vd) 662 { 663 int refcount = 0; 664 665 if (vd->vdev_ops->vdev_op_leaf) { 666 space_map_t *sm = vd->vdev_dtl_sm; 667 668 if (sm != NULL && 669 sm->sm_dbuf->db_size == sizeof (space_map_phys_t)) 670 return (1); 671 return (0); 672 } 673 674 for (unsigned c = 0; c < vd->vdev_children; c++) 675 refcount += get_dtl_refcount(vd->vdev_child[c]); 676 return (refcount); 677 } 678 679 static int 680 get_metaslab_refcount(vdev_t *vd) 681 { 682 int refcount = 0; 683 684 if (vd->vdev_top == vd) { 685 for (uint64_t m = 0; m < vd->vdev_ms_count; m++) { 686 space_map_t *sm = vd->vdev_ms[m]->ms_sm; 687 688 if (sm != NULL && 689 sm->sm_dbuf->db_size == sizeof (space_map_phys_t)) 690 refcount++; 691 } 692 } 693 for (unsigned c = 0; c < vd->vdev_children; c++) 694 refcount += get_metaslab_refcount(vd->vdev_child[c]); 695 696 return (refcount); 697 } 698 699 static int 700 get_obsolete_refcount(vdev_t *vd) 701 { 702 int refcount = 0; 703 704 uint64_t obsolete_sm_obj = vdev_obsolete_sm_object(vd); 705 if (vd->vdev_top == vd && obsolete_sm_obj != 0) { 706 dmu_object_info_t doi; 707 VERIFY0(dmu_object_info(vd->vdev_spa->spa_meta_objset, 708 obsolete_sm_obj, &doi)); 709 if (doi.doi_bonus_size == sizeof (space_map_phys_t)) { 710 refcount++; 711 } 712 } else { 713 ASSERT3P(vd->vdev_obsolete_sm, ==, NULL); 714 ASSERT3U(obsolete_sm_obj, ==, 0); 715 } 716 for (unsigned c = 0; c < vd->vdev_children; c++) { 717 refcount += get_obsolete_refcount(vd->vdev_child[c]); 718 } 719 720 return (refcount); 721 } 722 723 static int 724 get_prev_obsolete_spacemap_refcount(spa_t *spa) 725 { 726 uint64_t prev_obj = 727 spa->spa_condensing_indirect_phys.scip_prev_obsolete_sm_object; 728 if (prev_obj != 0) { 729 dmu_object_info_t doi; 730 VERIFY0(dmu_object_info(spa->spa_meta_objset, prev_obj, &doi)); 731 if (doi.doi_bonus_size == sizeof (space_map_phys_t)) { 732 return (1); 733 } 734 } 735 return (0); 736 } 737 738 static int 739 get_checkpoint_refcount(vdev_t *vd) 740 { 741 int refcount = 0; 742 743 if (vd->vdev_top == vd && vd->vdev_top_zap != 0 && 744 zap_contains(spa_meta_objset(vd->vdev_spa), 745 vd->vdev_top_zap, VDEV_TOP_ZAP_POOL_CHECKPOINT_SM) == 0) 746 refcount++; 747 748 for (uint64_t c = 0; c < vd->vdev_children; c++) 749 refcount += get_checkpoint_refcount(vd->vdev_child[c]); 750 751 return (refcount); 752 } 753 754 static int 755 verify_spacemap_refcounts(spa_t *spa) 756 { 757 uint64_t expected_refcount = 0; 758 uint64_t actual_refcount; 759 760 (void) feature_get_refcount(spa, 761 &spa_feature_table[SPA_FEATURE_SPACEMAP_HISTOGRAM], 762 &expected_refcount); 763 actual_refcount = get_dtl_refcount(spa->spa_root_vdev); 764 actual_refcount += get_metaslab_refcount(spa->spa_root_vdev); 765 actual_refcount += get_obsolete_refcount(spa->spa_root_vdev); 766 actual_refcount += get_prev_obsolete_spacemap_refcount(spa); 767 actual_refcount += get_checkpoint_refcount(spa->spa_root_vdev); 768 769 if (expected_refcount != actual_refcount) { 770 (void) printf("space map refcount mismatch: expected %lld != " 771 "actual %lld\n", 772 (longlong_t)expected_refcount, 773 (longlong_t)actual_refcount); 774 return (2); 775 } 776 return (0); 777 } 778 779 static void 780 dump_spacemap(objset_t *os, space_map_t *sm) 781 { 782 char *ddata[] = { "ALLOC", "FREE", "CONDENSE", "INVALID", 783 "INVALID", "INVALID", "INVALID", "INVALID" }; 784 785 if (sm == NULL) 786 return; 787 788 (void) printf("space map object %llu:\n", 789 (longlong_t)sm->sm_object); 790 (void) printf(" smp_length = 0x%llx\n", 791 (longlong_t)sm->sm_phys->smp_length); 792 (void) printf(" smp_alloc = 0x%llx\n", 793 (longlong_t)sm->sm_phys->smp_alloc); 794 795 if (dump_opt['d'] < 6 && dump_opt['m'] < 4) 796 return; 797 798 /* 799 * Print out the freelist entries in both encoded and decoded form. 800 */ 801 uint8_t mapshift = sm->sm_shift; 802 int64_t alloc = 0; 803 uint64_t word, entry_id = 0; 804 for (uint64_t offset = 0; offset < space_map_length(sm); 805 offset += sizeof (word)) { 806 807 VERIFY0(dmu_read(os, space_map_object(sm), offset, 808 sizeof (word), &word, DMU_READ_PREFETCH)); 809 810 if (sm_entry_is_debug(word)) { 811 (void) printf("\t [%6llu] %s: txg %llu pass %llu\n", 812 (u_longlong_t)entry_id, 813 ddata[SM_DEBUG_ACTION_DECODE(word)], 814 (u_longlong_t)SM_DEBUG_TXG_DECODE(word), 815 (u_longlong_t)SM_DEBUG_SYNCPASS_DECODE(word)); 816 entry_id++; 817 continue; 818 } 819 820 uint8_t words; 821 char entry_type; 822 uint64_t entry_off, entry_run, entry_vdev = SM_NO_VDEVID; 823 824 if (sm_entry_is_single_word(word)) { 825 entry_type = (SM_TYPE_DECODE(word) == SM_ALLOC) ? 826 'A' : 'F'; 827 entry_off = (SM_OFFSET_DECODE(word) << mapshift) + 828 sm->sm_start; 829 entry_run = SM_RUN_DECODE(word) << mapshift; 830 words = 1; 831 } else { 832 /* it is a two-word entry so we read another word */ 833 ASSERT(sm_entry_is_double_word(word)); 834 835 uint64_t extra_word; 836 offset += sizeof (extra_word); 837 VERIFY0(dmu_read(os, space_map_object(sm), offset, 838 sizeof (extra_word), &extra_word, 839 DMU_READ_PREFETCH)); 840 841 ASSERT3U(offset, <=, space_map_length(sm)); 842 843 entry_run = SM2_RUN_DECODE(word) << mapshift; 844 entry_vdev = SM2_VDEV_DECODE(word); 845 entry_type = (SM2_TYPE_DECODE(extra_word) == SM_ALLOC) ? 846 'A' : 'F'; 847 entry_off = (SM2_OFFSET_DECODE(extra_word) << 848 mapshift) + sm->sm_start; 849 words = 2; 850 } 851 852 (void) printf("\t [%6llu] %c range:" 853 " %010llx-%010llx size: %06llx vdev: %06llu words: %u\n", 854 (u_longlong_t)entry_id, 855 entry_type, (u_longlong_t)entry_off, 856 (u_longlong_t)(entry_off + entry_run), 857 (u_longlong_t)entry_run, 858 (u_longlong_t)entry_vdev, words); 859 860 if (entry_type == 'A') 861 alloc += entry_run; 862 else 863 alloc -= entry_run; 864 entry_id++; 865 } 866 if (alloc != space_map_allocated(sm)) { 867 (void) printf("space_map_object alloc (%lld) INCONSISTENT " 868 "with space map summary (%lld)\n", 869 (longlong_t)space_map_allocated(sm), (longlong_t)alloc); 870 } 871 } 872 873 static void 874 dump_metaslab_stats(metaslab_t *msp) 875 { 876 char maxbuf[32]; 877 range_tree_t *rt = msp->ms_allocatable; 878 avl_tree_t *t = &msp->ms_allocatable_by_size; 879 int free_pct = range_tree_space(rt) * 100 / msp->ms_size; 880 881 /* max sure nicenum has enough space */ 882 CTASSERT(sizeof (maxbuf) >= NN_NUMBUF_SZ); 883 884 zdb_nicenum(metaslab_block_maxsize(msp), maxbuf, sizeof (maxbuf)); 885 886 (void) printf("\t %25s %10lu %7s %6s %4s %4d%%\n", 887 "segments", avl_numnodes(t), "maxsize", maxbuf, 888 "freepct", free_pct); 889 (void) printf("\tIn-memory histogram:\n"); 890 dump_histogram(rt->rt_histogram, RANGE_TREE_HISTOGRAM_SIZE, 0); 891 } 892 893 static void 894 dump_metaslab(metaslab_t *msp) 895 { 896 vdev_t *vd = msp->ms_group->mg_vd; 897 spa_t *spa = vd->vdev_spa; 898 space_map_t *sm = msp->ms_sm; 899 char freebuf[32]; 900 901 zdb_nicenum(msp->ms_size - space_map_allocated(sm), freebuf, 902 sizeof (freebuf)); 903 904 (void) printf( 905 "\tmetaslab %6llu offset %12llx spacemap %6llu free %5s\n", 906 (u_longlong_t)msp->ms_id, (u_longlong_t)msp->ms_start, 907 (u_longlong_t)space_map_object(sm), freebuf); 908 909 if (dump_opt['m'] > 2 && !dump_opt['L']) { 910 mutex_enter(&msp->ms_lock); 911 VERIFY0(metaslab_load(msp)); 912 range_tree_stat_verify(msp->ms_allocatable); 913 dump_metaslab_stats(msp); 914 metaslab_unload(msp); 915 mutex_exit(&msp->ms_lock); 916 } 917 918 if (dump_opt['m'] > 1 && sm != NULL && 919 spa_feature_is_active(spa, SPA_FEATURE_SPACEMAP_HISTOGRAM)) { 920 /* 921 * The space map histogram represents free space in chunks 922 * of sm_shift (i.e. bucket 0 refers to 2^sm_shift). 923 */ 924 (void) printf("\tOn-disk histogram:\t\tfragmentation %llu\n", 925 (u_longlong_t)msp->ms_fragmentation); 926 dump_histogram(sm->sm_phys->smp_histogram, 927 SPACE_MAP_HISTOGRAM_SIZE, sm->sm_shift); 928 } 929 930 ASSERT(msp->ms_size == (1ULL << vd->vdev_ms_shift)); 931 dump_spacemap(spa->spa_meta_objset, msp->ms_sm); 932 } 933 934 static void 935 print_vdev_metaslab_header(vdev_t *vd) 936 { 937 vdev_alloc_bias_t alloc_bias = vd->vdev_alloc_bias; 938 const char *bias_str; 939 940 bias_str = (alloc_bias == VDEV_BIAS_LOG || vd->vdev_islog) ? 941 VDEV_ALLOC_BIAS_LOG : 942 (alloc_bias == VDEV_BIAS_SPECIAL) ? VDEV_ALLOC_BIAS_SPECIAL : 943 (alloc_bias == VDEV_BIAS_DEDUP) ? VDEV_ALLOC_BIAS_DEDUP : 944 vd->vdev_islog ? "log" : ""; 945 946 (void) printf("\tvdev %10llu %s\n" 947 "\t%-10s%5llu %-19s %-15s %-12s\n", 948 (u_longlong_t)vd->vdev_id, bias_str, 949 "metaslabs", (u_longlong_t)vd->vdev_ms_count, 950 "offset", "spacemap", "free"); 951 (void) printf("\t%15s %19s %15s %12s\n", 952 "---------------", "-------------------", 953 "---------------", "------------"); 954 } 955 956 static void 957 dump_metaslab_groups(spa_t *spa) 958 { 959 vdev_t *rvd = spa->spa_root_vdev; 960 metaslab_class_t *mc = spa_normal_class(spa); 961 uint64_t fragmentation; 962 963 metaslab_class_histogram_verify(mc); 964 965 for (unsigned c = 0; c < rvd->vdev_children; c++) { 966 vdev_t *tvd = rvd->vdev_child[c]; 967 metaslab_group_t *mg = tvd->vdev_mg; 968 969 if (mg == NULL || mg->mg_class != mc) 970 continue; 971 972 metaslab_group_histogram_verify(mg); 973 mg->mg_fragmentation = metaslab_group_fragmentation(mg); 974 975 (void) printf("\tvdev %10llu\t\tmetaslabs%5llu\t\t" 976 "fragmentation", 977 (u_longlong_t)tvd->vdev_id, 978 (u_longlong_t)tvd->vdev_ms_count); 979 if (mg->mg_fragmentation == ZFS_FRAG_INVALID) { 980 (void) printf("%3s\n", "-"); 981 } else { 982 (void) printf("%3llu%%\n", 983 (u_longlong_t)mg->mg_fragmentation); 984 } 985 dump_histogram(mg->mg_histogram, RANGE_TREE_HISTOGRAM_SIZE, 0); 986 } 987 988 (void) printf("\tpool %s\tfragmentation", spa_name(spa)); 989 fragmentation = metaslab_class_fragmentation(mc); 990 if (fragmentation == ZFS_FRAG_INVALID) 991 (void) printf("\t%3s\n", "-"); 992 else 993 (void) printf("\t%3llu%%\n", (u_longlong_t)fragmentation); 994 dump_histogram(mc->mc_histogram, RANGE_TREE_HISTOGRAM_SIZE, 0); 995 } 996 997 static void 998 print_vdev_indirect(vdev_t *vd) 999 { 1000 vdev_indirect_config_t *vic = &vd->vdev_indirect_config; 1001 vdev_indirect_mapping_t *vim = vd->vdev_indirect_mapping; 1002 vdev_indirect_births_t *vib = vd->vdev_indirect_births; 1003 1004 if (vim == NULL) { 1005 ASSERT3P(vib, ==, NULL); 1006 return; 1007 } 1008 1009 ASSERT3U(vdev_indirect_mapping_object(vim), ==, 1010 vic->vic_mapping_object); 1011 ASSERT3U(vdev_indirect_births_object(vib), ==, 1012 vic->vic_births_object); 1013 1014 (void) printf("indirect births obj %llu:\n", 1015 (longlong_t)vic->vic_births_object); 1016 (void) printf(" vib_count = %llu\n", 1017 (longlong_t)vdev_indirect_births_count(vib)); 1018 for (uint64_t i = 0; i < vdev_indirect_births_count(vib); i++) { 1019 vdev_indirect_birth_entry_phys_t *cur_vibe = 1020 &vib->vib_entries[i]; 1021 (void) printf("\toffset %llx -> txg %llu\n", 1022 (longlong_t)cur_vibe->vibe_offset, 1023 (longlong_t)cur_vibe->vibe_phys_birth_txg); 1024 } 1025 (void) printf("\n"); 1026 1027 (void) printf("indirect mapping obj %llu:\n", 1028 (longlong_t)vic->vic_mapping_object); 1029 (void) printf(" vim_max_offset = 0x%llx\n", 1030 (longlong_t)vdev_indirect_mapping_max_offset(vim)); 1031 (void) printf(" vim_bytes_mapped = 0x%llx\n", 1032 (longlong_t)vdev_indirect_mapping_bytes_mapped(vim)); 1033 (void) printf(" vim_count = %llu\n", 1034 (longlong_t)vdev_indirect_mapping_num_entries(vim)); 1035 1036 if (dump_opt['d'] <= 5 && dump_opt['m'] <= 3) 1037 return; 1038 1039 uint32_t *counts = vdev_indirect_mapping_load_obsolete_counts(vim); 1040 1041 for (uint64_t i = 0; i < vdev_indirect_mapping_num_entries(vim); i++) { 1042 vdev_indirect_mapping_entry_phys_t *vimep = 1043 &vim->vim_entries[i]; 1044 (void) printf("\t<%llx:%llx:%llx> -> " 1045 "<%llx:%llx:%llx> (%x obsolete)\n", 1046 (longlong_t)vd->vdev_id, 1047 (longlong_t)DVA_MAPPING_GET_SRC_OFFSET(vimep), 1048 (longlong_t)DVA_GET_ASIZE(&vimep->vimep_dst), 1049 (longlong_t)DVA_GET_VDEV(&vimep->vimep_dst), 1050 (longlong_t)DVA_GET_OFFSET(&vimep->vimep_dst), 1051 (longlong_t)DVA_GET_ASIZE(&vimep->vimep_dst), 1052 counts[i]); 1053 } 1054 (void) printf("\n"); 1055 1056 uint64_t obsolete_sm_object = vdev_obsolete_sm_object(vd); 1057 if (obsolete_sm_object != 0) { 1058 objset_t *mos = vd->vdev_spa->spa_meta_objset; 1059 (void) printf("obsolete space map object %llu:\n", 1060 (u_longlong_t)obsolete_sm_object); 1061 ASSERT(vd->vdev_obsolete_sm != NULL); 1062 ASSERT3U(space_map_object(vd->vdev_obsolete_sm), ==, 1063 obsolete_sm_object); 1064 dump_spacemap(mos, vd->vdev_obsolete_sm); 1065 (void) printf("\n"); 1066 } 1067 } 1068 1069 static void 1070 dump_metaslabs(spa_t *spa) 1071 { 1072 vdev_t *vd, *rvd = spa->spa_root_vdev; 1073 uint64_t m, c = 0, children = rvd->vdev_children; 1074 1075 (void) printf("\nMetaslabs:\n"); 1076 1077 if (!dump_opt['d'] && zopt_objects > 0) { 1078 c = zopt_object[0]; 1079 1080 if (c >= children) 1081 (void) fatal("bad vdev id: %llu", (u_longlong_t)c); 1082 1083 if (zopt_objects > 1) { 1084 vd = rvd->vdev_child[c]; 1085 print_vdev_metaslab_header(vd); 1086 1087 for (m = 1; m < zopt_objects; m++) { 1088 if (zopt_object[m] < vd->vdev_ms_count) 1089 dump_metaslab( 1090 vd->vdev_ms[zopt_object[m]]); 1091 else 1092 (void) fprintf(stderr, "bad metaslab " 1093 "number %llu\n", 1094 (u_longlong_t)zopt_object[m]); 1095 } 1096 (void) printf("\n"); 1097 return; 1098 } 1099 children = c + 1; 1100 } 1101 for (; c < children; c++) { 1102 vd = rvd->vdev_child[c]; 1103 print_vdev_metaslab_header(vd); 1104 1105 print_vdev_indirect(vd); 1106 1107 for (m = 0; m < vd->vdev_ms_count; m++) 1108 dump_metaslab(vd->vdev_ms[m]); 1109 (void) printf("\n"); 1110 } 1111 } 1112 1113 static void 1114 dump_dde(const ddt_t *ddt, const ddt_entry_t *dde, uint64_t index) 1115 { 1116 const ddt_phys_t *ddp = dde->dde_phys; 1117 const ddt_key_t *ddk = &dde->dde_key; 1118 const char *types[4] = { "ditto", "single", "double", "triple" }; 1119 char blkbuf[BP_SPRINTF_LEN]; 1120 blkptr_t blk; 1121 1122 for (int p = 0; p < DDT_PHYS_TYPES; p++, ddp++) { 1123 if (ddp->ddp_phys_birth == 0) 1124 continue; 1125 ddt_bp_create(ddt->ddt_checksum, ddk, ddp, &blk); 1126 snprintf_blkptr(blkbuf, sizeof (blkbuf), &blk); 1127 (void) printf("index %llx refcnt %llu %s %s\n", 1128 (u_longlong_t)index, (u_longlong_t)ddp->ddp_refcnt, 1129 types[p], blkbuf); 1130 } 1131 } 1132 1133 static void 1134 dump_dedup_ratio(const ddt_stat_t *dds) 1135 { 1136 double rL, rP, rD, D, dedup, compress, copies; 1137 1138 if (dds->dds_blocks == 0) 1139 return; 1140 1141 rL = (double)dds->dds_ref_lsize; 1142 rP = (double)dds->dds_ref_psize; 1143 rD = (double)dds->dds_ref_dsize; 1144 D = (double)dds->dds_dsize; 1145 1146 dedup = rD / D; 1147 compress = rL / rP; 1148 copies = rD / rP; 1149 1150 (void) printf("dedup = %.2f, compress = %.2f, copies = %.2f, " 1151 "dedup * compress / copies = %.2f\n\n", 1152 dedup, compress, copies, dedup * compress / copies); 1153 } 1154 1155 static void 1156 dump_ddt(ddt_t *ddt, enum ddt_type type, enum ddt_class class) 1157 { 1158 char name[DDT_NAMELEN]; 1159 ddt_entry_t dde; 1160 uint64_t walk = 0; 1161 dmu_object_info_t doi; 1162 uint64_t count, dspace, mspace; 1163 int error; 1164 1165 error = ddt_object_info(ddt, type, class, &doi); 1166 1167 if (error == ENOENT) 1168 return; 1169 ASSERT(error == 0); 1170 1171 if ((count = ddt_object_count(ddt, type, class)) == 0) 1172 return; 1173 1174 dspace = doi.doi_physical_blocks_512 << 9; 1175 mspace = doi.doi_fill_count * doi.doi_data_block_size; 1176 1177 ddt_object_name(ddt, type, class, name); 1178 1179 (void) printf("%s: %llu entries, size %llu on disk, %llu in core\n", 1180 name, 1181 (u_longlong_t)count, 1182 (u_longlong_t)(dspace / count), 1183 (u_longlong_t)(mspace / count)); 1184 1185 if (dump_opt['D'] < 3) 1186 return; 1187 1188 zpool_dump_ddt(NULL, &ddt->ddt_histogram[type][class]); 1189 1190 if (dump_opt['D'] < 4) 1191 return; 1192 1193 if (dump_opt['D'] < 5 && class == DDT_CLASS_UNIQUE) 1194 return; 1195 1196 (void) printf("%s contents:\n\n", name); 1197 1198 while ((error = ddt_object_walk(ddt, type, class, &walk, &dde)) == 0) 1199 dump_dde(ddt, &dde, walk); 1200 1201 ASSERT3U(error, ==, ENOENT); 1202 1203 (void) printf("\n"); 1204 } 1205 1206 static void 1207 dump_all_ddts(spa_t *spa) 1208 { 1209 ddt_histogram_t ddh_total; 1210 ddt_stat_t dds_total; 1211 1212 bzero(&ddh_total, sizeof (ddh_total)); 1213 bzero(&dds_total, sizeof (dds_total)); 1214 1215 for (enum zio_checksum c = 0; c < ZIO_CHECKSUM_FUNCTIONS; c++) { 1216 ddt_t *ddt = spa->spa_ddt[c]; 1217 for (enum ddt_type type = 0; type < DDT_TYPES; type++) { 1218 for (enum ddt_class class = 0; class < DDT_CLASSES; 1219 class++) { 1220 dump_ddt(ddt, type, class); 1221 } 1222 } 1223 } 1224 1225 ddt_get_dedup_stats(spa, &dds_total); 1226 1227 if (dds_total.dds_blocks == 0) { 1228 (void) printf("All DDTs are empty\n"); 1229 return; 1230 } 1231 1232 (void) printf("\n"); 1233 1234 if (dump_opt['D'] > 1) { 1235 (void) printf("DDT histogram (aggregated over all DDTs):\n"); 1236 ddt_get_dedup_histogram(spa, &ddh_total); 1237 zpool_dump_ddt(&dds_total, &ddh_total); 1238 } 1239 1240 dump_dedup_ratio(&dds_total); 1241 } 1242 1243 static void 1244 dump_dtl_seg(void *arg, uint64_t start, uint64_t size) 1245 { 1246 char *prefix = arg; 1247 1248 (void) printf("%s [%llu,%llu) length %llu\n", 1249 prefix, 1250 (u_longlong_t)start, 1251 (u_longlong_t)(start + size), 1252 (u_longlong_t)(size)); 1253 } 1254 1255 static void 1256 dump_dtl(vdev_t *vd, int indent) 1257 { 1258 spa_t *spa = vd->vdev_spa; 1259 boolean_t required; 1260 const char *name[DTL_TYPES] = { "missing", "partial", "scrub", 1261 "outage" }; 1262 char prefix[256]; 1263 1264 spa_vdev_state_enter(spa, SCL_NONE); 1265 required = vdev_dtl_required(vd); 1266 (void) spa_vdev_state_exit(spa, NULL, 0); 1267 1268 if (indent == 0) 1269 (void) printf("\nDirty time logs:\n\n"); 1270 1271 (void) printf("\t%*s%s [%s]\n", indent, "", 1272 vd->vdev_path ? vd->vdev_path : 1273 vd->vdev_parent ? vd->vdev_ops->vdev_op_type : spa_name(spa), 1274 required ? "DTL-required" : "DTL-expendable"); 1275 1276 for (int t = 0; t < DTL_TYPES; t++) { 1277 range_tree_t *rt = vd->vdev_dtl[t]; 1278 if (range_tree_space(rt) == 0) 1279 continue; 1280 (void) snprintf(prefix, sizeof (prefix), "\t%*s%s", 1281 indent + 2, "", name[t]); 1282 range_tree_walk(rt, dump_dtl_seg, prefix); 1283 if (dump_opt['d'] > 5 && vd->vdev_children == 0) 1284 dump_spacemap(spa->spa_meta_objset, vd->vdev_dtl_sm); 1285 } 1286 1287 for (unsigned c = 0; c < vd->vdev_children; c++) 1288 dump_dtl(vd->vdev_child[c], indent + 4); 1289 } 1290 1291 static void 1292 dump_history(spa_t *spa) 1293 { 1294 nvlist_t **events = NULL; 1295 uint64_t resid, len, off = 0; 1296 uint_t num = 0; 1297 int error; 1298 time_t tsec; 1299 struct tm t; 1300 char tbuf[30]; 1301 char internalstr[MAXPATHLEN]; 1302 1303 char *buf = umem_alloc(SPA_MAXBLOCKSIZE, UMEM_NOFAIL); 1304 do { 1305 len = SPA_MAXBLOCKSIZE; 1306 1307 if ((error = spa_history_get(spa, &off, &len, buf)) != 0) { 1308 (void) fprintf(stderr, "Unable to read history: " 1309 "error %d\n", error); 1310 umem_free(buf, SPA_MAXBLOCKSIZE); 1311 return; 1312 } 1313 1314 if (zpool_history_unpack(buf, len, &resid, &events, &num) != 0) 1315 break; 1316 1317 off -= resid; 1318 } while (len != 0); 1319 umem_free(buf, SPA_MAXBLOCKSIZE); 1320 1321 (void) printf("\nHistory:\n"); 1322 for (unsigned i = 0; i < num; i++) { 1323 uint64_t time, txg, ievent; 1324 char *cmd, *intstr; 1325 boolean_t printed = B_FALSE; 1326 1327 if (nvlist_lookup_uint64(events[i], ZPOOL_HIST_TIME, 1328 &time) != 0) 1329 goto next; 1330 if (nvlist_lookup_string(events[i], ZPOOL_HIST_CMD, 1331 &cmd) != 0) { 1332 if (nvlist_lookup_uint64(events[i], 1333 ZPOOL_HIST_INT_EVENT, &ievent) != 0) 1334 goto next; 1335 verify(nvlist_lookup_uint64(events[i], 1336 ZPOOL_HIST_TXG, &txg) == 0); 1337 verify(nvlist_lookup_string(events[i], 1338 ZPOOL_HIST_INT_STR, &intstr) == 0); 1339 if (ievent >= ZFS_NUM_LEGACY_HISTORY_EVENTS) 1340 goto next; 1341 1342 (void) snprintf(internalstr, 1343 sizeof (internalstr), 1344 "[internal %s txg:%ju] %s", 1345 zfs_history_event_names[ievent], (uintmax_t)txg, 1346 intstr); 1347 cmd = internalstr; 1348 } 1349 tsec = time; 1350 (void) localtime_r(&tsec, &t); 1351 (void) strftime(tbuf, sizeof (tbuf), "%F.%T", &t); 1352 (void) printf("%s %s\n", tbuf, cmd); 1353 printed = B_TRUE; 1354 1355 next: 1356 if (dump_opt['h'] > 1) { 1357 if (!printed) 1358 (void) printf("unrecognized record:\n"); 1359 dump_nvlist(events[i], 2); 1360 } 1361 } 1362 } 1363 1364 /*ARGSUSED*/ 1365 static void 1366 dump_dnode(objset_t *os, uint64_t object, void *data, size_t size) 1367 { 1368 } 1369 1370 static uint64_t 1371 blkid2offset(const dnode_phys_t *dnp, const blkptr_t *bp, 1372 const zbookmark_phys_t *zb) 1373 { 1374 if (dnp == NULL) { 1375 ASSERT(zb->zb_level < 0); 1376 if (zb->zb_object == 0) 1377 return (zb->zb_blkid); 1378 return (zb->zb_blkid * BP_GET_LSIZE(bp)); 1379 } 1380 1381 ASSERT(zb->zb_level >= 0); 1382 1383 return ((zb->zb_blkid << 1384 (zb->zb_level * (dnp->dn_indblkshift - SPA_BLKPTRSHIFT))) * 1385 dnp->dn_datablkszsec << SPA_MINBLOCKSHIFT); 1386 } 1387 1388 static void 1389 snprintf_blkptr_compact(char *blkbuf, size_t buflen, const blkptr_t *bp) 1390 { 1391 const dva_t *dva = bp->blk_dva; 1392 unsigned int ndvas = dump_opt['d'] > 5 ? BP_GET_NDVAS(bp) : 1; 1393 1394 if (dump_opt['b'] >= 6) { 1395 snprintf_blkptr(blkbuf, buflen, bp); 1396 return; 1397 } 1398 1399 if (BP_IS_EMBEDDED(bp)) { 1400 (void) sprintf(blkbuf, 1401 "EMBEDDED et=%u %llxL/%llxP B=%llu", 1402 (int)BPE_GET_ETYPE(bp), 1403 (u_longlong_t)BPE_GET_LSIZE(bp), 1404 (u_longlong_t)BPE_GET_PSIZE(bp), 1405 (u_longlong_t)bp->blk_birth); 1406 return; 1407 } 1408 1409 blkbuf[0] = '\0'; 1410 for (unsigned int i = 0; i < ndvas; i++) 1411 (void) snprintf(blkbuf + strlen(blkbuf), 1412 buflen - strlen(blkbuf), "%llu:%llx:%llx ", 1413 (u_longlong_t)DVA_GET_VDEV(&dva[i]), 1414 (u_longlong_t)DVA_GET_OFFSET(&dva[i]), 1415 (u_longlong_t)DVA_GET_ASIZE(&dva[i])); 1416 1417 if (BP_IS_HOLE(bp)) { 1418 (void) snprintf(blkbuf + strlen(blkbuf), 1419 buflen - strlen(blkbuf), 1420 "%llxL B=%llu", 1421 (u_longlong_t)BP_GET_LSIZE(bp), 1422 (u_longlong_t)bp->blk_birth); 1423 } else { 1424 (void) snprintf(blkbuf + strlen(blkbuf), 1425 buflen - strlen(blkbuf), 1426 "%llxL/%llxP F=%llu B=%llu/%llu", 1427 (u_longlong_t)BP_GET_LSIZE(bp), 1428 (u_longlong_t)BP_GET_PSIZE(bp), 1429 (u_longlong_t)BP_GET_FILL(bp), 1430 (u_longlong_t)bp->blk_birth, 1431 (u_longlong_t)BP_PHYSICAL_BIRTH(bp)); 1432 } 1433 } 1434 1435 static void 1436 print_indirect(blkptr_t *bp, const zbookmark_phys_t *zb, 1437 const dnode_phys_t *dnp) 1438 { 1439 char blkbuf[BP_SPRINTF_LEN]; 1440 int l; 1441 1442 if (!BP_IS_EMBEDDED(bp)) { 1443 ASSERT3U(BP_GET_TYPE(bp), ==, dnp->dn_type); 1444 ASSERT3U(BP_GET_LEVEL(bp), ==, zb->zb_level); 1445 } 1446 1447 (void) printf("%16llx ", (u_longlong_t)blkid2offset(dnp, bp, zb)); 1448 1449 ASSERT(zb->zb_level >= 0); 1450 1451 for (l = dnp->dn_nlevels - 1; l >= -1; l--) { 1452 if (l == zb->zb_level) { 1453 (void) printf("L%llx", (u_longlong_t)zb->zb_level); 1454 } else { 1455 (void) printf(" "); 1456 } 1457 } 1458 1459 snprintf_blkptr_compact(blkbuf, sizeof (blkbuf), bp); 1460 (void) printf("%s\n", blkbuf); 1461 } 1462 1463 static int 1464 visit_indirect(spa_t *spa, const dnode_phys_t *dnp, 1465 blkptr_t *bp, const zbookmark_phys_t *zb) 1466 { 1467 int err = 0; 1468 1469 if (bp->blk_birth == 0) 1470 return (0); 1471 1472 print_indirect(bp, zb, dnp); 1473 1474 if (BP_GET_LEVEL(bp) > 0 && !BP_IS_HOLE(bp)) { 1475 arc_flags_t flags = ARC_FLAG_WAIT; 1476 int i; 1477 blkptr_t *cbp; 1478 int epb = BP_GET_LSIZE(bp) >> SPA_BLKPTRSHIFT; 1479 arc_buf_t *buf; 1480 uint64_t fill = 0; 1481 1482 err = arc_read(NULL, spa, bp, arc_getbuf_func, &buf, 1483 ZIO_PRIORITY_ASYNC_READ, ZIO_FLAG_CANFAIL, &flags, zb); 1484 if (err) 1485 return (err); 1486 ASSERT(buf->b_data); 1487 1488 /* recursively visit blocks below this */ 1489 cbp = buf->b_data; 1490 for (i = 0; i < epb; i++, cbp++) { 1491 zbookmark_phys_t czb; 1492 1493 SET_BOOKMARK(&czb, zb->zb_objset, zb->zb_object, 1494 zb->zb_level - 1, 1495 zb->zb_blkid * epb + i); 1496 err = visit_indirect(spa, dnp, cbp, &czb); 1497 if (err) 1498 break; 1499 fill += BP_GET_FILL(cbp); 1500 } 1501 if (!err) 1502 ASSERT3U(fill, ==, BP_GET_FILL(bp)); 1503 arc_buf_destroy(buf, &buf); 1504 } 1505 1506 return (err); 1507 } 1508 1509 /*ARGSUSED*/ 1510 static void 1511 dump_indirect(dnode_t *dn) 1512 { 1513 dnode_phys_t *dnp = dn->dn_phys; 1514 int j; 1515 zbookmark_phys_t czb; 1516 1517 (void) printf("Indirect blocks:\n"); 1518 1519 SET_BOOKMARK(&czb, dmu_objset_id(dn->dn_objset), 1520 dn->dn_object, dnp->dn_nlevels - 1, 0); 1521 for (j = 0; j < dnp->dn_nblkptr; j++) { 1522 czb.zb_blkid = j; 1523 (void) visit_indirect(dmu_objset_spa(dn->dn_objset), dnp, 1524 &dnp->dn_blkptr[j], &czb); 1525 } 1526 1527 (void) printf("\n"); 1528 } 1529 1530 /*ARGSUSED*/ 1531 static void 1532 dump_dsl_dir(objset_t *os, uint64_t object, void *data, size_t size) 1533 { 1534 dsl_dir_phys_t *dd = data; 1535 time_t crtime; 1536 char nice[32]; 1537 1538 /* make sure nicenum has enough space */ 1539 CTASSERT(sizeof (nice) >= NN_NUMBUF_SZ); 1540 1541 if (dd == NULL) 1542 return; 1543 1544 ASSERT3U(size, >=, sizeof (dsl_dir_phys_t)); 1545 1546 crtime = dd->dd_creation_time; 1547 (void) printf("\t\tcreation_time = %s", ctime(&crtime)); 1548 (void) printf("\t\thead_dataset_obj = %llu\n", 1549 (u_longlong_t)dd->dd_head_dataset_obj); 1550 (void) printf("\t\tparent_dir_obj = %llu\n", 1551 (u_longlong_t)dd->dd_parent_obj); 1552 (void) printf("\t\torigin_obj = %llu\n", 1553 (u_longlong_t)dd->dd_origin_obj); 1554 (void) printf("\t\tchild_dir_zapobj = %llu\n", 1555 (u_longlong_t)dd->dd_child_dir_zapobj); 1556 zdb_nicenum(dd->dd_used_bytes, nice, sizeof (nice)); 1557 (void) printf("\t\tused_bytes = %s\n", nice); 1558 zdb_nicenum(dd->dd_compressed_bytes, nice, sizeof (nice)); 1559 (void) printf("\t\tcompressed_bytes = %s\n", nice); 1560 zdb_nicenum(dd->dd_uncompressed_bytes, nice, sizeof (nice)); 1561 (void) printf("\t\tuncompressed_bytes = %s\n", nice); 1562 zdb_nicenum(dd->dd_quota, nice, sizeof (nice)); 1563 (void) printf("\t\tquota = %s\n", nice); 1564 zdb_nicenum(dd->dd_reserved, nice, sizeof (nice)); 1565 (void) printf("\t\treserved = %s\n", nice); 1566 (void) printf("\t\tprops_zapobj = %llu\n", 1567 (u_longlong_t)dd->dd_props_zapobj); 1568 (void) printf("\t\tdeleg_zapobj = %llu\n", 1569 (u_longlong_t)dd->dd_deleg_zapobj); 1570 (void) printf("\t\tflags = %llx\n", 1571 (u_longlong_t)dd->dd_flags); 1572 1573 #define DO(which) \ 1574 zdb_nicenum(dd->dd_used_breakdown[DD_USED_ ## which], nice, \ 1575 sizeof (nice)); \ 1576 (void) printf("\t\tused_breakdown[" #which "] = %s\n", nice) 1577 DO(HEAD); 1578 DO(SNAP); 1579 DO(CHILD); 1580 DO(CHILD_RSRV); 1581 DO(REFRSRV); 1582 #undef DO 1583 (void) printf("\t\tclones = %llu\n", 1584 (u_longlong_t)dd->dd_clones); 1585 } 1586 1587 /*ARGSUSED*/ 1588 static void 1589 dump_dsl_dataset(objset_t *os, uint64_t object, void *data, size_t size) 1590 { 1591 dsl_dataset_phys_t *ds = data; 1592 time_t crtime; 1593 char used[32], compressed[32], uncompressed[32], unique[32]; 1594 char blkbuf[BP_SPRINTF_LEN]; 1595 1596 /* make sure nicenum has enough space */ 1597 CTASSERT(sizeof (used) >= NN_NUMBUF_SZ); 1598 CTASSERT(sizeof (compressed) >= NN_NUMBUF_SZ); 1599 CTASSERT(sizeof (uncompressed) >= NN_NUMBUF_SZ); 1600 CTASSERT(sizeof (unique) >= NN_NUMBUF_SZ); 1601 1602 if (ds == NULL) 1603 return; 1604 1605 ASSERT(size == sizeof (*ds)); 1606 crtime = ds->ds_creation_time; 1607 zdb_nicenum(ds->ds_referenced_bytes, used, sizeof (used)); 1608 zdb_nicenum(ds->ds_compressed_bytes, compressed, sizeof (compressed)); 1609 zdb_nicenum(ds->ds_uncompressed_bytes, uncompressed, 1610 sizeof (uncompressed)); 1611 zdb_nicenum(ds->ds_unique_bytes, unique, sizeof (unique)); 1612 snprintf_blkptr(blkbuf, sizeof (blkbuf), &ds->ds_bp); 1613 1614 (void) printf("\t\tdir_obj = %llu\n", 1615 (u_longlong_t)ds->ds_dir_obj); 1616 (void) printf("\t\tprev_snap_obj = %llu\n", 1617 (u_longlong_t)ds->ds_prev_snap_obj); 1618 (void) printf("\t\tprev_snap_txg = %llu\n", 1619 (u_longlong_t)ds->ds_prev_snap_txg); 1620 (void) printf("\t\tnext_snap_obj = %llu\n", 1621 (u_longlong_t)ds->ds_next_snap_obj); 1622 (void) printf("\t\tsnapnames_zapobj = %llu\n", 1623 (u_longlong_t)ds->ds_snapnames_zapobj); 1624 (void) printf("\t\tnum_children = %llu\n", 1625 (u_longlong_t)ds->ds_num_children); 1626 (void) printf("\t\tuserrefs_obj = %llu\n", 1627 (u_longlong_t)ds->ds_userrefs_obj); 1628 (void) printf("\t\tcreation_time = %s", ctime(&crtime)); 1629 (void) printf("\t\tcreation_txg = %llu\n", 1630 (u_longlong_t)ds->ds_creation_txg); 1631 (void) printf("\t\tdeadlist_obj = %llu\n", 1632 (u_longlong_t)ds->ds_deadlist_obj); 1633 (void) printf("\t\tused_bytes = %s\n", used); 1634 (void) printf("\t\tcompressed_bytes = %s\n", compressed); 1635 (void) printf("\t\tuncompressed_bytes = %s\n", uncompressed); 1636 (void) printf("\t\tunique = %s\n", unique); 1637 (void) printf("\t\tfsid_guid = %llu\n", 1638 (u_longlong_t)ds->ds_fsid_guid); 1639 (void) printf("\t\tguid = %llu\n", 1640 (u_longlong_t)ds->ds_guid); 1641 (void) printf("\t\tflags = %llx\n", 1642 (u_longlong_t)ds->ds_flags); 1643 (void) printf("\t\tnext_clones_obj = %llu\n", 1644 (u_longlong_t)ds->ds_next_clones_obj); 1645 (void) printf("\t\tprops_obj = %llu\n", 1646 (u_longlong_t)ds->ds_props_obj); 1647 (void) printf("\t\tbp = %s\n", blkbuf); 1648 } 1649 1650 /* ARGSUSED */ 1651 static int 1652 dump_bptree_cb(void *arg, const blkptr_t *bp, dmu_tx_t *tx) 1653 { 1654 char blkbuf[BP_SPRINTF_LEN]; 1655 1656 if (bp->blk_birth != 0) { 1657 snprintf_blkptr(blkbuf, sizeof (blkbuf), bp); 1658 (void) printf("\t%s\n", blkbuf); 1659 } 1660 return (0); 1661 } 1662 1663 static void 1664 dump_bptree(objset_t *os, uint64_t obj, const char *name) 1665 { 1666 char bytes[32]; 1667 bptree_phys_t *bt; 1668 dmu_buf_t *db; 1669 1670 /* make sure nicenum has enough space */ 1671 CTASSERT(sizeof (bytes) >= NN_NUMBUF_SZ); 1672 1673 if (dump_opt['d'] < 3) 1674 return; 1675 1676 VERIFY3U(0, ==, dmu_bonus_hold(os, obj, FTAG, &db)); 1677 bt = db->db_data; 1678 zdb_nicenum(bt->bt_bytes, bytes, sizeof (bytes)); 1679 (void) printf("\n %s: %llu datasets, %s\n", 1680 name, (unsigned long long)(bt->bt_end - bt->bt_begin), bytes); 1681 dmu_buf_rele(db, FTAG); 1682 1683 if (dump_opt['d'] < 5) 1684 return; 1685 1686 (void) printf("\n"); 1687 1688 (void) bptree_iterate(os, obj, B_FALSE, dump_bptree_cb, NULL, NULL); 1689 } 1690 1691 /* ARGSUSED */ 1692 static int 1693 dump_bpobj_cb(void *arg, const blkptr_t *bp, dmu_tx_t *tx) 1694 { 1695 char blkbuf[BP_SPRINTF_LEN]; 1696 1697 ASSERT(bp->blk_birth != 0); 1698 snprintf_blkptr_compact(blkbuf, sizeof (blkbuf), bp); 1699 (void) printf("\t%s\n", blkbuf); 1700 return (0); 1701 } 1702 1703 static void 1704 dump_full_bpobj(bpobj_t *bpo, const char *name, int indent) 1705 { 1706 char bytes[32]; 1707 char comp[32]; 1708 char uncomp[32]; 1709 1710 /* make sure nicenum has enough space */ 1711 CTASSERT(sizeof (bytes) >= NN_NUMBUF_SZ); 1712 CTASSERT(sizeof (comp) >= NN_NUMBUF_SZ); 1713 CTASSERT(sizeof (uncomp) >= NN_NUMBUF_SZ); 1714 1715 if (dump_opt['d'] < 3) 1716 return; 1717 1718 zdb_nicenum(bpo->bpo_phys->bpo_bytes, bytes, sizeof (bytes)); 1719 if (bpo->bpo_havesubobj && bpo->bpo_phys->bpo_subobjs != 0) { 1720 zdb_nicenum(bpo->bpo_phys->bpo_comp, comp, sizeof (comp)); 1721 zdb_nicenum(bpo->bpo_phys->bpo_uncomp, uncomp, sizeof (uncomp)); 1722 (void) printf(" %*s: object %llu, %llu local blkptrs, " 1723 "%llu subobjs in object %llu, %s (%s/%s comp)\n", 1724 indent * 8, name, 1725 (u_longlong_t)bpo->bpo_object, 1726 (u_longlong_t)bpo->bpo_phys->bpo_num_blkptrs, 1727 (u_longlong_t)bpo->bpo_phys->bpo_num_subobjs, 1728 (u_longlong_t)bpo->bpo_phys->bpo_subobjs, 1729 bytes, comp, uncomp); 1730 1731 for (uint64_t i = 0; i < bpo->bpo_phys->bpo_num_subobjs; i++) { 1732 uint64_t subobj; 1733 bpobj_t subbpo; 1734 int error; 1735 VERIFY0(dmu_read(bpo->bpo_os, 1736 bpo->bpo_phys->bpo_subobjs, 1737 i * sizeof (subobj), sizeof (subobj), &subobj, 0)); 1738 error = bpobj_open(&subbpo, bpo->bpo_os, subobj); 1739 if (error != 0) { 1740 (void) printf("ERROR %u while trying to open " 1741 "subobj id %llu\n", 1742 error, (u_longlong_t)subobj); 1743 continue; 1744 } 1745 dump_full_bpobj(&subbpo, "subobj", indent + 1); 1746 bpobj_close(&subbpo); 1747 } 1748 } else { 1749 (void) printf(" %*s: object %llu, %llu blkptrs, %s\n", 1750 indent * 8, name, 1751 (u_longlong_t)bpo->bpo_object, 1752 (u_longlong_t)bpo->bpo_phys->bpo_num_blkptrs, 1753 bytes); 1754 } 1755 1756 if (dump_opt['d'] < 5) 1757 return; 1758 1759 1760 if (indent == 0) { 1761 (void) bpobj_iterate_nofree(bpo, dump_bpobj_cb, NULL, NULL); 1762 (void) printf("\n"); 1763 } 1764 } 1765 1766 static void 1767 bpobj_count_refd(bpobj_t *bpo) 1768 { 1769 mos_obj_refd(bpo->bpo_object); 1770 1771 if (bpo->bpo_havesubobj && bpo->bpo_phys->bpo_subobjs != 0) { 1772 mos_obj_refd(bpo->bpo_phys->bpo_subobjs); 1773 for (uint64_t i = 0; i < bpo->bpo_phys->bpo_num_subobjs; i++) { 1774 uint64_t subobj; 1775 bpobj_t subbpo; 1776 int error; 1777 VERIFY0(dmu_read(bpo->bpo_os, 1778 bpo->bpo_phys->bpo_subobjs, 1779 i * sizeof (subobj), sizeof (subobj), &subobj, 0)); 1780 error = bpobj_open(&subbpo, bpo->bpo_os, subobj); 1781 if (error != 0) { 1782 (void) printf("ERROR %u while trying to open " 1783 "subobj id %llu\n", 1784 error, (u_longlong_t)subobj); 1785 continue; 1786 } 1787 bpobj_count_refd(&subbpo); 1788 bpobj_close(&subbpo); 1789 } 1790 } 1791 } 1792 1793 static void 1794 dump_deadlist(dsl_deadlist_t *dl) 1795 { 1796 dsl_deadlist_entry_t *dle; 1797 uint64_t unused; 1798 char bytes[32]; 1799 char comp[32]; 1800 char uncomp[32]; 1801 uint64_t empty_bpobj = 1802 dmu_objset_spa(dl->dl_os)->spa_dsl_pool->dp_empty_bpobj; 1803 1804 /* force the tree to be loaded */ 1805 dsl_deadlist_space_range(dl, 0, UINT64_MAX, &unused, &unused, &unused); 1806 1807 if (dl->dl_oldfmt) { 1808 if (dl->dl_bpobj.bpo_object != empty_bpobj) 1809 bpobj_count_refd(&dl->dl_bpobj); 1810 } else { 1811 mos_obj_refd(dl->dl_object); 1812 for (dle = avl_first(&dl->dl_tree); dle; 1813 dle = AVL_NEXT(&dl->dl_tree, dle)) { 1814 if (dle->dle_bpobj.bpo_object != empty_bpobj) 1815 bpobj_count_refd(&dle->dle_bpobj); 1816 } 1817 } 1818 1819 /* make sure nicenum has enough space */ 1820 CTASSERT(sizeof (bytes) >= NN_NUMBUF_SZ); 1821 CTASSERT(sizeof (comp) >= NN_NUMBUF_SZ); 1822 CTASSERT(sizeof (uncomp) >= NN_NUMBUF_SZ); 1823 1824 if (dump_opt['d'] < 3) 1825 return; 1826 1827 if (dl->dl_oldfmt) { 1828 dump_full_bpobj(&dl->dl_bpobj, "old-format deadlist", 0); 1829 return; 1830 } 1831 1832 zdb_nicenum(dl->dl_phys->dl_used, bytes, sizeof (bytes)); 1833 zdb_nicenum(dl->dl_phys->dl_comp, comp, sizeof (comp)); 1834 zdb_nicenum(dl->dl_phys->dl_uncomp, uncomp, sizeof (uncomp)); 1835 (void) printf("\n Deadlist: %s (%s/%s comp)\n", 1836 bytes, comp, uncomp); 1837 1838 if (dump_opt['d'] < 4) 1839 return; 1840 1841 (void) printf("\n"); 1842 1843 for (dle = avl_first(&dl->dl_tree); dle; 1844 dle = AVL_NEXT(&dl->dl_tree, dle)) { 1845 if (dump_opt['d'] >= 5) { 1846 char buf[128]; 1847 (void) snprintf(buf, sizeof (buf), 1848 "mintxg %llu -> obj %llu", 1849 (longlong_t)dle->dle_mintxg, 1850 (longlong_t)dle->dle_bpobj.bpo_object); 1851 1852 dump_full_bpobj(&dle->dle_bpobj, buf, 0); 1853 } else { 1854 (void) printf("mintxg %llu -> obj %llu\n", 1855 (longlong_t)dle->dle_mintxg, 1856 (longlong_t)dle->dle_bpobj.bpo_object); 1857 } 1858 } 1859 } 1860 1861 static avl_tree_t idx_tree; 1862 static avl_tree_t domain_tree; 1863 static boolean_t fuid_table_loaded; 1864 static objset_t *sa_os = NULL; 1865 static sa_attr_type_t *sa_attr_table = NULL; 1866 1867 static int 1868 open_objset(const char *path, dmu_objset_type_t type, void *tag, objset_t **osp) 1869 { 1870 int err; 1871 uint64_t sa_attrs = 0; 1872 uint64_t version = 0; 1873 1874 VERIFY3P(sa_os, ==, NULL); 1875 err = dmu_objset_own(path, type, B_TRUE, B_FALSE, tag, osp); 1876 if (err != 0) { 1877 (void) fprintf(stderr, "failed to own dataset '%s': %s\n", path, 1878 strerror(err)); 1879 return (err); 1880 } 1881 1882 if (dmu_objset_type(*osp) == DMU_OST_ZFS && !(*osp)->os_encrypted) { 1883 (void) zap_lookup(*osp, MASTER_NODE_OBJ, ZPL_VERSION_STR, 1884 8, 1, &version); 1885 if (version >= ZPL_VERSION_SA) { 1886 (void) zap_lookup(*osp, MASTER_NODE_OBJ, ZFS_SA_ATTRS, 1887 8, 1, &sa_attrs); 1888 } 1889 err = sa_setup(*osp, sa_attrs, zfs_attr_table, ZPL_END, 1890 &sa_attr_table); 1891 if (err != 0) { 1892 (void) fprintf(stderr, "sa_setup failed: %s\n", 1893 strerror(err)); 1894 dmu_objset_disown(*osp, B_FALSE, tag); 1895 *osp = NULL; 1896 } 1897 } 1898 sa_os = *osp; 1899 1900 return (0); 1901 } 1902 1903 static void 1904 close_objset(objset_t *os, void *tag) 1905 { 1906 VERIFY3P(os, ==, sa_os); 1907 if (os->os_sa != NULL) 1908 sa_tear_down(os); 1909 dmu_objset_disown(os, B_FALSE, tag); 1910 sa_attr_table = NULL; 1911 sa_os = NULL; 1912 } 1913 1914 static void 1915 fuid_table_destroy() 1916 { 1917 if (fuid_table_loaded) { 1918 zfs_fuid_table_destroy(&idx_tree, &domain_tree); 1919 fuid_table_loaded = B_FALSE; 1920 } 1921 } 1922 1923 /* 1924 * print uid or gid information. 1925 * For normal POSIX id just the id is printed in decimal format. 1926 * For CIFS files with FUID the fuid is printed in hex followed by 1927 * the domain-rid string. 1928 */ 1929 static void 1930 print_idstr(uint64_t id, const char *id_type) 1931 { 1932 if (FUID_INDEX(id)) { 1933 char *domain; 1934 1935 domain = zfs_fuid_idx_domain(&idx_tree, FUID_INDEX(id)); 1936 (void) printf("\t%s %llx [%s-%d]\n", id_type, 1937 (u_longlong_t)id, domain, (int)FUID_RID(id)); 1938 } else { 1939 (void) printf("\t%s %llu\n", id_type, (u_longlong_t)id); 1940 } 1941 1942 } 1943 1944 static void 1945 dump_uidgid(objset_t *os, uint64_t uid, uint64_t gid) 1946 { 1947 uint32_t uid_idx, gid_idx; 1948 1949 uid_idx = FUID_INDEX(uid); 1950 gid_idx = FUID_INDEX(gid); 1951 1952 /* Load domain table, if not already loaded */ 1953 if (!fuid_table_loaded && (uid_idx || gid_idx)) { 1954 uint64_t fuid_obj; 1955 1956 /* first find the fuid object. It lives in the master node */ 1957 VERIFY(zap_lookup(os, MASTER_NODE_OBJ, ZFS_FUID_TABLES, 1958 8, 1, &fuid_obj) == 0); 1959 zfs_fuid_avl_tree_create(&idx_tree, &domain_tree); 1960 (void) zfs_fuid_table_load(os, fuid_obj, 1961 &idx_tree, &domain_tree); 1962 fuid_table_loaded = B_TRUE; 1963 } 1964 1965 print_idstr(uid, "uid"); 1966 print_idstr(gid, "gid"); 1967 } 1968 1969 /*ARGSUSED*/ 1970 static void 1971 dump_znode(objset_t *os, uint64_t object, void *data, size_t size) 1972 { 1973 char path[MAXPATHLEN * 2]; /* allow for xattr and failure prefix */ 1974 sa_handle_t *hdl; 1975 uint64_t xattr, rdev, gen; 1976 uint64_t uid, gid, mode, fsize, parent, links; 1977 uint64_t pflags; 1978 uint64_t acctm[2], modtm[2], chgtm[2], crtm[2]; 1979 time_t z_crtime, z_atime, z_mtime, z_ctime; 1980 sa_bulk_attr_t bulk[12]; 1981 int idx = 0; 1982 int error; 1983 1984 VERIFY3P(os, ==, sa_os); 1985 if (sa_handle_get(os, object, NULL, SA_HDL_PRIVATE, &hdl)) { 1986 (void) printf("Failed to get handle for SA znode\n"); 1987 return; 1988 } 1989 1990 SA_ADD_BULK_ATTR(bulk, idx, sa_attr_table[ZPL_UID], NULL, &uid, 8); 1991 SA_ADD_BULK_ATTR(bulk, idx, sa_attr_table[ZPL_GID], NULL, &gid, 8); 1992 SA_ADD_BULK_ATTR(bulk, idx, sa_attr_table[ZPL_LINKS], NULL, 1993 &links, 8); 1994 SA_ADD_BULK_ATTR(bulk, idx, sa_attr_table[ZPL_GEN], NULL, &gen, 8); 1995 SA_ADD_BULK_ATTR(bulk, idx, sa_attr_table[ZPL_MODE], NULL, 1996 &mode, 8); 1997 SA_ADD_BULK_ATTR(bulk, idx, sa_attr_table[ZPL_PARENT], 1998 NULL, &parent, 8); 1999 SA_ADD_BULK_ATTR(bulk, idx, sa_attr_table[ZPL_SIZE], NULL, 2000 &fsize, 8); 2001 SA_ADD_BULK_ATTR(bulk, idx, sa_attr_table[ZPL_ATIME], NULL, 2002 acctm, 16); 2003 SA_ADD_BULK_ATTR(bulk, idx, sa_attr_table[ZPL_MTIME], NULL, 2004 modtm, 16); 2005 SA_ADD_BULK_ATTR(bulk, idx, sa_attr_table[ZPL_CRTIME], NULL, 2006 crtm, 16); 2007 SA_ADD_BULK_ATTR(bulk, idx, sa_attr_table[ZPL_CTIME], NULL, 2008 chgtm, 16); 2009 SA_ADD_BULK_ATTR(bulk, idx, sa_attr_table[ZPL_FLAGS], NULL, 2010 &pflags, 8); 2011 2012 if (sa_bulk_lookup(hdl, bulk, idx)) { 2013 (void) sa_handle_destroy(hdl); 2014 return; 2015 } 2016 2017 z_crtime = (time_t)crtm[0]; 2018 z_atime = (time_t)acctm[0]; 2019 z_mtime = (time_t)modtm[0]; 2020 z_ctime = (time_t)chgtm[0]; 2021 2022 if (dump_opt['d'] > 4) { 2023 error = zfs_obj_to_path(os, object, path, sizeof (path)); 2024 if (error == ESTALE) { 2025 (void) snprintf(path, sizeof (path), "on delete queue"); 2026 } else if (error != 0) { 2027 leaked_objects++; 2028 (void) snprintf(path, sizeof (path), 2029 "path not found, possibly leaked"); 2030 } 2031 (void) printf("\tpath %s\n", path); 2032 } 2033 dump_uidgid(os, uid, gid); 2034 (void) printf("\tatime %s", ctime(&z_atime)); 2035 (void) printf("\tmtime %s", ctime(&z_mtime)); 2036 (void) printf("\tctime %s", ctime(&z_ctime)); 2037 (void) printf("\tcrtime %s", ctime(&z_crtime)); 2038 (void) printf("\tgen %llu\n", (u_longlong_t)gen); 2039 (void) printf("\tmode %llo\n", (u_longlong_t)mode); 2040 (void) printf("\tsize %llu\n", (u_longlong_t)fsize); 2041 (void) printf("\tparent %llu\n", (u_longlong_t)parent); 2042 (void) printf("\tlinks %llu\n", (u_longlong_t)links); 2043 (void) printf("\tpflags %llx\n", (u_longlong_t)pflags); 2044 if (sa_lookup(hdl, sa_attr_table[ZPL_XATTR], &xattr, 2045 sizeof (uint64_t)) == 0) 2046 (void) printf("\txattr %llu\n", (u_longlong_t)xattr); 2047 if (sa_lookup(hdl, sa_attr_table[ZPL_RDEV], &rdev, 2048 sizeof (uint64_t)) == 0) 2049 (void) printf("\trdev 0x%016llx\n", (u_longlong_t)rdev); 2050 sa_handle_destroy(hdl); 2051 } 2052 2053 /*ARGSUSED*/ 2054 static void 2055 dump_acl(objset_t *os, uint64_t object, void *data, size_t size) 2056 { 2057 } 2058 2059 /*ARGSUSED*/ 2060 static void 2061 dump_dmu_objset(objset_t *os, uint64_t object, void *data, size_t size) 2062 { 2063 } 2064 2065 2066 static object_viewer_t *object_viewer[DMU_OT_NUMTYPES + 1] = { 2067 dump_none, /* unallocated */ 2068 dump_zap, /* object directory */ 2069 dump_uint64, /* object array */ 2070 dump_none, /* packed nvlist */ 2071 dump_packed_nvlist, /* packed nvlist size */ 2072 dump_none, /* bpobj */ 2073 dump_bpobj, /* bpobj header */ 2074 dump_none, /* SPA space map header */ 2075 dump_none, /* SPA space map */ 2076 dump_none, /* ZIL intent log */ 2077 dump_dnode, /* DMU dnode */ 2078 dump_dmu_objset, /* DMU objset */ 2079 dump_dsl_dir, /* DSL directory */ 2080 dump_zap, /* DSL directory child map */ 2081 dump_zap, /* DSL dataset snap map */ 2082 dump_zap, /* DSL props */ 2083 dump_dsl_dataset, /* DSL dataset */ 2084 dump_znode, /* ZFS znode */ 2085 dump_acl, /* ZFS V0 ACL */ 2086 dump_uint8, /* ZFS plain file */ 2087 dump_zpldir, /* ZFS directory */ 2088 dump_zap, /* ZFS master node */ 2089 dump_zap, /* ZFS delete queue */ 2090 dump_uint8, /* zvol object */ 2091 dump_zap, /* zvol prop */ 2092 dump_uint8, /* other uint8[] */ 2093 dump_uint64, /* other uint64[] */ 2094 dump_zap, /* other ZAP */ 2095 dump_zap, /* persistent error log */ 2096 dump_uint8, /* SPA history */ 2097 dump_history_offsets, /* SPA history offsets */ 2098 dump_zap, /* Pool properties */ 2099 dump_zap, /* DSL permissions */ 2100 dump_acl, /* ZFS ACL */ 2101 dump_uint8, /* ZFS SYSACL */ 2102 dump_none, /* FUID nvlist */ 2103 dump_packed_nvlist, /* FUID nvlist size */ 2104 dump_zap, /* DSL dataset next clones */ 2105 dump_zap, /* DSL scrub queue */ 2106 dump_zap, /* ZFS user/group used */ 2107 dump_zap, /* ZFS user/group quota */ 2108 dump_zap, /* snapshot refcount tags */ 2109 dump_ddt_zap, /* DDT ZAP object */ 2110 dump_zap, /* DDT statistics */ 2111 dump_znode, /* SA object */ 2112 dump_zap, /* SA Master Node */ 2113 dump_sa_attrs, /* SA attribute registration */ 2114 dump_sa_layouts, /* SA attribute layouts */ 2115 dump_zap, /* DSL scrub translations */ 2116 dump_none, /* fake dedup BP */ 2117 dump_zap, /* deadlist */ 2118 dump_none, /* deadlist hdr */ 2119 dump_zap, /* dsl clones */ 2120 dump_bpobj_subobjs, /* bpobj subobjs */ 2121 dump_unknown, /* Unknown type, must be last */ 2122 }; 2123 2124 static void 2125 dump_object(objset_t *os, uint64_t object, int verbosity, int *print_header, 2126 uint64_t *dnode_slots_used) 2127 { 2128 dmu_buf_t *db = NULL; 2129 dmu_object_info_t doi; 2130 dnode_t *dn; 2131 boolean_t dnode_held = B_FALSE; 2132 void *bonus = NULL; 2133 size_t bsize = 0; 2134 char iblk[32], dblk[32], lsize[32], asize[32], fill[32], dnsize[32]; 2135 char bonus_size[32]; 2136 char aux[50]; 2137 int error; 2138 2139 /* make sure nicenum has enough space */ 2140 CTASSERT(sizeof (iblk) >= NN_NUMBUF_SZ); 2141 CTASSERT(sizeof (dblk) >= NN_NUMBUF_SZ); 2142 CTASSERT(sizeof (lsize) >= NN_NUMBUF_SZ); 2143 CTASSERT(sizeof (asize) >= NN_NUMBUF_SZ); 2144 CTASSERT(sizeof (bonus_size) >= NN_NUMBUF_SZ); 2145 2146 if (*print_header) { 2147 (void) printf("\n%10s %3s %5s %5s %5s %6s %5s %6s %s\n", 2148 "Object", "lvl", "iblk", "dblk", "dsize", "dnsize", 2149 "lsize", "%full", "type"); 2150 *print_header = 0; 2151 } 2152 2153 if (object == 0) { 2154 dn = DMU_META_DNODE(os); 2155 dmu_object_info_from_dnode(dn, &doi); 2156 } else { 2157 /* 2158 * Encrypted datasets will have sensitive bonus buffers 2159 * encrypted. Therefore we cannot hold the bonus buffer and 2160 * must hold the dnode itself instead. 2161 */ 2162 error = dmu_object_info(os, object, &doi); 2163 if (error) 2164 fatal("dmu_object_info() failed, errno %u", error); 2165 2166 if (os->os_encrypted && 2167 DMU_OT_IS_ENCRYPTED(doi.doi_bonus_type)) { 2168 error = dnode_hold(os, object, FTAG, &dn); 2169 if (error) 2170 fatal("dnode_hold() failed, errno %u", error); 2171 dnode_held = B_TRUE; 2172 } else { 2173 error = dmu_bonus_hold(os, object, FTAG, &db); 2174 if (error) 2175 fatal("dmu_bonus_hold(%llu) failed, errno %u", 2176 object, error); 2177 bonus = db->db_data; 2178 bsize = db->db_size; 2179 dn = DB_DNODE((dmu_buf_impl_t *)db); 2180 } 2181 } 2182 2183 if (dnode_slots_used != NULL) 2184 *dnode_slots_used = doi.doi_dnodesize / DNODE_MIN_SIZE; 2185 2186 zdb_nicenum(doi.doi_metadata_block_size, iblk, sizeof (iblk)); 2187 zdb_nicenum(doi.doi_data_block_size, dblk, sizeof (dblk)); 2188 zdb_nicenum(doi.doi_max_offset, lsize, sizeof (lsize)); 2189 zdb_nicenum(doi.doi_physical_blocks_512 << 9, asize, sizeof (asize)); 2190 zdb_nicenum(doi.doi_bonus_size, bonus_size, sizeof (bonus_size)); 2191 zdb_nicenum(doi.doi_dnodesize, dnsize, sizeof (dnsize)); 2192 (void) sprintf(fill, "%6.2f", 100.0 * doi.doi_fill_count * 2193 doi.doi_data_block_size / (object == 0 ? DNODES_PER_BLOCK : 1) / 2194 doi.doi_max_offset); 2195 2196 aux[0] = '\0'; 2197 2198 if (doi.doi_checksum != ZIO_CHECKSUM_INHERIT || verbosity >= 6) { 2199 (void) snprintf(aux + strlen(aux), sizeof (aux), " (K=%s)", 2200 ZDB_CHECKSUM_NAME(doi.doi_checksum)); 2201 } 2202 2203 if (doi.doi_compress != ZIO_COMPRESS_INHERIT || verbosity >= 6) { 2204 (void) snprintf(aux + strlen(aux), sizeof (aux), " (Z=%s)", 2205 ZDB_COMPRESS_NAME(doi.doi_compress)); 2206 } 2207 2208 (void) printf("%10" PRIu64 2209 " %3u %5s %5s %5s %5s %5s %6s %s%s\n", 2210 object, doi.doi_indirection, iblk, dblk, 2211 asize, dnsize, lsize, fill, ZDB_OT_NAME(doi.doi_type), aux); 2212 2213 if (doi.doi_bonus_type != DMU_OT_NONE && verbosity > 3) { 2214 (void) printf("%10s %3s %5s %5s %5s %5s %5s %6s %s\n", 2215 "", "", "", "", "", "", bonus_size, "bonus", 2216 ZDB_OT_NAME(doi.doi_bonus_type)); 2217 } 2218 2219 if (verbosity >= 4) { 2220 (void) printf("\tdnode flags: %s%s%s\n", 2221 (dn->dn_phys->dn_flags & DNODE_FLAG_USED_BYTES) ? 2222 "USED_BYTES " : "", 2223 (dn->dn_phys->dn_flags & DNODE_FLAG_USERUSED_ACCOUNTED) ? 2224 "USERUSED_ACCOUNTED " : "", 2225 (dn->dn_phys->dn_flags & DNODE_FLAG_SPILL_BLKPTR) ? 2226 "SPILL_BLKPTR" : ""); 2227 (void) printf("\tdnode maxblkid: %llu\n", 2228 (longlong_t)dn->dn_phys->dn_maxblkid); 2229 2230 if (!dnode_held) { 2231 object_viewer[ZDB_OT_TYPE(doi.doi_bonus_type)](os, 2232 object, bonus, bsize); 2233 } else { 2234 (void) printf("\t\t(bonus encrypted)\n"); 2235 } 2236 2237 if (!os->os_encrypted || !DMU_OT_IS_ENCRYPTED(doi.doi_type)) { 2238 object_viewer[ZDB_OT_TYPE(doi.doi_type)](os, object, 2239 NULL, 0); 2240 } else { 2241 (void) printf("\t\t(object encrypted)\n"); 2242 } 2243 2244 *print_header = 1; 2245 } 2246 2247 if (verbosity >= 5) 2248 dump_indirect(dn); 2249 2250 if (verbosity >= 5) { 2251 /* 2252 * Report the list of segments that comprise the object. 2253 */ 2254 uint64_t start = 0; 2255 uint64_t end; 2256 uint64_t blkfill = 1; 2257 int minlvl = 1; 2258 2259 if (dn->dn_type == DMU_OT_DNODE) { 2260 minlvl = 0; 2261 blkfill = DNODES_PER_BLOCK; 2262 } 2263 2264 for (;;) { 2265 char segsize[32]; 2266 /* make sure nicenum has enough space */ 2267 CTASSERT(sizeof (segsize) >= NN_NUMBUF_SZ); 2268 error = dnode_next_offset(dn, 2269 0, &start, minlvl, blkfill, 0); 2270 if (error) 2271 break; 2272 end = start; 2273 error = dnode_next_offset(dn, 2274 DNODE_FIND_HOLE, &end, minlvl, blkfill, 0); 2275 zdb_nicenum(end - start, segsize, sizeof (segsize)); 2276 (void) printf("\t\tsegment [%016llx, %016llx)" 2277 " size %5s\n", (u_longlong_t)start, 2278 (u_longlong_t)end, segsize); 2279 if (error) 2280 break; 2281 start = end; 2282 } 2283 } 2284 2285 if (db != NULL) 2286 dmu_buf_rele(db, FTAG); 2287 if (dnode_held) 2288 dnode_rele(dn, FTAG); 2289 } 2290 2291 static void 2292 count_dir_mos_objects(dsl_dir_t *dd) 2293 { 2294 mos_obj_refd(dd->dd_object); 2295 mos_obj_refd(dsl_dir_phys(dd)->dd_child_dir_zapobj); 2296 mos_obj_refd(dsl_dir_phys(dd)->dd_deleg_zapobj); 2297 mos_obj_refd(dsl_dir_phys(dd)->dd_props_zapobj); 2298 mos_obj_refd(dsl_dir_phys(dd)->dd_clones); 2299 } 2300 2301 static void 2302 count_ds_mos_objects(dsl_dataset_t *ds) 2303 { 2304 mos_obj_refd(ds->ds_object); 2305 mos_obj_refd(dsl_dataset_phys(ds)->ds_next_clones_obj); 2306 mos_obj_refd(dsl_dataset_phys(ds)->ds_props_obj); 2307 mos_obj_refd(dsl_dataset_phys(ds)->ds_userrefs_obj); 2308 mos_obj_refd(dsl_dataset_phys(ds)->ds_snapnames_zapobj); 2309 2310 if (!dsl_dataset_is_snapshot(ds)) { 2311 count_dir_mos_objects(ds->ds_dir); 2312 } 2313 } 2314 2315 static const char *objset_types[DMU_OST_NUMTYPES] = { 2316 "NONE", "META", "ZPL", "ZVOL", "OTHER", "ANY" }; 2317 2318 static void 2319 dump_dir(objset_t *os) 2320 { 2321 dmu_objset_stats_t dds; 2322 uint64_t object, object_count; 2323 uint64_t refdbytes, usedobjs, scratch; 2324 char numbuf[32]; 2325 char blkbuf[BP_SPRINTF_LEN + 20]; 2326 char osname[ZFS_MAX_DATASET_NAME_LEN]; 2327 const char *type = "UNKNOWN"; 2328 int verbosity = dump_opt['d']; 2329 int print_header = 1; 2330 unsigned i; 2331 int error; 2332 uint64_t total_slots_used = 0; 2333 uint64_t max_slot_used = 0; 2334 uint64_t dnode_slots; 2335 2336 /* make sure nicenum has enough space */ 2337 CTASSERT(sizeof (numbuf) >= NN_NUMBUF_SZ); 2338 2339 dsl_pool_config_enter(dmu_objset_pool(os), FTAG); 2340 dmu_objset_fast_stat(os, &dds); 2341 dsl_pool_config_exit(dmu_objset_pool(os), FTAG); 2342 2343 if (dds.dds_type < DMU_OST_NUMTYPES) 2344 type = objset_types[dds.dds_type]; 2345 2346 if (dds.dds_type == DMU_OST_META) { 2347 dds.dds_creation_txg = TXG_INITIAL; 2348 usedobjs = BP_GET_FILL(os->os_rootbp); 2349 refdbytes = dsl_dir_phys(os->os_spa->spa_dsl_pool->dp_mos_dir)-> 2350 dd_used_bytes; 2351 } else { 2352 dmu_objset_space(os, &refdbytes, &scratch, &usedobjs, &scratch); 2353 } 2354 2355 ASSERT3U(usedobjs, ==, BP_GET_FILL(os->os_rootbp)); 2356 2357 zdb_nicenum(refdbytes, numbuf, sizeof (numbuf)); 2358 2359 if (verbosity >= 4) { 2360 (void) snprintf(blkbuf, sizeof (blkbuf), ", rootbp "); 2361 (void) snprintf_blkptr(blkbuf + strlen(blkbuf), 2362 sizeof (blkbuf) - strlen(blkbuf), os->os_rootbp); 2363 } else { 2364 blkbuf[0] = '\0'; 2365 } 2366 2367 dmu_objset_name(os, osname); 2368 2369 (void) printf("Dataset %s [%s], ID %llu, cr_txg %llu, " 2370 "%s, %llu objects%s%s\n", 2371 osname, type, (u_longlong_t)dmu_objset_id(os), 2372 (u_longlong_t)dds.dds_creation_txg, 2373 numbuf, (u_longlong_t)usedobjs, blkbuf, 2374 (dds.dds_inconsistent) ? " (inconsistent)" : ""); 2375 2376 if (zopt_objects != 0) { 2377 for (i = 0; i < zopt_objects; i++) 2378 dump_object(os, zopt_object[i], verbosity, 2379 &print_header, NULL); 2380 (void) printf("\n"); 2381 return; 2382 } 2383 2384 if (dump_opt['i'] != 0 || verbosity >= 2) 2385 dump_intent_log(dmu_objset_zil(os)); 2386 2387 if (dmu_objset_ds(os) != NULL) { 2388 dsl_dataset_t *ds = dmu_objset_ds(os); 2389 dump_deadlist(&ds->ds_deadlist); 2390 2391 if (dsl_dataset_remap_deadlist_exists(ds)) { 2392 (void) printf("ds_remap_deadlist:\n"); 2393 dump_deadlist(&ds->ds_remap_deadlist); 2394 } 2395 count_ds_mos_objects(ds); 2396 } 2397 2398 if (verbosity < 2) 2399 return; 2400 2401 if (BP_IS_HOLE(os->os_rootbp)) 2402 return; 2403 2404 dump_object(os, 0, verbosity, &print_header, NULL); 2405 object_count = 0; 2406 if (DMU_USERUSED_DNODE(os) != NULL && 2407 DMU_USERUSED_DNODE(os)->dn_type != 0) { 2408 dump_object(os, DMU_USERUSED_OBJECT, verbosity, &print_header, 2409 NULL); 2410 dump_object(os, DMU_GROUPUSED_OBJECT, verbosity, &print_header, 2411 NULL); 2412 } 2413 2414 object = 0; 2415 while ((error = dmu_object_next(os, &object, B_FALSE, 0)) == 0) { 2416 dump_object(os, object, verbosity, &print_header, &dnode_slots); 2417 object_count++; 2418 total_slots_used += dnode_slots; 2419 max_slot_used = object + dnode_slots - 1; 2420 } 2421 2422 (void) printf("\n"); 2423 2424 (void) printf(" Dnode slots:\n"); 2425 (void) printf("\tTotal used: %10llu\n", 2426 (u_longlong_t)total_slots_used); 2427 (void) printf("\tMax used: %10llu\n", 2428 (u_longlong_t)max_slot_used); 2429 (void) printf("\tPercent empty: %10lf\n", 2430 (double)(max_slot_used - total_slots_used)*100 / 2431 (double)max_slot_used); 2432 2433 (void) printf("\n"); 2434 2435 if (error != ESRCH) { 2436 (void) fprintf(stderr, "dmu_object_next() = %d\n", error); 2437 abort(); 2438 } 2439 if (leaked_objects != 0) { 2440 (void) printf("%d potentially leaked objects detected\n", 2441 leaked_objects); 2442 leaked_objects = 0; 2443 } 2444 2445 ASSERT3U(object_count, ==, usedobjs); 2446 } 2447 2448 static void 2449 dump_uberblock(uberblock_t *ub, const char *header, const char *footer) 2450 { 2451 time_t timestamp = ub->ub_timestamp; 2452 2453 (void) printf("%s", header ? header : ""); 2454 (void) printf("\tmagic = %016llx\n", (u_longlong_t)ub->ub_magic); 2455 (void) printf("\tversion = %llu\n", (u_longlong_t)ub->ub_version); 2456 (void) printf("\ttxg = %llu\n", (u_longlong_t)ub->ub_txg); 2457 (void) printf("\tguid_sum = %llu\n", (u_longlong_t)ub->ub_guid_sum); 2458 (void) printf("\ttimestamp = %llu UTC = %s", 2459 (u_longlong_t)ub->ub_timestamp, asctime(localtime(×tamp))); 2460 2461 (void) printf("\tmmp_magic = %016llx\n", 2462 (u_longlong_t)ub->ub_mmp_magic); 2463 if (ub->ub_mmp_magic == MMP_MAGIC) 2464 (void) printf("\tmmp_delay = %0llu\n", 2465 (u_longlong_t)ub->ub_mmp_delay); 2466 2467 if (dump_opt['u'] >= 3) { 2468 char blkbuf[BP_SPRINTF_LEN]; 2469 snprintf_blkptr(blkbuf, sizeof (blkbuf), &ub->ub_rootbp); 2470 (void) printf("\trootbp = %s\n", blkbuf); 2471 } 2472 (void) printf("\tcheckpoint_txg = %llu\n", 2473 (u_longlong_t)ub->ub_checkpoint_txg); 2474 (void) printf("%s", footer ? footer : ""); 2475 } 2476 2477 static void 2478 dump_config(spa_t *spa) 2479 { 2480 dmu_buf_t *db; 2481 size_t nvsize = 0; 2482 int error = 0; 2483 2484 2485 error = dmu_bonus_hold(spa->spa_meta_objset, 2486 spa->spa_config_object, FTAG, &db); 2487 2488 if (error == 0) { 2489 nvsize = *(uint64_t *)db->db_data; 2490 dmu_buf_rele(db, FTAG); 2491 2492 (void) printf("\nMOS Configuration:\n"); 2493 dump_packed_nvlist(spa->spa_meta_objset, 2494 spa->spa_config_object, (void *)&nvsize, 1); 2495 } else { 2496 (void) fprintf(stderr, "dmu_bonus_hold(%llu) failed, errno %d", 2497 (u_longlong_t)spa->spa_config_object, error); 2498 } 2499 } 2500 2501 static void 2502 dump_cachefile(const char *cachefile) 2503 { 2504 int fd; 2505 struct stat64 statbuf; 2506 char *buf; 2507 nvlist_t *config; 2508 2509 if ((fd = open64(cachefile, O_RDONLY)) < 0) { 2510 (void) printf("cannot open '%s': %s\n", cachefile, 2511 strerror(errno)); 2512 exit(1); 2513 } 2514 2515 if (fstat64(fd, &statbuf) != 0) { 2516 (void) printf("failed to stat '%s': %s\n", cachefile, 2517 strerror(errno)); 2518 exit(1); 2519 } 2520 2521 if ((buf = malloc(statbuf.st_size)) == NULL) { 2522 (void) fprintf(stderr, "failed to allocate %llu bytes\n", 2523 (u_longlong_t)statbuf.st_size); 2524 exit(1); 2525 } 2526 2527 if (read(fd, buf, statbuf.st_size) != statbuf.st_size) { 2528 (void) fprintf(stderr, "failed to read %llu bytes\n", 2529 (u_longlong_t)statbuf.st_size); 2530 exit(1); 2531 } 2532 2533 (void) close(fd); 2534 2535 if (nvlist_unpack(buf, statbuf.st_size, &config, 0) != 0) { 2536 (void) fprintf(stderr, "failed to unpack nvlist\n"); 2537 exit(1); 2538 } 2539 2540 free(buf); 2541 2542 dump_nvlist(config, 0); 2543 2544 nvlist_free(config); 2545 } 2546 2547 #define ZDB_MAX_UB_HEADER_SIZE 32 2548 2549 static void 2550 dump_label_uberblocks(vdev_label_t *lbl, uint64_t ashift) 2551 { 2552 vdev_t vd; 2553 vdev_t *vdp = &vd; 2554 char header[ZDB_MAX_UB_HEADER_SIZE]; 2555 2556 vd.vdev_ashift = ashift; 2557 vdp->vdev_top = vdp; 2558 2559 for (int i = 0; i < VDEV_UBERBLOCK_COUNT(vdp); i++) { 2560 uint64_t uoff = VDEV_UBERBLOCK_OFFSET(vdp, i); 2561 uberblock_t *ub = (void *)((char *)lbl + uoff); 2562 2563 if (uberblock_verify(ub)) 2564 continue; 2565 2566 if ((dump_opt['u'] < 4) && 2567 (ub->ub_mmp_magic == MMP_MAGIC) && ub->ub_mmp_delay && 2568 (i >= VDEV_UBERBLOCK_COUNT(&vd) - MMP_BLOCKS_PER_LABEL)) 2569 continue; 2570 2571 (void) snprintf(header, ZDB_MAX_UB_HEADER_SIZE, 2572 "Uberblock[%d]\n", i); 2573 dump_uberblock(ub, header, ""); 2574 } 2575 } 2576 2577 static char curpath[PATH_MAX]; 2578 2579 /* 2580 * Iterate through the path components, recursively passing 2581 * current one's obj and remaining path until we find the obj 2582 * for the last one. 2583 */ 2584 static int 2585 dump_path_impl(objset_t *os, uint64_t obj, char *name) 2586 { 2587 int err; 2588 int header = 1; 2589 uint64_t child_obj; 2590 char *s; 2591 dmu_buf_t *db; 2592 dmu_object_info_t doi; 2593 2594 if ((s = strchr(name, '/')) != NULL) 2595 *s = '\0'; 2596 err = zap_lookup(os, obj, name, 8, 1, &child_obj); 2597 2598 (void) strlcat(curpath, name, sizeof (curpath)); 2599 2600 if (err != 0) { 2601 (void) fprintf(stderr, "failed to lookup %s: %s\n", 2602 curpath, strerror(err)); 2603 return (err); 2604 } 2605 2606 child_obj = ZFS_DIRENT_OBJ(child_obj); 2607 err = sa_buf_hold(os, child_obj, FTAG, &db); 2608 if (err != 0) { 2609 (void) fprintf(stderr, 2610 "failed to get SA dbuf for obj %llu: %s\n", 2611 (u_longlong_t)child_obj, strerror(err)); 2612 return (EINVAL); 2613 } 2614 dmu_object_info_from_db(db, &doi); 2615 sa_buf_rele(db, FTAG); 2616 2617 if (doi.doi_bonus_type != DMU_OT_SA && 2618 doi.doi_bonus_type != DMU_OT_ZNODE) { 2619 (void) fprintf(stderr, "invalid bonus type %d for obj %llu\n", 2620 doi.doi_bonus_type, (u_longlong_t)child_obj); 2621 return (EINVAL); 2622 } 2623 2624 if (dump_opt['v'] > 6) { 2625 (void) printf("obj=%llu %s type=%d bonustype=%d\n", 2626 (u_longlong_t)child_obj, curpath, doi.doi_type, 2627 doi.doi_bonus_type); 2628 } 2629 2630 (void) strlcat(curpath, "/", sizeof (curpath)); 2631 2632 switch (doi.doi_type) { 2633 case DMU_OT_DIRECTORY_CONTENTS: 2634 if (s != NULL && *(s + 1) != '\0') 2635 return (dump_path_impl(os, child_obj, s + 1)); 2636 /*FALLTHROUGH*/ 2637 case DMU_OT_PLAIN_FILE_CONTENTS: 2638 dump_object(os, child_obj, dump_opt['v'], &header, NULL); 2639 return (0); 2640 default: 2641 (void) fprintf(stderr, "object %llu has non-file/directory " 2642 "type %d\n", (u_longlong_t)obj, doi.doi_type); 2643 break; 2644 } 2645 2646 return (EINVAL); 2647 } 2648 2649 /* 2650 * Dump the blocks for the object specified by path inside the dataset. 2651 */ 2652 static int 2653 dump_path(char *ds, char *path) 2654 { 2655 int err; 2656 objset_t *os; 2657 uint64_t root_obj; 2658 2659 err = open_objset(ds, DMU_OST_ZFS, FTAG, &os); 2660 if (err != 0) 2661 return (err); 2662 2663 err = zap_lookup(os, MASTER_NODE_OBJ, ZFS_ROOT_OBJ, 8, 1, &root_obj); 2664 if (err != 0) { 2665 (void) fprintf(stderr, "can't lookup root znode: %s\n", 2666 strerror(err)); 2667 dmu_objset_disown(os, B_FALSE, FTAG); 2668 return (EINVAL); 2669 } 2670 2671 (void) snprintf(curpath, sizeof (curpath), "dataset=%s path=/", ds); 2672 2673 err = dump_path_impl(os, root_obj, path); 2674 2675 close_objset(os, FTAG); 2676 return (err); 2677 } 2678 2679 static int 2680 dump_label(const char *dev) 2681 { 2682 int fd; 2683 vdev_label_t label; 2684 char path[MAXPATHLEN]; 2685 char *buf = label.vl_vdev_phys.vp_nvlist; 2686 size_t buflen = sizeof (label.vl_vdev_phys.vp_nvlist); 2687 struct stat64 statbuf; 2688 uint64_t psize, ashift; 2689 boolean_t label_found = B_FALSE; 2690 2691 (void) strlcpy(path, dev, sizeof (path)); 2692 if (dev[0] == '/') { 2693 if (strncmp(dev, ZFS_DISK_ROOTD, 2694 strlen(ZFS_DISK_ROOTD)) == 0) { 2695 (void) snprintf(path, sizeof (path), "%s%s", 2696 ZFS_RDISK_ROOTD, dev + strlen(ZFS_DISK_ROOTD)); 2697 } 2698 } else if (stat64(path, &statbuf) != 0) { 2699 char *s; 2700 2701 (void) snprintf(path, sizeof (path), "%s%s", ZFS_RDISK_ROOTD, 2702 dev); 2703 if (((s = strrchr(dev, 's')) == NULL && 2704 (s = strchr(dev, 'p')) == NULL) || 2705 !isdigit(*(s + 1))) 2706 (void) strlcat(path, "s0", sizeof (path)); 2707 } 2708 2709 if ((fd = open64(path, O_RDONLY)) < 0) { 2710 (void) fprintf(stderr, "cannot open '%s': %s\n", path, 2711 strerror(errno)); 2712 exit(1); 2713 } 2714 2715 if (fstat64(fd, &statbuf) != 0) { 2716 (void) fprintf(stderr, "failed to stat '%s': %s\n", path, 2717 strerror(errno)); 2718 (void) close(fd); 2719 exit(1); 2720 } 2721 2722 if (S_ISBLK(statbuf.st_mode)) { 2723 (void) fprintf(stderr, 2724 "cannot use '%s': character device required\n", path); 2725 (void) close(fd); 2726 exit(1); 2727 } 2728 2729 psize = statbuf.st_size; 2730 psize = P2ALIGN(psize, (uint64_t)sizeof (vdev_label_t)); 2731 2732 for (int l = 0; l < VDEV_LABELS; l++) { 2733 nvlist_t *config = NULL; 2734 2735 if (!dump_opt['q']) { 2736 (void) printf("------------------------------------\n"); 2737 (void) printf("LABEL %d\n", l); 2738 (void) printf("------------------------------------\n"); 2739 } 2740 2741 if (pread64(fd, &label, sizeof (label), 2742 vdev_label_offset(psize, l, 0)) != sizeof (label)) { 2743 if (!dump_opt['q']) 2744 (void) printf("failed to read label %d\n", l); 2745 continue; 2746 } 2747 2748 if (nvlist_unpack(buf, buflen, &config, 0) != 0) { 2749 if (!dump_opt['q']) 2750 (void) printf("failed to unpack label %d\n", l); 2751 ashift = SPA_MINBLOCKSHIFT; 2752 } else { 2753 nvlist_t *vdev_tree = NULL; 2754 2755 if (!dump_opt['q']) 2756 dump_nvlist(config, 4); 2757 if ((nvlist_lookup_nvlist(config, 2758 ZPOOL_CONFIG_VDEV_TREE, &vdev_tree) != 0) || 2759 (nvlist_lookup_uint64(vdev_tree, 2760 ZPOOL_CONFIG_ASHIFT, &ashift) != 0)) 2761 ashift = SPA_MINBLOCKSHIFT; 2762 nvlist_free(config); 2763 label_found = B_TRUE; 2764 } 2765 if (dump_opt['u']) 2766 dump_label_uberblocks(&label, ashift); 2767 } 2768 2769 (void) close(fd); 2770 2771 return (label_found ? 0 : 2); 2772 } 2773 2774 static uint64_t dataset_feature_count[SPA_FEATURES]; 2775 static uint64_t remap_deadlist_count = 0; 2776 2777 /*ARGSUSED*/ 2778 static int 2779 dump_one_dir(const char *dsname, void *arg) 2780 { 2781 int error; 2782 objset_t *os; 2783 2784 error = open_objset(dsname, DMU_OST_ANY, FTAG, &os); 2785 if (error != 0) 2786 return (0); 2787 2788 for (spa_feature_t f = 0; f < SPA_FEATURES; f++) { 2789 if (!dmu_objset_ds(os)->ds_feature_inuse[f]) 2790 continue; 2791 ASSERT(spa_feature_table[f].fi_flags & 2792 ZFEATURE_FLAG_PER_DATASET); 2793 dataset_feature_count[f]++; 2794 } 2795 2796 if (dsl_dataset_remap_deadlist_exists(dmu_objset_ds(os))) { 2797 remap_deadlist_count++; 2798 } 2799 2800 dump_dir(os); 2801 close_objset(os, FTAG); 2802 fuid_table_destroy(); 2803 return (0); 2804 } 2805 2806 /* 2807 * Block statistics. 2808 */ 2809 #define PSIZE_HISTO_SIZE (SPA_OLD_MAXBLOCKSIZE / SPA_MINBLOCKSIZE + 2) 2810 typedef struct zdb_blkstats { 2811 uint64_t zb_asize; 2812 uint64_t zb_lsize; 2813 uint64_t zb_psize; 2814 uint64_t zb_count; 2815 uint64_t zb_gangs; 2816 uint64_t zb_ditto_samevdev; 2817 uint64_t zb_ditto_same_ms; 2818 uint64_t zb_psize_histogram[PSIZE_HISTO_SIZE]; 2819 } zdb_blkstats_t; 2820 2821 /* 2822 * Extended object types to report deferred frees and dedup auto-ditto blocks. 2823 */ 2824 #define ZDB_OT_DEFERRED (DMU_OT_NUMTYPES + 0) 2825 #define ZDB_OT_DITTO (DMU_OT_NUMTYPES + 1) 2826 #define ZDB_OT_OTHER (DMU_OT_NUMTYPES + 2) 2827 #define ZDB_OT_TOTAL (DMU_OT_NUMTYPES + 3) 2828 2829 static const char *zdb_ot_extname[] = { 2830 "deferred free", 2831 "dedup ditto", 2832 "other", 2833 "Total", 2834 }; 2835 2836 #define ZB_TOTAL DN_MAX_LEVELS 2837 2838 typedef struct zdb_cb { 2839 zdb_blkstats_t zcb_type[ZB_TOTAL + 1][ZDB_OT_TOTAL + 1]; 2840 uint64_t zcb_removing_size; 2841 uint64_t zcb_checkpoint_size; 2842 uint64_t zcb_dedup_asize; 2843 uint64_t zcb_dedup_blocks; 2844 uint64_t zcb_embedded_blocks[NUM_BP_EMBEDDED_TYPES]; 2845 uint64_t zcb_embedded_histogram[NUM_BP_EMBEDDED_TYPES] 2846 [BPE_PAYLOAD_SIZE]; 2847 uint64_t zcb_start; 2848 hrtime_t zcb_lastprint; 2849 uint64_t zcb_totalasize; 2850 uint64_t zcb_errors[256]; 2851 int zcb_readfails; 2852 int zcb_haderrors; 2853 spa_t *zcb_spa; 2854 uint32_t **zcb_vd_obsolete_counts; 2855 } zdb_cb_t; 2856 2857 /* test if two DVA offsets from same vdev are within the same metaslab */ 2858 static boolean_t 2859 same_metaslab(spa_t *spa, uint64_t vdev, uint64_t off1, uint64_t off2) 2860 { 2861 vdev_t *vd = vdev_lookup_top(spa, vdev); 2862 uint64_t ms_shift = vd->vdev_ms_shift; 2863 2864 return ((off1 >> ms_shift) == (off2 >> ms_shift)); 2865 } 2866 2867 static void 2868 zdb_count_block(zdb_cb_t *zcb, zilog_t *zilog, const blkptr_t *bp, 2869 dmu_object_type_t type) 2870 { 2871 uint64_t refcnt = 0; 2872 2873 ASSERT(type < ZDB_OT_TOTAL); 2874 2875 if (zilog && zil_bp_tree_add(zilog, bp) != 0) 2876 return; 2877 2878 spa_config_enter(zcb->zcb_spa, SCL_CONFIG, FTAG, RW_READER); 2879 2880 for (int i = 0; i < 4; i++) { 2881 int l = (i < 2) ? BP_GET_LEVEL(bp) : ZB_TOTAL; 2882 int t = (i & 1) ? type : ZDB_OT_TOTAL; 2883 int equal; 2884 zdb_blkstats_t *zb = &zcb->zcb_type[l][t]; 2885 2886 zb->zb_asize += BP_GET_ASIZE(bp); 2887 zb->zb_lsize += BP_GET_LSIZE(bp); 2888 zb->zb_psize += BP_GET_PSIZE(bp); 2889 zb->zb_count++; 2890 2891 /* 2892 * The histogram is only big enough to record blocks up to 2893 * SPA_OLD_MAXBLOCKSIZE; larger blocks go into the last, 2894 * "other", bucket. 2895 */ 2896 unsigned idx = BP_GET_PSIZE(bp) >> SPA_MINBLOCKSHIFT; 2897 idx = MIN(idx, SPA_OLD_MAXBLOCKSIZE / SPA_MINBLOCKSIZE + 1); 2898 zb->zb_psize_histogram[idx]++; 2899 2900 zb->zb_gangs += BP_COUNT_GANG(bp); 2901 2902 switch (BP_GET_NDVAS(bp)) { 2903 case 2: 2904 if (DVA_GET_VDEV(&bp->blk_dva[0]) == 2905 DVA_GET_VDEV(&bp->blk_dva[1])) { 2906 zb->zb_ditto_samevdev++; 2907 2908 if (same_metaslab(zcb->zcb_spa, 2909 DVA_GET_VDEV(&bp->blk_dva[0]), 2910 DVA_GET_OFFSET(&bp->blk_dva[0]), 2911 DVA_GET_OFFSET(&bp->blk_dva[1]))) 2912 zb->zb_ditto_same_ms++; 2913 } 2914 break; 2915 case 3: 2916 equal = (DVA_GET_VDEV(&bp->blk_dva[0]) == 2917 DVA_GET_VDEV(&bp->blk_dva[1])) + 2918 (DVA_GET_VDEV(&bp->blk_dva[0]) == 2919 DVA_GET_VDEV(&bp->blk_dva[2])) + 2920 (DVA_GET_VDEV(&bp->blk_dva[1]) == 2921 DVA_GET_VDEV(&bp->blk_dva[2])); 2922 if (equal != 0) { 2923 zb->zb_ditto_samevdev++; 2924 2925 if (DVA_GET_VDEV(&bp->blk_dva[0]) == 2926 DVA_GET_VDEV(&bp->blk_dva[1]) && 2927 same_metaslab(zcb->zcb_spa, 2928 DVA_GET_VDEV(&bp->blk_dva[0]), 2929 DVA_GET_OFFSET(&bp->blk_dva[0]), 2930 DVA_GET_OFFSET(&bp->blk_dva[1]))) 2931 zb->zb_ditto_same_ms++; 2932 else if (DVA_GET_VDEV(&bp->blk_dva[0]) == 2933 DVA_GET_VDEV(&bp->blk_dva[2]) && 2934 same_metaslab(zcb->zcb_spa, 2935 DVA_GET_VDEV(&bp->blk_dva[0]), 2936 DVA_GET_OFFSET(&bp->blk_dva[0]), 2937 DVA_GET_OFFSET(&bp->blk_dva[2]))) 2938 zb->zb_ditto_same_ms++; 2939 else if (DVA_GET_VDEV(&bp->blk_dva[1]) == 2940 DVA_GET_VDEV(&bp->blk_dva[2]) && 2941 same_metaslab(zcb->zcb_spa, 2942 DVA_GET_VDEV(&bp->blk_dva[1]), 2943 DVA_GET_OFFSET(&bp->blk_dva[1]), 2944 DVA_GET_OFFSET(&bp->blk_dva[2]))) 2945 zb->zb_ditto_same_ms++; 2946 } 2947 break; 2948 } 2949 } 2950 2951 spa_config_exit(zcb->zcb_spa, SCL_CONFIG, FTAG); 2952 2953 if (BP_IS_EMBEDDED(bp)) { 2954 zcb->zcb_embedded_blocks[BPE_GET_ETYPE(bp)]++; 2955 zcb->zcb_embedded_histogram[BPE_GET_ETYPE(bp)] 2956 [BPE_GET_PSIZE(bp)]++; 2957 return; 2958 } 2959 2960 if (dump_opt['L']) 2961 return; 2962 2963 if (BP_GET_DEDUP(bp)) { 2964 ddt_t *ddt; 2965 ddt_entry_t *dde; 2966 2967 ddt = ddt_select(zcb->zcb_spa, bp); 2968 ddt_enter(ddt); 2969 dde = ddt_lookup(ddt, bp, B_FALSE); 2970 2971 if (dde == NULL) { 2972 refcnt = 0; 2973 } else { 2974 ddt_phys_t *ddp = ddt_phys_select(dde, bp); 2975 ddt_phys_decref(ddp); 2976 refcnt = ddp->ddp_refcnt; 2977 if (ddt_phys_total_refcnt(dde) == 0) 2978 ddt_remove(ddt, dde); 2979 } 2980 ddt_exit(ddt); 2981 } 2982 2983 VERIFY3U(zio_wait(zio_claim(NULL, zcb->zcb_spa, 2984 refcnt ? 0 : spa_min_claim_txg(zcb->zcb_spa), 2985 bp, NULL, NULL, ZIO_FLAG_CANFAIL)), ==, 0); 2986 } 2987 2988 static void 2989 zdb_blkptr_done(zio_t *zio) 2990 { 2991 spa_t *spa = zio->io_spa; 2992 blkptr_t *bp = zio->io_bp; 2993 int ioerr = zio->io_error; 2994 zdb_cb_t *zcb = zio->io_private; 2995 zbookmark_phys_t *zb = &zio->io_bookmark; 2996 2997 abd_free(zio->io_abd); 2998 2999 mutex_enter(&spa->spa_scrub_lock); 3000 spa->spa_load_verify_ios--; 3001 cv_broadcast(&spa->spa_scrub_io_cv); 3002 3003 if (ioerr && !(zio->io_flags & ZIO_FLAG_SPECULATIVE)) { 3004 char blkbuf[BP_SPRINTF_LEN]; 3005 3006 zcb->zcb_haderrors = 1; 3007 zcb->zcb_errors[ioerr]++; 3008 3009 if (dump_opt['b'] >= 2) 3010 snprintf_blkptr(blkbuf, sizeof (blkbuf), bp); 3011 else 3012 blkbuf[0] = '\0'; 3013 3014 (void) printf("zdb_blkptr_cb: " 3015 "Got error %d reading " 3016 "<%llu, %llu, %lld, %llx> %s -- skipping\n", 3017 ioerr, 3018 (u_longlong_t)zb->zb_objset, 3019 (u_longlong_t)zb->zb_object, 3020 (u_longlong_t)zb->zb_level, 3021 (u_longlong_t)zb->zb_blkid, 3022 blkbuf); 3023 } 3024 mutex_exit(&spa->spa_scrub_lock); 3025 } 3026 3027 static int 3028 zdb_blkptr_cb(spa_t *spa, zilog_t *zilog, const blkptr_t *bp, 3029 const zbookmark_phys_t *zb, const dnode_phys_t *dnp, void *arg) 3030 { 3031 zdb_cb_t *zcb = arg; 3032 dmu_object_type_t type; 3033 boolean_t is_metadata; 3034 3035 if (bp == NULL) 3036 return (0); 3037 3038 if (dump_opt['b'] >= 5 && bp->blk_birth > 0) { 3039 char blkbuf[BP_SPRINTF_LEN]; 3040 snprintf_blkptr(blkbuf, sizeof (blkbuf), bp); 3041 (void) printf("objset %llu object %llu " 3042 "level %lld offset 0x%llx %s\n", 3043 (u_longlong_t)zb->zb_objset, 3044 (u_longlong_t)zb->zb_object, 3045 (longlong_t)zb->zb_level, 3046 (u_longlong_t)blkid2offset(dnp, bp, zb), 3047 blkbuf); 3048 } 3049 3050 if (BP_IS_HOLE(bp)) 3051 return (0); 3052 3053 type = BP_GET_TYPE(bp); 3054 3055 zdb_count_block(zcb, zilog, bp, 3056 (type & DMU_OT_NEWTYPE) ? ZDB_OT_OTHER : type); 3057 3058 is_metadata = (BP_GET_LEVEL(bp) != 0 || DMU_OT_IS_METADATA(type)); 3059 3060 if (!BP_IS_EMBEDDED(bp) && 3061 (dump_opt['c'] > 1 || (dump_opt['c'] && is_metadata))) { 3062 size_t size = BP_GET_PSIZE(bp); 3063 abd_t *abd = abd_alloc(size, B_FALSE); 3064 int flags = ZIO_FLAG_CANFAIL | ZIO_FLAG_SCRUB | ZIO_FLAG_RAW; 3065 3066 /* If it's an intent log block, failure is expected. */ 3067 if (zb->zb_level == ZB_ZIL_LEVEL) 3068 flags |= ZIO_FLAG_SPECULATIVE; 3069 3070 mutex_enter(&spa->spa_scrub_lock); 3071 while (spa->spa_load_verify_ios > max_inflight) 3072 cv_wait(&spa->spa_scrub_io_cv, &spa->spa_scrub_lock); 3073 spa->spa_load_verify_ios++; 3074 mutex_exit(&spa->spa_scrub_lock); 3075 3076 zio_nowait(zio_read(NULL, spa, bp, abd, size, 3077 zdb_blkptr_done, zcb, ZIO_PRIORITY_ASYNC_READ, flags, zb)); 3078 } 3079 3080 zcb->zcb_readfails = 0; 3081 3082 /* only call gethrtime() every 100 blocks */ 3083 static int iters; 3084 if (++iters > 100) 3085 iters = 0; 3086 else 3087 return (0); 3088 3089 if (dump_opt['b'] < 5 && gethrtime() > zcb->zcb_lastprint + NANOSEC) { 3090 uint64_t now = gethrtime(); 3091 char buf[10]; 3092 uint64_t bytes = zcb->zcb_type[ZB_TOTAL][ZDB_OT_TOTAL].zb_asize; 3093 int kb_per_sec = 3094 1 + bytes / (1 + ((now - zcb->zcb_start) / 1000 / 1000)); 3095 int sec_remaining = 3096 (zcb->zcb_totalasize - bytes) / 1024 / kb_per_sec; 3097 3098 /* make sure nicenum has enough space */ 3099 CTASSERT(sizeof (buf) >= NN_NUMBUF_SZ); 3100 3101 zfs_nicenum(bytes, buf, sizeof (buf)); 3102 (void) fprintf(stderr, 3103 "\r%5s completed (%4dMB/s) " 3104 "estimated time remaining: %uhr %02umin %02usec ", 3105 buf, kb_per_sec / 1024, 3106 sec_remaining / 60 / 60, 3107 sec_remaining / 60 % 60, 3108 sec_remaining % 60); 3109 3110 zcb->zcb_lastprint = now; 3111 } 3112 3113 return (0); 3114 } 3115 3116 static void 3117 zdb_leak(void *arg, uint64_t start, uint64_t size) 3118 { 3119 vdev_t *vd = arg; 3120 3121 (void) printf("leaked space: vdev %llu, offset 0x%llx, size %llu\n", 3122 (u_longlong_t)vd->vdev_id, (u_longlong_t)start, (u_longlong_t)size); 3123 } 3124 3125 static metaslab_ops_t zdb_metaslab_ops = { 3126 NULL /* alloc */ 3127 }; 3128 3129 static void 3130 zdb_ddt_leak_init(spa_t *spa, zdb_cb_t *zcb) 3131 { 3132 ddt_bookmark_t ddb; 3133 ddt_entry_t dde; 3134 int error; 3135 3136 ASSERT(!dump_opt['L']); 3137 3138 bzero(&ddb, sizeof (ddb)); 3139 while ((error = ddt_walk(spa, &ddb, &dde)) == 0) { 3140 blkptr_t blk; 3141 ddt_phys_t *ddp = dde.dde_phys; 3142 3143 if (ddb.ddb_class == DDT_CLASS_UNIQUE) 3144 return; 3145 3146 ASSERT(ddt_phys_total_refcnt(&dde) > 1); 3147 3148 for (int p = 0; p < DDT_PHYS_TYPES; p++, ddp++) { 3149 if (ddp->ddp_phys_birth == 0) 3150 continue; 3151 ddt_bp_create(ddb.ddb_checksum, 3152 &dde.dde_key, ddp, &blk); 3153 if (p == DDT_PHYS_DITTO) { 3154 zdb_count_block(zcb, NULL, &blk, ZDB_OT_DITTO); 3155 } else { 3156 zcb->zcb_dedup_asize += 3157 BP_GET_ASIZE(&blk) * (ddp->ddp_refcnt - 1); 3158 zcb->zcb_dedup_blocks++; 3159 } 3160 } 3161 ddt_t *ddt = spa->spa_ddt[ddb.ddb_checksum]; 3162 ddt_enter(ddt); 3163 VERIFY(ddt_lookup(ddt, &blk, B_TRUE) != NULL); 3164 ddt_exit(ddt); 3165 } 3166 3167 ASSERT(error == ENOENT); 3168 } 3169 3170 /* ARGSUSED */ 3171 static void 3172 claim_segment_impl_cb(uint64_t inner_offset, vdev_t *vd, uint64_t offset, 3173 uint64_t size, void *arg) 3174 { 3175 /* 3176 * This callback was called through a remap from 3177 * a device being removed. Therefore, the vdev that 3178 * this callback is applied to is a concrete 3179 * vdev. 3180 */ 3181 ASSERT(vdev_is_concrete(vd)); 3182 3183 VERIFY0(metaslab_claim_impl(vd, offset, size, 3184 spa_min_claim_txg(vd->vdev_spa))); 3185 } 3186 3187 static void 3188 claim_segment_cb(void *arg, uint64_t offset, uint64_t size) 3189 { 3190 vdev_t *vd = arg; 3191 3192 vdev_indirect_ops.vdev_op_remap(vd, offset, size, 3193 claim_segment_impl_cb, NULL); 3194 } 3195 3196 /* 3197 * After accounting for all allocated blocks that are directly referenced, 3198 * we might have missed a reference to a block from a partially complete 3199 * (and thus unused) indirect mapping object. We perform a secondary pass 3200 * through the metaslabs we have already mapped and claim the destination 3201 * blocks. 3202 */ 3203 static void 3204 zdb_claim_removing(spa_t *spa, zdb_cb_t *zcb) 3205 { 3206 if (dump_opt['L']) 3207 return; 3208 3209 if (spa->spa_vdev_removal == NULL) 3210 return; 3211 3212 spa_config_enter(spa, SCL_CONFIG, FTAG, RW_READER); 3213 3214 spa_vdev_removal_t *svr = spa->spa_vdev_removal; 3215 vdev_t *vd = vdev_lookup_top(spa, svr->svr_vdev_id); 3216 vdev_indirect_mapping_t *vim = vd->vdev_indirect_mapping; 3217 3218 for (uint64_t msi = 0; msi < vd->vdev_ms_count; msi++) { 3219 metaslab_t *msp = vd->vdev_ms[msi]; 3220 3221 if (msp->ms_start >= vdev_indirect_mapping_max_offset(vim)) 3222 break; 3223 3224 ASSERT0(range_tree_space(svr->svr_allocd_segs)); 3225 3226 if (msp->ms_sm != NULL) { 3227 VERIFY0(space_map_load(msp->ms_sm, 3228 svr->svr_allocd_segs, SM_ALLOC)); 3229 3230 /* 3231 * Clear everything past what has been synced unless 3232 * it's past the spacemap, because we have not allocated 3233 * mappings for it yet. 3234 */ 3235 uint64_t vim_max_offset = 3236 vdev_indirect_mapping_max_offset(vim); 3237 uint64_t sm_end = msp->ms_sm->sm_start + 3238 msp->ms_sm->sm_size; 3239 if (sm_end > vim_max_offset) 3240 range_tree_clear(svr->svr_allocd_segs, 3241 vim_max_offset, sm_end - vim_max_offset); 3242 } 3243 3244 zcb->zcb_removing_size += 3245 range_tree_space(svr->svr_allocd_segs); 3246 range_tree_vacate(svr->svr_allocd_segs, claim_segment_cb, vd); 3247 } 3248 3249 spa_config_exit(spa, SCL_CONFIG, FTAG); 3250 } 3251 3252 /* ARGSUSED */ 3253 static int 3254 increment_indirect_mapping_cb(void *arg, const blkptr_t *bp, dmu_tx_t *tx) 3255 { 3256 zdb_cb_t *zcb = arg; 3257 spa_t *spa = zcb->zcb_spa; 3258 vdev_t *vd; 3259 const dva_t *dva = &bp->blk_dva[0]; 3260 3261 ASSERT(!dump_opt['L']); 3262 ASSERT3U(BP_GET_NDVAS(bp), ==, 1); 3263 3264 spa_config_enter(spa, SCL_VDEV, FTAG, RW_READER); 3265 vd = vdev_lookup_top(zcb->zcb_spa, DVA_GET_VDEV(dva)); 3266 ASSERT3P(vd, !=, NULL); 3267 spa_config_exit(spa, SCL_VDEV, FTAG); 3268 3269 ASSERT(vd->vdev_indirect_config.vic_mapping_object != 0); 3270 ASSERT3P(zcb->zcb_vd_obsolete_counts[vd->vdev_id], !=, NULL); 3271 3272 vdev_indirect_mapping_increment_obsolete_count( 3273 vd->vdev_indirect_mapping, 3274 DVA_GET_OFFSET(dva), DVA_GET_ASIZE(dva), 3275 zcb->zcb_vd_obsolete_counts[vd->vdev_id]); 3276 3277 return (0); 3278 } 3279 3280 static uint32_t * 3281 zdb_load_obsolete_counts(vdev_t *vd) 3282 { 3283 vdev_indirect_mapping_t *vim = vd->vdev_indirect_mapping; 3284 spa_t *spa = vd->vdev_spa; 3285 spa_condensing_indirect_phys_t *scip = 3286 &spa->spa_condensing_indirect_phys; 3287 uint32_t *counts; 3288 3289 EQUIV(vdev_obsolete_sm_object(vd) != 0, vd->vdev_obsolete_sm != NULL); 3290 counts = vdev_indirect_mapping_load_obsolete_counts(vim); 3291 if (vd->vdev_obsolete_sm != NULL) { 3292 vdev_indirect_mapping_load_obsolete_spacemap(vim, counts, 3293 vd->vdev_obsolete_sm); 3294 } 3295 if (scip->scip_vdev == vd->vdev_id && 3296 scip->scip_prev_obsolete_sm_object != 0) { 3297 space_map_t *prev_obsolete_sm = NULL; 3298 VERIFY0(space_map_open(&prev_obsolete_sm, spa->spa_meta_objset, 3299 scip->scip_prev_obsolete_sm_object, 0, vd->vdev_asize, 0)); 3300 vdev_indirect_mapping_load_obsolete_spacemap(vim, counts, 3301 prev_obsolete_sm); 3302 space_map_close(prev_obsolete_sm); 3303 } 3304 return (counts); 3305 } 3306 3307 typedef struct checkpoint_sm_exclude_entry_arg { 3308 vdev_t *cseea_vd; 3309 uint64_t cseea_checkpoint_size; 3310 } checkpoint_sm_exclude_entry_arg_t; 3311 3312 static int 3313 checkpoint_sm_exclude_entry_cb(space_map_entry_t *sme, void *arg) 3314 { 3315 checkpoint_sm_exclude_entry_arg_t *cseea = arg; 3316 vdev_t *vd = cseea->cseea_vd; 3317 metaslab_t *ms = vd->vdev_ms[sme->sme_offset >> vd->vdev_ms_shift]; 3318 uint64_t end = sme->sme_offset + sme->sme_run; 3319 3320 ASSERT(sme->sme_type == SM_FREE); 3321 3322 /* 3323 * Since the vdev_checkpoint_sm exists in the vdev level 3324 * and the ms_sm space maps exist in the metaslab level, 3325 * an entry in the checkpoint space map could theoretically 3326 * cross the boundaries of the metaslab that it belongs. 3327 * 3328 * In reality, because of the way that we populate and 3329 * manipulate the checkpoint's space maps currently, 3330 * there shouldn't be any entries that cross metaslabs. 3331 * Hence the assertion below. 3332 * 3333 * That said, there is no fundamental requirement that 3334 * the checkpoint's space map entries should not cross 3335 * metaslab boundaries. So if needed we could add code 3336 * that handles metaslab-crossing segments in the future. 3337 */ 3338 VERIFY3U(sme->sme_offset, >=, ms->ms_start); 3339 VERIFY3U(end, <=, ms->ms_start + ms->ms_size); 3340 3341 /* 3342 * By removing the entry from the allocated segments we 3343 * also verify that the entry is there to begin with. 3344 */ 3345 mutex_enter(&ms->ms_lock); 3346 range_tree_remove(ms->ms_allocatable, sme->sme_offset, sme->sme_run); 3347 mutex_exit(&ms->ms_lock); 3348 3349 cseea->cseea_checkpoint_size += sme->sme_run; 3350 return (0); 3351 } 3352 3353 static void 3354 zdb_leak_init_vdev_exclude_checkpoint(vdev_t *vd, zdb_cb_t *zcb) 3355 { 3356 spa_t *spa = vd->vdev_spa; 3357 space_map_t *checkpoint_sm = NULL; 3358 uint64_t checkpoint_sm_obj; 3359 3360 /* 3361 * If there is no vdev_top_zap, we are in a pool whose 3362 * version predates the pool checkpoint feature. 3363 */ 3364 if (vd->vdev_top_zap == 0) 3365 return; 3366 3367 /* 3368 * If there is no reference of the vdev_checkpoint_sm in 3369 * the vdev_top_zap, then one of the following scenarios 3370 * is true: 3371 * 3372 * 1] There is no checkpoint 3373 * 2] There is a checkpoint, but no checkpointed blocks 3374 * have been freed yet 3375 * 3] The current vdev is indirect 3376 * 3377 * In these cases we return immediately. 3378 */ 3379 if (zap_contains(spa_meta_objset(spa), vd->vdev_top_zap, 3380 VDEV_TOP_ZAP_POOL_CHECKPOINT_SM) != 0) 3381 return; 3382 3383 VERIFY0(zap_lookup(spa_meta_objset(spa), vd->vdev_top_zap, 3384 VDEV_TOP_ZAP_POOL_CHECKPOINT_SM, sizeof (uint64_t), 1, 3385 &checkpoint_sm_obj)); 3386 3387 checkpoint_sm_exclude_entry_arg_t cseea; 3388 cseea.cseea_vd = vd; 3389 cseea.cseea_checkpoint_size = 0; 3390 3391 VERIFY0(space_map_open(&checkpoint_sm, spa_meta_objset(spa), 3392 checkpoint_sm_obj, 0, vd->vdev_asize, vd->vdev_ashift)); 3393 3394 VERIFY0(space_map_iterate(checkpoint_sm, 3395 space_map_length(checkpoint_sm), 3396 checkpoint_sm_exclude_entry_cb, &cseea)); 3397 space_map_close(checkpoint_sm); 3398 3399 zcb->zcb_checkpoint_size += cseea.cseea_checkpoint_size; 3400 } 3401 3402 static void 3403 zdb_leak_init_exclude_checkpoint(spa_t *spa, zdb_cb_t *zcb) 3404 { 3405 ASSERT(!dump_opt['L']); 3406 3407 vdev_t *rvd = spa->spa_root_vdev; 3408 for (uint64_t c = 0; c < rvd->vdev_children; c++) { 3409 ASSERT3U(c, ==, rvd->vdev_child[c]->vdev_id); 3410 zdb_leak_init_vdev_exclude_checkpoint(rvd->vdev_child[c], zcb); 3411 } 3412 } 3413 3414 static void 3415 load_concrete_ms_allocatable_trees(spa_t *spa, maptype_t maptype) 3416 { 3417 vdev_t *rvd = spa->spa_root_vdev; 3418 for (uint64_t i = 0; i < rvd->vdev_children; i++) { 3419 vdev_t *vd = rvd->vdev_child[i]; 3420 3421 ASSERT3U(i, ==, vd->vdev_id); 3422 3423 if (vd->vdev_ops == &vdev_indirect_ops) 3424 continue; 3425 3426 for (uint64_t m = 0; m < vd->vdev_ms_count; m++) { 3427 metaslab_t *msp = vd->vdev_ms[m]; 3428 3429 (void) fprintf(stderr, 3430 "\rloading concrete vdev %llu, " 3431 "metaslab %llu of %llu ...", 3432 (longlong_t)vd->vdev_id, 3433 (longlong_t)msp->ms_id, 3434 (longlong_t)vd->vdev_ms_count); 3435 3436 mutex_enter(&msp->ms_lock); 3437 metaslab_unload(msp); 3438 3439 /* 3440 * We don't want to spend the CPU manipulating the 3441 * size-ordered tree, so clear the range_tree ops. 3442 */ 3443 msp->ms_allocatable->rt_ops = NULL; 3444 3445 if (msp->ms_sm != NULL) { 3446 VERIFY0(space_map_load(msp->ms_sm, 3447 msp->ms_allocatable, maptype)); 3448 } 3449 if (!msp->ms_loaded) 3450 msp->ms_loaded = B_TRUE; 3451 mutex_exit(&msp->ms_lock); 3452 } 3453 } 3454 } 3455 3456 /* 3457 * vm_idxp is an in-out parameter which (for indirect vdevs) is the 3458 * index in vim_entries that has the first entry in this metaslab. 3459 * On return, it will be set to the first entry after this metaslab. 3460 */ 3461 static void 3462 load_indirect_ms_allocatable_tree(vdev_t *vd, metaslab_t *msp, 3463 uint64_t *vim_idxp) 3464 { 3465 vdev_indirect_mapping_t *vim = vd->vdev_indirect_mapping; 3466 3467 mutex_enter(&msp->ms_lock); 3468 metaslab_unload(msp); 3469 3470 /* 3471 * We don't want to spend the CPU manipulating the 3472 * size-ordered tree, so clear the range_tree ops. 3473 */ 3474 msp->ms_allocatable->rt_ops = NULL; 3475 3476 for (; *vim_idxp < vdev_indirect_mapping_num_entries(vim); 3477 (*vim_idxp)++) { 3478 vdev_indirect_mapping_entry_phys_t *vimep = 3479 &vim->vim_entries[*vim_idxp]; 3480 uint64_t ent_offset = DVA_MAPPING_GET_SRC_OFFSET(vimep); 3481 uint64_t ent_len = DVA_GET_ASIZE(&vimep->vimep_dst); 3482 ASSERT3U(ent_offset, >=, msp->ms_start); 3483 if (ent_offset >= msp->ms_start + msp->ms_size) 3484 break; 3485 3486 /* 3487 * Mappings do not cross metaslab boundaries, 3488 * because we create them by walking the metaslabs. 3489 */ 3490 ASSERT3U(ent_offset + ent_len, <=, 3491 msp->ms_start + msp->ms_size); 3492 range_tree_add(msp->ms_allocatable, ent_offset, ent_len); 3493 } 3494 3495 if (!msp->ms_loaded) 3496 msp->ms_loaded = B_TRUE; 3497 mutex_exit(&msp->ms_lock); 3498 } 3499 3500 static void 3501 zdb_leak_init_prepare_indirect_vdevs(spa_t *spa, zdb_cb_t *zcb) 3502 { 3503 ASSERT(!dump_opt['L']); 3504 3505 vdev_t *rvd = spa->spa_root_vdev; 3506 for (uint64_t c = 0; c < rvd->vdev_children; c++) { 3507 vdev_t *vd = rvd->vdev_child[c]; 3508 3509 ASSERT3U(c, ==, vd->vdev_id); 3510 3511 if (vd->vdev_ops != &vdev_indirect_ops) 3512 continue; 3513 3514 /* 3515 * Note: we don't check for mapping leaks on 3516 * removing vdevs because their ms_allocatable's 3517 * are used to look for leaks in allocated space. 3518 */ 3519 zcb->zcb_vd_obsolete_counts[c] = zdb_load_obsolete_counts(vd); 3520 3521 /* 3522 * Normally, indirect vdevs don't have any 3523 * metaslabs. We want to set them up for 3524 * zio_claim(). 3525 */ 3526 VERIFY0(vdev_metaslab_init(vd, 0)); 3527 3528 vdev_indirect_mapping_t *vim = vd->vdev_indirect_mapping; 3529 uint64_t vim_idx = 0; 3530 for (uint64_t m = 0; m < vd->vdev_ms_count; m++) { 3531 3532 (void) fprintf(stderr, 3533 "\rloading indirect vdev %llu, " 3534 "metaslab %llu of %llu ...", 3535 (longlong_t)vd->vdev_id, 3536 (longlong_t)vd->vdev_ms[m]->ms_id, 3537 (longlong_t)vd->vdev_ms_count); 3538 3539 load_indirect_ms_allocatable_tree(vd, vd->vdev_ms[m], 3540 &vim_idx); 3541 } 3542 ASSERT3U(vim_idx, ==, vdev_indirect_mapping_num_entries(vim)); 3543 } 3544 } 3545 3546 static void 3547 zdb_leak_init(spa_t *spa, zdb_cb_t *zcb) 3548 { 3549 zcb->zcb_spa = spa; 3550 3551 if (dump_opt['L']) 3552 return; 3553 3554 dsl_pool_t *dp = spa->spa_dsl_pool; 3555 vdev_t *rvd = spa->spa_root_vdev; 3556 3557 /* 3558 * We are going to be changing the meaning of the metaslab's 3559 * ms_allocatable. Ensure that the allocator doesn't try to 3560 * use the tree. 3561 */ 3562 spa->spa_normal_class->mc_ops = &zdb_metaslab_ops; 3563 spa->spa_log_class->mc_ops = &zdb_metaslab_ops; 3564 3565 zcb->zcb_vd_obsolete_counts = 3566 umem_zalloc(rvd->vdev_children * sizeof (uint32_t *), 3567 UMEM_NOFAIL); 3568 3569 /* 3570 * For leak detection, we overload the ms_allocatable trees 3571 * to contain allocated segments instead of free segments. 3572 * As a result, we can't use the normal metaslab_load/unload 3573 * interfaces. 3574 */ 3575 zdb_leak_init_prepare_indirect_vdevs(spa, zcb); 3576 load_concrete_ms_allocatable_trees(spa, SM_ALLOC); 3577 3578 /* 3579 * On load_concrete_ms_allocatable_trees() we loaded all the 3580 * allocated entries from the ms_sm to the ms_allocatable for 3581 * each metaslab. If the pool has a checkpoint or is in the 3582 * middle of discarding a checkpoint, some of these blocks 3583 * may have been freed but their ms_sm may not have been 3584 * updated because they are referenced by the checkpoint. In 3585 * order to avoid false-positives during leak-detection, we 3586 * go through the vdev's checkpoint space map and exclude all 3587 * its entries from their relevant ms_allocatable. 3588 * 3589 * We also aggregate the space held by the checkpoint and add 3590 * it to zcb_checkpoint_size. 3591 * 3592 * Note that at this point we are also verifying that all the 3593 * entries on the checkpoint_sm are marked as allocated in 3594 * the ms_sm of their relevant metaslab. 3595 * [see comment in checkpoint_sm_exclude_entry_cb()] 3596 */ 3597 zdb_leak_init_exclude_checkpoint(spa, zcb); 3598 ASSERT3U(zcb->zcb_checkpoint_size, ==, spa_get_checkpoint_space(spa)); 3599 3600 /* for cleaner progress output */ 3601 (void) fprintf(stderr, "\n"); 3602 3603 if (bpobj_is_open(&dp->dp_obsolete_bpobj)) { 3604 ASSERT(spa_feature_is_enabled(spa, 3605 SPA_FEATURE_DEVICE_REMOVAL)); 3606 (void) bpobj_iterate_nofree(&dp->dp_obsolete_bpobj, 3607 increment_indirect_mapping_cb, zcb, NULL); 3608 } 3609 3610 spa_config_enter(spa, SCL_CONFIG, FTAG, RW_READER); 3611 zdb_ddt_leak_init(spa, zcb); 3612 spa_config_exit(spa, SCL_CONFIG, FTAG); 3613 } 3614 3615 static boolean_t 3616 zdb_check_for_obsolete_leaks(vdev_t *vd, zdb_cb_t *zcb) 3617 { 3618 boolean_t leaks = B_FALSE; 3619 vdev_indirect_mapping_t *vim = vd->vdev_indirect_mapping; 3620 uint64_t total_leaked = 0; 3621 3622 ASSERT(vim != NULL); 3623 3624 for (uint64_t i = 0; i < vdev_indirect_mapping_num_entries(vim); i++) { 3625 vdev_indirect_mapping_entry_phys_t *vimep = 3626 &vim->vim_entries[i]; 3627 uint64_t obsolete_bytes = 0; 3628 uint64_t offset = DVA_MAPPING_GET_SRC_OFFSET(vimep); 3629 metaslab_t *msp = vd->vdev_ms[offset >> vd->vdev_ms_shift]; 3630 3631 /* 3632 * This is not very efficient but it's easy to 3633 * verify correctness. 3634 */ 3635 for (uint64_t inner_offset = 0; 3636 inner_offset < DVA_GET_ASIZE(&vimep->vimep_dst); 3637 inner_offset += 1 << vd->vdev_ashift) { 3638 if (range_tree_contains(msp->ms_allocatable, 3639 offset + inner_offset, 1 << vd->vdev_ashift)) { 3640 obsolete_bytes += 1 << vd->vdev_ashift; 3641 } 3642 } 3643 3644 int64_t bytes_leaked = obsolete_bytes - 3645 zcb->zcb_vd_obsolete_counts[vd->vdev_id][i]; 3646 ASSERT3U(DVA_GET_ASIZE(&vimep->vimep_dst), >=, 3647 zcb->zcb_vd_obsolete_counts[vd->vdev_id][i]); 3648 if (bytes_leaked != 0 && 3649 (vdev_obsolete_counts_are_precise(vd) || 3650 dump_opt['d'] >= 5)) { 3651 (void) printf("obsolete indirect mapping count " 3652 "mismatch on %llu:%llx:%llx : %llx bytes leaked\n", 3653 (u_longlong_t)vd->vdev_id, 3654 (u_longlong_t)DVA_MAPPING_GET_SRC_OFFSET(vimep), 3655 (u_longlong_t)DVA_GET_ASIZE(&vimep->vimep_dst), 3656 (u_longlong_t)bytes_leaked); 3657 } 3658 total_leaked += ABS(bytes_leaked); 3659 } 3660 3661 if (!vdev_obsolete_counts_are_precise(vd) && total_leaked > 0) { 3662 int pct_leaked = total_leaked * 100 / 3663 vdev_indirect_mapping_bytes_mapped(vim); 3664 (void) printf("cannot verify obsolete indirect mapping " 3665 "counts of vdev %llu because precise feature was not " 3666 "enabled when it was removed: %d%% (%llx bytes) of mapping" 3667 "unreferenced\n", 3668 (u_longlong_t)vd->vdev_id, pct_leaked, 3669 (u_longlong_t)total_leaked); 3670 } else if (total_leaked > 0) { 3671 (void) printf("obsolete indirect mapping count mismatch " 3672 "for vdev %llu -- %llx total bytes mismatched\n", 3673 (u_longlong_t)vd->vdev_id, 3674 (u_longlong_t)total_leaked); 3675 leaks |= B_TRUE; 3676 } 3677 3678 vdev_indirect_mapping_free_obsolete_counts(vim, 3679 zcb->zcb_vd_obsolete_counts[vd->vdev_id]); 3680 zcb->zcb_vd_obsolete_counts[vd->vdev_id] = NULL; 3681 3682 return (leaks); 3683 } 3684 3685 static boolean_t 3686 zdb_leak_fini(spa_t *spa, zdb_cb_t *zcb) 3687 { 3688 if (dump_opt['L']) 3689 return (B_FALSE); 3690 3691 boolean_t leaks = B_FALSE; 3692 3693 vdev_t *rvd = spa->spa_root_vdev; 3694 for (unsigned c = 0; c < rvd->vdev_children; c++) { 3695 vdev_t *vd = rvd->vdev_child[c]; 3696 #if DEBUG 3697 metaslab_group_t *mg = vd->vdev_mg; 3698 #endif 3699 3700 if (zcb->zcb_vd_obsolete_counts[c] != NULL) { 3701 leaks |= zdb_check_for_obsolete_leaks(vd, zcb); 3702 } 3703 3704 for (uint64_t m = 0; m < vd->vdev_ms_count; m++) { 3705 metaslab_t *msp = vd->vdev_ms[m]; 3706 ASSERT3P(mg, ==, msp->ms_group); 3707 3708 /* 3709 * ms_allocatable has been overloaded 3710 * to contain allocated segments. Now that 3711 * we finished traversing all blocks, any 3712 * block that remains in the ms_allocatable 3713 * represents an allocated block that we 3714 * did not claim during the traversal. 3715 * Claimed blocks would have been removed 3716 * from the ms_allocatable. For indirect 3717 * vdevs, space remaining in the tree 3718 * represents parts of the mapping that are 3719 * not referenced, which is not a bug. 3720 */ 3721 if (vd->vdev_ops == &vdev_indirect_ops) { 3722 range_tree_vacate(msp->ms_allocatable, 3723 NULL, NULL); 3724 } else { 3725 range_tree_vacate(msp->ms_allocatable, 3726 zdb_leak, vd); 3727 } 3728 3729 if (msp->ms_loaded) { 3730 msp->ms_loaded = B_FALSE; 3731 } 3732 } 3733 3734 } 3735 3736 umem_free(zcb->zcb_vd_obsolete_counts, 3737 rvd->vdev_children * sizeof (uint32_t *)); 3738 zcb->zcb_vd_obsolete_counts = NULL; 3739 3740 return (leaks); 3741 } 3742 3743 /* ARGSUSED */ 3744 static int 3745 count_block_cb(void *arg, const blkptr_t *bp, dmu_tx_t *tx) 3746 { 3747 zdb_cb_t *zcb = arg; 3748 3749 if (dump_opt['b'] >= 5) { 3750 char blkbuf[BP_SPRINTF_LEN]; 3751 snprintf_blkptr(blkbuf, sizeof (blkbuf), bp); 3752 (void) printf("[%s] %s\n", 3753 "deferred free", blkbuf); 3754 } 3755 zdb_count_block(zcb, NULL, bp, ZDB_OT_DEFERRED); 3756 return (0); 3757 } 3758 3759 static int 3760 dump_block_stats(spa_t *spa) 3761 { 3762 zdb_cb_t zcb; 3763 zdb_blkstats_t *zb, *tzb; 3764 uint64_t norm_alloc, norm_space, total_alloc, total_found; 3765 int flags = TRAVERSE_PRE | TRAVERSE_PREFETCH_METADATA | 3766 TRAVERSE_NO_DECRYPT | TRAVERSE_HARD; 3767 boolean_t leaks = B_FALSE; 3768 int err; 3769 3770 bzero(&zcb, sizeof (zcb)); 3771 (void) printf("\nTraversing all blocks %s%s%s%s%s...\n\n", 3772 (dump_opt['c'] || !dump_opt['L']) ? "to verify " : "", 3773 (dump_opt['c'] == 1) ? "metadata " : "", 3774 dump_opt['c'] ? "checksums " : "", 3775 (dump_opt['c'] && !dump_opt['L']) ? "and verify " : "", 3776 !dump_opt['L'] ? "nothing leaked " : ""); 3777 3778 /* 3779 * When leak detection is enabled we load all space maps as SM_ALLOC 3780 * maps, then traverse the pool claiming each block we discover. If 3781 * the pool is perfectly consistent, the segment trees will be empty 3782 * when we're done. Anything left over is a leak; any block we can't 3783 * claim (because it's not part of any space map) is a double 3784 * allocation, reference to a freed block, or an unclaimed log block. 3785 * 3786 * When leak detection is disabled (-L option) we still traverse the 3787 * pool claiming each block we discover, but we skip opening any space 3788 * maps. 3789 */ 3790 bzero(&zcb, sizeof (zdb_cb_t)); 3791 zdb_leak_init(spa, &zcb); 3792 3793 /* 3794 * If there's a deferred-free bplist, process that first. 3795 */ 3796 (void) bpobj_iterate_nofree(&spa->spa_deferred_bpobj, 3797 count_block_cb, &zcb, NULL); 3798 3799 if (spa_version(spa) >= SPA_VERSION_DEADLISTS) { 3800 (void) bpobj_iterate_nofree(&spa->spa_dsl_pool->dp_free_bpobj, 3801 count_block_cb, &zcb, NULL); 3802 } 3803 3804 zdb_claim_removing(spa, &zcb); 3805 3806 if (spa_feature_is_active(spa, SPA_FEATURE_ASYNC_DESTROY)) { 3807 VERIFY3U(0, ==, bptree_iterate(spa->spa_meta_objset, 3808 spa->spa_dsl_pool->dp_bptree_obj, B_FALSE, count_block_cb, 3809 &zcb, NULL)); 3810 } 3811 3812 if (dump_opt['c'] > 1) 3813 flags |= TRAVERSE_PREFETCH_DATA; 3814 3815 zcb.zcb_totalasize = metaslab_class_get_alloc(spa_normal_class(spa)); 3816 zcb.zcb_totalasize += metaslab_class_get_alloc(spa_special_class(spa)); 3817 zcb.zcb_totalasize += metaslab_class_get_alloc(spa_dedup_class(spa)); 3818 zcb.zcb_start = zcb.zcb_lastprint = gethrtime(); 3819 err = traverse_pool(spa, 0, flags, zdb_blkptr_cb, &zcb); 3820 3821 /* 3822 * If we've traversed the data blocks then we need to wait for those 3823 * I/Os to complete. We leverage "The Godfather" zio to wait on 3824 * all async I/Os to complete. 3825 */ 3826 if (dump_opt['c']) { 3827 for (int i = 0; i < max_ncpus; i++) { 3828 (void) zio_wait(spa->spa_async_zio_root[i]); 3829 spa->spa_async_zio_root[i] = zio_root(spa, NULL, NULL, 3830 ZIO_FLAG_CANFAIL | ZIO_FLAG_SPECULATIVE | 3831 ZIO_FLAG_GODFATHER); 3832 } 3833 } 3834 3835 /* 3836 * Done after zio_wait() since zcb_haderrors is modified in 3837 * zdb_blkptr_done() 3838 */ 3839 zcb.zcb_haderrors |= err; 3840 3841 if (zcb.zcb_haderrors) { 3842 (void) printf("\nError counts:\n\n"); 3843 (void) printf("\t%5s %s\n", "errno", "count"); 3844 for (int e = 0; e < 256; e++) { 3845 if (zcb.zcb_errors[e] != 0) { 3846 (void) printf("\t%5d %llu\n", 3847 e, (u_longlong_t)zcb.zcb_errors[e]); 3848 } 3849 } 3850 } 3851 3852 /* 3853 * Report any leaked segments. 3854 */ 3855 leaks |= zdb_leak_fini(spa, &zcb); 3856 3857 tzb = &zcb.zcb_type[ZB_TOTAL][ZDB_OT_TOTAL]; 3858 3859 norm_alloc = metaslab_class_get_alloc(spa_normal_class(spa)); 3860 norm_space = metaslab_class_get_space(spa_normal_class(spa)); 3861 3862 total_alloc = norm_alloc + 3863 metaslab_class_get_alloc(spa_log_class(spa)) + 3864 metaslab_class_get_alloc(spa_special_class(spa)) + 3865 metaslab_class_get_alloc(spa_dedup_class(spa)); 3866 total_found = tzb->zb_asize - zcb.zcb_dedup_asize + 3867 zcb.zcb_removing_size + zcb.zcb_checkpoint_size; 3868 3869 if (total_found == total_alloc && !dump_opt['L']) { 3870 (void) printf("\n\tNo leaks (block sum matches space" 3871 " maps exactly)\n"); 3872 } else if (!dump_opt['L']) { 3873 (void) printf("block traversal size %llu != alloc %llu " 3874 "(%s %lld)\n", 3875 (u_longlong_t)total_found, 3876 (u_longlong_t)total_alloc, 3877 (dump_opt['L']) ? "unreachable" : "leaked", 3878 (longlong_t)(total_alloc - total_found)); 3879 leaks = B_TRUE; 3880 } 3881 3882 if (tzb->zb_count == 0) 3883 return (2); 3884 3885 (void) printf("\n"); 3886 (void) printf("\t%-16s %14llu\n", "bp count:", 3887 (u_longlong_t)tzb->zb_count); 3888 (void) printf("\t%-16s %14llu\n", "ganged count:", 3889 (longlong_t)tzb->zb_gangs); 3890 (void) printf("\t%-16s %14llu avg: %6llu\n", "bp logical:", 3891 (u_longlong_t)tzb->zb_lsize, 3892 (u_longlong_t)(tzb->zb_lsize / tzb->zb_count)); 3893 (void) printf("\t%-16s %14llu avg: %6llu compression: %6.2f\n", 3894 "bp physical:", (u_longlong_t)tzb->zb_psize, 3895 (u_longlong_t)(tzb->zb_psize / tzb->zb_count), 3896 (double)tzb->zb_lsize / tzb->zb_psize); 3897 (void) printf("\t%-16s %14llu avg: %6llu compression: %6.2f\n", 3898 "bp allocated:", (u_longlong_t)tzb->zb_asize, 3899 (u_longlong_t)(tzb->zb_asize / tzb->zb_count), 3900 (double)tzb->zb_lsize / tzb->zb_asize); 3901 (void) printf("\t%-16s %14llu ref>1: %6llu deduplication: %6.2f\n", 3902 "bp deduped:", (u_longlong_t)zcb.zcb_dedup_asize, 3903 (u_longlong_t)zcb.zcb_dedup_blocks, 3904 (double)zcb.zcb_dedup_asize / tzb->zb_asize + 1.0); 3905 (void) printf("\t%-16s %14llu used: %5.2f%%\n", "Normal class:", 3906 (u_longlong_t)norm_alloc, 100.0 * norm_alloc / norm_space); 3907 3908 if (spa_special_class(spa)->mc_rotor != NULL) { 3909 uint64_t alloc = metaslab_class_get_alloc( 3910 spa_special_class(spa)); 3911 uint64_t space = metaslab_class_get_space( 3912 spa_special_class(spa)); 3913 3914 (void) printf("\t%-16s %14llu used: %5.2f%%\n", 3915 "Special class", (u_longlong_t)alloc, 3916 100.0 * alloc / space); 3917 } 3918 3919 if (spa_dedup_class(spa)->mc_rotor != NULL) { 3920 uint64_t alloc = metaslab_class_get_alloc( 3921 spa_dedup_class(spa)); 3922 uint64_t space = metaslab_class_get_space( 3923 spa_dedup_class(spa)); 3924 3925 (void) printf("\t%-16s %14llu used: %5.2f%%\n", 3926 "Dedup class", (u_longlong_t)alloc, 3927 100.0 * alloc / space); 3928 } 3929 3930 for (bp_embedded_type_t i = 0; i < NUM_BP_EMBEDDED_TYPES; i++) { 3931 if (zcb.zcb_embedded_blocks[i] == 0) 3932 continue; 3933 (void) printf("\n"); 3934 (void) printf("\tadditional, non-pointer bps of type %u: " 3935 "%10llu\n", 3936 i, (u_longlong_t)zcb.zcb_embedded_blocks[i]); 3937 3938 if (dump_opt['b'] >= 3) { 3939 (void) printf("\t number of (compressed) bytes: " 3940 "number of bps\n"); 3941 dump_histogram(zcb.zcb_embedded_histogram[i], 3942 sizeof (zcb.zcb_embedded_histogram[i]) / 3943 sizeof (zcb.zcb_embedded_histogram[i][0]), 0); 3944 } 3945 } 3946 3947 if (tzb->zb_ditto_samevdev != 0) { 3948 (void) printf("\tDittoed blocks on same vdev: %llu\n", 3949 (longlong_t)tzb->zb_ditto_samevdev); 3950 } 3951 if (tzb->zb_ditto_same_ms != 0) { 3952 (void) printf("\tDittoed blocks in same metaslab: %llu\n", 3953 (longlong_t)tzb->zb_ditto_same_ms); 3954 } 3955 3956 for (uint64_t v = 0; v < spa->spa_root_vdev->vdev_children; v++) { 3957 vdev_t *vd = spa->spa_root_vdev->vdev_child[v]; 3958 vdev_indirect_mapping_t *vim = vd->vdev_indirect_mapping; 3959 3960 if (vim == NULL) { 3961 continue; 3962 } 3963 3964 char mem[32]; 3965 zdb_nicenum(vdev_indirect_mapping_num_entries(vim), 3966 mem, vdev_indirect_mapping_size(vim)); 3967 3968 (void) printf("\tindirect vdev id %llu has %llu segments " 3969 "(%s in memory)\n", 3970 (longlong_t)vd->vdev_id, 3971 (longlong_t)vdev_indirect_mapping_num_entries(vim), mem); 3972 } 3973 3974 if (dump_opt['b'] >= 2) { 3975 int l, t, level; 3976 (void) printf("\nBlocks\tLSIZE\tPSIZE\tASIZE" 3977 "\t avg\t comp\t%%Total\tType\n"); 3978 3979 for (t = 0; t <= ZDB_OT_TOTAL; t++) { 3980 char csize[32], lsize[32], psize[32], asize[32]; 3981 char avg[32], gang[32]; 3982 const char *typename; 3983 3984 /* make sure nicenum has enough space */ 3985 CTASSERT(sizeof (csize) >= NN_NUMBUF_SZ); 3986 CTASSERT(sizeof (lsize) >= NN_NUMBUF_SZ); 3987 CTASSERT(sizeof (psize) >= NN_NUMBUF_SZ); 3988 CTASSERT(sizeof (asize) >= NN_NUMBUF_SZ); 3989 CTASSERT(sizeof (avg) >= NN_NUMBUF_SZ); 3990 CTASSERT(sizeof (gang) >= NN_NUMBUF_SZ); 3991 3992 if (t < DMU_OT_NUMTYPES) 3993 typename = dmu_ot[t].ot_name; 3994 else 3995 typename = zdb_ot_extname[t - DMU_OT_NUMTYPES]; 3996 3997 if (zcb.zcb_type[ZB_TOTAL][t].zb_asize == 0) { 3998 (void) printf("%6s\t%5s\t%5s\t%5s" 3999 "\t%5s\t%5s\t%6s\t%s\n", 4000 "-", 4001 "-", 4002 "-", 4003 "-", 4004 "-", 4005 "-", 4006 "-", 4007 typename); 4008 continue; 4009 } 4010 4011 for (l = ZB_TOTAL - 1; l >= -1; l--) { 4012 level = (l == -1 ? ZB_TOTAL : l); 4013 zb = &zcb.zcb_type[level][t]; 4014 4015 if (zb->zb_asize == 0) 4016 continue; 4017 4018 if (dump_opt['b'] < 3 && level != ZB_TOTAL) 4019 continue; 4020 4021 if (level == 0 && zb->zb_asize == 4022 zcb.zcb_type[ZB_TOTAL][t].zb_asize) 4023 continue; 4024 4025 zdb_nicenum(zb->zb_count, csize, 4026 sizeof (csize)); 4027 zdb_nicenum(zb->zb_lsize, lsize, 4028 sizeof (lsize)); 4029 zdb_nicenum(zb->zb_psize, psize, 4030 sizeof (psize)); 4031 zdb_nicenum(zb->zb_asize, asize, 4032 sizeof (asize)); 4033 zdb_nicenum(zb->zb_asize / zb->zb_count, avg, 4034 sizeof (avg)); 4035 zdb_nicenum(zb->zb_gangs, gang, sizeof (gang)); 4036 4037 (void) printf("%6s\t%5s\t%5s\t%5s\t%5s" 4038 "\t%5.2f\t%6.2f\t", 4039 csize, lsize, psize, asize, avg, 4040 (double)zb->zb_lsize / zb->zb_psize, 4041 100.0 * zb->zb_asize / tzb->zb_asize); 4042 4043 if (level == ZB_TOTAL) 4044 (void) printf("%s\n", typename); 4045 else 4046 (void) printf(" L%d %s\n", 4047 level, typename); 4048 4049 if (dump_opt['b'] >= 3 && zb->zb_gangs > 0) { 4050 (void) printf("\t number of ganged " 4051 "blocks: %s\n", gang); 4052 } 4053 4054 if (dump_opt['b'] >= 4) { 4055 (void) printf("psize " 4056 "(in 512-byte sectors): " 4057 "number of blocks\n"); 4058 dump_histogram(zb->zb_psize_histogram, 4059 PSIZE_HISTO_SIZE, 0); 4060 } 4061 } 4062 } 4063 } 4064 4065 (void) printf("\n"); 4066 4067 if (leaks) 4068 return (2); 4069 4070 if (zcb.zcb_haderrors) 4071 return (3); 4072 4073 return (0); 4074 } 4075 4076 typedef struct zdb_ddt_entry { 4077 ddt_key_t zdde_key; 4078 uint64_t zdde_ref_blocks; 4079 uint64_t zdde_ref_lsize; 4080 uint64_t zdde_ref_psize; 4081 uint64_t zdde_ref_dsize; 4082 avl_node_t zdde_node; 4083 } zdb_ddt_entry_t; 4084 4085 /* ARGSUSED */ 4086 static int 4087 zdb_ddt_add_cb(spa_t *spa, zilog_t *zilog, const blkptr_t *bp, 4088 const zbookmark_phys_t *zb, const dnode_phys_t *dnp, void *arg) 4089 { 4090 avl_tree_t *t = arg; 4091 avl_index_t where; 4092 zdb_ddt_entry_t *zdde, zdde_search; 4093 4094 if (bp == NULL || BP_IS_HOLE(bp) || BP_IS_EMBEDDED(bp)) 4095 return (0); 4096 4097 if (dump_opt['S'] > 1 && zb->zb_level == ZB_ROOT_LEVEL) { 4098 (void) printf("traversing objset %llu, %llu objects, " 4099 "%lu blocks so far\n", 4100 (u_longlong_t)zb->zb_objset, 4101 (u_longlong_t)BP_GET_FILL(bp), 4102 avl_numnodes(t)); 4103 } 4104 4105 if (BP_IS_HOLE(bp) || BP_GET_CHECKSUM(bp) == ZIO_CHECKSUM_OFF || 4106 BP_GET_LEVEL(bp) > 0 || DMU_OT_IS_METADATA(BP_GET_TYPE(bp))) 4107 return (0); 4108 4109 ddt_key_fill(&zdde_search.zdde_key, bp); 4110 4111 zdde = avl_find(t, &zdde_search, &where); 4112 4113 if (zdde == NULL) { 4114 zdde = umem_zalloc(sizeof (*zdde), UMEM_NOFAIL); 4115 zdde->zdde_key = zdde_search.zdde_key; 4116 avl_insert(t, zdde, where); 4117 } 4118 4119 zdde->zdde_ref_blocks += 1; 4120 zdde->zdde_ref_lsize += BP_GET_LSIZE(bp); 4121 zdde->zdde_ref_psize += BP_GET_PSIZE(bp); 4122 zdde->zdde_ref_dsize += bp_get_dsize_sync(spa, bp); 4123 4124 return (0); 4125 } 4126 4127 static void 4128 dump_simulated_ddt(spa_t *spa) 4129 { 4130 avl_tree_t t; 4131 void *cookie = NULL; 4132 zdb_ddt_entry_t *zdde; 4133 ddt_histogram_t ddh_total; 4134 ddt_stat_t dds_total; 4135 4136 bzero(&ddh_total, sizeof (ddh_total)); 4137 bzero(&dds_total, sizeof (dds_total)); 4138 avl_create(&t, ddt_entry_compare, 4139 sizeof (zdb_ddt_entry_t), offsetof(zdb_ddt_entry_t, zdde_node)); 4140 4141 spa_config_enter(spa, SCL_CONFIG, FTAG, RW_READER); 4142 4143 (void) traverse_pool(spa, 0, TRAVERSE_PRE | TRAVERSE_PREFETCH_METADATA | 4144 TRAVERSE_NO_DECRYPT, zdb_ddt_add_cb, &t); 4145 4146 spa_config_exit(spa, SCL_CONFIG, FTAG); 4147 4148 while ((zdde = avl_destroy_nodes(&t, &cookie)) != NULL) { 4149 ddt_stat_t dds; 4150 uint64_t refcnt = zdde->zdde_ref_blocks; 4151 ASSERT(refcnt != 0); 4152 4153 dds.dds_blocks = zdde->zdde_ref_blocks / refcnt; 4154 dds.dds_lsize = zdde->zdde_ref_lsize / refcnt; 4155 dds.dds_psize = zdde->zdde_ref_psize / refcnt; 4156 dds.dds_dsize = zdde->zdde_ref_dsize / refcnt; 4157 4158 dds.dds_ref_blocks = zdde->zdde_ref_blocks; 4159 dds.dds_ref_lsize = zdde->zdde_ref_lsize; 4160 dds.dds_ref_psize = zdde->zdde_ref_psize; 4161 dds.dds_ref_dsize = zdde->zdde_ref_dsize; 4162 4163 ddt_stat_add(&ddh_total.ddh_stat[highbit64(refcnt) - 1], 4164 &dds, 0); 4165 4166 umem_free(zdde, sizeof (*zdde)); 4167 } 4168 4169 avl_destroy(&t); 4170 4171 ddt_histogram_stat(&dds_total, &ddh_total); 4172 4173 (void) printf("Simulated DDT histogram:\n"); 4174 4175 zpool_dump_ddt(&dds_total, &ddh_total); 4176 4177 dump_dedup_ratio(&dds_total); 4178 } 4179 4180 static int 4181 verify_device_removal_feature_counts(spa_t *spa) 4182 { 4183 uint64_t dr_feature_refcount = 0; 4184 uint64_t oc_feature_refcount = 0; 4185 uint64_t indirect_vdev_count = 0; 4186 uint64_t precise_vdev_count = 0; 4187 uint64_t obsolete_counts_object_count = 0; 4188 uint64_t obsolete_sm_count = 0; 4189 uint64_t obsolete_counts_count = 0; 4190 uint64_t scip_count = 0; 4191 uint64_t obsolete_bpobj_count = 0; 4192 int ret = 0; 4193 4194 spa_condensing_indirect_phys_t *scip = 4195 &spa->spa_condensing_indirect_phys; 4196 if (scip->scip_next_mapping_object != 0) { 4197 vdev_t *vd = spa->spa_root_vdev->vdev_child[scip->scip_vdev]; 4198 ASSERT(scip->scip_prev_obsolete_sm_object != 0); 4199 ASSERT3P(vd->vdev_ops, ==, &vdev_indirect_ops); 4200 4201 (void) printf("Condensing indirect vdev %llu: new mapping " 4202 "object %llu, prev obsolete sm %llu\n", 4203 (u_longlong_t)scip->scip_vdev, 4204 (u_longlong_t)scip->scip_next_mapping_object, 4205 (u_longlong_t)scip->scip_prev_obsolete_sm_object); 4206 if (scip->scip_prev_obsolete_sm_object != 0) { 4207 space_map_t *prev_obsolete_sm = NULL; 4208 VERIFY0(space_map_open(&prev_obsolete_sm, 4209 spa->spa_meta_objset, 4210 scip->scip_prev_obsolete_sm_object, 4211 0, vd->vdev_asize, 0)); 4212 dump_spacemap(spa->spa_meta_objset, prev_obsolete_sm); 4213 (void) printf("\n"); 4214 space_map_close(prev_obsolete_sm); 4215 } 4216 4217 scip_count += 2; 4218 } 4219 4220 for (uint64_t i = 0; i < spa->spa_root_vdev->vdev_children; i++) { 4221 vdev_t *vd = spa->spa_root_vdev->vdev_child[i]; 4222 vdev_indirect_config_t *vic = &vd->vdev_indirect_config; 4223 4224 if (vic->vic_mapping_object != 0) { 4225 ASSERT(vd->vdev_ops == &vdev_indirect_ops || 4226 vd->vdev_removing); 4227 indirect_vdev_count++; 4228 4229 if (vd->vdev_indirect_mapping->vim_havecounts) { 4230 obsolete_counts_count++; 4231 } 4232 } 4233 if (vdev_obsolete_counts_are_precise(vd)) { 4234 ASSERT(vic->vic_mapping_object != 0); 4235 precise_vdev_count++; 4236 } 4237 if (vdev_obsolete_sm_object(vd) != 0) { 4238 ASSERT(vic->vic_mapping_object != 0); 4239 obsolete_sm_count++; 4240 } 4241 } 4242 4243 (void) feature_get_refcount(spa, 4244 &spa_feature_table[SPA_FEATURE_DEVICE_REMOVAL], 4245 &dr_feature_refcount); 4246 (void) feature_get_refcount(spa, 4247 &spa_feature_table[SPA_FEATURE_OBSOLETE_COUNTS], 4248 &oc_feature_refcount); 4249 4250 if (dr_feature_refcount != indirect_vdev_count) { 4251 ret = 1; 4252 (void) printf("Number of indirect vdevs (%llu) " \ 4253 "does not match feature count (%llu)\n", 4254 (u_longlong_t)indirect_vdev_count, 4255 (u_longlong_t)dr_feature_refcount); 4256 } else { 4257 (void) printf("Verified device_removal feature refcount " \ 4258 "of %llu is correct\n", 4259 (u_longlong_t)dr_feature_refcount); 4260 } 4261 4262 if (zap_contains(spa_meta_objset(spa), DMU_POOL_DIRECTORY_OBJECT, 4263 DMU_POOL_OBSOLETE_BPOBJ) == 0) { 4264 obsolete_bpobj_count++; 4265 } 4266 4267 4268 obsolete_counts_object_count = precise_vdev_count; 4269 obsolete_counts_object_count += obsolete_sm_count; 4270 obsolete_counts_object_count += obsolete_counts_count; 4271 obsolete_counts_object_count += scip_count; 4272 obsolete_counts_object_count += obsolete_bpobj_count; 4273 obsolete_counts_object_count += remap_deadlist_count; 4274 4275 if (oc_feature_refcount != obsolete_counts_object_count) { 4276 ret = 1; 4277 (void) printf("Number of obsolete counts objects (%llu) " \ 4278 "does not match feature count (%llu)\n", 4279 (u_longlong_t)obsolete_counts_object_count, 4280 (u_longlong_t)oc_feature_refcount); 4281 (void) printf("pv:%llu os:%llu oc:%llu sc:%llu " 4282 "ob:%llu rd:%llu\n", 4283 (u_longlong_t)precise_vdev_count, 4284 (u_longlong_t)obsolete_sm_count, 4285 (u_longlong_t)obsolete_counts_count, 4286 (u_longlong_t)scip_count, 4287 (u_longlong_t)obsolete_bpobj_count, 4288 (u_longlong_t)remap_deadlist_count); 4289 } else { 4290 (void) printf("Verified indirect_refcount feature refcount " \ 4291 "of %llu is correct\n", 4292 (u_longlong_t)oc_feature_refcount); 4293 } 4294 return (ret); 4295 } 4296 4297 static void 4298 zdb_set_skip_mmp(char *target) 4299 { 4300 spa_t *spa; 4301 4302 /* 4303 * Disable the activity check to allow examination of 4304 * active pools. 4305 */ 4306 mutex_enter(&spa_namespace_lock); 4307 if ((spa = spa_lookup(target)) != NULL) { 4308 spa->spa_import_flags |= ZFS_IMPORT_SKIP_MMP; 4309 } 4310 mutex_exit(&spa_namespace_lock); 4311 } 4312 4313 #define BOGUS_SUFFIX "_CHECKPOINTED_UNIVERSE" 4314 /* 4315 * Import the checkpointed state of the pool specified by the target 4316 * parameter as readonly. The function also accepts a pool config 4317 * as an optional parameter, else it attempts to infer the config by 4318 * the name of the target pool. 4319 * 4320 * Note that the checkpointed state's pool name will be the name of 4321 * the original pool with the above suffix appened to it. In addition, 4322 * if the target is not a pool name (e.g. a path to a dataset) then 4323 * the new_path parameter is populated with the updated path to 4324 * reflect the fact that we are looking into the checkpointed state. 4325 * 4326 * The function returns a newly-allocated copy of the name of the 4327 * pool containing the checkpointed state. When this copy is no 4328 * longer needed it should be freed with free(3C). Same thing 4329 * applies to the new_path parameter if allocated. 4330 */ 4331 static char * 4332 import_checkpointed_state(char *target, nvlist_t *cfg, char **new_path) 4333 { 4334 int error = 0; 4335 char *poolname, *bogus_name; 4336 4337 /* If the target is not a pool, the extract the pool name */ 4338 char *path_start = strchr(target, '/'); 4339 if (path_start != NULL) { 4340 size_t poolname_len = path_start - target; 4341 poolname = strndup(target, poolname_len); 4342 } else { 4343 poolname = target; 4344 } 4345 4346 if (cfg == NULL) { 4347 zdb_set_skip_mmp(poolname); 4348 error = spa_get_stats(poolname, &cfg, NULL, 0); 4349 if (error != 0) { 4350 fatal("Tried to read config of pool \"%s\" but " 4351 "spa_get_stats() failed with error %d\n", 4352 poolname, error); 4353 } 4354 } 4355 4356 (void) asprintf(&bogus_name, "%s%s", poolname, BOGUS_SUFFIX); 4357 fnvlist_add_string(cfg, ZPOOL_CONFIG_POOL_NAME, bogus_name); 4358 4359 error = spa_import(bogus_name, cfg, NULL, 4360 ZFS_IMPORT_MISSING_LOG | ZFS_IMPORT_CHECKPOINT | 4361 ZFS_IMPORT_SKIP_MMP); 4362 if (error != 0) { 4363 fatal("Tried to import pool \"%s\" but spa_import() failed " 4364 "with error %d\n", bogus_name, error); 4365 } 4366 4367 if (new_path != NULL && path_start != NULL) 4368 (void) asprintf(new_path, "%s%s", bogus_name, path_start); 4369 4370 if (target != poolname) 4371 free(poolname); 4372 4373 return (bogus_name); 4374 } 4375 4376 typedef struct verify_checkpoint_sm_entry_cb_arg { 4377 vdev_t *vcsec_vd; 4378 4379 /* the following fields are only used for printing progress */ 4380 uint64_t vcsec_entryid; 4381 uint64_t vcsec_num_entries; 4382 } verify_checkpoint_sm_entry_cb_arg_t; 4383 4384 #define ENTRIES_PER_PROGRESS_UPDATE 10000 4385 4386 static int 4387 verify_checkpoint_sm_entry_cb(space_map_entry_t *sme, void *arg) 4388 { 4389 verify_checkpoint_sm_entry_cb_arg_t *vcsec = arg; 4390 vdev_t *vd = vcsec->vcsec_vd; 4391 metaslab_t *ms = vd->vdev_ms[sme->sme_offset >> vd->vdev_ms_shift]; 4392 uint64_t end = sme->sme_offset + sme->sme_run; 4393 4394 ASSERT(sme->sme_type == SM_FREE); 4395 4396 if ((vcsec->vcsec_entryid % ENTRIES_PER_PROGRESS_UPDATE) == 0) { 4397 (void) fprintf(stderr, 4398 "\rverifying vdev %llu, space map entry %llu of %llu ...", 4399 (longlong_t)vd->vdev_id, 4400 (longlong_t)vcsec->vcsec_entryid, 4401 (longlong_t)vcsec->vcsec_num_entries); 4402 } 4403 vcsec->vcsec_entryid++; 4404 4405 /* 4406 * See comment in checkpoint_sm_exclude_entry_cb() 4407 */ 4408 VERIFY3U(sme->sme_offset, >=, ms->ms_start); 4409 VERIFY3U(end, <=, ms->ms_start + ms->ms_size); 4410 4411 /* 4412 * The entries in the vdev_checkpoint_sm should be marked as 4413 * allocated in the checkpointed state of the pool, therefore 4414 * their respective ms_allocateable trees should not contain them. 4415 */ 4416 mutex_enter(&ms->ms_lock); 4417 range_tree_verify_not_present(ms->ms_allocatable, 4418 sme->sme_offset, sme->sme_run); 4419 mutex_exit(&ms->ms_lock); 4420 4421 return (0); 4422 } 4423 4424 /* 4425 * Verify that all segments in the vdev_checkpoint_sm are allocated 4426 * according to the checkpoint's ms_sm (i.e. are not in the checkpoint's 4427 * ms_allocatable). 4428 * 4429 * Do so by comparing the checkpoint space maps (vdev_checkpoint_sm) of 4430 * each vdev in the current state of the pool to the metaslab space maps 4431 * (ms_sm) of the checkpointed state of the pool. 4432 * 4433 * Note that the function changes the state of the ms_allocatable 4434 * trees of the current spa_t. The entries of these ms_allocatable 4435 * trees are cleared out and then repopulated from with the free 4436 * entries of their respective ms_sm space maps. 4437 */ 4438 static void 4439 verify_checkpoint_vdev_spacemaps(spa_t *checkpoint, spa_t *current) 4440 { 4441 vdev_t *ckpoint_rvd = checkpoint->spa_root_vdev; 4442 vdev_t *current_rvd = current->spa_root_vdev; 4443 4444 load_concrete_ms_allocatable_trees(checkpoint, SM_FREE); 4445 4446 for (uint64_t c = 0; c < ckpoint_rvd->vdev_children; c++) { 4447 vdev_t *ckpoint_vd = ckpoint_rvd->vdev_child[c]; 4448 vdev_t *current_vd = current_rvd->vdev_child[c]; 4449 4450 space_map_t *checkpoint_sm = NULL; 4451 uint64_t checkpoint_sm_obj; 4452 4453 if (ckpoint_vd->vdev_ops == &vdev_indirect_ops) { 4454 /* 4455 * Since we don't allow device removal in a pool 4456 * that has a checkpoint, we expect that all removed 4457 * vdevs were removed from the pool before the 4458 * checkpoint. 4459 */ 4460 ASSERT3P(current_vd->vdev_ops, ==, &vdev_indirect_ops); 4461 continue; 4462 } 4463 4464 /* 4465 * If the checkpoint space map doesn't exist, then nothing 4466 * here is checkpointed so there's nothing to verify. 4467 */ 4468 if (current_vd->vdev_top_zap == 0 || 4469 zap_contains(spa_meta_objset(current), 4470 current_vd->vdev_top_zap, 4471 VDEV_TOP_ZAP_POOL_CHECKPOINT_SM) != 0) 4472 continue; 4473 4474 VERIFY0(zap_lookup(spa_meta_objset(current), 4475 current_vd->vdev_top_zap, VDEV_TOP_ZAP_POOL_CHECKPOINT_SM, 4476 sizeof (uint64_t), 1, &checkpoint_sm_obj)); 4477 4478 VERIFY0(space_map_open(&checkpoint_sm, spa_meta_objset(current), 4479 checkpoint_sm_obj, 0, current_vd->vdev_asize, 4480 current_vd->vdev_ashift)); 4481 4482 verify_checkpoint_sm_entry_cb_arg_t vcsec; 4483 vcsec.vcsec_vd = ckpoint_vd; 4484 vcsec.vcsec_entryid = 0; 4485 vcsec.vcsec_num_entries = 4486 space_map_length(checkpoint_sm) / sizeof (uint64_t); 4487 VERIFY0(space_map_iterate(checkpoint_sm, 4488 space_map_length(checkpoint_sm), 4489 verify_checkpoint_sm_entry_cb, &vcsec)); 4490 dump_spacemap(current->spa_meta_objset, checkpoint_sm); 4491 space_map_close(checkpoint_sm); 4492 } 4493 4494 /* 4495 * If we've added vdevs since we took the checkpoint, ensure 4496 * that their checkpoint space maps are empty. 4497 */ 4498 if (ckpoint_rvd->vdev_children < current_rvd->vdev_children) { 4499 for (uint64_t c = ckpoint_rvd->vdev_children; 4500 c < current_rvd->vdev_children; c++) { 4501 vdev_t *current_vd = current_rvd->vdev_child[c]; 4502 ASSERT3P(current_vd->vdev_checkpoint_sm, ==, NULL); 4503 } 4504 } 4505 4506 /* for cleaner progress output */ 4507 (void) fprintf(stderr, "\n"); 4508 } 4509 4510 /* 4511 * Verifies that all space that's allocated in the checkpoint is 4512 * still allocated in the current version, by checking that everything 4513 * in checkpoint's ms_allocatable (which is actually allocated, not 4514 * allocatable/free) is not present in current's ms_allocatable. 4515 * 4516 * Note that the function changes the state of the ms_allocatable 4517 * trees of both spas when called. The entries of all ms_allocatable 4518 * trees are cleared out and then repopulated from their respective 4519 * ms_sm space maps. In the checkpointed state we load the allocated 4520 * entries, and in the current state we load the free entries. 4521 */ 4522 static void 4523 verify_checkpoint_ms_spacemaps(spa_t *checkpoint, spa_t *current) 4524 { 4525 vdev_t *ckpoint_rvd = checkpoint->spa_root_vdev; 4526 vdev_t *current_rvd = current->spa_root_vdev; 4527 4528 load_concrete_ms_allocatable_trees(checkpoint, SM_ALLOC); 4529 load_concrete_ms_allocatable_trees(current, SM_FREE); 4530 4531 for (uint64_t i = 0; i < ckpoint_rvd->vdev_children; i++) { 4532 vdev_t *ckpoint_vd = ckpoint_rvd->vdev_child[i]; 4533 vdev_t *current_vd = current_rvd->vdev_child[i]; 4534 4535 if (ckpoint_vd->vdev_ops == &vdev_indirect_ops) { 4536 /* 4537 * See comment in verify_checkpoint_vdev_spacemaps() 4538 */ 4539 ASSERT3P(current_vd->vdev_ops, ==, &vdev_indirect_ops); 4540 continue; 4541 } 4542 4543 for (uint64_t m = 0; m < ckpoint_vd->vdev_ms_count; m++) { 4544 metaslab_t *ckpoint_msp = ckpoint_vd->vdev_ms[m]; 4545 metaslab_t *current_msp = current_vd->vdev_ms[m]; 4546 4547 (void) fprintf(stderr, 4548 "\rverifying vdev %llu of %llu, " 4549 "metaslab %llu of %llu ...", 4550 (longlong_t)current_vd->vdev_id, 4551 (longlong_t)current_rvd->vdev_children, 4552 (longlong_t)current_vd->vdev_ms[m]->ms_id, 4553 (longlong_t)current_vd->vdev_ms_count); 4554 4555 /* 4556 * We walk through the ms_allocatable trees that 4557 * are loaded with the allocated blocks from the 4558 * ms_sm spacemaps of the checkpoint. For each 4559 * one of these ranges we ensure that none of them 4560 * exists in the ms_allocatable trees of the 4561 * current state which are loaded with the ranges 4562 * that are currently free. 4563 * 4564 * This way we ensure that none of the blocks that 4565 * are part of the checkpoint were freed by mistake. 4566 */ 4567 range_tree_walk(ckpoint_msp->ms_allocatable, 4568 (range_tree_func_t *)range_tree_verify_not_present, 4569 current_msp->ms_allocatable); 4570 } 4571 } 4572 4573 /* for cleaner progress output */ 4574 (void) fprintf(stderr, "\n"); 4575 } 4576 4577 static void 4578 verify_checkpoint_blocks(spa_t *spa) 4579 { 4580 ASSERT(!dump_opt['L']); 4581 4582 spa_t *checkpoint_spa; 4583 char *checkpoint_pool; 4584 nvlist_t *config = NULL; 4585 int error = 0; 4586 4587 /* 4588 * We import the checkpointed state of the pool (under a different 4589 * name) so we can do verification on it against the current state 4590 * of the pool. 4591 */ 4592 checkpoint_pool = import_checkpointed_state(spa->spa_name, config, 4593 NULL); 4594 ASSERT(strcmp(spa->spa_name, checkpoint_pool) != 0); 4595 4596 error = spa_open(checkpoint_pool, &checkpoint_spa, FTAG); 4597 if (error != 0) { 4598 fatal("Tried to open pool \"%s\" but spa_open() failed with " 4599 "error %d\n", checkpoint_pool, error); 4600 } 4601 4602 /* 4603 * Ensure that ranges in the checkpoint space maps of each vdev 4604 * are allocated according to the checkpointed state's metaslab 4605 * space maps. 4606 */ 4607 verify_checkpoint_vdev_spacemaps(checkpoint_spa, spa); 4608 4609 /* 4610 * Ensure that allocated ranges in the checkpoint's metaslab 4611 * space maps remain allocated in the metaslab space maps of 4612 * the current state. 4613 */ 4614 verify_checkpoint_ms_spacemaps(checkpoint_spa, spa); 4615 4616 /* 4617 * Once we are done, we get rid of the checkpointed state. 4618 */ 4619 spa_close(checkpoint_spa, FTAG); 4620 free(checkpoint_pool); 4621 } 4622 4623 static void 4624 dump_leftover_checkpoint_blocks(spa_t *spa) 4625 { 4626 vdev_t *rvd = spa->spa_root_vdev; 4627 4628 for (uint64_t i = 0; i < rvd->vdev_children; i++) { 4629 vdev_t *vd = rvd->vdev_child[i]; 4630 4631 space_map_t *checkpoint_sm = NULL; 4632 uint64_t checkpoint_sm_obj; 4633 4634 if (vd->vdev_top_zap == 0) 4635 continue; 4636 4637 if (zap_contains(spa_meta_objset(spa), vd->vdev_top_zap, 4638 VDEV_TOP_ZAP_POOL_CHECKPOINT_SM) != 0) 4639 continue; 4640 4641 VERIFY0(zap_lookup(spa_meta_objset(spa), vd->vdev_top_zap, 4642 VDEV_TOP_ZAP_POOL_CHECKPOINT_SM, 4643 sizeof (uint64_t), 1, &checkpoint_sm_obj)); 4644 4645 VERIFY0(space_map_open(&checkpoint_sm, spa_meta_objset(spa), 4646 checkpoint_sm_obj, 0, vd->vdev_asize, vd->vdev_ashift)); 4647 dump_spacemap(spa->spa_meta_objset, checkpoint_sm); 4648 space_map_close(checkpoint_sm); 4649 } 4650 } 4651 4652 static int 4653 verify_checkpoint(spa_t *spa) 4654 { 4655 uberblock_t checkpoint; 4656 int error; 4657 4658 if (!spa_feature_is_active(spa, SPA_FEATURE_POOL_CHECKPOINT)) 4659 return (0); 4660 4661 error = zap_lookup(spa->spa_meta_objset, DMU_POOL_DIRECTORY_OBJECT, 4662 DMU_POOL_ZPOOL_CHECKPOINT, sizeof (uint64_t), 4663 sizeof (uberblock_t) / sizeof (uint64_t), &checkpoint); 4664 4665 if (error == ENOENT && !dump_opt['L']) { 4666 /* 4667 * If the feature is active but the uberblock is missing 4668 * then we must be in the middle of discarding the 4669 * checkpoint. 4670 */ 4671 (void) printf("\nPartially discarded checkpoint " 4672 "state found:\n"); 4673 dump_leftover_checkpoint_blocks(spa); 4674 return (0); 4675 } else if (error != 0) { 4676 (void) printf("lookup error %d when looking for " 4677 "checkpointed uberblock in MOS\n", error); 4678 return (error); 4679 } 4680 dump_uberblock(&checkpoint, "\nCheckpointed uberblock found:\n", "\n"); 4681 4682 if (checkpoint.ub_checkpoint_txg == 0) { 4683 (void) printf("\nub_checkpoint_txg not set in checkpointed " 4684 "uberblock\n"); 4685 error = 3; 4686 } 4687 4688 if (error == 0 && !dump_opt['L']) 4689 verify_checkpoint_blocks(spa); 4690 4691 return (error); 4692 } 4693 4694 /* ARGSUSED */ 4695 static void 4696 mos_leaks_cb(void *arg, uint64_t start, uint64_t size) 4697 { 4698 for (uint64_t i = start; i < size; i++) { 4699 (void) printf("MOS object %llu referenced but not allocated\n", 4700 (u_longlong_t)i); 4701 } 4702 } 4703 4704 static range_tree_t *mos_refd_objs; 4705 4706 static void 4707 mos_obj_refd(uint64_t obj) 4708 { 4709 if (obj != 0 && mos_refd_objs != NULL) 4710 range_tree_add(mos_refd_objs, obj, 1); 4711 } 4712 4713 static void 4714 mos_leak_vdev(vdev_t *vd) 4715 { 4716 mos_obj_refd(vd->vdev_dtl_object); 4717 mos_obj_refd(vd->vdev_ms_array); 4718 mos_obj_refd(vd->vdev_top_zap); 4719 mos_obj_refd(vd->vdev_indirect_config.vic_births_object); 4720 mos_obj_refd(vd->vdev_indirect_config.vic_mapping_object); 4721 mos_obj_refd(vd->vdev_leaf_zap); 4722 if (vd->vdev_checkpoint_sm != NULL) 4723 mos_obj_refd(vd->vdev_checkpoint_sm->sm_object); 4724 if (vd->vdev_indirect_mapping != NULL) { 4725 mos_obj_refd(vd->vdev_indirect_mapping-> 4726 vim_phys->vimp_counts_object); 4727 } 4728 if (vd->vdev_obsolete_sm != NULL) 4729 mos_obj_refd(vd->vdev_obsolete_sm->sm_object); 4730 4731 for (uint64_t m = 0; m < vd->vdev_ms_count; m++) { 4732 metaslab_t *ms = vd->vdev_ms[m]; 4733 mos_obj_refd(space_map_object(ms->ms_sm)); 4734 } 4735 4736 for (uint64_t c = 0; c < vd->vdev_children; c++) { 4737 mos_leak_vdev(vd->vdev_child[c]); 4738 } 4739 } 4740 4741 static int 4742 dump_mos_leaks(spa_t *spa) 4743 { 4744 int rv = 0; 4745 objset_t *mos = spa->spa_meta_objset; 4746 dsl_pool_t *dp = spa->spa_dsl_pool; 4747 4748 /* Visit and mark all referenced objects in the MOS */ 4749 4750 mos_obj_refd(DMU_POOL_DIRECTORY_OBJECT); 4751 mos_obj_refd(spa->spa_pool_props_object); 4752 mos_obj_refd(spa->spa_config_object); 4753 mos_obj_refd(spa->spa_ddt_stat_object); 4754 mos_obj_refd(spa->spa_feat_desc_obj); 4755 mos_obj_refd(spa->spa_feat_enabled_txg_obj); 4756 mos_obj_refd(spa->spa_feat_for_read_obj); 4757 mos_obj_refd(spa->spa_feat_for_write_obj); 4758 mos_obj_refd(spa->spa_history); 4759 mos_obj_refd(spa->spa_errlog_last); 4760 mos_obj_refd(spa->spa_errlog_scrub); 4761 mos_obj_refd(spa->spa_all_vdev_zaps); 4762 mos_obj_refd(spa->spa_dsl_pool->dp_bptree_obj); 4763 mos_obj_refd(spa->spa_dsl_pool->dp_tmp_userrefs_obj); 4764 mos_obj_refd(spa->spa_dsl_pool->dp_scan->scn_phys.scn_queue_obj); 4765 bpobj_count_refd(&spa->spa_deferred_bpobj); 4766 mos_obj_refd(dp->dp_empty_bpobj); 4767 bpobj_count_refd(&dp->dp_obsolete_bpobj); 4768 bpobj_count_refd(&dp->dp_free_bpobj); 4769 mos_obj_refd(spa->spa_l2cache.sav_object); 4770 mos_obj_refd(spa->spa_spares.sav_object); 4771 4772 mos_obj_refd(spa->spa_condensing_indirect_phys. 4773 scip_next_mapping_object); 4774 mos_obj_refd(spa->spa_condensing_indirect_phys. 4775 scip_prev_obsolete_sm_object); 4776 if (spa->spa_condensing_indirect_phys.scip_next_mapping_object != 0) { 4777 vdev_indirect_mapping_t *vim = 4778 vdev_indirect_mapping_open(mos, 4779 spa->spa_condensing_indirect_phys.scip_next_mapping_object); 4780 mos_obj_refd(vim->vim_phys->vimp_counts_object); 4781 vdev_indirect_mapping_close(vim); 4782 } 4783 4784 if (dp->dp_origin_snap != NULL) { 4785 dsl_dataset_t *ds; 4786 4787 dsl_pool_config_enter(dp, FTAG); 4788 VERIFY0(dsl_dataset_hold_obj(dp, 4789 dsl_dataset_phys(dp->dp_origin_snap)->ds_next_snap_obj, 4790 FTAG, &ds)); 4791 count_ds_mos_objects(ds); 4792 dump_deadlist(&ds->ds_deadlist); 4793 dsl_dataset_rele(ds, FTAG); 4794 dsl_pool_config_exit(dp, FTAG); 4795 4796 count_ds_mos_objects(dp->dp_origin_snap); 4797 dump_deadlist(&dp->dp_origin_snap->ds_deadlist); 4798 } 4799 count_dir_mos_objects(dp->dp_mos_dir); 4800 if (dp->dp_free_dir != NULL) 4801 count_dir_mos_objects(dp->dp_free_dir); 4802 if (dp->dp_leak_dir != NULL) 4803 count_dir_mos_objects(dp->dp_leak_dir); 4804 4805 mos_leak_vdev(spa->spa_root_vdev); 4806 4807 for (uint64_t class = 0; class < DDT_CLASSES; class++) { 4808 for (uint64_t type = 0; type < DDT_TYPES; type++) { 4809 for (uint64_t cksum = 0; 4810 cksum < ZIO_CHECKSUM_FUNCTIONS; cksum++) { 4811 ddt_t *ddt = spa->spa_ddt[cksum]; 4812 mos_obj_refd(ddt->ddt_object[type][class]); 4813 } 4814 } 4815 } 4816 4817 /* 4818 * Visit all allocated objects and make sure they are referenced. 4819 */ 4820 uint64_t object = 0; 4821 while (dmu_object_next(mos, &object, B_FALSE, 0) == 0) { 4822 if (range_tree_contains(mos_refd_objs, object, 1)) { 4823 range_tree_remove(mos_refd_objs, object, 1); 4824 } else { 4825 dmu_object_info_t doi; 4826 const char *name; 4827 dmu_object_info(mos, object, &doi); 4828 if (doi.doi_type & DMU_OT_NEWTYPE) { 4829 dmu_object_byteswap_t bswap = 4830 DMU_OT_BYTESWAP(doi.doi_type); 4831 name = dmu_ot_byteswap[bswap].ob_name; 4832 } else { 4833 name = dmu_ot[doi.doi_type].ot_name; 4834 } 4835 4836 (void) printf("MOS object %llu (%s) leaked\n", 4837 (u_longlong_t)object, name); 4838 rv = 2; 4839 } 4840 } 4841 (void) range_tree_walk(mos_refd_objs, mos_leaks_cb, NULL); 4842 if (!range_tree_is_empty(mos_refd_objs)) 4843 rv = 2; 4844 range_tree_vacate(mos_refd_objs, NULL, NULL); 4845 range_tree_destroy(mos_refd_objs); 4846 return (rv); 4847 } 4848 4849 static void 4850 dump_zpool(spa_t *spa) 4851 { 4852 dsl_pool_t *dp = spa_get_dsl(spa); 4853 int rc = 0; 4854 4855 if (dump_opt['S']) { 4856 dump_simulated_ddt(spa); 4857 return; 4858 } 4859 4860 if (!dump_opt['e'] && dump_opt['C'] > 1) { 4861 (void) printf("\nCached configuration:\n"); 4862 dump_nvlist(spa->spa_config, 8); 4863 } 4864 4865 if (dump_opt['C']) 4866 dump_config(spa); 4867 4868 if (dump_opt['u']) 4869 dump_uberblock(&spa->spa_uberblock, "\nUberblock:\n", "\n"); 4870 4871 if (dump_opt['D']) 4872 dump_all_ddts(spa); 4873 4874 if (dump_opt['d'] > 2 || dump_opt['m']) 4875 dump_metaslabs(spa); 4876 if (dump_opt['M']) 4877 dump_metaslab_groups(spa); 4878 4879 if (dump_opt['d'] || dump_opt['i']) { 4880 mos_refd_objs = range_tree_create(NULL, NULL); 4881 dump_dir(dp->dp_meta_objset); 4882 4883 if (dump_opt['d'] >= 3) { 4884 dsl_pool_t *dp = spa->spa_dsl_pool; 4885 dump_full_bpobj(&spa->spa_deferred_bpobj, 4886 "Deferred frees", 0); 4887 if (spa_version(spa) >= SPA_VERSION_DEADLISTS) { 4888 dump_full_bpobj(&dp->dp_free_bpobj, 4889 "Pool snapshot frees", 0); 4890 } 4891 if (bpobj_is_open(&dp->dp_obsolete_bpobj)) { 4892 ASSERT(spa_feature_is_enabled(spa, 4893 SPA_FEATURE_DEVICE_REMOVAL)); 4894 dump_full_bpobj(&dp->dp_obsolete_bpobj, 4895 "Pool obsolete blocks", 0); 4896 } 4897 4898 if (spa_feature_is_active(spa, 4899 SPA_FEATURE_ASYNC_DESTROY)) { 4900 dump_bptree(spa->spa_meta_objset, 4901 dp->dp_bptree_obj, 4902 "Pool dataset frees"); 4903 } 4904 dump_dtl(spa->spa_root_vdev, 0); 4905 } 4906 (void) dmu_objset_find(spa_name(spa), dump_one_dir, 4907 NULL, DS_FIND_SNAPSHOTS | DS_FIND_CHILDREN); 4908 4909 if (rc == 0 && !dump_opt['L']) 4910 rc = dump_mos_leaks(spa); 4911 4912 for (spa_feature_t f = 0; f < SPA_FEATURES; f++) { 4913 uint64_t refcount; 4914 4915 if (!(spa_feature_table[f].fi_flags & 4916 ZFEATURE_FLAG_PER_DATASET) || 4917 !spa_feature_is_enabled(spa, f)) { 4918 ASSERT0(dataset_feature_count[f]); 4919 continue; 4920 } 4921 (void) feature_get_refcount(spa, 4922 &spa_feature_table[f], &refcount); 4923 if (dataset_feature_count[f] != refcount) { 4924 (void) printf("%s feature refcount mismatch: " 4925 "%lld datasets != %lld refcount\n", 4926 spa_feature_table[f].fi_uname, 4927 (longlong_t)dataset_feature_count[f], 4928 (longlong_t)refcount); 4929 rc = 2; 4930 } else { 4931 (void) printf("Verified %s feature refcount " 4932 "of %llu is correct\n", 4933 spa_feature_table[f].fi_uname, 4934 (longlong_t)refcount); 4935 } 4936 } 4937 4938 if (rc == 0) { 4939 rc = verify_device_removal_feature_counts(spa); 4940 } 4941 } 4942 4943 if (rc == 0 && (dump_opt['b'] || dump_opt['c'])) 4944 rc = dump_block_stats(spa); 4945 4946 if (rc == 0) 4947 rc = verify_spacemap_refcounts(spa); 4948 4949 if (dump_opt['s']) 4950 show_pool_stats(spa); 4951 4952 if (dump_opt['h']) 4953 dump_history(spa); 4954 4955 if (rc == 0) 4956 rc = verify_checkpoint(spa); 4957 4958 if (rc != 0) { 4959 dump_debug_buffer(); 4960 exit(rc); 4961 } 4962 } 4963 4964 #define ZDB_FLAG_CHECKSUM 0x0001 4965 #define ZDB_FLAG_DECOMPRESS 0x0002 4966 #define ZDB_FLAG_BSWAP 0x0004 4967 #define ZDB_FLAG_GBH 0x0008 4968 #define ZDB_FLAG_INDIRECT 0x0010 4969 #define ZDB_FLAG_PHYS 0x0020 4970 #define ZDB_FLAG_RAW 0x0040 4971 #define ZDB_FLAG_PRINT_BLKPTR 0x0080 4972 4973 static int flagbits[256]; 4974 4975 static void 4976 zdb_print_blkptr(blkptr_t *bp, int flags) 4977 { 4978 char blkbuf[BP_SPRINTF_LEN]; 4979 4980 if (flags & ZDB_FLAG_BSWAP) 4981 byteswap_uint64_array((void *)bp, sizeof (blkptr_t)); 4982 4983 snprintf_blkptr(blkbuf, sizeof (blkbuf), bp); 4984 (void) printf("%s\n", blkbuf); 4985 } 4986 4987 static void 4988 zdb_dump_indirect(blkptr_t *bp, int nbps, int flags) 4989 { 4990 int i; 4991 4992 for (i = 0; i < nbps; i++) 4993 zdb_print_blkptr(&bp[i], flags); 4994 } 4995 4996 static void 4997 zdb_dump_gbh(void *buf, int flags) 4998 { 4999 zdb_dump_indirect((blkptr_t *)buf, SPA_GBH_NBLKPTRS, flags); 5000 } 5001 5002 static void 5003 zdb_dump_block_raw(void *buf, uint64_t size, int flags) 5004 { 5005 if (flags & ZDB_FLAG_BSWAP) 5006 byteswap_uint64_array(buf, size); 5007 (void) write(1, buf, size); 5008 } 5009 5010 static void 5011 zdb_dump_block(char *label, void *buf, uint64_t size, int flags) 5012 { 5013 uint64_t *d = (uint64_t *)buf; 5014 unsigned nwords = size / sizeof (uint64_t); 5015 int do_bswap = !!(flags & ZDB_FLAG_BSWAP); 5016 unsigned i, j; 5017 const char *hdr; 5018 char *c; 5019 5020 5021 if (do_bswap) 5022 hdr = " 7 6 5 4 3 2 1 0 f e d c b a 9 8"; 5023 else 5024 hdr = " 0 1 2 3 4 5 6 7 8 9 a b c d e f"; 5025 5026 (void) printf("\n%s\n%6s %s 0123456789abcdef\n", label, "", hdr); 5027 5028 for (i = 0; i < nwords; i += 2) { 5029 (void) printf("%06llx: %016llx %016llx ", 5030 (u_longlong_t)(i * sizeof (uint64_t)), 5031 (u_longlong_t)(do_bswap ? BSWAP_64(d[i]) : d[i]), 5032 (u_longlong_t)(do_bswap ? BSWAP_64(d[i + 1]) : d[i + 1])); 5033 5034 c = (char *)&d[i]; 5035 for (j = 0; j < 2 * sizeof (uint64_t); j++) 5036 (void) printf("%c", isprint(c[j]) ? c[j] : '.'); 5037 (void) printf("\n"); 5038 } 5039 } 5040 5041 /* 5042 * There are two acceptable formats: 5043 * leaf_name - For example: c1t0d0 or /tmp/ztest.0a 5044 * child[.child]* - For example: 0.1.1 5045 * 5046 * The second form can be used to specify arbitrary vdevs anywhere 5047 * in the heirarchy. For example, in a pool with a mirror of 5048 * RAID-Zs, you can specify either RAID-Z vdev with 0.0 or 0.1 . 5049 */ 5050 static vdev_t * 5051 zdb_vdev_lookup(vdev_t *vdev, const char *path) 5052 { 5053 char *s, *p, *q; 5054 unsigned i; 5055 5056 if (vdev == NULL) 5057 return (NULL); 5058 5059 /* First, assume the x.x.x.x format */ 5060 i = strtoul(path, &s, 10); 5061 if (s == path || (s && *s != '.' && *s != '\0')) 5062 goto name; 5063 if (i >= vdev->vdev_children) 5064 return (NULL); 5065 5066 vdev = vdev->vdev_child[i]; 5067 if (*s == '\0') 5068 return (vdev); 5069 return (zdb_vdev_lookup(vdev, s+1)); 5070 5071 name: 5072 for (i = 0; i < vdev->vdev_children; i++) { 5073 vdev_t *vc = vdev->vdev_child[i]; 5074 5075 if (vc->vdev_path == NULL) { 5076 vc = zdb_vdev_lookup(vc, path); 5077 if (vc == NULL) 5078 continue; 5079 else 5080 return (vc); 5081 } 5082 5083 p = strrchr(vc->vdev_path, '/'); 5084 p = p ? p + 1 : vc->vdev_path; 5085 q = &vc->vdev_path[strlen(vc->vdev_path) - 2]; 5086 5087 if (strcmp(vc->vdev_path, path) == 0) 5088 return (vc); 5089 if (strcmp(p, path) == 0) 5090 return (vc); 5091 if (strcmp(q, "s0") == 0 && strncmp(p, path, q - p) == 0) 5092 return (vc); 5093 } 5094 5095 return (NULL); 5096 } 5097 5098 /* ARGSUSED */ 5099 static int 5100 random_get_pseudo_bytes_cb(void *buf, size_t len, void *unused) 5101 { 5102 return (random_get_pseudo_bytes(buf, len)); 5103 } 5104 5105 /* 5106 * Read a block from a pool and print it out. The syntax of the 5107 * block descriptor is: 5108 * 5109 * pool:vdev_specifier:offset:size[:flags] 5110 * 5111 * pool - The name of the pool you wish to read from 5112 * vdev_specifier - Which vdev (see comment for zdb_vdev_lookup) 5113 * offset - offset, in hex, in bytes 5114 * size - Amount of data to read, in hex, in bytes 5115 * flags - A string of characters specifying options 5116 * b: Decode a blkptr at given offset within block 5117 * *c: Calculate and display checksums 5118 * d: Decompress data before dumping 5119 * e: Byteswap data before dumping 5120 * g: Display data as a gang block header 5121 * i: Display as an indirect block 5122 * p: Do I/O to physical offset 5123 * r: Dump raw data to stdout 5124 * 5125 * * = not yet implemented 5126 */ 5127 static void 5128 zdb_read_block(char *thing, spa_t *spa) 5129 { 5130 blkptr_t blk, *bp = &blk; 5131 dva_t *dva = bp->blk_dva; 5132 int flags = 0; 5133 uint64_t offset = 0, size = 0, psize = 0, lsize = 0, blkptr_offset = 0; 5134 zio_t *zio; 5135 vdev_t *vd; 5136 abd_t *pabd; 5137 void *lbuf, *buf; 5138 const char *s, *vdev; 5139 char *p, *dup, *flagstr; 5140 int i, error; 5141 5142 dup = strdup(thing); 5143 s = strtok(dup, ":"); 5144 vdev = s ? s : ""; 5145 s = strtok(NULL, ":"); 5146 offset = strtoull(s ? s : "", NULL, 16); 5147 s = strtok(NULL, ":"); 5148 size = strtoull(s ? s : "", NULL, 16); 5149 s = strtok(NULL, ":"); 5150 if (s) 5151 flagstr = strdup(s); 5152 else 5153 flagstr = strdup(""); 5154 5155 s = NULL; 5156 if (size == 0) 5157 s = "size must not be zero"; 5158 if (!IS_P2ALIGNED(size, DEV_BSIZE)) 5159 s = "size must be a multiple of sector size"; 5160 if (!IS_P2ALIGNED(offset, DEV_BSIZE)) 5161 s = "offset must be a multiple of sector size"; 5162 if (s) { 5163 (void) printf("Invalid block specifier: %s - %s\n", thing, s); 5164 free(dup); 5165 return; 5166 } 5167 5168 for (s = strtok(flagstr, ":"); s; s = strtok(NULL, ":")) { 5169 for (i = 0; flagstr[i]; i++) { 5170 int bit = flagbits[(uchar_t)flagstr[i]]; 5171 5172 if (bit == 0) { 5173 (void) printf("***Invalid flag: %c\n", 5174 flagstr[i]); 5175 continue; 5176 } 5177 flags |= bit; 5178 5179 /* If it's not something with an argument, keep going */ 5180 if ((bit & (ZDB_FLAG_CHECKSUM | 5181 ZDB_FLAG_PRINT_BLKPTR)) == 0) 5182 continue; 5183 5184 p = &flagstr[i + 1]; 5185 if (bit == ZDB_FLAG_PRINT_BLKPTR) 5186 blkptr_offset = strtoull(p, &p, 16); 5187 if (*p != ':' && *p != '\0') { 5188 (void) printf("***Invalid flag arg: '%s'\n", s); 5189 free(dup); 5190 return; 5191 } 5192 } 5193 } 5194 free(flagstr); 5195 5196 vd = zdb_vdev_lookup(spa->spa_root_vdev, vdev); 5197 if (vd == NULL) { 5198 (void) printf("***Invalid vdev: %s\n", vdev); 5199 free(dup); 5200 return; 5201 } else { 5202 if (vd->vdev_path) 5203 (void) fprintf(stderr, "Found vdev: %s\n", 5204 vd->vdev_path); 5205 else 5206 (void) fprintf(stderr, "Found vdev type: %s\n", 5207 vd->vdev_ops->vdev_op_type); 5208 } 5209 5210 psize = size; 5211 lsize = size; 5212 5213 pabd = abd_alloc_linear(SPA_MAXBLOCKSIZE, B_FALSE); 5214 lbuf = umem_alloc(SPA_MAXBLOCKSIZE, UMEM_NOFAIL); 5215 5216 BP_ZERO(bp); 5217 5218 DVA_SET_VDEV(&dva[0], vd->vdev_id); 5219 DVA_SET_OFFSET(&dva[0], offset); 5220 DVA_SET_GANG(&dva[0], !!(flags & ZDB_FLAG_GBH)); 5221 DVA_SET_ASIZE(&dva[0], vdev_psize_to_asize(vd, psize)); 5222 5223 BP_SET_BIRTH(bp, TXG_INITIAL, TXG_INITIAL); 5224 5225 BP_SET_LSIZE(bp, lsize); 5226 BP_SET_PSIZE(bp, psize); 5227 BP_SET_COMPRESS(bp, ZIO_COMPRESS_OFF); 5228 BP_SET_CHECKSUM(bp, ZIO_CHECKSUM_OFF); 5229 BP_SET_TYPE(bp, DMU_OT_NONE); 5230 BP_SET_LEVEL(bp, 0); 5231 BP_SET_DEDUP(bp, 0); 5232 BP_SET_BYTEORDER(bp, ZFS_HOST_BYTEORDER); 5233 5234 spa_config_enter(spa, SCL_STATE, FTAG, RW_READER); 5235 zio = zio_root(spa, NULL, NULL, 0); 5236 5237 if (vd == vd->vdev_top) { 5238 /* 5239 * Treat this as a normal block read. 5240 */ 5241 zio_nowait(zio_read(zio, spa, bp, pabd, psize, NULL, NULL, 5242 ZIO_PRIORITY_SYNC_READ, 5243 ZIO_FLAG_CANFAIL | ZIO_FLAG_RAW, NULL)); 5244 } else { 5245 /* 5246 * Treat this as a vdev child I/O. 5247 */ 5248 zio_nowait(zio_vdev_child_io(zio, bp, vd, offset, pabd, 5249 psize, ZIO_TYPE_READ, ZIO_PRIORITY_SYNC_READ, 5250 ZIO_FLAG_DONT_CACHE | ZIO_FLAG_DONT_QUEUE | 5251 ZIO_FLAG_DONT_PROPAGATE | ZIO_FLAG_DONT_RETRY | 5252 ZIO_FLAG_CANFAIL | ZIO_FLAG_RAW | ZIO_FLAG_OPTIONAL, 5253 NULL, NULL)); 5254 } 5255 5256 error = zio_wait(zio); 5257 spa_config_exit(spa, SCL_STATE, FTAG); 5258 5259 if (error) { 5260 (void) printf("Read of %s failed, error: %d\n", thing, error); 5261 goto out; 5262 } 5263 5264 if (flags & ZDB_FLAG_DECOMPRESS) { 5265 /* 5266 * We don't know how the data was compressed, so just try 5267 * every decompress function at every inflated blocksize. 5268 */ 5269 enum zio_compress c; 5270 void *pbuf2 = umem_alloc(SPA_MAXBLOCKSIZE, UMEM_NOFAIL); 5271 void *lbuf2 = umem_alloc(SPA_MAXBLOCKSIZE, UMEM_NOFAIL); 5272 5273 abd_copy_to_buf(pbuf2, pabd, psize); 5274 5275 VERIFY0(abd_iterate_func(pabd, psize, SPA_MAXBLOCKSIZE - psize, 5276 random_get_pseudo_bytes_cb, NULL)); 5277 5278 VERIFY0(random_get_pseudo_bytes((uint8_t *)pbuf2 + psize, 5279 SPA_MAXBLOCKSIZE - psize)); 5280 5281 for (lsize = SPA_MAXBLOCKSIZE; lsize > psize; 5282 lsize -= SPA_MINBLOCKSIZE) { 5283 for (c = 0; c < ZIO_COMPRESS_FUNCTIONS; c++) { 5284 if (zio_decompress_data(c, pabd, 5285 lbuf, psize, lsize) == 0 && 5286 zio_decompress_data_buf(c, pbuf2, 5287 lbuf2, psize, lsize) == 0 && 5288 bcmp(lbuf, lbuf2, lsize) == 0) 5289 break; 5290 } 5291 if (c != ZIO_COMPRESS_FUNCTIONS) 5292 break; 5293 lsize -= SPA_MINBLOCKSIZE; 5294 } 5295 5296 umem_free(pbuf2, SPA_MAXBLOCKSIZE); 5297 umem_free(lbuf2, SPA_MAXBLOCKSIZE); 5298 5299 if (lsize <= psize) { 5300 (void) printf("Decompress of %s failed\n", thing); 5301 goto out; 5302 } 5303 buf = lbuf; 5304 size = lsize; 5305 } else { 5306 buf = abd_to_buf(pabd); 5307 size = psize; 5308 } 5309 5310 if (flags & ZDB_FLAG_PRINT_BLKPTR) 5311 zdb_print_blkptr((blkptr_t *)(void *) 5312 ((uintptr_t)buf + (uintptr_t)blkptr_offset), flags); 5313 else if (flags & ZDB_FLAG_RAW) 5314 zdb_dump_block_raw(buf, size, flags); 5315 else if (flags & ZDB_FLAG_INDIRECT) 5316 zdb_dump_indirect((blkptr_t *)buf, size / sizeof (blkptr_t), 5317 flags); 5318 else if (flags & ZDB_FLAG_GBH) 5319 zdb_dump_gbh(buf, flags); 5320 else 5321 zdb_dump_block(thing, buf, size, flags); 5322 5323 out: 5324 abd_free(pabd); 5325 umem_free(lbuf, SPA_MAXBLOCKSIZE); 5326 free(dup); 5327 } 5328 5329 static void 5330 zdb_embedded_block(char *thing) 5331 { 5332 blkptr_t bp; 5333 unsigned long long *words = (void *)&bp; 5334 char *buf; 5335 int err; 5336 5337 bzero(&bp, sizeof (bp)); 5338 err = sscanf(thing, "%llx:%llx:%llx:%llx:%llx:%llx:%llx:%llx:" 5339 "%llx:%llx:%llx:%llx:%llx:%llx:%llx:%llx", 5340 words + 0, words + 1, words + 2, words + 3, 5341 words + 4, words + 5, words + 6, words + 7, 5342 words + 8, words + 9, words + 10, words + 11, 5343 words + 12, words + 13, words + 14, words + 15); 5344 if (err != 16) { 5345 (void) fprintf(stderr, "invalid input format\n"); 5346 exit(1); 5347 } 5348 ASSERT3U(BPE_GET_LSIZE(&bp), <=, SPA_MAXBLOCKSIZE); 5349 buf = malloc(SPA_MAXBLOCKSIZE); 5350 if (buf == NULL) { 5351 (void) fprintf(stderr, "out of memory\n"); 5352 exit(1); 5353 } 5354 err = decode_embedded_bp(&bp, buf, BPE_GET_LSIZE(&bp)); 5355 if (err != 0) { 5356 (void) fprintf(stderr, "decode failed: %u\n", err); 5357 exit(1); 5358 } 5359 zdb_dump_block_raw(buf, BPE_GET_LSIZE(&bp), 0); 5360 free(buf); 5361 } 5362 5363 int 5364 main(int argc, char **argv) 5365 { 5366 int c; 5367 struct rlimit rl = { 1024, 1024 }; 5368 spa_t *spa = NULL; 5369 objset_t *os = NULL; 5370 int dump_all = 1; 5371 int verbose = 0; 5372 int error = 0; 5373 char **searchdirs = NULL; 5374 int nsearch = 0; 5375 char *target, *target_pool; 5376 nvlist_t *policy = NULL; 5377 uint64_t max_txg = UINT64_MAX; 5378 int flags = ZFS_IMPORT_MISSING_LOG; 5379 int rewind = ZPOOL_NEVER_REWIND; 5380 char *spa_config_path_env; 5381 boolean_t target_is_spa = B_TRUE; 5382 nvlist_t *cfg = NULL; 5383 5384 (void) setrlimit(RLIMIT_NOFILE, &rl); 5385 (void) enable_extended_FILE_stdio(-1, -1); 5386 5387 dprintf_setup(&argc, argv); 5388 5389 /* 5390 * If there is an environment variable SPA_CONFIG_PATH it overrides 5391 * default spa_config_path setting. If -U flag is specified it will 5392 * override this environment variable settings once again. 5393 */ 5394 spa_config_path_env = getenv("SPA_CONFIG_PATH"); 5395 if (spa_config_path_env != NULL) 5396 spa_config_path = spa_config_path_env; 5397 5398 while ((c = getopt(argc, argv, 5399 "AbcCdDeEFGhiI:klLmMo:Op:PqRsSt:uU:vVx:X")) != -1) { 5400 switch (c) { 5401 case 'b': 5402 case 'c': 5403 case 'C': 5404 case 'd': 5405 case 'D': 5406 case 'E': 5407 case 'G': 5408 case 'h': 5409 case 'i': 5410 case 'l': 5411 case 'm': 5412 case 'M': 5413 case 'O': 5414 case 'R': 5415 case 's': 5416 case 'S': 5417 case 'u': 5418 dump_opt[c]++; 5419 dump_all = 0; 5420 break; 5421 case 'A': 5422 case 'e': 5423 case 'F': 5424 case 'k': 5425 case 'L': 5426 case 'P': 5427 case 'q': 5428 case 'X': 5429 dump_opt[c]++; 5430 break; 5431 /* NB: Sort single match options below. */ 5432 case 'I': 5433 max_inflight = strtoull(optarg, NULL, 0); 5434 if (max_inflight == 0) { 5435 (void) fprintf(stderr, "maximum number " 5436 "of inflight I/Os must be greater " 5437 "than 0\n"); 5438 usage(); 5439 } 5440 break; 5441 case 'o': 5442 error = set_global_var(optarg); 5443 if (error != 0) 5444 usage(); 5445 break; 5446 case 'p': 5447 if (searchdirs == NULL) { 5448 searchdirs = umem_alloc(sizeof (char *), 5449 UMEM_NOFAIL); 5450 } else { 5451 char **tmp = umem_alloc((nsearch + 1) * 5452 sizeof (char *), UMEM_NOFAIL); 5453 bcopy(searchdirs, tmp, nsearch * 5454 sizeof (char *)); 5455 umem_free(searchdirs, 5456 nsearch * sizeof (char *)); 5457 searchdirs = tmp; 5458 } 5459 searchdirs[nsearch++] = optarg; 5460 break; 5461 case 't': 5462 max_txg = strtoull(optarg, NULL, 0); 5463 if (max_txg < TXG_INITIAL) { 5464 (void) fprintf(stderr, "incorrect txg " 5465 "specified: %s\n", optarg); 5466 usage(); 5467 } 5468 break; 5469 case 'U': 5470 spa_config_path = optarg; 5471 if (spa_config_path[0] != '/') { 5472 (void) fprintf(stderr, 5473 "cachefile must be an absolute path " 5474 "(i.e. start with a slash)\n"); 5475 usage(); 5476 } 5477 break; 5478 case 'v': 5479 verbose++; 5480 break; 5481 case 'V': 5482 flags = ZFS_IMPORT_VERBATIM; 5483 break; 5484 case 'x': 5485 vn_dumpdir = optarg; 5486 break; 5487 default: 5488 usage(); 5489 break; 5490 } 5491 } 5492 5493 if (!dump_opt['e'] && searchdirs != NULL) { 5494 (void) fprintf(stderr, "-p option requires use of -e\n"); 5495 usage(); 5496 } 5497 5498 /* 5499 * ZDB does not typically re-read blocks; therefore limit the ARC 5500 * to 256 MB, which can be used entirely for metadata. 5501 */ 5502 zfs_arc_max = zfs_arc_meta_limit = 256 * 1024 * 1024; 5503 5504 /* 5505 * "zdb -c" uses checksum-verifying scrub i/os which are async reads. 5506 * "zdb -b" uses traversal prefetch which uses async reads. 5507 * For good performance, let several of them be active at once. 5508 */ 5509 zfs_vdev_async_read_max_active = 10; 5510 5511 /* 5512 * Disable reference tracking for better performance. 5513 */ 5514 reference_tracking_enable = B_FALSE; 5515 5516 /* 5517 * Do not fail spa_load when spa_load_verify fails. This is needed 5518 * to load non-idle pools. 5519 */ 5520 spa_load_verify_dryrun = B_TRUE; 5521 5522 kernel_init(FREAD); 5523 g_zfs = libzfs_init(); 5524 ASSERT(g_zfs != NULL); 5525 5526 if (dump_all) 5527 verbose = MAX(verbose, 1); 5528 5529 for (c = 0; c < 256; c++) { 5530 if (dump_all && strchr("AeEFklLOPRSX", c) == NULL) 5531 dump_opt[c] = 1; 5532 if (dump_opt[c]) 5533 dump_opt[c] += verbose; 5534 } 5535 5536 aok = (dump_opt['A'] == 1) || (dump_opt['A'] > 2); 5537 zfs_recover = (dump_opt['A'] > 1); 5538 5539 argc -= optind; 5540 argv += optind; 5541 5542 if (argc < 2 && dump_opt['R']) 5543 usage(); 5544 5545 if (dump_opt['E']) { 5546 if (argc != 1) 5547 usage(); 5548 zdb_embedded_block(argv[0]); 5549 return (0); 5550 } 5551 5552 if (argc < 1) { 5553 if (!dump_opt['e'] && dump_opt['C']) { 5554 dump_cachefile(spa_config_path); 5555 return (0); 5556 } 5557 usage(); 5558 } 5559 5560 if (dump_opt['l']) 5561 return (dump_label(argv[0])); 5562 5563 if (dump_opt['O']) { 5564 if (argc != 2) 5565 usage(); 5566 dump_opt['v'] = verbose + 3; 5567 return (dump_path(argv[0], argv[1])); 5568 } 5569 5570 if (dump_opt['X'] || dump_opt['F']) 5571 rewind = ZPOOL_DO_REWIND | 5572 (dump_opt['X'] ? ZPOOL_EXTREME_REWIND : 0); 5573 5574 if (nvlist_alloc(&policy, NV_UNIQUE_NAME_TYPE, 0) != 0 || 5575 nvlist_add_uint64(policy, ZPOOL_LOAD_REQUEST_TXG, max_txg) != 0 || 5576 nvlist_add_uint32(policy, ZPOOL_LOAD_REWIND_POLICY, rewind) != 0) 5577 fatal("internal error: %s", strerror(ENOMEM)); 5578 5579 error = 0; 5580 target = argv[0]; 5581 5582 if (strpbrk(target, "/@") != NULL) { 5583 size_t targetlen; 5584 5585 target_pool = strdup(target); 5586 *strpbrk(target_pool, "/@") = '\0'; 5587 5588 target_is_spa = B_FALSE; 5589 targetlen = strlen(target); 5590 if (targetlen && target[targetlen - 1] == '/') 5591 target[targetlen - 1] = '\0'; 5592 } else { 5593 target_pool = target; 5594 } 5595 5596 if (dump_opt['e']) { 5597 importargs_t args = { 0 }; 5598 5599 args.paths = nsearch; 5600 args.path = searchdirs; 5601 args.can_be_active = B_TRUE; 5602 5603 error = zpool_tryimport(g_zfs, target_pool, &cfg, &args); 5604 5605 if (error == 0) { 5606 5607 if (nvlist_add_nvlist(cfg, 5608 ZPOOL_LOAD_POLICY, policy) != 0) { 5609 fatal("can't open '%s': %s", 5610 target, strerror(ENOMEM)); 5611 } 5612 5613 if (dump_opt['C'] > 1) { 5614 (void) printf("\nConfiguration for import:\n"); 5615 dump_nvlist(cfg, 8); 5616 } 5617 5618 /* 5619 * Disable the activity check to allow examination of 5620 * active pools. 5621 */ 5622 error = spa_import(target_pool, cfg, NULL, 5623 flags | ZFS_IMPORT_SKIP_MMP); 5624 } 5625 } 5626 5627 char *checkpoint_pool = NULL; 5628 char *checkpoint_target = NULL; 5629 if (dump_opt['k']) { 5630 checkpoint_pool = import_checkpointed_state(target, cfg, 5631 &checkpoint_target); 5632 5633 if (checkpoint_target != NULL) 5634 target = checkpoint_target; 5635 5636 } 5637 5638 if (error == 0) { 5639 if (dump_opt['k'] && (target_is_spa || dump_opt['R'])) { 5640 ASSERT(checkpoint_pool != NULL); 5641 ASSERT(checkpoint_target == NULL); 5642 5643 error = spa_open(checkpoint_pool, &spa, FTAG); 5644 if (error != 0) { 5645 fatal("Tried to open pool \"%s\" but " 5646 "spa_open() failed with error %d\n", 5647 checkpoint_pool, error); 5648 } 5649 5650 } else if (target_is_spa || dump_opt['R']) { 5651 zdb_set_skip_mmp(target); 5652 error = spa_open_rewind(target, &spa, FTAG, policy, 5653 NULL); 5654 if (error) { 5655 /* 5656 * If we're missing the log device then 5657 * try opening the pool after clearing the 5658 * log state. 5659 */ 5660 mutex_enter(&spa_namespace_lock); 5661 if ((spa = spa_lookup(target)) != NULL && 5662 spa->spa_log_state == SPA_LOG_MISSING) { 5663 spa->spa_log_state = SPA_LOG_CLEAR; 5664 error = 0; 5665 } 5666 mutex_exit(&spa_namespace_lock); 5667 5668 if (!error) { 5669 error = spa_open_rewind(target, &spa, 5670 FTAG, policy, NULL); 5671 } 5672 } 5673 } else { 5674 zdb_set_skip_mmp(target); 5675 error = open_objset(target, DMU_OST_ANY, FTAG, &os); 5676 } 5677 } 5678 nvlist_free(policy); 5679 5680 if (error) 5681 fatal("can't open '%s': %s", target, strerror(error)); 5682 5683 argv++; 5684 argc--; 5685 if (!dump_opt['R']) { 5686 if (argc > 0) { 5687 zopt_objects = argc; 5688 zopt_object = calloc(zopt_objects, sizeof (uint64_t)); 5689 for (unsigned i = 0; i < zopt_objects; i++) { 5690 errno = 0; 5691 zopt_object[i] = strtoull(argv[i], NULL, 0); 5692 if (zopt_object[i] == 0 && errno != 0) 5693 fatal("bad number %s: %s", 5694 argv[i], strerror(errno)); 5695 } 5696 } 5697 if (os != NULL) { 5698 dump_dir(os); 5699 } else if (zopt_objects > 0 && !dump_opt['m']) { 5700 dump_dir(spa->spa_meta_objset); 5701 } else { 5702 dump_zpool(spa); 5703 } 5704 } else { 5705 flagbits['b'] = ZDB_FLAG_PRINT_BLKPTR; 5706 flagbits['c'] = ZDB_FLAG_CHECKSUM; 5707 flagbits['d'] = ZDB_FLAG_DECOMPRESS; 5708 flagbits['e'] = ZDB_FLAG_BSWAP; 5709 flagbits['g'] = ZDB_FLAG_GBH; 5710 flagbits['i'] = ZDB_FLAG_INDIRECT; 5711 flagbits['p'] = ZDB_FLAG_PHYS; 5712 flagbits['r'] = ZDB_FLAG_RAW; 5713 5714 for (int i = 0; i < argc; i++) 5715 zdb_read_block(argv[i], spa); 5716 } 5717 5718 if (dump_opt['k']) { 5719 free(checkpoint_pool); 5720 if (!target_is_spa) 5721 free(checkpoint_target); 5722 } 5723 5724 if (os != NULL) 5725 close_objset(os, FTAG); 5726 else 5727 spa_close(spa, FTAG); 5728 5729 fuid_table_destroy(); 5730 5731 dump_debug_buffer(); 5732 5733 libzfs_fini(g_zfs); 5734 kernel_fini(); 5735 5736 return (error); 5737 } 5738