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