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