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