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