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