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