/* * CDDL HEADER START * * The contents of this file are subject to the terms of the * Common Development and Distribution License (the "License"). * You may not use this file except in compliance with the License. * * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE * or http://www.opensolaris.org/os/licensing. * See the License for the specific language governing permissions * and limitations under the License. * * When distributing Covered Code, include this CDDL HEADER in each * file and include the License file at usr/src/OPENSOLARIS.LICENSE. * If applicable, add the following below this CDDL HEADER, with the * fields enclosed by brackets "[]" replaced with your own identifying * information: Portions Copyright [yyyy] [name of copyright owner] * * CDDL HEADER END */ /* * Copyright 2006 Sun Microsystems, Inc. All rights reserved. * Use is subject to license terms. */ #pragma ident "%Z%%M% %I% %E% SMI" #include #include #include #include #include #include #include #include #include #include #include #include #include #ifndef _KERNEL #include "../genunix/list.h" #endif #ifdef _KERNEL #define ZFS_OBJ_NAME "zfs" #else #define ZFS_OBJ_NAME "libzpool.so.1" #endif static char * local_strdup(const char *s) { char *s1 = mdb_alloc(strlen(s) + 1, UM_SLEEP); (void) strcpy(s1, s); return (s1); } static int getmember(uintptr_t addr, const char *type, mdb_ctf_id_t *idp, const char *member, int len, void *buf) { mdb_ctf_id_t id; ulong_t off; char name[64]; if (idp == NULL) { if (mdb_ctf_lookup_by_name(type, &id) == -1) { mdb_warn("couldn't find type %s", type); return (DCMD_ERR); } idp = &id; } else { type = name; mdb_ctf_type_name(*idp, name, sizeof (name)); } if (mdb_ctf_offsetof(*idp, member, &off) == -1) { mdb_warn("couldn't find member %s of type %s\n", member, type); return (DCMD_ERR); } if (off % 8 != 0) { mdb_warn("member %s of type %s is unsupported bitfield", member, type); return (DCMD_ERR); } off /= 8; if (mdb_vread(buf, len, addr + off) == -1) { mdb_warn("failed to read %s from %s at %p", member, type, addr + off); return (DCMD_ERR); } /* mdb_warn("read %s from %s at %p+%llx\n", member, type, addr, off); */ return (0); } #define GETMEMB(addr, type, member, dest) \ getmember(addr, #type, NULL, #member, sizeof (dest), &(dest)) #define GETMEMBID(addr, ctfid, member, dest) \ getmember(addr, NULL, ctfid, #member, sizeof (dest), &(dest)) static int getrefcount(uintptr_t addr, mdb_ctf_id_t *id, const char *member, uint64_t *rc) { static int gotid; static mdb_ctf_id_t rc_id; ulong_t off; if (!gotid) { if (mdb_ctf_lookup_by_name("struct refcount", &rc_id) == -1) { mdb_warn("couldn't find struct refcount"); return (DCMD_ERR); } gotid = TRUE; } if (mdb_ctf_offsetof(*id, member, &off) == -1) { char name[64]; mdb_ctf_type_name(*id, name, sizeof (name)); mdb_warn("couldn't find member %s of type %s\n", member, name); return (DCMD_ERR); } off /= 8; return (GETMEMBID(addr + off, &rc_id, rc_count, *rc)); } static int read_symbol(char *sym_name, void **bufp) { GElf_Sym sym; if (mdb_lookup_by_obj(MDB_TGT_OBJ_EVERY, sym_name, &sym)) { mdb_warn("can't find symbol %s", sym_name); return (DCMD_ERR); } *bufp = mdb_alloc(sym.st_size, UM_SLEEP); if (mdb_vread(*bufp, sym.st_size, sym.st_value) == -1) { mdb_warn("can't read data for symbol %s", sym_name); mdb_free(*bufp, sym.st_size); return (DCMD_ERR); } return (DCMD_OK); } static int verbose; static int freelist_walk_init(mdb_walk_state_t *wsp) { if (wsp->walk_addr == NULL) { mdb_warn("must supply starting address\n"); return (WALK_ERR); } wsp->walk_data = 0; /* Index into the freelist */ return (WALK_NEXT); } static int freelist_walk_step(mdb_walk_state_t *wsp) { uint64_t entry; uintptr_t number = (uintptr_t)wsp->walk_data; char *ddata[] = { "ALLOC", "FREE", "CONDENSE", "INVALID" }; int mapshift = SPA_MINBLOCKSHIFT; if (mdb_vread(&entry, sizeof (entry), wsp->walk_addr) == -1) { mdb_warn("failed to read freelist entry %p", wsp->walk_addr); return (WALK_DONE); } wsp->walk_addr += sizeof (entry); wsp->walk_data = (void *)(number + 1); if (SM_DEBUG_DECODE(entry)) { mdb_printf("DEBUG: %3u %10s: txg=%llu pass=%llu\n", number, ddata[SM_DEBUG_ACTION_DECODE(entry)], SM_DEBUG_TXG_DECODE(entry), SM_DEBUG_SYNCPASS_DECODE(entry)); } else { mdb_printf("Entry: %3u offsets=%08llx-%08llx type=%c " "size=%06llx", number, SM_OFFSET_DECODE(entry) << mapshift, (SM_OFFSET_DECODE(entry) + SM_RUN_DECODE(entry)) << mapshift, SM_TYPE_DECODE(entry) == SM_ALLOC ? 'A' : 'F', SM_RUN_DECODE(entry) << mapshift); if (verbose) mdb_printf(" (raw=%012llx)\n", entry); mdb_printf("\n"); } return (WALK_NEXT); } /* ARGSUSED */ static void freelist_walk_fini(mdb_walk_state_t *wsp) { } typedef struct dbuf_walk_data { dbuf_hash_table_t ht; int64_t bucket; uintptr_t dbp; dmu_buf_impl_t db; } dbuf_walk_data_t; static int dbuf_walk_init(mdb_walk_state_t *wsp) { dbuf_walk_data_t *dwd; if (wsp->walk_addr != NULL) { mdb_warn("must supply starting address\n"); return (WALK_ERR); } dwd = mdb_alloc(sizeof (dbuf_walk_data_t), UM_SLEEP); if (mdb_readvar(&dwd->ht, "dbuf_hash_table") == -1) { mdb_warn("failed to read 'dbuf_hash_table'"); mdb_free(dwd, sizeof (dbuf_walk_data_t)); return (WALK_ERR); } dwd->bucket = -1; dwd->dbp = 0; wsp->walk_data = dwd; return (WALK_NEXT); } static int dbuf_walk_step(mdb_walk_state_t *wsp) { int status; dbuf_walk_data_t *dwd = wsp->walk_data; while (dwd->dbp == 0) { dwd->bucket++; if (dwd->bucket == dwd->ht.hash_table_mask+1) return (WALK_DONE); if (mdb_vread(&dwd->dbp, sizeof (void *), (uintptr_t)(dwd->ht.hash_table+dwd->bucket)) == -1) { mdb_warn("failed to read hash bucket %u at %p", dwd->bucket, dwd->ht.hash_table+dwd->bucket); return (WALK_DONE); } } wsp->walk_addr = dwd->dbp; if (mdb_vread(&dwd->db, sizeof (dmu_buf_impl_t), wsp->walk_addr) == -1) { mdb_warn("failed to read dbuf at %p", wsp->walk_addr); return (WALK_DONE); } status = wsp->walk_callback(wsp->walk_addr, &dwd->db, wsp->walk_cbdata); dwd->dbp = (uintptr_t)dwd->db.db_hash_next; return (status); } static void dbuf_walk_fini(mdb_walk_state_t *wsp) { dbuf_walk_data_t *dwd = wsp->walk_data; mdb_free(dwd, sizeof (dbuf_walk_data_t)); } static int dataset_name(uintptr_t addr, char *buf) { static int gotid; static mdb_ctf_id_t dd_id; uintptr_t dd_parent; char dd_myname[MAXNAMELEN]; if (!gotid) { if (mdb_ctf_lookup_by_name("struct dsl_dir", &dd_id) == -1) { mdb_warn("couldn't find struct dsl_dir"); return (DCMD_ERR); } gotid = TRUE; } if (GETMEMBID(addr, &dd_id, dd_parent, dd_parent) || GETMEMBID(addr, &dd_id, dd_myname, dd_myname)) { return (DCMD_ERR); } if (dd_parent) { if (dataset_name(dd_parent, buf)) return (DCMD_ERR); strcat(buf, "/"); } if (dd_myname[0]) strcat(buf, dd_myname); else strcat(buf, "???"); return (0); } static int objset_name(uintptr_t addr, char *buf) { static int gotid; static mdb_ctf_id_t osi_id, ds_id; uintptr_t os_dsl_dataset; char ds_snapname[MAXNAMELEN]; uintptr_t ds_dir; buf[0] = '\0'; if (!gotid) { if (mdb_ctf_lookup_by_name("struct objset_impl", &osi_id) == -1) { mdb_warn("couldn't find struct objset_impl"); return (DCMD_ERR); } if (mdb_ctf_lookup_by_name("struct dsl_dataset", &ds_id) == -1) { mdb_warn("couldn't find struct dsl_dataset"); return (DCMD_ERR); } gotid = TRUE; } if (GETMEMBID(addr, &osi_id, os_dsl_dataset, os_dsl_dataset)) return (DCMD_ERR); if (os_dsl_dataset == 0) { strcat(buf, "mos"); return (0); } if (GETMEMBID(os_dsl_dataset, &ds_id, ds_snapname, ds_snapname) || GETMEMBID(os_dsl_dataset, &ds_id, ds_dir, ds_dir)) { return (DCMD_ERR); } if (ds_dir && dataset_name(ds_dir, buf)) return (DCMD_ERR); if (ds_snapname[0]) { strcat(buf, "@"); strcat(buf, ds_snapname); } return (0); } static void enum_lookup(char *out, size_t size, mdb_ctf_id_t id, int val, const char *prefix) { const char *cp; size_t len = strlen(prefix); if ((cp = mdb_ctf_enum_name(id, val)) != NULL) { if (strncmp(cp, prefix, len) == 0) cp += len; (void) strncpy(out, cp, size); } else { mdb_snprintf(out, size, "? (%d)", val); } } /* ARGSUSED */ static int zio_pipeline(uintptr_t addr, uint_t flags, int argc, const mdb_arg_t *argv) { mdb_ctf_id_t pipe_enum; int i; char stage[1024]; if (mdb_ctf_lookup_by_name("enum zio_stage", &pipe_enum) == -1) { mdb_warn("Could not find enum zio_stage"); return (DCMD_ERR); } for (i = 0; i < 32; i++) { if (addr & (1U << i)) { enum_lookup(stage, sizeof (stage), pipe_enum, i, "ZIO_STAGE_"); mdb_printf(" %s\n", stage); } } return (DCMD_OK); } /* ARGSUSED */ static int blkptr(uintptr_t addr, uint_t flags, int argc, const mdb_arg_t *argv) { blkptr_t bp; dmu_object_type_info_t *doti; zio_compress_info_t *zct; zio_checksum_info_t *zci; int i; char buf[MAXPATHLEN]; if (mdb_vread(&bp, sizeof (blkptr_t), addr) == -1) { mdb_warn("failed to read blkptr_t"); return (DCMD_ERR); } if (read_symbol("dmu_ot", (void **)&doti) != DCMD_OK) return (DCMD_ERR); for (i = 0; i < DMU_OT_NUMTYPES; i++) { mdb_readstr(buf, sizeof (buf), (uintptr_t)doti[i].ot_name); doti[i].ot_name = local_strdup(buf); } if (read_symbol("zio_checksum_table", (void **)&zci) != DCMD_OK) return (DCMD_ERR); for (i = 0; i < ZIO_CHECKSUM_FUNCTIONS; i++) { mdb_readstr(buf, sizeof (buf), (uintptr_t)zci[i].ci_name); zci[i].ci_name = local_strdup(buf); } if (read_symbol("zio_compress_table", (void **)&zct) != DCMD_OK) return (DCMD_ERR); for (i = 0; i < ZIO_COMPRESS_FUNCTIONS; i++) { mdb_readstr(buf, sizeof (buf), (uintptr_t)zct[i].ci_name); zct[i].ci_name = local_strdup(buf); } for (i = 0; i < SPA_DVAS_PER_BP; i++) { dva_t *dva = &bp.blk_dva[i]; mdb_printf("DVA[%d]: GANG: %-5s GRID: %2x ASIZE: %5x " "vdev %llu offset %llx\n", i, DVA_GET_GANG(dva) ? "TRUE" : "FALSE", DVA_GET_GRID(dva), DVA_GET_ASIZE(dva), DVA_GET_VDEV(dva), DVA_GET_OFFSET(dva)); } mdb_printf("LSIZE: %-16llx\t\tPSIZE: %llx\n", BP_GET_LSIZE(&bp), BP_GET_PSIZE(&bp)); mdb_printf("ENDIAN: %-6s TYPE: %s\n", BP_GET_BYTEORDER(&bp) ? "LITTLE" : "BIG", doti[BP_GET_TYPE(&bp)].ot_name); mdb_printf("BIRTH: %-16llx LEVEL: %-2d\tFILL: %llx\n", bp.blk_birth, BP_GET_LEVEL(&bp), bp.blk_fill); mdb_printf("CKFUNC: %-16s\t\tCOMP: %s\n", zci[BP_GET_CHECKSUM(&bp)].ci_name, zct[BP_GET_COMPRESS(&bp)].ci_name); mdb_printf("CKSUM: %llx:%llx:%llx:%llx\n", bp.blk_cksum.zc_word[0], bp.blk_cksum.zc_word[1], bp.blk_cksum.zc_word[2], bp.blk_cksum.zc_word[3]); return (DCMD_OK); } /* ARGSUSED */ static int dbuf(uintptr_t addr, uint_t flags, int argc, const mdb_arg_t *argv) { mdb_ctf_id_t id; dmu_buf_t db; uintptr_t objset; uint8_t level; uint64_t blkid; uint64_t holds; char objectname[32]; char blkidname[32]; char path[MAXNAMELEN]; if (DCMD_HDRSPEC(flags)) { mdb_printf(" addr object lvl blkid holds os\n"); } if (mdb_ctf_lookup_by_name("struct dmu_buf_impl", &id) == -1) { mdb_warn("couldn't find struct dmu_buf_impl_t"); return (DCMD_ERR); } if (GETMEMBID(addr, &id, db_objset, objset) || GETMEMBID(addr, &id, db, db) || GETMEMBID(addr, &id, db_level, level) || GETMEMBID(addr, &id, db_blkid, blkid)) { return (WALK_ERR); } if (getrefcount(addr, &id, "db_holds", &holds)) { return (WALK_ERR); } if (db.db_object == DMU_META_DNODE_OBJECT) (void) strcpy(objectname, "mdn"); else (void) mdb_snprintf(objectname, sizeof (objectname), "%llx", (u_longlong_t)db.db_object); if (blkid == DB_BONUS_BLKID) (void) strcpy(blkidname, "bonus"); else (void) mdb_snprintf(blkidname, sizeof (blkidname), "%llx", (u_longlong_t)blkid); if (objset_name(objset, path)) { return (WALK_ERR); } mdb_printf("%p %8s %1u %9s %2llu %s\n", addr, objectname, level, blkidname, holds, path); return (DCMD_OK); } /* ARGSUSED */ static int dbuf_stats(uintptr_t addr, uint_t flags, int argc, const mdb_arg_t *argv) { #define HISTOSZ 32 uintptr_t dbp; dmu_buf_impl_t db; dbuf_hash_table_t ht; uint64_t bucket, ndbufs; uint64_t histo[HISTOSZ]; uint64_t histo2[HISTOSZ]; int i, maxidx; if (mdb_readvar(&ht, "dbuf_hash_table") == -1) { mdb_warn("failed to read 'dbuf_hash_table'"); return (DCMD_ERR); } for (i = 0; i < HISTOSZ; i++) { histo[i] = 0; histo2[i] = 0; } ndbufs = 0; for (bucket = 0; bucket < ht.hash_table_mask+1; bucket++) { int len; if (mdb_vread(&dbp, sizeof (void *), (uintptr_t)(ht.hash_table+bucket)) == -1) { mdb_warn("failed to read hash bucket %u at %p", bucket, ht.hash_table+bucket); return (DCMD_ERR); } len = 0; while (dbp != 0) { if (mdb_vread(&db, sizeof (dmu_buf_impl_t), dbp) == -1) { mdb_warn("failed to read dbuf at %p", dbp); return (DCMD_ERR); } dbp = (uintptr_t)db.db_hash_next; for (i = MIN(len, HISTOSZ - 1); i >= 0; i--) histo2[i]++; len++; ndbufs++; } if (len >= HISTOSZ) len = HISTOSZ-1; histo[len]++; } mdb_printf("hash table has %llu buckets, %llu dbufs " "(avg %llu buckets/dbuf)\n", ht.hash_table_mask+1, ndbufs, (ht.hash_table_mask+1)/ndbufs); mdb_printf("\n"); maxidx = 0; for (i = 0; i < HISTOSZ; i++) if (histo[i] > 0) maxidx = i; mdb_printf("hash chain length number of buckets\n"); for (i = 0; i <= maxidx; i++) mdb_printf("%u %llu\n", i, histo[i]); mdb_printf("\n"); maxidx = 0; for (i = 0; i < HISTOSZ; i++) if (histo2[i] > 0) maxidx = i; mdb_printf("hash chain depth number of dbufs\n"); for (i = 0; i <= maxidx; i++) mdb_printf("%u or more %llu %llu%%\n", i, histo2[i], histo2[i]*100/ndbufs); return (DCMD_OK); } typedef struct dbufs_data { mdb_ctf_id_t id; uint64_t objset; uint64_t object; uint64_t level; uint64_t blkid; char *osname; } dbufs_data_t; #define DBUFS_UNSET (0xbaddcafedeadbeefULL) /* ARGSUSED */ static int dbufs_cb(uintptr_t addr, const void *unknown, void *arg) { dbufs_data_t *data = arg; uintptr_t objset; dmu_buf_t db; uint8_t level; uint64_t blkid; char osname[MAXNAMELEN]; if (GETMEMBID(addr, &data->id, db_objset, objset) || GETMEMBID(addr, &data->id, db, db) || GETMEMBID(addr, &data->id, db_level, level) || GETMEMBID(addr, &data->id, db_blkid, blkid)) { return (WALK_ERR); } if ((data->objset == DBUFS_UNSET || data->objset == objset) && (data->osname == NULL || (objset_name(objset, osname) == 0 && strcmp(data->osname, osname) == 0)) && (data->object == DBUFS_UNSET || data->object == db.db_object) && (data->level == DBUFS_UNSET || data->level == level) && (data->blkid == DBUFS_UNSET || data->blkid == blkid)) { mdb_printf("%#lr\n", addr); } return (WALK_NEXT); } /* ARGSUSED */ static int dbufs(uintptr_t addr, uint_t flags, int argc, const mdb_arg_t *argv) { dbufs_data_t data; char *object = NULL; char *blkid = NULL; data.objset = data.object = data.level = data.blkid = DBUFS_UNSET; data.osname = NULL; if (mdb_getopts(argc, argv, 'O', MDB_OPT_UINT64, &data.objset, 'n', MDB_OPT_STR, &data.osname, 'o', MDB_OPT_STR, &object, 'l', MDB_OPT_UINT64, &data.level, 'b', MDB_OPT_STR, &blkid) != argc) { return (DCMD_USAGE); } if (object) { if (strcmp(object, "mdn") == 0) { data.object = DMU_META_DNODE_OBJECT; } else { data.object = mdb_strtoull(object); } } if (blkid) { if (strcmp(blkid, "bonus") == 0) { data.blkid = DB_BONUS_BLKID; } else { data.blkid = mdb_strtoull(blkid); } } if (mdb_ctf_lookup_by_name("struct dmu_buf_impl", &data.id) == -1) { mdb_warn("couldn't find struct dmu_buf_impl_t"); return (DCMD_ERR); } if (mdb_pwalk("dbufs", dbufs_cb, &data, 0) != 0) { mdb_warn("can't walk dbufs"); return (DCMD_ERR); } return (DCMD_OK); } typedef struct abuf_find_data { dva_t dva; mdb_ctf_id_t id; } abuf_find_data_t; /* ARGSUSED */ static int abuf_find_cb(uintptr_t addr, const void *unknown, void *arg) { abuf_find_data_t *data = arg; dva_t dva; if (GETMEMBID(addr, &data->id, b_dva, dva)) { return (WALK_ERR); } if (dva.dva_word[0] == data->dva.dva_word[0] && dva.dva_word[1] == data->dva.dva_word[1]) { mdb_printf("%#lr\n", addr); } return (WALK_NEXT); } /* ARGSUSED */ static int abuf_find(uintptr_t addr, uint_t flags, int argc, const mdb_arg_t *argv) { abuf_find_data_t data; GElf_Sym sym; int i; const char *syms[] = { "ARC_mru_top", "ARC_mru_bot", "ARC_mfu_top", "ARC_mfu_bot", }; if (argc != 2) return (DCMD_USAGE); for (i = 0; i < 2; i ++) { switch (argv[i].a_type) { case MDB_TYPE_STRING: data.dva.dva_word[i] = mdb_strtoull(argv[i].a_un.a_str); break; case MDB_TYPE_IMMEDIATE: data.dva.dva_word[i] = argv[i].a_un.a_val; break; default: return (DCMD_USAGE); } } if (mdb_ctf_lookup_by_name("struct arc_buf_hdr", &data.id) == -1) { mdb_warn("couldn't find struct arc_buf_hdr"); return (DCMD_ERR); } for (i = 0; i < sizeof (syms) / sizeof (syms[0]); i++) { if (mdb_lookup_by_name(syms[i], &sym)) { mdb_warn("can't find symbol %s", syms[i]); return (DCMD_ERR); } if (mdb_pwalk("list", abuf_find_cb, &data, sym.st_value) != 0) { mdb_warn("can't walk %s", syms[i]); return (DCMD_ERR); } } return (DCMD_OK); } void abuf_help(void) { mdb_printf("::abuf_find dva_word[0] dva_word[1]\n"); } /* * ::spa * * -c Print configuration information as well * -v Print vdev state * -e Print vdev error stats * * Print a summarized spa_t. When given no arguments, prints out a table of all * active pools on the system. */ /* ARGSUSED */ static int spa_print(uintptr_t addr, uint_t flags, int argc, const mdb_arg_t *argv) { spa_t spa; char poolname[MAXNAMELEN]; const char *statetab[] = { "ACTIVE", "EXPORTED", "DESTROYED", "UNINIT", "UNAVAIL" }; const char *state; int config = FALSE; int vdevs = FALSE; int errors = FALSE; if (mdb_getopts(argc, argv, 'c', MDB_OPT_SETBITS, TRUE, &config, 'v', MDB_OPT_SETBITS, TRUE, &vdevs, 'e', MDB_OPT_SETBITS, TRUE, &errors, NULL) != argc) return (DCMD_USAGE); if (!(flags & DCMD_ADDRSPEC)) { if (mdb_walk_dcmd("spa", "spa", argc, argv) == -1) { mdb_warn("can't walk spa"); return (DCMD_ERR); } return (DCMD_OK); } if (flags & DCMD_PIPE_OUT) { mdb_printf("%#lr\n", addr); return (DCMD_OK); } if (DCMD_HDRSPEC(flags)) mdb_printf("%%-?s %9s %-*s%\n", "ADDR", "STATE", sizeof (uintptr_t) == 4 ? 60 : 52, "NAME"); if (mdb_vread(&spa, sizeof (spa), addr) == -1) { mdb_warn("failed to read spa_t at %p", addr); return (DCMD_ERR); } if (mdb_readstr(poolname, sizeof (poolname), (uintptr_t)spa.spa_name) == -1) { mdb_warn("failed to read pool name at %p", spa.spa_name); return (DCMD_ERR); } if (spa.spa_state < 0 || spa.spa_state > POOL_STATE_UNAVAIL) state = "UNKNOWN"; else state = statetab[spa.spa_state]; mdb_printf("%0?p %9s %s\n", addr, state, poolname); if (config) { mdb_printf("\n"); mdb_inc_indent(4); if (mdb_call_dcmd("spa_config", addr, flags, 0, NULL) != DCMD_OK) return (DCMD_ERR); mdb_dec_indent(4); } if (vdevs || errors) { mdb_arg_t v; v.a_type = MDB_TYPE_STRING; v.a_un.a_str = "-e"; mdb_printf("\n"); mdb_inc_indent(4); if (mdb_call_dcmd("spa_vdevs", addr, flags, errors ? 1 : 0, &v) != DCMD_OK) return (DCMD_ERR); mdb_dec_indent(4); } return (DCMD_OK); } /* * ::spa_config * * Given a spa_t, print the configuration information stored in spa_config. * Since it's just an nvlist, format it as an indented list of name=value pairs. * We simply read the value of spa_config and pass off to ::nvlist. */ /* ARGSUSED */ static int spa_print_config(uintptr_t addr, uint_t flags, int argc, const mdb_arg_t *argv) { spa_t spa; if (argc != 0 || !(flags & DCMD_ADDRSPEC)) return (DCMD_USAGE); if (mdb_vread(&spa, sizeof (spa), addr) == -1) { mdb_warn("failed to read spa_t at %p", addr); return (DCMD_ERR); } if (spa.spa_config == NULL) { mdb_printf("(none)\n"); return (DCMD_OK); } return (mdb_call_dcmd("nvlist", (uintptr_t)spa.spa_config, flags, 0, NULL)); } void vdev_help(void) { mdb_printf("[vdev_t*]::vdev [-qr]\n" "\t-> -q display vdev_queue parameters\n" "\t-> -r recursive (visit all children)\n"); } /* * ::vdev * * Print out a summarized vdev_t, in the following form: * * ADDR STATE AUX DESC * fffffffbcde23df0 HEALTHY - /dev/dsk/c0t0d0 * * or with "-q" to print out a vdev_t's vdev_queue parameters: * * vdev_t: c26ae4c0 * c26ae73c min pending 0x2 * c26ae744 max pending 0x23 * c26ae74c agg limit 0x20000 * c26ae754 time shift 0x4 * c26ae75c ramp rate 0x2 * * If '-r' is specified, recursively visit all children. * * With '-e', the statistics associated with the vdev are printed as well. */ static int do_print_vdev(uintptr_t addr, int flags, int depth, int queue, int stats, int recursive) { vdev_t vdev; char desc[MAXNAMELEN]; int c, children; uintptr_t *child; const char *state, *aux; if (mdb_vread(&vdev, sizeof (vdev), (uintptr_t)addr) == -1) { mdb_warn("failed to read vdev_t at %p\n", (uintptr_t)addr); return (DCMD_ERR); } if (flags & DCMD_PIPE_OUT) { mdb_printf("%#lr", addr); } else { if (vdev.vdev_path != NULL) { if (mdb_readstr(desc, sizeof (desc), (uintptr_t)vdev.vdev_path) == -1) { mdb_warn("failed to read vdev_path at %p\n", vdev.vdev_path); return (DCMD_ERR); } } else if (vdev.vdev_ops != NULL) { vdev_ops_t ops; if (mdb_vread(&ops, sizeof (ops), (uintptr_t)vdev.vdev_ops) == -1) { mdb_warn("failed to read vdev_ops at %p\n", vdev.vdev_ops); return (DCMD_ERR); } (void) strcpy(desc, ops.vdev_op_type); } else { (void) strcpy(desc, ""); } if (depth == 0 && DCMD_HDRSPEC(flags)) mdb_printf("%%-?s %-9s %-12s %-*s%\n", "ADDR", "STATE", "AUX", sizeof (uintptr_t) == 4 ? 43 : 35, "DESCRIPTION"); mdb_printf("%0?p ", addr); switch (vdev.vdev_state) { case VDEV_STATE_CLOSED: state = "CLOSED"; break; case VDEV_STATE_OFFLINE: state = "OFFLINE"; break; case VDEV_STATE_CANT_OPEN: state = "CANT_OPEN"; break; case VDEV_STATE_DEGRADED: state = "DEGRADED"; break; case VDEV_STATE_HEALTHY: state = "HEALTHY"; break; default: state = "UNKNOWN"; break; } switch (vdev.vdev_stat.vs_aux) { case VDEV_AUX_NONE: aux = "-"; break; case VDEV_AUX_OPEN_FAILED: aux = "OPEN_FAILED"; break; case VDEV_AUX_CORRUPT_DATA: aux = "CORRUPT_DATA"; break; case VDEV_AUX_NO_REPLICAS: aux = "NO_REPLICAS"; break; case VDEV_AUX_BAD_GUID_SUM: aux = "BAD_GUID_SUM"; break; case VDEV_AUX_TOO_SMALL: aux = "TOO_SMALL"; break; case VDEV_AUX_BAD_LABEL: aux = "BAD_LABEL"; break; default: aux = "UNKNOWN"; break; } mdb_printf("%-9s %-12s %*s%s\n", state, aux, depth, "", desc); if (queue) { mdb_inc_indent(4); mdb_printf("\n"); mdb_printf("%p min pending 0x%llx\n", (uintptr_t)(addr + offsetof(vdev_t, vdev_queue.vq_min_pending)), vdev.vdev_queue.vq_min_pending); mdb_printf("%p max pending 0x%llx\n", (uintptr_t)(addr + offsetof(vdev_t, vdev_queue.vq_max_pending)), vdev.vdev_queue.vq_max_pending); mdb_printf("%p agg limit 0x%llx\n", (uintptr_t)(addr + offsetof(vdev_t, vdev_queue.vq_agg_limit)), vdev.vdev_queue.vq_agg_limit); mdb_printf("%p time shift 0x%llx\n", (uintptr_t)(addr + offsetof(vdev_t, vdev_queue.vq_time_shift)), vdev.vdev_queue.vq_time_shift); mdb_printf("%p ramp rate 0x%llx\n", (uintptr_t)(addr + offsetof(vdev_t, vdev_queue.vq_ramp_rate)), vdev.vdev_queue.vq_ramp_rate); mdb_dec_indent(4); } if (stats) { vdev_stat_t *vs = &vdev.vdev_stat; int i; mdb_inc_indent(4); mdb_printf("\n"); mdb_printf("% %12s %12s %12s %12s " "%12s%\n", "READ", "WRITE", "FREE", "CLAIM", "IOCTL"); mdb_printf("OPS "); for (i = 1; i < ZIO_TYPES; i++) mdb_printf("%11#llx%s", vs->vs_ops[i], i == ZIO_TYPES - 1 ? "" : " "); mdb_printf("\n"); mdb_printf("BYTES "); for (i = 1; i < ZIO_TYPES; i++) mdb_printf("%11#llx%s", vs->vs_bytes[i], i == ZIO_TYPES - 1 ? "" : " "); mdb_printf("\n"); mdb_printf("EREAD %10#llx\n", vs->vs_read_errors); mdb_printf("EWRITE %10#llx\n", vs->vs_write_errors); mdb_printf("ECKSUM %10#llx\n", vs->vs_checksum_errors); mdb_dec_indent(4); } if (queue || stats) mdb_printf("\n"); } children = vdev.vdev_children; if (children == 0 || !recursive) return (DCMD_OK); child = mdb_alloc(children * sizeof (void *), UM_SLEEP | UM_GC); if (mdb_vread(child, children * sizeof (void *), (uintptr_t)vdev.vdev_child) == -1) { mdb_warn("failed to read vdev children at %p", vdev.vdev_child); return (DCMD_ERR); } for (c = 0; c < children; c++) { if (do_print_vdev(child[c], flags, depth + 2, queue, stats, recursive)) return (DCMD_ERR); } return (DCMD_OK); } static int vdev_print(uintptr_t addr, uint_t flags, int argc, const mdb_arg_t *argv) { int print_queue = FALSE; int recursive = FALSE; int stats = FALSE; if (mdb_getopts(argc, argv, 'q', MDB_OPT_SETBITS, TRUE, &print_queue, 'r', MDB_OPT_SETBITS, TRUE, &recursive, 'e', MDB_OPT_SETBITS, TRUE, &stats, NULL) != argc) return (DCMD_USAGE); if (!(flags & DCMD_ADDRSPEC)) { mdb_warn("no vdev_t address given\n"); return (DCMD_ERR); } return (do_print_vdev(addr, flags, 0, print_queue, stats, recursive)); } typedef struct mdb_spa { uintptr_t spa_dsl_pool; uintptr_t spa_root_vdev; } mdb_spa_t; typedef struct mdb_dsl_dir { uintptr_t dd_phys; uint64_t dd_used_bytes; int64_t dd_space_towrite[TXG_SIZE]; } mdb_dsl_dir_t; typedef struct mdb_dsl_dir_phys { uint64_t dd_used_bytes; uint64_t dd_compressed_bytes; uint64_t dd_uncompressed_bytes; } mdb_dsl_dir_phys_t; typedef struct mdb_vdev { uintptr_t vdev_parent; uintptr_t vdev_ms; uint64_t vdev_ms_count; vdev_stat_t vdev_stat; } mdb_vdev_t; typedef struct mdb_metaslab { space_map_t ms_allocmap[TXG_SIZE]; space_map_t ms_freemap[TXG_SIZE]; space_map_t ms_map; space_map_obj_t ms_smo; } mdb_metaslab_t; /* * ::spa_space [-b] * * Given a spa_t, print out it's on-disk space usage and in-core * estimates of future usage. If -b is given, print space in bytes. * Otherwise print in megabytes. */ /* ARGSUSED */ static int spa_space(uintptr_t addr, uint_t flags, int argc, const mdb_arg_t *argv) { mdb_spa_t spa; uintptr_t dp_root_dir; mdb_dsl_dir_t dd; mdb_dsl_dir_phys_t dsp; uint64_t children; uintptr_t childaddr; uintptr_t *child; uint64_t ms_allocmap[TXG_SIZE] = {0, 0, 0, 0}; uint64_t ms_freemap[TXG_SIZE] = {0, 0, 0, 0}; uint64_t ms_map = 0; uint64_t avail = 0; int i, j; int havecompressed = TRUE; int shift = 20; char *suffix = "M"; int bits = FALSE; if (mdb_getopts(argc, argv, 'b', MDB_OPT_SETBITS, TRUE, &bits, NULL) != argc) return (DCMD_USAGE); if (!(flags & DCMD_ADDRSPEC)) return (DCMD_USAGE); if (bits) { shift = 0; suffix = ""; } if (GETMEMB(addr, struct spa, spa_dsl_pool, spa.spa_dsl_pool) || GETMEMB(addr, struct spa, spa_root_vdev, spa.spa_root_vdev) || GETMEMB(spa.spa_root_vdev, struct vdev, vdev_children, children) || GETMEMB(spa.spa_root_vdev, struct vdev, vdev_child, childaddr) || GETMEMB(spa.spa_dsl_pool, struct dsl_pool, dp_root_dir, dp_root_dir) || GETMEMB(dp_root_dir, struct dsl_dir, dd_phys, dd.dd_phys) || GETMEMB(dp_root_dir, struct dsl_dir, dd_used_bytes, dd.dd_used_bytes) || GETMEMB(dp_root_dir, struct dsl_dir, dd_space_towrite, dd.dd_space_towrite) || GETMEMB(dd.dd_phys, struct dsl_dir_phys, dd_used_bytes, dsp.dd_used_bytes)) { return (DCMD_ERR); } if (GETMEMB(dd.dd_phys, struct dsl_dir_phys, dd_compressed_bytes, dsp.dd_compressed_bytes) || GETMEMB(dd.dd_phys, struct dsl_dir_phys, dd_uncompressed_bytes, dsp.dd_uncompressed_bytes)) { havecompressed = FALSE; } child = mdb_alloc(children * sizeof (void *), UM_SLEEP | UM_GC); if (mdb_vread(child, children * sizeof (void *), childaddr) == -1) { mdb_warn("failed to read root vdev children at %p", childaddr); return (DCMD_ERR); } mdb_printf("dd_space_towrite = %llu%s %llu%s %llu%s %llu%s\n", dd.dd_space_towrite[0] >> shift, suffix, dd.dd_space_towrite[1] >> shift, suffix, dd.dd_space_towrite[2] >> shift, suffix, dd.dd_space_towrite[3] >> shift, suffix); mdb_printf("dd_used_bytes = %llu%s\n", dd.dd_used_bytes >> shift, suffix); mdb_printf("dd_phys.dd_used_bytes = %llu%s\n", dsp.dd_used_bytes >> shift, suffix); if (havecompressed) { mdb_printf("dd_phys.dd_compressed_bytes = %llu%s\n", dsp.dd_compressed_bytes >> shift, suffix); mdb_printf("dd_phys.dd_uncompressed_bytes = %llu%s\n", dsp.dd_uncompressed_bytes >> shift, suffix); } for (i = 0; i < children; i++) { mdb_vdev_t vd; uintptr_t *vdev_ms; if (GETMEMB(child[i], struct vdev, vdev_parent, vd.vdev_parent) || GETMEMB(child[i], struct vdev, vdev_stat, vd.vdev_stat) || GETMEMB(child[i], struct vdev, vdev_ms, vd.vdev_ms) || GETMEMB(child[i], struct vdev, vdev_ms_count, vd.vdev_ms_count)) { return (DCMD_ERR); } /* * If this is the root vdev, its stats are the pool-wide stats. */ if (vd.vdev_parent == NULL) { mdb_printf("pool_alloc = %llu%s\n", vd.vdev_stat.vs_alloc >> shift, suffix); mdb_printf("pool_space = %llu%s\n", vd.vdev_stat.vs_space >> shift, suffix); } /* * If this is not a top-level vdev, it doesn't have space. */ if (vd.vdev_parent != spa.spa_root_vdev) continue; vdev_ms = mdb_alloc(vd.vdev_ms_count * sizeof (void*), UM_SLEEP | UM_GC); if (mdb_vread(vdev_ms, vd.vdev_ms_count * sizeof (void*), (uintptr_t)vd.vdev_ms) == -1) { mdb_warn("failed to read vdev_ms at %p", vd.vdev_ms); return (DCMD_ERR); } for (j = 0; j < vd.vdev_ms_count; j++) { mdb_metaslab_t ms; if (GETMEMB(vdev_ms[j], struct metaslab, ms_allocmap, ms.ms_allocmap) || GETMEMB(vdev_ms[j], struct metaslab, ms_freemap, ms.ms_freemap) || GETMEMB(vdev_ms[j], struct metaslab, ms_map, ms.ms_map) || GETMEMB(vdev_ms[j], struct metaslab, ms_smo, ms.ms_smo)) { return (DCMD_ERR); } ms_allocmap[0] += ms.ms_allocmap[0].sm_space; ms_allocmap[1] += ms.ms_allocmap[1].sm_space; ms_allocmap[2] += ms.ms_allocmap[2].sm_space; ms_allocmap[3] += ms.ms_allocmap[3].sm_space; ms_freemap[0] += ms.ms_freemap[0].sm_space; ms_freemap[1] += ms.ms_freemap[1].sm_space; ms_freemap[2] += ms.ms_freemap[2].sm_space; ms_freemap[3] += ms.ms_freemap[3].sm_space; ms_map += ms.ms_map.sm_space; avail += ms.ms_map.sm_size - ms.ms_smo.smo_alloc; } } mdb_printf("ms_allocmap = %llu%s %llu%s %llu%s %llu%s\n", ms_allocmap[0] >> shift, suffix, ms_allocmap[1] >> shift, suffix, ms_allocmap[2] >> shift, suffix, ms_allocmap[3] >> shift, suffix); mdb_printf("ms_freemap = %llu%s %llu%s %llu%s %llu%s\n", ms_freemap[0] >> shift, suffix, ms_freemap[1] >> shift, suffix, ms_freemap[2] >> shift, suffix, ms_freemap[3] >> shift, suffix); mdb_printf("ms_map = %llu%s\n", ms_map >> shift, suffix); mdb_printf("avail = %llu%s\n", avail >> shift, suffix); return (DCMD_OK); } /* * ::spa_verify * * Given a spa_t, verify that that the pool is self-consistent. * Currently, it only checks to make sure that the vdev tree exists. */ /* ARGSUSED */ static int spa_verify(uintptr_t addr, uint_t flags, int argc, const mdb_arg_t *argv) { spa_t spa; if (argc != 0 || !(flags & DCMD_ADDRSPEC)) return (DCMD_USAGE); if (mdb_vread(&spa, sizeof (spa), addr) == -1) { mdb_warn("failed to read spa_t at %p", addr); return (DCMD_ERR); } if (spa.spa_root_vdev == NULL) { mdb_printf("no vdev tree present\n"); return (DCMD_OK); } return (DCMD_OK); } /* * ::spa_vdevs * * -e Include error stats * * Print out a summarized list of vdevs for the given spa_t. * This is accomplished by invoking "::vdev -re" on the root vdev. */ /* ARGSUSED */ static int spa_vdevs(uintptr_t addr, uint_t flags, int argc, const mdb_arg_t *argv) { spa_t spa; mdb_arg_t v; int errors = FALSE; if (mdb_getopts(argc, argv, 'e', MDB_OPT_SETBITS, TRUE, &errors, NULL) != argc) return (DCMD_USAGE); if (!(flags & DCMD_ADDRSPEC)) return (DCMD_USAGE); if (mdb_vread(&spa, sizeof (spa), addr) == -1) { mdb_warn("failed to read spa_t at %p", addr); return (DCMD_ERR); } /* * Unitialized spa_t structures can have a NULL root vdev. */ if (spa.spa_root_vdev == NULL) { mdb_printf("no associated vdevs\n"); return (DCMD_OK); } v.a_type = MDB_TYPE_STRING; v.a_un.a_str = errors ? "-re" : "-r"; return (mdb_call_dcmd("vdev", (uintptr_t)spa.spa_root_vdev, flags, 1, &v)); } typedef struct txg_list_walk_data { uintptr_t lw_head[TXG_SIZE]; int lw_txgoff; int lw_maxoff; size_t lw_offset; void *lw_obj; } txg_list_walk_data_t; static int txg_list_walk_init_common(mdb_walk_state_t *wsp, int txg, int maxoff) { txg_list_walk_data_t *lwd; txg_list_t list; int i; lwd = mdb_alloc(sizeof (txg_list_walk_data_t), UM_SLEEP | UM_GC); if (mdb_vread(&list, sizeof (txg_list_t), wsp->walk_addr) == -1) { mdb_warn("failed to read txg_list_t at %#lx", wsp->walk_addr); return (WALK_ERR); } for (i = 0; i < TXG_SIZE; i++) lwd->lw_head[i] = (uintptr_t)list.tl_head[i]; lwd->lw_offset = list.tl_offset; lwd->lw_obj = mdb_alloc(lwd->lw_offset + sizeof (txg_node_t), UM_SLEEP | UM_GC); lwd->lw_txgoff = txg; lwd->lw_maxoff = maxoff; wsp->walk_addr = lwd->lw_head[lwd->lw_txgoff]; wsp->walk_data = lwd; return (WALK_NEXT); } static int txg_list_walk_init(mdb_walk_state_t *wsp) { return (txg_list_walk_init_common(wsp, 0, TXG_SIZE-1)); } static int txg_list0_walk_init(mdb_walk_state_t *wsp) { return (txg_list_walk_init_common(wsp, 0, 0)); } static int txg_list1_walk_init(mdb_walk_state_t *wsp) { return (txg_list_walk_init_common(wsp, 1, 1)); } static int txg_list2_walk_init(mdb_walk_state_t *wsp) { return (txg_list_walk_init_common(wsp, 2, 2)); } static int txg_list3_walk_init(mdb_walk_state_t *wsp) { return (txg_list_walk_init_common(wsp, 3, 3)); } static int txg_list_walk_step(mdb_walk_state_t *wsp) { txg_list_walk_data_t *lwd = wsp->walk_data; uintptr_t addr; txg_node_t *node; int status; while (wsp->walk_addr == NULL && lwd->lw_txgoff < lwd->lw_maxoff) { lwd->lw_txgoff++; wsp->walk_addr = lwd->lw_head[lwd->lw_txgoff]; } if (wsp->walk_addr == NULL) return (WALK_DONE); addr = wsp->walk_addr - lwd->lw_offset; if (mdb_vread(lwd->lw_obj, lwd->lw_offset + sizeof (txg_node_t), addr) == -1) { mdb_warn("failed to read list element at %#lx", addr); return (WALK_ERR); } status = wsp->walk_callback(addr, lwd->lw_obj, wsp->walk_cbdata); node = (txg_node_t *)((uintptr_t)lwd->lw_obj + lwd->lw_offset); wsp->walk_addr = (uintptr_t)node->tn_next[lwd->lw_txgoff]; return (status); } /* ARGSUSED */ static void txg_list_walk_fini(mdb_walk_state_t *wsp) { } /* * ::walk spa * * Walk all named spa_t structures in the namespace. This is nothing more than * a layered avl walk. */ static int spa_walk_init(mdb_walk_state_t *wsp) { GElf_Sym sym; if (wsp->walk_addr != NULL) { mdb_warn("spa walk only supports global walks\n"); return (WALK_ERR); } if (mdb_lookup_by_obj(ZFS_OBJ_NAME, "spa_namespace_avl", &sym) == -1) { mdb_warn("failed to find symbol 'spa_namespace_avl'"); return (WALK_ERR); } wsp->walk_addr = (uintptr_t)sym.st_value; if (mdb_layered_walk("avl", wsp) == -1) { mdb_warn("failed to walk 'avl'\n"); return (WALK_ERR); } return (WALK_NEXT); } static int spa_walk_step(mdb_walk_state_t *wsp) { spa_t spa; if (mdb_vread(&spa, sizeof (spa), wsp->walk_addr) == -1) { mdb_warn("failed to read spa_t at %p", wsp->walk_addr); return (WALK_ERR); } return (wsp->walk_callback(wsp->walk_addr, &spa, wsp->walk_cbdata)); } /* * MDB module linkage information: * * We declare a list of structures describing our dcmds, and a function * named _mdb_init to return a pointer to our module information. */ static const mdb_dcmd_t dcmds[] = { { "blkptr", ":", "print blkptr_t", blkptr }, { "dbuf", ":", "print dmu_buf_impl_t", dbuf }, { "dbuf_stats", ":", "dbuf stats", dbuf_stats }, { "dbufs", "\t[-O objset_t*] [-n objset_name | \"mos\"] [-o object | \"mdn\"] \n" "\t[-l level] [-b blkid | \"bonus\"]", "find dmu_buf_impl_t's that meet criterion", dbufs }, { "abuf_find", "dva_word[0] dva_word[1]", "find arc_buf_hdr_t of a specified DVA", abuf_find }, { "spa", "?[-cv]", "spa_t summary", spa_print }, { "spa_config", ":", "print spa_t configuration", spa_print_config }, { "spa_verify", ":", "verify spa_t consistency", spa_verify }, { "spa_space", ":[-b]", "print spa_t on-disk space usage", spa_space }, { "spa_vdevs", ":", "given a spa_t, print vdev summary", spa_vdevs }, { "vdev", ":[-qre]", "vdev_t summary", vdev_print }, { "zio_pipeline", ":", "decode a zio pipeline", zio_pipeline }, { NULL } }; static const mdb_walker_t walkers[] = { /* * In userland, there is no generic provider of list_t walkers, so we * need to add it. */ #ifndef _KERNEL { LIST_WALK_NAME, LIST_WALK_DESC, list_walk_init, list_walk_step, list_walk_fini }, #endif { "dbufs", "walk cached ZFS dbufs", dbuf_walk_init, dbuf_walk_step, dbuf_walk_fini }, { "zms_freelist", "walk ZFS metaslab freelist", freelist_walk_init, freelist_walk_step, freelist_walk_fini }, { "txg_list", "given any txg_list_t *, walk all entries in all txgs", txg_list_walk_init, txg_list_walk_step, txg_list_walk_fini }, { "txg_list0", "given any txg_list_t *, walk all entries in txg 0", txg_list0_walk_init, txg_list_walk_step, txg_list_walk_fini }, { "txg_list1", "given any txg_list_t *, walk all entries in txg 1", txg_list1_walk_init, txg_list_walk_step, txg_list_walk_fini }, { "txg_list2", "given any txg_list_t *, walk all entries in txg 2", txg_list2_walk_init, txg_list_walk_step, txg_list_walk_fini }, { "txg_list3", "given any txg_list_t *, walk all entries in txg 3", txg_list3_walk_init, txg_list_walk_step, txg_list_walk_fini }, { "spa", "walk all spa_t entries in the namespace", spa_walk_init, spa_walk_step, NULL }, { NULL } }; static const mdb_modinfo_t modinfo = { MDB_API_VERSION, dcmds, walkers }; const mdb_modinfo_t * _mdb_init(void) { return (&modinfo); }