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