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