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