xref: /titanic_52/usr/src/cmd/mdb/common/modules/zfs/zfs.c (revision 3472f5dc5f31b4385e87145cddb24ae3c94de14b)
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 /*
780  * ::spa
781  *
782  * 	-c	Print configuration information as well
783  * 	-v	Print vdev state
784  * 	-e	Print vdev error stats
785  *
786  * Print a summarized spa_t.  When given no arguments, prints out a table of all
787  * active pools on the system.
788  */
789 /* ARGSUSED */
790 static int
791 spa_print(uintptr_t addr, uint_t flags, int argc, const mdb_arg_t *argv)
792 {
793 	spa_t spa;
794 	char poolname[MAXNAMELEN];
795 	const char *statetab[] = { "ACTIVE", "EXPORTED", "DESTROYED",
796 		"UNINIT", "UNAVAIL" };
797 	const char *state;
798 	int config = FALSE;
799 	int vdevs = FALSE;
800 	int errors = FALSE;
801 
802 	if (mdb_getopts(argc, argv,
803 	    'c', MDB_OPT_SETBITS, TRUE, &config,
804 	    'v', MDB_OPT_SETBITS, TRUE, &vdevs,
805 	    'e', MDB_OPT_SETBITS, TRUE, &errors,
806 	    NULL) != argc)
807 		return (DCMD_USAGE);
808 
809 	if (!(flags & DCMD_ADDRSPEC)) {
810 		if (mdb_walk_dcmd("spa", "spa", argc, argv) == -1) {
811 			mdb_warn("can't walk spa");
812 			return (DCMD_ERR);
813 		}
814 
815 		return (DCMD_OK);
816 	}
817 
818 	if (flags & DCMD_PIPE_OUT) {
819 		mdb_printf("%#lr\n", addr);
820 		return (DCMD_OK);
821 	}
822 
823 	if (DCMD_HDRSPEC(flags))
824 		mdb_printf("%<u>%-?s %9s %-*s%</u>\n", "ADDR", "STATE",
825 		    sizeof (uintptr_t) == 4 ? 60 : 52, "NAME");
826 
827 	if (mdb_vread(&spa, sizeof (spa), addr) == -1) {
828 		mdb_warn("failed to read spa_t at %p", addr);
829 		return (DCMD_ERR);
830 	}
831 
832 	if (mdb_readstr(poolname, sizeof (poolname), (uintptr_t)spa.spa_name)
833 	    == -1) {
834 		mdb_warn("failed to read pool name at %p", spa.spa_name);
835 		return (DCMD_ERR);
836 	}
837 
838 	if (spa.spa_state < 0 || spa.spa_state > POOL_STATE_UNAVAIL)
839 		state = "UNKNOWN";
840 	else
841 		state = statetab[spa.spa_state];
842 
843 	mdb_printf("%0?p %9s %s\n", addr, state, poolname);
844 
845 	if (config) {
846 		mdb_printf("\n");
847 		mdb_inc_indent(4);
848 		if (mdb_call_dcmd("spa_config", addr, flags, 0,
849 		    NULL) != DCMD_OK)
850 			return (DCMD_ERR);
851 		mdb_dec_indent(4);
852 	}
853 
854 	if (vdevs || errors) {
855 		mdb_arg_t v;
856 
857 		v.a_type = MDB_TYPE_STRING;
858 		v.a_un.a_str = "-e";
859 
860 		mdb_printf("\n");
861 		mdb_inc_indent(4);
862 		if (mdb_call_dcmd("spa_vdevs", addr, flags, errors ? 1 : 0,
863 		    &v) != DCMD_OK)
864 			return (DCMD_ERR);
865 		mdb_dec_indent(4);
866 	}
867 
868 	return (DCMD_OK);
869 }
870 
871 /*
872  * ::spa_config
873  *
874  * Given a spa_t, print the configuration information stored in spa_config.
875  * Since it's just an nvlist, format it as an indented list of name=value pairs.
876  * We simply read the value of spa_config and pass off to ::nvlist.
877  */
878 /* ARGSUSED */
879 static int
880 spa_print_config(uintptr_t addr, uint_t flags, int argc, const mdb_arg_t *argv)
881 {
882 	spa_t spa;
883 
884 	if (argc != 0 || !(flags & DCMD_ADDRSPEC))
885 		return (DCMD_USAGE);
886 
887 	if (mdb_vread(&spa, sizeof (spa), addr) == -1) {
888 		mdb_warn("failed to read spa_t at %p", addr);
889 		return (DCMD_ERR);
890 	}
891 
892 	if (spa.spa_config == NULL) {
893 		mdb_printf("(none)\n");
894 		return (DCMD_OK);
895 	}
896 
897 	return (mdb_call_dcmd("nvlist", (uintptr_t)spa.spa_config, flags,
898 	    0, NULL));
899 }
900 
901 void
902 vdev_help(void)
903 {
904 	mdb_printf("[vdev_t*]::vdev [-er]\n"
905 		"\t-> -e display vdev stats\n"
906 		"\t-> -r recursive (visit all children)\n");
907 }
908 
909 /*
910  * ::vdev
911  *
912  * Print out a summarized vdev_t, in the following form:
913  *
914  * ADDR             STATE	AUX            DESC
915  * fffffffbcde23df0 HEALTHY	-              /dev/dsk/c0t0d0
916  *
917  * If '-r' is specified, recursively visit all children.
918  *
919  * With '-e', the statistics associated with the vdev are printed as well.
920  */
921 static int
922 do_print_vdev(uintptr_t addr, int flags, int depth, int stats,
923     int recursive)
924 {
925 	vdev_t vdev;
926 	char desc[MAXNAMELEN];
927 	int c, children;
928 	uintptr_t *child;
929 	const char *state, *aux;
930 
931 	if (mdb_vread(&vdev, sizeof (vdev), (uintptr_t)addr) == -1) {
932 		mdb_warn("failed to read vdev_t at %p\n", (uintptr_t)addr);
933 		return (DCMD_ERR);
934 	}
935 
936 	if (flags & DCMD_PIPE_OUT) {
937 		mdb_printf("%#lr", addr);
938 	} else {
939 		if (vdev.vdev_path != NULL) {
940 			if (mdb_readstr(desc, sizeof (desc),
941 			    (uintptr_t)vdev.vdev_path) == -1) {
942 				mdb_warn("failed to read vdev_path at %p\n",
943 				    vdev.vdev_path);
944 				return (DCMD_ERR);
945 			}
946 		} else if (vdev.vdev_ops != NULL) {
947 			vdev_ops_t ops;
948 			if (mdb_vread(&ops, sizeof (ops),
949 			    (uintptr_t)vdev.vdev_ops) == -1) {
950 				mdb_warn("failed to read vdev_ops at %p\n",
951 				    vdev.vdev_ops);
952 				return (DCMD_ERR);
953 			}
954 			(void) strcpy(desc, ops.vdev_op_type);
955 		} else {
956 			(void) strcpy(desc, "<unknown>");
957 		}
958 
959 		if (depth == 0 && DCMD_HDRSPEC(flags))
960 			mdb_printf("%<u>%-?s %-9s %-12s %-*s%</u>\n",
961 			    "ADDR", "STATE", "AUX",
962 			    sizeof (uintptr_t) == 4 ? 43 : 35,
963 			    "DESCRIPTION");
964 
965 		mdb_printf("%0?p ", addr);
966 
967 		switch (vdev.vdev_state) {
968 		case VDEV_STATE_CLOSED:
969 		    state = "CLOSED";
970 		    break;
971 		case VDEV_STATE_OFFLINE:
972 		    state = "OFFLINE";
973 		    break;
974 		case VDEV_STATE_CANT_OPEN:
975 		    state = "CANT_OPEN";
976 		    break;
977 		case VDEV_STATE_DEGRADED:
978 		    state = "DEGRADED";
979 		    break;
980 		case VDEV_STATE_HEALTHY:
981 		    state = "HEALTHY";
982 		    break;
983 		default:
984 		    state = "UNKNOWN";
985 		    break;
986 		}
987 
988 		switch (vdev.vdev_stat.vs_aux) {
989 		case VDEV_AUX_NONE:
990 			aux = "-";
991 			break;
992 		case VDEV_AUX_OPEN_FAILED:
993 			aux = "OPEN_FAILED";
994 			break;
995 		case VDEV_AUX_CORRUPT_DATA:
996 			aux = "CORRUPT_DATA";
997 			break;
998 		case VDEV_AUX_NO_REPLICAS:
999 			aux = "NO_REPLICAS";
1000 			break;
1001 		case VDEV_AUX_BAD_GUID_SUM:
1002 			aux = "BAD_GUID_SUM";
1003 			break;
1004 		case VDEV_AUX_TOO_SMALL:
1005 			aux = "TOO_SMALL";
1006 			break;
1007 		case VDEV_AUX_BAD_LABEL:
1008 			aux = "BAD_LABEL";
1009 			break;
1010 		default:
1011 			aux = "UNKNOWN";
1012 			break;
1013 		}
1014 
1015 		mdb_printf("%-9s %-12s %*s%s\n", state, aux, depth, "", desc);
1016 
1017 		if (stats) {
1018 			vdev_stat_t *vs = &vdev.vdev_stat;
1019 			int i;
1020 
1021 			mdb_inc_indent(4);
1022 			mdb_printf("\n");
1023 			mdb_printf("%<u>       %12s %12s %12s %12s "
1024 			    "%12s%</u>\n", "READ", "WRITE", "FREE", "CLAIM",
1025 			    "IOCTL");
1026 			mdb_printf("OPS     ");
1027 			for (i = 1; i < ZIO_TYPES; i++)
1028 				mdb_printf("%11#llx%s", vs->vs_ops[i],
1029 				    i == ZIO_TYPES - 1 ? "" : "  ");
1030 			mdb_printf("\n");
1031 			mdb_printf("BYTES   ");
1032 			for (i = 1; i < ZIO_TYPES; i++)
1033 				mdb_printf("%11#llx%s", vs->vs_bytes[i],
1034 				    i == ZIO_TYPES - 1 ? "" : "  ");
1035 
1036 
1037 			mdb_printf("\n");
1038 			mdb_printf("EREAD    %10#llx\n", vs->vs_read_errors);
1039 			mdb_printf("EWRITE   %10#llx\n", vs->vs_write_errors);
1040 			mdb_printf("ECKSUM   %10#llx\n",
1041 			    vs->vs_checksum_errors);
1042 			mdb_dec_indent(4);
1043 		}
1044 
1045 		if (stats)
1046 			mdb_printf("\n");
1047 	}
1048 
1049 	children = vdev.vdev_children;
1050 
1051 	if (children == 0 || !recursive)
1052 		return (DCMD_OK);
1053 
1054 	child = mdb_alloc(children * sizeof (void *), UM_SLEEP | UM_GC);
1055 	if (mdb_vread(child, children * sizeof (void *),
1056 	    (uintptr_t)vdev.vdev_child) == -1) {
1057 		mdb_warn("failed to read vdev children at %p", vdev.vdev_child);
1058 		return (DCMD_ERR);
1059 	}
1060 
1061 	for (c = 0; c < children; c++) {
1062 		if (do_print_vdev(child[c], flags, depth + 2, stats,
1063 		    recursive))
1064 			return (DCMD_ERR);
1065 	}
1066 
1067 	return (DCMD_OK);
1068 }
1069 
1070 static int
1071 vdev_print(uintptr_t addr, uint_t flags, int argc, const mdb_arg_t *argv)
1072 {
1073 	int recursive = FALSE;
1074 	int stats = FALSE;
1075 
1076 	if (mdb_getopts(argc, argv,
1077 	    'r', MDB_OPT_SETBITS, TRUE, &recursive,
1078 	    'e', MDB_OPT_SETBITS, TRUE, &stats,
1079 	    NULL) != argc)
1080 		return (DCMD_USAGE);
1081 
1082 	if (!(flags & DCMD_ADDRSPEC)) {
1083 		mdb_warn("no vdev_t address given\n");
1084 		return (DCMD_ERR);
1085 	}
1086 
1087 	return (do_print_vdev(addr, flags, 0, stats, recursive));
1088 }
1089 
1090 typedef struct metaslab_walk_data {
1091 	uint64_t mw_numvdevs;
1092 	uintptr_t *mw_vdevs;
1093 	int mw_curvdev;
1094 	uint64_t mw_nummss;
1095 	uintptr_t *mw_mss;
1096 	int mw_curms;
1097 } metaslab_walk_data_t;
1098 
1099 static int
1100 metaslab_walk_step(mdb_walk_state_t *wsp)
1101 {
1102 	metaslab_walk_data_t *mw = wsp->walk_data;
1103 	metaslab_t ms;
1104 	uintptr_t msp;
1105 
1106 	if (mw->mw_curvdev >= mw->mw_numvdevs)
1107 		return (WALK_DONE);
1108 
1109 	if (mw->mw_mss == NULL) {
1110 		uintptr_t mssp;
1111 		uintptr_t vdevp;
1112 
1113 		ASSERT(mw->mw_curms == 0);
1114 		ASSERT(mw->mw_nummss == 0);
1115 
1116 		vdevp = mw->mw_vdevs[mw->mw_curvdev];
1117 		if (GETMEMB(vdevp, struct vdev, vdev_ms, mssp) ||
1118 		    GETMEMB(vdevp, struct vdev, vdev_ms_count, mw->mw_nummss)) {
1119 			return (WALK_ERR);
1120 		}
1121 
1122 		mw->mw_mss = mdb_alloc(mw->mw_nummss * sizeof (void*),
1123 		    UM_SLEEP | UM_GC);
1124 		if (mdb_vread(mw->mw_mss, mw->mw_nummss * sizeof (void*),
1125 		    mssp) == -1) {
1126 			mdb_warn("failed to read vdev_ms at %p", mssp);
1127 			return (WALK_ERR);
1128 		}
1129 	}
1130 
1131 	if (mw->mw_curms >= mw->mw_nummss) {
1132 		mw->mw_mss = NULL;
1133 		mw->mw_curms = 0;
1134 		mw->mw_nummss = 0;
1135 		mw->mw_curvdev++;
1136 		return (WALK_NEXT);
1137 	}
1138 
1139 	msp = mw->mw_mss[mw->mw_curms];
1140 	if (mdb_vread(&ms, sizeof (metaslab_t), msp) == -1) {
1141 		mdb_warn("failed to read metaslab_t at %p", msp);
1142 		return (WALK_ERR);
1143 	}
1144 
1145 	mw->mw_curms++;
1146 
1147 	return (wsp->walk_callback(msp, &ms, wsp->walk_cbdata));
1148 }
1149 
1150 /* ARGSUSED */
1151 static int
1152 metaslab_walk_init(mdb_walk_state_t *wsp)
1153 {
1154 	metaslab_walk_data_t *mw;
1155 	uintptr_t root_vdevp;
1156 	uintptr_t childp;
1157 
1158 	if (wsp->walk_addr == NULL) {
1159 		mdb_warn("must supply address of spa_t\n");
1160 		return (WALK_ERR);
1161 	}
1162 
1163 	mw = mdb_zalloc(sizeof (metaslab_walk_data_t), UM_SLEEP | UM_GC);
1164 
1165 	if (GETMEMB(wsp->walk_addr, struct spa, spa_root_vdev, root_vdevp) ||
1166 	    GETMEMB(root_vdevp, struct vdev, vdev_children, mw->mw_numvdevs) ||
1167 	    GETMEMB(root_vdevp, struct vdev, vdev_child, childp)) {
1168 		return (DCMD_ERR);
1169 	}
1170 
1171 	mw->mw_vdevs = mdb_alloc(mw->mw_numvdevs * sizeof (void *),
1172 	    UM_SLEEP | UM_GC);
1173 	if (mdb_vread(mw->mw_vdevs, mw->mw_numvdevs * sizeof (void *),
1174 	    childp) == -1) {
1175 		mdb_warn("failed to read root vdev children at %p", childp);
1176 		return (DCMD_ERR);
1177 	}
1178 
1179 	wsp->walk_data = mw;
1180 
1181 	return (WALK_NEXT);
1182 }
1183 
1184 typedef struct mdb_spa {
1185 	uintptr_t spa_dsl_pool;
1186 	uintptr_t spa_root_vdev;
1187 } mdb_spa_t;
1188 
1189 typedef struct mdb_dsl_dir {
1190 	uintptr_t dd_phys;
1191 	uint64_t dd_used_bytes;
1192 	int64_t dd_space_towrite[TXG_SIZE];
1193 } mdb_dsl_dir_t;
1194 
1195 typedef struct mdb_dsl_dir_phys {
1196 	uint64_t dd_used_bytes;
1197 	uint64_t dd_compressed_bytes;
1198 	uint64_t dd_uncompressed_bytes;
1199 } mdb_dsl_dir_phys_t;
1200 
1201 typedef struct mdb_vdev {
1202 	uintptr_t vdev_parent;
1203 	uintptr_t vdev_ms;
1204 	uint64_t vdev_ms_count;
1205 	vdev_stat_t vdev_stat;
1206 } mdb_vdev_t;
1207 
1208 typedef struct mdb_metaslab {
1209 	space_map_t ms_allocmap[TXG_SIZE];
1210 	space_map_t ms_freemap[TXG_SIZE];
1211 	space_map_t ms_map;
1212 	space_map_obj_t ms_smo;
1213 	space_map_obj_t ms_smo_syncing;
1214 } mdb_metaslab_t;
1215 
1216 typedef struct space_data {
1217 	uint64_t ms_allocmap[TXG_SIZE];
1218 	uint64_t ms_freemap[TXG_SIZE];
1219 	uint64_t ms_map;
1220 	uint64_t avail;
1221 	uint64_t nowavail;
1222 } space_data_t;
1223 
1224 /* ARGSUSED */
1225 static int
1226 space_cb(uintptr_t addr, const void *unknown, void *arg)
1227 {
1228 	space_data_t *sd = arg;
1229 	mdb_metaslab_t ms;
1230 
1231 	if (GETMEMB(addr, struct metaslab, ms_allocmap, ms.ms_allocmap) ||
1232 	    GETMEMB(addr, struct metaslab, ms_freemap, ms.ms_freemap) ||
1233 	    GETMEMB(addr, struct metaslab, ms_map, ms.ms_map) ||
1234 	    GETMEMB(addr, struct metaslab, ms_smo, ms.ms_smo) ||
1235 	    GETMEMB(addr, struct metaslab, ms_smo_syncing, ms.ms_smo_syncing)) {
1236 		return (WALK_ERR);
1237 	}
1238 
1239 	sd->ms_allocmap[0] += ms.ms_allocmap[0].sm_space;
1240 	sd->ms_allocmap[1] += ms.ms_allocmap[1].sm_space;
1241 	sd->ms_allocmap[2] += ms.ms_allocmap[2].sm_space;
1242 	sd->ms_allocmap[3] += ms.ms_allocmap[3].sm_space;
1243 	sd->ms_freemap[0] += ms.ms_freemap[0].sm_space;
1244 	sd->ms_freemap[1] += ms.ms_freemap[1].sm_space;
1245 	sd->ms_freemap[2] += ms.ms_freemap[2].sm_space;
1246 	sd->ms_freemap[3] += ms.ms_freemap[3].sm_space;
1247 	sd->ms_map += ms.ms_map.sm_space;
1248 	sd->avail += ms.ms_map.sm_size - ms.ms_smo.smo_alloc;
1249 	sd->nowavail += ms.ms_map.sm_size - ms.ms_smo_syncing.smo_alloc;
1250 
1251 	return (WALK_NEXT);
1252 }
1253 
1254 /*
1255  * ::spa_space [-b]
1256  *
1257  * Given a spa_t, print out it's on-disk space usage and in-core
1258  * estimates of future usage.  If -b is given, print space in bytes.
1259  * Otherwise print in megabytes.
1260  */
1261 /* ARGSUSED */
1262 static int
1263 spa_space(uintptr_t addr, uint_t flags, int argc, const mdb_arg_t *argv)
1264 {
1265 	mdb_spa_t spa;
1266 	uintptr_t dp_root_dir;
1267 	mdb_dsl_dir_t dd;
1268 	mdb_dsl_dir_phys_t dsp;
1269 	uint64_t children;
1270 	uintptr_t childaddr;
1271 	space_data_t sd;
1272 	int shift = 20;
1273 	char *suffix = "M";
1274 	int bits = FALSE;
1275 
1276 	if (mdb_getopts(argc, argv, 'b', MDB_OPT_SETBITS, TRUE, &bits, NULL) !=
1277 	    argc)
1278 		return (DCMD_USAGE);
1279 	if (!(flags & DCMD_ADDRSPEC))
1280 		return (DCMD_USAGE);
1281 
1282 	if (bits) {
1283 		shift = 0;
1284 		suffix = "";
1285 	}
1286 
1287 	if (GETMEMB(addr, struct spa, spa_dsl_pool, spa.spa_dsl_pool) ||
1288 	    GETMEMB(addr, struct spa, spa_root_vdev, spa.spa_root_vdev) ||
1289 	    GETMEMB(spa.spa_root_vdev, struct vdev, vdev_children, children) ||
1290 	    GETMEMB(spa.spa_root_vdev, struct vdev, vdev_child, childaddr) ||
1291 	    GETMEMB(spa.spa_dsl_pool, struct dsl_pool,
1292 	    dp_root_dir, dp_root_dir) ||
1293 	    GETMEMB(dp_root_dir, struct dsl_dir, dd_phys, dd.dd_phys) ||
1294 	    GETMEMB(dp_root_dir, struct dsl_dir,
1295 	    dd_used_bytes, dd.dd_used_bytes) ||
1296 	    GETMEMB(dp_root_dir, struct dsl_dir,
1297 	    dd_space_towrite, dd.dd_space_towrite) ||
1298 	    GETMEMB(dd.dd_phys, struct dsl_dir_phys,
1299 	    dd_used_bytes, dsp.dd_used_bytes) ||
1300 	    GETMEMB(dd.dd_phys, struct dsl_dir_phys,
1301 	    dd_compressed_bytes, dsp.dd_compressed_bytes) ||
1302 	    GETMEMB(dd.dd_phys, struct dsl_dir_phys,
1303 	    dd_uncompressed_bytes, dsp.dd_uncompressed_bytes)) {
1304 		return (DCMD_ERR);
1305 	}
1306 
1307 	mdb_printf("dd_space_towrite = %llu%s %llu%s %llu%s %llu%s\n",
1308 	    dd.dd_space_towrite[0] >> shift, suffix,
1309 	    dd.dd_space_towrite[1] >> shift, suffix,
1310 	    dd.dd_space_towrite[2] >> shift, suffix,
1311 	    dd.dd_space_towrite[3] >> shift, suffix);
1312 	mdb_printf("dd_used_bytes = %llu%s\n",
1313 	    dd.dd_used_bytes >> shift, suffix);
1314 
1315 	mdb_printf("dd_phys.dd_used_bytes = %llu%s\n",
1316 	    dsp.dd_used_bytes >> shift, suffix);
1317 	mdb_printf("dd_phys.dd_compressed_bytes = %llu%s\n",
1318 	    dsp.dd_compressed_bytes >> shift, suffix);
1319 	mdb_printf("dd_phys.dd_uncompressed_bytes = %llu%s\n",
1320 	    dsp.dd_uncompressed_bytes >> shift, suffix);
1321 
1322 	bzero(&sd, sizeof (sd));
1323 	if (mdb_pwalk("metaslab", space_cb, &sd, addr) != 0) {
1324 		mdb_warn("can't walk metaslabs");
1325 		return (DCMD_ERR);
1326 	}
1327 
1328 	mdb_printf("ms_allocmap = %llu%s %llu%s %llu%s %llu%s\n",
1329 	    sd.ms_allocmap[0] >> shift, suffix,
1330 	    sd.ms_allocmap[1] >> shift, suffix,
1331 	    sd.ms_allocmap[2] >> shift, suffix,
1332 	    sd.ms_allocmap[3] >> shift, suffix);
1333 	mdb_printf("ms_freemap = %llu%s %llu%s %llu%s %llu%s\n",
1334 	    sd.ms_freemap[0] >> shift, suffix,
1335 	    sd.ms_freemap[1] >> shift, suffix,
1336 	    sd.ms_freemap[2] >> shift, suffix,
1337 	    sd.ms_freemap[3] >> shift, suffix);
1338 	mdb_printf("ms_map = %llu%s\n", sd.ms_map >> shift, suffix);
1339 	mdb_printf("last synced avail = %llu%s\n", sd.avail >> shift, suffix);
1340 	mdb_printf("current syncing avail = %llu%s\n",
1341 	    sd.nowavail >> shift, suffix);
1342 
1343 	return (DCMD_OK);
1344 }
1345 
1346 /*
1347  * ::spa_verify
1348  *
1349  * Given a spa_t, verify that that the pool is self-consistent.
1350  * Currently, it only checks to make sure that the vdev tree exists.
1351  */
1352 /* ARGSUSED */
1353 static int
1354 spa_verify(uintptr_t addr, uint_t flags, int argc, const mdb_arg_t *argv)
1355 {
1356 	spa_t spa;
1357 
1358 	if (argc != 0 || !(flags & DCMD_ADDRSPEC))
1359 		return (DCMD_USAGE);
1360 
1361 	if (mdb_vread(&spa, sizeof (spa), addr) == -1) {
1362 		mdb_warn("failed to read spa_t at %p", addr);
1363 		return (DCMD_ERR);
1364 	}
1365 
1366 	if (spa.spa_root_vdev == NULL) {
1367 		mdb_printf("no vdev tree present\n");
1368 		return (DCMD_OK);
1369 	}
1370 
1371 	return (DCMD_OK);
1372 }
1373 
1374 /*
1375  * ::spa_vdevs
1376  *
1377  * 	-e	Include error stats
1378  *
1379  * Print out a summarized list of vdevs for the given spa_t.
1380  * This is accomplished by invoking "::vdev -re" on the root vdev.
1381  */
1382 /* ARGSUSED */
1383 static int
1384 spa_vdevs(uintptr_t addr, uint_t flags, int argc, const mdb_arg_t *argv)
1385 {
1386 	spa_t spa;
1387 	mdb_arg_t v;
1388 	int errors = FALSE;
1389 
1390 	if (mdb_getopts(argc, argv,
1391 	    'e', MDB_OPT_SETBITS, TRUE, &errors,
1392 	    NULL) != argc)
1393 		return (DCMD_USAGE);
1394 
1395 	if (!(flags & DCMD_ADDRSPEC))
1396 		return (DCMD_USAGE);
1397 
1398 	if (mdb_vread(&spa, sizeof (spa), addr) == -1) {
1399 		mdb_warn("failed to read spa_t at %p", addr);
1400 		return (DCMD_ERR);
1401 	}
1402 
1403 	/*
1404 	 * Unitialized spa_t structures can have a NULL root vdev.
1405 	 */
1406 	if (spa.spa_root_vdev == NULL) {
1407 		mdb_printf("no associated vdevs\n");
1408 		return (DCMD_OK);
1409 	}
1410 
1411 	v.a_type = MDB_TYPE_STRING;
1412 	v.a_un.a_str = errors ? "-re" : "-r";
1413 
1414 	return (mdb_call_dcmd("vdev", (uintptr_t)spa.spa_root_vdev,
1415 	    flags, 1, &v));
1416 }
1417 
1418 typedef struct txg_list_walk_data {
1419 	uintptr_t lw_head[TXG_SIZE];
1420 	int	lw_txgoff;
1421 	int	lw_maxoff;
1422 	size_t	lw_offset;
1423 	void	*lw_obj;
1424 } txg_list_walk_data_t;
1425 
1426 static int
1427 txg_list_walk_init_common(mdb_walk_state_t *wsp, int txg, int maxoff)
1428 {
1429 	txg_list_walk_data_t *lwd;
1430 	txg_list_t list;
1431 	int i;
1432 
1433 	lwd = mdb_alloc(sizeof (txg_list_walk_data_t), UM_SLEEP | UM_GC);
1434 	if (mdb_vread(&list, sizeof (txg_list_t), wsp->walk_addr) == -1) {
1435 		mdb_warn("failed to read txg_list_t at %#lx", wsp->walk_addr);
1436 		return (WALK_ERR);
1437 	}
1438 
1439 	for (i = 0; i < TXG_SIZE; i++)
1440 		lwd->lw_head[i] = (uintptr_t)list.tl_head[i];
1441 	lwd->lw_offset = list.tl_offset;
1442 	lwd->lw_obj = mdb_alloc(lwd->lw_offset + sizeof (txg_node_t),
1443 	    UM_SLEEP | UM_GC);
1444 	lwd->lw_txgoff = txg;
1445 	lwd->lw_maxoff = maxoff;
1446 
1447 	wsp->walk_addr = lwd->lw_head[lwd->lw_txgoff];
1448 	wsp->walk_data = lwd;
1449 
1450 	return (WALK_NEXT);
1451 }
1452 
1453 static int
1454 txg_list_walk_init(mdb_walk_state_t *wsp)
1455 {
1456 	return (txg_list_walk_init_common(wsp, 0, TXG_SIZE-1));
1457 }
1458 
1459 static int
1460 txg_list0_walk_init(mdb_walk_state_t *wsp)
1461 {
1462 	return (txg_list_walk_init_common(wsp, 0, 0));
1463 }
1464 
1465 static int
1466 txg_list1_walk_init(mdb_walk_state_t *wsp)
1467 {
1468 	return (txg_list_walk_init_common(wsp, 1, 1));
1469 }
1470 
1471 static int
1472 txg_list2_walk_init(mdb_walk_state_t *wsp)
1473 {
1474 	return (txg_list_walk_init_common(wsp, 2, 2));
1475 }
1476 
1477 static int
1478 txg_list3_walk_init(mdb_walk_state_t *wsp)
1479 {
1480 	return (txg_list_walk_init_common(wsp, 3, 3));
1481 }
1482 
1483 static int
1484 txg_list_walk_step(mdb_walk_state_t *wsp)
1485 {
1486 	txg_list_walk_data_t *lwd = wsp->walk_data;
1487 	uintptr_t addr;
1488 	txg_node_t *node;
1489 	int status;
1490 
1491 	while (wsp->walk_addr == NULL && lwd->lw_txgoff < lwd->lw_maxoff) {
1492 		lwd->lw_txgoff++;
1493 		wsp->walk_addr = lwd->lw_head[lwd->lw_txgoff];
1494 	}
1495 
1496 	if (wsp->walk_addr == NULL)
1497 		return (WALK_DONE);
1498 
1499 	addr = wsp->walk_addr - lwd->lw_offset;
1500 
1501 	if (mdb_vread(lwd->lw_obj,
1502 	    lwd->lw_offset + sizeof (txg_node_t), addr) == -1) {
1503 		mdb_warn("failed to read list element at %#lx", addr);
1504 		return (WALK_ERR);
1505 	}
1506 
1507 	status = wsp->walk_callback(addr, lwd->lw_obj, wsp->walk_cbdata);
1508 	node = (txg_node_t *)((uintptr_t)lwd->lw_obj + lwd->lw_offset);
1509 	wsp->walk_addr = (uintptr_t)node->tn_next[lwd->lw_txgoff];
1510 
1511 	return (status);
1512 }
1513 
1514 /*
1515  * ::walk spa
1516  *
1517  * Walk all named spa_t structures in the namespace.  This is nothing more than
1518  * a layered avl walk.
1519  */
1520 static int
1521 spa_walk_init(mdb_walk_state_t *wsp)
1522 {
1523 	GElf_Sym sym;
1524 
1525 	if (wsp->walk_addr != NULL) {
1526 		mdb_warn("spa walk only supports global walks\n");
1527 		return (WALK_ERR);
1528 	}
1529 
1530 	if (mdb_lookup_by_obj(ZFS_OBJ_NAME, "spa_namespace_avl", &sym) == -1) {
1531 		mdb_warn("failed to find symbol 'spa_namespace_avl'");
1532 		return (WALK_ERR);
1533 	}
1534 
1535 	wsp->walk_addr = (uintptr_t)sym.st_value;
1536 
1537 	if (mdb_layered_walk("avl", wsp) == -1) {
1538 		mdb_warn("failed to walk 'avl'\n");
1539 		return (WALK_ERR);
1540 	}
1541 
1542 	return (WALK_NEXT);
1543 }
1544 
1545 static int
1546 spa_walk_step(mdb_walk_state_t *wsp)
1547 {
1548 	spa_t	spa;
1549 
1550 	if (mdb_vread(&spa, sizeof (spa), wsp->walk_addr) == -1) {
1551 		mdb_warn("failed to read spa_t at %p", wsp->walk_addr);
1552 		return (WALK_ERR);
1553 	}
1554 
1555 	return (wsp->walk_callback(wsp->walk_addr, &spa, wsp->walk_cbdata));
1556 }
1557 
1558 /*
1559  * MDB module linkage information:
1560  *
1561  * We declare a list of structures describing our dcmds, and a function
1562  * named _mdb_init to return a pointer to our module information.
1563  */
1564 
1565 static const mdb_dcmd_t dcmds[] = {
1566 	{ "blkptr", ":", "print blkptr_t", blkptr },
1567 	{ "dbuf", ":", "print dmu_buf_impl_t", dbuf },
1568 	{ "dbuf_stats", ":", "dbuf stats", dbuf_stats },
1569 	{ "dbufs",
1570 	"\t[-O objset_t*] [-n objset_name | \"mos\"] [-o object | \"mdn\"] \n"
1571 	"\t[-l level] [-b blkid | \"bonus\"]",
1572 	"find dmu_buf_impl_t's that meet criterion", dbufs },
1573 	{ "abuf_find", "dva_word[0] dva_word[1]",
1574 	"find arc_buf_hdr_t of a specified DVA",
1575 	abuf_find },
1576 	{ "spa", "?[-cv]", "spa_t summary", spa_print },
1577 	{ "spa_config", ":", "print spa_t configuration", spa_print_config },
1578 	{ "spa_verify", ":", "verify spa_t consistency", spa_verify },
1579 	{ "spa_space", ":[-b]", "print spa_t on-disk space usage", spa_space },
1580 	{ "spa_vdevs", ":", "given a spa_t, print vdev summary", spa_vdevs },
1581 	{ "vdev", ":[-re]", "vdev_t summary", vdev_print },
1582 	{ "zio_pipeline", ":", "decode a zio pipeline", zio_pipeline },
1583 	{ "zfs_params", "", "print zfs tunable parameters", zfs_params },
1584 	{ NULL }
1585 };
1586 
1587 static const mdb_walker_t walkers[] = {
1588 	/*
1589 	 * In userland, there is no generic provider of list_t walkers, so we
1590 	 * need to add it.
1591 	 */
1592 #ifndef _KERNEL
1593 	{ LIST_WALK_NAME, LIST_WALK_DESC,
1594 		list_walk_init, list_walk_step, list_walk_fini },
1595 #endif
1596 	{ "zms_freelist", "walk ZFS metaslab freelist",
1597 		freelist_walk_init, freelist_walk_step, NULL },
1598 	{ "txg_list", "given any txg_list_t *, walk all entries in all txgs",
1599 		txg_list_walk_init, txg_list_walk_step, NULL },
1600 	{ "txg_list0", "given any txg_list_t *, walk all entries in txg 0",
1601 		txg_list0_walk_init, txg_list_walk_step, NULL },
1602 	{ "txg_list1", "given any txg_list_t *, walk all entries in txg 1",
1603 		txg_list1_walk_init, txg_list_walk_step, NULL },
1604 	{ "txg_list2", "given any txg_list_t *, walk all entries in txg 2",
1605 		txg_list2_walk_init, txg_list_walk_step, NULL },
1606 	{ "txg_list3", "given any txg_list_t *, walk all entries in txg 3",
1607 		txg_list3_walk_init, txg_list_walk_step, NULL },
1608 	{ "spa", "walk all spa_t entries in the namespace",
1609 		spa_walk_init, spa_walk_step, NULL },
1610 	{ "metaslab", "given a spa_t *, walk all metaslab_t structures",
1611 		metaslab_walk_init, metaslab_walk_step, NULL },
1612 	{ NULL }
1613 };
1614 
1615 static const mdb_modinfo_t modinfo = {
1616 	MDB_API_VERSION, dcmds, walkers
1617 };
1618 
1619 const mdb_modinfo_t *
1620 _mdb_init(void)
1621 {
1622 	return (&modinfo);
1623 }
1624