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