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