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 default: 1573 aux = "UNKNOWN"; 1574 break; 1575 } 1576 1577 mdb_printf("%-9s %-12s %*s%s\n", state, aux, depth, "", desc); 1578 1579 if (spa_flags & SPA_FLAG_ERRORS) { 1580 vdev_stat_t *vs = &vdev.vdev_stat; 1581 int i; 1582 1583 mdb_inc_indent(4); 1584 mdb_printf("\n"); 1585 mdb_printf("%<u> %12s %12s %12s %12s " 1586 "%12s%</u>\n", "READ", "WRITE", "FREE", "CLAIM", 1587 "IOCTL"); 1588 mdb_printf("OPS "); 1589 for (i = 1; i < ZIO_TYPES; i++) 1590 mdb_printf("%11#llx%s", vs->vs_ops[i], 1591 i == ZIO_TYPES - 1 ? "" : " "); 1592 mdb_printf("\n"); 1593 mdb_printf("BYTES "); 1594 for (i = 1; i < ZIO_TYPES; i++) 1595 mdb_printf("%11#llx%s", vs->vs_bytes[i], 1596 i == ZIO_TYPES - 1 ? "" : " "); 1597 1598 1599 mdb_printf("\n"); 1600 mdb_printf("EREAD %10#llx\n", vs->vs_read_errors); 1601 mdb_printf("EWRITE %10#llx\n", vs->vs_write_errors); 1602 mdb_printf("ECKSUM %10#llx\n", 1603 vs->vs_checksum_errors); 1604 mdb_dec_indent(4); 1605 mdb_printf("\n"); 1606 } 1607 1608 if (spa_flags & SPA_FLAG_METASLAB_GROUPS && 1609 vdev.vdev_mg != NULL) { 1610 metaslab_group_stats((uintptr_t)vdev.vdev_mg, 1611 spa_flags); 1612 } 1613 if (spa_flags & SPA_FLAG_METASLABS && vdev.vdev_ms != NULL) { 1614 metaslab_stats((uintptr_t)addr, spa_flags); 1615 } 1616 } 1617 1618 children = vdev.vdev_children; 1619 1620 if (children == 0 || !recursive) 1621 return (DCMD_OK); 1622 1623 child = mdb_alloc(children * sizeof (void *), UM_SLEEP | UM_GC); 1624 if (mdb_vread(child, children * sizeof (void *), 1625 (uintptr_t)vdev.vdev_child) == -1) { 1626 mdb_warn("failed to read vdev children at %p", vdev.vdev_child); 1627 return (DCMD_ERR); 1628 } 1629 1630 for (c = 0; c < children; c++) { 1631 if (do_print_vdev(child[c], flags, depth + 2, recursive, 1632 spa_flags)) { 1633 return (DCMD_ERR); 1634 } 1635 } 1636 1637 return (DCMD_OK); 1638 } 1639 1640 static int 1641 vdev_print(uintptr_t addr, uint_t flags, int argc, const mdb_arg_t *argv) 1642 { 1643 uint64_t depth = 0; 1644 boolean_t recursive = B_FALSE; 1645 int spa_flags = 0; 1646 1647 if (mdb_getopts(argc, argv, 1648 'e', MDB_OPT_SETBITS, SPA_FLAG_ERRORS, &spa_flags, 1649 'm', MDB_OPT_SETBITS, SPA_FLAG_METASLABS, &spa_flags, 1650 'M', MDB_OPT_SETBITS, SPA_FLAG_METASLAB_GROUPS, &spa_flags, 1651 'h', MDB_OPT_SETBITS, SPA_FLAG_HISTOGRAMS, &spa_flags, 1652 'r', MDB_OPT_SETBITS, TRUE, &recursive, 1653 'd', MDB_OPT_UINT64, &depth, NULL) != argc) 1654 return (DCMD_USAGE); 1655 1656 if (!(flags & DCMD_ADDRSPEC)) { 1657 mdb_warn("no vdev_t address given\n"); 1658 return (DCMD_ERR); 1659 } 1660 1661 return (do_print_vdev(addr, flags, (int)depth, recursive, spa_flags)); 1662 } 1663 1664 typedef struct mdb_metaslab_alloc_trace { 1665 uintptr_t mat_mg; 1666 uintptr_t mat_msp; 1667 uint64_t mat_size; 1668 uint64_t mat_weight; 1669 uint64_t mat_offset; 1670 uint32_t mat_dva_id; 1671 } mdb_metaslab_alloc_trace_t; 1672 1673 static void 1674 metaslab_print_weight(uint64_t weight) 1675 { 1676 char buf[100]; 1677 1678 if (WEIGHT_IS_SPACEBASED(weight)) { 1679 mdb_nicenum( 1680 weight & ~(METASLAB_ACTIVE_MASK | METASLAB_WEIGHT_TYPE), 1681 buf); 1682 } else { 1683 char size[NICENUM_BUFLEN]; 1684 mdb_nicenum(1ULL << WEIGHT_GET_INDEX(weight), size); 1685 (void) mdb_snprintf(buf, sizeof (buf), "%llu x %s", 1686 WEIGHT_GET_COUNT(weight), size); 1687 } 1688 mdb_printf("%11s ", buf); 1689 } 1690 1691 /* ARGSUSED */ 1692 static int 1693 metaslab_weight(uintptr_t addr, uint_t flags, int argc, const mdb_arg_t *argv) 1694 { 1695 uint64_t weight = 0; 1696 char active; 1697 1698 if (argc == 0 && (flags & DCMD_ADDRSPEC)) { 1699 if (mdb_vread(&weight, sizeof (uint64_t), addr) == -1) { 1700 mdb_warn("failed to read weight at %p\n", addr); 1701 return (DCMD_ERR); 1702 } 1703 } else if (argc == 1 && !(flags & DCMD_ADDRSPEC)) { 1704 weight = (argv[0].a_type == MDB_TYPE_IMMEDIATE) ? 1705 argv[0].a_un.a_val : mdb_strtoull(argv[0].a_un.a_str); 1706 } else { 1707 return (DCMD_USAGE); 1708 } 1709 1710 if (DCMD_HDRSPEC(flags)) { 1711 mdb_printf("%<u>%-6s %9s %9s%</u>\n", 1712 "ACTIVE", "ALGORITHM", "WEIGHT"); 1713 } 1714 1715 if (weight & METASLAB_WEIGHT_PRIMARY) 1716 active = 'P'; 1717 else if (weight & METASLAB_WEIGHT_SECONDARY) 1718 active = 'S'; 1719 else 1720 active = '-'; 1721 mdb_printf("%6c %8s ", active, 1722 WEIGHT_IS_SPACEBASED(weight) ? "SPACE" : "SEGMENT"); 1723 metaslab_print_weight(weight); 1724 mdb_printf("\n"); 1725 1726 return (DCMD_OK); 1727 } 1728 1729 /* ARGSUSED */ 1730 static int 1731 metaslab_trace(uintptr_t addr, uint_t flags, int argc, const mdb_arg_t *argv) 1732 { 1733 mdb_metaslab_alloc_trace_t mat; 1734 mdb_metaslab_group_t mg = { 0 }; 1735 char result_type[100]; 1736 1737 if (mdb_ctf_vread(&mat, "metaslab_alloc_trace_t", 1738 "mdb_metaslab_alloc_trace_t", addr, 0) == -1) { 1739 return (DCMD_ERR); 1740 } 1741 1742 if (!(flags & DCMD_PIPE_OUT) && DCMD_HDRSPEC(flags)) { 1743 mdb_printf("%<u>%6s %6s %8s %11s %18s %18s%</u>\n", 1744 "MSID", "DVA", "ASIZE", "WEIGHT", "RESULT", "VDEV"); 1745 } 1746 1747 if (mat.mat_msp != NULL) { 1748 mdb_metaslab_t ms; 1749 1750 if (mdb_ctf_vread(&ms, "metaslab_t", "mdb_metaslab_t", 1751 mat.mat_msp, 0) == -1) { 1752 return (DCMD_ERR); 1753 } 1754 mdb_printf("%6llu ", ms.ms_id); 1755 } else { 1756 mdb_printf("%6s ", "-"); 1757 } 1758 1759 mdb_printf("%6d %8llx ", mat.mat_dva_id, mat.mat_size); 1760 1761 metaslab_print_weight(mat.mat_weight); 1762 1763 if ((int64_t)mat.mat_offset < 0) { 1764 if (enum_lookup("enum trace_alloc_type", mat.mat_offset, 1765 "TRACE_", sizeof (result_type), result_type) == -1) { 1766 mdb_warn("Could not find enum for trace_alloc_type"); 1767 return (DCMD_ERR); 1768 } 1769 mdb_printf("%18s ", result_type); 1770 } else { 1771 mdb_printf("%<b>%18llx%</b> ", mat.mat_offset); 1772 } 1773 1774 if (mat.mat_mg != NULL && 1775 mdb_ctf_vread(&mg, "metaslab_group_t", "mdb_metaslab_group_t", 1776 mat.mat_mg, 0) == -1) { 1777 return (DCMD_ERR); 1778 } 1779 1780 if (mg.mg_vd != NULL) { 1781 mdb_vdev_t vdev; 1782 char desc[MAXNAMELEN]; 1783 1784 if (mdb_ctf_vread(&vdev, "vdev_t", "mdb_vdev_t", 1785 mg.mg_vd, 0) == -1) { 1786 return (DCMD_ERR); 1787 } 1788 1789 if (vdev.vdev_path != NULL) { 1790 char path[MAXNAMELEN]; 1791 1792 if (mdb_readstr(path, sizeof (path), 1793 vdev.vdev_path) == -1) { 1794 mdb_warn("failed to read vdev_path at %p\n", 1795 vdev.vdev_path); 1796 return (DCMD_ERR); 1797 } 1798 char *slash; 1799 if ((slash = strrchr(path, '/')) != NULL) { 1800 strcpy(desc, slash + 1); 1801 } else { 1802 strcpy(desc, path); 1803 } 1804 } else if (vdev.vdev_ops != NULL) { 1805 mdb_vdev_ops_t ops; 1806 if (mdb_ctf_vread(&ops, "vdev_ops_t", "mdb_vdev_ops_t", 1807 vdev.vdev_ops, 0) == -1) { 1808 mdb_warn("failed to read vdev_ops at %p\n", 1809 vdev.vdev_ops); 1810 return (DCMD_ERR); 1811 } 1812 (void) mdb_snprintf(desc, sizeof (desc), 1813 "%s-%llu", ops.vdev_op_type, vdev.vdev_id); 1814 } else { 1815 (void) strcpy(desc, "<unknown>"); 1816 } 1817 mdb_printf("%18s\n", desc); 1818 } 1819 1820 return (DCMD_OK); 1821 } 1822 1823 typedef struct metaslab_walk_data { 1824 uint64_t mw_numvdevs; 1825 uintptr_t *mw_vdevs; 1826 int mw_curvdev; 1827 uint64_t mw_nummss; 1828 uintptr_t *mw_mss; 1829 int mw_curms; 1830 } metaslab_walk_data_t; 1831 1832 static int 1833 metaslab_walk_step(mdb_walk_state_t *wsp) 1834 { 1835 metaslab_walk_data_t *mw = wsp->walk_data; 1836 metaslab_t ms; 1837 uintptr_t msp; 1838 1839 if (mw->mw_curvdev >= mw->mw_numvdevs) 1840 return (WALK_DONE); 1841 1842 if (mw->mw_mss == NULL) { 1843 uintptr_t mssp; 1844 uintptr_t vdevp; 1845 1846 ASSERT(mw->mw_curms == 0); 1847 ASSERT(mw->mw_nummss == 0); 1848 1849 vdevp = mw->mw_vdevs[mw->mw_curvdev]; 1850 if (GETMEMB(vdevp, "vdev", vdev_ms, mssp) || 1851 GETMEMB(vdevp, "vdev", vdev_ms_count, mw->mw_nummss)) { 1852 return (WALK_ERR); 1853 } 1854 1855 mw->mw_mss = mdb_alloc(mw->mw_nummss * sizeof (void*), 1856 UM_SLEEP | UM_GC); 1857 if (mdb_vread(mw->mw_mss, mw->mw_nummss * sizeof (void*), 1858 mssp) == -1) { 1859 mdb_warn("failed to read vdev_ms at %p", mssp); 1860 return (WALK_ERR); 1861 } 1862 } 1863 1864 if (mw->mw_curms >= mw->mw_nummss) { 1865 mw->mw_mss = NULL; 1866 mw->mw_curms = 0; 1867 mw->mw_nummss = 0; 1868 mw->mw_curvdev++; 1869 return (WALK_NEXT); 1870 } 1871 1872 msp = mw->mw_mss[mw->mw_curms]; 1873 if (mdb_vread(&ms, sizeof (metaslab_t), msp) == -1) { 1874 mdb_warn("failed to read metaslab_t at %p", msp); 1875 return (WALK_ERR); 1876 } 1877 1878 mw->mw_curms++; 1879 1880 return (wsp->walk_callback(msp, &ms, wsp->walk_cbdata)); 1881 } 1882 1883 static int 1884 metaslab_walk_init(mdb_walk_state_t *wsp) 1885 { 1886 metaslab_walk_data_t *mw; 1887 uintptr_t root_vdevp; 1888 uintptr_t childp; 1889 1890 if (wsp->walk_addr == NULL) { 1891 mdb_warn("must supply address of spa_t\n"); 1892 return (WALK_ERR); 1893 } 1894 1895 mw = mdb_zalloc(sizeof (metaslab_walk_data_t), UM_SLEEP | UM_GC); 1896 1897 if (GETMEMB(wsp->walk_addr, "spa", spa_root_vdev, root_vdevp) || 1898 GETMEMB(root_vdevp, "vdev", vdev_children, mw->mw_numvdevs) || 1899 GETMEMB(root_vdevp, "vdev", vdev_child, childp)) { 1900 return (DCMD_ERR); 1901 } 1902 1903 mw->mw_vdevs = mdb_alloc(mw->mw_numvdevs * sizeof (void *), 1904 UM_SLEEP | UM_GC); 1905 if (mdb_vread(mw->mw_vdevs, mw->mw_numvdevs * sizeof (void *), 1906 childp) == -1) { 1907 mdb_warn("failed to read root vdev children at %p", childp); 1908 return (DCMD_ERR); 1909 } 1910 1911 wsp->walk_data = mw; 1912 1913 return (WALK_NEXT); 1914 } 1915 1916 typedef struct mdb_spa { 1917 uintptr_t spa_dsl_pool; 1918 uintptr_t spa_root_vdev; 1919 } mdb_spa_t; 1920 1921 typedef struct mdb_dsl_pool { 1922 uintptr_t dp_root_dir; 1923 } mdb_dsl_pool_t; 1924 1925 typedef struct mdb_dsl_dir { 1926 uintptr_t dd_dbuf; 1927 int64_t dd_space_towrite[TXG_SIZE]; 1928 } mdb_dsl_dir_t; 1929 1930 typedef struct mdb_dsl_dir_phys { 1931 uint64_t dd_used_bytes; 1932 uint64_t dd_compressed_bytes; 1933 uint64_t dd_uncompressed_bytes; 1934 } mdb_dsl_dir_phys_t; 1935 1936 typedef struct space_data { 1937 uint64_t ms_alloctree[TXG_SIZE]; 1938 uint64_t ms_freeingtree; 1939 uint64_t ms_freedtree; 1940 uint64_t ms_tree; 1941 int64_t ms_deferspace; 1942 uint64_t avail; 1943 uint64_t nowavail; 1944 } space_data_t; 1945 1946 /* ARGSUSED */ 1947 static int 1948 space_cb(uintptr_t addr, const void *unknown, void *arg) 1949 { 1950 space_data_t *sd = arg; 1951 mdb_metaslab_t ms; 1952 mdb_range_tree_t rt; 1953 mdb_space_map_t sm = { 0 }; 1954 mdb_space_map_phys_t smp = { 0 }; 1955 int i; 1956 1957 if (mdb_ctf_vread(&ms, "metaslab_t", "mdb_metaslab_t", 1958 addr, 0) == -1) 1959 return (WALK_ERR); 1960 1961 for (i = 0; i < TXG_SIZE; i++) { 1962 if (mdb_ctf_vread(&rt, "range_tree_t", 1963 "mdb_range_tree_t", ms.ms_alloctree[i], 0) == -1) 1964 return (WALK_ERR); 1965 1966 sd->ms_alloctree[i] += rt.rt_space; 1967 1968 } 1969 1970 if (mdb_ctf_vread(&rt, "range_tree_t", 1971 "mdb_range_tree_t", ms.ms_freeingtree, 0) == -1) 1972 return (WALK_ERR); 1973 sd->ms_freeingtree += rt.rt_space; 1974 1975 if (mdb_ctf_vread(&rt, "range_tree_t", 1976 "mdb_range_tree_t", ms.ms_freedtree, 0) == -1) 1977 return (WALK_ERR); 1978 sd->ms_freedtree += rt.rt_space; 1979 1980 if (mdb_ctf_vread(&rt, "range_tree_t", 1981 "mdb_range_tree_t", ms.ms_tree, 0) == -1) 1982 return (WALK_ERR); 1983 sd->ms_tree += rt.rt_space; 1984 1985 if (ms.ms_sm != NULL && 1986 mdb_ctf_vread(&sm, "space_map_t", 1987 "mdb_space_map_t", ms.ms_sm, 0) == -1) 1988 return (WALK_ERR); 1989 1990 if (sm.sm_phys != NULL) { 1991 (void) mdb_ctf_vread(&smp, "space_map_phys_t", 1992 "mdb_space_map_phys_t", sm.sm_phys, 0); 1993 } 1994 1995 sd->ms_deferspace += ms.ms_deferspace; 1996 sd->avail += sm.sm_size - sm.sm_alloc; 1997 sd->nowavail += sm.sm_size - smp.smp_alloc; 1998 1999 return (WALK_NEXT); 2000 } 2001 2002 /* 2003 * ::spa_space [-b] 2004 * 2005 * Given a spa_t, print out it's on-disk space usage and in-core 2006 * estimates of future usage. If -b is given, print space in bytes. 2007 * Otherwise print in megabytes. 2008 */ 2009 /* ARGSUSED */ 2010 static int 2011 spa_space(uintptr_t addr, uint_t flags, int argc, const mdb_arg_t *argv) 2012 { 2013 mdb_spa_t spa; 2014 mdb_dsl_pool_t dp; 2015 mdb_dsl_dir_t dd; 2016 mdb_dmu_buf_impl_t db; 2017 mdb_dsl_dir_phys_t dsp; 2018 space_data_t sd; 2019 int shift = 20; 2020 char *suffix = "M"; 2021 int bytes = B_FALSE; 2022 2023 if (mdb_getopts(argc, argv, 'b', MDB_OPT_SETBITS, TRUE, &bytes, NULL) != 2024 argc) 2025 return (DCMD_USAGE); 2026 if (!(flags & DCMD_ADDRSPEC)) 2027 return (DCMD_USAGE); 2028 2029 if (bytes) { 2030 shift = 0; 2031 suffix = ""; 2032 } 2033 2034 if (mdb_ctf_vread(&spa, ZFS_STRUCT "spa", "mdb_spa_t", 2035 addr, 0) == -1 || 2036 mdb_ctf_vread(&dp, ZFS_STRUCT "dsl_pool", "mdb_dsl_pool_t", 2037 spa.spa_dsl_pool, 0) == -1 || 2038 mdb_ctf_vread(&dd, ZFS_STRUCT "dsl_dir", "mdb_dsl_dir_t", 2039 dp.dp_root_dir, 0) == -1 || 2040 mdb_ctf_vread(&db, ZFS_STRUCT "dmu_buf_impl", "mdb_dmu_buf_impl_t", 2041 dd.dd_dbuf, 0) == -1 || 2042 mdb_ctf_vread(&dsp, ZFS_STRUCT "dsl_dir_phys", 2043 "mdb_dsl_dir_phys_t", db.db.db_data, 0) == -1) { 2044 return (DCMD_ERR); 2045 } 2046 2047 mdb_printf("dd_space_towrite = %llu%s %llu%s %llu%s %llu%s\n", 2048 dd.dd_space_towrite[0] >> shift, suffix, 2049 dd.dd_space_towrite[1] >> shift, suffix, 2050 dd.dd_space_towrite[2] >> shift, suffix, 2051 dd.dd_space_towrite[3] >> shift, suffix); 2052 2053 mdb_printf("dd_phys.dd_used_bytes = %llu%s\n", 2054 dsp.dd_used_bytes >> shift, suffix); 2055 mdb_printf("dd_phys.dd_compressed_bytes = %llu%s\n", 2056 dsp.dd_compressed_bytes >> shift, suffix); 2057 mdb_printf("dd_phys.dd_uncompressed_bytes = %llu%s\n", 2058 dsp.dd_uncompressed_bytes >> shift, suffix); 2059 2060 bzero(&sd, sizeof (sd)); 2061 if (mdb_pwalk("metaslab", space_cb, &sd, addr) != 0) { 2062 mdb_warn("can't walk metaslabs"); 2063 return (DCMD_ERR); 2064 } 2065 2066 mdb_printf("ms_allocmap = %llu%s %llu%s %llu%s %llu%s\n", 2067 sd.ms_alloctree[0] >> shift, suffix, 2068 sd.ms_alloctree[1] >> shift, suffix, 2069 sd.ms_alloctree[2] >> shift, suffix, 2070 sd.ms_alloctree[3] >> shift, suffix); 2071 mdb_printf("ms_freeingtree = %llu%s\n", 2072 sd.ms_freeingtree >> shift, suffix); 2073 mdb_printf("ms_freedtree = %llu%s\n", 2074 sd.ms_freedtree >> shift, suffix); 2075 mdb_printf("ms_tree = %llu%s\n", 2076 sd.ms_tree >> shift, suffix); 2077 mdb_printf("ms_deferspace = %llu%s\n", 2078 sd.ms_deferspace >> shift, suffix); 2079 mdb_printf("last synced avail = %llu%s\n", 2080 sd.avail >> shift, suffix); 2081 mdb_printf("current syncing avail = %llu%s\n", 2082 sd.nowavail >> shift, suffix); 2083 2084 return (DCMD_OK); 2085 } 2086 2087 typedef struct mdb_spa_aux_vdev { 2088 int sav_count; 2089 uintptr_t sav_vdevs; 2090 } mdb_spa_aux_vdev_t; 2091 2092 typedef struct mdb_spa_vdevs { 2093 uintptr_t spa_root_vdev; 2094 mdb_spa_aux_vdev_t spa_l2cache; 2095 mdb_spa_aux_vdev_t spa_spares; 2096 } mdb_spa_vdevs_t; 2097 2098 static int 2099 spa_print_aux(mdb_spa_aux_vdev_t *sav, uint_t flags, mdb_arg_t *v, 2100 const char *name) 2101 { 2102 uintptr_t *aux; 2103 size_t len; 2104 int ret, i; 2105 2106 /* 2107 * Iterate over aux vdevs and print those out as well. This is a 2108 * little annoying because we don't have a root vdev to pass to ::vdev. 2109 * Instead, we print a single line and then call it for each child 2110 * vdev. 2111 */ 2112 if (sav->sav_count != 0) { 2113 v[1].a_type = MDB_TYPE_STRING; 2114 v[1].a_un.a_str = "-d"; 2115 v[2].a_type = MDB_TYPE_IMMEDIATE; 2116 v[2].a_un.a_val = 2; 2117 2118 len = sav->sav_count * sizeof (uintptr_t); 2119 aux = mdb_alloc(len, UM_SLEEP); 2120 if (mdb_vread(aux, len, sav->sav_vdevs) == -1) { 2121 mdb_free(aux, len); 2122 mdb_warn("failed to read l2cache vdevs at %p", 2123 sav->sav_vdevs); 2124 return (DCMD_ERR); 2125 } 2126 2127 mdb_printf("%-?s %-9s %-12s %s\n", "-", "-", "-", name); 2128 2129 for (i = 0; i < sav->sav_count; i++) { 2130 ret = mdb_call_dcmd("vdev", aux[i], flags, 3, v); 2131 if (ret != DCMD_OK) { 2132 mdb_free(aux, len); 2133 return (ret); 2134 } 2135 } 2136 2137 mdb_free(aux, len); 2138 } 2139 2140 return (0); 2141 } 2142 2143 /* 2144 * ::spa_vdevs 2145 * 2146 * -e Include error stats 2147 * -m Include metaslab information 2148 * -M Include metaslab group information 2149 * -h Include histogram information (requires -m or -M) 2150 * 2151 * Print out a summarized list of vdevs for the given spa_t. 2152 * This is accomplished by invoking "::vdev -re" on the root vdev, as well as 2153 * iterating over the cache devices. 2154 */ 2155 /* ARGSUSED */ 2156 static int 2157 spa_vdevs(uintptr_t addr, uint_t flags, int argc, const mdb_arg_t *argv) 2158 { 2159 mdb_arg_t v[3]; 2160 int ret; 2161 char opts[100] = "-r"; 2162 int spa_flags = 0; 2163 2164 if (mdb_getopts(argc, argv, 2165 'e', MDB_OPT_SETBITS, SPA_FLAG_ERRORS, &spa_flags, 2166 'm', MDB_OPT_SETBITS, SPA_FLAG_METASLABS, &spa_flags, 2167 'M', MDB_OPT_SETBITS, SPA_FLAG_METASLAB_GROUPS, &spa_flags, 2168 'h', MDB_OPT_SETBITS, SPA_FLAG_HISTOGRAMS, &spa_flags, 2169 NULL) != argc) 2170 return (DCMD_USAGE); 2171 2172 if (!(flags & DCMD_ADDRSPEC)) 2173 return (DCMD_USAGE); 2174 2175 mdb_spa_vdevs_t spa; 2176 if (mdb_ctf_vread(&spa, "spa_t", "mdb_spa_vdevs_t", addr, 0) == -1) 2177 return (DCMD_ERR); 2178 2179 /* 2180 * Unitialized spa_t structures can have a NULL root vdev. 2181 */ 2182 if (spa.spa_root_vdev == NULL) { 2183 mdb_printf("no associated vdevs\n"); 2184 return (DCMD_OK); 2185 } 2186 2187 if (spa_flags & SPA_FLAG_ERRORS) 2188 strcat(opts, "e"); 2189 if (spa_flags & SPA_FLAG_METASLABS) 2190 strcat(opts, "m"); 2191 if (spa_flags & SPA_FLAG_METASLAB_GROUPS) 2192 strcat(opts, "M"); 2193 if (spa_flags & SPA_FLAG_HISTOGRAMS) 2194 strcat(opts, "h"); 2195 2196 v[0].a_type = MDB_TYPE_STRING; 2197 v[0].a_un.a_str = opts; 2198 2199 ret = mdb_call_dcmd("vdev", (uintptr_t)spa.spa_root_vdev, 2200 flags, 1, v); 2201 if (ret != DCMD_OK) 2202 return (ret); 2203 2204 if (spa_print_aux(&spa.spa_l2cache, flags, v, "cache") != 0 || 2205 spa_print_aux(&spa.spa_spares, flags, v, "spares") != 0) 2206 return (DCMD_ERR); 2207 2208 return (DCMD_OK); 2209 } 2210 2211 /* 2212 * ::zio 2213 * 2214 * Print a summary of zio_t and all its children. This is intended to display a 2215 * zio tree, and hence we only pick the most important pieces of information for 2216 * the main summary. More detailed information can always be found by doing a 2217 * '::print zio' on the underlying zio_t. The columns we display are: 2218 * 2219 * ADDRESS TYPE STAGE WAITER TIME_ELAPSED 2220 * 2221 * The 'address' column is indented by one space for each depth level as we 2222 * descend down the tree. 2223 */ 2224 2225 #define ZIO_MAXINDENT 7 2226 #define ZIO_MAXWIDTH (sizeof (uintptr_t) * 2 + ZIO_MAXINDENT) 2227 #define ZIO_WALK_SELF 0 2228 #define ZIO_WALK_CHILD 1 2229 #define ZIO_WALK_PARENT 2 2230 2231 typedef struct zio_print_args { 2232 int zpa_current_depth; 2233 int zpa_min_depth; 2234 int zpa_max_depth; 2235 int zpa_type; 2236 uint_t zpa_flags; 2237 } zio_print_args_t; 2238 2239 typedef struct mdb_zio { 2240 enum zio_type io_type; 2241 enum zio_stage io_stage; 2242 uintptr_t io_waiter; 2243 uintptr_t io_spa; 2244 struct { 2245 struct { 2246 uintptr_t list_next; 2247 } list_head; 2248 } io_parent_list; 2249 int io_error; 2250 } mdb_zio_t; 2251 2252 typedef struct mdb_zio_timestamp { 2253 hrtime_t io_timestamp; 2254 } mdb_zio_timestamp_t; 2255 2256 static int zio_child_cb(uintptr_t addr, const void *unknown, void *arg); 2257 2258 static int 2259 zio_print_cb(uintptr_t addr, zio_print_args_t *zpa) 2260 { 2261 mdb_ctf_id_t type_enum, stage_enum; 2262 int indent = zpa->zpa_current_depth; 2263 const char *type, *stage; 2264 uintptr_t laddr; 2265 mdb_zio_t zio; 2266 mdb_zio_timestamp_t zio_timestamp = { 0 }; 2267 2268 if (mdb_ctf_vread(&zio, ZFS_STRUCT "zio", "mdb_zio_t", addr, 0) == -1) 2269 return (WALK_ERR); 2270 (void) mdb_ctf_vread(&zio_timestamp, ZFS_STRUCT "zio", 2271 "mdb_zio_timestamp_t", addr, MDB_CTF_VREAD_QUIET); 2272 2273 if (indent > ZIO_MAXINDENT) 2274 indent = ZIO_MAXINDENT; 2275 2276 if (mdb_ctf_lookup_by_name("enum zio_type", &type_enum) == -1 || 2277 mdb_ctf_lookup_by_name("enum zio_stage", &stage_enum) == -1) { 2278 mdb_warn("failed to lookup zio enums"); 2279 return (WALK_ERR); 2280 } 2281 2282 if ((type = mdb_ctf_enum_name(type_enum, zio.io_type)) != NULL) 2283 type += sizeof ("ZIO_TYPE_") - 1; 2284 else 2285 type = "?"; 2286 2287 if (zio.io_error == 0) { 2288 stage = mdb_ctf_enum_name(stage_enum, zio.io_stage); 2289 if (stage != NULL) 2290 stage += sizeof ("ZIO_STAGE_") - 1; 2291 else 2292 stage = "?"; 2293 } else { 2294 stage = "FAILED"; 2295 } 2296 2297 if (zpa->zpa_current_depth >= zpa->zpa_min_depth) { 2298 if (zpa->zpa_flags & DCMD_PIPE_OUT) { 2299 mdb_printf("%?p\n", addr); 2300 } else { 2301 mdb_printf("%*s%-*p %-5s %-16s ", indent, "", 2302 ZIO_MAXWIDTH - indent, addr, type, stage); 2303 if (zio.io_waiter != 0) 2304 mdb_printf("%-16lx ", zio.io_waiter); 2305 else 2306 mdb_printf("%-16s ", "-"); 2307 #ifdef _KERNEL 2308 if (zio_timestamp.io_timestamp != 0) { 2309 mdb_printf("%llums", (mdb_gethrtime() - 2310 zio_timestamp.io_timestamp) / 2311 1000000); 2312 } else { 2313 mdb_printf("%-12s ", "-"); 2314 } 2315 #else 2316 mdb_printf("%-12s ", "-"); 2317 #endif 2318 mdb_printf("\n"); 2319 } 2320 } 2321 2322 if (zpa->zpa_current_depth >= zpa->zpa_max_depth) 2323 return (WALK_NEXT); 2324 2325 if (zpa->zpa_type == ZIO_WALK_PARENT) 2326 laddr = addr + mdb_ctf_offsetof_by_name(ZFS_STRUCT "zio", 2327 "io_parent_list"); 2328 else 2329 laddr = addr + mdb_ctf_offsetof_by_name(ZFS_STRUCT "zio", 2330 "io_child_list"); 2331 2332 zpa->zpa_current_depth++; 2333 if (mdb_pwalk("list", zio_child_cb, zpa, laddr) != 0) { 2334 mdb_warn("failed to walk zio_t children at %p\n", laddr); 2335 return (WALK_ERR); 2336 } 2337 zpa->zpa_current_depth--; 2338 2339 return (WALK_NEXT); 2340 } 2341 2342 /* ARGSUSED */ 2343 static int 2344 zio_child_cb(uintptr_t addr, const void *unknown, void *arg) 2345 { 2346 zio_link_t zl; 2347 uintptr_t ziop; 2348 zio_print_args_t *zpa = arg; 2349 2350 if (mdb_vread(&zl, sizeof (zl), addr) == -1) { 2351 mdb_warn("failed to read zio_link_t at %p", addr); 2352 return (WALK_ERR); 2353 } 2354 2355 if (zpa->zpa_type == ZIO_WALK_PARENT) 2356 ziop = (uintptr_t)zl.zl_parent; 2357 else 2358 ziop = (uintptr_t)zl.zl_child; 2359 2360 return (zio_print_cb(ziop, zpa)); 2361 } 2362 2363 /* ARGSUSED */ 2364 static int 2365 zio_print(uintptr_t addr, uint_t flags, int argc, const mdb_arg_t *argv) 2366 { 2367 zio_print_args_t zpa = { 0 }; 2368 2369 if (!(flags & DCMD_ADDRSPEC)) 2370 return (DCMD_USAGE); 2371 2372 if (mdb_getopts(argc, argv, 2373 'r', MDB_OPT_SETBITS, INT_MAX, &zpa.zpa_max_depth, 2374 'c', MDB_OPT_SETBITS, ZIO_WALK_CHILD, &zpa.zpa_type, 2375 'p', MDB_OPT_SETBITS, ZIO_WALK_PARENT, &zpa.zpa_type, 2376 NULL) != argc) 2377 return (DCMD_USAGE); 2378 2379 zpa.zpa_flags = flags; 2380 if (zpa.zpa_max_depth != 0) { 2381 if (zpa.zpa_type == ZIO_WALK_SELF) 2382 zpa.zpa_type = ZIO_WALK_CHILD; 2383 } else if (zpa.zpa_type != ZIO_WALK_SELF) { 2384 zpa.zpa_min_depth = 1; 2385 zpa.zpa_max_depth = 1; 2386 } 2387 2388 if (!(flags & DCMD_PIPE_OUT) && DCMD_HDRSPEC(flags)) { 2389 mdb_printf("%<u>%-*s %-5s %-16s %-16s %-12s%</u>\n", 2390 ZIO_MAXWIDTH, "ADDRESS", "TYPE", "STAGE", "WAITER", 2391 "TIME_ELAPSED"); 2392 } 2393 2394 if (zio_print_cb(addr, &zpa) != WALK_NEXT) 2395 return (DCMD_ERR); 2396 2397 return (DCMD_OK); 2398 } 2399 2400 /* 2401 * [addr]::zio_state 2402 * 2403 * Print a summary of all zio_t structures on the system, or for a particular 2404 * pool. This is equivalent to '::walk zio_root | ::zio'. 2405 */ 2406 /*ARGSUSED*/ 2407 static int 2408 zio_state(uintptr_t addr, uint_t flags, int argc, const mdb_arg_t *argv) 2409 { 2410 /* 2411 * MDB will remember the last address of the pipeline, so if we don't 2412 * zero this we'll end up trying to walk zio structures for a 2413 * non-existent spa_t. 2414 */ 2415 if (!(flags & DCMD_ADDRSPEC)) 2416 addr = 0; 2417 2418 return (mdb_pwalk_dcmd("zio_root", "zio", argc, argv, addr)); 2419 } 2420 2421 typedef struct mdb_multilist { 2422 uint64_t ml_num_sublists; 2423 uintptr_t ml_sublists; 2424 } mdb_multilist_t; 2425 2426 typedef struct multilist_walk_data { 2427 uint64_t mwd_idx; 2428 mdb_multilist_t mwd_ml; 2429 } multilist_walk_data_t; 2430 2431 /* ARGSUSED */ 2432 static int 2433 multilist_print_cb(uintptr_t addr, const void *unknown, void *arg) 2434 { 2435 mdb_printf("%#lr\n", addr); 2436 return (WALK_NEXT); 2437 } 2438 2439 static int 2440 multilist_walk_step(mdb_walk_state_t *wsp) 2441 { 2442 multilist_walk_data_t *mwd = wsp->walk_data; 2443 2444 if (mwd->mwd_idx >= mwd->mwd_ml.ml_num_sublists) 2445 return (WALK_DONE); 2446 2447 wsp->walk_addr = mwd->mwd_ml.ml_sublists + 2448 mdb_ctf_sizeof_by_name("multilist_sublist_t") * mwd->mwd_idx + 2449 mdb_ctf_offsetof_by_name("multilist_sublist_t", "mls_list"); 2450 2451 mdb_pwalk("list", multilist_print_cb, (void*)NULL, wsp->walk_addr); 2452 mwd->mwd_idx++; 2453 2454 return (WALK_NEXT); 2455 } 2456 2457 static int 2458 multilist_walk_init(mdb_walk_state_t *wsp) 2459 { 2460 multilist_walk_data_t *mwd; 2461 2462 if (wsp->walk_addr == NULL) { 2463 mdb_warn("must supply address of multilist_t\n"); 2464 return (WALK_ERR); 2465 } 2466 2467 mwd = mdb_zalloc(sizeof (multilist_walk_data_t), UM_SLEEP | UM_GC); 2468 if (mdb_ctf_vread(&mwd->mwd_ml, "multilist_t", "mdb_multilist_t", 2469 wsp->walk_addr, 0) == -1) { 2470 return (WALK_ERR); 2471 } 2472 2473 if (mwd->mwd_ml.ml_num_sublists == 0 || 2474 mwd->mwd_ml.ml_sublists == NULL) { 2475 mdb_warn("invalid or uninitialized multilist at %#lx\n", 2476 wsp->walk_addr); 2477 return (WALK_ERR); 2478 } 2479 2480 wsp->walk_data = mwd; 2481 return (WALK_NEXT); 2482 } 2483 2484 typedef struct mdb_txg_list { 2485 size_t tl_offset; 2486 uintptr_t tl_head[TXG_SIZE]; 2487 } mdb_txg_list_t; 2488 2489 typedef struct txg_list_walk_data { 2490 uintptr_t lw_head[TXG_SIZE]; 2491 int lw_txgoff; 2492 int lw_maxoff; 2493 size_t lw_offset; 2494 void *lw_obj; 2495 } txg_list_walk_data_t; 2496 2497 static int 2498 txg_list_walk_init_common(mdb_walk_state_t *wsp, int txg, int maxoff) 2499 { 2500 txg_list_walk_data_t *lwd; 2501 mdb_txg_list_t list; 2502 int i; 2503 2504 lwd = mdb_alloc(sizeof (txg_list_walk_data_t), UM_SLEEP | UM_GC); 2505 if (mdb_ctf_vread(&list, "txg_list_t", "mdb_txg_list_t", wsp->walk_addr, 2506 0) == -1) { 2507 mdb_warn("failed to read txg_list_t at %#lx", wsp->walk_addr); 2508 return (WALK_ERR); 2509 } 2510 2511 for (i = 0; i < TXG_SIZE; i++) 2512 lwd->lw_head[i] = list.tl_head[i]; 2513 lwd->lw_offset = list.tl_offset; 2514 lwd->lw_obj = mdb_alloc(lwd->lw_offset + sizeof (txg_node_t), 2515 UM_SLEEP | UM_GC); 2516 lwd->lw_txgoff = txg; 2517 lwd->lw_maxoff = maxoff; 2518 2519 wsp->walk_addr = lwd->lw_head[lwd->lw_txgoff]; 2520 wsp->walk_data = lwd; 2521 2522 return (WALK_NEXT); 2523 } 2524 2525 static int 2526 txg_list_walk_init(mdb_walk_state_t *wsp) 2527 { 2528 return (txg_list_walk_init_common(wsp, 0, TXG_SIZE-1)); 2529 } 2530 2531 static int 2532 txg_list0_walk_init(mdb_walk_state_t *wsp) 2533 { 2534 return (txg_list_walk_init_common(wsp, 0, 0)); 2535 } 2536 2537 static int 2538 txg_list1_walk_init(mdb_walk_state_t *wsp) 2539 { 2540 return (txg_list_walk_init_common(wsp, 1, 1)); 2541 } 2542 2543 static int 2544 txg_list2_walk_init(mdb_walk_state_t *wsp) 2545 { 2546 return (txg_list_walk_init_common(wsp, 2, 2)); 2547 } 2548 2549 static int 2550 txg_list3_walk_init(mdb_walk_state_t *wsp) 2551 { 2552 return (txg_list_walk_init_common(wsp, 3, 3)); 2553 } 2554 2555 static int 2556 txg_list_walk_step(mdb_walk_state_t *wsp) 2557 { 2558 txg_list_walk_data_t *lwd = wsp->walk_data; 2559 uintptr_t addr; 2560 txg_node_t *node; 2561 int status; 2562 2563 while (wsp->walk_addr == NULL && lwd->lw_txgoff < lwd->lw_maxoff) { 2564 lwd->lw_txgoff++; 2565 wsp->walk_addr = lwd->lw_head[lwd->lw_txgoff]; 2566 } 2567 2568 if (wsp->walk_addr == NULL) 2569 return (WALK_DONE); 2570 2571 addr = wsp->walk_addr - lwd->lw_offset; 2572 2573 if (mdb_vread(lwd->lw_obj, 2574 lwd->lw_offset + sizeof (txg_node_t), addr) == -1) { 2575 mdb_warn("failed to read list element at %#lx", addr); 2576 return (WALK_ERR); 2577 } 2578 2579 status = wsp->walk_callback(addr, lwd->lw_obj, wsp->walk_cbdata); 2580 node = (txg_node_t *)((uintptr_t)lwd->lw_obj + lwd->lw_offset); 2581 wsp->walk_addr = (uintptr_t)node->tn_next[lwd->lw_txgoff]; 2582 2583 return (status); 2584 } 2585 2586 /* 2587 * ::walk spa 2588 * 2589 * Walk all named spa_t structures in the namespace. This is nothing more than 2590 * a layered avl walk. 2591 */ 2592 static int 2593 spa_walk_init(mdb_walk_state_t *wsp) 2594 { 2595 GElf_Sym sym; 2596 2597 if (wsp->walk_addr != NULL) { 2598 mdb_warn("spa walk only supports global walks\n"); 2599 return (WALK_ERR); 2600 } 2601 2602 if (mdb_lookup_by_obj(ZFS_OBJ_NAME, "spa_namespace_avl", &sym) == -1) { 2603 mdb_warn("failed to find symbol 'spa_namespace_avl'"); 2604 return (WALK_ERR); 2605 } 2606 2607 wsp->walk_addr = (uintptr_t)sym.st_value; 2608 2609 if (mdb_layered_walk("avl", wsp) == -1) { 2610 mdb_warn("failed to walk 'avl'\n"); 2611 return (WALK_ERR); 2612 } 2613 2614 return (WALK_NEXT); 2615 } 2616 2617 static int 2618 spa_walk_step(mdb_walk_state_t *wsp) 2619 { 2620 return (wsp->walk_callback(wsp->walk_addr, NULL, wsp->walk_cbdata)); 2621 } 2622 2623 /* 2624 * [addr]::walk zio 2625 * 2626 * Walk all active zio_t structures on the system. This is simply a layered 2627 * walk on top of ::walk zio_cache, with the optional ability to limit the 2628 * structures to a particular pool. 2629 */ 2630 static int 2631 zio_walk_init(mdb_walk_state_t *wsp) 2632 { 2633 wsp->walk_data = (void *)wsp->walk_addr; 2634 2635 if (mdb_layered_walk("zio_cache", wsp) == -1) { 2636 mdb_warn("failed to walk 'zio_cache'\n"); 2637 return (WALK_ERR); 2638 } 2639 2640 return (WALK_NEXT); 2641 } 2642 2643 static int 2644 zio_walk_step(mdb_walk_state_t *wsp) 2645 { 2646 mdb_zio_t zio; 2647 uintptr_t spa = (uintptr_t)wsp->walk_data; 2648 2649 if (mdb_ctf_vread(&zio, ZFS_STRUCT "zio", "mdb_zio_t", 2650 wsp->walk_addr, 0) == -1) 2651 return (WALK_ERR); 2652 2653 if (spa != 0 && spa != zio.io_spa) 2654 return (WALK_NEXT); 2655 2656 return (wsp->walk_callback(wsp->walk_addr, &zio, wsp->walk_cbdata)); 2657 } 2658 2659 /* 2660 * [addr]::walk zio_root 2661 * 2662 * Walk only root zio_t structures, optionally for a particular spa_t. 2663 */ 2664 static int 2665 zio_walk_root_step(mdb_walk_state_t *wsp) 2666 { 2667 mdb_zio_t zio; 2668 uintptr_t spa = (uintptr_t)wsp->walk_data; 2669 2670 if (mdb_ctf_vread(&zio, ZFS_STRUCT "zio", "mdb_zio_t", 2671 wsp->walk_addr, 0) == -1) 2672 return (WALK_ERR); 2673 2674 if (spa != 0 && spa != zio.io_spa) 2675 return (WALK_NEXT); 2676 2677 /* If the parent list is not empty, ignore */ 2678 if (zio.io_parent_list.list_head.list_next != 2679 wsp->walk_addr + 2680 mdb_ctf_offsetof_by_name(ZFS_STRUCT "zio", "io_parent_list") + 2681 mdb_ctf_offsetof_by_name("struct list", "list_head")) 2682 return (WALK_NEXT); 2683 2684 return (wsp->walk_callback(wsp->walk_addr, &zio, wsp->walk_cbdata)); 2685 } 2686 2687 /* 2688 * ::zfs_blkstats 2689 * 2690 * -v print verbose per-level information 2691 * 2692 */ 2693 static int 2694 zfs_blkstats(uintptr_t addr, uint_t flags, int argc, const mdb_arg_t *argv) 2695 { 2696 boolean_t verbose = B_FALSE; 2697 zfs_all_blkstats_t stats; 2698 dmu_object_type_t t; 2699 zfs_blkstat_t *tzb; 2700 uint64_t ditto; 2701 dmu_object_type_info_t dmu_ot[DMU_OT_NUMTYPES + 10]; 2702 /* +10 in case it grew */ 2703 2704 if (mdb_readvar(&dmu_ot, "dmu_ot") == -1) { 2705 mdb_warn("failed to read 'dmu_ot'"); 2706 return (DCMD_ERR); 2707 } 2708 2709 if (mdb_getopts(argc, argv, 2710 'v', MDB_OPT_SETBITS, TRUE, &verbose, 2711 NULL) != argc) 2712 return (DCMD_USAGE); 2713 2714 if (!(flags & DCMD_ADDRSPEC)) 2715 return (DCMD_USAGE); 2716 2717 if (GETMEMB(addr, "spa", spa_dsl_pool, addr) || 2718 GETMEMB(addr, "dsl_pool", dp_blkstats, addr) || 2719 mdb_vread(&stats, sizeof (zfs_all_blkstats_t), addr) == -1) { 2720 mdb_warn("failed to read data at %p;", addr); 2721 mdb_printf("maybe no stats? run \"zpool scrub\" first."); 2722 return (DCMD_ERR); 2723 } 2724 2725 tzb = &stats.zab_type[DN_MAX_LEVELS][DMU_OT_TOTAL]; 2726 if (tzb->zb_gangs != 0) { 2727 mdb_printf("Ganged blocks: %llu\n", 2728 (longlong_t)tzb->zb_gangs); 2729 } 2730 2731 ditto = tzb->zb_ditto_2_of_2_samevdev + tzb->zb_ditto_2_of_3_samevdev + 2732 tzb->zb_ditto_3_of_3_samevdev; 2733 if (ditto != 0) { 2734 mdb_printf("Dittoed blocks on same vdev: %llu\n", 2735 (longlong_t)ditto); 2736 } 2737 2738 mdb_printf("\nBlocks\tLSIZE\tPSIZE\tASIZE" 2739 "\t avg\t comp\t%%Total\tType\n"); 2740 2741 for (t = 0; t <= DMU_OT_TOTAL; t++) { 2742 char csize[NICENUM_BUFLEN], lsize[NICENUM_BUFLEN]; 2743 char psize[NICENUM_BUFLEN], asize[NICENUM_BUFLEN]; 2744 char avg[NICENUM_BUFLEN]; 2745 char comp[NICENUM_BUFLEN], pct[NICENUM_BUFLEN]; 2746 char typename[64]; 2747 int l; 2748 2749 2750 if (t == DMU_OT_DEFERRED) 2751 strcpy(typename, "deferred free"); 2752 else if (t == DMU_OT_OTHER) 2753 strcpy(typename, "other"); 2754 else if (t == DMU_OT_TOTAL) 2755 strcpy(typename, "Total"); 2756 else if (mdb_readstr(typename, sizeof (typename), 2757 (uintptr_t)dmu_ot[t].ot_name) == -1) { 2758 mdb_warn("failed to read type name"); 2759 return (DCMD_ERR); 2760 } 2761 2762 if (stats.zab_type[DN_MAX_LEVELS][t].zb_asize == 0) 2763 continue; 2764 2765 for (l = -1; l < DN_MAX_LEVELS; l++) { 2766 int level = (l == -1 ? DN_MAX_LEVELS : l); 2767 zfs_blkstat_t *zb = &stats.zab_type[level][t]; 2768 2769 if (zb->zb_asize == 0) 2770 continue; 2771 2772 /* 2773 * Don't print each level unless requested. 2774 */ 2775 if (!verbose && level != DN_MAX_LEVELS) 2776 continue; 2777 2778 /* 2779 * If all the space is level 0, don't print the 2780 * level 0 separately. 2781 */ 2782 if (level == 0 && zb->zb_asize == 2783 stats.zab_type[DN_MAX_LEVELS][t].zb_asize) 2784 continue; 2785 2786 mdb_nicenum(zb->zb_count, csize); 2787 mdb_nicenum(zb->zb_lsize, lsize); 2788 mdb_nicenum(zb->zb_psize, psize); 2789 mdb_nicenum(zb->zb_asize, asize); 2790 mdb_nicenum(zb->zb_asize / zb->zb_count, avg); 2791 (void) snprintfrac(comp, NICENUM_BUFLEN, 2792 zb->zb_lsize, zb->zb_psize, 2); 2793 (void) snprintfrac(pct, NICENUM_BUFLEN, 2794 100 * zb->zb_asize, tzb->zb_asize, 2); 2795 2796 mdb_printf("%6s\t%5s\t%5s\t%5s\t%5s" 2797 "\t%5s\t%6s\t", 2798 csize, lsize, psize, asize, avg, comp, pct); 2799 2800 if (level == DN_MAX_LEVELS) 2801 mdb_printf("%s\n", typename); 2802 else 2803 mdb_printf(" L%d %s\n", 2804 level, typename); 2805 } 2806 } 2807 2808 return (DCMD_OK); 2809 } 2810 2811 typedef struct mdb_reference { 2812 uintptr_t ref_holder; 2813 uintptr_t ref_removed; 2814 uint64_t ref_number; 2815 } mdb_reference_t; 2816 2817 /* ARGSUSED */ 2818 static int 2819 reference_cb(uintptr_t addr, const void *ignored, void *arg) 2820 { 2821 mdb_reference_t ref; 2822 boolean_t holder_is_str = B_FALSE; 2823 char holder_str[128]; 2824 boolean_t removed = (boolean_t)arg; 2825 2826 if (mdb_ctf_vread(&ref, "reference_t", "mdb_reference_t", addr, 2827 0) == -1) 2828 return (DCMD_ERR); 2829 2830 if (mdb_readstr(holder_str, sizeof (holder_str), 2831 ref.ref_holder) != -1) 2832 holder_is_str = strisprint(holder_str); 2833 2834 if (removed) 2835 mdb_printf("removed "); 2836 mdb_printf("reference "); 2837 if (ref.ref_number != 1) 2838 mdb_printf("with count=%llu ", ref.ref_number); 2839 mdb_printf("with tag %lx", ref.ref_holder); 2840 if (holder_is_str) 2841 mdb_printf(" \"%s\"", holder_str); 2842 mdb_printf(", held at:\n"); 2843 2844 (void) mdb_call_dcmd("whatis", addr, DCMD_ADDRSPEC, 0, NULL); 2845 2846 if (removed) { 2847 mdb_printf("removed at:\n"); 2848 (void) mdb_call_dcmd("whatis", ref.ref_removed, 2849 DCMD_ADDRSPEC, 0, NULL); 2850 } 2851 2852 mdb_printf("\n"); 2853 2854 return (WALK_NEXT); 2855 } 2856 2857 typedef struct mdb_refcount { 2858 uint64_t rc_count; 2859 } mdb_refcount_t; 2860 2861 typedef struct mdb_refcount_removed { 2862 uint64_t rc_removed_count; 2863 } mdb_refcount_removed_t; 2864 2865 typedef struct mdb_refcount_tracked { 2866 boolean_t rc_tracked; 2867 } mdb_refcount_tracked_t; 2868 2869 /* ARGSUSED */ 2870 static int 2871 refcount(uintptr_t addr, uint_t flags, int argc, const mdb_arg_t *argv) 2872 { 2873 mdb_refcount_t rc; 2874 mdb_refcount_removed_t rcr; 2875 mdb_refcount_tracked_t rct; 2876 int off; 2877 boolean_t released = B_FALSE; 2878 2879 if (!(flags & DCMD_ADDRSPEC)) 2880 return (DCMD_USAGE); 2881 2882 if (mdb_getopts(argc, argv, 2883 'r', MDB_OPT_SETBITS, B_TRUE, &released, 2884 NULL) != argc) 2885 return (DCMD_USAGE); 2886 2887 if (mdb_ctf_vread(&rc, "refcount_t", "mdb_refcount_t", addr, 2888 0) == -1) 2889 return (DCMD_ERR); 2890 2891 if (mdb_ctf_vread(&rcr, "refcount_t", "mdb_refcount_removed_t", addr, 2892 MDB_CTF_VREAD_QUIET) == -1) { 2893 mdb_printf("refcount_t at %p has %llu holds (untracked)\n", 2894 addr, (longlong_t)rc.rc_count); 2895 return (DCMD_OK); 2896 } 2897 2898 if (mdb_ctf_vread(&rct, "refcount_t", "mdb_refcount_tracked_t", addr, 2899 MDB_CTF_VREAD_QUIET) == -1) { 2900 /* If this is an old target, it might be tracked. */ 2901 rct.rc_tracked = B_TRUE; 2902 } 2903 2904 mdb_printf("refcount_t at %p has %llu current holds, " 2905 "%llu recently released holds\n", 2906 addr, (longlong_t)rc.rc_count, (longlong_t)rcr.rc_removed_count); 2907 2908 if (rct.rc_tracked && rc.rc_count > 0) 2909 mdb_printf("current holds:\n"); 2910 off = mdb_ctf_offsetof_by_name("refcount_t", "rc_list"); 2911 if (off == -1) 2912 return (DCMD_ERR); 2913 mdb_pwalk("list", reference_cb, (void*)B_FALSE, addr + off); 2914 2915 if (released && rcr.rc_removed_count > 0) { 2916 mdb_printf("released holds:\n"); 2917 2918 off = mdb_ctf_offsetof_by_name("refcount_t", "rc_removed"); 2919 if (off == -1) 2920 return (DCMD_ERR); 2921 mdb_pwalk("list", reference_cb, (void*)B_TRUE, addr + off); 2922 } 2923 2924 return (DCMD_OK); 2925 } 2926 2927 /* ARGSUSED */ 2928 static int 2929 sa_attr_table(uintptr_t addr, uint_t flags, int argc, const mdb_arg_t *argv) 2930 { 2931 sa_attr_table_t *table; 2932 sa_os_t sa_os; 2933 char *name; 2934 int i; 2935 2936 if (mdb_vread(&sa_os, sizeof (sa_os_t), addr) == -1) { 2937 mdb_warn("failed to read sa_os at %p", addr); 2938 return (DCMD_ERR); 2939 } 2940 2941 table = mdb_alloc(sizeof (sa_attr_table_t) * sa_os.sa_num_attrs, 2942 UM_SLEEP | UM_GC); 2943 name = mdb_alloc(MAXPATHLEN, UM_SLEEP | UM_GC); 2944 2945 if (mdb_vread(table, sizeof (sa_attr_table_t) * sa_os.sa_num_attrs, 2946 (uintptr_t)sa_os.sa_attr_table) == -1) { 2947 mdb_warn("failed to read sa_os at %p", addr); 2948 return (DCMD_ERR); 2949 } 2950 2951 mdb_printf("%<u>%-10s %-10s %-10s %-10s %s%</u>\n", 2952 "ATTR ID", "REGISTERED", "LENGTH", "BSWAP", "NAME"); 2953 for (i = 0; i != sa_os.sa_num_attrs; i++) { 2954 mdb_readstr(name, MAXPATHLEN, (uintptr_t)table[i].sa_name); 2955 mdb_printf("%5x %8x %8x %8x %-s\n", 2956 (int)table[i].sa_attr, (int)table[i].sa_registered, 2957 (int)table[i].sa_length, table[i].sa_byteswap, name); 2958 } 2959 2960 return (DCMD_OK); 2961 } 2962 2963 static int 2964 sa_get_off_table(uintptr_t addr, uint32_t **off_tab, int attr_count) 2965 { 2966 uintptr_t idx_table; 2967 2968 if (GETMEMB(addr, "sa_idx_tab", sa_idx_tab, idx_table)) { 2969 mdb_printf("can't find offset table in sa_idx_tab\n"); 2970 return (-1); 2971 } 2972 2973 *off_tab = mdb_alloc(attr_count * sizeof (uint32_t), 2974 UM_SLEEP | UM_GC); 2975 2976 if (mdb_vread(*off_tab, 2977 attr_count * sizeof (uint32_t), idx_table) == -1) { 2978 mdb_warn("failed to attribute offset table %p", idx_table); 2979 return (-1); 2980 } 2981 2982 return (DCMD_OK); 2983 } 2984 2985 /*ARGSUSED*/ 2986 static int 2987 sa_attr_print(uintptr_t addr, uint_t flags, int argc, const mdb_arg_t *argv) 2988 { 2989 uint32_t *offset_tab; 2990 int attr_count; 2991 uint64_t attr_id; 2992 uintptr_t attr_addr; 2993 uintptr_t bonus_tab, spill_tab; 2994 uintptr_t db_bonus, db_spill; 2995 uintptr_t os, os_sa; 2996 uintptr_t db_data; 2997 2998 if (argc != 1) 2999 return (DCMD_USAGE); 3000 3001 if (argv[0].a_type == MDB_TYPE_STRING) 3002 attr_id = mdb_strtoull(argv[0].a_un.a_str); 3003 else 3004 return (DCMD_USAGE); 3005 3006 if (GETMEMB(addr, "sa_handle", sa_bonus_tab, bonus_tab) || 3007 GETMEMB(addr, "sa_handle", sa_spill_tab, spill_tab) || 3008 GETMEMB(addr, "sa_handle", sa_os, os) || 3009 GETMEMB(addr, "sa_handle", sa_bonus, db_bonus) || 3010 GETMEMB(addr, "sa_handle", sa_spill, db_spill)) { 3011 mdb_printf("Can't find necessary information in sa_handle " 3012 "in sa_handle\n"); 3013 return (DCMD_ERR); 3014 } 3015 3016 if (GETMEMB(os, "objset", os_sa, os_sa)) { 3017 mdb_printf("Can't find os_sa in objset\n"); 3018 return (DCMD_ERR); 3019 } 3020 3021 if (GETMEMB(os_sa, "sa_os", sa_num_attrs, attr_count)) { 3022 mdb_printf("Can't find sa_num_attrs\n"); 3023 return (DCMD_ERR); 3024 } 3025 3026 if (attr_id > attr_count) { 3027 mdb_printf("attribute id number is out of range\n"); 3028 return (DCMD_ERR); 3029 } 3030 3031 if (bonus_tab) { 3032 if (sa_get_off_table(bonus_tab, &offset_tab, 3033 attr_count) == -1) { 3034 return (DCMD_ERR); 3035 } 3036 3037 if (GETMEMB(db_bonus, "dmu_buf", db_data, db_data)) { 3038 mdb_printf("can't find db_data in bonus dbuf\n"); 3039 return (DCMD_ERR); 3040 } 3041 } 3042 3043 if (bonus_tab && !TOC_ATTR_PRESENT(offset_tab[attr_id]) && 3044 spill_tab == NULL) { 3045 mdb_printf("Attribute does not exist\n"); 3046 return (DCMD_ERR); 3047 } else if (!TOC_ATTR_PRESENT(offset_tab[attr_id]) && spill_tab) { 3048 if (sa_get_off_table(spill_tab, &offset_tab, 3049 attr_count) == -1) { 3050 return (DCMD_ERR); 3051 } 3052 if (GETMEMB(db_spill, "dmu_buf", db_data, db_data)) { 3053 mdb_printf("can't find db_data in spill dbuf\n"); 3054 return (DCMD_ERR); 3055 } 3056 if (!TOC_ATTR_PRESENT(offset_tab[attr_id])) { 3057 mdb_printf("Attribute does not exist\n"); 3058 return (DCMD_ERR); 3059 } 3060 } 3061 attr_addr = db_data + TOC_OFF(offset_tab[attr_id]); 3062 mdb_printf("%p\n", attr_addr); 3063 return (DCMD_OK); 3064 } 3065 3066 /* ARGSUSED */ 3067 static int 3068 zfs_ace_print_common(uintptr_t addr, uint_t flags, 3069 uint64_t id, uint32_t access_mask, uint16_t ace_flags, 3070 uint16_t ace_type, int verbose) 3071 { 3072 if (DCMD_HDRSPEC(flags) && !verbose) 3073 mdb_printf("%<u>%-?s %-8s %-8s %-8s %s%</u>\n", 3074 "ADDR", "FLAGS", "MASK", "TYPE", "ID"); 3075 3076 if (!verbose) { 3077 mdb_printf("%0?p %-8x %-8x %-8x %-llx\n", addr, 3078 ace_flags, access_mask, ace_type, id); 3079 return (DCMD_OK); 3080 } 3081 3082 switch (ace_flags & ACE_TYPE_FLAGS) { 3083 case ACE_OWNER: 3084 mdb_printf("owner@:"); 3085 break; 3086 case (ACE_IDENTIFIER_GROUP | ACE_GROUP): 3087 mdb_printf("group@:"); 3088 break; 3089 case ACE_EVERYONE: 3090 mdb_printf("everyone@:"); 3091 break; 3092 case ACE_IDENTIFIER_GROUP: 3093 mdb_printf("group:%llx:", (u_longlong_t)id); 3094 break; 3095 case 0: /* User entry */ 3096 mdb_printf("user:%llx:", (u_longlong_t)id); 3097 break; 3098 } 3099 3100 /* print out permission mask */ 3101 if (access_mask & ACE_READ_DATA) 3102 mdb_printf("r"); 3103 else 3104 mdb_printf("-"); 3105 if (access_mask & ACE_WRITE_DATA) 3106 mdb_printf("w"); 3107 else 3108 mdb_printf("-"); 3109 if (access_mask & ACE_EXECUTE) 3110 mdb_printf("x"); 3111 else 3112 mdb_printf("-"); 3113 if (access_mask & ACE_APPEND_DATA) 3114 mdb_printf("p"); 3115 else 3116 mdb_printf("-"); 3117 if (access_mask & ACE_DELETE) 3118 mdb_printf("d"); 3119 else 3120 mdb_printf("-"); 3121 if (access_mask & ACE_DELETE_CHILD) 3122 mdb_printf("D"); 3123 else 3124 mdb_printf("-"); 3125 if (access_mask & ACE_READ_ATTRIBUTES) 3126 mdb_printf("a"); 3127 else 3128 mdb_printf("-"); 3129 if (access_mask & ACE_WRITE_ATTRIBUTES) 3130 mdb_printf("A"); 3131 else 3132 mdb_printf("-"); 3133 if (access_mask & ACE_READ_NAMED_ATTRS) 3134 mdb_printf("R"); 3135 else 3136 mdb_printf("-"); 3137 if (access_mask & ACE_WRITE_NAMED_ATTRS) 3138 mdb_printf("W"); 3139 else 3140 mdb_printf("-"); 3141 if (access_mask & ACE_READ_ACL) 3142 mdb_printf("c"); 3143 else 3144 mdb_printf("-"); 3145 if (access_mask & ACE_WRITE_ACL) 3146 mdb_printf("C"); 3147 else 3148 mdb_printf("-"); 3149 if (access_mask & ACE_WRITE_OWNER) 3150 mdb_printf("o"); 3151 else 3152 mdb_printf("-"); 3153 if (access_mask & ACE_SYNCHRONIZE) 3154 mdb_printf("s"); 3155 else 3156 mdb_printf("-"); 3157 3158 mdb_printf(":"); 3159 3160 /* Print out inheritance flags */ 3161 if (ace_flags & ACE_FILE_INHERIT_ACE) 3162 mdb_printf("f"); 3163 else 3164 mdb_printf("-"); 3165 if (ace_flags & ACE_DIRECTORY_INHERIT_ACE) 3166 mdb_printf("d"); 3167 else 3168 mdb_printf("-"); 3169 if (ace_flags & ACE_INHERIT_ONLY_ACE) 3170 mdb_printf("i"); 3171 else 3172 mdb_printf("-"); 3173 if (ace_flags & ACE_NO_PROPAGATE_INHERIT_ACE) 3174 mdb_printf("n"); 3175 else 3176 mdb_printf("-"); 3177 if (ace_flags & ACE_SUCCESSFUL_ACCESS_ACE_FLAG) 3178 mdb_printf("S"); 3179 else 3180 mdb_printf("-"); 3181 if (ace_flags & ACE_FAILED_ACCESS_ACE_FLAG) 3182 mdb_printf("F"); 3183 else 3184 mdb_printf("-"); 3185 if (ace_flags & ACE_INHERITED_ACE) 3186 mdb_printf("I"); 3187 else 3188 mdb_printf("-"); 3189 3190 switch (ace_type) { 3191 case ACE_ACCESS_ALLOWED_ACE_TYPE: 3192 mdb_printf(":allow\n"); 3193 break; 3194 case ACE_ACCESS_DENIED_ACE_TYPE: 3195 mdb_printf(":deny\n"); 3196 break; 3197 case ACE_SYSTEM_AUDIT_ACE_TYPE: 3198 mdb_printf(":audit\n"); 3199 break; 3200 case ACE_SYSTEM_ALARM_ACE_TYPE: 3201 mdb_printf(":alarm\n"); 3202 break; 3203 default: 3204 mdb_printf(":?\n"); 3205 } 3206 return (DCMD_OK); 3207 } 3208 3209 /* ARGSUSED */ 3210 static int 3211 zfs_ace_print(uintptr_t addr, uint_t flags, int argc, const mdb_arg_t *argv) 3212 { 3213 zfs_ace_t zace; 3214 int verbose = FALSE; 3215 uint64_t id; 3216 3217 if (!(flags & DCMD_ADDRSPEC)) 3218 return (DCMD_USAGE); 3219 3220 if (mdb_getopts(argc, argv, 3221 'v', MDB_OPT_SETBITS, TRUE, &verbose, TRUE, NULL) != argc) 3222 return (DCMD_USAGE); 3223 3224 if (mdb_vread(&zace, sizeof (zfs_ace_t), addr) == -1) { 3225 mdb_warn("failed to read zfs_ace_t"); 3226 return (DCMD_ERR); 3227 } 3228 3229 if ((zace.z_hdr.z_flags & ACE_TYPE_FLAGS) == 0 || 3230 (zace.z_hdr.z_flags & ACE_TYPE_FLAGS) == ACE_IDENTIFIER_GROUP) 3231 id = zace.z_fuid; 3232 else 3233 id = -1; 3234 3235 return (zfs_ace_print_common(addr, flags, id, zace.z_hdr.z_access_mask, 3236 zace.z_hdr.z_flags, zace.z_hdr.z_type, verbose)); 3237 } 3238 3239 /* ARGSUSED */ 3240 static int 3241 zfs_ace0_print(uintptr_t addr, uint_t flags, int argc, const mdb_arg_t *argv) 3242 { 3243 ace_t ace; 3244 uint64_t id; 3245 int verbose = FALSE; 3246 3247 if (!(flags & DCMD_ADDRSPEC)) 3248 return (DCMD_USAGE); 3249 3250 if (mdb_getopts(argc, argv, 3251 'v', MDB_OPT_SETBITS, TRUE, &verbose, TRUE, NULL) != argc) 3252 return (DCMD_USAGE); 3253 3254 if (mdb_vread(&ace, sizeof (ace_t), addr) == -1) { 3255 mdb_warn("failed to read ace_t"); 3256 return (DCMD_ERR); 3257 } 3258 3259 if ((ace.a_flags & ACE_TYPE_FLAGS) == 0 || 3260 (ace.a_flags & ACE_TYPE_FLAGS) == ACE_IDENTIFIER_GROUP) 3261 id = ace.a_who; 3262 else 3263 id = -1; 3264 3265 return (zfs_ace_print_common(addr, flags, id, ace.a_access_mask, 3266 ace.a_flags, ace.a_type, verbose)); 3267 } 3268 3269 typedef struct acl_dump_args { 3270 int a_argc; 3271 const mdb_arg_t *a_argv; 3272 uint16_t a_version; 3273 int a_flags; 3274 } acl_dump_args_t; 3275 3276 /* ARGSUSED */ 3277 static int 3278 acl_aces_cb(uintptr_t addr, const void *unknown, void *arg) 3279 { 3280 acl_dump_args_t *acl_args = (acl_dump_args_t *)arg; 3281 3282 if (acl_args->a_version == 1) { 3283 if (mdb_call_dcmd("zfs_ace", addr, 3284 DCMD_ADDRSPEC|acl_args->a_flags, acl_args->a_argc, 3285 acl_args->a_argv) != DCMD_OK) { 3286 return (WALK_ERR); 3287 } 3288 } else { 3289 if (mdb_call_dcmd("zfs_ace0", addr, 3290 DCMD_ADDRSPEC|acl_args->a_flags, acl_args->a_argc, 3291 acl_args->a_argv) != DCMD_OK) { 3292 return (WALK_ERR); 3293 } 3294 } 3295 acl_args->a_flags = DCMD_LOOP; 3296 return (WALK_NEXT); 3297 } 3298 3299 /* ARGSUSED */ 3300 static int 3301 acl_cb(uintptr_t addr, const void *unknown, void *arg) 3302 { 3303 acl_dump_args_t *acl_args = (acl_dump_args_t *)arg; 3304 3305 if (acl_args->a_version == 1) { 3306 if (mdb_pwalk("zfs_acl_node_aces", acl_aces_cb, 3307 arg, addr) != 0) { 3308 mdb_warn("can't walk ACEs"); 3309 return (DCMD_ERR); 3310 } 3311 } else { 3312 if (mdb_pwalk("zfs_acl_node_aces0", acl_aces_cb, 3313 arg, addr) != 0) { 3314 mdb_warn("can't walk ACEs"); 3315 return (DCMD_ERR); 3316 } 3317 } 3318 return (WALK_NEXT); 3319 } 3320 3321 /* ARGSUSED */ 3322 static int 3323 zfs_acl_dump(uintptr_t addr, uint_t flags, int argc, const mdb_arg_t *argv) 3324 { 3325 zfs_acl_t zacl; 3326 int verbose = FALSE; 3327 acl_dump_args_t acl_args; 3328 3329 if (!(flags & DCMD_ADDRSPEC)) 3330 return (DCMD_USAGE); 3331 3332 if (mdb_getopts(argc, argv, 3333 'v', MDB_OPT_SETBITS, TRUE, &verbose, TRUE, NULL) != argc) 3334 return (DCMD_USAGE); 3335 3336 if (mdb_vread(&zacl, sizeof (zfs_acl_t), addr) == -1) { 3337 mdb_warn("failed to read zfs_acl_t"); 3338 return (DCMD_ERR); 3339 } 3340 3341 acl_args.a_argc = argc; 3342 acl_args.a_argv = argv; 3343 acl_args.a_version = zacl.z_version; 3344 acl_args.a_flags = DCMD_LOOPFIRST; 3345 3346 if (mdb_pwalk("zfs_acl_node", acl_cb, &acl_args, addr) != 0) { 3347 mdb_warn("can't walk ACL"); 3348 return (DCMD_ERR); 3349 } 3350 3351 return (DCMD_OK); 3352 } 3353 3354 /* ARGSUSED */ 3355 static int 3356 zfs_acl_node_walk_init(mdb_walk_state_t *wsp) 3357 { 3358 if (wsp->walk_addr == NULL) { 3359 mdb_warn("must supply address of zfs_acl_node_t\n"); 3360 return (WALK_ERR); 3361 } 3362 3363 wsp->walk_addr += 3364 mdb_ctf_offsetof_by_name(ZFS_STRUCT "zfs_acl", "z_acl"); 3365 3366 if (mdb_layered_walk("list", wsp) == -1) { 3367 mdb_warn("failed to walk 'list'\n"); 3368 return (WALK_ERR); 3369 } 3370 3371 return (WALK_NEXT); 3372 } 3373 3374 static int 3375 zfs_acl_node_walk_step(mdb_walk_state_t *wsp) 3376 { 3377 zfs_acl_node_t aclnode; 3378 3379 if (mdb_vread(&aclnode, sizeof (zfs_acl_node_t), 3380 wsp->walk_addr) == -1) { 3381 mdb_warn("failed to read zfs_acl_node at %p", wsp->walk_addr); 3382 return (WALK_ERR); 3383 } 3384 3385 return (wsp->walk_callback(wsp->walk_addr, &aclnode, wsp->walk_cbdata)); 3386 } 3387 3388 typedef struct ace_walk_data { 3389 int ace_count; 3390 int ace_version; 3391 } ace_walk_data_t; 3392 3393 static int 3394 zfs_aces_walk_init_common(mdb_walk_state_t *wsp, int version, 3395 int ace_count, uintptr_t ace_data) 3396 { 3397 ace_walk_data_t *ace_walk_data; 3398 3399 if (wsp->walk_addr == NULL) { 3400 mdb_warn("must supply address of zfs_acl_node_t\n"); 3401 return (WALK_ERR); 3402 } 3403 3404 ace_walk_data = mdb_alloc(sizeof (ace_walk_data_t), UM_SLEEP | UM_GC); 3405 3406 ace_walk_data->ace_count = ace_count; 3407 ace_walk_data->ace_version = version; 3408 3409 wsp->walk_addr = ace_data; 3410 wsp->walk_data = ace_walk_data; 3411 3412 return (WALK_NEXT); 3413 } 3414 3415 static int 3416 zfs_acl_node_aces_walk_init_common(mdb_walk_state_t *wsp, int version) 3417 { 3418 static int gotid; 3419 static mdb_ctf_id_t acl_id; 3420 int z_ace_count; 3421 uintptr_t z_acldata; 3422 3423 if (!gotid) { 3424 if (mdb_ctf_lookup_by_name("struct zfs_acl_node", 3425 &acl_id) == -1) { 3426 mdb_warn("couldn't find struct zfs_acl_node"); 3427 return (DCMD_ERR); 3428 } 3429 gotid = TRUE; 3430 } 3431 3432 if (GETMEMBID(wsp->walk_addr, &acl_id, z_ace_count, z_ace_count)) { 3433 return (DCMD_ERR); 3434 } 3435 if (GETMEMBID(wsp->walk_addr, &acl_id, z_acldata, z_acldata)) { 3436 return (DCMD_ERR); 3437 } 3438 3439 return (zfs_aces_walk_init_common(wsp, version, 3440 z_ace_count, z_acldata)); 3441 } 3442 3443 /* ARGSUSED */ 3444 static int 3445 zfs_acl_node_aces_walk_init(mdb_walk_state_t *wsp) 3446 { 3447 return (zfs_acl_node_aces_walk_init_common(wsp, 1)); 3448 } 3449 3450 /* ARGSUSED */ 3451 static int 3452 zfs_acl_node_aces0_walk_init(mdb_walk_state_t *wsp) 3453 { 3454 return (zfs_acl_node_aces_walk_init_common(wsp, 0)); 3455 } 3456 3457 static int 3458 zfs_aces_walk_step(mdb_walk_state_t *wsp) 3459 { 3460 ace_walk_data_t *ace_data = wsp->walk_data; 3461 zfs_ace_t zace; 3462 ace_t *acep; 3463 int status; 3464 int entry_type; 3465 int allow_type; 3466 uintptr_t ptr; 3467 3468 if (ace_data->ace_count == 0) 3469 return (WALK_DONE); 3470 3471 if (mdb_vread(&zace, sizeof (zfs_ace_t), wsp->walk_addr) == -1) { 3472 mdb_warn("failed to read zfs_ace_t at %#lx", 3473 wsp->walk_addr); 3474 return (WALK_ERR); 3475 } 3476 3477 switch (ace_data->ace_version) { 3478 case 0: 3479 acep = (ace_t *)&zace; 3480 entry_type = acep->a_flags & ACE_TYPE_FLAGS; 3481 allow_type = acep->a_type; 3482 break; 3483 case 1: 3484 entry_type = zace.z_hdr.z_flags & ACE_TYPE_FLAGS; 3485 allow_type = zace.z_hdr.z_type; 3486 break; 3487 default: 3488 return (WALK_ERR); 3489 } 3490 3491 ptr = (uintptr_t)wsp->walk_addr; 3492 switch (entry_type) { 3493 case ACE_OWNER: 3494 case ACE_EVERYONE: 3495 case (ACE_IDENTIFIER_GROUP | ACE_GROUP): 3496 ptr += ace_data->ace_version == 0 ? 3497 sizeof (ace_t) : sizeof (zfs_ace_hdr_t); 3498 break; 3499 case ACE_IDENTIFIER_GROUP: 3500 default: 3501 switch (allow_type) { 3502 case ACE_ACCESS_ALLOWED_OBJECT_ACE_TYPE: 3503 case ACE_ACCESS_DENIED_OBJECT_ACE_TYPE: 3504 case ACE_SYSTEM_AUDIT_OBJECT_ACE_TYPE: 3505 case ACE_SYSTEM_ALARM_OBJECT_ACE_TYPE: 3506 ptr += ace_data->ace_version == 0 ? 3507 sizeof (ace_t) : sizeof (zfs_object_ace_t); 3508 break; 3509 default: 3510 ptr += ace_data->ace_version == 0 ? 3511 sizeof (ace_t) : sizeof (zfs_ace_t); 3512 break; 3513 } 3514 } 3515 3516 ace_data->ace_count--; 3517 status = wsp->walk_callback(wsp->walk_addr, 3518 (void *)(uintptr_t)&zace, wsp->walk_cbdata); 3519 3520 wsp->walk_addr = ptr; 3521 return (status); 3522 } 3523 3524 typedef struct mdb_zfs_rrwlock { 3525 uintptr_t rr_writer; 3526 boolean_t rr_writer_wanted; 3527 } mdb_zfs_rrwlock_t; 3528 3529 static uint_t rrw_key; 3530 3531 /* ARGSUSED */ 3532 static int 3533 rrwlock(uintptr_t addr, uint_t flags, int argc, const mdb_arg_t *argv) 3534 { 3535 mdb_zfs_rrwlock_t rrw; 3536 3537 if (rrw_key == 0) { 3538 if (mdb_ctf_readsym(&rrw_key, "uint_t", "rrw_tsd_key", 0) == -1) 3539 return (DCMD_ERR); 3540 } 3541 3542 if (mdb_ctf_vread(&rrw, "rrwlock_t", "mdb_zfs_rrwlock_t", addr, 3543 0) == -1) 3544 return (DCMD_ERR); 3545 3546 if (rrw.rr_writer != 0) { 3547 mdb_printf("write lock held by thread %lx\n", rrw.rr_writer); 3548 return (DCMD_OK); 3549 } 3550 3551 if (rrw.rr_writer_wanted) { 3552 mdb_printf("writer wanted\n"); 3553 } 3554 3555 mdb_printf("anonymous references:\n"); 3556 (void) mdb_call_dcmd("refcount", addr + 3557 mdb_ctf_offsetof_by_name(ZFS_STRUCT "rrwlock", "rr_anon_rcount"), 3558 DCMD_ADDRSPEC, 0, NULL); 3559 3560 mdb_printf("linked references:\n"); 3561 (void) mdb_call_dcmd("refcount", addr + 3562 mdb_ctf_offsetof_by_name(ZFS_STRUCT "rrwlock", "rr_linked_rcount"), 3563 DCMD_ADDRSPEC, 0, NULL); 3564 3565 /* 3566 * XXX This should find references from 3567 * "::walk thread | ::tsd -v <rrw_key>", but there is no support 3568 * for programmatic consumption of dcmds, so this would be 3569 * difficult, potentially requiring reimplementing ::tsd (both 3570 * user and kernel versions) in this MDB module. 3571 */ 3572 3573 return (DCMD_OK); 3574 } 3575 3576 typedef struct mdb_arc_buf_hdr_t { 3577 uint16_t b_psize; 3578 uint16_t b_lsize; 3579 struct { 3580 uint32_t b_bufcnt; 3581 uintptr_t b_state; 3582 } b_l1hdr; 3583 } mdb_arc_buf_hdr_t; 3584 3585 enum arc_cflags { 3586 ARC_CFLAG_VERBOSE = 1 << 0, 3587 ARC_CFLAG_ANON = 1 << 1, 3588 ARC_CFLAG_MRU = 1 << 2, 3589 ARC_CFLAG_MFU = 1 << 3, 3590 ARC_CFLAG_BUFS = 1 << 4, 3591 }; 3592 3593 typedef struct arc_compression_stats_data { 3594 GElf_Sym anon_sym; /* ARC_anon symbol */ 3595 GElf_Sym mru_sym; /* ARC_mru symbol */ 3596 GElf_Sym mrug_sym; /* ARC_mru_ghost symbol */ 3597 GElf_Sym mfu_sym; /* ARC_mfu symbol */ 3598 GElf_Sym mfug_sym; /* ARC_mfu_ghost symbol */ 3599 GElf_Sym l2c_sym; /* ARC_l2c_only symbol */ 3600 uint64_t *anon_c_hist; /* histogram of compressed sizes in anon */ 3601 uint64_t *anon_u_hist; /* histogram of uncompressed sizes in anon */ 3602 uint64_t *anon_bufs; /* histogram of buffer counts in anon state */ 3603 uint64_t *mru_c_hist; /* histogram of compressed sizes in mru */ 3604 uint64_t *mru_u_hist; /* histogram of uncompressed sizes in mru */ 3605 uint64_t *mru_bufs; /* histogram of buffer counts in mru */ 3606 uint64_t *mfu_c_hist; /* histogram of compressed sizes in mfu */ 3607 uint64_t *mfu_u_hist; /* histogram of uncompressed sizes in mfu */ 3608 uint64_t *mfu_bufs; /* histogram of buffer counts in mfu */ 3609 uint64_t *all_c_hist; /* histogram of compressed anon + mru + mfu */ 3610 uint64_t *all_u_hist; /* histogram of uncompressed anon + mru + mfu */ 3611 uint64_t *all_bufs; /* histogram of buffer counts in all states */ 3612 int arc_cflags; /* arc compression flags, specified by user */ 3613 int hist_nbuckets; /* number of buckets in each histogram */ 3614 } arc_compression_stats_data_t; 3615 3616 int 3617 highbit64(uint64_t i) 3618 { 3619 int h = 1; 3620 3621 if (i == 0) 3622 return (0); 3623 if (i & 0xffffffff00000000ULL) { 3624 h += 32; i >>= 32; 3625 } 3626 if (i & 0xffff0000) { 3627 h += 16; i >>= 16; 3628 } 3629 if (i & 0xff00) { 3630 h += 8; i >>= 8; 3631 } 3632 if (i & 0xf0) { 3633 h += 4; i >>= 4; 3634 } 3635 if (i & 0xc) { 3636 h += 2; i >>= 2; 3637 } 3638 if (i & 0x2) { 3639 h += 1; 3640 } 3641 return (h); 3642 } 3643 3644 /* ARGSUSED */ 3645 static int 3646 arc_compression_stats_cb(uintptr_t addr, const void *unknown, void *arg) 3647 { 3648 arc_compression_stats_data_t *data = arg; 3649 mdb_arc_buf_hdr_t hdr; 3650 int cbucket, ubucket, bufcnt; 3651 3652 if (mdb_ctf_vread(&hdr, "arc_buf_hdr_t", "mdb_arc_buf_hdr_t", 3653 addr, 0) == -1) { 3654 return (WALK_ERR); 3655 } 3656 3657 /* 3658 * Headers in the ghost states, or the l2c_only state don't have 3659 * arc buffers linked off of them. Thus, their compressed size 3660 * is meaningless, so we skip these from the stats. 3661 */ 3662 if (hdr.b_l1hdr.b_state == data->mrug_sym.st_value || 3663 hdr.b_l1hdr.b_state == data->mfug_sym.st_value || 3664 hdr.b_l1hdr.b_state == data->l2c_sym.st_value) { 3665 return (WALK_NEXT); 3666 } 3667 3668 /* 3669 * The physical size (compressed) and logical size 3670 * (uncompressed) are in units of SPA_MINBLOCKSIZE. By default, 3671 * we use the log2 of this value (rounded down to the nearest 3672 * integer) to determine the bucket to assign this header to. 3673 * Thus, the histogram is logarithmic with respect to the size 3674 * of the header. For example, the following is a mapping of the 3675 * bucket numbers and the range of header sizes they correspond to: 3676 * 3677 * 0: 0 byte headers 3678 * 1: 512 byte headers 3679 * 2: [1024 - 2048) byte headers 3680 * 3: [2048 - 4096) byte headers 3681 * 4: [4096 - 8192) byte headers 3682 * 5: [8192 - 16394) byte headers 3683 * 6: [16384 - 32768) byte headers 3684 * 7: [32768 - 65536) byte headers 3685 * 8: [65536 - 131072) byte headers 3686 * 9: 131072 byte headers 3687 * 3688 * If the ARC_CFLAG_VERBOSE flag was specified, we use the 3689 * physical and logical sizes directly. Thus, the histogram will 3690 * no longer be logarithmic; instead it will be linear with 3691 * respect to the size of the header. The following is a mapping 3692 * of the first many bucket numbers and the header size they 3693 * correspond to: 3694 * 3695 * 0: 0 byte headers 3696 * 1: 512 byte headers 3697 * 2: 1024 byte headers 3698 * 3: 1536 byte headers 3699 * 4: 2048 byte headers 3700 * 5: 2560 byte headers 3701 * 6: 3072 byte headers 3702 * 3703 * And so on. Keep in mind that a range of sizes isn't used in 3704 * the case of linear scale because the headers can only 3705 * increment or decrement in sizes of 512 bytes. So, it's not 3706 * possible for a header to be sized in between whats listed 3707 * above. 3708 * 3709 * Also, the above mapping values were calculated assuming a 3710 * SPA_MINBLOCKSHIFT of 512 bytes and a SPA_MAXBLOCKSIZE of 128K. 3711 */ 3712 3713 if (data->arc_cflags & ARC_CFLAG_VERBOSE) { 3714 cbucket = hdr.b_psize; 3715 ubucket = hdr.b_lsize; 3716 } else { 3717 cbucket = highbit64(hdr.b_psize); 3718 ubucket = highbit64(hdr.b_lsize); 3719 } 3720 3721 bufcnt = hdr.b_l1hdr.b_bufcnt; 3722 if (bufcnt >= data->hist_nbuckets) 3723 bufcnt = data->hist_nbuckets - 1; 3724 3725 /* Ensure we stay within the bounds of the histogram array */ 3726 ASSERT3U(cbucket, <, data->hist_nbuckets); 3727 ASSERT3U(ubucket, <, data->hist_nbuckets); 3728 3729 if (hdr.b_l1hdr.b_state == data->anon_sym.st_value) { 3730 data->anon_c_hist[cbucket]++; 3731 data->anon_u_hist[ubucket]++; 3732 data->anon_bufs[bufcnt]++; 3733 } else if (hdr.b_l1hdr.b_state == data->mru_sym.st_value) { 3734 data->mru_c_hist[cbucket]++; 3735 data->mru_u_hist[ubucket]++; 3736 data->mru_bufs[bufcnt]++; 3737 } else if (hdr.b_l1hdr.b_state == data->mfu_sym.st_value) { 3738 data->mfu_c_hist[cbucket]++; 3739 data->mfu_u_hist[ubucket]++; 3740 data->mfu_bufs[bufcnt]++; 3741 } 3742 3743 data->all_c_hist[cbucket]++; 3744 data->all_u_hist[ubucket]++; 3745 data->all_bufs[bufcnt]++; 3746 3747 return (WALK_NEXT); 3748 } 3749 3750 /* ARGSUSED */ 3751 static int 3752 arc_compression_stats(uintptr_t addr, uint_t flags, int argc, 3753 const mdb_arg_t *argv) 3754 { 3755 arc_compression_stats_data_t data = { 0 }; 3756 unsigned int max_shifted = SPA_MAXBLOCKSIZE >> SPA_MINBLOCKSHIFT; 3757 unsigned int hist_size; 3758 char range[32]; 3759 int rc = DCMD_OK; 3760 3761 if (mdb_getopts(argc, argv, 3762 'v', MDB_OPT_SETBITS, ARC_CFLAG_VERBOSE, &data.arc_cflags, 3763 'a', MDB_OPT_SETBITS, ARC_CFLAG_ANON, &data.arc_cflags, 3764 'b', MDB_OPT_SETBITS, ARC_CFLAG_BUFS, &data.arc_cflags, 3765 'r', MDB_OPT_SETBITS, ARC_CFLAG_MRU, &data.arc_cflags, 3766 'f', MDB_OPT_SETBITS, ARC_CFLAG_MFU, &data.arc_cflags) != argc) 3767 return (DCMD_USAGE); 3768 3769 if (mdb_lookup_by_obj(ZFS_OBJ_NAME, "ARC_anon", &data.anon_sym) || 3770 mdb_lookup_by_obj(ZFS_OBJ_NAME, "ARC_mru", &data.mru_sym) || 3771 mdb_lookup_by_obj(ZFS_OBJ_NAME, "ARC_mru_ghost", &data.mrug_sym) || 3772 mdb_lookup_by_obj(ZFS_OBJ_NAME, "ARC_mfu", &data.mfu_sym) || 3773 mdb_lookup_by_obj(ZFS_OBJ_NAME, "ARC_mfu_ghost", &data.mfug_sym) || 3774 mdb_lookup_by_obj(ZFS_OBJ_NAME, "ARC_l2c_only", &data.l2c_sym)) { 3775 mdb_warn("can't find arc state symbol"); 3776 return (DCMD_ERR); 3777 } 3778 3779 /* 3780 * Determine the maximum expected size for any header, and use 3781 * this to determine the number of buckets needed for each 3782 * histogram. If ARC_CFLAG_VERBOSE is specified, this value is 3783 * used directly; otherwise the log2 of the maximum size is 3784 * used. Thus, if using a log2 scale there's a maximum of 10 3785 * possible buckets, while the linear scale (when using 3786 * ARC_CFLAG_VERBOSE) has a maximum of 257 buckets. 3787 */ 3788 if (data.arc_cflags & ARC_CFLAG_VERBOSE) 3789 data.hist_nbuckets = max_shifted + 1; 3790 else 3791 data.hist_nbuckets = highbit64(max_shifted) + 1; 3792 3793 hist_size = sizeof (uint64_t) * data.hist_nbuckets; 3794 3795 data.anon_c_hist = mdb_zalloc(hist_size, UM_SLEEP); 3796 data.anon_u_hist = mdb_zalloc(hist_size, UM_SLEEP); 3797 data.anon_bufs = mdb_zalloc(hist_size, UM_SLEEP); 3798 3799 data.mru_c_hist = mdb_zalloc(hist_size, UM_SLEEP); 3800 data.mru_u_hist = mdb_zalloc(hist_size, UM_SLEEP); 3801 data.mru_bufs = mdb_zalloc(hist_size, UM_SLEEP); 3802 3803 data.mfu_c_hist = mdb_zalloc(hist_size, UM_SLEEP); 3804 data.mfu_u_hist = mdb_zalloc(hist_size, UM_SLEEP); 3805 data.mfu_bufs = mdb_zalloc(hist_size, UM_SLEEP); 3806 3807 data.all_c_hist = mdb_zalloc(hist_size, UM_SLEEP); 3808 data.all_u_hist = mdb_zalloc(hist_size, UM_SLEEP); 3809 data.all_bufs = mdb_zalloc(hist_size, UM_SLEEP); 3810 3811 if (mdb_walk("arc_buf_hdr_t_full", arc_compression_stats_cb, 3812 &data) != 0) { 3813 mdb_warn("can't walk arc_buf_hdr's"); 3814 rc = DCMD_ERR; 3815 goto out; 3816 } 3817 3818 if (data.arc_cflags & ARC_CFLAG_VERBOSE) { 3819 rc = mdb_snprintf(range, sizeof (range), 3820 "[n*%llu, (n+1)*%llu)", SPA_MINBLOCKSIZE, 3821 SPA_MINBLOCKSIZE); 3822 } else { 3823 rc = mdb_snprintf(range, sizeof (range), 3824 "[2^(n-1)*%llu, 2^n*%llu)", SPA_MINBLOCKSIZE, 3825 SPA_MINBLOCKSIZE); 3826 } 3827 3828 if (rc < 0) { 3829 /* snprintf failed, abort the dcmd */ 3830 rc = DCMD_ERR; 3831 goto out; 3832 } else { 3833 /* snprintf succeeded above, reset return code */ 3834 rc = DCMD_OK; 3835 } 3836 3837 if (data.arc_cflags & ARC_CFLAG_ANON) { 3838 if (data.arc_cflags & ARC_CFLAG_BUFS) { 3839 mdb_printf("Histogram of the number of anon buffers " 3840 "that are associated with an arc hdr.\n"); 3841 dump_histogram(data.anon_bufs, data.hist_nbuckets, 0); 3842 mdb_printf("\n"); 3843 } 3844 mdb_printf("Histogram of compressed anon buffers.\n" 3845 "Each bucket represents buffers of size: %s.\n", range); 3846 dump_histogram(data.anon_c_hist, data.hist_nbuckets, 0); 3847 mdb_printf("\n"); 3848 3849 mdb_printf("Histogram of uncompressed anon buffers.\n" 3850 "Each bucket represents buffers of size: %s.\n", range); 3851 dump_histogram(data.anon_u_hist, data.hist_nbuckets, 0); 3852 mdb_printf("\n"); 3853 } 3854 3855 if (data.arc_cflags & ARC_CFLAG_MRU) { 3856 if (data.arc_cflags & ARC_CFLAG_BUFS) { 3857 mdb_printf("Histogram of the number of mru buffers " 3858 "that are associated with an arc hdr.\n"); 3859 dump_histogram(data.mru_bufs, data.hist_nbuckets, 0); 3860 mdb_printf("\n"); 3861 } 3862 mdb_printf("Histogram of compressed mru buffers.\n" 3863 "Each bucket represents buffers of size: %s.\n", range); 3864 dump_histogram(data.mru_c_hist, data.hist_nbuckets, 0); 3865 mdb_printf("\n"); 3866 3867 mdb_printf("Histogram of uncompressed mru buffers.\n" 3868 "Each bucket represents buffers of size: %s.\n", range); 3869 dump_histogram(data.mru_u_hist, data.hist_nbuckets, 0); 3870 mdb_printf("\n"); 3871 } 3872 3873 if (data.arc_cflags & ARC_CFLAG_MFU) { 3874 if (data.arc_cflags & ARC_CFLAG_BUFS) { 3875 mdb_printf("Histogram of the number of mfu buffers " 3876 "that are associated with an arc hdr.\n"); 3877 dump_histogram(data.mfu_bufs, data.hist_nbuckets, 0); 3878 mdb_printf("\n"); 3879 } 3880 3881 mdb_printf("Histogram of compressed mfu buffers.\n" 3882 "Each bucket represents buffers of size: %s.\n", range); 3883 dump_histogram(data.mfu_c_hist, data.hist_nbuckets, 0); 3884 mdb_printf("\n"); 3885 3886 mdb_printf("Histogram of uncompressed mfu buffers.\n" 3887 "Each bucket represents buffers of size: %s.\n", range); 3888 dump_histogram(data.mfu_u_hist, data.hist_nbuckets, 0); 3889 mdb_printf("\n"); 3890 } 3891 3892 if (data.arc_cflags & ARC_CFLAG_BUFS) { 3893 mdb_printf("Histogram of all buffers that " 3894 "are associated with an arc hdr.\n"); 3895 dump_histogram(data.all_bufs, data.hist_nbuckets, 0); 3896 mdb_printf("\n"); 3897 } 3898 3899 mdb_printf("Histogram of all compressed buffers.\n" 3900 "Each bucket represents buffers of size: %s.\n", range); 3901 dump_histogram(data.all_c_hist, data.hist_nbuckets, 0); 3902 mdb_printf("\n"); 3903 3904 mdb_printf("Histogram of all uncompressed buffers.\n" 3905 "Each bucket represents buffers of size: %s.\n", range); 3906 dump_histogram(data.all_u_hist, data.hist_nbuckets, 0); 3907 3908 out: 3909 mdb_free(data.anon_c_hist, hist_size); 3910 mdb_free(data.anon_u_hist, hist_size); 3911 mdb_free(data.anon_bufs, hist_size); 3912 3913 mdb_free(data.mru_c_hist, hist_size); 3914 mdb_free(data.mru_u_hist, hist_size); 3915 mdb_free(data.mru_bufs, hist_size); 3916 3917 mdb_free(data.mfu_c_hist, hist_size); 3918 mdb_free(data.mfu_u_hist, hist_size); 3919 mdb_free(data.mfu_bufs, hist_size); 3920 3921 mdb_free(data.all_c_hist, hist_size); 3922 mdb_free(data.all_u_hist, hist_size); 3923 mdb_free(data.all_bufs, hist_size); 3924 3925 return (rc); 3926 } 3927 3928 /* 3929 * MDB module linkage information: 3930 * 3931 * We declare a list of structures describing our dcmds, and a function 3932 * named _mdb_init to return a pointer to our module information. 3933 */ 3934 3935 static const mdb_dcmd_t dcmds[] = { 3936 { "arc", "[-bkmg]", "print ARC variables", arc_print }, 3937 { "blkptr", ":", "print blkptr_t", blkptr }, 3938 { "dva", ":", "print dva_t", dva }, 3939 { "dbuf", ":", "print dmu_buf_impl_t", dbuf }, 3940 { "dbuf_stats", ":", "dbuf stats", dbuf_stats }, 3941 { "dbufs", 3942 "\t[-O objset_t*] [-n objset_name | \"mos\"] " 3943 "[-o object | \"mdn\"] \n" 3944 "\t[-l level] [-b blkid | \"bonus\"]", 3945 "find dmu_buf_impl_t's that match specified criteria", dbufs }, 3946 { "abuf_find", "dva_word[0] dva_word[1]", 3947 "find arc_buf_hdr_t of a specified DVA", 3948 abuf_find }, 3949 { "spa", "?[-cevmMh]\n" 3950 "\t-c display spa config\n" 3951 "\t-e display vdev statistics\n" 3952 "\t-v display vdev information\n" 3953 "\t-m display metaslab statistics\n" 3954 "\t-M display metaslab group statistics\n" 3955 "\t-h display histogram (requires -m or -M)\n", 3956 "spa_t summary", spa_print }, 3957 { "spa_config", ":", "print spa_t configuration", spa_print_config }, 3958 { "spa_space", ":[-b]", "print spa_t on-disk space usage", spa_space }, 3959 { "spa_vdevs", ":[-emMh]\n" 3960 "\t-e display vdev statistics\n" 3961 "\t-m dispaly metaslab statistics\n" 3962 "\t-M display metaslab group statistic\n" 3963 "\t-h display histogram (requires -m or -M)\n", 3964 "given a spa_t, print vdev summary", spa_vdevs }, 3965 { "vdev", ":[-remMh]\n" 3966 "\t-r display recursively\n" 3967 "\t-e display statistics\n" 3968 "\t-m display metaslab statistics (top level vdev only)\n" 3969 "\t-M display metaslab group statistics (top level vdev only)\n" 3970 "\t-h display histogram (requires -m or -M)\n", 3971 "vdev_t summary", vdev_print }, 3972 { "zio", ":[-cpr]\n" 3973 "\t-c display children\n" 3974 "\t-p display parents\n" 3975 "\t-r display recursively", 3976 "zio_t summary", zio_print }, 3977 { "zio_state", "?", "print out all zio_t structures on system or " 3978 "for a particular pool", zio_state }, 3979 { "zfs_blkstats", ":[-v]", 3980 "given a spa_t, print block type stats from last scrub", 3981 zfs_blkstats }, 3982 { "zfs_params", "", "print zfs tunable parameters", zfs_params }, 3983 { "refcount", ":[-r]\n" 3984 "\t-r display recently removed references", 3985 "print refcount_t holders", refcount }, 3986 { "zap_leaf", "", "print zap_leaf_phys_t", zap_leaf }, 3987 { "zfs_aces", ":[-v]", "print all ACEs from a zfs_acl_t", 3988 zfs_acl_dump }, 3989 { "zfs_ace", ":[-v]", "print zfs_ace", zfs_ace_print }, 3990 { "zfs_ace0", ":[-v]", "print zfs_ace0", zfs_ace0_print }, 3991 { "sa_attr_table", ":", "print SA attribute table from sa_os_t", 3992 sa_attr_table}, 3993 { "sa_attr", ": attr_id", 3994 "print SA attribute address when given sa_handle_t", sa_attr_print}, 3995 { "zfs_dbgmsg", ":[-va]", 3996 "print zfs debug log", dbgmsg}, 3997 { "rrwlock", ":", 3998 "print rrwlock_t, including readers", rrwlock}, 3999 { "metaslab_weight", "weight", 4000 "print metaslab weight", metaslab_weight}, 4001 { "metaslab_trace", ":", 4002 "print metaslab allocation trace records", metaslab_trace}, 4003 { "arc_compression_stats", ":[-vabrf]\n" 4004 "\t-v verbose, display a linearly scaled histogram\n" 4005 "\t-a display ARC_anon state statistics individually\n" 4006 "\t-r display ARC_mru state statistics individually\n" 4007 "\t-f display ARC_mfu state statistics individually\n" 4008 "\t-b display histogram of buffer counts\n", 4009 "print a histogram of compressed arc buffer sizes", 4010 arc_compression_stats}, 4011 { NULL } 4012 }; 4013 4014 static const mdb_walker_t walkers[] = { 4015 { "zms_freelist", "walk ZFS metaslab freelist", 4016 freelist_walk_init, freelist_walk_step, NULL }, 4017 { "txg_list", "given any txg_list_t *, walk all entries in all txgs", 4018 txg_list_walk_init, txg_list_walk_step, NULL }, 4019 { "txg_list0", "given any txg_list_t *, walk all entries in txg 0", 4020 txg_list0_walk_init, txg_list_walk_step, NULL }, 4021 { "txg_list1", "given any txg_list_t *, walk all entries in txg 1", 4022 txg_list1_walk_init, txg_list_walk_step, NULL }, 4023 { "txg_list2", "given any txg_list_t *, walk all entries in txg 2", 4024 txg_list2_walk_init, txg_list_walk_step, NULL }, 4025 { "txg_list3", "given any txg_list_t *, walk all entries in txg 3", 4026 txg_list3_walk_init, txg_list_walk_step, NULL }, 4027 { "zio", "walk all zio structures, optionally for a particular spa_t", 4028 zio_walk_init, zio_walk_step, NULL }, 4029 { "zio_root", 4030 "walk all root zio_t structures, optionally for a particular spa_t", 4031 zio_walk_init, zio_walk_root_step, NULL }, 4032 { "spa", "walk all spa_t entries in the namespace", 4033 spa_walk_init, spa_walk_step, NULL }, 4034 { "metaslab", "given a spa_t *, walk all metaslab_t structures", 4035 metaslab_walk_init, metaslab_walk_step, NULL }, 4036 { "multilist", "given a multilist_t *, walk all list_t structures", 4037 multilist_walk_init, multilist_walk_step, NULL }, 4038 { "zfs_acl_node", "given a zfs_acl_t, walk all zfs_acl_nodes", 4039 zfs_acl_node_walk_init, zfs_acl_node_walk_step, NULL }, 4040 { "zfs_acl_node_aces", "given a zfs_acl_node_t, walk all ACEs", 4041 zfs_acl_node_aces_walk_init, zfs_aces_walk_step, NULL }, 4042 { "zfs_acl_node_aces0", 4043 "given a zfs_acl_node_t, walk all ACEs as ace_t", 4044 zfs_acl_node_aces0_walk_init, zfs_aces_walk_step, NULL }, 4045 { NULL } 4046 }; 4047 4048 static const mdb_modinfo_t modinfo = { 4049 MDB_API_VERSION, dcmds, walkers 4050 }; 4051 4052 const mdb_modinfo_t * 4053 _mdb_init(void) 4054 { 4055 return (&modinfo); 4056 } 4057