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