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