xref: /illumos-gate/usr/src/cmd/mdb/common/modules/zfs/zfs.c (revision d6bb6a8465e557cb946ef49d56ed3202f6218652)
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 2006 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 	int mapshift = SPA_MINBLOCKSHIFT;
176 
177 	if (mdb_vread(&entry, sizeof (entry), wsp->walk_addr) == -1) {
178 		mdb_warn("failed to read freelist entry %p", wsp->walk_addr);
179 		return (WALK_DONE);
180 	}
181 	wsp->walk_addr += sizeof (entry);
182 	wsp->walk_data = (void *)(number + 1);
183 
184 	if (SM_DEBUG_DECODE(entry)) {
185 		mdb_printf("DEBUG: %3u  %10s: txg=%llu  pass=%llu\n",
186 		    number,
187 		    ddata[SM_DEBUG_ACTION_DECODE(entry)],
188 		    SM_DEBUG_TXG_DECODE(entry),
189 		    SM_DEBUG_SYNCPASS_DECODE(entry));
190 	} else {
191 		mdb_printf("Entry: %3u  offsets=%08llx-%08llx  type=%c  "
192 		    "size=%06llx", number,
193 		    SM_OFFSET_DECODE(entry) << mapshift,
194 		    (SM_OFFSET_DECODE(entry) + SM_RUN_DECODE(entry)) <<
195 		    mapshift,
196 		    SM_TYPE_DECODE(entry) == SM_ALLOC ? 'A' : 'F',
197 		    SM_RUN_DECODE(entry) << mapshift);
198 		if (verbose)
199 			mdb_printf("      (raw=%012llx)\n", entry);
200 		mdb_printf("\n");
201 	}
202 	return (WALK_NEXT);
203 }
204 
205 /* ARGSUSED */
206 static void
207 freelist_walk_fini(mdb_walk_state_t *wsp)
208 {
209 }
210 
211 typedef struct dbuf_walk_data {
212 	dbuf_hash_table_t ht;
213 	int64_t bucket;
214 	uintptr_t dbp;
215 	dmu_buf_impl_t db;
216 } dbuf_walk_data_t;
217 
218 static int
219 dbuf_walk_init(mdb_walk_state_t *wsp)
220 {
221 	dbuf_walk_data_t *dwd;
222 
223 	if (wsp->walk_addr != NULL) {
224 		mdb_warn("must supply starting address\n");
225 		return (WALK_ERR);
226 	}
227 
228 	dwd = mdb_alloc(sizeof (dbuf_walk_data_t), UM_SLEEP);
229 
230 	if (mdb_readvar(&dwd->ht, "dbuf_hash_table") == -1) {
231 		mdb_warn("failed to read 'dbuf_hash_table'");
232 		mdb_free(dwd, sizeof (dbuf_walk_data_t));
233 		return (WALK_ERR);
234 	}
235 	dwd->bucket = -1;
236 	dwd->dbp = 0;
237 	wsp->walk_data = dwd;
238 	return (WALK_NEXT);
239 }
240 
241 static int
242 dbuf_walk_step(mdb_walk_state_t *wsp)
243 {
244 	int status;
245 	dbuf_walk_data_t *dwd = wsp->walk_data;
246 
247 	while (dwd->dbp == 0) {
248 		dwd->bucket++;
249 		if (dwd->bucket == dwd->ht.hash_table_mask+1)
250 			return (WALK_DONE);
251 
252 		if (mdb_vread(&dwd->dbp, sizeof (void *),
253 		    (uintptr_t)(dwd->ht.hash_table+dwd->bucket)) == -1) {
254 			mdb_warn("failed to read hash bucket %u at %p",
255 			    dwd->bucket, dwd->ht.hash_table+dwd->bucket);
256 			return (WALK_DONE);
257 		}
258 	}
259 
260 	wsp->walk_addr = dwd->dbp;
261 	if (mdb_vread(&dwd->db, sizeof (dmu_buf_impl_t),
262 	    wsp->walk_addr) == -1) {
263 		mdb_warn("failed to read dbuf at %p", wsp->walk_addr);
264 		return (WALK_DONE);
265 	}
266 	status = wsp->walk_callback(wsp->walk_addr, &dwd->db, wsp->walk_cbdata);
267 
268 	dwd->dbp = (uintptr_t)dwd->db.db_hash_next;
269 	return (status);
270 }
271 
272 static void
273 dbuf_walk_fini(mdb_walk_state_t *wsp)
274 {
275 	dbuf_walk_data_t *dwd = wsp->walk_data;
276 	mdb_free(dwd, sizeof (dbuf_walk_data_t));
277 }
278 
279 static int
280 dataset_name(uintptr_t addr, char *buf)
281 {
282 	static int gotid;
283 	static mdb_ctf_id_t dd_id;
284 	uintptr_t dd_parent;
285 	char dd_myname[MAXNAMELEN];
286 
287 	if (!gotid) {
288 		if (mdb_ctf_lookup_by_name("struct dsl_dir",
289 		    &dd_id) == -1) {
290 			mdb_warn("couldn't find struct dsl_dir");
291 			return (DCMD_ERR);
292 		}
293 		gotid = TRUE;
294 	}
295 	if (GETMEMBID(addr, &dd_id, dd_parent, dd_parent) ||
296 	    GETMEMBID(addr, &dd_id, dd_myname, dd_myname)) {
297 		return (DCMD_ERR);
298 	}
299 
300 	if (dd_parent) {
301 		if (dataset_name(dd_parent, buf))
302 			return (DCMD_ERR);
303 		strcat(buf, "/");
304 	}
305 
306 	if (dd_myname[0])
307 		strcat(buf, dd_myname);
308 	else
309 		strcat(buf, "???");
310 
311 	return (0);
312 }
313 
314 static int
315 objset_name(uintptr_t addr, char *buf)
316 {
317 	static int gotid;
318 	static mdb_ctf_id_t osi_id, ds_id;
319 	uintptr_t os_dsl_dataset;
320 	char ds_snapname[MAXNAMELEN];
321 	uintptr_t ds_dir;
322 
323 	buf[0] = '\0';
324 
325 	if (!gotid) {
326 		if (mdb_ctf_lookup_by_name("struct objset_impl",
327 		    &osi_id) == -1) {
328 			mdb_warn("couldn't find struct objset_impl");
329 			return (DCMD_ERR);
330 		}
331 		if (mdb_ctf_lookup_by_name("struct dsl_dataset",
332 		    &ds_id) == -1) {
333 			mdb_warn("couldn't find struct dsl_dataset");
334 			return (DCMD_ERR);
335 		}
336 
337 		gotid = TRUE;
338 	}
339 
340 	if (GETMEMBID(addr, &osi_id, os_dsl_dataset, os_dsl_dataset))
341 		return (DCMD_ERR);
342 
343 	if (os_dsl_dataset == 0) {
344 		strcat(buf, "mos");
345 		return (0);
346 	}
347 
348 	if (GETMEMBID(os_dsl_dataset, &ds_id, ds_snapname, ds_snapname) ||
349 	    GETMEMBID(os_dsl_dataset, &ds_id, ds_dir, ds_dir)) {
350 		return (DCMD_ERR);
351 	}
352 
353 	if (ds_dir && dataset_name(ds_dir, buf))
354 		return (DCMD_ERR);
355 
356 	if (ds_snapname[0]) {
357 		strcat(buf, "@");
358 		strcat(buf, ds_snapname);
359 	}
360 	return (0);
361 }
362 
363 static void
364 enum_lookup(char *out, size_t size, mdb_ctf_id_t id, int val,
365     const char *prefix)
366 {
367 	const char *cp;
368 	size_t len = strlen(prefix);
369 
370 	if ((cp = mdb_ctf_enum_name(id, val)) != NULL) {
371 		if (strncmp(cp, prefix, len) == 0)
372 			cp += len;
373 		(void) strncpy(out, cp, size);
374 	} else {
375 		mdb_snprintf(out, size, "? (%d)", val);
376 	}
377 }
378 
379 /* ARGSUSED */
380 static int
381 zio_pipeline(uintptr_t addr, uint_t flags, int argc, const mdb_arg_t *argv)
382 {
383 	mdb_ctf_id_t pipe_enum;
384 	int i;
385 	char stage[1024];
386 
387 	if (mdb_ctf_lookup_by_name("enum zio_stage", &pipe_enum) == -1) {
388 		mdb_warn("Could not find enum zio_stage");
389 		return (DCMD_ERR);
390 	}
391 
392 	for (i = 0; i < 32; i++) {
393 		if (addr & (1U << i)) {
394 			enum_lookup(stage, sizeof (stage), pipe_enum, i,
395 			    "ZIO_STAGE_");
396 			mdb_printf("    %s\n", stage);
397 		}
398 	}
399 
400 	return (DCMD_OK);
401 }
402 
403 /* ARGSUSED */
404 static int
405 blkptr(uintptr_t addr, uint_t flags, int argc, const mdb_arg_t *argv)
406 {
407 	blkptr_t bp;
408 	dva_t *dva;
409 	dmu_object_type_info_t *doti;
410 	zio_compress_info_t *zct;
411 	zio_checksum_info_t *zci;
412 	int i;
413 	char buf[MAXPATHLEN];
414 
415 	if (mdb_vread(&bp, sizeof (blkptr_t), addr) == -1) {
416 		mdb_warn("failed to read blkptr_t");
417 		return (DCMD_ERR);
418 	}
419 
420 	if (read_symbol("dmu_ot", (void **)&doti) != DCMD_OK)
421 		return (DCMD_ERR);
422 	for (i = 0; i < DMU_OT_NUMTYPES; i++) {
423 		mdb_readstr(buf, sizeof (buf), (uintptr_t)doti[i].ot_name);
424 		doti[i].ot_name = local_strdup(buf);
425 	}
426 
427 	if (read_symbol("zio_checksum_table", (void **)&zci) != DCMD_OK)
428 		return (DCMD_ERR);
429 	for (i = 0; i < ZIO_CHECKSUM_FUNCTIONS; i++) {
430 		mdb_readstr(buf, sizeof (buf), (uintptr_t)zci[i].ci_name);
431 		zci[i].ci_name = local_strdup(buf);
432 	}
433 
434 	if (read_symbol("zio_compress_table", (void **)&zct) != DCMD_OK)
435 		return (DCMD_ERR);
436 	for (i = 0; i < ZIO_COMPRESS_FUNCTIONS; i++) {
437 		mdb_readstr(buf, sizeof (buf), (uintptr_t)zct[i].ci_name);
438 		zct[i].ci_name = local_strdup(buf);
439 	}
440 
441 	for (i = 0; i < SPA_DVAS_PER_BP; i++) {
442 		dva = &bp.blk_dva[i];
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]:                    GRID:  %04x\t"
446 		    "ASIZE: %llx\n", i, DVA_GET_GRID(dva), DVA_GET_ASIZE(dva));
447 	}
448 	mdb_printf("LSIZE:  %-16llx\t\tPSIZE: %llx\n",
449 	    BP_GET_LSIZE(&bp), BP_GET_PSIZE(&bp));
450 	mdb_printf("ENDIAN: %6s             GANG:  %-5s\tTYPE:  %s\n",
451 	    BP_GET_BYTEORDER(&bp) ? "LITTLE" : "BIG",
452 	    DVA_GET_GANG(dva) ? "TRUE" : "FALSE",
453 	    doti[BP_GET_TYPE(&bp)].ot_name);
454 	mdb_printf("BIRTH:  %-16llx   LEVEL: %-2d\tFILL:  %llx\n",
455 	    bp.blk_birth, BP_GET_LEVEL(&bp), bp.blk_fill);
456 	mdb_printf("CKFUNC: %-16s\t\tCOMP:  %s\n",
457 	    zci[BP_GET_CHECKSUM(&bp)].ci_name,
458 	    zct[BP_GET_COMPRESS(&bp)].ci_name);
459 	mdb_printf("CKSUM:  %llx:%llx:%llx:%llx\n",
460 	    bp.blk_cksum.zc_word[0],
461 	    bp.blk_cksum.zc_word[1],
462 	    bp.blk_cksum.zc_word[2],
463 	    bp.blk_cksum.zc_word[3]);
464 
465 	return (DCMD_OK);
466 }
467 
468 /* ARGSUSED */
469 static int
470 dbuf(uintptr_t addr, uint_t flags, int argc, const mdb_arg_t *argv)
471 {
472 	mdb_ctf_id_t id;
473 	dmu_buf_t db;
474 	uintptr_t objset;
475 	uint8_t level;
476 	uint64_t blkid;
477 	uint64_t holds;
478 	char objectname[32];
479 	char blkidname[32];
480 	char path[MAXNAMELEN];
481 
482 	if (DCMD_HDRSPEC(flags)) {
483 		mdb_printf("        addr object lvl blkid holds os\n");
484 	}
485 
486 	if (mdb_ctf_lookup_by_name("struct dmu_buf_impl", &id) == -1) {
487 		mdb_warn("couldn't find struct dmu_buf_impl_t");
488 		return (DCMD_ERR);
489 	}
490 
491 	if (GETMEMBID(addr, &id, db_objset, objset) ||
492 	    GETMEMBID(addr, &id, db, db) ||
493 	    GETMEMBID(addr, &id, db_level, level) ||
494 	    GETMEMBID(addr, &id, db_blkid, blkid)) {
495 		return (WALK_ERR);
496 	}
497 
498 	if (getrefcount(addr, &id, "db_holds", &holds)) {
499 		return (WALK_ERR);
500 	}
501 
502 	if (db.db_object == DMU_META_DNODE_OBJECT)
503 		(void) strcpy(objectname, "mdn");
504 	else
505 		(void) mdb_snprintf(objectname, sizeof (objectname), "%llx",
506 		    (u_longlong_t)db.db_object);
507 
508 	if (blkid == DB_BONUS_BLKID)
509 		(void) strcpy(blkidname, "bonus");
510 	else
511 		(void) mdb_snprintf(blkidname, sizeof (blkidname), "%llx",
512 		    (u_longlong_t)blkid);
513 
514 	if (objset_name(objset, path)) {
515 		return (WALK_ERR);
516 	}
517 
518 	mdb_printf("%p %8s %1u %9s %2llu %s\n",
519 	    addr, objectname, level, blkidname, holds, path);
520 
521 	return (DCMD_OK);
522 }
523 
524 /* ARGSUSED */
525 static int
526 dbuf_stats(uintptr_t addr, uint_t flags, int argc, const mdb_arg_t *argv)
527 {
528 #define	HISTOSZ 32
529 	uintptr_t dbp;
530 	dmu_buf_impl_t db;
531 	dbuf_hash_table_t ht;
532 	uint64_t bucket, ndbufs;
533 	uint64_t histo[HISTOSZ];
534 	uint64_t histo2[HISTOSZ];
535 	int i, maxidx;
536 
537 	if (mdb_readvar(&ht, "dbuf_hash_table") == -1) {
538 		mdb_warn("failed to read 'dbuf_hash_table'");
539 		return (DCMD_ERR);
540 	}
541 
542 	for (i = 0; i < HISTOSZ; i++) {
543 		histo[i] = 0;
544 		histo2[i] = 0;
545 	}
546 
547 	ndbufs = 0;
548 	for (bucket = 0; bucket < ht.hash_table_mask+1; bucket++) {
549 		int len;
550 
551 		if (mdb_vread(&dbp, sizeof (void *),
552 		    (uintptr_t)(ht.hash_table+bucket)) == -1) {
553 			mdb_warn("failed to read hash bucket %u at %p",
554 			    bucket, ht.hash_table+bucket);
555 			return (DCMD_ERR);
556 		}
557 
558 		len = 0;
559 		while (dbp != 0) {
560 			if (mdb_vread(&db, sizeof (dmu_buf_impl_t),
561 			    dbp) == -1) {
562 				mdb_warn("failed to read dbuf at %p", dbp);
563 				return (DCMD_ERR);
564 			}
565 			dbp = (uintptr_t)db.db_hash_next;
566 			for (i = MIN(len, HISTOSZ - 1); i >= 0; i--)
567 				histo2[i]++;
568 			len++;
569 			ndbufs++;
570 		}
571 
572 		if (len >= HISTOSZ)
573 			len = HISTOSZ-1;
574 		histo[len]++;
575 	}
576 
577 	mdb_printf("hash table has %llu buckets, %llu dbufs "
578 	    "(avg %llu buckets/dbuf)\n",
579 	    ht.hash_table_mask+1, ndbufs,
580 	    (ht.hash_table_mask+1)/ndbufs);
581 
582 	mdb_printf("\n");
583 	maxidx = 0;
584 	for (i = 0; i < HISTOSZ; i++)
585 		if (histo[i] > 0)
586 			maxidx = i;
587 	mdb_printf("hash chain length	number of buckets\n");
588 	for (i = 0; i <= maxidx; i++)
589 		mdb_printf("%u			%llu\n", i, histo[i]);
590 
591 	mdb_printf("\n");
592 	maxidx = 0;
593 	for (i = 0; i < HISTOSZ; i++)
594 		if (histo2[i] > 0)
595 			maxidx = i;
596 	mdb_printf("hash chain depth	number of dbufs\n");
597 	for (i = 0; i <= maxidx; i++)
598 		mdb_printf("%u or more		%llu	%llu%%\n",
599 		    i, histo2[i], histo2[i]*100/ndbufs);
600 
601 
602 	return (DCMD_OK);
603 }
604 
605 typedef struct dbufs_data {
606 	mdb_ctf_id_t id;
607 	uint64_t objset;
608 	uint64_t object;
609 	uint64_t level;
610 	uint64_t blkid;
611 	char *osname;
612 } dbufs_data_t;
613 
614 #define	DBUFS_UNSET	(0xbaddcafedeadbeefULL)
615 
616 /* ARGSUSED */
617 static int
618 dbufs_cb(uintptr_t addr, const void *unknown, void *arg)
619 {
620 	dbufs_data_t *data = arg;
621 	uintptr_t objset;
622 	dmu_buf_t db;
623 	uint8_t level;
624 	uint64_t blkid;
625 	char osname[MAXNAMELEN];
626 
627 	if (GETMEMBID(addr, &data->id, db_objset, objset) ||
628 	    GETMEMBID(addr, &data->id, db, db) ||
629 	    GETMEMBID(addr, &data->id, db_level, level) ||
630 	    GETMEMBID(addr, &data->id, db_blkid, blkid)) {
631 		return (WALK_ERR);
632 	}
633 
634 	if ((data->objset == DBUFS_UNSET || data->objset == objset) &&
635 	    (data->osname == NULL || (objset_name(objset, osname) == 0 &&
636 		strcmp(data->osname, osname) == 0)) &&
637 	    (data->object == DBUFS_UNSET || data->object == db.db_object) &&
638 	    (data->level == DBUFS_UNSET || data->level == level) &&
639 	    (data->blkid == DBUFS_UNSET || data->blkid == blkid)) {
640 		mdb_printf("%#lr\n", addr);
641 	}
642 	return (WALK_NEXT);
643 }
644 
645 /* ARGSUSED */
646 static int
647 dbufs(uintptr_t addr, uint_t flags, int argc, const mdb_arg_t *argv)
648 {
649 	dbufs_data_t data;
650 	char *object = NULL;
651 	char *blkid = NULL;
652 
653 	data.objset = data.object = data.level = data.blkid = DBUFS_UNSET;
654 	data.osname = NULL;
655 
656 	if (mdb_getopts(argc, argv,
657 	    'O', MDB_OPT_UINT64, &data.objset,
658 	    'n', MDB_OPT_STR, &data.osname,
659 	    'o', MDB_OPT_STR, &object,
660 	    'l', MDB_OPT_UINT64, &data.level,
661 	    'b', MDB_OPT_STR, &blkid) != argc) {
662 		return (DCMD_USAGE);
663 	}
664 
665 	if (object) {
666 		if (strcmp(object, "mdn") == 0) {
667 			data.object = DMU_META_DNODE_OBJECT;
668 		} else {
669 			data.object = mdb_strtoull(object);
670 		}
671 	}
672 
673 	if (blkid) {
674 		if (strcmp(blkid, "bonus") == 0) {
675 			data.blkid = DB_BONUS_BLKID;
676 		} else {
677 			data.blkid = mdb_strtoull(blkid);
678 		}
679 	}
680 
681 	if (mdb_ctf_lookup_by_name("struct dmu_buf_impl", &data.id) == -1) {
682 		mdb_warn("couldn't find struct dmu_buf_impl_t");
683 		return (DCMD_ERR);
684 	}
685 
686 	if (mdb_pwalk("dbufs", dbufs_cb, &data, 0) != 0) {
687 		mdb_warn("can't walk dbufs");
688 		return (DCMD_ERR);
689 	}
690 
691 	return (DCMD_OK);
692 }
693 
694 typedef struct abuf_find_data {
695 	dva_t dva;
696 	mdb_ctf_id_t id;
697 } abuf_find_data_t;
698 
699 /* ARGSUSED */
700 static int
701 abuf_find_cb(uintptr_t addr, const void *unknown, void *arg)
702 {
703 	abuf_find_data_t *data = arg;
704 	dva_t dva;
705 
706 	if (GETMEMBID(addr, &data->id, b_dva, dva)) {
707 		return (WALK_ERR);
708 	}
709 
710 	if (dva.dva_word[0] == data->dva.dva_word[0] &&
711 	    dva.dva_word[1] == data->dva.dva_word[1]) {
712 		mdb_printf("%#lr\n", addr);
713 	}
714 	return (WALK_NEXT);
715 }
716 
717 /* ARGSUSED */
718 static int
719 abuf_find(uintptr_t addr, uint_t flags, int argc, const mdb_arg_t *argv)
720 {
721 	abuf_find_data_t data;
722 	GElf_Sym sym;
723 	int i;
724 	const char *syms[] = {
725 		"ARC_mru_top",
726 		"ARC_mru_bot",
727 		"ARC_mfu_top",
728 		"ARC_mfu_bot",
729 	};
730 
731 	if (argc != 2)
732 		return (DCMD_USAGE);
733 
734 	for (i = 0; i < 2; i ++) {
735 		switch (argv[i].a_type) {
736 		case MDB_TYPE_STRING:
737 			data.dva.dva_word[i] = mdb_strtoull(argv[i].a_un.a_str);
738 			break;
739 		case MDB_TYPE_IMMEDIATE:
740 			data.dva.dva_word[i] = argv[i].a_un.a_val;
741 			break;
742 		default:
743 			return (DCMD_USAGE);
744 		}
745 	}
746 
747 	if (mdb_ctf_lookup_by_name("struct arc_buf_hdr", &data.id) == -1) {
748 		mdb_warn("couldn't find struct arc_buf_hdr");
749 		return (DCMD_ERR);
750 	}
751 
752 	for (i = 0; i < sizeof (syms) / sizeof (syms[0]); i++) {
753 		if (mdb_lookup_by_name(syms[i], &sym)) {
754 			mdb_warn("can't find symbol %s", syms[i]);
755 			return (DCMD_ERR);
756 		}
757 
758 		if (mdb_pwalk("list", abuf_find_cb, &data, sym.st_value) != 0) {
759 			mdb_warn("can't walk %s", syms[i]);
760 			return (DCMD_ERR);
761 		}
762 	}
763 
764 	return (DCMD_OK);
765 }
766 
767 void
768 abuf_help(void)
769 {
770 	mdb_printf("::abuf_find dva_word[0] dva_word[1]\n");
771 }
772 
773 /*
774  * ::spa
775  *
776  * 	-c	Print configuration information as well
777  * 	-v	Print vdev state
778  * 	-e	Print vdev error stats
779  *
780  * Print a summarized spa_t.  When given no arguments, prints out a table of all
781  * active pools on the system.
782  */
783 /* ARGSUSED */
784 static int
785 spa_print(uintptr_t addr, uint_t flags, int argc, const mdb_arg_t *argv)
786 {
787 	spa_t spa;
788 	char poolname[MAXNAMELEN];
789 	const char *statetab[] = { "ACTIVE", "EXPORTED", "DESTROYED",
790 		"UNINIT", "UNAVAIL" };
791 	const char *state;
792 	int config = FALSE;
793 	int vdevs = FALSE;
794 	int errors = FALSE;
795 
796 	if (mdb_getopts(argc, argv,
797 	    'c', MDB_OPT_SETBITS, TRUE, &config,
798 	    'v', MDB_OPT_SETBITS, TRUE, &vdevs,
799 	    'e', MDB_OPT_SETBITS, TRUE, &errors,
800 	    NULL) != argc)
801 		return (DCMD_USAGE);
802 
803 	if (!(flags & DCMD_ADDRSPEC)) {
804 		if (mdb_walk_dcmd("spa", "spa", argc, argv) == -1) {
805 			mdb_warn("can't walk spa");
806 			return (DCMD_ERR);
807 		}
808 
809 		return (DCMD_OK);
810 	}
811 
812 	if (flags & DCMD_PIPE_OUT) {
813 		mdb_printf("%#lr\n", addr);
814 		return (DCMD_OK);
815 	}
816 
817 	if (DCMD_HDRSPEC(flags))
818 		mdb_printf("%<u>%-?s %9s %-*s%</u>\n", "ADDR", "STATE",
819 		    sizeof (uintptr_t) == 4 ? 60 : 52, "NAME");
820 
821 	if (mdb_vread(&spa, sizeof (spa), addr) == -1) {
822 		mdb_warn("failed to read spa_t at %p", addr);
823 		return (DCMD_ERR);
824 	}
825 
826 	if (mdb_readstr(poolname, sizeof (poolname), (uintptr_t)spa.spa_name)
827 	    == -1) {
828 		mdb_warn("failed to read pool name at %p", spa.spa_name);
829 		return (DCMD_ERR);
830 	}
831 
832 	if (spa.spa_state < 0 || spa.spa_state > POOL_STATE_UNAVAIL)
833 		state = "UNKNOWN";
834 	else
835 		state = statetab[spa.spa_state];
836 
837 	mdb_printf("%0?p %9s %s\n", addr, state, poolname);
838 
839 	if (config) {
840 		mdb_printf("\n");
841 		mdb_inc_indent(4);
842 		if (mdb_call_dcmd("spa_config", addr, flags, 0,
843 		    NULL) != DCMD_OK)
844 			return (DCMD_ERR);
845 		mdb_dec_indent(4);
846 	}
847 
848 	if (vdevs || errors) {
849 		mdb_arg_t v;
850 
851 		v.a_type = MDB_TYPE_STRING;
852 		v.a_un.a_str = "-e";
853 
854 		mdb_printf("\n");
855 		mdb_inc_indent(4);
856 		if (mdb_call_dcmd("spa_vdevs", addr, flags, errors ? 1 : 0,
857 		    &v) != DCMD_OK)
858 			return (DCMD_ERR);
859 		mdb_dec_indent(4);
860 	}
861 
862 	return (DCMD_OK);
863 }
864 
865 /*
866  * ::spa_config
867  *
868  * Given a spa_t, print the configuration information stored in spa_config.
869  * Since it's just an nvlist, format it as an indented list of name=value pairs.
870  * We simply read the value of spa_config and pass off to ::nvlist.
871  */
872 /* ARGSUSED */
873 static int
874 spa_print_config(uintptr_t addr, uint_t flags, int argc, const mdb_arg_t *argv)
875 {
876 	spa_t spa;
877 
878 	if (argc != 0 || !(flags & DCMD_ADDRSPEC))
879 		return (DCMD_USAGE);
880 
881 	if (mdb_vread(&spa, sizeof (spa), addr) == -1) {
882 		mdb_warn("failed to read spa_t at %p", addr);
883 		return (DCMD_ERR);
884 	}
885 
886 	if (spa.spa_config == NULL) {
887 		mdb_printf("(none)\n");
888 		return (DCMD_OK);
889 	}
890 
891 	return (mdb_call_dcmd("nvlist", (uintptr_t)spa.spa_config, flags,
892 	    0, NULL));
893 }
894 
895 void
896 vdev_help(void)
897 {
898 	mdb_printf("[vdev_t*]::vdev [-qr]\n"
899 		"\t-> -q display vdev_queue parameters\n"
900 		"\t-> -r recursive (visit all children)\n");
901 }
902 
903 /*
904  * ::vdev
905  *
906  * Print out a summarized vdev_t, in the following form:
907  *
908  * ADDR             STATE	AUX            DESC
909  * fffffffbcde23df0 HEALTHY	-              /dev/dsk/c0t0d0
910  *
911  * or with "-q" to print out a vdev_t's vdev_queue parameters:
912  *
913  *  vdev_t: c26ae4c0
914  *     c26ae73c min pending         0x2
915  *     c26ae744 max pending         0x23
916  *     c26ae74c agg limit           0x20000
917  *     c26ae754 time shift          0x4
918  *     c26ae75c ramp rate           0x2
919  *
920  * If '-r' is specified, recursively visit all children.
921  *
922  * With '-e', the statistics associated with the vdev are printed as well.
923  */
924 static int
925 do_print_vdev(uintptr_t addr, int flags, int depth, int queue, int stats,
926     int recursive)
927 {
928 	vdev_t vdev;
929 	char desc[MAXNAMELEN];
930 	int c, children;
931 	uintptr_t *child;
932 	const char *state, *aux;
933 
934 	if (mdb_vread(&vdev, sizeof (vdev), (uintptr_t)addr) == -1) {
935 		mdb_warn("failed to read vdev_t at %p\n", (uintptr_t)addr);
936 		return (DCMD_ERR);
937 	}
938 
939 	if (flags & DCMD_PIPE_OUT) {
940 		mdb_printf("%#lr", addr);
941 	} else {
942 		if (vdev.vdev_path != NULL) {
943 			if (mdb_readstr(desc, sizeof (desc),
944 			    (uintptr_t)vdev.vdev_path) == -1) {
945 				mdb_warn("failed to read vdev_path at %p\n",
946 				    vdev.vdev_path);
947 				return (DCMD_ERR);
948 			}
949 		} else if (vdev.vdev_ops != NULL) {
950 			vdev_ops_t ops;
951 			if (mdb_vread(&ops, sizeof (ops),
952 			    (uintptr_t)vdev.vdev_ops) == -1) {
953 				mdb_warn("failed to read vdev_ops at %p\n",
954 				    vdev.vdev_ops);
955 				return (DCMD_ERR);
956 			}
957 			(void) strcpy(desc, ops.vdev_op_type);
958 		} else {
959 			(void) strcpy(desc, "<unknown>");
960 		}
961 
962 		if (depth == 0 && DCMD_HDRSPEC(flags))
963 			mdb_printf("%<u>%-?s %-9s %-12s %-*s%</u>\n",
964 			    "ADDR", "STATE", "AUX",
965 			    sizeof (uintptr_t) == 4 ? 43 : 35,
966 			    "DESCRIPTION");
967 
968 		mdb_printf("%0?p ", addr);
969 
970 		switch (vdev.vdev_state) {
971 		case VDEV_STATE_CLOSED:
972 		    state = "CLOSED";
973 		    break;
974 		case VDEV_STATE_OFFLINE:
975 		    state = "OFFLINE";
976 		    break;
977 		case VDEV_STATE_CANT_OPEN:
978 		    state = "CANT_OPEN";
979 		    break;
980 		case VDEV_STATE_DEGRADED:
981 		    state = "DEGRADED";
982 		    break;
983 		case VDEV_STATE_HEALTHY:
984 		    state = "HEALTHY";
985 		    break;
986 		default:
987 		    state = "UNKNOWN";
988 		    break;
989 		}
990 
991 		switch (vdev.vdev_stat.vs_aux) {
992 		case VDEV_AUX_NONE:
993 			aux = "-";
994 			break;
995 		case VDEV_AUX_OPEN_FAILED:
996 			aux = "OPEN_FAILED";
997 			break;
998 		case VDEV_AUX_CORRUPT_DATA:
999 			aux = "CORRUPT_DATA";
1000 			break;
1001 		case VDEV_AUX_NO_REPLICAS:
1002 			aux = "NO_REPLICAS";
1003 			break;
1004 		case VDEV_AUX_BAD_GUID_SUM:
1005 			aux = "BAD_GUID_SUM";
1006 			break;
1007 		case VDEV_AUX_TOO_SMALL:
1008 			aux = "TOO_SMALL";
1009 			break;
1010 		case VDEV_AUX_BAD_LABEL:
1011 			aux = "BAD_LABEL";
1012 			break;
1013 		default:
1014 			aux = "UNKNOWN";
1015 			break;
1016 		}
1017 
1018 		mdb_printf("%-9s %-12s %*s%s\n", state, aux, depth, "", desc);
1019 
1020 		if (queue) {
1021 			mdb_inc_indent(4);
1022 			mdb_printf("\n");
1023 			mdb_printf("%p min pending		0x%llx\n",
1024 			    (uintptr_t)(addr + offsetof(vdev_t,
1025 			    vdev_queue.vq_min_pending)),
1026 			    vdev.vdev_queue.vq_min_pending);
1027 			mdb_printf("%p max pending		0x%llx\n",
1028 			    (uintptr_t)(addr + offsetof(vdev_t,
1029 			    vdev_queue.vq_max_pending)),
1030 			    vdev.vdev_queue.vq_max_pending);
1031 			mdb_printf("%p agg limit		0x%llx\n",
1032 			    (uintptr_t)(addr + offsetof(vdev_t,
1033 			    vdev_queue.vq_agg_limit)),
1034 			    vdev.vdev_queue.vq_agg_limit);
1035 			mdb_printf("%p time shift		0x%llx\n",
1036 			    (uintptr_t)(addr + offsetof(vdev_t,
1037 			    vdev_queue.vq_time_shift)),
1038 			    vdev.vdev_queue.vq_time_shift);
1039 			mdb_printf("%p ramp rate 		0x%llx\n",
1040 			    (uintptr_t)(addr + offsetof(vdev_t,
1041 			    vdev_queue.vq_ramp_rate)),
1042 			    vdev.vdev_queue.vq_ramp_rate);
1043 			mdb_dec_indent(4);
1044 		}
1045 
1046 		if (stats) {
1047 			vdev_stat_t *vs = &vdev.vdev_stat;
1048 			int i;
1049 
1050 			mdb_inc_indent(4);
1051 			mdb_printf("\n");
1052 			mdb_printf("%<u>       %12s %12s %12s %12s "
1053 			    "%12s%</u>\n", "READ", "WRITE", "FREE", "CLAIM",
1054 			    "IOCTL");
1055 			mdb_printf("OPS     ");
1056 			for (i = 1; i < ZIO_TYPES; i++)
1057 				mdb_printf("%11#llx%s", vs->vs_ops[i],
1058 				    i == ZIO_TYPES - 1 ? "" : "  ");
1059 			mdb_printf("\n");
1060 			mdb_printf("BYTES   ");
1061 			for (i = 1; i < ZIO_TYPES; i++)
1062 				mdb_printf("%11#llx%s", vs->vs_bytes[i],
1063 				    i == ZIO_TYPES - 1 ? "" : "  ");
1064 
1065 
1066 			mdb_printf("\n");
1067 			mdb_printf("EREAD    %10#llx\n", vs->vs_read_errors);
1068 			mdb_printf("EWRITE   %10#llx\n", vs->vs_write_errors);
1069 			mdb_printf("ECKSUM   %10#llx\n",
1070 			    vs->vs_checksum_errors);
1071 			mdb_dec_indent(4);
1072 		}
1073 
1074 		if (queue || stats)
1075 			mdb_printf("\n");
1076 	}
1077 
1078 	children = vdev.vdev_children;
1079 
1080 	if (children == 0 || !recursive)
1081 		return (DCMD_OK);
1082 
1083 	child = mdb_alloc(children * sizeof (void *), UM_SLEEP | UM_GC);
1084 	if (mdb_vread(child, children * sizeof (void *),
1085 	    (uintptr_t)vdev.vdev_child) == -1) {
1086 		mdb_warn("failed to read vdev children at %p", vdev.vdev_child);
1087 		return (DCMD_ERR);
1088 	}
1089 
1090 	for (c = 0; c < children; c++) {
1091 		if (do_print_vdev(child[c], flags, depth + 2, queue, stats,
1092 		    recursive))
1093 			return (DCMD_ERR);
1094 	}
1095 
1096 	return (DCMD_OK);
1097 }
1098 
1099 static int
1100 vdev_print(uintptr_t addr, uint_t flags, int argc, const mdb_arg_t *argv)
1101 {
1102 	int print_queue = FALSE;
1103 	int recursive = FALSE;
1104 	int stats = FALSE;
1105 
1106 	if (mdb_getopts(argc, argv,
1107 	    'q', MDB_OPT_SETBITS, TRUE, &print_queue,
1108 	    'r', MDB_OPT_SETBITS, TRUE, &recursive,
1109 	    'e', MDB_OPT_SETBITS, TRUE, &stats,
1110 	    NULL) != argc)
1111 		return (DCMD_USAGE);
1112 
1113 	if (!(flags & DCMD_ADDRSPEC)) {
1114 		mdb_warn("no vdev_t address given\n");
1115 		return (DCMD_ERR);
1116 	}
1117 
1118 	return (do_print_vdev(addr, flags, 0, print_queue, stats, recursive));
1119 }
1120 
1121 typedef struct mdb_spa {
1122 	uintptr_t spa_dsl_pool;
1123 	uintptr_t spa_root_vdev;
1124 } mdb_spa_t;
1125 
1126 typedef struct mdb_dsl_dir {
1127 	uintptr_t dd_phys;
1128 	uint64_t dd_used_bytes;
1129 	int64_t dd_space_towrite[TXG_SIZE];
1130 } mdb_dsl_dir_t;
1131 
1132 typedef struct mdb_dsl_dir_phys {
1133 	uint64_t dd_used_bytes;
1134 	uint64_t dd_compressed_bytes;
1135 	uint64_t dd_uncompressed_bytes;
1136 } mdb_dsl_dir_phys_t;
1137 
1138 typedef struct mdb_vdev {
1139 	uintptr_t vdev_parent;
1140 	uintptr_t vdev_ms;
1141 	uint64_t vdev_ms_count;
1142 	vdev_stat_t vdev_stat;
1143 } mdb_vdev_t;
1144 
1145 typedef struct mdb_metaslab {
1146 	space_map_t ms_allocmap[TXG_SIZE];
1147 	space_map_t ms_freemap[TXG_SIZE];
1148 	space_map_t ms_map;
1149 	uint64_t ms_usable_space;
1150 } mdb_metaslab_t;
1151 
1152 /*
1153  * ::spa_space [-b]
1154  *
1155  * Given a spa_t, print out it's on-disk space usage and in-core
1156  * estimates of future usage.  If -b is given, print space in bytes.
1157  * Otherwise print in megabytes.
1158  */
1159 /* ARGSUSED */
1160 static int
1161 spa_space(uintptr_t addr, uint_t flags, int argc, const mdb_arg_t *argv)
1162 {
1163 	mdb_spa_t spa;
1164 	uintptr_t dp_root_dir;
1165 	mdb_dsl_dir_t dd;
1166 	mdb_dsl_dir_phys_t dsp;
1167 	uint64_t children;
1168 	uintptr_t childaddr;
1169 	uintptr_t *child;
1170 	uint64_t ms_allocmap[TXG_SIZE] = {0, 0, 0, 0};
1171 	uint64_t ms_freemap[TXG_SIZE] = {0, 0, 0, 0};
1172 	uint64_t ms_map = 0;
1173 	uint64_t ms_usable_space = 0;
1174 	int i, j;
1175 	int havecompressed = TRUE;
1176 	int shift = 20;
1177 	char *suffix = "M";
1178 	int bits = FALSE;
1179 
1180 	if (mdb_getopts(argc, argv, 'b', MDB_OPT_SETBITS, TRUE, &bits, NULL) !=
1181 	    argc)
1182 		return (DCMD_USAGE);
1183 	if (!(flags & DCMD_ADDRSPEC))
1184 		return (DCMD_USAGE);
1185 
1186 	if (bits) {
1187 		shift = 0;
1188 		suffix = "";
1189 	}
1190 
1191 	if (GETMEMB(addr, struct spa, spa_dsl_pool, spa.spa_dsl_pool) ||
1192 	    GETMEMB(addr, struct spa, spa_root_vdev, spa.spa_root_vdev) ||
1193 	    GETMEMB(spa.spa_root_vdev, struct vdev, vdev_children, children) ||
1194 	    GETMEMB(spa.spa_root_vdev, struct vdev, vdev_child, childaddr) ||
1195 	    GETMEMB(spa.spa_dsl_pool, struct dsl_pool,
1196 	    dp_root_dir, dp_root_dir) ||
1197 	    GETMEMB(dp_root_dir, struct dsl_dir, dd_phys, dd.dd_phys) ||
1198 	    GETMEMB(dp_root_dir, struct dsl_dir,
1199 	    dd_used_bytes, dd.dd_used_bytes) ||
1200 	    GETMEMB(dp_root_dir, struct dsl_dir,
1201 	    dd_space_towrite, dd.dd_space_towrite) ||
1202 	    GETMEMB(dd.dd_phys, struct dsl_dir_phys,
1203 	    dd_used_bytes, dsp.dd_used_bytes)) {
1204 		return (DCMD_ERR);
1205 	}
1206 
1207 	if (GETMEMB(dd.dd_phys, struct dsl_dir_phys,
1208 	    dd_compressed_bytes, dsp.dd_compressed_bytes) ||
1209 	    GETMEMB(dd.dd_phys, struct dsl_dir_phys,
1210 	    dd_uncompressed_bytes, dsp.dd_uncompressed_bytes)) {
1211 		havecompressed = FALSE;
1212 	}
1213 
1214 	child = mdb_alloc(children * sizeof (void *), UM_SLEEP | UM_GC);
1215 	if (mdb_vread(child, children * sizeof (void *), childaddr) == -1) {
1216 		mdb_warn("failed to read root vdev children at %p", childaddr);
1217 		return (DCMD_ERR);
1218 	}
1219 
1220 	mdb_printf("dd_space_towrite = %llu%s %llu%s %llu%s %llu%s\n",
1221 	    dd.dd_space_towrite[0] >> shift, suffix,
1222 	    dd.dd_space_towrite[1] >> shift, suffix,
1223 	    dd.dd_space_towrite[2] >> shift, suffix,
1224 	    dd.dd_space_towrite[3] >> shift, suffix);
1225 	mdb_printf("dd_used_bytes = %llu%s\n",
1226 	    dd.dd_used_bytes >> shift, suffix);
1227 
1228 	mdb_printf("dd_phys.dd_used_bytes = %llu%s\n",
1229 	    dsp.dd_used_bytes >> shift, suffix);
1230 	if (havecompressed) {
1231 		mdb_printf("dd_phys.dd_compressed_bytes = %llu%s\n",
1232 		    dsp.dd_compressed_bytes >> shift, suffix);
1233 		mdb_printf("dd_phys.dd_uncompressed_bytes = %llu%s\n",
1234 		    dsp.dd_uncompressed_bytes >> shift, suffix);
1235 	}
1236 
1237 	for (i = 0; i < children; i++) {
1238 		mdb_vdev_t vd;
1239 		uintptr_t *vdev_ms;
1240 
1241 		if (GETMEMB(child[i], struct vdev,
1242 		    vdev_parent, vd.vdev_parent) ||
1243 		    GETMEMB(child[i], struct vdev,
1244 		    vdev_stat, vd.vdev_stat) ||
1245 		    GETMEMB(child[i], struct vdev, vdev_ms, vd.vdev_ms) ||
1246 		    GETMEMB(child[i], struct vdev,
1247 		    vdev_ms_count, vd.vdev_ms_count)) {
1248 			return (DCMD_ERR);
1249 		}
1250 
1251 		/*
1252 		 * If this is the root vdev, its stats are the pool-wide stats.
1253 		 */
1254 		if (vd.vdev_parent == NULL) {
1255 			mdb_printf("pool_alloc = %llu%s\n",
1256 			    vd.vdev_stat.vs_alloc >> shift, suffix);
1257 			mdb_printf("pool_space = %llu%s\n",
1258 			    vd.vdev_stat.vs_space >> shift, suffix);
1259 		}
1260 
1261 		/*
1262 		 * If this is not a top-level vdev, it doesn't have space.
1263 		 */
1264 		if (vd.vdev_parent != spa.spa_root_vdev)
1265 			continue;
1266 
1267 		vdev_ms = mdb_alloc(vd.vdev_ms_count * sizeof (void*),
1268 		    UM_SLEEP | UM_GC);
1269 		if (mdb_vread(vdev_ms, vd.vdev_ms_count * sizeof (void*),
1270 		    (uintptr_t)vd.vdev_ms) == -1) {
1271 			mdb_warn("failed to read vdev_ms at %p", vd.vdev_ms);
1272 			return (DCMD_ERR);
1273 		}
1274 
1275 		for (j = 0; j < vd.vdev_ms_count; j++) {
1276 			mdb_metaslab_t ms;
1277 
1278 			if (GETMEMB(vdev_ms[j], struct metaslab,
1279 			    ms_allocmap, ms.ms_allocmap) ||
1280 			    GETMEMB(vdev_ms[j], struct metaslab,
1281 			    ms_freemap, ms.ms_freemap) ||
1282 			    GETMEMB(vdev_ms[j], struct metaslab,
1283 			    ms_map, ms.ms_map) ||
1284 			    GETMEMB(vdev_ms[j], struct metaslab,
1285 			    ms_usable_space, ms.ms_usable_space)) {
1286 				return (DCMD_ERR);
1287 			}
1288 
1289 			ms_allocmap[0] += ms.ms_allocmap[0].sm_space;
1290 			ms_allocmap[1] += ms.ms_allocmap[1].sm_space;
1291 			ms_allocmap[2] += ms.ms_allocmap[2].sm_space;
1292 			ms_allocmap[3] += ms.ms_allocmap[3].sm_space;
1293 			ms_freemap[0] += ms.ms_freemap[0].sm_space;
1294 			ms_freemap[1] += ms.ms_freemap[1].sm_space;
1295 			ms_freemap[2] += ms.ms_freemap[2].sm_space;
1296 			ms_freemap[3] += ms.ms_freemap[3].sm_space;
1297 			ms_map += ms.ms_map.sm_space;
1298 			ms_usable_space += ms.ms_usable_space;
1299 		}
1300 	}
1301 
1302 	mdb_printf("ms_allocmap = %llu%s %llu%s %llu%s %llu%s\n",
1303 	    ms_allocmap[0] >> shift, suffix,
1304 	    ms_allocmap[1] >> shift, suffix,
1305 	    ms_allocmap[2] >> shift, suffix,
1306 	    ms_allocmap[3] >> shift, suffix);
1307 	mdb_printf("ms_freemap = %llu%s %llu%s %llu%s %llu%s\n",
1308 	    ms_freemap[0] >> shift, suffix,
1309 	    ms_freemap[1] >> shift, suffix,
1310 	    ms_freemap[2] >> shift, suffix,
1311 	    ms_freemap[3] >> shift, suffix);
1312 	mdb_printf("ms_map = %llu%s\n", ms_map >> shift, suffix);
1313 	mdb_printf("ms_usable_space = %llu%s\n",
1314 	    ms_usable_space >> shift, suffix);
1315 
1316 	return (DCMD_OK);
1317 }
1318 
1319 /*
1320  * ::spa_verify
1321  *
1322  * Given a spa_t, verify that that the pool is self-consistent.
1323  * Currently, it only checks to make sure that the vdev tree exists.
1324  */
1325 /* ARGSUSED */
1326 static int
1327 spa_verify(uintptr_t addr, uint_t flags, int argc, const mdb_arg_t *argv)
1328 {
1329 	spa_t spa;
1330 
1331 	if (argc != 0 || !(flags & DCMD_ADDRSPEC))
1332 		return (DCMD_USAGE);
1333 
1334 	if (mdb_vread(&spa, sizeof (spa), addr) == -1) {
1335 		mdb_warn("failed to read spa_t at %p", addr);
1336 		return (DCMD_ERR);
1337 	}
1338 
1339 	if (spa.spa_root_vdev == NULL) {
1340 		mdb_printf("no vdev tree present\n");
1341 		return (DCMD_OK);
1342 	}
1343 
1344 	return (DCMD_OK);
1345 }
1346 
1347 /*
1348  * ::spa_vdevs
1349  *
1350  * 	-e	Include error stats
1351  *
1352  * Print out a summarized list of vdevs for the given spa_t.
1353  * This is accomplished by invoking "::vdev -re" on the root vdev.
1354  */
1355 /* ARGSUSED */
1356 static int
1357 spa_vdevs(uintptr_t addr, uint_t flags, int argc, const mdb_arg_t *argv)
1358 {
1359 	spa_t spa;
1360 	mdb_arg_t v;
1361 	int errors = FALSE;
1362 
1363 	if (mdb_getopts(argc, argv,
1364 	    'e', MDB_OPT_SETBITS, TRUE, &errors,
1365 	    NULL) != argc)
1366 		return (DCMD_USAGE);
1367 
1368 	if (!(flags & DCMD_ADDRSPEC))
1369 		return (DCMD_USAGE);
1370 
1371 	if (mdb_vread(&spa, sizeof (spa), addr) == -1) {
1372 		mdb_warn("failed to read spa_t at %p", addr);
1373 		return (DCMD_ERR);
1374 	}
1375 
1376 	/*
1377 	 * Unitialized spa_t structures can have a NULL root vdev.
1378 	 */
1379 	if (spa.spa_root_vdev == NULL) {
1380 		mdb_printf("no associated vdevs\n");
1381 		return (DCMD_OK);
1382 	}
1383 
1384 	v.a_type = MDB_TYPE_STRING;
1385 	v.a_un.a_str = errors ? "-re" : "-r";
1386 
1387 	return (mdb_call_dcmd("vdev", (uintptr_t)spa.spa_root_vdev,
1388 	    flags, 1, &v));
1389 }
1390 
1391 typedef struct txg_list_walk_data {
1392 	uintptr_t lw_head[TXG_SIZE];
1393 	int	lw_txgoff;
1394 	int	lw_maxoff;
1395 	size_t	lw_offset;
1396 	void	*lw_obj;
1397 } txg_list_walk_data_t;
1398 
1399 static int
1400 txg_list_walk_init_common(mdb_walk_state_t *wsp, int txg, int maxoff)
1401 {
1402 	txg_list_walk_data_t *lwd;
1403 	txg_list_t list;
1404 	int i;
1405 
1406 	lwd = mdb_alloc(sizeof (txg_list_walk_data_t), UM_SLEEP | UM_GC);
1407 	if (mdb_vread(&list, sizeof (txg_list_t), wsp->walk_addr) == -1) {
1408 		mdb_warn("failed to read txg_list_t at %#lx", wsp->walk_addr);
1409 		return (WALK_ERR);
1410 	}
1411 
1412 	for (i = 0; i < TXG_SIZE; i++)
1413 		lwd->lw_head[i] = (uintptr_t)list.tl_head[i];
1414 	lwd->lw_offset = list.tl_offset;
1415 	lwd->lw_obj = mdb_alloc(lwd->lw_offset + sizeof (txg_node_t),
1416 	    UM_SLEEP | UM_GC);
1417 	lwd->lw_txgoff = txg;
1418 	lwd->lw_maxoff = maxoff;
1419 
1420 	wsp->walk_addr = lwd->lw_head[lwd->lw_txgoff];
1421 	wsp->walk_data = lwd;
1422 
1423 	return (WALK_NEXT);
1424 }
1425 
1426 static int
1427 txg_list_walk_init(mdb_walk_state_t *wsp)
1428 {
1429 	return (txg_list_walk_init_common(wsp, 0, TXG_SIZE-1));
1430 }
1431 
1432 static int
1433 txg_list0_walk_init(mdb_walk_state_t *wsp)
1434 {
1435 	return (txg_list_walk_init_common(wsp, 0, 0));
1436 }
1437 
1438 static int
1439 txg_list1_walk_init(mdb_walk_state_t *wsp)
1440 {
1441 	return (txg_list_walk_init_common(wsp, 1, 1));
1442 }
1443 
1444 static int
1445 txg_list2_walk_init(mdb_walk_state_t *wsp)
1446 {
1447 	return (txg_list_walk_init_common(wsp, 2, 2));
1448 }
1449 
1450 static int
1451 txg_list3_walk_init(mdb_walk_state_t *wsp)
1452 {
1453 	return (txg_list_walk_init_common(wsp, 3, 3));
1454 }
1455 
1456 static int
1457 txg_list_walk_step(mdb_walk_state_t *wsp)
1458 {
1459 	txg_list_walk_data_t *lwd = wsp->walk_data;
1460 	uintptr_t addr;
1461 	txg_node_t *node;
1462 	int status;
1463 
1464 	while (wsp->walk_addr == NULL && lwd->lw_txgoff < lwd->lw_maxoff) {
1465 		lwd->lw_txgoff++;
1466 		wsp->walk_addr = lwd->lw_head[lwd->lw_txgoff];
1467 	}
1468 
1469 	if (wsp->walk_addr == NULL)
1470 		return (WALK_DONE);
1471 
1472 	addr = wsp->walk_addr - lwd->lw_offset;
1473 
1474 	if (mdb_vread(lwd->lw_obj,
1475 	    lwd->lw_offset + sizeof (txg_node_t), addr) == -1) {
1476 		mdb_warn("failed to read list element at %#lx", addr);
1477 		return (WALK_ERR);
1478 	}
1479 
1480 	status = wsp->walk_callback(addr, lwd->lw_obj, wsp->walk_cbdata);
1481 	node = (txg_node_t *)((uintptr_t)lwd->lw_obj + lwd->lw_offset);
1482 	wsp->walk_addr = (uintptr_t)node->tn_next[lwd->lw_txgoff];
1483 
1484 	return (status);
1485 }
1486 
1487 /* ARGSUSED */
1488 static void
1489 txg_list_walk_fini(mdb_walk_state_t *wsp)
1490 {
1491 }
1492 
1493 /*
1494  * ::walk spa
1495  *
1496  * Walk all named spa_t structures in the namespace.  This is nothing more than
1497  * a layered avl walk.
1498  */
1499 static int
1500 spa_walk_init(mdb_walk_state_t *wsp)
1501 {
1502 	GElf_Sym sym;
1503 
1504 	if (wsp->walk_addr != NULL) {
1505 		mdb_warn("spa walk only supports global walks\n");
1506 		return (WALK_ERR);
1507 	}
1508 
1509 	if (mdb_lookup_by_obj(ZFS_OBJ_NAME, "spa_namespace_avl", &sym) == -1) {
1510 		mdb_warn("failed to find symbol 'spa_namespace_avl'");
1511 		return (WALK_ERR);
1512 	}
1513 
1514 	wsp->walk_addr = (uintptr_t)sym.st_value;
1515 
1516 	if (mdb_layered_walk("avl", wsp) == -1) {
1517 		mdb_warn("failed to walk 'avl'\n");
1518 		return (WALK_ERR);
1519 	}
1520 
1521 	return (WALK_NEXT);
1522 }
1523 
1524 static int
1525 spa_walk_step(mdb_walk_state_t *wsp)
1526 {
1527 	spa_t	spa;
1528 
1529 	if (mdb_vread(&spa, sizeof (spa), wsp->walk_addr) == -1) {
1530 		mdb_warn("failed to read spa_t at %p", wsp->walk_addr);
1531 		return (WALK_ERR);
1532 	}
1533 
1534 	return (wsp->walk_callback(wsp->walk_addr, &spa, wsp->walk_cbdata));
1535 }
1536 
1537 /*
1538  * MDB module linkage information:
1539  *
1540  * We declare a list of structures describing our dcmds, and a function
1541  * named _mdb_init to return a pointer to our module information.
1542  */
1543 
1544 static const mdb_dcmd_t dcmds[] = {
1545 	{ "blkptr", ":", "print blkptr_t", blkptr },
1546 	{ "dbuf", ":", "print dmu_buf_impl_t", dbuf },
1547 	{ "dbuf_stats", ":", "dbuf stats", dbuf_stats },
1548 	{ "dbufs",
1549 	"\t[-O objset_t*] [-n objset_name | \"mos\"] [-o object | \"mdn\"] \n"
1550 	"\t[-l level] [-b blkid | \"bonus\"]",
1551 	"find dmu_buf_impl_t's that meet criterion", dbufs },
1552 	{ "abuf_find", "dva_word[0] dva_word[1]",
1553 	"find arc_buf_hdr_t of a specified DVA",
1554 	abuf_find },
1555 	{ "spa", "?[-cv]", "spa_t summary", spa_print },
1556 	{ "spa_config", ":", "print spa_t configuration", spa_print_config },
1557 	{ "spa_verify", ":", "verify spa_t consistency", spa_verify },
1558 	{ "spa_space", ":[-b]", "print spa_t on-disk space usage", spa_space },
1559 	{ "spa_vdevs", ":", "given a spa_t, print vdev summary", spa_vdevs },
1560 	{ "vdev", ":[-qre]", "vdev_t summary", vdev_print },
1561 	{ "zio_pipeline", ":", "decode a zio pipeline", zio_pipeline },
1562 	{ NULL }
1563 };
1564 
1565 static const mdb_walker_t walkers[] = {
1566 	/*
1567 	 * In userland, there is no generic provider of list_t walkers, so we
1568 	 * need to add it.
1569 	 */
1570 #ifndef _KERNEL
1571 	{ LIST_WALK_NAME, LIST_WALK_DESC,
1572 		list_walk_init, list_walk_step, list_walk_fini },
1573 #endif
1574 	{ "dbufs", "walk cached ZFS dbufs",
1575 		dbuf_walk_init, dbuf_walk_step, dbuf_walk_fini },
1576 	{ "zms_freelist", "walk ZFS metaslab freelist",
1577 		freelist_walk_init, freelist_walk_step, freelist_walk_fini },
1578 	{ "txg_list", "given any txg_list_t *, walk all entries in all txgs",
1579 		txg_list_walk_init, txg_list_walk_step, txg_list_walk_fini },
1580 	{ "txg_list0", "given any txg_list_t *, walk all entries in txg 0",
1581 		txg_list0_walk_init, txg_list_walk_step, txg_list_walk_fini },
1582 	{ "txg_list1", "given any txg_list_t *, walk all entries in txg 1",
1583 		txg_list1_walk_init, txg_list_walk_step, txg_list_walk_fini },
1584 	{ "txg_list2", "given any txg_list_t *, walk all entries in txg 2",
1585 		txg_list2_walk_init, txg_list_walk_step, txg_list_walk_fini },
1586 	{ "txg_list3", "given any txg_list_t *, walk all entries in txg 3",
1587 		txg_list3_walk_init, txg_list_walk_step, txg_list_walk_fini },
1588 	{ "spa", "walk all spa_t entries in the namespace",
1589 		spa_walk_init, spa_walk_step, NULL },
1590 	{ NULL }
1591 };
1592 
1593 static const mdb_modinfo_t modinfo = {
1594 	MDB_API_VERSION, dcmds, walkers
1595 };
1596 
1597 const mdb_modinfo_t *
1598 _mdb_init(void)
1599 {
1600 	return (&modinfo);
1601 }
1602