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 2024 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 extern int64_t mdb_gethrtime(void); 64 #else 65 #define ZFS_OBJ_NAME "libzpool.so.1" 66 #endif 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 1081 typedef struct dbgmsg_arg { 1082 boolean_t da_verbose; 1083 boolean_t da_verbose_hr; 1084 boolean_t da_address; 1085 } dbgmsg_arg_t; 1086 1087 /* ARGSUSED */ 1088 static int 1089 dbgmsg_cb(uintptr_t addr, const void *unknown, void *arg) 1090 { 1091 static mdb_ctf_id_t id; 1092 static boolean_t gotid; 1093 static ulong_t off; 1094 1095 dbgmsg_arg_t *da = arg; 1096 time_t timestamp; 1097 hrtime_t hrtime; 1098 char buf[1024]; 1099 1100 if (!gotid) { 1101 if (mdb_ctf_lookup_by_name(ZFS_STRUCT "zfs_dbgmsg", &id) == 1102 -1) { 1103 mdb_warn("couldn't find struct zfs_dbgmsg"); 1104 return (WALK_ERR); 1105 } 1106 gotid = TRUE; 1107 if (mdb_ctf_offsetof(id, "zdm_msg", &off) == -1) { 1108 mdb_warn("couldn't find zdm_msg"); 1109 return (WALK_ERR); 1110 } 1111 off /= 8; 1112 } 1113 1114 1115 if (GETMEMBID(addr, &id, zdm_timestamp, timestamp)) { 1116 return (WALK_ERR); 1117 } 1118 1119 if (da->da_verbose_hr) { 1120 if (GETMEMBID(addr, &id, zdm_hrtime, hrtime)) { 1121 return (WALK_ERR); 1122 } 1123 } 1124 1125 if (mdb_readstr(buf, sizeof (buf), addr + off) == -1) { 1126 mdb_warn("failed to read zdm_msg at %p\n", addr + off); 1127 return (DCMD_ERR); 1128 } 1129 1130 if (da->da_address) 1131 mdb_printf("%p ", addr); 1132 if (da->da_verbose) 1133 mdb_printf("%Y ", timestamp); 1134 if (da->da_verbose_hr) 1135 mdb_printf("%016x ", hrtime); 1136 1137 mdb_printf("%s\n", buf); 1138 1139 if (da->da_verbose || da->da_verbose_hr) 1140 (void) mdb_call_dcmd("whatis", addr, DCMD_ADDRSPEC, 0, NULL); 1141 1142 return (WALK_NEXT); 1143 } 1144 1145 /* ARGSUSED */ 1146 static int 1147 dbgmsg(uintptr_t addr, uint_t flags, int argc, const mdb_arg_t *argv) 1148 { 1149 GElf_Sym sym; 1150 dbgmsg_arg_t da = { 0 }; 1151 1152 if (mdb_getopts(argc, argv, 1153 'v', MDB_OPT_SETBITS, B_TRUE, &da.da_verbose, 1154 'r', MDB_OPT_SETBITS, B_TRUE, &da.da_verbose_hr, 1155 'a', MDB_OPT_SETBITS, B_TRUE, &da.da_address, 1156 NULL) != argc) 1157 return (DCMD_USAGE); 1158 1159 if (mdb_lookup_by_obj(ZFS_OBJ_NAME, "zfs_dbgmsgs", &sym)) { 1160 mdb_warn("can't find zfs_dbgmsgs"); 1161 return (DCMD_ERR); 1162 } 1163 1164 if (mdb_pwalk("list", dbgmsg_cb, &da, sym.st_value) != 0) { 1165 mdb_warn("can't walk zfs_dbgmsgs"); 1166 return (DCMD_ERR); 1167 } 1168 1169 return (DCMD_OK); 1170 } 1171 1172 /*ARGSUSED*/ 1173 static int 1174 arc_print(uintptr_t addr, uint_t flags, int argc, const mdb_arg_t *argv) 1175 { 1176 kstat_named_t *stats; 1177 GElf_Sym sym; 1178 int nstats, i; 1179 uint_t opt_a = FALSE; 1180 uint_t opt_b = FALSE; 1181 uint_t shift = 0; 1182 const char *suffix; 1183 1184 static const char *bytestats[] = { 1185 "p", "c", "c_min", "c_max", "size", "duplicate_buffers_size", 1186 "arc_meta_used", "arc_meta_limit", "arc_meta_max", 1187 "arc_meta_min", "hdr_size", "data_size", "metadata_size", 1188 "other_size", "anon_size", "anon_evictable_data", 1189 "anon_evictable_metadata", "mru_size", "mru_evictable_data", 1190 "mru_evictable_metadata", "mru_ghost_size", 1191 "mru_ghost_evictable_data", "mru_ghost_evictable_metadata", 1192 "mfu_size", "mfu_evictable_data", "mfu_evictable_metadata", 1193 "mfu_ghost_size", "mfu_ghost_evictable_data", 1194 "mfu_ghost_evictable_metadata", "evict_l2_cached", 1195 "evict_l2_eligible", "evict_l2_ineligible", "l2_read_bytes", 1196 "l2_write_bytes", "l2_size", "l2_asize", "l2_hdr_size", 1197 "compressed_size", "uncompressed_size", "overhead_size", 1198 NULL 1199 }; 1200 1201 static const char *extras[] = { 1202 "arc_no_grow", "arc_tempreserve", 1203 NULL 1204 }; 1205 1206 if (mdb_lookup_by_obj(ZFS_OBJ_NAME, "arc_stats", &sym) == -1) { 1207 mdb_warn("failed to find 'arc_stats'"); 1208 return (DCMD_ERR); 1209 } 1210 1211 stats = mdb_zalloc(sym.st_size, UM_SLEEP | UM_GC); 1212 1213 if (mdb_vread(stats, sym.st_size, sym.st_value) == -1) { 1214 mdb_warn("couldn't read 'arc_stats' at %p", sym.st_value); 1215 return (DCMD_ERR); 1216 } 1217 1218 nstats = sym.st_size / sizeof (kstat_named_t); 1219 1220 /* NB: -a / opt_a are ignored for backwards compatability */ 1221 if (mdb_getopts(argc, argv, 1222 'a', MDB_OPT_SETBITS, TRUE, &opt_a, 1223 'b', MDB_OPT_SETBITS, TRUE, &opt_b, 1224 'k', MDB_OPT_SETBITS, 10, &shift, 1225 'm', MDB_OPT_SETBITS, 20, &shift, 1226 'g', MDB_OPT_SETBITS, 30, &shift, 1227 NULL) != argc) 1228 return (DCMD_USAGE); 1229 1230 if (!opt_b && !shift) 1231 shift = 20; 1232 1233 switch (shift) { 1234 case 0: 1235 suffix = "B"; 1236 break; 1237 case 10: 1238 suffix = "KB"; 1239 break; 1240 case 20: 1241 suffix = "MB"; 1242 break; 1243 case 30: 1244 suffix = "GB"; 1245 break; 1246 default: 1247 suffix = "XX"; 1248 } 1249 1250 for (i = 0; i < nstats; i++) { 1251 int j; 1252 boolean_t bytes = B_FALSE; 1253 1254 for (j = 0; bytestats[j]; j++) { 1255 if (strcmp(stats[i].name, bytestats[j]) == 0) { 1256 bytes = B_TRUE; 1257 break; 1258 } 1259 } 1260 1261 if (bytes) { 1262 mdb_printf("%-25s = %9llu %s\n", stats[i].name, 1263 stats[i].value.ui64 >> shift, suffix); 1264 } else { 1265 mdb_printf("%-25s = %9llu\n", stats[i].name, 1266 stats[i].value.ui64); 1267 } 1268 } 1269 1270 for (i = 0; extras[i]; i++) { 1271 uint64_t buf; 1272 1273 if (mdb_lookup_by_obj(ZFS_OBJ_NAME, extras[i], &sym) == -1) { 1274 mdb_warn("failed to find '%s'", extras[i]); 1275 return (DCMD_ERR); 1276 } 1277 1278 if (sym.st_size != sizeof (uint64_t) && 1279 sym.st_size != sizeof (uint32_t)) { 1280 mdb_warn("expected scalar for variable '%s'\n", 1281 extras[i]); 1282 return (DCMD_ERR); 1283 } 1284 1285 if (mdb_vread(&buf, sym.st_size, sym.st_value) == -1) { 1286 mdb_warn("couldn't read '%s'", extras[i]); 1287 return (DCMD_ERR); 1288 } 1289 1290 mdb_printf("%-25s = ", extras[i]); 1291 1292 /* NB: all the 64-bit extras happen to be byte counts */ 1293 if (sym.st_size == sizeof (uint64_t)) 1294 mdb_printf("%9llu %s\n", buf >> shift, suffix); 1295 1296 if (sym.st_size == sizeof (uint32_t)) 1297 mdb_printf("%9d\n", *((uint32_t *)&buf)); 1298 } 1299 return (DCMD_OK); 1300 } 1301 1302 typedef struct mdb_spa_print { 1303 pool_state_t spa_state; 1304 char spa_name[ZFS_MAX_DATASET_NAME_LEN]; 1305 uintptr_t spa_normal_class; 1306 } mdb_spa_print_t; 1307 1308 1309 const char histo_stars[] = "****************************************"; 1310 const int histo_width = sizeof (histo_stars) - 1; 1311 1312 static void 1313 dump_histogram(const uint64_t *histo, int size, int offset) 1314 { 1315 int i; 1316 int minidx = size - 1; 1317 int maxidx = 0; 1318 uint64_t max = 0; 1319 1320 for (i = 0; i < size; i++) { 1321 if (histo[i] > max) 1322 max = histo[i]; 1323 if (histo[i] > 0 && i > maxidx) 1324 maxidx = i; 1325 if (histo[i] > 0 && i < minidx) 1326 minidx = i; 1327 } 1328 1329 if (max < histo_width) 1330 max = histo_width; 1331 1332 for (i = minidx; i <= maxidx; i++) { 1333 mdb_printf("%3u: %6llu %s\n", 1334 i + offset, (u_longlong_t)histo[i], 1335 &histo_stars[(max - histo[i]) * histo_width / max]); 1336 } 1337 } 1338 1339 typedef struct mdb_metaslab_class { 1340 uint64_t mc_histogram[RANGE_TREE_HISTOGRAM_SIZE]; 1341 } mdb_metaslab_class_t; 1342 1343 /* 1344 * spa_class_histogram(uintptr_t class_addr) 1345 * 1346 * Prints free space histogram for a device class 1347 * 1348 * Returns DCMD_OK, or DCMD_ERR. 1349 */ 1350 static int 1351 spa_class_histogram(uintptr_t class_addr) 1352 { 1353 mdb_metaslab_class_t mc; 1354 if (mdb_ctf_vread(&mc, "metaslab_class_t", 1355 "mdb_metaslab_class_t", class_addr, 0) == -1) 1356 return (DCMD_ERR); 1357 1358 mdb_inc_indent(4); 1359 dump_histogram(mc.mc_histogram, RANGE_TREE_HISTOGRAM_SIZE, 0); 1360 mdb_dec_indent(4); 1361 return (DCMD_OK); 1362 } 1363 1364 /* 1365 * ::spa 1366 * 1367 * -c Print configuration information as well 1368 * -v Print vdev state 1369 * -e Print vdev error stats 1370 * -m Print vdev metaslab info 1371 * -M print vdev metaslab group info 1372 * -h Print histogram info (must be combined with -m or -M) 1373 * 1374 * Print a summarized spa_t. When given no arguments, prints out a table of all 1375 * active pools on the system. 1376 */ 1377 /* ARGSUSED */ 1378 static int 1379 spa_print(uintptr_t addr, uint_t flags, int argc, const mdb_arg_t *argv) 1380 { 1381 const char *statetab[] = { "ACTIVE", "EXPORTED", "DESTROYED", 1382 "SPARE", "L2CACHE", "UNINIT", "UNAVAIL", "POTENTIAL" }; 1383 const char *state; 1384 int spa_flags = 0; 1385 1386 if (mdb_getopts(argc, argv, 1387 'c', MDB_OPT_SETBITS, SPA_FLAG_CONFIG, &spa_flags, 1388 'v', MDB_OPT_SETBITS, SPA_FLAG_VDEVS, &spa_flags, 1389 'e', MDB_OPT_SETBITS, SPA_FLAG_ERRORS, &spa_flags, 1390 'M', MDB_OPT_SETBITS, SPA_FLAG_METASLAB_GROUPS, &spa_flags, 1391 'm', MDB_OPT_SETBITS, SPA_FLAG_METASLABS, &spa_flags, 1392 'h', MDB_OPT_SETBITS, SPA_FLAG_HISTOGRAMS, &spa_flags, 1393 NULL) != argc) 1394 return (DCMD_USAGE); 1395 1396 if (!(flags & DCMD_ADDRSPEC)) { 1397 if (mdb_walk_dcmd("spa", "spa", argc, argv) == -1) { 1398 mdb_warn("can't walk spa"); 1399 return (DCMD_ERR); 1400 } 1401 1402 return (DCMD_OK); 1403 } 1404 1405 if (flags & DCMD_PIPE_OUT) { 1406 mdb_printf("%#lr\n", addr); 1407 return (DCMD_OK); 1408 } 1409 1410 if (DCMD_HDRSPEC(flags)) 1411 mdb_printf("%<u>%-?s %9s %-*s%</u>\n", "ADDR", "STATE", 1412 sizeof (uintptr_t) == 4 ? 60 : 52, "NAME"); 1413 1414 mdb_spa_print_t spa; 1415 if (mdb_ctf_vread(&spa, "spa_t", "mdb_spa_print_t", addr, 0) == -1) 1416 return (DCMD_ERR); 1417 1418 if (spa.spa_state < 0 || spa.spa_state > POOL_STATE_UNAVAIL) 1419 state = "UNKNOWN"; 1420 else 1421 state = statetab[spa.spa_state]; 1422 1423 mdb_printf("%0?p %9s %s\n", addr, state, spa.spa_name); 1424 if (spa_flags & SPA_FLAG_HISTOGRAMS) 1425 spa_class_histogram(spa.spa_normal_class); 1426 1427 if (spa_flags & SPA_FLAG_CONFIG) { 1428 mdb_printf("\n"); 1429 mdb_inc_indent(4); 1430 if (mdb_call_dcmd("spa_config", addr, flags, 0, 1431 NULL) != DCMD_OK) 1432 return (DCMD_ERR); 1433 mdb_dec_indent(4); 1434 } 1435 1436 if (spa_flags & SPA_FLAG_ALL_VDEV) { 1437 mdb_arg_t v; 1438 char opts[100] = "-"; 1439 int args = 1440 (spa_flags | SPA_FLAG_VDEVS) == SPA_FLAG_VDEVS ? 0 : 1; 1441 1442 if (spa_flags & SPA_FLAG_ERRORS) 1443 strcat(opts, "e"); 1444 if (spa_flags & SPA_FLAG_METASLABS) 1445 strcat(opts, "m"); 1446 if (spa_flags & SPA_FLAG_METASLAB_GROUPS) 1447 strcat(opts, "M"); 1448 if (spa_flags & SPA_FLAG_HISTOGRAMS) 1449 strcat(opts, "h"); 1450 1451 v.a_type = MDB_TYPE_STRING; 1452 v.a_un.a_str = opts; 1453 1454 mdb_printf("\n"); 1455 mdb_inc_indent(4); 1456 if (mdb_call_dcmd("spa_vdevs", addr, flags, args, 1457 &v) != DCMD_OK) 1458 return (DCMD_ERR); 1459 mdb_dec_indent(4); 1460 } 1461 1462 return (DCMD_OK); 1463 } 1464 1465 typedef struct mdb_spa_config_spa { 1466 uintptr_t spa_config; 1467 } mdb_spa_config_spa_t; 1468 1469 /* 1470 * ::spa_config 1471 * 1472 * Given a spa_t, print the configuration information stored in spa_config. 1473 * Since it's just an nvlist, format it as an indented list of name=value pairs. 1474 * We simply read the value of spa_config and pass off to ::nvlist. 1475 */ 1476 /* ARGSUSED */ 1477 static int 1478 spa_print_config(uintptr_t addr, uint_t flags, int argc, const mdb_arg_t *argv) 1479 { 1480 mdb_spa_config_spa_t spa; 1481 1482 if (argc != 0 || !(flags & DCMD_ADDRSPEC)) 1483 return (DCMD_USAGE); 1484 1485 if (mdb_ctf_vread(&spa, ZFS_STRUCT "spa", "mdb_spa_config_spa_t", 1486 addr, 0) == -1) 1487 return (DCMD_ERR); 1488 1489 if (spa.spa_config == 0) { 1490 mdb_printf("(none)\n"); 1491 return (DCMD_OK); 1492 } 1493 1494 return (mdb_call_dcmd("nvlist", spa.spa_config, flags, 1495 0, NULL)); 1496 } 1497 1498 typedef struct mdb_range_tree { 1499 struct { 1500 uint64_t bt_num_elems; 1501 uint64_t bt_num_nodes; 1502 } rt_root; 1503 uint64_t rt_space; 1504 range_seg_type_t rt_type; 1505 uint8_t rt_shift; 1506 uint64_t rt_start; 1507 } mdb_range_tree_t; 1508 1509 typedef struct mdb_metaslab_group { 1510 uint64_t mg_fragmentation; 1511 uint64_t mg_histogram[RANGE_TREE_HISTOGRAM_SIZE]; 1512 uintptr_t mg_vd; 1513 } mdb_metaslab_group_t; 1514 1515 typedef struct mdb_metaslab { 1516 uint64_t ms_id; 1517 uint64_t ms_start; 1518 uint64_t ms_size; 1519 int64_t ms_deferspace; 1520 uint64_t ms_fragmentation; 1521 uint64_t ms_weight; 1522 uintptr_t ms_allocating[TXG_SIZE]; 1523 uintptr_t ms_checkpointing; 1524 uintptr_t ms_freeing; 1525 uintptr_t ms_freed; 1526 uintptr_t ms_allocatable; 1527 uintptr_t ms_unflushed_frees; 1528 uintptr_t ms_unflushed_allocs; 1529 uintptr_t ms_sm; 1530 } mdb_metaslab_t; 1531 1532 typedef struct mdb_space_map_phys_t { 1533 int64_t smp_alloc; 1534 uint64_t smp_histogram[SPACE_MAP_HISTOGRAM_SIZE]; 1535 } mdb_space_map_phys_t; 1536 1537 typedef struct mdb_space_map { 1538 uint64_t sm_size; 1539 uint8_t sm_shift; 1540 uintptr_t sm_phys; 1541 } mdb_space_map_t; 1542 1543 typedef struct mdb_vdev { 1544 uint64_t vdev_id; 1545 uint64_t vdev_state; 1546 uintptr_t vdev_ops; 1547 struct { 1548 uint64_t vs_aux; 1549 uint64_t vs_ops[VS_ZIO_TYPES]; 1550 uint64_t vs_bytes[VS_ZIO_TYPES]; 1551 uint64_t vs_read_errors; 1552 uint64_t vs_write_errors; 1553 uint64_t vs_checksum_errors; 1554 } vdev_stat; 1555 uintptr_t vdev_child; 1556 uint64_t vdev_children; 1557 uint64_t vdev_ms_count; 1558 uintptr_t vdev_mg; 1559 uintptr_t vdev_ms; 1560 uintptr_t vdev_path; 1561 } mdb_vdev_t; 1562 1563 typedef struct mdb_vdev_ops { 1564 char vdev_op_type[16]; 1565 } mdb_vdev_ops_t; 1566 1567 static int 1568 metaslab_stats(mdb_vdev_t *vd, int spa_flags) 1569 { 1570 mdb_inc_indent(4); 1571 mdb_printf("%<u>%-?s %6s %20s %10s %10s %10s%</u>\n", "ADDR", "ID", 1572 "OFFSET", "FREE", "FRAG", "UCMU"); 1573 1574 uintptr_t *vdev_ms = mdb_alloc(vd->vdev_ms_count * sizeof (vdev_ms), 1575 UM_SLEEP | UM_GC); 1576 if (mdb_vread(vdev_ms, vd->vdev_ms_count * sizeof (uintptr_t), 1577 vd->vdev_ms) == -1) { 1578 mdb_warn("failed to read vdev_ms at %p\n", vd->vdev_ms); 1579 return (DCMD_ERR); 1580 } 1581 1582 for (int m = 0; m < vd->vdev_ms_count; m++) { 1583 mdb_metaslab_t ms; 1584 mdb_space_map_t sm = { 0 }; 1585 mdb_space_map_phys_t smp = { 0 }; 1586 mdb_range_tree_t rt; 1587 uint64_t uallocs, ufrees, raw_free, raw_uchanges_mem; 1588 char free[MDB_NICENUM_BUFLEN]; 1589 char uchanges_mem[MDB_NICENUM_BUFLEN]; 1590 1591 if (mdb_ctf_vread(&ms, "metaslab_t", "mdb_metaslab_t", 1592 vdev_ms[m], 0) == -1) 1593 return (DCMD_ERR); 1594 1595 if (ms.ms_sm != 0 && 1596 mdb_ctf_vread(&sm, "space_map_t", "mdb_space_map_t", 1597 ms.ms_sm, 0) == -1) 1598 return (DCMD_ERR); 1599 1600 if (mdb_ctf_vread(&rt, "range_tree_t", "mdb_range_tree_t", 1601 ms.ms_unflushed_frees, 0) == -1) 1602 return (DCMD_ERR); 1603 ufrees = rt.rt_space; 1604 raw_uchanges_mem = rt.rt_root.bt_num_nodes * BTREE_LEAF_SIZE; 1605 1606 if (mdb_ctf_vread(&rt, "range_tree_t", "mdb_range_tree_t", 1607 ms.ms_unflushed_allocs, 0) == -1) 1608 return (DCMD_ERR); 1609 uallocs = rt.rt_space; 1610 raw_uchanges_mem += rt.rt_root.bt_num_nodes * BTREE_LEAF_SIZE; 1611 mdb_nicenum(raw_uchanges_mem, uchanges_mem); 1612 1613 raw_free = ms.ms_size; 1614 if (ms.ms_sm != 0 && sm.sm_phys != 0) { 1615 (void) mdb_ctf_vread(&smp, "space_map_phys_t", 1616 "mdb_space_map_phys_t", sm.sm_phys, 0); 1617 raw_free -= smp.smp_alloc; 1618 } 1619 raw_free += ufrees - uallocs; 1620 mdb_nicenum(raw_free, free); 1621 1622 mdb_printf("%0?p %6llu %20llx %10s ", vdev_ms[m], ms.ms_id, 1623 ms.ms_start, free); 1624 if (ms.ms_fragmentation == ZFS_FRAG_INVALID) 1625 mdb_printf("%9s ", "-"); 1626 else 1627 mdb_printf("%9llu%% ", ms.ms_fragmentation); 1628 mdb_printf("%10s\n", uchanges_mem); 1629 1630 if ((spa_flags & SPA_FLAG_HISTOGRAMS) && ms.ms_sm != 0 && 1631 sm.sm_phys != 0) { 1632 dump_histogram(smp.smp_histogram, 1633 SPACE_MAP_HISTOGRAM_SIZE, sm.sm_shift); 1634 } 1635 } 1636 mdb_dec_indent(4); 1637 return (DCMD_OK); 1638 } 1639 1640 static int 1641 metaslab_group_stats(mdb_vdev_t *vd, int spa_flags) 1642 { 1643 mdb_metaslab_group_t mg; 1644 if (mdb_ctf_vread(&mg, "metaslab_group_t", "mdb_metaslab_group_t", 1645 vd->vdev_mg, 0) == -1) { 1646 mdb_warn("failed to read vdev_mg at %p\n", vd->vdev_mg); 1647 return (DCMD_ERR); 1648 } 1649 1650 mdb_inc_indent(4); 1651 mdb_printf("%<u>%-?s %7s %9s%</u>\n", "ADDR", "FRAG", "UCMU"); 1652 1653 if (mg.mg_fragmentation == ZFS_FRAG_INVALID) 1654 mdb_printf("%0?p %6s\n", vd->vdev_mg, "-"); 1655 else 1656 mdb_printf("%0?p %6llu%%", vd->vdev_mg, mg.mg_fragmentation); 1657 1658 1659 uintptr_t *vdev_ms = mdb_alloc(vd->vdev_ms_count * sizeof (vdev_ms), 1660 UM_SLEEP | UM_GC); 1661 if (mdb_vread(vdev_ms, vd->vdev_ms_count * sizeof (uintptr_t), 1662 vd->vdev_ms) == -1) { 1663 mdb_warn("failed to read vdev_ms at %p\n", vd->vdev_ms); 1664 return (DCMD_ERR); 1665 } 1666 1667 uint64_t raw_uchanges_mem = 0; 1668 char uchanges_mem[MDB_NICENUM_BUFLEN]; 1669 for (int m = 0; m < vd->vdev_ms_count; m++) { 1670 mdb_metaslab_t ms; 1671 mdb_range_tree_t rt; 1672 1673 if (mdb_ctf_vread(&ms, "metaslab_t", "mdb_metaslab_t", 1674 vdev_ms[m], 0) == -1) 1675 return (DCMD_ERR); 1676 1677 if (mdb_ctf_vread(&rt, "range_tree_t", "mdb_range_tree_t", 1678 ms.ms_unflushed_frees, 0) == -1) 1679 return (DCMD_ERR); 1680 raw_uchanges_mem += rt.rt_root.bt_num_nodes * BTREE_LEAF_SIZE; 1681 1682 if (mdb_ctf_vread(&rt, "range_tree_t", "mdb_range_tree_t", 1683 ms.ms_unflushed_allocs, 0) == -1) 1684 return (DCMD_ERR); 1685 raw_uchanges_mem += rt.rt_root.bt_num_nodes * BTREE_LEAF_SIZE; 1686 } 1687 mdb_nicenum(raw_uchanges_mem, uchanges_mem); 1688 mdb_printf("%10s\n", uchanges_mem); 1689 1690 if (spa_flags & SPA_FLAG_HISTOGRAMS) 1691 dump_histogram(mg.mg_histogram, RANGE_TREE_HISTOGRAM_SIZE, 0); 1692 mdb_dec_indent(4); 1693 return (DCMD_OK); 1694 } 1695 1696 /* 1697 * ::vdev 1698 * 1699 * Print out a summarized vdev_t, in the following form: 1700 * 1701 * ADDR STATE AUX DESC 1702 * fffffffbcde23df0 HEALTHY - /dev/dsk/c0t0d0 1703 * 1704 * If '-r' is specified, recursively visit all children. 1705 * 1706 * With '-e', the statistics associated with the vdev are printed as well. 1707 */ 1708 static int 1709 do_print_vdev(uintptr_t addr, int flags, int depth, boolean_t recursive, 1710 int spa_flags) 1711 { 1712 mdb_vdev_t vd; 1713 if (mdb_ctf_vread(&vd, "vdev_t", "mdb_vdev_t", 1714 (uintptr_t)addr, 0) == -1) 1715 return (DCMD_ERR); 1716 1717 if (flags & DCMD_PIPE_OUT) { 1718 mdb_printf("%#lr\n", addr); 1719 } else { 1720 char desc[MAXNAMELEN]; 1721 if (vd.vdev_path != 0) { 1722 if (mdb_readstr(desc, sizeof (desc), 1723 (uintptr_t)vd.vdev_path) == -1) { 1724 mdb_warn("failed to read vdev_path at %p\n", 1725 vd.vdev_path); 1726 return (DCMD_ERR); 1727 } 1728 } else if (vd.vdev_ops != 0) { 1729 vdev_ops_t ops; 1730 if (mdb_vread(&ops, sizeof (ops), 1731 (uintptr_t)vd.vdev_ops) == -1) { 1732 mdb_warn("failed to read vdev_ops at %p\n", 1733 vd.vdev_ops); 1734 return (DCMD_ERR); 1735 } 1736 (void) strcpy(desc, ops.vdev_op_type); 1737 } else { 1738 (void) strcpy(desc, "<unknown>"); 1739 } 1740 1741 if (depth == 0 && DCMD_HDRSPEC(flags)) 1742 mdb_printf("%<u>%-?s %-9s %-12s %-*s%</u>\n", 1743 "ADDR", "STATE", "AUX", 1744 sizeof (uintptr_t) == 4 ? 43 : 35, 1745 "DESCRIPTION"); 1746 1747 mdb_printf("%0?p ", addr); 1748 1749 const char *state, *aux; 1750 switch (vd.vdev_state) { 1751 case VDEV_STATE_CLOSED: 1752 state = "CLOSED"; 1753 break; 1754 case VDEV_STATE_OFFLINE: 1755 state = "OFFLINE"; 1756 break; 1757 case VDEV_STATE_CANT_OPEN: 1758 state = "CANT_OPEN"; 1759 break; 1760 case VDEV_STATE_DEGRADED: 1761 state = "DEGRADED"; 1762 break; 1763 case VDEV_STATE_HEALTHY: 1764 state = "HEALTHY"; 1765 break; 1766 case VDEV_STATE_REMOVED: 1767 state = "REMOVED"; 1768 break; 1769 case VDEV_STATE_FAULTED: 1770 state = "FAULTED"; 1771 break; 1772 default: 1773 state = "UNKNOWN"; 1774 break; 1775 } 1776 1777 switch (vd.vdev_stat.vs_aux) { 1778 case VDEV_AUX_NONE: 1779 aux = "-"; 1780 break; 1781 case VDEV_AUX_OPEN_FAILED: 1782 aux = "OPEN_FAILED"; 1783 break; 1784 case VDEV_AUX_CORRUPT_DATA: 1785 aux = "CORRUPT_DATA"; 1786 break; 1787 case VDEV_AUX_NO_REPLICAS: 1788 aux = "NO_REPLICAS"; 1789 break; 1790 case VDEV_AUX_BAD_GUID_SUM: 1791 aux = "BAD_GUID_SUM"; 1792 break; 1793 case VDEV_AUX_TOO_SMALL: 1794 aux = "TOO_SMALL"; 1795 break; 1796 case VDEV_AUX_BAD_LABEL: 1797 aux = "BAD_LABEL"; 1798 break; 1799 case VDEV_AUX_VERSION_NEWER: 1800 aux = "VERS_NEWER"; 1801 break; 1802 case VDEV_AUX_VERSION_OLDER: 1803 aux = "VERS_OLDER"; 1804 break; 1805 case VDEV_AUX_UNSUP_FEAT: 1806 aux = "UNSUP_FEAT"; 1807 break; 1808 case VDEV_AUX_SPARED: 1809 aux = "SPARED"; 1810 break; 1811 case VDEV_AUX_ERR_EXCEEDED: 1812 aux = "ERR_EXCEEDED"; 1813 break; 1814 case VDEV_AUX_IO_FAILURE: 1815 aux = "IO_FAILURE"; 1816 break; 1817 case VDEV_AUX_BAD_LOG: 1818 aux = "BAD_LOG"; 1819 break; 1820 case VDEV_AUX_EXTERNAL: 1821 aux = "EXTERNAL"; 1822 break; 1823 case VDEV_AUX_SPLIT_POOL: 1824 aux = "SPLIT_POOL"; 1825 break; 1826 case VDEV_AUX_CHILDREN_OFFLINE: 1827 aux = "CHILDREN_OFFLINE"; 1828 break; 1829 default: 1830 aux = "UNKNOWN"; 1831 break; 1832 } 1833 1834 mdb_printf("%-9s %-12s %*s%s\n", state, aux, depth, "", desc); 1835 1836 if (spa_flags & SPA_FLAG_ERRORS) { 1837 int i; 1838 1839 mdb_inc_indent(4); 1840 mdb_printf("\n"); 1841 mdb_printf("%<u> %12s %12s %12s %12s " 1842 "%12s%</u>\n", "READ", "WRITE", "FREE", "CLAIM", 1843 "IOCTL"); 1844 mdb_printf("OPS "); 1845 for (i = 1; i < VS_ZIO_TYPES; i++) 1846 mdb_printf("%11#llx%s", 1847 vd.vdev_stat.vs_ops[i], 1848 i == VS_ZIO_TYPES - 1 ? "" : " "); 1849 mdb_printf("\n"); 1850 mdb_printf("BYTES "); 1851 for (i = 1; i < VS_ZIO_TYPES; i++) 1852 mdb_printf("%11#llx%s", 1853 vd.vdev_stat.vs_bytes[i], 1854 i == VS_ZIO_TYPES - 1 ? "" : " "); 1855 1856 1857 mdb_printf("\n"); 1858 mdb_printf("EREAD %10#llx\n", 1859 vd.vdev_stat.vs_read_errors); 1860 mdb_printf("EWRITE %10#llx\n", 1861 vd.vdev_stat.vs_write_errors); 1862 mdb_printf("ECKSUM %10#llx\n", 1863 vd.vdev_stat.vs_checksum_errors); 1864 mdb_dec_indent(4); 1865 mdb_printf("\n"); 1866 } 1867 1868 if ((spa_flags & SPA_FLAG_METASLAB_GROUPS) && 1869 vd.vdev_mg != 0) { 1870 metaslab_group_stats(&vd, spa_flags); 1871 } 1872 if ((spa_flags & SPA_FLAG_METASLABS) && vd.vdev_ms != 0) { 1873 metaslab_stats(&vd, spa_flags); 1874 } 1875 } 1876 1877 uint64_t children = vd.vdev_children; 1878 if (children == 0 || !recursive) 1879 return (DCMD_OK); 1880 1881 uintptr_t *child = mdb_alloc(children * sizeof (child), 1882 UM_SLEEP | UM_GC); 1883 if (mdb_vread(child, children * sizeof (void *), vd.vdev_child) == -1) { 1884 mdb_warn("failed to read vdev children at %p", vd.vdev_child); 1885 return (DCMD_ERR); 1886 } 1887 1888 for (uint64_t c = 0; c < children; c++) { 1889 if (do_print_vdev(child[c], flags, depth + 2, recursive, 1890 spa_flags)) { 1891 return (DCMD_ERR); 1892 } 1893 } 1894 1895 return (DCMD_OK); 1896 } 1897 1898 static int 1899 vdev_print(uintptr_t addr, uint_t flags, int argc, const mdb_arg_t *argv) 1900 { 1901 uint64_t depth = 0; 1902 boolean_t recursive = B_FALSE; 1903 int spa_flags = 0; 1904 1905 if (mdb_getopts(argc, argv, 1906 'e', MDB_OPT_SETBITS, SPA_FLAG_ERRORS, &spa_flags, 1907 'm', MDB_OPT_SETBITS, SPA_FLAG_METASLABS, &spa_flags, 1908 'M', MDB_OPT_SETBITS, SPA_FLAG_METASLAB_GROUPS, &spa_flags, 1909 'h', MDB_OPT_SETBITS, SPA_FLAG_HISTOGRAMS, &spa_flags, 1910 'r', MDB_OPT_SETBITS, TRUE, &recursive, 1911 'd', MDB_OPT_UINT64, &depth, NULL) != argc) 1912 return (DCMD_USAGE); 1913 1914 if (!(flags & DCMD_ADDRSPEC)) { 1915 mdb_warn("no vdev_t address given\n"); 1916 return (DCMD_ERR); 1917 } 1918 1919 return (do_print_vdev(addr, flags, (int)depth, recursive, spa_flags)); 1920 } 1921 1922 typedef struct mdb_metaslab_alloc_trace { 1923 uintptr_t mat_mg; 1924 uintptr_t mat_msp; 1925 uint64_t mat_size; 1926 uint64_t mat_weight; 1927 uint64_t mat_offset; 1928 uint32_t mat_dva_id; 1929 int mat_allocator; 1930 } mdb_metaslab_alloc_trace_t; 1931 1932 static void 1933 metaslab_print_weight(uint64_t weight) 1934 { 1935 char buf[100]; 1936 1937 if (WEIGHT_IS_SPACEBASED(weight)) { 1938 mdb_nicenum( 1939 weight & ~(METASLAB_ACTIVE_MASK | METASLAB_WEIGHT_TYPE), 1940 buf); 1941 } else { 1942 char size[MDB_NICENUM_BUFLEN]; 1943 mdb_nicenum(1ULL << WEIGHT_GET_INDEX(weight), size); 1944 (void) mdb_snprintf(buf, sizeof (buf), "%llu x %s", 1945 WEIGHT_GET_COUNT(weight), size); 1946 } 1947 mdb_printf("%11s ", buf); 1948 } 1949 1950 /* ARGSUSED */ 1951 static int 1952 metaslab_weight(uintptr_t addr, uint_t flags, int argc, const mdb_arg_t *argv) 1953 { 1954 uint64_t weight = 0; 1955 char active; 1956 1957 if (argc == 0 && (flags & DCMD_ADDRSPEC)) { 1958 if (mdb_vread(&weight, sizeof (uint64_t), addr) == -1) { 1959 mdb_warn("failed to read weight at %p\n", addr); 1960 return (DCMD_ERR); 1961 } 1962 } else if (argc == 1 && !(flags & DCMD_ADDRSPEC)) { 1963 weight = (argv[0].a_type == MDB_TYPE_IMMEDIATE) ? 1964 argv[0].a_un.a_val : mdb_strtoull(argv[0].a_un.a_str); 1965 } else { 1966 return (DCMD_USAGE); 1967 } 1968 1969 if (DCMD_HDRSPEC(flags)) { 1970 mdb_printf("%<u>%-6s %9s %9s%</u>\n", 1971 "ACTIVE", "ALGORITHM", "WEIGHT"); 1972 } 1973 1974 if (weight & METASLAB_WEIGHT_PRIMARY) 1975 active = 'P'; 1976 else if (weight & METASLAB_WEIGHT_SECONDARY) 1977 active = 'S'; 1978 else 1979 active = '-'; 1980 mdb_printf("%6c %8s ", active, 1981 WEIGHT_IS_SPACEBASED(weight) ? "SPACE" : "SEGMENT"); 1982 metaslab_print_weight(weight); 1983 mdb_printf("\n"); 1984 1985 return (DCMD_OK); 1986 } 1987 1988 /* ARGSUSED */ 1989 static int 1990 metaslab_trace(uintptr_t addr, uint_t flags, int argc, const mdb_arg_t *argv) 1991 { 1992 mdb_metaslab_alloc_trace_t mat; 1993 mdb_metaslab_group_t mg = { 0 }; 1994 char result_type[100]; 1995 1996 if (mdb_ctf_vread(&mat, "metaslab_alloc_trace_t", 1997 "mdb_metaslab_alloc_trace_t", addr, 0) == -1) { 1998 return (DCMD_ERR); 1999 } 2000 2001 if (!(flags & DCMD_PIPE_OUT) && DCMD_HDRSPEC(flags)) { 2002 mdb_printf("%<u>%6s %6s %8s %11s %11s %18s %18s%</u>\n", 2003 "MSID", "DVA", "ASIZE", "ALLOCATOR", "WEIGHT", "RESULT", 2004 "VDEV"); 2005 } 2006 2007 if (mat.mat_msp != 0) { 2008 mdb_metaslab_t ms; 2009 2010 if (mdb_ctf_vread(&ms, "metaslab_t", "mdb_metaslab_t", 2011 mat.mat_msp, 0) == -1) { 2012 return (DCMD_ERR); 2013 } 2014 mdb_printf("%6llu ", ms.ms_id); 2015 } else { 2016 mdb_printf("%6s ", "-"); 2017 } 2018 2019 mdb_printf("%6d %8llx %11llx ", mat.mat_dva_id, mat.mat_size, 2020 mat.mat_allocator); 2021 2022 metaslab_print_weight(mat.mat_weight); 2023 2024 if ((int64_t)mat.mat_offset < 0) { 2025 if (enum_lookup("enum trace_alloc_type", mat.mat_offset, 2026 "TRACE_", sizeof (result_type), result_type) == -1) { 2027 mdb_warn("Could not find enum for trace_alloc_type"); 2028 return (DCMD_ERR); 2029 } 2030 mdb_printf("%18s ", result_type); 2031 } else { 2032 mdb_printf("%<b>%18llx%</b> ", mat.mat_offset); 2033 } 2034 2035 if (mat.mat_mg != 0 && 2036 mdb_ctf_vread(&mg, "metaslab_group_t", "mdb_metaslab_group_t", 2037 mat.mat_mg, 0) == -1) { 2038 return (DCMD_ERR); 2039 } 2040 2041 if (mg.mg_vd != 0) { 2042 mdb_vdev_t vdev; 2043 char desc[MAXNAMELEN]; 2044 2045 if (mdb_ctf_vread(&vdev, "vdev_t", "mdb_vdev_t", 2046 mg.mg_vd, 0) == -1) { 2047 return (DCMD_ERR); 2048 } 2049 2050 if (vdev.vdev_path != 0) { 2051 char path[MAXNAMELEN]; 2052 2053 if (mdb_readstr(path, sizeof (path), 2054 vdev.vdev_path) == -1) { 2055 mdb_warn("failed to read vdev_path at %p\n", 2056 vdev.vdev_path); 2057 return (DCMD_ERR); 2058 } 2059 char *slash; 2060 if ((slash = strrchr(path, '/')) != NULL) { 2061 strcpy(desc, slash + 1); 2062 } else { 2063 strcpy(desc, path); 2064 } 2065 } else if (vdev.vdev_ops != 0) { 2066 mdb_vdev_ops_t ops; 2067 if (mdb_ctf_vread(&ops, "vdev_ops_t", "mdb_vdev_ops_t", 2068 vdev.vdev_ops, 0) == -1) { 2069 mdb_warn("failed to read vdev_ops at %p\n", 2070 vdev.vdev_ops); 2071 return (DCMD_ERR); 2072 } 2073 (void) mdb_snprintf(desc, sizeof (desc), 2074 "%s-%llu", ops.vdev_op_type, vdev.vdev_id); 2075 } else { 2076 (void) strcpy(desc, "<unknown>"); 2077 } 2078 mdb_printf("%18s\n", desc); 2079 } 2080 2081 return (DCMD_OK); 2082 } 2083 2084 typedef struct metaslab_walk_data { 2085 uint64_t mw_numvdevs; 2086 uintptr_t *mw_vdevs; 2087 int mw_curvdev; 2088 uint64_t mw_nummss; 2089 uintptr_t *mw_mss; 2090 int mw_curms; 2091 } metaslab_walk_data_t; 2092 2093 static int 2094 metaslab_walk_step(mdb_walk_state_t *wsp) 2095 { 2096 metaslab_walk_data_t *mw = wsp->walk_data; 2097 metaslab_t ms; 2098 uintptr_t msp; 2099 2100 if (mw->mw_curvdev >= mw->mw_numvdevs) 2101 return (WALK_DONE); 2102 2103 if (mw->mw_mss == NULL) { 2104 uintptr_t mssp; 2105 uintptr_t vdevp; 2106 2107 ASSERT(mw->mw_curms == 0); 2108 ASSERT(mw->mw_nummss == 0); 2109 2110 vdevp = mw->mw_vdevs[mw->mw_curvdev]; 2111 if (GETMEMB(vdevp, "vdev", vdev_ms, mssp) || 2112 GETMEMB(vdevp, "vdev", vdev_ms_count, mw->mw_nummss)) { 2113 return (WALK_ERR); 2114 } 2115 2116 mw->mw_mss = mdb_alloc(mw->mw_nummss * sizeof (void*), 2117 UM_SLEEP | UM_GC); 2118 if (mdb_vread(mw->mw_mss, mw->mw_nummss * sizeof (void*), 2119 mssp) == -1) { 2120 mdb_warn("failed to read vdev_ms at %p", mssp); 2121 return (WALK_ERR); 2122 } 2123 } 2124 2125 if (mw->mw_curms >= mw->mw_nummss) { 2126 mw->mw_mss = NULL; 2127 mw->mw_curms = 0; 2128 mw->mw_nummss = 0; 2129 mw->mw_curvdev++; 2130 return (WALK_NEXT); 2131 } 2132 2133 msp = mw->mw_mss[mw->mw_curms]; 2134 if (mdb_vread(&ms, sizeof (metaslab_t), msp) == -1) { 2135 mdb_warn("failed to read metaslab_t at %p", msp); 2136 return (WALK_ERR); 2137 } 2138 2139 mw->mw_curms++; 2140 2141 return (wsp->walk_callback(msp, &ms, wsp->walk_cbdata)); 2142 } 2143 2144 static int 2145 metaslab_walk_init(mdb_walk_state_t *wsp) 2146 { 2147 metaslab_walk_data_t *mw; 2148 uintptr_t root_vdevp; 2149 uintptr_t childp; 2150 2151 if (wsp->walk_addr == 0) { 2152 mdb_warn("must supply address of spa_t\n"); 2153 return (WALK_ERR); 2154 } 2155 2156 mw = mdb_zalloc(sizeof (metaslab_walk_data_t), UM_SLEEP | UM_GC); 2157 2158 if (GETMEMB(wsp->walk_addr, "spa", spa_root_vdev, root_vdevp) || 2159 GETMEMB(root_vdevp, "vdev", vdev_children, mw->mw_numvdevs) || 2160 GETMEMB(root_vdevp, "vdev", vdev_child, childp)) { 2161 return (DCMD_ERR); 2162 } 2163 2164 mw->mw_vdevs = mdb_alloc(mw->mw_numvdevs * sizeof (void *), 2165 UM_SLEEP | UM_GC); 2166 if (mdb_vread(mw->mw_vdevs, mw->mw_numvdevs * sizeof (void *), 2167 childp) == -1) { 2168 mdb_warn("failed to read root vdev children at %p", childp); 2169 return (DCMD_ERR); 2170 } 2171 2172 wsp->walk_data = mw; 2173 2174 return (WALK_NEXT); 2175 } 2176 2177 typedef struct mdb_spa { 2178 uintptr_t spa_dsl_pool; 2179 uintptr_t spa_root_vdev; 2180 } mdb_spa_t; 2181 2182 typedef struct mdb_dsl_pool { 2183 uintptr_t dp_root_dir; 2184 } mdb_dsl_pool_t; 2185 2186 typedef struct mdb_dsl_dir { 2187 uintptr_t dd_dbuf; 2188 int64_t dd_space_towrite[TXG_SIZE]; 2189 } mdb_dsl_dir_t; 2190 2191 typedef struct mdb_dsl_dir_phys { 2192 uint64_t dd_used_bytes; 2193 uint64_t dd_compressed_bytes; 2194 uint64_t dd_uncompressed_bytes; 2195 } mdb_dsl_dir_phys_t; 2196 2197 typedef struct space_data { 2198 uint64_t ms_allocating[TXG_SIZE]; 2199 uint64_t ms_checkpointing; 2200 uint64_t ms_freeing; 2201 uint64_t ms_freed; 2202 uint64_t ms_unflushed_frees; 2203 uint64_t ms_unflushed_allocs; 2204 uint64_t ms_allocatable; 2205 int64_t ms_deferspace; 2206 uint64_t avail; 2207 } space_data_t; 2208 2209 /* ARGSUSED */ 2210 static int 2211 space_cb(uintptr_t addr, const void *unknown, void *arg) 2212 { 2213 space_data_t *sd = arg; 2214 mdb_metaslab_t ms; 2215 mdb_range_tree_t rt; 2216 mdb_space_map_t sm = { 0 }; 2217 mdb_space_map_phys_t smp = { 0 }; 2218 uint64_t uallocs, ufrees; 2219 int i; 2220 2221 if (mdb_ctf_vread(&ms, "metaslab_t", "mdb_metaslab_t", 2222 addr, 0) == -1) 2223 return (WALK_ERR); 2224 2225 for (i = 0; i < TXG_SIZE; i++) { 2226 if (mdb_ctf_vread(&rt, "range_tree_t", 2227 "mdb_range_tree_t", ms.ms_allocating[i], 0) == -1) 2228 return (WALK_ERR); 2229 sd->ms_allocating[i] += rt.rt_space; 2230 } 2231 2232 if (mdb_ctf_vread(&rt, "range_tree_t", 2233 "mdb_range_tree_t", ms.ms_checkpointing, 0) == -1) 2234 return (WALK_ERR); 2235 sd->ms_checkpointing += rt.rt_space; 2236 2237 if (mdb_ctf_vread(&rt, "range_tree_t", 2238 "mdb_range_tree_t", ms.ms_freeing, 0) == -1) 2239 return (WALK_ERR); 2240 sd->ms_freeing += rt.rt_space; 2241 2242 if (mdb_ctf_vread(&rt, "range_tree_t", 2243 "mdb_range_tree_t", ms.ms_freed, 0) == -1) 2244 return (WALK_ERR); 2245 sd->ms_freed += rt.rt_space; 2246 2247 if (mdb_ctf_vread(&rt, "range_tree_t", 2248 "mdb_range_tree_t", ms.ms_allocatable, 0) == -1) 2249 return (WALK_ERR); 2250 sd->ms_allocatable += rt.rt_space; 2251 2252 if (mdb_ctf_vread(&rt, "range_tree_t", 2253 "mdb_range_tree_t", ms.ms_unflushed_frees, 0) == -1) 2254 return (WALK_ERR); 2255 sd->ms_unflushed_frees += rt.rt_space; 2256 ufrees = rt.rt_space; 2257 2258 if (mdb_ctf_vread(&rt, "range_tree_t", 2259 "mdb_range_tree_t", ms.ms_unflushed_allocs, 0) == -1) 2260 return (WALK_ERR); 2261 sd->ms_unflushed_allocs += rt.rt_space; 2262 uallocs = rt.rt_space; 2263 2264 if (ms.ms_sm != 0 && 2265 mdb_ctf_vread(&sm, "space_map_t", 2266 "mdb_space_map_t", ms.ms_sm, 0) == -1) 2267 return (WALK_ERR); 2268 2269 if (sm.sm_phys != 0) { 2270 (void) mdb_ctf_vread(&smp, "space_map_phys_t", 2271 "mdb_space_map_phys_t", sm.sm_phys, 0); 2272 } 2273 2274 sd->ms_deferspace += ms.ms_deferspace; 2275 sd->avail += sm.sm_size - smp.smp_alloc + ufrees - uallocs; 2276 2277 return (WALK_NEXT); 2278 } 2279 2280 /* 2281 * ::spa_space [-b] 2282 * 2283 * Given a spa_t, print out it's on-disk space usage and in-core 2284 * estimates of future usage. If -b is given, print space in bytes. 2285 * Otherwise print in megabytes. 2286 */ 2287 /* ARGSUSED */ 2288 static int 2289 spa_space(uintptr_t addr, uint_t flags, int argc, const mdb_arg_t *argv) 2290 { 2291 mdb_spa_t spa; 2292 mdb_dsl_pool_t dp; 2293 mdb_dsl_dir_t dd; 2294 mdb_dmu_buf_impl_t db; 2295 mdb_dsl_dir_phys_t dsp; 2296 space_data_t sd; 2297 int shift = 20; 2298 char *suffix = "M"; 2299 int bytes = B_FALSE; 2300 2301 if (mdb_getopts(argc, argv, 'b', MDB_OPT_SETBITS, TRUE, &bytes, NULL) != 2302 argc) 2303 return (DCMD_USAGE); 2304 if (!(flags & DCMD_ADDRSPEC)) 2305 return (DCMD_USAGE); 2306 2307 if (bytes) { 2308 shift = 0; 2309 suffix = ""; 2310 } 2311 2312 if (mdb_ctf_vread(&spa, ZFS_STRUCT "spa", "mdb_spa_t", 2313 addr, 0) == -1 || 2314 mdb_ctf_vread(&dp, ZFS_STRUCT "dsl_pool", "mdb_dsl_pool_t", 2315 spa.spa_dsl_pool, 0) == -1 || 2316 mdb_ctf_vread(&dd, ZFS_STRUCT "dsl_dir", "mdb_dsl_dir_t", 2317 dp.dp_root_dir, 0) == -1 || 2318 mdb_ctf_vread(&db, ZFS_STRUCT "dmu_buf_impl", "mdb_dmu_buf_impl_t", 2319 dd.dd_dbuf, 0) == -1 || 2320 mdb_ctf_vread(&dsp, ZFS_STRUCT "dsl_dir_phys", 2321 "mdb_dsl_dir_phys_t", db.db.db_data, 0) == -1) { 2322 return (DCMD_ERR); 2323 } 2324 2325 mdb_printf("dd_space_towrite = %llu%s %llu%s %llu%s %llu%s\n", 2326 dd.dd_space_towrite[0] >> shift, suffix, 2327 dd.dd_space_towrite[1] >> shift, suffix, 2328 dd.dd_space_towrite[2] >> shift, suffix, 2329 dd.dd_space_towrite[3] >> shift, suffix); 2330 2331 mdb_printf("dd_phys.dd_used_bytes = %llu%s\n", 2332 dsp.dd_used_bytes >> shift, suffix); 2333 mdb_printf("dd_phys.dd_compressed_bytes = %llu%s\n", 2334 dsp.dd_compressed_bytes >> shift, suffix); 2335 mdb_printf("dd_phys.dd_uncompressed_bytes = %llu%s\n", 2336 dsp.dd_uncompressed_bytes >> shift, suffix); 2337 2338 bzero(&sd, sizeof (sd)); 2339 if (mdb_pwalk("metaslab", space_cb, &sd, addr) != 0) { 2340 mdb_warn("can't walk metaslabs"); 2341 return (DCMD_ERR); 2342 } 2343 2344 mdb_printf("ms_allocmap = %llu%s %llu%s %llu%s %llu%s\n", 2345 sd.ms_allocating[0] >> shift, suffix, 2346 sd.ms_allocating[1] >> shift, suffix, 2347 sd.ms_allocating[2] >> shift, suffix, 2348 sd.ms_allocating[3] >> shift, suffix); 2349 mdb_printf("ms_checkpointing = %llu%s\n", 2350 sd.ms_checkpointing >> shift, suffix); 2351 mdb_printf("ms_freeing = %llu%s\n", 2352 sd.ms_freeing >> shift, suffix); 2353 mdb_printf("ms_freed = %llu%s\n", 2354 sd.ms_freed >> shift, suffix); 2355 mdb_printf("ms_unflushed_frees = %llu%s\n", 2356 sd.ms_unflushed_frees >> shift, suffix); 2357 mdb_printf("ms_unflushed_allocs = %llu%s\n", 2358 sd.ms_unflushed_allocs >> shift, suffix); 2359 mdb_printf("ms_allocatable = %llu%s\n", 2360 sd.ms_allocatable >> shift, suffix); 2361 mdb_printf("ms_deferspace = %llu%s\n", 2362 sd.ms_deferspace >> shift, suffix); 2363 mdb_printf("current avail = %llu%s\n", 2364 sd.avail >> shift, suffix); 2365 2366 return (DCMD_OK); 2367 } 2368 2369 typedef struct mdb_spa_aux_vdev { 2370 int sav_count; 2371 uintptr_t sav_vdevs; 2372 } mdb_spa_aux_vdev_t; 2373 2374 typedef struct mdb_spa_vdevs { 2375 uintptr_t spa_root_vdev; 2376 mdb_spa_aux_vdev_t spa_l2cache; 2377 mdb_spa_aux_vdev_t spa_spares; 2378 } mdb_spa_vdevs_t; 2379 2380 static int 2381 spa_print_aux(mdb_spa_aux_vdev_t *sav, uint_t flags, mdb_arg_t *v, 2382 const char *name) 2383 { 2384 uintptr_t *aux; 2385 size_t len; 2386 int ret, i; 2387 2388 /* 2389 * Iterate over aux vdevs and print those out as well. This is a 2390 * little annoying because we don't have a root vdev to pass to ::vdev. 2391 * Instead, we print a single line and then call it for each child 2392 * vdev. 2393 */ 2394 if (sav->sav_count != 0) { 2395 v[1].a_type = MDB_TYPE_STRING; 2396 v[1].a_un.a_str = "-d"; 2397 v[2].a_type = MDB_TYPE_IMMEDIATE; 2398 v[2].a_un.a_val = 2; 2399 2400 len = sav->sav_count * sizeof (uintptr_t); 2401 aux = mdb_alloc(len, UM_SLEEP); 2402 if (mdb_vread(aux, len, sav->sav_vdevs) == -1) { 2403 mdb_free(aux, len); 2404 mdb_warn("failed to read l2cache vdevs at %p", 2405 sav->sav_vdevs); 2406 return (DCMD_ERR); 2407 } 2408 2409 mdb_printf("%-?s %-9s %-12s %s\n", "-", "-", "-", name); 2410 2411 for (i = 0; i < sav->sav_count; i++) { 2412 ret = mdb_call_dcmd("vdev", aux[i], flags, 3, v); 2413 if (ret != DCMD_OK) { 2414 mdb_free(aux, len); 2415 return (ret); 2416 } 2417 } 2418 2419 mdb_free(aux, len); 2420 } 2421 2422 return (0); 2423 } 2424 2425 /* 2426 * ::spa_vdevs 2427 * 2428 * -e Include error stats 2429 * -m Include metaslab information 2430 * -M Include metaslab group information 2431 * -h Include histogram information (requires -m or -M) 2432 * 2433 * Print out a summarized list of vdevs for the given spa_t. 2434 * This is accomplished by invoking "::vdev -re" on the root vdev, as well as 2435 * iterating over the cache devices. 2436 */ 2437 /* ARGSUSED */ 2438 static int 2439 spa_vdevs(uintptr_t addr, uint_t flags, int argc, const mdb_arg_t *argv) 2440 { 2441 mdb_arg_t v[3]; 2442 int ret; 2443 char opts[100] = "-r"; 2444 int spa_flags = 0; 2445 2446 if (mdb_getopts(argc, argv, 2447 'e', MDB_OPT_SETBITS, SPA_FLAG_ERRORS, &spa_flags, 2448 'm', MDB_OPT_SETBITS, SPA_FLAG_METASLABS, &spa_flags, 2449 'M', MDB_OPT_SETBITS, SPA_FLAG_METASLAB_GROUPS, &spa_flags, 2450 'h', MDB_OPT_SETBITS, SPA_FLAG_HISTOGRAMS, &spa_flags, 2451 NULL) != argc) 2452 return (DCMD_USAGE); 2453 2454 if (!(flags & DCMD_ADDRSPEC)) 2455 return (DCMD_USAGE); 2456 2457 mdb_spa_vdevs_t spa; 2458 if (mdb_ctf_vread(&spa, "spa_t", "mdb_spa_vdevs_t", addr, 0) == -1) 2459 return (DCMD_ERR); 2460 2461 /* 2462 * Unitialized spa_t structures can have a NULL root vdev. 2463 */ 2464 if (spa.spa_root_vdev == 0) { 2465 mdb_printf("no associated vdevs\n"); 2466 return (DCMD_OK); 2467 } 2468 2469 if (spa_flags & SPA_FLAG_ERRORS) 2470 strcat(opts, "e"); 2471 if (spa_flags & SPA_FLAG_METASLABS) 2472 strcat(opts, "m"); 2473 if (spa_flags & SPA_FLAG_METASLAB_GROUPS) 2474 strcat(opts, "M"); 2475 if (spa_flags & SPA_FLAG_HISTOGRAMS) 2476 strcat(opts, "h"); 2477 2478 v[0].a_type = MDB_TYPE_STRING; 2479 v[0].a_un.a_str = opts; 2480 2481 ret = mdb_call_dcmd("vdev", (uintptr_t)spa.spa_root_vdev, 2482 flags, 1, v); 2483 if (ret != DCMD_OK) 2484 return (ret); 2485 2486 if (spa_print_aux(&spa.spa_l2cache, flags, v, "cache") != 0 || 2487 spa_print_aux(&spa.spa_spares, flags, v, "spares") != 0) 2488 return (DCMD_ERR); 2489 2490 return (DCMD_OK); 2491 } 2492 2493 /* 2494 * ::zio 2495 * 2496 * Print a summary of zio_t and all its children. This is intended to display a 2497 * zio tree, and hence we only pick the most important pieces of information for 2498 * the main summary. More detailed information can always be found by doing a 2499 * '::print zio' on the underlying zio_t. The columns we display are: 2500 * 2501 * ADDRESS TYPE STAGE WAITER TIME_ELAPSED 2502 * 2503 * The 'address' column is indented by one space for each depth level as we 2504 * descend down the tree. 2505 */ 2506 2507 #define ZIO_MAXINDENT 7 2508 #define ZIO_MAXWIDTH (sizeof (uintptr_t) * 2 + ZIO_MAXINDENT) 2509 #define ZIO_WALK_SELF 0 2510 #define ZIO_WALK_CHILD 1 2511 #define ZIO_WALK_PARENT 2 2512 2513 typedef struct zio_print_args { 2514 int zpa_current_depth; 2515 int zpa_min_depth; 2516 int zpa_max_depth; 2517 int zpa_type; 2518 uint_t zpa_flags; 2519 } zio_print_args_t; 2520 2521 typedef struct mdb_zio { 2522 enum zio_type io_type; 2523 enum zio_stage io_stage; 2524 uintptr_t io_waiter; 2525 uintptr_t io_spa; 2526 struct { 2527 struct { 2528 uintptr_t list_next; 2529 } list_head; 2530 } io_parent_list; 2531 int io_error; 2532 } mdb_zio_t; 2533 2534 typedef struct mdb_zio_timestamp { 2535 hrtime_t io_timestamp; 2536 } mdb_zio_timestamp_t; 2537 2538 static int zio_child_cb(uintptr_t addr, const void *unknown, void *arg); 2539 2540 static int 2541 zio_print_cb(uintptr_t addr, zio_print_args_t *zpa) 2542 { 2543 mdb_ctf_id_t type_enum, stage_enum; 2544 int indent = zpa->zpa_current_depth; 2545 const char *type, *stage; 2546 uintptr_t laddr; 2547 mdb_zio_t zio; 2548 mdb_zio_timestamp_t zio_timestamp = { 0 }; 2549 2550 if (mdb_ctf_vread(&zio, ZFS_STRUCT "zio", "mdb_zio_t", addr, 0) == -1) 2551 return (WALK_ERR); 2552 (void) mdb_ctf_vread(&zio_timestamp, ZFS_STRUCT "zio", 2553 "mdb_zio_timestamp_t", addr, MDB_CTF_VREAD_QUIET); 2554 2555 if (indent > ZIO_MAXINDENT) 2556 indent = ZIO_MAXINDENT; 2557 2558 if (mdb_ctf_lookup_by_name("enum zio_type", &type_enum) == -1 || 2559 mdb_ctf_lookup_by_name("enum zio_stage", &stage_enum) == -1) { 2560 mdb_warn("failed to lookup zio enums"); 2561 return (WALK_ERR); 2562 } 2563 2564 if ((type = mdb_ctf_enum_name(type_enum, zio.io_type)) != NULL) 2565 type += sizeof ("ZIO_TYPE_") - 1; 2566 else 2567 type = "?"; 2568 2569 if (zio.io_error == 0) { 2570 stage = mdb_ctf_enum_name(stage_enum, zio.io_stage); 2571 if (stage != NULL) 2572 stage += sizeof ("ZIO_STAGE_") - 1; 2573 else 2574 stage = "?"; 2575 } else { 2576 stage = "FAILED"; 2577 } 2578 2579 if (zpa->zpa_current_depth >= zpa->zpa_min_depth) { 2580 if (zpa->zpa_flags & DCMD_PIPE_OUT) { 2581 mdb_printf("%?p\n", addr); 2582 } else { 2583 mdb_printf("%*s%-*p %-5s %-16s ", indent, "", 2584 ZIO_MAXWIDTH - indent, addr, type, stage); 2585 if (zio.io_waiter != 0) 2586 mdb_printf("%-16lx ", zio.io_waiter); 2587 else 2588 mdb_printf("%-16s ", "-"); 2589 #ifdef _KERNEL 2590 if (zio_timestamp.io_timestamp != 0) { 2591 mdb_printf("%llums", (mdb_gethrtime() - 2592 zio_timestamp.io_timestamp) / 2593 1000000); 2594 } else { 2595 mdb_printf("%-12s ", "-"); 2596 } 2597 #else 2598 mdb_printf("%-12s ", "-"); 2599 #endif 2600 mdb_printf("\n"); 2601 } 2602 } 2603 2604 if (zpa->zpa_current_depth >= zpa->zpa_max_depth) 2605 return (WALK_NEXT); 2606 2607 if (zpa->zpa_type == ZIO_WALK_PARENT) 2608 laddr = addr + mdb_ctf_offsetof_by_name(ZFS_STRUCT "zio", 2609 "io_parent_list"); 2610 else 2611 laddr = addr + mdb_ctf_offsetof_by_name(ZFS_STRUCT "zio", 2612 "io_child_list"); 2613 2614 zpa->zpa_current_depth++; 2615 if (mdb_pwalk("list", zio_child_cb, zpa, laddr) != 0) { 2616 mdb_warn("failed to walk zio_t children at %p\n", laddr); 2617 return (WALK_ERR); 2618 } 2619 zpa->zpa_current_depth--; 2620 2621 return (WALK_NEXT); 2622 } 2623 2624 /* ARGSUSED */ 2625 static int 2626 zio_child_cb(uintptr_t addr, const void *unknown, void *arg) 2627 { 2628 zio_link_t zl; 2629 uintptr_t ziop; 2630 zio_print_args_t *zpa = arg; 2631 2632 if (mdb_vread(&zl, sizeof (zl), addr) == -1) { 2633 mdb_warn("failed to read zio_link_t at %p", addr); 2634 return (WALK_ERR); 2635 } 2636 2637 if (zpa->zpa_type == ZIO_WALK_PARENT) 2638 ziop = (uintptr_t)zl.zl_parent; 2639 else 2640 ziop = (uintptr_t)zl.zl_child; 2641 2642 return (zio_print_cb(ziop, zpa)); 2643 } 2644 2645 /* ARGSUSED */ 2646 static int 2647 zio_print(uintptr_t addr, uint_t flags, int argc, const mdb_arg_t *argv) 2648 { 2649 zio_print_args_t zpa = { 0 }; 2650 2651 if (!(flags & DCMD_ADDRSPEC)) 2652 return (DCMD_USAGE); 2653 2654 if (mdb_getopts(argc, argv, 2655 'r', MDB_OPT_SETBITS, INT_MAX, &zpa.zpa_max_depth, 2656 'c', MDB_OPT_SETBITS, ZIO_WALK_CHILD, &zpa.zpa_type, 2657 'p', MDB_OPT_SETBITS, ZIO_WALK_PARENT, &zpa.zpa_type, 2658 NULL) != argc) 2659 return (DCMD_USAGE); 2660 2661 zpa.zpa_flags = flags; 2662 if (zpa.zpa_max_depth != 0) { 2663 if (zpa.zpa_type == ZIO_WALK_SELF) 2664 zpa.zpa_type = ZIO_WALK_CHILD; 2665 } else if (zpa.zpa_type != ZIO_WALK_SELF) { 2666 zpa.zpa_min_depth = 1; 2667 zpa.zpa_max_depth = 1; 2668 } 2669 2670 if (!(flags & DCMD_PIPE_OUT) && DCMD_HDRSPEC(flags)) { 2671 mdb_printf("%<u>%-*s %-5s %-16s %-16s %-12s%</u>\n", 2672 ZIO_MAXWIDTH, "ADDRESS", "TYPE", "STAGE", "WAITER", 2673 "TIME_ELAPSED"); 2674 } 2675 2676 if (zio_print_cb(addr, &zpa) != WALK_NEXT) 2677 return (DCMD_ERR); 2678 2679 return (DCMD_OK); 2680 } 2681 2682 /* 2683 * [addr]::zio_state 2684 * 2685 * Print a summary of all zio_t structures on the system, or for a particular 2686 * pool. This is equivalent to '::walk zio_root | ::zio'. 2687 */ 2688 /*ARGSUSED*/ 2689 static int 2690 zio_state(uintptr_t addr, uint_t flags, int argc, const mdb_arg_t *argv) 2691 { 2692 /* 2693 * MDB will remember the last address of the pipeline, so if we don't 2694 * zero this we'll end up trying to walk zio structures for a 2695 * non-existent spa_t. 2696 */ 2697 if (!(flags & DCMD_ADDRSPEC)) 2698 addr = 0; 2699 2700 return (mdb_pwalk_dcmd("zio_root", "zio", argc, argv, addr)); 2701 } 2702 2703 2704 typedef struct mdb_zfs_btree_hdr { 2705 uintptr_t bth_parent; 2706 boolean_t bth_core; 2707 /* 2708 * For both leaf and core nodes, represents the number of elements in 2709 * the node. For core nodes, they will have bth_count + 1 children. 2710 */ 2711 uint32_t bth_count; 2712 } mdb_zfs_btree_hdr_t; 2713 2714 typedef struct mdb_zfs_btree_core { 2715 mdb_zfs_btree_hdr_t btc_hdr; 2716 uintptr_t btc_children[BTREE_CORE_ELEMS + 1]; 2717 uint8_t btc_elems[]; 2718 } mdb_zfs_btree_core_t; 2719 2720 typedef struct mdb_zfs_btree_leaf { 2721 mdb_zfs_btree_hdr_t btl_hdr; 2722 uint8_t btl_elems[]; 2723 } mdb_zfs_btree_leaf_t; 2724 2725 typedef struct mdb_zfs_btree { 2726 uintptr_t bt_root; 2727 size_t bt_elem_size; 2728 } mdb_zfs_btree_t; 2729 2730 typedef struct btree_walk_data { 2731 mdb_zfs_btree_t bwd_btree; 2732 mdb_zfs_btree_hdr_t *bwd_node; 2733 uint64_t bwd_offset; // In units of bt_node_size 2734 } btree_walk_data_t; 2735 2736 static uintptr_t 2737 btree_leftmost_child(uintptr_t addr, mdb_zfs_btree_hdr_t *buf) 2738 { 2739 size_t size = offsetof(zfs_btree_core_t, btc_children) + 2740 sizeof (uintptr_t); 2741 for (;;) { 2742 if (mdb_vread(buf, size, addr) == -1) { 2743 mdb_warn("failed to read at %p\n", addr); 2744 return ((uintptr_t)0ULL); 2745 } 2746 if (!buf->bth_core) 2747 return (addr); 2748 mdb_zfs_btree_core_t *node = (mdb_zfs_btree_core_t *)buf; 2749 addr = node->btc_children[0]; 2750 } 2751 } 2752 2753 static int 2754 btree_walk_step(mdb_walk_state_t *wsp) 2755 { 2756 btree_walk_data_t *bwd = wsp->walk_data; 2757 size_t elem_size = bwd->bwd_btree.bt_elem_size; 2758 if (wsp->walk_addr == 0ULL) 2759 return (WALK_DONE); 2760 2761 if (!bwd->bwd_node->bth_core) { 2762 /* 2763 * For the first element in a leaf node, read in the full 2764 * leaf, since we only had part of it read in before. 2765 */ 2766 if (bwd->bwd_offset == 0) { 2767 if (mdb_vread(bwd->bwd_node, BTREE_LEAF_SIZE, 2768 wsp->walk_addr) == -1) { 2769 mdb_warn("failed to read at %p\n", 2770 wsp->walk_addr); 2771 return (WALK_ERR); 2772 } 2773 } 2774 2775 int status = wsp->walk_callback((uintptr_t)(wsp->walk_addr + 2776 offsetof(mdb_zfs_btree_leaf_t, btl_elems) + 2777 bwd->bwd_offset * elem_size), bwd->bwd_node, 2778 wsp->walk_cbdata); 2779 if (status != WALK_NEXT) 2780 return (status); 2781 bwd->bwd_offset++; 2782 2783 /* Find the next element, if we're at the end of the leaf. */ 2784 while (bwd->bwd_offset == bwd->bwd_node->bth_count) { 2785 uintptr_t par = bwd->bwd_node->bth_parent; 2786 uintptr_t cur = wsp->walk_addr; 2787 wsp->walk_addr = par; 2788 if (par == 0ULL) 2789 return (WALK_NEXT); 2790 2791 size_t size = sizeof (zfs_btree_core_t) + 2792 BTREE_CORE_ELEMS * elem_size; 2793 if (mdb_vread(bwd->bwd_node, size, wsp->walk_addr) == 2794 -1) { 2795 mdb_warn("failed to read at %p\n", 2796 wsp->walk_addr); 2797 return (WALK_ERR); 2798 } 2799 mdb_zfs_btree_core_t *node = 2800 (mdb_zfs_btree_core_t *)bwd->bwd_node; 2801 int i; 2802 for (i = 0; i <= bwd->bwd_node->bth_count; i++) { 2803 if (node->btc_children[i] == cur) 2804 break; 2805 } 2806 if (i > bwd->bwd_node->bth_count) { 2807 mdb_warn("btree parent/child mismatch at " 2808 "%#lx\n", cur); 2809 return (WALK_ERR); 2810 } 2811 bwd->bwd_offset = i; 2812 } 2813 return (WALK_NEXT); 2814 } 2815 2816 if (!bwd->bwd_node->bth_core) { 2817 mdb_warn("Invalid btree node at %#lx\n", wsp->walk_addr); 2818 return (WALK_ERR); 2819 } 2820 mdb_zfs_btree_core_t *node = (mdb_zfs_btree_core_t *)bwd->bwd_node; 2821 int status = wsp->walk_callback((uintptr_t)(wsp->walk_addr + 2822 offsetof(mdb_zfs_btree_core_t, btc_elems) + bwd->bwd_offset * 2823 elem_size), bwd->bwd_node, wsp->walk_cbdata); 2824 if (status != WALK_NEXT) 2825 return (status); 2826 2827 uintptr_t new_child = node->btc_children[bwd->bwd_offset + 1]; 2828 wsp->walk_addr = btree_leftmost_child(new_child, bwd->bwd_node); 2829 if (wsp->walk_addr == 0ULL) 2830 return (WALK_ERR); 2831 2832 bwd->bwd_offset = 0; 2833 return (WALK_NEXT); 2834 } 2835 2836 static int 2837 btree_walk_init(mdb_walk_state_t *wsp) 2838 { 2839 btree_walk_data_t *bwd; 2840 2841 if (wsp->walk_addr == 0ULL) { 2842 mdb_warn("must supply address of zfs_btree_t\n"); 2843 return (WALK_ERR); 2844 } 2845 2846 bwd = mdb_zalloc(sizeof (btree_walk_data_t), UM_SLEEP); 2847 if (mdb_ctf_vread(&bwd->bwd_btree, "zfs_btree_t", "mdb_zfs_btree_t", 2848 wsp->walk_addr, 0) == -1) { 2849 mdb_free(bwd, sizeof (*bwd)); 2850 return (WALK_ERR); 2851 } 2852 2853 if (bwd->bwd_btree.bt_elem_size == 0) { 2854 mdb_warn("invalid or uninitialized btree at %#lx\n", 2855 wsp->walk_addr); 2856 mdb_free(bwd, sizeof (*bwd)); 2857 return (WALK_ERR); 2858 } 2859 2860 size_t size = MAX(BTREE_LEAF_SIZE, sizeof (zfs_btree_core_t) + 2861 BTREE_CORE_ELEMS * bwd->bwd_btree.bt_elem_size); 2862 bwd->bwd_node = mdb_zalloc(size, UM_SLEEP); 2863 2864 uintptr_t node = (uintptr_t)bwd->bwd_btree.bt_root; 2865 if (node == 0ULL) { 2866 wsp->walk_addr = 0ULL; 2867 wsp->walk_data = bwd; 2868 return (WALK_NEXT); 2869 } 2870 node = btree_leftmost_child(node, bwd->bwd_node); 2871 if (node == 0ULL) { 2872 mdb_free(bwd->bwd_node, size); 2873 mdb_free(bwd, sizeof (*bwd)); 2874 return (WALK_ERR); 2875 } 2876 bwd->bwd_offset = 0; 2877 2878 wsp->walk_addr = node; 2879 wsp->walk_data = bwd; 2880 return (WALK_NEXT); 2881 } 2882 2883 static void 2884 btree_walk_fini(mdb_walk_state_t *wsp) 2885 { 2886 btree_walk_data_t *bwd = (btree_walk_data_t *)wsp->walk_data; 2887 2888 if (bwd == NULL) 2889 return; 2890 2891 size_t size = MAX(BTREE_LEAF_SIZE, sizeof (zfs_btree_core_t) + 2892 BTREE_CORE_ELEMS * bwd->bwd_btree.bt_elem_size); 2893 if (bwd->bwd_node != NULL) 2894 mdb_free(bwd->bwd_node, size); 2895 2896 mdb_free(bwd, sizeof (*bwd)); 2897 } 2898 2899 typedef struct mdb_multilist { 2900 uint64_t ml_num_sublists; 2901 uintptr_t ml_sublists; 2902 } mdb_multilist_t; 2903 2904 static int 2905 multilist_walk_step(mdb_walk_state_t *wsp) 2906 { 2907 return (wsp->walk_callback(wsp->walk_addr, wsp->walk_layer, 2908 wsp->walk_cbdata)); 2909 } 2910 2911 static int 2912 multilist_walk_init(mdb_walk_state_t *wsp) 2913 { 2914 mdb_multilist_t ml; 2915 ssize_t sublist_sz; 2916 int list_offset; 2917 size_t i; 2918 2919 if (wsp->walk_addr == 0) { 2920 mdb_warn("must supply address of multilist_t\n"); 2921 return (WALK_ERR); 2922 } 2923 2924 if (mdb_ctf_vread(&ml, "multilist_t", "mdb_multilist_t", 2925 wsp->walk_addr, 0) == -1) { 2926 return (WALK_ERR); 2927 } 2928 2929 if (ml.ml_num_sublists == 0 || ml.ml_sublists == 0) { 2930 mdb_warn("invalid or uninitialized multilist at %#lx\n", 2931 wsp->walk_addr); 2932 return (WALK_ERR); 2933 } 2934 2935 /* mdb_ctf_sizeof_by_name() will print an error for us */ 2936 sublist_sz = mdb_ctf_sizeof_by_name("multilist_sublist_t"); 2937 if (sublist_sz == -1) 2938 return (WALK_ERR); 2939 2940 /* mdb_ctf_offsetof_by_name will print an error for us */ 2941 list_offset = mdb_ctf_offsetof_by_name("multilist_sublist_t", 2942 "mls_list"); 2943 if (list_offset == -1) 2944 return (WALK_ERR); 2945 2946 for (i = 0; i < ml.ml_num_sublists; i++) { 2947 wsp->walk_addr = ml.ml_sublists + i * sublist_sz + list_offset; 2948 2949 if (mdb_layered_walk("list", wsp) == -1) { 2950 mdb_warn("can't walk multilist sublist"); 2951 return (WALK_ERR); 2952 } 2953 } 2954 2955 return (WALK_NEXT); 2956 } 2957 2958 typedef struct mdb_txg_list { 2959 size_t tl_offset; 2960 uintptr_t tl_head[TXG_SIZE]; 2961 } mdb_txg_list_t; 2962 2963 typedef struct txg_list_walk_data { 2964 uintptr_t lw_head[TXG_SIZE]; 2965 int lw_txgoff; 2966 int lw_maxoff; 2967 size_t lw_offset; 2968 void *lw_obj; 2969 } txg_list_walk_data_t; 2970 2971 static int 2972 txg_list_walk_init_common(mdb_walk_state_t *wsp, int txg, int maxoff) 2973 { 2974 txg_list_walk_data_t *lwd; 2975 mdb_txg_list_t list; 2976 int i; 2977 2978 lwd = mdb_alloc(sizeof (txg_list_walk_data_t), UM_SLEEP | UM_GC); 2979 if (mdb_ctf_vread(&list, "txg_list_t", "mdb_txg_list_t", wsp->walk_addr, 2980 0) == -1) { 2981 mdb_warn("failed to read txg_list_t at %#lx", wsp->walk_addr); 2982 return (WALK_ERR); 2983 } 2984 2985 for (i = 0; i < TXG_SIZE; i++) 2986 lwd->lw_head[i] = list.tl_head[i]; 2987 lwd->lw_offset = list.tl_offset; 2988 lwd->lw_obj = mdb_alloc(lwd->lw_offset + sizeof (txg_node_t), 2989 UM_SLEEP | UM_GC); 2990 lwd->lw_txgoff = txg; 2991 lwd->lw_maxoff = maxoff; 2992 2993 wsp->walk_addr = lwd->lw_head[lwd->lw_txgoff]; 2994 wsp->walk_data = lwd; 2995 2996 return (WALK_NEXT); 2997 } 2998 2999 static int 3000 txg_list_walk_init(mdb_walk_state_t *wsp) 3001 { 3002 return (txg_list_walk_init_common(wsp, 0, TXG_SIZE-1)); 3003 } 3004 3005 static int 3006 txg_list0_walk_init(mdb_walk_state_t *wsp) 3007 { 3008 return (txg_list_walk_init_common(wsp, 0, 0)); 3009 } 3010 3011 static int 3012 txg_list1_walk_init(mdb_walk_state_t *wsp) 3013 { 3014 return (txg_list_walk_init_common(wsp, 1, 1)); 3015 } 3016 3017 static int 3018 txg_list2_walk_init(mdb_walk_state_t *wsp) 3019 { 3020 return (txg_list_walk_init_common(wsp, 2, 2)); 3021 } 3022 3023 static int 3024 txg_list3_walk_init(mdb_walk_state_t *wsp) 3025 { 3026 return (txg_list_walk_init_common(wsp, 3, 3)); 3027 } 3028 3029 static int 3030 txg_list_walk_step(mdb_walk_state_t *wsp) 3031 { 3032 txg_list_walk_data_t *lwd = wsp->walk_data; 3033 uintptr_t addr; 3034 txg_node_t *node; 3035 int status; 3036 3037 while (wsp->walk_addr == 0 && lwd->lw_txgoff < lwd->lw_maxoff) { 3038 lwd->lw_txgoff++; 3039 wsp->walk_addr = lwd->lw_head[lwd->lw_txgoff]; 3040 } 3041 3042 if (wsp->walk_addr == 0) 3043 return (WALK_DONE); 3044 3045 addr = wsp->walk_addr - lwd->lw_offset; 3046 3047 if (mdb_vread(lwd->lw_obj, 3048 lwd->lw_offset + sizeof (txg_node_t), addr) == -1) { 3049 mdb_warn("failed to read list element at %#lx", addr); 3050 return (WALK_ERR); 3051 } 3052 3053 status = wsp->walk_callback(addr, lwd->lw_obj, wsp->walk_cbdata); 3054 node = (txg_node_t *)((uintptr_t)lwd->lw_obj + lwd->lw_offset); 3055 wsp->walk_addr = (uintptr_t)node->tn_next[lwd->lw_txgoff]; 3056 3057 return (status); 3058 } 3059 3060 /* 3061 * ::walk spa 3062 * 3063 * Walk all named spa_t structures in the namespace. This is nothing more than 3064 * a layered avl walk. 3065 */ 3066 static int 3067 spa_walk_init(mdb_walk_state_t *wsp) 3068 { 3069 GElf_Sym sym; 3070 3071 if (wsp->walk_addr != 0) { 3072 mdb_warn("spa walk only supports global walks\n"); 3073 return (WALK_ERR); 3074 } 3075 3076 if (mdb_lookup_by_obj(ZFS_OBJ_NAME, "spa_namespace_avl", &sym) == -1) { 3077 mdb_warn("failed to find symbol 'spa_namespace_avl'"); 3078 return (WALK_ERR); 3079 } 3080 3081 wsp->walk_addr = (uintptr_t)sym.st_value; 3082 3083 if (mdb_layered_walk("avl", wsp) == -1) { 3084 mdb_warn("failed to walk 'avl'\n"); 3085 return (WALK_ERR); 3086 } 3087 3088 return (WALK_NEXT); 3089 } 3090 3091 static int 3092 spa_walk_step(mdb_walk_state_t *wsp) 3093 { 3094 return (wsp->walk_callback(wsp->walk_addr, NULL, wsp->walk_cbdata)); 3095 } 3096 3097 /* 3098 * [addr]::walk zio 3099 * 3100 * Walk all active zio_t structures on the system. This is simply a layered 3101 * walk on top of ::walk zio_cache, with the optional ability to limit the 3102 * structures to a particular pool. 3103 */ 3104 static int 3105 zio_walk_init(mdb_walk_state_t *wsp) 3106 { 3107 wsp->walk_data = (void *)wsp->walk_addr; 3108 3109 if (mdb_layered_walk("zio_cache", wsp) == -1) { 3110 mdb_warn("failed to walk 'zio_cache'\n"); 3111 return (WALK_ERR); 3112 } 3113 3114 return (WALK_NEXT); 3115 } 3116 3117 static int 3118 zio_walk_step(mdb_walk_state_t *wsp) 3119 { 3120 mdb_zio_t zio; 3121 uintptr_t spa = (uintptr_t)wsp->walk_data; 3122 3123 if (mdb_ctf_vread(&zio, ZFS_STRUCT "zio", "mdb_zio_t", 3124 wsp->walk_addr, 0) == -1) 3125 return (WALK_ERR); 3126 3127 if (spa != 0 && spa != zio.io_spa) 3128 return (WALK_NEXT); 3129 3130 return (wsp->walk_callback(wsp->walk_addr, &zio, wsp->walk_cbdata)); 3131 } 3132 3133 /* 3134 * [addr]::walk zio_root 3135 * 3136 * Walk only root zio_t structures, optionally for a particular spa_t. 3137 */ 3138 static int 3139 zio_walk_root_step(mdb_walk_state_t *wsp) 3140 { 3141 mdb_zio_t zio; 3142 uintptr_t spa = (uintptr_t)wsp->walk_data; 3143 3144 if (mdb_ctf_vread(&zio, ZFS_STRUCT "zio", "mdb_zio_t", 3145 wsp->walk_addr, 0) == -1) 3146 return (WALK_ERR); 3147 3148 if (spa != 0 && spa != zio.io_spa) 3149 return (WALK_NEXT); 3150 3151 /* If the parent list is not empty, ignore */ 3152 if (zio.io_parent_list.list_head.list_next != 3153 wsp->walk_addr + 3154 mdb_ctf_offsetof_by_name(ZFS_STRUCT "zio", "io_parent_list") + 3155 mdb_ctf_offsetof_by_name("struct list", "list_head")) 3156 return (WALK_NEXT); 3157 3158 return (wsp->walk_callback(wsp->walk_addr, &zio, wsp->walk_cbdata)); 3159 } 3160 3161 /* 3162 * ::zfs_blkstats 3163 * 3164 * -v print verbose per-level information 3165 * 3166 */ 3167 static int 3168 zfs_blkstats(uintptr_t addr, uint_t flags, int argc, const mdb_arg_t *argv) 3169 { 3170 boolean_t verbose = B_FALSE; 3171 zfs_all_blkstats_t stats; 3172 dmu_object_type_t t; 3173 zfs_blkstat_t *tzb; 3174 uint64_t ditto; 3175 3176 if (mdb_getopts(argc, argv, 3177 'v', MDB_OPT_SETBITS, TRUE, &verbose, 3178 NULL) != argc) 3179 return (DCMD_USAGE); 3180 3181 if (!(flags & DCMD_ADDRSPEC)) 3182 return (DCMD_USAGE); 3183 3184 if (GETMEMB(addr, "spa", spa_dsl_pool, addr) || 3185 GETMEMB(addr, "dsl_pool", dp_blkstats, addr) || 3186 mdb_vread(&stats, sizeof (zfs_all_blkstats_t), addr) == -1) { 3187 mdb_warn("failed to read data at %p;", addr); 3188 mdb_printf("maybe no stats? run \"zpool scrub\" first."); 3189 return (DCMD_ERR); 3190 } 3191 3192 tzb = &stats.zab_type[DN_MAX_LEVELS][DMU_OT_TOTAL]; 3193 if (tzb->zb_gangs != 0) { 3194 mdb_printf("Ganged blocks: %llu\n", 3195 (longlong_t)tzb->zb_gangs); 3196 } 3197 3198 ditto = tzb->zb_ditto_2_of_2_samevdev + tzb->zb_ditto_2_of_3_samevdev + 3199 tzb->zb_ditto_3_of_3_samevdev; 3200 if (ditto != 0) { 3201 mdb_printf("Dittoed blocks on same vdev: %llu\n", 3202 (longlong_t)ditto); 3203 } 3204 3205 mdb_printf("\nBlocks\tLSIZE\tPSIZE\tASIZE" 3206 "\t avg\t comp\t%%Total\tType\n"); 3207 3208 for (t = 0; t <= DMU_OT_TOTAL; t++) { 3209 char csize[MDB_NICENUM_BUFLEN], lsize[MDB_NICENUM_BUFLEN]; 3210 char psize[MDB_NICENUM_BUFLEN], asize[MDB_NICENUM_BUFLEN]; 3211 char avg[MDB_NICENUM_BUFLEN]; 3212 char comp[MDB_NICENUM_BUFLEN], pct[MDB_NICENUM_BUFLEN]; 3213 char typename[64]; 3214 int l; 3215 3216 3217 if (t == DMU_OT_DEFERRED) 3218 strcpy(typename, "deferred free"); 3219 else if (t == DMU_OT_OTHER) 3220 strcpy(typename, "other"); 3221 else if (t == DMU_OT_TOTAL) 3222 strcpy(typename, "Total"); 3223 else if (enum_lookup("enum dmu_object_type", 3224 t, "DMU_OT_", sizeof (typename), typename) == -1) { 3225 mdb_warn("failed to read type name"); 3226 return (DCMD_ERR); 3227 } 3228 3229 if (stats.zab_type[DN_MAX_LEVELS][t].zb_asize == 0) 3230 continue; 3231 3232 for (l = -1; l < DN_MAX_LEVELS; l++) { 3233 int level = (l == -1 ? DN_MAX_LEVELS : l); 3234 zfs_blkstat_t *zb = &stats.zab_type[level][t]; 3235 3236 if (zb->zb_asize == 0) 3237 continue; 3238 3239 /* 3240 * Don't print each level unless requested. 3241 */ 3242 if (!verbose && level != DN_MAX_LEVELS) 3243 continue; 3244 3245 /* 3246 * If all the space is level 0, don't print the 3247 * level 0 separately. 3248 */ 3249 if (level == 0 && zb->zb_asize == 3250 stats.zab_type[DN_MAX_LEVELS][t].zb_asize) 3251 continue; 3252 3253 mdb_nicenum(zb->zb_count, csize); 3254 mdb_nicenum(zb->zb_lsize, lsize); 3255 mdb_nicenum(zb->zb_psize, psize); 3256 mdb_nicenum(zb->zb_asize, asize); 3257 mdb_nicenum(zb->zb_asize / zb->zb_count, avg); 3258 (void) mdb_snprintfrac(comp, MDB_NICENUM_BUFLEN, 3259 zb->zb_lsize, zb->zb_psize, 2); 3260 (void) mdb_snprintfrac(pct, MDB_NICENUM_BUFLEN, 3261 100 * zb->zb_asize, tzb->zb_asize, 2); 3262 3263 mdb_printf("%6s\t%5s\t%5s\t%5s\t%5s" 3264 "\t%5s\t%6s\t", 3265 csize, lsize, psize, asize, avg, comp, pct); 3266 3267 if (level == DN_MAX_LEVELS) 3268 mdb_printf("%s\n", typename); 3269 else 3270 mdb_printf(" L%d %s\n", 3271 level, typename); 3272 } 3273 } 3274 3275 return (DCMD_OK); 3276 } 3277 3278 typedef struct mdb_reference { 3279 uintptr_t ref_holder; 3280 uintptr_t ref_removed; 3281 uint64_t ref_number; 3282 } mdb_reference_t; 3283 3284 /* ARGSUSED */ 3285 static int 3286 reference_cb(uintptr_t addr, const void *ignored, void *arg) 3287 { 3288 mdb_reference_t ref; 3289 boolean_t holder_is_str = B_FALSE; 3290 char holder_str[128]; 3291 boolean_t removed = (boolean_t)arg; 3292 3293 if (mdb_ctf_vread(&ref, "reference_t", "mdb_reference_t", addr, 3294 0) == -1) 3295 return (DCMD_ERR); 3296 3297 if (mdb_readstr(holder_str, sizeof (holder_str), 3298 ref.ref_holder) != -1) 3299 holder_is_str = strisprint(holder_str); 3300 3301 if (removed) 3302 mdb_printf("removed "); 3303 mdb_printf("reference "); 3304 if (ref.ref_number != 1) 3305 mdb_printf("with count=%llu ", ref.ref_number); 3306 mdb_printf("with tag %lx", ref.ref_holder); 3307 if (holder_is_str) 3308 mdb_printf(" \"%s\"", holder_str); 3309 mdb_printf(", held at:\n"); 3310 3311 (void) mdb_call_dcmd("whatis", addr, DCMD_ADDRSPEC, 0, NULL); 3312 3313 if (removed) { 3314 mdb_printf("removed at:\n"); 3315 (void) mdb_call_dcmd("whatis", ref.ref_removed, 3316 DCMD_ADDRSPEC, 0, NULL); 3317 } 3318 3319 mdb_printf("\n"); 3320 3321 return (WALK_NEXT); 3322 } 3323 3324 typedef struct mdb_zfs_refcount { 3325 uint64_t rc_count; 3326 } mdb_zfs_refcount_t; 3327 3328 typedef struct mdb_zfs_refcount_removed { 3329 uint_t rc_removed_count; 3330 } mdb_zfs_refcount_removed_t; 3331 3332 typedef struct mdb_zfs_refcount_tracked { 3333 boolean_t rc_tracked; 3334 } mdb_zfs_refcount_tracked_t; 3335 3336 /* ARGSUSED */ 3337 static int 3338 zfs_refcount(uintptr_t addr, uint_t flags, int argc, const mdb_arg_t *argv) 3339 { 3340 mdb_zfs_refcount_t rc; 3341 mdb_zfs_refcount_removed_t rcr; 3342 mdb_zfs_refcount_tracked_t rct; 3343 int off; 3344 boolean_t released = B_FALSE; 3345 3346 if (!(flags & DCMD_ADDRSPEC)) 3347 return (DCMD_USAGE); 3348 3349 if (mdb_getopts(argc, argv, 3350 'r', MDB_OPT_SETBITS, B_TRUE, &released, 3351 NULL) != argc) 3352 return (DCMD_USAGE); 3353 3354 if (mdb_ctf_vread(&rc, "zfs_refcount_t", "mdb_zfs_refcount_t", addr, 3355 0) == -1) 3356 return (DCMD_ERR); 3357 3358 if (mdb_ctf_vread(&rcr, "zfs_refcount_t", "mdb_zfs_refcount_removed_t", 3359 addr, MDB_CTF_VREAD_QUIET) == -1) { 3360 mdb_printf("zfs_refcount_t at %p has %llu holds (untracked)\n", 3361 addr, (longlong_t)rc.rc_count); 3362 return (DCMD_OK); 3363 } 3364 3365 if (mdb_ctf_vread(&rct, "zfs_refcount_t", "mdb_zfs_refcount_tracked_t", 3366 addr, MDB_CTF_VREAD_QUIET) == -1) { 3367 /* If this is an old target, it might be tracked. */ 3368 rct.rc_tracked = B_TRUE; 3369 } 3370 3371 mdb_printf("zfs_refcount_t at %p has %llu current holds, " 3372 "%llu recently released holds\n", 3373 addr, (longlong_t)rc.rc_count, (longlong_t)rcr.rc_removed_count); 3374 3375 if (rct.rc_tracked && rc.rc_count > 0) 3376 mdb_printf("current holds:\n"); 3377 off = mdb_ctf_offsetof_by_name("zfs_refcount_t", "rc_tree"); 3378 if (off == -1) 3379 return (DCMD_ERR); 3380 mdb_pwalk("avl", reference_cb, (void *)B_FALSE, addr + off); 3381 3382 if (released && rcr.rc_removed_count > 0) { 3383 mdb_printf("released holds:\n"); 3384 3385 off = mdb_ctf_offsetof_by_name("zfs_refcount_t", "rc_removed"); 3386 if (off == -1) 3387 return (DCMD_ERR); 3388 mdb_pwalk("list", reference_cb, (void *)B_TRUE, addr + off); 3389 } 3390 3391 return (DCMD_OK); 3392 } 3393 3394 /* ARGSUSED */ 3395 static int 3396 sa_attr_table(uintptr_t addr, uint_t flags, int argc, const mdb_arg_t *argv) 3397 { 3398 sa_attr_table_t *table; 3399 sa_os_t sa_os; 3400 char *name; 3401 int i; 3402 3403 if (mdb_vread(&sa_os, sizeof (sa_os_t), addr) == -1) { 3404 mdb_warn("failed to read sa_os at %p", addr); 3405 return (DCMD_ERR); 3406 } 3407 3408 table = mdb_alloc(sizeof (sa_attr_table_t) * sa_os.sa_num_attrs, 3409 UM_SLEEP | UM_GC); 3410 name = mdb_alloc(MAXPATHLEN, UM_SLEEP | UM_GC); 3411 3412 if (mdb_vread(table, sizeof (sa_attr_table_t) * sa_os.sa_num_attrs, 3413 (uintptr_t)sa_os.sa_attr_table) == -1) { 3414 mdb_warn("failed to read sa_os at %p", addr); 3415 return (DCMD_ERR); 3416 } 3417 3418 mdb_printf("%<u>%-10s %-10s %-10s %-10s %s%</u>\n", 3419 "ATTR ID", "REGISTERED", "LENGTH", "BSWAP", "NAME"); 3420 for (i = 0; i != sa_os.sa_num_attrs; i++) { 3421 mdb_readstr(name, MAXPATHLEN, (uintptr_t)table[i].sa_name); 3422 mdb_printf("%5x %8x %8x %8x %-s\n", 3423 (int)table[i].sa_attr, (int)table[i].sa_registered, 3424 (int)table[i].sa_length, table[i].sa_byteswap, name); 3425 } 3426 3427 return (DCMD_OK); 3428 } 3429 3430 static int 3431 sa_get_off_table(uintptr_t addr, uint32_t **off_tab, int attr_count) 3432 { 3433 uintptr_t idx_table; 3434 3435 if (GETMEMB(addr, "sa_idx_tab", sa_idx_tab, idx_table)) { 3436 mdb_printf("can't find offset table in sa_idx_tab\n"); 3437 return (-1); 3438 } 3439 3440 *off_tab = mdb_alloc(attr_count * sizeof (uint32_t), 3441 UM_SLEEP | UM_GC); 3442 3443 if (mdb_vread(*off_tab, 3444 attr_count * sizeof (uint32_t), idx_table) == -1) { 3445 mdb_warn("failed to attribute offset table %p", idx_table); 3446 return (-1); 3447 } 3448 3449 return (DCMD_OK); 3450 } 3451 3452 /*ARGSUSED*/ 3453 static int 3454 sa_attr_print(uintptr_t addr, uint_t flags, int argc, const mdb_arg_t *argv) 3455 { 3456 uint32_t *offset_tab; 3457 int attr_count; 3458 uint64_t attr_id; 3459 uintptr_t attr_addr; 3460 uintptr_t bonus_tab, spill_tab; 3461 uintptr_t db_bonus, db_spill; 3462 uintptr_t os, os_sa; 3463 uintptr_t db_data; 3464 3465 if (argc != 1) 3466 return (DCMD_USAGE); 3467 3468 if (argv[0].a_type == MDB_TYPE_STRING) 3469 attr_id = mdb_strtoull(argv[0].a_un.a_str); 3470 else 3471 return (DCMD_USAGE); 3472 3473 if (GETMEMB(addr, "sa_handle", sa_bonus_tab, bonus_tab) || 3474 GETMEMB(addr, "sa_handle", sa_spill_tab, spill_tab) || 3475 GETMEMB(addr, "sa_handle", sa_os, os) || 3476 GETMEMB(addr, "sa_handle", sa_bonus, db_bonus) || 3477 GETMEMB(addr, "sa_handle", sa_spill, db_spill)) { 3478 mdb_printf("Can't find necessary information in sa_handle " 3479 "in sa_handle\n"); 3480 return (DCMD_ERR); 3481 } 3482 3483 if (GETMEMB(os, "objset", os_sa, os_sa)) { 3484 mdb_printf("Can't find os_sa in objset\n"); 3485 return (DCMD_ERR); 3486 } 3487 3488 if (GETMEMB(os_sa, "sa_os", sa_num_attrs, attr_count)) { 3489 mdb_printf("Can't find sa_num_attrs\n"); 3490 return (DCMD_ERR); 3491 } 3492 3493 if (attr_id > attr_count) { 3494 mdb_printf("attribute id number is out of range\n"); 3495 return (DCMD_ERR); 3496 } 3497 3498 if (bonus_tab) { 3499 if (sa_get_off_table(bonus_tab, &offset_tab, 3500 attr_count) == -1) { 3501 return (DCMD_ERR); 3502 } 3503 3504 if (GETMEMB(db_bonus, "dmu_buf", db_data, db_data)) { 3505 mdb_printf("can't find db_data in bonus dbuf\n"); 3506 return (DCMD_ERR); 3507 } 3508 } 3509 3510 if (bonus_tab && !TOC_ATTR_PRESENT(offset_tab[attr_id]) && 3511 spill_tab == 0) { 3512 mdb_printf("Attribute does not exist\n"); 3513 return (DCMD_ERR); 3514 } else if (!TOC_ATTR_PRESENT(offset_tab[attr_id]) && spill_tab) { 3515 if (sa_get_off_table(spill_tab, &offset_tab, 3516 attr_count) == -1) { 3517 return (DCMD_ERR); 3518 } 3519 if (GETMEMB(db_spill, "dmu_buf", db_data, db_data)) { 3520 mdb_printf("can't find db_data in spill dbuf\n"); 3521 return (DCMD_ERR); 3522 } 3523 if (!TOC_ATTR_PRESENT(offset_tab[attr_id])) { 3524 mdb_printf("Attribute does not exist\n"); 3525 return (DCMD_ERR); 3526 } 3527 } 3528 attr_addr = db_data + TOC_OFF(offset_tab[attr_id]); 3529 mdb_printf("%p\n", attr_addr); 3530 return (DCMD_OK); 3531 } 3532 3533 /* ARGSUSED */ 3534 static int 3535 zfs_ace_print_common(uintptr_t addr, uint_t flags, 3536 uint64_t id, uint32_t access_mask, uint16_t ace_flags, 3537 uint16_t ace_type, int verbose) 3538 { 3539 if (DCMD_HDRSPEC(flags) && !verbose) 3540 mdb_printf("%<u>%-?s %-8s %-8s %-8s %s%</u>\n", 3541 "ADDR", "FLAGS", "MASK", "TYPE", "ID"); 3542 3543 if (!verbose) { 3544 mdb_printf("%0?p %-8x %-8x %-8x %-llx\n", addr, 3545 ace_flags, access_mask, ace_type, id); 3546 return (DCMD_OK); 3547 } 3548 3549 switch (ace_flags & ACE_TYPE_FLAGS) { 3550 case ACE_OWNER: 3551 mdb_printf("owner@:"); 3552 break; 3553 case (ACE_IDENTIFIER_GROUP | ACE_GROUP): 3554 mdb_printf("group@:"); 3555 break; 3556 case ACE_EVERYONE: 3557 mdb_printf("everyone@:"); 3558 break; 3559 case ACE_IDENTIFIER_GROUP: 3560 mdb_printf("group:%llx:", (u_longlong_t)id); 3561 break; 3562 case 0: /* User entry */ 3563 mdb_printf("user:%llx:", (u_longlong_t)id); 3564 break; 3565 } 3566 3567 /* print out permission mask */ 3568 if (access_mask & ACE_READ_DATA) 3569 mdb_printf("r"); 3570 else 3571 mdb_printf("-"); 3572 if (access_mask & ACE_WRITE_DATA) 3573 mdb_printf("w"); 3574 else 3575 mdb_printf("-"); 3576 if (access_mask & ACE_EXECUTE) 3577 mdb_printf("x"); 3578 else 3579 mdb_printf("-"); 3580 if (access_mask & ACE_APPEND_DATA) 3581 mdb_printf("p"); 3582 else 3583 mdb_printf("-"); 3584 if (access_mask & ACE_DELETE) 3585 mdb_printf("d"); 3586 else 3587 mdb_printf("-"); 3588 if (access_mask & ACE_DELETE_CHILD) 3589 mdb_printf("D"); 3590 else 3591 mdb_printf("-"); 3592 if (access_mask & ACE_READ_ATTRIBUTES) 3593 mdb_printf("a"); 3594 else 3595 mdb_printf("-"); 3596 if (access_mask & ACE_WRITE_ATTRIBUTES) 3597 mdb_printf("A"); 3598 else 3599 mdb_printf("-"); 3600 if (access_mask & ACE_READ_NAMED_ATTRS) 3601 mdb_printf("R"); 3602 else 3603 mdb_printf("-"); 3604 if (access_mask & ACE_WRITE_NAMED_ATTRS) 3605 mdb_printf("W"); 3606 else 3607 mdb_printf("-"); 3608 if (access_mask & ACE_READ_ACL) 3609 mdb_printf("c"); 3610 else 3611 mdb_printf("-"); 3612 if (access_mask & ACE_WRITE_ACL) 3613 mdb_printf("C"); 3614 else 3615 mdb_printf("-"); 3616 if (access_mask & ACE_WRITE_OWNER) 3617 mdb_printf("o"); 3618 else 3619 mdb_printf("-"); 3620 if (access_mask & ACE_SYNCHRONIZE) 3621 mdb_printf("s"); 3622 else 3623 mdb_printf("-"); 3624 3625 mdb_printf(":"); 3626 3627 /* Print out inheritance flags */ 3628 if (ace_flags & ACE_FILE_INHERIT_ACE) 3629 mdb_printf("f"); 3630 else 3631 mdb_printf("-"); 3632 if (ace_flags & ACE_DIRECTORY_INHERIT_ACE) 3633 mdb_printf("d"); 3634 else 3635 mdb_printf("-"); 3636 if (ace_flags & ACE_INHERIT_ONLY_ACE) 3637 mdb_printf("i"); 3638 else 3639 mdb_printf("-"); 3640 if (ace_flags & ACE_NO_PROPAGATE_INHERIT_ACE) 3641 mdb_printf("n"); 3642 else 3643 mdb_printf("-"); 3644 if (ace_flags & ACE_SUCCESSFUL_ACCESS_ACE_FLAG) 3645 mdb_printf("S"); 3646 else 3647 mdb_printf("-"); 3648 if (ace_flags & ACE_FAILED_ACCESS_ACE_FLAG) 3649 mdb_printf("F"); 3650 else 3651 mdb_printf("-"); 3652 if (ace_flags & ACE_INHERITED_ACE) 3653 mdb_printf("I"); 3654 else 3655 mdb_printf("-"); 3656 3657 switch (ace_type) { 3658 case ACE_ACCESS_ALLOWED_ACE_TYPE: 3659 mdb_printf(":allow\n"); 3660 break; 3661 case ACE_ACCESS_DENIED_ACE_TYPE: 3662 mdb_printf(":deny\n"); 3663 break; 3664 case ACE_SYSTEM_AUDIT_ACE_TYPE: 3665 mdb_printf(":audit\n"); 3666 break; 3667 case ACE_SYSTEM_ALARM_ACE_TYPE: 3668 mdb_printf(":alarm\n"); 3669 break; 3670 default: 3671 mdb_printf(":?\n"); 3672 } 3673 return (DCMD_OK); 3674 } 3675 3676 /* ARGSUSED */ 3677 static int 3678 zfs_ace_print(uintptr_t addr, uint_t flags, int argc, const mdb_arg_t *argv) 3679 { 3680 zfs_ace_t zace; 3681 int verbose = FALSE; 3682 uint64_t id; 3683 3684 if (!(flags & DCMD_ADDRSPEC)) 3685 return (DCMD_USAGE); 3686 3687 if (mdb_getopts(argc, argv, 3688 'v', MDB_OPT_SETBITS, TRUE, &verbose, TRUE, NULL) != argc) 3689 return (DCMD_USAGE); 3690 3691 if (mdb_vread(&zace, sizeof (zfs_ace_t), addr) == -1) { 3692 mdb_warn("failed to read zfs_ace_t"); 3693 return (DCMD_ERR); 3694 } 3695 3696 if ((zace.z_hdr.z_flags & ACE_TYPE_FLAGS) == 0 || 3697 (zace.z_hdr.z_flags & ACE_TYPE_FLAGS) == ACE_IDENTIFIER_GROUP) 3698 id = zace.z_fuid; 3699 else 3700 id = -1; 3701 3702 return (zfs_ace_print_common(addr, flags, id, zace.z_hdr.z_access_mask, 3703 zace.z_hdr.z_flags, zace.z_hdr.z_type, verbose)); 3704 } 3705 3706 /* ARGSUSED */ 3707 static int 3708 zfs_ace0_print(uintptr_t addr, uint_t flags, int argc, const mdb_arg_t *argv) 3709 { 3710 ace_t ace; 3711 uint64_t id; 3712 int verbose = FALSE; 3713 3714 if (!(flags & DCMD_ADDRSPEC)) 3715 return (DCMD_USAGE); 3716 3717 if (mdb_getopts(argc, argv, 3718 'v', MDB_OPT_SETBITS, TRUE, &verbose, TRUE, NULL) != argc) 3719 return (DCMD_USAGE); 3720 3721 if (mdb_vread(&ace, sizeof (ace_t), addr) == -1) { 3722 mdb_warn("failed to read ace_t"); 3723 return (DCMD_ERR); 3724 } 3725 3726 if ((ace.a_flags & ACE_TYPE_FLAGS) == 0 || 3727 (ace.a_flags & ACE_TYPE_FLAGS) == ACE_IDENTIFIER_GROUP) 3728 id = ace.a_who; 3729 else 3730 id = -1; 3731 3732 return (zfs_ace_print_common(addr, flags, id, ace.a_access_mask, 3733 ace.a_flags, ace.a_type, verbose)); 3734 } 3735 3736 typedef struct acl_dump_args { 3737 int a_argc; 3738 const mdb_arg_t *a_argv; 3739 uint16_t a_version; 3740 int a_flags; 3741 } acl_dump_args_t; 3742 3743 /* ARGSUSED */ 3744 static int 3745 acl_aces_cb(uintptr_t addr, const void *unknown, void *arg) 3746 { 3747 acl_dump_args_t *acl_args = (acl_dump_args_t *)arg; 3748 3749 if (acl_args->a_version == 1) { 3750 if (mdb_call_dcmd("zfs_ace", addr, 3751 DCMD_ADDRSPEC|acl_args->a_flags, acl_args->a_argc, 3752 acl_args->a_argv) != DCMD_OK) { 3753 return (WALK_ERR); 3754 } 3755 } else { 3756 if (mdb_call_dcmd("zfs_ace0", addr, 3757 DCMD_ADDRSPEC|acl_args->a_flags, acl_args->a_argc, 3758 acl_args->a_argv) != DCMD_OK) { 3759 return (WALK_ERR); 3760 } 3761 } 3762 acl_args->a_flags = DCMD_LOOP; 3763 return (WALK_NEXT); 3764 } 3765 3766 /* ARGSUSED */ 3767 static int 3768 acl_cb(uintptr_t addr, const void *unknown, void *arg) 3769 { 3770 acl_dump_args_t *acl_args = (acl_dump_args_t *)arg; 3771 3772 if (acl_args->a_version == 1) { 3773 if (mdb_pwalk("zfs_acl_node_aces", acl_aces_cb, 3774 arg, addr) != 0) { 3775 mdb_warn("can't walk ACEs"); 3776 return (DCMD_ERR); 3777 } 3778 } else { 3779 if (mdb_pwalk("zfs_acl_node_aces0", acl_aces_cb, 3780 arg, addr) != 0) { 3781 mdb_warn("can't walk ACEs"); 3782 return (DCMD_ERR); 3783 } 3784 } 3785 return (WALK_NEXT); 3786 } 3787 3788 /* ARGSUSED */ 3789 static int 3790 zfs_acl_dump(uintptr_t addr, uint_t flags, int argc, const mdb_arg_t *argv) 3791 { 3792 zfs_acl_t zacl; 3793 int verbose = FALSE; 3794 acl_dump_args_t acl_args; 3795 3796 if (!(flags & DCMD_ADDRSPEC)) 3797 return (DCMD_USAGE); 3798 3799 if (mdb_getopts(argc, argv, 3800 'v', MDB_OPT_SETBITS, TRUE, &verbose, NULL) != argc) 3801 return (DCMD_USAGE); 3802 3803 if (mdb_vread(&zacl, sizeof (zfs_acl_t), addr) == -1) { 3804 mdb_warn("failed to read zfs_acl_t"); 3805 return (DCMD_ERR); 3806 } 3807 3808 acl_args.a_argc = argc; 3809 acl_args.a_argv = argv; 3810 acl_args.a_version = zacl.z_version; 3811 acl_args.a_flags = DCMD_LOOPFIRST; 3812 3813 if (mdb_pwalk("zfs_acl_node", acl_cb, &acl_args, addr) != 0) { 3814 mdb_warn("can't walk ACL"); 3815 return (DCMD_ERR); 3816 } 3817 3818 return (DCMD_OK); 3819 } 3820 3821 /* ARGSUSED */ 3822 static int 3823 zfs_acl_node_walk_init(mdb_walk_state_t *wsp) 3824 { 3825 if (wsp->walk_addr == 0) { 3826 mdb_warn("must supply address of zfs_acl_node_t\n"); 3827 return (WALK_ERR); 3828 } 3829 3830 wsp->walk_addr += 3831 mdb_ctf_offsetof_by_name(ZFS_STRUCT "zfs_acl", "z_acl"); 3832 3833 if (mdb_layered_walk("list", wsp) == -1) { 3834 mdb_warn("failed to walk 'list'\n"); 3835 return (WALK_ERR); 3836 } 3837 3838 return (WALK_NEXT); 3839 } 3840 3841 static int 3842 zfs_acl_node_walk_step(mdb_walk_state_t *wsp) 3843 { 3844 zfs_acl_node_t aclnode; 3845 3846 if (mdb_vread(&aclnode, sizeof (zfs_acl_node_t), 3847 wsp->walk_addr) == -1) { 3848 mdb_warn("failed to read zfs_acl_node at %p", wsp->walk_addr); 3849 return (WALK_ERR); 3850 } 3851 3852 return (wsp->walk_callback(wsp->walk_addr, &aclnode, wsp->walk_cbdata)); 3853 } 3854 3855 typedef struct ace_walk_data { 3856 int ace_count; 3857 int ace_version; 3858 } ace_walk_data_t; 3859 3860 static int 3861 zfs_aces_walk_init_common(mdb_walk_state_t *wsp, int version, 3862 int ace_count, uintptr_t ace_data) 3863 { 3864 ace_walk_data_t *ace_walk_data; 3865 3866 if (wsp->walk_addr == 0) { 3867 mdb_warn("must supply address of zfs_acl_node_t\n"); 3868 return (WALK_ERR); 3869 } 3870 3871 ace_walk_data = mdb_alloc(sizeof (ace_walk_data_t), UM_SLEEP | UM_GC); 3872 3873 ace_walk_data->ace_count = ace_count; 3874 ace_walk_data->ace_version = version; 3875 3876 wsp->walk_addr = ace_data; 3877 wsp->walk_data = ace_walk_data; 3878 3879 return (WALK_NEXT); 3880 } 3881 3882 static int 3883 zfs_acl_node_aces_walk_init_common(mdb_walk_state_t *wsp, int version) 3884 { 3885 static int gotid; 3886 static mdb_ctf_id_t acl_id; 3887 int z_ace_count; 3888 uintptr_t z_acldata; 3889 3890 if (!gotid) { 3891 if (mdb_ctf_lookup_by_name("struct zfs_acl_node", 3892 &acl_id) == -1) { 3893 mdb_warn("couldn't find struct zfs_acl_node"); 3894 return (DCMD_ERR); 3895 } 3896 gotid = TRUE; 3897 } 3898 3899 if (GETMEMBID(wsp->walk_addr, &acl_id, z_ace_count, z_ace_count)) { 3900 return (DCMD_ERR); 3901 } 3902 if (GETMEMBID(wsp->walk_addr, &acl_id, z_acldata, z_acldata)) { 3903 return (DCMD_ERR); 3904 } 3905 3906 return (zfs_aces_walk_init_common(wsp, version, 3907 z_ace_count, z_acldata)); 3908 } 3909 3910 /* ARGSUSED */ 3911 static int 3912 zfs_acl_node_aces_walk_init(mdb_walk_state_t *wsp) 3913 { 3914 return (zfs_acl_node_aces_walk_init_common(wsp, 1)); 3915 } 3916 3917 /* ARGSUSED */ 3918 static int 3919 zfs_acl_node_aces0_walk_init(mdb_walk_state_t *wsp) 3920 { 3921 return (zfs_acl_node_aces_walk_init_common(wsp, 0)); 3922 } 3923 3924 static int 3925 zfs_aces_walk_step(mdb_walk_state_t *wsp) 3926 { 3927 ace_walk_data_t *ace_data = wsp->walk_data; 3928 zfs_ace_t zace; 3929 ace_t *acep; 3930 int status; 3931 int entry_type; 3932 int allow_type; 3933 uintptr_t ptr; 3934 3935 if (ace_data->ace_count == 0) 3936 return (WALK_DONE); 3937 3938 if (mdb_vread(&zace, sizeof (zfs_ace_t), wsp->walk_addr) == -1) { 3939 mdb_warn("failed to read zfs_ace_t at %#lx", 3940 wsp->walk_addr); 3941 return (WALK_ERR); 3942 } 3943 3944 switch (ace_data->ace_version) { 3945 case 0: 3946 acep = (ace_t *)&zace; 3947 entry_type = acep->a_flags & ACE_TYPE_FLAGS; 3948 allow_type = acep->a_type; 3949 break; 3950 case 1: 3951 entry_type = zace.z_hdr.z_flags & ACE_TYPE_FLAGS; 3952 allow_type = zace.z_hdr.z_type; 3953 break; 3954 default: 3955 return (WALK_ERR); 3956 } 3957 3958 ptr = (uintptr_t)wsp->walk_addr; 3959 switch (entry_type) { 3960 case ACE_OWNER: 3961 case ACE_EVERYONE: 3962 case (ACE_IDENTIFIER_GROUP | ACE_GROUP): 3963 ptr += ace_data->ace_version == 0 ? 3964 sizeof (ace_t) : sizeof (zfs_ace_hdr_t); 3965 break; 3966 case ACE_IDENTIFIER_GROUP: 3967 default: 3968 switch (allow_type) { 3969 case ACE_ACCESS_ALLOWED_OBJECT_ACE_TYPE: 3970 case ACE_ACCESS_DENIED_OBJECT_ACE_TYPE: 3971 case ACE_SYSTEM_AUDIT_OBJECT_ACE_TYPE: 3972 case ACE_SYSTEM_ALARM_OBJECT_ACE_TYPE: 3973 ptr += ace_data->ace_version == 0 ? 3974 sizeof (ace_t) : sizeof (zfs_object_ace_t); 3975 break; 3976 default: 3977 ptr += ace_data->ace_version == 0 ? 3978 sizeof (ace_t) : sizeof (zfs_ace_t); 3979 break; 3980 } 3981 } 3982 3983 ace_data->ace_count--; 3984 status = wsp->walk_callback(wsp->walk_addr, 3985 (void *)(uintptr_t)&zace, wsp->walk_cbdata); 3986 3987 wsp->walk_addr = ptr; 3988 return (status); 3989 } 3990 3991 typedef struct mdb_zfs_rrwlock { 3992 uintptr_t rr_writer; 3993 boolean_t rr_writer_wanted; 3994 } mdb_zfs_rrwlock_t; 3995 3996 static uint_t rrw_key; 3997 3998 /* ARGSUSED */ 3999 static int 4000 rrwlock(uintptr_t addr, uint_t flags, int argc, const mdb_arg_t *argv) 4001 { 4002 mdb_zfs_rrwlock_t rrw; 4003 4004 if (rrw_key == 0) { 4005 if (mdb_ctf_readsym(&rrw_key, "uint_t", "rrw_tsd_key", 0) == -1) 4006 return (DCMD_ERR); 4007 } 4008 4009 if (mdb_ctf_vread(&rrw, "rrwlock_t", "mdb_zfs_rrwlock_t", addr, 4010 0) == -1) 4011 return (DCMD_ERR); 4012 4013 if (rrw.rr_writer != 0) { 4014 mdb_printf("write lock held by thread %lx\n", rrw.rr_writer); 4015 return (DCMD_OK); 4016 } 4017 4018 if (rrw.rr_writer_wanted) { 4019 mdb_printf("writer wanted\n"); 4020 } 4021 4022 mdb_printf("anonymous references:\n"); 4023 (void) mdb_call_dcmd("zfs_refcount", addr + 4024 mdb_ctf_offsetof_by_name(ZFS_STRUCT "rrwlock", "rr_anon_rcount"), 4025 DCMD_ADDRSPEC, 0, NULL); 4026 4027 mdb_printf("linked references:\n"); 4028 (void) mdb_call_dcmd("zfs_refcount", addr + 4029 mdb_ctf_offsetof_by_name(ZFS_STRUCT "rrwlock", "rr_linked_rcount"), 4030 DCMD_ADDRSPEC, 0, NULL); 4031 4032 /* 4033 * XXX This should find references from 4034 * "::walk thread | ::tsd -v <rrw_key>", but there is no support 4035 * for programmatic consumption of dcmds, so this would be 4036 * difficult, potentially requiring reimplementing ::tsd (both 4037 * user and kernel versions) in this MDB module. 4038 */ 4039 4040 return (DCMD_OK); 4041 } 4042 4043 typedef struct mdb_arc_buf_hdr_t { 4044 uint16_t b_psize; 4045 uint16_t b_lsize; 4046 struct { 4047 uint32_t b_bufcnt; 4048 uintptr_t b_state; 4049 } b_l1hdr; 4050 } mdb_arc_buf_hdr_t; 4051 4052 enum arc_cflags { 4053 ARC_CFLAG_VERBOSE = 1 << 0, 4054 ARC_CFLAG_ANON = 1 << 1, 4055 ARC_CFLAG_MRU = 1 << 2, 4056 ARC_CFLAG_MFU = 1 << 3, 4057 ARC_CFLAG_BUFS = 1 << 4, 4058 }; 4059 4060 typedef struct arc_compression_stats_data { 4061 GElf_Sym anon_sym; /* ARC_anon symbol */ 4062 GElf_Sym mru_sym; /* ARC_mru symbol */ 4063 GElf_Sym mrug_sym; /* ARC_mru_ghost symbol */ 4064 GElf_Sym mfu_sym; /* ARC_mfu symbol */ 4065 GElf_Sym mfug_sym; /* ARC_mfu_ghost symbol */ 4066 GElf_Sym l2c_sym; /* ARC_l2c_only symbol */ 4067 uint64_t *anon_c_hist; /* histogram of compressed sizes in anon */ 4068 uint64_t *anon_u_hist; /* histogram of uncompressed sizes in anon */ 4069 uint64_t *anon_bufs; /* histogram of buffer counts in anon state */ 4070 uint64_t *mru_c_hist; /* histogram of compressed sizes in mru */ 4071 uint64_t *mru_u_hist; /* histogram of uncompressed sizes in mru */ 4072 uint64_t *mru_bufs; /* histogram of buffer counts in mru */ 4073 uint64_t *mfu_c_hist; /* histogram of compressed sizes in mfu */ 4074 uint64_t *mfu_u_hist; /* histogram of uncompressed sizes in mfu */ 4075 uint64_t *mfu_bufs; /* histogram of buffer counts in mfu */ 4076 uint64_t *all_c_hist; /* histogram of compressed anon + mru + mfu */ 4077 uint64_t *all_u_hist; /* histogram of uncompressed anon + mru + mfu */ 4078 uint64_t *all_bufs; /* histogram of buffer counts in all states */ 4079 int arc_cflags; /* arc compression flags, specified by user */ 4080 int hist_nbuckets; /* number of buckets in each histogram */ 4081 4082 ulong_t l1hdr_off; /* offset of b_l1hdr in arc_buf_hdr_t */ 4083 } arc_compression_stats_data_t; 4084 4085 int 4086 highbit64(uint64_t i) 4087 { 4088 int h = 1; 4089 4090 if (i == 0) 4091 return (0); 4092 if (i & 0xffffffff00000000ULL) { 4093 h += 32; i >>= 32; 4094 } 4095 if (i & 0xffff0000) { 4096 h += 16; i >>= 16; 4097 } 4098 if (i & 0xff00) { 4099 h += 8; i >>= 8; 4100 } 4101 if (i & 0xf0) { 4102 h += 4; i >>= 4; 4103 } 4104 if (i & 0xc) { 4105 h += 2; i >>= 2; 4106 } 4107 if (i & 0x2) { 4108 h += 1; 4109 } 4110 return (h); 4111 } 4112 4113 /* ARGSUSED */ 4114 static int 4115 arc_compression_stats_cb(uintptr_t addr, const void *unknown, void *arg) 4116 { 4117 arc_compression_stats_data_t *data = arg; 4118 arc_flags_t flags; 4119 mdb_arc_buf_hdr_t hdr; 4120 int cbucket, ubucket, bufcnt; 4121 4122 /* 4123 * mdb_ctf_vread() uses the sizeof the target type (e.g. 4124 * sizeof (arc_buf_hdr_t) in the target) to read in the entire contents 4125 * of the target type into a buffer and then copy the values of the 4126 * desired members from the mdb typename (e.g. mdb_arc_buf_hdr_t) from 4127 * this buffer. Unfortunately, the way arc_buf_hdr_t is used by zfs, 4128 * the actual size allocated by the kernel for arc_buf_hdr_t is often 4129 * smaller than `sizeof (arc_buf_hdr_t)` (see the definitions of 4130 * l1arc_buf_hdr_t and arc_buf_hdr_t in 4131 * usr/src/uts/common/fs/zfs/arc.c). Attempting to read the entire 4132 * contents of arc_buf_hdr_t from the target (as mdb_ctf_vread() does) 4133 * can cause an error if the allocated size is indeed smaller--it's 4134 * possible that the 'missing' trailing members of arc_buf_hdr_t 4135 * (l1arc_buf_hdr_t and/or arc_buf_hdr_crypt_t) may fall into unmapped 4136 * memory. 4137 * 4138 * We use the GETMEMB macro instead which performs an mdb_vread() 4139 * but only reads enough of the target to retrieve the desired struct 4140 * member instead of the entire struct. 4141 */ 4142 if (GETMEMB(addr, "arc_buf_hdr", b_flags, flags) == -1) 4143 return (WALK_ERR); 4144 4145 /* 4146 * We only count headers that have data loaded in the kernel. 4147 * This means an L1 header must be present as well as the data 4148 * that corresponds to the L1 header. If there's no L1 header, 4149 * we can skip the arc_buf_hdr_t completely. If it's present, we 4150 * must look at the ARC state (b_l1hdr.b_state) to determine if 4151 * the data is present. 4152 */ 4153 if ((flags & ARC_FLAG_HAS_L1HDR) == 0) 4154 return (WALK_NEXT); 4155 4156 if (GETMEMB(addr, "arc_buf_hdr", b_psize, hdr.b_psize) == -1 || 4157 GETMEMB(addr, "arc_buf_hdr", b_lsize, hdr.b_lsize) == -1 || 4158 GETMEMB(addr + data->l1hdr_off, "l1arc_buf_hdr", b_bufcnt, 4159 hdr.b_l1hdr.b_bufcnt) == -1 || 4160 GETMEMB(addr + data->l1hdr_off, "l1arc_buf_hdr", b_state, 4161 hdr.b_l1hdr.b_state) == -1) 4162 return (WALK_ERR); 4163 4164 /* 4165 * Headers in the ghost states, or the l2c_only state don't have 4166 * arc buffers linked off of them. Thus, their compressed size 4167 * is meaningless, so we skip these from the stats. 4168 */ 4169 if (hdr.b_l1hdr.b_state == data->mrug_sym.st_value || 4170 hdr.b_l1hdr.b_state == data->mfug_sym.st_value || 4171 hdr.b_l1hdr.b_state == data->l2c_sym.st_value) { 4172 return (WALK_NEXT); 4173 } 4174 4175 /* 4176 * The physical size (compressed) and logical size 4177 * (uncompressed) are in units of SPA_MINBLOCKSIZE. By default, 4178 * we use the log2 of this value (rounded down to the nearest 4179 * integer) to determine the bucket to assign this header to. 4180 * Thus, the histogram is logarithmic with respect to the size 4181 * of the header. For example, the following is a mapping of the 4182 * bucket numbers and the range of header sizes they correspond to: 4183 * 4184 * 0: 0 byte headers 4185 * 1: 512 byte headers 4186 * 2: [1024 - 2048) byte headers 4187 * 3: [2048 - 4096) byte headers 4188 * 4: [4096 - 8192) byte headers 4189 * 5: [8192 - 16394) byte headers 4190 * 6: [16384 - 32768) byte headers 4191 * 7: [32768 - 65536) byte headers 4192 * 8: [65536 - 131072) byte headers 4193 * 9: 131072 byte headers 4194 * 4195 * If the ARC_CFLAG_VERBOSE flag was specified, we use the 4196 * physical and logical sizes directly. Thus, the histogram will 4197 * no longer be logarithmic; instead it will be linear with 4198 * respect to the size of the header. The following is a mapping 4199 * of the first many bucket numbers and the header size they 4200 * correspond to: 4201 * 4202 * 0: 0 byte headers 4203 * 1: 512 byte headers 4204 * 2: 1024 byte headers 4205 * 3: 1536 byte headers 4206 * 4: 2048 byte headers 4207 * 5: 2560 byte headers 4208 * 6: 3072 byte headers 4209 * 4210 * And so on. Keep in mind that a range of sizes isn't used in 4211 * the case of linear scale because the headers can only 4212 * increment or decrement in sizes of 512 bytes. So, it's not 4213 * possible for a header to be sized in between whats listed 4214 * above. 4215 * 4216 * Also, the above mapping values were calculated assuming a 4217 * SPA_MINBLOCKSHIFT of 512 bytes and a SPA_MAXBLOCKSIZE of 128K. 4218 */ 4219 4220 if (data->arc_cflags & ARC_CFLAG_VERBOSE) { 4221 cbucket = hdr.b_psize; 4222 ubucket = hdr.b_lsize; 4223 } else { 4224 cbucket = highbit64(hdr.b_psize); 4225 ubucket = highbit64(hdr.b_lsize); 4226 } 4227 4228 bufcnt = hdr.b_l1hdr.b_bufcnt; 4229 if (bufcnt >= data->hist_nbuckets) 4230 bufcnt = data->hist_nbuckets - 1; 4231 4232 /* Ensure we stay within the bounds of the histogram array */ 4233 ASSERT3U(cbucket, <, data->hist_nbuckets); 4234 ASSERT3U(ubucket, <, data->hist_nbuckets); 4235 4236 if (hdr.b_l1hdr.b_state == data->anon_sym.st_value) { 4237 data->anon_c_hist[cbucket]++; 4238 data->anon_u_hist[ubucket]++; 4239 data->anon_bufs[bufcnt]++; 4240 } else if (hdr.b_l1hdr.b_state == data->mru_sym.st_value) { 4241 data->mru_c_hist[cbucket]++; 4242 data->mru_u_hist[ubucket]++; 4243 data->mru_bufs[bufcnt]++; 4244 } else if (hdr.b_l1hdr.b_state == data->mfu_sym.st_value) { 4245 data->mfu_c_hist[cbucket]++; 4246 data->mfu_u_hist[ubucket]++; 4247 data->mfu_bufs[bufcnt]++; 4248 } 4249 4250 data->all_c_hist[cbucket]++; 4251 data->all_u_hist[ubucket]++; 4252 data->all_bufs[bufcnt]++; 4253 4254 return (WALK_NEXT); 4255 } 4256 4257 /* ARGSUSED */ 4258 static int 4259 arc_compression_stats(uintptr_t addr, uint_t flags, int argc, 4260 const mdb_arg_t *argv) 4261 { 4262 arc_compression_stats_data_t data = { 0 }; 4263 unsigned int max_shifted = SPA_MAXBLOCKSIZE >> SPA_MINBLOCKSHIFT; 4264 unsigned int hist_size; 4265 char range[32]; 4266 int rc = DCMD_OK; 4267 int off; 4268 4269 if (mdb_getopts(argc, argv, 4270 'v', MDB_OPT_SETBITS, ARC_CFLAG_VERBOSE, &data.arc_cflags, 4271 'a', MDB_OPT_SETBITS, ARC_CFLAG_ANON, &data.arc_cflags, 4272 'b', MDB_OPT_SETBITS, ARC_CFLAG_BUFS, &data.arc_cflags, 4273 'r', MDB_OPT_SETBITS, ARC_CFLAG_MRU, &data.arc_cflags, 4274 'f', MDB_OPT_SETBITS, ARC_CFLAG_MFU, &data.arc_cflags, 4275 NULL) != argc) 4276 return (DCMD_USAGE); 4277 4278 if (mdb_lookup_by_obj(ZFS_OBJ_NAME, "ARC_anon", &data.anon_sym) || 4279 mdb_lookup_by_obj(ZFS_OBJ_NAME, "ARC_mru", &data.mru_sym) || 4280 mdb_lookup_by_obj(ZFS_OBJ_NAME, "ARC_mru_ghost", &data.mrug_sym) || 4281 mdb_lookup_by_obj(ZFS_OBJ_NAME, "ARC_mfu", &data.mfu_sym) || 4282 mdb_lookup_by_obj(ZFS_OBJ_NAME, "ARC_mfu_ghost", &data.mfug_sym) || 4283 mdb_lookup_by_obj(ZFS_OBJ_NAME, "ARC_l2c_only", &data.l2c_sym)) { 4284 mdb_warn("can't find arc state symbol"); 4285 return (DCMD_ERR); 4286 } 4287 4288 /* 4289 * Determine the maximum expected size for any header, and use 4290 * this to determine the number of buckets needed for each 4291 * histogram. If ARC_CFLAG_VERBOSE is specified, this value is 4292 * used directly; otherwise the log2 of the maximum size is 4293 * used. Thus, if using a log2 scale there's a maximum of 10 4294 * possible buckets, while the linear scale (when using 4295 * ARC_CFLAG_VERBOSE) has a maximum of 257 buckets. 4296 */ 4297 if (data.arc_cflags & ARC_CFLAG_VERBOSE) 4298 data.hist_nbuckets = max_shifted + 1; 4299 else 4300 data.hist_nbuckets = highbit64(max_shifted) + 1; 4301 4302 hist_size = sizeof (uint64_t) * data.hist_nbuckets; 4303 4304 data.anon_c_hist = mdb_zalloc(hist_size, UM_SLEEP); 4305 data.anon_u_hist = mdb_zalloc(hist_size, UM_SLEEP); 4306 data.anon_bufs = mdb_zalloc(hist_size, UM_SLEEP); 4307 4308 data.mru_c_hist = mdb_zalloc(hist_size, UM_SLEEP); 4309 data.mru_u_hist = mdb_zalloc(hist_size, UM_SLEEP); 4310 data.mru_bufs = mdb_zalloc(hist_size, UM_SLEEP); 4311 4312 data.mfu_c_hist = mdb_zalloc(hist_size, UM_SLEEP); 4313 data.mfu_u_hist = mdb_zalloc(hist_size, UM_SLEEP); 4314 data.mfu_bufs = mdb_zalloc(hist_size, UM_SLEEP); 4315 4316 data.all_c_hist = mdb_zalloc(hist_size, UM_SLEEP); 4317 data.all_u_hist = mdb_zalloc(hist_size, UM_SLEEP); 4318 data.all_bufs = mdb_zalloc(hist_size, UM_SLEEP); 4319 4320 if ((off = mdb_ctf_offsetof_by_name(ZFS_STRUCT "arc_buf_hdr", 4321 "b_l1hdr")) == -1) { 4322 mdb_warn("could not get offset of b_l1hdr from arc_buf_hdr_t"); 4323 rc = DCMD_ERR; 4324 goto out; 4325 } 4326 data.l1hdr_off = off; 4327 4328 if (mdb_walk("arc_buf_hdr_t_full", arc_compression_stats_cb, 4329 &data) != 0) { 4330 mdb_warn("can't walk arc_buf_hdr's"); 4331 rc = DCMD_ERR; 4332 goto out; 4333 } 4334 4335 if (data.arc_cflags & ARC_CFLAG_VERBOSE) { 4336 rc = mdb_snprintf(range, sizeof (range), 4337 "[n*%llu, (n+1)*%llu)", SPA_MINBLOCKSIZE, 4338 SPA_MINBLOCKSIZE); 4339 } else { 4340 rc = mdb_snprintf(range, sizeof (range), 4341 "[2^(n-1)*%llu, 2^n*%llu)", SPA_MINBLOCKSIZE, 4342 SPA_MINBLOCKSIZE); 4343 } 4344 4345 if (rc < 0) { 4346 /* snprintf failed, abort the dcmd */ 4347 rc = DCMD_ERR; 4348 goto out; 4349 } else { 4350 /* snprintf succeeded above, reset return code */ 4351 rc = DCMD_OK; 4352 } 4353 4354 if (data.arc_cflags & ARC_CFLAG_ANON) { 4355 if (data.arc_cflags & ARC_CFLAG_BUFS) { 4356 mdb_printf("Histogram of the number of anon buffers " 4357 "that are associated with an arc hdr.\n"); 4358 dump_histogram(data.anon_bufs, data.hist_nbuckets, 0); 4359 mdb_printf("\n"); 4360 } 4361 mdb_printf("Histogram of compressed anon buffers.\n" 4362 "Each bucket represents buffers of size: %s.\n", range); 4363 dump_histogram(data.anon_c_hist, data.hist_nbuckets, 0); 4364 mdb_printf("\n"); 4365 4366 mdb_printf("Histogram of uncompressed anon buffers.\n" 4367 "Each bucket represents buffers of size: %s.\n", range); 4368 dump_histogram(data.anon_u_hist, data.hist_nbuckets, 0); 4369 mdb_printf("\n"); 4370 } 4371 4372 if (data.arc_cflags & ARC_CFLAG_MRU) { 4373 if (data.arc_cflags & ARC_CFLAG_BUFS) { 4374 mdb_printf("Histogram of the number of mru buffers " 4375 "that are associated with an arc hdr.\n"); 4376 dump_histogram(data.mru_bufs, data.hist_nbuckets, 0); 4377 mdb_printf("\n"); 4378 } 4379 mdb_printf("Histogram of compressed mru buffers.\n" 4380 "Each bucket represents buffers of size: %s.\n", range); 4381 dump_histogram(data.mru_c_hist, data.hist_nbuckets, 0); 4382 mdb_printf("\n"); 4383 4384 mdb_printf("Histogram of uncompressed mru buffers.\n" 4385 "Each bucket represents buffers of size: %s.\n", range); 4386 dump_histogram(data.mru_u_hist, data.hist_nbuckets, 0); 4387 mdb_printf("\n"); 4388 } 4389 4390 if (data.arc_cflags & ARC_CFLAG_MFU) { 4391 if (data.arc_cflags & ARC_CFLAG_BUFS) { 4392 mdb_printf("Histogram of the number of mfu buffers " 4393 "that are associated with an arc hdr.\n"); 4394 dump_histogram(data.mfu_bufs, data.hist_nbuckets, 0); 4395 mdb_printf("\n"); 4396 } 4397 4398 mdb_printf("Histogram of compressed mfu buffers.\n" 4399 "Each bucket represents buffers of size: %s.\n", range); 4400 dump_histogram(data.mfu_c_hist, data.hist_nbuckets, 0); 4401 mdb_printf("\n"); 4402 4403 mdb_printf("Histogram of uncompressed mfu buffers.\n" 4404 "Each bucket represents buffers of size: %s.\n", range); 4405 dump_histogram(data.mfu_u_hist, data.hist_nbuckets, 0); 4406 mdb_printf("\n"); 4407 } 4408 4409 if (data.arc_cflags & ARC_CFLAG_BUFS) { 4410 mdb_printf("Histogram of all buffers that " 4411 "are associated with an arc hdr.\n"); 4412 dump_histogram(data.all_bufs, data.hist_nbuckets, 0); 4413 mdb_printf("\n"); 4414 } 4415 4416 mdb_printf("Histogram of all compressed buffers.\n" 4417 "Each bucket represents buffers of size: %s.\n", range); 4418 dump_histogram(data.all_c_hist, data.hist_nbuckets, 0); 4419 mdb_printf("\n"); 4420 4421 mdb_printf("Histogram of all uncompressed buffers.\n" 4422 "Each bucket represents buffers of size: %s.\n", range); 4423 dump_histogram(data.all_u_hist, data.hist_nbuckets, 0); 4424 4425 out: 4426 mdb_free(data.anon_c_hist, hist_size); 4427 mdb_free(data.anon_u_hist, hist_size); 4428 mdb_free(data.anon_bufs, hist_size); 4429 4430 mdb_free(data.mru_c_hist, hist_size); 4431 mdb_free(data.mru_u_hist, hist_size); 4432 mdb_free(data.mru_bufs, hist_size); 4433 4434 mdb_free(data.mfu_c_hist, hist_size); 4435 mdb_free(data.mfu_u_hist, hist_size); 4436 mdb_free(data.mfu_bufs, hist_size); 4437 4438 mdb_free(data.all_c_hist, hist_size); 4439 mdb_free(data.all_u_hist, hist_size); 4440 mdb_free(data.all_bufs, hist_size); 4441 4442 return (rc); 4443 } 4444 4445 typedef struct mdb_range_seg64 { 4446 uint64_t rs_start; 4447 uint64_t rs_end; 4448 } mdb_range_seg64_t; 4449 4450 typedef struct mdb_range_seg32 { 4451 uint32_t rs_start; 4452 uint32_t rs_end; 4453 } mdb_range_seg32_t; 4454 4455 /* ARGSUSED */ 4456 static int 4457 range_tree_cb(uintptr_t addr, const void *unknown, void *arg) 4458 { 4459 mdb_range_tree_t *rt = (mdb_range_tree_t *)arg; 4460 uint64_t start, end; 4461 4462 if (rt->rt_type == RANGE_SEG64) { 4463 mdb_range_seg64_t rs; 4464 4465 if (mdb_ctf_vread(&rs, ZFS_STRUCT "range_seg64", 4466 "mdb_range_seg64_t", addr, 0) == -1) 4467 return (DCMD_ERR); 4468 start = rs.rs_start; 4469 end = rs.rs_end; 4470 } else { 4471 ASSERT3U(rt->rt_type, ==, RANGE_SEG32); 4472 mdb_range_seg32_t rs; 4473 4474 if (mdb_ctf_vread(&rs, ZFS_STRUCT "range_seg32", 4475 "mdb_range_seg32_t", addr, 0) == -1) 4476 return (DCMD_ERR); 4477 start = ((uint64_t)rs.rs_start << rt->rt_shift) + rt->rt_start; 4478 end = ((uint64_t)rs.rs_end << rt->rt_shift) + rt->rt_start; 4479 } 4480 4481 mdb_printf("\t[%llx %llx) (length %llx)\n", start, end, end - start); 4482 4483 return (0); 4484 } 4485 4486 /* ARGSUSED */ 4487 static int 4488 range_tree(uintptr_t addr, uint_t flags, int argc, 4489 const mdb_arg_t *argv) 4490 { 4491 mdb_range_tree_t rt; 4492 uintptr_t btree_addr; 4493 4494 if (!(flags & DCMD_ADDRSPEC)) 4495 return (DCMD_USAGE); 4496 4497 if (mdb_ctf_vread(&rt, ZFS_STRUCT "range_tree", "mdb_range_tree_t", 4498 addr, 0) == -1) 4499 return (DCMD_ERR); 4500 4501 mdb_printf("%p: range tree of %llu entries, %llu bytes\n", 4502 addr, rt.rt_root.bt_num_elems, rt.rt_space); 4503 4504 btree_addr = addr + 4505 mdb_ctf_offsetof_by_name(ZFS_STRUCT "range_tree", "rt_root"); 4506 4507 if (mdb_pwalk("zfs_btree", range_tree_cb, &rt, btree_addr) != 0) { 4508 mdb_warn("can't walk range_tree segments"); 4509 return (DCMD_ERR); 4510 } 4511 return (DCMD_OK); 4512 } 4513 4514 typedef struct mdb_spa_log_sm { 4515 uint64_t sls_sm_obj; 4516 uint64_t sls_txg; 4517 uint64_t sls_nblocks; 4518 uint64_t sls_mscount; 4519 } mdb_spa_log_sm_t; 4520 4521 /* ARGSUSED */ 4522 static int 4523 logsm_stats_cb(uintptr_t addr, const void *unknown, void *arg) 4524 { 4525 mdb_spa_log_sm_t sls; 4526 if (mdb_ctf_vread(&sls, ZFS_STRUCT "spa_log_sm", "mdb_spa_log_sm_t", 4527 addr, 0) == -1) 4528 return (WALK_ERR); 4529 4530 mdb_printf("%7lld %7lld %7lld %7lld\n", 4531 sls.sls_txg, sls.sls_nblocks, sls.sls_mscount, sls.sls_sm_obj); 4532 4533 return (WALK_NEXT); 4534 } 4535 typedef struct mdb_log_summary_entry { 4536 uint64_t lse_start; 4537 uint64_t lse_blkcount; 4538 uint64_t lse_mscount; 4539 } mdb_log_summary_entry_t; 4540 4541 /* ARGSUSED */ 4542 static int 4543 logsm_summary_cb(uintptr_t addr, const void *unknown, void *arg) 4544 { 4545 mdb_log_summary_entry_t lse; 4546 if (mdb_ctf_vread(&lse, ZFS_STRUCT "log_summary_entry", 4547 "mdb_log_summary_entry_t", addr, 0) == -1) 4548 return (WALK_ERR); 4549 4550 mdb_printf("%7lld %7lld %7lld\n", 4551 lse.lse_start, lse.lse_blkcount, lse.lse_mscount); 4552 return (WALK_NEXT); 4553 } 4554 4555 /* ARGSUSED */ 4556 static int 4557 logsm_stats(uintptr_t addr, uint_t flags, int argc, const mdb_arg_t *argv) 4558 { 4559 if (!(flags & DCMD_ADDRSPEC)) 4560 return (DCMD_USAGE); 4561 4562 uintptr_t sls_avl_addr = addr + 4563 mdb_ctf_offsetof_by_name(ZFS_STRUCT "spa", "spa_sm_logs_by_txg"); 4564 uintptr_t summary_addr = addr + 4565 mdb_ctf_offsetof_by_name(ZFS_STRUCT "spa", "spa_log_summary"); 4566 4567 mdb_printf("Log Entries:\n"); 4568 mdb_printf("%7s %7s %7s %7s\n", "txg", "blk", "ms", "obj"); 4569 if (mdb_pwalk("avl", logsm_stats_cb, NULL, sls_avl_addr) != 0) 4570 return (DCMD_ERR); 4571 4572 mdb_printf("\nSummary Entries:\n"); 4573 mdb_printf("%7s %7s %7s\n", "txg", "blk", "ms"); 4574 if (mdb_pwalk("list", logsm_summary_cb, NULL, summary_addr) != 0) 4575 return (DCMD_ERR); 4576 4577 return (DCMD_OK); 4578 } 4579 4580 /* 4581 * MDB module linkage information: 4582 * 4583 * We declare a list of structures describing our dcmds, and a function 4584 * named _mdb_init to return a pointer to our module information. 4585 */ 4586 4587 static const mdb_dcmd_t dcmds[] = { 4588 { "arc", "[-bkmg]", "print ARC variables", arc_print }, 4589 { "blkptr", ":", "print blkptr_t", blkptr }, 4590 { "dva", ":", "print dva_t", dva }, 4591 { "dbuf", ":", "print dmu_buf_impl_t", dbuf }, 4592 { "dbuf_stats", ":", "dbuf stats", dbuf_stats }, 4593 { "dbufs", 4594 "\t[-O objset_t*] [-n objset_name | \"mos\"] " 4595 "[-o object | \"mdn\"] \n" 4596 "\t[-l level] [-b blkid | \"bonus\"]", 4597 "find dmu_buf_impl_t's that match specified criteria", dbufs }, 4598 { "abuf_find", "dva_word[0] dva_word[1]", 4599 "find arc_buf_hdr_t of a specified DVA", 4600 abuf_find }, 4601 { "logsm_stats", ":", "print log space map statistics of a spa_t", 4602 logsm_stats}, 4603 { "spa", "?[-cevmMh]\n" 4604 "\t-c display spa config\n" 4605 "\t-e display vdev statistics\n" 4606 "\t-v display vdev information\n" 4607 "\t-m display metaslab statistics\n" 4608 "\t-M display metaslab group statistics\n" 4609 "\t-h display histogram (requires -m or -M)\n", 4610 "spa_t summary", spa_print }, 4611 { "spa_config", ":", "print spa_t configuration", spa_print_config }, 4612 { "spa_space", ":[-b]", "print spa_t on-disk space usage", spa_space }, 4613 { "spa_vdevs", ":[-emMh]\n" 4614 "\t-e display vdev statistics\n" 4615 "\t-m dispaly metaslab statistics\n" 4616 "\t-M display metaslab group statistic\n" 4617 "\t-h display histogram (requires -m or -M)\n", 4618 "given a spa_t, print vdev summary", spa_vdevs }, 4619 { "sm_entries", "<buffer length in bytes>", 4620 "print out space map entries from a buffer decoded", 4621 sm_entries}, 4622 { "vdev", ":[-remMh]\n" 4623 "\t-r display recursively\n" 4624 "\t-e display statistics\n" 4625 "\t-m display metaslab statistics (top level vdev only)\n" 4626 "\t-M display metaslab group statistics (top level vdev only)\n" 4627 "\t-h display histogram (requires -m or -M)\n", 4628 "vdev_t summary", vdev_print }, 4629 { "zio", ":[-cpr]\n" 4630 "\t-c display children\n" 4631 "\t-p display parents\n" 4632 "\t-r display recursively", 4633 "zio_t summary", zio_print }, 4634 { "zio_state", "?", "print out all zio_t structures on system or " 4635 "for a particular pool", zio_state }, 4636 { "zfs_blkstats", ":[-v]", 4637 "given a spa_t, print block type stats from last scrub", 4638 zfs_blkstats }, 4639 { "zfs_params", "", "print zfs tunable parameters", zfs_params }, 4640 { "zfs_refcount", ":[-r]\n" 4641 "\t-r display recently removed references", 4642 "print zfs_refcount_t holders", zfs_refcount }, 4643 { "zap_leaf", "", "print zap_leaf_phys_t", zap_leaf }, 4644 { "zfs_aces", ":[-v]", "print all ACEs from a zfs_acl_t", 4645 zfs_acl_dump }, 4646 { "zfs_ace", ":[-v]", "print zfs_ace", zfs_ace_print }, 4647 { "zfs_ace0", ":[-v]", "print zfs_ace0", zfs_ace0_print }, 4648 { "sa_attr_table", ":", "print SA attribute table from sa_os_t", 4649 sa_attr_table}, 4650 { "sa_attr", ": attr_id", 4651 "print SA attribute address when given sa_handle_t", sa_attr_print}, 4652 { "zfs_dbgmsg", ":[-var]", 4653 "print zfs debug log", dbgmsg}, 4654 { "rrwlock", ":", 4655 "print rrwlock_t, including readers", rrwlock}, 4656 { "metaslab_weight", "weight", 4657 "print metaslab weight", metaslab_weight}, 4658 { "metaslab_trace", ":", 4659 "print metaslab allocation trace records", metaslab_trace}, 4660 { "arc_compression_stats", ":[-vabrf]\n" 4661 "\t-v verbose, display a linearly scaled histogram\n" 4662 "\t-a display ARC_anon state statistics individually\n" 4663 "\t-r display ARC_mru state statistics individually\n" 4664 "\t-f display ARC_mfu state statistics individually\n" 4665 "\t-b display histogram of buffer counts\n", 4666 "print a histogram of compressed arc buffer sizes", 4667 arc_compression_stats}, 4668 { "range_tree", ":", 4669 "print entries in range_tree_t", range_tree}, 4670 { NULL } 4671 }; 4672 4673 static const mdb_walker_t walkers[] = { 4674 { "txg_list", "given any txg_list_t *, walk all entries in all txgs", 4675 txg_list_walk_init, txg_list_walk_step, NULL }, 4676 { "txg_list0", "given any txg_list_t *, walk all entries in txg 0", 4677 txg_list0_walk_init, txg_list_walk_step, NULL }, 4678 { "txg_list1", "given any txg_list_t *, walk all entries in txg 1", 4679 txg_list1_walk_init, txg_list_walk_step, NULL }, 4680 { "txg_list2", "given any txg_list_t *, walk all entries in txg 2", 4681 txg_list2_walk_init, txg_list_walk_step, NULL }, 4682 { "txg_list3", "given any txg_list_t *, walk all entries in txg 3", 4683 txg_list3_walk_init, txg_list_walk_step, NULL }, 4684 { "zio", "walk all zio structures, optionally for a particular spa_t", 4685 zio_walk_init, zio_walk_step, NULL }, 4686 { "zio_root", 4687 "walk all root zio_t structures, optionally for a particular spa_t", 4688 zio_walk_init, zio_walk_root_step, NULL }, 4689 { "spa", "walk all spa_t entries in the namespace", 4690 spa_walk_init, spa_walk_step, NULL }, 4691 { "metaslab", "given a spa_t *, walk all metaslab_t structures", 4692 metaslab_walk_init, metaslab_walk_step, NULL }, 4693 { "multilist", "given a multilist_t *, walk all list_t structures", 4694 multilist_walk_init, multilist_walk_step, NULL }, 4695 { "zfs_acl_node", "given a zfs_acl_t, walk all zfs_acl_nodes", 4696 zfs_acl_node_walk_init, zfs_acl_node_walk_step, NULL }, 4697 { "zfs_acl_node_aces", "given a zfs_acl_node_t, walk all ACEs", 4698 zfs_acl_node_aces_walk_init, zfs_aces_walk_step, NULL }, 4699 { "zfs_acl_node_aces0", 4700 "given a zfs_acl_node_t, walk all ACEs as ace_t", 4701 zfs_acl_node_aces0_walk_init, zfs_aces_walk_step, NULL }, 4702 { "zfs_btree", "given a zfs_btree_t *, walk all entries", 4703 btree_walk_init, btree_walk_step, btree_walk_fini }, 4704 { NULL } 4705 }; 4706 4707 static const mdb_modinfo_t modinfo = { 4708 MDB_API_VERSION, dcmds, walkers 4709 }; 4710 4711 const mdb_modinfo_t * 4712 _mdb_init(void) 4713 { 4714 return (&modinfo); 4715 } 4716