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) 2012 by Delphix. All rights reserved. 25 */ 26 27 /* Portions Copyright 2010 Robert Milkowski */ 28 29 #include <mdb/mdb_ctf.h> 30 #include <sys/zfs_context.h> 31 #include <sys/mdb_modapi.h> 32 #include <sys/dbuf.h> 33 #include <sys/dmu_objset.h> 34 #include <sys/dsl_dir.h> 35 #include <sys/dsl_pool.h> 36 #include <sys/metaslab_impl.h> 37 #include <sys/space_map.h> 38 #include <sys/list.h> 39 #include <sys/spa_impl.h> 40 #include <sys/vdev_impl.h> 41 #include <sys/zap_leaf.h> 42 #include <sys/zap_impl.h> 43 #include <ctype.h> 44 #include <sys/zfs_acl.h> 45 #include <sys/sa_impl.h> 46 47 #ifdef _KERNEL 48 #define ZFS_OBJ_NAME "zfs" 49 #else 50 #define ZFS_OBJ_NAME "libzpool.so.1" 51 #endif 52 53 #ifndef _KERNEL 54 int aok; 55 #endif 56 57 static int 58 getmember(uintptr_t addr, const char *type, mdb_ctf_id_t *idp, 59 const char *member, int len, void *buf) 60 { 61 mdb_ctf_id_t id; 62 ulong_t off; 63 char name[64]; 64 65 if (idp == NULL) { 66 if (mdb_ctf_lookup_by_name(type, &id) == -1) { 67 mdb_warn("couldn't find type %s", type); 68 return (DCMD_ERR); 69 } 70 idp = &id; 71 } else { 72 type = name; 73 mdb_ctf_type_name(*idp, name, sizeof (name)); 74 } 75 76 if (mdb_ctf_offsetof(*idp, member, &off) == -1) { 77 mdb_warn("couldn't find member %s of type %s\n", member, type); 78 return (DCMD_ERR); 79 } 80 if (off % 8 != 0) { 81 mdb_warn("member %s of type %s is unsupported bitfield", 82 member, type); 83 return (DCMD_ERR); 84 } 85 off /= 8; 86 87 if (mdb_vread(buf, len, addr + off) == -1) { 88 mdb_warn("failed to read %s from %s at %p", 89 member, type, addr + off); 90 return (DCMD_ERR); 91 } 92 /* mdb_warn("read %s from %s at %p+%llx\n", member, type, addr, off); */ 93 94 return (0); 95 } 96 97 #define GETMEMB(addr, type, member, dest) \ 98 getmember(addr, #type, NULL, #member, sizeof (dest), &(dest)) 99 100 #define GETMEMBID(addr, ctfid, member, dest) \ 101 getmember(addr, NULL, ctfid, #member, sizeof (dest), &(dest)) 102 103 static int 104 getrefcount(uintptr_t addr, mdb_ctf_id_t *id, 105 const char *member, uint64_t *rc) 106 { 107 static int gotid; 108 static mdb_ctf_id_t rc_id; 109 ulong_t off; 110 111 if (!gotid) { 112 if (mdb_ctf_lookup_by_name("struct refcount", &rc_id) == -1) { 113 mdb_warn("couldn't find struct refcount"); 114 return (DCMD_ERR); 115 } 116 gotid = TRUE; 117 } 118 119 if (mdb_ctf_offsetof(*id, member, &off) == -1) { 120 char name[64]; 121 mdb_ctf_type_name(*id, name, sizeof (name)); 122 mdb_warn("couldn't find member %s of type %s\n", member, name); 123 return (DCMD_ERR); 124 } 125 off /= 8; 126 127 return (GETMEMBID(addr + off, &rc_id, rc_count, *rc)); 128 } 129 130 static boolean_t 131 strisprint(const char *cp) 132 { 133 for (; *cp; cp++) { 134 if (!isprint(*cp)) 135 return (B_FALSE); 136 } 137 return (B_TRUE); 138 } 139 140 static int verbose; 141 142 static int 143 freelist_walk_init(mdb_walk_state_t *wsp) 144 { 145 if (wsp->walk_addr == NULL) { 146 mdb_warn("must supply starting address\n"); 147 return (WALK_ERR); 148 } 149 150 wsp->walk_data = 0; /* Index into the freelist */ 151 return (WALK_NEXT); 152 } 153 154 static int 155 freelist_walk_step(mdb_walk_state_t *wsp) 156 { 157 uint64_t entry; 158 uintptr_t number = (uintptr_t)wsp->walk_data; 159 char *ddata[] = { "ALLOC", "FREE", "CONDENSE", "INVALID", 160 "INVALID", "INVALID", "INVALID", "INVALID" }; 161 int mapshift = SPA_MINBLOCKSHIFT; 162 163 if (mdb_vread(&entry, sizeof (entry), wsp->walk_addr) == -1) { 164 mdb_warn("failed to read freelist entry %p", wsp->walk_addr); 165 return (WALK_DONE); 166 } 167 wsp->walk_addr += sizeof (entry); 168 wsp->walk_data = (void *)(number + 1); 169 170 if (SM_DEBUG_DECODE(entry)) { 171 mdb_printf("DEBUG: %3u %10s: txg=%llu pass=%llu\n", 172 number, 173 ddata[SM_DEBUG_ACTION_DECODE(entry)], 174 SM_DEBUG_TXG_DECODE(entry), 175 SM_DEBUG_SYNCPASS_DECODE(entry)); 176 } else { 177 mdb_printf("Entry: %3u offsets=%08llx-%08llx type=%c " 178 "size=%06llx", number, 179 SM_OFFSET_DECODE(entry) << mapshift, 180 (SM_OFFSET_DECODE(entry) + SM_RUN_DECODE(entry)) << 181 mapshift, 182 SM_TYPE_DECODE(entry) == SM_ALLOC ? 'A' : 'F', 183 SM_RUN_DECODE(entry) << mapshift); 184 if (verbose) 185 mdb_printf(" (raw=%012llx)\n", entry); 186 mdb_printf("\n"); 187 } 188 return (WALK_NEXT); 189 } 190 191 192 static int 193 dataset_name(uintptr_t addr, char *buf) 194 { 195 static int gotid; 196 static mdb_ctf_id_t dd_id; 197 uintptr_t dd_parent; 198 char dd_myname[MAXNAMELEN]; 199 200 if (!gotid) { 201 if (mdb_ctf_lookup_by_name("struct dsl_dir", 202 &dd_id) == -1) { 203 mdb_warn("couldn't find struct dsl_dir"); 204 return (DCMD_ERR); 205 } 206 gotid = TRUE; 207 } 208 if (GETMEMBID(addr, &dd_id, dd_parent, dd_parent) || 209 GETMEMBID(addr, &dd_id, dd_myname, dd_myname)) { 210 return (DCMD_ERR); 211 } 212 213 if (dd_parent) { 214 if (dataset_name(dd_parent, buf)) 215 return (DCMD_ERR); 216 strcat(buf, "/"); 217 } 218 219 if (dd_myname[0]) 220 strcat(buf, dd_myname); 221 else 222 strcat(buf, "???"); 223 224 return (0); 225 } 226 227 static int 228 objset_name(uintptr_t addr, char *buf) 229 { 230 static int gotid; 231 static mdb_ctf_id_t os_id, ds_id; 232 uintptr_t os_dsl_dataset; 233 char ds_snapname[MAXNAMELEN]; 234 uintptr_t ds_dir; 235 236 buf[0] = '\0'; 237 238 if (!gotid) { 239 if (mdb_ctf_lookup_by_name("struct objset", 240 &os_id) == -1) { 241 mdb_warn("couldn't find struct objset"); 242 return (DCMD_ERR); 243 } 244 if (mdb_ctf_lookup_by_name("struct dsl_dataset", 245 &ds_id) == -1) { 246 mdb_warn("couldn't find struct dsl_dataset"); 247 return (DCMD_ERR); 248 } 249 250 gotid = TRUE; 251 } 252 253 if (GETMEMBID(addr, &os_id, os_dsl_dataset, os_dsl_dataset)) 254 return (DCMD_ERR); 255 256 if (os_dsl_dataset == 0) { 257 strcat(buf, "mos"); 258 return (0); 259 } 260 261 if (GETMEMBID(os_dsl_dataset, &ds_id, ds_snapname, ds_snapname) || 262 GETMEMBID(os_dsl_dataset, &ds_id, ds_dir, ds_dir)) { 263 return (DCMD_ERR); 264 } 265 266 if (ds_dir && dataset_name(ds_dir, buf)) 267 return (DCMD_ERR); 268 269 if (ds_snapname[0]) { 270 strcat(buf, "@"); 271 strcat(buf, ds_snapname); 272 } 273 return (0); 274 } 275 276 static void 277 enum_lookup(char *out, size_t size, mdb_ctf_id_t id, int val, 278 const char *prefix) 279 { 280 const char *cp; 281 size_t len = strlen(prefix); 282 283 if ((cp = mdb_ctf_enum_name(id, val)) != NULL) { 284 if (strncmp(cp, prefix, len) == 0) 285 cp += len; 286 (void) strncpy(out, cp, size); 287 } else { 288 mdb_snprintf(out, size, "? (%d)", val); 289 } 290 } 291 292 /* ARGSUSED */ 293 static int 294 zfs_params(uintptr_t addr, uint_t flags, int argc, const mdb_arg_t *argv) 295 { 296 /* 297 * This table can be approximately generated by running: 298 * egrep "^[a-z0-9_]+ [a-z0-9_]+( =.*)?;" *.c | cut -d ' ' -f 2 299 */ 300 static const char *params[] = { 301 "arc_reduce_dnlc_percent", 302 "zfs_arc_max", 303 "zfs_arc_min", 304 "arc_shrink_shift", 305 "zfs_mdcomp_disable", 306 "zfs_prefetch_disable", 307 "zfetch_max_streams", 308 "zfetch_min_sec_reap", 309 "zfetch_block_cap", 310 "zfetch_array_rd_sz", 311 "zfs_default_bs", 312 "zfs_default_ibs", 313 "metaslab_aliquot", 314 "reference_tracking_enable", 315 "reference_history", 316 "spa_max_replication_override", 317 "spa_mode_global", 318 "zfs_flags", 319 "zfs_txg_synctime_ms", 320 "zfs_txg_timeout", 321 "zfs_write_limit_min", 322 "zfs_write_limit_max", 323 "zfs_write_limit_shift", 324 "zfs_write_limit_override", 325 "zfs_no_write_throttle", 326 "zfs_vdev_cache_max", 327 "zfs_vdev_cache_size", 328 "zfs_vdev_cache_bshift", 329 "vdev_mirror_shift", 330 "zfs_vdev_max_pending", 331 "zfs_vdev_min_pending", 332 "zfs_scrub_limit", 333 "zfs_no_scrub_io", 334 "zfs_no_scrub_prefetch", 335 "zfs_vdev_time_shift", 336 "zfs_vdev_ramp_rate", 337 "zfs_vdev_aggregation_limit", 338 "fzap_default_block_shift", 339 "zfs_immediate_write_sz", 340 "zfs_read_chunk_size", 341 "zfs_nocacheflush", 342 "zil_replay_disable", 343 "metaslab_gang_bang", 344 "metaslab_df_alloc_threshold", 345 "metaslab_df_free_pct", 346 "zio_injection_enabled", 347 "zvol_immediate_write_sz", 348 }; 349 350 for (int i = 0; i < sizeof (params) / sizeof (params[0]); i++) { 351 int sz; 352 uint64_t val64; 353 uint32_t *val32p = (uint32_t *)&val64; 354 355 sz = mdb_readvar(&val64, params[i]); 356 if (sz == 4) { 357 mdb_printf("%s = 0x%x\n", params[i], *val32p); 358 } else if (sz == 8) { 359 mdb_printf("%s = 0x%llx\n", params[i], val64); 360 } else { 361 mdb_warn("variable %s not found", params[i]); 362 } 363 } 364 365 return (DCMD_OK); 366 } 367 368 /* ARGSUSED */ 369 static int 370 blkptr(uintptr_t addr, uint_t flags, int argc, const mdb_arg_t *argv) 371 { 372 mdb_ctf_id_t type_enum, checksum_enum, compress_enum; 373 char type[80], checksum[80], compress[80]; 374 blkptr_t blk, *bp = &blk; 375 char buf[BP_SPRINTF_LEN]; 376 377 if (mdb_vread(&blk, sizeof (blkptr_t), addr) == -1) { 378 mdb_warn("failed to read blkptr_t"); 379 return (DCMD_ERR); 380 } 381 382 if (mdb_ctf_lookup_by_name("enum dmu_object_type", &type_enum) == -1 || 383 mdb_ctf_lookup_by_name("enum zio_checksum", &checksum_enum) == -1 || 384 mdb_ctf_lookup_by_name("enum zio_compress", &compress_enum) == -1) { 385 mdb_warn("Could not find blkptr enumerated types"); 386 return (DCMD_ERR); 387 } 388 389 enum_lookup(type, sizeof (type), type_enum, 390 BP_GET_TYPE(bp), "DMU_OT_"); 391 enum_lookup(checksum, sizeof (checksum), checksum_enum, 392 BP_GET_CHECKSUM(bp), "ZIO_CHECKSUM_"); 393 enum_lookup(compress, sizeof (compress), compress_enum, 394 BP_GET_COMPRESS(bp), "ZIO_COMPRESS_"); 395 396 SPRINTF_BLKPTR(mdb_snprintf, '\n', buf, bp, type, checksum, compress); 397 398 mdb_printf("%s\n", buf); 399 400 return (DCMD_OK); 401 } 402 403 /* ARGSUSED */ 404 static int 405 dbuf(uintptr_t addr, uint_t flags, int argc, const mdb_arg_t *argv) 406 { 407 mdb_ctf_id_t id; 408 dmu_buf_t db; 409 uintptr_t objset; 410 uint8_t level; 411 uint64_t blkid; 412 uint64_t holds; 413 char objectname[32]; 414 char blkidname[32]; 415 char path[MAXNAMELEN]; 416 417 if (DCMD_HDRSPEC(flags)) { 418 mdb_printf(" addr object lvl blkid holds os\n"); 419 } 420 421 if (mdb_ctf_lookup_by_name("struct dmu_buf_impl", &id) == -1) { 422 mdb_warn("couldn't find struct dmu_buf_impl_t"); 423 return (DCMD_ERR); 424 } 425 426 if (GETMEMBID(addr, &id, db_objset, objset) || 427 GETMEMBID(addr, &id, db, db) || 428 GETMEMBID(addr, &id, db_level, level) || 429 GETMEMBID(addr, &id, db_blkid, blkid)) { 430 return (WALK_ERR); 431 } 432 433 if (getrefcount(addr, &id, "db_holds", &holds)) { 434 return (WALK_ERR); 435 } 436 437 if (db.db_object == DMU_META_DNODE_OBJECT) 438 (void) strcpy(objectname, "mdn"); 439 else 440 (void) mdb_snprintf(objectname, sizeof (objectname), "%llx", 441 (u_longlong_t)db.db_object); 442 443 if (blkid == DMU_BONUS_BLKID) 444 (void) strcpy(blkidname, "bonus"); 445 else 446 (void) mdb_snprintf(blkidname, sizeof (blkidname), "%llx", 447 (u_longlong_t)blkid); 448 449 if (objset_name(objset, path)) { 450 return (WALK_ERR); 451 } 452 453 mdb_printf("%p %8s %1u %9s %2llu %s\n", 454 addr, objectname, level, blkidname, holds, path); 455 456 return (DCMD_OK); 457 } 458 459 /* ARGSUSED */ 460 static int 461 dbuf_stats(uintptr_t addr, uint_t flags, int argc, const mdb_arg_t *argv) 462 { 463 #define HISTOSZ 32 464 uintptr_t dbp; 465 dmu_buf_impl_t db; 466 dbuf_hash_table_t ht; 467 uint64_t bucket, ndbufs; 468 uint64_t histo[HISTOSZ]; 469 uint64_t histo2[HISTOSZ]; 470 int i, maxidx; 471 472 if (mdb_readvar(&ht, "dbuf_hash_table") == -1) { 473 mdb_warn("failed to read 'dbuf_hash_table'"); 474 return (DCMD_ERR); 475 } 476 477 for (i = 0; i < HISTOSZ; i++) { 478 histo[i] = 0; 479 histo2[i] = 0; 480 } 481 482 ndbufs = 0; 483 for (bucket = 0; bucket < ht.hash_table_mask+1; bucket++) { 484 int len; 485 486 if (mdb_vread(&dbp, sizeof (void *), 487 (uintptr_t)(ht.hash_table+bucket)) == -1) { 488 mdb_warn("failed to read hash bucket %u at %p", 489 bucket, ht.hash_table+bucket); 490 return (DCMD_ERR); 491 } 492 493 len = 0; 494 while (dbp != 0) { 495 if (mdb_vread(&db, sizeof (dmu_buf_impl_t), 496 dbp) == -1) { 497 mdb_warn("failed to read dbuf at %p", dbp); 498 return (DCMD_ERR); 499 } 500 dbp = (uintptr_t)db.db_hash_next; 501 for (i = MIN(len, HISTOSZ - 1); i >= 0; i--) 502 histo2[i]++; 503 len++; 504 ndbufs++; 505 } 506 507 if (len >= HISTOSZ) 508 len = HISTOSZ-1; 509 histo[len]++; 510 } 511 512 mdb_printf("hash table has %llu buckets, %llu dbufs " 513 "(avg %llu buckets/dbuf)\n", 514 ht.hash_table_mask+1, ndbufs, 515 (ht.hash_table_mask+1)/ndbufs); 516 517 mdb_printf("\n"); 518 maxidx = 0; 519 for (i = 0; i < HISTOSZ; i++) 520 if (histo[i] > 0) 521 maxidx = i; 522 mdb_printf("hash chain length number of buckets\n"); 523 for (i = 0; i <= maxidx; i++) 524 mdb_printf("%u %llu\n", i, histo[i]); 525 526 mdb_printf("\n"); 527 maxidx = 0; 528 for (i = 0; i < HISTOSZ; i++) 529 if (histo2[i] > 0) 530 maxidx = i; 531 mdb_printf("hash chain depth number of dbufs\n"); 532 for (i = 0; i <= maxidx; i++) 533 mdb_printf("%u or more %llu %llu%%\n", 534 i, histo2[i], histo2[i]*100/ndbufs); 535 536 537 return (DCMD_OK); 538 } 539 540 #define CHAIN_END 0xffff 541 /* 542 * ::zap_leaf [-v] 543 * 544 * Print a zap_leaf_phys_t, assumed to be 16k 545 */ 546 /* ARGSUSED */ 547 static int 548 zap_leaf(uintptr_t addr, uint_t flags, int argc, const mdb_arg_t *argv) 549 { 550 char buf[16*1024]; 551 int verbose = B_FALSE; 552 int four = B_FALSE; 553 zap_leaf_t l; 554 zap_leaf_phys_t *zlp = (void *)buf; 555 int i; 556 557 if (mdb_getopts(argc, argv, 558 'v', MDB_OPT_SETBITS, TRUE, &verbose, 559 '4', MDB_OPT_SETBITS, TRUE, &four, 560 NULL) != argc) 561 return (DCMD_USAGE); 562 563 l.l_phys = zlp; 564 l.l_bs = 14; /* assume 16k blocks */ 565 if (four) 566 l.l_bs = 12; 567 568 if (!(flags & DCMD_ADDRSPEC)) { 569 return (DCMD_USAGE); 570 } 571 572 if (mdb_vread(buf, sizeof (buf), addr) == -1) { 573 mdb_warn("failed to read zap_leaf_phys_t at %p", addr); 574 return (DCMD_ERR); 575 } 576 577 if (zlp->l_hdr.lh_block_type != ZBT_LEAF || 578 zlp->l_hdr.lh_magic != ZAP_LEAF_MAGIC) { 579 mdb_warn("This does not appear to be a zap_leaf_phys_t"); 580 return (DCMD_ERR); 581 } 582 583 mdb_printf("zap_leaf_phys_t at %p:\n", addr); 584 mdb_printf(" lh_prefix_len = %u\n", zlp->l_hdr.lh_prefix_len); 585 mdb_printf(" lh_prefix = %llx\n", zlp->l_hdr.lh_prefix); 586 mdb_printf(" lh_nentries = %u\n", zlp->l_hdr.lh_nentries); 587 mdb_printf(" lh_nfree = %u\n", zlp->l_hdr.lh_nfree, 588 zlp->l_hdr.lh_nfree * 100 / (ZAP_LEAF_NUMCHUNKS(&l))); 589 mdb_printf(" lh_freelist = %u\n", zlp->l_hdr.lh_freelist); 590 mdb_printf(" lh_flags = %x (%s)\n", zlp->l_hdr.lh_flags, 591 zlp->l_hdr.lh_flags & ZLF_ENTRIES_CDSORTED ? 592 "ENTRIES_CDSORTED" : ""); 593 594 if (verbose) { 595 mdb_printf(" hash table:\n"); 596 for (i = 0; i < ZAP_LEAF_HASH_NUMENTRIES(&l); i++) { 597 if (zlp->l_hash[i] != CHAIN_END) 598 mdb_printf(" %u: %u\n", i, zlp->l_hash[i]); 599 } 600 } 601 602 mdb_printf(" chunks:\n"); 603 for (i = 0; i < ZAP_LEAF_NUMCHUNKS(&l); i++) { 604 /* LINTED: alignment */ 605 zap_leaf_chunk_t *zlc = &ZAP_LEAF_CHUNK(&l, i); 606 switch (zlc->l_entry.le_type) { 607 case ZAP_CHUNK_FREE: 608 if (verbose) { 609 mdb_printf(" %u: free; lf_next = %u\n", 610 i, zlc->l_free.lf_next); 611 } 612 break; 613 case ZAP_CHUNK_ENTRY: 614 mdb_printf(" %u: entry\n", i); 615 if (verbose) { 616 mdb_printf(" le_next = %u\n", 617 zlc->l_entry.le_next); 618 } 619 mdb_printf(" le_name_chunk = %u\n", 620 zlc->l_entry.le_name_chunk); 621 mdb_printf(" le_name_numints = %u\n", 622 zlc->l_entry.le_name_numints); 623 mdb_printf(" le_value_chunk = %u\n", 624 zlc->l_entry.le_value_chunk); 625 mdb_printf(" le_value_intlen = %u\n", 626 zlc->l_entry.le_value_intlen); 627 mdb_printf(" le_value_numints = %u\n", 628 zlc->l_entry.le_value_numints); 629 mdb_printf(" le_cd = %u\n", 630 zlc->l_entry.le_cd); 631 mdb_printf(" le_hash = %llx\n", 632 zlc->l_entry.le_hash); 633 break; 634 case ZAP_CHUNK_ARRAY: 635 mdb_printf(" %u: array", i); 636 if (strisprint((char *)zlc->l_array.la_array)) 637 mdb_printf(" \"%s\"", zlc->l_array.la_array); 638 mdb_printf("\n"); 639 if (verbose) { 640 int j; 641 mdb_printf(" "); 642 for (j = 0; j < ZAP_LEAF_ARRAY_BYTES; j++) { 643 mdb_printf("%02x ", 644 zlc->l_array.la_array[j]); 645 } 646 mdb_printf("\n"); 647 } 648 if (zlc->l_array.la_next != CHAIN_END) { 649 mdb_printf(" lf_next = %u\n", 650 zlc->l_array.la_next); 651 } 652 break; 653 default: 654 mdb_printf(" %u: undefined type %u\n", 655 zlc->l_entry.le_type); 656 } 657 } 658 659 return (DCMD_OK); 660 } 661 662 typedef struct dbufs_data { 663 mdb_ctf_id_t id; 664 uint64_t objset; 665 uint64_t object; 666 uint64_t level; 667 uint64_t blkid; 668 char *osname; 669 } dbufs_data_t; 670 671 #define DBUFS_UNSET (0xbaddcafedeadbeefULL) 672 673 /* ARGSUSED */ 674 static int 675 dbufs_cb(uintptr_t addr, const void *unknown, void *arg) 676 { 677 dbufs_data_t *data = arg; 678 uintptr_t objset; 679 dmu_buf_t db; 680 uint8_t level; 681 uint64_t blkid; 682 char osname[MAXNAMELEN]; 683 684 if (GETMEMBID(addr, &data->id, db_objset, objset) || 685 GETMEMBID(addr, &data->id, db, db) || 686 GETMEMBID(addr, &data->id, db_level, level) || 687 GETMEMBID(addr, &data->id, db_blkid, blkid)) { 688 return (WALK_ERR); 689 } 690 691 if ((data->objset == DBUFS_UNSET || data->objset == objset) && 692 (data->osname == NULL || (objset_name(objset, osname) == 0 && 693 strcmp(data->osname, osname) == 0)) && 694 (data->object == DBUFS_UNSET || data->object == db.db_object) && 695 (data->level == DBUFS_UNSET || data->level == level) && 696 (data->blkid == DBUFS_UNSET || data->blkid == blkid)) { 697 mdb_printf("%#lr\n", addr); 698 } 699 return (WALK_NEXT); 700 } 701 702 /* ARGSUSED */ 703 static int 704 dbufs(uintptr_t addr, uint_t flags, int argc, const mdb_arg_t *argv) 705 { 706 dbufs_data_t data; 707 char *object = NULL; 708 char *blkid = NULL; 709 710 data.objset = data.object = data.level = data.blkid = DBUFS_UNSET; 711 data.osname = NULL; 712 713 if (mdb_getopts(argc, argv, 714 'O', MDB_OPT_UINT64, &data.objset, 715 'n', MDB_OPT_STR, &data.osname, 716 'o', MDB_OPT_STR, &object, 717 'l', MDB_OPT_UINT64, &data.level, 718 'b', MDB_OPT_STR, &blkid) != argc) { 719 return (DCMD_USAGE); 720 } 721 722 if (object) { 723 if (strcmp(object, "mdn") == 0) { 724 data.object = DMU_META_DNODE_OBJECT; 725 } else { 726 data.object = mdb_strtoull(object); 727 } 728 } 729 730 if (blkid) { 731 if (strcmp(blkid, "bonus") == 0) { 732 data.blkid = DMU_BONUS_BLKID; 733 } else { 734 data.blkid = mdb_strtoull(blkid); 735 } 736 } 737 738 if (mdb_ctf_lookup_by_name("struct dmu_buf_impl", &data.id) == -1) { 739 mdb_warn("couldn't find struct dmu_buf_impl_t"); 740 return (DCMD_ERR); 741 } 742 743 if (mdb_walk("dmu_buf_impl_t", dbufs_cb, &data) != 0) { 744 mdb_warn("can't walk dbufs"); 745 return (DCMD_ERR); 746 } 747 748 return (DCMD_OK); 749 } 750 751 typedef struct abuf_find_data { 752 dva_t dva; 753 mdb_ctf_id_t id; 754 } abuf_find_data_t; 755 756 /* ARGSUSED */ 757 static int 758 abuf_find_cb(uintptr_t addr, const void *unknown, void *arg) 759 { 760 abuf_find_data_t *data = arg; 761 dva_t dva; 762 763 if (GETMEMBID(addr, &data->id, b_dva, dva)) { 764 return (WALK_ERR); 765 } 766 767 if (dva.dva_word[0] == data->dva.dva_word[0] && 768 dva.dva_word[1] == data->dva.dva_word[1]) { 769 mdb_printf("%#lr\n", addr); 770 } 771 return (WALK_NEXT); 772 } 773 774 /* ARGSUSED */ 775 static int 776 abuf_find(uintptr_t addr, uint_t flags, int argc, const mdb_arg_t *argv) 777 { 778 abuf_find_data_t data; 779 GElf_Sym sym; 780 int i; 781 const char *syms[] = { 782 "ARC_mru", 783 "ARC_mru_ghost", 784 "ARC_mfu", 785 "ARC_mfu_ghost", 786 }; 787 788 if (argc != 2) 789 return (DCMD_USAGE); 790 791 for (i = 0; i < 2; i ++) { 792 switch (argv[i].a_type) { 793 case MDB_TYPE_STRING: 794 data.dva.dva_word[i] = mdb_strtoull(argv[i].a_un.a_str); 795 break; 796 case MDB_TYPE_IMMEDIATE: 797 data.dva.dva_word[i] = argv[i].a_un.a_val; 798 break; 799 default: 800 return (DCMD_USAGE); 801 } 802 } 803 804 if (mdb_ctf_lookup_by_name("struct arc_buf_hdr", &data.id) == -1) { 805 mdb_warn("couldn't find struct arc_buf_hdr"); 806 return (DCMD_ERR); 807 } 808 809 for (i = 0; i < sizeof (syms) / sizeof (syms[0]); i++) { 810 if (mdb_lookup_by_name(syms[i], &sym)) { 811 mdb_warn("can't find symbol %s", syms[i]); 812 return (DCMD_ERR); 813 } 814 815 if (mdb_pwalk("list", abuf_find_cb, &data, sym.st_value) != 0) { 816 mdb_warn("can't walk %s", syms[i]); 817 return (DCMD_ERR); 818 } 819 } 820 821 return (DCMD_OK); 822 } 823 824 /* ARGSUSED */ 825 static int 826 dbgmsg_cb(uintptr_t addr, const void *unknown, void *arg) 827 { 828 static mdb_ctf_id_t id; 829 static boolean_t gotid; 830 static ulong_t off; 831 832 int *verbosep = arg; 833 time_t timestamp; 834 char buf[1024]; 835 836 if (!gotid) { 837 if (mdb_ctf_lookup_by_name("struct zfs_dbgmsg", &id) == -1) { 838 mdb_warn("couldn't find struct zfs_dbgmsg"); 839 return (WALK_ERR); 840 } 841 gotid = TRUE; 842 if (mdb_ctf_offsetof(id, "zdm_msg", &off) == -1) { 843 mdb_warn("couldn't find zdm_msg"); 844 return (WALK_ERR); 845 } 846 off /= 8; 847 } 848 849 850 if (GETMEMBID(addr, &id, zdm_timestamp, timestamp)) { 851 return (WALK_ERR); 852 } 853 854 if (mdb_readstr(buf, sizeof (buf), addr + off) == -1) { 855 mdb_warn("failed to read zdm_msg at %p\n", addr + off); 856 return (DCMD_ERR); 857 } 858 859 if (*verbosep) 860 mdb_printf("%Y ", timestamp); 861 862 mdb_printf("%s\n", buf); 863 864 if (*verbosep) 865 (void) mdb_call_dcmd("whatis", addr, DCMD_ADDRSPEC, 0, NULL); 866 867 return (WALK_NEXT); 868 } 869 870 /* ARGSUSED */ 871 static int 872 dbgmsg(uintptr_t addr, uint_t flags, int argc, const mdb_arg_t *argv) 873 { 874 GElf_Sym sym; 875 int verbose = FALSE; 876 877 if (mdb_getopts(argc, argv, 878 'v', MDB_OPT_SETBITS, TRUE, &verbose, 879 NULL) != argc) 880 return (DCMD_USAGE); 881 882 if (mdb_lookup_by_name("zfs_dbgmsgs", &sym)) { 883 mdb_warn("can't find zfs_dbgmsgs"); 884 return (DCMD_ERR); 885 } 886 887 if (mdb_pwalk("list", dbgmsg_cb, &verbose, sym.st_value) != 0) { 888 mdb_warn("can't walk zfs_dbgmsgs"); 889 return (DCMD_ERR); 890 } 891 892 return (DCMD_OK); 893 } 894 895 /*ARGSUSED*/ 896 static int 897 arc_print(uintptr_t addr, uint_t flags, int argc, const mdb_arg_t *argv) 898 { 899 kstat_named_t *stats; 900 GElf_Sym sym; 901 int nstats, i; 902 uint_t opt_a = FALSE; 903 uint_t opt_b = FALSE; 904 uint_t shift = 0; 905 const char *suffix; 906 907 static const char *bytestats[] = { 908 "p", "c", "c_min", "c_max", "size", NULL 909 }; 910 911 static const char *extras[] = { 912 "arc_no_grow", "arc_tempreserve", 913 "arc_meta_used", "arc_meta_limit", "arc_meta_max", 914 NULL 915 }; 916 917 if (mdb_lookup_by_name("arc_stats", &sym) == -1) { 918 mdb_warn("failed to find 'arc_stats'"); 919 return (DCMD_ERR); 920 } 921 922 stats = mdb_zalloc(sym.st_size, UM_SLEEP | UM_GC); 923 924 if (mdb_vread(stats, sym.st_size, sym.st_value) == -1) { 925 mdb_warn("couldn't read 'arc_stats' at %p", sym.st_value); 926 return (DCMD_ERR); 927 } 928 929 nstats = sym.st_size / sizeof (kstat_named_t); 930 931 /* NB: -a / opt_a are ignored for backwards compatability */ 932 if (mdb_getopts(argc, argv, 933 'a', MDB_OPT_SETBITS, TRUE, &opt_a, 934 'b', MDB_OPT_SETBITS, TRUE, &opt_b, 935 'k', MDB_OPT_SETBITS, 10, &shift, 936 'm', MDB_OPT_SETBITS, 20, &shift, 937 'g', MDB_OPT_SETBITS, 30, &shift, 938 NULL) != argc) 939 return (DCMD_USAGE); 940 941 if (!opt_b && !shift) 942 shift = 20; 943 944 switch (shift) { 945 case 0: 946 suffix = "B"; 947 break; 948 case 10: 949 suffix = "KB"; 950 break; 951 case 20: 952 suffix = "MB"; 953 break; 954 case 30: 955 suffix = "GB"; 956 break; 957 default: 958 suffix = "XX"; 959 } 960 961 for (i = 0; i < nstats; i++) { 962 int j; 963 boolean_t bytes = B_FALSE; 964 965 for (j = 0; bytestats[j]; j++) { 966 if (strcmp(stats[i].name, bytestats[j]) == 0) { 967 bytes = B_TRUE; 968 break; 969 } 970 } 971 972 if (bytes) { 973 mdb_printf("%-25s = %9llu %s\n", stats[i].name, 974 stats[i].value.ui64 >> shift, suffix); 975 } else { 976 mdb_printf("%-25s = %9llu\n", stats[i].name, 977 stats[i].value.ui64); 978 } 979 } 980 981 for (i = 0; extras[i]; i++) { 982 uint64_t buf; 983 984 if (mdb_lookup_by_name(extras[i], &sym) == -1) { 985 mdb_warn("failed to find '%s'", extras[i]); 986 return (DCMD_ERR); 987 } 988 989 if (sym.st_size != sizeof (uint64_t) && 990 sym.st_size != sizeof (uint32_t)) { 991 mdb_warn("expected scalar for variable '%s'\n", 992 extras[i]); 993 return (DCMD_ERR); 994 } 995 996 if (mdb_vread(&buf, sym.st_size, sym.st_value) == -1) { 997 mdb_warn("couldn't read '%s'", extras[i]); 998 return (DCMD_ERR); 999 } 1000 1001 mdb_printf("%-25s = ", extras[i]); 1002 1003 /* NB: all the 64-bit extras happen to be byte counts */ 1004 if (sym.st_size == sizeof (uint64_t)) 1005 mdb_printf("%9llu %s\n", buf >> shift, suffix); 1006 1007 if (sym.st_size == sizeof (uint32_t)) 1008 mdb_printf("%9d\n", *((uint32_t *)&buf)); 1009 } 1010 return (DCMD_OK); 1011 } 1012 1013 /* 1014 * ::spa 1015 * 1016 * -c Print configuration information as well 1017 * -v Print vdev state 1018 * -e Print vdev error stats 1019 * 1020 * Print a summarized spa_t. When given no arguments, prints out a table of all 1021 * active pools on the system. 1022 */ 1023 /* ARGSUSED */ 1024 static int 1025 spa_print(uintptr_t addr, uint_t flags, int argc, const mdb_arg_t *argv) 1026 { 1027 spa_t spa; 1028 const char *statetab[] = { "ACTIVE", "EXPORTED", "DESTROYED", 1029 "SPARE", "L2CACHE", "UNINIT", "UNAVAIL", "POTENTIAL" }; 1030 const char *state; 1031 int config = FALSE; 1032 int vdevs = FALSE; 1033 int errors = FALSE; 1034 1035 if (mdb_getopts(argc, argv, 1036 'c', MDB_OPT_SETBITS, TRUE, &config, 1037 'v', MDB_OPT_SETBITS, TRUE, &vdevs, 1038 'e', MDB_OPT_SETBITS, TRUE, &errors, 1039 NULL) != argc) 1040 return (DCMD_USAGE); 1041 1042 if (!(flags & DCMD_ADDRSPEC)) { 1043 if (mdb_walk_dcmd("spa", "spa", argc, argv) == -1) { 1044 mdb_warn("can't walk spa"); 1045 return (DCMD_ERR); 1046 } 1047 1048 return (DCMD_OK); 1049 } 1050 1051 if (flags & DCMD_PIPE_OUT) { 1052 mdb_printf("%#lr\n", addr); 1053 return (DCMD_OK); 1054 } 1055 1056 if (DCMD_HDRSPEC(flags)) 1057 mdb_printf("%<u>%-?s %9s %-*s%</u>\n", "ADDR", "STATE", 1058 sizeof (uintptr_t) == 4 ? 60 : 52, "NAME"); 1059 1060 if (mdb_vread(&spa, sizeof (spa), addr) == -1) { 1061 mdb_warn("failed to read spa_t at %p", addr); 1062 return (DCMD_ERR); 1063 } 1064 1065 if (spa.spa_state < 0 || spa.spa_state > POOL_STATE_UNAVAIL) 1066 state = "UNKNOWN"; 1067 else 1068 state = statetab[spa.spa_state]; 1069 1070 mdb_printf("%0?p %9s %s\n", addr, state, spa.spa_name); 1071 1072 if (config) { 1073 mdb_printf("\n"); 1074 mdb_inc_indent(4); 1075 if (mdb_call_dcmd("spa_config", addr, flags, 0, 1076 NULL) != DCMD_OK) 1077 return (DCMD_ERR); 1078 mdb_dec_indent(4); 1079 } 1080 1081 if (vdevs || errors) { 1082 mdb_arg_t v; 1083 1084 v.a_type = MDB_TYPE_STRING; 1085 v.a_un.a_str = "-e"; 1086 1087 mdb_printf("\n"); 1088 mdb_inc_indent(4); 1089 if (mdb_call_dcmd("spa_vdevs", addr, flags, errors ? 1 : 0, 1090 &v) != DCMD_OK) 1091 return (DCMD_ERR); 1092 mdb_dec_indent(4); 1093 } 1094 1095 return (DCMD_OK); 1096 } 1097 1098 /* 1099 * ::spa_config 1100 * 1101 * Given a spa_t, print the configuration information stored in spa_config. 1102 * Since it's just an nvlist, format it as an indented list of name=value pairs. 1103 * We simply read the value of spa_config and pass off to ::nvlist. 1104 */ 1105 /* ARGSUSED */ 1106 static int 1107 spa_print_config(uintptr_t addr, uint_t flags, int argc, const mdb_arg_t *argv) 1108 { 1109 spa_t spa; 1110 1111 if (argc != 0 || !(flags & DCMD_ADDRSPEC)) 1112 return (DCMD_USAGE); 1113 1114 if (mdb_vread(&spa, sizeof (spa), addr) == -1) { 1115 mdb_warn("failed to read spa_t at %p", addr); 1116 return (DCMD_ERR); 1117 } 1118 1119 if (spa.spa_config == NULL) { 1120 mdb_printf("(none)\n"); 1121 return (DCMD_OK); 1122 } 1123 1124 return (mdb_call_dcmd("nvlist", (uintptr_t)spa.spa_config, flags, 1125 0, NULL)); 1126 } 1127 1128 /* 1129 * ::vdev 1130 * 1131 * Print out a summarized vdev_t, in the following form: 1132 * 1133 * ADDR STATE AUX DESC 1134 * fffffffbcde23df0 HEALTHY - /dev/dsk/c0t0d0 1135 * 1136 * If '-r' is specified, recursively visit all children. 1137 * 1138 * With '-e', the statistics associated with the vdev are printed as well. 1139 */ 1140 static int 1141 do_print_vdev(uintptr_t addr, int flags, int depth, int stats, 1142 int recursive) 1143 { 1144 vdev_t vdev; 1145 char desc[MAXNAMELEN]; 1146 int c, children; 1147 uintptr_t *child; 1148 const char *state, *aux; 1149 1150 if (mdb_vread(&vdev, sizeof (vdev), (uintptr_t)addr) == -1) { 1151 mdb_warn("failed to read vdev_t at %p\n", (uintptr_t)addr); 1152 return (DCMD_ERR); 1153 } 1154 1155 if (flags & DCMD_PIPE_OUT) { 1156 mdb_printf("%#lr", addr); 1157 } else { 1158 if (vdev.vdev_path != NULL) { 1159 if (mdb_readstr(desc, sizeof (desc), 1160 (uintptr_t)vdev.vdev_path) == -1) { 1161 mdb_warn("failed to read vdev_path at %p\n", 1162 vdev.vdev_path); 1163 return (DCMD_ERR); 1164 } 1165 } else if (vdev.vdev_ops != NULL) { 1166 vdev_ops_t ops; 1167 if (mdb_vread(&ops, sizeof (ops), 1168 (uintptr_t)vdev.vdev_ops) == -1) { 1169 mdb_warn("failed to read vdev_ops at %p\n", 1170 vdev.vdev_ops); 1171 return (DCMD_ERR); 1172 } 1173 (void) strcpy(desc, ops.vdev_op_type); 1174 } else { 1175 (void) strcpy(desc, "<unknown>"); 1176 } 1177 1178 if (depth == 0 && DCMD_HDRSPEC(flags)) 1179 mdb_printf("%<u>%-?s %-9s %-12s %-*s%</u>\n", 1180 "ADDR", "STATE", "AUX", 1181 sizeof (uintptr_t) == 4 ? 43 : 35, 1182 "DESCRIPTION"); 1183 1184 mdb_printf("%0?p ", addr); 1185 1186 switch (vdev.vdev_state) { 1187 case VDEV_STATE_CLOSED: 1188 state = "CLOSED"; 1189 break; 1190 case VDEV_STATE_OFFLINE: 1191 state = "OFFLINE"; 1192 break; 1193 case VDEV_STATE_CANT_OPEN: 1194 state = "CANT_OPEN"; 1195 break; 1196 case VDEV_STATE_DEGRADED: 1197 state = "DEGRADED"; 1198 break; 1199 case VDEV_STATE_HEALTHY: 1200 state = "HEALTHY"; 1201 break; 1202 case VDEV_STATE_REMOVED: 1203 state = "REMOVED"; 1204 break; 1205 case VDEV_STATE_FAULTED: 1206 state = "FAULTED"; 1207 break; 1208 default: 1209 state = "UNKNOWN"; 1210 break; 1211 } 1212 1213 switch (vdev.vdev_stat.vs_aux) { 1214 case VDEV_AUX_NONE: 1215 aux = "-"; 1216 break; 1217 case VDEV_AUX_OPEN_FAILED: 1218 aux = "OPEN_FAILED"; 1219 break; 1220 case VDEV_AUX_CORRUPT_DATA: 1221 aux = "CORRUPT_DATA"; 1222 break; 1223 case VDEV_AUX_NO_REPLICAS: 1224 aux = "NO_REPLICAS"; 1225 break; 1226 case VDEV_AUX_BAD_GUID_SUM: 1227 aux = "BAD_GUID_SUM"; 1228 break; 1229 case VDEV_AUX_TOO_SMALL: 1230 aux = "TOO_SMALL"; 1231 break; 1232 case VDEV_AUX_BAD_LABEL: 1233 aux = "BAD_LABEL"; 1234 break; 1235 case VDEV_AUX_VERSION_NEWER: 1236 aux = "VERS_NEWER"; 1237 break; 1238 case VDEV_AUX_VERSION_OLDER: 1239 aux = "VERS_OLDER"; 1240 break; 1241 case VDEV_AUX_UNSUP_FEAT: 1242 aux = "UNSUP_FEAT"; 1243 break; 1244 case VDEV_AUX_SPARED: 1245 aux = "SPARED"; 1246 break; 1247 case VDEV_AUX_ERR_EXCEEDED: 1248 aux = "ERR_EXCEEDED"; 1249 break; 1250 case VDEV_AUX_IO_FAILURE: 1251 aux = "IO_FAILURE"; 1252 break; 1253 case VDEV_AUX_BAD_LOG: 1254 aux = "BAD_LOG"; 1255 break; 1256 case VDEV_AUX_EXTERNAL: 1257 aux = "EXTERNAL"; 1258 break; 1259 case VDEV_AUX_SPLIT_POOL: 1260 aux = "SPLIT_POOL"; 1261 break; 1262 default: 1263 aux = "UNKNOWN"; 1264 break; 1265 } 1266 1267 mdb_printf("%-9s %-12s %*s%s\n", state, aux, depth, "", desc); 1268 1269 if (stats) { 1270 vdev_stat_t *vs = &vdev.vdev_stat; 1271 int i; 1272 1273 mdb_inc_indent(4); 1274 mdb_printf("\n"); 1275 mdb_printf("%<u> %12s %12s %12s %12s " 1276 "%12s%</u>\n", "READ", "WRITE", "FREE", "CLAIM", 1277 "IOCTL"); 1278 mdb_printf("OPS "); 1279 for (i = 1; i < ZIO_TYPES; i++) 1280 mdb_printf("%11#llx%s", vs->vs_ops[i], 1281 i == ZIO_TYPES - 1 ? "" : " "); 1282 mdb_printf("\n"); 1283 mdb_printf("BYTES "); 1284 for (i = 1; i < ZIO_TYPES; i++) 1285 mdb_printf("%11#llx%s", vs->vs_bytes[i], 1286 i == ZIO_TYPES - 1 ? "" : " "); 1287 1288 1289 mdb_printf("\n"); 1290 mdb_printf("EREAD %10#llx\n", vs->vs_read_errors); 1291 mdb_printf("EWRITE %10#llx\n", vs->vs_write_errors); 1292 mdb_printf("ECKSUM %10#llx\n", 1293 vs->vs_checksum_errors); 1294 mdb_dec_indent(4); 1295 } 1296 1297 if (stats) 1298 mdb_printf("\n"); 1299 } 1300 1301 children = vdev.vdev_children; 1302 1303 if (children == 0 || !recursive) 1304 return (DCMD_OK); 1305 1306 child = mdb_alloc(children * sizeof (void *), UM_SLEEP | UM_GC); 1307 if (mdb_vread(child, children * sizeof (void *), 1308 (uintptr_t)vdev.vdev_child) == -1) { 1309 mdb_warn("failed to read vdev children at %p", vdev.vdev_child); 1310 return (DCMD_ERR); 1311 } 1312 1313 for (c = 0; c < children; c++) { 1314 if (do_print_vdev(child[c], flags, depth + 2, stats, 1315 recursive)) 1316 return (DCMD_ERR); 1317 } 1318 1319 return (DCMD_OK); 1320 } 1321 1322 static int 1323 vdev_print(uintptr_t addr, uint_t flags, int argc, const mdb_arg_t *argv) 1324 { 1325 int recursive = FALSE; 1326 int stats = FALSE; 1327 uint64_t depth = 0; 1328 1329 if (mdb_getopts(argc, argv, 1330 'r', MDB_OPT_SETBITS, TRUE, &recursive, 1331 'e', MDB_OPT_SETBITS, TRUE, &stats, 1332 'd', MDB_OPT_UINT64, &depth, 1333 NULL) != argc) 1334 return (DCMD_USAGE); 1335 1336 if (!(flags & DCMD_ADDRSPEC)) { 1337 mdb_warn("no vdev_t address given\n"); 1338 return (DCMD_ERR); 1339 } 1340 1341 return (do_print_vdev(addr, flags, (int)depth, stats, recursive)); 1342 } 1343 1344 typedef struct metaslab_walk_data { 1345 uint64_t mw_numvdevs; 1346 uintptr_t *mw_vdevs; 1347 int mw_curvdev; 1348 uint64_t mw_nummss; 1349 uintptr_t *mw_mss; 1350 int mw_curms; 1351 } metaslab_walk_data_t; 1352 1353 static int 1354 metaslab_walk_step(mdb_walk_state_t *wsp) 1355 { 1356 metaslab_walk_data_t *mw = wsp->walk_data; 1357 metaslab_t ms; 1358 uintptr_t msp; 1359 1360 if (mw->mw_curvdev >= mw->mw_numvdevs) 1361 return (WALK_DONE); 1362 1363 if (mw->mw_mss == NULL) { 1364 uintptr_t mssp; 1365 uintptr_t vdevp; 1366 1367 ASSERT(mw->mw_curms == 0); 1368 ASSERT(mw->mw_nummss == 0); 1369 1370 vdevp = mw->mw_vdevs[mw->mw_curvdev]; 1371 if (GETMEMB(vdevp, struct vdev, vdev_ms, mssp) || 1372 GETMEMB(vdevp, struct vdev, vdev_ms_count, mw->mw_nummss)) { 1373 return (WALK_ERR); 1374 } 1375 1376 mw->mw_mss = mdb_alloc(mw->mw_nummss * sizeof (void*), 1377 UM_SLEEP | UM_GC); 1378 if (mdb_vread(mw->mw_mss, mw->mw_nummss * sizeof (void*), 1379 mssp) == -1) { 1380 mdb_warn("failed to read vdev_ms at %p", mssp); 1381 return (WALK_ERR); 1382 } 1383 } 1384 1385 if (mw->mw_curms >= mw->mw_nummss) { 1386 mw->mw_mss = NULL; 1387 mw->mw_curms = 0; 1388 mw->mw_nummss = 0; 1389 mw->mw_curvdev++; 1390 return (WALK_NEXT); 1391 } 1392 1393 msp = mw->mw_mss[mw->mw_curms]; 1394 if (mdb_vread(&ms, sizeof (metaslab_t), msp) == -1) { 1395 mdb_warn("failed to read metaslab_t at %p", msp); 1396 return (WALK_ERR); 1397 } 1398 1399 mw->mw_curms++; 1400 1401 return (wsp->walk_callback(msp, &ms, wsp->walk_cbdata)); 1402 } 1403 1404 /* ARGSUSED */ 1405 static int 1406 metaslab_walk_init(mdb_walk_state_t *wsp) 1407 { 1408 metaslab_walk_data_t *mw; 1409 uintptr_t root_vdevp; 1410 uintptr_t childp; 1411 1412 if (wsp->walk_addr == NULL) { 1413 mdb_warn("must supply address of spa_t\n"); 1414 return (WALK_ERR); 1415 } 1416 1417 mw = mdb_zalloc(sizeof (metaslab_walk_data_t), UM_SLEEP | UM_GC); 1418 1419 if (GETMEMB(wsp->walk_addr, struct spa, spa_root_vdev, root_vdevp) || 1420 GETMEMB(root_vdevp, struct vdev, vdev_children, mw->mw_numvdevs) || 1421 GETMEMB(root_vdevp, struct vdev, vdev_child, childp)) { 1422 return (DCMD_ERR); 1423 } 1424 1425 mw->mw_vdevs = mdb_alloc(mw->mw_numvdevs * sizeof (void *), 1426 UM_SLEEP | UM_GC); 1427 if (mdb_vread(mw->mw_vdevs, mw->mw_numvdevs * sizeof (void *), 1428 childp) == -1) { 1429 mdb_warn("failed to read root vdev children at %p", childp); 1430 return (DCMD_ERR); 1431 } 1432 1433 wsp->walk_data = mw; 1434 1435 return (WALK_NEXT); 1436 } 1437 1438 typedef struct mdb_spa { 1439 uintptr_t spa_dsl_pool; 1440 uintptr_t spa_root_vdev; 1441 } mdb_spa_t; 1442 1443 typedef struct mdb_dsl_dir { 1444 uintptr_t dd_phys; 1445 int64_t dd_space_towrite[TXG_SIZE]; 1446 } mdb_dsl_dir_t; 1447 1448 typedef struct mdb_dsl_dir_phys { 1449 uint64_t dd_used_bytes; 1450 uint64_t dd_compressed_bytes; 1451 uint64_t dd_uncompressed_bytes; 1452 } mdb_dsl_dir_phys_t; 1453 1454 typedef struct mdb_vdev { 1455 uintptr_t vdev_parent; 1456 uintptr_t vdev_ms; 1457 uint64_t vdev_ms_count; 1458 vdev_stat_t vdev_stat; 1459 } mdb_vdev_t; 1460 1461 typedef struct mdb_metaslab { 1462 space_map_t ms_allocmap[TXG_SIZE]; 1463 space_map_t ms_freemap[TXG_SIZE]; 1464 space_map_t ms_map; 1465 space_map_obj_t ms_smo; 1466 space_map_obj_t ms_smo_syncing; 1467 } mdb_metaslab_t; 1468 1469 typedef struct space_data { 1470 uint64_t ms_allocmap[TXG_SIZE]; 1471 uint64_t ms_freemap[TXG_SIZE]; 1472 uint64_t ms_map; 1473 uint64_t avail; 1474 uint64_t nowavail; 1475 } space_data_t; 1476 1477 /* ARGSUSED */ 1478 static int 1479 space_cb(uintptr_t addr, const void *unknown, void *arg) 1480 { 1481 space_data_t *sd = arg; 1482 mdb_metaslab_t ms; 1483 1484 if (GETMEMB(addr, struct metaslab, ms_allocmap, ms.ms_allocmap) || 1485 GETMEMB(addr, struct metaslab, ms_freemap, ms.ms_freemap) || 1486 GETMEMB(addr, struct metaslab, ms_map, ms.ms_map) || 1487 GETMEMB(addr, struct metaslab, ms_smo, ms.ms_smo) || 1488 GETMEMB(addr, struct metaslab, ms_smo_syncing, ms.ms_smo_syncing)) { 1489 return (WALK_ERR); 1490 } 1491 1492 sd->ms_allocmap[0] += ms.ms_allocmap[0].sm_space; 1493 sd->ms_allocmap[1] += ms.ms_allocmap[1].sm_space; 1494 sd->ms_allocmap[2] += ms.ms_allocmap[2].sm_space; 1495 sd->ms_allocmap[3] += ms.ms_allocmap[3].sm_space; 1496 sd->ms_freemap[0] += ms.ms_freemap[0].sm_space; 1497 sd->ms_freemap[1] += ms.ms_freemap[1].sm_space; 1498 sd->ms_freemap[2] += ms.ms_freemap[2].sm_space; 1499 sd->ms_freemap[3] += ms.ms_freemap[3].sm_space; 1500 sd->ms_map += ms.ms_map.sm_space; 1501 sd->avail += ms.ms_map.sm_size - ms.ms_smo.smo_alloc; 1502 sd->nowavail += ms.ms_map.sm_size - ms.ms_smo_syncing.smo_alloc; 1503 1504 return (WALK_NEXT); 1505 } 1506 1507 /* 1508 * ::spa_space [-b] 1509 * 1510 * Given a spa_t, print out it's on-disk space usage and in-core 1511 * estimates of future usage. If -b is given, print space in bytes. 1512 * Otherwise print in megabytes. 1513 */ 1514 /* ARGSUSED */ 1515 static int 1516 spa_space(uintptr_t addr, uint_t flags, int argc, const mdb_arg_t *argv) 1517 { 1518 mdb_spa_t spa; 1519 uintptr_t dp_root_dir; 1520 mdb_dsl_dir_t dd; 1521 mdb_dsl_dir_phys_t dsp; 1522 uint64_t children; 1523 uintptr_t childaddr; 1524 space_data_t sd; 1525 int shift = 20; 1526 char *suffix = "M"; 1527 int bits = FALSE; 1528 1529 if (mdb_getopts(argc, argv, 'b', MDB_OPT_SETBITS, TRUE, &bits, NULL) != 1530 argc) 1531 return (DCMD_USAGE); 1532 if (!(flags & DCMD_ADDRSPEC)) 1533 return (DCMD_USAGE); 1534 1535 if (bits) { 1536 shift = 0; 1537 suffix = ""; 1538 } 1539 1540 if (GETMEMB(addr, struct spa, spa_dsl_pool, spa.spa_dsl_pool) || 1541 GETMEMB(addr, struct spa, spa_root_vdev, spa.spa_root_vdev) || 1542 GETMEMB(spa.spa_root_vdev, struct vdev, vdev_children, children) || 1543 GETMEMB(spa.spa_root_vdev, struct vdev, vdev_child, childaddr) || 1544 GETMEMB(spa.spa_dsl_pool, struct dsl_pool, 1545 dp_root_dir, dp_root_dir) || 1546 GETMEMB(dp_root_dir, struct dsl_dir, dd_phys, dd.dd_phys) || 1547 GETMEMB(dp_root_dir, struct dsl_dir, 1548 dd_space_towrite, dd.dd_space_towrite) || 1549 GETMEMB(dd.dd_phys, struct dsl_dir_phys, 1550 dd_used_bytes, dsp.dd_used_bytes) || 1551 GETMEMB(dd.dd_phys, struct dsl_dir_phys, 1552 dd_compressed_bytes, dsp.dd_compressed_bytes) || 1553 GETMEMB(dd.dd_phys, struct dsl_dir_phys, 1554 dd_uncompressed_bytes, dsp.dd_uncompressed_bytes)) { 1555 return (DCMD_ERR); 1556 } 1557 1558 mdb_printf("dd_space_towrite = %llu%s %llu%s %llu%s %llu%s\n", 1559 dd.dd_space_towrite[0] >> shift, suffix, 1560 dd.dd_space_towrite[1] >> shift, suffix, 1561 dd.dd_space_towrite[2] >> shift, suffix, 1562 dd.dd_space_towrite[3] >> shift, suffix); 1563 1564 mdb_printf("dd_phys.dd_used_bytes = %llu%s\n", 1565 dsp.dd_used_bytes >> shift, suffix); 1566 mdb_printf("dd_phys.dd_compressed_bytes = %llu%s\n", 1567 dsp.dd_compressed_bytes >> shift, suffix); 1568 mdb_printf("dd_phys.dd_uncompressed_bytes = %llu%s\n", 1569 dsp.dd_uncompressed_bytes >> shift, suffix); 1570 1571 bzero(&sd, sizeof (sd)); 1572 if (mdb_pwalk("metaslab", space_cb, &sd, addr) != 0) { 1573 mdb_warn("can't walk metaslabs"); 1574 return (DCMD_ERR); 1575 } 1576 1577 mdb_printf("ms_allocmap = %llu%s %llu%s %llu%s %llu%s\n", 1578 sd.ms_allocmap[0] >> shift, suffix, 1579 sd.ms_allocmap[1] >> shift, suffix, 1580 sd.ms_allocmap[2] >> shift, suffix, 1581 sd.ms_allocmap[3] >> shift, suffix); 1582 mdb_printf("ms_freemap = %llu%s %llu%s %llu%s %llu%s\n", 1583 sd.ms_freemap[0] >> shift, suffix, 1584 sd.ms_freemap[1] >> shift, suffix, 1585 sd.ms_freemap[2] >> shift, suffix, 1586 sd.ms_freemap[3] >> shift, suffix); 1587 mdb_printf("ms_map = %llu%s\n", sd.ms_map >> shift, suffix); 1588 mdb_printf("last synced avail = %llu%s\n", sd.avail >> shift, suffix); 1589 mdb_printf("current syncing avail = %llu%s\n", 1590 sd.nowavail >> shift, suffix); 1591 1592 return (DCMD_OK); 1593 } 1594 1595 /* 1596 * ::spa_verify 1597 * 1598 * Given a spa_t, verify that that the pool is self-consistent. 1599 * Currently, it only checks to make sure that the vdev tree exists. 1600 */ 1601 /* ARGSUSED */ 1602 static int 1603 spa_verify(uintptr_t addr, uint_t flags, int argc, const mdb_arg_t *argv) 1604 { 1605 spa_t spa; 1606 1607 if (argc != 0 || !(flags & DCMD_ADDRSPEC)) 1608 return (DCMD_USAGE); 1609 1610 if (mdb_vread(&spa, sizeof (spa), addr) == -1) { 1611 mdb_warn("failed to read spa_t at %p", addr); 1612 return (DCMD_ERR); 1613 } 1614 1615 if (spa.spa_root_vdev == NULL) { 1616 mdb_printf("no vdev tree present\n"); 1617 return (DCMD_OK); 1618 } 1619 1620 return (DCMD_OK); 1621 } 1622 1623 static int 1624 spa_print_aux(spa_aux_vdev_t *sav, uint_t flags, mdb_arg_t *v, 1625 const char *name) 1626 { 1627 uintptr_t *aux; 1628 size_t len; 1629 int ret, i; 1630 1631 /* 1632 * Iterate over aux vdevs and print those out as well. This is a 1633 * little annoying because we don't have a root vdev to pass to ::vdev. 1634 * Instead, we print a single line and then call it for each child 1635 * vdev. 1636 */ 1637 if (sav->sav_count != 0) { 1638 v[1].a_type = MDB_TYPE_STRING; 1639 v[1].a_un.a_str = "-d"; 1640 v[2].a_type = MDB_TYPE_IMMEDIATE; 1641 v[2].a_un.a_val = 2; 1642 1643 len = sav->sav_count * sizeof (uintptr_t); 1644 aux = mdb_alloc(len, UM_SLEEP); 1645 if (mdb_vread(aux, len, 1646 (uintptr_t)sav->sav_vdevs) == -1) { 1647 mdb_free(aux, len); 1648 mdb_warn("failed to read l2cache vdevs at %p", 1649 sav->sav_vdevs); 1650 return (DCMD_ERR); 1651 } 1652 1653 mdb_printf("%-?s %-9s %-12s %s\n", "-", "-", "-", name); 1654 1655 for (i = 0; i < sav->sav_count; i++) { 1656 ret = mdb_call_dcmd("vdev", aux[i], flags, 3, v); 1657 if (ret != DCMD_OK) { 1658 mdb_free(aux, len); 1659 return (ret); 1660 } 1661 } 1662 1663 mdb_free(aux, len); 1664 } 1665 1666 return (0); 1667 } 1668 1669 /* 1670 * ::spa_vdevs 1671 * 1672 * -e Include error stats 1673 * 1674 * Print out a summarized list of vdevs for the given spa_t. 1675 * This is accomplished by invoking "::vdev -re" on the root vdev, as well as 1676 * iterating over the cache devices. 1677 */ 1678 /* ARGSUSED */ 1679 static int 1680 spa_vdevs(uintptr_t addr, uint_t flags, int argc, const mdb_arg_t *argv) 1681 { 1682 spa_t spa; 1683 mdb_arg_t v[3]; 1684 int errors = FALSE; 1685 int ret; 1686 1687 if (mdb_getopts(argc, argv, 1688 'e', MDB_OPT_SETBITS, TRUE, &errors, 1689 NULL) != argc) 1690 return (DCMD_USAGE); 1691 1692 if (!(flags & DCMD_ADDRSPEC)) 1693 return (DCMD_USAGE); 1694 1695 if (mdb_vread(&spa, sizeof (spa), addr) == -1) { 1696 mdb_warn("failed to read spa_t at %p", addr); 1697 return (DCMD_ERR); 1698 } 1699 1700 /* 1701 * Unitialized spa_t structures can have a NULL root vdev. 1702 */ 1703 if (spa.spa_root_vdev == NULL) { 1704 mdb_printf("no associated vdevs\n"); 1705 return (DCMD_OK); 1706 } 1707 1708 v[0].a_type = MDB_TYPE_STRING; 1709 v[0].a_un.a_str = errors ? "-re" : "-r"; 1710 1711 ret = mdb_call_dcmd("vdev", (uintptr_t)spa.spa_root_vdev, 1712 flags, 1, v); 1713 if (ret != DCMD_OK) 1714 return (ret); 1715 1716 if (spa_print_aux(&spa.spa_l2cache, flags, v, "cache") != 0 || 1717 spa_print_aux(&spa.spa_spares, flags, v, "spares") != 0) 1718 return (DCMD_ERR); 1719 1720 return (DCMD_OK); 1721 } 1722 1723 /* 1724 * ::zio 1725 * 1726 * Print a summary of zio_t and all its children. This is intended to display a 1727 * zio tree, and hence we only pick the most important pieces of information for 1728 * the main summary. More detailed information can always be found by doing a 1729 * '::print zio' on the underlying zio_t. The columns we display are: 1730 * 1731 * ADDRESS TYPE STAGE WAITER 1732 * 1733 * The 'address' column is indented by one space for each depth level as we 1734 * descend down the tree. 1735 */ 1736 1737 #define ZIO_MAXINDENT 24 1738 #define ZIO_MAXWIDTH (sizeof (uintptr_t) * 2 + ZIO_MAXINDENT) 1739 #define ZIO_WALK_SELF 0 1740 #define ZIO_WALK_CHILD 1 1741 #define ZIO_WALK_PARENT 2 1742 1743 typedef struct zio_print_args { 1744 int zpa_current_depth; 1745 int zpa_min_depth; 1746 int zpa_max_depth; 1747 int zpa_type; 1748 uint_t zpa_flags; 1749 } zio_print_args_t; 1750 1751 static int zio_child_cb(uintptr_t addr, const void *unknown, void *arg); 1752 1753 static int 1754 zio_print_cb(uintptr_t addr, const void *data, void *priv) 1755 { 1756 const zio_t *zio = data; 1757 zio_print_args_t *zpa = priv; 1758 mdb_ctf_id_t type_enum, stage_enum; 1759 int indent = zpa->zpa_current_depth; 1760 const char *type, *stage; 1761 uintptr_t laddr; 1762 1763 if (indent > ZIO_MAXINDENT) 1764 indent = ZIO_MAXINDENT; 1765 1766 if (mdb_ctf_lookup_by_name("enum zio_type", &type_enum) == -1 || 1767 mdb_ctf_lookup_by_name("enum zio_stage", &stage_enum) == -1) { 1768 mdb_warn("failed to lookup zio enums"); 1769 return (WALK_ERR); 1770 } 1771 1772 if ((type = mdb_ctf_enum_name(type_enum, zio->io_type)) != NULL) 1773 type += sizeof ("ZIO_TYPE_") - 1; 1774 else 1775 type = "?"; 1776 1777 if ((stage = mdb_ctf_enum_name(stage_enum, zio->io_stage)) != NULL) 1778 stage += sizeof ("ZIO_STAGE_") - 1; 1779 else 1780 stage = "?"; 1781 1782 if (zpa->zpa_current_depth >= zpa->zpa_min_depth) { 1783 if (zpa->zpa_flags & DCMD_PIPE_OUT) { 1784 mdb_printf("%?p\n", addr); 1785 } else { 1786 mdb_printf("%*s%-*p %-5s %-16s ", indent, "", 1787 ZIO_MAXWIDTH - indent, addr, type, stage); 1788 if (zio->io_waiter) 1789 mdb_printf("%?p\n", zio->io_waiter); 1790 else 1791 mdb_printf("-\n"); 1792 } 1793 } 1794 1795 if (zpa->zpa_current_depth >= zpa->zpa_max_depth) 1796 return (WALK_NEXT); 1797 1798 if (zpa->zpa_type == ZIO_WALK_PARENT) 1799 laddr = addr + OFFSETOF(zio_t, io_parent_list); 1800 else 1801 laddr = addr + OFFSETOF(zio_t, io_child_list); 1802 1803 zpa->zpa_current_depth++; 1804 if (mdb_pwalk("list", zio_child_cb, zpa, laddr) != 0) { 1805 mdb_warn("failed to walk zio_t children at %p\n", laddr); 1806 return (WALK_ERR); 1807 } 1808 zpa->zpa_current_depth--; 1809 1810 return (WALK_NEXT); 1811 } 1812 1813 /* ARGSUSED */ 1814 static int 1815 zio_child_cb(uintptr_t addr, const void *unknown, void *arg) 1816 { 1817 zio_link_t zl; 1818 zio_t zio; 1819 uintptr_t ziop; 1820 zio_print_args_t *zpa = arg; 1821 1822 if (mdb_vread(&zl, sizeof (zl), addr) == -1) { 1823 mdb_warn("failed to read zio_link_t at %p", addr); 1824 return (WALK_ERR); 1825 } 1826 1827 if (zpa->zpa_type == ZIO_WALK_PARENT) 1828 ziop = (uintptr_t)zl.zl_parent; 1829 else 1830 ziop = (uintptr_t)zl.zl_child; 1831 1832 if (mdb_vread(&zio, sizeof (zio_t), ziop) == -1) { 1833 mdb_warn("failed to read zio_t at %p", ziop); 1834 return (WALK_ERR); 1835 } 1836 1837 return (zio_print_cb(ziop, &zio, arg)); 1838 } 1839 1840 /* ARGSUSED */ 1841 static int 1842 zio_print(uintptr_t addr, uint_t flags, int argc, const mdb_arg_t *argv) 1843 { 1844 zio_t zio; 1845 zio_print_args_t zpa = { 0 }; 1846 1847 if (!(flags & DCMD_ADDRSPEC)) 1848 return (DCMD_USAGE); 1849 1850 if (mdb_getopts(argc, argv, 1851 'r', MDB_OPT_SETBITS, INT_MAX, &zpa.zpa_max_depth, 1852 'c', MDB_OPT_SETBITS, ZIO_WALK_CHILD, &zpa.zpa_type, 1853 'p', MDB_OPT_SETBITS, ZIO_WALK_PARENT, &zpa.zpa_type, 1854 NULL) != argc) 1855 return (DCMD_USAGE); 1856 1857 zpa.zpa_flags = flags; 1858 if (zpa.zpa_max_depth != 0) { 1859 if (zpa.zpa_type == ZIO_WALK_SELF) 1860 zpa.zpa_type = ZIO_WALK_CHILD; 1861 } else if (zpa.zpa_type != ZIO_WALK_SELF) { 1862 zpa.zpa_min_depth = 1; 1863 zpa.zpa_max_depth = 1; 1864 } 1865 1866 if (mdb_vread(&zio, sizeof (zio_t), addr) == -1) { 1867 mdb_warn("failed to read zio_t at %p", addr); 1868 return (DCMD_ERR); 1869 } 1870 1871 if (!(flags & DCMD_PIPE_OUT) && DCMD_HDRSPEC(flags)) 1872 mdb_printf("%<u>%-*s %-5s %-16s %-?s%</u>\n", ZIO_MAXWIDTH, 1873 "ADDRESS", "TYPE", "STAGE", "WAITER"); 1874 1875 if (zio_print_cb(addr, &zio, &zpa) != WALK_NEXT) 1876 return (DCMD_ERR); 1877 1878 return (DCMD_OK); 1879 } 1880 1881 /* 1882 * [addr]::zio_state 1883 * 1884 * Print a summary of all zio_t structures on the system, or for a particular 1885 * pool. This is equivalent to '::walk zio_root | ::zio'. 1886 */ 1887 /*ARGSUSED*/ 1888 static int 1889 zio_state(uintptr_t addr, uint_t flags, int argc, const mdb_arg_t *argv) 1890 { 1891 /* 1892 * MDB will remember the last address of the pipeline, so if we don't 1893 * zero this we'll end up trying to walk zio structures for a 1894 * non-existent spa_t. 1895 */ 1896 if (!(flags & DCMD_ADDRSPEC)) 1897 addr = 0; 1898 1899 return (mdb_pwalk_dcmd("zio_root", "zio", argc, argv, addr)); 1900 } 1901 1902 typedef struct txg_list_walk_data { 1903 uintptr_t lw_head[TXG_SIZE]; 1904 int lw_txgoff; 1905 int lw_maxoff; 1906 size_t lw_offset; 1907 void *lw_obj; 1908 } txg_list_walk_data_t; 1909 1910 static int 1911 txg_list_walk_init_common(mdb_walk_state_t *wsp, int txg, int maxoff) 1912 { 1913 txg_list_walk_data_t *lwd; 1914 txg_list_t list; 1915 int i; 1916 1917 lwd = mdb_alloc(sizeof (txg_list_walk_data_t), UM_SLEEP | UM_GC); 1918 if (mdb_vread(&list, sizeof (txg_list_t), wsp->walk_addr) == -1) { 1919 mdb_warn("failed to read txg_list_t at %#lx", wsp->walk_addr); 1920 return (WALK_ERR); 1921 } 1922 1923 for (i = 0; i < TXG_SIZE; i++) 1924 lwd->lw_head[i] = (uintptr_t)list.tl_head[i]; 1925 lwd->lw_offset = list.tl_offset; 1926 lwd->lw_obj = mdb_alloc(lwd->lw_offset + sizeof (txg_node_t), 1927 UM_SLEEP | UM_GC); 1928 lwd->lw_txgoff = txg; 1929 lwd->lw_maxoff = maxoff; 1930 1931 wsp->walk_addr = lwd->lw_head[lwd->lw_txgoff]; 1932 wsp->walk_data = lwd; 1933 1934 return (WALK_NEXT); 1935 } 1936 1937 static int 1938 txg_list_walk_init(mdb_walk_state_t *wsp) 1939 { 1940 return (txg_list_walk_init_common(wsp, 0, TXG_SIZE-1)); 1941 } 1942 1943 static int 1944 txg_list0_walk_init(mdb_walk_state_t *wsp) 1945 { 1946 return (txg_list_walk_init_common(wsp, 0, 0)); 1947 } 1948 1949 static int 1950 txg_list1_walk_init(mdb_walk_state_t *wsp) 1951 { 1952 return (txg_list_walk_init_common(wsp, 1, 1)); 1953 } 1954 1955 static int 1956 txg_list2_walk_init(mdb_walk_state_t *wsp) 1957 { 1958 return (txg_list_walk_init_common(wsp, 2, 2)); 1959 } 1960 1961 static int 1962 txg_list3_walk_init(mdb_walk_state_t *wsp) 1963 { 1964 return (txg_list_walk_init_common(wsp, 3, 3)); 1965 } 1966 1967 static int 1968 txg_list_walk_step(mdb_walk_state_t *wsp) 1969 { 1970 txg_list_walk_data_t *lwd = wsp->walk_data; 1971 uintptr_t addr; 1972 txg_node_t *node; 1973 int status; 1974 1975 while (wsp->walk_addr == NULL && lwd->lw_txgoff < lwd->lw_maxoff) { 1976 lwd->lw_txgoff++; 1977 wsp->walk_addr = lwd->lw_head[lwd->lw_txgoff]; 1978 } 1979 1980 if (wsp->walk_addr == NULL) 1981 return (WALK_DONE); 1982 1983 addr = wsp->walk_addr - lwd->lw_offset; 1984 1985 if (mdb_vread(lwd->lw_obj, 1986 lwd->lw_offset + sizeof (txg_node_t), addr) == -1) { 1987 mdb_warn("failed to read list element at %#lx", addr); 1988 return (WALK_ERR); 1989 } 1990 1991 status = wsp->walk_callback(addr, lwd->lw_obj, wsp->walk_cbdata); 1992 node = (txg_node_t *)((uintptr_t)lwd->lw_obj + lwd->lw_offset); 1993 wsp->walk_addr = (uintptr_t)node->tn_next[lwd->lw_txgoff]; 1994 1995 return (status); 1996 } 1997 1998 /* 1999 * ::walk spa 2000 * 2001 * Walk all named spa_t structures in the namespace. This is nothing more than 2002 * a layered avl walk. 2003 */ 2004 static int 2005 spa_walk_init(mdb_walk_state_t *wsp) 2006 { 2007 GElf_Sym sym; 2008 2009 if (wsp->walk_addr != NULL) { 2010 mdb_warn("spa walk only supports global walks\n"); 2011 return (WALK_ERR); 2012 } 2013 2014 if (mdb_lookup_by_obj(ZFS_OBJ_NAME, "spa_namespace_avl", &sym) == -1) { 2015 mdb_warn("failed to find symbol 'spa_namespace_avl'"); 2016 return (WALK_ERR); 2017 } 2018 2019 wsp->walk_addr = (uintptr_t)sym.st_value; 2020 2021 if (mdb_layered_walk("avl", wsp) == -1) { 2022 mdb_warn("failed to walk 'avl'\n"); 2023 return (WALK_ERR); 2024 } 2025 2026 return (WALK_NEXT); 2027 } 2028 2029 static int 2030 spa_walk_step(mdb_walk_state_t *wsp) 2031 { 2032 spa_t spa; 2033 2034 if (mdb_vread(&spa, sizeof (spa), wsp->walk_addr) == -1) { 2035 mdb_warn("failed to read spa_t at %p", wsp->walk_addr); 2036 return (WALK_ERR); 2037 } 2038 2039 return (wsp->walk_callback(wsp->walk_addr, &spa, wsp->walk_cbdata)); 2040 } 2041 2042 /* 2043 * [addr]::walk zio 2044 * 2045 * Walk all active zio_t structures on the system. This is simply a layered 2046 * walk on top of ::walk zio_cache, with the optional ability to limit the 2047 * structures to a particular pool. 2048 */ 2049 static int 2050 zio_walk_init(mdb_walk_state_t *wsp) 2051 { 2052 wsp->walk_data = (void *)wsp->walk_addr; 2053 2054 if (mdb_layered_walk("zio_cache", wsp) == -1) { 2055 mdb_warn("failed to walk 'zio_cache'\n"); 2056 return (WALK_ERR); 2057 } 2058 2059 return (WALK_NEXT); 2060 } 2061 2062 static int 2063 zio_walk_step(mdb_walk_state_t *wsp) 2064 { 2065 zio_t zio; 2066 2067 if (mdb_vread(&zio, sizeof (zio), wsp->walk_addr) == -1) { 2068 mdb_warn("failed to read zio_t at %p", wsp->walk_addr); 2069 return (WALK_ERR); 2070 } 2071 2072 if (wsp->walk_data != NULL && wsp->walk_data != zio.io_spa) 2073 return (WALK_NEXT); 2074 2075 return (wsp->walk_callback(wsp->walk_addr, &zio, wsp->walk_cbdata)); 2076 } 2077 2078 /* 2079 * [addr]::walk zio_root 2080 * 2081 * Walk only root zio_t structures, optionally for a particular spa_t. 2082 */ 2083 static int 2084 zio_walk_root_step(mdb_walk_state_t *wsp) 2085 { 2086 zio_t zio; 2087 2088 if (mdb_vread(&zio, sizeof (zio), wsp->walk_addr) == -1) { 2089 mdb_warn("failed to read zio_t at %p", wsp->walk_addr); 2090 return (WALK_ERR); 2091 } 2092 2093 if (wsp->walk_data != NULL && wsp->walk_data != zio.io_spa) 2094 return (WALK_NEXT); 2095 2096 /* If the parent list is not empty, ignore */ 2097 if (zio.io_parent_list.list_head.list_next != 2098 &((zio_t *)wsp->walk_addr)->io_parent_list.list_head) 2099 return (WALK_NEXT); 2100 2101 return (wsp->walk_callback(wsp->walk_addr, &zio, wsp->walk_cbdata)); 2102 } 2103 2104 #define NICENUM_BUFLEN 6 2105 2106 static int 2107 snprintfrac(char *buf, int len, 2108 uint64_t numerator, uint64_t denom, int frac_digits) 2109 { 2110 int mul = 1; 2111 int whole, frac, i; 2112 2113 for (i = frac_digits; i; i--) 2114 mul *= 10; 2115 whole = numerator / denom; 2116 frac = mul * numerator / denom - mul * whole; 2117 return (mdb_snprintf(buf, len, "%u.%0*u", whole, frac_digits, frac)); 2118 } 2119 2120 static void 2121 mdb_nicenum(uint64_t num, char *buf) 2122 { 2123 uint64_t n = num; 2124 int index = 0; 2125 char *u; 2126 2127 while (n >= 1024) { 2128 n = (n + (1024 / 2)) / 1024; /* Round up or down */ 2129 index++; 2130 } 2131 2132 u = &" \0K\0M\0G\0T\0P\0E\0"[index*2]; 2133 2134 if (index == 0) { 2135 (void) mdb_snprintf(buf, NICENUM_BUFLEN, "%llu", 2136 (u_longlong_t)n); 2137 } else if (n < 10 && (num & (num - 1)) != 0) { 2138 (void) snprintfrac(buf, NICENUM_BUFLEN, 2139 num, 1ULL << 10 * index, 2); 2140 strcat(buf, u); 2141 } else if (n < 100 && (num & (num - 1)) != 0) { 2142 (void) snprintfrac(buf, NICENUM_BUFLEN, 2143 num, 1ULL << 10 * index, 1); 2144 strcat(buf, u); 2145 } else { 2146 (void) mdb_snprintf(buf, NICENUM_BUFLEN, "%llu%s", 2147 (u_longlong_t)n, u); 2148 } 2149 } 2150 2151 /* 2152 * ::zfs_blkstats 2153 * 2154 * -v print verbose per-level information 2155 * 2156 */ 2157 static int 2158 zfs_blkstats(uintptr_t addr, uint_t flags, int argc, const mdb_arg_t *argv) 2159 { 2160 boolean_t verbose = B_FALSE; 2161 zfs_all_blkstats_t stats; 2162 dmu_object_type_t t; 2163 zfs_blkstat_t *tzb; 2164 uint64_t ditto; 2165 dmu_object_type_info_t dmu_ot[DMU_OT_NUMTYPES + 10]; 2166 /* +10 in case it grew */ 2167 2168 if (mdb_readvar(&dmu_ot, "dmu_ot") == -1) { 2169 mdb_warn("failed to read 'dmu_ot'"); 2170 return (DCMD_ERR); 2171 } 2172 2173 if (mdb_getopts(argc, argv, 2174 'v', MDB_OPT_SETBITS, TRUE, &verbose, 2175 NULL) != argc) 2176 return (DCMD_USAGE); 2177 2178 if (!(flags & DCMD_ADDRSPEC)) 2179 return (DCMD_USAGE); 2180 2181 if (GETMEMB(addr, struct spa, spa_dsl_pool, addr) || 2182 GETMEMB(addr, struct dsl_pool, dp_blkstats, addr) || 2183 mdb_vread(&stats, sizeof (zfs_all_blkstats_t), addr) == -1) { 2184 mdb_warn("failed to read data at %p;", addr); 2185 mdb_printf("maybe no stats? run \"zpool scrub\" first."); 2186 return (DCMD_ERR); 2187 } 2188 2189 tzb = &stats.zab_type[DN_MAX_LEVELS][DMU_OT_TOTAL]; 2190 if (tzb->zb_gangs != 0) { 2191 mdb_printf("Ganged blocks: %llu\n", 2192 (longlong_t)tzb->zb_gangs); 2193 } 2194 2195 ditto = tzb->zb_ditto_2_of_2_samevdev + tzb->zb_ditto_2_of_3_samevdev + 2196 tzb->zb_ditto_3_of_3_samevdev; 2197 if (ditto != 0) { 2198 mdb_printf("Dittoed blocks on same vdev: %llu\n", 2199 (longlong_t)ditto); 2200 } 2201 2202 mdb_printf("\nBlocks\tLSIZE\tPSIZE\tASIZE" 2203 "\t avg\t comp\t%%Total\tType\n"); 2204 2205 for (t = 0; t <= DMU_OT_TOTAL; t++) { 2206 char csize[NICENUM_BUFLEN], lsize[NICENUM_BUFLEN]; 2207 char psize[NICENUM_BUFLEN], asize[NICENUM_BUFLEN]; 2208 char avg[NICENUM_BUFLEN]; 2209 char comp[NICENUM_BUFLEN], pct[NICENUM_BUFLEN]; 2210 char typename[64]; 2211 int l; 2212 2213 2214 if (t == DMU_OT_DEFERRED) 2215 strcpy(typename, "deferred free"); 2216 else if (t == DMU_OT_OTHER) 2217 strcpy(typename, "other"); 2218 else if (t == DMU_OT_TOTAL) 2219 strcpy(typename, "Total"); 2220 else if (mdb_readstr(typename, sizeof (typename), 2221 (uintptr_t)dmu_ot[t].ot_name) == -1) { 2222 mdb_warn("failed to read type name"); 2223 return (DCMD_ERR); 2224 } 2225 2226 if (stats.zab_type[DN_MAX_LEVELS][t].zb_asize == 0) 2227 continue; 2228 2229 for (l = -1; l < DN_MAX_LEVELS; l++) { 2230 int level = (l == -1 ? DN_MAX_LEVELS : l); 2231 zfs_blkstat_t *zb = &stats.zab_type[level][t]; 2232 2233 if (zb->zb_asize == 0) 2234 continue; 2235 2236 /* 2237 * Don't print each level unless requested. 2238 */ 2239 if (!verbose && level != DN_MAX_LEVELS) 2240 continue; 2241 2242 /* 2243 * If all the space is level 0, don't print the 2244 * level 0 separately. 2245 */ 2246 if (level == 0 && zb->zb_asize == 2247 stats.zab_type[DN_MAX_LEVELS][t].zb_asize) 2248 continue; 2249 2250 mdb_nicenum(zb->zb_count, csize); 2251 mdb_nicenum(zb->zb_lsize, lsize); 2252 mdb_nicenum(zb->zb_psize, psize); 2253 mdb_nicenum(zb->zb_asize, asize); 2254 mdb_nicenum(zb->zb_asize / zb->zb_count, avg); 2255 (void) snprintfrac(comp, NICENUM_BUFLEN, 2256 zb->zb_lsize, zb->zb_psize, 2); 2257 (void) snprintfrac(pct, NICENUM_BUFLEN, 2258 100 * zb->zb_asize, tzb->zb_asize, 2); 2259 2260 mdb_printf("%6s\t%5s\t%5s\t%5s\t%5s" 2261 "\t%5s\t%6s\t", 2262 csize, lsize, psize, asize, avg, comp, pct); 2263 2264 if (level == DN_MAX_LEVELS) 2265 mdb_printf("%s\n", typename); 2266 else 2267 mdb_printf(" L%d %s\n", 2268 level, typename); 2269 } 2270 } 2271 2272 return (DCMD_OK); 2273 } 2274 2275 /* ARGSUSED */ 2276 static int 2277 reference_cb(uintptr_t addr, const void *ignored, void *arg) 2278 { 2279 static int gotid; 2280 static mdb_ctf_id_t ref_id; 2281 uintptr_t ref_holder; 2282 uintptr_t ref_removed; 2283 uint64_t ref_number; 2284 boolean_t holder_is_str = B_FALSE; 2285 char holder_str[128]; 2286 boolean_t removed = (boolean_t)arg; 2287 2288 if (!gotid) { 2289 if (mdb_ctf_lookup_by_name("struct reference", &ref_id) == -1) { 2290 mdb_warn("couldn't find struct reference"); 2291 return (WALK_ERR); 2292 } 2293 gotid = TRUE; 2294 } 2295 2296 if (GETMEMBID(addr, &ref_id, ref_holder, ref_holder) || 2297 GETMEMBID(addr, &ref_id, ref_removed, ref_removed) || 2298 GETMEMBID(addr, &ref_id, ref_number, ref_number)) 2299 return (WALK_ERR); 2300 2301 if (mdb_readstr(holder_str, sizeof (holder_str), ref_holder) != -1) 2302 holder_is_str = strisprint(holder_str); 2303 2304 if (removed) 2305 mdb_printf("removed "); 2306 mdb_printf("reference "); 2307 if (ref_number != 1) 2308 mdb_printf("with count=%llu ", ref_number); 2309 mdb_printf("with tag %p", (void*)ref_holder); 2310 if (holder_is_str) 2311 mdb_printf(" \"%s\"", holder_str); 2312 mdb_printf(", held at:\n"); 2313 2314 (void) mdb_call_dcmd("whatis", addr, DCMD_ADDRSPEC, 0, NULL); 2315 2316 if (removed) { 2317 mdb_printf("removed at:\n"); 2318 (void) mdb_call_dcmd("whatis", ref_removed, 2319 DCMD_ADDRSPEC, 0, NULL); 2320 } 2321 2322 mdb_printf("\n"); 2323 2324 return (WALK_NEXT); 2325 } 2326 2327 /* ARGSUSED */ 2328 static int 2329 refcount(uintptr_t addr, uint_t flags, int argc, const mdb_arg_t *argv) 2330 { 2331 uint64_t rc_count, rc_removed_count; 2332 uintptr_t rc_list, rc_removed; 2333 static int gotid; 2334 static mdb_ctf_id_t rc_id; 2335 ulong_t off; 2336 2337 if (!(flags & DCMD_ADDRSPEC)) 2338 return (DCMD_USAGE); 2339 2340 if (!gotid) { 2341 /* 2342 * The refcount structure is different when compiled debug 2343 * vs nondebug. Therefore, we want to make sure we get the 2344 * refcount definition from the ZFS module, in case it has 2345 * been compiled debug but genunix is nondebug. 2346 */ 2347 if (mdb_ctf_lookup_by_name("struct " ZFS_OBJ_NAME "`refcount", 2348 &rc_id) == -1) { 2349 mdb_warn("couldn't find struct refcount"); 2350 return (DCMD_ERR); 2351 } 2352 gotid = TRUE; 2353 } 2354 2355 if (GETMEMBID(addr, &rc_id, rc_count, rc_count) || 2356 GETMEMBID(addr, &rc_id, rc_removed_count, rc_removed_count)) 2357 return (DCMD_ERR); 2358 2359 mdb_printf("refcount_t at %p has %llu current holds, " 2360 "%llu recently released holds\n", 2361 addr, (longlong_t)rc_count, (longlong_t)rc_removed_count); 2362 2363 if (rc_count > 0) 2364 mdb_printf("current holds:\n"); 2365 if (mdb_ctf_offsetof(rc_id, "rc_list", &off) == -1) 2366 return (DCMD_ERR); 2367 rc_list = addr + off/NBBY; 2368 mdb_pwalk("list", reference_cb, (void*)B_FALSE, rc_list); 2369 2370 if (rc_removed_count > 0) 2371 mdb_printf("released holds:\n"); 2372 if (mdb_ctf_offsetof(rc_id, "rc_removed", &off) == -1) 2373 return (DCMD_ERR); 2374 rc_removed = addr + off/NBBY; 2375 mdb_pwalk("list", reference_cb, (void*)B_TRUE, rc_removed); 2376 2377 return (DCMD_OK); 2378 } 2379 2380 /* ARGSUSED */ 2381 static int 2382 sa_attr_table(uintptr_t addr, uint_t flags, int argc, const mdb_arg_t *argv) 2383 { 2384 sa_attr_table_t *table; 2385 sa_os_t sa_os; 2386 char *name; 2387 int i; 2388 2389 if (mdb_vread(&sa_os, sizeof (sa_os_t), addr) == -1) { 2390 mdb_warn("failed to read sa_os at %p", addr); 2391 return (DCMD_ERR); 2392 } 2393 2394 table = mdb_alloc(sizeof (sa_attr_table_t) * sa_os.sa_num_attrs, 2395 UM_SLEEP | UM_GC); 2396 name = mdb_alloc(MAXPATHLEN, UM_SLEEP | UM_GC); 2397 2398 if (mdb_vread(table, sizeof (sa_attr_table_t) * sa_os.sa_num_attrs, 2399 (uintptr_t)sa_os.sa_attr_table) == -1) { 2400 mdb_warn("failed to read sa_os at %p", addr); 2401 return (DCMD_ERR); 2402 } 2403 2404 mdb_printf("%<u>%-10s %-10s %-10s %-10s %s%</u>\n", 2405 "ATTR ID", "REGISTERED", "LENGTH", "BSWAP", "NAME"); 2406 for (i = 0; i != sa_os.sa_num_attrs; i++) { 2407 mdb_readstr(name, MAXPATHLEN, (uintptr_t)table[i].sa_name); 2408 mdb_printf("%5x %8x %8x %8x %-s\n", 2409 (int)table[i].sa_attr, (int)table[i].sa_registered, 2410 (int)table[i].sa_length, table[i].sa_byteswap, name); 2411 } 2412 2413 return (DCMD_OK); 2414 } 2415 2416 static int 2417 sa_get_off_table(uintptr_t addr, uint32_t **off_tab, int attr_count) 2418 { 2419 uintptr_t idx_table; 2420 2421 if (GETMEMB(addr, struct sa_idx_tab, sa_idx_tab, idx_table)) { 2422 mdb_printf("can't find offset table in sa_idx_tab\n"); 2423 return (-1); 2424 } 2425 2426 *off_tab = mdb_alloc(attr_count * sizeof (uint32_t), 2427 UM_SLEEP | UM_GC); 2428 2429 if (mdb_vread(*off_tab, 2430 attr_count * sizeof (uint32_t), idx_table) == -1) { 2431 mdb_warn("failed to attribute offset table %p", idx_table); 2432 return (-1); 2433 } 2434 2435 return (DCMD_OK); 2436 } 2437 2438 /*ARGSUSED*/ 2439 static int 2440 sa_attr_print(uintptr_t addr, uint_t flags, int argc, const mdb_arg_t *argv) 2441 { 2442 uint32_t *offset_tab; 2443 int attr_count; 2444 uint64_t attr_id; 2445 uintptr_t attr_addr; 2446 uintptr_t bonus_tab, spill_tab; 2447 uintptr_t db_bonus, db_spill; 2448 uintptr_t os, os_sa; 2449 uintptr_t db_data; 2450 2451 if (argc != 1) 2452 return (DCMD_USAGE); 2453 2454 if (argv[0].a_type == MDB_TYPE_STRING) 2455 attr_id = mdb_strtoull(argv[0].a_un.a_str); 2456 else 2457 return (DCMD_USAGE); 2458 2459 if (GETMEMB(addr, struct sa_handle, sa_bonus_tab, bonus_tab) || 2460 GETMEMB(addr, struct sa_handle, sa_spill_tab, spill_tab) || 2461 GETMEMB(addr, struct sa_handle, sa_os, os) || 2462 GETMEMB(addr, struct sa_handle, sa_bonus, db_bonus) || 2463 GETMEMB(addr, struct sa_handle, sa_spill, db_spill)) { 2464 mdb_printf("Can't find necessary information in sa_handle " 2465 "in sa_handle\n"); 2466 return (DCMD_ERR); 2467 } 2468 2469 if (GETMEMB(os, struct objset, os_sa, os_sa)) { 2470 mdb_printf("Can't find os_sa in objset\n"); 2471 return (DCMD_ERR); 2472 } 2473 2474 if (GETMEMB(os_sa, struct sa_os, sa_num_attrs, attr_count)) { 2475 mdb_printf("Can't find sa_num_attrs\n"); 2476 return (DCMD_ERR); 2477 } 2478 2479 if (attr_id > attr_count) { 2480 mdb_printf("attribute id number is out of range\n"); 2481 return (DCMD_ERR); 2482 } 2483 2484 if (bonus_tab) { 2485 if (sa_get_off_table(bonus_tab, &offset_tab, 2486 attr_count) == -1) { 2487 return (DCMD_ERR); 2488 } 2489 2490 if (GETMEMB(db_bonus, struct dmu_buf, db_data, db_data)) { 2491 mdb_printf("can't find db_data in bonus dbuf\n"); 2492 return (DCMD_ERR); 2493 } 2494 } 2495 2496 if (bonus_tab && !TOC_ATTR_PRESENT(offset_tab[attr_id]) && 2497 spill_tab == NULL) { 2498 mdb_printf("Attribute does not exist\n"); 2499 return (DCMD_ERR); 2500 } else if (!TOC_ATTR_PRESENT(offset_tab[attr_id]) && spill_tab) { 2501 if (sa_get_off_table(spill_tab, &offset_tab, 2502 attr_count) == -1) { 2503 return (DCMD_ERR); 2504 } 2505 if (GETMEMB(db_spill, struct dmu_buf, db_data, db_data)) { 2506 mdb_printf("can't find db_data in spill dbuf\n"); 2507 return (DCMD_ERR); 2508 } 2509 if (!TOC_ATTR_PRESENT(offset_tab[attr_id])) { 2510 mdb_printf("Attribute does not exist\n"); 2511 return (DCMD_ERR); 2512 } 2513 } 2514 attr_addr = db_data + TOC_OFF(offset_tab[attr_id]); 2515 mdb_printf("%p\n", attr_addr); 2516 return (DCMD_OK); 2517 } 2518 2519 /* ARGSUSED */ 2520 static int 2521 zfs_ace_print_common(uintptr_t addr, uint_t flags, 2522 uint64_t id, uint32_t access_mask, uint16_t ace_flags, 2523 uint16_t ace_type, int verbose) 2524 { 2525 if (DCMD_HDRSPEC(flags) && !verbose) 2526 mdb_printf("%<u>%-?s %-8s %-8s %-8s %s%</u>\n", 2527 "ADDR", "FLAGS", "MASK", "TYPE", "ID"); 2528 2529 if (!verbose) { 2530 mdb_printf("%0?p %-8x %-8x %-8x %-llx\n", addr, 2531 ace_flags, access_mask, ace_type, id); 2532 return (DCMD_OK); 2533 } 2534 2535 switch (ace_flags & ACE_TYPE_FLAGS) { 2536 case ACE_OWNER: 2537 mdb_printf("owner@:"); 2538 break; 2539 case (ACE_IDENTIFIER_GROUP | ACE_GROUP): 2540 mdb_printf("group@:"); 2541 break; 2542 case ACE_EVERYONE: 2543 mdb_printf("everyone@:"); 2544 break; 2545 case ACE_IDENTIFIER_GROUP: 2546 mdb_printf("group:%llx:", (u_longlong_t)id); 2547 break; 2548 case 0: /* User entry */ 2549 mdb_printf("user:%llx:", (u_longlong_t)id); 2550 break; 2551 } 2552 2553 /* print out permission mask */ 2554 if (access_mask & ACE_READ_DATA) 2555 mdb_printf("r"); 2556 else 2557 mdb_printf("-"); 2558 if (access_mask & ACE_WRITE_DATA) 2559 mdb_printf("w"); 2560 else 2561 mdb_printf("-"); 2562 if (access_mask & ACE_EXECUTE) 2563 mdb_printf("x"); 2564 else 2565 mdb_printf("-"); 2566 if (access_mask & ACE_APPEND_DATA) 2567 mdb_printf("p"); 2568 else 2569 mdb_printf("-"); 2570 if (access_mask & ACE_DELETE) 2571 mdb_printf("d"); 2572 else 2573 mdb_printf("-"); 2574 if (access_mask & ACE_DELETE_CHILD) 2575 mdb_printf("D"); 2576 else 2577 mdb_printf("-"); 2578 if (access_mask & ACE_READ_ATTRIBUTES) 2579 mdb_printf("a"); 2580 else 2581 mdb_printf("-"); 2582 if (access_mask & ACE_WRITE_ATTRIBUTES) 2583 mdb_printf("A"); 2584 else 2585 mdb_printf("-"); 2586 if (access_mask & ACE_READ_NAMED_ATTRS) 2587 mdb_printf("R"); 2588 else 2589 mdb_printf("-"); 2590 if (access_mask & ACE_WRITE_NAMED_ATTRS) 2591 mdb_printf("W"); 2592 else 2593 mdb_printf("-"); 2594 if (access_mask & ACE_READ_ACL) 2595 mdb_printf("c"); 2596 else 2597 mdb_printf("-"); 2598 if (access_mask & ACE_WRITE_ACL) 2599 mdb_printf("C"); 2600 else 2601 mdb_printf("-"); 2602 if (access_mask & ACE_WRITE_OWNER) 2603 mdb_printf("o"); 2604 else 2605 mdb_printf("-"); 2606 if (access_mask & ACE_SYNCHRONIZE) 2607 mdb_printf("s"); 2608 else 2609 mdb_printf("-"); 2610 2611 mdb_printf(":"); 2612 2613 /* Print out inheritance flags */ 2614 if (ace_flags & ACE_FILE_INHERIT_ACE) 2615 mdb_printf("f"); 2616 else 2617 mdb_printf("-"); 2618 if (ace_flags & ACE_DIRECTORY_INHERIT_ACE) 2619 mdb_printf("d"); 2620 else 2621 mdb_printf("-"); 2622 if (ace_flags & ACE_INHERIT_ONLY_ACE) 2623 mdb_printf("i"); 2624 else 2625 mdb_printf("-"); 2626 if (ace_flags & ACE_NO_PROPAGATE_INHERIT_ACE) 2627 mdb_printf("n"); 2628 else 2629 mdb_printf("-"); 2630 if (ace_flags & ACE_SUCCESSFUL_ACCESS_ACE_FLAG) 2631 mdb_printf("S"); 2632 else 2633 mdb_printf("-"); 2634 if (ace_flags & ACE_FAILED_ACCESS_ACE_FLAG) 2635 mdb_printf("F"); 2636 else 2637 mdb_printf("-"); 2638 if (ace_flags & ACE_INHERITED_ACE) 2639 mdb_printf("I"); 2640 else 2641 mdb_printf("-"); 2642 2643 switch (ace_type) { 2644 case ACE_ACCESS_ALLOWED_ACE_TYPE: 2645 mdb_printf(":allow\n"); 2646 break; 2647 case ACE_ACCESS_DENIED_ACE_TYPE: 2648 mdb_printf(":deny\n"); 2649 break; 2650 case ACE_SYSTEM_AUDIT_ACE_TYPE: 2651 mdb_printf(":audit\n"); 2652 break; 2653 case ACE_SYSTEM_ALARM_ACE_TYPE: 2654 mdb_printf(":alarm\n"); 2655 break; 2656 default: 2657 mdb_printf(":?\n"); 2658 } 2659 return (DCMD_OK); 2660 } 2661 2662 /* ARGSUSED */ 2663 static int 2664 zfs_ace_print(uintptr_t addr, uint_t flags, int argc, const mdb_arg_t *argv) 2665 { 2666 zfs_ace_t zace; 2667 int verbose = FALSE; 2668 uint64_t id; 2669 2670 if (!(flags & DCMD_ADDRSPEC)) 2671 return (DCMD_USAGE); 2672 2673 if (mdb_getopts(argc, argv, 2674 'v', MDB_OPT_SETBITS, TRUE, &verbose, TRUE, NULL) != argc) 2675 return (DCMD_USAGE); 2676 2677 if (mdb_vread(&zace, sizeof (zfs_ace_t), addr) == -1) { 2678 mdb_warn("failed to read zfs_ace_t"); 2679 return (DCMD_ERR); 2680 } 2681 2682 if ((zace.z_hdr.z_flags & ACE_TYPE_FLAGS) == 0 || 2683 (zace.z_hdr.z_flags & ACE_TYPE_FLAGS) == ACE_IDENTIFIER_GROUP) 2684 id = zace.z_fuid; 2685 else 2686 id = -1; 2687 2688 return (zfs_ace_print_common(addr, flags, id, zace.z_hdr.z_access_mask, 2689 zace.z_hdr.z_flags, zace.z_hdr.z_type, verbose)); 2690 } 2691 2692 /* ARGSUSED */ 2693 static int 2694 zfs_ace0_print(uintptr_t addr, uint_t flags, int argc, const mdb_arg_t *argv) 2695 { 2696 ace_t ace; 2697 uint64_t id; 2698 int verbose = FALSE; 2699 2700 if (!(flags & DCMD_ADDRSPEC)) 2701 return (DCMD_USAGE); 2702 2703 if (mdb_getopts(argc, argv, 2704 'v', MDB_OPT_SETBITS, TRUE, &verbose, TRUE, NULL) != argc) 2705 return (DCMD_USAGE); 2706 2707 if (mdb_vread(&ace, sizeof (ace_t), addr) == -1) { 2708 mdb_warn("failed to read ace_t"); 2709 return (DCMD_ERR); 2710 } 2711 2712 if ((ace.a_flags & ACE_TYPE_FLAGS) == 0 || 2713 (ace.a_flags & ACE_TYPE_FLAGS) == ACE_IDENTIFIER_GROUP) 2714 id = ace.a_who; 2715 else 2716 id = -1; 2717 2718 return (zfs_ace_print_common(addr, flags, id, ace.a_access_mask, 2719 ace.a_flags, ace.a_type, verbose)); 2720 } 2721 2722 typedef struct acl_dump_args { 2723 int a_argc; 2724 const mdb_arg_t *a_argv; 2725 uint16_t a_version; 2726 int a_flags; 2727 } acl_dump_args_t; 2728 2729 /* ARGSUSED */ 2730 static int 2731 acl_aces_cb(uintptr_t addr, const void *unknown, void *arg) 2732 { 2733 acl_dump_args_t *acl_args = (acl_dump_args_t *)arg; 2734 2735 if (acl_args->a_version == 1) { 2736 if (mdb_call_dcmd("zfs_ace", addr, 2737 DCMD_ADDRSPEC|acl_args->a_flags, acl_args->a_argc, 2738 acl_args->a_argv) != DCMD_OK) { 2739 return (WALK_ERR); 2740 } 2741 } else { 2742 if (mdb_call_dcmd("zfs_ace0", addr, 2743 DCMD_ADDRSPEC|acl_args->a_flags, acl_args->a_argc, 2744 acl_args->a_argv) != DCMD_OK) { 2745 return (WALK_ERR); 2746 } 2747 } 2748 acl_args->a_flags = DCMD_LOOP; 2749 return (WALK_NEXT); 2750 } 2751 2752 /* ARGSUSED */ 2753 static int 2754 acl_cb(uintptr_t addr, const void *unknown, void *arg) 2755 { 2756 acl_dump_args_t *acl_args = (acl_dump_args_t *)arg; 2757 2758 if (acl_args->a_version == 1) { 2759 if (mdb_pwalk("zfs_acl_node_aces", acl_aces_cb, 2760 arg, addr) != 0) { 2761 mdb_warn("can't walk ACEs"); 2762 return (DCMD_ERR); 2763 } 2764 } else { 2765 if (mdb_pwalk("zfs_acl_node_aces0", acl_aces_cb, 2766 arg, addr) != 0) { 2767 mdb_warn("can't walk ACEs"); 2768 return (DCMD_ERR); 2769 } 2770 } 2771 return (WALK_NEXT); 2772 } 2773 2774 /* ARGSUSED */ 2775 static int 2776 zfs_acl_dump(uintptr_t addr, uint_t flags, int argc, const mdb_arg_t *argv) 2777 { 2778 zfs_acl_t zacl; 2779 int verbose = FALSE; 2780 acl_dump_args_t acl_args; 2781 2782 if (!(flags & DCMD_ADDRSPEC)) 2783 return (DCMD_USAGE); 2784 2785 if (mdb_getopts(argc, argv, 2786 'v', MDB_OPT_SETBITS, TRUE, &verbose, TRUE, NULL) != argc) 2787 return (DCMD_USAGE); 2788 2789 if (mdb_vread(&zacl, sizeof (zfs_acl_t), addr) == -1) { 2790 mdb_warn("failed to read zfs_acl_t"); 2791 return (DCMD_ERR); 2792 } 2793 2794 acl_args.a_argc = argc; 2795 acl_args.a_argv = argv; 2796 acl_args.a_version = zacl.z_version; 2797 acl_args.a_flags = DCMD_LOOPFIRST; 2798 2799 if (mdb_pwalk("zfs_acl_node", acl_cb, &acl_args, addr) != 0) { 2800 mdb_warn("can't walk ACL"); 2801 return (DCMD_ERR); 2802 } 2803 2804 return (DCMD_OK); 2805 } 2806 2807 /* ARGSUSED */ 2808 static int 2809 zfs_acl_node_walk_init(mdb_walk_state_t *wsp) 2810 { 2811 if (wsp->walk_addr == NULL) { 2812 mdb_warn("must supply address of zfs_acl_node_t\n"); 2813 return (WALK_ERR); 2814 } 2815 2816 wsp->walk_addr += OFFSETOF(zfs_acl_t, z_acl); 2817 2818 if (mdb_layered_walk("list", wsp) == -1) { 2819 mdb_warn("failed to walk 'list'\n"); 2820 return (WALK_ERR); 2821 } 2822 2823 return (WALK_NEXT); 2824 } 2825 2826 static int 2827 zfs_acl_node_walk_step(mdb_walk_state_t *wsp) 2828 { 2829 zfs_acl_node_t aclnode; 2830 2831 if (mdb_vread(&aclnode, sizeof (zfs_acl_node_t), 2832 wsp->walk_addr) == -1) { 2833 mdb_warn("failed to read zfs_acl_node at %p", wsp->walk_addr); 2834 return (WALK_ERR); 2835 } 2836 2837 return (wsp->walk_callback(wsp->walk_addr, &aclnode, wsp->walk_cbdata)); 2838 } 2839 2840 typedef struct ace_walk_data { 2841 int ace_count; 2842 int ace_version; 2843 } ace_walk_data_t; 2844 2845 static int 2846 zfs_aces_walk_init_common(mdb_walk_state_t *wsp, int version, 2847 int ace_count, uintptr_t ace_data) 2848 { 2849 ace_walk_data_t *ace_walk_data; 2850 2851 if (wsp->walk_addr == NULL) { 2852 mdb_warn("must supply address of zfs_acl_node_t\n"); 2853 return (WALK_ERR); 2854 } 2855 2856 ace_walk_data = mdb_alloc(sizeof (ace_walk_data_t), UM_SLEEP | UM_GC); 2857 2858 ace_walk_data->ace_count = ace_count; 2859 ace_walk_data->ace_version = version; 2860 2861 wsp->walk_addr = ace_data; 2862 wsp->walk_data = ace_walk_data; 2863 2864 return (WALK_NEXT); 2865 } 2866 2867 static int 2868 zfs_acl_node_aces_walk_init_common(mdb_walk_state_t *wsp, int version) 2869 { 2870 static int gotid; 2871 static mdb_ctf_id_t acl_id; 2872 int z_ace_count; 2873 uintptr_t z_acldata; 2874 2875 if (!gotid) { 2876 if (mdb_ctf_lookup_by_name("struct zfs_acl_node", 2877 &acl_id) == -1) { 2878 mdb_warn("couldn't find struct zfs_acl_node"); 2879 return (DCMD_ERR); 2880 } 2881 gotid = TRUE; 2882 } 2883 2884 if (GETMEMBID(wsp->walk_addr, &acl_id, z_ace_count, z_ace_count)) { 2885 return (DCMD_ERR); 2886 } 2887 if (GETMEMBID(wsp->walk_addr, &acl_id, z_acldata, z_acldata)) { 2888 return (DCMD_ERR); 2889 } 2890 2891 return (zfs_aces_walk_init_common(wsp, version, 2892 z_ace_count, z_acldata)); 2893 } 2894 2895 /* ARGSUSED */ 2896 static int 2897 zfs_acl_node_aces_walk_init(mdb_walk_state_t *wsp) 2898 { 2899 return (zfs_acl_node_aces_walk_init_common(wsp, 1)); 2900 } 2901 2902 /* ARGSUSED */ 2903 static int 2904 zfs_acl_node_aces0_walk_init(mdb_walk_state_t *wsp) 2905 { 2906 return (zfs_acl_node_aces_walk_init_common(wsp, 0)); 2907 } 2908 2909 static int 2910 zfs_aces_walk_step(mdb_walk_state_t *wsp) 2911 { 2912 ace_walk_data_t *ace_data = wsp->walk_data; 2913 zfs_ace_t zace; 2914 ace_t *acep; 2915 int status; 2916 int entry_type; 2917 int allow_type; 2918 uintptr_t ptr; 2919 2920 if (ace_data->ace_count == 0) 2921 return (WALK_DONE); 2922 2923 if (mdb_vread(&zace, sizeof (zfs_ace_t), wsp->walk_addr) == -1) { 2924 mdb_warn("failed to read zfs_ace_t at %#lx", 2925 wsp->walk_addr); 2926 return (WALK_ERR); 2927 } 2928 2929 switch (ace_data->ace_version) { 2930 case 0: 2931 acep = (ace_t *)&zace; 2932 entry_type = acep->a_flags & ACE_TYPE_FLAGS; 2933 allow_type = acep->a_type; 2934 break; 2935 case 1: 2936 entry_type = zace.z_hdr.z_flags & ACE_TYPE_FLAGS; 2937 allow_type = zace.z_hdr.z_type; 2938 break; 2939 default: 2940 return (WALK_ERR); 2941 } 2942 2943 ptr = (uintptr_t)wsp->walk_addr; 2944 switch (entry_type) { 2945 case ACE_OWNER: 2946 case ACE_EVERYONE: 2947 case (ACE_IDENTIFIER_GROUP | ACE_GROUP): 2948 ptr += ace_data->ace_version == 0 ? 2949 sizeof (ace_t) : sizeof (zfs_ace_hdr_t); 2950 break; 2951 case ACE_IDENTIFIER_GROUP: 2952 default: 2953 switch (allow_type) { 2954 case ACE_ACCESS_ALLOWED_OBJECT_ACE_TYPE: 2955 case ACE_ACCESS_DENIED_OBJECT_ACE_TYPE: 2956 case ACE_SYSTEM_AUDIT_OBJECT_ACE_TYPE: 2957 case ACE_SYSTEM_ALARM_OBJECT_ACE_TYPE: 2958 ptr += ace_data->ace_version == 0 ? 2959 sizeof (ace_t) : sizeof (zfs_object_ace_t); 2960 break; 2961 default: 2962 ptr += ace_data->ace_version == 0 ? 2963 sizeof (ace_t) : sizeof (zfs_ace_t); 2964 break; 2965 } 2966 } 2967 2968 ace_data->ace_count--; 2969 status = wsp->walk_callback(wsp->walk_addr, 2970 (void *)(uintptr_t)&zace, wsp->walk_cbdata); 2971 2972 wsp->walk_addr = ptr; 2973 return (status); 2974 } 2975 2976 /* 2977 * MDB module linkage information: 2978 * 2979 * We declare a list of structures describing our dcmds, and a function 2980 * named _mdb_init to return a pointer to our module information. 2981 */ 2982 2983 static const mdb_dcmd_t dcmds[] = { 2984 { "arc", "[-bkmg]", "print ARC variables", arc_print }, 2985 { "blkptr", ":", "print blkptr_t", blkptr }, 2986 { "dbuf", ":", "print dmu_buf_impl_t", dbuf }, 2987 { "dbuf_stats", ":", "dbuf stats", dbuf_stats }, 2988 { "dbufs", 2989 "\t[-O objset_t*] [-n objset_name | \"mos\"] " 2990 "[-o object | \"mdn\"] \n" 2991 "\t[-l level] [-b blkid | \"bonus\"]", 2992 "find dmu_buf_impl_t's that match specified criteria", dbufs }, 2993 { "abuf_find", "dva_word[0] dva_word[1]", 2994 "find arc_buf_hdr_t of a specified DVA", 2995 abuf_find }, 2996 { "spa", "?[-cv]", "spa_t summary", spa_print }, 2997 { "spa_config", ":", "print spa_t configuration", spa_print_config }, 2998 { "spa_verify", ":", "verify spa_t consistency", spa_verify }, 2999 { "spa_space", ":[-b]", "print spa_t on-disk space usage", spa_space }, 3000 { "spa_vdevs", ":", "given a spa_t, print vdev summary", spa_vdevs }, 3001 { "vdev", ":[-re]\n" 3002 "\t-r display recursively\n" 3003 "\t-e print statistics", 3004 "vdev_t summary", vdev_print }, 3005 { "zio", ":[cpr]\n" 3006 "\t-c display children\n" 3007 "\t-p display parents\n" 3008 "\t-r display recursively", 3009 "zio_t summary", zio_print }, 3010 { "zio_state", "?", "print out all zio_t structures on system or " 3011 "for a particular pool", zio_state }, 3012 { "zfs_blkstats", ":[-v]", 3013 "given a spa_t, print block type stats from last scrub", 3014 zfs_blkstats }, 3015 { "zfs_params", "", "print zfs tunable parameters", zfs_params }, 3016 { "refcount", "", "print refcount_t holders", refcount }, 3017 { "zap_leaf", "", "print zap_leaf_phys_t", zap_leaf }, 3018 { "zfs_aces", ":[-v]", "print all ACEs from a zfs_acl_t", 3019 zfs_acl_dump }, 3020 { "zfs_ace", ":[-v]", "print zfs_ace", zfs_ace_print }, 3021 { "zfs_ace0", ":[-v]", "print zfs_ace0", zfs_ace0_print }, 3022 { "sa_attr_table", ":", "print SA attribute table from sa_os_t", 3023 sa_attr_table}, 3024 { "sa_attr", ": attr_id", 3025 "print SA attribute address when given sa_handle_t", sa_attr_print}, 3026 { "zfs_dbgmsg", ":[-v]", 3027 "print zfs debug log", dbgmsg}, 3028 { NULL } 3029 }; 3030 3031 static const mdb_walker_t walkers[] = { 3032 { "zms_freelist", "walk ZFS metaslab freelist", 3033 freelist_walk_init, freelist_walk_step, NULL }, 3034 { "txg_list", "given any txg_list_t *, walk all entries in all txgs", 3035 txg_list_walk_init, txg_list_walk_step, NULL }, 3036 { "txg_list0", "given any txg_list_t *, walk all entries in txg 0", 3037 txg_list0_walk_init, txg_list_walk_step, NULL }, 3038 { "txg_list1", "given any txg_list_t *, walk all entries in txg 1", 3039 txg_list1_walk_init, txg_list_walk_step, NULL }, 3040 { "txg_list2", "given any txg_list_t *, walk all entries in txg 2", 3041 txg_list2_walk_init, txg_list_walk_step, NULL }, 3042 { "txg_list3", "given any txg_list_t *, walk all entries in txg 3", 3043 txg_list3_walk_init, txg_list_walk_step, NULL }, 3044 { "zio", "walk all zio structures, optionally for a particular spa_t", 3045 zio_walk_init, zio_walk_step, NULL }, 3046 { "zio_root", "walk all root zio_t structures, optionally for a " 3047 "particular spa_t", 3048 zio_walk_init, zio_walk_root_step, NULL }, 3049 { "spa", "walk all spa_t entries in the namespace", 3050 spa_walk_init, spa_walk_step, NULL }, 3051 { "metaslab", "given a spa_t *, walk all metaslab_t structures", 3052 metaslab_walk_init, metaslab_walk_step, NULL }, 3053 { "zfs_acl_node", "given a zfs_acl_t, walk all zfs_acl_nodes", 3054 zfs_acl_node_walk_init, zfs_acl_node_walk_step, NULL }, 3055 { "zfs_acl_node_aces", "given a zfs_acl_node_t, walk all ACEs", 3056 zfs_acl_node_aces_walk_init, zfs_aces_walk_step, NULL }, 3057 { "zfs_acl_node_aces0", 3058 "given a zfs_acl_node_t, walk all ACEs as ace_t", 3059 zfs_acl_node_aces0_walk_init, zfs_aces_walk_step, NULL }, 3060 { NULL } 3061 }; 3062 3063 static const mdb_modinfo_t modinfo = { 3064 MDB_API_VERSION, dcmds, walkers 3065 }; 3066 3067 const mdb_modinfo_t * 3068 _mdb_init(void) 3069 { 3070 return (&modinfo); 3071 } 3072