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 /* 23 * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved. 24 * Copyright (c) 2012 by Delphix. All rights reserved. 25 */ 26 27 #include <stdio.h> 28 #include <stdio_ext.h> 29 #include <stdlib.h> 30 #include <ctype.h> 31 #include <sys/zfs_context.h> 32 #include <sys/spa.h> 33 #include <sys/spa_impl.h> 34 #include <sys/dmu.h> 35 #include <sys/zap.h> 36 #include <sys/fs/zfs.h> 37 #include <sys/zfs_znode.h> 38 #include <sys/zfs_sa.h> 39 #include <sys/sa.h> 40 #include <sys/sa_impl.h> 41 #include <sys/vdev.h> 42 #include <sys/vdev_impl.h> 43 #include <sys/metaslab_impl.h> 44 #include <sys/dmu_objset.h> 45 #include <sys/dsl_dir.h> 46 #include <sys/dsl_dataset.h> 47 #include <sys/dsl_pool.h> 48 #include <sys/dbuf.h> 49 #include <sys/zil.h> 50 #include <sys/zil_impl.h> 51 #include <sys/stat.h> 52 #include <sys/resource.h> 53 #include <sys/dmu_traverse.h> 54 #include <sys/zio_checksum.h> 55 #include <sys/zio_compress.h> 56 #include <sys/zfs_fuid.h> 57 #include <sys/arc.h> 58 #include <sys/ddt.h> 59 #include <sys/zfeature.h> 60 #include <zfs_comutil.h> 61 #undef ZFS_MAXNAMELEN 62 #undef verify 63 #include <libzfs.h> 64 65 #define ZDB_COMPRESS_NAME(idx) ((idx) < ZIO_COMPRESS_FUNCTIONS ? \ 66 zio_compress_table[(idx)].ci_name : "UNKNOWN") 67 #define ZDB_CHECKSUM_NAME(idx) ((idx) < ZIO_CHECKSUM_FUNCTIONS ? \ 68 zio_checksum_table[(idx)].ci_name : "UNKNOWN") 69 #define ZDB_OT_NAME(idx) ((idx) < DMU_OT_NUMTYPES ? \ 70 dmu_ot[(idx)].ot_name : DMU_OT_IS_VALID(idx) ? \ 71 dmu_ot_byteswap[DMU_OT_BYTESWAP(idx)].ob_name : "UNKNOWN") 72 #define ZDB_OT_TYPE(idx) ((idx) < DMU_OT_NUMTYPES ? (idx) : DMU_OT_NUMTYPES) 73 74 #ifndef lint 75 extern int zfs_recover; 76 #else 77 int zfs_recover; 78 #endif 79 80 const char cmdname[] = "zdb"; 81 uint8_t dump_opt[256]; 82 83 typedef void object_viewer_t(objset_t *, uint64_t, void *data, size_t size); 84 85 extern void dump_intent_log(zilog_t *); 86 uint64_t *zopt_object = NULL; 87 int zopt_objects = 0; 88 libzfs_handle_t *g_zfs; 89 uint64_t max_inflight = 200; 90 91 /* 92 * These libumem hooks provide a reasonable set of defaults for the allocator's 93 * debugging facilities. 94 */ 95 const char * 96 _umem_debug_init() 97 { 98 return ("default,verbose"); /* $UMEM_DEBUG setting */ 99 } 100 101 const char * 102 _umem_logging_init(void) 103 { 104 return ("fail,contents"); /* $UMEM_LOGGING setting */ 105 } 106 107 static void 108 usage(void) 109 { 110 (void) fprintf(stderr, 111 "Usage: %s [-CumdibcsDvhLXFPA] [-t txg] [-e [-p path...]] " 112 "[-U config] [-M inflight I/Os] poolname [object...]\n" 113 " %s [-divPA] [-e -p path...] [-U config] dataset " 114 "[object...]\n" 115 " %s -m [-LXFPA] [-t txg] [-e [-p path...]] [-U config] " 116 "poolname [vdev [metaslab...]]\n" 117 " %s -R [-A] [-e [-p path...]] poolname " 118 "vdev:offset:size[:flags]\n" 119 " %s -S [-PA] [-e [-p path...]] [-U config] poolname\n" 120 " %s -l [-uA] device\n" 121 " %s -C [-A] [-U config]\n\n", 122 cmdname, cmdname, cmdname, cmdname, cmdname, cmdname, cmdname); 123 124 (void) fprintf(stderr, " Dataset name must include at least one " 125 "separator character '/' or '@'\n"); 126 (void) fprintf(stderr, " If dataset name is specified, only that " 127 "dataset is dumped\n"); 128 (void) fprintf(stderr, " If object numbers are specified, only " 129 "those objects are dumped\n\n"); 130 (void) fprintf(stderr, " Options to control amount of output:\n"); 131 (void) fprintf(stderr, " -u uberblock\n"); 132 (void) fprintf(stderr, " -d dataset(s)\n"); 133 (void) fprintf(stderr, " -i intent logs\n"); 134 (void) fprintf(stderr, " -C config (or cachefile if alone)\n"); 135 (void) fprintf(stderr, " -h pool history\n"); 136 (void) fprintf(stderr, " -b block statistics\n"); 137 (void) fprintf(stderr, " -m metaslabs\n"); 138 (void) fprintf(stderr, " -c checksum all metadata (twice for " 139 "all data) blocks\n"); 140 (void) fprintf(stderr, " -s report stats on zdb's I/O\n"); 141 (void) fprintf(stderr, " -D dedup statistics\n"); 142 (void) fprintf(stderr, " -S simulate dedup to measure effect\n"); 143 (void) fprintf(stderr, " -v verbose (applies to all others)\n"); 144 (void) fprintf(stderr, " -l dump label contents\n"); 145 (void) fprintf(stderr, " -L disable leak tracking (do not " 146 "load spacemaps)\n"); 147 (void) fprintf(stderr, " -R read and display block from a " 148 "device\n\n"); 149 (void) fprintf(stderr, " Below options are intended for use " 150 "with other options (except -l):\n"); 151 (void) fprintf(stderr, " -A ignore assertions (-A), enable " 152 "panic recovery (-AA) or both (-AAA)\n"); 153 (void) fprintf(stderr, " -F attempt automatic rewind within " 154 "safe range of transaction groups\n"); 155 (void) fprintf(stderr, " -U <cachefile_path> -- use alternate " 156 "cachefile\n"); 157 (void) fprintf(stderr, " -X attempt extreme rewind (does not " 158 "work with dataset)\n"); 159 (void) fprintf(stderr, " -e pool is exported/destroyed/" 160 "has altroot/not in a cachefile\n"); 161 (void) fprintf(stderr, " -p <path> -- use one or more with " 162 "-e to specify path to vdev dir\n"); 163 (void) fprintf(stderr, " -P print numbers in parseable form\n"); 164 (void) fprintf(stderr, " -t <txg> -- highest txg to use when " 165 "searching for uberblocks\n"); 166 (void) fprintf(stderr, " -M <number of inflight I/Os> -- " 167 "specify the maximum number of checksumming I/Os [default is 200]"); 168 (void) fprintf(stderr, "Specify an option more than once (e.g. -bb) " 169 "to make only that option verbose\n"); 170 (void) fprintf(stderr, "Default is to dump everything non-verbosely\n"); 171 exit(1); 172 } 173 174 /* 175 * Called for usage errors that are discovered after a call to spa_open(), 176 * dmu_bonus_hold(), or pool_match(). abort() is called for other errors. 177 */ 178 179 static void 180 fatal(const char *fmt, ...) 181 { 182 va_list ap; 183 184 va_start(ap, fmt); 185 (void) fprintf(stderr, "%s: ", cmdname); 186 (void) vfprintf(stderr, fmt, ap); 187 va_end(ap); 188 (void) fprintf(stderr, "\n"); 189 190 exit(1); 191 } 192 193 /* ARGSUSED */ 194 static void 195 dump_packed_nvlist(objset_t *os, uint64_t object, void *data, size_t size) 196 { 197 nvlist_t *nv; 198 size_t nvsize = *(uint64_t *)data; 199 char *packed = umem_alloc(nvsize, UMEM_NOFAIL); 200 201 VERIFY(0 == dmu_read(os, object, 0, nvsize, packed, DMU_READ_PREFETCH)); 202 203 VERIFY(nvlist_unpack(packed, nvsize, &nv, 0) == 0); 204 205 umem_free(packed, nvsize); 206 207 dump_nvlist(nv, 8); 208 209 nvlist_free(nv); 210 } 211 212 /* ARGSUSED */ 213 static void 214 dump_history_offsets(objset_t *os, uint64_t object, void *data, size_t size) 215 { 216 spa_history_phys_t *shp = data; 217 218 if (shp == NULL) 219 return; 220 221 (void) printf("\t\tpool_create_len = %llu\n", 222 (u_longlong_t)shp->sh_pool_create_len); 223 (void) printf("\t\tphys_max_off = %llu\n", 224 (u_longlong_t)shp->sh_phys_max_off); 225 (void) printf("\t\tbof = %llu\n", 226 (u_longlong_t)shp->sh_bof); 227 (void) printf("\t\teof = %llu\n", 228 (u_longlong_t)shp->sh_eof); 229 (void) printf("\t\trecords_lost = %llu\n", 230 (u_longlong_t)shp->sh_records_lost); 231 } 232 233 static void 234 zdb_nicenum(uint64_t num, char *buf) 235 { 236 if (dump_opt['P']) 237 (void) sprintf(buf, "%llu", (longlong_t)num); 238 else 239 nicenum(num, buf); 240 } 241 242 const char dump_zap_stars[] = "****************************************"; 243 const int dump_zap_width = sizeof (dump_zap_stars) - 1; 244 245 static void 246 dump_zap_histogram(uint64_t histo[ZAP_HISTOGRAM_SIZE]) 247 { 248 int i; 249 int minidx = ZAP_HISTOGRAM_SIZE - 1; 250 int maxidx = 0; 251 uint64_t max = 0; 252 253 for (i = 0; i < ZAP_HISTOGRAM_SIZE; i++) { 254 if (histo[i] > max) 255 max = histo[i]; 256 if (histo[i] > 0 && i > maxidx) 257 maxidx = i; 258 if (histo[i] > 0 && i < minidx) 259 minidx = i; 260 } 261 262 if (max < dump_zap_width) 263 max = dump_zap_width; 264 265 for (i = minidx; i <= maxidx; i++) 266 (void) printf("\t\t\t%u: %6llu %s\n", i, (u_longlong_t)histo[i], 267 &dump_zap_stars[(max - histo[i]) * dump_zap_width / max]); 268 } 269 270 static void 271 dump_zap_stats(objset_t *os, uint64_t object) 272 { 273 int error; 274 zap_stats_t zs; 275 276 error = zap_get_stats(os, object, &zs); 277 if (error) 278 return; 279 280 if (zs.zs_ptrtbl_len == 0) { 281 ASSERT(zs.zs_num_blocks == 1); 282 (void) printf("\tmicrozap: %llu bytes, %llu entries\n", 283 (u_longlong_t)zs.zs_blocksize, 284 (u_longlong_t)zs.zs_num_entries); 285 return; 286 } 287 288 (void) printf("\tFat ZAP stats:\n"); 289 290 (void) printf("\t\tPointer table:\n"); 291 (void) printf("\t\t\t%llu elements\n", 292 (u_longlong_t)zs.zs_ptrtbl_len); 293 (void) printf("\t\t\tzt_blk: %llu\n", 294 (u_longlong_t)zs.zs_ptrtbl_zt_blk); 295 (void) printf("\t\t\tzt_numblks: %llu\n", 296 (u_longlong_t)zs.zs_ptrtbl_zt_numblks); 297 (void) printf("\t\t\tzt_shift: %llu\n", 298 (u_longlong_t)zs.zs_ptrtbl_zt_shift); 299 (void) printf("\t\t\tzt_blks_copied: %llu\n", 300 (u_longlong_t)zs.zs_ptrtbl_blks_copied); 301 (void) printf("\t\t\tzt_nextblk: %llu\n", 302 (u_longlong_t)zs.zs_ptrtbl_nextblk); 303 304 (void) printf("\t\tZAP entries: %llu\n", 305 (u_longlong_t)zs.zs_num_entries); 306 (void) printf("\t\tLeaf blocks: %llu\n", 307 (u_longlong_t)zs.zs_num_leafs); 308 (void) printf("\t\tTotal blocks: %llu\n", 309 (u_longlong_t)zs.zs_num_blocks); 310 (void) printf("\t\tzap_block_type: 0x%llx\n", 311 (u_longlong_t)zs.zs_block_type); 312 (void) printf("\t\tzap_magic: 0x%llx\n", 313 (u_longlong_t)zs.zs_magic); 314 (void) printf("\t\tzap_salt: 0x%llx\n", 315 (u_longlong_t)zs.zs_salt); 316 317 (void) printf("\t\tLeafs with 2^n pointers:\n"); 318 dump_zap_histogram(zs.zs_leafs_with_2n_pointers); 319 320 (void) printf("\t\tBlocks with n*5 entries:\n"); 321 dump_zap_histogram(zs.zs_blocks_with_n5_entries); 322 323 (void) printf("\t\tBlocks n/10 full:\n"); 324 dump_zap_histogram(zs.zs_blocks_n_tenths_full); 325 326 (void) printf("\t\tEntries with n chunks:\n"); 327 dump_zap_histogram(zs.zs_entries_using_n_chunks); 328 329 (void) printf("\t\tBuckets with n entries:\n"); 330 dump_zap_histogram(zs.zs_buckets_with_n_entries); 331 } 332 333 /*ARGSUSED*/ 334 static void 335 dump_none(objset_t *os, uint64_t object, void *data, size_t size) 336 { 337 } 338 339 /*ARGSUSED*/ 340 static void 341 dump_unknown(objset_t *os, uint64_t object, void *data, size_t size) 342 { 343 (void) printf("\tUNKNOWN OBJECT TYPE\n"); 344 } 345 346 /*ARGSUSED*/ 347 void 348 dump_uint8(objset_t *os, uint64_t object, void *data, size_t size) 349 { 350 } 351 352 /*ARGSUSED*/ 353 static void 354 dump_uint64(objset_t *os, uint64_t object, void *data, size_t size) 355 { 356 } 357 358 /*ARGSUSED*/ 359 static void 360 dump_zap(objset_t *os, uint64_t object, void *data, size_t size) 361 { 362 zap_cursor_t zc; 363 zap_attribute_t attr; 364 void *prop; 365 int i; 366 367 dump_zap_stats(os, object); 368 (void) printf("\n"); 369 370 for (zap_cursor_init(&zc, os, object); 371 zap_cursor_retrieve(&zc, &attr) == 0; 372 zap_cursor_advance(&zc)) { 373 (void) printf("\t\t%s = ", attr.za_name); 374 if (attr.za_num_integers == 0) { 375 (void) printf("\n"); 376 continue; 377 } 378 prop = umem_zalloc(attr.za_num_integers * 379 attr.za_integer_length, UMEM_NOFAIL); 380 (void) zap_lookup(os, object, attr.za_name, 381 attr.za_integer_length, attr.za_num_integers, prop); 382 if (attr.za_integer_length == 1) { 383 (void) printf("%s", (char *)prop); 384 } else { 385 for (i = 0; i < attr.za_num_integers; i++) { 386 switch (attr.za_integer_length) { 387 case 2: 388 (void) printf("%u ", 389 ((uint16_t *)prop)[i]); 390 break; 391 case 4: 392 (void) printf("%u ", 393 ((uint32_t *)prop)[i]); 394 break; 395 case 8: 396 (void) printf("%lld ", 397 (u_longlong_t)((int64_t *)prop)[i]); 398 break; 399 } 400 } 401 } 402 (void) printf("\n"); 403 umem_free(prop, attr.za_num_integers * attr.za_integer_length); 404 } 405 zap_cursor_fini(&zc); 406 } 407 408 /*ARGSUSED*/ 409 static void 410 dump_ddt_zap(objset_t *os, uint64_t object, void *data, size_t size) 411 { 412 dump_zap_stats(os, object); 413 /* contents are printed elsewhere, properly decoded */ 414 } 415 416 /*ARGSUSED*/ 417 static void 418 dump_sa_attrs(objset_t *os, uint64_t object, void *data, size_t size) 419 { 420 zap_cursor_t zc; 421 zap_attribute_t attr; 422 423 dump_zap_stats(os, object); 424 (void) printf("\n"); 425 426 for (zap_cursor_init(&zc, os, object); 427 zap_cursor_retrieve(&zc, &attr) == 0; 428 zap_cursor_advance(&zc)) { 429 (void) printf("\t\t%s = ", attr.za_name); 430 if (attr.za_num_integers == 0) { 431 (void) printf("\n"); 432 continue; 433 } 434 (void) printf(" %llx : [%d:%d:%d]\n", 435 (u_longlong_t)attr.za_first_integer, 436 (int)ATTR_LENGTH(attr.za_first_integer), 437 (int)ATTR_BSWAP(attr.za_first_integer), 438 (int)ATTR_NUM(attr.za_first_integer)); 439 } 440 zap_cursor_fini(&zc); 441 } 442 443 /*ARGSUSED*/ 444 static void 445 dump_sa_layouts(objset_t *os, uint64_t object, void *data, size_t size) 446 { 447 zap_cursor_t zc; 448 zap_attribute_t attr; 449 uint16_t *layout_attrs; 450 int i; 451 452 dump_zap_stats(os, object); 453 (void) printf("\n"); 454 455 for (zap_cursor_init(&zc, os, object); 456 zap_cursor_retrieve(&zc, &attr) == 0; 457 zap_cursor_advance(&zc)) { 458 (void) printf("\t\t%s = [", attr.za_name); 459 if (attr.za_num_integers == 0) { 460 (void) printf("\n"); 461 continue; 462 } 463 464 VERIFY(attr.za_integer_length == 2); 465 layout_attrs = umem_zalloc(attr.za_num_integers * 466 attr.za_integer_length, UMEM_NOFAIL); 467 468 VERIFY(zap_lookup(os, object, attr.za_name, 469 attr.za_integer_length, 470 attr.za_num_integers, layout_attrs) == 0); 471 472 for (i = 0; i != attr.za_num_integers; i++) 473 (void) printf(" %d ", (int)layout_attrs[i]); 474 (void) printf("]\n"); 475 umem_free(layout_attrs, 476 attr.za_num_integers * attr.za_integer_length); 477 } 478 zap_cursor_fini(&zc); 479 } 480 481 /*ARGSUSED*/ 482 static void 483 dump_zpldir(objset_t *os, uint64_t object, void *data, size_t size) 484 { 485 zap_cursor_t zc; 486 zap_attribute_t attr; 487 const char *typenames[] = { 488 /* 0 */ "not specified", 489 /* 1 */ "FIFO", 490 /* 2 */ "Character Device", 491 /* 3 */ "3 (invalid)", 492 /* 4 */ "Directory", 493 /* 5 */ "5 (invalid)", 494 /* 6 */ "Block Device", 495 /* 7 */ "7 (invalid)", 496 /* 8 */ "Regular File", 497 /* 9 */ "9 (invalid)", 498 /* 10 */ "Symbolic Link", 499 /* 11 */ "11 (invalid)", 500 /* 12 */ "Socket", 501 /* 13 */ "Door", 502 /* 14 */ "Event Port", 503 /* 15 */ "15 (invalid)", 504 }; 505 506 dump_zap_stats(os, object); 507 (void) printf("\n"); 508 509 for (zap_cursor_init(&zc, os, object); 510 zap_cursor_retrieve(&zc, &attr) == 0; 511 zap_cursor_advance(&zc)) { 512 (void) printf("\t\t%s = %lld (type: %s)\n", 513 attr.za_name, ZFS_DIRENT_OBJ(attr.za_first_integer), 514 typenames[ZFS_DIRENT_TYPE(attr.za_first_integer)]); 515 } 516 zap_cursor_fini(&zc); 517 } 518 519 static void 520 dump_spacemap(objset_t *os, space_map_obj_t *smo, space_map_t *sm) 521 { 522 uint64_t alloc, offset, entry; 523 uint8_t mapshift = sm->sm_shift; 524 uint64_t mapstart = sm->sm_start; 525 char *ddata[] = { "ALLOC", "FREE", "CONDENSE", "INVALID", 526 "INVALID", "INVALID", "INVALID", "INVALID" }; 527 528 if (smo->smo_object == 0) 529 return; 530 531 /* 532 * Print out the freelist entries in both encoded and decoded form. 533 */ 534 alloc = 0; 535 for (offset = 0; offset < smo->smo_objsize; offset += sizeof (entry)) { 536 VERIFY3U(0, ==, dmu_read(os, smo->smo_object, offset, 537 sizeof (entry), &entry, DMU_READ_PREFETCH)); 538 if (SM_DEBUG_DECODE(entry)) { 539 (void) printf("\t [%6llu] %s: txg %llu, pass %llu\n", 540 (u_longlong_t)(offset / sizeof (entry)), 541 ddata[SM_DEBUG_ACTION_DECODE(entry)], 542 (u_longlong_t)SM_DEBUG_TXG_DECODE(entry), 543 (u_longlong_t)SM_DEBUG_SYNCPASS_DECODE(entry)); 544 } else { 545 (void) printf("\t [%6llu] %c range:" 546 " %010llx-%010llx size: %06llx\n", 547 (u_longlong_t)(offset / sizeof (entry)), 548 SM_TYPE_DECODE(entry) == SM_ALLOC ? 'A' : 'F', 549 (u_longlong_t)((SM_OFFSET_DECODE(entry) << 550 mapshift) + mapstart), 551 (u_longlong_t)((SM_OFFSET_DECODE(entry) << 552 mapshift) + mapstart + (SM_RUN_DECODE(entry) << 553 mapshift)), 554 (u_longlong_t)(SM_RUN_DECODE(entry) << mapshift)); 555 if (SM_TYPE_DECODE(entry) == SM_ALLOC) 556 alloc += SM_RUN_DECODE(entry) << mapshift; 557 else 558 alloc -= SM_RUN_DECODE(entry) << mapshift; 559 } 560 } 561 if (alloc != smo->smo_alloc) { 562 (void) printf("space_map_object alloc (%llu) INCONSISTENT " 563 "with space map summary (%llu)\n", 564 (u_longlong_t)smo->smo_alloc, (u_longlong_t)alloc); 565 } 566 } 567 568 static void 569 dump_metaslab_stats(metaslab_t *msp) 570 { 571 char maxbuf[32]; 572 space_map_t *sm = &msp->ms_map; 573 avl_tree_t *t = sm->sm_pp_root; 574 int free_pct = sm->sm_space * 100 / sm->sm_size; 575 576 zdb_nicenum(space_map_maxsize(sm), maxbuf); 577 578 (void) printf("\t %25s %10lu %7s %6s %4s %4d%%\n", 579 "segments", avl_numnodes(t), "maxsize", maxbuf, 580 "freepct", free_pct); 581 } 582 583 static void 584 dump_metaslab(metaslab_t *msp) 585 { 586 vdev_t *vd = msp->ms_group->mg_vd; 587 spa_t *spa = vd->vdev_spa; 588 space_map_t *sm = &msp->ms_map; 589 space_map_obj_t *smo = &msp->ms_smo; 590 char freebuf[32]; 591 592 zdb_nicenum(sm->sm_size - smo->smo_alloc, freebuf); 593 594 (void) printf( 595 "\tmetaslab %6llu offset %12llx spacemap %6llu free %5s\n", 596 (u_longlong_t)(sm->sm_start / sm->sm_size), 597 (u_longlong_t)sm->sm_start, (u_longlong_t)smo->smo_object, freebuf); 598 599 if (dump_opt['m'] > 1 && !dump_opt['L']) { 600 mutex_enter(&msp->ms_lock); 601 space_map_load_wait(sm); 602 if (!sm->sm_loaded) 603 VERIFY(space_map_load(sm, zfs_metaslab_ops, 604 SM_FREE, smo, spa->spa_meta_objset) == 0); 605 dump_metaslab_stats(msp); 606 space_map_unload(sm); 607 mutex_exit(&msp->ms_lock); 608 } 609 610 if (dump_opt['d'] > 5 || dump_opt['m'] > 2) { 611 ASSERT(sm->sm_size == (1ULL << vd->vdev_ms_shift)); 612 613 mutex_enter(&msp->ms_lock); 614 dump_spacemap(spa->spa_meta_objset, smo, sm); 615 mutex_exit(&msp->ms_lock); 616 } 617 } 618 619 static void 620 print_vdev_metaslab_header(vdev_t *vd) 621 { 622 (void) printf("\tvdev %10llu\n\t%-10s%5llu %-19s %-15s %-10s\n", 623 (u_longlong_t)vd->vdev_id, 624 "metaslabs", (u_longlong_t)vd->vdev_ms_count, 625 "offset", "spacemap", "free"); 626 (void) printf("\t%15s %19s %15s %10s\n", 627 "---------------", "-------------------", 628 "---------------", "-------------"); 629 } 630 631 static void 632 dump_metaslabs(spa_t *spa) 633 { 634 vdev_t *vd, *rvd = spa->spa_root_vdev; 635 uint64_t m, c = 0, children = rvd->vdev_children; 636 637 (void) printf("\nMetaslabs:\n"); 638 639 if (!dump_opt['d'] && zopt_objects > 0) { 640 c = zopt_object[0]; 641 642 if (c >= children) 643 (void) fatal("bad vdev id: %llu", (u_longlong_t)c); 644 645 if (zopt_objects > 1) { 646 vd = rvd->vdev_child[c]; 647 print_vdev_metaslab_header(vd); 648 649 for (m = 1; m < zopt_objects; m++) { 650 if (zopt_object[m] < vd->vdev_ms_count) 651 dump_metaslab( 652 vd->vdev_ms[zopt_object[m]]); 653 else 654 (void) fprintf(stderr, "bad metaslab " 655 "number %llu\n", 656 (u_longlong_t)zopt_object[m]); 657 } 658 (void) printf("\n"); 659 return; 660 } 661 children = c + 1; 662 } 663 for (; c < children; c++) { 664 vd = rvd->vdev_child[c]; 665 print_vdev_metaslab_header(vd); 666 667 for (m = 0; m < vd->vdev_ms_count; m++) 668 dump_metaslab(vd->vdev_ms[m]); 669 (void) printf("\n"); 670 } 671 } 672 673 static void 674 dump_dde(const ddt_t *ddt, const ddt_entry_t *dde, uint64_t index) 675 { 676 const ddt_phys_t *ddp = dde->dde_phys; 677 const ddt_key_t *ddk = &dde->dde_key; 678 char *types[4] = { "ditto", "single", "double", "triple" }; 679 char blkbuf[BP_SPRINTF_LEN]; 680 blkptr_t blk; 681 682 for (int p = 0; p < DDT_PHYS_TYPES; p++, ddp++) { 683 if (ddp->ddp_phys_birth == 0) 684 continue; 685 ddt_bp_create(ddt->ddt_checksum, ddk, ddp, &blk); 686 sprintf_blkptr(blkbuf, &blk); 687 (void) printf("index %llx refcnt %llu %s %s\n", 688 (u_longlong_t)index, (u_longlong_t)ddp->ddp_refcnt, 689 types[p], blkbuf); 690 } 691 } 692 693 static void 694 dump_dedup_ratio(const ddt_stat_t *dds) 695 { 696 double rL, rP, rD, D, dedup, compress, copies; 697 698 if (dds->dds_blocks == 0) 699 return; 700 701 rL = (double)dds->dds_ref_lsize; 702 rP = (double)dds->dds_ref_psize; 703 rD = (double)dds->dds_ref_dsize; 704 D = (double)dds->dds_dsize; 705 706 dedup = rD / D; 707 compress = rL / rP; 708 copies = rD / rP; 709 710 (void) printf("dedup = %.2f, compress = %.2f, copies = %.2f, " 711 "dedup * compress / copies = %.2f\n\n", 712 dedup, compress, copies, dedup * compress / copies); 713 } 714 715 static void 716 dump_ddt(ddt_t *ddt, enum ddt_type type, enum ddt_class class) 717 { 718 char name[DDT_NAMELEN]; 719 ddt_entry_t dde; 720 uint64_t walk = 0; 721 dmu_object_info_t doi; 722 uint64_t count, dspace, mspace; 723 int error; 724 725 error = ddt_object_info(ddt, type, class, &doi); 726 727 if (error == ENOENT) 728 return; 729 ASSERT(error == 0); 730 731 if ((count = ddt_object_count(ddt, type, class)) == 0) 732 return; 733 734 dspace = doi.doi_physical_blocks_512 << 9; 735 mspace = doi.doi_fill_count * doi.doi_data_block_size; 736 737 ddt_object_name(ddt, type, class, name); 738 739 (void) printf("%s: %llu entries, size %llu on disk, %llu in core\n", 740 name, 741 (u_longlong_t)count, 742 (u_longlong_t)(dspace / count), 743 (u_longlong_t)(mspace / count)); 744 745 if (dump_opt['D'] < 3) 746 return; 747 748 zpool_dump_ddt(NULL, &ddt->ddt_histogram[type][class]); 749 750 if (dump_opt['D'] < 4) 751 return; 752 753 if (dump_opt['D'] < 5 && class == DDT_CLASS_UNIQUE) 754 return; 755 756 (void) printf("%s contents:\n\n", name); 757 758 while ((error = ddt_object_walk(ddt, type, class, &walk, &dde)) == 0) 759 dump_dde(ddt, &dde, walk); 760 761 ASSERT(error == ENOENT); 762 763 (void) printf("\n"); 764 } 765 766 static void 767 dump_all_ddts(spa_t *spa) 768 { 769 ddt_histogram_t ddh_total = { 0 }; 770 ddt_stat_t dds_total = { 0 }; 771 772 for (enum zio_checksum c = 0; c < ZIO_CHECKSUM_FUNCTIONS; c++) { 773 ddt_t *ddt = spa->spa_ddt[c]; 774 for (enum ddt_type type = 0; type < DDT_TYPES; type++) { 775 for (enum ddt_class class = 0; class < DDT_CLASSES; 776 class++) { 777 dump_ddt(ddt, type, class); 778 } 779 } 780 } 781 782 ddt_get_dedup_stats(spa, &dds_total); 783 784 if (dds_total.dds_blocks == 0) { 785 (void) printf("All DDTs are empty\n"); 786 return; 787 } 788 789 (void) printf("\n"); 790 791 if (dump_opt['D'] > 1) { 792 (void) printf("DDT histogram (aggregated over all DDTs):\n"); 793 ddt_get_dedup_histogram(spa, &ddh_total); 794 zpool_dump_ddt(&dds_total, &ddh_total); 795 } 796 797 dump_dedup_ratio(&dds_total); 798 } 799 800 static void 801 dump_dtl_seg(space_map_t *sm, uint64_t start, uint64_t size) 802 { 803 char *prefix = (void *)sm; 804 805 (void) printf("%s [%llu,%llu) length %llu\n", 806 prefix, 807 (u_longlong_t)start, 808 (u_longlong_t)(start + size), 809 (u_longlong_t)(size)); 810 } 811 812 static void 813 dump_dtl(vdev_t *vd, int indent) 814 { 815 spa_t *spa = vd->vdev_spa; 816 boolean_t required; 817 char *name[DTL_TYPES] = { "missing", "partial", "scrub", "outage" }; 818 char prefix[256]; 819 820 spa_vdev_state_enter(spa, SCL_NONE); 821 required = vdev_dtl_required(vd); 822 (void) spa_vdev_state_exit(spa, NULL, 0); 823 824 if (indent == 0) 825 (void) printf("\nDirty time logs:\n\n"); 826 827 (void) printf("\t%*s%s [%s]\n", indent, "", 828 vd->vdev_path ? vd->vdev_path : 829 vd->vdev_parent ? vd->vdev_ops->vdev_op_type : spa_name(spa), 830 required ? "DTL-required" : "DTL-expendable"); 831 832 for (int t = 0; t < DTL_TYPES; t++) { 833 space_map_t *sm = &vd->vdev_dtl[t]; 834 if (sm->sm_space == 0) 835 continue; 836 (void) snprintf(prefix, sizeof (prefix), "\t%*s%s", 837 indent + 2, "", name[t]); 838 mutex_enter(sm->sm_lock); 839 space_map_walk(sm, dump_dtl_seg, (void *)prefix); 840 mutex_exit(sm->sm_lock); 841 if (dump_opt['d'] > 5 && vd->vdev_children == 0) 842 dump_spacemap(spa->spa_meta_objset, 843 &vd->vdev_dtl_smo, sm); 844 } 845 846 for (int c = 0; c < vd->vdev_children; c++) 847 dump_dtl(vd->vdev_child[c], indent + 4); 848 } 849 850 static void 851 dump_history(spa_t *spa) 852 { 853 nvlist_t **events = NULL; 854 char buf[SPA_MAXBLOCKSIZE]; 855 uint64_t resid, len, off = 0; 856 uint_t num = 0; 857 int error; 858 time_t tsec; 859 struct tm t; 860 char tbuf[30]; 861 char internalstr[MAXPATHLEN]; 862 863 do { 864 len = sizeof (buf); 865 866 if ((error = spa_history_get(spa, &off, &len, buf)) != 0) { 867 (void) fprintf(stderr, "Unable to read history: " 868 "error %d\n", error); 869 return; 870 } 871 872 if (zpool_history_unpack(buf, len, &resid, &events, &num) != 0) 873 break; 874 875 off -= resid; 876 } while (len != 0); 877 878 (void) printf("\nHistory:\n"); 879 for (int i = 0; i < num; i++) { 880 uint64_t time, txg, ievent; 881 char *cmd, *intstr; 882 boolean_t printed = B_FALSE; 883 884 if (nvlist_lookup_uint64(events[i], ZPOOL_HIST_TIME, 885 &time) != 0) 886 goto next; 887 if (nvlist_lookup_string(events[i], ZPOOL_HIST_CMD, 888 &cmd) != 0) { 889 if (nvlist_lookup_uint64(events[i], 890 ZPOOL_HIST_INT_EVENT, &ievent) != 0) 891 goto next; 892 verify(nvlist_lookup_uint64(events[i], 893 ZPOOL_HIST_TXG, &txg) == 0); 894 verify(nvlist_lookup_string(events[i], 895 ZPOOL_HIST_INT_STR, &intstr) == 0); 896 if (ievent >= ZFS_NUM_LEGACY_HISTORY_EVENTS) 897 goto next; 898 899 (void) snprintf(internalstr, 900 sizeof (internalstr), 901 "[internal %s txg:%lld] %s", 902 zfs_history_event_names[ievent], txg, 903 intstr); 904 cmd = internalstr; 905 } 906 tsec = time; 907 (void) localtime_r(&tsec, &t); 908 (void) strftime(tbuf, sizeof (tbuf), "%F.%T", &t); 909 (void) printf("%s %s\n", tbuf, cmd); 910 printed = B_TRUE; 911 912 next: 913 if (dump_opt['h'] > 1) { 914 if (!printed) 915 (void) printf("unrecognized record:\n"); 916 dump_nvlist(events[i], 2); 917 } 918 } 919 } 920 921 /*ARGSUSED*/ 922 static void 923 dump_dnode(objset_t *os, uint64_t object, void *data, size_t size) 924 { 925 } 926 927 static uint64_t 928 blkid2offset(const dnode_phys_t *dnp, const blkptr_t *bp, const zbookmark_t *zb) 929 { 930 if (dnp == NULL) { 931 ASSERT(zb->zb_level < 0); 932 if (zb->zb_object == 0) 933 return (zb->zb_blkid); 934 return (zb->zb_blkid * BP_GET_LSIZE(bp)); 935 } 936 937 ASSERT(zb->zb_level >= 0); 938 939 return ((zb->zb_blkid << 940 (zb->zb_level * (dnp->dn_indblkshift - SPA_BLKPTRSHIFT))) * 941 dnp->dn_datablkszsec << SPA_MINBLOCKSHIFT); 942 } 943 944 static void 945 sprintf_blkptr_compact(char *blkbuf, const blkptr_t *bp) 946 { 947 const dva_t *dva = bp->blk_dva; 948 int ndvas = dump_opt['d'] > 5 ? BP_GET_NDVAS(bp) : 1; 949 950 if (dump_opt['b'] >= 5) { 951 sprintf_blkptr(blkbuf, bp); 952 return; 953 } 954 955 blkbuf[0] = '\0'; 956 957 for (int i = 0; i < ndvas; i++) 958 (void) sprintf(blkbuf + strlen(blkbuf), "%llu:%llx:%llx ", 959 (u_longlong_t)DVA_GET_VDEV(&dva[i]), 960 (u_longlong_t)DVA_GET_OFFSET(&dva[i]), 961 (u_longlong_t)DVA_GET_ASIZE(&dva[i])); 962 963 (void) sprintf(blkbuf + strlen(blkbuf), 964 "%llxL/%llxP F=%llu B=%llu/%llu", 965 (u_longlong_t)BP_GET_LSIZE(bp), 966 (u_longlong_t)BP_GET_PSIZE(bp), 967 (u_longlong_t)bp->blk_fill, 968 (u_longlong_t)bp->blk_birth, 969 (u_longlong_t)BP_PHYSICAL_BIRTH(bp)); 970 } 971 972 static void 973 print_indirect(blkptr_t *bp, const zbookmark_t *zb, 974 const dnode_phys_t *dnp) 975 { 976 char blkbuf[BP_SPRINTF_LEN]; 977 int l; 978 979 ASSERT3U(BP_GET_TYPE(bp), ==, dnp->dn_type); 980 ASSERT3U(BP_GET_LEVEL(bp), ==, zb->zb_level); 981 982 (void) printf("%16llx ", (u_longlong_t)blkid2offset(dnp, bp, zb)); 983 984 ASSERT(zb->zb_level >= 0); 985 986 for (l = dnp->dn_nlevels - 1; l >= -1; l--) { 987 if (l == zb->zb_level) { 988 (void) printf("L%llx", (u_longlong_t)zb->zb_level); 989 } else { 990 (void) printf(" "); 991 } 992 } 993 994 sprintf_blkptr_compact(blkbuf, bp); 995 (void) printf("%s\n", blkbuf); 996 } 997 998 static int 999 visit_indirect(spa_t *spa, const dnode_phys_t *dnp, 1000 blkptr_t *bp, const zbookmark_t *zb) 1001 { 1002 int err = 0; 1003 1004 if (bp->blk_birth == 0) 1005 return (0); 1006 1007 print_indirect(bp, zb, dnp); 1008 1009 if (BP_GET_LEVEL(bp) > 0) { 1010 uint32_t flags = ARC_WAIT; 1011 int i; 1012 blkptr_t *cbp; 1013 int epb = BP_GET_LSIZE(bp) >> SPA_BLKPTRSHIFT; 1014 arc_buf_t *buf; 1015 uint64_t fill = 0; 1016 1017 err = arc_read_nolock(NULL, spa, bp, arc_getbuf_func, &buf, 1018 ZIO_PRIORITY_ASYNC_READ, ZIO_FLAG_CANFAIL, &flags, zb); 1019 if (err) 1020 return (err); 1021 ASSERT(buf->b_data); 1022 1023 /* recursively visit blocks below this */ 1024 cbp = buf->b_data; 1025 for (i = 0; i < epb; i++, cbp++) { 1026 zbookmark_t czb; 1027 1028 SET_BOOKMARK(&czb, zb->zb_objset, zb->zb_object, 1029 zb->zb_level - 1, 1030 zb->zb_blkid * epb + i); 1031 err = visit_indirect(spa, dnp, cbp, &czb); 1032 if (err) 1033 break; 1034 fill += cbp->blk_fill; 1035 } 1036 if (!err) 1037 ASSERT3U(fill, ==, bp->blk_fill); 1038 (void) arc_buf_remove_ref(buf, &buf); 1039 } 1040 1041 return (err); 1042 } 1043 1044 /*ARGSUSED*/ 1045 static void 1046 dump_indirect(dnode_t *dn) 1047 { 1048 dnode_phys_t *dnp = dn->dn_phys; 1049 int j; 1050 zbookmark_t czb; 1051 1052 (void) printf("Indirect blocks:\n"); 1053 1054 SET_BOOKMARK(&czb, dmu_objset_id(dn->dn_objset), 1055 dn->dn_object, dnp->dn_nlevels - 1, 0); 1056 for (j = 0; j < dnp->dn_nblkptr; j++) { 1057 czb.zb_blkid = j; 1058 (void) visit_indirect(dmu_objset_spa(dn->dn_objset), dnp, 1059 &dnp->dn_blkptr[j], &czb); 1060 } 1061 1062 (void) printf("\n"); 1063 } 1064 1065 /*ARGSUSED*/ 1066 static void 1067 dump_dsl_dir(objset_t *os, uint64_t object, void *data, size_t size) 1068 { 1069 dsl_dir_phys_t *dd = data; 1070 time_t crtime; 1071 char nice[32]; 1072 1073 if (dd == NULL) 1074 return; 1075 1076 ASSERT3U(size, >=, sizeof (dsl_dir_phys_t)); 1077 1078 crtime = dd->dd_creation_time; 1079 (void) printf("\t\tcreation_time = %s", ctime(&crtime)); 1080 (void) printf("\t\thead_dataset_obj = %llu\n", 1081 (u_longlong_t)dd->dd_head_dataset_obj); 1082 (void) printf("\t\tparent_dir_obj = %llu\n", 1083 (u_longlong_t)dd->dd_parent_obj); 1084 (void) printf("\t\torigin_obj = %llu\n", 1085 (u_longlong_t)dd->dd_origin_obj); 1086 (void) printf("\t\tchild_dir_zapobj = %llu\n", 1087 (u_longlong_t)dd->dd_child_dir_zapobj); 1088 zdb_nicenum(dd->dd_used_bytes, nice); 1089 (void) printf("\t\tused_bytes = %s\n", nice); 1090 zdb_nicenum(dd->dd_compressed_bytes, nice); 1091 (void) printf("\t\tcompressed_bytes = %s\n", nice); 1092 zdb_nicenum(dd->dd_uncompressed_bytes, nice); 1093 (void) printf("\t\tuncompressed_bytes = %s\n", nice); 1094 zdb_nicenum(dd->dd_quota, nice); 1095 (void) printf("\t\tquota = %s\n", nice); 1096 zdb_nicenum(dd->dd_reserved, nice); 1097 (void) printf("\t\treserved = %s\n", nice); 1098 (void) printf("\t\tprops_zapobj = %llu\n", 1099 (u_longlong_t)dd->dd_props_zapobj); 1100 (void) printf("\t\tdeleg_zapobj = %llu\n", 1101 (u_longlong_t)dd->dd_deleg_zapobj); 1102 (void) printf("\t\tflags = %llx\n", 1103 (u_longlong_t)dd->dd_flags); 1104 1105 #define DO(which) \ 1106 zdb_nicenum(dd->dd_used_breakdown[DD_USED_ ## which], nice); \ 1107 (void) printf("\t\tused_breakdown[" #which "] = %s\n", nice) 1108 DO(HEAD); 1109 DO(SNAP); 1110 DO(CHILD); 1111 DO(CHILD_RSRV); 1112 DO(REFRSRV); 1113 #undef DO 1114 } 1115 1116 /*ARGSUSED*/ 1117 static void 1118 dump_dsl_dataset(objset_t *os, uint64_t object, void *data, size_t size) 1119 { 1120 dsl_dataset_phys_t *ds = data; 1121 time_t crtime; 1122 char used[32], compressed[32], uncompressed[32], unique[32]; 1123 char blkbuf[BP_SPRINTF_LEN]; 1124 1125 if (ds == NULL) 1126 return; 1127 1128 ASSERT(size == sizeof (*ds)); 1129 crtime = ds->ds_creation_time; 1130 zdb_nicenum(ds->ds_referenced_bytes, used); 1131 zdb_nicenum(ds->ds_compressed_bytes, compressed); 1132 zdb_nicenum(ds->ds_uncompressed_bytes, uncompressed); 1133 zdb_nicenum(ds->ds_unique_bytes, unique); 1134 sprintf_blkptr(blkbuf, &ds->ds_bp); 1135 1136 (void) printf("\t\tdir_obj = %llu\n", 1137 (u_longlong_t)ds->ds_dir_obj); 1138 (void) printf("\t\tprev_snap_obj = %llu\n", 1139 (u_longlong_t)ds->ds_prev_snap_obj); 1140 (void) printf("\t\tprev_snap_txg = %llu\n", 1141 (u_longlong_t)ds->ds_prev_snap_txg); 1142 (void) printf("\t\tnext_snap_obj = %llu\n", 1143 (u_longlong_t)ds->ds_next_snap_obj); 1144 (void) printf("\t\tsnapnames_zapobj = %llu\n", 1145 (u_longlong_t)ds->ds_snapnames_zapobj); 1146 (void) printf("\t\tnum_children = %llu\n", 1147 (u_longlong_t)ds->ds_num_children); 1148 (void) printf("\t\tuserrefs_obj = %llu\n", 1149 (u_longlong_t)ds->ds_userrefs_obj); 1150 (void) printf("\t\tcreation_time = %s", ctime(&crtime)); 1151 (void) printf("\t\tcreation_txg = %llu\n", 1152 (u_longlong_t)ds->ds_creation_txg); 1153 (void) printf("\t\tdeadlist_obj = %llu\n", 1154 (u_longlong_t)ds->ds_deadlist_obj); 1155 (void) printf("\t\tused_bytes = %s\n", used); 1156 (void) printf("\t\tcompressed_bytes = %s\n", compressed); 1157 (void) printf("\t\tuncompressed_bytes = %s\n", uncompressed); 1158 (void) printf("\t\tunique = %s\n", unique); 1159 (void) printf("\t\tfsid_guid = %llu\n", 1160 (u_longlong_t)ds->ds_fsid_guid); 1161 (void) printf("\t\tguid = %llu\n", 1162 (u_longlong_t)ds->ds_guid); 1163 (void) printf("\t\tflags = %llx\n", 1164 (u_longlong_t)ds->ds_flags); 1165 (void) printf("\t\tnext_clones_obj = %llu\n", 1166 (u_longlong_t)ds->ds_next_clones_obj); 1167 (void) printf("\t\tprops_obj = %llu\n", 1168 (u_longlong_t)ds->ds_props_obj); 1169 (void) printf("\t\tbp = %s\n", blkbuf); 1170 } 1171 1172 /* ARGSUSED */ 1173 static int 1174 dump_bptree_cb(void *arg, const blkptr_t *bp, dmu_tx_t *tx) 1175 { 1176 char blkbuf[BP_SPRINTF_LEN]; 1177 1178 if (bp->blk_birth != 0) { 1179 sprintf_blkptr(blkbuf, bp); 1180 (void) printf("\t%s\n", blkbuf); 1181 } 1182 return (0); 1183 } 1184 1185 static void 1186 dump_bptree(objset_t *os, uint64_t obj, char *name) 1187 { 1188 char bytes[32]; 1189 bptree_phys_t *bt; 1190 dmu_buf_t *db; 1191 1192 if (dump_opt['d'] < 3) 1193 return; 1194 1195 VERIFY3U(0, ==, dmu_bonus_hold(os, obj, FTAG, &db)); 1196 bt = db->db_data; 1197 zdb_nicenum(bt->bt_bytes, bytes); 1198 (void) printf("\n %s: %llu datasets, %s\n", 1199 name, (unsigned long long)(bt->bt_end - bt->bt_begin), bytes); 1200 dmu_buf_rele(db, FTAG); 1201 1202 if (dump_opt['d'] < 5) 1203 return; 1204 1205 (void) printf("\n"); 1206 1207 (void) bptree_iterate(os, obj, B_FALSE, dump_bptree_cb, NULL, NULL); 1208 } 1209 1210 /* ARGSUSED */ 1211 static int 1212 dump_bpobj_cb(void *arg, const blkptr_t *bp, dmu_tx_t *tx) 1213 { 1214 char blkbuf[BP_SPRINTF_LEN]; 1215 1216 ASSERT(bp->blk_birth != 0); 1217 sprintf_blkptr_compact(blkbuf, bp); 1218 (void) printf("\t%s\n", blkbuf); 1219 return (0); 1220 } 1221 1222 static void 1223 dump_bpobj(bpobj_t *bpo, char *name) 1224 { 1225 char bytes[32]; 1226 char comp[32]; 1227 char uncomp[32]; 1228 1229 if (dump_opt['d'] < 3) 1230 return; 1231 1232 zdb_nicenum(bpo->bpo_phys->bpo_bytes, bytes); 1233 if (bpo->bpo_havesubobj) { 1234 zdb_nicenum(bpo->bpo_phys->bpo_comp, comp); 1235 zdb_nicenum(bpo->bpo_phys->bpo_uncomp, uncomp); 1236 (void) printf("\n %s: %llu local blkptrs, %llu subobjs, " 1237 "%s (%s/%s comp)\n", 1238 name, (u_longlong_t)bpo->bpo_phys->bpo_num_blkptrs, 1239 (u_longlong_t)bpo->bpo_phys->bpo_num_subobjs, 1240 bytes, comp, uncomp); 1241 } else { 1242 (void) printf("\n %s: %llu blkptrs, %s\n", 1243 name, (u_longlong_t)bpo->bpo_phys->bpo_num_blkptrs, bytes); 1244 } 1245 1246 if (dump_opt['d'] < 5) 1247 return; 1248 1249 (void) printf("\n"); 1250 1251 (void) bpobj_iterate_nofree(bpo, dump_bpobj_cb, NULL, NULL); 1252 } 1253 1254 static void 1255 dump_deadlist(dsl_deadlist_t *dl) 1256 { 1257 dsl_deadlist_entry_t *dle; 1258 char bytes[32]; 1259 char comp[32]; 1260 char uncomp[32]; 1261 1262 if (dump_opt['d'] < 3) 1263 return; 1264 1265 zdb_nicenum(dl->dl_phys->dl_used, bytes); 1266 zdb_nicenum(dl->dl_phys->dl_comp, comp); 1267 zdb_nicenum(dl->dl_phys->dl_uncomp, uncomp); 1268 (void) printf("\n Deadlist: %s (%s/%s comp)\n", 1269 bytes, comp, uncomp); 1270 1271 if (dump_opt['d'] < 4) 1272 return; 1273 1274 (void) printf("\n"); 1275 1276 for (dle = avl_first(&dl->dl_tree); dle; 1277 dle = AVL_NEXT(&dl->dl_tree, dle)) { 1278 (void) printf(" mintxg %llu -> obj %llu\n", 1279 (longlong_t)dle->dle_mintxg, 1280 (longlong_t)dle->dle_bpobj.bpo_object); 1281 1282 if (dump_opt['d'] >= 5) 1283 dump_bpobj(&dle->dle_bpobj, ""); 1284 } 1285 } 1286 1287 static avl_tree_t idx_tree; 1288 static avl_tree_t domain_tree; 1289 static boolean_t fuid_table_loaded; 1290 static boolean_t sa_loaded; 1291 sa_attr_type_t *sa_attr_table; 1292 1293 static void 1294 fuid_table_destroy() 1295 { 1296 if (fuid_table_loaded) { 1297 zfs_fuid_table_destroy(&idx_tree, &domain_tree); 1298 fuid_table_loaded = B_FALSE; 1299 } 1300 } 1301 1302 /* 1303 * print uid or gid information. 1304 * For normal POSIX id just the id is printed in decimal format. 1305 * For CIFS files with FUID the fuid is printed in hex followed by 1306 * the doman-rid string. 1307 */ 1308 static void 1309 print_idstr(uint64_t id, const char *id_type) 1310 { 1311 if (FUID_INDEX(id)) { 1312 char *domain; 1313 1314 domain = zfs_fuid_idx_domain(&idx_tree, FUID_INDEX(id)); 1315 (void) printf("\t%s %llx [%s-%d]\n", id_type, 1316 (u_longlong_t)id, domain, (int)FUID_RID(id)); 1317 } else { 1318 (void) printf("\t%s %llu\n", id_type, (u_longlong_t)id); 1319 } 1320 1321 } 1322 1323 static void 1324 dump_uidgid(objset_t *os, uint64_t uid, uint64_t gid) 1325 { 1326 uint32_t uid_idx, gid_idx; 1327 1328 uid_idx = FUID_INDEX(uid); 1329 gid_idx = FUID_INDEX(gid); 1330 1331 /* Load domain table, if not already loaded */ 1332 if (!fuid_table_loaded && (uid_idx || gid_idx)) { 1333 uint64_t fuid_obj; 1334 1335 /* first find the fuid object. It lives in the master node */ 1336 VERIFY(zap_lookup(os, MASTER_NODE_OBJ, ZFS_FUID_TABLES, 1337 8, 1, &fuid_obj) == 0); 1338 zfs_fuid_avl_tree_create(&idx_tree, &domain_tree); 1339 (void) zfs_fuid_table_load(os, fuid_obj, 1340 &idx_tree, &domain_tree); 1341 fuid_table_loaded = B_TRUE; 1342 } 1343 1344 print_idstr(uid, "uid"); 1345 print_idstr(gid, "gid"); 1346 } 1347 1348 /*ARGSUSED*/ 1349 static void 1350 dump_znode(objset_t *os, uint64_t object, void *data, size_t size) 1351 { 1352 char path[MAXPATHLEN * 2]; /* allow for xattr and failure prefix */ 1353 sa_handle_t *hdl; 1354 uint64_t xattr, rdev, gen; 1355 uint64_t uid, gid, mode, fsize, parent, links; 1356 uint64_t pflags; 1357 uint64_t acctm[2], modtm[2], chgtm[2], crtm[2]; 1358 time_t z_crtime, z_atime, z_mtime, z_ctime; 1359 sa_bulk_attr_t bulk[12]; 1360 int idx = 0; 1361 int error; 1362 1363 if (!sa_loaded) { 1364 uint64_t sa_attrs = 0; 1365 uint64_t version; 1366 1367 VERIFY(zap_lookup(os, MASTER_NODE_OBJ, ZPL_VERSION_STR, 1368 8, 1, &version) == 0); 1369 if (version >= ZPL_VERSION_SA) { 1370 VERIFY(zap_lookup(os, MASTER_NODE_OBJ, ZFS_SA_ATTRS, 1371 8, 1, &sa_attrs) == 0); 1372 } 1373 if ((error = sa_setup(os, sa_attrs, zfs_attr_table, 1374 ZPL_END, &sa_attr_table)) != 0) { 1375 (void) printf("sa_setup failed errno %d, can't " 1376 "display znode contents\n", error); 1377 return; 1378 } 1379 sa_loaded = B_TRUE; 1380 } 1381 1382 if (sa_handle_get(os, object, NULL, SA_HDL_PRIVATE, &hdl)) { 1383 (void) printf("Failed to get handle for SA znode\n"); 1384 return; 1385 } 1386 1387 SA_ADD_BULK_ATTR(bulk, idx, sa_attr_table[ZPL_UID], NULL, &uid, 8); 1388 SA_ADD_BULK_ATTR(bulk, idx, sa_attr_table[ZPL_GID], NULL, &gid, 8); 1389 SA_ADD_BULK_ATTR(bulk, idx, sa_attr_table[ZPL_LINKS], NULL, 1390 &links, 8); 1391 SA_ADD_BULK_ATTR(bulk, idx, sa_attr_table[ZPL_GEN], NULL, &gen, 8); 1392 SA_ADD_BULK_ATTR(bulk, idx, sa_attr_table[ZPL_MODE], NULL, 1393 &mode, 8); 1394 SA_ADD_BULK_ATTR(bulk, idx, sa_attr_table[ZPL_PARENT], 1395 NULL, &parent, 8); 1396 SA_ADD_BULK_ATTR(bulk, idx, sa_attr_table[ZPL_SIZE], NULL, 1397 &fsize, 8); 1398 SA_ADD_BULK_ATTR(bulk, idx, sa_attr_table[ZPL_ATIME], NULL, 1399 acctm, 16); 1400 SA_ADD_BULK_ATTR(bulk, idx, sa_attr_table[ZPL_MTIME], NULL, 1401 modtm, 16); 1402 SA_ADD_BULK_ATTR(bulk, idx, sa_attr_table[ZPL_CRTIME], NULL, 1403 crtm, 16); 1404 SA_ADD_BULK_ATTR(bulk, idx, sa_attr_table[ZPL_CTIME], NULL, 1405 chgtm, 16); 1406 SA_ADD_BULK_ATTR(bulk, idx, sa_attr_table[ZPL_FLAGS], NULL, 1407 &pflags, 8); 1408 1409 if (sa_bulk_lookup(hdl, bulk, idx)) { 1410 (void) sa_handle_destroy(hdl); 1411 return; 1412 } 1413 1414 error = zfs_obj_to_path(os, object, path, sizeof (path)); 1415 if (error != 0) { 1416 (void) snprintf(path, sizeof (path), "\?\?\?<object#%llu>", 1417 (u_longlong_t)object); 1418 } 1419 if (dump_opt['d'] < 3) { 1420 (void) printf("\t%s\n", path); 1421 (void) sa_handle_destroy(hdl); 1422 return; 1423 } 1424 1425 z_crtime = (time_t)crtm[0]; 1426 z_atime = (time_t)acctm[0]; 1427 z_mtime = (time_t)modtm[0]; 1428 z_ctime = (time_t)chgtm[0]; 1429 1430 (void) printf("\tpath %s\n", path); 1431 dump_uidgid(os, uid, gid); 1432 (void) printf("\tatime %s", ctime(&z_atime)); 1433 (void) printf("\tmtime %s", ctime(&z_mtime)); 1434 (void) printf("\tctime %s", ctime(&z_ctime)); 1435 (void) printf("\tcrtime %s", ctime(&z_crtime)); 1436 (void) printf("\tgen %llu\n", (u_longlong_t)gen); 1437 (void) printf("\tmode %llo\n", (u_longlong_t)mode); 1438 (void) printf("\tsize %llu\n", (u_longlong_t)fsize); 1439 (void) printf("\tparent %llu\n", (u_longlong_t)parent); 1440 (void) printf("\tlinks %llu\n", (u_longlong_t)links); 1441 (void) printf("\tpflags %llx\n", (u_longlong_t)pflags); 1442 if (sa_lookup(hdl, sa_attr_table[ZPL_XATTR], &xattr, 1443 sizeof (uint64_t)) == 0) 1444 (void) printf("\txattr %llu\n", (u_longlong_t)xattr); 1445 if (sa_lookup(hdl, sa_attr_table[ZPL_RDEV], &rdev, 1446 sizeof (uint64_t)) == 0) 1447 (void) printf("\trdev 0x%016llx\n", (u_longlong_t)rdev); 1448 sa_handle_destroy(hdl); 1449 } 1450 1451 /*ARGSUSED*/ 1452 static void 1453 dump_acl(objset_t *os, uint64_t object, void *data, size_t size) 1454 { 1455 } 1456 1457 /*ARGSUSED*/ 1458 static void 1459 dump_dmu_objset(objset_t *os, uint64_t object, void *data, size_t size) 1460 { 1461 } 1462 1463 static object_viewer_t *object_viewer[DMU_OT_NUMTYPES + 1] = { 1464 dump_none, /* unallocated */ 1465 dump_zap, /* object directory */ 1466 dump_uint64, /* object array */ 1467 dump_none, /* packed nvlist */ 1468 dump_packed_nvlist, /* packed nvlist size */ 1469 dump_none, /* bplist */ 1470 dump_none, /* bplist header */ 1471 dump_none, /* SPA space map header */ 1472 dump_none, /* SPA space map */ 1473 dump_none, /* ZIL intent log */ 1474 dump_dnode, /* DMU dnode */ 1475 dump_dmu_objset, /* DMU objset */ 1476 dump_dsl_dir, /* DSL directory */ 1477 dump_zap, /* DSL directory child map */ 1478 dump_zap, /* DSL dataset snap map */ 1479 dump_zap, /* DSL props */ 1480 dump_dsl_dataset, /* DSL dataset */ 1481 dump_znode, /* ZFS znode */ 1482 dump_acl, /* ZFS V0 ACL */ 1483 dump_uint8, /* ZFS plain file */ 1484 dump_zpldir, /* ZFS directory */ 1485 dump_zap, /* ZFS master node */ 1486 dump_zap, /* ZFS delete queue */ 1487 dump_uint8, /* zvol object */ 1488 dump_zap, /* zvol prop */ 1489 dump_uint8, /* other uint8[] */ 1490 dump_uint64, /* other uint64[] */ 1491 dump_zap, /* other ZAP */ 1492 dump_zap, /* persistent error log */ 1493 dump_uint8, /* SPA history */ 1494 dump_history_offsets, /* SPA history offsets */ 1495 dump_zap, /* Pool properties */ 1496 dump_zap, /* DSL permissions */ 1497 dump_acl, /* ZFS ACL */ 1498 dump_uint8, /* ZFS SYSACL */ 1499 dump_none, /* FUID nvlist */ 1500 dump_packed_nvlist, /* FUID nvlist size */ 1501 dump_zap, /* DSL dataset next clones */ 1502 dump_zap, /* DSL scrub queue */ 1503 dump_zap, /* ZFS user/group used */ 1504 dump_zap, /* ZFS user/group quota */ 1505 dump_zap, /* snapshot refcount tags */ 1506 dump_ddt_zap, /* DDT ZAP object */ 1507 dump_zap, /* DDT statistics */ 1508 dump_znode, /* SA object */ 1509 dump_zap, /* SA Master Node */ 1510 dump_sa_attrs, /* SA attribute registration */ 1511 dump_sa_layouts, /* SA attribute layouts */ 1512 dump_zap, /* DSL scrub translations */ 1513 dump_none, /* fake dedup BP */ 1514 dump_zap, /* deadlist */ 1515 dump_none, /* deadlist hdr */ 1516 dump_zap, /* dsl clones */ 1517 dump_none, /* bpobj subobjs */ 1518 dump_unknown, /* Unknown type, must be last */ 1519 }; 1520 1521 static void 1522 dump_object(objset_t *os, uint64_t object, int verbosity, int *print_header) 1523 { 1524 dmu_buf_t *db = NULL; 1525 dmu_object_info_t doi; 1526 dnode_t *dn; 1527 void *bonus = NULL; 1528 size_t bsize = 0; 1529 char iblk[32], dblk[32], lsize[32], asize[32], fill[32]; 1530 char bonus_size[32]; 1531 char aux[50]; 1532 int error; 1533 1534 if (*print_header) { 1535 (void) printf("\n%10s %3s %5s %5s %5s %5s %6s %s\n", 1536 "Object", "lvl", "iblk", "dblk", "dsize", "lsize", 1537 "%full", "type"); 1538 *print_header = 0; 1539 } 1540 1541 if (object == 0) { 1542 dn = DMU_META_DNODE(os); 1543 } else { 1544 error = dmu_bonus_hold(os, object, FTAG, &db); 1545 if (error) 1546 fatal("dmu_bonus_hold(%llu) failed, errno %u", 1547 object, error); 1548 bonus = db->db_data; 1549 bsize = db->db_size; 1550 dn = DB_DNODE((dmu_buf_impl_t *)db); 1551 } 1552 dmu_object_info_from_dnode(dn, &doi); 1553 1554 zdb_nicenum(doi.doi_metadata_block_size, iblk); 1555 zdb_nicenum(doi.doi_data_block_size, dblk); 1556 zdb_nicenum(doi.doi_max_offset, lsize); 1557 zdb_nicenum(doi.doi_physical_blocks_512 << 9, asize); 1558 zdb_nicenum(doi.doi_bonus_size, bonus_size); 1559 (void) sprintf(fill, "%6.2f", 100.0 * doi.doi_fill_count * 1560 doi.doi_data_block_size / (object == 0 ? DNODES_PER_BLOCK : 1) / 1561 doi.doi_max_offset); 1562 1563 aux[0] = '\0'; 1564 1565 if (doi.doi_checksum != ZIO_CHECKSUM_INHERIT || verbosity >= 6) { 1566 (void) snprintf(aux + strlen(aux), sizeof (aux), " (K=%s)", 1567 ZDB_CHECKSUM_NAME(doi.doi_checksum)); 1568 } 1569 1570 if (doi.doi_compress != ZIO_COMPRESS_INHERIT || verbosity >= 6) { 1571 (void) snprintf(aux + strlen(aux), sizeof (aux), " (Z=%s)", 1572 ZDB_COMPRESS_NAME(doi.doi_compress)); 1573 } 1574 1575 (void) printf("%10lld %3u %5s %5s %5s %5s %6s %s%s\n", 1576 (u_longlong_t)object, doi.doi_indirection, iblk, dblk, 1577 asize, lsize, fill, ZDB_OT_NAME(doi.doi_type), aux); 1578 1579 if (doi.doi_bonus_type != DMU_OT_NONE && verbosity > 3) { 1580 (void) printf("%10s %3s %5s %5s %5s %5s %6s %s\n", 1581 "", "", "", "", "", bonus_size, "bonus", 1582 ZDB_OT_NAME(doi.doi_bonus_type)); 1583 } 1584 1585 if (verbosity >= 4) { 1586 (void) printf("\tdnode flags: %s%s%s\n", 1587 (dn->dn_phys->dn_flags & DNODE_FLAG_USED_BYTES) ? 1588 "USED_BYTES " : "", 1589 (dn->dn_phys->dn_flags & DNODE_FLAG_USERUSED_ACCOUNTED) ? 1590 "USERUSED_ACCOUNTED " : "", 1591 (dn->dn_phys->dn_flags & DNODE_FLAG_SPILL_BLKPTR) ? 1592 "SPILL_BLKPTR" : ""); 1593 (void) printf("\tdnode maxblkid: %llu\n", 1594 (longlong_t)dn->dn_phys->dn_maxblkid); 1595 1596 object_viewer[ZDB_OT_TYPE(doi.doi_bonus_type)](os, object, 1597 bonus, bsize); 1598 object_viewer[ZDB_OT_TYPE(doi.doi_type)](os, object, NULL, 0); 1599 *print_header = 1; 1600 } 1601 1602 if (verbosity >= 5) 1603 dump_indirect(dn); 1604 1605 if (verbosity >= 5) { 1606 /* 1607 * Report the list of segments that comprise the object. 1608 */ 1609 uint64_t start = 0; 1610 uint64_t end; 1611 uint64_t blkfill = 1; 1612 int minlvl = 1; 1613 1614 if (dn->dn_type == DMU_OT_DNODE) { 1615 minlvl = 0; 1616 blkfill = DNODES_PER_BLOCK; 1617 } 1618 1619 for (;;) { 1620 char segsize[32]; 1621 error = dnode_next_offset(dn, 1622 0, &start, minlvl, blkfill, 0); 1623 if (error) 1624 break; 1625 end = start; 1626 error = dnode_next_offset(dn, 1627 DNODE_FIND_HOLE, &end, minlvl, blkfill, 0); 1628 zdb_nicenum(end - start, segsize); 1629 (void) printf("\t\tsegment [%016llx, %016llx)" 1630 " size %5s\n", (u_longlong_t)start, 1631 (u_longlong_t)end, segsize); 1632 if (error) 1633 break; 1634 start = end; 1635 } 1636 } 1637 1638 if (db != NULL) 1639 dmu_buf_rele(db, FTAG); 1640 } 1641 1642 static char *objset_types[DMU_OST_NUMTYPES] = { 1643 "NONE", "META", "ZPL", "ZVOL", "OTHER", "ANY" }; 1644 1645 static void 1646 dump_dir(objset_t *os) 1647 { 1648 dmu_objset_stats_t dds; 1649 uint64_t object, object_count; 1650 uint64_t refdbytes, usedobjs, scratch; 1651 char numbuf[32]; 1652 char blkbuf[BP_SPRINTF_LEN + 20]; 1653 char osname[MAXNAMELEN]; 1654 char *type = "UNKNOWN"; 1655 int verbosity = dump_opt['d']; 1656 int print_header = 1; 1657 int i, error; 1658 1659 dmu_objset_fast_stat(os, &dds); 1660 1661 if (dds.dds_type < DMU_OST_NUMTYPES) 1662 type = objset_types[dds.dds_type]; 1663 1664 if (dds.dds_type == DMU_OST_META) { 1665 dds.dds_creation_txg = TXG_INITIAL; 1666 usedobjs = os->os_rootbp->blk_fill; 1667 refdbytes = os->os_spa->spa_dsl_pool-> 1668 dp_mos_dir->dd_phys->dd_used_bytes; 1669 } else { 1670 dmu_objset_space(os, &refdbytes, &scratch, &usedobjs, &scratch); 1671 } 1672 1673 ASSERT3U(usedobjs, ==, os->os_rootbp->blk_fill); 1674 1675 zdb_nicenum(refdbytes, numbuf); 1676 1677 if (verbosity >= 4) { 1678 (void) sprintf(blkbuf, ", rootbp "); 1679 (void) sprintf_blkptr(blkbuf + strlen(blkbuf), os->os_rootbp); 1680 } else { 1681 blkbuf[0] = '\0'; 1682 } 1683 1684 dmu_objset_name(os, osname); 1685 1686 (void) printf("Dataset %s [%s], ID %llu, cr_txg %llu, " 1687 "%s, %llu objects%s\n", 1688 osname, type, (u_longlong_t)dmu_objset_id(os), 1689 (u_longlong_t)dds.dds_creation_txg, 1690 numbuf, (u_longlong_t)usedobjs, blkbuf); 1691 1692 if (zopt_objects != 0) { 1693 for (i = 0; i < zopt_objects; i++) 1694 dump_object(os, zopt_object[i], verbosity, 1695 &print_header); 1696 (void) printf("\n"); 1697 return; 1698 } 1699 1700 if (dump_opt['i'] != 0 || verbosity >= 2) 1701 dump_intent_log(dmu_objset_zil(os)); 1702 1703 if (dmu_objset_ds(os) != NULL) 1704 dump_deadlist(&dmu_objset_ds(os)->ds_deadlist); 1705 1706 if (verbosity < 2) 1707 return; 1708 1709 if (os->os_rootbp->blk_birth == 0) 1710 return; 1711 1712 dump_object(os, 0, verbosity, &print_header); 1713 object_count = 0; 1714 if (DMU_USERUSED_DNODE(os) != NULL && 1715 DMU_USERUSED_DNODE(os)->dn_type != 0) { 1716 dump_object(os, DMU_USERUSED_OBJECT, verbosity, &print_header); 1717 dump_object(os, DMU_GROUPUSED_OBJECT, verbosity, &print_header); 1718 } 1719 1720 object = 0; 1721 while ((error = dmu_object_next(os, &object, B_FALSE, 0)) == 0) { 1722 dump_object(os, object, verbosity, &print_header); 1723 object_count++; 1724 } 1725 1726 ASSERT3U(object_count, ==, usedobjs); 1727 1728 (void) printf("\n"); 1729 1730 if (error != ESRCH) { 1731 (void) fprintf(stderr, "dmu_object_next() = %d\n", error); 1732 abort(); 1733 } 1734 } 1735 1736 static void 1737 dump_uberblock(uberblock_t *ub, const char *header, const char *footer) 1738 { 1739 time_t timestamp = ub->ub_timestamp; 1740 1741 (void) printf(header ? header : ""); 1742 (void) printf("\tmagic = %016llx\n", (u_longlong_t)ub->ub_magic); 1743 (void) printf("\tversion = %llu\n", (u_longlong_t)ub->ub_version); 1744 (void) printf("\ttxg = %llu\n", (u_longlong_t)ub->ub_txg); 1745 (void) printf("\tguid_sum = %llu\n", (u_longlong_t)ub->ub_guid_sum); 1746 (void) printf("\ttimestamp = %llu UTC = %s", 1747 (u_longlong_t)ub->ub_timestamp, asctime(localtime(×tamp))); 1748 if (dump_opt['u'] >= 3) { 1749 char blkbuf[BP_SPRINTF_LEN]; 1750 sprintf_blkptr(blkbuf, &ub->ub_rootbp); 1751 (void) printf("\trootbp = %s\n", blkbuf); 1752 } 1753 (void) printf(footer ? footer : ""); 1754 } 1755 1756 static void 1757 dump_config(spa_t *spa) 1758 { 1759 dmu_buf_t *db; 1760 size_t nvsize = 0; 1761 int error = 0; 1762 1763 1764 error = dmu_bonus_hold(spa->spa_meta_objset, 1765 spa->spa_config_object, FTAG, &db); 1766 1767 if (error == 0) { 1768 nvsize = *(uint64_t *)db->db_data; 1769 dmu_buf_rele(db, FTAG); 1770 1771 (void) printf("\nMOS Configuration:\n"); 1772 dump_packed_nvlist(spa->spa_meta_objset, 1773 spa->spa_config_object, (void *)&nvsize, 1); 1774 } else { 1775 (void) fprintf(stderr, "dmu_bonus_hold(%llu) failed, errno %d", 1776 (u_longlong_t)spa->spa_config_object, error); 1777 } 1778 } 1779 1780 static void 1781 dump_cachefile(const char *cachefile) 1782 { 1783 int fd; 1784 struct stat64 statbuf; 1785 char *buf; 1786 nvlist_t *config; 1787 1788 if ((fd = open64(cachefile, O_RDONLY)) < 0) { 1789 (void) printf("cannot open '%s': %s\n", cachefile, 1790 strerror(errno)); 1791 exit(1); 1792 } 1793 1794 if (fstat64(fd, &statbuf) != 0) { 1795 (void) printf("failed to stat '%s': %s\n", cachefile, 1796 strerror(errno)); 1797 exit(1); 1798 } 1799 1800 if ((buf = malloc(statbuf.st_size)) == NULL) { 1801 (void) fprintf(stderr, "failed to allocate %llu bytes\n", 1802 (u_longlong_t)statbuf.st_size); 1803 exit(1); 1804 } 1805 1806 if (read(fd, buf, statbuf.st_size) != statbuf.st_size) { 1807 (void) fprintf(stderr, "failed to read %llu bytes\n", 1808 (u_longlong_t)statbuf.st_size); 1809 exit(1); 1810 } 1811 1812 (void) close(fd); 1813 1814 if (nvlist_unpack(buf, statbuf.st_size, &config, 0) != 0) { 1815 (void) fprintf(stderr, "failed to unpack nvlist\n"); 1816 exit(1); 1817 } 1818 1819 free(buf); 1820 1821 dump_nvlist(config, 0); 1822 1823 nvlist_free(config); 1824 } 1825 1826 #define ZDB_MAX_UB_HEADER_SIZE 32 1827 1828 static void 1829 dump_label_uberblocks(vdev_label_t *lbl, uint64_t ashift) 1830 { 1831 vdev_t vd; 1832 vdev_t *vdp = &vd; 1833 char header[ZDB_MAX_UB_HEADER_SIZE]; 1834 1835 vd.vdev_ashift = ashift; 1836 vdp->vdev_top = vdp; 1837 1838 for (int i = 0; i < VDEV_UBERBLOCK_COUNT(vdp); i++) { 1839 uint64_t uoff = VDEV_UBERBLOCK_OFFSET(vdp, i); 1840 uberblock_t *ub = (void *)((char *)lbl + uoff); 1841 1842 if (uberblock_verify(ub)) 1843 continue; 1844 (void) snprintf(header, ZDB_MAX_UB_HEADER_SIZE, 1845 "Uberblock[%d]\n", i); 1846 dump_uberblock(ub, header, ""); 1847 } 1848 } 1849 1850 static void 1851 dump_label(const char *dev) 1852 { 1853 int fd; 1854 vdev_label_t label; 1855 char *path, *buf = label.vl_vdev_phys.vp_nvlist; 1856 size_t buflen = sizeof (label.vl_vdev_phys.vp_nvlist); 1857 struct stat64 statbuf; 1858 uint64_t psize, ashift; 1859 int len = strlen(dev) + 1; 1860 1861 if (strncmp(dev, "/dev/dsk/", 9) == 0) { 1862 len++; 1863 path = malloc(len); 1864 (void) snprintf(path, len, "%s%s", "/dev/rdsk/", dev + 9); 1865 } else { 1866 path = strdup(dev); 1867 } 1868 1869 if ((fd = open64(path, O_RDONLY)) < 0) { 1870 (void) printf("cannot open '%s': %s\n", path, strerror(errno)); 1871 free(path); 1872 exit(1); 1873 } 1874 1875 if (fstat64(fd, &statbuf) != 0) { 1876 (void) printf("failed to stat '%s': %s\n", path, 1877 strerror(errno)); 1878 free(path); 1879 (void) close(fd); 1880 exit(1); 1881 } 1882 1883 if (S_ISBLK(statbuf.st_mode)) { 1884 (void) printf("cannot use '%s': character device required\n", 1885 path); 1886 free(path); 1887 (void) close(fd); 1888 exit(1); 1889 } 1890 1891 psize = statbuf.st_size; 1892 psize = P2ALIGN(psize, (uint64_t)sizeof (vdev_label_t)); 1893 1894 for (int l = 0; l < VDEV_LABELS; l++) { 1895 nvlist_t *config = NULL; 1896 1897 (void) printf("--------------------------------------------\n"); 1898 (void) printf("LABEL %d\n", l); 1899 (void) printf("--------------------------------------------\n"); 1900 1901 if (pread64(fd, &label, sizeof (label), 1902 vdev_label_offset(psize, l, 0)) != sizeof (label)) { 1903 (void) printf("failed to read label %d\n", l); 1904 continue; 1905 } 1906 1907 if (nvlist_unpack(buf, buflen, &config, 0) != 0) { 1908 (void) printf("failed to unpack label %d\n", l); 1909 ashift = SPA_MINBLOCKSHIFT; 1910 } else { 1911 nvlist_t *vdev_tree = NULL; 1912 1913 dump_nvlist(config, 4); 1914 if ((nvlist_lookup_nvlist(config, 1915 ZPOOL_CONFIG_VDEV_TREE, &vdev_tree) != 0) || 1916 (nvlist_lookup_uint64(vdev_tree, 1917 ZPOOL_CONFIG_ASHIFT, &ashift) != 0)) 1918 ashift = SPA_MINBLOCKSHIFT; 1919 nvlist_free(config); 1920 } 1921 if (dump_opt['u']) 1922 dump_label_uberblocks(&label, ashift); 1923 } 1924 1925 free(path); 1926 (void) close(fd); 1927 } 1928 1929 /*ARGSUSED*/ 1930 static int 1931 dump_one_dir(const char *dsname, void *arg) 1932 { 1933 int error; 1934 objset_t *os; 1935 1936 error = dmu_objset_own(dsname, DMU_OST_ANY, B_TRUE, FTAG, &os); 1937 if (error) { 1938 (void) printf("Could not open %s, error %d\n", dsname, error); 1939 return (0); 1940 } 1941 dump_dir(os); 1942 dmu_objset_disown(os, FTAG); 1943 fuid_table_destroy(); 1944 sa_loaded = B_FALSE; 1945 return (0); 1946 } 1947 1948 /* 1949 * Block statistics. 1950 */ 1951 typedef struct zdb_blkstats { 1952 uint64_t zb_asize; 1953 uint64_t zb_lsize; 1954 uint64_t zb_psize; 1955 uint64_t zb_count; 1956 } zdb_blkstats_t; 1957 1958 /* 1959 * Extended object types to report deferred frees and dedup auto-ditto blocks. 1960 */ 1961 #define ZDB_OT_DEFERRED (DMU_OT_NUMTYPES + 0) 1962 #define ZDB_OT_DITTO (DMU_OT_NUMTYPES + 1) 1963 #define ZDB_OT_OTHER (DMU_OT_NUMTYPES + 2) 1964 #define ZDB_OT_TOTAL (DMU_OT_NUMTYPES + 3) 1965 1966 static char *zdb_ot_extname[] = { 1967 "deferred free", 1968 "dedup ditto", 1969 "other", 1970 "Total", 1971 }; 1972 1973 #define ZB_TOTAL DN_MAX_LEVELS 1974 1975 typedef struct zdb_cb { 1976 zdb_blkstats_t zcb_type[ZB_TOTAL + 1][ZDB_OT_TOTAL + 1]; 1977 uint64_t zcb_dedup_asize; 1978 uint64_t zcb_dedup_blocks; 1979 uint64_t zcb_errors[256]; 1980 int zcb_readfails; 1981 int zcb_haderrors; 1982 spa_t *zcb_spa; 1983 } zdb_cb_t; 1984 1985 static void 1986 zdb_count_block(zdb_cb_t *zcb, zilog_t *zilog, const blkptr_t *bp, 1987 dmu_object_type_t type) 1988 { 1989 uint64_t refcnt = 0; 1990 1991 ASSERT(type < ZDB_OT_TOTAL); 1992 1993 if (zilog && zil_bp_tree_add(zilog, bp) != 0) 1994 return; 1995 1996 for (int i = 0; i < 4; i++) { 1997 int l = (i < 2) ? BP_GET_LEVEL(bp) : ZB_TOTAL; 1998 int t = (i & 1) ? type : ZDB_OT_TOTAL; 1999 zdb_blkstats_t *zb = &zcb->zcb_type[l][t]; 2000 2001 zb->zb_asize += BP_GET_ASIZE(bp); 2002 zb->zb_lsize += BP_GET_LSIZE(bp); 2003 zb->zb_psize += BP_GET_PSIZE(bp); 2004 zb->zb_count++; 2005 } 2006 2007 if (dump_opt['L']) 2008 return; 2009 2010 if (BP_GET_DEDUP(bp)) { 2011 ddt_t *ddt; 2012 ddt_entry_t *dde; 2013 2014 ddt = ddt_select(zcb->zcb_spa, bp); 2015 ddt_enter(ddt); 2016 dde = ddt_lookup(ddt, bp, B_FALSE); 2017 2018 if (dde == NULL) { 2019 refcnt = 0; 2020 } else { 2021 ddt_phys_t *ddp = ddt_phys_select(dde, bp); 2022 ddt_phys_decref(ddp); 2023 refcnt = ddp->ddp_refcnt; 2024 if (ddt_phys_total_refcnt(dde) == 0) 2025 ddt_remove(ddt, dde); 2026 } 2027 ddt_exit(ddt); 2028 } 2029 2030 VERIFY3U(zio_wait(zio_claim(NULL, zcb->zcb_spa, 2031 refcnt ? 0 : spa_first_txg(zcb->zcb_spa), 2032 bp, NULL, NULL, ZIO_FLAG_CANFAIL)), ==, 0); 2033 } 2034 2035 static void 2036 zdb_blkptr_done(zio_t *zio) 2037 { 2038 spa_t *spa = zio->io_spa; 2039 blkptr_t *bp = zio->io_bp; 2040 int ioerr = zio->io_error; 2041 zdb_cb_t *zcb = zio->io_private; 2042 zbookmark_t *zb = &zio->io_bookmark; 2043 2044 zio_data_buf_free(zio->io_data, zio->io_size); 2045 2046 mutex_enter(&spa->spa_scrub_lock); 2047 spa->spa_scrub_inflight--; 2048 cv_broadcast(&spa->spa_scrub_io_cv); 2049 2050 if (ioerr && !(zio->io_flags & ZIO_FLAG_SPECULATIVE)) { 2051 char blkbuf[BP_SPRINTF_LEN]; 2052 2053 zcb->zcb_haderrors = 1; 2054 zcb->zcb_errors[ioerr]++; 2055 2056 if (dump_opt['b'] >= 2) 2057 sprintf_blkptr(blkbuf, bp); 2058 else 2059 blkbuf[0] = '\0'; 2060 2061 (void) printf("zdb_blkptr_cb: " 2062 "Got error %d reading " 2063 "<%llu, %llu, %lld, %llx> %s -- skipping\n", 2064 ioerr, 2065 (u_longlong_t)zb->zb_objset, 2066 (u_longlong_t)zb->zb_object, 2067 (u_longlong_t)zb->zb_level, 2068 (u_longlong_t)zb->zb_blkid, 2069 blkbuf); 2070 } 2071 mutex_exit(&spa->spa_scrub_lock); 2072 } 2073 2074 /* ARGSUSED */ 2075 static int 2076 zdb_blkptr_cb(spa_t *spa, zilog_t *zilog, const blkptr_t *bp, arc_buf_t *pbuf, 2077 const zbookmark_t *zb, const dnode_phys_t *dnp, void *arg) 2078 { 2079 zdb_cb_t *zcb = arg; 2080 char blkbuf[BP_SPRINTF_LEN]; 2081 dmu_object_type_t type; 2082 boolean_t is_metadata; 2083 2084 if (bp == NULL) 2085 return (0); 2086 2087 type = BP_GET_TYPE(bp); 2088 2089 zdb_count_block(zcb, zilog, bp, 2090 (type & DMU_OT_NEWTYPE) ? ZDB_OT_OTHER : type); 2091 2092 is_metadata = (BP_GET_LEVEL(bp) != 0 || DMU_OT_IS_METADATA(type)); 2093 2094 if (dump_opt['c'] > 1 || (dump_opt['c'] && is_metadata)) { 2095 size_t size = BP_GET_PSIZE(bp); 2096 void *data = zio_data_buf_alloc(size); 2097 int flags = ZIO_FLAG_CANFAIL | ZIO_FLAG_SCRUB | ZIO_FLAG_RAW; 2098 2099 /* If it's an intent log block, failure is expected. */ 2100 if (zb->zb_level == ZB_ZIL_LEVEL) 2101 flags |= ZIO_FLAG_SPECULATIVE; 2102 2103 mutex_enter(&spa->spa_scrub_lock); 2104 while (spa->spa_scrub_inflight > max_inflight) 2105 cv_wait(&spa->spa_scrub_io_cv, &spa->spa_scrub_lock); 2106 spa->spa_scrub_inflight++; 2107 mutex_exit(&spa->spa_scrub_lock); 2108 2109 zio_nowait(zio_read(NULL, spa, bp, data, size, 2110 zdb_blkptr_done, zcb, ZIO_PRIORITY_ASYNC_READ, flags, zb)); 2111 2112 } 2113 2114 zcb->zcb_readfails = 0; 2115 2116 if (dump_opt['b'] >= 4) { 2117 sprintf_blkptr(blkbuf, bp); 2118 (void) printf("objset %llu object %llu " 2119 "level %lld offset 0x%llx %s\n", 2120 (u_longlong_t)zb->zb_objset, 2121 (u_longlong_t)zb->zb_object, 2122 (longlong_t)zb->zb_level, 2123 (u_longlong_t)blkid2offset(dnp, bp, zb), 2124 blkbuf); 2125 } 2126 2127 return (0); 2128 } 2129 2130 static void 2131 zdb_leak(space_map_t *sm, uint64_t start, uint64_t size) 2132 { 2133 vdev_t *vd = sm->sm_ppd; 2134 2135 (void) printf("leaked space: vdev %llu, offset 0x%llx, size %llu\n", 2136 (u_longlong_t)vd->vdev_id, (u_longlong_t)start, (u_longlong_t)size); 2137 } 2138 2139 /* ARGSUSED */ 2140 static void 2141 zdb_space_map_load(space_map_t *sm) 2142 { 2143 } 2144 2145 static void 2146 zdb_space_map_unload(space_map_t *sm) 2147 { 2148 space_map_vacate(sm, zdb_leak, sm); 2149 } 2150 2151 /* ARGSUSED */ 2152 static void 2153 zdb_space_map_claim(space_map_t *sm, uint64_t start, uint64_t size) 2154 { 2155 } 2156 2157 static space_map_ops_t zdb_space_map_ops = { 2158 zdb_space_map_load, 2159 zdb_space_map_unload, 2160 NULL, /* alloc */ 2161 zdb_space_map_claim, 2162 NULL, /* free */ 2163 NULL /* maxsize */ 2164 }; 2165 2166 static void 2167 zdb_ddt_leak_init(spa_t *spa, zdb_cb_t *zcb) 2168 { 2169 ddt_bookmark_t ddb = { 0 }; 2170 ddt_entry_t dde; 2171 int error; 2172 2173 while ((error = ddt_walk(spa, &ddb, &dde)) == 0) { 2174 blkptr_t blk; 2175 ddt_phys_t *ddp = dde.dde_phys; 2176 2177 if (ddb.ddb_class == DDT_CLASS_UNIQUE) 2178 return; 2179 2180 ASSERT(ddt_phys_total_refcnt(&dde) > 1); 2181 2182 for (int p = 0; p < DDT_PHYS_TYPES; p++, ddp++) { 2183 if (ddp->ddp_phys_birth == 0) 2184 continue; 2185 ddt_bp_create(ddb.ddb_checksum, 2186 &dde.dde_key, ddp, &blk); 2187 if (p == DDT_PHYS_DITTO) { 2188 zdb_count_block(zcb, NULL, &blk, ZDB_OT_DITTO); 2189 } else { 2190 zcb->zcb_dedup_asize += 2191 BP_GET_ASIZE(&blk) * (ddp->ddp_refcnt - 1); 2192 zcb->zcb_dedup_blocks++; 2193 } 2194 } 2195 if (!dump_opt['L']) { 2196 ddt_t *ddt = spa->spa_ddt[ddb.ddb_checksum]; 2197 ddt_enter(ddt); 2198 VERIFY(ddt_lookup(ddt, &blk, B_TRUE) != NULL); 2199 ddt_exit(ddt); 2200 } 2201 } 2202 2203 ASSERT(error == ENOENT); 2204 } 2205 2206 static void 2207 zdb_leak_init(spa_t *spa, zdb_cb_t *zcb) 2208 { 2209 zcb->zcb_spa = spa; 2210 2211 if (!dump_opt['L']) { 2212 vdev_t *rvd = spa->spa_root_vdev; 2213 for (int c = 0; c < rvd->vdev_children; c++) { 2214 vdev_t *vd = rvd->vdev_child[c]; 2215 for (int m = 0; m < vd->vdev_ms_count; m++) { 2216 metaslab_t *msp = vd->vdev_ms[m]; 2217 mutex_enter(&msp->ms_lock); 2218 space_map_unload(&msp->ms_map); 2219 VERIFY(space_map_load(&msp->ms_map, 2220 &zdb_space_map_ops, SM_ALLOC, &msp->ms_smo, 2221 spa->spa_meta_objset) == 0); 2222 msp->ms_map.sm_ppd = vd; 2223 mutex_exit(&msp->ms_lock); 2224 } 2225 } 2226 } 2227 2228 spa_config_enter(spa, SCL_CONFIG, FTAG, RW_READER); 2229 2230 zdb_ddt_leak_init(spa, zcb); 2231 2232 spa_config_exit(spa, SCL_CONFIG, FTAG); 2233 } 2234 2235 static void 2236 zdb_leak_fini(spa_t *spa) 2237 { 2238 if (!dump_opt['L']) { 2239 vdev_t *rvd = spa->spa_root_vdev; 2240 for (int c = 0; c < rvd->vdev_children; c++) { 2241 vdev_t *vd = rvd->vdev_child[c]; 2242 for (int m = 0; m < vd->vdev_ms_count; m++) { 2243 metaslab_t *msp = vd->vdev_ms[m]; 2244 mutex_enter(&msp->ms_lock); 2245 space_map_unload(&msp->ms_map); 2246 mutex_exit(&msp->ms_lock); 2247 } 2248 } 2249 } 2250 } 2251 2252 /* ARGSUSED */ 2253 static int 2254 count_block_cb(void *arg, const blkptr_t *bp, dmu_tx_t *tx) 2255 { 2256 zdb_cb_t *zcb = arg; 2257 2258 if (dump_opt['b'] >= 4) { 2259 char blkbuf[BP_SPRINTF_LEN]; 2260 sprintf_blkptr(blkbuf, bp); 2261 (void) printf("[%s] %s\n", 2262 "deferred free", blkbuf); 2263 } 2264 zdb_count_block(zcb, NULL, bp, ZDB_OT_DEFERRED); 2265 return (0); 2266 } 2267 2268 static int 2269 dump_block_stats(spa_t *spa) 2270 { 2271 zdb_cb_t zcb = { 0 }; 2272 zdb_blkstats_t *zb, *tzb; 2273 uint64_t norm_alloc, norm_space, total_alloc, total_found; 2274 int flags = TRAVERSE_PRE | TRAVERSE_PREFETCH_METADATA | TRAVERSE_HARD; 2275 int leaks = 0; 2276 2277 (void) printf("\nTraversing all blocks %s%s%s%s%s...\n", 2278 (dump_opt['c'] || !dump_opt['L']) ? "to verify " : "", 2279 (dump_opt['c'] == 1) ? "metadata " : "", 2280 dump_opt['c'] ? "checksums " : "", 2281 (dump_opt['c'] && !dump_opt['L']) ? "and verify " : "", 2282 !dump_opt['L'] ? "nothing leaked " : ""); 2283 2284 /* 2285 * Load all space maps as SM_ALLOC maps, then traverse the pool 2286 * claiming each block we discover. If the pool is perfectly 2287 * consistent, the space maps will be empty when we're done. 2288 * Anything left over is a leak; any block we can't claim (because 2289 * it's not part of any space map) is a double allocation, 2290 * reference to a freed block, or an unclaimed log block. 2291 */ 2292 zdb_leak_init(spa, &zcb); 2293 2294 /* 2295 * If there's a deferred-free bplist, process that first. 2296 */ 2297 (void) bpobj_iterate_nofree(&spa->spa_deferred_bpobj, 2298 count_block_cb, &zcb, NULL); 2299 (void) bpobj_iterate_nofree(&spa->spa_dsl_pool->dp_free_bpobj, 2300 count_block_cb, &zcb, NULL); 2301 if (spa_feature_is_active(spa, 2302 &spa_feature_table[SPA_FEATURE_ASYNC_DESTROY])) { 2303 VERIFY3U(0, ==, bptree_iterate(spa->spa_meta_objset, 2304 spa->spa_dsl_pool->dp_bptree_obj, B_FALSE, count_block_cb, 2305 &zcb, NULL)); 2306 } 2307 2308 if (dump_opt['c'] > 1) 2309 flags |= TRAVERSE_PREFETCH_DATA; 2310 2311 zcb.zcb_haderrors |= traverse_pool(spa, 0, flags, zdb_blkptr_cb, &zcb); 2312 2313 /* 2314 * If we've traversed the data blocks then we need to wait for those 2315 * I/Os to complete. We leverage "The Godfather" zio to wait on 2316 * all async I/Os to complete. 2317 */ 2318 if (dump_opt['c']) { 2319 (void) zio_wait(spa->spa_async_zio_root); 2320 spa->spa_async_zio_root = zio_root(spa, NULL, NULL, 2321 ZIO_FLAG_CANFAIL | ZIO_FLAG_SPECULATIVE | 2322 ZIO_FLAG_GODFATHER); 2323 } 2324 2325 if (zcb.zcb_haderrors) { 2326 (void) printf("\nError counts:\n\n"); 2327 (void) printf("\t%5s %s\n", "errno", "count"); 2328 for (int e = 0; e < 256; e++) { 2329 if (zcb.zcb_errors[e] != 0) { 2330 (void) printf("\t%5d %llu\n", 2331 e, (u_longlong_t)zcb.zcb_errors[e]); 2332 } 2333 } 2334 } 2335 2336 /* 2337 * Report any leaked segments. 2338 */ 2339 zdb_leak_fini(spa); 2340 2341 tzb = &zcb.zcb_type[ZB_TOTAL][ZDB_OT_TOTAL]; 2342 2343 norm_alloc = metaslab_class_get_alloc(spa_normal_class(spa)); 2344 norm_space = metaslab_class_get_space(spa_normal_class(spa)); 2345 2346 total_alloc = norm_alloc + metaslab_class_get_alloc(spa_log_class(spa)); 2347 total_found = tzb->zb_asize - zcb.zcb_dedup_asize; 2348 2349 if (total_found == total_alloc) { 2350 if (!dump_opt['L']) 2351 (void) printf("\n\tNo leaks (block sum matches space" 2352 " maps exactly)\n"); 2353 } else { 2354 (void) printf("block traversal size %llu != alloc %llu " 2355 "(%s %lld)\n", 2356 (u_longlong_t)total_found, 2357 (u_longlong_t)total_alloc, 2358 (dump_opt['L']) ? "unreachable" : "leaked", 2359 (longlong_t)(total_alloc - total_found)); 2360 leaks = 1; 2361 } 2362 2363 if (tzb->zb_count == 0) 2364 return (2); 2365 2366 (void) printf("\n"); 2367 (void) printf("\tbp count: %10llu\n", 2368 (u_longlong_t)tzb->zb_count); 2369 (void) printf("\tbp logical: %10llu avg: %6llu\n", 2370 (u_longlong_t)tzb->zb_lsize, 2371 (u_longlong_t)(tzb->zb_lsize / tzb->zb_count)); 2372 (void) printf("\tbp physical: %10llu avg:" 2373 " %6llu compression: %6.2f\n", 2374 (u_longlong_t)tzb->zb_psize, 2375 (u_longlong_t)(tzb->zb_psize / tzb->zb_count), 2376 (double)tzb->zb_lsize / tzb->zb_psize); 2377 (void) printf("\tbp allocated: %10llu avg:" 2378 " %6llu compression: %6.2f\n", 2379 (u_longlong_t)tzb->zb_asize, 2380 (u_longlong_t)(tzb->zb_asize / tzb->zb_count), 2381 (double)tzb->zb_lsize / tzb->zb_asize); 2382 (void) printf("\tbp deduped: %10llu ref>1:" 2383 " %6llu deduplication: %6.2f\n", 2384 (u_longlong_t)zcb.zcb_dedup_asize, 2385 (u_longlong_t)zcb.zcb_dedup_blocks, 2386 (double)zcb.zcb_dedup_asize / tzb->zb_asize + 1.0); 2387 (void) printf("\tSPA allocated: %10llu used: %5.2f%%\n", 2388 (u_longlong_t)norm_alloc, 100.0 * norm_alloc / norm_space); 2389 2390 if (dump_opt['b'] >= 2) { 2391 int l, t, level; 2392 (void) printf("\nBlocks\tLSIZE\tPSIZE\tASIZE" 2393 "\t avg\t comp\t%%Total\tType\n"); 2394 2395 for (t = 0; t <= ZDB_OT_TOTAL; t++) { 2396 char csize[32], lsize[32], psize[32], asize[32]; 2397 char avg[32]; 2398 char *typename; 2399 2400 if (t < DMU_OT_NUMTYPES) 2401 typename = dmu_ot[t].ot_name; 2402 else 2403 typename = zdb_ot_extname[t - DMU_OT_NUMTYPES]; 2404 2405 if (zcb.zcb_type[ZB_TOTAL][t].zb_asize == 0) { 2406 (void) printf("%6s\t%5s\t%5s\t%5s" 2407 "\t%5s\t%5s\t%6s\t%s\n", 2408 "-", 2409 "-", 2410 "-", 2411 "-", 2412 "-", 2413 "-", 2414 "-", 2415 typename); 2416 continue; 2417 } 2418 2419 for (l = ZB_TOTAL - 1; l >= -1; l--) { 2420 level = (l == -1 ? ZB_TOTAL : l); 2421 zb = &zcb.zcb_type[level][t]; 2422 2423 if (zb->zb_asize == 0) 2424 continue; 2425 2426 if (dump_opt['b'] < 3 && level != ZB_TOTAL) 2427 continue; 2428 2429 if (level == 0 && zb->zb_asize == 2430 zcb.zcb_type[ZB_TOTAL][t].zb_asize) 2431 continue; 2432 2433 zdb_nicenum(zb->zb_count, csize); 2434 zdb_nicenum(zb->zb_lsize, lsize); 2435 zdb_nicenum(zb->zb_psize, psize); 2436 zdb_nicenum(zb->zb_asize, asize); 2437 zdb_nicenum(zb->zb_asize / zb->zb_count, avg); 2438 2439 (void) printf("%6s\t%5s\t%5s\t%5s\t%5s" 2440 "\t%5.2f\t%6.2f\t", 2441 csize, lsize, psize, asize, avg, 2442 (double)zb->zb_lsize / zb->zb_psize, 2443 100.0 * zb->zb_asize / tzb->zb_asize); 2444 2445 if (level == ZB_TOTAL) 2446 (void) printf("%s\n", typename); 2447 else 2448 (void) printf(" L%d %s\n", 2449 level, typename); 2450 } 2451 } 2452 } 2453 2454 (void) printf("\n"); 2455 2456 if (leaks) 2457 return (2); 2458 2459 if (zcb.zcb_haderrors) 2460 return (3); 2461 2462 return (0); 2463 } 2464 2465 typedef struct zdb_ddt_entry { 2466 ddt_key_t zdde_key; 2467 uint64_t zdde_ref_blocks; 2468 uint64_t zdde_ref_lsize; 2469 uint64_t zdde_ref_psize; 2470 uint64_t zdde_ref_dsize; 2471 avl_node_t zdde_node; 2472 } zdb_ddt_entry_t; 2473 2474 /* ARGSUSED */ 2475 static int 2476 zdb_ddt_add_cb(spa_t *spa, zilog_t *zilog, const blkptr_t *bp, 2477 arc_buf_t *pbuf, const zbookmark_t *zb, const dnode_phys_t *dnp, void *arg) 2478 { 2479 avl_tree_t *t = arg; 2480 avl_index_t where; 2481 zdb_ddt_entry_t *zdde, zdde_search; 2482 2483 if (bp == NULL) 2484 return (0); 2485 2486 if (dump_opt['S'] > 1 && zb->zb_level == ZB_ROOT_LEVEL) { 2487 (void) printf("traversing objset %llu, %llu objects, " 2488 "%lu blocks so far\n", 2489 (u_longlong_t)zb->zb_objset, 2490 (u_longlong_t)bp->blk_fill, 2491 avl_numnodes(t)); 2492 } 2493 2494 if (BP_IS_HOLE(bp) || BP_GET_CHECKSUM(bp) == ZIO_CHECKSUM_OFF || 2495 BP_GET_LEVEL(bp) > 0 || DMU_OT_IS_METADATA(BP_GET_TYPE(bp))) 2496 return (0); 2497 2498 ddt_key_fill(&zdde_search.zdde_key, bp); 2499 2500 zdde = avl_find(t, &zdde_search, &where); 2501 2502 if (zdde == NULL) { 2503 zdde = umem_zalloc(sizeof (*zdde), UMEM_NOFAIL); 2504 zdde->zdde_key = zdde_search.zdde_key; 2505 avl_insert(t, zdde, where); 2506 } 2507 2508 zdde->zdde_ref_blocks += 1; 2509 zdde->zdde_ref_lsize += BP_GET_LSIZE(bp); 2510 zdde->zdde_ref_psize += BP_GET_PSIZE(bp); 2511 zdde->zdde_ref_dsize += bp_get_dsize_sync(spa, bp); 2512 2513 return (0); 2514 } 2515 2516 static void 2517 dump_simulated_ddt(spa_t *spa) 2518 { 2519 avl_tree_t t; 2520 void *cookie = NULL; 2521 zdb_ddt_entry_t *zdde; 2522 ddt_histogram_t ddh_total = { 0 }; 2523 ddt_stat_t dds_total = { 0 }; 2524 2525 avl_create(&t, ddt_entry_compare, 2526 sizeof (zdb_ddt_entry_t), offsetof(zdb_ddt_entry_t, zdde_node)); 2527 2528 spa_config_enter(spa, SCL_CONFIG, FTAG, RW_READER); 2529 2530 (void) traverse_pool(spa, 0, TRAVERSE_PRE | TRAVERSE_PREFETCH_METADATA, 2531 zdb_ddt_add_cb, &t); 2532 2533 spa_config_exit(spa, SCL_CONFIG, FTAG); 2534 2535 while ((zdde = avl_destroy_nodes(&t, &cookie)) != NULL) { 2536 ddt_stat_t dds; 2537 uint64_t refcnt = zdde->zdde_ref_blocks; 2538 ASSERT(refcnt != 0); 2539 2540 dds.dds_blocks = zdde->zdde_ref_blocks / refcnt; 2541 dds.dds_lsize = zdde->zdde_ref_lsize / refcnt; 2542 dds.dds_psize = zdde->zdde_ref_psize / refcnt; 2543 dds.dds_dsize = zdde->zdde_ref_dsize / refcnt; 2544 2545 dds.dds_ref_blocks = zdde->zdde_ref_blocks; 2546 dds.dds_ref_lsize = zdde->zdde_ref_lsize; 2547 dds.dds_ref_psize = zdde->zdde_ref_psize; 2548 dds.dds_ref_dsize = zdde->zdde_ref_dsize; 2549 2550 ddt_stat_add(&ddh_total.ddh_stat[highbit(refcnt) - 1], &dds, 0); 2551 2552 umem_free(zdde, sizeof (*zdde)); 2553 } 2554 2555 avl_destroy(&t); 2556 2557 ddt_histogram_stat(&dds_total, &ddh_total); 2558 2559 (void) printf("Simulated DDT histogram:\n"); 2560 2561 zpool_dump_ddt(&dds_total, &ddh_total); 2562 2563 dump_dedup_ratio(&dds_total); 2564 } 2565 2566 static void 2567 dump_zpool(spa_t *spa) 2568 { 2569 dsl_pool_t *dp = spa_get_dsl(spa); 2570 int rc = 0; 2571 2572 if (dump_opt['S']) { 2573 dump_simulated_ddt(spa); 2574 return; 2575 } 2576 2577 if (!dump_opt['e'] && dump_opt['C'] > 1) { 2578 (void) printf("\nCached configuration:\n"); 2579 dump_nvlist(spa->spa_config, 8); 2580 } 2581 2582 if (dump_opt['C']) 2583 dump_config(spa); 2584 2585 if (dump_opt['u']) 2586 dump_uberblock(&spa->spa_uberblock, "\nUberblock:\n", "\n"); 2587 2588 if (dump_opt['D']) 2589 dump_all_ddts(spa); 2590 2591 if (dump_opt['d'] > 2 || dump_opt['m']) 2592 dump_metaslabs(spa); 2593 2594 if (dump_opt['d'] || dump_opt['i']) { 2595 dump_dir(dp->dp_meta_objset); 2596 if (dump_opt['d'] >= 3) { 2597 dump_bpobj(&spa->spa_deferred_bpobj, "Deferred frees"); 2598 if (spa_version(spa) >= SPA_VERSION_DEADLISTS) { 2599 dump_bpobj(&spa->spa_dsl_pool->dp_free_bpobj, 2600 "Pool snapshot frees"); 2601 } 2602 2603 if (spa_feature_is_active(spa, 2604 &spa_feature_table[SPA_FEATURE_ASYNC_DESTROY])) { 2605 dump_bptree(spa->spa_meta_objset, 2606 spa->spa_dsl_pool->dp_bptree_obj, 2607 "Pool dataset frees"); 2608 } 2609 dump_dtl(spa->spa_root_vdev, 0); 2610 } 2611 (void) dmu_objset_find(spa_name(spa), dump_one_dir, 2612 NULL, DS_FIND_SNAPSHOTS | DS_FIND_CHILDREN); 2613 } 2614 if (dump_opt['b'] || dump_opt['c']) 2615 rc = dump_block_stats(spa); 2616 2617 if (dump_opt['s']) 2618 show_pool_stats(spa); 2619 2620 if (dump_opt['h']) 2621 dump_history(spa); 2622 2623 if (rc != 0) 2624 exit(rc); 2625 } 2626 2627 #define ZDB_FLAG_CHECKSUM 0x0001 2628 #define ZDB_FLAG_DECOMPRESS 0x0002 2629 #define ZDB_FLAG_BSWAP 0x0004 2630 #define ZDB_FLAG_GBH 0x0008 2631 #define ZDB_FLAG_INDIRECT 0x0010 2632 #define ZDB_FLAG_PHYS 0x0020 2633 #define ZDB_FLAG_RAW 0x0040 2634 #define ZDB_FLAG_PRINT_BLKPTR 0x0080 2635 2636 int flagbits[256]; 2637 2638 static void 2639 zdb_print_blkptr(blkptr_t *bp, int flags) 2640 { 2641 char blkbuf[BP_SPRINTF_LEN]; 2642 2643 if (flags & ZDB_FLAG_BSWAP) 2644 byteswap_uint64_array((void *)bp, sizeof (blkptr_t)); 2645 2646 sprintf_blkptr(blkbuf, bp); 2647 (void) printf("%s\n", blkbuf); 2648 } 2649 2650 static void 2651 zdb_dump_indirect(blkptr_t *bp, int nbps, int flags) 2652 { 2653 int i; 2654 2655 for (i = 0; i < nbps; i++) 2656 zdb_print_blkptr(&bp[i], flags); 2657 } 2658 2659 static void 2660 zdb_dump_gbh(void *buf, int flags) 2661 { 2662 zdb_dump_indirect((blkptr_t *)buf, SPA_GBH_NBLKPTRS, flags); 2663 } 2664 2665 static void 2666 zdb_dump_block_raw(void *buf, uint64_t size, int flags) 2667 { 2668 if (flags & ZDB_FLAG_BSWAP) 2669 byteswap_uint64_array(buf, size); 2670 (void) write(1, buf, size); 2671 } 2672 2673 static void 2674 zdb_dump_block(char *label, void *buf, uint64_t size, int flags) 2675 { 2676 uint64_t *d = (uint64_t *)buf; 2677 int nwords = size / sizeof (uint64_t); 2678 int do_bswap = !!(flags & ZDB_FLAG_BSWAP); 2679 int i, j; 2680 char *hdr, *c; 2681 2682 2683 if (do_bswap) 2684 hdr = " 7 6 5 4 3 2 1 0 f e d c b a 9 8"; 2685 else 2686 hdr = " 0 1 2 3 4 5 6 7 8 9 a b c d e f"; 2687 2688 (void) printf("\n%s\n%6s %s 0123456789abcdef\n", label, "", hdr); 2689 2690 for (i = 0; i < nwords; i += 2) { 2691 (void) printf("%06llx: %016llx %016llx ", 2692 (u_longlong_t)(i * sizeof (uint64_t)), 2693 (u_longlong_t)(do_bswap ? BSWAP_64(d[i]) : d[i]), 2694 (u_longlong_t)(do_bswap ? BSWAP_64(d[i + 1]) : d[i + 1])); 2695 2696 c = (char *)&d[i]; 2697 for (j = 0; j < 2 * sizeof (uint64_t); j++) 2698 (void) printf("%c", isprint(c[j]) ? c[j] : '.'); 2699 (void) printf("\n"); 2700 } 2701 } 2702 2703 /* 2704 * There are two acceptable formats: 2705 * leaf_name - For example: c1t0d0 or /tmp/ztest.0a 2706 * child[.child]* - For example: 0.1.1 2707 * 2708 * The second form can be used to specify arbitrary vdevs anywhere 2709 * in the heirarchy. For example, in a pool with a mirror of 2710 * RAID-Zs, you can specify either RAID-Z vdev with 0.0 or 0.1 . 2711 */ 2712 static vdev_t * 2713 zdb_vdev_lookup(vdev_t *vdev, char *path) 2714 { 2715 char *s, *p, *q; 2716 int i; 2717 2718 if (vdev == NULL) 2719 return (NULL); 2720 2721 /* First, assume the x.x.x.x format */ 2722 i = (int)strtoul(path, &s, 10); 2723 if (s == path || (s && *s != '.' && *s != '\0')) 2724 goto name; 2725 if (i < 0 || i >= vdev->vdev_children) 2726 return (NULL); 2727 2728 vdev = vdev->vdev_child[i]; 2729 if (*s == '\0') 2730 return (vdev); 2731 return (zdb_vdev_lookup(vdev, s+1)); 2732 2733 name: 2734 for (i = 0; i < vdev->vdev_children; i++) { 2735 vdev_t *vc = vdev->vdev_child[i]; 2736 2737 if (vc->vdev_path == NULL) { 2738 vc = zdb_vdev_lookup(vc, path); 2739 if (vc == NULL) 2740 continue; 2741 else 2742 return (vc); 2743 } 2744 2745 p = strrchr(vc->vdev_path, '/'); 2746 p = p ? p + 1 : vc->vdev_path; 2747 q = &vc->vdev_path[strlen(vc->vdev_path) - 2]; 2748 2749 if (strcmp(vc->vdev_path, path) == 0) 2750 return (vc); 2751 if (strcmp(p, path) == 0) 2752 return (vc); 2753 if (strcmp(q, "s0") == 0 && strncmp(p, path, q - p) == 0) 2754 return (vc); 2755 } 2756 2757 return (NULL); 2758 } 2759 2760 /* 2761 * Read a block from a pool and print it out. The syntax of the 2762 * block descriptor is: 2763 * 2764 * pool:vdev_specifier:offset:size[:flags] 2765 * 2766 * pool - The name of the pool you wish to read from 2767 * vdev_specifier - Which vdev (see comment for zdb_vdev_lookup) 2768 * offset - offset, in hex, in bytes 2769 * size - Amount of data to read, in hex, in bytes 2770 * flags - A string of characters specifying options 2771 * b: Decode a blkptr at given offset within block 2772 * *c: Calculate and display checksums 2773 * d: Decompress data before dumping 2774 * e: Byteswap data before dumping 2775 * g: Display data as a gang block header 2776 * i: Display as an indirect block 2777 * p: Do I/O to physical offset 2778 * r: Dump raw data to stdout 2779 * 2780 * * = not yet implemented 2781 */ 2782 static void 2783 zdb_read_block(char *thing, spa_t *spa) 2784 { 2785 blkptr_t blk, *bp = &blk; 2786 dva_t *dva = bp->blk_dva; 2787 int flags = 0; 2788 uint64_t offset = 0, size = 0, psize = 0, lsize = 0, blkptr_offset = 0; 2789 zio_t *zio; 2790 vdev_t *vd; 2791 void *pbuf, *lbuf, *buf; 2792 char *s, *p, *dup, *vdev, *flagstr; 2793 int i, error; 2794 2795 dup = strdup(thing); 2796 s = strtok(dup, ":"); 2797 vdev = s ? s : ""; 2798 s = strtok(NULL, ":"); 2799 offset = strtoull(s ? s : "", NULL, 16); 2800 s = strtok(NULL, ":"); 2801 size = strtoull(s ? s : "", NULL, 16); 2802 s = strtok(NULL, ":"); 2803 flagstr = s ? s : ""; 2804 2805 s = NULL; 2806 if (size == 0) 2807 s = "size must not be zero"; 2808 if (!IS_P2ALIGNED(size, DEV_BSIZE)) 2809 s = "size must be a multiple of sector size"; 2810 if (!IS_P2ALIGNED(offset, DEV_BSIZE)) 2811 s = "offset must be a multiple of sector size"; 2812 if (s) { 2813 (void) printf("Invalid block specifier: %s - %s\n", thing, s); 2814 free(dup); 2815 return; 2816 } 2817 2818 for (s = strtok(flagstr, ":"); s; s = strtok(NULL, ":")) { 2819 for (i = 0; flagstr[i]; i++) { 2820 int bit = flagbits[(uchar_t)flagstr[i]]; 2821 2822 if (bit == 0) { 2823 (void) printf("***Invalid flag: %c\n", 2824 flagstr[i]); 2825 continue; 2826 } 2827 flags |= bit; 2828 2829 /* If it's not something with an argument, keep going */ 2830 if ((bit & (ZDB_FLAG_CHECKSUM | 2831 ZDB_FLAG_PRINT_BLKPTR)) == 0) 2832 continue; 2833 2834 p = &flagstr[i + 1]; 2835 if (bit == ZDB_FLAG_PRINT_BLKPTR) 2836 blkptr_offset = strtoull(p, &p, 16); 2837 if (*p != ':' && *p != '\0') { 2838 (void) printf("***Invalid flag arg: '%s'\n", s); 2839 free(dup); 2840 return; 2841 } 2842 } 2843 } 2844 2845 vd = zdb_vdev_lookup(spa->spa_root_vdev, vdev); 2846 if (vd == NULL) { 2847 (void) printf("***Invalid vdev: %s\n", vdev); 2848 free(dup); 2849 return; 2850 } else { 2851 if (vd->vdev_path) 2852 (void) fprintf(stderr, "Found vdev: %s\n", 2853 vd->vdev_path); 2854 else 2855 (void) fprintf(stderr, "Found vdev type: %s\n", 2856 vd->vdev_ops->vdev_op_type); 2857 } 2858 2859 psize = size; 2860 lsize = size; 2861 2862 pbuf = umem_alloc(SPA_MAXBLOCKSIZE, UMEM_NOFAIL); 2863 lbuf = umem_alloc(SPA_MAXBLOCKSIZE, UMEM_NOFAIL); 2864 2865 BP_ZERO(bp); 2866 2867 DVA_SET_VDEV(&dva[0], vd->vdev_id); 2868 DVA_SET_OFFSET(&dva[0], offset); 2869 DVA_SET_GANG(&dva[0], !!(flags & ZDB_FLAG_GBH)); 2870 DVA_SET_ASIZE(&dva[0], vdev_psize_to_asize(vd, psize)); 2871 2872 BP_SET_BIRTH(bp, TXG_INITIAL, TXG_INITIAL); 2873 2874 BP_SET_LSIZE(bp, lsize); 2875 BP_SET_PSIZE(bp, psize); 2876 BP_SET_COMPRESS(bp, ZIO_COMPRESS_OFF); 2877 BP_SET_CHECKSUM(bp, ZIO_CHECKSUM_OFF); 2878 BP_SET_TYPE(bp, DMU_OT_NONE); 2879 BP_SET_LEVEL(bp, 0); 2880 BP_SET_DEDUP(bp, 0); 2881 BP_SET_BYTEORDER(bp, ZFS_HOST_BYTEORDER); 2882 2883 spa_config_enter(spa, SCL_STATE, FTAG, RW_READER); 2884 zio = zio_root(spa, NULL, NULL, 0); 2885 2886 if (vd == vd->vdev_top) { 2887 /* 2888 * Treat this as a normal block read. 2889 */ 2890 zio_nowait(zio_read(zio, spa, bp, pbuf, psize, NULL, NULL, 2891 ZIO_PRIORITY_SYNC_READ, 2892 ZIO_FLAG_CANFAIL | ZIO_FLAG_RAW, NULL)); 2893 } else { 2894 /* 2895 * Treat this as a vdev child I/O. 2896 */ 2897 zio_nowait(zio_vdev_child_io(zio, bp, vd, offset, pbuf, psize, 2898 ZIO_TYPE_READ, ZIO_PRIORITY_SYNC_READ, 2899 ZIO_FLAG_DONT_CACHE | ZIO_FLAG_DONT_QUEUE | 2900 ZIO_FLAG_DONT_PROPAGATE | ZIO_FLAG_DONT_RETRY | 2901 ZIO_FLAG_CANFAIL | ZIO_FLAG_RAW, NULL, NULL)); 2902 } 2903 2904 error = zio_wait(zio); 2905 spa_config_exit(spa, SCL_STATE, FTAG); 2906 2907 if (error) { 2908 (void) printf("Read of %s failed, error: %d\n", thing, error); 2909 goto out; 2910 } 2911 2912 if (flags & ZDB_FLAG_DECOMPRESS) { 2913 /* 2914 * We don't know how the data was compressed, so just try 2915 * every decompress function at every inflated blocksize. 2916 */ 2917 enum zio_compress c; 2918 void *pbuf2 = umem_alloc(SPA_MAXBLOCKSIZE, UMEM_NOFAIL); 2919 void *lbuf2 = umem_alloc(SPA_MAXBLOCKSIZE, UMEM_NOFAIL); 2920 2921 bcopy(pbuf, pbuf2, psize); 2922 2923 VERIFY(random_get_pseudo_bytes((uint8_t *)pbuf + psize, 2924 SPA_MAXBLOCKSIZE - psize) == 0); 2925 2926 VERIFY(random_get_pseudo_bytes((uint8_t *)pbuf2 + psize, 2927 SPA_MAXBLOCKSIZE - psize) == 0); 2928 2929 for (lsize = SPA_MAXBLOCKSIZE; lsize > psize; 2930 lsize -= SPA_MINBLOCKSIZE) { 2931 for (c = 0; c < ZIO_COMPRESS_FUNCTIONS; c++) { 2932 if (zio_decompress_data(c, pbuf, lbuf, 2933 psize, lsize) == 0 && 2934 zio_decompress_data(c, pbuf2, lbuf2, 2935 psize, lsize) == 0 && 2936 bcmp(lbuf, lbuf2, lsize) == 0) 2937 break; 2938 } 2939 if (c != ZIO_COMPRESS_FUNCTIONS) 2940 break; 2941 lsize -= SPA_MINBLOCKSIZE; 2942 } 2943 2944 umem_free(pbuf2, SPA_MAXBLOCKSIZE); 2945 umem_free(lbuf2, SPA_MAXBLOCKSIZE); 2946 2947 if (lsize <= psize) { 2948 (void) printf("Decompress of %s failed\n", thing); 2949 goto out; 2950 } 2951 buf = lbuf; 2952 size = lsize; 2953 } else { 2954 buf = pbuf; 2955 size = psize; 2956 } 2957 2958 if (flags & ZDB_FLAG_PRINT_BLKPTR) 2959 zdb_print_blkptr((blkptr_t *)(void *) 2960 ((uintptr_t)buf + (uintptr_t)blkptr_offset), flags); 2961 else if (flags & ZDB_FLAG_RAW) 2962 zdb_dump_block_raw(buf, size, flags); 2963 else if (flags & ZDB_FLAG_INDIRECT) 2964 zdb_dump_indirect((blkptr_t *)buf, size / sizeof (blkptr_t), 2965 flags); 2966 else if (flags & ZDB_FLAG_GBH) 2967 zdb_dump_gbh(buf, flags); 2968 else 2969 zdb_dump_block(thing, buf, size, flags); 2970 2971 out: 2972 umem_free(pbuf, SPA_MAXBLOCKSIZE); 2973 umem_free(lbuf, SPA_MAXBLOCKSIZE); 2974 free(dup); 2975 } 2976 2977 static boolean_t 2978 pool_match(nvlist_t *cfg, char *tgt) 2979 { 2980 uint64_t v, guid = strtoull(tgt, NULL, 0); 2981 char *s; 2982 2983 if (guid != 0) { 2984 if (nvlist_lookup_uint64(cfg, ZPOOL_CONFIG_POOL_GUID, &v) == 0) 2985 return (v == guid); 2986 } else { 2987 if (nvlist_lookup_string(cfg, ZPOOL_CONFIG_POOL_NAME, &s) == 0) 2988 return (strcmp(s, tgt) == 0); 2989 } 2990 return (B_FALSE); 2991 } 2992 2993 static char * 2994 find_zpool(char **target, nvlist_t **configp, int dirc, char **dirv) 2995 { 2996 nvlist_t *pools; 2997 nvlist_t *match = NULL; 2998 char *name = NULL; 2999 char *sepp = NULL; 3000 char sep; 3001 int count = 0; 3002 importargs_t args = { 0 }; 3003 3004 args.paths = dirc; 3005 args.path = dirv; 3006 args.can_be_active = B_TRUE; 3007 3008 if ((sepp = strpbrk(*target, "/@")) != NULL) { 3009 sep = *sepp; 3010 *sepp = '\0'; 3011 } 3012 3013 pools = zpool_search_import(g_zfs, &args); 3014 3015 if (pools != NULL) { 3016 nvpair_t *elem = NULL; 3017 while ((elem = nvlist_next_nvpair(pools, elem)) != NULL) { 3018 verify(nvpair_value_nvlist(elem, configp) == 0); 3019 if (pool_match(*configp, *target)) { 3020 count++; 3021 if (match != NULL) { 3022 /* print previously found config */ 3023 if (name != NULL) { 3024 (void) printf("%s\n", name); 3025 dump_nvlist(match, 8); 3026 name = NULL; 3027 } 3028 (void) printf("%s\n", 3029 nvpair_name(elem)); 3030 dump_nvlist(*configp, 8); 3031 } else { 3032 match = *configp; 3033 name = nvpair_name(elem); 3034 } 3035 } 3036 } 3037 } 3038 if (count > 1) 3039 (void) fatal("\tMatched %d pools - use pool GUID " 3040 "instead of pool name or \n" 3041 "\tpool name part of a dataset name to select pool", count); 3042 3043 if (sepp) 3044 *sepp = sep; 3045 /* 3046 * If pool GUID was specified for pool id, replace it with pool name 3047 */ 3048 if (name && (strstr(*target, name) != *target)) { 3049 int sz = 1 + strlen(name) + ((sepp) ? strlen(sepp) : 0); 3050 3051 *target = umem_alloc(sz, UMEM_NOFAIL); 3052 (void) snprintf(*target, sz, "%s%s", name, sepp ? sepp : ""); 3053 } 3054 3055 *configp = name ? match : NULL; 3056 3057 return (name); 3058 } 3059 3060 int 3061 main(int argc, char **argv) 3062 { 3063 int i, c; 3064 struct rlimit rl = { 1024, 1024 }; 3065 spa_t *spa = NULL; 3066 objset_t *os = NULL; 3067 int dump_all = 1; 3068 int verbose = 0; 3069 int error = 0; 3070 char **searchdirs = NULL; 3071 int nsearch = 0; 3072 char *target; 3073 nvlist_t *policy = NULL; 3074 uint64_t max_txg = UINT64_MAX; 3075 int rewind = ZPOOL_NEVER_REWIND; 3076 3077 (void) setrlimit(RLIMIT_NOFILE, &rl); 3078 (void) enable_extended_FILE_stdio(-1, -1); 3079 3080 dprintf_setup(&argc, argv); 3081 3082 while ((c = getopt(argc, argv, "bcdhilmM:suCDRSAFLXevp:t:U:P")) != -1) { 3083 switch (c) { 3084 case 'b': 3085 case 'c': 3086 case 'd': 3087 case 'h': 3088 case 'i': 3089 case 'l': 3090 case 'm': 3091 case 's': 3092 case 'u': 3093 case 'C': 3094 case 'D': 3095 case 'R': 3096 case 'S': 3097 dump_opt[c]++; 3098 dump_all = 0; 3099 break; 3100 case 'A': 3101 case 'F': 3102 case 'L': 3103 case 'X': 3104 case 'e': 3105 case 'P': 3106 dump_opt[c]++; 3107 break; 3108 case 'v': 3109 verbose++; 3110 break; 3111 case 'M': 3112 max_inflight = strtoull(optarg, NULL, 0); 3113 if (max_inflight == 0) { 3114 (void) fprintf(stderr, "maximum number " 3115 "of inflight I/Os must be greater " 3116 "than 0\n"); 3117 usage(); 3118 } 3119 break; 3120 case 'p': 3121 if (searchdirs == NULL) { 3122 searchdirs = umem_alloc(sizeof (char *), 3123 UMEM_NOFAIL); 3124 } else { 3125 char **tmp = umem_alloc((nsearch + 1) * 3126 sizeof (char *), UMEM_NOFAIL); 3127 bcopy(searchdirs, tmp, nsearch * 3128 sizeof (char *)); 3129 umem_free(searchdirs, 3130 nsearch * sizeof (char *)); 3131 searchdirs = tmp; 3132 } 3133 searchdirs[nsearch++] = optarg; 3134 break; 3135 case 't': 3136 max_txg = strtoull(optarg, NULL, 0); 3137 if (max_txg < TXG_INITIAL) { 3138 (void) fprintf(stderr, "incorrect txg " 3139 "specified: %s\n", optarg); 3140 usage(); 3141 } 3142 break; 3143 case 'U': 3144 spa_config_path = optarg; 3145 break; 3146 default: 3147 usage(); 3148 break; 3149 } 3150 } 3151 3152 if (!dump_opt['e'] && searchdirs != NULL) { 3153 (void) fprintf(stderr, "-p option requires use of -e\n"); 3154 usage(); 3155 } 3156 3157 kernel_init(FREAD); 3158 g_zfs = libzfs_init(); 3159 ASSERT(g_zfs != NULL); 3160 3161 if (dump_all) 3162 verbose = MAX(verbose, 1); 3163 3164 for (c = 0; c < 256; c++) { 3165 if (dump_all && !strchr("elAFLRSXP", c)) 3166 dump_opt[c] = 1; 3167 if (dump_opt[c]) 3168 dump_opt[c] += verbose; 3169 } 3170 3171 aok = (dump_opt['A'] == 1) || (dump_opt['A'] > 2); 3172 zfs_recover = (dump_opt['A'] > 1); 3173 3174 argc -= optind; 3175 argv += optind; 3176 3177 if (argc < 2 && dump_opt['R']) 3178 usage(); 3179 if (argc < 1) { 3180 if (!dump_opt['e'] && dump_opt['C']) { 3181 dump_cachefile(spa_config_path); 3182 return (0); 3183 } 3184 usage(); 3185 } 3186 3187 if (dump_opt['l']) { 3188 dump_label(argv[0]); 3189 return (0); 3190 } 3191 3192 if (dump_opt['X'] || dump_opt['F']) 3193 rewind = ZPOOL_DO_REWIND | 3194 (dump_opt['X'] ? ZPOOL_EXTREME_REWIND : 0); 3195 3196 if (nvlist_alloc(&policy, NV_UNIQUE_NAME_TYPE, 0) != 0 || 3197 nvlist_add_uint64(policy, ZPOOL_REWIND_REQUEST_TXG, max_txg) != 0 || 3198 nvlist_add_uint32(policy, ZPOOL_REWIND_REQUEST, rewind) != 0) 3199 fatal("internal error: %s", strerror(ENOMEM)); 3200 3201 error = 0; 3202 target = argv[0]; 3203 3204 if (dump_opt['e']) { 3205 nvlist_t *cfg = NULL; 3206 char *name = find_zpool(&target, &cfg, nsearch, searchdirs); 3207 3208 error = ENOENT; 3209 if (name) { 3210 if (dump_opt['C'] > 1) { 3211 (void) printf("\nConfiguration for import:\n"); 3212 dump_nvlist(cfg, 8); 3213 } 3214 if (nvlist_add_nvlist(cfg, 3215 ZPOOL_REWIND_POLICY, policy) != 0) { 3216 fatal("can't open '%s': %s", 3217 target, strerror(ENOMEM)); 3218 } 3219 if ((error = spa_import(name, cfg, NULL, 3220 ZFS_IMPORT_MISSING_LOG)) != 0) { 3221 error = spa_import(name, cfg, NULL, 3222 ZFS_IMPORT_VERBATIM); 3223 } 3224 } 3225 } 3226 3227 if (error == 0) { 3228 if (strpbrk(target, "/@") == NULL || dump_opt['R']) { 3229 error = spa_open_rewind(target, &spa, FTAG, policy, 3230 NULL); 3231 if (error) { 3232 /* 3233 * If we're missing the log device then 3234 * try opening the pool after clearing the 3235 * log state. 3236 */ 3237 mutex_enter(&spa_namespace_lock); 3238 if ((spa = spa_lookup(target)) != NULL && 3239 spa->spa_log_state == SPA_LOG_MISSING) { 3240 spa->spa_log_state = SPA_LOG_CLEAR; 3241 error = 0; 3242 } 3243 mutex_exit(&spa_namespace_lock); 3244 3245 if (!error) { 3246 error = spa_open_rewind(target, &spa, 3247 FTAG, policy, NULL); 3248 } 3249 } 3250 } else { 3251 error = dmu_objset_own(target, DMU_OST_ANY, 3252 B_TRUE, FTAG, &os); 3253 } 3254 } 3255 nvlist_free(policy); 3256 3257 if (error) 3258 fatal("can't open '%s': %s", target, strerror(error)); 3259 3260 argv++; 3261 argc--; 3262 if (!dump_opt['R']) { 3263 if (argc > 0) { 3264 zopt_objects = argc; 3265 zopt_object = calloc(zopt_objects, sizeof (uint64_t)); 3266 for (i = 0; i < zopt_objects; i++) { 3267 errno = 0; 3268 zopt_object[i] = strtoull(argv[i], NULL, 0); 3269 if (zopt_object[i] == 0 && errno != 0) 3270 fatal("bad number %s: %s", 3271 argv[i], strerror(errno)); 3272 } 3273 } 3274 (os != NULL) ? dump_dir(os) : dump_zpool(spa); 3275 } else { 3276 flagbits['b'] = ZDB_FLAG_PRINT_BLKPTR; 3277 flagbits['c'] = ZDB_FLAG_CHECKSUM; 3278 flagbits['d'] = ZDB_FLAG_DECOMPRESS; 3279 flagbits['e'] = ZDB_FLAG_BSWAP; 3280 flagbits['g'] = ZDB_FLAG_GBH; 3281 flagbits['i'] = ZDB_FLAG_INDIRECT; 3282 flagbits['p'] = ZDB_FLAG_PHYS; 3283 flagbits['r'] = ZDB_FLAG_RAW; 3284 3285 for (i = 0; i < argc; i++) 3286 zdb_read_block(argv[i], spa); 3287 } 3288 3289 (os != NULL) ? dmu_objset_disown(os, FTAG) : spa_close(spa, FTAG); 3290 3291 fuid_table_destroy(); 3292 sa_loaded = B_FALSE; 3293 3294 libzfs_fini(g_zfs); 3295 kernel_fini(); 3296 3297 return (0); 3298 } 3299