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