1 /* 2 * CDDL HEADER START 3 * 4 * The contents of this file are subject to the terms of the 5 * Common Development and Distribution License (the "License"). 6 * You may not use this file except in compliance with the License. 7 * 8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 9 * or http://www.opensolaris.org/os/licensing. 10 * See the License for the specific language governing permissions 11 * and limitations under the License. 12 * 13 * When distributing Covered Code, include this CDDL HEADER in each 14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 15 * If applicable, add the following below this CDDL HEADER, with the 16 * fields enclosed by brackets "[]" replaced with your own identifying 17 * information: Portions Copyright [yyyy] [name of copyright owner] 18 * 19 * CDDL HEADER END 20 */ 21 /* 22 * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved. 23 * Copyright 2011 Nexenta Systems, Inc. All rights reserved. 24 * Copyright (c) 2011, 2016 by Delphix. All rights reserved. 25 * Copyright (c) 2017, Joyent, Inc. All rights reserved. 26 */ 27 28 /* Portions Copyright 2010 Robert Milkowski */ 29 30 #include <mdb/mdb_ctf.h> 31 #include <sys/zfs_context.h> 32 #include <sys/mdb_modapi.h> 33 #include <sys/dbuf.h> 34 #include <sys/dmu_objset.h> 35 #include <sys/dsl_dir.h> 36 #include <sys/dsl_pool.h> 37 #include <sys/metaslab_impl.h> 38 #include <sys/space_map.h> 39 #include <sys/list.h> 40 #include <sys/vdev_impl.h> 41 #include <sys/zap_leaf.h> 42 #include <sys/zap_impl.h> 43 #include <ctype.h> 44 #include <sys/zfs_acl.h> 45 #include <sys/sa_impl.h> 46 #include <sys/multilist.h> 47 48 #ifdef _KERNEL 49 #define ZFS_OBJ_NAME "zfs" 50 extern int64_t mdb_gethrtime(void); 51 #else 52 #define ZFS_OBJ_NAME "libzpool.so.1" 53 #endif 54 55 #define ZFS_STRUCT "struct " ZFS_OBJ_NAME "`" 56 57 #ifndef _KERNEL 58 int aok; 59 #endif 60 61 enum spa_flags { 62 SPA_FLAG_CONFIG = 1 << 0, 63 SPA_FLAG_VDEVS = 1 << 1, 64 SPA_FLAG_ERRORS = 1 << 2, 65 SPA_FLAG_METASLAB_GROUPS = 1 << 3, 66 SPA_FLAG_METASLABS = 1 << 4, 67 SPA_FLAG_HISTOGRAMS = 1 << 5 68 }; 69 70 /* 71 * If any of these flags are set, call spa_vdevs in spa_print 72 */ 73 #define SPA_FLAG_ALL_VDEV \ 74 (SPA_FLAG_VDEVS | SPA_FLAG_ERRORS | SPA_FLAG_METASLAB_GROUPS | \ 75 SPA_FLAG_METASLABS) 76 77 static int 78 getmember(uintptr_t addr, const char *type, mdb_ctf_id_t *idp, 79 const char *member, int len, void *buf) 80 { 81 mdb_ctf_id_t id; 82 ulong_t off; 83 char name[64]; 84 85 if (idp == NULL) { 86 if (mdb_ctf_lookup_by_name(type, &id) == -1) { 87 mdb_warn("couldn't find type %s", type); 88 return (DCMD_ERR); 89 } 90 idp = &id; 91 } else { 92 type = name; 93 mdb_ctf_type_name(*idp, name, sizeof (name)); 94 } 95 96 if (mdb_ctf_offsetof(*idp, member, &off) == -1) { 97 mdb_warn("couldn't find member %s of type %s\n", member, type); 98 return (DCMD_ERR); 99 } 100 if (off % 8 != 0) { 101 mdb_warn("member %s of type %s is unsupported bitfield", 102 member, type); 103 return (DCMD_ERR); 104 } 105 off /= 8; 106 107 if (mdb_vread(buf, len, addr + off) == -1) { 108 mdb_warn("failed to read %s from %s at %p", 109 member, type, addr + off); 110 return (DCMD_ERR); 111 } 112 /* mdb_warn("read %s from %s at %p+%llx\n", member, type, addr, off); */ 113 114 return (0); 115 } 116 117 #define GETMEMB(addr, structname, member, dest) \ 118 getmember(addr, ZFS_STRUCT structname, NULL, #member, \ 119 sizeof (dest), &(dest)) 120 121 #define GETMEMBID(addr, ctfid, member, dest) \ 122 getmember(addr, NULL, ctfid, #member, sizeof (dest), &(dest)) 123 124 static boolean_t 125 strisprint(const char *cp) 126 { 127 for (; *cp; cp++) { 128 if (!isprint(*cp)) 129 return (B_FALSE); 130 } 131 return (B_TRUE); 132 } 133 134 #define NICENUM_BUFLEN 6 135 136 static int 137 snprintfrac(char *buf, int len, 138 uint64_t numerator, uint64_t denom, int frac_digits) 139 { 140 int mul = 1; 141 int whole, frac, i; 142 143 for (i = frac_digits; i; i--) 144 mul *= 10; 145 whole = numerator / denom; 146 frac = mul * numerator / denom - mul * whole; 147 return (mdb_snprintf(buf, len, "%u.%0*u", whole, frac_digits, frac)); 148 } 149 150 static void 151 mdb_nicenum(uint64_t num, char *buf) 152 { 153 uint64_t n = num; 154 int index = 0; 155 char *u; 156 157 while (n >= 1024) { 158 n = (n + (1024 / 2)) / 1024; /* Round up or down */ 159 index++; 160 } 161 162 u = &" \0K\0M\0G\0T\0P\0E\0"[index*2]; 163 164 if (index == 0) { 165 (void) mdb_snprintf(buf, NICENUM_BUFLEN, "%llu", 166 (u_longlong_t)n); 167 } else if (n < 10 && (num & (num - 1)) != 0) { 168 (void) snprintfrac(buf, NICENUM_BUFLEN, 169 num, 1ULL << 10 * index, 2); 170 strcat(buf, u); 171 } else if (n < 100 && (num & (num - 1)) != 0) { 172 (void) snprintfrac(buf, NICENUM_BUFLEN, 173 num, 1ULL << 10 * index, 1); 174 strcat(buf, u); 175 } else { 176 (void) mdb_snprintf(buf, NICENUM_BUFLEN, "%llu%s", 177 (u_longlong_t)n, u); 178 } 179 } 180 181 static int verbose; 182 183 static int 184 freelist_walk_init(mdb_walk_state_t *wsp) 185 { 186 if (wsp->walk_addr == NULL) { 187 mdb_warn("must supply starting address\n"); 188 return (WALK_ERR); 189 } 190 191 wsp->walk_data = 0; /* Index into the freelist */ 192 return (WALK_NEXT); 193 } 194 195 static int 196 freelist_walk_step(mdb_walk_state_t *wsp) 197 { 198 uint64_t entry; 199 uintptr_t number = (uintptr_t)wsp->walk_data; 200 char *ddata[] = { "ALLOC", "FREE", "CONDENSE", "INVALID", 201 "INVALID", "INVALID", "INVALID", "INVALID" }; 202 int mapshift = SPA_MINBLOCKSHIFT; 203 204 if (mdb_vread(&entry, sizeof (entry), wsp->walk_addr) == -1) { 205 mdb_warn("failed to read freelist entry %p", wsp->walk_addr); 206 return (WALK_DONE); 207 } 208 wsp->walk_addr += sizeof (entry); 209 wsp->walk_data = (void *)(number + 1); 210 211 if (SM_DEBUG_DECODE(entry)) { 212 mdb_printf("DEBUG: %3u %10s: txg=%llu pass=%llu\n", 213 number, 214 ddata[SM_DEBUG_ACTION_DECODE(entry)], 215 SM_DEBUG_TXG_DECODE(entry), 216 SM_DEBUG_SYNCPASS_DECODE(entry)); 217 } else { 218 mdb_printf("Entry: %3u offsets=%08llx-%08llx type=%c " 219 "size=%06llx", number, 220 SM_OFFSET_DECODE(entry) << mapshift, 221 (SM_OFFSET_DECODE(entry) + SM_RUN_DECODE(entry)) << 222 mapshift, 223 SM_TYPE_DECODE(entry) == SM_ALLOC ? 'A' : 'F', 224 SM_RUN_DECODE(entry) << mapshift); 225 if (verbose) 226 mdb_printf(" (raw=%012llx)\n", entry); 227 mdb_printf("\n"); 228 } 229 return (WALK_NEXT); 230 } 231 232 static int 233 mdb_dsl_dir_name(uintptr_t addr, char *buf) 234 { 235 static int gotid; 236 static mdb_ctf_id_t dd_id; 237 uintptr_t dd_parent; 238 char dd_myname[ZFS_MAX_DATASET_NAME_LEN]; 239 240 if (!gotid) { 241 if (mdb_ctf_lookup_by_name(ZFS_STRUCT "dsl_dir", 242 &dd_id) == -1) { 243 mdb_warn("couldn't find struct dsl_dir"); 244 return (DCMD_ERR); 245 } 246 gotid = TRUE; 247 } 248 if (GETMEMBID(addr, &dd_id, dd_parent, dd_parent) || 249 GETMEMBID(addr, &dd_id, dd_myname, dd_myname)) { 250 return (DCMD_ERR); 251 } 252 253 if (dd_parent) { 254 if (mdb_dsl_dir_name(dd_parent, buf)) 255 return (DCMD_ERR); 256 strcat(buf, "/"); 257 } 258 259 if (dd_myname[0]) 260 strcat(buf, dd_myname); 261 else 262 strcat(buf, "???"); 263 264 return (0); 265 } 266 267 static int 268 objset_name(uintptr_t addr, char *buf) 269 { 270 static int gotid; 271 static mdb_ctf_id_t os_id, ds_id; 272 uintptr_t os_dsl_dataset; 273 char ds_snapname[ZFS_MAX_DATASET_NAME_LEN]; 274 uintptr_t ds_dir; 275 276 buf[0] = '\0'; 277 278 if (!gotid) { 279 if (mdb_ctf_lookup_by_name(ZFS_STRUCT "objset", 280 &os_id) == -1) { 281 mdb_warn("couldn't find struct objset"); 282 return (DCMD_ERR); 283 } 284 if (mdb_ctf_lookup_by_name(ZFS_STRUCT "dsl_dataset", 285 &ds_id) == -1) { 286 mdb_warn("couldn't find struct dsl_dataset"); 287 return (DCMD_ERR); 288 } 289 290 gotid = TRUE; 291 } 292 293 if (GETMEMBID(addr, &os_id, os_dsl_dataset, os_dsl_dataset)) 294 return (DCMD_ERR); 295 296 if (os_dsl_dataset == 0) { 297 strcat(buf, "mos"); 298 return (0); 299 } 300 301 if (GETMEMBID(os_dsl_dataset, &ds_id, ds_snapname, ds_snapname) || 302 GETMEMBID(os_dsl_dataset, &ds_id, ds_dir, ds_dir)) { 303 return (DCMD_ERR); 304 } 305 306 if (ds_dir && mdb_dsl_dir_name(ds_dir, buf)) 307 return (DCMD_ERR); 308 309 if (ds_snapname[0]) { 310 strcat(buf, "@"); 311 strcat(buf, ds_snapname); 312 } 313 return (0); 314 } 315 316 static int 317 enum_lookup(char *type, int val, const char *prefix, size_t size, char *out) 318 { 319 const char *cp; 320 size_t len = strlen(prefix); 321 mdb_ctf_id_t enum_type; 322 323 if (mdb_ctf_lookup_by_name(type, &enum_type) != 0) { 324 mdb_warn("Could not find enum for %s", type); 325 return (-1); 326 } 327 328 if ((cp = mdb_ctf_enum_name(enum_type, val)) != NULL) { 329 if (strncmp(cp, prefix, len) == 0) 330 cp += len; 331 (void) strncpy(out, cp, size); 332 } else { 333 mdb_snprintf(out, size, "? (%d)", val); 334 } 335 return (0); 336 } 337 338 /* ARGSUSED */ 339 static int 340 zfs_params(uintptr_t addr, uint_t flags, int argc, const mdb_arg_t *argv) 341 { 342 /* 343 * This table can be approximately generated by running: 344 * egrep "^[a-z0-9_]+ [a-z0-9_]+( =.*)?;" *.c | cut -d ' ' -f 2 345 */ 346 static const char *params[] = { 347 "arc_reduce_dnlc_percent", 348 "arc_lotsfree_percent", 349 "zfs_dirty_data_max", 350 "zfs_dirty_data_sync", 351 "zfs_delay_max_ns", 352 "zfs_delay_min_dirty_percent", 353 "zfs_delay_scale", 354 "zfs_vdev_max_active", 355 "zfs_vdev_sync_read_min_active", 356 "zfs_vdev_sync_read_max_active", 357 "zfs_vdev_sync_write_min_active", 358 "zfs_vdev_sync_write_max_active", 359 "zfs_vdev_async_read_min_active", 360 "zfs_vdev_async_read_max_active", 361 "zfs_vdev_async_write_min_active", 362 "zfs_vdev_async_write_max_active", 363 "zfs_vdev_scrub_min_active", 364 "zfs_vdev_scrub_max_active", 365 "zfs_vdev_async_write_active_min_dirty_percent", 366 "zfs_vdev_async_write_active_max_dirty_percent", 367 "spa_asize_inflation", 368 "zfs_arc_max", 369 "zfs_arc_min", 370 "arc_shrink_shift", 371 "zfs_mdcomp_disable", 372 "zfs_prefetch_disable", 373 "zfetch_max_streams", 374 "zfetch_min_sec_reap", 375 "zfetch_block_cap", 376 "zfetch_array_rd_sz", 377 "zfs_default_bs", 378 "zfs_default_ibs", 379 "metaslab_aliquot", 380 "reference_tracking_enable", 381 "reference_history", 382 "spa_max_replication_override", 383 "spa_mode_global", 384 "zfs_flags", 385 "zfs_txg_timeout", 386 "zfs_vdev_cache_max", 387 "zfs_vdev_cache_size", 388 "zfs_vdev_cache_bshift", 389 "vdev_mirror_shift", 390 "zfs_scrub_limit", 391 "zfs_no_scrub_io", 392 "zfs_no_scrub_prefetch", 393 "zfs_vdev_aggregation_limit", 394 "fzap_default_block_shift", 395 "zfs_immediate_write_sz", 396 "zfs_read_chunk_size", 397 "zfs_nocacheflush", 398 "zil_replay_disable", 399 "metaslab_gang_bang", 400 "metaslab_df_alloc_threshold", 401 "metaslab_df_free_pct", 402 "zio_injection_enabled", 403 "zvol_immediate_write_sz", 404 }; 405 406 for (int i = 0; i < sizeof (params) / sizeof (params[0]); i++) { 407 int sz; 408 uint64_t val64; 409 uint32_t *val32p = (uint32_t *)&val64; 410 411 sz = mdb_readvar(&val64, params[i]); 412 if (sz == 4) { 413 mdb_printf("%s = 0x%x\n", params[i], *val32p); 414 } else if (sz == 8) { 415 mdb_printf("%s = 0x%llx\n", params[i], val64); 416 } else { 417 mdb_warn("variable %s not found", params[i]); 418 } 419 } 420 421 return (DCMD_OK); 422 } 423 424 /* ARGSUSED */ 425 static int 426 blkptr(uintptr_t addr, uint_t flags, int argc, const mdb_arg_t *argv) 427 { 428 char type[80], checksum[80], compress[80]; 429 blkptr_t blk, *bp = &blk; 430 char buf[BP_SPRINTF_LEN]; 431 432 if (mdb_vread(&blk, sizeof (blkptr_t), addr) == -1) { 433 mdb_warn("failed to read blkptr_t"); 434 return (DCMD_ERR); 435 } 436 437 if (enum_lookup("enum dmu_object_type", BP_GET_TYPE(bp), "DMU_OT_", 438 sizeof (type), type) == -1 || 439 enum_lookup("enum zio_checksum", BP_GET_CHECKSUM(bp), 440 "ZIO_CHECKSUM_", sizeof (checksum), checksum) == -1 || 441 enum_lookup("enum zio_compress", BP_GET_COMPRESS(bp), 442 "ZIO_COMPRESS_", sizeof (compress), compress) == -1) { 443 mdb_warn("Could not find blkptr enumerated types"); 444 return (DCMD_ERR); 445 } 446 447 SNPRINTF_BLKPTR(mdb_snprintf, '\n', buf, sizeof (buf), bp, type, 448 checksum, compress); 449 450 mdb_printf("%s\n", buf); 451 452 return (DCMD_OK); 453 } 454 455 typedef struct mdb_dmu_buf_impl { 456 struct { 457 uint64_t db_object; 458 uintptr_t db_data; 459 } db; 460 uintptr_t db_objset; 461 uint64_t db_level; 462 uint64_t db_blkid; 463 struct { 464 uint64_t rc_count; 465 } db_holds; 466 } mdb_dmu_buf_impl_t; 467 468 /* ARGSUSED */ 469 static int 470 dbuf(uintptr_t addr, uint_t flags, int argc, const mdb_arg_t *argv) 471 { 472 mdb_dmu_buf_impl_t db; 473 char objectname[32]; 474 char blkidname[32]; 475 char path[ZFS_MAX_DATASET_NAME_LEN]; 476 int ptr_width = (int)(sizeof (void *)) * 2; 477 478 if (DCMD_HDRSPEC(flags)) 479 mdb_printf("%*s %8s %3s %9s %5s %s\n", 480 ptr_width, "addr", "object", "lvl", "blkid", "holds", "os"); 481 482 if (mdb_ctf_vread(&db, ZFS_STRUCT "dmu_buf_impl", "mdb_dmu_buf_impl_t", 483 addr, 0) == -1) 484 return (DCMD_ERR); 485 486 if (db.db.db_object == DMU_META_DNODE_OBJECT) 487 (void) strcpy(objectname, "mdn"); 488 else 489 (void) mdb_snprintf(objectname, sizeof (objectname), "%llx", 490 (u_longlong_t)db.db.db_object); 491 492 if (db.db_blkid == DMU_BONUS_BLKID) 493 (void) strcpy(blkidname, "bonus"); 494 else 495 (void) mdb_snprintf(blkidname, sizeof (blkidname), "%llx", 496 (u_longlong_t)db.db_blkid); 497 498 if (objset_name(db.db_objset, path)) { 499 return (DCMD_ERR); 500 } 501 502 mdb_printf("%*p %8s %3u %9s %5llu %s\n", ptr_width, addr, 503 objectname, (int)db.db_level, blkidname, 504 db.db_holds.rc_count, path); 505 506 return (DCMD_OK); 507 } 508 509 /* ARGSUSED */ 510 static int 511 dbuf_stats(uintptr_t addr, uint_t flags, int argc, const mdb_arg_t *argv) 512 { 513 #define HISTOSZ 32 514 uintptr_t dbp; 515 dmu_buf_impl_t db; 516 dbuf_hash_table_t ht; 517 uint64_t bucket, ndbufs; 518 uint64_t histo[HISTOSZ]; 519 uint64_t histo2[HISTOSZ]; 520 int i, maxidx; 521 522 if (mdb_readvar(&ht, "dbuf_hash_table") == -1) { 523 mdb_warn("failed to read 'dbuf_hash_table'"); 524 return (DCMD_ERR); 525 } 526 527 for (i = 0; i < HISTOSZ; i++) { 528 histo[i] = 0; 529 histo2[i] = 0; 530 } 531 532 ndbufs = 0; 533 for (bucket = 0; bucket < ht.hash_table_mask+1; bucket++) { 534 int len; 535 536 if (mdb_vread(&dbp, sizeof (void *), 537 (uintptr_t)(ht.hash_table+bucket)) == -1) { 538 mdb_warn("failed to read hash bucket %u at %p", 539 bucket, ht.hash_table+bucket); 540 return (DCMD_ERR); 541 } 542 543 len = 0; 544 while (dbp != 0) { 545 if (mdb_vread(&db, sizeof (dmu_buf_impl_t), 546 dbp) == -1) { 547 mdb_warn("failed to read dbuf at %p", dbp); 548 return (DCMD_ERR); 549 } 550 dbp = (uintptr_t)db.db_hash_next; 551 for (i = MIN(len, HISTOSZ - 1); i >= 0; i--) 552 histo2[i]++; 553 len++; 554 ndbufs++; 555 } 556 557 if (len >= HISTOSZ) 558 len = HISTOSZ-1; 559 histo[len]++; 560 } 561 562 mdb_printf("hash table has %llu buckets, %llu dbufs " 563 "(avg %llu buckets/dbuf)\n", 564 ht.hash_table_mask+1, ndbufs, 565 (ht.hash_table_mask+1)/ndbufs); 566 567 mdb_printf("\n"); 568 maxidx = 0; 569 for (i = 0; i < HISTOSZ; i++) 570 if (histo[i] > 0) 571 maxidx = i; 572 mdb_printf("hash chain length number of buckets\n"); 573 for (i = 0; i <= maxidx; i++) 574 mdb_printf("%u %llu\n", i, histo[i]); 575 576 mdb_printf("\n"); 577 maxidx = 0; 578 for (i = 0; i < HISTOSZ; i++) 579 if (histo2[i] > 0) 580 maxidx = i; 581 mdb_printf("hash chain depth number of dbufs\n"); 582 for (i = 0; i <= maxidx; i++) 583 mdb_printf("%u or more %llu %llu%%\n", 584 i, histo2[i], histo2[i]*100/ndbufs); 585 586 587 return (DCMD_OK); 588 } 589 590 #define CHAIN_END 0xffff 591 /* 592 * ::zap_leaf [-v] 593 * 594 * Print a zap_leaf_phys_t, assumed to be 16k 595 */ 596 /* ARGSUSED */ 597 static int 598 zap_leaf(uintptr_t addr, uint_t flags, int argc, const mdb_arg_t *argv) 599 { 600 char buf[16*1024]; 601 int verbose = B_FALSE; 602 int four = B_FALSE; 603 dmu_buf_t l_dbuf; 604 zap_leaf_t l; 605 zap_leaf_phys_t *zlp = (void *)buf; 606 int i; 607 608 if (mdb_getopts(argc, argv, 609 'v', MDB_OPT_SETBITS, TRUE, &verbose, 610 '4', MDB_OPT_SETBITS, TRUE, &four, 611 NULL) != argc) 612 return (DCMD_USAGE); 613 614 l_dbuf.db_data = zlp; 615 l.l_dbuf = &l_dbuf; 616 l.l_bs = 14; /* assume 16k blocks */ 617 if (four) 618 l.l_bs = 12; 619 620 if (!(flags & DCMD_ADDRSPEC)) { 621 return (DCMD_USAGE); 622 } 623 624 if (mdb_vread(buf, sizeof (buf), addr) == -1) { 625 mdb_warn("failed to read zap_leaf_phys_t at %p", addr); 626 return (DCMD_ERR); 627 } 628 629 if (zlp->l_hdr.lh_block_type != ZBT_LEAF || 630 zlp->l_hdr.lh_magic != ZAP_LEAF_MAGIC) { 631 mdb_warn("This does not appear to be a zap_leaf_phys_t"); 632 return (DCMD_ERR); 633 } 634 635 mdb_printf("zap_leaf_phys_t at %p:\n", addr); 636 mdb_printf(" lh_prefix_len = %u\n", zlp->l_hdr.lh_prefix_len); 637 mdb_printf(" lh_prefix = %llx\n", zlp->l_hdr.lh_prefix); 638 mdb_printf(" lh_nentries = %u\n", zlp->l_hdr.lh_nentries); 639 mdb_printf(" lh_nfree = %u\n", zlp->l_hdr.lh_nfree, 640 zlp->l_hdr.lh_nfree * 100 / (ZAP_LEAF_NUMCHUNKS(&l))); 641 mdb_printf(" lh_freelist = %u\n", zlp->l_hdr.lh_freelist); 642 mdb_printf(" lh_flags = %x (%s)\n", zlp->l_hdr.lh_flags, 643 zlp->l_hdr.lh_flags & ZLF_ENTRIES_CDSORTED ? 644 "ENTRIES_CDSORTED" : ""); 645 646 if (verbose) { 647 mdb_printf(" hash table:\n"); 648 for (i = 0; i < ZAP_LEAF_HASH_NUMENTRIES(&l); i++) { 649 if (zlp->l_hash[i] != CHAIN_END) 650 mdb_printf(" %u: %u\n", i, zlp->l_hash[i]); 651 } 652 } 653 654 mdb_printf(" chunks:\n"); 655 for (i = 0; i < ZAP_LEAF_NUMCHUNKS(&l); i++) { 656 /* LINTED: alignment */ 657 zap_leaf_chunk_t *zlc = &ZAP_LEAF_CHUNK(&l, i); 658 switch (zlc->l_entry.le_type) { 659 case ZAP_CHUNK_FREE: 660 if (verbose) { 661 mdb_printf(" %u: free; lf_next = %u\n", 662 i, zlc->l_free.lf_next); 663 } 664 break; 665 case ZAP_CHUNK_ENTRY: 666 mdb_printf(" %u: entry\n", i); 667 if (verbose) { 668 mdb_printf(" le_next = %u\n", 669 zlc->l_entry.le_next); 670 } 671 mdb_printf(" le_name_chunk = %u\n", 672 zlc->l_entry.le_name_chunk); 673 mdb_printf(" le_name_numints = %u\n", 674 zlc->l_entry.le_name_numints); 675 mdb_printf(" le_value_chunk = %u\n", 676 zlc->l_entry.le_value_chunk); 677 mdb_printf(" le_value_intlen = %u\n", 678 zlc->l_entry.le_value_intlen); 679 mdb_printf(" le_value_numints = %u\n", 680 zlc->l_entry.le_value_numints); 681 mdb_printf(" le_cd = %u\n", 682 zlc->l_entry.le_cd); 683 mdb_printf(" le_hash = %llx\n", 684 zlc->l_entry.le_hash); 685 break; 686 case ZAP_CHUNK_ARRAY: 687 mdb_printf(" %u: array", i); 688 if (strisprint((char *)zlc->l_array.la_array)) 689 mdb_printf(" \"%s\"", zlc->l_array.la_array); 690 mdb_printf("\n"); 691 if (verbose) { 692 int j; 693 mdb_printf(" "); 694 for (j = 0; j < ZAP_LEAF_ARRAY_BYTES; j++) { 695 mdb_printf("%02x ", 696 zlc->l_array.la_array[j]); 697 } 698 mdb_printf("\n"); 699 } 700 if (zlc->l_array.la_next != CHAIN_END) { 701 mdb_printf(" lf_next = %u\n", 702 zlc->l_array.la_next); 703 } 704 break; 705 default: 706 mdb_printf(" %u: undefined type %u\n", 707 zlc->l_entry.le_type); 708 } 709 } 710 711 return (DCMD_OK); 712 } 713 714 typedef struct dbufs_data { 715 mdb_ctf_id_t id; 716 uint64_t objset; 717 uint64_t object; 718 uint64_t level; 719 uint64_t blkid; 720 char *osname; 721 } dbufs_data_t; 722 723 #define DBUFS_UNSET (0xbaddcafedeadbeefULL) 724 725 /* ARGSUSED */ 726 static int 727 dbufs_cb(uintptr_t addr, const void *unknown, void *arg) 728 { 729 dbufs_data_t *data = arg; 730 uintptr_t objset; 731 dmu_buf_t db; 732 uint8_t level; 733 uint64_t blkid; 734 char osname[ZFS_MAX_DATASET_NAME_LEN]; 735 736 if (GETMEMBID(addr, &data->id, db_objset, objset) || 737 GETMEMBID(addr, &data->id, db, db) || 738 GETMEMBID(addr, &data->id, db_level, level) || 739 GETMEMBID(addr, &data->id, db_blkid, blkid)) { 740 return (WALK_ERR); 741 } 742 743 if ((data->objset == DBUFS_UNSET || data->objset == objset) && 744 (data->osname == NULL || (objset_name(objset, osname) == 0 && 745 strcmp(data->osname, osname) == 0)) && 746 (data->object == DBUFS_UNSET || data->object == db.db_object) && 747 (data->level == DBUFS_UNSET || data->level == level) && 748 (data->blkid == DBUFS_UNSET || data->blkid == blkid)) { 749 mdb_printf("%#lr\n", addr); 750 } 751 return (WALK_NEXT); 752 } 753 754 /* ARGSUSED */ 755 static int 756 dbufs(uintptr_t addr, uint_t flags, int argc, const mdb_arg_t *argv) 757 { 758 dbufs_data_t data; 759 char *object = NULL; 760 char *blkid = NULL; 761 762 data.objset = data.object = data.level = data.blkid = DBUFS_UNSET; 763 data.osname = NULL; 764 765 if (mdb_getopts(argc, argv, 766 'O', MDB_OPT_UINT64, &data.objset, 767 'n', MDB_OPT_STR, &data.osname, 768 'o', MDB_OPT_STR, &object, 769 'l', MDB_OPT_UINT64, &data.level, 770 'b', MDB_OPT_STR, &blkid) != argc) { 771 return (DCMD_USAGE); 772 } 773 774 if (object) { 775 if (strcmp(object, "mdn") == 0) { 776 data.object = DMU_META_DNODE_OBJECT; 777 } else { 778 data.object = mdb_strtoull(object); 779 } 780 } 781 782 if (blkid) { 783 if (strcmp(blkid, "bonus") == 0) { 784 data.blkid = DMU_BONUS_BLKID; 785 } else { 786 data.blkid = mdb_strtoull(blkid); 787 } 788 } 789 790 if (mdb_ctf_lookup_by_name(ZFS_STRUCT "dmu_buf_impl", &data.id) == -1) { 791 mdb_warn("couldn't find struct dmu_buf_impl_t"); 792 return (DCMD_ERR); 793 } 794 795 if (mdb_walk("dmu_buf_impl_t", dbufs_cb, &data) != 0) { 796 mdb_warn("can't walk dbufs"); 797 return (DCMD_ERR); 798 } 799 800 return (DCMD_OK); 801 } 802 803 typedef struct abuf_find_data { 804 dva_t dva; 805 mdb_ctf_id_t id; 806 } abuf_find_data_t; 807 808 /* ARGSUSED */ 809 static int 810 abuf_find_cb(uintptr_t addr, const void *unknown, void *arg) 811 { 812 abuf_find_data_t *data = arg; 813 dva_t dva; 814 815 if (GETMEMBID(addr, &data->id, b_dva, dva)) { 816 return (WALK_ERR); 817 } 818 819 if (dva.dva_word[0] == data->dva.dva_word[0] && 820 dva.dva_word[1] == data->dva.dva_word[1]) { 821 mdb_printf("%#lr\n", addr); 822 } 823 return (WALK_NEXT); 824 } 825 826 /* ARGSUSED */ 827 static int 828 abuf_find(uintptr_t addr, uint_t flags, int argc, const mdb_arg_t *argv) 829 { 830 abuf_find_data_t data; 831 GElf_Sym sym; 832 int i; 833 const char *syms[] = { 834 "ARC_mru", 835 "ARC_mru_ghost", 836 "ARC_mfu", 837 "ARC_mfu_ghost", 838 }; 839 840 if (argc != 2) 841 return (DCMD_USAGE); 842 843 for (i = 0; i < 2; i ++) { 844 switch (argv[i].a_type) { 845 case MDB_TYPE_STRING: 846 data.dva.dva_word[i] = mdb_strtoull(argv[i].a_un.a_str); 847 break; 848 case MDB_TYPE_IMMEDIATE: 849 data.dva.dva_word[i] = argv[i].a_un.a_val; 850 break; 851 default: 852 return (DCMD_USAGE); 853 } 854 } 855 856 if (mdb_ctf_lookup_by_name(ZFS_STRUCT "arc_buf_hdr", &data.id) == -1) { 857 mdb_warn("couldn't find struct arc_buf_hdr"); 858 return (DCMD_ERR); 859 } 860 861 for (i = 0; i < sizeof (syms) / sizeof (syms[0]); i++) { 862 if (mdb_lookup_by_obj(ZFS_OBJ_NAME, syms[i], &sym)) { 863 mdb_warn("can't find symbol %s", syms[i]); 864 return (DCMD_ERR); 865 } 866 867 if (mdb_pwalk("list", abuf_find_cb, &data, sym.st_value) != 0) { 868 mdb_warn("can't walk %s", syms[i]); 869 return (DCMD_ERR); 870 } 871 } 872 873 return (DCMD_OK); 874 } 875 876 877 typedef struct dbgmsg_arg { 878 boolean_t da_verbose; 879 boolean_t da_address; 880 } dbgmsg_arg_t; 881 882 /* ARGSUSED */ 883 static int 884 dbgmsg_cb(uintptr_t addr, const void *unknown, void *arg) 885 { 886 static mdb_ctf_id_t id; 887 static boolean_t gotid; 888 static ulong_t off; 889 890 dbgmsg_arg_t *da = arg; 891 time_t timestamp; 892 char buf[1024]; 893 894 if (!gotid) { 895 if (mdb_ctf_lookup_by_name(ZFS_STRUCT "zfs_dbgmsg", &id) == 896 -1) { 897 mdb_warn("couldn't find struct zfs_dbgmsg"); 898 return (WALK_ERR); 899 } 900 gotid = TRUE; 901 if (mdb_ctf_offsetof(id, "zdm_msg", &off) == -1) { 902 mdb_warn("couldn't find zdm_msg"); 903 return (WALK_ERR); 904 } 905 off /= 8; 906 } 907 908 909 if (GETMEMBID(addr, &id, zdm_timestamp, timestamp)) { 910 return (WALK_ERR); 911 } 912 913 if (mdb_readstr(buf, sizeof (buf), addr + off) == -1) { 914 mdb_warn("failed to read zdm_msg at %p\n", addr + off); 915 return (DCMD_ERR); 916 } 917 918 if (da->da_address) 919 mdb_printf("%p ", addr); 920 if (da->da_verbose) 921 mdb_printf("%Y ", timestamp); 922 923 mdb_printf("%s\n", buf); 924 925 if (da->da_verbose) 926 (void) mdb_call_dcmd("whatis", addr, DCMD_ADDRSPEC, 0, NULL); 927 928 return (WALK_NEXT); 929 } 930 931 /* ARGSUSED */ 932 static int 933 dbgmsg(uintptr_t addr, uint_t flags, int argc, const mdb_arg_t *argv) 934 { 935 GElf_Sym sym; 936 dbgmsg_arg_t da = { 0 }; 937 938 if (mdb_getopts(argc, argv, 939 'v', MDB_OPT_SETBITS, B_TRUE, &da.da_verbose, 940 'a', MDB_OPT_SETBITS, B_TRUE, &da.da_address, 941 NULL) != argc) 942 return (DCMD_USAGE); 943 944 if (mdb_lookup_by_obj(ZFS_OBJ_NAME, "zfs_dbgmsgs", &sym)) { 945 mdb_warn("can't find zfs_dbgmsgs"); 946 return (DCMD_ERR); 947 } 948 949 if (mdb_pwalk("list", dbgmsg_cb, &da, sym.st_value) != 0) { 950 mdb_warn("can't walk zfs_dbgmsgs"); 951 return (DCMD_ERR); 952 } 953 954 return (DCMD_OK); 955 } 956 957 /*ARGSUSED*/ 958 static int 959 arc_print(uintptr_t addr, uint_t flags, int argc, const mdb_arg_t *argv) 960 { 961 kstat_named_t *stats; 962 GElf_Sym sym; 963 int nstats, i; 964 uint_t opt_a = FALSE; 965 uint_t opt_b = FALSE; 966 uint_t shift = 0; 967 const char *suffix; 968 969 static const char *bytestats[] = { 970 "p", "c", "c_min", "c_max", "size", "duplicate_buffers_size", 971 "arc_meta_used", "arc_meta_limit", "arc_meta_max", 972 "arc_meta_min", "hdr_size", "data_size", "metadata_size", 973 "other_size", "anon_size", "anon_evictable_data", 974 "anon_evictable_metadata", "mru_size", "mru_evictable_data", 975 "mru_evictable_metadata", "mru_ghost_size", 976 "mru_ghost_evictable_data", "mru_ghost_evictable_metadata", 977 "mfu_size", "mfu_evictable_data", "mfu_evictable_metadata", 978 "mfu_ghost_size", "mfu_ghost_evictable_data", 979 "mfu_ghost_evictable_metadata", "evict_l2_cached", 980 "evict_l2_eligible", "evict_l2_ineligible", "l2_read_bytes", 981 "l2_write_bytes", "l2_size", "l2_asize", "l2_hdr_size", 982 "compressed_size", "uncompressed_size", "overhead_size", 983 NULL 984 }; 985 986 static const char *extras[] = { 987 "arc_no_grow", "arc_tempreserve", 988 NULL 989 }; 990 991 if (mdb_lookup_by_obj(ZFS_OBJ_NAME, "arc_stats", &sym) == -1) { 992 mdb_warn("failed to find 'arc_stats'"); 993 return (DCMD_ERR); 994 } 995 996 stats = mdb_zalloc(sym.st_size, UM_SLEEP | UM_GC); 997 998 if (mdb_vread(stats, sym.st_size, sym.st_value) == -1) { 999 mdb_warn("couldn't read 'arc_stats' at %p", sym.st_value); 1000 return (DCMD_ERR); 1001 } 1002 1003 nstats = sym.st_size / sizeof (kstat_named_t); 1004 1005 /* NB: -a / opt_a are ignored for backwards compatability */ 1006 if (mdb_getopts(argc, argv, 1007 'a', MDB_OPT_SETBITS, TRUE, &opt_a, 1008 'b', MDB_OPT_SETBITS, TRUE, &opt_b, 1009 'k', MDB_OPT_SETBITS, 10, &shift, 1010 'm', MDB_OPT_SETBITS, 20, &shift, 1011 'g', MDB_OPT_SETBITS, 30, &shift, 1012 NULL) != argc) 1013 return (DCMD_USAGE); 1014 1015 if (!opt_b && !shift) 1016 shift = 20; 1017 1018 switch (shift) { 1019 case 0: 1020 suffix = "B"; 1021 break; 1022 case 10: 1023 suffix = "KB"; 1024 break; 1025 case 20: 1026 suffix = "MB"; 1027 break; 1028 case 30: 1029 suffix = "GB"; 1030 break; 1031 default: 1032 suffix = "XX"; 1033 } 1034 1035 for (i = 0; i < nstats; i++) { 1036 int j; 1037 boolean_t bytes = B_FALSE; 1038 1039 for (j = 0; bytestats[j]; j++) { 1040 if (strcmp(stats[i].name, bytestats[j]) == 0) { 1041 bytes = B_TRUE; 1042 break; 1043 } 1044 } 1045 1046 if (bytes) { 1047 mdb_printf("%-25s = %9llu %s\n", stats[i].name, 1048 stats[i].value.ui64 >> shift, suffix); 1049 } else { 1050 mdb_printf("%-25s = %9llu\n", stats[i].name, 1051 stats[i].value.ui64); 1052 } 1053 } 1054 1055 for (i = 0; extras[i]; i++) { 1056 uint64_t buf; 1057 1058 if (mdb_lookup_by_obj(ZFS_OBJ_NAME, extras[i], &sym) == -1) { 1059 mdb_warn("failed to find '%s'", extras[i]); 1060 return (DCMD_ERR); 1061 } 1062 1063 if (sym.st_size != sizeof (uint64_t) && 1064 sym.st_size != sizeof (uint32_t)) { 1065 mdb_warn("expected scalar for variable '%s'\n", 1066 extras[i]); 1067 return (DCMD_ERR); 1068 } 1069 1070 if (mdb_vread(&buf, sym.st_size, sym.st_value) == -1) { 1071 mdb_warn("couldn't read '%s'", extras[i]); 1072 return (DCMD_ERR); 1073 } 1074 1075 mdb_printf("%-25s = ", extras[i]); 1076 1077 /* NB: all the 64-bit extras happen to be byte counts */ 1078 if (sym.st_size == sizeof (uint64_t)) 1079 mdb_printf("%9llu %s\n", buf >> shift, suffix); 1080 1081 if (sym.st_size == sizeof (uint32_t)) 1082 mdb_printf("%9d\n", *((uint32_t *)&buf)); 1083 } 1084 return (DCMD_OK); 1085 } 1086 1087 typedef struct mdb_spa_print { 1088 pool_state_t spa_state; 1089 char spa_name[ZFS_MAX_DATASET_NAME_LEN]; 1090 uintptr_t spa_normal_class; 1091 } mdb_spa_print_t; 1092 1093 1094 const char histo_stars[] = "****************************************"; 1095 const int histo_width = sizeof (histo_stars) - 1; 1096 1097 static void 1098 dump_histogram(const uint64_t *histo, int size, int offset) 1099 { 1100 int i; 1101 int minidx = size - 1; 1102 int maxidx = 0; 1103 uint64_t max = 0; 1104 1105 for (i = 0; i < size; i++) { 1106 if (histo[i] > max) 1107 max = histo[i]; 1108 if (histo[i] > 0 && i > maxidx) 1109 maxidx = i; 1110 if (histo[i] > 0 && i < minidx) 1111 minidx = i; 1112 } 1113 1114 if (max < histo_width) 1115 max = histo_width; 1116 1117 for (i = minidx; i <= maxidx; i++) { 1118 mdb_printf("%3u: %6llu %s\n", 1119 i + offset, (u_longlong_t)histo[i], 1120 &histo_stars[(max - histo[i]) * histo_width / max]); 1121 } 1122 } 1123 1124 typedef struct mdb_metaslab_class { 1125 uint64_t mc_histogram[RANGE_TREE_HISTOGRAM_SIZE]; 1126 } mdb_metaslab_class_t; 1127 1128 /* 1129 * spa_class_histogram(uintptr_t class_addr) 1130 * 1131 * Prints free space histogram for a device class 1132 * 1133 * Returns DCMD_OK, or DCMD_ERR. 1134 */ 1135 static int 1136 spa_class_histogram(uintptr_t class_addr) 1137 { 1138 mdb_metaslab_class_t mc; 1139 if (mdb_ctf_vread(&mc, "metaslab_class_t", 1140 "mdb_metaslab_class_t", class_addr, 0) == -1) 1141 return (DCMD_ERR); 1142 1143 mdb_inc_indent(4); 1144 dump_histogram(mc.mc_histogram, RANGE_TREE_HISTOGRAM_SIZE, 0); 1145 mdb_dec_indent(4); 1146 return (DCMD_OK); 1147 } 1148 1149 /* 1150 * ::spa 1151 * 1152 * -c Print configuration information as well 1153 * -v Print vdev state 1154 * -e Print vdev error stats 1155 * -m Print vdev metaslab info 1156 * -M print vdev metaslab group info 1157 * -h Print histogram info (must be combined with -m or -M) 1158 * 1159 * Print a summarized spa_t. When given no arguments, prints out a table of all 1160 * active pools on the system. 1161 */ 1162 /* ARGSUSED */ 1163 static int 1164 spa_print(uintptr_t addr, uint_t flags, int argc, const mdb_arg_t *argv) 1165 { 1166 const char *statetab[] = { "ACTIVE", "EXPORTED", "DESTROYED", 1167 "SPARE", "L2CACHE", "UNINIT", "UNAVAIL", "POTENTIAL" }; 1168 const char *state; 1169 int spa_flags = 0; 1170 1171 if (mdb_getopts(argc, argv, 1172 'c', MDB_OPT_SETBITS, SPA_FLAG_CONFIG, &spa_flags, 1173 'v', MDB_OPT_SETBITS, SPA_FLAG_VDEVS, &spa_flags, 1174 'e', MDB_OPT_SETBITS, SPA_FLAG_ERRORS, &spa_flags, 1175 'M', MDB_OPT_SETBITS, SPA_FLAG_METASLAB_GROUPS, &spa_flags, 1176 'm', MDB_OPT_SETBITS, SPA_FLAG_METASLABS, &spa_flags, 1177 'h', MDB_OPT_SETBITS, SPA_FLAG_HISTOGRAMS, &spa_flags, 1178 NULL) != argc) 1179 return (DCMD_USAGE); 1180 1181 if (!(flags & DCMD_ADDRSPEC)) { 1182 if (mdb_walk_dcmd("spa", "spa", argc, argv) == -1) { 1183 mdb_warn("can't walk spa"); 1184 return (DCMD_ERR); 1185 } 1186 1187 return (DCMD_OK); 1188 } 1189 1190 if (flags & DCMD_PIPE_OUT) { 1191 mdb_printf("%#lr\n", addr); 1192 return (DCMD_OK); 1193 } 1194 1195 if (DCMD_HDRSPEC(flags)) 1196 mdb_printf("%<u>%-?s %9s %-*s%</u>\n", "ADDR", "STATE", 1197 sizeof (uintptr_t) == 4 ? 60 : 52, "NAME"); 1198 1199 mdb_spa_print_t spa; 1200 if (mdb_ctf_vread(&spa, "spa_t", "mdb_spa_print_t", addr, 0) == -1) 1201 return (DCMD_ERR); 1202 1203 if (spa.spa_state < 0 || spa.spa_state > POOL_STATE_UNAVAIL) 1204 state = "UNKNOWN"; 1205 else 1206 state = statetab[spa.spa_state]; 1207 1208 mdb_printf("%0?p %9s %s\n", addr, state, spa.spa_name); 1209 if (spa_flags & SPA_FLAG_HISTOGRAMS) 1210 spa_class_histogram(spa.spa_normal_class); 1211 1212 if (spa_flags & SPA_FLAG_CONFIG) { 1213 mdb_printf("\n"); 1214 mdb_inc_indent(4); 1215 if (mdb_call_dcmd("spa_config", addr, flags, 0, 1216 NULL) != DCMD_OK) 1217 return (DCMD_ERR); 1218 mdb_dec_indent(4); 1219 } 1220 1221 if (spa_flags & SPA_FLAG_ALL_VDEV) { 1222 mdb_arg_t v; 1223 char opts[100] = "-"; 1224 int args = 1225 (spa_flags | SPA_FLAG_VDEVS) == SPA_FLAG_VDEVS ? 0 : 1; 1226 1227 if (spa_flags & SPA_FLAG_ERRORS) 1228 strcat(opts, "e"); 1229 if (spa_flags & SPA_FLAG_METASLABS) 1230 strcat(opts, "m"); 1231 if (spa_flags & SPA_FLAG_METASLAB_GROUPS) 1232 strcat(opts, "M"); 1233 if (spa_flags & SPA_FLAG_HISTOGRAMS) 1234 strcat(opts, "h"); 1235 1236 v.a_type = MDB_TYPE_STRING; 1237 v.a_un.a_str = opts; 1238 1239 mdb_printf("\n"); 1240 mdb_inc_indent(4); 1241 if (mdb_call_dcmd("spa_vdevs", addr, flags, args, 1242 &v) != DCMD_OK) 1243 return (DCMD_ERR); 1244 mdb_dec_indent(4); 1245 } 1246 1247 return (DCMD_OK); 1248 } 1249 1250 typedef struct mdb_spa_config_spa { 1251 uintptr_t spa_config; 1252 } mdb_spa_config_spa_t; 1253 1254 /* 1255 * ::spa_config 1256 * 1257 * Given a spa_t, print the configuration information stored in spa_config. 1258 * Since it's just an nvlist, format it as an indented list of name=value pairs. 1259 * We simply read the value of spa_config and pass off to ::nvlist. 1260 */ 1261 /* ARGSUSED */ 1262 static int 1263 spa_print_config(uintptr_t addr, uint_t flags, int argc, const mdb_arg_t *argv) 1264 { 1265 mdb_spa_config_spa_t spa; 1266 1267 if (argc != 0 || !(flags & DCMD_ADDRSPEC)) 1268 return (DCMD_USAGE); 1269 1270 if (mdb_ctf_vread(&spa, ZFS_STRUCT "spa", "mdb_spa_config_spa_t", 1271 addr, 0) == -1) 1272 return (DCMD_ERR); 1273 1274 if (spa.spa_config == 0) { 1275 mdb_printf("(none)\n"); 1276 return (DCMD_OK); 1277 } 1278 1279 return (mdb_call_dcmd("nvlist", spa.spa_config, flags, 1280 0, NULL)); 1281 } 1282 1283 1284 1285 typedef struct mdb_range_tree { 1286 uint64_t rt_space; 1287 } mdb_range_tree_t; 1288 1289 typedef struct mdb_metaslab_group { 1290 uint64_t mg_fragmentation; 1291 uint64_t mg_histogram[RANGE_TREE_HISTOGRAM_SIZE]; 1292 uintptr_t mg_vd; 1293 } mdb_metaslab_group_t; 1294 1295 typedef struct mdb_metaslab { 1296 uint64_t ms_id; 1297 uint64_t ms_start; 1298 uint64_t ms_size; 1299 int64_t ms_deferspace; 1300 uint64_t ms_fragmentation; 1301 uint64_t ms_weight; 1302 uintptr_t ms_alloctree[TXG_SIZE]; 1303 uintptr_t ms_freeingtree; 1304 uintptr_t ms_freedtree; 1305 uintptr_t ms_tree; 1306 uintptr_t ms_sm; 1307 } mdb_metaslab_t; 1308 1309 typedef struct mdb_space_map_phys_t { 1310 uint64_t smp_alloc; 1311 uint64_t smp_histogram[SPACE_MAP_HISTOGRAM_SIZE]; 1312 } mdb_space_map_phys_t; 1313 1314 typedef struct mdb_space_map { 1315 uint64_t sm_size; 1316 uint8_t sm_shift; 1317 uint64_t sm_alloc; 1318 uintptr_t sm_phys; 1319 } mdb_space_map_t; 1320 1321 typedef struct mdb_vdev { 1322 uintptr_t vdev_path; 1323 uintptr_t vdev_ms; 1324 uintptr_t vdev_ops; 1325 uint64_t vdev_ms_count; 1326 uint64_t vdev_id; 1327 vdev_stat_t vdev_stat; 1328 } mdb_vdev_t; 1329 1330 typedef struct mdb_vdev_ops { 1331 char vdev_op_type[16]; 1332 } mdb_vdev_ops_t; 1333 1334 static int 1335 metaslab_stats(uintptr_t addr, int spa_flags) 1336 { 1337 mdb_vdev_t vdev; 1338 uintptr_t *vdev_ms; 1339 1340 if (mdb_ctf_vread(&vdev, "vdev_t", "mdb_vdev_t", 1341 (uintptr_t)addr, 0) == -1) { 1342 mdb_warn("failed to read vdev at %p\n", addr); 1343 return (DCMD_ERR); 1344 } 1345 1346 mdb_inc_indent(4); 1347 mdb_printf("%<u>%-?s %6s %20s %10s %9s%</u>\n", "ADDR", "ID", 1348 "OFFSET", "FREE", "FRAGMENTATION"); 1349 1350 vdev_ms = mdb_alloc(vdev.vdev_ms_count * sizeof (void *), 1351 UM_SLEEP | UM_GC); 1352 if (mdb_vread(vdev_ms, vdev.vdev_ms_count * sizeof (void *), 1353 (uintptr_t)vdev.vdev_ms) == -1) { 1354 mdb_warn("failed to read vdev_ms at %p\n", vdev.vdev_ms); 1355 return (DCMD_ERR); 1356 } 1357 1358 for (int m = 0; m < vdev.vdev_ms_count; m++) { 1359 mdb_metaslab_t ms; 1360 mdb_space_map_t sm = { 0 }; 1361 char free[NICENUM_BUFLEN]; 1362 1363 if (mdb_ctf_vread(&ms, "metaslab_t", "mdb_metaslab_t", 1364 (uintptr_t)vdev_ms[m], 0) == -1) 1365 return (DCMD_ERR); 1366 1367 if (ms.ms_sm != NULL && 1368 mdb_ctf_vread(&sm, "space_map_t", "mdb_space_map_t", 1369 ms.ms_sm, 0) == -1) 1370 return (DCMD_ERR); 1371 1372 mdb_nicenum(ms.ms_size - sm.sm_alloc, free); 1373 1374 mdb_printf("%0?p %6llu %20llx %10s ", vdev_ms[m], ms.ms_id, 1375 ms.ms_start, free); 1376 if (ms.ms_fragmentation == ZFS_FRAG_INVALID) 1377 mdb_printf("%9s\n", "-"); 1378 else 1379 mdb_printf("%9llu%%\n", ms.ms_fragmentation); 1380 1381 if ((spa_flags & SPA_FLAG_HISTOGRAMS) && ms.ms_sm != NULL) { 1382 mdb_space_map_phys_t smp; 1383 1384 if (sm.sm_phys == NULL) 1385 continue; 1386 1387 (void) mdb_ctf_vread(&smp, "space_map_phys_t", 1388 "mdb_space_map_phys_t", sm.sm_phys, 0); 1389 1390 dump_histogram(smp.smp_histogram, 1391 SPACE_MAP_HISTOGRAM_SIZE, sm.sm_shift); 1392 } 1393 } 1394 mdb_dec_indent(4); 1395 return (DCMD_OK); 1396 } 1397 1398 static int 1399 metaslab_group_stats(uintptr_t addr, int spa_flags) 1400 { 1401 mdb_metaslab_group_t mg; 1402 if (mdb_ctf_vread(&mg, "metaslab_group_t", "mdb_metaslab_group_t", 1403 (uintptr_t)addr, 0) == -1) { 1404 mdb_warn("failed to read vdev_mg at %p\n", addr); 1405 return (DCMD_ERR); 1406 } 1407 1408 mdb_inc_indent(4); 1409 mdb_printf("%<u>%-?s %15s%</u>\n", "ADDR", "FRAGMENTATION"); 1410 if (mg.mg_fragmentation == ZFS_FRAG_INVALID) 1411 mdb_printf("%0?p %15s\n", addr, "-"); 1412 else 1413 mdb_printf("%0?p %15llu%%\n", addr, mg.mg_fragmentation); 1414 1415 if (spa_flags & SPA_FLAG_HISTOGRAMS) 1416 dump_histogram(mg.mg_histogram, RANGE_TREE_HISTOGRAM_SIZE, 0); 1417 mdb_dec_indent(4); 1418 return (DCMD_OK); 1419 } 1420 1421 /* 1422 * ::vdev 1423 * 1424 * Print out a summarized vdev_t, in the following form: 1425 * 1426 * ADDR STATE AUX DESC 1427 * fffffffbcde23df0 HEALTHY - /dev/dsk/c0t0d0 1428 * 1429 * If '-r' is specified, recursively visit all children. 1430 * 1431 * With '-e', the statistics associated with the vdev are printed as well. 1432 */ 1433 static int 1434 do_print_vdev(uintptr_t addr, int flags, int depth, boolean_t recursive, 1435 int spa_flags) 1436 { 1437 vdev_t vdev; 1438 char desc[MAXNAMELEN]; 1439 int c, children; 1440 uintptr_t *child; 1441 const char *state, *aux; 1442 1443 if (mdb_vread(&vdev, sizeof (vdev), (uintptr_t)addr) == -1) { 1444 mdb_warn("failed to read vdev_t at %p\n", (uintptr_t)addr); 1445 return (DCMD_ERR); 1446 } 1447 1448 if (flags & DCMD_PIPE_OUT) { 1449 mdb_printf("%#lr\n", addr); 1450 } else { 1451 if (vdev.vdev_path != NULL) { 1452 if (mdb_readstr(desc, sizeof (desc), 1453 (uintptr_t)vdev.vdev_path) == -1) { 1454 mdb_warn("failed to read vdev_path at %p\n", 1455 vdev.vdev_path); 1456 return (DCMD_ERR); 1457 } 1458 } else if (vdev.vdev_ops != NULL) { 1459 vdev_ops_t ops; 1460 if (mdb_vread(&ops, sizeof (ops), 1461 (uintptr_t)vdev.vdev_ops) == -1) { 1462 mdb_warn("failed to read vdev_ops at %p\n", 1463 vdev.vdev_ops); 1464 return (DCMD_ERR); 1465 } 1466 (void) strcpy(desc, ops.vdev_op_type); 1467 } else { 1468 (void) strcpy(desc, "<unknown>"); 1469 } 1470 1471 if (depth == 0 && DCMD_HDRSPEC(flags)) 1472 mdb_printf("%<u>%-?s %-9s %-12s %-*s%</u>\n", 1473 "ADDR", "STATE", "AUX", 1474 sizeof (uintptr_t) == 4 ? 43 : 35, 1475 "DESCRIPTION"); 1476 1477 mdb_printf("%0?p ", addr); 1478 1479 switch (vdev.vdev_state) { 1480 case VDEV_STATE_CLOSED: 1481 state = "CLOSED"; 1482 break; 1483 case VDEV_STATE_OFFLINE: 1484 state = "OFFLINE"; 1485 break; 1486 case VDEV_STATE_CANT_OPEN: 1487 state = "CANT_OPEN"; 1488 break; 1489 case VDEV_STATE_DEGRADED: 1490 state = "DEGRADED"; 1491 break; 1492 case VDEV_STATE_HEALTHY: 1493 state = "HEALTHY"; 1494 break; 1495 case VDEV_STATE_REMOVED: 1496 state = "REMOVED"; 1497 break; 1498 case VDEV_STATE_FAULTED: 1499 state = "FAULTED"; 1500 break; 1501 default: 1502 state = "UNKNOWN"; 1503 break; 1504 } 1505 1506 switch (vdev.vdev_stat.vs_aux) { 1507 case VDEV_AUX_NONE: 1508 aux = "-"; 1509 break; 1510 case VDEV_AUX_OPEN_FAILED: 1511 aux = "OPEN_FAILED"; 1512 break; 1513 case VDEV_AUX_CORRUPT_DATA: 1514 aux = "CORRUPT_DATA"; 1515 break; 1516 case VDEV_AUX_NO_REPLICAS: 1517 aux = "NO_REPLICAS"; 1518 break; 1519 case VDEV_AUX_BAD_GUID_SUM: 1520 aux = "BAD_GUID_SUM"; 1521 break; 1522 case VDEV_AUX_TOO_SMALL: 1523 aux = "TOO_SMALL"; 1524 break; 1525 case VDEV_AUX_BAD_LABEL: 1526 aux = "BAD_LABEL"; 1527 break; 1528 case VDEV_AUX_VERSION_NEWER: 1529 aux = "VERS_NEWER"; 1530 break; 1531 case VDEV_AUX_VERSION_OLDER: 1532 aux = "VERS_OLDER"; 1533 break; 1534 case VDEV_AUX_UNSUP_FEAT: 1535 aux = "UNSUP_FEAT"; 1536 break; 1537 case VDEV_AUX_SPARED: 1538 aux = "SPARED"; 1539 break; 1540 case VDEV_AUX_ERR_EXCEEDED: 1541 aux = "ERR_EXCEEDED"; 1542 break; 1543 case VDEV_AUX_IO_FAILURE: 1544 aux = "IO_FAILURE"; 1545 break; 1546 case VDEV_AUX_BAD_LOG: 1547 aux = "BAD_LOG"; 1548 break; 1549 case VDEV_AUX_EXTERNAL: 1550 aux = "EXTERNAL"; 1551 break; 1552 case VDEV_AUX_SPLIT_POOL: 1553 aux = "SPLIT_POOL"; 1554 break; 1555 default: 1556 aux = "UNKNOWN"; 1557 break; 1558 } 1559 1560 mdb_printf("%-9s %-12s %*s%s\n", state, aux, depth, "", desc); 1561 1562 if (spa_flags & SPA_FLAG_ERRORS) { 1563 vdev_stat_t *vs = &vdev.vdev_stat; 1564 int i; 1565 1566 mdb_inc_indent(4); 1567 mdb_printf("\n"); 1568 mdb_printf("%<u> %12s %12s %12s %12s " 1569 "%12s%</u>\n", "READ", "WRITE", "FREE", "CLAIM", 1570 "IOCTL"); 1571 mdb_printf("OPS "); 1572 for (i = 1; i < ZIO_TYPES; i++) 1573 mdb_printf("%11#llx%s", vs->vs_ops[i], 1574 i == ZIO_TYPES - 1 ? "" : " "); 1575 mdb_printf("\n"); 1576 mdb_printf("BYTES "); 1577 for (i = 1; i < ZIO_TYPES; i++) 1578 mdb_printf("%11#llx%s", vs->vs_bytes[i], 1579 i == ZIO_TYPES - 1 ? "" : " "); 1580 1581 1582 mdb_printf("\n"); 1583 mdb_printf("EREAD %10#llx\n", vs->vs_read_errors); 1584 mdb_printf("EWRITE %10#llx\n", vs->vs_write_errors); 1585 mdb_printf("ECKSUM %10#llx\n", 1586 vs->vs_checksum_errors); 1587 mdb_dec_indent(4); 1588 mdb_printf("\n"); 1589 } 1590 1591 if (spa_flags & SPA_FLAG_METASLAB_GROUPS && 1592 vdev.vdev_mg != NULL) { 1593 metaslab_group_stats((uintptr_t)vdev.vdev_mg, 1594 spa_flags); 1595 } 1596 if (spa_flags & SPA_FLAG_METASLABS && vdev.vdev_ms != NULL) { 1597 metaslab_stats((uintptr_t)addr, spa_flags); 1598 } 1599 } 1600 1601 children = vdev.vdev_children; 1602 1603 if (children == 0 || !recursive) 1604 return (DCMD_OK); 1605 1606 child = mdb_alloc(children * sizeof (void *), UM_SLEEP | UM_GC); 1607 if (mdb_vread(child, children * sizeof (void *), 1608 (uintptr_t)vdev.vdev_child) == -1) { 1609 mdb_warn("failed to read vdev children at %p", vdev.vdev_child); 1610 return (DCMD_ERR); 1611 } 1612 1613 for (c = 0; c < children; c++) { 1614 if (do_print_vdev(child[c], flags, depth + 2, recursive, 1615 spa_flags)) { 1616 return (DCMD_ERR); 1617 } 1618 } 1619 1620 return (DCMD_OK); 1621 } 1622 1623 static int 1624 vdev_print(uintptr_t addr, uint_t flags, int argc, const mdb_arg_t *argv) 1625 { 1626 uint64_t depth = 0; 1627 boolean_t recursive = B_FALSE; 1628 int spa_flags = 0; 1629 1630 if (mdb_getopts(argc, argv, 1631 'e', MDB_OPT_SETBITS, SPA_FLAG_ERRORS, &spa_flags, 1632 'm', MDB_OPT_SETBITS, SPA_FLAG_METASLABS, &spa_flags, 1633 'M', MDB_OPT_SETBITS, SPA_FLAG_METASLAB_GROUPS, &spa_flags, 1634 'h', MDB_OPT_SETBITS, SPA_FLAG_HISTOGRAMS, &spa_flags, 1635 'r', MDB_OPT_SETBITS, TRUE, &recursive, 1636 'd', MDB_OPT_UINT64, &depth, NULL) != argc) 1637 return (DCMD_USAGE); 1638 1639 if (!(flags & DCMD_ADDRSPEC)) { 1640 mdb_warn("no vdev_t address given\n"); 1641 return (DCMD_ERR); 1642 } 1643 1644 return (do_print_vdev(addr, flags, (int)depth, recursive, spa_flags)); 1645 } 1646 1647 typedef struct mdb_metaslab_alloc_trace { 1648 uintptr_t mat_mg; 1649 uintptr_t mat_msp; 1650 uint64_t mat_size; 1651 uint64_t mat_weight; 1652 uint64_t mat_offset; 1653 uint32_t mat_dva_id; 1654 } mdb_metaslab_alloc_trace_t; 1655 1656 static void 1657 metaslab_print_weight(uint64_t weight) 1658 { 1659 char buf[100]; 1660 1661 if (WEIGHT_IS_SPACEBASED(weight)) { 1662 mdb_nicenum( 1663 weight & ~(METASLAB_ACTIVE_MASK | METASLAB_WEIGHT_TYPE), 1664 buf); 1665 } else { 1666 char size[NICENUM_BUFLEN]; 1667 mdb_nicenum(1ULL << WEIGHT_GET_INDEX(weight), size); 1668 (void) mdb_snprintf(buf, sizeof (buf), "%llu x %s", 1669 WEIGHT_GET_COUNT(weight), size); 1670 } 1671 mdb_printf("%11s ", buf); 1672 } 1673 1674 /* ARGSUSED */ 1675 static int 1676 metaslab_weight(uintptr_t addr, uint_t flags, int argc, const mdb_arg_t *argv) 1677 { 1678 uint64_t weight = 0; 1679 char active; 1680 1681 if (argc == 0 && (flags & DCMD_ADDRSPEC)) { 1682 if (mdb_vread(&weight, sizeof (uint64_t), addr) == -1) { 1683 mdb_warn("failed to read weight at %p\n", addr); 1684 return (DCMD_ERR); 1685 } 1686 } else if (argc == 1 && !(flags & DCMD_ADDRSPEC)) { 1687 weight = (argv[0].a_type == MDB_TYPE_IMMEDIATE) ? 1688 argv[0].a_un.a_val : mdb_strtoull(argv[0].a_un.a_str); 1689 } else { 1690 return (DCMD_USAGE); 1691 } 1692 1693 if (DCMD_HDRSPEC(flags)) { 1694 mdb_printf("%<u>%-6s %9s %9s%</u>\n", 1695 "ACTIVE", "ALGORITHM", "WEIGHT"); 1696 } 1697 1698 if (weight & METASLAB_WEIGHT_PRIMARY) 1699 active = 'P'; 1700 else if (weight & METASLAB_WEIGHT_SECONDARY) 1701 active = 'S'; 1702 else 1703 active = '-'; 1704 mdb_printf("%6c %8s ", active, 1705 WEIGHT_IS_SPACEBASED(weight) ? "SPACE" : "SEGMENT"); 1706 metaslab_print_weight(weight); 1707 mdb_printf("\n"); 1708 1709 return (DCMD_OK); 1710 } 1711 1712 /* ARGSUSED */ 1713 static int 1714 metaslab_trace(uintptr_t addr, uint_t flags, int argc, const mdb_arg_t *argv) 1715 { 1716 mdb_metaslab_alloc_trace_t mat; 1717 mdb_metaslab_group_t mg = { 0 }; 1718 char result_type[100]; 1719 1720 if (mdb_ctf_vread(&mat, "metaslab_alloc_trace_t", 1721 "mdb_metaslab_alloc_trace_t", addr, 0) == -1) { 1722 return (DCMD_ERR); 1723 } 1724 1725 if (!(flags & DCMD_PIPE_OUT) && DCMD_HDRSPEC(flags)) { 1726 mdb_printf("%<u>%6s %6s %8s %11s %18s %18s%</u>\n", 1727 "MSID", "DVA", "ASIZE", "WEIGHT", "RESULT", "VDEV"); 1728 } 1729 1730 if (mat.mat_msp != NULL) { 1731 mdb_metaslab_t ms; 1732 1733 if (mdb_ctf_vread(&ms, "metaslab_t", "mdb_metaslab_t", 1734 mat.mat_msp, 0) == -1) { 1735 return (DCMD_ERR); 1736 } 1737 mdb_printf("%6llu ", ms.ms_id); 1738 } else { 1739 mdb_printf("%6s ", "-"); 1740 } 1741 1742 mdb_printf("%6d %8llx ", mat.mat_dva_id, mat.mat_size); 1743 1744 metaslab_print_weight(mat.mat_weight); 1745 1746 if ((int64_t)mat.mat_offset < 0) { 1747 if (enum_lookup("enum trace_alloc_type", mat.mat_offset, 1748 "TRACE_", sizeof (result_type), result_type) == -1) { 1749 mdb_warn("Could not find enum for trace_alloc_type"); 1750 return (DCMD_ERR); 1751 } 1752 mdb_printf("%18s ", result_type); 1753 } else { 1754 mdb_printf("%<b>%18llx%</b> ", mat.mat_offset); 1755 } 1756 1757 if (mat.mat_mg != NULL && 1758 mdb_ctf_vread(&mg, "metaslab_group_t", "mdb_metaslab_group_t", 1759 mat.mat_mg, 0) == -1) { 1760 return (DCMD_ERR); 1761 } 1762 1763 if (mg.mg_vd != NULL) { 1764 mdb_vdev_t vdev; 1765 char desc[MAXNAMELEN]; 1766 1767 if (mdb_ctf_vread(&vdev, "vdev_t", "mdb_vdev_t", 1768 mg.mg_vd, 0) == -1) { 1769 return (DCMD_ERR); 1770 } 1771 1772 if (vdev.vdev_path != NULL) { 1773 char path[MAXNAMELEN]; 1774 1775 if (mdb_readstr(path, sizeof (path), 1776 vdev.vdev_path) == -1) { 1777 mdb_warn("failed to read vdev_path at %p\n", 1778 vdev.vdev_path); 1779 return (DCMD_ERR); 1780 } 1781 char *slash; 1782 if ((slash = strrchr(path, '/')) != NULL) { 1783 strcpy(desc, slash + 1); 1784 } else { 1785 strcpy(desc, path); 1786 } 1787 } else if (vdev.vdev_ops != NULL) { 1788 mdb_vdev_ops_t ops; 1789 if (mdb_ctf_vread(&ops, "vdev_ops_t", "mdb_vdev_ops_t", 1790 vdev.vdev_ops, 0) == -1) { 1791 mdb_warn("failed to read vdev_ops at %p\n", 1792 vdev.vdev_ops); 1793 return (DCMD_ERR); 1794 } 1795 (void) mdb_snprintf(desc, sizeof (desc), 1796 "%s-%llu", ops.vdev_op_type, vdev.vdev_id); 1797 } else { 1798 (void) strcpy(desc, "<unknown>"); 1799 } 1800 mdb_printf("%18s\n", desc); 1801 } 1802 1803 return (DCMD_OK); 1804 } 1805 1806 typedef struct metaslab_walk_data { 1807 uint64_t mw_numvdevs; 1808 uintptr_t *mw_vdevs; 1809 int mw_curvdev; 1810 uint64_t mw_nummss; 1811 uintptr_t *mw_mss; 1812 int mw_curms; 1813 } metaslab_walk_data_t; 1814 1815 static int 1816 metaslab_walk_step(mdb_walk_state_t *wsp) 1817 { 1818 metaslab_walk_data_t *mw = wsp->walk_data; 1819 metaslab_t ms; 1820 uintptr_t msp; 1821 1822 if (mw->mw_curvdev >= mw->mw_numvdevs) 1823 return (WALK_DONE); 1824 1825 if (mw->mw_mss == NULL) { 1826 uintptr_t mssp; 1827 uintptr_t vdevp; 1828 1829 ASSERT(mw->mw_curms == 0); 1830 ASSERT(mw->mw_nummss == 0); 1831 1832 vdevp = mw->mw_vdevs[mw->mw_curvdev]; 1833 if (GETMEMB(vdevp, "vdev", vdev_ms, mssp) || 1834 GETMEMB(vdevp, "vdev", vdev_ms_count, mw->mw_nummss)) { 1835 return (WALK_ERR); 1836 } 1837 1838 mw->mw_mss = mdb_alloc(mw->mw_nummss * sizeof (void*), 1839 UM_SLEEP | UM_GC); 1840 if (mdb_vread(mw->mw_mss, mw->mw_nummss * sizeof (void*), 1841 mssp) == -1) { 1842 mdb_warn("failed to read vdev_ms at %p", mssp); 1843 return (WALK_ERR); 1844 } 1845 } 1846 1847 if (mw->mw_curms >= mw->mw_nummss) { 1848 mw->mw_mss = NULL; 1849 mw->mw_curms = 0; 1850 mw->mw_nummss = 0; 1851 mw->mw_curvdev++; 1852 return (WALK_NEXT); 1853 } 1854 1855 msp = mw->mw_mss[mw->mw_curms]; 1856 if (mdb_vread(&ms, sizeof (metaslab_t), msp) == -1) { 1857 mdb_warn("failed to read metaslab_t at %p", msp); 1858 return (WALK_ERR); 1859 } 1860 1861 mw->mw_curms++; 1862 1863 return (wsp->walk_callback(msp, &ms, wsp->walk_cbdata)); 1864 } 1865 1866 static int 1867 metaslab_walk_init(mdb_walk_state_t *wsp) 1868 { 1869 metaslab_walk_data_t *mw; 1870 uintptr_t root_vdevp; 1871 uintptr_t childp; 1872 1873 if (wsp->walk_addr == NULL) { 1874 mdb_warn("must supply address of spa_t\n"); 1875 return (WALK_ERR); 1876 } 1877 1878 mw = mdb_zalloc(sizeof (metaslab_walk_data_t), UM_SLEEP | UM_GC); 1879 1880 if (GETMEMB(wsp->walk_addr, "spa", spa_root_vdev, root_vdevp) || 1881 GETMEMB(root_vdevp, "vdev", vdev_children, mw->mw_numvdevs) || 1882 GETMEMB(root_vdevp, "vdev", vdev_child, childp)) { 1883 return (DCMD_ERR); 1884 } 1885 1886 mw->mw_vdevs = mdb_alloc(mw->mw_numvdevs * sizeof (void *), 1887 UM_SLEEP | UM_GC); 1888 if (mdb_vread(mw->mw_vdevs, mw->mw_numvdevs * sizeof (void *), 1889 childp) == -1) { 1890 mdb_warn("failed to read root vdev children at %p", childp); 1891 return (DCMD_ERR); 1892 } 1893 1894 wsp->walk_data = mw; 1895 1896 return (WALK_NEXT); 1897 } 1898 1899 typedef struct mdb_spa { 1900 uintptr_t spa_dsl_pool; 1901 uintptr_t spa_root_vdev; 1902 } mdb_spa_t; 1903 1904 typedef struct mdb_dsl_pool { 1905 uintptr_t dp_root_dir; 1906 } mdb_dsl_pool_t; 1907 1908 typedef struct mdb_dsl_dir { 1909 uintptr_t dd_dbuf; 1910 int64_t dd_space_towrite[TXG_SIZE]; 1911 } mdb_dsl_dir_t; 1912 1913 typedef struct mdb_dsl_dir_phys { 1914 uint64_t dd_used_bytes; 1915 uint64_t dd_compressed_bytes; 1916 uint64_t dd_uncompressed_bytes; 1917 } mdb_dsl_dir_phys_t; 1918 1919 typedef struct space_data { 1920 uint64_t ms_alloctree[TXG_SIZE]; 1921 uint64_t ms_freeingtree; 1922 uint64_t ms_freedtree; 1923 uint64_t ms_tree; 1924 int64_t ms_deferspace; 1925 uint64_t avail; 1926 uint64_t nowavail; 1927 } space_data_t; 1928 1929 /* ARGSUSED */ 1930 static int 1931 space_cb(uintptr_t addr, const void *unknown, void *arg) 1932 { 1933 space_data_t *sd = arg; 1934 mdb_metaslab_t ms; 1935 mdb_range_tree_t rt; 1936 mdb_space_map_t sm = { 0 }; 1937 mdb_space_map_phys_t smp = { 0 }; 1938 int i; 1939 1940 if (mdb_ctf_vread(&ms, "metaslab_t", "mdb_metaslab_t", 1941 addr, 0) == -1) 1942 return (WALK_ERR); 1943 1944 for (i = 0; i < TXG_SIZE; i++) { 1945 if (mdb_ctf_vread(&rt, "range_tree_t", 1946 "mdb_range_tree_t", ms.ms_alloctree[i], 0) == -1) 1947 return (WALK_ERR); 1948 1949 sd->ms_alloctree[i] += rt.rt_space; 1950 1951 } 1952 1953 if (mdb_ctf_vread(&rt, "range_tree_t", 1954 "mdb_range_tree_t", ms.ms_freeingtree, 0) == -1) 1955 return (WALK_ERR); 1956 sd->ms_freeingtree += rt.rt_space; 1957 1958 if (mdb_ctf_vread(&rt, "range_tree_t", 1959 "mdb_range_tree_t", ms.ms_freedtree, 0) == -1) 1960 return (WALK_ERR); 1961 sd->ms_freedtree += rt.rt_space; 1962 1963 if (mdb_ctf_vread(&rt, "range_tree_t", 1964 "mdb_range_tree_t", ms.ms_tree, 0) == -1) 1965 return (WALK_ERR); 1966 sd->ms_tree += rt.rt_space; 1967 1968 if (ms.ms_sm != NULL && 1969 mdb_ctf_vread(&sm, "space_map_t", 1970 "mdb_space_map_t", ms.ms_sm, 0) == -1) 1971 return (WALK_ERR); 1972 1973 if (sm.sm_phys != NULL) { 1974 (void) mdb_ctf_vread(&smp, "space_map_phys_t", 1975 "mdb_space_map_phys_t", sm.sm_phys, 0); 1976 } 1977 1978 sd->ms_deferspace += ms.ms_deferspace; 1979 sd->avail += sm.sm_size - sm.sm_alloc; 1980 sd->nowavail += sm.sm_size - smp.smp_alloc; 1981 1982 return (WALK_NEXT); 1983 } 1984 1985 /* 1986 * ::spa_space [-b] 1987 * 1988 * Given a spa_t, print out it's on-disk space usage and in-core 1989 * estimates of future usage. If -b is given, print space in bytes. 1990 * Otherwise print in megabytes. 1991 */ 1992 /* ARGSUSED */ 1993 static int 1994 spa_space(uintptr_t addr, uint_t flags, int argc, const mdb_arg_t *argv) 1995 { 1996 mdb_spa_t spa; 1997 mdb_dsl_pool_t dp; 1998 mdb_dsl_dir_t dd; 1999 mdb_dmu_buf_impl_t db; 2000 mdb_dsl_dir_phys_t dsp; 2001 space_data_t sd; 2002 int shift = 20; 2003 char *suffix = "M"; 2004 int bytes = B_FALSE; 2005 2006 if (mdb_getopts(argc, argv, 'b', MDB_OPT_SETBITS, TRUE, &bytes, NULL) != 2007 argc) 2008 return (DCMD_USAGE); 2009 if (!(flags & DCMD_ADDRSPEC)) 2010 return (DCMD_USAGE); 2011 2012 if (bytes) { 2013 shift = 0; 2014 suffix = ""; 2015 } 2016 2017 if (mdb_ctf_vread(&spa, ZFS_STRUCT "spa", "mdb_spa_t", 2018 addr, 0) == -1 || 2019 mdb_ctf_vread(&dp, ZFS_STRUCT "dsl_pool", "mdb_dsl_pool_t", 2020 spa.spa_dsl_pool, 0) == -1 || 2021 mdb_ctf_vread(&dd, ZFS_STRUCT "dsl_dir", "mdb_dsl_dir_t", 2022 dp.dp_root_dir, 0) == -1 || 2023 mdb_ctf_vread(&db, ZFS_STRUCT "dmu_buf_impl", "mdb_dmu_buf_impl_t", 2024 dd.dd_dbuf, 0) == -1 || 2025 mdb_ctf_vread(&dsp, ZFS_STRUCT "dsl_dir_phys", 2026 "mdb_dsl_dir_phys_t", db.db.db_data, 0) == -1) { 2027 return (DCMD_ERR); 2028 } 2029 2030 mdb_printf("dd_space_towrite = %llu%s %llu%s %llu%s %llu%s\n", 2031 dd.dd_space_towrite[0] >> shift, suffix, 2032 dd.dd_space_towrite[1] >> shift, suffix, 2033 dd.dd_space_towrite[2] >> shift, suffix, 2034 dd.dd_space_towrite[3] >> shift, suffix); 2035 2036 mdb_printf("dd_phys.dd_used_bytes = %llu%s\n", 2037 dsp.dd_used_bytes >> shift, suffix); 2038 mdb_printf("dd_phys.dd_compressed_bytes = %llu%s\n", 2039 dsp.dd_compressed_bytes >> shift, suffix); 2040 mdb_printf("dd_phys.dd_uncompressed_bytes = %llu%s\n", 2041 dsp.dd_uncompressed_bytes >> shift, suffix); 2042 2043 bzero(&sd, sizeof (sd)); 2044 if (mdb_pwalk("metaslab", space_cb, &sd, addr) != 0) { 2045 mdb_warn("can't walk metaslabs"); 2046 return (DCMD_ERR); 2047 } 2048 2049 mdb_printf("ms_allocmap = %llu%s %llu%s %llu%s %llu%s\n", 2050 sd.ms_alloctree[0] >> shift, suffix, 2051 sd.ms_alloctree[1] >> shift, suffix, 2052 sd.ms_alloctree[2] >> shift, suffix, 2053 sd.ms_alloctree[3] >> shift, suffix); 2054 mdb_printf("ms_freeingtree = %llu%s\n", 2055 sd.ms_freeingtree >> shift, suffix); 2056 mdb_printf("ms_freedtree = %llu%s\n", 2057 sd.ms_freedtree >> shift, suffix); 2058 mdb_printf("ms_tree = %llu%s\n", sd.ms_tree >> shift, suffix); 2059 mdb_printf("ms_deferspace = %llu%s\n", 2060 sd.ms_deferspace >> shift, suffix); 2061 mdb_printf("last synced avail = %llu%s\n", sd.avail >> shift, suffix); 2062 mdb_printf("current syncing avail = %llu%s\n", 2063 sd.nowavail >> shift, suffix); 2064 2065 return (DCMD_OK); 2066 } 2067 2068 typedef struct mdb_spa_aux_vdev { 2069 int sav_count; 2070 uintptr_t sav_vdevs; 2071 } mdb_spa_aux_vdev_t; 2072 2073 typedef struct mdb_spa_vdevs { 2074 uintptr_t spa_root_vdev; 2075 mdb_spa_aux_vdev_t spa_l2cache; 2076 mdb_spa_aux_vdev_t spa_spares; 2077 } mdb_spa_vdevs_t; 2078 2079 static int 2080 spa_print_aux(mdb_spa_aux_vdev_t *sav, uint_t flags, mdb_arg_t *v, 2081 const char *name) 2082 { 2083 uintptr_t *aux; 2084 size_t len; 2085 int ret, i; 2086 2087 /* 2088 * Iterate over aux vdevs and print those out as well. This is a 2089 * little annoying because we don't have a root vdev to pass to ::vdev. 2090 * Instead, we print a single line and then call it for each child 2091 * vdev. 2092 */ 2093 if (sav->sav_count != 0) { 2094 v[1].a_type = MDB_TYPE_STRING; 2095 v[1].a_un.a_str = "-d"; 2096 v[2].a_type = MDB_TYPE_IMMEDIATE; 2097 v[2].a_un.a_val = 2; 2098 2099 len = sav->sav_count * sizeof (uintptr_t); 2100 aux = mdb_alloc(len, UM_SLEEP); 2101 if (mdb_vread(aux, len, sav->sav_vdevs) == -1) { 2102 mdb_free(aux, len); 2103 mdb_warn("failed to read l2cache vdevs at %p", 2104 sav->sav_vdevs); 2105 return (DCMD_ERR); 2106 } 2107 2108 mdb_printf("%-?s %-9s %-12s %s\n", "-", "-", "-", name); 2109 2110 for (i = 0; i < sav->sav_count; i++) { 2111 ret = mdb_call_dcmd("vdev", aux[i], flags, 3, v); 2112 if (ret != DCMD_OK) { 2113 mdb_free(aux, len); 2114 return (ret); 2115 } 2116 } 2117 2118 mdb_free(aux, len); 2119 } 2120 2121 return (0); 2122 } 2123 2124 /* 2125 * ::spa_vdevs 2126 * 2127 * -e Include error stats 2128 * -m Include metaslab information 2129 * -M Include metaslab group information 2130 * -h Include histogram information (requires -m or -M) 2131 * 2132 * Print out a summarized list of vdevs for the given spa_t. 2133 * This is accomplished by invoking "::vdev -re" on the root vdev, as well as 2134 * iterating over the cache devices. 2135 */ 2136 /* ARGSUSED */ 2137 static int 2138 spa_vdevs(uintptr_t addr, uint_t flags, int argc, const mdb_arg_t *argv) 2139 { 2140 mdb_arg_t v[3]; 2141 int ret; 2142 char opts[100] = "-r"; 2143 int spa_flags = 0; 2144 2145 if (mdb_getopts(argc, argv, 2146 'e', MDB_OPT_SETBITS, SPA_FLAG_ERRORS, &spa_flags, 2147 'm', MDB_OPT_SETBITS, SPA_FLAG_METASLABS, &spa_flags, 2148 'M', MDB_OPT_SETBITS, SPA_FLAG_METASLAB_GROUPS, &spa_flags, 2149 'h', MDB_OPT_SETBITS, SPA_FLAG_HISTOGRAMS, &spa_flags, 2150 NULL) != argc) 2151 return (DCMD_USAGE); 2152 2153 if (!(flags & DCMD_ADDRSPEC)) 2154 return (DCMD_USAGE); 2155 2156 mdb_spa_vdevs_t spa; 2157 if (mdb_ctf_vread(&spa, "spa_t", "mdb_spa_vdevs_t", addr, 0) == -1) 2158 return (DCMD_ERR); 2159 2160 /* 2161 * Unitialized spa_t structures can have a NULL root vdev. 2162 */ 2163 if (spa.spa_root_vdev == NULL) { 2164 mdb_printf("no associated vdevs\n"); 2165 return (DCMD_OK); 2166 } 2167 2168 if (spa_flags & SPA_FLAG_ERRORS) 2169 strcat(opts, "e"); 2170 if (spa_flags & SPA_FLAG_METASLABS) 2171 strcat(opts, "m"); 2172 if (spa_flags & SPA_FLAG_METASLAB_GROUPS) 2173 strcat(opts, "M"); 2174 if (spa_flags & SPA_FLAG_HISTOGRAMS) 2175 strcat(opts, "h"); 2176 2177 v[0].a_type = MDB_TYPE_STRING; 2178 v[0].a_un.a_str = opts; 2179 2180 ret = mdb_call_dcmd("vdev", (uintptr_t)spa.spa_root_vdev, 2181 flags, 1, v); 2182 if (ret != DCMD_OK) 2183 return (ret); 2184 2185 if (spa_print_aux(&spa.spa_l2cache, flags, v, "cache") != 0 || 2186 spa_print_aux(&spa.spa_spares, flags, v, "spares") != 0) 2187 return (DCMD_ERR); 2188 2189 return (DCMD_OK); 2190 } 2191 2192 /* 2193 * ::zio 2194 * 2195 * Print a summary of zio_t and all its children. This is intended to display a 2196 * zio tree, and hence we only pick the most important pieces of information for 2197 * the main summary. More detailed information can always be found by doing a 2198 * '::print zio' on the underlying zio_t. The columns we display are: 2199 * 2200 * ADDRESS TYPE STAGE WAITER TIME_ELAPSED 2201 * 2202 * The 'address' column is indented by one space for each depth level as we 2203 * descend down the tree. 2204 */ 2205 2206 #define ZIO_MAXINDENT 7 2207 #define ZIO_MAXWIDTH (sizeof (uintptr_t) * 2 + ZIO_MAXINDENT) 2208 #define ZIO_WALK_SELF 0 2209 #define ZIO_WALK_CHILD 1 2210 #define ZIO_WALK_PARENT 2 2211 2212 typedef struct zio_print_args { 2213 int zpa_current_depth; 2214 int zpa_min_depth; 2215 int zpa_max_depth; 2216 int zpa_type; 2217 uint_t zpa_flags; 2218 } zio_print_args_t; 2219 2220 typedef struct mdb_zio { 2221 enum zio_type io_type; 2222 enum zio_stage io_stage; 2223 uintptr_t io_waiter; 2224 uintptr_t io_spa; 2225 struct { 2226 struct { 2227 uintptr_t list_next; 2228 } list_head; 2229 } io_parent_list; 2230 int io_error; 2231 } mdb_zio_t; 2232 2233 typedef struct mdb_zio_timestamp { 2234 hrtime_t io_timestamp; 2235 } mdb_zio_timestamp_t; 2236 2237 static int zio_child_cb(uintptr_t addr, const void *unknown, void *arg); 2238 2239 static int 2240 zio_print_cb(uintptr_t addr, zio_print_args_t *zpa) 2241 { 2242 mdb_ctf_id_t type_enum, stage_enum; 2243 int indent = zpa->zpa_current_depth; 2244 const char *type, *stage; 2245 uintptr_t laddr; 2246 mdb_zio_t zio; 2247 mdb_zio_timestamp_t zio_timestamp = { 0 }; 2248 2249 if (mdb_ctf_vread(&zio, ZFS_STRUCT "zio", "mdb_zio_t", addr, 0) == -1) 2250 return (WALK_ERR); 2251 (void) mdb_ctf_vread(&zio_timestamp, ZFS_STRUCT "zio", 2252 "mdb_zio_timestamp_t", addr, MDB_CTF_VREAD_QUIET); 2253 2254 if (indent > ZIO_MAXINDENT) 2255 indent = ZIO_MAXINDENT; 2256 2257 if (mdb_ctf_lookup_by_name("enum zio_type", &type_enum) == -1 || 2258 mdb_ctf_lookup_by_name("enum zio_stage", &stage_enum) == -1) { 2259 mdb_warn("failed to lookup zio enums"); 2260 return (WALK_ERR); 2261 } 2262 2263 if ((type = mdb_ctf_enum_name(type_enum, zio.io_type)) != NULL) 2264 type += sizeof ("ZIO_TYPE_") - 1; 2265 else 2266 type = "?"; 2267 2268 if (zio.io_error == 0) { 2269 stage = mdb_ctf_enum_name(stage_enum, zio.io_stage); 2270 if (stage != NULL) 2271 stage += sizeof ("ZIO_STAGE_") - 1; 2272 else 2273 stage = "?"; 2274 } else { 2275 stage = "FAILED"; 2276 } 2277 2278 if (zpa->zpa_current_depth >= zpa->zpa_min_depth) { 2279 if (zpa->zpa_flags & DCMD_PIPE_OUT) { 2280 mdb_printf("%?p\n", addr); 2281 } else { 2282 mdb_printf("%*s%-*p %-5s %-16s ", indent, "", 2283 ZIO_MAXWIDTH - indent, addr, type, stage); 2284 if (zio.io_waiter != 0) 2285 mdb_printf("%-16lx ", zio.io_waiter); 2286 else 2287 mdb_printf("%-16s ", "-"); 2288 #ifdef _KERNEL 2289 if (zio_timestamp.io_timestamp != 0) { 2290 mdb_printf("%llums", (mdb_gethrtime() - 2291 zio_timestamp.io_timestamp) / 2292 1000000); 2293 } else { 2294 mdb_printf("%-12s ", "-"); 2295 } 2296 #else 2297 mdb_printf("%-12s ", "-"); 2298 #endif 2299 mdb_printf("\n"); 2300 } 2301 } 2302 2303 if (zpa->zpa_current_depth >= zpa->zpa_max_depth) 2304 return (WALK_NEXT); 2305 2306 if (zpa->zpa_type == ZIO_WALK_PARENT) 2307 laddr = addr + mdb_ctf_offsetof_by_name(ZFS_STRUCT "zio", 2308 "io_parent_list"); 2309 else 2310 laddr = addr + mdb_ctf_offsetof_by_name(ZFS_STRUCT "zio", 2311 "io_child_list"); 2312 2313 zpa->zpa_current_depth++; 2314 if (mdb_pwalk("list", zio_child_cb, zpa, laddr) != 0) { 2315 mdb_warn("failed to walk zio_t children at %p\n", laddr); 2316 return (WALK_ERR); 2317 } 2318 zpa->zpa_current_depth--; 2319 2320 return (WALK_NEXT); 2321 } 2322 2323 /* ARGSUSED */ 2324 static int 2325 zio_child_cb(uintptr_t addr, const void *unknown, void *arg) 2326 { 2327 zio_link_t zl; 2328 uintptr_t ziop; 2329 zio_print_args_t *zpa = arg; 2330 2331 if (mdb_vread(&zl, sizeof (zl), addr) == -1) { 2332 mdb_warn("failed to read zio_link_t at %p", addr); 2333 return (WALK_ERR); 2334 } 2335 2336 if (zpa->zpa_type == ZIO_WALK_PARENT) 2337 ziop = (uintptr_t)zl.zl_parent; 2338 else 2339 ziop = (uintptr_t)zl.zl_child; 2340 2341 return (zio_print_cb(ziop, zpa)); 2342 } 2343 2344 /* ARGSUSED */ 2345 static int 2346 zio_print(uintptr_t addr, uint_t flags, int argc, const mdb_arg_t *argv) 2347 { 2348 zio_print_args_t zpa = { 0 }; 2349 2350 if (!(flags & DCMD_ADDRSPEC)) 2351 return (DCMD_USAGE); 2352 2353 if (mdb_getopts(argc, argv, 2354 'r', MDB_OPT_SETBITS, INT_MAX, &zpa.zpa_max_depth, 2355 'c', MDB_OPT_SETBITS, ZIO_WALK_CHILD, &zpa.zpa_type, 2356 'p', MDB_OPT_SETBITS, ZIO_WALK_PARENT, &zpa.zpa_type, 2357 NULL) != argc) 2358 return (DCMD_USAGE); 2359 2360 zpa.zpa_flags = flags; 2361 if (zpa.zpa_max_depth != 0) { 2362 if (zpa.zpa_type == ZIO_WALK_SELF) 2363 zpa.zpa_type = ZIO_WALK_CHILD; 2364 } else if (zpa.zpa_type != ZIO_WALK_SELF) { 2365 zpa.zpa_min_depth = 1; 2366 zpa.zpa_max_depth = 1; 2367 } 2368 2369 if (!(flags & DCMD_PIPE_OUT) && DCMD_HDRSPEC(flags)) { 2370 mdb_printf("%<u>%-*s %-5s %-16s %-16s %-12s%</u>\n", 2371 ZIO_MAXWIDTH, "ADDRESS", "TYPE", "STAGE", "WAITER", 2372 "TIME_ELAPSED"); 2373 } 2374 2375 if (zio_print_cb(addr, &zpa) != WALK_NEXT) 2376 return (DCMD_ERR); 2377 2378 return (DCMD_OK); 2379 } 2380 2381 /* 2382 * [addr]::zio_state 2383 * 2384 * Print a summary of all zio_t structures on the system, or for a particular 2385 * pool. This is equivalent to '::walk zio_root | ::zio'. 2386 */ 2387 /*ARGSUSED*/ 2388 static int 2389 zio_state(uintptr_t addr, uint_t flags, int argc, const mdb_arg_t *argv) 2390 { 2391 /* 2392 * MDB will remember the last address of the pipeline, so if we don't 2393 * zero this we'll end up trying to walk zio structures for a 2394 * non-existent spa_t. 2395 */ 2396 if (!(flags & DCMD_ADDRSPEC)) 2397 addr = 0; 2398 2399 return (mdb_pwalk_dcmd("zio_root", "zio", argc, argv, addr)); 2400 } 2401 2402 typedef struct mdb_multilist { 2403 uint64_t ml_num_sublists; 2404 uintptr_t ml_sublists; 2405 } mdb_multilist_t; 2406 2407 typedef struct multilist_walk_data { 2408 uint64_t mwd_idx; 2409 mdb_multilist_t mwd_ml; 2410 } multilist_walk_data_t; 2411 2412 /* ARGSUSED */ 2413 static int 2414 multilist_print_cb(uintptr_t addr, const void *unknown, void *arg) 2415 { 2416 mdb_printf("%#lr\n", addr); 2417 return (WALK_NEXT); 2418 } 2419 2420 static int 2421 multilist_walk_step(mdb_walk_state_t *wsp) 2422 { 2423 multilist_walk_data_t *mwd = wsp->walk_data; 2424 2425 if (mwd->mwd_idx >= mwd->mwd_ml.ml_num_sublists) 2426 return (WALK_DONE); 2427 2428 wsp->walk_addr = mwd->mwd_ml.ml_sublists + 2429 mdb_ctf_sizeof_by_name("multilist_sublist_t") * mwd->mwd_idx + 2430 mdb_ctf_offsetof_by_name("multilist_sublist_t", "mls_list"); 2431 2432 mdb_pwalk("list", multilist_print_cb, (void*)NULL, wsp->walk_addr); 2433 mwd->mwd_idx++; 2434 2435 return (WALK_NEXT); 2436 } 2437 2438 static int 2439 multilist_walk_init(mdb_walk_state_t *wsp) 2440 { 2441 multilist_walk_data_t *mwd; 2442 2443 if (wsp->walk_addr == NULL) { 2444 mdb_warn("must supply address of multilist_t\n"); 2445 return (WALK_ERR); 2446 } 2447 2448 mwd = mdb_zalloc(sizeof (multilist_walk_data_t), UM_SLEEP | UM_GC); 2449 if (mdb_ctf_vread(&mwd->mwd_ml, "multilist_t", "mdb_multilist_t", 2450 wsp->walk_addr, 0) == -1) { 2451 return (WALK_ERR); 2452 } 2453 2454 if (mwd->mwd_ml.ml_num_sublists == 0 || 2455 mwd->mwd_ml.ml_sublists == NULL) { 2456 mdb_warn("invalid or uninitialized multilist at %#lx\n", 2457 wsp->walk_addr); 2458 return (WALK_ERR); 2459 } 2460 2461 wsp->walk_data = mwd; 2462 return (WALK_NEXT); 2463 } 2464 2465 typedef struct mdb_txg_list { 2466 size_t tl_offset; 2467 uintptr_t tl_head[TXG_SIZE]; 2468 } mdb_txg_list_t; 2469 2470 typedef struct txg_list_walk_data { 2471 uintptr_t lw_head[TXG_SIZE]; 2472 int lw_txgoff; 2473 int lw_maxoff; 2474 size_t lw_offset; 2475 void *lw_obj; 2476 } txg_list_walk_data_t; 2477 2478 static int 2479 txg_list_walk_init_common(mdb_walk_state_t *wsp, int txg, int maxoff) 2480 { 2481 txg_list_walk_data_t *lwd; 2482 mdb_txg_list_t list; 2483 int i; 2484 2485 lwd = mdb_alloc(sizeof (txg_list_walk_data_t), UM_SLEEP | UM_GC); 2486 if (mdb_ctf_vread(&list, "txg_list_t", "mdb_txg_list_t", wsp->walk_addr, 2487 0) == -1) { 2488 mdb_warn("failed to read txg_list_t at %#lx", wsp->walk_addr); 2489 return (WALK_ERR); 2490 } 2491 2492 for (i = 0; i < TXG_SIZE; i++) 2493 lwd->lw_head[i] = list.tl_head[i]; 2494 lwd->lw_offset = list.tl_offset; 2495 lwd->lw_obj = mdb_alloc(lwd->lw_offset + sizeof (txg_node_t), 2496 UM_SLEEP | UM_GC); 2497 lwd->lw_txgoff = txg; 2498 lwd->lw_maxoff = maxoff; 2499 2500 wsp->walk_addr = lwd->lw_head[lwd->lw_txgoff]; 2501 wsp->walk_data = lwd; 2502 2503 return (WALK_NEXT); 2504 } 2505 2506 static int 2507 txg_list_walk_init(mdb_walk_state_t *wsp) 2508 { 2509 return (txg_list_walk_init_common(wsp, 0, TXG_SIZE-1)); 2510 } 2511 2512 static int 2513 txg_list0_walk_init(mdb_walk_state_t *wsp) 2514 { 2515 return (txg_list_walk_init_common(wsp, 0, 0)); 2516 } 2517 2518 static int 2519 txg_list1_walk_init(mdb_walk_state_t *wsp) 2520 { 2521 return (txg_list_walk_init_common(wsp, 1, 1)); 2522 } 2523 2524 static int 2525 txg_list2_walk_init(mdb_walk_state_t *wsp) 2526 { 2527 return (txg_list_walk_init_common(wsp, 2, 2)); 2528 } 2529 2530 static int 2531 txg_list3_walk_init(mdb_walk_state_t *wsp) 2532 { 2533 return (txg_list_walk_init_common(wsp, 3, 3)); 2534 } 2535 2536 static int 2537 txg_list_walk_step(mdb_walk_state_t *wsp) 2538 { 2539 txg_list_walk_data_t *lwd = wsp->walk_data; 2540 uintptr_t addr; 2541 txg_node_t *node; 2542 int status; 2543 2544 while (wsp->walk_addr == NULL && lwd->lw_txgoff < lwd->lw_maxoff) { 2545 lwd->lw_txgoff++; 2546 wsp->walk_addr = lwd->lw_head[lwd->lw_txgoff]; 2547 } 2548 2549 if (wsp->walk_addr == NULL) 2550 return (WALK_DONE); 2551 2552 addr = wsp->walk_addr - lwd->lw_offset; 2553 2554 if (mdb_vread(lwd->lw_obj, 2555 lwd->lw_offset + sizeof (txg_node_t), addr) == -1) { 2556 mdb_warn("failed to read list element at %#lx", addr); 2557 return (WALK_ERR); 2558 } 2559 2560 status = wsp->walk_callback(addr, lwd->lw_obj, wsp->walk_cbdata); 2561 node = (txg_node_t *)((uintptr_t)lwd->lw_obj + lwd->lw_offset); 2562 wsp->walk_addr = (uintptr_t)node->tn_next[lwd->lw_txgoff]; 2563 2564 return (status); 2565 } 2566 2567 /* 2568 * ::walk spa 2569 * 2570 * Walk all named spa_t structures in the namespace. This is nothing more than 2571 * a layered avl walk. 2572 */ 2573 static int 2574 spa_walk_init(mdb_walk_state_t *wsp) 2575 { 2576 GElf_Sym sym; 2577 2578 if (wsp->walk_addr != NULL) { 2579 mdb_warn("spa walk only supports global walks\n"); 2580 return (WALK_ERR); 2581 } 2582 2583 if (mdb_lookup_by_obj(ZFS_OBJ_NAME, "spa_namespace_avl", &sym) == -1) { 2584 mdb_warn("failed to find symbol 'spa_namespace_avl'"); 2585 return (WALK_ERR); 2586 } 2587 2588 wsp->walk_addr = (uintptr_t)sym.st_value; 2589 2590 if (mdb_layered_walk("avl", wsp) == -1) { 2591 mdb_warn("failed to walk 'avl'\n"); 2592 return (WALK_ERR); 2593 } 2594 2595 return (WALK_NEXT); 2596 } 2597 2598 static int 2599 spa_walk_step(mdb_walk_state_t *wsp) 2600 { 2601 return (wsp->walk_callback(wsp->walk_addr, NULL, wsp->walk_cbdata)); 2602 } 2603 2604 /* 2605 * [addr]::walk zio 2606 * 2607 * Walk all active zio_t structures on the system. This is simply a layered 2608 * walk on top of ::walk zio_cache, with the optional ability to limit the 2609 * structures to a particular pool. 2610 */ 2611 static int 2612 zio_walk_init(mdb_walk_state_t *wsp) 2613 { 2614 wsp->walk_data = (void *)wsp->walk_addr; 2615 2616 if (mdb_layered_walk("zio_cache", wsp) == -1) { 2617 mdb_warn("failed to walk 'zio_cache'\n"); 2618 return (WALK_ERR); 2619 } 2620 2621 return (WALK_NEXT); 2622 } 2623 2624 static int 2625 zio_walk_step(mdb_walk_state_t *wsp) 2626 { 2627 mdb_zio_t zio; 2628 uintptr_t spa = (uintptr_t)wsp->walk_data; 2629 2630 if (mdb_ctf_vread(&zio, ZFS_STRUCT "zio", "mdb_zio_t", 2631 wsp->walk_addr, 0) == -1) 2632 return (WALK_ERR); 2633 2634 if (spa != 0 && spa != zio.io_spa) 2635 return (WALK_NEXT); 2636 2637 return (wsp->walk_callback(wsp->walk_addr, &zio, wsp->walk_cbdata)); 2638 } 2639 2640 /* 2641 * [addr]::walk zio_root 2642 * 2643 * Walk only root zio_t structures, optionally for a particular spa_t. 2644 */ 2645 static int 2646 zio_walk_root_step(mdb_walk_state_t *wsp) 2647 { 2648 mdb_zio_t zio; 2649 uintptr_t spa = (uintptr_t)wsp->walk_data; 2650 2651 if (mdb_ctf_vread(&zio, ZFS_STRUCT "zio", "mdb_zio_t", 2652 wsp->walk_addr, 0) == -1) 2653 return (WALK_ERR); 2654 2655 if (spa != 0 && spa != zio.io_spa) 2656 return (WALK_NEXT); 2657 2658 /* If the parent list is not empty, ignore */ 2659 if (zio.io_parent_list.list_head.list_next != 2660 wsp->walk_addr + 2661 mdb_ctf_offsetof_by_name(ZFS_STRUCT "zio", "io_parent_list") + 2662 mdb_ctf_offsetof_by_name("struct list", "list_head")) 2663 return (WALK_NEXT); 2664 2665 return (wsp->walk_callback(wsp->walk_addr, &zio, wsp->walk_cbdata)); 2666 } 2667 2668 /* 2669 * ::zfs_blkstats 2670 * 2671 * -v print verbose per-level information 2672 * 2673 */ 2674 static int 2675 zfs_blkstats(uintptr_t addr, uint_t flags, int argc, const mdb_arg_t *argv) 2676 { 2677 boolean_t verbose = B_FALSE; 2678 zfs_all_blkstats_t stats; 2679 dmu_object_type_t t; 2680 zfs_blkstat_t *tzb; 2681 uint64_t ditto; 2682 dmu_object_type_info_t dmu_ot[DMU_OT_NUMTYPES + 10]; 2683 /* +10 in case it grew */ 2684 2685 if (mdb_readvar(&dmu_ot, "dmu_ot") == -1) { 2686 mdb_warn("failed to read 'dmu_ot'"); 2687 return (DCMD_ERR); 2688 } 2689 2690 if (mdb_getopts(argc, argv, 2691 'v', MDB_OPT_SETBITS, TRUE, &verbose, 2692 NULL) != argc) 2693 return (DCMD_USAGE); 2694 2695 if (!(flags & DCMD_ADDRSPEC)) 2696 return (DCMD_USAGE); 2697 2698 if (GETMEMB(addr, "spa", spa_dsl_pool, addr) || 2699 GETMEMB(addr, "dsl_pool", dp_blkstats, addr) || 2700 mdb_vread(&stats, sizeof (zfs_all_blkstats_t), addr) == -1) { 2701 mdb_warn("failed to read data at %p;", addr); 2702 mdb_printf("maybe no stats? run \"zpool scrub\" first."); 2703 return (DCMD_ERR); 2704 } 2705 2706 tzb = &stats.zab_type[DN_MAX_LEVELS][DMU_OT_TOTAL]; 2707 if (tzb->zb_gangs != 0) { 2708 mdb_printf("Ganged blocks: %llu\n", 2709 (longlong_t)tzb->zb_gangs); 2710 } 2711 2712 ditto = tzb->zb_ditto_2_of_2_samevdev + tzb->zb_ditto_2_of_3_samevdev + 2713 tzb->zb_ditto_3_of_3_samevdev; 2714 if (ditto != 0) { 2715 mdb_printf("Dittoed blocks on same vdev: %llu\n", 2716 (longlong_t)ditto); 2717 } 2718 2719 mdb_printf("\nBlocks\tLSIZE\tPSIZE\tASIZE" 2720 "\t avg\t comp\t%%Total\tType\n"); 2721 2722 for (t = 0; t <= DMU_OT_TOTAL; t++) { 2723 char csize[NICENUM_BUFLEN], lsize[NICENUM_BUFLEN]; 2724 char psize[NICENUM_BUFLEN], asize[NICENUM_BUFLEN]; 2725 char avg[NICENUM_BUFLEN]; 2726 char comp[NICENUM_BUFLEN], pct[NICENUM_BUFLEN]; 2727 char typename[64]; 2728 int l; 2729 2730 2731 if (t == DMU_OT_DEFERRED) 2732 strcpy(typename, "deferred free"); 2733 else if (t == DMU_OT_OTHER) 2734 strcpy(typename, "other"); 2735 else if (t == DMU_OT_TOTAL) 2736 strcpy(typename, "Total"); 2737 else if (mdb_readstr(typename, sizeof (typename), 2738 (uintptr_t)dmu_ot[t].ot_name) == -1) { 2739 mdb_warn("failed to read type name"); 2740 return (DCMD_ERR); 2741 } 2742 2743 if (stats.zab_type[DN_MAX_LEVELS][t].zb_asize == 0) 2744 continue; 2745 2746 for (l = -1; l < DN_MAX_LEVELS; l++) { 2747 int level = (l == -1 ? DN_MAX_LEVELS : l); 2748 zfs_blkstat_t *zb = &stats.zab_type[level][t]; 2749 2750 if (zb->zb_asize == 0) 2751 continue; 2752 2753 /* 2754 * Don't print each level unless requested. 2755 */ 2756 if (!verbose && level != DN_MAX_LEVELS) 2757 continue; 2758 2759 /* 2760 * If all the space is level 0, don't print the 2761 * level 0 separately. 2762 */ 2763 if (level == 0 && zb->zb_asize == 2764 stats.zab_type[DN_MAX_LEVELS][t].zb_asize) 2765 continue; 2766 2767 mdb_nicenum(zb->zb_count, csize); 2768 mdb_nicenum(zb->zb_lsize, lsize); 2769 mdb_nicenum(zb->zb_psize, psize); 2770 mdb_nicenum(zb->zb_asize, asize); 2771 mdb_nicenum(zb->zb_asize / zb->zb_count, avg); 2772 (void) snprintfrac(comp, NICENUM_BUFLEN, 2773 zb->zb_lsize, zb->zb_psize, 2); 2774 (void) snprintfrac(pct, NICENUM_BUFLEN, 2775 100 * zb->zb_asize, tzb->zb_asize, 2); 2776 2777 mdb_printf("%6s\t%5s\t%5s\t%5s\t%5s" 2778 "\t%5s\t%6s\t", 2779 csize, lsize, psize, asize, avg, comp, pct); 2780 2781 if (level == DN_MAX_LEVELS) 2782 mdb_printf("%s\n", typename); 2783 else 2784 mdb_printf(" L%d %s\n", 2785 level, typename); 2786 } 2787 } 2788 2789 return (DCMD_OK); 2790 } 2791 2792 typedef struct mdb_reference { 2793 uintptr_t ref_holder; 2794 uintptr_t ref_removed; 2795 uint64_t ref_number; 2796 } mdb_reference_t; 2797 2798 /* ARGSUSED */ 2799 static int 2800 reference_cb(uintptr_t addr, const void *ignored, void *arg) 2801 { 2802 mdb_reference_t ref; 2803 boolean_t holder_is_str = B_FALSE; 2804 char holder_str[128]; 2805 boolean_t removed = (boolean_t)arg; 2806 2807 if (mdb_ctf_vread(&ref, "reference_t", "mdb_reference_t", addr, 2808 0) == -1) 2809 return (DCMD_ERR); 2810 2811 if (mdb_readstr(holder_str, sizeof (holder_str), 2812 ref.ref_holder) != -1) 2813 holder_is_str = strisprint(holder_str); 2814 2815 if (removed) 2816 mdb_printf("removed "); 2817 mdb_printf("reference "); 2818 if (ref.ref_number != 1) 2819 mdb_printf("with count=%llu ", ref.ref_number); 2820 mdb_printf("with tag %lx", ref.ref_holder); 2821 if (holder_is_str) 2822 mdb_printf(" \"%s\"", holder_str); 2823 mdb_printf(", held at:\n"); 2824 2825 (void) mdb_call_dcmd("whatis", addr, DCMD_ADDRSPEC, 0, NULL); 2826 2827 if (removed) { 2828 mdb_printf("removed at:\n"); 2829 (void) mdb_call_dcmd("whatis", ref.ref_removed, 2830 DCMD_ADDRSPEC, 0, NULL); 2831 } 2832 2833 mdb_printf("\n"); 2834 2835 return (WALK_NEXT); 2836 } 2837 2838 typedef struct mdb_refcount { 2839 uint64_t rc_count; 2840 } mdb_refcount_t; 2841 2842 typedef struct mdb_refcount_removed { 2843 uint64_t rc_removed_count; 2844 } mdb_refcount_removed_t; 2845 2846 typedef struct mdb_refcount_tracked { 2847 boolean_t rc_tracked; 2848 } mdb_refcount_tracked_t; 2849 2850 /* ARGSUSED */ 2851 static int 2852 refcount(uintptr_t addr, uint_t flags, int argc, const mdb_arg_t *argv) 2853 { 2854 mdb_refcount_t rc; 2855 mdb_refcount_removed_t rcr; 2856 mdb_refcount_tracked_t rct; 2857 int off; 2858 boolean_t released = B_FALSE; 2859 2860 if (!(flags & DCMD_ADDRSPEC)) 2861 return (DCMD_USAGE); 2862 2863 if (mdb_getopts(argc, argv, 2864 'r', MDB_OPT_SETBITS, B_TRUE, &released, 2865 NULL) != argc) 2866 return (DCMD_USAGE); 2867 2868 if (mdb_ctf_vread(&rc, "refcount_t", "mdb_refcount_t", addr, 2869 0) == -1) 2870 return (DCMD_ERR); 2871 2872 if (mdb_ctf_vread(&rcr, "refcount_t", "mdb_refcount_removed_t", addr, 2873 MDB_CTF_VREAD_QUIET) == -1) { 2874 mdb_printf("refcount_t at %p has %llu holds (untracked)\n", 2875 addr, (longlong_t)rc.rc_count); 2876 return (DCMD_OK); 2877 } 2878 2879 if (mdb_ctf_vread(&rct, "refcount_t", "mdb_refcount_tracked_t", addr, 2880 MDB_CTF_VREAD_QUIET) == -1) { 2881 /* If this is an old target, it might be tracked. */ 2882 rct.rc_tracked = B_TRUE; 2883 } 2884 2885 mdb_printf("refcount_t at %p has %llu current holds, " 2886 "%llu recently released holds\n", 2887 addr, (longlong_t)rc.rc_count, (longlong_t)rcr.rc_removed_count); 2888 2889 if (rct.rc_tracked && rc.rc_count > 0) 2890 mdb_printf("current holds:\n"); 2891 off = mdb_ctf_offsetof_by_name("refcount_t", "rc_list"); 2892 if (off == -1) 2893 return (DCMD_ERR); 2894 mdb_pwalk("list", reference_cb, (void*)B_FALSE, addr + off); 2895 2896 if (released && rcr.rc_removed_count > 0) { 2897 mdb_printf("released holds:\n"); 2898 2899 off = mdb_ctf_offsetof_by_name("refcount_t", "rc_removed"); 2900 if (off == -1) 2901 return (DCMD_ERR); 2902 mdb_pwalk("list", reference_cb, (void*)B_TRUE, addr + off); 2903 } 2904 2905 return (DCMD_OK); 2906 } 2907 2908 /* ARGSUSED */ 2909 static int 2910 sa_attr_table(uintptr_t addr, uint_t flags, int argc, const mdb_arg_t *argv) 2911 { 2912 sa_attr_table_t *table; 2913 sa_os_t sa_os; 2914 char *name; 2915 int i; 2916 2917 if (mdb_vread(&sa_os, sizeof (sa_os_t), addr) == -1) { 2918 mdb_warn("failed to read sa_os at %p", addr); 2919 return (DCMD_ERR); 2920 } 2921 2922 table = mdb_alloc(sizeof (sa_attr_table_t) * sa_os.sa_num_attrs, 2923 UM_SLEEP | UM_GC); 2924 name = mdb_alloc(MAXPATHLEN, UM_SLEEP | UM_GC); 2925 2926 if (mdb_vread(table, sizeof (sa_attr_table_t) * sa_os.sa_num_attrs, 2927 (uintptr_t)sa_os.sa_attr_table) == -1) { 2928 mdb_warn("failed to read sa_os at %p", addr); 2929 return (DCMD_ERR); 2930 } 2931 2932 mdb_printf("%<u>%-10s %-10s %-10s %-10s %s%</u>\n", 2933 "ATTR ID", "REGISTERED", "LENGTH", "BSWAP", "NAME"); 2934 for (i = 0; i != sa_os.sa_num_attrs; i++) { 2935 mdb_readstr(name, MAXPATHLEN, (uintptr_t)table[i].sa_name); 2936 mdb_printf("%5x %8x %8x %8x %-s\n", 2937 (int)table[i].sa_attr, (int)table[i].sa_registered, 2938 (int)table[i].sa_length, table[i].sa_byteswap, name); 2939 } 2940 2941 return (DCMD_OK); 2942 } 2943 2944 static int 2945 sa_get_off_table(uintptr_t addr, uint32_t **off_tab, int attr_count) 2946 { 2947 uintptr_t idx_table; 2948 2949 if (GETMEMB(addr, "sa_idx_tab", sa_idx_tab, idx_table)) { 2950 mdb_printf("can't find offset table in sa_idx_tab\n"); 2951 return (-1); 2952 } 2953 2954 *off_tab = mdb_alloc(attr_count * sizeof (uint32_t), 2955 UM_SLEEP | UM_GC); 2956 2957 if (mdb_vread(*off_tab, 2958 attr_count * sizeof (uint32_t), idx_table) == -1) { 2959 mdb_warn("failed to attribute offset table %p", idx_table); 2960 return (-1); 2961 } 2962 2963 return (DCMD_OK); 2964 } 2965 2966 /*ARGSUSED*/ 2967 static int 2968 sa_attr_print(uintptr_t addr, uint_t flags, int argc, const mdb_arg_t *argv) 2969 { 2970 uint32_t *offset_tab; 2971 int attr_count; 2972 uint64_t attr_id; 2973 uintptr_t attr_addr; 2974 uintptr_t bonus_tab, spill_tab; 2975 uintptr_t db_bonus, db_spill; 2976 uintptr_t os, os_sa; 2977 uintptr_t db_data; 2978 2979 if (argc != 1) 2980 return (DCMD_USAGE); 2981 2982 if (argv[0].a_type == MDB_TYPE_STRING) 2983 attr_id = mdb_strtoull(argv[0].a_un.a_str); 2984 else 2985 return (DCMD_USAGE); 2986 2987 if (GETMEMB(addr, "sa_handle", sa_bonus_tab, bonus_tab) || 2988 GETMEMB(addr, "sa_handle", sa_spill_tab, spill_tab) || 2989 GETMEMB(addr, "sa_handle", sa_os, os) || 2990 GETMEMB(addr, "sa_handle", sa_bonus, db_bonus) || 2991 GETMEMB(addr, "sa_handle", sa_spill, db_spill)) { 2992 mdb_printf("Can't find necessary information in sa_handle " 2993 "in sa_handle\n"); 2994 return (DCMD_ERR); 2995 } 2996 2997 if (GETMEMB(os, "objset", os_sa, os_sa)) { 2998 mdb_printf("Can't find os_sa in objset\n"); 2999 return (DCMD_ERR); 3000 } 3001 3002 if (GETMEMB(os_sa, "sa_os", sa_num_attrs, attr_count)) { 3003 mdb_printf("Can't find sa_num_attrs\n"); 3004 return (DCMD_ERR); 3005 } 3006 3007 if (attr_id > attr_count) { 3008 mdb_printf("attribute id number is out of range\n"); 3009 return (DCMD_ERR); 3010 } 3011 3012 if (bonus_tab) { 3013 if (sa_get_off_table(bonus_tab, &offset_tab, 3014 attr_count) == -1) { 3015 return (DCMD_ERR); 3016 } 3017 3018 if (GETMEMB(db_bonus, "dmu_buf", db_data, db_data)) { 3019 mdb_printf("can't find db_data in bonus dbuf\n"); 3020 return (DCMD_ERR); 3021 } 3022 } 3023 3024 if (bonus_tab && !TOC_ATTR_PRESENT(offset_tab[attr_id]) && 3025 spill_tab == NULL) { 3026 mdb_printf("Attribute does not exist\n"); 3027 return (DCMD_ERR); 3028 } else if (!TOC_ATTR_PRESENT(offset_tab[attr_id]) && spill_tab) { 3029 if (sa_get_off_table(spill_tab, &offset_tab, 3030 attr_count) == -1) { 3031 return (DCMD_ERR); 3032 } 3033 if (GETMEMB(db_spill, "dmu_buf", db_data, db_data)) { 3034 mdb_printf("can't find db_data in spill dbuf\n"); 3035 return (DCMD_ERR); 3036 } 3037 if (!TOC_ATTR_PRESENT(offset_tab[attr_id])) { 3038 mdb_printf("Attribute does not exist\n"); 3039 return (DCMD_ERR); 3040 } 3041 } 3042 attr_addr = db_data + TOC_OFF(offset_tab[attr_id]); 3043 mdb_printf("%p\n", attr_addr); 3044 return (DCMD_OK); 3045 } 3046 3047 /* ARGSUSED */ 3048 static int 3049 zfs_ace_print_common(uintptr_t addr, uint_t flags, 3050 uint64_t id, uint32_t access_mask, uint16_t ace_flags, 3051 uint16_t ace_type, int verbose) 3052 { 3053 if (DCMD_HDRSPEC(flags) && !verbose) 3054 mdb_printf("%<u>%-?s %-8s %-8s %-8s %s%</u>\n", 3055 "ADDR", "FLAGS", "MASK", "TYPE", "ID"); 3056 3057 if (!verbose) { 3058 mdb_printf("%0?p %-8x %-8x %-8x %-llx\n", addr, 3059 ace_flags, access_mask, ace_type, id); 3060 return (DCMD_OK); 3061 } 3062 3063 switch (ace_flags & ACE_TYPE_FLAGS) { 3064 case ACE_OWNER: 3065 mdb_printf("owner@:"); 3066 break; 3067 case (ACE_IDENTIFIER_GROUP | ACE_GROUP): 3068 mdb_printf("group@:"); 3069 break; 3070 case ACE_EVERYONE: 3071 mdb_printf("everyone@:"); 3072 break; 3073 case ACE_IDENTIFIER_GROUP: 3074 mdb_printf("group:%llx:", (u_longlong_t)id); 3075 break; 3076 case 0: /* User entry */ 3077 mdb_printf("user:%llx:", (u_longlong_t)id); 3078 break; 3079 } 3080 3081 /* print out permission mask */ 3082 if (access_mask & ACE_READ_DATA) 3083 mdb_printf("r"); 3084 else 3085 mdb_printf("-"); 3086 if (access_mask & ACE_WRITE_DATA) 3087 mdb_printf("w"); 3088 else 3089 mdb_printf("-"); 3090 if (access_mask & ACE_EXECUTE) 3091 mdb_printf("x"); 3092 else 3093 mdb_printf("-"); 3094 if (access_mask & ACE_APPEND_DATA) 3095 mdb_printf("p"); 3096 else 3097 mdb_printf("-"); 3098 if (access_mask & ACE_DELETE) 3099 mdb_printf("d"); 3100 else 3101 mdb_printf("-"); 3102 if (access_mask & ACE_DELETE_CHILD) 3103 mdb_printf("D"); 3104 else 3105 mdb_printf("-"); 3106 if (access_mask & ACE_READ_ATTRIBUTES) 3107 mdb_printf("a"); 3108 else 3109 mdb_printf("-"); 3110 if (access_mask & ACE_WRITE_ATTRIBUTES) 3111 mdb_printf("A"); 3112 else 3113 mdb_printf("-"); 3114 if (access_mask & ACE_READ_NAMED_ATTRS) 3115 mdb_printf("R"); 3116 else 3117 mdb_printf("-"); 3118 if (access_mask & ACE_WRITE_NAMED_ATTRS) 3119 mdb_printf("W"); 3120 else 3121 mdb_printf("-"); 3122 if (access_mask & ACE_READ_ACL) 3123 mdb_printf("c"); 3124 else 3125 mdb_printf("-"); 3126 if (access_mask & ACE_WRITE_ACL) 3127 mdb_printf("C"); 3128 else 3129 mdb_printf("-"); 3130 if (access_mask & ACE_WRITE_OWNER) 3131 mdb_printf("o"); 3132 else 3133 mdb_printf("-"); 3134 if (access_mask & ACE_SYNCHRONIZE) 3135 mdb_printf("s"); 3136 else 3137 mdb_printf("-"); 3138 3139 mdb_printf(":"); 3140 3141 /* Print out inheritance flags */ 3142 if (ace_flags & ACE_FILE_INHERIT_ACE) 3143 mdb_printf("f"); 3144 else 3145 mdb_printf("-"); 3146 if (ace_flags & ACE_DIRECTORY_INHERIT_ACE) 3147 mdb_printf("d"); 3148 else 3149 mdb_printf("-"); 3150 if (ace_flags & ACE_INHERIT_ONLY_ACE) 3151 mdb_printf("i"); 3152 else 3153 mdb_printf("-"); 3154 if (ace_flags & ACE_NO_PROPAGATE_INHERIT_ACE) 3155 mdb_printf("n"); 3156 else 3157 mdb_printf("-"); 3158 if (ace_flags & ACE_SUCCESSFUL_ACCESS_ACE_FLAG) 3159 mdb_printf("S"); 3160 else 3161 mdb_printf("-"); 3162 if (ace_flags & ACE_FAILED_ACCESS_ACE_FLAG) 3163 mdb_printf("F"); 3164 else 3165 mdb_printf("-"); 3166 if (ace_flags & ACE_INHERITED_ACE) 3167 mdb_printf("I"); 3168 else 3169 mdb_printf("-"); 3170 3171 switch (ace_type) { 3172 case ACE_ACCESS_ALLOWED_ACE_TYPE: 3173 mdb_printf(":allow\n"); 3174 break; 3175 case ACE_ACCESS_DENIED_ACE_TYPE: 3176 mdb_printf(":deny\n"); 3177 break; 3178 case ACE_SYSTEM_AUDIT_ACE_TYPE: 3179 mdb_printf(":audit\n"); 3180 break; 3181 case ACE_SYSTEM_ALARM_ACE_TYPE: 3182 mdb_printf(":alarm\n"); 3183 break; 3184 default: 3185 mdb_printf(":?\n"); 3186 } 3187 return (DCMD_OK); 3188 } 3189 3190 /* ARGSUSED */ 3191 static int 3192 zfs_ace_print(uintptr_t addr, uint_t flags, int argc, const mdb_arg_t *argv) 3193 { 3194 zfs_ace_t zace; 3195 int verbose = FALSE; 3196 uint64_t id; 3197 3198 if (!(flags & DCMD_ADDRSPEC)) 3199 return (DCMD_USAGE); 3200 3201 if (mdb_getopts(argc, argv, 3202 'v', MDB_OPT_SETBITS, TRUE, &verbose, TRUE, NULL) != argc) 3203 return (DCMD_USAGE); 3204 3205 if (mdb_vread(&zace, sizeof (zfs_ace_t), addr) == -1) { 3206 mdb_warn("failed to read zfs_ace_t"); 3207 return (DCMD_ERR); 3208 } 3209 3210 if ((zace.z_hdr.z_flags & ACE_TYPE_FLAGS) == 0 || 3211 (zace.z_hdr.z_flags & ACE_TYPE_FLAGS) == ACE_IDENTIFIER_GROUP) 3212 id = zace.z_fuid; 3213 else 3214 id = -1; 3215 3216 return (zfs_ace_print_common(addr, flags, id, zace.z_hdr.z_access_mask, 3217 zace.z_hdr.z_flags, zace.z_hdr.z_type, verbose)); 3218 } 3219 3220 /* ARGSUSED */ 3221 static int 3222 zfs_ace0_print(uintptr_t addr, uint_t flags, int argc, const mdb_arg_t *argv) 3223 { 3224 ace_t ace; 3225 uint64_t id; 3226 int verbose = FALSE; 3227 3228 if (!(flags & DCMD_ADDRSPEC)) 3229 return (DCMD_USAGE); 3230 3231 if (mdb_getopts(argc, argv, 3232 'v', MDB_OPT_SETBITS, TRUE, &verbose, TRUE, NULL) != argc) 3233 return (DCMD_USAGE); 3234 3235 if (mdb_vread(&ace, sizeof (ace_t), addr) == -1) { 3236 mdb_warn("failed to read ace_t"); 3237 return (DCMD_ERR); 3238 } 3239 3240 if ((ace.a_flags & ACE_TYPE_FLAGS) == 0 || 3241 (ace.a_flags & ACE_TYPE_FLAGS) == ACE_IDENTIFIER_GROUP) 3242 id = ace.a_who; 3243 else 3244 id = -1; 3245 3246 return (zfs_ace_print_common(addr, flags, id, ace.a_access_mask, 3247 ace.a_flags, ace.a_type, verbose)); 3248 } 3249 3250 typedef struct acl_dump_args { 3251 int a_argc; 3252 const mdb_arg_t *a_argv; 3253 uint16_t a_version; 3254 int a_flags; 3255 } acl_dump_args_t; 3256 3257 /* ARGSUSED */ 3258 static int 3259 acl_aces_cb(uintptr_t addr, const void *unknown, void *arg) 3260 { 3261 acl_dump_args_t *acl_args = (acl_dump_args_t *)arg; 3262 3263 if (acl_args->a_version == 1) { 3264 if (mdb_call_dcmd("zfs_ace", addr, 3265 DCMD_ADDRSPEC|acl_args->a_flags, acl_args->a_argc, 3266 acl_args->a_argv) != DCMD_OK) { 3267 return (WALK_ERR); 3268 } 3269 } else { 3270 if (mdb_call_dcmd("zfs_ace0", addr, 3271 DCMD_ADDRSPEC|acl_args->a_flags, acl_args->a_argc, 3272 acl_args->a_argv) != DCMD_OK) { 3273 return (WALK_ERR); 3274 } 3275 } 3276 acl_args->a_flags = DCMD_LOOP; 3277 return (WALK_NEXT); 3278 } 3279 3280 /* ARGSUSED */ 3281 static int 3282 acl_cb(uintptr_t addr, const void *unknown, void *arg) 3283 { 3284 acl_dump_args_t *acl_args = (acl_dump_args_t *)arg; 3285 3286 if (acl_args->a_version == 1) { 3287 if (mdb_pwalk("zfs_acl_node_aces", acl_aces_cb, 3288 arg, addr) != 0) { 3289 mdb_warn("can't walk ACEs"); 3290 return (DCMD_ERR); 3291 } 3292 } else { 3293 if (mdb_pwalk("zfs_acl_node_aces0", acl_aces_cb, 3294 arg, addr) != 0) { 3295 mdb_warn("can't walk ACEs"); 3296 return (DCMD_ERR); 3297 } 3298 } 3299 return (WALK_NEXT); 3300 } 3301 3302 /* ARGSUSED */ 3303 static int 3304 zfs_acl_dump(uintptr_t addr, uint_t flags, int argc, const mdb_arg_t *argv) 3305 { 3306 zfs_acl_t zacl; 3307 int verbose = FALSE; 3308 acl_dump_args_t acl_args; 3309 3310 if (!(flags & DCMD_ADDRSPEC)) 3311 return (DCMD_USAGE); 3312 3313 if (mdb_getopts(argc, argv, 3314 'v', MDB_OPT_SETBITS, TRUE, &verbose, TRUE, NULL) != argc) 3315 return (DCMD_USAGE); 3316 3317 if (mdb_vread(&zacl, sizeof (zfs_acl_t), addr) == -1) { 3318 mdb_warn("failed to read zfs_acl_t"); 3319 return (DCMD_ERR); 3320 } 3321 3322 acl_args.a_argc = argc; 3323 acl_args.a_argv = argv; 3324 acl_args.a_version = zacl.z_version; 3325 acl_args.a_flags = DCMD_LOOPFIRST; 3326 3327 if (mdb_pwalk("zfs_acl_node", acl_cb, &acl_args, addr) != 0) { 3328 mdb_warn("can't walk ACL"); 3329 return (DCMD_ERR); 3330 } 3331 3332 return (DCMD_OK); 3333 } 3334 3335 /* ARGSUSED */ 3336 static int 3337 zfs_acl_node_walk_init(mdb_walk_state_t *wsp) 3338 { 3339 if (wsp->walk_addr == NULL) { 3340 mdb_warn("must supply address of zfs_acl_node_t\n"); 3341 return (WALK_ERR); 3342 } 3343 3344 wsp->walk_addr += 3345 mdb_ctf_offsetof_by_name(ZFS_STRUCT "zfs_acl", "z_acl"); 3346 3347 if (mdb_layered_walk("list", wsp) == -1) { 3348 mdb_warn("failed to walk 'list'\n"); 3349 return (WALK_ERR); 3350 } 3351 3352 return (WALK_NEXT); 3353 } 3354 3355 static int 3356 zfs_acl_node_walk_step(mdb_walk_state_t *wsp) 3357 { 3358 zfs_acl_node_t aclnode; 3359 3360 if (mdb_vread(&aclnode, sizeof (zfs_acl_node_t), 3361 wsp->walk_addr) == -1) { 3362 mdb_warn("failed to read zfs_acl_node at %p", wsp->walk_addr); 3363 return (WALK_ERR); 3364 } 3365 3366 return (wsp->walk_callback(wsp->walk_addr, &aclnode, wsp->walk_cbdata)); 3367 } 3368 3369 typedef struct ace_walk_data { 3370 int ace_count; 3371 int ace_version; 3372 } ace_walk_data_t; 3373 3374 static int 3375 zfs_aces_walk_init_common(mdb_walk_state_t *wsp, int version, 3376 int ace_count, uintptr_t ace_data) 3377 { 3378 ace_walk_data_t *ace_walk_data; 3379 3380 if (wsp->walk_addr == NULL) { 3381 mdb_warn("must supply address of zfs_acl_node_t\n"); 3382 return (WALK_ERR); 3383 } 3384 3385 ace_walk_data = mdb_alloc(sizeof (ace_walk_data_t), UM_SLEEP | UM_GC); 3386 3387 ace_walk_data->ace_count = ace_count; 3388 ace_walk_data->ace_version = version; 3389 3390 wsp->walk_addr = ace_data; 3391 wsp->walk_data = ace_walk_data; 3392 3393 return (WALK_NEXT); 3394 } 3395 3396 static int 3397 zfs_acl_node_aces_walk_init_common(mdb_walk_state_t *wsp, int version) 3398 { 3399 static int gotid; 3400 static mdb_ctf_id_t acl_id; 3401 int z_ace_count; 3402 uintptr_t z_acldata; 3403 3404 if (!gotid) { 3405 if (mdb_ctf_lookup_by_name("struct zfs_acl_node", 3406 &acl_id) == -1) { 3407 mdb_warn("couldn't find struct zfs_acl_node"); 3408 return (DCMD_ERR); 3409 } 3410 gotid = TRUE; 3411 } 3412 3413 if (GETMEMBID(wsp->walk_addr, &acl_id, z_ace_count, z_ace_count)) { 3414 return (DCMD_ERR); 3415 } 3416 if (GETMEMBID(wsp->walk_addr, &acl_id, z_acldata, z_acldata)) { 3417 return (DCMD_ERR); 3418 } 3419 3420 return (zfs_aces_walk_init_common(wsp, version, 3421 z_ace_count, z_acldata)); 3422 } 3423 3424 /* ARGSUSED */ 3425 static int 3426 zfs_acl_node_aces_walk_init(mdb_walk_state_t *wsp) 3427 { 3428 return (zfs_acl_node_aces_walk_init_common(wsp, 1)); 3429 } 3430 3431 /* ARGSUSED */ 3432 static int 3433 zfs_acl_node_aces0_walk_init(mdb_walk_state_t *wsp) 3434 { 3435 return (zfs_acl_node_aces_walk_init_common(wsp, 0)); 3436 } 3437 3438 static int 3439 zfs_aces_walk_step(mdb_walk_state_t *wsp) 3440 { 3441 ace_walk_data_t *ace_data = wsp->walk_data; 3442 zfs_ace_t zace; 3443 ace_t *acep; 3444 int status; 3445 int entry_type; 3446 int allow_type; 3447 uintptr_t ptr; 3448 3449 if (ace_data->ace_count == 0) 3450 return (WALK_DONE); 3451 3452 if (mdb_vread(&zace, sizeof (zfs_ace_t), wsp->walk_addr) == -1) { 3453 mdb_warn("failed to read zfs_ace_t at %#lx", 3454 wsp->walk_addr); 3455 return (WALK_ERR); 3456 } 3457 3458 switch (ace_data->ace_version) { 3459 case 0: 3460 acep = (ace_t *)&zace; 3461 entry_type = acep->a_flags & ACE_TYPE_FLAGS; 3462 allow_type = acep->a_type; 3463 break; 3464 case 1: 3465 entry_type = zace.z_hdr.z_flags & ACE_TYPE_FLAGS; 3466 allow_type = zace.z_hdr.z_type; 3467 break; 3468 default: 3469 return (WALK_ERR); 3470 } 3471 3472 ptr = (uintptr_t)wsp->walk_addr; 3473 switch (entry_type) { 3474 case ACE_OWNER: 3475 case ACE_EVERYONE: 3476 case (ACE_IDENTIFIER_GROUP | ACE_GROUP): 3477 ptr += ace_data->ace_version == 0 ? 3478 sizeof (ace_t) : sizeof (zfs_ace_hdr_t); 3479 break; 3480 case ACE_IDENTIFIER_GROUP: 3481 default: 3482 switch (allow_type) { 3483 case ACE_ACCESS_ALLOWED_OBJECT_ACE_TYPE: 3484 case ACE_ACCESS_DENIED_OBJECT_ACE_TYPE: 3485 case ACE_SYSTEM_AUDIT_OBJECT_ACE_TYPE: 3486 case ACE_SYSTEM_ALARM_OBJECT_ACE_TYPE: 3487 ptr += ace_data->ace_version == 0 ? 3488 sizeof (ace_t) : sizeof (zfs_object_ace_t); 3489 break; 3490 default: 3491 ptr += ace_data->ace_version == 0 ? 3492 sizeof (ace_t) : sizeof (zfs_ace_t); 3493 break; 3494 } 3495 } 3496 3497 ace_data->ace_count--; 3498 status = wsp->walk_callback(wsp->walk_addr, 3499 (void *)(uintptr_t)&zace, wsp->walk_cbdata); 3500 3501 wsp->walk_addr = ptr; 3502 return (status); 3503 } 3504 3505 typedef struct mdb_zfs_rrwlock { 3506 uintptr_t rr_writer; 3507 boolean_t rr_writer_wanted; 3508 } mdb_zfs_rrwlock_t; 3509 3510 static uint_t rrw_key; 3511 3512 /* ARGSUSED */ 3513 static int 3514 rrwlock(uintptr_t addr, uint_t flags, int argc, const mdb_arg_t *argv) 3515 { 3516 mdb_zfs_rrwlock_t rrw; 3517 3518 if (rrw_key == 0) { 3519 if (mdb_ctf_readsym(&rrw_key, "uint_t", "rrw_tsd_key", 0) == -1) 3520 return (DCMD_ERR); 3521 } 3522 3523 if (mdb_ctf_vread(&rrw, "rrwlock_t", "mdb_zfs_rrwlock_t", addr, 3524 0) == -1) 3525 return (DCMD_ERR); 3526 3527 if (rrw.rr_writer != 0) { 3528 mdb_printf("write lock held by thread %lx\n", rrw.rr_writer); 3529 return (DCMD_OK); 3530 } 3531 3532 if (rrw.rr_writer_wanted) { 3533 mdb_printf("writer wanted\n"); 3534 } 3535 3536 mdb_printf("anonymous references:\n"); 3537 (void) mdb_call_dcmd("refcount", addr + 3538 mdb_ctf_offsetof_by_name(ZFS_STRUCT "rrwlock", "rr_anon_rcount"), 3539 DCMD_ADDRSPEC, 0, NULL); 3540 3541 mdb_printf("linked references:\n"); 3542 (void) mdb_call_dcmd("refcount", addr + 3543 mdb_ctf_offsetof_by_name(ZFS_STRUCT "rrwlock", "rr_linked_rcount"), 3544 DCMD_ADDRSPEC, 0, NULL); 3545 3546 /* 3547 * XXX This should find references from 3548 * "::walk thread | ::tsd -v <rrw_key>", but there is no support 3549 * for programmatic consumption of dcmds, so this would be 3550 * difficult, potentially requiring reimplementing ::tsd (both 3551 * user and kernel versions) in this MDB module. 3552 */ 3553 3554 return (DCMD_OK); 3555 } 3556 3557 typedef struct mdb_arc_buf_hdr_t { 3558 uint16_t b_psize; 3559 uint16_t b_lsize; 3560 struct { 3561 uint32_t b_bufcnt; 3562 uintptr_t b_state; 3563 } b_l1hdr; 3564 } mdb_arc_buf_hdr_t; 3565 3566 enum arc_cflags { 3567 ARC_CFLAG_VERBOSE = 1 << 0, 3568 ARC_CFLAG_ANON = 1 << 1, 3569 ARC_CFLAG_MRU = 1 << 2, 3570 ARC_CFLAG_MFU = 1 << 3, 3571 ARC_CFLAG_BUFS = 1 << 4, 3572 }; 3573 3574 typedef struct arc_compression_stats_data { 3575 GElf_Sym anon_sym; /* ARC_anon symbol */ 3576 GElf_Sym mru_sym; /* ARC_mru symbol */ 3577 GElf_Sym mrug_sym; /* ARC_mru_ghost symbol */ 3578 GElf_Sym mfu_sym; /* ARC_mfu symbol */ 3579 GElf_Sym mfug_sym; /* ARC_mfu_ghost symbol */ 3580 GElf_Sym l2c_sym; /* ARC_l2c_only symbol */ 3581 uint64_t *anon_c_hist; /* histogram of compressed sizes in anon */ 3582 uint64_t *anon_u_hist; /* histogram of uncompressed sizes in anon */ 3583 uint64_t *anon_bufs; /* histogram of buffer counts in anon state */ 3584 uint64_t *mru_c_hist; /* histogram of compressed sizes in mru */ 3585 uint64_t *mru_u_hist; /* histogram of uncompressed sizes in mru */ 3586 uint64_t *mru_bufs; /* histogram of buffer counts in mru */ 3587 uint64_t *mfu_c_hist; /* histogram of compressed sizes in mfu */ 3588 uint64_t *mfu_u_hist; /* histogram of uncompressed sizes in mfu */ 3589 uint64_t *mfu_bufs; /* histogram of buffer counts in mfu */ 3590 uint64_t *all_c_hist; /* histogram of compressed anon + mru + mfu */ 3591 uint64_t *all_u_hist; /* histogram of uncompressed anon + mru + mfu */ 3592 uint64_t *all_bufs; /* histogram of buffer counts in all states */ 3593 int arc_cflags; /* arc compression flags, specified by user */ 3594 int hist_nbuckets; /* number of buckets in each histogram */ 3595 } arc_compression_stats_data_t; 3596 3597 int 3598 highbit64(uint64_t i) 3599 { 3600 int h = 1; 3601 3602 if (i == 0) 3603 return (0); 3604 if (i & 0xffffffff00000000ULL) { 3605 h += 32; i >>= 32; 3606 } 3607 if (i & 0xffff0000) { 3608 h += 16; i >>= 16; 3609 } 3610 if (i & 0xff00) { 3611 h += 8; i >>= 8; 3612 } 3613 if (i & 0xf0) { 3614 h += 4; i >>= 4; 3615 } 3616 if (i & 0xc) { 3617 h += 2; i >>= 2; 3618 } 3619 if (i & 0x2) { 3620 h += 1; 3621 } 3622 return (h); 3623 } 3624 3625 /* ARGSUSED */ 3626 static int 3627 arc_compression_stats_cb(uintptr_t addr, const void *unknown, void *arg) 3628 { 3629 arc_compression_stats_data_t *data = arg; 3630 mdb_arc_buf_hdr_t hdr; 3631 int cbucket, ubucket, bufcnt; 3632 3633 if (mdb_ctf_vread(&hdr, "arc_buf_hdr_t", "mdb_arc_buf_hdr_t", 3634 addr, 0) == -1) { 3635 return (WALK_ERR); 3636 } 3637 3638 /* 3639 * Headers in the ghost states, or the l2c_only state don't have 3640 * arc buffers linked off of them. Thus, their compressed size 3641 * is meaningless, so we skip these from the stats. 3642 */ 3643 if (hdr.b_l1hdr.b_state == data->mrug_sym.st_value || 3644 hdr.b_l1hdr.b_state == data->mfug_sym.st_value || 3645 hdr.b_l1hdr.b_state == data->l2c_sym.st_value) { 3646 return (WALK_NEXT); 3647 } 3648 3649 /* 3650 * The physical size (compressed) and logical size 3651 * (uncompressed) are in units of SPA_MINBLOCKSIZE. By default, 3652 * we use the log2 of this value (rounded down to the nearest 3653 * integer) to determine the bucket to assign this header to. 3654 * Thus, the histogram is logarithmic with respect to the size 3655 * of the header. For example, the following is a mapping of the 3656 * bucket numbers and the range of header sizes they correspond to: 3657 * 3658 * 0: 0 byte headers 3659 * 1: 512 byte headers 3660 * 2: [1024 - 2048) byte headers 3661 * 3: [2048 - 4096) byte headers 3662 * 4: [4096 - 8192) byte headers 3663 * 5: [8192 - 16394) byte headers 3664 * 6: [16384 - 32768) byte headers 3665 * 7: [32768 - 65536) byte headers 3666 * 8: [65536 - 131072) byte headers 3667 * 9: 131072 byte headers 3668 * 3669 * If the ARC_CFLAG_VERBOSE flag was specified, we use the 3670 * physical and logical sizes directly. Thus, the histogram will 3671 * no longer be logarithmic; instead it will be linear with 3672 * respect to the size of the header. The following is a mapping 3673 * of the first many bucket numbers and the header size they 3674 * correspond to: 3675 * 3676 * 0: 0 byte headers 3677 * 1: 512 byte headers 3678 * 2: 1024 byte headers 3679 * 3: 1536 byte headers 3680 * 4: 2048 byte headers 3681 * 5: 2560 byte headers 3682 * 6: 3072 byte headers 3683 * 3684 * And so on. Keep in mind that a range of sizes isn't used in 3685 * the case of linear scale because the headers can only 3686 * increment or decrement in sizes of 512 bytes. So, it's not 3687 * possible for a header to be sized in between whats listed 3688 * above. 3689 * 3690 * Also, the above mapping values were calculated assuming a 3691 * SPA_MINBLOCKSHIFT of 512 bytes and a SPA_MAXBLOCKSIZE of 128K. 3692 */ 3693 3694 if (data->arc_cflags & ARC_CFLAG_VERBOSE) { 3695 cbucket = hdr.b_psize; 3696 ubucket = hdr.b_lsize; 3697 } else { 3698 cbucket = highbit64(hdr.b_psize); 3699 ubucket = highbit64(hdr.b_lsize); 3700 } 3701 3702 bufcnt = hdr.b_l1hdr.b_bufcnt; 3703 if (bufcnt >= data->hist_nbuckets) 3704 bufcnt = data->hist_nbuckets - 1; 3705 3706 /* Ensure we stay within the bounds of the histogram array */ 3707 ASSERT3U(cbucket, <, data->hist_nbuckets); 3708 ASSERT3U(ubucket, <, data->hist_nbuckets); 3709 3710 if (hdr.b_l1hdr.b_state == data->anon_sym.st_value) { 3711 data->anon_c_hist[cbucket]++; 3712 data->anon_u_hist[ubucket]++; 3713 data->anon_bufs[bufcnt]++; 3714 } else if (hdr.b_l1hdr.b_state == data->mru_sym.st_value) { 3715 data->mru_c_hist[cbucket]++; 3716 data->mru_u_hist[ubucket]++; 3717 data->mru_bufs[bufcnt]++; 3718 } else if (hdr.b_l1hdr.b_state == data->mfu_sym.st_value) { 3719 data->mfu_c_hist[cbucket]++; 3720 data->mfu_u_hist[ubucket]++; 3721 data->mfu_bufs[bufcnt]++; 3722 } 3723 3724 data->all_c_hist[cbucket]++; 3725 data->all_u_hist[ubucket]++; 3726 data->all_bufs[bufcnt]++; 3727 3728 return (WALK_NEXT); 3729 } 3730 3731 /* ARGSUSED */ 3732 static int 3733 arc_compression_stats(uintptr_t addr, uint_t flags, int argc, 3734 const mdb_arg_t *argv) 3735 { 3736 arc_compression_stats_data_t data = { 0 }; 3737 unsigned int max_shifted = SPA_MAXBLOCKSIZE >> SPA_MINBLOCKSHIFT; 3738 unsigned int hist_size; 3739 char range[32]; 3740 int rc = DCMD_OK; 3741 3742 if (mdb_getopts(argc, argv, 3743 'v', MDB_OPT_SETBITS, ARC_CFLAG_VERBOSE, &data.arc_cflags, 3744 'a', MDB_OPT_SETBITS, ARC_CFLAG_ANON, &data.arc_cflags, 3745 'b', MDB_OPT_SETBITS, ARC_CFLAG_BUFS, &data.arc_cflags, 3746 'r', MDB_OPT_SETBITS, ARC_CFLAG_MRU, &data.arc_cflags, 3747 'f', MDB_OPT_SETBITS, ARC_CFLAG_MFU, &data.arc_cflags) != argc) 3748 return (DCMD_USAGE); 3749 3750 if (mdb_lookup_by_obj(ZFS_OBJ_NAME, "ARC_anon", &data.anon_sym) || 3751 mdb_lookup_by_obj(ZFS_OBJ_NAME, "ARC_mru", &data.mru_sym) || 3752 mdb_lookup_by_obj(ZFS_OBJ_NAME, "ARC_mru_ghost", &data.mrug_sym) || 3753 mdb_lookup_by_obj(ZFS_OBJ_NAME, "ARC_mfu", &data.mfu_sym) || 3754 mdb_lookup_by_obj(ZFS_OBJ_NAME, "ARC_mfu_ghost", &data.mfug_sym) || 3755 mdb_lookup_by_obj(ZFS_OBJ_NAME, "ARC_l2c_only", &data.l2c_sym)) { 3756 mdb_warn("can't find arc state symbol"); 3757 return (DCMD_ERR); 3758 } 3759 3760 /* 3761 * Determine the maximum expected size for any header, and use 3762 * this to determine the number of buckets needed for each 3763 * histogram. If ARC_CFLAG_VERBOSE is specified, this value is 3764 * used directly; otherwise the log2 of the maximum size is 3765 * used. Thus, if using a log2 scale there's a maximum of 10 3766 * possible buckets, while the linear scale (when using 3767 * ARC_CFLAG_VERBOSE) has a maximum of 257 buckets. 3768 */ 3769 if (data.arc_cflags & ARC_CFLAG_VERBOSE) 3770 data.hist_nbuckets = max_shifted + 1; 3771 else 3772 data.hist_nbuckets = highbit64(max_shifted) + 1; 3773 3774 hist_size = sizeof (uint64_t) * data.hist_nbuckets; 3775 3776 data.anon_c_hist = mdb_zalloc(hist_size, UM_SLEEP); 3777 data.anon_u_hist = mdb_zalloc(hist_size, UM_SLEEP); 3778 data.anon_bufs = mdb_zalloc(hist_size, UM_SLEEP); 3779 3780 data.mru_c_hist = mdb_zalloc(hist_size, UM_SLEEP); 3781 data.mru_u_hist = mdb_zalloc(hist_size, UM_SLEEP); 3782 data.mru_bufs = mdb_zalloc(hist_size, UM_SLEEP); 3783 3784 data.mfu_c_hist = mdb_zalloc(hist_size, UM_SLEEP); 3785 data.mfu_u_hist = mdb_zalloc(hist_size, UM_SLEEP); 3786 data.mfu_bufs = mdb_zalloc(hist_size, UM_SLEEP); 3787 3788 data.all_c_hist = mdb_zalloc(hist_size, UM_SLEEP); 3789 data.all_u_hist = mdb_zalloc(hist_size, UM_SLEEP); 3790 data.all_bufs = mdb_zalloc(hist_size, UM_SLEEP); 3791 3792 if (mdb_walk("arc_buf_hdr_t_full", arc_compression_stats_cb, 3793 &data) != 0) { 3794 mdb_warn("can't walk arc_buf_hdr's"); 3795 rc = DCMD_ERR; 3796 goto out; 3797 } 3798 3799 if (data.arc_cflags & ARC_CFLAG_VERBOSE) { 3800 rc = mdb_snprintf(range, sizeof (range), 3801 "[n*%llu, (n+1)*%llu)", SPA_MINBLOCKSIZE, 3802 SPA_MINBLOCKSIZE); 3803 } else { 3804 rc = mdb_snprintf(range, sizeof (range), 3805 "[2^(n-1)*%llu, 2^n*%llu)", SPA_MINBLOCKSIZE, 3806 SPA_MINBLOCKSIZE); 3807 } 3808 3809 if (rc < 0) { 3810 /* snprintf failed, abort the dcmd */ 3811 rc = DCMD_ERR; 3812 goto out; 3813 } else { 3814 /* snprintf succeeded above, reset return code */ 3815 rc = DCMD_OK; 3816 } 3817 3818 if (data.arc_cflags & ARC_CFLAG_ANON) { 3819 if (data.arc_cflags & ARC_CFLAG_BUFS) { 3820 mdb_printf("Histogram of the number of anon buffers " 3821 "that are associated with an arc hdr.\n"); 3822 dump_histogram(data.anon_bufs, data.hist_nbuckets, 0); 3823 mdb_printf("\n"); 3824 } 3825 mdb_printf("Histogram of compressed anon buffers.\n" 3826 "Each bucket represents buffers of size: %s.\n", range); 3827 dump_histogram(data.anon_c_hist, data.hist_nbuckets, 0); 3828 mdb_printf("\n"); 3829 3830 mdb_printf("Histogram of uncompressed anon buffers.\n" 3831 "Each bucket represents buffers of size: %s.\n", range); 3832 dump_histogram(data.anon_u_hist, data.hist_nbuckets, 0); 3833 mdb_printf("\n"); 3834 } 3835 3836 if (data.arc_cflags & ARC_CFLAG_MRU) { 3837 if (data.arc_cflags & ARC_CFLAG_BUFS) { 3838 mdb_printf("Histogram of the number of mru buffers " 3839 "that are associated with an arc hdr.\n"); 3840 dump_histogram(data.mru_bufs, data.hist_nbuckets, 0); 3841 mdb_printf("\n"); 3842 } 3843 mdb_printf("Histogram of compressed mru buffers.\n" 3844 "Each bucket represents buffers of size: %s.\n", range); 3845 dump_histogram(data.mru_c_hist, data.hist_nbuckets, 0); 3846 mdb_printf("\n"); 3847 3848 mdb_printf("Histogram of uncompressed mru buffers.\n" 3849 "Each bucket represents buffers of size: %s.\n", range); 3850 dump_histogram(data.mru_u_hist, data.hist_nbuckets, 0); 3851 mdb_printf("\n"); 3852 } 3853 3854 if (data.arc_cflags & ARC_CFLAG_MFU) { 3855 if (data.arc_cflags & ARC_CFLAG_BUFS) { 3856 mdb_printf("Histogram of the number of mfu buffers " 3857 "that are associated with an arc hdr.\n"); 3858 dump_histogram(data.mfu_bufs, data.hist_nbuckets, 0); 3859 mdb_printf("\n"); 3860 } 3861 3862 mdb_printf("Histogram of compressed mfu buffers.\n" 3863 "Each bucket represents buffers of size: %s.\n", range); 3864 dump_histogram(data.mfu_c_hist, data.hist_nbuckets, 0); 3865 mdb_printf("\n"); 3866 3867 mdb_printf("Histogram of uncompressed mfu buffers.\n" 3868 "Each bucket represents buffers of size: %s.\n", range); 3869 dump_histogram(data.mfu_u_hist, data.hist_nbuckets, 0); 3870 mdb_printf("\n"); 3871 } 3872 3873 if (data.arc_cflags & ARC_CFLAG_BUFS) { 3874 mdb_printf("Histogram of all buffers that " 3875 "are associated with an arc hdr.\n"); 3876 dump_histogram(data.all_bufs, data.hist_nbuckets, 0); 3877 mdb_printf("\n"); 3878 } 3879 3880 mdb_printf("Histogram of all compressed buffers.\n" 3881 "Each bucket represents buffers of size: %s.\n", range); 3882 dump_histogram(data.all_c_hist, data.hist_nbuckets, 0); 3883 mdb_printf("\n"); 3884 3885 mdb_printf("Histogram of all uncompressed buffers.\n" 3886 "Each bucket represents buffers of size: %s.\n", range); 3887 dump_histogram(data.all_u_hist, data.hist_nbuckets, 0); 3888 3889 out: 3890 mdb_free(data.anon_c_hist, hist_size); 3891 mdb_free(data.anon_u_hist, hist_size); 3892 mdb_free(data.anon_bufs, hist_size); 3893 3894 mdb_free(data.mru_c_hist, hist_size); 3895 mdb_free(data.mru_u_hist, hist_size); 3896 mdb_free(data.mru_bufs, hist_size); 3897 3898 mdb_free(data.mfu_c_hist, hist_size); 3899 mdb_free(data.mfu_u_hist, hist_size); 3900 mdb_free(data.mfu_bufs, hist_size); 3901 3902 mdb_free(data.all_c_hist, hist_size); 3903 mdb_free(data.all_u_hist, hist_size); 3904 mdb_free(data.all_bufs, hist_size); 3905 3906 return (rc); 3907 } 3908 3909 /* 3910 * MDB module linkage information: 3911 * 3912 * We declare a list of structures describing our dcmds, and a function 3913 * named _mdb_init to return a pointer to our module information. 3914 */ 3915 3916 static const mdb_dcmd_t dcmds[] = { 3917 { "arc", "[-bkmg]", "print ARC variables", arc_print }, 3918 { "blkptr", ":", "print blkptr_t", blkptr }, 3919 { "dbuf", ":", "print dmu_buf_impl_t", dbuf }, 3920 { "dbuf_stats", ":", "dbuf stats", dbuf_stats }, 3921 { "dbufs", 3922 "\t[-O objset_t*] [-n objset_name | \"mos\"] " 3923 "[-o object | \"mdn\"] \n" 3924 "\t[-l level] [-b blkid | \"bonus\"]", 3925 "find dmu_buf_impl_t's that match specified criteria", dbufs }, 3926 { "abuf_find", "dva_word[0] dva_word[1]", 3927 "find arc_buf_hdr_t of a specified DVA", 3928 abuf_find }, 3929 { "spa", "?[-cevmMh]\n" 3930 "\t-c display spa config\n" 3931 "\t-e display vdev statistics\n" 3932 "\t-v display vdev information\n" 3933 "\t-m display metaslab statistics\n" 3934 "\t-M display metaslab group statistics\n" 3935 "\t-h display histogram (requires -m or -M)\n", 3936 "spa_t summary", spa_print }, 3937 { "spa_config", ":", "print spa_t configuration", spa_print_config }, 3938 { "spa_space", ":[-b]", "print spa_t on-disk space usage", spa_space }, 3939 { "spa_vdevs", ":[-emMh]\n" 3940 "\t-e display vdev statistics\n" 3941 "\t-m dispaly metaslab statistics\n" 3942 "\t-M display metaslab group statistic\n" 3943 "\t-h display histogram (requires -m or -M)\n", 3944 "given a spa_t, print vdev summary", spa_vdevs }, 3945 { "vdev", ":[-remMh]\n" 3946 "\t-r display recursively\n" 3947 "\t-e display statistics\n" 3948 "\t-m display metaslab statistics (top level vdev only)\n" 3949 "\t-M display metaslab group statistics (top level vdev only)\n" 3950 "\t-h display histogram (requires -m or -M)\n", 3951 "vdev_t summary", vdev_print }, 3952 { "zio", ":[-cpr]\n" 3953 "\t-c display children\n" 3954 "\t-p display parents\n" 3955 "\t-r display recursively", 3956 "zio_t summary", zio_print }, 3957 { "zio_state", "?", "print out all zio_t structures on system or " 3958 "for a particular pool", zio_state }, 3959 { "zfs_blkstats", ":[-v]", 3960 "given a spa_t, print block type stats from last scrub", 3961 zfs_blkstats }, 3962 { "zfs_params", "", "print zfs tunable parameters", zfs_params }, 3963 { "refcount", ":[-r]\n" 3964 "\t-r display recently removed references", 3965 "print refcount_t holders", refcount }, 3966 { "zap_leaf", "", "print zap_leaf_phys_t", zap_leaf }, 3967 { "zfs_aces", ":[-v]", "print all ACEs from a zfs_acl_t", 3968 zfs_acl_dump }, 3969 { "zfs_ace", ":[-v]", "print zfs_ace", zfs_ace_print }, 3970 { "zfs_ace0", ":[-v]", "print zfs_ace0", zfs_ace0_print }, 3971 { "sa_attr_table", ":", "print SA attribute table from sa_os_t", 3972 sa_attr_table}, 3973 { "sa_attr", ": attr_id", 3974 "print SA attribute address when given sa_handle_t", sa_attr_print}, 3975 { "zfs_dbgmsg", ":[-va]", 3976 "print zfs debug log", dbgmsg}, 3977 { "rrwlock", ":", 3978 "print rrwlock_t, including readers", rrwlock}, 3979 { "metaslab_weight", "weight", 3980 "print metaslab weight", metaslab_weight}, 3981 { "metaslab_trace", ":", 3982 "print metaslab allocation trace records", metaslab_trace}, 3983 { "arc_compression_stats", ":[-vabrf]\n" 3984 "\t-v verbose, display a linearly scaled histogram\n" 3985 "\t-a display ARC_anon state statistics individually\n" 3986 "\t-r display ARC_mru state statistics individually\n" 3987 "\t-f display ARC_mfu state statistics individually\n" 3988 "\t-b display histogram of buffer counts\n", 3989 "print a histogram of compressed arc buffer sizes", 3990 arc_compression_stats}, 3991 { NULL } 3992 }; 3993 3994 static const mdb_walker_t walkers[] = { 3995 { "zms_freelist", "walk ZFS metaslab freelist", 3996 freelist_walk_init, freelist_walk_step, NULL }, 3997 { "txg_list", "given any txg_list_t *, walk all entries in all txgs", 3998 txg_list_walk_init, txg_list_walk_step, NULL }, 3999 { "txg_list0", "given any txg_list_t *, walk all entries in txg 0", 4000 txg_list0_walk_init, txg_list_walk_step, NULL }, 4001 { "txg_list1", "given any txg_list_t *, walk all entries in txg 1", 4002 txg_list1_walk_init, txg_list_walk_step, NULL }, 4003 { "txg_list2", "given any txg_list_t *, walk all entries in txg 2", 4004 txg_list2_walk_init, txg_list_walk_step, NULL }, 4005 { "txg_list3", "given any txg_list_t *, walk all entries in txg 3", 4006 txg_list3_walk_init, txg_list_walk_step, NULL }, 4007 { "zio", "walk all zio structures, optionally for a particular spa_t", 4008 zio_walk_init, zio_walk_step, NULL }, 4009 { "zio_root", 4010 "walk all root zio_t structures, optionally for a particular spa_t", 4011 zio_walk_init, zio_walk_root_step, NULL }, 4012 { "spa", "walk all spa_t entries in the namespace", 4013 spa_walk_init, spa_walk_step, NULL }, 4014 { "metaslab", "given a spa_t *, walk all metaslab_t structures", 4015 metaslab_walk_init, metaslab_walk_step, NULL }, 4016 { "multilist", "given a multilist_t *, walk all list_t structures", 4017 multilist_walk_init, multilist_walk_step, NULL }, 4018 { "zfs_acl_node", "given a zfs_acl_t, walk all zfs_acl_nodes", 4019 zfs_acl_node_walk_init, zfs_acl_node_walk_step, NULL }, 4020 { "zfs_acl_node_aces", "given a zfs_acl_node_t, walk all ACEs", 4021 zfs_acl_node_aces_walk_init, zfs_aces_walk_step, NULL }, 4022 { "zfs_acl_node_aces0", 4023 "given a zfs_acl_node_t, walk all ACEs as ace_t", 4024 zfs_acl_node_aces0_walk_init, zfs_aces_walk_step, NULL }, 4025 { NULL } 4026 }; 4027 4028 static const mdb_modinfo_t modinfo = { 4029 MDB_API_VERSION, dcmds, walkers 4030 }; 4031 4032 const mdb_modinfo_t * 4033 _mdb_init(void) 4034 { 4035 return (&modinfo); 4036 } 4037