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