zdb.c revision 321610
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, 2016 by Delphix. All rights reserved. 25 * Copyright (c) 2014 Integros [integros.com] 26 */ 27 28#include <stdio.h> 29#include <unistd.h> 30#include <stdio_ext.h> 31#include <stdlib.h> 32#include <ctype.h> 33#include <sys/zfs_context.h> 34#include <sys/spa.h> 35#include <sys/spa_impl.h> 36#include <sys/dmu.h> 37#include <sys/zap.h> 38#include <sys/fs/zfs.h> 39#include <sys/zfs_znode.h> 40#include <sys/zfs_sa.h> 41#include <sys/sa.h> 42#include <sys/sa_impl.h> 43#include <sys/vdev.h> 44#include <sys/vdev_impl.h> 45#include <sys/metaslab_impl.h> 46#include <sys/dmu_objset.h> 47#include <sys/dsl_dir.h> 48#include <sys/dsl_dataset.h> 49#include <sys/dsl_pool.h> 50#include <sys/dbuf.h> 51#include <sys/zil.h> 52#include <sys/zil_impl.h> 53#include <sys/stat.h> 54#include <sys/resource.h> 55#include <sys/dmu_traverse.h> 56#include <sys/zio_checksum.h> 57#include <sys/zio_compress.h> 58#include <sys/zfs_fuid.h> 59#include <sys/arc.h> 60#include <sys/ddt.h> 61#include <sys/zfeature.h> 62#include <sys/abd.h> 63#include <zfs_comutil.h> 64#undef verify 65#include <libzfs.h> 66 67#define ZDB_COMPRESS_NAME(idx) ((idx) < ZIO_COMPRESS_FUNCTIONS ? \ 68 zio_compress_table[(idx)].ci_name : "UNKNOWN") 69#define ZDB_CHECKSUM_NAME(idx) ((idx) < ZIO_CHECKSUM_FUNCTIONS ? \ 70 zio_checksum_table[(idx)].ci_name : "UNKNOWN") 71#define ZDB_OT_NAME(idx) ((idx) < DMU_OT_NUMTYPES ? \ 72 dmu_ot[(idx)].ot_name : DMU_OT_IS_VALID(idx) ? \ 73 dmu_ot_byteswap[DMU_OT_BYTESWAP(idx)].ob_name : "UNKNOWN") 74#define ZDB_OT_TYPE(idx) ((idx) < DMU_OT_NUMTYPES ? (idx) : \ 75 (((idx) == DMU_OTN_ZAP_DATA || (idx) == DMU_OTN_ZAP_METADATA) ? \ 76 DMU_OT_ZAP_OTHER : DMU_OT_NUMTYPES)) 77 78#ifndef lint 79extern int reference_tracking_enable; 80extern boolean_t zfs_recover; 81extern uint64_t zfs_arc_max, zfs_arc_meta_limit; 82extern int zfs_vdev_async_read_max_active; 83#else 84int reference_tracking_enable; 85boolean_t zfs_recover; 86uint64_t zfs_arc_max, zfs_arc_meta_limit; 87int zfs_vdev_async_read_max_active; 88#endif 89 90const char cmdname[] = "zdb"; 91uint8_t dump_opt[256]; 92 93typedef void object_viewer_t(objset_t *, uint64_t, void *data, size_t size); 94 95extern void dump_intent_log(zilog_t *); 96static uint64_t *zopt_object = NULL; 97static int zopt_objects = 0; 98static libzfs_handle_t *g_zfs; 99static uint64_t max_inflight = 1000; 100 101static void snprintf_blkptr_compact(char *, size_t, const blkptr_t *); 102 103/* 104 * These libumem hooks provide a reasonable set of defaults for the allocator's 105 * debugging facilities. 106 */ 107const char * 108_umem_debug_init() 109{ 110 return ("default,verbose"); /* $UMEM_DEBUG setting */ 111} 112 113const char * 114_umem_logging_init(void) 115{ 116 return ("fail,contents"); /* $UMEM_LOGGING setting */ 117} 118 119static void 120usage(void) 121{ 122 (void) fprintf(stderr, 123 "Usage: %s [-CumMdibcsDvhLXFPAG] [-t txg] [-e [-p path...]] " 124 "[-U config] [-I inflight I/Os] [-x dumpdir] poolname [object...]\n" 125 " %s [-divPA] [-e -p path...] [-U config] dataset " 126 "[object...]\n" 127 " %s -mM [-LXFPA] [-t txg] [-e [-p path...]] [-U config] " 128 "poolname [vdev [metaslab...]]\n" 129 " %s -R [-A] [-e [-p path...]] poolname " 130 "vdev:offset:size[:flags]\n" 131 " %s -S [-PA] [-e [-p path...]] [-U config] poolname\n" 132 " %s -l [-uA] device\n" 133 " %s -C [-A] [-U config]\n\n", 134 cmdname, cmdname, cmdname, cmdname, cmdname, cmdname, cmdname); 135 136 (void) fprintf(stderr, " Dataset name must include at least one " 137 "separator character '/' or '@'\n"); 138 (void) fprintf(stderr, " If dataset name is specified, only that " 139 "dataset is dumped\n"); 140 (void) fprintf(stderr, " If object numbers are specified, only " 141 "those objects are dumped\n\n"); 142 (void) fprintf(stderr, " Options to control amount of output:\n"); 143 (void) fprintf(stderr, " -u uberblock\n"); 144 (void) fprintf(stderr, " -d dataset(s)\n"); 145 (void) fprintf(stderr, " -i intent logs\n"); 146 (void) fprintf(stderr, " -C config (or cachefile if alone)\n"); 147 (void) fprintf(stderr, " -h pool history\n"); 148 (void) fprintf(stderr, " -b block statistics\n"); 149 (void) fprintf(stderr, " -m metaslabs\n"); 150 (void) fprintf(stderr, " -M metaslab groups\n"); 151 (void) fprintf(stderr, " -c checksum all metadata (twice for " 152 "all data) blocks\n"); 153 (void) fprintf(stderr, " -s report stats on zdb's I/O\n"); 154 (void) fprintf(stderr, " -D dedup statistics\n"); 155 (void) fprintf(stderr, " -S simulate dedup to measure effect\n"); 156 (void) fprintf(stderr, " -v verbose (applies to all others)\n"); 157 (void) fprintf(stderr, " -l dump label contents\n"); 158 (void) fprintf(stderr, " -L disable leak tracking (do not " 159 "load spacemaps)\n"); 160 (void) fprintf(stderr, " -R read and display block from a " 161 "device\n\n"); 162 (void) fprintf(stderr, " Below options are intended for use " 163 "with other options:\n"); 164 (void) fprintf(stderr, " -A ignore assertions (-A), enable " 165 "panic recovery (-AA) or both (-AAA)\n"); 166 (void) fprintf(stderr, " -F attempt automatic rewind within " 167 "safe range of transaction groups\n"); 168 (void) fprintf(stderr, " -U <cachefile_path> -- use alternate " 169 "cachefile\n"); 170 (void) fprintf(stderr, " -X attempt extreme rewind (does not " 171 "work with dataset)\n"); 172 (void) fprintf(stderr, " -e pool is exported/destroyed/" 173 "has altroot/not in a cachefile\n"); 174 (void) fprintf(stderr, " -p <path> -- use one or more with " 175 "-e to specify path to vdev dir\n"); 176 (void) fprintf(stderr, " -x <dumpdir> -- " 177 "dump all read blocks into specified directory\n"); 178 (void) fprintf(stderr, " -P print numbers in parseable form\n"); 179 (void) fprintf(stderr, " -t <txg> -- highest txg to use when " 180 "searching for uberblocks\n"); 181 (void) fprintf(stderr, " -I <number of inflight I/Os> -- " 182 "specify the maximum number of " 183 "checksumming I/Os [default is 200]\n"); 184 (void) fprintf(stderr, " -G dump zfs_dbgmsg buffer before " 185 "exiting\n"); 186 (void) fprintf(stderr, "Specify an option more than once (e.g. -bb) " 187 "to make only that option verbose\n"); 188 (void) fprintf(stderr, "Default is to dump everything non-verbosely\n"); 189 exit(1); 190} 191 192static void 193dump_debug_buffer() 194{ 195 if (dump_opt['G']) { 196 (void) printf("\n"); 197 zfs_dbgmsg_print("zdb"); 198 } 199} 200 201/* 202 * Called for usage errors that are discovered after a call to spa_open(), 203 * dmu_bonus_hold(), or pool_match(). abort() is called for other errors. 204 */ 205 206static void 207fatal(const char *fmt, ...) 208{ 209 va_list ap; 210 211 va_start(ap, fmt); 212 (void) fprintf(stderr, "%s: ", cmdname); 213 (void) vfprintf(stderr, fmt, ap); 214 va_end(ap); 215 (void) fprintf(stderr, "\n"); 216 217 dump_debug_buffer(); 218 219 exit(1); 220} 221 222/* ARGSUSED */ 223static void 224dump_packed_nvlist(objset_t *os, uint64_t object, void *data, size_t size) 225{ 226 nvlist_t *nv; 227 size_t nvsize = *(uint64_t *)data; 228 char *packed = umem_alloc(nvsize, UMEM_NOFAIL); 229 230 VERIFY(0 == dmu_read(os, object, 0, nvsize, packed, DMU_READ_PREFETCH)); 231 232 VERIFY(nvlist_unpack(packed, nvsize, &nv, 0) == 0); 233 234 umem_free(packed, nvsize); 235 236 dump_nvlist(nv, 8); 237 238 nvlist_free(nv); 239} 240 241/* ARGSUSED */ 242static void 243dump_history_offsets(objset_t *os, uint64_t object, void *data, size_t size) 244{ 245 spa_history_phys_t *shp = data; 246 247 if (shp == NULL) 248 return; 249 250 (void) printf("\t\tpool_create_len = %llu\n", 251 (u_longlong_t)shp->sh_pool_create_len); 252 (void) printf("\t\tphys_max_off = %llu\n", 253 (u_longlong_t)shp->sh_phys_max_off); 254 (void) printf("\t\tbof = %llu\n", 255 (u_longlong_t)shp->sh_bof); 256 (void) printf("\t\teof = %llu\n", 257 (u_longlong_t)shp->sh_eof); 258 (void) printf("\t\trecords_lost = %llu\n", 259 (u_longlong_t)shp->sh_records_lost); 260} 261 262static void 263zdb_nicenum(uint64_t num, char *buf) 264{ 265 if (dump_opt['P']) 266 (void) sprintf(buf, "%llu", (longlong_t)num); 267 else 268 nicenum(num, buf); 269} 270 271const char histo_stars[] = "****************************************"; 272const int histo_width = sizeof (histo_stars) - 1; 273 274static void 275dump_histogram(const uint64_t *histo, int size, int offset) 276{ 277 int i; 278 int minidx = size - 1; 279 int maxidx = 0; 280 uint64_t max = 0; 281 282 for (i = 0; i < size; i++) { 283 if (histo[i] > max) 284 max = histo[i]; 285 if (histo[i] > 0 && i > maxidx) 286 maxidx = i; 287 if (histo[i] > 0 && i < minidx) 288 minidx = i; 289 } 290 291 if (max < histo_width) 292 max = histo_width; 293 294 for (i = minidx; i <= maxidx; i++) { 295 (void) printf("\t\t\t%3u: %6llu %s\n", 296 i + offset, (u_longlong_t)histo[i], 297 &histo_stars[(max - histo[i]) * histo_width / max]); 298 } 299} 300 301static void 302dump_zap_stats(objset_t *os, uint64_t object) 303{ 304 int error; 305 zap_stats_t zs; 306 307 error = zap_get_stats(os, object, &zs); 308 if (error) 309 return; 310 311 if (zs.zs_ptrtbl_len == 0) { 312 ASSERT(zs.zs_num_blocks == 1); 313 (void) printf("\tmicrozap: %llu bytes, %llu entries\n", 314 (u_longlong_t)zs.zs_blocksize, 315 (u_longlong_t)zs.zs_num_entries); 316 return; 317 } 318 319 (void) printf("\tFat ZAP stats:\n"); 320 321 (void) printf("\t\tPointer table:\n"); 322 (void) printf("\t\t\t%llu elements\n", 323 (u_longlong_t)zs.zs_ptrtbl_len); 324 (void) printf("\t\t\tzt_blk: %llu\n", 325 (u_longlong_t)zs.zs_ptrtbl_zt_blk); 326 (void) printf("\t\t\tzt_numblks: %llu\n", 327 (u_longlong_t)zs.zs_ptrtbl_zt_numblks); 328 (void) printf("\t\t\tzt_shift: %llu\n", 329 (u_longlong_t)zs.zs_ptrtbl_zt_shift); 330 (void) printf("\t\t\tzt_blks_copied: %llu\n", 331 (u_longlong_t)zs.zs_ptrtbl_blks_copied); 332 (void) printf("\t\t\tzt_nextblk: %llu\n", 333 (u_longlong_t)zs.zs_ptrtbl_nextblk); 334 335 (void) printf("\t\tZAP entries: %llu\n", 336 (u_longlong_t)zs.zs_num_entries); 337 (void) printf("\t\tLeaf blocks: %llu\n", 338 (u_longlong_t)zs.zs_num_leafs); 339 (void) printf("\t\tTotal blocks: %llu\n", 340 (u_longlong_t)zs.zs_num_blocks); 341 (void) printf("\t\tzap_block_type: 0x%llx\n", 342 (u_longlong_t)zs.zs_block_type); 343 (void) printf("\t\tzap_magic: 0x%llx\n", 344 (u_longlong_t)zs.zs_magic); 345 (void) printf("\t\tzap_salt: 0x%llx\n", 346 (u_longlong_t)zs.zs_salt); 347 348 (void) printf("\t\tLeafs with 2^n pointers:\n"); 349 dump_histogram(zs.zs_leafs_with_2n_pointers, ZAP_HISTOGRAM_SIZE, 0); 350 351 (void) printf("\t\tBlocks with n*5 entries:\n"); 352 dump_histogram(zs.zs_blocks_with_n5_entries, ZAP_HISTOGRAM_SIZE, 0); 353 354 (void) printf("\t\tBlocks n/10 full:\n"); 355 dump_histogram(zs.zs_blocks_n_tenths_full, ZAP_HISTOGRAM_SIZE, 0); 356 357 (void) printf("\t\tEntries with n chunks:\n"); 358 dump_histogram(zs.zs_entries_using_n_chunks, ZAP_HISTOGRAM_SIZE, 0); 359 360 (void) printf("\t\tBuckets with n entries:\n"); 361 dump_histogram(zs.zs_buckets_with_n_entries, ZAP_HISTOGRAM_SIZE, 0); 362} 363 364/*ARGSUSED*/ 365static void 366dump_none(objset_t *os, uint64_t object, void *data, size_t size) 367{ 368} 369 370/*ARGSUSED*/ 371static void 372dump_unknown(objset_t *os, uint64_t object, void *data, size_t size) 373{ 374 (void) printf("\tUNKNOWN OBJECT TYPE\n"); 375} 376 377/*ARGSUSED*/ 378void 379dump_uint8(objset_t *os, uint64_t object, void *data, size_t size) 380{ 381} 382 383/*ARGSUSED*/ 384static void 385dump_uint64(objset_t *os, uint64_t object, void *data, size_t size) 386{ 387} 388 389/*ARGSUSED*/ 390static void 391dump_zap(objset_t *os, uint64_t object, void *data, size_t size) 392{ 393 zap_cursor_t zc; 394 zap_attribute_t attr; 395 void *prop; 396 int i; 397 398 dump_zap_stats(os, object); 399 (void) printf("\n"); 400 401 for (zap_cursor_init(&zc, os, object); 402 zap_cursor_retrieve(&zc, &attr) == 0; 403 zap_cursor_advance(&zc)) { 404 (void) printf("\t\t%s = ", attr.za_name); 405 if (attr.za_num_integers == 0) { 406 (void) printf("\n"); 407 continue; 408 } 409 prop = umem_zalloc(attr.za_num_integers * 410 attr.za_integer_length, UMEM_NOFAIL); 411 (void) zap_lookup(os, object, attr.za_name, 412 attr.za_integer_length, attr.za_num_integers, prop); 413 if (attr.za_integer_length == 1) { 414 (void) printf("%s", (char *)prop); 415 } else { 416 for (i = 0; i < attr.za_num_integers; i++) { 417 switch (attr.za_integer_length) { 418 case 2: 419 (void) printf("%u ", 420 ((uint16_t *)prop)[i]); 421 break; 422 case 4: 423 (void) printf("%u ", 424 ((uint32_t *)prop)[i]); 425 break; 426 case 8: 427 (void) printf("%lld ", 428 (u_longlong_t)((int64_t *)prop)[i]); 429 break; 430 } 431 } 432 } 433 (void) printf("\n"); 434 umem_free(prop, attr.za_num_integers * attr.za_integer_length); 435 } 436 zap_cursor_fini(&zc); 437} 438 439static void 440dump_bpobj(objset_t *os, uint64_t object, void *data, size_t size) 441{ 442 bpobj_phys_t *bpop = data; 443 char bytes[32], comp[32], uncomp[32]; 444 445 if (bpop == NULL) 446 return; 447 448 zdb_nicenum(bpop->bpo_bytes, bytes); 449 zdb_nicenum(bpop->bpo_comp, comp); 450 zdb_nicenum(bpop->bpo_uncomp, uncomp); 451 452 (void) printf("\t\tnum_blkptrs = %llu\n", 453 (u_longlong_t)bpop->bpo_num_blkptrs); 454 (void) printf("\t\tbytes = %s\n", bytes); 455 if (size >= BPOBJ_SIZE_V1) { 456 (void) printf("\t\tcomp = %s\n", comp); 457 (void) printf("\t\tuncomp = %s\n", uncomp); 458 } 459 if (size >= sizeof (*bpop)) { 460 (void) printf("\t\tsubobjs = %llu\n", 461 (u_longlong_t)bpop->bpo_subobjs); 462 (void) printf("\t\tnum_subobjs = %llu\n", 463 (u_longlong_t)bpop->bpo_num_subobjs); 464 } 465 466 if (dump_opt['d'] < 5) 467 return; 468 469 for (uint64_t i = 0; i < bpop->bpo_num_blkptrs; i++) { 470 char blkbuf[BP_SPRINTF_LEN]; 471 blkptr_t bp; 472 473 int err = dmu_read(os, object, 474 i * sizeof (bp), sizeof (bp), &bp, 0); 475 if (err != 0) { 476 (void) printf("got error %u from dmu_read\n", err); 477 break; 478 } 479 snprintf_blkptr_compact(blkbuf, sizeof (blkbuf), &bp); 480 (void) printf("\t%s\n", blkbuf); 481 } 482} 483 484/* ARGSUSED */ 485static void 486dump_bpobj_subobjs(objset_t *os, uint64_t object, void *data, size_t size) 487{ 488 dmu_object_info_t doi; 489 490 VERIFY0(dmu_object_info(os, object, &doi)); 491 uint64_t *subobjs = kmem_alloc(doi.doi_max_offset, KM_SLEEP); 492 493 int err = dmu_read(os, object, 0, doi.doi_max_offset, subobjs, 0); 494 if (err != 0) { 495 (void) printf("got error %u from dmu_read\n", err); 496 kmem_free(subobjs, doi.doi_max_offset); 497 return; 498 } 499 500 int64_t last_nonzero = -1; 501 for (uint64_t i = 0; i < doi.doi_max_offset / 8; i++) { 502 if (subobjs[i] != 0) 503 last_nonzero = i; 504 } 505 506 for (int64_t i = 0; i <= last_nonzero; i++) { 507 (void) printf("\t%llu\n", (longlong_t)subobjs[i]); 508 } 509 kmem_free(subobjs, doi.doi_max_offset); 510} 511 512/*ARGSUSED*/ 513static void 514dump_ddt_zap(objset_t *os, uint64_t object, void *data, size_t size) 515{ 516 dump_zap_stats(os, object); 517 /* contents are printed elsewhere, properly decoded */ 518} 519 520/*ARGSUSED*/ 521static void 522dump_sa_attrs(objset_t *os, uint64_t object, void *data, size_t size) 523{ 524 zap_cursor_t zc; 525 zap_attribute_t attr; 526 527 dump_zap_stats(os, object); 528 (void) printf("\n"); 529 530 for (zap_cursor_init(&zc, os, object); 531 zap_cursor_retrieve(&zc, &attr) == 0; 532 zap_cursor_advance(&zc)) { 533 (void) printf("\t\t%s = ", attr.za_name); 534 if (attr.za_num_integers == 0) { 535 (void) printf("\n"); 536 continue; 537 } 538 (void) printf(" %llx : [%d:%d:%d]\n", 539 (u_longlong_t)attr.za_first_integer, 540 (int)ATTR_LENGTH(attr.za_first_integer), 541 (int)ATTR_BSWAP(attr.za_first_integer), 542 (int)ATTR_NUM(attr.za_first_integer)); 543 } 544 zap_cursor_fini(&zc); 545} 546 547/*ARGSUSED*/ 548static void 549dump_sa_layouts(objset_t *os, uint64_t object, void *data, size_t size) 550{ 551 zap_cursor_t zc; 552 zap_attribute_t attr; 553 uint16_t *layout_attrs; 554 int i; 555 556 dump_zap_stats(os, object); 557 (void) printf("\n"); 558 559 for (zap_cursor_init(&zc, os, object); 560 zap_cursor_retrieve(&zc, &attr) == 0; 561 zap_cursor_advance(&zc)) { 562 (void) printf("\t\t%s = [", attr.za_name); 563 if (attr.za_num_integers == 0) { 564 (void) printf("\n"); 565 continue; 566 } 567 568 VERIFY(attr.za_integer_length == 2); 569 layout_attrs = umem_zalloc(attr.za_num_integers * 570 attr.za_integer_length, UMEM_NOFAIL); 571 572 VERIFY(zap_lookup(os, object, attr.za_name, 573 attr.za_integer_length, 574 attr.za_num_integers, layout_attrs) == 0); 575 576 for (i = 0; i != attr.za_num_integers; i++) 577 (void) printf(" %d ", (int)layout_attrs[i]); 578 (void) printf("]\n"); 579 umem_free(layout_attrs, 580 attr.za_num_integers * attr.za_integer_length); 581 } 582 zap_cursor_fini(&zc); 583} 584 585/*ARGSUSED*/ 586static void 587dump_zpldir(objset_t *os, uint64_t object, void *data, size_t size) 588{ 589 zap_cursor_t zc; 590 zap_attribute_t attr; 591 const char *typenames[] = { 592 /* 0 */ "not specified", 593 /* 1 */ "FIFO", 594 /* 2 */ "Character Device", 595 /* 3 */ "3 (invalid)", 596 /* 4 */ "Directory", 597 /* 5 */ "5 (invalid)", 598 /* 6 */ "Block Device", 599 /* 7 */ "7 (invalid)", 600 /* 8 */ "Regular File", 601 /* 9 */ "9 (invalid)", 602 /* 10 */ "Symbolic Link", 603 /* 11 */ "11 (invalid)", 604 /* 12 */ "Socket", 605 /* 13 */ "Door", 606 /* 14 */ "Event Port", 607 /* 15 */ "15 (invalid)", 608 }; 609 610 dump_zap_stats(os, object); 611 (void) printf("\n"); 612 613 for (zap_cursor_init(&zc, os, object); 614 zap_cursor_retrieve(&zc, &attr) == 0; 615 zap_cursor_advance(&zc)) { 616 (void) printf("\t\t%s = %lld (type: %s)\n", 617 attr.za_name, ZFS_DIRENT_OBJ(attr.za_first_integer), 618 typenames[ZFS_DIRENT_TYPE(attr.za_first_integer)]); 619 } 620 zap_cursor_fini(&zc); 621} 622 623int 624get_dtl_refcount(vdev_t *vd) 625{ 626 int refcount = 0; 627 628 if (vd->vdev_ops->vdev_op_leaf) { 629 space_map_t *sm = vd->vdev_dtl_sm; 630 631 if (sm != NULL && 632 sm->sm_dbuf->db_size == sizeof (space_map_phys_t)) 633 return (1); 634 return (0); 635 } 636 637 for (int c = 0; c < vd->vdev_children; c++) 638 refcount += get_dtl_refcount(vd->vdev_child[c]); 639 return (refcount); 640} 641 642int 643get_metaslab_refcount(vdev_t *vd) 644{ 645 int refcount = 0; 646 647 if (vd->vdev_top == vd && !vd->vdev_removing) { 648 for (int m = 0; m < vd->vdev_ms_count; m++) { 649 space_map_t *sm = vd->vdev_ms[m]->ms_sm; 650 651 if (sm != NULL && 652 sm->sm_dbuf->db_size == sizeof (space_map_phys_t)) 653 refcount++; 654 } 655 } 656 for (int c = 0; c < vd->vdev_children; c++) 657 refcount += get_metaslab_refcount(vd->vdev_child[c]); 658 659 return (refcount); 660} 661 662static int 663verify_spacemap_refcounts(spa_t *spa) 664{ 665 uint64_t expected_refcount = 0; 666 uint64_t actual_refcount; 667 668 (void) feature_get_refcount(spa, 669 &spa_feature_table[SPA_FEATURE_SPACEMAP_HISTOGRAM], 670 &expected_refcount); 671 actual_refcount = get_dtl_refcount(spa->spa_root_vdev); 672 actual_refcount += get_metaslab_refcount(spa->spa_root_vdev); 673 674 if (expected_refcount != actual_refcount) { 675 (void) printf("space map refcount mismatch: expected %lld != " 676 "actual %lld\n", 677 (longlong_t)expected_refcount, 678 (longlong_t)actual_refcount); 679 return (2); 680 } 681 return (0); 682} 683 684static void 685dump_spacemap(objset_t *os, space_map_t *sm) 686{ 687 uint64_t alloc, offset, entry; 688 char *ddata[] = { "ALLOC", "FREE", "CONDENSE", "INVALID", 689 "INVALID", "INVALID", "INVALID", "INVALID" }; 690 691 if (sm == NULL) 692 return; 693 694 /* 695 * Print out the freelist entries in both encoded and decoded form. 696 */ 697 alloc = 0; 698 for (offset = 0; offset < space_map_length(sm); 699 offset += sizeof (entry)) { 700 uint8_t mapshift = sm->sm_shift; 701 702 VERIFY0(dmu_read(os, space_map_object(sm), offset, 703 sizeof (entry), &entry, DMU_READ_PREFETCH)); 704 if (SM_DEBUG_DECODE(entry)) { 705 706 (void) printf("\t [%6llu] %s: txg %llu, pass %llu\n", 707 (u_longlong_t)(offset / sizeof (entry)), 708 ddata[SM_DEBUG_ACTION_DECODE(entry)], 709 (u_longlong_t)SM_DEBUG_TXG_DECODE(entry), 710 (u_longlong_t)SM_DEBUG_SYNCPASS_DECODE(entry)); 711 } else { 712 (void) printf("\t [%6llu] %c range:" 713 " %010llx-%010llx size: %06llx\n", 714 (u_longlong_t)(offset / sizeof (entry)), 715 SM_TYPE_DECODE(entry) == SM_ALLOC ? 'A' : 'F', 716 (u_longlong_t)((SM_OFFSET_DECODE(entry) << 717 mapshift) + sm->sm_start), 718 (u_longlong_t)((SM_OFFSET_DECODE(entry) << 719 mapshift) + sm->sm_start + 720 (SM_RUN_DECODE(entry) << mapshift)), 721 (u_longlong_t)(SM_RUN_DECODE(entry) << mapshift)); 722 if (SM_TYPE_DECODE(entry) == SM_ALLOC) 723 alloc += SM_RUN_DECODE(entry) << mapshift; 724 else 725 alloc -= SM_RUN_DECODE(entry) << mapshift; 726 } 727 } 728 if (alloc != space_map_allocated(sm)) { 729 (void) printf("space_map_object alloc (%llu) INCONSISTENT " 730 "with space map summary (%llu)\n", 731 (u_longlong_t)space_map_allocated(sm), (u_longlong_t)alloc); 732 } 733} 734 735static void 736dump_metaslab_stats(metaslab_t *msp) 737{ 738 char maxbuf[32]; 739 range_tree_t *rt = msp->ms_tree; 740 avl_tree_t *t = &msp->ms_size_tree; 741 int free_pct = range_tree_space(rt) * 100 / msp->ms_size; 742 743 zdb_nicenum(metaslab_block_maxsize(msp), maxbuf); 744 745 (void) printf("\t %25s %10lu %7s %6s %4s %4d%%\n", 746 "segments", avl_numnodes(t), "maxsize", maxbuf, 747 "freepct", free_pct); 748 (void) printf("\tIn-memory histogram:\n"); 749 dump_histogram(rt->rt_histogram, RANGE_TREE_HISTOGRAM_SIZE, 0); 750} 751 752static void 753dump_metaslab(metaslab_t *msp) 754{ 755 vdev_t *vd = msp->ms_group->mg_vd; 756 spa_t *spa = vd->vdev_spa; 757 space_map_t *sm = msp->ms_sm; 758 char freebuf[32]; 759 760 zdb_nicenum(msp->ms_size - space_map_allocated(sm), freebuf); 761 762 (void) printf( 763 "\tmetaslab %6llu offset %12llx spacemap %6llu free %5s\n", 764 (u_longlong_t)msp->ms_id, (u_longlong_t)msp->ms_start, 765 (u_longlong_t)space_map_object(sm), freebuf); 766 767 if (dump_opt['m'] > 2 && !dump_opt['L']) { 768 mutex_enter(&msp->ms_lock); 769 metaslab_load_wait(msp); 770 if (!msp->ms_loaded) { 771 VERIFY0(metaslab_load(msp)); 772 range_tree_stat_verify(msp->ms_tree); 773 } 774 dump_metaslab_stats(msp); 775 metaslab_unload(msp); 776 mutex_exit(&msp->ms_lock); 777 } 778 779 if (dump_opt['m'] > 1 && sm != NULL && 780 spa_feature_is_active(spa, SPA_FEATURE_SPACEMAP_HISTOGRAM)) { 781 /* 782 * The space map histogram represents free space in chunks 783 * of sm_shift (i.e. bucket 0 refers to 2^sm_shift). 784 */ 785 (void) printf("\tOn-disk histogram:\t\tfragmentation %llu\n", 786 (u_longlong_t)msp->ms_fragmentation); 787 dump_histogram(sm->sm_phys->smp_histogram, 788 SPACE_MAP_HISTOGRAM_SIZE, sm->sm_shift); 789 } 790 791 if (dump_opt['d'] > 5 || dump_opt['m'] > 3) { 792 ASSERT(msp->ms_size == (1ULL << vd->vdev_ms_shift)); 793 794 mutex_enter(&msp->ms_lock); 795 dump_spacemap(spa->spa_meta_objset, msp->ms_sm); 796 mutex_exit(&msp->ms_lock); 797 } 798} 799 800static void 801print_vdev_metaslab_header(vdev_t *vd) 802{ 803 (void) printf("\tvdev %10llu\n\t%-10s%5llu %-19s %-15s %-10s\n", 804 (u_longlong_t)vd->vdev_id, 805 "metaslabs", (u_longlong_t)vd->vdev_ms_count, 806 "offset", "spacemap", "free"); 807 (void) printf("\t%15s %19s %15s %10s\n", 808 "---------------", "-------------------", 809 "---------------", "-------------"); 810} 811 812static void 813dump_metaslab_groups(spa_t *spa) 814{ 815 vdev_t *rvd = spa->spa_root_vdev; 816 metaslab_class_t *mc = spa_normal_class(spa); 817 uint64_t fragmentation; 818 819 metaslab_class_histogram_verify(mc); 820 821 for (int c = 0; c < rvd->vdev_children; c++) { 822 vdev_t *tvd = rvd->vdev_child[c]; 823 metaslab_group_t *mg = tvd->vdev_mg; 824 825 if (mg->mg_class != mc) 826 continue; 827 828 metaslab_group_histogram_verify(mg); 829 mg->mg_fragmentation = metaslab_group_fragmentation(mg); 830 831 (void) printf("\tvdev %10llu\t\tmetaslabs%5llu\t\t" 832 "fragmentation", 833 (u_longlong_t)tvd->vdev_id, 834 (u_longlong_t)tvd->vdev_ms_count); 835 if (mg->mg_fragmentation == ZFS_FRAG_INVALID) { 836 (void) printf("%3s\n", "-"); 837 } else { 838 (void) printf("%3llu%%\n", 839 (u_longlong_t)mg->mg_fragmentation); 840 } 841 dump_histogram(mg->mg_histogram, RANGE_TREE_HISTOGRAM_SIZE, 0); 842 } 843 844 (void) printf("\tpool %s\tfragmentation", spa_name(spa)); 845 fragmentation = metaslab_class_fragmentation(mc); 846 if (fragmentation == ZFS_FRAG_INVALID) 847 (void) printf("\t%3s\n", "-"); 848 else 849 (void) printf("\t%3llu%%\n", (u_longlong_t)fragmentation); 850 dump_histogram(mc->mc_histogram, RANGE_TREE_HISTOGRAM_SIZE, 0); 851} 852 853static void 854dump_metaslabs(spa_t *spa) 855{ 856 vdev_t *vd, *rvd = spa->spa_root_vdev; 857 uint64_t m, c = 0, children = rvd->vdev_children; 858 859 (void) printf("\nMetaslabs:\n"); 860 861 if (!dump_opt['d'] && zopt_objects > 0) { 862 c = zopt_object[0]; 863 864 if (c >= children) 865 (void) fatal("bad vdev id: %llu", (u_longlong_t)c); 866 867 if (zopt_objects > 1) { 868 vd = rvd->vdev_child[c]; 869 print_vdev_metaslab_header(vd); 870 871 for (m = 1; m < zopt_objects; m++) { 872 if (zopt_object[m] < vd->vdev_ms_count) 873 dump_metaslab( 874 vd->vdev_ms[zopt_object[m]]); 875 else 876 (void) fprintf(stderr, "bad metaslab " 877 "number %llu\n", 878 (u_longlong_t)zopt_object[m]); 879 } 880 (void) printf("\n"); 881 return; 882 } 883 children = c + 1; 884 } 885 for (; c < children; c++) { 886 vd = rvd->vdev_child[c]; 887 print_vdev_metaslab_header(vd); 888 889 for (m = 0; m < vd->vdev_ms_count; m++) 890 dump_metaslab(vd->vdev_ms[m]); 891 (void) printf("\n"); 892 } 893} 894 895static void 896dump_dde(const ddt_t *ddt, const ddt_entry_t *dde, uint64_t index) 897{ 898 const ddt_phys_t *ddp = dde->dde_phys; 899 const ddt_key_t *ddk = &dde->dde_key; 900 char *types[4] = { "ditto", "single", "double", "triple" }; 901 char blkbuf[BP_SPRINTF_LEN]; 902 blkptr_t blk; 903 904 for (int p = 0; p < DDT_PHYS_TYPES; p++, ddp++) { 905 if (ddp->ddp_phys_birth == 0) 906 continue; 907 ddt_bp_create(ddt->ddt_checksum, ddk, ddp, &blk); 908 snprintf_blkptr(blkbuf, sizeof (blkbuf), &blk); 909 (void) printf("index %llx refcnt %llu %s %s\n", 910 (u_longlong_t)index, (u_longlong_t)ddp->ddp_refcnt, 911 types[p], blkbuf); 912 } 913} 914 915static void 916dump_dedup_ratio(const ddt_stat_t *dds) 917{ 918 double rL, rP, rD, D, dedup, compress, copies; 919 920 if (dds->dds_blocks == 0) 921 return; 922 923 rL = (double)dds->dds_ref_lsize; 924 rP = (double)dds->dds_ref_psize; 925 rD = (double)dds->dds_ref_dsize; 926 D = (double)dds->dds_dsize; 927 928 dedup = rD / D; 929 compress = rL / rP; 930 copies = rD / rP; 931 932 (void) printf("dedup = %.2f, compress = %.2f, copies = %.2f, " 933 "dedup * compress / copies = %.2f\n\n", 934 dedup, compress, copies, dedup * compress / copies); 935} 936 937static void 938dump_ddt(ddt_t *ddt, enum ddt_type type, enum ddt_class class) 939{ 940 char name[DDT_NAMELEN]; 941 ddt_entry_t dde; 942 uint64_t walk = 0; 943 dmu_object_info_t doi; 944 uint64_t count, dspace, mspace; 945 int error; 946 947 error = ddt_object_info(ddt, type, class, &doi); 948 949 if (error == ENOENT) 950 return; 951 ASSERT(error == 0); 952 953 error = ddt_object_count(ddt, type, class, &count); 954 ASSERT(error == 0); 955 if (count == 0) 956 return; 957 958 dspace = doi.doi_physical_blocks_512 << 9; 959 mspace = doi.doi_fill_count * doi.doi_data_block_size; 960 961 ddt_object_name(ddt, type, class, name); 962 963 (void) printf("%s: %llu entries, size %llu on disk, %llu in core\n", 964 name, 965 (u_longlong_t)count, 966 (u_longlong_t)(dspace / count), 967 (u_longlong_t)(mspace / count)); 968 969 if (dump_opt['D'] < 3) 970 return; 971 972 zpool_dump_ddt(NULL, &ddt->ddt_histogram[type][class]); 973 974 if (dump_opt['D'] < 4) 975 return; 976 977 if (dump_opt['D'] < 5 && class == DDT_CLASS_UNIQUE) 978 return; 979 980 (void) printf("%s contents:\n\n", name); 981 982 while ((error = ddt_object_walk(ddt, type, class, &walk, &dde)) == 0) 983 dump_dde(ddt, &dde, walk); 984 985 ASSERT(error == ENOENT); 986 987 (void) printf("\n"); 988} 989 990static void 991dump_all_ddts(spa_t *spa) 992{ 993 ddt_histogram_t ddh_total = { 0 }; 994 ddt_stat_t dds_total = { 0 }; 995 996 for (enum zio_checksum c = 0; c < ZIO_CHECKSUM_FUNCTIONS; c++) { 997 ddt_t *ddt = spa->spa_ddt[c]; 998 for (enum ddt_type type = 0; type < DDT_TYPES; type++) { 999 for (enum ddt_class class = 0; class < DDT_CLASSES; 1000 class++) { 1001 dump_ddt(ddt, type, class); 1002 } 1003 } 1004 } 1005 1006 ddt_get_dedup_stats(spa, &dds_total); 1007 1008 if (dds_total.dds_blocks == 0) { 1009 (void) printf("All DDTs are empty\n"); 1010 return; 1011 } 1012 1013 (void) printf("\n"); 1014 1015 if (dump_opt['D'] > 1) { 1016 (void) printf("DDT histogram (aggregated over all DDTs):\n"); 1017 ddt_get_dedup_histogram(spa, &ddh_total); 1018 zpool_dump_ddt(&dds_total, &ddh_total); 1019 } 1020 1021 dump_dedup_ratio(&dds_total); 1022} 1023 1024static void 1025dump_dtl_seg(void *arg, uint64_t start, uint64_t size) 1026{ 1027 char *prefix = arg; 1028 1029 (void) printf("%s [%llu,%llu) length %llu\n", 1030 prefix, 1031 (u_longlong_t)start, 1032 (u_longlong_t)(start + size), 1033 (u_longlong_t)(size)); 1034} 1035 1036static void 1037dump_dtl(vdev_t *vd, int indent) 1038{ 1039 spa_t *spa = vd->vdev_spa; 1040 boolean_t required; 1041 char *name[DTL_TYPES] = { "missing", "partial", "scrub", "outage" }; 1042 char prefix[256]; 1043 1044 spa_vdev_state_enter(spa, SCL_NONE); 1045 required = vdev_dtl_required(vd); 1046 (void) spa_vdev_state_exit(spa, NULL, 0); 1047 1048 if (indent == 0) 1049 (void) printf("\nDirty time logs:\n\n"); 1050 1051 (void) printf("\t%*s%s [%s]\n", indent, "", 1052 vd->vdev_path ? vd->vdev_path : 1053 vd->vdev_parent ? vd->vdev_ops->vdev_op_type : spa_name(spa), 1054 required ? "DTL-required" : "DTL-expendable"); 1055 1056 for (int t = 0; t < DTL_TYPES; t++) { 1057 range_tree_t *rt = vd->vdev_dtl[t]; 1058 if (range_tree_space(rt) == 0) 1059 continue; 1060 (void) snprintf(prefix, sizeof (prefix), "\t%*s%s", 1061 indent + 2, "", name[t]); 1062 mutex_enter(rt->rt_lock); 1063 range_tree_walk(rt, dump_dtl_seg, prefix); 1064 mutex_exit(rt->rt_lock); 1065 if (dump_opt['d'] > 5 && vd->vdev_children == 0) 1066 dump_spacemap(spa->spa_meta_objset, vd->vdev_dtl_sm); 1067 } 1068 1069 for (int c = 0; c < vd->vdev_children; c++) 1070 dump_dtl(vd->vdev_child[c], indent + 4); 1071} 1072 1073/* from spa_history.c: spa_history_create_obj() */ 1074#define HIS_BUF_LEN_DEF (128 << 10) 1075#define HIS_BUF_LEN_MAX (1 << 30) 1076 1077static void 1078dump_history(spa_t *spa) 1079{ 1080 nvlist_t **events = NULL; 1081 char *buf = NULL; 1082 uint64_t bufsize = HIS_BUF_LEN_DEF; 1083 uint64_t resid, len, off = 0; 1084 uint_t num = 0; 1085 int error; 1086 time_t tsec; 1087 struct tm t; 1088 char tbuf[30]; 1089 char internalstr[MAXPATHLEN]; 1090 1091 if ((buf = malloc(bufsize)) == NULL) 1092 (void) fprintf(stderr, "Unable to read history: " 1093 "out of memory\n"); 1094 do { 1095 len = bufsize; 1096 1097 if ((error = spa_history_get(spa, &off, &len, buf)) != 0) { 1098 (void) fprintf(stderr, "Unable to read history: " 1099 "error %d\n", error); 1100 return; 1101 } 1102 1103 if (zpool_history_unpack(buf, len, &resid, &events, &num) != 0) 1104 break; 1105 off -= resid; 1106 1107 /* 1108 * If the history block is too big, double the buffer 1109 * size and try again. 1110 */ 1111 if (resid == len) { 1112 free(buf); 1113 buf = NULL; 1114 1115 bufsize <<= 1; 1116 if ((bufsize >= HIS_BUF_LEN_MAX) || 1117 ((buf = malloc(bufsize)) == NULL)) { 1118 (void) fprintf(stderr, "Unable to read history: " 1119 "out of memory\n"); 1120 return; 1121 } 1122 } 1123 } while (len != 0); 1124 free(buf); 1125 1126 (void) printf("\nHistory:\n"); 1127 for (int i = 0; i < num; i++) { 1128 uint64_t time, txg, ievent; 1129 char *cmd, *intstr; 1130 boolean_t printed = B_FALSE; 1131 1132 if (nvlist_lookup_uint64(events[i], ZPOOL_HIST_TIME, 1133 &time) != 0) 1134 goto next; 1135 if (nvlist_lookup_string(events[i], ZPOOL_HIST_CMD, 1136 &cmd) != 0) { 1137 if (nvlist_lookup_uint64(events[i], 1138 ZPOOL_HIST_INT_EVENT, &ievent) != 0) 1139 goto next; 1140 verify(nvlist_lookup_uint64(events[i], 1141 ZPOOL_HIST_TXG, &txg) == 0); 1142 verify(nvlist_lookup_string(events[i], 1143 ZPOOL_HIST_INT_STR, &intstr) == 0); 1144 if (ievent >= ZFS_NUM_LEGACY_HISTORY_EVENTS) 1145 goto next; 1146 1147 (void) snprintf(internalstr, 1148 sizeof (internalstr), 1149 "[internal %s txg:%lld] %s", 1150 zfs_history_event_names[ievent], txg, 1151 intstr); 1152 cmd = internalstr; 1153 } 1154 tsec = time; 1155 (void) localtime_r(&tsec, &t); 1156 (void) strftime(tbuf, sizeof (tbuf), "%F.%T", &t); 1157 (void) printf("%s %s\n", tbuf, cmd); 1158 printed = B_TRUE; 1159 1160next: 1161 if (dump_opt['h'] > 1) { 1162 if (!printed) 1163 (void) printf("unrecognized record:\n"); 1164 dump_nvlist(events[i], 2); 1165 } 1166 } 1167} 1168 1169/*ARGSUSED*/ 1170static void 1171dump_dnode(objset_t *os, uint64_t object, void *data, size_t size) 1172{ 1173} 1174 1175static uint64_t 1176blkid2offset(const dnode_phys_t *dnp, const blkptr_t *bp, 1177 const zbookmark_phys_t *zb) 1178{ 1179 if (dnp == NULL) { 1180 ASSERT(zb->zb_level < 0); 1181 if (zb->zb_object == 0) 1182 return (zb->zb_blkid); 1183 return (zb->zb_blkid * BP_GET_LSIZE(bp)); 1184 } 1185 1186 ASSERT(zb->zb_level >= 0); 1187 1188 return ((zb->zb_blkid << 1189 (zb->zb_level * (dnp->dn_indblkshift - SPA_BLKPTRSHIFT))) * 1190 dnp->dn_datablkszsec << SPA_MINBLOCKSHIFT); 1191} 1192 1193static void 1194snprintf_blkptr_compact(char *blkbuf, size_t buflen, const blkptr_t *bp) 1195{ 1196 const dva_t *dva = bp->blk_dva; 1197 int ndvas = dump_opt['d'] > 5 ? BP_GET_NDVAS(bp) : 1; 1198 1199 if (dump_opt['b'] >= 6) { 1200 snprintf_blkptr(blkbuf, buflen, bp); 1201 return; 1202 } 1203 1204 if (BP_IS_EMBEDDED(bp)) { 1205 (void) sprintf(blkbuf, 1206 "EMBEDDED et=%u %llxL/%llxP B=%llu", 1207 (int)BPE_GET_ETYPE(bp), 1208 (u_longlong_t)BPE_GET_LSIZE(bp), 1209 (u_longlong_t)BPE_GET_PSIZE(bp), 1210 (u_longlong_t)bp->blk_birth); 1211 return; 1212 } 1213 1214 blkbuf[0] = '\0'; 1215 for (int i = 0; i < ndvas; i++) 1216 (void) snprintf(blkbuf + strlen(blkbuf), 1217 buflen - strlen(blkbuf), "%llu:%llx:%llx ", 1218 (u_longlong_t)DVA_GET_VDEV(&dva[i]), 1219 (u_longlong_t)DVA_GET_OFFSET(&dva[i]), 1220 (u_longlong_t)DVA_GET_ASIZE(&dva[i])); 1221 1222 if (BP_IS_HOLE(bp)) { 1223 (void) snprintf(blkbuf + strlen(blkbuf), 1224 buflen - strlen(blkbuf), 1225 "%llxL B=%llu", 1226 (u_longlong_t)BP_GET_LSIZE(bp), 1227 (u_longlong_t)bp->blk_birth); 1228 } else { 1229 (void) snprintf(blkbuf + strlen(blkbuf), 1230 buflen - strlen(blkbuf), 1231 "%llxL/%llxP F=%llu B=%llu/%llu", 1232 (u_longlong_t)BP_GET_LSIZE(bp), 1233 (u_longlong_t)BP_GET_PSIZE(bp), 1234 (u_longlong_t)BP_GET_FILL(bp), 1235 (u_longlong_t)bp->blk_birth, 1236 (u_longlong_t)BP_PHYSICAL_BIRTH(bp)); 1237 } 1238} 1239 1240static void 1241print_indirect(blkptr_t *bp, const zbookmark_phys_t *zb, 1242 const dnode_phys_t *dnp) 1243{ 1244 char blkbuf[BP_SPRINTF_LEN]; 1245 int l; 1246 1247 if (!BP_IS_EMBEDDED(bp)) { 1248 ASSERT3U(BP_GET_TYPE(bp), ==, dnp->dn_type); 1249 ASSERT3U(BP_GET_LEVEL(bp), ==, zb->zb_level); 1250 } 1251 1252 (void) printf("%16llx ", (u_longlong_t)blkid2offset(dnp, bp, zb)); 1253 1254 ASSERT(zb->zb_level >= 0); 1255 1256 for (l = dnp->dn_nlevels - 1; l >= -1; l--) { 1257 if (l == zb->zb_level) { 1258 (void) printf("L%llx", (u_longlong_t)zb->zb_level); 1259 } else { 1260 (void) printf(" "); 1261 } 1262 } 1263 1264 snprintf_blkptr_compact(blkbuf, sizeof (blkbuf), bp); 1265 (void) printf("%s\n", blkbuf); 1266} 1267 1268static int 1269visit_indirect(spa_t *spa, const dnode_phys_t *dnp, 1270 blkptr_t *bp, const zbookmark_phys_t *zb) 1271{ 1272 int err = 0; 1273 1274 if (bp->blk_birth == 0) 1275 return (0); 1276 1277 print_indirect(bp, zb, dnp); 1278 1279 if (BP_GET_LEVEL(bp) > 0 && !BP_IS_HOLE(bp)) { 1280 arc_flags_t flags = ARC_FLAG_WAIT; 1281 int i; 1282 blkptr_t *cbp; 1283 int epb = BP_GET_LSIZE(bp) >> SPA_BLKPTRSHIFT; 1284 arc_buf_t *buf; 1285 uint64_t fill = 0; 1286 1287 err = arc_read(NULL, spa, bp, arc_getbuf_func, &buf, 1288 ZIO_PRIORITY_ASYNC_READ, ZIO_FLAG_CANFAIL, &flags, zb); 1289 if (err) 1290 return (err); 1291 ASSERT(buf->b_data); 1292 1293 /* recursively visit blocks below this */ 1294 cbp = buf->b_data; 1295 for (i = 0; i < epb; i++, cbp++) { 1296 zbookmark_phys_t czb; 1297 1298 SET_BOOKMARK(&czb, zb->zb_objset, zb->zb_object, 1299 zb->zb_level - 1, 1300 zb->zb_blkid * epb + i); 1301 err = visit_indirect(spa, dnp, cbp, &czb); 1302 if (err) 1303 break; 1304 fill += BP_GET_FILL(cbp); 1305 } 1306 if (!err) 1307 ASSERT3U(fill, ==, BP_GET_FILL(bp)); 1308 arc_buf_destroy(buf, &buf); 1309 } 1310 1311 return (err); 1312} 1313 1314/*ARGSUSED*/ 1315static void 1316dump_indirect(dnode_t *dn) 1317{ 1318 dnode_phys_t *dnp = dn->dn_phys; 1319 int j; 1320 zbookmark_phys_t czb; 1321 1322 (void) printf("Indirect blocks:\n"); 1323 1324 SET_BOOKMARK(&czb, dmu_objset_id(dn->dn_objset), 1325 dn->dn_object, dnp->dn_nlevels - 1, 0); 1326 for (j = 0; j < dnp->dn_nblkptr; j++) { 1327 czb.zb_blkid = j; 1328 (void) visit_indirect(dmu_objset_spa(dn->dn_objset), dnp, 1329 &dnp->dn_blkptr[j], &czb); 1330 } 1331 1332 (void) printf("\n"); 1333} 1334 1335/*ARGSUSED*/ 1336static void 1337dump_dsl_dir(objset_t *os, uint64_t object, void *data, size_t size) 1338{ 1339 dsl_dir_phys_t *dd = data; 1340 time_t crtime; 1341 char nice[32]; 1342 1343 if (dd == NULL) 1344 return; 1345 1346 ASSERT3U(size, >=, sizeof (dsl_dir_phys_t)); 1347 1348 crtime = dd->dd_creation_time; 1349 (void) printf("\t\tcreation_time = %s", ctime(&crtime)); 1350 (void) printf("\t\thead_dataset_obj = %llu\n", 1351 (u_longlong_t)dd->dd_head_dataset_obj); 1352 (void) printf("\t\tparent_dir_obj = %llu\n", 1353 (u_longlong_t)dd->dd_parent_obj); 1354 (void) printf("\t\torigin_obj = %llu\n", 1355 (u_longlong_t)dd->dd_origin_obj); 1356 (void) printf("\t\tchild_dir_zapobj = %llu\n", 1357 (u_longlong_t)dd->dd_child_dir_zapobj); 1358 zdb_nicenum(dd->dd_used_bytes, nice); 1359 (void) printf("\t\tused_bytes = %s\n", nice); 1360 zdb_nicenum(dd->dd_compressed_bytes, nice); 1361 (void) printf("\t\tcompressed_bytes = %s\n", nice); 1362 zdb_nicenum(dd->dd_uncompressed_bytes, nice); 1363 (void) printf("\t\tuncompressed_bytes = %s\n", nice); 1364 zdb_nicenum(dd->dd_quota, nice); 1365 (void) printf("\t\tquota = %s\n", nice); 1366 zdb_nicenum(dd->dd_reserved, nice); 1367 (void) printf("\t\treserved = %s\n", nice); 1368 (void) printf("\t\tprops_zapobj = %llu\n", 1369 (u_longlong_t)dd->dd_props_zapobj); 1370 (void) printf("\t\tdeleg_zapobj = %llu\n", 1371 (u_longlong_t)dd->dd_deleg_zapobj); 1372 (void) printf("\t\tflags = %llx\n", 1373 (u_longlong_t)dd->dd_flags); 1374 1375#define DO(which) \ 1376 zdb_nicenum(dd->dd_used_breakdown[DD_USED_ ## which], nice); \ 1377 (void) printf("\t\tused_breakdown[" #which "] = %s\n", nice) 1378 DO(HEAD); 1379 DO(SNAP); 1380 DO(CHILD); 1381 DO(CHILD_RSRV); 1382 DO(REFRSRV); 1383#undef DO 1384} 1385 1386/*ARGSUSED*/ 1387static void 1388dump_dsl_dataset(objset_t *os, uint64_t object, void *data, size_t size) 1389{ 1390 dsl_dataset_phys_t *ds = data; 1391 time_t crtime; 1392 char used[32], compressed[32], uncompressed[32], unique[32]; 1393 char blkbuf[BP_SPRINTF_LEN]; 1394 1395 if (ds == NULL) 1396 return; 1397 1398 ASSERT(size == sizeof (*ds)); 1399 crtime = ds->ds_creation_time; 1400 zdb_nicenum(ds->ds_referenced_bytes, used); 1401 zdb_nicenum(ds->ds_compressed_bytes, compressed); 1402 zdb_nicenum(ds->ds_uncompressed_bytes, uncompressed); 1403 zdb_nicenum(ds->ds_unique_bytes, unique); 1404 snprintf_blkptr(blkbuf, sizeof (blkbuf), &ds->ds_bp); 1405 1406 (void) printf("\t\tdir_obj = %llu\n", 1407 (u_longlong_t)ds->ds_dir_obj); 1408 (void) printf("\t\tprev_snap_obj = %llu\n", 1409 (u_longlong_t)ds->ds_prev_snap_obj); 1410 (void) printf("\t\tprev_snap_txg = %llu\n", 1411 (u_longlong_t)ds->ds_prev_snap_txg); 1412 (void) printf("\t\tnext_snap_obj = %llu\n", 1413 (u_longlong_t)ds->ds_next_snap_obj); 1414 (void) printf("\t\tsnapnames_zapobj = %llu\n", 1415 (u_longlong_t)ds->ds_snapnames_zapobj); 1416 (void) printf("\t\tnum_children = %llu\n", 1417 (u_longlong_t)ds->ds_num_children); 1418 (void) printf("\t\tuserrefs_obj = %llu\n", 1419 (u_longlong_t)ds->ds_userrefs_obj); 1420 (void) printf("\t\tcreation_time = %s", ctime(&crtime)); 1421 (void) printf("\t\tcreation_txg = %llu\n", 1422 (u_longlong_t)ds->ds_creation_txg); 1423 (void) printf("\t\tdeadlist_obj = %llu\n", 1424 (u_longlong_t)ds->ds_deadlist_obj); 1425 (void) printf("\t\tused_bytes = %s\n", used); 1426 (void) printf("\t\tcompressed_bytes = %s\n", compressed); 1427 (void) printf("\t\tuncompressed_bytes = %s\n", uncompressed); 1428 (void) printf("\t\tunique = %s\n", unique); 1429 (void) printf("\t\tfsid_guid = %llu\n", 1430 (u_longlong_t)ds->ds_fsid_guid); 1431 (void) printf("\t\tguid = %llu\n", 1432 (u_longlong_t)ds->ds_guid); 1433 (void) printf("\t\tflags = %llx\n", 1434 (u_longlong_t)ds->ds_flags); 1435 (void) printf("\t\tnext_clones_obj = %llu\n", 1436 (u_longlong_t)ds->ds_next_clones_obj); 1437 (void) printf("\t\tprops_obj = %llu\n", 1438 (u_longlong_t)ds->ds_props_obj); 1439 (void) printf("\t\tbp = %s\n", blkbuf); 1440} 1441 1442/* ARGSUSED */ 1443static int 1444dump_bptree_cb(void *arg, const blkptr_t *bp, dmu_tx_t *tx) 1445{ 1446 char blkbuf[BP_SPRINTF_LEN]; 1447 1448 if (bp->blk_birth != 0) { 1449 snprintf_blkptr(blkbuf, sizeof (blkbuf), bp); 1450 (void) printf("\t%s\n", blkbuf); 1451 } 1452 return (0); 1453} 1454 1455static void 1456dump_bptree(objset_t *os, uint64_t obj, char *name) 1457{ 1458 char bytes[32]; 1459 bptree_phys_t *bt; 1460 dmu_buf_t *db; 1461 1462 if (dump_opt['d'] < 3) 1463 return; 1464 1465 VERIFY3U(0, ==, dmu_bonus_hold(os, obj, FTAG, &db)); 1466 bt = db->db_data; 1467 zdb_nicenum(bt->bt_bytes, bytes); 1468 (void) printf("\n %s: %llu datasets, %s\n", 1469 name, (unsigned long long)(bt->bt_end - bt->bt_begin), bytes); 1470 dmu_buf_rele(db, FTAG); 1471 1472 if (dump_opt['d'] < 5) 1473 return; 1474 1475 (void) printf("\n"); 1476 1477 (void) bptree_iterate(os, obj, B_FALSE, dump_bptree_cb, NULL, NULL); 1478} 1479 1480/* ARGSUSED */ 1481static int 1482dump_bpobj_cb(void *arg, const blkptr_t *bp, dmu_tx_t *tx) 1483{ 1484 char blkbuf[BP_SPRINTF_LEN]; 1485 1486 ASSERT(bp->blk_birth != 0); 1487 snprintf_blkptr_compact(blkbuf, sizeof (blkbuf), bp); 1488 (void) printf("\t%s\n", blkbuf); 1489 return (0); 1490} 1491 1492static void 1493dump_full_bpobj(bpobj_t *bpo, char *name, int indent) 1494{ 1495 char bytes[32]; 1496 char comp[32]; 1497 char uncomp[32]; 1498 1499 if (dump_opt['d'] < 3) 1500 return; 1501 1502 zdb_nicenum(bpo->bpo_phys->bpo_bytes, bytes); 1503 if (bpo->bpo_havesubobj && bpo->bpo_phys->bpo_subobjs != 0) { 1504 zdb_nicenum(bpo->bpo_phys->bpo_comp, comp); 1505 zdb_nicenum(bpo->bpo_phys->bpo_uncomp, uncomp); 1506 (void) printf(" %*s: object %llu, %llu local blkptrs, " 1507 "%llu subobjs in object %llu, %s (%s/%s comp)\n", 1508 indent * 8, name, 1509 (u_longlong_t)bpo->bpo_object, 1510 (u_longlong_t)bpo->bpo_phys->bpo_num_blkptrs, 1511 (u_longlong_t)bpo->bpo_phys->bpo_num_subobjs, 1512 (u_longlong_t)bpo->bpo_phys->bpo_subobjs, 1513 bytes, comp, uncomp); 1514 1515 for (uint64_t i = 0; i < bpo->bpo_phys->bpo_num_subobjs; i++) { 1516 uint64_t subobj; 1517 bpobj_t subbpo; 1518 int error; 1519 VERIFY0(dmu_read(bpo->bpo_os, 1520 bpo->bpo_phys->bpo_subobjs, 1521 i * sizeof (subobj), sizeof (subobj), &subobj, 0)); 1522 error = bpobj_open(&subbpo, bpo->bpo_os, subobj); 1523 if (error != 0) { 1524 (void) printf("ERROR %u while trying to open " 1525 "subobj id %llu\n", 1526 error, (u_longlong_t)subobj); 1527 continue; 1528 } 1529 dump_full_bpobj(&subbpo, "subobj", indent + 1); 1530 bpobj_close(&subbpo); 1531 } 1532 } else { 1533 (void) printf(" %*s: object %llu, %llu blkptrs, %s\n", 1534 indent * 8, name, 1535 (u_longlong_t)bpo->bpo_object, 1536 (u_longlong_t)bpo->bpo_phys->bpo_num_blkptrs, 1537 bytes); 1538 } 1539 1540 if (dump_opt['d'] < 5) 1541 return; 1542 1543 1544 if (indent == 0) { 1545 (void) bpobj_iterate_nofree(bpo, dump_bpobj_cb, NULL, NULL); 1546 (void) printf("\n"); 1547 } 1548} 1549 1550static void 1551dump_deadlist(dsl_deadlist_t *dl) 1552{ 1553 dsl_deadlist_entry_t *dle; 1554 uint64_t unused; 1555 char bytes[32]; 1556 char comp[32]; 1557 char uncomp[32]; 1558 1559 if (dump_opt['d'] < 3) 1560 return; 1561 1562 if (dl->dl_oldfmt) { 1563 dump_full_bpobj(&dl->dl_bpobj, "old-format deadlist", 0); 1564 return; 1565 } 1566 1567 zdb_nicenum(dl->dl_phys->dl_used, bytes); 1568 zdb_nicenum(dl->dl_phys->dl_comp, comp); 1569 zdb_nicenum(dl->dl_phys->dl_uncomp, uncomp); 1570 (void) printf("\n Deadlist: %s (%s/%s comp)\n", 1571 bytes, comp, uncomp); 1572 1573 if (dump_opt['d'] < 4) 1574 return; 1575 1576 (void) printf("\n"); 1577 1578 /* force the tree to be loaded */ 1579 dsl_deadlist_space_range(dl, 0, UINT64_MAX, &unused, &unused, &unused); 1580 1581 for (dle = avl_first(&dl->dl_tree); dle; 1582 dle = AVL_NEXT(&dl->dl_tree, dle)) { 1583 if (dump_opt['d'] >= 5) { 1584 char buf[128]; 1585 (void) snprintf(buf, sizeof (buf), "mintxg %llu -> " 1586 "obj %llu", (longlong_t)dle->dle_mintxg, 1587 (longlong_t)dle->dle_bpobj.bpo_object); 1588 dump_full_bpobj(&dle->dle_bpobj, buf, 0); 1589 } else { 1590 (void) printf("mintxg %llu -> obj %llu\n", 1591 (longlong_t)dle->dle_mintxg, 1592 (longlong_t)dle->dle_bpobj.bpo_object); 1593 } 1594 } 1595} 1596 1597static avl_tree_t idx_tree; 1598static avl_tree_t domain_tree; 1599static boolean_t fuid_table_loaded; 1600static boolean_t sa_loaded; 1601sa_attr_type_t *sa_attr_table; 1602 1603static void 1604fuid_table_destroy() 1605{ 1606 if (fuid_table_loaded) { 1607 zfs_fuid_table_destroy(&idx_tree, &domain_tree); 1608 fuid_table_loaded = B_FALSE; 1609 } 1610} 1611 1612/* 1613 * print uid or gid information. 1614 * For normal POSIX id just the id is printed in decimal format. 1615 * For CIFS files with FUID the fuid is printed in hex followed by 1616 * the domain-rid string. 1617 */ 1618static void 1619print_idstr(uint64_t id, const char *id_type) 1620{ 1621 if (FUID_INDEX(id)) { 1622 char *domain; 1623 1624 domain = zfs_fuid_idx_domain(&idx_tree, FUID_INDEX(id)); 1625 (void) printf("\t%s %llx [%s-%d]\n", id_type, 1626 (u_longlong_t)id, domain, (int)FUID_RID(id)); 1627 } else { 1628 (void) printf("\t%s %llu\n", id_type, (u_longlong_t)id); 1629 } 1630 1631} 1632 1633static void 1634dump_uidgid(objset_t *os, uint64_t uid, uint64_t gid) 1635{ 1636 uint32_t uid_idx, gid_idx; 1637 1638 uid_idx = FUID_INDEX(uid); 1639 gid_idx = FUID_INDEX(gid); 1640 1641 /* Load domain table, if not already loaded */ 1642 if (!fuid_table_loaded && (uid_idx || gid_idx)) { 1643 uint64_t fuid_obj; 1644 1645 /* first find the fuid object. It lives in the master node */ 1646 VERIFY(zap_lookup(os, MASTER_NODE_OBJ, ZFS_FUID_TABLES, 1647 8, 1, &fuid_obj) == 0); 1648 zfs_fuid_avl_tree_create(&idx_tree, &domain_tree); 1649 (void) zfs_fuid_table_load(os, fuid_obj, 1650 &idx_tree, &domain_tree); 1651 fuid_table_loaded = B_TRUE; 1652 } 1653 1654 print_idstr(uid, "uid"); 1655 print_idstr(gid, "gid"); 1656} 1657 1658/*ARGSUSED*/ 1659static void 1660dump_znode(objset_t *os, uint64_t object, void *data, size_t size) 1661{ 1662 char path[MAXPATHLEN * 2]; /* allow for xattr and failure prefix */ 1663 sa_handle_t *hdl; 1664 uint64_t xattr, rdev, gen; 1665 uint64_t uid, gid, mode, fsize, parent, links; 1666 uint64_t pflags; 1667 uint64_t acctm[2], modtm[2], chgtm[2], crtm[2]; 1668 time_t z_crtime, z_atime, z_mtime, z_ctime; 1669 sa_bulk_attr_t bulk[12]; 1670 int idx = 0; 1671 int error; 1672 1673 if (!sa_loaded) { 1674 uint64_t sa_attrs = 0; 1675 uint64_t version; 1676 1677 VERIFY(zap_lookup(os, MASTER_NODE_OBJ, ZPL_VERSION_STR, 1678 8, 1, &version) == 0); 1679 if (version >= ZPL_VERSION_SA) { 1680 VERIFY(zap_lookup(os, MASTER_NODE_OBJ, ZFS_SA_ATTRS, 1681 8, 1, &sa_attrs) == 0); 1682 } 1683 if ((error = sa_setup(os, sa_attrs, zfs_attr_table, 1684 ZPL_END, &sa_attr_table)) != 0) { 1685 (void) printf("sa_setup failed errno %d, can't " 1686 "display znode contents\n", error); 1687 return; 1688 } 1689 sa_loaded = B_TRUE; 1690 } 1691 1692 if (sa_handle_get(os, object, NULL, SA_HDL_PRIVATE, &hdl)) { 1693 (void) printf("Failed to get handle for SA znode\n"); 1694 return; 1695 } 1696 1697 SA_ADD_BULK_ATTR(bulk, idx, sa_attr_table[ZPL_UID], NULL, &uid, 8); 1698 SA_ADD_BULK_ATTR(bulk, idx, sa_attr_table[ZPL_GID], NULL, &gid, 8); 1699 SA_ADD_BULK_ATTR(bulk, idx, sa_attr_table[ZPL_LINKS], NULL, 1700 &links, 8); 1701 SA_ADD_BULK_ATTR(bulk, idx, sa_attr_table[ZPL_GEN], NULL, &gen, 8); 1702 SA_ADD_BULK_ATTR(bulk, idx, sa_attr_table[ZPL_MODE], NULL, 1703 &mode, 8); 1704 SA_ADD_BULK_ATTR(bulk, idx, sa_attr_table[ZPL_PARENT], 1705 NULL, &parent, 8); 1706 SA_ADD_BULK_ATTR(bulk, idx, sa_attr_table[ZPL_SIZE], NULL, 1707 &fsize, 8); 1708 SA_ADD_BULK_ATTR(bulk, idx, sa_attr_table[ZPL_ATIME], NULL, 1709 acctm, 16); 1710 SA_ADD_BULK_ATTR(bulk, idx, sa_attr_table[ZPL_MTIME], NULL, 1711 modtm, 16); 1712 SA_ADD_BULK_ATTR(bulk, idx, sa_attr_table[ZPL_CRTIME], NULL, 1713 crtm, 16); 1714 SA_ADD_BULK_ATTR(bulk, idx, sa_attr_table[ZPL_CTIME], NULL, 1715 chgtm, 16); 1716 SA_ADD_BULK_ATTR(bulk, idx, sa_attr_table[ZPL_FLAGS], NULL, 1717 &pflags, 8); 1718 1719 if (sa_bulk_lookup(hdl, bulk, idx)) { 1720 (void) sa_handle_destroy(hdl); 1721 return; 1722 } 1723 1724 z_crtime = (time_t)crtm[0]; 1725 z_atime = (time_t)acctm[0]; 1726 z_mtime = (time_t)modtm[0]; 1727 z_ctime = (time_t)chgtm[0]; 1728 1729 if (dump_opt['d'] > 4) { 1730 error = zfs_obj_to_path(os, object, path, sizeof (path)); 1731 if (error != 0) { 1732 (void) snprintf(path, sizeof (path), 1733 "\?\?\?<object#%llu>", (u_longlong_t)object); 1734 } 1735 (void) printf("\tpath %s\n", path); 1736 } 1737 dump_uidgid(os, uid, gid); 1738 (void) printf("\tatime %s", ctime(&z_atime)); 1739 (void) printf("\tmtime %s", ctime(&z_mtime)); 1740 (void) printf("\tctime %s", ctime(&z_ctime)); 1741 (void) printf("\tcrtime %s", ctime(&z_crtime)); 1742 (void) printf("\tgen %llu\n", (u_longlong_t)gen); 1743 (void) printf("\tmode %llo\n", (u_longlong_t)mode); 1744 (void) printf("\tsize %llu\n", (u_longlong_t)fsize); 1745 (void) printf("\tparent %llu\n", (u_longlong_t)parent); 1746 (void) printf("\tlinks %llu\n", (u_longlong_t)links); 1747 (void) printf("\tpflags %llx\n", (u_longlong_t)pflags); 1748 if (sa_lookup(hdl, sa_attr_table[ZPL_XATTR], &xattr, 1749 sizeof (uint64_t)) == 0) 1750 (void) printf("\txattr %llu\n", (u_longlong_t)xattr); 1751 if (sa_lookup(hdl, sa_attr_table[ZPL_RDEV], &rdev, 1752 sizeof (uint64_t)) == 0) 1753 (void) printf("\trdev 0x%016llx\n", (u_longlong_t)rdev); 1754 sa_handle_destroy(hdl); 1755} 1756 1757/*ARGSUSED*/ 1758static void 1759dump_acl(objset_t *os, uint64_t object, void *data, size_t size) 1760{ 1761} 1762 1763/*ARGSUSED*/ 1764static void 1765dump_dmu_objset(objset_t *os, uint64_t object, void *data, size_t size) 1766{ 1767} 1768 1769static object_viewer_t *object_viewer[DMU_OT_NUMTYPES + 1] = { 1770 dump_none, /* unallocated */ 1771 dump_zap, /* object directory */ 1772 dump_uint64, /* object array */ 1773 dump_none, /* packed nvlist */ 1774 dump_packed_nvlist, /* packed nvlist size */ 1775 dump_none, /* bpobj */ 1776 dump_bpobj, /* bpobj header */ 1777 dump_none, /* SPA space map header */ 1778 dump_none, /* SPA space map */ 1779 dump_none, /* ZIL intent log */ 1780 dump_dnode, /* DMU dnode */ 1781 dump_dmu_objset, /* DMU objset */ 1782 dump_dsl_dir, /* DSL directory */ 1783 dump_zap, /* DSL directory child map */ 1784 dump_zap, /* DSL dataset snap map */ 1785 dump_zap, /* DSL props */ 1786 dump_dsl_dataset, /* DSL dataset */ 1787 dump_znode, /* ZFS znode */ 1788 dump_acl, /* ZFS V0 ACL */ 1789 dump_uint8, /* ZFS plain file */ 1790 dump_zpldir, /* ZFS directory */ 1791 dump_zap, /* ZFS master node */ 1792 dump_zap, /* ZFS delete queue */ 1793 dump_uint8, /* zvol object */ 1794 dump_zap, /* zvol prop */ 1795 dump_uint8, /* other uint8[] */ 1796 dump_uint64, /* other uint64[] */ 1797 dump_zap, /* other ZAP */ 1798 dump_zap, /* persistent error log */ 1799 dump_uint8, /* SPA history */ 1800 dump_history_offsets, /* SPA history offsets */ 1801 dump_zap, /* Pool properties */ 1802 dump_zap, /* DSL permissions */ 1803 dump_acl, /* ZFS ACL */ 1804 dump_uint8, /* ZFS SYSACL */ 1805 dump_none, /* FUID nvlist */ 1806 dump_packed_nvlist, /* FUID nvlist size */ 1807 dump_zap, /* DSL dataset next clones */ 1808 dump_zap, /* DSL scrub queue */ 1809 dump_zap, /* ZFS user/group used */ 1810 dump_zap, /* ZFS user/group quota */ 1811 dump_zap, /* snapshot refcount tags */ 1812 dump_ddt_zap, /* DDT ZAP object */ 1813 dump_zap, /* DDT statistics */ 1814 dump_znode, /* SA object */ 1815 dump_zap, /* SA Master Node */ 1816 dump_sa_attrs, /* SA attribute registration */ 1817 dump_sa_layouts, /* SA attribute layouts */ 1818 dump_zap, /* DSL scrub translations */ 1819 dump_none, /* fake dedup BP */ 1820 dump_zap, /* deadlist */ 1821 dump_none, /* deadlist hdr */ 1822 dump_zap, /* dsl clones */ 1823 dump_bpobj_subobjs, /* bpobj subobjs */ 1824 dump_unknown, /* Unknown type, must be last */ 1825}; 1826 1827static void 1828dump_object(objset_t *os, uint64_t object, int verbosity, int *print_header) 1829{ 1830 dmu_buf_t *db = NULL; 1831 dmu_object_info_t doi; 1832 dnode_t *dn; 1833 void *bonus = NULL; 1834 size_t bsize = 0; 1835 char iblk[32], dblk[32], lsize[32], asize[32], fill[32]; 1836 char bonus_size[32]; 1837 char aux[50]; 1838 int error; 1839 1840 if (*print_header) { 1841 (void) printf("\n%10s %3s %5s %5s %5s %5s %6s %s\n", 1842 "Object", "lvl", "iblk", "dblk", "dsize", "lsize", 1843 "%full", "type"); 1844 *print_header = 0; 1845 } 1846 1847 if (object == 0) { 1848 dn = DMU_META_DNODE(os); 1849 } else { 1850 error = dmu_bonus_hold(os, object, FTAG, &db); 1851 if (error) 1852 fatal("dmu_bonus_hold(%llu) failed, errno %u", 1853 object, error); 1854 bonus = db->db_data; 1855 bsize = db->db_size; 1856 dn = DB_DNODE((dmu_buf_impl_t *)db); 1857 } 1858 dmu_object_info_from_dnode(dn, &doi); 1859 1860 zdb_nicenum(doi.doi_metadata_block_size, iblk); 1861 zdb_nicenum(doi.doi_data_block_size, dblk); 1862 zdb_nicenum(doi.doi_max_offset, lsize); 1863 zdb_nicenum(doi.doi_physical_blocks_512 << 9, asize); 1864 zdb_nicenum(doi.doi_bonus_size, bonus_size); 1865 (void) sprintf(fill, "%6.2f", 100.0 * doi.doi_fill_count * 1866 doi.doi_data_block_size / (object == 0 ? DNODES_PER_BLOCK : 1) / 1867 doi.doi_max_offset); 1868 1869 aux[0] = '\0'; 1870 1871 if (doi.doi_checksum != ZIO_CHECKSUM_INHERIT || verbosity >= 6) { 1872 (void) snprintf(aux + strlen(aux), sizeof (aux), " (K=%s)", 1873 ZDB_CHECKSUM_NAME(doi.doi_checksum)); 1874 } 1875 1876 if (doi.doi_compress != ZIO_COMPRESS_INHERIT || verbosity >= 6) { 1877 (void) snprintf(aux + strlen(aux), sizeof (aux), " (Z=%s)", 1878 ZDB_COMPRESS_NAME(doi.doi_compress)); 1879 } 1880 1881 (void) printf("%10lld %3u %5s %5s %5s %5s %6s %s%s\n", 1882 (u_longlong_t)object, doi.doi_indirection, iblk, dblk, 1883 asize, lsize, fill, ZDB_OT_NAME(doi.doi_type), aux); 1884 1885 if (doi.doi_bonus_type != DMU_OT_NONE && verbosity > 3) { 1886 (void) printf("%10s %3s %5s %5s %5s %5s %6s %s\n", 1887 "", "", "", "", "", bonus_size, "bonus", 1888 ZDB_OT_NAME(doi.doi_bonus_type)); 1889 } 1890 1891 if (verbosity >= 4) { 1892 (void) printf("\tdnode flags: %s%s%s\n", 1893 (dn->dn_phys->dn_flags & DNODE_FLAG_USED_BYTES) ? 1894 "USED_BYTES " : "", 1895 (dn->dn_phys->dn_flags & DNODE_FLAG_USERUSED_ACCOUNTED) ? 1896 "USERUSED_ACCOUNTED " : "", 1897 (dn->dn_phys->dn_flags & DNODE_FLAG_SPILL_BLKPTR) ? 1898 "SPILL_BLKPTR" : ""); 1899 (void) printf("\tdnode maxblkid: %llu\n", 1900 (longlong_t)dn->dn_phys->dn_maxblkid); 1901 1902 object_viewer[ZDB_OT_TYPE(doi.doi_bonus_type)](os, object, 1903 bonus, bsize); 1904 object_viewer[ZDB_OT_TYPE(doi.doi_type)](os, object, NULL, 0); 1905 *print_header = 1; 1906 } 1907 1908 if (verbosity >= 5) 1909 dump_indirect(dn); 1910 1911 if (verbosity >= 5) { 1912 /* 1913 * Report the list of segments that comprise the object. 1914 */ 1915 uint64_t start = 0; 1916 uint64_t end; 1917 uint64_t blkfill = 1; 1918 int minlvl = 1; 1919 1920 if (dn->dn_type == DMU_OT_DNODE) { 1921 minlvl = 0; 1922 blkfill = DNODES_PER_BLOCK; 1923 } 1924 1925 for (;;) { 1926 char segsize[32]; 1927 error = dnode_next_offset(dn, 1928 0, &start, minlvl, blkfill, 0); 1929 if (error) 1930 break; 1931 end = start; 1932 error = dnode_next_offset(dn, 1933 DNODE_FIND_HOLE, &end, minlvl, blkfill, 0); 1934 zdb_nicenum(end - start, segsize); 1935 (void) printf("\t\tsegment [%016llx, %016llx)" 1936 " size %5s\n", (u_longlong_t)start, 1937 (u_longlong_t)end, segsize); 1938 if (error) 1939 break; 1940 start = end; 1941 } 1942 } 1943 1944 if (db != NULL) 1945 dmu_buf_rele(db, FTAG); 1946} 1947 1948static char *objset_types[DMU_OST_NUMTYPES] = { 1949 "NONE", "META", "ZPL", "ZVOL", "OTHER", "ANY" }; 1950 1951static void 1952dump_dir(objset_t *os) 1953{ 1954 dmu_objset_stats_t dds; 1955 uint64_t object, object_count; 1956 uint64_t refdbytes, usedobjs, scratch; 1957 char numbuf[32]; 1958 char blkbuf[BP_SPRINTF_LEN + 20]; 1959 char osname[ZFS_MAX_DATASET_NAME_LEN]; 1960 char *type = "UNKNOWN"; 1961 int verbosity = dump_opt['d']; 1962 int print_header = 1; 1963 int i, error; 1964 1965 dsl_pool_config_enter(dmu_objset_pool(os), FTAG); 1966 dmu_objset_fast_stat(os, &dds); 1967 dsl_pool_config_exit(dmu_objset_pool(os), FTAG); 1968 1969 if (dds.dds_type < DMU_OST_NUMTYPES) 1970 type = objset_types[dds.dds_type]; 1971 1972 if (dds.dds_type == DMU_OST_META) { 1973 dds.dds_creation_txg = TXG_INITIAL; 1974 usedobjs = BP_GET_FILL(os->os_rootbp); 1975 refdbytes = dsl_dir_phys(os->os_spa->spa_dsl_pool->dp_mos_dir)-> 1976 dd_used_bytes; 1977 } else { 1978 dmu_objset_space(os, &refdbytes, &scratch, &usedobjs, &scratch); 1979 } 1980 1981 ASSERT3U(usedobjs, ==, BP_GET_FILL(os->os_rootbp)); 1982 1983 zdb_nicenum(refdbytes, numbuf); 1984 1985 if (verbosity >= 4) { 1986 (void) snprintf(blkbuf, sizeof (blkbuf), ", rootbp "); 1987 (void) snprintf_blkptr(blkbuf + strlen(blkbuf), 1988 sizeof (blkbuf) - strlen(blkbuf), os->os_rootbp); 1989 } else { 1990 blkbuf[0] = '\0'; 1991 } 1992 1993 dmu_objset_name(os, osname); 1994 1995 (void) printf("Dataset %s [%s], ID %llu, cr_txg %llu, " 1996 "%s, %llu objects%s\n", 1997 osname, type, (u_longlong_t)dmu_objset_id(os), 1998 (u_longlong_t)dds.dds_creation_txg, 1999 numbuf, (u_longlong_t)usedobjs, blkbuf); 2000 2001 if (zopt_objects != 0) { 2002 for (i = 0; i < zopt_objects; i++) 2003 dump_object(os, zopt_object[i], verbosity, 2004 &print_header); 2005 (void) printf("\n"); 2006 return; 2007 } 2008 2009 if (dump_opt['i'] != 0 || verbosity >= 2) 2010 dump_intent_log(dmu_objset_zil(os)); 2011 2012 if (dmu_objset_ds(os) != NULL) 2013 dump_deadlist(&dmu_objset_ds(os)->ds_deadlist); 2014 2015 if (verbosity < 2) 2016 return; 2017 2018 if (BP_IS_HOLE(os->os_rootbp)) 2019 return; 2020 2021 dump_object(os, 0, verbosity, &print_header); 2022 object_count = 0; 2023 if (DMU_USERUSED_DNODE(os) != NULL && 2024 DMU_USERUSED_DNODE(os)->dn_type != 0) { 2025 dump_object(os, DMU_USERUSED_OBJECT, verbosity, &print_header); 2026 dump_object(os, DMU_GROUPUSED_OBJECT, verbosity, &print_header); 2027 } 2028 2029 object = 0; 2030 while ((error = dmu_object_next(os, &object, B_FALSE, 0)) == 0) { 2031 dump_object(os, object, verbosity, &print_header); 2032 object_count++; 2033 } 2034 2035 ASSERT3U(object_count, ==, usedobjs); 2036 2037 (void) printf("\n"); 2038 2039 if (error != ESRCH) { 2040 (void) fprintf(stderr, "dmu_object_next() = %d\n", error); 2041 abort(); 2042 } 2043} 2044 2045static void 2046dump_uberblock(uberblock_t *ub, const char *header, const char *footer) 2047{ 2048 time_t timestamp = ub->ub_timestamp; 2049 2050 (void) printf(header ? header : ""); 2051 (void) printf("\tmagic = %016llx\n", (u_longlong_t)ub->ub_magic); 2052 (void) printf("\tversion = %llu\n", (u_longlong_t)ub->ub_version); 2053 (void) printf("\ttxg = %llu\n", (u_longlong_t)ub->ub_txg); 2054 (void) printf("\tguid_sum = %llu\n", (u_longlong_t)ub->ub_guid_sum); 2055 (void) printf("\ttimestamp = %llu UTC = %s", 2056 (u_longlong_t)ub->ub_timestamp, asctime(localtime(×tamp))); 2057 if (dump_opt['u'] >= 3) { 2058 char blkbuf[BP_SPRINTF_LEN]; 2059 snprintf_blkptr(blkbuf, sizeof (blkbuf), &ub->ub_rootbp); 2060 (void) printf("\trootbp = %s\n", blkbuf); 2061 } 2062 (void) printf(footer ? footer : ""); 2063} 2064 2065static void 2066dump_config(spa_t *spa) 2067{ 2068 dmu_buf_t *db; 2069 size_t nvsize = 0; 2070 int error = 0; 2071 2072 2073 error = dmu_bonus_hold(spa->spa_meta_objset, 2074 spa->spa_config_object, FTAG, &db); 2075 2076 if (error == 0) { 2077 nvsize = *(uint64_t *)db->db_data; 2078 dmu_buf_rele(db, FTAG); 2079 2080 (void) printf("\nMOS Configuration:\n"); 2081 dump_packed_nvlist(spa->spa_meta_objset, 2082 spa->spa_config_object, (void *)&nvsize, 1); 2083 } else { 2084 (void) fprintf(stderr, "dmu_bonus_hold(%llu) failed, errno %d", 2085 (u_longlong_t)spa->spa_config_object, error); 2086 } 2087} 2088 2089static void 2090dump_cachefile(const char *cachefile) 2091{ 2092 int fd; 2093 struct stat64 statbuf; 2094 char *buf; 2095 nvlist_t *config; 2096 2097 if ((fd = open64(cachefile, O_RDONLY)) < 0) { 2098 (void) fprintf(stderr, "cannot open '%s': %s\n", cachefile, 2099 strerror(errno)); 2100 exit(1); 2101 } 2102 2103 if (fstat64(fd, &statbuf) != 0) { 2104 (void) fprintf(stderr, "failed to stat '%s': %s\n", cachefile, 2105 strerror(errno)); 2106 exit(1); 2107 } 2108 2109 if ((buf = malloc(statbuf.st_size)) == NULL) { 2110 (void) fprintf(stderr, "failed to allocate %llu bytes\n", 2111 (u_longlong_t)statbuf.st_size); 2112 exit(1); 2113 } 2114 2115 if (read(fd, buf, statbuf.st_size) != statbuf.st_size) { 2116 (void) fprintf(stderr, "failed to read %llu bytes\n", 2117 (u_longlong_t)statbuf.st_size); 2118 exit(1); 2119 } 2120 2121 (void) close(fd); 2122 2123 if (nvlist_unpack(buf, statbuf.st_size, &config, 0) != 0) { 2124 (void) fprintf(stderr, "failed to unpack nvlist\n"); 2125 exit(1); 2126 } 2127 2128 free(buf); 2129 2130 dump_nvlist(config, 0); 2131 2132 nvlist_free(config); 2133} 2134 2135#define ZDB_MAX_UB_HEADER_SIZE 32 2136 2137static void 2138dump_label_uberblocks(vdev_label_t *lbl, uint64_t ashift) 2139{ 2140 vdev_t vd; 2141 vdev_t *vdp = &vd; 2142 char header[ZDB_MAX_UB_HEADER_SIZE]; 2143 2144 vd.vdev_ashift = ashift; 2145 vdp->vdev_top = vdp; 2146 2147 for (int i = 0; i < VDEV_UBERBLOCK_COUNT(vdp); i++) { 2148 uint64_t uoff = VDEV_UBERBLOCK_OFFSET(vdp, i); 2149 uberblock_t *ub = (void *)((char *)lbl + uoff); 2150 2151 if (uberblock_verify(ub)) 2152 continue; 2153 (void) snprintf(header, ZDB_MAX_UB_HEADER_SIZE, 2154 "Uberblock[%d]\n", i); 2155 dump_uberblock(ub, header, ""); 2156 } 2157} 2158 2159static void 2160dump_label(const char *dev) 2161{ 2162 int fd; 2163 vdev_label_t label; 2164 char *path, *buf = label.vl_vdev_phys.vp_nvlist; 2165 size_t buflen = sizeof (label.vl_vdev_phys.vp_nvlist); 2166 struct stat64 statbuf; 2167 uint64_t psize, ashift; 2168 int len = strlen(dev) + 1; 2169 2170 if (strncmp(dev, ZFS_DISK_ROOTD, strlen(ZFS_DISK_ROOTD)) == 0) { 2171 len++; 2172 path = malloc(len); 2173 (void) snprintf(path, len, "%s%s", ZFS_RDISK_ROOTD, 2174 dev + strlen(ZFS_DISK_ROOTD)); 2175 } else { 2176 path = strdup(dev); 2177 } 2178 2179 if ((fd = open64(path, O_RDONLY)) < 0) { 2180 (void) printf("cannot open '%s': %s\n", path, strerror(errno)); 2181 free(path); 2182 exit(1); 2183 } 2184 2185 if (fstat64(fd, &statbuf) != 0) { 2186 (void) printf("failed to stat '%s': %s\n", path, 2187 strerror(errno)); 2188 free(path); 2189 (void) close(fd); 2190 exit(1); 2191 } 2192 2193 if (S_ISBLK(statbuf.st_mode)) { 2194 (void) printf("cannot use '%s': character device required\n", 2195 path); 2196 free(path); 2197 (void) close(fd); 2198 exit(1); 2199 } 2200 2201 psize = statbuf.st_size; 2202 psize = P2ALIGN(psize, (uint64_t)sizeof (vdev_label_t)); 2203 2204 for (int l = 0; l < VDEV_LABELS; l++) { 2205 nvlist_t *config = NULL; 2206 2207 (void) printf("--------------------------------------------\n"); 2208 (void) printf("LABEL %d\n", l); 2209 (void) printf("--------------------------------------------\n"); 2210 2211 if (pread64(fd, &label, sizeof (label), 2212 vdev_label_offset(psize, l, 0)) != sizeof (label)) { 2213 (void) printf("failed to read label %d\n", l); 2214 continue; 2215 } 2216 2217 if (nvlist_unpack(buf, buflen, &config, 0) != 0) { 2218 (void) printf("failed to unpack label %d\n", l); 2219 ashift = SPA_MINBLOCKSHIFT; 2220 } else { 2221 nvlist_t *vdev_tree = NULL; 2222 2223 dump_nvlist(config, 4); 2224 if ((nvlist_lookup_nvlist(config, 2225 ZPOOL_CONFIG_VDEV_TREE, &vdev_tree) != 0) || 2226 (nvlist_lookup_uint64(vdev_tree, 2227 ZPOOL_CONFIG_ASHIFT, &ashift) != 0)) 2228 ashift = SPA_MINBLOCKSHIFT; 2229 nvlist_free(config); 2230 } 2231 if (dump_opt['u']) 2232 dump_label_uberblocks(&label, ashift); 2233 } 2234 2235 free(path); 2236 (void) close(fd); 2237} 2238 2239static uint64_t dataset_feature_count[SPA_FEATURES]; 2240 2241/*ARGSUSED*/ 2242static int 2243dump_one_dir(const char *dsname, void *arg) 2244{ 2245 int error; 2246 objset_t *os; 2247 2248 error = dmu_objset_own(dsname, DMU_OST_ANY, B_TRUE, FTAG, &os); 2249 if (error) { 2250 (void) printf("Could not open %s, error %d\n", dsname, error); 2251 return (0); 2252 } 2253 2254 for (spa_feature_t f = 0; f < SPA_FEATURES; f++) { 2255 if (!dmu_objset_ds(os)->ds_feature_inuse[f]) 2256 continue; 2257 ASSERT(spa_feature_table[f].fi_flags & 2258 ZFEATURE_FLAG_PER_DATASET); 2259 dataset_feature_count[f]++; 2260 } 2261 2262 dump_dir(os); 2263 dmu_objset_disown(os, FTAG); 2264 fuid_table_destroy(); 2265 sa_loaded = B_FALSE; 2266 return (0); 2267} 2268 2269/* 2270 * Block statistics. 2271 */ 2272#define PSIZE_HISTO_SIZE (SPA_OLD_MAXBLOCKSIZE / SPA_MINBLOCKSIZE + 2) 2273typedef struct zdb_blkstats { 2274 uint64_t zb_asize; 2275 uint64_t zb_lsize; 2276 uint64_t zb_psize; 2277 uint64_t zb_count; 2278 uint64_t zb_gangs; 2279 uint64_t zb_ditto_samevdev; 2280 uint64_t zb_psize_histogram[PSIZE_HISTO_SIZE]; 2281} zdb_blkstats_t; 2282 2283/* 2284 * Extended object types to report deferred frees and dedup auto-ditto blocks. 2285 */ 2286#define ZDB_OT_DEFERRED (DMU_OT_NUMTYPES + 0) 2287#define ZDB_OT_DITTO (DMU_OT_NUMTYPES + 1) 2288#define ZDB_OT_OTHER (DMU_OT_NUMTYPES + 2) 2289#define ZDB_OT_TOTAL (DMU_OT_NUMTYPES + 3) 2290 2291static char *zdb_ot_extname[] = { 2292 "deferred free", 2293 "dedup ditto", 2294 "other", 2295 "Total", 2296}; 2297 2298#define ZB_TOTAL DN_MAX_LEVELS 2299 2300typedef struct zdb_cb { 2301 zdb_blkstats_t zcb_type[ZB_TOTAL + 1][ZDB_OT_TOTAL + 1]; 2302 uint64_t zcb_dedup_asize; 2303 uint64_t zcb_dedup_blocks; 2304 uint64_t zcb_embedded_blocks[NUM_BP_EMBEDDED_TYPES]; 2305 uint64_t zcb_embedded_histogram[NUM_BP_EMBEDDED_TYPES] 2306 [BPE_PAYLOAD_SIZE]; 2307 uint64_t zcb_start; 2308 uint64_t zcb_lastprint; 2309 uint64_t zcb_totalasize; 2310 uint64_t zcb_errors[256]; 2311 int zcb_readfails; 2312 int zcb_haderrors; 2313 spa_t *zcb_spa; 2314} zdb_cb_t; 2315 2316static void 2317zdb_count_block(zdb_cb_t *zcb, zilog_t *zilog, const blkptr_t *bp, 2318 dmu_object_type_t type) 2319{ 2320 uint64_t refcnt = 0; 2321 2322 ASSERT(type < ZDB_OT_TOTAL); 2323 2324 if (zilog && zil_bp_tree_add(zilog, bp) != 0) 2325 return; 2326 2327 for (int i = 0; i < 4; i++) { 2328 int l = (i < 2) ? BP_GET_LEVEL(bp) : ZB_TOTAL; 2329 int t = (i & 1) ? type : ZDB_OT_TOTAL; 2330 int equal; 2331 zdb_blkstats_t *zb = &zcb->zcb_type[l][t]; 2332 2333 zb->zb_asize += BP_GET_ASIZE(bp); 2334 zb->zb_lsize += BP_GET_LSIZE(bp); 2335 zb->zb_psize += BP_GET_PSIZE(bp); 2336 zb->zb_count++; 2337 2338 /* 2339 * The histogram is only big enough to record blocks up to 2340 * SPA_OLD_MAXBLOCKSIZE; larger blocks go into the last, 2341 * "other", bucket. 2342 */ 2343 int idx = BP_GET_PSIZE(bp) >> SPA_MINBLOCKSHIFT; 2344 idx = MIN(idx, SPA_OLD_MAXBLOCKSIZE / SPA_MINBLOCKSIZE + 1); 2345 zb->zb_psize_histogram[idx]++; 2346 2347 zb->zb_gangs += BP_COUNT_GANG(bp); 2348 2349 switch (BP_GET_NDVAS(bp)) { 2350 case 2: 2351 if (DVA_GET_VDEV(&bp->blk_dva[0]) == 2352 DVA_GET_VDEV(&bp->blk_dva[1])) 2353 zb->zb_ditto_samevdev++; 2354 break; 2355 case 3: 2356 equal = (DVA_GET_VDEV(&bp->blk_dva[0]) == 2357 DVA_GET_VDEV(&bp->blk_dva[1])) + 2358 (DVA_GET_VDEV(&bp->blk_dva[0]) == 2359 DVA_GET_VDEV(&bp->blk_dva[2])) + 2360 (DVA_GET_VDEV(&bp->blk_dva[1]) == 2361 DVA_GET_VDEV(&bp->blk_dva[2])); 2362 if (equal != 0) 2363 zb->zb_ditto_samevdev++; 2364 break; 2365 } 2366 2367 } 2368 2369 if (BP_IS_EMBEDDED(bp)) { 2370 zcb->zcb_embedded_blocks[BPE_GET_ETYPE(bp)]++; 2371 zcb->zcb_embedded_histogram[BPE_GET_ETYPE(bp)] 2372 [BPE_GET_PSIZE(bp)]++; 2373 return; 2374 } 2375 2376 if (dump_opt['L']) 2377 return; 2378 2379 if (BP_GET_DEDUP(bp)) { 2380 ddt_t *ddt; 2381 ddt_entry_t *dde; 2382 2383 ddt = ddt_select(zcb->zcb_spa, bp); 2384 ddt_enter(ddt); 2385 dde = ddt_lookup(ddt, bp, B_FALSE); 2386 2387 if (dde == NULL) { 2388 refcnt = 0; 2389 } else { 2390 ddt_phys_t *ddp = ddt_phys_select(dde, bp); 2391 ddt_phys_decref(ddp); 2392 refcnt = ddp->ddp_refcnt; 2393 if (ddt_phys_total_refcnt(dde) == 0) 2394 ddt_remove(ddt, dde); 2395 } 2396 ddt_exit(ddt); 2397 } 2398 2399 VERIFY3U(zio_wait(zio_claim(NULL, zcb->zcb_spa, 2400 refcnt ? 0 : spa_first_txg(zcb->zcb_spa), 2401 bp, NULL, NULL, ZIO_FLAG_CANFAIL)), ==, 0); 2402} 2403 2404/* ARGSUSED */ 2405static void 2406zdb_blkptr_done(zio_t *zio) 2407{ 2408 spa_t *spa = zio->io_spa; 2409 blkptr_t *bp = zio->io_bp; 2410 int ioerr = zio->io_error; 2411 zdb_cb_t *zcb = zio->io_private; 2412 zbookmark_phys_t *zb = &zio->io_bookmark; 2413 2414 abd_free(zio->io_abd); 2415 2416 mutex_enter(&spa->spa_scrub_lock); 2417 spa->spa_scrub_inflight--; 2418 cv_broadcast(&spa->spa_scrub_io_cv); 2419 2420 if (ioerr && !(zio->io_flags & ZIO_FLAG_SPECULATIVE)) { 2421 char blkbuf[BP_SPRINTF_LEN]; 2422 2423 zcb->zcb_haderrors = 1; 2424 zcb->zcb_errors[ioerr]++; 2425 2426 if (dump_opt['b'] >= 2) 2427 snprintf_blkptr(blkbuf, sizeof (blkbuf), bp); 2428 else 2429 blkbuf[0] = '\0'; 2430 2431 (void) printf("zdb_blkptr_cb: " 2432 "Got error %d reading " 2433 "<%llu, %llu, %lld, %llx> %s -- skipping\n", 2434 ioerr, 2435 (u_longlong_t)zb->zb_objset, 2436 (u_longlong_t)zb->zb_object, 2437 (u_longlong_t)zb->zb_level, 2438 (u_longlong_t)zb->zb_blkid, 2439 blkbuf); 2440 } 2441 mutex_exit(&spa->spa_scrub_lock); 2442} 2443 2444/* ARGSUSED */ 2445static int 2446zdb_blkptr_cb(spa_t *spa, zilog_t *zilog, const blkptr_t *bp, 2447 const zbookmark_phys_t *zb, const dnode_phys_t *dnp, void *arg) 2448{ 2449 zdb_cb_t *zcb = arg; 2450 dmu_object_type_t type; 2451 boolean_t is_metadata; 2452 2453 if (bp == NULL) 2454 return (0); 2455 2456 if (dump_opt['b'] >= 5 && bp->blk_birth > 0) { 2457 char blkbuf[BP_SPRINTF_LEN]; 2458 snprintf_blkptr(blkbuf, sizeof (blkbuf), bp); 2459 (void) printf("objset %llu object %llu " 2460 "level %lld offset 0x%llx %s\n", 2461 (u_longlong_t)zb->zb_objset, 2462 (u_longlong_t)zb->zb_object, 2463 (longlong_t)zb->zb_level, 2464 (u_longlong_t)blkid2offset(dnp, bp, zb), 2465 blkbuf); 2466 } 2467 2468 if (BP_IS_HOLE(bp)) 2469 return (0); 2470 2471 type = BP_GET_TYPE(bp); 2472 2473 zdb_count_block(zcb, zilog, bp, 2474 (type & DMU_OT_NEWTYPE) ? ZDB_OT_OTHER : type); 2475 2476 is_metadata = (BP_GET_LEVEL(bp) != 0 || DMU_OT_IS_METADATA(type)); 2477 2478 if (!BP_IS_EMBEDDED(bp) && 2479 (dump_opt['c'] > 1 || (dump_opt['c'] && is_metadata))) { 2480 size_t size = BP_GET_PSIZE(bp); 2481 abd_t *abd = abd_alloc(size, B_FALSE); 2482 int flags = ZIO_FLAG_CANFAIL | ZIO_FLAG_SCRUB | ZIO_FLAG_RAW; 2483 2484 /* If it's an intent log block, failure is expected. */ 2485 if (zb->zb_level == ZB_ZIL_LEVEL) 2486 flags |= ZIO_FLAG_SPECULATIVE; 2487 2488 mutex_enter(&spa->spa_scrub_lock); 2489 while (spa->spa_scrub_inflight > max_inflight) 2490 cv_wait(&spa->spa_scrub_io_cv, &spa->spa_scrub_lock); 2491 spa->spa_scrub_inflight++; 2492 mutex_exit(&spa->spa_scrub_lock); 2493 2494 zio_nowait(zio_read(NULL, spa, bp, abd, size, 2495 zdb_blkptr_done, zcb, ZIO_PRIORITY_ASYNC_READ, flags, zb)); 2496 } 2497 2498 zcb->zcb_readfails = 0; 2499 2500 /* only call gethrtime() every 100 blocks */ 2501 static int iters; 2502 if (++iters > 100) 2503 iters = 0; 2504 else 2505 return (0); 2506 2507 if (dump_opt['b'] < 5 && gethrtime() > zcb->zcb_lastprint + NANOSEC) { 2508 uint64_t now = gethrtime(); 2509 char buf[10]; 2510 uint64_t bytes = zcb->zcb_type[ZB_TOTAL][ZDB_OT_TOTAL].zb_asize; 2511 int kb_per_sec = 2512 1 + bytes / (1 + ((now - zcb->zcb_start) / 1000 / 1000)); 2513 int sec_remaining = 2514 (zcb->zcb_totalasize - bytes) / 1024 / kb_per_sec; 2515 2516 zfs_nicenum(bytes, buf, sizeof (buf)); 2517 (void) fprintf(stderr, 2518 "\r%5s completed (%4dMB/s) " 2519 "estimated time remaining: %uhr %02umin %02usec ", 2520 buf, kb_per_sec / 1024, 2521 sec_remaining / 60 / 60, 2522 sec_remaining / 60 % 60, 2523 sec_remaining % 60); 2524 2525 zcb->zcb_lastprint = now; 2526 } 2527 2528 return (0); 2529} 2530 2531static void 2532zdb_leak(void *arg, uint64_t start, uint64_t size) 2533{ 2534 vdev_t *vd = arg; 2535 2536 (void) printf("leaked space: vdev %llu, offset 0x%llx, size %llu\n", 2537 (u_longlong_t)vd->vdev_id, (u_longlong_t)start, (u_longlong_t)size); 2538} 2539 2540static metaslab_ops_t zdb_metaslab_ops = { 2541 NULL /* alloc */ 2542}; 2543 2544static void 2545zdb_ddt_leak_init(spa_t *spa, zdb_cb_t *zcb) 2546{ 2547 ddt_bookmark_t ddb = { 0 }; 2548 ddt_entry_t dde; 2549 int error; 2550 2551 while ((error = ddt_walk(spa, &ddb, &dde)) == 0) { 2552 blkptr_t blk; 2553 ddt_phys_t *ddp = dde.dde_phys; 2554 2555 if (ddb.ddb_class == DDT_CLASS_UNIQUE) 2556 return; 2557 2558 ASSERT(ddt_phys_total_refcnt(&dde) > 1); 2559 2560 for (int p = 0; p < DDT_PHYS_TYPES; p++, ddp++) { 2561 if (ddp->ddp_phys_birth == 0) 2562 continue; 2563 ddt_bp_create(ddb.ddb_checksum, 2564 &dde.dde_key, ddp, &blk); 2565 if (p == DDT_PHYS_DITTO) { 2566 zdb_count_block(zcb, NULL, &blk, ZDB_OT_DITTO); 2567 } else { 2568 zcb->zcb_dedup_asize += 2569 BP_GET_ASIZE(&blk) * (ddp->ddp_refcnt - 1); 2570 zcb->zcb_dedup_blocks++; 2571 } 2572 } 2573 if (!dump_opt['L']) { 2574 ddt_t *ddt = spa->spa_ddt[ddb.ddb_checksum]; 2575 ddt_enter(ddt); 2576 VERIFY(ddt_lookup(ddt, &blk, B_TRUE) != NULL); 2577 ddt_exit(ddt); 2578 } 2579 } 2580 2581 ASSERT(error == ENOENT); 2582} 2583 2584static void 2585zdb_leak_init(spa_t *spa, zdb_cb_t *zcb) 2586{ 2587 zcb->zcb_spa = spa; 2588 2589 if (!dump_opt['L']) { 2590 vdev_t *rvd = spa->spa_root_vdev; 2591 2592 /* 2593 * We are going to be changing the meaning of the metaslab's 2594 * ms_tree. Ensure that the allocator doesn't try to 2595 * use the tree. 2596 */ 2597 spa->spa_normal_class->mc_ops = &zdb_metaslab_ops; 2598 spa->spa_log_class->mc_ops = &zdb_metaslab_ops; 2599 2600 for (uint64_t c = 0; c < rvd->vdev_children; c++) { 2601 vdev_t *vd = rvd->vdev_child[c]; 2602 metaslab_group_t *mg = vd->vdev_mg; 2603 for (uint64_t m = 0; m < vd->vdev_ms_count; m++) { 2604 metaslab_t *msp = vd->vdev_ms[m]; 2605 ASSERT3P(msp->ms_group, ==, mg); 2606 mutex_enter(&msp->ms_lock); 2607 metaslab_unload(msp); 2608 2609 /* 2610 * For leak detection, we overload the metaslab 2611 * ms_tree to contain allocated segments 2612 * instead of free segments. As a result, 2613 * we can't use the normal metaslab_load/unload 2614 * interfaces. 2615 */ 2616 if (msp->ms_sm != NULL) { 2617 (void) fprintf(stderr, 2618 "\rloading space map for " 2619 "vdev %llu of %llu, " 2620 "metaslab %llu of %llu ...", 2621 (longlong_t)c, 2622 (longlong_t)rvd->vdev_children, 2623 (longlong_t)m, 2624 (longlong_t)vd->vdev_ms_count); 2625 2626 /* 2627 * We don't want to spend the CPU 2628 * manipulating the size-ordered 2629 * tree, so clear the range_tree 2630 * ops. 2631 */ 2632 msp->ms_tree->rt_ops = NULL; 2633 VERIFY0(space_map_load(msp->ms_sm, 2634 msp->ms_tree, SM_ALLOC)); 2635 2636 if (!msp->ms_loaded) { 2637 msp->ms_loaded = B_TRUE; 2638 } 2639 } 2640 mutex_exit(&msp->ms_lock); 2641 } 2642 } 2643 (void) fprintf(stderr, "\n"); 2644 } 2645 2646 spa_config_enter(spa, SCL_CONFIG, FTAG, RW_READER); 2647 2648 zdb_ddt_leak_init(spa, zcb); 2649 2650 spa_config_exit(spa, SCL_CONFIG, FTAG); 2651} 2652 2653static void 2654zdb_leak_fini(spa_t *spa) 2655{ 2656 if (!dump_opt['L']) { 2657 vdev_t *rvd = spa->spa_root_vdev; 2658 for (int c = 0; c < rvd->vdev_children; c++) { 2659 vdev_t *vd = rvd->vdev_child[c]; 2660 metaslab_group_t *mg = vd->vdev_mg; 2661 for (int m = 0; m < vd->vdev_ms_count; m++) { 2662 metaslab_t *msp = vd->vdev_ms[m]; 2663 ASSERT3P(mg, ==, msp->ms_group); 2664 mutex_enter(&msp->ms_lock); 2665 2666 /* 2667 * The ms_tree has been overloaded to 2668 * contain allocated segments. Now that we 2669 * finished traversing all blocks, any 2670 * block that remains in the ms_tree 2671 * represents an allocated block that we 2672 * did not claim during the traversal. 2673 * Claimed blocks would have been removed 2674 * from the ms_tree. 2675 */ 2676 range_tree_vacate(msp->ms_tree, zdb_leak, vd); 2677 2678 if (msp->ms_loaded) { 2679 msp->ms_loaded = B_FALSE; 2680 } 2681 2682 mutex_exit(&msp->ms_lock); 2683 } 2684 } 2685 } 2686} 2687 2688/* ARGSUSED */ 2689static int 2690count_block_cb(void *arg, const blkptr_t *bp, dmu_tx_t *tx) 2691{ 2692 zdb_cb_t *zcb = arg; 2693 2694 if (dump_opt['b'] >= 5) { 2695 char blkbuf[BP_SPRINTF_LEN]; 2696 snprintf_blkptr(blkbuf, sizeof (blkbuf), bp); 2697 (void) printf("[%s] %s\n", 2698 "deferred free", blkbuf); 2699 } 2700 zdb_count_block(zcb, NULL, bp, ZDB_OT_DEFERRED); 2701 return (0); 2702} 2703 2704static int 2705dump_block_stats(spa_t *spa) 2706{ 2707 zdb_cb_t zcb = { 0 }; 2708 zdb_blkstats_t *zb, *tzb; 2709 uint64_t norm_alloc, norm_space, total_alloc, total_found; 2710 int flags = TRAVERSE_PRE | TRAVERSE_PREFETCH_METADATA | TRAVERSE_HARD; 2711 boolean_t leaks = B_FALSE; 2712 2713 (void) printf("\nTraversing all blocks %s%s%s%s%s...\n\n", 2714 (dump_opt['c'] || !dump_opt['L']) ? "to verify " : "", 2715 (dump_opt['c'] == 1) ? "metadata " : "", 2716 dump_opt['c'] ? "checksums " : "", 2717 (dump_opt['c'] && !dump_opt['L']) ? "and verify " : "", 2718 !dump_opt['L'] ? "nothing leaked " : ""); 2719 2720 /* 2721 * Load all space maps as SM_ALLOC maps, then traverse the pool 2722 * claiming each block we discover. If the pool is perfectly 2723 * consistent, the space maps will be empty when we're done. 2724 * Anything left over is a leak; any block we can't claim (because 2725 * it's not part of any space map) is a double allocation, 2726 * reference to a freed block, or an unclaimed log block. 2727 */ 2728 zdb_leak_init(spa, &zcb); 2729 2730 /* 2731 * If there's a deferred-free bplist, process that first. 2732 */ 2733 (void) bpobj_iterate_nofree(&spa->spa_deferred_bpobj, 2734 count_block_cb, &zcb, NULL); 2735 if (spa_version(spa) >= SPA_VERSION_DEADLISTS) { 2736 (void) bpobj_iterate_nofree(&spa->spa_dsl_pool->dp_free_bpobj, 2737 count_block_cb, &zcb, NULL); 2738 } 2739 if (spa_feature_is_active(spa, SPA_FEATURE_ASYNC_DESTROY)) { 2740 VERIFY3U(0, ==, bptree_iterate(spa->spa_meta_objset, 2741 spa->spa_dsl_pool->dp_bptree_obj, B_FALSE, count_block_cb, 2742 &zcb, NULL)); 2743 } 2744 2745 if (dump_opt['c'] > 1) 2746 flags |= TRAVERSE_PREFETCH_DATA; 2747 2748 zcb.zcb_totalasize = metaslab_class_get_alloc(spa_normal_class(spa)); 2749 zcb.zcb_start = zcb.zcb_lastprint = gethrtime(); 2750 zcb.zcb_haderrors |= traverse_pool(spa, 0, flags, zdb_blkptr_cb, &zcb); 2751 2752 /* 2753 * If we've traversed the data blocks then we need to wait for those 2754 * I/Os to complete. We leverage "The Godfather" zio to wait on 2755 * all async I/Os to complete. 2756 */ 2757 if (dump_opt['c']) { 2758 for (int i = 0; i < max_ncpus; i++) { 2759 (void) zio_wait(spa->spa_async_zio_root[i]); 2760 spa->spa_async_zio_root[i] = zio_root(spa, NULL, NULL, 2761 ZIO_FLAG_CANFAIL | ZIO_FLAG_SPECULATIVE | 2762 ZIO_FLAG_GODFATHER); 2763 } 2764 } 2765 2766 if (zcb.zcb_haderrors) { 2767 (void) printf("\nError counts:\n\n"); 2768 (void) printf("\t%5s %s\n", "errno", "count"); 2769 for (int e = 0; e < 256; e++) { 2770 if (zcb.zcb_errors[e] != 0) { 2771 (void) printf("\t%5d %llu\n", 2772 e, (u_longlong_t)zcb.zcb_errors[e]); 2773 } 2774 } 2775 } 2776 2777 /* 2778 * Report any leaked segments. 2779 */ 2780 zdb_leak_fini(spa); 2781 2782 tzb = &zcb.zcb_type[ZB_TOTAL][ZDB_OT_TOTAL]; 2783 2784 norm_alloc = metaslab_class_get_alloc(spa_normal_class(spa)); 2785 norm_space = metaslab_class_get_space(spa_normal_class(spa)); 2786 2787 total_alloc = norm_alloc + metaslab_class_get_alloc(spa_log_class(spa)); 2788 total_found = tzb->zb_asize - zcb.zcb_dedup_asize; 2789 2790 if (total_found == total_alloc) { 2791 if (!dump_opt['L']) 2792 (void) printf("\n\tNo leaks (block sum matches space" 2793 " maps exactly)\n"); 2794 } else { 2795 (void) printf("block traversal size %llu != alloc %llu " 2796 "(%s %lld)\n", 2797 (u_longlong_t)total_found, 2798 (u_longlong_t)total_alloc, 2799 (dump_opt['L']) ? "unreachable" : "leaked", 2800 (longlong_t)(total_alloc - total_found)); 2801 leaks = B_TRUE; 2802 } 2803 2804 if (tzb->zb_count == 0) 2805 return (2); 2806 2807 (void) printf("\n"); 2808 (void) printf("\tbp count: %10llu\n", 2809 (u_longlong_t)tzb->zb_count); 2810 (void) printf("\tganged count: %10llu\n", 2811 (longlong_t)tzb->zb_gangs); 2812 (void) printf("\tbp logical: %10llu avg: %6llu\n", 2813 (u_longlong_t)tzb->zb_lsize, 2814 (u_longlong_t)(tzb->zb_lsize / tzb->zb_count)); 2815 (void) printf("\tbp physical: %10llu avg:" 2816 " %6llu compression: %6.2f\n", 2817 (u_longlong_t)tzb->zb_psize, 2818 (u_longlong_t)(tzb->zb_psize / tzb->zb_count), 2819 (double)tzb->zb_lsize / tzb->zb_psize); 2820 (void) printf("\tbp allocated: %10llu avg:" 2821 " %6llu compression: %6.2f\n", 2822 (u_longlong_t)tzb->zb_asize, 2823 (u_longlong_t)(tzb->zb_asize / tzb->zb_count), 2824 (double)tzb->zb_lsize / tzb->zb_asize); 2825 (void) printf("\tbp deduped: %10llu ref>1:" 2826 " %6llu deduplication: %6.2f\n", 2827 (u_longlong_t)zcb.zcb_dedup_asize, 2828 (u_longlong_t)zcb.zcb_dedup_blocks, 2829 (double)zcb.zcb_dedup_asize / tzb->zb_asize + 1.0); 2830 (void) printf("\tSPA allocated: %10llu used: %5.2f%%\n", 2831 (u_longlong_t)norm_alloc, 100.0 * norm_alloc / norm_space); 2832 2833 for (bp_embedded_type_t i = 0; i < NUM_BP_EMBEDDED_TYPES; i++) { 2834 if (zcb.zcb_embedded_blocks[i] == 0) 2835 continue; 2836 (void) printf("\n"); 2837 (void) printf("\tadditional, non-pointer bps of type %u: " 2838 "%10llu\n", 2839 i, (u_longlong_t)zcb.zcb_embedded_blocks[i]); 2840 2841 if (dump_opt['b'] >= 3) { 2842 (void) printf("\t number of (compressed) bytes: " 2843 "number of bps\n"); 2844 dump_histogram(zcb.zcb_embedded_histogram[i], 2845 sizeof (zcb.zcb_embedded_histogram[i]) / 2846 sizeof (zcb.zcb_embedded_histogram[i][0]), 0); 2847 } 2848 } 2849 2850 if (tzb->zb_ditto_samevdev != 0) { 2851 (void) printf("\tDittoed blocks on same vdev: %llu\n", 2852 (longlong_t)tzb->zb_ditto_samevdev); 2853 } 2854 2855 if (dump_opt['b'] >= 2) { 2856 int l, t, level; 2857 (void) printf("\nBlocks\tLSIZE\tPSIZE\tASIZE" 2858 "\t avg\t comp\t%%Total\tType\n"); 2859 2860 for (t = 0; t <= ZDB_OT_TOTAL; t++) { 2861 char csize[32], lsize[32], psize[32], asize[32]; 2862 char avg[32], gang[32]; 2863 char *typename; 2864 2865 if (t < DMU_OT_NUMTYPES) 2866 typename = dmu_ot[t].ot_name; 2867 else 2868 typename = zdb_ot_extname[t - DMU_OT_NUMTYPES]; 2869 2870 if (zcb.zcb_type[ZB_TOTAL][t].zb_asize == 0) { 2871 (void) printf("%6s\t%5s\t%5s\t%5s" 2872 "\t%5s\t%5s\t%6s\t%s\n", 2873 "-", 2874 "-", 2875 "-", 2876 "-", 2877 "-", 2878 "-", 2879 "-", 2880 typename); 2881 continue; 2882 } 2883 2884 for (l = ZB_TOTAL - 1; l >= -1; l--) { 2885 level = (l == -1 ? ZB_TOTAL : l); 2886 zb = &zcb.zcb_type[level][t]; 2887 2888 if (zb->zb_asize == 0) 2889 continue; 2890 2891 if (dump_opt['b'] < 3 && level != ZB_TOTAL) 2892 continue; 2893 2894 if (level == 0 && zb->zb_asize == 2895 zcb.zcb_type[ZB_TOTAL][t].zb_asize) 2896 continue; 2897 2898 zdb_nicenum(zb->zb_count, csize); 2899 zdb_nicenum(zb->zb_lsize, lsize); 2900 zdb_nicenum(zb->zb_psize, psize); 2901 zdb_nicenum(zb->zb_asize, asize); 2902 zdb_nicenum(zb->zb_asize / zb->zb_count, avg); 2903 zdb_nicenum(zb->zb_gangs, gang); 2904 2905 (void) printf("%6s\t%5s\t%5s\t%5s\t%5s" 2906 "\t%5.2f\t%6.2f\t", 2907 csize, lsize, psize, asize, avg, 2908 (double)zb->zb_lsize / zb->zb_psize, 2909 100.0 * zb->zb_asize / tzb->zb_asize); 2910 2911 if (level == ZB_TOTAL) 2912 (void) printf("%s\n", typename); 2913 else 2914 (void) printf(" L%d %s\n", 2915 level, typename); 2916 2917 if (dump_opt['b'] >= 3 && zb->zb_gangs > 0) { 2918 (void) printf("\t number of ganged " 2919 "blocks: %s\n", gang); 2920 } 2921 2922 if (dump_opt['b'] >= 4) { 2923 (void) printf("psize " 2924 "(in 512-byte sectors): " 2925 "number of blocks\n"); 2926 dump_histogram(zb->zb_psize_histogram, 2927 PSIZE_HISTO_SIZE, 0); 2928 } 2929 } 2930 } 2931 } 2932 2933 (void) printf("\n"); 2934 2935 if (leaks) 2936 return (2); 2937 2938 if (zcb.zcb_haderrors) 2939 return (3); 2940 2941 return (0); 2942} 2943 2944typedef struct zdb_ddt_entry { 2945 ddt_key_t zdde_key; 2946 uint64_t zdde_ref_blocks; 2947 uint64_t zdde_ref_lsize; 2948 uint64_t zdde_ref_psize; 2949 uint64_t zdde_ref_dsize; 2950 avl_node_t zdde_node; 2951} zdb_ddt_entry_t; 2952 2953/* ARGSUSED */ 2954static int 2955zdb_ddt_add_cb(spa_t *spa, zilog_t *zilog, const blkptr_t *bp, 2956 const zbookmark_phys_t *zb, const dnode_phys_t *dnp, void *arg) 2957{ 2958 avl_tree_t *t = arg; 2959 avl_index_t where; 2960 zdb_ddt_entry_t *zdde, zdde_search; 2961 2962 if (bp == NULL || BP_IS_HOLE(bp) || BP_IS_EMBEDDED(bp)) 2963 return (0); 2964 2965 if (dump_opt['S'] > 1 && zb->zb_level == ZB_ROOT_LEVEL) { 2966 (void) printf("traversing objset %llu, %llu objects, " 2967 "%lu blocks so far\n", 2968 (u_longlong_t)zb->zb_objset, 2969 (u_longlong_t)BP_GET_FILL(bp), 2970 avl_numnodes(t)); 2971 } 2972 2973 if (BP_IS_HOLE(bp) || BP_GET_CHECKSUM(bp) == ZIO_CHECKSUM_OFF || 2974 BP_GET_LEVEL(bp) > 0 || DMU_OT_IS_METADATA(BP_GET_TYPE(bp))) 2975 return (0); 2976 2977 ddt_key_fill(&zdde_search.zdde_key, bp); 2978 2979 zdde = avl_find(t, &zdde_search, &where); 2980 2981 if (zdde == NULL) { 2982 zdde = umem_zalloc(sizeof (*zdde), UMEM_NOFAIL); 2983 zdde->zdde_key = zdde_search.zdde_key; 2984 avl_insert(t, zdde, where); 2985 } 2986 2987 zdde->zdde_ref_blocks += 1; 2988 zdde->zdde_ref_lsize += BP_GET_LSIZE(bp); 2989 zdde->zdde_ref_psize += BP_GET_PSIZE(bp); 2990 zdde->zdde_ref_dsize += bp_get_dsize_sync(spa, bp); 2991 2992 return (0); 2993} 2994 2995static void 2996dump_simulated_ddt(spa_t *spa) 2997{ 2998 avl_tree_t t; 2999 void *cookie = NULL; 3000 zdb_ddt_entry_t *zdde; 3001 ddt_histogram_t ddh_total = { 0 }; 3002 ddt_stat_t dds_total = { 0 }; 3003 3004 avl_create(&t, ddt_entry_compare, 3005 sizeof (zdb_ddt_entry_t), offsetof(zdb_ddt_entry_t, zdde_node)); 3006 3007 spa_config_enter(spa, SCL_CONFIG, FTAG, RW_READER); 3008 3009 (void) traverse_pool(spa, 0, TRAVERSE_PRE | TRAVERSE_PREFETCH_METADATA, 3010 zdb_ddt_add_cb, &t); 3011 3012 spa_config_exit(spa, SCL_CONFIG, FTAG); 3013 3014 while ((zdde = avl_destroy_nodes(&t, &cookie)) != NULL) { 3015 ddt_stat_t dds; 3016 uint64_t refcnt = zdde->zdde_ref_blocks; 3017 ASSERT(refcnt != 0); 3018 3019 dds.dds_blocks = zdde->zdde_ref_blocks / refcnt; 3020 dds.dds_lsize = zdde->zdde_ref_lsize / refcnt; 3021 dds.dds_psize = zdde->zdde_ref_psize / refcnt; 3022 dds.dds_dsize = zdde->zdde_ref_dsize / refcnt; 3023 3024 dds.dds_ref_blocks = zdde->zdde_ref_blocks; 3025 dds.dds_ref_lsize = zdde->zdde_ref_lsize; 3026 dds.dds_ref_psize = zdde->zdde_ref_psize; 3027 dds.dds_ref_dsize = zdde->zdde_ref_dsize; 3028 3029 ddt_stat_add(&ddh_total.ddh_stat[highbit64(refcnt) - 1], 3030 &dds, 0); 3031 3032 umem_free(zdde, sizeof (*zdde)); 3033 } 3034 3035 avl_destroy(&t); 3036 3037 ddt_histogram_stat(&dds_total, &ddh_total); 3038 3039 (void) printf("Simulated DDT histogram:\n"); 3040 3041 zpool_dump_ddt(&dds_total, &ddh_total); 3042 3043 dump_dedup_ratio(&dds_total); 3044} 3045 3046static void 3047dump_zpool(spa_t *spa) 3048{ 3049 dsl_pool_t *dp = spa_get_dsl(spa); 3050 int rc = 0; 3051 3052 if (dump_opt['S']) { 3053 dump_simulated_ddt(spa); 3054 return; 3055 } 3056 3057 if (!dump_opt['e'] && dump_opt['C'] > 1) { 3058 (void) printf("\nCached configuration:\n"); 3059 dump_nvlist(spa->spa_config, 8); 3060 } 3061 3062 if (dump_opt['C']) 3063 dump_config(spa); 3064 3065 if (dump_opt['u']) 3066 dump_uberblock(&spa->spa_uberblock, "\nUberblock:\n", "\n"); 3067 3068 if (dump_opt['D']) 3069 dump_all_ddts(spa); 3070 3071 if (dump_opt['d'] > 2 || dump_opt['m']) 3072 dump_metaslabs(spa); 3073 if (dump_opt['M']) 3074 dump_metaslab_groups(spa); 3075 3076 if (dump_opt['d'] || dump_opt['i']) { 3077 dump_dir(dp->dp_meta_objset); 3078 if (dump_opt['d'] >= 3) { 3079 dump_full_bpobj(&spa->spa_deferred_bpobj, 3080 "Deferred frees", 0); 3081 if (spa_version(spa) >= SPA_VERSION_DEADLISTS) { 3082 dump_full_bpobj( 3083 &spa->spa_dsl_pool->dp_free_bpobj, 3084 "Pool snapshot frees", 0); 3085 } 3086 3087 if (spa_feature_is_active(spa, 3088 SPA_FEATURE_ASYNC_DESTROY)) { 3089 dump_bptree(spa->spa_meta_objset, 3090 spa->spa_dsl_pool->dp_bptree_obj, 3091 "Pool dataset frees"); 3092 } 3093 dump_dtl(spa->spa_root_vdev, 0); 3094 } 3095 (void) dmu_objset_find(spa_name(spa), dump_one_dir, 3096 NULL, DS_FIND_SNAPSHOTS | DS_FIND_CHILDREN); 3097 3098 for (spa_feature_t f = 0; f < SPA_FEATURES; f++) { 3099 uint64_t refcount; 3100 3101 if (!(spa_feature_table[f].fi_flags & 3102 ZFEATURE_FLAG_PER_DATASET)) { 3103 ASSERT0(dataset_feature_count[f]); 3104 continue; 3105 } 3106 (void) feature_get_refcount(spa, 3107 &spa_feature_table[f], &refcount); 3108 if (dataset_feature_count[f] != refcount) { 3109 (void) printf("%s feature refcount mismatch: " 3110 "%lld datasets != %lld refcount\n", 3111 spa_feature_table[f].fi_uname, 3112 (longlong_t)dataset_feature_count[f], 3113 (longlong_t)refcount); 3114 rc = 2; 3115 } else { 3116 (void) printf("Verified %s feature refcount " 3117 "of %llu is correct\n", 3118 spa_feature_table[f].fi_uname, 3119 (longlong_t)refcount); 3120 } 3121 } 3122 } 3123 if (rc == 0 && (dump_opt['b'] || dump_opt['c'])) 3124 rc = dump_block_stats(spa); 3125 3126 if (rc == 0) 3127 rc = verify_spacemap_refcounts(spa); 3128 3129 if (dump_opt['s']) 3130 show_pool_stats(spa); 3131 3132 if (dump_opt['h']) 3133 dump_history(spa); 3134 3135 if (rc != 0) { 3136 dump_debug_buffer(); 3137 exit(rc); 3138 } 3139} 3140 3141#define ZDB_FLAG_CHECKSUM 0x0001 3142#define ZDB_FLAG_DECOMPRESS 0x0002 3143#define ZDB_FLAG_BSWAP 0x0004 3144#define ZDB_FLAG_GBH 0x0008 3145#define ZDB_FLAG_INDIRECT 0x0010 3146#define ZDB_FLAG_PHYS 0x0020 3147#define ZDB_FLAG_RAW 0x0040 3148#define ZDB_FLAG_PRINT_BLKPTR 0x0080 3149 3150int flagbits[256]; 3151 3152static void 3153zdb_print_blkptr(blkptr_t *bp, int flags) 3154{ 3155 char blkbuf[BP_SPRINTF_LEN]; 3156 3157 if (flags & ZDB_FLAG_BSWAP) 3158 byteswap_uint64_array((void *)bp, sizeof (blkptr_t)); 3159 3160 snprintf_blkptr(blkbuf, sizeof (blkbuf), bp); 3161 (void) printf("%s\n", blkbuf); 3162} 3163 3164static void 3165zdb_dump_indirect(blkptr_t *bp, int nbps, int flags) 3166{ 3167 int i; 3168 3169 for (i = 0; i < nbps; i++) 3170 zdb_print_blkptr(&bp[i], flags); 3171} 3172 3173static void 3174zdb_dump_gbh(void *buf, int flags) 3175{ 3176 zdb_dump_indirect((blkptr_t *)buf, SPA_GBH_NBLKPTRS, flags); 3177} 3178 3179static void 3180zdb_dump_block_raw(void *buf, uint64_t size, int flags) 3181{ 3182 if (flags & ZDB_FLAG_BSWAP) 3183 byteswap_uint64_array(buf, size); 3184 (void) write(1, buf, size); 3185} 3186 3187static void 3188zdb_dump_block(char *label, void *buf, uint64_t size, int flags) 3189{ 3190 uint64_t *d = (uint64_t *)buf; 3191 int nwords = size / sizeof (uint64_t); 3192 int do_bswap = !!(flags & ZDB_FLAG_BSWAP); 3193 int i, j; 3194 char *hdr, *c; 3195 3196 3197 if (do_bswap) 3198 hdr = " 7 6 5 4 3 2 1 0 f e d c b a 9 8"; 3199 else 3200 hdr = " 0 1 2 3 4 5 6 7 8 9 a b c d e f"; 3201 3202 (void) printf("\n%s\n%6s %s 0123456789abcdef\n", label, "", hdr); 3203 3204 for (i = 0; i < nwords; i += 2) { 3205 (void) printf("%06llx: %016llx %016llx ", 3206 (u_longlong_t)(i * sizeof (uint64_t)), 3207 (u_longlong_t)(do_bswap ? BSWAP_64(d[i]) : d[i]), 3208 (u_longlong_t)(do_bswap ? BSWAP_64(d[i + 1]) : d[i + 1])); 3209 3210 c = (char *)&d[i]; 3211 for (j = 0; j < 2 * sizeof (uint64_t); j++) 3212 (void) printf("%c", isprint(c[j]) ? c[j] : '.'); 3213 (void) printf("\n"); 3214 } 3215} 3216 3217/* 3218 * There are two acceptable formats: 3219 * leaf_name - For example: c1t0d0 or /tmp/ztest.0a 3220 * child[.child]* - For example: 0.1.1 3221 * 3222 * The second form can be used to specify arbitrary vdevs anywhere 3223 * in the heirarchy. For example, in a pool with a mirror of 3224 * RAID-Zs, you can specify either RAID-Z vdev with 0.0 or 0.1 . 3225 */ 3226static vdev_t * 3227zdb_vdev_lookup(vdev_t *vdev, char *path) 3228{ 3229 char *s, *p, *q; 3230 int i; 3231 3232 if (vdev == NULL) 3233 return (NULL); 3234 3235 /* First, assume the x.x.x.x format */ 3236 i = (int)strtoul(path, &s, 10); 3237 if (s == path || (s && *s != '.' && *s != '\0')) 3238 goto name; 3239 if (i < 0 || i >= vdev->vdev_children) 3240 return (NULL); 3241 3242 vdev = vdev->vdev_child[i]; 3243 if (*s == '\0') 3244 return (vdev); 3245 return (zdb_vdev_lookup(vdev, s+1)); 3246 3247name: 3248 for (i = 0; i < vdev->vdev_children; i++) { 3249 vdev_t *vc = vdev->vdev_child[i]; 3250 3251 if (vc->vdev_path == NULL) { 3252 vc = zdb_vdev_lookup(vc, path); 3253 if (vc == NULL) 3254 continue; 3255 else 3256 return (vc); 3257 } 3258 3259 p = strrchr(vc->vdev_path, '/'); 3260 p = p ? p + 1 : vc->vdev_path; 3261 q = &vc->vdev_path[strlen(vc->vdev_path) - 2]; 3262 3263 if (strcmp(vc->vdev_path, path) == 0) 3264 return (vc); 3265 if (strcmp(p, path) == 0) 3266 return (vc); 3267 if (strcmp(q, "s0") == 0 && strncmp(p, path, q - p) == 0) 3268 return (vc); 3269 } 3270 3271 return (NULL); 3272} 3273 3274/* ARGSUSED */ 3275static int 3276random_get_pseudo_bytes_cb(void *buf, size_t len, void *unused) 3277{ 3278 return (random_get_pseudo_bytes(buf, len)); 3279} 3280 3281/* 3282 * Read a block from a pool and print it out. The syntax of the 3283 * block descriptor is: 3284 * 3285 * pool:vdev_specifier:offset:size[:flags] 3286 * 3287 * pool - The name of the pool you wish to read from 3288 * vdev_specifier - Which vdev (see comment for zdb_vdev_lookup) 3289 * offset - offset, in hex, in bytes 3290 * size - Amount of data to read, in hex, in bytes 3291 * flags - A string of characters specifying options 3292 * b: Decode a blkptr at given offset within block 3293 * *c: Calculate and display checksums 3294 * d: Decompress data before dumping 3295 * e: Byteswap data before dumping 3296 * g: Display data as a gang block header 3297 * i: Display as an indirect block 3298 * p: Do I/O to physical offset 3299 * r: Dump raw data to stdout 3300 * 3301 * * = not yet implemented 3302 */ 3303static void 3304zdb_read_block(char *thing, spa_t *spa) 3305{ 3306 blkptr_t blk, *bp = &blk; 3307 dva_t *dva = bp->blk_dva; 3308 int flags = 0; 3309 uint64_t offset = 0, size = 0, psize = 0, lsize = 0, blkptr_offset = 0; 3310 zio_t *zio; 3311 vdev_t *vd; 3312 abd_t *pabd; 3313 void *lbuf, *buf; 3314 char *s, *p, *dup, *vdev, *flagstr; 3315 int i, error; 3316 3317 dup = strdup(thing); 3318 s = strtok(dup, ":"); 3319 vdev = s ? s : ""; 3320 s = strtok(NULL, ":"); 3321 offset = strtoull(s ? s : "", NULL, 16); 3322 s = strtok(NULL, ":"); 3323 size = strtoull(s ? s : "", NULL, 16); 3324 s = strtok(NULL, ":"); 3325 flagstr = s ? s : ""; 3326 3327 s = NULL; 3328 if (size == 0) 3329 s = "size must not be zero"; 3330 if (!IS_P2ALIGNED(size, DEV_BSIZE)) 3331 s = "size must be a multiple of sector size"; 3332 if (!IS_P2ALIGNED(offset, DEV_BSIZE)) 3333 s = "offset must be a multiple of sector size"; 3334 if (s) { 3335 (void) printf("Invalid block specifier: %s - %s\n", thing, s); 3336 free(dup); 3337 return; 3338 } 3339 3340 for (s = strtok(flagstr, ":"); s; s = strtok(NULL, ":")) { 3341 for (i = 0; flagstr[i]; i++) { 3342 int bit = flagbits[(uchar_t)flagstr[i]]; 3343 3344 if (bit == 0) { 3345 (void) printf("***Invalid flag: %c\n", 3346 flagstr[i]); 3347 continue; 3348 } 3349 flags |= bit; 3350 3351 /* If it's not something with an argument, keep going */ 3352 if ((bit & (ZDB_FLAG_CHECKSUM | 3353 ZDB_FLAG_PRINT_BLKPTR)) == 0) 3354 continue; 3355 3356 p = &flagstr[i + 1]; 3357 if (bit == ZDB_FLAG_PRINT_BLKPTR) 3358 blkptr_offset = strtoull(p, &p, 16); 3359 if (*p != ':' && *p != '\0') { 3360 (void) printf("***Invalid flag arg: '%s'\n", s); 3361 free(dup); 3362 return; 3363 } 3364 i += p - &flagstr[i + 1]; /* skip over the number */ 3365 } 3366 } 3367 3368 vd = zdb_vdev_lookup(spa->spa_root_vdev, vdev); 3369 if (vd == NULL) { 3370 (void) printf("***Invalid vdev: %s\n", vdev); 3371 free(dup); 3372 return; 3373 } else { 3374 if (vd->vdev_path) 3375 (void) fprintf(stderr, "Found vdev: %s\n", 3376 vd->vdev_path); 3377 else 3378 (void) fprintf(stderr, "Found vdev type: %s\n", 3379 vd->vdev_ops->vdev_op_type); 3380 } 3381 3382 psize = size; 3383 lsize = size; 3384 3385 pabd = abd_alloc_linear(SPA_MAXBLOCKSIZE, B_FALSE); 3386 lbuf = umem_alloc(SPA_MAXBLOCKSIZE, UMEM_NOFAIL); 3387 3388 BP_ZERO(bp); 3389 3390 DVA_SET_VDEV(&dva[0], vd->vdev_id); 3391 DVA_SET_OFFSET(&dva[0], offset); 3392 DVA_SET_GANG(&dva[0], !!(flags & ZDB_FLAG_GBH)); 3393 DVA_SET_ASIZE(&dva[0], vdev_psize_to_asize(vd, psize)); 3394 3395 BP_SET_BIRTH(bp, TXG_INITIAL, TXG_INITIAL); 3396 3397 BP_SET_LSIZE(bp, lsize); 3398 BP_SET_PSIZE(bp, psize); 3399 BP_SET_COMPRESS(bp, ZIO_COMPRESS_OFF); 3400 BP_SET_CHECKSUM(bp, ZIO_CHECKSUM_OFF); 3401 BP_SET_TYPE(bp, DMU_OT_NONE); 3402 BP_SET_LEVEL(bp, 0); 3403 BP_SET_DEDUP(bp, 0); 3404 BP_SET_BYTEORDER(bp, ZFS_HOST_BYTEORDER); 3405 3406 spa_config_enter(spa, SCL_STATE, FTAG, RW_READER); 3407 zio = zio_root(spa, NULL, NULL, 0); 3408 3409 if (vd == vd->vdev_top) { 3410 /* 3411 * Treat this as a normal block read. 3412 */ 3413 zio_nowait(zio_read(zio, spa, bp, pabd, psize, NULL, NULL, 3414 ZIO_PRIORITY_SYNC_READ, 3415 ZIO_FLAG_CANFAIL | ZIO_FLAG_RAW, NULL)); 3416 } else { 3417 /* 3418 * Treat this as a vdev child I/O. 3419 */ 3420 zio_nowait(zio_vdev_child_io(zio, bp, vd, offset, pabd, 3421 psize, ZIO_TYPE_READ, ZIO_PRIORITY_SYNC_READ, 3422 ZIO_FLAG_DONT_CACHE | ZIO_FLAG_DONT_QUEUE | 3423 ZIO_FLAG_DONT_PROPAGATE | ZIO_FLAG_DONT_RETRY | 3424 ZIO_FLAG_CANFAIL | ZIO_FLAG_RAW, NULL, NULL)); 3425 } 3426 3427 error = zio_wait(zio); 3428 spa_config_exit(spa, SCL_STATE, FTAG); 3429 3430 if (error) { 3431 (void) printf("Read of %s failed, error: %d\n", thing, error); 3432 goto out; 3433 } 3434 3435 if (flags & ZDB_FLAG_DECOMPRESS) { 3436 /* 3437 * We don't know how the data was compressed, so just try 3438 * every decompress function at every inflated blocksize. 3439 */ 3440 enum zio_compress c; 3441 void *pbuf2 = umem_alloc(SPA_MAXBLOCKSIZE, UMEM_NOFAIL); 3442 void *lbuf2 = umem_alloc(SPA_MAXBLOCKSIZE, UMEM_NOFAIL); 3443 3444 abd_copy_to_buf(pbuf2, pabd, psize); 3445 3446 VERIFY0(abd_iterate_func(pabd, psize, SPA_MAXBLOCKSIZE - psize, 3447 random_get_pseudo_bytes_cb, NULL)); 3448 3449 VERIFY0(random_get_pseudo_bytes((uint8_t *)pbuf2 + psize, 3450 SPA_MAXBLOCKSIZE - psize)); 3451 3452 for (lsize = SPA_MAXBLOCKSIZE; lsize > psize; 3453 lsize -= SPA_MINBLOCKSIZE) { 3454 for (c = 0; c < ZIO_COMPRESS_FUNCTIONS; c++) { 3455 if (zio_decompress_data(c, pabd, 3456 lbuf, psize, lsize) == 0 && 3457 zio_decompress_data_buf(c, pbuf2, 3458 lbuf2, psize, lsize) == 0 && 3459 bcmp(lbuf, lbuf2, lsize) == 0) 3460 break; 3461 } 3462 if (c != ZIO_COMPRESS_FUNCTIONS) 3463 break; 3464 lsize -= SPA_MINBLOCKSIZE; 3465 } 3466 3467 umem_free(pbuf2, SPA_MAXBLOCKSIZE); 3468 umem_free(lbuf2, SPA_MAXBLOCKSIZE); 3469 3470 if (lsize <= psize) { 3471 (void) printf("Decompress of %s failed\n", thing); 3472 goto out; 3473 } 3474 buf = lbuf; 3475 size = lsize; 3476 } else { 3477 buf = abd_to_buf(pabd); 3478 size = psize; 3479 } 3480 3481 if (flags & ZDB_FLAG_PRINT_BLKPTR) 3482 zdb_print_blkptr((blkptr_t *)(void *) 3483 ((uintptr_t)buf + (uintptr_t)blkptr_offset), flags); 3484 else if (flags & ZDB_FLAG_RAW) 3485 zdb_dump_block_raw(buf, size, flags); 3486 else if (flags & ZDB_FLAG_INDIRECT) 3487 zdb_dump_indirect((blkptr_t *)buf, size / sizeof (blkptr_t), 3488 flags); 3489 else if (flags & ZDB_FLAG_GBH) 3490 zdb_dump_gbh(buf, flags); 3491 else 3492 zdb_dump_block(thing, buf, size, flags); 3493 3494out: 3495 abd_free(pabd); 3496 umem_free(lbuf, SPA_MAXBLOCKSIZE); 3497 free(dup); 3498} 3499 3500static boolean_t 3501pool_match(nvlist_t *cfg, char *tgt) 3502{ 3503 uint64_t v, guid = strtoull(tgt, NULL, 0); 3504 char *s; 3505 3506 if (guid != 0) { 3507 if (nvlist_lookup_uint64(cfg, ZPOOL_CONFIG_POOL_GUID, &v) == 0) 3508 return (v == guid); 3509 } else { 3510 if (nvlist_lookup_string(cfg, ZPOOL_CONFIG_POOL_NAME, &s) == 0) 3511 return (strcmp(s, tgt) == 0); 3512 } 3513 return (B_FALSE); 3514} 3515 3516static char * 3517find_zpool(char **target, nvlist_t **configp, int dirc, char **dirv) 3518{ 3519 nvlist_t *pools; 3520 nvlist_t *match = NULL; 3521 char *name = NULL; 3522 char *sepp = NULL; 3523 char sep = '\0'; 3524 int count = 0; 3525 importargs_t args = { 0 }; 3526 3527 args.paths = dirc; 3528 args.path = dirv; 3529 args.can_be_active = B_TRUE; 3530 3531 if ((sepp = strpbrk(*target, "/@")) != NULL) { 3532 sep = *sepp; 3533 *sepp = '\0'; 3534 } 3535 3536 pools = zpool_search_import(g_zfs, &args); 3537 3538 if (pools != NULL) { 3539 nvpair_t *elem = NULL; 3540 while ((elem = nvlist_next_nvpair(pools, elem)) != NULL) { 3541 verify(nvpair_value_nvlist(elem, configp) == 0); 3542 if (pool_match(*configp, *target)) { 3543 count++; 3544 if (match != NULL) { 3545 /* print previously found config */ 3546 if (name != NULL) { 3547 (void) printf("%s\n", name); 3548 dump_nvlist(match, 8); 3549 name = NULL; 3550 } 3551 (void) printf("%s\n", 3552 nvpair_name(elem)); 3553 dump_nvlist(*configp, 8); 3554 } else { 3555 match = *configp; 3556 name = nvpair_name(elem); 3557 } 3558 } 3559 } 3560 } 3561 if (count > 1) 3562 (void) fatal("\tMatched %d pools - use pool GUID " 3563 "instead of pool name or \n" 3564 "\tpool name part of a dataset name to select pool", count); 3565 3566 if (sepp) 3567 *sepp = sep; 3568 /* 3569 * If pool GUID was specified for pool id, replace it with pool name 3570 */ 3571 if (name && (strstr(*target, name) != *target)) { 3572 int sz = 1 + strlen(name) + ((sepp) ? strlen(sepp) : 0); 3573 3574 *target = umem_alloc(sz, UMEM_NOFAIL); 3575 (void) snprintf(*target, sz, "%s%s", name, sepp ? sepp : ""); 3576 } 3577 3578 *configp = name ? match : NULL; 3579 3580 return (name); 3581} 3582 3583int 3584main(int argc, char **argv) 3585{ 3586 int i, c; 3587 struct rlimit rl = { 1024, 1024 }; 3588 spa_t *spa = NULL; 3589 objset_t *os = NULL; 3590 int dump_all = 1; 3591 int verbose = 0; 3592 int error = 0; 3593 char **searchdirs = NULL; 3594 int nsearch = 0; 3595 char *target; 3596 nvlist_t *policy = NULL; 3597 uint64_t max_txg = UINT64_MAX; 3598 int rewind = ZPOOL_NEVER_REWIND; 3599 char *spa_config_path_env; 3600 boolean_t target_is_spa = B_TRUE; 3601 3602 (void) setrlimit(RLIMIT_NOFILE, &rl); 3603 (void) enable_extended_FILE_stdio(-1, -1); 3604 3605 dprintf_setup(&argc, argv); 3606 3607 /* 3608 * If there is an environment variable SPA_CONFIG_PATH it overrides 3609 * default spa_config_path setting. If -U flag is specified it will 3610 * override this environment variable settings once again. 3611 */ 3612 spa_config_path_env = getenv("SPA_CONFIG_PATH"); 3613 if (spa_config_path_env != NULL) 3614 spa_config_path = spa_config_path_env; 3615 3616 while ((c = getopt(argc, argv, 3617 "bcdhilmMI:suCDRSAFLXx:evp:t:U:PG")) != -1) { 3618 switch (c) { 3619 case 'b': 3620 case 'c': 3621 case 'd': 3622 case 'h': 3623 case 'i': 3624 case 'l': 3625 case 'm': 3626 case 's': 3627 case 'u': 3628 case 'C': 3629 case 'D': 3630 case 'M': 3631 case 'R': 3632 case 'S': 3633 case 'G': 3634 dump_opt[c]++; 3635 dump_all = 0; 3636 break; 3637 case 'A': 3638 case 'F': 3639 case 'L': 3640 case 'X': 3641 case 'e': 3642 case 'P': 3643 dump_opt[c]++; 3644 break; 3645 case 'I': 3646 max_inflight = strtoull(optarg, NULL, 0); 3647 if (max_inflight == 0) { 3648 (void) fprintf(stderr, "maximum number " 3649 "of inflight I/Os must be greater " 3650 "than 0\n"); 3651 usage(); 3652 } 3653 break; 3654 case 'p': 3655 if (searchdirs == NULL) { 3656 searchdirs = umem_alloc(sizeof (char *), 3657 UMEM_NOFAIL); 3658 } else { 3659 char **tmp = umem_alloc((nsearch + 1) * 3660 sizeof (char *), UMEM_NOFAIL); 3661 bcopy(searchdirs, tmp, nsearch * 3662 sizeof (char *)); 3663 umem_free(searchdirs, 3664 nsearch * sizeof (char *)); 3665 searchdirs = tmp; 3666 } 3667 searchdirs[nsearch++] = optarg; 3668 break; 3669 case 't': 3670 max_txg = strtoull(optarg, NULL, 0); 3671 if (max_txg < TXG_INITIAL) { 3672 (void) fprintf(stderr, "incorrect txg " 3673 "specified: %s\n", optarg); 3674 usage(); 3675 } 3676 break; 3677 case 'U': 3678 spa_config_path = optarg; 3679 break; 3680 case 'v': 3681 verbose++; 3682 break; 3683 case 'x': 3684 vn_dumpdir = optarg; 3685 break; 3686 default: 3687 usage(); 3688 break; 3689 } 3690 } 3691 3692 if (!dump_opt['e'] && searchdirs != NULL) { 3693 (void) fprintf(stderr, "-p option requires use of -e\n"); 3694 usage(); 3695 } 3696 3697 /* 3698 * ZDB does not typically re-read blocks; therefore limit the ARC 3699 * to 256 MB, which can be used entirely for metadata. 3700 */ 3701 zfs_arc_max = zfs_arc_meta_limit = 256 * 1024 * 1024; 3702 3703 /* 3704 * "zdb -c" uses checksum-verifying scrub i/os which are async reads. 3705 * "zdb -b" uses traversal prefetch which uses async reads. 3706 * For good performance, let several of them be active at once. 3707 */ 3708 zfs_vdev_async_read_max_active = 10; 3709 3710 /* 3711 * Disable reference tracking for better performance. 3712 */ 3713 reference_tracking_enable = B_FALSE; 3714 3715 kernel_init(FREAD); 3716 g_zfs = libzfs_init(); 3717 if (g_zfs == NULL) 3718 fatal("Fail to initialize zfs"); 3719 3720 if (dump_all) 3721 verbose = MAX(verbose, 1); 3722 3723 for (c = 0; c < 256; c++) { 3724 if (dump_all && !strchr("elAFLRSXP", c)) 3725 dump_opt[c] = 1; 3726 if (dump_opt[c]) 3727 dump_opt[c] += verbose; 3728 } 3729 3730 aok = (dump_opt['A'] == 1) || (dump_opt['A'] > 2); 3731 zfs_recover = (dump_opt['A'] > 1); 3732 3733 argc -= optind; 3734 argv += optind; 3735 3736 if (argc < 2 && dump_opt['R']) 3737 usage(); 3738 if (argc < 1) { 3739 if (!dump_opt['e'] && dump_opt['C']) { 3740 dump_cachefile(spa_config_path); 3741 return (0); 3742 } 3743 usage(); 3744 } 3745 3746 if (dump_opt['l']) { 3747 dump_label(argv[0]); 3748 return (0); 3749 } 3750 3751 if (dump_opt['X'] || dump_opt['F']) 3752 rewind = ZPOOL_DO_REWIND | 3753 (dump_opt['X'] ? ZPOOL_EXTREME_REWIND : 0); 3754 3755 if (nvlist_alloc(&policy, NV_UNIQUE_NAME_TYPE, 0) != 0 || 3756 nvlist_add_uint64(policy, ZPOOL_REWIND_REQUEST_TXG, max_txg) != 0 || 3757 nvlist_add_uint32(policy, ZPOOL_REWIND_REQUEST, rewind) != 0) 3758 fatal("internal error: %s", strerror(ENOMEM)); 3759 3760 error = 0; 3761 target = argv[0]; 3762 3763 if (dump_opt['e']) { 3764 nvlist_t *cfg = NULL; 3765 char *name = find_zpool(&target, &cfg, nsearch, searchdirs); 3766 3767 error = ENOENT; 3768 if (name) { 3769 if (dump_opt['C'] > 1) { 3770 (void) printf("\nConfiguration for import:\n"); 3771 dump_nvlist(cfg, 8); 3772 } 3773 if (nvlist_add_nvlist(cfg, 3774 ZPOOL_REWIND_POLICY, policy) != 0) { 3775 fatal("can't open '%s': %s", 3776 target, strerror(ENOMEM)); 3777 } 3778 if ((error = spa_import(name, cfg, NULL, 3779 ZFS_IMPORT_MISSING_LOG)) != 0) { 3780 error = spa_import(name, cfg, NULL, 3781 ZFS_IMPORT_VERBATIM); 3782 } 3783 } 3784 } 3785 3786 if (strpbrk(target, "/@") != NULL) { 3787 size_t targetlen; 3788 3789 target_is_spa = B_FALSE; 3790 /* 3791 * Remove any trailing slash. Later code would get confused 3792 * by it, but we want to allow it so that "pool/" can 3793 * indicate that we want to dump the topmost filesystem, 3794 * rather than the whole pool. 3795 */ 3796 targetlen = strlen(target); 3797 if (targetlen != 0 && target[targetlen - 1] == '/') 3798 target[targetlen - 1] = '\0'; 3799 } 3800 3801 if (error == 0) { 3802 if (target_is_spa || dump_opt['R']) { 3803 error = spa_open_rewind(target, &spa, FTAG, policy, 3804 NULL); 3805 if (error) { 3806 /* 3807 * If we're missing the log device then 3808 * try opening the pool after clearing the 3809 * log state. 3810 */ 3811 mutex_enter(&spa_namespace_lock); 3812 if ((spa = spa_lookup(target)) != NULL && 3813 spa->spa_log_state == SPA_LOG_MISSING) { 3814 spa->spa_log_state = SPA_LOG_CLEAR; 3815 error = 0; 3816 } 3817 mutex_exit(&spa_namespace_lock); 3818 3819 if (!error) { 3820 error = spa_open_rewind(target, &spa, 3821 FTAG, policy, NULL); 3822 } 3823 } 3824 } else { 3825 error = dmu_objset_own(target, DMU_OST_ANY, 3826 B_TRUE, FTAG, &os); 3827 } 3828 } 3829 nvlist_free(policy); 3830 3831 if (error) 3832 fatal("can't open '%s': %s", target, strerror(error)); 3833 3834 argv++; 3835 argc--; 3836 if (!dump_opt['R']) { 3837 if (argc > 0) { 3838 zopt_objects = argc; 3839 zopt_object = calloc(zopt_objects, sizeof (uint64_t)); 3840 for (i = 0; i < zopt_objects; i++) { 3841 errno = 0; 3842 zopt_object[i] = strtoull(argv[i], NULL, 0); 3843 if (zopt_object[i] == 0 && errno != 0) 3844 fatal("bad number %s: %s", 3845 argv[i], strerror(errno)); 3846 } 3847 } 3848 if (os != NULL) { 3849 dump_dir(os); 3850 } else if (zopt_objects > 0 && !dump_opt['m']) { 3851 dump_dir(spa->spa_meta_objset); 3852 } else { 3853 dump_zpool(spa); 3854 } 3855 } else { 3856 flagbits['b'] = ZDB_FLAG_PRINT_BLKPTR; 3857 flagbits['c'] = ZDB_FLAG_CHECKSUM; 3858 flagbits['d'] = ZDB_FLAG_DECOMPRESS; 3859 flagbits['e'] = ZDB_FLAG_BSWAP; 3860 flagbits['g'] = ZDB_FLAG_GBH; 3861 flagbits['i'] = ZDB_FLAG_INDIRECT; 3862 flagbits['p'] = ZDB_FLAG_PHYS; 3863 flagbits['r'] = ZDB_FLAG_RAW; 3864 3865 for (i = 0; i < argc; i++) 3866 zdb_read_block(argv[i], spa); 3867 } 3868 3869 (os != NULL) ? dmu_objset_disown(os, FTAG) : spa_close(spa, FTAG); 3870 3871 fuid_table_destroy(); 3872 sa_loaded = B_FALSE; 3873 3874 dump_debug_buffer(); 3875 3876 libzfs_fini(g_zfs); 3877 kernel_fini(); 3878 3879 return (0); 3880} 3881