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