xref: /illumos-gate/usr/src/cmd/mdb/common/modules/zfs/zfs.c (revision 201ceb75ab95f9bf1f42ea1dc9ab363b43ba47cf)
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, 2018 by Delphix. All rights reserved.
25  * Copyright 2020 Joyent, Inc.
26  */
27 
28 /* Portions Copyright 2010 Robert Milkowski */
29 
30 /*
31  * ZFS_MDB lets dmu.h know that we don't have dmu_ot, and we will define our
32  * own macros to access the target's dmu_ot.  Therefore it must be defined
33  * before including any ZFS headers.  Note that we don't define
34  * DMU_OT_IS_ENCRYPTED_IMPL() or DMU_OT_BYTESWAP_IMPL(), therefore using them
35  * will result in a compilation error.  If they are needed in the future, we
36  * can implement them similarly to mdb_dmu_ot_is_encrypted_impl().
37  */
38 #define	ZFS_MDB
39 #define	DMU_OT_IS_ENCRYPTED_IMPL(ot) mdb_dmu_ot_is_encrypted_impl(ot)
40 
41 #include <mdb/mdb_ctf.h>
42 #include <sys/zfs_context.h>
43 #include <sys/mdb_modapi.h>
44 #include <sys/dbuf.h>
45 #include <sys/dmu_objset.h>
46 #include <sys/dsl_dir.h>
47 #include <sys/dsl_pool.h>
48 #include <sys/metaslab_impl.h>
49 #include <sys/space_map.h>
50 #include <sys/list.h>
51 #include <sys/vdev_impl.h>
52 #include <sys/zap_leaf.h>
53 #include <sys/zap_impl.h>
54 #include <ctype.h>
55 #include <sys/zfs_acl.h>
56 #include <sys/sa_impl.h>
57 #include <sys/multilist.h>
58 #include <sys/btree.h>
59 
60 #ifdef _KERNEL
61 #define	ZFS_OBJ_NAME	"zfs"
62 extern int64_t mdb_gethrtime(void);
63 #else
64 #define	ZFS_OBJ_NAME	"libzpool.so.1"
65 #endif
66 
67 #define	ZFS_STRUCT	"struct " ZFS_OBJ_NAME "`"
68 
69 #ifndef _KERNEL
70 int aok;
71 #endif
72 
73 enum spa_flags {
74 	SPA_FLAG_CONFIG			= 1 << 0,
75 	SPA_FLAG_VDEVS			= 1 << 1,
76 	SPA_FLAG_ERRORS			= 1 << 2,
77 	SPA_FLAG_METASLAB_GROUPS	= 1 << 3,
78 	SPA_FLAG_METASLABS		= 1 << 4,
79 	SPA_FLAG_HISTOGRAMS		= 1 << 5
80 };
81 
82 /*
83  * If any of these flags are set, call spa_vdevs in spa_print
84  */
85 #define	SPA_FLAG_ALL_VDEV	\
86 	(SPA_FLAG_VDEVS | SPA_FLAG_ERRORS | SPA_FLAG_METASLAB_GROUPS | \
87 	SPA_FLAG_METASLABS)
88 
89 static int
90 getmember(uintptr_t addr, const char *type, mdb_ctf_id_t *idp,
91     const char *member, int len, void *buf)
92 {
93 	mdb_ctf_id_t id;
94 	ulong_t off;
95 	char name[64];
96 
97 	if (idp == NULL) {
98 		if (mdb_ctf_lookup_by_name(type, &id) == -1) {
99 			mdb_warn("couldn't find type %s", type);
100 			return (DCMD_ERR);
101 		}
102 		idp = &id;
103 	} else {
104 		type = name;
105 		mdb_ctf_type_name(*idp, name, sizeof (name));
106 	}
107 
108 	if (mdb_ctf_offsetof(*idp, member, &off) == -1) {
109 		mdb_warn("couldn't find member %s of type %s\n", member, type);
110 		return (DCMD_ERR);
111 	}
112 	if (off % 8 != 0) {
113 		mdb_warn("member %s of type %s is unsupported bitfield",
114 		    member, type);
115 		return (DCMD_ERR);
116 	}
117 	off /= 8;
118 
119 	if (mdb_vread(buf, len, addr + off) == -1) {
120 		mdb_warn("failed to read %s from %s at %p",
121 		    member, type, addr + off);
122 		return (DCMD_ERR);
123 	}
124 	/* mdb_warn("read %s from %s at %p+%llx\n", member, type, addr, off); */
125 
126 	return (0);
127 }
128 
129 #define	GETMEMB(addr, structname, member, dest) \
130 	getmember(addr, ZFS_STRUCT structname, NULL, #member, \
131 	sizeof (dest), &(dest))
132 
133 #define	GETMEMBID(addr, ctfid, member, dest) \
134 	getmember(addr, NULL, ctfid, #member, sizeof (dest), &(dest))
135 
136 static boolean_t
137 strisprint(const char *cp)
138 {
139 	for (; *cp; cp++) {
140 		if (!isprint(*cp))
141 			return (B_FALSE);
142 	}
143 	return (B_TRUE);
144 }
145 
146 /*
147  * <addr>::sm_entries <buffer length in bytes>
148  *
149  * Treat the buffer specified by the given address as a buffer that contains
150  * space map entries. Iterate over the specified number of entries and print
151  * them in both encoded and decoded form.
152  */
153 /* ARGSUSED */
154 static int
155 sm_entries(uintptr_t addr, uint_t flags, int argc, const mdb_arg_t *argv)
156 {
157 	uint64_t bufsz = 0;
158 	boolean_t preview = B_FALSE;
159 
160 	if (!(flags & DCMD_ADDRSPEC))
161 		return (DCMD_USAGE);
162 
163 	if (argc < 1) {
164 		preview = B_TRUE;
165 		bufsz = 2;
166 	} else if (argc != 1) {
167 		return (DCMD_USAGE);
168 	} else {
169 		switch (argv[0].a_type) {
170 		case MDB_TYPE_STRING:
171 			bufsz = mdb_strtoull(argv[0].a_un.a_str);
172 			break;
173 		case MDB_TYPE_IMMEDIATE:
174 			bufsz = argv[0].a_un.a_val;
175 			break;
176 		default:
177 			return (DCMD_USAGE);
178 		}
179 	}
180 
181 	char *actions[] = { "ALLOC", "FREE", "INVALID" };
182 	for (uintptr_t bufend = addr + bufsz; addr < bufend;
183 	    addr += sizeof (uint64_t)) {
184 		uint64_t nwords;
185 		uint64_t start_addr = addr;
186 
187 		uint64_t word = 0;
188 		if (mdb_vread(&word, sizeof (word), addr) == -1) {
189 			mdb_warn("failed to read space map entry %p", addr);
190 			return (DCMD_ERR);
191 		}
192 
193 		if (SM_PREFIX_DECODE(word) == SM_DEBUG_PREFIX) {
194 			(void) mdb_printf("\t    [%6llu] %s: txg %llu, "
195 			    "pass %llu\n",
196 			    (u_longlong_t)(addr),
197 			    actions[SM_DEBUG_ACTION_DECODE(word)],
198 			    (u_longlong_t)SM_DEBUG_TXG_DECODE(word),
199 			    (u_longlong_t)SM_DEBUG_SYNCPASS_DECODE(word));
200 			continue;
201 		}
202 
203 		char entry_type;
204 		uint64_t raw_offset, raw_run, vdev_id = SM_NO_VDEVID;
205 
206 		if (SM_PREFIX_DECODE(word) != SM2_PREFIX) {
207 			entry_type = (SM_TYPE_DECODE(word) == SM_ALLOC) ?
208 			    'A' : 'F';
209 			raw_offset = SM_OFFSET_DECODE(word);
210 			raw_run = SM_RUN_DECODE(word);
211 			nwords = 1;
212 		} else {
213 			ASSERT3U(SM_PREFIX_DECODE(word), ==, SM2_PREFIX);
214 
215 			raw_run = SM2_RUN_DECODE(word);
216 			vdev_id = SM2_VDEV_DECODE(word);
217 
218 			/* it is a two-word entry so we read another word */
219 			addr += sizeof (uint64_t);
220 			if (addr >= bufend) {
221 				mdb_warn("buffer ends in the middle of a two "
222 				    "word entry\n", addr);
223 				return (DCMD_ERR);
224 			}
225 
226 			if (mdb_vread(&word, sizeof (word), addr) == -1) {
227 				mdb_warn("failed to read space map entry %p",
228 				    addr);
229 				return (DCMD_ERR);
230 			}
231 
232 			entry_type = (SM2_TYPE_DECODE(word) == SM_ALLOC) ?
233 			    'A' : 'F';
234 			raw_offset = SM2_OFFSET_DECODE(word);
235 			nwords = 2;
236 		}
237 
238 		(void) mdb_printf("\t    [%6llx]    %c  range:"
239 		    " %010llx-%010llx  size: %06llx vdev: %06llu words: %llu\n",
240 		    (u_longlong_t)start_addr,
241 		    entry_type, (u_longlong_t)raw_offset,
242 		    (u_longlong_t)(raw_offset + raw_run),
243 		    (u_longlong_t)raw_run,
244 		    (u_longlong_t)vdev_id, (u_longlong_t)nwords);
245 
246 		if (preview)
247 			break;
248 	}
249 	return (DCMD_OK);
250 }
251 
252 static int
253 mdb_dsl_dir_name(uintptr_t addr, char *buf)
254 {
255 	static int gotid;
256 	static mdb_ctf_id_t dd_id;
257 	uintptr_t dd_parent;
258 	char dd_myname[ZFS_MAX_DATASET_NAME_LEN];
259 
260 	if (!gotid) {
261 		if (mdb_ctf_lookup_by_name(ZFS_STRUCT "dsl_dir",
262 		    &dd_id) == -1) {
263 			mdb_warn("couldn't find struct dsl_dir");
264 			return (DCMD_ERR);
265 		}
266 		gotid = TRUE;
267 	}
268 	if (GETMEMBID(addr, &dd_id, dd_parent, dd_parent) ||
269 	    GETMEMBID(addr, &dd_id, dd_myname, dd_myname)) {
270 		return (DCMD_ERR);
271 	}
272 
273 	if (dd_parent) {
274 		if (mdb_dsl_dir_name(dd_parent, buf))
275 			return (DCMD_ERR);
276 		strcat(buf, "/");
277 	}
278 
279 	if (dd_myname[0])
280 		strcat(buf, dd_myname);
281 	else
282 		strcat(buf, "???");
283 
284 	return (0);
285 }
286 
287 static int
288 objset_name(uintptr_t addr, char *buf)
289 {
290 	static int gotid;
291 	static mdb_ctf_id_t os_id, ds_id;
292 	uintptr_t os_dsl_dataset;
293 	char ds_snapname[ZFS_MAX_DATASET_NAME_LEN];
294 	uintptr_t ds_dir;
295 
296 	buf[0] = '\0';
297 
298 	if (!gotid) {
299 		if (mdb_ctf_lookup_by_name(ZFS_STRUCT "objset",
300 		    &os_id) == -1) {
301 			mdb_warn("couldn't find struct objset");
302 			return (DCMD_ERR);
303 		}
304 		if (mdb_ctf_lookup_by_name(ZFS_STRUCT "dsl_dataset",
305 		    &ds_id) == -1) {
306 			mdb_warn("couldn't find struct dsl_dataset");
307 			return (DCMD_ERR);
308 		}
309 
310 		gotid = TRUE;
311 	}
312 
313 	if (GETMEMBID(addr, &os_id, os_dsl_dataset, os_dsl_dataset))
314 		return (DCMD_ERR);
315 
316 	if (os_dsl_dataset == 0) {
317 		strcat(buf, "mos");
318 		return (0);
319 	}
320 
321 	if (GETMEMBID(os_dsl_dataset, &ds_id, ds_snapname, ds_snapname) ||
322 	    GETMEMBID(os_dsl_dataset, &ds_id, ds_dir, ds_dir)) {
323 		return (DCMD_ERR);
324 	}
325 
326 	if (ds_dir && mdb_dsl_dir_name(ds_dir, buf))
327 		return (DCMD_ERR);
328 
329 	if (ds_snapname[0]) {
330 		strcat(buf, "@");
331 		strcat(buf, ds_snapname);
332 	}
333 	return (0);
334 }
335 
336 static int
337 enum_lookup(char *type, int val, const char *prefix, size_t size, char *out)
338 {
339 	const char *cp;
340 	size_t len = strlen(prefix);
341 	mdb_ctf_id_t enum_type;
342 
343 	if (mdb_ctf_lookup_by_name(type, &enum_type) != 0) {
344 		mdb_warn("Could not find enum for %s", type);
345 		return (-1);
346 	}
347 
348 	if ((cp = mdb_ctf_enum_name(enum_type, val)) != NULL) {
349 		if (strncmp(cp, prefix, len) == 0)
350 			cp += len;
351 		(void) strncpy(out, cp, size);
352 	} else {
353 		mdb_snprintf(out, size, "? (%d)", val);
354 	}
355 	return (0);
356 }
357 
358 /* ARGSUSED */
359 static int
360 zfs_params(uintptr_t addr, uint_t flags, int argc, const mdb_arg_t *argv)
361 {
362 	/*
363 	 * This table can be approximately generated by running:
364 	 * egrep "^[a-z0-9_]+ [a-z0-9_]+( =.*)?;" *.c | cut -d ' ' -f 2
365 	 */
366 	static const char *params[] = {
367 		"arc_lotsfree_percent",
368 		"arc_pages_pp_reserve",
369 		"arc_reduce_dnlc_percent",
370 		"arc_swapfs_reserve",
371 		"arc_zio_arena_free_shift",
372 		"dbuf_cache_hiwater_pct",
373 		"dbuf_cache_lowater_pct",
374 		"dbuf_cache_max_bytes",
375 		"dbuf_cache_max_shift",
376 		"ddt_zap_indirect_blockshift",
377 		"ddt_zap_leaf_blockshift",
378 		"ditto_same_vdev_distance_shift",
379 		"dmu_find_threads",
380 		"dmu_rescan_dnode_threshold",
381 		"dsl_scan_delay_completion",
382 		"fzap_default_block_shift",
383 		"l2arc_feed_again",
384 		"l2arc_feed_min_ms",
385 		"l2arc_feed_secs",
386 		"l2arc_headroom",
387 		"l2arc_headroom_boost",
388 		"l2arc_noprefetch",
389 		"l2arc_norw",
390 		"l2arc_write_boost",
391 		"l2arc_write_max",
392 		"metaslab_aliquot",
393 		"metaslab_bias_enabled",
394 		"metaslab_debug_load",
395 		"metaslab_debug_unload",
396 		"metaslab_df_alloc_threshold",
397 		"metaslab_df_free_pct",
398 		"metaslab_fragmentation_factor_enabled",
399 		"metaslab_force_ganging",
400 		"metaslab_lba_weighting_enabled",
401 		"metaslab_load_pct",
402 		"metaslab_min_alloc_size",
403 		"metaslab_ndf_clump_shift",
404 		"metaslab_preload_enabled",
405 		"metaslab_preload_limit",
406 		"metaslab_trace_enabled",
407 		"metaslab_trace_max_entries",
408 		"metaslab_unload_delay",
409 		"metaslabs_per_vdev",
410 		"reference_history",
411 		"reference_tracking_enable",
412 		"send_holes_without_birth_time",
413 		"spa_asize_inflation",
414 		"spa_load_verify_data",
415 		"spa_load_verify_maxinflight",
416 		"spa_load_verify_metadata",
417 		"spa_max_replication_override",
418 		"spa_min_slop",
419 		"spa_mode_global",
420 		"spa_slop_shift",
421 		"space_map_blksz",
422 		"vdev_mirror_shift",
423 		"zfetch_max_distance",
424 		"zfs_abd_chunk_size",
425 		"zfs_abd_scatter_enabled",
426 		"zfs_arc_average_blocksize",
427 		"zfs_arc_evict_batch_limit",
428 		"zfs_arc_grow_retry",
429 		"zfs_arc_max",
430 		"zfs_arc_meta_limit",
431 		"zfs_arc_meta_min",
432 		"zfs_arc_min",
433 		"zfs_arc_p_min_shift",
434 		"zfs_arc_shrink_shift",
435 		"zfs_async_block_max_blocks",
436 		"zfs_ccw_retry_interval",
437 		"zfs_commit_timeout_pct",
438 		"zfs_compressed_arc_enabled",
439 		"zfs_condense_indirect_commit_entry_delay_ticks",
440 		"zfs_condense_indirect_vdevs_enable",
441 		"zfs_condense_max_obsolete_bytes",
442 		"zfs_condense_min_mapping_bytes",
443 		"zfs_condense_pct",
444 		"zfs_dbgmsg_maxsize",
445 		"zfs_deadman_checktime_ms",
446 		"zfs_deadman_enabled",
447 		"zfs_deadman_synctime_ms",
448 		"zfs_dedup_prefetch",
449 		"zfs_default_bs",
450 		"zfs_default_ibs",
451 		"zfs_delay_max_ns",
452 		"zfs_delay_min_dirty_percent",
453 		"zfs_delay_resolution_ns",
454 		"zfs_delay_scale",
455 		"zfs_dirty_data_max",
456 		"zfs_dirty_data_max_max",
457 		"zfs_dirty_data_max_percent",
458 		"zfs_dirty_data_sync",
459 		"zfs_flags",
460 		"zfs_free_bpobj_enabled",
461 		"zfs_free_leak_on_eio",
462 		"zfs_free_min_time_ms",
463 		"zfs_fsync_sync_cnt",
464 		"zfs_immediate_write_sz",
465 		"zfs_indirect_condense_obsolete_pct",
466 		"zfs_lua_check_instrlimit_interval",
467 		"zfs_lua_max_instrlimit",
468 		"zfs_lua_max_memlimit",
469 		"zfs_max_recordsize",
470 		"zfs_mdcomp_disable",
471 		"zfs_metaslab_condense_block_threshold",
472 		"zfs_metaslab_fragmentation_threshold",
473 		"zfs_metaslab_segment_weight_enabled",
474 		"zfs_metaslab_switch_threshold",
475 		"zfs_mg_fragmentation_threshold",
476 		"zfs_mg_noalloc_threshold",
477 		"zfs_multilist_num_sublists",
478 		"zfs_no_scrub_io",
479 		"zfs_no_scrub_prefetch",
480 		"zfs_nocacheflush",
481 		"zfs_nopwrite_enabled",
482 		"zfs_object_remap_one_indirect_delay_ticks",
483 		"zfs_obsolete_min_time_ms",
484 		"zfs_pd_bytes_max",
485 		"zfs_per_txg_dirty_frees_percent",
486 		"zfs_prefetch_disable",
487 		"zfs_read_chunk_size",
488 		"zfs_recover",
489 		"zfs_recv_queue_length",
490 		"zfs_redundant_metadata_most_ditto_level",
491 		"zfs_remap_blkptr_enable",
492 		"zfs_remove_max_copy_bytes",
493 		"zfs_remove_max_segment",
494 		"zfs_resilver_delay",
495 		"zfs_resilver_min_time_ms",
496 		"zfs_scan_idle",
497 		"zfs_scan_min_time_ms",
498 		"zfs_scrub_delay",
499 		"zfs_scrub_limit",
500 		"zfs_send_corrupt_data",
501 		"zfs_send_queue_length",
502 		"zfs_send_set_freerecords_bit",
503 		"zfs_sync_pass_deferred_free",
504 		"zfs_sync_pass_dont_compress",
505 		"zfs_sync_pass_rewrite",
506 		"zfs_sync_taskq_batch_pct",
507 		"zfs_top_maxinflight",
508 		"zfs_txg_timeout",
509 		"zfs_vdev_aggregation_limit",
510 		"zfs_vdev_async_read_max_active",
511 		"zfs_vdev_async_read_min_active",
512 		"zfs_vdev_async_write_active_max_dirty_percent",
513 		"zfs_vdev_async_write_active_min_dirty_percent",
514 		"zfs_vdev_async_write_max_active",
515 		"zfs_vdev_async_write_min_active",
516 		"zfs_vdev_cache_bshift",
517 		"zfs_vdev_cache_max",
518 		"zfs_vdev_cache_size",
519 		"zfs_vdev_max_active",
520 		"zfs_vdev_queue_depth_pct",
521 		"zfs_vdev_read_gap_limit",
522 		"zfs_vdev_removal_max_active",
523 		"zfs_vdev_removal_min_active",
524 		"zfs_vdev_scrub_max_active",
525 		"zfs_vdev_scrub_min_active",
526 		"zfs_vdev_sync_read_max_active",
527 		"zfs_vdev_sync_read_min_active",
528 		"zfs_vdev_sync_write_max_active",
529 		"zfs_vdev_sync_write_min_active",
530 		"zfs_vdev_write_gap_limit",
531 		"zfs_write_implies_delete_child",
532 		"zfs_zil_clean_taskq_maxalloc",
533 		"zfs_zil_clean_taskq_minalloc",
534 		"zfs_zil_clean_taskq_nthr_pct",
535 		"zil_replay_disable",
536 		"zil_slog_bulk",
537 		"zio_buf_debug_limit",
538 		"zio_dva_throttle_enabled",
539 		"zio_injection_enabled",
540 		"zvol_immediate_write_sz",
541 		"zvol_maxphys",
542 		"zvol_unmap_enabled",
543 		"zvol_unmap_sync_enabled",
544 		"zfs_max_dataset_nesting",
545 	};
546 
547 	for (int i = 0; i < sizeof (params) / sizeof (params[0]); i++) {
548 		int sz;
549 		uint64_t val64;
550 		uint32_t *val32p = (uint32_t *)&val64;
551 
552 		sz = mdb_readvar(&val64, params[i]);
553 		if (sz == 4) {
554 			mdb_printf("%s = 0x%x\n", params[i], *val32p);
555 		} else if (sz == 8) {
556 			mdb_printf("%s = 0x%llx\n", params[i], val64);
557 		} else {
558 			mdb_warn("variable %s not found", params[i]);
559 		}
560 	}
561 
562 	return (DCMD_OK);
563 }
564 
565 /* ARGSUSED */
566 static int
567 dva(uintptr_t addr, uint_t flags, int argc, const mdb_arg_t *argv)
568 {
569 	dva_t dva;
570 	if (mdb_vread(&dva, sizeof (dva_t), addr) == -1) {
571 		mdb_warn("failed to read dva_t");
572 		return (DCMD_ERR);
573 	}
574 	mdb_printf("<%llu:%llx:%llx>\n",
575 	    (u_longlong_t)DVA_GET_VDEV(&dva),
576 	    (u_longlong_t)DVA_GET_OFFSET(&dva),
577 	    (u_longlong_t)DVA_GET_ASIZE(&dva));
578 
579 	return (DCMD_OK);
580 }
581 
582 typedef struct mdb_dmu_object_type_info {
583 	boolean_t ot_encrypt;
584 } mdb_dmu_object_type_info_t;
585 
586 static boolean_t
587 mdb_dmu_ot_is_encrypted_impl(dmu_object_type_t ot)
588 {
589 	mdb_dmu_object_type_info_t mdoti;
590 	GElf_Sym sym;
591 	size_t sz = mdb_ctf_sizeof_by_name("dmu_object_type_info_t");
592 
593 	if (mdb_lookup_by_obj(ZFS_OBJ_NAME, "dmu_ot", &sym)) {
594 		mdb_warn("failed to find " ZFS_OBJ_NAME "`dmu_ot");
595 		return (B_FALSE);
596 	}
597 
598 	if (mdb_ctf_vread(&mdoti, "dmu_object_type_info_t",
599 	    "mdb_dmu_object_type_info_t", sym.st_value + sz * ot, 0) != 0) {
600 		return (B_FALSE);
601 	}
602 
603 	return (mdoti.ot_encrypt);
604 }
605 
606 /* ARGSUSED */
607 static int
608 blkptr(uintptr_t addr, uint_t flags, int argc, const mdb_arg_t *argv)
609 {
610 	char type[80], checksum[80], compress[80];
611 	blkptr_t blk, *bp = &blk;
612 	char buf[BP_SPRINTF_LEN];
613 
614 	if (mdb_vread(&blk, sizeof (blkptr_t), addr) == -1) {
615 		mdb_warn("failed to read blkptr_t");
616 		return (DCMD_ERR);
617 	}
618 
619 	if (enum_lookup("enum dmu_object_type", BP_GET_TYPE(bp), "DMU_OT_",
620 	    sizeof (type), type) == -1 ||
621 	    enum_lookup("enum zio_checksum", BP_GET_CHECKSUM(bp),
622 	    "ZIO_CHECKSUM_", sizeof (checksum), checksum) == -1 ||
623 	    enum_lookup("enum zio_compress", BP_GET_COMPRESS(bp),
624 	    "ZIO_COMPRESS_", sizeof (compress), compress) == -1) {
625 		mdb_warn("Could not find blkptr enumerated types");
626 		return (DCMD_ERR);
627 	}
628 
629 	SNPRINTF_BLKPTR(mdb_snprintf, '\n', buf, sizeof (buf), bp, type,
630 	    checksum, compress);
631 
632 	mdb_printf("%s\n", buf);
633 
634 	return (DCMD_OK);
635 }
636 
637 typedef struct mdb_dmu_buf_impl {
638 	struct {
639 		uint64_t db_object;
640 		uintptr_t db_data;
641 	} db;
642 	uintptr_t db_objset;
643 	uint64_t db_level;
644 	uint64_t db_blkid;
645 	struct {
646 		uint64_t rc_count;
647 	} db_holds;
648 } mdb_dmu_buf_impl_t;
649 
650 /* ARGSUSED */
651 static int
652 dbuf(uintptr_t addr, uint_t flags, int argc, const mdb_arg_t *argv)
653 {
654 	mdb_dmu_buf_impl_t db;
655 	char objectname[32];
656 	char blkidname[32];
657 	char path[ZFS_MAX_DATASET_NAME_LEN];
658 	int ptr_width = (int)(sizeof (void *)) * 2;
659 
660 	if (DCMD_HDRSPEC(flags))
661 		mdb_printf("%*s %8s %3s %9s %5s %s\n",
662 		    ptr_width, "addr", "object", "lvl", "blkid", "holds", "os");
663 
664 	if (mdb_ctf_vread(&db, ZFS_STRUCT "dmu_buf_impl", "mdb_dmu_buf_impl_t",
665 	    addr, 0) == -1)
666 		return (DCMD_ERR);
667 
668 	if (db.db.db_object == DMU_META_DNODE_OBJECT)
669 		(void) strcpy(objectname, "mdn");
670 	else
671 		(void) mdb_snprintf(objectname, sizeof (objectname), "%llx",
672 		    (u_longlong_t)db.db.db_object);
673 
674 	if (db.db_blkid == DMU_BONUS_BLKID)
675 		(void) strcpy(blkidname, "bonus");
676 	else
677 		(void) mdb_snprintf(blkidname, sizeof (blkidname), "%llx",
678 		    (u_longlong_t)db.db_blkid);
679 
680 	if (objset_name(db.db_objset, path)) {
681 		return (DCMD_ERR);
682 	}
683 
684 	mdb_printf("%*p %8s %3u %9s %5llu %s\n", ptr_width, addr,
685 	    objectname, (int)db.db_level, blkidname,
686 	    db.db_holds.rc_count, path);
687 
688 	return (DCMD_OK);
689 }
690 
691 /* ARGSUSED */
692 static int
693 dbuf_stats(uintptr_t addr, uint_t flags, int argc, const mdb_arg_t *argv)
694 {
695 #define	HISTOSZ 32
696 	uintptr_t dbp;
697 	dmu_buf_impl_t db;
698 	dbuf_hash_table_t ht;
699 	uint64_t bucket, ndbufs;
700 	uint64_t histo[HISTOSZ];
701 	uint64_t histo2[HISTOSZ];
702 	int i, maxidx;
703 
704 	if (mdb_readvar(&ht, "dbuf_hash_table") == -1) {
705 		mdb_warn("failed to read 'dbuf_hash_table'");
706 		return (DCMD_ERR);
707 	}
708 
709 	for (i = 0; i < HISTOSZ; i++) {
710 		histo[i] = 0;
711 		histo2[i] = 0;
712 	}
713 
714 	ndbufs = 0;
715 	for (bucket = 0; bucket < ht.hash_table_mask+1; bucket++) {
716 		int len;
717 
718 		if (mdb_vread(&dbp, sizeof (void *),
719 		    (uintptr_t)(ht.hash_table+bucket)) == -1) {
720 			mdb_warn("failed to read hash bucket %u at %p",
721 			    bucket, ht.hash_table+bucket);
722 			return (DCMD_ERR);
723 		}
724 
725 		len = 0;
726 		while (dbp != 0) {
727 			if (mdb_vread(&db, sizeof (dmu_buf_impl_t),
728 			    dbp) == -1) {
729 				mdb_warn("failed to read dbuf at %p", dbp);
730 				return (DCMD_ERR);
731 			}
732 			dbp = (uintptr_t)db.db_hash_next;
733 			for (i = MIN(len, HISTOSZ - 1); i >= 0; i--)
734 				histo2[i]++;
735 			len++;
736 			ndbufs++;
737 		}
738 
739 		if (len >= HISTOSZ)
740 			len = HISTOSZ-1;
741 		histo[len]++;
742 	}
743 
744 	mdb_printf("hash table has %llu buckets, %llu dbufs "
745 	    "(avg %llu buckets/dbuf)\n",
746 	    ht.hash_table_mask+1, ndbufs,
747 	    (ht.hash_table_mask+1)/ndbufs);
748 
749 	mdb_printf("\n");
750 	maxidx = 0;
751 	for (i = 0; i < HISTOSZ; i++)
752 		if (histo[i] > 0)
753 			maxidx = i;
754 	mdb_printf("hash chain length	number of buckets\n");
755 	for (i = 0; i <= maxidx; i++)
756 		mdb_printf("%u			%llu\n", i, histo[i]);
757 
758 	mdb_printf("\n");
759 	maxidx = 0;
760 	for (i = 0; i < HISTOSZ; i++)
761 		if (histo2[i] > 0)
762 			maxidx = i;
763 	mdb_printf("hash chain depth	number of dbufs\n");
764 	for (i = 0; i <= maxidx; i++)
765 		mdb_printf("%u or more		%llu	%llu%%\n",
766 		    i, histo2[i], histo2[i]*100/ndbufs);
767 
768 
769 	return (DCMD_OK);
770 }
771 
772 #define	CHAIN_END 0xffff
773 /*
774  * ::zap_leaf [-v]
775  *
776  * Print a zap_leaf_phys_t, assumed to be 16k
777  */
778 /* ARGSUSED */
779 static int
780 zap_leaf(uintptr_t addr, uint_t flags, int argc, const mdb_arg_t *argv)
781 {
782 	char buf[16*1024];
783 	int verbose = B_FALSE;
784 	int four = B_FALSE;
785 	dmu_buf_t l_dbuf;
786 	zap_leaf_t l;
787 	zap_leaf_phys_t *zlp = (void *)buf;
788 	int i;
789 
790 	if (mdb_getopts(argc, argv,
791 	    'v', MDB_OPT_SETBITS, TRUE, &verbose,
792 	    '4', MDB_OPT_SETBITS, TRUE, &four,
793 	    NULL) != argc)
794 		return (DCMD_USAGE);
795 
796 	l_dbuf.db_data = zlp;
797 	l.l_dbuf = &l_dbuf;
798 	l.l_bs = 14; /* assume 16k blocks */
799 	if (four)
800 		l.l_bs = 12;
801 
802 	if (!(flags & DCMD_ADDRSPEC)) {
803 		return (DCMD_USAGE);
804 	}
805 
806 	if (mdb_vread(buf, sizeof (buf), addr) == -1) {
807 		mdb_warn("failed to read zap_leaf_phys_t at %p", addr);
808 		return (DCMD_ERR);
809 	}
810 
811 	if (zlp->l_hdr.lh_block_type != ZBT_LEAF ||
812 	    zlp->l_hdr.lh_magic != ZAP_LEAF_MAGIC) {
813 		mdb_warn("This does not appear to be a zap_leaf_phys_t");
814 		return (DCMD_ERR);
815 	}
816 
817 	mdb_printf("zap_leaf_phys_t at %p:\n", addr);
818 	mdb_printf("    lh_prefix_len = %u\n", zlp->l_hdr.lh_prefix_len);
819 	mdb_printf("    lh_prefix = %llx\n", zlp->l_hdr.lh_prefix);
820 	mdb_printf("    lh_nentries = %u\n", zlp->l_hdr.lh_nentries);
821 	mdb_printf("    lh_nfree = %u\n", zlp->l_hdr.lh_nfree,
822 	    zlp->l_hdr.lh_nfree * 100 / (ZAP_LEAF_NUMCHUNKS(&l)));
823 	mdb_printf("    lh_freelist = %u\n", zlp->l_hdr.lh_freelist);
824 	mdb_printf("    lh_flags = %x (%s)\n", zlp->l_hdr.lh_flags,
825 	    zlp->l_hdr.lh_flags & ZLF_ENTRIES_CDSORTED ?
826 	    "ENTRIES_CDSORTED" : "");
827 
828 	if (verbose) {
829 		mdb_printf(" hash table:\n");
830 		for (i = 0; i < ZAP_LEAF_HASH_NUMENTRIES(&l); i++) {
831 			if (zlp->l_hash[i] != CHAIN_END)
832 				mdb_printf("    %u: %u\n", i, zlp->l_hash[i]);
833 		}
834 	}
835 
836 	mdb_printf(" chunks:\n");
837 	for (i = 0; i < ZAP_LEAF_NUMCHUNKS(&l); i++) {
838 		/* LINTED: alignment */
839 		zap_leaf_chunk_t *zlc = &ZAP_LEAF_CHUNK(&l, i);
840 		switch (zlc->l_entry.le_type) {
841 		case ZAP_CHUNK_FREE:
842 			if (verbose) {
843 				mdb_printf("    %u: free; lf_next = %u\n",
844 				    i, zlc->l_free.lf_next);
845 			}
846 			break;
847 		case ZAP_CHUNK_ENTRY:
848 			mdb_printf("    %u: entry\n", i);
849 			if (verbose) {
850 				mdb_printf("        le_next = %u\n",
851 				    zlc->l_entry.le_next);
852 			}
853 			mdb_printf("        le_name_chunk = %u\n",
854 			    zlc->l_entry.le_name_chunk);
855 			mdb_printf("        le_name_numints = %u\n",
856 			    zlc->l_entry.le_name_numints);
857 			mdb_printf("        le_value_chunk = %u\n",
858 			    zlc->l_entry.le_value_chunk);
859 			mdb_printf("        le_value_intlen = %u\n",
860 			    zlc->l_entry.le_value_intlen);
861 			mdb_printf("        le_value_numints = %u\n",
862 			    zlc->l_entry.le_value_numints);
863 			mdb_printf("        le_cd = %u\n",
864 			    zlc->l_entry.le_cd);
865 			mdb_printf("        le_hash = %llx\n",
866 			    zlc->l_entry.le_hash);
867 			break;
868 		case ZAP_CHUNK_ARRAY:
869 			mdb_printf("    %u: array", i);
870 			if (strisprint((char *)zlc->l_array.la_array))
871 				mdb_printf(" \"%s\"", zlc->l_array.la_array);
872 			mdb_printf("\n");
873 			if (verbose) {
874 				int j;
875 				mdb_printf("        ");
876 				for (j = 0; j < ZAP_LEAF_ARRAY_BYTES; j++) {
877 					mdb_printf("%02x ",
878 					    zlc->l_array.la_array[j]);
879 				}
880 				mdb_printf("\n");
881 			}
882 			if (zlc->l_array.la_next != CHAIN_END) {
883 				mdb_printf("        lf_next = %u\n",
884 				    zlc->l_array.la_next);
885 			}
886 			break;
887 		default:
888 			mdb_printf("    %u: undefined type %u\n",
889 			    zlc->l_entry.le_type);
890 		}
891 	}
892 
893 	return (DCMD_OK);
894 }
895 
896 typedef struct dbufs_data {
897 	mdb_ctf_id_t id;
898 	uint64_t objset;
899 	uint64_t object;
900 	uint64_t level;
901 	uint64_t blkid;
902 	char *osname;
903 } dbufs_data_t;
904 
905 #define	DBUFS_UNSET	(0xbaddcafedeadbeefULL)
906 
907 /* ARGSUSED */
908 static int
909 dbufs_cb(uintptr_t addr, const void *unknown, void *arg)
910 {
911 	dbufs_data_t *data = arg;
912 	uintptr_t objset;
913 	dmu_buf_t db;
914 	uint8_t level;
915 	uint64_t blkid;
916 	char osname[ZFS_MAX_DATASET_NAME_LEN];
917 
918 	if (GETMEMBID(addr, &data->id, db_objset, objset) ||
919 	    GETMEMBID(addr, &data->id, db, db) ||
920 	    GETMEMBID(addr, &data->id, db_level, level) ||
921 	    GETMEMBID(addr, &data->id, db_blkid, blkid)) {
922 		return (WALK_ERR);
923 	}
924 
925 	if ((data->objset == DBUFS_UNSET || data->objset == objset) &&
926 	    (data->osname == NULL || (objset_name(objset, osname) == 0 &&
927 	    strcmp(data->osname, osname) == 0)) &&
928 	    (data->object == DBUFS_UNSET || data->object == db.db_object) &&
929 	    (data->level == DBUFS_UNSET || data->level == level) &&
930 	    (data->blkid == DBUFS_UNSET || data->blkid == blkid)) {
931 		mdb_printf("%#lr\n", addr);
932 	}
933 	return (WALK_NEXT);
934 }
935 
936 /* ARGSUSED */
937 static int
938 dbufs(uintptr_t addr, uint_t flags, int argc, const mdb_arg_t *argv)
939 {
940 	dbufs_data_t data;
941 	char *object = NULL;
942 	char *blkid = NULL;
943 
944 	data.objset = data.object = data.level = data.blkid = DBUFS_UNSET;
945 	data.osname = NULL;
946 
947 	if (mdb_getopts(argc, argv,
948 	    'O', MDB_OPT_UINT64, &data.objset,
949 	    'n', MDB_OPT_STR, &data.osname,
950 	    'o', MDB_OPT_STR, &object,
951 	    'l', MDB_OPT_UINT64, &data.level,
952 	    'b', MDB_OPT_STR, &blkid,
953 	    NULL) != argc) {
954 		return (DCMD_USAGE);
955 	}
956 
957 	if (object) {
958 		if (strcmp(object, "mdn") == 0) {
959 			data.object = DMU_META_DNODE_OBJECT;
960 		} else {
961 			data.object = mdb_strtoull(object);
962 		}
963 	}
964 
965 	if (blkid) {
966 		if (strcmp(blkid, "bonus") == 0) {
967 			data.blkid = DMU_BONUS_BLKID;
968 		} else {
969 			data.blkid = mdb_strtoull(blkid);
970 		}
971 	}
972 
973 	if (mdb_ctf_lookup_by_name(ZFS_STRUCT "dmu_buf_impl", &data.id) == -1) {
974 		mdb_warn("couldn't find struct dmu_buf_impl_t");
975 		return (DCMD_ERR);
976 	}
977 
978 	if (mdb_walk("dmu_buf_impl_t", dbufs_cb, &data) != 0) {
979 		mdb_warn("can't walk dbufs");
980 		return (DCMD_ERR);
981 	}
982 
983 	return (DCMD_OK);
984 }
985 
986 typedef struct abuf_find_data {
987 	dva_t dva;
988 	mdb_ctf_id_t id;
989 } abuf_find_data_t;
990 
991 /* ARGSUSED */
992 static int
993 abuf_find_cb(uintptr_t addr, const void *unknown, void *arg)
994 {
995 	abuf_find_data_t *data = arg;
996 	dva_t dva;
997 
998 	if (GETMEMBID(addr, &data->id, b_dva, dva)) {
999 		return (WALK_ERR);
1000 	}
1001 
1002 	if (dva.dva_word[0] == data->dva.dva_word[0] &&
1003 	    dva.dva_word[1] == data->dva.dva_word[1]) {
1004 		mdb_printf("%#lr\n", addr);
1005 	}
1006 	return (WALK_NEXT);
1007 }
1008 
1009 typedef struct mdb_arc_state {
1010 	uintptr_t	arcs_list[ARC_BUFC_NUMTYPES];
1011 } mdb_arc_state_t;
1012 
1013 /* ARGSUSED */
1014 static int
1015 abuf_find(uintptr_t addr, uint_t flags, int argc, const mdb_arg_t *argv)
1016 {
1017 	abuf_find_data_t data;
1018 	GElf_Sym sym;
1019 	int i, j;
1020 	const char *syms[] = {
1021 		"ARC_mru",
1022 		"ARC_mru_ghost",
1023 		"ARC_mfu",
1024 		"ARC_mfu_ghost",
1025 	};
1026 
1027 	if (argc != 2)
1028 		return (DCMD_USAGE);
1029 
1030 	for (i = 0; i < 2; i ++) {
1031 		switch (argv[i].a_type) {
1032 		case MDB_TYPE_STRING:
1033 			data.dva.dva_word[i] = mdb_strtoull(argv[i].a_un.a_str);
1034 			break;
1035 		case MDB_TYPE_IMMEDIATE:
1036 			data.dva.dva_word[i] = argv[i].a_un.a_val;
1037 			break;
1038 		default:
1039 			return (DCMD_USAGE);
1040 		}
1041 	}
1042 
1043 	if (mdb_ctf_lookup_by_name(ZFS_STRUCT "arc_buf_hdr", &data.id) == -1) {
1044 		mdb_warn("couldn't find struct arc_buf_hdr");
1045 		return (DCMD_ERR);
1046 	}
1047 
1048 	for (i = 0; i < sizeof (syms) / sizeof (syms[0]); i++) {
1049 		mdb_arc_state_t mas;
1050 
1051 		if (mdb_lookup_by_obj(ZFS_OBJ_NAME, syms[i], &sym)) {
1052 			mdb_warn("can't find symbol %s", syms[i]);
1053 			return (DCMD_ERR);
1054 		}
1055 
1056 		if (mdb_ctf_vread(&mas, "arc_state_t", "mdb_arc_state_t",
1057 		    sym.st_value, 0) != 0) {
1058 			mdb_warn("can't read arcs_list of %s", syms[i]);
1059 			return (DCMD_ERR);
1060 		}
1061 
1062 		for (j = 0; j < ARC_BUFC_NUMTYPES; j++) {
1063 			uintptr_t addr = mas.arcs_list[j];
1064 
1065 			if (addr == 0)
1066 				continue;
1067 
1068 			if (mdb_pwalk("multilist", abuf_find_cb, &data,
1069 			    addr) != 0) {
1070 				mdb_warn("can't walk %s", syms[i]);
1071 				return (DCMD_ERR);
1072 			}
1073 		}
1074 	}
1075 
1076 	return (DCMD_OK);
1077 }
1078 
1079 
1080 typedef struct dbgmsg_arg {
1081 	boolean_t da_verbose;
1082 	boolean_t da_verbose_hr;
1083 	boolean_t da_address;
1084 } dbgmsg_arg_t;
1085 
1086 /* ARGSUSED */
1087 static int
1088 dbgmsg_cb(uintptr_t addr, const void *unknown, void *arg)
1089 {
1090 	static mdb_ctf_id_t id;
1091 	static boolean_t gotid;
1092 	static ulong_t off;
1093 
1094 	dbgmsg_arg_t *da = arg;
1095 	time_t timestamp;
1096 	hrtime_t hrtime;
1097 	char buf[1024];
1098 
1099 	if (!gotid) {
1100 		if (mdb_ctf_lookup_by_name(ZFS_STRUCT "zfs_dbgmsg", &id) ==
1101 		    -1) {
1102 			mdb_warn("couldn't find struct zfs_dbgmsg");
1103 			return (WALK_ERR);
1104 		}
1105 		gotid = TRUE;
1106 		if (mdb_ctf_offsetof(id, "zdm_msg", &off) == -1) {
1107 			mdb_warn("couldn't find zdm_msg");
1108 			return (WALK_ERR);
1109 		}
1110 		off /= 8;
1111 	}
1112 
1113 
1114 	if (GETMEMBID(addr, &id, zdm_timestamp, timestamp)) {
1115 		return (WALK_ERR);
1116 	}
1117 
1118 	if (da->da_verbose_hr) {
1119 		if (GETMEMBID(addr, &id, zdm_hrtime, hrtime)) {
1120 			return (WALK_ERR);
1121 		}
1122 	}
1123 
1124 	if (mdb_readstr(buf, sizeof (buf), addr + off) == -1) {
1125 		mdb_warn("failed to read zdm_msg at %p\n", addr + off);
1126 		return (DCMD_ERR);
1127 	}
1128 
1129 	if (da->da_address)
1130 		mdb_printf("%p ", addr);
1131 	if (da->da_verbose)
1132 		mdb_printf("%Y ", timestamp);
1133 	if (da->da_verbose_hr)
1134 		mdb_printf("%016x ", hrtime);
1135 
1136 	mdb_printf("%s\n", buf);
1137 
1138 	if (da->da_verbose || da->da_verbose_hr)
1139 		(void) mdb_call_dcmd("whatis", addr, DCMD_ADDRSPEC, 0, NULL);
1140 
1141 	return (WALK_NEXT);
1142 }
1143 
1144 /* ARGSUSED */
1145 static int
1146 dbgmsg(uintptr_t addr, uint_t flags, int argc, const mdb_arg_t *argv)
1147 {
1148 	GElf_Sym sym;
1149 	dbgmsg_arg_t da = { 0 };
1150 
1151 	if (mdb_getopts(argc, argv,
1152 	    'v', MDB_OPT_SETBITS, B_TRUE, &da.da_verbose,
1153 	    'r', MDB_OPT_SETBITS, B_TRUE, &da.da_verbose_hr,
1154 	    'a', MDB_OPT_SETBITS, B_TRUE, &da.da_address,
1155 	    NULL) != argc)
1156 		return (DCMD_USAGE);
1157 
1158 	if (mdb_lookup_by_obj(ZFS_OBJ_NAME, "zfs_dbgmsgs", &sym)) {
1159 		mdb_warn("can't find zfs_dbgmsgs");
1160 		return (DCMD_ERR);
1161 	}
1162 
1163 	if (mdb_pwalk("list", dbgmsg_cb, &da, sym.st_value) != 0) {
1164 		mdb_warn("can't walk zfs_dbgmsgs");
1165 		return (DCMD_ERR);
1166 	}
1167 
1168 	return (DCMD_OK);
1169 }
1170 
1171 /*ARGSUSED*/
1172 static int
1173 arc_print(uintptr_t addr, uint_t flags, int argc, const mdb_arg_t *argv)
1174 {
1175 	kstat_named_t *stats;
1176 	GElf_Sym sym;
1177 	int nstats, i;
1178 	uint_t opt_a = FALSE;
1179 	uint_t opt_b = FALSE;
1180 	uint_t shift = 0;
1181 	const char *suffix;
1182 
1183 	static const char *bytestats[] = {
1184 		"p", "c", "c_min", "c_max", "size", "duplicate_buffers_size",
1185 		"arc_meta_used", "arc_meta_limit", "arc_meta_max",
1186 		"arc_meta_min", "hdr_size", "data_size", "metadata_size",
1187 		"other_size", "anon_size", "anon_evictable_data",
1188 		"anon_evictable_metadata", "mru_size", "mru_evictable_data",
1189 		"mru_evictable_metadata", "mru_ghost_size",
1190 		"mru_ghost_evictable_data", "mru_ghost_evictable_metadata",
1191 		"mfu_size", "mfu_evictable_data", "mfu_evictable_metadata",
1192 		"mfu_ghost_size", "mfu_ghost_evictable_data",
1193 		"mfu_ghost_evictable_metadata", "evict_l2_cached",
1194 		"evict_l2_eligible", "evict_l2_ineligible", "l2_read_bytes",
1195 		"l2_write_bytes", "l2_size", "l2_asize", "l2_hdr_size",
1196 		"compressed_size", "uncompressed_size", "overhead_size",
1197 		NULL
1198 	};
1199 
1200 	static const char *extras[] = {
1201 		"arc_no_grow", "arc_tempreserve",
1202 		NULL
1203 	};
1204 
1205 	if (mdb_lookup_by_obj(ZFS_OBJ_NAME, "arc_stats", &sym) == -1) {
1206 		mdb_warn("failed to find 'arc_stats'");
1207 		return (DCMD_ERR);
1208 	}
1209 
1210 	stats = mdb_zalloc(sym.st_size, UM_SLEEP | UM_GC);
1211 
1212 	if (mdb_vread(stats, sym.st_size, sym.st_value) == -1) {
1213 		mdb_warn("couldn't read 'arc_stats' at %p", sym.st_value);
1214 		return (DCMD_ERR);
1215 	}
1216 
1217 	nstats = sym.st_size / sizeof (kstat_named_t);
1218 
1219 	/* NB: -a / opt_a are ignored for backwards compatability */
1220 	if (mdb_getopts(argc, argv,
1221 	    'a', MDB_OPT_SETBITS, TRUE, &opt_a,
1222 	    'b', MDB_OPT_SETBITS, TRUE, &opt_b,
1223 	    'k', MDB_OPT_SETBITS, 10, &shift,
1224 	    'm', MDB_OPT_SETBITS, 20, &shift,
1225 	    'g', MDB_OPT_SETBITS, 30, &shift,
1226 	    NULL) != argc)
1227 		return (DCMD_USAGE);
1228 
1229 	if (!opt_b && !shift)
1230 		shift = 20;
1231 
1232 	switch (shift) {
1233 	case 0:
1234 		suffix = "B";
1235 		break;
1236 	case 10:
1237 		suffix = "KB";
1238 		break;
1239 	case 20:
1240 		suffix = "MB";
1241 		break;
1242 	case 30:
1243 		suffix = "GB";
1244 		break;
1245 	default:
1246 		suffix = "XX";
1247 	}
1248 
1249 	for (i = 0; i < nstats; i++) {
1250 		int j;
1251 		boolean_t bytes = B_FALSE;
1252 
1253 		for (j = 0; bytestats[j]; j++) {
1254 			if (strcmp(stats[i].name, bytestats[j]) == 0) {
1255 				bytes = B_TRUE;
1256 				break;
1257 			}
1258 		}
1259 
1260 		if (bytes) {
1261 			mdb_printf("%-25s = %9llu %s\n", stats[i].name,
1262 			    stats[i].value.ui64 >> shift, suffix);
1263 		} else {
1264 			mdb_printf("%-25s = %9llu\n", stats[i].name,
1265 			    stats[i].value.ui64);
1266 		}
1267 	}
1268 
1269 	for (i = 0; extras[i]; i++) {
1270 		uint64_t buf;
1271 
1272 		if (mdb_lookup_by_obj(ZFS_OBJ_NAME, extras[i], &sym) == -1) {
1273 			mdb_warn("failed to find '%s'", extras[i]);
1274 			return (DCMD_ERR);
1275 		}
1276 
1277 		if (sym.st_size != sizeof (uint64_t) &&
1278 		    sym.st_size != sizeof (uint32_t)) {
1279 			mdb_warn("expected scalar for variable '%s'\n",
1280 			    extras[i]);
1281 			return (DCMD_ERR);
1282 		}
1283 
1284 		if (mdb_vread(&buf, sym.st_size, sym.st_value) == -1) {
1285 			mdb_warn("couldn't read '%s'", extras[i]);
1286 			return (DCMD_ERR);
1287 		}
1288 
1289 		mdb_printf("%-25s = ", extras[i]);
1290 
1291 		/* NB: all the 64-bit extras happen to be byte counts */
1292 		if (sym.st_size == sizeof (uint64_t))
1293 			mdb_printf("%9llu %s\n", buf >> shift, suffix);
1294 
1295 		if (sym.st_size == sizeof (uint32_t))
1296 			mdb_printf("%9d\n", *((uint32_t *)&buf));
1297 	}
1298 	return (DCMD_OK);
1299 }
1300 
1301 typedef struct mdb_spa_print {
1302 	pool_state_t spa_state;
1303 	char spa_name[ZFS_MAX_DATASET_NAME_LEN];
1304 	uintptr_t spa_normal_class;
1305 } mdb_spa_print_t;
1306 
1307 
1308 const char histo_stars[] = "****************************************";
1309 const int histo_width = sizeof (histo_stars) - 1;
1310 
1311 static void
1312 dump_histogram(const uint64_t *histo, int size, int offset)
1313 {
1314 	int i;
1315 	int minidx = size - 1;
1316 	int maxidx = 0;
1317 	uint64_t max = 0;
1318 
1319 	for (i = 0; i < size; i++) {
1320 		if (histo[i] > max)
1321 			max = histo[i];
1322 		if (histo[i] > 0 && i > maxidx)
1323 			maxidx = i;
1324 		if (histo[i] > 0 && i < minidx)
1325 			minidx = i;
1326 	}
1327 
1328 	if (max < histo_width)
1329 		max = histo_width;
1330 
1331 	for (i = minidx; i <= maxidx; i++) {
1332 		mdb_printf("%3u: %6llu %s\n",
1333 		    i + offset, (u_longlong_t)histo[i],
1334 		    &histo_stars[(max - histo[i]) * histo_width / max]);
1335 	}
1336 }
1337 
1338 typedef struct mdb_metaslab_class {
1339 	uint64_t mc_histogram[RANGE_TREE_HISTOGRAM_SIZE];
1340 } mdb_metaslab_class_t;
1341 
1342 /*
1343  * spa_class_histogram(uintptr_t class_addr)
1344  *
1345  * Prints free space histogram for a device class
1346  *
1347  * Returns DCMD_OK, or DCMD_ERR.
1348  */
1349 static int
1350 spa_class_histogram(uintptr_t class_addr)
1351 {
1352 	mdb_metaslab_class_t mc;
1353 	if (mdb_ctf_vread(&mc, "metaslab_class_t",
1354 	    "mdb_metaslab_class_t", class_addr, 0) == -1)
1355 		return (DCMD_ERR);
1356 
1357 	mdb_inc_indent(4);
1358 	dump_histogram(mc.mc_histogram, RANGE_TREE_HISTOGRAM_SIZE, 0);
1359 	mdb_dec_indent(4);
1360 	return (DCMD_OK);
1361 }
1362 
1363 /*
1364  * ::spa
1365  *
1366  *	-c	Print configuration information as well
1367  *	-v	Print vdev state
1368  *	-e	Print vdev error stats
1369  *	-m	Print vdev metaslab info
1370  *	-M	print vdev metaslab group info
1371  *	-h	Print histogram info (must be combined with -m or -M)
1372  *
1373  * Print a summarized spa_t.  When given no arguments, prints out a table of all
1374  * active pools on the system.
1375  */
1376 /* ARGSUSED */
1377 static int
1378 spa_print(uintptr_t addr, uint_t flags, int argc, const mdb_arg_t *argv)
1379 {
1380 	const char *statetab[] = { "ACTIVE", "EXPORTED", "DESTROYED",
1381 		"SPARE", "L2CACHE", "UNINIT", "UNAVAIL", "POTENTIAL" };
1382 	const char *state;
1383 	int spa_flags = 0;
1384 
1385 	if (mdb_getopts(argc, argv,
1386 	    'c', MDB_OPT_SETBITS, SPA_FLAG_CONFIG, &spa_flags,
1387 	    'v', MDB_OPT_SETBITS, SPA_FLAG_VDEVS, &spa_flags,
1388 	    'e', MDB_OPT_SETBITS, SPA_FLAG_ERRORS, &spa_flags,
1389 	    'M', MDB_OPT_SETBITS, SPA_FLAG_METASLAB_GROUPS, &spa_flags,
1390 	    'm', MDB_OPT_SETBITS, SPA_FLAG_METASLABS, &spa_flags,
1391 	    'h', MDB_OPT_SETBITS, SPA_FLAG_HISTOGRAMS, &spa_flags,
1392 	    NULL) != argc)
1393 		return (DCMD_USAGE);
1394 
1395 	if (!(flags & DCMD_ADDRSPEC)) {
1396 		if (mdb_walk_dcmd("spa", "spa", argc, argv) == -1) {
1397 			mdb_warn("can't walk spa");
1398 			return (DCMD_ERR);
1399 		}
1400 
1401 		return (DCMD_OK);
1402 	}
1403 
1404 	if (flags & DCMD_PIPE_OUT) {
1405 		mdb_printf("%#lr\n", addr);
1406 		return (DCMD_OK);
1407 	}
1408 
1409 	if (DCMD_HDRSPEC(flags))
1410 		mdb_printf("%<u>%-?s %9s %-*s%</u>\n", "ADDR", "STATE",
1411 		    sizeof (uintptr_t) == 4 ? 60 : 52, "NAME");
1412 
1413 	mdb_spa_print_t spa;
1414 	if (mdb_ctf_vread(&spa, "spa_t", "mdb_spa_print_t", addr, 0) == -1)
1415 		return (DCMD_ERR);
1416 
1417 	if (spa.spa_state < 0 || spa.spa_state > POOL_STATE_UNAVAIL)
1418 		state = "UNKNOWN";
1419 	else
1420 		state = statetab[spa.spa_state];
1421 
1422 	mdb_printf("%0?p %9s %s\n", addr, state, spa.spa_name);
1423 	if (spa_flags & SPA_FLAG_HISTOGRAMS)
1424 		spa_class_histogram(spa.spa_normal_class);
1425 
1426 	if (spa_flags & SPA_FLAG_CONFIG) {
1427 		mdb_printf("\n");
1428 		mdb_inc_indent(4);
1429 		if (mdb_call_dcmd("spa_config", addr, flags, 0,
1430 		    NULL) != DCMD_OK)
1431 			return (DCMD_ERR);
1432 		mdb_dec_indent(4);
1433 	}
1434 
1435 	if (spa_flags & SPA_FLAG_ALL_VDEV) {
1436 		mdb_arg_t v;
1437 		char opts[100] = "-";
1438 		int args =
1439 		    (spa_flags | SPA_FLAG_VDEVS) == SPA_FLAG_VDEVS ? 0 : 1;
1440 
1441 		if (spa_flags & SPA_FLAG_ERRORS)
1442 			strcat(opts, "e");
1443 		if (spa_flags & SPA_FLAG_METASLABS)
1444 			strcat(opts, "m");
1445 		if (spa_flags & SPA_FLAG_METASLAB_GROUPS)
1446 			strcat(opts, "M");
1447 		if (spa_flags & SPA_FLAG_HISTOGRAMS)
1448 			strcat(opts, "h");
1449 
1450 		v.a_type = MDB_TYPE_STRING;
1451 		v.a_un.a_str = opts;
1452 
1453 		mdb_printf("\n");
1454 		mdb_inc_indent(4);
1455 		if (mdb_call_dcmd("spa_vdevs", addr, flags, args,
1456 		    &v) != DCMD_OK)
1457 			return (DCMD_ERR);
1458 		mdb_dec_indent(4);
1459 	}
1460 
1461 	return (DCMD_OK);
1462 }
1463 
1464 typedef struct mdb_spa_config_spa {
1465 	uintptr_t spa_config;
1466 } mdb_spa_config_spa_t;
1467 
1468 /*
1469  * ::spa_config
1470  *
1471  * Given a spa_t, print the configuration information stored in spa_config.
1472  * Since it's just an nvlist, format it as an indented list of name=value pairs.
1473  * We simply read the value of spa_config and pass off to ::nvlist.
1474  */
1475 /* ARGSUSED */
1476 static int
1477 spa_print_config(uintptr_t addr, uint_t flags, int argc, const mdb_arg_t *argv)
1478 {
1479 	mdb_spa_config_spa_t spa;
1480 
1481 	if (argc != 0 || !(flags & DCMD_ADDRSPEC))
1482 		return (DCMD_USAGE);
1483 
1484 	if (mdb_ctf_vread(&spa, ZFS_STRUCT "spa", "mdb_spa_config_spa_t",
1485 	    addr, 0) == -1)
1486 		return (DCMD_ERR);
1487 
1488 	if (spa.spa_config == 0) {
1489 		mdb_printf("(none)\n");
1490 		return (DCMD_OK);
1491 	}
1492 
1493 	return (mdb_call_dcmd("nvlist", spa.spa_config, flags,
1494 	    0, NULL));
1495 }
1496 
1497 typedef struct mdb_range_tree {
1498 	struct {
1499 		uint64_t bt_num_elems;
1500 		uint64_t bt_num_nodes;
1501 	} rt_root;
1502 	uint64_t rt_space;
1503 	range_seg_type_t rt_type;
1504 	uint8_t		rt_shift;
1505 	uint64_t	rt_start;
1506 } mdb_range_tree_t;
1507 
1508 typedef struct mdb_metaslab_group {
1509 	uint64_t mg_fragmentation;
1510 	uint64_t mg_histogram[RANGE_TREE_HISTOGRAM_SIZE];
1511 	uintptr_t mg_vd;
1512 } mdb_metaslab_group_t;
1513 
1514 typedef struct mdb_metaslab {
1515 	uint64_t ms_id;
1516 	uint64_t ms_start;
1517 	uint64_t ms_size;
1518 	int64_t ms_deferspace;
1519 	uint64_t ms_fragmentation;
1520 	uint64_t ms_weight;
1521 	uintptr_t ms_allocating[TXG_SIZE];
1522 	uintptr_t ms_checkpointing;
1523 	uintptr_t ms_freeing;
1524 	uintptr_t ms_freed;
1525 	uintptr_t ms_allocatable;
1526 	uintptr_t ms_unflushed_frees;
1527 	uintptr_t ms_unflushed_allocs;
1528 	uintptr_t ms_sm;
1529 } mdb_metaslab_t;
1530 
1531 typedef struct mdb_space_map_phys_t {
1532 	int64_t smp_alloc;
1533 	uint64_t smp_histogram[SPACE_MAP_HISTOGRAM_SIZE];
1534 } mdb_space_map_phys_t;
1535 
1536 typedef struct mdb_space_map {
1537 	uint64_t sm_size;
1538 	uint8_t sm_shift;
1539 	uintptr_t sm_phys;
1540 } mdb_space_map_t;
1541 
1542 typedef struct mdb_vdev {
1543 	uint64_t vdev_id;
1544 	uint64_t vdev_state;
1545 	uintptr_t vdev_ops;
1546 	struct {
1547 		uint64_t vs_aux;
1548 		uint64_t vs_ops[VS_ZIO_TYPES];
1549 		uint64_t vs_bytes[VS_ZIO_TYPES];
1550 		uint64_t vs_read_errors;
1551 		uint64_t vs_write_errors;
1552 		uint64_t vs_checksum_errors;
1553 	} vdev_stat;
1554 	uintptr_t vdev_child;
1555 	uint64_t vdev_children;
1556 	uint64_t vdev_ms_count;
1557 	uintptr_t vdev_mg;
1558 	uintptr_t vdev_ms;
1559 	uintptr_t vdev_path;
1560 } mdb_vdev_t;
1561 
1562 typedef struct mdb_vdev_ops {
1563 	char vdev_op_type[16];
1564 } mdb_vdev_ops_t;
1565 
1566 static int
1567 metaslab_stats(mdb_vdev_t *vd, int spa_flags)
1568 {
1569 	mdb_inc_indent(4);
1570 	mdb_printf("%<u>%-?s %6s %20s %10s %10s %10s%</u>\n", "ADDR", "ID",
1571 	    "OFFSET", "FREE", "FRAG", "UCMU");
1572 
1573 	uintptr_t *vdev_ms = mdb_alloc(vd->vdev_ms_count * sizeof (vdev_ms),
1574 	    UM_SLEEP | UM_GC);
1575 	if (mdb_vread(vdev_ms, vd->vdev_ms_count * sizeof (uintptr_t),
1576 	    vd->vdev_ms) == -1) {
1577 		mdb_warn("failed to read vdev_ms at %p\n", vd->vdev_ms);
1578 		return (DCMD_ERR);
1579 	}
1580 
1581 	for (int m = 0; m < vd->vdev_ms_count; m++) {
1582 		mdb_metaslab_t ms;
1583 		mdb_space_map_t sm = { 0 };
1584 		mdb_space_map_phys_t smp = { 0 };
1585 		mdb_range_tree_t rt;
1586 		uint64_t uallocs, ufrees, raw_free, raw_uchanges_mem;
1587 		char free[MDB_NICENUM_BUFLEN];
1588 		char uchanges_mem[MDB_NICENUM_BUFLEN];
1589 
1590 		if (mdb_ctf_vread(&ms, "metaslab_t", "mdb_metaslab_t",
1591 		    vdev_ms[m], 0) == -1)
1592 			return (DCMD_ERR);
1593 
1594 		if (ms.ms_sm != 0 &&
1595 		    mdb_ctf_vread(&sm, "space_map_t", "mdb_space_map_t",
1596 		    ms.ms_sm, 0) == -1)
1597 			return (DCMD_ERR);
1598 
1599 		if (mdb_ctf_vread(&rt, "range_tree_t", "mdb_range_tree_t",
1600 		    ms.ms_unflushed_frees, 0) == -1)
1601 			return (DCMD_ERR);
1602 		ufrees = rt.rt_space;
1603 		raw_uchanges_mem = rt.rt_root.bt_num_nodes * BTREE_LEAF_SIZE;
1604 
1605 		if (mdb_ctf_vread(&rt, "range_tree_t", "mdb_range_tree_t",
1606 		    ms.ms_unflushed_allocs, 0) == -1)
1607 			return (DCMD_ERR);
1608 		uallocs = rt.rt_space;
1609 		raw_uchanges_mem += rt.rt_root.bt_num_nodes * BTREE_LEAF_SIZE;
1610 		mdb_nicenum(raw_uchanges_mem, uchanges_mem);
1611 
1612 		raw_free = ms.ms_size;
1613 		if (ms.ms_sm != 0 && sm.sm_phys != 0) {
1614 			(void) mdb_ctf_vread(&smp, "space_map_phys_t",
1615 			    "mdb_space_map_phys_t", sm.sm_phys, 0);
1616 			raw_free -= smp.smp_alloc;
1617 		}
1618 		raw_free += ufrees - uallocs;
1619 		mdb_nicenum(raw_free, free);
1620 
1621 		mdb_printf("%0?p %6llu %20llx %10s ", vdev_ms[m], ms.ms_id,
1622 		    ms.ms_start, free);
1623 		if (ms.ms_fragmentation == ZFS_FRAG_INVALID)
1624 			mdb_printf("%9s ", "-");
1625 		else
1626 			mdb_printf("%9llu%% ", ms.ms_fragmentation);
1627 		mdb_printf("%10s\n", uchanges_mem);
1628 
1629 		if ((spa_flags & SPA_FLAG_HISTOGRAMS) && ms.ms_sm != 0 &&
1630 		    sm.sm_phys != 0) {
1631 			dump_histogram(smp.smp_histogram,
1632 			    SPACE_MAP_HISTOGRAM_SIZE, sm.sm_shift);
1633 		}
1634 	}
1635 	mdb_dec_indent(4);
1636 	return (DCMD_OK);
1637 }
1638 
1639 static int
1640 metaslab_group_stats(mdb_vdev_t *vd, int spa_flags)
1641 {
1642 	mdb_metaslab_group_t mg;
1643 	if (mdb_ctf_vread(&mg, "metaslab_group_t", "mdb_metaslab_group_t",
1644 	    vd->vdev_mg, 0) == -1) {
1645 		mdb_warn("failed to read vdev_mg at %p\n", vd->vdev_mg);
1646 		return (DCMD_ERR);
1647 	}
1648 
1649 	mdb_inc_indent(4);
1650 	mdb_printf("%<u>%-?s %7s %9s%</u>\n", "ADDR", "FRAG", "UCMU");
1651 
1652 	if (mg.mg_fragmentation == ZFS_FRAG_INVALID)
1653 		mdb_printf("%0?p %6s\n", vd->vdev_mg, "-");
1654 	else
1655 		mdb_printf("%0?p %6llu%%", vd->vdev_mg, mg.mg_fragmentation);
1656 
1657 
1658 	uintptr_t *vdev_ms = mdb_alloc(vd->vdev_ms_count * sizeof (vdev_ms),
1659 	    UM_SLEEP | UM_GC);
1660 	if (mdb_vread(vdev_ms, vd->vdev_ms_count * sizeof (uintptr_t),
1661 	    vd->vdev_ms) == -1) {
1662 		mdb_warn("failed to read vdev_ms at %p\n", vd->vdev_ms);
1663 		return (DCMD_ERR);
1664 	}
1665 
1666 	uint64_t raw_uchanges_mem = 0;
1667 	char uchanges_mem[MDB_NICENUM_BUFLEN];
1668 	for (int m = 0; m < vd->vdev_ms_count; m++) {
1669 		mdb_metaslab_t ms;
1670 		mdb_range_tree_t rt;
1671 
1672 		if (mdb_ctf_vread(&ms, "metaslab_t", "mdb_metaslab_t",
1673 		    vdev_ms[m], 0) == -1)
1674 			return (DCMD_ERR);
1675 
1676 		if (mdb_ctf_vread(&rt, "range_tree_t", "mdb_range_tree_t",
1677 		    ms.ms_unflushed_frees, 0) == -1)
1678 			return (DCMD_ERR);
1679 		raw_uchanges_mem += rt.rt_root.bt_num_nodes * BTREE_LEAF_SIZE;
1680 
1681 		if (mdb_ctf_vread(&rt, "range_tree_t", "mdb_range_tree_t",
1682 		    ms.ms_unflushed_allocs, 0) == -1)
1683 			return (DCMD_ERR);
1684 		raw_uchanges_mem += rt.rt_root.bt_num_nodes * BTREE_LEAF_SIZE;
1685 	}
1686 	mdb_nicenum(raw_uchanges_mem, uchanges_mem);
1687 	mdb_printf("%10s\n", uchanges_mem);
1688 
1689 	if (spa_flags & SPA_FLAG_HISTOGRAMS)
1690 		dump_histogram(mg.mg_histogram, RANGE_TREE_HISTOGRAM_SIZE, 0);
1691 	mdb_dec_indent(4);
1692 	return (DCMD_OK);
1693 }
1694 
1695 /*
1696  * ::vdev
1697  *
1698  * Print out a summarized vdev_t, in the following form:
1699  *
1700  * ADDR             STATE	AUX            DESC
1701  * fffffffbcde23df0 HEALTHY	-              /dev/dsk/c0t0d0
1702  *
1703  * If '-r' is specified, recursively visit all children.
1704  *
1705  * With '-e', the statistics associated with the vdev are printed as well.
1706  */
1707 static int
1708 do_print_vdev(uintptr_t addr, int flags, int depth, boolean_t recursive,
1709     int spa_flags)
1710 {
1711 	mdb_vdev_t vd;
1712 	if (mdb_ctf_vread(&vd, "vdev_t", "mdb_vdev_t",
1713 	    (uintptr_t)addr, 0) == -1)
1714 		return (DCMD_ERR);
1715 
1716 	if (flags & DCMD_PIPE_OUT) {
1717 		mdb_printf("%#lr\n", addr);
1718 	} else {
1719 		char desc[MAXNAMELEN];
1720 		if (vd.vdev_path != 0) {
1721 			if (mdb_readstr(desc, sizeof (desc),
1722 			    (uintptr_t)vd.vdev_path) == -1) {
1723 				mdb_warn("failed to read vdev_path at %p\n",
1724 				    vd.vdev_path);
1725 				return (DCMD_ERR);
1726 			}
1727 		} else if (vd.vdev_ops != 0) {
1728 			vdev_ops_t ops;
1729 			if (mdb_vread(&ops, sizeof (ops),
1730 			    (uintptr_t)vd.vdev_ops) == -1) {
1731 				mdb_warn("failed to read vdev_ops at %p\n",
1732 				    vd.vdev_ops);
1733 				return (DCMD_ERR);
1734 			}
1735 			(void) strcpy(desc, ops.vdev_op_type);
1736 		} else {
1737 			(void) strcpy(desc, "<unknown>");
1738 		}
1739 
1740 		if (depth == 0 && DCMD_HDRSPEC(flags))
1741 			mdb_printf("%<u>%-?s %-9s %-12s %-*s%</u>\n",
1742 			    "ADDR", "STATE", "AUX",
1743 			    sizeof (uintptr_t) == 4 ? 43 : 35,
1744 			    "DESCRIPTION");
1745 
1746 		mdb_printf("%0?p ", addr);
1747 
1748 		const char *state, *aux;
1749 		switch (vd.vdev_state) {
1750 		case VDEV_STATE_CLOSED:
1751 			state = "CLOSED";
1752 			break;
1753 		case VDEV_STATE_OFFLINE:
1754 			state = "OFFLINE";
1755 			break;
1756 		case VDEV_STATE_CANT_OPEN:
1757 			state = "CANT_OPEN";
1758 			break;
1759 		case VDEV_STATE_DEGRADED:
1760 			state = "DEGRADED";
1761 			break;
1762 		case VDEV_STATE_HEALTHY:
1763 			state = "HEALTHY";
1764 			break;
1765 		case VDEV_STATE_REMOVED:
1766 			state = "REMOVED";
1767 			break;
1768 		case VDEV_STATE_FAULTED:
1769 			state = "FAULTED";
1770 			break;
1771 		default:
1772 			state = "UNKNOWN";
1773 			break;
1774 		}
1775 
1776 		switch (vd.vdev_stat.vs_aux) {
1777 		case VDEV_AUX_NONE:
1778 			aux = "-";
1779 			break;
1780 		case VDEV_AUX_OPEN_FAILED:
1781 			aux = "OPEN_FAILED";
1782 			break;
1783 		case VDEV_AUX_CORRUPT_DATA:
1784 			aux = "CORRUPT_DATA";
1785 			break;
1786 		case VDEV_AUX_NO_REPLICAS:
1787 			aux = "NO_REPLICAS";
1788 			break;
1789 		case VDEV_AUX_BAD_GUID_SUM:
1790 			aux = "BAD_GUID_SUM";
1791 			break;
1792 		case VDEV_AUX_TOO_SMALL:
1793 			aux = "TOO_SMALL";
1794 			break;
1795 		case VDEV_AUX_BAD_LABEL:
1796 			aux = "BAD_LABEL";
1797 			break;
1798 		case VDEV_AUX_VERSION_NEWER:
1799 			aux = "VERS_NEWER";
1800 			break;
1801 		case VDEV_AUX_VERSION_OLDER:
1802 			aux = "VERS_OLDER";
1803 			break;
1804 		case VDEV_AUX_UNSUP_FEAT:
1805 			aux = "UNSUP_FEAT";
1806 			break;
1807 		case VDEV_AUX_SPARED:
1808 			aux = "SPARED";
1809 			break;
1810 		case VDEV_AUX_ERR_EXCEEDED:
1811 			aux = "ERR_EXCEEDED";
1812 			break;
1813 		case VDEV_AUX_IO_FAILURE:
1814 			aux = "IO_FAILURE";
1815 			break;
1816 		case VDEV_AUX_BAD_LOG:
1817 			aux = "BAD_LOG";
1818 			break;
1819 		case VDEV_AUX_EXTERNAL:
1820 			aux = "EXTERNAL";
1821 			break;
1822 		case VDEV_AUX_SPLIT_POOL:
1823 			aux = "SPLIT_POOL";
1824 			break;
1825 		case VDEV_AUX_CHILDREN_OFFLINE:
1826 			aux = "CHILDREN_OFFLINE";
1827 			break;
1828 		default:
1829 			aux = "UNKNOWN";
1830 			break;
1831 		}
1832 
1833 		mdb_printf("%-9s %-12s %*s%s\n", state, aux, depth, "", desc);
1834 
1835 		if (spa_flags & SPA_FLAG_ERRORS) {
1836 			int i;
1837 
1838 			mdb_inc_indent(4);
1839 			mdb_printf("\n");
1840 			mdb_printf("%<u>       %12s %12s %12s %12s "
1841 			    "%12s%</u>\n", "READ", "WRITE", "FREE", "CLAIM",
1842 			    "IOCTL");
1843 			mdb_printf("OPS     ");
1844 			for (i = 1; i < VS_ZIO_TYPES; i++)
1845 				mdb_printf("%11#llx%s",
1846 				    vd.vdev_stat.vs_ops[i],
1847 				    i == VS_ZIO_TYPES - 1 ? "" : "  ");
1848 			mdb_printf("\n");
1849 			mdb_printf("BYTES   ");
1850 			for (i = 1; i < VS_ZIO_TYPES; i++)
1851 				mdb_printf("%11#llx%s",
1852 				    vd.vdev_stat.vs_bytes[i],
1853 				    i == VS_ZIO_TYPES - 1 ? "" : "  ");
1854 
1855 
1856 			mdb_printf("\n");
1857 			mdb_printf("EREAD    %10#llx\n",
1858 			    vd.vdev_stat.vs_read_errors);
1859 			mdb_printf("EWRITE   %10#llx\n",
1860 			    vd.vdev_stat.vs_write_errors);
1861 			mdb_printf("ECKSUM   %10#llx\n",
1862 			    vd.vdev_stat.vs_checksum_errors);
1863 			mdb_dec_indent(4);
1864 			mdb_printf("\n");
1865 		}
1866 
1867 		if ((spa_flags & SPA_FLAG_METASLAB_GROUPS) &&
1868 		    vd.vdev_mg != 0) {
1869 			metaslab_group_stats(&vd, spa_flags);
1870 		}
1871 		if ((spa_flags & SPA_FLAG_METASLABS) && vd.vdev_ms != 0) {
1872 			metaslab_stats(&vd, spa_flags);
1873 		}
1874 	}
1875 
1876 	uint64_t children = vd.vdev_children;
1877 	if (children == 0 || !recursive)
1878 		return (DCMD_OK);
1879 
1880 	uintptr_t *child = mdb_alloc(children * sizeof (child),
1881 	    UM_SLEEP | UM_GC);
1882 	if (mdb_vread(child, children * sizeof (void *), vd.vdev_child) == -1) {
1883 		mdb_warn("failed to read vdev children at %p", vd.vdev_child);
1884 		return (DCMD_ERR);
1885 	}
1886 
1887 	for (uint64_t c = 0; c < children; c++) {
1888 		if (do_print_vdev(child[c], flags, depth + 2, recursive,
1889 		    spa_flags)) {
1890 			return (DCMD_ERR);
1891 		}
1892 	}
1893 
1894 	return (DCMD_OK);
1895 }
1896 
1897 static int
1898 vdev_print(uintptr_t addr, uint_t flags, int argc, const mdb_arg_t *argv)
1899 {
1900 	uint64_t depth = 0;
1901 	boolean_t recursive = B_FALSE;
1902 	int spa_flags = 0;
1903 
1904 	if (mdb_getopts(argc, argv,
1905 	    'e', MDB_OPT_SETBITS, SPA_FLAG_ERRORS, &spa_flags,
1906 	    'm', MDB_OPT_SETBITS, SPA_FLAG_METASLABS, &spa_flags,
1907 	    'M', MDB_OPT_SETBITS, SPA_FLAG_METASLAB_GROUPS, &spa_flags,
1908 	    'h', MDB_OPT_SETBITS, SPA_FLAG_HISTOGRAMS, &spa_flags,
1909 	    'r', MDB_OPT_SETBITS, TRUE, &recursive,
1910 	    'd', MDB_OPT_UINT64, &depth, NULL) != argc)
1911 		return (DCMD_USAGE);
1912 
1913 	if (!(flags & DCMD_ADDRSPEC)) {
1914 		mdb_warn("no vdev_t address given\n");
1915 		return (DCMD_ERR);
1916 	}
1917 
1918 	return (do_print_vdev(addr, flags, (int)depth, recursive, spa_flags));
1919 }
1920 
1921 typedef struct mdb_metaslab_alloc_trace {
1922 	uintptr_t mat_mg;
1923 	uintptr_t mat_msp;
1924 	uint64_t mat_size;
1925 	uint64_t mat_weight;
1926 	uint64_t mat_offset;
1927 	uint32_t mat_dva_id;
1928 	int mat_allocator;
1929 } mdb_metaslab_alloc_trace_t;
1930 
1931 static void
1932 metaslab_print_weight(uint64_t weight)
1933 {
1934 	char buf[100];
1935 
1936 	if (WEIGHT_IS_SPACEBASED(weight)) {
1937 		mdb_nicenum(
1938 		    weight & ~(METASLAB_ACTIVE_MASK | METASLAB_WEIGHT_TYPE),
1939 		    buf);
1940 	} else {
1941 		char size[MDB_NICENUM_BUFLEN];
1942 		mdb_nicenum(1ULL << WEIGHT_GET_INDEX(weight), size);
1943 		(void) mdb_snprintf(buf, sizeof (buf), "%llu x %s",
1944 		    WEIGHT_GET_COUNT(weight), size);
1945 	}
1946 	mdb_printf("%11s ", buf);
1947 }
1948 
1949 /* ARGSUSED */
1950 static int
1951 metaslab_weight(uintptr_t addr, uint_t flags, int argc, const mdb_arg_t *argv)
1952 {
1953 	uint64_t weight = 0;
1954 	char active;
1955 
1956 	if (argc == 0 && (flags & DCMD_ADDRSPEC)) {
1957 		if (mdb_vread(&weight, sizeof (uint64_t), addr) == -1) {
1958 			mdb_warn("failed to read weight at %p\n", addr);
1959 			return (DCMD_ERR);
1960 		}
1961 	} else if (argc == 1 && !(flags & DCMD_ADDRSPEC)) {
1962 		weight = (argv[0].a_type == MDB_TYPE_IMMEDIATE) ?
1963 		    argv[0].a_un.a_val : mdb_strtoull(argv[0].a_un.a_str);
1964 	} else {
1965 		return (DCMD_USAGE);
1966 	}
1967 
1968 	if (DCMD_HDRSPEC(flags)) {
1969 		mdb_printf("%<u>%-6s %9s %9s%</u>\n",
1970 		    "ACTIVE", "ALGORITHM", "WEIGHT");
1971 	}
1972 
1973 	if (weight & METASLAB_WEIGHT_PRIMARY)
1974 		active = 'P';
1975 	else if (weight & METASLAB_WEIGHT_SECONDARY)
1976 		active = 'S';
1977 	else
1978 		active = '-';
1979 	mdb_printf("%6c %8s ", active,
1980 	    WEIGHT_IS_SPACEBASED(weight) ? "SPACE" : "SEGMENT");
1981 	metaslab_print_weight(weight);
1982 	mdb_printf("\n");
1983 
1984 	return (DCMD_OK);
1985 }
1986 
1987 /* ARGSUSED */
1988 static int
1989 metaslab_trace(uintptr_t addr, uint_t flags, int argc, const mdb_arg_t *argv)
1990 {
1991 	mdb_metaslab_alloc_trace_t mat;
1992 	mdb_metaslab_group_t mg = { 0 };
1993 	char result_type[100];
1994 
1995 	if (mdb_ctf_vread(&mat, "metaslab_alloc_trace_t",
1996 	    "mdb_metaslab_alloc_trace_t", addr, 0) == -1) {
1997 		return (DCMD_ERR);
1998 	}
1999 
2000 	if (!(flags & DCMD_PIPE_OUT) && DCMD_HDRSPEC(flags)) {
2001 		mdb_printf("%<u>%6s %6s %8s %11s %11s %18s %18s%</u>\n",
2002 		    "MSID", "DVA", "ASIZE", "ALLOCATOR", "WEIGHT", "RESULT",
2003 		    "VDEV");
2004 	}
2005 
2006 	if (mat.mat_msp != 0) {
2007 		mdb_metaslab_t ms;
2008 
2009 		if (mdb_ctf_vread(&ms, "metaslab_t", "mdb_metaslab_t",
2010 		    mat.mat_msp, 0) == -1) {
2011 			return (DCMD_ERR);
2012 		}
2013 		mdb_printf("%6llu ", ms.ms_id);
2014 	} else {
2015 		mdb_printf("%6s ", "-");
2016 	}
2017 
2018 	mdb_printf("%6d %8llx %11llx ", mat.mat_dva_id, mat.mat_size,
2019 	    mat.mat_allocator);
2020 
2021 	metaslab_print_weight(mat.mat_weight);
2022 
2023 	if ((int64_t)mat.mat_offset < 0) {
2024 		if (enum_lookup("enum trace_alloc_type", mat.mat_offset,
2025 		    "TRACE_", sizeof (result_type), result_type) == -1) {
2026 			mdb_warn("Could not find enum for trace_alloc_type");
2027 			return (DCMD_ERR);
2028 		}
2029 		mdb_printf("%18s ", result_type);
2030 	} else {
2031 		mdb_printf("%<b>%18llx%</b> ", mat.mat_offset);
2032 	}
2033 
2034 	if (mat.mat_mg != 0 &&
2035 	    mdb_ctf_vread(&mg, "metaslab_group_t", "mdb_metaslab_group_t",
2036 	    mat.mat_mg, 0) == -1) {
2037 		return (DCMD_ERR);
2038 	}
2039 
2040 	if (mg.mg_vd != 0) {
2041 		mdb_vdev_t vdev;
2042 		char desc[MAXNAMELEN];
2043 
2044 		if (mdb_ctf_vread(&vdev, "vdev_t", "mdb_vdev_t",
2045 		    mg.mg_vd, 0) == -1) {
2046 			return (DCMD_ERR);
2047 		}
2048 
2049 		if (vdev.vdev_path != 0) {
2050 			char path[MAXNAMELEN];
2051 
2052 			if (mdb_readstr(path, sizeof (path),
2053 			    vdev.vdev_path) == -1) {
2054 				mdb_warn("failed to read vdev_path at %p\n",
2055 				    vdev.vdev_path);
2056 				return (DCMD_ERR);
2057 			}
2058 			char *slash;
2059 			if ((slash = strrchr(path, '/')) != NULL) {
2060 				strcpy(desc, slash + 1);
2061 			} else {
2062 				strcpy(desc, path);
2063 			}
2064 		} else if (vdev.vdev_ops != 0) {
2065 			mdb_vdev_ops_t ops;
2066 			if (mdb_ctf_vread(&ops, "vdev_ops_t", "mdb_vdev_ops_t",
2067 			    vdev.vdev_ops, 0) == -1) {
2068 				mdb_warn("failed to read vdev_ops at %p\n",
2069 				    vdev.vdev_ops);
2070 				return (DCMD_ERR);
2071 			}
2072 			(void) mdb_snprintf(desc, sizeof (desc),
2073 			    "%s-%llu", ops.vdev_op_type, vdev.vdev_id);
2074 		} else {
2075 			(void) strcpy(desc, "<unknown>");
2076 		}
2077 		mdb_printf("%18s\n", desc);
2078 	}
2079 
2080 	return (DCMD_OK);
2081 }
2082 
2083 typedef struct metaslab_walk_data {
2084 	uint64_t mw_numvdevs;
2085 	uintptr_t *mw_vdevs;
2086 	int mw_curvdev;
2087 	uint64_t mw_nummss;
2088 	uintptr_t *mw_mss;
2089 	int mw_curms;
2090 } metaslab_walk_data_t;
2091 
2092 static int
2093 metaslab_walk_step(mdb_walk_state_t *wsp)
2094 {
2095 	metaslab_walk_data_t *mw = wsp->walk_data;
2096 	metaslab_t ms;
2097 	uintptr_t msp;
2098 
2099 	if (mw->mw_curvdev >= mw->mw_numvdevs)
2100 		return (WALK_DONE);
2101 
2102 	if (mw->mw_mss == NULL) {
2103 		uintptr_t mssp;
2104 		uintptr_t vdevp;
2105 
2106 		ASSERT(mw->mw_curms == 0);
2107 		ASSERT(mw->mw_nummss == 0);
2108 
2109 		vdevp = mw->mw_vdevs[mw->mw_curvdev];
2110 		if (GETMEMB(vdevp, "vdev", vdev_ms, mssp) ||
2111 		    GETMEMB(vdevp, "vdev", vdev_ms_count, mw->mw_nummss)) {
2112 			return (WALK_ERR);
2113 		}
2114 
2115 		mw->mw_mss = mdb_alloc(mw->mw_nummss * sizeof (void*),
2116 		    UM_SLEEP | UM_GC);
2117 		if (mdb_vread(mw->mw_mss, mw->mw_nummss * sizeof (void*),
2118 		    mssp) == -1) {
2119 			mdb_warn("failed to read vdev_ms at %p", mssp);
2120 			return (WALK_ERR);
2121 		}
2122 	}
2123 
2124 	if (mw->mw_curms >= mw->mw_nummss) {
2125 		mw->mw_mss = NULL;
2126 		mw->mw_curms = 0;
2127 		mw->mw_nummss = 0;
2128 		mw->mw_curvdev++;
2129 		return (WALK_NEXT);
2130 	}
2131 
2132 	msp = mw->mw_mss[mw->mw_curms];
2133 	if (mdb_vread(&ms, sizeof (metaslab_t), msp) == -1) {
2134 		mdb_warn("failed to read metaslab_t at %p", msp);
2135 		return (WALK_ERR);
2136 	}
2137 
2138 	mw->mw_curms++;
2139 
2140 	return (wsp->walk_callback(msp, &ms, wsp->walk_cbdata));
2141 }
2142 
2143 static int
2144 metaslab_walk_init(mdb_walk_state_t *wsp)
2145 {
2146 	metaslab_walk_data_t *mw;
2147 	uintptr_t root_vdevp;
2148 	uintptr_t childp;
2149 
2150 	if (wsp->walk_addr == 0) {
2151 		mdb_warn("must supply address of spa_t\n");
2152 		return (WALK_ERR);
2153 	}
2154 
2155 	mw = mdb_zalloc(sizeof (metaslab_walk_data_t), UM_SLEEP | UM_GC);
2156 
2157 	if (GETMEMB(wsp->walk_addr, "spa", spa_root_vdev, root_vdevp) ||
2158 	    GETMEMB(root_vdevp, "vdev", vdev_children, mw->mw_numvdevs) ||
2159 	    GETMEMB(root_vdevp, "vdev", vdev_child, childp)) {
2160 		return (DCMD_ERR);
2161 	}
2162 
2163 	mw->mw_vdevs = mdb_alloc(mw->mw_numvdevs * sizeof (void *),
2164 	    UM_SLEEP | UM_GC);
2165 	if (mdb_vread(mw->mw_vdevs, mw->mw_numvdevs * sizeof (void *),
2166 	    childp) == -1) {
2167 		mdb_warn("failed to read root vdev children at %p", childp);
2168 		return (DCMD_ERR);
2169 	}
2170 
2171 	wsp->walk_data = mw;
2172 
2173 	return (WALK_NEXT);
2174 }
2175 
2176 typedef struct mdb_spa {
2177 	uintptr_t spa_dsl_pool;
2178 	uintptr_t spa_root_vdev;
2179 } mdb_spa_t;
2180 
2181 typedef struct mdb_dsl_pool {
2182 	uintptr_t dp_root_dir;
2183 } mdb_dsl_pool_t;
2184 
2185 typedef struct mdb_dsl_dir {
2186 	uintptr_t dd_dbuf;
2187 	int64_t dd_space_towrite[TXG_SIZE];
2188 } mdb_dsl_dir_t;
2189 
2190 typedef struct mdb_dsl_dir_phys {
2191 	uint64_t dd_used_bytes;
2192 	uint64_t dd_compressed_bytes;
2193 	uint64_t dd_uncompressed_bytes;
2194 } mdb_dsl_dir_phys_t;
2195 
2196 typedef struct space_data {
2197 	uint64_t ms_allocating[TXG_SIZE];
2198 	uint64_t ms_checkpointing;
2199 	uint64_t ms_freeing;
2200 	uint64_t ms_freed;
2201 	uint64_t ms_unflushed_frees;
2202 	uint64_t ms_unflushed_allocs;
2203 	uint64_t ms_allocatable;
2204 	int64_t ms_deferspace;
2205 	uint64_t avail;
2206 } space_data_t;
2207 
2208 /* ARGSUSED */
2209 static int
2210 space_cb(uintptr_t addr, const void *unknown, void *arg)
2211 {
2212 	space_data_t *sd = arg;
2213 	mdb_metaslab_t ms;
2214 	mdb_range_tree_t rt;
2215 	mdb_space_map_t sm = { 0 };
2216 	mdb_space_map_phys_t smp = { 0 };
2217 	uint64_t uallocs, ufrees;
2218 	int i;
2219 
2220 	if (mdb_ctf_vread(&ms, "metaslab_t", "mdb_metaslab_t",
2221 	    addr, 0) == -1)
2222 		return (WALK_ERR);
2223 
2224 	for (i = 0; i < TXG_SIZE; i++) {
2225 		if (mdb_ctf_vread(&rt, "range_tree_t",
2226 		    "mdb_range_tree_t", ms.ms_allocating[i], 0) == -1)
2227 			return (WALK_ERR);
2228 		sd->ms_allocating[i] += rt.rt_space;
2229 	}
2230 
2231 	if (mdb_ctf_vread(&rt, "range_tree_t",
2232 	    "mdb_range_tree_t", ms.ms_checkpointing, 0) == -1)
2233 		return (WALK_ERR);
2234 	sd->ms_checkpointing += rt.rt_space;
2235 
2236 	if (mdb_ctf_vread(&rt, "range_tree_t",
2237 	    "mdb_range_tree_t", ms.ms_freeing, 0) == -1)
2238 		return (WALK_ERR);
2239 	sd->ms_freeing += rt.rt_space;
2240 
2241 	if (mdb_ctf_vread(&rt, "range_tree_t",
2242 	    "mdb_range_tree_t", ms.ms_freed, 0) == -1)
2243 		return (WALK_ERR);
2244 	sd->ms_freed += rt.rt_space;
2245 
2246 	if (mdb_ctf_vread(&rt, "range_tree_t",
2247 	    "mdb_range_tree_t", ms.ms_allocatable, 0) == -1)
2248 		return (WALK_ERR);
2249 	sd->ms_allocatable += rt.rt_space;
2250 
2251 	if (mdb_ctf_vread(&rt, "range_tree_t",
2252 	    "mdb_range_tree_t", ms.ms_unflushed_frees, 0) == -1)
2253 		return (WALK_ERR);
2254 	sd->ms_unflushed_frees += rt.rt_space;
2255 	ufrees = rt.rt_space;
2256 
2257 	if (mdb_ctf_vread(&rt, "range_tree_t",
2258 	    "mdb_range_tree_t", ms.ms_unflushed_allocs, 0) == -1)
2259 		return (WALK_ERR);
2260 	sd->ms_unflushed_allocs += rt.rt_space;
2261 	uallocs = rt.rt_space;
2262 
2263 	if (ms.ms_sm != 0 &&
2264 	    mdb_ctf_vread(&sm, "space_map_t",
2265 	    "mdb_space_map_t", ms.ms_sm, 0) == -1)
2266 		return (WALK_ERR);
2267 
2268 	if (sm.sm_phys != 0) {
2269 		(void) mdb_ctf_vread(&smp, "space_map_phys_t",
2270 		    "mdb_space_map_phys_t", sm.sm_phys, 0);
2271 	}
2272 
2273 	sd->ms_deferspace += ms.ms_deferspace;
2274 	sd->avail += sm.sm_size - smp.smp_alloc + ufrees - uallocs;
2275 
2276 	return (WALK_NEXT);
2277 }
2278 
2279 /*
2280  * ::spa_space [-b]
2281  *
2282  * Given a spa_t, print out it's on-disk space usage and in-core
2283  * estimates of future usage.  If -b is given, print space in bytes.
2284  * Otherwise print in megabytes.
2285  */
2286 /* ARGSUSED */
2287 static int
2288 spa_space(uintptr_t addr, uint_t flags, int argc, const mdb_arg_t *argv)
2289 {
2290 	mdb_spa_t spa;
2291 	mdb_dsl_pool_t dp;
2292 	mdb_dsl_dir_t dd;
2293 	mdb_dmu_buf_impl_t db;
2294 	mdb_dsl_dir_phys_t dsp;
2295 	space_data_t sd;
2296 	int shift = 20;
2297 	char *suffix = "M";
2298 	int bytes = B_FALSE;
2299 
2300 	if (mdb_getopts(argc, argv, 'b', MDB_OPT_SETBITS, TRUE, &bytes, NULL) !=
2301 	    argc)
2302 		return (DCMD_USAGE);
2303 	if (!(flags & DCMD_ADDRSPEC))
2304 		return (DCMD_USAGE);
2305 
2306 	if (bytes) {
2307 		shift = 0;
2308 		suffix = "";
2309 	}
2310 
2311 	if (mdb_ctf_vread(&spa, ZFS_STRUCT "spa", "mdb_spa_t",
2312 	    addr, 0) == -1 ||
2313 	    mdb_ctf_vread(&dp, ZFS_STRUCT "dsl_pool", "mdb_dsl_pool_t",
2314 	    spa.spa_dsl_pool, 0) == -1 ||
2315 	    mdb_ctf_vread(&dd, ZFS_STRUCT "dsl_dir", "mdb_dsl_dir_t",
2316 	    dp.dp_root_dir, 0) == -1 ||
2317 	    mdb_ctf_vread(&db, ZFS_STRUCT "dmu_buf_impl", "mdb_dmu_buf_impl_t",
2318 	    dd.dd_dbuf, 0) == -1 ||
2319 	    mdb_ctf_vread(&dsp, ZFS_STRUCT "dsl_dir_phys",
2320 	    "mdb_dsl_dir_phys_t", db.db.db_data, 0) == -1) {
2321 		return (DCMD_ERR);
2322 	}
2323 
2324 	mdb_printf("dd_space_towrite = %llu%s %llu%s %llu%s %llu%s\n",
2325 	    dd.dd_space_towrite[0] >> shift, suffix,
2326 	    dd.dd_space_towrite[1] >> shift, suffix,
2327 	    dd.dd_space_towrite[2] >> shift, suffix,
2328 	    dd.dd_space_towrite[3] >> shift, suffix);
2329 
2330 	mdb_printf("dd_phys.dd_used_bytes = %llu%s\n",
2331 	    dsp.dd_used_bytes >> shift, suffix);
2332 	mdb_printf("dd_phys.dd_compressed_bytes = %llu%s\n",
2333 	    dsp.dd_compressed_bytes >> shift, suffix);
2334 	mdb_printf("dd_phys.dd_uncompressed_bytes = %llu%s\n",
2335 	    dsp.dd_uncompressed_bytes >> shift, suffix);
2336 
2337 	bzero(&sd, sizeof (sd));
2338 	if (mdb_pwalk("metaslab", space_cb, &sd, addr) != 0) {
2339 		mdb_warn("can't walk metaslabs");
2340 		return (DCMD_ERR);
2341 	}
2342 
2343 	mdb_printf("ms_allocmap = %llu%s %llu%s %llu%s %llu%s\n",
2344 	    sd.ms_allocating[0] >> shift, suffix,
2345 	    sd.ms_allocating[1] >> shift, suffix,
2346 	    sd.ms_allocating[2] >> shift, suffix,
2347 	    sd.ms_allocating[3] >> shift, suffix);
2348 	mdb_printf("ms_checkpointing = %llu%s\n",
2349 	    sd.ms_checkpointing >> shift, suffix);
2350 	mdb_printf("ms_freeing = %llu%s\n",
2351 	    sd.ms_freeing >> shift, suffix);
2352 	mdb_printf("ms_freed = %llu%s\n",
2353 	    sd.ms_freed >> shift, suffix);
2354 	mdb_printf("ms_unflushed_frees = %llu%s\n",
2355 	    sd.ms_unflushed_frees >> shift, suffix);
2356 	mdb_printf("ms_unflushed_allocs = %llu%s\n",
2357 	    sd.ms_unflushed_allocs >> shift, suffix);
2358 	mdb_printf("ms_allocatable = %llu%s\n",
2359 	    sd.ms_allocatable >> shift, suffix);
2360 	mdb_printf("ms_deferspace = %llu%s\n",
2361 	    sd.ms_deferspace >> shift, suffix);
2362 	mdb_printf("current avail = %llu%s\n",
2363 	    sd.avail >> shift, suffix);
2364 
2365 	return (DCMD_OK);
2366 }
2367 
2368 typedef struct mdb_spa_aux_vdev {
2369 	int sav_count;
2370 	uintptr_t sav_vdevs;
2371 } mdb_spa_aux_vdev_t;
2372 
2373 typedef struct mdb_spa_vdevs {
2374 	uintptr_t spa_root_vdev;
2375 	mdb_spa_aux_vdev_t spa_l2cache;
2376 	mdb_spa_aux_vdev_t spa_spares;
2377 } mdb_spa_vdevs_t;
2378 
2379 static int
2380 spa_print_aux(mdb_spa_aux_vdev_t *sav, uint_t flags, mdb_arg_t *v,
2381     const char *name)
2382 {
2383 	uintptr_t *aux;
2384 	size_t len;
2385 	int ret, i;
2386 
2387 	/*
2388 	 * Iterate over aux vdevs and print those out as well.  This is a
2389 	 * little annoying because we don't have a root vdev to pass to ::vdev.
2390 	 * Instead, we print a single line and then call it for each child
2391 	 * vdev.
2392 	 */
2393 	if (sav->sav_count != 0) {
2394 		v[1].a_type = MDB_TYPE_STRING;
2395 		v[1].a_un.a_str = "-d";
2396 		v[2].a_type = MDB_TYPE_IMMEDIATE;
2397 		v[2].a_un.a_val = 2;
2398 
2399 		len = sav->sav_count * sizeof (uintptr_t);
2400 		aux = mdb_alloc(len, UM_SLEEP);
2401 		if (mdb_vread(aux, len, sav->sav_vdevs) == -1) {
2402 			mdb_free(aux, len);
2403 			mdb_warn("failed to read l2cache vdevs at %p",
2404 			    sav->sav_vdevs);
2405 			return (DCMD_ERR);
2406 		}
2407 
2408 		mdb_printf("%-?s %-9s %-12s %s\n", "-", "-", "-", name);
2409 
2410 		for (i = 0; i < sav->sav_count; i++) {
2411 			ret = mdb_call_dcmd("vdev", aux[i], flags, 3, v);
2412 			if (ret != DCMD_OK) {
2413 				mdb_free(aux, len);
2414 				return (ret);
2415 			}
2416 		}
2417 
2418 		mdb_free(aux, len);
2419 	}
2420 
2421 	return (0);
2422 }
2423 
2424 /*
2425  * ::spa_vdevs
2426  *
2427  *	-e	Include error stats
2428  *	-m	Include metaslab information
2429  *	-M	Include metaslab group information
2430  *	-h	Include histogram information (requires -m or -M)
2431  *
2432  * Print out a summarized list of vdevs for the given spa_t.
2433  * This is accomplished by invoking "::vdev -re" on the root vdev, as well as
2434  * iterating over the cache devices.
2435  */
2436 /* ARGSUSED */
2437 static int
2438 spa_vdevs(uintptr_t addr, uint_t flags, int argc, const mdb_arg_t *argv)
2439 {
2440 	mdb_arg_t v[3];
2441 	int ret;
2442 	char opts[100] = "-r";
2443 	int spa_flags = 0;
2444 
2445 	if (mdb_getopts(argc, argv,
2446 	    'e', MDB_OPT_SETBITS, SPA_FLAG_ERRORS, &spa_flags,
2447 	    'm', MDB_OPT_SETBITS, SPA_FLAG_METASLABS, &spa_flags,
2448 	    'M', MDB_OPT_SETBITS, SPA_FLAG_METASLAB_GROUPS, &spa_flags,
2449 	    'h', MDB_OPT_SETBITS, SPA_FLAG_HISTOGRAMS, &spa_flags,
2450 	    NULL) != argc)
2451 		return (DCMD_USAGE);
2452 
2453 	if (!(flags & DCMD_ADDRSPEC))
2454 		return (DCMD_USAGE);
2455 
2456 	mdb_spa_vdevs_t spa;
2457 	if (mdb_ctf_vread(&spa, "spa_t", "mdb_spa_vdevs_t", addr, 0) == -1)
2458 		return (DCMD_ERR);
2459 
2460 	/*
2461 	 * Unitialized spa_t structures can have a NULL root vdev.
2462 	 */
2463 	if (spa.spa_root_vdev == 0) {
2464 		mdb_printf("no associated vdevs\n");
2465 		return (DCMD_OK);
2466 	}
2467 
2468 	if (spa_flags & SPA_FLAG_ERRORS)
2469 		strcat(opts, "e");
2470 	if (spa_flags & SPA_FLAG_METASLABS)
2471 		strcat(opts, "m");
2472 	if (spa_flags & SPA_FLAG_METASLAB_GROUPS)
2473 		strcat(opts, "M");
2474 	if (spa_flags & SPA_FLAG_HISTOGRAMS)
2475 		strcat(opts, "h");
2476 
2477 	v[0].a_type = MDB_TYPE_STRING;
2478 	v[0].a_un.a_str = opts;
2479 
2480 	ret = mdb_call_dcmd("vdev", (uintptr_t)spa.spa_root_vdev,
2481 	    flags, 1, v);
2482 	if (ret != DCMD_OK)
2483 		return (ret);
2484 
2485 	if (spa_print_aux(&spa.spa_l2cache, flags, v, "cache") != 0 ||
2486 	    spa_print_aux(&spa.spa_spares, flags, v, "spares") != 0)
2487 		return (DCMD_ERR);
2488 
2489 	return (DCMD_OK);
2490 }
2491 
2492 /*
2493  * ::zio
2494  *
2495  * Print a summary of zio_t and all its children.  This is intended to display a
2496  * zio tree, and hence we only pick the most important pieces of information for
2497  * the main summary.  More detailed information can always be found by doing a
2498  * '::print zio' on the underlying zio_t.  The columns we display are:
2499  *
2500  *	ADDRESS  TYPE  STAGE  WAITER  TIME_ELAPSED
2501  *
2502  * The 'address' column is indented by one space for each depth level as we
2503  * descend down the tree.
2504  */
2505 
2506 #define	ZIO_MAXINDENT	7
2507 #define	ZIO_MAXWIDTH	(sizeof (uintptr_t) * 2 + ZIO_MAXINDENT)
2508 #define	ZIO_WALK_SELF	0
2509 #define	ZIO_WALK_CHILD	1
2510 #define	ZIO_WALK_PARENT	2
2511 
2512 typedef struct zio_print_args {
2513 	int	zpa_current_depth;
2514 	int	zpa_min_depth;
2515 	int	zpa_max_depth;
2516 	int	zpa_type;
2517 	uint_t	zpa_flags;
2518 } zio_print_args_t;
2519 
2520 typedef struct mdb_zio {
2521 	enum zio_type io_type;
2522 	enum zio_stage io_stage;
2523 	uintptr_t io_waiter;
2524 	uintptr_t io_spa;
2525 	struct {
2526 		struct {
2527 			uintptr_t list_next;
2528 		} list_head;
2529 	} io_parent_list;
2530 	int io_error;
2531 } mdb_zio_t;
2532 
2533 typedef struct mdb_zio_timestamp {
2534 	hrtime_t io_timestamp;
2535 } mdb_zio_timestamp_t;
2536 
2537 static int zio_child_cb(uintptr_t addr, const void *unknown, void *arg);
2538 
2539 static int
2540 zio_print_cb(uintptr_t addr, zio_print_args_t *zpa)
2541 {
2542 	mdb_ctf_id_t type_enum, stage_enum;
2543 	int indent = zpa->zpa_current_depth;
2544 	const char *type, *stage;
2545 	uintptr_t laddr;
2546 	mdb_zio_t zio;
2547 	mdb_zio_timestamp_t zio_timestamp = { 0 };
2548 
2549 	if (mdb_ctf_vread(&zio, ZFS_STRUCT "zio", "mdb_zio_t", addr, 0) == -1)
2550 		return (WALK_ERR);
2551 	(void) mdb_ctf_vread(&zio_timestamp, ZFS_STRUCT "zio",
2552 	    "mdb_zio_timestamp_t", addr, MDB_CTF_VREAD_QUIET);
2553 
2554 	if (indent > ZIO_MAXINDENT)
2555 		indent = ZIO_MAXINDENT;
2556 
2557 	if (mdb_ctf_lookup_by_name("enum zio_type", &type_enum) == -1 ||
2558 	    mdb_ctf_lookup_by_name("enum zio_stage", &stage_enum) == -1) {
2559 		mdb_warn("failed to lookup zio enums");
2560 		return (WALK_ERR);
2561 	}
2562 
2563 	if ((type = mdb_ctf_enum_name(type_enum, zio.io_type)) != NULL)
2564 		type += sizeof ("ZIO_TYPE_") - 1;
2565 	else
2566 		type = "?";
2567 
2568 	if (zio.io_error == 0) {
2569 		stage = mdb_ctf_enum_name(stage_enum, zio.io_stage);
2570 		if (stage != NULL)
2571 			stage += sizeof ("ZIO_STAGE_") - 1;
2572 		else
2573 			stage = "?";
2574 	} else {
2575 		stage = "FAILED";
2576 	}
2577 
2578 	if (zpa->zpa_current_depth >= zpa->zpa_min_depth) {
2579 		if (zpa->zpa_flags & DCMD_PIPE_OUT) {
2580 			mdb_printf("%?p\n", addr);
2581 		} else {
2582 			mdb_printf("%*s%-*p %-5s %-16s ", indent, "",
2583 			    ZIO_MAXWIDTH - indent, addr, type, stage);
2584 			if (zio.io_waiter != 0)
2585 				mdb_printf("%-16lx ", zio.io_waiter);
2586 			else
2587 				mdb_printf("%-16s ", "-");
2588 #ifdef _KERNEL
2589 			if (zio_timestamp.io_timestamp != 0) {
2590 				mdb_printf("%llums", (mdb_gethrtime() -
2591 				    zio_timestamp.io_timestamp) /
2592 				    1000000);
2593 			} else {
2594 				mdb_printf("%-12s ", "-");
2595 			}
2596 #else
2597 			mdb_printf("%-12s ", "-");
2598 #endif
2599 			mdb_printf("\n");
2600 		}
2601 	}
2602 
2603 	if (zpa->zpa_current_depth >= zpa->zpa_max_depth)
2604 		return (WALK_NEXT);
2605 
2606 	if (zpa->zpa_type == ZIO_WALK_PARENT)
2607 		laddr = addr + mdb_ctf_offsetof_by_name(ZFS_STRUCT "zio",
2608 		    "io_parent_list");
2609 	else
2610 		laddr = addr + mdb_ctf_offsetof_by_name(ZFS_STRUCT "zio",
2611 		    "io_child_list");
2612 
2613 	zpa->zpa_current_depth++;
2614 	if (mdb_pwalk("list", zio_child_cb, zpa, laddr) != 0) {
2615 		mdb_warn("failed to walk zio_t children at %p\n", laddr);
2616 		return (WALK_ERR);
2617 	}
2618 	zpa->zpa_current_depth--;
2619 
2620 	return (WALK_NEXT);
2621 }
2622 
2623 /* ARGSUSED */
2624 static int
2625 zio_child_cb(uintptr_t addr, const void *unknown, void *arg)
2626 {
2627 	zio_link_t zl;
2628 	uintptr_t ziop;
2629 	zio_print_args_t *zpa = arg;
2630 
2631 	if (mdb_vread(&zl, sizeof (zl), addr) == -1) {
2632 		mdb_warn("failed to read zio_link_t at %p", addr);
2633 		return (WALK_ERR);
2634 	}
2635 
2636 	if (zpa->zpa_type == ZIO_WALK_PARENT)
2637 		ziop = (uintptr_t)zl.zl_parent;
2638 	else
2639 		ziop = (uintptr_t)zl.zl_child;
2640 
2641 	return (zio_print_cb(ziop, zpa));
2642 }
2643 
2644 /* ARGSUSED */
2645 static int
2646 zio_print(uintptr_t addr, uint_t flags, int argc, const mdb_arg_t *argv)
2647 {
2648 	zio_print_args_t zpa = { 0 };
2649 
2650 	if (!(flags & DCMD_ADDRSPEC))
2651 		return (DCMD_USAGE);
2652 
2653 	if (mdb_getopts(argc, argv,
2654 	    'r', MDB_OPT_SETBITS, INT_MAX, &zpa.zpa_max_depth,
2655 	    'c', MDB_OPT_SETBITS, ZIO_WALK_CHILD, &zpa.zpa_type,
2656 	    'p', MDB_OPT_SETBITS, ZIO_WALK_PARENT, &zpa.zpa_type,
2657 	    NULL) != argc)
2658 		return (DCMD_USAGE);
2659 
2660 	zpa.zpa_flags = flags;
2661 	if (zpa.zpa_max_depth != 0) {
2662 		if (zpa.zpa_type == ZIO_WALK_SELF)
2663 			zpa.zpa_type = ZIO_WALK_CHILD;
2664 	} else if (zpa.zpa_type != ZIO_WALK_SELF) {
2665 		zpa.zpa_min_depth = 1;
2666 		zpa.zpa_max_depth = 1;
2667 	}
2668 
2669 	if (!(flags & DCMD_PIPE_OUT) && DCMD_HDRSPEC(flags)) {
2670 		mdb_printf("%<u>%-*s %-5s %-16s %-16s %-12s%</u>\n",
2671 		    ZIO_MAXWIDTH, "ADDRESS", "TYPE", "STAGE", "WAITER",
2672 		    "TIME_ELAPSED");
2673 	}
2674 
2675 	if (zio_print_cb(addr, &zpa) != WALK_NEXT)
2676 		return (DCMD_ERR);
2677 
2678 	return (DCMD_OK);
2679 }
2680 
2681 /*
2682  * [addr]::zio_state
2683  *
2684  * Print a summary of all zio_t structures on the system, or for a particular
2685  * pool.  This is equivalent to '::walk zio_root | ::zio'.
2686  */
2687 /*ARGSUSED*/
2688 static int
2689 zio_state(uintptr_t addr, uint_t flags, int argc, const mdb_arg_t *argv)
2690 {
2691 	/*
2692 	 * MDB will remember the last address of the pipeline, so if we don't
2693 	 * zero this we'll end up trying to walk zio structures for a
2694 	 * non-existent spa_t.
2695 	 */
2696 	if (!(flags & DCMD_ADDRSPEC))
2697 		addr = 0;
2698 
2699 	return (mdb_pwalk_dcmd("zio_root", "zio", argc, argv, addr));
2700 }
2701 
2702 
2703 typedef struct mdb_zfs_btree_hdr {
2704 	uintptr_t		bth_parent;
2705 	boolean_t		bth_core;
2706 	/*
2707 	 * For both leaf and core nodes, represents the number of elements in
2708 	 * the node. For core nodes, they will have bth_count + 1 children.
2709 	 */
2710 	uint32_t		bth_count;
2711 } mdb_zfs_btree_hdr_t;
2712 
2713 typedef struct mdb_zfs_btree_core {
2714 	mdb_zfs_btree_hdr_t	btc_hdr;
2715 	uintptr_t		btc_children[BTREE_CORE_ELEMS + 1];
2716 	uint8_t			btc_elems[];
2717 } mdb_zfs_btree_core_t;
2718 
2719 typedef struct mdb_zfs_btree_leaf {
2720 	mdb_zfs_btree_hdr_t	btl_hdr;
2721 	uint8_t			btl_elems[];
2722 } mdb_zfs_btree_leaf_t;
2723 
2724 typedef struct mdb_zfs_btree {
2725 	uintptr_t		bt_root;
2726 	size_t			bt_elem_size;
2727 } mdb_zfs_btree_t;
2728 
2729 typedef struct btree_walk_data {
2730 	mdb_zfs_btree_t		bwd_btree;
2731 	mdb_zfs_btree_hdr_t	*bwd_node;
2732 	uint64_t		bwd_offset; // In units of bt_node_size
2733 } btree_walk_data_t;
2734 
2735 static uintptr_t
2736 btree_leftmost_child(uintptr_t addr, mdb_zfs_btree_hdr_t *buf)
2737 {
2738 	size_t size = offsetof(zfs_btree_core_t, btc_children) +
2739 	    sizeof (uintptr_t);
2740 	for (;;) {
2741 		if (mdb_vread(buf, size, addr) == -1) {
2742 			mdb_warn("failed to read at %p\n", addr);
2743 			return ((uintptr_t)0ULL);
2744 		}
2745 		if (!buf->bth_core)
2746 			return (addr);
2747 		mdb_zfs_btree_core_t *node = (mdb_zfs_btree_core_t *)buf;
2748 		addr = node->btc_children[0];
2749 	}
2750 }
2751 
2752 static int
2753 btree_walk_step(mdb_walk_state_t *wsp)
2754 {
2755 	btree_walk_data_t *bwd = wsp->walk_data;
2756 	size_t elem_size = bwd->bwd_btree.bt_elem_size;
2757 	if (wsp->walk_addr == 0ULL)
2758 		return (WALK_DONE);
2759 
2760 	if (!bwd->bwd_node->bth_core) {
2761 		/*
2762 		 * For the first element in a leaf node, read in the full
2763 		 * leaf, since we only had part of it read in before.
2764 		 */
2765 		if (bwd->bwd_offset == 0) {
2766 			if (mdb_vread(bwd->bwd_node, BTREE_LEAF_SIZE,
2767 			    wsp->walk_addr) == -1) {
2768 				mdb_warn("failed to read at %p\n",
2769 				    wsp->walk_addr);
2770 				return (WALK_ERR);
2771 			}
2772 		}
2773 
2774 		int status = wsp->walk_callback((uintptr_t)(wsp->walk_addr +
2775 		    offsetof(mdb_zfs_btree_leaf_t, btl_elems) +
2776 		    bwd->bwd_offset * elem_size), bwd->bwd_node,
2777 		    wsp->walk_cbdata);
2778 		if (status != WALK_NEXT)
2779 			return (status);
2780 		bwd->bwd_offset++;
2781 
2782 		/* Find the next element, if we're at the end of the leaf. */
2783 		while (bwd->bwd_offset == bwd->bwd_node->bth_count) {
2784 			uintptr_t par = bwd->bwd_node->bth_parent;
2785 			uintptr_t cur = wsp->walk_addr;
2786 			wsp->walk_addr = par;
2787 			if (par == 0ULL)
2788 				return (WALK_NEXT);
2789 
2790 			size_t size = sizeof (zfs_btree_core_t) +
2791 			    BTREE_CORE_ELEMS * elem_size;
2792 			if (mdb_vread(bwd->bwd_node, size, wsp->walk_addr) ==
2793 			    -1) {
2794 				mdb_warn("failed to read at %p\n",
2795 				    wsp->walk_addr);
2796 				return (WALK_ERR);
2797 			}
2798 			mdb_zfs_btree_core_t *node =
2799 			    (mdb_zfs_btree_core_t *)bwd->bwd_node;
2800 			int i;
2801 			for (i = 0; i <= bwd->bwd_node->bth_count; i++) {
2802 				if (node->btc_children[i] == cur)
2803 					break;
2804 			}
2805 			if (i > bwd->bwd_node->bth_count) {
2806 				mdb_warn("btree parent/child mismatch at "
2807 				    "%#lx\n", cur);
2808 				return (WALK_ERR);
2809 			}
2810 			bwd->bwd_offset = i;
2811 		}
2812 		return (WALK_NEXT);
2813 	}
2814 
2815 	if (!bwd->bwd_node->bth_core) {
2816 		mdb_warn("Invalid btree node at %#lx\n", wsp->walk_addr);
2817 		return (WALK_ERR);
2818 	}
2819 	mdb_zfs_btree_core_t *node = (mdb_zfs_btree_core_t *)bwd->bwd_node;
2820 	int status = wsp->walk_callback((uintptr_t)(wsp->walk_addr +
2821 	    offsetof(mdb_zfs_btree_core_t, btc_elems) + bwd->bwd_offset *
2822 	    elem_size), bwd->bwd_node, wsp->walk_cbdata);
2823 	if (status != WALK_NEXT)
2824 		return (status);
2825 
2826 	uintptr_t new_child = node->btc_children[bwd->bwd_offset + 1];
2827 	wsp->walk_addr = btree_leftmost_child(new_child, bwd->bwd_node);
2828 	if (wsp->walk_addr == 0ULL)
2829 		return (WALK_ERR);
2830 
2831 	bwd->bwd_offset = 0;
2832 	return (WALK_NEXT);
2833 }
2834 
2835 static int
2836 btree_walk_init(mdb_walk_state_t *wsp)
2837 {
2838 	btree_walk_data_t *bwd;
2839 
2840 	if (wsp->walk_addr == 0ULL) {
2841 		mdb_warn("must supply address of zfs_btree_t\n");
2842 		return (WALK_ERR);
2843 	}
2844 
2845 	bwd = mdb_zalloc(sizeof (btree_walk_data_t), UM_SLEEP);
2846 	if (mdb_ctf_vread(&bwd->bwd_btree, "zfs_btree_t", "mdb_zfs_btree_t",
2847 	    wsp->walk_addr, 0) == -1) {
2848 		mdb_free(bwd, sizeof (*bwd));
2849 		return (WALK_ERR);
2850 	}
2851 
2852 	if (bwd->bwd_btree.bt_elem_size == 0) {
2853 		mdb_warn("invalid or uninitialized btree at %#lx\n",
2854 		    wsp->walk_addr);
2855 		mdb_free(bwd, sizeof (*bwd));
2856 		return (WALK_ERR);
2857 	}
2858 
2859 	size_t size = MAX(BTREE_LEAF_SIZE, sizeof (zfs_btree_core_t) +
2860 	    BTREE_CORE_ELEMS * bwd->bwd_btree.bt_elem_size);
2861 	bwd->bwd_node = mdb_zalloc(size, UM_SLEEP);
2862 
2863 	uintptr_t node = (uintptr_t)bwd->bwd_btree.bt_root;
2864 	if (node == 0ULL) {
2865 		wsp->walk_addr = 0ULL;
2866 		wsp->walk_data = bwd;
2867 		return (WALK_NEXT);
2868 	}
2869 	node = btree_leftmost_child(node, bwd->bwd_node);
2870 	if (node == 0ULL) {
2871 		mdb_free(bwd->bwd_node, size);
2872 		mdb_free(bwd, sizeof (*bwd));
2873 		return (WALK_ERR);
2874 	}
2875 	bwd->bwd_offset = 0;
2876 
2877 	wsp->walk_addr = node;
2878 	wsp->walk_data = bwd;
2879 	return (WALK_NEXT);
2880 }
2881 
2882 static void
2883 btree_walk_fini(mdb_walk_state_t *wsp)
2884 {
2885 	btree_walk_data_t *bwd = (btree_walk_data_t *)wsp->walk_data;
2886 
2887 	if (bwd == NULL)
2888 		return;
2889 
2890 	size_t size = MAX(BTREE_LEAF_SIZE, sizeof (zfs_btree_core_t) +
2891 	    BTREE_CORE_ELEMS * bwd->bwd_btree.bt_elem_size);
2892 	if (bwd->bwd_node != NULL)
2893 		mdb_free(bwd->bwd_node, size);
2894 
2895 	mdb_free(bwd, sizeof (*bwd));
2896 }
2897 
2898 typedef struct mdb_multilist {
2899 	uint64_t ml_num_sublists;
2900 	uintptr_t ml_sublists;
2901 } mdb_multilist_t;
2902 
2903 static int
2904 multilist_walk_step(mdb_walk_state_t *wsp)
2905 {
2906 	return (wsp->walk_callback(wsp->walk_addr, wsp->walk_layer,
2907 	    wsp->walk_cbdata));
2908 }
2909 
2910 static int
2911 multilist_walk_init(mdb_walk_state_t *wsp)
2912 {
2913 	mdb_multilist_t ml;
2914 	ssize_t sublist_sz;
2915 	int list_offset;
2916 	size_t i;
2917 
2918 	if (wsp->walk_addr == 0) {
2919 		mdb_warn("must supply address of multilist_t\n");
2920 		return (WALK_ERR);
2921 	}
2922 
2923 	if (mdb_ctf_vread(&ml, "multilist_t", "mdb_multilist_t",
2924 	    wsp->walk_addr, 0) == -1) {
2925 		return (WALK_ERR);
2926 	}
2927 
2928 	if (ml.ml_num_sublists == 0 || ml.ml_sublists == 0) {
2929 		mdb_warn("invalid or uninitialized multilist at %#lx\n",
2930 		    wsp->walk_addr);
2931 		return (WALK_ERR);
2932 	}
2933 
2934 	/* mdb_ctf_sizeof_by_name() will print an error for us */
2935 	sublist_sz = mdb_ctf_sizeof_by_name("multilist_sublist_t");
2936 	if (sublist_sz == -1)
2937 		return (WALK_ERR);
2938 
2939 	/* mdb_ctf_offsetof_by_name will print an error for us */
2940 	list_offset = mdb_ctf_offsetof_by_name("multilist_sublist_t",
2941 	    "mls_list");
2942 	if (list_offset == -1)
2943 		return (WALK_ERR);
2944 
2945 	for (i = 0; i < ml.ml_num_sublists; i++) {
2946 		wsp->walk_addr = ml.ml_sublists + i * sublist_sz + list_offset;
2947 
2948 		if (mdb_layered_walk("list", wsp) == -1) {
2949 			mdb_warn("can't walk multilist sublist");
2950 			return (WALK_ERR);
2951 		}
2952 	}
2953 
2954 	return (WALK_NEXT);
2955 }
2956 
2957 typedef struct mdb_txg_list {
2958 	size_t		tl_offset;
2959 	uintptr_t	tl_head[TXG_SIZE];
2960 } mdb_txg_list_t;
2961 
2962 typedef struct txg_list_walk_data {
2963 	uintptr_t lw_head[TXG_SIZE];
2964 	int	lw_txgoff;
2965 	int	lw_maxoff;
2966 	size_t	lw_offset;
2967 	void	*lw_obj;
2968 } txg_list_walk_data_t;
2969 
2970 static int
2971 txg_list_walk_init_common(mdb_walk_state_t *wsp, int txg, int maxoff)
2972 {
2973 	txg_list_walk_data_t *lwd;
2974 	mdb_txg_list_t list;
2975 	int i;
2976 
2977 	lwd = mdb_alloc(sizeof (txg_list_walk_data_t), UM_SLEEP | UM_GC);
2978 	if (mdb_ctf_vread(&list, "txg_list_t", "mdb_txg_list_t", wsp->walk_addr,
2979 	    0) == -1) {
2980 		mdb_warn("failed to read txg_list_t at %#lx", wsp->walk_addr);
2981 		return (WALK_ERR);
2982 	}
2983 
2984 	for (i = 0; i < TXG_SIZE; i++)
2985 		lwd->lw_head[i] = list.tl_head[i];
2986 	lwd->lw_offset = list.tl_offset;
2987 	lwd->lw_obj = mdb_alloc(lwd->lw_offset + sizeof (txg_node_t),
2988 	    UM_SLEEP | UM_GC);
2989 	lwd->lw_txgoff = txg;
2990 	lwd->lw_maxoff = maxoff;
2991 
2992 	wsp->walk_addr = lwd->lw_head[lwd->lw_txgoff];
2993 	wsp->walk_data = lwd;
2994 
2995 	return (WALK_NEXT);
2996 }
2997 
2998 static int
2999 txg_list_walk_init(mdb_walk_state_t *wsp)
3000 {
3001 	return (txg_list_walk_init_common(wsp, 0, TXG_SIZE-1));
3002 }
3003 
3004 static int
3005 txg_list0_walk_init(mdb_walk_state_t *wsp)
3006 {
3007 	return (txg_list_walk_init_common(wsp, 0, 0));
3008 }
3009 
3010 static int
3011 txg_list1_walk_init(mdb_walk_state_t *wsp)
3012 {
3013 	return (txg_list_walk_init_common(wsp, 1, 1));
3014 }
3015 
3016 static int
3017 txg_list2_walk_init(mdb_walk_state_t *wsp)
3018 {
3019 	return (txg_list_walk_init_common(wsp, 2, 2));
3020 }
3021 
3022 static int
3023 txg_list3_walk_init(mdb_walk_state_t *wsp)
3024 {
3025 	return (txg_list_walk_init_common(wsp, 3, 3));
3026 }
3027 
3028 static int
3029 txg_list_walk_step(mdb_walk_state_t *wsp)
3030 {
3031 	txg_list_walk_data_t *lwd = wsp->walk_data;
3032 	uintptr_t addr;
3033 	txg_node_t *node;
3034 	int status;
3035 
3036 	while (wsp->walk_addr == 0 && lwd->lw_txgoff < lwd->lw_maxoff) {
3037 		lwd->lw_txgoff++;
3038 		wsp->walk_addr = lwd->lw_head[lwd->lw_txgoff];
3039 	}
3040 
3041 	if (wsp->walk_addr == 0)
3042 		return (WALK_DONE);
3043 
3044 	addr = wsp->walk_addr - lwd->lw_offset;
3045 
3046 	if (mdb_vread(lwd->lw_obj,
3047 	    lwd->lw_offset + sizeof (txg_node_t), addr) == -1) {
3048 		mdb_warn("failed to read list element at %#lx", addr);
3049 		return (WALK_ERR);
3050 	}
3051 
3052 	status = wsp->walk_callback(addr, lwd->lw_obj, wsp->walk_cbdata);
3053 	node = (txg_node_t *)((uintptr_t)lwd->lw_obj + lwd->lw_offset);
3054 	wsp->walk_addr = (uintptr_t)node->tn_next[lwd->lw_txgoff];
3055 
3056 	return (status);
3057 }
3058 
3059 /*
3060  * ::walk spa
3061  *
3062  * Walk all named spa_t structures in the namespace.  This is nothing more than
3063  * a layered avl walk.
3064  */
3065 static int
3066 spa_walk_init(mdb_walk_state_t *wsp)
3067 {
3068 	GElf_Sym sym;
3069 
3070 	if (wsp->walk_addr != 0) {
3071 		mdb_warn("spa walk only supports global walks\n");
3072 		return (WALK_ERR);
3073 	}
3074 
3075 	if (mdb_lookup_by_obj(ZFS_OBJ_NAME, "spa_namespace_avl", &sym) == -1) {
3076 		mdb_warn("failed to find symbol 'spa_namespace_avl'");
3077 		return (WALK_ERR);
3078 	}
3079 
3080 	wsp->walk_addr = (uintptr_t)sym.st_value;
3081 
3082 	if (mdb_layered_walk("avl", wsp) == -1) {
3083 		mdb_warn("failed to walk 'avl'\n");
3084 		return (WALK_ERR);
3085 	}
3086 
3087 	return (WALK_NEXT);
3088 }
3089 
3090 static int
3091 spa_walk_step(mdb_walk_state_t *wsp)
3092 {
3093 	return (wsp->walk_callback(wsp->walk_addr, NULL, wsp->walk_cbdata));
3094 }
3095 
3096 /*
3097  * [addr]::walk zio
3098  *
3099  * Walk all active zio_t structures on the system.  This is simply a layered
3100  * walk on top of ::walk zio_cache, with the optional ability to limit the
3101  * structures to a particular pool.
3102  */
3103 static int
3104 zio_walk_init(mdb_walk_state_t *wsp)
3105 {
3106 	wsp->walk_data = (void *)wsp->walk_addr;
3107 
3108 	if (mdb_layered_walk("zio_cache", wsp) == -1) {
3109 		mdb_warn("failed to walk 'zio_cache'\n");
3110 		return (WALK_ERR);
3111 	}
3112 
3113 	return (WALK_NEXT);
3114 }
3115 
3116 static int
3117 zio_walk_step(mdb_walk_state_t *wsp)
3118 {
3119 	mdb_zio_t zio;
3120 	uintptr_t spa = (uintptr_t)wsp->walk_data;
3121 
3122 	if (mdb_ctf_vread(&zio, ZFS_STRUCT "zio", "mdb_zio_t",
3123 	    wsp->walk_addr, 0) == -1)
3124 		return (WALK_ERR);
3125 
3126 	if (spa != 0 && spa != zio.io_spa)
3127 		return (WALK_NEXT);
3128 
3129 	return (wsp->walk_callback(wsp->walk_addr, &zio, wsp->walk_cbdata));
3130 }
3131 
3132 /*
3133  * [addr]::walk zio_root
3134  *
3135  * Walk only root zio_t structures, optionally for a particular spa_t.
3136  */
3137 static int
3138 zio_walk_root_step(mdb_walk_state_t *wsp)
3139 {
3140 	mdb_zio_t zio;
3141 	uintptr_t spa = (uintptr_t)wsp->walk_data;
3142 
3143 	if (mdb_ctf_vread(&zio, ZFS_STRUCT "zio", "mdb_zio_t",
3144 	    wsp->walk_addr, 0) == -1)
3145 		return (WALK_ERR);
3146 
3147 	if (spa != 0 && spa != zio.io_spa)
3148 		return (WALK_NEXT);
3149 
3150 	/* If the parent list is not empty, ignore */
3151 	if (zio.io_parent_list.list_head.list_next !=
3152 	    wsp->walk_addr +
3153 	    mdb_ctf_offsetof_by_name(ZFS_STRUCT "zio", "io_parent_list") +
3154 	    mdb_ctf_offsetof_by_name("struct list", "list_head"))
3155 		return (WALK_NEXT);
3156 
3157 	return (wsp->walk_callback(wsp->walk_addr, &zio, wsp->walk_cbdata));
3158 }
3159 
3160 /*
3161  * ::zfs_blkstats
3162  *
3163  *	-v	print verbose per-level information
3164  *
3165  */
3166 static int
3167 zfs_blkstats(uintptr_t addr, uint_t flags, int argc, const mdb_arg_t *argv)
3168 {
3169 	boolean_t verbose = B_FALSE;
3170 	zfs_all_blkstats_t stats;
3171 	dmu_object_type_t t;
3172 	zfs_blkstat_t *tzb;
3173 	uint64_t ditto;
3174 
3175 	if (mdb_getopts(argc, argv,
3176 	    'v', MDB_OPT_SETBITS, TRUE, &verbose,
3177 	    NULL) != argc)
3178 		return (DCMD_USAGE);
3179 
3180 	if (!(flags & DCMD_ADDRSPEC))
3181 		return (DCMD_USAGE);
3182 
3183 	if (GETMEMB(addr, "spa", spa_dsl_pool, addr) ||
3184 	    GETMEMB(addr, "dsl_pool", dp_blkstats, addr) ||
3185 	    mdb_vread(&stats, sizeof (zfs_all_blkstats_t), addr) == -1) {
3186 		mdb_warn("failed to read data at %p;", addr);
3187 		mdb_printf("maybe no stats? run \"zpool scrub\" first.");
3188 		return (DCMD_ERR);
3189 	}
3190 
3191 	tzb = &stats.zab_type[DN_MAX_LEVELS][DMU_OT_TOTAL];
3192 	if (tzb->zb_gangs != 0) {
3193 		mdb_printf("Ganged blocks: %llu\n",
3194 		    (longlong_t)tzb->zb_gangs);
3195 	}
3196 
3197 	ditto = tzb->zb_ditto_2_of_2_samevdev + tzb->zb_ditto_2_of_3_samevdev +
3198 	    tzb->zb_ditto_3_of_3_samevdev;
3199 	if (ditto != 0) {
3200 		mdb_printf("Dittoed blocks on same vdev: %llu\n",
3201 		    (longlong_t)ditto);
3202 	}
3203 
3204 	mdb_printf("\nBlocks\tLSIZE\tPSIZE\tASIZE"
3205 	    "\t  avg\t comp\t%%Total\tType\n");
3206 
3207 	for (t = 0; t <= DMU_OT_TOTAL; t++) {
3208 		char csize[MDB_NICENUM_BUFLEN], lsize[MDB_NICENUM_BUFLEN];
3209 		char psize[MDB_NICENUM_BUFLEN], asize[MDB_NICENUM_BUFLEN];
3210 		char avg[MDB_NICENUM_BUFLEN];
3211 		char comp[MDB_NICENUM_BUFLEN], pct[MDB_NICENUM_BUFLEN];
3212 		char typename[64];
3213 		int l;
3214 
3215 
3216 		if (t == DMU_OT_DEFERRED)
3217 			strcpy(typename, "deferred free");
3218 		else if (t == DMU_OT_OTHER)
3219 			strcpy(typename, "other");
3220 		else if (t == DMU_OT_TOTAL)
3221 			strcpy(typename, "Total");
3222 		else if (enum_lookup("enum dmu_object_type",
3223 		    t, "DMU_OT_", sizeof (typename), typename) == -1) {
3224 			mdb_warn("failed to read type name");
3225 			return (DCMD_ERR);
3226 		}
3227 
3228 		if (stats.zab_type[DN_MAX_LEVELS][t].zb_asize == 0)
3229 			continue;
3230 
3231 		for (l = -1; l < DN_MAX_LEVELS; l++) {
3232 			int level = (l == -1 ? DN_MAX_LEVELS : l);
3233 			zfs_blkstat_t *zb = &stats.zab_type[level][t];
3234 
3235 			if (zb->zb_asize == 0)
3236 				continue;
3237 
3238 			/*
3239 			 * Don't print each level unless requested.
3240 			 */
3241 			if (!verbose && level != DN_MAX_LEVELS)
3242 				continue;
3243 
3244 			/*
3245 			 * If all the space is level 0, don't print the
3246 			 * level 0 separately.
3247 			 */
3248 			if (level == 0 && zb->zb_asize ==
3249 			    stats.zab_type[DN_MAX_LEVELS][t].zb_asize)
3250 				continue;
3251 
3252 			mdb_nicenum(zb->zb_count, csize);
3253 			mdb_nicenum(zb->zb_lsize, lsize);
3254 			mdb_nicenum(zb->zb_psize, psize);
3255 			mdb_nicenum(zb->zb_asize, asize);
3256 			mdb_nicenum(zb->zb_asize / zb->zb_count, avg);
3257 			(void) mdb_snprintfrac(comp, MDB_NICENUM_BUFLEN,
3258 			    zb->zb_lsize, zb->zb_psize, 2);
3259 			(void) mdb_snprintfrac(pct, MDB_NICENUM_BUFLEN,
3260 			    100 * zb->zb_asize, tzb->zb_asize, 2);
3261 
3262 			mdb_printf("%6s\t%5s\t%5s\t%5s\t%5s"
3263 			    "\t%5s\t%6s\t",
3264 			    csize, lsize, psize, asize, avg, comp, pct);
3265 
3266 			if (level == DN_MAX_LEVELS)
3267 				mdb_printf("%s\n", typename);
3268 			else
3269 				mdb_printf("  L%d %s\n",
3270 				    level, typename);
3271 		}
3272 	}
3273 
3274 	return (DCMD_OK);
3275 }
3276 
3277 typedef struct mdb_reference {
3278 	uintptr_t ref_holder;
3279 	uintptr_t ref_removed;
3280 	uint64_t ref_number;
3281 } mdb_reference_t;
3282 
3283 /* ARGSUSED */
3284 static int
3285 reference_cb(uintptr_t addr, const void *ignored, void *arg)
3286 {
3287 	mdb_reference_t ref;
3288 	boolean_t holder_is_str = B_FALSE;
3289 	char holder_str[128];
3290 	boolean_t removed = (boolean_t)arg;
3291 
3292 	if (mdb_ctf_vread(&ref, "reference_t", "mdb_reference_t", addr,
3293 	    0) == -1)
3294 		return (DCMD_ERR);
3295 
3296 	if (mdb_readstr(holder_str, sizeof (holder_str),
3297 	    ref.ref_holder) != -1)
3298 		holder_is_str = strisprint(holder_str);
3299 
3300 	if (removed)
3301 		mdb_printf("removed ");
3302 	mdb_printf("reference ");
3303 	if (ref.ref_number != 1)
3304 		mdb_printf("with count=%llu ", ref.ref_number);
3305 	mdb_printf("with tag %lx", ref.ref_holder);
3306 	if (holder_is_str)
3307 		mdb_printf(" \"%s\"", holder_str);
3308 	mdb_printf(", held at:\n");
3309 
3310 	(void) mdb_call_dcmd("whatis", addr, DCMD_ADDRSPEC, 0, NULL);
3311 
3312 	if (removed) {
3313 		mdb_printf("removed at:\n");
3314 		(void) mdb_call_dcmd("whatis", ref.ref_removed,
3315 		    DCMD_ADDRSPEC, 0, NULL);
3316 	}
3317 
3318 	mdb_printf("\n");
3319 
3320 	return (WALK_NEXT);
3321 }
3322 
3323 typedef struct mdb_zfs_refcount {
3324 	uint64_t rc_count;
3325 } mdb_zfs_refcount_t;
3326 
3327 typedef struct mdb_zfs_refcount_removed {
3328 	uint64_t rc_removed_count;
3329 } mdb_zfs_refcount_removed_t;
3330 
3331 typedef struct mdb_zfs_refcount_tracked {
3332 	boolean_t rc_tracked;
3333 } mdb_zfs_refcount_tracked_t;
3334 
3335 /* ARGSUSED */
3336 static int
3337 zfs_refcount(uintptr_t addr, uint_t flags, int argc, const mdb_arg_t *argv)
3338 {
3339 	mdb_zfs_refcount_t rc;
3340 	mdb_zfs_refcount_removed_t rcr;
3341 	mdb_zfs_refcount_tracked_t rct;
3342 	int off;
3343 	boolean_t released = B_FALSE;
3344 
3345 	if (!(flags & DCMD_ADDRSPEC))
3346 		return (DCMD_USAGE);
3347 
3348 	if (mdb_getopts(argc, argv,
3349 	    'r', MDB_OPT_SETBITS, B_TRUE, &released,
3350 	    NULL) != argc)
3351 		return (DCMD_USAGE);
3352 
3353 	if (mdb_ctf_vread(&rc, "zfs_refcount_t", "mdb_zfs_refcount_t", addr,
3354 	    0) == -1)
3355 		return (DCMD_ERR);
3356 
3357 	if (mdb_ctf_vread(&rcr, "zfs_refcount_t", "mdb_zfs_refcount_removed_t",
3358 	    addr, MDB_CTF_VREAD_QUIET) == -1) {
3359 		mdb_printf("zfs_refcount_t at %p has %llu holds (untracked)\n",
3360 		    addr, (longlong_t)rc.rc_count);
3361 		return (DCMD_OK);
3362 	}
3363 
3364 	if (mdb_ctf_vread(&rct, "zfs_refcount_t", "mdb_zfs_refcount_tracked_t",
3365 	    addr, MDB_CTF_VREAD_QUIET) == -1) {
3366 		/* If this is an old target, it might be tracked. */
3367 		rct.rc_tracked = B_TRUE;
3368 	}
3369 
3370 	mdb_printf("zfs_refcount_t at %p has %llu current holds, "
3371 	    "%llu recently released holds\n",
3372 	    addr, (longlong_t)rc.rc_count, (longlong_t)rcr.rc_removed_count);
3373 
3374 	if (rct.rc_tracked && rc.rc_count > 0)
3375 		mdb_printf("current holds:\n");
3376 	off = mdb_ctf_offsetof_by_name("zfs_refcount_t", "rc_list");
3377 	if (off == -1)
3378 		return (DCMD_ERR);
3379 	mdb_pwalk("list", reference_cb, (void*)B_FALSE, addr + off);
3380 
3381 	if (released && rcr.rc_removed_count > 0) {
3382 		mdb_printf("released holds:\n");
3383 
3384 		off = mdb_ctf_offsetof_by_name("zfs_refcount_t", "rc_removed");
3385 		if (off == -1)
3386 			return (DCMD_ERR);
3387 		mdb_pwalk("list", reference_cb, (void*)B_TRUE, addr + off);
3388 	}
3389 
3390 	return (DCMD_OK);
3391 }
3392 
3393 /* ARGSUSED */
3394 static int
3395 sa_attr_table(uintptr_t addr, uint_t flags, int argc, const mdb_arg_t *argv)
3396 {
3397 	sa_attr_table_t *table;
3398 	sa_os_t sa_os;
3399 	char *name;
3400 	int i;
3401 
3402 	if (mdb_vread(&sa_os, sizeof (sa_os_t), addr) == -1) {
3403 		mdb_warn("failed to read sa_os at %p", addr);
3404 		return (DCMD_ERR);
3405 	}
3406 
3407 	table = mdb_alloc(sizeof (sa_attr_table_t) * sa_os.sa_num_attrs,
3408 	    UM_SLEEP | UM_GC);
3409 	name = mdb_alloc(MAXPATHLEN, UM_SLEEP | UM_GC);
3410 
3411 	if (mdb_vread(table, sizeof (sa_attr_table_t) * sa_os.sa_num_attrs,
3412 	    (uintptr_t)sa_os.sa_attr_table) == -1) {
3413 		mdb_warn("failed to read sa_os at %p", addr);
3414 		return (DCMD_ERR);
3415 	}
3416 
3417 	mdb_printf("%<u>%-10s %-10s %-10s %-10s %s%</u>\n",
3418 	    "ATTR ID", "REGISTERED", "LENGTH", "BSWAP", "NAME");
3419 	for (i = 0; i != sa_os.sa_num_attrs; i++) {
3420 		mdb_readstr(name, MAXPATHLEN, (uintptr_t)table[i].sa_name);
3421 		mdb_printf("%5x   %8x %8x %8x          %-s\n",
3422 		    (int)table[i].sa_attr, (int)table[i].sa_registered,
3423 		    (int)table[i].sa_length, table[i].sa_byteswap, name);
3424 	}
3425 
3426 	return (DCMD_OK);
3427 }
3428 
3429 static int
3430 sa_get_off_table(uintptr_t addr, uint32_t **off_tab, int attr_count)
3431 {
3432 	uintptr_t idx_table;
3433 
3434 	if (GETMEMB(addr, "sa_idx_tab", sa_idx_tab, idx_table)) {
3435 		mdb_printf("can't find offset table in sa_idx_tab\n");
3436 		return (-1);
3437 	}
3438 
3439 	*off_tab = mdb_alloc(attr_count * sizeof (uint32_t),
3440 	    UM_SLEEP | UM_GC);
3441 
3442 	if (mdb_vread(*off_tab,
3443 	    attr_count * sizeof (uint32_t), idx_table) == -1) {
3444 		mdb_warn("failed to attribute offset table %p", idx_table);
3445 		return (-1);
3446 	}
3447 
3448 	return (DCMD_OK);
3449 }
3450 
3451 /*ARGSUSED*/
3452 static int
3453 sa_attr_print(uintptr_t addr, uint_t flags, int argc, const mdb_arg_t *argv)
3454 {
3455 	uint32_t *offset_tab;
3456 	int attr_count;
3457 	uint64_t attr_id;
3458 	uintptr_t attr_addr;
3459 	uintptr_t bonus_tab, spill_tab;
3460 	uintptr_t db_bonus, db_spill;
3461 	uintptr_t os, os_sa;
3462 	uintptr_t db_data;
3463 
3464 	if (argc != 1)
3465 		return (DCMD_USAGE);
3466 
3467 	if (argv[0].a_type == MDB_TYPE_STRING)
3468 		attr_id = mdb_strtoull(argv[0].a_un.a_str);
3469 	else
3470 		return (DCMD_USAGE);
3471 
3472 	if (GETMEMB(addr, "sa_handle", sa_bonus_tab, bonus_tab) ||
3473 	    GETMEMB(addr, "sa_handle", sa_spill_tab, spill_tab) ||
3474 	    GETMEMB(addr, "sa_handle", sa_os, os) ||
3475 	    GETMEMB(addr, "sa_handle", sa_bonus, db_bonus) ||
3476 	    GETMEMB(addr, "sa_handle", sa_spill, db_spill)) {
3477 		mdb_printf("Can't find necessary information in sa_handle "
3478 		    "in sa_handle\n");
3479 		return (DCMD_ERR);
3480 	}
3481 
3482 	if (GETMEMB(os, "objset", os_sa, os_sa)) {
3483 		mdb_printf("Can't find os_sa in objset\n");
3484 		return (DCMD_ERR);
3485 	}
3486 
3487 	if (GETMEMB(os_sa, "sa_os", sa_num_attrs, attr_count)) {
3488 		mdb_printf("Can't find sa_num_attrs\n");
3489 		return (DCMD_ERR);
3490 	}
3491 
3492 	if (attr_id > attr_count) {
3493 		mdb_printf("attribute id number is out of range\n");
3494 		return (DCMD_ERR);
3495 	}
3496 
3497 	if (bonus_tab) {
3498 		if (sa_get_off_table(bonus_tab, &offset_tab,
3499 		    attr_count) == -1) {
3500 			return (DCMD_ERR);
3501 		}
3502 
3503 		if (GETMEMB(db_bonus, "dmu_buf", db_data, db_data)) {
3504 			mdb_printf("can't find db_data in bonus dbuf\n");
3505 			return (DCMD_ERR);
3506 		}
3507 	}
3508 
3509 	if (bonus_tab && !TOC_ATTR_PRESENT(offset_tab[attr_id]) &&
3510 	    spill_tab == 0) {
3511 		mdb_printf("Attribute does not exist\n");
3512 		return (DCMD_ERR);
3513 	} else if (!TOC_ATTR_PRESENT(offset_tab[attr_id]) && spill_tab) {
3514 		if (sa_get_off_table(spill_tab, &offset_tab,
3515 		    attr_count) == -1) {
3516 			return (DCMD_ERR);
3517 		}
3518 		if (GETMEMB(db_spill, "dmu_buf", db_data, db_data)) {
3519 			mdb_printf("can't find db_data in spill dbuf\n");
3520 			return (DCMD_ERR);
3521 		}
3522 		if (!TOC_ATTR_PRESENT(offset_tab[attr_id])) {
3523 			mdb_printf("Attribute does not exist\n");
3524 			return (DCMD_ERR);
3525 		}
3526 	}
3527 	attr_addr = db_data + TOC_OFF(offset_tab[attr_id]);
3528 	mdb_printf("%p\n", attr_addr);
3529 	return (DCMD_OK);
3530 }
3531 
3532 /* ARGSUSED */
3533 static int
3534 zfs_ace_print_common(uintptr_t addr, uint_t flags,
3535     uint64_t id, uint32_t access_mask, uint16_t ace_flags,
3536     uint16_t ace_type, int verbose)
3537 {
3538 	if (DCMD_HDRSPEC(flags) && !verbose)
3539 		mdb_printf("%<u>%-?s %-8s %-8s %-8s %s%</u>\n",
3540 		    "ADDR", "FLAGS", "MASK", "TYPE", "ID");
3541 
3542 	if (!verbose) {
3543 		mdb_printf("%0?p %-8x %-8x %-8x %-llx\n", addr,
3544 		    ace_flags, access_mask, ace_type, id);
3545 		return (DCMD_OK);
3546 	}
3547 
3548 	switch (ace_flags & ACE_TYPE_FLAGS) {
3549 	case ACE_OWNER:
3550 		mdb_printf("owner@:");
3551 		break;
3552 	case (ACE_IDENTIFIER_GROUP | ACE_GROUP):
3553 		mdb_printf("group@:");
3554 		break;
3555 	case ACE_EVERYONE:
3556 		mdb_printf("everyone@:");
3557 		break;
3558 	case ACE_IDENTIFIER_GROUP:
3559 		mdb_printf("group:%llx:", (u_longlong_t)id);
3560 		break;
3561 	case 0: /* User entry */
3562 		mdb_printf("user:%llx:", (u_longlong_t)id);
3563 		break;
3564 	}
3565 
3566 	/* print out permission mask */
3567 	if (access_mask & ACE_READ_DATA)
3568 		mdb_printf("r");
3569 	else
3570 		mdb_printf("-");
3571 	if (access_mask & ACE_WRITE_DATA)
3572 		mdb_printf("w");
3573 	else
3574 		mdb_printf("-");
3575 	if (access_mask & ACE_EXECUTE)
3576 		mdb_printf("x");
3577 	else
3578 		mdb_printf("-");
3579 	if (access_mask & ACE_APPEND_DATA)
3580 		mdb_printf("p");
3581 	else
3582 		mdb_printf("-");
3583 	if (access_mask & ACE_DELETE)
3584 		mdb_printf("d");
3585 	else
3586 		mdb_printf("-");
3587 	if (access_mask & ACE_DELETE_CHILD)
3588 		mdb_printf("D");
3589 	else
3590 		mdb_printf("-");
3591 	if (access_mask & ACE_READ_ATTRIBUTES)
3592 		mdb_printf("a");
3593 	else
3594 		mdb_printf("-");
3595 	if (access_mask & ACE_WRITE_ATTRIBUTES)
3596 		mdb_printf("A");
3597 	else
3598 		mdb_printf("-");
3599 	if (access_mask & ACE_READ_NAMED_ATTRS)
3600 		mdb_printf("R");
3601 	else
3602 		mdb_printf("-");
3603 	if (access_mask & ACE_WRITE_NAMED_ATTRS)
3604 		mdb_printf("W");
3605 	else
3606 		mdb_printf("-");
3607 	if (access_mask & ACE_READ_ACL)
3608 		mdb_printf("c");
3609 	else
3610 		mdb_printf("-");
3611 	if (access_mask & ACE_WRITE_ACL)
3612 		mdb_printf("C");
3613 	else
3614 		mdb_printf("-");
3615 	if (access_mask & ACE_WRITE_OWNER)
3616 		mdb_printf("o");
3617 	else
3618 		mdb_printf("-");
3619 	if (access_mask & ACE_SYNCHRONIZE)
3620 		mdb_printf("s");
3621 	else
3622 		mdb_printf("-");
3623 
3624 	mdb_printf(":");
3625 
3626 	/* Print out inheritance flags */
3627 	if (ace_flags & ACE_FILE_INHERIT_ACE)
3628 		mdb_printf("f");
3629 	else
3630 		mdb_printf("-");
3631 	if (ace_flags & ACE_DIRECTORY_INHERIT_ACE)
3632 		mdb_printf("d");
3633 	else
3634 		mdb_printf("-");
3635 	if (ace_flags & ACE_INHERIT_ONLY_ACE)
3636 		mdb_printf("i");
3637 	else
3638 		mdb_printf("-");
3639 	if (ace_flags & ACE_NO_PROPAGATE_INHERIT_ACE)
3640 		mdb_printf("n");
3641 	else
3642 		mdb_printf("-");
3643 	if (ace_flags & ACE_SUCCESSFUL_ACCESS_ACE_FLAG)
3644 		mdb_printf("S");
3645 	else
3646 		mdb_printf("-");
3647 	if (ace_flags & ACE_FAILED_ACCESS_ACE_FLAG)
3648 		mdb_printf("F");
3649 	else
3650 		mdb_printf("-");
3651 	if (ace_flags & ACE_INHERITED_ACE)
3652 		mdb_printf("I");
3653 	else
3654 		mdb_printf("-");
3655 
3656 	switch (ace_type) {
3657 	case ACE_ACCESS_ALLOWED_ACE_TYPE:
3658 		mdb_printf(":allow\n");
3659 		break;
3660 	case ACE_ACCESS_DENIED_ACE_TYPE:
3661 		mdb_printf(":deny\n");
3662 		break;
3663 	case ACE_SYSTEM_AUDIT_ACE_TYPE:
3664 		mdb_printf(":audit\n");
3665 		break;
3666 	case ACE_SYSTEM_ALARM_ACE_TYPE:
3667 		mdb_printf(":alarm\n");
3668 		break;
3669 	default:
3670 		mdb_printf(":?\n");
3671 	}
3672 	return (DCMD_OK);
3673 }
3674 
3675 /* ARGSUSED */
3676 static int
3677 zfs_ace_print(uintptr_t addr, uint_t flags, int argc, const mdb_arg_t *argv)
3678 {
3679 	zfs_ace_t zace;
3680 	int verbose = FALSE;
3681 	uint64_t id;
3682 
3683 	if (!(flags & DCMD_ADDRSPEC))
3684 		return (DCMD_USAGE);
3685 
3686 	if (mdb_getopts(argc, argv,
3687 	    'v', MDB_OPT_SETBITS, TRUE, &verbose, TRUE, NULL) != argc)
3688 		return (DCMD_USAGE);
3689 
3690 	if (mdb_vread(&zace, sizeof (zfs_ace_t), addr) == -1) {
3691 		mdb_warn("failed to read zfs_ace_t");
3692 		return (DCMD_ERR);
3693 	}
3694 
3695 	if ((zace.z_hdr.z_flags & ACE_TYPE_FLAGS) == 0 ||
3696 	    (zace.z_hdr.z_flags & ACE_TYPE_FLAGS) == ACE_IDENTIFIER_GROUP)
3697 		id = zace.z_fuid;
3698 	else
3699 		id = -1;
3700 
3701 	return (zfs_ace_print_common(addr, flags, id, zace.z_hdr.z_access_mask,
3702 	    zace.z_hdr.z_flags, zace.z_hdr.z_type, verbose));
3703 }
3704 
3705 /* ARGSUSED */
3706 static int
3707 zfs_ace0_print(uintptr_t addr, uint_t flags, int argc, const mdb_arg_t *argv)
3708 {
3709 	ace_t ace;
3710 	uint64_t id;
3711 	int verbose = FALSE;
3712 
3713 	if (!(flags & DCMD_ADDRSPEC))
3714 		return (DCMD_USAGE);
3715 
3716 	if (mdb_getopts(argc, argv,
3717 	    'v', MDB_OPT_SETBITS, TRUE, &verbose, TRUE, NULL) != argc)
3718 		return (DCMD_USAGE);
3719 
3720 	if (mdb_vread(&ace, sizeof (ace_t), addr) == -1) {
3721 		mdb_warn("failed to read ace_t");
3722 		return (DCMD_ERR);
3723 	}
3724 
3725 	if ((ace.a_flags & ACE_TYPE_FLAGS) == 0 ||
3726 	    (ace.a_flags & ACE_TYPE_FLAGS) == ACE_IDENTIFIER_GROUP)
3727 		id = ace.a_who;
3728 	else
3729 		id = -1;
3730 
3731 	return (zfs_ace_print_common(addr, flags, id, ace.a_access_mask,
3732 	    ace.a_flags, ace.a_type, verbose));
3733 }
3734 
3735 typedef struct acl_dump_args {
3736 	int a_argc;
3737 	const mdb_arg_t *a_argv;
3738 	uint16_t a_version;
3739 	int a_flags;
3740 } acl_dump_args_t;
3741 
3742 /* ARGSUSED */
3743 static int
3744 acl_aces_cb(uintptr_t addr, const void *unknown, void *arg)
3745 {
3746 	acl_dump_args_t *acl_args = (acl_dump_args_t *)arg;
3747 
3748 	if (acl_args->a_version == 1) {
3749 		if (mdb_call_dcmd("zfs_ace", addr,
3750 		    DCMD_ADDRSPEC|acl_args->a_flags, acl_args->a_argc,
3751 		    acl_args->a_argv) != DCMD_OK) {
3752 			return (WALK_ERR);
3753 		}
3754 	} else {
3755 		if (mdb_call_dcmd("zfs_ace0", addr,
3756 		    DCMD_ADDRSPEC|acl_args->a_flags, acl_args->a_argc,
3757 		    acl_args->a_argv) != DCMD_OK) {
3758 			return (WALK_ERR);
3759 		}
3760 	}
3761 	acl_args->a_flags = DCMD_LOOP;
3762 	return (WALK_NEXT);
3763 }
3764 
3765 /* ARGSUSED */
3766 static int
3767 acl_cb(uintptr_t addr, const void *unknown, void *arg)
3768 {
3769 	acl_dump_args_t *acl_args = (acl_dump_args_t *)arg;
3770 
3771 	if (acl_args->a_version == 1) {
3772 		if (mdb_pwalk("zfs_acl_node_aces", acl_aces_cb,
3773 		    arg, addr) != 0) {
3774 			mdb_warn("can't walk ACEs");
3775 			return (DCMD_ERR);
3776 		}
3777 	} else {
3778 		if (mdb_pwalk("zfs_acl_node_aces0", acl_aces_cb,
3779 		    arg, addr) != 0) {
3780 			mdb_warn("can't walk ACEs");
3781 			return (DCMD_ERR);
3782 		}
3783 	}
3784 	return (WALK_NEXT);
3785 }
3786 
3787 /* ARGSUSED */
3788 static int
3789 zfs_acl_dump(uintptr_t addr, uint_t flags, int argc, const mdb_arg_t *argv)
3790 {
3791 	zfs_acl_t zacl;
3792 	int verbose = FALSE;
3793 	acl_dump_args_t acl_args;
3794 
3795 	if (!(flags & DCMD_ADDRSPEC))
3796 		return (DCMD_USAGE);
3797 
3798 	if (mdb_getopts(argc, argv,
3799 	    'v', MDB_OPT_SETBITS, TRUE, &verbose, NULL) != argc)
3800 		return (DCMD_USAGE);
3801 
3802 	if (mdb_vread(&zacl, sizeof (zfs_acl_t), addr) == -1) {
3803 		mdb_warn("failed to read zfs_acl_t");
3804 		return (DCMD_ERR);
3805 	}
3806 
3807 	acl_args.a_argc = argc;
3808 	acl_args.a_argv = argv;
3809 	acl_args.a_version = zacl.z_version;
3810 	acl_args.a_flags = DCMD_LOOPFIRST;
3811 
3812 	if (mdb_pwalk("zfs_acl_node", acl_cb, &acl_args, addr) != 0) {
3813 		mdb_warn("can't walk ACL");
3814 		return (DCMD_ERR);
3815 	}
3816 
3817 	return (DCMD_OK);
3818 }
3819 
3820 /* ARGSUSED */
3821 static int
3822 zfs_acl_node_walk_init(mdb_walk_state_t *wsp)
3823 {
3824 	if (wsp->walk_addr == 0) {
3825 		mdb_warn("must supply address of zfs_acl_node_t\n");
3826 		return (WALK_ERR);
3827 	}
3828 
3829 	wsp->walk_addr +=
3830 	    mdb_ctf_offsetof_by_name(ZFS_STRUCT "zfs_acl", "z_acl");
3831 
3832 	if (mdb_layered_walk("list", wsp) == -1) {
3833 		mdb_warn("failed to walk 'list'\n");
3834 		return (WALK_ERR);
3835 	}
3836 
3837 	return (WALK_NEXT);
3838 }
3839 
3840 static int
3841 zfs_acl_node_walk_step(mdb_walk_state_t *wsp)
3842 {
3843 	zfs_acl_node_t	aclnode;
3844 
3845 	if (mdb_vread(&aclnode, sizeof (zfs_acl_node_t),
3846 	    wsp->walk_addr) == -1) {
3847 		mdb_warn("failed to read zfs_acl_node at %p", wsp->walk_addr);
3848 		return (WALK_ERR);
3849 	}
3850 
3851 	return (wsp->walk_callback(wsp->walk_addr, &aclnode, wsp->walk_cbdata));
3852 }
3853 
3854 typedef struct ace_walk_data {
3855 	int		ace_count;
3856 	int		ace_version;
3857 } ace_walk_data_t;
3858 
3859 static int
3860 zfs_aces_walk_init_common(mdb_walk_state_t *wsp, int version,
3861     int ace_count, uintptr_t ace_data)
3862 {
3863 	ace_walk_data_t *ace_walk_data;
3864 
3865 	if (wsp->walk_addr == 0) {
3866 		mdb_warn("must supply address of zfs_acl_node_t\n");
3867 		return (WALK_ERR);
3868 	}
3869 
3870 	ace_walk_data = mdb_alloc(sizeof (ace_walk_data_t), UM_SLEEP | UM_GC);
3871 
3872 	ace_walk_data->ace_count = ace_count;
3873 	ace_walk_data->ace_version = version;
3874 
3875 	wsp->walk_addr = ace_data;
3876 	wsp->walk_data = ace_walk_data;
3877 
3878 	return (WALK_NEXT);
3879 }
3880 
3881 static int
3882 zfs_acl_node_aces_walk_init_common(mdb_walk_state_t *wsp, int version)
3883 {
3884 	static int gotid;
3885 	static mdb_ctf_id_t acl_id;
3886 	int z_ace_count;
3887 	uintptr_t z_acldata;
3888 
3889 	if (!gotid) {
3890 		if (mdb_ctf_lookup_by_name("struct zfs_acl_node",
3891 		    &acl_id) == -1) {
3892 			mdb_warn("couldn't find struct zfs_acl_node");
3893 			return (DCMD_ERR);
3894 		}
3895 		gotid = TRUE;
3896 	}
3897 
3898 	if (GETMEMBID(wsp->walk_addr, &acl_id, z_ace_count, z_ace_count)) {
3899 		return (DCMD_ERR);
3900 	}
3901 	if (GETMEMBID(wsp->walk_addr, &acl_id, z_acldata, z_acldata)) {
3902 		return (DCMD_ERR);
3903 	}
3904 
3905 	return (zfs_aces_walk_init_common(wsp, version,
3906 	    z_ace_count, z_acldata));
3907 }
3908 
3909 /* ARGSUSED */
3910 static int
3911 zfs_acl_node_aces_walk_init(mdb_walk_state_t *wsp)
3912 {
3913 	return (zfs_acl_node_aces_walk_init_common(wsp, 1));
3914 }
3915 
3916 /* ARGSUSED */
3917 static int
3918 zfs_acl_node_aces0_walk_init(mdb_walk_state_t *wsp)
3919 {
3920 	return (zfs_acl_node_aces_walk_init_common(wsp, 0));
3921 }
3922 
3923 static int
3924 zfs_aces_walk_step(mdb_walk_state_t *wsp)
3925 {
3926 	ace_walk_data_t *ace_data = wsp->walk_data;
3927 	zfs_ace_t zace;
3928 	ace_t *acep;
3929 	int status;
3930 	int entry_type;
3931 	int allow_type;
3932 	uintptr_t ptr;
3933 
3934 	if (ace_data->ace_count == 0)
3935 		return (WALK_DONE);
3936 
3937 	if (mdb_vread(&zace, sizeof (zfs_ace_t), wsp->walk_addr) == -1) {
3938 		mdb_warn("failed to read zfs_ace_t at %#lx",
3939 		    wsp->walk_addr);
3940 		return (WALK_ERR);
3941 	}
3942 
3943 	switch (ace_data->ace_version) {
3944 	case 0:
3945 		acep = (ace_t *)&zace;
3946 		entry_type = acep->a_flags & ACE_TYPE_FLAGS;
3947 		allow_type = acep->a_type;
3948 		break;
3949 	case 1:
3950 		entry_type = zace.z_hdr.z_flags & ACE_TYPE_FLAGS;
3951 		allow_type = zace.z_hdr.z_type;
3952 		break;
3953 	default:
3954 		return (WALK_ERR);
3955 	}
3956 
3957 	ptr = (uintptr_t)wsp->walk_addr;
3958 	switch (entry_type) {
3959 	case ACE_OWNER:
3960 	case ACE_EVERYONE:
3961 	case (ACE_IDENTIFIER_GROUP | ACE_GROUP):
3962 		ptr += ace_data->ace_version == 0 ?
3963 		    sizeof (ace_t) : sizeof (zfs_ace_hdr_t);
3964 		break;
3965 	case ACE_IDENTIFIER_GROUP:
3966 	default:
3967 		switch (allow_type) {
3968 		case ACE_ACCESS_ALLOWED_OBJECT_ACE_TYPE:
3969 		case ACE_ACCESS_DENIED_OBJECT_ACE_TYPE:
3970 		case ACE_SYSTEM_AUDIT_OBJECT_ACE_TYPE:
3971 		case ACE_SYSTEM_ALARM_OBJECT_ACE_TYPE:
3972 			ptr += ace_data->ace_version == 0 ?
3973 			    sizeof (ace_t) : sizeof (zfs_object_ace_t);
3974 			break;
3975 		default:
3976 			ptr += ace_data->ace_version == 0 ?
3977 			    sizeof (ace_t) : sizeof (zfs_ace_t);
3978 			break;
3979 		}
3980 	}
3981 
3982 	ace_data->ace_count--;
3983 	status = wsp->walk_callback(wsp->walk_addr,
3984 	    (void *)(uintptr_t)&zace, wsp->walk_cbdata);
3985 
3986 	wsp->walk_addr = ptr;
3987 	return (status);
3988 }
3989 
3990 typedef struct mdb_zfs_rrwlock {
3991 	uintptr_t	rr_writer;
3992 	boolean_t	rr_writer_wanted;
3993 } mdb_zfs_rrwlock_t;
3994 
3995 static uint_t rrw_key;
3996 
3997 /* ARGSUSED */
3998 static int
3999 rrwlock(uintptr_t addr, uint_t flags, int argc, const mdb_arg_t *argv)
4000 {
4001 	mdb_zfs_rrwlock_t rrw;
4002 
4003 	if (rrw_key == 0) {
4004 		if (mdb_ctf_readsym(&rrw_key, "uint_t", "rrw_tsd_key", 0) == -1)
4005 			return (DCMD_ERR);
4006 	}
4007 
4008 	if (mdb_ctf_vread(&rrw, "rrwlock_t", "mdb_zfs_rrwlock_t", addr,
4009 	    0) == -1)
4010 		return (DCMD_ERR);
4011 
4012 	if (rrw.rr_writer != 0) {
4013 		mdb_printf("write lock held by thread %lx\n", rrw.rr_writer);
4014 		return (DCMD_OK);
4015 	}
4016 
4017 	if (rrw.rr_writer_wanted) {
4018 		mdb_printf("writer wanted\n");
4019 	}
4020 
4021 	mdb_printf("anonymous references:\n");
4022 	(void) mdb_call_dcmd("zfs_refcount", addr +
4023 	    mdb_ctf_offsetof_by_name(ZFS_STRUCT "rrwlock", "rr_anon_rcount"),
4024 	    DCMD_ADDRSPEC, 0, NULL);
4025 
4026 	mdb_printf("linked references:\n");
4027 	(void) mdb_call_dcmd("zfs_refcount", addr +
4028 	    mdb_ctf_offsetof_by_name(ZFS_STRUCT "rrwlock", "rr_linked_rcount"),
4029 	    DCMD_ADDRSPEC, 0, NULL);
4030 
4031 	/*
4032 	 * XXX This should find references from
4033 	 * "::walk thread | ::tsd -v <rrw_key>", but there is no support
4034 	 * for programmatic consumption of dcmds, so this would be
4035 	 * difficult, potentially requiring reimplementing ::tsd (both
4036 	 * user and kernel versions) in this MDB module.
4037 	 */
4038 
4039 	return (DCMD_OK);
4040 }
4041 
4042 typedef struct mdb_arc_buf_hdr_t {
4043 	uint16_t b_psize;
4044 	uint16_t b_lsize;
4045 	struct {
4046 		uint32_t	b_bufcnt;
4047 		uintptr_t	b_state;
4048 	} b_l1hdr;
4049 } mdb_arc_buf_hdr_t;
4050 
4051 enum arc_cflags {
4052 	ARC_CFLAG_VERBOSE		= 1 << 0,
4053 	ARC_CFLAG_ANON			= 1 << 1,
4054 	ARC_CFLAG_MRU			= 1 << 2,
4055 	ARC_CFLAG_MFU			= 1 << 3,
4056 	ARC_CFLAG_BUFS			= 1 << 4,
4057 };
4058 
4059 typedef struct arc_compression_stats_data {
4060 	GElf_Sym anon_sym;	/* ARC_anon symbol */
4061 	GElf_Sym mru_sym;	/* ARC_mru symbol */
4062 	GElf_Sym mrug_sym;	/* ARC_mru_ghost symbol */
4063 	GElf_Sym mfu_sym;	/* ARC_mfu symbol */
4064 	GElf_Sym mfug_sym;	/* ARC_mfu_ghost symbol */
4065 	GElf_Sym l2c_sym;	/* ARC_l2c_only symbol */
4066 	uint64_t *anon_c_hist;	/* histogram of compressed sizes in anon */
4067 	uint64_t *anon_u_hist;	/* histogram of uncompressed sizes in anon */
4068 	uint64_t *anon_bufs;	/* histogram of buffer counts in anon state */
4069 	uint64_t *mru_c_hist;	/* histogram of compressed sizes in mru */
4070 	uint64_t *mru_u_hist;	/* histogram of uncompressed sizes in mru */
4071 	uint64_t *mru_bufs;	/* histogram of buffer counts in mru */
4072 	uint64_t *mfu_c_hist;	/* histogram of compressed sizes in mfu */
4073 	uint64_t *mfu_u_hist;	/* histogram of uncompressed sizes in mfu */
4074 	uint64_t *mfu_bufs;	/* histogram of buffer counts in mfu */
4075 	uint64_t *all_c_hist;	/* histogram of compressed anon + mru + mfu */
4076 	uint64_t *all_u_hist;	/* histogram of uncompressed anon + mru + mfu */
4077 	uint64_t *all_bufs;	/* histogram of buffer counts in all states  */
4078 	int arc_cflags;		/* arc compression flags, specified by user */
4079 	int hist_nbuckets;	/* number of buckets in each histogram */
4080 
4081 	ulong_t l1hdr_off;	/* offset of b_l1hdr in arc_buf_hdr_t */
4082 } arc_compression_stats_data_t;
4083 
4084 int
4085 highbit64(uint64_t i)
4086 {
4087 	int h = 1;
4088 
4089 	if (i == 0)
4090 		return (0);
4091 	if (i & 0xffffffff00000000ULL) {
4092 		h += 32; i >>= 32;
4093 	}
4094 	if (i & 0xffff0000) {
4095 		h += 16; i >>= 16;
4096 	}
4097 	if (i & 0xff00) {
4098 		h += 8; i >>= 8;
4099 	}
4100 	if (i & 0xf0) {
4101 		h += 4; i >>= 4;
4102 	}
4103 	if (i & 0xc) {
4104 		h += 2; i >>= 2;
4105 	}
4106 	if (i & 0x2) {
4107 		h += 1;
4108 	}
4109 	return (h);
4110 }
4111 
4112 /* ARGSUSED */
4113 static int
4114 arc_compression_stats_cb(uintptr_t addr, const void *unknown, void *arg)
4115 {
4116 	arc_compression_stats_data_t *data = arg;
4117 	arc_flags_t flags;
4118 	mdb_arc_buf_hdr_t hdr;
4119 	int cbucket, ubucket, bufcnt;
4120 
4121 	/*
4122 	 * mdb_ctf_vread() uses the sizeof the target type (e.g.
4123 	 * sizeof (arc_buf_hdr_t) in the target) to read in the entire contents
4124 	 * of the target type into a buffer and then copy the values of the
4125 	 * desired members from the mdb typename (e.g. mdb_arc_buf_hdr_t) from
4126 	 * this buffer. Unfortunately, the way arc_buf_hdr_t is used by zfs,
4127 	 * the actual size allocated by the kernel for arc_buf_hdr_t is often
4128 	 * smaller than `sizeof (arc_buf_hdr_t)` (see the definitions of
4129 	 * l1arc_buf_hdr_t and arc_buf_hdr_t in
4130 	 * usr/src/uts/common/fs/zfs/arc.c). Attempting to read the entire
4131 	 * contents of arc_buf_hdr_t from the target (as mdb_ctf_vread() does)
4132 	 * can cause an error if the allocated size is indeed smaller--it's
4133 	 * possible that the 'missing' trailing members of arc_buf_hdr_t
4134 	 * (l1arc_buf_hdr_t and/or arc_buf_hdr_crypt_t) may fall into unmapped
4135 	 * memory.
4136 	 *
4137 	 * We use the GETMEMB macro instead which performs an mdb_vread()
4138 	 * but only reads enough of the target to retrieve the desired struct
4139 	 * member instead of the entire struct.
4140 	 */
4141 	if (GETMEMB(addr, "arc_buf_hdr", b_flags, flags) == -1)
4142 		return (WALK_ERR);
4143 
4144 	/*
4145 	 * We only count headers that have data loaded in the kernel.
4146 	 * This means an L1 header must be present as well as the data
4147 	 * that corresponds to the L1 header. If there's no L1 header,
4148 	 * we can skip the arc_buf_hdr_t completely. If it's present, we
4149 	 * must look at the ARC state (b_l1hdr.b_state) to determine if
4150 	 * the data is present.
4151 	 */
4152 	if ((flags & ARC_FLAG_HAS_L1HDR) == 0)
4153 		return (WALK_NEXT);
4154 
4155 	if (GETMEMB(addr, "arc_buf_hdr", b_psize, hdr.b_psize) == -1 ||
4156 	    GETMEMB(addr, "arc_buf_hdr", b_lsize, hdr.b_lsize) == -1 ||
4157 	    GETMEMB(addr + data->l1hdr_off, "l1arc_buf_hdr", b_bufcnt,
4158 	    hdr.b_l1hdr.b_bufcnt) == -1 ||
4159 	    GETMEMB(addr + data->l1hdr_off, "l1arc_buf_hdr", b_state,
4160 	    hdr.b_l1hdr.b_state) == -1)
4161 		return (WALK_ERR);
4162 
4163 	/*
4164 	 * Headers in the ghost states, or the l2c_only state don't have
4165 	 * arc buffers linked off of them. Thus, their compressed size
4166 	 * is meaningless, so we skip these from the stats.
4167 	 */
4168 	if (hdr.b_l1hdr.b_state == data->mrug_sym.st_value ||
4169 	    hdr.b_l1hdr.b_state == data->mfug_sym.st_value ||
4170 	    hdr.b_l1hdr.b_state == data->l2c_sym.st_value) {
4171 		return (WALK_NEXT);
4172 	}
4173 
4174 	/*
4175 	 * The physical size (compressed) and logical size
4176 	 * (uncompressed) are in units of SPA_MINBLOCKSIZE. By default,
4177 	 * we use the log2 of this value (rounded down to the nearest
4178 	 * integer) to determine the bucket to assign this header to.
4179 	 * Thus, the histogram is logarithmic with respect to the size
4180 	 * of the header. For example, the following is a mapping of the
4181 	 * bucket numbers and the range of header sizes they correspond to:
4182 	 *
4183 	 *	0: 0 byte headers
4184 	 *	1: 512 byte headers
4185 	 *	2: [1024 - 2048) byte headers
4186 	 *	3: [2048 - 4096) byte headers
4187 	 *	4: [4096 - 8192) byte headers
4188 	 *	5: [8192 - 16394) byte headers
4189 	 *	6: [16384 - 32768) byte headers
4190 	 *	7: [32768 - 65536) byte headers
4191 	 *	8: [65536 - 131072) byte headers
4192 	 *	9: 131072 byte headers
4193 	 *
4194 	 * If the ARC_CFLAG_VERBOSE flag was specified, we use the
4195 	 * physical and logical sizes directly. Thus, the histogram will
4196 	 * no longer be logarithmic; instead it will be linear with
4197 	 * respect to the size of the header. The following is a mapping
4198 	 * of the first many bucket numbers and the header size they
4199 	 * correspond to:
4200 	 *
4201 	 *	0: 0 byte headers
4202 	 *	1: 512 byte headers
4203 	 *	2: 1024 byte headers
4204 	 *	3: 1536 byte headers
4205 	 *	4: 2048 byte headers
4206 	 *	5: 2560 byte headers
4207 	 *	6: 3072 byte headers
4208 	 *
4209 	 * And so on. Keep in mind that a range of sizes isn't used in
4210 	 * the case of linear scale because the headers can only
4211 	 * increment or decrement in sizes of 512 bytes. So, it's not
4212 	 * possible for a header to be sized in between whats listed
4213 	 * above.
4214 	 *
4215 	 * Also, the above mapping values were calculated assuming a
4216 	 * SPA_MINBLOCKSHIFT of 512 bytes and a SPA_MAXBLOCKSIZE of 128K.
4217 	 */
4218 
4219 	if (data->arc_cflags & ARC_CFLAG_VERBOSE) {
4220 		cbucket = hdr.b_psize;
4221 		ubucket = hdr.b_lsize;
4222 	} else {
4223 		cbucket = highbit64(hdr.b_psize);
4224 		ubucket = highbit64(hdr.b_lsize);
4225 	}
4226 
4227 	bufcnt = hdr.b_l1hdr.b_bufcnt;
4228 	if (bufcnt >= data->hist_nbuckets)
4229 		bufcnt = data->hist_nbuckets - 1;
4230 
4231 	/* Ensure we stay within the bounds of the histogram array */
4232 	ASSERT3U(cbucket, <, data->hist_nbuckets);
4233 	ASSERT3U(ubucket, <, data->hist_nbuckets);
4234 
4235 	if (hdr.b_l1hdr.b_state == data->anon_sym.st_value) {
4236 		data->anon_c_hist[cbucket]++;
4237 		data->anon_u_hist[ubucket]++;
4238 		data->anon_bufs[bufcnt]++;
4239 	} else if (hdr.b_l1hdr.b_state == data->mru_sym.st_value) {
4240 		data->mru_c_hist[cbucket]++;
4241 		data->mru_u_hist[ubucket]++;
4242 		data->mru_bufs[bufcnt]++;
4243 	} else if (hdr.b_l1hdr.b_state == data->mfu_sym.st_value) {
4244 		data->mfu_c_hist[cbucket]++;
4245 		data->mfu_u_hist[ubucket]++;
4246 		data->mfu_bufs[bufcnt]++;
4247 	}
4248 
4249 	data->all_c_hist[cbucket]++;
4250 	data->all_u_hist[ubucket]++;
4251 	data->all_bufs[bufcnt]++;
4252 
4253 	return (WALK_NEXT);
4254 }
4255 
4256 /* ARGSUSED */
4257 static int
4258 arc_compression_stats(uintptr_t addr, uint_t flags, int argc,
4259     const mdb_arg_t *argv)
4260 {
4261 	arc_compression_stats_data_t data = { 0 };
4262 	unsigned int max_shifted = SPA_MAXBLOCKSIZE >> SPA_MINBLOCKSHIFT;
4263 	unsigned int hist_size;
4264 	char range[32];
4265 	int rc = DCMD_OK;
4266 	int off;
4267 
4268 	if (mdb_getopts(argc, argv,
4269 	    'v', MDB_OPT_SETBITS, ARC_CFLAG_VERBOSE, &data.arc_cflags,
4270 	    'a', MDB_OPT_SETBITS, ARC_CFLAG_ANON, &data.arc_cflags,
4271 	    'b', MDB_OPT_SETBITS, ARC_CFLAG_BUFS, &data.arc_cflags,
4272 	    'r', MDB_OPT_SETBITS, ARC_CFLAG_MRU, &data.arc_cflags,
4273 	    'f', MDB_OPT_SETBITS, ARC_CFLAG_MFU, &data.arc_cflags,
4274 	    NULL) != argc)
4275 		return (DCMD_USAGE);
4276 
4277 	if (mdb_lookup_by_obj(ZFS_OBJ_NAME, "ARC_anon", &data.anon_sym) ||
4278 	    mdb_lookup_by_obj(ZFS_OBJ_NAME, "ARC_mru", &data.mru_sym) ||
4279 	    mdb_lookup_by_obj(ZFS_OBJ_NAME, "ARC_mru_ghost", &data.mrug_sym) ||
4280 	    mdb_lookup_by_obj(ZFS_OBJ_NAME, "ARC_mfu", &data.mfu_sym) ||
4281 	    mdb_lookup_by_obj(ZFS_OBJ_NAME, "ARC_mfu_ghost", &data.mfug_sym) ||
4282 	    mdb_lookup_by_obj(ZFS_OBJ_NAME, "ARC_l2c_only", &data.l2c_sym)) {
4283 		mdb_warn("can't find arc state symbol");
4284 		return (DCMD_ERR);
4285 	}
4286 
4287 	/*
4288 	 * Determine the maximum expected size for any header, and use
4289 	 * this to determine the number of buckets needed for each
4290 	 * histogram. If ARC_CFLAG_VERBOSE is specified, this value is
4291 	 * used directly; otherwise the log2 of the maximum size is
4292 	 * used. Thus, if using a log2 scale there's a maximum of 10
4293 	 * possible buckets, while the linear scale (when using
4294 	 * ARC_CFLAG_VERBOSE) has a maximum of 257 buckets.
4295 	 */
4296 	if (data.arc_cflags & ARC_CFLAG_VERBOSE)
4297 		data.hist_nbuckets = max_shifted + 1;
4298 	else
4299 		data.hist_nbuckets = highbit64(max_shifted) + 1;
4300 
4301 	hist_size = sizeof (uint64_t) * data.hist_nbuckets;
4302 
4303 	data.anon_c_hist = mdb_zalloc(hist_size, UM_SLEEP);
4304 	data.anon_u_hist = mdb_zalloc(hist_size, UM_SLEEP);
4305 	data.anon_bufs = mdb_zalloc(hist_size, UM_SLEEP);
4306 
4307 	data.mru_c_hist = mdb_zalloc(hist_size, UM_SLEEP);
4308 	data.mru_u_hist = mdb_zalloc(hist_size, UM_SLEEP);
4309 	data.mru_bufs = mdb_zalloc(hist_size, UM_SLEEP);
4310 
4311 	data.mfu_c_hist = mdb_zalloc(hist_size, UM_SLEEP);
4312 	data.mfu_u_hist = mdb_zalloc(hist_size, UM_SLEEP);
4313 	data.mfu_bufs = mdb_zalloc(hist_size, UM_SLEEP);
4314 
4315 	data.all_c_hist = mdb_zalloc(hist_size, UM_SLEEP);
4316 	data.all_u_hist = mdb_zalloc(hist_size, UM_SLEEP);
4317 	data.all_bufs = mdb_zalloc(hist_size, UM_SLEEP);
4318 
4319 	if ((off = mdb_ctf_offsetof_by_name(ZFS_STRUCT "arc_buf_hdr",
4320 	    "b_l1hdr")) == -1) {
4321 		mdb_warn("could not get offset of b_l1hdr from arc_buf_hdr_t");
4322 		rc = DCMD_ERR;
4323 		goto out;
4324 	}
4325 	data.l1hdr_off = off;
4326 
4327 	if (mdb_walk("arc_buf_hdr_t_full", arc_compression_stats_cb,
4328 	    &data) != 0) {
4329 		mdb_warn("can't walk arc_buf_hdr's");
4330 		rc = DCMD_ERR;
4331 		goto out;
4332 	}
4333 
4334 	if (data.arc_cflags & ARC_CFLAG_VERBOSE) {
4335 		rc = mdb_snprintf(range, sizeof (range),
4336 		    "[n*%llu, (n+1)*%llu)", SPA_MINBLOCKSIZE,
4337 		    SPA_MINBLOCKSIZE);
4338 	} else {
4339 		rc = mdb_snprintf(range, sizeof (range),
4340 		    "[2^(n-1)*%llu, 2^n*%llu)", SPA_MINBLOCKSIZE,
4341 		    SPA_MINBLOCKSIZE);
4342 	}
4343 
4344 	if (rc < 0) {
4345 		/* snprintf failed, abort the dcmd */
4346 		rc = DCMD_ERR;
4347 		goto out;
4348 	} else {
4349 		/* snprintf succeeded above, reset return code */
4350 		rc = DCMD_OK;
4351 	}
4352 
4353 	if (data.arc_cflags & ARC_CFLAG_ANON) {
4354 		if (data.arc_cflags & ARC_CFLAG_BUFS) {
4355 			mdb_printf("Histogram of the number of anon buffers "
4356 			    "that are associated with an arc hdr.\n");
4357 			dump_histogram(data.anon_bufs, data.hist_nbuckets, 0);
4358 			mdb_printf("\n");
4359 		}
4360 		mdb_printf("Histogram of compressed anon buffers.\n"
4361 		    "Each bucket represents buffers of size: %s.\n", range);
4362 		dump_histogram(data.anon_c_hist, data.hist_nbuckets, 0);
4363 		mdb_printf("\n");
4364 
4365 		mdb_printf("Histogram of uncompressed anon buffers.\n"
4366 		    "Each bucket represents buffers of size: %s.\n", range);
4367 		dump_histogram(data.anon_u_hist, data.hist_nbuckets, 0);
4368 		mdb_printf("\n");
4369 	}
4370 
4371 	if (data.arc_cflags & ARC_CFLAG_MRU) {
4372 		if (data.arc_cflags & ARC_CFLAG_BUFS) {
4373 			mdb_printf("Histogram of the number of mru buffers "
4374 			    "that are associated with an arc hdr.\n");
4375 			dump_histogram(data.mru_bufs, data.hist_nbuckets, 0);
4376 			mdb_printf("\n");
4377 		}
4378 		mdb_printf("Histogram of compressed mru buffers.\n"
4379 		    "Each bucket represents buffers of size: %s.\n", range);
4380 		dump_histogram(data.mru_c_hist, data.hist_nbuckets, 0);
4381 		mdb_printf("\n");
4382 
4383 		mdb_printf("Histogram of uncompressed mru buffers.\n"
4384 		    "Each bucket represents buffers of size: %s.\n", range);
4385 		dump_histogram(data.mru_u_hist, data.hist_nbuckets, 0);
4386 		mdb_printf("\n");
4387 	}
4388 
4389 	if (data.arc_cflags & ARC_CFLAG_MFU) {
4390 		if (data.arc_cflags & ARC_CFLAG_BUFS) {
4391 			mdb_printf("Histogram of the number of mfu buffers "
4392 			    "that are associated with an arc hdr.\n");
4393 			dump_histogram(data.mfu_bufs, data.hist_nbuckets, 0);
4394 			mdb_printf("\n");
4395 		}
4396 
4397 		mdb_printf("Histogram of compressed mfu buffers.\n"
4398 		    "Each bucket represents buffers of size: %s.\n", range);
4399 		dump_histogram(data.mfu_c_hist, data.hist_nbuckets, 0);
4400 		mdb_printf("\n");
4401 
4402 		mdb_printf("Histogram of uncompressed mfu buffers.\n"
4403 		    "Each bucket represents buffers of size: %s.\n", range);
4404 		dump_histogram(data.mfu_u_hist, data.hist_nbuckets, 0);
4405 		mdb_printf("\n");
4406 	}
4407 
4408 	if (data.arc_cflags & ARC_CFLAG_BUFS) {
4409 		mdb_printf("Histogram of all buffers that "
4410 		    "are associated with an arc hdr.\n");
4411 		dump_histogram(data.all_bufs, data.hist_nbuckets, 0);
4412 		mdb_printf("\n");
4413 	}
4414 
4415 	mdb_printf("Histogram of all compressed buffers.\n"
4416 	    "Each bucket represents buffers of size: %s.\n", range);
4417 	dump_histogram(data.all_c_hist, data.hist_nbuckets, 0);
4418 	mdb_printf("\n");
4419 
4420 	mdb_printf("Histogram of all uncompressed buffers.\n"
4421 	    "Each bucket represents buffers of size: %s.\n", range);
4422 	dump_histogram(data.all_u_hist, data.hist_nbuckets, 0);
4423 
4424 out:
4425 	mdb_free(data.anon_c_hist, hist_size);
4426 	mdb_free(data.anon_u_hist, hist_size);
4427 	mdb_free(data.anon_bufs, hist_size);
4428 
4429 	mdb_free(data.mru_c_hist, hist_size);
4430 	mdb_free(data.mru_u_hist, hist_size);
4431 	mdb_free(data.mru_bufs, hist_size);
4432 
4433 	mdb_free(data.mfu_c_hist, hist_size);
4434 	mdb_free(data.mfu_u_hist, hist_size);
4435 	mdb_free(data.mfu_bufs, hist_size);
4436 
4437 	mdb_free(data.all_c_hist, hist_size);
4438 	mdb_free(data.all_u_hist, hist_size);
4439 	mdb_free(data.all_bufs, hist_size);
4440 
4441 	return (rc);
4442 }
4443 
4444 typedef struct mdb_range_seg64 {
4445 	uint64_t rs_start;
4446 	uint64_t rs_end;
4447 } mdb_range_seg64_t;
4448 
4449 typedef struct mdb_range_seg32 {
4450 	uint32_t rs_start;
4451 	uint32_t rs_end;
4452 } mdb_range_seg32_t;
4453 
4454 /* ARGSUSED */
4455 static int
4456 range_tree_cb(uintptr_t addr, const void *unknown, void *arg)
4457 {
4458 	mdb_range_tree_t *rt = (mdb_range_tree_t *)arg;
4459 	uint64_t start, end;
4460 
4461 	if (rt->rt_type == RANGE_SEG64) {
4462 		mdb_range_seg64_t rs;
4463 
4464 		if (mdb_ctf_vread(&rs, ZFS_STRUCT "range_seg64",
4465 		    "mdb_range_seg64_t", addr, 0) == -1)
4466 			return (DCMD_ERR);
4467 		start = rs.rs_start;
4468 		end = rs.rs_end;
4469 	} else {
4470 		ASSERT3U(rt->rt_type, ==, RANGE_SEG32);
4471 		mdb_range_seg32_t rs;
4472 
4473 		if (mdb_ctf_vread(&rs, ZFS_STRUCT "range_seg32",
4474 		    "mdb_range_seg32_t", addr, 0) == -1)
4475 			return (DCMD_ERR);
4476 		start = ((uint64_t)rs.rs_start << rt->rt_shift) + rt->rt_start;
4477 		end = ((uint64_t)rs.rs_end << rt->rt_shift) + rt->rt_start;
4478 	}
4479 
4480 	mdb_printf("\t[%llx %llx) (length %llx)\n", start, end, end - start);
4481 
4482 	return (0);
4483 }
4484 
4485 /* ARGSUSED */
4486 static int
4487 range_tree(uintptr_t addr, uint_t flags, int argc,
4488     const mdb_arg_t *argv)
4489 {
4490 	mdb_range_tree_t rt;
4491 	uintptr_t btree_addr;
4492 
4493 	if (!(flags & DCMD_ADDRSPEC))
4494 		return (DCMD_USAGE);
4495 
4496 	if (mdb_ctf_vread(&rt, ZFS_STRUCT "range_tree", "mdb_range_tree_t",
4497 	    addr, 0) == -1)
4498 		return (DCMD_ERR);
4499 
4500 	mdb_printf("%p: range tree of %llu entries, %llu bytes\n",
4501 	    addr, rt.rt_root.bt_num_elems, rt.rt_space);
4502 
4503 	btree_addr = addr +
4504 	    mdb_ctf_offsetof_by_name(ZFS_STRUCT "range_tree", "rt_root");
4505 
4506 	if (mdb_pwalk("zfs_btree", range_tree_cb, &rt, btree_addr) != 0) {
4507 		mdb_warn("can't walk range_tree segments");
4508 		return (DCMD_ERR);
4509 	}
4510 	return (DCMD_OK);
4511 }
4512 
4513 typedef struct mdb_spa_log_sm {
4514 	uint64_t sls_sm_obj;
4515 	uint64_t sls_txg;
4516 	uint64_t sls_nblocks;
4517 	uint64_t sls_mscount;
4518 } mdb_spa_log_sm_t;
4519 
4520 /* ARGSUSED */
4521 static int
4522 logsm_stats_cb(uintptr_t addr, const void *unknown, void *arg)
4523 {
4524 	mdb_spa_log_sm_t sls;
4525 	if (mdb_ctf_vread(&sls, ZFS_STRUCT "spa_log_sm", "mdb_spa_log_sm_t",
4526 	    addr, 0) == -1)
4527 		return (WALK_ERR);
4528 
4529 	mdb_printf("%7lld %7lld %7lld %7lld\n",
4530 	    sls.sls_txg, sls.sls_nblocks, sls.sls_mscount, sls.sls_sm_obj);
4531 
4532 	return (WALK_NEXT);
4533 }
4534 typedef struct mdb_log_summary_entry {
4535 	uint64_t lse_start;
4536 	uint64_t lse_blkcount;
4537 	uint64_t lse_mscount;
4538 } mdb_log_summary_entry_t;
4539 
4540 /* ARGSUSED */
4541 static int
4542 logsm_summary_cb(uintptr_t addr, const void *unknown, void *arg)
4543 {
4544 	mdb_log_summary_entry_t lse;
4545 	if (mdb_ctf_vread(&lse, ZFS_STRUCT "log_summary_entry",
4546 	    "mdb_log_summary_entry_t", addr, 0) == -1)
4547 		return (WALK_ERR);
4548 
4549 	mdb_printf("%7lld %7lld %7lld\n",
4550 	    lse.lse_start, lse.lse_blkcount, lse.lse_mscount);
4551 	return (WALK_NEXT);
4552 }
4553 
4554 /* ARGSUSED */
4555 static int
4556 logsm_stats(uintptr_t addr, uint_t flags, int argc, const mdb_arg_t *argv)
4557 {
4558 	if (!(flags & DCMD_ADDRSPEC))
4559 		return (DCMD_USAGE);
4560 
4561 	uintptr_t sls_avl_addr = addr +
4562 	    mdb_ctf_offsetof_by_name(ZFS_STRUCT "spa", "spa_sm_logs_by_txg");
4563 	uintptr_t summary_addr = addr +
4564 	    mdb_ctf_offsetof_by_name(ZFS_STRUCT "spa", "spa_log_summary");
4565 
4566 	mdb_printf("Log Entries:\n");
4567 	mdb_printf("%7s %7s %7s %7s\n", "txg", "blk", "ms", "obj");
4568 	if (mdb_pwalk("avl", logsm_stats_cb, NULL, sls_avl_addr) != 0)
4569 		return (DCMD_ERR);
4570 
4571 	mdb_printf("\nSummary Entries:\n");
4572 	mdb_printf("%7s %7s %7s\n", "txg", "blk", "ms");
4573 	if (mdb_pwalk("list", logsm_summary_cb, NULL, summary_addr) != 0)
4574 		return (DCMD_ERR);
4575 
4576 	return (DCMD_OK);
4577 }
4578 
4579 /*
4580  * MDB module linkage information:
4581  *
4582  * We declare a list of structures describing our dcmds, and a function
4583  * named _mdb_init to return a pointer to our module information.
4584  */
4585 
4586 static const mdb_dcmd_t dcmds[] = {
4587 	{ "arc", "[-bkmg]", "print ARC variables", arc_print },
4588 	{ "blkptr", ":", "print blkptr_t", blkptr },
4589 	{ "dva", ":", "print dva_t", dva },
4590 	{ "dbuf", ":", "print dmu_buf_impl_t", dbuf },
4591 	{ "dbuf_stats", ":", "dbuf stats", dbuf_stats },
4592 	{ "dbufs",
4593 	    "\t[-O objset_t*] [-n objset_name | \"mos\"] "
4594 	    "[-o object | \"mdn\"] \n"
4595 	    "\t[-l level] [-b blkid | \"bonus\"]",
4596 	    "find dmu_buf_impl_t's that match specified criteria", dbufs },
4597 	{ "abuf_find", "dva_word[0] dva_word[1]",
4598 	    "find arc_buf_hdr_t of a specified DVA",
4599 	    abuf_find },
4600 	{ "logsm_stats", ":", "print log space map statistics of a spa_t",
4601 	    logsm_stats},
4602 	{ "spa", "?[-cevmMh]\n"
4603 	    "\t-c display spa config\n"
4604 	    "\t-e display vdev statistics\n"
4605 	    "\t-v display vdev information\n"
4606 	    "\t-m display metaslab statistics\n"
4607 	    "\t-M display metaslab group statistics\n"
4608 	    "\t-h display histogram (requires -m or -M)\n",
4609 	    "spa_t summary", spa_print },
4610 	{ "spa_config", ":", "print spa_t configuration", spa_print_config },
4611 	{ "spa_space", ":[-b]", "print spa_t on-disk space usage", spa_space },
4612 	{ "spa_vdevs", ":[-emMh]\n"
4613 	    "\t-e display vdev statistics\n"
4614 	    "\t-m dispaly metaslab statistics\n"
4615 	    "\t-M display metaslab group statistic\n"
4616 	    "\t-h display histogram (requires -m or -M)\n",
4617 	    "given a spa_t, print vdev summary", spa_vdevs },
4618 	{ "sm_entries", "<buffer length in bytes>",
4619 	    "print out space map entries from a buffer decoded",
4620 	    sm_entries},
4621 	{ "vdev", ":[-remMh]\n"
4622 	    "\t-r display recursively\n"
4623 	    "\t-e display statistics\n"
4624 	    "\t-m display metaslab statistics (top level vdev only)\n"
4625 	    "\t-M display metaslab group statistics (top level vdev only)\n"
4626 	    "\t-h display histogram (requires -m or -M)\n",
4627 	    "vdev_t summary", vdev_print },
4628 	{ "zio", ":[-cpr]\n"
4629 	    "\t-c display children\n"
4630 	    "\t-p display parents\n"
4631 	    "\t-r display recursively",
4632 	    "zio_t summary", zio_print },
4633 	{ "zio_state", "?", "print out all zio_t structures on system or "
4634 	    "for a particular pool", zio_state },
4635 	{ "zfs_blkstats", ":[-v]",
4636 	    "given a spa_t, print block type stats from last scrub",
4637 	    zfs_blkstats },
4638 	{ "zfs_params", "", "print zfs tunable parameters", zfs_params },
4639 	{ "zfs_refcount", ":[-r]\n"
4640 	    "\t-r display recently removed references",
4641 	    "print zfs_refcount_t holders", zfs_refcount },
4642 	{ "zap_leaf", "", "print zap_leaf_phys_t", zap_leaf },
4643 	{ "zfs_aces", ":[-v]", "print all ACEs from a zfs_acl_t",
4644 	    zfs_acl_dump },
4645 	{ "zfs_ace", ":[-v]", "print zfs_ace", zfs_ace_print },
4646 	{ "zfs_ace0", ":[-v]", "print zfs_ace0", zfs_ace0_print },
4647 	{ "sa_attr_table", ":", "print SA attribute table from sa_os_t",
4648 	    sa_attr_table},
4649 	{ "sa_attr", ": attr_id",
4650 	    "print SA attribute address when given sa_handle_t", sa_attr_print},
4651 	{ "zfs_dbgmsg", ":[-var]",
4652 	    "print zfs debug log", dbgmsg},
4653 	{ "rrwlock", ":",
4654 	    "print rrwlock_t, including readers", rrwlock},
4655 	{ "metaslab_weight", "weight",
4656 	    "print metaslab weight", metaslab_weight},
4657 	{ "metaslab_trace", ":",
4658 	    "print metaslab allocation trace records", metaslab_trace},
4659 	{ "arc_compression_stats", ":[-vabrf]\n"
4660 	    "\t-v verbose, display a linearly scaled histogram\n"
4661 	    "\t-a display ARC_anon state statistics individually\n"
4662 	    "\t-r display ARC_mru state statistics individually\n"
4663 	    "\t-f display ARC_mfu state statistics individually\n"
4664 	    "\t-b display histogram of buffer counts\n",
4665 	    "print a histogram of compressed arc buffer sizes",
4666 	    arc_compression_stats},
4667 	{ "range_tree", ":",
4668 	    "print entries in range_tree_t", range_tree},
4669 	{ NULL }
4670 };
4671 
4672 static const mdb_walker_t walkers[] = {
4673 	{ "txg_list", "given any txg_list_t *, walk all entries in all txgs",
4674 	    txg_list_walk_init, txg_list_walk_step, NULL },
4675 	{ "txg_list0", "given any txg_list_t *, walk all entries in txg 0",
4676 	    txg_list0_walk_init, txg_list_walk_step, NULL },
4677 	{ "txg_list1", "given any txg_list_t *, walk all entries in txg 1",
4678 	    txg_list1_walk_init, txg_list_walk_step, NULL },
4679 	{ "txg_list2", "given any txg_list_t *, walk all entries in txg 2",
4680 	    txg_list2_walk_init, txg_list_walk_step, NULL },
4681 	{ "txg_list3", "given any txg_list_t *, walk all entries in txg 3",
4682 	    txg_list3_walk_init, txg_list_walk_step, NULL },
4683 	{ "zio", "walk all zio structures, optionally for a particular spa_t",
4684 	    zio_walk_init, zio_walk_step, NULL },
4685 	{ "zio_root",
4686 	    "walk all root zio_t structures, optionally for a particular spa_t",
4687 	    zio_walk_init, zio_walk_root_step, NULL },
4688 	{ "spa", "walk all spa_t entries in the namespace",
4689 	    spa_walk_init, spa_walk_step, NULL },
4690 	{ "metaslab", "given a spa_t *, walk all metaslab_t structures",
4691 	    metaslab_walk_init, metaslab_walk_step, NULL },
4692 	{ "multilist", "given a multilist_t *, walk all list_t structures",
4693 	    multilist_walk_init, multilist_walk_step, NULL },
4694 	{ "zfs_acl_node", "given a zfs_acl_t, walk all zfs_acl_nodes",
4695 	    zfs_acl_node_walk_init, zfs_acl_node_walk_step, NULL },
4696 	{ "zfs_acl_node_aces", "given a zfs_acl_node_t, walk all ACEs",
4697 	    zfs_acl_node_aces_walk_init, zfs_aces_walk_step, NULL },
4698 	{ "zfs_acl_node_aces0",
4699 	    "given a zfs_acl_node_t, walk all ACEs as ace_t",
4700 	    zfs_acl_node_aces0_walk_init, zfs_aces_walk_step, NULL },
4701 	{ "zfs_btree", "given a zfs_btree_t *, walk all entries",
4702 	    btree_walk_init, btree_walk_step, btree_walk_fini },
4703 	{ NULL }
4704 };
4705 
4706 static const mdb_modinfo_t modinfo = {
4707 	MDB_API_VERSION, dcmds, walkers
4708 };
4709 
4710 const mdb_modinfo_t *
4711 _mdb_init(void)
4712 {
4713 	return (&modinfo);
4714 }
4715