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