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