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