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