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