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