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