xref: /titanic_51/usr/src/cmd/mdb/common/modules/zfs/zfs.c (revision 90bcde942a3919300ffc73f98ea903b58386c395)
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 2008 Sun Microsystems, Inc.  All rights reserved.
23  * Use is subject to license terms.
24  */
25 
26 #pragma ident	"%Z%%M%	%I%	%E% SMI"
27 
28 #include <mdb/mdb_ctf.h>
29 #include <sys/zfs_context.h>
30 #include <sys/mdb_modapi.h>
31 #include <sys/dbuf.h>
32 #include <sys/dmu_objset.h>
33 #include <sys/dsl_dir.h>
34 #include <sys/dsl_pool.h>
35 #include <sys/metaslab_impl.h>
36 #include <sys/space_map.h>
37 #include <sys/list.h>
38 #include <sys/spa_impl.h>
39 #include <sys/vdev_impl.h>
40 #include <sys/zio_compress.h>
41 
42 #ifndef _KERNEL
43 #include "../genunix/list.h"
44 #endif
45 
46 #ifdef _KERNEL
47 #define	ZFS_OBJ_NAME	"zfs"
48 #else
49 #define	ZFS_OBJ_NAME	"libzpool.so.1"
50 #endif
51 
52 static char *
53 local_strdup(const char *s)
54 {
55 	char *s1 = mdb_alloc(strlen(s) + 1, UM_SLEEP);
56 
57 	(void) strcpy(s1, s);
58 	return (s1);
59 }
60 
61 static int
62 getmember(uintptr_t addr, const char *type, mdb_ctf_id_t *idp,
63     const char *member, int len, void *buf)
64 {
65 	mdb_ctf_id_t id;
66 	ulong_t off;
67 	char name[64];
68 
69 	if (idp == NULL) {
70 		if (mdb_ctf_lookup_by_name(type, &id) == -1) {
71 			mdb_warn("couldn't find type %s", type);
72 			return (DCMD_ERR);
73 		}
74 		idp = &id;
75 	} else {
76 		type = name;
77 		mdb_ctf_type_name(*idp, name, sizeof (name));
78 	}
79 
80 	if (mdb_ctf_offsetof(*idp, member, &off) == -1) {
81 		mdb_warn("couldn't find member %s of type %s\n", member, type);
82 		return (DCMD_ERR);
83 	}
84 	if (off % 8 != 0) {
85 		mdb_warn("member %s of type %s is unsupported bitfield",
86 		    member, type);
87 		return (DCMD_ERR);
88 	}
89 	off /= 8;
90 
91 	if (mdb_vread(buf, len, addr + off) == -1) {
92 		mdb_warn("failed to read %s from %s at %p",
93 		    member, type, addr + off);
94 		return (DCMD_ERR);
95 	}
96 	/* mdb_warn("read %s from %s at %p+%llx\n", member, type, addr, off); */
97 
98 	return (0);
99 }
100 
101 #define	GETMEMB(addr, type, member, dest) \
102 	getmember(addr, #type, NULL, #member, sizeof (dest), &(dest))
103 
104 #define	GETMEMBID(addr, ctfid, member, dest) \
105 	getmember(addr, NULL, ctfid, #member, sizeof (dest), &(dest))
106 
107 static int
108 getrefcount(uintptr_t addr, mdb_ctf_id_t *id,
109     const char *member, uint64_t *rc)
110 {
111 	static int gotid;
112 	static mdb_ctf_id_t rc_id;
113 	ulong_t off;
114 
115 	if (!gotid) {
116 		if (mdb_ctf_lookup_by_name("struct refcount", &rc_id) == -1) {
117 			mdb_warn("couldn't find struct refcount");
118 			return (DCMD_ERR);
119 		}
120 		gotid = TRUE;
121 	}
122 
123 	if (mdb_ctf_offsetof(*id, member, &off) == -1) {
124 		char name[64];
125 		mdb_ctf_type_name(*id, name, sizeof (name));
126 		mdb_warn("couldn't find member %s of type %s\n", member, name);
127 		return (DCMD_ERR);
128 	}
129 	off /= 8;
130 
131 	return (GETMEMBID(addr + off, &rc_id, rc_count, *rc));
132 }
133 
134 static int
135 read_symbol(char *sym_name, void **bufp)
136 {
137 	GElf_Sym sym;
138 
139 	if (mdb_lookup_by_obj(MDB_TGT_OBJ_EVERY, sym_name, &sym)) {
140 		mdb_warn("can't find symbol %s", sym_name);
141 		return (DCMD_ERR);
142 	}
143 
144 	*bufp = mdb_alloc(sym.st_size, UM_SLEEP);
145 
146 	if (mdb_vread(*bufp, sym.st_size, sym.st_value) == -1) {
147 		mdb_warn("can't read data for symbol %s", sym_name);
148 		mdb_free(*bufp, sym.st_size);
149 		return (DCMD_ERR);
150 	}
151 
152 	return (DCMD_OK);
153 }
154 
155 static int verbose;
156 
157 static int
158 freelist_walk_init(mdb_walk_state_t *wsp)
159 {
160 	if (wsp->walk_addr == NULL) {
161 		mdb_warn("must supply starting address\n");
162 		return (WALK_ERR);
163 	}
164 
165 	wsp->walk_data = 0;  /* Index into the freelist */
166 	return (WALK_NEXT);
167 }
168 
169 static int
170 freelist_walk_step(mdb_walk_state_t *wsp)
171 {
172 	uint64_t entry;
173 	uintptr_t number = (uintptr_t)wsp->walk_data;
174 	char *ddata[] = { "ALLOC", "FREE", "CONDENSE", "INVALID",
175 			    "INVALID", "INVALID", "INVALID", "INVALID" };
176 	int mapshift = SPA_MINBLOCKSHIFT;
177 
178 	if (mdb_vread(&entry, sizeof (entry), wsp->walk_addr) == -1) {
179 		mdb_warn("failed to read freelist entry %p", wsp->walk_addr);
180 		return (WALK_DONE);
181 	}
182 	wsp->walk_addr += sizeof (entry);
183 	wsp->walk_data = (void *)(number + 1);
184 
185 	if (SM_DEBUG_DECODE(entry)) {
186 		mdb_printf("DEBUG: %3u  %10s: txg=%llu  pass=%llu\n",
187 		    number,
188 		    ddata[SM_DEBUG_ACTION_DECODE(entry)],
189 		    SM_DEBUG_TXG_DECODE(entry),
190 		    SM_DEBUG_SYNCPASS_DECODE(entry));
191 	} else {
192 		mdb_printf("Entry: %3u  offsets=%08llx-%08llx  type=%c  "
193 		    "size=%06llx", number,
194 		    SM_OFFSET_DECODE(entry) << mapshift,
195 		    (SM_OFFSET_DECODE(entry) + SM_RUN_DECODE(entry)) <<
196 		    mapshift,
197 		    SM_TYPE_DECODE(entry) == SM_ALLOC ? 'A' : 'F',
198 		    SM_RUN_DECODE(entry) << mapshift);
199 		if (verbose)
200 			mdb_printf("      (raw=%012llx)\n", entry);
201 		mdb_printf("\n");
202 	}
203 	return (WALK_NEXT);
204 }
205 
206 
207 static int
208 dataset_name(uintptr_t addr, char *buf)
209 {
210 	static int gotid;
211 	static mdb_ctf_id_t dd_id;
212 	uintptr_t dd_parent;
213 	char dd_myname[MAXNAMELEN];
214 
215 	if (!gotid) {
216 		if (mdb_ctf_lookup_by_name("struct dsl_dir",
217 		    &dd_id) == -1) {
218 			mdb_warn("couldn't find struct dsl_dir");
219 			return (DCMD_ERR);
220 		}
221 		gotid = TRUE;
222 	}
223 	if (GETMEMBID(addr, &dd_id, dd_parent, dd_parent) ||
224 	    GETMEMBID(addr, &dd_id, dd_myname, dd_myname)) {
225 		return (DCMD_ERR);
226 	}
227 
228 	if (dd_parent) {
229 		if (dataset_name(dd_parent, buf))
230 			return (DCMD_ERR);
231 		strcat(buf, "/");
232 	}
233 
234 	if (dd_myname[0])
235 		strcat(buf, dd_myname);
236 	else
237 		strcat(buf, "???");
238 
239 	return (0);
240 }
241 
242 static int
243 objset_name(uintptr_t addr, char *buf)
244 {
245 	static int gotid;
246 	static mdb_ctf_id_t osi_id, ds_id;
247 	uintptr_t os_dsl_dataset;
248 	char ds_snapname[MAXNAMELEN];
249 	uintptr_t ds_dir;
250 
251 	buf[0] = '\0';
252 
253 	if (!gotid) {
254 		if (mdb_ctf_lookup_by_name("struct objset_impl",
255 		    &osi_id) == -1) {
256 			mdb_warn("couldn't find struct objset_impl");
257 			return (DCMD_ERR);
258 		}
259 		if (mdb_ctf_lookup_by_name("struct dsl_dataset",
260 		    &ds_id) == -1) {
261 			mdb_warn("couldn't find struct dsl_dataset");
262 			return (DCMD_ERR);
263 		}
264 
265 		gotid = TRUE;
266 	}
267 
268 	if (GETMEMBID(addr, &osi_id, os_dsl_dataset, os_dsl_dataset))
269 		return (DCMD_ERR);
270 
271 	if (os_dsl_dataset == 0) {
272 		strcat(buf, "mos");
273 		return (0);
274 	}
275 
276 	if (GETMEMBID(os_dsl_dataset, &ds_id, ds_snapname, ds_snapname) ||
277 	    GETMEMBID(os_dsl_dataset, &ds_id, ds_dir, ds_dir)) {
278 		return (DCMD_ERR);
279 	}
280 
281 	if (ds_dir && dataset_name(ds_dir, buf))
282 		return (DCMD_ERR);
283 
284 	if (ds_snapname[0]) {
285 		strcat(buf, "@");
286 		strcat(buf, ds_snapname);
287 	}
288 	return (0);
289 }
290 
291 static void
292 enum_lookup(char *out, size_t size, mdb_ctf_id_t id, int val,
293     const char *prefix)
294 {
295 	const char *cp;
296 	size_t len = strlen(prefix);
297 
298 	if ((cp = mdb_ctf_enum_name(id, val)) != NULL) {
299 		if (strncmp(cp, prefix, len) == 0)
300 			cp += len;
301 		(void) strncpy(out, cp, size);
302 	} else {
303 		mdb_snprintf(out, size, "? (%d)", val);
304 	}
305 }
306 
307 /* ARGSUSED */
308 static int
309 zio_pipeline(uintptr_t addr, uint_t flags, int argc, const mdb_arg_t *argv)
310 {
311 	mdb_ctf_id_t pipe_enum;
312 	int i;
313 	char stage[1024];
314 
315 	if (mdb_ctf_lookup_by_name("enum zio_stage", &pipe_enum) == -1) {
316 		mdb_warn("Could not find enum zio_stage");
317 		return (DCMD_ERR);
318 	}
319 
320 	for (i = 0; i < 32; i++) {
321 		if (addr & (1U << i)) {
322 			enum_lookup(stage, sizeof (stage), pipe_enum, i,
323 			    "ZIO_STAGE_");
324 			mdb_printf("    %s\n", stage);
325 		}
326 	}
327 
328 	return (DCMD_OK);
329 }
330 
331 /* ARGSUSED */
332 static int
333 zfs_params(uintptr_t addr, uint_t flags, int argc, const mdb_arg_t *argv)
334 {
335 	/*
336 	 * This table can be approximately generated by running:
337 	 * egrep "^[a-z0-9_]+ [a-z0-9_]+( =.*)?;" *.c | cut -d ' ' -f 2
338 	 */
339 	static const char *params[] = {
340 		"arc_reduce_dnlc_percent",
341 		"zfs_arc_max",
342 		"zfs_arc_min",
343 		"arc_shrink_shift",
344 		"zfs_mdcomp_disable",
345 		"zfs_prefetch_disable",
346 		"zfetch_max_streams",
347 		"zfetch_min_sec_reap",
348 		"zfetch_block_cap",
349 		"zfetch_array_rd_sz",
350 		"zfs_default_bs",
351 		"zfs_default_ibs",
352 		"metaslab_aliquot",
353 		"reference_tracking_enable",
354 		"reference_history",
355 		"zio_taskq_threads",
356 		"spa_max_replication_override",
357 		"spa_mode",
358 		"zfs_flags",
359 		"zfs_txg_synctime",
360 		"zfs_txg_timeout",
361 		"zfs_write_limit_min",
362 		"zfs_write_limit_max",
363 		"zfs_write_limit_shift",
364 		"zfs_write_limit_override",
365 		"zfs_no_write_throttle",
366 		"zfs_vdev_cache_max",
367 		"zfs_vdev_cache_size",
368 		"zfs_vdev_cache_bshift",
369 		"vdev_mirror_shift",
370 		"zfs_vdev_max_pending",
371 		"zfs_vdev_min_pending",
372 		"zfs_scrub_limit",
373 		"zfs_vdev_time_shift",
374 		"zfs_vdev_ramp_rate",
375 		"zfs_vdev_aggregation_limit",
376 		"fzap_default_block_shift",
377 		"zfs_immediate_write_sz",
378 		"zfs_read_chunk_size",
379 		"zil_disable",
380 		"zfs_nocacheflush",
381 		"metaslab_gang_bang",
382 		"zio_injection_enabled",
383 		"zvol_immediate_write_sz",
384 	};
385 	int i;
386 
387 	for (i = 0; i < sizeof (params) / sizeof (params[0]); i++) {
388 		int sz;
389 		uint64_t val64;
390 		uint32_t *val32p = (uint32_t *)&val64;
391 
392 		sz = mdb_readvar(&val64, params[i]);
393 		if (sz == 4) {
394 			mdb_printf("%s = 0x%x\n", params[i], *val32p);
395 		} else if (sz == 8) {
396 			mdb_printf("%s = 0x%llx\n", params[i], val64);
397 		} else {
398 			mdb_warn("variable %s not found", params[i]);
399 		}
400 	}
401 
402 	return (DCMD_OK);
403 }
404 
405 /* ARGSUSED */
406 static int
407 blkptr(uintptr_t addr, uint_t flags, int argc, const mdb_arg_t *argv)
408 {
409 	blkptr_t bp;
410 	dmu_object_type_info_t *doti;
411 	zio_compress_info_t *zct;
412 	zio_checksum_info_t *zci;
413 	int i;
414 	char buf[MAXPATHLEN];
415 
416 	if (mdb_vread(&bp, sizeof (blkptr_t), addr) == -1) {
417 		mdb_warn("failed to read blkptr_t");
418 		return (DCMD_ERR);
419 	}
420 
421 	if (read_symbol("dmu_ot", (void **)&doti) != DCMD_OK)
422 		return (DCMD_ERR);
423 	for (i = 0; i < DMU_OT_NUMTYPES; i++) {
424 		mdb_readstr(buf, sizeof (buf), (uintptr_t)doti[i].ot_name);
425 		doti[i].ot_name = local_strdup(buf);
426 	}
427 
428 	if (read_symbol("zio_checksum_table", (void **)&zci) != DCMD_OK)
429 		return (DCMD_ERR);
430 	for (i = 0; i < ZIO_CHECKSUM_FUNCTIONS; i++) {
431 		mdb_readstr(buf, sizeof (buf), (uintptr_t)zci[i].ci_name);
432 		zci[i].ci_name = local_strdup(buf);
433 	}
434 
435 	if (read_symbol("zio_compress_table", (void **)&zct) != DCMD_OK)
436 		return (DCMD_ERR);
437 	for (i = 0; i < ZIO_COMPRESS_FUNCTIONS; i++) {
438 		mdb_readstr(buf, sizeof (buf), (uintptr_t)zct[i].ci_name);
439 		zct[i].ci_name = local_strdup(buf);
440 	}
441 
442 	/*
443 	 * Super-ick warning:  This code is also duplicated in
444 	 * cmd/zdb.c .   Yeah, I hate code replication, too.
445 	 */
446 	for (i = 0; i < BP_GET_NDVAS(&bp); i++) {
447 		dva_t *dva = &bp.blk_dva[i];
448 
449 		mdb_printf("DVA[%d]: vdev_id %lld / %llx\n", i,
450 		    DVA_GET_VDEV(dva), DVA_GET_OFFSET(dva));
451 		mdb_printf("DVA[%d]:       GANG: %-5s  GRID:  %04x\t"
452 		    "ASIZE: %llx\n", i, DVA_GET_GANG(dva) ? "TRUE" : "FALSE",
453 		    DVA_GET_GRID(dva), DVA_GET_ASIZE(dva));
454 		mdb_printf("DVA[%d]: :%llu:%llx:%llx:%s%s%s%s\n", i,
455 		    DVA_GET_VDEV(dva), DVA_GET_OFFSET(dva), BP_GET_PSIZE(&bp),
456 		    BP_SHOULD_BYTESWAP(&bp) ? "e" : "",
457 		    !DVA_GET_GANG(dva) && BP_GET_LEVEL(&bp) != 0 ? "i" : "",
458 		    DVA_GET_GANG(dva) ? "g" : "",
459 		    BP_GET_COMPRESS(&bp) != 0 ? "d" : "");
460 	}
461 	mdb_printf("LSIZE:  %-16llx\t\tPSIZE: %llx\n",
462 	    BP_GET_LSIZE(&bp), BP_GET_PSIZE(&bp));
463 	mdb_printf("ENDIAN: %6s\t\t\t\t\tTYPE:  %s\n",
464 	    BP_GET_BYTEORDER(&bp) ? "LITTLE" : "BIG",
465 	    doti[BP_GET_TYPE(&bp)].ot_name);
466 	mdb_printf("BIRTH:  %-16llx   LEVEL: %-2d\tFILL:  %llx\n",
467 	    bp.blk_birth, BP_GET_LEVEL(&bp), bp.blk_fill);
468 	mdb_printf("CKFUNC: %-16s\t\tCOMP:  %s\n",
469 	    zci[BP_GET_CHECKSUM(&bp)].ci_name,
470 	    zct[BP_GET_COMPRESS(&bp)].ci_name);
471 	mdb_printf("CKSUM:  %llx:%llx:%llx:%llx\n",
472 	    bp.blk_cksum.zc_word[0],
473 	    bp.blk_cksum.zc_word[1],
474 	    bp.blk_cksum.zc_word[2],
475 	    bp.blk_cksum.zc_word[3]);
476 
477 	return (DCMD_OK);
478 }
479 
480 /* ARGSUSED */
481 static int
482 dbuf(uintptr_t addr, uint_t flags, int argc, const mdb_arg_t *argv)
483 {
484 	mdb_ctf_id_t id;
485 	dmu_buf_t db;
486 	uintptr_t objset;
487 	uint8_t level;
488 	uint64_t blkid;
489 	uint64_t holds;
490 	char objectname[32];
491 	char blkidname[32];
492 	char path[MAXNAMELEN];
493 
494 	if (DCMD_HDRSPEC(flags)) {
495 		mdb_printf("        addr object lvl blkid holds os\n");
496 	}
497 
498 	if (mdb_ctf_lookup_by_name("struct dmu_buf_impl", &id) == -1) {
499 		mdb_warn("couldn't find struct dmu_buf_impl_t");
500 		return (DCMD_ERR);
501 	}
502 
503 	if (GETMEMBID(addr, &id, db_objset, objset) ||
504 	    GETMEMBID(addr, &id, db, db) ||
505 	    GETMEMBID(addr, &id, db_level, level) ||
506 	    GETMEMBID(addr, &id, db_blkid, blkid)) {
507 		return (WALK_ERR);
508 	}
509 
510 	if (getrefcount(addr, &id, "db_holds", &holds)) {
511 		return (WALK_ERR);
512 	}
513 
514 	if (db.db_object == DMU_META_DNODE_OBJECT)
515 		(void) strcpy(objectname, "mdn");
516 	else
517 		(void) mdb_snprintf(objectname, sizeof (objectname), "%llx",
518 		    (u_longlong_t)db.db_object);
519 
520 	if (blkid == DB_BONUS_BLKID)
521 		(void) strcpy(blkidname, "bonus");
522 	else
523 		(void) mdb_snprintf(blkidname, sizeof (blkidname), "%llx",
524 		    (u_longlong_t)blkid);
525 
526 	if (objset_name(objset, path)) {
527 		return (WALK_ERR);
528 	}
529 
530 	mdb_printf("%p %8s %1u %9s %2llu %s\n",
531 	    addr, objectname, level, blkidname, holds, path);
532 
533 	return (DCMD_OK);
534 }
535 
536 /* ARGSUSED */
537 static int
538 dbuf_stats(uintptr_t addr, uint_t flags, int argc, const mdb_arg_t *argv)
539 {
540 #define	HISTOSZ 32
541 	uintptr_t dbp;
542 	dmu_buf_impl_t db;
543 	dbuf_hash_table_t ht;
544 	uint64_t bucket, ndbufs;
545 	uint64_t histo[HISTOSZ];
546 	uint64_t histo2[HISTOSZ];
547 	int i, maxidx;
548 
549 	if (mdb_readvar(&ht, "dbuf_hash_table") == -1) {
550 		mdb_warn("failed to read 'dbuf_hash_table'");
551 		return (DCMD_ERR);
552 	}
553 
554 	for (i = 0; i < HISTOSZ; i++) {
555 		histo[i] = 0;
556 		histo2[i] = 0;
557 	}
558 
559 	ndbufs = 0;
560 	for (bucket = 0; bucket < ht.hash_table_mask+1; bucket++) {
561 		int len;
562 
563 		if (mdb_vread(&dbp, sizeof (void *),
564 		    (uintptr_t)(ht.hash_table+bucket)) == -1) {
565 			mdb_warn("failed to read hash bucket %u at %p",
566 			    bucket, ht.hash_table+bucket);
567 			return (DCMD_ERR);
568 		}
569 
570 		len = 0;
571 		while (dbp != 0) {
572 			if (mdb_vread(&db, sizeof (dmu_buf_impl_t),
573 			    dbp) == -1) {
574 				mdb_warn("failed to read dbuf at %p", dbp);
575 				return (DCMD_ERR);
576 			}
577 			dbp = (uintptr_t)db.db_hash_next;
578 			for (i = MIN(len, HISTOSZ - 1); i >= 0; i--)
579 				histo2[i]++;
580 			len++;
581 			ndbufs++;
582 		}
583 
584 		if (len >= HISTOSZ)
585 			len = HISTOSZ-1;
586 		histo[len]++;
587 	}
588 
589 	mdb_printf("hash table has %llu buckets, %llu dbufs "
590 	    "(avg %llu buckets/dbuf)\n",
591 	    ht.hash_table_mask+1, ndbufs,
592 	    (ht.hash_table_mask+1)/ndbufs);
593 
594 	mdb_printf("\n");
595 	maxidx = 0;
596 	for (i = 0; i < HISTOSZ; i++)
597 		if (histo[i] > 0)
598 			maxidx = i;
599 	mdb_printf("hash chain length	number of buckets\n");
600 	for (i = 0; i <= maxidx; i++)
601 		mdb_printf("%u			%llu\n", i, histo[i]);
602 
603 	mdb_printf("\n");
604 	maxidx = 0;
605 	for (i = 0; i < HISTOSZ; i++)
606 		if (histo2[i] > 0)
607 			maxidx = i;
608 	mdb_printf("hash chain depth	number of dbufs\n");
609 	for (i = 0; i <= maxidx; i++)
610 		mdb_printf("%u or more		%llu	%llu%%\n",
611 		    i, histo2[i], histo2[i]*100/ndbufs);
612 
613 
614 	return (DCMD_OK);
615 }
616 
617 typedef struct dbufs_data {
618 	mdb_ctf_id_t id;
619 	uint64_t objset;
620 	uint64_t object;
621 	uint64_t level;
622 	uint64_t blkid;
623 	char *osname;
624 } dbufs_data_t;
625 
626 #define	DBUFS_UNSET	(0xbaddcafedeadbeefULL)
627 
628 /* ARGSUSED */
629 static int
630 dbufs_cb(uintptr_t addr, const void *unknown, void *arg)
631 {
632 	dbufs_data_t *data = arg;
633 	uintptr_t objset;
634 	dmu_buf_t db;
635 	uint8_t level;
636 	uint64_t blkid;
637 	char osname[MAXNAMELEN];
638 
639 	if (GETMEMBID(addr, &data->id, db_objset, objset) ||
640 	    GETMEMBID(addr, &data->id, db, db) ||
641 	    GETMEMBID(addr, &data->id, db_level, level) ||
642 	    GETMEMBID(addr, &data->id, db_blkid, blkid)) {
643 		return (WALK_ERR);
644 	}
645 
646 	if ((data->objset == DBUFS_UNSET || data->objset == objset) &&
647 	    (data->osname == NULL || (objset_name(objset, osname) == 0 &&
648 	    strcmp(data->osname, osname) == 0)) &&
649 	    (data->object == DBUFS_UNSET || data->object == db.db_object) &&
650 	    (data->level == DBUFS_UNSET || data->level == level) &&
651 	    (data->blkid == DBUFS_UNSET || data->blkid == blkid)) {
652 		mdb_printf("%#lr\n", addr);
653 	}
654 	return (WALK_NEXT);
655 }
656 
657 /* ARGSUSED */
658 static int
659 dbufs(uintptr_t addr, uint_t flags, int argc, const mdb_arg_t *argv)
660 {
661 	dbufs_data_t data;
662 	char *object = NULL;
663 	char *blkid = NULL;
664 
665 	data.objset = data.object = data.level = data.blkid = DBUFS_UNSET;
666 	data.osname = NULL;
667 
668 	if (mdb_getopts(argc, argv,
669 	    'O', MDB_OPT_UINT64, &data.objset,
670 	    'n', MDB_OPT_STR, &data.osname,
671 	    'o', MDB_OPT_STR, &object,
672 	    'l', MDB_OPT_UINT64, &data.level,
673 	    'b', MDB_OPT_STR, &blkid) != argc) {
674 		return (DCMD_USAGE);
675 	}
676 
677 	if (object) {
678 		if (strcmp(object, "mdn") == 0) {
679 			data.object = DMU_META_DNODE_OBJECT;
680 		} else {
681 			data.object = mdb_strtoull(object);
682 		}
683 	}
684 
685 	if (blkid) {
686 		if (strcmp(blkid, "bonus") == 0) {
687 			data.blkid = DB_BONUS_BLKID;
688 		} else {
689 			data.blkid = mdb_strtoull(blkid);
690 		}
691 	}
692 
693 	if (mdb_ctf_lookup_by_name("struct dmu_buf_impl", &data.id) == -1) {
694 		mdb_warn("couldn't find struct dmu_buf_impl_t");
695 		return (DCMD_ERR);
696 	}
697 
698 	if (mdb_walk("dmu_buf_impl_t", dbufs_cb, &data) != 0) {
699 		mdb_warn("can't walk dbufs");
700 		return (DCMD_ERR);
701 	}
702 
703 	return (DCMD_OK);
704 }
705 
706 typedef struct abuf_find_data {
707 	dva_t dva;
708 	mdb_ctf_id_t id;
709 } abuf_find_data_t;
710 
711 /* ARGSUSED */
712 static int
713 abuf_find_cb(uintptr_t addr, const void *unknown, void *arg)
714 {
715 	abuf_find_data_t *data = arg;
716 	dva_t dva;
717 
718 	if (GETMEMBID(addr, &data->id, b_dva, dva)) {
719 		return (WALK_ERR);
720 	}
721 
722 	if (dva.dva_word[0] == data->dva.dva_word[0] &&
723 	    dva.dva_word[1] == data->dva.dva_word[1]) {
724 		mdb_printf("%#lr\n", addr);
725 	}
726 	return (WALK_NEXT);
727 }
728 
729 /* ARGSUSED */
730 static int
731 abuf_find(uintptr_t addr, uint_t flags, int argc, const mdb_arg_t *argv)
732 {
733 	abuf_find_data_t data;
734 	GElf_Sym sym;
735 	int i;
736 	const char *syms[] = {
737 		"ARC_mru",
738 		"ARC_mru_ghost",
739 		"ARC_mfu",
740 		"ARC_mfu_ghost",
741 	};
742 
743 	if (argc != 2)
744 		return (DCMD_USAGE);
745 
746 	for (i = 0; i < 2; i ++) {
747 		switch (argv[i].a_type) {
748 		case MDB_TYPE_STRING:
749 			data.dva.dva_word[i] = mdb_strtoull(argv[i].a_un.a_str);
750 			break;
751 		case MDB_TYPE_IMMEDIATE:
752 			data.dva.dva_word[i] = argv[i].a_un.a_val;
753 			break;
754 		default:
755 			return (DCMD_USAGE);
756 		}
757 	}
758 
759 	if (mdb_ctf_lookup_by_name("struct arc_buf_hdr", &data.id) == -1) {
760 		mdb_warn("couldn't find struct arc_buf_hdr");
761 		return (DCMD_ERR);
762 	}
763 
764 	for (i = 0; i < sizeof (syms) / sizeof (syms[0]); i++) {
765 		if (mdb_lookup_by_name(syms[i], &sym)) {
766 			mdb_warn("can't find symbol %s", syms[i]);
767 			return (DCMD_ERR);
768 		}
769 
770 		if (mdb_pwalk("list", abuf_find_cb, &data, sym.st_value) != 0) {
771 			mdb_warn("can't walk %s", syms[i]);
772 			return (DCMD_ERR);
773 		}
774 	}
775 
776 	return (DCMD_OK);
777 }
778 
779 /*ARGSUSED*/
780 static int
781 arc_print(uintptr_t addr, uint_t flags, int argc, const mdb_arg_t *argv)
782 {
783 	kstat_named_t *stats;
784 	GElf_Sym sym;
785 	int nstats, i;
786 	uint_t opt_a = FALSE;
787 	uint_t opt_b = FALSE;
788 	uint_t shift = 0;
789 	const char *suffix;
790 
791 	static const char *bytestats[] = {
792 		"p", "c", "c_min", "c_max", "size", NULL
793 	};
794 
795 	static const char *extras[] = {
796 		"arc_no_grow", "arc_tempreserve",
797 		"arc_meta_used", "arc_meta_limit", "arc_meta_max",
798 		NULL
799 	};
800 
801 	if (mdb_lookup_by_name("arc_stats", &sym) == -1) {
802 		mdb_warn("failed to find 'arc_stats'");
803 		return (DCMD_ERR);
804 	}
805 
806 	stats = mdb_zalloc(sym.st_size, UM_SLEEP | UM_GC);
807 
808 	if (mdb_vread(stats, sym.st_size, sym.st_value) == -1) {
809 		mdb_warn("couldn't read 'arc_stats' at %p", sym.st_value);
810 		return (DCMD_ERR);
811 	}
812 
813 	nstats = sym.st_size / sizeof (kstat_named_t);
814 
815 	/* NB: -a / opt_a are ignored for backwards compatability */
816 	if (mdb_getopts(argc, argv,
817 	    'a', MDB_OPT_SETBITS, TRUE, &opt_a,
818 	    'b', MDB_OPT_SETBITS, TRUE, &opt_b,
819 	    'k', MDB_OPT_SETBITS, 10, &shift,
820 	    'm', MDB_OPT_SETBITS, 20, &shift,
821 	    'g', MDB_OPT_SETBITS, 30, &shift,
822 	    NULL) != argc)
823 		return (DCMD_USAGE);
824 
825 	if (!opt_b && !shift)
826 		shift = 20;
827 
828 	switch (shift) {
829 	case 0:
830 		suffix = "B";
831 		break;
832 	case 10:
833 		suffix = "KB";
834 		break;
835 	case 20:
836 		suffix = "MB";
837 		break;
838 	case 30:
839 		suffix = "GB";
840 		break;
841 	default:
842 		suffix = "XX";
843 	}
844 
845 	for (i = 0; i < nstats; i++) {
846 		int j;
847 		boolean_t bytes = B_FALSE;
848 
849 		for (j = 0; bytestats[j]; j++) {
850 			if (strcmp(stats[i].name, bytestats[j]) == 0) {
851 				bytes = B_TRUE;
852 				break;
853 			}
854 		}
855 
856 		if (bytes) {
857 			mdb_printf("%-25s = %9llu %s\n", stats[i].name,
858 			    stats[i].value.ui64 >> shift, suffix);
859 		} else {
860 			mdb_printf("%-25s = %9llu\n", stats[i].name,
861 			    stats[i].value.ui64);
862 		}
863 	}
864 
865 	for (i = 0; extras[i]; i++) {
866 		uint64_t buf;
867 
868 		if (mdb_lookup_by_name(extras[i], &sym) == -1) {
869 			mdb_warn("failed to find '%s'", extras[i]);
870 			return (DCMD_ERR);
871 		}
872 
873 		if (sym.st_size != sizeof (uint64_t) &&
874 		    sym.st_size != sizeof (uint32_t)) {
875 			mdb_warn("expected scalar for variable '%s'\n",
876 			    extras[i]);
877 			return (DCMD_ERR);
878 		}
879 
880 		if (mdb_vread(&buf, sym.st_size, sym.st_value) == -1) {
881 			mdb_warn("couldn't read '%s'", extras[i]);
882 			return (DCMD_ERR);
883 		}
884 
885 		mdb_printf("%-25s = ", extras[i]);
886 
887 		/* NB: all the 64-bit extras happen to be byte counts */
888 		if (sym.st_size == sizeof (uint64_t))
889 			mdb_printf("%9llu %s\n", buf >> shift, suffix);
890 
891 		if (sym.st_size == sizeof (uint32_t))
892 			mdb_printf("%9d\n", *((uint32_t *)&buf));
893 	}
894 	return (DCMD_OK);
895 }
896 
897 /*
898  * ::spa
899  *
900  * 	-c	Print configuration information as well
901  * 	-v	Print vdev state
902  * 	-e	Print vdev error stats
903  *
904  * Print a summarized spa_t.  When given no arguments, prints out a table of all
905  * active pools on the system.
906  */
907 /* ARGSUSED */
908 static int
909 spa_print(uintptr_t addr, uint_t flags, int argc, const mdb_arg_t *argv)
910 {
911 	spa_t spa;
912 	char poolname[MAXNAMELEN];
913 	const char *statetab[] = { "ACTIVE", "EXPORTED", "DESTROYED",
914 		"SPARE", "UNINIT", "IOFAILURE", "UNAVAIL" };
915 	const char *state;
916 	int config = FALSE;
917 	int vdevs = FALSE;
918 	int errors = FALSE;
919 
920 	if (mdb_getopts(argc, argv,
921 	    'c', MDB_OPT_SETBITS, TRUE, &config,
922 	    'v', MDB_OPT_SETBITS, TRUE, &vdevs,
923 	    'e', MDB_OPT_SETBITS, TRUE, &errors,
924 	    NULL) != argc)
925 		return (DCMD_USAGE);
926 
927 	if (!(flags & DCMD_ADDRSPEC)) {
928 		if (mdb_walk_dcmd("spa", "spa", argc, argv) == -1) {
929 			mdb_warn("can't walk spa");
930 			return (DCMD_ERR);
931 		}
932 
933 		return (DCMD_OK);
934 	}
935 
936 	if (flags & DCMD_PIPE_OUT) {
937 		mdb_printf("%#lr\n", addr);
938 		return (DCMD_OK);
939 	}
940 
941 	if (DCMD_HDRSPEC(flags))
942 		mdb_printf("%<u>%-?s %9s %-*s%</u>\n", "ADDR", "STATE",
943 		    sizeof (uintptr_t) == 4 ? 60 : 52, "NAME");
944 
945 	if (mdb_vread(&spa, sizeof (spa), addr) == -1) {
946 		mdb_warn("failed to read spa_t at %p", addr);
947 		return (DCMD_ERR);
948 	}
949 
950 	if (mdb_readstr(poolname, sizeof (poolname), (uintptr_t)spa.spa_name)
951 	    == -1) {
952 		mdb_warn("failed to read pool name at %p", spa.spa_name);
953 		return (DCMD_ERR);
954 	}
955 
956 	if (spa.spa_state < 0 || spa.spa_state > POOL_STATE_UNAVAIL)
957 		state = "UNKNOWN";
958 	else
959 		state = statetab[spa.spa_state];
960 
961 	mdb_printf("%0?p %9s %s\n", addr, state, poolname);
962 
963 	if (config) {
964 		mdb_printf("\n");
965 		mdb_inc_indent(4);
966 		if (mdb_call_dcmd("spa_config", addr, flags, 0,
967 		    NULL) != DCMD_OK)
968 			return (DCMD_ERR);
969 		mdb_dec_indent(4);
970 	}
971 
972 	if (vdevs || errors) {
973 		mdb_arg_t v;
974 
975 		v.a_type = MDB_TYPE_STRING;
976 		v.a_un.a_str = "-e";
977 
978 		mdb_printf("\n");
979 		mdb_inc_indent(4);
980 		if (mdb_call_dcmd("spa_vdevs", addr, flags, errors ? 1 : 0,
981 		    &v) != DCMD_OK)
982 			return (DCMD_ERR);
983 		mdb_dec_indent(4);
984 	}
985 
986 	return (DCMD_OK);
987 }
988 
989 /*
990  * ::spa_config
991  *
992  * Given a spa_t, print the configuration information stored in spa_config.
993  * Since it's just an nvlist, format it as an indented list of name=value pairs.
994  * We simply read the value of spa_config and pass off to ::nvlist.
995  */
996 /* ARGSUSED */
997 static int
998 spa_print_config(uintptr_t addr, uint_t flags, int argc, const mdb_arg_t *argv)
999 {
1000 	spa_t spa;
1001 
1002 	if (argc != 0 || !(flags & DCMD_ADDRSPEC))
1003 		return (DCMD_USAGE);
1004 
1005 	if (mdb_vread(&spa, sizeof (spa), addr) == -1) {
1006 		mdb_warn("failed to read spa_t at %p", addr);
1007 		return (DCMD_ERR);
1008 	}
1009 
1010 	if (spa.spa_config == NULL) {
1011 		mdb_printf("(none)\n");
1012 		return (DCMD_OK);
1013 	}
1014 
1015 	return (mdb_call_dcmd("nvlist", (uintptr_t)spa.spa_config, flags,
1016 	    0, NULL));
1017 }
1018 
1019 /*
1020  * ::vdev
1021  *
1022  * Print out a summarized vdev_t, in the following form:
1023  *
1024  * ADDR             STATE	AUX            DESC
1025  * fffffffbcde23df0 HEALTHY	-              /dev/dsk/c0t0d0
1026  *
1027  * If '-r' is specified, recursively visit all children.
1028  *
1029  * With '-e', the statistics associated with the vdev are printed as well.
1030  */
1031 static int
1032 do_print_vdev(uintptr_t addr, int flags, int depth, int stats,
1033     int recursive)
1034 {
1035 	vdev_t vdev;
1036 	char desc[MAXNAMELEN];
1037 	int c, children;
1038 	uintptr_t *child;
1039 	const char *state, *aux;
1040 
1041 	if (mdb_vread(&vdev, sizeof (vdev), (uintptr_t)addr) == -1) {
1042 		mdb_warn("failed to read vdev_t at %p\n", (uintptr_t)addr);
1043 		return (DCMD_ERR);
1044 	}
1045 
1046 	if (flags & DCMD_PIPE_OUT) {
1047 		mdb_printf("%#lr", addr);
1048 	} else {
1049 		if (vdev.vdev_path != NULL) {
1050 			if (mdb_readstr(desc, sizeof (desc),
1051 			    (uintptr_t)vdev.vdev_path) == -1) {
1052 				mdb_warn("failed to read vdev_path at %p\n",
1053 				    vdev.vdev_path);
1054 				return (DCMD_ERR);
1055 			}
1056 		} else if (vdev.vdev_ops != NULL) {
1057 			vdev_ops_t ops;
1058 			if (mdb_vread(&ops, sizeof (ops),
1059 			    (uintptr_t)vdev.vdev_ops) == -1) {
1060 				mdb_warn("failed to read vdev_ops at %p\n",
1061 				    vdev.vdev_ops);
1062 				return (DCMD_ERR);
1063 			}
1064 			(void) strcpy(desc, ops.vdev_op_type);
1065 		} else {
1066 			(void) strcpy(desc, "<unknown>");
1067 		}
1068 
1069 		if (depth == 0 && DCMD_HDRSPEC(flags))
1070 			mdb_printf("%<u>%-?s %-9s %-12s %-*s%</u>\n",
1071 			    "ADDR", "STATE", "AUX",
1072 			    sizeof (uintptr_t) == 4 ? 43 : 35,
1073 			    "DESCRIPTION");
1074 
1075 		mdb_printf("%0?p ", addr);
1076 
1077 		switch (vdev.vdev_state) {
1078 		case VDEV_STATE_CLOSED:
1079 			state = "CLOSED";
1080 			break;
1081 		case VDEV_STATE_OFFLINE:
1082 			state = "OFFLINE";
1083 			break;
1084 		case VDEV_STATE_CANT_OPEN:
1085 			state = "CANT_OPEN";
1086 			break;
1087 		case VDEV_STATE_DEGRADED:
1088 			state = "DEGRADED";
1089 			break;
1090 		case VDEV_STATE_HEALTHY:
1091 			state = "HEALTHY";
1092 			break;
1093 		case VDEV_STATE_REMOVED:
1094 			state = "REMOVED";
1095 			break;
1096 		case VDEV_STATE_FAULTED:
1097 			state = "FAULTED";
1098 			break;
1099 		default:
1100 			state = "UNKNOWN";
1101 			break;
1102 		}
1103 
1104 		switch (vdev.vdev_stat.vs_aux) {
1105 		case VDEV_AUX_NONE:
1106 			aux = "-";
1107 			break;
1108 		case VDEV_AUX_OPEN_FAILED:
1109 			aux = "OPEN_FAILED";
1110 			break;
1111 		case VDEV_AUX_CORRUPT_DATA:
1112 			aux = "CORRUPT_DATA";
1113 			break;
1114 		case VDEV_AUX_NO_REPLICAS:
1115 			aux = "NO_REPLICAS";
1116 			break;
1117 		case VDEV_AUX_BAD_GUID_SUM:
1118 			aux = "BAD_GUID_SUM";
1119 			break;
1120 		case VDEV_AUX_TOO_SMALL:
1121 			aux = "TOO_SMALL";
1122 			break;
1123 		case VDEV_AUX_BAD_LABEL:
1124 			aux = "BAD_LABEL";
1125 			break;
1126 		default:
1127 			aux = "UNKNOWN";
1128 			break;
1129 		}
1130 
1131 		mdb_printf("%-9s %-12s %*s%s\n", state, aux, depth, "", desc);
1132 
1133 		if (stats) {
1134 			vdev_stat_t *vs = &vdev.vdev_stat;
1135 			int i;
1136 
1137 			mdb_inc_indent(4);
1138 			mdb_printf("\n");
1139 			mdb_printf("%<u>       %12s %12s %12s %12s "
1140 			    "%12s%</u>\n", "READ", "WRITE", "FREE", "CLAIM",
1141 			    "IOCTL");
1142 			mdb_printf("OPS     ");
1143 			for (i = 1; i < ZIO_TYPES; i++)
1144 				mdb_printf("%11#llx%s", vs->vs_ops[i],
1145 				    i == ZIO_TYPES - 1 ? "" : "  ");
1146 			mdb_printf("\n");
1147 			mdb_printf("BYTES   ");
1148 			for (i = 1; i < ZIO_TYPES; i++)
1149 				mdb_printf("%11#llx%s", vs->vs_bytes[i],
1150 				    i == ZIO_TYPES - 1 ? "" : "  ");
1151 
1152 
1153 			mdb_printf("\n");
1154 			mdb_printf("EREAD    %10#llx\n", vs->vs_read_errors);
1155 			mdb_printf("EWRITE   %10#llx\n", vs->vs_write_errors);
1156 			mdb_printf("ECKSUM   %10#llx\n",
1157 			    vs->vs_checksum_errors);
1158 			mdb_dec_indent(4);
1159 		}
1160 
1161 		if (stats)
1162 			mdb_printf("\n");
1163 	}
1164 
1165 	children = vdev.vdev_children;
1166 
1167 	if (children == 0 || !recursive)
1168 		return (DCMD_OK);
1169 
1170 	child = mdb_alloc(children * sizeof (void *), UM_SLEEP | UM_GC);
1171 	if (mdb_vread(child, children * sizeof (void *),
1172 	    (uintptr_t)vdev.vdev_child) == -1) {
1173 		mdb_warn("failed to read vdev children at %p", vdev.vdev_child);
1174 		return (DCMD_ERR);
1175 	}
1176 
1177 	for (c = 0; c < children; c++) {
1178 		if (do_print_vdev(child[c], flags, depth + 2, stats,
1179 		    recursive))
1180 			return (DCMD_ERR);
1181 	}
1182 
1183 	return (DCMD_OK);
1184 }
1185 
1186 static int
1187 vdev_print(uintptr_t addr, uint_t flags, int argc, const mdb_arg_t *argv)
1188 {
1189 	int recursive = FALSE;
1190 	int stats = FALSE;
1191 	uint64_t depth = 0;
1192 
1193 	if (mdb_getopts(argc, argv,
1194 	    'r', MDB_OPT_SETBITS, TRUE, &recursive,
1195 	    'e', MDB_OPT_SETBITS, TRUE, &stats,
1196 	    'd', MDB_OPT_UINT64, &depth,
1197 	    NULL) != argc)
1198 		return (DCMD_USAGE);
1199 
1200 	if (!(flags & DCMD_ADDRSPEC)) {
1201 		mdb_warn("no vdev_t address given\n");
1202 		return (DCMD_ERR);
1203 	}
1204 
1205 	return (do_print_vdev(addr, flags, (int)depth, stats, recursive));
1206 }
1207 
1208 typedef struct metaslab_walk_data {
1209 	uint64_t mw_numvdevs;
1210 	uintptr_t *mw_vdevs;
1211 	int mw_curvdev;
1212 	uint64_t mw_nummss;
1213 	uintptr_t *mw_mss;
1214 	int mw_curms;
1215 } metaslab_walk_data_t;
1216 
1217 static int
1218 metaslab_walk_step(mdb_walk_state_t *wsp)
1219 {
1220 	metaslab_walk_data_t *mw = wsp->walk_data;
1221 	metaslab_t ms;
1222 	uintptr_t msp;
1223 
1224 	if (mw->mw_curvdev >= mw->mw_numvdevs)
1225 		return (WALK_DONE);
1226 
1227 	if (mw->mw_mss == NULL) {
1228 		uintptr_t mssp;
1229 		uintptr_t vdevp;
1230 
1231 		ASSERT(mw->mw_curms == 0);
1232 		ASSERT(mw->mw_nummss == 0);
1233 
1234 		vdevp = mw->mw_vdevs[mw->mw_curvdev];
1235 		if (GETMEMB(vdevp, struct vdev, vdev_ms, mssp) ||
1236 		    GETMEMB(vdevp, struct vdev, vdev_ms_count, mw->mw_nummss)) {
1237 			return (WALK_ERR);
1238 		}
1239 
1240 		mw->mw_mss = mdb_alloc(mw->mw_nummss * sizeof (void*),
1241 		    UM_SLEEP | UM_GC);
1242 		if (mdb_vread(mw->mw_mss, mw->mw_nummss * sizeof (void*),
1243 		    mssp) == -1) {
1244 			mdb_warn("failed to read vdev_ms at %p", mssp);
1245 			return (WALK_ERR);
1246 		}
1247 	}
1248 
1249 	if (mw->mw_curms >= mw->mw_nummss) {
1250 		mw->mw_mss = NULL;
1251 		mw->mw_curms = 0;
1252 		mw->mw_nummss = 0;
1253 		mw->mw_curvdev++;
1254 		return (WALK_NEXT);
1255 	}
1256 
1257 	msp = mw->mw_mss[mw->mw_curms];
1258 	if (mdb_vread(&ms, sizeof (metaslab_t), msp) == -1) {
1259 		mdb_warn("failed to read metaslab_t at %p", msp);
1260 		return (WALK_ERR);
1261 	}
1262 
1263 	mw->mw_curms++;
1264 
1265 	return (wsp->walk_callback(msp, &ms, wsp->walk_cbdata));
1266 }
1267 
1268 /* ARGSUSED */
1269 static int
1270 metaslab_walk_init(mdb_walk_state_t *wsp)
1271 {
1272 	metaslab_walk_data_t *mw;
1273 	uintptr_t root_vdevp;
1274 	uintptr_t childp;
1275 
1276 	if (wsp->walk_addr == NULL) {
1277 		mdb_warn("must supply address of spa_t\n");
1278 		return (WALK_ERR);
1279 	}
1280 
1281 	mw = mdb_zalloc(sizeof (metaslab_walk_data_t), UM_SLEEP | UM_GC);
1282 
1283 	if (GETMEMB(wsp->walk_addr, struct spa, spa_root_vdev, root_vdevp) ||
1284 	    GETMEMB(root_vdevp, struct vdev, vdev_children, mw->mw_numvdevs) ||
1285 	    GETMEMB(root_vdevp, struct vdev, vdev_child, childp)) {
1286 		return (DCMD_ERR);
1287 	}
1288 
1289 	mw->mw_vdevs = mdb_alloc(mw->mw_numvdevs * sizeof (void *),
1290 	    UM_SLEEP | UM_GC);
1291 	if (mdb_vread(mw->mw_vdevs, mw->mw_numvdevs * sizeof (void *),
1292 	    childp) == -1) {
1293 		mdb_warn("failed to read root vdev children at %p", childp);
1294 		return (DCMD_ERR);
1295 	}
1296 
1297 	wsp->walk_data = mw;
1298 
1299 	return (WALK_NEXT);
1300 }
1301 
1302 typedef struct mdb_spa {
1303 	uintptr_t spa_dsl_pool;
1304 	uintptr_t spa_root_vdev;
1305 } mdb_spa_t;
1306 
1307 typedef struct mdb_dsl_dir {
1308 	uintptr_t dd_phys;
1309 	uint64_t dd_used_bytes;
1310 	int64_t dd_space_towrite[TXG_SIZE];
1311 } mdb_dsl_dir_t;
1312 
1313 typedef struct mdb_dsl_dir_phys {
1314 	uint64_t dd_used_bytes;
1315 	uint64_t dd_compressed_bytes;
1316 	uint64_t dd_uncompressed_bytes;
1317 } mdb_dsl_dir_phys_t;
1318 
1319 typedef struct mdb_vdev {
1320 	uintptr_t vdev_parent;
1321 	uintptr_t vdev_ms;
1322 	uint64_t vdev_ms_count;
1323 	vdev_stat_t vdev_stat;
1324 } mdb_vdev_t;
1325 
1326 typedef struct mdb_metaslab {
1327 	space_map_t ms_allocmap[TXG_SIZE];
1328 	space_map_t ms_freemap[TXG_SIZE];
1329 	space_map_t ms_map;
1330 	space_map_obj_t ms_smo;
1331 	space_map_obj_t ms_smo_syncing;
1332 } mdb_metaslab_t;
1333 
1334 typedef struct space_data {
1335 	uint64_t ms_allocmap[TXG_SIZE];
1336 	uint64_t ms_freemap[TXG_SIZE];
1337 	uint64_t ms_map;
1338 	uint64_t avail;
1339 	uint64_t nowavail;
1340 } space_data_t;
1341 
1342 /* ARGSUSED */
1343 static int
1344 space_cb(uintptr_t addr, const void *unknown, void *arg)
1345 {
1346 	space_data_t *sd = arg;
1347 	mdb_metaslab_t ms;
1348 
1349 	if (GETMEMB(addr, struct metaslab, ms_allocmap, ms.ms_allocmap) ||
1350 	    GETMEMB(addr, struct metaslab, ms_freemap, ms.ms_freemap) ||
1351 	    GETMEMB(addr, struct metaslab, ms_map, ms.ms_map) ||
1352 	    GETMEMB(addr, struct metaslab, ms_smo, ms.ms_smo) ||
1353 	    GETMEMB(addr, struct metaslab, ms_smo_syncing, ms.ms_smo_syncing)) {
1354 		return (WALK_ERR);
1355 	}
1356 
1357 	sd->ms_allocmap[0] += ms.ms_allocmap[0].sm_space;
1358 	sd->ms_allocmap[1] += ms.ms_allocmap[1].sm_space;
1359 	sd->ms_allocmap[2] += ms.ms_allocmap[2].sm_space;
1360 	sd->ms_allocmap[3] += ms.ms_allocmap[3].sm_space;
1361 	sd->ms_freemap[0] += ms.ms_freemap[0].sm_space;
1362 	sd->ms_freemap[1] += ms.ms_freemap[1].sm_space;
1363 	sd->ms_freemap[2] += ms.ms_freemap[2].sm_space;
1364 	sd->ms_freemap[3] += ms.ms_freemap[3].sm_space;
1365 	sd->ms_map += ms.ms_map.sm_space;
1366 	sd->avail += ms.ms_map.sm_size - ms.ms_smo.smo_alloc;
1367 	sd->nowavail += ms.ms_map.sm_size - ms.ms_smo_syncing.smo_alloc;
1368 
1369 	return (WALK_NEXT);
1370 }
1371 
1372 /*
1373  * ::spa_space [-b]
1374  *
1375  * Given a spa_t, print out it's on-disk space usage and in-core
1376  * estimates of future usage.  If -b is given, print space in bytes.
1377  * Otherwise print in megabytes.
1378  */
1379 /* ARGSUSED */
1380 static int
1381 spa_space(uintptr_t addr, uint_t flags, int argc, const mdb_arg_t *argv)
1382 {
1383 	mdb_spa_t spa;
1384 	uintptr_t dp_root_dir;
1385 	mdb_dsl_dir_t dd;
1386 	mdb_dsl_dir_phys_t dsp;
1387 	uint64_t children;
1388 	uintptr_t childaddr;
1389 	space_data_t sd;
1390 	int shift = 20;
1391 	char *suffix = "M";
1392 	int bits = FALSE;
1393 
1394 	if (mdb_getopts(argc, argv, 'b', MDB_OPT_SETBITS, TRUE, &bits, NULL) !=
1395 	    argc)
1396 		return (DCMD_USAGE);
1397 	if (!(flags & DCMD_ADDRSPEC))
1398 		return (DCMD_USAGE);
1399 
1400 	if (bits) {
1401 		shift = 0;
1402 		suffix = "";
1403 	}
1404 
1405 	if (GETMEMB(addr, struct spa, spa_dsl_pool, spa.spa_dsl_pool) ||
1406 	    GETMEMB(addr, struct spa, spa_root_vdev, spa.spa_root_vdev) ||
1407 	    GETMEMB(spa.spa_root_vdev, struct vdev, vdev_children, children) ||
1408 	    GETMEMB(spa.spa_root_vdev, struct vdev, vdev_child, childaddr) ||
1409 	    GETMEMB(spa.spa_dsl_pool, struct dsl_pool,
1410 	    dp_root_dir, dp_root_dir) ||
1411 	    GETMEMB(dp_root_dir, struct dsl_dir, dd_phys, dd.dd_phys) ||
1412 	    GETMEMB(dp_root_dir, struct dsl_dir,
1413 	    dd_used_bytes, dd.dd_used_bytes) ||
1414 	    GETMEMB(dp_root_dir, struct dsl_dir,
1415 	    dd_space_towrite, dd.dd_space_towrite) ||
1416 	    GETMEMB(dd.dd_phys, struct dsl_dir_phys,
1417 	    dd_used_bytes, dsp.dd_used_bytes) ||
1418 	    GETMEMB(dd.dd_phys, struct dsl_dir_phys,
1419 	    dd_compressed_bytes, dsp.dd_compressed_bytes) ||
1420 	    GETMEMB(dd.dd_phys, struct dsl_dir_phys,
1421 	    dd_uncompressed_bytes, dsp.dd_uncompressed_bytes)) {
1422 		return (DCMD_ERR);
1423 	}
1424 
1425 	mdb_printf("dd_space_towrite = %llu%s %llu%s %llu%s %llu%s\n",
1426 	    dd.dd_space_towrite[0] >> shift, suffix,
1427 	    dd.dd_space_towrite[1] >> shift, suffix,
1428 	    dd.dd_space_towrite[2] >> shift, suffix,
1429 	    dd.dd_space_towrite[3] >> shift, suffix);
1430 	mdb_printf("dd_used_bytes = %llu%s\n",
1431 	    dd.dd_used_bytes >> shift, suffix);
1432 
1433 	mdb_printf("dd_phys.dd_used_bytes = %llu%s\n",
1434 	    dsp.dd_used_bytes >> shift, suffix);
1435 	mdb_printf("dd_phys.dd_compressed_bytes = %llu%s\n",
1436 	    dsp.dd_compressed_bytes >> shift, suffix);
1437 	mdb_printf("dd_phys.dd_uncompressed_bytes = %llu%s\n",
1438 	    dsp.dd_uncompressed_bytes >> shift, suffix);
1439 
1440 	bzero(&sd, sizeof (sd));
1441 	if (mdb_pwalk("metaslab", space_cb, &sd, addr) != 0) {
1442 		mdb_warn("can't walk metaslabs");
1443 		return (DCMD_ERR);
1444 	}
1445 
1446 	mdb_printf("ms_allocmap = %llu%s %llu%s %llu%s %llu%s\n",
1447 	    sd.ms_allocmap[0] >> shift, suffix,
1448 	    sd.ms_allocmap[1] >> shift, suffix,
1449 	    sd.ms_allocmap[2] >> shift, suffix,
1450 	    sd.ms_allocmap[3] >> shift, suffix);
1451 	mdb_printf("ms_freemap = %llu%s %llu%s %llu%s %llu%s\n",
1452 	    sd.ms_freemap[0] >> shift, suffix,
1453 	    sd.ms_freemap[1] >> shift, suffix,
1454 	    sd.ms_freemap[2] >> shift, suffix,
1455 	    sd.ms_freemap[3] >> shift, suffix);
1456 	mdb_printf("ms_map = %llu%s\n", sd.ms_map >> shift, suffix);
1457 	mdb_printf("last synced avail = %llu%s\n", sd.avail >> shift, suffix);
1458 	mdb_printf("current syncing avail = %llu%s\n",
1459 	    sd.nowavail >> shift, suffix);
1460 
1461 	return (DCMD_OK);
1462 }
1463 
1464 /*
1465  * ::spa_verify
1466  *
1467  * Given a spa_t, verify that that the pool is self-consistent.
1468  * Currently, it only checks to make sure that the vdev tree exists.
1469  */
1470 /* ARGSUSED */
1471 static int
1472 spa_verify(uintptr_t addr, uint_t flags, int argc, const mdb_arg_t *argv)
1473 {
1474 	spa_t spa;
1475 
1476 	if (argc != 0 || !(flags & DCMD_ADDRSPEC))
1477 		return (DCMD_USAGE);
1478 
1479 	if (mdb_vread(&spa, sizeof (spa), addr) == -1) {
1480 		mdb_warn("failed to read spa_t at %p", addr);
1481 		return (DCMD_ERR);
1482 	}
1483 
1484 	if (spa.spa_root_vdev == NULL) {
1485 		mdb_printf("no vdev tree present\n");
1486 		return (DCMD_OK);
1487 	}
1488 
1489 	return (DCMD_OK);
1490 }
1491 
1492 /*
1493  * ::spa_vdevs
1494  *
1495  * 	-e	Include error stats
1496  *
1497  * Print out a summarized list of vdevs for the given spa_t.
1498  * This is accomplished by invoking "::vdev -re" on the root vdev, as well as
1499  * iterating over the cache devices.
1500  */
1501 /* ARGSUSED */
1502 static int
1503 spa_vdevs(uintptr_t addr, uint_t flags, int argc, const mdb_arg_t *argv)
1504 {
1505 	spa_t spa;
1506 	mdb_arg_t v[3];
1507 	int errors = FALSE;
1508 	int ret, i;
1509 	uintptr_t *aux;
1510 	size_t len;
1511 
1512 	if (mdb_getopts(argc, argv,
1513 	    'e', MDB_OPT_SETBITS, TRUE, &errors,
1514 	    NULL) != argc)
1515 		return (DCMD_USAGE);
1516 
1517 	if (!(flags & DCMD_ADDRSPEC))
1518 		return (DCMD_USAGE);
1519 
1520 	if (mdb_vread(&spa, sizeof (spa), addr) == -1) {
1521 		mdb_warn("failed to read spa_t at %p", addr);
1522 		return (DCMD_ERR);
1523 	}
1524 
1525 	/*
1526 	 * Unitialized spa_t structures can have a NULL root vdev.
1527 	 */
1528 	if (spa.spa_root_vdev == NULL) {
1529 		mdb_printf("no associated vdevs\n");
1530 		return (DCMD_OK);
1531 	}
1532 
1533 	v[0].a_type = MDB_TYPE_STRING;
1534 	v[0].a_un.a_str = errors ? "-re" : "-r";
1535 
1536 	ret = mdb_call_dcmd("vdev", (uintptr_t)spa.spa_root_vdev,
1537 	    flags, 1, v);
1538 	if (ret != DCMD_OK)
1539 		return (ret);
1540 
1541 	/*
1542 	 * Iterate over cache devices and print those out as well.  This is a
1543 	 * little annoying because we don't have a root vdev to pass to ::vdev.
1544 	 * Instead, we print a single 'cache' line and then call it for each
1545 	 * child vdev.
1546 	 */
1547 	if (spa.spa_l2cache.sav_count != 0) {
1548 		v[1].a_type = MDB_TYPE_STRING;
1549 		v[1].a_un.a_str = "-d";
1550 		v[2].a_type = MDB_TYPE_IMMEDIATE;
1551 		v[2].a_un.a_val = 2;
1552 
1553 		len = spa.spa_l2cache.sav_count * sizeof (uintptr_t);
1554 		aux = mdb_alloc(len, UM_SLEEP);
1555 		if (mdb_vread(aux, len,
1556 		    (uintptr_t)spa.spa_l2cache.sav_vdevs) == -1) {
1557 			mdb_free(aux, len);
1558 			mdb_warn("failed to read l2cache vdevs at %p",
1559 			    spa.spa_l2cache.sav_vdevs);
1560 			return (DCMD_ERR);
1561 		}
1562 
1563 		mdb_printf("%-?s %-9s %-12s cache\n", "-", "-", "-");
1564 
1565 		for (i = 0; i < spa.spa_l2cache.sav_count; i++) {
1566 			ret = mdb_call_dcmd("vdev", aux[i], flags, 3, v);
1567 			if (ret != DCMD_OK) {
1568 				mdb_free(aux, len);
1569 				return (ret);
1570 			}
1571 		}
1572 
1573 		mdb_free(aux, len);
1574 	}
1575 
1576 	return (DCMD_OK);
1577 }
1578 
1579 /*
1580  * ::zio
1581  *
1582  * Print a summary of zio_t and all its children.  This is intended to display a
1583  * zio tree, and hence we only pick the most important pieces of information for
1584  * the main summary.  More detailed information can always be found by doing a
1585  * '::print zio' on the underlying zio_t.  The columns we display are:
1586  *
1587  *	ADDRESS		TYPE	STAGE		WAITER
1588  *
1589  * The 'address' column is indented by one space for each depth level as we
1590  * descend down the tree.
1591  */
1592 
1593 #define	ZIO_MAXDEPTH	16
1594 
1595 static int
1596 zio_print_cb(uintptr_t addr, const void *data, void *priv)
1597 {
1598 	const zio_t *zio = data;
1599 	uintptr_t depth = (uintptr_t)priv;
1600 	mdb_ctf_id_t type_enum, stage_enum;
1601 	const char *type, *stage;
1602 	int maxdepth;
1603 
1604 	maxdepth = sizeof (uintptr_t) * 2 + ZIO_MAXDEPTH;
1605 	if (depth > ZIO_MAXDEPTH)
1606 		depth = ZIO_MAXDEPTH;
1607 
1608 	if (mdb_ctf_lookup_by_name("enum zio_type", &type_enum) == -1 ||
1609 	    mdb_ctf_lookup_by_name("enum zio_stage", &stage_enum) == -1) {
1610 		mdb_warn("failed to lookup zio enums");
1611 		return (WALK_ERR);
1612 	}
1613 
1614 	if ((type = mdb_ctf_enum_name(type_enum, zio->io_type)) != NULL)
1615 		type += sizeof ("ZIO_TYPE_") - 1;
1616 	else
1617 		type = "?";
1618 
1619 	if ((stage = mdb_ctf_enum_name(stage_enum, zio->io_stage)) != NULL)
1620 		stage += sizeof ("ZIO_STAGE_") - 1;
1621 	else
1622 		stage = "?";
1623 
1624 
1625 	mdb_printf("%*s%-*p %-5s %-22s ",
1626 	    depth, "", maxdepth - depth, addr, type, stage);
1627 	if (zio->io_waiter)
1628 		mdb_printf("%?p\n", zio->io_waiter);
1629 	else
1630 		mdb_printf("-\n");
1631 
1632 	if (mdb_pwalk("zio_child", zio_print_cb, (void *)(depth + 1),
1633 	    addr) !=  0) {
1634 		mdb_warn("failed to walk zio_t children at %p\n", addr);
1635 		return (WALK_ERR);
1636 	}
1637 
1638 	return (WALK_NEXT);
1639 }
1640 
1641 /*ARGSUSED*/
1642 static int
1643 zio_print(uintptr_t addr, uint_t flags, int argc, const mdb_arg_t *argv)
1644 {
1645 	zio_t zio;
1646 	int maxdepth;
1647 
1648 	maxdepth = sizeof (uintptr_t) * 2 + ZIO_MAXDEPTH;
1649 
1650 	if (!(flags & DCMD_ADDRSPEC))
1651 		return (DCMD_USAGE);
1652 
1653 	if (mdb_vread(&zio, sizeof (zio_t), addr) == -1) {
1654 		mdb_warn("failed to read zio_t at %p", addr);
1655 		return (DCMD_ERR);
1656 	}
1657 
1658 	if (DCMD_HDRSPEC(flags))
1659 		mdb_printf("%<u>%-*s %-5s %-22s %-?s%</u>\n", maxdepth,
1660 		    "ADDRESS", "TYPE", "STAGE", "WAITER");
1661 
1662 	if (zio_print_cb(addr, &zio, NULL) != WALK_NEXT)
1663 		return (DCMD_ERR);
1664 
1665 	return (DCMD_OK);
1666 }
1667 
1668 /*
1669  * [addr]::zio_state
1670  *
1671  * Print a summary of all zio_t structures on the system, or for a particular
1672  * pool.  This is equivalent to '::walk zio_root | ::zio'.
1673  */
1674 /*ARGSUSED*/
1675 static int
1676 zio_state(uintptr_t addr, uint_t flags, int argc, const mdb_arg_t *argv)
1677 {
1678 	/*
1679 	 * MDB will remember the last address of the pipeline, so if we don't
1680 	 * zero this we'll end up trying to walk zio structures for a
1681 	 * non-existent spa_t.
1682 	 */
1683 	if (!(flags & DCMD_ADDRSPEC))
1684 		addr = 0;
1685 
1686 	return (mdb_pwalk_dcmd("zio_root", "zio", argc, argv, addr));
1687 }
1688 
1689 typedef struct txg_list_walk_data {
1690 	uintptr_t lw_head[TXG_SIZE];
1691 	int	lw_txgoff;
1692 	int	lw_maxoff;
1693 	size_t	lw_offset;
1694 	void	*lw_obj;
1695 } txg_list_walk_data_t;
1696 
1697 static int
1698 txg_list_walk_init_common(mdb_walk_state_t *wsp, int txg, int maxoff)
1699 {
1700 	txg_list_walk_data_t *lwd;
1701 	txg_list_t list;
1702 	int i;
1703 
1704 	lwd = mdb_alloc(sizeof (txg_list_walk_data_t), UM_SLEEP | UM_GC);
1705 	if (mdb_vread(&list, sizeof (txg_list_t), wsp->walk_addr) == -1) {
1706 		mdb_warn("failed to read txg_list_t at %#lx", wsp->walk_addr);
1707 		return (WALK_ERR);
1708 	}
1709 
1710 	for (i = 0; i < TXG_SIZE; i++)
1711 		lwd->lw_head[i] = (uintptr_t)list.tl_head[i];
1712 	lwd->lw_offset = list.tl_offset;
1713 	lwd->lw_obj = mdb_alloc(lwd->lw_offset + sizeof (txg_node_t),
1714 	    UM_SLEEP | UM_GC);
1715 	lwd->lw_txgoff = txg;
1716 	lwd->lw_maxoff = maxoff;
1717 
1718 	wsp->walk_addr = lwd->lw_head[lwd->lw_txgoff];
1719 	wsp->walk_data = lwd;
1720 
1721 	return (WALK_NEXT);
1722 }
1723 
1724 static int
1725 txg_list_walk_init(mdb_walk_state_t *wsp)
1726 {
1727 	return (txg_list_walk_init_common(wsp, 0, TXG_SIZE-1));
1728 }
1729 
1730 static int
1731 txg_list0_walk_init(mdb_walk_state_t *wsp)
1732 {
1733 	return (txg_list_walk_init_common(wsp, 0, 0));
1734 }
1735 
1736 static int
1737 txg_list1_walk_init(mdb_walk_state_t *wsp)
1738 {
1739 	return (txg_list_walk_init_common(wsp, 1, 1));
1740 }
1741 
1742 static int
1743 txg_list2_walk_init(mdb_walk_state_t *wsp)
1744 {
1745 	return (txg_list_walk_init_common(wsp, 2, 2));
1746 }
1747 
1748 static int
1749 txg_list3_walk_init(mdb_walk_state_t *wsp)
1750 {
1751 	return (txg_list_walk_init_common(wsp, 3, 3));
1752 }
1753 
1754 static int
1755 txg_list_walk_step(mdb_walk_state_t *wsp)
1756 {
1757 	txg_list_walk_data_t *lwd = wsp->walk_data;
1758 	uintptr_t addr;
1759 	txg_node_t *node;
1760 	int status;
1761 
1762 	while (wsp->walk_addr == NULL && lwd->lw_txgoff < lwd->lw_maxoff) {
1763 		lwd->lw_txgoff++;
1764 		wsp->walk_addr = lwd->lw_head[lwd->lw_txgoff];
1765 	}
1766 
1767 	if (wsp->walk_addr == NULL)
1768 		return (WALK_DONE);
1769 
1770 	addr = wsp->walk_addr - lwd->lw_offset;
1771 
1772 	if (mdb_vread(lwd->lw_obj,
1773 	    lwd->lw_offset + sizeof (txg_node_t), addr) == -1) {
1774 		mdb_warn("failed to read list element at %#lx", addr);
1775 		return (WALK_ERR);
1776 	}
1777 
1778 	status = wsp->walk_callback(addr, lwd->lw_obj, wsp->walk_cbdata);
1779 	node = (txg_node_t *)((uintptr_t)lwd->lw_obj + lwd->lw_offset);
1780 	wsp->walk_addr = (uintptr_t)node->tn_next[lwd->lw_txgoff];
1781 
1782 	return (status);
1783 }
1784 
1785 /*
1786  * ::walk spa
1787  *
1788  * Walk all named spa_t structures in the namespace.  This is nothing more than
1789  * a layered avl walk.
1790  */
1791 static int
1792 spa_walk_init(mdb_walk_state_t *wsp)
1793 {
1794 	GElf_Sym sym;
1795 
1796 	if (wsp->walk_addr != NULL) {
1797 		mdb_warn("spa walk only supports global walks\n");
1798 		return (WALK_ERR);
1799 	}
1800 
1801 	if (mdb_lookup_by_obj(ZFS_OBJ_NAME, "spa_namespace_avl", &sym) == -1) {
1802 		mdb_warn("failed to find symbol 'spa_namespace_avl'");
1803 		return (WALK_ERR);
1804 	}
1805 
1806 	wsp->walk_addr = (uintptr_t)sym.st_value;
1807 
1808 	if (mdb_layered_walk("avl", wsp) == -1) {
1809 		mdb_warn("failed to walk 'avl'\n");
1810 		return (WALK_ERR);
1811 	}
1812 
1813 	return (WALK_NEXT);
1814 }
1815 
1816 static int
1817 spa_walk_step(mdb_walk_state_t *wsp)
1818 {
1819 	spa_t	spa;
1820 
1821 	if (mdb_vread(&spa, sizeof (spa), wsp->walk_addr) == -1) {
1822 		mdb_warn("failed to read spa_t at %p", wsp->walk_addr);
1823 		return (WALK_ERR);
1824 	}
1825 
1826 	return (wsp->walk_callback(wsp->walk_addr, &spa, wsp->walk_cbdata));
1827 }
1828 
1829 /*
1830  * [addr]::walk zio
1831  *
1832  * Walk all active zio_t structures on the system.  This is simply a layered
1833  * walk on top of ::walk zio_cache, with the optional ability to limit the
1834  * structures to a particular pool.
1835  */
1836 static int
1837 zio_walk_init(mdb_walk_state_t *wsp)
1838 {
1839 	wsp->walk_data = (void *)wsp->walk_addr;
1840 
1841 	if (mdb_layered_walk("zio_cache", wsp) == -1) {
1842 		mdb_warn("failed to walk 'zio_cache'\n");
1843 		return (WALK_ERR);
1844 	}
1845 
1846 	return (WALK_NEXT);
1847 }
1848 
1849 static int
1850 zio_walk_step(mdb_walk_state_t *wsp)
1851 {
1852 	zio_t zio;
1853 
1854 	if (mdb_vread(&zio, sizeof (zio), wsp->walk_addr) == -1) {
1855 		mdb_warn("failed to read zio_t at %p", wsp->walk_addr);
1856 		return (WALK_ERR);
1857 	}
1858 
1859 	if (wsp->walk_data != NULL && wsp->walk_data != zio.io_spa)
1860 		return (WALK_NEXT);
1861 
1862 	return (wsp->walk_callback(wsp->walk_addr, &zio, wsp->walk_cbdata));
1863 }
1864 
1865 /*
1866  * ::walk zio_child
1867  *
1868  * Walk the children of a zio_t structure.
1869  */
1870 static int
1871 zio_child_walk_init(mdb_walk_state_t *wsp)
1872 {
1873 	zio_t zio;
1874 
1875 	if (wsp->walk_addr == 0) {
1876 		mdb_warn("::walk zio_child doesn't support global walks\n");
1877 		return (WALK_ERR);
1878 	}
1879 
1880 	if (mdb_vread(&zio, sizeof (zio), wsp->walk_addr) == -1) {
1881 		mdb_warn("failed to read zio_t at %p", wsp->walk_addr);
1882 		return (WALK_ERR);
1883 	}
1884 
1885 	wsp->walk_addr = (uintptr_t)zio.io_child;
1886 	return (WALK_NEXT);
1887 }
1888 
1889 static int
1890 zio_sibling_walk_step(mdb_walk_state_t *wsp)
1891 {
1892 	zio_t zio;
1893 	int status;
1894 
1895 	if (wsp->walk_addr == NULL)
1896 		return (WALK_DONE);
1897 
1898 	if (mdb_vread(&zio, sizeof (zio), wsp->walk_addr) == -1) {
1899 		mdb_warn("failed to read zio_t at %p", wsp->walk_addr);
1900 		return (WALK_ERR);
1901 	}
1902 
1903 	status = wsp->walk_callback(wsp->walk_addr, &zio, wsp->walk_cbdata);
1904 
1905 	wsp->walk_addr = (uintptr_t)zio.io_sibling_next;
1906 	return (status);
1907 }
1908 
1909 /*
1910  * [addr]::walk zio_root
1911  *
1912  * Walk only root zio_t structures, optionally for a particular spa_t.
1913  */
1914 static int
1915 zio_walk_root_step(mdb_walk_state_t *wsp)
1916 {
1917 	zio_t zio;
1918 
1919 	if (mdb_vread(&zio, sizeof (zio), wsp->walk_addr) == -1) {
1920 		mdb_warn("failed to read zio_t at %p", wsp->walk_addr);
1921 		return (WALK_ERR);
1922 	}
1923 
1924 	if (wsp->walk_data != NULL && wsp->walk_data != zio.io_spa)
1925 		return (WALK_NEXT);
1926 
1927 	if ((uintptr_t)zio.io_root != wsp->walk_addr)
1928 		return (WALK_NEXT);
1929 
1930 	return (wsp->walk_callback(wsp->walk_addr, &zio, wsp->walk_cbdata));
1931 }
1932 
1933 /*
1934  * MDB module linkage information:
1935  *
1936  * We declare a list of structures describing our dcmds, and a function
1937  * named _mdb_init to return a pointer to our module information.
1938  */
1939 
1940 static const mdb_dcmd_t dcmds[] = {
1941 	{ "arc", "[-bkmg]", "print ARC variables", arc_print },
1942 	{ "blkptr", ":", "print blkptr_t", blkptr },
1943 	{ "dbuf", ":", "print dmu_buf_impl_t", dbuf },
1944 	{ "dbuf_stats", ":", "dbuf stats", dbuf_stats },
1945 	{ "dbufs",
1946 	"\t[-O objset_impl_t*] [-n objset_name | \"mos\"] "
1947 	"[-o object | \"mdn\"] \n"
1948 	"\t[-l level] [-b blkid | \"bonus\"]",
1949 	"find dmu_buf_impl_t's that match specified criteria", dbufs },
1950 	{ "abuf_find", "dva_word[0] dva_word[1]",
1951 	"find arc_buf_hdr_t of a specified DVA",
1952 	abuf_find },
1953 	{ "spa", "?[-cv]", "spa_t summary", spa_print },
1954 	{ "spa_config", ":", "print spa_t configuration", spa_print_config },
1955 	{ "spa_verify", ":", "verify spa_t consistency", spa_verify },
1956 	{ "spa_space", ":[-b]", "print spa_t on-disk space usage", spa_space },
1957 	{ "spa_vdevs", ":", "given a spa_t, print vdev summary", spa_vdevs },
1958 	{ "vdev", ":[-re]\n"
1959 	"\t-r display recursively\n"
1960 	"\t-e print statistics\n",
1961 	"vdev_t summary", vdev_print },
1962 	{ "zio", ":", "zio_t summary", zio_print },
1963 	{ "zio_state", "?", "print out all zio_t structures on system or "
1964 	    "for a particular pool", zio_state },
1965 	{ "zio_pipeline", ":", "decode a zio pipeline", zio_pipeline },
1966 	{ "zfs_params", "", "print zfs tunable parameters", zfs_params },
1967 	{ NULL }
1968 };
1969 
1970 static const mdb_walker_t walkers[] = {
1971 	/*
1972 	 * In userland, there is no generic provider of list_t walkers, so we
1973 	 * need to add it.
1974 	 */
1975 #ifndef _KERNEL
1976 	{ LIST_WALK_NAME, LIST_WALK_DESC,
1977 		list_walk_init, list_walk_step, list_walk_fini },
1978 #endif
1979 	{ "zms_freelist", "walk ZFS metaslab freelist",
1980 		freelist_walk_init, freelist_walk_step, NULL },
1981 	{ "txg_list", "given any txg_list_t *, walk all entries in all txgs",
1982 		txg_list_walk_init, txg_list_walk_step, NULL },
1983 	{ "txg_list0", "given any txg_list_t *, walk all entries in txg 0",
1984 		txg_list0_walk_init, txg_list_walk_step, NULL },
1985 	{ "txg_list1", "given any txg_list_t *, walk all entries in txg 1",
1986 		txg_list1_walk_init, txg_list_walk_step, NULL },
1987 	{ "txg_list2", "given any txg_list_t *, walk all entries in txg 2",
1988 		txg_list2_walk_init, txg_list_walk_step, NULL },
1989 	{ "txg_list3", "given any txg_list_t *, walk all entries in txg 3",
1990 		txg_list3_walk_init, txg_list_walk_step, NULL },
1991 	{ "zio", "walk all zio structures, optionally for a particular spa_t",
1992 		zio_walk_init, zio_walk_step, NULL },
1993 	{ "zio_child", "walk children of a zio_t structure",
1994 		zio_child_walk_init, zio_sibling_walk_step, NULL },
1995 	{ "zio_root", "walk all root zio_t structures, optionally for a "
1996 	    "particular spa_t",
1997 		zio_walk_init, zio_walk_root_step, NULL },
1998 	{ "spa", "walk all spa_t entries in the namespace",
1999 		spa_walk_init, spa_walk_step, NULL },
2000 	{ "metaslab", "given a spa_t *, walk all metaslab_t structures",
2001 		metaslab_walk_init, metaslab_walk_step, NULL },
2002 	{ NULL }
2003 };
2004 
2005 static const mdb_modinfo_t modinfo = {
2006 	MDB_API_VERSION, dcmds, walkers
2007 };
2008 
2009 const mdb_modinfo_t *
2010 _mdb_init(void)
2011 {
2012 	return (&modinfo);
2013 }
2014