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