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 uint64_t refdbytes, usedobjs, scratch; 1051 char numbuf[8]; 1052 char blkbuf[BP_SPRINTF_LEN]; 1053 char osname[MAXNAMELEN]; 1054 char *type = "UNKNOWN"; 1055 int verbosity = dump_opt['d']; 1056 int print_header = 1; 1057 int i, error; 1058 1059 dmu_objset_fast_stat(os, &dds); 1060 1061 if (dds.dds_type < DMU_OST_NUMTYPES) 1062 type = objset_types[dds.dds_type]; 1063 1064 if (dds.dds_type == DMU_OST_META) { 1065 dds.dds_creation_txg = TXG_INITIAL; 1066 usedobjs = os->os->os_rootbp.blk_fill; 1067 refdbytes = 1068 os->os->os_spa->spa_dsl_pool->dp_mos_dir->dd_used_bytes; 1069 } else { 1070 dmu_objset_space(os, &refdbytes, &scratch, &usedobjs, &scratch); 1071 } 1072 1073 ASSERT3U(usedobjs, ==, os->os->os_rootbp.blk_fill); 1074 1075 nicenum(refdbytes, numbuf); 1076 1077 if (verbosity >= 4) { 1078 (void) strcpy(blkbuf, ", rootbp "); 1079 sprintf_blkptr(blkbuf + strlen(blkbuf), 1080 BP_SPRINTF_LEN - strlen(blkbuf), &os->os->os_rootbp); 1081 } else { 1082 blkbuf[0] = '\0'; 1083 } 1084 1085 dmu_objset_name(os, osname); 1086 1087 (void) printf("Dataset %s [%s], ID %llu, cr_txg %llu, " 1088 "%s, %llu objects%s\n", 1089 osname, type, (u_longlong_t)dmu_objset_id(os), 1090 (u_longlong_t)dds.dds_creation_txg, 1091 numbuf, (u_longlong_t)usedobjs, blkbuf); 1092 1093 dump_intent_log(dmu_objset_zil(os)); 1094 1095 if (dmu_objset_ds(os) != NULL) 1096 dump_bplist(dmu_objset_pool(os)->dp_meta_objset, 1097 dmu_objset_ds(os)->ds_phys->ds_deadlist_obj, "Deadlist"); 1098 1099 if (verbosity < 2) 1100 return; 1101 1102 if (zopt_objects != 0) { 1103 for (i = 0; i < zopt_objects; i++) 1104 dump_object(os, zopt_object[i], verbosity, 1105 &print_header); 1106 (void) printf("\n"); 1107 return; 1108 } 1109 1110 dump_object(os, 0, verbosity, &print_header); 1111 object_count = 1; 1112 1113 object = 0; 1114 while ((error = dmu_object_next(os, &object, B_FALSE)) == 0) { 1115 dump_object(os, object, verbosity, &print_header); 1116 object_count++; 1117 } 1118 1119 ASSERT3U(object_count, ==, usedobjs); 1120 1121 (void) printf("\n"); 1122 1123 if (error != ESRCH) 1124 fatal("dmu_object_next() = %d", error); 1125 } 1126 1127 static void 1128 dump_uberblock(uberblock_t *ub) 1129 { 1130 time_t timestamp = ub->ub_timestamp; 1131 1132 (void) printf("Uberblock\n\n"); 1133 (void) printf("\tmagic = %016llx\n", (u_longlong_t)ub->ub_magic); 1134 (void) printf("\tversion = %llu\n", (u_longlong_t)ub->ub_version); 1135 (void) printf("\ttxg = %llu\n", (u_longlong_t)ub->ub_txg); 1136 (void) printf("\tguid_sum = %llu\n", (u_longlong_t)ub->ub_guid_sum); 1137 (void) printf("\ttimestamp = %llu UTC = %s", 1138 (u_longlong_t)ub->ub_timestamp, asctime(localtime(×tamp))); 1139 if (dump_opt['u'] >= 3) { 1140 char blkbuf[BP_SPRINTF_LEN]; 1141 sprintf_blkptr(blkbuf, BP_SPRINTF_LEN, &ub->ub_rootbp); 1142 (void) printf("\trootbp = %s\n", blkbuf); 1143 } 1144 (void) printf("\n"); 1145 } 1146 1147 static void 1148 dump_config(const char *pool) 1149 { 1150 spa_t *spa = NULL; 1151 1152 mutex_enter(&spa_namespace_lock); 1153 while ((spa = spa_next(spa)) != NULL) { 1154 if (pool == NULL) 1155 (void) printf("%s\n", spa_name(spa)); 1156 if (pool == NULL || strcmp(pool, spa_name(spa)) == 0) 1157 dump_nvlist(spa->spa_config, 4); 1158 } 1159 mutex_exit(&spa_namespace_lock); 1160 } 1161 1162 static void 1163 dump_label(const char *dev) 1164 { 1165 int fd; 1166 vdev_label_t label; 1167 char *buf = label.vl_vdev_phys.vp_nvlist; 1168 size_t buflen = sizeof (label.vl_vdev_phys.vp_nvlist); 1169 struct stat64 statbuf; 1170 uint64_t psize; 1171 int l; 1172 1173 if ((fd = open64(dev, O_RDONLY)) < 0) { 1174 (void) printf("cannot open '%s': %s\n", dev, strerror(errno)); 1175 exit(1); 1176 } 1177 1178 if (fstat64(fd, &statbuf) != 0) { 1179 (void) printf("failed to stat '%s': %s\n", dev, 1180 strerror(errno)); 1181 exit(1); 1182 } 1183 1184 psize = statbuf.st_size; 1185 psize = P2ALIGN(psize, (uint64_t)sizeof (vdev_label_t)); 1186 1187 for (l = 0; l < VDEV_LABELS; l++) { 1188 1189 nvlist_t *config = NULL; 1190 1191 (void) printf("--------------------------------------------\n"); 1192 (void) printf("LABEL %d\n", l); 1193 (void) printf("--------------------------------------------\n"); 1194 1195 if (pread64(fd, &label, sizeof (label), 1196 vdev_label_offset(psize, l, 0)) != sizeof (label)) { 1197 (void) printf("failed to read label %d\n", l); 1198 continue; 1199 } 1200 1201 if (nvlist_unpack(buf, buflen, &config, 0) != 0) { 1202 (void) printf("failed to unpack label %d\n", l); 1203 continue; 1204 } 1205 dump_nvlist(config, 4); 1206 nvlist_free(config); 1207 } 1208 } 1209 1210 /*ARGSUSED*/ 1211 static int 1212 dump_one_dir(char *dsname, void *arg) 1213 { 1214 int error; 1215 objset_t *os; 1216 1217 error = dmu_objset_open(dsname, DMU_OST_ANY, 1218 DS_MODE_STANDARD | DS_MODE_READONLY, &os); 1219 if (error) { 1220 (void) printf("Could not open %s\n", dsname); 1221 return (0); 1222 } 1223 dump_dir(os); 1224 dmu_objset_close(os); 1225 return (0); 1226 } 1227 1228 static void 1229 zdb_space_map_load(spa_t *spa) 1230 { 1231 vdev_t *rvd = spa->spa_root_vdev; 1232 vdev_t *vd; 1233 int c, m, error; 1234 1235 for (c = 0; c < rvd->vdev_children; c++) { 1236 vd = rvd->vdev_child[c]; 1237 for (m = 0; m < vd->vdev_ms_count; m++) { 1238 metaslab_t *msp = vd->vdev_ms[m]; 1239 mutex_enter(&msp->ms_lock); 1240 error = space_map_load(&msp->ms_allocmap[0], NULL, 1241 SM_ALLOC, &msp->ms_smo, spa->spa_meta_objset); 1242 mutex_exit(&msp->ms_lock); 1243 if (error) 1244 fatal("%s bad space map #%d, error %d", 1245 spa->spa_name, c, error); 1246 } 1247 } 1248 } 1249 1250 static int 1251 zdb_space_map_claim(spa_t *spa, blkptr_t *bp, zbookmark_t *zb) 1252 { 1253 dva_t *dva = bp->blk_dva; 1254 vdev_t *vd; 1255 metaslab_t *msp; 1256 space_map_t *allocmap, *freemap; 1257 int error; 1258 int d; 1259 blkptr_t blk = *bp; 1260 1261 for (d = 0; d < BP_GET_NDVAS(bp); d++) { 1262 uint64_t vdev = DVA_GET_VDEV(&dva[d]); 1263 uint64_t offset = DVA_GET_OFFSET(&dva[d]); 1264 uint64_t size = DVA_GET_ASIZE(&dva[d]); 1265 1266 if ((vd = vdev_lookup_top(spa, vdev)) == NULL) 1267 return (ENXIO); 1268 1269 if ((offset >> vd->vdev_ms_shift) >= vd->vdev_ms_count) 1270 return (ENXIO); 1271 1272 msp = vd->vdev_ms[offset >> vd->vdev_ms_shift]; 1273 allocmap = &msp->ms_allocmap[0]; 1274 freemap = &msp->ms_freemap[0]; 1275 1276 /* Prepare our copy of the bp in case we need to read GBHs */ 1277 if (DVA_GET_GANG(&dva[d])) { 1278 size = vdev_psize_to_asize(vd, SPA_GANGBLOCKSIZE); 1279 DVA_SET_ASIZE(&blk.blk_dva[d], size); 1280 DVA_SET_GANG(&blk.blk_dva[d], 0); 1281 } 1282 1283 mutex_enter(&msp->ms_lock); 1284 if (space_map_contains(freemap, offset, size)) { 1285 mutex_exit(&msp->ms_lock); 1286 return (EAGAIN); /* allocated more than once */ 1287 } 1288 1289 if (!space_map_contains(allocmap, offset, size)) { 1290 mutex_exit(&msp->ms_lock); 1291 return (ESTALE); /* not allocated at all */ 1292 } 1293 1294 space_map_remove(allocmap, offset, size); 1295 space_map_add(freemap, offset, size); 1296 1297 mutex_exit(&msp->ms_lock); 1298 } 1299 1300 if (BP_IS_GANG(bp)) { 1301 zio_gbh_phys_t gbh; 1302 int g; 1303 1304 /* LINTED - compile time assert */ 1305 ASSERT(sizeof (zio_gbh_phys_t) == SPA_GANGBLOCKSIZE); 1306 1307 BP_SET_CHECKSUM(&blk, ZIO_CHECKSUM_GANG_HEADER); 1308 BP_SET_PSIZE(&blk, SPA_GANGBLOCKSIZE); 1309 BP_SET_LSIZE(&blk, SPA_GANGBLOCKSIZE); 1310 BP_SET_COMPRESS(&blk, ZIO_COMPRESS_OFF); 1311 error = zio_wait(zio_read(NULL, spa, &blk, &gbh, 1312 SPA_GANGBLOCKSIZE, NULL, NULL, ZIO_PRIORITY_SYNC_READ, 1313 ZIO_FLAG_CANFAIL | ZIO_FLAG_CONFIG_HELD, zb)); 1314 if (error) 1315 return (error); 1316 if (BP_SHOULD_BYTESWAP(&blk)) 1317 byteswap_uint64_array(&gbh, SPA_GANGBLOCKSIZE); 1318 for (g = 0; g < SPA_GBH_NBLKPTRS; g++) { 1319 if (BP_IS_HOLE(&gbh.zg_blkptr[g])) 1320 break; 1321 error = zdb_space_map_claim(spa, &gbh.zg_blkptr[g], zb); 1322 if (error) 1323 return (error); 1324 } 1325 } 1326 1327 return (0); 1328 } 1329 1330 static void 1331 zdb_leak(space_map_t *sm, uint64_t start, uint64_t size) 1332 { 1333 metaslab_t *msp; 1334 1335 /* LINTED */ 1336 msp = (metaslab_t *)((char *)sm - offsetof(metaslab_t, ms_allocmap[0])); 1337 1338 (void) printf("leaked space: vdev %llu, offset 0x%llx, size %llu\n", 1339 (u_longlong_t)msp->ms_group->mg_vd->vdev_id, 1340 (u_longlong_t)start, 1341 (u_longlong_t)size); 1342 } 1343 1344 static void 1345 zdb_space_map_unload(spa_t *spa) 1346 { 1347 vdev_t *rvd = spa->spa_root_vdev; 1348 vdev_t *vd; 1349 int c, m; 1350 1351 for (c = 0; c < rvd->vdev_children; c++) { 1352 vd = rvd->vdev_child[c]; 1353 for (m = 0; m < vd->vdev_ms_count; m++) { 1354 metaslab_t *msp = vd->vdev_ms[m]; 1355 mutex_enter(&msp->ms_lock); 1356 space_map_vacate(&msp->ms_allocmap[0], zdb_leak, 1357 &msp->ms_allocmap[0]); 1358 space_map_unload(&msp->ms_allocmap[0]); 1359 space_map_vacate(&msp->ms_freemap[0], NULL, NULL); 1360 mutex_exit(&msp->ms_lock); 1361 } 1362 } 1363 } 1364 1365 static void 1366 zdb_refresh_ubsync(spa_t *spa) 1367 { 1368 uberblock_t ub = { 0 }; 1369 vdev_t *rvd = spa->spa_root_vdev; 1370 zio_t *zio; 1371 1372 /* 1373 * Reload the uberblock. 1374 */ 1375 zio = zio_root(spa, NULL, NULL, 1376 ZIO_FLAG_CANFAIL | ZIO_FLAG_SPECULATIVE); 1377 vdev_uberblock_load(zio, rvd, &ub); 1378 (void) zio_wait(zio); 1379 1380 if (ub.ub_txg != 0) 1381 spa->spa_ubsync = ub; 1382 } 1383 1384 /* 1385 * Verify that the sum of the sizes of all blocks in the pool adds up 1386 * to the SPA's sa_alloc total. 1387 */ 1388 typedef struct zdb_blkstats { 1389 uint64_t zb_asize; 1390 uint64_t zb_lsize; 1391 uint64_t zb_psize; 1392 uint64_t zb_count; 1393 } zdb_blkstats_t; 1394 1395 #define DMU_OT_DEFERRED DMU_OT_NONE 1396 #define DMU_OT_TOTAL DMU_OT_NUMTYPES 1397 1398 #define ZB_TOTAL ZB_MAXLEVEL 1399 1400 typedef struct zdb_cb { 1401 zdb_blkstats_t zcb_type[ZB_TOTAL + 1][DMU_OT_TOTAL + 1]; 1402 uint64_t zcb_errors[256]; 1403 traverse_blk_cache_t *zcb_cache; 1404 int zcb_readfails; 1405 int zcb_haderrors; 1406 } zdb_cb_t; 1407 1408 static void 1409 zdb_count_block(spa_t *spa, zdb_cb_t *zcb, blkptr_t *bp, int type) 1410 { 1411 int i, error; 1412 1413 for (i = 0; i < 4; i++) { 1414 int l = (i < 2) ? BP_GET_LEVEL(bp) : ZB_TOTAL; 1415 int t = (i & 1) ? type : DMU_OT_TOTAL; 1416 zdb_blkstats_t *zb = &zcb->zcb_type[l][t]; 1417 1418 zb->zb_asize += BP_GET_ASIZE(bp); 1419 zb->zb_lsize += BP_GET_LSIZE(bp); 1420 zb->zb_psize += BP_GET_PSIZE(bp); 1421 zb->zb_count++; 1422 } 1423 1424 if (dump_opt['L']) 1425 return; 1426 1427 error = zdb_space_map_claim(spa, bp, &zcb->zcb_cache->bc_bookmark); 1428 1429 if (error == 0) 1430 return; 1431 1432 if (error == EAGAIN) 1433 (void) fatal("double-allocation, bp=%p", bp); 1434 1435 if (error == ESTALE) 1436 (void) fatal("reference to freed block, bp=%p", bp); 1437 1438 (void) fatal("fatal error %d in bp %p", error, bp); 1439 } 1440 1441 static int 1442 zdb_blkptr_cb(traverse_blk_cache_t *bc, spa_t *spa, void *arg) 1443 { 1444 zbookmark_t *zb = &bc->bc_bookmark; 1445 zdb_cb_t *zcb = arg; 1446 blkptr_t *bp = &bc->bc_blkptr; 1447 dmu_object_type_t type = BP_GET_TYPE(bp); 1448 char blkbuf[BP_SPRINTF_LEN]; 1449 int error = 0; 1450 1451 if (bc->bc_errno) { 1452 if (zcb->zcb_readfails++ < 10 && dump_opt['L']) { 1453 zdb_refresh_ubsync(spa); 1454 error = EAGAIN; 1455 } else { 1456 zcb->zcb_haderrors = 1; 1457 zcb->zcb_errors[bc->bc_errno]++; 1458 error = ERESTART; 1459 } 1460 1461 if (dump_opt['b'] >= 3 || (dump_opt['b'] >= 2 && bc->bc_errno)) 1462 sprintf_blkptr(blkbuf, BP_SPRINTF_LEN, bp); 1463 else 1464 blkbuf[0] = '\0'; 1465 1466 (void) printf("zdb_blkptr_cb: Got error %d reading " 1467 "<%llu, %llu, %lld, %llx> %s -- %s\n", 1468 bc->bc_errno, 1469 (u_longlong_t)zb->zb_objset, 1470 (u_longlong_t)zb->zb_object, 1471 (u_longlong_t)zb->zb_level, 1472 (u_longlong_t)zb->zb_blkid, 1473 blkbuf, 1474 error == EAGAIN ? "retrying" : "skipping"); 1475 1476 return (error); 1477 } 1478 1479 zcb->zcb_readfails = 0; 1480 1481 ASSERT(!BP_IS_HOLE(bp)); 1482 1483 if (dump_opt['b'] >= 4) { 1484 sprintf_blkptr(blkbuf, BP_SPRINTF_LEN, bp); 1485 (void) printf("objset %llu object %llu offset 0x%llx %s\n", 1486 (u_longlong_t)zb->zb_objset, 1487 (u_longlong_t)zb->zb_object, 1488 (u_longlong_t)blkid2offset(bc->bc_dnode, 1489 zb->zb_level, zb->zb_blkid), 1490 blkbuf); 1491 } 1492 1493 zdb_count_block(spa, zcb, bp, type); 1494 1495 return (0); 1496 } 1497 1498 static int 1499 dump_block_stats(spa_t *spa) 1500 { 1501 traverse_handle_t *th; 1502 zdb_cb_t zcb = { 0 }; 1503 traverse_blk_cache_t dummy_cache = { 0 }; 1504 zdb_blkstats_t *zb, *tzb; 1505 uint64_t alloc, space; 1506 int leaks = 0; 1507 int advance = zdb_advance; 1508 int flags; 1509 int e; 1510 1511 zcb.zcb_cache = &dummy_cache; 1512 1513 if (dump_opt['c']) 1514 advance |= ADVANCE_DATA; 1515 1516 advance |= ADVANCE_PRUNE | ADVANCE_ZIL; 1517 1518 (void) printf("\nTraversing all blocks to %sverify" 1519 " nothing leaked ...\n", 1520 dump_opt['c'] ? "verify checksums and " : ""); 1521 1522 /* 1523 * Load all space maps. As we traverse the pool, if we find a block 1524 * that's not in its space map, that indicates a double-allocation, 1525 * reference to a freed block, or an unclaimed block. Otherwise we 1526 * remove the block from the space map. If the space maps are not 1527 * empty when we're done, that indicates leaked blocks. 1528 */ 1529 if (!dump_opt['L']) 1530 zdb_space_map_load(spa); 1531 1532 /* 1533 * If there's a deferred-free bplist, process that first. 1534 */ 1535 if (spa->spa_sync_bplist_obj != 0) { 1536 bplist_t *bpl = &spa->spa_sync_bplist; 1537 blkptr_t blk; 1538 uint64_t itor = 0; 1539 1540 VERIFY(0 == bplist_open(bpl, spa->spa_meta_objset, 1541 spa->spa_sync_bplist_obj)); 1542 1543 while (bplist_iterate(bpl, &itor, &blk) == 0) { 1544 if (dump_opt['b'] >= 4) { 1545 char blkbuf[BP_SPRINTF_LEN]; 1546 sprintf_blkptr(blkbuf, BP_SPRINTF_LEN, &blk); 1547 (void) printf("[%s] %s\n", 1548 "deferred free", blkbuf); 1549 } 1550 zdb_count_block(spa, &zcb, &blk, DMU_OT_DEFERRED); 1551 } 1552 1553 bplist_close(bpl); 1554 } 1555 1556 /* 1557 * Now traverse the pool. If we're reading all data to verify 1558 * checksums, do a scrubbing read so that we validate all copies. 1559 */ 1560 flags = ZIO_FLAG_CANFAIL; 1561 if (advance & ADVANCE_DATA) 1562 flags |= ZIO_FLAG_SCRUB; 1563 th = traverse_init(spa, zdb_blkptr_cb, &zcb, advance, flags); 1564 th->th_noread = zdb_noread; 1565 1566 traverse_add_pool(th, 0, spa_first_txg(spa) + TXG_CONCURRENT_STATES); 1567 1568 while (traverse_more(th) == EAGAIN) 1569 continue; 1570 1571 traverse_fini(th); 1572 1573 if (zcb.zcb_haderrors) { 1574 (void) printf("\nError counts:\n\n"); 1575 (void) printf("\t%5s %s\n", "errno", "count"); 1576 for (e = 0; e < 256; e++) { 1577 if (zcb.zcb_errors[e] != 0) { 1578 (void) printf("\t%5d %llu\n", 1579 e, (u_longlong_t)zcb.zcb_errors[e]); 1580 } 1581 } 1582 } 1583 1584 /* 1585 * Report any leaked segments. 1586 */ 1587 if (!dump_opt['L']) 1588 zdb_space_map_unload(spa); 1589 1590 if (dump_opt['L']) 1591 (void) printf("\n\n *** Live pool traversal; " 1592 "block counts are only approximate ***\n\n"); 1593 1594 alloc = spa_get_alloc(spa); 1595 space = spa_get_space(spa); 1596 1597 tzb = &zcb.zcb_type[ZB_TOTAL][DMU_OT_TOTAL]; 1598 1599 if (tzb->zb_asize == alloc) { 1600 (void) printf("\n\tNo leaks (block sum matches space" 1601 " maps exactly)\n"); 1602 } else { 1603 (void) printf("block traversal size %llu != alloc %llu " 1604 "(leaked %lld)\n", 1605 (u_longlong_t)tzb->zb_asize, 1606 (u_longlong_t)alloc, 1607 (u_longlong_t)(alloc - tzb->zb_asize)); 1608 leaks = 1; 1609 } 1610 1611 if (tzb->zb_count == 0) 1612 return (2); 1613 1614 (void) printf("\n"); 1615 (void) printf("\tbp count: %10llu\n", 1616 (u_longlong_t)tzb->zb_count); 1617 (void) printf("\tbp logical: %10llu\t avg: %6llu\n", 1618 (u_longlong_t)tzb->zb_lsize, 1619 (u_longlong_t)(tzb->zb_lsize / tzb->zb_count)); 1620 (void) printf("\tbp physical: %10llu\t avg:" 1621 " %6llu\tcompression: %6.2f\n", 1622 (u_longlong_t)tzb->zb_psize, 1623 (u_longlong_t)(tzb->zb_psize / tzb->zb_count), 1624 (double)tzb->zb_lsize / tzb->zb_psize); 1625 (void) printf("\tbp allocated: %10llu\t avg:" 1626 " %6llu\tcompression: %6.2f\n", 1627 (u_longlong_t)tzb->zb_asize, 1628 (u_longlong_t)(tzb->zb_asize / tzb->zb_count), 1629 (double)tzb->zb_lsize / tzb->zb_asize); 1630 (void) printf("\tSPA allocated: %10llu\tused: %5.2f%%\n", 1631 (u_longlong_t)alloc, 100.0 * alloc / space); 1632 1633 if (dump_opt['b'] >= 2) { 1634 int l, t, level; 1635 (void) printf("\nBlocks\tLSIZE\tPSIZE\tASIZE" 1636 "\t avg\t comp\t%%Total\tType\n"); 1637 1638 for (t = 0; t <= DMU_OT_NUMTYPES; t++) { 1639 char csize[6], lsize[6], psize[6], asize[6], avg[6]; 1640 char *typename; 1641 1642 typename = t == DMU_OT_DEFERRED ? "deferred free" : 1643 t == DMU_OT_TOTAL ? "Total" : dmu_ot[t].ot_name; 1644 1645 if (zcb.zcb_type[ZB_TOTAL][t].zb_asize == 0) { 1646 (void) printf("%6s\t%5s\t%5s\t%5s" 1647 "\t%5s\t%5s\t%6s\t%s\n", 1648 "-", 1649 "-", 1650 "-", 1651 "-", 1652 "-", 1653 "-", 1654 "-", 1655 typename); 1656 continue; 1657 } 1658 1659 for (l = ZB_TOTAL - 1; l >= -1; l--) { 1660 level = (l == -1 ? ZB_TOTAL : l); 1661 zb = &zcb.zcb_type[level][t]; 1662 1663 if (zb->zb_asize == 0) 1664 continue; 1665 1666 if (dump_opt['b'] < 3 && level != ZB_TOTAL) 1667 continue; 1668 1669 if (level == 0 && zb->zb_asize == 1670 zcb.zcb_type[ZB_TOTAL][t].zb_asize) 1671 continue; 1672 1673 nicenum(zb->zb_count, csize); 1674 nicenum(zb->zb_lsize, lsize); 1675 nicenum(zb->zb_psize, psize); 1676 nicenum(zb->zb_asize, asize); 1677 nicenum(zb->zb_asize / zb->zb_count, avg); 1678 1679 (void) printf("%6s\t%5s\t%5s\t%5s\t%5s" 1680 "\t%5.2f\t%6.2f\t", 1681 csize, lsize, psize, asize, avg, 1682 (double)zb->zb_lsize / zb->zb_psize, 1683 100.0 * zb->zb_asize / tzb->zb_asize); 1684 1685 if (level == ZB_TOTAL) 1686 (void) printf("%s\n", typename); 1687 else 1688 (void) printf(" L%d %s\n", 1689 level, typename); 1690 } 1691 } 1692 } 1693 1694 (void) printf("\n"); 1695 1696 if (leaks) 1697 return (2); 1698 1699 if (zcb.zcb_haderrors) 1700 return (3); 1701 1702 return (0); 1703 } 1704 1705 static void 1706 dump_zpool(spa_t *spa) 1707 { 1708 dsl_pool_t *dp = spa_get_dsl(spa); 1709 int rc = 0; 1710 1711 if (dump_opt['u']) 1712 dump_uberblock(&spa->spa_uberblock); 1713 1714 if (dump_opt['d'] || dump_opt['i']) { 1715 dump_dir(dp->dp_meta_objset); 1716 if (dump_opt['d'] >= 3) { 1717 dump_bplist(dp->dp_meta_objset, 1718 spa->spa_sync_bplist_obj, "Deferred frees"); 1719 dump_dtl(spa->spa_root_vdev, 0); 1720 dump_metaslabs(spa); 1721 } 1722 (void) dmu_objset_find(spa->spa_name, dump_one_dir, NULL, 1723 DS_FIND_SNAPSHOTS | DS_FIND_CHILDREN); 1724 } 1725 1726 if (dump_opt['b'] || dump_opt['c']) 1727 rc = dump_block_stats(spa); 1728 1729 if (dump_opt['s']) 1730 show_pool_stats(spa); 1731 1732 if (rc != 0) 1733 exit(rc); 1734 } 1735 1736 #define ZDB_FLAG_CHECKSUM 0x0001 1737 #define ZDB_FLAG_DECOMPRESS 0x0002 1738 #define ZDB_FLAG_BSWAP 0x0004 1739 #define ZDB_FLAG_GBH 0x0008 1740 #define ZDB_FLAG_INDIRECT 0x0010 1741 #define ZDB_FLAG_PHYS 0x0020 1742 #define ZDB_FLAG_RAW 0x0040 1743 #define ZDB_FLAG_PRINT_BLKPTR 0x0080 1744 1745 int flagbits[256]; 1746 1747 static void 1748 zdb_print_blkptr(blkptr_t *bp, int flags) 1749 { 1750 dva_t *dva = bp->blk_dva; 1751 int d; 1752 1753 if (flags & ZDB_FLAG_BSWAP) 1754 byteswap_uint64_array((void *)bp, sizeof (blkptr_t)); 1755 /* 1756 * Super-ick warning: This code is also duplicated in 1757 * cmd/mdb/common/modules/zfs/zfs.c . Yeah, I hate code 1758 * replication, too. 1759 */ 1760 for (d = 0; d < BP_GET_NDVAS(bp); d++) { 1761 (void) printf("\tDVA[%d]: vdev_id %lld / %llx\n", d, 1762 (longlong_t)DVA_GET_VDEV(&dva[d]), 1763 (longlong_t)DVA_GET_OFFSET(&dva[d])); 1764 (void) printf("\tDVA[%d]: GANG: %-5s GRID: %04llx\t" 1765 "ASIZE: %llx\n", d, 1766 DVA_GET_GANG(&dva[d]) ? "TRUE" : "FALSE", 1767 (longlong_t)DVA_GET_GRID(&dva[d]), 1768 (longlong_t)DVA_GET_ASIZE(&dva[d])); 1769 (void) printf("\tDVA[%d]: :%llu:%llx:%llx:%s%s%s%s\n", d, 1770 (u_longlong_t)DVA_GET_VDEV(&dva[d]), 1771 (longlong_t)DVA_GET_OFFSET(&dva[d]), 1772 (longlong_t)BP_GET_PSIZE(bp), 1773 BP_SHOULD_BYTESWAP(bp) ? "e" : "", 1774 !DVA_GET_GANG(&dva[d]) && BP_GET_LEVEL(bp) != 0 ? 1775 "d" : "", 1776 DVA_GET_GANG(&dva[d]) ? "g" : "", 1777 BP_GET_COMPRESS(bp) != 0 ? "d" : ""); 1778 } 1779 (void) printf("\tLSIZE: %-16llx\t\tPSIZE: %llx\n", 1780 (longlong_t)BP_GET_LSIZE(bp), (longlong_t)BP_GET_PSIZE(bp)); 1781 (void) printf("\tENDIAN: %6s\t\t\t\t\tTYPE: %s\n", 1782 BP_GET_BYTEORDER(bp) ? "LITTLE" : "BIG", 1783 dmu_ot[BP_GET_TYPE(bp)].ot_name); 1784 (void) printf("\tBIRTH: %-16llx LEVEL: %-2llu\tFILL: %llx\n", 1785 (u_longlong_t)bp->blk_birth, (u_longlong_t)BP_GET_LEVEL(bp), 1786 (u_longlong_t)bp->blk_fill); 1787 (void) printf("\tCKFUNC: %-16s\t\tCOMP: %s\n", 1788 zio_checksum_table[BP_GET_CHECKSUM(bp)].ci_name, 1789 zio_compress_table[BP_GET_COMPRESS(bp)].ci_name); 1790 (void) printf("\tCKSUM: %llx:%llx:%llx:%llx\n", 1791 (u_longlong_t)bp->blk_cksum.zc_word[0], 1792 (u_longlong_t)bp->blk_cksum.zc_word[1], 1793 (u_longlong_t)bp->blk_cksum.zc_word[2], 1794 (u_longlong_t)bp->blk_cksum.zc_word[3]); 1795 } 1796 1797 static void 1798 zdb_dump_indirect(blkptr_t *bp, int nbps, int flags) 1799 { 1800 int i; 1801 1802 for (i = 0; i < nbps; i++) 1803 zdb_print_blkptr(&bp[i], flags); 1804 } 1805 1806 static void 1807 zdb_dump_gbh(void *buf, int flags) 1808 { 1809 zdb_dump_indirect((blkptr_t *)buf, SPA_GBH_NBLKPTRS, flags); 1810 } 1811 1812 static void 1813 zdb_dump_block_raw(void *buf, uint64_t size, int flags) 1814 { 1815 if (flags & ZDB_FLAG_BSWAP) 1816 byteswap_uint64_array(buf, size); 1817 (void) write(2, buf, size); 1818 } 1819 1820 static void 1821 zdb_dump_block(char *label, void *buf, uint64_t size, int flags) 1822 { 1823 uint64_t *d = (uint64_t *)buf; 1824 int nwords = size / sizeof (uint64_t); 1825 int do_bswap = !!(flags & ZDB_FLAG_BSWAP); 1826 int i, j; 1827 char *hdr, *c; 1828 1829 1830 if (do_bswap) 1831 hdr = " 7 6 5 4 3 2 1 0 f e d c b a 9 8"; 1832 else 1833 hdr = " 0 1 2 3 4 5 6 7 8 9 a b c d e f"; 1834 1835 (void) printf("\n%s\n%6s %s 0123456789abcdef\n", label, "", hdr); 1836 1837 for (i = 0; i < nwords; i += 2) { 1838 (void) printf("%06llx: %016llx %016llx ", 1839 (u_longlong_t)(i * sizeof (uint64_t)), 1840 (u_longlong_t)(do_bswap ? BSWAP_64(d[i]) : d[i]), 1841 (u_longlong_t)(do_bswap ? BSWAP_64(d[i + 1]) : d[i + 1])); 1842 1843 c = (char *)&d[i]; 1844 for (j = 0; j < 2 * sizeof (uint64_t); j++) 1845 (void) printf("%c", isprint(c[j]) ? c[j] : '.'); 1846 (void) printf("\n"); 1847 } 1848 } 1849 1850 /* 1851 * There are two acceptable formats: 1852 * leaf_name - For example: c1t0d0 or /tmp/ztest.0a 1853 * child[.child]* - For example: 0.1.1 1854 * 1855 * The second form can be used to specify arbitrary vdevs anywhere 1856 * in the heirarchy. For example, in a pool with a mirror of 1857 * RAID-Zs, you can specify either RAID-Z vdev with 0.0 or 0.1 . 1858 */ 1859 static vdev_t * 1860 zdb_vdev_lookup(vdev_t *vdev, char *path) 1861 { 1862 char *s, *p, *q; 1863 int i; 1864 1865 if (vdev == NULL) 1866 return (NULL); 1867 1868 /* First, assume the x.x.x.x format */ 1869 i = (int)strtoul(path, &s, 10); 1870 if (s == path || (s && *s != '.' && *s != '\0')) 1871 goto name; 1872 if (i < 0 || i >= vdev->vdev_children) 1873 return (NULL); 1874 1875 vdev = vdev->vdev_child[i]; 1876 if (*s == '\0') 1877 return (vdev); 1878 return (zdb_vdev_lookup(vdev, s+1)); 1879 1880 name: 1881 for (i = 0; i < vdev->vdev_children; i++) { 1882 vdev_t *vc = vdev->vdev_child[i]; 1883 1884 if (vc->vdev_path == NULL) { 1885 vc = zdb_vdev_lookup(vc, path); 1886 if (vc == NULL) 1887 continue; 1888 else 1889 return (vc); 1890 } 1891 1892 p = strrchr(vc->vdev_path, '/'); 1893 p = p ? p + 1 : vc->vdev_path; 1894 q = &vc->vdev_path[strlen(vc->vdev_path) - 2]; 1895 1896 if (strcmp(vc->vdev_path, path) == 0) 1897 return (vc); 1898 if (strcmp(p, path) == 0) 1899 return (vc); 1900 if (strcmp(q, "s0") == 0 && strncmp(p, path, q - p) == 0) 1901 return (vc); 1902 } 1903 1904 return (NULL); 1905 } 1906 1907 /* 1908 * Read a block from a pool and print it out. The syntax of the 1909 * block descriptor is: 1910 * 1911 * pool:vdev_specifier:offset:size[:flags] 1912 * 1913 * pool - The name of the pool you wish to read from 1914 * vdev_specifier - Which vdev (see comment for zdb_vdev_lookup) 1915 * offset - offset, in hex, in bytes 1916 * size - Amount of data to read, in hex, in bytes 1917 * flags - A string of characters specifying options 1918 * b: Decode a blkptr at given offset within block 1919 * *c: Calculate and display checksums 1920 * *d: Decompress data before dumping 1921 * e: Byteswap data before dumping 1922 * *g: Display data as a gang block header 1923 * *i: Display as an indirect block 1924 * p: Do I/O to physical offset 1925 * r: Dump raw data to stdout 1926 * 1927 * * = not yet implemented 1928 */ 1929 static void 1930 zdb_read_block(char *thing, spa_t **spap) 1931 { 1932 spa_t *spa = *spap; 1933 int flags = 0; 1934 uint64_t offset = 0, size = 0, blkptr_offset = 0; 1935 zio_t *zio; 1936 vdev_t *vd; 1937 void *buf; 1938 char *s, *p, *dup, *spa_name, *vdev, *flagstr; 1939 int i, error, zio_flags; 1940 1941 dup = strdup(thing); 1942 s = strtok(dup, ":"); 1943 spa_name = s ? s : ""; 1944 s = strtok(NULL, ":"); 1945 vdev = s ? s : ""; 1946 s = strtok(NULL, ":"); 1947 offset = strtoull(s ? s : "", NULL, 16); 1948 s = strtok(NULL, ":"); 1949 size = strtoull(s ? s : "", NULL, 16); 1950 s = strtok(NULL, ":"); 1951 flagstr = s ? s : ""; 1952 1953 s = NULL; 1954 if (size == 0) 1955 s = "size must not be zero"; 1956 if (!IS_P2ALIGNED(size, DEV_BSIZE)) 1957 s = "size must be a multiple of sector size"; 1958 if (!IS_P2ALIGNED(offset, DEV_BSIZE)) 1959 s = "offset must be a multiple of sector size"; 1960 if (s) { 1961 (void) printf("Invalid block specifier: %s - %s\n", thing, s); 1962 free(dup); 1963 return; 1964 } 1965 1966 for (s = strtok(flagstr, ":"); s; s = strtok(NULL, ":")) { 1967 for (i = 0; flagstr[i]; i++) { 1968 int bit = flagbits[(uchar_t)flagstr[i]]; 1969 1970 if (bit == 0) { 1971 (void) printf("***Invalid flag: %c\n", 1972 flagstr[i]); 1973 continue; 1974 } 1975 flags |= bit; 1976 1977 /* If it's not something with an argument, keep going */ 1978 if ((bit & (ZDB_FLAG_CHECKSUM | ZDB_FLAG_DECOMPRESS | 1979 ZDB_FLAG_PRINT_BLKPTR)) == 0) 1980 continue; 1981 1982 p = &flagstr[i + 1]; 1983 if (bit == ZDB_FLAG_PRINT_BLKPTR) 1984 blkptr_offset = strtoull(p, &p, 16); 1985 if (*p != ':' && *p != '\0') { 1986 (void) printf("***Invalid flag arg: '%s'\n", s); 1987 free(dup); 1988 return; 1989 } 1990 } 1991 } 1992 1993 if (spa == NULL || spa->spa_name == NULL || 1994 strcmp(spa->spa_name, spa_name)) { 1995 if (spa && spa->spa_name) 1996 spa_close(spa, (void *)zdb_read_block); 1997 error = spa_open(spa_name, spap, (void *)zdb_read_block); 1998 if (error) 1999 fatal("Failed to open pool '%s': errno = %d\n", 2000 spa_name, error); 2001 spa = *spap; 2002 } 2003 2004 vd = zdb_vdev_lookup(spa->spa_root_vdev, vdev); 2005 if (vd == NULL) { 2006 (void) printf("***Invalid vdev: %s\n", vdev); 2007 free(dup); 2008 return; 2009 } else { 2010 if (vd->vdev_path) 2011 (void) printf("Found vdev: %s\n", vd->vdev_path); 2012 else 2013 (void) printf("Found vdev type: %s\n", 2014 vd->vdev_ops->vdev_op_type); 2015 } 2016 2017 buf = umem_alloc(size, UMEM_NOFAIL); 2018 2019 zio_flags = ZIO_FLAG_DONT_CACHE | ZIO_FLAG_DONT_QUEUE | 2020 ZIO_FLAG_DONT_PROPAGATE | ZIO_FLAG_DONT_RETRY | ZIO_FLAG_NOBOOKMARK; 2021 2022 if (flags & ZDB_FLAG_PHYS) 2023 zio_flags |= ZIO_FLAG_PHYSICAL; 2024 2025 zio = zio_root(spa, NULL, NULL, 0); 2026 /* XXX todo - cons up a BP so RAID-Z will be happy */ 2027 zio_nowait(zio_vdev_child_io(zio, NULL, vd, offset, buf, size, 2028 ZIO_TYPE_READ, ZIO_PRIORITY_SYNC_READ, zio_flags, NULL, NULL)); 2029 error = zio_wait(zio); 2030 2031 if (error) { 2032 (void) printf("Read of %s failed, error: %d\n", thing, error); 2033 goto out; 2034 } 2035 2036 if (flags & ZDB_FLAG_PRINT_BLKPTR) 2037 zdb_print_blkptr((blkptr_t *)(void *) 2038 ((uintptr_t)buf + (uintptr_t)blkptr_offset), flags); 2039 else if (flags & ZDB_FLAG_RAW) 2040 zdb_dump_block_raw(buf, size, flags); 2041 else if (flags & ZDB_FLAG_INDIRECT) 2042 zdb_dump_indirect((blkptr_t *)buf, size / sizeof (blkptr_t), 2043 flags); 2044 else if (flags & ZDB_FLAG_GBH) 2045 zdb_dump_gbh(buf, flags); 2046 else 2047 zdb_dump_block(thing, buf, size, flags); 2048 2049 out: 2050 umem_free(buf, size); 2051 free(dup); 2052 } 2053 2054 int 2055 main(int argc, char **argv) 2056 { 2057 int i, c; 2058 struct rlimit rl = { 1024, 1024 }; 2059 spa_t *spa; 2060 objset_t *os = NULL; 2061 char *endstr; 2062 int dump_all = 1; 2063 int verbose = 0; 2064 int error; 2065 int flag, set; 2066 vdev_knob_t *vk; 2067 2068 (void) setrlimit(RLIMIT_NOFILE, &rl); 2069 (void) enable_extended_FILE_stdio(-1, -1); 2070 2071 dprintf_setup(&argc, argv); 2072 2073 while ((c = getopt(argc, argv, "udibcsvCLO:B:UlR")) != -1) { 2074 switch (c) { 2075 case 'u': 2076 case 'd': 2077 case 'i': 2078 case 'b': 2079 case 'c': 2080 case 's': 2081 case 'C': 2082 case 'l': 2083 case 'R': 2084 dump_opt[c]++; 2085 dump_all = 0; 2086 break; 2087 case 'L': 2088 dump_opt[c]++; 2089 break; 2090 case 'O': 2091 endstr = optarg; 2092 if (endstr[0] == '!') { 2093 endstr++; 2094 set = 0; 2095 } else { 2096 set = 1; 2097 } 2098 if (strcmp(endstr, "post") == 0) { 2099 flag = ADVANCE_PRE; 2100 set = !set; 2101 } else if (strcmp(endstr, "pre") == 0) { 2102 flag = ADVANCE_PRE; 2103 } else if (strcmp(endstr, "prune") == 0) { 2104 flag = ADVANCE_PRUNE; 2105 } else if (strcmp(endstr, "data") == 0) { 2106 flag = ADVANCE_DATA; 2107 } else if (strcmp(endstr, "holes") == 0) { 2108 flag = ADVANCE_HOLES; 2109 } else { 2110 usage(); 2111 } 2112 if (set) 2113 zdb_advance |= flag; 2114 else 2115 zdb_advance &= ~flag; 2116 break; 2117 case 'B': 2118 endstr = optarg - 1; 2119 zdb_noread.zb_objset = strtoull(endstr + 1, &endstr, 0); 2120 zdb_noread.zb_object = strtoull(endstr + 1, &endstr, 0); 2121 zdb_noread.zb_level = strtol(endstr + 1, &endstr, 0); 2122 zdb_noread.zb_blkid = strtoull(endstr + 1, &endstr, 16); 2123 (void) printf("simulating bad block " 2124 "<%llu, %llu, %lld, %llx>\n", 2125 (u_longlong_t)zdb_noread.zb_objset, 2126 (u_longlong_t)zdb_noread.zb_object, 2127 (u_longlong_t)zdb_noread.zb_level, 2128 (u_longlong_t)zdb_noread.zb_blkid); 2129 break; 2130 case 'v': 2131 verbose++; 2132 break; 2133 case 'U': 2134 spa_config_dir = "/tmp"; 2135 break; 2136 default: 2137 usage(); 2138 break; 2139 } 2140 } 2141 2142 kernel_init(FREAD); 2143 2144 /* 2145 * Disable vdev caching. If we don't do this, live pool traversal 2146 * won't make progress because it will never see disk updates. 2147 */ 2148 for (vk = vdev_knob_next(NULL); vk != NULL; vk = vdev_knob_next(vk)) { 2149 if (strcmp(vk->vk_name, "cache_size") == 0) 2150 vk->vk_default = 0; 2151 } 2152 2153 for (c = 0; c < 256; c++) { 2154 if (dump_all && c != 'L' && c != 'l' && c != 'R') 2155 dump_opt[c] = 1; 2156 if (dump_opt[c]) 2157 dump_opt[c] += verbose; 2158 } 2159 2160 argc -= optind; 2161 argv += optind; 2162 2163 if (argc < 1) { 2164 if (dump_opt['C']) { 2165 dump_config(NULL); 2166 return (0); 2167 } 2168 usage(); 2169 } 2170 2171 if (dump_opt['l']) { 2172 dump_label(argv[0]); 2173 return (0); 2174 } 2175 2176 if (dump_opt['R']) { 2177 flagbits['b'] = ZDB_FLAG_PRINT_BLKPTR; 2178 flagbits['c'] = ZDB_FLAG_CHECKSUM; 2179 flagbits['d'] = ZDB_FLAG_DECOMPRESS; 2180 flagbits['e'] = ZDB_FLAG_BSWAP; 2181 flagbits['g'] = ZDB_FLAG_GBH; 2182 flagbits['i'] = ZDB_FLAG_INDIRECT; 2183 flagbits['p'] = ZDB_FLAG_PHYS; 2184 flagbits['r'] = ZDB_FLAG_RAW; 2185 2186 spa = NULL; 2187 while (argv[0]) { 2188 zdb_read_block(argv[0], &spa); 2189 argv++; 2190 argc--; 2191 } 2192 if (spa) 2193 spa_close(spa, (void *)zdb_read_block); 2194 return (0); 2195 } 2196 2197 if (dump_opt['C']) 2198 dump_config(argv[0]); 2199 2200 if (strchr(argv[0], '/') != NULL) { 2201 error = dmu_objset_open(argv[0], DMU_OST_ANY, 2202 DS_MODE_STANDARD | DS_MODE_READONLY, &os); 2203 } else { 2204 error = spa_open(argv[0], &spa, FTAG); 2205 } 2206 2207 if (error) 2208 fatal("can't open %s: error %d", argv[0], error); 2209 2210 argv++; 2211 if (--argc > 0) { 2212 zopt_objects = argc; 2213 zopt_object = calloc(zopt_objects, sizeof (uint64_t)); 2214 for (i = 0; i < zopt_objects; i++) { 2215 errno = 0; 2216 zopt_object[i] = strtoull(argv[i], NULL, 0); 2217 if (zopt_object[i] == 0 && errno != 0) 2218 fatal("bad object number %s: %s", 2219 argv[i], strerror(errno)); 2220 } 2221 } 2222 2223 if (os != NULL) { 2224 dump_dir(os); 2225 dmu_objset_close(os); 2226 } else { 2227 dump_zpool(spa); 2228 spa_close(spa, FTAG); 2229 } 2230 2231 kernel_fini(); 2232 2233 return (0); 2234 } 2235