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