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