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