xref: /illumos-gate/usr/src/cmd/mdb/common/modules/zfs/zfs.c (revision 43466aae)
1fa9e4066Sahrens /*
2fa9e4066Sahrens  * CDDL HEADER START
3fa9e4066Sahrens  *
4fa9e4066Sahrens  * The contents of this file are subject to the terms of the
5ea8dc4b6Seschrock  * Common Development and Distribution License (the "License").
6ea8dc4b6Seschrock  * You may not use this file except in compliance with the License.
7fa9e4066Sahrens  *
8fa9e4066Sahrens  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9fa9e4066Sahrens  * or http://www.opensolaris.org/os/licensing.
10fa9e4066Sahrens  * See the License for the specific language governing permissions
11fa9e4066Sahrens  * and limitations under the License.
12fa9e4066Sahrens  *
13fa9e4066Sahrens  * When distributing Covered Code, include this CDDL HEADER in each
14fa9e4066Sahrens  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15fa9e4066Sahrens  * If applicable, add the following below this CDDL HEADER, with the
16fa9e4066Sahrens  * fields enclosed by brackets "[]" replaced with your own identifying
17fa9e4066Sahrens  * information: Portions Copyright [yyyy] [name of copyright owner]
18fa9e4066Sahrens  *
19fa9e4066Sahrens  * CDDL HEADER END
20fa9e4066Sahrens  */
21fa9e4066Sahrens /*
2255da60b9SMark J Musante  * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
23beb56283SShampavman  * Copyright 2011 Nexenta Systems, Inc. All rights reserved.
2428e4da25SMatthew Ahrens  * Copyright (c) 2013 by Delphix. All rights reserved.
25fa9e4066Sahrens  */
26fa9e4066Sahrens 
2755da60b9SMark J Musante /* Portions Copyright 2010 Robert Milkowski */
2855da60b9SMark J Musante 
29fa9e4066Sahrens #include <mdb/mdb_ctf.h>
30fa9e4066Sahrens #include <sys/zfs_context.h>
31fa9e4066Sahrens #include <sys/mdb_modapi.h>
32fa9e4066Sahrens #include <sys/dbuf.h>
33fa9e4066Sahrens #include <sys/dmu_objset.h>
34fa9e4066Sahrens #include <sys/dsl_dir.h>
35fa9e4066Sahrens #include <sys/dsl_pool.h>
36fa9e4066Sahrens #include <sys/metaslab_impl.h>
37fa9e4066Sahrens #include <sys/space_map.h>
38fa9e4066Sahrens #include <sys/list.h>
39fa9e4066Sahrens #include <sys/vdev_impl.h>
403f1f8012SMatthew Ahrens #include <sys/zap_leaf.h>
413f1f8012SMatthew Ahrens #include <sys/zap_impl.h>
429966ca11SMatthew Ahrens #include <ctype.h>
430a586ceaSMark Shellenbaum #include <sys/zfs_acl.h>
440a586ceaSMark Shellenbaum #include <sys/sa_impl.h>
45fa9e4066Sahrens 
46fa9e4066Sahrens #ifdef _KERNEL
47fa9e4066Sahrens #define	ZFS_OBJ_NAME	"zfs"
48c55e05cbSMatthew Ahrens extern int64_t mdb_gethrtime(void);
49fa9e4066Sahrens #else
50fa9e4066Sahrens #define	ZFS_OBJ_NAME	"libzpool.so.1"
51fa9e4066Sahrens #endif
52fa9e4066Sahrens 
5328e4da25SMatthew Ahrens #define	ZFS_STRUCT	"struct " ZFS_OBJ_NAME "`"
5428e4da25SMatthew Ahrens 
55feef89cfSVictor Latushkin #ifndef _KERNEL
56feef89cfSVictor Latushkin int aok;
57feef89cfSVictor Latushkin #endif
58feef89cfSVictor Latushkin 
59fa9e4066Sahrens static int
60fa9e4066Sahrens getmember(uintptr_t addr, const char *type, mdb_ctf_id_t *idp,
61fa9e4066Sahrens     const char *member, int len, void *buf)
62fa9e4066Sahrens {
63fa9e4066Sahrens 	mdb_ctf_id_t id;
64fa9e4066Sahrens 	ulong_t off;
65fa9e4066Sahrens 	char name[64];
66fa9e4066Sahrens 
67fa9e4066Sahrens 	if (idp == NULL) {
68fa9e4066Sahrens 		if (mdb_ctf_lookup_by_name(type, &id) == -1) {
69fa9e4066Sahrens 			mdb_warn("couldn't find type %s", type);
70fa9e4066Sahrens 			return (DCMD_ERR);
71fa9e4066Sahrens 		}
72fa9e4066Sahrens 		idp = &id;
73fa9e4066Sahrens 	} else {
74fa9e4066Sahrens 		type = name;
75fa9e4066Sahrens 		mdb_ctf_type_name(*idp, name, sizeof (name));
76fa9e4066Sahrens 	}
77fa9e4066Sahrens 
78fa9e4066Sahrens 	if (mdb_ctf_offsetof(*idp, member, &off) == -1) {
79fa9e4066Sahrens 		mdb_warn("couldn't find member %s of type %s\n", member, type);
80fa9e4066Sahrens 		return (DCMD_ERR);
81fa9e4066Sahrens 	}
82fa9e4066Sahrens 	if (off % 8 != 0) {
83fa9e4066Sahrens 		mdb_warn("member %s of type %s is unsupported bitfield",
84fa9e4066Sahrens 		    member, type);
85fa9e4066Sahrens 		return (DCMD_ERR);
86fa9e4066Sahrens 	}
87fa9e4066Sahrens 	off /= 8;
88fa9e4066Sahrens 
89fa9e4066Sahrens 	if (mdb_vread(buf, len, addr + off) == -1) {
90fa9e4066Sahrens 		mdb_warn("failed to read %s from %s at %p",
91fa9e4066Sahrens 		    member, type, addr + off);
92fa9e4066Sahrens 		return (DCMD_ERR);
93fa9e4066Sahrens 	}
94fa9e4066Sahrens 	/* mdb_warn("read %s from %s at %p+%llx\n", member, type, addr, off); */
95fa9e4066Sahrens 
96fa9e4066Sahrens 	return (0);
97fa9e4066Sahrens }
98fa9e4066Sahrens 
9928e4da25SMatthew Ahrens #define	GETMEMB(addr, structname, member, dest) \
10028e4da25SMatthew Ahrens 	getmember(addr, ZFS_STRUCT structname, NULL, #member, \
10128e4da25SMatthew Ahrens 	sizeof (dest), &(dest))
102fa9e4066Sahrens 
103fa9e4066Sahrens #define	GETMEMBID(addr, ctfid, member, dest) \
104fa9e4066Sahrens 	getmember(addr, NULL, ctfid, #member, sizeof (dest), &(dest))
105fa9e4066Sahrens 
1063f9d6ad7SLin Ling static boolean_t
1073f9d6ad7SLin Ling strisprint(const char *cp)
1083f9d6ad7SLin Ling {
1093f9d6ad7SLin Ling 	for (; *cp; cp++) {
1103f9d6ad7SLin Ling 		if (!isprint(*cp))
1113f9d6ad7SLin Ling 			return (B_FALSE);
1123f9d6ad7SLin Ling 	}
1133f9d6ad7SLin Ling 	return (B_TRUE);
1143f9d6ad7SLin Ling }
1153f9d6ad7SLin Ling 
116fa9e4066Sahrens static int verbose;
117fa9e4066Sahrens 
118fa9e4066Sahrens static int
119fa9e4066Sahrens freelist_walk_init(mdb_walk_state_t *wsp)
120fa9e4066Sahrens {
121fa9e4066Sahrens 	if (wsp->walk_addr == NULL) {
122fa9e4066Sahrens 		mdb_warn("must supply starting address\n");
123fa9e4066Sahrens 		return (WALK_ERR);
124fa9e4066Sahrens 	}
125fa9e4066Sahrens 
126fa9e4066Sahrens 	wsp->walk_data = 0;  /* Index into the freelist */
127fa9e4066Sahrens 	return (WALK_NEXT);
128fa9e4066Sahrens }
129fa9e4066Sahrens 
130fa9e4066Sahrens static int
131fa9e4066Sahrens freelist_walk_step(mdb_walk_state_t *wsp)
132fa9e4066Sahrens {
133fa9e4066Sahrens 	uint64_t entry;
134fa9e4066Sahrens 	uintptr_t number = (uintptr_t)wsp->walk_data;
1358053a263Sck 	char *ddata[] = { "ALLOC", "FREE", "CONDENSE", "INVALID",
1368053a263Sck 			    "INVALID", "INVALID", "INVALID", "INVALID" };
137fa9e4066Sahrens 	int mapshift = SPA_MINBLOCKSHIFT;
138fa9e4066Sahrens 
139fa9e4066Sahrens 	if (mdb_vread(&entry, sizeof (entry), wsp->walk_addr) == -1) {
140fa9e4066Sahrens 		mdb_warn("failed to read freelist entry %p", wsp->walk_addr);
141fa9e4066Sahrens 		return (WALK_DONE);
142fa9e4066Sahrens 	}
143fa9e4066Sahrens 	wsp->walk_addr += sizeof (entry);
144fa9e4066Sahrens 	wsp->walk_data = (void *)(number + 1);
145fa9e4066Sahrens 
146fa9e4066Sahrens 	if (SM_DEBUG_DECODE(entry)) {
147fa9e4066Sahrens 		mdb_printf("DEBUG: %3u  %10s: txg=%llu  pass=%llu\n",
148fa9e4066Sahrens 		    number,
149fa9e4066Sahrens 		    ddata[SM_DEBUG_ACTION_DECODE(entry)],
150fa9e4066Sahrens 		    SM_DEBUG_TXG_DECODE(entry),
151fa9e4066Sahrens 		    SM_DEBUG_SYNCPASS_DECODE(entry));
152fa9e4066Sahrens 	} else {
153fa9e4066Sahrens 		mdb_printf("Entry: %3u  offsets=%08llx-%08llx  type=%c  "
154fa9e4066Sahrens 		    "size=%06llx", number,
155fa9e4066Sahrens 		    SM_OFFSET_DECODE(entry) << mapshift,
156fa9e4066Sahrens 		    (SM_OFFSET_DECODE(entry) + SM_RUN_DECODE(entry)) <<
157fa9e4066Sahrens 		    mapshift,
158fa9e4066Sahrens 		    SM_TYPE_DECODE(entry) == SM_ALLOC ? 'A' : 'F',
159fa9e4066Sahrens 		    SM_RUN_DECODE(entry) << mapshift);
160fa9e4066Sahrens 		if (verbose)
161fa9e4066Sahrens 			mdb_printf("      (raw=%012llx)\n", entry);
162fa9e4066Sahrens 		mdb_printf("\n");
163fa9e4066Sahrens 	}
164fa9e4066Sahrens 	return (WALK_NEXT);
165fa9e4066Sahrens }
166fa9e4066Sahrens 
167fa9e4066Sahrens static int
16828e4da25SMatthew Ahrens mdb_dsl_dir_name(uintptr_t addr, char *buf)
169fa9e4066Sahrens {
170fa9e4066Sahrens 	static int gotid;
171fa9e4066Sahrens 	static mdb_ctf_id_t dd_id;
172fa9e4066Sahrens 	uintptr_t dd_parent;
173fa9e4066Sahrens 	char dd_myname[MAXNAMELEN];
174fa9e4066Sahrens 
175fa9e4066Sahrens 	if (!gotid) {
17628e4da25SMatthew Ahrens 		if (mdb_ctf_lookup_by_name(ZFS_STRUCT "dsl_dir",
177fa9e4066Sahrens 		    &dd_id) == -1) {
178fa9e4066Sahrens 			mdb_warn("couldn't find struct dsl_dir");
179fa9e4066Sahrens 			return (DCMD_ERR);
180fa9e4066Sahrens 		}
181fa9e4066Sahrens 		gotid = TRUE;
182fa9e4066Sahrens 	}
183fa9e4066Sahrens 	if (GETMEMBID(addr, &dd_id, dd_parent, dd_parent) ||
184fa9e4066Sahrens 	    GETMEMBID(addr, &dd_id, dd_myname, dd_myname)) {
185fa9e4066Sahrens 		return (DCMD_ERR);
186fa9e4066Sahrens 	}
187fa9e4066Sahrens 
188fa9e4066Sahrens 	if (dd_parent) {
18928e4da25SMatthew Ahrens 		if (mdb_dsl_dir_name(dd_parent, buf))
190fa9e4066Sahrens 			return (DCMD_ERR);
191fa9e4066Sahrens 		strcat(buf, "/");
192fa9e4066Sahrens 	}
193fa9e4066Sahrens 
194fa9e4066Sahrens 	if (dd_myname[0])
195fa9e4066Sahrens 		strcat(buf, dd_myname);
196fa9e4066Sahrens 	else
197fa9e4066Sahrens 		strcat(buf, "???");
198fa9e4066Sahrens 
199fa9e4066Sahrens 	return (0);
200fa9e4066Sahrens }
201fa9e4066Sahrens 
202fa9e4066Sahrens static int
203fa9e4066Sahrens objset_name(uintptr_t addr, char *buf)
204fa9e4066Sahrens {
205fa9e4066Sahrens 	static int gotid;
2064223fc7cSMark Shellenbaum 	static mdb_ctf_id_t os_id, ds_id;
207fa9e4066Sahrens 	uintptr_t os_dsl_dataset;
208fa9e4066Sahrens 	char ds_snapname[MAXNAMELEN];
209fa9e4066Sahrens 	uintptr_t ds_dir;
210fa9e4066Sahrens 
211fa9e4066Sahrens 	buf[0] = '\0';
212fa9e4066Sahrens 
213fa9e4066Sahrens 	if (!gotid) {
21428e4da25SMatthew Ahrens 		if (mdb_ctf_lookup_by_name(ZFS_STRUCT "objset",
2154223fc7cSMark Shellenbaum 		    &os_id) == -1) {
2164223fc7cSMark Shellenbaum 			mdb_warn("couldn't find struct objset");
217fa9e4066Sahrens 			return (DCMD_ERR);
218fa9e4066Sahrens 		}
21928e4da25SMatthew Ahrens 		if (mdb_ctf_lookup_by_name(ZFS_STRUCT "dsl_dataset",
220fa9e4066Sahrens 		    &ds_id) == -1) {
221fa9e4066Sahrens 			mdb_warn("couldn't find struct dsl_dataset");
222fa9e4066Sahrens 			return (DCMD_ERR);
223fa9e4066Sahrens 		}
224fa9e4066Sahrens 
225fa9e4066Sahrens 		gotid = TRUE;
226fa9e4066Sahrens 	}
227fa9e4066Sahrens 
2284223fc7cSMark Shellenbaum 	if (GETMEMBID(addr, &os_id, os_dsl_dataset, os_dsl_dataset))
229fa9e4066Sahrens 		return (DCMD_ERR);
230fa9e4066Sahrens 
231fa9e4066Sahrens 	if (os_dsl_dataset == 0) {
232fa9e4066Sahrens 		strcat(buf, "mos");
233fa9e4066Sahrens 		return (0);
234fa9e4066Sahrens 	}
235fa9e4066Sahrens 
236fa9e4066Sahrens 	if (GETMEMBID(os_dsl_dataset, &ds_id, ds_snapname, ds_snapname) ||
237fa9e4066Sahrens 	    GETMEMBID(os_dsl_dataset, &ds_id, ds_dir, ds_dir)) {
238fa9e4066Sahrens 		return (DCMD_ERR);
239fa9e4066Sahrens 	}
240fa9e4066Sahrens 
24128e4da25SMatthew Ahrens 	if (ds_dir && mdb_dsl_dir_name(ds_dir, buf))
242fa9e4066Sahrens 		return (DCMD_ERR);
243fa9e4066Sahrens 
244fa9e4066Sahrens 	if (ds_snapname[0]) {
245fa9e4066Sahrens 		strcat(buf, "@");
246fa9e4066Sahrens 		strcat(buf, ds_snapname);
247fa9e4066Sahrens 	}
248fa9e4066Sahrens 	return (0);
249fa9e4066Sahrens }
250fa9e4066Sahrens 
251fa9e4066Sahrens static void
252fa9e4066Sahrens enum_lookup(char *out, size_t size, mdb_ctf_id_t id, int val,
253fa9e4066Sahrens     const char *prefix)
254fa9e4066Sahrens {
255fa9e4066Sahrens 	const char *cp;
256fa9e4066Sahrens 	size_t len = strlen(prefix);
257fa9e4066Sahrens 
258fa9e4066Sahrens 	if ((cp = mdb_ctf_enum_name(id, val)) != NULL) {
259fa9e4066Sahrens 		if (strncmp(cp, prefix, len) == 0)
260fa9e4066Sahrens 			cp += len;
261fa9e4066Sahrens 		(void) strncpy(out, cp, size);
262fa9e4066Sahrens 	} else {
263fa9e4066Sahrens 		mdb_snprintf(out, size, "? (%d)", val);
264fa9e4066Sahrens 	}
265fa9e4066Sahrens }
266fa9e4066Sahrens 
267614409b5Sahrens /* ARGSUSED */
268614409b5Sahrens static int
269614409b5Sahrens zfs_params(uintptr_t addr, uint_t flags, int argc, const mdb_arg_t *argv)
270614409b5Sahrens {
271614409b5Sahrens 	/*
272614409b5Sahrens 	 * This table can be approximately generated by running:
273614409b5Sahrens 	 * egrep "^[a-z0-9_]+ [a-z0-9_]+( =.*)?;" *.c | cut -d ' ' -f 2
274614409b5Sahrens 	 */
275614409b5Sahrens 	static const char *params[] = {
276614409b5Sahrens 		"arc_reduce_dnlc_percent",
27769962b56SMatthew Ahrens 		"arc_lotsfree_percent",
27869962b56SMatthew Ahrens 		"zfs_dirty_data_max",
27969962b56SMatthew Ahrens 		"zfs_dirty_data_sync",
28069962b56SMatthew Ahrens 		"zfs_delay_max_ns",
28169962b56SMatthew Ahrens 		"zfs_delay_min_dirty_percent",
28269962b56SMatthew Ahrens 		"zfs_delay_scale",
28369962b56SMatthew Ahrens 		"zfs_vdev_max_active",
28469962b56SMatthew Ahrens 		"zfs_vdev_sync_read_min_active",
28569962b56SMatthew Ahrens 		"zfs_vdev_sync_read_max_active",
28669962b56SMatthew Ahrens 		"zfs_vdev_sync_write_min_active",
28769962b56SMatthew Ahrens 		"zfs_vdev_sync_write_max_active",
28869962b56SMatthew Ahrens 		"zfs_vdev_async_read_min_active",
28969962b56SMatthew Ahrens 		"zfs_vdev_async_read_max_active",
29069962b56SMatthew Ahrens 		"zfs_vdev_async_write_min_active",
29169962b56SMatthew Ahrens 		"zfs_vdev_async_write_max_active",
29269962b56SMatthew Ahrens 		"zfs_vdev_scrub_min_active",
29369962b56SMatthew Ahrens 		"zfs_vdev_scrub_max_active",
29469962b56SMatthew Ahrens 		"zfs_vdev_async_write_active_min_dirty_percent",
29569962b56SMatthew Ahrens 		"zfs_vdev_async_write_active_max_dirty_percent",
29669962b56SMatthew Ahrens 		"spa_asize_inflation",
297614409b5Sahrens 		"zfs_arc_max",
298614409b5Sahrens 		"zfs_arc_min",
29949e3519aSmaybee 		"arc_shrink_shift",
300614409b5Sahrens 		"zfs_mdcomp_disable",
301614409b5Sahrens 		"zfs_prefetch_disable",
302614409b5Sahrens 		"zfetch_max_streams",
303614409b5Sahrens 		"zfetch_min_sec_reap",
304614409b5Sahrens 		"zfetch_block_cap",
305614409b5Sahrens 		"zfetch_array_rd_sz",
306614409b5Sahrens 		"zfs_default_bs",
307614409b5Sahrens 		"zfs_default_ibs",
308614409b5Sahrens 		"metaslab_aliquot",
309614409b5Sahrens 		"reference_tracking_enable",
310614409b5Sahrens 		"reference_history",
311614409b5Sahrens 		"spa_max_replication_override",
312b24ab676SJeff Bonwick 		"spa_mode_global",
313614409b5Sahrens 		"zfs_flags",
3141ab7f2deSmaybee 		"zfs_txg_timeout",
315614409b5Sahrens 		"zfs_vdev_cache_max",
316614409b5Sahrens 		"zfs_vdev_cache_size",
317614409b5Sahrens 		"zfs_vdev_cache_bshift",
318614409b5Sahrens 		"vdev_mirror_shift",
319614409b5Sahrens 		"zfs_scrub_limit",
320b16da2e2SGeorge Wilson 		"zfs_no_scrub_io",
321b16da2e2SGeorge Wilson 		"zfs_no_scrub_prefetch",
322614409b5Sahrens 		"zfs_vdev_aggregation_limit",
323614409b5Sahrens 		"fzap_default_block_shift",
324614409b5Sahrens 		"zfs_immediate_write_sz",
325614409b5Sahrens 		"zfs_read_chunk_size",
326614409b5Sahrens 		"zfs_nocacheflush",
32755da60b9SMark J Musante 		"zil_replay_disable",
3281ab7f2deSmaybee 		"metaslab_gang_bang",
329d6e555bdSGeorge Wilson 		"metaslab_df_alloc_threshold",
330d6e555bdSGeorge Wilson 		"metaslab_df_free_pct",
331614409b5Sahrens 		"zio_injection_enabled",
332614409b5Sahrens 		"zvol_immediate_write_sz",
333614409b5Sahrens 	};
334614409b5Sahrens 
335b24ab676SJeff Bonwick 	for (int i = 0; i < sizeof (params) / sizeof (params[0]); i++) {
336614409b5Sahrens 		int sz;
337614409b5Sahrens 		uint64_t val64;
338614409b5Sahrens 		uint32_t *val32p = (uint32_t *)&val64;
339614409b5Sahrens 
340614409b5Sahrens 		sz = mdb_readvar(&val64, params[i]);
341614409b5Sahrens 		if (sz == 4) {
342614409b5Sahrens 			mdb_printf("%s = 0x%x\n", params[i], *val32p);
343614409b5Sahrens 		} else if (sz == 8) {
344614409b5Sahrens 			mdb_printf("%s = 0x%llx\n", params[i], val64);
345614409b5Sahrens 		} else {
346614409b5Sahrens 			mdb_warn("variable %s not found", params[i]);
347614409b5Sahrens 		}
348614409b5Sahrens 	}
349614409b5Sahrens 
350614409b5Sahrens 	return (DCMD_OK);
351614409b5Sahrens }
352614409b5Sahrens 
353fa9e4066Sahrens /* ARGSUSED */
354fa9e4066Sahrens static int
355fa9e4066Sahrens blkptr(uintptr_t addr, uint_t flags, int argc, const mdb_arg_t *argv)
356fa9e4066Sahrens {
357b24ab676SJeff Bonwick 	mdb_ctf_id_t type_enum, checksum_enum, compress_enum;
358b24ab676SJeff Bonwick 	char type[80], checksum[80], compress[80];
359b24ab676SJeff Bonwick 	blkptr_t blk, *bp = &blk;
360b24ab676SJeff Bonwick 	char buf[BP_SPRINTF_LEN];
361fa9e4066Sahrens 
362b24ab676SJeff Bonwick 	if (mdb_vread(&blk, sizeof (blkptr_t), addr) == -1) {
363fa9e4066Sahrens 		mdb_warn("failed to read blkptr_t");
364fa9e4066Sahrens 		return (DCMD_ERR);
365fa9e4066Sahrens 	}
366fa9e4066Sahrens 
367b24ab676SJeff Bonwick 	if (mdb_ctf_lookup_by_name("enum dmu_object_type", &type_enum) == -1 ||
368b24ab676SJeff Bonwick 	    mdb_ctf_lookup_by_name("enum zio_checksum", &checksum_enum) == -1 ||
369b24ab676SJeff Bonwick 	    mdb_ctf_lookup_by_name("enum zio_compress", &compress_enum) == -1) {
370b24ab676SJeff Bonwick 		mdb_warn("Could not find blkptr enumerated types");
371fa9e4066Sahrens 		return (DCMD_ERR);
372fa9e4066Sahrens 	}
373fa9e4066Sahrens 
374b24ab676SJeff Bonwick 	enum_lookup(type, sizeof (type), type_enum,
375b24ab676SJeff Bonwick 	    BP_GET_TYPE(bp), "DMU_OT_");
376b24ab676SJeff Bonwick 	enum_lookup(checksum, sizeof (checksum), checksum_enum,
377b24ab676SJeff Bonwick 	    BP_GET_CHECKSUM(bp), "ZIO_CHECKSUM_");
378b24ab676SJeff Bonwick 	enum_lookup(compress, sizeof (compress), compress_enum,
379b24ab676SJeff Bonwick 	    BP_GET_COMPRESS(bp), "ZIO_COMPRESS_");
380fa9e4066Sahrens 
381*43466aaeSMax Grossman 	SNPRINTF_BLKPTR(mdb_snprintf, '\n', buf, sizeof (buf), bp, type,
382*43466aaeSMax Grossman 	    checksum, compress);
383fa9e4066Sahrens 
384b24ab676SJeff Bonwick 	mdb_printf("%s\n", buf);
385fa9e4066Sahrens 
386fa9e4066Sahrens 	return (DCMD_OK);
387fa9e4066Sahrens }
388fa9e4066Sahrens 
38928e4da25SMatthew Ahrens typedef struct mdb_dmu_buf_impl {
39028e4da25SMatthew Ahrens 	struct {
39128e4da25SMatthew Ahrens 		uint64_t db_object;
39228e4da25SMatthew Ahrens 	} db;
393d5ee8a13SMatthew Ahrens 	uintptr_t db_objset;
39428e4da25SMatthew Ahrens 	uint64_t db_level;
39528e4da25SMatthew Ahrens 	uint64_t db_blkid;
39628e4da25SMatthew Ahrens 	struct {
39728e4da25SMatthew Ahrens 		uint64_t rc_count;
39828e4da25SMatthew Ahrens 	} db_holds;
39928e4da25SMatthew Ahrens } mdb_dmu_buf_impl_t;
40028e4da25SMatthew Ahrens 
401fa9e4066Sahrens /* ARGSUSED */
402fa9e4066Sahrens static int
403fa9e4066Sahrens dbuf(uintptr_t addr, uint_t flags, int argc, const mdb_arg_t *argv)
404fa9e4066Sahrens {
40528e4da25SMatthew Ahrens 	mdb_dmu_buf_impl_t db;
406fa9e4066Sahrens 	char objectname[32];
407fa9e4066Sahrens 	char blkidname[32];
408fa9e4066Sahrens 	char path[MAXNAMELEN];
409fa9e4066Sahrens 
41028e4da25SMatthew Ahrens 	if (DCMD_HDRSPEC(flags))
411fa9e4066Sahrens 		mdb_printf("        addr object lvl blkid holds os\n");
412fa9e4066Sahrens 
41328e4da25SMatthew Ahrens 	if (mdb_ctf_vread(&db, ZFS_STRUCT "dmu_buf_impl", "mdb_dmu_buf_impl_t",
41428e4da25SMatthew Ahrens 	    addr, 0) == -1)
415fa9e4066Sahrens 		return (DCMD_ERR);
416fa9e4066Sahrens 
41728e4da25SMatthew Ahrens 	if (db.db.db_object == DMU_META_DNODE_OBJECT)
418fa9e4066Sahrens 		(void) strcpy(objectname, "mdn");
419fa9e4066Sahrens 	else
420fa9e4066Sahrens 		(void) mdb_snprintf(objectname, sizeof (objectname), "%llx",
42128e4da25SMatthew Ahrens 		    (u_longlong_t)db.db.db_object);
422fa9e4066Sahrens 
42328e4da25SMatthew Ahrens 	if (db.db_blkid == DMU_BONUS_BLKID)
424fa9e4066Sahrens 		(void) strcpy(blkidname, "bonus");
425fa9e4066Sahrens 	else
426fa9e4066Sahrens 		(void) mdb_snprintf(blkidname, sizeof (blkidname), "%llx",
42728e4da25SMatthew Ahrens 		    (u_longlong_t)db.db_blkid);
428fa9e4066Sahrens 
429d5ee8a13SMatthew Ahrens 	if (objset_name(db.db_objset, path)) {
43028e4da25SMatthew Ahrens 		return (DCMD_ERR);
431fa9e4066Sahrens 	}
432fa9e4066Sahrens 
43328e4da25SMatthew Ahrens 	mdb_printf("%p %8s %1u %9s %2llu %s\n", addr,
43428e4da25SMatthew Ahrens 	    objectname, (int)db.db_level, blkidname,
43528e4da25SMatthew Ahrens 	    db.db_holds.rc_count, path);
436fa9e4066Sahrens 
437fa9e4066Sahrens 	return (DCMD_OK);
438fa9e4066Sahrens }
439fa9e4066Sahrens 
440fa9e4066Sahrens /* ARGSUSED */
441fa9e4066Sahrens static int
442fa9e4066Sahrens dbuf_stats(uintptr_t addr, uint_t flags, int argc, const mdb_arg_t *argv)
443fa9e4066Sahrens {
444fa9e4066Sahrens #define	HISTOSZ 32
445fa9e4066Sahrens 	uintptr_t dbp;
446fa9e4066Sahrens 	dmu_buf_impl_t db;
447fa9e4066Sahrens 	dbuf_hash_table_t ht;
448fa9e4066Sahrens 	uint64_t bucket, ndbufs;
449fa9e4066Sahrens 	uint64_t histo[HISTOSZ];
450fa9e4066Sahrens 	uint64_t histo2[HISTOSZ];
451fa9e4066Sahrens 	int i, maxidx;
452fa9e4066Sahrens 
453fa9e4066Sahrens 	if (mdb_readvar(&ht, "dbuf_hash_table") == -1) {
454fa9e4066Sahrens 		mdb_warn("failed to read 'dbuf_hash_table'");
455fa9e4066Sahrens 		return (DCMD_ERR);
456fa9e4066Sahrens 	}
457fa9e4066Sahrens 
458fa9e4066Sahrens 	for (i = 0; i < HISTOSZ; i++) {
459fa9e4066Sahrens 		histo[i] = 0;
460fa9e4066Sahrens 		histo2[i] = 0;
461fa9e4066Sahrens 	}
462fa9e4066Sahrens 
463fa9e4066Sahrens 	ndbufs = 0;
464fa9e4066Sahrens 	for (bucket = 0; bucket < ht.hash_table_mask+1; bucket++) {
465fa9e4066Sahrens 		int len;
466fa9e4066Sahrens 
467fa9e4066Sahrens 		if (mdb_vread(&dbp, sizeof (void *),
468fa9e4066Sahrens 		    (uintptr_t)(ht.hash_table+bucket)) == -1) {
469fa9e4066Sahrens 			mdb_warn("failed to read hash bucket %u at %p",
470fa9e4066Sahrens 			    bucket, ht.hash_table+bucket);
471fa9e4066Sahrens 			return (DCMD_ERR);
472fa9e4066Sahrens 		}
473fa9e4066Sahrens 
474fa9e4066Sahrens 		len = 0;
475fa9e4066Sahrens 		while (dbp != 0) {
476fa9e4066Sahrens 			if (mdb_vread(&db, sizeof (dmu_buf_impl_t),
477fa9e4066Sahrens 			    dbp) == -1) {
478fa9e4066Sahrens 				mdb_warn("failed to read dbuf at %p", dbp);
479fa9e4066Sahrens 				return (DCMD_ERR);
480fa9e4066Sahrens 			}
481fa9e4066Sahrens 			dbp = (uintptr_t)db.db_hash_next;
482fa9e4066Sahrens 			for (i = MIN(len, HISTOSZ - 1); i >= 0; i--)
483fa9e4066Sahrens 				histo2[i]++;
484fa9e4066Sahrens 			len++;
485fa9e4066Sahrens 			ndbufs++;
486fa9e4066Sahrens 		}
487fa9e4066Sahrens 
488fa9e4066Sahrens 		if (len >= HISTOSZ)
489fa9e4066Sahrens 			len = HISTOSZ-1;
490fa9e4066Sahrens 		histo[len]++;
491fa9e4066Sahrens 	}
492fa9e4066Sahrens 
493fa9e4066Sahrens 	mdb_printf("hash table has %llu buckets, %llu dbufs "
494fa9e4066Sahrens 	    "(avg %llu buckets/dbuf)\n",
495fa9e4066Sahrens 	    ht.hash_table_mask+1, ndbufs,
496fa9e4066Sahrens 	    (ht.hash_table_mask+1)/ndbufs);
497fa9e4066Sahrens 
498fa9e4066Sahrens 	mdb_printf("\n");
499fa9e4066Sahrens 	maxidx = 0;
500fa9e4066Sahrens 	for (i = 0; i < HISTOSZ; i++)
501fa9e4066Sahrens 		if (histo[i] > 0)
502fa9e4066Sahrens 			maxidx = i;
503fa9e4066Sahrens 	mdb_printf("hash chain length	number of buckets\n");
504fa9e4066Sahrens 	for (i = 0; i <= maxidx; i++)
505fa9e4066Sahrens 		mdb_printf("%u			%llu\n", i, histo[i]);
506fa9e4066Sahrens 
507fa9e4066Sahrens 	mdb_printf("\n");
508fa9e4066Sahrens 	maxidx = 0;
509fa9e4066Sahrens 	for (i = 0; i < HISTOSZ; i++)
510fa9e4066Sahrens 		if (histo2[i] > 0)
511fa9e4066Sahrens 			maxidx = i;
512fa9e4066Sahrens 	mdb_printf("hash chain depth	number of dbufs\n");
513fa9e4066Sahrens 	for (i = 0; i <= maxidx; i++)
514fa9e4066Sahrens 		mdb_printf("%u or more		%llu	%llu%%\n",
515fa9e4066Sahrens 		    i, histo2[i], histo2[i]*100/ndbufs);
516fa9e4066Sahrens 
517fa9e4066Sahrens 
518fa9e4066Sahrens 	return (DCMD_OK);
519fa9e4066Sahrens }
520fa9e4066Sahrens 
5213f1f8012SMatthew Ahrens #define	CHAIN_END 0xffff
5223f1f8012SMatthew Ahrens /*
5233f1f8012SMatthew Ahrens  * ::zap_leaf [-v]
5243f1f8012SMatthew Ahrens  *
5253f1f8012SMatthew Ahrens  * Print a zap_leaf_phys_t, assumed to be 16k
5263f1f8012SMatthew Ahrens  */
5273f1f8012SMatthew Ahrens /* ARGSUSED */
5283f1f8012SMatthew Ahrens static int
5293f1f8012SMatthew Ahrens zap_leaf(uintptr_t addr, uint_t flags, int argc, const mdb_arg_t *argv)
5303f1f8012SMatthew Ahrens {
5313f1f8012SMatthew Ahrens 	char buf[16*1024];
5323f1f8012SMatthew Ahrens 	int verbose = B_FALSE;
5333f1f8012SMatthew Ahrens 	int four = B_FALSE;
5343f1f8012SMatthew Ahrens 	zap_leaf_t l;
5353f1f8012SMatthew Ahrens 	zap_leaf_phys_t *zlp = (void *)buf;
5363f1f8012SMatthew Ahrens 	int i;
5373f1f8012SMatthew Ahrens 
5383f1f8012SMatthew Ahrens 	if (mdb_getopts(argc, argv,
5393f1f8012SMatthew Ahrens 	    'v', MDB_OPT_SETBITS, TRUE, &verbose,
5403f1f8012SMatthew Ahrens 	    '4', MDB_OPT_SETBITS, TRUE, &four,
5413f1f8012SMatthew Ahrens 	    NULL) != argc)
5423f1f8012SMatthew Ahrens 		return (DCMD_USAGE);
5433f1f8012SMatthew Ahrens 
5443f1f8012SMatthew Ahrens 	l.l_phys = zlp;
5453f1f8012SMatthew Ahrens 	l.l_bs = 14; /* assume 16k blocks */
5463f1f8012SMatthew Ahrens 	if (four)
5473f1f8012SMatthew Ahrens 		l.l_bs = 12;
5483f1f8012SMatthew Ahrens 
5493f1f8012SMatthew Ahrens 	if (!(flags & DCMD_ADDRSPEC)) {
5503f1f8012SMatthew Ahrens 		return (DCMD_USAGE);
5513f1f8012SMatthew Ahrens 	}
5523f1f8012SMatthew Ahrens 
5533f1f8012SMatthew Ahrens 	if (mdb_vread(buf, sizeof (buf), addr) == -1) {
5543f1f8012SMatthew Ahrens 		mdb_warn("failed to read zap_leaf_phys_t at %p", addr);
5553f1f8012SMatthew Ahrens 		return (DCMD_ERR);
5563f1f8012SMatthew Ahrens 	}
5573f1f8012SMatthew Ahrens 
5583f1f8012SMatthew Ahrens 	if (zlp->l_hdr.lh_block_type != ZBT_LEAF ||
5593f1f8012SMatthew Ahrens 	    zlp->l_hdr.lh_magic != ZAP_LEAF_MAGIC) {
5603f1f8012SMatthew Ahrens 		mdb_warn("This does not appear to be a zap_leaf_phys_t");
5613f1f8012SMatthew Ahrens 		return (DCMD_ERR);
5623f1f8012SMatthew Ahrens 	}
5633f1f8012SMatthew Ahrens 
5643f1f8012SMatthew Ahrens 	mdb_printf("zap_leaf_phys_t at %p:\n", addr);
5653f1f8012SMatthew Ahrens 	mdb_printf("    lh_prefix_len = %u\n", zlp->l_hdr.lh_prefix_len);
5663f1f8012SMatthew Ahrens 	mdb_printf("    lh_prefix = %llx\n", zlp->l_hdr.lh_prefix);
5673f1f8012SMatthew Ahrens 	mdb_printf("    lh_nentries = %u\n", zlp->l_hdr.lh_nentries);
5683f1f8012SMatthew Ahrens 	mdb_printf("    lh_nfree = %u\n", zlp->l_hdr.lh_nfree,
5693f1f8012SMatthew Ahrens 	    zlp->l_hdr.lh_nfree * 100 / (ZAP_LEAF_NUMCHUNKS(&l)));
5703f1f8012SMatthew Ahrens 	mdb_printf("    lh_freelist = %u\n", zlp->l_hdr.lh_freelist);
5713f1f8012SMatthew Ahrens 	mdb_printf("    lh_flags = %x (%s)\n", zlp->l_hdr.lh_flags,
5723f1f8012SMatthew Ahrens 	    zlp->l_hdr.lh_flags & ZLF_ENTRIES_CDSORTED ?
5733f1f8012SMatthew Ahrens 	    "ENTRIES_CDSORTED" : "");
5743f1f8012SMatthew Ahrens 
5753f1f8012SMatthew Ahrens 	if (verbose) {
5763f1f8012SMatthew Ahrens 		mdb_printf(" hash table:\n");
5773f1f8012SMatthew Ahrens 		for (i = 0; i < ZAP_LEAF_HASH_NUMENTRIES(&l); i++) {
5783f1f8012SMatthew Ahrens 			if (zlp->l_hash[i] != CHAIN_END)
5793f1f8012SMatthew Ahrens 				mdb_printf("    %u: %u\n", i, zlp->l_hash[i]);
5803f1f8012SMatthew Ahrens 		}
5813f1f8012SMatthew Ahrens 	}
5823f1f8012SMatthew Ahrens 
5833f1f8012SMatthew Ahrens 	mdb_printf(" chunks:\n");
5843f1f8012SMatthew Ahrens 	for (i = 0; i < ZAP_LEAF_NUMCHUNKS(&l); i++) {
5853f1f8012SMatthew Ahrens 		/* LINTED: alignment */
5863f1f8012SMatthew Ahrens 		zap_leaf_chunk_t *zlc = &ZAP_LEAF_CHUNK(&l, i);
5873f1f8012SMatthew Ahrens 		switch (zlc->l_entry.le_type) {
5883f1f8012SMatthew Ahrens 		case ZAP_CHUNK_FREE:
5893f1f8012SMatthew Ahrens 			if (verbose) {
5903f1f8012SMatthew Ahrens 				mdb_printf("    %u: free; lf_next = %u\n",
5913f1f8012SMatthew Ahrens 				    i, zlc->l_free.lf_next);
5923f1f8012SMatthew Ahrens 			}
5933f1f8012SMatthew Ahrens 			break;
5943f1f8012SMatthew Ahrens 		case ZAP_CHUNK_ENTRY:
5953f1f8012SMatthew Ahrens 			mdb_printf("    %u: entry\n", i);
5963f1f8012SMatthew Ahrens 			if (verbose) {
5973f1f8012SMatthew Ahrens 				mdb_printf("        le_next = %u\n",
5983f1f8012SMatthew Ahrens 				    zlc->l_entry.le_next);
5993f1f8012SMatthew Ahrens 			}
6003f1f8012SMatthew Ahrens 			mdb_printf("        le_name_chunk = %u\n",
6013f1f8012SMatthew Ahrens 			    zlc->l_entry.le_name_chunk);
6023f1f8012SMatthew Ahrens 			mdb_printf("        le_name_numints = %u\n",
6033f1f8012SMatthew Ahrens 			    zlc->l_entry.le_name_numints);
6043f1f8012SMatthew Ahrens 			mdb_printf("        le_value_chunk = %u\n",
6053f1f8012SMatthew Ahrens 			    zlc->l_entry.le_value_chunk);
6063f1f8012SMatthew Ahrens 			mdb_printf("        le_value_intlen = %u\n",
6073f1f8012SMatthew Ahrens 			    zlc->l_entry.le_value_intlen);
6083f1f8012SMatthew Ahrens 			mdb_printf("        le_value_numints = %u\n",
6093f1f8012SMatthew Ahrens 			    zlc->l_entry.le_value_numints);
6103f1f8012SMatthew Ahrens 			mdb_printf("        le_cd = %u\n",
6113f1f8012SMatthew Ahrens 			    zlc->l_entry.le_cd);
6123f1f8012SMatthew Ahrens 			mdb_printf("        le_hash = %llx\n",
6133f1f8012SMatthew Ahrens 			    zlc->l_entry.le_hash);
6143f1f8012SMatthew Ahrens 			break;
6153f1f8012SMatthew Ahrens 		case ZAP_CHUNK_ARRAY:
6163f9d6ad7SLin Ling 			mdb_printf("    %u: array", i);
6173f9d6ad7SLin Ling 			if (strisprint((char *)zlc->l_array.la_array))
6183f9d6ad7SLin Ling 				mdb_printf(" \"%s\"", zlc->l_array.la_array);
6193f9d6ad7SLin Ling 			mdb_printf("\n");
6203f1f8012SMatthew Ahrens 			if (verbose) {
6213f1f8012SMatthew Ahrens 				int j;
6223f1f8012SMatthew Ahrens 				mdb_printf("        ");
6233f1f8012SMatthew Ahrens 				for (j = 0; j < ZAP_LEAF_ARRAY_BYTES; j++) {
6243f1f8012SMatthew Ahrens 					mdb_printf("%02x ",
6253f1f8012SMatthew Ahrens 					    zlc->l_array.la_array[j]);
6263f1f8012SMatthew Ahrens 				}
6273f1f8012SMatthew Ahrens 				mdb_printf("\n");
6283f1f8012SMatthew Ahrens 			}
6293f1f8012SMatthew Ahrens 			if (zlc->l_array.la_next != CHAIN_END) {
6303f1f8012SMatthew Ahrens 				mdb_printf("        lf_next = %u\n",
6313f1f8012SMatthew Ahrens 				    zlc->l_array.la_next);
6323f1f8012SMatthew Ahrens 			}
6333f1f8012SMatthew Ahrens 			break;
6343f1f8012SMatthew Ahrens 		default:
6353f1f8012SMatthew Ahrens 			mdb_printf("    %u: undefined type %u\n",
6363f1f8012SMatthew Ahrens 			    zlc->l_entry.le_type);
6373f1f8012SMatthew Ahrens 		}
6383f1f8012SMatthew Ahrens 	}
6393f1f8012SMatthew Ahrens 
6403f1f8012SMatthew Ahrens 	return (DCMD_OK);
6413f1f8012SMatthew Ahrens }
6423f1f8012SMatthew Ahrens 
643fa9e4066Sahrens typedef struct dbufs_data {
644fa9e4066Sahrens 	mdb_ctf_id_t id;
645fa9e4066Sahrens 	uint64_t objset;
646fa9e4066Sahrens 	uint64_t object;
647fa9e4066Sahrens 	uint64_t level;
648fa9e4066Sahrens 	uint64_t blkid;
649fa9e4066Sahrens 	char *osname;
650fa9e4066Sahrens } dbufs_data_t;
651fa9e4066Sahrens 
652fa9e4066Sahrens #define	DBUFS_UNSET	(0xbaddcafedeadbeefULL)
653fa9e4066Sahrens 
654fa9e4066Sahrens /* ARGSUSED */
655fa9e4066Sahrens static int
656fa9e4066Sahrens dbufs_cb(uintptr_t addr, const void *unknown, void *arg)
657fa9e4066Sahrens {
658fa9e4066Sahrens 	dbufs_data_t *data = arg;
659fa9e4066Sahrens 	uintptr_t objset;
660fa9e4066Sahrens 	dmu_buf_t db;
661fa9e4066Sahrens 	uint8_t level;
662fa9e4066Sahrens 	uint64_t blkid;
663fa9e4066Sahrens 	char osname[MAXNAMELEN];
664fa9e4066Sahrens 
665fa9e4066Sahrens 	if (GETMEMBID(addr, &data->id, db_objset, objset) ||
666fa9e4066Sahrens 	    GETMEMBID(addr, &data->id, db, db) ||
667fa9e4066Sahrens 	    GETMEMBID(addr, &data->id, db_level, level) ||
668fa9e4066Sahrens 	    GETMEMBID(addr, &data->id, db_blkid, blkid)) {
669fa9e4066Sahrens 		return (WALK_ERR);
670fa9e4066Sahrens 	}
671fa9e4066Sahrens 
672fa9e4066Sahrens 	if ((data->objset == DBUFS_UNSET || data->objset == objset) &&
673fa9e4066Sahrens 	    (data->osname == NULL || (objset_name(objset, osname) == 0 &&
674ccae0b50Seschrock 	    strcmp(data->osname, osname) == 0)) &&
675fa9e4066Sahrens 	    (data->object == DBUFS_UNSET || data->object == db.db_object) &&
676fa9e4066Sahrens 	    (data->level == DBUFS_UNSET || data->level == level) &&
677fa9e4066Sahrens 	    (data->blkid == DBUFS_UNSET || data->blkid == blkid)) {
678fa9e4066Sahrens 		mdb_printf("%#lr\n", addr);
679fa9e4066Sahrens 	}
680fa9e4066Sahrens 	return (WALK_NEXT);
681fa9e4066Sahrens }
682fa9e4066Sahrens 
683fa9e4066Sahrens /* ARGSUSED */
684fa9e4066Sahrens static int
685fa9e4066Sahrens dbufs(uintptr_t addr, uint_t flags, int argc, const mdb_arg_t *argv)
686fa9e4066Sahrens {
687fa9e4066Sahrens 	dbufs_data_t data;
688fa9e4066Sahrens 	char *object = NULL;
689fa9e4066Sahrens 	char *blkid = NULL;
690fa9e4066Sahrens 
691fa9e4066Sahrens 	data.objset = data.object = data.level = data.blkid = DBUFS_UNSET;
692fa9e4066Sahrens 	data.osname = NULL;
693fa9e4066Sahrens 
694fa9e4066Sahrens 	if (mdb_getopts(argc, argv,
695fa9e4066Sahrens 	    'O', MDB_OPT_UINT64, &data.objset,
696fa9e4066Sahrens 	    'n', MDB_OPT_STR, &data.osname,
697fa9e4066Sahrens 	    'o', MDB_OPT_STR, &object,
698fa9e4066Sahrens 	    'l', MDB_OPT_UINT64, &data.level,
699fa9e4066Sahrens 	    'b', MDB_OPT_STR, &blkid) != argc) {
700fa9e4066Sahrens 		return (DCMD_USAGE);
701fa9e4066Sahrens 	}
702fa9e4066Sahrens 
703fa9e4066Sahrens 	if (object) {
704fa9e4066Sahrens 		if (strcmp(object, "mdn") == 0) {
705fa9e4066Sahrens 			data.object = DMU_META_DNODE_OBJECT;
706fa9e4066Sahrens 		} else {
707fa9e4066Sahrens 			data.object = mdb_strtoull(object);
708fa9e4066Sahrens 		}
709fa9e4066Sahrens 	}
710fa9e4066Sahrens 
711fa9e4066Sahrens 	if (blkid) {
712fa9e4066Sahrens 		if (strcmp(blkid, "bonus") == 0) {
7130a586ceaSMark Shellenbaum 			data.blkid = DMU_BONUS_BLKID;
714fa9e4066Sahrens 		} else {
715fa9e4066Sahrens 			data.blkid = mdb_strtoull(blkid);
716fa9e4066Sahrens 		}
717fa9e4066Sahrens 	}
718fa9e4066Sahrens 
71928e4da25SMatthew Ahrens 	if (mdb_ctf_lookup_by_name(ZFS_STRUCT "dmu_buf_impl", &data.id) == -1) {
720fa9e4066Sahrens 		mdb_warn("couldn't find struct dmu_buf_impl_t");
721fa9e4066Sahrens 		return (DCMD_ERR);
722fa9e4066Sahrens 	}
723fa9e4066Sahrens 
7245f5f7a6fSahrens 	if (mdb_walk("dmu_buf_impl_t", dbufs_cb, &data) != 0) {
725fa9e4066Sahrens 		mdb_warn("can't walk dbufs");
726fa9e4066Sahrens 		return (DCMD_ERR);
727fa9e4066Sahrens 	}
728fa9e4066Sahrens 
729fa9e4066Sahrens 	return (DCMD_OK);
730fa9e4066Sahrens }
731fa9e4066Sahrens 
732fa9e4066Sahrens typedef struct abuf_find_data {
733fa9e4066Sahrens 	dva_t dva;
734fa9e4066Sahrens 	mdb_ctf_id_t id;
735fa9e4066Sahrens } abuf_find_data_t;
736fa9e4066Sahrens 
737fa9e4066Sahrens /* ARGSUSED */
738fa9e4066Sahrens static int
739fa9e4066Sahrens abuf_find_cb(uintptr_t addr, const void *unknown, void *arg)
740fa9e4066Sahrens {
741fa9e4066Sahrens 	abuf_find_data_t *data = arg;
742fa9e4066Sahrens 	dva_t dva;
743fa9e4066Sahrens 
744fa9e4066Sahrens 	if (GETMEMBID(addr, &data->id, b_dva, dva)) {
745fa9e4066Sahrens 		return (WALK_ERR);
746fa9e4066Sahrens 	}
747fa9e4066Sahrens 
748fa9e4066Sahrens 	if (dva.dva_word[0] == data->dva.dva_word[0] &&
749fa9e4066Sahrens 	    dva.dva_word[1] == data->dva.dva_word[1]) {
750fa9e4066Sahrens 		mdb_printf("%#lr\n", addr);
751fa9e4066Sahrens 	}
752fa9e4066Sahrens 	return (WALK_NEXT);
753fa9e4066Sahrens }
754fa9e4066Sahrens 
755fa9e4066Sahrens /* ARGSUSED */
756fa9e4066Sahrens static int
757fa9e4066Sahrens abuf_find(uintptr_t addr, uint_t flags, int argc, const mdb_arg_t *argv)
758fa9e4066Sahrens {
759fa9e4066Sahrens 	abuf_find_data_t data;
760fa9e4066Sahrens 	GElf_Sym sym;
761fa9e4066Sahrens 	int i;
762fa9e4066Sahrens 	const char *syms[] = {
763a2eea2e1Sahrens 		"ARC_mru",
764a2eea2e1Sahrens 		"ARC_mru_ghost",
765a2eea2e1Sahrens 		"ARC_mfu",
766a2eea2e1Sahrens 		"ARC_mfu_ghost",
767fa9e4066Sahrens 	};
768fa9e4066Sahrens 
769fa9e4066Sahrens 	if (argc != 2)
770fa9e4066Sahrens 		return (DCMD_USAGE);
771fa9e4066Sahrens 
772fa9e4066Sahrens 	for (i = 0; i < 2; i ++) {
773fa9e4066Sahrens 		switch (argv[i].a_type) {
774fa9e4066Sahrens 		case MDB_TYPE_STRING:
775fa9e4066Sahrens 			data.dva.dva_word[i] = mdb_strtoull(argv[i].a_un.a_str);
776fa9e4066Sahrens 			break;
777fa9e4066Sahrens 		case MDB_TYPE_IMMEDIATE:
778fa9e4066Sahrens 			data.dva.dva_word[i] = argv[i].a_un.a_val;
779fa9e4066Sahrens 			break;
780fa9e4066Sahrens 		default:
781fa9e4066Sahrens 			return (DCMD_USAGE);
782fa9e4066Sahrens 		}
783fa9e4066Sahrens 	}
784fa9e4066Sahrens 
78528e4da25SMatthew Ahrens 	if (mdb_ctf_lookup_by_name(ZFS_STRUCT "arc_buf_hdr", &data.id) == -1) {
786fa9e4066Sahrens 		mdb_warn("couldn't find struct arc_buf_hdr");
787fa9e4066Sahrens 		return (DCMD_ERR);
788fa9e4066Sahrens 	}
789fa9e4066Sahrens 
790fa9e4066Sahrens 	for (i = 0; i < sizeof (syms) / sizeof (syms[0]); i++) {
79122ce0148SMatthew Ahrens 		if (mdb_lookup_by_obj(ZFS_OBJ_NAME, syms[i], &sym)) {
792fa9e4066Sahrens 			mdb_warn("can't find symbol %s", syms[i]);
793fa9e4066Sahrens 			return (DCMD_ERR);
794fa9e4066Sahrens 		}
795fa9e4066Sahrens 
796fa9e4066Sahrens 		if (mdb_pwalk("list", abuf_find_cb, &data, sym.st_value) != 0) {
797fa9e4066Sahrens 			mdb_warn("can't walk %s", syms[i]);
798fa9e4066Sahrens 			return (DCMD_ERR);
799fa9e4066Sahrens 		}
800fa9e4066Sahrens 	}
801fa9e4066Sahrens 
802fa9e4066Sahrens 	return (DCMD_OK);
803fa9e4066Sahrens }
804fa9e4066Sahrens 
80528e4da25SMatthew Ahrens 
80628e4da25SMatthew Ahrens typedef struct dbgmsg_arg {
80728e4da25SMatthew Ahrens 	boolean_t da_verbose;
80828e4da25SMatthew Ahrens 	boolean_t da_address;
80928e4da25SMatthew Ahrens } dbgmsg_arg_t;
81028e4da25SMatthew Ahrens 
8113f9d6ad7SLin Ling /* ARGSUSED */
8123f9d6ad7SLin Ling static int
8133f9d6ad7SLin Ling dbgmsg_cb(uintptr_t addr, const void *unknown, void *arg)
8143f9d6ad7SLin Ling {
8153f9d6ad7SLin Ling 	static mdb_ctf_id_t id;
8163f9d6ad7SLin Ling 	static boolean_t gotid;
8173f9d6ad7SLin Ling 	static ulong_t off;
8183f9d6ad7SLin Ling 
81928e4da25SMatthew Ahrens 	dbgmsg_arg_t *da = arg;
8203f9d6ad7SLin Ling 	time_t timestamp;
8213f9d6ad7SLin Ling 	char buf[1024];
8223f9d6ad7SLin Ling 
8233f9d6ad7SLin Ling 	if (!gotid) {
82428e4da25SMatthew Ahrens 		if (mdb_ctf_lookup_by_name(ZFS_STRUCT "zfs_dbgmsg", &id) ==
82528e4da25SMatthew Ahrens 		    -1) {
8263f9d6ad7SLin Ling 			mdb_warn("couldn't find struct zfs_dbgmsg");
8273f9d6ad7SLin Ling 			return (WALK_ERR);
8283f9d6ad7SLin Ling 		}
8293f9d6ad7SLin Ling 		gotid = TRUE;
8303f9d6ad7SLin Ling 		if (mdb_ctf_offsetof(id, "zdm_msg", &off) == -1) {
8313f9d6ad7SLin Ling 			mdb_warn("couldn't find zdm_msg");
8323f9d6ad7SLin Ling 			return (WALK_ERR);
8333f9d6ad7SLin Ling 		}
8343f9d6ad7SLin Ling 		off /= 8;
8353f9d6ad7SLin Ling 	}
8363f9d6ad7SLin Ling 
8373f9d6ad7SLin Ling 
8383f9d6ad7SLin Ling 	if (GETMEMBID(addr, &id, zdm_timestamp, timestamp)) {
8393f9d6ad7SLin Ling 		return (WALK_ERR);
8403f9d6ad7SLin Ling 	}
8413f9d6ad7SLin Ling 
8423f9d6ad7SLin Ling 	if (mdb_readstr(buf, sizeof (buf), addr + off) == -1) {
8433f9d6ad7SLin Ling 		mdb_warn("failed to read zdm_msg at %p\n", addr + off);
8443f9d6ad7SLin Ling 		return (DCMD_ERR);
8453f9d6ad7SLin Ling 	}
8463f9d6ad7SLin Ling 
84728e4da25SMatthew Ahrens 	if (da->da_address)
84828e4da25SMatthew Ahrens 		mdb_printf("%p ", addr);
84928e4da25SMatthew Ahrens 	if (da->da_verbose)
8503f9d6ad7SLin Ling 		mdb_printf("%Y ", timestamp);
8513f9d6ad7SLin Ling 
8523f9d6ad7SLin Ling 	mdb_printf("%s\n", buf);
8533f9d6ad7SLin Ling 
85428e4da25SMatthew Ahrens 	if (da->da_verbose)
8553f9d6ad7SLin Ling 		(void) mdb_call_dcmd("whatis", addr, DCMD_ADDRSPEC, 0, NULL);
8563f9d6ad7SLin Ling 
8573f9d6ad7SLin Ling 	return (WALK_NEXT);
8583f9d6ad7SLin Ling }
8593f9d6ad7SLin Ling 
8603f9d6ad7SLin Ling /* ARGSUSED */
8613f9d6ad7SLin Ling static int
8623f9d6ad7SLin Ling dbgmsg(uintptr_t addr, uint_t flags, int argc, const mdb_arg_t *argv)
8633f9d6ad7SLin Ling {
8643f9d6ad7SLin Ling 	GElf_Sym sym;
86528e4da25SMatthew Ahrens 	dbgmsg_arg_t da = { 0 };
8663f9d6ad7SLin Ling 
8673f9d6ad7SLin Ling 	if (mdb_getopts(argc, argv,
86828e4da25SMatthew Ahrens 	    'v', MDB_OPT_SETBITS, B_TRUE, &da.da_verbose,
86928e4da25SMatthew Ahrens 	    'a', MDB_OPT_SETBITS, B_TRUE, &da.da_address,
8703f9d6ad7SLin Ling 	    NULL) != argc)
8713f9d6ad7SLin Ling 		return (DCMD_USAGE);
8723f9d6ad7SLin Ling 
8733b2aab18SMatthew Ahrens 	if (mdb_lookup_by_obj(ZFS_OBJ_NAME, "zfs_dbgmsgs", &sym)) {
8743f9d6ad7SLin Ling 		mdb_warn("can't find zfs_dbgmsgs");
8753f9d6ad7SLin Ling 		return (DCMD_ERR);
8763f9d6ad7SLin Ling 	}
8773f9d6ad7SLin Ling 
87828e4da25SMatthew Ahrens 	if (mdb_pwalk("list", dbgmsg_cb, &da, sym.st_value) != 0) {
8793f9d6ad7SLin Ling 		mdb_warn("can't walk zfs_dbgmsgs");
8803f9d6ad7SLin Ling 		return (DCMD_ERR);
8813f9d6ad7SLin Ling 	}
8823f9d6ad7SLin Ling 
8833f9d6ad7SLin Ling 	return (DCMD_OK);
8843f9d6ad7SLin Ling }
8853f9d6ad7SLin Ling 
88644cb6abcSbmc /*ARGSUSED*/
88744cb6abcSbmc static int
88844cb6abcSbmc arc_print(uintptr_t addr, uint_t flags, int argc, const mdb_arg_t *argv)
88944cb6abcSbmc {
89044cb6abcSbmc 	kstat_named_t *stats;
89144cb6abcSbmc 	GElf_Sym sym;
89291ebeef5Sahrens 	int nstats, i;
89344cb6abcSbmc 	uint_t opt_a = FALSE;
89491ebeef5Sahrens 	uint_t opt_b = FALSE;
89591ebeef5Sahrens 	uint_t shift = 0;
89691ebeef5Sahrens 	const char *suffix;
89744cb6abcSbmc 
89891ebeef5Sahrens 	static const char *bytestats[] = {
8999253d63dSGeorge Wilson 		"p", "c", "c_min", "c_max", "size", "duplicate_buffers_size",
90020128a08SGeorge Wilson 		"arc_meta_used", "arc_meta_limit", "arc_meta_max",
9019253d63dSGeorge Wilson 		NULL
90291ebeef5Sahrens 	};
90391ebeef5Sahrens 
90491ebeef5Sahrens 	static const char *extras[] = {
90591ebeef5Sahrens 		"arc_no_grow", "arc_tempreserve",
90691ebeef5Sahrens 		NULL
90744cb6abcSbmc 	};
90844cb6abcSbmc 
90922ce0148SMatthew Ahrens 	if (mdb_lookup_by_obj(ZFS_OBJ_NAME, "arc_stats", &sym) == -1) {
91044cb6abcSbmc 		mdb_warn("failed to find 'arc_stats'");
91144cb6abcSbmc 		return (DCMD_ERR);
91244cb6abcSbmc 	}
91344cb6abcSbmc 
91444cb6abcSbmc 	stats = mdb_zalloc(sym.st_size, UM_SLEEP | UM_GC);
91544cb6abcSbmc 
91644cb6abcSbmc 	if (mdb_vread(stats, sym.st_size, sym.st_value) == -1) {
91744cb6abcSbmc 		mdb_warn("couldn't read 'arc_stats' at %p", sym.st_value);
91844cb6abcSbmc 		return (DCMD_ERR);
91944cb6abcSbmc 	}
92044cb6abcSbmc 
92144cb6abcSbmc 	nstats = sym.st_size / sizeof (kstat_named_t);
92244cb6abcSbmc 
92391ebeef5Sahrens 	/* NB: -a / opt_a are ignored for backwards compatability */
92491ebeef5Sahrens 	if (mdb_getopts(argc, argv,
92591ebeef5Sahrens 	    'a', MDB_OPT_SETBITS, TRUE, &opt_a,
92691ebeef5Sahrens 	    'b', MDB_OPT_SETBITS, TRUE, &opt_b,
92791ebeef5Sahrens 	    'k', MDB_OPT_SETBITS, 10, &shift,
92891ebeef5Sahrens 	    'm', MDB_OPT_SETBITS, 20, &shift,
92991ebeef5Sahrens 	    'g', MDB_OPT_SETBITS, 30, &shift,
93091ebeef5Sahrens 	    NULL) != argc)
93144cb6abcSbmc 		return (DCMD_USAGE);
93244cb6abcSbmc 
93391ebeef5Sahrens 	if (!opt_b && !shift)
93491ebeef5Sahrens 		shift = 20;
93591ebeef5Sahrens 
93691ebeef5Sahrens 	switch (shift) {
93791ebeef5Sahrens 	case 0:
93891ebeef5Sahrens 		suffix = "B";
93991ebeef5Sahrens 		break;
94091ebeef5Sahrens 	case 10:
94191ebeef5Sahrens 		suffix = "KB";
94291ebeef5Sahrens 		break;
94391ebeef5Sahrens 	case 20:
94491ebeef5Sahrens 		suffix = "MB";
94591ebeef5Sahrens 		break;
94691ebeef5Sahrens 	case 30:
94791ebeef5Sahrens 		suffix = "GB";
94891ebeef5Sahrens 		break;
94991ebeef5Sahrens 	default:
95091ebeef5Sahrens 		suffix = "XX";
95191ebeef5Sahrens 	}
95291ebeef5Sahrens 
95391ebeef5Sahrens 	for (i = 0; i < nstats; i++) {
95491ebeef5Sahrens 		int j;
95591ebeef5Sahrens 		boolean_t bytes = B_FALSE;
95691ebeef5Sahrens 
95791ebeef5Sahrens 		for (j = 0; bytestats[j]; j++) {
95891ebeef5Sahrens 			if (strcmp(stats[i].name, bytestats[j]) == 0) {
95991ebeef5Sahrens 				bytes = B_TRUE;
96091ebeef5Sahrens 				break;
96191ebeef5Sahrens 			}
96291ebeef5Sahrens 		}
96344cb6abcSbmc 
96491ebeef5Sahrens 		if (bytes) {
96591ebeef5Sahrens 			mdb_printf("%-25s = %9llu %s\n", stats[i].name,
96691ebeef5Sahrens 			    stats[i].value.ui64 >> shift, suffix);
96791ebeef5Sahrens 		} else {
96891ebeef5Sahrens 			mdb_printf("%-25s = %9llu\n", stats[i].name,
96944cb6abcSbmc 			    stats[i].value.ui64);
97044cb6abcSbmc 		}
97144cb6abcSbmc 	}
97244cb6abcSbmc 
97391ebeef5Sahrens 	for (i = 0; extras[i]; i++) {
97491ebeef5Sahrens 		uint64_t buf;
97544cb6abcSbmc 
97622ce0148SMatthew Ahrens 		if (mdb_lookup_by_obj(ZFS_OBJ_NAME, extras[i], &sym) == -1) {
97791ebeef5Sahrens 			mdb_warn("failed to find '%s'", extras[i]);
97891ebeef5Sahrens 			return (DCMD_ERR);
97944cb6abcSbmc 		}
98044cb6abcSbmc 
98191ebeef5Sahrens 		if (sym.st_size != sizeof (uint64_t) &&
98291ebeef5Sahrens 		    sym.st_size != sizeof (uint32_t)) {
98391ebeef5Sahrens 			mdb_warn("expected scalar for variable '%s'\n",
98491ebeef5Sahrens 			    extras[i]);
98591ebeef5Sahrens 			return (DCMD_ERR);
98644cb6abcSbmc 		}
98744cb6abcSbmc 
98891ebeef5Sahrens 		if (mdb_vread(&buf, sym.st_size, sym.st_value) == -1) {
98991ebeef5Sahrens 			mdb_warn("couldn't read '%s'", extras[i]);
99091ebeef5Sahrens 			return (DCMD_ERR);
99144cb6abcSbmc 		}
99244cb6abcSbmc 
99391ebeef5Sahrens 		mdb_printf("%-25s = ", extras[i]);
99491ebeef5Sahrens 
99591ebeef5Sahrens 		/* NB: all the 64-bit extras happen to be byte counts */
99691ebeef5Sahrens 		if (sym.st_size == sizeof (uint64_t))
99791ebeef5Sahrens 			mdb_printf("%9llu %s\n", buf >> shift, suffix);
99844cb6abcSbmc 
99991ebeef5Sahrens 		if (sym.st_size == sizeof (uint32_t))
100091ebeef5Sahrens 			mdb_printf("%9d\n", *((uint32_t *)&buf));
100191ebeef5Sahrens 	}
100244cb6abcSbmc 	return (DCMD_OK);
100344cb6abcSbmc }
100444cb6abcSbmc 
100522ce0148SMatthew Ahrens typedef struct mdb_spa_print {
100622ce0148SMatthew Ahrens 	pool_state_t spa_state;
100722ce0148SMatthew Ahrens 	char spa_name[MAXNAMELEN];
100822ce0148SMatthew Ahrens } mdb_spa_print_t;
100922ce0148SMatthew Ahrens 
1010fa9e4066Sahrens /*
1011fa9e4066Sahrens  * ::spa
1012fa9e4066Sahrens  *
101322ce0148SMatthew Ahrens  *	-c	Print configuration information as well
101422ce0148SMatthew Ahrens  *	-v	Print vdev state
101522ce0148SMatthew Ahrens  *	-e	Print vdev error stats
1016fa9e4066Sahrens  *
1017fa9e4066Sahrens  * Print a summarized spa_t.  When given no arguments, prints out a table of all
1018fa9e4066Sahrens  * active pools on the system.
1019fa9e4066Sahrens  */
1020fa9e4066Sahrens /* ARGSUSED */
1021fa9e4066Sahrens static int
1022fa9e4066Sahrens spa_print(uintptr_t addr, uint_t flags, int argc, const mdb_arg_t *argv)
1023fa9e4066Sahrens {
1024fa9e4066Sahrens 	const char *statetab[] = { "ACTIVE", "EXPORTED", "DESTROYED",
1025e14bb325SJeff Bonwick 		"SPARE", "L2CACHE", "UNINIT", "UNAVAIL", "POTENTIAL" };
1026fa9e4066Sahrens 	const char *state;
1027fa9e4066Sahrens 	int config = FALSE;
1028fa9e4066Sahrens 	int vdevs = FALSE;
1029fa9e4066Sahrens 	int errors = FALSE;
1030fa9e4066Sahrens 
1031fa9e4066Sahrens 	if (mdb_getopts(argc, argv,
1032fa9e4066Sahrens 	    'c', MDB_OPT_SETBITS, TRUE, &config,
1033fa9e4066Sahrens 	    'v', MDB_OPT_SETBITS, TRUE, &vdevs,
1034fa9e4066Sahrens 	    'e', MDB_OPT_SETBITS, TRUE, &errors,
1035fa9e4066Sahrens 	    NULL) != argc)
1036fa9e4066Sahrens 		return (DCMD_USAGE);
1037fa9e4066Sahrens 
1038fa9e4066Sahrens 	if (!(flags & DCMD_ADDRSPEC)) {
1039fa9e4066Sahrens 		if (mdb_walk_dcmd("spa", "spa", argc, argv) == -1) {
1040fa9e4066Sahrens 			mdb_warn("can't walk spa");
1041fa9e4066Sahrens 			return (DCMD_ERR);
1042fa9e4066Sahrens 		}
1043fa9e4066Sahrens 
1044fa9e4066Sahrens 		return (DCMD_OK);
1045fa9e4066Sahrens 	}
1046fa9e4066Sahrens 
1047fa9e4066Sahrens 	if (flags & DCMD_PIPE_OUT) {
1048fa9e4066Sahrens 		mdb_printf("%#lr\n", addr);
1049fa9e4066Sahrens 		return (DCMD_OK);
1050fa9e4066Sahrens 	}
1051fa9e4066Sahrens 
1052fa9e4066Sahrens 	if (DCMD_HDRSPEC(flags))
1053fa9e4066Sahrens 		mdb_printf("%<u>%-?s %9s %-*s%</u>\n", "ADDR", "STATE",
1054fa9e4066Sahrens 		    sizeof (uintptr_t) == 4 ? 60 : 52, "NAME");
1055fa9e4066Sahrens 
105622ce0148SMatthew Ahrens 	mdb_spa_print_t spa;
105722ce0148SMatthew Ahrens 	if (mdb_ctf_vread(&spa, "spa_t", "mdb_spa_print_t", addr, 0) == -1)
1058fa9e4066Sahrens 		return (DCMD_ERR);
1059fa9e4066Sahrens 
1060fa9e4066Sahrens 	if (spa.spa_state < 0 || spa.spa_state > POOL_STATE_UNAVAIL)
1061ea8dc4b6Seschrock 		state = "UNKNOWN";
1062fa9e4066Sahrens 	else
1063fa9e4066Sahrens 		state = statetab[spa.spa_state];
1064fa9e4066Sahrens 
1065e14bb325SJeff Bonwick 	mdb_printf("%0?p %9s %s\n", addr, state, spa.spa_name);
1066fa9e4066Sahrens 
1067fa9e4066Sahrens 	if (config) {
1068fa9e4066Sahrens 		mdb_printf("\n");
1069fa9e4066Sahrens 		mdb_inc_indent(4);
1070fa9e4066Sahrens 		if (mdb_call_dcmd("spa_config", addr, flags, 0,
1071fa9e4066Sahrens 		    NULL) != DCMD_OK)
1072fa9e4066Sahrens 			return (DCMD_ERR);
1073fa9e4066Sahrens 		mdb_dec_indent(4);
1074fa9e4066Sahrens 	}
1075fa9e4066Sahrens 
1076fa9e4066Sahrens 	if (vdevs || errors) {
1077fa9e4066Sahrens 		mdb_arg_t v;
1078fa9e4066Sahrens 
1079fa9e4066Sahrens 		v.a_type = MDB_TYPE_STRING;
1080fa9e4066Sahrens 		v.a_un.a_str = "-e";
1081fa9e4066Sahrens 
1082fa9e4066Sahrens 		mdb_printf("\n");
1083fa9e4066Sahrens 		mdb_inc_indent(4);
1084fa9e4066Sahrens 		if (mdb_call_dcmd("spa_vdevs", addr, flags, errors ? 1 : 0,
1085fa9e4066Sahrens 		    &v) != DCMD_OK)
1086fa9e4066Sahrens 			return (DCMD_ERR);
1087fa9e4066Sahrens 		mdb_dec_indent(4);
1088fa9e4066Sahrens 	}
1089fa9e4066Sahrens 
1090fa9e4066Sahrens 	return (DCMD_OK);
1091fa9e4066Sahrens }
1092fa9e4066Sahrens 
109328e4da25SMatthew Ahrens typedef struct mdb_spa_config_spa {
1094d5ee8a13SMatthew Ahrens 	uintptr_t spa_config;
109528e4da25SMatthew Ahrens } mdb_spa_config_spa_t;
109628e4da25SMatthew Ahrens 
1097fa9e4066Sahrens /*
1098fa9e4066Sahrens  * ::spa_config
1099fa9e4066Sahrens  *
1100fa9e4066Sahrens  * Given a spa_t, print the configuration information stored in spa_config.
1101fa9e4066Sahrens  * Since it's just an nvlist, format it as an indented list of name=value pairs.
1102fa9e4066Sahrens  * We simply read the value of spa_config and pass off to ::nvlist.
1103fa9e4066Sahrens  */
1104fa9e4066Sahrens /* ARGSUSED */
1105fa9e4066Sahrens static int
1106fa9e4066Sahrens spa_print_config(uintptr_t addr, uint_t flags, int argc, const mdb_arg_t *argv)
1107fa9e4066Sahrens {
110828e4da25SMatthew Ahrens 	mdb_spa_config_spa_t spa;
1109fa9e4066Sahrens 
1110fa9e4066Sahrens 	if (argc != 0 || !(flags & DCMD_ADDRSPEC))
1111fa9e4066Sahrens 		return (DCMD_USAGE);
1112fa9e4066Sahrens 
111328e4da25SMatthew Ahrens 	if (mdb_ctf_vread(&spa, ZFS_STRUCT "spa", "mdb_spa_config_spa_t",
111428e4da25SMatthew Ahrens 	    addr, 0) == -1)
1115fa9e4066Sahrens 		return (DCMD_ERR);
1116fa9e4066Sahrens 
1117d5ee8a13SMatthew Ahrens 	if (spa.spa_config == 0) {
1118fa9e4066Sahrens 		mdb_printf("(none)\n");
1119fa9e4066Sahrens 		return (DCMD_OK);
1120fa9e4066Sahrens 	}
1121fa9e4066Sahrens 
1122d5ee8a13SMatthew Ahrens 	return (mdb_call_dcmd("nvlist", spa.spa_config, flags,
1123fa9e4066Sahrens 	    0, NULL));
1124fa9e4066Sahrens }
1125fa9e4066Sahrens 
1126fa9e4066Sahrens /*
1127fa9e4066Sahrens  * ::vdev
1128fa9e4066Sahrens  *
1129fa9e4066Sahrens  * Print out a summarized vdev_t, in the following form:
1130fa9e4066Sahrens  *
1131fa9e4066Sahrens  * ADDR             STATE	AUX            DESC
1132fa9e4066Sahrens  * fffffffbcde23df0 HEALTHY	-              /dev/dsk/c0t0d0
1133fa9e4066Sahrens  *
1134fa9e4066Sahrens  * If '-r' is specified, recursively visit all children.
1135fa9e4066Sahrens  *
1136fa9e4066Sahrens  * With '-e', the statistics associated with the vdev are printed as well.
1137fa9e4066Sahrens  */
1138fa9e4066Sahrens static int
1139614409b5Sahrens do_print_vdev(uintptr_t addr, int flags, int depth, int stats,
1140fa9e4066Sahrens     int recursive)
1141fa9e4066Sahrens {
1142fa9e4066Sahrens 	vdev_t vdev;
1143fa9e4066Sahrens 	char desc[MAXNAMELEN];
1144fa9e4066Sahrens 	int c, children;
1145fa9e4066Sahrens 	uintptr_t *child;
1146fa9e4066Sahrens 	const char *state, *aux;
1147fa9e4066Sahrens 
1148fa9e4066Sahrens 	if (mdb_vread(&vdev, sizeof (vdev), (uintptr_t)addr) == -1) {
1149fa9e4066Sahrens 		mdb_warn("failed to read vdev_t at %p\n", (uintptr_t)addr);
1150fa9e4066Sahrens 		return (DCMD_ERR);
1151fa9e4066Sahrens 	}
1152fa9e4066Sahrens 
1153fa9e4066Sahrens 	if (flags & DCMD_PIPE_OUT) {
1154b4952e17SGeorge Wilson 		mdb_printf("%#lr\n", addr);
1155fa9e4066Sahrens 	} else {
1156fa9e4066Sahrens 		if (vdev.vdev_path != NULL) {
1157fa9e4066Sahrens 			if (mdb_readstr(desc, sizeof (desc),
1158fa9e4066Sahrens 			    (uintptr_t)vdev.vdev_path) == -1) {
1159fa9e4066Sahrens 				mdb_warn("failed to read vdev_path at %p\n",
1160fa9e4066Sahrens 				    vdev.vdev_path);
1161fa9e4066Sahrens 				return (DCMD_ERR);
1162fa9e4066Sahrens 			}
1163fa9e4066Sahrens 		} else if (vdev.vdev_ops != NULL) {
1164fa9e4066Sahrens 			vdev_ops_t ops;
1165fa9e4066Sahrens 			if (mdb_vread(&ops, sizeof (ops),
1166fa9e4066Sahrens 			    (uintptr_t)vdev.vdev_ops) == -1) {
1167fa9e4066Sahrens 				mdb_warn("failed to read vdev_ops at %p\n",
1168fa9e4066Sahrens 				    vdev.vdev_ops);
1169fa9e4066Sahrens 				return (DCMD_ERR);
1170fa9e4066Sahrens 			}
1171fa9e4066Sahrens 			(void) strcpy(desc, ops.vdev_op_type);
1172fa9e4066Sahrens 		} else {
1173fa9e4066Sahrens 			(void) strcpy(desc, "<unknown>");
1174fa9e4066Sahrens 		}
1175fa9e4066Sahrens 
1176fa9e4066Sahrens 		if (depth == 0 && DCMD_HDRSPEC(flags))
1177fa9e4066Sahrens 			mdb_printf("%<u>%-?s %-9s %-12s %-*s%</u>\n",
1178fa9e4066Sahrens 			    "ADDR", "STATE", "AUX",
1179fa9e4066Sahrens 			    sizeof (uintptr_t) == 4 ? 43 : 35,
1180fa9e4066Sahrens 			    "DESCRIPTION");
1181fa9e4066Sahrens 
1182fa9e4066Sahrens 		mdb_printf("%0?p ", addr);
1183fa9e4066Sahrens 
1184fa9e4066Sahrens 		switch (vdev.vdev_state) {
1185fa9e4066Sahrens 		case VDEV_STATE_CLOSED:
1186ccae0b50Seschrock 			state = "CLOSED";
1187ccae0b50Seschrock 			break;
1188fa9e4066Sahrens 		case VDEV_STATE_OFFLINE:
1189ccae0b50Seschrock 			state = "OFFLINE";
1190ccae0b50Seschrock 			break;
1191fa9e4066Sahrens 		case VDEV_STATE_CANT_OPEN:
1192ccae0b50Seschrock 			state = "CANT_OPEN";
1193ccae0b50Seschrock 			break;
1194fa9e4066Sahrens 		case VDEV_STATE_DEGRADED:
1195ccae0b50Seschrock 			state = "DEGRADED";
1196ccae0b50Seschrock 			break;
1197fa9e4066Sahrens 		case VDEV_STATE_HEALTHY:
1198ccae0b50Seschrock 			state = "HEALTHY";
1199ccae0b50Seschrock 			break;
12003d7072f8Seschrock 		case VDEV_STATE_REMOVED:
12013d7072f8Seschrock 			state = "REMOVED";
12023d7072f8Seschrock 			break;
12033d7072f8Seschrock 		case VDEV_STATE_FAULTED:
12043d7072f8Seschrock 			state = "FAULTED";
12053d7072f8Seschrock 			break;
1206fa9e4066Sahrens 		default:
1207ccae0b50Seschrock 			state = "UNKNOWN";
1208ccae0b50Seschrock 			break;
1209fa9e4066Sahrens 		}
1210fa9e4066Sahrens 
1211fa9e4066Sahrens 		switch (vdev.vdev_stat.vs_aux) {
1212fa9e4066Sahrens 		case VDEV_AUX_NONE:
1213fa9e4066Sahrens 			aux = "-";
1214fa9e4066Sahrens 			break;
1215fa9e4066Sahrens 		case VDEV_AUX_OPEN_FAILED:
1216fa9e4066Sahrens 			aux = "OPEN_FAILED";
1217fa9e4066Sahrens 			break;
1218fa9e4066Sahrens 		case VDEV_AUX_CORRUPT_DATA:
1219fa9e4066Sahrens 			aux = "CORRUPT_DATA";
1220fa9e4066Sahrens 			break;
1221fa9e4066Sahrens 		case VDEV_AUX_NO_REPLICAS:
1222fa9e4066Sahrens 			aux = "NO_REPLICAS";
1223fa9e4066Sahrens 			break;
1224fa9e4066Sahrens 		case VDEV_AUX_BAD_GUID_SUM:
1225fa9e4066Sahrens 			aux = "BAD_GUID_SUM";
1226fa9e4066Sahrens 			break;
1227fa9e4066Sahrens 		case VDEV_AUX_TOO_SMALL:
1228fa9e4066Sahrens 			aux = "TOO_SMALL";
1229fa9e4066Sahrens 			break;
1230fa9e4066Sahrens 		case VDEV_AUX_BAD_LABEL:
1231fa9e4066Sahrens 			aux = "BAD_LABEL";
1232fa9e4066Sahrens 			break;
1233b87f3af3Sperrin 		case VDEV_AUX_VERSION_NEWER:
1234b87f3af3Sperrin 			aux = "VERS_NEWER";
1235b87f3af3Sperrin 			break;
1236b87f3af3Sperrin 		case VDEV_AUX_VERSION_OLDER:
1237b87f3af3Sperrin 			aux = "VERS_OLDER";
1238b87f3af3Sperrin 			break;
1239ad135b5dSChristopher Siden 		case VDEV_AUX_UNSUP_FEAT:
1240ad135b5dSChristopher Siden 			aux = "UNSUP_FEAT";
1241ad135b5dSChristopher Siden 			break;
1242b87f3af3Sperrin 		case VDEV_AUX_SPARED:
1243b87f3af3Sperrin 			aux = "SPARED";
1244b87f3af3Sperrin 			break;
1245b87f3af3Sperrin 		case VDEV_AUX_ERR_EXCEEDED:
1246b87f3af3Sperrin 			aux = "ERR_EXCEEDED";
1247b87f3af3Sperrin 			break;
1248b87f3af3Sperrin 		case VDEV_AUX_IO_FAILURE:
1249b87f3af3Sperrin 			aux = "IO_FAILURE";
1250b87f3af3Sperrin 			break;
1251b87f3af3Sperrin 		case VDEV_AUX_BAD_LOG:
1252b87f3af3Sperrin 			aux = "BAD_LOG";
1253b87f3af3Sperrin 			break;
12541195e687SMark J Musante 		case VDEV_AUX_EXTERNAL:
12551195e687SMark J Musante 			aux = "EXTERNAL";
12561195e687SMark J Musante 			break;
12571195e687SMark J Musante 		case VDEV_AUX_SPLIT_POOL:
12581195e687SMark J Musante 			aux = "SPLIT_POOL";
12591195e687SMark J Musante 			break;
1260fa9e4066Sahrens 		default:
1261fa9e4066Sahrens 			aux = "UNKNOWN";
1262fa9e4066Sahrens 			break;
1263fa9e4066Sahrens 		}
1264fa9e4066Sahrens 
1265fa9e4066Sahrens 		mdb_printf("%-9s %-12s %*s%s\n", state, aux, depth, "", desc);
1266fa9e4066Sahrens 
1267fa9e4066Sahrens 		if (stats) {
1268fa9e4066Sahrens 			vdev_stat_t *vs = &vdev.vdev_stat;
1269fa9e4066Sahrens 			int i;
1270fa9e4066Sahrens 
1271fa9e4066Sahrens 			mdb_inc_indent(4);
1272fa9e4066Sahrens 			mdb_printf("\n");
1273fa9e4066Sahrens 			mdb_printf("%<u>       %12s %12s %12s %12s "
1274fa9e4066Sahrens 			    "%12s%</u>\n", "READ", "WRITE", "FREE", "CLAIM",
1275fa9e4066Sahrens 			    "IOCTL");
1276fa9e4066Sahrens 			mdb_printf("OPS     ");
1277fa9e4066Sahrens 			for (i = 1; i < ZIO_TYPES; i++)
1278fa9e4066Sahrens 				mdb_printf("%11#llx%s", vs->vs_ops[i],
1279fa9e4066Sahrens 				    i == ZIO_TYPES - 1 ? "" : "  ");
1280fa9e4066Sahrens 			mdb_printf("\n");
1281fa9e4066Sahrens 			mdb_printf("BYTES   ");
1282fa9e4066Sahrens 			for (i = 1; i < ZIO_TYPES; i++)
1283fa9e4066Sahrens 				mdb_printf("%11#llx%s", vs->vs_bytes[i],
1284fa9e4066Sahrens 				    i == ZIO_TYPES - 1 ? "" : "  ");
1285fa9e4066Sahrens 
1286fa9e4066Sahrens 
1287fa9e4066Sahrens 			mdb_printf("\n");
1288fa9e4066Sahrens 			mdb_printf("EREAD    %10#llx\n", vs->vs_read_errors);
1289fa9e4066Sahrens 			mdb_printf("EWRITE   %10#llx\n", vs->vs_write_errors);
1290fa9e4066Sahrens 			mdb_printf("ECKSUM   %10#llx\n",
1291fa9e4066Sahrens 			    vs->vs_checksum_errors);
1292fa9e4066Sahrens 			mdb_dec_indent(4);
1293fa9e4066Sahrens 		}
1294fa9e4066Sahrens 
1295614409b5Sahrens 		if (stats)
1296fa9e4066Sahrens 			mdb_printf("\n");
1297fa9e4066Sahrens 	}
1298fa9e4066Sahrens 
1299fa9e4066Sahrens 	children = vdev.vdev_children;
1300fa9e4066Sahrens 
1301fa9e4066Sahrens 	if (children == 0 || !recursive)
1302fa9e4066Sahrens 		return (DCMD_OK);
1303fa9e4066Sahrens 
1304fa9e4066Sahrens 	child = mdb_alloc(children * sizeof (void *), UM_SLEEP | UM_GC);
1305fa9e4066Sahrens 	if (mdb_vread(child, children * sizeof (void *),
1306fa9e4066Sahrens 	    (uintptr_t)vdev.vdev_child) == -1) {
1307fa9e4066Sahrens 		mdb_warn("failed to read vdev children at %p", vdev.vdev_child);
1308fa9e4066Sahrens 		return (DCMD_ERR);
1309fa9e4066Sahrens 	}
1310fa9e4066Sahrens 
1311fa9e4066Sahrens 	for (c = 0; c < children; c++) {
1312614409b5Sahrens 		if (do_print_vdev(child[c], flags, depth + 2, stats,
1313fa9e4066Sahrens 		    recursive))
1314fa9e4066Sahrens 			return (DCMD_ERR);
1315fa9e4066Sahrens 	}
1316fa9e4066Sahrens 
1317fa9e4066Sahrens 	return (DCMD_OK);
1318fa9e4066Sahrens }
1319fa9e4066Sahrens 
1320fa9e4066Sahrens static int
1321fa9e4066Sahrens vdev_print(uintptr_t addr, uint_t flags, int argc, const mdb_arg_t *argv)
1322fa9e4066Sahrens {
1323fa9e4066Sahrens 	int recursive = FALSE;
1324fa9e4066Sahrens 	int stats = FALSE;
1325c5904d13Seschrock 	uint64_t depth = 0;
1326fa9e4066Sahrens 
1327fa9e4066Sahrens 	if (mdb_getopts(argc, argv,
1328fa9e4066Sahrens 	    'r', MDB_OPT_SETBITS, TRUE, &recursive,
1329fa9e4066Sahrens 	    'e', MDB_OPT_SETBITS, TRUE, &stats,
1330c5904d13Seschrock 	    'd', MDB_OPT_UINT64, &depth,
1331fa9e4066Sahrens 	    NULL) != argc)
1332fa9e4066Sahrens 		return (DCMD_USAGE);
1333fa9e4066Sahrens 
1334fa9e4066Sahrens 	if (!(flags & DCMD_ADDRSPEC)) {
1335fa9e4066Sahrens 		mdb_warn("no vdev_t address given\n");
1336fa9e4066Sahrens 		return (DCMD_ERR);
1337fa9e4066Sahrens 	}
1338fa9e4066Sahrens 
1339c5904d13Seschrock 	return (do_print_vdev(addr, flags, (int)depth, stats, recursive));
1340fa9e4066Sahrens }
1341fa9e4066Sahrens 
13425f5f7a6fSahrens typedef struct metaslab_walk_data {
13435f5f7a6fSahrens 	uint64_t mw_numvdevs;
13445f5f7a6fSahrens 	uintptr_t *mw_vdevs;
13455f5f7a6fSahrens 	int mw_curvdev;
13465f5f7a6fSahrens 	uint64_t mw_nummss;
13475f5f7a6fSahrens 	uintptr_t *mw_mss;
13485f5f7a6fSahrens 	int mw_curms;
13495f5f7a6fSahrens } metaslab_walk_data_t;
13505f5f7a6fSahrens 
13515f5f7a6fSahrens static int
13525f5f7a6fSahrens metaslab_walk_step(mdb_walk_state_t *wsp)
13535f5f7a6fSahrens {
13545f5f7a6fSahrens 	metaslab_walk_data_t *mw = wsp->walk_data;
13555f5f7a6fSahrens 	metaslab_t ms;
13565f5f7a6fSahrens 	uintptr_t msp;
13575f5f7a6fSahrens 
13585f5f7a6fSahrens 	if (mw->mw_curvdev >= mw->mw_numvdevs)
13595f5f7a6fSahrens 		return (WALK_DONE);
13605f5f7a6fSahrens 
13615f5f7a6fSahrens 	if (mw->mw_mss == NULL) {
13625f5f7a6fSahrens 		uintptr_t mssp;
13635f5f7a6fSahrens 		uintptr_t vdevp;
13645f5f7a6fSahrens 
13655f5f7a6fSahrens 		ASSERT(mw->mw_curms == 0);
13665f5f7a6fSahrens 		ASSERT(mw->mw_nummss == 0);
13675f5f7a6fSahrens 
13685f5f7a6fSahrens 		vdevp = mw->mw_vdevs[mw->mw_curvdev];
136928e4da25SMatthew Ahrens 		if (GETMEMB(vdevp, "vdev", vdev_ms, mssp) ||
137028e4da25SMatthew Ahrens 		    GETMEMB(vdevp, "vdev", vdev_ms_count, mw->mw_nummss)) {
13715f5f7a6fSahrens 			return (WALK_ERR);
13725f5f7a6fSahrens 		}
13735f5f7a6fSahrens 
13745f5f7a6fSahrens 		mw->mw_mss = mdb_alloc(mw->mw_nummss * sizeof (void*),
13755f5f7a6fSahrens 		    UM_SLEEP | UM_GC);
13765f5f7a6fSahrens 		if (mdb_vread(mw->mw_mss, mw->mw_nummss * sizeof (void*),
13775f5f7a6fSahrens 		    mssp) == -1) {
13785f5f7a6fSahrens 			mdb_warn("failed to read vdev_ms at %p", mssp);
13795f5f7a6fSahrens 			return (WALK_ERR);
13805f5f7a6fSahrens 		}
13815f5f7a6fSahrens 	}
13825f5f7a6fSahrens 
13835f5f7a6fSahrens 	if (mw->mw_curms >= mw->mw_nummss) {
13845f5f7a6fSahrens 		mw->mw_mss = NULL;
13855f5f7a6fSahrens 		mw->mw_curms = 0;
13865f5f7a6fSahrens 		mw->mw_nummss = 0;
13875f5f7a6fSahrens 		mw->mw_curvdev++;
13885f5f7a6fSahrens 		return (WALK_NEXT);
13895f5f7a6fSahrens 	}
13905f5f7a6fSahrens 
13915f5f7a6fSahrens 	msp = mw->mw_mss[mw->mw_curms];
13925f5f7a6fSahrens 	if (mdb_vread(&ms, sizeof (metaslab_t), msp) == -1) {
13935f5f7a6fSahrens 		mdb_warn("failed to read metaslab_t at %p", msp);
13945f5f7a6fSahrens 		return (WALK_ERR);
13955f5f7a6fSahrens 	}
13965f5f7a6fSahrens 
13975f5f7a6fSahrens 	mw->mw_curms++;
13985f5f7a6fSahrens 
13995f5f7a6fSahrens 	return (wsp->walk_callback(msp, &ms, wsp->walk_cbdata));
14005f5f7a6fSahrens }
14015f5f7a6fSahrens 
14025f5f7a6fSahrens /* ARGSUSED */
14035f5f7a6fSahrens static int
14045f5f7a6fSahrens metaslab_walk_init(mdb_walk_state_t *wsp)
14055f5f7a6fSahrens {
14065f5f7a6fSahrens 	metaslab_walk_data_t *mw;
14075f5f7a6fSahrens 	uintptr_t root_vdevp;
14085f5f7a6fSahrens 	uintptr_t childp;
14095f5f7a6fSahrens 
14105f5f7a6fSahrens 	if (wsp->walk_addr == NULL) {
14115f5f7a6fSahrens 		mdb_warn("must supply address of spa_t\n");
14125f5f7a6fSahrens 		return (WALK_ERR);
14135f5f7a6fSahrens 	}
14145f5f7a6fSahrens 
14155f5f7a6fSahrens 	mw = mdb_zalloc(sizeof (metaslab_walk_data_t), UM_SLEEP | UM_GC);
14165f5f7a6fSahrens 
141728e4da25SMatthew Ahrens 	if (GETMEMB(wsp->walk_addr, "spa", spa_root_vdev, root_vdevp) ||
141828e4da25SMatthew Ahrens 	    GETMEMB(root_vdevp, "vdev", vdev_children, mw->mw_numvdevs) ||
141928e4da25SMatthew Ahrens 	    GETMEMB(root_vdevp, "vdev", vdev_child, childp)) {
14205f5f7a6fSahrens 		return (DCMD_ERR);
14215f5f7a6fSahrens 	}
14225f5f7a6fSahrens 
14235f5f7a6fSahrens 	mw->mw_vdevs = mdb_alloc(mw->mw_numvdevs * sizeof (void *),
14245f5f7a6fSahrens 	    UM_SLEEP | UM_GC);
14255f5f7a6fSahrens 	if (mdb_vread(mw->mw_vdevs, mw->mw_numvdevs * sizeof (void *),
14265f5f7a6fSahrens 	    childp) == -1) {
14275f5f7a6fSahrens 		mdb_warn("failed to read root vdev children at %p", childp);
14285f5f7a6fSahrens 		return (DCMD_ERR);
14295f5f7a6fSahrens 	}
14305f5f7a6fSahrens 
14315f5f7a6fSahrens 	wsp->walk_data = mw;
14325f5f7a6fSahrens 
14335f5f7a6fSahrens 	return (WALK_NEXT);
14345f5f7a6fSahrens }
14355f5f7a6fSahrens 
1436fa9e4066Sahrens typedef struct mdb_spa {
1437fa9e4066Sahrens 	uintptr_t spa_dsl_pool;
1438fa9e4066Sahrens 	uintptr_t spa_root_vdev;
1439fa9e4066Sahrens } mdb_spa_t;
1440fa9e4066Sahrens 
1441fa9e4066Sahrens typedef struct mdb_dsl_dir {
1442fa9e4066Sahrens 	uintptr_t dd_phys;
1443fa9e4066Sahrens 	int64_t dd_space_towrite[TXG_SIZE];
1444fa9e4066Sahrens } mdb_dsl_dir_t;
1445fa9e4066Sahrens 
1446fa9e4066Sahrens typedef struct mdb_dsl_dir_phys {
1447fa9e4066Sahrens 	uint64_t dd_used_bytes;
1448fa9e4066Sahrens 	uint64_t dd_compressed_bytes;
1449fa9e4066Sahrens 	uint64_t dd_uncompressed_bytes;
1450fa9e4066Sahrens } mdb_dsl_dir_phys_t;
1451fa9e4066Sahrens 
1452fa9e4066Sahrens typedef struct mdb_vdev {
1453fa9e4066Sahrens 	uintptr_t vdev_parent;
1454fa9e4066Sahrens 	uintptr_t vdev_ms;
1455fa9e4066Sahrens 	uint64_t vdev_ms_count;
1456fa9e4066Sahrens 	vdev_stat_t vdev_stat;
1457fa9e4066Sahrens } mdb_vdev_t;
1458fa9e4066Sahrens 
14590713e232SGeorge Wilson typedef struct mdb_space_map_phys_t {
14600713e232SGeorge Wilson 	uint64_t smp_alloc;
14610713e232SGeorge Wilson } mdb_space_map_phys_t;
14620713e232SGeorge Wilson 
14630713e232SGeorge Wilson typedef struct mdb_space_map {
14640713e232SGeorge Wilson 	uint64_t sm_size;
14650713e232SGeorge Wilson 	uint64_t sm_alloc;
14660713e232SGeorge Wilson 	uintptr_t sm_phys;
14670713e232SGeorge Wilson } mdb_space_map_t;
14680713e232SGeorge Wilson 
14690713e232SGeorge Wilson typedef struct mdb_range_tree {
14700713e232SGeorge Wilson 	uint64_t rt_space;
14710713e232SGeorge Wilson } mdb_range_tree_t;
14720713e232SGeorge Wilson 
1473fa9e4066Sahrens typedef struct mdb_metaslab {
14740713e232SGeorge Wilson 	uintptr_t ms_alloctree[TXG_SIZE];
14750713e232SGeorge Wilson 	uintptr_t ms_freetree[TXG_SIZE];
14760713e232SGeorge Wilson 	uintptr_t ms_tree;
14770713e232SGeorge Wilson 	uintptr_t ms_sm;
1478fa9e4066Sahrens } mdb_metaslab_t;
1479fa9e4066Sahrens 
14805f5f7a6fSahrens typedef struct space_data {
14810713e232SGeorge Wilson 	uint64_t ms_alloctree[TXG_SIZE];
14820713e232SGeorge Wilson 	uint64_t ms_freetree[TXG_SIZE];
14830713e232SGeorge Wilson 	uint64_t ms_tree;
14845f5f7a6fSahrens 	uint64_t avail;
14855f5f7a6fSahrens 	uint64_t nowavail;
14865f5f7a6fSahrens } space_data_t;
14875f5f7a6fSahrens 
14885f5f7a6fSahrens /* ARGSUSED */
14895f5f7a6fSahrens static int
14905f5f7a6fSahrens space_cb(uintptr_t addr, const void *unknown, void *arg)
14915f5f7a6fSahrens {
14925f5f7a6fSahrens 	space_data_t *sd = arg;
14935f5f7a6fSahrens 	mdb_metaslab_t ms;
14940713e232SGeorge Wilson 	mdb_range_tree_t rt;
14950713e232SGeorge Wilson 	mdb_space_map_t sm;
14960713e232SGeorge Wilson 	mdb_space_map_phys_t smp = { 0 };
14970713e232SGeorge Wilson 	int i;
14980713e232SGeorge Wilson 
14990713e232SGeorge Wilson 	if (mdb_ctf_vread(&ms, "metaslab_t", "mdb_metaslab_t",
15000713e232SGeorge Wilson 	    addr, 0) == -1)
15010713e232SGeorge Wilson 		return (WALK_ERR);
15020713e232SGeorge Wilson 
15030713e232SGeorge Wilson 	for (i = 0; i < TXG_SIZE; i++) {
15040713e232SGeorge Wilson 
15050713e232SGeorge Wilson 		if (mdb_ctf_vread(&rt, "range_tree_t",
15060713e232SGeorge Wilson 		    "mdb_range_tree_t", ms.ms_alloctree[i], 0) == -1)
15070713e232SGeorge Wilson 		sd->ms_alloctree[i] += rt.rt_space;
15085f5f7a6fSahrens 
15090713e232SGeorge Wilson 		if (mdb_ctf_vread(&rt, "range_tree_t",
15100713e232SGeorge Wilson 		    "mdb_range_tree_t", ms.ms_freetree[i], 0) == -1)
15110713e232SGeorge Wilson 		sd->ms_freetree[i] += rt.rt_space;
15120713e232SGeorge Wilson 	}
15130713e232SGeorge Wilson 
15140713e232SGeorge Wilson 	if (mdb_ctf_vread(&rt, "range_tree_t",
15150713e232SGeorge Wilson 	    "mdb_range_tree_t", ms.ms_tree, 0) == -1 ||
15160713e232SGeorge Wilson 	    mdb_ctf_vread(&sm, "space_map_t",
15170713e232SGeorge Wilson 	    "mdb_space_map_t", ms.ms_sm, 0) == -1)
15185f5f7a6fSahrens 		return (WALK_ERR);
15190713e232SGeorge Wilson 
15200713e232SGeorge Wilson 	if (sm.sm_phys != NULL) {
15210713e232SGeorge Wilson 		(void) mdb_ctf_vread(&smp, "space_map_phys_t",
15220713e232SGeorge Wilson 		    "mdb_space_map_phys_t", sm.sm_phys, 0);
15235f5f7a6fSahrens 	}
15245f5f7a6fSahrens 
15250713e232SGeorge Wilson 	sd->ms_tree += rt.rt_space;
15260713e232SGeorge Wilson 	sd->avail += sm.sm_size - sm.sm_alloc;
15270713e232SGeorge Wilson 	sd->nowavail += sm.sm_size - smp.smp_alloc;
15285f5f7a6fSahrens 
15295f5f7a6fSahrens 	return (WALK_NEXT);
15305f5f7a6fSahrens }
15315f5f7a6fSahrens 
1532fa9e4066Sahrens /*
1533fa9e4066Sahrens  * ::spa_space [-b]
1534fa9e4066Sahrens  *
1535fa9e4066Sahrens  * Given a spa_t, print out it's on-disk space usage and in-core
1536fa9e4066Sahrens  * estimates of future usage.  If -b is given, print space in bytes.
1537fa9e4066Sahrens  * Otherwise print in megabytes.
1538fa9e4066Sahrens  */
1539fa9e4066Sahrens /* ARGSUSED */
1540fa9e4066Sahrens static int
1541fa9e4066Sahrens spa_space(uintptr_t addr, uint_t flags, int argc, const mdb_arg_t *argv)
1542fa9e4066Sahrens {
1543fa9e4066Sahrens 	mdb_spa_t spa;
1544fa9e4066Sahrens 	uintptr_t dp_root_dir;
1545fa9e4066Sahrens 	mdb_dsl_dir_t dd;
1546fa9e4066Sahrens 	mdb_dsl_dir_phys_t dsp;
1547fa9e4066Sahrens 	uint64_t children;
1548fa9e4066Sahrens 	uintptr_t childaddr;
15495f5f7a6fSahrens 	space_data_t sd;
1550fa9e4066Sahrens 	int shift = 20;
1551fa9e4066Sahrens 	char *suffix = "M";
155228e4da25SMatthew Ahrens 	int bytes = B_FALSE;
1553fa9e4066Sahrens 
155428e4da25SMatthew Ahrens 	if (mdb_getopts(argc, argv, 'b', MDB_OPT_SETBITS, TRUE, &bytes, NULL) !=
1555fa9e4066Sahrens 	    argc)
1556fa9e4066Sahrens 		return (DCMD_USAGE);
1557fa9e4066Sahrens 	if (!(flags & DCMD_ADDRSPEC))
1558fa9e4066Sahrens 		return (DCMD_USAGE);
1559fa9e4066Sahrens 
156028e4da25SMatthew Ahrens 	if (bytes) {
1561fa9e4066Sahrens 		shift = 0;
1562fa9e4066Sahrens 		suffix = "";
1563fa9e4066Sahrens 	}
1564fa9e4066Sahrens 
156528e4da25SMatthew Ahrens 	if (GETMEMB(addr, "spa", spa_dsl_pool, spa.spa_dsl_pool) ||
156628e4da25SMatthew Ahrens 	    GETMEMB(addr, "spa", spa_root_vdev, spa.spa_root_vdev) ||
156728e4da25SMatthew Ahrens 	    GETMEMB(spa.spa_root_vdev, "vdev", vdev_children, children) ||
156828e4da25SMatthew Ahrens 	    GETMEMB(spa.spa_root_vdev, "vdev", vdev_child, childaddr) ||
156928e4da25SMatthew Ahrens 	    GETMEMB(spa.spa_dsl_pool, "dsl_pool",
1570fa9e4066Sahrens 	    dp_root_dir, dp_root_dir) ||
157128e4da25SMatthew Ahrens 	    GETMEMB(dp_root_dir, "dsl_dir", dd_phys, dd.dd_phys) ||
157228e4da25SMatthew Ahrens 	    GETMEMB(dp_root_dir, "dsl_dir",
1573fa9e4066Sahrens 	    dd_space_towrite, dd.dd_space_towrite) ||
157428e4da25SMatthew Ahrens 	    GETMEMB(dd.dd_phys, "dsl_dir_phys",
15755f5f7a6fSahrens 	    dd_used_bytes, dsp.dd_used_bytes) ||
157628e4da25SMatthew Ahrens 	    GETMEMB(dd.dd_phys, "dsl_dir_phys",
1577fa9e4066Sahrens 	    dd_compressed_bytes, dsp.dd_compressed_bytes) ||
157828e4da25SMatthew Ahrens 	    GETMEMB(dd.dd_phys, "dsl_dir_phys",
1579fa9e4066Sahrens 	    dd_uncompressed_bytes, dsp.dd_uncompressed_bytes)) {
1580fa9e4066Sahrens 		return (DCMD_ERR);
1581fa9e4066Sahrens 	}
1582fa9e4066Sahrens 
1583fa9e4066Sahrens 	mdb_printf("dd_space_towrite = %llu%s %llu%s %llu%s %llu%s\n",
1584fa9e4066Sahrens 	    dd.dd_space_towrite[0] >> shift, suffix,
1585fa9e4066Sahrens 	    dd.dd_space_towrite[1] >> shift, suffix,
1586fa9e4066Sahrens 	    dd.dd_space_towrite[2] >> shift, suffix,
1587fa9e4066Sahrens 	    dd.dd_space_towrite[3] >> shift, suffix);
1588fa9e4066Sahrens 
1589fa9e4066Sahrens 	mdb_printf("dd_phys.dd_used_bytes = %llu%s\n",
1590fa9e4066Sahrens 	    dsp.dd_used_bytes >> shift, suffix);
15915f5f7a6fSahrens 	mdb_printf("dd_phys.dd_compressed_bytes = %llu%s\n",
15925f5f7a6fSahrens 	    dsp.dd_compressed_bytes >> shift, suffix);
15935f5f7a6fSahrens 	mdb_printf("dd_phys.dd_uncompressed_bytes = %llu%s\n",
15945f5f7a6fSahrens 	    dsp.dd_uncompressed_bytes >> shift, suffix);
15955f5f7a6fSahrens 
15965f5f7a6fSahrens 	bzero(&sd, sizeof (sd));
15975f5f7a6fSahrens 	if (mdb_pwalk("metaslab", space_cb, &sd, addr) != 0) {
15985f5f7a6fSahrens 		mdb_warn("can't walk metaslabs");
15995f5f7a6fSahrens 		return (DCMD_ERR);
1600fa9e4066Sahrens 	}
1601fa9e4066Sahrens 
1602fa9e4066Sahrens 	mdb_printf("ms_allocmap = %llu%s %llu%s %llu%s %llu%s\n",
16030713e232SGeorge Wilson 	    sd.ms_alloctree[0] >> shift, suffix,
16040713e232SGeorge Wilson 	    sd.ms_alloctree[1] >> shift, suffix,
16050713e232SGeorge Wilson 	    sd.ms_alloctree[2] >> shift, suffix,
16060713e232SGeorge Wilson 	    sd.ms_alloctree[3] >> shift, suffix);
1607fa9e4066Sahrens 	mdb_printf("ms_freemap = %llu%s %llu%s %llu%s %llu%s\n",
16080713e232SGeorge Wilson 	    sd.ms_freetree[0] >> shift, suffix,
16090713e232SGeorge Wilson 	    sd.ms_freetree[1] >> shift, suffix,
16100713e232SGeorge Wilson 	    sd.ms_freetree[2] >> shift, suffix,
16110713e232SGeorge Wilson 	    sd.ms_freetree[3] >> shift, suffix);
16120713e232SGeorge Wilson 	mdb_printf("ms_tree = %llu%s\n", sd.ms_tree >> shift, suffix);
16135f5f7a6fSahrens 	mdb_printf("last synced avail = %llu%s\n", sd.avail >> shift, suffix);
16145f5f7a6fSahrens 	mdb_printf("current syncing avail = %llu%s\n",
16155f5f7a6fSahrens 	    sd.nowavail >> shift, suffix);
1616fa9e4066Sahrens 
1617fa9e4066Sahrens 	return (DCMD_OK);
1618fa9e4066Sahrens }
1619fa9e4066Sahrens 
162022ce0148SMatthew Ahrens typedef struct mdb_spa_aux_vdev {
162122ce0148SMatthew Ahrens 	int sav_count;
162222ce0148SMatthew Ahrens 	uintptr_t sav_vdevs;
162322ce0148SMatthew Ahrens } mdb_spa_aux_vdev_t;
1624fa9e4066Sahrens 
162522ce0148SMatthew Ahrens typedef struct mdb_spa_vdevs {
162622ce0148SMatthew Ahrens 	uintptr_t spa_root_vdev;
162722ce0148SMatthew Ahrens 	mdb_spa_aux_vdev_t spa_l2cache;
162822ce0148SMatthew Ahrens 	mdb_spa_aux_vdev_t spa_spares;
162922ce0148SMatthew Ahrens } mdb_spa_vdevs_t;
1630fa9e4066Sahrens 
1631ec9f632eSEric Schrock static int
163222ce0148SMatthew Ahrens spa_print_aux(mdb_spa_aux_vdev_t *sav, uint_t flags, mdb_arg_t *v,
1633ec9f632eSEric Schrock     const char *name)
1634ec9f632eSEric Schrock {
1635ec9f632eSEric Schrock 	uintptr_t *aux;
1636ec9f632eSEric Schrock 	size_t len;
1637ec9f632eSEric Schrock 	int ret, i;
1638ec9f632eSEric Schrock 
1639ec9f632eSEric Schrock 	/*
1640ec9f632eSEric Schrock 	 * Iterate over aux vdevs and print those out as well.  This is a
1641ec9f632eSEric Schrock 	 * little annoying because we don't have a root vdev to pass to ::vdev.
1642ec9f632eSEric Schrock 	 * Instead, we print a single line and then call it for each child
1643ec9f632eSEric Schrock 	 * vdev.
1644ec9f632eSEric Schrock 	 */
1645ec9f632eSEric Schrock 	if (sav->sav_count != 0) {
1646ec9f632eSEric Schrock 		v[1].a_type = MDB_TYPE_STRING;
1647ec9f632eSEric Schrock 		v[1].a_un.a_str = "-d";
1648ec9f632eSEric Schrock 		v[2].a_type = MDB_TYPE_IMMEDIATE;
1649ec9f632eSEric Schrock 		v[2].a_un.a_val = 2;
1650ec9f632eSEric Schrock 
1651ec9f632eSEric Schrock 		len = sav->sav_count * sizeof (uintptr_t);
1652ec9f632eSEric Schrock 		aux = mdb_alloc(len, UM_SLEEP);
165322ce0148SMatthew Ahrens 		if (mdb_vread(aux, len, sav->sav_vdevs) == -1) {
1654ec9f632eSEric Schrock 			mdb_free(aux, len);
1655ec9f632eSEric Schrock 			mdb_warn("failed to read l2cache vdevs at %p",
1656ec9f632eSEric Schrock 			    sav->sav_vdevs);
1657ec9f632eSEric Schrock 			return (DCMD_ERR);
1658ec9f632eSEric Schrock 		}
1659ec9f632eSEric Schrock 
1660ec9f632eSEric Schrock 		mdb_printf("%-?s %-9s %-12s %s\n", "-", "-", "-", name);
1661ec9f632eSEric Schrock 
1662ec9f632eSEric Schrock 		for (i = 0; i < sav->sav_count; i++) {
1663ec9f632eSEric Schrock 			ret = mdb_call_dcmd("vdev", aux[i], flags, 3, v);
1664ec9f632eSEric Schrock 			if (ret != DCMD_OK) {
1665ec9f632eSEric Schrock 				mdb_free(aux, len);
1666ec9f632eSEric Schrock 				return (ret);
1667ec9f632eSEric Schrock 			}
1668ec9f632eSEric Schrock 		}
1669ec9f632eSEric Schrock 
1670ec9f632eSEric Schrock 		mdb_free(aux, len);
1671ec9f632eSEric Schrock 	}
1672ec9f632eSEric Schrock 
1673ec9f632eSEric Schrock 	return (0);
1674ec9f632eSEric Schrock }
1675ec9f632eSEric Schrock 
1676fa9e4066Sahrens /*
1677fa9e4066Sahrens  * ::spa_vdevs
1678fa9e4066Sahrens  *
1679fa9e4066Sahrens  * 	-e	Include error stats
1680fa9e4066Sahrens  *
1681fa9e4066Sahrens  * Print out a summarized list of vdevs for the given spa_t.
1682c5904d13Seschrock  * This is accomplished by invoking "::vdev -re" on the root vdev, as well as
1683c5904d13Seschrock  * iterating over the cache devices.
1684fa9e4066Sahrens  */
1685fa9e4066Sahrens /* ARGSUSED */
1686fa9e4066Sahrens static int
1687fa9e4066Sahrens spa_vdevs(uintptr_t addr, uint_t flags, int argc, const mdb_arg_t *argv)
1688fa9e4066Sahrens {
1689c5904d13Seschrock 	mdb_arg_t v[3];
1690fa9e4066Sahrens 	int errors = FALSE;
1691ec9f632eSEric Schrock 	int ret;
1692fa9e4066Sahrens 
1693fa9e4066Sahrens 	if (mdb_getopts(argc, argv,
1694fa9e4066Sahrens 	    'e', MDB_OPT_SETBITS, TRUE, &errors,
1695fa9e4066Sahrens 	    NULL) != argc)
1696fa9e4066Sahrens 		return (DCMD_USAGE);
1697fa9e4066Sahrens 
1698fa9e4066Sahrens 	if (!(flags & DCMD_ADDRSPEC))
1699fa9e4066Sahrens 		return (DCMD_USAGE);
1700fa9e4066Sahrens 
170122ce0148SMatthew Ahrens 	mdb_spa_vdevs_t spa;
170222ce0148SMatthew Ahrens 	if (mdb_ctf_vread(&spa, "spa_t", "mdb_spa_vdevs_t", addr, 0) == -1)
1703fa9e4066Sahrens 		return (DCMD_ERR);
1704fa9e4066Sahrens 
1705088e9d47Seschrock 	/*
1706088e9d47Seschrock 	 * Unitialized spa_t structures can have a NULL root vdev.
1707088e9d47Seschrock 	 */
1708088e9d47Seschrock 	if (spa.spa_root_vdev == NULL) {
1709088e9d47Seschrock 		mdb_printf("no associated vdevs\n");
1710088e9d47Seschrock 		return (DCMD_OK);
1711088e9d47Seschrock 	}
1712088e9d47Seschrock 
1713c5904d13Seschrock 	v[0].a_type = MDB_TYPE_STRING;
1714c5904d13Seschrock 	v[0].a_un.a_str = errors ? "-re" : "-r";
1715fa9e4066Sahrens 
1716c5904d13Seschrock 	ret = mdb_call_dcmd("vdev", (uintptr_t)spa.spa_root_vdev,
1717c5904d13Seschrock 	    flags, 1, v);
1718c5904d13Seschrock 	if (ret != DCMD_OK)
1719c5904d13Seschrock 		return (ret);
1720c5904d13Seschrock 
1721ec9f632eSEric Schrock 	if (spa_print_aux(&spa.spa_l2cache, flags, v, "cache") != 0 ||
1722ec9f632eSEric Schrock 	    spa_print_aux(&spa.spa_spares, flags, v, "spares") != 0)
1723ec9f632eSEric Schrock 		return (DCMD_ERR);
1724c5904d13Seschrock 
1725c5904d13Seschrock 	return (DCMD_OK);
1726fa9e4066Sahrens }
1727fa9e4066Sahrens 
1728ccae0b50Seschrock /*
1729ccae0b50Seschrock  * ::zio
1730ccae0b50Seschrock  *
1731ccae0b50Seschrock  * Print a summary of zio_t and all its children.  This is intended to display a
1732ccae0b50Seschrock  * zio tree, and hence we only pick the most important pieces of information for
1733ccae0b50Seschrock  * the main summary.  More detailed information can always be found by doing a
1734ccae0b50Seschrock  * '::print zio' on the underlying zio_t.  The columns we display are:
1735ccae0b50Seschrock  *
1736c55e05cbSMatthew Ahrens  *	ADDRESS  TYPE  STAGE  WAITER  TIME_ELAPSED
1737ccae0b50Seschrock  *
1738ccae0b50Seschrock  * The 'address' column is indented by one space for each depth level as we
1739ccae0b50Seschrock  * descend down the tree.
1740ccae0b50Seschrock  */
1741fac3008cSeschrock 
1742c55e05cbSMatthew Ahrens #define	ZIO_MAXINDENT	7
1743a3f829aeSBill Moore #define	ZIO_MAXWIDTH	(sizeof (uintptr_t) * 2 + ZIO_MAXINDENT)
1744a3f829aeSBill Moore #define	ZIO_WALK_SELF	0
1745a3f829aeSBill Moore #define	ZIO_WALK_CHILD	1
1746a3f829aeSBill Moore #define	ZIO_WALK_PARENT	2
1747a3f829aeSBill Moore 
1748a3f829aeSBill Moore typedef struct zio_print_args {
1749a3f829aeSBill Moore 	int	zpa_current_depth;
1750a3f829aeSBill Moore 	int	zpa_min_depth;
1751a3f829aeSBill Moore 	int	zpa_max_depth;
1752a3f829aeSBill Moore 	int	zpa_type;
1753a3f829aeSBill Moore 	uint_t	zpa_flags;
1754a3f829aeSBill Moore } zio_print_args_t;
1755a3f829aeSBill Moore 
175628e4da25SMatthew Ahrens typedef struct mdb_zio {
175728e4da25SMatthew Ahrens 	enum zio_type io_type;
175828e4da25SMatthew Ahrens 	enum zio_stage io_stage;
1759d5ee8a13SMatthew Ahrens 	uintptr_t io_waiter;
1760d5ee8a13SMatthew Ahrens 	uintptr_t io_spa;
176128e4da25SMatthew Ahrens 	struct {
176228e4da25SMatthew Ahrens 		struct {
1763d5ee8a13SMatthew Ahrens 			uintptr_t list_next;
176428e4da25SMatthew Ahrens 		} list_head;
176528e4da25SMatthew Ahrens 	} io_parent_list;
176628e4da25SMatthew Ahrens 	int io_error;
176728e4da25SMatthew Ahrens } mdb_zio_t;
176828e4da25SMatthew Ahrens 
1769c55e05cbSMatthew Ahrens typedef struct mdb_zio_timestamp {
1770c55e05cbSMatthew Ahrens 	hrtime_t io_timestamp;
1771c55e05cbSMatthew Ahrens } mdb_zio_timestamp_t;
1772c55e05cbSMatthew Ahrens 
1773a3f829aeSBill Moore static int zio_child_cb(uintptr_t addr, const void *unknown, void *arg);
1774fac3008cSeschrock 
1775ccae0b50Seschrock static int
1776c55e05cbSMatthew Ahrens zio_print_cb(uintptr_t addr, zio_print_args_t *zpa)
1777ccae0b50Seschrock {
1778ccae0b50Seschrock 	mdb_ctf_id_t type_enum, stage_enum;
1779a3f829aeSBill Moore 	int indent = zpa->zpa_current_depth;
1780ccae0b50Seschrock 	const char *type, *stage;
1781a3f829aeSBill Moore 	uintptr_t laddr;
1782c55e05cbSMatthew Ahrens 	mdb_zio_t zio;
1783c55e05cbSMatthew Ahrens 	mdb_zio_timestamp_t zio_timestamp = { 0 };
1784c55e05cbSMatthew Ahrens 
1785c55e05cbSMatthew Ahrens 	if (mdb_ctf_vread(&zio, ZFS_STRUCT "zio", "mdb_zio_t", addr, 0) == -1)
1786c55e05cbSMatthew Ahrens 		return (WALK_ERR);
1787c55e05cbSMatthew Ahrens 	(void) mdb_ctf_vread(&zio_timestamp, ZFS_STRUCT "zio",
1788c55e05cbSMatthew Ahrens 	    "mdb_zio_timestamp_t", addr, MDB_CTF_VREAD_QUIET);
1789ccae0b50Seschrock 
1790a3f829aeSBill Moore 	if (indent > ZIO_MAXINDENT)
1791a3f829aeSBill Moore 		indent = ZIO_MAXINDENT;
1792ccae0b50Seschrock 
1793ccae0b50Seschrock 	if (mdb_ctf_lookup_by_name("enum zio_type", &type_enum) == -1 ||
1794ccae0b50Seschrock 	    mdb_ctf_lookup_by_name("enum zio_stage", &stage_enum) == -1) {
1795ccae0b50Seschrock 		mdb_warn("failed to lookup zio enums");
1796ccae0b50Seschrock 		return (WALK_ERR);
1797ccae0b50Seschrock 	}
1798ccae0b50Seschrock 
1799c55e05cbSMatthew Ahrens 	if ((type = mdb_ctf_enum_name(type_enum, zio.io_type)) != NULL)
1800ccae0b50Seschrock 		type += sizeof ("ZIO_TYPE_") - 1;
1801ccae0b50Seschrock 	else
1802ccae0b50Seschrock 		type = "?";
1803ccae0b50Seschrock 
1804c55e05cbSMatthew Ahrens 	if (zio.io_error == 0) {
1805c55e05cbSMatthew Ahrens 		stage = mdb_ctf_enum_name(stage_enum, zio.io_stage);
180628e4da25SMatthew Ahrens 		if (stage != NULL)
180728e4da25SMatthew Ahrens 			stage += sizeof ("ZIO_STAGE_") - 1;
180828e4da25SMatthew Ahrens 		else
180928e4da25SMatthew Ahrens 			stage = "?";
181028e4da25SMatthew Ahrens 	} else {
181128e4da25SMatthew Ahrens 		stage = "FAILED";
181228e4da25SMatthew Ahrens 	}
1813ccae0b50Seschrock 
1814a3f829aeSBill Moore 	if (zpa->zpa_current_depth >= zpa->zpa_min_depth) {
1815a3f829aeSBill Moore 		if (zpa->zpa_flags & DCMD_PIPE_OUT) {
1816a3f829aeSBill Moore 			mdb_printf("%?p\n", addr);
1817a3f829aeSBill Moore 		} else {
1818a3f829aeSBill Moore 			mdb_printf("%*s%-*p %-5s %-16s ", indent, "",
1819a3f829aeSBill Moore 			    ZIO_MAXWIDTH - indent, addr, type, stage);
1820d5ee8a13SMatthew Ahrens 			if (zio.io_waiter != 0)
1821d5ee8a13SMatthew Ahrens 				mdb_printf("%-16lx ", zio.io_waiter);
1822a3f829aeSBill Moore 			else
1823c55e05cbSMatthew Ahrens 				mdb_printf("%-16s ", "-");
1824c55e05cbSMatthew Ahrens #ifdef _KERNEL
1825c55e05cbSMatthew Ahrens 			if (zio_timestamp.io_timestamp != 0) {
1826c55e05cbSMatthew Ahrens 				mdb_printf("%llums", (mdb_gethrtime() -
1827c55e05cbSMatthew Ahrens 				    zio_timestamp.io_timestamp) /
1828c55e05cbSMatthew Ahrens 				    1000000);
1829c55e05cbSMatthew Ahrens 			} else {
1830c55e05cbSMatthew Ahrens 				mdb_printf("%-12s ", "-");
1831c55e05cbSMatthew Ahrens 			}
1832c55e05cbSMatthew Ahrens #else
1833c55e05cbSMatthew Ahrens 			mdb_printf("%-12s ", "-");
1834c55e05cbSMatthew Ahrens #endif
1835c55e05cbSMatthew Ahrens 			mdb_printf("\n");
1836a3f829aeSBill Moore 		}
1837a3f829aeSBill Moore 	}
1838a3f829aeSBill Moore 
1839a3f829aeSBill Moore 	if (zpa->zpa_current_depth >= zpa->zpa_max_depth)
1840a3f829aeSBill Moore 		return (WALK_NEXT);
1841ccae0b50Seschrock 
1842a3f829aeSBill Moore 	if (zpa->zpa_type == ZIO_WALK_PARENT)
184328e4da25SMatthew Ahrens 		laddr = addr + mdb_ctf_offsetof_by_name(ZFS_STRUCT "zio",
184428e4da25SMatthew Ahrens 		    "io_parent_list");
1845ccae0b50Seschrock 	else
184628e4da25SMatthew Ahrens 		laddr = addr + mdb_ctf_offsetof_by_name(ZFS_STRUCT "zio",
184728e4da25SMatthew Ahrens 		    "io_child_list");
1848ccae0b50Seschrock 
1849a3f829aeSBill Moore 	zpa->zpa_current_depth++;
1850a3f829aeSBill Moore 	if (mdb_pwalk("list", zio_child_cb, zpa, laddr) != 0) {
1851a3f829aeSBill Moore 		mdb_warn("failed to walk zio_t children at %p\n", laddr);
1852ccae0b50Seschrock 		return (WALK_ERR);
1853ccae0b50Seschrock 	}
1854a3f829aeSBill Moore 	zpa->zpa_current_depth--;
1855ccae0b50Seschrock 
1856ccae0b50Seschrock 	return (WALK_NEXT);
1857ccae0b50Seschrock }
1858ccae0b50Seschrock 
1859a3f829aeSBill Moore /* ARGSUSED */
1860ccae0b50Seschrock static int
1861a3f829aeSBill Moore zio_child_cb(uintptr_t addr, const void *unknown, void *arg)
1862ccae0b50Seschrock {
1863a3f829aeSBill Moore 	zio_link_t zl;
1864a3f829aeSBill Moore 	uintptr_t ziop;
1865a3f829aeSBill Moore 	zio_print_args_t *zpa = arg;
1866a3f829aeSBill Moore 
1867a3f829aeSBill Moore 	if (mdb_vread(&zl, sizeof (zl), addr) == -1) {
1868a3f829aeSBill Moore 		mdb_warn("failed to read zio_link_t at %p", addr);
1869a3f829aeSBill Moore 		return (WALK_ERR);
1870a3f829aeSBill Moore 	}
1871a3f829aeSBill Moore 
1872a3f829aeSBill Moore 	if (zpa->zpa_type == ZIO_WALK_PARENT)
1873a3f829aeSBill Moore 		ziop = (uintptr_t)zl.zl_parent;
1874a3f829aeSBill Moore 	else
1875a3f829aeSBill Moore 		ziop = (uintptr_t)zl.zl_child;
1876a3f829aeSBill Moore 
187769962b56SMatthew Ahrens 	return (zio_print_cb(ziop, zpa));
1878a3f829aeSBill Moore }
1879a3f829aeSBill Moore 
1880a3f829aeSBill Moore /* ARGSUSED */
1881a3f829aeSBill Moore static int
1882a3f829aeSBill Moore zio_print(uintptr_t addr, uint_t flags, int argc, const mdb_arg_t *argv)
1883a3f829aeSBill Moore {
1884a3f829aeSBill Moore 	zio_print_args_t zpa = { 0 };
1885ccae0b50Seschrock 
1886ccae0b50Seschrock 	if (!(flags & DCMD_ADDRSPEC))
1887ccae0b50Seschrock 		return (DCMD_USAGE);
1888ccae0b50Seschrock 
1889a3f829aeSBill Moore 	if (mdb_getopts(argc, argv,
1890a3f829aeSBill Moore 	    'r', MDB_OPT_SETBITS, INT_MAX, &zpa.zpa_max_depth,
1891a3f829aeSBill Moore 	    'c', MDB_OPT_SETBITS, ZIO_WALK_CHILD, &zpa.zpa_type,
1892a3f829aeSBill Moore 	    'p', MDB_OPT_SETBITS, ZIO_WALK_PARENT, &zpa.zpa_type,
1893a3f829aeSBill Moore 	    NULL) != argc)
1894a3f829aeSBill Moore 		return (DCMD_USAGE);
1895a3f829aeSBill Moore 
1896a3f829aeSBill Moore 	zpa.zpa_flags = flags;
1897a3f829aeSBill Moore 	if (zpa.zpa_max_depth != 0) {
1898a3f829aeSBill Moore 		if (zpa.zpa_type == ZIO_WALK_SELF)
1899a3f829aeSBill Moore 			zpa.zpa_type = ZIO_WALK_CHILD;
1900a3f829aeSBill Moore 	} else if (zpa.zpa_type != ZIO_WALK_SELF) {
1901a3f829aeSBill Moore 		zpa.zpa_min_depth = 1;
1902a3f829aeSBill Moore 		zpa.zpa_max_depth = 1;
1903a3f829aeSBill Moore 	}
1904a3f829aeSBill Moore 
1905c55e05cbSMatthew Ahrens 	if (!(flags & DCMD_PIPE_OUT) && DCMD_HDRSPEC(flags)) {
1906c55e05cbSMatthew Ahrens 		mdb_printf("%<u>%-*s %-5s %-16s %-16s %-12s%</u>\n",
1907c55e05cbSMatthew Ahrens 		    ZIO_MAXWIDTH, "ADDRESS", "TYPE", "STAGE", "WAITER",
1908c55e05cbSMatthew Ahrens 		    "TIME_ELAPSED");
1909c55e05cbSMatthew Ahrens 	}
1910fac3008cSeschrock 
1911c55e05cbSMatthew Ahrens 	if (zio_print_cb(addr, &zpa) != WALK_NEXT)
1912ccae0b50Seschrock 		return (DCMD_ERR);
1913ccae0b50Seschrock 
1914ccae0b50Seschrock 	return (DCMD_OK);
1915ccae0b50Seschrock }
1916ccae0b50Seschrock 
1917ccae0b50Seschrock /*
1918ccae0b50Seschrock  * [addr]::zio_state
1919ccae0b50Seschrock  *
1920ccae0b50Seschrock  * Print a summary of all zio_t structures on the system, or for a particular
1921ccae0b50Seschrock  * pool.  This is equivalent to '::walk zio_root | ::zio'.
1922ccae0b50Seschrock  */
1923ccae0b50Seschrock /*ARGSUSED*/
1924ccae0b50Seschrock static int
1925ccae0b50Seschrock zio_state(uintptr_t addr, uint_t flags, int argc, const mdb_arg_t *argv)
1926ccae0b50Seschrock {
1927ccae0b50Seschrock 	/*
1928ccae0b50Seschrock 	 * MDB will remember the last address of the pipeline, so if we don't
1929ccae0b50Seschrock 	 * zero this we'll end up trying to walk zio structures for a
1930ccae0b50Seschrock 	 * non-existent spa_t.
1931ccae0b50Seschrock 	 */
1932ccae0b50Seschrock 	if (!(flags & DCMD_ADDRSPEC))
1933ccae0b50Seschrock 		addr = 0;
1934ccae0b50Seschrock 
1935ccae0b50Seschrock 	return (mdb_pwalk_dcmd("zio_root", "zio", argc, argv, addr));
1936ccae0b50Seschrock }
1937ccae0b50Seschrock 
1938fa9e4066Sahrens typedef struct txg_list_walk_data {
1939fa9e4066Sahrens 	uintptr_t lw_head[TXG_SIZE];
1940fa9e4066Sahrens 	int	lw_txgoff;
1941fa9e4066Sahrens 	int	lw_maxoff;
1942fa9e4066Sahrens 	size_t	lw_offset;
1943fa9e4066Sahrens 	void	*lw_obj;
1944fa9e4066Sahrens } txg_list_walk_data_t;
1945fa9e4066Sahrens 
1946fa9e4066Sahrens static int
1947fa9e4066Sahrens txg_list_walk_init_common(mdb_walk_state_t *wsp, int txg, int maxoff)
1948fa9e4066Sahrens {
1949fa9e4066Sahrens 	txg_list_walk_data_t *lwd;
1950fa9e4066Sahrens 	txg_list_t list;
1951fa9e4066Sahrens 	int i;
1952fa9e4066Sahrens 
1953fa9e4066Sahrens 	lwd = mdb_alloc(sizeof (txg_list_walk_data_t), UM_SLEEP | UM_GC);
1954fa9e4066Sahrens 	if (mdb_vread(&list, sizeof (txg_list_t), wsp->walk_addr) == -1) {
1955fa9e4066Sahrens 		mdb_warn("failed to read txg_list_t at %#lx", wsp->walk_addr);
1956fa9e4066Sahrens 		return (WALK_ERR);
1957fa9e4066Sahrens 	}
1958fa9e4066Sahrens 
1959fa9e4066Sahrens 	for (i = 0; i < TXG_SIZE; i++)
1960fa9e4066Sahrens 		lwd->lw_head[i] = (uintptr_t)list.tl_head[i];
1961fa9e4066Sahrens 	lwd->lw_offset = list.tl_offset;
1962fa9e4066Sahrens 	lwd->lw_obj = mdb_alloc(lwd->lw_offset + sizeof (txg_node_t),
1963fa9e4066Sahrens 	    UM_SLEEP | UM_GC);
1964fa9e4066Sahrens 	lwd->lw_txgoff = txg;
1965fa9e4066Sahrens 	lwd->lw_maxoff = maxoff;
1966fa9e4066Sahrens 
1967fa9e4066Sahrens 	wsp->walk_addr = lwd->lw_head[lwd->lw_txgoff];
1968fa9e4066Sahrens 	wsp->walk_data = lwd;
1969fa9e4066Sahrens 
1970fa9e4066Sahrens 	return (WALK_NEXT);
1971fa9e4066Sahrens }
1972fa9e4066Sahrens 
1973fa9e4066Sahrens static int
1974fa9e4066Sahrens txg_list_walk_init(mdb_walk_state_t *wsp)
1975fa9e4066Sahrens {
1976fa9e4066Sahrens 	return (txg_list_walk_init_common(wsp, 0, TXG_SIZE-1));
1977fa9e4066Sahrens }
1978fa9e4066Sahrens 
1979fa9e4066Sahrens static int
1980fa9e4066Sahrens txg_list0_walk_init(mdb_walk_state_t *wsp)
1981fa9e4066Sahrens {
1982fa9e4066Sahrens 	return (txg_list_walk_init_common(wsp, 0, 0));
1983fa9e4066Sahrens }
1984fa9e4066Sahrens 
1985fa9e4066Sahrens static int
1986fa9e4066Sahrens txg_list1_walk_init(mdb_walk_state_t *wsp)
1987fa9e4066Sahrens {
1988fa9e4066Sahrens 	return (txg_list_walk_init_common(wsp, 1, 1));
1989fa9e4066Sahrens }
1990fa9e4066Sahrens 
1991fa9e4066Sahrens static int
1992fa9e4066Sahrens txg_list2_walk_init(mdb_walk_state_t *wsp)
1993fa9e4066Sahrens {
1994fa9e4066Sahrens 	return (txg_list_walk_init_common(wsp, 2, 2));
1995fa9e4066Sahrens }
1996fa9e4066Sahrens 
1997fa9e4066Sahrens static int
1998fa9e4066Sahrens txg_list3_walk_init(mdb_walk_state_t *wsp)
1999fa9e4066Sahrens {
2000fa9e4066Sahrens 	return (txg_list_walk_init_common(wsp, 3, 3));
2001fa9e4066Sahrens }
2002fa9e4066Sahrens 
2003fa9e4066Sahrens static int
2004fa9e4066Sahrens txg_list_walk_step(mdb_walk_state_t *wsp)
2005fa9e4066Sahrens {
2006fa9e4066Sahrens 	txg_list_walk_data_t *lwd = wsp->walk_data;
2007fa9e4066Sahrens 	uintptr_t addr;
2008fa9e4066Sahrens 	txg_node_t *node;
2009fa9e4066Sahrens 	int status;
2010fa9e4066Sahrens 
2011fa9e4066Sahrens 	while (wsp->walk_addr == NULL && lwd->lw_txgoff < lwd->lw_maxoff) {
2012fa9e4066Sahrens 		lwd->lw_txgoff++;
2013fa9e4066Sahrens 		wsp->walk_addr = lwd->lw_head[lwd->lw_txgoff];
2014fa9e4066Sahrens 	}
2015fa9e4066Sahrens 
2016fa9e4066Sahrens 	if (wsp->walk_addr == NULL)
2017fa9e4066Sahrens 		return (WALK_DONE);
2018fa9e4066Sahrens 
2019fa9e4066Sahrens 	addr = wsp->walk_addr - lwd->lw_offset;
2020fa9e4066Sahrens 
2021fa9e4066Sahrens 	if (mdb_vread(lwd->lw_obj,
2022fa9e4066Sahrens 	    lwd->lw_offset + sizeof (txg_node_t), addr) == -1) {
2023fa9e4066Sahrens 		mdb_warn("failed to read list element at %#lx", addr);
2024fa9e4066Sahrens 		return (WALK_ERR);
2025fa9e4066Sahrens 	}
2026fa9e4066Sahrens 
2027fa9e4066Sahrens 	status = wsp->walk_callback(addr, lwd->lw_obj, wsp->walk_cbdata);
2028fa9e4066Sahrens 	node = (txg_node_t *)((uintptr_t)lwd->lw_obj + lwd->lw_offset);
2029fa9e4066Sahrens 	wsp->walk_addr = (uintptr_t)node->tn_next[lwd->lw_txgoff];
2030fa9e4066Sahrens 
2031fa9e4066Sahrens 	return (status);
2032fa9e4066Sahrens }
2033fa9e4066Sahrens 
2034fa9e4066Sahrens /*
2035fa9e4066Sahrens  * ::walk spa
2036fa9e4066Sahrens  *
2037fa9e4066Sahrens  * Walk all named spa_t structures in the namespace.  This is nothing more than
2038fa9e4066Sahrens  * a layered avl walk.
2039fa9e4066Sahrens  */
2040fa9e4066Sahrens static int
2041fa9e4066Sahrens spa_walk_init(mdb_walk_state_t *wsp)
2042fa9e4066Sahrens {
2043fa9e4066Sahrens 	GElf_Sym sym;
2044fa9e4066Sahrens 
2045fa9e4066Sahrens 	if (wsp->walk_addr != NULL) {
2046fa9e4066Sahrens 		mdb_warn("spa walk only supports global walks\n");
2047fa9e4066Sahrens 		return (WALK_ERR);
2048fa9e4066Sahrens 	}
2049fa9e4066Sahrens 
2050fa9e4066Sahrens 	if (mdb_lookup_by_obj(ZFS_OBJ_NAME, "spa_namespace_avl", &sym) == -1) {
2051fa9e4066Sahrens 		mdb_warn("failed to find symbol 'spa_namespace_avl'");
2052fa9e4066Sahrens 		return (WALK_ERR);
2053fa9e4066Sahrens 	}
2054fa9e4066Sahrens 
2055fa9e4066Sahrens 	wsp->walk_addr = (uintptr_t)sym.st_value;
2056fa9e4066Sahrens 
2057fa9e4066Sahrens 	if (mdb_layered_walk("avl", wsp) == -1) {
2058fa9e4066Sahrens 		mdb_warn("failed to walk 'avl'\n");
2059fa9e4066Sahrens 		return (WALK_ERR);
2060fa9e4066Sahrens 	}
2061fa9e4066Sahrens 
2062fa9e4066Sahrens 	return (WALK_NEXT);
2063fa9e4066Sahrens }
2064fa9e4066Sahrens 
2065fa9e4066Sahrens static int
2066fa9e4066Sahrens spa_walk_step(mdb_walk_state_t *wsp)
2067fa9e4066Sahrens {
206822ce0148SMatthew Ahrens 	return (wsp->walk_callback(wsp->walk_addr, NULL, wsp->walk_cbdata));
2069fa9e4066Sahrens }
2070fa9e4066Sahrens 
2071ccae0b50Seschrock /*
2072ccae0b50Seschrock  * [addr]::walk zio
2073ccae0b50Seschrock  *
2074ccae0b50Seschrock  * Walk all active zio_t structures on the system.  This is simply a layered
2075ccae0b50Seschrock  * walk on top of ::walk zio_cache, with the optional ability to limit the
2076ccae0b50Seschrock  * structures to a particular pool.
2077ccae0b50Seschrock  */
2078ccae0b50Seschrock static int
2079ccae0b50Seschrock zio_walk_init(mdb_walk_state_t *wsp)
2080ccae0b50Seschrock {
2081dee140dfSMatthew Ahrens 	wsp->walk_data = (void *)wsp->walk_addr;
2082ccae0b50Seschrock 
2083ccae0b50Seschrock 	if (mdb_layered_walk("zio_cache", wsp) == -1) {
2084ccae0b50Seschrock 		mdb_warn("failed to walk 'zio_cache'\n");
2085ccae0b50Seschrock 		return (WALK_ERR);
2086ccae0b50Seschrock 	}
2087ccae0b50Seschrock 
2088ccae0b50Seschrock 	return (WALK_NEXT);
2089ccae0b50Seschrock }
2090ccae0b50Seschrock 
2091ccae0b50Seschrock static int
2092ccae0b50Seschrock zio_walk_step(mdb_walk_state_t *wsp)
2093ccae0b50Seschrock {
209428e4da25SMatthew Ahrens 	mdb_zio_t zio;
2095dee140dfSMatthew Ahrens 	uintptr_t spa = (uintptr_t)wsp->walk_data;
2096ccae0b50Seschrock 
209728e4da25SMatthew Ahrens 	if (mdb_ctf_vread(&zio, ZFS_STRUCT "zio", "mdb_zio_t",
209828e4da25SMatthew Ahrens 	    wsp->walk_addr, 0) == -1)
2099ccae0b50Seschrock 		return (WALK_ERR);
2100ccae0b50Seschrock 
2101dee140dfSMatthew Ahrens 	if (spa != 0 && spa != zio.io_spa)
2102ccae0b50Seschrock 		return (WALK_NEXT);
2103ccae0b50Seschrock 
2104ccae0b50Seschrock 	return (wsp->walk_callback(wsp->walk_addr, &zio, wsp->walk_cbdata));
2105ccae0b50Seschrock }
2106ccae0b50Seschrock 
2107ccae0b50Seschrock /*
2108ccae0b50Seschrock  * [addr]::walk zio_root
2109ccae0b50Seschrock  *
2110ccae0b50Seschrock  * Walk only root zio_t structures, optionally for a particular spa_t.
2111ccae0b50Seschrock  */
2112ccae0b50Seschrock static int
2113ccae0b50Seschrock zio_walk_root_step(mdb_walk_state_t *wsp)
2114ccae0b50Seschrock {
211528e4da25SMatthew Ahrens 	mdb_zio_t zio;
2116dee140dfSMatthew Ahrens 	uintptr_t spa = (uintptr_t)wsp->walk_data;
2117ccae0b50Seschrock 
211828e4da25SMatthew Ahrens 	if (mdb_ctf_vread(&zio, ZFS_STRUCT "zio", "mdb_zio_t",
211928e4da25SMatthew Ahrens 	    wsp->walk_addr, 0) == -1)
2120ccae0b50Seschrock 		return (WALK_ERR);
2121ccae0b50Seschrock 
2122dee140dfSMatthew Ahrens 	if (spa != 0 && spa != zio.io_spa)
2123ccae0b50Seschrock 		return (WALK_NEXT);
2124ccae0b50Seschrock 
2125a3f829aeSBill Moore 	/* If the parent list is not empty, ignore */
2126d5ee8a13SMatthew Ahrens 	if (zio.io_parent_list.list_head.list_next !=
212728e4da25SMatthew Ahrens 	    wsp->walk_addr +
212828e4da25SMatthew Ahrens 	    mdb_ctf_offsetof_by_name(ZFS_STRUCT "zio", "io_parent_list") +
212928e4da25SMatthew Ahrens 	    mdb_ctf_offsetof_by_name("struct list", "list_head"))
2130ccae0b50Seschrock 		return (WALK_NEXT);
2131ccae0b50Seschrock 
2132ccae0b50Seschrock 	return (wsp->walk_callback(wsp->walk_addr, &zio, wsp->walk_cbdata));
2133ccae0b50Seschrock }
2134ccae0b50Seschrock 
213588b7b0f2SMatthew Ahrens #define	NICENUM_BUFLEN 6
213688b7b0f2SMatthew Ahrens 
213788b7b0f2SMatthew Ahrens static int
2138ff64c0f7SMatthew Ahrens snprintfrac(char *buf, int len,
2139ff64c0f7SMatthew Ahrens     uint64_t numerator, uint64_t denom, int frac_digits)
214088b7b0f2SMatthew Ahrens {
2141ff64c0f7SMatthew Ahrens 	int mul = 1;
214288b7b0f2SMatthew Ahrens 	int whole, frac, i;
214388b7b0f2SMatthew Ahrens 
214488b7b0f2SMatthew Ahrens 	for (i = frac_digits; i; i--)
214588b7b0f2SMatthew Ahrens 		mul *= 10;
2146ff64c0f7SMatthew Ahrens 	whole = numerator / denom;
2147ff64c0f7SMatthew Ahrens 	frac = mul * numerator / denom - mul * whole;
214888b7b0f2SMatthew Ahrens 	return (mdb_snprintf(buf, len, "%u.%0*u", whole, frac_digits, frac));
214988b7b0f2SMatthew Ahrens }
215088b7b0f2SMatthew Ahrens 
215188b7b0f2SMatthew Ahrens static void
215288b7b0f2SMatthew Ahrens mdb_nicenum(uint64_t num, char *buf)
215388b7b0f2SMatthew Ahrens {
215488b7b0f2SMatthew Ahrens 	uint64_t n = num;
215588b7b0f2SMatthew Ahrens 	int index = 0;
215688b7b0f2SMatthew Ahrens 	char *u;
215788b7b0f2SMatthew Ahrens 
215888b7b0f2SMatthew Ahrens 	while (n >= 1024) {
215988b7b0f2SMatthew Ahrens 		n = (n + (1024 / 2)) / 1024; /* Round up or down */
216088b7b0f2SMatthew Ahrens 		index++;
216188b7b0f2SMatthew Ahrens 	}
216288b7b0f2SMatthew Ahrens 
216388b7b0f2SMatthew Ahrens 	u = &" \0K\0M\0G\0T\0P\0E\0"[index*2];
216488b7b0f2SMatthew Ahrens 
216588b7b0f2SMatthew Ahrens 	if (index == 0) {
216688b7b0f2SMatthew Ahrens 		(void) mdb_snprintf(buf, NICENUM_BUFLEN, "%llu",
216788b7b0f2SMatthew Ahrens 		    (u_longlong_t)n);
216888b7b0f2SMatthew Ahrens 	} else if (n < 10 && (num & (num - 1)) != 0) {
2169ff64c0f7SMatthew Ahrens 		(void) snprintfrac(buf, NICENUM_BUFLEN,
2170ff64c0f7SMatthew Ahrens 		    num, 1ULL << 10 * index, 2);
217188b7b0f2SMatthew Ahrens 		strcat(buf, u);
217288b7b0f2SMatthew Ahrens 	} else if (n < 100 && (num & (num - 1)) != 0) {
2173ff64c0f7SMatthew Ahrens 		(void) snprintfrac(buf, NICENUM_BUFLEN,
2174ff64c0f7SMatthew Ahrens 		    num, 1ULL << 10 * index, 1);
217588b7b0f2SMatthew Ahrens 		strcat(buf, u);
217688b7b0f2SMatthew Ahrens 	} else {
217788b7b0f2SMatthew Ahrens 		(void) mdb_snprintf(buf, NICENUM_BUFLEN, "%llu%s",
217888b7b0f2SMatthew Ahrens 		    (u_longlong_t)n, u);
217988b7b0f2SMatthew Ahrens 	}
218088b7b0f2SMatthew Ahrens }
218188b7b0f2SMatthew Ahrens 
218288b7b0f2SMatthew Ahrens /*
218388b7b0f2SMatthew Ahrens  * ::zfs_blkstats
218488b7b0f2SMatthew Ahrens  *
218588b7b0f2SMatthew Ahrens  * 	-v	print verbose per-level information
218688b7b0f2SMatthew Ahrens  *
218788b7b0f2SMatthew Ahrens  */
218888b7b0f2SMatthew Ahrens static int
218988b7b0f2SMatthew Ahrens zfs_blkstats(uintptr_t addr, uint_t flags, int argc, const mdb_arg_t *argv)
219088b7b0f2SMatthew Ahrens {
219188b7b0f2SMatthew Ahrens 	boolean_t verbose = B_FALSE;
219288b7b0f2SMatthew Ahrens 	zfs_all_blkstats_t stats;
219388b7b0f2SMatthew Ahrens 	dmu_object_type_t t;
219488b7b0f2SMatthew Ahrens 	zfs_blkstat_t *tzb;
219588b7b0f2SMatthew Ahrens 	uint64_t ditto;
219688b7b0f2SMatthew Ahrens 	dmu_object_type_info_t dmu_ot[DMU_OT_NUMTYPES + 10];
219788b7b0f2SMatthew Ahrens 	/* +10 in case it grew */
219888b7b0f2SMatthew Ahrens 
219988b7b0f2SMatthew Ahrens 	if (mdb_readvar(&dmu_ot, "dmu_ot") == -1) {
220088b7b0f2SMatthew Ahrens 		mdb_warn("failed to read 'dmu_ot'");
220188b7b0f2SMatthew Ahrens 		return (DCMD_ERR);
220288b7b0f2SMatthew Ahrens 	}
220388b7b0f2SMatthew Ahrens 
220488b7b0f2SMatthew Ahrens 	if (mdb_getopts(argc, argv,
220588b7b0f2SMatthew Ahrens 	    'v', MDB_OPT_SETBITS, TRUE, &verbose,
220688b7b0f2SMatthew Ahrens 	    NULL) != argc)
220788b7b0f2SMatthew Ahrens 		return (DCMD_USAGE);
220888b7b0f2SMatthew Ahrens 
220988b7b0f2SMatthew Ahrens 	if (!(flags & DCMD_ADDRSPEC))
221088b7b0f2SMatthew Ahrens 		return (DCMD_USAGE);
221188b7b0f2SMatthew Ahrens 
221228e4da25SMatthew Ahrens 	if (GETMEMB(addr, "spa", spa_dsl_pool, addr) ||
221328e4da25SMatthew Ahrens 	    GETMEMB(addr, "dsl_pool", dp_blkstats, addr) ||
221488b7b0f2SMatthew Ahrens 	    mdb_vread(&stats, sizeof (zfs_all_blkstats_t), addr) == -1) {
221588b7b0f2SMatthew Ahrens 		mdb_warn("failed to read data at %p;", addr);
221688b7b0f2SMatthew Ahrens 		mdb_printf("maybe no stats? run \"zpool scrub\" first.");
221788b7b0f2SMatthew Ahrens 		return (DCMD_ERR);
221888b7b0f2SMatthew Ahrens 	}
221988b7b0f2SMatthew Ahrens 
2220ad135b5dSChristopher Siden 	tzb = &stats.zab_type[DN_MAX_LEVELS][DMU_OT_TOTAL];
222188b7b0f2SMatthew Ahrens 	if (tzb->zb_gangs != 0) {
222288b7b0f2SMatthew Ahrens 		mdb_printf("Ganged blocks: %llu\n",
222388b7b0f2SMatthew Ahrens 		    (longlong_t)tzb->zb_gangs);
222488b7b0f2SMatthew Ahrens 	}
222588b7b0f2SMatthew Ahrens 
222688b7b0f2SMatthew Ahrens 	ditto = tzb->zb_ditto_2_of_2_samevdev + tzb->zb_ditto_2_of_3_samevdev +
222788b7b0f2SMatthew Ahrens 	    tzb->zb_ditto_3_of_3_samevdev;
222888b7b0f2SMatthew Ahrens 	if (ditto != 0) {
222988b7b0f2SMatthew Ahrens 		mdb_printf("Dittoed blocks on same vdev: %llu\n",
223088b7b0f2SMatthew Ahrens 		    (longlong_t)ditto);
223188b7b0f2SMatthew Ahrens 	}
223288b7b0f2SMatthew Ahrens 
223388b7b0f2SMatthew Ahrens 	mdb_printf("\nBlocks\tLSIZE\tPSIZE\tASIZE"
223488b7b0f2SMatthew Ahrens 	    "\t  avg\t comp\t%%Total\tType\n");
223588b7b0f2SMatthew Ahrens 
2236ad135b5dSChristopher Siden 	for (t = 0; t <= DMU_OT_TOTAL; t++) {
223788b7b0f2SMatthew Ahrens 		char csize[NICENUM_BUFLEN], lsize[NICENUM_BUFLEN];
223888b7b0f2SMatthew Ahrens 		char psize[NICENUM_BUFLEN], asize[NICENUM_BUFLEN];
223988b7b0f2SMatthew Ahrens 		char avg[NICENUM_BUFLEN];
224088b7b0f2SMatthew Ahrens 		char comp[NICENUM_BUFLEN], pct[NICENUM_BUFLEN];
224188b7b0f2SMatthew Ahrens 		char typename[64];
224288b7b0f2SMatthew Ahrens 		int l;
224388b7b0f2SMatthew Ahrens 
224488b7b0f2SMatthew Ahrens 
224588b7b0f2SMatthew Ahrens 		if (t == DMU_OT_DEFERRED)
224688b7b0f2SMatthew Ahrens 			strcpy(typename, "deferred free");
2247ad135b5dSChristopher Siden 		else if (t == DMU_OT_OTHER)
2248ad135b5dSChristopher Siden 			strcpy(typename, "other");
224988b7b0f2SMatthew Ahrens 		else if (t == DMU_OT_TOTAL)
225088b7b0f2SMatthew Ahrens 			strcpy(typename, "Total");
225188b7b0f2SMatthew Ahrens 		else if (mdb_readstr(typename, sizeof (typename),
225288b7b0f2SMatthew Ahrens 		    (uintptr_t)dmu_ot[t].ot_name) == -1) {
225388b7b0f2SMatthew Ahrens 			mdb_warn("failed to read type name");
225488b7b0f2SMatthew Ahrens 			return (DCMD_ERR);
225588b7b0f2SMatthew Ahrens 		}
225688b7b0f2SMatthew Ahrens 
225788b7b0f2SMatthew Ahrens 		if (stats.zab_type[DN_MAX_LEVELS][t].zb_asize == 0)
225888b7b0f2SMatthew Ahrens 			continue;
225988b7b0f2SMatthew Ahrens 
226088b7b0f2SMatthew Ahrens 		for (l = -1; l < DN_MAX_LEVELS; l++) {
226188b7b0f2SMatthew Ahrens 			int level = (l == -1 ? DN_MAX_LEVELS : l);
226288b7b0f2SMatthew Ahrens 			zfs_blkstat_t *zb = &stats.zab_type[level][t];
226388b7b0f2SMatthew Ahrens 
226488b7b0f2SMatthew Ahrens 			if (zb->zb_asize == 0)
226588b7b0f2SMatthew Ahrens 				continue;
226688b7b0f2SMatthew Ahrens 
226788b7b0f2SMatthew Ahrens 			/*
226888b7b0f2SMatthew Ahrens 			 * Don't print each level unless requested.
226988b7b0f2SMatthew Ahrens 			 */
227088b7b0f2SMatthew Ahrens 			if (!verbose && level != DN_MAX_LEVELS)
227188b7b0f2SMatthew Ahrens 				continue;
227288b7b0f2SMatthew Ahrens 
227388b7b0f2SMatthew Ahrens 			/*
227488b7b0f2SMatthew Ahrens 			 * If all the space is level 0, don't print the
227588b7b0f2SMatthew Ahrens 			 * level 0 separately.
227688b7b0f2SMatthew Ahrens 			 */
227788b7b0f2SMatthew Ahrens 			if (level == 0 && zb->zb_asize ==
227888b7b0f2SMatthew Ahrens 			    stats.zab_type[DN_MAX_LEVELS][t].zb_asize)
227988b7b0f2SMatthew Ahrens 				continue;
228088b7b0f2SMatthew Ahrens 
228188b7b0f2SMatthew Ahrens 			mdb_nicenum(zb->zb_count, csize);
228288b7b0f2SMatthew Ahrens 			mdb_nicenum(zb->zb_lsize, lsize);
228388b7b0f2SMatthew Ahrens 			mdb_nicenum(zb->zb_psize, psize);
228488b7b0f2SMatthew Ahrens 			mdb_nicenum(zb->zb_asize, asize);
228588b7b0f2SMatthew Ahrens 			mdb_nicenum(zb->zb_asize / zb->zb_count, avg);
2286ff64c0f7SMatthew Ahrens 			(void) snprintfrac(comp, NICENUM_BUFLEN,
2287ff64c0f7SMatthew Ahrens 			    zb->zb_lsize, zb->zb_psize, 2);
2288ff64c0f7SMatthew Ahrens 			(void) snprintfrac(pct, NICENUM_BUFLEN,
2289ff64c0f7SMatthew Ahrens 			    100 * zb->zb_asize, tzb->zb_asize, 2);
229088b7b0f2SMatthew Ahrens 
229188b7b0f2SMatthew Ahrens 			mdb_printf("%6s\t%5s\t%5s\t%5s\t%5s"
229288b7b0f2SMatthew Ahrens 			    "\t%5s\t%6s\t",
229388b7b0f2SMatthew Ahrens 			    csize, lsize, psize, asize, avg, comp, pct);
229488b7b0f2SMatthew Ahrens 
229588b7b0f2SMatthew Ahrens 			if (level == DN_MAX_LEVELS)
229688b7b0f2SMatthew Ahrens 				mdb_printf("%s\n", typename);
229788b7b0f2SMatthew Ahrens 			else
229888b7b0f2SMatthew Ahrens 				mdb_printf("  L%d %s\n",
229988b7b0f2SMatthew Ahrens 				    level, typename);
230088b7b0f2SMatthew Ahrens 		}
230188b7b0f2SMatthew Ahrens 	}
230288b7b0f2SMatthew Ahrens 
230388b7b0f2SMatthew Ahrens 	return (DCMD_OK);
230488b7b0f2SMatthew Ahrens }
230588b7b0f2SMatthew Ahrens 
2306d5ee8a13SMatthew Ahrens typedef struct mdb_reference {
2307d5ee8a13SMatthew Ahrens 	uintptr_t ref_holder;
2308d5ee8a13SMatthew Ahrens 	uintptr_t ref_removed;
2309d5ee8a13SMatthew Ahrens 	uint64_t ref_number;
2310d5ee8a13SMatthew Ahrens } mdb_reference_t;
2311d5ee8a13SMatthew Ahrens 
23129966ca11SMatthew Ahrens /* ARGSUSED */
23139966ca11SMatthew Ahrens static int
23149966ca11SMatthew Ahrens reference_cb(uintptr_t addr, const void *ignored, void *arg)
23159966ca11SMatthew Ahrens {
2316d5ee8a13SMatthew Ahrens 	mdb_reference_t ref;
23173f9d6ad7SLin Ling 	boolean_t holder_is_str = B_FALSE;
23189966ca11SMatthew Ahrens 	char holder_str[128];
23199966ca11SMatthew Ahrens 	boolean_t removed = (boolean_t)arg;
23209966ca11SMatthew Ahrens 
2321d5ee8a13SMatthew Ahrens 	if (mdb_ctf_vread(&ref, "reference_t", "mdb_reference_t", addr,
2322d5ee8a13SMatthew Ahrens 	    0) == -1)
2323d5ee8a13SMatthew Ahrens 		return (DCMD_ERR);
23249966ca11SMatthew Ahrens 
2325d5ee8a13SMatthew Ahrens 	if (mdb_readstr(holder_str, sizeof (holder_str),
2326d5ee8a13SMatthew Ahrens 	    ref.ref_holder) != -1)
23273f9d6ad7SLin Ling 		holder_is_str = strisprint(holder_str);
23289966ca11SMatthew Ahrens 
23299966ca11SMatthew Ahrens 	if (removed)
23309966ca11SMatthew Ahrens 		mdb_printf("removed ");
23319966ca11SMatthew Ahrens 	mdb_printf("reference ");
2332d5ee8a13SMatthew Ahrens 	if (ref.ref_number != 1)
2333d5ee8a13SMatthew Ahrens 		mdb_printf("with count=%llu ", ref.ref_number);
2334d5ee8a13SMatthew Ahrens 	mdb_printf("with tag %lx", ref.ref_holder);
23359966ca11SMatthew Ahrens 	if (holder_is_str)
23369966ca11SMatthew Ahrens 		mdb_printf(" \"%s\"", holder_str);
23379966ca11SMatthew Ahrens 	mdb_printf(", held at:\n");
23389966ca11SMatthew Ahrens 
23399966ca11SMatthew Ahrens 	(void) mdb_call_dcmd("whatis", addr, DCMD_ADDRSPEC, 0, NULL);
23409966ca11SMatthew Ahrens 
23419966ca11SMatthew Ahrens 	if (removed) {
23429966ca11SMatthew Ahrens 		mdb_printf("removed at:\n");
2343d5ee8a13SMatthew Ahrens 		(void) mdb_call_dcmd("whatis", ref.ref_removed,
23449966ca11SMatthew Ahrens 		    DCMD_ADDRSPEC, 0, NULL);
23459966ca11SMatthew Ahrens 	}
23469966ca11SMatthew Ahrens 
23479966ca11SMatthew Ahrens 	mdb_printf("\n");
23489966ca11SMatthew Ahrens 
23499966ca11SMatthew Ahrens 	return (WALK_NEXT);
23509966ca11SMatthew Ahrens }
23519966ca11SMatthew Ahrens 
2352d5ee8a13SMatthew Ahrens typedef struct mdb_refcount {
2353d5ee8a13SMatthew Ahrens 	uint64_t rc_count;
2354d5ee8a13SMatthew Ahrens } mdb_refcount_t;
2355d5ee8a13SMatthew Ahrens 
2356d5ee8a13SMatthew Ahrens typedef struct mdb_refcount_removed {
2357d5ee8a13SMatthew Ahrens 	uint64_t rc_removed_count;
2358d5ee8a13SMatthew Ahrens } mdb_refcount_removed_t;
2359d5ee8a13SMatthew Ahrens 
2360d5ee8a13SMatthew Ahrens typedef struct mdb_refcount_tracked {
2361d5ee8a13SMatthew Ahrens 	boolean_t rc_tracked;
2362d5ee8a13SMatthew Ahrens } mdb_refcount_tracked_t;
2363d5ee8a13SMatthew Ahrens 
23649966ca11SMatthew Ahrens /* ARGSUSED */
23659966ca11SMatthew Ahrens static int
23669966ca11SMatthew Ahrens refcount(uintptr_t addr, uint_t flags, int argc, const mdb_arg_t *argv)
23679966ca11SMatthew Ahrens {
2368d5ee8a13SMatthew Ahrens 	mdb_refcount_t rc;
2369d5ee8a13SMatthew Ahrens 	mdb_refcount_removed_t rcr;
2370d5ee8a13SMatthew Ahrens 	mdb_refcount_tracked_t rct;
2371d5ee8a13SMatthew Ahrens 	int off;
237228e4da25SMatthew Ahrens 	boolean_t released = B_FALSE;
23739966ca11SMatthew Ahrens 
23749966ca11SMatthew Ahrens 	if (!(flags & DCMD_ADDRSPEC))
23759966ca11SMatthew Ahrens 		return (DCMD_USAGE);
23769966ca11SMatthew Ahrens 
237728e4da25SMatthew Ahrens 	if (mdb_getopts(argc, argv,
237828e4da25SMatthew Ahrens 	    'r', MDB_OPT_SETBITS, B_TRUE, &released,
237928e4da25SMatthew Ahrens 	    NULL) != argc)
238028e4da25SMatthew Ahrens 		return (DCMD_USAGE);
238128e4da25SMatthew Ahrens 
2382d5ee8a13SMatthew Ahrens 	if (mdb_ctf_vread(&rc, "refcount_t", "mdb_refcount_t", addr,
2383d5ee8a13SMatthew Ahrens 	    0) == -1)
23849966ca11SMatthew Ahrens 		return (DCMD_ERR);
23859966ca11SMatthew Ahrens 
2386d5ee8a13SMatthew Ahrens 	if (mdb_ctf_vread(&rcr, "refcount_t", "mdb_refcount_removed_t", addr,
2387d5ee8a13SMatthew Ahrens 	    MDB_CTF_VREAD_QUIET) == -1) {
2388d5ee8a13SMatthew Ahrens 		mdb_printf("refcount_t at %p has %llu holds (untracked)\n",
2389d5ee8a13SMatthew Ahrens 		    addr, (longlong_t)rc.rc_count);
239028e4da25SMatthew Ahrens 		return (DCMD_OK);
239128e4da25SMatthew Ahrens 	}
239228e4da25SMatthew Ahrens 
2393d5ee8a13SMatthew Ahrens 	if (mdb_ctf_vread(&rct, "refcount_t", "mdb_refcount_tracked_t", addr,
2394d5ee8a13SMatthew Ahrens 	    MDB_CTF_VREAD_QUIET) == -1) {
2395d5ee8a13SMatthew Ahrens 		/* If this is an old target, it might be tracked. */
2396d5ee8a13SMatthew Ahrens 		rct.rc_tracked = B_TRUE;
2397d5ee8a13SMatthew Ahrens 	}
2398d5ee8a13SMatthew Ahrens 
23999966ca11SMatthew Ahrens 	mdb_printf("refcount_t at %p has %llu current holds, "
24009966ca11SMatthew Ahrens 	    "%llu recently released holds\n",
2401d5ee8a13SMatthew Ahrens 	    addr, (longlong_t)rc.rc_count, (longlong_t)rcr.rc_removed_count);
24029966ca11SMatthew Ahrens 
2403d5ee8a13SMatthew Ahrens 	if (rct.rc_tracked && rc.rc_count > 0)
24049966ca11SMatthew Ahrens 		mdb_printf("current holds:\n");
2405d5ee8a13SMatthew Ahrens 	off = mdb_ctf_offsetof_by_name("refcount_t", "rc_list");
2406d5ee8a13SMatthew Ahrens 	if (off == -1)
24079966ca11SMatthew Ahrens 		return (DCMD_ERR);
2408d5ee8a13SMatthew Ahrens 	mdb_pwalk("list", reference_cb, (void*)B_FALSE, addr + off);
2409d5ee8a13SMatthew Ahrens 
2410d5ee8a13SMatthew Ahrens 	if (released && rcr.rc_removed_count > 0) {
2411d5ee8a13SMatthew Ahrens 		mdb_printf("released holds:\n");
24129966ca11SMatthew Ahrens 
2413d5ee8a13SMatthew Ahrens 		off = mdb_ctf_offsetof_by_name("refcount_t", "rc_removed");
2414d5ee8a13SMatthew Ahrens 		if (off == -1)
241528e4da25SMatthew Ahrens 			return (DCMD_ERR);
2416d5ee8a13SMatthew Ahrens 		mdb_pwalk("list", reference_cb, (void*)B_FALSE, addr + off);
241728e4da25SMatthew Ahrens 	}
24189966ca11SMatthew Ahrens 
24199966ca11SMatthew Ahrens 	return (DCMD_OK);
24209966ca11SMatthew Ahrens }
24219966ca11SMatthew Ahrens 
24220a586ceaSMark Shellenbaum /* ARGSUSED */
24230a586ceaSMark Shellenbaum static int
24240a586ceaSMark Shellenbaum sa_attr_table(uintptr_t addr, uint_t flags, int argc, const mdb_arg_t *argv)
24250a586ceaSMark Shellenbaum {
24260a586ceaSMark Shellenbaum 	sa_attr_table_t *table;
24270a586ceaSMark Shellenbaum 	sa_os_t sa_os;
24280a586ceaSMark Shellenbaum 	char *name;
24290a586ceaSMark Shellenbaum 	int i;
24300a586ceaSMark Shellenbaum 
24310a586ceaSMark Shellenbaum 	if (mdb_vread(&sa_os, sizeof (sa_os_t), addr) == -1) {
24320a586ceaSMark Shellenbaum 		mdb_warn("failed to read sa_os at %p", addr);
24330a586ceaSMark Shellenbaum 		return (DCMD_ERR);
24340a586ceaSMark Shellenbaum 	}
24350a586ceaSMark Shellenbaum 
24360a586ceaSMark Shellenbaum 	table = mdb_alloc(sizeof (sa_attr_table_t) * sa_os.sa_num_attrs,
24370a586ceaSMark Shellenbaum 	    UM_SLEEP | UM_GC);
24380a586ceaSMark Shellenbaum 	name = mdb_alloc(MAXPATHLEN, UM_SLEEP | UM_GC);
24390a586ceaSMark Shellenbaum 
24400a586ceaSMark Shellenbaum 	if (mdb_vread(table, sizeof (sa_attr_table_t) * sa_os.sa_num_attrs,
24410a586ceaSMark Shellenbaum 	    (uintptr_t)sa_os.sa_attr_table) == -1) {
24420a586ceaSMark Shellenbaum 		mdb_warn("failed to read sa_os at %p", addr);
24430a586ceaSMark Shellenbaum 		return (DCMD_ERR);
24440a586ceaSMark Shellenbaum 	}
24450a586ceaSMark Shellenbaum 
24460a586ceaSMark Shellenbaum 	mdb_printf("%<u>%-10s %-10s %-10s %-10s %s%</u>\n",
24470a586ceaSMark Shellenbaum 	    "ATTR ID", "REGISTERED", "LENGTH", "BSWAP", "NAME");
24480a586ceaSMark Shellenbaum 	for (i = 0; i != sa_os.sa_num_attrs; i++) {
24490a586ceaSMark Shellenbaum 		mdb_readstr(name, MAXPATHLEN, (uintptr_t)table[i].sa_name);
24500a586ceaSMark Shellenbaum 		mdb_printf("%5x   %8x %8x %8x          %-s\n",
24510a586ceaSMark Shellenbaum 		    (int)table[i].sa_attr, (int)table[i].sa_registered,
24520a586ceaSMark Shellenbaum 		    (int)table[i].sa_length, table[i].sa_byteswap, name);
24530a586ceaSMark Shellenbaum 	}
24540a586ceaSMark Shellenbaum 
24550a586ceaSMark Shellenbaum 	return (DCMD_OK);
24560a586ceaSMark Shellenbaum }
24570a586ceaSMark Shellenbaum 
24580a586ceaSMark Shellenbaum static int
24590a586ceaSMark Shellenbaum sa_get_off_table(uintptr_t addr, uint32_t **off_tab, int attr_count)
24600a586ceaSMark Shellenbaum {
24610a586ceaSMark Shellenbaum 	uintptr_t idx_table;
24620a586ceaSMark Shellenbaum 
246328e4da25SMatthew Ahrens 	if (GETMEMB(addr, "sa_idx_tab", sa_idx_tab, idx_table)) {
24640a586ceaSMark Shellenbaum 		mdb_printf("can't find offset table in sa_idx_tab\n");
24650a586ceaSMark Shellenbaum 		return (-1);
24660a586ceaSMark Shellenbaum 	}
24670a586ceaSMark Shellenbaum 
24680a586ceaSMark Shellenbaum 	*off_tab = mdb_alloc(attr_count * sizeof (uint32_t),
24690a586ceaSMark Shellenbaum 	    UM_SLEEP | UM_GC);
24700a586ceaSMark Shellenbaum 
24710a586ceaSMark Shellenbaum 	if (mdb_vread(*off_tab,
24720a586ceaSMark Shellenbaum 	    attr_count * sizeof (uint32_t), idx_table) == -1) {
24730a586ceaSMark Shellenbaum 		mdb_warn("failed to attribute offset table %p", idx_table);
24740a586ceaSMark Shellenbaum 		return (-1);
24750a586ceaSMark Shellenbaum 	}
24760a586ceaSMark Shellenbaum 
24770a586ceaSMark Shellenbaum 	return (DCMD_OK);
24780a586ceaSMark Shellenbaum }
24790a586ceaSMark Shellenbaum 
24800a586ceaSMark Shellenbaum /*ARGSUSED*/
24810a586ceaSMark Shellenbaum static int
24820a586ceaSMark Shellenbaum sa_attr_print(uintptr_t addr, uint_t flags, int argc, const mdb_arg_t *argv)
24830a586ceaSMark Shellenbaum {
24840a586ceaSMark Shellenbaum 	uint32_t *offset_tab;
24850a586ceaSMark Shellenbaum 	int attr_count;
24860a586ceaSMark Shellenbaum 	uint64_t attr_id;
24870a586ceaSMark Shellenbaum 	uintptr_t attr_addr;
24880a586ceaSMark Shellenbaum 	uintptr_t bonus_tab, spill_tab;
24890a586ceaSMark Shellenbaum 	uintptr_t db_bonus, db_spill;
24900a586ceaSMark Shellenbaum 	uintptr_t os, os_sa;
24910a586ceaSMark Shellenbaum 	uintptr_t db_data;
24920a586ceaSMark Shellenbaum 
24930a586ceaSMark Shellenbaum 	if (argc != 1)
24940a586ceaSMark Shellenbaum 		return (DCMD_USAGE);
24950a586ceaSMark Shellenbaum 
24960a586ceaSMark Shellenbaum 	if (argv[0].a_type == MDB_TYPE_STRING)
24970a586ceaSMark Shellenbaum 		attr_id = mdb_strtoull(argv[0].a_un.a_str);
24980a586ceaSMark Shellenbaum 	else
24990a586ceaSMark Shellenbaum 		return (DCMD_USAGE);
25000a586ceaSMark Shellenbaum 
250128e4da25SMatthew Ahrens 	if (GETMEMB(addr, "sa_handle", sa_bonus_tab, bonus_tab) ||
250228e4da25SMatthew Ahrens 	    GETMEMB(addr, "sa_handle", sa_spill_tab, spill_tab) ||
250328e4da25SMatthew Ahrens 	    GETMEMB(addr, "sa_handle", sa_os, os) ||
250428e4da25SMatthew Ahrens 	    GETMEMB(addr, "sa_handle", sa_bonus, db_bonus) ||
250528e4da25SMatthew Ahrens 	    GETMEMB(addr, "sa_handle", sa_spill, db_spill)) {
25060a586ceaSMark Shellenbaum 		mdb_printf("Can't find necessary information in sa_handle "
25070a586ceaSMark Shellenbaum 		    "in sa_handle\n");
25080a586ceaSMark Shellenbaum 		return (DCMD_ERR);
25090a586ceaSMark Shellenbaum 	}
25100a586ceaSMark Shellenbaum 
251128e4da25SMatthew Ahrens 	if (GETMEMB(os, "objset", os_sa, os_sa)) {
25120a586ceaSMark Shellenbaum 		mdb_printf("Can't find os_sa in objset\n");
25130a586ceaSMark Shellenbaum 		return (DCMD_ERR);
25140a586ceaSMark Shellenbaum 	}
25150a586ceaSMark Shellenbaum 
251628e4da25SMatthew Ahrens 	if (GETMEMB(os_sa, "sa_os", sa_num_attrs, attr_count)) {
25170a586ceaSMark Shellenbaum 		mdb_printf("Can't find sa_num_attrs\n");
25180a586ceaSMark Shellenbaum 		return (DCMD_ERR);
25190a586ceaSMark Shellenbaum 	}
25200a586ceaSMark Shellenbaum 
25210a586ceaSMark Shellenbaum 	if (attr_id > attr_count) {
25220a586ceaSMark Shellenbaum 		mdb_printf("attribute id number is out of range\n");
25230a586ceaSMark Shellenbaum 		return (DCMD_ERR);
25240a586ceaSMark Shellenbaum 	}
25250a586ceaSMark Shellenbaum 
25260a586ceaSMark Shellenbaum 	if (bonus_tab) {
25270a586ceaSMark Shellenbaum 		if (sa_get_off_table(bonus_tab, &offset_tab,
25280a586ceaSMark Shellenbaum 		    attr_count) == -1) {
25290a586ceaSMark Shellenbaum 			return (DCMD_ERR);
25300a586ceaSMark Shellenbaum 		}
25310a586ceaSMark Shellenbaum 
253228e4da25SMatthew Ahrens 		if (GETMEMB(db_bonus, "dmu_buf", db_data, db_data)) {
25330a586ceaSMark Shellenbaum 			mdb_printf("can't find db_data in bonus dbuf\n");
25340a586ceaSMark Shellenbaum 			return (DCMD_ERR);
25350a586ceaSMark Shellenbaum 		}
25360a586ceaSMark Shellenbaum 	}
25370a586ceaSMark Shellenbaum 
25380a586ceaSMark Shellenbaum 	if (bonus_tab && !TOC_ATTR_PRESENT(offset_tab[attr_id]) &&
25390a586ceaSMark Shellenbaum 	    spill_tab == NULL) {
25400a586ceaSMark Shellenbaum 		mdb_printf("Attribute does not exist\n");
25410a586ceaSMark Shellenbaum 		return (DCMD_ERR);
25420a586ceaSMark Shellenbaum 	} else if (!TOC_ATTR_PRESENT(offset_tab[attr_id]) && spill_tab) {
25430a586ceaSMark Shellenbaum 		if (sa_get_off_table(spill_tab, &offset_tab,
25440a586ceaSMark Shellenbaum 		    attr_count) == -1) {
25450a586ceaSMark Shellenbaum 			return (DCMD_ERR);
25460a586ceaSMark Shellenbaum 		}
254728e4da25SMatthew Ahrens 		if (GETMEMB(db_spill, "dmu_buf", db_data, db_data)) {
25480a586ceaSMark Shellenbaum 			mdb_printf("can't find db_data in spill dbuf\n");
25490a586ceaSMark Shellenbaum 			return (DCMD_ERR);
25500a586ceaSMark Shellenbaum 		}
25510a586ceaSMark Shellenbaum 		if (!TOC_ATTR_PRESENT(offset_tab[attr_id])) {
25520a586ceaSMark Shellenbaum 			mdb_printf("Attribute does not exist\n");
25530a586ceaSMark Shellenbaum 			return (DCMD_ERR);
25540a586ceaSMark Shellenbaum 		}
25550a586ceaSMark Shellenbaum 	}
25560a586ceaSMark Shellenbaum 	attr_addr = db_data + TOC_OFF(offset_tab[attr_id]);
25570a586ceaSMark Shellenbaum 	mdb_printf("%p\n", attr_addr);
25580a586ceaSMark Shellenbaum 	return (DCMD_OK);
25590a586ceaSMark Shellenbaum }
25600a586ceaSMark Shellenbaum 
25610a586ceaSMark Shellenbaum /* ARGSUSED */
25620a586ceaSMark Shellenbaum static int
25630a586ceaSMark Shellenbaum zfs_ace_print_common(uintptr_t addr, uint_t flags,
25640a586ceaSMark Shellenbaum     uint64_t id, uint32_t access_mask, uint16_t ace_flags,
25650a586ceaSMark Shellenbaum     uint16_t ace_type, int verbose)
25660a586ceaSMark Shellenbaum {
25670a586ceaSMark Shellenbaum 	if (DCMD_HDRSPEC(flags) && !verbose)
25680a586ceaSMark Shellenbaum 		mdb_printf("%<u>%-?s %-8s %-8s %-8s %s%</u>\n",
25690a586ceaSMark Shellenbaum 		    "ADDR", "FLAGS", "MASK", "TYPE", "ID");
25700a586ceaSMark Shellenbaum 
25710a586ceaSMark Shellenbaum 	if (!verbose) {
25720a586ceaSMark Shellenbaum 		mdb_printf("%0?p %-8x %-8x %-8x %-llx\n", addr,
25730a586ceaSMark Shellenbaum 		    ace_flags, access_mask, ace_type, id);
25740a586ceaSMark Shellenbaum 		return (DCMD_OK);
25750a586ceaSMark Shellenbaum 	}
25760a586ceaSMark Shellenbaum 
25770a586ceaSMark Shellenbaum 	switch (ace_flags & ACE_TYPE_FLAGS) {
25780a586ceaSMark Shellenbaum 	case ACE_OWNER:
25790a586ceaSMark Shellenbaum 		mdb_printf("owner@:");
25800a586ceaSMark Shellenbaum 		break;
25810a586ceaSMark Shellenbaum 	case (ACE_IDENTIFIER_GROUP | ACE_GROUP):
25820a586ceaSMark Shellenbaum 		mdb_printf("group@:");
25830a586ceaSMark Shellenbaum 		break;
25840a586ceaSMark Shellenbaum 	case ACE_EVERYONE:
25850a586ceaSMark Shellenbaum 		mdb_printf("everyone@:");
25860a586ceaSMark Shellenbaum 		break;
25870a586ceaSMark Shellenbaum 	case ACE_IDENTIFIER_GROUP:
25880a586ceaSMark Shellenbaum 		mdb_printf("group:%llx:", (u_longlong_t)id);
25890a586ceaSMark Shellenbaum 		break;
25900a586ceaSMark Shellenbaum 	case 0: /* User entry */
25910a586ceaSMark Shellenbaum 		mdb_printf("user:%llx:", (u_longlong_t)id);
25920a586ceaSMark Shellenbaum 		break;
25930a586ceaSMark Shellenbaum 	}
25940a586ceaSMark Shellenbaum 
25950a586ceaSMark Shellenbaum 	/* print out permission mask */
25960a586ceaSMark Shellenbaum 	if (access_mask & ACE_READ_DATA)
25970a586ceaSMark Shellenbaum 		mdb_printf("r");
25980a586ceaSMark Shellenbaum 	else
25990a586ceaSMark Shellenbaum 		mdb_printf("-");
26000a586ceaSMark Shellenbaum 	if (access_mask & ACE_WRITE_DATA)
26010a586ceaSMark Shellenbaum 		mdb_printf("w");
26020a586ceaSMark Shellenbaum 	else
26030a586ceaSMark Shellenbaum 		mdb_printf("-");
26040a586ceaSMark Shellenbaum 	if (access_mask & ACE_EXECUTE)
26050a586ceaSMark Shellenbaum 		mdb_printf("x");
26060a586ceaSMark Shellenbaum 	else
26070a586ceaSMark Shellenbaum 		mdb_printf("-");
26080a586ceaSMark Shellenbaum 	if (access_mask & ACE_APPEND_DATA)
26090a586ceaSMark Shellenbaum 		mdb_printf("p");
26100a586ceaSMark Shellenbaum 	else
26110a586ceaSMark Shellenbaum 		mdb_printf("-");
26120a586ceaSMark Shellenbaum 	if (access_mask & ACE_DELETE)
26130a586ceaSMark Shellenbaum 		mdb_printf("d");
26140a586ceaSMark Shellenbaum 	else
26150a586ceaSMark Shellenbaum 		mdb_printf("-");
26160a586ceaSMark Shellenbaum 	if (access_mask & ACE_DELETE_CHILD)
26170a586ceaSMark Shellenbaum 		mdb_printf("D");
26180a586ceaSMark Shellenbaum 	else
26190a586ceaSMark Shellenbaum 		mdb_printf("-");
26200a586ceaSMark Shellenbaum 	if (access_mask & ACE_READ_ATTRIBUTES)
26210a586ceaSMark Shellenbaum 		mdb_printf("a");
26220a586ceaSMark Shellenbaum 	else
26230a586ceaSMark Shellenbaum 		mdb_printf("-");
26240a586ceaSMark Shellenbaum 	if (access_mask & ACE_WRITE_ATTRIBUTES)
26250a586ceaSMark Shellenbaum 		mdb_printf("A");
26260a586ceaSMark Shellenbaum 	else
26270a586ceaSMark Shellenbaum 		mdb_printf("-");
26280a586ceaSMark Shellenbaum 	if (access_mask & ACE_READ_NAMED_ATTRS)
26290a586ceaSMark Shellenbaum 		mdb_printf("R");
26300a586ceaSMark Shellenbaum 	else
26310a586ceaSMark Shellenbaum 		mdb_printf("-");
26320a586ceaSMark Shellenbaum 	if (access_mask & ACE_WRITE_NAMED_ATTRS)
26330a586ceaSMark Shellenbaum 		mdb_printf("W");
26340a586ceaSMark Shellenbaum 	else
26350a586ceaSMark Shellenbaum 		mdb_printf("-");
26360a586ceaSMark Shellenbaum 	if (access_mask & ACE_READ_ACL)
26370a586ceaSMark Shellenbaum 		mdb_printf("c");
26380a586ceaSMark Shellenbaum 	else
26390a586ceaSMark Shellenbaum 		mdb_printf("-");
26400a586ceaSMark Shellenbaum 	if (access_mask & ACE_WRITE_ACL)
26410a586ceaSMark Shellenbaum 		mdb_printf("C");
26420a586ceaSMark Shellenbaum 	else
26430a586ceaSMark Shellenbaum 		mdb_printf("-");
26440a586ceaSMark Shellenbaum 	if (access_mask & ACE_WRITE_OWNER)
26450a586ceaSMark Shellenbaum 		mdb_printf("o");
26460a586ceaSMark Shellenbaum 	else
26470a586ceaSMark Shellenbaum 		mdb_printf("-");
26480a586ceaSMark Shellenbaum 	if (access_mask & ACE_SYNCHRONIZE)
26490a586ceaSMark Shellenbaum 		mdb_printf("s");
26500a586ceaSMark Shellenbaum 	else
26510a586ceaSMark Shellenbaum 		mdb_printf("-");
26520a586ceaSMark Shellenbaum 
26530a586ceaSMark Shellenbaum 	mdb_printf(":");
26540a586ceaSMark Shellenbaum 
26550a586ceaSMark Shellenbaum 	/* Print out inheritance flags */
26560a586ceaSMark Shellenbaum 	if (ace_flags & ACE_FILE_INHERIT_ACE)
26570a586ceaSMark Shellenbaum 		mdb_printf("f");
26580a586ceaSMark Shellenbaum 	else
26590a586ceaSMark Shellenbaum 		mdb_printf("-");
26600a586ceaSMark Shellenbaum 	if (ace_flags & ACE_DIRECTORY_INHERIT_ACE)
26610a586ceaSMark Shellenbaum 		mdb_printf("d");
26620a586ceaSMark Shellenbaum 	else
26630a586ceaSMark Shellenbaum 		mdb_printf("-");
26640a586ceaSMark Shellenbaum 	if (ace_flags & ACE_INHERIT_ONLY_ACE)
26650a586ceaSMark Shellenbaum 		mdb_printf("i");
26660a586ceaSMark Shellenbaum 	else
26670a586ceaSMark Shellenbaum 		mdb_printf("-");
26680a586ceaSMark Shellenbaum 	if (ace_flags & ACE_NO_PROPAGATE_INHERIT_ACE)
26690a586ceaSMark Shellenbaum 		mdb_printf("n");
26700a586ceaSMark Shellenbaum 	else
26710a586ceaSMark Shellenbaum 		mdb_printf("-");
26720a586ceaSMark Shellenbaum 	if (ace_flags & ACE_SUCCESSFUL_ACCESS_ACE_FLAG)
26730a586ceaSMark Shellenbaum 		mdb_printf("S");
26740a586ceaSMark Shellenbaum 	else
26750a586ceaSMark Shellenbaum 		mdb_printf("-");
26760a586ceaSMark Shellenbaum 	if (ace_flags & ACE_FAILED_ACCESS_ACE_FLAG)
26770a586ceaSMark Shellenbaum 		mdb_printf("F");
26780a586ceaSMark Shellenbaum 	else
26790a586ceaSMark Shellenbaum 		mdb_printf("-");
26800a586ceaSMark Shellenbaum 	if (ace_flags & ACE_INHERITED_ACE)
26810a586ceaSMark Shellenbaum 		mdb_printf("I");
26820a586ceaSMark Shellenbaum 	else
26830a586ceaSMark Shellenbaum 		mdb_printf("-");
26840a586ceaSMark Shellenbaum 
26850a586ceaSMark Shellenbaum 	switch (ace_type) {
26860a586ceaSMark Shellenbaum 	case ACE_ACCESS_ALLOWED_ACE_TYPE:
26870a586ceaSMark Shellenbaum 		mdb_printf(":allow\n");
26880a586ceaSMark Shellenbaum 		break;
26890a586ceaSMark Shellenbaum 	case ACE_ACCESS_DENIED_ACE_TYPE:
26900a586ceaSMark Shellenbaum 		mdb_printf(":deny\n");
26910a586ceaSMark Shellenbaum 		break;
26920a586ceaSMark Shellenbaum 	case ACE_SYSTEM_AUDIT_ACE_TYPE:
26930a586ceaSMark Shellenbaum 		mdb_printf(":audit\n");
26940a586ceaSMark Shellenbaum 		break;
26950a586ceaSMark Shellenbaum 	case ACE_SYSTEM_ALARM_ACE_TYPE:
26960a586ceaSMark Shellenbaum 		mdb_printf(":alarm\n");
26970a586ceaSMark Shellenbaum 		break;
26980a586ceaSMark Shellenbaum 	default:
26990a586ceaSMark Shellenbaum 		mdb_printf(":?\n");
27000a586ceaSMark Shellenbaum 	}
27010a586ceaSMark Shellenbaum 	return (DCMD_OK);
27020a586ceaSMark Shellenbaum }
27030a586ceaSMark Shellenbaum 
27040a586ceaSMark Shellenbaum /* ARGSUSED */
27050a586ceaSMark Shellenbaum static int
27060a586ceaSMark Shellenbaum zfs_ace_print(uintptr_t addr, uint_t flags, int argc, const mdb_arg_t *argv)
27070a586ceaSMark Shellenbaum {
27080a586ceaSMark Shellenbaum 	zfs_ace_t zace;
27090a586ceaSMark Shellenbaum 	int verbose = FALSE;
27100a586ceaSMark Shellenbaum 	uint64_t id;
27110a586ceaSMark Shellenbaum 
27120a586ceaSMark Shellenbaum 	if (!(flags & DCMD_ADDRSPEC))
27130a586ceaSMark Shellenbaum 		return (DCMD_USAGE);
27140a586ceaSMark Shellenbaum 
27150a586ceaSMark Shellenbaum 	if (mdb_getopts(argc, argv,
27160a586ceaSMark Shellenbaum 	    'v', MDB_OPT_SETBITS, TRUE, &verbose, TRUE, NULL) != argc)
27170a586ceaSMark Shellenbaum 		return (DCMD_USAGE);
27180a586ceaSMark Shellenbaum 
27190a586ceaSMark Shellenbaum 	if (mdb_vread(&zace, sizeof (zfs_ace_t), addr) == -1) {
27200a586ceaSMark Shellenbaum 		mdb_warn("failed to read zfs_ace_t");
27210a586ceaSMark Shellenbaum 		return (DCMD_ERR);
27220a586ceaSMark Shellenbaum 	}
27230a586ceaSMark Shellenbaum 
27240a586ceaSMark Shellenbaum 	if ((zace.z_hdr.z_flags & ACE_TYPE_FLAGS) == 0 ||
27250a586ceaSMark Shellenbaum 	    (zace.z_hdr.z_flags & ACE_TYPE_FLAGS) == ACE_IDENTIFIER_GROUP)
27260a586ceaSMark Shellenbaum 		id = zace.z_fuid;
27270a586ceaSMark Shellenbaum 	else
27280a586ceaSMark Shellenbaum 		id = -1;
27290a586ceaSMark Shellenbaum 
27300a586ceaSMark Shellenbaum 	return (zfs_ace_print_common(addr, flags, id, zace.z_hdr.z_access_mask,
27310a586ceaSMark Shellenbaum 	    zace.z_hdr.z_flags, zace.z_hdr.z_type, verbose));
27320a586ceaSMark Shellenbaum }
27330a586ceaSMark Shellenbaum 
27340a586ceaSMark Shellenbaum /* ARGSUSED */
27350a586ceaSMark Shellenbaum static int
27360a586ceaSMark Shellenbaum zfs_ace0_print(uintptr_t addr, uint_t flags, int argc, const mdb_arg_t *argv)
27370a586ceaSMark Shellenbaum {
27380a586ceaSMark Shellenbaum 	ace_t ace;
27390a586ceaSMark Shellenbaum 	uint64_t id;
27400a586ceaSMark Shellenbaum 	int verbose = FALSE;
27410a586ceaSMark Shellenbaum 
27420a586ceaSMark Shellenbaum 	if (!(flags & DCMD_ADDRSPEC))
27430a586ceaSMark Shellenbaum 		return (DCMD_USAGE);
27440a586ceaSMark Shellenbaum 
27450a586ceaSMark Shellenbaum 	if (mdb_getopts(argc, argv,
27460a586ceaSMark Shellenbaum 	    'v', MDB_OPT_SETBITS, TRUE, &verbose, TRUE, NULL) != argc)
27470a586ceaSMark Shellenbaum 		return (DCMD_USAGE);
27480a586ceaSMark Shellenbaum 
27490a586ceaSMark Shellenbaum 	if (mdb_vread(&ace, sizeof (ace_t), addr) == -1) {
27500a586ceaSMark Shellenbaum 		mdb_warn("failed to read ace_t");
27510a586ceaSMark Shellenbaum 		return (DCMD_ERR);
27520a586ceaSMark Shellenbaum 	}
27530a586ceaSMark Shellenbaum 
27540a586ceaSMark Shellenbaum 	if ((ace.a_flags & ACE_TYPE_FLAGS) == 0 ||
27550a586ceaSMark Shellenbaum 	    (ace.a_flags & ACE_TYPE_FLAGS) == ACE_IDENTIFIER_GROUP)
27560a586ceaSMark Shellenbaum 		id = ace.a_who;
27570a586ceaSMark Shellenbaum 	else
27580a586ceaSMark Shellenbaum 		id = -1;
27590a586ceaSMark Shellenbaum 
27600a586ceaSMark Shellenbaum 	return (zfs_ace_print_common(addr, flags, id, ace.a_access_mask,
27610a586ceaSMark Shellenbaum 	    ace.a_flags, ace.a_type, verbose));
27620a586ceaSMark Shellenbaum }
27630a586ceaSMark Shellenbaum 
27640a586ceaSMark Shellenbaum typedef struct acl_dump_args {
27650a586ceaSMark Shellenbaum 	int a_argc;
27660a586ceaSMark Shellenbaum 	const mdb_arg_t *a_argv;
27670a586ceaSMark Shellenbaum 	uint16_t a_version;
27680a586ceaSMark Shellenbaum 	int a_flags;
27690a586ceaSMark Shellenbaum } acl_dump_args_t;
27700a586ceaSMark Shellenbaum 
27710a586ceaSMark Shellenbaum /* ARGSUSED */
27720a586ceaSMark Shellenbaum static int
27730a586ceaSMark Shellenbaum acl_aces_cb(uintptr_t addr, const void *unknown, void *arg)
27740a586ceaSMark Shellenbaum {
27750a586ceaSMark Shellenbaum 	acl_dump_args_t *acl_args = (acl_dump_args_t *)arg;
27760a586ceaSMark Shellenbaum 
27770a586ceaSMark Shellenbaum 	if (acl_args->a_version == 1) {
27780a586ceaSMark Shellenbaum 		if (mdb_call_dcmd("zfs_ace", addr,
27790a586ceaSMark Shellenbaum 		    DCMD_ADDRSPEC|acl_args->a_flags, acl_args->a_argc,
27800a586ceaSMark Shellenbaum 		    acl_args->a_argv) != DCMD_OK) {
27810a586ceaSMark Shellenbaum 			return (WALK_ERR);
27820a586ceaSMark Shellenbaum 		}
27830a586ceaSMark Shellenbaum 	} else {
27840a586ceaSMark Shellenbaum 		if (mdb_call_dcmd("zfs_ace0", addr,
27850a586ceaSMark Shellenbaum 		    DCMD_ADDRSPEC|acl_args->a_flags, acl_args->a_argc,
27860a586ceaSMark Shellenbaum 		    acl_args->a_argv) != DCMD_OK) {
27870a586ceaSMark Shellenbaum 			return (WALK_ERR);
27880a586ceaSMark Shellenbaum 		}
27890a586ceaSMark Shellenbaum 	}
27900a586ceaSMark Shellenbaum 	acl_args->a_flags = DCMD_LOOP;
27910a586ceaSMark Shellenbaum 	return (WALK_NEXT);
27920a586ceaSMark Shellenbaum }
27930a586ceaSMark Shellenbaum 
27940a586ceaSMark Shellenbaum /* ARGSUSED */
27950a586ceaSMark Shellenbaum static int
27960a586ceaSMark Shellenbaum acl_cb(uintptr_t addr, const void *unknown, void *arg)
27970a586ceaSMark Shellenbaum {
27980a586ceaSMark Shellenbaum 	acl_dump_args_t *acl_args = (acl_dump_args_t *)arg;
27990a586ceaSMark Shellenbaum 
28000a586ceaSMark Shellenbaum 	if (acl_args->a_version == 1) {
28010a586ceaSMark Shellenbaum 		if (mdb_pwalk("zfs_acl_node_aces", acl_aces_cb,
28020a586ceaSMark Shellenbaum 		    arg, addr) != 0) {
28030a586ceaSMark Shellenbaum 			mdb_warn("can't walk ACEs");
28040a586ceaSMark Shellenbaum 			return (DCMD_ERR);
28050a586ceaSMark Shellenbaum 		}
28060a586ceaSMark Shellenbaum 	} else {
28070a586ceaSMark Shellenbaum 		if (mdb_pwalk("zfs_acl_node_aces0", acl_aces_cb,
28080a586ceaSMark Shellenbaum 		    arg, addr) != 0) {
28090a586ceaSMark Shellenbaum 			mdb_warn("can't walk ACEs");
28100a586ceaSMark Shellenbaum 			return (DCMD_ERR);
28110a586ceaSMark Shellenbaum 		}
28120a586ceaSMark Shellenbaum 	}
28130a586ceaSMark Shellenbaum 	return (WALK_NEXT);
28140a586ceaSMark Shellenbaum }
28150a586ceaSMark Shellenbaum 
28160a586ceaSMark Shellenbaum /* ARGSUSED */
28170a586ceaSMark Shellenbaum static int
28180a586ceaSMark Shellenbaum zfs_acl_dump(uintptr_t addr, uint_t flags, int argc, const mdb_arg_t *argv)
28190a586ceaSMark Shellenbaum {
28200a586ceaSMark Shellenbaum 	zfs_acl_t zacl;
28210a586ceaSMark Shellenbaum 	int verbose = FALSE;
28220a586ceaSMark Shellenbaum 	acl_dump_args_t acl_args;
28230a586ceaSMark Shellenbaum 
28240a586ceaSMark Shellenbaum 	if (!(flags & DCMD_ADDRSPEC))
28250a586ceaSMark Shellenbaum 		return (DCMD_USAGE);
28260a586ceaSMark Shellenbaum 
28270a586ceaSMark Shellenbaum 	if (mdb_getopts(argc, argv,
28280a586ceaSMark Shellenbaum 	    'v', MDB_OPT_SETBITS, TRUE, &verbose, TRUE, NULL) != argc)
28290a586ceaSMark Shellenbaum 		return (DCMD_USAGE);
28300a586ceaSMark Shellenbaum 
28310a586ceaSMark Shellenbaum 	if (mdb_vread(&zacl, sizeof (zfs_acl_t), addr) == -1) {
28320a586ceaSMark Shellenbaum 		mdb_warn("failed to read zfs_acl_t");
28330a586ceaSMark Shellenbaum 		return (DCMD_ERR);
28340a586ceaSMark Shellenbaum 	}
28350a586ceaSMark Shellenbaum 
28360a586ceaSMark Shellenbaum 	acl_args.a_argc = argc;
28370a586ceaSMark Shellenbaum 	acl_args.a_argv = argv;
28380a586ceaSMark Shellenbaum 	acl_args.a_version = zacl.z_version;
28390a586ceaSMark Shellenbaum 	acl_args.a_flags = DCMD_LOOPFIRST;
28400a586ceaSMark Shellenbaum 
28410a586ceaSMark Shellenbaum 	if (mdb_pwalk("zfs_acl_node", acl_cb, &acl_args, addr) != 0) {
28420a586ceaSMark Shellenbaum 		mdb_warn("can't walk ACL");
28430a586ceaSMark Shellenbaum 		return (DCMD_ERR);
28440a586ceaSMark Shellenbaum 	}
28450a586ceaSMark Shellenbaum 
28460a586ceaSMark Shellenbaum 	return (DCMD_OK);
28470a586ceaSMark Shellenbaum }
28480a586ceaSMark Shellenbaum 
28490a586ceaSMark Shellenbaum /* ARGSUSED */
28500a586ceaSMark Shellenbaum static int
28510a586ceaSMark Shellenbaum zfs_acl_node_walk_init(mdb_walk_state_t *wsp)
28520a586ceaSMark Shellenbaum {
28530a586ceaSMark Shellenbaum 	if (wsp->walk_addr == NULL) {
28540a586ceaSMark Shellenbaum 		mdb_warn("must supply address of zfs_acl_node_t\n");
28550a586ceaSMark Shellenbaum 		return (WALK_ERR);
28560a586ceaSMark Shellenbaum 	}
28570a586ceaSMark Shellenbaum 
285828e4da25SMatthew Ahrens 	wsp->walk_addr +=
285928e4da25SMatthew Ahrens 	    mdb_ctf_offsetof_by_name(ZFS_STRUCT "zfs_acl", "z_acl");
28600a586ceaSMark Shellenbaum 
28610a586ceaSMark Shellenbaum 	if (mdb_layered_walk("list", wsp) == -1) {
28620a586ceaSMark Shellenbaum 		mdb_warn("failed to walk 'list'\n");
28630a586ceaSMark Shellenbaum 		return (WALK_ERR);
28640a586ceaSMark Shellenbaum 	}
28650a586ceaSMark Shellenbaum 
28660a586ceaSMark Shellenbaum 	return (WALK_NEXT);
28670a586ceaSMark Shellenbaum }
28680a586ceaSMark Shellenbaum 
28690a586ceaSMark Shellenbaum static int
28700a586ceaSMark Shellenbaum zfs_acl_node_walk_step(mdb_walk_state_t *wsp)
28710a586ceaSMark Shellenbaum {
28720a586ceaSMark Shellenbaum 	zfs_acl_node_t	aclnode;
28730a586ceaSMark Shellenbaum 
28740a586ceaSMark Shellenbaum 	if (mdb_vread(&aclnode, sizeof (zfs_acl_node_t),
28750a586ceaSMark Shellenbaum 	    wsp->walk_addr) == -1) {
28760a586ceaSMark Shellenbaum 		mdb_warn("failed to read zfs_acl_node at %p", wsp->walk_addr);
28770a586ceaSMark Shellenbaum 		return (WALK_ERR);
28780a586ceaSMark Shellenbaum 	}
28790a586ceaSMark Shellenbaum 
28800a586ceaSMark Shellenbaum 	return (wsp->walk_callback(wsp->walk_addr, &aclnode, wsp->walk_cbdata));
28810a586ceaSMark Shellenbaum }
28820a586ceaSMark Shellenbaum 
28830a586ceaSMark Shellenbaum typedef struct ace_walk_data {
28840a586ceaSMark Shellenbaum 	int		ace_count;
28850a586ceaSMark Shellenbaum 	int		ace_version;
28860a586ceaSMark Shellenbaum } ace_walk_data_t;
28870a586ceaSMark Shellenbaum 
28880a586ceaSMark Shellenbaum static int
28890a586ceaSMark Shellenbaum zfs_aces_walk_init_common(mdb_walk_state_t *wsp, int version,
28900a586ceaSMark Shellenbaum     int ace_count, uintptr_t ace_data)
28910a586ceaSMark Shellenbaum {
28920a586ceaSMark Shellenbaum 	ace_walk_data_t *ace_walk_data;
28930a586ceaSMark Shellenbaum 
28940a586ceaSMark Shellenbaum 	if (wsp->walk_addr == NULL) {
28950a586ceaSMark Shellenbaum 		mdb_warn("must supply address of zfs_acl_node_t\n");
28960a586ceaSMark Shellenbaum 		return (WALK_ERR);
28970a586ceaSMark Shellenbaum 	}
28980a586ceaSMark Shellenbaum 
28990a586ceaSMark Shellenbaum 	ace_walk_data = mdb_alloc(sizeof (ace_walk_data_t), UM_SLEEP | UM_GC);
29000a586ceaSMark Shellenbaum 
29010a586ceaSMark Shellenbaum 	ace_walk_data->ace_count = ace_count;
29020a586ceaSMark Shellenbaum 	ace_walk_data->ace_version = version;
29030a586ceaSMark Shellenbaum 
29040a586ceaSMark Shellenbaum 	wsp->walk_addr = ace_data;
29050a586ceaSMark Shellenbaum 	wsp->walk_data = ace_walk_data;
29060a586ceaSMark Shellenbaum 
29070a586ceaSMark Shellenbaum 	return (WALK_NEXT);
29080a586ceaSMark Shellenbaum }
29090a586ceaSMark Shellenbaum 
29100a586ceaSMark Shellenbaum static int
29110a586ceaSMark Shellenbaum zfs_acl_node_aces_walk_init_common(mdb_walk_state_t *wsp, int version)
29120a586ceaSMark Shellenbaum {
29130a586ceaSMark Shellenbaum 	static int gotid;
29140a586ceaSMark Shellenbaum 	static mdb_ctf_id_t acl_id;
29150a586ceaSMark Shellenbaum 	int z_ace_count;
29160a586ceaSMark Shellenbaum 	uintptr_t z_acldata;
29170a586ceaSMark Shellenbaum 
29180a586ceaSMark Shellenbaum 	if (!gotid) {
29190a586ceaSMark Shellenbaum 		if (mdb_ctf_lookup_by_name("struct zfs_acl_node",
29200a586ceaSMark Shellenbaum 		    &acl_id) == -1) {
29210a586ceaSMark Shellenbaum 			mdb_warn("couldn't find struct zfs_acl_node");
29220a586ceaSMark Shellenbaum 			return (DCMD_ERR);
29230a586ceaSMark Shellenbaum 		}
29240a586ceaSMark Shellenbaum 		gotid = TRUE;
29250a586ceaSMark Shellenbaum 	}
29260a586ceaSMark Shellenbaum 
29270a586ceaSMark Shellenbaum 	if (GETMEMBID(wsp->walk_addr, &acl_id, z_ace_count, z_ace_count)) {
29280a586ceaSMark Shellenbaum 		return (DCMD_ERR);
29290a586ceaSMark Shellenbaum 	}
29300a586ceaSMark Shellenbaum 	if (GETMEMBID(wsp->walk_addr, &acl_id, z_acldata, z_acldata)) {
29310a586ceaSMark Shellenbaum 		return (DCMD_ERR);
29320a586ceaSMark Shellenbaum 	}
29330a586ceaSMark Shellenbaum 
29340a586ceaSMark Shellenbaum 	return (zfs_aces_walk_init_common(wsp, version,
29350a586ceaSMark Shellenbaum 	    z_ace_count, z_acldata));
29360a586ceaSMark Shellenbaum }
29370a586ceaSMark Shellenbaum 
29380a586ceaSMark Shellenbaum /* ARGSUSED */
29390a586ceaSMark Shellenbaum static int
29400a586ceaSMark Shellenbaum zfs_acl_node_aces_walk_init(mdb_walk_state_t *wsp)
29410a586ceaSMark Shellenbaum {
29420a586ceaSMark Shellenbaum 	return (zfs_acl_node_aces_walk_init_common(wsp, 1));
29430a586ceaSMark Shellenbaum }
29440a586ceaSMark Shellenbaum 
29450a586ceaSMark Shellenbaum /* ARGSUSED */
29460a586ceaSMark Shellenbaum static int
29470a586ceaSMark Shellenbaum zfs_acl_node_aces0_walk_init(mdb_walk_state_t *wsp)
29480a586ceaSMark Shellenbaum {
29490a586ceaSMark Shellenbaum 	return (zfs_acl_node_aces_walk_init_common(wsp, 0));
29500a586ceaSMark Shellenbaum }
29510a586ceaSMark Shellenbaum 
29520a586ceaSMark Shellenbaum static int
29530a586ceaSMark Shellenbaum zfs_aces_walk_step(mdb_walk_state_t *wsp)
29540a586ceaSMark Shellenbaum {
29550a586ceaSMark Shellenbaum 	ace_walk_data_t *ace_data = wsp->walk_data;
29560a586ceaSMark Shellenbaum 	zfs_ace_t zace;
29570a586ceaSMark Shellenbaum 	ace_t *acep;
29580a586ceaSMark Shellenbaum 	int status;
29590a586ceaSMark Shellenbaum 	int entry_type;
29600a586ceaSMark Shellenbaum 	int allow_type;
29610a586ceaSMark Shellenbaum 	uintptr_t ptr;
29620a586ceaSMark Shellenbaum 
29630a586ceaSMark Shellenbaum 	if (ace_data->ace_count == 0)
29640a586ceaSMark Shellenbaum 		return (WALK_DONE);
29650a586ceaSMark Shellenbaum 
29660a586ceaSMark Shellenbaum 	if (mdb_vread(&zace, sizeof (zfs_ace_t), wsp->walk_addr) == -1) {
29670a586ceaSMark Shellenbaum 		mdb_warn("failed to read zfs_ace_t at %#lx",
29680a586ceaSMark Shellenbaum 		    wsp->walk_addr);
29690a586ceaSMark Shellenbaum 		return (WALK_ERR);
29700a586ceaSMark Shellenbaum 	}
29710a586ceaSMark Shellenbaum 
29720a586ceaSMark Shellenbaum 	switch (ace_data->ace_version) {
29730a586ceaSMark Shellenbaum 	case 0:
29740a586ceaSMark Shellenbaum 		acep = (ace_t *)&zace;
29750a586ceaSMark Shellenbaum 		entry_type = acep->a_flags & ACE_TYPE_FLAGS;
29760a586ceaSMark Shellenbaum 		allow_type = acep->a_type;
29770a586ceaSMark Shellenbaum 		break;
29780a586ceaSMark Shellenbaum 	case 1:
29790a586ceaSMark Shellenbaum 		entry_type = zace.z_hdr.z_flags & ACE_TYPE_FLAGS;
29800a586ceaSMark Shellenbaum 		allow_type = zace.z_hdr.z_type;
29810a586ceaSMark Shellenbaum 		break;
29820a586ceaSMark Shellenbaum 	default:
29830a586ceaSMark Shellenbaum 		return (WALK_ERR);
29840a586ceaSMark Shellenbaum 	}
29850a586ceaSMark Shellenbaum 
29860a586ceaSMark Shellenbaum 	ptr = (uintptr_t)wsp->walk_addr;
29870a586ceaSMark Shellenbaum 	switch (entry_type) {
29880a586ceaSMark Shellenbaum 	case ACE_OWNER:
29890a586ceaSMark Shellenbaum 	case ACE_EVERYONE:
29900a586ceaSMark Shellenbaum 	case (ACE_IDENTIFIER_GROUP | ACE_GROUP):
29910a586ceaSMark Shellenbaum 		ptr += ace_data->ace_version == 0 ?
29920a586ceaSMark Shellenbaum 		    sizeof (ace_t) : sizeof (zfs_ace_hdr_t);
29930a586ceaSMark Shellenbaum 		break;
29940a586ceaSMark Shellenbaum 	case ACE_IDENTIFIER_GROUP:
29950a586ceaSMark Shellenbaum 	default:
29960a586ceaSMark Shellenbaum 		switch (allow_type) {
29970a586ceaSMark Shellenbaum 		case ACE_ACCESS_ALLOWED_OBJECT_ACE_TYPE:
29980a586ceaSMark Shellenbaum 		case ACE_ACCESS_DENIED_OBJECT_ACE_TYPE:
29990a586ceaSMark Shellenbaum 		case ACE_SYSTEM_AUDIT_OBJECT_ACE_TYPE:
30000a586ceaSMark Shellenbaum 		case ACE_SYSTEM_ALARM_OBJECT_ACE_TYPE:
30010a586ceaSMark Shellenbaum 			ptr += ace_data->ace_version == 0 ?
30020a586ceaSMark Shellenbaum 			    sizeof (ace_t) : sizeof (zfs_object_ace_t);
30030a586ceaSMark Shellenbaum 			break;
30040a586ceaSMark Shellenbaum 		default:
30050a586ceaSMark Shellenbaum 			ptr += ace_data->ace_version == 0 ?
30060a586ceaSMark Shellenbaum 			    sizeof (ace_t) : sizeof (zfs_ace_t);
30070a586ceaSMark Shellenbaum 			break;
30080a586ceaSMark Shellenbaum 		}
30090a586ceaSMark Shellenbaum 	}
30100a586ceaSMark Shellenbaum 
30110a586ceaSMark Shellenbaum 	ace_data->ace_count--;
30120a586ceaSMark Shellenbaum 	status = wsp->walk_callback(wsp->walk_addr,
30130a586ceaSMark Shellenbaum 	    (void *)(uintptr_t)&zace, wsp->walk_cbdata);
30140a586ceaSMark Shellenbaum 
30150a586ceaSMark Shellenbaum 	wsp->walk_addr = ptr;
30160a586ceaSMark Shellenbaum 	return (status);
30170a586ceaSMark Shellenbaum }
30180a586ceaSMark Shellenbaum 
301928e4da25SMatthew Ahrens typedef struct mdb_zfs_rrwlock {
3020d5ee8a13SMatthew Ahrens 	uintptr_t	rr_writer;
302128e4da25SMatthew Ahrens 	boolean_t	rr_writer_wanted;
302228e4da25SMatthew Ahrens } mdb_zfs_rrwlock_t;
302328e4da25SMatthew Ahrens 
302428e4da25SMatthew Ahrens static uint_t rrw_key;
302528e4da25SMatthew Ahrens 
302628e4da25SMatthew Ahrens /* ARGSUSED */
302728e4da25SMatthew Ahrens static int
302828e4da25SMatthew Ahrens rrwlock(uintptr_t addr, uint_t flags, int argc, const mdb_arg_t *argv)
302928e4da25SMatthew Ahrens {
303028e4da25SMatthew Ahrens 	mdb_zfs_rrwlock_t rrw;
303128e4da25SMatthew Ahrens 
303228e4da25SMatthew Ahrens 	if (rrw_key == 0) {
303328e4da25SMatthew Ahrens 		if (mdb_ctf_readsym(&rrw_key, "uint_t", "rrw_tsd_key", 0) == -1)
303428e4da25SMatthew Ahrens 			return (DCMD_ERR);
303528e4da25SMatthew Ahrens 	}
303628e4da25SMatthew Ahrens 
303728e4da25SMatthew Ahrens 	if (mdb_ctf_vread(&rrw, "rrwlock_t", "mdb_zfs_rrwlock_t", addr,
303828e4da25SMatthew Ahrens 	    0) == -1)
303928e4da25SMatthew Ahrens 		return (DCMD_ERR);
304028e4da25SMatthew Ahrens 
3041d5ee8a13SMatthew Ahrens 	if (rrw.rr_writer != 0) {
3042d5ee8a13SMatthew Ahrens 		mdb_printf("write lock held by thread %lx\n", rrw.rr_writer);
304328e4da25SMatthew Ahrens 		return (DCMD_OK);
304428e4da25SMatthew Ahrens 	}
304528e4da25SMatthew Ahrens 
304628e4da25SMatthew Ahrens 	if (rrw.rr_writer_wanted) {
304728e4da25SMatthew Ahrens 		mdb_printf("writer wanted\n");
304828e4da25SMatthew Ahrens 	}
304928e4da25SMatthew Ahrens 
305028e4da25SMatthew Ahrens 	mdb_printf("anonymous references:\n");
305128e4da25SMatthew Ahrens 	(void) mdb_call_dcmd("refcount", addr +
305228e4da25SMatthew Ahrens 	    mdb_ctf_offsetof_by_name(ZFS_STRUCT "rrwlock", "rr_anon_rcount"),
305328e4da25SMatthew Ahrens 	    DCMD_ADDRSPEC, 0, NULL);
305428e4da25SMatthew Ahrens 
305528e4da25SMatthew Ahrens 	mdb_printf("linked references:\n");
305628e4da25SMatthew Ahrens 	(void) mdb_call_dcmd("refcount", addr +
305728e4da25SMatthew Ahrens 	    mdb_ctf_offsetof_by_name(ZFS_STRUCT "rrwlock", "rr_linked_rcount"),
305828e4da25SMatthew Ahrens 	    DCMD_ADDRSPEC, 0, NULL);
305928e4da25SMatthew Ahrens 
306028e4da25SMatthew Ahrens 	/*
306128e4da25SMatthew Ahrens 	 * XXX This should find references from
306228e4da25SMatthew Ahrens 	 * "::walk thread | ::tsd -v <rrw_key>", but there is no support
306328e4da25SMatthew Ahrens 	 * for programmatic consumption of dcmds, so this would be
306428e4da25SMatthew Ahrens 	 * difficult, potentially requiring reimplementing ::tsd (both
306528e4da25SMatthew Ahrens 	 * user and kernel versions) in this MDB module.
306628e4da25SMatthew Ahrens 	 */
306728e4da25SMatthew Ahrens 
306828e4da25SMatthew Ahrens 	return (DCMD_OK);
306928e4da25SMatthew Ahrens }
307028e4da25SMatthew Ahrens 
3071fa9e4066Sahrens /*
3072fa9e4066Sahrens  * MDB module linkage information:
3073fa9e4066Sahrens  *
3074fa9e4066Sahrens  * We declare a list of structures describing our dcmds, and a function
3075fa9e4066Sahrens  * named _mdb_init to return a pointer to our module information.
3076fa9e4066Sahrens  */
3077fa9e4066Sahrens 
3078fa9e4066Sahrens static const mdb_dcmd_t dcmds[] = {
307991ebeef5Sahrens 	{ "arc", "[-bkmg]", "print ARC variables", arc_print },
3080fa9e4066Sahrens 	{ "blkptr", ":", "print blkptr_t", blkptr },
3081fa9e4066Sahrens 	{ "dbuf", ":", "print dmu_buf_impl_t", dbuf },
3082fa9e4066Sahrens 	{ "dbuf_stats", ":", "dbuf stats", dbuf_stats },
3083fa9e4066Sahrens 	{ "dbufs",
30844223fc7cSMark Shellenbaum 	    "\t[-O objset_t*] [-n objset_name | \"mos\"] "
3085a3f829aeSBill Moore 	    "[-o object | \"mdn\"] \n"
3086a3f829aeSBill Moore 	    "\t[-l level] [-b blkid | \"bonus\"]",
3087a3f829aeSBill Moore 	    "find dmu_buf_impl_t's that match specified criteria", dbufs },
3088fa9e4066Sahrens 	{ "abuf_find", "dva_word[0] dva_word[1]",
3089a3f829aeSBill Moore 	    "find arc_buf_hdr_t of a specified DVA",
3090a3f829aeSBill Moore 	    abuf_find },
3091fa9e4066Sahrens 	{ "spa", "?[-cv]", "spa_t summary", spa_print },
3092fa9e4066Sahrens 	{ "spa_config", ":", "print spa_t configuration", spa_print_config },
3093fa9e4066Sahrens 	{ "spa_space", ":[-b]", "print spa_t on-disk space usage", spa_space },
3094fa9e4066Sahrens 	{ "spa_vdevs", ":", "given a spa_t, print vdev summary", spa_vdevs },
309591ebeef5Sahrens 	{ "vdev", ":[-re]\n"
3096a3f829aeSBill Moore 	    "\t-r display recursively\n"
3097a3f829aeSBill Moore 	    "\t-e print statistics",
3098a3f829aeSBill Moore 	    "vdev_t summary", vdev_print },
3099a3f829aeSBill Moore 	{ "zio", ":[cpr]\n"
3100a3f829aeSBill Moore 	    "\t-c display children\n"
3101a3f829aeSBill Moore 	    "\t-p display parents\n"
3102a3f829aeSBill Moore 	    "\t-r display recursively",
3103a3f829aeSBill Moore 	    "zio_t summary", zio_print },
3104ccae0b50Seschrock 	{ "zio_state", "?", "print out all zio_t structures on system or "
3105ccae0b50Seschrock 	    "for a particular pool", zio_state },
310688b7b0f2SMatthew Ahrens 	{ "zfs_blkstats", ":[-v]",
310788b7b0f2SMatthew Ahrens 	    "given a spa_t, print block type stats from last scrub",
310888b7b0f2SMatthew Ahrens 	    zfs_blkstats },
3109614409b5Sahrens 	{ "zfs_params", "", "print zfs tunable parameters", zfs_params },
311028e4da25SMatthew Ahrens 	{ "refcount", ":[-r]\n"
311128e4da25SMatthew Ahrens 	    "\t-r display recently removed references",
311228e4da25SMatthew Ahrens 	    "print refcount_t holders", refcount },
31133f1f8012SMatthew Ahrens 	{ "zap_leaf", "", "print zap_leaf_phys_t", zap_leaf },
31140a586ceaSMark Shellenbaum 	{ "zfs_aces", ":[-v]", "print all ACEs from a zfs_acl_t",
31150a586ceaSMark Shellenbaum 	    zfs_acl_dump },
31160a586ceaSMark Shellenbaum 	{ "zfs_ace", ":[-v]", "print zfs_ace", zfs_ace_print },
31170a586ceaSMark Shellenbaum 	{ "zfs_ace0", ":[-v]", "print zfs_ace0", zfs_ace0_print },
31180a586ceaSMark Shellenbaum 	{ "sa_attr_table", ":", "print SA attribute table from sa_os_t",
31190a586ceaSMark Shellenbaum 	    sa_attr_table},
31200a586ceaSMark Shellenbaum 	{ "sa_attr", ": attr_id",
31210a586ceaSMark Shellenbaum 	    "print SA attribute address when given sa_handle_t", sa_attr_print},
312228e4da25SMatthew Ahrens 	{ "zfs_dbgmsg", ":[-va]",
31233f9d6ad7SLin Ling 	    "print zfs debug log", dbgmsg},
312428e4da25SMatthew Ahrens 	{ "rrwlock", ":",
312528e4da25SMatthew Ahrens 	    "print rrwlock_t, including readers", rrwlock},
3126fa9e4066Sahrens 	{ NULL }
3127fa9e4066Sahrens };
3128fa9e4066Sahrens 
3129fa9e4066Sahrens static const mdb_walker_t walkers[] = {
3130fa9e4066Sahrens 	{ "zms_freelist", "walk ZFS metaslab freelist",
313128e4da25SMatthew Ahrens 	    freelist_walk_init, freelist_walk_step, NULL },
3132fa9e4066Sahrens 	{ "txg_list", "given any txg_list_t *, walk all entries in all txgs",
313328e4da25SMatthew Ahrens 	    txg_list_walk_init, txg_list_walk_step, NULL },
3134fa9e4066Sahrens 	{ "txg_list0", "given any txg_list_t *, walk all entries in txg 0",
313528e4da25SMatthew Ahrens 	    txg_list0_walk_init, txg_list_walk_step, NULL },
3136fa9e4066Sahrens 	{ "txg_list1", "given any txg_list_t *, walk all entries in txg 1",
313728e4da25SMatthew Ahrens 	    txg_list1_walk_init, txg_list_walk_step, NULL },
3138fa9e4066Sahrens 	{ "txg_list2", "given any txg_list_t *, walk all entries in txg 2",
313928e4da25SMatthew Ahrens 	    txg_list2_walk_init, txg_list_walk_step, NULL },
3140fa9e4066Sahrens 	{ "txg_list3", "given any txg_list_t *, walk all entries in txg 3",
314128e4da25SMatthew Ahrens 	    txg_list3_walk_init, txg_list_walk_step, NULL },
3142ccae0b50Seschrock 	{ "zio", "walk all zio structures, optionally for a particular spa_t",
314328e4da25SMatthew Ahrens 	    zio_walk_init, zio_walk_step, NULL },
314428e4da25SMatthew Ahrens 	{ "zio_root",
314528e4da25SMatthew Ahrens 	    "walk all root zio_t structures, optionally for a particular spa_t",
314628e4da25SMatthew Ahrens 	    zio_walk_init, zio_walk_root_step, NULL },
3147fa9e4066Sahrens 	{ "spa", "walk all spa_t entries in the namespace",
314828e4da25SMatthew Ahrens 	    spa_walk_init, spa_walk_step, NULL },
31495f5f7a6fSahrens 	{ "metaslab", "given a spa_t *, walk all metaslab_t structures",
315028e4da25SMatthew Ahrens 	    metaslab_walk_init, metaslab_walk_step, NULL },
31510a586ceaSMark Shellenbaum 	{ "zfs_acl_node", "given a zfs_acl_t, walk all zfs_acl_nodes",
31520a586ceaSMark Shellenbaum 	    zfs_acl_node_walk_init, zfs_acl_node_walk_step, NULL },
31530a586ceaSMark Shellenbaum 	{ "zfs_acl_node_aces", "given a zfs_acl_node_t, walk all ACEs",
31540a586ceaSMark Shellenbaum 	    zfs_acl_node_aces_walk_init, zfs_aces_walk_step, NULL },
31550a586ceaSMark Shellenbaum 	{ "zfs_acl_node_aces0",
31560a586ceaSMark Shellenbaum 	    "given a zfs_acl_node_t, walk all ACEs as ace_t",
31570a586ceaSMark Shellenbaum 	    zfs_acl_node_aces0_walk_init, zfs_aces_walk_step, NULL },
3158fa9e4066Sahrens 	{ NULL }
3159fa9e4066Sahrens };
3160fa9e4066Sahrens 
3161fa9e4066Sahrens static const mdb_modinfo_t modinfo = {
3162fa9e4066Sahrens 	MDB_API_VERSION, dcmds, walkers
3163fa9e4066Sahrens };
3164fa9e4066Sahrens 
3165fa9e4066Sahrens const mdb_modinfo_t *
3166fa9e4066Sahrens _mdb_init(void)
3167fa9e4066Sahrens {
3168fa9e4066Sahrens 	return (&modinfo);
3169fa9e4066Sahrens }
3170