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