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