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