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