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