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