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 * Copyright 2006 Sun Microsystems, Inc. All rights reserved. 23 * Use is subject to license terms. 24 */ 25 26 #pragma ident "%Z%%M% %I% %E% SMI" 27 28 #include <stdio.h> 29 #include <stdlib.h> 30 #include <sys/zfs_context.h> 31 #include <sys/spa.h> 32 #include <sys/spa_impl.h> 33 #include <sys/dmu.h> 34 #include <sys/zap.h> 35 #include <sys/fs/zfs.h> 36 #include <sys/zfs_znode.h> 37 #include <sys/vdev.h> 38 #include <sys/vdev_impl.h> 39 #include <sys/metaslab_impl.h> 40 #include <sys/dmu_objset.h> 41 #include <sys/dsl_dir.h> 42 #include <sys/dsl_dataset.h> 43 #include <sys/dsl_pool.h> 44 #include <sys/dbuf.h> 45 #include <sys/zil.h> 46 #include <sys/zil_impl.h> 47 #include <sys/stat.h> 48 #include <sys/resource.h> 49 #include <sys/dmu_traverse.h> 50 #include <sys/zio_checksum.h> 51 #include <sys/zio_compress.h> 52 53 const char cmdname[] = "zdb"; 54 uint8_t dump_opt[256]; 55 56 typedef void object_viewer_t(objset_t *, uint64_t, void *data, size_t size); 57 58 extern void dump_intent_log(zilog_t *); 59 uint64_t *zopt_object = NULL; 60 int zopt_objects = 0; 61 int zdb_advance = ADVANCE_PRE; 62 zbookmark_t zdb_noread = { 0, 0, ZB_NO_LEVEL, 0 }; 63 64 /* 65 * These libumem hooks provide a reasonable set of defaults for the allocator's 66 * debugging facilities. 67 */ 68 const char * 69 _umem_debug_init() 70 { 71 return ("default,verbose"); /* $UMEM_DEBUG setting */ 72 } 73 74 const char * 75 _umem_logging_init(void) 76 { 77 return ("fail,contents"); /* $UMEM_LOGGING setting */ 78 } 79 80 static void 81 usage(void) 82 { 83 (void) fprintf(stderr, 84 "Usage: %s [-udibcsvLU] [-O order] [-B os:obj:level:blkid] " 85 "dataset [object...]\n" 86 " %s -C [pool]\n" 87 " %s -l dev\n", 88 cmdname, cmdname, cmdname); 89 90 (void) fprintf(stderr, " -u uberblock\n"); 91 (void) fprintf(stderr, " -d datasets\n"); 92 (void) fprintf(stderr, " -C cached pool configuration\n"); 93 (void) fprintf(stderr, " -i intent logs\n"); 94 (void) fprintf(stderr, " -b block statistics\n"); 95 (void) fprintf(stderr, " -c checksum all data blocks\n"); 96 (void) fprintf(stderr, " -s report stats on zdb's I/O\n"); 97 (void) fprintf(stderr, " -v verbose (applies to all others)\n"); 98 (void) fprintf(stderr, " -l dump label contents\n"); 99 (void) fprintf(stderr, " -L live pool (allows some errors)\n"); 100 (void) fprintf(stderr, " -O [!]<pre|post|prune|data|holes> " 101 "visitation order\n"); 102 (void) fprintf(stderr, " -U use zpool.cache in /tmp\n"); 103 (void) fprintf(stderr, " -B objset:object:level:blkid -- " 104 "simulate bad block\n"); 105 (void) fprintf(stderr, "Specify an option more than once (e.g. -bb) " 106 "to make only that option verbose\n"); 107 (void) fprintf(stderr, "Default is to dump everything non-verbosely\n"); 108 exit(1); 109 } 110 111 static void 112 fatal(const char *fmt, ...) 113 { 114 va_list ap; 115 116 va_start(ap, fmt); 117 (void) fprintf(stderr, "%s: ", cmdname); 118 (void) vfprintf(stderr, fmt, ap); 119 va_end(ap); 120 (void) fprintf(stderr, "\n"); 121 122 exit(1); 123 } 124 125 static void 126 dump_nvlist(nvlist_t *list, int indent) 127 { 128 nvpair_t *elem = NULL; 129 130 while ((elem = nvlist_next_nvpair(list, elem)) != NULL) { 131 switch (nvpair_type(elem)) { 132 case DATA_TYPE_STRING: 133 { 134 char *value; 135 136 VERIFY(nvpair_value_string(elem, &value) == 0); 137 (void) printf("%*s%s='%s'\n", indent, "", 138 nvpair_name(elem), value); 139 } 140 break; 141 142 case DATA_TYPE_UINT64: 143 { 144 uint64_t value; 145 146 VERIFY(nvpair_value_uint64(elem, &value) == 0); 147 (void) printf("%*s%s=%llu\n", indent, "", 148 nvpair_name(elem), (u_longlong_t)value); 149 } 150 break; 151 152 case DATA_TYPE_NVLIST: 153 { 154 nvlist_t *value; 155 156 VERIFY(nvpair_value_nvlist(elem, &value) == 0); 157 (void) printf("%*s%s\n", indent, "", 158 nvpair_name(elem)); 159 dump_nvlist(value, indent + 4); 160 } 161 break; 162 163 case DATA_TYPE_NVLIST_ARRAY: 164 { 165 nvlist_t **value; 166 uint_t c, count; 167 168 VERIFY(nvpair_value_nvlist_array(elem, &value, 169 &count) == 0); 170 171 for (c = 0; c < count; c++) { 172 (void) printf("%*s%s[%u]\n", indent, "", 173 nvpair_name(elem), c); 174 dump_nvlist(value[c], indent + 8); 175 } 176 } 177 break; 178 179 default: 180 181 (void) printf("bad config type %d for %s\n", 182 nvpair_type(elem), nvpair_name(elem)); 183 } 184 } 185 } 186 187 /* ARGSUSED */ 188 static void 189 dump_packed_nvlist(objset_t *os, uint64_t object, void *data, size_t size) 190 { 191 nvlist_t *nv; 192 size_t nvsize = *(uint64_t *)data; 193 char *packed = umem_alloc(nvsize, UMEM_NOFAIL); 194 195 VERIFY(0 == dmu_read(os, object, 0, nvsize, packed)); 196 197 VERIFY(nvlist_unpack(packed, nvsize, &nv, 0) == 0); 198 199 umem_free(packed, nvsize); 200 201 dump_nvlist(nv, 8); 202 203 nvlist_free(nv); 204 } 205 206 const char dump_zap_stars[] = "****************************************"; 207 const int dump_zap_width = sizeof (dump_zap_stars) - 1; 208 209 static void 210 dump_zap_histogram(uint64_t histo[ZAP_HISTOGRAM_SIZE]) 211 { 212 int i; 213 int minidx = ZAP_HISTOGRAM_SIZE - 1; 214 int maxidx = 0; 215 uint64_t max = 0; 216 217 for (i = 0; i < ZAP_HISTOGRAM_SIZE; i++) { 218 if (histo[i] > max) 219 max = histo[i]; 220 if (histo[i] > 0 && i > maxidx) 221 maxidx = i; 222 if (histo[i] > 0 && i < minidx) 223 minidx = i; 224 } 225 226 if (max < dump_zap_width) 227 max = dump_zap_width; 228 229 for (i = minidx; i <= maxidx; i++) 230 (void) printf("\t\t\t%u: %6llu %s\n", i, (u_longlong_t)histo[i], 231 &dump_zap_stars[(max - histo[i]) * dump_zap_width / max]); 232 } 233 234 static void 235 dump_zap_stats(objset_t *os, uint64_t object) 236 { 237 int error; 238 zap_stats_t zs; 239 240 error = zap_get_stats(os, object, &zs); 241 if (error) 242 return; 243 244 if (zs.zs_ptrtbl_len == 0) { 245 ASSERT(zs.zs_num_blocks == 1); 246 (void) printf("\tmicrozap: %llu bytes, %llu entries\n", 247 (u_longlong_t)zs.zs_blocksize, 248 (u_longlong_t)zs.zs_num_entries); 249 return; 250 } 251 252 (void) printf("\tFat ZAP stats:\n"); 253 (void) printf("\t\tPointer table: %llu elements\n", 254 (u_longlong_t)zs.zs_ptrtbl_len); 255 (void) printf("\t\tZAP entries: %llu\n", 256 (u_longlong_t)zs.zs_num_entries); 257 (void) printf("\t\tLeaf blocks: %llu\n", 258 (u_longlong_t)zs.zs_num_leafs); 259 (void) printf("\t\tTotal blocks: %llu\n", 260 (u_longlong_t)zs.zs_num_blocks); 261 262 (void) printf("\t\tLeafs with 2^n pointers:\n"); 263 dump_zap_histogram(zs.zs_leafs_with_2n_pointers); 264 265 (void) printf("\t\tBlocks with n*5 entries:\n"); 266 dump_zap_histogram(zs.zs_blocks_with_n5_entries); 267 268 (void) printf("\t\tBlocks n/10 full:\n"); 269 dump_zap_histogram(zs.zs_blocks_n_tenths_full); 270 271 (void) printf("\t\tEntries with n chunks:\n"); 272 dump_zap_histogram(zs.zs_entries_using_n_chunks); 273 274 (void) printf("\t\tBuckets with n entries:\n"); 275 dump_zap_histogram(zs.zs_buckets_with_n_entries); 276 } 277 278 /*ARGSUSED*/ 279 static void 280 dump_none(objset_t *os, uint64_t object, void *data, size_t size) 281 { 282 } 283 284 /*ARGSUSED*/ 285 void 286 dump_uint8(objset_t *os, uint64_t object, void *data, size_t size) 287 { 288 } 289 290 /*ARGSUSED*/ 291 static void 292 dump_uint64(objset_t *os, uint64_t object, void *data, size_t size) 293 { 294 } 295 296 /*ARGSUSED*/ 297 static void 298 dump_zap(objset_t *os, uint64_t object, void *data, size_t size) 299 { 300 zap_cursor_t zc; 301 zap_attribute_t attr; 302 void *prop; 303 int i; 304 305 dump_zap_stats(os, object); 306 (void) printf("\n"); 307 308 for (zap_cursor_init(&zc, os, object); 309 zap_cursor_retrieve(&zc, &attr) == 0; 310 zap_cursor_advance(&zc)) { 311 (void) printf("\t\t%s = ", attr.za_name); 312 if (attr.za_num_integers == 0) { 313 (void) printf("\n"); 314 continue; 315 } 316 prop = umem_zalloc(attr.za_num_integers * 317 attr.za_integer_length, UMEM_NOFAIL); 318 (void) zap_lookup(os, object, attr.za_name, 319 attr.za_integer_length, attr.za_num_integers, prop); 320 if (attr.za_integer_length == 1) { 321 (void) printf("%s", (char *)prop); 322 } else { 323 for (i = 0; i < attr.za_num_integers; i++) { 324 switch (attr.za_integer_length) { 325 case 2: 326 (void) printf("%u ", 327 ((uint16_t *)prop)[i]); 328 break; 329 case 4: 330 (void) printf("%u ", 331 ((uint32_t *)prop)[i]); 332 break; 333 case 8: 334 (void) printf("%lld ", 335 (u_longlong_t)((int64_t *)prop)[i]); 336 break; 337 } 338 } 339 } 340 (void) printf("\n"); 341 umem_free(prop, attr.za_num_integers * attr.za_integer_length); 342 } 343 zap_cursor_fini(&zc); 344 } 345 346 static void 347 dump_spacemap(objset_t *os, space_map_obj_t *smo, space_map_t *sm) 348 { 349 uint64_t alloc, offset, entry; 350 int mapshift = sm->sm_shift; 351 uint64_t mapstart = sm->sm_start; 352 char *ddata[] = { "ALLOC", "FREE", "CONDENSE", "INVALID" }; 353 354 if (smo->smo_object == 0) 355 return; 356 357 /* 358 * Print out the freelist entries in both encoded and decoded form. 359 */ 360 alloc = 0; 361 for (offset = 0; offset < smo->smo_objsize; offset += sizeof (entry)) { 362 VERIFY(0 == dmu_read(os, smo->smo_object, offset, 363 sizeof (entry), &entry)); 364 if (SM_DEBUG_DECODE(entry)) { 365 (void) printf("\t\t[%4llu] %s: txg %llu, pass %llu\n", 366 (u_longlong_t)(offset / sizeof (entry)), 367 ddata[SM_DEBUG_ACTION_DECODE(entry)], 368 SM_DEBUG_TXG_DECODE(entry), 369 SM_DEBUG_SYNCPASS_DECODE(entry)); 370 } else { 371 (void) printf("\t\t[%4llu] %c range:" 372 " %08llx-%08llx size: %06llx\n", 373 (u_longlong_t)(offset / sizeof (entry)), 374 SM_TYPE_DECODE(entry) == SM_ALLOC ? 'A' : 'F', 375 (SM_OFFSET_DECODE(entry) << mapshift) + mapstart, 376 (SM_OFFSET_DECODE(entry) << mapshift) + mapstart + 377 (SM_RUN_DECODE(entry) << mapshift), 378 (SM_RUN_DECODE(entry) << mapshift)); 379 if (SM_TYPE_DECODE(entry) == SM_ALLOC) 380 alloc += SM_RUN_DECODE(entry) << mapshift; 381 else 382 alloc -= SM_RUN_DECODE(entry) << mapshift; 383 } 384 } 385 if (alloc != smo->smo_alloc) { 386 (void) printf("space_map_object alloc (%llu) INCONSISTENT " 387 "with space map summary (%llu)\n", 388 (u_longlong_t)smo->smo_alloc, (u_longlong_t)alloc); 389 } 390 } 391 392 static void 393 dump_metaslab(metaslab_t *msp) 394 { 395 char freebuf[5]; 396 space_map_obj_t *smo = msp->ms_smo; 397 vdev_t *vd = msp->ms_group->mg_vd; 398 spa_t *spa = vd->vdev_spa; 399 400 nicenum(msp->ms_map.sm_size - smo->smo_alloc, freebuf); 401 402 if (dump_opt['d'] <= 5) { 403 (void) printf("\t%10llx %10llu %5s\n", 404 (u_longlong_t)msp->ms_map.sm_start, 405 (u_longlong_t)smo->smo_object, 406 freebuf); 407 return; 408 } 409 410 (void) printf( 411 "\tvdev %llu offset %08llx spacemap %4llu free %5s\n", 412 (u_longlong_t)vd->vdev_id, (u_longlong_t)msp->ms_map.sm_start, 413 (u_longlong_t)smo->smo_object, freebuf); 414 415 ASSERT(msp->ms_map.sm_size == (1ULL << vd->vdev_ms_shift)); 416 417 dump_spacemap(spa->spa_meta_objset, smo, &msp->ms_map); 418 } 419 420 static void 421 dump_metaslabs(spa_t *spa) 422 { 423 vdev_t *rvd = spa->spa_root_vdev; 424 vdev_t *vd; 425 int c, m; 426 427 (void) printf("\nMetaslabs:\n"); 428 429 for (c = 0; c < rvd->vdev_children; c++) { 430 vd = rvd->vdev_child[c]; 431 432 spa_config_enter(spa, RW_READER, FTAG); 433 (void) printf("\n vdev %llu = %s\n\n", 434 (u_longlong_t)vd->vdev_id, vdev_description(vd)); 435 spa_config_exit(spa, FTAG); 436 437 if (dump_opt['d'] <= 5) { 438 (void) printf("\t%10s %10s %5s\n", 439 "offset", "spacemap", "free"); 440 (void) printf("\t%10s %10s %5s\n", 441 "------", "--------", "----"); 442 } 443 for (m = 0; m < vd->vdev_ms_count; m++) 444 dump_metaslab(vd->vdev_ms[m]); 445 (void) printf("\n"); 446 } 447 } 448 449 static void 450 dump_dtl(vdev_t *vd, int indent) 451 { 452 avl_tree_t *t = &vd->vdev_dtl_map.sm_root; 453 spa_t *spa = vd->vdev_spa; 454 space_seg_t *ss; 455 vdev_t *pvd; 456 int c; 457 458 if (indent == 0) 459 (void) printf("\nDirty time logs:\n\n"); 460 461 spa_config_enter(spa, RW_READER, FTAG); 462 (void) printf("\t%*s%s\n", indent, "", vdev_description(vd)); 463 spa_config_exit(spa, FTAG); 464 465 for (ss = avl_first(t); ss; ss = AVL_NEXT(t, ss)) { 466 /* 467 * Everything in this DTL must appear in all parent DTL unions. 468 */ 469 for (pvd = vd; pvd; pvd = pvd->vdev_parent) 470 ASSERT(vdev_dtl_contains(&pvd->vdev_dtl_map, 471 ss->ss_start, ss->ss_end - ss->ss_start)); 472 (void) printf("\t%*soutage [%llu,%llu] length %llu\n", 473 indent, "", 474 (u_longlong_t)ss->ss_start, 475 (u_longlong_t)ss->ss_end - 1, 476 (u_longlong_t)ss->ss_end - ss->ss_start); 477 } 478 479 (void) printf("\n"); 480 481 if (dump_opt['d'] > 5 && vd->vdev_children == 0) { 482 dump_spacemap(vd->vdev_spa->spa_meta_objset, &vd->vdev_dtl, 483 &vd->vdev_dtl_map); 484 (void) printf("\n"); 485 } 486 487 for (c = 0; c < vd->vdev_children; c++) 488 dump_dtl(vd->vdev_child[c], indent + 4); 489 } 490 491 /*ARGSUSED*/ 492 static void 493 dump_dnode(objset_t *os, uint64_t object, void *data, size_t size) 494 { 495 } 496 497 static uint64_t 498 blkid2offset(dnode_phys_t *dnp, int level, uint64_t blkid) 499 { 500 if (level < 0) 501 return (blkid); 502 503 return ((blkid << (level * (dnp->dn_indblkshift - SPA_BLKPTRSHIFT))) * 504 dnp->dn_datablkszsec << SPA_MINBLOCKSHIFT); 505 } 506 507 /* ARGSUSED */ 508 static int 509 zdb_indirect_cb(traverse_blk_cache_t *bc, spa_t *spa, void *a) 510 { 511 zbookmark_t *zb = &bc->bc_bookmark; 512 blkptr_t *bp = &bc->bc_blkptr; 513 dva_t *dva = &bp->blk_dva[0]; 514 void *data = bc->bc_data; 515 dnode_phys_t *dnp = bc->bc_dnode; 516 char buffer[300]; 517 int l; 518 519 if (bc->bc_errno) { 520 (void) sprintf(buffer, 521 "Error %d reading <%llu, %llu, %lld, %llu>: ", 522 bc->bc_errno, 523 (u_longlong_t)zb->zb_objset, 524 (u_longlong_t)zb->zb_object, 525 (u_longlong_t)zb->zb_level, 526 (u_longlong_t)zb->zb_blkid); 527 goto out; 528 } 529 530 if (zb->zb_level == -1) { 531 ASSERT3U(BP_GET_TYPE(bp), ==, DMU_OT_OBJSET); 532 ASSERT3U(BP_GET_LEVEL(bp), ==, 0); 533 } else { 534 ASSERT3U(BP_GET_TYPE(bp), ==, dnp->dn_type); 535 ASSERT3U(BP_GET_LEVEL(bp), ==, zb->zb_level); 536 } 537 538 if (zb->zb_level > 0) { 539 uint64_t fill = 0; 540 blkptr_t *bpx, *bpend; 541 542 for (bpx = data, bpend = bpx + BP_GET_LSIZE(bp) / sizeof (*bpx); 543 bpx < bpend; bpx++) { 544 if (bpx->blk_birth != 0) { 545 fill += bpx->blk_fill; 546 } else { 547 ASSERT(bpx->blk_fill == 0); 548 } 549 } 550 ASSERT3U(fill, ==, bp->blk_fill); 551 } 552 553 if (zb->zb_level == 0 && dnp->dn_type == DMU_OT_DNODE) { 554 uint64_t fill = 0; 555 dnode_phys_t *dnx, *dnend; 556 557 for (dnx = data, dnend = dnx + (BP_GET_LSIZE(bp)>>DNODE_SHIFT); 558 dnx < dnend; dnx++) { 559 if (dnx->dn_type != DMU_OT_NONE) 560 fill++; 561 } 562 ASSERT3U(fill, ==, bp->blk_fill); 563 } 564 565 (void) sprintf(buffer, "%16llx ", 566 (u_longlong_t)blkid2offset(dnp, zb->zb_level, zb->zb_blkid)); 567 568 ASSERT(zb->zb_level >= 0); 569 570 for (l = dnp->dn_nlevels - 1; l >= -1; l--) { 571 if (l == zb->zb_level) { 572 (void) sprintf(buffer + strlen(buffer), "L%llx", 573 (u_longlong_t)zb->zb_level); 574 } else { 575 (void) sprintf(buffer + strlen(buffer), " "); 576 } 577 } 578 579 out: 580 if (bp->blk_birth == 0) { 581 (void) sprintf(buffer + strlen(buffer), "<hole>"); 582 (void) printf("%s\n", buffer); 583 } else { 584 // XXBP - Need to print number of active BPs here 585 (void) sprintf(buffer + strlen(buffer), 586 "vdev=%llu off=%llx %llxL/%llxP/%llxA F=%llu B=%llu", 587 (u_longlong_t)DVA_GET_VDEV(dva), 588 (u_longlong_t)DVA_GET_OFFSET(dva), 589 (u_longlong_t)BP_GET_LSIZE(bp), 590 (u_longlong_t)BP_GET_PSIZE(bp), 591 (u_longlong_t)DVA_GET_ASIZE(dva), 592 (u_longlong_t)bp->blk_fill, 593 (u_longlong_t)bp->blk_birth); 594 595 (void) printf("%s\n", buffer); 596 } 597 598 return (bc->bc_errno ? ERESTART : 0); 599 } 600 601 /*ARGSUSED*/ 602 static void 603 dump_indirect(objset_t *os, uint64_t object, void *data, size_t size) 604 { 605 traverse_handle_t *th; 606 uint64_t objset = dmu_objset_id(os); 607 int advance = zdb_advance; 608 609 (void) printf("Indirect blocks:\n"); 610 611 if (object == 0) 612 advance |= ADVANCE_DATA; 613 614 th = traverse_init(dmu_objset_spa(os), zdb_indirect_cb, NULL, advance, 615 ZIO_FLAG_CANFAIL); 616 th->th_noread = zdb_noread; 617 618 traverse_add_dnode(th, 0, -1ULL, objset, object); 619 620 while (traverse_more(th) == EAGAIN) 621 continue; 622 623 (void) printf("\n"); 624 625 traverse_fini(th); 626 } 627 628 /*ARGSUSED*/ 629 static void 630 dump_dsl_dir(objset_t *os, uint64_t object, void *data, size_t size) 631 { 632 dsl_dir_phys_t *dd = data; 633 time_t crtime; 634 char used[6], compressed[6], uncompressed[6], quota[6], resv[6]; 635 636 if (dd == NULL) 637 return; 638 639 ASSERT(size == sizeof (*dd)); 640 641 crtime = dd->dd_creation_time; 642 nicenum(dd->dd_used_bytes, used); 643 nicenum(dd->dd_compressed_bytes, compressed); 644 nicenum(dd->dd_uncompressed_bytes, uncompressed); 645 nicenum(dd->dd_quota, quota); 646 nicenum(dd->dd_reserved, resv); 647 648 (void) printf("\t\tcreation_time = %s", ctime(&crtime)); 649 (void) printf("\t\thead_dataset_obj = %llu\n", 650 (u_longlong_t)dd->dd_head_dataset_obj); 651 (void) printf("\t\tparent_dir_obj = %llu\n", 652 (u_longlong_t)dd->dd_parent_obj); 653 (void) printf("\t\tclone_parent_obj = %llu\n", 654 (u_longlong_t)dd->dd_clone_parent_obj); 655 (void) printf("\t\tchild_dir_zapobj = %llu\n", 656 (u_longlong_t)dd->dd_child_dir_zapobj); 657 (void) printf("\t\tused_bytes = %s\n", used); 658 (void) printf("\t\tcompressed_bytes = %s\n", compressed); 659 (void) printf("\t\tuncompressed_bytes = %s\n", uncompressed); 660 (void) printf("\t\tquota = %s\n", quota); 661 (void) printf("\t\treserved = %s\n", resv); 662 (void) printf("\t\tprops_zapobj = %llu\n", 663 (u_longlong_t)dd->dd_props_zapobj); 664 } 665 666 /*ARGSUSED*/ 667 static void 668 dump_dsl_dataset(objset_t *os, uint64_t object, void *data, size_t size) 669 { 670 dsl_dataset_phys_t *ds = data; 671 time_t crtime; 672 char used[6], compressed[6], uncompressed[6], unique[6]; 673 char blkbuf[BP_SPRINTF_LEN]; 674 675 if (ds == NULL) 676 return; 677 678 ASSERT(size == sizeof (*ds)); 679 crtime = ds->ds_creation_time; 680 nicenum(ds->ds_used_bytes, used); 681 nicenum(ds->ds_compressed_bytes, compressed); 682 nicenum(ds->ds_uncompressed_bytes, uncompressed); 683 nicenum(ds->ds_unique_bytes, unique); 684 sprintf_blkptr(blkbuf, BP_SPRINTF_LEN, &ds->ds_bp); 685 686 (void) printf("\t\tdataset_obj = %llu\n", 687 (u_longlong_t)ds->ds_dir_obj); 688 (void) printf("\t\tprev_snap_obj = %llu\n", 689 (u_longlong_t)ds->ds_prev_snap_obj); 690 (void) printf("\t\tprev_snap_txg = %llu\n", 691 (u_longlong_t)ds->ds_prev_snap_txg); 692 (void) printf("\t\tnext_snap_obj = %llu\n", 693 (u_longlong_t)ds->ds_next_snap_obj); 694 (void) printf("\t\tsnapnames_zapobj = %llu\n", 695 (u_longlong_t)ds->ds_snapnames_zapobj); 696 (void) printf("\t\tnum_children = %llu\n", 697 (u_longlong_t)ds->ds_num_children); 698 (void) printf("\t\tcreation_time = %s", ctime(&crtime)); 699 (void) printf("\t\tcreation_txg = %llu\n", 700 (u_longlong_t)ds->ds_creation_txg); 701 (void) printf("\t\tdeadlist_obj = %llu\n", 702 (u_longlong_t)ds->ds_deadlist_obj); 703 (void) printf("\t\tused_bytes = %s\n", used); 704 (void) printf("\t\tcompressed_bytes = %s\n", compressed); 705 (void) printf("\t\tuncompressed_bytes = %s\n", uncompressed); 706 (void) printf("\t\tunique = %s\n", unique); 707 (void) printf("\t\tfsid_guid = %llu\n", 708 (u_longlong_t)ds->ds_fsid_guid); 709 (void) printf("\t\tguid = %llu\n", 710 (u_longlong_t)ds->ds_guid); 711 (void) printf("\t\trestoring = %llu\n", 712 (u_longlong_t)ds->ds_restoring); 713 (void) printf("\t\tbp = %s\n", blkbuf); 714 } 715 716 static void 717 dump_bplist(objset_t *mos, uint64_t object, char *name) 718 { 719 bplist_t bpl = { 0 }; 720 blkptr_t blk, *bp = &blk; 721 uint64_t itor = 0; 722 char numbuf[6]; 723 724 if (dump_opt['d'] < 3) 725 return; 726 727 VERIFY(0 == bplist_open(&bpl, mos, object)); 728 if (bplist_empty(&bpl)) { 729 bplist_close(&bpl); 730 return; 731 } 732 733 nicenum(bpl.bpl_phys->bpl_bytes, numbuf); 734 735 (void) printf("\n %s: %llu entries, %s\n", 736 name, (u_longlong_t)bpl.bpl_phys->bpl_entries, numbuf); 737 738 if (dump_opt['d'] < 5) { 739 bplist_close(&bpl); 740 return; 741 } 742 743 (void) printf("\n"); 744 745 while (bplist_iterate(&bpl, &itor, bp) == 0) { 746 ASSERT(bp->blk_birth != 0); 747 // XXBP - Do we want to see all DVAs, or just one? 748 (void) printf("\tItem %3llu: vdev=%llu off=%llx " 749 "%llxL/%llxP/%llxA F=%llu B=%llu\n", 750 (u_longlong_t)itor - 1, 751 (u_longlong_t)DVA_GET_VDEV(&bp->blk_dva[0]), 752 (u_longlong_t)DVA_GET_OFFSET(&bp->blk_dva[0]), 753 (u_longlong_t)BP_GET_LSIZE(bp), 754 (u_longlong_t)BP_GET_PSIZE(bp), 755 (u_longlong_t)DVA_GET_ASIZE(&bp->blk_dva[0]), 756 (u_longlong_t)bp->blk_fill, 757 (u_longlong_t)bp->blk_birth); 758 } 759 760 bplist_close(&bpl); 761 } 762 763 static char * 764 znode_path(objset_t *os, uint64_t object, char *pathbuf, size_t size) 765 { 766 dmu_buf_t *db; 767 dmu_object_info_t doi; 768 znode_phys_t *zp; 769 uint64_t parent = 0; 770 size_t complen; 771 char component[MAXNAMELEN + 1]; 772 char *path; 773 int error; 774 775 path = pathbuf + size; 776 *--path = '\0'; 777 778 for (;;) { 779 error = dmu_bonus_hold(os, object, FTAG, &db); 780 if (error) 781 break; 782 783 dmu_object_info_from_db(db, &doi); 784 zp = db->db_data; 785 parent = zp->zp_parent; 786 dmu_buf_rele(db, FTAG); 787 788 if (doi.doi_bonus_type != DMU_OT_ZNODE) 789 break; 790 791 if (parent == object) { 792 if (path[0] != '/') 793 *--path = '/'; 794 return (path); 795 } 796 797 if (zap_value_search(os, parent, object, component) != 0) 798 break; 799 800 complen = strlen(component); 801 path -= complen; 802 bcopy(component, path, complen); 803 *--path = '/'; 804 805 object = parent; 806 } 807 808 (void) sprintf(component, "???<object#%llu>", (u_longlong_t)object); 809 810 complen = strlen(component); 811 path -= complen; 812 bcopy(component, path, complen); 813 814 return (path); 815 } 816 817 /*ARGSUSED*/ 818 static void 819 dump_znode(objset_t *os, uint64_t object, void *data, size_t size) 820 { 821 znode_phys_t *zp = data; 822 time_t z_crtime, z_atime, z_mtime, z_ctime; 823 char path[MAXPATHLEN * 2]; /* allow for xattr and failure prefix */ 824 825 ASSERT(size >= sizeof (znode_phys_t)); 826 827 if (dump_opt['d'] < 3) { 828 (void) printf("\t%s\n", 829 znode_path(os, object, path, sizeof (path))); 830 return; 831 } 832 833 z_crtime = (time_t)zp->zp_crtime[0]; 834 z_atime = (time_t)zp->zp_atime[0]; 835 z_mtime = (time_t)zp->zp_mtime[0]; 836 z_ctime = (time_t)zp->zp_ctime[0]; 837 838 (void) printf("\tpath %s\n", 839 znode_path(os, object, path, sizeof (path))); 840 (void) printf("\tatime %s", ctime(&z_atime)); 841 (void) printf("\tmtime %s", ctime(&z_mtime)); 842 (void) printf("\tctime %s", ctime(&z_ctime)); 843 (void) printf("\tcrtime %s", ctime(&z_crtime)); 844 (void) printf("\tgen %llu\n", (u_longlong_t)zp->zp_gen); 845 (void) printf("\tmode %llo\n", (u_longlong_t)zp->zp_mode); 846 (void) printf("\tsize %llu\n", (u_longlong_t)zp->zp_size); 847 (void) printf("\tparent %llu\n", (u_longlong_t)zp->zp_parent); 848 (void) printf("\tlinks %llu\n", (u_longlong_t)zp->zp_links); 849 (void) printf("\txattr %llu\n", (u_longlong_t)zp->zp_xattr); 850 (void) printf("\trdev 0x%016llx\n", (u_longlong_t)zp->zp_rdev); 851 } 852 853 /*ARGSUSED*/ 854 static void 855 dump_acl(objset_t *os, uint64_t object, void *data, size_t size) 856 { 857 } 858 859 /*ARGSUSED*/ 860 static void 861 dump_dmu_objset(objset_t *os, uint64_t object, void *data, size_t size) 862 { 863 } 864 865 static object_viewer_t *object_viewer[DMU_OT_NUMTYPES] = { 866 dump_none, /* unallocated */ 867 dump_zap, /* object directory */ 868 dump_uint64, /* object array */ 869 dump_none, /* packed nvlist */ 870 dump_packed_nvlist, /* packed nvlist size */ 871 dump_none, /* bplist */ 872 dump_none, /* bplist header */ 873 dump_none, /* SPA space map header */ 874 dump_none, /* SPA space map */ 875 dump_none, /* ZIL intent log */ 876 dump_dnode, /* DMU dnode */ 877 dump_dmu_objset, /* DMU objset */ 878 dump_dsl_dir, /* DSL directory */ 879 dump_zap, /* DSL directory child map */ 880 dump_zap, /* DSL dataset snap map */ 881 dump_zap, /* DSL props */ 882 dump_dsl_dataset, /* DSL dataset */ 883 dump_znode, /* ZFS znode */ 884 dump_acl, /* ZFS ACL */ 885 dump_uint8, /* ZFS plain file */ 886 dump_zap, /* ZFS directory */ 887 dump_zap, /* ZFS master node */ 888 dump_zap, /* ZFS delete queue */ 889 dump_uint8, /* zvol object */ 890 dump_zap, /* zvol prop */ 891 dump_uint8, /* other uint8[] */ 892 dump_uint64, /* other uint64[] */ 893 dump_zap, /* other ZAP */ 894 dump_zap, /* persistent error log */ 895 }; 896 897 static void 898 dump_object(objset_t *os, uint64_t object, int verbosity, int *print_header) 899 { 900 dmu_buf_t *db = NULL; 901 dmu_object_info_t doi; 902 dnode_t *dn; 903 void *bonus = NULL; 904 size_t bsize = 0; 905 char iblk[6], dblk[6], lsize[6], psize[6], bonus_size[6], segsize[6]; 906 char aux[50]; 907 int error; 908 909 if (*print_header) { 910 (void) printf("\n Object lvl iblk dblk lsize" 911 " psize type\n"); 912 *print_header = 0; 913 } 914 915 if (object == 0) { 916 dn = os->os->os_meta_dnode; 917 } else { 918 error = dmu_bonus_hold(os, object, FTAG, &db); 919 if (error) 920 fatal("dmu_bonus_hold(%llu) failed, errno %u", 921 object, error); 922 bonus = db->db_data; 923 bsize = db->db_size; 924 dn = ((dmu_buf_impl_t *)db)->db_dnode; 925 } 926 dmu_object_info_from_dnode(dn, &doi); 927 928 nicenum(doi.doi_metadata_block_size, iblk); 929 nicenum(doi.doi_data_block_size, dblk); 930 nicenum(doi.doi_data_block_size * (doi.doi_max_block_offset + 1), 931 lsize); 932 nicenum(doi.doi_physical_blks << 9, psize); 933 nicenum(doi.doi_bonus_size, bonus_size); 934 935 aux[0] = '\0'; 936 937 if (doi.doi_checksum != ZIO_CHECKSUM_INHERIT || verbosity >= 6) 938 (void) snprintf(aux + strlen(aux), sizeof (aux), " (K=%s)", 939 zio_checksum_table[doi.doi_checksum].ci_name); 940 941 if (doi.doi_compress != ZIO_COMPRESS_INHERIT || verbosity >= 6) 942 (void) snprintf(aux + strlen(aux), sizeof (aux), " (Z=%s)", 943 zio_compress_table[doi.doi_compress].ci_name); 944 945 (void) printf("%10lld %3u %5s %5s %5s %5s %s%s\n", 946 (u_longlong_t)object, doi.doi_indirection, iblk, dblk, lsize, 947 psize, dmu_ot[doi.doi_type].ot_name, aux); 948 949 if (doi.doi_bonus_type != DMU_OT_NONE && verbosity > 3) { 950 (void) printf("%10s %3s %5s %5s %5s %5s %s\n", 951 "", "", "", "", bonus_size, "bonus", 952 dmu_ot[doi.doi_bonus_type].ot_name); 953 } 954 955 if (verbosity >= 4) { 956 object_viewer[doi.doi_bonus_type](os, object, bonus, bsize); 957 object_viewer[doi.doi_type](os, object, NULL, 0); 958 *print_header = 1; 959 } 960 961 if (verbosity >= 5) 962 dump_indirect(os, object, NULL, 0); 963 964 if (verbosity >= 5) { 965 /* 966 * Report the list of segments that comprise the object. 967 */ 968 uint64_t start = 0; 969 uint64_t end; 970 uint64_t blkfill = 1; 971 int minlvl = 1; 972 973 if (dn->dn_type == DMU_OT_DNODE) { 974 minlvl = 0; 975 blkfill = DNODES_PER_BLOCK; 976 } 977 978 for (;;) { 979 error = dnode_next_offset(dn, B_FALSE, &start, minlvl, 980 blkfill); 981 if (error) 982 break; 983 end = start; 984 error = dnode_next_offset(dn, B_TRUE, &end, minlvl, 985 blkfill); 986 nicenum(end - start, segsize); 987 (void) printf("\t\tsegment [%016llx, %016llx)" 988 " size %5s\n", (u_longlong_t)start, 989 (u_longlong_t)end, segsize); 990 if (error) 991 break; 992 start = end; 993 } 994 } 995 996 if (db != NULL) 997 dmu_buf_rele(db, FTAG); 998 } 999 1000 static char *objset_types[DMU_OST_NUMTYPES] = { 1001 "NONE", "META", "ZPL", "ZVOL", "OTHER", "ANY" }; 1002 1003 /*ARGSUSED*/ 1004 static void 1005 dump_dir(objset_t *os) 1006 { 1007 dmu_objset_stats_t dds; 1008 uint64_t object, object_count; 1009 char numbuf[8]; 1010 char blkbuf[BP_SPRINTF_LEN]; 1011 char osname[MAXNAMELEN]; 1012 char *type = "UNKNOWN"; 1013 int verbosity = dump_opt['d']; 1014 int print_header = 1; 1015 int i, error; 1016 1017 dmu_objset_stats(os, &dds); 1018 1019 if (dds.dds_type < DMU_OST_NUMTYPES) 1020 type = objset_types[dds.dds_type]; 1021 1022 if (dds.dds_type == DMU_OST_META) { 1023 dds.dds_creation_txg = TXG_INITIAL; 1024 dds.dds_last_txg = os->os->os_rootbp.blk_birth; 1025 dds.dds_objects_used = os->os->os_rootbp.blk_fill; 1026 dds.dds_space_refd = 1027 os->os->os_spa->spa_dsl_pool->dp_mos_dir->dd_used_bytes; 1028 } 1029 1030 ASSERT3U(dds.dds_objects_used, ==, os->os->os_rootbp.blk_fill); 1031 1032 nicenum(dds.dds_space_refd, numbuf); 1033 1034 if (verbosity >= 4) { 1035 (void) strcpy(blkbuf, ", rootbp "); 1036 sprintf_blkptr(blkbuf + strlen(blkbuf), 1037 BP_SPRINTF_LEN - strlen(blkbuf), &os->os->os_rootbp); 1038 } else { 1039 blkbuf[0] = '\0'; 1040 } 1041 1042 dmu_objset_name(os, osname); 1043 1044 (void) printf("Dataset %s [%s], ID %llu, cr_txg %llu, last_txg %llu, " 1045 "%s, %llu objects%s\n", 1046 osname, type, (u_longlong_t)dmu_objset_id(os), 1047 (u_longlong_t)dds.dds_creation_txg, 1048 (u_longlong_t)dds.dds_last_txg, 1049 numbuf, 1050 (u_longlong_t)dds.dds_objects_used, 1051 blkbuf); 1052 1053 dump_intent_log(dmu_objset_zil(os)); 1054 1055 if (dmu_objset_ds(os) != NULL) 1056 dump_bplist(dmu_objset_pool(os)->dp_meta_objset, 1057 dmu_objset_ds(os)->ds_phys->ds_deadlist_obj, "Deadlist"); 1058 1059 if (verbosity < 2) 1060 return; 1061 1062 if (zopt_objects != 0) { 1063 for (i = 0; i < zopt_objects; i++) 1064 dump_object(os, zopt_object[i], verbosity, 1065 &print_header); 1066 (void) printf("\n"); 1067 return; 1068 } 1069 1070 dump_object(os, 0, verbosity, &print_header); 1071 object_count = 1; 1072 1073 object = 0; 1074 while ((error = dmu_object_next(os, &object, B_FALSE)) == 0) { 1075 dump_object(os, object, verbosity, &print_header); 1076 object_count++; 1077 } 1078 1079 ASSERT3U(object_count, ==, dds.dds_objects_used); 1080 1081 (void) printf("\n"); 1082 1083 if (error != ESRCH) 1084 fatal("dmu_object_next() = %d", error); 1085 } 1086 1087 static void 1088 dump_uberblock(uberblock_t *ub) 1089 { 1090 time_t timestamp = ub->ub_timestamp; 1091 1092 (void) printf("Uberblock\n\n"); 1093 (void) printf("\tmagic = %016llx\n", (u_longlong_t)ub->ub_magic); 1094 (void) printf("\tversion = %llu\n", (u_longlong_t)ub->ub_version); 1095 (void) printf("\ttxg = %llu\n", (u_longlong_t)ub->ub_txg); 1096 (void) printf("\tguid_sum = %llu\n", (u_longlong_t)ub->ub_guid_sum); 1097 (void) printf("\ttimestamp = %llu UTC = %s", 1098 (u_longlong_t)ub->ub_timestamp, asctime(localtime(×tamp))); 1099 if (dump_opt['u'] >= 3) { 1100 char blkbuf[BP_SPRINTF_LEN]; 1101 sprintf_blkptr(blkbuf, BP_SPRINTF_LEN, &ub->ub_rootbp); 1102 (void) printf("\trootbp = %s\n", blkbuf); 1103 } 1104 (void) printf("\n"); 1105 } 1106 1107 static void 1108 dump_config(const char *pool) 1109 { 1110 spa_t *spa = NULL; 1111 1112 mutex_enter(&spa_namespace_lock); 1113 while ((spa = spa_next(spa)) != NULL) { 1114 if (pool == NULL) 1115 (void) printf("%s\n", spa_name(spa)); 1116 if (pool == NULL || strcmp(pool, spa_name(spa)) == 0) 1117 dump_nvlist(spa->spa_config, 4); 1118 } 1119 mutex_exit(&spa_namespace_lock); 1120 } 1121 1122 static void 1123 dump_label(const char *dev) 1124 { 1125 int fd; 1126 vdev_label_t label; 1127 char *buf = label.vl_vdev_phys.vp_nvlist; 1128 size_t buflen = sizeof (label.vl_vdev_phys.vp_nvlist); 1129 struct stat64 statbuf; 1130 uint64_t psize; 1131 int l; 1132 1133 if ((fd = open64(dev, O_RDONLY)) < 0) { 1134 (void) printf("cannot open '%s': %s\n", dev, strerror(errno)); 1135 exit(1); 1136 } 1137 1138 if (fstat64(fd, &statbuf) != 0) { 1139 (void) printf("failed to stat '%s': %s\n", dev, 1140 strerror(errno)); 1141 exit(1); 1142 } 1143 1144 psize = statbuf.st_size; 1145 psize = P2ALIGN(psize, (uint64_t)sizeof (vdev_label_t)); 1146 1147 for (l = 0; l < VDEV_LABELS; l++) { 1148 1149 nvlist_t *config = NULL; 1150 1151 (void) printf("--------------------------------------------\n"); 1152 (void) printf("LABEL %d\n", l); 1153 (void) printf("--------------------------------------------\n"); 1154 1155 if (pread64(fd, &label, sizeof (label), 1156 vdev_label_offset(psize, l, 0)) != sizeof (label)) { 1157 (void) printf("failed to read label %d\n", l); 1158 continue; 1159 } 1160 1161 if (nvlist_unpack(buf, buflen, &config, 0) != 0) { 1162 (void) printf("failed to unpack label %d\n", l); 1163 continue; 1164 } 1165 dump_nvlist(config, 4); 1166 nvlist_free(config); 1167 } 1168 } 1169 1170 /*ARGSUSED*/ 1171 static void 1172 dump_one_dir(char *dsname, void *arg) 1173 { 1174 int error; 1175 objset_t *os; 1176 1177 error = dmu_objset_open(dsname, DMU_OST_ANY, 1178 DS_MODE_STANDARD | DS_MODE_READONLY, &os); 1179 if (error) { 1180 (void) printf("Could not open %s\n", dsname); 1181 return; 1182 } 1183 dump_dir(os); 1184 dmu_objset_close(os); 1185 } 1186 1187 static void 1188 zdb_space_map_load(spa_t *spa) 1189 { 1190 vdev_t *rvd = spa->spa_root_vdev; 1191 vdev_t *vd; 1192 int c, m, error; 1193 1194 for (c = 0; c < rvd->vdev_children; c++) { 1195 vd = rvd->vdev_child[c]; 1196 for (m = 0; m < vd->vdev_ms_count; m++) { 1197 metaslab_t *msp = vd->vdev_ms[m]; 1198 space_map_t *sm = &msp->ms_allocmap[0]; 1199 mutex_enter(&msp->ms_lock); 1200 error = space_map_load(sm, msp->ms_smo, SM_ALLOC, 1201 spa->spa_meta_objset, msp->ms_usable_end, 1202 sm->sm_size - msp->ms_usable_space); 1203 mutex_exit(&msp->ms_lock); 1204 if (error) 1205 fatal("%s bad space map #%d, error %d", 1206 spa->spa_name, c, error); 1207 } 1208 } 1209 } 1210 1211 static int 1212 zdb_space_map_claim(spa_t *spa, blkptr_t *bp, zbookmark_t *zb) 1213 { 1214 dva_t *dva = &bp->blk_dva[0]; 1215 uint64_t vdev = DVA_GET_VDEV(dva); 1216 uint64_t offset = DVA_GET_OFFSET(dva); 1217 uint64_t size = DVA_GET_ASIZE(dva); 1218 vdev_t *vd; 1219 metaslab_t *msp; 1220 space_map_t *allocmap, *freemap; 1221 int error; 1222 1223 if ((vd = vdev_lookup_top(spa, vdev)) == NULL) 1224 return (ENXIO); 1225 1226 if ((offset >> vd->vdev_ms_shift) >= vd->vdev_ms_count) 1227 return (ENXIO); 1228 1229 if (DVA_GET_GANG(dva)) { 1230 zio_gbh_phys_t gbh; 1231 blkptr_t blk = *bp; 1232 int g; 1233 1234 /* LINTED - compile time assert */ 1235 ASSERT(sizeof (zio_gbh_phys_t) == SPA_GANGBLOCKSIZE); 1236 size = vdev_psize_to_asize(vd, SPA_GANGBLOCKSIZE); 1237 DVA_SET_GANG(&blk.blk_dva[0], 0); 1238 DVA_SET_ASIZE(&blk.blk_dva[0], size); 1239 BP_SET_CHECKSUM(&blk, ZIO_CHECKSUM_GANG_HEADER); 1240 BP_SET_PSIZE(&blk, SPA_GANGBLOCKSIZE); 1241 BP_SET_LSIZE(&blk, SPA_GANGBLOCKSIZE); 1242 BP_SET_COMPRESS(&blk, ZIO_COMPRESS_OFF); 1243 error = zio_wait(zio_read(NULL, spa, &blk, 1244 &gbh, SPA_GANGBLOCKSIZE, NULL, NULL, 1245 ZIO_PRIORITY_SYNC_READ, 1246 ZIO_FLAG_CANFAIL | ZIO_FLAG_CONFIG_HELD, zb)); 1247 if (error) 1248 return (error); 1249 if (BP_SHOULD_BYTESWAP(&blk)) 1250 byteswap_uint64_array(&gbh, SPA_GANGBLOCKSIZE); 1251 for (g = 0; g < SPA_GBH_NBLKPTRS; g++) { 1252 if (gbh.zg_blkptr[g].blk_birth == 0) 1253 break; 1254 error = zdb_space_map_claim(spa, &gbh.zg_blkptr[g], zb); 1255 if (error) 1256 return (error); 1257 } 1258 } 1259 1260 msp = vd->vdev_ms[offset >> vd->vdev_ms_shift]; 1261 allocmap = &msp->ms_allocmap[0]; 1262 freemap = &msp->ms_freemap[0]; 1263 1264 mutex_enter(&msp->ms_lock); 1265 if (space_map_contains(freemap, offset, size)) { 1266 mutex_exit(&msp->ms_lock); 1267 return (EAGAIN); /* allocated more than once */ 1268 } 1269 1270 if (!space_map_contains(allocmap, offset, size)) { 1271 mutex_exit(&msp->ms_lock); 1272 return (ESTALE); /* not allocated at all */ 1273 } 1274 1275 space_map_remove(allocmap, offset, size); 1276 space_map_add(freemap, offset, size); 1277 1278 mutex_exit(&msp->ms_lock); 1279 1280 return (0); 1281 } 1282 1283 static void 1284 zdb_leak(space_map_t *sm, uint64_t start, uint64_t size) 1285 { 1286 metaslab_t *msp; 1287 1288 /* LINTED */ 1289 msp = (metaslab_t *)((char *)sm - offsetof(metaslab_t, ms_allocmap[0])); 1290 1291 (void) printf("leaked space: vdev %llu, offset 0x%llx, size %llu\n", 1292 (u_longlong_t)msp->ms_group->mg_vd->vdev_id, 1293 (u_longlong_t)start, 1294 (u_longlong_t)size); 1295 } 1296 1297 static void 1298 zdb_space_map_vacate(spa_t *spa) 1299 { 1300 vdev_t *rvd = spa->spa_root_vdev; 1301 vdev_t *vd; 1302 int c, m; 1303 1304 for (c = 0; c < rvd->vdev_children; c++) { 1305 vd = rvd->vdev_child[c]; 1306 for (m = 0; m < vd->vdev_ms_count; m++) { 1307 metaslab_t *msp = vd->vdev_ms[m]; 1308 mutex_enter(&msp->ms_lock); 1309 space_map_vacate(&msp->ms_allocmap[0], zdb_leak, 1310 &msp->ms_allocmap[0]); 1311 space_map_vacate(&msp->ms_freemap[0], NULL, NULL); 1312 mutex_exit(&msp->ms_lock); 1313 } 1314 } 1315 } 1316 1317 static void 1318 zdb_refresh_ubsync(spa_t *spa) 1319 { 1320 uberblock_t ub = { 0 }; 1321 vdev_t *rvd = spa->spa_root_vdev; 1322 zio_t *zio; 1323 1324 /* 1325 * Reload the uberblock. 1326 */ 1327 zio = zio_root(spa, NULL, NULL, 1328 ZIO_FLAG_CANFAIL | ZIO_FLAG_SPECULATIVE); 1329 vdev_uberblock_load(zio, rvd, &ub); 1330 (void) zio_wait(zio); 1331 1332 if (ub.ub_txg != 0) 1333 spa->spa_ubsync = ub; 1334 } 1335 1336 /* 1337 * Verify that the sum of the sizes of all blocks in the pool adds up 1338 * to the SPA's sa_alloc total. 1339 */ 1340 typedef struct zdb_blkstats { 1341 uint64_t zb_asize; 1342 uint64_t zb_lsize; 1343 uint64_t zb_psize; 1344 uint64_t zb_count; 1345 } zdb_blkstats_t; 1346 1347 #define DMU_OT_DEFERRED DMU_OT_NONE 1348 #define DMU_OT_TOTAL DMU_OT_NUMTYPES 1349 1350 #define ZB_TOTAL ZB_MAXLEVEL 1351 1352 typedef struct zdb_cb { 1353 zdb_blkstats_t zcb_type[ZB_TOTAL + 1][DMU_OT_TOTAL + 1]; 1354 uint64_t zcb_errors[256]; 1355 traverse_blk_cache_t *zcb_cache; 1356 int zcb_readfails; 1357 int zcb_haderrors; 1358 } zdb_cb_t; 1359 1360 static void 1361 zdb_count_block(spa_t *spa, zdb_cb_t *zcb, blkptr_t *bp, int type) 1362 { 1363 int i, error; 1364 1365 for (i = 0; i < 4; i++) { 1366 int l = (i < 2) ? BP_GET_LEVEL(bp) : ZB_TOTAL; 1367 int t = (i & 1) ? type : DMU_OT_TOTAL; 1368 zdb_blkstats_t *zb = &zcb->zcb_type[l][t]; 1369 1370 zb->zb_asize += BP_GET_ASIZE(bp); 1371 zb->zb_lsize += BP_GET_LSIZE(bp); 1372 zb->zb_psize += BP_GET_PSIZE(bp); 1373 zb->zb_count++; 1374 } 1375 1376 if (dump_opt['L']) 1377 return; 1378 1379 error = zdb_space_map_claim(spa, bp, &zcb->zcb_cache->bc_bookmark); 1380 1381 if (error == 0) 1382 return; 1383 1384 if (error == EAGAIN) 1385 (void) fatal("double-allocation, bp=%p", bp); 1386 1387 if (error == ESTALE) 1388 (void) fatal("reference to freed block, bp=%p", bp); 1389 1390 (void) fatal("fatal error %d in bp %p", error, bp); 1391 } 1392 1393 static int 1394 zdb_blkptr_cb(traverse_blk_cache_t *bc, spa_t *spa, void *arg) 1395 { 1396 zbookmark_t *zb = &bc->bc_bookmark; 1397 zdb_cb_t *zcb = arg; 1398 blkptr_t *bp = &bc->bc_blkptr; 1399 dmu_object_type_t type = BP_GET_TYPE(bp); 1400 char blkbuf[BP_SPRINTF_LEN]; 1401 int error = 0; 1402 1403 if (bc->bc_errno) { 1404 if (zcb->zcb_readfails++ < 10 && dump_opt['L']) { 1405 zdb_refresh_ubsync(spa); 1406 error = EAGAIN; 1407 } else { 1408 zcb->zcb_haderrors = 1; 1409 zcb->zcb_errors[bc->bc_errno]++; 1410 error = ERESTART; 1411 } 1412 1413 if (dump_opt['b'] >= 3 || (dump_opt['b'] >= 2 && bc->bc_errno)) 1414 sprintf_blkptr(blkbuf, BP_SPRINTF_LEN, bp); 1415 else 1416 blkbuf[0] = '\0'; 1417 1418 (void) printf("zdb_blkptr_cb: Got error %d reading " 1419 "<%llu, %llu, %lld, %llx> %s -- %s\n", 1420 bc->bc_errno, 1421 (u_longlong_t)zb->zb_objset, 1422 (u_longlong_t)zb->zb_object, 1423 (u_longlong_t)zb->zb_level, 1424 (u_longlong_t)zb->zb_blkid, 1425 blkbuf, 1426 error == EAGAIN ? "retrying" : "skipping"); 1427 1428 return (error); 1429 } 1430 1431 zcb->zcb_readfails = 0; 1432 1433 ASSERT(bp->blk_birth != 0); 1434 1435 zdb_count_block(spa, zcb, bp, type); 1436 1437 if (dump_opt['b'] >= 4) { 1438 sprintf_blkptr(blkbuf, BP_SPRINTF_LEN, bp); 1439 (void) printf("objset %llu object %llu offset 0x%llx %s\n", 1440 (u_longlong_t)zb->zb_objset, 1441 (u_longlong_t)zb->zb_object, 1442 (u_longlong_t)blkid2offset(bc->bc_dnode, 1443 zb->zb_level, zb->zb_blkid), 1444 blkbuf); 1445 } 1446 1447 return (0); 1448 } 1449 1450 static int 1451 dump_block_stats(spa_t *spa) 1452 { 1453 traverse_handle_t *th; 1454 zdb_cb_t zcb = { 0 }; 1455 traverse_blk_cache_t dummy_cache = { 0 }; 1456 zdb_blkstats_t *zb, *tzb; 1457 uint64_t alloc, space; 1458 int leaks = 0; 1459 int advance = zdb_advance; 1460 int flags; 1461 int e; 1462 1463 zcb.zcb_cache = &dummy_cache; 1464 1465 if (dump_opt['c']) 1466 advance |= ADVANCE_DATA; 1467 1468 advance |= ADVANCE_PRUNE | ADVANCE_ZIL; 1469 1470 (void) printf("\nTraversing all blocks to %sverify" 1471 " nothing leaked ...\n", 1472 dump_opt['c'] ? "verify checksums and " : ""); 1473 1474 /* 1475 * Load all space maps. As we traverse the pool, if we find a block 1476 * that's not in its space map, that indicates a double-allocation, 1477 * reference to a freed block, or an unclaimed block. Otherwise we 1478 * remove the block from the space map. If the space maps are not 1479 * empty when we're done, that indicates leaked blocks. 1480 */ 1481 if (!dump_opt['L']) 1482 zdb_space_map_load(spa); 1483 1484 /* 1485 * If there's a deferred-free bplist, process that first. 1486 */ 1487 if (spa->spa_sync_bplist_obj != 0) { 1488 bplist_t *bpl = &spa->spa_sync_bplist; 1489 blkptr_t blk; 1490 uint64_t itor = 0; 1491 1492 VERIFY(0 == bplist_open(bpl, spa->spa_meta_objset, 1493 spa->spa_sync_bplist_obj)); 1494 1495 while (bplist_iterate(bpl, &itor, &blk) == 0) { 1496 zdb_count_block(spa, &zcb, &blk, DMU_OT_DEFERRED); 1497 if (dump_opt['b'] >= 4) { 1498 char blkbuf[BP_SPRINTF_LEN]; 1499 sprintf_blkptr(blkbuf, BP_SPRINTF_LEN, &blk); 1500 (void) printf("[%s] %s\n", 1501 "deferred free", blkbuf); 1502 } 1503 } 1504 1505 bplist_close(bpl); 1506 } 1507 1508 /* 1509 * Now traverse the pool. If we're reading all data to verify 1510 * checksums, do a scrubbing read so that we validate all copies. 1511 */ 1512 flags = ZIO_FLAG_CANFAIL; 1513 if (advance & ADVANCE_DATA) 1514 flags |= ZIO_FLAG_SCRUB; 1515 th = traverse_init(spa, zdb_blkptr_cb, &zcb, advance, flags); 1516 th->th_noread = zdb_noread; 1517 1518 traverse_add_pool(th, 0, spa_first_txg(spa)); 1519 1520 while (traverse_more(th) == EAGAIN) 1521 continue; 1522 1523 traverse_fini(th); 1524 1525 if (zcb.zcb_haderrors) { 1526 (void) printf("\nError counts:\n\n"); 1527 (void) printf("\t%5s %s\n", "errno", "count"); 1528 for (e = 0; e < 256; e++) { 1529 if (zcb.zcb_errors[e] != 0) { 1530 (void) printf("\t%5d %llu\n", 1531 e, (u_longlong_t)zcb.zcb_errors[e]); 1532 } 1533 } 1534 } 1535 1536 /* 1537 * Report any leaked segments. 1538 */ 1539 if (!dump_opt['L']) 1540 zdb_space_map_vacate(spa); 1541 1542 if (dump_opt['L']) 1543 (void) printf("\n\n *** Live pool traversal; " 1544 "block counts are only approximate ***\n\n"); 1545 1546 alloc = spa_get_alloc(spa); 1547 space = spa_get_space(spa); 1548 1549 tzb = &zcb.zcb_type[ZB_TOTAL][DMU_OT_TOTAL]; 1550 1551 if (tzb->zb_asize == alloc) { 1552 (void) printf("\n\tNo leaks (block sum matches space" 1553 " maps exactly)\n"); 1554 } else { 1555 (void) printf("block traversal size %llu != alloc %llu " 1556 "(leaked %lld)\n", 1557 (u_longlong_t)tzb->zb_asize, 1558 (u_longlong_t)alloc, 1559 (u_longlong_t)(alloc - tzb->zb_asize)); 1560 leaks = 1; 1561 } 1562 1563 if (tzb->zb_count == 0) 1564 return (2); 1565 1566 (void) printf("\n"); 1567 (void) printf("\tbp count: %10llu\n", 1568 (u_longlong_t)tzb->zb_count); 1569 (void) printf("\tbp logical: %10llu\t avg: %6llu\n", 1570 (u_longlong_t)tzb->zb_lsize, 1571 (u_longlong_t)(tzb->zb_lsize / tzb->zb_count)); 1572 (void) printf("\tbp physical: %10llu\t avg:" 1573 " %6llu\tcompression: %6.2f\n", 1574 (u_longlong_t)tzb->zb_psize, 1575 (u_longlong_t)(tzb->zb_psize / tzb->zb_count), 1576 (double)tzb->zb_lsize / tzb->zb_psize); 1577 (void) printf("\tbp allocated: %10llu\t avg:" 1578 " %6llu\tcompression: %6.2f\n", 1579 (u_longlong_t)tzb->zb_asize, 1580 (u_longlong_t)(tzb->zb_asize / tzb->zb_count), 1581 (double)tzb->zb_lsize / tzb->zb_asize); 1582 (void) printf("\tSPA allocated: %10llu\tused: %5.2f%%\n", 1583 (u_longlong_t)alloc, 100.0 * alloc / space); 1584 1585 if (dump_opt['b'] >= 2) { 1586 int l, t, level; 1587 (void) printf("\nBlocks\tLSIZE\tPSIZE\tASIZE" 1588 "\t avg\t comp\t%%Total\tType\n"); 1589 1590 for (t = 0; t <= DMU_OT_NUMTYPES; t++) { 1591 char csize[6], lsize[6], psize[6], asize[6], avg[6]; 1592 char *typename; 1593 1594 typename = t == DMU_OT_DEFERRED ? "deferred free" : 1595 t == DMU_OT_TOTAL ? "Total" : dmu_ot[t].ot_name; 1596 1597 if (zcb.zcb_type[ZB_TOTAL][t].zb_asize == 0) { 1598 (void) printf("%6s\t%5s\t%5s\t%5s" 1599 "\t%5s\t%5s\t%6s\t%s\n", 1600 "-", 1601 "-", 1602 "-", 1603 "-", 1604 "-", 1605 "-", 1606 "-", 1607 typename); 1608 continue; 1609 } 1610 1611 for (l = ZB_TOTAL - 1; l >= -1; l--) { 1612 level = (l == -1 ? ZB_TOTAL : l); 1613 zb = &zcb.zcb_type[level][t]; 1614 1615 if (zb->zb_asize == 0) 1616 continue; 1617 1618 if (dump_opt['b'] < 3 && level != ZB_TOTAL) 1619 continue; 1620 1621 if (level == 0 && zb->zb_asize == 1622 zcb.zcb_type[ZB_TOTAL][t].zb_asize) 1623 continue; 1624 1625 nicenum(zb->zb_count, csize); 1626 nicenum(zb->zb_lsize, lsize); 1627 nicenum(zb->zb_psize, psize); 1628 nicenum(zb->zb_asize, asize); 1629 nicenum(zb->zb_asize / zb->zb_count, avg); 1630 1631 (void) printf("%6s\t%5s\t%5s\t%5s\t%5s" 1632 "\t%5.2f\t%6.2f\t", 1633 csize, lsize, psize, asize, avg, 1634 (double)zb->zb_lsize / zb->zb_psize, 1635 100.0 * zb->zb_asize / tzb->zb_asize); 1636 1637 if (level == ZB_TOTAL) 1638 (void) printf("%s\n", typename); 1639 else 1640 (void) printf(" L%d %s\n", 1641 level, typename); 1642 } 1643 } 1644 } 1645 1646 (void) printf("\n"); 1647 1648 if (leaks) 1649 return (2); 1650 1651 if (zcb.zcb_haderrors) 1652 return (3); 1653 1654 return (0); 1655 } 1656 1657 static void 1658 dump_zpool(spa_t *spa) 1659 { 1660 dsl_pool_t *dp = spa_get_dsl(spa); 1661 int rc = 0; 1662 1663 if (dump_opt['u']) 1664 dump_uberblock(&spa->spa_uberblock); 1665 1666 if (dump_opt['d'] || dump_opt['i']) { 1667 dump_dir(dp->dp_meta_objset); 1668 if (dump_opt['d'] >= 3) { 1669 dump_bplist(dp->dp_meta_objset, 1670 spa->spa_sync_bplist_obj, "Deferred frees"); 1671 dump_dtl(spa->spa_root_vdev, 0); 1672 dump_metaslabs(spa); 1673 } 1674 dmu_objset_find(spa->spa_name, dump_one_dir, NULL, 1675 DS_FIND_SNAPSHOTS); 1676 } 1677 1678 if (dump_opt['b'] || dump_opt['c']) 1679 rc = dump_block_stats(spa); 1680 1681 if (dump_opt['s']) 1682 show_pool_stats(spa); 1683 1684 if (rc != 0) 1685 exit(rc); 1686 } 1687 1688 int 1689 main(int argc, char **argv) 1690 { 1691 int i, c; 1692 struct rlimit rl = { 1024, 1024 }; 1693 spa_t *spa; 1694 objset_t *os = NULL; 1695 char *endstr; 1696 int dump_all = 1; 1697 int verbose = 0; 1698 int error; 1699 int flag, set; 1700 vdev_knob_t *vk; 1701 1702 (void) setrlimit(RLIMIT_NOFILE, &rl); 1703 1704 dprintf_setup(&argc, argv); 1705 1706 while ((c = getopt(argc, argv, "udibcsvCLO:B:Ul")) != -1) { 1707 switch (c) { 1708 case 'u': 1709 case 'd': 1710 case 'i': 1711 case 'b': 1712 case 'c': 1713 case 's': 1714 case 'C': 1715 case 'l': 1716 dump_opt[c]++; 1717 dump_all = 0; 1718 break; 1719 case 'L': 1720 dump_opt[c]++; 1721 break; 1722 case 'O': 1723 endstr = optarg; 1724 if (endstr[0] == '!') { 1725 endstr++; 1726 set = 0; 1727 } else { 1728 set = 1; 1729 } 1730 if (strcmp(endstr, "post") == 0) { 1731 flag = ADVANCE_PRE; 1732 set = !set; 1733 } else if (strcmp(endstr, "pre") == 0) { 1734 flag = ADVANCE_PRE; 1735 } else if (strcmp(endstr, "prune") == 0) { 1736 flag = ADVANCE_PRUNE; 1737 } else if (strcmp(endstr, "data") == 0) { 1738 flag = ADVANCE_DATA; 1739 } else if (strcmp(endstr, "holes") == 0) { 1740 flag = ADVANCE_HOLES; 1741 } else { 1742 usage(); 1743 } 1744 if (set) 1745 zdb_advance |= flag; 1746 else 1747 zdb_advance &= ~flag; 1748 break; 1749 case 'B': 1750 endstr = optarg - 1; 1751 zdb_noread.zb_objset = strtoull(endstr + 1, &endstr, 0); 1752 zdb_noread.zb_object = strtoull(endstr + 1, &endstr, 0); 1753 zdb_noread.zb_level = strtol(endstr + 1, &endstr, 0); 1754 zdb_noread.zb_blkid = strtoull(endstr + 1, &endstr, 16); 1755 (void) printf("simulating bad block " 1756 "<%llu, %llu, %lld, %llx>\n", 1757 (u_longlong_t)zdb_noread.zb_objset, 1758 (u_longlong_t)zdb_noread.zb_object, 1759 (u_longlong_t)zdb_noread.zb_level, 1760 (u_longlong_t)zdb_noread.zb_blkid); 1761 break; 1762 case 'v': 1763 verbose++; 1764 break; 1765 case 'U': 1766 spa_config_dir = "/tmp"; 1767 break; 1768 default: 1769 usage(); 1770 break; 1771 } 1772 } 1773 1774 kernel_init(FREAD); 1775 1776 /* 1777 * Disable vdev caching. If we don't do this, live pool traversal 1778 * won't make progress because it will never see disk updates. 1779 */ 1780 for (vk = vdev_knob_next(NULL); vk != NULL; vk = vdev_knob_next(vk)) { 1781 if (strcmp(vk->vk_name, "cache_size") == 0) 1782 vk->vk_default = 0; 1783 } 1784 1785 for (c = 0; c < 256; c++) { 1786 if (dump_all && c != 'L' && c != 'l') 1787 dump_opt[c] = 1; 1788 if (dump_opt[c]) 1789 dump_opt[c] += verbose; 1790 } 1791 1792 argc -= optind; 1793 argv += optind; 1794 1795 if (argc < 1) { 1796 if (dump_opt['C']) { 1797 dump_config(NULL); 1798 return (0); 1799 } 1800 usage(); 1801 } 1802 1803 if (dump_opt['l']) { 1804 dump_label(argv[0]); 1805 return (0); 1806 } 1807 1808 if (dump_opt['C']) 1809 dump_config(argv[0]); 1810 1811 if (strchr(argv[0], '/') != NULL) { 1812 error = dmu_objset_open(argv[0], DMU_OST_ANY, 1813 DS_MODE_STANDARD | DS_MODE_READONLY, &os); 1814 } else { 1815 error = spa_open(argv[0], &spa, FTAG); 1816 } 1817 1818 if (error) 1819 fatal("can't open %s: error %d", argv[0], error); 1820 1821 argv++; 1822 if (--argc > 0) { 1823 zopt_objects = argc; 1824 zopt_object = calloc(zopt_objects, sizeof (uint64_t)); 1825 for (i = 0; i < zopt_objects; i++) { 1826 errno = 0; 1827 zopt_object[i] = strtoull(argv[i], NULL, 0); 1828 if (zopt_object[i] == 0 && errno != 0) 1829 fatal("bad object number %s: %s", 1830 argv[i], strerror(errno)); 1831 } 1832 } 1833 1834 if (os != NULL) { 1835 dump_dir(os); 1836 dmu_objset_close(os); 1837 } else { 1838 dump_zpool(spa); 1839 spa_close(spa, FTAG); 1840 } 1841 1842 kernel_fini(); 1843 1844 return (0); 1845 } 1846