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