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) 2011, 2016 by Delphix. All rights reserved. 25 * Copyright (c) 2014 Integros [integros.com] 26 * Copyright 2017 Nexenta Systems, Inc. 27 */ 28 29 #include <stdio.h> 30 #include <unistd.h> 31 #include <stdio_ext.h> 32 #include <stdlib.h> 33 #include <ctype.h> 34 #include <sys/zfs_context.h> 35 #include <sys/spa.h> 36 #include <sys/spa_impl.h> 37 #include <sys/dmu.h> 38 #include <sys/zap.h> 39 #include <sys/fs/zfs.h> 40 #include <sys/zfs_znode.h> 41 #include <sys/zfs_sa.h> 42 #include <sys/sa.h> 43 #include <sys/sa_impl.h> 44 #include <sys/vdev.h> 45 #include <sys/vdev_impl.h> 46 #include <sys/metaslab_impl.h> 47 #include <sys/dmu_objset.h> 48 #include <sys/dsl_dir.h> 49 #include <sys/dsl_dataset.h> 50 #include <sys/dsl_pool.h> 51 #include <sys/dbuf.h> 52 #include <sys/zil.h> 53 #include <sys/zil_impl.h> 54 #include <sys/stat.h> 55 #include <sys/resource.h> 56 #include <sys/dmu_traverse.h> 57 #include <sys/zio_checksum.h> 58 #include <sys/zio_compress.h> 59 #include <sys/zfs_fuid.h> 60 #include <sys/arc.h> 61 #include <sys/ddt.h> 62 #include <sys/zfeature.h> 63 #include <sys/abd.h> 64 #include <sys/blkptr.h> 65 #include <zfs_comutil.h> 66 #include <libcmdutils.h> 67 #undef verify 68 #include <libzfs.h> 69 70 #define ZDB_COMPRESS_NAME(idx) ((idx) < ZIO_COMPRESS_FUNCTIONS ? \ 71 zio_compress_table[(idx)].ci_name : "UNKNOWN") 72 #define ZDB_CHECKSUM_NAME(idx) ((idx) < ZIO_CHECKSUM_FUNCTIONS ? \ 73 zio_checksum_table[(idx)].ci_name : "UNKNOWN") 74 #define ZDB_OT_NAME(idx) ((idx) < DMU_OT_NUMTYPES ? \ 75 dmu_ot[(idx)].ot_name : DMU_OT_IS_VALID(idx) ? \ 76 dmu_ot_byteswap[DMU_OT_BYTESWAP(idx)].ob_name : "UNKNOWN") 77 #define ZDB_OT_TYPE(idx) ((idx) < DMU_OT_NUMTYPES ? (idx) : \ 78 (((idx) == DMU_OTN_ZAP_DATA || (idx) == DMU_OTN_ZAP_METADATA) ? \ 79 DMU_OT_ZAP_OTHER : DMU_OT_NUMTYPES)) 80 81 #ifndef lint 82 extern int reference_tracking_enable; 83 extern boolean_t zfs_recover; 84 extern uint64_t zfs_arc_max, zfs_arc_meta_limit; 85 extern int zfs_vdev_async_read_max_active; 86 #else 87 int reference_tracking_enable; 88 boolean_t zfs_recover; 89 uint64_t zfs_arc_max, zfs_arc_meta_limit; 90 int zfs_vdev_async_read_max_active; 91 #endif 92 93 const char cmdname[] = "zdb"; 94 uint8_t dump_opt[256]; 95 96 typedef void object_viewer_t(objset_t *, uint64_t, void *data, size_t size); 97 98 extern void dump_intent_log(zilog_t *); 99 uint64_t *zopt_object = NULL; 100 int zopt_objects = 0; 101 libzfs_handle_t *g_zfs; 102 uint64_t max_inflight = 1000; 103 104 static void snprintf_blkptr_compact(char *, size_t, const blkptr_t *); 105 106 /* 107 * These libumem hooks provide a reasonable set of defaults for the allocator's 108 * debugging facilities. 109 */ 110 const char * 111 _umem_debug_init() 112 { 113 return ("default,verbose"); /* $UMEM_DEBUG setting */ 114 } 115 116 const char * 117 _umem_logging_init(void) 118 { 119 return ("fail,contents"); /* $UMEM_LOGGING setting */ 120 } 121 122 static void 123 usage(void) 124 { 125 (void) fprintf(stderr, 126 "Usage:\t%s [-AbcdDFGhiLMPsvX] [-e [-V] [-p <path> ...]] " 127 "[-I <inflight I/Os>]\n" 128 "\t\t[-o <var>=<value>]... [-t <txg>] [-U <cache>] [-x <dumpdir>]\n" 129 "\t\t[<poolname> [<object> ...]]\n" 130 "\t%s [-AdiPv] [-e [-V] [-p <path> ...]] [-U <cache>] <dataset> " 131 "[<object> ...]\n" 132 "\t%s -C [-A] [-U <cache>]\n" 133 "\t%s -l [-Aqu] <device>\n" 134 "\t%s -m [-AFLPX] [-e [-V] [-p <path> ...]] [-t <txg>] " 135 "[-U <cache>]\n\t\t<poolname> [<vdev> [<metaslab> ...]]\n" 136 "\t%s -O <dataset> <path>\n" 137 "\t%s -R [-A] [-e [-V] [-p <path> ...]] [-U <cache>]\n" 138 "\t\t<poolname> <vdev>:<offset>:<size>[:<flags>]\n" 139 "\t%s -E [-A] word0:word1:...:word15\n" 140 "\t%s -S [-AP] [-e [-V] [-p <path> ...]] [-U <cache>] " 141 "<poolname>\n\n", 142 cmdname, cmdname, cmdname, cmdname, cmdname, cmdname, cmdname, 143 cmdname, cmdname); 144 145 (void) fprintf(stderr, " Dataset name must include at least one " 146 "separator character '/' or '@'\n"); 147 (void) fprintf(stderr, " If dataset name is specified, only that " 148 "dataset is dumped\n"); 149 (void) fprintf(stderr, " If object numbers are specified, only " 150 "those objects are dumped\n\n"); 151 (void) fprintf(stderr, " Options to control amount of output:\n"); 152 (void) fprintf(stderr, " -b block statistics\n"); 153 (void) fprintf(stderr, " -c checksum all metadata (twice for " 154 "all data) blocks\n"); 155 (void) fprintf(stderr, " -C config (or cachefile if alone)\n"); 156 (void) fprintf(stderr, " -d dataset(s)\n"); 157 (void) fprintf(stderr, " -D dedup statistics\n"); 158 (void) fprintf(stderr, " -E decode and display block from an " 159 "embedded block pointer\n"); 160 (void) fprintf(stderr, " -h pool history\n"); 161 (void) fprintf(stderr, " -i intent logs\n"); 162 (void) fprintf(stderr, " -l read label contents\n"); 163 (void) fprintf(stderr, " -L disable leak tracking (do not " 164 "load spacemaps)\n"); 165 (void) fprintf(stderr, " -m metaslabs\n"); 166 (void) fprintf(stderr, " -M metaslab groups\n"); 167 (void) fprintf(stderr, " -O perform object lookups by path\n"); 168 (void) fprintf(stderr, " -R read and display block from a " 169 "device\n"); 170 (void) fprintf(stderr, " -s report stats on zdb's I/O\n"); 171 (void) fprintf(stderr, " -S simulate dedup to measure effect\n"); 172 (void) fprintf(stderr, " -v verbose (applies to all " 173 "others)\n\n"); 174 (void) fprintf(stderr, " Below options are intended for use " 175 "with other options:\n"); 176 (void) fprintf(stderr, " -A ignore assertions (-A), enable " 177 "panic recovery (-AA) or both (-AAA)\n"); 178 (void) fprintf(stderr, " -e pool is exported/destroyed/" 179 "has altroot/not in a cachefile\n"); 180 (void) fprintf(stderr, " -F attempt automatic rewind within " 181 "safe range of transaction groups\n"); 182 (void) fprintf(stderr, " -G dump zfs_dbgmsg buffer before " 183 "exiting\n"); 184 (void) fprintf(stderr, " -I <number of inflight I/Os> -- " 185 "specify the maximum number of " 186 "checksumming I/Os [default is 200]\n"); 187 (void) fprintf(stderr, " -o <variable>=<value> set global " 188 "variable to an unsigned 32-bit integer value\n"); 189 (void) fprintf(stderr, " -p <path> -- use one or more with " 190 "-e to specify path to vdev dir\n"); 191 (void) fprintf(stderr, " -P print numbers in parseable form\n"); 192 (void) fprintf(stderr, " -q don't print label contents\n"); 193 (void) fprintf(stderr, " -t <txg> -- highest txg to use when " 194 "searching for uberblocks\n"); 195 (void) fprintf(stderr, " -u uberblock\n"); 196 (void) fprintf(stderr, " -U <cachefile_path> -- use alternate " 197 "cachefile\n"); 198 (void) fprintf(stderr, " -V do verbatim import\n"); 199 (void) fprintf(stderr, " -x <dumpdir> -- " 200 "dump all read blocks into specified directory\n"); 201 (void) fprintf(stderr, " -X attempt extreme rewind (does not " 202 "work with dataset)\n\n"); 203 (void) fprintf(stderr, "Specify an option more than once (e.g. -bb) " 204 "to make only that option verbose\n"); 205 (void) fprintf(stderr, "Default is to dump everything non-verbosely\n"); 206 exit(1); 207 } 208 209 static void 210 dump_debug_buffer() 211 { 212 if (dump_opt['G']) { 213 (void) printf("\n"); 214 zfs_dbgmsg_print("zdb"); 215 } 216 } 217 218 /* 219 * Called for usage errors that are discovered after a call to spa_open(), 220 * dmu_bonus_hold(), or pool_match(). abort() is called for other errors. 221 */ 222 223 static void 224 fatal(const char *fmt, ...) 225 { 226 va_list ap; 227 228 va_start(ap, fmt); 229 (void) fprintf(stderr, "%s: ", cmdname); 230 (void) vfprintf(stderr, fmt, ap); 231 va_end(ap); 232 (void) fprintf(stderr, "\n"); 233 234 dump_debug_buffer(); 235 236 exit(1); 237 } 238 239 /* ARGSUSED */ 240 static void 241 dump_packed_nvlist(objset_t *os, uint64_t object, void *data, size_t size) 242 { 243 nvlist_t *nv; 244 size_t nvsize = *(uint64_t *)data; 245 char *packed = umem_alloc(nvsize, UMEM_NOFAIL); 246 247 VERIFY(0 == dmu_read(os, object, 0, nvsize, packed, DMU_READ_PREFETCH)); 248 249 VERIFY(nvlist_unpack(packed, nvsize, &nv, 0) == 0); 250 251 umem_free(packed, nvsize); 252 253 dump_nvlist(nv, 8); 254 255 nvlist_free(nv); 256 } 257 258 /* ARGSUSED */ 259 static void 260 dump_history_offsets(objset_t *os, uint64_t object, void *data, size_t size) 261 { 262 spa_history_phys_t *shp = data; 263 264 if (shp == NULL) 265 return; 266 267 (void) printf("\t\tpool_create_len = %llu\n", 268 (u_longlong_t)shp->sh_pool_create_len); 269 (void) printf("\t\tphys_max_off = %llu\n", 270 (u_longlong_t)shp->sh_phys_max_off); 271 (void) printf("\t\tbof = %llu\n", 272 (u_longlong_t)shp->sh_bof); 273 (void) printf("\t\teof = %llu\n", 274 (u_longlong_t)shp->sh_eof); 275 (void) printf("\t\trecords_lost = %llu\n", 276 (u_longlong_t)shp->sh_records_lost); 277 } 278 279 static void 280 zdb_nicenum(uint64_t num, char *buf, size_t buflen) 281 { 282 if (dump_opt['P']) 283 (void) snprintf(buf, buflen, "%llu", (longlong_t)num); 284 else 285 nicenum(num, buf, sizeof (buf)); 286 } 287 288 const char histo_stars[] = "****************************************"; 289 const int histo_width = sizeof (histo_stars) - 1; 290 291 static void 292 dump_histogram(const uint64_t *histo, int size, int offset) 293 { 294 int i; 295 int minidx = size - 1; 296 int maxidx = 0; 297 uint64_t max = 0; 298 299 for (i = 0; i < size; i++) { 300 if (histo[i] > max) 301 max = histo[i]; 302 if (histo[i] > 0 && i > maxidx) 303 maxidx = i; 304 if (histo[i] > 0 && i < minidx) 305 minidx = i; 306 } 307 308 if (max < histo_width) 309 max = histo_width; 310 311 for (i = minidx; i <= maxidx; i++) { 312 (void) printf("\t\t\t%3u: %6llu %s\n", 313 i + offset, (u_longlong_t)histo[i], 314 &histo_stars[(max - histo[i]) * histo_width / max]); 315 } 316 } 317 318 static void 319 dump_zap_stats(objset_t *os, uint64_t object) 320 { 321 int error; 322 zap_stats_t zs; 323 324 error = zap_get_stats(os, object, &zs); 325 if (error) 326 return; 327 328 if (zs.zs_ptrtbl_len == 0) { 329 ASSERT(zs.zs_num_blocks == 1); 330 (void) printf("\tmicrozap: %llu bytes, %llu entries\n", 331 (u_longlong_t)zs.zs_blocksize, 332 (u_longlong_t)zs.zs_num_entries); 333 return; 334 } 335 336 (void) printf("\tFat ZAP stats:\n"); 337 338 (void) printf("\t\tPointer table:\n"); 339 (void) printf("\t\t\t%llu elements\n", 340 (u_longlong_t)zs.zs_ptrtbl_len); 341 (void) printf("\t\t\tzt_blk: %llu\n", 342 (u_longlong_t)zs.zs_ptrtbl_zt_blk); 343 (void) printf("\t\t\tzt_numblks: %llu\n", 344 (u_longlong_t)zs.zs_ptrtbl_zt_numblks); 345 (void) printf("\t\t\tzt_shift: %llu\n", 346 (u_longlong_t)zs.zs_ptrtbl_zt_shift); 347 (void) printf("\t\t\tzt_blks_copied: %llu\n", 348 (u_longlong_t)zs.zs_ptrtbl_blks_copied); 349 (void) printf("\t\t\tzt_nextblk: %llu\n", 350 (u_longlong_t)zs.zs_ptrtbl_nextblk); 351 352 (void) printf("\t\tZAP entries: %llu\n", 353 (u_longlong_t)zs.zs_num_entries); 354 (void) printf("\t\tLeaf blocks: %llu\n", 355 (u_longlong_t)zs.zs_num_leafs); 356 (void) printf("\t\tTotal blocks: %llu\n", 357 (u_longlong_t)zs.zs_num_blocks); 358 (void) printf("\t\tzap_block_type: 0x%llx\n", 359 (u_longlong_t)zs.zs_block_type); 360 (void) printf("\t\tzap_magic: 0x%llx\n", 361 (u_longlong_t)zs.zs_magic); 362 (void) printf("\t\tzap_salt: 0x%llx\n", 363 (u_longlong_t)zs.zs_salt); 364 365 (void) printf("\t\tLeafs with 2^n pointers:\n"); 366 dump_histogram(zs.zs_leafs_with_2n_pointers, ZAP_HISTOGRAM_SIZE, 0); 367 368 (void) printf("\t\tBlocks with n*5 entries:\n"); 369 dump_histogram(zs.zs_blocks_with_n5_entries, ZAP_HISTOGRAM_SIZE, 0); 370 371 (void) printf("\t\tBlocks n/10 full:\n"); 372 dump_histogram(zs.zs_blocks_n_tenths_full, ZAP_HISTOGRAM_SIZE, 0); 373 374 (void) printf("\t\tEntries with n chunks:\n"); 375 dump_histogram(zs.zs_entries_using_n_chunks, ZAP_HISTOGRAM_SIZE, 0); 376 377 (void) printf("\t\tBuckets with n entries:\n"); 378 dump_histogram(zs.zs_buckets_with_n_entries, ZAP_HISTOGRAM_SIZE, 0); 379 } 380 381 /*ARGSUSED*/ 382 static void 383 dump_none(objset_t *os, uint64_t object, void *data, size_t size) 384 { 385 } 386 387 /*ARGSUSED*/ 388 static void 389 dump_unknown(objset_t *os, uint64_t object, void *data, size_t size) 390 { 391 (void) printf("\tUNKNOWN OBJECT TYPE\n"); 392 } 393 394 /*ARGSUSED*/ 395 void 396 dump_uint8(objset_t *os, uint64_t object, void *data, size_t size) 397 { 398 } 399 400 /*ARGSUSED*/ 401 static void 402 dump_uint64(objset_t *os, uint64_t object, void *data, size_t size) 403 { 404 } 405 406 /*ARGSUSED*/ 407 static void 408 dump_zap(objset_t *os, uint64_t object, void *data, size_t size) 409 { 410 zap_cursor_t zc; 411 zap_attribute_t attr; 412 void *prop; 413 int i; 414 415 dump_zap_stats(os, object); 416 (void) printf("\n"); 417 418 for (zap_cursor_init(&zc, os, object); 419 zap_cursor_retrieve(&zc, &attr) == 0; 420 zap_cursor_advance(&zc)) { 421 (void) printf("\t\t%s = ", attr.za_name); 422 if (attr.za_num_integers == 0) { 423 (void) printf("\n"); 424 continue; 425 } 426 prop = umem_zalloc(attr.za_num_integers * 427 attr.za_integer_length, UMEM_NOFAIL); 428 (void) zap_lookup(os, object, attr.za_name, 429 attr.za_integer_length, attr.za_num_integers, prop); 430 if (attr.za_integer_length == 1) { 431 (void) printf("%s", (char *)prop); 432 } else { 433 for (i = 0; i < attr.za_num_integers; i++) { 434 switch (attr.za_integer_length) { 435 case 2: 436 (void) printf("%u ", 437 ((uint16_t *)prop)[i]); 438 break; 439 case 4: 440 (void) printf("%u ", 441 ((uint32_t *)prop)[i]); 442 break; 443 case 8: 444 (void) printf("%lld ", 445 (u_longlong_t)((int64_t *)prop)[i]); 446 break; 447 } 448 } 449 } 450 (void) printf("\n"); 451 umem_free(prop, attr.za_num_integers * attr.za_integer_length); 452 } 453 zap_cursor_fini(&zc); 454 } 455 456 static void 457 dump_bpobj(objset_t *os, uint64_t object, void *data, size_t size) 458 { 459 bpobj_phys_t *bpop = data; 460 char bytes[32], comp[32], uncomp[32]; 461 462 /* make sure the output won't get truncated */ 463 CTASSERT(sizeof (bytes) >= NN_NUMBUF_SZ); 464 CTASSERT(sizeof (comp) >= NN_NUMBUF_SZ); 465 CTASSERT(sizeof (uncomp) >= NN_NUMBUF_SZ); 466 467 if (bpop == NULL) 468 return; 469 470 zdb_nicenum(bpop->bpo_bytes, bytes, sizeof (bytes)); 471 zdb_nicenum(bpop->bpo_comp, comp, sizeof (comp)); 472 zdb_nicenum(bpop->bpo_uncomp, uncomp, sizeof (uncomp)); 473 474 (void) printf("\t\tnum_blkptrs = %llu\n", 475 (u_longlong_t)bpop->bpo_num_blkptrs); 476 (void) printf("\t\tbytes = %s\n", bytes); 477 if (size >= BPOBJ_SIZE_V1) { 478 (void) printf("\t\tcomp = %s\n", comp); 479 (void) printf("\t\tuncomp = %s\n", uncomp); 480 } 481 if (size >= sizeof (*bpop)) { 482 (void) printf("\t\tsubobjs = %llu\n", 483 (u_longlong_t)bpop->bpo_subobjs); 484 (void) printf("\t\tnum_subobjs = %llu\n", 485 (u_longlong_t)bpop->bpo_num_subobjs); 486 } 487 488 if (dump_opt['d'] < 5) 489 return; 490 491 for (uint64_t i = 0; i < bpop->bpo_num_blkptrs; i++) { 492 char blkbuf[BP_SPRINTF_LEN]; 493 blkptr_t bp; 494 495 int err = dmu_read(os, object, 496 i * sizeof (bp), sizeof (bp), &bp, 0); 497 if (err != 0) { 498 (void) printf("got error %u from dmu_read\n", err); 499 break; 500 } 501 snprintf_blkptr_compact(blkbuf, sizeof (blkbuf), &bp); 502 (void) printf("\t%s\n", blkbuf); 503 } 504 } 505 506 /* ARGSUSED */ 507 static void 508 dump_bpobj_subobjs(objset_t *os, uint64_t object, void *data, size_t size) 509 { 510 dmu_object_info_t doi; 511 512 VERIFY0(dmu_object_info(os, object, &doi)); 513 uint64_t *subobjs = kmem_alloc(doi.doi_max_offset, KM_SLEEP); 514 515 int err = dmu_read(os, object, 0, doi.doi_max_offset, subobjs, 0); 516 if (err != 0) { 517 (void) printf("got error %u from dmu_read\n", err); 518 kmem_free(subobjs, doi.doi_max_offset); 519 return; 520 } 521 522 int64_t last_nonzero = -1; 523 for (uint64_t i = 0; i < doi.doi_max_offset / 8; i++) { 524 if (subobjs[i] != 0) 525 last_nonzero = i; 526 } 527 528 for (int64_t i = 0; i <= last_nonzero; i++) { 529 (void) printf("\t%llu\n", (longlong_t)subobjs[i]); 530 } 531 kmem_free(subobjs, doi.doi_max_offset); 532 } 533 534 /*ARGSUSED*/ 535 static void 536 dump_ddt_zap(objset_t *os, uint64_t object, void *data, size_t size) 537 { 538 dump_zap_stats(os, object); 539 /* contents are printed elsewhere, properly decoded */ 540 } 541 542 /*ARGSUSED*/ 543 static void 544 dump_sa_attrs(objset_t *os, uint64_t object, void *data, size_t size) 545 { 546 zap_cursor_t zc; 547 zap_attribute_t attr; 548 549 dump_zap_stats(os, object); 550 (void) printf("\n"); 551 552 for (zap_cursor_init(&zc, os, object); 553 zap_cursor_retrieve(&zc, &attr) == 0; 554 zap_cursor_advance(&zc)) { 555 (void) printf("\t\t%s = ", attr.za_name); 556 if (attr.za_num_integers == 0) { 557 (void) printf("\n"); 558 continue; 559 } 560 (void) printf(" %llx : [%d:%d:%d]\n", 561 (u_longlong_t)attr.za_first_integer, 562 (int)ATTR_LENGTH(attr.za_first_integer), 563 (int)ATTR_BSWAP(attr.za_first_integer), 564 (int)ATTR_NUM(attr.za_first_integer)); 565 } 566 zap_cursor_fini(&zc); 567 } 568 569 /*ARGSUSED*/ 570 static void 571 dump_sa_layouts(objset_t *os, uint64_t object, void *data, size_t size) 572 { 573 zap_cursor_t zc; 574 zap_attribute_t attr; 575 uint16_t *layout_attrs; 576 int i; 577 578 dump_zap_stats(os, object); 579 (void) printf("\n"); 580 581 for (zap_cursor_init(&zc, os, object); 582 zap_cursor_retrieve(&zc, &attr) == 0; 583 zap_cursor_advance(&zc)) { 584 (void) printf("\t\t%s = [", attr.za_name); 585 if (attr.za_num_integers == 0) { 586 (void) printf("\n"); 587 continue; 588 } 589 590 VERIFY(attr.za_integer_length == 2); 591 layout_attrs = umem_zalloc(attr.za_num_integers * 592 attr.za_integer_length, UMEM_NOFAIL); 593 594 VERIFY(zap_lookup(os, object, attr.za_name, 595 attr.za_integer_length, 596 attr.za_num_integers, layout_attrs) == 0); 597 598 for (i = 0; i != attr.za_num_integers; i++) 599 (void) printf(" %d ", (int)layout_attrs[i]); 600 (void) printf("]\n"); 601 umem_free(layout_attrs, 602 attr.za_num_integers * attr.za_integer_length); 603 } 604 zap_cursor_fini(&zc); 605 } 606 607 /*ARGSUSED*/ 608 static void 609 dump_zpldir(objset_t *os, uint64_t object, void *data, size_t size) 610 { 611 zap_cursor_t zc; 612 zap_attribute_t attr; 613 const char *typenames[] = { 614 /* 0 */ "not specified", 615 /* 1 */ "FIFO", 616 /* 2 */ "Character Device", 617 /* 3 */ "3 (invalid)", 618 /* 4 */ "Directory", 619 /* 5 */ "5 (invalid)", 620 /* 6 */ "Block Device", 621 /* 7 */ "7 (invalid)", 622 /* 8 */ "Regular File", 623 /* 9 */ "9 (invalid)", 624 /* 10 */ "Symbolic Link", 625 /* 11 */ "11 (invalid)", 626 /* 12 */ "Socket", 627 /* 13 */ "Door", 628 /* 14 */ "Event Port", 629 /* 15 */ "15 (invalid)", 630 }; 631 632 dump_zap_stats(os, object); 633 (void) printf("\n"); 634 635 for (zap_cursor_init(&zc, os, object); 636 zap_cursor_retrieve(&zc, &attr) == 0; 637 zap_cursor_advance(&zc)) { 638 (void) printf("\t\t%s = %lld (type: %s)\n", 639 attr.za_name, ZFS_DIRENT_OBJ(attr.za_first_integer), 640 typenames[ZFS_DIRENT_TYPE(attr.za_first_integer)]); 641 } 642 zap_cursor_fini(&zc); 643 } 644 645 int 646 get_dtl_refcount(vdev_t *vd) 647 { 648 int refcount = 0; 649 650 if (vd->vdev_ops->vdev_op_leaf) { 651 space_map_t *sm = vd->vdev_dtl_sm; 652 653 if (sm != NULL && 654 sm->sm_dbuf->db_size == sizeof (space_map_phys_t)) 655 return (1); 656 return (0); 657 } 658 659 for (int c = 0; c < vd->vdev_children; c++) 660 refcount += get_dtl_refcount(vd->vdev_child[c]); 661 return (refcount); 662 } 663 664 int 665 get_metaslab_refcount(vdev_t *vd) 666 { 667 int refcount = 0; 668 669 if (vd->vdev_top == vd && !vd->vdev_removing) { 670 for (int m = 0; m < vd->vdev_ms_count; m++) { 671 space_map_t *sm = vd->vdev_ms[m]->ms_sm; 672 673 if (sm != NULL && 674 sm->sm_dbuf->db_size == sizeof (space_map_phys_t)) 675 refcount++; 676 } 677 } 678 for (int c = 0; c < vd->vdev_children; c++) 679 refcount += get_metaslab_refcount(vd->vdev_child[c]); 680 681 return (refcount); 682 } 683 684 static int 685 verify_spacemap_refcounts(spa_t *spa) 686 { 687 uint64_t expected_refcount = 0; 688 uint64_t actual_refcount; 689 690 (void) feature_get_refcount(spa, 691 &spa_feature_table[SPA_FEATURE_SPACEMAP_HISTOGRAM], 692 &expected_refcount); 693 actual_refcount = get_dtl_refcount(spa->spa_root_vdev); 694 actual_refcount += get_metaslab_refcount(spa->spa_root_vdev); 695 696 if (expected_refcount != actual_refcount) { 697 (void) printf("space map refcount mismatch: expected %lld != " 698 "actual %lld\n", 699 (longlong_t)expected_refcount, 700 (longlong_t)actual_refcount); 701 return (2); 702 } 703 return (0); 704 } 705 706 static void 707 dump_spacemap(objset_t *os, space_map_t *sm) 708 { 709 uint64_t alloc, offset, entry; 710 char *ddata[] = { "ALLOC", "FREE", "CONDENSE", "INVALID", 711 "INVALID", "INVALID", "INVALID", "INVALID" }; 712 713 if (sm == NULL) 714 return; 715 716 /* 717 * Print out the freelist entries in both encoded and decoded form. 718 */ 719 alloc = 0; 720 for (offset = 0; offset < space_map_length(sm); 721 offset += sizeof (entry)) { 722 uint8_t mapshift = sm->sm_shift; 723 724 VERIFY0(dmu_read(os, space_map_object(sm), offset, 725 sizeof (entry), &entry, DMU_READ_PREFETCH)); 726 if (SM_DEBUG_DECODE(entry)) { 727 728 (void) printf("\t [%6llu] %s: txg %llu, pass %llu\n", 729 (u_longlong_t)(offset / sizeof (entry)), 730 ddata[SM_DEBUG_ACTION_DECODE(entry)], 731 (u_longlong_t)SM_DEBUG_TXG_DECODE(entry), 732 (u_longlong_t)SM_DEBUG_SYNCPASS_DECODE(entry)); 733 } else { 734 (void) printf("\t [%6llu] %c range:" 735 " %010llx-%010llx size: %06llx\n", 736 (u_longlong_t)(offset / sizeof (entry)), 737 SM_TYPE_DECODE(entry) == SM_ALLOC ? 'A' : 'F', 738 (u_longlong_t)((SM_OFFSET_DECODE(entry) << 739 mapshift) + sm->sm_start), 740 (u_longlong_t)((SM_OFFSET_DECODE(entry) << 741 mapshift) + sm->sm_start + 742 (SM_RUN_DECODE(entry) << mapshift)), 743 (u_longlong_t)(SM_RUN_DECODE(entry) << mapshift)); 744 if (SM_TYPE_DECODE(entry) == SM_ALLOC) 745 alloc += SM_RUN_DECODE(entry) << mapshift; 746 else 747 alloc -= SM_RUN_DECODE(entry) << mapshift; 748 } 749 } 750 if (alloc != space_map_allocated(sm)) { 751 (void) printf("space_map_object alloc (%llu) INCONSISTENT " 752 "with space map summary (%llu)\n", 753 (u_longlong_t)space_map_allocated(sm), (u_longlong_t)alloc); 754 } 755 } 756 757 static void 758 dump_metaslab_stats(metaslab_t *msp) 759 { 760 char maxbuf[32]; 761 range_tree_t *rt = msp->ms_tree; 762 avl_tree_t *t = &msp->ms_size_tree; 763 int free_pct = range_tree_space(rt) * 100 / msp->ms_size; 764 765 /* max sure nicenum has enough space */ 766 CTASSERT(sizeof (maxbuf) >= NN_NUMBUF_SZ); 767 768 zdb_nicenum(metaslab_block_maxsize(msp), maxbuf, sizeof (maxbuf)); 769 770 (void) printf("\t %25s %10lu %7s %6s %4s %4d%%\n", 771 "segments", avl_numnodes(t), "maxsize", maxbuf, 772 "freepct", free_pct); 773 (void) printf("\tIn-memory histogram:\n"); 774 dump_histogram(rt->rt_histogram, RANGE_TREE_HISTOGRAM_SIZE, 0); 775 } 776 777 static void 778 dump_metaslab(metaslab_t *msp) 779 { 780 vdev_t *vd = msp->ms_group->mg_vd; 781 spa_t *spa = vd->vdev_spa; 782 space_map_t *sm = msp->ms_sm; 783 char freebuf[32]; 784 785 zdb_nicenum(msp->ms_size - space_map_allocated(sm), freebuf, 786 sizeof (freebuf)); 787 788 (void) printf( 789 "\tmetaslab %6llu offset %12llx spacemap %6llu free %5s\n", 790 (u_longlong_t)msp->ms_id, (u_longlong_t)msp->ms_start, 791 (u_longlong_t)space_map_object(sm), freebuf); 792 793 if (dump_opt['m'] > 2 && !dump_opt['L']) { 794 mutex_enter(&msp->ms_lock); 795 metaslab_load_wait(msp); 796 if (!msp->ms_loaded) { 797 VERIFY0(metaslab_load(msp)); 798 range_tree_stat_verify(msp->ms_tree); 799 } 800 dump_metaslab_stats(msp); 801 metaslab_unload(msp); 802 mutex_exit(&msp->ms_lock); 803 } 804 805 if (dump_opt['m'] > 1 && sm != NULL && 806 spa_feature_is_active(spa, SPA_FEATURE_SPACEMAP_HISTOGRAM)) { 807 /* 808 * The space map histogram represents free space in chunks 809 * of sm_shift (i.e. bucket 0 refers to 2^sm_shift). 810 */ 811 (void) printf("\tOn-disk histogram:\t\tfragmentation %llu\n", 812 (u_longlong_t)msp->ms_fragmentation); 813 dump_histogram(sm->sm_phys->smp_histogram, 814 SPACE_MAP_HISTOGRAM_SIZE, sm->sm_shift); 815 } 816 817 if (dump_opt['d'] > 5 || dump_opt['m'] > 3) { 818 ASSERT(msp->ms_size == (1ULL << vd->vdev_ms_shift)); 819 820 mutex_enter(&msp->ms_lock); 821 dump_spacemap(spa->spa_meta_objset, msp->ms_sm); 822 mutex_exit(&msp->ms_lock); 823 } 824 } 825 826 static void 827 print_vdev_metaslab_header(vdev_t *vd) 828 { 829 (void) printf("\tvdev %10llu\n\t%-10s%5llu %-19s %-15s %-10s\n", 830 (u_longlong_t)vd->vdev_id, 831 "metaslabs", (u_longlong_t)vd->vdev_ms_count, 832 "offset", "spacemap", "free"); 833 (void) printf("\t%15s %19s %15s %10s\n", 834 "---------------", "-------------------", 835 "---------------", "-------------"); 836 } 837 838 static void 839 dump_metaslab_groups(spa_t *spa) 840 { 841 vdev_t *rvd = spa->spa_root_vdev; 842 metaslab_class_t *mc = spa_normal_class(spa); 843 uint64_t fragmentation; 844 845 metaslab_class_histogram_verify(mc); 846 847 for (int c = 0; c < rvd->vdev_children; c++) { 848 vdev_t *tvd = rvd->vdev_child[c]; 849 metaslab_group_t *mg = tvd->vdev_mg; 850 851 if (mg->mg_class != mc) 852 continue; 853 854 metaslab_group_histogram_verify(mg); 855 mg->mg_fragmentation = metaslab_group_fragmentation(mg); 856 857 (void) printf("\tvdev %10llu\t\tmetaslabs%5llu\t\t" 858 "fragmentation", 859 (u_longlong_t)tvd->vdev_id, 860 (u_longlong_t)tvd->vdev_ms_count); 861 if (mg->mg_fragmentation == ZFS_FRAG_INVALID) { 862 (void) printf("%3s\n", "-"); 863 } else { 864 (void) printf("%3llu%%\n", 865 (u_longlong_t)mg->mg_fragmentation); 866 } 867 dump_histogram(mg->mg_histogram, RANGE_TREE_HISTOGRAM_SIZE, 0); 868 } 869 870 (void) printf("\tpool %s\tfragmentation", spa_name(spa)); 871 fragmentation = metaslab_class_fragmentation(mc); 872 if (fragmentation == ZFS_FRAG_INVALID) 873 (void) printf("\t%3s\n", "-"); 874 else 875 (void) printf("\t%3llu%%\n", (u_longlong_t)fragmentation); 876 dump_histogram(mc->mc_histogram, RANGE_TREE_HISTOGRAM_SIZE, 0); 877 } 878 879 static void 880 dump_metaslabs(spa_t *spa) 881 { 882 vdev_t *vd, *rvd = spa->spa_root_vdev; 883 uint64_t m, c = 0, children = rvd->vdev_children; 884 885 (void) printf("\nMetaslabs:\n"); 886 887 if (!dump_opt['d'] && zopt_objects > 0) { 888 c = zopt_object[0]; 889 890 if (c >= children) 891 (void) fatal("bad vdev id: %llu", (u_longlong_t)c); 892 893 if (zopt_objects > 1) { 894 vd = rvd->vdev_child[c]; 895 print_vdev_metaslab_header(vd); 896 897 for (m = 1; m < zopt_objects; m++) { 898 if (zopt_object[m] < vd->vdev_ms_count) 899 dump_metaslab( 900 vd->vdev_ms[zopt_object[m]]); 901 else 902 (void) fprintf(stderr, "bad metaslab " 903 "number %llu\n", 904 (u_longlong_t)zopt_object[m]); 905 } 906 (void) printf("\n"); 907 return; 908 } 909 children = c + 1; 910 } 911 for (; c < children; c++) { 912 vd = rvd->vdev_child[c]; 913 print_vdev_metaslab_header(vd); 914 915 for (m = 0; m < vd->vdev_ms_count; m++) 916 dump_metaslab(vd->vdev_ms[m]); 917 (void) printf("\n"); 918 } 919 } 920 921 static void 922 dump_dde(const ddt_t *ddt, const ddt_entry_t *dde, uint64_t index) 923 { 924 const ddt_phys_t *ddp = dde->dde_phys; 925 const ddt_key_t *ddk = &dde->dde_key; 926 char *types[4] = { "ditto", "single", "double", "triple" }; 927 char blkbuf[BP_SPRINTF_LEN]; 928 blkptr_t blk; 929 930 for (int p = 0; p < DDT_PHYS_TYPES; p++, ddp++) { 931 if (ddp->ddp_phys_birth == 0) 932 continue; 933 ddt_bp_create(ddt->ddt_checksum, ddk, ddp, &blk); 934 snprintf_blkptr(blkbuf, sizeof (blkbuf), &blk); 935 (void) printf("index %llx refcnt %llu %s %s\n", 936 (u_longlong_t)index, (u_longlong_t)ddp->ddp_refcnt, 937 types[p], blkbuf); 938 } 939 } 940 941 static void 942 dump_dedup_ratio(const ddt_stat_t *dds) 943 { 944 double rL, rP, rD, D, dedup, compress, copies; 945 946 if (dds->dds_blocks == 0) 947 return; 948 949 rL = (double)dds->dds_ref_lsize; 950 rP = (double)dds->dds_ref_psize; 951 rD = (double)dds->dds_ref_dsize; 952 D = (double)dds->dds_dsize; 953 954 dedup = rD / D; 955 compress = rL / rP; 956 copies = rD / rP; 957 958 (void) printf("dedup = %.2f, compress = %.2f, copies = %.2f, " 959 "dedup * compress / copies = %.2f\n\n", 960 dedup, compress, copies, dedup * compress / copies); 961 } 962 963 static void 964 dump_ddt(ddt_t *ddt, enum ddt_type type, enum ddt_class class) 965 { 966 char name[DDT_NAMELEN]; 967 ddt_entry_t dde; 968 uint64_t walk = 0; 969 dmu_object_info_t doi; 970 uint64_t count, dspace, mspace; 971 int error; 972 973 error = ddt_object_info(ddt, type, class, &doi); 974 975 if (error == ENOENT) 976 return; 977 ASSERT(error == 0); 978 979 if ((count = ddt_object_count(ddt, type, class)) == 0) 980 return; 981 982 dspace = doi.doi_physical_blocks_512 << 9; 983 mspace = doi.doi_fill_count * doi.doi_data_block_size; 984 985 ddt_object_name(ddt, type, class, name); 986 987 (void) printf("%s: %llu entries, size %llu on disk, %llu in core\n", 988 name, 989 (u_longlong_t)count, 990 (u_longlong_t)(dspace / count), 991 (u_longlong_t)(mspace / count)); 992 993 if (dump_opt['D'] < 3) 994 return; 995 996 zpool_dump_ddt(NULL, &ddt->ddt_histogram[type][class]); 997 998 if (dump_opt['D'] < 4) 999 return; 1000 1001 if (dump_opt['D'] < 5 && class == DDT_CLASS_UNIQUE) 1002 return; 1003 1004 (void) printf("%s contents:\n\n", name); 1005 1006 while ((error = ddt_object_walk(ddt, type, class, &walk, &dde)) == 0) 1007 dump_dde(ddt, &dde, walk); 1008 1009 ASSERT(error == ENOENT); 1010 1011 (void) printf("\n"); 1012 } 1013 1014 static void 1015 dump_all_ddts(spa_t *spa) 1016 { 1017 ddt_histogram_t ddh_total = { 0 }; 1018 ddt_stat_t dds_total = { 0 }; 1019 1020 for (enum zio_checksum c = 0; c < ZIO_CHECKSUM_FUNCTIONS; c++) { 1021 ddt_t *ddt = spa->spa_ddt[c]; 1022 for (enum ddt_type type = 0; type < DDT_TYPES; type++) { 1023 for (enum ddt_class class = 0; class < DDT_CLASSES; 1024 class++) { 1025 dump_ddt(ddt, type, class); 1026 } 1027 } 1028 } 1029 1030 ddt_get_dedup_stats(spa, &dds_total); 1031 1032 if (dds_total.dds_blocks == 0) { 1033 (void) printf("All DDTs are empty\n"); 1034 return; 1035 } 1036 1037 (void) printf("\n"); 1038 1039 if (dump_opt['D'] > 1) { 1040 (void) printf("DDT histogram (aggregated over all DDTs):\n"); 1041 ddt_get_dedup_histogram(spa, &ddh_total); 1042 zpool_dump_ddt(&dds_total, &ddh_total); 1043 } 1044 1045 dump_dedup_ratio(&dds_total); 1046 } 1047 1048 static void 1049 dump_dtl_seg(void *arg, uint64_t start, uint64_t size) 1050 { 1051 char *prefix = arg; 1052 1053 (void) printf("%s [%llu,%llu) length %llu\n", 1054 prefix, 1055 (u_longlong_t)start, 1056 (u_longlong_t)(start + size), 1057 (u_longlong_t)(size)); 1058 } 1059 1060 static void 1061 dump_dtl(vdev_t *vd, int indent) 1062 { 1063 spa_t *spa = vd->vdev_spa; 1064 boolean_t required; 1065 char *name[DTL_TYPES] = { "missing", "partial", "scrub", "outage" }; 1066 char prefix[256]; 1067 1068 spa_vdev_state_enter(spa, SCL_NONE); 1069 required = vdev_dtl_required(vd); 1070 (void) spa_vdev_state_exit(spa, NULL, 0); 1071 1072 if (indent == 0) 1073 (void) printf("\nDirty time logs:\n\n"); 1074 1075 (void) printf("\t%*s%s [%s]\n", indent, "", 1076 vd->vdev_path ? vd->vdev_path : 1077 vd->vdev_parent ? vd->vdev_ops->vdev_op_type : spa_name(spa), 1078 required ? "DTL-required" : "DTL-expendable"); 1079 1080 for (int t = 0; t < DTL_TYPES; t++) { 1081 range_tree_t *rt = vd->vdev_dtl[t]; 1082 if (range_tree_space(rt) == 0) 1083 continue; 1084 (void) snprintf(prefix, sizeof (prefix), "\t%*s%s", 1085 indent + 2, "", name[t]); 1086 mutex_enter(rt->rt_lock); 1087 range_tree_walk(rt, dump_dtl_seg, prefix); 1088 mutex_exit(rt->rt_lock); 1089 if (dump_opt['d'] > 5 && vd->vdev_children == 0) 1090 dump_spacemap(spa->spa_meta_objset, vd->vdev_dtl_sm); 1091 } 1092 1093 for (int c = 0; c < vd->vdev_children; c++) 1094 dump_dtl(vd->vdev_child[c], indent + 4); 1095 } 1096 1097 static void 1098 dump_history(spa_t *spa) 1099 { 1100 nvlist_t **events = NULL; 1101 uint64_t resid, len, off = 0; 1102 uint_t num = 0; 1103 int error; 1104 time_t tsec; 1105 struct tm t; 1106 char tbuf[30]; 1107 char internalstr[MAXPATHLEN]; 1108 1109 char *buf = umem_alloc(SPA_MAXBLOCKSIZE, UMEM_NOFAIL); 1110 do { 1111 len = SPA_MAXBLOCKSIZE; 1112 1113 if ((error = spa_history_get(spa, &off, &len, buf)) != 0) { 1114 (void) fprintf(stderr, "Unable to read history: " 1115 "error %d\n", error); 1116 umem_free(buf, SPA_MAXBLOCKSIZE); 1117 return; 1118 } 1119 1120 if (zpool_history_unpack(buf, len, &resid, &events, &num) != 0) 1121 break; 1122 1123 off -= resid; 1124 } while (len != 0); 1125 umem_free(buf, SPA_MAXBLOCKSIZE); 1126 1127 (void) printf("\nHistory:\n"); 1128 for (int i = 0; i < num; i++) { 1129 uint64_t time, txg, ievent; 1130 char *cmd, *intstr; 1131 boolean_t printed = B_FALSE; 1132 1133 if (nvlist_lookup_uint64(events[i], ZPOOL_HIST_TIME, 1134 &time) != 0) 1135 goto next; 1136 if (nvlist_lookup_string(events[i], ZPOOL_HIST_CMD, 1137 &cmd) != 0) { 1138 if (nvlist_lookup_uint64(events[i], 1139 ZPOOL_HIST_INT_EVENT, &ievent) != 0) 1140 goto next; 1141 verify(nvlist_lookup_uint64(events[i], 1142 ZPOOL_HIST_TXG, &txg) == 0); 1143 verify(nvlist_lookup_string(events[i], 1144 ZPOOL_HIST_INT_STR, &intstr) == 0); 1145 if (ievent >= ZFS_NUM_LEGACY_HISTORY_EVENTS) 1146 goto next; 1147 1148 (void) snprintf(internalstr, 1149 sizeof (internalstr), 1150 "[internal %s txg:%lld] %s", 1151 zfs_history_event_names[ievent], txg, 1152 intstr); 1153 cmd = internalstr; 1154 } 1155 tsec = time; 1156 (void) localtime_r(&tsec, &t); 1157 (void) strftime(tbuf, sizeof (tbuf), "%F.%T", &t); 1158 (void) printf("%s %s\n", tbuf, cmd); 1159 printed = B_TRUE; 1160 1161 next: 1162 if (dump_opt['h'] > 1) { 1163 if (!printed) 1164 (void) printf("unrecognized record:\n"); 1165 dump_nvlist(events[i], 2); 1166 } 1167 } 1168 } 1169 1170 /*ARGSUSED*/ 1171 static void 1172 dump_dnode(objset_t *os, uint64_t object, void *data, size_t size) 1173 { 1174 } 1175 1176 static uint64_t 1177 blkid2offset(const dnode_phys_t *dnp, const blkptr_t *bp, 1178 const zbookmark_phys_t *zb) 1179 { 1180 if (dnp == NULL) { 1181 ASSERT(zb->zb_level < 0); 1182 if (zb->zb_object == 0) 1183 return (zb->zb_blkid); 1184 return (zb->zb_blkid * BP_GET_LSIZE(bp)); 1185 } 1186 1187 ASSERT(zb->zb_level >= 0); 1188 1189 return ((zb->zb_blkid << 1190 (zb->zb_level * (dnp->dn_indblkshift - SPA_BLKPTRSHIFT))) * 1191 dnp->dn_datablkszsec << SPA_MINBLOCKSHIFT); 1192 } 1193 1194 static void 1195 snprintf_blkptr_compact(char *blkbuf, size_t buflen, const blkptr_t *bp) 1196 { 1197 const dva_t *dva = bp->blk_dva; 1198 int ndvas = dump_opt['d'] > 5 ? BP_GET_NDVAS(bp) : 1; 1199 1200 if (dump_opt['b'] >= 6) { 1201 snprintf_blkptr(blkbuf, buflen, bp); 1202 return; 1203 } 1204 1205 if (BP_IS_EMBEDDED(bp)) { 1206 (void) sprintf(blkbuf, 1207 "EMBEDDED et=%u %llxL/%llxP B=%llu", 1208 (int)BPE_GET_ETYPE(bp), 1209 (u_longlong_t)BPE_GET_LSIZE(bp), 1210 (u_longlong_t)BPE_GET_PSIZE(bp), 1211 (u_longlong_t)bp->blk_birth); 1212 return; 1213 } 1214 1215 blkbuf[0] = '\0'; 1216 for (int i = 0; i < ndvas; i++) 1217 (void) snprintf(blkbuf + strlen(blkbuf), 1218 buflen - strlen(blkbuf), "%llu:%llx:%llx ", 1219 (u_longlong_t)DVA_GET_VDEV(&dva[i]), 1220 (u_longlong_t)DVA_GET_OFFSET(&dva[i]), 1221 (u_longlong_t)DVA_GET_ASIZE(&dva[i])); 1222 1223 if (BP_IS_HOLE(bp)) { 1224 (void) snprintf(blkbuf + strlen(blkbuf), 1225 buflen - strlen(blkbuf), 1226 "%llxL B=%llu", 1227 (u_longlong_t)BP_GET_LSIZE(bp), 1228 (u_longlong_t)bp->blk_birth); 1229 } else { 1230 (void) snprintf(blkbuf + strlen(blkbuf), 1231 buflen - strlen(blkbuf), 1232 "%llxL/%llxP F=%llu B=%llu/%llu", 1233 (u_longlong_t)BP_GET_LSIZE(bp), 1234 (u_longlong_t)BP_GET_PSIZE(bp), 1235 (u_longlong_t)BP_GET_FILL(bp), 1236 (u_longlong_t)bp->blk_birth, 1237 (u_longlong_t)BP_PHYSICAL_BIRTH(bp)); 1238 } 1239 } 1240 1241 static void 1242 print_indirect(blkptr_t *bp, const zbookmark_phys_t *zb, 1243 const dnode_phys_t *dnp) 1244 { 1245 char blkbuf[BP_SPRINTF_LEN]; 1246 int l; 1247 1248 if (!BP_IS_EMBEDDED(bp)) { 1249 ASSERT3U(BP_GET_TYPE(bp), ==, dnp->dn_type); 1250 ASSERT3U(BP_GET_LEVEL(bp), ==, zb->zb_level); 1251 } 1252 1253 (void) printf("%16llx ", (u_longlong_t)blkid2offset(dnp, bp, zb)); 1254 1255 ASSERT(zb->zb_level >= 0); 1256 1257 for (l = dnp->dn_nlevels - 1; l >= -1; l--) { 1258 if (l == zb->zb_level) { 1259 (void) printf("L%llx", (u_longlong_t)zb->zb_level); 1260 } else { 1261 (void) printf(" "); 1262 } 1263 } 1264 1265 snprintf_blkptr_compact(blkbuf, sizeof (blkbuf), bp); 1266 (void) printf("%s\n", blkbuf); 1267 } 1268 1269 static int 1270 visit_indirect(spa_t *spa, const dnode_phys_t *dnp, 1271 blkptr_t *bp, const zbookmark_phys_t *zb) 1272 { 1273 int err = 0; 1274 1275 if (bp->blk_birth == 0) 1276 return (0); 1277 1278 print_indirect(bp, zb, dnp); 1279 1280 if (BP_GET_LEVEL(bp) > 0 && !BP_IS_HOLE(bp)) { 1281 arc_flags_t flags = ARC_FLAG_WAIT; 1282 int i; 1283 blkptr_t *cbp; 1284 int epb = BP_GET_LSIZE(bp) >> SPA_BLKPTRSHIFT; 1285 arc_buf_t *buf; 1286 uint64_t fill = 0; 1287 1288 err = arc_read(NULL, spa, bp, arc_getbuf_func, &buf, 1289 ZIO_PRIORITY_ASYNC_READ, ZIO_FLAG_CANFAIL, &flags, zb); 1290 if (err) 1291 return (err); 1292 ASSERT(buf->b_data); 1293 1294 /* recursively visit blocks below this */ 1295 cbp = buf->b_data; 1296 for (i = 0; i < epb; i++, cbp++) { 1297 zbookmark_phys_t czb; 1298 1299 SET_BOOKMARK(&czb, zb->zb_objset, zb->zb_object, 1300 zb->zb_level - 1, 1301 zb->zb_blkid * epb + i); 1302 err = visit_indirect(spa, dnp, cbp, &czb); 1303 if (err) 1304 break; 1305 fill += BP_GET_FILL(cbp); 1306 } 1307 if (!err) 1308 ASSERT3U(fill, ==, BP_GET_FILL(bp)); 1309 arc_buf_destroy(buf, &buf); 1310 } 1311 1312 return (err); 1313 } 1314 1315 /*ARGSUSED*/ 1316 static void 1317 dump_indirect(dnode_t *dn) 1318 { 1319 dnode_phys_t *dnp = dn->dn_phys; 1320 int j; 1321 zbookmark_phys_t czb; 1322 1323 (void) printf("Indirect blocks:\n"); 1324 1325 SET_BOOKMARK(&czb, dmu_objset_id(dn->dn_objset), 1326 dn->dn_object, dnp->dn_nlevels - 1, 0); 1327 for (j = 0; j < dnp->dn_nblkptr; j++) { 1328 czb.zb_blkid = j; 1329 (void) visit_indirect(dmu_objset_spa(dn->dn_objset), dnp, 1330 &dnp->dn_blkptr[j], &czb); 1331 } 1332 1333 (void) printf("\n"); 1334 } 1335 1336 /*ARGSUSED*/ 1337 static void 1338 dump_dsl_dir(objset_t *os, uint64_t object, void *data, size_t size) 1339 { 1340 dsl_dir_phys_t *dd = data; 1341 time_t crtime; 1342 char nice[32]; 1343 1344 /* make sure nicenum has enough space */ 1345 CTASSERT(sizeof (nice) >= NN_NUMBUF_SZ); 1346 1347 if (dd == NULL) 1348 return; 1349 1350 ASSERT3U(size, >=, sizeof (dsl_dir_phys_t)); 1351 1352 crtime = dd->dd_creation_time; 1353 (void) printf("\t\tcreation_time = %s", ctime(&crtime)); 1354 (void) printf("\t\thead_dataset_obj = %llu\n", 1355 (u_longlong_t)dd->dd_head_dataset_obj); 1356 (void) printf("\t\tparent_dir_obj = %llu\n", 1357 (u_longlong_t)dd->dd_parent_obj); 1358 (void) printf("\t\torigin_obj = %llu\n", 1359 (u_longlong_t)dd->dd_origin_obj); 1360 (void) printf("\t\tchild_dir_zapobj = %llu\n", 1361 (u_longlong_t)dd->dd_child_dir_zapobj); 1362 zdb_nicenum(dd->dd_used_bytes, nice, sizeof (nice)); 1363 (void) printf("\t\tused_bytes = %s\n", nice); 1364 zdb_nicenum(dd->dd_compressed_bytes, nice, sizeof (nice)); 1365 (void) printf("\t\tcompressed_bytes = %s\n", nice); 1366 zdb_nicenum(dd->dd_uncompressed_bytes, nice, sizeof (nice)); 1367 (void) printf("\t\tuncompressed_bytes = %s\n", nice); 1368 zdb_nicenum(dd->dd_quota, nice, sizeof (nice)); 1369 (void) printf("\t\tquota = %s\n", nice); 1370 zdb_nicenum(dd->dd_reserved, nice, sizeof (nice)); 1371 (void) printf("\t\treserved = %s\n", nice); 1372 (void) printf("\t\tprops_zapobj = %llu\n", 1373 (u_longlong_t)dd->dd_props_zapobj); 1374 (void) printf("\t\tdeleg_zapobj = %llu\n", 1375 (u_longlong_t)dd->dd_deleg_zapobj); 1376 (void) printf("\t\tflags = %llx\n", 1377 (u_longlong_t)dd->dd_flags); 1378 1379 #define DO(which) \ 1380 zdb_nicenum(dd->dd_used_breakdown[DD_USED_ ## which], nice, \ 1381 sizeof (nice)); \ 1382 (void) printf("\t\tused_breakdown[" #which "] = %s\n", nice) 1383 DO(HEAD); 1384 DO(SNAP); 1385 DO(CHILD); 1386 DO(CHILD_RSRV); 1387 DO(REFRSRV); 1388 #undef DO 1389 } 1390 1391 /*ARGSUSED*/ 1392 static void 1393 dump_dsl_dataset(objset_t *os, uint64_t object, void *data, size_t size) 1394 { 1395 dsl_dataset_phys_t *ds = data; 1396 time_t crtime; 1397 char used[32], compressed[32], uncompressed[32], unique[32]; 1398 char blkbuf[BP_SPRINTF_LEN]; 1399 1400 /* make sure nicenum has enough space */ 1401 CTASSERT(sizeof (used) >= NN_NUMBUF_SZ); 1402 CTASSERT(sizeof (compressed) >= NN_NUMBUF_SZ); 1403 CTASSERT(sizeof (uncompressed) >= NN_NUMBUF_SZ); 1404 CTASSERT(sizeof (unique) >= NN_NUMBUF_SZ); 1405 1406 if (ds == NULL) 1407 return; 1408 1409 ASSERT(size == sizeof (*ds)); 1410 crtime = ds->ds_creation_time; 1411 zdb_nicenum(ds->ds_referenced_bytes, used, sizeof (used)); 1412 zdb_nicenum(ds->ds_compressed_bytes, compressed, sizeof (compressed)); 1413 zdb_nicenum(ds->ds_uncompressed_bytes, uncompressed, 1414 sizeof (uncompressed)); 1415 zdb_nicenum(ds->ds_unique_bytes, unique, sizeof (unique)); 1416 snprintf_blkptr(blkbuf, sizeof (blkbuf), &ds->ds_bp); 1417 1418 (void) printf("\t\tdir_obj = %llu\n", 1419 (u_longlong_t)ds->ds_dir_obj); 1420 (void) printf("\t\tprev_snap_obj = %llu\n", 1421 (u_longlong_t)ds->ds_prev_snap_obj); 1422 (void) printf("\t\tprev_snap_txg = %llu\n", 1423 (u_longlong_t)ds->ds_prev_snap_txg); 1424 (void) printf("\t\tnext_snap_obj = %llu\n", 1425 (u_longlong_t)ds->ds_next_snap_obj); 1426 (void) printf("\t\tsnapnames_zapobj = %llu\n", 1427 (u_longlong_t)ds->ds_snapnames_zapobj); 1428 (void) printf("\t\tnum_children = %llu\n", 1429 (u_longlong_t)ds->ds_num_children); 1430 (void) printf("\t\tuserrefs_obj = %llu\n", 1431 (u_longlong_t)ds->ds_userrefs_obj); 1432 (void) printf("\t\tcreation_time = %s", ctime(&crtime)); 1433 (void) printf("\t\tcreation_txg = %llu\n", 1434 (u_longlong_t)ds->ds_creation_txg); 1435 (void) printf("\t\tdeadlist_obj = %llu\n", 1436 (u_longlong_t)ds->ds_deadlist_obj); 1437 (void) printf("\t\tused_bytes = %s\n", used); 1438 (void) printf("\t\tcompressed_bytes = %s\n", compressed); 1439 (void) printf("\t\tuncompressed_bytes = %s\n", uncompressed); 1440 (void) printf("\t\tunique = %s\n", unique); 1441 (void) printf("\t\tfsid_guid = %llu\n", 1442 (u_longlong_t)ds->ds_fsid_guid); 1443 (void) printf("\t\tguid = %llu\n", 1444 (u_longlong_t)ds->ds_guid); 1445 (void) printf("\t\tflags = %llx\n", 1446 (u_longlong_t)ds->ds_flags); 1447 (void) printf("\t\tnext_clones_obj = %llu\n", 1448 (u_longlong_t)ds->ds_next_clones_obj); 1449 (void) printf("\t\tprops_obj = %llu\n", 1450 (u_longlong_t)ds->ds_props_obj); 1451 (void) printf("\t\tbp = %s\n", blkbuf); 1452 } 1453 1454 /* ARGSUSED */ 1455 static int 1456 dump_bptree_cb(void *arg, const blkptr_t *bp, dmu_tx_t *tx) 1457 { 1458 char blkbuf[BP_SPRINTF_LEN]; 1459 1460 if (bp->blk_birth != 0) { 1461 snprintf_blkptr(blkbuf, sizeof (blkbuf), bp); 1462 (void) printf("\t%s\n", blkbuf); 1463 } 1464 return (0); 1465 } 1466 1467 static void 1468 dump_bptree(objset_t *os, uint64_t obj, char *name) 1469 { 1470 char bytes[32]; 1471 bptree_phys_t *bt; 1472 dmu_buf_t *db; 1473 1474 /* make sure nicenum has enough space */ 1475 CTASSERT(sizeof (bytes) >= NN_NUMBUF_SZ); 1476 1477 if (dump_opt['d'] < 3) 1478 return; 1479 1480 VERIFY3U(0, ==, dmu_bonus_hold(os, obj, FTAG, &db)); 1481 bt = db->db_data; 1482 zdb_nicenum(bt->bt_bytes, bytes, sizeof (bytes)); 1483 (void) printf("\n %s: %llu datasets, %s\n", 1484 name, (unsigned long long)(bt->bt_end - bt->bt_begin), bytes); 1485 dmu_buf_rele(db, FTAG); 1486 1487 if (dump_opt['d'] < 5) 1488 return; 1489 1490 (void) printf("\n"); 1491 1492 (void) bptree_iterate(os, obj, B_FALSE, dump_bptree_cb, NULL, NULL); 1493 } 1494 1495 /* ARGSUSED */ 1496 static int 1497 dump_bpobj_cb(void *arg, const blkptr_t *bp, dmu_tx_t *tx) 1498 { 1499 char blkbuf[BP_SPRINTF_LEN]; 1500 1501 ASSERT(bp->blk_birth != 0); 1502 snprintf_blkptr_compact(blkbuf, sizeof (blkbuf), bp); 1503 (void) printf("\t%s\n", blkbuf); 1504 return (0); 1505 } 1506 1507 static void 1508 dump_full_bpobj(bpobj_t *bpo, char *name, int indent) 1509 { 1510 char bytes[32]; 1511 char comp[32]; 1512 char uncomp[32]; 1513 1514 /* make sure nicenum has enough space */ 1515 CTASSERT(sizeof (bytes) >= NN_NUMBUF_SZ); 1516 CTASSERT(sizeof (comp) >= NN_NUMBUF_SZ); 1517 CTASSERT(sizeof (uncomp) >= NN_NUMBUF_SZ); 1518 1519 if (dump_opt['d'] < 3) 1520 return; 1521 1522 zdb_nicenum(bpo->bpo_phys->bpo_bytes, bytes, sizeof (bytes)); 1523 if (bpo->bpo_havesubobj && bpo->bpo_phys->bpo_subobjs != 0) { 1524 zdb_nicenum(bpo->bpo_phys->bpo_comp, comp, sizeof (comp)); 1525 zdb_nicenum(bpo->bpo_phys->bpo_uncomp, uncomp, sizeof (uncomp)); 1526 (void) printf(" %*s: object %llu, %llu local blkptrs, " 1527 "%llu subobjs in object %llu, %s (%s/%s comp)\n", 1528 indent * 8, name, 1529 (u_longlong_t)bpo->bpo_object, 1530 (u_longlong_t)bpo->bpo_phys->bpo_num_blkptrs, 1531 (u_longlong_t)bpo->bpo_phys->bpo_num_subobjs, 1532 (u_longlong_t)bpo->bpo_phys->bpo_subobjs, 1533 bytes, comp, uncomp); 1534 1535 for (uint64_t i = 0; i < bpo->bpo_phys->bpo_num_subobjs; i++) { 1536 uint64_t subobj; 1537 bpobj_t subbpo; 1538 int error; 1539 VERIFY0(dmu_read(bpo->bpo_os, 1540 bpo->bpo_phys->bpo_subobjs, 1541 i * sizeof (subobj), sizeof (subobj), &subobj, 0)); 1542 error = bpobj_open(&subbpo, bpo->bpo_os, subobj); 1543 if (error != 0) { 1544 (void) printf("ERROR %u while trying to open " 1545 "subobj id %llu\n", 1546 error, (u_longlong_t)subobj); 1547 continue; 1548 } 1549 dump_full_bpobj(&subbpo, "subobj", indent + 1); 1550 bpobj_close(&subbpo); 1551 } 1552 } else { 1553 (void) printf(" %*s: object %llu, %llu blkptrs, %s\n", 1554 indent * 8, name, 1555 (u_longlong_t)bpo->bpo_object, 1556 (u_longlong_t)bpo->bpo_phys->bpo_num_blkptrs, 1557 bytes); 1558 } 1559 1560 if (dump_opt['d'] < 5) 1561 return; 1562 1563 1564 if (indent == 0) { 1565 (void) bpobj_iterate_nofree(bpo, dump_bpobj_cb, NULL, NULL); 1566 (void) printf("\n"); 1567 } 1568 } 1569 1570 static void 1571 dump_deadlist(dsl_deadlist_t *dl) 1572 { 1573 dsl_deadlist_entry_t *dle; 1574 uint64_t unused; 1575 char bytes[32]; 1576 char comp[32]; 1577 char uncomp[32]; 1578 1579 /* make sure nicenum has enough space */ 1580 CTASSERT(sizeof (bytes) >= NN_NUMBUF_SZ); 1581 CTASSERT(sizeof (comp) >= NN_NUMBUF_SZ); 1582 CTASSERT(sizeof (uncomp) >= NN_NUMBUF_SZ); 1583 1584 if (dump_opt['d'] < 3) 1585 return; 1586 1587 if (dl->dl_oldfmt) { 1588 dump_full_bpobj(&dl->dl_bpobj, "old-format deadlist", 0); 1589 return; 1590 } 1591 1592 zdb_nicenum(dl->dl_phys->dl_used, bytes, sizeof (bytes)); 1593 zdb_nicenum(dl->dl_phys->dl_comp, comp, sizeof (comp)); 1594 zdb_nicenum(dl->dl_phys->dl_uncomp, uncomp, sizeof (uncomp)); 1595 (void) printf("\n Deadlist: %s (%s/%s comp)\n", 1596 bytes, comp, uncomp); 1597 1598 if (dump_opt['d'] < 4) 1599 return; 1600 1601 (void) printf("\n"); 1602 1603 /* force the tree to be loaded */ 1604 dsl_deadlist_space_range(dl, 0, UINT64_MAX, &unused, &unused, &unused); 1605 1606 for (dle = avl_first(&dl->dl_tree); dle; 1607 dle = AVL_NEXT(&dl->dl_tree, dle)) { 1608 if (dump_opt['d'] >= 5) { 1609 char buf[128]; 1610 (void) snprintf(buf, sizeof (buf), 1611 "mintxg %llu -> obj %llu", 1612 (longlong_t)dle->dle_mintxg, 1613 (longlong_t)dle->dle_bpobj.bpo_object); 1614 1615 dump_full_bpobj(&dle->dle_bpobj, buf, 0); 1616 } else { 1617 (void) printf("mintxg %llu -> obj %llu\n", 1618 (longlong_t)dle->dle_mintxg, 1619 (longlong_t)dle->dle_bpobj.bpo_object); 1620 1621 } 1622 } 1623 } 1624 1625 static avl_tree_t idx_tree; 1626 static avl_tree_t domain_tree; 1627 static boolean_t fuid_table_loaded; 1628 static objset_t *sa_os = NULL; 1629 static sa_attr_type_t *sa_attr_table = NULL; 1630 1631 static int 1632 open_objset(const char *path, dmu_objset_type_t type, void *tag, objset_t **osp) 1633 { 1634 int err; 1635 uint64_t sa_attrs = 0; 1636 uint64_t version = 0; 1637 1638 VERIFY3P(sa_os, ==, NULL); 1639 err = dmu_objset_own(path, type, B_TRUE, tag, osp); 1640 if (err != 0) { 1641 (void) fprintf(stderr, "failed to own dataset '%s': %s\n", path, 1642 strerror(err)); 1643 return (err); 1644 } 1645 1646 if (dmu_objset_type(*osp) == DMU_OST_ZFS) { 1647 (void) zap_lookup(*osp, MASTER_NODE_OBJ, ZPL_VERSION_STR, 1648 8, 1, &version); 1649 if (version >= ZPL_VERSION_SA) { 1650 (void) zap_lookup(*osp, MASTER_NODE_OBJ, ZFS_SA_ATTRS, 1651 8, 1, &sa_attrs); 1652 } 1653 err = sa_setup(*osp, sa_attrs, zfs_attr_table, ZPL_END, 1654 &sa_attr_table); 1655 if (err != 0) { 1656 (void) fprintf(stderr, "sa_setup failed: %s\n", 1657 strerror(err)); 1658 dmu_objset_disown(*osp, tag); 1659 *osp = NULL; 1660 } 1661 } 1662 sa_os = *osp; 1663 1664 return (0); 1665 } 1666 1667 static void 1668 close_objset(objset_t *os, void *tag) 1669 { 1670 VERIFY3P(os, ==, sa_os); 1671 if (os->os_sa != NULL) 1672 sa_tear_down(os); 1673 dmu_objset_disown(os, tag); 1674 sa_attr_table = NULL; 1675 sa_os = NULL; 1676 } 1677 1678 static void 1679 fuid_table_destroy() 1680 { 1681 if (fuid_table_loaded) { 1682 zfs_fuid_table_destroy(&idx_tree, &domain_tree); 1683 fuid_table_loaded = B_FALSE; 1684 } 1685 } 1686 1687 /* 1688 * print uid or gid information. 1689 * For normal POSIX id just the id is printed in decimal format. 1690 * For CIFS files with FUID the fuid is printed in hex followed by 1691 * the domain-rid string. 1692 */ 1693 static void 1694 print_idstr(uint64_t id, const char *id_type) 1695 { 1696 if (FUID_INDEX(id)) { 1697 char *domain; 1698 1699 domain = zfs_fuid_idx_domain(&idx_tree, FUID_INDEX(id)); 1700 (void) printf("\t%s %llx [%s-%d]\n", id_type, 1701 (u_longlong_t)id, domain, (int)FUID_RID(id)); 1702 } else { 1703 (void) printf("\t%s %llu\n", id_type, (u_longlong_t)id); 1704 } 1705 1706 } 1707 1708 static void 1709 dump_uidgid(objset_t *os, uint64_t uid, uint64_t gid) 1710 { 1711 uint32_t uid_idx, gid_idx; 1712 1713 uid_idx = FUID_INDEX(uid); 1714 gid_idx = FUID_INDEX(gid); 1715 1716 /* Load domain table, if not already loaded */ 1717 if (!fuid_table_loaded && (uid_idx || gid_idx)) { 1718 uint64_t fuid_obj; 1719 1720 /* first find the fuid object. It lives in the master node */ 1721 VERIFY(zap_lookup(os, MASTER_NODE_OBJ, ZFS_FUID_TABLES, 1722 8, 1, &fuid_obj) == 0); 1723 zfs_fuid_avl_tree_create(&idx_tree, &domain_tree); 1724 (void) zfs_fuid_table_load(os, fuid_obj, 1725 &idx_tree, &domain_tree); 1726 fuid_table_loaded = B_TRUE; 1727 } 1728 1729 print_idstr(uid, "uid"); 1730 print_idstr(gid, "gid"); 1731 } 1732 1733 /*ARGSUSED*/ 1734 static void 1735 dump_znode(objset_t *os, uint64_t object, void *data, size_t size) 1736 { 1737 char path[MAXPATHLEN * 2]; /* allow for xattr and failure prefix */ 1738 sa_handle_t *hdl; 1739 uint64_t xattr, rdev, gen; 1740 uint64_t uid, gid, mode, fsize, parent, links; 1741 uint64_t pflags; 1742 uint64_t acctm[2], modtm[2], chgtm[2], crtm[2]; 1743 time_t z_crtime, z_atime, z_mtime, z_ctime; 1744 sa_bulk_attr_t bulk[12]; 1745 int idx = 0; 1746 int error; 1747 1748 VERIFY3P(os, ==, sa_os); 1749 if (sa_handle_get(os, object, NULL, SA_HDL_PRIVATE, &hdl)) { 1750 (void) printf("Failed to get handle for SA znode\n"); 1751 return; 1752 } 1753 1754 SA_ADD_BULK_ATTR(bulk, idx, sa_attr_table[ZPL_UID], NULL, &uid, 8); 1755 SA_ADD_BULK_ATTR(bulk, idx, sa_attr_table[ZPL_GID], NULL, &gid, 8); 1756 SA_ADD_BULK_ATTR(bulk, idx, sa_attr_table[ZPL_LINKS], NULL, 1757 &links, 8); 1758 SA_ADD_BULK_ATTR(bulk, idx, sa_attr_table[ZPL_GEN], NULL, &gen, 8); 1759 SA_ADD_BULK_ATTR(bulk, idx, sa_attr_table[ZPL_MODE], NULL, 1760 &mode, 8); 1761 SA_ADD_BULK_ATTR(bulk, idx, sa_attr_table[ZPL_PARENT], 1762 NULL, &parent, 8); 1763 SA_ADD_BULK_ATTR(bulk, idx, sa_attr_table[ZPL_SIZE], NULL, 1764 &fsize, 8); 1765 SA_ADD_BULK_ATTR(bulk, idx, sa_attr_table[ZPL_ATIME], NULL, 1766 acctm, 16); 1767 SA_ADD_BULK_ATTR(bulk, idx, sa_attr_table[ZPL_MTIME], NULL, 1768 modtm, 16); 1769 SA_ADD_BULK_ATTR(bulk, idx, sa_attr_table[ZPL_CRTIME], NULL, 1770 crtm, 16); 1771 SA_ADD_BULK_ATTR(bulk, idx, sa_attr_table[ZPL_CTIME], NULL, 1772 chgtm, 16); 1773 SA_ADD_BULK_ATTR(bulk, idx, sa_attr_table[ZPL_FLAGS], NULL, 1774 &pflags, 8); 1775 1776 if (sa_bulk_lookup(hdl, bulk, idx)) { 1777 (void) sa_handle_destroy(hdl); 1778 return; 1779 } 1780 1781 z_crtime = (time_t)crtm[0]; 1782 z_atime = (time_t)acctm[0]; 1783 z_mtime = (time_t)modtm[0]; 1784 z_ctime = (time_t)chgtm[0]; 1785 1786 if (dump_opt['d'] > 4) { 1787 error = zfs_obj_to_path(os, object, path, sizeof (path)); 1788 if (error != 0) { 1789 (void) snprintf(path, sizeof (path), 1790 "\?\?\?<object#%llu>", (u_longlong_t)object); 1791 } 1792 (void) printf("\tpath %s\n", path); 1793 } 1794 dump_uidgid(os, uid, gid); 1795 (void) printf("\tatime %s", ctime(&z_atime)); 1796 (void) printf("\tmtime %s", ctime(&z_mtime)); 1797 (void) printf("\tctime %s", ctime(&z_ctime)); 1798 (void) printf("\tcrtime %s", ctime(&z_crtime)); 1799 (void) printf("\tgen %llu\n", (u_longlong_t)gen); 1800 (void) printf("\tmode %llo\n", (u_longlong_t)mode); 1801 (void) printf("\tsize %llu\n", (u_longlong_t)fsize); 1802 (void) printf("\tparent %llu\n", (u_longlong_t)parent); 1803 (void) printf("\tlinks %llu\n", (u_longlong_t)links); 1804 (void) printf("\tpflags %llx\n", (u_longlong_t)pflags); 1805 if (sa_lookup(hdl, sa_attr_table[ZPL_XATTR], &xattr, 1806 sizeof (uint64_t)) == 0) 1807 (void) printf("\txattr %llu\n", (u_longlong_t)xattr); 1808 if (sa_lookup(hdl, sa_attr_table[ZPL_RDEV], &rdev, 1809 sizeof (uint64_t)) == 0) 1810 (void) printf("\trdev 0x%016llx\n", (u_longlong_t)rdev); 1811 sa_handle_destroy(hdl); 1812 } 1813 1814 /*ARGSUSED*/ 1815 static void 1816 dump_acl(objset_t *os, uint64_t object, void *data, size_t size) 1817 { 1818 } 1819 1820 /*ARGSUSED*/ 1821 static void 1822 dump_dmu_objset(objset_t *os, uint64_t object, void *data, size_t size) 1823 { 1824 } 1825 1826 static object_viewer_t *object_viewer[DMU_OT_NUMTYPES + 1] = { 1827 dump_none, /* unallocated */ 1828 dump_zap, /* object directory */ 1829 dump_uint64, /* object array */ 1830 dump_none, /* packed nvlist */ 1831 dump_packed_nvlist, /* packed nvlist size */ 1832 dump_none, /* bpobj */ 1833 dump_bpobj, /* bpobj header */ 1834 dump_none, /* SPA space map header */ 1835 dump_none, /* SPA space map */ 1836 dump_none, /* ZIL intent log */ 1837 dump_dnode, /* DMU dnode */ 1838 dump_dmu_objset, /* DMU objset */ 1839 dump_dsl_dir, /* DSL directory */ 1840 dump_zap, /* DSL directory child map */ 1841 dump_zap, /* DSL dataset snap map */ 1842 dump_zap, /* DSL props */ 1843 dump_dsl_dataset, /* DSL dataset */ 1844 dump_znode, /* ZFS znode */ 1845 dump_acl, /* ZFS V0 ACL */ 1846 dump_uint8, /* ZFS plain file */ 1847 dump_zpldir, /* ZFS directory */ 1848 dump_zap, /* ZFS master node */ 1849 dump_zap, /* ZFS delete queue */ 1850 dump_uint8, /* zvol object */ 1851 dump_zap, /* zvol prop */ 1852 dump_uint8, /* other uint8[] */ 1853 dump_uint64, /* other uint64[] */ 1854 dump_zap, /* other ZAP */ 1855 dump_zap, /* persistent error log */ 1856 dump_uint8, /* SPA history */ 1857 dump_history_offsets, /* SPA history offsets */ 1858 dump_zap, /* Pool properties */ 1859 dump_zap, /* DSL permissions */ 1860 dump_acl, /* ZFS ACL */ 1861 dump_uint8, /* ZFS SYSACL */ 1862 dump_none, /* FUID nvlist */ 1863 dump_packed_nvlist, /* FUID nvlist size */ 1864 dump_zap, /* DSL dataset next clones */ 1865 dump_zap, /* DSL scrub queue */ 1866 dump_zap, /* ZFS user/group used */ 1867 dump_zap, /* ZFS user/group quota */ 1868 dump_zap, /* snapshot refcount tags */ 1869 dump_ddt_zap, /* DDT ZAP object */ 1870 dump_zap, /* DDT statistics */ 1871 dump_znode, /* SA object */ 1872 dump_zap, /* SA Master Node */ 1873 dump_sa_attrs, /* SA attribute registration */ 1874 dump_sa_layouts, /* SA attribute layouts */ 1875 dump_zap, /* DSL scrub translations */ 1876 dump_none, /* fake dedup BP */ 1877 dump_zap, /* deadlist */ 1878 dump_none, /* deadlist hdr */ 1879 dump_zap, /* dsl clones */ 1880 dump_bpobj_subobjs, /* bpobj subobjs */ 1881 dump_unknown, /* Unknown type, must be last */ 1882 }; 1883 1884 static void 1885 dump_object(objset_t *os, uint64_t object, int verbosity, int *print_header) 1886 { 1887 dmu_buf_t *db = NULL; 1888 dmu_object_info_t doi; 1889 dnode_t *dn; 1890 void *bonus = NULL; 1891 size_t bsize = 0; 1892 char iblk[32], dblk[32], lsize[32], asize[32], fill[32]; 1893 char bonus_size[32]; 1894 char aux[50]; 1895 int error; 1896 1897 /* make sure nicenum has enough space */ 1898 CTASSERT(sizeof (iblk) >= NN_NUMBUF_SZ); 1899 CTASSERT(sizeof (dblk) >= NN_NUMBUF_SZ); 1900 CTASSERT(sizeof (lsize) >= NN_NUMBUF_SZ); 1901 CTASSERT(sizeof (asize) >= NN_NUMBUF_SZ); 1902 CTASSERT(sizeof (bonus_size) >= NN_NUMBUF_SZ); 1903 1904 if (*print_header) { 1905 (void) printf("\n%10s %3s %5s %5s %5s %5s %6s %s\n", 1906 "Object", "lvl", "iblk", "dblk", "dsize", "lsize", 1907 "%full", "type"); 1908 *print_header = 0; 1909 } 1910 1911 if (object == 0) { 1912 dn = DMU_META_DNODE(os); 1913 } else { 1914 error = dmu_bonus_hold(os, object, FTAG, &db); 1915 if (error) 1916 fatal("dmu_bonus_hold(%llu) failed, errno %u", 1917 object, error); 1918 bonus = db->db_data; 1919 bsize = db->db_size; 1920 dn = DB_DNODE((dmu_buf_impl_t *)db); 1921 } 1922 dmu_object_info_from_dnode(dn, &doi); 1923 1924 zdb_nicenum(doi.doi_metadata_block_size, iblk, sizeof (iblk)); 1925 zdb_nicenum(doi.doi_data_block_size, dblk, sizeof (dblk)); 1926 zdb_nicenum(doi.doi_max_offset, lsize, sizeof (lsize)); 1927 zdb_nicenum(doi.doi_physical_blocks_512 << 9, asize, sizeof (asize)); 1928 zdb_nicenum(doi.doi_bonus_size, bonus_size, sizeof (bonus_size)); 1929 (void) sprintf(fill, "%6.2f", 100.0 * doi.doi_fill_count * 1930 doi.doi_data_block_size / (object == 0 ? DNODES_PER_BLOCK : 1) / 1931 doi.doi_max_offset); 1932 1933 aux[0] = '\0'; 1934 1935 if (doi.doi_checksum != ZIO_CHECKSUM_INHERIT || verbosity >= 6) { 1936 (void) snprintf(aux + strlen(aux), sizeof (aux), " (K=%s)", 1937 ZDB_CHECKSUM_NAME(doi.doi_checksum)); 1938 } 1939 1940 if (doi.doi_compress != ZIO_COMPRESS_INHERIT || verbosity >= 6) { 1941 (void) snprintf(aux + strlen(aux), sizeof (aux), " (Z=%s)", 1942 ZDB_COMPRESS_NAME(doi.doi_compress)); 1943 } 1944 1945 (void) printf("%10lld %3u %5s %5s %5s %5s %6s %s%s\n", 1946 (u_longlong_t)object, doi.doi_indirection, iblk, dblk, 1947 asize, lsize, fill, ZDB_OT_NAME(doi.doi_type), aux); 1948 1949 if (doi.doi_bonus_type != DMU_OT_NONE && verbosity > 3) { 1950 (void) printf("%10s %3s %5s %5s %5s %5s %6s %s\n", 1951 "", "", "", "", "", bonus_size, "bonus", 1952 ZDB_OT_NAME(doi.doi_bonus_type)); 1953 } 1954 1955 if (verbosity >= 4) { 1956 (void) printf("\tdnode flags: %s%s%s\n", 1957 (dn->dn_phys->dn_flags & DNODE_FLAG_USED_BYTES) ? 1958 "USED_BYTES " : "", 1959 (dn->dn_phys->dn_flags & DNODE_FLAG_USERUSED_ACCOUNTED) ? 1960 "USERUSED_ACCOUNTED " : "", 1961 (dn->dn_phys->dn_flags & DNODE_FLAG_SPILL_BLKPTR) ? 1962 "SPILL_BLKPTR" : ""); 1963 (void) printf("\tdnode maxblkid: %llu\n", 1964 (longlong_t)dn->dn_phys->dn_maxblkid); 1965 1966 object_viewer[ZDB_OT_TYPE(doi.doi_bonus_type)](os, object, 1967 bonus, bsize); 1968 object_viewer[ZDB_OT_TYPE(doi.doi_type)](os, object, NULL, 0); 1969 *print_header = 1; 1970 } 1971 1972 if (verbosity >= 5) 1973 dump_indirect(dn); 1974 1975 if (verbosity >= 5) { 1976 /* 1977 * Report the list of segments that comprise the object. 1978 */ 1979 uint64_t start = 0; 1980 uint64_t end; 1981 uint64_t blkfill = 1; 1982 int minlvl = 1; 1983 1984 if (dn->dn_type == DMU_OT_DNODE) { 1985 minlvl = 0; 1986 blkfill = DNODES_PER_BLOCK; 1987 } 1988 1989 for (;;) { 1990 char segsize[32]; 1991 /* make sure nicenum has enough space */ 1992 CTASSERT(sizeof (segsize) >= NN_NUMBUF_SZ); 1993 error = dnode_next_offset(dn, 1994 0, &start, minlvl, blkfill, 0); 1995 if (error) 1996 break; 1997 end = start; 1998 error = dnode_next_offset(dn, 1999 DNODE_FIND_HOLE, &end, minlvl, blkfill, 0); 2000 zdb_nicenum(end - start, segsize, sizeof (segsize)); 2001 (void) printf("\t\tsegment [%016llx, %016llx)" 2002 " size %5s\n", (u_longlong_t)start, 2003 (u_longlong_t)end, segsize); 2004 if (error) 2005 break; 2006 start = end; 2007 } 2008 } 2009 2010 if (db != NULL) 2011 dmu_buf_rele(db, FTAG); 2012 } 2013 2014 static char *objset_types[DMU_OST_NUMTYPES] = { 2015 "NONE", "META", "ZPL", "ZVOL", "OTHER", "ANY" }; 2016 2017 static void 2018 dump_dir(objset_t *os) 2019 { 2020 dmu_objset_stats_t dds; 2021 uint64_t object, object_count; 2022 uint64_t refdbytes, usedobjs, scratch; 2023 char numbuf[32]; 2024 char blkbuf[BP_SPRINTF_LEN + 20]; 2025 char osname[ZFS_MAX_DATASET_NAME_LEN]; 2026 char *type = "UNKNOWN"; 2027 int verbosity = dump_opt['d']; 2028 int print_header = 1; 2029 int i, error; 2030 2031 /* make sure nicenum has enough space */ 2032 CTASSERT(sizeof (numbuf) >= NN_NUMBUF_SZ); 2033 2034 dsl_pool_config_enter(dmu_objset_pool(os), FTAG); 2035 dmu_objset_fast_stat(os, &dds); 2036 dsl_pool_config_exit(dmu_objset_pool(os), FTAG); 2037 2038 if (dds.dds_type < DMU_OST_NUMTYPES) 2039 type = objset_types[dds.dds_type]; 2040 2041 if (dds.dds_type == DMU_OST_META) { 2042 dds.dds_creation_txg = TXG_INITIAL; 2043 usedobjs = BP_GET_FILL(os->os_rootbp); 2044 refdbytes = dsl_dir_phys(os->os_spa->spa_dsl_pool->dp_mos_dir)-> 2045 dd_used_bytes; 2046 } else { 2047 dmu_objset_space(os, &refdbytes, &scratch, &usedobjs, &scratch); 2048 } 2049 2050 ASSERT3U(usedobjs, ==, BP_GET_FILL(os->os_rootbp)); 2051 2052 zdb_nicenum(refdbytes, numbuf, sizeof (numbuf)); 2053 2054 if (verbosity >= 4) { 2055 (void) snprintf(blkbuf, sizeof (blkbuf), ", rootbp "); 2056 (void) snprintf_blkptr(blkbuf + strlen(blkbuf), 2057 sizeof (blkbuf) - strlen(blkbuf), os->os_rootbp); 2058 } else { 2059 blkbuf[0] = '\0'; 2060 } 2061 2062 dmu_objset_name(os, osname); 2063 2064 (void) printf("Dataset %s [%s], ID %llu, cr_txg %llu, " 2065 "%s, %llu objects%s\n", 2066 osname, type, (u_longlong_t)dmu_objset_id(os), 2067 (u_longlong_t)dds.dds_creation_txg, 2068 numbuf, (u_longlong_t)usedobjs, blkbuf); 2069 2070 if (zopt_objects != 0) { 2071 for (i = 0; i < zopt_objects; i++) 2072 dump_object(os, zopt_object[i], verbosity, 2073 &print_header); 2074 (void) printf("\n"); 2075 return; 2076 } 2077 2078 if (dump_opt['i'] != 0 || verbosity >= 2) 2079 dump_intent_log(dmu_objset_zil(os)); 2080 2081 if (dmu_objset_ds(os) != NULL) 2082 dump_deadlist(&dmu_objset_ds(os)->ds_deadlist); 2083 2084 if (verbosity < 2) 2085 return; 2086 2087 if (BP_IS_HOLE(os->os_rootbp)) 2088 return; 2089 2090 dump_object(os, 0, verbosity, &print_header); 2091 object_count = 0; 2092 if (DMU_USERUSED_DNODE(os) != NULL && 2093 DMU_USERUSED_DNODE(os)->dn_type != 0) { 2094 dump_object(os, DMU_USERUSED_OBJECT, verbosity, &print_header); 2095 dump_object(os, DMU_GROUPUSED_OBJECT, verbosity, &print_header); 2096 } 2097 2098 object = 0; 2099 while ((error = dmu_object_next(os, &object, B_FALSE, 0)) == 0) { 2100 dump_object(os, object, verbosity, &print_header); 2101 object_count++; 2102 } 2103 2104 ASSERT3U(object_count, ==, usedobjs); 2105 2106 (void) printf("\n"); 2107 2108 if (error != ESRCH) { 2109 (void) fprintf(stderr, "dmu_object_next() = %d\n", error); 2110 abort(); 2111 } 2112 } 2113 2114 static void 2115 dump_uberblock(uberblock_t *ub, const char *header, const char *footer) 2116 { 2117 time_t timestamp = ub->ub_timestamp; 2118 2119 (void) printf(header ? header : ""); 2120 (void) printf("\tmagic = %016llx\n", (u_longlong_t)ub->ub_magic); 2121 (void) printf("\tversion = %llu\n", (u_longlong_t)ub->ub_version); 2122 (void) printf("\ttxg = %llu\n", (u_longlong_t)ub->ub_txg); 2123 (void) printf("\tguid_sum = %llu\n", (u_longlong_t)ub->ub_guid_sum); 2124 (void) printf("\ttimestamp = %llu UTC = %s", 2125 (u_longlong_t)ub->ub_timestamp, asctime(localtime(×tamp))); 2126 if (dump_opt['u'] >= 3) { 2127 char blkbuf[BP_SPRINTF_LEN]; 2128 snprintf_blkptr(blkbuf, sizeof (blkbuf), &ub->ub_rootbp); 2129 (void) printf("\trootbp = %s\n", blkbuf); 2130 } 2131 (void) printf(footer ? footer : ""); 2132 } 2133 2134 static void 2135 dump_config(spa_t *spa) 2136 { 2137 dmu_buf_t *db; 2138 size_t nvsize = 0; 2139 int error = 0; 2140 2141 2142 error = dmu_bonus_hold(spa->spa_meta_objset, 2143 spa->spa_config_object, FTAG, &db); 2144 2145 if (error == 0) { 2146 nvsize = *(uint64_t *)db->db_data; 2147 dmu_buf_rele(db, FTAG); 2148 2149 (void) printf("\nMOS Configuration:\n"); 2150 dump_packed_nvlist(spa->spa_meta_objset, 2151 spa->spa_config_object, (void *)&nvsize, 1); 2152 } else { 2153 (void) fprintf(stderr, "dmu_bonus_hold(%llu) failed, errno %d", 2154 (u_longlong_t)spa->spa_config_object, error); 2155 } 2156 } 2157 2158 static void 2159 dump_cachefile(const char *cachefile) 2160 { 2161 int fd; 2162 struct stat64 statbuf; 2163 char *buf; 2164 nvlist_t *config; 2165 2166 if ((fd = open64(cachefile, O_RDONLY)) < 0) { 2167 (void) printf("cannot open '%s': %s\n", cachefile, 2168 strerror(errno)); 2169 exit(1); 2170 } 2171 2172 if (fstat64(fd, &statbuf) != 0) { 2173 (void) printf("failed to stat '%s': %s\n", cachefile, 2174 strerror(errno)); 2175 exit(1); 2176 } 2177 2178 if ((buf = malloc(statbuf.st_size)) == NULL) { 2179 (void) fprintf(stderr, "failed to allocate %llu bytes\n", 2180 (u_longlong_t)statbuf.st_size); 2181 exit(1); 2182 } 2183 2184 if (read(fd, buf, statbuf.st_size) != statbuf.st_size) { 2185 (void) fprintf(stderr, "failed to read %llu bytes\n", 2186 (u_longlong_t)statbuf.st_size); 2187 exit(1); 2188 } 2189 2190 (void) close(fd); 2191 2192 if (nvlist_unpack(buf, statbuf.st_size, &config, 0) != 0) { 2193 (void) fprintf(stderr, "failed to unpack nvlist\n"); 2194 exit(1); 2195 } 2196 2197 free(buf); 2198 2199 dump_nvlist(config, 0); 2200 2201 nvlist_free(config); 2202 } 2203 2204 #define ZDB_MAX_UB_HEADER_SIZE 32 2205 2206 static void 2207 dump_label_uberblocks(vdev_label_t *lbl, uint64_t ashift) 2208 { 2209 vdev_t vd; 2210 vdev_t *vdp = &vd; 2211 char header[ZDB_MAX_UB_HEADER_SIZE]; 2212 2213 vd.vdev_ashift = ashift; 2214 vdp->vdev_top = vdp; 2215 2216 for (int i = 0; i < VDEV_UBERBLOCK_COUNT(vdp); i++) { 2217 uint64_t uoff = VDEV_UBERBLOCK_OFFSET(vdp, i); 2218 uberblock_t *ub = (void *)((char *)lbl + uoff); 2219 2220 if (uberblock_verify(ub)) 2221 continue; 2222 (void) snprintf(header, ZDB_MAX_UB_HEADER_SIZE, 2223 "Uberblock[%d]\n", i); 2224 dump_uberblock(ub, header, ""); 2225 } 2226 } 2227 2228 static char curpath[PATH_MAX]; 2229 2230 /* 2231 * Iterate through the path components, recursively passing 2232 * current one's obj and remaining path until we find the obj 2233 * for the last one. 2234 */ 2235 static int 2236 dump_path_impl(objset_t *os, uint64_t obj, char *name) 2237 { 2238 int err; 2239 int header = 1; 2240 uint64_t child_obj; 2241 char *s; 2242 dmu_buf_t *db; 2243 dmu_object_info_t doi; 2244 2245 if ((s = strchr(name, '/')) != NULL) 2246 *s = '\0'; 2247 err = zap_lookup(os, obj, name, 8, 1, &child_obj); 2248 2249 (void) strlcat(curpath, name, sizeof (curpath)); 2250 2251 if (err != 0) { 2252 (void) fprintf(stderr, "failed to lookup %s: %s\n", 2253 curpath, strerror(err)); 2254 return (err); 2255 } 2256 2257 child_obj = ZFS_DIRENT_OBJ(child_obj); 2258 err = sa_buf_hold(os, child_obj, FTAG, &db); 2259 if (err != 0) { 2260 (void) fprintf(stderr, 2261 "failed to get SA dbuf for obj %llu: %s\n", 2262 (u_longlong_t)child_obj, strerror(err)); 2263 return (EINVAL); 2264 } 2265 dmu_object_info_from_db(db, &doi); 2266 sa_buf_rele(db, FTAG); 2267 2268 if (doi.doi_bonus_type != DMU_OT_SA && 2269 doi.doi_bonus_type != DMU_OT_ZNODE) { 2270 (void) fprintf(stderr, "invalid bonus type %d for obj %llu\n", 2271 doi.doi_bonus_type, (u_longlong_t)child_obj); 2272 return (EINVAL); 2273 } 2274 2275 if (dump_opt['v'] > 6) { 2276 (void) printf("obj=%llu %s type=%d bonustype=%d\n", 2277 (u_longlong_t)child_obj, curpath, doi.doi_type, 2278 doi.doi_bonus_type); 2279 } 2280 2281 (void) strlcat(curpath, "/", sizeof (curpath)); 2282 2283 switch (doi.doi_type) { 2284 case DMU_OT_DIRECTORY_CONTENTS: 2285 if (s != NULL && *(s + 1) != '\0') 2286 return (dump_path_impl(os, child_obj, s + 1)); 2287 /*FALLTHROUGH*/ 2288 case DMU_OT_PLAIN_FILE_CONTENTS: 2289 dump_object(os, child_obj, dump_opt['v'], &header); 2290 return (0); 2291 default: 2292 (void) fprintf(stderr, "object %llu has non-file/directory " 2293 "type %d\n", (u_longlong_t)obj, doi.doi_type); 2294 break; 2295 } 2296 2297 return (EINVAL); 2298 } 2299 2300 /* 2301 * Dump the blocks for the object specified by path inside the dataset. 2302 */ 2303 static int 2304 dump_path(char *ds, char *path) 2305 { 2306 int err; 2307 objset_t *os; 2308 uint64_t root_obj; 2309 2310 err = open_objset(ds, DMU_OST_ZFS, FTAG, &os); 2311 if (err != 0) 2312 return (err); 2313 2314 err = zap_lookup(os, MASTER_NODE_OBJ, ZFS_ROOT_OBJ, 8, 1, &root_obj); 2315 if (err != 0) { 2316 (void) fprintf(stderr, "can't lookup root znode: %s\n", 2317 strerror(err)); 2318 dmu_objset_disown(os, FTAG); 2319 return (EINVAL); 2320 } 2321 2322 (void) snprintf(curpath, sizeof (curpath), "dataset=%s path=/", ds); 2323 2324 err = dump_path_impl(os, root_obj, path); 2325 2326 close_objset(os, FTAG); 2327 return (err); 2328 } 2329 2330 static int 2331 dump_label(const char *dev) 2332 { 2333 int fd; 2334 vdev_label_t label; 2335 char path[MAXPATHLEN]; 2336 char *buf = label.vl_vdev_phys.vp_nvlist; 2337 size_t buflen = sizeof (label.vl_vdev_phys.vp_nvlist); 2338 struct stat64 statbuf; 2339 uint64_t psize, ashift; 2340 boolean_t label_found = B_FALSE; 2341 2342 (void) strlcpy(path, dev, sizeof (path)); 2343 if (dev[0] == '/') { 2344 if (strncmp(dev, ZFS_DISK_ROOTD, 2345 strlen(ZFS_DISK_ROOTD)) == 0) { 2346 (void) snprintf(path, sizeof (path), "%s%s", 2347 ZFS_RDISK_ROOTD, dev + strlen(ZFS_DISK_ROOTD)); 2348 } 2349 } else if (stat64(path, &statbuf) != 0) { 2350 char *s; 2351 2352 (void) snprintf(path, sizeof (path), "%s%s", ZFS_RDISK_ROOTD, 2353 dev); 2354 if (((s = strrchr(dev, 's')) == NULL && 2355 (s = strchr(dev, 'p')) == NULL) || 2356 !isdigit(*(s + 1))) 2357 (void) strlcat(path, "s0", sizeof (path)); 2358 } 2359 2360 if ((fd = open64(path, O_RDONLY)) < 0) { 2361 (void) fprintf(stderr, "cannot open '%s': %s\n", path, 2362 strerror(errno)); 2363 exit(1); 2364 } 2365 2366 if (fstat64(fd, &statbuf) != 0) { 2367 (void) fprintf(stderr, "failed to stat '%s': %s\n", path, 2368 strerror(errno)); 2369 (void) close(fd); 2370 exit(1); 2371 } 2372 2373 if (S_ISBLK(statbuf.st_mode)) { 2374 (void) fprintf(stderr, 2375 "cannot use '%s': character device required\n", path); 2376 (void) close(fd); 2377 exit(1); 2378 } 2379 2380 psize = statbuf.st_size; 2381 psize = P2ALIGN(psize, (uint64_t)sizeof (vdev_label_t)); 2382 2383 for (int l = 0; l < VDEV_LABELS; l++) { 2384 nvlist_t *config = NULL; 2385 2386 if (!dump_opt['q']) { 2387 (void) printf("------------------------------------\n"); 2388 (void) printf("LABEL %d\n", l); 2389 (void) printf("------------------------------------\n"); 2390 } 2391 2392 if (pread64(fd, &label, sizeof (label), 2393 vdev_label_offset(psize, l, 0)) != sizeof (label)) { 2394 if (!dump_opt['q']) 2395 (void) printf("failed to read label %d\n", l); 2396 continue; 2397 } 2398 2399 if (nvlist_unpack(buf, buflen, &config, 0) != 0) { 2400 if (!dump_opt['q']) 2401 (void) printf("failed to unpack label %d\n", l); 2402 ashift = SPA_MINBLOCKSHIFT; 2403 } else { 2404 nvlist_t *vdev_tree = NULL; 2405 2406 if (!dump_opt['q']) 2407 dump_nvlist(config, 4); 2408 if ((nvlist_lookup_nvlist(config, 2409 ZPOOL_CONFIG_VDEV_TREE, &vdev_tree) != 0) || 2410 (nvlist_lookup_uint64(vdev_tree, 2411 ZPOOL_CONFIG_ASHIFT, &ashift) != 0)) 2412 ashift = SPA_MINBLOCKSHIFT; 2413 nvlist_free(config); 2414 label_found = B_TRUE; 2415 } 2416 if (dump_opt['u']) 2417 dump_label_uberblocks(&label, ashift); 2418 } 2419 2420 (void) close(fd); 2421 2422 return (label_found ? 0 : 2); 2423 } 2424 2425 static uint64_t dataset_feature_count[SPA_FEATURES]; 2426 2427 /*ARGSUSED*/ 2428 static int 2429 dump_one_dir(const char *dsname, void *arg) 2430 { 2431 int error; 2432 objset_t *os; 2433 2434 error = open_objset(dsname, DMU_OST_ANY, FTAG, &os); 2435 if (error != 0) 2436 return (0); 2437 2438 for (spa_feature_t f = 0; f < SPA_FEATURES; f++) { 2439 if (!dmu_objset_ds(os)->ds_feature_inuse[f]) 2440 continue; 2441 ASSERT(spa_feature_table[f].fi_flags & 2442 ZFEATURE_FLAG_PER_DATASET); 2443 dataset_feature_count[f]++; 2444 } 2445 2446 dump_dir(os); 2447 close_objset(os, FTAG); 2448 fuid_table_destroy(); 2449 return (0); 2450 } 2451 2452 /* 2453 * Block statistics. 2454 */ 2455 #define PSIZE_HISTO_SIZE (SPA_OLD_MAXBLOCKSIZE / SPA_MINBLOCKSIZE + 2) 2456 typedef struct zdb_blkstats { 2457 uint64_t zb_asize; 2458 uint64_t zb_lsize; 2459 uint64_t zb_psize; 2460 uint64_t zb_count; 2461 uint64_t zb_gangs; 2462 uint64_t zb_ditto_samevdev; 2463 uint64_t zb_psize_histogram[PSIZE_HISTO_SIZE]; 2464 } zdb_blkstats_t; 2465 2466 /* 2467 * Extended object types to report deferred frees and dedup auto-ditto blocks. 2468 */ 2469 #define ZDB_OT_DEFERRED (DMU_OT_NUMTYPES + 0) 2470 #define ZDB_OT_DITTO (DMU_OT_NUMTYPES + 1) 2471 #define ZDB_OT_OTHER (DMU_OT_NUMTYPES + 2) 2472 #define ZDB_OT_TOTAL (DMU_OT_NUMTYPES + 3) 2473 2474 static char *zdb_ot_extname[] = { 2475 "deferred free", 2476 "dedup ditto", 2477 "other", 2478 "Total", 2479 }; 2480 2481 #define ZB_TOTAL DN_MAX_LEVELS 2482 2483 typedef struct zdb_cb { 2484 zdb_blkstats_t zcb_type[ZB_TOTAL + 1][ZDB_OT_TOTAL + 1]; 2485 uint64_t zcb_dedup_asize; 2486 uint64_t zcb_dedup_blocks; 2487 uint64_t zcb_embedded_blocks[NUM_BP_EMBEDDED_TYPES]; 2488 uint64_t zcb_embedded_histogram[NUM_BP_EMBEDDED_TYPES] 2489 [BPE_PAYLOAD_SIZE]; 2490 uint64_t zcb_start; 2491 uint64_t zcb_lastprint; 2492 uint64_t zcb_totalasize; 2493 uint64_t zcb_errors[256]; 2494 int zcb_readfails; 2495 int zcb_haderrors; 2496 spa_t *zcb_spa; 2497 } zdb_cb_t; 2498 2499 static void 2500 zdb_count_block(zdb_cb_t *zcb, zilog_t *zilog, const blkptr_t *bp, 2501 dmu_object_type_t type) 2502 { 2503 uint64_t refcnt = 0; 2504 2505 ASSERT(type < ZDB_OT_TOTAL); 2506 2507 if (zilog && zil_bp_tree_add(zilog, bp) != 0) 2508 return; 2509 2510 for (int i = 0; i < 4; i++) { 2511 int l = (i < 2) ? BP_GET_LEVEL(bp) : ZB_TOTAL; 2512 int t = (i & 1) ? type : ZDB_OT_TOTAL; 2513 int equal; 2514 zdb_blkstats_t *zb = &zcb->zcb_type[l][t]; 2515 2516 zb->zb_asize += BP_GET_ASIZE(bp); 2517 zb->zb_lsize += BP_GET_LSIZE(bp); 2518 zb->zb_psize += BP_GET_PSIZE(bp); 2519 zb->zb_count++; 2520 2521 /* 2522 * The histogram is only big enough to record blocks up to 2523 * SPA_OLD_MAXBLOCKSIZE; larger blocks go into the last, 2524 * "other", bucket. 2525 */ 2526 int idx = BP_GET_PSIZE(bp) >> SPA_MINBLOCKSHIFT; 2527 idx = MIN(idx, SPA_OLD_MAXBLOCKSIZE / SPA_MINBLOCKSIZE + 1); 2528 zb->zb_psize_histogram[idx]++; 2529 2530 zb->zb_gangs += BP_COUNT_GANG(bp); 2531 2532 switch (BP_GET_NDVAS(bp)) { 2533 case 2: 2534 if (DVA_GET_VDEV(&bp->blk_dva[0]) == 2535 DVA_GET_VDEV(&bp->blk_dva[1])) 2536 zb->zb_ditto_samevdev++; 2537 break; 2538 case 3: 2539 equal = (DVA_GET_VDEV(&bp->blk_dva[0]) == 2540 DVA_GET_VDEV(&bp->blk_dva[1])) + 2541 (DVA_GET_VDEV(&bp->blk_dva[0]) == 2542 DVA_GET_VDEV(&bp->blk_dva[2])) + 2543 (DVA_GET_VDEV(&bp->blk_dva[1]) == 2544 DVA_GET_VDEV(&bp->blk_dva[2])); 2545 if (equal != 0) 2546 zb->zb_ditto_samevdev++; 2547 break; 2548 } 2549 2550 } 2551 2552 if (BP_IS_EMBEDDED(bp)) { 2553 zcb->zcb_embedded_blocks[BPE_GET_ETYPE(bp)]++; 2554 zcb->zcb_embedded_histogram[BPE_GET_ETYPE(bp)] 2555 [BPE_GET_PSIZE(bp)]++; 2556 return; 2557 } 2558 2559 if (dump_opt['L']) 2560 return; 2561 2562 if (BP_GET_DEDUP(bp)) { 2563 ddt_t *ddt; 2564 ddt_entry_t *dde; 2565 2566 ddt = ddt_select(zcb->zcb_spa, bp); 2567 ddt_enter(ddt); 2568 dde = ddt_lookup(ddt, bp, B_FALSE); 2569 2570 if (dde == NULL) { 2571 refcnt = 0; 2572 } else { 2573 ddt_phys_t *ddp = ddt_phys_select(dde, bp); 2574 ddt_phys_decref(ddp); 2575 refcnt = ddp->ddp_refcnt; 2576 if (ddt_phys_total_refcnt(dde) == 0) 2577 ddt_remove(ddt, dde); 2578 } 2579 ddt_exit(ddt); 2580 } 2581 2582 VERIFY3U(zio_wait(zio_claim(NULL, zcb->zcb_spa, 2583 refcnt ? 0 : spa_first_txg(zcb->zcb_spa), 2584 bp, NULL, NULL, ZIO_FLAG_CANFAIL)), ==, 0); 2585 } 2586 2587 static void 2588 zdb_blkptr_done(zio_t *zio) 2589 { 2590 spa_t *spa = zio->io_spa; 2591 blkptr_t *bp = zio->io_bp; 2592 int ioerr = zio->io_error; 2593 zdb_cb_t *zcb = zio->io_private; 2594 zbookmark_phys_t *zb = &zio->io_bookmark; 2595 2596 abd_free(zio->io_abd); 2597 2598 mutex_enter(&spa->spa_scrub_lock); 2599 spa->spa_scrub_inflight--; 2600 cv_broadcast(&spa->spa_scrub_io_cv); 2601 2602 if (ioerr && !(zio->io_flags & ZIO_FLAG_SPECULATIVE)) { 2603 char blkbuf[BP_SPRINTF_LEN]; 2604 2605 zcb->zcb_haderrors = 1; 2606 zcb->zcb_errors[ioerr]++; 2607 2608 if (dump_opt['b'] >= 2) 2609 snprintf_blkptr(blkbuf, sizeof (blkbuf), bp); 2610 else 2611 blkbuf[0] = '\0'; 2612 2613 (void) printf("zdb_blkptr_cb: " 2614 "Got error %d reading " 2615 "<%llu, %llu, %lld, %llx> %s -- skipping\n", 2616 ioerr, 2617 (u_longlong_t)zb->zb_objset, 2618 (u_longlong_t)zb->zb_object, 2619 (u_longlong_t)zb->zb_level, 2620 (u_longlong_t)zb->zb_blkid, 2621 blkbuf); 2622 } 2623 mutex_exit(&spa->spa_scrub_lock); 2624 } 2625 2626 static int 2627 zdb_blkptr_cb(spa_t *spa, zilog_t *zilog, const blkptr_t *bp, 2628 const zbookmark_phys_t *zb, const dnode_phys_t *dnp, void *arg) 2629 { 2630 zdb_cb_t *zcb = arg; 2631 dmu_object_type_t type; 2632 boolean_t is_metadata; 2633 2634 if (bp == NULL) 2635 return (0); 2636 2637 if (dump_opt['b'] >= 5 && bp->blk_birth > 0) { 2638 char blkbuf[BP_SPRINTF_LEN]; 2639 snprintf_blkptr(blkbuf, sizeof (blkbuf), bp); 2640 (void) printf("objset %llu object %llu " 2641 "level %lld offset 0x%llx %s\n", 2642 (u_longlong_t)zb->zb_objset, 2643 (u_longlong_t)zb->zb_object, 2644 (longlong_t)zb->zb_level, 2645 (u_longlong_t)blkid2offset(dnp, bp, zb), 2646 blkbuf); 2647 } 2648 2649 if (BP_IS_HOLE(bp)) 2650 return (0); 2651 2652 type = BP_GET_TYPE(bp); 2653 2654 zdb_count_block(zcb, zilog, bp, 2655 (type & DMU_OT_NEWTYPE) ? ZDB_OT_OTHER : type); 2656 2657 is_metadata = (BP_GET_LEVEL(bp) != 0 || DMU_OT_IS_METADATA(type)); 2658 2659 if (!BP_IS_EMBEDDED(bp) && 2660 (dump_opt['c'] > 1 || (dump_opt['c'] && is_metadata))) { 2661 size_t size = BP_GET_PSIZE(bp); 2662 abd_t *abd = abd_alloc(size, B_FALSE); 2663 int flags = ZIO_FLAG_CANFAIL | ZIO_FLAG_SCRUB | ZIO_FLAG_RAW; 2664 2665 /* If it's an intent log block, failure is expected. */ 2666 if (zb->zb_level == ZB_ZIL_LEVEL) 2667 flags |= ZIO_FLAG_SPECULATIVE; 2668 2669 mutex_enter(&spa->spa_scrub_lock); 2670 while (spa->spa_scrub_inflight > max_inflight) 2671 cv_wait(&spa->spa_scrub_io_cv, &spa->spa_scrub_lock); 2672 spa->spa_scrub_inflight++; 2673 mutex_exit(&spa->spa_scrub_lock); 2674 2675 zio_nowait(zio_read(NULL, spa, bp, abd, size, 2676 zdb_blkptr_done, zcb, ZIO_PRIORITY_ASYNC_READ, flags, zb)); 2677 } 2678 2679 zcb->zcb_readfails = 0; 2680 2681 /* only call gethrtime() every 100 blocks */ 2682 static int iters; 2683 if (++iters > 100) 2684 iters = 0; 2685 else 2686 return (0); 2687 2688 if (dump_opt['b'] < 5 && gethrtime() > zcb->zcb_lastprint + NANOSEC) { 2689 uint64_t now = gethrtime(); 2690 char buf[10]; 2691 uint64_t bytes = zcb->zcb_type[ZB_TOTAL][ZDB_OT_TOTAL].zb_asize; 2692 int kb_per_sec = 2693 1 + bytes / (1 + ((now - zcb->zcb_start) / 1000 / 1000)); 2694 int sec_remaining = 2695 (zcb->zcb_totalasize - bytes) / 1024 / kb_per_sec; 2696 2697 /* make sure nicenum has enough space */ 2698 CTASSERT(sizeof (buf) >= NN_NUMBUF_SZ); 2699 2700 zfs_nicenum(bytes, buf, sizeof (buf)); 2701 (void) fprintf(stderr, 2702 "\r%5s completed (%4dMB/s) " 2703 "estimated time remaining: %uhr %02umin %02usec ", 2704 buf, kb_per_sec / 1024, 2705 sec_remaining / 60 / 60, 2706 sec_remaining / 60 % 60, 2707 sec_remaining % 60); 2708 2709 zcb->zcb_lastprint = now; 2710 } 2711 2712 return (0); 2713 } 2714 2715 static void 2716 zdb_leak(void *arg, uint64_t start, uint64_t size) 2717 { 2718 vdev_t *vd = arg; 2719 2720 (void) printf("leaked space: vdev %llu, offset 0x%llx, size %llu\n", 2721 (u_longlong_t)vd->vdev_id, (u_longlong_t)start, (u_longlong_t)size); 2722 } 2723 2724 static metaslab_ops_t zdb_metaslab_ops = { 2725 NULL /* alloc */ 2726 }; 2727 2728 static void 2729 zdb_ddt_leak_init(spa_t *spa, zdb_cb_t *zcb) 2730 { 2731 ddt_bookmark_t ddb = { 0 }; 2732 ddt_entry_t dde; 2733 int error; 2734 2735 while ((error = ddt_walk(spa, &ddb, &dde)) == 0) { 2736 blkptr_t blk; 2737 ddt_phys_t *ddp = dde.dde_phys; 2738 2739 if (ddb.ddb_class == DDT_CLASS_UNIQUE) 2740 return; 2741 2742 ASSERT(ddt_phys_total_refcnt(&dde) > 1); 2743 2744 for (int p = 0; p < DDT_PHYS_TYPES; p++, ddp++) { 2745 if (ddp->ddp_phys_birth == 0) 2746 continue; 2747 ddt_bp_create(ddb.ddb_checksum, 2748 &dde.dde_key, ddp, &blk); 2749 if (p == DDT_PHYS_DITTO) { 2750 zdb_count_block(zcb, NULL, &blk, ZDB_OT_DITTO); 2751 } else { 2752 zcb->zcb_dedup_asize += 2753 BP_GET_ASIZE(&blk) * (ddp->ddp_refcnt - 1); 2754 zcb->zcb_dedup_blocks++; 2755 } 2756 } 2757 if (!dump_opt['L']) { 2758 ddt_t *ddt = spa->spa_ddt[ddb.ddb_checksum]; 2759 ddt_enter(ddt); 2760 VERIFY(ddt_lookup(ddt, &blk, B_TRUE) != NULL); 2761 ddt_exit(ddt); 2762 } 2763 } 2764 2765 ASSERT(error == ENOENT); 2766 } 2767 2768 static void 2769 zdb_leak_init(spa_t *spa, zdb_cb_t *zcb) 2770 { 2771 zcb->zcb_spa = spa; 2772 2773 if (!dump_opt['L']) { 2774 vdev_t *rvd = spa->spa_root_vdev; 2775 2776 /* 2777 * We are going to be changing the meaning of the metaslab's 2778 * ms_tree. Ensure that the allocator doesn't try to 2779 * use the tree. 2780 */ 2781 spa->spa_normal_class->mc_ops = &zdb_metaslab_ops; 2782 spa->spa_log_class->mc_ops = &zdb_metaslab_ops; 2783 2784 for (uint64_t c = 0; c < rvd->vdev_children; c++) { 2785 vdev_t *vd = rvd->vdev_child[c]; 2786 metaslab_group_t *mg = vd->vdev_mg; 2787 for (uint64_t m = 0; m < vd->vdev_ms_count; m++) { 2788 metaslab_t *msp = vd->vdev_ms[m]; 2789 ASSERT3P(msp->ms_group, ==, mg); 2790 mutex_enter(&msp->ms_lock); 2791 metaslab_unload(msp); 2792 2793 /* 2794 * For leak detection, we overload the metaslab 2795 * ms_tree to contain allocated segments 2796 * instead of free segments. As a result, 2797 * we can't use the normal metaslab_load/unload 2798 * interfaces. 2799 */ 2800 if (msp->ms_sm != NULL) { 2801 (void) fprintf(stderr, 2802 "\rloading space map for " 2803 "vdev %llu of %llu, " 2804 "metaslab %llu of %llu ...", 2805 (longlong_t)c, 2806 (longlong_t)rvd->vdev_children, 2807 (longlong_t)m, 2808 (longlong_t)vd->vdev_ms_count); 2809 2810 /* 2811 * We don't want to spend the CPU 2812 * manipulating the size-ordered 2813 * tree, so clear the range_tree 2814 * ops. 2815 */ 2816 msp->ms_tree->rt_ops = NULL; 2817 VERIFY0(space_map_load(msp->ms_sm, 2818 msp->ms_tree, SM_ALLOC)); 2819 2820 if (!msp->ms_loaded) { 2821 msp->ms_loaded = B_TRUE; 2822 } 2823 } 2824 mutex_exit(&msp->ms_lock); 2825 } 2826 } 2827 (void) fprintf(stderr, "\n"); 2828 } 2829 2830 spa_config_enter(spa, SCL_CONFIG, FTAG, RW_READER); 2831 2832 zdb_ddt_leak_init(spa, zcb); 2833 2834 spa_config_exit(spa, SCL_CONFIG, FTAG); 2835 } 2836 2837 static void 2838 zdb_leak_fini(spa_t *spa) 2839 { 2840 if (!dump_opt['L']) { 2841 vdev_t *rvd = spa->spa_root_vdev; 2842 for (int c = 0; c < rvd->vdev_children; c++) { 2843 vdev_t *vd = rvd->vdev_child[c]; 2844 metaslab_group_t *mg = vd->vdev_mg; 2845 for (int m = 0; m < vd->vdev_ms_count; m++) { 2846 metaslab_t *msp = vd->vdev_ms[m]; 2847 ASSERT3P(mg, ==, msp->ms_group); 2848 mutex_enter(&msp->ms_lock); 2849 2850 /* 2851 * The ms_tree has been overloaded to 2852 * contain allocated segments. Now that we 2853 * finished traversing all blocks, any 2854 * block that remains in the ms_tree 2855 * represents an allocated block that we 2856 * did not claim during the traversal. 2857 * Claimed blocks would have been removed 2858 * from the ms_tree. 2859 */ 2860 range_tree_vacate(msp->ms_tree, zdb_leak, vd); 2861 2862 if (msp->ms_loaded) { 2863 msp->ms_loaded = B_FALSE; 2864 } 2865 2866 mutex_exit(&msp->ms_lock); 2867 } 2868 } 2869 } 2870 } 2871 2872 /* ARGSUSED */ 2873 static int 2874 count_block_cb(void *arg, const blkptr_t *bp, dmu_tx_t *tx) 2875 { 2876 zdb_cb_t *zcb = arg; 2877 2878 if (dump_opt['b'] >= 5) { 2879 char blkbuf[BP_SPRINTF_LEN]; 2880 snprintf_blkptr(blkbuf, sizeof (blkbuf), bp); 2881 (void) printf("[%s] %s\n", 2882 "deferred free", blkbuf); 2883 } 2884 zdb_count_block(zcb, NULL, bp, ZDB_OT_DEFERRED); 2885 return (0); 2886 } 2887 2888 static int 2889 dump_block_stats(spa_t *spa) 2890 { 2891 zdb_cb_t zcb = { 0 }; 2892 zdb_blkstats_t *zb, *tzb; 2893 uint64_t norm_alloc, norm_space, total_alloc, total_found; 2894 int flags = TRAVERSE_PRE | TRAVERSE_PREFETCH_METADATA | TRAVERSE_HARD; 2895 boolean_t leaks = B_FALSE; 2896 2897 (void) printf("\nTraversing all blocks %s%s%s%s%s...\n\n", 2898 (dump_opt['c'] || !dump_opt['L']) ? "to verify " : "", 2899 (dump_opt['c'] == 1) ? "metadata " : "", 2900 dump_opt['c'] ? "checksums " : "", 2901 (dump_opt['c'] && !dump_opt['L']) ? "and verify " : "", 2902 !dump_opt['L'] ? "nothing leaked " : ""); 2903 2904 /* 2905 * Load all space maps as SM_ALLOC maps, then traverse the pool 2906 * claiming each block we discover. If the pool is perfectly 2907 * consistent, the space maps will be empty when we're done. 2908 * Anything left over is a leak; any block we can't claim (because 2909 * it's not part of any space map) is a double allocation, 2910 * reference to a freed block, or an unclaimed log block. 2911 */ 2912 zdb_leak_init(spa, &zcb); 2913 2914 /* 2915 * If there's a deferred-free bplist, process that first. 2916 */ 2917 (void) bpobj_iterate_nofree(&spa->spa_deferred_bpobj, 2918 count_block_cb, &zcb, NULL); 2919 if (spa_version(spa) >= SPA_VERSION_DEADLISTS) { 2920 (void) bpobj_iterate_nofree(&spa->spa_dsl_pool->dp_free_bpobj, 2921 count_block_cb, &zcb, NULL); 2922 } 2923 if (spa_feature_is_active(spa, SPA_FEATURE_ASYNC_DESTROY)) { 2924 VERIFY3U(0, ==, bptree_iterate(spa->spa_meta_objset, 2925 spa->spa_dsl_pool->dp_bptree_obj, B_FALSE, count_block_cb, 2926 &zcb, NULL)); 2927 } 2928 2929 if (dump_opt['c'] > 1) 2930 flags |= TRAVERSE_PREFETCH_DATA; 2931 2932 zcb.zcb_totalasize = metaslab_class_get_alloc(spa_normal_class(spa)); 2933 zcb.zcb_start = zcb.zcb_lastprint = gethrtime(); 2934 zcb.zcb_haderrors |= traverse_pool(spa, 0, flags, zdb_blkptr_cb, &zcb); 2935 2936 /* 2937 * If we've traversed the data blocks then we need to wait for those 2938 * I/Os to complete. We leverage "The Godfather" zio to wait on 2939 * all async I/Os to complete. 2940 */ 2941 if (dump_opt['c']) { 2942 for (int i = 0; i < max_ncpus; i++) { 2943 (void) zio_wait(spa->spa_async_zio_root[i]); 2944 spa->spa_async_zio_root[i] = zio_root(spa, NULL, NULL, 2945 ZIO_FLAG_CANFAIL | ZIO_FLAG_SPECULATIVE | 2946 ZIO_FLAG_GODFATHER); 2947 } 2948 } 2949 2950 if (zcb.zcb_haderrors) { 2951 (void) printf("\nError counts:\n\n"); 2952 (void) printf("\t%5s %s\n", "errno", "count"); 2953 for (int e = 0; e < 256; e++) { 2954 if (zcb.zcb_errors[e] != 0) { 2955 (void) printf("\t%5d %llu\n", 2956 e, (u_longlong_t)zcb.zcb_errors[e]); 2957 } 2958 } 2959 } 2960 2961 /* 2962 * Report any leaked segments. 2963 */ 2964 zdb_leak_fini(spa); 2965 2966 tzb = &zcb.zcb_type[ZB_TOTAL][ZDB_OT_TOTAL]; 2967 2968 norm_alloc = metaslab_class_get_alloc(spa_normal_class(spa)); 2969 norm_space = metaslab_class_get_space(spa_normal_class(spa)); 2970 2971 total_alloc = norm_alloc + metaslab_class_get_alloc(spa_log_class(spa)); 2972 total_found = tzb->zb_asize - zcb.zcb_dedup_asize; 2973 2974 if (total_found == total_alloc) { 2975 if (!dump_opt['L']) 2976 (void) printf("\n\tNo leaks (block sum matches space" 2977 " maps exactly)\n"); 2978 } else { 2979 (void) printf("block traversal size %llu != alloc %llu " 2980 "(%s %lld)\n", 2981 (u_longlong_t)total_found, 2982 (u_longlong_t)total_alloc, 2983 (dump_opt['L']) ? "unreachable" : "leaked", 2984 (longlong_t)(total_alloc - total_found)); 2985 leaks = B_TRUE; 2986 } 2987 2988 if (tzb->zb_count == 0) 2989 return (2); 2990 2991 (void) printf("\n"); 2992 (void) printf("\tbp count: %10llu\n", 2993 (u_longlong_t)tzb->zb_count); 2994 (void) printf("\tganged count: %10llu\n", 2995 (longlong_t)tzb->zb_gangs); 2996 (void) printf("\tbp logical: %10llu avg: %6llu\n", 2997 (u_longlong_t)tzb->zb_lsize, 2998 (u_longlong_t)(tzb->zb_lsize / tzb->zb_count)); 2999 (void) printf("\tbp physical: %10llu avg:" 3000 " %6llu compression: %6.2f\n", 3001 (u_longlong_t)tzb->zb_psize, 3002 (u_longlong_t)(tzb->zb_psize / tzb->zb_count), 3003 (double)tzb->zb_lsize / tzb->zb_psize); 3004 (void) printf("\tbp allocated: %10llu avg:" 3005 " %6llu compression: %6.2f\n", 3006 (u_longlong_t)tzb->zb_asize, 3007 (u_longlong_t)(tzb->zb_asize / tzb->zb_count), 3008 (double)tzb->zb_lsize / tzb->zb_asize); 3009 (void) printf("\tbp deduped: %10llu ref>1:" 3010 " %6llu deduplication: %6.2f\n", 3011 (u_longlong_t)zcb.zcb_dedup_asize, 3012 (u_longlong_t)zcb.zcb_dedup_blocks, 3013 (double)zcb.zcb_dedup_asize / tzb->zb_asize + 1.0); 3014 (void) printf("\tSPA allocated: %10llu used: %5.2f%%\n", 3015 (u_longlong_t)norm_alloc, 100.0 * norm_alloc / norm_space); 3016 3017 for (bp_embedded_type_t i = 0; i < NUM_BP_EMBEDDED_TYPES; i++) { 3018 if (zcb.zcb_embedded_blocks[i] == 0) 3019 continue; 3020 (void) printf("\n"); 3021 (void) printf("\tadditional, non-pointer bps of type %u: " 3022 "%10llu\n", 3023 i, (u_longlong_t)zcb.zcb_embedded_blocks[i]); 3024 3025 if (dump_opt['b'] >= 3) { 3026 (void) printf("\t number of (compressed) bytes: " 3027 "number of bps\n"); 3028 dump_histogram(zcb.zcb_embedded_histogram[i], 3029 sizeof (zcb.zcb_embedded_histogram[i]) / 3030 sizeof (zcb.zcb_embedded_histogram[i][0]), 0); 3031 } 3032 } 3033 3034 if (tzb->zb_ditto_samevdev != 0) { 3035 (void) printf("\tDittoed blocks on same vdev: %llu\n", 3036 (longlong_t)tzb->zb_ditto_samevdev); 3037 } 3038 3039 if (dump_opt['b'] >= 2) { 3040 int l, t, level; 3041 (void) printf("\nBlocks\tLSIZE\tPSIZE\tASIZE" 3042 "\t avg\t comp\t%%Total\tType\n"); 3043 3044 for (t = 0; t <= ZDB_OT_TOTAL; t++) { 3045 char csize[32], lsize[32], psize[32], asize[32]; 3046 char avg[32], gang[32]; 3047 char *typename; 3048 3049 /* make sure nicenum has enough space */ 3050 CTASSERT(sizeof (csize) >= NN_NUMBUF_SZ); 3051 CTASSERT(sizeof (lsize) >= NN_NUMBUF_SZ); 3052 CTASSERT(sizeof (psize) >= NN_NUMBUF_SZ); 3053 CTASSERT(sizeof (asize) >= NN_NUMBUF_SZ); 3054 CTASSERT(sizeof (avg) >= NN_NUMBUF_SZ); 3055 CTASSERT(sizeof (gang) >= NN_NUMBUF_SZ); 3056 3057 if (t < DMU_OT_NUMTYPES) 3058 typename = dmu_ot[t].ot_name; 3059 else 3060 typename = zdb_ot_extname[t - DMU_OT_NUMTYPES]; 3061 3062 if (zcb.zcb_type[ZB_TOTAL][t].zb_asize == 0) { 3063 (void) printf("%6s\t%5s\t%5s\t%5s" 3064 "\t%5s\t%5s\t%6s\t%s\n", 3065 "-", 3066 "-", 3067 "-", 3068 "-", 3069 "-", 3070 "-", 3071 "-", 3072 typename); 3073 continue; 3074 } 3075 3076 for (l = ZB_TOTAL - 1; l >= -1; l--) { 3077 level = (l == -1 ? ZB_TOTAL : l); 3078 zb = &zcb.zcb_type[level][t]; 3079 3080 if (zb->zb_asize == 0) 3081 continue; 3082 3083 if (dump_opt['b'] < 3 && level != ZB_TOTAL) 3084 continue; 3085 3086 if (level == 0 && zb->zb_asize == 3087 zcb.zcb_type[ZB_TOTAL][t].zb_asize) 3088 continue; 3089 3090 zdb_nicenum(zb->zb_count, csize, 3091 sizeof (csize)); 3092 zdb_nicenum(zb->zb_lsize, lsize, 3093 sizeof (lsize)); 3094 zdb_nicenum(zb->zb_psize, psize, 3095 sizeof (psize)); 3096 zdb_nicenum(zb->zb_asize, asize, 3097 sizeof (asize)); 3098 zdb_nicenum(zb->zb_asize / zb->zb_count, avg, 3099 sizeof (avg)); 3100 zdb_nicenum(zb->zb_gangs, gang, sizeof (gang)); 3101 3102 (void) printf("%6s\t%5s\t%5s\t%5s\t%5s" 3103 "\t%5.2f\t%6.2f\t", 3104 csize, lsize, psize, asize, avg, 3105 (double)zb->zb_lsize / zb->zb_psize, 3106 100.0 * zb->zb_asize / tzb->zb_asize); 3107 3108 if (level == ZB_TOTAL) 3109 (void) printf("%s\n", typename); 3110 else 3111 (void) printf(" L%d %s\n", 3112 level, typename); 3113 3114 if (dump_opt['b'] >= 3 && zb->zb_gangs > 0) { 3115 (void) printf("\t number of ganged " 3116 "blocks: %s\n", gang); 3117 } 3118 3119 if (dump_opt['b'] >= 4) { 3120 (void) printf("psize " 3121 "(in 512-byte sectors): " 3122 "number of blocks\n"); 3123 dump_histogram(zb->zb_psize_histogram, 3124 PSIZE_HISTO_SIZE, 0); 3125 } 3126 } 3127 } 3128 } 3129 3130 (void) printf("\n"); 3131 3132 if (leaks) 3133 return (2); 3134 3135 if (zcb.zcb_haderrors) 3136 return (3); 3137 3138 return (0); 3139 } 3140 3141 typedef struct zdb_ddt_entry { 3142 ddt_key_t zdde_key; 3143 uint64_t zdde_ref_blocks; 3144 uint64_t zdde_ref_lsize; 3145 uint64_t zdde_ref_psize; 3146 uint64_t zdde_ref_dsize; 3147 avl_node_t zdde_node; 3148 } zdb_ddt_entry_t; 3149 3150 /* ARGSUSED */ 3151 static int 3152 zdb_ddt_add_cb(spa_t *spa, zilog_t *zilog, const blkptr_t *bp, 3153 const zbookmark_phys_t *zb, const dnode_phys_t *dnp, void *arg) 3154 { 3155 avl_tree_t *t = arg; 3156 avl_index_t where; 3157 zdb_ddt_entry_t *zdde, zdde_search; 3158 3159 if (bp == NULL || BP_IS_HOLE(bp) || BP_IS_EMBEDDED(bp)) 3160 return (0); 3161 3162 if (dump_opt['S'] > 1 && zb->zb_level == ZB_ROOT_LEVEL) { 3163 (void) printf("traversing objset %llu, %llu objects, " 3164 "%lu blocks so far\n", 3165 (u_longlong_t)zb->zb_objset, 3166 (u_longlong_t)BP_GET_FILL(bp), 3167 avl_numnodes(t)); 3168 } 3169 3170 if (BP_IS_HOLE(bp) || BP_GET_CHECKSUM(bp) == ZIO_CHECKSUM_OFF || 3171 BP_GET_LEVEL(bp) > 0 || DMU_OT_IS_METADATA(BP_GET_TYPE(bp))) 3172 return (0); 3173 3174 ddt_key_fill(&zdde_search.zdde_key, bp); 3175 3176 zdde = avl_find(t, &zdde_search, &where); 3177 3178 if (zdde == NULL) { 3179 zdde = umem_zalloc(sizeof (*zdde), UMEM_NOFAIL); 3180 zdde->zdde_key = zdde_search.zdde_key; 3181 avl_insert(t, zdde, where); 3182 } 3183 3184 zdde->zdde_ref_blocks += 1; 3185 zdde->zdde_ref_lsize += BP_GET_LSIZE(bp); 3186 zdde->zdde_ref_psize += BP_GET_PSIZE(bp); 3187 zdde->zdde_ref_dsize += bp_get_dsize_sync(spa, bp); 3188 3189 return (0); 3190 } 3191 3192 static void 3193 dump_simulated_ddt(spa_t *spa) 3194 { 3195 avl_tree_t t; 3196 void *cookie = NULL; 3197 zdb_ddt_entry_t *zdde; 3198 ddt_histogram_t ddh_total = { 0 }; 3199 ddt_stat_t dds_total = { 0 }; 3200 3201 avl_create(&t, ddt_entry_compare, 3202 sizeof (zdb_ddt_entry_t), offsetof(zdb_ddt_entry_t, zdde_node)); 3203 3204 spa_config_enter(spa, SCL_CONFIG, FTAG, RW_READER); 3205 3206 (void) traverse_pool(spa, 0, TRAVERSE_PRE | TRAVERSE_PREFETCH_METADATA, 3207 zdb_ddt_add_cb, &t); 3208 3209 spa_config_exit(spa, SCL_CONFIG, FTAG); 3210 3211 while ((zdde = avl_destroy_nodes(&t, &cookie)) != NULL) { 3212 ddt_stat_t dds; 3213 uint64_t refcnt = zdde->zdde_ref_blocks; 3214 ASSERT(refcnt != 0); 3215 3216 dds.dds_blocks = zdde->zdde_ref_blocks / refcnt; 3217 dds.dds_lsize = zdde->zdde_ref_lsize / refcnt; 3218 dds.dds_psize = zdde->zdde_ref_psize / refcnt; 3219 dds.dds_dsize = zdde->zdde_ref_dsize / refcnt; 3220 3221 dds.dds_ref_blocks = zdde->zdde_ref_blocks; 3222 dds.dds_ref_lsize = zdde->zdde_ref_lsize; 3223 dds.dds_ref_psize = zdde->zdde_ref_psize; 3224 dds.dds_ref_dsize = zdde->zdde_ref_dsize; 3225 3226 ddt_stat_add(&ddh_total.ddh_stat[highbit64(refcnt) - 1], 3227 &dds, 0); 3228 3229 umem_free(zdde, sizeof (*zdde)); 3230 } 3231 3232 avl_destroy(&t); 3233 3234 ddt_histogram_stat(&dds_total, &ddh_total); 3235 3236 (void) printf("Simulated DDT histogram:\n"); 3237 3238 zpool_dump_ddt(&dds_total, &ddh_total); 3239 3240 dump_dedup_ratio(&dds_total); 3241 } 3242 3243 static void 3244 dump_zpool(spa_t *spa) 3245 { 3246 dsl_pool_t *dp = spa_get_dsl(spa); 3247 int rc = 0; 3248 3249 if (dump_opt['S']) { 3250 dump_simulated_ddt(spa); 3251 return; 3252 } 3253 3254 if (!dump_opt['e'] && dump_opt['C'] > 1) { 3255 (void) printf("\nCached configuration:\n"); 3256 dump_nvlist(spa->spa_config, 8); 3257 } 3258 3259 if (dump_opt['C']) 3260 dump_config(spa); 3261 3262 if (dump_opt['u']) 3263 dump_uberblock(&spa->spa_uberblock, "\nUberblock:\n", "\n"); 3264 3265 if (dump_opt['D']) 3266 dump_all_ddts(spa); 3267 3268 if (dump_opt['d'] > 2 || dump_opt['m']) 3269 dump_metaslabs(spa); 3270 if (dump_opt['M']) 3271 dump_metaslab_groups(spa); 3272 3273 if (dump_opt['d'] || dump_opt['i']) { 3274 dump_dir(dp->dp_meta_objset); 3275 if (dump_opt['d'] >= 3) { 3276 dump_full_bpobj(&spa->spa_deferred_bpobj, 3277 "Deferred frees", 0); 3278 if (spa_version(spa) >= SPA_VERSION_DEADLISTS) { 3279 dump_full_bpobj( 3280 &spa->spa_dsl_pool->dp_free_bpobj, 3281 "Pool snapshot frees", 0); 3282 } 3283 3284 if (spa_feature_is_active(spa, 3285 SPA_FEATURE_ASYNC_DESTROY)) { 3286 dump_bptree(spa->spa_meta_objset, 3287 spa->spa_dsl_pool->dp_bptree_obj, 3288 "Pool dataset frees"); 3289 } 3290 dump_dtl(spa->spa_root_vdev, 0); 3291 } 3292 (void) dmu_objset_find(spa_name(spa), dump_one_dir, 3293 NULL, DS_FIND_SNAPSHOTS | DS_FIND_CHILDREN); 3294 3295 for (spa_feature_t f = 0; f < SPA_FEATURES; f++) { 3296 uint64_t refcount; 3297 3298 if (!(spa_feature_table[f].fi_flags & 3299 ZFEATURE_FLAG_PER_DATASET) || 3300 !spa_feature_is_enabled(spa, f)) { 3301 ASSERT0(dataset_feature_count[f]); 3302 continue; 3303 } 3304 (void) feature_get_refcount(spa, 3305 &spa_feature_table[f], &refcount); 3306 if (dataset_feature_count[f] != refcount) { 3307 (void) printf("%s feature refcount mismatch: " 3308 "%lld datasets != %lld refcount\n", 3309 spa_feature_table[f].fi_uname, 3310 (longlong_t)dataset_feature_count[f], 3311 (longlong_t)refcount); 3312 rc = 2; 3313 } else { 3314 (void) printf("Verified %s feature refcount " 3315 "of %llu is correct\n", 3316 spa_feature_table[f].fi_uname, 3317 (longlong_t)refcount); 3318 } 3319 } 3320 } 3321 if (rc == 0 && (dump_opt['b'] || dump_opt['c'])) 3322 rc = dump_block_stats(spa); 3323 3324 if (rc == 0) 3325 rc = verify_spacemap_refcounts(spa); 3326 3327 if (dump_opt['s']) 3328 show_pool_stats(spa); 3329 3330 if (dump_opt['h']) 3331 dump_history(spa); 3332 3333 if (rc != 0) { 3334 dump_debug_buffer(); 3335 exit(rc); 3336 } 3337 } 3338 3339 #define ZDB_FLAG_CHECKSUM 0x0001 3340 #define ZDB_FLAG_DECOMPRESS 0x0002 3341 #define ZDB_FLAG_BSWAP 0x0004 3342 #define ZDB_FLAG_GBH 0x0008 3343 #define ZDB_FLAG_INDIRECT 0x0010 3344 #define ZDB_FLAG_PHYS 0x0020 3345 #define ZDB_FLAG_RAW 0x0040 3346 #define ZDB_FLAG_PRINT_BLKPTR 0x0080 3347 3348 int flagbits[256]; 3349 3350 static void 3351 zdb_print_blkptr(blkptr_t *bp, int flags) 3352 { 3353 char blkbuf[BP_SPRINTF_LEN]; 3354 3355 if (flags & ZDB_FLAG_BSWAP) 3356 byteswap_uint64_array((void *)bp, sizeof (blkptr_t)); 3357 3358 snprintf_blkptr(blkbuf, sizeof (blkbuf), bp); 3359 (void) printf("%s\n", blkbuf); 3360 } 3361 3362 static void 3363 zdb_dump_indirect(blkptr_t *bp, int nbps, int flags) 3364 { 3365 int i; 3366 3367 for (i = 0; i < nbps; i++) 3368 zdb_print_blkptr(&bp[i], flags); 3369 } 3370 3371 static void 3372 zdb_dump_gbh(void *buf, int flags) 3373 { 3374 zdb_dump_indirect((blkptr_t *)buf, SPA_GBH_NBLKPTRS, flags); 3375 } 3376 3377 static void 3378 zdb_dump_block_raw(void *buf, uint64_t size, int flags) 3379 { 3380 if (flags & ZDB_FLAG_BSWAP) 3381 byteswap_uint64_array(buf, size); 3382 (void) write(1, buf, size); 3383 } 3384 3385 static void 3386 zdb_dump_block(char *label, void *buf, uint64_t size, int flags) 3387 { 3388 uint64_t *d = (uint64_t *)buf; 3389 int nwords = size / sizeof (uint64_t); 3390 int do_bswap = !!(flags & ZDB_FLAG_BSWAP); 3391 int i, j; 3392 char *hdr, *c; 3393 3394 3395 if (do_bswap) 3396 hdr = " 7 6 5 4 3 2 1 0 f e d c b a 9 8"; 3397 else 3398 hdr = " 0 1 2 3 4 5 6 7 8 9 a b c d e f"; 3399 3400 (void) printf("\n%s\n%6s %s 0123456789abcdef\n", label, "", hdr); 3401 3402 for (i = 0; i < nwords; i += 2) { 3403 (void) printf("%06llx: %016llx %016llx ", 3404 (u_longlong_t)(i * sizeof (uint64_t)), 3405 (u_longlong_t)(do_bswap ? BSWAP_64(d[i]) : d[i]), 3406 (u_longlong_t)(do_bswap ? BSWAP_64(d[i + 1]) : d[i + 1])); 3407 3408 c = (char *)&d[i]; 3409 for (j = 0; j < 2 * sizeof (uint64_t); j++) 3410 (void) printf("%c", isprint(c[j]) ? c[j] : '.'); 3411 (void) printf("\n"); 3412 } 3413 } 3414 3415 /* 3416 * There are two acceptable formats: 3417 * leaf_name - For example: c1t0d0 or /tmp/ztest.0a 3418 * child[.child]* - For example: 0.1.1 3419 * 3420 * The second form can be used to specify arbitrary vdevs anywhere 3421 * in the heirarchy. For example, in a pool with a mirror of 3422 * RAID-Zs, you can specify either RAID-Z vdev with 0.0 or 0.1 . 3423 */ 3424 static vdev_t * 3425 zdb_vdev_lookup(vdev_t *vdev, char *path) 3426 { 3427 char *s, *p, *q; 3428 int i; 3429 3430 if (vdev == NULL) 3431 return (NULL); 3432 3433 /* First, assume the x.x.x.x format */ 3434 i = (int)strtoul(path, &s, 10); 3435 if (s == path || (s && *s != '.' && *s != '\0')) 3436 goto name; 3437 if (i < 0 || i >= vdev->vdev_children) 3438 return (NULL); 3439 3440 vdev = vdev->vdev_child[i]; 3441 if (*s == '\0') 3442 return (vdev); 3443 return (zdb_vdev_lookup(vdev, s+1)); 3444 3445 name: 3446 for (i = 0; i < vdev->vdev_children; i++) { 3447 vdev_t *vc = vdev->vdev_child[i]; 3448 3449 if (vc->vdev_path == NULL) { 3450 vc = zdb_vdev_lookup(vc, path); 3451 if (vc == NULL) 3452 continue; 3453 else 3454 return (vc); 3455 } 3456 3457 p = strrchr(vc->vdev_path, '/'); 3458 p = p ? p + 1 : vc->vdev_path; 3459 q = &vc->vdev_path[strlen(vc->vdev_path) - 2]; 3460 3461 if (strcmp(vc->vdev_path, path) == 0) 3462 return (vc); 3463 if (strcmp(p, path) == 0) 3464 return (vc); 3465 if (strcmp(q, "s0") == 0 && strncmp(p, path, q - p) == 0) 3466 return (vc); 3467 } 3468 3469 return (NULL); 3470 } 3471 3472 /* ARGSUSED */ 3473 static int 3474 random_get_pseudo_bytes_cb(void *buf, size_t len, void *unused) 3475 { 3476 return (random_get_pseudo_bytes(buf, len)); 3477 } 3478 3479 /* 3480 * Read a block from a pool and print it out. The syntax of the 3481 * block descriptor is: 3482 * 3483 * pool:vdev_specifier:offset:size[:flags] 3484 * 3485 * pool - The name of the pool you wish to read from 3486 * vdev_specifier - Which vdev (see comment for zdb_vdev_lookup) 3487 * offset - offset, in hex, in bytes 3488 * size - Amount of data to read, in hex, in bytes 3489 * flags - A string of characters specifying options 3490 * b: Decode a blkptr at given offset within block 3491 * *c: Calculate and display checksums 3492 * d: Decompress data before dumping 3493 * e: Byteswap data before dumping 3494 * g: Display data as a gang block header 3495 * i: Display as an indirect block 3496 * p: Do I/O to physical offset 3497 * r: Dump raw data to stdout 3498 * 3499 * * = not yet implemented 3500 */ 3501 static void 3502 zdb_read_block(char *thing, spa_t *spa) 3503 { 3504 blkptr_t blk, *bp = &blk; 3505 dva_t *dva = bp->blk_dva; 3506 int flags = 0; 3507 uint64_t offset = 0, size = 0, psize = 0, lsize = 0, blkptr_offset = 0; 3508 zio_t *zio; 3509 vdev_t *vd; 3510 abd_t *pabd; 3511 void *lbuf, *buf; 3512 char *s, *p, *dup, *vdev, *flagstr; 3513 int i, error; 3514 3515 dup = strdup(thing); 3516 s = strtok(dup, ":"); 3517 vdev = s ? s : ""; 3518 s = strtok(NULL, ":"); 3519 offset = strtoull(s ? s : "", NULL, 16); 3520 s = strtok(NULL, ":"); 3521 size = strtoull(s ? s : "", NULL, 16); 3522 s = strtok(NULL, ":"); 3523 flagstr = s ? s : ""; 3524 3525 s = NULL; 3526 if (size == 0) 3527 s = "size must not be zero"; 3528 if (!IS_P2ALIGNED(size, DEV_BSIZE)) 3529 s = "size must be a multiple of sector size"; 3530 if (!IS_P2ALIGNED(offset, DEV_BSIZE)) 3531 s = "offset must be a multiple of sector size"; 3532 if (s) { 3533 (void) printf("Invalid block specifier: %s - %s\n", thing, s); 3534 free(dup); 3535 return; 3536 } 3537 3538 for (s = strtok(flagstr, ":"); s; s = strtok(NULL, ":")) { 3539 for (i = 0; flagstr[i]; i++) { 3540 int bit = flagbits[(uchar_t)flagstr[i]]; 3541 3542 if (bit == 0) { 3543 (void) printf("***Invalid flag: %c\n", 3544 flagstr[i]); 3545 continue; 3546 } 3547 flags |= bit; 3548 3549 /* If it's not something with an argument, keep going */ 3550 if ((bit & (ZDB_FLAG_CHECKSUM | 3551 ZDB_FLAG_PRINT_BLKPTR)) == 0) 3552 continue; 3553 3554 p = &flagstr[i + 1]; 3555 if (bit == ZDB_FLAG_PRINT_BLKPTR) 3556 blkptr_offset = strtoull(p, &p, 16); 3557 if (*p != ':' && *p != '\0') { 3558 (void) printf("***Invalid flag arg: '%s'\n", s); 3559 free(dup); 3560 return; 3561 } 3562 } 3563 } 3564 3565 vd = zdb_vdev_lookup(spa->spa_root_vdev, vdev); 3566 if (vd == NULL) { 3567 (void) printf("***Invalid vdev: %s\n", vdev); 3568 free(dup); 3569 return; 3570 } else { 3571 if (vd->vdev_path) 3572 (void) fprintf(stderr, "Found vdev: %s\n", 3573 vd->vdev_path); 3574 else 3575 (void) fprintf(stderr, "Found vdev type: %s\n", 3576 vd->vdev_ops->vdev_op_type); 3577 } 3578 3579 psize = size; 3580 lsize = size; 3581 3582 pabd = abd_alloc_linear(SPA_MAXBLOCKSIZE, B_FALSE); 3583 lbuf = umem_alloc(SPA_MAXBLOCKSIZE, UMEM_NOFAIL); 3584 3585 BP_ZERO(bp); 3586 3587 DVA_SET_VDEV(&dva[0], vd->vdev_id); 3588 DVA_SET_OFFSET(&dva[0], offset); 3589 DVA_SET_GANG(&dva[0], !!(flags & ZDB_FLAG_GBH)); 3590 DVA_SET_ASIZE(&dva[0], vdev_psize_to_asize(vd, psize)); 3591 3592 BP_SET_BIRTH(bp, TXG_INITIAL, TXG_INITIAL); 3593 3594 BP_SET_LSIZE(bp, lsize); 3595 BP_SET_PSIZE(bp, psize); 3596 BP_SET_COMPRESS(bp, ZIO_COMPRESS_OFF); 3597 BP_SET_CHECKSUM(bp, ZIO_CHECKSUM_OFF); 3598 BP_SET_TYPE(bp, DMU_OT_NONE); 3599 BP_SET_LEVEL(bp, 0); 3600 BP_SET_DEDUP(bp, 0); 3601 BP_SET_BYTEORDER(bp, ZFS_HOST_BYTEORDER); 3602 3603 spa_config_enter(spa, SCL_STATE, FTAG, RW_READER); 3604 zio = zio_root(spa, NULL, NULL, 0); 3605 3606 if (vd == vd->vdev_top) { 3607 /* 3608 * Treat this as a normal block read. 3609 */ 3610 zio_nowait(zio_read(zio, spa, bp, pabd, psize, NULL, NULL, 3611 ZIO_PRIORITY_SYNC_READ, 3612 ZIO_FLAG_CANFAIL | ZIO_FLAG_RAW, NULL)); 3613 } else { 3614 /* 3615 * Treat this as a vdev child I/O. 3616 */ 3617 zio_nowait(zio_vdev_child_io(zio, bp, vd, offset, pabd, 3618 psize, ZIO_TYPE_READ, ZIO_PRIORITY_SYNC_READ, 3619 ZIO_FLAG_DONT_CACHE | ZIO_FLAG_DONT_QUEUE | 3620 ZIO_FLAG_DONT_PROPAGATE | ZIO_FLAG_DONT_RETRY | 3621 ZIO_FLAG_CANFAIL | ZIO_FLAG_RAW, NULL, NULL)); 3622 } 3623 3624 error = zio_wait(zio); 3625 spa_config_exit(spa, SCL_STATE, FTAG); 3626 3627 if (error) { 3628 (void) printf("Read of %s failed, error: %d\n", thing, error); 3629 goto out; 3630 } 3631 3632 if (flags & ZDB_FLAG_DECOMPRESS) { 3633 /* 3634 * We don't know how the data was compressed, so just try 3635 * every decompress function at every inflated blocksize. 3636 */ 3637 enum zio_compress c; 3638 void *pbuf2 = umem_alloc(SPA_MAXBLOCKSIZE, UMEM_NOFAIL); 3639 void *lbuf2 = umem_alloc(SPA_MAXBLOCKSIZE, UMEM_NOFAIL); 3640 3641 abd_copy_to_buf(pbuf2, pabd, psize); 3642 3643 VERIFY0(abd_iterate_func(pabd, psize, SPA_MAXBLOCKSIZE - psize, 3644 random_get_pseudo_bytes_cb, NULL)); 3645 3646 VERIFY0(random_get_pseudo_bytes((uint8_t *)pbuf2 + psize, 3647 SPA_MAXBLOCKSIZE - psize)); 3648 3649 for (lsize = SPA_MAXBLOCKSIZE; lsize > psize; 3650 lsize -= SPA_MINBLOCKSIZE) { 3651 for (c = 0; c < ZIO_COMPRESS_FUNCTIONS; c++) { 3652 if (zio_decompress_data(c, pabd, 3653 lbuf, psize, lsize) == 0 && 3654 zio_decompress_data_buf(c, pbuf2, 3655 lbuf2, psize, lsize) == 0 && 3656 bcmp(lbuf, lbuf2, lsize) == 0) 3657 break; 3658 } 3659 if (c != ZIO_COMPRESS_FUNCTIONS) 3660 break; 3661 lsize -= SPA_MINBLOCKSIZE; 3662 } 3663 3664 umem_free(pbuf2, SPA_MAXBLOCKSIZE); 3665 umem_free(lbuf2, SPA_MAXBLOCKSIZE); 3666 3667 if (lsize <= psize) { 3668 (void) printf("Decompress of %s failed\n", thing); 3669 goto out; 3670 } 3671 buf = lbuf; 3672 size = lsize; 3673 } else { 3674 buf = abd_to_buf(pabd); 3675 size = psize; 3676 } 3677 3678 if (flags & ZDB_FLAG_PRINT_BLKPTR) 3679 zdb_print_blkptr((blkptr_t *)(void *) 3680 ((uintptr_t)buf + (uintptr_t)blkptr_offset), flags); 3681 else if (flags & ZDB_FLAG_RAW) 3682 zdb_dump_block_raw(buf, size, flags); 3683 else if (flags & ZDB_FLAG_INDIRECT) 3684 zdb_dump_indirect((blkptr_t *)buf, size / sizeof (blkptr_t), 3685 flags); 3686 else if (flags & ZDB_FLAG_GBH) 3687 zdb_dump_gbh(buf, flags); 3688 else 3689 zdb_dump_block(thing, buf, size, flags); 3690 3691 out: 3692 abd_free(pabd); 3693 umem_free(lbuf, SPA_MAXBLOCKSIZE); 3694 free(dup); 3695 } 3696 3697 static void 3698 zdb_embedded_block(char *thing) 3699 { 3700 blkptr_t bp = { 0 }; 3701 unsigned long long *words = (void *)&bp; 3702 char buf[SPA_MAXBLOCKSIZE]; 3703 int err; 3704 3705 err = sscanf(thing, "%llx:%llx:%llx:%llx:%llx:%llx:%llx:%llx:" 3706 "%llx:%llx:%llx:%llx:%llx:%llx:%llx:%llx", 3707 words + 0, words + 1, words + 2, words + 3, 3708 words + 4, words + 5, words + 6, words + 7, 3709 words + 8, words + 9, words + 10, words + 11, 3710 words + 12, words + 13, words + 14, words + 15); 3711 if (err != 16) { 3712 (void) printf("invalid input format\n"); 3713 exit(1); 3714 } 3715 ASSERT3U(BPE_GET_LSIZE(&bp), <=, SPA_MAXBLOCKSIZE); 3716 err = decode_embedded_bp(&bp, buf, BPE_GET_LSIZE(&bp)); 3717 if (err != 0) { 3718 (void) printf("decode failed: %u\n", err); 3719 exit(1); 3720 } 3721 zdb_dump_block_raw(buf, BPE_GET_LSIZE(&bp), 0); 3722 } 3723 3724 static boolean_t 3725 pool_match(nvlist_t *cfg, char *tgt) 3726 { 3727 uint64_t v, guid = strtoull(tgt, NULL, 0); 3728 char *s; 3729 3730 if (guid != 0) { 3731 if (nvlist_lookup_uint64(cfg, ZPOOL_CONFIG_POOL_GUID, &v) == 0) 3732 return (v == guid); 3733 } else { 3734 if (nvlist_lookup_string(cfg, ZPOOL_CONFIG_POOL_NAME, &s) == 0) 3735 return (strcmp(s, tgt) == 0); 3736 } 3737 return (B_FALSE); 3738 } 3739 3740 static char * 3741 find_zpool(char **target, nvlist_t **configp, int dirc, char **dirv) 3742 { 3743 nvlist_t *pools; 3744 nvlist_t *match = NULL; 3745 char *name = NULL; 3746 char *sepp = NULL; 3747 char sep = '\0'; 3748 int count = 0; 3749 importargs_t args = { 0 }; 3750 3751 args.paths = dirc; 3752 args.path = dirv; 3753 args.can_be_active = B_TRUE; 3754 3755 if ((sepp = strpbrk(*target, "/@")) != NULL) { 3756 sep = *sepp; 3757 *sepp = '\0'; 3758 } 3759 3760 pools = zpool_search_import(g_zfs, &args); 3761 3762 if (pools != NULL) { 3763 nvpair_t *elem = NULL; 3764 while ((elem = nvlist_next_nvpair(pools, elem)) != NULL) { 3765 verify(nvpair_value_nvlist(elem, configp) == 0); 3766 if (pool_match(*configp, *target)) { 3767 count++; 3768 if (match != NULL) { 3769 /* print previously found config */ 3770 if (name != NULL) { 3771 (void) printf("%s\n", name); 3772 dump_nvlist(match, 8); 3773 name = NULL; 3774 } 3775 (void) printf("%s\n", 3776 nvpair_name(elem)); 3777 dump_nvlist(*configp, 8); 3778 } else { 3779 match = *configp; 3780 name = nvpair_name(elem); 3781 } 3782 } 3783 } 3784 } 3785 if (count > 1) 3786 (void) fatal("\tMatched %d pools - use pool GUID " 3787 "instead of pool name or \n" 3788 "\tpool name part of a dataset name to select pool", count); 3789 3790 if (sepp) 3791 *sepp = sep; 3792 /* 3793 * If pool GUID was specified for pool id, replace it with pool name 3794 */ 3795 if (name && (strstr(*target, name) != *target)) { 3796 int sz = 1 + strlen(name) + ((sepp) ? strlen(sepp) : 0); 3797 3798 *target = umem_alloc(sz, UMEM_NOFAIL); 3799 (void) snprintf(*target, sz, "%s%s", name, sepp ? sepp : ""); 3800 } 3801 3802 *configp = name ? match : NULL; 3803 3804 return (name); 3805 } 3806 3807 int 3808 main(int argc, char **argv) 3809 { 3810 int i, c; 3811 struct rlimit rl = { 1024, 1024 }; 3812 spa_t *spa = NULL; 3813 objset_t *os = NULL; 3814 int dump_all = 1; 3815 int verbose = 0; 3816 int error = 0; 3817 char **searchdirs = NULL; 3818 int nsearch = 0; 3819 char *target; 3820 nvlist_t *policy = NULL; 3821 uint64_t max_txg = UINT64_MAX; 3822 int flags = ZFS_IMPORT_MISSING_LOG; 3823 int rewind = ZPOOL_NEVER_REWIND; 3824 char *spa_config_path_env; 3825 boolean_t target_is_spa = B_TRUE; 3826 3827 (void) setrlimit(RLIMIT_NOFILE, &rl); 3828 (void) enable_extended_FILE_stdio(-1, -1); 3829 3830 dprintf_setup(&argc, argv); 3831 3832 /* 3833 * If there is an environment variable SPA_CONFIG_PATH it overrides 3834 * default spa_config_path setting. If -U flag is specified it will 3835 * override this environment variable settings once again. 3836 */ 3837 spa_config_path_env = getenv("SPA_CONFIG_PATH"); 3838 if (spa_config_path_env != NULL) 3839 spa_config_path = spa_config_path_env; 3840 3841 while ((c = getopt(argc, argv, 3842 "AbcCdDeEFGhiI:lLmMo:Op:PqRsSt:uU:vVx:X")) != -1) { 3843 switch (c) { 3844 case 'b': 3845 case 'c': 3846 case 'C': 3847 case 'd': 3848 case 'D': 3849 case 'E': 3850 case 'G': 3851 case 'h': 3852 case 'i': 3853 case 'l': 3854 case 'm': 3855 case 'M': 3856 case 'O': 3857 case 'R': 3858 case 's': 3859 case 'S': 3860 case 'u': 3861 dump_opt[c]++; 3862 dump_all = 0; 3863 break; 3864 case 'A': 3865 case 'e': 3866 case 'F': 3867 case 'L': 3868 case 'P': 3869 case 'q': 3870 case 'X': 3871 dump_opt[c]++; 3872 break; 3873 /* NB: Sort single match options below. */ 3874 case 'I': 3875 max_inflight = strtoull(optarg, NULL, 0); 3876 if (max_inflight == 0) { 3877 (void) fprintf(stderr, "maximum number " 3878 "of inflight I/Os must be greater " 3879 "than 0\n"); 3880 usage(); 3881 } 3882 break; 3883 case 'o': 3884 error = set_global_var(optarg); 3885 if (error != 0) 3886 usage(); 3887 break; 3888 case 'p': 3889 if (searchdirs == NULL) { 3890 searchdirs = umem_alloc(sizeof (char *), 3891 UMEM_NOFAIL); 3892 } else { 3893 char **tmp = umem_alloc((nsearch + 1) * 3894 sizeof (char *), UMEM_NOFAIL); 3895 bcopy(searchdirs, tmp, nsearch * 3896 sizeof (char *)); 3897 umem_free(searchdirs, 3898 nsearch * sizeof (char *)); 3899 searchdirs = tmp; 3900 } 3901 searchdirs[nsearch++] = optarg; 3902 break; 3903 case 't': 3904 max_txg = strtoull(optarg, NULL, 0); 3905 if (max_txg < TXG_INITIAL) { 3906 (void) fprintf(stderr, "incorrect txg " 3907 "specified: %s\n", optarg); 3908 usage(); 3909 } 3910 break; 3911 case 'U': 3912 spa_config_path = optarg; 3913 if (spa_config_path[0] != '/') { 3914 (void) fprintf(stderr, 3915 "cachefile must be an absolute path " 3916 "(i.e. start with a slash)\n"); 3917 usage(); 3918 } 3919 break; 3920 case 'v': 3921 verbose++; 3922 break; 3923 case 'V': 3924 flags = ZFS_IMPORT_VERBATIM; 3925 break; 3926 case 'x': 3927 vn_dumpdir = optarg; 3928 break; 3929 default: 3930 usage(); 3931 break; 3932 } 3933 } 3934 3935 if (!dump_opt['e'] && searchdirs != NULL) { 3936 (void) fprintf(stderr, "-p option requires use of -e\n"); 3937 usage(); 3938 } 3939 3940 /* 3941 * ZDB does not typically re-read blocks; therefore limit the ARC 3942 * to 256 MB, which can be used entirely for metadata. 3943 */ 3944 zfs_arc_max = zfs_arc_meta_limit = 256 * 1024 * 1024; 3945 3946 /* 3947 * "zdb -c" uses checksum-verifying scrub i/os which are async reads. 3948 * "zdb -b" uses traversal prefetch which uses async reads. 3949 * For good performance, let several of them be active at once. 3950 */ 3951 zfs_vdev_async_read_max_active = 10; 3952 3953 /* 3954 * Disable reference tracking for better performance. 3955 */ 3956 reference_tracking_enable = B_FALSE; 3957 3958 kernel_init(FREAD); 3959 g_zfs = libzfs_init(); 3960 ASSERT(g_zfs != NULL); 3961 3962 if (dump_all) 3963 verbose = MAX(verbose, 1); 3964 3965 for (c = 0; c < 256; c++) { 3966 if (dump_all && strchr("AeEFlLOPRSX", c) == NULL) 3967 dump_opt[c] = 1; 3968 if (dump_opt[c]) 3969 dump_opt[c] += verbose; 3970 } 3971 3972 aok = (dump_opt['A'] == 1) || (dump_opt['A'] > 2); 3973 zfs_recover = (dump_opt['A'] > 1); 3974 3975 argc -= optind; 3976 argv += optind; 3977 3978 if (argc < 2 && dump_opt['R']) 3979 usage(); 3980 3981 if (dump_opt['E']) { 3982 if (argc != 1) 3983 usage(); 3984 zdb_embedded_block(argv[0]); 3985 return (0); 3986 } 3987 3988 if (argc < 1) { 3989 if (!dump_opt['e'] && dump_opt['C']) { 3990 dump_cachefile(spa_config_path); 3991 return (0); 3992 } 3993 usage(); 3994 } 3995 3996 if (dump_opt['l']) 3997 return (dump_label(argv[0])); 3998 3999 if (dump_opt['O']) { 4000 if (argc != 2) 4001 usage(); 4002 dump_opt['v'] = verbose + 3; 4003 return (dump_path(argv[0], argv[1])); 4004 } 4005 4006 if (dump_opt['X'] || dump_opt['F']) 4007 rewind = ZPOOL_DO_REWIND | 4008 (dump_opt['X'] ? ZPOOL_EXTREME_REWIND : 0); 4009 4010 if (nvlist_alloc(&policy, NV_UNIQUE_NAME_TYPE, 0) != 0 || 4011 nvlist_add_uint64(policy, ZPOOL_REWIND_REQUEST_TXG, max_txg) != 0 || 4012 nvlist_add_uint32(policy, ZPOOL_REWIND_REQUEST, rewind) != 0) 4013 fatal("internal error: %s", strerror(ENOMEM)); 4014 4015 error = 0; 4016 target = argv[0]; 4017 4018 if (dump_opt['e']) { 4019 nvlist_t *cfg = NULL; 4020 char *name = find_zpool(&target, &cfg, nsearch, searchdirs); 4021 4022 error = ENOENT; 4023 if (name) { 4024 if (dump_opt['C'] > 1) { 4025 (void) printf("\nConfiguration for import:\n"); 4026 dump_nvlist(cfg, 8); 4027 } 4028 if (nvlist_add_nvlist(cfg, 4029 ZPOOL_REWIND_POLICY, policy) != 0) { 4030 fatal("can't open '%s': %s", 4031 target, strerror(ENOMEM)); 4032 } 4033 error = spa_import(name, cfg, NULL, flags); 4034 } 4035 } 4036 4037 if (strpbrk(target, "/@") != NULL) { 4038 size_t targetlen; 4039 4040 target_is_spa = B_FALSE; 4041 /* 4042 * Remove any trailing slash. Later code would get confused 4043 * by it, but we want to allow it so that "pool/" can 4044 * indicate that we want to dump the topmost filesystem, 4045 * rather than the whole pool. 4046 */ 4047 targetlen = strlen(target); 4048 if (targetlen != 0 && target[targetlen - 1] == '/') 4049 target[targetlen - 1] = '\0'; 4050 } 4051 4052 if (error == 0) { 4053 if (target_is_spa || dump_opt['R']) { 4054 error = spa_open_rewind(target, &spa, FTAG, policy, 4055 NULL); 4056 if (error) { 4057 /* 4058 * If we're missing the log device then 4059 * try opening the pool after clearing the 4060 * log state. 4061 */ 4062 mutex_enter(&spa_namespace_lock); 4063 if ((spa = spa_lookup(target)) != NULL && 4064 spa->spa_log_state == SPA_LOG_MISSING) { 4065 spa->spa_log_state = SPA_LOG_CLEAR; 4066 error = 0; 4067 } 4068 mutex_exit(&spa_namespace_lock); 4069 4070 if (!error) { 4071 error = spa_open_rewind(target, &spa, 4072 FTAG, policy, NULL); 4073 } 4074 } 4075 } else { 4076 error = open_objset(target, DMU_OST_ANY, FTAG, &os); 4077 } 4078 } 4079 nvlist_free(policy); 4080 4081 if (error) 4082 fatal("can't open '%s': %s", target, strerror(error)); 4083 4084 argv++; 4085 argc--; 4086 if (!dump_opt['R']) { 4087 if (argc > 0) { 4088 zopt_objects = argc; 4089 zopt_object = calloc(zopt_objects, sizeof (uint64_t)); 4090 for (i = 0; i < zopt_objects; i++) { 4091 errno = 0; 4092 zopt_object[i] = strtoull(argv[i], NULL, 0); 4093 if (zopt_object[i] == 0 && errno != 0) 4094 fatal("bad number %s: %s", 4095 argv[i], strerror(errno)); 4096 } 4097 } 4098 if (os != NULL) { 4099 dump_dir(os); 4100 } else if (zopt_objects > 0 && !dump_opt['m']) { 4101 dump_dir(spa->spa_meta_objset); 4102 } else { 4103 dump_zpool(spa); 4104 } 4105 } else { 4106 flagbits['b'] = ZDB_FLAG_PRINT_BLKPTR; 4107 flagbits['c'] = ZDB_FLAG_CHECKSUM; 4108 flagbits['d'] = ZDB_FLAG_DECOMPRESS; 4109 flagbits['e'] = ZDB_FLAG_BSWAP; 4110 flagbits['g'] = ZDB_FLAG_GBH; 4111 flagbits['i'] = ZDB_FLAG_INDIRECT; 4112 flagbits['p'] = ZDB_FLAG_PHYS; 4113 flagbits['r'] = ZDB_FLAG_RAW; 4114 4115 for (i = 0; i < argc; i++) 4116 zdb_read_block(argv[i], spa); 4117 } 4118 4119 if (os != NULL) 4120 close_objset(os, FTAG); 4121 else 4122 spa_close(spa, FTAG); 4123 4124 fuid_table_destroy(); 4125 4126 dump_debug_buffer(); 4127 4128 libzfs_fini(g_zfs); 4129 kernel_fini(); 4130 4131 return (0); 4132 } 4133