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