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