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