xref: /titanic_51/usr/src/cmd/mdb/common/modules/zfs/zfs.c (revision 5413ab1b25eb8f7cbf9070adc2e222772aa054da)
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 2009 Sun Microsystems, Inc.  All rights reserved.
23  * Use is subject to license terms.
24  */
25 
26 #include <mdb/mdb_ctf.h>
27 #include <sys/zfs_context.h>
28 #include <sys/mdb_modapi.h>
29 #include <sys/dbuf.h>
30 #include <sys/dmu_objset.h>
31 #include <sys/dsl_dir.h>
32 #include <sys/dsl_pool.h>
33 #include <sys/metaslab_impl.h>
34 #include <sys/space_map.h>
35 #include <sys/list.h>
36 #include <sys/spa_impl.h>
37 #include <sys/vdev_impl.h>
38 #include <sys/zio_compress.h>
39 
40 #ifndef _KERNEL
41 #include "../genunix/list.h"
42 #endif
43 
44 #ifdef _KERNEL
45 #define	ZFS_OBJ_NAME	"zfs"
46 #else
47 #define	ZFS_OBJ_NAME	"libzpool.so.1"
48 #endif
49 
50 static char *
51 local_strdup(const char *s)
52 {
53 	char *s1 = mdb_alloc(strlen(s) + 1, UM_SLEEP);
54 
55 	(void) strcpy(s1, s);
56 	return (s1);
57 }
58 
59 static int
60 getmember(uintptr_t addr, const char *type, mdb_ctf_id_t *idp,
61     const char *member, int len, void *buf)
62 {
63 	mdb_ctf_id_t id;
64 	ulong_t off;
65 	char name[64];
66 
67 	if (idp == NULL) {
68 		if (mdb_ctf_lookup_by_name(type, &id) == -1) {
69 			mdb_warn("couldn't find type %s", type);
70 			return (DCMD_ERR);
71 		}
72 		idp = &id;
73 	} else {
74 		type = name;
75 		mdb_ctf_type_name(*idp, name, sizeof (name));
76 	}
77 
78 	if (mdb_ctf_offsetof(*idp, member, &off) == -1) {
79 		mdb_warn("couldn't find member %s of type %s\n", member, type);
80 		return (DCMD_ERR);
81 	}
82 	if (off % 8 != 0) {
83 		mdb_warn("member %s of type %s is unsupported bitfield",
84 		    member, type);
85 		return (DCMD_ERR);
86 	}
87 	off /= 8;
88 
89 	if (mdb_vread(buf, len, addr + off) == -1) {
90 		mdb_warn("failed to read %s from %s at %p",
91 		    member, type, addr + off);
92 		return (DCMD_ERR);
93 	}
94 	/* mdb_warn("read %s from %s at %p+%llx\n", member, type, addr, off); */
95 
96 	return (0);
97 }
98 
99 #define	GETMEMB(addr, type, member, dest) \
100 	getmember(addr, #type, NULL, #member, sizeof (dest), &(dest))
101 
102 #define	GETMEMBID(addr, ctfid, member, dest) \
103 	getmember(addr, NULL, ctfid, #member, sizeof (dest), &(dest))
104 
105 static int
106 getrefcount(uintptr_t addr, mdb_ctf_id_t *id,
107     const char *member, uint64_t *rc)
108 {
109 	static int gotid;
110 	static mdb_ctf_id_t rc_id;
111 	ulong_t off;
112 
113 	if (!gotid) {
114 		if (mdb_ctf_lookup_by_name("struct refcount", &rc_id) == -1) {
115 			mdb_warn("couldn't find struct refcount");
116 			return (DCMD_ERR);
117 		}
118 		gotid = TRUE;
119 	}
120 
121 	if (mdb_ctf_offsetof(*id, member, &off) == -1) {
122 		char name[64];
123 		mdb_ctf_type_name(*id, name, sizeof (name));
124 		mdb_warn("couldn't find member %s of type %s\n", member, name);
125 		return (DCMD_ERR);
126 	}
127 	off /= 8;
128 
129 	return (GETMEMBID(addr + off, &rc_id, rc_count, *rc));
130 }
131 
132 static int
133 read_symbol(char *sym_name, void **bufp)
134 {
135 	GElf_Sym sym;
136 
137 	if (mdb_lookup_by_obj(MDB_TGT_OBJ_EVERY, sym_name, &sym)) {
138 		mdb_warn("can't find symbol %s", sym_name);
139 		return (DCMD_ERR);
140 	}
141 
142 	*bufp = mdb_alloc(sym.st_size, UM_SLEEP);
143 
144 	if (mdb_vread(*bufp, sym.st_size, sym.st_value) == -1) {
145 		mdb_warn("can't read data for symbol %s", sym_name);
146 		mdb_free(*bufp, sym.st_size);
147 		return (DCMD_ERR);
148 	}
149 
150 	return (DCMD_OK);
151 }
152 
153 static int verbose;
154 
155 static int
156 freelist_walk_init(mdb_walk_state_t *wsp)
157 {
158 	if (wsp->walk_addr == NULL) {
159 		mdb_warn("must supply starting address\n");
160 		return (WALK_ERR);
161 	}
162 
163 	wsp->walk_data = 0;  /* Index into the freelist */
164 	return (WALK_NEXT);
165 }
166 
167 static int
168 freelist_walk_step(mdb_walk_state_t *wsp)
169 {
170 	uint64_t entry;
171 	uintptr_t number = (uintptr_t)wsp->walk_data;
172 	char *ddata[] = { "ALLOC", "FREE", "CONDENSE", "INVALID",
173 			    "INVALID", "INVALID", "INVALID", "INVALID" };
174 	int mapshift = SPA_MINBLOCKSHIFT;
175 
176 	if (mdb_vread(&entry, sizeof (entry), wsp->walk_addr) == -1) {
177 		mdb_warn("failed to read freelist entry %p", wsp->walk_addr);
178 		return (WALK_DONE);
179 	}
180 	wsp->walk_addr += sizeof (entry);
181 	wsp->walk_data = (void *)(number + 1);
182 
183 	if (SM_DEBUG_DECODE(entry)) {
184 		mdb_printf("DEBUG: %3u  %10s: txg=%llu  pass=%llu\n",
185 		    number,
186 		    ddata[SM_DEBUG_ACTION_DECODE(entry)],
187 		    SM_DEBUG_TXG_DECODE(entry),
188 		    SM_DEBUG_SYNCPASS_DECODE(entry));
189 	} else {
190 		mdb_printf("Entry: %3u  offsets=%08llx-%08llx  type=%c  "
191 		    "size=%06llx", number,
192 		    SM_OFFSET_DECODE(entry) << mapshift,
193 		    (SM_OFFSET_DECODE(entry) + SM_RUN_DECODE(entry)) <<
194 		    mapshift,
195 		    SM_TYPE_DECODE(entry) == SM_ALLOC ? 'A' : 'F',
196 		    SM_RUN_DECODE(entry) << mapshift);
197 		if (verbose)
198 			mdb_printf("      (raw=%012llx)\n", entry);
199 		mdb_printf("\n");
200 	}
201 	return (WALK_NEXT);
202 }
203 
204 
205 static int
206 dataset_name(uintptr_t addr, char *buf)
207 {
208 	static int gotid;
209 	static mdb_ctf_id_t dd_id;
210 	uintptr_t dd_parent;
211 	char dd_myname[MAXNAMELEN];
212 
213 	if (!gotid) {
214 		if (mdb_ctf_lookup_by_name("struct dsl_dir",
215 		    &dd_id) == -1) {
216 			mdb_warn("couldn't find struct dsl_dir");
217 			return (DCMD_ERR);
218 		}
219 		gotid = TRUE;
220 	}
221 	if (GETMEMBID(addr, &dd_id, dd_parent, dd_parent) ||
222 	    GETMEMBID(addr, &dd_id, dd_myname, dd_myname)) {
223 		return (DCMD_ERR);
224 	}
225 
226 	if (dd_parent) {
227 		if (dataset_name(dd_parent, buf))
228 			return (DCMD_ERR);
229 		strcat(buf, "/");
230 	}
231 
232 	if (dd_myname[0])
233 		strcat(buf, dd_myname);
234 	else
235 		strcat(buf, "???");
236 
237 	return (0);
238 }
239 
240 static int
241 objset_name(uintptr_t addr, char *buf)
242 {
243 	static int gotid;
244 	static mdb_ctf_id_t os_id, ds_id;
245 	uintptr_t os_dsl_dataset;
246 	char ds_snapname[MAXNAMELEN];
247 	uintptr_t ds_dir;
248 
249 	buf[0] = '\0';
250 
251 	if (!gotid) {
252 		if (mdb_ctf_lookup_by_name("struct objset",
253 		    &os_id) == -1) {
254 			mdb_warn("couldn't find struct objset");
255 			return (DCMD_ERR);
256 		}
257 		if (mdb_ctf_lookup_by_name("struct dsl_dataset",
258 		    &ds_id) == -1) {
259 			mdb_warn("couldn't find struct dsl_dataset");
260 			return (DCMD_ERR);
261 		}
262 
263 		gotid = TRUE;
264 	}
265 
266 	if (GETMEMBID(addr, &os_id, os_dsl_dataset, os_dsl_dataset))
267 		return (DCMD_ERR);
268 
269 	if (os_dsl_dataset == 0) {
270 		strcat(buf, "mos");
271 		return (0);
272 	}
273 
274 	if (GETMEMBID(os_dsl_dataset, &ds_id, ds_snapname, ds_snapname) ||
275 	    GETMEMBID(os_dsl_dataset, &ds_id, ds_dir, ds_dir)) {
276 		return (DCMD_ERR);
277 	}
278 
279 	if (ds_dir && dataset_name(ds_dir, buf))
280 		return (DCMD_ERR);
281 
282 	if (ds_snapname[0]) {
283 		strcat(buf, "@");
284 		strcat(buf, ds_snapname);
285 	}
286 	return (0);
287 }
288 
289 static void
290 enum_lookup(char *out, size_t size, mdb_ctf_id_t id, int val,
291     const char *prefix)
292 {
293 	const char *cp;
294 	size_t len = strlen(prefix);
295 
296 	if ((cp = mdb_ctf_enum_name(id, val)) != NULL) {
297 		if (strncmp(cp, prefix, len) == 0)
298 			cp += len;
299 		(void) strncpy(out, cp, size);
300 	} else {
301 		mdb_snprintf(out, size, "? (%d)", val);
302 	}
303 }
304 
305 /* ARGSUSED */
306 static int
307 zio_pipeline(uintptr_t addr, uint_t flags, int argc, const mdb_arg_t *argv)
308 {
309 	mdb_ctf_id_t pipe_enum;
310 	int i;
311 	char stage[1024];
312 
313 	if (mdb_ctf_lookup_by_name("enum zio_stage", &pipe_enum) == -1) {
314 		mdb_warn("Could not find enum zio_stage");
315 		return (DCMD_ERR);
316 	}
317 
318 	for (i = 0; i < 32; i++) {
319 		if (addr & (1U << i)) {
320 			enum_lookup(stage, sizeof (stage), pipe_enum, i,
321 			    "ZIO_STAGE_");
322 			mdb_printf("    %s\n", stage);
323 		}
324 	}
325 
326 	return (DCMD_OK);
327 }
328 
329 /* ARGSUSED */
330 static int
331 zfs_params(uintptr_t addr, uint_t flags, int argc, const mdb_arg_t *argv)
332 {
333 	/*
334 	 * This table can be approximately generated by running:
335 	 * egrep "^[a-z0-9_]+ [a-z0-9_]+( =.*)?;" *.c | cut -d ' ' -f 2
336 	 */
337 	static const char *params[] = {
338 		"arc_reduce_dnlc_percent",
339 		"zfs_arc_max",
340 		"zfs_arc_min",
341 		"arc_shrink_shift",
342 		"zfs_mdcomp_disable",
343 		"zfs_prefetch_disable",
344 		"zfetch_max_streams",
345 		"zfetch_min_sec_reap",
346 		"zfetch_block_cap",
347 		"zfetch_array_rd_sz",
348 		"zfs_default_bs",
349 		"zfs_default_ibs",
350 		"metaslab_aliquot",
351 		"reference_tracking_enable",
352 		"reference_history",
353 		"zio_taskq_threads",
354 		"spa_max_replication_override",
355 		"spa_mode",
356 		"zfs_flags",
357 		"zfs_txg_synctime",
358 		"zfs_txg_timeout",
359 		"zfs_write_limit_min",
360 		"zfs_write_limit_max",
361 		"zfs_write_limit_shift",
362 		"zfs_write_limit_override",
363 		"zfs_no_write_throttle",
364 		"zfs_vdev_cache_max",
365 		"zfs_vdev_cache_size",
366 		"zfs_vdev_cache_bshift",
367 		"vdev_mirror_shift",
368 		"zfs_vdev_max_pending",
369 		"zfs_vdev_min_pending",
370 		"zfs_scrub_limit",
371 		"zfs_vdev_time_shift",
372 		"zfs_vdev_ramp_rate",
373 		"zfs_vdev_aggregation_limit",
374 		"fzap_default_block_shift",
375 		"zfs_immediate_write_sz",
376 		"zfs_read_chunk_size",
377 		"zil_disable",
378 		"zfs_nocacheflush",
379 		"metaslab_gang_bang",
380 		"metaslab_df_alloc_threshold",
381 		"metaslab_df_free_pct",
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 	const char *statetab[] = { "ACTIVE", "EXPORTED", "DESTROYED",
913 		"SPARE", "L2CACHE", "UNINIT", "UNAVAIL", "POTENTIAL" };
914 	const char *state;
915 	int config = FALSE;
916 	int vdevs = FALSE;
917 	int errors = FALSE;
918 
919 	if (mdb_getopts(argc, argv,
920 	    'c', MDB_OPT_SETBITS, TRUE, &config,
921 	    'v', MDB_OPT_SETBITS, TRUE, &vdevs,
922 	    'e', MDB_OPT_SETBITS, TRUE, &errors,
923 	    NULL) != argc)
924 		return (DCMD_USAGE);
925 
926 	if (!(flags & DCMD_ADDRSPEC)) {
927 		if (mdb_walk_dcmd("spa", "spa", argc, argv) == -1) {
928 			mdb_warn("can't walk spa");
929 			return (DCMD_ERR);
930 		}
931 
932 		return (DCMD_OK);
933 	}
934 
935 	if (flags & DCMD_PIPE_OUT) {
936 		mdb_printf("%#lr\n", addr);
937 		return (DCMD_OK);
938 	}
939 
940 	if (DCMD_HDRSPEC(flags))
941 		mdb_printf("%<u>%-?s %9s %-*s%</u>\n", "ADDR", "STATE",
942 		    sizeof (uintptr_t) == 4 ? 60 : 52, "NAME");
943 
944 	if (mdb_vread(&spa, sizeof (spa), addr) == -1) {
945 		mdb_warn("failed to read spa_t at %p", addr);
946 		return (DCMD_ERR);
947 	}
948 
949 	if (spa.spa_state < 0 || spa.spa_state > POOL_STATE_UNAVAIL)
950 		state = "UNKNOWN";
951 	else
952 		state = statetab[spa.spa_state];
953 
954 	mdb_printf("%0?p %9s %s\n", addr, state, spa.spa_name);
955 
956 	if (config) {
957 		mdb_printf("\n");
958 		mdb_inc_indent(4);
959 		if (mdb_call_dcmd("spa_config", addr, flags, 0,
960 		    NULL) != DCMD_OK)
961 			return (DCMD_ERR);
962 		mdb_dec_indent(4);
963 	}
964 
965 	if (vdevs || errors) {
966 		mdb_arg_t v;
967 
968 		v.a_type = MDB_TYPE_STRING;
969 		v.a_un.a_str = "-e";
970 
971 		mdb_printf("\n");
972 		mdb_inc_indent(4);
973 		if (mdb_call_dcmd("spa_vdevs", addr, flags, errors ? 1 : 0,
974 		    &v) != DCMD_OK)
975 			return (DCMD_ERR);
976 		mdb_dec_indent(4);
977 	}
978 
979 	return (DCMD_OK);
980 }
981 
982 /*
983  * ::spa_config
984  *
985  * Given a spa_t, print the configuration information stored in spa_config.
986  * Since it's just an nvlist, format it as an indented list of name=value pairs.
987  * We simply read the value of spa_config and pass off to ::nvlist.
988  */
989 /* ARGSUSED */
990 static int
991 spa_print_config(uintptr_t addr, uint_t flags, int argc, const mdb_arg_t *argv)
992 {
993 	spa_t spa;
994 
995 	if (argc != 0 || !(flags & DCMD_ADDRSPEC))
996 		return (DCMD_USAGE);
997 
998 	if (mdb_vread(&spa, sizeof (spa), addr) == -1) {
999 		mdb_warn("failed to read spa_t at %p", addr);
1000 		return (DCMD_ERR);
1001 	}
1002 
1003 	if (spa.spa_config == NULL) {
1004 		mdb_printf("(none)\n");
1005 		return (DCMD_OK);
1006 	}
1007 
1008 	return (mdb_call_dcmd("nvlist", (uintptr_t)spa.spa_config, flags,
1009 	    0, NULL));
1010 }
1011 
1012 /*
1013  * ::vdev
1014  *
1015  * Print out a summarized vdev_t, in the following form:
1016  *
1017  * ADDR             STATE	AUX            DESC
1018  * fffffffbcde23df0 HEALTHY	-              /dev/dsk/c0t0d0
1019  *
1020  * If '-r' is specified, recursively visit all children.
1021  *
1022  * With '-e', the statistics associated with the vdev are printed as well.
1023  */
1024 static int
1025 do_print_vdev(uintptr_t addr, int flags, int depth, int stats,
1026     int recursive)
1027 {
1028 	vdev_t vdev;
1029 	char desc[MAXNAMELEN];
1030 	int c, children;
1031 	uintptr_t *child;
1032 	const char *state, *aux;
1033 
1034 	if (mdb_vread(&vdev, sizeof (vdev), (uintptr_t)addr) == -1) {
1035 		mdb_warn("failed to read vdev_t at %p\n", (uintptr_t)addr);
1036 		return (DCMD_ERR);
1037 	}
1038 
1039 	if (flags & DCMD_PIPE_OUT) {
1040 		mdb_printf("%#lr", addr);
1041 	} else {
1042 		if (vdev.vdev_path != NULL) {
1043 			if (mdb_readstr(desc, sizeof (desc),
1044 			    (uintptr_t)vdev.vdev_path) == -1) {
1045 				mdb_warn("failed to read vdev_path at %p\n",
1046 				    vdev.vdev_path);
1047 				return (DCMD_ERR);
1048 			}
1049 		} else if (vdev.vdev_ops != NULL) {
1050 			vdev_ops_t ops;
1051 			if (mdb_vread(&ops, sizeof (ops),
1052 			    (uintptr_t)vdev.vdev_ops) == -1) {
1053 				mdb_warn("failed to read vdev_ops at %p\n",
1054 				    vdev.vdev_ops);
1055 				return (DCMD_ERR);
1056 			}
1057 			(void) strcpy(desc, ops.vdev_op_type);
1058 		} else {
1059 			(void) strcpy(desc, "<unknown>");
1060 		}
1061 
1062 		if (depth == 0 && DCMD_HDRSPEC(flags))
1063 			mdb_printf("%<u>%-?s %-9s %-12s %-*s%</u>\n",
1064 			    "ADDR", "STATE", "AUX",
1065 			    sizeof (uintptr_t) == 4 ? 43 : 35,
1066 			    "DESCRIPTION");
1067 
1068 		mdb_printf("%0?p ", addr);
1069 
1070 		switch (vdev.vdev_state) {
1071 		case VDEV_STATE_CLOSED:
1072 			state = "CLOSED";
1073 			break;
1074 		case VDEV_STATE_OFFLINE:
1075 			state = "OFFLINE";
1076 			break;
1077 		case VDEV_STATE_CANT_OPEN:
1078 			state = "CANT_OPEN";
1079 			break;
1080 		case VDEV_STATE_DEGRADED:
1081 			state = "DEGRADED";
1082 			break;
1083 		case VDEV_STATE_HEALTHY:
1084 			state = "HEALTHY";
1085 			break;
1086 		case VDEV_STATE_REMOVED:
1087 			state = "REMOVED";
1088 			break;
1089 		case VDEV_STATE_FAULTED:
1090 			state = "FAULTED";
1091 			break;
1092 		default:
1093 			state = "UNKNOWN";
1094 			break;
1095 		}
1096 
1097 		switch (vdev.vdev_stat.vs_aux) {
1098 		case VDEV_AUX_NONE:
1099 			aux = "-";
1100 			break;
1101 		case VDEV_AUX_OPEN_FAILED:
1102 			aux = "OPEN_FAILED";
1103 			break;
1104 		case VDEV_AUX_CORRUPT_DATA:
1105 			aux = "CORRUPT_DATA";
1106 			break;
1107 		case VDEV_AUX_NO_REPLICAS:
1108 			aux = "NO_REPLICAS";
1109 			break;
1110 		case VDEV_AUX_BAD_GUID_SUM:
1111 			aux = "BAD_GUID_SUM";
1112 			break;
1113 		case VDEV_AUX_TOO_SMALL:
1114 			aux = "TOO_SMALL";
1115 			break;
1116 		case VDEV_AUX_BAD_LABEL:
1117 			aux = "BAD_LABEL";
1118 			break;
1119 		case VDEV_AUX_VERSION_NEWER:
1120 			aux = "VERS_NEWER";
1121 			break;
1122 		case VDEV_AUX_VERSION_OLDER:
1123 			aux = "VERS_OLDER";
1124 			break;
1125 		case VDEV_AUX_SPARED:
1126 			aux = "SPARED";
1127 			break;
1128 		case VDEV_AUX_ERR_EXCEEDED:
1129 			aux = "ERR_EXCEEDED";
1130 			break;
1131 		case VDEV_AUX_IO_FAILURE:
1132 			aux = "IO_FAILURE";
1133 			break;
1134 		case VDEV_AUX_BAD_LOG:
1135 			aux = "BAD_LOG";
1136 			break;
1137 		default:
1138 			aux = "UNKNOWN";
1139 			break;
1140 		}
1141 
1142 		mdb_printf("%-9s %-12s %*s%s\n", state, aux, depth, "", desc);
1143 
1144 		if (stats) {
1145 			vdev_stat_t *vs = &vdev.vdev_stat;
1146 			int i;
1147 
1148 			mdb_inc_indent(4);
1149 			mdb_printf("\n");
1150 			mdb_printf("%<u>       %12s %12s %12s %12s "
1151 			    "%12s%</u>\n", "READ", "WRITE", "FREE", "CLAIM",
1152 			    "IOCTL");
1153 			mdb_printf("OPS     ");
1154 			for (i = 1; i < ZIO_TYPES; i++)
1155 				mdb_printf("%11#llx%s", vs->vs_ops[i],
1156 				    i == ZIO_TYPES - 1 ? "" : "  ");
1157 			mdb_printf("\n");
1158 			mdb_printf("BYTES   ");
1159 			for (i = 1; i < ZIO_TYPES; i++)
1160 				mdb_printf("%11#llx%s", vs->vs_bytes[i],
1161 				    i == ZIO_TYPES - 1 ? "" : "  ");
1162 
1163 
1164 			mdb_printf("\n");
1165 			mdb_printf("EREAD    %10#llx\n", vs->vs_read_errors);
1166 			mdb_printf("EWRITE   %10#llx\n", vs->vs_write_errors);
1167 			mdb_printf("ECKSUM   %10#llx\n",
1168 			    vs->vs_checksum_errors);
1169 			mdb_dec_indent(4);
1170 		}
1171 
1172 		if (stats)
1173 			mdb_printf("\n");
1174 	}
1175 
1176 	children = vdev.vdev_children;
1177 
1178 	if (children == 0 || !recursive)
1179 		return (DCMD_OK);
1180 
1181 	child = mdb_alloc(children * sizeof (void *), UM_SLEEP | UM_GC);
1182 	if (mdb_vread(child, children * sizeof (void *),
1183 	    (uintptr_t)vdev.vdev_child) == -1) {
1184 		mdb_warn("failed to read vdev children at %p", vdev.vdev_child);
1185 		return (DCMD_ERR);
1186 	}
1187 
1188 	for (c = 0; c < children; c++) {
1189 		if (do_print_vdev(child[c], flags, depth + 2, stats,
1190 		    recursive))
1191 			return (DCMD_ERR);
1192 	}
1193 
1194 	return (DCMD_OK);
1195 }
1196 
1197 static int
1198 vdev_print(uintptr_t addr, uint_t flags, int argc, const mdb_arg_t *argv)
1199 {
1200 	int recursive = FALSE;
1201 	int stats = FALSE;
1202 	uint64_t depth = 0;
1203 
1204 	if (mdb_getopts(argc, argv,
1205 	    'r', MDB_OPT_SETBITS, TRUE, &recursive,
1206 	    'e', MDB_OPT_SETBITS, TRUE, &stats,
1207 	    'd', MDB_OPT_UINT64, &depth,
1208 	    NULL) != argc)
1209 		return (DCMD_USAGE);
1210 
1211 	if (!(flags & DCMD_ADDRSPEC)) {
1212 		mdb_warn("no vdev_t address given\n");
1213 		return (DCMD_ERR);
1214 	}
1215 
1216 	return (do_print_vdev(addr, flags, (int)depth, stats, recursive));
1217 }
1218 
1219 typedef struct metaslab_walk_data {
1220 	uint64_t mw_numvdevs;
1221 	uintptr_t *mw_vdevs;
1222 	int mw_curvdev;
1223 	uint64_t mw_nummss;
1224 	uintptr_t *mw_mss;
1225 	int mw_curms;
1226 } metaslab_walk_data_t;
1227 
1228 static int
1229 metaslab_walk_step(mdb_walk_state_t *wsp)
1230 {
1231 	metaslab_walk_data_t *mw = wsp->walk_data;
1232 	metaslab_t ms;
1233 	uintptr_t msp;
1234 
1235 	if (mw->mw_curvdev >= mw->mw_numvdevs)
1236 		return (WALK_DONE);
1237 
1238 	if (mw->mw_mss == NULL) {
1239 		uintptr_t mssp;
1240 		uintptr_t vdevp;
1241 
1242 		ASSERT(mw->mw_curms == 0);
1243 		ASSERT(mw->mw_nummss == 0);
1244 
1245 		vdevp = mw->mw_vdevs[mw->mw_curvdev];
1246 		if (GETMEMB(vdevp, struct vdev, vdev_ms, mssp) ||
1247 		    GETMEMB(vdevp, struct vdev, vdev_ms_count, mw->mw_nummss)) {
1248 			return (WALK_ERR);
1249 		}
1250 
1251 		mw->mw_mss = mdb_alloc(mw->mw_nummss * sizeof (void*),
1252 		    UM_SLEEP | UM_GC);
1253 		if (mdb_vread(mw->mw_mss, mw->mw_nummss * sizeof (void*),
1254 		    mssp) == -1) {
1255 			mdb_warn("failed to read vdev_ms at %p", mssp);
1256 			return (WALK_ERR);
1257 		}
1258 	}
1259 
1260 	if (mw->mw_curms >= mw->mw_nummss) {
1261 		mw->mw_mss = NULL;
1262 		mw->mw_curms = 0;
1263 		mw->mw_nummss = 0;
1264 		mw->mw_curvdev++;
1265 		return (WALK_NEXT);
1266 	}
1267 
1268 	msp = mw->mw_mss[mw->mw_curms];
1269 	if (mdb_vread(&ms, sizeof (metaslab_t), msp) == -1) {
1270 		mdb_warn("failed to read metaslab_t at %p", msp);
1271 		return (WALK_ERR);
1272 	}
1273 
1274 	mw->mw_curms++;
1275 
1276 	return (wsp->walk_callback(msp, &ms, wsp->walk_cbdata));
1277 }
1278 
1279 /* ARGSUSED */
1280 static int
1281 metaslab_walk_init(mdb_walk_state_t *wsp)
1282 {
1283 	metaslab_walk_data_t *mw;
1284 	uintptr_t root_vdevp;
1285 	uintptr_t childp;
1286 
1287 	if (wsp->walk_addr == NULL) {
1288 		mdb_warn("must supply address of spa_t\n");
1289 		return (WALK_ERR);
1290 	}
1291 
1292 	mw = mdb_zalloc(sizeof (metaslab_walk_data_t), UM_SLEEP | UM_GC);
1293 
1294 	if (GETMEMB(wsp->walk_addr, struct spa, spa_root_vdev, root_vdevp) ||
1295 	    GETMEMB(root_vdevp, struct vdev, vdev_children, mw->mw_numvdevs) ||
1296 	    GETMEMB(root_vdevp, struct vdev, vdev_child, childp)) {
1297 		return (DCMD_ERR);
1298 	}
1299 
1300 	mw->mw_vdevs = mdb_alloc(mw->mw_numvdevs * sizeof (void *),
1301 	    UM_SLEEP | UM_GC);
1302 	if (mdb_vread(mw->mw_vdevs, mw->mw_numvdevs * sizeof (void *),
1303 	    childp) == -1) {
1304 		mdb_warn("failed to read root vdev children at %p", childp);
1305 		return (DCMD_ERR);
1306 	}
1307 
1308 	wsp->walk_data = mw;
1309 
1310 	return (WALK_NEXT);
1311 }
1312 
1313 typedef struct mdb_spa {
1314 	uintptr_t spa_dsl_pool;
1315 	uintptr_t spa_root_vdev;
1316 } mdb_spa_t;
1317 
1318 typedef struct mdb_dsl_dir {
1319 	uintptr_t dd_phys;
1320 	int64_t dd_space_towrite[TXG_SIZE];
1321 } mdb_dsl_dir_t;
1322 
1323 typedef struct mdb_dsl_dir_phys {
1324 	uint64_t dd_used_bytes;
1325 	uint64_t dd_compressed_bytes;
1326 	uint64_t dd_uncompressed_bytes;
1327 } mdb_dsl_dir_phys_t;
1328 
1329 typedef struct mdb_vdev {
1330 	uintptr_t vdev_parent;
1331 	uintptr_t vdev_ms;
1332 	uint64_t vdev_ms_count;
1333 	vdev_stat_t vdev_stat;
1334 } mdb_vdev_t;
1335 
1336 typedef struct mdb_metaslab {
1337 	space_map_t ms_allocmap[TXG_SIZE];
1338 	space_map_t ms_freemap[TXG_SIZE];
1339 	space_map_t ms_map;
1340 	space_map_obj_t ms_smo;
1341 	space_map_obj_t ms_smo_syncing;
1342 } mdb_metaslab_t;
1343 
1344 typedef struct space_data {
1345 	uint64_t ms_allocmap[TXG_SIZE];
1346 	uint64_t ms_freemap[TXG_SIZE];
1347 	uint64_t ms_map;
1348 	uint64_t avail;
1349 	uint64_t nowavail;
1350 } space_data_t;
1351 
1352 /* ARGSUSED */
1353 static int
1354 space_cb(uintptr_t addr, const void *unknown, void *arg)
1355 {
1356 	space_data_t *sd = arg;
1357 	mdb_metaslab_t ms;
1358 
1359 	if (GETMEMB(addr, struct metaslab, ms_allocmap, ms.ms_allocmap) ||
1360 	    GETMEMB(addr, struct metaslab, ms_freemap, ms.ms_freemap) ||
1361 	    GETMEMB(addr, struct metaslab, ms_map, ms.ms_map) ||
1362 	    GETMEMB(addr, struct metaslab, ms_smo, ms.ms_smo) ||
1363 	    GETMEMB(addr, struct metaslab, ms_smo_syncing, ms.ms_smo_syncing)) {
1364 		return (WALK_ERR);
1365 	}
1366 
1367 	sd->ms_allocmap[0] += ms.ms_allocmap[0].sm_space;
1368 	sd->ms_allocmap[1] += ms.ms_allocmap[1].sm_space;
1369 	sd->ms_allocmap[2] += ms.ms_allocmap[2].sm_space;
1370 	sd->ms_allocmap[3] += ms.ms_allocmap[3].sm_space;
1371 	sd->ms_freemap[0] += ms.ms_freemap[0].sm_space;
1372 	sd->ms_freemap[1] += ms.ms_freemap[1].sm_space;
1373 	sd->ms_freemap[2] += ms.ms_freemap[2].sm_space;
1374 	sd->ms_freemap[3] += ms.ms_freemap[3].sm_space;
1375 	sd->ms_map += ms.ms_map.sm_space;
1376 	sd->avail += ms.ms_map.sm_size - ms.ms_smo.smo_alloc;
1377 	sd->nowavail += ms.ms_map.sm_size - ms.ms_smo_syncing.smo_alloc;
1378 
1379 	return (WALK_NEXT);
1380 }
1381 
1382 /*
1383  * ::spa_space [-b]
1384  *
1385  * Given a spa_t, print out it's on-disk space usage and in-core
1386  * estimates of future usage.  If -b is given, print space in bytes.
1387  * Otherwise print in megabytes.
1388  */
1389 /* ARGSUSED */
1390 static int
1391 spa_space(uintptr_t addr, uint_t flags, int argc, const mdb_arg_t *argv)
1392 {
1393 	mdb_spa_t spa;
1394 	uintptr_t dp_root_dir;
1395 	mdb_dsl_dir_t dd;
1396 	mdb_dsl_dir_phys_t dsp;
1397 	uint64_t children;
1398 	uintptr_t childaddr;
1399 	space_data_t sd;
1400 	int shift = 20;
1401 	char *suffix = "M";
1402 	int bits = FALSE;
1403 
1404 	if (mdb_getopts(argc, argv, 'b', MDB_OPT_SETBITS, TRUE, &bits, NULL) !=
1405 	    argc)
1406 		return (DCMD_USAGE);
1407 	if (!(flags & DCMD_ADDRSPEC))
1408 		return (DCMD_USAGE);
1409 
1410 	if (bits) {
1411 		shift = 0;
1412 		suffix = "";
1413 	}
1414 
1415 	if (GETMEMB(addr, struct spa, spa_dsl_pool, spa.spa_dsl_pool) ||
1416 	    GETMEMB(addr, struct spa, spa_root_vdev, spa.spa_root_vdev) ||
1417 	    GETMEMB(spa.spa_root_vdev, struct vdev, vdev_children, children) ||
1418 	    GETMEMB(spa.spa_root_vdev, struct vdev, vdev_child, childaddr) ||
1419 	    GETMEMB(spa.spa_dsl_pool, struct dsl_pool,
1420 	    dp_root_dir, dp_root_dir) ||
1421 	    GETMEMB(dp_root_dir, struct dsl_dir, dd_phys, dd.dd_phys) ||
1422 	    GETMEMB(dp_root_dir, struct dsl_dir,
1423 	    dd_space_towrite, dd.dd_space_towrite) ||
1424 	    GETMEMB(dd.dd_phys, struct dsl_dir_phys,
1425 	    dd_used_bytes, dsp.dd_used_bytes) ||
1426 	    GETMEMB(dd.dd_phys, struct dsl_dir_phys,
1427 	    dd_compressed_bytes, dsp.dd_compressed_bytes) ||
1428 	    GETMEMB(dd.dd_phys, struct dsl_dir_phys,
1429 	    dd_uncompressed_bytes, dsp.dd_uncompressed_bytes)) {
1430 		return (DCMD_ERR);
1431 	}
1432 
1433 	mdb_printf("dd_space_towrite = %llu%s %llu%s %llu%s %llu%s\n",
1434 	    dd.dd_space_towrite[0] >> shift, suffix,
1435 	    dd.dd_space_towrite[1] >> shift, suffix,
1436 	    dd.dd_space_towrite[2] >> shift, suffix,
1437 	    dd.dd_space_towrite[3] >> shift, suffix);
1438 
1439 	mdb_printf("dd_phys.dd_used_bytes = %llu%s\n",
1440 	    dsp.dd_used_bytes >> shift, suffix);
1441 	mdb_printf("dd_phys.dd_compressed_bytes = %llu%s\n",
1442 	    dsp.dd_compressed_bytes >> shift, suffix);
1443 	mdb_printf("dd_phys.dd_uncompressed_bytes = %llu%s\n",
1444 	    dsp.dd_uncompressed_bytes >> shift, suffix);
1445 
1446 	bzero(&sd, sizeof (sd));
1447 	if (mdb_pwalk("metaslab", space_cb, &sd, addr) != 0) {
1448 		mdb_warn("can't walk metaslabs");
1449 		return (DCMD_ERR);
1450 	}
1451 
1452 	mdb_printf("ms_allocmap = %llu%s %llu%s %llu%s %llu%s\n",
1453 	    sd.ms_allocmap[0] >> shift, suffix,
1454 	    sd.ms_allocmap[1] >> shift, suffix,
1455 	    sd.ms_allocmap[2] >> shift, suffix,
1456 	    sd.ms_allocmap[3] >> shift, suffix);
1457 	mdb_printf("ms_freemap = %llu%s %llu%s %llu%s %llu%s\n",
1458 	    sd.ms_freemap[0] >> shift, suffix,
1459 	    sd.ms_freemap[1] >> shift, suffix,
1460 	    sd.ms_freemap[2] >> shift, suffix,
1461 	    sd.ms_freemap[3] >> shift, suffix);
1462 	mdb_printf("ms_map = %llu%s\n", sd.ms_map >> shift, suffix);
1463 	mdb_printf("last synced avail = %llu%s\n", sd.avail >> shift, suffix);
1464 	mdb_printf("current syncing avail = %llu%s\n",
1465 	    sd.nowavail >> shift, suffix);
1466 
1467 	return (DCMD_OK);
1468 }
1469 
1470 /*
1471  * ::spa_verify
1472  *
1473  * Given a spa_t, verify that that the pool is self-consistent.
1474  * Currently, it only checks to make sure that the vdev tree exists.
1475  */
1476 /* ARGSUSED */
1477 static int
1478 spa_verify(uintptr_t addr, uint_t flags, int argc, const mdb_arg_t *argv)
1479 {
1480 	spa_t spa;
1481 
1482 	if (argc != 0 || !(flags & DCMD_ADDRSPEC))
1483 		return (DCMD_USAGE);
1484 
1485 	if (mdb_vread(&spa, sizeof (spa), addr) == -1) {
1486 		mdb_warn("failed to read spa_t at %p", addr);
1487 		return (DCMD_ERR);
1488 	}
1489 
1490 	if (spa.spa_root_vdev == NULL) {
1491 		mdb_printf("no vdev tree present\n");
1492 		return (DCMD_OK);
1493 	}
1494 
1495 	return (DCMD_OK);
1496 }
1497 
1498 static int
1499 spa_print_aux(spa_aux_vdev_t *sav, uint_t flags, mdb_arg_t *v,
1500     const char *name)
1501 {
1502 	uintptr_t *aux;
1503 	size_t len;
1504 	int ret, i;
1505 
1506 	/*
1507 	 * Iterate over aux vdevs and print those out as well.  This is a
1508 	 * little annoying because we don't have a root vdev to pass to ::vdev.
1509 	 * Instead, we print a single line and then call it for each child
1510 	 * vdev.
1511 	 */
1512 	if (sav->sav_count != 0) {
1513 		v[1].a_type = MDB_TYPE_STRING;
1514 		v[1].a_un.a_str = "-d";
1515 		v[2].a_type = MDB_TYPE_IMMEDIATE;
1516 		v[2].a_un.a_val = 2;
1517 
1518 		len = sav->sav_count * sizeof (uintptr_t);
1519 		aux = mdb_alloc(len, UM_SLEEP);
1520 		if (mdb_vread(aux, len,
1521 		    (uintptr_t)sav->sav_vdevs) == -1) {
1522 			mdb_free(aux, len);
1523 			mdb_warn("failed to read l2cache vdevs at %p",
1524 			    sav->sav_vdevs);
1525 			return (DCMD_ERR);
1526 		}
1527 
1528 		mdb_printf("%-?s %-9s %-12s %s\n", "-", "-", "-", name);
1529 
1530 		for (i = 0; i < sav->sav_count; i++) {
1531 			ret = mdb_call_dcmd("vdev", aux[i], flags, 3, v);
1532 			if (ret != DCMD_OK) {
1533 				mdb_free(aux, len);
1534 				return (ret);
1535 			}
1536 		}
1537 
1538 		mdb_free(aux, len);
1539 	}
1540 
1541 	return (0);
1542 }
1543 
1544 /*
1545  * ::spa_vdevs
1546  *
1547  * 	-e	Include error stats
1548  *
1549  * Print out a summarized list of vdevs for the given spa_t.
1550  * This is accomplished by invoking "::vdev -re" on the root vdev, as well as
1551  * iterating over the cache devices.
1552  */
1553 /* ARGSUSED */
1554 static int
1555 spa_vdevs(uintptr_t addr, uint_t flags, int argc, const mdb_arg_t *argv)
1556 {
1557 	spa_t spa;
1558 	mdb_arg_t v[3];
1559 	int errors = FALSE;
1560 	int ret;
1561 
1562 	if (mdb_getopts(argc, argv,
1563 	    'e', MDB_OPT_SETBITS, TRUE, &errors,
1564 	    NULL) != argc)
1565 		return (DCMD_USAGE);
1566 
1567 	if (!(flags & DCMD_ADDRSPEC))
1568 		return (DCMD_USAGE);
1569 
1570 	if (mdb_vread(&spa, sizeof (spa), addr) == -1) {
1571 		mdb_warn("failed to read spa_t at %p", addr);
1572 		return (DCMD_ERR);
1573 	}
1574 
1575 	/*
1576 	 * Unitialized spa_t structures can have a NULL root vdev.
1577 	 */
1578 	if (spa.spa_root_vdev == NULL) {
1579 		mdb_printf("no associated vdevs\n");
1580 		return (DCMD_OK);
1581 	}
1582 
1583 	v[0].a_type = MDB_TYPE_STRING;
1584 	v[0].a_un.a_str = errors ? "-re" : "-r";
1585 
1586 	ret = mdb_call_dcmd("vdev", (uintptr_t)spa.spa_root_vdev,
1587 	    flags, 1, v);
1588 	if (ret != DCMD_OK)
1589 		return (ret);
1590 
1591 	if (spa_print_aux(&spa.spa_l2cache, flags, v, "cache") != 0 ||
1592 	    spa_print_aux(&spa.spa_spares, flags, v, "spares") != 0)
1593 		return (DCMD_ERR);
1594 
1595 	return (DCMD_OK);
1596 }
1597 
1598 /*
1599  * ::zio
1600  *
1601  * Print a summary of zio_t and all its children.  This is intended to display a
1602  * zio tree, and hence we only pick the most important pieces of information for
1603  * the main summary.  More detailed information can always be found by doing a
1604  * '::print zio' on the underlying zio_t.  The columns we display are:
1605  *
1606  *	ADDRESS		TYPE	STAGE		WAITER
1607  *
1608  * The 'address' column is indented by one space for each depth level as we
1609  * descend down the tree.
1610  */
1611 
1612 #define	ZIO_MAXINDENT	24
1613 #define	ZIO_MAXWIDTH	(sizeof (uintptr_t) * 2 + ZIO_MAXINDENT)
1614 #define	ZIO_WALK_SELF	0
1615 #define	ZIO_WALK_CHILD	1
1616 #define	ZIO_WALK_PARENT	2
1617 
1618 typedef struct zio_print_args {
1619 	int	zpa_current_depth;
1620 	int	zpa_min_depth;
1621 	int	zpa_max_depth;
1622 	int	zpa_type;
1623 	uint_t	zpa_flags;
1624 } zio_print_args_t;
1625 
1626 static int zio_child_cb(uintptr_t addr, const void *unknown, void *arg);
1627 
1628 static int
1629 zio_print_cb(uintptr_t addr, const void *data, void *priv)
1630 {
1631 	const zio_t *zio = data;
1632 	zio_print_args_t *zpa = priv;
1633 	mdb_ctf_id_t type_enum, stage_enum;
1634 	int indent = zpa->zpa_current_depth;
1635 	const char *type, *stage;
1636 	uintptr_t laddr;
1637 
1638 	if (indent > ZIO_MAXINDENT)
1639 		indent = ZIO_MAXINDENT;
1640 
1641 	if (mdb_ctf_lookup_by_name("enum zio_type", &type_enum) == -1 ||
1642 	    mdb_ctf_lookup_by_name("enum zio_stage", &stage_enum) == -1) {
1643 		mdb_warn("failed to lookup zio enums");
1644 		return (WALK_ERR);
1645 	}
1646 
1647 	if ((type = mdb_ctf_enum_name(type_enum, zio->io_type)) != NULL)
1648 		type += sizeof ("ZIO_TYPE_") - 1;
1649 	else
1650 		type = "?";
1651 
1652 	if ((stage = mdb_ctf_enum_name(stage_enum, zio->io_stage)) != NULL)
1653 		stage += sizeof ("ZIO_STAGE_") - 1;
1654 	else
1655 		stage = "?";
1656 
1657 	if (zpa->zpa_current_depth >= zpa->zpa_min_depth) {
1658 		if (zpa->zpa_flags & DCMD_PIPE_OUT) {
1659 			mdb_printf("%?p\n", addr);
1660 		} else {
1661 			mdb_printf("%*s%-*p %-5s %-16s ", indent, "",
1662 			    ZIO_MAXWIDTH - indent, addr, type, stage);
1663 			if (zio->io_waiter)
1664 				mdb_printf("%?p\n", zio->io_waiter);
1665 			else
1666 				mdb_printf("-\n");
1667 		}
1668 	}
1669 
1670 	if (zpa->zpa_current_depth >= zpa->zpa_max_depth)
1671 		return (WALK_NEXT);
1672 
1673 	if (zpa->zpa_type == ZIO_WALK_PARENT)
1674 		laddr = addr + OFFSETOF(zio_t, io_parent_list);
1675 	else
1676 		laddr = addr + OFFSETOF(zio_t, io_child_list);
1677 
1678 	zpa->zpa_current_depth++;
1679 	if (mdb_pwalk("list", zio_child_cb, zpa, laddr) != 0) {
1680 		mdb_warn("failed to walk zio_t children at %p\n", laddr);
1681 		return (WALK_ERR);
1682 	}
1683 	zpa->zpa_current_depth--;
1684 
1685 	return (WALK_NEXT);
1686 }
1687 
1688 /* ARGSUSED */
1689 static int
1690 zio_child_cb(uintptr_t addr, const void *unknown, void *arg)
1691 {
1692 	zio_link_t zl;
1693 	zio_t zio;
1694 	uintptr_t ziop;
1695 	zio_print_args_t *zpa = arg;
1696 
1697 	if (mdb_vread(&zl, sizeof (zl), addr) == -1) {
1698 		mdb_warn("failed to read zio_link_t at %p", addr);
1699 		return (WALK_ERR);
1700 	}
1701 
1702 	if (zpa->zpa_type == ZIO_WALK_PARENT)
1703 		ziop = (uintptr_t)zl.zl_parent;
1704 	else
1705 		ziop = (uintptr_t)zl.zl_child;
1706 
1707 	if (mdb_vread(&zio, sizeof (zio_t), ziop) == -1) {
1708 		mdb_warn("failed to read zio_t at %p", ziop);
1709 		return (WALK_ERR);
1710 	}
1711 
1712 	return (zio_print_cb(ziop, &zio, arg));
1713 }
1714 
1715 /* ARGSUSED */
1716 static int
1717 zio_print(uintptr_t addr, uint_t flags, int argc, const mdb_arg_t *argv)
1718 {
1719 	zio_t zio;
1720 	zio_print_args_t zpa = { 0 };
1721 
1722 	if (!(flags & DCMD_ADDRSPEC))
1723 		return (DCMD_USAGE);
1724 
1725 	if (mdb_getopts(argc, argv,
1726 	    'r', MDB_OPT_SETBITS, INT_MAX, &zpa.zpa_max_depth,
1727 	    'c', MDB_OPT_SETBITS, ZIO_WALK_CHILD, &zpa.zpa_type,
1728 	    'p', MDB_OPT_SETBITS, ZIO_WALK_PARENT, &zpa.zpa_type,
1729 	    NULL) != argc)
1730 		return (DCMD_USAGE);
1731 
1732 	zpa.zpa_flags = flags;
1733 	if (zpa.zpa_max_depth != 0) {
1734 		if (zpa.zpa_type == ZIO_WALK_SELF)
1735 			zpa.zpa_type = ZIO_WALK_CHILD;
1736 	} else if (zpa.zpa_type != ZIO_WALK_SELF) {
1737 		zpa.zpa_min_depth = 1;
1738 		zpa.zpa_max_depth = 1;
1739 	}
1740 
1741 	if (mdb_vread(&zio, sizeof (zio_t), addr) == -1) {
1742 		mdb_warn("failed to read zio_t at %p", addr);
1743 		return (DCMD_ERR);
1744 	}
1745 
1746 	if (!(flags & DCMD_PIPE_OUT) && DCMD_HDRSPEC(flags))
1747 		mdb_printf("%<u>%-*s %-5s %-16s %-?s%</u>\n", ZIO_MAXWIDTH,
1748 		    "ADDRESS", "TYPE", "STAGE", "WAITER");
1749 
1750 	if (zio_print_cb(addr, &zio, &zpa) != WALK_NEXT)
1751 		return (DCMD_ERR);
1752 
1753 	return (DCMD_OK);
1754 }
1755 
1756 /*
1757  * [addr]::zio_state
1758  *
1759  * Print a summary of all zio_t structures on the system, or for a particular
1760  * pool.  This is equivalent to '::walk zio_root | ::zio'.
1761  */
1762 /*ARGSUSED*/
1763 static int
1764 zio_state(uintptr_t addr, uint_t flags, int argc, const mdb_arg_t *argv)
1765 {
1766 	/*
1767 	 * MDB will remember the last address of the pipeline, so if we don't
1768 	 * zero this we'll end up trying to walk zio structures for a
1769 	 * non-existent spa_t.
1770 	 */
1771 	if (!(flags & DCMD_ADDRSPEC))
1772 		addr = 0;
1773 
1774 	return (mdb_pwalk_dcmd("zio_root", "zio", argc, argv, addr));
1775 }
1776 
1777 typedef struct txg_list_walk_data {
1778 	uintptr_t lw_head[TXG_SIZE];
1779 	int	lw_txgoff;
1780 	int	lw_maxoff;
1781 	size_t	lw_offset;
1782 	void	*lw_obj;
1783 } txg_list_walk_data_t;
1784 
1785 static int
1786 txg_list_walk_init_common(mdb_walk_state_t *wsp, int txg, int maxoff)
1787 {
1788 	txg_list_walk_data_t *lwd;
1789 	txg_list_t list;
1790 	int i;
1791 
1792 	lwd = mdb_alloc(sizeof (txg_list_walk_data_t), UM_SLEEP | UM_GC);
1793 	if (mdb_vread(&list, sizeof (txg_list_t), wsp->walk_addr) == -1) {
1794 		mdb_warn("failed to read txg_list_t at %#lx", wsp->walk_addr);
1795 		return (WALK_ERR);
1796 	}
1797 
1798 	for (i = 0; i < TXG_SIZE; i++)
1799 		lwd->lw_head[i] = (uintptr_t)list.tl_head[i];
1800 	lwd->lw_offset = list.tl_offset;
1801 	lwd->lw_obj = mdb_alloc(lwd->lw_offset + sizeof (txg_node_t),
1802 	    UM_SLEEP | UM_GC);
1803 	lwd->lw_txgoff = txg;
1804 	lwd->lw_maxoff = maxoff;
1805 
1806 	wsp->walk_addr = lwd->lw_head[lwd->lw_txgoff];
1807 	wsp->walk_data = lwd;
1808 
1809 	return (WALK_NEXT);
1810 }
1811 
1812 static int
1813 txg_list_walk_init(mdb_walk_state_t *wsp)
1814 {
1815 	return (txg_list_walk_init_common(wsp, 0, TXG_SIZE-1));
1816 }
1817 
1818 static int
1819 txg_list0_walk_init(mdb_walk_state_t *wsp)
1820 {
1821 	return (txg_list_walk_init_common(wsp, 0, 0));
1822 }
1823 
1824 static int
1825 txg_list1_walk_init(mdb_walk_state_t *wsp)
1826 {
1827 	return (txg_list_walk_init_common(wsp, 1, 1));
1828 }
1829 
1830 static int
1831 txg_list2_walk_init(mdb_walk_state_t *wsp)
1832 {
1833 	return (txg_list_walk_init_common(wsp, 2, 2));
1834 }
1835 
1836 static int
1837 txg_list3_walk_init(mdb_walk_state_t *wsp)
1838 {
1839 	return (txg_list_walk_init_common(wsp, 3, 3));
1840 }
1841 
1842 static int
1843 txg_list_walk_step(mdb_walk_state_t *wsp)
1844 {
1845 	txg_list_walk_data_t *lwd = wsp->walk_data;
1846 	uintptr_t addr;
1847 	txg_node_t *node;
1848 	int status;
1849 
1850 	while (wsp->walk_addr == NULL && lwd->lw_txgoff < lwd->lw_maxoff) {
1851 		lwd->lw_txgoff++;
1852 		wsp->walk_addr = lwd->lw_head[lwd->lw_txgoff];
1853 	}
1854 
1855 	if (wsp->walk_addr == NULL)
1856 		return (WALK_DONE);
1857 
1858 	addr = wsp->walk_addr - lwd->lw_offset;
1859 
1860 	if (mdb_vread(lwd->lw_obj,
1861 	    lwd->lw_offset + sizeof (txg_node_t), addr) == -1) {
1862 		mdb_warn("failed to read list element at %#lx", addr);
1863 		return (WALK_ERR);
1864 	}
1865 
1866 	status = wsp->walk_callback(addr, lwd->lw_obj, wsp->walk_cbdata);
1867 	node = (txg_node_t *)((uintptr_t)lwd->lw_obj + lwd->lw_offset);
1868 	wsp->walk_addr = (uintptr_t)node->tn_next[lwd->lw_txgoff];
1869 
1870 	return (status);
1871 }
1872 
1873 /*
1874  * ::walk spa
1875  *
1876  * Walk all named spa_t structures in the namespace.  This is nothing more than
1877  * a layered avl walk.
1878  */
1879 static int
1880 spa_walk_init(mdb_walk_state_t *wsp)
1881 {
1882 	GElf_Sym sym;
1883 
1884 	if (wsp->walk_addr != NULL) {
1885 		mdb_warn("spa walk only supports global walks\n");
1886 		return (WALK_ERR);
1887 	}
1888 
1889 	if (mdb_lookup_by_obj(ZFS_OBJ_NAME, "spa_namespace_avl", &sym) == -1) {
1890 		mdb_warn("failed to find symbol 'spa_namespace_avl'");
1891 		return (WALK_ERR);
1892 	}
1893 
1894 	wsp->walk_addr = (uintptr_t)sym.st_value;
1895 
1896 	if (mdb_layered_walk("avl", wsp) == -1) {
1897 		mdb_warn("failed to walk 'avl'\n");
1898 		return (WALK_ERR);
1899 	}
1900 
1901 	return (WALK_NEXT);
1902 }
1903 
1904 static int
1905 spa_walk_step(mdb_walk_state_t *wsp)
1906 {
1907 	spa_t	spa;
1908 
1909 	if (mdb_vread(&spa, sizeof (spa), wsp->walk_addr) == -1) {
1910 		mdb_warn("failed to read spa_t at %p", wsp->walk_addr);
1911 		return (WALK_ERR);
1912 	}
1913 
1914 	return (wsp->walk_callback(wsp->walk_addr, &spa, wsp->walk_cbdata));
1915 }
1916 
1917 /*
1918  * [addr]::walk zio
1919  *
1920  * Walk all active zio_t structures on the system.  This is simply a layered
1921  * walk on top of ::walk zio_cache, with the optional ability to limit the
1922  * structures to a particular pool.
1923  */
1924 static int
1925 zio_walk_init(mdb_walk_state_t *wsp)
1926 {
1927 	wsp->walk_data = (void *)wsp->walk_addr;
1928 
1929 	if (mdb_layered_walk("zio_cache", wsp) == -1) {
1930 		mdb_warn("failed to walk 'zio_cache'\n");
1931 		return (WALK_ERR);
1932 	}
1933 
1934 	return (WALK_NEXT);
1935 }
1936 
1937 static int
1938 zio_walk_step(mdb_walk_state_t *wsp)
1939 {
1940 	zio_t zio;
1941 
1942 	if (mdb_vread(&zio, sizeof (zio), wsp->walk_addr) == -1) {
1943 		mdb_warn("failed to read zio_t at %p", wsp->walk_addr);
1944 		return (WALK_ERR);
1945 	}
1946 
1947 	if (wsp->walk_data != NULL && wsp->walk_data != zio.io_spa)
1948 		return (WALK_NEXT);
1949 
1950 	return (wsp->walk_callback(wsp->walk_addr, &zio, wsp->walk_cbdata));
1951 }
1952 
1953 /*
1954  * [addr]::walk zio_root
1955  *
1956  * Walk only root zio_t structures, optionally for a particular spa_t.
1957  */
1958 static int
1959 zio_walk_root_step(mdb_walk_state_t *wsp)
1960 {
1961 	zio_t zio;
1962 
1963 	if (mdb_vread(&zio, sizeof (zio), wsp->walk_addr) == -1) {
1964 		mdb_warn("failed to read zio_t at %p", wsp->walk_addr);
1965 		return (WALK_ERR);
1966 	}
1967 
1968 	if (wsp->walk_data != NULL && wsp->walk_data != zio.io_spa)
1969 		return (WALK_NEXT);
1970 
1971 	/* If the parent list is not empty, ignore */
1972 	if (zio.io_parent_list.list_head.list_next !=
1973 	    &((zio_t *)wsp->walk_addr)->io_parent_list.list_head)
1974 		return (WALK_NEXT);
1975 
1976 	return (wsp->walk_callback(wsp->walk_addr, &zio, wsp->walk_cbdata));
1977 }
1978 
1979 #define	NICENUM_BUFLEN 6
1980 
1981 static int
1982 snprintfrac(char *buf, int len,
1983     uint64_t numerator, uint64_t denom, int frac_digits)
1984 {
1985 	int mul = 1;
1986 	int whole, frac, i;
1987 
1988 	for (i = frac_digits; i; i--)
1989 		mul *= 10;
1990 	whole = numerator / denom;
1991 	frac = mul * numerator / denom - mul * whole;
1992 	return (mdb_snprintf(buf, len, "%u.%0*u", whole, frac_digits, frac));
1993 }
1994 
1995 static void
1996 mdb_nicenum(uint64_t num, char *buf)
1997 {
1998 	uint64_t n = num;
1999 	int index = 0;
2000 	char *u;
2001 
2002 	while (n >= 1024) {
2003 		n = (n + (1024 / 2)) / 1024; /* Round up or down */
2004 		index++;
2005 	}
2006 
2007 	u = &" \0K\0M\0G\0T\0P\0E\0"[index*2];
2008 
2009 	if (index == 0) {
2010 		(void) mdb_snprintf(buf, NICENUM_BUFLEN, "%llu",
2011 		    (u_longlong_t)n);
2012 	} else if (n < 10 && (num & (num - 1)) != 0) {
2013 		(void) snprintfrac(buf, NICENUM_BUFLEN,
2014 		    num, 1ULL << 10 * index, 2);
2015 		strcat(buf, u);
2016 	} else if (n < 100 && (num & (num - 1)) != 0) {
2017 		(void) snprintfrac(buf, NICENUM_BUFLEN,
2018 		    num, 1ULL << 10 * index, 1);
2019 		strcat(buf, u);
2020 	} else {
2021 		(void) mdb_snprintf(buf, NICENUM_BUFLEN, "%llu%s",
2022 		    (u_longlong_t)n, u);
2023 	}
2024 }
2025 
2026 /*
2027  * ::zfs_blkstats
2028  *
2029  * 	-v	print verbose per-level information
2030  *
2031  */
2032 static int
2033 zfs_blkstats(uintptr_t addr, uint_t flags, int argc, const mdb_arg_t *argv)
2034 {
2035 	boolean_t verbose = B_FALSE;
2036 	zfs_all_blkstats_t stats;
2037 	dmu_object_type_t t;
2038 	zfs_blkstat_t *tzb;
2039 	uint64_t ditto;
2040 	dmu_object_type_info_t dmu_ot[DMU_OT_NUMTYPES + 10];
2041 	/* +10 in case it grew */
2042 
2043 	if (mdb_readvar(&dmu_ot, "dmu_ot") == -1) {
2044 		mdb_warn("failed to read 'dmu_ot'");
2045 		return (DCMD_ERR);
2046 	}
2047 
2048 	if (mdb_getopts(argc, argv,
2049 	    'v', MDB_OPT_SETBITS, TRUE, &verbose,
2050 	    NULL) != argc)
2051 		return (DCMD_USAGE);
2052 
2053 	if (!(flags & DCMD_ADDRSPEC))
2054 		return (DCMD_USAGE);
2055 
2056 	if (GETMEMB(addr, struct spa, spa_dsl_pool, addr) ||
2057 	    GETMEMB(addr, struct dsl_pool, dp_blkstats, addr) ||
2058 	    mdb_vread(&stats, sizeof (zfs_all_blkstats_t), addr) == -1) {
2059 		mdb_warn("failed to read data at %p;", addr);
2060 		mdb_printf("maybe no stats? run \"zpool scrub\" first.");
2061 		return (DCMD_ERR);
2062 	}
2063 
2064 	tzb = &stats.zab_type[DN_MAX_LEVELS][DMU_OT_NUMTYPES];
2065 	if (tzb->zb_gangs != 0) {
2066 		mdb_printf("Ganged blocks: %llu\n",
2067 		    (longlong_t)tzb->zb_gangs);
2068 	}
2069 
2070 	ditto = tzb->zb_ditto_2_of_2_samevdev + tzb->zb_ditto_2_of_3_samevdev +
2071 	    tzb->zb_ditto_3_of_3_samevdev;
2072 	if (ditto != 0) {
2073 		mdb_printf("Dittoed blocks on same vdev: %llu\n",
2074 		    (longlong_t)ditto);
2075 	}
2076 
2077 	mdb_printf("\nBlocks\tLSIZE\tPSIZE\tASIZE"
2078 	    "\t  avg\t comp\t%%Total\tType\n");
2079 
2080 	for (t = 0; t <= DMU_OT_NUMTYPES; t++) {
2081 		char csize[NICENUM_BUFLEN], lsize[NICENUM_BUFLEN];
2082 		char psize[NICENUM_BUFLEN], asize[NICENUM_BUFLEN];
2083 		char avg[NICENUM_BUFLEN];
2084 		char comp[NICENUM_BUFLEN], pct[NICENUM_BUFLEN];
2085 		char typename[64];
2086 		int l;
2087 
2088 
2089 		if (t == DMU_OT_DEFERRED)
2090 			strcpy(typename, "deferred free");
2091 		else if (t == DMU_OT_TOTAL)
2092 			strcpy(typename, "Total");
2093 		else if (mdb_readstr(typename, sizeof (typename),
2094 		    (uintptr_t)dmu_ot[t].ot_name) == -1) {
2095 			mdb_warn("failed to read type name");
2096 			return (DCMD_ERR);
2097 		}
2098 
2099 		if (stats.zab_type[DN_MAX_LEVELS][t].zb_asize == 0)
2100 			continue;
2101 
2102 		for (l = -1; l < DN_MAX_LEVELS; l++) {
2103 			int level = (l == -1 ? DN_MAX_LEVELS : l);
2104 			zfs_blkstat_t *zb = &stats.zab_type[level][t];
2105 
2106 			if (zb->zb_asize == 0)
2107 				continue;
2108 
2109 			/*
2110 			 * Don't print each level unless requested.
2111 			 */
2112 			if (!verbose && level != DN_MAX_LEVELS)
2113 				continue;
2114 
2115 			/*
2116 			 * If all the space is level 0, don't print the
2117 			 * level 0 separately.
2118 			 */
2119 			if (level == 0 && zb->zb_asize ==
2120 			    stats.zab_type[DN_MAX_LEVELS][t].zb_asize)
2121 				continue;
2122 
2123 			mdb_nicenum(zb->zb_count, csize);
2124 			mdb_nicenum(zb->zb_lsize, lsize);
2125 			mdb_nicenum(zb->zb_psize, psize);
2126 			mdb_nicenum(zb->zb_asize, asize);
2127 			mdb_nicenum(zb->zb_asize / zb->zb_count, avg);
2128 			(void) snprintfrac(comp, NICENUM_BUFLEN,
2129 			    zb->zb_lsize, zb->zb_psize, 2);
2130 			(void) snprintfrac(pct, NICENUM_BUFLEN,
2131 			    100 * zb->zb_asize, tzb->zb_asize, 2);
2132 
2133 			mdb_printf("%6s\t%5s\t%5s\t%5s\t%5s"
2134 			    "\t%5s\t%6s\t",
2135 			    csize, lsize, psize, asize, avg, comp, pct);
2136 
2137 			if (level == DN_MAX_LEVELS)
2138 				mdb_printf("%s\n", typename);
2139 			else
2140 				mdb_printf("  L%d %s\n",
2141 				    level, typename);
2142 		}
2143 	}
2144 
2145 	return (DCMD_OK);
2146 }
2147 
2148 /*
2149  * MDB module linkage information:
2150  *
2151  * We declare a list of structures describing our dcmds, and a function
2152  * named _mdb_init to return a pointer to our module information.
2153  */
2154 
2155 static const mdb_dcmd_t dcmds[] = {
2156 	{ "arc", "[-bkmg]", "print ARC variables", arc_print },
2157 	{ "blkptr", ":", "print blkptr_t", blkptr },
2158 	{ "dbuf", ":", "print dmu_buf_impl_t", dbuf },
2159 	{ "dbuf_stats", ":", "dbuf stats", dbuf_stats },
2160 	{ "dbufs",
2161 	    "\t[-O objset_t*] [-n objset_name | \"mos\"] "
2162 	    "[-o object | \"mdn\"] \n"
2163 	    "\t[-l level] [-b blkid | \"bonus\"]",
2164 	    "find dmu_buf_impl_t's that match specified criteria", dbufs },
2165 	{ "abuf_find", "dva_word[0] dva_word[1]",
2166 	    "find arc_buf_hdr_t of a specified DVA",
2167 	    abuf_find },
2168 	{ "spa", "?[-cv]", "spa_t summary", spa_print },
2169 	{ "spa_config", ":", "print spa_t configuration", spa_print_config },
2170 	{ "spa_verify", ":", "verify spa_t consistency", spa_verify },
2171 	{ "spa_space", ":[-b]", "print spa_t on-disk space usage", spa_space },
2172 	{ "spa_vdevs", ":", "given a spa_t, print vdev summary", spa_vdevs },
2173 	{ "vdev", ":[-re]\n"
2174 	    "\t-r display recursively\n"
2175 	    "\t-e print statistics",
2176 	    "vdev_t summary", vdev_print },
2177 	{ "zio", ":[cpr]\n"
2178 	    "\t-c display children\n"
2179 	    "\t-p display parents\n"
2180 	    "\t-r display recursively",
2181 	    "zio_t summary", zio_print },
2182 	{ "zio_state", "?", "print out all zio_t structures on system or "
2183 	    "for a particular pool", zio_state },
2184 	{ "zio_pipeline", ":", "decode a zio pipeline", zio_pipeline },
2185 	{ "zfs_blkstats", ":[-v]",
2186 	    "given a spa_t, print block type stats from last scrub",
2187 	    zfs_blkstats },
2188 	{ "zfs_params", "", "print zfs tunable parameters", zfs_params },
2189 	{ NULL }
2190 };
2191 
2192 static const mdb_walker_t walkers[] = {
2193 	/*
2194 	 * In userland, there is no generic provider of list_t walkers, so we
2195 	 * need to add it.
2196 	 */
2197 #ifndef _KERNEL
2198 	{ LIST_WALK_NAME, LIST_WALK_DESC,
2199 		list_walk_init, list_walk_step, list_walk_fini },
2200 #endif
2201 	{ "zms_freelist", "walk ZFS metaslab freelist",
2202 		freelist_walk_init, freelist_walk_step, NULL },
2203 	{ "txg_list", "given any txg_list_t *, walk all entries in all txgs",
2204 		txg_list_walk_init, txg_list_walk_step, NULL },
2205 	{ "txg_list0", "given any txg_list_t *, walk all entries in txg 0",
2206 		txg_list0_walk_init, txg_list_walk_step, NULL },
2207 	{ "txg_list1", "given any txg_list_t *, walk all entries in txg 1",
2208 		txg_list1_walk_init, txg_list_walk_step, NULL },
2209 	{ "txg_list2", "given any txg_list_t *, walk all entries in txg 2",
2210 		txg_list2_walk_init, txg_list_walk_step, NULL },
2211 	{ "txg_list3", "given any txg_list_t *, walk all entries in txg 3",
2212 		txg_list3_walk_init, txg_list_walk_step, NULL },
2213 	{ "zio", "walk all zio structures, optionally for a particular spa_t",
2214 		zio_walk_init, zio_walk_step, NULL },
2215 	{ "zio_root", "walk all root zio_t structures, optionally for a "
2216 	    "particular spa_t",
2217 		zio_walk_init, zio_walk_root_step, NULL },
2218 	{ "spa", "walk all spa_t entries in the namespace",
2219 		spa_walk_init, spa_walk_step, NULL },
2220 	{ "metaslab", "given a spa_t *, walk all metaslab_t structures",
2221 		metaslab_walk_init, metaslab_walk_step, NULL },
2222 	{ NULL }
2223 };
2224 
2225 static const mdb_modinfo_t modinfo = {
2226 	MDB_API_VERSION, dcmds, walkers
2227 };
2228 
2229 const mdb_modinfo_t *
2230 _mdb_init(void)
2231 {
2232 	return (&modinfo);
2233 }
2234