xref: /illumos-gate/usr/src/cmd/mdb/common/modules/zfs/zfs.c (revision ba3132cfb269da4118d7dc25a8c6c1a91f9f5303)
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.
24*ba3132cfSJoe Stein  * Copyright (c) 2011, 2015 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 
592e4c9986SGeorge Wilson enum spa_flags {
602e4c9986SGeorge Wilson 	SPA_FLAG_CONFIG			= 1 << 0,
612e4c9986SGeorge Wilson 	SPA_FLAG_VDEVS			= 1 << 1,
622e4c9986SGeorge Wilson 	SPA_FLAG_ERRORS			= 1 << 2,
632e4c9986SGeorge Wilson 	SPA_FLAG_METASLAB_GROUPS	= 1 << 3,
642e4c9986SGeorge Wilson 	SPA_FLAG_METASLABS		= 1 << 4,
652e4c9986SGeorge Wilson 	SPA_FLAG_HISTOGRAMS		= 1 << 5
662e4c9986SGeorge Wilson };
672e4c9986SGeorge Wilson 
682e4c9986SGeorge Wilson #define	SPA_FLAG_ALL_VDEV	\
692e4c9986SGeorge Wilson 	(SPA_FLAG_VDEVS | SPA_FLAG_ERRORS | SPA_FLAG_METASLAB_GROUPS | \
702e4c9986SGeorge Wilson 	SPA_FLAG_METASLABS | SPA_FLAG_HISTOGRAMS)
712e4c9986SGeorge Wilson 
72fa9e4066Sahrens static int
73fa9e4066Sahrens getmember(uintptr_t addr, const char *type, mdb_ctf_id_t *idp,
74fa9e4066Sahrens     const char *member, int len, void *buf)
75fa9e4066Sahrens {
76fa9e4066Sahrens 	mdb_ctf_id_t id;
77fa9e4066Sahrens 	ulong_t off;
78fa9e4066Sahrens 	char name[64];
79fa9e4066Sahrens 
80fa9e4066Sahrens 	if (idp == NULL) {
81fa9e4066Sahrens 		if (mdb_ctf_lookup_by_name(type, &id) == -1) {
82fa9e4066Sahrens 			mdb_warn("couldn't find type %s", type);
83fa9e4066Sahrens 			return (DCMD_ERR);
84fa9e4066Sahrens 		}
85fa9e4066Sahrens 		idp = &id;
86fa9e4066Sahrens 	} else {
87fa9e4066Sahrens 		type = name;
88fa9e4066Sahrens 		mdb_ctf_type_name(*idp, name, sizeof (name));
89fa9e4066Sahrens 	}
90fa9e4066Sahrens 
91fa9e4066Sahrens 	if (mdb_ctf_offsetof(*idp, member, &off) == -1) {
92fa9e4066Sahrens 		mdb_warn("couldn't find member %s of type %s\n", member, type);
93fa9e4066Sahrens 		return (DCMD_ERR);
94fa9e4066Sahrens 	}
95fa9e4066Sahrens 	if (off % 8 != 0) {
96fa9e4066Sahrens 		mdb_warn("member %s of type %s is unsupported bitfield",
97fa9e4066Sahrens 		    member, type);
98fa9e4066Sahrens 		return (DCMD_ERR);
99fa9e4066Sahrens 	}
100fa9e4066Sahrens 	off /= 8;
101fa9e4066Sahrens 
102fa9e4066Sahrens 	if (mdb_vread(buf, len, addr + off) == -1) {
103fa9e4066Sahrens 		mdb_warn("failed to read %s from %s at %p",
104fa9e4066Sahrens 		    member, type, addr + off);
105fa9e4066Sahrens 		return (DCMD_ERR);
106fa9e4066Sahrens 	}
107fa9e4066Sahrens 	/* mdb_warn("read %s from %s at %p+%llx\n", member, type, addr, off); */
108fa9e4066Sahrens 
109fa9e4066Sahrens 	return (0);
110fa9e4066Sahrens }
111fa9e4066Sahrens 
11228e4da25SMatthew Ahrens #define	GETMEMB(addr, structname, member, dest) \
11328e4da25SMatthew Ahrens 	getmember(addr, ZFS_STRUCT structname, NULL, #member, \
11428e4da25SMatthew Ahrens 	sizeof (dest), &(dest))
115fa9e4066Sahrens 
116fa9e4066Sahrens #define	GETMEMBID(addr, ctfid, member, dest) \
117fa9e4066Sahrens 	getmember(addr, NULL, ctfid, #member, sizeof (dest), &(dest))
118fa9e4066Sahrens 
1193f9d6ad7SLin Ling static boolean_t
1203f9d6ad7SLin Ling strisprint(const char *cp)
1213f9d6ad7SLin Ling {
1223f9d6ad7SLin Ling 	for (; *cp; cp++) {
1233f9d6ad7SLin Ling 		if (!isprint(*cp))
1243f9d6ad7SLin Ling 			return (B_FALSE);
1253f9d6ad7SLin Ling 	}
1263f9d6ad7SLin Ling 	return (B_TRUE);
1273f9d6ad7SLin Ling }
1283f9d6ad7SLin Ling 
1292e4c9986SGeorge Wilson #define	NICENUM_BUFLEN 6
1302e4c9986SGeorge Wilson 
1312e4c9986SGeorge Wilson static int
1322e4c9986SGeorge Wilson snprintfrac(char *buf, int len,
1332e4c9986SGeorge Wilson     uint64_t numerator, uint64_t denom, int frac_digits)
1342e4c9986SGeorge Wilson {
1352e4c9986SGeorge Wilson 	int mul = 1;
1362e4c9986SGeorge Wilson 	int whole, frac, i;
1372e4c9986SGeorge Wilson 
1382e4c9986SGeorge Wilson 	for (i = frac_digits; i; i--)
1392e4c9986SGeorge Wilson 		mul *= 10;
1402e4c9986SGeorge Wilson 	whole = numerator / denom;
1412e4c9986SGeorge Wilson 	frac = mul * numerator / denom - mul * whole;
1422e4c9986SGeorge Wilson 	return (mdb_snprintf(buf, len, "%u.%0*u", whole, frac_digits, frac));
1432e4c9986SGeorge Wilson }
1442e4c9986SGeorge Wilson 
1452e4c9986SGeorge Wilson static void
1462e4c9986SGeorge Wilson mdb_nicenum(uint64_t num, char *buf)
1472e4c9986SGeorge Wilson {
1482e4c9986SGeorge Wilson 	uint64_t n = num;
1492e4c9986SGeorge Wilson 	int index = 0;
1502e4c9986SGeorge Wilson 	char *u;
1512e4c9986SGeorge Wilson 
1522e4c9986SGeorge Wilson 	while (n >= 1024) {
1532e4c9986SGeorge Wilson 		n = (n + (1024 / 2)) / 1024; /* Round up or down */
1542e4c9986SGeorge Wilson 		index++;
1552e4c9986SGeorge Wilson 	}
1562e4c9986SGeorge Wilson 
1572e4c9986SGeorge Wilson 	u = &" \0K\0M\0G\0T\0P\0E\0"[index*2];
1582e4c9986SGeorge Wilson 
1592e4c9986SGeorge Wilson 	if (index == 0) {
1602e4c9986SGeorge Wilson 		(void) mdb_snprintf(buf, NICENUM_BUFLEN, "%llu",
1612e4c9986SGeorge Wilson 		    (u_longlong_t)n);
1622e4c9986SGeorge Wilson 	} else if (n < 10 && (num & (num - 1)) != 0) {
1632e4c9986SGeorge Wilson 		(void) snprintfrac(buf, NICENUM_BUFLEN,
1642e4c9986SGeorge Wilson 		    num, 1ULL << 10 * index, 2);
1652e4c9986SGeorge Wilson 		strcat(buf, u);
1662e4c9986SGeorge Wilson 	} else if (n < 100 && (num & (num - 1)) != 0) {
1672e4c9986SGeorge Wilson 		(void) snprintfrac(buf, NICENUM_BUFLEN,
1682e4c9986SGeorge Wilson 		    num, 1ULL << 10 * index, 1);
1692e4c9986SGeorge Wilson 		strcat(buf, u);
1702e4c9986SGeorge Wilson 	} else {
1712e4c9986SGeorge Wilson 		(void) mdb_snprintf(buf, NICENUM_BUFLEN, "%llu%s",
1722e4c9986SGeorge Wilson 		    (u_longlong_t)n, u);
1732e4c9986SGeorge Wilson 	}
1742e4c9986SGeorge Wilson }
1752e4c9986SGeorge Wilson 
176fa9e4066Sahrens static int verbose;
177fa9e4066Sahrens 
178fa9e4066Sahrens static int
179fa9e4066Sahrens freelist_walk_init(mdb_walk_state_t *wsp)
180fa9e4066Sahrens {
181fa9e4066Sahrens 	if (wsp->walk_addr == NULL) {
182fa9e4066Sahrens 		mdb_warn("must supply starting address\n");
183fa9e4066Sahrens 		return (WALK_ERR);
184fa9e4066Sahrens 	}
185fa9e4066Sahrens 
186fa9e4066Sahrens 	wsp->walk_data = 0;  /* Index into the freelist */
187fa9e4066Sahrens 	return (WALK_NEXT);
188fa9e4066Sahrens }
189fa9e4066Sahrens 
190fa9e4066Sahrens static int
191fa9e4066Sahrens freelist_walk_step(mdb_walk_state_t *wsp)
192fa9e4066Sahrens {
193fa9e4066Sahrens 	uint64_t entry;
194fa9e4066Sahrens 	uintptr_t number = (uintptr_t)wsp->walk_data;
1958053a263Sck 	char *ddata[] = { "ALLOC", "FREE", "CONDENSE", "INVALID",
1968053a263Sck 			    "INVALID", "INVALID", "INVALID", "INVALID" };
197fa9e4066Sahrens 	int mapshift = SPA_MINBLOCKSHIFT;
198fa9e4066Sahrens 
199fa9e4066Sahrens 	if (mdb_vread(&entry, sizeof (entry), wsp->walk_addr) == -1) {
200fa9e4066Sahrens 		mdb_warn("failed to read freelist entry %p", wsp->walk_addr);
201fa9e4066Sahrens 		return (WALK_DONE);
202fa9e4066Sahrens 	}
203fa9e4066Sahrens 	wsp->walk_addr += sizeof (entry);
204fa9e4066Sahrens 	wsp->walk_data = (void *)(number + 1);
205fa9e4066Sahrens 
206fa9e4066Sahrens 	if (SM_DEBUG_DECODE(entry)) {
207fa9e4066Sahrens 		mdb_printf("DEBUG: %3u  %10s: txg=%llu  pass=%llu\n",
208fa9e4066Sahrens 		    number,
209fa9e4066Sahrens 		    ddata[SM_DEBUG_ACTION_DECODE(entry)],
210fa9e4066Sahrens 		    SM_DEBUG_TXG_DECODE(entry),
211fa9e4066Sahrens 		    SM_DEBUG_SYNCPASS_DECODE(entry));
212fa9e4066Sahrens 	} else {
213fa9e4066Sahrens 		mdb_printf("Entry: %3u  offsets=%08llx-%08llx  type=%c  "
214fa9e4066Sahrens 		    "size=%06llx", number,
215fa9e4066Sahrens 		    SM_OFFSET_DECODE(entry) << mapshift,
216fa9e4066Sahrens 		    (SM_OFFSET_DECODE(entry) + SM_RUN_DECODE(entry)) <<
217fa9e4066Sahrens 		    mapshift,
218fa9e4066Sahrens 		    SM_TYPE_DECODE(entry) == SM_ALLOC ? 'A' : 'F',
219fa9e4066Sahrens 		    SM_RUN_DECODE(entry) << mapshift);
220fa9e4066Sahrens 		if (verbose)
221fa9e4066Sahrens 			mdb_printf("      (raw=%012llx)\n", entry);
222fa9e4066Sahrens 		mdb_printf("\n");
223fa9e4066Sahrens 	}
224fa9e4066Sahrens 	return (WALK_NEXT);
225fa9e4066Sahrens }
226fa9e4066Sahrens 
227fa9e4066Sahrens static int
22828e4da25SMatthew Ahrens mdb_dsl_dir_name(uintptr_t addr, char *buf)
229fa9e4066Sahrens {
230fa9e4066Sahrens 	static int gotid;
231fa9e4066Sahrens 	static mdb_ctf_id_t dd_id;
232fa9e4066Sahrens 	uintptr_t dd_parent;
233fa9e4066Sahrens 	char dd_myname[MAXNAMELEN];
234fa9e4066Sahrens 
235fa9e4066Sahrens 	if (!gotid) {
23628e4da25SMatthew Ahrens 		if (mdb_ctf_lookup_by_name(ZFS_STRUCT "dsl_dir",
237fa9e4066Sahrens 		    &dd_id) == -1) {
238fa9e4066Sahrens 			mdb_warn("couldn't find struct dsl_dir");
239fa9e4066Sahrens 			return (DCMD_ERR);
240fa9e4066Sahrens 		}
241fa9e4066Sahrens 		gotid = TRUE;
242fa9e4066Sahrens 	}
243fa9e4066Sahrens 	if (GETMEMBID(addr, &dd_id, dd_parent, dd_parent) ||
244fa9e4066Sahrens 	    GETMEMBID(addr, &dd_id, dd_myname, dd_myname)) {
245fa9e4066Sahrens 		return (DCMD_ERR);
246fa9e4066Sahrens 	}
247fa9e4066Sahrens 
248fa9e4066Sahrens 	if (dd_parent) {
24928e4da25SMatthew Ahrens 		if (mdb_dsl_dir_name(dd_parent, buf))
250fa9e4066Sahrens 			return (DCMD_ERR);
251fa9e4066Sahrens 		strcat(buf, "/");
252fa9e4066Sahrens 	}
253fa9e4066Sahrens 
254fa9e4066Sahrens 	if (dd_myname[0])
255fa9e4066Sahrens 		strcat(buf, dd_myname);
256fa9e4066Sahrens 	else
257fa9e4066Sahrens 		strcat(buf, "???");
258fa9e4066Sahrens 
259fa9e4066Sahrens 	return (0);
260fa9e4066Sahrens }
261fa9e4066Sahrens 
262fa9e4066Sahrens static int
263fa9e4066Sahrens objset_name(uintptr_t addr, char *buf)
264fa9e4066Sahrens {
265fa9e4066Sahrens 	static int gotid;
2664223fc7cSMark Shellenbaum 	static mdb_ctf_id_t os_id, ds_id;
267fa9e4066Sahrens 	uintptr_t os_dsl_dataset;
268fa9e4066Sahrens 	char ds_snapname[MAXNAMELEN];
269fa9e4066Sahrens 	uintptr_t ds_dir;
270fa9e4066Sahrens 
271fa9e4066Sahrens 	buf[0] = '\0';
272fa9e4066Sahrens 
273fa9e4066Sahrens 	if (!gotid) {
27428e4da25SMatthew Ahrens 		if (mdb_ctf_lookup_by_name(ZFS_STRUCT "objset",
2754223fc7cSMark Shellenbaum 		    &os_id) == -1) {
2764223fc7cSMark Shellenbaum 			mdb_warn("couldn't find struct objset");
277fa9e4066Sahrens 			return (DCMD_ERR);
278fa9e4066Sahrens 		}
27928e4da25SMatthew Ahrens 		if (mdb_ctf_lookup_by_name(ZFS_STRUCT "dsl_dataset",
280fa9e4066Sahrens 		    &ds_id) == -1) {
281fa9e4066Sahrens 			mdb_warn("couldn't find struct dsl_dataset");
282fa9e4066Sahrens 			return (DCMD_ERR);
283fa9e4066Sahrens 		}
284fa9e4066Sahrens 
285fa9e4066Sahrens 		gotid = TRUE;
286fa9e4066Sahrens 	}
287fa9e4066Sahrens 
2884223fc7cSMark Shellenbaum 	if (GETMEMBID(addr, &os_id, os_dsl_dataset, os_dsl_dataset))
289fa9e4066Sahrens 		return (DCMD_ERR);
290fa9e4066Sahrens 
291fa9e4066Sahrens 	if (os_dsl_dataset == 0) {
292fa9e4066Sahrens 		strcat(buf, "mos");
293fa9e4066Sahrens 		return (0);
294fa9e4066Sahrens 	}
295fa9e4066Sahrens 
296fa9e4066Sahrens 	if (GETMEMBID(os_dsl_dataset, &ds_id, ds_snapname, ds_snapname) ||
297fa9e4066Sahrens 	    GETMEMBID(os_dsl_dataset, &ds_id, ds_dir, ds_dir)) {
298fa9e4066Sahrens 		return (DCMD_ERR);
299fa9e4066Sahrens 	}
300fa9e4066Sahrens 
30128e4da25SMatthew Ahrens 	if (ds_dir && mdb_dsl_dir_name(ds_dir, buf))
302fa9e4066Sahrens 		return (DCMD_ERR);
303fa9e4066Sahrens 
304fa9e4066Sahrens 	if (ds_snapname[0]) {
305fa9e4066Sahrens 		strcat(buf, "@");
306fa9e4066Sahrens 		strcat(buf, ds_snapname);
307fa9e4066Sahrens 	}
308fa9e4066Sahrens 	return (0);
309fa9e4066Sahrens }
310fa9e4066Sahrens 
311fa9e4066Sahrens static void
312fa9e4066Sahrens enum_lookup(char *out, size_t size, mdb_ctf_id_t id, int val,
313fa9e4066Sahrens     const char *prefix)
314fa9e4066Sahrens {
315fa9e4066Sahrens 	const char *cp;
316fa9e4066Sahrens 	size_t len = strlen(prefix);
317fa9e4066Sahrens 
318fa9e4066Sahrens 	if ((cp = mdb_ctf_enum_name(id, val)) != NULL) {
319fa9e4066Sahrens 		if (strncmp(cp, prefix, len) == 0)
320fa9e4066Sahrens 			cp += len;
321fa9e4066Sahrens 		(void) strncpy(out, cp, size);
322fa9e4066Sahrens 	} else {
323fa9e4066Sahrens 		mdb_snprintf(out, size, "? (%d)", val);
324fa9e4066Sahrens 	}
325fa9e4066Sahrens }
326fa9e4066Sahrens 
327614409b5Sahrens /* ARGSUSED */
328614409b5Sahrens static int
329614409b5Sahrens zfs_params(uintptr_t addr, uint_t flags, int argc, const mdb_arg_t *argv)
330614409b5Sahrens {
331614409b5Sahrens 	/*
332614409b5Sahrens 	 * This table can be approximately generated by running:
333614409b5Sahrens 	 * egrep "^[a-z0-9_]+ [a-z0-9_]+( =.*)?;" *.c | cut -d ' ' -f 2
334614409b5Sahrens 	 */
335614409b5Sahrens 	static const char *params[] = {
336614409b5Sahrens 		"arc_reduce_dnlc_percent",
33769962b56SMatthew Ahrens 		"arc_lotsfree_percent",
33869962b56SMatthew Ahrens 		"zfs_dirty_data_max",
33969962b56SMatthew Ahrens 		"zfs_dirty_data_sync",
34069962b56SMatthew Ahrens 		"zfs_delay_max_ns",
34169962b56SMatthew Ahrens 		"zfs_delay_min_dirty_percent",
34269962b56SMatthew Ahrens 		"zfs_delay_scale",
34369962b56SMatthew Ahrens 		"zfs_vdev_max_active",
34469962b56SMatthew Ahrens 		"zfs_vdev_sync_read_min_active",
34569962b56SMatthew Ahrens 		"zfs_vdev_sync_read_max_active",
34669962b56SMatthew Ahrens 		"zfs_vdev_sync_write_min_active",
34769962b56SMatthew Ahrens 		"zfs_vdev_sync_write_max_active",
34869962b56SMatthew Ahrens 		"zfs_vdev_async_read_min_active",
34969962b56SMatthew Ahrens 		"zfs_vdev_async_read_max_active",
35069962b56SMatthew Ahrens 		"zfs_vdev_async_write_min_active",
35169962b56SMatthew Ahrens 		"zfs_vdev_async_write_max_active",
35269962b56SMatthew Ahrens 		"zfs_vdev_scrub_min_active",
35369962b56SMatthew Ahrens 		"zfs_vdev_scrub_max_active",
35469962b56SMatthew Ahrens 		"zfs_vdev_async_write_active_min_dirty_percent",
35569962b56SMatthew Ahrens 		"zfs_vdev_async_write_active_max_dirty_percent",
35669962b56SMatthew Ahrens 		"spa_asize_inflation",
357614409b5Sahrens 		"zfs_arc_max",
358614409b5Sahrens 		"zfs_arc_min",
35949e3519aSmaybee 		"arc_shrink_shift",
360614409b5Sahrens 		"zfs_mdcomp_disable",
361614409b5Sahrens 		"zfs_prefetch_disable",
362614409b5Sahrens 		"zfetch_max_streams",
363614409b5Sahrens 		"zfetch_min_sec_reap",
364614409b5Sahrens 		"zfetch_block_cap",
365614409b5Sahrens 		"zfetch_array_rd_sz",
366614409b5Sahrens 		"zfs_default_bs",
367614409b5Sahrens 		"zfs_default_ibs",
368614409b5Sahrens 		"metaslab_aliquot",
369614409b5Sahrens 		"reference_tracking_enable",
370614409b5Sahrens 		"reference_history",
371614409b5Sahrens 		"spa_max_replication_override",
372b24ab676SJeff Bonwick 		"spa_mode_global",
373614409b5Sahrens 		"zfs_flags",
3741ab7f2deSmaybee 		"zfs_txg_timeout",
375614409b5Sahrens 		"zfs_vdev_cache_max",
376614409b5Sahrens 		"zfs_vdev_cache_size",
377614409b5Sahrens 		"zfs_vdev_cache_bshift",
378614409b5Sahrens 		"vdev_mirror_shift",
379614409b5Sahrens 		"zfs_scrub_limit",
380b16da2e2SGeorge Wilson 		"zfs_no_scrub_io",
381b16da2e2SGeorge Wilson 		"zfs_no_scrub_prefetch",
382614409b5Sahrens 		"zfs_vdev_aggregation_limit",
383614409b5Sahrens 		"fzap_default_block_shift",
384614409b5Sahrens 		"zfs_immediate_write_sz",
385614409b5Sahrens 		"zfs_read_chunk_size",
386614409b5Sahrens 		"zfs_nocacheflush",
38755da60b9SMark J Musante 		"zil_replay_disable",
3881ab7f2deSmaybee 		"metaslab_gang_bang",
389d6e555bdSGeorge Wilson 		"metaslab_df_alloc_threshold",
390d6e555bdSGeorge Wilson 		"metaslab_df_free_pct",
391614409b5Sahrens 		"zio_injection_enabled",
392614409b5Sahrens 		"zvol_immediate_write_sz",
393614409b5Sahrens 	};
394614409b5Sahrens 
395b24ab676SJeff Bonwick 	for (int i = 0; i < sizeof (params) / sizeof (params[0]); i++) {
396614409b5Sahrens 		int sz;
397614409b5Sahrens 		uint64_t val64;
398614409b5Sahrens 		uint32_t *val32p = (uint32_t *)&val64;
399614409b5Sahrens 
400614409b5Sahrens 		sz = mdb_readvar(&val64, params[i]);
401614409b5Sahrens 		if (sz == 4) {
402614409b5Sahrens 			mdb_printf("%s = 0x%x\n", params[i], *val32p);
403614409b5Sahrens 		} else if (sz == 8) {
404614409b5Sahrens 			mdb_printf("%s = 0x%llx\n", params[i], val64);
405614409b5Sahrens 		} else {
406614409b5Sahrens 			mdb_warn("variable %s not found", params[i]);
407614409b5Sahrens 		}
408614409b5Sahrens 	}
409614409b5Sahrens 
410614409b5Sahrens 	return (DCMD_OK);
411614409b5Sahrens }
412614409b5Sahrens 
413fa9e4066Sahrens /* ARGSUSED */
414fa9e4066Sahrens static int
415fa9e4066Sahrens blkptr(uintptr_t addr, uint_t flags, int argc, const mdb_arg_t *argv)
416fa9e4066Sahrens {
417b24ab676SJeff Bonwick 	mdb_ctf_id_t type_enum, checksum_enum, compress_enum;
418b24ab676SJeff Bonwick 	char type[80], checksum[80], compress[80];
419b24ab676SJeff Bonwick 	blkptr_t blk, *bp = &blk;
420b24ab676SJeff Bonwick 	char buf[BP_SPRINTF_LEN];
421fa9e4066Sahrens 
422b24ab676SJeff Bonwick 	if (mdb_vread(&blk, sizeof (blkptr_t), addr) == -1) {
423fa9e4066Sahrens 		mdb_warn("failed to read blkptr_t");
424fa9e4066Sahrens 		return (DCMD_ERR);
425fa9e4066Sahrens 	}
426fa9e4066Sahrens 
427b24ab676SJeff Bonwick 	if (mdb_ctf_lookup_by_name("enum dmu_object_type", &type_enum) == -1 ||
428b24ab676SJeff Bonwick 	    mdb_ctf_lookup_by_name("enum zio_checksum", &checksum_enum) == -1 ||
429b24ab676SJeff Bonwick 	    mdb_ctf_lookup_by_name("enum zio_compress", &compress_enum) == -1) {
430b24ab676SJeff Bonwick 		mdb_warn("Could not find blkptr enumerated types");
431fa9e4066Sahrens 		return (DCMD_ERR);
432fa9e4066Sahrens 	}
433fa9e4066Sahrens 
434b24ab676SJeff Bonwick 	enum_lookup(type, sizeof (type), type_enum,
435b24ab676SJeff Bonwick 	    BP_GET_TYPE(bp), "DMU_OT_");
436b24ab676SJeff Bonwick 	enum_lookup(checksum, sizeof (checksum), checksum_enum,
437b24ab676SJeff Bonwick 	    BP_GET_CHECKSUM(bp), "ZIO_CHECKSUM_");
438b24ab676SJeff Bonwick 	enum_lookup(compress, sizeof (compress), compress_enum,
439b24ab676SJeff Bonwick 	    BP_GET_COMPRESS(bp), "ZIO_COMPRESS_");
440fa9e4066Sahrens 
44143466aaeSMax Grossman 	SNPRINTF_BLKPTR(mdb_snprintf, '\n', buf, sizeof (buf), bp, type,
44243466aaeSMax Grossman 	    checksum, compress);
443fa9e4066Sahrens 
444b24ab676SJeff Bonwick 	mdb_printf("%s\n", buf);
445fa9e4066Sahrens 
446fa9e4066Sahrens 	return (DCMD_OK);
447fa9e4066Sahrens }
448fa9e4066Sahrens 
44928e4da25SMatthew Ahrens typedef struct mdb_dmu_buf_impl {
45028e4da25SMatthew Ahrens 	struct {
45128e4da25SMatthew Ahrens 		uint64_t db_object;
4522515f5d4SJustin T. Gibbs 		uintptr_t db_data;
45328e4da25SMatthew Ahrens 	} db;
454d5ee8a13SMatthew Ahrens 	uintptr_t db_objset;
45528e4da25SMatthew Ahrens 	uint64_t db_level;
45628e4da25SMatthew Ahrens 	uint64_t db_blkid;
45728e4da25SMatthew Ahrens 	struct {
45828e4da25SMatthew Ahrens 		uint64_t rc_count;
45928e4da25SMatthew Ahrens 	} db_holds;
46028e4da25SMatthew Ahrens } mdb_dmu_buf_impl_t;
46128e4da25SMatthew Ahrens 
462fa9e4066Sahrens /* ARGSUSED */
463fa9e4066Sahrens static int
464fa9e4066Sahrens dbuf(uintptr_t addr, uint_t flags, int argc, const mdb_arg_t *argv)
465fa9e4066Sahrens {
46628e4da25SMatthew Ahrens 	mdb_dmu_buf_impl_t db;
467fa9e4066Sahrens 	char objectname[32];
468fa9e4066Sahrens 	char blkidname[32];
469fa9e4066Sahrens 	char path[MAXNAMELEN];
470*ba3132cfSJoe Stein 	int ptr_width = (int)(sizeof (void *)) * 2;
471fa9e4066Sahrens 
47228e4da25SMatthew Ahrens 	if (DCMD_HDRSPEC(flags))
473*ba3132cfSJoe Stein 		mdb_printf("%*s %8s %3s %9s %5s %s\n",
474*ba3132cfSJoe Stein 		    ptr_width, "addr", "object", "lvl", "blkid", "holds", "os");
475fa9e4066Sahrens 
47628e4da25SMatthew Ahrens 	if (mdb_ctf_vread(&db, ZFS_STRUCT "dmu_buf_impl", "mdb_dmu_buf_impl_t",
47728e4da25SMatthew Ahrens 	    addr, 0) == -1)
478fa9e4066Sahrens 		return (DCMD_ERR);
479fa9e4066Sahrens 
48028e4da25SMatthew Ahrens 	if (db.db.db_object == DMU_META_DNODE_OBJECT)
481fa9e4066Sahrens 		(void) strcpy(objectname, "mdn");
482fa9e4066Sahrens 	else
483fa9e4066Sahrens 		(void) mdb_snprintf(objectname, sizeof (objectname), "%llx",
48428e4da25SMatthew Ahrens 		    (u_longlong_t)db.db.db_object);
485fa9e4066Sahrens 
48628e4da25SMatthew Ahrens 	if (db.db_blkid == DMU_BONUS_BLKID)
487fa9e4066Sahrens 		(void) strcpy(blkidname, "bonus");
488fa9e4066Sahrens 	else
489fa9e4066Sahrens 		(void) mdb_snprintf(blkidname, sizeof (blkidname), "%llx",
49028e4da25SMatthew Ahrens 		    (u_longlong_t)db.db_blkid);
491fa9e4066Sahrens 
492d5ee8a13SMatthew Ahrens 	if (objset_name(db.db_objset, path)) {
49328e4da25SMatthew Ahrens 		return (DCMD_ERR);
494fa9e4066Sahrens 	}
495fa9e4066Sahrens 
496*ba3132cfSJoe Stein 	mdb_printf("%*p %8s %3u %9s %5llu %s\n", ptr_width, addr,
49728e4da25SMatthew Ahrens 	    objectname, (int)db.db_level, blkidname,
49828e4da25SMatthew Ahrens 	    db.db_holds.rc_count, path);
499fa9e4066Sahrens 
500fa9e4066Sahrens 	return (DCMD_OK);
501fa9e4066Sahrens }
502fa9e4066Sahrens 
503fa9e4066Sahrens /* ARGSUSED */
504fa9e4066Sahrens static int
505fa9e4066Sahrens dbuf_stats(uintptr_t addr, uint_t flags, int argc, const mdb_arg_t *argv)
506fa9e4066Sahrens {
507fa9e4066Sahrens #define	HISTOSZ 32
508fa9e4066Sahrens 	uintptr_t dbp;
509fa9e4066Sahrens 	dmu_buf_impl_t db;
510fa9e4066Sahrens 	dbuf_hash_table_t ht;
511fa9e4066Sahrens 	uint64_t bucket, ndbufs;
512fa9e4066Sahrens 	uint64_t histo[HISTOSZ];
513fa9e4066Sahrens 	uint64_t histo2[HISTOSZ];
514fa9e4066Sahrens 	int i, maxidx;
515fa9e4066Sahrens 
516fa9e4066Sahrens 	if (mdb_readvar(&ht, "dbuf_hash_table") == -1) {
517fa9e4066Sahrens 		mdb_warn("failed to read 'dbuf_hash_table'");
518fa9e4066Sahrens 		return (DCMD_ERR);
519fa9e4066Sahrens 	}
520fa9e4066Sahrens 
521fa9e4066Sahrens 	for (i = 0; i < HISTOSZ; i++) {
522fa9e4066Sahrens 		histo[i] = 0;
523fa9e4066Sahrens 		histo2[i] = 0;
524fa9e4066Sahrens 	}
525fa9e4066Sahrens 
526fa9e4066Sahrens 	ndbufs = 0;
527fa9e4066Sahrens 	for (bucket = 0; bucket < ht.hash_table_mask+1; bucket++) {
528fa9e4066Sahrens 		int len;
529fa9e4066Sahrens 
530fa9e4066Sahrens 		if (mdb_vread(&dbp, sizeof (void *),
531fa9e4066Sahrens 		    (uintptr_t)(ht.hash_table+bucket)) == -1) {
532fa9e4066Sahrens 			mdb_warn("failed to read hash bucket %u at %p",
533fa9e4066Sahrens 			    bucket, ht.hash_table+bucket);
534fa9e4066Sahrens 			return (DCMD_ERR);
535fa9e4066Sahrens 		}
536fa9e4066Sahrens 
537fa9e4066Sahrens 		len = 0;
538fa9e4066Sahrens 		while (dbp != 0) {
539fa9e4066Sahrens 			if (mdb_vread(&db, sizeof (dmu_buf_impl_t),
540fa9e4066Sahrens 			    dbp) == -1) {
541fa9e4066Sahrens 				mdb_warn("failed to read dbuf at %p", dbp);
542fa9e4066Sahrens 				return (DCMD_ERR);
543fa9e4066Sahrens 			}
544fa9e4066Sahrens 			dbp = (uintptr_t)db.db_hash_next;
545fa9e4066Sahrens 			for (i = MIN(len, HISTOSZ - 1); i >= 0; i--)
546fa9e4066Sahrens 				histo2[i]++;
547fa9e4066Sahrens 			len++;
548fa9e4066Sahrens 			ndbufs++;
549fa9e4066Sahrens 		}
550fa9e4066Sahrens 
551fa9e4066Sahrens 		if (len >= HISTOSZ)
552fa9e4066Sahrens 			len = HISTOSZ-1;
553fa9e4066Sahrens 		histo[len]++;
554fa9e4066Sahrens 	}
555fa9e4066Sahrens 
556fa9e4066Sahrens 	mdb_printf("hash table has %llu buckets, %llu dbufs "
557fa9e4066Sahrens 	    "(avg %llu buckets/dbuf)\n",
558fa9e4066Sahrens 	    ht.hash_table_mask+1, ndbufs,
559fa9e4066Sahrens 	    (ht.hash_table_mask+1)/ndbufs);
560fa9e4066Sahrens 
561fa9e4066Sahrens 	mdb_printf("\n");
562fa9e4066Sahrens 	maxidx = 0;
563fa9e4066Sahrens 	for (i = 0; i < HISTOSZ; i++)
564fa9e4066Sahrens 		if (histo[i] > 0)
565fa9e4066Sahrens 			maxidx = i;
566fa9e4066Sahrens 	mdb_printf("hash chain length	number of buckets\n");
567fa9e4066Sahrens 	for (i = 0; i <= maxidx; i++)
568fa9e4066Sahrens 		mdb_printf("%u			%llu\n", i, histo[i]);
569fa9e4066Sahrens 
570fa9e4066Sahrens 	mdb_printf("\n");
571fa9e4066Sahrens 	maxidx = 0;
572fa9e4066Sahrens 	for (i = 0; i < HISTOSZ; i++)
573fa9e4066Sahrens 		if (histo2[i] > 0)
574fa9e4066Sahrens 			maxidx = i;
575fa9e4066Sahrens 	mdb_printf("hash chain depth	number of dbufs\n");
576fa9e4066Sahrens 	for (i = 0; i <= maxidx; i++)
577fa9e4066Sahrens 		mdb_printf("%u or more		%llu	%llu%%\n",
578fa9e4066Sahrens 		    i, histo2[i], histo2[i]*100/ndbufs);
579fa9e4066Sahrens 
580fa9e4066Sahrens 
581fa9e4066Sahrens 	return (DCMD_OK);
582fa9e4066Sahrens }
583fa9e4066Sahrens 
5843f1f8012SMatthew Ahrens #define	CHAIN_END 0xffff
5853f1f8012SMatthew Ahrens /*
5863f1f8012SMatthew Ahrens  * ::zap_leaf [-v]
5873f1f8012SMatthew Ahrens  *
5883f1f8012SMatthew Ahrens  * Print a zap_leaf_phys_t, assumed to be 16k
5893f1f8012SMatthew Ahrens  */
5903f1f8012SMatthew Ahrens /* ARGSUSED */
5913f1f8012SMatthew Ahrens static int
5923f1f8012SMatthew Ahrens zap_leaf(uintptr_t addr, uint_t flags, int argc, const mdb_arg_t *argv)
5933f1f8012SMatthew Ahrens {
5943f1f8012SMatthew Ahrens 	char buf[16*1024];
5953f1f8012SMatthew Ahrens 	int verbose = B_FALSE;
5963f1f8012SMatthew Ahrens 	int four = B_FALSE;
597c1379625SJustin T. Gibbs 	dmu_buf_t l_dbuf;
5983f1f8012SMatthew Ahrens 	zap_leaf_t l;
5993f1f8012SMatthew Ahrens 	zap_leaf_phys_t *zlp = (void *)buf;
6003f1f8012SMatthew Ahrens 	int i;
6013f1f8012SMatthew Ahrens 
6023f1f8012SMatthew Ahrens 	if (mdb_getopts(argc, argv,
6033f1f8012SMatthew Ahrens 	    'v', MDB_OPT_SETBITS, TRUE, &verbose,
6043f1f8012SMatthew Ahrens 	    '4', MDB_OPT_SETBITS, TRUE, &four,
6053f1f8012SMatthew Ahrens 	    NULL) != argc)
6063f1f8012SMatthew Ahrens 		return (DCMD_USAGE);
6073f1f8012SMatthew Ahrens 
608c1379625SJustin T. Gibbs 	l_dbuf.db_data = zlp;
609c1379625SJustin T. Gibbs 	l.l_dbuf = &l_dbuf;
6103f1f8012SMatthew Ahrens 	l.l_bs = 14; /* assume 16k blocks */
6113f1f8012SMatthew Ahrens 	if (four)
6123f1f8012SMatthew Ahrens 		l.l_bs = 12;
6133f1f8012SMatthew Ahrens 
6143f1f8012SMatthew Ahrens 	if (!(flags & DCMD_ADDRSPEC)) {
6153f1f8012SMatthew Ahrens 		return (DCMD_USAGE);
6163f1f8012SMatthew Ahrens 	}
6173f1f8012SMatthew Ahrens 
6183f1f8012SMatthew Ahrens 	if (mdb_vread(buf, sizeof (buf), addr) == -1) {
6193f1f8012SMatthew Ahrens 		mdb_warn("failed to read zap_leaf_phys_t at %p", addr);
6203f1f8012SMatthew Ahrens 		return (DCMD_ERR);
6213f1f8012SMatthew Ahrens 	}
6223f1f8012SMatthew Ahrens 
6233f1f8012SMatthew Ahrens 	if (zlp->l_hdr.lh_block_type != ZBT_LEAF ||
6243f1f8012SMatthew Ahrens 	    zlp->l_hdr.lh_magic != ZAP_LEAF_MAGIC) {
6253f1f8012SMatthew Ahrens 		mdb_warn("This does not appear to be a zap_leaf_phys_t");
6263f1f8012SMatthew Ahrens 		return (DCMD_ERR);
6273f1f8012SMatthew Ahrens 	}
6283f1f8012SMatthew Ahrens 
6293f1f8012SMatthew Ahrens 	mdb_printf("zap_leaf_phys_t at %p:\n", addr);
6303f1f8012SMatthew Ahrens 	mdb_printf("    lh_prefix_len = %u\n", zlp->l_hdr.lh_prefix_len);
6313f1f8012SMatthew Ahrens 	mdb_printf("    lh_prefix = %llx\n", zlp->l_hdr.lh_prefix);
6323f1f8012SMatthew Ahrens 	mdb_printf("    lh_nentries = %u\n", zlp->l_hdr.lh_nentries);
6333f1f8012SMatthew Ahrens 	mdb_printf("    lh_nfree = %u\n", zlp->l_hdr.lh_nfree,
6343f1f8012SMatthew Ahrens 	    zlp->l_hdr.lh_nfree * 100 / (ZAP_LEAF_NUMCHUNKS(&l)));
6353f1f8012SMatthew Ahrens 	mdb_printf("    lh_freelist = %u\n", zlp->l_hdr.lh_freelist);
6363f1f8012SMatthew Ahrens 	mdb_printf("    lh_flags = %x (%s)\n", zlp->l_hdr.lh_flags,
6373f1f8012SMatthew Ahrens 	    zlp->l_hdr.lh_flags & ZLF_ENTRIES_CDSORTED ?
6383f1f8012SMatthew Ahrens 	    "ENTRIES_CDSORTED" : "");
6393f1f8012SMatthew Ahrens 
6403f1f8012SMatthew Ahrens 	if (verbose) {
6413f1f8012SMatthew Ahrens 		mdb_printf(" hash table:\n");
6423f1f8012SMatthew Ahrens 		for (i = 0; i < ZAP_LEAF_HASH_NUMENTRIES(&l); i++) {
6433f1f8012SMatthew Ahrens 			if (zlp->l_hash[i] != CHAIN_END)
6443f1f8012SMatthew Ahrens 				mdb_printf("    %u: %u\n", i, zlp->l_hash[i]);
6453f1f8012SMatthew Ahrens 		}
6463f1f8012SMatthew Ahrens 	}
6473f1f8012SMatthew Ahrens 
6483f1f8012SMatthew Ahrens 	mdb_printf(" chunks:\n");
6493f1f8012SMatthew Ahrens 	for (i = 0; i < ZAP_LEAF_NUMCHUNKS(&l); i++) {
6503f1f8012SMatthew Ahrens 		/* LINTED: alignment */
6513f1f8012SMatthew Ahrens 		zap_leaf_chunk_t *zlc = &ZAP_LEAF_CHUNK(&l, i);
6523f1f8012SMatthew Ahrens 		switch (zlc->l_entry.le_type) {
6533f1f8012SMatthew Ahrens 		case ZAP_CHUNK_FREE:
6543f1f8012SMatthew Ahrens 			if (verbose) {
6553f1f8012SMatthew Ahrens 				mdb_printf("    %u: free; lf_next = %u\n",
6563f1f8012SMatthew Ahrens 				    i, zlc->l_free.lf_next);
6573f1f8012SMatthew Ahrens 			}
6583f1f8012SMatthew Ahrens 			break;
6593f1f8012SMatthew Ahrens 		case ZAP_CHUNK_ENTRY:
6603f1f8012SMatthew Ahrens 			mdb_printf("    %u: entry\n", i);
6613f1f8012SMatthew Ahrens 			if (verbose) {
6623f1f8012SMatthew Ahrens 				mdb_printf("        le_next = %u\n",
6633f1f8012SMatthew Ahrens 				    zlc->l_entry.le_next);
6643f1f8012SMatthew Ahrens 			}
6653f1f8012SMatthew Ahrens 			mdb_printf("        le_name_chunk = %u\n",
6663f1f8012SMatthew Ahrens 			    zlc->l_entry.le_name_chunk);
6673f1f8012SMatthew Ahrens 			mdb_printf("        le_name_numints = %u\n",
6683f1f8012SMatthew Ahrens 			    zlc->l_entry.le_name_numints);
6693f1f8012SMatthew Ahrens 			mdb_printf("        le_value_chunk = %u\n",
6703f1f8012SMatthew Ahrens 			    zlc->l_entry.le_value_chunk);
6713f1f8012SMatthew Ahrens 			mdb_printf("        le_value_intlen = %u\n",
6723f1f8012SMatthew Ahrens 			    zlc->l_entry.le_value_intlen);
6733f1f8012SMatthew Ahrens 			mdb_printf("        le_value_numints = %u\n",
6743f1f8012SMatthew Ahrens 			    zlc->l_entry.le_value_numints);
6753f1f8012SMatthew Ahrens 			mdb_printf("        le_cd = %u\n",
6763f1f8012SMatthew Ahrens 			    zlc->l_entry.le_cd);
6773f1f8012SMatthew Ahrens 			mdb_printf("        le_hash = %llx\n",
6783f1f8012SMatthew Ahrens 			    zlc->l_entry.le_hash);
6793f1f8012SMatthew Ahrens 			break;
6803f1f8012SMatthew Ahrens 		case ZAP_CHUNK_ARRAY:
6813f9d6ad7SLin Ling 			mdb_printf("    %u: array", i);
6823f9d6ad7SLin Ling 			if (strisprint((char *)zlc->l_array.la_array))
6833f9d6ad7SLin Ling 				mdb_printf(" \"%s\"", zlc->l_array.la_array);
6843f9d6ad7SLin Ling 			mdb_printf("\n");
6853f1f8012SMatthew Ahrens 			if (verbose) {
6863f1f8012SMatthew Ahrens 				int j;
6873f1f8012SMatthew Ahrens 				mdb_printf("        ");
6883f1f8012SMatthew Ahrens 				for (j = 0; j < ZAP_LEAF_ARRAY_BYTES; j++) {
6893f1f8012SMatthew Ahrens 					mdb_printf("%02x ",
6903f1f8012SMatthew Ahrens 					    zlc->l_array.la_array[j]);
6913f1f8012SMatthew Ahrens 				}
6923f1f8012SMatthew Ahrens 				mdb_printf("\n");
6933f1f8012SMatthew Ahrens 			}
6943f1f8012SMatthew Ahrens 			if (zlc->l_array.la_next != CHAIN_END) {
6953f1f8012SMatthew Ahrens 				mdb_printf("        lf_next = %u\n",
6963f1f8012SMatthew Ahrens 				    zlc->l_array.la_next);
6973f1f8012SMatthew Ahrens 			}
6983f1f8012SMatthew Ahrens 			break;
6993f1f8012SMatthew Ahrens 		default:
7003f1f8012SMatthew Ahrens 			mdb_printf("    %u: undefined type %u\n",
7013f1f8012SMatthew Ahrens 			    zlc->l_entry.le_type);
7023f1f8012SMatthew Ahrens 		}
7033f1f8012SMatthew Ahrens 	}
7043f1f8012SMatthew Ahrens 
7053f1f8012SMatthew Ahrens 	return (DCMD_OK);
7063f1f8012SMatthew Ahrens }
7073f1f8012SMatthew Ahrens 
708fa9e4066Sahrens typedef struct dbufs_data {
709fa9e4066Sahrens 	mdb_ctf_id_t id;
710fa9e4066Sahrens 	uint64_t objset;
711fa9e4066Sahrens 	uint64_t object;
712fa9e4066Sahrens 	uint64_t level;
713fa9e4066Sahrens 	uint64_t blkid;
714fa9e4066Sahrens 	char *osname;
715fa9e4066Sahrens } dbufs_data_t;
716fa9e4066Sahrens 
717fa9e4066Sahrens #define	DBUFS_UNSET	(0xbaddcafedeadbeefULL)
718fa9e4066Sahrens 
719fa9e4066Sahrens /* ARGSUSED */
720fa9e4066Sahrens static int
721fa9e4066Sahrens dbufs_cb(uintptr_t addr, const void *unknown, void *arg)
722fa9e4066Sahrens {
723fa9e4066Sahrens 	dbufs_data_t *data = arg;
724fa9e4066Sahrens 	uintptr_t objset;
725fa9e4066Sahrens 	dmu_buf_t db;
726fa9e4066Sahrens 	uint8_t level;
727fa9e4066Sahrens 	uint64_t blkid;
728fa9e4066Sahrens 	char osname[MAXNAMELEN];
729fa9e4066Sahrens 
730fa9e4066Sahrens 	if (GETMEMBID(addr, &data->id, db_objset, objset) ||
731fa9e4066Sahrens 	    GETMEMBID(addr, &data->id, db, db) ||
732fa9e4066Sahrens 	    GETMEMBID(addr, &data->id, db_level, level) ||
733fa9e4066Sahrens 	    GETMEMBID(addr, &data->id, db_blkid, blkid)) {
734fa9e4066Sahrens 		return (WALK_ERR);
735fa9e4066Sahrens 	}
736fa9e4066Sahrens 
737fa9e4066Sahrens 	if ((data->objset == DBUFS_UNSET || data->objset == objset) &&
738fa9e4066Sahrens 	    (data->osname == NULL || (objset_name(objset, osname) == 0 &&
739ccae0b50Seschrock 	    strcmp(data->osname, osname) == 0)) &&
740fa9e4066Sahrens 	    (data->object == DBUFS_UNSET || data->object == db.db_object) &&
741fa9e4066Sahrens 	    (data->level == DBUFS_UNSET || data->level == level) &&
742fa9e4066Sahrens 	    (data->blkid == DBUFS_UNSET || data->blkid == blkid)) {
743fa9e4066Sahrens 		mdb_printf("%#lr\n", addr);
744fa9e4066Sahrens 	}
745fa9e4066Sahrens 	return (WALK_NEXT);
746fa9e4066Sahrens }
747fa9e4066Sahrens 
748fa9e4066Sahrens /* ARGSUSED */
749fa9e4066Sahrens static int
750fa9e4066Sahrens dbufs(uintptr_t addr, uint_t flags, int argc, const mdb_arg_t *argv)
751fa9e4066Sahrens {
752fa9e4066Sahrens 	dbufs_data_t data;
753fa9e4066Sahrens 	char *object = NULL;
754fa9e4066Sahrens 	char *blkid = NULL;
755fa9e4066Sahrens 
756fa9e4066Sahrens 	data.objset = data.object = data.level = data.blkid = DBUFS_UNSET;
757fa9e4066Sahrens 	data.osname = NULL;
758fa9e4066Sahrens 
759fa9e4066Sahrens 	if (mdb_getopts(argc, argv,
760fa9e4066Sahrens 	    'O', MDB_OPT_UINT64, &data.objset,
761fa9e4066Sahrens 	    'n', MDB_OPT_STR, &data.osname,
762fa9e4066Sahrens 	    'o', MDB_OPT_STR, &object,
763fa9e4066Sahrens 	    'l', MDB_OPT_UINT64, &data.level,
764fa9e4066Sahrens 	    'b', MDB_OPT_STR, &blkid) != argc) {
765fa9e4066Sahrens 		return (DCMD_USAGE);
766fa9e4066Sahrens 	}
767fa9e4066Sahrens 
768fa9e4066Sahrens 	if (object) {
769fa9e4066Sahrens 		if (strcmp(object, "mdn") == 0) {
770fa9e4066Sahrens 			data.object = DMU_META_DNODE_OBJECT;
771fa9e4066Sahrens 		} else {
772fa9e4066Sahrens 			data.object = mdb_strtoull(object);
773fa9e4066Sahrens 		}
774fa9e4066Sahrens 	}
775fa9e4066Sahrens 
776fa9e4066Sahrens 	if (blkid) {
777fa9e4066Sahrens 		if (strcmp(blkid, "bonus") == 0) {
7780a586ceaSMark Shellenbaum 			data.blkid = DMU_BONUS_BLKID;
779fa9e4066Sahrens 		} else {
780fa9e4066Sahrens 			data.blkid = mdb_strtoull(blkid);
781fa9e4066Sahrens 		}
782fa9e4066Sahrens 	}
783fa9e4066Sahrens 
78428e4da25SMatthew Ahrens 	if (mdb_ctf_lookup_by_name(ZFS_STRUCT "dmu_buf_impl", &data.id) == -1) {
785fa9e4066Sahrens 		mdb_warn("couldn't find struct dmu_buf_impl_t");
786fa9e4066Sahrens 		return (DCMD_ERR);
787fa9e4066Sahrens 	}
788fa9e4066Sahrens 
7895f5f7a6fSahrens 	if (mdb_walk("dmu_buf_impl_t", dbufs_cb, &data) != 0) {
790fa9e4066Sahrens 		mdb_warn("can't walk dbufs");
791fa9e4066Sahrens 		return (DCMD_ERR);
792fa9e4066Sahrens 	}
793fa9e4066Sahrens 
794fa9e4066Sahrens 	return (DCMD_OK);
795fa9e4066Sahrens }
796fa9e4066Sahrens 
797fa9e4066Sahrens typedef struct abuf_find_data {
798fa9e4066Sahrens 	dva_t dva;
799fa9e4066Sahrens 	mdb_ctf_id_t id;
800fa9e4066Sahrens } abuf_find_data_t;
801fa9e4066Sahrens 
802fa9e4066Sahrens /* ARGSUSED */
803fa9e4066Sahrens static int
804fa9e4066Sahrens abuf_find_cb(uintptr_t addr, const void *unknown, void *arg)
805fa9e4066Sahrens {
806fa9e4066Sahrens 	abuf_find_data_t *data = arg;
807fa9e4066Sahrens 	dva_t dva;
808fa9e4066Sahrens 
809fa9e4066Sahrens 	if (GETMEMBID(addr, &data->id, b_dva, dva)) {
810fa9e4066Sahrens 		return (WALK_ERR);
811fa9e4066Sahrens 	}
812fa9e4066Sahrens 
813fa9e4066Sahrens 	if (dva.dva_word[0] == data->dva.dva_word[0] &&
814fa9e4066Sahrens 	    dva.dva_word[1] == data->dva.dva_word[1]) {
815fa9e4066Sahrens 		mdb_printf("%#lr\n", addr);
816fa9e4066Sahrens 	}
817fa9e4066Sahrens 	return (WALK_NEXT);
818fa9e4066Sahrens }
819fa9e4066Sahrens 
820fa9e4066Sahrens /* ARGSUSED */
821fa9e4066Sahrens static int
822fa9e4066Sahrens abuf_find(uintptr_t addr, uint_t flags, int argc, const mdb_arg_t *argv)
823fa9e4066Sahrens {
824fa9e4066Sahrens 	abuf_find_data_t data;
825fa9e4066Sahrens 	GElf_Sym sym;
826fa9e4066Sahrens 	int i;
827fa9e4066Sahrens 	const char *syms[] = {
828a2eea2e1Sahrens 		"ARC_mru",
829a2eea2e1Sahrens 		"ARC_mru_ghost",
830a2eea2e1Sahrens 		"ARC_mfu",
831a2eea2e1Sahrens 		"ARC_mfu_ghost",
832fa9e4066Sahrens 	};
833fa9e4066Sahrens 
834fa9e4066Sahrens 	if (argc != 2)
835fa9e4066Sahrens 		return (DCMD_USAGE);
836fa9e4066Sahrens 
837fa9e4066Sahrens 	for (i = 0; i < 2; i ++) {
838fa9e4066Sahrens 		switch (argv[i].a_type) {
839fa9e4066Sahrens 		case MDB_TYPE_STRING:
840fa9e4066Sahrens 			data.dva.dva_word[i] = mdb_strtoull(argv[i].a_un.a_str);
841fa9e4066Sahrens 			break;
842fa9e4066Sahrens 		case MDB_TYPE_IMMEDIATE:
843fa9e4066Sahrens 			data.dva.dva_word[i] = argv[i].a_un.a_val;
844fa9e4066Sahrens 			break;
845fa9e4066Sahrens 		default:
846fa9e4066Sahrens 			return (DCMD_USAGE);
847fa9e4066Sahrens 		}
848fa9e4066Sahrens 	}
849fa9e4066Sahrens 
85028e4da25SMatthew Ahrens 	if (mdb_ctf_lookup_by_name(ZFS_STRUCT "arc_buf_hdr", &data.id) == -1) {
851fa9e4066Sahrens 		mdb_warn("couldn't find struct arc_buf_hdr");
852fa9e4066Sahrens 		return (DCMD_ERR);
853fa9e4066Sahrens 	}
854fa9e4066Sahrens 
855fa9e4066Sahrens 	for (i = 0; i < sizeof (syms) / sizeof (syms[0]); i++) {
85622ce0148SMatthew Ahrens 		if (mdb_lookup_by_obj(ZFS_OBJ_NAME, syms[i], &sym)) {
857fa9e4066Sahrens 			mdb_warn("can't find symbol %s", syms[i]);
858fa9e4066Sahrens 			return (DCMD_ERR);
859fa9e4066Sahrens 		}
860fa9e4066Sahrens 
861fa9e4066Sahrens 		if (mdb_pwalk("list", abuf_find_cb, &data, sym.st_value) != 0) {
862fa9e4066Sahrens 			mdb_warn("can't walk %s", syms[i]);
863fa9e4066Sahrens 			return (DCMD_ERR);
864fa9e4066Sahrens 		}
865fa9e4066Sahrens 	}
866fa9e4066Sahrens 
867fa9e4066Sahrens 	return (DCMD_OK);
868fa9e4066Sahrens }
869fa9e4066Sahrens 
87028e4da25SMatthew Ahrens 
87128e4da25SMatthew Ahrens typedef struct dbgmsg_arg {
87228e4da25SMatthew Ahrens 	boolean_t da_verbose;
87328e4da25SMatthew Ahrens 	boolean_t da_address;
87428e4da25SMatthew Ahrens } dbgmsg_arg_t;
87528e4da25SMatthew Ahrens 
8763f9d6ad7SLin Ling /* ARGSUSED */
8773f9d6ad7SLin Ling static int
8783f9d6ad7SLin Ling dbgmsg_cb(uintptr_t addr, const void *unknown, void *arg)
8793f9d6ad7SLin Ling {
8803f9d6ad7SLin Ling 	static mdb_ctf_id_t id;
8813f9d6ad7SLin Ling 	static boolean_t gotid;
8823f9d6ad7SLin Ling 	static ulong_t off;
8833f9d6ad7SLin Ling 
88428e4da25SMatthew Ahrens 	dbgmsg_arg_t *da = arg;
8853f9d6ad7SLin Ling 	time_t timestamp;
8863f9d6ad7SLin Ling 	char buf[1024];
8873f9d6ad7SLin Ling 
8883f9d6ad7SLin Ling 	if (!gotid) {
88928e4da25SMatthew Ahrens 		if (mdb_ctf_lookup_by_name(ZFS_STRUCT "zfs_dbgmsg", &id) ==
89028e4da25SMatthew Ahrens 		    -1) {
8913f9d6ad7SLin Ling 			mdb_warn("couldn't find struct zfs_dbgmsg");
8923f9d6ad7SLin Ling 			return (WALK_ERR);
8933f9d6ad7SLin Ling 		}
8943f9d6ad7SLin Ling 		gotid = TRUE;
8953f9d6ad7SLin Ling 		if (mdb_ctf_offsetof(id, "zdm_msg", &off) == -1) {
8963f9d6ad7SLin Ling 			mdb_warn("couldn't find zdm_msg");
8973f9d6ad7SLin Ling 			return (WALK_ERR);
8983f9d6ad7SLin Ling 		}
8993f9d6ad7SLin Ling 		off /= 8;
9003f9d6ad7SLin Ling 	}
9013f9d6ad7SLin Ling 
9023f9d6ad7SLin Ling 
9033f9d6ad7SLin Ling 	if (GETMEMBID(addr, &id, zdm_timestamp, timestamp)) {
9043f9d6ad7SLin Ling 		return (WALK_ERR);
9053f9d6ad7SLin Ling 	}
9063f9d6ad7SLin Ling 
9073f9d6ad7SLin Ling 	if (mdb_readstr(buf, sizeof (buf), addr + off) == -1) {
9083f9d6ad7SLin Ling 		mdb_warn("failed to read zdm_msg at %p\n", addr + off);
9093f9d6ad7SLin Ling 		return (DCMD_ERR);
9103f9d6ad7SLin Ling 	}
9113f9d6ad7SLin Ling 
91228e4da25SMatthew Ahrens 	if (da->da_address)
91328e4da25SMatthew Ahrens 		mdb_printf("%p ", addr);
91428e4da25SMatthew Ahrens 	if (da->da_verbose)
9153f9d6ad7SLin Ling 		mdb_printf("%Y ", timestamp);
9163f9d6ad7SLin Ling 
9173f9d6ad7SLin Ling 	mdb_printf("%s\n", buf);
9183f9d6ad7SLin Ling 
91928e4da25SMatthew Ahrens 	if (da->da_verbose)
9203f9d6ad7SLin Ling 		(void) mdb_call_dcmd("whatis", addr, DCMD_ADDRSPEC, 0, NULL);
9213f9d6ad7SLin Ling 
9223f9d6ad7SLin Ling 	return (WALK_NEXT);
9233f9d6ad7SLin Ling }
9243f9d6ad7SLin Ling 
9253f9d6ad7SLin Ling /* ARGSUSED */
9263f9d6ad7SLin Ling static int
9273f9d6ad7SLin Ling dbgmsg(uintptr_t addr, uint_t flags, int argc, const mdb_arg_t *argv)
9283f9d6ad7SLin Ling {
9293f9d6ad7SLin Ling 	GElf_Sym sym;
93028e4da25SMatthew Ahrens 	dbgmsg_arg_t da = { 0 };
9313f9d6ad7SLin Ling 
9323f9d6ad7SLin Ling 	if (mdb_getopts(argc, argv,
93328e4da25SMatthew Ahrens 	    'v', MDB_OPT_SETBITS, B_TRUE, &da.da_verbose,
93428e4da25SMatthew Ahrens 	    'a', MDB_OPT_SETBITS, B_TRUE, &da.da_address,
9353f9d6ad7SLin Ling 	    NULL) != argc)
9363f9d6ad7SLin Ling 		return (DCMD_USAGE);
9373f9d6ad7SLin Ling 
9383b2aab18SMatthew Ahrens 	if (mdb_lookup_by_obj(ZFS_OBJ_NAME, "zfs_dbgmsgs", &sym)) {
9393f9d6ad7SLin Ling 		mdb_warn("can't find zfs_dbgmsgs");
9403f9d6ad7SLin Ling 		return (DCMD_ERR);
9413f9d6ad7SLin Ling 	}
9423f9d6ad7SLin Ling 
94328e4da25SMatthew Ahrens 	if (mdb_pwalk("list", dbgmsg_cb, &da, sym.st_value) != 0) {
9443f9d6ad7SLin Ling 		mdb_warn("can't walk zfs_dbgmsgs");
9453f9d6ad7SLin Ling 		return (DCMD_ERR);
9463f9d6ad7SLin Ling 	}
9473f9d6ad7SLin Ling 
9483f9d6ad7SLin Ling 	return (DCMD_OK);
9493f9d6ad7SLin Ling }
9503f9d6ad7SLin Ling 
95144cb6abcSbmc /*ARGSUSED*/
95244cb6abcSbmc static int
95344cb6abcSbmc arc_print(uintptr_t addr, uint_t flags, int argc, const mdb_arg_t *argv)
95444cb6abcSbmc {
95544cb6abcSbmc 	kstat_named_t *stats;
95644cb6abcSbmc 	GElf_Sym sym;
95791ebeef5Sahrens 	int nstats, i;
95844cb6abcSbmc 	uint_t opt_a = FALSE;
95991ebeef5Sahrens 	uint_t opt_b = FALSE;
96091ebeef5Sahrens 	uint_t shift = 0;
96191ebeef5Sahrens 	const char *suffix;
96244cb6abcSbmc 
96391ebeef5Sahrens 	static const char *bytestats[] = {
9649253d63dSGeorge Wilson 		"p", "c", "c_min", "c_max", "size", "duplicate_buffers_size",
96520128a08SGeorge Wilson 		"arc_meta_used", "arc_meta_limit", "arc_meta_max",
9664076b1bfSPrakash Surya 		"arc_meta_min", "hdr_size", "data_size", "metadata_size",
9674076b1bfSPrakash Surya 		"other_size", "anon_size", "anon_evictable_data",
9684076b1bfSPrakash Surya 		"anon_evictable_metadata", "mru_size", "mru_evictable_data",
9694076b1bfSPrakash Surya 		"mru_evictable_metadata", "mru_ghost_size",
9704076b1bfSPrakash Surya 		"mru_ghost_evictable_data", "mru_ghost_evictable_metadata",
9714076b1bfSPrakash Surya 		"mfu_size", "mfu_evictable_data", "mfu_evictable_metadata",
9724076b1bfSPrakash Surya 		"mfu_ghost_size", "mfu_ghost_evictable_data",
9734076b1bfSPrakash Surya 		"mfu_ghost_evictable_metadata", "evict_l2_cached",
9744076b1bfSPrakash Surya 		"evict_l2_eligible", "evict_l2_ineligible", "l2_read_bytes",
9754076b1bfSPrakash Surya 		"l2_write_bytes", "l2_size", "l2_asize", "l2_hdr_size",
9769253d63dSGeorge Wilson 		NULL
97791ebeef5Sahrens 	};
97891ebeef5Sahrens 
97991ebeef5Sahrens 	static const char *extras[] = {
98091ebeef5Sahrens 		"arc_no_grow", "arc_tempreserve",
98191ebeef5Sahrens 		NULL
98244cb6abcSbmc 	};
98344cb6abcSbmc 
98422ce0148SMatthew Ahrens 	if (mdb_lookup_by_obj(ZFS_OBJ_NAME, "arc_stats", &sym) == -1) {
98544cb6abcSbmc 		mdb_warn("failed to find 'arc_stats'");
98644cb6abcSbmc 		return (DCMD_ERR);
98744cb6abcSbmc 	}
98844cb6abcSbmc 
98944cb6abcSbmc 	stats = mdb_zalloc(sym.st_size, UM_SLEEP | UM_GC);
99044cb6abcSbmc 
99144cb6abcSbmc 	if (mdb_vread(stats, sym.st_size, sym.st_value) == -1) {
99244cb6abcSbmc 		mdb_warn("couldn't read 'arc_stats' at %p", sym.st_value);
99344cb6abcSbmc 		return (DCMD_ERR);
99444cb6abcSbmc 	}
99544cb6abcSbmc 
99644cb6abcSbmc 	nstats = sym.st_size / sizeof (kstat_named_t);
99744cb6abcSbmc 
99891ebeef5Sahrens 	/* NB: -a / opt_a are ignored for backwards compatability */
99991ebeef5Sahrens 	if (mdb_getopts(argc, argv,
100091ebeef5Sahrens 	    'a', MDB_OPT_SETBITS, TRUE, &opt_a,
100191ebeef5Sahrens 	    'b', MDB_OPT_SETBITS, TRUE, &opt_b,
100291ebeef5Sahrens 	    'k', MDB_OPT_SETBITS, 10, &shift,
100391ebeef5Sahrens 	    'm', MDB_OPT_SETBITS, 20, &shift,
100491ebeef5Sahrens 	    'g', MDB_OPT_SETBITS, 30, &shift,
100591ebeef5Sahrens 	    NULL) != argc)
100644cb6abcSbmc 		return (DCMD_USAGE);
100744cb6abcSbmc 
100891ebeef5Sahrens 	if (!opt_b && !shift)
100991ebeef5Sahrens 		shift = 20;
101091ebeef5Sahrens 
101191ebeef5Sahrens 	switch (shift) {
101291ebeef5Sahrens 	case 0:
101391ebeef5Sahrens 		suffix = "B";
101491ebeef5Sahrens 		break;
101591ebeef5Sahrens 	case 10:
101691ebeef5Sahrens 		suffix = "KB";
101791ebeef5Sahrens 		break;
101891ebeef5Sahrens 	case 20:
101991ebeef5Sahrens 		suffix = "MB";
102091ebeef5Sahrens 		break;
102191ebeef5Sahrens 	case 30:
102291ebeef5Sahrens 		suffix = "GB";
102391ebeef5Sahrens 		break;
102491ebeef5Sahrens 	default:
102591ebeef5Sahrens 		suffix = "XX";
102691ebeef5Sahrens 	}
102791ebeef5Sahrens 
102891ebeef5Sahrens 	for (i = 0; i < nstats; i++) {
102991ebeef5Sahrens 		int j;
103091ebeef5Sahrens 		boolean_t bytes = B_FALSE;
103191ebeef5Sahrens 
103291ebeef5Sahrens 		for (j = 0; bytestats[j]; j++) {
103391ebeef5Sahrens 			if (strcmp(stats[i].name, bytestats[j]) == 0) {
103491ebeef5Sahrens 				bytes = B_TRUE;
103591ebeef5Sahrens 				break;
103691ebeef5Sahrens 			}
103791ebeef5Sahrens 		}
103844cb6abcSbmc 
103991ebeef5Sahrens 		if (bytes) {
104091ebeef5Sahrens 			mdb_printf("%-25s = %9llu %s\n", stats[i].name,
104191ebeef5Sahrens 			    stats[i].value.ui64 >> shift, suffix);
104291ebeef5Sahrens 		} else {
104391ebeef5Sahrens 			mdb_printf("%-25s = %9llu\n", stats[i].name,
104444cb6abcSbmc 			    stats[i].value.ui64);
104544cb6abcSbmc 		}
104644cb6abcSbmc 	}
104744cb6abcSbmc 
104891ebeef5Sahrens 	for (i = 0; extras[i]; i++) {
104991ebeef5Sahrens 		uint64_t buf;
105044cb6abcSbmc 
105122ce0148SMatthew Ahrens 		if (mdb_lookup_by_obj(ZFS_OBJ_NAME, extras[i], &sym) == -1) {
105291ebeef5Sahrens 			mdb_warn("failed to find '%s'", extras[i]);
105391ebeef5Sahrens 			return (DCMD_ERR);
105444cb6abcSbmc 		}
105544cb6abcSbmc 
105691ebeef5Sahrens 		if (sym.st_size != sizeof (uint64_t) &&
105791ebeef5Sahrens 		    sym.st_size != sizeof (uint32_t)) {
105891ebeef5Sahrens 			mdb_warn("expected scalar for variable '%s'\n",
105991ebeef5Sahrens 			    extras[i]);
106091ebeef5Sahrens 			return (DCMD_ERR);
106144cb6abcSbmc 		}
106244cb6abcSbmc 
106391ebeef5Sahrens 		if (mdb_vread(&buf, sym.st_size, sym.st_value) == -1) {
106491ebeef5Sahrens 			mdb_warn("couldn't read '%s'", extras[i]);
106591ebeef5Sahrens 			return (DCMD_ERR);
106644cb6abcSbmc 		}
106744cb6abcSbmc 
106891ebeef5Sahrens 		mdb_printf("%-25s = ", extras[i]);
106991ebeef5Sahrens 
107091ebeef5Sahrens 		/* NB: all the 64-bit extras happen to be byte counts */
107191ebeef5Sahrens 		if (sym.st_size == sizeof (uint64_t))
107291ebeef5Sahrens 			mdb_printf("%9llu %s\n", buf >> shift, suffix);
107344cb6abcSbmc 
107491ebeef5Sahrens 		if (sym.st_size == sizeof (uint32_t))
107591ebeef5Sahrens 			mdb_printf("%9d\n", *((uint32_t *)&buf));
107691ebeef5Sahrens 	}
107744cb6abcSbmc 	return (DCMD_OK);
107844cb6abcSbmc }
107944cb6abcSbmc 
108022ce0148SMatthew Ahrens typedef struct mdb_spa_print {
108122ce0148SMatthew Ahrens 	pool_state_t spa_state;
108222ce0148SMatthew Ahrens 	char spa_name[MAXNAMELEN];
108322ce0148SMatthew Ahrens } mdb_spa_print_t;
108422ce0148SMatthew Ahrens 
1085fa9e4066Sahrens /*
1086fa9e4066Sahrens  * ::spa
1087fa9e4066Sahrens  *
108822ce0148SMatthew Ahrens  *	-c	Print configuration information as well
108922ce0148SMatthew Ahrens  *	-v	Print vdev state
109022ce0148SMatthew Ahrens  *	-e	Print vdev error stats
10912e4c9986SGeorge Wilson  *	-m	Print vdev metaslab info
10922e4c9986SGeorge Wilson  *	-M	print vdev metaslab group info
10932e4c9986SGeorge Wilson  *	-h	Print histogram info (must be combined with -m or -M)
1094fa9e4066Sahrens  *
1095fa9e4066Sahrens  * Print a summarized spa_t.  When given no arguments, prints out a table of all
1096fa9e4066Sahrens  * active pools on the system.
1097fa9e4066Sahrens  */
1098fa9e4066Sahrens /* ARGSUSED */
1099fa9e4066Sahrens static int
1100fa9e4066Sahrens spa_print(uintptr_t addr, uint_t flags, int argc, const mdb_arg_t *argv)
1101fa9e4066Sahrens {
1102fa9e4066Sahrens 	const char *statetab[] = { "ACTIVE", "EXPORTED", "DESTROYED",
1103e14bb325SJeff Bonwick 		"SPARE", "L2CACHE", "UNINIT", "UNAVAIL", "POTENTIAL" };
1104fa9e4066Sahrens 	const char *state;
11052e4c9986SGeorge Wilson 	int spa_flags = 0;
1106fa9e4066Sahrens 
1107fa9e4066Sahrens 	if (mdb_getopts(argc, argv,
11082e4c9986SGeorge Wilson 	    'c', MDB_OPT_SETBITS, SPA_FLAG_CONFIG, &spa_flags,
11092e4c9986SGeorge Wilson 	    'v', MDB_OPT_SETBITS, SPA_FLAG_VDEVS, &spa_flags,
11102e4c9986SGeorge Wilson 	    'e', MDB_OPT_SETBITS, SPA_FLAG_ERRORS, &spa_flags,
11112e4c9986SGeorge Wilson 	    'M', MDB_OPT_SETBITS, SPA_FLAG_METASLAB_GROUPS, &spa_flags,
11122e4c9986SGeorge Wilson 	    'm', MDB_OPT_SETBITS, SPA_FLAG_METASLABS, &spa_flags,
11132e4c9986SGeorge Wilson 	    'h', MDB_OPT_SETBITS, SPA_FLAG_HISTOGRAMS, &spa_flags,
1114fa9e4066Sahrens 	    NULL) != argc)
1115fa9e4066Sahrens 		return (DCMD_USAGE);
1116fa9e4066Sahrens 
1117fa9e4066Sahrens 	if (!(flags & DCMD_ADDRSPEC)) {
1118fa9e4066Sahrens 		if (mdb_walk_dcmd("spa", "spa", argc, argv) == -1) {
1119fa9e4066Sahrens 			mdb_warn("can't walk spa");
1120fa9e4066Sahrens 			return (DCMD_ERR);
1121fa9e4066Sahrens 		}
1122fa9e4066Sahrens 
1123fa9e4066Sahrens 		return (DCMD_OK);
1124fa9e4066Sahrens 	}
1125fa9e4066Sahrens 
1126fa9e4066Sahrens 	if (flags & DCMD_PIPE_OUT) {
1127fa9e4066Sahrens 		mdb_printf("%#lr\n", addr);
1128fa9e4066Sahrens 		return (DCMD_OK);
1129fa9e4066Sahrens 	}
1130fa9e4066Sahrens 
1131fa9e4066Sahrens 	if (DCMD_HDRSPEC(flags))
1132fa9e4066Sahrens 		mdb_printf("%<u>%-?s %9s %-*s%</u>\n", "ADDR", "STATE",
1133fa9e4066Sahrens 		    sizeof (uintptr_t) == 4 ? 60 : 52, "NAME");
1134fa9e4066Sahrens 
113522ce0148SMatthew Ahrens 	mdb_spa_print_t spa;
113622ce0148SMatthew Ahrens 	if (mdb_ctf_vread(&spa, "spa_t", "mdb_spa_print_t", addr, 0) == -1)
1137fa9e4066Sahrens 		return (DCMD_ERR);
1138fa9e4066Sahrens 
1139fa9e4066Sahrens 	if (spa.spa_state < 0 || spa.spa_state > POOL_STATE_UNAVAIL)
1140ea8dc4b6Seschrock 		state = "UNKNOWN";
1141fa9e4066Sahrens 	else
1142fa9e4066Sahrens 		state = statetab[spa.spa_state];
1143fa9e4066Sahrens 
1144e14bb325SJeff Bonwick 	mdb_printf("%0?p %9s %s\n", addr, state, spa.spa_name);
1145fa9e4066Sahrens 
11462e4c9986SGeorge Wilson 	if (spa_flags & SPA_FLAG_CONFIG) {
1147fa9e4066Sahrens 		mdb_printf("\n");
1148fa9e4066Sahrens 		mdb_inc_indent(4);
1149fa9e4066Sahrens 		if (mdb_call_dcmd("spa_config", addr, flags, 0,
1150fa9e4066Sahrens 		    NULL) != DCMD_OK)
1151fa9e4066Sahrens 			return (DCMD_ERR);
1152fa9e4066Sahrens 		mdb_dec_indent(4);
1153fa9e4066Sahrens 	}
1154fa9e4066Sahrens 
11552e4c9986SGeorge Wilson 	if (spa_flags & SPA_FLAG_ALL_VDEV) {
1156fa9e4066Sahrens 		mdb_arg_t v;
11572e4c9986SGeorge Wilson 		char opts[100] = "-";
11582e4c9986SGeorge Wilson 		int args =
11592e4c9986SGeorge Wilson 		    (spa_flags | SPA_FLAG_VDEVS) == SPA_FLAG_VDEVS ? 0 : 1;
11602e4c9986SGeorge Wilson 
11612e4c9986SGeorge Wilson 		if (spa_flags & SPA_FLAG_ERRORS)
11622e4c9986SGeorge Wilson 			strcat(opts, "e");
11632e4c9986SGeorge Wilson 		if (spa_flags & SPA_FLAG_METASLABS)
11642e4c9986SGeorge Wilson 			strcat(opts, "m");
11652e4c9986SGeorge Wilson 		if (spa_flags & SPA_FLAG_METASLAB_GROUPS)
11662e4c9986SGeorge Wilson 			strcat(opts, "M");
11672e4c9986SGeorge Wilson 		if (spa_flags & SPA_FLAG_HISTOGRAMS)
11682e4c9986SGeorge Wilson 			strcat(opts, "h");
1169fa9e4066Sahrens 
1170fa9e4066Sahrens 		v.a_type = MDB_TYPE_STRING;
11712e4c9986SGeorge Wilson 		v.a_un.a_str = opts;
1172fa9e4066Sahrens 
1173fa9e4066Sahrens 		mdb_printf("\n");
1174fa9e4066Sahrens 		mdb_inc_indent(4);
11752e4c9986SGeorge Wilson 		if (mdb_call_dcmd("spa_vdevs", addr, flags, args,
1176fa9e4066Sahrens 		    &v) != DCMD_OK)
1177fa9e4066Sahrens 			return (DCMD_ERR);
1178fa9e4066Sahrens 		mdb_dec_indent(4);
1179fa9e4066Sahrens 	}
1180fa9e4066Sahrens 
1181fa9e4066Sahrens 	return (DCMD_OK);
1182fa9e4066Sahrens }
1183fa9e4066Sahrens 
118428e4da25SMatthew Ahrens typedef struct mdb_spa_config_spa {
1185d5ee8a13SMatthew Ahrens 	uintptr_t spa_config;
118628e4da25SMatthew Ahrens } mdb_spa_config_spa_t;
118728e4da25SMatthew Ahrens 
1188fa9e4066Sahrens /*
1189fa9e4066Sahrens  * ::spa_config
1190fa9e4066Sahrens  *
1191fa9e4066Sahrens  * Given a spa_t, print the configuration information stored in spa_config.
1192fa9e4066Sahrens  * Since it's just an nvlist, format it as an indented list of name=value pairs.
1193fa9e4066Sahrens  * We simply read the value of spa_config and pass off to ::nvlist.
1194fa9e4066Sahrens  */
1195fa9e4066Sahrens /* ARGSUSED */
1196fa9e4066Sahrens static int
1197fa9e4066Sahrens spa_print_config(uintptr_t addr, uint_t flags, int argc, const mdb_arg_t *argv)
1198fa9e4066Sahrens {
119928e4da25SMatthew Ahrens 	mdb_spa_config_spa_t spa;
1200fa9e4066Sahrens 
1201fa9e4066Sahrens 	if (argc != 0 || !(flags & DCMD_ADDRSPEC))
1202fa9e4066Sahrens 		return (DCMD_USAGE);
1203fa9e4066Sahrens 
120428e4da25SMatthew Ahrens 	if (mdb_ctf_vread(&spa, ZFS_STRUCT "spa", "mdb_spa_config_spa_t",
120528e4da25SMatthew Ahrens 	    addr, 0) == -1)
1206fa9e4066Sahrens 		return (DCMD_ERR);
1207fa9e4066Sahrens 
1208d5ee8a13SMatthew Ahrens 	if (spa.spa_config == 0) {
1209fa9e4066Sahrens 		mdb_printf("(none)\n");
1210fa9e4066Sahrens 		return (DCMD_OK);
1211fa9e4066Sahrens 	}
1212fa9e4066Sahrens 
1213d5ee8a13SMatthew Ahrens 	return (mdb_call_dcmd("nvlist", spa.spa_config, flags,
1214fa9e4066Sahrens 	    0, NULL));
1215fa9e4066Sahrens }
1216fa9e4066Sahrens 
12172e4c9986SGeorge Wilson const char histo_stars[] = "****************************************";
12182e4c9986SGeorge Wilson const int histo_width = sizeof (histo_stars) - 1;
12192e4c9986SGeorge Wilson 
12202e4c9986SGeorge Wilson static void
12212e4c9986SGeorge Wilson dump_histogram(const uint64_t *histo, int size, int offset)
12222e4c9986SGeorge Wilson {
12232e4c9986SGeorge Wilson 	int i;
12242e4c9986SGeorge Wilson 	int minidx = size - 1;
12252e4c9986SGeorge Wilson 	int maxidx = 0;
12262e4c9986SGeorge Wilson 	uint64_t max = 0;
12272e4c9986SGeorge Wilson 
12282e4c9986SGeorge Wilson 	for (i = 0; i < size; i++) {
12292e4c9986SGeorge Wilson 		if (histo[i] > max)
12302e4c9986SGeorge Wilson 			max = histo[i];
12312e4c9986SGeorge Wilson 		if (histo[i] > 0 && i > maxidx)
12322e4c9986SGeorge Wilson 			maxidx = i;
12332e4c9986SGeorge Wilson 		if (histo[i] > 0 && i < minidx)
12342e4c9986SGeorge Wilson 			minidx = i;
12352e4c9986SGeorge Wilson 	}
12362e4c9986SGeorge Wilson 
12372e4c9986SGeorge Wilson 	if (max < histo_width)
12382e4c9986SGeorge Wilson 		max = histo_width;
12392e4c9986SGeorge Wilson 
12402e4c9986SGeorge Wilson 	for (i = minidx; i <= maxidx; i++) {
12412e4c9986SGeorge Wilson 		mdb_printf("%3u: %6llu %s\n",
12422e4c9986SGeorge Wilson 		    i + offset, (u_longlong_t)histo[i],
12432e4c9986SGeorge Wilson 		    &histo_stars[(max - histo[i]) * histo_width / max]);
12442e4c9986SGeorge Wilson 	}
12452e4c9986SGeorge Wilson }
12462e4c9986SGeorge Wilson 
12472e4c9986SGeorge Wilson typedef struct mdb_range_tree {
12482e4c9986SGeorge Wilson 	uint64_t rt_space;
12492e4c9986SGeorge Wilson } mdb_range_tree_t;
12502e4c9986SGeorge Wilson 
12512e4c9986SGeorge Wilson typedef struct mdb_metaslab_group {
12522e4c9986SGeorge Wilson 	uint64_t mg_fragmentation;
12532e4c9986SGeorge Wilson 	uint64_t mg_histogram[RANGE_TREE_HISTOGRAM_SIZE];
12542e4c9986SGeorge Wilson } mdb_metaslab_group_t;
12552e4c9986SGeorge Wilson 
12562e4c9986SGeorge Wilson typedef struct mdb_metaslab {
12572e4c9986SGeorge Wilson 	uint64_t ms_id;
12582e4c9986SGeorge Wilson 	uint64_t ms_start;
12592e4c9986SGeorge Wilson 	uint64_t ms_size;
12602e4c9986SGeorge Wilson 	uint64_t ms_fragmentation;
12612e4c9986SGeorge Wilson 	uintptr_t ms_alloctree[TXG_SIZE];
12622e4c9986SGeorge Wilson 	uintptr_t ms_freetree[TXG_SIZE];
12632e4c9986SGeorge Wilson 	uintptr_t ms_tree;
12642e4c9986SGeorge Wilson 	uintptr_t ms_sm;
12652e4c9986SGeorge Wilson } mdb_metaslab_t;
12662e4c9986SGeorge Wilson 
12672e4c9986SGeorge Wilson typedef struct mdb_space_map_phys_t {
12682e4c9986SGeorge Wilson 	uint64_t smp_alloc;
12692e4c9986SGeorge Wilson 	uint64_t smp_histogram[SPACE_MAP_HISTOGRAM_SIZE];
12702e4c9986SGeorge Wilson } mdb_space_map_phys_t;
12712e4c9986SGeorge Wilson 
12722e4c9986SGeorge Wilson typedef struct mdb_space_map {
12732e4c9986SGeorge Wilson 	uint64_t sm_size;
12742e4c9986SGeorge Wilson 	uint8_t sm_shift;
12752e4c9986SGeorge Wilson 	uint64_t sm_alloc;
12762e4c9986SGeorge Wilson 	uintptr_t sm_phys;
12772e4c9986SGeorge Wilson } mdb_space_map_t;
12782e4c9986SGeorge Wilson 
12792e4c9986SGeorge Wilson typedef struct mdb_vdev {
12802e4c9986SGeorge Wilson 	uintptr_t vdev_ms;
12812e4c9986SGeorge Wilson 	uint64_t vdev_ms_count;
12822e4c9986SGeorge Wilson 	vdev_stat_t vdev_stat;
12832e4c9986SGeorge Wilson } mdb_vdev_t;
12842e4c9986SGeorge Wilson 
12852e4c9986SGeorge Wilson static int
12862e4c9986SGeorge Wilson metaslab_stats(uintptr_t addr, int spa_flags)
12872e4c9986SGeorge Wilson {
12882e4c9986SGeorge Wilson 	mdb_vdev_t vdev;
12892e4c9986SGeorge Wilson 	uintptr_t *vdev_ms;
12902e4c9986SGeorge Wilson 
12912e4c9986SGeorge Wilson 	if (mdb_ctf_vread(&vdev, "vdev_t", "mdb_vdev_t",
12922e4c9986SGeorge Wilson 	    (uintptr_t)addr, 0) == -1) {
12932e4c9986SGeorge Wilson 		mdb_warn("failed to read vdev at %p\n", addr);
12942e4c9986SGeorge Wilson 		return (DCMD_ERR);
12952e4c9986SGeorge Wilson 	}
12962e4c9986SGeorge Wilson 
12972e4c9986SGeorge Wilson 	mdb_inc_indent(4);
12982e4c9986SGeorge Wilson 	mdb_printf("%<u>%-?s %6s %20s %10s %9s%</u>\n", "ADDR", "ID",
12992e4c9986SGeorge Wilson 	    "OFFSET", "FREE", "FRAGMENTATION");
13002e4c9986SGeorge Wilson 
13012e4c9986SGeorge Wilson 	vdev_ms = mdb_alloc(vdev.vdev_ms_count * sizeof (void *),
13022e4c9986SGeorge Wilson 	    UM_SLEEP | UM_GC);
13032e4c9986SGeorge Wilson 	if (mdb_vread(vdev_ms, vdev.vdev_ms_count * sizeof (void *),
13042e4c9986SGeorge Wilson 	    (uintptr_t)vdev.vdev_ms) == -1) {
13052e4c9986SGeorge Wilson 		mdb_warn("failed to read vdev_ms at %p\n", vdev.vdev_ms);
13062e4c9986SGeorge Wilson 		return (DCMD_ERR);
13072e4c9986SGeorge Wilson 	}
13082e4c9986SGeorge Wilson 
13092e4c9986SGeorge Wilson 	for (int m = 0; m < vdev.vdev_ms_count; m++) {
13102e4c9986SGeorge Wilson 		mdb_metaslab_t ms;
13112e4c9986SGeorge Wilson 		mdb_space_map_t sm = { 0 };
13122e4c9986SGeorge Wilson 		char free[NICENUM_BUFLEN];
13132e4c9986SGeorge Wilson 
13142e4c9986SGeorge Wilson 		if (mdb_ctf_vread(&ms, "metaslab_t", "mdb_metaslab_t",
13152e4c9986SGeorge Wilson 		    (uintptr_t)vdev_ms[m], 0) == -1)
13162e4c9986SGeorge Wilson 			return (DCMD_ERR);
13172e4c9986SGeorge Wilson 
13182e4c9986SGeorge Wilson 		if (ms.ms_sm != NULL &&
13192e4c9986SGeorge Wilson 		    mdb_ctf_vread(&sm, "space_map_t", "mdb_space_map_t",
13202e4c9986SGeorge Wilson 		    ms.ms_sm, 0) == -1)
13212e4c9986SGeorge Wilson 			return (DCMD_ERR);
13222e4c9986SGeorge Wilson 
13232e4c9986SGeorge Wilson 		mdb_nicenum(ms.ms_size - sm.sm_alloc, free);
13242e4c9986SGeorge Wilson 
13252e4c9986SGeorge Wilson 		mdb_printf("%0?p %6llu %20llx %10s ", vdev_ms[m], ms.ms_id,
13262e4c9986SGeorge Wilson 		    ms.ms_start, free);
13272e4c9986SGeorge Wilson 		if (ms.ms_fragmentation == ZFS_FRAG_INVALID)
13282e4c9986SGeorge Wilson 			mdb_printf("%9s\n", "-");
13292e4c9986SGeorge Wilson 		else
13302e4c9986SGeorge Wilson 			mdb_printf("%9llu%%\n", ms.ms_fragmentation);
13312e4c9986SGeorge Wilson 
13322e4c9986SGeorge Wilson 		if ((spa_flags & SPA_FLAG_HISTOGRAMS) && ms.ms_sm != NULL) {
13332e4c9986SGeorge Wilson 			mdb_space_map_phys_t smp;
13342e4c9986SGeorge Wilson 
13352e4c9986SGeorge Wilson 			if (sm.sm_phys == NULL)
13362e4c9986SGeorge Wilson 				continue;
13372e4c9986SGeorge Wilson 
13382e4c9986SGeorge Wilson 			(void) mdb_ctf_vread(&smp, "space_map_phys_t",
13392e4c9986SGeorge Wilson 			    "mdb_space_map_phys_t", sm.sm_phys, 0);
13402e4c9986SGeorge Wilson 
13412e4c9986SGeorge Wilson 			dump_histogram(smp.smp_histogram,
13422e4c9986SGeorge Wilson 			    SPACE_MAP_HISTOGRAM_SIZE, sm.sm_shift);
13432e4c9986SGeorge Wilson 		}
13442e4c9986SGeorge Wilson 	}
13452e4c9986SGeorge Wilson 	mdb_dec_indent(4);
13462e4c9986SGeorge Wilson 	return (DCMD_OK);
13472e4c9986SGeorge Wilson }
13482e4c9986SGeorge Wilson 
13492e4c9986SGeorge Wilson static int
13502e4c9986SGeorge Wilson metaslab_group_stats(uintptr_t addr, int spa_flags)
13512e4c9986SGeorge Wilson {
13522e4c9986SGeorge Wilson 	mdb_metaslab_group_t mg;
13532e4c9986SGeorge Wilson 	if (mdb_ctf_vread(&mg, "metaslab_group_t", "mdb_metaslab_group_t",
13542e4c9986SGeorge Wilson 	    (uintptr_t)addr, 0) == -1) {
13552e4c9986SGeorge Wilson 		mdb_warn("failed to read vdev_mg at %p\n", addr);
13562e4c9986SGeorge Wilson 		return (DCMD_ERR);
13572e4c9986SGeorge Wilson 	}
13582e4c9986SGeorge Wilson 
13592e4c9986SGeorge Wilson 	mdb_inc_indent(4);
13602e4c9986SGeorge Wilson 	mdb_printf("%<u>%-?s %15s%</u>\n", "ADDR", "FRAGMENTATION");
13612e4c9986SGeorge Wilson 	if (mg.mg_fragmentation == ZFS_FRAG_INVALID)
13622e4c9986SGeorge Wilson 		mdb_printf("%0?p %15s\n", addr, "-");
13632e4c9986SGeorge Wilson 	else
13642e4c9986SGeorge Wilson 		mdb_printf("%0?p %15llu%%\n", addr, mg.mg_fragmentation);
13652e4c9986SGeorge Wilson 
13662e4c9986SGeorge Wilson 	if (spa_flags & SPA_FLAG_HISTOGRAMS)
13672e4c9986SGeorge Wilson 		dump_histogram(mg.mg_histogram, RANGE_TREE_HISTOGRAM_SIZE, 0);
13682e4c9986SGeorge Wilson 	mdb_dec_indent(4);
13692e4c9986SGeorge Wilson 	return (DCMD_OK);
13702e4c9986SGeorge Wilson }
13712e4c9986SGeorge Wilson 
1372fa9e4066Sahrens /*
1373fa9e4066Sahrens  * ::vdev
1374fa9e4066Sahrens  *
1375fa9e4066Sahrens  * Print out a summarized vdev_t, in the following form:
1376fa9e4066Sahrens  *
1377fa9e4066Sahrens  * ADDR             STATE	AUX            DESC
1378fa9e4066Sahrens  * fffffffbcde23df0 HEALTHY	-              /dev/dsk/c0t0d0
1379fa9e4066Sahrens  *
1380fa9e4066Sahrens  * If '-r' is specified, recursively visit all children.
1381fa9e4066Sahrens  *
1382fa9e4066Sahrens  * With '-e', the statistics associated with the vdev are printed as well.
1383fa9e4066Sahrens  */
1384fa9e4066Sahrens static int
13852e4c9986SGeorge Wilson do_print_vdev(uintptr_t addr, int flags, int depth, boolean_t recursive,
13862e4c9986SGeorge Wilson     int spa_flags)
1387fa9e4066Sahrens {
1388fa9e4066Sahrens 	vdev_t vdev;
1389fa9e4066Sahrens 	char desc[MAXNAMELEN];
1390fa9e4066Sahrens 	int c, children;
1391fa9e4066Sahrens 	uintptr_t *child;
1392fa9e4066Sahrens 	const char *state, *aux;
1393fa9e4066Sahrens 
1394fa9e4066Sahrens 	if (mdb_vread(&vdev, sizeof (vdev), (uintptr_t)addr) == -1) {
1395fa9e4066Sahrens 		mdb_warn("failed to read vdev_t at %p\n", (uintptr_t)addr);
1396fa9e4066Sahrens 		return (DCMD_ERR);
1397fa9e4066Sahrens 	}
1398fa9e4066Sahrens 
1399fa9e4066Sahrens 	if (flags & DCMD_PIPE_OUT) {
1400b4952e17SGeorge Wilson 		mdb_printf("%#lr\n", addr);
1401fa9e4066Sahrens 	} else {
1402fa9e4066Sahrens 		if (vdev.vdev_path != NULL) {
1403fa9e4066Sahrens 			if (mdb_readstr(desc, sizeof (desc),
1404fa9e4066Sahrens 			    (uintptr_t)vdev.vdev_path) == -1) {
1405fa9e4066Sahrens 				mdb_warn("failed to read vdev_path at %p\n",
1406fa9e4066Sahrens 				    vdev.vdev_path);
1407fa9e4066Sahrens 				return (DCMD_ERR);
1408fa9e4066Sahrens 			}
1409fa9e4066Sahrens 		} else if (vdev.vdev_ops != NULL) {
1410fa9e4066Sahrens 			vdev_ops_t ops;
1411fa9e4066Sahrens 			if (mdb_vread(&ops, sizeof (ops),
1412fa9e4066Sahrens 			    (uintptr_t)vdev.vdev_ops) == -1) {
1413fa9e4066Sahrens 				mdb_warn("failed to read vdev_ops at %p\n",
1414fa9e4066Sahrens 				    vdev.vdev_ops);
1415fa9e4066Sahrens 				return (DCMD_ERR);
1416fa9e4066Sahrens 			}
1417fa9e4066Sahrens 			(void) strcpy(desc, ops.vdev_op_type);
1418fa9e4066Sahrens 		} else {
1419fa9e4066Sahrens 			(void) strcpy(desc, "<unknown>");
1420fa9e4066Sahrens 		}
1421fa9e4066Sahrens 
1422fa9e4066Sahrens 		if (depth == 0 && DCMD_HDRSPEC(flags))
1423fa9e4066Sahrens 			mdb_printf("%<u>%-?s %-9s %-12s %-*s%</u>\n",
1424fa9e4066Sahrens 			    "ADDR", "STATE", "AUX",
1425fa9e4066Sahrens 			    sizeof (uintptr_t) == 4 ? 43 : 35,
1426fa9e4066Sahrens 			    "DESCRIPTION");
1427fa9e4066Sahrens 
1428fa9e4066Sahrens 		mdb_printf("%0?p ", addr);
1429fa9e4066Sahrens 
1430fa9e4066Sahrens 		switch (vdev.vdev_state) {
1431fa9e4066Sahrens 		case VDEV_STATE_CLOSED:
1432ccae0b50Seschrock 			state = "CLOSED";
1433ccae0b50Seschrock 			break;
1434fa9e4066Sahrens 		case VDEV_STATE_OFFLINE:
1435ccae0b50Seschrock 			state = "OFFLINE";
1436ccae0b50Seschrock 			break;
1437fa9e4066Sahrens 		case VDEV_STATE_CANT_OPEN:
1438ccae0b50Seschrock 			state = "CANT_OPEN";
1439ccae0b50Seschrock 			break;
1440fa9e4066Sahrens 		case VDEV_STATE_DEGRADED:
1441ccae0b50Seschrock 			state = "DEGRADED";
1442ccae0b50Seschrock 			break;
1443fa9e4066Sahrens 		case VDEV_STATE_HEALTHY:
1444ccae0b50Seschrock 			state = "HEALTHY";
1445ccae0b50Seschrock 			break;
14463d7072f8Seschrock 		case VDEV_STATE_REMOVED:
14473d7072f8Seschrock 			state = "REMOVED";
14483d7072f8Seschrock 			break;
14493d7072f8Seschrock 		case VDEV_STATE_FAULTED:
14503d7072f8Seschrock 			state = "FAULTED";
14513d7072f8Seschrock 			break;
1452fa9e4066Sahrens 		default:
1453ccae0b50Seschrock 			state = "UNKNOWN";
1454ccae0b50Seschrock 			break;
1455fa9e4066Sahrens 		}
1456fa9e4066Sahrens 
1457fa9e4066Sahrens 		switch (vdev.vdev_stat.vs_aux) {
1458fa9e4066Sahrens 		case VDEV_AUX_NONE:
1459fa9e4066Sahrens 			aux = "-";
1460fa9e4066Sahrens 			break;
1461fa9e4066Sahrens 		case VDEV_AUX_OPEN_FAILED:
1462fa9e4066Sahrens 			aux = "OPEN_FAILED";
1463fa9e4066Sahrens 			break;
1464fa9e4066Sahrens 		case VDEV_AUX_CORRUPT_DATA:
1465fa9e4066Sahrens 			aux = "CORRUPT_DATA";
1466fa9e4066Sahrens 			break;
1467fa9e4066Sahrens 		case VDEV_AUX_NO_REPLICAS:
1468fa9e4066Sahrens 			aux = "NO_REPLICAS";
1469fa9e4066Sahrens 			break;
1470fa9e4066Sahrens 		case VDEV_AUX_BAD_GUID_SUM:
1471fa9e4066Sahrens 			aux = "BAD_GUID_SUM";
1472fa9e4066Sahrens 			break;
1473fa9e4066Sahrens 		case VDEV_AUX_TOO_SMALL:
1474fa9e4066Sahrens 			aux = "TOO_SMALL";
1475fa9e4066Sahrens 			break;
1476fa9e4066Sahrens 		case VDEV_AUX_BAD_LABEL:
1477fa9e4066Sahrens 			aux = "BAD_LABEL";
1478fa9e4066Sahrens 			break;
1479b87f3af3Sperrin 		case VDEV_AUX_VERSION_NEWER:
1480b87f3af3Sperrin 			aux = "VERS_NEWER";
1481b87f3af3Sperrin 			break;
1482b87f3af3Sperrin 		case VDEV_AUX_VERSION_OLDER:
1483b87f3af3Sperrin 			aux = "VERS_OLDER";
1484b87f3af3Sperrin 			break;
1485ad135b5dSChristopher Siden 		case VDEV_AUX_UNSUP_FEAT:
1486ad135b5dSChristopher Siden 			aux = "UNSUP_FEAT";
1487ad135b5dSChristopher Siden 			break;
1488b87f3af3Sperrin 		case VDEV_AUX_SPARED:
1489b87f3af3Sperrin 			aux = "SPARED";
1490b87f3af3Sperrin 			break;
1491b87f3af3Sperrin 		case VDEV_AUX_ERR_EXCEEDED:
1492b87f3af3Sperrin 			aux = "ERR_EXCEEDED";
1493b87f3af3Sperrin 			break;
1494b87f3af3Sperrin 		case VDEV_AUX_IO_FAILURE:
1495b87f3af3Sperrin 			aux = "IO_FAILURE";
1496b87f3af3Sperrin 			break;
1497b87f3af3Sperrin 		case VDEV_AUX_BAD_LOG:
1498b87f3af3Sperrin 			aux = "BAD_LOG";
1499b87f3af3Sperrin 			break;
15001195e687SMark J Musante 		case VDEV_AUX_EXTERNAL:
15011195e687SMark J Musante 			aux = "EXTERNAL";
15021195e687SMark J Musante 			break;
15031195e687SMark J Musante 		case VDEV_AUX_SPLIT_POOL:
15041195e687SMark J Musante 			aux = "SPLIT_POOL";
15051195e687SMark J Musante 			break;
1506fa9e4066Sahrens 		default:
1507fa9e4066Sahrens 			aux = "UNKNOWN";
1508fa9e4066Sahrens 			break;
1509fa9e4066Sahrens 		}
1510fa9e4066Sahrens 
1511fa9e4066Sahrens 		mdb_printf("%-9s %-12s %*s%s\n", state, aux, depth, "", desc);
1512fa9e4066Sahrens 
15132e4c9986SGeorge Wilson 		if (spa_flags & SPA_FLAG_ERRORS) {
1514fa9e4066Sahrens 			vdev_stat_t *vs = &vdev.vdev_stat;
1515fa9e4066Sahrens 			int i;
1516fa9e4066Sahrens 
1517fa9e4066Sahrens 			mdb_inc_indent(4);
1518fa9e4066Sahrens 			mdb_printf("\n");
1519fa9e4066Sahrens 			mdb_printf("%<u>       %12s %12s %12s %12s "
1520fa9e4066Sahrens 			    "%12s%</u>\n", "READ", "WRITE", "FREE", "CLAIM",
1521fa9e4066Sahrens 			    "IOCTL");
1522fa9e4066Sahrens 			mdb_printf("OPS     ");
1523fa9e4066Sahrens 			for (i = 1; i < ZIO_TYPES; i++)
1524fa9e4066Sahrens 				mdb_printf("%11#llx%s", vs->vs_ops[i],
1525fa9e4066Sahrens 				    i == ZIO_TYPES - 1 ? "" : "  ");
1526fa9e4066Sahrens 			mdb_printf("\n");
1527fa9e4066Sahrens 			mdb_printf("BYTES   ");
1528fa9e4066Sahrens 			for (i = 1; i < ZIO_TYPES; i++)
1529fa9e4066Sahrens 				mdb_printf("%11#llx%s", vs->vs_bytes[i],
1530fa9e4066Sahrens 				    i == ZIO_TYPES - 1 ? "" : "  ");
1531fa9e4066Sahrens 
1532fa9e4066Sahrens 
1533fa9e4066Sahrens 			mdb_printf("\n");
1534fa9e4066Sahrens 			mdb_printf("EREAD    %10#llx\n", vs->vs_read_errors);
1535fa9e4066Sahrens 			mdb_printf("EWRITE   %10#llx\n", vs->vs_write_errors);
1536fa9e4066Sahrens 			mdb_printf("ECKSUM   %10#llx\n",
1537fa9e4066Sahrens 			    vs->vs_checksum_errors);
1538fa9e4066Sahrens 			mdb_dec_indent(4);
15392e4c9986SGeorge Wilson 			mdb_printf("\n");
1540fa9e4066Sahrens 		}
1541fa9e4066Sahrens 
15422e4c9986SGeorge Wilson 		if (spa_flags & SPA_FLAG_METASLAB_GROUPS &&
15432e4c9986SGeorge Wilson 		    vdev.vdev_mg != NULL) {
15442e4c9986SGeorge Wilson 			metaslab_group_stats((uintptr_t)vdev.vdev_mg,
15452e4c9986SGeorge Wilson 			    spa_flags);
15462e4c9986SGeorge Wilson 		}
15472e4c9986SGeorge Wilson 		if (spa_flags & SPA_FLAG_METASLABS && vdev.vdev_ms != NULL) {
15482e4c9986SGeorge Wilson 			metaslab_stats((uintptr_t)addr, spa_flags);
15492e4c9986SGeorge Wilson 		}
1550fa9e4066Sahrens 	}
1551fa9e4066Sahrens 
1552fa9e4066Sahrens 	children = vdev.vdev_children;
1553fa9e4066Sahrens 
1554fa9e4066Sahrens 	if (children == 0 || !recursive)
1555fa9e4066Sahrens 		return (DCMD_OK);
1556fa9e4066Sahrens 
1557fa9e4066Sahrens 	child = mdb_alloc(children * sizeof (void *), UM_SLEEP | UM_GC);
1558fa9e4066Sahrens 	if (mdb_vread(child, children * sizeof (void *),
1559fa9e4066Sahrens 	    (uintptr_t)vdev.vdev_child) == -1) {
1560fa9e4066Sahrens 		mdb_warn("failed to read vdev children at %p", vdev.vdev_child);
1561fa9e4066Sahrens 		return (DCMD_ERR);
1562fa9e4066Sahrens 	}
1563fa9e4066Sahrens 
1564fa9e4066Sahrens 	for (c = 0; c < children; c++) {
15652e4c9986SGeorge Wilson 		if (do_print_vdev(child[c], flags, depth + 2, recursive,
15662e4c9986SGeorge Wilson 		    spa_flags)) {
1567fa9e4066Sahrens 			return (DCMD_ERR);
15682e4c9986SGeorge Wilson 		}
1569fa9e4066Sahrens 	}
1570fa9e4066Sahrens 
1571fa9e4066Sahrens 	return (DCMD_OK);
1572fa9e4066Sahrens }
1573fa9e4066Sahrens 
1574fa9e4066Sahrens static int
1575fa9e4066Sahrens vdev_print(uintptr_t addr, uint_t flags, int argc, const mdb_arg_t *argv)
1576fa9e4066Sahrens {
1577c5904d13Seschrock 	uint64_t depth = 0;
15782e4c9986SGeorge Wilson 	boolean_t recursive = B_FALSE;
15792e4c9986SGeorge Wilson 	int spa_flags = 0;
1580fa9e4066Sahrens 
1581fa9e4066Sahrens 	if (mdb_getopts(argc, argv,
15822e4c9986SGeorge Wilson 	    'e', MDB_OPT_SETBITS, SPA_FLAG_ERRORS, &spa_flags,
15832e4c9986SGeorge Wilson 	    'm', MDB_OPT_SETBITS, SPA_FLAG_METASLABS, &spa_flags,
15842e4c9986SGeorge Wilson 	    'M', MDB_OPT_SETBITS, SPA_FLAG_METASLAB_GROUPS, &spa_flags,
15852e4c9986SGeorge Wilson 	    'h', MDB_OPT_SETBITS, SPA_FLAG_HISTOGRAMS, &spa_flags,
1586fa9e4066Sahrens 	    'r', MDB_OPT_SETBITS, TRUE, &recursive,
15872e4c9986SGeorge Wilson 	    'd', MDB_OPT_UINT64, &depth, NULL) != argc)
1588fa9e4066Sahrens 		return (DCMD_USAGE);
1589fa9e4066Sahrens 
1590fa9e4066Sahrens 	if (!(flags & DCMD_ADDRSPEC)) {
1591fa9e4066Sahrens 		mdb_warn("no vdev_t address given\n");
1592fa9e4066Sahrens 		return (DCMD_ERR);
1593fa9e4066Sahrens 	}
1594fa9e4066Sahrens 
15952e4c9986SGeorge Wilson 	return (do_print_vdev(addr, flags, (int)depth, recursive, spa_flags));
1596fa9e4066Sahrens }
1597fa9e4066Sahrens 
15985f5f7a6fSahrens typedef struct metaslab_walk_data {
15995f5f7a6fSahrens 	uint64_t mw_numvdevs;
16005f5f7a6fSahrens 	uintptr_t *mw_vdevs;
16015f5f7a6fSahrens 	int mw_curvdev;
16025f5f7a6fSahrens 	uint64_t mw_nummss;
16035f5f7a6fSahrens 	uintptr_t *mw_mss;
16045f5f7a6fSahrens 	int mw_curms;
16055f5f7a6fSahrens } metaslab_walk_data_t;
16065f5f7a6fSahrens 
16075f5f7a6fSahrens static int
16085f5f7a6fSahrens metaslab_walk_step(mdb_walk_state_t *wsp)
16095f5f7a6fSahrens {
16105f5f7a6fSahrens 	metaslab_walk_data_t *mw = wsp->walk_data;
16115f5f7a6fSahrens 	metaslab_t ms;
16125f5f7a6fSahrens 	uintptr_t msp;
16135f5f7a6fSahrens 
16145f5f7a6fSahrens 	if (mw->mw_curvdev >= mw->mw_numvdevs)
16155f5f7a6fSahrens 		return (WALK_DONE);
16165f5f7a6fSahrens 
16175f5f7a6fSahrens 	if (mw->mw_mss == NULL) {
16185f5f7a6fSahrens 		uintptr_t mssp;
16195f5f7a6fSahrens 		uintptr_t vdevp;
16205f5f7a6fSahrens 
16215f5f7a6fSahrens 		ASSERT(mw->mw_curms == 0);
16225f5f7a6fSahrens 		ASSERT(mw->mw_nummss == 0);
16235f5f7a6fSahrens 
16245f5f7a6fSahrens 		vdevp = mw->mw_vdevs[mw->mw_curvdev];
162528e4da25SMatthew Ahrens 		if (GETMEMB(vdevp, "vdev", vdev_ms, mssp) ||
162628e4da25SMatthew Ahrens 		    GETMEMB(vdevp, "vdev", vdev_ms_count, mw->mw_nummss)) {
16275f5f7a6fSahrens 			return (WALK_ERR);
16285f5f7a6fSahrens 		}
16295f5f7a6fSahrens 
16305f5f7a6fSahrens 		mw->mw_mss = mdb_alloc(mw->mw_nummss * sizeof (void*),
16315f5f7a6fSahrens 		    UM_SLEEP | UM_GC);
16325f5f7a6fSahrens 		if (mdb_vread(mw->mw_mss, mw->mw_nummss * sizeof (void*),
16335f5f7a6fSahrens 		    mssp) == -1) {
16345f5f7a6fSahrens 			mdb_warn("failed to read vdev_ms at %p", mssp);
16355f5f7a6fSahrens 			return (WALK_ERR);
16365f5f7a6fSahrens 		}
16375f5f7a6fSahrens 	}
16385f5f7a6fSahrens 
16395f5f7a6fSahrens 	if (mw->mw_curms >= mw->mw_nummss) {
16405f5f7a6fSahrens 		mw->mw_mss = NULL;
16415f5f7a6fSahrens 		mw->mw_curms = 0;
16425f5f7a6fSahrens 		mw->mw_nummss = 0;
16435f5f7a6fSahrens 		mw->mw_curvdev++;
16445f5f7a6fSahrens 		return (WALK_NEXT);
16455f5f7a6fSahrens 	}
16465f5f7a6fSahrens 
16475f5f7a6fSahrens 	msp = mw->mw_mss[mw->mw_curms];
16485f5f7a6fSahrens 	if (mdb_vread(&ms, sizeof (metaslab_t), msp) == -1) {
16495f5f7a6fSahrens 		mdb_warn("failed to read metaslab_t at %p", msp);
16505f5f7a6fSahrens 		return (WALK_ERR);
16515f5f7a6fSahrens 	}
16525f5f7a6fSahrens 
16535f5f7a6fSahrens 	mw->mw_curms++;
16545f5f7a6fSahrens 
16555f5f7a6fSahrens 	return (wsp->walk_callback(msp, &ms, wsp->walk_cbdata));
16565f5f7a6fSahrens }
16575f5f7a6fSahrens 
16585f5f7a6fSahrens /* ARGSUSED */
16595f5f7a6fSahrens static int
16605f5f7a6fSahrens metaslab_walk_init(mdb_walk_state_t *wsp)
16615f5f7a6fSahrens {
16625f5f7a6fSahrens 	metaslab_walk_data_t *mw;
16635f5f7a6fSahrens 	uintptr_t root_vdevp;
16645f5f7a6fSahrens 	uintptr_t childp;
16655f5f7a6fSahrens 
16665f5f7a6fSahrens 	if (wsp->walk_addr == NULL) {
16675f5f7a6fSahrens 		mdb_warn("must supply address of spa_t\n");
16685f5f7a6fSahrens 		return (WALK_ERR);
16695f5f7a6fSahrens 	}
16705f5f7a6fSahrens 
16715f5f7a6fSahrens 	mw = mdb_zalloc(sizeof (metaslab_walk_data_t), UM_SLEEP | UM_GC);
16725f5f7a6fSahrens 
167328e4da25SMatthew Ahrens 	if (GETMEMB(wsp->walk_addr, "spa", spa_root_vdev, root_vdevp) ||
167428e4da25SMatthew Ahrens 	    GETMEMB(root_vdevp, "vdev", vdev_children, mw->mw_numvdevs) ||
167528e4da25SMatthew Ahrens 	    GETMEMB(root_vdevp, "vdev", vdev_child, childp)) {
16765f5f7a6fSahrens 		return (DCMD_ERR);
16775f5f7a6fSahrens 	}
16785f5f7a6fSahrens 
16795f5f7a6fSahrens 	mw->mw_vdevs = mdb_alloc(mw->mw_numvdevs * sizeof (void *),
16805f5f7a6fSahrens 	    UM_SLEEP | UM_GC);
16815f5f7a6fSahrens 	if (mdb_vread(mw->mw_vdevs, mw->mw_numvdevs * sizeof (void *),
16825f5f7a6fSahrens 	    childp) == -1) {
16835f5f7a6fSahrens 		mdb_warn("failed to read root vdev children at %p", childp);
16845f5f7a6fSahrens 		return (DCMD_ERR);
16855f5f7a6fSahrens 	}
16865f5f7a6fSahrens 
16875f5f7a6fSahrens 	wsp->walk_data = mw;
16885f5f7a6fSahrens 
16895f5f7a6fSahrens 	return (WALK_NEXT);
16905f5f7a6fSahrens }
16915f5f7a6fSahrens 
1692fa9e4066Sahrens typedef struct mdb_spa {
1693fa9e4066Sahrens 	uintptr_t spa_dsl_pool;
1694fa9e4066Sahrens 	uintptr_t spa_root_vdev;
1695fa9e4066Sahrens } mdb_spa_t;
1696fa9e4066Sahrens 
16972515f5d4SJustin T. Gibbs typedef struct mdb_dsl_pool {
16982515f5d4SJustin T. Gibbs 	uintptr_t dp_root_dir;
16992515f5d4SJustin T. Gibbs } mdb_dsl_pool_t;
17002515f5d4SJustin T. Gibbs 
1701fa9e4066Sahrens typedef struct mdb_dsl_dir {
17022515f5d4SJustin T. Gibbs 	uintptr_t dd_dbuf;
1703fa9e4066Sahrens 	int64_t dd_space_towrite[TXG_SIZE];
1704fa9e4066Sahrens } mdb_dsl_dir_t;
1705fa9e4066Sahrens 
1706fa9e4066Sahrens typedef struct mdb_dsl_dir_phys {
1707fa9e4066Sahrens 	uint64_t dd_used_bytes;
1708fa9e4066Sahrens 	uint64_t dd_compressed_bytes;
1709fa9e4066Sahrens 	uint64_t dd_uncompressed_bytes;
1710fa9e4066Sahrens } mdb_dsl_dir_phys_t;
1711fa9e4066Sahrens 
17125f5f7a6fSahrens typedef struct space_data {
17130713e232SGeorge Wilson 	uint64_t ms_alloctree[TXG_SIZE];
17140713e232SGeorge Wilson 	uint64_t ms_freetree[TXG_SIZE];
17150713e232SGeorge Wilson 	uint64_t ms_tree;
17165f5f7a6fSahrens 	uint64_t avail;
17175f5f7a6fSahrens 	uint64_t nowavail;
17185f5f7a6fSahrens } space_data_t;
17195f5f7a6fSahrens 
17205f5f7a6fSahrens /* ARGSUSED */
17215f5f7a6fSahrens static int
17225f5f7a6fSahrens space_cb(uintptr_t addr, const void *unknown, void *arg)
17235f5f7a6fSahrens {
17245f5f7a6fSahrens 	space_data_t *sd = arg;
17255f5f7a6fSahrens 	mdb_metaslab_t ms;
17260713e232SGeorge Wilson 	mdb_range_tree_t rt;
17272e4c9986SGeorge Wilson 	mdb_space_map_t sm = { 0 };
17280713e232SGeorge Wilson 	mdb_space_map_phys_t smp = { 0 };
17290713e232SGeorge Wilson 	int i;
17300713e232SGeorge Wilson 
17310713e232SGeorge Wilson 	if (mdb_ctf_vread(&ms, "metaslab_t", "mdb_metaslab_t",
17320713e232SGeorge Wilson 	    addr, 0) == -1)
17330713e232SGeorge Wilson 		return (WALK_ERR);
17340713e232SGeorge Wilson 
17350713e232SGeorge Wilson 	for (i = 0; i < TXG_SIZE; i++) {
17360713e232SGeorge Wilson 		if (mdb_ctf_vread(&rt, "range_tree_t",
17370713e232SGeorge Wilson 		    "mdb_range_tree_t", ms.ms_alloctree[i], 0) == -1)
17382e4c9986SGeorge Wilson 			return (WALK_ERR);
17392e4c9986SGeorge Wilson 
17400713e232SGeorge Wilson 		sd->ms_alloctree[i] += rt.rt_space;
17415f5f7a6fSahrens 
17420713e232SGeorge Wilson 		if (mdb_ctf_vread(&rt, "range_tree_t",
17430713e232SGeorge Wilson 		    "mdb_range_tree_t", ms.ms_freetree[i], 0) == -1)
17442e4c9986SGeorge Wilson 			return (WALK_ERR);
17452e4c9986SGeorge Wilson 
17460713e232SGeorge Wilson 		sd->ms_freetree[i] += rt.rt_space;
17470713e232SGeorge Wilson 	}
17480713e232SGeorge Wilson 
17490713e232SGeorge Wilson 	if (mdb_ctf_vread(&rt, "range_tree_t",
17502e4c9986SGeorge Wilson 	    "mdb_range_tree_t", ms.ms_tree, 0) == -1)
17512e4c9986SGeorge Wilson 		return (WALK_ERR);
17522e4c9986SGeorge Wilson 
17532e4c9986SGeorge Wilson 	if (ms.ms_sm != NULL &&
17540713e232SGeorge Wilson 	    mdb_ctf_vread(&sm, "space_map_t",
17550713e232SGeorge Wilson 	    "mdb_space_map_t", ms.ms_sm, 0) == -1)
17565f5f7a6fSahrens 		return (WALK_ERR);
17570713e232SGeorge Wilson 
17580713e232SGeorge Wilson 	if (sm.sm_phys != NULL) {
17590713e232SGeorge Wilson 		(void) mdb_ctf_vread(&smp, "space_map_phys_t",
17600713e232SGeorge Wilson 		    "mdb_space_map_phys_t", sm.sm_phys, 0);
17615f5f7a6fSahrens 	}
17625f5f7a6fSahrens 
17630713e232SGeorge Wilson 	sd->ms_tree += rt.rt_space;
17640713e232SGeorge Wilson 	sd->avail += sm.sm_size - sm.sm_alloc;
17650713e232SGeorge Wilson 	sd->nowavail += sm.sm_size - smp.smp_alloc;
17665f5f7a6fSahrens 
17675f5f7a6fSahrens 	return (WALK_NEXT);
17685f5f7a6fSahrens }
17695f5f7a6fSahrens 
1770fa9e4066Sahrens /*
1771fa9e4066Sahrens  * ::spa_space [-b]
1772fa9e4066Sahrens  *
1773fa9e4066Sahrens  * Given a spa_t, print out it's on-disk space usage and in-core
1774fa9e4066Sahrens  * estimates of future usage.  If -b is given, print space in bytes.
1775fa9e4066Sahrens  * Otherwise print in megabytes.
1776fa9e4066Sahrens  */
1777fa9e4066Sahrens /* ARGSUSED */
1778fa9e4066Sahrens static int
1779fa9e4066Sahrens spa_space(uintptr_t addr, uint_t flags, int argc, const mdb_arg_t *argv)
1780fa9e4066Sahrens {
1781fa9e4066Sahrens 	mdb_spa_t spa;
17822515f5d4SJustin T. Gibbs 	mdb_dsl_pool_t dp;
1783fa9e4066Sahrens 	mdb_dsl_dir_t dd;
17842515f5d4SJustin T. Gibbs 	mdb_dmu_buf_impl_t db;
1785fa9e4066Sahrens 	mdb_dsl_dir_phys_t dsp;
17865f5f7a6fSahrens 	space_data_t sd;
1787fa9e4066Sahrens 	int shift = 20;
1788fa9e4066Sahrens 	char *suffix = "M";
178928e4da25SMatthew Ahrens 	int bytes = B_FALSE;
1790fa9e4066Sahrens 
179128e4da25SMatthew Ahrens 	if (mdb_getopts(argc, argv, 'b', MDB_OPT_SETBITS, TRUE, &bytes, NULL) !=
1792fa9e4066Sahrens 	    argc)
1793fa9e4066Sahrens 		return (DCMD_USAGE);
1794fa9e4066Sahrens 	if (!(flags & DCMD_ADDRSPEC))
1795fa9e4066Sahrens 		return (DCMD_USAGE);
1796fa9e4066Sahrens 
179728e4da25SMatthew Ahrens 	if (bytes) {
1798fa9e4066Sahrens 		shift = 0;
1799fa9e4066Sahrens 		suffix = "";
1800fa9e4066Sahrens 	}
1801fa9e4066Sahrens 
18022515f5d4SJustin T. Gibbs 	if (mdb_ctf_vread(&spa, ZFS_STRUCT "spa", "mdb_spa_t",
18032515f5d4SJustin T. Gibbs 	    addr, 0) == -1 ||
18042515f5d4SJustin T. Gibbs 	    mdb_ctf_vread(&dp, ZFS_STRUCT "dsl_pool", "mdb_dsl_pool_t",
18052515f5d4SJustin T. Gibbs 	    spa.spa_dsl_pool, 0) == -1 ||
18062515f5d4SJustin T. Gibbs 	    mdb_ctf_vread(&dd, ZFS_STRUCT "dsl_dir", "mdb_dsl_dir_t",
18072515f5d4SJustin T. Gibbs 	    dp.dp_root_dir, 0) == -1 ||
18082515f5d4SJustin T. Gibbs 	    mdb_ctf_vread(&db, ZFS_STRUCT "dmu_buf_impl", "mdb_dmu_buf_impl_t",
18092515f5d4SJustin T. Gibbs 	    dd.dd_dbuf, 0) == -1 ||
18102515f5d4SJustin T. Gibbs 	    mdb_ctf_vread(&dsp, ZFS_STRUCT "dsl_dir_phys",
18112515f5d4SJustin T. Gibbs 	    "mdb_dsl_dir_phys_t", db.db.db_data, 0) == -1) {
1812fa9e4066Sahrens 		return (DCMD_ERR);
1813fa9e4066Sahrens 	}
1814fa9e4066Sahrens 
1815fa9e4066Sahrens 	mdb_printf("dd_space_towrite = %llu%s %llu%s %llu%s %llu%s\n",
1816fa9e4066Sahrens 	    dd.dd_space_towrite[0] >> shift, suffix,
1817fa9e4066Sahrens 	    dd.dd_space_towrite[1] >> shift, suffix,
1818fa9e4066Sahrens 	    dd.dd_space_towrite[2] >> shift, suffix,
1819fa9e4066Sahrens 	    dd.dd_space_towrite[3] >> shift, suffix);
1820fa9e4066Sahrens 
1821fa9e4066Sahrens 	mdb_printf("dd_phys.dd_used_bytes = %llu%s\n",
1822fa9e4066Sahrens 	    dsp.dd_used_bytes >> shift, suffix);
18235f5f7a6fSahrens 	mdb_printf("dd_phys.dd_compressed_bytes = %llu%s\n",
18245f5f7a6fSahrens 	    dsp.dd_compressed_bytes >> shift, suffix);
18255f5f7a6fSahrens 	mdb_printf("dd_phys.dd_uncompressed_bytes = %llu%s\n",
18265f5f7a6fSahrens 	    dsp.dd_uncompressed_bytes >> shift, suffix);
18275f5f7a6fSahrens 
18285f5f7a6fSahrens 	bzero(&sd, sizeof (sd));
18295f5f7a6fSahrens 	if (mdb_pwalk("metaslab", space_cb, &sd, addr) != 0) {
18305f5f7a6fSahrens 		mdb_warn("can't walk metaslabs");
18315f5f7a6fSahrens 		return (DCMD_ERR);
1832fa9e4066Sahrens 	}
1833fa9e4066Sahrens 
1834fa9e4066Sahrens 	mdb_printf("ms_allocmap = %llu%s %llu%s %llu%s %llu%s\n",
18350713e232SGeorge Wilson 	    sd.ms_alloctree[0] >> shift, suffix,
18360713e232SGeorge Wilson 	    sd.ms_alloctree[1] >> shift, suffix,
18370713e232SGeorge Wilson 	    sd.ms_alloctree[2] >> shift, suffix,
18380713e232SGeorge Wilson 	    sd.ms_alloctree[3] >> shift, suffix);
1839fa9e4066Sahrens 	mdb_printf("ms_freemap = %llu%s %llu%s %llu%s %llu%s\n",
18400713e232SGeorge Wilson 	    sd.ms_freetree[0] >> shift, suffix,
18410713e232SGeorge Wilson 	    sd.ms_freetree[1] >> shift, suffix,
18420713e232SGeorge Wilson 	    sd.ms_freetree[2] >> shift, suffix,
18430713e232SGeorge Wilson 	    sd.ms_freetree[3] >> shift, suffix);
18440713e232SGeorge Wilson 	mdb_printf("ms_tree = %llu%s\n", sd.ms_tree >> shift, suffix);
18455f5f7a6fSahrens 	mdb_printf("last synced avail = %llu%s\n", sd.avail >> shift, suffix);
18465f5f7a6fSahrens 	mdb_printf("current syncing avail = %llu%s\n",
18475f5f7a6fSahrens 	    sd.nowavail >> shift, suffix);
1848fa9e4066Sahrens 
1849fa9e4066Sahrens 	return (DCMD_OK);
1850fa9e4066Sahrens }
1851fa9e4066Sahrens 
185222ce0148SMatthew Ahrens typedef struct mdb_spa_aux_vdev {
185322ce0148SMatthew Ahrens 	int sav_count;
185422ce0148SMatthew Ahrens 	uintptr_t sav_vdevs;
185522ce0148SMatthew Ahrens } mdb_spa_aux_vdev_t;
1856fa9e4066Sahrens 
185722ce0148SMatthew Ahrens typedef struct mdb_spa_vdevs {
185822ce0148SMatthew Ahrens 	uintptr_t spa_root_vdev;
185922ce0148SMatthew Ahrens 	mdb_spa_aux_vdev_t spa_l2cache;
186022ce0148SMatthew Ahrens 	mdb_spa_aux_vdev_t spa_spares;
186122ce0148SMatthew Ahrens } mdb_spa_vdevs_t;
1862fa9e4066Sahrens 
1863ec9f632eSEric Schrock static int
186422ce0148SMatthew Ahrens spa_print_aux(mdb_spa_aux_vdev_t *sav, uint_t flags, mdb_arg_t *v,
1865ec9f632eSEric Schrock     const char *name)
1866ec9f632eSEric Schrock {
1867ec9f632eSEric Schrock 	uintptr_t *aux;
1868ec9f632eSEric Schrock 	size_t len;
1869ec9f632eSEric Schrock 	int ret, i;
1870ec9f632eSEric Schrock 
1871ec9f632eSEric Schrock 	/*
1872ec9f632eSEric Schrock 	 * Iterate over aux vdevs and print those out as well.  This is a
1873ec9f632eSEric Schrock 	 * little annoying because we don't have a root vdev to pass to ::vdev.
1874ec9f632eSEric Schrock 	 * Instead, we print a single line and then call it for each child
1875ec9f632eSEric Schrock 	 * vdev.
1876ec9f632eSEric Schrock 	 */
1877ec9f632eSEric Schrock 	if (sav->sav_count != 0) {
1878ec9f632eSEric Schrock 		v[1].a_type = MDB_TYPE_STRING;
1879ec9f632eSEric Schrock 		v[1].a_un.a_str = "-d";
1880ec9f632eSEric Schrock 		v[2].a_type = MDB_TYPE_IMMEDIATE;
1881ec9f632eSEric Schrock 		v[2].a_un.a_val = 2;
1882ec9f632eSEric Schrock 
1883ec9f632eSEric Schrock 		len = sav->sav_count * sizeof (uintptr_t);
1884ec9f632eSEric Schrock 		aux = mdb_alloc(len, UM_SLEEP);
188522ce0148SMatthew Ahrens 		if (mdb_vread(aux, len, sav->sav_vdevs) == -1) {
1886ec9f632eSEric Schrock 			mdb_free(aux, len);
1887ec9f632eSEric Schrock 			mdb_warn("failed to read l2cache vdevs at %p",
1888ec9f632eSEric Schrock 			    sav->sav_vdevs);
1889ec9f632eSEric Schrock 			return (DCMD_ERR);
1890ec9f632eSEric Schrock 		}
1891ec9f632eSEric Schrock 
1892ec9f632eSEric Schrock 		mdb_printf("%-?s %-9s %-12s %s\n", "-", "-", "-", name);
1893ec9f632eSEric Schrock 
1894ec9f632eSEric Schrock 		for (i = 0; i < sav->sav_count; i++) {
1895ec9f632eSEric Schrock 			ret = mdb_call_dcmd("vdev", aux[i], flags, 3, v);
1896ec9f632eSEric Schrock 			if (ret != DCMD_OK) {
1897ec9f632eSEric Schrock 				mdb_free(aux, len);
1898ec9f632eSEric Schrock 				return (ret);
1899ec9f632eSEric Schrock 			}
1900ec9f632eSEric Schrock 		}
1901ec9f632eSEric Schrock 
1902ec9f632eSEric Schrock 		mdb_free(aux, len);
1903ec9f632eSEric Schrock 	}
1904ec9f632eSEric Schrock 
1905ec9f632eSEric Schrock 	return (0);
1906ec9f632eSEric Schrock }
1907ec9f632eSEric Schrock 
1908fa9e4066Sahrens /*
1909fa9e4066Sahrens  * ::spa_vdevs
1910fa9e4066Sahrens  *
19112e4c9986SGeorge Wilson  *	-e	Include error stats
19122e4c9986SGeorge Wilson  *	-m	Include metaslab information
19132e4c9986SGeorge Wilson  *	-M	Include metaslab group information
19142e4c9986SGeorge Wilson  *	-h	Include histogram information (requires -m or -M)
1915fa9e4066Sahrens  *
1916fa9e4066Sahrens  * Print out a summarized list of vdevs for the given spa_t.
1917c5904d13Seschrock  * This is accomplished by invoking "::vdev -re" on the root vdev, as well as
1918c5904d13Seschrock  * iterating over the cache devices.
1919fa9e4066Sahrens  */
1920fa9e4066Sahrens /* ARGSUSED */
1921fa9e4066Sahrens static int
1922fa9e4066Sahrens spa_vdevs(uintptr_t addr, uint_t flags, int argc, const mdb_arg_t *argv)
1923fa9e4066Sahrens {
1924c5904d13Seschrock 	mdb_arg_t v[3];
1925ec9f632eSEric Schrock 	int ret;
19262e4c9986SGeorge Wilson 	char opts[100] = "-r";
19272e4c9986SGeorge Wilson 	int spa_flags = 0;
1928fa9e4066Sahrens 
1929fa9e4066Sahrens 	if (mdb_getopts(argc, argv,
19302e4c9986SGeorge Wilson 	    'e', MDB_OPT_SETBITS, SPA_FLAG_ERRORS, &spa_flags,
19312e4c9986SGeorge Wilson 	    'm', MDB_OPT_SETBITS, SPA_FLAG_METASLABS, &spa_flags,
19322e4c9986SGeorge Wilson 	    'M', MDB_OPT_SETBITS, SPA_FLAG_METASLAB_GROUPS, &spa_flags,
19332e4c9986SGeorge Wilson 	    'h', MDB_OPT_SETBITS, SPA_FLAG_HISTOGRAMS, &spa_flags,
1934fa9e4066Sahrens 	    NULL) != argc)
1935fa9e4066Sahrens 		return (DCMD_USAGE);
1936fa9e4066Sahrens 
1937fa9e4066Sahrens 	if (!(flags & DCMD_ADDRSPEC))
1938fa9e4066Sahrens 		return (DCMD_USAGE);
1939fa9e4066Sahrens 
194022ce0148SMatthew Ahrens 	mdb_spa_vdevs_t spa;
194122ce0148SMatthew Ahrens 	if (mdb_ctf_vread(&spa, "spa_t", "mdb_spa_vdevs_t", addr, 0) == -1)
1942fa9e4066Sahrens 		return (DCMD_ERR);
1943fa9e4066Sahrens 
1944088e9d47Seschrock 	/*
1945088e9d47Seschrock 	 * Unitialized spa_t structures can have a NULL root vdev.
1946088e9d47Seschrock 	 */
1947088e9d47Seschrock 	if (spa.spa_root_vdev == NULL) {
1948088e9d47Seschrock 		mdb_printf("no associated vdevs\n");
1949088e9d47Seschrock 		return (DCMD_OK);
1950088e9d47Seschrock 	}
1951088e9d47Seschrock 
19522e4c9986SGeorge Wilson 	if (spa_flags & SPA_FLAG_ERRORS)
19532e4c9986SGeorge Wilson 		strcat(opts, "e");
19542e4c9986SGeorge Wilson 	if (spa_flags & SPA_FLAG_METASLABS)
19552e4c9986SGeorge Wilson 		strcat(opts, "m");
19562e4c9986SGeorge Wilson 	if (spa_flags & SPA_FLAG_METASLAB_GROUPS)
19572e4c9986SGeorge Wilson 		strcat(opts, "M");
19582e4c9986SGeorge Wilson 	if (spa_flags & SPA_FLAG_HISTOGRAMS)
19592e4c9986SGeorge Wilson 		strcat(opts, "h");
19602e4c9986SGeorge Wilson 
1961c5904d13Seschrock 	v[0].a_type = MDB_TYPE_STRING;
19622e4c9986SGeorge Wilson 	v[0].a_un.a_str = opts;
1963fa9e4066Sahrens 
1964c5904d13Seschrock 	ret = mdb_call_dcmd("vdev", (uintptr_t)spa.spa_root_vdev,
1965c5904d13Seschrock 	    flags, 1, v);
1966c5904d13Seschrock 	if (ret != DCMD_OK)
1967c5904d13Seschrock 		return (ret);
1968c5904d13Seschrock 
1969ec9f632eSEric Schrock 	if (spa_print_aux(&spa.spa_l2cache, flags, v, "cache") != 0 ||
1970ec9f632eSEric Schrock 	    spa_print_aux(&spa.spa_spares, flags, v, "spares") != 0)
1971ec9f632eSEric Schrock 		return (DCMD_ERR);
1972c5904d13Seschrock 
1973c5904d13Seschrock 	return (DCMD_OK);
1974fa9e4066Sahrens }
1975fa9e4066Sahrens 
1976ccae0b50Seschrock /*
1977ccae0b50Seschrock  * ::zio
1978ccae0b50Seschrock  *
1979ccae0b50Seschrock  * Print a summary of zio_t and all its children.  This is intended to display a
1980ccae0b50Seschrock  * zio tree, and hence we only pick the most important pieces of information for
1981ccae0b50Seschrock  * the main summary.  More detailed information can always be found by doing a
1982ccae0b50Seschrock  * '::print zio' on the underlying zio_t.  The columns we display are:
1983ccae0b50Seschrock  *
1984c55e05cbSMatthew Ahrens  *	ADDRESS  TYPE  STAGE  WAITER  TIME_ELAPSED
1985ccae0b50Seschrock  *
1986ccae0b50Seschrock  * The 'address' column is indented by one space for each depth level as we
1987ccae0b50Seschrock  * descend down the tree.
1988ccae0b50Seschrock  */
1989fac3008cSeschrock 
1990c55e05cbSMatthew Ahrens #define	ZIO_MAXINDENT	7
1991a3f829aeSBill Moore #define	ZIO_MAXWIDTH	(sizeof (uintptr_t) * 2 + ZIO_MAXINDENT)
1992a3f829aeSBill Moore #define	ZIO_WALK_SELF	0
1993a3f829aeSBill Moore #define	ZIO_WALK_CHILD	1
1994a3f829aeSBill Moore #define	ZIO_WALK_PARENT	2
1995a3f829aeSBill Moore 
1996a3f829aeSBill Moore typedef struct zio_print_args {
1997a3f829aeSBill Moore 	int	zpa_current_depth;
1998a3f829aeSBill Moore 	int	zpa_min_depth;
1999a3f829aeSBill Moore 	int	zpa_max_depth;
2000a3f829aeSBill Moore 	int	zpa_type;
2001a3f829aeSBill Moore 	uint_t	zpa_flags;
2002a3f829aeSBill Moore } zio_print_args_t;
2003a3f829aeSBill Moore 
200428e4da25SMatthew Ahrens typedef struct mdb_zio {
200528e4da25SMatthew Ahrens 	enum zio_type io_type;
200628e4da25SMatthew Ahrens 	enum zio_stage io_stage;
2007d5ee8a13SMatthew Ahrens 	uintptr_t io_waiter;
2008d5ee8a13SMatthew Ahrens 	uintptr_t io_spa;
200928e4da25SMatthew Ahrens 	struct {
201028e4da25SMatthew Ahrens 		struct {
2011d5ee8a13SMatthew Ahrens 			uintptr_t list_next;
201228e4da25SMatthew Ahrens 		} list_head;
201328e4da25SMatthew Ahrens 	} io_parent_list;
201428e4da25SMatthew Ahrens 	int io_error;
201528e4da25SMatthew Ahrens } mdb_zio_t;
201628e4da25SMatthew Ahrens 
2017c55e05cbSMatthew Ahrens typedef struct mdb_zio_timestamp {
2018c55e05cbSMatthew Ahrens 	hrtime_t io_timestamp;
2019c55e05cbSMatthew Ahrens } mdb_zio_timestamp_t;
2020c55e05cbSMatthew Ahrens 
2021a3f829aeSBill Moore static int zio_child_cb(uintptr_t addr, const void *unknown, void *arg);
2022fac3008cSeschrock 
2023ccae0b50Seschrock static int
2024c55e05cbSMatthew Ahrens zio_print_cb(uintptr_t addr, zio_print_args_t *zpa)
2025ccae0b50Seschrock {
2026ccae0b50Seschrock 	mdb_ctf_id_t type_enum, stage_enum;
2027a3f829aeSBill Moore 	int indent = zpa->zpa_current_depth;
2028ccae0b50Seschrock 	const char *type, *stage;
2029a3f829aeSBill Moore 	uintptr_t laddr;
2030c55e05cbSMatthew Ahrens 	mdb_zio_t zio;
2031c55e05cbSMatthew Ahrens 	mdb_zio_timestamp_t zio_timestamp = { 0 };
2032c55e05cbSMatthew Ahrens 
2033c55e05cbSMatthew Ahrens 	if (mdb_ctf_vread(&zio, ZFS_STRUCT "zio", "mdb_zio_t", addr, 0) == -1)
2034c55e05cbSMatthew Ahrens 		return (WALK_ERR);
2035c55e05cbSMatthew Ahrens 	(void) mdb_ctf_vread(&zio_timestamp, ZFS_STRUCT "zio",
2036c55e05cbSMatthew Ahrens 	    "mdb_zio_timestamp_t", addr, MDB_CTF_VREAD_QUIET);
2037ccae0b50Seschrock 
2038a3f829aeSBill Moore 	if (indent > ZIO_MAXINDENT)
2039a3f829aeSBill Moore 		indent = ZIO_MAXINDENT;
2040ccae0b50Seschrock 
2041ccae0b50Seschrock 	if (mdb_ctf_lookup_by_name("enum zio_type", &type_enum) == -1 ||
2042ccae0b50Seschrock 	    mdb_ctf_lookup_by_name("enum zio_stage", &stage_enum) == -1) {
2043ccae0b50Seschrock 		mdb_warn("failed to lookup zio enums");
2044ccae0b50Seschrock 		return (WALK_ERR);
2045ccae0b50Seschrock 	}
2046ccae0b50Seschrock 
2047c55e05cbSMatthew Ahrens 	if ((type = mdb_ctf_enum_name(type_enum, zio.io_type)) != NULL)
2048ccae0b50Seschrock 		type += sizeof ("ZIO_TYPE_") - 1;
2049ccae0b50Seschrock 	else
2050ccae0b50Seschrock 		type = "?";
2051ccae0b50Seschrock 
2052c55e05cbSMatthew Ahrens 	if (zio.io_error == 0) {
2053c55e05cbSMatthew Ahrens 		stage = mdb_ctf_enum_name(stage_enum, zio.io_stage);
205428e4da25SMatthew Ahrens 		if (stage != NULL)
205528e4da25SMatthew Ahrens 			stage += sizeof ("ZIO_STAGE_") - 1;
205628e4da25SMatthew Ahrens 		else
205728e4da25SMatthew Ahrens 			stage = "?";
205828e4da25SMatthew Ahrens 	} else {
205928e4da25SMatthew Ahrens 		stage = "FAILED";
206028e4da25SMatthew Ahrens 	}
2061ccae0b50Seschrock 
2062a3f829aeSBill Moore 	if (zpa->zpa_current_depth >= zpa->zpa_min_depth) {
2063a3f829aeSBill Moore 		if (zpa->zpa_flags & DCMD_PIPE_OUT) {
2064a3f829aeSBill Moore 			mdb_printf("%?p\n", addr);
2065a3f829aeSBill Moore 		} else {
2066a3f829aeSBill Moore 			mdb_printf("%*s%-*p %-5s %-16s ", indent, "",
2067a3f829aeSBill Moore 			    ZIO_MAXWIDTH - indent, addr, type, stage);
2068d5ee8a13SMatthew Ahrens 			if (zio.io_waiter != 0)
2069d5ee8a13SMatthew Ahrens 				mdb_printf("%-16lx ", zio.io_waiter);
2070a3f829aeSBill Moore 			else
2071c55e05cbSMatthew Ahrens 				mdb_printf("%-16s ", "-");
2072c55e05cbSMatthew Ahrens #ifdef _KERNEL
2073c55e05cbSMatthew Ahrens 			if (zio_timestamp.io_timestamp != 0) {
2074c55e05cbSMatthew Ahrens 				mdb_printf("%llums", (mdb_gethrtime() -
2075c55e05cbSMatthew Ahrens 				    zio_timestamp.io_timestamp) /
2076c55e05cbSMatthew Ahrens 				    1000000);
2077c55e05cbSMatthew Ahrens 			} else {
2078c55e05cbSMatthew Ahrens 				mdb_printf("%-12s ", "-");
2079c55e05cbSMatthew Ahrens 			}
2080c55e05cbSMatthew Ahrens #else
2081c55e05cbSMatthew Ahrens 			mdb_printf("%-12s ", "-");
2082c55e05cbSMatthew Ahrens #endif
2083c55e05cbSMatthew Ahrens 			mdb_printf("\n");
2084a3f829aeSBill Moore 		}
2085a3f829aeSBill Moore 	}
2086a3f829aeSBill Moore 
2087a3f829aeSBill Moore 	if (zpa->zpa_current_depth >= zpa->zpa_max_depth)
2088a3f829aeSBill Moore 		return (WALK_NEXT);
2089ccae0b50Seschrock 
2090a3f829aeSBill Moore 	if (zpa->zpa_type == ZIO_WALK_PARENT)
209128e4da25SMatthew Ahrens 		laddr = addr + mdb_ctf_offsetof_by_name(ZFS_STRUCT "zio",
209228e4da25SMatthew Ahrens 		    "io_parent_list");
2093ccae0b50Seschrock 	else
209428e4da25SMatthew Ahrens 		laddr = addr + mdb_ctf_offsetof_by_name(ZFS_STRUCT "zio",
209528e4da25SMatthew Ahrens 		    "io_child_list");
2096ccae0b50Seschrock 
2097a3f829aeSBill Moore 	zpa->zpa_current_depth++;
2098a3f829aeSBill Moore 	if (mdb_pwalk("list", zio_child_cb, zpa, laddr) != 0) {
2099a3f829aeSBill Moore 		mdb_warn("failed to walk zio_t children at %p\n", laddr);
2100ccae0b50Seschrock 		return (WALK_ERR);
2101ccae0b50Seschrock 	}
2102a3f829aeSBill Moore 	zpa->zpa_current_depth--;
2103ccae0b50Seschrock 
2104ccae0b50Seschrock 	return (WALK_NEXT);
2105ccae0b50Seschrock }
2106ccae0b50Seschrock 
2107a3f829aeSBill Moore /* ARGSUSED */
2108ccae0b50Seschrock static int
2109a3f829aeSBill Moore zio_child_cb(uintptr_t addr, const void *unknown, void *arg)
2110ccae0b50Seschrock {
2111a3f829aeSBill Moore 	zio_link_t zl;
2112a3f829aeSBill Moore 	uintptr_t ziop;
2113a3f829aeSBill Moore 	zio_print_args_t *zpa = arg;
2114a3f829aeSBill Moore 
2115a3f829aeSBill Moore 	if (mdb_vread(&zl, sizeof (zl), addr) == -1) {
2116a3f829aeSBill Moore 		mdb_warn("failed to read zio_link_t at %p", addr);
2117a3f829aeSBill Moore 		return (WALK_ERR);
2118a3f829aeSBill Moore 	}
2119a3f829aeSBill Moore 
2120a3f829aeSBill Moore 	if (zpa->zpa_type == ZIO_WALK_PARENT)
2121a3f829aeSBill Moore 		ziop = (uintptr_t)zl.zl_parent;
2122a3f829aeSBill Moore 	else
2123a3f829aeSBill Moore 		ziop = (uintptr_t)zl.zl_child;
2124a3f829aeSBill Moore 
212569962b56SMatthew Ahrens 	return (zio_print_cb(ziop, zpa));
2126a3f829aeSBill Moore }
2127a3f829aeSBill Moore 
2128a3f829aeSBill Moore /* ARGSUSED */
2129a3f829aeSBill Moore static int
2130a3f829aeSBill Moore zio_print(uintptr_t addr, uint_t flags, int argc, const mdb_arg_t *argv)
2131a3f829aeSBill Moore {
2132a3f829aeSBill Moore 	zio_print_args_t zpa = { 0 };
2133ccae0b50Seschrock 
2134ccae0b50Seschrock 	if (!(flags & DCMD_ADDRSPEC))
2135ccae0b50Seschrock 		return (DCMD_USAGE);
2136ccae0b50Seschrock 
2137a3f829aeSBill Moore 	if (mdb_getopts(argc, argv,
2138a3f829aeSBill Moore 	    'r', MDB_OPT_SETBITS, INT_MAX, &zpa.zpa_max_depth,
2139a3f829aeSBill Moore 	    'c', MDB_OPT_SETBITS, ZIO_WALK_CHILD, &zpa.zpa_type,
2140a3f829aeSBill Moore 	    'p', MDB_OPT_SETBITS, ZIO_WALK_PARENT, &zpa.zpa_type,
2141a3f829aeSBill Moore 	    NULL) != argc)
2142a3f829aeSBill Moore 		return (DCMD_USAGE);
2143a3f829aeSBill Moore 
2144a3f829aeSBill Moore 	zpa.zpa_flags = flags;
2145a3f829aeSBill Moore 	if (zpa.zpa_max_depth != 0) {
2146a3f829aeSBill Moore 		if (zpa.zpa_type == ZIO_WALK_SELF)
2147a3f829aeSBill Moore 			zpa.zpa_type = ZIO_WALK_CHILD;
2148a3f829aeSBill Moore 	} else if (zpa.zpa_type != ZIO_WALK_SELF) {
2149a3f829aeSBill Moore 		zpa.zpa_min_depth = 1;
2150a3f829aeSBill Moore 		zpa.zpa_max_depth = 1;
2151a3f829aeSBill Moore 	}
2152a3f829aeSBill Moore 
2153c55e05cbSMatthew Ahrens 	if (!(flags & DCMD_PIPE_OUT) && DCMD_HDRSPEC(flags)) {
2154c55e05cbSMatthew Ahrens 		mdb_printf("%<u>%-*s %-5s %-16s %-16s %-12s%</u>\n",
2155c55e05cbSMatthew Ahrens 		    ZIO_MAXWIDTH, "ADDRESS", "TYPE", "STAGE", "WAITER",
2156c55e05cbSMatthew Ahrens 		    "TIME_ELAPSED");
2157c55e05cbSMatthew Ahrens 	}
2158fac3008cSeschrock 
2159c55e05cbSMatthew Ahrens 	if (zio_print_cb(addr, &zpa) != WALK_NEXT)
2160ccae0b50Seschrock 		return (DCMD_ERR);
2161ccae0b50Seschrock 
2162ccae0b50Seschrock 	return (DCMD_OK);
2163ccae0b50Seschrock }
2164ccae0b50Seschrock 
2165ccae0b50Seschrock /*
2166ccae0b50Seschrock  * [addr]::zio_state
2167ccae0b50Seschrock  *
2168ccae0b50Seschrock  * Print a summary of all zio_t structures on the system, or for a particular
2169ccae0b50Seschrock  * pool.  This is equivalent to '::walk zio_root | ::zio'.
2170ccae0b50Seschrock  */
2171ccae0b50Seschrock /*ARGSUSED*/
2172ccae0b50Seschrock static int
2173ccae0b50Seschrock zio_state(uintptr_t addr, uint_t flags, int argc, const mdb_arg_t *argv)
2174ccae0b50Seschrock {
2175ccae0b50Seschrock 	/*
2176ccae0b50Seschrock 	 * MDB will remember the last address of the pipeline, so if we don't
2177ccae0b50Seschrock 	 * zero this we'll end up trying to walk zio structures for a
2178ccae0b50Seschrock 	 * non-existent spa_t.
2179ccae0b50Seschrock 	 */
2180ccae0b50Seschrock 	if (!(flags & DCMD_ADDRSPEC))
2181ccae0b50Seschrock 		addr = 0;
2182ccae0b50Seschrock 
2183ccae0b50Seschrock 	return (mdb_pwalk_dcmd("zio_root", "zio", argc, argv, addr));
2184ccae0b50Seschrock }
2185ccae0b50Seschrock 
2186fa9e4066Sahrens typedef struct txg_list_walk_data {
2187fa9e4066Sahrens 	uintptr_t lw_head[TXG_SIZE];
2188fa9e4066Sahrens 	int	lw_txgoff;
2189fa9e4066Sahrens 	int	lw_maxoff;
2190fa9e4066Sahrens 	size_t	lw_offset;
2191fa9e4066Sahrens 	void	*lw_obj;
2192fa9e4066Sahrens } txg_list_walk_data_t;
2193fa9e4066Sahrens 
2194fa9e4066Sahrens static int
2195fa9e4066Sahrens txg_list_walk_init_common(mdb_walk_state_t *wsp, int txg, int maxoff)
2196fa9e4066Sahrens {
2197fa9e4066Sahrens 	txg_list_walk_data_t *lwd;
2198fa9e4066Sahrens 	txg_list_t list;
2199fa9e4066Sahrens 	int i;
2200fa9e4066Sahrens 
2201fa9e4066Sahrens 	lwd = mdb_alloc(sizeof (txg_list_walk_data_t), UM_SLEEP | UM_GC);
2202fa9e4066Sahrens 	if (mdb_vread(&list, sizeof (txg_list_t), wsp->walk_addr) == -1) {
2203fa9e4066Sahrens 		mdb_warn("failed to read txg_list_t at %#lx", wsp->walk_addr);
2204fa9e4066Sahrens 		return (WALK_ERR);
2205fa9e4066Sahrens 	}
2206fa9e4066Sahrens 
2207fa9e4066Sahrens 	for (i = 0; i < TXG_SIZE; i++)
2208fa9e4066Sahrens 		lwd->lw_head[i] = (uintptr_t)list.tl_head[i];
2209fa9e4066Sahrens 	lwd->lw_offset = list.tl_offset;
2210fa9e4066Sahrens 	lwd->lw_obj = mdb_alloc(lwd->lw_offset + sizeof (txg_node_t),
2211fa9e4066Sahrens 	    UM_SLEEP | UM_GC);
2212fa9e4066Sahrens 	lwd->lw_txgoff = txg;
2213fa9e4066Sahrens 	lwd->lw_maxoff = maxoff;
2214fa9e4066Sahrens 
2215fa9e4066Sahrens 	wsp->walk_addr = lwd->lw_head[lwd->lw_txgoff];
2216fa9e4066Sahrens 	wsp->walk_data = lwd;
2217fa9e4066Sahrens 
2218fa9e4066Sahrens 	return (WALK_NEXT);
2219fa9e4066Sahrens }
2220fa9e4066Sahrens 
2221fa9e4066Sahrens static int
2222fa9e4066Sahrens txg_list_walk_init(mdb_walk_state_t *wsp)
2223fa9e4066Sahrens {
2224fa9e4066Sahrens 	return (txg_list_walk_init_common(wsp, 0, TXG_SIZE-1));
2225fa9e4066Sahrens }
2226fa9e4066Sahrens 
2227fa9e4066Sahrens static int
2228fa9e4066Sahrens txg_list0_walk_init(mdb_walk_state_t *wsp)
2229fa9e4066Sahrens {
2230fa9e4066Sahrens 	return (txg_list_walk_init_common(wsp, 0, 0));
2231fa9e4066Sahrens }
2232fa9e4066Sahrens 
2233fa9e4066Sahrens static int
2234fa9e4066Sahrens txg_list1_walk_init(mdb_walk_state_t *wsp)
2235fa9e4066Sahrens {
2236fa9e4066Sahrens 	return (txg_list_walk_init_common(wsp, 1, 1));
2237fa9e4066Sahrens }
2238fa9e4066Sahrens 
2239fa9e4066Sahrens static int
2240fa9e4066Sahrens txg_list2_walk_init(mdb_walk_state_t *wsp)
2241fa9e4066Sahrens {
2242fa9e4066Sahrens 	return (txg_list_walk_init_common(wsp, 2, 2));
2243fa9e4066Sahrens }
2244fa9e4066Sahrens 
2245fa9e4066Sahrens static int
2246fa9e4066Sahrens txg_list3_walk_init(mdb_walk_state_t *wsp)
2247fa9e4066Sahrens {
2248fa9e4066Sahrens 	return (txg_list_walk_init_common(wsp, 3, 3));
2249fa9e4066Sahrens }
2250fa9e4066Sahrens 
2251fa9e4066Sahrens static int
2252fa9e4066Sahrens txg_list_walk_step(mdb_walk_state_t *wsp)
2253fa9e4066Sahrens {
2254fa9e4066Sahrens 	txg_list_walk_data_t *lwd = wsp->walk_data;
2255fa9e4066Sahrens 	uintptr_t addr;
2256fa9e4066Sahrens 	txg_node_t *node;
2257fa9e4066Sahrens 	int status;
2258fa9e4066Sahrens 
2259fa9e4066Sahrens 	while (wsp->walk_addr == NULL && lwd->lw_txgoff < lwd->lw_maxoff) {
2260fa9e4066Sahrens 		lwd->lw_txgoff++;
2261fa9e4066Sahrens 		wsp->walk_addr = lwd->lw_head[lwd->lw_txgoff];
2262fa9e4066Sahrens 	}
2263fa9e4066Sahrens 
2264fa9e4066Sahrens 	if (wsp->walk_addr == NULL)
2265fa9e4066Sahrens 		return (WALK_DONE);
2266fa9e4066Sahrens 
2267fa9e4066Sahrens 	addr = wsp->walk_addr - lwd->lw_offset;
2268fa9e4066Sahrens 
2269fa9e4066Sahrens 	if (mdb_vread(lwd->lw_obj,
2270fa9e4066Sahrens 	    lwd->lw_offset + sizeof (txg_node_t), addr) == -1) {
2271fa9e4066Sahrens 		mdb_warn("failed to read list element at %#lx", addr);
2272fa9e4066Sahrens 		return (WALK_ERR);
2273fa9e4066Sahrens 	}
2274fa9e4066Sahrens 
2275fa9e4066Sahrens 	status = wsp->walk_callback(addr, lwd->lw_obj, wsp->walk_cbdata);
2276fa9e4066Sahrens 	node = (txg_node_t *)((uintptr_t)lwd->lw_obj + lwd->lw_offset);
2277fa9e4066Sahrens 	wsp->walk_addr = (uintptr_t)node->tn_next[lwd->lw_txgoff];
2278fa9e4066Sahrens 
2279fa9e4066Sahrens 	return (status);
2280fa9e4066Sahrens }
2281fa9e4066Sahrens 
2282fa9e4066Sahrens /*
2283fa9e4066Sahrens  * ::walk spa
2284fa9e4066Sahrens  *
2285fa9e4066Sahrens  * Walk all named spa_t structures in the namespace.  This is nothing more than
2286fa9e4066Sahrens  * a layered avl walk.
2287fa9e4066Sahrens  */
2288fa9e4066Sahrens static int
2289fa9e4066Sahrens spa_walk_init(mdb_walk_state_t *wsp)
2290fa9e4066Sahrens {
2291fa9e4066Sahrens 	GElf_Sym sym;
2292fa9e4066Sahrens 
2293fa9e4066Sahrens 	if (wsp->walk_addr != NULL) {
2294fa9e4066Sahrens 		mdb_warn("spa walk only supports global walks\n");
2295fa9e4066Sahrens 		return (WALK_ERR);
2296fa9e4066Sahrens 	}
2297fa9e4066Sahrens 
2298fa9e4066Sahrens 	if (mdb_lookup_by_obj(ZFS_OBJ_NAME, "spa_namespace_avl", &sym) == -1) {
2299fa9e4066Sahrens 		mdb_warn("failed to find symbol 'spa_namespace_avl'");
2300fa9e4066Sahrens 		return (WALK_ERR);
2301fa9e4066Sahrens 	}
2302fa9e4066Sahrens 
2303fa9e4066Sahrens 	wsp->walk_addr = (uintptr_t)sym.st_value;
2304fa9e4066Sahrens 
2305fa9e4066Sahrens 	if (mdb_layered_walk("avl", wsp) == -1) {
2306fa9e4066Sahrens 		mdb_warn("failed to walk 'avl'\n");
2307fa9e4066Sahrens 		return (WALK_ERR);
2308fa9e4066Sahrens 	}
2309fa9e4066Sahrens 
2310fa9e4066Sahrens 	return (WALK_NEXT);
2311fa9e4066Sahrens }
2312fa9e4066Sahrens 
2313fa9e4066Sahrens static int
2314fa9e4066Sahrens spa_walk_step(mdb_walk_state_t *wsp)
2315fa9e4066Sahrens {
231622ce0148SMatthew Ahrens 	return (wsp->walk_callback(wsp->walk_addr, NULL, wsp->walk_cbdata));
2317fa9e4066Sahrens }
2318fa9e4066Sahrens 
2319ccae0b50Seschrock /*
2320ccae0b50Seschrock  * [addr]::walk zio
2321ccae0b50Seschrock  *
2322ccae0b50Seschrock  * Walk all active zio_t structures on the system.  This is simply a layered
2323ccae0b50Seschrock  * walk on top of ::walk zio_cache, with the optional ability to limit the
2324ccae0b50Seschrock  * structures to a particular pool.
2325ccae0b50Seschrock  */
2326ccae0b50Seschrock static int
2327ccae0b50Seschrock zio_walk_init(mdb_walk_state_t *wsp)
2328ccae0b50Seschrock {
2329dee140dfSMatthew Ahrens 	wsp->walk_data = (void *)wsp->walk_addr;
2330ccae0b50Seschrock 
2331ccae0b50Seschrock 	if (mdb_layered_walk("zio_cache", wsp) == -1) {
2332ccae0b50Seschrock 		mdb_warn("failed to walk 'zio_cache'\n");
2333ccae0b50Seschrock 		return (WALK_ERR);
2334ccae0b50Seschrock 	}
2335ccae0b50Seschrock 
2336ccae0b50Seschrock 	return (WALK_NEXT);
2337ccae0b50Seschrock }
2338ccae0b50Seschrock 
2339ccae0b50Seschrock static int
2340ccae0b50Seschrock zio_walk_step(mdb_walk_state_t *wsp)
2341ccae0b50Seschrock {
234228e4da25SMatthew Ahrens 	mdb_zio_t zio;
2343dee140dfSMatthew Ahrens 	uintptr_t spa = (uintptr_t)wsp->walk_data;
2344ccae0b50Seschrock 
234528e4da25SMatthew Ahrens 	if (mdb_ctf_vread(&zio, ZFS_STRUCT "zio", "mdb_zio_t",
234628e4da25SMatthew Ahrens 	    wsp->walk_addr, 0) == -1)
2347ccae0b50Seschrock 		return (WALK_ERR);
2348ccae0b50Seschrock 
2349dee140dfSMatthew Ahrens 	if (spa != 0 && spa != zio.io_spa)
2350ccae0b50Seschrock 		return (WALK_NEXT);
2351ccae0b50Seschrock 
2352ccae0b50Seschrock 	return (wsp->walk_callback(wsp->walk_addr, &zio, wsp->walk_cbdata));
2353ccae0b50Seschrock }
2354ccae0b50Seschrock 
2355ccae0b50Seschrock /*
2356ccae0b50Seschrock  * [addr]::walk zio_root
2357ccae0b50Seschrock  *
2358ccae0b50Seschrock  * Walk only root zio_t structures, optionally for a particular spa_t.
2359ccae0b50Seschrock  */
2360ccae0b50Seschrock static int
2361ccae0b50Seschrock zio_walk_root_step(mdb_walk_state_t *wsp)
2362ccae0b50Seschrock {
236328e4da25SMatthew Ahrens 	mdb_zio_t zio;
2364dee140dfSMatthew Ahrens 	uintptr_t spa = (uintptr_t)wsp->walk_data;
2365ccae0b50Seschrock 
236628e4da25SMatthew Ahrens 	if (mdb_ctf_vread(&zio, ZFS_STRUCT "zio", "mdb_zio_t",
236728e4da25SMatthew Ahrens 	    wsp->walk_addr, 0) == -1)
2368ccae0b50Seschrock 		return (WALK_ERR);
2369ccae0b50Seschrock 
2370dee140dfSMatthew Ahrens 	if (spa != 0 && spa != zio.io_spa)
2371ccae0b50Seschrock 		return (WALK_NEXT);
2372ccae0b50Seschrock 
2373a3f829aeSBill Moore 	/* If the parent list is not empty, ignore */
2374d5ee8a13SMatthew Ahrens 	if (zio.io_parent_list.list_head.list_next !=
237528e4da25SMatthew Ahrens 	    wsp->walk_addr +
237628e4da25SMatthew Ahrens 	    mdb_ctf_offsetof_by_name(ZFS_STRUCT "zio", "io_parent_list") +
237728e4da25SMatthew Ahrens 	    mdb_ctf_offsetof_by_name("struct list", "list_head"))
2378ccae0b50Seschrock 		return (WALK_NEXT);
2379ccae0b50Seschrock 
2380ccae0b50Seschrock 	return (wsp->walk_callback(wsp->walk_addr, &zio, wsp->walk_cbdata));
2381ccae0b50Seschrock }
2382ccae0b50Seschrock 
238388b7b0f2SMatthew Ahrens /*
238488b7b0f2SMatthew Ahrens  * ::zfs_blkstats
238588b7b0f2SMatthew Ahrens  *
23862e4c9986SGeorge Wilson  *	-v	print verbose per-level information
238788b7b0f2SMatthew Ahrens  *
238888b7b0f2SMatthew Ahrens  */
238988b7b0f2SMatthew Ahrens static int
239088b7b0f2SMatthew Ahrens zfs_blkstats(uintptr_t addr, uint_t flags, int argc, const mdb_arg_t *argv)
239188b7b0f2SMatthew Ahrens {
239288b7b0f2SMatthew Ahrens 	boolean_t verbose = B_FALSE;
239388b7b0f2SMatthew Ahrens 	zfs_all_blkstats_t stats;
239488b7b0f2SMatthew Ahrens 	dmu_object_type_t t;
239588b7b0f2SMatthew Ahrens 	zfs_blkstat_t *tzb;
239688b7b0f2SMatthew Ahrens 	uint64_t ditto;
239788b7b0f2SMatthew Ahrens 	dmu_object_type_info_t dmu_ot[DMU_OT_NUMTYPES + 10];
239888b7b0f2SMatthew Ahrens 	/* +10 in case it grew */
239988b7b0f2SMatthew Ahrens 
240088b7b0f2SMatthew Ahrens 	if (mdb_readvar(&dmu_ot, "dmu_ot") == -1) {
240188b7b0f2SMatthew Ahrens 		mdb_warn("failed to read 'dmu_ot'");
240288b7b0f2SMatthew Ahrens 		return (DCMD_ERR);
240388b7b0f2SMatthew Ahrens 	}
240488b7b0f2SMatthew Ahrens 
240588b7b0f2SMatthew Ahrens 	if (mdb_getopts(argc, argv,
240688b7b0f2SMatthew Ahrens 	    'v', MDB_OPT_SETBITS, TRUE, &verbose,
240788b7b0f2SMatthew Ahrens 	    NULL) != argc)
240888b7b0f2SMatthew Ahrens 		return (DCMD_USAGE);
240988b7b0f2SMatthew Ahrens 
241088b7b0f2SMatthew Ahrens 	if (!(flags & DCMD_ADDRSPEC))
241188b7b0f2SMatthew Ahrens 		return (DCMD_USAGE);
241288b7b0f2SMatthew Ahrens 
241328e4da25SMatthew Ahrens 	if (GETMEMB(addr, "spa", spa_dsl_pool, addr) ||
241428e4da25SMatthew Ahrens 	    GETMEMB(addr, "dsl_pool", dp_blkstats, addr) ||
241588b7b0f2SMatthew Ahrens 	    mdb_vread(&stats, sizeof (zfs_all_blkstats_t), addr) == -1) {
241688b7b0f2SMatthew Ahrens 		mdb_warn("failed to read data at %p;", addr);
241788b7b0f2SMatthew Ahrens 		mdb_printf("maybe no stats? run \"zpool scrub\" first.");
241888b7b0f2SMatthew Ahrens 		return (DCMD_ERR);
241988b7b0f2SMatthew Ahrens 	}
242088b7b0f2SMatthew Ahrens 
2421ad135b5dSChristopher Siden 	tzb = &stats.zab_type[DN_MAX_LEVELS][DMU_OT_TOTAL];
242288b7b0f2SMatthew Ahrens 	if (tzb->zb_gangs != 0) {
242388b7b0f2SMatthew Ahrens 		mdb_printf("Ganged blocks: %llu\n",
242488b7b0f2SMatthew Ahrens 		    (longlong_t)tzb->zb_gangs);
242588b7b0f2SMatthew Ahrens 	}
242688b7b0f2SMatthew Ahrens 
242788b7b0f2SMatthew Ahrens 	ditto = tzb->zb_ditto_2_of_2_samevdev + tzb->zb_ditto_2_of_3_samevdev +
242888b7b0f2SMatthew Ahrens 	    tzb->zb_ditto_3_of_3_samevdev;
242988b7b0f2SMatthew Ahrens 	if (ditto != 0) {
243088b7b0f2SMatthew Ahrens 		mdb_printf("Dittoed blocks on same vdev: %llu\n",
243188b7b0f2SMatthew Ahrens 		    (longlong_t)ditto);
243288b7b0f2SMatthew Ahrens 	}
243388b7b0f2SMatthew Ahrens 
243488b7b0f2SMatthew Ahrens 	mdb_printf("\nBlocks\tLSIZE\tPSIZE\tASIZE"
243588b7b0f2SMatthew Ahrens 	    "\t  avg\t comp\t%%Total\tType\n");
243688b7b0f2SMatthew Ahrens 
2437ad135b5dSChristopher Siden 	for (t = 0; t <= DMU_OT_TOTAL; t++) {
243888b7b0f2SMatthew Ahrens 		char csize[NICENUM_BUFLEN], lsize[NICENUM_BUFLEN];
243988b7b0f2SMatthew Ahrens 		char psize[NICENUM_BUFLEN], asize[NICENUM_BUFLEN];
244088b7b0f2SMatthew Ahrens 		char avg[NICENUM_BUFLEN];
244188b7b0f2SMatthew Ahrens 		char comp[NICENUM_BUFLEN], pct[NICENUM_BUFLEN];
244288b7b0f2SMatthew Ahrens 		char typename[64];
244388b7b0f2SMatthew Ahrens 		int l;
244488b7b0f2SMatthew Ahrens 
244588b7b0f2SMatthew Ahrens 
244688b7b0f2SMatthew Ahrens 		if (t == DMU_OT_DEFERRED)
244788b7b0f2SMatthew Ahrens 			strcpy(typename, "deferred free");
2448ad135b5dSChristopher Siden 		else if (t == DMU_OT_OTHER)
2449ad135b5dSChristopher Siden 			strcpy(typename, "other");
245088b7b0f2SMatthew Ahrens 		else if (t == DMU_OT_TOTAL)
245188b7b0f2SMatthew Ahrens 			strcpy(typename, "Total");
245288b7b0f2SMatthew Ahrens 		else if (mdb_readstr(typename, sizeof (typename),
245388b7b0f2SMatthew Ahrens 		    (uintptr_t)dmu_ot[t].ot_name) == -1) {
245488b7b0f2SMatthew Ahrens 			mdb_warn("failed to read type name");
245588b7b0f2SMatthew Ahrens 			return (DCMD_ERR);
245688b7b0f2SMatthew Ahrens 		}
245788b7b0f2SMatthew Ahrens 
245888b7b0f2SMatthew Ahrens 		if (stats.zab_type[DN_MAX_LEVELS][t].zb_asize == 0)
245988b7b0f2SMatthew Ahrens 			continue;
246088b7b0f2SMatthew Ahrens 
246188b7b0f2SMatthew Ahrens 		for (l = -1; l < DN_MAX_LEVELS; l++) {
246288b7b0f2SMatthew Ahrens 			int level = (l == -1 ? DN_MAX_LEVELS : l);
246388b7b0f2SMatthew Ahrens 			zfs_blkstat_t *zb = &stats.zab_type[level][t];
246488b7b0f2SMatthew Ahrens 
246588b7b0f2SMatthew Ahrens 			if (zb->zb_asize == 0)
246688b7b0f2SMatthew Ahrens 				continue;
246788b7b0f2SMatthew Ahrens 
246888b7b0f2SMatthew Ahrens 			/*
246988b7b0f2SMatthew Ahrens 			 * Don't print each level unless requested.
247088b7b0f2SMatthew Ahrens 			 */
247188b7b0f2SMatthew Ahrens 			if (!verbose && level != DN_MAX_LEVELS)
247288b7b0f2SMatthew Ahrens 				continue;
247388b7b0f2SMatthew Ahrens 
247488b7b0f2SMatthew Ahrens 			/*
247588b7b0f2SMatthew Ahrens 			 * If all the space is level 0, don't print the
247688b7b0f2SMatthew Ahrens 			 * level 0 separately.
247788b7b0f2SMatthew Ahrens 			 */
247888b7b0f2SMatthew Ahrens 			if (level == 0 && zb->zb_asize ==
247988b7b0f2SMatthew Ahrens 			    stats.zab_type[DN_MAX_LEVELS][t].zb_asize)
248088b7b0f2SMatthew Ahrens 				continue;
248188b7b0f2SMatthew Ahrens 
248288b7b0f2SMatthew Ahrens 			mdb_nicenum(zb->zb_count, csize);
248388b7b0f2SMatthew Ahrens 			mdb_nicenum(zb->zb_lsize, lsize);
248488b7b0f2SMatthew Ahrens 			mdb_nicenum(zb->zb_psize, psize);
248588b7b0f2SMatthew Ahrens 			mdb_nicenum(zb->zb_asize, asize);
248688b7b0f2SMatthew Ahrens 			mdb_nicenum(zb->zb_asize / zb->zb_count, avg);
2487ff64c0f7SMatthew Ahrens 			(void) snprintfrac(comp, NICENUM_BUFLEN,
2488ff64c0f7SMatthew Ahrens 			    zb->zb_lsize, zb->zb_psize, 2);
2489ff64c0f7SMatthew Ahrens 			(void) snprintfrac(pct, NICENUM_BUFLEN,
2490ff64c0f7SMatthew Ahrens 			    100 * zb->zb_asize, tzb->zb_asize, 2);
249188b7b0f2SMatthew Ahrens 
249288b7b0f2SMatthew Ahrens 			mdb_printf("%6s\t%5s\t%5s\t%5s\t%5s"
249388b7b0f2SMatthew Ahrens 			    "\t%5s\t%6s\t",
249488b7b0f2SMatthew Ahrens 			    csize, lsize, psize, asize, avg, comp, pct);
249588b7b0f2SMatthew Ahrens 
249688b7b0f2SMatthew Ahrens 			if (level == DN_MAX_LEVELS)
249788b7b0f2SMatthew Ahrens 				mdb_printf("%s\n", typename);
249888b7b0f2SMatthew Ahrens 			else
249988b7b0f2SMatthew Ahrens 				mdb_printf("  L%d %s\n",
250088b7b0f2SMatthew Ahrens 				    level, typename);
250188b7b0f2SMatthew Ahrens 		}
250288b7b0f2SMatthew Ahrens 	}
250388b7b0f2SMatthew Ahrens 
250488b7b0f2SMatthew Ahrens 	return (DCMD_OK);
250588b7b0f2SMatthew Ahrens }
250688b7b0f2SMatthew Ahrens 
2507d5ee8a13SMatthew Ahrens typedef struct mdb_reference {
2508d5ee8a13SMatthew Ahrens 	uintptr_t ref_holder;
2509d5ee8a13SMatthew Ahrens 	uintptr_t ref_removed;
2510d5ee8a13SMatthew Ahrens 	uint64_t ref_number;
2511d5ee8a13SMatthew Ahrens } mdb_reference_t;
2512d5ee8a13SMatthew Ahrens 
25139966ca11SMatthew Ahrens /* ARGSUSED */
25149966ca11SMatthew Ahrens static int
25159966ca11SMatthew Ahrens reference_cb(uintptr_t addr, const void *ignored, void *arg)
25169966ca11SMatthew Ahrens {
2517d5ee8a13SMatthew Ahrens 	mdb_reference_t ref;
25183f9d6ad7SLin Ling 	boolean_t holder_is_str = B_FALSE;
25199966ca11SMatthew Ahrens 	char holder_str[128];
25209966ca11SMatthew Ahrens 	boolean_t removed = (boolean_t)arg;
25219966ca11SMatthew Ahrens 
2522d5ee8a13SMatthew Ahrens 	if (mdb_ctf_vread(&ref, "reference_t", "mdb_reference_t", addr,
2523d5ee8a13SMatthew Ahrens 	    0) == -1)
2524d5ee8a13SMatthew Ahrens 		return (DCMD_ERR);
25259966ca11SMatthew Ahrens 
2526d5ee8a13SMatthew Ahrens 	if (mdb_readstr(holder_str, sizeof (holder_str),
2527d5ee8a13SMatthew Ahrens 	    ref.ref_holder) != -1)
25283f9d6ad7SLin Ling 		holder_is_str = strisprint(holder_str);
25299966ca11SMatthew Ahrens 
25309966ca11SMatthew Ahrens 	if (removed)
25319966ca11SMatthew Ahrens 		mdb_printf("removed ");
25329966ca11SMatthew Ahrens 	mdb_printf("reference ");
2533d5ee8a13SMatthew Ahrens 	if (ref.ref_number != 1)
2534d5ee8a13SMatthew Ahrens 		mdb_printf("with count=%llu ", ref.ref_number);
2535d5ee8a13SMatthew Ahrens 	mdb_printf("with tag %lx", ref.ref_holder);
25369966ca11SMatthew Ahrens 	if (holder_is_str)
25379966ca11SMatthew Ahrens 		mdb_printf(" \"%s\"", holder_str);
25389966ca11SMatthew Ahrens 	mdb_printf(", held at:\n");
25399966ca11SMatthew Ahrens 
25409966ca11SMatthew Ahrens 	(void) mdb_call_dcmd("whatis", addr, DCMD_ADDRSPEC, 0, NULL);
25419966ca11SMatthew Ahrens 
25429966ca11SMatthew Ahrens 	if (removed) {
25439966ca11SMatthew Ahrens 		mdb_printf("removed at:\n");
2544d5ee8a13SMatthew Ahrens 		(void) mdb_call_dcmd("whatis", ref.ref_removed,
25459966ca11SMatthew Ahrens 		    DCMD_ADDRSPEC, 0, NULL);
25469966ca11SMatthew Ahrens 	}
25479966ca11SMatthew Ahrens 
25489966ca11SMatthew Ahrens 	mdb_printf("\n");
25499966ca11SMatthew Ahrens 
25509966ca11SMatthew Ahrens 	return (WALK_NEXT);
25519966ca11SMatthew Ahrens }
25529966ca11SMatthew Ahrens 
2553d5ee8a13SMatthew Ahrens typedef struct mdb_refcount {
2554d5ee8a13SMatthew Ahrens 	uint64_t rc_count;
2555d5ee8a13SMatthew Ahrens } mdb_refcount_t;
2556d5ee8a13SMatthew Ahrens 
2557d5ee8a13SMatthew Ahrens typedef struct mdb_refcount_removed {
2558d5ee8a13SMatthew Ahrens 	uint64_t rc_removed_count;
2559d5ee8a13SMatthew Ahrens } mdb_refcount_removed_t;
2560d5ee8a13SMatthew Ahrens 
2561d5ee8a13SMatthew Ahrens typedef struct mdb_refcount_tracked {
2562d5ee8a13SMatthew Ahrens 	boolean_t rc_tracked;
2563d5ee8a13SMatthew Ahrens } mdb_refcount_tracked_t;
2564d5ee8a13SMatthew Ahrens 
25659966ca11SMatthew Ahrens /* ARGSUSED */
25669966ca11SMatthew Ahrens static int
25679966ca11SMatthew Ahrens refcount(uintptr_t addr, uint_t flags, int argc, const mdb_arg_t *argv)
25689966ca11SMatthew Ahrens {
2569d5ee8a13SMatthew Ahrens 	mdb_refcount_t rc;
2570d5ee8a13SMatthew Ahrens 	mdb_refcount_removed_t rcr;
2571d5ee8a13SMatthew Ahrens 	mdb_refcount_tracked_t rct;
2572d5ee8a13SMatthew Ahrens 	int off;
257328e4da25SMatthew Ahrens 	boolean_t released = B_FALSE;
25749966ca11SMatthew Ahrens 
25759966ca11SMatthew Ahrens 	if (!(flags & DCMD_ADDRSPEC))
25769966ca11SMatthew Ahrens 		return (DCMD_USAGE);
25779966ca11SMatthew Ahrens 
257828e4da25SMatthew Ahrens 	if (mdb_getopts(argc, argv,
257928e4da25SMatthew Ahrens 	    'r', MDB_OPT_SETBITS, B_TRUE, &released,
258028e4da25SMatthew Ahrens 	    NULL) != argc)
258128e4da25SMatthew Ahrens 		return (DCMD_USAGE);
258228e4da25SMatthew Ahrens 
2583d5ee8a13SMatthew Ahrens 	if (mdb_ctf_vread(&rc, "refcount_t", "mdb_refcount_t", addr,
2584d5ee8a13SMatthew Ahrens 	    0) == -1)
25859966ca11SMatthew Ahrens 		return (DCMD_ERR);
25869966ca11SMatthew Ahrens 
2587d5ee8a13SMatthew Ahrens 	if (mdb_ctf_vread(&rcr, "refcount_t", "mdb_refcount_removed_t", addr,
2588d5ee8a13SMatthew Ahrens 	    MDB_CTF_VREAD_QUIET) == -1) {
2589d5ee8a13SMatthew Ahrens 		mdb_printf("refcount_t at %p has %llu holds (untracked)\n",
2590d5ee8a13SMatthew Ahrens 		    addr, (longlong_t)rc.rc_count);
259128e4da25SMatthew Ahrens 		return (DCMD_OK);
259228e4da25SMatthew Ahrens 	}
259328e4da25SMatthew Ahrens 
2594d5ee8a13SMatthew Ahrens 	if (mdb_ctf_vread(&rct, "refcount_t", "mdb_refcount_tracked_t", addr,
2595d5ee8a13SMatthew Ahrens 	    MDB_CTF_VREAD_QUIET) == -1) {
2596d5ee8a13SMatthew Ahrens 		/* If this is an old target, it might be tracked. */
2597d5ee8a13SMatthew Ahrens 		rct.rc_tracked = B_TRUE;
2598d5ee8a13SMatthew Ahrens 	}
2599d5ee8a13SMatthew Ahrens 
26009966ca11SMatthew Ahrens 	mdb_printf("refcount_t at %p has %llu current holds, "
26019966ca11SMatthew Ahrens 	    "%llu recently released holds\n",
2602d5ee8a13SMatthew Ahrens 	    addr, (longlong_t)rc.rc_count, (longlong_t)rcr.rc_removed_count);
26039966ca11SMatthew Ahrens 
2604d5ee8a13SMatthew Ahrens 	if (rct.rc_tracked && rc.rc_count > 0)
26059966ca11SMatthew Ahrens 		mdb_printf("current holds:\n");
2606d5ee8a13SMatthew Ahrens 	off = mdb_ctf_offsetof_by_name("refcount_t", "rc_list");
2607d5ee8a13SMatthew Ahrens 	if (off == -1)
26089966ca11SMatthew Ahrens 		return (DCMD_ERR);
2609d5ee8a13SMatthew Ahrens 	mdb_pwalk("list", reference_cb, (void*)B_FALSE, addr + off);
2610d5ee8a13SMatthew Ahrens 
2611d5ee8a13SMatthew Ahrens 	if (released && rcr.rc_removed_count > 0) {
2612d5ee8a13SMatthew Ahrens 		mdb_printf("released holds:\n");
26139966ca11SMatthew Ahrens 
2614d5ee8a13SMatthew Ahrens 		off = mdb_ctf_offsetof_by_name("refcount_t", "rc_removed");
2615d5ee8a13SMatthew Ahrens 		if (off == -1)
261628e4da25SMatthew Ahrens 			return (DCMD_ERR);
2617421ff020SPrakash Surya 		mdb_pwalk("list", reference_cb, (void*)B_TRUE, addr + off);
261828e4da25SMatthew Ahrens 	}
26199966ca11SMatthew Ahrens 
26209966ca11SMatthew Ahrens 	return (DCMD_OK);
26219966ca11SMatthew Ahrens }
26229966ca11SMatthew Ahrens 
26230a586ceaSMark Shellenbaum /* ARGSUSED */
26240a586ceaSMark Shellenbaum static int
26250a586ceaSMark Shellenbaum sa_attr_table(uintptr_t addr, uint_t flags, int argc, const mdb_arg_t *argv)
26260a586ceaSMark Shellenbaum {
26270a586ceaSMark Shellenbaum 	sa_attr_table_t *table;
26280a586ceaSMark Shellenbaum 	sa_os_t sa_os;
26290a586ceaSMark Shellenbaum 	char *name;
26300a586ceaSMark Shellenbaum 	int i;
26310a586ceaSMark Shellenbaum 
26320a586ceaSMark Shellenbaum 	if (mdb_vread(&sa_os, sizeof (sa_os_t), addr) == -1) {
26330a586ceaSMark Shellenbaum 		mdb_warn("failed to read sa_os at %p", addr);
26340a586ceaSMark Shellenbaum 		return (DCMD_ERR);
26350a586ceaSMark Shellenbaum 	}
26360a586ceaSMark Shellenbaum 
26370a586ceaSMark Shellenbaum 	table = mdb_alloc(sizeof (sa_attr_table_t) * sa_os.sa_num_attrs,
26380a586ceaSMark Shellenbaum 	    UM_SLEEP | UM_GC);
26390a586ceaSMark Shellenbaum 	name = mdb_alloc(MAXPATHLEN, UM_SLEEP | UM_GC);
26400a586ceaSMark Shellenbaum 
26410a586ceaSMark Shellenbaum 	if (mdb_vread(table, sizeof (sa_attr_table_t) * sa_os.sa_num_attrs,
26420a586ceaSMark Shellenbaum 	    (uintptr_t)sa_os.sa_attr_table) == -1) {
26430a586ceaSMark Shellenbaum 		mdb_warn("failed to read sa_os at %p", addr);
26440a586ceaSMark Shellenbaum 		return (DCMD_ERR);
26450a586ceaSMark Shellenbaum 	}
26460a586ceaSMark Shellenbaum 
26470a586ceaSMark Shellenbaum 	mdb_printf("%<u>%-10s %-10s %-10s %-10s %s%</u>\n",
26480a586ceaSMark Shellenbaum 	    "ATTR ID", "REGISTERED", "LENGTH", "BSWAP", "NAME");
26490a586ceaSMark Shellenbaum 	for (i = 0; i != sa_os.sa_num_attrs; i++) {
26500a586ceaSMark Shellenbaum 		mdb_readstr(name, MAXPATHLEN, (uintptr_t)table[i].sa_name);
26510a586ceaSMark Shellenbaum 		mdb_printf("%5x   %8x %8x %8x          %-s\n",
26520a586ceaSMark Shellenbaum 		    (int)table[i].sa_attr, (int)table[i].sa_registered,
26530a586ceaSMark Shellenbaum 		    (int)table[i].sa_length, table[i].sa_byteswap, name);
26540a586ceaSMark Shellenbaum 	}
26550a586ceaSMark Shellenbaum 
26560a586ceaSMark Shellenbaum 	return (DCMD_OK);
26570a586ceaSMark Shellenbaum }
26580a586ceaSMark Shellenbaum 
26590a586ceaSMark Shellenbaum static int
26600a586ceaSMark Shellenbaum sa_get_off_table(uintptr_t addr, uint32_t **off_tab, int attr_count)
26610a586ceaSMark Shellenbaum {
26620a586ceaSMark Shellenbaum 	uintptr_t idx_table;
26630a586ceaSMark Shellenbaum 
266428e4da25SMatthew Ahrens 	if (GETMEMB(addr, "sa_idx_tab", sa_idx_tab, idx_table)) {
26650a586ceaSMark Shellenbaum 		mdb_printf("can't find offset table in sa_idx_tab\n");
26660a586ceaSMark Shellenbaum 		return (-1);
26670a586ceaSMark Shellenbaum 	}
26680a586ceaSMark Shellenbaum 
26690a586ceaSMark Shellenbaum 	*off_tab = mdb_alloc(attr_count * sizeof (uint32_t),
26700a586ceaSMark Shellenbaum 	    UM_SLEEP | UM_GC);
26710a586ceaSMark Shellenbaum 
26720a586ceaSMark Shellenbaum 	if (mdb_vread(*off_tab,
26730a586ceaSMark Shellenbaum 	    attr_count * sizeof (uint32_t), idx_table) == -1) {
26740a586ceaSMark Shellenbaum 		mdb_warn("failed to attribute offset table %p", idx_table);
26750a586ceaSMark Shellenbaum 		return (-1);
26760a586ceaSMark Shellenbaum 	}
26770a586ceaSMark Shellenbaum 
26780a586ceaSMark Shellenbaum 	return (DCMD_OK);
26790a586ceaSMark Shellenbaum }
26800a586ceaSMark Shellenbaum 
26810a586ceaSMark Shellenbaum /*ARGSUSED*/
26820a586ceaSMark Shellenbaum static int
26830a586ceaSMark Shellenbaum sa_attr_print(uintptr_t addr, uint_t flags, int argc, const mdb_arg_t *argv)
26840a586ceaSMark Shellenbaum {
26850a586ceaSMark Shellenbaum 	uint32_t *offset_tab;
26860a586ceaSMark Shellenbaum 	int attr_count;
26870a586ceaSMark Shellenbaum 	uint64_t attr_id;
26880a586ceaSMark Shellenbaum 	uintptr_t attr_addr;
26890a586ceaSMark Shellenbaum 	uintptr_t bonus_tab, spill_tab;
26900a586ceaSMark Shellenbaum 	uintptr_t db_bonus, db_spill;
26910a586ceaSMark Shellenbaum 	uintptr_t os, os_sa;
26920a586ceaSMark Shellenbaum 	uintptr_t db_data;
26930a586ceaSMark Shellenbaum 
26940a586ceaSMark Shellenbaum 	if (argc != 1)
26950a586ceaSMark Shellenbaum 		return (DCMD_USAGE);
26960a586ceaSMark Shellenbaum 
26970a586ceaSMark Shellenbaum 	if (argv[0].a_type == MDB_TYPE_STRING)
26980a586ceaSMark Shellenbaum 		attr_id = mdb_strtoull(argv[0].a_un.a_str);
26990a586ceaSMark Shellenbaum 	else
27000a586ceaSMark Shellenbaum 		return (DCMD_USAGE);
27010a586ceaSMark Shellenbaum 
270228e4da25SMatthew Ahrens 	if (GETMEMB(addr, "sa_handle", sa_bonus_tab, bonus_tab) ||
270328e4da25SMatthew Ahrens 	    GETMEMB(addr, "sa_handle", sa_spill_tab, spill_tab) ||
270428e4da25SMatthew Ahrens 	    GETMEMB(addr, "sa_handle", sa_os, os) ||
270528e4da25SMatthew Ahrens 	    GETMEMB(addr, "sa_handle", sa_bonus, db_bonus) ||
270628e4da25SMatthew Ahrens 	    GETMEMB(addr, "sa_handle", sa_spill, db_spill)) {
27070a586ceaSMark Shellenbaum 		mdb_printf("Can't find necessary information in sa_handle "
27080a586ceaSMark Shellenbaum 		    "in sa_handle\n");
27090a586ceaSMark Shellenbaum 		return (DCMD_ERR);
27100a586ceaSMark Shellenbaum 	}
27110a586ceaSMark Shellenbaum 
271228e4da25SMatthew Ahrens 	if (GETMEMB(os, "objset", os_sa, os_sa)) {
27130a586ceaSMark Shellenbaum 		mdb_printf("Can't find os_sa in objset\n");
27140a586ceaSMark Shellenbaum 		return (DCMD_ERR);
27150a586ceaSMark Shellenbaum 	}
27160a586ceaSMark Shellenbaum 
271728e4da25SMatthew Ahrens 	if (GETMEMB(os_sa, "sa_os", sa_num_attrs, attr_count)) {
27180a586ceaSMark Shellenbaum 		mdb_printf("Can't find sa_num_attrs\n");
27190a586ceaSMark Shellenbaum 		return (DCMD_ERR);
27200a586ceaSMark Shellenbaum 	}
27210a586ceaSMark Shellenbaum 
27220a586ceaSMark Shellenbaum 	if (attr_id > attr_count) {
27230a586ceaSMark Shellenbaum 		mdb_printf("attribute id number is out of range\n");
27240a586ceaSMark Shellenbaum 		return (DCMD_ERR);
27250a586ceaSMark Shellenbaum 	}
27260a586ceaSMark Shellenbaum 
27270a586ceaSMark Shellenbaum 	if (bonus_tab) {
27280a586ceaSMark Shellenbaum 		if (sa_get_off_table(bonus_tab, &offset_tab,
27290a586ceaSMark Shellenbaum 		    attr_count) == -1) {
27300a586ceaSMark Shellenbaum 			return (DCMD_ERR);
27310a586ceaSMark Shellenbaum 		}
27320a586ceaSMark Shellenbaum 
273328e4da25SMatthew Ahrens 		if (GETMEMB(db_bonus, "dmu_buf", db_data, db_data)) {
27340a586ceaSMark Shellenbaum 			mdb_printf("can't find db_data in bonus dbuf\n");
27350a586ceaSMark Shellenbaum 			return (DCMD_ERR);
27360a586ceaSMark Shellenbaum 		}
27370a586ceaSMark Shellenbaum 	}
27380a586ceaSMark Shellenbaum 
27390a586ceaSMark Shellenbaum 	if (bonus_tab && !TOC_ATTR_PRESENT(offset_tab[attr_id]) &&
27400a586ceaSMark Shellenbaum 	    spill_tab == NULL) {
27410a586ceaSMark Shellenbaum 		mdb_printf("Attribute does not exist\n");
27420a586ceaSMark Shellenbaum 		return (DCMD_ERR);
27430a586ceaSMark Shellenbaum 	} else if (!TOC_ATTR_PRESENT(offset_tab[attr_id]) && spill_tab) {
27440a586ceaSMark Shellenbaum 		if (sa_get_off_table(spill_tab, &offset_tab,
27450a586ceaSMark Shellenbaum 		    attr_count) == -1) {
27460a586ceaSMark Shellenbaum 			return (DCMD_ERR);
27470a586ceaSMark Shellenbaum 		}
274828e4da25SMatthew Ahrens 		if (GETMEMB(db_spill, "dmu_buf", db_data, db_data)) {
27490a586ceaSMark Shellenbaum 			mdb_printf("can't find db_data in spill dbuf\n");
27500a586ceaSMark Shellenbaum 			return (DCMD_ERR);
27510a586ceaSMark Shellenbaum 		}
27520a586ceaSMark Shellenbaum 		if (!TOC_ATTR_PRESENT(offset_tab[attr_id])) {
27530a586ceaSMark Shellenbaum 			mdb_printf("Attribute does not exist\n");
27540a586ceaSMark Shellenbaum 			return (DCMD_ERR);
27550a586ceaSMark Shellenbaum 		}
27560a586ceaSMark Shellenbaum 	}
27570a586ceaSMark Shellenbaum 	attr_addr = db_data + TOC_OFF(offset_tab[attr_id]);
27580a586ceaSMark Shellenbaum 	mdb_printf("%p\n", attr_addr);
27590a586ceaSMark Shellenbaum 	return (DCMD_OK);
27600a586ceaSMark Shellenbaum }
27610a586ceaSMark Shellenbaum 
27620a586ceaSMark Shellenbaum /* ARGSUSED */
27630a586ceaSMark Shellenbaum static int
27640a586ceaSMark Shellenbaum zfs_ace_print_common(uintptr_t addr, uint_t flags,
27650a586ceaSMark Shellenbaum     uint64_t id, uint32_t access_mask, uint16_t ace_flags,
27660a586ceaSMark Shellenbaum     uint16_t ace_type, int verbose)
27670a586ceaSMark Shellenbaum {
27680a586ceaSMark Shellenbaum 	if (DCMD_HDRSPEC(flags) && !verbose)
27690a586ceaSMark Shellenbaum 		mdb_printf("%<u>%-?s %-8s %-8s %-8s %s%</u>\n",
27700a586ceaSMark Shellenbaum 		    "ADDR", "FLAGS", "MASK", "TYPE", "ID");
27710a586ceaSMark Shellenbaum 
27720a586ceaSMark Shellenbaum 	if (!verbose) {
27730a586ceaSMark Shellenbaum 		mdb_printf("%0?p %-8x %-8x %-8x %-llx\n", addr,
27740a586ceaSMark Shellenbaum 		    ace_flags, access_mask, ace_type, id);
27750a586ceaSMark Shellenbaum 		return (DCMD_OK);
27760a586ceaSMark Shellenbaum 	}
27770a586ceaSMark Shellenbaum 
27780a586ceaSMark Shellenbaum 	switch (ace_flags & ACE_TYPE_FLAGS) {
27790a586ceaSMark Shellenbaum 	case ACE_OWNER:
27800a586ceaSMark Shellenbaum 		mdb_printf("owner@:");
27810a586ceaSMark Shellenbaum 		break;
27820a586ceaSMark Shellenbaum 	case (ACE_IDENTIFIER_GROUP | ACE_GROUP):
27830a586ceaSMark Shellenbaum 		mdb_printf("group@:");
27840a586ceaSMark Shellenbaum 		break;
27850a586ceaSMark Shellenbaum 	case ACE_EVERYONE:
27860a586ceaSMark Shellenbaum 		mdb_printf("everyone@:");
27870a586ceaSMark Shellenbaum 		break;
27880a586ceaSMark Shellenbaum 	case ACE_IDENTIFIER_GROUP:
27890a586ceaSMark Shellenbaum 		mdb_printf("group:%llx:", (u_longlong_t)id);
27900a586ceaSMark Shellenbaum 		break;
27910a586ceaSMark Shellenbaum 	case 0: /* User entry */
27920a586ceaSMark Shellenbaum 		mdb_printf("user:%llx:", (u_longlong_t)id);
27930a586ceaSMark Shellenbaum 		break;
27940a586ceaSMark Shellenbaum 	}
27950a586ceaSMark Shellenbaum 
27960a586ceaSMark Shellenbaum 	/* print out permission mask */
27970a586ceaSMark Shellenbaum 	if (access_mask & ACE_READ_DATA)
27980a586ceaSMark Shellenbaum 		mdb_printf("r");
27990a586ceaSMark Shellenbaum 	else
28000a586ceaSMark Shellenbaum 		mdb_printf("-");
28010a586ceaSMark Shellenbaum 	if (access_mask & ACE_WRITE_DATA)
28020a586ceaSMark Shellenbaum 		mdb_printf("w");
28030a586ceaSMark Shellenbaum 	else
28040a586ceaSMark Shellenbaum 		mdb_printf("-");
28050a586ceaSMark Shellenbaum 	if (access_mask & ACE_EXECUTE)
28060a586ceaSMark Shellenbaum 		mdb_printf("x");
28070a586ceaSMark Shellenbaum 	else
28080a586ceaSMark Shellenbaum 		mdb_printf("-");
28090a586ceaSMark Shellenbaum 	if (access_mask & ACE_APPEND_DATA)
28100a586ceaSMark Shellenbaum 		mdb_printf("p");
28110a586ceaSMark Shellenbaum 	else
28120a586ceaSMark Shellenbaum 		mdb_printf("-");
28130a586ceaSMark Shellenbaum 	if (access_mask & ACE_DELETE)
28140a586ceaSMark Shellenbaum 		mdb_printf("d");
28150a586ceaSMark Shellenbaum 	else
28160a586ceaSMark Shellenbaum 		mdb_printf("-");
28170a586ceaSMark Shellenbaum 	if (access_mask & ACE_DELETE_CHILD)
28180a586ceaSMark Shellenbaum 		mdb_printf("D");
28190a586ceaSMark Shellenbaum 	else
28200a586ceaSMark Shellenbaum 		mdb_printf("-");
28210a586ceaSMark Shellenbaum 	if (access_mask & ACE_READ_ATTRIBUTES)
28220a586ceaSMark Shellenbaum 		mdb_printf("a");
28230a586ceaSMark Shellenbaum 	else
28240a586ceaSMark Shellenbaum 		mdb_printf("-");
28250a586ceaSMark Shellenbaum 	if (access_mask & ACE_WRITE_ATTRIBUTES)
28260a586ceaSMark Shellenbaum 		mdb_printf("A");
28270a586ceaSMark Shellenbaum 	else
28280a586ceaSMark Shellenbaum 		mdb_printf("-");
28290a586ceaSMark Shellenbaum 	if (access_mask & ACE_READ_NAMED_ATTRS)
28300a586ceaSMark Shellenbaum 		mdb_printf("R");
28310a586ceaSMark Shellenbaum 	else
28320a586ceaSMark Shellenbaum 		mdb_printf("-");
28330a586ceaSMark Shellenbaum 	if (access_mask & ACE_WRITE_NAMED_ATTRS)
28340a586ceaSMark Shellenbaum 		mdb_printf("W");
28350a586ceaSMark Shellenbaum 	else
28360a586ceaSMark Shellenbaum 		mdb_printf("-");
28370a586ceaSMark Shellenbaum 	if (access_mask & ACE_READ_ACL)
28380a586ceaSMark Shellenbaum 		mdb_printf("c");
28390a586ceaSMark Shellenbaum 	else
28400a586ceaSMark Shellenbaum 		mdb_printf("-");
28410a586ceaSMark Shellenbaum 	if (access_mask & ACE_WRITE_ACL)
28420a586ceaSMark Shellenbaum 		mdb_printf("C");
28430a586ceaSMark Shellenbaum 	else
28440a586ceaSMark Shellenbaum 		mdb_printf("-");
28450a586ceaSMark Shellenbaum 	if (access_mask & ACE_WRITE_OWNER)
28460a586ceaSMark Shellenbaum 		mdb_printf("o");
28470a586ceaSMark Shellenbaum 	else
28480a586ceaSMark Shellenbaum 		mdb_printf("-");
28490a586ceaSMark Shellenbaum 	if (access_mask & ACE_SYNCHRONIZE)
28500a586ceaSMark Shellenbaum 		mdb_printf("s");
28510a586ceaSMark Shellenbaum 	else
28520a586ceaSMark Shellenbaum 		mdb_printf("-");
28530a586ceaSMark Shellenbaum 
28540a586ceaSMark Shellenbaum 	mdb_printf(":");
28550a586ceaSMark Shellenbaum 
28560a586ceaSMark Shellenbaum 	/* Print out inheritance flags */
28570a586ceaSMark Shellenbaum 	if (ace_flags & ACE_FILE_INHERIT_ACE)
28580a586ceaSMark Shellenbaum 		mdb_printf("f");
28590a586ceaSMark Shellenbaum 	else
28600a586ceaSMark Shellenbaum 		mdb_printf("-");
28610a586ceaSMark Shellenbaum 	if (ace_flags & ACE_DIRECTORY_INHERIT_ACE)
28620a586ceaSMark Shellenbaum 		mdb_printf("d");
28630a586ceaSMark Shellenbaum 	else
28640a586ceaSMark Shellenbaum 		mdb_printf("-");
28650a586ceaSMark Shellenbaum 	if (ace_flags & ACE_INHERIT_ONLY_ACE)
28660a586ceaSMark Shellenbaum 		mdb_printf("i");
28670a586ceaSMark Shellenbaum 	else
28680a586ceaSMark Shellenbaum 		mdb_printf("-");
28690a586ceaSMark Shellenbaum 	if (ace_flags & ACE_NO_PROPAGATE_INHERIT_ACE)
28700a586ceaSMark Shellenbaum 		mdb_printf("n");
28710a586ceaSMark Shellenbaum 	else
28720a586ceaSMark Shellenbaum 		mdb_printf("-");
28730a586ceaSMark Shellenbaum 	if (ace_flags & ACE_SUCCESSFUL_ACCESS_ACE_FLAG)
28740a586ceaSMark Shellenbaum 		mdb_printf("S");
28750a586ceaSMark Shellenbaum 	else
28760a586ceaSMark Shellenbaum 		mdb_printf("-");
28770a586ceaSMark Shellenbaum 	if (ace_flags & ACE_FAILED_ACCESS_ACE_FLAG)
28780a586ceaSMark Shellenbaum 		mdb_printf("F");
28790a586ceaSMark Shellenbaum 	else
28800a586ceaSMark Shellenbaum 		mdb_printf("-");
28810a586ceaSMark Shellenbaum 	if (ace_flags & ACE_INHERITED_ACE)
28820a586ceaSMark Shellenbaum 		mdb_printf("I");
28830a586ceaSMark Shellenbaum 	else
28840a586ceaSMark Shellenbaum 		mdb_printf("-");
28850a586ceaSMark Shellenbaum 
28860a586ceaSMark Shellenbaum 	switch (ace_type) {
28870a586ceaSMark Shellenbaum 	case ACE_ACCESS_ALLOWED_ACE_TYPE:
28880a586ceaSMark Shellenbaum 		mdb_printf(":allow\n");
28890a586ceaSMark Shellenbaum 		break;
28900a586ceaSMark Shellenbaum 	case ACE_ACCESS_DENIED_ACE_TYPE:
28910a586ceaSMark Shellenbaum 		mdb_printf(":deny\n");
28920a586ceaSMark Shellenbaum 		break;
28930a586ceaSMark Shellenbaum 	case ACE_SYSTEM_AUDIT_ACE_TYPE:
28940a586ceaSMark Shellenbaum 		mdb_printf(":audit\n");
28950a586ceaSMark Shellenbaum 		break;
28960a586ceaSMark Shellenbaum 	case ACE_SYSTEM_ALARM_ACE_TYPE:
28970a586ceaSMark Shellenbaum 		mdb_printf(":alarm\n");
28980a586ceaSMark Shellenbaum 		break;
28990a586ceaSMark Shellenbaum 	default:
29000a586ceaSMark Shellenbaum 		mdb_printf(":?\n");
29010a586ceaSMark Shellenbaum 	}
29020a586ceaSMark Shellenbaum 	return (DCMD_OK);
29030a586ceaSMark Shellenbaum }
29040a586ceaSMark Shellenbaum 
29050a586ceaSMark Shellenbaum /* ARGSUSED */
29060a586ceaSMark Shellenbaum static int
29070a586ceaSMark Shellenbaum zfs_ace_print(uintptr_t addr, uint_t flags, int argc, const mdb_arg_t *argv)
29080a586ceaSMark Shellenbaum {
29090a586ceaSMark Shellenbaum 	zfs_ace_t zace;
29100a586ceaSMark Shellenbaum 	int verbose = FALSE;
29110a586ceaSMark Shellenbaum 	uint64_t id;
29120a586ceaSMark Shellenbaum 
29130a586ceaSMark Shellenbaum 	if (!(flags & DCMD_ADDRSPEC))
29140a586ceaSMark Shellenbaum 		return (DCMD_USAGE);
29150a586ceaSMark Shellenbaum 
29160a586ceaSMark Shellenbaum 	if (mdb_getopts(argc, argv,
29170a586ceaSMark Shellenbaum 	    'v', MDB_OPT_SETBITS, TRUE, &verbose, TRUE, NULL) != argc)
29180a586ceaSMark Shellenbaum 		return (DCMD_USAGE);
29190a586ceaSMark Shellenbaum 
29200a586ceaSMark Shellenbaum 	if (mdb_vread(&zace, sizeof (zfs_ace_t), addr) == -1) {
29210a586ceaSMark Shellenbaum 		mdb_warn("failed to read zfs_ace_t");
29220a586ceaSMark Shellenbaum 		return (DCMD_ERR);
29230a586ceaSMark Shellenbaum 	}
29240a586ceaSMark Shellenbaum 
29250a586ceaSMark Shellenbaum 	if ((zace.z_hdr.z_flags & ACE_TYPE_FLAGS) == 0 ||
29260a586ceaSMark Shellenbaum 	    (zace.z_hdr.z_flags & ACE_TYPE_FLAGS) == ACE_IDENTIFIER_GROUP)
29270a586ceaSMark Shellenbaum 		id = zace.z_fuid;
29280a586ceaSMark Shellenbaum 	else
29290a586ceaSMark Shellenbaum 		id = -1;
29300a586ceaSMark Shellenbaum 
29310a586ceaSMark Shellenbaum 	return (zfs_ace_print_common(addr, flags, id, zace.z_hdr.z_access_mask,
29320a586ceaSMark Shellenbaum 	    zace.z_hdr.z_flags, zace.z_hdr.z_type, verbose));
29330a586ceaSMark Shellenbaum }
29340a586ceaSMark Shellenbaum 
29350a586ceaSMark Shellenbaum /* ARGSUSED */
29360a586ceaSMark Shellenbaum static int
29370a586ceaSMark Shellenbaum zfs_ace0_print(uintptr_t addr, uint_t flags, int argc, const mdb_arg_t *argv)
29380a586ceaSMark Shellenbaum {
29390a586ceaSMark Shellenbaum 	ace_t ace;
29400a586ceaSMark Shellenbaum 	uint64_t id;
29410a586ceaSMark Shellenbaum 	int verbose = FALSE;
29420a586ceaSMark Shellenbaum 
29430a586ceaSMark Shellenbaum 	if (!(flags & DCMD_ADDRSPEC))
29440a586ceaSMark Shellenbaum 		return (DCMD_USAGE);
29450a586ceaSMark Shellenbaum 
29460a586ceaSMark Shellenbaum 	if (mdb_getopts(argc, argv,
29470a586ceaSMark Shellenbaum 	    'v', MDB_OPT_SETBITS, TRUE, &verbose, TRUE, NULL) != argc)
29480a586ceaSMark Shellenbaum 		return (DCMD_USAGE);
29490a586ceaSMark Shellenbaum 
29500a586ceaSMark Shellenbaum 	if (mdb_vread(&ace, sizeof (ace_t), addr) == -1) {
29510a586ceaSMark Shellenbaum 		mdb_warn("failed to read ace_t");
29520a586ceaSMark Shellenbaum 		return (DCMD_ERR);
29530a586ceaSMark Shellenbaum 	}
29540a586ceaSMark Shellenbaum 
29550a586ceaSMark Shellenbaum 	if ((ace.a_flags & ACE_TYPE_FLAGS) == 0 ||
29560a586ceaSMark Shellenbaum 	    (ace.a_flags & ACE_TYPE_FLAGS) == ACE_IDENTIFIER_GROUP)
29570a586ceaSMark Shellenbaum 		id = ace.a_who;
29580a586ceaSMark Shellenbaum 	else
29590a586ceaSMark Shellenbaum 		id = -1;
29600a586ceaSMark Shellenbaum 
29610a586ceaSMark Shellenbaum 	return (zfs_ace_print_common(addr, flags, id, ace.a_access_mask,
29620a586ceaSMark Shellenbaum 	    ace.a_flags, ace.a_type, verbose));
29630a586ceaSMark Shellenbaum }
29640a586ceaSMark Shellenbaum 
29650a586ceaSMark Shellenbaum typedef struct acl_dump_args {
29660a586ceaSMark Shellenbaum 	int a_argc;
29670a586ceaSMark Shellenbaum 	const mdb_arg_t *a_argv;
29680a586ceaSMark Shellenbaum 	uint16_t a_version;
29690a586ceaSMark Shellenbaum 	int a_flags;
29700a586ceaSMark Shellenbaum } acl_dump_args_t;
29710a586ceaSMark Shellenbaum 
29720a586ceaSMark Shellenbaum /* ARGSUSED */
29730a586ceaSMark Shellenbaum static int
29740a586ceaSMark Shellenbaum acl_aces_cb(uintptr_t addr, const void *unknown, void *arg)
29750a586ceaSMark Shellenbaum {
29760a586ceaSMark Shellenbaum 	acl_dump_args_t *acl_args = (acl_dump_args_t *)arg;
29770a586ceaSMark Shellenbaum 
29780a586ceaSMark Shellenbaum 	if (acl_args->a_version == 1) {
29790a586ceaSMark Shellenbaum 		if (mdb_call_dcmd("zfs_ace", addr,
29800a586ceaSMark Shellenbaum 		    DCMD_ADDRSPEC|acl_args->a_flags, acl_args->a_argc,
29810a586ceaSMark Shellenbaum 		    acl_args->a_argv) != DCMD_OK) {
29820a586ceaSMark Shellenbaum 			return (WALK_ERR);
29830a586ceaSMark Shellenbaum 		}
29840a586ceaSMark Shellenbaum 	} else {
29850a586ceaSMark Shellenbaum 		if (mdb_call_dcmd("zfs_ace0", addr,
29860a586ceaSMark Shellenbaum 		    DCMD_ADDRSPEC|acl_args->a_flags, acl_args->a_argc,
29870a586ceaSMark Shellenbaum 		    acl_args->a_argv) != DCMD_OK) {
29880a586ceaSMark Shellenbaum 			return (WALK_ERR);
29890a586ceaSMark Shellenbaum 		}
29900a586ceaSMark Shellenbaum 	}
29910a586ceaSMark Shellenbaum 	acl_args->a_flags = DCMD_LOOP;
29920a586ceaSMark Shellenbaum 	return (WALK_NEXT);
29930a586ceaSMark Shellenbaum }
29940a586ceaSMark Shellenbaum 
29950a586ceaSMark Shellenbaum /* ARGSUSED */
29960a586ceaSMark Shellenbaum static int
29970a586ceaSMark Shellenbaum acl_cb(uintptr_t addr, const void *unknown, void *arg)
29980a586ceaSMark Shellenbaum {
29990a586ceaSMark Shellenbaum 	acl_dump_args_t *acl_args = (acl_dump_args_t *)arg;
30000a586ceaSMark Shellenbaum 
30010a586ceaSMark Shellenbaum 	if (acl_args->a_version == 1) {
30020a586ceaSMark Shellenbaum 		if (mdb_pwalk("zfs_acl_node_aces", acl_aces_cb,
30030a586ceaSMark Shellenbaum 		    arg, addr) != 0) {
30040a586ceaSMark Shellenbaum 			mdb_warn("can't walk ACEs");
30050a586ceaSMark Shellenbaum 			return (DCMD_ERR);
30060a586ceaSMark Shellenbaum 		}
30070a586ceaSMark Shellenbaum 	} else {
30080a586ceaSMark Shellenbaum 		if (mdb_pwalk("zfs_acl_node_aces0", acl_aces_cb,
30090a586ceaSMark Shellenbaum 		    arg, addr) != 0) {
30100a586ceaSMark Shellenbaum 			mdb_warn("can't walk ACEs");
30110a586ceaSMark Shellenbaum 			return (DCMD_ERR);
30120a586ceaSMark Shellenbaum 		}
30130a586ceaSMark Shellenbaum 	}
30140a586ceaSMark Shellenbaum 	return (WALK_NEXT);
30150a586ceaSMark Shellenbaum }
30160a586ceaSMark Shellenbaum 
30170a586ceaSMark Shellenbaum /* ARGSUSED */
30180a586ceaSMark Shellenbaum static int
30190a586ceaSMark Shellenbaum zfs_acl_dump(uintptr_t addr, uint_t flags, int argc, const mdb_arg_t *argv)
30200a586ceaSMark Shellenbaum {
30210a586ceaSMark Shellenbaum 	zfs_acl_t zacl;
30220a586ceaSMark Shellenbaum 	int verbose = FALSE;
30230a586ceaSMark Shellenbaum 	acl_dump_args_t acl_args;
30240a586ceaSMark Shellenbaum 
30250a586ceaSMark Shellenbaum 	if (!(flags & DCMD_ADDRSPEC))
30260a586ceaSMark Shellenbaum 		return (DCMD_USAGE);
30270a586ceaSMark Shellenbaum 
30280a586ceaSMark Shellenbaum 	if (mdb_getopts(argc, argv,
30290a586ceaSMark Shellenbaum 	    'v', MDB_OPT_SETBITS, TRUE, &verbose, TRUE, NULL) != argc)
30300a586ceaSMark Shellenbaum 		return (DCMD_USAGE);
30310a586ceaSMark Shellenbaum 
30320a586ceaSMark Shellenbaum 	if (mdb_vread(&zacl, sizeof (zfs_acl_t), addr) == -1) {
30330a586ceaSMark Shellenbaum 		mdb_warn("failed to read zfs_acl_t");
30340a586ceaSMark Shellenbaum 		return (DCMD_ERR);
30350a586ceaSMark Shellenbaum 	}
30360a586ceaSMark Shellenbaum 
30370a586ceaSMark Shellenbaum 	acl_args.a_argc = argc;
30380a586ceaSMark Shellenbaum 	acl_args.a_argv = argv;
30390a586ceaSMark Shellenbaum 	acl_args.a_version = zacl.z_version;
30400a586ceaSMark Shellenbaum 	acl_args.a_flags = DCMD_LOOPFIRST;
30410a586ceaSMark Shellenbaum 
30420a586ceaSMark Shellenbaum 	if (mdb_pwalk("zfs_acl_node", acl_cb, &acl_args, addr) != 0) {
30430a586ceaSMark Shellenbaum 		mdb_warn("can't walk ACL");
30440a586ceaSMark Shellenbaum 		return (DCMD_ERR);
30450a586ceaSMark Shellenbaum 	}
30460a586ceaSMark Shellenbaum 
30470a586ceaSMark Shellenbaum 	return (DCMD_OK);
30480a586ceaSMark Shellenbaum }
30490a586ceaSMark Shellenbaum 
30500a586ceaSMark Shellenbaum /* ARGSUSED */
30510a586ceaSMark Shellenbaum static int
30520a586ceaSMark Shellenbaum zfs_acl_node_walk_init(mdb_walk_state_t *wsp)
30530a586ceaSMark Shellenbaum {
30540a586ceaSMark Shellenbaum 	if (wsp->walk_addr == NULL) {
30550a586ceaSMark Shellenbaum 		mdb_warn("must supply address of zfs_acl_node_t\n");
30560a586ceaSMark Shellenbaum 		return (WALK_ERR);
30570a586ceaSMark Shellenbaum 	}
30580a586ceaSMark Shellenbaum 
305928e4da25SMatthew Ahrens 	wsp->walk_addr +=
306028e4da25SMatthew Ahrens 	    mdb_ctf_offsetof_by_name(ZFS_STRUCT "zfs_acl", "z_acl");
30610a586ceaSMark Shellenbaum 
30620a586ceaSMark Shellenbaum 	if (mdb_layered_walk("list", wsp) == -1) {
30630a586ceaSMark Shellenbaum 		mdb_warn("failed to walk 'list'\n");
30640a586ceaSMark Shellenbaum 		return (WALK_ERR);
30650a586ceaSMark Shellenbaum 	}
30660a586ceaSMark Shellenbaum 
30670a586ceaSMark Shellenbaum 	return (WALK_NEXT);
30680a586ceaSMark Shellenbaum }
30690a586ceaSMark Shellenbaum 
30700a586ceaSMark Shellenbaum static int
30710a586ceaSMark Shellenbaum zfs_acl_node_walk_step(mdb_walk_state_t *wsp)
30720a586ceaSMark Shellenbaum {
30730a586ceaSMark Shellenbaum 	zfs_acl_node_t	aclnode;
30740a586ceaSMark Shellenbaum 
30750a586ceaSMark Shellenbaum 	if (mdb_vread(&aclnode, sizeof (zfs_acl_node_t),
30760a586ceaSMark Shellenbaum 	    wsp->walk_addr) == -1) {
30770a586ceaSMark Shellenbaum 		mdb_warn("failed to read zfs_acl_node at %p", wsp->walk_addr);
30780a586ceaSMark Shellenbaum 		return (WALK_ERR);
30790a586ceaSMark Shellenbaum 	}
30800a586ceaSMark Shellenbaum 
30810a586ceaSMark Shellenbaum 	return (wsp->walk_callback(wsp->walk_addr, &aclnode, wsp->walk_cbdata));
30820a586ceaSMark Shellenbaum }
30830a586ceaSMark Shellenbaum 
30840a586ceaSMark Shellenbaum typedef struct ace_walk_data {
30850a586ceaSMark Shellenbaum 	int		ace_count;
30860a586ceaSMark Shellenbaum 	int		ace_version;
30870a586ceaSMark Shellenbaum } ace_walk_data_t;
30880a586ceaSMark Shellenbaum 
30890a586ceaSMark Shellenbaum static int
30900a586ceaSMark Shellenbaum zfs_aces_walk_init_common(mdb_walk_state_t *wsp, int version,
30910a586ceaSMark Shellenbaum     int ace_count, uintptr_t ace_data)
30920a586ceaSMark Shellenbaum {
30930a586ceaSMark Shellenbaum 	ace_walk_data_t *ace_walk_data;
30940a586ceaSMark Shellenbaum 
30950a586ceaSMark Shellenbaum 	if (wsp->walk_addr == NULL) {
30960a586ceaSMark Shellenbaum 		mdb_warn("must supply address of zfs_acl_node_t\n");
30970a586ceaSMark Shellenbaum 		return (WALK_ERR);
30980a586ceaSMark Shellenbaum 	}
30990a586ceaSMark Shellenbaum 
31000a586ceaSMark Shellenbaum 	ace_walk_data = mdb_alloc(sizeof (ace_walk_data_t), UM_SLEEP | UM_GC);
31010a586ceaSMark Shellenbaum 
31020a586ceaSMark Shellenbaum 	ace_walk_data->ace_count = ace_count;
31030a586ceaSMark Shellenbaum 	ace_walk_data->ace_version = version;
31040a586ceaSMark Shellenbaum 
31050a586ceaSMark Shellenbaum 	wsp->walk_addr = ace_data;
31060a586ceaSMark Shellenbaum 	wsp->walk_data = ace_walk_data;
31070a586ceaSMark Shellenbaum 
31080a586ceaSMark Shellenbaum 	return (WALK_NEXT);
31090a586ceaSMark Shellenbaum }
31100a586ceaSMark Shellenbaum 
31110a586ceaSMark Shellenbaum static int
31120a586ceaSMark Shellenbaum zfs_acl_node_aces_walk_init_common(mdb_walk_state_t *wsp, int version)
31130a586ceaSMark Shellenbaum {
31140a586ceaSMark Shellenbaum 	static int gotid;
31150a586ceaSMark Shellenbaum 	static mdb_ctf_id_t acl_id;
31160a586ceaSMark Shellenbaum 	int z_ace_count;
31170a586ceaSMark Shellenbaum 	uintptr_t z_acldata;
31180a586ceaSMark Shellenbaum 
31190a586ceaSMark Shellenbaum 	if (!gotid) {
31200a586ceaSMark Shellenbaum 		if (mdb_ctf_lookup_by_name("struct zfs_acl_node",
31210a586ceaSMark Shellenbaum 		    &acl_id) == -1) {
31220a586ceaSMark Shellenbaum 			mdb_warn("couldn't find struct zfs_acl_node");
31230a586ceaSMark Shellenbaum 			return (DCMD_ERR);
31240a586ceaSMark Shellenbaum 		}
31250a586ceaSMark Shellenbaum 		gotid = TRUE;
31260a586ceaSMark Shellenbaum 	}
31270a586ceaSMark Shellenbaum 
31280a586ceaSMark Shellenbaum 	if (GETMEMBID(wsp->walk_addr, &acl_id, z_ace_count, z_ace_count)) {
31290a586ceaSMark Shellenbaum 		return (DCMD_ERR);
31300a586ceaSMark Shellenbaum 	}
31310a586ceaSMark Shellenbaum 	if (GETMEMBID(wsp->walk_addr, &acl_id, z_acldata, z_acldata)) {
31320a586ceaSMark Shellenbaum 		return (DCMD_ERR);
31330a586ceaSMark Shellenbaum 	}
31340a586ceaSMark Shellenbaum 
31350a586ceaSMark Shellenbaum 	return (zfs_aces_walk_init_common(wsp, version,
31360a586ceaSMark Shellenbaum 	    z_ace_count, z_acldata));
31370a586ceaSMark Shellenbaum }
31380a586ceaSMark Shellenbaum 
31390a586ceaSMark Shellenbaum /* ARGSUSED */
31400a586ceaSMark Shellenbaum static int
31410a586ceaSMark Shellenbaum zfs_acl_node_aces_walk_init(mdb_walk_state_t *wsp)
31420a586ceaSMark Shellenbaum {
31430a586ceaSMark Shellenbaum 	return (zfs_acl_node_aces_walk_init_common(wsp, 1));
31440a586ceaSMark Shellenbaum }
31450a586ceaSMark Shellenbaum 
31460a586ceaSMark Shellenbaum /* ARGSUSED */
31470a586ceaSMark Shellenbaum static int
31480a586ceaSMark Shellenbaum zfs_acl_node_aces0_walk_init(mdb_walk_state_t *wsp)
31490a586ceaSMark Shellenbaum {
31500a586ceaSMark Shellenbaum 	return (zfs_acl_node_aces_walk_init_common(wsp, 0));
31510a586ceaSMark Shellenbaum }
31520a586ceaSMark Shellenbaum 
31530a586ceaSMark Shellenbaum static int
31540a586ceaSMark Shellenbaum zfs_aces_walk_step(mdb_walk_state_t *wsp)
31550a586ceaSMark Shellenbaum {
31560a586ceaSMark Shellenbaum 	ace_walk_data_t *ace_data = wsp->walk_data;
31570a586ceaSMark Shellenbaum 	zfs_ace_t zace;
31580a586ceaSMark Shellenbaum 	ace_t *acep;
31590a586ceaSMark Shellenbaum 	int status;
31600a586ceaSMark Shellenbaum 	int entry_type;
31610a586ceaSMark Shellenbaum 	int allow_type;
31620a586ceaSMark Shellenbaum 	uintptr_t ptr;
31630a586ceaSMark Shellenbaum 
31640a586ceaSMark Shellenbaum 	if (ace_data->ace_count == 0)
31650a586ceaSMark Shellenbaum 		return (WALK_DONE);
31660a586ceaSMark Shellenbaum 
31670a586ceaSMark Shellenbaum 	if (mdb_vread(&zace, sizeof (zfs_ace_t), wsp->walk_addr) == -1) {
31680a586ceaSMark Shellenbaum 		mdb_warn("failed to read zfs_ace_t at %#lx",
31690a586ceaSMark Shellenbaum 		    wsp->walk_addr);
31700a586ceaSMark Shellenbaum 		return (WALK_ERR);
31710a586ceaSMark Shellenbaum 	}
31720a586ceaSMark Shellenbaum 
31730a586ceaSMark Shellenbaum 	switch (ace_data->ace_version) {
31740a586ceaSMark Shellenbaum 	case 0:
31750a586ceaSMark Shellenbaum 		acep = (ace_t *)&zace;
31760a586ceaSMark Shellenbaum 		entry_type = acep->a_flags & ACE_TYPE_FLAGS;
31770a586ceaSMark Shellenbaum 		allow_type = acep->a_type;
31780a586ceaSMark Shellenbaum 		break;
31790a586ceaSMark Shellenbaum 	case 1:
31800a586ceaSMark Shellenbaum 		entry_type = zace.z_hdr.z_flags & ACE_TYPE_FLAGS;
31810a586ceaSMark Shellenbaum 		allow_type = zace.z_hdr.z_type;
31820a586ceaSMark Shellenbaum 		break;
31830a586ceaSMark Shellenbaum 	default:
31840a586ceaSMark Shellenbaum 		return (WALK_ERR);
31850a586ceaSMark Shellenbaum 	}
31860a586ceaSMark Shellenbaum 
31870a586ceaSMark Shellenbaum 	ptr = (uintptr_t)wsp->walk_addr;
31880a586ceaSMark Shellenbaum 	switch (entry_type) {
31890a586ceaSMark Shellenbaum 	case ACE_OWNER:
31900a586ceaSMark Shellenbaum 	case ACE_EVERYONE:
31910a586ceaSMark Shellenbaum 	case (ACE_IDENTIFIER_GROUP | ACE_GROUP):
31920a586ceaSMark Shellenbaum 		ptr += ace_data->ace_version == 0 ?
31930a586ceaSMark Shellenbaum 		    sizeof (ace_t) : sizeof (zfs_ace_hdr_t);
31940a586ceaSMark Shellenbaum 		break;
31950a586ceaSMark Shellenbaum 	case ACE_IDENTIFIER_GROUP:
31960a586ceaSMark Shellenbaum 	default:
31970a586ceaSMark Shellenbaum 		switch (allow_type) {
31980a586ceaSMark Shellenbaum 		case ACE_ACCESS_ALLOWED_OBJECT_ACE_TYPE:
31990a586ceaSMark Shellenbaum 		case ACE_ACCESS_DENIED_OBJECT_ACE_TYPE:
32000a586ceaSMark Shellenbaum 		case ACE_SYSTEM_AUDIT_OBJECT_ACE_TYPE:
32010a586ceaSMark Shellenbaum 		case ACE_SYSTEM_ALARM_OBJECT_ACE_TYPE:
32020a586ceaSMark Shellenbaum 			ptr += ace_data->ace_version == 0 ?
32030a586ceaSMark Shellenbaum 			    sizeof (ace_t) : sizeof (zfs_object_ace_t);
32040a586ceaSMark Shellenbaum 			break;
32050a586ceaSMark Shellenbaum 		default:
32060a586ceaSMark Shellenbaum 			ptr += ace_data->ace_version == 0 ?
32070a586ceaSMark Shellenbaum 			    sizeof (ace_t) : sizeof (zfs_ace_t);
32080a586ceaSMark Shellenbaum 			break;
32090a586ceaSMark Shellenbaum 		}
32100a586ceaSMark Shellenbaum 	}
32110a586ceaSMark Shellenbaum 
32120a586ceaSMark Shellenbaum 	ace_data->ace_count--;
32130a586ceaSMark Shellenbaum 	status = wsp->walk_callback(wsp->walk_addr,
32140a586ceaSMark Shellenbaum 	    (void *)(uintptr_t)&zace, wsp->walk_cbdata);
32150a586ceaSMark Shellenbaum 
32160a586ceaSMark Shellenbaum 	wsp->walk_addr = ptr;
32170a586ceaSMark Shellenbaum 	return (status);
32180a586ceaSMark Shellenbaum }
32190a586ceaSMark Shellenbaum 
322028e4da25SMatthew Ahrens typedef struct mdb_zfs_rrwlock {
3221d5ee8a13SMatthew Ahrens 	uintptr_t	rr_writer;
322228e4da25SMatthew Ahrens 	boolean_t	rr_writer_wanted;
322328e4da25SMatthew Ahrens } mdb_zfs_rrwlock_t;
322428e4da25SMatthew Ahrens 
322528e4da25SMatthew Ahrens static uint_t rrw_key;
322628e4da25SMatthew Ahrens 
322728e4da25SMatthew Ahrens /* ARGSUSED */
322828e4da25SMatthew Ahrens static int
322928e4da25SMatthew Ahrens rrwlock(uintptr_t addr, uint_t flags, int argc, const mdb_arg_t *argv)
323028e4da25SMatthew Ahrens {
323128e4da25SMatthew Ahrens 	mdb_zfs_rrwlock_t rrw;
323228e4da25SMatthew Ahrens 
323328e4da25SMatthew Ahrens 	if (rrw_key == 0) {
323428e4da25SMatthew Ahrens 		if (mdb_ctf_readsym(&rrw_key, "uint_t", "rrw_tsd_key", 0) == -1)
323528e4da25SMatthew Ahrens 			return (DCMD_ERR);
323628e4da25SMatthew Ahrens 	}
323728e4da25SMatthew Ahrens 
323828e4da25SMatthew Ahrens 	if (mdb_ctf_vread(&rrw, "rrwlock_t", "mdb_zfs_rrwlock_t", addr,
323928e4da25SMatthew Ahrens 	    0) == -1)
324028e4da25SMatthew Ahrens 		return (DCMD_ERR);
324128e4da25SMatthew Ahrens 
3242d5ee8a13SMatthew Ahrens 	if (rrw.rr_writer != 0) {
3243d5ee8a13SMatthew Ahrens 		mdb_printf("write lock held by thread %lx\n", rrw.rr_writer);
324428e4da25SMatthew Ahrens 		return (DCMD_OK);
324528e4da25SMatthew Ahrens 	}
324628e4da25SMatthew Ahrens 
324728e4da25SMatthew Ahrens 	if (rrw.rr_writer_wanted) {
324828e4da25SMatthew Ahrens 		mdb_printf("writer wanted\n");
324928e4da25SMatthew Ahrens 	}
325028e4da25SMatthew Ahrens 
325128e4da25SMatthew Ahrens 	mdb_printf("anonymous references:\n");
325228e4da25SMatthew Ahrens 	(void) mdb_call_dcmd("refcount", addr +
325328e4da25SMatthew Ahrens 	    mdb_ctf_offsetof_by_name(ZFS_STRUCT "rrwlock", "rr_anon_rcount"),
325428e4da25SMatthew Ahrens 	    DCMD_ADDRSPEC, 0, NULL);
325528e4da25SMatthew Ahrens 
325628e4da25SMatthew Ahrens 	mdb_printf("linked references:\n");
325728e4da25SMatthew Ahrens 	(void) mdb_call_dcmd("refcount", addr +
325828e4da25SMatthew Ahrens 	    mdb_ctf_offsetof_by_name(ZFS_STRUCT "rrwlock", "rr_linked_rcount"),
325928e4da25SMatthew Ahrens 	    DCMD_ADDRSPEC, 0, NULL);
326028e4da25SMatthew Ahrens 
326128e4da25SMatthew Ahrens 	/*
326228e4da25SMatthew Ahrens 	 * XXX This should find references from
326328e4da25SMatthew Ahrens 	 * "::walk thread | ::tsd -v <rrw_key>", but there is no support
326428e4da25SMatthew Ahrens 	 * for programmatic consumption of dcmds, so this would be
326528e4da25SMatthew Ahrens 	 * difficult, potentially requiring reimplementing ::tsd (both
326628e4da25SMatthew Ahrens 	 * user and kernel versions) in this MDB module.
326728e4da25SMatthew Ahrens 	 */
326828e4da25SMatthew Ahrens 
326928e4da25SMatthew Ahrens 	return (DCMD_OK);
327028e4da25SMatthew Ahrens }
327128e4da25SMatthew Ahrens 
3272fa9e4066Sahrens /*
3273fa9e4066Sahrens  * MDB module linkage information:
3274fa9e4066Sahrens  *
3275fa9e4066Sahrens  * We declare a list of structures describing our dcmds, and a function
3276fa9e4066Sahrens  * named _mdb_init to return a pointer to our module information.
3277fa9e4066Sahrens  */
3278fa9e4066Sahrens 
3279fa9e4066Sahrens static const mdb_dcmd_t dcmds[] = {
328091ebeef5Sahrens 	{ "arc", "[-bkmg]", "print ARC variables", arc_print },
3281fa9e4066Sahrens 	{ "blkptr", ":", "print blkptr_t", blkptr },
3282fa9e4066Sahrens 	{ "dbuf", ":", "print dmu_buf_impl_t", dbuf },
3283fa9e4066Sahrens 	{ "dbuf_stats", ":", "dbuf stats", dbuf_stats },
3284fa9e4066Sahrens 	{ "dbufs",
32854223fc7cSMark Shellenbaum 	    "\t[-O objset_t*] [-n objset_name | \"mos\"] "
3286a3f829aeSBill Moore 	    "[-o object | \"mdn\"] \n"
3287a3f829aeSBill Moore 	    "\t[-l level] [-b blkid | \"bonus\"]",
3288a3f829aeSBill Moore 	    "find dmu_buf_impl_t's that match specified criteria", dbufs },
3289fa9e4066Sahrens 	{ "abuf_find", "dva_word[0] dva_word[1]",
3290a3f829aeSBill Moore 	    "find arc_buf_hdr_t of a specified DVA",
3291a3f829aeSBill Moore 	    abuf_find },
32922e4c9986SGeorge Wilson 	{ "spa", "?[-cevmMh]\n"
32932e4c9986SGeorge Wilson 	    "\t-c display spa config\n"
32942e4c9986SGeorge Wilson 	    "\t-e display vdev statistics\n"
32952e4c9986SGeorge Wilson 	    "\t-v display vdev information\n"
32962e4c9986SGeorge Wilson 	    "\t-m display metaslab statistics\n"
32972e4c9986SGeorge Wilson 	    "\t-M display metaslab group statistics\n"
32982e4c9986SGeorge Wilson 	    "\t-h display histogram (requires -m or -M)\n",
32992e4c9986SGeorge Wilson 	    "spa_t summary", spa_print },
3300fa9e4066Sahrens 	{ "spa_config", ":", "print spa_t configuration", spa_print_config },
3301fa9e4066Sahrens 	{ "spa_space", ":[-b]", "print spa_t on-disk space usage", spa_space },
33022e4c9986SGeorge Wilson 	{ "spa_vdevs", ":[-emMh]\n"
33032e4c9986SGeorge Wilson 	    "\t-e display vdev statistics\n"
33042e4c9986SGeorge Wilson 	    "\t-m dispaly metaslab statistics\n"
33052e4c9986SGeorge Wilson 	    "\t-M display metaslab group statistic\n"
33062e4c9986SGeorge Wilson 	    "\t-h display histogram (requires -m or -M)\n",
33072e4c9986SGeorge Wilson 	    "given a spa_t, print vdev summary", spa_vdevs },
330891ebeef5Sahrens 	{ "vdev", ":[-re]\n"
3309a3f829aeSBill Moore 	    "\t-r display recursively\n"
33102e4c9986SGeorge Wilson 	    "\t-e display statistics\n"
33112e4c9986SGeorge Wilson 	    "\t-m display metaslab statistics\n"
33122e4c9986SGeorge Wilson 	    "\t-M display metaslab group statistics\n"
33132e4c9986SGeorge Wilson 	    "\t-h display histogram (requires -m or -M)\n",
3314a3f829aeSBill Moore 	    "vdev_t summary", vdev_print },
33152e4c9986SGeorge Wilson 	{ "zio", ":[-cpr]\n"
3316a3f829aeSBill Moore 	    "\t-c display children\n"
3317a3f829aeSBill Moore 	    "\t-p display parents\n"
3318a3f829aeSBill Moore 	    "\t-r display recursively",
3319a3f829aeSBill Moore 	    "zio_t summary", zio_print },
3320ccae0b50Seschrock 	{ "zio_state", "?", "print out all zio_t structures on system or "
3321ccae0b50Seschrock 	    "for a particular pool", zio_state },
332288b7b0f2SMatthew Ahrens 	{ "zfs_blkstats", ":[-v]",
332388b7b0f2SMatthew Ahrens 	    "given a spa_t, print block type stats from last scrub",
332488b7b0f2SMatthew Ahrens 	    zfs_blkstats },
3325614409b5Sahrens 	{ "zfs_params", "", "print zfs tunable parameters", zfs_params },
332628e4da25SMatthew Ahrens 	{ "refcount", ":[-r]\n"
332728e4da25SMatthew Ahrens 	    "\t-r display recently removed references",
332828e4da25SMatthew Ahrens 	    "print refcount_t holders", refcount },
33293f1f8012SMatthew Ahrens 	{ "zap_leaf", "", "print zap_leaf_phys_t", zap_leaf },
33300a586ceaSMark Shellenbaum 	{ "zfs_aces", ":[-v]", "print all ACEs from a zfs_acl_t",
33310a586ceaSMark Shellenbaum 	    zfs_acl_dump },
33320a586ceaSMark Shellenbaum 	{ "zfs_ace", ":[-v]", "print zfs_ace", zfs_ace_print },
33330a586ceaSMark Shellenbaum 	{ "zfs_ace0", ":[-v]", "print zfs_ace0", zfs_ace0_print },
33340a586ceaSMark Shellenbaum 	{ "sa_attr_table", ":", "print SA attribute table from sa_os_t",
33350a586ceaSMark Shellenbaum 	    sa_attr_table},
33360a586ceaSMark Shellenbaum 	{ "sa_attr", ": attr_id",
33370a586ceaSMark Shellenbaum 	    "print SA attribute address when given sa_handle_t", sa_attr_print},
333828e4da25SMatthew Ahrens 	{ "zfs_dbgmsg", ":[-va]",
33393f9d6ad7SLin Ling 	    "print zfs debug log", dbgmsg},
334028e4da25SMatthew Ahrens 	{ "rrwlock", ":",
334128e4da25SMatthew Ahrens 	    "print rrwlock_t, including readers", rrwlock},
3342fa9e4066Sahrens 	{ NULL }
3343fa9e4066Sahrens };
3344fa9e4066Sahrens 
3345fa9e4066Sahrens static const mdb_walker_t walkers[] = {
3346fa9e4066Sahrens 	{ "zms_freelist", "walk ZFS metaslab freelist",
334728e4da25SMatthew Ahrens 	    freelist_walk_init, freelist_walk_step, NULL },
3348fa9e4066Sahrens 	{ "txg_list", "given any txg_list_t *, walk all entries in all txgs",
334928e4da25SMatthew Ahrens 	    txg_list_walk_init, txg_list_walk_step, NULL },
3350fa9e4066Sahrens 	{ "txg_list0", "given any txg_list_t *, walk all entries in txg 0",
335128e4da25SMatthew Ahrens 	    txg_list0_walk_init, txg_list_walk_step, NULL },
3352fa9e4066Sahrens 	{ "txg_list1", "given any txg_list_t *, walk all entries in txg 1",
335328e4da25SMatthew Ahrens 	    txg_list1_walk_init, txg_list_walk_step, NULL },
3354fa9e4066Sahrens 	{ "txg_list2", "given any txg_list_t *, walk all entries in txg 2",
335528e4da25SMatthew Ahrens 	    txg_list2_walk_init, txg_list_walk_step, NULL },
3356fa9e4066Sahrens 	{ "txg_list3", "given any txg_list_t *, walk all entries in txg 3",
335728e4da25SMatthew Ahrens 	    txg_list3_walk_init, txg_list_walk_step, NULL },
3358ccae0b50Seschrock 	{ "zio", "walk all zio structures, optionally for a particular spa_t",
335928e4da25SMatthew Ahrens 	    zio_walk_init, zio_walk_step, NULL },
336028e4da25SMatthew Ahrens 	{ "zio_root",
336128e4da25SMatthew Ahrens 	    "walk all root zio_t structures, optionally for a particular spa_t",
336228e4da25SMatthew Ahrens 	    zio_walk_init, zio_walk_root_step, NULL },
3363fa9e4066Sahrens 	{ "spa", "walk all spa_t entries in the namespace",
336428e4da25SMatthew Ahrens 	    spa_walk_init, spa_walk_step, NULL },
33655f5f7a6fSahrens 	{ "metaslab", "given a spa_t *, walk all metaslab_t structures",
336628e4da25SMatthew Ahrens 	    metaslab_walk_init, metaslab_walk_step, NULL },
33670a586ceaSMark Shellenbaum 	{ "zfs_acl_node", "given a zfs_acl_t, walk all zfs_acl_nodes",
33680a586ceaSMark Shellenbaum 	    zfs_acl_node_walk_init, zfs_acl_node_walk_step, NULL },
33690a586ceaSMark Shellenbaum 	{ "zfs_acl_node_aces", "given a zfs_acl_node_t, walk all ACEs",
33700a586ceaSMark Shellenbaum 	    zfs_acl_node_aces_walk_init, zfs_aces_walk_step, NULL },
33710a586ceaSMark Shellenbaum 	{ "zfs_acl_node_aces0",
33720a586ceaSMark Shellenbaum 	    "given a zfs_acl_node_t, walk all ACEs as ace_t",
33730a586ceaSMark Shellenbaum 	    zfs_acl_node_aces0_walk_init, zfs_aces_walk_step, NULL },
3374fa9e4066Sahrens 	{ NULL }
3375fa9e4066Sahrens };
3376fa9e4066Sahrens 
3377fa9e4066Sahrens static const mdb_modinfo_t modinfo = {
3378fa9e4066Sahrens 	MDB_API_VERSION, dcmds, walkers
3379fa9e4066Sahrens };
3380fa9e4066Sahrens 
3381fa9e4066Sahrens const mdb_modinfo_t *
3382fa9e4066Sahrens _mdb_init(void)
3383fa9e4066Sahrens {
3384fa9e4066Sahrens 	return (&modinfo);
3385fa9e4066Sahrens }
3386