zdb.c revision 307266
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, 2015 by Delphix. All rights reserved. 25 * Copyright (c) 2014 Integros [integros.com] 26 */ 27 28#include <stdio.h> 29#include <unistd.h> 30#include <stdio_ext.h> 31#include <stdlib.h> 32#include <ctype.h> 33#include <sys/zfs_context.h> 34#include <sys/spa.h> 35#include <sys/spa_impl.h> 36#include <sys/dmu.h> 37#include <sys/zap.h> 38#include <sys/fs/zfs.h> 39#include <sys/zfs_znode.h> 40#include <sys/zfs_sa.h> 41#include <sys/sa.h> 42#include <sys/sa_impl.h> 43#include <sys/vdev.h> 44#include <sys/vdev_impl.h> 45#include <sys/metaslab_impl.h> 46#include <sys/dmu_objset.h> 47#include <sys/dsl_dir.h> 48#include <sys/dsl_dataset.h> 49#include <sys/dsl_pool.h> 50#include <sys/dbuf.h> 51#include <sys/zil.h> 52#include <sys/zil_impl.h> 53#include <sys/stat.h> 54#include <sys/resource.h> 55#include <sys/dmu_traverse.h> 56#include <sys/zio_checksum.h> 57#include <sys/zio_compress.h> 58#include <sys/zfs_fuid.h> 59#include <sys/arc.h> 60#include <sys/ddt.h> 61#include <sys/zfeature.h> 62#include <zfs_comutil.h> 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), 1209 "%llxL B=%llu", 1210 (u_longlong_t)BP_GET_LSIZE(bp), 1211 (u_longlong_t)bp->blk_birth); 1212 } else { 1213 (void) snprintf(blkbuf + strlen(blkbuf), 1214 buflen - strlen(blkbuf), 1215 "%llxL/%llxP F=%llu B=%llu/%llu", 1216 (u_longlong_t)BP_GET_LSIZE(bp), 1217 (u_longlong_t)BP_GET_PSIZE(bp), 1218 (u_longlong_t)BP_GET_FILL(bp), 1219 (u_longlong_t)bp->blk_birth, 1220 (u_longlong_t)BP_PHYSICAL_BIRTH(bp)); 1221 } 1222} 1223 1224static void 1225print_indirect(blkptr_t *bp, const zbookmark_phys_t *zb, 1226 const dnode_phys_t *dnp) 1227{ 1228 char blkbuf[BP_SPRINTF_LEN]; 1229 int l; 1230 1231 if (!BP_IS_EMBEDDED(bp)) { 1232 ASSERT3U(BP_GET_TYPE(bp), ==, dnp->dn_type); 1233 ASSERT3U(BP_GET_LEVEL(bp), ==, zb->zb_level); 1234 } 1235 1236 (void) printf("%16llx ", (u_longlong_t)blkid2offset(dnp, bp, zb)); 1237 1238 ASSERT(zb->zb_level >= 0); 1239 1240 for (l = dnp->dn_nlevels - 1; l >= -1; l--) { 1241 if (l == zb->zb_level) { 1242 (void) printf("L%llx", (u_longlong_t)zb->zb_level); 1243 } else { 1244 (void) printf(" "); 1245 } 1246 } 1247 1248 snprintf_blkptr_compact(blkbuf, sizeof (blkbuf), bp); 1249 (void) printf("%s\n", blkbuf); 1250} 1251 1252static int 1253visit_indirect(spa_t *spa, const dnode_phys_t *dnp, 1254 blkptr_t *bp, const zbookmark_phys_t *zb) 1255{ 1256 int err = 0; 1257 1258 if (bp->blk_birth == 0) 1259 return (0); 1260 1261 print_indirect(bp, zb, dnp); 1262 1263 if (BP_GET_LEVEL(bp) > 0 && !BP_IS_HOLE(bp)) { 1264 arc_flags_t flags = ARC_FLAG_WAIT; 1265 int i; 1266 blkptr_t *cbp; 1267 int epb = BP_GET_LSIZE(bp) >> SPA_BLKPTRSHIFT; 1268 arc_buf_t *buf; 1269 uint64_t fill = 0; 1270 1271 err = arc_read(NULL, spa, bp, arc_getbuf_func, &buf, 1272 ZIO_PRIORITY_ASYNC_READ, ZIO_FLAG_CANFAIL, &flags, zb); 1273 if (err) 1274 return (err); 1275 ASSERT(buf->b_data); 1276 1277 /* recursively visit blocks below this */ 1278 cbp = buf->b_data; 1279 for (i = 0; i < epb; i++, cbp++) { 1280 zbookmark_phys_t czb; 1281 1282 SET_BOOKMARK(&czb, zb->zb_objset, zb->zb_object, 1283 zb->zb_level - 1, 1284 zb->zb_blkid * epb + i); 1285 err = visit_indirect(spa, dnp, cbp, &czb); 1286 if (err) 1287 break; 1288 fill += BP_GET_FILL(cbp); 1289 } 1290 if (!err) 1291 ASSERT3U(fill, ==, BP_GET_FILL(bp)); 1292 arc_buf_destroy(buf, &buf); 1293 } 1294 1295 return (err); 1296} 1297 1298/*ARGSUSED*/ 1299static void 1300dump_indirect(dnode_t *dn) 1301{ 1302 dnode_phys_t *dnp = dn->dn_phys; 1303 int j; 1304 zbookmark_phys_t czb; 1305 1306 (void) printf("Indirect blocks:\n"); 1307 1308 SET_BOOKMARK(&czb, dmu_objset_id(dn->dn_objset), 1309 dn->dn_object, dnp->dn_nlevels - 1, 0); 1310 for (j = 0; j < dnp->dn_nblkptr; j++) { 1311 czb.zb_blkid = j; 1312 (void) visit_indirect(dmu_objset_spa(dn->dn_objset), dnp, 1313 &dnp->dn_blkptr[j], &czb); 1314 } 1315 1316 (void) printf("\n"); 1317} 1318 1319/*ARGSUSED*/ 1320static void 1321dump_dsl_dir(objset_t *os, uint64_t object, void *data, size_t size) 1322{ 1323 dsl_dir_phys_t *dd = data; 1324 time_t crtime; 1325 char nice[32]; 1326 1327 if (dd == NULL) 1328 return; 1329 1330 ASSERT3U(size, >=, sizeof (dsl_dir_phys_t)); 1331 1332 crtime = dd->dd_creation_time; 1333 (void) printf("\t\tcreation_time = %s", ctime(&crtime)); 1334 (void) printf("\t\thead_dataset_obj = %llu\n", 1335 (u_longlong_t)dd->dd_head_dataset_obj); 1336 (void) printf("\t\tparent_dir_obj = %llu\n", 1337 (u_longlong_t)dd->dd_parent_obj); 1338 (void) printf("\t\torigin_obj = %llu\n", 1339 (u_longlong_t)dd->dd_origin_obj); 1340 (void) printf("\t\tchild_dir_zapobj = %llu\n", 1341 (u_longlong_t)dd->dd_child_dir_zapobj); 1342 zdb_nicenum(dd->dd_used_bytes, nice); 1343 (void) printf("\t\tused_bytes = %s\n", nice); 1344 zdb_nicenum(dd->dd_compressed_bytes, nice); 1345 (void) printf("\t\tcompressed_bytes = %s\n", nice); 1346 zdb_nicenum(dd->dd_uncompressed_bytes, nice); 1347 (void) printf("\t\tuncompressed_bytes = %s\n", nice); 1348 zdb_nicenum(dd->dd_quota, nice); 1349 (void) printf("\t\tquota = %s\n", nice); 1350 zdb_nicenum(dd->dd_reserved, nice); 1351 (void) printf("\t\treserved = %s\n", nice); 1352 (void) printf("\t\tprops_zapobj = %llu\n", 1353 (u_longlong_t)dd->dd_props_zapobj); 1354 (void) printf("\t\tdeleg_zapobj = %llu\n", 1355 (u_longlong_t)dd->dd_deleg_zapobj); 1356 (void) printf("\t\tflags = %llx\n", 1357 (u_longlong_t)dd->dd_flags); 1358 1359#define DO(which) \ 1360 zdb_nicenum(dd->dd_used_breakdown[DD_USED_ ## which], nice); \ 1361 (void) printf("\t\tused_breakdown[" #which "] = %s\n", nice) 1362 DO(HEAD); 1363 DO(SNAP); 1364 DO(CHILD); 1365 DO(CHILD_RSRV); 1366 DO(REFRSRV); 1367#undef DO 1368} 1369 1370/*ARGSUSED*/ 1371static void 1372dump_dsl_dataset(objset_t *os, uint64_t object, void *data, size_t size) 1373{ 1374 dsl_dataset_phys_t *ds = data; 1375 time_t crtime; 1376 char used[32], compressed[32], uncompressed[32], unique[32]; 1377 char blkbuf[BP_SPRINTF_LEN]; 1378 1379 if (ds == NULL) 1380 return; 1381 1382 ASSERT(size == sizeof (*ds)); 1383 crtime = ds->ds_creation_time; 1384 zdb_nicenum(ds->ds_referenced_bytes, used); 1385 zdb_nicenum(ds->ds_compressed_bytes, compressed); 1386 zdb_nicenum(ds->ds_uncompressed_bytes, uncompressed); 1387 zdb_nicenum(ds->ds_unique_bytes, unique); 1388 snprintf_blkptr(blkbuf, sizeof (blkbuf), &ds->ds_bp); 1389 1390 (void) printf("\t\tdir_obj = %llu\n", 1391 (u_longlong_t)ds->ds_dir_obj); 1392 (void) printf("\t\tprev_snap_obj = %llu\n", 1393 (u_longlong_t)ds->ds_prev_snap_obj); 1394 (void) printf("\t\tprev_snap_txg = %llu\n", 1395 (u_longlong_t)ds->ds_prev_snap_txg); 1396 (void) printf("\t\tnext_snap_obj = %llu\n", 1397 (u_longlong_t)ds->ds_next_snap_obj); 1398 (void) printf("\t\tsnapnames_zapobj = %llu\n", 1399 (u_longlong_t)ds->ds_snapnames_zapobj); 1400 (void) printf("\t\tnum_children = %llu\n", 1401 (u_longlong_t)ds->ds_num_children); 1402 (void) printf("\t\tuserrefs_obj = %llu\n", 1403 (u_longlong_t)ds->ds_userrefs_obj); 1404 (void) printf("\t\tcreation_time = %s", ctime(&crtime)); 1405 (void) printf("\t\tcreation_txg = %llu\n", 1406 (u_longlong_t)ds->ds_creation_txg); 1407 (void) printf("\t\tdeadlist_obj = %llu\n", 1408 (u_longlong_t)ds->ds_deadlist_obj); 1409 (void) printf("\t\tused_bytes = %s\n", used); 1410 (void) printf("\t\tcompressed_bytes = %s\n", compressed); 1411 (void) printf("\t\tuncompressed_bytes = %s\n", uncompressed); 1412 (void) printf("\t\tunique = %s\n", unique); 1413 (void) printf("\t\tfsid_guid = %llu\n", 1414 (u_longlong_t)ds->ds_fsid_guid); 1415 (void) printf("\t\tguid = %llu\n", 1416 (u_longlong_t)ds->ds_guid); 1417 (void) printf("\t\tflags = %llx\n", 1418 (u_longlong_t)ds->ds_flags); 1419 (void) printf("\t\tnext_clones_obj = %llu\n", 1420 (u_longlong_t)ds->ds_next_clones_obj); 1421 (void) printf("\t\tprops_obj = %llu\n", 1422 (u_longlong_t)ds->ds_props_obj); 1423 (void) printf("\t\tbp = %s\n", blkbuf); 1424} 1425 1426/* ARGSUSED */ 1427static int 1428dump_bptree_cb(void *arg, const blkptr_t *bp, dmu_tx_t *tx) 1429{ 1430 char blkbuf[BP_SPRINTF_LEN]; 1431 1432 if (bp->blk_birth != 0) { 1433 snprintf_blkptr(blkbuf, sizeof (blkbuf), bp); 1434 (void) printf("\t%s\n", blkbuf); 1435 } 1436 return (0); 1437} 1438 1439static void 1440dump_bptree(objset_t *os, uint64_t obj, char *name) 1441{ 1442 char bytes[32]; 1443 bptree_phys_t *bt; 1444 dmu_buf_t *db; 1445 1446 if (dump_opt['d'] < 3) 1447 return; 1448 1449 VERIFY3U(0, ==, dmu_bonus_hold(os, obj, FTAG, &db)); 1450 bt = db->db_data; 1451 zdb_nicenum(bt->bt_bytes, bytes); 1452 (void) printf("\n %s: %llu datasets, %s\n", 1453 name, (unsigned long long)(bt->bt_end - bt->bt_begin), bytes); 1454 dmu_buf_rele(db, FTAG); 1455 1456 if (dump_opt['d'] < 5) 1457 return; 1458 1459 (void) printf("\n"); 1460 1461 (void) bptree_iterate(os, obj, B_FALSE, dump_bptree_cb, NULL, NULL); 1462} 1463 1464/* ARGSUSED */ 1465static int 1466dump_bpobj_cb(void *arg, const blkptr_t *bp, dmu_tx_t *tx) 1467{ 1468 char blkbuf[BP_SPRINTF_LEN]; 1469 1470 ASSERT(bp->blk_birth != 0); 1471 snprintf_blkptr_compact(blkbuf, sizeof (blkbuf), bp); 1472 (void) printf("\t%s\n", blkbuf); 1473 return (0); 1474} 1475 1476static void 1477dump_full_bpobj(bpobj_t *bpo, char *name, int indent) 1478{ 1479 char bytes[32]; 1480 char comp[32]; 1481 char uncomp[32]; 1482 1483 if (dump_opt['d'] < 3) 1484 return; 1485 1486 zdb_nicenum(bpo->bpo_phys->bpo_bytes, bytes); 1487 if (bpo->bpo_havesubobj && bpo->bpo_phys->bpo_subobjs != 0) { 1488 zdb_nicenum(bpo->bpo_phys->bpo_comp, comp); 1489 zdb_nicenum(bpo->bpo_phys->bpo_uncomp, uncomp); 1490 (void) printf(" %*s: object %llu, %llu local blkptrs, " 1491 "%llu subobjs in object %llu, %s (%s/%s comp)\n", 1492 indent * 8, name, 1493 (u_longlong_t)bpo->bpo_object, 1494 (u_longlong_t)bpo->bpo_phys->bpo_num_blkptrs, 1495 (u_longlong_t)bpo->bpo_phys->bpo_num_subobjs, 1496 (u_longlong_t)bpo->bpo_phys->bpo_subobjs, 1497 bytes, comp, uncomp); 1498 1499 for (uint64_t i = 0; i < bpo->bpo_phys->bpo_num_subobjs; i++) { 1500 uint64_t subobj; 1501 bpobj_t subbpo; 1502 int error; 1503 VERIFY0(dmu_read(bpo->bpo_os, 1504 bpo->bpo_phys->bpo_subobjs, 1505 i * sizeof (subobj), sizeof (subobj), &subobj, 0)); 1506 error = bpobj_open(&subbpo, bpo->bpo_os, subobj); 1507 if (error != 0) { 1508 (void) printf("ERROR %u while trying to open " 1509 "subobj id %llu\n", 1510 error, (u_longlong_t)subobj); 1511 continue; 1512 } 1513 dump_full_bpobj(&subbpo, "subobj", indent + 1); 1514 bpobj_close(&subbpo); 1515 } 1516 } else { 1517 (void) printf(" %*s: object %llu, %llu blkptrs, %s\n", 1518 indent * 8, name, 1519 (u_longlong_t)bpo->bpo_object, 1520 (u_longlong_t)bpo->bpo_phys->bpo_num_blkptrs, 1521 bytes); 1522 } 1523 1524 if (dump_opt['d'] < 5) 1525 return; 1526 1527 1528 if (indent == 0) { 1529 (void) bpobj_iterate_nofree(bpo, dump_bpobj_cb, NULL, NULL); 1530 (void) printf("\n"); 1531 } 1532} 1533 1534static void 1535dump_deadlist(dsl_deadlist_t *dl) 1536{ 1537 dsl_deadlist_entry_t *dle; 1538 uint64_t unused; 1539 char bytes[32]; 1540 char comp[32]; 1541 char uncomp[32]; 1542 1543 if (dump_opt['d'] < 3) 1544 return; 1545 1546 if (dl->dl_oldfmt) { 1547 dump_full_bpobj(&dl->dl_bpobj, "old-format deadlist", 0); 1548 return; 1549 } 1550 1551 zdb_nicenum(dl->dl_phys->dl_used, bytes); 1552 zdb_nicenum(dl->dl_phys->dl_comp, comp); 1553 zdb_nicenum(dl->dl_phys->dl_uncomp, uncomp); 1554 (void) printf("\n Deadlist: %s (%s/%s comp)\n", 1555 bytes, comp, uncomp); 1556 1557 if (dump_opt['d'] < 4) 1558 return; 1559 1560 (void) printf("\n"); 1561 1562 /* force the tree to be loaded */ 1563 dsl_deadlist_space_range(dl, 0, UINT64_MAX, &unused, &unused, &unused); 1564 1565 for (dle = avl_first(&dl->dl_tree); dle; 1566 dle = AVL_NEXT(&dl->dl_tree, dle)) { 1567 if (dump_opt['d'] >= 5) { 1568 char buf[128]; 1569 (void) snprintf(buf, sizeof (buf), "mintxg %llu -> ", 1570 (longlong_t)dle->dle_mintxg, 1571 (longlong_t)dle->dle_bpobj.bpo_object); 1572 dump_full_bpobj(&dle->dle_bpobj, buf, 0); 1573 } else { 1574 (void) printf("mintxg %llu -> obj %llu\n", 1575 (longlong_t)dle->dle_mintxg, 1576 (longlong_t)dle->dle_bpobj.bpo_object); 1577 1578 } 1579 } 1580} 1581 1582static avl_tree_t idx_tree; 1583static avl_tree_t domain_tree; 1584static boolean_t fuid_table_loaded; 1585static boolean_t sa_loaded; 1586sa_attr_type_t *sa_attr_table; 1587 1588static void 1589fuid_table_destroy() 1590{ 1591 if (fuid_table_loaded) { 1592 zfs_fuid_table_destroy(&idx_tree, &domain_tree); 1593 fuid_table_loaded = B_FALSE; 1594 } 1595} 1596 1597/* 1598 * print uid or gid information. 1599 * For normal POSIX id just the id is printed in decimal format. 1600 * For CIFS files with FUID the fuid is printed in hex followed by 1601 * the domain-rid string. 1602 */ 1603static void 1604print_idstr(uint64_t id, const char *id_type) 1605{ 1606 if (FUID_INDEX(id)) { 1607 char *domain; 1608 1609 domain = zfs_fuid_idx_domain(&idx_tree, FUID_INDEX(id)); 1610 (void) printf("\t%s %llx [%s-%d]\n", id_type, 1611 (u_longlong_t)id, domain, (int)FUID_RID(id)); 1612 } else { 1613 (void) printf("\t%s %llu\n", id_type, (u_longlong_t)id); 1614 } 1615 1616} 1617 1618static void 1619dump_uidgid(objset_t *os, uint64_t uid, uint64_t gid) 1620{ 1621 uint32_t uid_idx, gid_idx; 1622 1623 uid_idx = FUID_INDEX(uid); 1624 gid_idx = FUID_INDEX(gid); 1625 1626 /* Load domain table, if not already loaded */ 1627 if (!fuid_table_loaded && (uid_idx || gid_idx)) { 1628 uint64_t fuid_obj; 1629 1630 /* first find the fuid object. It lives in the master node */ 1631 VERIFY(zap_lookup(os, MASTER_NODE_OBJ, ZFS_FUID_TABLES, 1632 8, 1, &fuid_obj) == 0); 1633 zfs_fuid_avl_tree_create(&idx_tree, &domain_tree); 1634 (void) zfs_fuid_table_load(os, fuid_obj, 1635 &idx_tree, &domain_tree); 1636 fuid_table_loaded = B_TRUE; 1637 } 1638 1639 print_idstr(uid, "uid"); 1640 print_idstr(gid, "gid"); 1641} 1642 1643/*ARGSUSED*/ 1644static void 1645dump_znode(objset_t *os, uint64_t object, void *data, size_t size) 1646{ 1647 char path[MAXPATHLEN * 2]; /* allow for xattr and failure prefix */ 1648 sa_handle_t *hdl; 1649 uint64_t xattr, rdev, gen; 1650 uint64_t uid, gid, mode, fsize, parent, links; 1651 uint64_t pflags; 1652 uint64_t acctm[2], modtm[2], chgtm[2], crtm[2]; 1653 time_t z_crtime, z_atime, z_mtime, z_ctime; 1654 sa_bulk_attr_t bulk[12]; 1655 int idx = 0; 1656 int error; 1657 1658 if (!sa_loaded) { 1659 uint64_t sa_attrs = 0; 1660 uint64_t version; 1661 1662 VERIFY(zap_lookup(os, MASTER_NODE_OBJ, ZPL_VERSION_STR, 1663 8, 1, &version) == 0); 1664 if (version >= ZPL_VERSION_SA) { 1665 VERIFY(zap_lookup(os, MASTER_NODE_OBJ, ZFS_SA_ATTRS, 1666 8, 1, &sa_attrs) == 0); 1667 } 1668 if ((error = sa_setup(os, sa_attrs, zfs_attr_table, 1669 ZPL_END, &sa_attr_table)) != 0) { 1670 (void) printf("sa_setup failed errno %d, can't " 1671 "display znode contents\n", error); 1672 return; 1673 } 1674 sa_loaded = B_TRUE; 1675 } 1676 1677 if (sa_handle_get(os, object, NULL, SA_HDL_PRIVATE, &hdl)) { 1678 (void) printf("Failed to get handle for SA znode\n"); 1679 return; 1680 } 1681 1682 SA_ADD_BULK_ATTR(bulk, idx, sa_attr_table[ZPL_UID], NULL, &uid, 8); 1683 SA_ADD_BULK_ATTR(bulk, idx, sa_attr_table[ZPL_GID], NULL, &gid, 8); 1684 SA_ADD_BULK_ATTR(bulk, idx, sa_attr_table[ZPL_LINKS], NULL, 1685 &links, 8); 1686 SA_ADD_BULK_ATTR(bulk, idx, sa_attr_table[ZPL_GEN], NULL, &gen, 8); 1687 SA_ADD_BULK_ATTR(bulk, idx, sa_attr_table[ZPL_MODE], NULL, 1688 &mode, 8); 1689 SA_ADD_BULK_ATTR(bulk, idx, sa_attr_table[ZPL_PARENT], 1690 NULL, &parent, 8); 1691 SA_ADD_BULK_ATTR(bulk, idx, sa_attr_table[ZPL_SIZE], NULL, 1692 &fsize, 8); 1693 SA_ADD_BULK_ATTR(bulk, idx, sa_attr_table[ZPL_ATIME], NULL, 1694 acctm, 16); 1695 SA_ADD_BULK_ATTR(bulk, idx, sa_attr_table[ZPL_MTIME], NULL, 1696 modtm, 16); 1697 SA_ADD_BULK_ATTR(bulk, idx, sa_attr_table[ZPL_CRTIME], NULL, 1698 crtm, 16); 1699 SA_ADD_BULK_ATTR(bulk, idx, sa_attr_table[ZPL_CTIME], NULL, 1700 chgtm, 16); 1701 SA_ADD_BULK_ATTR(bulk, idx, sa_attr_table[ZPL_FLAGS], NULL, 1702 &pflags, 8); 1703 1704 if (sa_bulk_lookup(hdl, bulk, idx)) { 1705 (void) sa_handle_destroy(hdl); 1706 return; 1707 } 1708 1709 error = zfs_obj_to_path(os, object, path, sizeof (path)); 1710 if (error != 0) { 1711 (void) snprintf(path, sizeof (path), "\?\?\?<object#%llu>", 1712 (u_longlong_t)object); 1713 } 1714 if (dump_opt['d'] < 3) { 1715 (void) printf("\t%s\n", path); 1716 (void) sa_handle_destroy(hdl); 1717 return; 1718 } 1719 1720 z_crtime = (time_t)crtm[0]; 1721 z_atime = (time_t)acctm[0]; 1722 z_mtime = (time_t)modtm[0]; 1723 z_ctime = (time_t)chgtm[0]; 1724 1725 (void) printf("\tpath %s\n", path); 1726 dump_uidgid(os, uid, gid); 1727 (void) printf("\tatime %s", ctime(&z_atime)); 1728 (void) printf("\tmtime %s", ctime(&z_mtime)); 1729 (void) printf("\tctime %s", ctime(&z_ctime)); 1730 (void) printf("\tcrtime %s", ctime(&z_crtime)); 1731 (void) printf("\tgen %llu\n", (u_longlong_t)gen); 1732 (void) printf("\tmode %llo\n", (u_longlong_t)mode); 1733 (void) printf("\tsize %llu\n", (u_longlong_t)fsize); 1734 (void) printf("\tparent %llu\n", (u_longlong_t)parent); 1735 (void) printf("\tlinks %llu\n", (u_longlong_t)links); 1736 (void) printf("\tpflags %llx\n", (u_longlong_t)pflags); 1737 if (sa_lookup(hdl, sa_attr_table[ZPL_XATTR], &xattr, 1738 sizeof (uint64_t)) == 0) 1739 (void) printf("\txattr %llu\n", (u_longlong_t)xattr); 1740 if (sa_lookup(hdl, sa_attr_table[ZPL_RDEV], &rdev, 1741 sizeof (uint64_t)) == 0) 1742 (void) printf("\trdev 0x%016llx\n", (u_longlong_t)rdev); 1743 sa_handle_destroy(hdl); 1744} 1745 1746/*ARGSUSED*/ 1747static void 1748dump_acl(objset_t *os, uint64_t object, void *data, size_t size) 1749{ 1750} 1751 1752/*ARGSUSED*/ 1753static void 1754dump_dmu_objset(objset_t *os, uint64_t object, void *data, size_t size) 1755{ 1756} 1757 1758static object_viewer_t *object_viewer[DMU_OT_NUMTYPES + 1] = { 1759 dump_none, /* unallocated */ 1760 dump_zap, /* object directory */ 1761 dump_uint64, /* object array */ 1762 dump_none, /* packed nvlist */ 1763 dump_packed_nvlist, /* packed nvlist size */ 1764 dump_none, /* bpobj */ 1765 dump_bpobj, /* bpobj header */ 1766 dump_none, /* SPA space map header */ 1767 dump_none, /* SPA space map */ 1768 dump_none, /* ZIL intent log */ 1769 dump_dnode, /* DMU dnode */ 1770 dump_dmu_objset, /* DMU objset */ 1771 dump_dsl_dir, /* DSL directory */ 1772 dump_zap, /* DSL directory child map */ 1773 dump_zap, /* DSL dataset snap map */ 1774 dump_zap, /* DSL props */ 1775 dump_dsl_dataset, /* DSL dataset */ 1776 dump_znode, /* ZFS znode */ 1777 dump_acl, /* ZFS V0 ACL */ 1778 dump_uint8, /* ZFS plain file */ 1779 dump_zpldir, /* ZFS directory */ 1780 dump_zap, /* ZFS master node */ 1781 dump_zap, /* ZFS delete queue */ 1782 dump_uint8, /* zvol object */ 1783 dump_zap, /* zvol prop */ 1784 dump_uint8, /* other uint8[] */ 1785 dump_uint64, /* other uint64[] */ 1786 dump_zap, /* other ZAP */ 1787 dump_zap, /* persistent error log */ 1788 dump_uint8, /* SPA history */ 1789 dump_history_offsets, /* SPA history offsets */ 1790 dump_zap, /* Pool properties */ 1791 dump_zap, /* DSL permissions */ 1792 dump_acl, /* ZFS ACL */ 1793 dump_uint8, /* ZFS SYSACL */ 1794 dump_none, /* FUID nvlist */ 1795 dump_packed_nvlist, /* FUID nvlist size */ 1796 dump_zap, /* DSL dataset next clones */ 1797 dump_zap, /* DSL scrub queue */ 1798 dump_zap, /* ZFS user/group used */ 1799 dump_zap, /* ZFS user/group quota */ 1800 dump_zap, /* snapshot refcount tags */ 1801 dump_ddt_zap, /* DDT ZAP object */ 1802 dump_zap, /* DDT statistics */ 1803 dump_znode, /* SA object */ 1804 dump_zap, /* SA Master Node */ 1805 dump_sa_attrs, /* SA attribute registration */ 1806 dump_sa_layouts, /* SA attribute layouts */ 1807 dump_zap, /* DSL scrub translations */ 1808 dump_none, /* fake dedup BP */ 1809 dump_zap, /* deadlist */ 1810 dump_none, /* deadlist hdr */ 1811 dump_zap, /* dsl clones */ 1812 dump_bpobj_subobjs, /* bpobj subobjs */ 1813 dump_unknown, /* Unknown type, must be last */ 1814}; 1815 1816static void 1817dump_object(objset_t *os, uint64_t object, int verbosity, int *print_header) 1818{ 1819 dmu_buf_t *db = NULL; 1820 dmu_object_info_t doi; 1821 dnode_t *dn; 1822 void *bonus = NULL; 1823 size_t bsize = 0; 1824 char iblk[32], dblk[32], lsize[32], asize[32], fill[32]; 1825 char bonus_size[32]; 1826 char aux[50]; 1827 int error; 1828 1829 if (*print_header) { 1830 (void) printf("\n%10s %3s %5s %5s %5s %5s %6s %s\n", 1831 "Object", "lvl", "iblk", "dblk", "dsize", "lsize", 1832 "%full", "type"); 1833 *print_header = 0; 1834 } 1835 1836 if (object == 0) { 1837 dn = DMU_META_DNODE(os); 1838 } else { 1839 error = dmu_bonus_hold(os, object, FTAG, &db); 1840 if (error) 1841 fatal("dmu_bonus_hold(%llu) failed, errno %u", 1842 object, error); 1843 bonus = db->db_data; 1844 bsize = db->db_size; 1845 dn = DB_DNODE((dmu_buf_impl_t *)db); 1846 } 1847 dmu_object_info_from_dnode(dn, &doi); 1848 1849 zdb_nicenum(doi.doi_metadata_block_size, iblk); 1850 zdb_nicenum(doi.doi_data_block_size, dblk); 1851 zdb_nicenum(doi.doi_max_offset, lsize); 1852 zdb_nicenum(doi.doi_physical_blocks_512 << 9, asize); 1853 zdb_nicenum(doi.doi_bonus_size, bonus_size); 1854 (void) sprintf(fill, "%6.2f", 100.0 * doi.doi_fill_count * 1855 doi.doi_data_block_size / (object == 0 ? DNODES_PER_BLOCK : 1) / 1856 doi.doi_max_offset); 1857 1858 aux[0] = '\0'; 1859 1860 if (doi.doi_checksum != ZIO_CHECKSUM_INHERIT || verbosity >= 6) { 1861 (void) snprintf(aux + strlen(aux), sizeof (aux), " (K=%s)", 1862 ZDB_CHECKSUM_NAME(doi.doi_checksum)); 1863 } 1864 1865 if (doi.doi_compress != ZIO_COMPRESS_INHERIT || verbosity >= 6) { 1866 (void) snprintf(aux + strlen(aux), sizeof (aux), " (Z=%s)", 1867 ZDB_COMPRESS_NAME(doi.doi_compress)); 1868 } 1869 1870 (void) printf("%10lld %3u %5s %5s %5s %5s %6s %s%s\n", 1871 (u_longlong_t)object, doi.doi_indirection, iblk, dblk, 1872 asize, lsize, fill, ZDB_OT_NAME(doi.doi_type), aux); 1873 1874 if (doi.doi_bonus_type != DMU_OT_NONE && verbosity > 3) { 1875 (void) printf("%10s %3s %5s %5s %5s %5s %6s %s\n", 1876 "", "", "", "", "", bonus_size, "bonus", 1877 ZDB_OT_NAME(doi.doi_bonus_type)); 1878 } 1879 1880 if (verbosity >= 4) { 1881 (void) printf("\tdnode flags: %s%s%s\n", 1882 (dn->dn_phys->dn_flags & DNODE_FLAG_USED_BYTES) ? 1883 "USED_BYTES " : "", 1884 (dn->dn_phys->dn_flags & DNODE_FLAG_USERUSED_ACCOUNTED) ? 1885 "USERUSED_ACCOUNTED " : "", 1886 (dn->dn_phys->dn_flags & DNODE_FLAG_SPILL_BLKPTR) ? 1887 "SPILL_BLKPTR" : ""); 1888 (void) printf("\tdnode maxblkid: %llu\n", 1889 (longlong_t)dn->dn_phys->dn_maxblkid); 1890 1891 object_viewer[ZDB_OT_TYPE(doi.doi_bonus_type)](os, object, 1892 bonus, bsize); 1893 object_viewer[ZDB_OT_TYPE(doi.doi_type)](os, object, NULL, 0); 1894 *print_header = 1; 1895 } 1896 1897 if (verbosity >= 5) 1898 dump_indirect(dn); 1899 1900 if (verbosity >= 5) { 1901 /* 1902 * Report the list of segments that comprise the object. 1903 */ 1904 uint64_t start = 0; 1905 uint64_t end; 1906 uint64_t blkfill = 1; 1907 int minlvl = 1; 1908 1909 if (dn->dn_type == DMU_OT_DNODE) { 1910 minlvl = 0; 1911 blkfill = DNODES_PER_BLOCK; 1912 } 1913 1914 for (;;) { 1915 char segsize[32]; 1916 error = dnode_next_offset(dn, 1917 0, &start, minlvl, blkfill, 0); 1918 if (error) 1919 break; 1920 end = start; 1921 error = dnode_next_offset(dn, 1922 DNODE_FIND_HOLE, &end, minlvl, blkfill, 0); 1923 zdb_nicenum(end - start, segsize); 1924 (void) printf("\t\tsegment [%016llx, %016llx)" 1925 " size %5s\n", (u_longlong_t)start, 1926 (u_longlong_t)end, segsize); 1927 if (error) 1928 break; 1929 start = end; 1930 } 1931 } 1932 1933 if (db != NULL) 1934 dmu_buf_rele(db, FTAG); 1935} 1936 1937static char *objset_types[DMU_OST_NUMTYPES] = { 1938 "NONE", "META", "ZPL", "ZVOL", "OTHER", "ANY" }; 1939 1940static void 1941dump_dir(objset_t *os) 1942{ 1943 dmu_objset_stats_t dds; 1944 uint64_t object, object_count; 1945 uint64_t refdbytes, usedobjs, scratch; 1946 char numbuf[32]; 1947 char blkbuf[BP_SPRINTF_LEN + 20]; 1948 char osname[ZFS_MAX_DATASET_NAME_LEN]; 1949 char *type = "UNKNOWN"; 1950 int verbosity = dump_opt['d']; 1951 int print_header = 1; 1952 int i, error; 1953 1954 dsl_pool_config_enter(dmu_objset_pool(os), FTAG); 1955 dmu_objset_fast_stat(os, &dds); 1956 dsl_pool_config_exit(dmu_objset_pool(os), FTAG); 1957 1958 if (dds.dds_type < DMU_OST_NUMTYPES) 1959 type = objset_types[dds.dds_type]; 1960 1961 if (dds.dds_type == DMU_OST_META) { 1962 dds.dds_creation_txg = TXG_INITIAL; 1963 usedobjs = BP_GET_FILL(os->os_rootbp); 1964 refdbytes = dsl_dir_phys(os->os_spa->spa_dsl_pool->dp_mos_dir)-> 1965 dd_used_bytes; 1966 } else { 1967 dmu_objset_space(os, &refdbytes, &scratch, &usedobjs, &scratch); 1968 } 1969 1970 ASSERT3U(usedobjs, ==, BP_GET_FILL(os->os_rootbp)); 1971 1972 zdb_nicenum(refdbytes, numbuf); 1973 1974 if (verbosity >= 4) { 1975 (void) snprintf(blkbuf, sizeof (blkbuf), ", rootbp "); 1976 (void) snprintf_blkptr(blkbuf + strlen(blkbuf), 1977 sizeof (blkbuf) - strlen(blkbuf), os->os_rootbp); 1978 } else { 1979 blkbuf[0] = '\0'; 1980 } 1981 1982 dmu_objset_name(os, osname); 1983 1984 (void) printf("Dataset %s [%s], ID %llu, cr_txg %llu, " 1985 "%s, %llu objects%s\n", 1986 osname, type, (u_longlong_t)dmu_objset_id(os), 1987 (u_longlong_t)dds.dds_creation_txg, 1988 numbuf, (u_longlong_t)usedobjs, blkbuf); 1989 1990 if (zopt_objects != 0) { 1991 for (i = 0; i < zopt_objects; i++) 1992 dump_object(os, zopt_object[i], verbosity, 1993 &print_header); 1994 (void) printf("\n"); 1995 return; 1996 } 1997 1998 if (dump_opt['i'] != 0 || verbosity >= 2) 1999 dump_intent_log(dmu_objset_zil(os)); 2000 2001 if (dmu_objset_ds(os) != NULL) 2002 dump_deadlist(&dmu_objset_ds(os)->ds_deadlist); 2003 2004 if (verbosity < 2) 2005 return; 2006 2007 if (BP_IS_HOLE(os->os_rootbp)) 2008 return; 2009 2010 dump_object(os, 0, verbosity, &print_header); 2011 object_count = 0; 2012 if (DMU_USERUSED_DNODE(os) != NULL && 2013 DMU_USERUSED_DNODE(os)->dn_type != 0) { 2014 dump_object(os, DMU_USERUSED_OBJECT, verbosity, &print_header); 2015 dump_object(os, DMU_GROUPUSED_OBJECT, verbosity, &print_header); 2016 } 2017 2018 object = 0; 2019 while ((error = dmu_object_next(os, &object, B_FALSE, 0)) == 0) { 2020 dump_object(os, object, verbosity, &print_header); 2021 object_count++; 2022 } 2023 2024 ASSERT3U(object_count, ==, usedobjs); 2025 2026 (void) printf("\n"); 2027 2028 if (error != ESRCH) { 2029 (void) fprintf(stderr, "dmu_object_next() = %d\n", error); 2030 abort(); 2031 } 2032} 2033 2034static void 2035dump_uberblock(uberblock_t *ub, const char *header, const char *footer) 2036{ 2037 time_t timestamp = ub->ub_timestamp; 2038 2039 (void) printf(header ? header : ""); 2040 (void) printf("\tmagic = %016llx\n", (u_longlong_t)ub->ub_magic); 2041 (void) printf("\tversion = %llu\n", (u_longlong_t)ub->ub_version); 2042 (void) printf("\ttxg = %llu\n", (u_longlong_t)ub->ub_txg); 2043 (void) printf("\tguid_sum = %llu\n", (u_longlong_t)ub->ub_guid_sum); 2044 (void) printf("\ttimestamp = %llu UTC = %s", 2045 (u_longlong_t)ub->ub_timestamp, asctime(localtime(×tamp))); 2046 if (dump_opt['u'] >= 3) { 2047 char blkbuf[BP_SPRINTF_LEN]; 2048 snprintf_blkptr(blkbuf, sizeof (blkbuf), &ub->ub_rootbp); 2049 (void) printf("\trootbp = %s\n", blkbuf); 2050 } 2051 (void) printf(footer ? footer : ""); 2052} 2053 2054static void 2055dump_config(spa_t *spa) 2056{ 2057 dmu_buf_t *db; 2058 size_t nvsize = 0; 2059 int error = 0; 2060 2061 2062 error = dmu_bonus_hold(spa->spa_meta_objset, 2063 spa->spa_config_object, FTAG, &db); 2064 2065 if (error == 0) { 2066 nvsize = *(uint64_t *)db->db_data; 2067 dmu_buf_rele(db, FTAG); 2068 2069 (void) printf("\nMOS Configuration:\n"); 2070 dump_packed_nvlist(spa->spa_meta_objset, 2071 spa->spa_config_object, (void *)&nvsize, 1); 2072 } else { 2073 (void) fprintf(stderr, "dmu_bonus_hold(%llu) failed, errno %d", 2074 (u_longlong_t)spa->spa_config_object, error); 2075 } 2076} 2077 2078static void 2079dump_cachefile(const char *cachefile) 2080{ 2081 int fd; 2082 struct stat64 statbuf; 2083 char *buf; 2084 nvlist_t *config; 2085 2086 if ((fd = open64(cachefile, O_RDONLY)) < 0) { 2087 (void) printf("cannot open '%s': %s\n", cachefile, 2088 strerror(errno)); 2089 exit(1); 2090 } 2091 2092 if (fstat64(fd, &statbuf) != 0) { 2093 (void) printf("failed to stat '%s': %s\n", cachefile, 2094 strerror(errno)); 2095 exit(1); 2096 } 2097 2098 if ((buf = malloc(statbuf.st_size)) == NULL) { 2099 (void) fprintf(stderr, "failed to allocate %llu bytes\n", 2100 (u_longlong_t)statbuf.st_size); 2101 exit(1); 2102 } 2103 2104 if (read(fd, buf, statbuf.st_size) != statbuf.st_size) { 2105 (void) fprintf(stderr, "failed to read %llu bytes\n", 2106 (u_longlong_t)statbuf.st_size); 2107 exit(1); 2108 } 2109 2110 (void) close(fd); 2111 2112 if (nvlist_unpack(buf, statbuf.st_size, &config, 0) != 0) { 2113 (void) fprintf(stderr, "failed to unpack nvlist\n"); 2114 exit(1); 2115 } 2116 2117 free(buf); 2118 2119 dump_nvlist(config, 0); 2120 2121 nvlist_free(config); 2122} 2123 2124#define ZDB_MAX_UB_HEADER_SIZE 32 2125 2126static void 2127dump_label_uberblocks(vdev_label_t *lbl, uint64_t ashift) 2128{ 2129 vdev_t vd; 2130 vdev_t *vdp = &vd; 2131 char header[ZDB_MAX_UB_HEADER_SIZE]; 2132 2133 vd.vdev_ashift = ashift; 2134 vdp->vdev_top = vdp; 2135 2136 for (int i = 0; i < VDEV_UBERBLOCK_COUNT(vdp); i++) { 2137 uint64_t uoff = VDEV_UBERBLOCK_OFFSET(vdp, i); 2138 uberblock_t *ub = (void *)((char *)lbl + uoff); 2139 2140 if (uberblock_verify(ub)) 2141 continue; 2142 (void) snprintf(header, ZDB_MAX_UB_HEADER_SIZE, 2143 "Uberblock[%d]\n", i); 2144 dump_uberblock(ub, header, ""); 2145 } 2146} 2147 2148static void 2149dump_label(const char *dev) 2150{ 2151 int fd; 2152 vdev_label_t label; 2153 char *path, *buf = label.vl_vdev_phys.vp_nvlist; 2154 size_t buflen = sizeof (label.vl_vdev_phys.vp_nvlist); 2155 struct stat64 statbuf; 2156 uint64_t psize, ashift; 2157 int len = strlen(dev) + 1; 2158 2159 if (strncmp(dev, ZFS_DISK_ROOTD, strlen(ZFS_DISK_ROOTD)) == 0) { 2160 len++; 2161 path = malloc(len); 2162 (void) snprintf(path, len, "%s%s", ZFS_RDISK_ROOTD, 2163 dev + strlen(ZFS_DISK_ROOTD)); 2164 } else { 2165 path = strdup(dev); 2166 } 2167 2168 if ((fd = open64(path, O_RDONLY)) < 0) { 2169 (void) printf("cannot open '%s': %s\n", path, strerror(errno)); 2170 free(path); 2171 exit(1); 2172 } 2173 2174 if (fstat64(fd, &statbuf) != 0) { 2175 (void) printf("failed to stat '%s': %s\n", path, 2176 strerror(errno)); 2177 free(path); 2178 (void) close(fd); 2179 exit(1); 2180 } 2181 2182 if (S_ISBLK(statbuf.st_mode)) { 2183 (void) printf("cannot use '%s': character device required\n", 2184 path); 2185 free(path); 2186 (void) close(fd); 2187 exit(1); 2188 } 2189 2190 psize = statbuf.st_size; 2191 psize = P2ALIGN(psize, (uint64_t)sizeof (vdev_label_t)); 2192 2193 for (int l = 0; l < VDEV_LABELS; l++) { 2194 nvlist_t *config = NULL; 2195 2196 (void) printf("--------------------------------------------\n"); 2197 (void) printf("LABEL %d\n", l); 2198 (void) printf("--------------------------------------------\n"); 2199 2200 if (pread64(fd, &label, sizeof (label), 2201 vdev_label_offset(psize, l, 0)) != sizeof (label)) { 2202 (void) printf("failed to read label %d\n", l); 2203 continue; 2204 } 2205 2206 if (nvlist_unpack(buf, buflen, &config, 0) != 0) { 2207 (void) printf("failed to unpack label %d\n", l); 2208 ashift = SPA_MINBLOCKSHIFT; 2209 } else { 2210 nvlist_t *vdev_tree = NULL; 2211 2212 dump_nvlist(config, 4); 2213 if ((nvlist_lookup_nvlist(config, 2214 ZPOOL_CONFIG_VDEV_TREE, &vdev_tree) != 0) || 2215 (nvlist_lookup_uint64(vdev_tree, 2216 ZPOOL_CONFIG_ASHIFT, &ashift) != 0)) 2217 ashift = SPA_MINBLOCKSHIFT; 2218 nvlist_free(config); 2219 } 2220 if (dump_opt['u']) 2221 dump_label_uberblocks(&label, ashift); 2222 } 2223 2224 free(path); 2225 (void) close(fd); 2226} 2227 2228static uint64_t dataset_feature_count[SPA_FEATURES]; 2229 2230/*ARGSUSED*/ 2231static int 2232dump_one_dir(const char *dsname, void *arg) 2233{ 2234 int error; 2235 objset_t *os; 2236 2237 error = dmu_objset_own(dsname, DMU_OST_ANY, B_TRUE, FTAG, &os); 2238 if (error) { 2239 (void) printf("Could not open %s, error %d\n", dsname, error); 2240 return (0); 2241 } 2242 2243 for (spa_feature_t f = 0; f < SPA_FEATURES; f++) { 2244 if (!dmu_objset_ds(os)->ds_feature_inuse[f]) 2245 continue; 2246 ASSERT(spa_feature_table[f].fi_flags & 2247 ZFEATURE_FLAG_PER_DATASET); 2248 dataset_feature_count[f]++; 2249 } 2250 2251 dump_dir(os); 2252 dmu_objset_disown(os, FTAG); 2253 fuid_table_destroy(); 2254 sa_loaded = B_FALSE; 2255 return (0); 2256} 2257 2258/* 2259 * Block statistics. 2260 */ 2261#define PSIZE_HISTO_SIZE (SPA_OLD_MAXBLOCKSIZE / SPA_MINBLOCKSIZE + 2) 2262typedef struct zdb_blkstats { 2263 uint64_t zb_asize; 2264 uint64_t zb_lsize; 2265 uint64_t zb_psize; 2266 uint64_t zb_count; 2267 uint64_t zb_gangs; 2268 uint64_t zb_ditto_samevdev; 2269 uint64_t zb_psize_histogram[PSIZE_HISTO_SIZE]; 2270} zdb_blkstats_t; 2271 2272/* 2273 * Extended object types to report deferred frees and dedup auto-ditto blocks. 2274 */ 2275#define ZDB_OT_DEFERRED (DMU_OT_NUMTYPES + 0) 2276#define ZDB_OT_DITTO (DMU_OT_NUMTYPES + 1) 2277#define ZDB_OT_OTHER (DMU_OT_NUMTYPES + 2) 2278#define ZDB_OT_TOTAL (DMU_OT_NUMTYPES + 3) 2279 2280static char *zdb_ot_extname[] = { 2281 "deferred free", 2282 "dedup ditto", 2283 "other", 2284 "Total", 2285}; 2286 2287#define ZB_TOTAL DN_MAX_LEVELS 2288 2289typedef struct zdb_cb { 2290 zdb_blkstats_t zcb_type[ZB_TOTAL + 1][ZDB_OT_TOTAL + 1]; 2291 uint64_t zcb_dedup_asize; 2292 uint64_t zcb_dedup_blocks; 2293 uint64_t zcb_embedded_blocks[NUM_BP_EMBEDDED_TYPES]; 2294 uint64_t zcb_embedded_histogram[NUM_BP_EMBEDDED_TYPES] 2295 [BPE_PAYLOAD_SIZE]; 2296 uint64_t zcb_start; 2297 uint64_t zcb_lastprint; 2298 uint64_t zcb_totalasize; 2299 uint64_t zcb_errors[256]; 2300 int zcb_readfails; 2301 int zcb_haderrors; 2302 spa_t *zcb_spa; 2303} zdb_cb_t; 2304 2305static void 2306zdb_count_block(zdb_cb_t *zcb, zilog_t *zilog, const blkptr_t *bp, 2307 dmu_object_type_t type) 2308{ 2309 uint64_t refcnt = 0; 2310 2311 ASSERT(type < ZDB_OT_TOTAL); 2312 2313 if (zilog && zil_bp_tree_add(zilog, bp) != 0) 2314 return; 2315 2316 for (int i = 0; i < 4; i++) { 2317 int l = (i < 2) ? BP_GET_LEVEL(bp) : ZB_TOTAL; 2318 int t = (i & 1) ? type : ZDB_OT_TOTAL; 2319 int equal; 2320 zdb_blkstats_t *zb = &zcb->zcb_type[l][t]; 2321 2322 zb->zb_asize += BP_GET_ASIZE(bp); 2323 zb->zb_lsize += BP_GET_LSIZE(bp); 2324 zb->zb_psize += BP_GET_PSIZE(bp); 2325 zb->zb_count++; 2326 2327 /* 2328 * The histogram is only big enough to record blocks up to 2329 * SPA_OLD_MAXBLOCKSIZE; larger blocks go into the last, 2330 * "other", bucket. 2331 */ 2332 int idx = BP_GET_PSIZE(bp) >> SPA_MINBLOCKSHIFT; 2333 idx = MIN(idx, SPA_OLD_MAXBLOCKSIZE / SPA_MINBLOCKSIZE + 1); 2334 zb->zb_psize_histogram[idx]++; 2335 2336 zb->zb_gangs += BP_COUNT_GANG(bp); 2337 2338 switch (BP_GET_NDVAS(bp)) { 2339 case 2: 2340 if (DVA_GET_VDEV(&bp->blk_dva[0]) == 2341 DVA_GET_VDEV(&bp->blk_dva[1])) 2342 zb->zb_ditto_samevdev++; 2343 break; 2344 case 3: 2345 equal = (DVA_GET_VDEV(&bp->blk_dva[0]) == 2346 DVA_GET_VDEV(&bp->blk_dva[1])) + 2347 (DVA_GET_VDEV(&bp->blk_dva[0]) == 2348 DVA_GET_VDEV(&bp->blk_dva[2])) + 2349 (DVA_GET_VDEV(&bp->blk_dva[1]) == 2350 DVA_GET_VDEV(&bp->blk_dva[2])); 2351 if (equal != 0) 2352 zb->zb_ditto_samevdev++; 2353 break; 2354 } 2355 2356 } 2357 2358 if (BP_IS_EMBEDDED(bp)) { 2359 zcb->zcb_embedded_blocks[BPE_GET_ETYPE(bp)]++; 2360 zcb->zcb_embedded_histogram[BPE_GET_ETYPE(bp)] 2361 [BPE_GET_PSIZE(bp)]++; 2362 return; 2363 } 2364 2365 if (dump_opt['L']) 2366 return; 2367 2368 if (BP_GET_DEDUP(bp)) { 2369 ddt_t *ddt; 2370 ddt_entry_t *dde; 2371 2372 ddt = ddt_select(zcb->zcb_spa, bp); 2373 ddt_enter(ddt); 2374 dde = ddt_lookup(ddt, bp, B_FALSE); 2375 2376 if (dde == NULL) { 2377 refcnt = 0; 2378 } else { 2379 ddt_phys_t *ddp = ddt_phys_select(dde, bp); 2380 ddt_phys_decref(ddp); 2381 refcnt = ddp->ddp_refcnt; 2382 if (ddt_phys_total_refcnt(dde) == 0) 2383 ddt_remove(ddt, dde); 2384 } 2385 ddt_exit(ddt); 2386 } 2387 2388 VERIFY3U(zio_wait(zio_claim(NULL, zcb->zcb_spa, 2389 refcnt ? 0 : spa_first_txg(zcb->zcb_spa), 2390 bp, NULL, NULL, ZIO_FLAG_CANFAIL)), ==, 0); 2391} 2392 2393/* ARGSUSED */ 2394static void 2395zdb_blkptr_done(zio_t *zio) 2396{ 2397 spa_t *spa = zio->io_spa; 2398 blkptr_t *bp = zio->io_bp; 2399 int ioerr = zio->io_error; 2400 zdb_cb_t *zcb = zio->io_private; 2401 zbookmark_phys_t *zb = &zio->io_bookmark; 2402 2403 zio_data_buf_free(zio->io_data, zio->io_size); 2404 2405 mutex_enter(&spa->spa_scrub_lock); 2406 spa->spa_scrub_inflight--; 2407 cv_broadcast(&spa->spa_scrub_io_cv); 2408 2409 if (ioerr && !(zio->io_flags & ZIO_FLAG_SPECULATIVE)) { 2410 char blkbuf[BP_SPRINTF_LEN]; 2411 2412 zcb->zcb_haderrors = 1; 2413 zcb->zcb_errors[ioerr]++; 2414 2415 if (dump_opt['b'] >= 2) 2416 snprintf_blkptr(blkbuf, sizeof (blkbuf), bp); 2417 else 2418 blkbuf[0] = '\0'; 2419 2420 (void) printf("zdb_blkptr_cb: " 2421 "Got error %d reading " 2422 "<%llu, %llu, %lld, %llx> %s -- skipping\n", 2423 ioerr, 2424 (u_longlong_t)zb->zb_objset, 2425 (u_longlong_t)zb->zb_object, 2426 (u_longlong_t)zb->zb_level, 2427 (u_longlong_t)zb->zb_blkid, 2428 blkbuf); 2429 } 2430 mutex_exit(&spa->spa_scrub_lock); 2431} 2432 2433/* ARGSUSED */ 2434static int 2435zdb_blkptr_cb(spa_t *spa, zilog_t *zilog, const blkptr_t *bp, 2436 const zbookmark_phys_t *zb, const dnode_phys_t *dnp, void *arg) 2437{ 2438 zdb_cb_t *zcb = arg; 2439 dmu_object_type_t type; 2440 boolean_t is_metadata; 2441 2442 if (bp == NULL) 2443 return (0); 2444 2445 if (dump_opt['b'] >= 5 && bp->blk_birth > 0) { 2446 char blkbuf[BP_SPRINTF_LEN]; 2447 snprintf_blkptr(blkbuf, sizeof (blkbuf), bp); 2448 (void) printf("objset %llu object %llu " 2449 "level %lld offset 0x%llx %s\n", 2450 (u_longlong_t)zb->zb_objset, 2451 (u_longlong_t)zb->zb_object, 2452 (longlong_t)zb->zb_level, 2453 (u_longlong_t)blkid2offset(dnp, bp, zb), 2454 blkbuf); 2455 } 2456 2457 if (BP_IS_HOLE(bp)) 2458 return (0); 2459 2460 type = BP_GET_TYPE(bp); 2461 2462 zdb_count_block(zcb, zilog, bp, 2463 (type & DMU_OT_NEWTYPE) ? ZDB_OT_OTHER : type); 2464 2465 is_metadata = (BP_GET_LEVEL(bp) != 0 || DMU_OT_IS_METADATA(type)); 2466 2467 if (!BP_IS_EMBEDDED(bp) && 2468 (dump_opt['c'] > 1 || (dump_opt['c'] && is_metadata))) { 2469 size_t size = BP_GET_PSIZE(bp); 2470 void *data = zio_data_buf_alloc(size); 2471 int flags = ZIO_FLAG_CANFAIL | ZIO_FLAG_SCRUB | ZIO_FLAG_RAW; 2472 2473 /* If it's an intent log block, failure is expected. */ 2474 if (zb->zb_level == ZB_ZIL_LEVEL) 2475 flags |= ZIO_FLAG_SPECULATIVE; 2476 2477 mutex_enter(&spa->spa_scrub_lock); 2478 while (spa->spa_scrub_inflight > max_inflight) 2479 cv_wait(&spa->spa_scrub_io_cv, &spa->spa_scrub_lock); 2480 spa->spa_scrub_inflight++; 2481 mutex_exit(&spa->spa_scrub_lock); 2482 2483 zio_nowait(zio_read(NULL, spa, bp, data, size, 2484 zdb_blkptr_done, zcb, ZIO_PRIORITY_ASYNC_READ, flags, zb)); 2485 } 2486 2487 zcb->zcb_readfails = 0; 2488 2489 /* only call gethrtime() every 100 blocks */ 2490 static int iters; 2491 if (++iters > 100) 2492 iters = 0; 2493 else 2494 return (0); 2495 2496 if (dump_opt['b'] < 5 && gethrtime() > zcb->zcb_lastprint + NANOSEC) { 2497 uint64_t now = gethrtime(); 2498 char buf[10]; 2499 uint64_t bytes = zcb->zcb_type[ZB_TOTAL][ZDB_OT_TOTAL].zb_asize; 2500 int kb_per_sec = 2501 1 + bytes / (1 + ((now - zcb->zcb_start) / 1000 / 1000)); 2502 int sec_remaining = 2503 (zcb->zcb_totalasize - bytes) / 1024 / kb_per_sec; 2504 2505 zfs_nicenum(bytes, buf, sizeof (buf)); 2506 (void) fprintf(stderr, 2507 "\r%5s completed (%4dMB/s) " 2508 "estimated time remaining: %uhr %02umin %02usec ", 2509 buf, kb_per_sec / 1024, 2510 sec_remaining / 60 / 60, 2511 sec_remaining / 60 % 60, 2512 sec_remaining % 60); 2513 2514 zcb->zcb_lastprint = now; 2515 } 2516 2517 return (0); 2518} 2519 2520static void 2521zdb_leak(void *arg, uint64_t start, uint64_t size) 2522{ 2523 vdev_t *vd = arg; 2524 2525 (void) printf("leaked space: vdev %llu, offset 0x%llx, size %llu\n", 2526 (u_longlong_t)vd->vdev_id, (u_longlong_t)start, (u_longlong_t)size); 2527} 2528 2529static metaslab_ops_t zdb_metaslab_ops = { 2530 NULL /* alloc */ 2531}; 2532 2533static void 2534zdb_ddt_leak_init(spa_t *spa, zdb_cb_t *zcb) 2535{ 2536 ddt_bookmark_t ddb = { 0 }; 2537 ddt_entry_t dde; 2538 int error; 2539 2540 while ((error = ddt_walk(spa, &ddb, &dde)) == 0) { 2541 blkptr_t blk; 2542 ddt_phys_t *ddp = dde.dde_phys; 2543 2544 if (ddb.ddb_class == DDT_CLASS_UNIQUE) 2545 return; 2546 2547 ASSERT(ddt_phys_total_refcnt(&dde) > 1); 2548 2549 for (int p = 0; p < DDT_PHYS_TYPES; p++, ddp++) { 2550 if (ddp->ddp_phys_birth == 0) 2551 continue; 2552 ddt_bp_create(ddb.ddb_checksum, 2553 &dde.dde_key, ddp, &blk); 2554 if (p == DDT_PHYS_DITTO) { 2555 zdb_count_block(zcb, NULL, &blk, ZDB_OT_DITTO); 2556 } else { 2557 zcb->zcb_dedup_asize += 2558 BP_GET_ASIZE(&blk) * (ddp->ddp_refcnt - 1); 2559 zcb->zcb_dedup_blocks++; 2560 } 2561 } 2562 if (!dump_opt['L']) { 2563 ddt_t *ddt = spa->spa_ddt[ddb.ddb_checksum]; 2564 ddt_enter(ddt); 2565 VERIFY(ddt_lookup(ddt, &blk, B_TRUE) != NULL); 2566 ddt_exit(ddt); 2567 } 2568 } 2569 2570 ASSERT(error == ENOENT); 2571} 2572 2573static void 2574zdb_leak_init(spa_t *spa, zdb_cb_t *zcb) 2575{ 2576 zcb->zcb_spa = spa; 2577 2578 if (!dump_opt['L']) { 2579 vdev_t *rvd = spa->spa_root_vdev; 2580 for (uint64_t c = 0; c < rvd->vdev_children; c++) { 2581 vdev_t *vd = rvd->vdev_child[c]; 2582 for (uint64_t m = 0; m < vd->vdev_ms_count; m++) { 2583 metaslab_t *msp = vd->vdev_ms[m]; 2584 mutex_enter(&msp->ms_lock); 2585 metaslab_unload(msp); 2586 2587 /* 2588 * For leak detection, we overload the metaslab 2589 * ms_tree to contain allocated segments 2590 * instead of free segments. As a result, 2591 * we can't use the normal metaslab_load/unload 2592 * interfaces. 2593 */ 2594 if (msp->ms_sm != NULL) { 2595 (void) fprintf(stderr, 2596 "\rloading space map for " 2597 "vdev %llu of %llu, " 2598 "metaslab %llu of %llu ...", 2599 (longlong_t)c, 2600 (longlong_t)rvd->vdev_children, 2601 (longlong_t)m, 2602 (longlong_t)vd->vdev_ms_count); 2603 2604 msp->ms_ops = &zdb_metaslab_ops; 2605 2606 /* 2607 * We don't want to spend the CPU 2608 * manipulating the size-ordered 2609 * tree, so clear the range_tree 2610 * ops. 2611 */ 2612 msp->ms_tree->rt_ops = NULL; 2613 VERIFY0(space_map_load(msp->ms_sm, 2614 msp->ms_tree, SM_ALLOC)); 2615 msp->ms_loaded = B_TRUE; 2616 } 2617 mutex_exit(&msp->ms_lock); 2618 } 2619 } 2620 (void) fprintf(stderr, "\n"); 2621 } 2622 2623 spa_config_enter(spa, SCL_CONFIG, FTAG, RW_READER); 2624 2625 zdb_ddt_leak_init(spa, zcb); 2626 2627 spa_config_exit(spa, SCL_CONFIG, FTAG); 2628} 2629 2630static void 2631zdb_leak_fini(spa_t *spa) 2632{ 2633 if (!dump_opt['L']) { 2634 vdev_t *rvd = spa->spa_root_vdev; 2635 for (int c = 0; c < rvd->vdev_children; c++) { 2636 vdev_t *vd = rvd->vdev_child[c]; 2637 for (int m = 0; m < vd->vdev_ms_count; m++) { 2638 metaslab_t *msp = vd->vdev_ms[m]; 2639 mutex_enter(&msp->ms_lock); 2640 2641 /* 2642 * The ms_tree has been overloaded to 2643 * contain allocated segments. Now that we 2644 * finished traversing all blocks, any 2645 * block that remains in the ms_tree 2646 * represents an allocated block that we 2647 * did not claim during the traversal. 2648 * Claimed blocks would have been removed 2649 * from the ms_tree. 2650 */ 2651 range_tree_vacate(msp->ms_tree, zdb_leak, vd); 2652 msp->ms_loaded = B_FALSE; 2653 2654 mutex_exit(&msp->ms_lock); 2655 } 2656 } 2657 } 2658} 2659 2660/* ARGSUSED */ 2661static int 2662count_block_cb(void *arg, const blkptr_t *bp, dmu_tx_t *tx) 2663{ 2664 zdb_cb_t *zcb = arg; 2665 2666 if (dump_opt['b'] >= 5) { 2667 char blkbuf[BP_SPRINTF_LEN]; 2668 snprintf_blkptr(blkbuf, sizeof (blkbuf), bp); 2669 (void) printf("[%s] %s\n", 2670 "deferred free", blkbuf); 2671 } 2672 zdb_count_block(zcb, NULL, bp, ZDB_OT_DEFERRED); 2673 return (0); 2674} 2675 2676static int 2677dump_block_stats(spa_t *spa) 2678{ 2679 zdb_cb_t zcb = { 0 }; 2680 zdb_blkstats_t *zb, *tzb; 2681 uint64_t norm_alloc, norm_space, total_alloc, total_found; 2682 int flags = TRAVERSE_PRE | TRAVERSE_PREFETCH_METADATA | TRAVERSE_HARD; 2683 boolean_t leaks = B_FALSE; 2684 2685 (void) printf("\nTraversing all blocks %s%s%s%s%s...\n\n", 2686 (dump_opt['c'] || !dump_opt['L']) ? "to verify " : "", 2687 (dump_opt['c'] == 1) ? "metadata " : "", 2688 dump_opt['c'] ? "checksums " : "", 2689 (dump_opt['c'] && !dump_opt['L']) ? "and verify " : "", 2690 !dump_opt['L'] ? "nothing leaked " : ""); 2691 2692 /* 2693 * Load all space maps as SM_ALLOC maps, then traverse the pool 2694 * claiming each block we discover. If the pool is perfectly 2695 * consistent, the space maps will be empty when we're done. 2696 * Anything left over is a leak; any block we can't claim (because 2697 * it's not part of any space map) is a double allocation, 2698 * reference to a freed block, or an unclaimed log block. 2699 */ 2700 zdb_leak_init(spa, &zcb); 2701 2702 /* 2703 * If there's a deferred-free bplist, process that first. 2704 */ 2705 (void) bpobj_iterate_nofree(&spa->spa_deferred_bpobj, 2706 count_block_cb, &zcb, NULL); 2707 if (spa_version(spa) >= SPA_VERSION_DEADLISTS) { 2708 (void) bpobj_iterate_nofree(&spa->spa_dsl_pool->dp_free_bpobj, 2709 count_block_cb, &zcb, NULL); 2710 } 2711 if (spa_feature_is_active(spa, SPA_FEATURE_ASYNC_DESTROY)) { 2712 VERIFY3U(0, ==, bptree_iterate(spa->spa_meta_objset, 2713 spa->spa_dsl_pool->dp_bptree_obj, B_FALSE, count_block_cb, 2714 &zcb, NULL)); 2715 } 2716 2717 if (dump_opt['c'] > 1) 2718 flags |= TRAVERSE_PREFETCH_DATA; 2719 2720 zcb.zcb_totalasize = metaslab_class_get_alloc(spa_normal_class(spa)); 2721 zcb.zcb_start = zcb.zcb_lastprint = gethrtime(); 2722 zcb.zcb_haderrors |= traverse_pool(spa, 0, flags, zdb_blkptr_cb, &zcb); 2723 2724 /* 2725 * If we've traversed the data blocks then we need to wait for those 2726 * I/Os to complete. We leverage "The Godfather" zio to wait on 2727 * all async I/Os to complete. 2728 */ 2729 if (dump_opt['c']) { 2730 for (int i = 0; i < max_ncpus; i++) { 2731 (void) zio_wait(spa->spa_async_zio_root[i]); 2732 spa->spa_async_zio_root[i] = zio_root(spa, NULL, NULL, 2733 ZIO_FLAG_CANFAIL | ZIO_FLAG_SPECULATIVE | 2734 ZIO_FLAG_GODFATHER); 2735 } 2736 } 2737 2738 if (zcb.zcb_haderrors) { 2739 (void) printf("\nError counts:\n\n"); 2740 (void) printf("\t%5s %s\n", "errno", "count"); 2741 for (int e = 0; e < 256; e++) { 2742 if (zcb.zcb_errors[e] != 0) { 2743 (void) printf("\t%5d %llu\n", 2744 e, (u_longlong_t)zcb.zcb_errors[e]); 2745 } 2746 } 2747 } 2748 2749 /* 2750 * Report any leaked segments. 2751 */ 2752 zdb_leak_fini(spa); 2753 2754 tzb = &zcb.zcb_type[ZB_TOTAL][ZDB_OT_TOTAL]; 2755 2756 norm_alloc = metaslab_class_get_alloc(spa_normal_class(spa)); 2757 norm_space = metaslab_class_get_space(spa_normal_class(spa)); 2758 2759 total_alloc = norm_alloc + metaslab_class_get_alloc(spa_log_class(spa)); 2760 total_found = tzb->zb_asize - zcb.zcb_dedup_asize; 2761 2762 if (total_found == total_alloc) { 2763 if (!dump_opt['L']) 2764 (void) printf("\n\tNo leaks (block sum matches space" 2765 " maps exactly)\n"); 2766 } else { 2767 (void) printf("block traversal size %llu != alloc %llu " 2768 "(%s %lld)\n", 2769 (u_longlong_t)total_found, 2770 (u_longlong_t)total_alloc, 2771 (dump_opt['L']) ? "unreachable" : "leaked", 2772 (longlong_t)(total_alloc - total_found)); 2773 leaks = B_TRUE; 2774 } 2775 2776 if (tzb->zb_count == 0) 2777 return (2); 2778 2779 (void) printf("\n"); 2780 (void) printf("\tbp count: %10llu\n", 2781 (u_longlong_t)tzb->zb_count); 2782 (void) printf("\tganged count: %10llu\n", 2783 (longlong_t)tzb->zb_gangs); 2784 (void) printf("\tbp logical: %10llu avg: %6llu\n", 2785 (u_longlong_t)tzb->zb_lsize, 2786 (u_longlong_t)(tzb->zb_lsize / tzb->zb_count)); 2787 (void) printf("\tbp physical: %10llu avg:" 2788 " %6llu compression: %6.2f\n", 2789 (u_longlong_t)tzb->zb_psize, 2790 (u_longlong_t)(tzb->zb_psize / tzb->zb_count), 2791 (double)tzb->zb_lsize / tzb->zb_psize); 2792 (void) printf("\tbp allocated: %10llu avg:" 2793 " %6llu compression: %6.2f\n", 2794 (u_longlong_t)tzb->zb_asize, 2795 (u_longlong_t)(tzb->zb_asize / tzb->zb_count), 2796 (double)tzb->zb_lsize / tzb->zb_asize); 2797 (void) printf("\tbp deduped: %10llu ref>1:" 2798 " %6llu deduplication: %6.2f\n", 2799 (u_longlong_t)zcb.zcb_dedup_asize, 2800 (u_longlong_t)zcb.zcb_dedup_blocks, 2801 (double)zcb.zcb_dedup_asize / tzb->zb_asize + 1.0); 2802 (void) printf("\tSPA allocated: %10llu used: %5.2f%%\n", 2803 (u_longlong_t)norm_alloc, 100.0 * norm_alloc / norm_space); 2804 2805 for (bp_embedded_type_t i = 0; i < NUM_BP_EMBEDDED_TYPES; i++) { 2806 if (zcb.zcb_embedded_blocks[i] == 0) 2807 continue; 2808 (void) printf("\n"); 2809 (void) printf("\tadditional, non-pointer bps of type %u: " 2810 "%10llu\n", 2811 i, (u_longlong_t)zcb.zcb_embedded_blocks[i]); 2812 2813 if (dump_opt['b'] >= 3) { 2814 (void) printf("\t number of (compressed) bytes: " 2815 "number of bps\n"); 2816 dump_histogram(zcb.zcb_embedded_histogram[i], 2817 sizeof (zcb.zcb_embedded_histogram[i]) / 2818 sizeof (zcb.zcb_embedded_histogram[i][0]), 0); 2819 } 2820 } 2821 2822 if (tzb->zb_ditto_samevdev != 0) { 2823 (void) printf("\tDittoed blocks on same vdev: %llu\n", 2824 (longlong_t)tzb->zb_ditto_samevdev); 2825 } 2826 2827 if (dump_opt['b'] >= 2) { 2828 int l, t, level; 2829 (void) printf("\nBlocks\tLSIZE\tPSIZE\tASIZE" 2830 "\t avg\t comp\t%%Total\tType\n"); 2831 2832 for (t = 0; t <= ZDB_OT_TOTAL; t++) { 2833 char csize[32], lsize[32], psize[32], asize[32]; 2834 char avg[32], gang[32]; 2835 char *typename; 2836 2837 if (t < DMU_OT_NUMTYPES) 2838 typename = dmu_ot[t].ot_name; 2839 else 2840 typename = zdb_ot_extname[t - DMU_OT_NUMTYPES]; 2841 2842 if (zcb.zcb_type[ZB_TOTAL][t].zb_asize == 0) { 2843 (void) printf("%6s\t%5s\t%5s\t%5s" 2844 "\t%5s\t%5s\t%6s\t%s\n", 2845 "-", 2846 "-", 2847 "-", 2848 "-", 2849 "-", 2850 "-", 2851 "-", 2852 typename); 2853 continue; 2854 } 2855 2856 for (l = ZB_TOTAL - 1; l >= -1; l--) { 2857 level = (l == -1 ? ZB_TOTAL : l); 2858 zb = &zcb.zcb_type[level][t]; 2859 2860 if (zb->zb_asize == 0) 2861 continue; 2862 2863 if (dump_opt['b'] < 3 && level != ZB_TOTAL) 2864 continue; 2865 2866 if (level == 0 && zb->zb_asize == 2867 zcb.zcb_type[ZB_TOTAL][t].zb_asize) 2868 continue; 2869 2870 zdb_nicenum(zb->zb_count, csize); 2871 zdb_nicenum(zb->zb_lsize, lsize); 2872 zdb_nicenum(zb->zb_psize, psize); 2873 zdb_nicenum(zb->zb_asize, asize); 2874 zdb_nicenum(zb->zb_asize / zb->zb_count, avg); 2875 zdb_nicenum(zb->zb_gangs, gang); 2876 2877 (void) printf("%6s\t%5s\t%5s\t%5s\t%5s" 2878 "\t%5.2f\t%6.2f\t", 2879 csize, lsize, psize, asize, avg, 2880 (double)zb->zb_lsize / zb->zb_psize, 2881 100.0 * zb->zb_asize / tzb->zb_asize); 2882 2883 if (level == ZB_TOTAL) 2884 (void) printf("%s\n", typename); 2885 else 2886 (void) printf(" L%d %s\n", 2887 level, typename); 2888 2889 if (dump_opt['b'] >= 3 && zb->zb_gangs > 0) { 2890 (void) printf("\t number of ganged " 2891 "blocks: %s\n", gang); 2892 } 2893 2894 if (dump_opt['b'] >= 4) { 2895 (void) printf("psize " 2896 "(in 512-byte sectors): " 2897 "number of blocks\n"); 2898 dump_histogram(zb->zb_psize_histogram, 2899 PSIZE_HISTO_SIZE, 0); 2900 } 2901 } 2902 } 2903 } 2904 2905 (void) printf("\n"); 2906 2907 if (leaks) 2908 return (2); 2909 2910 if (zcb.zcb_haderrors) 2911 return (3); 2912 2913 return (0); 2914} 2915 2916typedef struct zdb_ddt_entry { 2917 ddt_key_t zdde_key; 2918 uint64_t zdde_ref_blocks; 2919 uint64_t zdde_ref_lsize; 2920 uint64_t zdde_ref_psize; 2921 uint64_t zdde_ref_dsize; 2922 avl_node_t zdde_node; 2923} zdb_ddt_entry_t; 2924 2925/* ARGSUSED */ 2926static int 2927zdb_ddt_add_cb(spa_t *spa, zilog_t *zilog, const blkptr_t *bp, 2928 const zbookmark_phys_t *zb, const dnode_phys_t *dnp, void *arg) 2929{ 2930 avl_tree_t *t = arg; 2931 avl_index_t where; 2932 zdb_ddt_entry_t *zdde, zdde_search; 2933 2934 if (bp == NULL || BP_IS_HOLE(bp) || BP_IS_EMBEDDED(bp)) 2935 return (0); 2936 2937 if (dump_opt['S'] > 1 && zb->zb_level == ZB_ROOT_LEVEL) { 2938 (void) printf("traversing objset %llu, %llu objects, " 2939 "%lu blocks so far\n", 2940 (u_longlong_t)zb->zb_objset, 2941 (u_longlong_t)BP_GET_FILL(bp), 2942 avl_numnodes(t)); 2943 } 2944 2945 if (BP_IS_HOLE(bp) || BP_GET_CHECKSUM(bp) == ZIO_CHECKSUM_OFF || 2946 BP_GET_LEVEL(bp) > 0 || DMU_OT_IS_METADATA(BP_GET_TYPE(bp))) 2947 return (0); 2948 2949 ddt_key_fill(&zdde_search.zdde_key, bp); 2950 2951 zdde = avl_find(t, &zdde_search, &where); 2952 2953 if (zdde == NULL) { 2954 zdde = umem_zalloc(sizeof (*zdde), UMEM_NOFAIL); 2955 zdde->zdde_key = zdde_search.zdde_key; 2956 avl_insert(t, zdde, where); 2957 } 2958 2959 zdde->zdde_ref_blocks += 1; 2960 zdde->zdde_ref_lsize += BP_GET_LSIZE(bp); 2961 zdde->zdde_ref_psize += BP_GET_PSIZE(bp); 2962 zdde->zdde_ref_dsize += bp_get_dsize_sync(spa, bp); 2963 2964 return (0); 2965} 2966 2967static void 2968dump_simulated_ddt(spa_t *spa) 2969{ 2970 avl_tree_t t; 2971 void *cookie = NULL; 2972 zdb_ddt_entry_t *zdde; 2973 ddt_histogram_t ddh_total = { 0 }; 2974 ddt_stat_t dds_total = { 0 }; 2975 2976 avl_create(&t, ddt_entry_compare, 2977 sizeof (zdb_ddt_entry_t), offsetof(zdb_ddt_entry_t, zdde_node)); 2978 2979 spa_config_enter(spa, SCL_CONFIG, FTAG, RW_READER); 2980 2981 (void) traverse_pool(spa, 0, TRAVERSE_PRE | TRAVERSE_PREFETCH_METADATA, 2982 zdb_ddt_add_cb, &t); 2983 2984 spa_config_exit(spa, SCL_CONFIG, FTAG); 2985 2986 while ((zdde = avl_destroy_nodes(&t, &cookie)) != NULL) { 2987 ddt_stat_t dds; 2988 uint64_t refcnt = zdde->zdde_ref_blocks; 2989 ASSERT(refcnt != 0); 2990 2991 dds.dds_blocks = zdde->zdde_ref_blocks / refcnt; 2992 dds.dds_lsize = zdde->zdde_ref_lsize / refcnt; 2993 dds.dds_psize = zdde->zdde_ref_psize / refcnt; 2994 dds.dds_dsize = zdde->zdde_ref_dsize / refcnt; 2995 2996 dds.dds_ref_blocks = zdde->zdde_ref_blocks; 2997 dds.dds_ref_lsize = zdde->zdde_ref_lsize; 2998 dds.dds_ref_psize = zdde->zdde_ref_psize; 2999 dds.dds_ref_dsize = zdde->zdde_ref_dsize; 3000 3001 ddt_stat_add(&ddh_total.ddh_stat[highbit64(refcnt) - 1], 3002 &dds, 0); 3003 3004 umem_free(zdde, sizeof (*zdde)); 3005 } 3006 3007 avl_destroy(&t); 3008 3009 ddt_histogram_stat(&dds_total, &ddh_total); 3010 3011 (void) printf("Simulated DDT histogram:\n"); 3012 3013 zpool_dump_ddt(&dds_total, &ddh_total); 3014 3015 dump_dedup_ratio(&dds_total); 3016} 3017 3018static void 3019dump_zpool(spa_t *spa) 3020{ 3021 dsl_pool_t *dp = spa_get_dsl(spa); 3022 int rc = 0; 3023 3024 if (dump_opt['S']) { 3025 dump_simulated_ddt(spa); 3026 return; 3027 } 3028 3029 if (!dump_opt['e'] && dump_opt['C'] > 1) { 3030 (void) printf("\nCached configuration:\n"); 3031 dump_nvlist(spa->spa_config, 8); 3032 } 3033 3034 if (dump_opt['C']) 3035 dump_config(spa); 3036 3037 if (dump_opt['u']) 3038 dump_uberblock(&spa->spa_uberblock, "\nUberblock:\n", "\n"); 3039 3040 if (dump_opt['D']) 3041 dump_all_ddts(spa); 3042 3043 if (dump_opt['d'] > 2 || dump_opt['m']) 3044 dump_metaslabs(spa); 3045 if (dump_opt['M']) 3046 dump_metaslab_groups(spa); 3047 3048 if (dump_opt['d'] || dump_opt['i']) { 3049 dump_dir(dp->dp_meta_objset); 3050 if (dump_opt['d'] >= 3) { 3051 dump_full_bpobj(&spa->spa_deferred_bpobj, 3052 "Deferred frees", 0); 3053 if (spa_version(spa) >= SPA_VERSION_DEADLISTS) { 3054 dump_full_bpobj( 3055 &spa->spa_dsl_pool->dp_free_bpobj, 3056 "Pool snapshot frees", 0); 3057 } 3058 3059 if (spa_feature_is_active(spa, 3060 SPA_FEATURE_ASYNC_DESTROY)) { 3061 dump_bptree(spa->spa_meta_objset, 3062 spa->spa_dsl_pool->dp_bptree_obj, 3063 "Pool dataset frees"); 3064 } 3065 dump_dtl(spa->spa_root_vdev, 0); 3066 } 3067 (void) dmu_objset_find(spa_name(spa), dump_one_dir, 3068 NULL, DS_FIND_SNAPSHOTS | DS_FIND_CHILDREN); 3069 3070 for (spa_feature_t f = 0; f < SPA_FEATURES; f++) { 3071 uint64_t refcount; 3072 3073 if (!(spa_feature_table[f].fi_flags & 3074 ZFEATURE_FLAG_PER_DATASET)) { 3075 ASSERT0(dataset_feature_count[f]); 3076 continue; 3077 } 3078 (void) feature_get_refcount(spa, 3079 &spa_feature_table[f], &refcount); 3080 if (dataset_feature_count[f] != refcount) { 3081 (void) printf("%s feature refcount mismatch: " 3082 "%lld datasets != %lld refcount\n", 3083 spa_feature_table[f].fi_uname, 3084 (longlong_t)dataset_feature_count[f], 3085 (longlong_t)refcount); 3086 rc = 2; 3087 } else { 3088 (void) printf("Verified %s feature refcount " 3089 "of %llu is correct\n", 3090 spa_feature_table[f].fi_uname, 3091 (longlong_t)refcount); 3092 } 3093 } 3094 } 3095 if (rc == 0 && (dump_opt['b'] || dump_opt['c'])) 3096 rc = dump_block_stats(spa); 3097 3098 if (rc == 0) 3099 rc = verify_spacemap_refcounts(spa); 3100 3101 if (dump_opt['s']) 3102 show_pool_stats(spa); 3103 3104 if (dump_opt['h']) 3105 dump_history(spa); 3106 3107 if (rc != 0) 3108 exit(rc); 3109} 3110 3111#define ZDB_FLAG_CHECKSUM 0x0001 3112#define ZDB_FLAG_DECOMPRESS 0x0002 3113#define ZDB_FLAG_BSWAP 0x0004 3114#define ZDB_FLAG_GBH 0x0008 3115#define ZDB_FLAG_INDIRECT 0x0010 3116#define ZDB_FLAG_PHYS 0x0020 3117#define ZDB_FLAG_RAW 0x0040 3118#define ZDB_FLAG_PRINT_BLKPTR 0x0080 3119 3120int flagbits[256]; 3121 3122static void 3123zdb_print_blkptr(blkptr_t *bp, int flags) 3124{ 3125 char blkbuf[BP_SPRINTF_LEN]; 3126 3127 if (flags & ZDB_FLAG_BSWAP) 3128 byteswap_uint64_array((void *)bp, sizeof (blkptr_t)); 3129 3130 snprintf_blkptr(blkbuf, sizeof (blkbuf), bp); 3131 (void) printf("%s\n", blkbuf); 3132} 3133 3134static void 3135zdb_dump_indirect(blkptr_t *bp, int nbps, int flags) 3136{ 3137 int i; 3138 3139 for (i = 0; i < nbps; i++) 3140 zdb_print_blkptr(&bp[i], flags); 3141} 3142 3143static void 3144zdb_dump_gbh(void *buf, int flags) 3145{ 3146 zdb_dump_indirect((blkptr_t *)buf, SPA_GBH_NBLKPTRS, flags); 3147} 3148 3149static void 3150zdb_dump_block_raw(void *buf, uint64_t size, int flags) 3151{ 3152 if (flags & ZDB_FLAG_BSWAP) 3153 byteswap_uint64_array(buf, size); 3154 (void) write(1, buf, size); 3155} 3156 3157static void 3158zdb_dump_block(char *label, void *buf, uint64_t size, int flags) 3159{ 3160 uint64_t *d = (uint64_t *)buf; 3161 int nwords = size / sizeof (uint64_t); 3162 int do_bswap = !!(flags & ZDB_FLAG_BSWAP); 3163 int i, j; 3164 char *hdr, *c; 3165 3166 3167 if (do_bswap) 3168 hdr = " 7 6 5 4 3 2 1 0 f e d c b a 9 8"; 3169 else 3170 hdr = " 0 1 2 3 4 5 6 7 8 9 a b c d e f"; 3171 3172 (void) printf("\n%s\n%6s %s 0123456789abcdef\n", label, "", hdr); 3173 3174 for (i = 0; i < nwords; i += 2) { 3175 (void) printf("%06llx: %016llx %016llx ", 3176 (u_longlong_t)(i * sizeof (uint64_t)), 3177 (u_longlong_t)(do_bswap ? BSWAP_64(d[i]) : d[i]), 3178 (u_longlong_t)(do_bswap ? BSWAP_64(d[i + 1]) : d[i + 1])); 3179 3180 c = (char *)&d[i]; 3181 for (j = 0; j < 2 * sizeof (uint64_t); j++) 3182 (void) printf("%c", isprint(c[j]) ? c[j] : '.'); 3183 (void) printf("\n"); 3184 } 3185} 3186 3187/* 3188 * There are two acceptable formats: 3189 * leaf_name - For example: c1t0d0 or /tmp/ztest.0a 3190 * child[.child]* - For example: 0.1.1 3191 * 3192 * The second form can be used to specify arbitrary vdevs anywhere 3193 * in the heirarchy. For example, in a pool with a mirror of 3194 * RAID-Zs, you can specify either RAID-Z vdev with 0.0 or 0.1 . 3195 */ 3196static vdev_t * 3197zdb_vdev_lookup(vdev_t *vdev, char *path) 3198{ 3199 char *s, *p, *q; 3200 int i; 3201 3202 if (vdev == NULL) 3203 return (NULL); 3204 3205 /* First, assume the x.x.x.x format */ 3206 i = (int)strtoul(path, &s, 10); 3207 if (s == path || (s && *s != '.' && *s != '\0')) 3208 goto name; 3209 if (i < 0 || i >= vdev->vdev_children) 3210 return (NULL); 3211 3212 vdev = vdev->vdev_child[i]; 3213 if (*s == '\0') 3214 return (vdev); 3215 return (zdb_vdev_lookup(vdev, s+1)); 3216 3217name: 3218 for (i = 0; i < vdev->vdev_children; i++) { 3219 vdev_t *vc = vdev->vdev_child[i]; 3220 3221 if (vc->vdev_path == NULL) { 3222 vc = zdb_vdev_lookup(vc, path); 3223 if (vc == NULL) 3224 continue; 3225 else 3226 return (vc); 3227 } 3228 3229 p = strrchr(vc->vdev_path, '/'); 3230 p = p ? p + 1 : vc->vdev_path; 3231 q = &vc->vdev_path[strlen(vc->vdev_path) - 2]; 3232 3233 if (strcmp(vc->vdev_path, path) == 0) 3234 return (vc); 3235 if (strcmp(p, path) == 0) 3236 return (vc); 3237 if (strcmp(q, "s0") == 0 && strncmp(p, path, q - p) == 0) 3238 return (vc); 3239 } 3240 3241 return (NULL); 3242} 3243 3244/* 3245 * Read a block from a pool and print it out. The syntax of the 3246 * block descriptor is: 3247 * 3248 * pool:vdev_specifier:offset:size[:flags] 3249 * 3250 * pool - The name of the pool you wish to read from 3251 * vdev_specifier - Which vdev (see comment for zdb_vdev_lookup) 3252 * offset - offset, in hex, in bytes 3253 * size - Amount of data to read, in hex, in bytes 3254 * flags - A string of characters specifying options 3255 * b: Decode a blkptr at given offset within block 3256 * *c: Calculate and display checksums 3257 * d: Decompress data before dumping 3258 * e: Byteswap data before dumping 3259 * g: Display data as a gang block header 3260 * i: Display as an indirect block 3261 * p: Do I/O to physical offset 3262 * r: Dump raw data to stdout 3263 * 3264 * * = not yet implemented 3265 */ 3266static void 3267zdb_read_block(char *thing, spa_t *spa) 3268{ 3269 blkptr_t blk, *bp = &blk; 3270 dva_t *dva = bp->blk_dva; 3271 int flags = 0; 3272 uint64_t offset = 0, size = 0, psize = 0, lsize = 0, blkptr_offset = 0; 3273 zio_t *zio; 3274 vdev_t *vd; 3275 void *pbuf, *lbuf, *buf; 3276 char *s, *p, *dup, *vdev, *flagstr; 3277 int i, error; 3278 3279 dup = strdup(thing); 3280 s = strtok(dup, ":"); 3281 vdev = s ? s : ""; 3282 s = strtok(NULL, ":"); 3283 offset = strtoull(s ? s : "", NULL, 16); 3284 s = strtok(NULL, ":"); 3285 size = strtoull(s ? s : "", NULL, 16); 3286 s = strtok(NULL, ":"); 3287 flagstr = s ? s : ""; 3288 3289 s = NULL; 3290 if (size == 0) 3291 s = "size must not be zero"; 3292 if (!IS_P2ALIGNED(size, DEV_BSIZE)) 3293 s = "size must be a multiple of sector size"; 3294 if (!IS_P2ALIGNED(offset, DEV_BSIZE)) 3295 s = "offset must be a multiple of sector size"; 3296 if (s) { 3297 (void) printf("Invalid block specifier: %s - %s\n", thing, s); 3298 free(dup); 3299 return; 3300 } 3301 3302 for (s = strtok(flagstr, ":"); s; s = strtok(NULL, ":")) { 3303 for (i = 0; flagstr[i]; i++) { 3304 int bit = flagbits[(uchar_t)flagstr[i]]; 3305 3306 if (bit == 0) { 3307 (void) printf("***Invalid flag: %c\n", 3308 flagstr[i]); 3309 continue; 3310 } 3311 flags |= bit; 3312 3313 /* If it's not something with an argument, keep going */ 3314 if ((bit & (ZDB_FLAG_CHECKSUM | 3315 ZDB_FLAG_PRINT_BLKPTR)) == 0) 3316 continue; 3317 3318 p = &flagstr[i + 1]; 3319 if (bit == ZDB_FLAG_PRINT_BLKPTR) 3320 blkptr_offset = strtoull(p, &p, 16); 3321 if (*p != ':' && *p != '\0') { 3322 (void) printf("***Invalid flag arg: '%s'\n", s); 3323 free(dup); 3324 return; 3325 } 3326 i += p - &flagstr[i + 1]; /* skip over the number */ 3327 } 3328 } 3329 3330 vd = zdb_vdev_lookup(spa->spa_root_vdev, vdev); 3331 if (vd == NULL) { 3332 (void) printf("***Invalid vdev: %s\n", vdev); 3333 free(dup); 3334 return; 3335 } else { 3336 if (vd->vdev_path) 3337 (void) fprintf(stderr, "Found vdev: %s\n", 3338 vd->vdev_path); 3339 else 3340 (void) fprintf(stderr, "Found vdev type: %s\n", 3341 vd->vdev_ops->vdev_op_type); 3342 } 3343 3344 psize = size; 3345 lsize = size; 3346 3347 pbuf = umem_alloc(SPA_MAXBLOCKSIZE, UMEM_NOFAIL); 3348 lbuf = umem_alloc(SPA_MAXBLOCKSIZE, UMEM_NOFAIL); 3349 3350 BP_ZERO(bp); 3351 3352 DVA_SET_VDEV(&dva[0], vd->vdev_id); 3353 DVA_SET_OFFSET(&dva[0], offset); 3354 DVA_SET_GANG(&dva[0], !!(flags & ZDB_FLAG_GBH)); 3355 DVA_SET_ASIZE(&dva[0], vdev_psize_to_asize(vd, psize)); 3356 3357 BP_SET_BIRTH(bp, TXG_INITIAL, TXG_INITIAL); 3358 3359 BP_SET_LSIZE(bp, lsize); 3360 BP_SET_PSIZE(bp, psize); 3361 BP_SET_COMPRESS(bp, ZIO_COMPRESS_OFF); 3362 BP_SET_CHECKSUM(bp, ZIO_CHECKSUM_OFF); 3363 BP_SET_TYPE(bp, DMU_OT_NONE); 3364 BP_SET_LEVEL(bp, 0); 3365 BP_SET_DEDUP(bp, 0); 3366 BP_SET_BYTEORDER(bp, ZFS_HOST_BYTEORDER); 3367 3368 spa_config_enter(spa, SCL_STATE, FTAG, RW_READER); 3369 zio = zio_root(spa, NULL, NULL, 0); 3370 3371 if (vd == vd->vdev_top) { 3372 /* 3373 * Treat this as a normal block read. 3374 */ 3375 zio_nowait(zio_read(zio, spa, bp, pbuf, psize, NULL, NULL, 3376 ZIO_PRIORITY_SYNC_READ, 3377 ZIO_FLAG_CANFAIL | ZIO_FLAG_RAW, NULL)); 3378 } else { 3379 /* 3380 * Treat this as a vdev child I/O. 3381 */ 3382 zio_nowait(zio_vdev_child_io(zio, bp, vd, offset, pbuf, psize, 3383 ZIO_TYPE_READ, ZIO_PRIORITY_SYNC_READ, 3384 ZIO_FLAG_DONT_CACHE | ZIO_FLAG_DONT_QUEUE | 3385 ZIO_FLAG_DONT_PROPAGATE | ZIO_FLAG_DONT_RETRY | 3386 ZIO_FLAG_CANFAIL | ZIO_FLAG_RAW, NULL, NULL)); 3387 } 3388 3389 error = zio_wait(zio); 3390 spa_config_exit(spa, SCL_STATE, FTAG); 3391 3392 if (error) { 3393 (void) printf("Read of %s failed, error: %d\n", thing, error); 3394 goto out; 3395 } 3396 3397 if (flags & ZDB_FLAG_DECOMPRESS) { 3398 /* 3399 * We don't know how the data was compressed, so just try 3400 * every decompress function at every inflated blocksize. 3401 */ 3402 enum zio_compress c; 3403 void *pbuf2 = umem_alloc(SPA_MAXBLOCKSIZE, UMEM_NOFAIL); 3404 void *lbuf2 = umem_alloc(SPA_MAXBLOCKSIZE, UMEM_NOFAIL); 3405 3406 bcopy(pbuf, pbuf2, psize); 3407 3408 VERIFY(random_get_pseudo_bytes((uint8_t *)pbuf + psize, 3409 SPA_MAXBLOCKSIZE - psize) == 0); 3410 3411 VERIFY(random_get_pseudo_bytes((uint8_t *)pbuf2 + psize, 3412 SPA_MAXBLOCKSIZE - psize) == 0); 3413 3414 for (lsize = SPA_MAXBLOCKSIZE; lsize > psize; 3415 lsize -= SPA_MINBLOCKSIZE) { 3416 for (c = 0; c < ZIO_COMPRESS_FUNCTIONS; c++) { 3417 if (zio_decompress_data(c, pbuf, lbuf, 3418 psize, lsize) == 0 && 3419 zio_decompress_data(c, pbuf2, lbuf2, 3420 psize, lsize) == 0 && 3421 bcmp(lbuf, lbuf2, lsize) == 0) 3422 break; 3423 } 3424 if (c != ZIO_COMPRESS_FUNCTIONS) 3425 break; 3426 lsize -= SPA_MINBLOCKSIZE; 3427 } 3428 3429 umem_free(pbuf2, SPA_MAXBLOCKSIZE); 3430 umem_free(lbuf2, SPA_MAXBLOCKSIZE); 3431 3432 if (lsize <= psize) { 3433 (void) printf("Decompress of %s failed\n", thing); 3434 goto out; 3435 } 3436 buf = lbuf; 3437 size = lsize; 3438 } else { 3439 buf = pbuf; 3440 size = psize; 3441 } 3442 3443 if (flags & ZDB_FLAG_PRINT_BLKPTR) 3444 zdb_print_blkptr((blkptr_t *)(void *) 3445 ((uintptr_t)buf + (uintptr_t)blkptr_offset), flags); 3446 else if (flags & ZDB_FLAG_RAW) 3447 zdb_dump_block_raw(buf, size, flags); 3448 else if (flags & ZDB_FLAG_INDIRECT) 3449 zdb_dump_indirect((blkptr_t *)buf, size / sizeof (blkptr_t), 3450 flags); 3451 else if (flags & ZDB_FLAG_GBH) 3452 zdb_dump_gbh(buf, flags); 3453 else 3454 zdb_dump_block(thing, buf, size, flags); 3455 3456out: 3457 umem_free(pbuf, SPA_MAXBLOCKSIZE); 3458 umem_free(lbuf, SPA_MAXBLOCKSIZE); 3459 free(dup); 3460} 3461 3462static boolean_t 3463pool_match(nvlist_t *cfg, char *tgt) 3464{ 3465 uint64_t v, guid = strtoull(tgt, NULL, 0); 3466 char *s; 3467 3468 if (guid != 0) { 3469 if (nvlist_lookup_uint64(cfg, ZPOOL_CONFIG_POOL_GUID, &v) == 0) 3470 return (v == guid); 3471 } else { 3472 if (nvlist_lookup_string(cfg, ZPOOL_CONFIG_POOL_NAME, &s) == 0) 3473 return (strcmp(s, tgt) == 0); 3474 } 3475 return (B_FALSE); 3476} 3477 3478static char * 3479find_zpool(char **target, nvlist_t **configp, int dirc, char **dirv) 3480{ 3481 nvlist_t *pools; 3482 nvlist_t *match = NULL; 3483 char *name = NULL; 3484 char *sepp = NULL; 3485 char sep = '\0'; 3486 int count = 0; 3487 importargs_t args = { 0 }; 3488 3489 args.paths = dirc; 3490 args.path = dirv; 3491 args.can_be_active = B_TRUE; 3492 3493 if ((sepp = strpbrk(*target, "/@")) != NULL) { 3494 sep = *sepp; 3495 *sepp = '\0'; 3496 } 3497 3498 pools = zpool_search_import(g_zfs, &args); 3499 3500 if (pools != NULL) { 3501 nvpair_t *elem = NULL; 3502 while ((elem = nvlist_next_nvpair(pools, elem)) != NULL) { 3503 verify(nvpair_value_nvlist(elem, configp) == 0); 3504 if (pool_match(*configp, *target)) { 3505 count++; 3506 if (match != NULL) { 3507 /* print previously found config */ 3508 if (name != NULL) { 3509 (void) printf("%s\n", name); 3510 dump_nvlist(match, 8); 3511 name = NULL; 3512 } 3513 (void) printf("%s\n", 3514 nvpair_name(elem)); 3515 dump_nvlist(*configp, 8); 3516 } else { 3517 match = *configp; 3518 name = nvpair_name(elem); 3519 } 3520 } 3521 } 3522 } 3523 if (count > 1) 3524 (void) fatal("\tMatched %d pools - use pool GUID " 3525 "instead of pool name or \n" 3526 "\tpool name part of a dataset name to select pool", count); 3527 3528 if (sepp) 3529 *sepp = sep; 3530 /* 3531 * If pool GUID was specified for pool id, replace it with pool name 3532 */ 3533 if (name && (strstr(*target, name) != *target)) { 3534 int sz = 1 + strlen(name) + ((sepp) ? strlen(sepp) : 0); 3535 3536 *target = umem_alloc(sz, UMEM_NOFAIL); 3537 (void) snprintf(*target, sz, "%s%s", name, sepp ? sepp : ""); 3538 } 3539 3540 *configp = name ? match : NULL; 3541 3542 return (name); 3543} 3544 3545int 3546main(int argc, char **argv) 3547{ 3548 int i, c; 3549 struct rlimit rl = { 1024, 1024 }; 3550 spa_t *spa = NULL; 3551 objset_t *os = NULL; 3552 int dump_all = 1; 3553 int verbose = 0; 3554 int error = 0; 3555 char **searchdirs = NULL; 3556 int nsearch = 0; 3557 char *target; 3558 nvlist_t *policy = NULL; 3559 uint64_t max_txg = UINT64_MAX; 3560 int rewind = ZPOOL_NEVER_REWIND; 3561 char *spa_config_path_env; 3562 boolean_t target_is_spa = B_TRUE; 3563 3564 (void) setrlimit(RLIMIT_NOFILE, &rl); 3565 (void) enable_extended_FILE_stdio(-1, -1); 3566 3567 dprintf_setup(&argc, argv); 3568 3569 /* 3570 * If there is an environment variable SPA_CONFIG_PATH it overrides 3571 * default spa_config_path setting. If -U flag is specified it will 3572 * override this environment variable settings once again. 3573 */ 3574 spa_config_path_env = getenv("SPA_CONFIG_PATH"); 3575 if (spa_config_path_env != NULL) 3576 spa_config_path = spa_config_path_env; 3577 3578 while ((c = getopt(argc, argv, 3579 "bcdhilmMI:suCDRSAFLXx:evp:t:U:P")) != -1) { 3580 switch (c) { 3581 case 'b': 3582 case 'c': 3583 case 'd': 3584 case 'h': 3585 case 'i': 3586 case 'l': 3587 case 'm': 3588 case 's': 3589 case 'u': 3590 case 'C': 3591 case 'D': 3592 case 'M': 3593 case 'R': 3594 case 'S': 3595 dump_opt[c]++; 3596 dump_all = 0; 3597 break; 3598 case 'A': 3599 case 'F': 3600 case 'L': 3601 case 'X': 3602 case 'e': 3603 case 'P': 3604 dump_opt[c]++; 3605 break; 3606 case 'I': 3607 max_inflight = strtoull(optarg, NULL, 0); 3608 if (max_inflight == 0) { 3609 (void) fprintf(stderr, "maximum number " 3610 "of inflight I/Os must be greater " 3611 "than 0\n"); 3612 usage(); 3613 } 3614 break; 3615 case 'p': 3616 if (searchdirs == NULL) { 3617 searchdirs = umem_alloc(sizeof (char *), 3618 UMEM_NOFAIL); 3619 } else { 3620 char **tmp = umem_alloc((nsearch + 1) * 3621 sizeof (char *), UMEM_NOFAIL); 3622 bcopy(searchdirs, tmp, nsearch * 3623 sizeof (char *)); 3624 umem_free(searchdirs, 3625 nsearch * sizeof (char *)); 3626 searchdirs = tmp; 3627 } 3628 searchdirs[nsearch++] = optarg; 3629 break; 3630 case 't': 3631 max_txg = strtoull(optarg, NULL, 0); 3632 if (max_txg < TXG_INITIAL) { 3633 (void) fprintf(stderr, "incorrect txg " 3634 "specified: %s\n", optarg); 3635 usage(); 3636 } 3637 break; 3638 case 'U': 3639 spa_config_path = optarg; 3640 break; 3641 case 'v': 3642 verbose++; 3643 break; 3644 case 'x': 3645 vn_dumpdir = optarg; 3646 break; 3647 default: 3648 usage(); 3649 break; 3650 } 3651 } 3652 3653 if (!dump_opt['e'] && searchdirs != NULL) { 3654 (void) fprintf(stderr, "-p option requires use of -e\n"); 3655 usage(); 3656 } 3657 3658 /* 3659 * ZDB does not typically re-read blocks; therefore limit the ARC 3660 * to 256 MB, which can be used entirely for metadata. 3661 */ 3662 zfs_arc_max = zfs_arc_meta_limit = 256 * 1024 * 1024; 3663 3664 /* 3665 * "zdb -c" uses checksum-verifying scrub i/os which are async reads. 3666 * "zdb -b" uses traversal prefetch which uses async reads. 3667 * For good performance, let several of them be active at once. 3668 */ 3669 zfs_vdev_async_read_max_active = 10; 3670 3671 kernel_init(FREAD); 3672 g_zfs = libzfs_init(); 3673 if (g_zfs == NULL) 3674 fatal("Fail to initialize zfs"); 3675 3676 if (dump_all) 3677 verbose = MAX(verbose, 1); 3678 3679 for (c = 0; c < 256; c++) { 3680 if (dump_all && !strchr("elAFLRSXP", c)) 3681 dump_opt[c] = 1; 3682 if (dump_opt[c]) 3683 dump_opt[c] += verbose; 3684 } 3685 3686 aok = (dump_opt['A'] == 1) || (dump_opt['A'] > 2); 3687 zfs_recover = (dump_opt['A'] > 1); 3688 3689 argc -= optind; 3690 argv += optind; 3691 3692 if (argc < 2 && dump_opt['R']) 3693 usage(); 3694 if (argc < 1) { 3695 if (!dump_opt['e'] && dump_opt['C']) { 3696 dump_cachefile(spa_config_path); 3697 return (0); 3698 } 3699 usage(); 3700 } 3701 3702 if (dump_opt['l']) { 3703 dump_label(argv[0]); 3704 return (0); 3705 } 3706 3707 if (dump_opt['X'] || dump_opt['F']) 3708 rewind = ZPOOL_DO_REWIND | 3709 (dump_opt['X'] ? ZPOOL_EXTREME_REWIND : 0); 3710 3711 if (nvlist_alloc(&policy, NV_UNIQUE_NAME_TYPE, 0) != 0 || 3712 nvlist_add_uint64(policy, ZPOOL_REWIND_REQUEST_TXG, max_txg) != 0 || 3713 nvlist_add_uint32(policy, ZPOOL_REWIND_REQUEST, rewind) != 0) 3714 fatal("internal error: %s", strerror(ENOMEM)); 3715 3716 error = 0; 3717 target = argv[0]; 3718 3719 if (dump_opt['e']) { 3720 nvlist_t *cfg = NULL; 3721 char *name = find_zpool(&target, &cfg, nsearch, searchdirs); 3722 3723 error = ENOENT; 3724 if (name) { 3725 if (dump_opt['C'] > 1) { 3726 (void) printf("\nConfiguration for import:\n"); 3727 dump_nvlist(cfg, 8); 3728 } 3729 if (nvlist_add_nvlist(cfg, 3730 ZPOOL_REWIND_POLICY, policy) != 0) { 3731 fatal("can't open '%s': %s", 3732 target, strerror(ENOMEM)); 3733 } 3734 if ((error = spa_import(name, cfg, NULL, 3735 ZFS_IMPORT_MISSING_LOG)) != 0) { 3736 error = spa_import(name, cfg, NULL, 3737 ZFS_IMPORT_VERBATIM); 3738 } 3739 } 3740 } 3741 3742 if (strpbrk(target, "/@") != NULL) { 3743 size_t targetlen; 3744 3745 target_is_spa = B_FALSE; 3746 /* 3747 * Remove any trailing slash. Later code would get confused 3748 * by it, but we want to allow it so that "pool/" can 3749 * indicate that we want to dump the topmost filesystem, 3750 * rather than the whole pool. 3751 */ 3752 targetlen = strlen(target); 3753 if (targetlen != 0 && target[targetlen - 1] == '/') 3754 target[targetlen - 1] = '\0'; 3755 } 3756 3757 if (error == 0) { 3758 if (target_is_spa || dump_opt['R']) { 3759 error = spa_open_rewind(target, &spa, FTAG, policy, 3760 NULL); 3761 if (error) { 3762 /* 3763 * If we're missing the log device then 3764 * try opening the pool after clearing the 3765 * log state. 3766 */ 3767 mutex_enter(&spa_namespace_lock); 3768 if ((spa = spa_lookup(target)) != NULL && 3769 spa->spa_log_state == SPA_LOG_MISSING) { 3770 spa->spa_log_state = SPA_LOG_CLEAR; 3771 error = 0; 3772 } 3773 mutex_exit(&spa_namespace_lock); 3774 3775 if (!error) { 3776 error = spa_open_rewind(target, &spa, 3777 FTAG, policy, NULL); 3778 } 3779 } 3780 } else { 3781 error = dmu_objset_own(target, DMU_OST_ANY, 3782 B_TRUE, FTAG, &os); 3783 } 3784 } 3785 nvlist_free(policy); 3786 3787 if (error) 3788 fatal("can't open '%s': %s", target, strerror(error)); 3789 3790 argv++; 3791 argc--; 3792 if (!dump_opt['R']) { 3793 if (argc > 0) { 3794 zopt_objects = argc; 3795 zopt_object = calloc(zopt_objects, sizeof (uint64_t)); 3796 for (i = 0; i < zopt_objects; i++) { 3797 errno = 0; 3798 zopt_object[i] = strtoull(argv[i], NULL, 0); 3799 if (zopt_object[i] == 0 && errno != 0) 3800 fatal("bad number %s: %s", 3801 argv[i], strerror(errno)); 3802 } 3803 } 3804 if (os != NULL) { 3805 dump_dir(os); 3806 } else if (zopt_objects > 0 && !dump_opt['m']) { 3807 dump_dir(spa->spa_meta_objset); 3808 } else { 3809 dump_zpool(spa); 3810 } 3811 } else { 3812 flagbits['b'] = ZDB_FLAG_PRINT_BLKPTR; 3813 flagbits['c'] = ZDB_FLAG_CHECKSUM; 3814 flagbits['d'] = ZDB_FLAG_DECOMPRESS; 3815 flagbits['e'] = ZDB_FLAG_BSWAP; 3816 flagbits['g'] = ZDB_FLAG_GBH; 3817 flagbits['i'] = ZDB_FLAG_INDIRECT; 3818 flagbits['p'] = ZDB_FLAG_PHYS; 3819 flagbits['r'] = ZDB_FLAG_RAW; 3820 3821 for (i = 0; i < argc; i++) 3822 zdb_read_block(argv[i], spa); 3823 } 3824 3825 (os != NULL) ? dmu_objset_disown(os, FTAG) : spa_close(spa, FTAG); 3826 3827 fuid_table_destroy(); 3828 sa_loaded = B_FALSE; 3829 3830 libzfs_fini(g_zfs); 3831 kernel_fini(); 3832 3833 return (0); 3834} 3835