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