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