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