xref: /titanic_41/usr/src/cmd/mdb/common/modules/zfs/zfs.c (revision 07d06da50d310a325b457d6330165aebab1e0064)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License (the "License").
6  * You may not use this file except in compliance with the License.
7  *
8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9  * or http://www.opensolaris.org/os/licensing.
10  * See the License for the specific language governing permissions
11  * and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL HEADER in each
14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15  * If applicable, add the following below this CDDL HEADER, with the
16  * fields enclosed by brackets "[]" replaced with your own identifying
17  * information: Portions Copyright [yyyy] [name of copyright owner]
18  *
19  * CDDL HEADER END
20  */
21 /*
22  * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
23  * Use is subject to license terms.
24  */
25 
26 #include <mdb/mdb_ctf.h>
27 #include <sys/zfs_context.h>
28 #include <sys/mdb_modapi.h>
29 #include <sys/dbuf.h>
30 #include <sys/dmu_objset.h>
31 #include <sys/dsl_dir.h>
32 #include <sys/dsl_pool.h>
33 #include <sys/metaslab_impl.h>
34 #include <sys/space_map.h>
35 #include <sys/list.h>
36 #include <sys/spa_impl.h>
37 #include <sys/vdev_impl.h>
38 #include <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 		default:
1044 			aux = "UNKNOWN";
1045 			break;
1046 		}
1047 
1048 		mdb_printf("%-9s %-12s %*s%s\n", state, aux, depth, "", desc);
1049 
1050 		if (stats) {
1051 			vdev_stat_t *vs = &vdev.vdev_stat;
1052 			int i;
1053 
1054 			mdb_inc_indent(4);
1055 			mdb_printf("\n");
1056 			mdb_printf("%<u>       %12s %12s %12s %12s "
1057 			    "%12s%</u>\n", "READ", "WRITE", "FREE", "CLAIM",
1058 			    "IOCTL");
1059 			mdb_printf("OPS     ");
1060 			for (i = 1; i < ZIO_TYPES; i++)
1061 				mdb_printf("%11#llx%s", vs->vs_ops[i],
1062 				    i == ZIO_TYPES - 1 ? "" : "  ");
1063 			mdb_printf("\n");
1064 			mdb_printf("BYTES   ");
1065 			for (i = 1; i < ZIO_TYPES; i++)
1066 				mdb_printf("%11#llx%s", vs->vs_bytes[i],
1067 				    i == ZIO_TYPES - 1 ? "" : "  ");
1068 
1069 
1070 			mdb_printf("\n");
1071 			mdb_printf("EREAD    %10#llx\n", vs->vs_read_errors);
1072 			mdb_printf("EWRITE   %10#llx\n", vs->vs_write_errors);
1073 			mdb_printf("ECKSUM   %10#llx\n",
1074 			    vs->vs_checksum_errors);
1075 			mdb_dec_indent(4);
1076 		}
1077 
1078 		if (stats)
1079 			mdb_printf("\n");
1080 	}
1081 
1082 	children = vdev.vdev_children;
1083 
1084 	if (children == 0 || !recursive)
1085 		return (DCMD_OK);
1086 
1087 	child = mdb_alloc(children * sizeof (void *), UM_SLEEP | UM_GC);
1088 	if (mdb_vread(child, children * sizeof (void *),
1089 	    (uintptr_t)vdev.vdev_child) == -1) {
1090 		mdb_warn("failed to read vdev children at %p", vdev.vdev_child);
1091 		return (DCMD_ERR);
1092 	}
1093 
1094 	for (c = 0; c < children; c++) {
1095 		if (do_print_vdev(child[c], flags, depth + 2, stats,
1096 		    recursive))
1097 			return (DCMD_ERR);
1098 	}
1099 
1100 	return (DCMD_OK);
1101 }
1102 
1103 static int
1104 vdev_print(uintptr_t addr, uint_t flags, int argc, const mdb_arg_t *argv)
1105 {
1106 	int recursive = FALSE;
1107 	int stats = FALSE;
1108 	uint64_t depth = 0;
1109 
1110 	if (mdb_getopts(argc, argv,
1111 	    'r', MDB_OPT_SETBITS, TRUE, &recursive,
1112 	    'e', MDB_OPT_SETBITS, TRUE, &stats,
1113 	    'd', MDB_OPT_UINT64, &depth,
1114 	    NULL) != argc)
1115 		return (DCMD_USAGE);
1116 
1117 	if (!(flags & DCMD_ADDRSPEC)) {
1118 		mdb_warn("no vdev_t address given\n");
1119 		return (DCMD_ERR);
1120 	}
1121 
1122 	return (do_print_vdev(addr, flags, (int)depth, stats, recursive));
1123 }
1124 
1125 typedef struct metaslab_walk_data {
1126 	uint64_t mw_numvdevs;
1127 	uintptr_t *mw_vdevs;
1128 	int mw_curvdev;
1129 	uint64_t mw_nummss;
1130 	uintptr_t *mw_mss;
1131 	int mw_curms;
1132 } metaslab_walk_data_t;
1133 
1134 static int
1135 metaslab_walk_step(mdb_walk_state_t *wsp)
1136 {
1137 	metaslab_walk_data_t *mw = wsp->walk_data;
1138 	metaslab_t ms;
1139 	uintptr_t msp;
1140 
1141 	if (mw->mw_curvdev >= mw->mw_numvdevs)
1142 		return (WALK_DONE);
1143 
1144 	if (mw->mw_mss == NULL) {
1145 		uintptr_t mssp;
1146 		uintptr_t vdevp;
1147 
1148 		ASSERT(mw->mw_curms == 0);
1149 		ASSERT(mw->mw_nummss == 0);
1150 
1151 		vdevp = mw->mw_vdevs[mw->mw_curvdev];
1152 		if (GETMEMB(vdevp, struct vdev, vdev_ms, mssp) ||
1153 		    GETMEMB(vdevp, struct vdev, vdev_ms_count, mw->mw_nummss)) {
1154 			return (WALK_ERR);
1155 		}
1156 
1157 		mw->mw_mss = mdb_alloc(mw->mw_nummss * sizeof (void*),
1158 		    UM_SLEEP | UM_GC);
1159 		if (mdb_vread(mw->mw_mss, mw->mw_nummss * sizeof (void*),
1160 		    mssp) == -1) {
1161 			mdb_warn("failed to read vdev_ms at %p", mssp);
1162 			return (WALK_ERR);
1163 		}
1164 	}
1165 
1166 	if (mw->mw_curms >= mw->mw_nummss) {
1167 		mw->mw_mss = NULL;
1168 		mw->mw_curms = 0;
1169 		mw->mw_nummss = 0;
1170 		mw->mw_curvdev++;
1171 		return (WALK_NEXT);
1172 	}
1173 
1174 	msp = mw->mw_mss[mw->mw_curms];
1175 	if (mdb_vread(&ms, sizeof (metaslab_t), msp) == -1) {
1176 		mdb_warn("failed to read metaslab_t at %p", msp);
1177 		return (WALK_ERR);
1178 	}
1179 
1180 	mw->mw_curms++;
1181 
1182 	return (wsp->walk_callback(msp, &ms, wsp->walk_cbdata));
1183 }
1184 
1185 /* ARGSUSED */
1186 static int
1187 metaslab_walk_init(mdb_walk_state_t *wsp)
1188 {
1189 	metaslab_walk_data_t *mw;
1190 	uintptr_t root_vdevp;
1191 	uintptr_t childp;
1192 
1193 	if (wsp->walk_addr == NULL) {
1194 		mdb_warn("must supply address of spa_t\n");
1195 		return (WALK_ERR);
1196 	}
1197 
1198 	mw = mdb_zalloc(sizeof (metaslab_walk_data_t), UM_SLEEP | UM_GC);
1199 
1200 	if (GETMEMB(wsp->walk_addr, struct spa, spa_root_vdev, root_vdevp) ||
1201 	    GETMEMB(root_vdevp, struct vdev, vdev_children, mw->mw_numvdevs) ||
1202 	    GETMEMB(root_vdevp, struct vdev, vdev_child, childp)) {
1203 		return (DCMD_ERR);
1204 	}
1205 
1206 	mw->mw_vdevs = mdb_alloc(mw->mw_numvdevs * sizeof (void *),
1207 	    UM_SLEEP | UM_GC);
1208 	if (mdb_vread(mw->mw_vdevs, mw->mw_numvdevs * sizeof (void *),
1209 	    childp) == -1) {
1210 		mdb_warn("failed to read root vdev children at %p", childp);
1211 		return (DCMD_ERR);
1212 	}
1213 
1214 	wsp->walk_data = mw;
1215 
1216 	return (WALK_NEXT);
1217 }
1218 
1219 typedef struct mdb_spa {
1220 	uintptr_t spa_dsl_pool;
1221 	uintptr_t spa_root_vdev;
1222 } mdb_spa_t;
1223 
1224 typedef struct mdb_dsl_dir {
1225 	uintptr_t dd_phys;
1226 	int64_t dd_space_towrite[TXG_SIZE];
1227 } mdb_dsl_dir_t;
1228 
1229 typedef struct mdb_dsl_dir_phys {
1230 	uint64_t dd_used_bytes;
1231 	uint64_t dd_compressed_bytes;
1232 	uint64_t dd_uncompressed_bytes;
1233 } mdb_dsl_dir_phys_t;
1234 
1235 typedef struct mdb_vdev {
1236 	uintptr_t vdev_parent;
1237 	uintptr_t vdev_ms;
1238 	uint64_t vdev_ms_count;
1239 	vdev_stat_t vdev_stat;
1240 } mdb_vdev_t;
1241 
1242 typedef struct mdb_metaslab {
1243 	space_map_t ms_allocmap[TXG_SIZE];
1244 	space_map_t ms_freemap[TXG_SIZE];
1245 	space_map_t ms_map;
1246 	space_map_obj_t ms_smo;
1247 	space_map_obj_t ms_smo_syncing;
1248 } mdb_metaslab_t;
1249 
1250 typedef struct space_data {
1251 	uint64_t ms_allocmap[TXG_SIZE];
1252 	uint64_t ms_freemap[TXG_SIZE];
1253 	uint64_t ms_map;
1254 	uint64_t avail;
1255 	uint64_t nowavail;
1256 } space_data_t;
1257 
1258 /* ARGSUSED */
1259 static int
1260 space_cb(uintptr_t addr, const void *unknown, void *arg)
1261 {
1262 	space_data_t *sd = arg;
1263 	mdb_metaslab_t ms;
1264 
1265 	if (GETMEMB(addr, struct metaslab, ms_allocmap, ms.ms_allocmap) ||
1266 	    GETMEMB(addr, struct metaslab, ms_freemap, ms.ms_freemap) ||
1267 	    GETMEMB(addr, struct metaslab, ms_map, ms.ms_map) ||
1268 	    GETMEMB(addr, struct metaslab, ms_smo, ms.ms_smo) ||
1269 	    GETMEMB(addr, struct metaslab, ms_smo_syncing, ms.ms_smo_syncing)) {
1270 		return (WALK_ERR);
1271 	}
1272 
1273 	sd->ms_allocmap[0] += ms.ms_allocmap[0].sm_space;
1274 	sd->ms_allocmap[1] += ms.ms_allocmap[1].sm_space;
1275 	sd->ms_allocmap[2] += ms.ms_allocmap[2].sm_space;
1276 	sd->ms_allocmap[3] += ms.ms_allocmap[3].sm_space;
1277 	sd->ms_freemap[0] += ms.ms_freemap[0].sm_space;
1278 	sd->ms_freemap[1] += ms.ms_freemap[1].sm_space;
1279 	sd->ms_freemap[2] += ms.ms_freemap[2].sm_space;
1280 	sd->ms_freemap[3] += ms.ms_freemap[3].sm_space;
1281 	sd->ms_map += ms.ms_map.sm_space;
1282 	sd->avail += ms.ms_map.sm_size - ms.ms_smo.smo_alloc;
1283 	sd->nowavail += ms.ms_map.sm_size - ms.ms_smo_syncing.smo_alloc;
1284 
1285 	return (WALK_NEXT);
1286 }
1287 
1288 /*
1289  * ::spa_space [-b]
1290  *
1291  * Given a spa_t, print out it's on-disk space usage and in-core
1292  * estimates of future usage.  If -b is given, print space in bytes.
1293  * Otherwise print in megabytes.
1294  */
1295 /* ARGSUSED */
1296 static int
1297 spa_space(uintptr_t addr, uint_t flags, int argc, const mdb_arg_t *argv)
1298 {
1299 	mdb_spa_t spa;
1300 	uintptr_t dp_root_dir;
1301 	mdb_dsl_dir_t dd;
1302 	mdb_dsl_dir_phys_t dsp;
1303 	uint64_t children;
1304 	uintptr_t childaddr;
1305 	space_data_t sd;
1306 	int shift = 20;
1307 	char *suffix = "M";
1308 	int bits = FALSE;
1309 
1310 	if (mdb_getopts(argc, argv, 'b', MDB_OPT_SETBITS, TRUE, &bits, NULL) !=
1311 	    argc)
1312 		return (DCMD_USAGE);
1313 	if (!(flags & DCMD_ADDRSPEC))
1314 		return (DCMD_USAGE);
1315 
1316 	if (bits) {
1317 		shift = 0;
1318 		suffix = "";
1319 	}
1320 
1321 	if (GETMEMB(addr, struct spa, spa_dsl_pool, spa.spa_dsl_pool) ||
1322 	    GETMEMB(addr, struct spa, spa_root_vdev, spa.spa_root_vdev) ||
1323 	    GETMEMB(spa.spa_root_vdev, struct vdev, vdev_children, children) ||
1324 	    GETMEMB(spa.spa_root_vdev, struct vdev, vdev_child, childaddr) ||
1325 	    GETMEMB(spa.spa_dsl_pool, struct dsl_pool,
1326 	    dp_root_dir, dp_root_dir) ||
1327 	    GETMEMB(dp_root_dir, struct dsl_dir, dd_phys, dd.dd_phys) ||
1328 	    GETMEMB(dp_root_dir, struct dsl_dir,
1329 	    dd_space_towrite, dd.dd_space_towrite) ||
1330 	    GETMEMB(dd.dd_phys, struct dsl_dir_phys,
1331 	    dd_used_bytes, dsp.dd_used_bytes) ||
1332 	    GETMEMB(dd.dd_phys, struct dsl_dir_phys,
1333 	    dd_compressed_bytes, dsp.dd_compressed_bytes) ||
1334 	    GETMEMB(dd.dd_phys, struct dsl_dir_phys,
1335 	    dd_uncompressed_bytes, dsp.dd_uncompressed_bytes)) {
1336 		return (DCMD_ERR);
1337 	}
1338 
1339 	mdb_printf("dd_space_towrite = %llu%s %llu%s %llu%s %llu%s\n",
1340 	    dd.dd_space_towrite[0] >> shift, suffix,
1341 	    dd.dd_space_towrite[1] >> shift, suffix,
1342 	    dd.dd_space_towrite[2] >> shift, suffix,
1343 	    dd.dd_space_towrite[3] >> shift, suffix);
1344 
1345 	mdb_printf("dd_phys.dd_used_bytes = %llu%s\n",
1346 	    dsp.dd_used_bytes >> shift, suffix);
1347 	mdb_printf("dd_phys.dd_compressed_bytes = %llu%s\n",
1348 	    dsp.dd_compressed_bytes >> shift, suffix);
1349 	mdb_printf("dd_phys.dd_uncompressed_bytes = %llu%s\n",
1350 	    dsp.dd_uncompressed_bytes >> shift, suffix);
1351 
1352 	bzero(&sd, sizeof (sd));
1353 	if (mdb_pwalk("metaslab", space_cb, &sd, addr) != 0) {
1354 		mdb_warn("can't walk metaslabs");
1355 		return (DCMD_ERR);
1356 	}
1357 
1358 	mdb_printf("ms_allocmap = %llu%s %llu%s %llu%s %llu%s\n",
1359 	    sd.ms_allocmap[0] >> shift, suffix,
1360 	    sd.ms_allocmap[1] >> shift, suffix,
1361 	    sd.ms_allocmap[2] >> shift, suffix,
1362 	    sd.ms_allocmap[3] >> shift, suffix);
1363 	mdb_printf("ms_freemap = %llu%s %llu%s %llu%s %llu%s\n",
1364 	    sd.ms_freemap[0] >> shift, suffix,
1365 	    sd.ms_freemap[1] >> shift, suffix,
1366 	    sd.ms_freemap[2] >> shift, suffix,
1367 	    sd.ms_freemap[3] >> shift, suffix);
1368 	mdb_printf("ms_map = %llu%s\n", sd.ms_map >> shift, suffix);
1369 	mdb_printf("last synced avail = %llu%s\n", sd.avail >> shift, suffix);
1370 	mdb_printf("current syncing avail = %llu%s\n",
1371 	    sd.nowavail >> shift, suffix);
1372 
1373 	return (DCMD_OK);
1374 }
1375 
1376 /*
1377  * ::spa_verify
1378  *
1379  * Given a spa_t, verify that that the pool is self-consistent.
1380  * Currently, it only checks to make sure that the vdev tree exists.
1381  */
1382 /* ARGSUSED */
1383 static int
1384 spa_verify(uintptr_t addr, uint_t flags, int argc, const mdb_arg_t *argv)
1385 {
1386 	spa_t spa;
1387 
1388 	if (argc != 0 || !(flags & DCMD_ADDRSPEC))
1389 		return (DCMD_USAGE);
1390 
1391 	if (mdb_vread(&spa, sizeof (spa), addr) == -1) {
1392 		mdb_warn("failed to read spa_t at %p", addr);
1393 		return (DCMD_ERR);
1394 	}
1395 
1396 	if (spa.spa_root_vdev == NULL) {
1397 		mdb_printf("no vdev tree present\n");
1398 		return (DCMD_OK);
1399 	}
1400 
1401 	return (DCMD_OK);
1402 }
1403 
1404 static int
1405 spa_print_aux(spa_aux_vdev_t *sav, uint_t flags, mdb_arg_t *v,
1406     const char *name)
1407 {
1408 	uintptr_t *aux;
1409 	size_t len;
1410 	int ret, i;
1411 
1412 	/*
1413 	 * Iterate over aux vdevs and print those out as well.  This is a
1414 	 * little annoying because we don't have a root vdev to pass to ::vdev.
1415 	 * Instead, we print a single line and then call it for each child
1416 	 * vdev.
1417 	 */
1418 	if (sav->sav_count != 0) {
1419 		v[1].a_type = MDB_TYPE_STRING;
1420 		v[1].a_un.a_str = "-d";
1421 		v[2].a_type = MDB_TYPE_IMMEDIATE;
1422 		v[2].a_un.a_val = 2;
1423 
1424 		len = sav->sav_count * sizeof (uintptr_t);
1425 		aux = mdb_alloc(len, UM_SLEEP);
1426 		if (mdb_vread(aux, len,
1427 		    (uintptr_t)sav->sav_vdevs) == -1) {
1428 			mdb_free(aux, len);
1429 			mdb_warn("failed to read l2cache vdevs at %p",
1430 			    sav->sav_vdevs);
1431 			return (DCMD_ERR);
1432 		}
1433 
1434 		mdb_printf("%-?s %-9s %-12s %s\n", "-", "-", "-", name);
1435 
1436 		for (i = 0; i < sav->sav_count; i++) {
1437 			ret = mdb_call_dcmd("vdev", aux[i], flags, 3, v);
1438 			if (ret != DCMD_OK) {
1439 				mdb_free(aux, len);
1440 				return (ret);
1441 			}
1442 		}
1443 
1444 		mdb_free(aux, len);
1445 	}
1446 
1447 	return (0);
1448 }
1449 
1450 /*
1451  * ::spa_vdevs
1452  *
1453  * 	-e	Include error stats
1454  *
1455  * Print out a summarized list of vdevs for the given spa_t.
1456  * This is accomplished by invoking "::vdev -re" on the root vdev, as well as
1457  * iterating over the cache devices.
1458  */
1459 /* ARGSUSED */
1460 static int
1461 spa_vdevs(uintptr_t addr, uint_t flags, int argc, const mdb_arg_t *argv)
1462 {
1463 	spa_t spa;
1464 	mdb_arg_t v[3];
1465 	int errors = FALSE;
1466 	int ret;
1467 
1468 	if (mdb_getopts(argc, argv,
1469 	    'e', MDB_OPT_SETBITS, TRUE, &errors,
1470 	    NULL) != argc)
1471 		return (DCMD_USAGE);
1472 
1473 	if (!(flags & DCMD_ADDRSPEC))
1474 		return (DCMD_USAGE);
1475 
1476 	if (mdb_vread(&spa, sizeof (spa), addr) == -1) {
1477 		mdb_warn("failed to read spa_t at %p", addr);
1478 		return (DCMD_ERR);
1479 	}
1480 
1481 	/*
1482 	 * Unitialized spa_t structures can have a NULL root vdev.
1483 	 */
1484 	if (spa.spa_root_vdev == NULL) {
1485 		mdb_printf("no associated vdevs\n");
1486 		return (DCMD_OK);
1487 	}
1488 
1489 	v[0].a_type = MDB_TYPE_STRING;
1490 	v[0].a_un.a_str = errors ? "-re" : "-r";
1491 
1492 	ret = mdb_call_dcmd("vdev", (uintptr_t)spa.spa_root_vdev,
1493 	    flags, 1, v);
1494 	if (ret != DCMD_OK)
1495 		return (ret);
1496 
1497 	if (spa_print_aux(&spa.spa_l2cache, flags, v, "cache") != 0 ||
1498 	    spa_print_aux(&spa.spa_spares, flags, v, "spares") != 0)
1499 		return (DCMD_ERR);
1500 
1501 	return (DCMD_OK);
1502 }
1503 
1504 /*
1505  * ::zio
1506  *
1507  * Print a summary of zio_t and all its children.  This is intended to display a
1508  * zio tree, and hence we only pick the most important pieces of information for
1509  * the main summary.  More detailed information can always be found by doing a
1510  * '::print zio' on the underlying zio_t.  The columns we display are:
1511  *
1512  *	ADDRESS		TYPE	STAGE		WAITER
1513  *
1514  * The 'address' column is indented by one space for each depth level as we
1515  * descend down the tree.
1516  */
1517 
1518 #define	ZIO_MAXINDENT	24
1519 #define	ZIO_MAXWIDTH	(sizeof (uintptr_t) * 2 + ZIO_MAXINDENT)
1520 #define	ZIO_WALK_SELF	0
1521 #define	ZIO_WALK_CHILD	1
1522 #define	ZIO_WALK_PARENT	2
1523 
1524 typedef struct zio_print_args {
1525 	int	zpa_current_depth;
1526 	int	zpa_min_depth;
1527 	int	zpa_max_depth;
1528 	int	zpa_type;
1529 	uint_t	zpa_flags;
1530 } zio_print_args_t;
1531 
1532 static int zio_child_cb(uintptr_t addr, const void *unknown, void *arg);
1533 
1534 static int
1535 zio_print_cb(uintptr_t addr, const void *data, void *priv)
1536 {
1537 	const zio_t *zio = data;
1538 	zio_print_args_t *zpa = priv;
1539 	mdb_ctf_id_t type_enum, stage_enum;
1540 	int indent = zpa->zpa_current_depth;
1541 	const char *type, *stage;
1542 	uintptr_t laddr;
1543 
1544 	if (indent > ZIO_MAXINDENT)
1545 		indent = ZIO_MAXINDENT;
1546 
1547 	if (mdb_ctf_lookup_by_name("enum zio_type", &type_enum) == -1 ||
1548 	    mdb_ctf_lookup_by_name("enum zio_stage", &stage_enum) == -1) {
1549 		mdb_warn("failed to lookup zio enums");
1550 		return (WALK_ERR);
1551 	}
1552 
1553 	if ((type = mdb_ctf_enum_name(type_enum, zio->io_type)) != NULL)
1554 		type += sizeof ("ZIO_TYPE_") - 1;
1555 	else
1556 		type = "?";
1557 
1558 	if ((stage = mdb_ctf_enum_name(stage_enum, zio->io_stage)) != NULL)
1559 		stage += sizeof ("ZIO_STAGE_") - 1;
1560 	else
1561 		stage = "?";
1562 
1563 	if (zpa->zpa_current_depth >= zpa->zpa_min_depth) {
1564 		if (zpa->zpa_flags & DCMD_PIPE_OUT) {
1565 			mdb_printf("%?p\n", addr);
1566 		} else {
1567 			mdb_printf("%*s%-*p %-5s %-16s ", indent, "",
1568 			    ZIO_MAXWIDTH - indent, addr, type, stage);
1569 			if (zio->io_waiter)
1570 				mdb_printf("%?p\n", zio->io_waiter);
1571 			else
1572 				mdb_printf("-\n");
1573 		}
1574 	}
1575 
1576 	if (zpa->zpa_current_depth >= zpa->zpa_max_depth)
1577 		return (WALK_NEXT);
1578 
1579 	if (zpa->zpa_type == ZIO_WALK_PARENT)
1580 		laddr = addr + OFFSETOF(zio_t, io_parent_list);
1581 	else
1582 		laddr = addr + OFFSETOF(zio_t, io_child_list);
1583 
1584 	zpa->zpa_current_depth++;
1585 	if (mdb_pwalk("list", zio_child_cb, zpa, laddr) != 0) {
1586 		mdb_warn("failed to walk zio_t children at %p\n", laddr);
1587 		return (WALK_ERR);
1588 	}
1589 	zpa->zpa_current_depth--;
1590 
1591 	return (WALK_NEXT);
1592 }
1593 
1594 /* ARGSUSED */
1595 static int
1596 zio_child_cb(uintptr_t addr, const void *unknown, void *arg)
1597 {
1598 	zio_link_t zl;
1599 	zio_t zio;
1600 	uintptr_t ziop;
1601 	zio_print_args_t *zpa = arg;
1602 
1603 	if (mdb_vread(&zl, sizeof (zl), addr) == -1) {
1604 		mdb_warn("failed to read zio_link_t at %p", addr);
1605 		return (WALK_ERR);
1606 	}
1607 
1608 	if (zpa->zpa_type == ZIO_WALK_PARENT)
1609 		ziop = (uintptr_t)zl.zl_parent;
1610 	else
1611 		ziop = (uintptr_t)zl.zl_child;
1612 
1613 	if (mdb_vread(&zio, sizeof (zio_t), ziop) == -1) {
1614 		mdb_warn("failed to read zio_t at %p", ziop);
1615 		return (WALK_ERR);
1616 	}
1617 
1618 	return (zio_print_cb(ziop, &zio, arg));
1619 }
1620 
1621 /* ARGSUSED */
1622 static int
1623 zio_print(uintptr_t addr, uint_t flags, int argc, const mdb_arg_t *argv)
1624 {
1625 	zio_t zio;
1626 	zio_print_args_t zpa = { 0 };
1627 
1628 	if (!(flags & DCMD_ADDRSPEC))
1629 		return (DCMD_USAGE);
1630 
1631 	if (mdb_getopts(argc, argv,
1632 	    'r', MDB_OPT_SETBITS, INT_MAX, &zpa.zpa_max_depth,
1633 	    'c', MDB_OPT_SETBITS, ZIO_WALK_CHILD, &zpa.zpa_type,
1634 	    'p', MDB_OPT_SETBITS, ZIO_WALK_PARENT, &zpa.zpa_type,
1635 	    NULL) != argc)
1636 		return (DCMD_USAGE);
1637 
1638 	zpa.zpa_flags = flags;
1639 	if (zpa.zpa_max_depth != 0) {
1640 		if (zpa.zpa_type == ZIO_WALK_SELF)
1641 			zpa.zpa_type = ZIO_WALK_CHILD;
1642 	} else if (zpa.zpa_type != ZIO_WALK_SELF) {
1643 		zpa.zpa_min_depth = 1;
1644 		zpa.zpa_max_depth = 1;
1645 	}
1646 
1647 	if (mdb_vread(&zio, sizeof (zio_t), addr) == -1) {
1648 		mdb_warn("failed to read zio_t at %p", addr);
1649 		return (DCMD_ERR);
1650 	}
1651 
1652 	if (!(flags & DCMD_PIPE_OUT) && DCMD_HDRSPEC(flags))
1653 		mdb_printf("%<u>%-*s %-5s %-16s %-?s%</u>\n", ZIO_MAXWIDTH,
1654 		    "ADDRESS", "TYPE", "STAGE", "WAITER");
1655 
1656 	if (zio_print_cb(addr, &zio, &zpa) != WALK_NEXT)
1657 		return (DCMD_ERR);
1658 
1659 	return (DCMD_OK);
1660 }
1661 
1662 /*
1663  * [addr]::zio_state
1664  *
1665  * Print a summary of all zio_t structures on the system, or for a particular
1666  * pool.  This is equivalent to '::walk zio_root | ::zio'.
1667  */
1668 /*ARGSUSED*/
1669 static int
1670 zio_state(uintptr_t addr, uint_t flags, int argc, const mdb_arg_t *argv)
1671 {
1672 	/*
1673 	 * MDB will remember the last address of the pipeline, so if we don't
1674 	 * zero this we'll end up trying to walk zio structures for a
1675 	 * non-existent spa_t.
1676 	 */
1677 	if (!(flags & DCMD_ADDRSPEC))
1678 		addr = 0;
1679 
1680 	return (mdb_pwalk_dcmd("zio_root", "zio", argc, argv, addr));
1681 }
1682 
1683 typedef struct txg_list_walk_data {
1684 	uintptr_t lw_head[TXG_SIZE];
1685 	int	lw_txgoff;
1686 	int	lw_maxoff;
1687 	size_t	lw_offset;
1688 	void	*lw_obj;
1689 } txg_list_walk_data_t;
1690 
1691 static int
1692 txg_list_walk_init_common(mdb_walk_state_t *wsp, int txg, int maxoff)
1693 {
1694 	txg_list_walk_data_t *lwd;
1695 	txg_list_t list;
1696 	int i;
1697 
1698 	lwd = mdb_alloc(sizeof (txg_list_walk_data_t), UM_SLEEP | UM_GC);
1699 	if (mdb_vread(&list, sizeof (txg_list_t), wsp->walk_addr) == -1) {
1700 		mdb_warn("failed to read txg_list_t at %#lx", wsp->walk_addr);
1701 		return (WALK_ERR);
1702 	}
1703 
1704 	for (i = 0; i < TXG_SIZE; i++)
1705 		lwd->lw_head[i] = (uintptr_t)list.tl_head[i];
1706 	lwd->lw_offset = list.tl_offset;
1707 	lwd->lw_obj = mdb_alloc(lwd->lw_offset + sizeof (txg_node_t),
1708 	    UM_SLEEP | UM_GC);
1709 	lwd->lw_txgoff = txg;
1710 	lwd->lw_maxoff = maxoff;
1711 
1712 	wsp->walk_addr = lwd->lw_head[lwd->lw_txgoff];
1713 	wsp->walk_data = lwd;
1714 
1715 	return (WALK_NEXT);
1716 }
1717 
1718 static int
1719 txg_list_walk_init(mdb_walk_state_t *wsp)
1720 {
1721 	return (txg_list_walk_init_common(wsp, 0, TXG_SIZE-1));
1722 }
1723 
1724 static int
1725 txg_list0_walk_init(mdb_walk_state_t *wsp)
1726 {
1727 	return (txg_list_walk_init_common(wsp, 0, 0));
1728 }
1729 
1730 static int
1731 txg_list1_walk_init(mdb_walk_state_t *wsp)
1732 {
1733 	return (txg_list_walk_init_common(wsp, 1, 1));
1734 }
1735 
1736 static int
1737 txg_list2_walk_init(mdb_walk_state_t *wsp)
1738 {
1739 	return (txg_list_walk_init_common(wsp, 2, 2));
1740 }
1741 
1742 static int
1743 txg_list3_walk_init(mdb_walk_state_t *wsp)
1744 {
1745 	return (txg_list_walk_init_common(wsp, 3, 3));
1746 }
1747 
1748 static int
1749 txg_list_walk_step(mdb_walk_state_t *wsp)
1750 {
1751 	txg_list_walk_data_t *lwd = wsp->walk_data;
1752 	uintptr_t addr;
1753 	txg_node_t *node;
1754 	int status;
1755 
1756 	while (wsp->walk_addr == NULL && lwd->lw_txgoff < lwd->lw_maxoff) {
1757 		lwd->lw_txgoff++;
1758 		wsp->walk_addr = lwd->lw_head[lwd->lw_txgoff];
1759 	}
1760 
1761 	if (wsp->walk_addr == NULL)
1762 		return (WALK_DONE);
1763 
1764 	addr = wsp->walk_addr - lwd->lw_offset;
1765 
1766 	if (mdb_vread(lwd->lw_obj,
1767 	    lwd->lw_offset + sizeof (txg_node_t), addr) == -1) {
1768 		mdb_warn("failed to read list element at %#lx", addr);
1769 		return (WALK_ERR);
1770 	}
1771 
1772 	status = wsp->walk_callback(addr, lwd->lw_obj, wsp->walk_cbdata);
1773 	node = (txg_node_t *)((uintptr_t)lwd->lw_obj + lwd->lw_offset);
1774 	wsp->walk_addr = (uintptr_t)node->tn_next[lwd->lw_txgoff];
1775 
1776 	return (status);
1777 }
1778 
1779 /*
1780  * ::walk spa
1781  *
1782  * Walk all named spa_t structures in the namespace.  This is nothing more than
1783  * a layered avl walk.
1784  */
1785 static int
1786 spa_walk_init(mdb_walk_state_t *wsp)
1787 {
1788 	GElf_Sym sym;
1789 
1790 	if (wsp->walk_addr != NULL) {
1791 		mdb_warn("spa walk only supports global walks\n");
1792 		return (WALK_ERR);
1793 	}
1794 
1795 	if (mdb_lookup_by_obj(ZFS_OBJ_NAME, "spa_namespace_avl", &sym) == -1) {
1796 		mdb_warn("failed to find symbol 'spa_namespace_avl'");
1797 		return (WALK_ERR);
1798 	}
1799 
1800 	wsp->walk_addr = (uintptr_t)sym.st_value;
1801 
1802 	if (mdb_layered_walk("avl", wsp) == -1) {
1803 		mdb_warn("failed to walk 'avl'\n");
1804 		return (WALK_ERR);
1805 	}
1806 
1807 	return (WALK_NEXT);
1808 }
1809 
1810 static int
1811 spa_walk_step(mdb_walk_state_t *wsp)
1812 {
1813 	spa_t	spa;
1814 
1815 	if (mdb_vread(&spa, sizeof (spa), wsp->walk_addr) == -1) {
1816 		mdb_warn("failed to read spa_t at %p", wsp->walk_addr);
1817 		return (WALK_ERR);
1818 	}
1819 
1820 	return (wsp->walk_callback(wsp->walk_addr, &spa, wsp->walk_cbdata));
1821 }
1822 
1823 /*
1824  * [addr]::walk zio
1825  *
1826  * Walk all active zio_t structures on the system.  This is simply a layered
1827  * walk on top of ::walk zio_cache, with the optional ability to limit the
1828  * structures to a particular pool.
1829  */
1830 static int
1831 zio_walk_init(mdb_walk_state_t *wsp)
1832 {
1833 	wsp->walk_data = (void *)wsp->walk_addr;
1834 
1835 	if (mdb_layered_walk("zio_cache", wsp) == -1) {
1836 		mdb_warn("failed to walk 'zio_cache'\n");
1837 		return (WALK_ERR);
1838 	}
1839 
1840 	return (WALK_NEXT);
1841 }
1842 
1843 static int
1844 zio_walk_step(mdb_walk_state_t *wsp)
1845 {
1846 	zio_t zio;
1847 
1848 	if (mdb_vread(&zio, sizeof (zio), wsp->walk_addr) == -1) {
1849 		mdb_warn("failed to read zio_t at %p", wsp->walk_addr);
1850 		return (WALK_ERR);
1851 	}
1852 
1853 	if (wsp->walk_data != NULL && wsp->walk_data != zio.io_spa)
1854 		return (WALK_NEXT);
1855 
1856 	return (wsp->walk_callback(wsp->walk_addr, &zio, wsp->walk_cbdata));
1857 }
1858 
1859 /*
1860  * [addr]::walk zio_root
1861  *
1862  * Walk only root zio_t structures, optionally for a particular spa_t.
1863  */
1864 static int
1865 zio_walk_root_step(mdb_walk_state_t *wsp)
1866 {
1867 	zio_t zio;
1868 
1869 	if (mdb_vread(&zio, sizeof (zio), wsp->walk_addr) == -1) {
1870 		mdb_warn("failed to read zio_t at %p", wsp->walk_addr);
1871 		return (WALK_ERR);
1872 	}
1873 
1874 	if (wsp->walk_data != NULL && wsp->walk_data != zio.io_spa)
1875 		return (WALK_NEXT);
1876 
1877 	/* If the parent list is not empty, ignore */
1878 	if (zio.io_parent_list.list_head.list_next !=
1879 	    &((zio_t *)wsp->walk_addr)->io_parent_list.list_head)
1880 		return (WALK_NEXT);
1881 
1882 	return (wsp->walk_callback(wsp->walk_addr, &zio, wsp->walk_cbdata));
1883 }
1884 
1885 #define	NICENUM_BUFLEN 6
1886 
1887 static int
1888 snprintfrac(char *buf, int len,
1889     uint64_t numerator, uint64_t denom, int frac_digits)
1890 {
1891 	int mul = 1;
1892 	int whole, frac, i;
1893 
1894 	for (i = frac_digits; i; i--)
1895 		mul *= 10;
1896 	whole = numerator / denom;
1897 	frac = mul * numerator / denom - mul * whole;
1898 	return (mdb_snprintf(buf, len, "%u.%0*u", whole, frac_digits, frac));
1899 }
1900 
1901 static void
1902 mdb_nicenum(uint64_t num, char *buf)
1903 {
1904 	uint64_t n = num;
1905 	int index = 0;
1906 	char *u;
1907 
1908 	while (n >= 1024) {
1909 		n = (n + (1024 / 2)) / 1024; /* Round up or down */
1910 		index++;
1911 	}
1912 
1913 	u = &" \0K\0M\0G\0T\0P\0E\0"[index*2];
1914 
1915 	if (index == 0) {
1916 		(void) mdb_snprintf(buf, NICENUM_BUFLEN, "%llu",
1917 		    (u_longlong_t)n);
1918 	} else if (n < 10 && (num & (num - 1)) != 0) {
1919 		(void) snprintfrac(buf, NICENUM_BUFLEN,
1920 		    num, 1ULL << 10 * index, 2);
1921 		strcat(buf, u);
1922 	} else if (n < 100 && (num & (num - 1)) != 0) {
1923 		(void) snprintfrac(buf, NICENUM_BUFLEN,
1924 		    num, 1ULL << 10 * index, 1);
1925 		strcat(buf, u);
1926 	} else {
1927 		(void) mdb_snprintf(buf, NICENUM_BUFLEN, "%llu%s",
1928 		    (u_longlong_t)n, u);
1929 	}
1930 }
1931 
1932 /*
1933  * ::zfs_blkstats
1934  *
1935  * 	-v	print verbose per-level information
1936  *
1937  */
1938 static int
1939 zfs_blkstats(uintptr_t addr, uint_t flags, int argc, const mdb_arg_t *argv)
1940 {
1941 	boolean_t verbose = B_FALSE;
1942 	zfs_all_blkstats_t stats;
1943 	dmu_object_type_t t;
1944 	zfs_blkstat_t *tzb;
1945 	uint64_t ditto;
1946 	dmu_object_type_info_t dmu_ot[DMU_OT_NUMTYPES + 10];
1947 	/* +10 in case it grew */
1948 
1949 	if (mdb_readvar(&dmu_ot, "dmu_ot") == -1) {
1950 		mdb_warn("failed to read 'dmu_ot'");
1951 		return (DCMD_ERR);
1952 	}
1953 
1954 	if (mdb_getopts(argc, argv,
1955 	    'v', MDB_OPT_SETBITS, TRUE, &verbose,
1956 	    NULL) != argc)
1957 		return (DCMD_USAGE);
1958 
1959 	if (!(flags & DCMD_ADDRSPEC))
1960 		return (DCMD_USAGE);
1961 
1962 	if (GETMEMB(addr, struct spa, spa_dsl_pool, addr) ||
1963 	    GETMEMB(addr, struct dsl_pool, dp_blkstats, addr) ||
1964 	    mdb_vread(&stats, sizeof (zfs_all_blkstats_t), addr) == -1) {
1965 		mdb_warn("failed to read data at %p;", addr);
1966 		mdb_printf("maybe no stats? run \"zpool scrub\" first.");
1967 		return (DCMD_ERR);
1968 	}
1969 
1970 	tzb = &stats.zab_type[DN_MAX_LEVELS][DMU_OT_NUMTYPES];
1971 	if (tzb->zb_gangs != 0) {
1972 		mdb_printf("Ganged blocks: %llu\n",
1973 		    (longlong_t)tzb->zb_gangs);
1974 	}
1975 
1976 	ditto = tzb->zb_ditto_2_of_2_samevdev + tzb->zb_ditto_2_of_3_samevdev +
1977 	    tzb->zb_ditto_3_of_3_samevdev;
1978 	if (ditto != 0) {
1979 		mdb_printf("Dittoed blocks on same vdev: %llu\n",
1980 		    (longlong_t)ditto);
1981 	}
1982 
1983 	mdb_printf("\nBlocks\tLSIZE\tPSIZE\tASIZE"
1984 	    "\t  avg\t comp\t%%Total\tType\n");
1985 
1986 	for (t = 0; t <= DMU_OT_NUMTYPES; t++) {
1987 		char csize[NICENUM_BUFLEN], lsize[NICENUM_BUFLEN];
1988 		char psize[NICENUM_BUFLEN], asize[NICENUM_BUFLEN];
1989 		char avg[NICENUM_BUFLEN];
1990 		char comp[NICENUM_BUFLEN], pct[NICENUM_BUFLEN];
1991 		char typename[64];
1992 		int l;
1993 
1994 
1995 		if (t == DMU_OT_DEFERRED)
1996 			strcpy(typename, "deferred free");
1997 		else if (t == DMU_OT_TOTAL)
1998 			strcpy(typename, "Total");
1999 		else if (mdb_readstr(typename, sizeof (typename),
2000 		    (uintptr_t)dmu_ot[t].ot_name) == -1) {
2001 			mdb_warn("failed to read type name");
2002 			return (DCMD_ERR);
2003 		}
2004 
2005 		if (stats.zab_type[DN_MAX_LEVELS][t].zb_asize == 0)
2006 			continue;
2007 
2008 		for (l = -1; l < DN_MAX_LEVELS; l++) {
2009 			int level = (l == -1 ? DN_MAX_LEVELS : l);
2010 			zfs_blkstat_t *zb = &stats.zab_type[level][t];
2011 
2012 			if (zb->zb_asize == 0)
2013 				continue;
2014 
2015 			/*
2016 			 * Don't print each level unless requested.
2017 			 */
2018 			if (!verbose && level != DN_MAX_LEVELS)
2019 				continue;
2020 
2021 			/*
2022 			 * If all the space is level 0, don't print the
2023 			 * level 0 separately.
2024 			 */
2025 			if (level == 0 && zb->zb_asize ==
2026 			    stats.zab_type[DN_MAX_LEVELS][t].zb_asize)
2027 				continue;
2028 
2029 			mdb_nicenum(zb->zb_count, csize);
2030 			mdb_nicenum(zb->zb_lsize, lsize);
2031 			mdb_nicenum(zb->zb_psize, psize);
2032 			mdb_nicenum(zb->zb_asize, asize);
2033 			mdb_nicenum(zb->zb_asize / zb->zb_count, avg);
2034 			(void) snprintfrac(comp, NICENUM_BUFLEN,
2035 			    zb->zb_lsize, zb->zb_psize, 2);
2036 			(void) snprintfrac(pct, NICENUM_BUFLEN,
2037 			    100 * zb->zb_asize, tzb->zb_asize, 2);
2038 
2039 			mdb_printf("%6s\t%5s\t%5s\t%5s\t%5s"
2040 			    "\t%5s\t%6s\t",
2041 			    csize, lsize, psize, asize, avg, comp, pct);
2042 
2043 			if (level == DN_MAX_LEVELS)
2044 				mdb_printf("%s\n", typename);
2045 			else
2046 				mdb_printf("  L%d %s\n",
2047 				    level, typename);
2048 		}
2049 	}
2050 
2051 	return (DCMD_OK);
2052 }
2053 
2054 /* ARGSUSED */
2055 static int
2056 reference_cb(uintptr_t addr, const void *ignored, void *arg)
2057 {
2058 	static int gotid;
2059 	static mdb_ctf_id_t ref_id;
2060 	uintptr_t ref_holder;
2061 	uintptr_t ref_removed;
2062 	uint64_t ref_number;
2063 	boolean_t holder_is_str;
2064 	char holder_str[128];
2065 	boolean_t removed = (boolean_t)arg;
2066 
2067 	if (!gotid) {
2068 		if (mdb_ctf_lookup_by_name("struct reference", &ref_id) == -1) {
2069 			mdb_warn("couldn't find struct reference");
2070 			return (WALK_ERR);
2071 		}
2072 		gotid = TRUE;
2073 	}
2074 
2075 	if (GETMEMBID(addr, &ref_id, ref_holder, ref_holder) ||
2076 	    GETMEMBID(addr, &ref_id, ref_removed, ref_removed) ||
2077 	    GETMEMBID(addr, &ref_id, ref_number, ref_number))
2078 		return (WALK_ERR);
2079 
2080 	if (mdb_readstr(holder_str, sizeof (holder_str), ref_holder) != -1) {
2081 		char *cp;
2082 		holder_is_str = B_TRUE;
2083 		for (cp = holder_str; *cp; cp++) {
2084 			if (!isprint(*cp)) {
2085 				holder_is_str = B_FALSE;
2086 				break;
2087 			}
2088 		}
2089 	} else {
2090 		holder_is_str = B_FALSE;
2091 	}
2092 
2093 	if (removed)
2094 		mdb_printf("removed ");
2095 	mdb_printf("reference ");
2096 	if (ref_number != 1)
2097 		mdb_printf("with count=%llu ", ref_number);
2098 	mdb_printf("with tag %p", (void*)ref_holder);
2099 	if (holder_is_str)
2100 		mdb_printf(" \"%s\"", holder_str);
2101 	mdb_printf(", held at:\n");
2102 
2103 	(void) mdb_call_dcmd("whatis", addr, DCMD_ADDRSPEC, 0, NULL);
2104 
2105 	if (removed) {
2106 		mdb_printf("removed at:\n");
2107 		(void) mdb_call_dcmd("whatis", ref_removed,
2108 		    DCMD_ADDRSPEC, 0, NULL);
2109 	}
2110 
2111 	mdb_printf("\n");
2112 
2113 	return (WALK_NEXT);
2114 }
2115 
2116 /* ARGSUSED */
2117 static int
2118 refcount(uintptr_t addr, uint_t flags, int argc, const mdb_arg_t *argv)
2119 {
2120 	uint64_t rc_count, rc_removed_count;
2121 	uintptr_t rc_list, rc_removed;
2122 	static int gotid;
2123 	static mdb_ctf_id_t rc_id;
2124 	ulong_t off;
2125 
2126 	if (!(flags & DCMD_ADDRSPEC))
2127 		return (DCMD_USAGE);
2128 
2129 	if (!gotid) {
2130 		if (mdb_ctf_lookup_by_name("struct refcount", &rc_id) == -1) {
2131 			mdb_warn("couldn't find struct refcount");
2132 			return (DCMD_ERR);
2133 		}
2134 		gotid = TRUE;
2135 	}
2136 
2137 	if (GETMEMBID(addr, &rc_id, rc_count, rc_count) ||
2138 	    GETMEMBID(addr, &rc_id, rc_removed_count, rc_removed_count))
2139 		return (DCMD_ERR);
2140 
2141 	mdb_printf("refcount_t at %p has %llu current holds, "
2142 	    "%llu recently released holds\n",
2143 	    addr, (longlong_t)rc_count, (longlong_t)rc_removed_count);
2144 
2145 	if (rc_count > 0)
2146 		mdb_printf("current holds:\n");
2147 	if (mdb_ctf_offsetof(rc_id, "rc_list", &off) == -1)
2148 		return (DCMD_ERR);
2149 	rc_list = addr + off/NBBY;
2150 	mdb_pwalk("list", reference_cb, (void*)B_FALSE, rc_list);
2151 
2152 	if (rc_removed_count > 0)
2153 		mdb_printf("released holds:\n");
2154 	if (mdb_ctf_offsetof(rc_id, "rc_removed", &off) == -1)
2155 		return (DCMD_ERR);
2156 	rc_removed = addr + off/NBBY;
2157 	mdb_pwalk("list", reference_cb, (void*)B_TRUE, rc_removed);
2158 
2159 	return (DCMD_OK);
2160 }
2161 
2162 /*
2163  * MDB module linkage information:
2164  *
2165  * We declare a list of structures describing our dcmds, and a function
2166  * named _mdb_init to return a pointer to our module information.
2167  */
2168 
2169 static const mdb_dcmd_t dcmds[] = {
2170 	{ "arc", "[-bkmg]", "print ARC variables", arc_print },
2171 	{ "blkptr", ":", "print blkptr_t", blkptr },
2172 	{ "dbuf", ":", "print dmu_buf_impl_t", dbuf },
2173 	{ "dbuf_stats", ":", "dbuf stats", dbuf_stats },
2174 	{ "dbufs",
2175 	    "\t[-O objset_t*] [-n objset_name | \"mos\"] "
2176 	    "[-o object | \"mdn\"] \n"
2177 	    "\t[-l level] [-b blkid | \"bonus\"]",
2178 	    "find dmu_buf_impl_t's that match specified criteria", dbufs },
2179 	{ "abuf_find", "dva_word[0] dva_word[1]",
2180 	    "find arc_buf_hdr_t of a specified DVA",
2181 	    abuf_find },
2182 	{ "spa", "?[-cv]", "spa_t summary", spa_print },
2183 	{ "spa_config", ":", "print spa_t configuration", spa_print_config },
2184 	{ "spa_verify", ":", "verify spa_t consistency", spa_verify },
2185 	{ "spa_space", ":[-b]", "print spa_t on-disk space usage", spa_space },
2186 	{ "spa_vdevs", ":", "given a spa_t, print vdev summary", spa_vdevs },
2187 	{ "vdev", ":[-re]\n"
2188 	    "\t-r display recursively\n"
2189 	    "\t-e print statistics",
2190 	    "vdev_t summary", vdev_print },
2191 	{ "zio", ":[cpr]\n"
2192 	    "\t-c display children\n"
2193 	    "\t-p display parents\n"
2194 	    "\t-r display recursively",
2195 	    "zio_t summary", zio_print },
2196 	{ "zio_state", "?", "print out all zio_t structures on system or "
2197 	    "for a particular pool", zio_state },
2198 	{ "zfs_blkstats", ":[-v]",
2199 	    "given a spa_t, print block type stats from last scrub",
2200 	    zfs_blkstats },
2201 	{ "zfs_params", "", "print zfs tunable parameters", zfs_params },
2202 	{ "refcount", "", "print refcount_t holders", refcount },
2203 	{ NULL }
2204 };
2205 
2206 static const mdb_walker_t walkers[] = {
2207 	/*
2208 	 * In userland, there is no generic provider of list_t walkers, so we
2209 	 * need to add it.
2210 	 */
2211 #ifndef _KERNEL
2212 	{ LIST_WALK_NAME, LIST_WALK_DESC,
2213 		list_walk_init, list_walk_step, list_walk_fini },
2214 #endif
2215 	{ "zms_freelist", "walk ZFS metaslab freelist",
2216 		freelist_walk_init, freelist_walk_step, NULL },
2217 	{ "txg_list", "given any txg_list_t *, walk all entries in all txgs",
2218 		txg_list_walk_init, txg_list_walk_step, NULL },
2219 	{ "txg_list0", "given any txg_list_t *, walk all entries in txg 0",
2220 		txg_list0_walk_init, txg_list_walk_step, NULL },
2221 	{ "txg_list1", "given any txg_list_t *, walk all entries in txg 1",
2222 		txg_list1_walk_init, txg_list_walk_step, NULL },
2223 	{ "txg_list2", "given any txg_list_t *, walk all entries in txg 2",
2224 		txg_list2_walk_init, txg_list_walk_step, NULL },
2225 	{ "txg_list3", "given any txg_list_t *, walk all entries in txg 3",
2226 		txg_list3_walk_init, txg_list_walk_step, NULL },
2227 	{ "zio", "walk all zio structures, optionally for a particular spa_t",
2228 		zio_walk_init, zio_walk_step, NULL },
2229 	{ "zio_root", "walk all root zio_t structures, optionally for a "
2230 	    "particular spa_t",
2231 		zio_walk_init, zio_walk_root_step, NULL },
2232 	{ "spa", "walk all spa_t entries in the namespace",
2233 		spa_walk_init, spa_walk_step, NULL },
2234 	{ "metaslab", "given a spa_t *, walk all metaslab_t structures",
2235 		metaslab_walk_init, metaslab_walk_step, NULL },
2236 	{ NULL }
2237 };
2238 
2239 static const mdb_modinfo_t modinfo = {
2240 	MDB_API_VERSION, dcmds, walkers
2241 };
2242 
2243 const mdb_modinfo_t *
2244 _mdb_init(void)
2245 {
2246 	return (&modinfo);
2247 }
2248