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