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