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