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