xref: /titanic_50/usr/src/cmd/zdb/zdb.c (revision 64cfc8ed0c5da83f827c424588bfe709e3fb5a7a)
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 <stdio.h>
29 #include <stdio_ext.h>
30 #include <stdlib.h>
31 #include <ctype.h>
32 #include <sys/zfs_context.h>
33 #include <sys/spa.h>
34 #include <sys/spa_impl.h>
35 #include <sys/dmu.h>
36 #include <sys/zap.h>
37 #include <sys/fs/zfs.h>
38 #include <sys/zfs_znode.h>
39 #include <sys/vdev.h>
40 #include <sys/vdev_impl.h>
41 #include <sys/metaslab_impl.h>
42 #include <sys/dmu_objset.h>
43 #include <sys/dsl_dir.h>
44 #include <sys/dsl_dataset.h>
45 #include <sys/dsl_pool.h>
46 #include <sys/dbuf.h>
47 #include <sys/zil.h>
48 #include <sys/zil_impl.h>
49 #include <sys/stat.h>
50 #include <sys/resource.h>
51 #include <sys/dmu_traverse.h>
52 #include <sys/zio_checksum.h>
53 #include <sys/zio_compress.h>
54 #undef ZFS_MAXNAMELEN
55 #undef verify
56 #include <libzfs.h>
57 
58 const char cmdname[] = "zdb";
59 uint8_t dump_opt[256];
60 
61 typedef void object_viewer_t(objset_t *, uint64_t, void *data, size_t size);
62 
63 extern void dump_intent_log(zilog_t *);
64 uint64_t *zopt_object = NULL;
65 int zopt_objects = 0;
66 int zdb_advance = ADVANCE_PRE;
67 zbookmark_t zdb_noread = { 0, 0, ZB_NO_LEVEL, 0 };
68 libzfs_handle_t *g_zfs;
69 
70 /*
71  * These libumem hooks provide a reasonable set of defaults for the allocator's
72  * debugging facilities.
73  */
74 const char *
75 _umem_debug_init()
76 {
77 	return ("default,verbose"); /* $UMEM_DEBUG setting */
78 }
79 
80 const char *
81 _umem_logging_init(void)
82 {
83 	return ("fail,contents"); /* $UMEM_LOGGING setting */
84 }
85 
86 static void
87 usage(void)
88 {
89 	(void) fprintf(stderr,
90 	    "Usage: %s [-udibcsvLUe] [-O order] [-B os:obj:level:blkid] "
91 	    "dataset [object...]\n"
92 	    "       %s -C [pool]\n"
93 	    "       %s -l dev\n"
94 	    "       %s -R vdev:offset:size:flags\n"
95 	    "       %s [-p path_to_vdev_dir]\n",
96 	    cmdname, cmdname, cmdname, cmdname, cmdname);
97 
98 	(void) fprintf(stderr, "	-u uberblock\n");
99 	(void) fprintf(stderr, "	-d datasets\n");
100 	(void) fprintf(stderr, "        -C cached pool configuration\n");
101 	(void) fprintf(stderr, "	-i intent logs\n");
102 	(void) fprintf(stderr, "	-b block statistics\n");
103 	(void) fprintf(stderr, "	-c checksum all data blocks\n");
104 	(void) fprintf(stderr, "	-s report stats on zdb's I/O\n");
105 	(void) fprintf(stderr, "	-v verbose (applies to all others)\n");
106 	(void) fprintf(stderr, "        -l dump label contents\n");
107 	(void) fprintf(stderr, "	-L live pool (allows some errors)\n");
108 	(void) fprintf(stderr, "	-O [!]<pre|post|prune|data|holes> "
109 	    "visitation order\n");
110 	(void) fprintf(stderr, "	-U use zpool.cache in /tmp\n");
111 	(void) fprintf(stderr, "	-B objset:object:level:blkid -- "
112 	    "simulate bad block\n");
113 	(void) fprintf(stderr, "        -R read and display block from a"
114 	    "device\n");
115 	(void) fprintf(stderr, "        -e Pool is exported/destroyed\n");
116 	(void) fprintf(stderr, "	-p <Path to vdev dir> (use with -e)\n");
117 	(void) fprintf(stderr, "Specify an option more than once (e.g. -bb) "
118 	    "to make only that option verbose\n");
119 	(void) fprintf(stderr, "Default is to dump everything non-verbosely\n");
120 	exit(1);
121 }
122 
123 static void
124 fatal(const char *fmt, ...)
125 {
126 	va_list ap;
127 
128 	va_start(ap, fmt);
129 	(void) fprintf(stderr, "%s: ", cmdname);
130 	(void) vfprintf(stderr, fmt, ap);
131 	va_end(ap);
132 	(void) fprintf(stderr, "\n");
133 
134 	exit(1);
135 }
136 
137 static void
138 dump_nvlist(nvlist_t *list, int indent)
139 {
140 	nvpair_t *elem = NULL;
141 
142 	while ((elem = nvlist_next_nvpair(list, elem)) != NULL) {
143 		switch (nvpair_type(elem)) {
144 		case DATA_TYPE_STRING:
145 			{
146 				char *value;
147 
148 				VERIFY(nvpair_value_string(elem, &value) == 0);
149 				(void) printf("%*s%s='%s'\n", indent, "",
150 				    nvpair_name(elem), value);
151 			}
152 			break;
153 
154 		case DATA_TYPE_UINT64:
155 			{
156 				uint64_t value;
157 
158 				VERIFY(nvpair_value_uint64(elem, &value) == 0);
159 				(void) printf("%*s%s=%llu\n", indent, "",
160 				    nvpair_name(elem), (u_longlong_t)value);
161 			}
162 			break;
163 
164 		case DATA_TYPE_NVLIST:
165 			{
166 				nvlist_t *value;
167 
168 				VERIFY(nvpair_value_nvlist(elem, &value) == 0);
169 				(void) printf("%*s%s\n", indent, "",
170 				    nvpair_name(elem));
171 				dump_nvlist(value, indent + 4);
172 			}
173 			break;
174 
175 		case DATA_TYPE_NVLIST_ARRAY:
176 			{
177 				nvlist_t **value;
178 				uint_t c, count;
179 
180 				VERIFY(nvpair_value_nvlist_array(elem, &value,
181 				    &count) == 0);
182 
183 				for (c = 0; c < count; c++) {
184 					(void) printf("%*s%s[%u]\n", indent, "",
185 					    nvpair_name(elem), c);
186 					dump_nvlist(value[c], indent + 8);
187 				}
188 			}
189 			break;
190 
191 		default:
192 
193 			(void) printf("bad config type %d for %s\n",
194 			    nvpair_type(elem), nvpair_name(elem));
195 		}
196 	}
197 }
198 
199 /* ARGSUSED */
200 static void
201 dump_packed_nvlist(objset_t *os, uint64_t object, void *data, size_t size)
202 {
203 	nvlist_t *nv;
204 	size_t nvsize = *(uint64_t *)data;
205 	char *packed = umem_alloc(nvsize, UMEM_NOFAIL);
206 
207 	VERIFY(0 == dmu_read(os, object, 0, nvsize, packed));
208 
209 	VERIFY(nvlist_unpack(packed, nvsize, &nv, 0) == 0);
210 
211 	umem_free(packed, nvsize);
212 
213 	dump_nvlist(nv, 8);
214 
215 	nvlist_free(nv);
216 }
217 
218 const char dump_zap_stars[] = "****************************************";
219 const int dump_zap_width = sizeof (dump_zap_stars) - 1;
220 
221 static void
222 dump_zap_histogram(uint64_t histo[ZAP_HISTOGRAM_SIZE])
223 {
224 	int i;
225 	int minidx = ZAP_HISTOGRAM_SIZE - 1;
226 	int maxidx = 0;
227 	uint64_t max = 0;
228 
229 	for (i = 0; i < ZAP_HISTOGRAM_SIZE; i++) {
230 		if (histo[i] > max)
231 			max = histo[i];
232 		if (histo[i] > 0 && i > maxidx)
233 			maxidx = i;
234 		if (histo[i] > 0 && i < minidx)
235 			minidx = i;
236 	}
237 
238 	if (max < dump_zap_width)
239 		max = dump_zap_width;
240 
241 	for (i = minidx; i <= maxidx; i++)
242 		(void) printf("\t\t\t%u: %6llu %s\n", i, (u_longlong_t)histo[i],
243 		    &dump_zap_stars[(max - histo[i]) * dump_zap_width / max]);
244 }
245 
246 static void
247 dump_zap_stats(objset_t *os, uint64_t object)
248 {
249 	int error;
250 	zap_stats_t zs;
251 
252 	error = zap_get_stats(os, object, &zs);
253 	if (error)
254 		return;
255 
256 	if (zs.zs_ptrtbl_len == 0) {
257 		ASSERT(zs.zs_num_blocks == 1);
258 		(void) printf("\tmicrozap: %llu bytes, %llu entries\n",
259 		    (u_longlong_t)zs.zs_blocksize,
260 		    (u_longlong_t)zs.zs_num_entries);
261 		return;
262 	}
263 
264 	(void) printf("\tFat ZAP stats:\n");
265 
266 	(void) printf("\t\tPointer table:\n");
267 	(void) printf("\t\t\t%llu elements\n",
268 	    (u_longlong_t)zs.zs_ptrtbl_len);
269 	(void) printf("\t\t\tzt_blk: %llu\n",
270 	    (u_longlong_t)zs.zs_ptrtbl_zt_blk);
271 	(void) printf("\t\t\tzt_numblks: %llu\n",
272 	    (u_longlong_t)zs.zs_ptrtbl_zt_numblks);
273 	(void) printf("\t\t\tzt_shift: %llu\n",
274 	    (u_longlong_t)zs.zs_ptrtbl_zt_shift);
275 	(void) printf("\t\t\tzt_blks_copied: %llu\n",
276 	    (u_longlong_t)zs.zs_ptrtbl_blks_copied);
277 	(void) printf("\t\t\tzt_nextblk: %llu\n",
278 	    (u_longlong_t)zs.zs_ptrtbl_nextblk);
279 
280 	(void) printf("\t\tZAP entries: %llu\n",
281 	    (u_longlong_t)zs.zs_num_entries);
282 	(void) printf("\t\tLeaf blocks: %llu\n",
283 	    (u_longlong_t)zs.zs_num_leafs);
284 	(void) printf("\t\tTotal blocks: %llu\n",
285 	    (u_longlong_t)zs.zs_num_blocks);
286 	(void) printf("\t\tzap_block_type: 0x%llx\n",
287 	    (u_longlong_t)zs.zs_block_type);
288 	(void) printf("\t\tzap_magic: 0x%llx\n",
289 	    (u_longlong_t)zs.zs_magic);
290 	(void) printf("\t\tzap_salt: 0x%llx\n",
291 	    (u_longlong_t)zs.zs_salt);
292 
293 	(void) printf("\t\tLeafs with 2^n pointers:\n");
294 	dump_zap_histogram(zs.zs_leafs_with_2n_pointers);
295 
296 	(void) printf("\t\tBlocks with n*5 entries:\n");
297 	dump_zap_histogram(zs.zs_blocks_with_n5_entries);
298 
299 	(void) printf("\t\tBlocks n/10 full:\n");
300 	dump_zap_histogram(zs.zs_blocks_n_tenths_full);
301 
302 	(void) printf("\t\tEntries with n chunks:\n");
303 	dump_zap_histogram(zs.zs_entries_using_n_chunks);
304 
305 	(void) printf("\t\tBuckets with n entries:\n");
306 	dump_zap_histogram(zs.zs_buckets_with_n_entries);
307 }
308 
309 /*ARGSUSED*/
310 static void
311 dump_none(objset_t *os, uint64_t object, void *data, size_t size)
312 {
313 }
314 
315 /*ARGSUSED*/
316 void
317 dump_uint8(objset_t *os, uint64_t object, void *data, size_t size)
318 {
319 }
320 
321 /*ARGSUSED*/
322 static void
323 dump_uint64(objset_t *os, uint64_t object, void *data, size_t size)
324 {
325 }
326 
327 /*ARGSUSED*/
328 static void
329 dump_zap(objset_t *os, uint64_t object, void *data, size_t size)
330 {
331 	zap_cursor_t zc;
332 	zap_attribute_t attr;
333 	void *prop;
334 	int i;
335 
336 	dump_zap_stats(os, object);
337 	(void) printf("\n");
338 
339 	for (zap_cursor_init(&zc, os, object);
340 	    zap_cursor_retrieve(&zc, &attr) == 0;
341 	    zap_cursor_advance(&zc)) {
342 		(void) printf("\t\t%s = ", attr.za_name);
343 		if (attr.za_num_integers == 0) {
344 			(void) printf("\n");
345 			continue;
346 		}
347 		prop = umem_zalloc(attr.za_num_integers *
348 		    attr.za_integer_length, UMEM_NOFAIL);
349 		(void) zap_lookup(os, object, attr.za_name,
350 		    attr.za_integer_length, attr.za_num_integers, prop);
351 		if (attr.za_integer_length == 1) {
352 			(void) printf("%s", (char *)prop);
353 		} else {
354 			for (i = 0; i < attr.za_num_integers; i++) {
355 				switch (attr.za_integer_length) {
356 				case 2:
357 					(void) printf("%u ",
358 					    ((uint16_t *)prop)[i]);
359 					break;
360 				case 4:
361 					(void) printf("%u ",
362 					    ((uint32_t *)prop)[i]);
363 					break;
364 				case 8:
365 					(void) printf("%lld ",
366 					    (u_longlong_t)((int64_t *)prop)[i]);
367 					break;
368 				}
369 			}
370 		}
371 		(void) printf("\n");
372 		umem_free(prop, attr.za_num_integers * attr.za_integer_length);
373 	}
374 	zap_cursor_fini(&zc);
375 }
376 
377 /*ARGSUSED*/
378 static void
379 dump_zpldir(objset_t *os, uint64_t object, void *data, size_t size)
380 {
381 	zap_cursor_t zc;
382 	zap_attribute_t attr;
383 	const char *typenames[] = {
384 		/* 0 */ "not specified",
385 		/* 1 */ "FIFO",
386 		/* 2 */ "Character Device",
387 		/* 3 */ "3 (invalid)",
388 		/* 4 */ "Directory",
389 		/* 5 */ "5 (invalid)",
390 		/* 6 */ "Block Device",
391 		/* 7 */ "7 (invalid)",
392 		/* 8 */ "Regular File",
393 		/* 9 */ "9 (invalid)",
394 		/* 10 */ "Symbolic Link",
395 		/* 11 */ "11 (invalid)",
396 		/* 12 */ "Socket",
397 		/* 13 */ "Door",
398 		/* 14 */ "Event Port",
399 		/* 15 */ "15 (invalid)",
400 	};
401 
402 	dump_zap_stats(os, object);
403 	(void) printf("\n");
404 
405 	for (zap_cursor_init(&zc, os, object);
406 	    zap_cursor_retrieve(&zc, &attr) == 0;
407 	    zap_cursor_advance(&zc)) {
408 		(void) printf("\t\t%s = %lld (type: %s)\n",
409 		    attr.za_name, ZFS_DIRENT_OBJ(attr.za_first_integer),
410 		    typenames[ZFS_DIRENT_TYPE(attr.za_first_integer)]);
411 	}
412 	zap_cursor_fini(&zc);
413 }
414 
415 static void
416 dump_spacemap(objset_t *os, space_map_obj_t *smo, space_map_t *sm)
417 {
418 	uint64_t alloc, offset, entry;
419 	uint8_t mapshift = sm->sm_shift;
420 	uint64_t mapstart = sm->sm_start;
421 	char *ddata[] = { "ALLOC", "FREE", "CONDENSE", "INVALID",
422 			    "INVALID", "INVALID", "INVALID", "INVALID" };
423 
424 	if (smo->smo_object == 0)
425 		return;
426 
427 	/*
428 	 * Print out the freelist entries in both encoded and decoded form.
429 	 */
430 	alloc = 0;
431 	for (offset = 0; offset < smo->smo_objsize; offset += sizeof (entry)) {
432 		VERIFY(0 == dmu_read(os, smo->smo_object, offset,
433 		    sizeof (entry), &entry));
434 		if (SM_DEBUG_DECODE(entry)) {
435 			(void) printf("\t\t[%4llu] %s: txg %llu, pass %llu\n",
436 			    (u_longlong_t)(offset / sizeof (entry)),
437 			    ddata[SM_DEBUG_ACTION_DECODE(entry)],
438 			    (u_longlong_t)SM_DEBUG_TXG_DECODE(entry),
439 			    (u_longlong_t)SM_DEBUG_SYNCPASS_DECODE(entry));
440 		} else {
441 			(void) printf("\t\t[%4llu]    %c  range:"
442 			    " %08llx-%08llx  size: %06llx\n",
443 			    (u_longlong_t)(offset / sizeof (entry)),
444 			    SM_TYPE_DECODE(entry) == SM_ALLOC ? 'A' : 'F',
445 			    (u_longlong_t)((SM_OFFSET_DECODE(entry) <<
446 			    mapshift) + mapstart),
447 			    (u_longlong_t)((SM_OFFSET_DECODE(entry) <<
448 			    mapshift) + mapstart + (SM_RUN_DECODE(entry) <<
449 			    mapshift)),
450 			    (u_longlong_t)(SM_RUN_DECODE(entry) << mapshift));
451 			if (SM_TYPE_DECODE(entry) == SM_ALLOC)
452 				alloc += SM_RUN_DECODE(entry) << mapshift;
453 			else
454 				alloc -= SM_RUN_DECODE(entry) << mapshift;
455 		}
456 	}
457 	if (alloc != smo->smo_alloc) {
458 		(void) printf("space_map_object alloc (%llu) INCONSISTENT "
459 		    "with space map summary (%llu)\n",
460 		    (u_longlong_t)smo->smo_alloc, (u_longlong_t)alloc);
461 	}
462 }
463 
464 static void
465 dump_metaslab(metaslab_t *msp)
466 {
467 	char freebuf[5];
468 	space_map_obj_t *smo = &msp->ms_smo;
469 	vdev_t *vd = msp->ms_group->mg_vd;
470 	spa_t *spa = vd->vdev_spa;
471 
472 	nicenum(msp->ms_map.sm_size - smo->smo_alloc, freebuf);
473 
474 	if (dump_opt['d'] <= 5) {
475 		(void) printf("\t%10llx   %10llu   %5s\n",
476 		    (u_longlong_t)msp->ms_map.sm_start,
477 		    (u_longlong_t)smo->smo_object,
478 		    freebuf);
479 		return;
480 	}
481 
482 	(void) printf(
483 	    "\tvdev %llu   offset %08llx   spacemap %4llu   free %5s\n",
484 	    (u_longlong_t)vd->vdev_id, (u_longlong_t)msp->ms_map.sm_start,
485 	    (u_longlong_t)smo->smo_object, freebuf);
486 
487 	ASSERT(msp->ms_map.sm_size == (1ULL << vd->vdev_ms_shift));
488 
489 	dump_spacemap(spa->spa_meta_objset, smo, &msp->ms_map);
490 }
491 
492 static void
493 dump_metaslabs(spa_t *spa)
494 {
495 	vdev_t *rvd = spa->spa_root_vdev;
496 	vdev_t *vd;
497 	int c, m;
498 
499 	(void) printf("\nMetaslabs:\n");
500 
501 	for (c = 0; c < rvd->vdev_children; c++) {
502 		vd = rvd->vdev_child[c];
503 
504 		spa_config_enter(spa, RW_READER, FTAG);
505 		(void) printf("\n    vdev %llu = %s\n\n",
506 		    (u_longlong_t)vd->vdev_id, vdev_description(vd));
507 		spa_config_exit(spa, FTAG);
508 
509 		if (dump_opt['d'] <= 5) {
510 			(void) printf("\t%10s   %10s   %5s\n",
511 			    "offset", "spacemap", "free");
512 			(void) printf("\t%10s   %10s   %5s\n",
513 			    "------", "--------", "----");
514 		}
515 		for (m = 0; m < vd->vdev_ms_count; m++)
516 			dump_metaslab(vd->vdev_ms[m]);
517 		(void) printf("\n");
518 	}
519 }
520 
521 static void
522 dump_dtl(vdev_t *vd, int indent)
523 {
524 	avl_tree_t *t = &vd->vdev_dtl_map.sm_root;
525 	spa_t *spa = vd->vdev_spa;
526 	space_seg_t *ss;
527 	vdev_t *pvd;
528 	int c;
529 
530 	if (indent == 0)
531 		(void) printf("\nDirty time logs:\n\n");
532 
533 	spa_config_enter(spa, RW_READER, FTAG);
534 	(void) printf("\t%*s%s\n", indent, "", vdev_description(vd));
535 	spa_config_exit(spa, FTAG);
536 
537 	for (ss = avl_first(t); ss; ss = AVL_NEXT(t, ss)) {
538 		/*
539 		 * Everything in this DTL must appear in all parent DTL unions.
540 		 */
541 		for (pvd = vd; pvd; pvd = pvd->vdev_parent)
542 			ASSERT(vdev_dtl_contains(&pvd->vdev_dtl_map,
543 			    ss->ss_start, ss->ss_end - ss->ss_start));
544 		(void) printf("\t%*soutage [%llu,%llu] length %llu\n",
545 		    indent, "",
546 		    (u_longlong_t)ss->ss_start,
547 		    (u_longlong_t)ss->ss_end - 1,
548 		    (u_longlong_t)(ss->ss_end - ss->ss_start));
549 	}
550 
551 	(void) printf("\n");
552 
553 	if (dump_opt['d'] > 5 && vd->vdev_children == 0) {
554 		dump_spacemap(vd->vdev_spa->spa_meta_objset, &vd->vdev_dtl,
555 		    &vd->vdev_dtl_map);
556 		(void) printf("\n");
557 	}
558 
559 	for (c = 0; c < vd->vdev_children; c++)
560 		dump_dtl(vd->vdev_child[c], indent + 4);
561 }
562 
563 /*ARGSUSED*/
564 static void
565 dump_dnode(objset_t *os, uint64_t object, void *data, size_t size)
566 {
567 }
568 
569 static uint64_t
570 blkid2offset(dnode_phys_t *dnp, int level, uint64_t blkid)
571 {
572 	if (level < 0)
573 		return (blkid);
574 
575 	return ((blkid << (level * (dnp->dn_indblkshift - SPA_BLKPTRSHIFT))) *
576 	    dnp->dn_datablkszsec << SPA_MINBLOCKSHIFT);
577 }
578 
579 static void
580 sprintf_blkptr_compact(char *blkbuf, blkptr_t *bp, int alldvas)
581 {
582 	dva_t *dva = bp->blk_dva;
583 	int ndvas = alldvas ? BP_GET_NDVAS(bp) : 1;
584 	int i;
585 
586 	blkbuf[0] = '\0';
587 
588 	for (i = 0; i < ndvas; i++)
589 		(void) sprintf(blkbuf + strlen(blkbuf), "%llu:%llx:%llx ",
590 		    (u_longlong_t)DVA_GET_VDEV(&dva[i]),
591 		    (u_longlong_t)DVA_GET_OFFSET(&dva[i]),
592 		    (u_longlong_t)DVA_GET_ASIZE(&dva[i]));
593 
594 	(void) sprintf(blkbuf + strlen(blkbuf), "%llxL/%llxP F=%llu B=%llu",
595 	    (u_longlong_t)BP_GET_LSIZE(bp),
596 	    (u_longlong_t)BP_GET_PSIZE(bp),
597 	    (u_longlong_t)bp->blk_fill,
598 	    (u_longlong_t)bp->blk_birth);
599 }
600 
601 /* ARGSUSED */
602 static int
603 zdb_indirect_cb(traverse_blk_cache_t *bc, spa_t *spa, void *a)
604 {
605 	zbookmark_t *zb = &bc->bc_bookmark;
606 	blkptr_t *bp = &bc->bc_blkptr;
607 	void *data = bc->bc_data;
608 	dnode_phys_t *dnp = bc->bc_dnode;
609 	char blkbuf[BP_SPRINTF_LEN + 80];
610 	int l;
611 
612 	if (bc->bc_errno) {
613 		(void) sprintf(blkbuf,
614 		    "Error %d reading <%llu, %llu, %lld, %llu>: ",
615 		    bc->bc_errno,
616 		    (u_longlong_t)zb->zb_objset,
617 		    (u_longlong_t)zb->zb_object,
618 		    (u_longlong_t)zb->zb_level,
619 		    (u_longlong_t)zb->zb_blkid);
620 		goto out;
621 	}
622 
623 	if (zb->zb_level == -1) {
624 		ASSERT3U(BP_GET_TYPE(bp), ==, DMU_OT_OBJSET);
625 		ASSERT3U(BP_GET_LEVEL(bp), ==, 0);
626 	} else {
627 		ASSERT3U(BP_GET_TYPE(bp), ==, dnp->dn_type);
628 		ASSERT3U(BP_GET_LEVEL(bp), ==, zb->zb_level);
629 	}
630 
631 	if (zb->zb_level > 0) {
632 		uint64_t fill = 0;
633 		blkptr_t *bpx, *bpend;
634 
635 		for (bpx = data, bpend = bpx + BP_GET_LSIZE(bp) / sizeof (*bpx);
636 		    bpx < bpend; bpx++) {
637 			if (bpx->blk_birth != 0) {
638 				fill += bpx->blk_fill;
639 			} else {
640 				ASSERT(bpx->blk_fill == 0);
641 			}
642 		}
643 		ASSERT3U(fill, ==, bp->blk_fill);
644 	}
645 
646 	if (zb->zb_level == 0 && dnp->dn_type == DMU_OT_DNODE) {
647 		uint64_t fill = 0;
648 		dnode_phys_t *dnx, *dnend;
649 
650 		for (dnx = data, dnend = dnx + (BP_GET_LSIZE(bp)>>DNODE_SHIFT);
651 		    dnx < dnend; dnx++) {
652 			if (dnx->dn_type != DMU_OT_NONE)
653 				fill++;
654 		}
655 		ASSERT3U(fill, ==, bp->blk_fill);
656 	}
657 
658 	(void) sprintf(blkbuf, "%16llx ",
659 	    (u_longlong_t)blkid2offset(dnp, zb->zb_level, zb->zb_blkid));
660 
661 	ASSERT(zb->zb_level >= 0);
662 
663 	for (l = dnp->dn_nlevels - 1; l >= -1; l--) {
664 		if (l == zb->zb_level) {
665 			(void) sprintf(blkbuf + strlen(blkbuf), "L%llx",
666 			    (u_longlong_t)zb->zb_level);
667 		} else {
668 			(void) sprintf(blkbuf + strlen(blkbuf), " ");
669 		}
670 	}
671 
672 out:
673 	if (bp->blk_birth == 0) {
674 		(void) sprintf(blkbuf + strlen(blkbuf), "<hole>");
675 		(void) printf("%s\n", blkbuf);
676 	} else {
677 		sprintf_blkptr_compact(blkbuf + strlen(blkbuf), bp,
678 		    dump_opt['d'] > 5 ? 1 : 0);
679 		(void) printf("%s\n", blkbuf);
680 	}
681 
682 	return (bc->bc_errno ? ERESTART : 0);
683 }
684 
685 /*ARGSUSED*/
686 static void
687 dump_indirect(objset_t *os, uint64_t object, void *data, size_t size)
688 {
689 	traverse_handle_t *th;
690 	uint64_t objset = dmu_objset_id(os);
691 	int advance = zdb_advance;
692 
693 	(void) printf("Indirect blocks:\n");
694 
695 	if (object == 0)
696 		advance |= ADVANCE_DATA;
697 
698 	th = traverse_init(dmu_objset_spa(os), zdb_indirect_cb, NULL, advance,
699 	    ZIO_FLAG_CANFAIL);
700 	th->th_noread = zdb_noread;
701 
702 	traverse_add_dnode(th, 0, -1ULL, objset, object);
703 
704 	while (traverse_more(th) == EAGAIN)
705 		continue;
706 
707 	(void) printf("\n");
708 
709 	traverse_fini(th);
710 }
711 
712 /*ARGSUSED*/
713 static void
714 dump_dsl_dir(objset_t *os, uint64_t object, void *data, size_t size)
715 {
716 	dsl_dir_phys_t *dd = data;
717 	time_t crtime;
718 	char used[6], compressed[6], uncompressed[6], quota[6], resv[6];
719 
720 	if (dd == NULL)
721 		return;
722 
723 	ASSERT(size == sizeof (*dd));
724 
725 	crtime = dd->dd_creation_time;
726 	nicenum(dd->dd_used_bytes, used);
727 	nicenum(dd->dd_compressed_bytes, compressed);
728 	nicenum(dd->dd_uncompressed_bytes, uncompressed);
729 	nicenum(dd->dd_quota, quota);
730 	nicenum(dd->dd_reserved, resv);
731 
732 	(void) printf("\t\tcreation_time = %s", ctime(&crtime));
733 	(void) printf("\t\thead_dataset_obj = %llu\n",
734 	    (u_longlong_t)dd->dd_head_dataset_obj);
735 	(void) printf("\t\tparent_dir_obj = %llu\n",
736 	    (u_longlong_t)dd->dd_parent_obj);
737 	(void) printf("\t\tclone_parent_obj = %llu\n",
738 	    (u_longlong_t)dd->dd_clone_parent_obj);
739 	(void) printf("\t\tchild_dir_zapobj = %llu\n",
740 	    (u_longlong_t)dd->dd_child_dir_zapobj);
741 	(void) printf("\t\tused_bytes = %s\n", used);
742 	(void) printf("\t\tcompressed_bytes = %s\n", compressed);
743 	(void) printf("\t\tuncompressed_bytes = %s\n", uncompressed);
744 	(void) printf("\t\tquota = %s\n", quota);
745 	(void) printf("\t\treserved = %s\n", resv);
746 	(void) printf("\t\tprops_zapobj = %llu\n",
747 	    (u_longlong_t)dd->dd_props_zapobj);
748 	(void) printf("\t\tdeleg_zapobj = %llu\n",
749 	    (u_longlong_t)dd->dd_deleg_zapobj);
750 }
751 
752 /*ARGSUSED*/
753 static void
754 dump_dsl_dataset(objset_t *os, uint64_t object, void *data, size_t size)
755 {
756 	dsl_dataset_phys_t *ds = data;
757 	time_t crtime;
758 	char used[6], compressed[6], uncompressed[6], unique[6];
759 	char blkbuf[BP_SPRINTF_LEN];
760 
761 	if (ds == NULL)
762 		return;
763 
764 	ASSERT(size == sizeof (*ds));
765 	crtime = ds->ds_creation_time;
766 	nicenum(ds->ds_used_bytes, used);
767 	nicenum(ds->ds_compressed_bytes, compressed);
768 	nicenum(ds->ds_uncompressed_bytes, uncompressed);
769 	nicenum(ds->ds_unique_bytes, unique);
770 	sprintf_blkptr(blkbuf, BP_SPRINTF_LEN, &ds->ds_bp);
771 
772 	(void) printf("\t\tdataset_obj = %llu\n",
773 	    (u_longlong_t)ds->ds_dir_obj);
774 	(void) printf("\t\tprev_snap_obj = %llu\n",
775 	    (u_longlong_t)ds->ds_prev_snap_obj);
776 	(void) printf("\t\tprev_snap_txg = %llu\n",
777 	    (u_longlong_t)ds->ds_prev_snap_txg);
778 	(void) printf("\t\tnext_snap_obj = %llu\n",
779 	    (u_longlong_t)ds->ds_next_snap_obj);
780 	(void) printf("\t\tsnapnames_zapobj = %llu\n",
781 	    (u_longlong_t)ds->ds_snapnames_zapobj);
782 	(void) printf("\t\tnum_children = %llu\n",
783 	    (u_longlong_t)ds->ds_num_children);
784 	(void) printf("\t\tcreation_time = %s", ctime(&crtime));
785 	(void) printf("\t\tcreation_txg = %llu\n",
786 	    (u_longlong_t)ds->ds_creation_txg);
787 	(void) printf("\t\tdeadlist_obj = %llu\n",
788 	    (u_longlong_t)ds->ds_deadlist_obj);
789 	(void) printf("\t\tused_bytes = %s\n", used);
790 	(void) printf("\t\tcompressed_bytes = %s\n", compressed);
791 	(void) printf("\t\tuncompressed_bytes = %s\n", uncompressed);
792 	(void) printf("\t\tunique = %s\n", unique);
793 	(void) printf("\t\tfsid_guid = %llu\n",
794 	    (u_longlong_t)ds->ds_fsid_guid);
795 	(void) printf("\t\tguid = %llu\n",
796 	    (u_longlong_t)ds->ds_guid);
797 	(void) printf("\t\tflags = %llx\n",
798 	    (u_longlong_t)ds->ds_flags);
799 	(void) printf("\t\tbp = %s\n", blkbuf);
800 }
801 
802 static void
803 dump_bplist(objset_t *mos, uint64_t object, char *name)
804 {
805 	bplist_t bpl = { 0 };
806 	blkptr_t blk, *bp = &blk;
807 	uint64_t itor = 0;
808 	char bytes[6];
809 	char comp[6];
810 	char uncomp[6];
811 
812 	if (dump_opt['d'] < 3)
813 		return;
814 
815 	mutex_init(&bpl.bpl_lock, NULL, MUTEX_DEFAULT, NULL);
816 	VERIFY(0 == bplist_open(&bpl, mos, object));
817 	if (bplist_empty(&bpl)) {
818 		bplist_close(&bpl);
819 		mutex_destroy(&bpl.bpl_lock);
820 		return;
821 	}
822 
823 	nicenum(bpl.bpl_phys->bpl_bytes, bytes);
824 	if (bpl.bpl_dbuf->db_size == sizeof (bplist_phys_t)) {
825 		nicenum(bpl.bpl_phys->bpl_comp, comp);
826 		nicenum(bpl.bpl_phys->bpl_uncomp, uncomp);
827 		(void) printf("\n    %s: %llu entries, %s (%s/%s comp)\n",
828 		    name, (u_longlong_t)bpl.bpl_phys->bpl_entries,
829 		    bytes, comp, uncomp);
830 	} else {
831 		(void) printf("\n    %s: %llu entries, %s\n",
832 		    name, (u_longlong_t)bpl.bpl_phys->bpl_entries, bytes);
833 	}
834 
835 	if (dump_opt['d'] < 5) {
836 		bplist_close(&bpl);
837 		mutex_destroy(&bpl.bpl_lock);
838 		return;
839 	}
840 
841 	(void) printf("\n");
842 
843 	while (bplist_iterate(&bpl, &itor, bp) == 0) {
844 		char blkbuf[BP_SPRINTF_LEN];
845 
846 		ASSERT(bp->blk_birth != 0);
847 		sprintf_blkptr_compact(blkbuf, bp, dump_opt['d'] > 5 ? 1 : 0);
848 		(void) printf("\tItem %3llu: %s\n",
849 		    (u_longlong_t)itor - 1, blkbuf);
850 	}
851 
852 	bplist_close(&bpl);
853 	mutex_destroy(&bpl.bpl_lock);
854 }
855 
856 /*ARGSUSED*/
857 static void
858 dump_znode(objset_t *os, uint64_t object, void *data, size_t size)
859 {
860 	znode_phys_t *zp = data;
861 	time_t z_crtime, z_atime, z_mtime, z_ctime;
862 	char path[MAXPATHLEN * 2];	/* allow for xattr and failure prefix */
863 	int error;
864 
865 	ASSERT(size >= sizeof (znode_phys_t));
866 
867 	error = zfs_obj_to_path(os, object, path, sizeof (path));
868 	if (error != 0) {
869 		(void) snprintf(path, sizeof (path), "\?\?\?<object#%llu>",
870 		    (u_longlong_t)object);
871 	}
872 
873 	if (dump_opt['d'] < 3) {
874 		(void) printf("\t%s\n", path);
875 		return;
876 	}
877 
878 	z_crtime = (time_t)zp->zp_crtime[0];
879 	z_atime = (time_t)zp->zp_atime[0];
880 	z_mtime = (time_t)zp->zp_mtime[0];
881 	z_ctime = (time_t)zp->zp_ctime[0];
882 
883 	(void) printf("\tpath	%s\n", path);
884 	(void) printf("\tatime	%s", ctime(&z_atime));
885 	(void) printf("\tmtime	%s", ctime(&z_mtime));
886 	(void) printf("\tctime	%s", ctime(&z_ctime));
887 	(void) printf("\tcrtime	%s", ctime(&z_crtime));
888 	(void) printf("\tgen	%llu\n", (u_longlong_t)zp->zp_gen);
889 	(void) printf("\tmode	%llo\n", (u_longlong_t)zp->zp_mode);
890 	(void) printf("\tsize	%llu\n", (u_longlong_t)zp->zp_size);
891 	(void) printf("\tparent	%llu\n", (u_longlong_t)zp->zp_parent);
892 	(void) printf("\tlinks	%llu\n", (u_longlong_t)zp->zp_links);
893 	(void) printf("\txattr	%llu\n", (u_longlong_t)zp->zp_xattr);
894 	(void) printf("\trdev	0x%016llx\n", (u_longlong_t)zp->zp_rdev);
895 }
896 
897 /*ARGSUSED*/
898 static void
899 dump_acl(objset_t *os, uint64_t object, void *data, size_t size)
900 {
901 }
902 
903 /*ARGSUSED*/
904 static void
905 dump_dmu_objset(objset_t *os, uint64_t object, void *data, size_t size)
906 {
907 }
908 
909 static object_viewer_t *object_viewer[DMU_OT_NUMTYPES] = {
910 	dump_none,		/* unallocated			*/
911 	dump_zap,		/* object directory		*/
912 	dump_uint64,		/* object array			*/
913 	dump_none,		/* packed nvlist		*/
914 	dump_packed_nvlist,	/* packed nvlist size		*/
915 	dump_none,		/* bplist			*/
916 	dump_none,		/* bplist header		*/
917 	dump_none,		/* SPA space map header		*/
918 	dump_none,		/* SPA space map		*/
919 	dump_none,		/* ZIL intent log		*/
920 	dump_dnode,		/* DMU dnode			*/
921 	dump_dmu_objset,	/* DMU objset			*/
922 	dump_dsl_dir,		/* DSL directory		*/
923 	dump_zap,		/* DSL directory child map	*/
924 	dump_zap,		/* DSL dataset snap map		*/
925 	dump_zap,		/* DSL props			*/
926 	dump_dsl_dataset,	/* DSL dataset			*/
927 	dump_znode,		/* ZFS znode			*/
928 	dump_acl,		/* ZFS ACL			*/
929 	dump_uint8,		/* ZFS plain file		*/
930 	dump_zpldir,		/* ZFS directory		*/
931 	dump_zap,		/* ZFS master node		*/
932 	dump_zap,		/* ZFS delete queue		*/
933 	dump_uint8,		/* zvol object			*/
934 	dump_zap,		/* zvol prop			*/
935 	dump_uint8,		/* other uint8[]		*/
936 	dump_uint64,		/* other uint64[]		*/
937 	dump_zap,		/* other ZAP			*/
938 	dump_zap,		/* persistent error log		*/
939 	dump_uint8,		/* SPA history			*/
940 	dump_uint64,		/* SPA history offsets		*/
941 	dump_zap,		/* Pool properties		*/
942 	dump_zap,		/* DSL permissions		*/
943 };
944 
945 static void
946 dump_object(objset_t *os, uint64_t object, int verbosity, int *print_header)
947 {
948 	dmu_buf_t *db = NULL;
949 	dmu_object_info_t doi;
950 	dnode_t *dn;
951 	void *bonus = NULL;
952 	size_t bsize = 0;
953 	char iblk[6], dblk[6], lsize[6], asize[6], bonus_size[6], segsize[6];
954 	char aux[50];
955 	int error;
956 
957 	if (*print_header) {
958 		(void) printf("\n    Object  lvl   iblk   dblk  lsize"
959 		    "  asize  type\n");
960 		*print_header = 0;
961 	}
962 
963 	if (object == 0) {
964 		dn = os->os->os_meta_dnode;
965 	} else {
966 		error = dmu_bonus_hold(os, object, FTAG, &db);
967 		if (error)
968 			fatal("dmu_bonus_hold(%llu) failed, errno %u",
969 			    object, error);
970 		bonus = db->db_data;
971 		bsize = db->db_size;
972 		dn = ((dmu_buf_impl_t *)db)->db_dnode;
973 	}
974 	dmu_object_info_from_dnode(dn, &doi);
975 
976 	nicenum(doi.doi_metadata_block_size, iblk);
977 	nicenum(doi.doi_data_block_size, dblk);
978 	nicenum(doi.doi_data_block_size * (doi.doi_max_block_offset + 1),
979 	    lsize);
980 	nicenum(doi.doi_physical_blks << 9, asize);
981 	nicenum(doi.doi_bonus_size, bonus_size);
982 
983 	aux[0] = '\0';
984 
985 	if (doi.doi_checksum != ZIO_CHECKSUM_INHERIT || verbosity >= 6) {
986 		(void) snprintf(aux + strlen(aux), sizeof (aux), " (K=%s)",
987 		    zio_checksum_table[doi.doi_checksum].ci_name);
988 	}
989 
990 	if (doi.doi_compress != ZIO_COMPRESS_INHERIT || verbosity >= 6) {
991 		(void) snprintf(aux + strlen(aux), sizeof (aux), " (Z=%s)",
992 		    zio_compress_table[doi.doi_compress].ci_name);
993 	}
994 
995 	(void) printf("%10lld  %3u  %5s  %5s  %5s  %5s  %s%s\n",
996 	    (u_longlong_t)object, doi.doi_indirection, iblk, dblk, lsize,
997 	    asize, dmu_ot[doi.doi_type].ot_name, aux);
998 
999 	if (doi.doi_bonus_type != DMU_OT_NONE && verbosity > 3) {
1000 		(void) printf("%10s  %3s  %5s  %5s  %5s  %5s  %s\n",
1001 		    "", "", "", "", bonus_size, "bonus",
1002 		    dmu_ot[doi.doi_bonus_type].ot_name);
1003 	}
1004 
1005 	if (verbosity >= 4) {
1006 		object_viewer[doi.doi_bonus_type](os, object, bonus, bsize);
1007 		object_viewer[doi.doi_type](os, object, NULL, 0);
1008 		*print_header = 1;
1009 	}
1010 
1011 	if (verbosity >= 5)
1012 		dump_indirect(os, object, NULL, 0);
1013 
1014 	if (verbosity >= 5) {
1015 		/*
1016 		 * Report the list of segments that comprise the object.
1017 		 */
1018 		uint64_t start = 0;
1019 		uint64_t end;
1020 		uint64_t blkfill = 1;
1021 		int minlvl = 1;
1022 
1023 		if (dn->dn_type == DMU_OT_DNODE) {
1024 			minlvl = 0;
1025 			blkfill = DNODES_PER_BLOCK;
1026 		}
1027 
1028 		for (;;) {
1029 			error = dnode_next_offset(dn, B_FALSE, &start, minlvl,
1030 			    blkfill, 0);
1031 			if (error)
1032 				break;
1033 			end = start;
1034 			error = dnode_next_offset(dn, B_TRUE, &end, minlvl,
1035 			    blkfill, 0);
1036 			nicenum(end - start, segsize);
1037 			(void) printf("\t\tsegment [%016llx, %016llx)"
1038 			    " size %5s\n", (u_longlong_t)start,
1039 			    (u_longlong_t)end, segsize);
1040 			if (error)
1041 				break;
1042 			start = end;
1043 		}
1044 	}
1045 
1046 	if (db != NULL)
1047 		dmu_buf_rele(db, FTAG);
1048 }
1049 
1050 static char *objset_types[DMU_OST_NUMTYPES] = {
1051 	"NONE", "META", "ZPL", "ZVOL", "OTHER", "ANY" };
1052 
1053 /*ARGSUSED*/
1054 static void
1055 dump_dir(objset_t *os)
1056 {
1057 	dmu_objset_stats_t dds;
1058 	uint64_t object, object_count;
1059 	uint64_t refdbytes, usedobjs, scratch;
1060 	char numbuf[8];
1061 	char blkbuf[BP_SPRINTF_LEN];
1062 	char osname[MAXNAMELEN];
1063 	char *type = "UNKNOWN";
1064 	int verbosity = dump_opt['d'];
1065 	int print_header = 1;
1066 	int i, error;
1067 
1068 	dmu_objset_fast_stat(os, &dds);
1069 
1070 	if (dds.dds_type < DMU_OST_NUMTYPES)
1071 		type = objset_types[dds.dds_type];
1072 
1073 	if (dds.dds_type == DMU_OST_META) {
1074 		dds.dds_creation_txg = TXG_INITIAL;
1075 		usedobjs = os->os->os_rootbp->blk_fill;
1076 		refdbytes =
1077 		    os->os->os_spa->spa_dsl_pool->dp_mos_dir->dd_used_bytes;
1078 	} else {
1079 		dmu_objset_space(os, &refdbytes, &scratch, &usedobjs, &scratch);
1080 	}
1081 
1082 	ASSERT3U(usedobjs, ==, os->os->os_rootbp->blk_fill);
1083 
1084 	nicenum(refdbytes, numbuf);
1085 
1086 	if (verbosity >= 4) {
1087 		(void) strcpy(blkbuf, ", rootbp ");
1088 		sprintf_blkptr(blkbuf + strlen(blkbuf),
1089 		    BP_SPRINTF_LEN - strlen(blkbuf), os->os->os_rootbp);
1090 	} else {
1091 		blkbuf[0] = '\0';
1092 	}
1093 
1094 	dmu_objset_name(os, osname);
1095 
1096 	(void) printf("Dataset %s [%s], ID %llu, cr_txg %llu, "
1097 	    "%s, %llu objects%s\n",
1098 	    osname, type, (u_longlong_t)dmu_objset_id(os),
1099 	    (u_longlong_t)dds.dds_creation_txg,
1100 	    numbuf, (u_longlong_t)usedobjs, blkbuf);
1101 
1102 	dump_intent_log(dmu_objset_zil(os));
1103 
1104 	if (dmu_objset_ds(os) != NULL)
1105 		dump_bplist(dmu_objset_pool(os)->dp_meta_objset,
1106 		    dmu_objset_ds(os)->ds_phys->ds_deadlist_obj, "Deadlist");
1107 
1108 	if (verbosity < 2)
1109 		return;
1110 
1111 	if (zopt_objects != 0) {
1112 		for (i = 0; i < zopt_objects; i++)
1113 			dump_object(os, zopt_object[i], verbosity,
1114 			    &print_header);
1115 		(void) printf("\n");
1116 		return;
1117 	}
1118 
1119 	dump_object(os, 0, verbosity, &print_header);
1120 	object_count = 1;
1121 
1122 	object = 0;
1123 	while ((error = dmu_object_next(os, &object, B_FALSE, 0)) == 0) {
1124 		dump_object(os, object, verbosity, &print_header);
1125 		object_count++;
1126 	}
1127 
1128 	ASSERT3U(object_count, ==, usedobjs);
1129 
1130 	(void) printf("\n");
1131 
1132 	if (error != ESRCH)
1133 		fatal("dmu_object_next() = %d", error);
1134 }
1135 
1136 static void
1137 dump_uberblock(uberblock_t *ub)
1138 {
1139 	time_t timestamp = ub->ub_timestamp;
1140 
1141 	(void) printf("Uberblock\n\n");
1142 	(void) printf("\tmagic = %016llx\n", (u_longlong_t)ub->ub_magic);
1143 	(void) printf("\tversion = %llu\n", (u_longlong_t)ub->ub_version);
1144 	(void) printf("\ttxg = %llu\n", (u_longlong_t)ub->ub_txg);
1145 	(void) printf("\tguid_sum = %llu\n", (u_longlong_t)ub->ub_guid_sum);
1146 	(void) printf("\ttimestamp = %llu UTC = %s",
1147 	    (u_longlong_t)ub->ub_timestamp, asctime(localtime(&timestamp)));
1148 	if (dump_opt['u'] >= 3) {
1149 		char blkbuf[BP_SPRINTF_LEN];
1150 		sprintf_blkptr(blkbuf, BP_SPRINTF_LEN, &ub->ub_rootbp);
1151 		(void) printf("\trootbp = %s\n", blkbuf);
1152 	}
1153 	(void) printf("\n");
1154 }
1155 
1156 static void
1157 dump_config(const char *pool)
1158 {
1159 	spa_t *spa = NULL;
1160 
1161 	mutex_enter(&spa_namespace_lock);
1162 	while ((spa = spa_next(spa)) != NULL) {
1163 		if (pool == NULL)
1164 			(void) printf("%s\n", spa_name(spa));
1165 		if (pool == NULL || strcmp(pool, spa_name(spa)) == 0)
1166 			dump_nvlist(spa->spa_config, 4);
1167 	}
1168 	mutex_exit(&spa_namespace_lock);
1169 }
1170 
1171 static void
1172 dump_label(const char *dev)
1173 {
1174 	int fd;
1175 	vdev_label_t label;
1176 	char *buf = label.vl_vdev_phys.vp_nvlist;
1177 	size_t buflen = sizeof (label.vl_vdev_phys.vp_nvlist);
1178 	struct stat64 statbuf;
1179 	uint64_t psize;
1180 	int l;
1181 
1182 	if ((fd = open64(dev, O_RDONLY)) < 0) {
1183 		(void) printf("cannot open '%s': %s\n", dev, strerror(errno));
1184 		exit(1);
1185 	}
1186 
1187 	if (fstat64(fd, &statbuf) != 0) {
1188 		(void) printf("failed to stat '%s': %s\n", dev,
1189 		    strerror(errno));
1190 		exit(1);
1191 	}
1192 
1193 	psize = statbuf.st_size;
1194 	psize = P2ALIGN(psize, (uint64_t)sizeof (vdev_label_t));
1195 
1196 	for (l = 0; l < VDEV_LABELS; l++) {
1197 
1198 		nvlist_t *config = NULL;
1199 
1200 		(void) printf("--------------------------------------------\n");
1201 		(void) printf("LABEL %d\n", l);
1202 		(void) printf("--------------------------------------------\n");
1203 
1204 		if (pread64(fd, &label, sizeof (label),
1205 		    vdev_label_offset(psize, l, 0)) != sizeof (label)) {
1206 			(void) printf("failed to read label %d\n", l);
1207 			continue;
1208 		}
1209 
1210 		if (nvlist_unpack(buf, buflen, &config, 0) != 0) {
1211 			(void) printf("failed to unpack label %d\n", l);
1212 			continue;
1213 		}
1214 		dump_nvlist(config, 4);
1215 		nvlist_free(config);
1216 	}
1217 }
1218 
1219 /*ARGSUSED*/
1220 static int
1221 dump_one_dir(char *dsname, void *arg)
1222 {
1223 	int error;
1224 	objset_t *os;
1225 
1226 	error = dmu_objset_open(dsname, DMU_OST_ANY,
1227 	    DS_MODE_STANDARD | DS_MODE_READONLY, &os);
1228 	if (error) {
1229 		(void) printf("Could not open %s\n", dsname);
1230 		return (0);
1231 	}
1232 	dump_dir(os);
1233 	dmu_objset_close(os);
1234 	return (0);
1235 }
1236 
1237 static void
1238 zdb_space_map_load(spa_t *spa)
1239 {
1240 	vdev_t *rvd = spa->spa_root_vdev;
1241 	vdev_t *vd;
1242 	int c, m, error;
1243 
1244 	for (c = 0; c < rvd->vdev_children; c++) {
1245 		vd = rvd->vdev_child[c];
1246 		for (m = 0; m < vd->vdev_ms_count; m++) {
1247 			metaslab_t *msp = vd->vdev_ms[m];
1248 			mutex_enter(&msp->ms_lock);
1249 			error = space_map_load(&msp->ms_allocmap[0], NULL,
1250 			    SM_ALLOC, &msp->ms_smo, spa->spa_meta_objset);
1251 			mutex_exit(&msp->ms_lock);
1252 			if (error)
1253 				fatal("%s bad space map #%d, error %d",
1254 				    spa->spa_name, c, error);
1255 		}
1256 	}
1257 }
1258 
1259 static int
1260 zdb_space_map_claim(spa_t *spa, blkptr_t *bp, zbookmark_t *zb)
1261 {
1262 	dva_t *dva = bp->blk_dva;
1263 	vdev_t *vd;
1264 	metaslab_t *msp;
1265 	space_map_t *allocmap, *freemap;
1266 	int error;
1267 	int d;
1268 	blkptr_t blk = *bp;
1269 
1270 	for (d = 0; d < BP_GET_NDVAS(bp); d++) {
1271 		uint64_t vdev = DVA_GET_VDEV(&dva[d]);
1272 		uint64_t offset = DVA_GET_OFFSET(&dva[d]);
1273 		uint64_t size = DVA_GET_ASIZE(&dva[d]);
1274 
1275 		if ((vd = vdev_lookup_top(spa, vdev)) == NULL)
1276 			return (ENXIO);
1277 
1278 		if ((offset >> vd->vdev_ms_shift) >= vd->vdev_ms_count)
1279 			return (ENXIO);
1280 
1281 		msp = vd->vdev_ms[offset >> vd->vdev_ms_shift];
1282 		allocmap = &msp->ms_allocmap[0];
1283 		freemap = &msp->ms_freemap[0];
1284 
1285 		/* Prepare our copy of the bp in case we need to read GBHs */
1286 		if (DVA_GET_GANG(&dva[d])) {
1287 			size = vdev_psize_to_asize(vd, SPA_GANGBLOCKSIZE);
1288 			DVA_SET_ASIZE(&blk.blk_dva[d], size);
1289 			DVA_SET_GANG(&blk.blk_dva[d], 0);
1290 		}
1291 
1292 		mutex_enter(&msp->ms_lock);
1293 		if (space_map_contains(freemap, offset, size)) {
1294 			mutex_exit(&msp->ms_lock);
1295 			return (EAGAIN);	/* allocated more than once */
1296 		}
1297 
1298 		if (!space_map_contains(allocmap, offset, size)) {
1299 			mutex_exit(&msp->ms_lock);
1300 			return (ESTALE);	/* not allocated at all */
1301 		}
1302 
1303 		space_map_remove(allocmap, offset, size);
1304 		space_map_add(freemap, offset, size);
1305 
1306 		mutex_exit(&msp->ms_lock);
1307 	}
1308 
1309 	if (BP_IS_GANG(bp)) {
1310 		zio_gbh_phys_t gbh;
1311 		int g;
1312 
1313 		/* LINTED - compile time assert */
1314 		ASSERT(sizeof (zio_gbh_phys_t) == SPA_GANGBLOCKSIZE);
1315 
1316 		BP_SET_CHECKSUM(&blk, ZIO_CHECKSUM_GANG_HEADER);
1317 		BP_SET_PSIZE(&blk, SPA_GANGBLOCKSIZE);
1318 		BP_SET_LSIZE(&blk, SPA_GANGBLOCKSIZE);
1319 		BP_SET_COMPRESS(&blk, ZIO_COMPRESS_OFF);
1320 		error = zio_wait(zio_read(NULL, spa, &blk, &gbh,
1321 		    SPA_GANGBLOCKSIZE, NULL, NULL, ZIO_PRIORITY_SYNC_READ,
1322 		    ZIO_FLAG_CANFAIL | ZIO_FLAG_CONFIG_HELD, zb));
1323 		if (error)
1324 			return (error);
1325 		if (BP_SHOULD_BYTESWAP(&blk))
1326 			byteswap_uint64_array(&gbh, SPA_GANGBLOCKSIZE);
1327 		for (g = 0; g < SPA_GBH_NBLKPTRS; g++) {
1328 			if (BP_IS_HOLE(&gbh.zg_blkptr[g]))
1329 				break;
1330 			error = zdb_space_map_claim(spa, &gbh.zg_blkptr[g], zb);
1331 			if (error)
1332 				return (error);
1333 		}
1334 	}
1335 
1336 	return (0);
1337 }
1338 
1339 static void
1340 zdb_leak(space_map_t *sm, uint64_t start, uint64_t size)
1341 {
1342 	metaslab_t *msp;
1343 
1344 	/* LINTED */
1345 	msp = (metaslab_t *)((char *)sm - offsetof(metaslab_t, ms_allocmap[0]));
1346 
1347 	(void) printf("leaked space: vdev %llu, offset 0x%llx, size %llu\n",
1348 	    (u_longlong_t)msp->ms_group->mg_vd->vdev_id,
1349 	    (u_longlong_t)start,
1350 	    (u_longlong_t)size);
1351 }
1352 
1353 static void
1354 zdb_space_map_unload(spa_t *spa)
1355 {
1356 	vdev_t *rvd = spa->spa_root_vdev;
1357 	vdev_t *vd;
1358 	int c, m;
1359 
1360 	for (c = 0; c < rvd->vdev_children; c++) {
1361 		vd = rvd->vdev_child[c];
1362 		for (m = 0; m < vd->vdev_ms_count; m++) {
1363 			metaslab_t *msp = vd->vdev_ms[m];
1364 			mutex_enter(&msp->ms_lock);
1365 			space_map_vacate(&msp->ms_allocmap[0], zdb_leak,
1366 			    &msp->ms_allocmap[0]);
1367 			space_map_unload(&msp->ms_allocmap[0]);
1368 			space_map_vacate(&msp->ms_freemap[0], NULL, NULL);
1369 			mutex_exit(&msp->ms_lock);
1370 		}
1371 	}
1372 }
1373 
1374 static void
1375 zdb_refresh_ubsync(spa_t *spa)
1376 {
1377 	uberblock_t ub = { 0 };
1378 	vdev_t *rvd = spa->spa_root_vdev;
1379 	zio_t *zio;
1380 
1381 	/*
1382 	 * Reload the uberblock.
1383 	 */
1384 	zio = zio_root(spa, NULL, NULL,
1385 	    ZIO_FLAG_CANFAIL | ZIO_FLAG_SPECULATIVE);
1386 	vdev_uberblock_load(zio, rvd, &ub);
1387 	(void) zio_wait(zio);
1388 
1389 	if (ub.ub_txg != 0)
1390 		spa->spa_ubsync = ub;
1391 }
1392 
1393 /*
1394  * Verify that the sum of the sizes of all blocks in the pool adds up
1395  * to the SPA's sa_alloc total.
1396  */
1397 typedef struct zdb_blkstats {
1398 	uint64_t	zb_asize;
1399 	uint64_t	zb_lsize;
1400 	uint64_t	zb_psize;
1401 	uint64_t	zb_count;
1402 } zdb_blkstats_t;
1403 
1404 #define	DMU_OT_DEFERRED	DMU_OT_NONE
1405 #define	DMU_OT_TOTAL	DMU_OT_NUMTYPES
1406 
1407 #define	ZB_TOTAL	ZB_MAXLEVEL
1408 
1409 typedef struct zdb_cb {
1410 	zdb_blkstats_t	zcb_type[ZB_TOTAL + 1][DMU_OT_TOTAL + 1];
1411 	uint64_t	zcb_errors[256];
1412 	traverse_blk_cache_t *zcb_cache;
1413 	int		zcb_readfails;
1414 	int		zcb_haderrors;
1415 } zdb_cb_t;
1416 
1417 static void
1418 zdb_count_block(spa_t *spa, zdb_cb_t *zcb, blkptr_t *bp, int type)
1419 {
1420 	int i, error;
1421 
1422 	for (i = 0; i < 4; i++) {
1423 		int l = (i < 2) ? BP_GET_LEVEL(bp) : ZB_TOTAL;
1424 		int t = (i & 1) ? type : DMU_OT_TOTAL;
1425 		zdb_blkstats_t *zb = &zcb->zcb_type[l][t];
1426 
1427 		zb->zb_asize += BP_GET_ASIZE(bp);
1428 		zb->zb_lsize += BP_GET_LSIZE(bp);
1429 		zb->zb_psize += BP_GET_PSIZE(bp);
1430 		zb->zb_count++;
1431 	}
1432 
1433 	if (dump_opt['L'])
1434 		return;
1435 
1436 	error = zdb_space_map_claim(spa, bp, &zcb->zcb_cache->bc_bookmark);
1437 
1438 	if (error == 0)
1439 		return;
1440 
1441 	if (error == EAGAIN)
1442 		(void) fatal("double-allocation, bp=%p", bp);
1443 
1444 	if (error == ESTALE)
1445 		(void) fatal("reference to freed block, bp=%p", bp);
1446 
1447 	(void) fatal("fatal error %d in bp %p", error, bp);
1448 }
1449 
1450 static int
1451 zdb_blkptr_cb(traverse_blk_cache_t *bc, spa_t *spa, void *arg)
1452 {
1453 	zbookmark_t *zb = &bc->bc_bookmark;
1454 	zdb_cb_t *zcb = arg;
1455 	blkptr_t *bp = &bc->bc_blkptr;
1456 	dmu_object_type_t type = BP_GET_TYPE(bp);
1457 	char blkbuf[BP_SPRINTF_LEN];
1458 	int error = 0;
1459 
1460 	if (bc->bc_errno) {
1461 		if (zcb->zcb_readfails++ < 10 && dump_opt['L']) {
1462 			zdb_refresh_ubsync(spa);
1463 			error = EAGAIN;
1464 		} else {
1465 			zcb->zcb_haderrors = 1;
1466 			zcb->zcb_errors[bc->bc_errno]++;
1467 			error = ERESTART;
1468 		}
1469 
1470 		if (dump_opt['b'] >= 3 || (dump_opt['b'] >= 2 && bc->bc_errno))
1471 			sprintf_blkptr(blkbuf, BP_SPRINTF_LEN, bp);
1472 		else
1473 			blkbuf[0] = '\0';
1474 
1475 		(void) printf("zdb_blkptr_cb: Got error %d reading "
1476 		    "<%llu, %llu, %lld, %llx> %s -- %s\n",
1477 		    bc->bc_errno,
1478 		    (u_longlong_t)zb->zb_objset,
1479 		    (u_longlong_t)zb->zb_object,
1480 		    (u_longlong_t)zb->zb_level,
1481 		    (u_longlong_t)zb->zb_blkid,
1482 		    blkbuf,
1483 		    error == EAGAIN ? "retrying" : "skipping");
1484 
1485 		return (error);
1486 	}
1487 
1488 	zcb->zcb_readfails = 0;
1489 
1490 	ASSERT(!BP_IS_HOLE(bp));
1491 
1492 	if (dump_opt['b'] >= 4) {
1493 		sprintf_blkptr(blkbuf, BP_SPRINTF_LEN, bp);
1494 		(void) printf("objset %llu object %llu offset 0x%llx %s\n",
1495 		    (u_longlong_t)zb->zb_objset,
1496 		    (u_longlong_t)zb->zb_object,
1497 		    (u_longlong_t)blkid2offset(bc->bc_dnode,
1498 		    zb->zb_level, zb->zb_blkid), blkbuf);
1499 	}
1500 
1501 	zdb_count_block(spa, zcb, bp, type);
1502 
1503 	return (0);
1504 }
1505 
1506 static int
1507 dump_block_stats(spa_t *spa)
1508 {
1509 	traverse_handle_t *th;
1510 	zdb_cb_t zcb = { 0 };
1511 	traverse_blk_cache_t dummy_cache = { 0 };
1512 	zdb_blkstats_t *zb, *tzb;
1513 	uint64_t alloc, space, logalloc;
1514 	vdev_t *rvd = spa->spa_root_vdev;
1515 	int leaks = 0;
1516 	int advance = zdb_advance;
1517 	int c, e, flags;
1518 
1519 	zcb.zcb_cache = &dummy_cache;
1520 
1521 	if (dump_opt['c'])
1522 		advance |= ADVANCE_DATA;
1523 
1524 	advance |= ADVANCE_PRUNE | ADVANCE_ZIL;
1525 
1526 	(void) printf("\nTraversing all blocks to %sverify"
1527 	    " nothing leaked ...\n",
1528 	    dump_opt['c'] ? "verify checksums and " : "");
1529 
1530 	/*
1531 	 * Load all space maps.  As we traverse the pool, if we find a block
1532 	 * that's not in its space map, that indicates a double-allocation,
1533 	 * reference to a freed block, or an unclaimed block.  Otherwise we
1534 	 * remove the block from the space map.  If the space maps are not
1535 	 * empty when we're done, that indicates leaked blocks.
1536 	 */
1537 	if (!dump_opt['L'])
1538 		zdb_space_map_load(spa);
1539 
1540 	/*
1541 	 * If there's a deferred-free bplist, process that first.
1542 	 */
1543 	if (spa->spa_sync_bplist_obj != 0) {
1544 		bplist_t *bpl = &spa->spa_sync_bplist;
1545 		blkptr_t blk;
1546 		uint64_t itor = 0;
1547 
1548 		VERIFY(0 == bplist_open(bpl, spa->spa_meta_objset,
1549 		    spa->spa_sync_bplist_obj));
1550 
1551 		while (bplist_iterate(bpl, &itor, &blk) == 0) {
1552 			if (dump_opt['b'] >= 4) {
1553 				char blkbuf[BP_SPRINTF_LEN];
1554 				sprintf_blkptr(blkbuf, BP_SPRINTF_LEN, &blk);
1555 				(void) printf("[%s] %s\n",
1556 				    "deferred free", blkbuf);
1557 			}
1558 			zdb_count_block(spa, &zcb, &blk, DMU_OT_DEFERRED);
1559 		}
1560 
1561 		bplist_close(bpl);
1562 	}
1563 
1564 	/*
1565 	 * Now traverse the pool.  If we're reading all data to verify
1566 	 * checksums, do a scrubbing read so that we validate all copies.
1567 	 */
1568 	flags = ZIO_FLAG_CANFAIL;
1569 	if (advance & ADVANCE_DATA)
1570 		flags |= ZIO_FLAG_SCRUB;
1571 	th = traverse_init(spa, zdb_blkptr_cb, &zcb, advance, flags);
1572 	th->th_noread = zdb_noread;
1573 
1574 	traverse_add_pool(th, 0, spa_first_txg(spa) + TXG_CONCURRENT_STATES);
1575 
1576 	while (traverse_more(th) == EAGAIN)
1577 		continue;
1578 
1579 	traverse_fini(th);
1580 
1581 	if (zcb.zcb_haderrors) {
1582 		(void) printf("\nError counts:\n\n");
1583 		(void) printf("\t%5s  %s\n", "errno", "count");
1584 		for (e = 0; e < 256; e++) {
1585 			if (zcb.zcb_errors[e] != 0) {
1586 				(void) printf("\t%5d  %llu\n",
1587 				    e, (u_longlong_t)zcb.zcb_errors[e]);
1588 			}
1589 		}
1590 	}
1591 
1592 	/*
1593 	 * Report any leaked segments.
1594 	 */
1595 	if (!dump_opt['L'])
1596 		zdb_space_map_unload(spa);
1597 
1598 	if (dump_opt['L'])
1599 		(void) printf("\n\n *** Live pool traversal; "
1600 		    "block counts are only approximate ***\n\n");
1601 
1602 	alloc = spa_get_alloc(spa);
1603 	space = spa_get_space(spa);
1604 
1605 	/*
1606 	 * Log blocks allocated from a separate log device don't count
1607 	 * as part of the normal pool space; factor them in here.
1608 	 */
1609 	logalloc = 0;
1610 
1611 	for (c = 0; c < rvd->vdev_children; c++)
1612 		if (rvd->vdev_child[c]->vdev_islog)
1613 			logalloc += rvd->vdev_child[c]->vdev_stat.vs_alloc;
1614 
1615 	tzb = &zcb.zcb_type[ZB_TOTAL][DMU_OT_TOTAL];
1616 
1617 	if (tzb->zb_asize == alloc + logalloc) {
1618 		(void) printf("\n\tNo leaks (block sum matches space"
1619 		    " maps exactly)\n");
1620 	} else {
1621 		(void) printf("block traversal size %llu != alloc %llu "
1622 		    "(leaked %lld)\n",
1623 		    (u_longlong_t)tzb->zb_asize,
1624 		    (u_longlong_t)alloc + logalloc,
1625 		    (u_longlong_t)(alloc + logalloc - tzb->zb_asize));
1626 		leaks = 1;
1627 	}
1628 
1629 	if (tzb->zb_count == 0)
1630 		return (2);
1631 
1632 	(void) printf("\n");
1633 	(void) printf("\tbp count:      %10llu\n",
1634 	    (u_longlong_t)tzb->zb_count);
1635 	(void) printf("\tbp logical:    %10llu\t avg: %6llu\n",
1636 	    (u_longlong_t)tzb->zb_lsize,
1637 	    (u_longlong_t)(tzb->zb_lsize / tzb->zb_count));
1638 	(void) printf("\tbp physical:   %10llu\t avg:"
1639 	    " %6llu\tcompression: %6.2f\n",
1640 	    (u_longlong_t)tzb->zb_psize,
1641 	    (u_longlong_t)(tzb->zb_psize / tzb->zb_count),
1642 	    (double)tzb->zb_lsize / tzb->zb_psize);
1643 	(void) printf("\tbp allocated:  %10llu\t avg:"
1644 	    " %6llu\tcompression: %6.2f\n",
1645 	    (u_longlong_t)tzb->zb_asize,
1646 	    (u_longlong_t)(tzb->zb_asize / tzb->zb_count),
1647 	    (double)tzb->zb_lsize / tzb->zb_asize);
1648 	(void) printf("\tSPA allocated: %10llu\tused: %5.2f%%\n",
1649 	    (u_longlong_t)alloc, 100.0 * alloc / space);
1650 
1651 	if (dump_opt['b'] >= 2) {
1652 		int l, t, level;
1653 		(void) printf("\nBlocks\tLSIZE\tPSIZE\tASIZE"
1654 		    "\t  avg\t comp\t%%Total\tType\n");
1655 
1656 		for (t = 0; t <= DMU_OT_NUMTYPES; t++) {
1657 			char csize[6], lsize[6], psize[6], asize[6], avg[6];
1658 			char *typename;
1659 
1660 			typename = t == DMU_OT_DEFERRED ? "deferred free" :
1661 			    t == DMU_OT_TOTAL ? "Total" : dmu_ot[t].ot_name;
1662 
1663 			if (zcb.zcb_type[ZB_TOTAL][t].zb_asize == 0) {
1664 				(void) printf("%6s\t%5s\t%5s\t%5s"
1665 				    "\t%5s\t%5s\t%6s\t%s\n",
1666 				    "-",
1667 				    "-",
1668 				    "-",
1669 				    "-",
1670 				    "-",
1671 				    "-",
1672 				    "-",
1673 				    typename);
1674 				continue;
1675 			}
1676 
1677 			for (l = ZB_TOTAL - 1; l >= -1; l--) {
1678 				level = (l == -1 ? ZB_TOTAL : l);
1679 				zb = &zcb.zcb_type[level][t];
1680 
1681 				if (zb->zb_asize == 0)
1682 					continue;
1683 
1684 				if (dump_opt['b'] < 3 && level != ZB_TOTAL)
1685 					continue;
1686 
1687 				if (level == 0 && zb->zb_asize ==
1688 				    zcb.zcb_type[ZB_TOTAL][t].zb_asize)
1689 					continue;
1690 
1691 				nicenum(zb->zb_count, csize);
1692 				nicenum(zb->zb_lsize, lsize);
1693 				nicenum(zb->zb_psize, psize);
1694 				nicenum(zb->zb_asize, asize);
1695 				nicenum(zb->zb_asize / zb->zb_count, avg);
1696 
1697 				(void) printf("%6s\t%5s\t%5s\t%5s\t%5s"
1698 				    "\t%5.2f\t%6.2f\t",
1699 				    csize, lsize, psize, asize, avg,
1700 				    (double)zb->zb_lsize / zb->zb_psize,
1701 				    100.0 * zb->zb_asize / tzb->zb_asize);
1702 
1703 				if (level == ZB_TOTAL)
1704 					(void) printf("%s\n", typename);
1705 				else
1706 					(void) printf("    L%d %s\n",
1707 					    level, typename);
1708 			}
1709 		}
1710 	}
1711 
1712 	(void) printf("\n");
1713 
1714 	if (leaks)
1715 		return (2);
1716 
1717 	if (zcb.zcb_haderrors)
1718 		return (3);
1719 
1720 	return (0);
1721 }
1722 
1723 static void
1724 dump_zpool(spa_t *spa)
1725 {
1726 	dsl_pool_t *dp = spa_get_dsl(spa);
1727 	int rc = 0;
1728 
1729 	if (dump_opt['u'])
1730 		dump_uberblock(&spa->spa_uberblock);
1731 
1732 	if (dump_opt['d'] || dump_opt['i']) {
1733 		dump_dir(dp->dp_meta_objset);
1734 		if (dump_opt['d'] >= 3) {
1735 			dump_bplist(dp->dp_meta_objset,
1736 			    spa->spa_sync_bplist_obj, "Deferred frees");
1737 			dump_dtl(spa->spa_root_vdev, 0);
1738 			dump_metaslabs(spa);
1739 		}
1740 		(void) dmu_objset_find(spa->spa_name, dump_one_dir, NULL,
1741 		    DS_FIND_SNAPSHOTS | DS_FIND_CHILDREN);
1742 	}
1743 
1744 	if (dump_opt['b'] || dump_opt['c'])
1745 		rc = dump_block_stats(spa);
1746 
1747 	if (dump_opt['s'])
1748 		show_pool_stats(spa);
1749 
1750 	if (rc != 0)
1751 		exit(rc);
1752 }
1753 
1754 #define	ZDB_FLAG_CHECKSUM	0x0001
1755 #define	ZDB_FLAG_DECOMPRESS	0x0002
1756 #define	ZDB_FLAG_BSWAP		0x0004
1757 #define	ZDB_FLAG_GBH		0x0008
1758 #define	ZDB_FLAG_INDIRECT	0x0010
1759 #define	ZDB_FLAG_PHYS		0x0020
1760 #define	ZDB_FLAG_RAW		0x0040
1761 #define	ZDB_FLAG_PRINT_BLKPTR	0x0080
1762 
1763 int flagbits[256];
1764 
1765 static void
1766 zdb_print_blkptr(blkptr_t *bp, int flags)
1767 {
1768 	dva_t *dva = bp->blk_dva;
1769 	int d;
1770 
1771 	if (flags & ZDB_FLAG_BSWAP)
1772 		byteswap_uint64_array((void *)bp, sizeof (blkptr_t));
1773 	/*
1774 	 * Super-ick warning:  This code is also duplicated in
1775 	 * cmd/mdb/common/modules/zfs/zfs.c .  Yeah, I hate code
1776 	 * replication, too.
1777 	 */
1778 	for (d = 0; d < BP_GET_NDVAS(bp); d++) {
1779 		(void) printf("\tDVA[%d]: vdev_id %lld / %llx\n", d,
1780 		    (longlong_t)DVA_GET_VDEV(&dva[d]),
1781 		    (longlong_t)DVA_GET_OFFSET(&dva[d]));
1782 		(void) printf("\tDVA[%d]:       GANG: %-5s  GRID:  %04llx\t"
1783 		    "ASIZE: %llx\n", d,
1784 		    DVA_GET_GANG(&dva[d]) ? "TRUE" : "FALSE",
1785 		    (longlong_t)DVA_GET_GRID(&dva[d]),
1786 		    (longlong_t)DVA_GET_ASIZE(&dva[d]));
1787 		(void) printf("\tDVA[%d]: :%llu:%llx:%llx:%s%s%s%s\n", d,
1788 		    (u_longlong_t)DVA_GET_VDEV(&dva[d]),
1789 		    (longlong_t)DVA_GET_OFFSET(&dva[d]),
1790 		    (longlong_t)BP_GET_PSIZE(bp),
1791 		    BP_SHOULD_BYTESWAP(bp) ? "e" : "",
1792 		    !DVA_GET_GANG(&dva[d]) && BP_GET_LEVEL(bp) != 0 ?
1793 		    "d" : "",
1794 		    DVA_GET_GANG(&dva[d]) ? "g" : "",
1795 		    BP_GET_COMPRESS(bp) != 0 ? "d" : "");
1796 	}
1797 	(void) printf("\tLSIZE:  %-16llx\t\tPSIZE: %llx\n",
1798 	    (longlong_t)BP_GET_LSIZE(bp), (longlong_t)BP_GET_PSIZE(bp));
1799 	(void) printf("\tENDIAN: %6s\t\t\t\t\tTYPE:  %s\n",
1800 	    BP_GET_BYTEORDER(bp) ? "LITTLE" : "BIG",
1801 	    dmu_ot[BP_GET_TYPE(bp)].ot_name);
1802 	(void) printf("\tBIRTH:  %-16llx   LEVEL: %-2llu\tFILL:  %llx\n",
1803 	    (u_longlong_t)bp->blk_birth, (u_longlong_t)BP_GET_LEVEL(bp),
1804 	    (u_longlong_t)bp->blk_fill);
1805 	(void) printf("\tCKFUNC: %-16s\t\tCOMP:  %s\n",
1806 	    zio_checksum_table[BP_GET_CHECKSUM(bp)].ci_name,
1807 	    zio_compress_table[BP_GET_COMPRESS(bp)].ci_name);
1808 	(void) printf("\tCKSUM:  %llx:%llx:%llx:%llx\n",
1809 	    (u_longlong_t)bp->blk_cksum.zc_word[0],
1810 	    (u_longlong_t)bp->blk_cksum.zc_word[1],
1811 	    (u_longlong_t)bp->blk_cksum.zc_word[2],
1812 	    (u_longlong_t)bp->blk_cksum.zc_word[3]);
1813 }
1814 
1815 static void
1816 zdb_dump_indirect(blkptr_t *bp, int nbps, int flags)
1817 {
1818 	int i;
1819 
1820 	for (i = 0; i < nbps; i++)
1821 		zdb_print_blkptr(&bp[i], flags);
1822 }
1823 
1824 static void
1825 zdb_dump_gbh(void *buf, int flags)
1826 {
1827 	zdb_dump_indirect((blkptr_t *)buf, SPA_GBH_NBLKPTRS, flags);
1828 }
1829 
1830 static void
1831 zdb_dump_block_raw(void *buf, uint64_t size, int flags)
1832 {
1833 	if (flags & ZDB_FLAG_BSWAP)
1834 		byteswap_uint64_array(buf, size);
1835 	(void) write(2, buf, size);
1836 }
1837 
1838 static void
1839 zdb_dump_block(char *label, void *buf, uint64_t size, int flags)
1840 {
1841 	uint64_t *d = (uint64_t *)buf;
1842 	int nwords = size / sizeof (uint64_t);
1843 	int do_bswap = !!(flags & ZDB_FLAG_BSWAP);
1844 	int i, j;
1845 	char *hdr, *c;
1846 
1847 
1848 	if (do_bswap)
1849 		hdr = " 7 6 5 4 3 2 1 0   f e d c b a 9 8";
1850 	else
1851 		hdr = " 0 1 2 3 4 5 6 7   8 9 a b c d e f";
1852 
1853 	(void) printf("\n%s\n%6s   %s  0123456789abcdef\n", label, "", hdr);
1854 
1855 	for (i = 0; i < nwords; i += 2) {
1856 		(void) printf("%06llx:  %016llx  %016llx  ",
1857 		    (u_longlong_t)(i * sizeof (uint64_t)),
1858 		    (u_longlong_t)(do_bswap ? BSWAP_64(d[i]) : d[i]),
1859 		    (u_longlong_t)(do_bswap ? BSWAP_64(d[i + 1]) : d[i + 1]));
1860 
1861 		c = (char *)&d[i];
1862 		for (j = 0; j < 2 * sizeof (uint64_t); j++)
1863 			(void) printf("%c", isprint(c[j]) ? c[j] : '.');
1864 		(void) printf("\n");
1865 	}
1866 }
1867 
1868 /*
1869  * There are two acceptable formats:
1870  *	leaf_name	  - For example: c1t0d0 or /tmp/ztest.0a
1871  *	child[.child]*    - For example: 0.1.1
1872  *
1873  * The second form can be used to specify arbitrary vdevs anywhere
1874  * in the heirarchy.  For example, in a pool with a mirror of
1875  * RAID-Zs, you can specify either RAID-Z vdev with 0.0 or 0.1 .
1876  */
1877 static vdev_t *
1878 zdb_vdev_lookup(vdev_t *vdev, char *path)
1879 {
1880 	char *s, *p, *q;
1881 	int i;
1882 
1883 	if (vdev == NULL)
1884 		return (NULL);
1885 
1886 	/* First, assume the x.x.x.x format */
1887 	i = (int)strtoul(path, &s, 10);
1888 	if (s == path || (s && *s != '.' && *s != '\0'))
1889 		goto name;
1890 	if (i < 0 || i >= vdev->vdev_children)
1891 		return (NULL);
1892 
1893 	vdev = vdev->vdev_child[i];
1894 	if (*s == '\0')
1895 		return (vdev);
1896 	return (zdb_vdev_lookup(vdev, s+1));
1897 
1898 name:
1899 	for (i = 0; i < vdev->vdev_children; i++) {
1900 		vdev_t *vc = vdev->vdev_child[i];
1901 
1902 		if (vc->vdev_path == NULL) {
1903 			vc = zdb_vdev_lookup(vc, path);
1904 			if (vc == NULL)
1905 				continue;
1906 			else
1907 				return (vc);
1908 		}
1909 
1910 		p = strrchr(vc->vdev_path, '/');
1911 		p = p ? p + 1 : vc->vdev_path;
1912 		q = &vc->vdev_path[strlen(vc->vdev_path) - 2];
1913 
1914 		if (strcmp(vc->vdev_path, path) == 0)
1915 			return (vc);
1916 		if (strcmp(p, path) == 0)
1917 			return (vc);
1918 		if (strcmp(q, "s0") == 0 && strncmp(p, path, q - p) == 0)
1919 			return (vc);
1920 	}
1921 
1922 	return (NULL);
1923 }
1924 
1925 /*
1926  * Read a block from a pool and print it out.  The syntax of the
1927  * block descriptor is:
1928  *
1929  *	pool:vdev_specifier:offset:size[:flags]
1930  *
1931  *	pool           - The name of the pool you wish to read from
1932  *	vdev_specifier - Which vdev (see comment for zdb_vdev_lookup)
1933  *	offset         - offset, in hex, in bytes
1934  *	size           - Amount of data to read, in hex, in bytes
1935  *	flags          - A string of characters specifying options
1936  *		 b: Decode a blkptr at given offset within block
1937  *		*c: Calculate and display checksums
1938  *		*d: Decompress data before dumping
1939  *		 e: Byteswap data before dumping
1940  *		*g: Display data as a gang block header
1941  *		*i: Display as an indirect block
1942  *		 p: Do I/O to physical offset
1943  *		 r: Dump raw data to stdout
1944  *
1945  *              * = not yet implemented
1946  */
1947 static void
1948 zdb_read_block(char *thing, spa_t **spap)
1949 {
1950 	spa_t *spa = *spap;
1951 	int flags = 0;
1952 	uint64_t offset = 0, size = 0, blkptr_offset = 0;
1953 	zio_t *zio;
1954 	vdev_t *vd;
1955 	void *buf;
1956 	char *s, *p, *dup, *spa_name, *vdev, *flagstr;
1957 	int i, error, zio_flags;
1958 
1959 	dup = strdup(thing);
1960 	s = strtok(dup, ":");
1961 	spa_name = s ? s : "";
1962 	s = strtok(NULL, ":");
1963 	vdev = s ? s : "";
1964 	s = strtok(NULL, ":");
1965 	offset = strtoull(s ? s : "", NULL, 16);
1966 	s = strtok(NULL, ":");
1967 	size = strtoull(s ? s : "", NULL, 16);
1968 	s = strtok(NULL, ":");
1969 	flagstr = s ? s : "";
1970 
1971 	s = NULL;
1972 	if (size == 0)
1973 		s = "size must not be zero";
1974 	if (!IS_P2ALIGNED(size, DEV_BSIZE))
1975 		s = "size must be a multiple of sector size";
1976 	if (!IS_P2ALIGNED(offset, DEV_BSIZE))
1977 		s = "offset must be a multiple of sector size";
1978 	if (s) {
1979 		(void) printf("Invalid block specifier: %s  - %s\n", thing, s);
1980 		free(dup);
1981 		return;
1982 	}
1983 
1984 	for (s = strtok(flagstr, ":"); s; s = strtok(NULL, ":")) {
1985 		for (i = 0; flagstr[i]; i++) {
1986 			int bit = flagbits[(uchar_t)flagstr[i]];
1987 
1988 			if (bit == 0) {
1989 				(void) printf("***Invalid flag: %c\n",
1990 				    flagstr[i]);
1991 				continue;
1992 			}
1993 			flags |= bit;
1994 
1995 			/* If it's not something with an argument, keep going */
1996 			if ((bit & (ZDB_FLAG_CHECKSUM | ZDB_FLAG_DECOMPRESS |
1997 			    ZDB_FLAG_PRINT_BLKPTR)) == 0)
1998 				continue;
1999 
2000 			p = &flagstr[i + 1];
2001 			if (bit == ZDB_FLAG_PRINT_BLKPTR)
2002 				blkptr_offset = strtoull(p, &p, 16);
2003 			if (*p != ':' && *p != '\0') {
2004 				(void) printf("***Invalid flag arg: '%s'\n", s);
2005 				free(dup);
2006 				return;
2007 			}
2008 		}
2009 	}
2010 
2011 	if (spa == NULL || spa->spa_name == NULL ||
2012 	    strcmp(spa->spa_name, spa_name)) {
2013 		if (spa && spa->spa_name)
2014 			spa_close(spa, (void *)zdb_read_block);
2015 		error = spa_open(spa_name, spap, (void *)zdb_read_block);
2016 		if (error)
2017 			fatal("Failed to open pool '%s': %s",
2018 			    spa_name, strerror(error));
2019 		spa = *spap;
2020 	}
2021 
2022 	vd = zdb_vdev_lookup(spa->spa_root_vdev, vdev);
2023 	if (vd == NULL) {
2024 		(void) printf("***Invalid vdev: %s\n", vdev);
2025 		free(dup);
2026 		return;
2027 	} else {
2028 		if (vd->vdev_path)
2029 			(void) printf("Found vdev: %s\n", vd->vdev_path);
2030 		else
2031 			(void) printf("Found vdev type: %s\n",
2032 			    vd->vdev_ops->vdev_op_type);
2033 	}
2034 
2035 	buf = umem_alloc(size, UMEM_NOFAIL);
2036 
2037 	zio_flags = ZIO_FLAG_DONT_CACHE | ZIO_FLAG_DONT_QUEUE |
2038 	    ZIO_FLAG_DONT_PROPAGATE | ZIO_FLAG_DONT_RETRY | ZIO_FLAG_NOBOOKMARK;
2039 
2040 	if (flags & ZDB_FLAG_PHYS)
2041 		zio_flags |= ZIO_FLAG_PHYSICAL;
2042 
2043 	zio = zio_root(spa, NULL, NULL, 0);
2044 	/* XXX todo - cons up a BP so RAID-Z will be happy */
2045 	zio_nowait(zio_vdev_child_io(zio, NULL, vd, offset, buf, size,
2046 	    ZIO_TYPE_READ, ZIO_PRIORITY_SYNC_READ, zio_flags, NULL, NULL));
2047 	error = zio_wait(zio);
2048 
2049 	if (error) {
2050 		(void) printf("Read of %s failed, error: %d\n", thing, error);
2051 		goto out;
2052 	}
2053 
2054 	if (flags & ZDB_FLAG_PRINT_BLKPTR)
2055 		zdb_print_blkptr((blkptr_t *)(void *)
2056 		    ((uintptr_t)buf + (uintptr_t)blkptr_offset), flags);
2057 	else if (flags & ZDB_FLAG_RAW)
2058 		zdb_dump_block_raw(buf, size, flags);
2059 	else if (flags & ZDB_FLAG_INDIRECT)
2060 		zdb_dump_indirect((blkptr_t *)buf, size / sizeof (blkptr_t),
2061 		    flags);
2062 	else if (flags & ZDB_FLAG_GBH)
2063 		zdb_dump_gbh(buf, flags);
2064 	else
2065 		zdb_dump_block(thing, buf, size, flags);
2066 
2067 out:
2068 	umem_free(buf, size);
2069 	free(dup);
2070 }
2071 
2072 static boolean_t
2073 nvlist_string_match(nvlist_t *config, char *name, char *tgt)
2074 {
2075 	char *s;
2076 
2077 	verify(nvlist_lookup_string(config, name, &s) == 0);
2078 	return (strcmp(s, tgt) == 0);
2079 }
2080 
2081 static boolean_t
2082 nvlist_uint64_match(nvlist_t *config, char *name, uint64_t tgt)
2083 {
2084 	uint64_t val;
2085 
2086 	verify(nvlist_lookup_uint64(config, name, &val) == 0);
2087 	return (val == tgt);
2088 }
2089 
2090 static boolean_t
2091 vdev_child_guid_match(nvlist_t *vdev, uint64_t guid)
2092 {
2093 	nvlist_t **child;
2094 	uint_t c, children;
2095 
2096 	verify(nvlist_lookup_nvlist_array(vdev, ZPOOL_CONFIG_CHILDREN,
2097 	    &child, &children) == 0);
2098 	for (c = 0; c < children; ++c)
2099 		if (nvlist_uint64_match(child[c], ZPOOL_CONFIG_GUID, guid))
2100 			return (B_TRUE);
2101 	return (B_FALSE);
2102 }
2103 
2104 static boolean_t
2105 vdev_child_string_match(nvlist_t *vdev, char *tgt)
2106 {
2107 	nvlist_t **child;
2108 	uint_t c, children;
2109 
2110 	verify(nvlist_lookup_nvlist_array(vdev, ZPOOL_CONFIG_CHILDREN,
2111 	    &child, &children) == 0);
2112 	for (c = 0; c < children; ++c) {
2113 		if (nvlist_string_match(child[c], ZPOOL_CONFIG_PATH, tgt) ||
2114 		    nvlist_string_match(child[c], ZPOOL_CONFIG_DEVID, tgt))
2115 			return (B_TRUE);
2116 	}
2117 	return (B_FALSE);
2118 }
2119 
2120 static boolean_t
2121 vdev_guid_match(nvlist_t *config, uint64_t guid)
2122 {
2123 	nvlist_t *nvroot;
2124 
2125 	verify(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE,
2126 	    &nvroot) == 0);
2127 
2128 	return (nvlist_uint64_match(nvroot, ZPOOL_CONFIG_GUID, guid) ||
2129 	    vdev_child_guid_match(nvroot, guid));
2130 }
2131 
2132 static boolean_t
2133 vdev_string_match(nvlist_t *config, char *tgt)
2134 {
2135 	nvlist_t *nvroot;
2136 
2137 	verify(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE,
2138 	    &nvroot) == 0);
2139 
2140 	return (vdev_child_string_match(nvroot, tgt));
2141 }
2142 
2143 static boolean_t
2144 pool_match(nvlist_t *config, char *tgt)
2145 {
2146 	uint64_t guid = strtoull(tgt, NULL, 0);
2147 
2148 	if (guid != 0) {
2149 		return (
2150 		    nvlist_uint64_match(config, ZPOOL_CONFIG_POOL_GUID, guid) ||
2151 		    vdev_guid_match(config, guid));
2152 	} else {
2153 		return (
2154 		    nvlist_string_match(config, ZPOOL_CONFIG_POOL_NAME, tgt) ||
2155 		    vdev_string_match(config, tgt));
2156 	}
2157 }
2158 
2159 static int
2160 find_exported_zpool(char *pool_id, nvlist_t **configp, char *vdev_dir)
2161 {
2162 	nvlist_t *pools;
2163 	int error = ENOENT;
2164 	nvlist_t *match = NULL;
2165 
2166 	if (vdev_dir != NULL)
2167 		pools = zpool_find_import(g_zfs, 1, &vdev_dir);
2168 	else
2169 		pools = zpool_find_import(g_zfs, 0, NULL);
2170 
2171 	if (pools != NULL) {
2172 		nvpair_t *elem = NULL;
2173 
2174 		while ((elem = nvlist_next_nvpair(pools, elem)) != NULL) {
2175 			verify(nvpair_value_nvlist(elem, configp) == 0);
2176 			if (pool_match(*configp, pool_id)) {
2177 				if (match != NULL) {
2178 					(void) fatal(
2179 					    "More than one matching pool - "
2180 					    "specify guid/devid/device path.");
2181 				} else {
2182 					match = *configp;
2183 					error = 0;
2184 				}
2185 			}
2186 		}
2187 	}
2188 
2189 	*configp = error ? NULL : match;
2190 
2191 	return (error);
2192 }
2193 
2194 int
2195 main(int argc, char **argv)
2196 {
2197 	int i, c;
2198 	struct rlimit rl = { 1024, 1024 };
2199 	spa_t *spa;
2200 	objset_t *os = NULL;
2201 	char *endstr;
2202 	int dump_all = 1;
2203 	int verbose = 0;
2204 	int error;
2205 	int flag, set;
2206 	int exported = 0;
2207 	char *vdev_dir = NULL;
2208 
2209 	(void) setrlimit(RLIMIT_NOFILE, &rl);
2210 	(void) enable_extended_FILE_stdio(-1, -1);
2211 
2212 	dprintf_setup(&argc, argv);
2213 
2214 	while ((c = getopt(argc, argv, "udibcsvCLO:B:UlRep:")) != -1) {
2215 		switch (c) {
2216 		case 'u':
2217 		case 'd':
2218 		case 'i':
2219 		case 'b':
2220 		case 'c':
2221 		case 's':
2222 		case 'C':
2223 		case 'l':
2224 		case 'R':
2225 			dump_opt[c]++;
2226 			dump_all = 0;
2227 			break;
2228 		case 'L':
2229 			dump_opt[c]++;
2230 			break;
2231 		case 'O':
2232 			endstr = optarg;
2233 			if (endstr[0] == '!') {
2234 				endstr++;
2235 				set = 0;
2236 			} else {
2237 				set = 1;
2238 			}
2239 			if (strcmp(endstr, "post") == 0) {
2240 				flag = ADVANCE_PRE;
2241 				set = !set;
2242 			} else if (strcmp(endstr, "pre") == 0) {
2243 				flag = ADVANCE_PRE;
2244 			} else if (strcmp(endstr, "prune") == 0) {
2245 				flag = ADVANCE_PRUNE;
2246 			} else if (strcmp(endstr, "data") == 0) {
2247 				flag = ADVANCE_DATA;
2248 			} else if (strcmp(endstr, "holes") == 0) {
2249 				flag = ADVANCE_HOLES;
2250 			} else {
2251 				usage();
2252 			}
2253 			if (set)
2254 				zdb_advance |= flag;
2255 			else
2256 				zdb_advance &= ~flag;
2257 			break;
2258 		case 'B':
2259 			endstr = optarg - 1;
2260 			zdb_noread.zb_objset = strtoull(endstr + 1, &endstr, 0);
2261 			zdb_noread.zb_object = strtoull(endstr + 1, &endstr, 0);
2262 			zdb_noread.zb_level = strtol(endstr + 1, &endstr, 0);
2263 			zdb_noread.zb_blkid = strtoull(endstr + 1, &endstr, 16);
2264 			(void) printf("simulating bad block "
2265 			    "<%llu, %llu, %lld, %llx>\n",
2266 			    (u_longlong_t)zdb_noread.zb_objset,
2267 			    (u_longlong_t)zdb_noread.zb_object,
2268 			    (u_longlong_t)zdb_noread.zb_level,
2269 			    (u_longlong_t)zdb_noread.zb_blkid);
2270 			break;
2271 		case 'v':
2272 			verbose++;
2273 			break;
2274 		case 'U':
2275 			spa_config_dir = "/tmp";
2276 			break;
2277 		case 'e':
2278 			exported = 1;
2279 			break;
2280 		case 'p':
2281 			vdev_dir = optarg;
2282 			break;
2283 		default:
2284 			usage();
2285 			break;
2286 		}
2287 	}
2288 
2289 	if (vdev_dir != NULL && exported == 0)
2290 		(void) fatal("-p option requires use of -e\n");
2291 
2292 	kernel_init(FREAD);
2293 	g_zfs = libzfs_init();
2294 	ASSERT(g_zfs != NULL);
2295 
2296 	/*
2297 	 * Disable vdev caching.  If we don't do this, live pool traversal
2298 	 * won't make progress because it will never see disk updates.
2299 	 */
2300 	zfs_vdev_cache_size = 0;
2301 
2302 	for (c = 0; c < 256; c++) {
2303 		if (dump_all && c != 'L' && c != 'l' && c != 'R')
2304 			dump_opt[c] = 1;
2305 		if (dump_opt[c])
2306 			dump_opt[c] += verbose;
2307 	}
2308 
2309 	argc -= optind;
2310 	argv += optind;
2311 
2312 	if (argc < 1) {
2313 		if (dump_opt['C']) {
2314 			dump_config(NULL);
2315 			return (0);
2316 		}
2317 		usage();
2318 	}
2319 
2320 	if (dump_opt['l']) {
2321 		dump_label(argv[0]);
2322 		return (0);
2323 	}
2324 
2325 	if (dump_opt['R']) {
2326 		flagbits['b'] = ZDB_FLAG_PRINT_BLKPTR;
2327 		flagbits['c'] = ZDB_FLAG_CHECKSUM;
2328 		flagbits['d'] = ZDB_FLAG_DECOMPRESS;
2329 		flagbits['e'] = ZDB_FLAG_BSWAP;
2330 		flagbits['g'] = ZDB_FLAG_GBH;
2331 		flagbits['i'] = ZDB_FLAG_INDIRECT;
2332 		flagbits['p'] = ZDB_FLAG_PHYS;
2333 		flagbits['r'] = ZDB_FLAG_RAW;
2334 
2335 		spa = NULL;
2336 		while (argv[0]) {
2337 			zdb_read_block(argv[0], &spa);
2338 			argv++;
2339 			argc--;
2340 		}
2341 		if (spa)
2342 			spa_close(spa, (void *)zdb_read_block);
2343 		return (0);
2344 	}
2345 
2346 	if (dump_opt['C'])
2347 		dump_config(argv[0]);
2348 
2349 	if (exported == 0) {
2350 		if (strchr(argv[0], '/') != NULL) {
2351 			error = dmu_objset_open(argv[0], DMU_OST_ANY,
2352 			    DS_MODE_STANDARD | DS_MODE_READONLY, &os);
2353 		} else {
2354 			error = spa_open(argv[0], &spa, FTAG);
2355 		}
2356 	} else {
2357 		/*
2358 		 * Check to see if the name refers to an exported zpool
2359 		 */
2360 		nvlist_t *exported_conf = NULL;
2361 
2362 		error = find_exported_zpool(argv[0], &exported_conf, vdev_dir);
2363 		if (error == 0) {
2364 			nvlist_t *nvl = NULL;
2365 
2366 			if (vdev_dir != NULL) {
2367 				if (nvlist_alloc(&nvl, NV_UNIQUE_NAME, 0) != 0)
2368 					error = ENOMEM;
2369 				else if (nvlist_add_string(nvl,
2370 				    zpool_prop_to_name(ZPOOL_PROP_ALTROOT),
2371 				    vdev_dir) != 0)
2372 					error = ENOMEM;
2373 			}
2374 
2375 			if (error == 0)
2376 				error = spa_import(argv[0], exported_conf, nvl);
2377 			if (error == 0)
2378 				error = spa_open(argv[0], &spa, FTAG);
2379 
2380 			nvlist_free(nvl);
2381 		}
2382 	}
2383 
2384 	if (error)
2385 		fatal("can't open %s: %s", argv[0], strerror(error));
2386 
2387 	argv++;
2388 	if (--argc > 0) {
2389 		zopt_objects = argc;
2390 		zopt_object = calloc(zopt_objects, sizeof (uint64_t));
2391 		for (i = 0; i < zopt_objects; i++) {
2392 			errno = 0;
2393 			zopt_object[i] = strtoull(argv[i], NULL, 0);
2394 			if (zopt_object[i] == 0 && errno != 0)
2395 				fatal("bad object number %s: %s",
2396 				    argv[i], strerror(errno));
2397 		}
2398 	}
2399 
2400 	if (os != NULL) {
2401 		dump_dir(os);
2402 		dmu_objset_close(os);
2403 	} else {
2404 		dump_zpool(spa);
2405 		spa_close(spa, FTAG);
2406 	}
2407 
2408 	libzfs_fini(g_zfs);
2409 	kernel_fini();
2410 
2411 	return (0);
2412 }
2413