xref: /onnv-gate/usr/src/cmd/zdb/zdb.c (revision 12949:b521d551715f)
1789Sahrens /*
2789Sahrens  * CDDL HEADER START
3789Sahrens  *
4789Sahrens  * The contents of this file are subject to the terms of the
51544Seschrock  * Common Development and Distribution License (the "License").
61544Seschrock  * You may not use this file except in compliance with the License.
7789Sahrens  *
8789Sahrens  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9789Sahrens  * or http://www.opensolaris.org/os/licensing.
10789Sahrens  * See the License for the specific language governing permissions
11789Sahrens  * and limitations under the License.
12789Sahrens  *
13789Sahrens  * When distributing Covered Code, include this CDDL HEADER in each
14789Sahrens  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15789Sahrens  * If applicable, add the following below this CDDL HEADER, with the
16789Sahrens  * fields enclosed by brackets "[]" replaced with your own identifying
17789Sahrens  * information: Portions Copyright [yyyy] [name of copyright owner]
18789Sahrens  *
19789Sahrens  * CDDL HEADER END
20789Sahrens  */
21789Sahrens /*
2212050SMark.Shellenbaum@Sun.COM  * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
23789Sahrens  */
24789Sahrens 
25789Sahrens #include <stdio.h>
261914Scasper #include <stdio_ext.h>
27789Sahrens #include <stdlib.h>
281775Sbillm #include <ctype.h>
29789Sahrens #include <sys/zfs_context.h>
30789Sahrens #include <sys/spa.h>
31789Sahrens #include <sys/spa_impl.h>
32789Sahrens #include <sys/dmu.h>
33789Sahrens #include <sys/zap.h>
34789Sahrens #include <sys/fs/zfs.h>
35789Sahrens #include <sys/zfs_znode.h>
3611935SMark.Shellenbaum@Sun.COM #include <sys/zfs_sa.h>
3711935SMark.Shellenbaum@Sun.COM #include <sys/sa.h>
3811935SMark.Shellenbaum@Sun.COM #include <sys/sa_impl.h>
39789Sahrens #include <sys/vdev.h>
40789Sahrens #include <sys/vdev_impl.h>
41789Sahrens #include <sys/metaslab_impl.h>
42789Sahrens #include <sys/dmu_objset.h>
43789Sahrens #include <sys/dsl_dir.h>
44789Sahrens #include <sys/dsl_dataset.h>
45789Sahrens #include <sys/dsl_pool.h>
46789Sahrens #include <sys/dbuf.h>
47789Sahrens #include <sys/zil.h>
48789Sahrens #include <sys/zil_impl.h>
49789Sahrens #include <sys/stat.h>
50789Sahrens #include <sys/resource.h>
51789Sahrens #include <sys/dmu_traverse.h>
52789Sahrens #include <sys/zio_checksum.h>
53789Sahrens #include <sys/zio_compress.h>
545959Smarks #include <sys/zfs_fuid.h>
557837SMatthew.Ahrens@Sun.COM #include <sys/arc.h>
5610922SJeff.Bonwick@Sun.COM #include <sys/ddt.h>
574627Sck153898 #undef ZFS_MAXNAMELEN
584627Sck153898 #undef verify
594627Sck153898 #include <libzfs.h>
60789Sahrens 
6110859SVictor.Latushkin@Sun.COM #define	ZDB_COMPRESS_NAME(idx) ((idx) < ZIO_COMPRESS_FUNCTIONS ? \
6210859SVictor.Latushkin@Sun.COM     zio_compress_table[(idx)].ci_name : "UNKNOWN")
6310859SVictor.Latushkin@Sun.COM #define	ZDB_CHECKSUM_NAME(idx) ((idx) < ZIO_CHECKSUM_FUNCTIONS ? \
6410859SVictor.Latushkin@Sun.COM     zio_checksum_table[(idx)].ci_name : "UNKNOWN")
6510859SVictor.Latushkin@Sun.COM #define	ZDB_OT_NAME(idx) ((idx) < DMU_OT_NUMTYPES ? \
6610859SVictor.Latushkin@Sun.COM     dmu_ot[(idx)].ot_name : "UNKNOWN")
6710859SVictor.Latushkin@Sun.COM #define	ZDB_OT_TYPE(idx) ((idx) < DMU_OT_NUMTYPES ? (idx) : DMU_OT_NUMTYPES)
6810859SVictor.Latushkin@Sun.COM 
6911726SVictor.Latushkin@Sun.COM #ifndef lint
7011726SVictor.Latushkin@Sun.COM extern int zfs_recover;
7111726SVictor.Latushkin@Sun.COM #else
7211726SVictor.Latushkin@Sun.COM int zfs_recover;
7311726SVictor.Latushkin@Sun.COM #endif
7411726SVictor.Latushkin@Sun.COM 
75789Sahrens const char cmdname[] = "zdb";
76789Sahrens uint8_t dump_opt[256];
77789Sahrens 
78789Sahrens typedef void object_viewer_t(objset_t *, uint64_t, void *data, size_t size);
79789Sahrens 
80789Sahrens extern void dump_intent_log(zilog_t *);
81789Sahrens uint64_t *zopt_object = NULL;
82789Sahrens int zopt_objects = 0;
834627Sck153898 libzfs_handle_t *g_zfs;
84789Sahrens 
85789Sahrens /*
86789Sahrens  * These libumem hooks provide a reasonable set of defaults for the allocator's
87789Sahrens  * debugging facilities.
88789Sahrens  */
89789Sahrens const char *
_umem_debug_init()90789Sahrens _umem_debug_init()
91789Sahrens {
92789Sahrens 	return ("default,verbose"); /* $UMEM_DEBUG setting */
93789Sahrens }
94789Sahrens 
95789Sahrens const char *
_umem_logging_init(void)96789Sahrens _umem_logging_init(void)
97789Sahrens {
98789Sahrens 	return ("fail,contents"); /* $UMEM_LOGGING setting */
99789Sahrens }
100789Sahrens 
101789Sahrens static void
usage(void)102789Sahrens usage(void)
103789Sahrens {
104789Sahrens 	(void) fprintf(stderr,
10511811SJeff.Bonwick@Sun.COM 	    "Usage: %s [-CumdibcsDvhL] poolname [object...]\n"
10610858SVictor.Latushkin@Sun.COM 	    "       %s [-div] dataset [object...]\n"
10710861SVictor.Latushkin@Sun.COM 	    "       %s -m [-L] poolname [vdev [metaslab...]]\n"
10810860SVictor.Latushkin@Sun.COM 	    "       %s -R poolname vdev:offset:size[:flags]\n"
10911125SJeff.Bonwick@Sun.COM 	    "       %s -S poolname\n"
11011725SVictor.Latushkin@Sun.COM 	    "       %s -l [-u] device\n"
11110860SVictor.Latushkin@Sun.COM 	    "       %s -C\n\n",
11211125SJeff.Bonwick@Sun.COM 	    cmdname, cmdname, cmdname, cmdname, cmdname, cmdname, cmdname);
113789Sahrens 
11410858SVictor.Latushkin@Sun.COM 	(void) fprintf(stderr, "    Dataset name must include at least one "
11510858SVictor.Latushkin@Sun.COM 	    "separator character '/' or '@'\n");
11610858SVictor.Latushkin@Sun.COM 	(void) fprintf(stderr, "    If dataset name is specified, only that "
11710858SVictor.Latushkin@Sun.COM 	    "dataset is dumped\n");
11810858SVictor.Latushkin@Sun.COM 	(void) fprintf(stderr, "    If object numbers are specified, only "
11910858SVictor.Latushkin@Sun.COM 	    "those objects are dumped\n\n");
12010858SVictor.Latushkin@Sun.COM 	(void) fprintf(stderr, "    Options to control amount of output:\n");
12110858SVictor.Latushkin@Sun.COM 	(void) fprintf(stderr, "        -u uberblock\n");
12210858SVictor.Latushkin@Sun.COM 	(void) fprintf(stderr, "        -d dataset(s)\n");
12310858SVictor.Latushkin@Sun.COM 	(void) fprintf(stderr, "        -i intent logs\n");
12410860SVictor.Latushkin@Sun.COM 	(void) fprintf(stderr, "        -C config (or cachefile if alone)\n");
12510685SGeorge.Wilson@Sun.COM 	(void) fprintf(stderr, "        -h pool history\n");
12610858SVictor.Latushkin@Sun.COM 	(void) fprintf(stderr, "        -b block statistics\n");
12710858SVictor.Latushkin@Sun.COM 	(void) fprintf(stderr, "        -m metaslabs\n");
12810858SVictor.Latushkin@Sun.COM 	(void) fprintf(stderr, "        -c checksum all metadata (twice for "
1299463SVictor.Latushkin@Sun.COM 	    "all data) blocks\n");
13010858SVictor.Latushkin@Sun.COM 	(void) fprintf(stderr, "        -s report stats on zdb's I/O\n");
13111811SJeff.Bonwick@Sun.COM 	(void) fprintf(stderr, "        -D dedup statistics\n");
13210922SJeff.Bonwick@Sun.COM 	(void) fprintf(stderr, "        -S simulate dedup to measure effect\n");
13310858SVictor.Latushkin@Sun.COM 	(void) fprintf(stderr, "        -v verbose (applies to all others)\n");
134789Sahrens 	(void) fprintf(stderr, "        -l dump label contents\n");
1358121SVictor.Latushkin@Sun.COM 	(void) fprintf(stderr, "        -L disable leak tracking (do not "
1368121SVictor.Latushkin@Sun.COM 	    "load spacemaps)\n");
13710858SVictor.Latushkin@Sun.COM 	(void) fprintf(stderr, "        -R read and display block from a "
13810858SVictor.Latushkin@Sun.COM 	    "device\n\n");
13910858SVictor.Latushkin@Sun.COM 	(void) fprintf(stderr, "    Below options are intended for use "
14010858SVictor.Latushkin@Sun.COM 	    "with other options (except -l):\n");
14111726SVictor.Latushkin@Sun.COM 	(void) fprintf(stderr, "        -A ignore assertions (-A), enable "
14211726SVictor.Latushkin@Sun.COM 	    "panic recovery (-AA) or both (-AAA)\n");
14311727SVictor.Latushkin@Sun.COM 	(void) fprintf(stderr, "        -F attempt automatic rewind within "
14411727SVictor.Latushkin@Sun.COM 	    "safe range of transaction groups\n");
14510858SVictor.Latushkin@Sun.COM 	(void) fprintf(stderr, "        -U <cachefile_path> -- use alternate "
1465994Sck153898 	    "cachefile\n");
14711727SVictor.Latushkin@Sun.COM 	(void) fprintf(stderr, "        -X attempt extreme rewind (does not "
14811727SVictor.Latushkin@Sun.COM 	    "work with dataset)\n");
14910858SVictor.Latushkin@Sun.COM 	(void) fprintf(stderr, "        -e pool is exported/destroyed/"
15010858SVictor.Latushkin@Sun.COM 	    "has altroot/not in a cachefile\n");
15110858SVictor.Latushkin@Sun.COM 	(void) fprintf(stderr, "        -p <path> -- use one or more with "
15210858SVictor.Latushkin@Sun.COM 	    "-e to specify path to vdev dir\n");
15312296SLin.Ling@Sun.COM 	(void) fprintf(stderr, "	-P print numbers parsable\n");
15410858SVictor.Latushkin@Sun.COM 	(void) fprintf(stderr, "        -t <txg> -- highest txg to use when "
1558188SVictor.Latushkin@Sun.COM 	    "searching for uberblocks\n");
156789Sahrens 	(void) fprintf(stderr, "Specify an option more than once (e.g. -bb) "
157789Sahrens 	    "to make only that option verbose\n");
158789Sahrens 	(void) fprintf(stderr, "Default is to dump everything non-verbosely\n");
159789Sahrens 	exit(1);
160789Sahrens }
161789Sahrens 
1628924SRichard.Morris@Sun.COM /*
1638924SRichard.Morris@Sun.COM  * Called for usage errors that are discovered after a call to spa_open(),
1648924SRichard.Morris@Sun.COM  * dmu_bonus_hold(), or pool_match().  abort() is called for other errors.
1658924SRichard.Morris@Sun.COM  */
1668924SRichard.Morris@Sun.COM 
167789Sahrens static void
fatal(const char * fmt,...)168789Sahrens fatal(const char *fmt, ...)
169789Sahrens {
170789Sahrens 	va_list ap;
171789Sahrens 
172789Sahrens 	va_start(ap, fmt);
173789Sahrens 	(void) fprintf(stderr, "%s: ", cmdname);
174789Sahrens 	(void) vfprintf(stderr, fmt, ap);
175789Sahrens 	va_end(ap);
176789Sahrens 	(void) fprintf(stderr, "\n");
177789Sahrens 
1788924SRichard.Morris@Sun.COM 	exit(1);
179789Sahrens }
180789Sahrens 
181789Sahrens /* ARGSUSED */
182789Sahrens static void
dump_packed_nvlist(objset_t * os,uint64_t object,void * data,size_t size)183789Sahrens dump_packed_nvlist(objset_t *os, uint64_t object, void *data, size_t size)
184789Sahrens {
185789Sahrens 	nvlist_t *nv;
186789Sahrens 	size_t nvsize = *(uint64_t *)data;
187789Sahrens 	char *packed = umem_alloc(nvsize, UMEM_NOFAIL);
188789Sahrens 
1899512SNeil.Perrin@Sun.COM 	VERIFY(0 == dmu_read(os, object, 0, nvsize, packed, DMU_READ_PREFETCH));
190789Sahrens 
191789Sahrens 	VERIFY(nvlist_unpack(packed, nvsize, &nv, 0) == 0);
192789Sahrens 
193789Sahrens 	umem_free(packed, nvsize);
194789Sahrens 
195789Sahrens 	dump_nvlist(nv, 8);
196789Sahrens 
197789Sahrens 	nvlist_free(nv);
198789Sahrens }
199789Sahrens 
20012296SLin.Ling@Sun.COM static void
zdb_nicenum(uint64_t num,char * buf)20112296SLin.Ling@Sun.COM zdb_nicenum(uint64_t num, char *buf)
20212296SLin.Ling@Sun.COM {
20312296SLin.Ling@Sun.COM 	if (dump_opt['P'])
20412296SLin.Ling@Sun.COM 		(void) sprintf(buf, "%llu", (longlong_t)num);
20512296SLin.Ling@Sun.COM 	else
20612296SLin.Ling@Sun.COM 		nicenum(num, buf);
20712296SLin.Ling@Sun.COM }
20812296SLin.Ling@Sun.COM 
209789Sahrens const char dump_zap_stars[] = "****************************************";
210789Sahrens const int dump_zap_width = sizeof (dump_zap_stars) - 1;
211789Sahrens 
212789Sahrens static void
dump_zap_histogram(uint64_t histo[ZAP_HISTOGRAM_SIZE])213789Sahrens dump_zap_histogram(uint64_t histo[ZAP_HISTOGRAM_SIZE])
214789Sahrens {
215789Sahrens 	int i;
216789Sahrens 	int minidx = ZAP_HISTOGRAM_SIZE - 1;
217789Sahrens 	int maxidx = 0;
218789Sahrens 	uint64_t max = 0;
219789Sahrens 
220789Sahrens 	for (i = 0; i < ZAP_HISTOGRAM_SIZE; i++) {
221789Sahrens 		if (histo[i] > max)
222789Sahrens 			max = histo[i];
223789Sahrens 		if (histo[i] > 0 && i > maxidx)
224789Sahrens 			maxidx = i;
225789Sahrens 		if (histo[i] > 0 && i < minidx)
226789Sahrens 			minidx = i;
227789Sahrens 	}
228789Sahrens 
229789Sahrens 	if (max < dump_zap_width)
230789Sahrens 		max = dump_zap_width;
231789Sahrens 
232789Sahrens 	for (i = minidx; i <= maxidx; i++)
233789Sahrens 		(void) printf("\t\t\t%u: %6llu %s\n", i, (u_longlong_t)histo[i],
234789Sahrens 		    &dump_zap_stars[(max - histo[i]) * dump_zap_width / max]);
235789Sahrens }
236789Sahrens 
237789Sahrens static void
dump_zap_stats(objset_t * os,uint64_t object)238789Sahrens dump_zap_stats(objset_t *os, uint64_t object)
239789Sahrens {
240789Sahrens 	int error;
241789Sahrens 	zap_stats_t zs;
242789Sahrens 
243789Sahrens 	error = zap_get_stats(os, object, &zs);
244789Sahrens 	if (error)
245789Sahrens 		return;
246789Sahrens 
247789Sahrens 	if (zs.zs_ptrtbl_len == 0) {
248789Sahrens 		ASSERT(zs.zs_num_blocks == 1);
249789Sahrens 		(void) printf("\tmicrozap: %llu bytes, %llu entries\n",
250789Sahrens 		    (u_longlong_t)zs.zs_blocksize,
251789Sahrens 		    (u_longlong_t)zs.zs_num_entries);
252789Sahrens 		return;
253789Sahrens 	}
254789Sahrens 
255789Sahrens 	(void) printf("\tFat ZAP stats:\n");
2561632Snd150628 
2571632Snd150628 	(void) printf("\t\tPointer table:\n");
2581632Snd150628 	(void) printf("\t\t\t%llu elements\n",
259789Sahrens 	    (u_longlong_t)zs.zs_ptrtbl_len);
2601632Snd150628 	(void) printf("\t\t\tzt_blk: %llu\n",
2611632Snd150628 	    (u_longlong_t)zs.zs_ptrtbl_zt_blk);
2621632Snd150628 	(void) printf("\t\t\tzt_numblks: %llu\n",
2631632Snd150628 	    (u_longlong_t)zs.zs_ptrtbl_zt_numblks);
2641632Snd150628 	(void) printf("\t\t\tzt_shift: %llu\n",
2651632Snd150628 	    (u_longlong_t)zs.zs_ptrtbl_zt_shift);
2661632Snd150628 	(void) printf("\t\t\tzt_blks_copied: %llu\n",
2671632Snd150628 	    (u_longlong_t)zs.zs_ptrtbl_blks_copied);
2681632Snd150628 	(void) printf("\t\t\tzt_nextblk: %llu\n",
2691632Snd150628 	    (u_longlong_t)zs.zs_ptrtbl_nextblk);
2701632Snd150628 
271789Sahrens 	(void) printf("\t\tZAP entries: %llu\n",
272789Sahrens 	    (u_longlong_t)zs.zs_num_entries);
273789Sahrens 	(void) printf("\t\tLeaf blocks: %llu\n",
274789Sahrens 	    (u_longlong_t)zs.zs_num_leafs);
275789Sahrens 	(void) printf("\t\tTotal blocks: %llu\n",
276789Sahrens 	    (u_longlong_t)zs.zs_num_blocks);
2771632Snd150628 	(void) printf("\t\tzap_block_type: 0x%llx\n",
2781632Snd150628 	    (u_longlong_t)zs.zs_block_type);
2791632Snd150628 	(void) printf("\t\tzap_magic: 0x%llx\n",
2801632Snd150628 	    (u_longlong_t)zs.zs_magic);
2811632Snd150628 	(void) printf("\t\tzap_salt: 0x%llx\n",
2821632Snd150628 	    (u_longlong_t)zs.zs_salt);
283789Sahrens 
284789Sahrens 	(void) printf("\t\tLeafs with 2^n pointers:\n");
285789Sahrens 	dump_zap_histogram(zs.zs_leafs_with_2n_pointers);
286789Sahrens 
287789Sahrens 	(void) printf("\t\tBlocks with n*5 entries:\n");
288789Sahrens 	dump_zap_histogram(zs.zs_blocks_with_n5_entries);
289789Sahrens 
290789Sahrens 	(void) printf("\t\tBlocks n/10 full:\n");
291789Sahrens 	dump_zap_histogram(zs.zs_blocks_n_tenths_full);
292789Sahrens 
293789Sahrens 	(void) printf("\t\tEntries with n chunks:\n");
294789Sahrens 	dump_zap_histogram(zs.zs_entries_using_n_chunks);
295789Sahrens 
296789Sahrens 	(void) printf("\t\tBuckets with n entries:\n");
297789Sahrens 	dump_zap_histogram(zs.zs_buckets_with_n_entries);
298789Sahrens }
299789Sahrens 
300789Sahrens /*ARGSUSED*/
301789Sahrens static void
dump_none(objset_t * os,uint64_t object,void * data,size_t size)302789Sahrens dump_none(objset_t *os, uint64_t object, void *data, size_t size)
303789Sahrens {
304789Sahrens }
305789Sahrens 
306789Sahrens /*ARGSUSED*/
30710859SVictor.Latushkin@Sun.COM static void
dump_unknown(objset_t * os,uint64_t object,void * data,size_t size)30810859SVictor.Latushkin@Sun.COM dump_unknown(objset_t *os, uint64_t object, void *data, size_t size)
30910859SVictor.Latushkin@Sun.COM {
31010859SVictor.Latushkin@Sun.COM 	(void) printf("\tUNKNOWN OBJECT TYPE\n");
31110859SVictor.Latushkin@Sun.COM }
31210859SVictor.Latushkin@Sun.COM 
31310859SVictor.Latushkin@Sun.COM /*ARGSUSED*/
314789Sahrens void
dump_uint8(objset_t * os,uint64_t object,void * data,size_t size)315789Sahrens dump_uint8(objset_t *os, uint64_t object, void *data, size_t size)
316789Sahrens {
317789Sahrens }
318789Sahrens 
319789Sahrens /*ARGSUSED*/
320789Sahrens static void
dump_uint64(objset_t * os,uint64_t object,void * data,size_t size)321789Sahrens dump_uint64(objset_t *os, uint64_t object, void *data, size_t size)
322789Sahrens {
323789Sahrens }
324789Sahrens 
325789Sahrens /*ARGSUSED*/
326789Sahrens static void
dump_zap(objset_t * os,uint64_t object,void * data,size_t size)327789Sahrens dump_zap(objset_t *os, uint64_t object, void *data, size_t size)
328789Sahrens {
329789Sahrens 	zap_cursor_t zc;
330789Sahrens 	zap_attribute_t attr;
331789Sahrens 	void *prop;
332789Sahrens 	int i;
333789Sahrens 
334789Sahrens 	dump_zap_stats(os, object);
335789Sahrens 	(void) printf("\n");
336789Sahrens 
337789Sahrens 	for (zap_cursor_init(&zc, os, object);
338789Sahrens 	    zap_cursor_retrieve(&zc, &attr) == 0;
339789Sahrens 	    zap_cursor_advance(&zc)) {
340789Sahrens 		(void) printf("\t\t%s = ", attr.za_name);
341789Sahrens 		if (attr.za_num_integers == 0) {
342789Sahrens 			(void) printf("\n");
343789Sahrens 			continue;
344789Sahrens 		}
345789Sahrens 		prop = umem_zalloc(attr.za_num_integers *
346789Sahrens 		    attr.za_integer_length, UMEM_NOFAIL);
347789Sahrens 		(void) zap_lookup(os, object, attr.za_name,
348789Sahrens 		    attr.za_integer_length, attr.za_num_integers, prop);
349789Sahrens 		if (attr.za_integer_length == 1) {
350789Sahrens 			(void) printf("%s", (char *)prop);
351789Sahrens 		} else {
352789Sahrens 			for (i = 0; i < attr.za_num_integers; i++) {
353789Sahrens 				switch (attr.za_integer_length) {
354789Sahrens 				case 2:
355789Sahrens 					(void) printf("%u ",
356789Sahrens 					    ((uint16_t *)prop)[i]);
357789Sahrens 					break;
358789Sahrens 				case 4:
359789Sahrens 					(void) printf("%u ",
360789Sahrens 					    ((uint32_t *)prop)[i]);
361789Sahrens 					break;
362789Sahrens 				case 8:
363789Sahrens 					(void) printf("%lld ",
364789Sahrens 					    (u_longlong_t)((int64_t *)prop)[i]);
365789Sahrens 					break;
366789Sahrens 				}
367789Sahrens 			}
368789Sahrens 		}
369789Sahrens 		(void) printf("\n");
370789Sahrens 		umem_free(prop, attr.za_num_integers * attr.za_integer_length);
371789Sahrens 	}
372885Sahrens 	zap_cursor_fini(&zc);
373789Sahrens }
374789Sahrens 
3754577Sahrens /*ARGSUSED*/
3764577Sahrens static void
dump_ddt_zap(objset_t * os,uint64_t object,void * data,size_t size)37711165SMatthew.Ahrens@Sun.COM dump_ddt_zap(objset_t *os, uint64_t object, void *data, size_t size)
37811165SMatthew.Ahrens@Sun.COM {
37911165SMatthew.Ahrens@Sun.COM 	dump_zap_stats(os, object);
38011165SMatthew.Ahrens@Sun.COM 	/* contents are printed elsewhere, properly decoded */
38111165SMatthew.Ahrens@Sun.COM }
38211165SMatthew.Ahrens@Sun.COM 
38311165SMatthew.Ahrens@Sun.COM /*ARGSUSED*/
38411165SMatthew.Ahrens@Sun.COM static void
dump_sa_attrs(objset_t * os,uint64_t object,void * data,size_t size)38511935SMark.Shellenbaum@Sun.COM dump_sa_attrs(objset_t *os, uint64_t object, void *data, size_t size)
38611935SMark.Shellenbaum@Sun.COM {
38711935SMark.Shellenbaum@Sun.COM 	zap_cursor_t zc;
38811935SMark.Shellenbaum@Sun.COM 	zap_attribute_t attr;
38911935SMark.Shellenbaum@Sun.COM 
39011935SMark.Shellenbaum@Sun.COM 	dump_zap_stats(os, object);
39111935SMark.Shellenbaum@Sun.COM 	(void) printf("\n");
39211935SMark.Shellenbaum@Sun.COM 
39311935SMark.Shellenbaum@Sun.COM 	for (zap_cursor_init(&zc, os, object);
39411935SMark.Shellenbaum@Sun.COM 	    zap_cursor_retrieve(&zc, &attr) == 0;
39511935SMark.Shellenbaum@Sun.COM 	    zap_cursor_advance(&zc)) {
39611935SMark.Shellenbaum@Sun.COM 		(void) printf("\t\t%s = ", attr.za_name);
39711935SMark.Shellenbaum@Sun.COM 		if (attr.za_num_integers == 0) {
39811935SMark.Shellenbaum@Sun.COM 			(void) printf("\n");
39911935SMark.Shellenbaum@Sun.COM 			continue;
40011935SMark.Shellenbaum@Sun.COM 		}
40111935SMark.Shellenbaum@Sun.COM 		(void) printf(" %llx : [%d:%d:%d]\n",
40211935SMark.Shellenbaum@Sun.COM 		    (u_longlong_t)attr.za_first_integer,
40311935SMark.Shellenbaum@Sun.COM 		    (int)ATTR_LENGTH(attr.za_first_integer),
40411935SMark.Shellenbaum@Sun.COM 		    (int)ATTR_BSWAP(attr.za_first_integer),
40511935SMark.Shellenbaum@Sun.COM 		    (int)ATTR_NUM(attr.za_first_integer));
40611935SMark.Shellenbaum@Sun.COM 	}
40711935SMark.Shellenbaum@Sun.COM 	zap_cursor_fini(&zc);
40811935SMark.Shellenbaum@Sun.COM }
40911935SMark.Shellenbaum@Sun.COM 
41011935SMark.Shellenbaum@Sun.COM /*ARGSUSED*/
41111935SMark.Shellenbaum@Sun.COM static void
dump_sa_layouts(objset_t * os,uint64_t object,void * data,size_t size)41211935SMark.Shellenbaum@Sun.COM dump_sa_layouts(objset_t *os, uint64_t object, void *data, size_t size)
41311935SMark.Shellenbaum@Sun.COM {
41411935SMark.Shellenbaum@Sun.COM 	zap_cursor_t zc;
41511935SMark.Shellenbaum@Sun.COM 	zap_attribute_t attr;
41611935SMark.Shellenbaum@Sun.COM 	uint16_t *layout_attrs;
41711935SMark.Shellenbaum@Sun.COM 	int i;
41811935SMark.Shellenbaum@Sun.COM 
41911935SMark.Shellenbaum@Sun.COM 	dump_zap_stats(os, object);
42011935SMark.Shellenbaum@Sun.COM 	(void) printf("\n");
42111935SMark.Shellenbaum@Sun.COM 
42211935SMark.Shellenbaum@Sun.COM 	for (zap_cursor_init(&zc, os, object);
42311935SMark.Shellenbaum@Sun.COM 	    zap_cursor_retrieve(&zc, &attr) == 0;
42411935SMark.Shellenbaum@Sun.COM 	    zap_cursor_advance(&zc)) {
42511935SMark.Shellenbaum@Sun.COM 		(void) printf("\t\t%s = [", attr.za_name);
42611935SMark.Shellenbaum@Sun.COM 		if (attr.za_num_integers == 0) {
42711935SMark.Shellenbaum@Sun.COM 			(void) printf("\n");
42811935SMark.Shellenbaum@Sun.COM 			continue;
42911935SMark.Shellenbaum@Sun.COM 		}
43011935SMark.Shellenbaum@Sun.COM 
43111935SMark.Shellenbaum@Sun.COM 		VERIFY(attr.za_integer_length == 2);
43211935SMark.Shellenbaum@Sun.COM 		layout_attrs = umem_zalloc(attr.za_num_integers *
43311935SMark.Shellenbaum@Sun.COM 		    attr.za_integer_length, UMEM_NOFAIL);
43411935SMark.Shellenbaum@Sun.COM 
43511935SMark.Shellenbaum@Sun.COM 		VERIFY(zap_lookup(os, object, attr.za_name,
43611935SMark.Shellenbaum@Sun.COM 		    attr.za_integer_length,
43711935SMark.Shellenbaum@Sun.COM 		    attr.za_num_integers, layout_attrs) == 0);
43811935SMark.Shellenbaum@Sun.COM 
43911935SMark.Shellenbaum@Sun.COM 		for (i = 0; i != attr.za_num_integers; i++)
44011935SMark.Shellenbaum@Sun.COM 			(void) printf(" %d ", (int)layout_attrs[i]);
44111935SMark.Shellenbaum@Sun.COM 		(void) printf("]\n");
44211935SMark.Shellenbaum@Sun.COM 		umem_free(layout_attrs,
44311935SMark.Shellenbaum@Sun.COM 		    attr.za_num_integers * attr.za_integer_length);
44411935SMark.Shellenbaum@Sun.COM 	}
44511935SMark.Shellenbaum@Sun.COM 	zap_cursor_fini(&zc);
44611935SMark.Shellenbaum@Sun.COM }
44711935SMark.Shellenbaum@Sun.COM 
44811935SMark.Shellenbaum@Sun.COM /*ARGSUSED*/
44911935SMark.Shellenbaum@Sun.COM static void
dump_zpldir(objset_t * os,uint64_t object,void * data,size_t size)4504577Sahrens dump_zpldir(objset_t *os, uint64_t object, void *data, size_t size)
4514577Sahrens {
4524577Sahrens 	zap_cursor_t zc;
4534577Sahrens 	zap_attribute_t attr;
4544577Sahrens 	const char *typenames[] = {
4554577Sahrens 		/* 0 */ "not specified",
4564577Sahrens 		/* 1 */ "FIFO",
4574577Sahrens 		/* 2 */ "Character Device",
4584577Sahrens 		/* 3 */ "3 (invalid)",
4594577Sahrens 		/* 4 */ "Directory",
4604577Sahrens 		/* 5 */ "5 (invalid)",
4614577Sahrens 		/* 6 */ "Block Device",
4624577Sahrens 		/* 7 */ "7 (invalid)",
4634577Sahrens 		/* 8 */ "Regular File",
4644577Sahrens 		/* 9 */ "9 (invalid)",
4654577Sahrens 		/* 10 */ "Symbolic Link",
4664577Sahrens 		/* 11 */ "11 (invalid)",
4674577Sahrens 		/* 12 */ "Socket",
4684577Sahrens 		/* 13 */ "Door",
4694577Sahrens 		/* 14 */ "Event Port",
4704577Sahrens 		/* 15 */ "15 (invalid)",
4714577Sahrens 	};
4724577Sahrens 
4734577Sahrens 	dump_zap_stats(os, object);
4744577Sahrens 	(void) printf("\n");
4754577Sahrens 
4764577Sahrens 	for (zap_cursor_init(&zc, os, object);
4774577Sahrens 	    zap_cursor_retrieve(&zc, &attr) == 0;
4784577Sahrens 	    zap_cursor_advance(&zc)) {
4794577Sahrens 		(void) printf("\t\t%s = %lld (type: %s)\n",
4804577Sahrens 		    attr.za_name, ZFS_DIRENT_OBJ(attr.za_first_integer),
4814577Sahrens 		    typenames[ZFS_DIRENT_TYPE(attr.za_first_integer)]);
4824577Sahrens 	}
4834577Sahrens 	zap_cursor_fini(&zc);
4844577Sahrens }
4854577Sahrens 
486789Sahrens static void
dump_spacemap(objset_t * os,space_map_obj_t * smo,space_map_t * sm)487789Sahrens dump_spacemap(objset_t *os, space_map_obj_t *smo, space_map_t *sm)
488789Sahrens {
489789Sahrens 	uint64_t alloc, offset, entry;
4901732Sbonwick 	uint8_t mapshift = sm->sm_shift;
491789Sahrens 	uint64_t mapstart = sm->sm_start;
4923361Sck153898 	char *ddata[] = { "ALLOC", "FREE", "CONDENSE", "INVALID",
4933361Sck153898 			    "INVALID", "INVALID", "INVALID", "INVALID" };
494789Sahrens 
495789Sahrens 	if (smo->smo_object == 0)
496789Sahrens 		return;
497789Sahrens 
498789Sahrens 	/*
499789Sahrens 	 * Print out the freelist entries in both encoded and decoded form.
500789Sahrens 	 */
501789Sahrens 	alloc = 0;
502789Sahrens 	for (offset = 0; offset < smo->smo_objsize; offset += sizeof (entry)) {
50312296SLin.Ling@Sun.COM 		VERIFY3U(0, ==, dmu_read(os, smo->smo_object, offset,
5049512SNeil.Perrin@Sun.COM 		    sizeof (entry), &entry, DMU_READ_PREFETCH));
505789Sahrens 		if (SM_DEBUG_DECODE(entry)) {
50610861SVictor.Latushkin@Sun.COM 			(void) printf("\t    [%6llu] %s: txg %llu, pass %llu\n",
507789Sahrens 			    (u_longlong_t)(offset / sizeof (entry)),
508789Sahrens 			    ddata[SM_DEBUG_ACTION_DECODE(entry)],
5092856Snd150628 			    (u_longlong_t)SM_DEBUG_TXG_DECODE(entry),
5102856Snd150628 			    (u_longlong_t)SM_DEBUG_SYNCPASS_DECODE(entry));
511789Sahrens 		} else {
51210861SVictor.Latushkin@Sun.COM 			(void) printf("\t    [%6llu]    %c  range:"
51310861SVictor.Latushkin@Sun.COM 			    " %010llx-%010llx  size: %06llx\n",
514789Sahrens 			    (u_longlong_t)(offset / sizeof (entry)),
515789Sahrens 			    SM_TYPE_DECODE(entry) == SM_ALLOC ? 'A' : 'F',
5162856Snd150628 			    (u_longlong_t)((SM_OFFSET_DECODE(entry) <<
5172856Snd150628 			    mapshift) + mapstart),
5182856Snd150628 			    (u_longlong_t)((SM_OFFSET_DECODE(entry) <<
5192856Snd150628 			    mapshift) + mapstart + (SM_RUN_DECODE(entry) <<
5202856Snd150628 			    mapshift)),
5212856Snd150628 			    (u_longlong_t)(SM_RUN_DECODE(entry) << mapshift));
522789Sahrens 			if (SM_TYPE_DECODE(entry) == SM_ALLOC)
523789Sahrens 				alloc += SM_RUN_DECODE(entry) << mapshift;
524789Sahrens 			else
525789Sahrens 				alloc -= SM_RUN_DECODE(entry) << mapshift;
526789Sahrens 		}
527789Sahrens 	}
528789Sahrens 	if (alloc != smo->smo_alloc) {
529789Sahrens 		(void) printf("space_map_object alloc (%llu) INCONSISTENT "
530789Sahrens 		    "with space map summary (%llu)\n",
531789Sahrens 		    (u_longlong_t)smo->smo_alloc, (u_longlong_t)alloc);
532789Sahrens 	}
533789Sahrens }
534789Sahrens 
535789Sahrens static void
dump_metaslab_stats(metaslab_t * msp)5369480SGeorge.Wilson@Sun.COM dump_metaslab_stats(metaslab_t *msp)
5379480SGeorge.Wilson@Sun.COM {
53812296SLin.Ling@Sun.COM 	char maxbuf[32];
5399480SGeorge.Wilson@Sun.COM 	space_map_t *sm = &msp->ms_map;
5409480SGeorge.Wilson@Sun.COM 	avl_tree_t *t = sm->sm_pp_root;
5419480SGeorge.Wilson@Sun.COM 	int free_pct = sm->sm_space * 100 / sm->sm_size;
5429480SGeorge.Wilson@Sun.COM 
54312296SLin.Ling@Sun.COM 	zdb_nicenum(space_map_maxsize(sm), maxbuf);
5449480SGeorge.Wilson@Sun.COM 
54510861SVictor.Latushkin@Sun.COM 	(void) printf("\t %25s %10lu   %7s  %6s   %4s %4d%%\n",
5469480SGeorge.Wilson@Sun.COM 	    "segments", avl_numnodes(t), "maxsize", maxbuf,
5479480SGeorge.Wilson@Sun.COM 	    "freepct", free_pct);
5489480SGeorge.Wilson@Sun.COM }
5499480SGeorge.Wilson@Sun.COM 
5509480SGeorge.Wilson@Sun.COM static void
dump_metaslab(metaslab_t * msp)551789Sahrens dump_metaslab(metaslab_t *msp)
552789Sahrens {
553789Sahrens 	vdev_t *vd = msp->ms_group->mg_vd;
554789Sahrens 	spa_t *spa = vd->vdev_spa;
55511146SGeorge.Wilson@Sun.COM 	space_map_t *sm = &msp->ms_map;
55611146SGeorge.Wilson@Sun.COM 	space_map_obj_t *smo = &msp->ms_smo;
55712296SLin.Ling@Sun.COM 	char freebuf[32];
55812296SLin.Ling@Sun.COM 
55912296SLin.Ling@Sun.COM 	zdb_nicenum(sm->sm_size - smo->smo_alloc, freebuf);
560789Sahrens 
561789Sahrens 	(void) printf(
56210861SVictor.Latushkin@Sun.COM 	    "\tmetaslab %6llu   offset %12llx   spacemap %6llu   free    %5s\n",
56311146SGeorge.Wilson@Sun.COM 	    (u_longlong_t)(sm->sm_start / sm->sm_size),
56411146SGeorge.Wilson@Sun.COM 	    (u_longlong_t)sm->sm_start, (u_longlong_t)smo->smo_object, freebuf);
565789Sahrens 
56610861SVictor.Latushkin@Sun.COM 	if (dump_opt['m'] > 1 && !dump_opt['L']) {
5679480SGeorge.Wilson@Sun.COM 		mutex_enter(&msp->ms_lock);
56811146SGeorge.Wilson@Sun.COM 		space_map_load_wait(sm);
56911728SVictor.Latushkin@Sun.COM 		if (!sm->sm_loaded)
57011146SGeorge.Wilson@Sun.COM 			VERIFY(space_map_load(sm, zfs_metaslab_ops,
57111146SGeorge.Wilson@Sun.COM 			    SM_FREE, smo, spa->spa_meta_objset) == 0);
57211728SVictor.Latushkin@Sun.COM 		dump_metaslab_stats(msp);
57311728SVictor.Latushkin@Sun.COM 		space_map_unload(sm);
5749480SGeorge.Wilson@Sun.COM 		mutex_exit(&msp->ms_lock);
5759480SGeorge.Wilson@Sun.COM 	}
576789Sahrens 
5779480SGeorge.Wilson@Sun.COM 	if (dump_opt['d'] > 5 || dump_opt['m'] > 2) {
57811146SGeorge.Wilson@Sun.COM 		ASSERT(sm->sm_size == (1ULL << vd->vdev_ms_shift));
5799480SGeorge.Wilson@Sun.COM 
5809480SGeorge.Wilson@Sun.COM 		mutex_enter(&msp->ms_lock);
58111146SGeorge.Wilson@Sun.COM 		dump_spacemap(spa->spa_meta_objset, smo, sm);
5829480SGeorge.Wilson@Sun.COM 		mutex_exit(&msp->ms_lock);
5839480SGeorge.Wilson@Sun.COM 	}
58410861SVictor.Latushkin@Sun.COM }
5859480SGeorge.Wilson@Sun.COM 
58610861SVictor.Latushkin@Sun.COM static void
print_vdev_metaslab_header(vdev_t * vd)58710861SVictor.Latushkin@Sun.COM print_vdev_metaslab_header(vdev_t *vd)
58810861SVictor.Latushkin@Sun.COM {
58910861SVictor.Latushkin@Sun.COM 	(void) printf("\tvdev %10llu\n\t%-10s%5llu   %-19s   %-15s   %-10s\n",
59010861SVictor.Latushkin@Sun.COM 	    (u_longlong_t)vd->vdev_id,
59110861SVictor.Latushkin@Sun.COM 	    "metaslabs", (u_longlong_t)vd->vdev_ms_count,
59210861SVictor.Latushkin@Sun.COM 	    "offset", "spacemap", "free");
59310861SVictor.Latushkin@Sun.COM 	(void) printf("\t%15s   %19s   %15s   %10s\n",
59410861SVictor.Latushkin@Sun.COM 	    "---------------", "-------------------",
59510861SVictor.Latushkin@Sun.COM 	    "---------------", "-------------");
596789Sahrens }
597789Sahrens 
598789Sahrens static void
dump_metaslabs(spa_t * spa)599789Sahrens dump_metaslabs(spa_t *spa)
600789Sahrens {
60110861SVictor.Latushkin@Sun.COM 	vdev_t *vd, *rvd = spa->spa_root_vdev;
60210861SVictor.Latushkin@Sun.COM 	uint64_t m, c = 0, children = rvd->vdev_children;
603789Sahrens 
604789Sahrens 	(void) printf("\nMetaslabs:\n");
605789Sahrens 
60610861SVictor.Latushkin@Sun.COM 	if (!dump_opt['d'] && zopt_objects > 0) {
60710861SVictor.Latushkin@Sun.COM 		c = zopt_object[0];
60810861SVictor.Latushkin@Sun.COM 
60910861SVictor.Latushkin@Sun.COM 		if (c >= children)
61010861SVictor.Latushkin@Sun.COM 			(void) fatal("bad vdev id: %llu", (u_longlong_t)c);
61110861SVictor.Latushkin@Sun.COM 
61210861SVictor.Latushkin@Sun.COM 		if (zopt_objects > 1) {
61310861SVictor.Latushkin@Sun.COM 			vd = rvd->vdev_child[c];
61410861SVictor.Latushkin@Sun.COM 			print_vdev_metaslab_header(vd);
615789Sahrens 
61610861SVictor.Latushkin@Sun.COM 			for (m = 1; m < zopt_objects; m++) {
61710861SVictor.Latushkin@Sun.COM 				if (zopt_object[m] < vd->vdev_ms_count)
61810861SVictor.Latushkin@Sun.COM 					dump_metaslab(
61910861SVictor.Latushkin@Sun.COM 					    vd->vdev_ms[zopt_object[m]]);
62010861SVictor.Latushkin@Sun.COM 				else
62110861SVictor.Latushkin@Sun.COM 					(void) fprintf(stderr, "bad metaslab "
62210861SVictor.Latushkin@Sun.COM 					    "number %llu\n",
62310861SVictor.Latushkin@Sun.COM 					    (u_longlong_t)zopt_object[m]);
62410861SVictor.Latushkin@Sun.COM 			}
62510861SVictor.Latushkin@Sun.COM 			(void) printf("\n");
62610861SVictor.Latushkin@Sun.COM 			return;
62710861SVictor.Latushkin@Sun.COM 		}
62810861SVictor.Latushkin@Sun.COM 		children = c + 1;
62910861SVictor.Latushkin@Sun.COM 	}
63010861SVictor.Latushkin@Sun.COM 	for (; c < children; c++) {
63110861SVictor.Latushkin@Sun.COM 		vd = rvd->vdev_child[c];
63210861SVictor.Latushkin@Sun.COM 		print_vdev_metaslab_header(vd);
633789Sahrens 
634789Sahrens 		for (m = 0; m < vd->vdev_ms_count; m++)
635789Sahrens 			dump_metaslab(vd->vdev_ms[m]);
636789Sahrens 		(void) printf("\n");
637789Sahrens 	}
638789Sahrens }
639789Sahrens 
640789Sahrens static void
dump_dde(const ddt_t * ddt,const ddt_entry_t * dde,uint64_t index)64110922SJeff.Bonwick@Sun.COM dump_dde(const ddt_t *ddt, const ddt_entry_t *dde, uint64_t index)
64210922SJeff.Bonwick@Sun.COM {
64310922SJeff.Bonwick@Sun.COM 	const ddt_phys_t *ddp = dde->dde_phys;
64410922SJeff.Bonwick@Sun.COM 	const ddt_key_t *ddk = &dde->dde_key;
64510922SJeff.Bonwick@Sun.COM 	char *types[4] = { "ditto", "single", "double", "triple" };
64610922SJeff.Bonwick@Sun.COM 	char blkbuf[BP_SPRINTF_LEN];
64710922SJeff.Bonwick@Sun.COM 	blkptr_t blk;
64810922SJeff.Bonwick@Sun.COM 
64910922SJeff.Bonwick@Sun.COM 	for (int p = 0; p < DDT_PHYS_TYPES; p++, ddp++) {
65010922SJeff.Bonwick@Sun.COM 		if (ddp->ddp_phys_birth == 0)
65110922SJeff.Bonwick@Sun.COM 			continue;
65211125SJeff.Bonwick@Sun.COM 		ddt_bp_create(ddt->ddt_checksum, ddk, ddp, &blk);
65310922SJeff.Bonwick@Sun.COM 		sprintf_blkptr(blkbuf, &blk);
65410922SJeff.Bonwick@Sun.COM 		(void) printf("index %llx refcnt %llu %s %s\n",
65510922SJeff.Bonwick@Sun.COM 		    (u_longlong_t)index, (u_longlong_t)ddp->ddp_refcnt,
65610922SJeff.Bonwick@Sun.COM 		    types[p], blkbuf);
65710922SJeff.Bonwick@Sun.COM 	}
65810922SJeff.Bonwick@Sun.COM }
65910922SJeff.Bonwick@Sun.COM 
66010922SJeff.Bonwick@Sun.COM static void
dump_dedup_ratio(const ddt_stat_t * dds)66110922SJeff.Bonwick@Sun.COM dump_dedup_ratio(const ddt_stat_t *dds)
66210922SJeff.Bonwick@Sun.COM {
66310922SJeff.Bonwick@Sun.COM 	double rL, rP, rD, D, dedup, compress, copies;
66410922SJeff.Bonwick@Sun.COM 
66510922SJeff.Bonwick@Sun.COM 	if (dds->dds_blocks == 0)
66610922SJeff.Bonwick@Sun.COM 		return;
66710922SJeff.Bonwick@Sun.COM 
66810922SJeff.Bonwick@Sun.COM 	rL = (double)dds->dds_ref_lsize;
66910922SJeff.Bonwick@Sun.COM 	rP = (double)dds->dds_ref_psize;
67010922SJeff.Bonwick@Sun.COM 	rD = (double)dds->dds_ref_dsize;
67110922SJeff.Bonwick@Sun.COM 	D = (double)dds->dds_dsize;
67210922SJeff.Bonwick@Sun.COM 
67310922SJeff.Bonwick@Sun.COM 	dedup = rD / D;
67410922SJeff.Bonwick@Sun.COM 	compress = rL / rP;
67510922SJeff.Bonwick@Sun.COM 	copies = rD / rP;
67610922SJeff.Bonwick@Sun.COM 
67710922SJeff.Bonwick@Sun.COM 	(void) printf("dedup = %.2f, compress = %.2f, copies = %.2f, "
67810922SJeff.Bonwick@Sun.COM 	    "dedup * compress / copies = %.2f\n\n",
67910922SJeff.Bonwick@Sun.COM 	    dedup, compress, copies, dedup * compress / copies);
68010922SJeff.Bonwick@Sun.COM }
68110922SJeff.Bonwick@Sun.COM 
68210922SJeff.Bonwick@Sun.COM static void
dump_ddt(ddt_t * ddt,enum ddt_type type,enum ddt_class class)68310922SJeff.Bonwick@Sun.COM dump_ddt(ddt_t *ddt, enum ddt_type type, enum ddt_class class)
68410922SJeff.Bonwick@Sun.COM {
68510922SJeff.Bonwick@Sun.COM 	char name[DDT_NAMELEN];
68610922SJeff.Bonwick@Sun.COM 	ddt_entry_t dde;
68710922SJeff.Bonwick@Sun.COM 	uint64_t walk = 0;
68810922SJeff.Bonwick@Sun.COM 	dmu_object_info_t doi;
68910922SJeff.Bonwick@Sun.COM 	uint64_t count, dspace, mspace;
69010922SJeff.Bonwick@Sun.COM 	int error;
69110922SJeff.Bonwick@Sun.COM 
69210922SJeff.Bonwick@Sun.COM 	error = ddt_object_info(ddt, type, class, &doi);
69310922SJeff.Bonwick@Sun.COM 
69410922SJeff.Bonwick@Sun.COM 	if (error == ENOENT)
69510922SJeff.Bonwick@Sun.COM 		return;
69610922SJeff.Bonwick@Sun.COM 	ASSERT(error == 0);
69710922SJeff.Bonwick@Sun.COM 
69812671SGeorge.Wilson@Sun.COM 	if ((count = ddt_object_count(ddt, type, class)) == 0)
69912671SGeorge.Wilson@Sun.COM 		return;
70012671SGeorge.Wilson@Sun.COM 
70110922SJeff.Bonwick@Sun.COM 	dspace = doi.doi_physical_blocks_512 << 9;
70210922SJeff.Bonwick@Sun.COM 	mspace = doi.doi_fill_count * doi.doi_data_block_size;
70310922SJeff.Bonwick@Sun.COM 
70410922SJeff.Bonwick@Sun.COM 	ddt_object_name(ddt, type, class, name);
70510922SJeff.Bonwick@Sun.COM 
70610922SJeff.Bonwick@Sun.COM 	(void) printf("%s: %llu entries, size %llu on disk, %llu in core\n",
70710922SJeff.Bonwick@Sun.COM 	    name,
70810922SJeff.Bonwick@Sun.COM 	    (u_longlong_t)count,
70910922SJeff.Bonwick@Sun.COM 	    (u_longlong_t)(dspace / count),
71010922SJeff.Bonwick@Sun.COM 	    (u_longlong_t)(mspace / count));
71110922SJeff.Bonwick@Sun.COM 
71210922SJeff.Bonwick@Sun.COM 	if (dump_opt['D'] < 3)
71310922SJeff.Bonwick@Sun.COM 		return;
71410922SJeff.Bonwick@Sun.COM 
71511149SGeorge.Wilson@Sun.COM 	zpool_dump_ddt(NULL, &ddt->ddt_histogram[type][class]);
71610922SJeff.Bonwick@Sun.COM 
71710922SJeff.Bonwick@Sun.COM 	if (dump_opt['D'] < 4)
71810922SJeff.Bonwick@Sun.COM 		return;
71910922SJeff.Bonwick@Sun.COM 
72010922SJeff.Bonwick@Sun.COM 	if (dump_opt['D'] < 5 && class == DDT_CLASS_UNIQUE)
72110922SJeff.Bonwick@Sun.COM 		return;
72210922SJeff.Bonwick@Sun.COM 
72310922SJeff.Bonwick@Sun.COM 	(void) printf("%s contents:\n\n", name);
72410922SJeff.Bonwick@Sun.COM 
72511125SJeff.Bonwick@Sun.COM 	while ((error = ddt_object_walk(ddt, type, class, &walk, &dde)) == 0)
72610922SJeff.Bonwick@Sun.COM 		dump_dde(ddt, &dde, walk);
72710922SJeff.Bonwick@Sun.COM 
72810922SJeff.Bonwick@Sun.COM 	ASSERT(error == ENOENT);
72910922SJeff.Bonwick@Sun.COM 
73010922SJeff.Bonwick@Sun.COM 	(void) printf("\n");
73110922SJeff.Bonwick@Sun.COM }
73210922SJeff.Bonwick@Sun.COM 
73310922SJeff.Bonwick@Sun.COM static void
dump_all_ddts(spa_t * spa)73410922SJeff.Bonwick@Sun.COM dump_all_ddts(spa_t *spa)
73510922SJeff.Bonwick@Sun.COM {
73610922SJeff.Bonwick@Sun.COM 	ddt_histogram_t ddh_total = { 0 };
73710922SJeff.Bonwick@Sun.COM 	ddt_stat_t dds_total = { 0 };
73810922SJeff.Bonwick@Sun.COM 
73910922SJeff.Bonwick@Sun.COM 	for (enum zio_checksum c = 0; c < ZIO_CHECKSUM_FUNCTIONS; c++) {
74010922SJeff.Bonwick@Sun.COM 		ddt_t *ddt = spa->spa_ddt[c];
74110922SJeff.Bonwick@Sun.COM 		for (enum ddt_type type = 0; type < DDT_TYPES; type++) {
74210922SJeff.Bonwick@Sun.COM 			for (enum ddt_class class = 0; class < DDT_CLASSES;
74310922SJeff.Bonwick@Sun.COM 			    class++) {
74410922SJeff.Bonwick@Sun.COM 				dump_ddt(ddt, type, class);
74510922SJeff.Bonwick@Sun.COM 			}
74610922SJeff.Bonwick@Sun.COM 		}
74710922SJeff.Bonwick@Sun.COM 	}
74810922SJeff.Bonwick@Sun.COM 
74911149SGeorge.Wilson@Sun.COM 	ddt_get_dedup_stats(spa, &dds_total);
75010922SJeff.Bonwick@Sun.COM 
75110922SJeff.Bonwick@Sun.COM 	if (dds_total.dds_blocks == 0) {
75210922SJeff.Bonwick@Sun.COM 		(void) printf("All DDTs are empty\n");
75310922SJeff.Bonwick@Sun.COM 		return;
75410922SJeff.Bonwick@Sun.COM 	}
75510922SJeff.Bonwick@Sun.COM 
75610922SJeff.Bonwick@Sun.COM 	(void) printf("\n");
75710922SJeff.Bonwick@Sun.COM 
75810922SJeff.Bonwick@Sun.COM 	if (dump_opt['D'] > 1) {
75910922SJeff.Bonwick@Sun.COM 		(void) printf("DDT histogram (aggregated over all DDTs):\n");
76011149SGeorge.Wilson@Sun.COM 		ddt_get_dedup_histogram(spa, &ddh_total);
76111149SGeorge.Wilson@Sun.COM 		zpool_dump_ddt(&dds_total, &ddh_total);
76210922SJeff.Bonwick@Sun.COM 	}
76310922SJeff.Bonwick@Sun.COM 
76410922SJeff.Bonwick@Sun.COM 	dump_dedup_ratio(&dds_total);
76510922SJeff.Bonwick@Sun.COM }
76610922SJeff.Bonwick@Sun.COM 
76710922SJeff.Bonwick@Sun.COM static void
dump_dtl_seg(space_map_t * sm,uint64_t start,uint64_t size)7688241SJeff.Bonwick@Sun.COM dump_dtl_seg(space_map_t *sm, uint64_t start, uint64_t size)
7698241SJeff.Bonwick@Sun.COM {
7708241SJeff.Bonwick@Sun.COM 	char *prefix = (void *)sm;
7718241SJeff.Bonwick@Sun.COM 
7728241SJeff.Bonwick@Sun.COM 	(void) printf("%s [%llu,%llu) length %llu\n",
7738241SJeff.Bonwick@Sun.COM 	    prefix,
7748241SJeff.Bonwick@Sun.COM 	    (u_longlong_t)start,
7758241SJeff.Bonwick@Sun.COM 	    (u_longlong_t)(start + size),
7768241SJeff.Bonwick@Sun.COM 	    (u_longlong_t)(size));
7778241SJeff.Bonwick@Sun.COM }
7788241SJeff.Bonwick@Sun.COM 
7798241SJeff.Bonwick@Sun.COM static void
dump_dtl(vdev_t * vd,int indent)780789Sahrens dump_dtl(vdev_t *vd, int indent)
781789Sahrens {
7828241SJeff.Bonwick@Sun.COM 	spa_t *spa = vd->vdev_spa;
7838241SJeff.Bonwick@Sun.COM 	boolean_t required;
7848241SJeff.Bonwick@Sun.COM 	char *name[DTL_TYPES] = { "missing", "partial", "scrub", "outage" };
7858241SJeff.Bonwick@Sun.COM 	char prefix[256];
7868241SJeff.Bonwick@Sun.COM 
78710685SGeorge.Wilson@Sun.COM 	spa_vdev_state_enter(spa, SCL_NONE);
7888241SJeff.Bonwick@Sun.COM 	required = vdev_dtl_required(vd);
7898241SJeff.Bonwick@Sun.COM 	(void) spa_vdev_state_exit(spa, NULL, 0);
790789Sahrens 
791789Sahrens 	if (indent == 0)
792789Sahrens 		(void) printf("\nDirty time logs:\n\n");
793789Sahrens 
7948241SJeff.Bonwick@Sun.COM 	(void) printf("\t%*s%s [%s]\n", indent, "",
7957754SJeff.Bonwick@Sun.COM 	    vd->vdev_path ? vd->vdev_path :
7968241SJeff.Bonwick@Sun.COM 	    vd->vdev_parent ? vd->vdev_ops->vdev_op_type : spa_name(spa),
7978241SJeff.Bonwick@Sun.COM 	    required ? "DTL-required" : "DTL-expendable");
798789Sahrens 
7998241SJeff.Bonwick@Sun.COM 	for (int t = 0; t < DTL_TYPES; t++) {
8008241SJeff.Bonwick@Sun.COM 		space_map_t *sm = &vd->vdev_dtl[t];
8018241SJeff.Bonwick@Sun.COM 		if (sm->sm_space == 0)
8028241SJeff.Bonwick@Sun.COM 			continue;
8038241SJeff.Bonwick@Sun.COM 		(void) snprintf(prefix, sizeof (prefix), "\t%*s%s",
8048241SJeff.Bonwick@Sun.COM 		    indent + 2, "", name[t]);
8058241SJeff.Bonwick@Sun.COM 		mutex_enter(sm->sm_lock);
8068241SJeff.Bonwick@Sun.COM 		space_map_walk(sm, dump_dtl_seg, (void *)prefix);
8078241SJeff.Bonwick@Sun.COM 		mutex_exit(sm->sm_lock);
8088241SJeff.Bonwick@Sun.COM 		if (dump_opt['d'] > 5 && vd->vdev_children == 0)
8098241SJeff.Bonwick@Sun.COM 			dump_spacemap(spa->spa_meta_objset,
8108241SJeff.Bonwick@Sun.COM 			    &vd->vdev_dtl_smo, sm);
811789Sahrens 	}
812789Sahrens 
8138241SJeff.Bonwick@Sun.COM 	for (int c = 0; c < vd->vdev_children; c++)
814789Sahrens 		dump_dtl(vd->vdev_child[c], indent + 4);
815789Sahrens }
816789Sahrens 
81710685SGeorge.Wilson@Sun.COM static void
dump_history(spa_t * spa)81810685SGeorge.Wilson@Sun.COM dump_history(spa_t *spa)
81910685SGeorge.Wilson@Sun.COM {
82010685SGeorge.Wilson@Sun.COM 	nvlist_t **events = NULL;
82110685SGeorge.Wilson@Sun.COM 	char buf[SPA_MAXBLOCKSIZE];
82210857SVictor.Latushkin@Sun.COM 	uint64_t resid, len, off = 0;
82310685SGeorge.Wilson@Sun.COM 	uint_t num = 0;
82410685SGeorge.Wilson@Sun.COM 	int error;
82510685SGeorge.Wilson@Sun.COM 	time_t tsec;
82610685SGeorge.Wilson@Sun.COM 	struct tm t;
82710685SGeorge.Wilson@Sun.COM 	char tbuf[30];
82810685SGeorge.Wilson@Sun.COM 	char internalstr[MAXPATHLEN];
82910685SGeorge.Wilson@Sun.COM 
83010685SGeorge.Wilson@Sun.COM 	do {
83110857SVictor.Latushkin@Sun.COM 		len = sizeof (buf);
83210857SVictor.Latushkin@Sun.COM 
83310685SGeorge.Wilson@Sun.COM 		if ((error = spa_history_get(spa, &off, &len, buf)) != 0) {
83410685SGeorge.Wilson@Sun.COM 			(void) fprintf(stderr, "Unable to read history: "
83510685SGeorge.Wilson@Sun.COM 			    "error %d\n", error);
83610685SGeorge.Wilson@Sun.COM 			return;
83710685SGeorge.Wilson@Sun.COM 		}
83810685SGeorge.Wilson@Sun.COM 
83910685SGeorge.Wilson@Sun.COM 		if (zpool_history_unpack(buf, len, &resid, &events, &num) != 0)
84010685SGeorge.Wilson@Sun.COM 			break;
84110685SGeorge.Wilson@Sun.COM 
84210685SGeorge.Wilson@Sun.COM 		off -= resid;
84310685SGeorge.Wilson@Sun.COM 	} while (len != 0);
84410685SGeorge.Wilson@Sun.COM 
84510685SGeorge.Wilson@Sun.COM 	(void) printf("\nHistory:\n");
84610685SGeorge.Wilson@Sun.COM 	for (int i = 0; i < num; i++) {
84710685SGeorge.Wilson@Sun.COM 		uint64_t time, txg, ievent;
84810685SGeorge.Wilson@Sun.COM 		char *cmd, *intstr;
84910685SGeorge.Wilson@Sun.COM 
85010685SGeorge.Wilson@Sun.COM 		if (nvlist_lookup_uint64(events[i], ZPOOL_HIST_TIME,
85110685SGeorge.Wilson@Sun.COM 		    &time) != 0)
85210685SGeorge.Wilson@Sun.COM 			continue;
85310685SGeorge.Wilson@Sun.COM 		if (nvlist_lookup_string(events[i], ZPOOL_HIST_CMD,
85410685SGeorge.Wilson@Sun.COM 		    &cmd) != 0) {
85510685SGeorge.Wilson@Sun.COM 			if (nvlist_lookup_uint64(events[i],
85610685SGeorge.Wilson@Sun.COM 			    ZPOOL_HIST_INT_EVENT, &ievent) != 0)
85710685SGeorge.Wilson@Sun.COM 				continue;
85810685SGeorge.Wilson@Sun.COM 			verify(nvlist_lookup_uint64(events[i],
85910685SGeorge.Wilson@Sun.COM 			    ZPOOL_HIST_TXG, &txg) == 0);
86010685SGeorge.Wilson@Sun.COM 			verify(nvlist_lookup_string(events[i],
86110685SGeorge.Wilson@Sun.COM 			    ZPOOL_HIST_INT_STR, &intstr) == 0);
86210685SGeorge.Wilson@Sun.COM 			if (ievent >= LOG_END)
86310685SGeorge.Wilson@Sun.COM 				continue;
86410685SGeorge.Wilson@Sun.COM 
86510685SGeorge.Wilson@Sun.COM 			(void) snprintf(internalstr,
86610685SGeorge.Wilson@Sun.COM 			    sizeof (internalstr),
86710685SGeorge.Wilson@Sun.COM 			    "[internal %s txg:%lld] %s",
86812296SLin.Ling@Sun.COM 			    zfs_history_event_names[ievent], txg,
86910685SGeorge.Wilson@Sun.COM 			    intstr);
87010685SGeorge.Wilson@Sun.COM 			cmd = internalstr;
87110685SGeorge.Wilson@Sun.COM 		}
87210685SGeorge.Wilson@Sun.COM 		tsec = time;
87310685SGeorge.Wilson@Sun.COM 		(void) localtime_r(&tsec, &t);
87410685SGeorge.Wilson@Sun.COM 		(void) strftime(tbuf, sizeof (tbuf), "%F.%T", &t);
87510685SGeorge.Wilson@Sun.COM 		(void) printf("%s %s\n", tbuf, cmd);
87610685SGeorge.Wilson@Sun.COM 	}
87710685SGeorge.Wilson@Sun.COM }
87810685SGeorge.Wilson@Sun.COM 
879789Sahrens /*ARGSUSED*/
880789Sahrens static void
dump_dnode(objset_t * os,uint64_t object,void * data,size_t size)881789Sahrens dump_dnode(objset_t *os, uint64_t object, void *data, size_t size)
882789Sahrens {
883789Sahrens }
884789Sahrens 
885789Sahrens static uint64_t
blkid2offset(const dnode_phys_t * dnp,const blkptr_t * bp,const zbookmark_t * zb)88610922SJeff.Bonwick@Sun.COM blkid2offset(const dnode_phys_t *dnp, const blkptr_t *bp, const zbookmark_t *zb)
887789Sahrens {
88810922SJeff.Bonwick@Sun.COM 	if (dnp == NULL) {
88910922SJeff.Bonwick@Sun.COM 		ASSERT(zb->zb_level < 0);
89010922SJeff.Bonwick@Sun.COM 		if (zb->zb_object == 0)
89110922SJeff.Bonwick@Sun.COM 			return (zb->zb_blkid);
89210922SJeff.Bonwick@Sun.COM 		return (zb->zb_blkid * BP_GET_LSIZE(bp));
89310922SJeff.Bonwick@Sun.COM 	}
89410922SJeff.Bonwick@Sun.COM 
89510922SJeff.Bonwick@Sun.COM 	ASSERT(zb->zb_level >= 0);
89610922SJeff.Bonwick@Sun.COM 
89710922SJeff.Bonwick@Sun.COM 	return ((zb->zb_blkid <<
89810922SJeff.Bonwick@Sun.COM 	    (zb->zb_level * (dnp->dn_indblkshift - SPA_BLKPTRSHIFT))) *
899789Sahrens 	    dnp->dn_datablkszsec << SPA_MINBLOCKSHIFT);
900789Sahrens }
901789Sahrens 
9021775Sbillm static void
sprintf_blkptr_compact(char * blkbuf,const blkptr_t * bp)90312470SMatthew.Ahrens@Sun.COM sprintf_blkptr_compact(char *blkbuf, const blkptr_t *bp)
9041775Sbillm {
90512470SMatthew.Ahrens@Sun.COM 	const dva_t *dva = bp->blk_dva;
90610922SJeff.Bonwick@Sun.COM 	int ndvas = dump_opt['d'] > 5 ? BP_GET_NDVAS(bp) : 1;
90710922SJeff.Bonwick@Sun.COM 
90810922SJeff.Bonwick@Sun.COM 	if (dump_opt['b'] >= 5) {
90910922SJeff.Bonwick@Sun.COM 		sprintf_blkptr(blkbuf, bp);
91010922SJeff.Bonwick@Sun.COM 		return;
91110922SJeff.Bonwick@Sun.COM 	}
9121775Sbillm 
9131775Sbillm 	blkbuf[0] = '\0';
9141775Sbillm 
91510922SJeff.Bonwick@Sun.COM 	for (int i = 0; i < ndvas; i++)
9161775Sbillm 		(void) sprintf(blkbuf + strlen(blkbuf), "%llu:%llx:%llx ",
9171775Sbillm 		    (u_longlong_t)DVA_GET_VDEV(&dva[i]),
9181775Sbillm 		    (u_longlong_t)DVA_GET_OFFSET(&dva[i]),
9191775Sbillm 		    (u_longlong_t)DVA_GET_ASIZE(&dva[i]));
9201775Sbillm 
92110922SJeff.Bonwick@Sun.COM 	(void) sprintf(blkbuf + strlen(blkbuf),
92210922SJeff.Bonwick@Sun.COM 	    "%llxL/%llxP F=%llu B=%llu/%llu",
9231775Sbillm 	    (u_longlong_t)BP_GET_LSIZE(bp),
9241775Sbillm 	    (u_longlong_t)BP_GET_PSIZE(bp),
9251775Sbillm 	    (u_longlong_t)bp->blk_fill,
92610922SJeff.Bonwick@Sun.COM 	    (u_longlong_t)bp->blk_birth,
92710922SJeff.Bonwick@Sun.COM 	    (u_longlong_t)BP_PHYSICAL_BIRTH(bp));
9281775Sbillm }
9291775Sbillm 
9307837SMatthew.Ahrens@Sun.COM static void
print_indirect(blkptr_t * bp,const zbookmark_t * zb,const dnode_phys_t * dnp)9317837SMatthew.Ahrens@Sun.COM print_indirect(blkptr_t *bp, const zbookmark_t *zb,
9327837SMatthew.Ahrens@Sun.COM     const dnode_phys_t *dnp)
933789Sahrens {
9347837SMatthew.Ahrens@Sun.COM 	char blkbuf[BP_SPRINTF_LEN];
935789Sahrens 	int l;
936789Sahrens 
9377837SMatthew.Ahrens@Sun.COM 	ASSERT3U(BP_GET_TYPE(bp), ==, dnp->dn_type);
9387837SMatthew.Ahrens@Sun.COM 	ASSERT3U(BP_GET_LEVEL(bp), ==, zb->zb_level);
939789Sahrens 
94010922SJeff.Bonwick@Sun.COM 	(void) printf("%16llx ", (u_longlong_t)blkid2offset(dnp, bp, zb));
941789Sahrens 
942789Sahrens 	ASSERT(zb->zb_level >= 0);
943789Sahrens 
944789Sahrens 	for (l = dnp->dn_nlevels - 1; l >= -1; l--) {
945789Sahrens 		if (l == zb->zb_level) {
9467837SMatthew.Ahrens@Sun.COM 			(void) printf("L%llx", (u_longlong_t)zb->zb_level);
947789Sahrens 		} else {
9487837SMatthew.Ahrens@Sun.COM 			(void) printf(" ");
949789Sahrens 		}
950789Sahrens 	}
951789Sahrens 
95210922SJeff.Bonwick@Sun.COM 	sprintf_blkptr_compact(blkbuf, bp);
9537837SMatthew.Ahrens@Sun.COM 	(void) printf("%s\n", blkbuf);
9547837SMatthew.Ahrens@Sun.COM }
9557837SMatthew.Ahrens@Sun.COM 
9567837SMatthew.Ahrens@Sun.COM static int
visit_indirect(spa_t * spa,const dnode_phys_t * dnp,blkptr_t * bp,const zbookmark_t * zb)9577837SMatthew.Ahrens@Sun.COM visit_indirect(spa_t *spa, const dnode_phys_t *dnp,
9587837SMatthew.Ahrens@Sun.COM     blkptr_t *bp, const zbookmark_t *zb)
9597837SMatthew.Ahrens@Sun.COM {
96010857SVictor.Latushkin@Sun.COM 	int err = 0;
9617837SMatthew.Ahrens@Sun.COM 
9627837SMatthew.Ahrens@Sun.COM 	if (bp->blk_birth == 0)
9637837SMatthew.Ahrens@Sun.COM 		return (0);
9647837SMatthew.Ahrens@Sun.COM 
9657837SMatthew.Ahrens@Sun.COM 	print_indirect(bp, zb, dnp);
9667837SMatthew.Ahrens@Sun.COM 
9677837SMatthew.Ahrens@Sun.COM 	if (BP_GET_LEVEL(bp) > 0) {
9687837SMatthew.Ahrens@Sun.COM 		uint32_t flags = ARC_WAIT;
9697837SMatthew.Ahrens@Sun.COM 		int i;
9707837SMatthew.Ahrens@Sun.COM 		blkptr_t *cbp;
9717837SMatthew.Ahrens@Sun.COM 		int epb = BP_GET_LSIZE(bp) >> SPA_BLKPTRSHIFT;
9727837SMatthew.Ahrens@Sun.COM 		arc_buf_t *buf;
9737837SMatthew.Ahrens@Sun.COM 		uint64_t fill = 0;
9747837SMatthew.Ahrens@Sun.COM 
9757837SMatthew.Ahrens@Sun.COM 		err = arc_read_nolock(NULL, spa, bp, arc_getbuf_func, &buf,
9767837SMatthew.Ahrens@Sun.COM 		    ZIO_PRIORITY_ASYNC_READ, ZIO_FLAG_CANFAIL, &flags, zb);
9777837SMatthew.Ahrens@Sun.COM 		if (err)
9787837SMatthew.Ahrens@Sun.COM 			return (err);
97912296SLin.Ling@Sun.COM 		ASSERT(buf->b_data);
9807837SMatthew.Ahrens@Sun.COM 
9817837SMatthew.Ahrens@Sun.COM 		/* recursively visit blocks below this */
9827837SMatthew.Ahrens@Sun.COM 		cbp = buf->b_data;
9837837SMatthew.Ahrens@Sun.COM 		for (i = 0; i < epb; i++, cbp++) {
9847837SMatthew.Ahrens@Sun.COM 			zbookmark_t czb;
9857837SMatthew.Ahrens@Sun.COM 
9867837SMatthew.Ahrens@Sun.COM 			SET_BOOKMARK(&czb, zb->zb_objset, zb->zb_object,
9877837SMatthew.Ahrens@Sun.COM 			    zb->zb_level - 1,
9887837SMatthew.Ahrens@Sun.COM 			    zb->zb_blkid * epb + i);
9897837SMatthew.Ahrens@Sun.COM 			err = visit_indirect(spa, dnp, cbp, &czb);
9907837SMatthew.Ahrens@Sun.COM 			if (err)
9917837SMatthew.Ahrens@Sun.COM 				break;
9927837SMatthew.Ahrens@Sun.COM 			fill += cbp->blk_fill;
9937837SMatthew.Ahrens@Sun.COM 		}
9948241SJeff.Bonwick@Sun.COM 		if (!err)
9958241SJeff.Bonwick@Sun.COM 			ASSERT3U(fill, ==, bp->blk_fill);
9967837SMatthew.Ahrens@Sun.COM 		(void) arc_buf_remove_ref(buf, &buf);
997789Sahrens 	}
998789Sahrens 
9997837SMatthew.Ahrens@Sun.COM 	return (err);
1000789Sahrens }
1001789Sahrens 
1002789Sahrens /*ARGSUSED*/
1003789Sahrens static void
dump_indirect(dnode_t * dn)10047837SMatthew.Ahrens@Sun.COM dump_indirect(dnode_t *dn)
1005789Sahrens {
10067837SMatthew.Ahrens@Sun.COM 	dnode_phys_t *dnp = dn->dn_phys;
10077837SMatthew.Ahrens@Sun.COM 	int j;
10087837SMatthew.Ahrens@Sun.COM 	zbookmark_t czb;
1009789Sahrens 
1010789Sahrens 	(void) printf("Indirect blocks:\n");
1011789Sahrens 
101210298SMatthew.Ahrens@Sun.COM 	SET_BOOKMARK(&czb, dmu_objset_id(dn->dn_objset),
10137837SMatthew.Ahrens@Sun.COM 	    dn->dn_object, dnp->dn_nlevels - 1, 0);
10147837SMatthew.Ahrens@Sun.COM 	for (j = 0; j < dnp->dn_nblkptr; j++) {
10157837SMatthew.Ahrens@Sun.COM 		czb.zb_blkid = j;
101610298SMatthew.Ahrens@Sun.COM 		(void) visit_indirect(dmu_objset_spa(dn->dn_objset), dnp,
10177837SMatthew.Ahrens@Sun.COM 		    &dnp->dn_blkptr[j], &czb);
10187837SMatthew.Ahrens@Sun.COM 	}
1019789Sahrens 
1020789Sahrens 	(void) printf("\n");
1021789Sahrens }
1022789Sahrens 
1023789Sahrens /*ARGSUSED*/
1024789Sahrens static void
dump_dsl_dir(objset_t * os,uint64_t object,void * data,size_t size)1025789Sahrens dump_dsl_dir(objset_t *os, uint64_t object, void *data, size_t size)
1026789Sahrens {
1027789Sahrens 	dsl_dir_phys_t *dd = data;
1028789Sahrens 	time_t crtime;
102912296SLin.Ling@Sun.COM 	char nice[32];
1030789Sahrens 
1031789Sahrens 	if (dd == NULL)
1032789Sahrens 		return;
1033789Sahrens 
10345331Samw 	ASSERT3U(size, >=, sizeof (dsl_dir_phys_t));
1035789Sahrens 
1036789Sahrens 	crtime = dd->dd_creation_time;
1037789Sahrens 	(void) printf("\t\tcreation_time = %s", ctime(&crtime));
1038789Sahrens 	(void) printf("\t\thead_dataset_obj = %llu\n",
1039789Sahrens 	    (u_longlong_t)dd->dd_head_dataset_obj);
1040789Sahrens 	(void) printf("\t\tparent_dir_obj = %llu\n",
1041789Sahrens 	    (u_longlong_t)dd->dd_parent_obj);
10425367Sahrens 	(void) printf("\t\torigin_obj = %llu\n",
10435367Sahrens 	    (u_longlong_t)dd->dd_origin_obj);
1044789Sahrens 	(void) printf("\t\tchild_dir_zapobj = %llu\n",
1045789Sahrens 	    (u_longlong_t)dd->dd_child_dir_zapobj);
104612296SLin.Ling@Sun.COM 	zdb_nicenum(dd->dd_used_bytes, nice);
10477390SMatthew.Ahrens@Sun.COM 	(void) printf("\t\tused_bytes = %s\n", nice);
104812296SLin.Ling@Sun.COM 	zdb_nicenum(dd->dd_compressed_bytes, nice);
10497390SMatthew.Ahrens@Sun.COM 	(void) printf("\t\tcompressed_bytes = %s\n", nice);
105012296SLin.Ling@Sun.COM 	zdb_nicenum(dd->dd_uncompressed_bytes, nice);
10517390SMatthew.Ahrens@Sun.COM 	(void) printf("\t\tuncompressed_bytes = %s\n", nice);
105212296SLin.Ling@Sun.COM 	zdb_nicenum(dd->dd_quota, nice);
10537390SMatthew.Ahrens@Sun.COM 	(void) printf("\t\tquota = %s\n", nice);
105412296SLin.Ling@Sun.COM 	zdb_nicenum(dd->dd_reserved, nice);
10557390SMatthew.Ahrens@Sun.COM 	(void) printf("\t\treserved = %s\n", nice);
1056789Sahrens 	(void) printf("\t\tprops_zapobj = %llu\n",
1057789Sahrens 	    (u_longlong_t)dd->dd_props_zapobj);
10584543Smarks 	(void) printf("\t\tdeleg_zapobj = %llu\n",
10594543Smarks 	    (u_longlong_t)dd->dd_deleg_zapobj);
10607390SMatthew.Ahrens@Sun.COM 	(void) printf("\t\tflags = %llx\n",
10617390SMatthew.Ahrens@Sun.COM 	    (u_longlong_t)dd->dd_flags);
10627390SMatthew.Ahrens@Sun.COM 
10637390SMatthew.Ahrens@Sun.COM #define	DO(which) \
106412296SLin.Ling@Sun.COM 	zdb_nicenum(dd->dd_used_breakdown[DD_USED_ ## which], nice); \
10657390SMatthew.Ahrens@Sun.COM 	(void) printf("\t\tused_breakdown[" #which "] = %s\n", nice)
10667390SMatthew.Ahrens@Sun.COM 	DO(HEAD);
10677390SMatthew.Ahrens@Sun.COM 	DO(SNAP);
10687390SMatthew.Ahrens@Sun.COM 	DO(CHILD);
10697390SMatthew.Ahrens@Sun.COM 	DO(CHILD_RSRV);
10707390SMatthew.Ahrens@Sun.COM 	DO(REFRSRV);
10717390SMatthew.Ahrens@Sun.COM #undef DO
1072789Sahrens }
1073789Sahrens 
1074789Sahrens /*ARGSUSED*/
1075789Sahrens static void
dump_dsl_dataset(objset_t * os,uint64_t object,void * data,size_t size)1076789Sahrens dump_dsl_dataset(objset_t *os, uint64_t object, void *data, size_t size)
1077789Sahrens {
1078789Sahrens 	dsl_dataset_phys_t *ds = data;
1079789Sahrens 	time_t crtime;
108012296SLin.Ling@Sun.COM 	char used[32], compressed[32], uncompressed[32], unique[32];
1081896Smaybee 	char blkbuf[BP_SPRINTF_LEN];
1082789Sahrens 
1083789Sahrens 	if (ds == NULL)
1084789Sahrens 		return;
1085789Sahrens 
1086789Sahrens 	ASSERT(size == sizeof (*ds));
1087789Sahrens 	crtime = ds->ds_creation_time;
108812296SLin.Ling@Sun.COM 	zdb_nicenum(ds->ds_used_bytes, used);
108912296SLin.Ling@Sun.COM 	zdb_nicenum(ds->ds_compressed_bytes, compressed);
109012296SLin.Ling@Sun.COM 	zdb_nicenum(ds->ds_uncompressed_bytes, uncompressed);
109112296SLin.Ling@Sun.COM 	zdb_nicenum(ds->ds_unique_bytes, unique);
109210922SJeff.Bonwick@Sun.COM 	sprintf_blkptr(blkbuf, &ds->ds_bp);
1093789Sahrens 
10947046Sahrens 	(void) printf("\t\tdir_obj = %llu\n",
1095789Sahrens 	    (u_longlong_t)ds->ds_dir_obj);
1096789Sahrens 	(void) printf("\t\tprev_snap_obj = %llu\n",
1097789Sahrens 	    (u_longlong_t)ds->ds_prev_snap_obj);
1098789Sahrens 	(void) printf("\t\tprev_snap_txg = %llu\n",
1099789Sahrens 	    (u_longlong_t)ds->ds_prev_snap_txg);
1100789Sahrens 	(void) printf("\t\tnext_snap_obj = %llu\n",
1101789Sahrens 	    (u_longlong_t)ds->ds_next_snap_obj);
1102789Sahrens 	(void) printf("\t\tsnapnames_zapobj = %llu\n",
1103789Sahrens 	    (u_longlong_t)ds->ds_snapnames_zapobj);
1104789Sahrens 	(void) printf("\t\tnum_children = %llu\n",
1105789Sahrens 	    (u_longlong_t)ds->ds_num_children);
110610242Schris.kirby@sun.com 	(void) printf("\t\tuserrefs_obj = %llu\n",
110710242Schris.kirby@sun.com 	    (u_longlong_t)ds->ds_userrefs_obj);
1108789Sahrens 	(void) printf("\t\tcreation_time = %s", ctime(&crtime));
1109789Sahrens 	(void) printf("\t\tcreation_txg = %llu\n",
1110789Sahrens 	    (u_longlong_t)ds->ds_creation_txg);
1111789Sahrens 	(void) printf("\t\tdeadlist_obj = %llu\n",
1112789Sahrens 	    (u_longlong_t)ds->ds_deadlist_obj);
1113789Sahrens 	(void) printf("\t\tused_bytes = %s\n", used);
1114789Sahrens 	(void) printf("\t\tcompressed_bytes = %s\n", compressed);
1115789Sahrens 	(void) printf("\t\tuncompressed_bytes = %s\n", uncompressed);
1116789Sahrens 	(void) printf("\t\tunique = %s\n", unique);
1117789Sahrens 	(void) printf("\t\tfsid_guid = %llu\n",
1118789Sahrens 	    (u_longlong_t)ds->ds_fsid_guid);
1119789Sahrens 	(void) printf("\t\tguid = %llu\n",
1120789Sahrens 	    (u_longlong_t)ds->ds_guid);
11212082Seschrock 	(void) printf("\t\tflags = %llx\n",
11222082Seschrock 	    (u_longlong_t)ds->ds_flags);
11237046Sahrens 	(void) printf("\t\tnext_clones_obj = %llu\n",
11247046Sahrens 	    (u_longlong_t)ds->ds_next_clones_obj);
11257265Sahrens 	(void) printf("\t\tprops_obj = %llu\n",
11267265Sahrens 	    (u_longlong_t)ds->ds_props_obj);
1127789Sahrens 	(void) printf("\t\tbp = %s\n", blkbuf);
1128789Sahrens }
1129789Sahrens 
113012470SMatthew.Ahrens@Sun.COM /* ARGSUSED */
113112470SMatthew.Ahrens@Sun.COM static int
dump_bpobj_cb(void * arg,const blkptr_t * bp,dmu_tx_t * tx)113212470SMatthew.Ahrens@Sun.COM dump_bpobj_cb(void *arg, const blkptr_t *bp, dmu_tx_t *tx)
1133789Sahrens {
113412470SMatthew.Ahrens@Sun.COM 	char blkbuf[BP_SPRINTF_LEN];
113512470SMatthew.Ahrens@Sun.COM 
113612470SMatthew.Ahrens@Sun.COM 	ASSERT(bp->blk_birth != 0);
113712470SMatthew.Ahrens@Sun.COM 	sprintf_blkptr_compact(blkbuf, bp);
113812470SMatthew.Ahrens@Sun.COM 	(void) printf("\t%s\n", blkbuf);
113912470SMatthew.Ahrens@Sun.COM 	return (0);
114012470SMatthew.Ahrens@Sun.COM }
114112470SMatthew.Ahrens@Sun.COM 
114212470SMatthew.Ahrens@Sun.COM static void
dump_bpobj(bpobj_t * bpo,char * name)114312470SMatthew.Ahrens@Sun.COM dump_bpobj(bpobj_t *bpo, char *name)
114412470SMatthew.Ahrens@Sun.COM {
114512296SLin.Ling@Sun.COM 	char bytes[32];
114612296SLin.Ling@Sun.COM 	char comp[32];
114712296SLin.Ling@Sun.COM 	char uncomp[32];
1148789Sahrens 
1149789Sahrens 	if (dump_opt['d'] < 3)
1150789Sahrens 		return;
1151789Sahrens 
115212470SMatthew.Ahrens@Sun.COM 	zdb_nicenum(bpo->bpo_phys->bpo_bytes, bytes);
115312470SMatthew.Ahrens@Sun.COM 	if (bpo->bpo_havesubobj) {
115412470SMatthew.Ahrens@Sun.COM 		zdb_nicenum(bpo->bpo_phys->bpo_comp, comp);
115512470SMatthew.Ahrens@Sun.COM 		zdb_nicenum(bpo->bpo_phys->bpo_uncomp, uncomp);
115612470SMatthew.Ahrens@Sun.COM 		(void) printf("\n    %s: %llu local blkptrs, %llu subobjs, "
115712470SMatthew.Ahrens@Sun.COM 		    "%s (%s/%s comp)\n",
115812470SMatthew.Ahrens@Sun.COM 		    name, (u_longlong_t)bpo->bpo_phys->bpo_num_blkptrs,
115912470SMatthew.Ahrens@Sun.COM 		    (u_longlong_t)bpo->bpo_phys->bpo_num_subobjs,
11602082Seschrock 		    bytes, comp, uncomp);
11612082Seschrock 	} else {
116212470SMatthew.Ahrens@Sun.COM 		(void) printf("\n    %s: %llu blkptrs, %s\n",
116312470SMatthew.Ahrens@Sun.COM 		    name, (u_longlong_t)bpo->bpo_phys->bpo_num_blkptrs, bytes);
11642082Seschrock 	}
1165789Sahrens 
116612470SMatthew.Ahrens@Sun.COM 	if (dump_opt['d'] < 5)
1167789Sahrens 		return;
1168789Sahrens 
1169789Sahrens 	(void) printf("\n");
1170789Sahrens 
117112470SMatthew.Ahrens@Sun.COM 	(void) bpobj_iterate_nofree(bpo, dump_bpobj_cb, NULL, NULL);
117212470SMatthew.Ahrens@Sun.COM }
117312470SMatthew.Ahrens@Sun.COM 
117412470SMatthew.Ahrens@Sun.COM static void
dump_deadlist(dsl_deadlist_t * dl)117512470SMatthew.Ahrens@Sun.COM dump_deadlist(dsl_deadlist_t *dl)
117612470SMatthew.Ahrens@Sun.COM {
117712470SMatthew.Ahrens@Sun.COM 	dsl_deadlist_entry_t *dle;
117812470SMatthew.Ahrens@Sun.COM 	char bytes[32];
117912470SMatthew.Ahrens@Sun.COM 	char comp[32];
118012470SMatthew.Ahrens@Sun.COM 	char uncomp[32];
118112470SMatthew.Ahrens@Sun.COM 
118212470SMatthew.Ahrens@Sun.COM 	if (dump_opt['d'] < 3)
118312470SMatthew.Ahrens@Sun.COM 		return;
118412470SMatthew.Ahrens@Sun.COM 
118512470SMatthew.Ahrens@Sun.COM 	zdb_nicenum(dl->dl_phys->dl_used, bytes);
118612470SMatthew.Ahrens@Sun.COM 	zdb_nicenum(dl->dl_phys->dl_comp, comp);
118712470SMatthew.Ahrens@Sun.COM 	zdb_nicenum(dl->dl_phys->dl_uncomp, uncomp);
118812470SMatthew.Ahrens@Sun.COM 	(void) printf("\n    Deadlist: %s (%s/%s comp)\n",
118912470SMatthew.Ahrens@Sun.COM 	    bytes, comp, uncomp);
119012470SMatthew.Ahrens@Sun.COM 
119112470SMatthew.Ahrens@Sun.COM 	if (dump_opt['d'] < 4)
119212470SMatthew.Ahrens@Sun.COM 		return;
119312470SMatthew.Ahrens@Sun.COM 
119412470SMatthew.Ahrens@Sun.COM 	(void) printf("\n");
119512470SMatthew.Ahrens@Sun.COM 
119612470SMatthew.Ahrens@Sun.COM 	for (dle = avl_first(&dl->dl_tree); dle;
119712470SMatthew.Ahrens@Sun.COM 	    dle = AVL_NEXT(&dl->dl_tree, dle)) {
119812470SMatthew.Ahrens@Sun.COM 		(void) printf("      mintxg %llu -> obj %llu\n",
119912470SMatthew.Ahrens@Sun.COM 		    (longlong_t)dle->dle_mintxg,
120012470SMatthew.Ahrens@Sun.COM 		    (longlong_t)dle->dle_bpobj.bpo_object);
120112470SMatthew.Ahrens@Sun.COM 
120212470SMatthew.Ahrens@Sun.COM 		if (dump_opt['d'] >= 5)
120312470SMatthew.Ahrens@Sun.COM 			dump_bpobj(&dle->dle_bpobj, "");
1204789Sahrens 	}
1205789Sahrens }
1206789Sahrens 
12075959Smarks static avl_tree_t idx_tree;
12085959Smarks static avl_tree_t domain_tree;
12095959Smarks static boolean_t fuid_table_loaded;
121011935SMark.Shellenbaum@Sun.COM static boolean_t sa_loaded;
121111935SMark.Shellenbaum@Sun.COM sa_attr_type_t *sa_attr_table;
12125959Smarks 
12135959Smarks static void
fuid_table_destroy()12145959Smarks fuid_table_destroy()
12155959Smarks {
12165959Smarks 	if (fuid_table_loaded) {
12175959Smarks 		zfs_fuid_table_destroy(&idx_tree, &domain_tree);
12185959Smarks 		fuid_table_loaded = B_FALSE;
12195959Smarks 	}
12205959Smarks }
12215959Smarks 
12225959Smarks /*
12235959Smarks  * print uid or gid information.
12245959Smarks  * For normal POSIX id just the id is printed in decimal format.
12255959Smarks  * For CIFS files with FUID the fuid is printed in hex followed by
12265959Smarks  * the doman-rid string.
12275959Smarks  */
12285959Smarks static void
print_idstr(uint64_t id,const char * id_type)12295959Smarks print_idstr(uint64_t id, const char *id_type)
12305959Smarks {
12315959Smarks 	if (FUID_INDEX(id)) {
12325959Smarks 		char *domain;
12335959Smarks 
12345959Smarks 		domain = zfs_fuid_idx_domain(&idx_tree, FUID_INDEX(id));
12355959Smarks 		(void) printf("\t%s     %llx [%s-%d]\n", id_type,
12365959Smarks 		    (u_longlong_t)id, domain, (int)FUID_RID(id));
12375959Smarks 	} else {
12385959Smarks 		(void) printf("\t%s     %llu\n", id_type, (u_longlong_t)id);
12395959Smarks 	}
12405959Smarks 
12415959Smarks }
12425959Smarks 
12435959Smarks static void
dump_uidgid(objset_t * os,uint64_t uid,uint64_t gid)124411935SMark.Shellenbaum@Sun.COM dump_uidgid(objset_t *os, uint64_t uid, uint64_t gid)
12455959Smarks {
12465959Smarks 	uint32_t uid_idx, gid_idx;
12475959Smarks 
124811935SMark.Shellenbaum@Sun.COM 	uid_idx = FUID_INDEX(uid);
124911935SMark.Shellenbaum@Sun.COM 	gid_idx = FUID_INDEX(gid);
12505959Smarks 
12515959Smarks 	/* Load domain table, if not already loaded */
12525959Smarks 	if (!fuid_table_loaded && (uid_idx || gid_idx)) {
12535959Smarks 		uint64_t fuid_obj;
12545959Smarks 
12555959Smarks 		/* first find the fuid object.  It lives in the master node */
12565959Smarks 		VERIFY(zap_lookup(os, MASTER_NODE_OBJ, ZFS_FUID_TABLES,
12575959Smarks 		    8, 1, &fuid_obj) == 0);
12589179SMark.Shellenbaum@Sun.COM 		zfs_fuid_avl_tree_create(&idx_tree, &domain_tree);
12595959Smarks 		(void) zfs_fuid_table_load(os, fuid_obj,
12605959Smarks 		    &idx_tree, &domain_tree);
12615959Smarks 		fuid_table_loaded = B_TRUE;
12625959Smarks 	}
12635959Smarks 
126411935SMark.Shellenbaum@Sun.COM 	print_idstr(uid, "uid");
126511935SMark.Shellenbaum@Sun.COM 	print_idstr(gid, "gid");
12665959Smarks }
12675959Smarks 
1268789Sahrens /*ARGSUSED*/
1269789Sahrens static void
dump_znode(objset_t * os,uint64_t object,void * data,size_t size)1270789Sahrens dump_znode(objset_t *os, uint64_t object, void *data, size_t size)
1271789Sahrens {
127211935SMark.Shellenbaum@Sun.COM 	char path[MAXPATHLEN * 2];	/* allow for xattr and failure prefix */
127311935SMark.Shellenbaum@Sun.COM 	sa_handle_t *hdl;
127411935SMark.Shellenbaum@Sun.COM 	uint64_t xattr, rdev, gen;
127511935SMark.Shellenbaum@Sun.COM 	uint64_t uid, gid, mode, fsize, parent, links;
127612050SMark.Shellenbaum@Sun.COM 	uint64_t pflags;
127711935SMark.Shellenbaum@Sun.COM 	uint64_t acctm[2], modtm[2], chgtm[2], crtm[2];
1278789Sahrens 	time_t z_crtime, z_atime, z_mtime, z_ctime;
127912050SMark.Shellenbaum@Sun.COM 	sa_bulk_attr_t bulk[12];
128011935SMark.Shellenbaum@Sun.COM 	int idx = 0;
12813444Sek110237 	int error;
1282789Sahrens 
128311935SMark.Shellenbaum@Sun.COM 	if (!sa_loaded) {
128411935SMark.Shellenbaum@Sun.COM 		uint64_t sa_attrs = 0;
128511935SMark.Shellenbaum@Sun.COM 		uint64_t version;
128611935SMark.Shellenbaum@Sun.COM 
128711935SMark.Shellenbaum@Sun.COM 		VERIFY(zap_lookup(os, MASTER_NODE_OBJ, ZPL_VERSION_STR,
128811935SMark.Shellenbaum@Sun.COM 		    8, 1, &version) == 0);
128911935SMark.Shellenbaum@Sun.COM 		if (version >= ZPL_VERSION_SA) {
129011935SMark.Shellenbaum@Sun.COM 			VERIFY(zap_lookup(os, MASTER_NODE_OBJ, ZFS_SA_ATTRS,
129111935SMark.Shellenbaum@Sun.COM 			    8, 1, &sa_attrs) == 0);
129211935SMark.Shellenbaum@Sun.COM 		}
129312493SMark.Shellenbaum@Oracle.COM 		if ((error = sa_setup(os, sa_attrs, zfs_attr_table,
129412493SMark.Shellenbaum@Oracle.COM 		    ZPL_END, &sa_attr_table)) != 0) {
129512493SMark.Shellenbaum@Oracle.COM 			(void) printf("sa_setup failed errno %d, can't "
129612493SMark.Shellenbaum@Oracle.COM 			    "display znode contents\n", error);
129712493SMark.Shellenbaum@Oracle.COM 			return;
129812493SMark.Shellenbaum@Oracle.COM 		}
129911935SMark.Shellenbaum@Sun.COM 		sa_loaded = B_TRUE;
130011935SMark.Shellenbaum@Sun.COM 	}
130111935SMark.Shellenbaum@Sun.COM 
130211935SMark.Shellenbaum@Sun.COM 	if (sa_handle_get(os, object, NULL, SA_HDL_PRIVATE, &hdl)) {
130311935SMark.Shellenbaum@Sun.COM 		(void) printf("Failed to get handle for SA znode\n");
130411935SMark.Shellenbaum@Sun.COM 		return;
130511935SMark.Shellenbaum@Sun.COM 	}
130611935SMark.Shellenbaum@Sun.COM 
130711935SMark.Shellenbaum@Sun.COM 	SA_ADD_BULK_ATTR(bulk, idx, sa_attr_table[ZPL_UID], NULL, &uid, 8);
130811935SMark.Shellenbaum@Sun.COM 	SA_ADD_BULK_ATTR(bulk, idx, sa_attr_table[ZPL_GID], NULL, &gid, 8);
130911935SMark.Shellenbaum@Sun.COM 	SA_ADD_BULK_ATTR(bulk, idx, sa_attr_table[ZPL_LINKS], NULL,
131011935SMark.Shellenbaum@Sun.COM 	    &links, 8);
131111935SMark.Shellenbaum@Sun.COM 	SA_ADD_BULK_ATTR(bulk, idx, sa_attr_table[ZPL_GEN], NULL, &gen, 8);
131211935SMark.Shellenbaum@Sun.COM 	SA_ADD_BULK_ATTR(bulk, idx, sa_attr_table[ZPL_MODE], NULL,
131311935SMark.Shellenbaum@Sun.COM 	    &mode, 8);
131411935SMark.Shellenbaum@Sun.COM 	SA_ADD_BULK_ATTR(bulk, idx, sa_attr_table[ZPL_PARENT],
131511935SMark.Shellenbaum@Sun.COM 	    NULL, &parent, 8);
131611935SMark.Shellenbaum@Sun.COM 	SA_ADD_BULK_ATTR(bulk, idx, sa_attr_table[ZPL_SIZE], NULL,
131711935SMark.Shellenbaum@Sun.COM 	    &fsize, 8);
131811935SMark.Shellenbaum@Sun.COM 	SA_ADD_BULK_ATTR(bulk, idx, sa_attr_table[ZPL_ATIME], NULL,
131911935SMark.Shellenbaum@Sun.COM 	    acctm, 16);
132011935SMark.Shellenbaum@Sun.COM 	SA_ADD_BULK_ATTR(bulk, idx, sa_attr_table[ZPL_MTIME], NULL,
132111935SMark.Shellenbaum@Sun.COM 	    modtm, 16);
132211935SMark.Shellenbaum@Sun.COM 	SA_ADD_BULK_ATTR(bulk, idx, sa_attr_table[ZPL_CRTIME], NULL,
132311935SMark.Shellenbaum@Sun.COM 	    crtm, 16);
132411935SMark.Shellenbaum@Sun.COM 	SA_ADD_BULK_ATTR(bulk, idx, sa_attr_table[ZPL_CTIME], NULL,
132511935SMark.Shellenbaum@Sun.COM 	    chgtm, 16);
132612050SMark.Shellenbaum@Sun.COM 	SA_ADD_BULK_ATTR(bulk, idx, sa_attr_table[ZPL_FLAGS], NULL,
132712050SMark.Shellenbaum@Sun.COM 	    &pflags, 8);
132811935SMark.Shellenbaum@Sun.COM 
132911935SMark.Shellenbaum@Sun.COM 	if (sa_bulk_lookup(hdl, bulk, idx)) {
133011935SMark.Shellenbaum@Sun.COM 		(void) sa_handle_destroy(hdl);
133111935SMark.Shellenbaum@Sun.COM 		return;
133211935SMark.Shellenbaum@Sun.COM 	}
1333789Sahrens 
13343444Sek110237 	error = zfs_obj_to_path(os, object, path, sizeof (path));
13353444Sek110237 	if (error != 0) {
13363444Sek110237 		(void) snprintf(path, sizeof (path), "\?\?\?<object#%llu>",
13373444Sek110237 		    (u_longlong_t)object);
13383444Sek110237 	}
1339789Sahrens 	if (dump_opt['d'] < 3) {
13403444Sek110237 		(void) printf("\t%s\n", path);
134111935SMark.Shellenbaum@Sun.COM 		(void) sa_handle_destroy(hdl);
1342789Sahrens 		return;
1343789Sahrens 	}
1344789Sahrens 
134511935SMark.Shellenbaum@Sun.COM 	z_crtime = (time_t)crtm[0];
134611935SMark.Shellenbaum@Sun.COM 	z_atime = (time_t)acctm[0];
134711935SMark.Shellenbaum@Sun.COM 	z_mtime = (time_t)modtm[0];
134811935SMark.Shellenbaum@Sun.COM 	z_ctime = (time_t)chgtm[0];
1349789Sahrens 
13503444Sek110237 	(void) printf("\tpath	%s\n", path);
135111935SMark.Shellenbaum@Sun.COM 	dump_uidgid(os, uid, gid);
1352789Sahrens 	(void) printf("\tatime	%s", ctime(&z_atime));
1353789Sahrens 	(void) printf("\tmtime	%s", ctime(&z_mtime));
1354789Sahrens 	(void) printf("\tctime	%s", ctime(&z_ctime));
1355789Sahrens 	(void) printf("\tcrtime	%s", ctime(&z_crtime));
135611935SMark.Shellenbaum@Sun.COM 	(void) printf("\tgen	%llu\n", (u_longlong_t)gen);
135711935SMark.Shellenbaum@Sun.COM 	(void) printf("\tmode	%llo\n", (u_longlong_t)mode);
135811935SMark.Shellenbaum@Sun.COM 	(void) printf("\tsize	%llu\n", (u_longlong_t)fsize);
135911935SMark.Shellenbaum@Sun.COM 	(void) printf("\tparent	%llu\n", (u_longlong_t)parent);
136011935SMark.Shellenbaum@Sun.COM 	(void) printf("\tlinks	%llu\n", (u_longlong_t)links);
136112050SMark.Shellenbaum@Sun.COM 	(void) printf("\tpflags	%llx\n", (u_longlong_t)pflags);
136211935SMark.Shellenbaum@Sun.COM 	if (sa_lookup(hdl, sa_attr_table[ZPL_XATTR], &xattr,
136311935SMark.Shellenbaum@Sun.COM 	    sizeof (uint64_t)) == 0)
136411935SMark.Shellenbaum@Sun.COM 		(void) printf("\txattr	%llu\n", (u_longlong_t)xattr);
136511935SMark.Shellenbaum@Sun.COM 	if (sa_lookup(hdl, sa_attr_table[ZPL_RDEV], &rdev,
136611935SMark.Shellenbaum@Sun.COM 	    sizeof (uint64_t)) == 0)
136711935SMark.Shellenbaum@Sun.COM 		(void) printf("\trdev	0x%016llx\n", (u_longlong_t)rdev);
136811935SMark.Shellenbaum@Sun.COM 	sa_handle_destroy(hdl);
1369789Sahrens }
1370789Sahrens 
1371789Sahrens /*ARGSUSED*/
1372789Sahrens static void
dump_acl(objset_t * os,uint64_t object,void * data,size_t size)1373789Sahrens dump_acl(objset_t *os, uint64_t object, void *data, size_t size)
1374789Sahrens {
1375789Sahrens }
1376789Sahrens 
1377789Sahrens /*ARGSUSED*/
1378789Sahrens static void
dump_dmu_objset(objset_t * os,uint64_t object,void * data,size_t size)1379789Sahrens dump_dmu_objset(objset_t *os, uint64_t object, void *data, size_t size)
1380789Sahrens {
1381789Sahrens }
1382789Sahrens 
138310859SVictor.Latushkin@Sun.COM static object_viewer_t *object_viewer[DMU_OT_NUMTYPES + 1] = {
1384789Sahrens 	dump_none,		/* unallocated			*/
1385789Sahrens 	dump_zap,		/* object directory		*/
1386789Sahrens 	dump_uint64,		/* object array			*/
1387789Sahrens 	dump_none,		/* packed nvlist		*/
1388789Sahrens 	dump_packed_nvlist,	/* packed nvlist size		*/
1389789Sahrens 	dump_none,		/* bplist			*/
1390789Sahrens 	dump_none,		/* bplist header		*/
1391789Sahrens 	dump_none,		/* SPA space map header		*/
1392789Sahrens 	dump_none,		/* SPA space map		*/
1393789Sahrens 	dump_none,		/* ZIL intent log		*/
1394789Sahrens 	dump_dnode,		/* DMU dnode			*/
1395789Sahrens 	dump_dmu_objset,	/* DMU objset			*/
13961544Seschrock 	dump_dsl_dir,		/* DSL directory		*/
1397789Sahrens 	dump_zap,		/* DSL directory child map	*/
1398789Sahrens 	dump_zap,		/* DSL dataset snap map		*/
1399789Sahrens 	dump_zap,		/* DSL props			*/
1400789Sahrens 	dump_dsl_dataset,	/* DSL dataset			*/
1401789Sahrens 	dump_znode,		/* ZFS znode			*/
14025331Samw 	dump_acl,		/* ZFS V0 ACL			*/
1403789Sahrens 	dump_uint8,		/* ZFS plain file		*/
14044577Sahrens 	dump_zpldir,		/* ZFS directory		*/
1405789Sahrens 	dump_zap,		/* ZFS master node		*/
1406789Sahrens 	dump_zap,		/* ZFS delete queue		*/
1407789Sahrens 	dump_uint8,		/* zvol object			*/
1408789Sahrens 	dump_zap,		/* zvol prop			*/
1409789Sahrens 	dump_uint8,		/* other uint8[]		*/
1410789Sahrens 	dump_uint64,		/* other uint64[]		*/
1411789Sahrens 	dump_zap,		/* other ZAP			*/
14121544Seschrock 	dump_zap,		/* persistent error log		*/
14132926Sek110237 	dump_uint8,		/* SPA history			*/
14142926Sek110237 	dump_uint64,		/* SPA history offsets		*/
14153912Slling 	dump_zap,		/* Pool properties		*/
14164543Smarks 	dump_zap,		/* DSL permissions		*/
14175331Samw 	dump_acl,		/* ZFS ACL			*/
14185331Samw 	dump_uint8,		/* ZFS SYSACL			*/
14195331Samw 	dump_none,		/* FUID nvlist			*/
14205331Samw 	dump_packed_nvlist,	/* FUID nvlist size		*/
14217046Sahrens 	dump_zap,		/* DSL dataset next clones	*/
14227046Sahrens 	dump_zap,		/* DSL scrub queue		*/
14239396SMatthew.Ahrens@Sun.COM 	dump_zap,		/* ZFS user/group used		*/
14249396SMatthew.Ahrens@Sun.COM 	dump_zap,		/* ZFS user/group quota		*/
142510242Schris.kirby@sun.com 	dump_zap,		/* snapshot refcount tags	*/
142611165SMatthew.Ahrens@Sun.COM 	dump_ddt_zap,		/* DDT ZAP object		*/
142710922SJeff.Bonwick@Sun.COM 	dump_zap,		/* DDT statistics		*/
142811935SMark.Shellenbaum@Sun.COM 	dump_znode,		/* SA object			*/
142911935SMark.Shellenbaum@Sun.COM 	dump_zap,		/* SA Master Node		*/
143011935SMark.Shellenbaum@Sun.COM 	dump_sa_attrs,		/* SA attribute registration	*/
143111935SMark.Shellenbaum@Sun.COM 	dump_sa_layouts,	/* SA attribute layouts		*/
143212296SLin.Ling@Sun.COM 	dump_zap,		/* DSL scrub translations	*/
143312296SLin.Ling@Sun.COM 	dump_none,		/* fake dedup BP		*/
143412470SMatthew.Ahrens@Sun.COM 	dump_zap,		/* deadlist			*/
143512470SMatthew.Ahrens@Sun.COM 	dump_none,		/* deadlist hdr			*/
143612470SMatthew.Ahrens@Sun.COM 	dump_zap,		/* dsl clones			*/
143712470SMatthew.Ahrens@Sun.COM 	dump_none,		/* bpobj subobjs		*/
143811935SMark.Shellenbaum@Sun.COM 	dump_unknown,		/* Unknown type, must be last	*/
1439789Sahrens };
1440789Sahrens 
1441789Sahrens static void
dump_object(objset_t * os,uint64_t object,int verbosity,int * print_header)1442789Sahrens dump_object(objset_t *os, uint64_t object, int verbosity, int *print_header)
1443789Sahrens {
1444789Sahrens 	dmu_buf_t *db = NULL;
1445789Sahrens 	dmu_object_info_t doi;
1446789Sahrens 	dnode_t *dn;
1447789Sahrens 	void *bonus = NULL;
1448789Sahrens 	size_t bsize = 0;
144912296SLin.Ling@Sun.COM 	char iblk[32], dblk[32], lsize[32], asize[32], fill[32];
145012296SLin.Ling@Sun.COM 	char bonus_size[32];
1451789Sahrens 	char aux[50];
1452789Sahrens 	int error;
1453789Sahrens 
1454789Sahrens 	if (*print_header) {
145510922SJeff.Bonwick@Sun.COM 		(void) printf("\n%10s  %3s  %5s  %5s  %5s  %5s  %6s  %s\n",
145610922SJeff.Bonwick@Sun.COM 		    "Object", "lvl", "iblk", "dblk", "dsize", "lsize",
145710922SJeff.Bonwick@Sun.COM 		    "%full", "type");
1458789Sahrens 		*print_header = 0;
1459789Sahrens 	}
1460789Sahrens 
1461789Sahrens 	if (object == 0) {
146212684STom.Erickson@Sun.COM 		dn = DMU_META_DNODE(os);
1463789Sahrens 	} else {
14641544Seschrock 		error = dmu_bonus_hold(os, object, FTAG, &db);
14651544Seschrock 		if (error)
14661544Seschrock 			fatal("dmu_bonus_hold(%llu) failed, errno %u",
14671544Seschrock 			    object, error);
1468789Sahrens 		bonus = db->db_data;
1469789Sahrens 		bsize = db->db_size;
147012684STom.Erickson@Sun.COM 		dn = DB_DNODE((dmu_buf_impl_t *)db);
1471789Sahrens 	}
1472789Sahrens 	dmu_object_info_from_dnode(dn, &doi);
1473789Sahrens 
147412296SLin.Ling@Sun.COM 	zdb_nicenum(doi.doi_metadata_block_size, iblk);
147512296SLin.Ling@Sun.COM 	zdb_nicenum(doi.doi_data_block_size, dblk);
147612296SLin.Ling@Sun.COM 	zdb_nicenum(doi.doi_max_offset, lsize);
147712296SLin.Ling@Sun.COM 	zdb_nicenum(doi.doi_physical_blocks_512 << 9, asize);
147812296SLin.Ling@Sun.COM 	zdb_nicenum(doi.doi_bonus_size, bonus_size);
147910922SJeff.Bonwick@Sun.COM 	(void) sprintf(fill, "%6.2f", 100.0 * doi.doi_fill_count *
148011125SJeff.Bonwick@Sun.COM 	    doi.doi_data_block_size / (object == 0 ? DNODES_PER_BLOCK : 1) /
148111125SJeff.Bonwick@Sun.COM 	    doi.doi_max_offset);
1482789Sahrens 
1483789Sahrens 	aux[0] = '\0';
1484789Sahrens 
14854577Sahrens 	if (doi.doi_checksum != ZIO_CHECKSUM_INHERIT || verbosity >= 6) {
1486789Sahrens 		(void) snprintf(aux + strlen(aux), sizeof (aux), " (K=%s)",
148710859SVictor.Latushkin@Sun.COM 		    ZDB_CHECKSUM_NAME(doi.doi_checksum));
14884577Sahrens 	}
1489789Sahrens 
14904577Sahrens 	if (doi.doi_compress != ZIO_COMPRESS_INHERIT || verbosity >= 6) {
1491789Sahrens 		(void) snprintf(aux + strlen(aux), sizeof (aux), " (Z=%s)",
149210859SVictor.Latushkin@Sun.COM 		    ZDB_COMPRESS_NAME(doi.doi_compress));
14934577Sahrens 	}
1494789Sahrens 
149510922SJeff.Bonwick@Sun.COM 	(void) printf("%10lld  %3u  %5s  %5s  %5s  %5s  %6s  %s%s\n",
149610922SJeff.Bonwick@Sun.COM 	    (u_longlong_t)object, doi.doi_indirection, iblk, dblk,
149710922SJeff.Bonwick@Sun.COM 	    asize, lsize, fill, ZDB_OT_NAME(doi.doi_type), aux);
1498789Sahrens 
1499789Sahrens 	if (doi.doi_bonus_type != DMU_OT_NONE && verbosity > 3) {
150010922SJeff.Bonwick@Sun.COM 		(void) printf("%10s  %3s  %5s  %5s  %5s  %5s  %6s  %s\n",
150110922SJeff.Bonwick@Sun.COM 		    "", "", "", "", "", bonus_size, "bonus",
150210859SVictor.Latushkin@Sun.COM 		    ZDB_OT_NAME(doi.doi_bonus_type));
1503789Sahrens 	}
1504789Sahrens 
1505789Sahrens 	if (verbosity >= 4) {
150611935SMark.Shellenbaum@Sun.COM 		(void) printf("\tdnode flags: %s%s%s\n",
15079396SMatthew.Ahrens@Sun.COM 		    (dn->dn_phys->dn_flags & DNODE_FLAG_USED_BYTES) ?
15089396SMatthew.Ahrens@Sun.COM 		    "USED_BYTES " : "",
15099396SMatthew.Ahrens@Sun.COM 		    (dn->dn_phys->dn_flags & DNODE_FLAG_USERUSED_ACCOUNTED) ?
151011935SMark.Shellenbaum@Sun.COM 		    "USERUSED_ACCOUNTED " : "",
151111935SMark.Shellenbaum@Sun.COM 		    (dn->dn_phys->dn_flags & DNODE_FLAG_SPILL_BLKPTR) ?
151211935SMark.Shellenbaum@Sun.COM 		    "SPILL_BLKPTR" : "");
15139396SMatthew.Ahrens@Sun.COM 		(void) printf("\tdnode maxblkid: %llu\n",
15149396SMatthew.Ahrens@Sun.COM 		    (longlong_t)dn->dn_phys->dn_maxblkid);
15159396SMatthew.Ahrens@Sun.COM 
151610859SVictor.Latushkin@Sun.COM 		object_viewer[ZDB_OT_TYPE(doi.doi_bonus_type)](os, object,
151710859SVictor.Latushkin@Sun.COM 		    bonus, bsize);
151810859SVictor.Latushkin@Sun.COM 		object_viewer[ZDB_OT_TYPE(doi.doi_type)](os, object, NULL, 0);
1519789Sahrens 		*print_header = 1;
1520789Sahrens 	}
1521789Sahrens 
1522789Sahrens 	if (verbosity >= 5)
15237837SMatthew.Ahrens@Sun.COM 		dump_indirect(dn);
1524789Sahrens 
1525789Sahrens 	if (verbosity >= 5) {
1526789Sahrens 		/*
1527789Sahrens 		 * Report the list of segments that comprise the object.
1528789Sahrens 		 */
1529789Sahrens 		uint64_t start = 0;
1530789Sahrens 		uint64_t end;
1531789Sahrens 		uint64_t blkfill = 1;
1532789Sahrens 		int minlvl = 1;
1533789Sahrens 
1534789Sahrens 		if (dn->dn_type == DMU_OT_DNODE) {
1535789Sahrens 			minlvl = 0;
1536789Sahrens 			blkfill = DNODES_PER_BLOCK;
1537789Sahrens 		}
1538789Sahrens 
1539789Sahrens 		for (;;) {
154012296SLin.Ling@Sun.COM 			char segsize[32];
15416992Smaybee 			error = dnode_next_offset(dn,
15426992Smaybee 			    0, &start, minlvl, blkfill, 0);
1543789Sahrens 			if (error)
1544789Sahrens 				break;
1545789Sahrens 			end = start;
15466992Smaybee 			error = dnode_next_offset(dn,
15476992Smaybee 			    DNODE_FIND_HOLE, &end, minlvl, blkfill, 0);
154812296SLin.Ling@Sun.COM 			zdb_nicenum(end - start, segsize);
1549789Sahrens 			(void) printf("\t\tsegment [%016llx, %016llx)"
1550789Sahrens 			    " size %5s\n", (u_longlong_t)start,
1551789Sahrens 			    (u_longlong_t)end, segsize);
1552789Sahrens 			if (error)
1553789Sahrens 				break;
1554789Sahrens 			start = end;
1555789Sahrens 		}
1556789Sahrens 	}
1557789Sahrens 
1558789Sahrens 	if (db != NULL)
15591544Seschrock 		dmu_buf_rele(db, FTAG);
1560789Sahrens }
1561789Sahrens 
1562789Sahrens static char *objset_types[DMU_OST_NUMTYPES] = {
1563789Sahrens 	"NONE", "META", "ZPL", "ZVOL", "OTHER", "ANY" };
1564789Sahrens 
1565789Sahrens static void
dump_dir(objset_t * os)1566789Sahrens dump_dir(objset_t *os)
1567789Sahrens {
1568789Sahrens 	dmu_objset_stats_t dds;
1569789Sahrens 	uint64_t object, object_count;
15702885Sahrens 	uint64_t refdbytes, usedobjs, scratch;
157112296SLin.Ling@Sun.COM 	char numbuf[32];
15729396SMatthew.Ahrens@Sun.COM 	char blkbuf[BP_SPRINTF_LEN + 20];
1573789Sahrens 	char osname[MAXNAMELEN];
1574789Sahrens 	char *type = "UNKNOWN";
1575789Sahrens 	int verbosity = dump_opt['d'];
1576789Sahrens 	int print_header = 1;
1577789Sahrens 	int i, error;
1578789Sahrens 
15792885Sahrens 	dmu_objset_fast_stat(os, &dds);
1580789Sahrens 
1581789Sahrens 	if (dds.dds_type < DMU_OST_NUMTYPES)
1582789Sahrens 		type = objset_types[dds.dds_type];
1583789Sahrens 
1584789Sahrens 	if (dds.dds_type == DMU_OST_META) {
1585789Sahrens 		dds.dds_creation_txg = TXG_INITIAL;
158610298SMatthew.Ahrens@Sun.COM 		usedobjs = os->os_rootbp->blk_fill;
158710298SMatthew.Ahrens@Sun.COM 		refdbytes = os->os_spa->spa_dsl_pool->
15887390SMatthew.Ahrens@Sun.COM 		    dp_mos_dir->dd_phys->dd_used_bytes;
15892885Sahrens 	} else {
15902885Sahrens 		dmu_objset_space(os, &refdbytes, &scratch, &usedobjs, &scratch);
1591789Sahrens 	}
1592789Sahrens 
159310298SMatthew.Ahrens@Sun.COM 	ASSERT3U(usedobjs, ==, os->os_rootbp->blk_fill);
1594789Sahrens 
159512296SLin.Ling@Sun.COM 	zdb_nicenum(refdbytes, numbuf);
1596789Sahrens 
1597789Sahrens 	if (verbosity >= 4) {
159810857SVictor.Latushkin@Sun.COM 		(void) sprintf(blkbuf, ", rootbp ");
159910922SJeff.Bonwick@Sun.COM 		(void) sprintf_blkptr(blkbuf + strlen(blkbuf), os->os_rootbp);
1600789Sahrens 	} else {
1601789Sahrens 		blkbuf[0] = '\0';
1602789Sahrens 	}
1603789Sahrens 
1604789Sahrens 	dmu_objset_name(os, osname);
1605789Sahrens 
16062885Sahrens 	(void) printf("Dataset %s [%s], ID %llu, cr_txg %llu, "
1607789Sahrens 	    "%s, %llu objects%s\n",
1608789Sahrens 	    osname, type, (u_longlong_t)dmu_objset_id(os),
1609789Sahrens 	    (u_longlong_t)dds.dds_creation_txg,
16102885Sahrens 	    numbuf, (u_longlong_t)usedobjs, blkbuf);
1611789Sahrens 
161210922SJeff.Bonwick@Sun.COM 	if (zopt_objects != 0) {
161310922SJeff.Bonwick@Sun.COM 		for (i = 0; i < zopt_objects; i++)
161410922SJeff.Bonwick@Sun.COM 			dump_object(os, zopt_object[i], verbosity,
161510922SJeff.Bonwick@Sun.COM 			    &print_header);
161610922SJeff.Bonwick@Sun.COM 		(void) printf("\n");
161710922SJeff.Bonwick@Sun.COM 		return;
161810922SJeff.Bonwick@Sun.COM 	}
161910922SJeff.Bonwick@Sun.COM 
162010922SJeff.Bonwick@Sun.COM 	if (dump_opt['i'] != 0 || verbosity >= 2)
162110922SJeff.Bonwick@Sun.COM 		dump_intent_log(dmu_objset_zil(os));
1622789Sahrens 
1623789Sahrens 	if (dmu_objset_ds(os) != NULL)
162412470SMatthew.Ahrens@Sun.COM 		dump_deadlist(&dmu_objset_ds(os)->ds_deadlist);
1625789Sahrens 
1626789Sahrens 	if (verbosity < 2)
1627789Sahrens 		return;
1628789Sahrens 
162910298SMatthew.Ahrens@Sun.COM 	if (os->os_rootbp->blk_birth == 0)
16307046Sahrens 		return;
16317046Sahrens 
1632789Sahrens 	dump_object(os, 0, verbosity, &print_header);
16339396SMatthew.Ahrens@Sun.COM 	object_count = 0;
163412684STom.Erickson@Sun.COM 	if (DMU_USERUSED_DNODE(os) != NULL &&
163512684STom.Erickson@Sun.COM 	    DMU_USERUSED_DNODE(os)->dn_type != 0) {
16369396SMatthew.Ahrens@Sun.COM 		dump_object(os, DMU_USERUSED_OBJECT, verbosity, &print_header);
16379396SMatthew.Ahrens@Sun.COM 		dump_object(os, DMU_GROUPUSED_OBJECT, verbosity, &print_header);
16389396SMatthew.Ahrens@Sun.COM 	}
1639789Sahrens 
1640789Sahrens 	object = 0;
16413025Sahrens 	while ((error = dmu_object_next(os, &object, B_FALSE, 0)) == 0) {
1642789Sahrens 		dump_object(os, object, verbosity, &print_header);
1643789Sahrens 		object_count++;
1644789Sahrens 	}
1645789Sahrens 
16462885Sahrens 	ASSERT3U(object_count, ==, usedobjs);
1647789Sahrens 
1648789Sahrens 	(void) printf("\n");
1649789Sahrens 
16508924SRichard.Morris@Sun.COM 	if (error != ESRCH) {
16518924SRichard.Morris@Sun.COM 		(void) fprintf(stderr, "dmu_object_next() = %d\n", error);
16528924SRichard.Morris@Sun.COM 		abort();
16538924SRichard.Morris@Sun.COM 	}
1654789Sahrens }
1655789Sahrens 
1656789Sahrens static void
dump_uberblock(uberblock_t * ub,const char * header,const char * footer)165711725SVictor.Latushkin@Sun.COM dump_uberblock(uberblock_t *ub, const char *header, const char *footer)
1658789Sahrens {
1659789Sahrens 	time_t timestamp = ub->ub_timestamp;
1660789Sahrens 
166111725SVictor.Latushkin@Sun.COM 	(void) printf(header ? header : "");
1662789Sahrens 	(void) printf("\tmagic = %016llx\n", (u_longlong_t)ub->ub_magic);
1663789Sahrens 	(void) printf("\tversion = %llu\n", (u_longlong_t)ub->ub_version);
1664789Sahrens 	(void) printf("\ttxg = %llu\n", (u_longlong_t)ub->ub_txg);
1665789Sahrens 	(void) printf("\tguid_sum = %llu\n", (u_longlong_t)ub->ub_guid_sum);
1666789Sahrens 	(void) printf("\ttimestamp = %llu UTC = %s",
1667789Sahrens 	    (u_longlong_t)ub->ub_timestamp, asctime(localtime(&timestamp)));
1668789Sahrens 	if (dump_opt['u'] >= 3) {
1669896Smaybee 		char blkbuf[BP_SPRINTF_LEN];
167010922SJeff.Bonwick@Sun.COM 		sprintf_blkptr(blkbuf, &ub->ub_rootbp);
1671789Sahrens 		(void) printf("\trootbp = %s\n", blkbuf);
1672789Sahrens 	}
167311725SVictor.Latushkin@Sun.COM 	(void) printf(footer ? footer : "");
1674789Sahrens }
1675789Sahrens 
1676789Sahrens static void
dump_config(spa_t * spa)167710860SVictor.Latushkin@Sun.COM dump_config(spa_t *spa)
1678789Sahrens {
167910860SVictor.Latushkin@Sun.COM 	dmu_buf_t *db;
168010860SVictor.Latushkin@Sun.COM 	size_t nvsize = 0;
168110860SVictor.Latushkin@Sun.COM 	int error = 0;
168210860SVictor.Latushkin@Sun.COM 
168310860SVictor.Latushkin@Sun.COM 
168410860SVictor.Latushkin@Sun.COM 	error = dmu_bonus_hold(spa->spa_meta_objset,
168510860SVictor.Latushkin@Sun.COM 	    spa->spa_config_object, FTAG, &db);
1686789Sahrens 
168710860SVictor.Latushkin@Sun.COM 	if (error == 0) {
168810860SVictor.Latushkin@Sun.COM 		nvsize = *(uint64_t *)db->db_data;
168910860SVictor.Latushkin@Sun.COM 		dmu_buf_rele(db, FTAG);
169010860SVictor.Latushkin@Sun.COM 
169110860SVictor.Latushkin@Sun.COM 		(void) printf("\nMOS Configuration:\n");
169210860SVictor.Latushkin@Sun.COM 		dump_packed_nvlist(spa->spa_meta_objset,
169310860SVictor.Latushkin@Sun.COM 		    spa->spa_config_object, (void *)&nvsize, 1);
169410860SVictor.Latushkin@Sun.COM 	} else {
169510860SVictor.Latushkin@Sun.COM 		(void) fprintf(stderr, "dmu_bonus_hold(%llu) failed, errno %d",
169610860SVictor.Latushkin@Sun.COM 		    (u_longlong_t)spa->spa_config_object, error);
1697789Sahrens 	}
1698789Sahrens }
1699789Sahrens 
1700789Sahrens static void
dump_cachefile(const char * cachefile)17016643Seschrock dump_cachefile(const char *cachefile)
17026643Seschrock {
17036643Seschrock 	int fd;
17046643Seschrock 	struct stat64 statbuf;
17056643Seschrock 	char *buf;
17066643Seschrock 	nvlist_t *config;
17076643Seschrock 
17086643Seschrock 	if ((fd = open64(cachefile, O_RDONLY)) < 0) {
17096643Seschrock 		(void) printf("cannot open '%s': %s\n", cachefile,
17106643Seschrock 		    strerror(errno));
17116643Seschrock 		exit(1);
17126643Seschrock 	}
17136643Seschrock 
17146643Seschrock 	if (fstat64(fd, &statbuf) != 0) {
17156643Seschrock 		(void) printf("failed to stat '%s': %s\n", cachefile,
17166643Seschrock 		    strerror(errno));
17176643Seschrock 		exit(1);
17186643Seschrock 	}
17196643Seschrock 
17206643Seschrock 	if ((buf = malloc(statbuf.st_size)) == NULL) {
17216643Seschrock 		(void) fprintf(stderr, "failed to allocate %llu bytes\n",
17226643Seschrock 		    (u_longlong_t)statbuf.st_size);
17236643Seschrock 		exit(1);
17246643Seschrock 	}
17256643Seschrock 
17266643Seschrock 	if (read(fd, buf, statbuf.st_size) != statbuf.st_size) {
17276643Seschrock 		(void) fprintf(stderr, "failed to read %llu bytes\n",
17286643Seschrock 		    (u_longlong_t)statbuf.st_size);
17296643Seschrock 		exit(1);
17306643Seschrock 	}
17316643Seschrock 
17326643Seschrock 	(void) close(fd);
17336643Seschrock 
17346643Seschrock 	if (nvlist_unpack(buf, statbuf.st_size, &config, 0) != 0) {
17356643Seschrock 		(void) fprintf(stderr, "failed to unpack nvlist\n");
17366643Seschrock 		exit(1);
17376643Seschrock 	}
17386643Seschrock 
17396643Seschrock 	free(buf);
17406643Seschrock 
17416643Seschrock 	dump_nvlist(config, 0);
17426643Seschrock 
17436643Seschrock 	nvlist_free(config);
17446643Seschrock }
17456643Seschrock 
174611725SVictor.Latushkin@Sun.COM #define	ZDB_MAX_UB_HEADER_SIZE 32
174711725SVictor.Latushkin@Sun.COM 
174811725SVictor.Latushkin@Sun.COM static void
dump_label_uberblocks(vdev_label_t * lbl,uint64_t ashift)174911725SVictor.Latushkin@Sun.COM dump_label_uberblocks(vdev_label_t *lbl, uint64_t ashift)
175011725SVictor.Latushkin@Sun.COM {
175111725SVictor.Latushkin@Sun.COM 	vdev_t vd;
175211725SVictor.Latushkin@Sun.COM 	vdev_t *vdp = &vd;
175311725SVictor.Latushkin@Sun.COM 	char header[ZDB_MAX_UB_HEADER_SIZE];
175411725SVictor.Latushkin@Sun.COM 
175511725SVictor.Latushkin@Sun.COM 	vd.vdev_ashift = ashift;
175611725SVictor.Latushkin@Sun.COM 	vdp->vdev_top = vdp;
175711725SVictor.Latushkin@Sun.COM 
175811725SVictor.Latushkin@Sun.COM 	for (int i = 0; i < VDEV_UBERBLOCK_COUNT(vdp); i++) {
175911725SVictor.Latushkin@Sun.COM 		uint64_t uoff = VDEV_UBERBLOCK_OFFSET(vdp, i);
176011725SVictor.Latushkin@Sun.COM 		uberblock_t *ub = (void *)((char *)lbl + uoff);
176111725SVictor.Latushkin@Sun.COM 
176211725SVictor.Latushkin@Sun.COM 		if (uberblock_verify(ub))
176311725SVictor.Latushkin@Sun.COM 			continue;
176411725SVictor.Latushkin@Sun.COM 		(void) snprintf(header, ZDB_MAX_UB_HEADER_SIZE,
176511725SVictor.Latushkin@Sun.COM 		    "Uberblock[%d]\n", i);
176611725SVictor.Latushkin@Sun.COM 		dump_uberblock(ub, header, "");
176711725SVictor.Latushkin@Sun.COM 	}
176811725SVictor.Latushkin@Sun.COM }
176911725SVictor.Latushkin@Sun.COM 
17706643Seschrock static void
dump_label(const char * dev)1771789Sahrens dump_label(const char *dev)
1772789Sahrens {
1773789Sahrens 	int fd;
1774789Sahrens 	vdev_label_t label;
177511826SGeorge.Wilson@Sun.COM 	char *path, *buf = label.vl_vdev_phys.vp_nvlist;
1776789Sahrens 	size_t buflen = sizeof (label.vl_vdev_phys.vp_nvlist);
1777789Sahrens 	struct stat64 statbuf;
177811725SVictor.Latushkin@Sun.COM 	uint64_t psize, ashift;
177911826SGeorge.Wilson@Sun.COM 	int len = strlen(dev) + 1;
178011826SGeorge.Wilson@Sun.COM 
178111826SGeorge.Wilson@Sun.COM 	if (strncmp(dev, "/dev/dsk/", 9) == 0) {
178211826SGeorge.Wilson@Sun.COM 		len++;
178311826SGeorge.Wilson@Sun.COM 		path = malloc(len);
178411826SGeorge.Wilson@Sun.COM 		(void) snprintf(path, len, "%s%s", "/dev/rdsk/", dev + 9);
178511826SGeorge.Wilson@Sun.COM 	} else {
178611826SGeorge.Wilson@Sun.COM 		path = strdup(dev);
178711826SGeorge.Wilson@Sun.COM 	}
178811826SGeorge.Wilson@Sun.COM 
178911826SGeorge.Wilson@Sun.COM 	if ((fd = open64(path, O_RDONLY)) < 0) {
179011826SGeorge.Wilson@Sun.COM 		(void) printf("cannot open '%s': %s\n", path, strerror(errno));
179111826SGeorge.Wilson@Sun.COM 		free(path);
1792789Sahrens 		exit(1);
1793789Sahrens 	}
1794789Sahrens 
1795789Sahrens 	if (fstat64(fd, &statbuf) != 0) {
179611826SGeorge.Wilson@Sun.COM 		(void) printf("failed to stat '%s': %s\n", path,
1797789Sahrens 		    strerror(errno));
179811826SGeorge.Wilson@Sun.COM 		free(path);
179911826SGeorge.Wilson@Sun.COM 		(void) close(fd);
180011826SGeorge.Wilson@Sun.COM 		exit(1);
180111826SGeorge.Wilson@Sun.COM 	}
180211826SGeorge.Wilson@Sun.COM 
180311826SGeorge.Wilson@Sun.COM 	if (S_ISBLK(statbuf.st_mode)) {
180411826SGeorge.Wilson@Sun.COM 		(void) printf("cannot use '%s': character device required\n",
180511826SGeorge.Wilson@Sun.COM 		    path);
180611826SGeorge.Wilson@Sun.COM 		free(path);
180711826SGeorge.Wilson@Sun.COM 		(void) close(fd);
180811826SGeorge.Wilson@Sun.COM 		exit(1);
1809789Sahrens 	}
1810789Sahrens 
1811789Sahrens 	psize = statbuf.st_size;
1812789Sahrens 	psize = P2ALIGN(psize, (uint64_t)sizeof (vdev_label_t));
1813789Sahrens 
181411725SVictor.Latushkin@Sun.COM 	for (int l = 0; l < VDEV_LABELS; l++) {
1815789Sahrens 		nvlist_t *config = NULL;
1816789Sahrens 
1817789Sahrens 		(void) printf("--------------------------------------------\n");
1818789Sahrens 		(void) printf("LABEL %d\n", l);
1819789Sahrens 		(void) printf("--------------------------------------------\n");
1820789Sahrens 
18211170Seschrock 		if (pread64(fd, &label, sizeof (label),
1822789Sahrens 		    vdev_label_offset(psize, l, 0)) != sizeof (label)) {
1823789Sahrens 			(void) printf("failed to read label %d\n", l);
1824789Sahrens 			continue;
1825789Sahrens 		}
1826789Sahrens 
1827789Sahrens 		if (nvlist_unpack(buf, buflen, &config, 0) != 0) {
1828789Sahrens 			(void) printf("failed to unpack label %d\n", l);
182911725SVictor.Latushkin@Sun.COM 			ashift = SPA_MINBLOCKSHIFT;
183011725SVictor.Latushkin@Sun.COM 		} else {
183111725SVictor.Latushkin@Sun.COM 			nvlist_t *vdev_tree = NULL;
183211725SVictor.Latushkin@Sun.COM 
183311725SVictor.Latushkin@Sun.COM 			dump_nvlist(config, 4);
183411725SVictor.Latushkin@Sun.COM 			if ((nvlist_lookup_nvlist(config,
183511725SVictor.Latushkin@Sun.COM 			    ZPOOL_CONFIG_VDEV_TREE, &vdev_tree) != 0) ||
183611725SVictor.Latushkin@Sun.COM 			    (nvlist_lookup_uint64(vdev_tree,
183711725SVictor.Latushkin@Sun.COM 			    ZPOOL_CONFIG_ASHIFT, &ashift) != 0))
183811725SVictor.Latushkin@Sun.COM 				ashift = SPA_MINBLOCKSHIFT;
183911725SVictor.Latushkin@Sun.COM 			nvlist_free(config);
1840789Sahrens 		}
184111725SVictor.Latushkin@Sun.COM 		if (dump_opt['u'])
184211725SVictor.Latushkin@Sun.COM 			dump_label_uberblocks(&label, ashift);
1843789Sahrens 	}
184411826SGeorge.Wilson@Sun.COM 
184511826SGeorge.Wilson@Sun.COM 	free(path);
184611826SGeorge.Wilson@Sun.COM 	(void) close(fd);
1847789Sahrens }
1848789Sahrens 
1849789Sahrens /*ARGSUSED*/
18502199Sahrens static int
dump_one_dir(const char * dsname,void * arg)185111209SMatthew.Ahrens@Sun.COM dump_one_dir(const char *dsname, void *arg)
1852789Sahrens {
1853789Sahrens 	int error;
1854789Sahrens 	objset_t *os;
1855789Sahrens 
185610298SMatthew.Ahrens@Sun.COM 	error = dmu_objset_own(dsname, DMU_OST_ANY, B_TRUE, FTAG, &os);
1857789Sahrens 	if (error) {
185810922SJeff.Bonwick@Sun.COM 		(void) printf("Could not open %s, error %d\n", dsname, error);
18592199Sahrens 		return (0);
1860789Sahrens 	}
1861789Sahrens 	dump_dir(os);
186210298SMatthew.Ahrens@Sun.COM 	dmu_objset_disown(os, FTAG);
18635959Smarks 	fuid_table_destroy();
186411935SMark.Shellenbaum@Sun.COM 	sa_loaded = B_FALSE;
18652199Sahrens 	return (0);
1866789Sahrens }
1867789Sahrens 
186810922SJeff.Bonwick@Sun.COM /*
186910922SJeff.Bonwick@Sun.COM  * Block statistics.
187010922SJeff.Bonwick@Sun.COM  */
187110922SJeff.Bonwick@Sun.COM typedef struct zdb_blkstats {
187210922SJeff.Bonwick@Sun.COM 	uint64_t	zb_asize;
187310922SJeff.Bonwick@Sun.COM 	uint64_t	zb_lsize;
187410922SJeff.Bonwick@Sun.COM 	uint64_t	zb_psize;
187510922SJeff.Bonwick@Sun.COM 	uint64_t	zb_count;
187610922SJeff.Bonwick@Sun.COM } zdb_blkstats_t;
187710922SJeff.Bonwick@Sun.COM 
187810922SJeff.Bonwick@Sun.COM /*
187910922SJeff.Bonwick@Sun.COM  * Extended object types to report deferred frees and dedup auto-ditto blocks.
188010922SJeff.Bonwick@Sun.COM  */
188110922SJeff.Bonwick@Sun.COM #define	ZDB_OT_DEFERRED	(DMU_OT_NUMTYPES + 0)
188210922SJeff.Bonwick@Sun.COM #define	ZDB_OT_DITTO	(DMU_OT_NUMTYPES + 1)
188310922SJeff.Bonwick@Sun.COM #define	ZDB_OT_TOTAL	(DMU_OT_NUMTYPES + 2)
188410922SJeff.Bonwick@Sun.COM 
188510922SJeff.Bonwick@Sun.COM static char *zdb_ot_extname[] = {
188610922SJeff.Bonwick@Sun.COM 	"deferred free",
188710922SJeff.Bonwick@Sun.COM 	"dedup ditto",
188810922SJeff.Bonwick@Sun.COM 	"Total",
188910922SJeff.Bonwick@Sun.COM };
189010922SJeff.Bonwick@Sun.COM 
189110922SJeff.Bonwick@Sun.COM #define	ZB_TOTAL	DN_MAX_LEVELS
189210922SJeff.Bonwick@Sun.COM 
189310922SJeff.Bonwick@Sun.COM typedef struct zdb_cb {
189410922SJeff.Bonwick@Sun.COM 	zdb_blkstats_t	zcb_type[ZB_TOTAL + 1][ZDB_OT_TOTAL + 1];
189510922SJeff.Bonwick@Sun.COM 	uint64_t	zcb_dedup_asize;
189610922SJeff.Bonwick@Sun.COM 	uint64_t	zcb_dedup_blocks;
189710922SJeff.Bonwick@Sun.COM 	uint64_t	zcb_errors[256];
189810922SJeff.Bonwick@Sun.COM 	int		zcb_readfails;
189910922SJeff.Bonwick@Sun.COM 	int		zcb_haderrors;
190012470SMatthew.Ahrens@Sun.COM 	spa_t		*zcb_spa;
190110922SJeff.Bonwick@Sun.COM } zdb_cb_t;
190210922SJeff.Bonwick@Sun.COM 
190310922SJeff.Bonwick@Sun.COM static void
zdb_count_block(zdb_cb_t * zcb,zilog_t * zilog,const blkptr_t * bp,dmu_object_type_t type)190412470SMatthew.Ahrens@Sun.COM zdb_count_block(zdb_cb_t *zcb, zilog_t *zilog, const blkptr_t *bp,
190510922SJeff.Bonwick@Sun.COM     dmu_object_type_t type)
190610922SJeff.Bonwick@Sun.COM {
190710922SJeff.Bonwick@Sun.COM 	uint64_t refcnt = 0;
190810922SJeff.Bonwick@Sun.COM 
190910922SJeff.Bonwick@Sun.COM 	ASSERT(type < ZDB_OT_TOTAL);
191010922SJeff.Bonwick@Sun.COM 
191110922SJeff.Bonwick@Sun.COM 	if (zilog && zil_bp_tree_add(zilog, bp) != 0)
191210922SJeff.Bonwick@Sun.COM 		return;
191310922SJeff.Bonwick@Sun.COM 
191410922SJeff.Bonwick@Sun.COM 	for (int i = 0; i < 4; i++) {
191510922SJeff.Bonwick@Sun.COM 		int l = (i < 2) ? BP_GET_LEVEL(bp) : ZB_TOTAL;
191610922SJeff.Bonwick@Sun.COM 		int t = (i & 1) ? type : ZDB_OT_TOTAL;
191710922SJeff.Bonwick@Sun.COM 		zdb_blkstats_t *zb = &zcb->zcb_type[l][t];
191810922SJeff.Bonwick@Sun.COM 
191910922SJeff.Bonwick@Sun.COM 		zb->zb_asize += BP_GET_ASIZE(bp);
192010922SJeff.Bonwick@Sun.COM 		zb->zb_lsize += BP_GET_LSIZE(bp);
192110922SJeff.Bonwick@Sun.COM 		zb->zb_psize += BP_GET_PSIZE(bp);
192210922SJeff.Bonwick@Sun.COM 		zb->zb_count++;
192310922SJeff.Bonwick@Sun.COM 	}
192410922SJeff.Bonwick@Sun.COM 
192510922SJeff.Bonwick@Sun.COM 	if (dump_opt['L'])
192610922SJeff.Bonwick@Sun.COM 		return;
192710922SJeff.Bonwick@Sun.COM 
192810922SJeff.Bonwick@Sun.COM 	if (BP_GET_DEDUP(bp)) {
192910922SJeff.Bonwick@Sun.COM 		ddt_t *ddt;
193010922SJeff.Bonwick@Sun.COM 		ddt_entry_t *dde;
193110922SJeff.Bonwick@Sun.COM 
193212470SMatthew.Ahrens@Sun.COM 		ddt = ddt_select(zcb->zcb_spa, bp);
193310922SJeff.Bonwick@Sun.COM 		ddt_enter(ddt);
193410922SJeff.Bonwick@Sun.COM 		dde = ddt_lookup(ddt, bp, B_FALSE);
193510922SJeff.Bonwick@Sun.COM 
193610922SJeff.Bonwick@Sun.COM 		if (dde == NULL) {
193710922SJeff.Bonwick@Sun.COM 			refcnt = 0;
193810922SJeff.Bonwick@Sun.COM 		} else {
193910922SJeff.Bonwick@Sun.COM 			ddt_phys_t *ddp = ddt_phys_select(dde, bp);
194010922SJeff.Bonwick@Sun.COM 			ddt_phys_decref(ddp);
194110922SJeff.Bonwick@Sun.COM 			refcnt = ddp->ddp_refcnt;
194210922SJeff.Bonwick@Sun.COM 			if (ddt_phys_total_refcnt(dde) == 0)
194310922SJeff.Bonwick@Sun.COM 				ddt_remove(ddt, dde);
194410922SJeff.Bonwick@Sun.COM 		}
194510922SJeff.Bonwick@Sun.COM 		ddt_exit(ddt);
194610922SJeff.Bonwick@Sun.COM 	}
194710922SJeff.Bonwick@Sun.COM 
194812470SMatthew.Ahrens@Sun.COM 	VERIFY3U(zio_wait(zio_claim(NULL, zcb->zcb_spa,
194912470SMatthew.Ahrens@Sun.COM 	    refcnt ? 0 : spa_first_txg(zcb->zcb_spa),
195010922SJeff.Bonwick@Sun.COM 	    bp, NULL, NULL, ZIO_FLAG_CANFAIL)), ==, 0);
195110922SJeff.Bonwick@Sun.COM }
195210922SJeff.Bonwick@Sun.COM 
195312296SLin.Ling@Sun.COM /* ARGSUSED */
195410922SJeff.Bonwick@Sun.COM static int
zdb_blkptr_cb(spa_t * spa,zilog_t * zilog,const blkptr_t * bp,arc_buf_t * pbuf,const zbookmark_t * zb,const dnode_phys_t * dnp,void * arg)195512296SLin.Ling@Sun.COM zdb_blkptr_cb(spa_t *spa, zilog_t *zilog, const blkptr_t *bp, arc_buf_t *pbuf,
195610922SJeff.Bonwick@Sun.COM     const zbookmark_t *zb, const dnode_phys_t *dnp, void *arg)
195710922SJeff.Bonwick@Sun.COM {
195810922SJeff.Bonwick@Sun.COM 	zdb_cb_t *zcb = arg;
195910922SJeff.Bonwick@Sun.COM 	char blkbuf[BP_SPRINTF_LEN];
196010922SJeff.Bonwick@Sun.COM 	dmu_object_type_t type;
196110922SJeff.Bonwick@Sun.COM 	boolean_t is_metadata;
196210922SJeff.Bonwick@Sun.COM 
196310922SJeff.Bonwick@Sun.COM 	if (bp == NULL)
196410922SJeff.Bonwick@Sun.COM 		return (0);
196510922SJeff.Bonwick@Sun.COM 
196610922SJeff.Bonwick@Sun.COM 	type = BP_GET_TYPE(bp);
196710922SJeff.Bonwick@Sun.COM 
196812470SMatthew.Ahrens@Sun.COM 	zdb_count_block(zcb, zilog, bp, type);
196910922SJeff.Bonwick@Sun.COM 
197010922SJeff.Bonwick@Sun.COM 	is_metadata = (BP_GET_LEVEL(bp) != 0 || dmu_ot[type].ot_metadata);
197110922SJeff.Bonwick@Sun.COM 
197210922SJeff.Bonwick@Sun.COM 	if (dump_opt['c'] > 1 || (dump_opt['c'] && is_metadata)) {
197310922SJeff.Bonwick@Sun.COM 		int ioerr;
197410922SJeff.Bonwick@Sun.COM 		size_t size = BP_GET_PSIZE(bp);
197510922SJeff.Bonwick@Sun.COM 		void *data = malloc(size);
197610922SJeff.Bonwick@Sun.COM 		int flags = ZIO_FLAG_CANFAIL | ZIO_FLAG_SCRUB | ZIO_FLAG_RAW;
197710922SJeff.Bonwick@Sun.COM 
197810922SJeff.Bonwick@Sun.COM 		/* If it's an intent log block, failure is expected. */
197910922SJeff.Bonwick@Sun.COM 		if (zb->zb_level == ZB_ZIL_LEVEL)
198010922SJeff.Bonwick@Sun.COM 			flags |= ZIO_FLAG_SPECULATIVE;
198110922SJeff.Bonwick@Sun.COM 
198210922SJeff.Bonwick@Sun.COM 		ioerr = zio_wait(zio_read(NULL, spa, bp, data, size,
198310922SJeff.Bonwick@Sun.COM 		    NULL, NULL, ZIO_PRIORITY_ASYNC_READ, flags, zb));
198410922SJeff.Bonwick@Sun.COM 
198510922SJeff.Bonwick@Sun.COM 		free(data);
198610922SJeff.Bonwick@Sun.COM 
198710922SJeff.Bonwick@Sun.COM 		if (ioerr && !(flags & ZIO_FLAG_SPECULATIVE)) {
198810922SJeff.Bonwick@Sun.COM 			zcb->zcb_haderrors = 1;
198910922SJeff.Bonwick@Sun.COM 			zcb->zcb_errors[ioerr]++;
199010922SJeff.Bonwick@Sun.COM 
199110922SJeff.Bonwick@Sun.COM 			if (dump_opt['b'] >= 2)
199210922SJeff.Bonwick@Sun.COM 				sprintf_blkptr(blkbuf, bp);
199310922SJeff.Bonwick@Sun.COM 			else
199410922SJeff.Bonwick@Sun.COM 				blkbuf[0] = '\0';
199510922SJeff.Bonwick@Sun.COM 
199610922SJeff.Bonwick@Sun.COM 			(void) printf("zdb_blkptr_cb: "
199710922SJeff.Bonwick@Sun.COM 			    "Got error %d reading "
199810922SJeff.Bonwick@Sun.COM 			    "<%llu, %llu, %lld, %llx> %s -- skipping\n",
199910922SJeff.Bonwick@Sun.COM 			    ioerr,
200010922SJeff.Bonwick@Sun.COM 			    (u_longlong_t)zb->zb_objset,
200110922SJeff.Bonwick@Sun.COM 			    (u_longlong_t)zb->zb_object,
200210922SJeff.Bonwick@Sun.COM 			    (u_longlong_t)zb->zb_level,
200310922SJeff.Bonwick@Sun.COM 			    (u_longlong_t)zb->zb_blkid,
200410922SJeff.Bonwick@Sun.COM 			    blkbuf);
200510922SJeff.Bonwick@Sun.COM 		}
200610922SJeff.Bonwick@Sun.COM 	}
200710922SJeff.Bonwick@Sun.COM 
200810922SJeff.Bonwick@Sun.COM 	zcb->zcb_readfails = 0;
200910922SJeff.Bonwick@Sun.COM 
201010922SJeff.Bonwick@Sun.COM 	if (dump_opt['b'] >= 4) {
201110922SJeff.Bonwick@Sun.COM 		sprintf_blkptr(blkbuf, bp);
201210922SJeff.Bonwick@Sun.COM 		(void) printf("objset %llu object %llu "
201310922SJeff.Bonwick@Sun.COM 		    "level %lld offset 0x%llx %s\n",
201410922SJeff.Bonwick@Sun.COM 		    (u_longlong_t)zb->zb_objset,
201510922SJeff.Bonwick@Sun.COM 		    (u_longlong_t)zb->zb_object,
201610922SJeff.Bonwick@Sun.COM 		    (longlong_t)zb->zb_level,
201710922SJeff.Bonwick@Sun.COM 		    (u_longlong_t)blkid2offset(dnp, bp, zb),
201810922SJeff.Bonwick@Sun.COM 		    blkbuf);
201910922SJeff.Bonwick@Sun.COM 	}
202010922SJeff.Bonwick@Sun.COM 
202110922SJeff.Bonwick@Sun.COM 	return (0);
202210922SJeff.Bonwick@Sun.COM }
202310922SJeff.Bonwick@Sun.COM 
2024789Sahrens static void
zdb_leak(space_map_t * sm,uint64_t start,uint64_t size)20257754SJeff.Bonwick@Sun.COM zdb_leak(space_map_t *sm, uint64_t start, uint64_t size)
2026789Sahrens {
20277754SJeff.Bonwick@Sun.COM 	vdev_t *vd = sm->sm_ppd;
2028789Sahrens 
20297754SJeff.Bonwick@Sun.COM 	(void) printf("leaked space: vdev %llu, offset 0x%llx, size %llu\n",
20307754SJeff.Bonwick@Sun.COM 	    (u_longlong_t)vd->vdev_id, (u_longlong_t)start, (u_longlong_t)size);
2031789Sahrens }
2032789Sahrens 
20337754SJeff.Bonwick@Sun.COM /* ARGSUSED */
20347754SJeff.Bonwick@Sun.COM static void
zdb_space_map_load(space_map_t * sm)20357754SJeff.Bonwick@Sun.COM zdb_space_map_load(space_map_t *sm)
2036789Sahrens {
2037789Sahrens }
2038789Sahrens 
2039789Sahrens static void
zdb_space_map_unload(space_map_t * sm)20407754SJeff.Bonwick@Sun.COM zdb_space_map_unload(space_map_t *sm)
2041789Sahrens {
20427754SJeff.Bonwick@Sun.COM 	space_map_vacate(sm, zdb_leak, sm);
20437754SJeff.Bonwick@Sun.COM }
2044789Sahrens 
20457754SJeff.Bonwick@Sun.COM /* ARGSUSED */
20467754SJeff.Bonwick@Sun.COM static void
zdb_space_map_claim(space_map_t * sm,uint64_t start,uint64_t size)20477754SJeff.Bonwick@Sun.COM zdb_space_map_claim(space_map_t *sm, uint64_t start, uint64_t size)
20487754SJeff.Bonwick@Sun.COM {
20497754SJeff.Bonwick@Sun.COM }
2050789Sahrens 
20517754SJeff.Bonwick@Sun.COM static space_map_ops_t zdb_space_map_ops = {
20527754SJeff.Bonwick@Sun.COM 	zdb_space_map_load,
20537754SJeff.Bonwick@Sun.COM 	zdb_space_map_unload,
20547754SJeff.Bonwick@Sun.COM 	NULL,	/* alloc */
20557754SJeff.Bonwick@Sun.COM 	zdb_space_map_claim,
20569480SGeorge.Wilson@Sun.COM 	NULL,	/* free */
20579480SGeorge.Wilson@Sun.COM 	NULL	/* maxsize */
20587754SJeff.Bonwick@Sun.COM };
2059789Sahrens 
2060789Sahrens static void
zdb_ddt_leak_init(spa_t * spa,zdb_cb_t * zcb)206111125SJeff.Bonwick@Sun.COM zdb_ddt_leak_init(spa_t *spa, zdb_cb_t *zcb)
2062789Sahrens {
206311125SJeff.Bonwick@Sun.COM 	ddt_bookmark_t ddb = { 0 };
206410922SJeff.Bonwick@Sun.COM 	ddt_entry_t dde;
206510922SJeff.Bonwick@Sun.COM 	int error;
206610922SJeff.Bonwick@Sun.COM 
206711125SJeff.Bonwick@Sun.COM 	while ((error = ddt_walk(spa, &ddb, &dde)) == 0) {
206810922SJeff.Bonwick@Sun.COM 		blkptr_t blk;
206910922SJeff.Bonwick@Sun.COM 		ddt_phys_t *ddp = dde.dde_phys;
207011125SJeff.Bonwick@Sun.COM 
207111125SJeff.Bonwick@Sun.COM 		if (ddb.ddb_class == DDT_CLASS_UNIQUE)
207211125SJeff.Bonwick@Sun.COM 			return;
207311125SJeff.Bonwick@Sun.COM 
207410922SJeff.Bonwick@Sun.COM 		ASSERT(ddt_phys_total_refcnt(&dde) > 1);
207511125SJeff.Bonwick@Sun.COM 
207610922SJeff.Bonwick@Sun.COM 		for (int p = 0; p < DDT_PHYS_TYPES; p++, ddp++) {
207710922SJeff.Bonwick@Sun.COM 			if (ddp->ddp_phys_birth == 0)
207810922SJeff.Bonwick@Sun.COM 				continue;
207911125SJeff.Bonwick@Sun.COM 			ddt_bp_create(ddb.ddb_checksum,
208011125SJeff.Bonwick@Sun.COM 			    &dde.dde_key, ddp, &blk);
208110922SJeff.Bonwick@Sun.COM 			if (p == DDT_PHYS_DITTO) {
208212470SMatthew.Ahrens@Sun.COM 				zdb_count_block(zcb, NULL, &blk, ZDB_OT_DITTO);
208310922SJeff.Bonwick@Sun.COM 			} else {
208410922SJeff.Bonwick@Sun.COM 				zcb->zcb_dedup_asize +=
208510922SJeff.Bonwick@Sun.COM 				    BP_GET_ASIZE(&blk) * (ddp->ddp_refcnt - 1);
208610922SJeff.Bonwick@Sun.COM 				zcb->zcb_dedup_blocks++;
208710922SJeff.Bonwick@Sun.COM 			}
208810922SJeff.Bonwick@Sun.COM 		}
208910922SJeff.Bonwick@Sun.COM 		if (!dump_opt['L']) {
209011125SJeff.Bonwick@Sun.COM 			ddt_t *ddt = spa->spa_ddt[ddb.ddb_checksum];
209110922SJeff.Bonwick@Sun.COM 			ddt_enter(ddt);
209210922SJeff.Bonwick@Sun.COM 			VERIFY(ddt_lookup(ddt, &blk, B_TRUE) != NULL);
209310922SJeff.Bonwick@Sun.COM 			ddt_exit(ddt);
2094789Sahrens 		}
2095789Sahrens 	}
209610922SJeff.Bonwick@Sun.COM 
209710922SJeff.Bonwick@Sun.COM 	ASSERT(error == ENOENT);
209810922SJeff.Bonwick@Sun.COM }
209910922SJeff.Bonwick@Sun.COM 
210010922SJeff.Bonwick@Sun.COM static void
zdb_leak_init(spa_t * spa,zdb_cb_t * zcb)210110922SJeff.Bonwick@Sun.COM zdb_leak_init(spa_t *spa, zdb_cb_t *zcb)
210210922SJeff.Bonwick@Sun.COM {
210312470SMatthew.Ahrens@Sun.COM 	zcb->zcb_spa = spa;
210412470SMatthew.Ahrens@Sun.COM 
210510922SJeff.Bonwick@Sun.COM 	if (!dump_opt['L']) {
210610922SJeff.Bonwick@Sun.COM 		vdev_t *rvd = spa->spa_root_vdev;
210710922SJeff.Bonwick@Sun.COM 		for (int c = 0; c < rvd->vdev_children; c++) {
210810922SJeff.Bonwick@Sun.COM 			vdev_t *vd = rvd->vdev_child[c];
210910922SJeff.Bonwick@Sun.COM 			for (int m = 0; m < vd->vdev_ms_count; m++) {
211010922SJeff.Bonwick@Sun.COM 				metaslab_t *msp = vd->vdev_ms[m];
211110922SJeff.Bonwick@Sun.COM 				mutex_enter(&msp->ms_lock);
211210922SJeff.Bonwick@Sun.COM 				space_map_unload(&msp->ms_map);
211310922SJeff.Bonwick@Sun.COM 				VERIFY(space_map_load(&msp->ms_map,
211410922SJeff.Bonwick@Sun.COM 				    &zdb_space_map_ops, SM_ALLOC, &msp->ms_smo,
211510922SJeff.Bonwick@Sun.COM 				    spa->spa_meta_objset) == 0);
211610922SJeff.Bonwick@Sun.COM 				msp->ms_map.sm_ppd = vd;
211710922SJeff.Bonwick@Sun.COM 				mutex_exit(&msp->ms_lock);
211810922SJeff.Bonwick@Sun.COM 			}
211910922SJeff.Bonwick@Sun.COM 		}
212010922SJeff.Bonwick@Sun.COM 	}
212110922SJeff.Bonwick@Sun.COM 
212210922SJeff.Bonwick@Sun.COM 	spa_config_enter(spa, SCL_CONFIG, FTAG, RW_READER);
212310922SJeff.Bonwick@Sun.COM 
212411125SJeff.Bonwick@Sun.COM 	zdb_ddt_leak_init(spa, zcb);
212510922SJeff.Bonwick@Sun.COM 
212610922SJeff.Bonwick@Sun.COM 	spa_config_exit(spa, SCL_CONFIG, FTAG);
2127789Sahrens }
2128789Sahrens 
2129789Sahrens static void
zdb_leak_fini(spa_t * spa)21307754SJeff.Bonwick@Sun.COM zdb_leak_fini(spa_t *spa)
2131789Sahrens {
213210922SJeff.Bonwick@Sun.COM 	if (!dump_opt['L']) {
213310922SJeff.Bonwick@Sun.COM 		vdev_t *rvd = spa->spa_root_vdev;
213410922SJeff.Bonwick@Sun.COM 		for (int c = 0; c < rvd->vdev_children; c++) {
213510922SJeff.Bonwick@Sun.COM 			vdev_t *vd = rvd->vdev_child[c];
213610922SJeff.Bonwick@Sun.COM 			for (int m = 0; m < vd->vdev_ms_count; m++) {
213710922SJeff.Bonwick@Sun.COM 				metaslab_t *msp = vd->vdev_ms[m];
213810922SJeff.Bonwick@Sun.COM 				mutex_enter(&msp->ms_lock);
213910922SJeff.Bonwick@Sun.COM 				space_map_unload(&msp->ms_map);
214010922SJeff.Bonwick@Sun.COM 				mutex_exit(&msp->ms_lock);
21417837SMatthew.Ahrens@Sun.COM 			}
21426217Sek110237 		}
2143789Sahrens 	}
2144789Sahrens }
2145789Sahrens 
214612470SMatthew.Ahrens@Sun.COM /* ARGSUSED */
214712470SMatthew.Ahrens@Sun.COM static int
count_block_cb(void * arg,const blkptr_t * bp,dmu_tx_t * tx)214812470SMatthew.Ahrens@Sun.COM count_block_cb(void *arg, const blkptr_t *bp, dmu_tx_t *tx)
214912470SMatthew.Ahrens@Sun.COM {
215012470SMatthew.Ahrens@Sun.COM 	zdb_cb_t *zcb = arg;
215112470SMatthew.Ahrens@Sun.COM 
215212470SMatthew.Ahrens@Sun.COM 	if (dump_opt['b'] >= 4) {
215312470SMatthew.Ahrens@Sun.COM 		char blkbuf[BP_SPRINTF_LEN];
215412470SMatthew.Ahrens@Sun.COM 		sprintf_blkptr(blkbuf, bp);
215512470SMatthew.Ahrens@Sun.COM 		(void) printf("[%s] %s\n",
215612470SMatthew.Ahrens@Sun.COM 		    "deferred free", blkbuf);
215712470SMatthew.Ahrens@Sun.COM 	}
215812470SMatthew.Ahrens@Sun.COM 	zdb_count_block(zcb, NULL, bp, ZDB_OT_DEFERRED);
215912470SMatthew.Ahrens@Sun.COM 	return (0);
216012470SMatthew.Ahrens@Sun.COM }
216112470SMatthew.Ahrens@Sun.COM 
2162789Sahrens static int
dump_block_stats(spa_t * spa)2163789Sahrens dump_block_stats(spa_t *spa)
2164789Sahrens {
2165789Sahrens 	zdb_cb_t zcb = { 0 };
2166789Sahrens 	zdb_blkstats_t *zb, *tzb;
216710922SJeff.Bonwick@Sun.COM 	uint64_t norm_alloc, norm_space, total_alloc, total_found;
216811724SVictor.Latushkin@Sun.COM 	int flags = TRAVERSE_PRE | TRAVERSE_PREFETCH_METADATA | TRAVERSE_HARD;
2169789Sahrens 	int leaks = 0;
217010922SJeff.Bonwick@Sun.COM 
217110922SJeff.Bonwick@Sun.COM 	(void) printf("\nTraversing all blocks %s%s%s%s%s...\n",
217210922SJeff.Bonwick@Sun.COM 	    (dump_opt['c'] || !dump_opt['L']) ? "to verify " : "",
217310922SJeff.Bonwick@Sun.COM 	    (dump_opt['c'] == 1) ? "metadata " : "",
217410922SJeff.Bonwick@Sun.COM 	    dump_opt['c'] ? "checksums " : "",
217510922SJeff.Bonwick@Sun.COM 	    (dump_opt['c'] && !dump_opt['L']) ? "and verify " : "",
217610922SJeff.Bonwick@Sun.COM 	    !dump_opt['L'] ? "nothing leaked " : "");
2177789Sahrens 
2178789Sahrens 	/*
21797754SJeff.Bonwick@Sun.COM 	 * Load all space maps as SM_ALLOC maps, then traverse the pool
21807754SJeff.Bonwick@Sun.COM 	 * claiming each block we discover.  If the pool is perfectly
21817754SJeff.Bonwick@Sun.COM 	 * consistent, the space maps will be empty when we're done.
21827754SJeff.Bonwick@Sun.COM 	 * Anything left over is a leak; any block we can't claim (because
21837754SJeff.Bonwick@Sun.COM 	 * it's not part of any space map) is a double allocation,
21847754SJeff.Bonwick@Sun.COM 	 * reference to a freed block, or an unclaimed log block.
2185789Sahrens 	 */
218610922SJeff.Bonwick@Sun.COM 	zdb_leak_init(spa, &zcb);
2187789Sahrens 
2188789Sahrens 	/*
2189789Sahrens 	 * If there's a deferred-free bplist, process that first.
2190789Sahrens 	 */
219112470SMatthew.Ahrens@Sun.COM 	(void) bpobj_iterate_nofree(&spa->spa_deferred_bpobj,
219212470SMatthew.Ahrens@Sun.COM 	    count_block_cb, &zcb, NULL);
219312470SMatthew.Ahrens@Sun.COM 	(void) bpobj_iterate_nofree(&spa->spa_dsl_pool->dp_free_bpobj,
219412470SMatthew.Ahrens@Sun.COM 	    count_block_cb, &zcb, NULL);
2195789Sahrens 
219611125SJeff.Bonwick@Sun.COM 	if (dump_opt['c'] > 1)
219711125SJeff.Bonwick@Sun.COM 		flags |= TRAVERSE_PREFETCH_DATA;
219811125SJeff.Bonwick@Sun.COM 
219911125SJeff.Bonwick@Sun.COM 	zcb.zcb_haderrors |= traverse_pool(spa, 0, flags, zdb_blkptr_cb, &zcb);
2200789Sahrens 
220110922SJeff.Bonwick@Sun.COM 	if (zcb.zcb_haderrors) {
2202789Sahrens 		(void) printf("\nError counts:\n\n");
2203789Sahrens 		(void) printf("\t%5s  %s\n", "errno", "count");
220410922SJeff.Bonwick@Sun.COM 		for (int e = 0; e < 256; e++) {
2205789Sahrens 			if (zcb.zcb_errors[e] != 0) {
2206789Sahrens 				(void) printf("\t%5d  %llu\n",
2207789Sahrens 				    e, (u_longlong_t)zcb.zcb_errors[e]);
2208789Sahrens 			}
2209789Sahrens 		}
2210789Sahrens 	}
2211789Sahrens 
2212789Sahrens 	/*
2213789Sahrens 	 * Report any leaked segments.
2214789Sahrens 	 */
221510922SJeff.Bonwick@Sun.COM 	zdb_leak_fini(spa);
221610922SJeff.Bonwick@Sun.COM 
221710922SJeff.Bonwick@Sun.COM 	tzb = &zcb.zcb_type[ZB_TOTAL][ZDB_OT_TOTAL];
221810922SJeff.Bonwick@Sun.COM 
221910922SJeff.Bonwick@Sun.COM 	norm_alloc = metaslab_class_get_alloc(spa_normal_class(spa));
222010922SJeff.Bonwick@Sun.COM 	norm_space = metaslab_class_get_space(spa_normal_class(spa));
222110922SJeff.Bonwick@Sun.COM 
222210922SJeff.Bonwick@Sun.COM 	total_alloc = norm_alloc + metaslab_class_get_alloc(spa_log_class(spa));
222310922SJeff.Bonwick@Sun.COM 	total_found = tzb->zb_asize - zcb.zcb_dedup_asize;
222410922SJeff.Bonwick@Sun.COM 
222510922SJeff.Bonwick@Sun.COM 	if (total_found == total_alloc) {
22268121SVictor.Latushkin@Sun.COM 		if (!dump_opt['L'])
22278121SVictor.Latushkin@Sun.COM 			(void) printf("\n\tNo leaks (block sum matches space"
22288121SVictor.Latushkin@Sun.COM 			    " maps exactly)\n");
2229789Sahrens 	} else {
2230789Sahrens 		(void) printf("block traversal size %llu != alloc %llu "
22318121SVictor.Latushkin@Sun.COM 		    "(%s %lld)\n",
223210922SJeff.Bonwick@Sun.COM 		    (u_longlong_t)total_found,
223310922SJeff.Bonwick@Sun.COM 		    (u_longlong_t)total_alloc,
22348121SVictor.Latushkin@Sun.COM 		    (dump_opt['L']) ? "unreachable" : "leaked",
223510922SJeff.Bonwick@Sun.COM 		    (longlong_t)(total_alloc - total_found));
2236789Sahrens 		leaks = 1;
2237789Sahrens 	}
2238789Sahrens 
2239789Sahrens 	if (tzb->zb_count == 0)
2240789Sahrens 		return (2);
2241789Sahrens 
2242789Sahrens 	(void) printf("\n");
2243789Sahrens 	(void) printf("\tbp count:      %10llu\n",
2244789Sahrens 	    (u_longlong_t)tzb->zb_count);
224510922SJeff.Bonwick@Sun.COM 	(void) printf("\tbp logical:    %10llu      avg: %6llu\n",
2246789Sahrens 	    (u_longlong_t)tzb->zb_lsize,
2247789Sahrens 	    (u_longlong_t)(tzb->zb_lsize / tzb->zb_count));
224810922SJeff.Bonwick@Sun.COM 	(void) printf("\tbp physical:   %10llu      avg:"
224910922SJeff.Bonwick@Sun.COM 	    " %6llu     compression: %6.2f\n",
2250789Sahrens 	    (u_longlong_t)tzb->zb_psize,
2251789Sahrens 	    (u_longlong_t)(tzb->zb_psize / tzb->zb_count),
2252789Sahrens 	    (double)tzb->zb_lsize / tzb->zb_psize);
225310922SJeff.Bonwick@Sun.COM 	(void) printf("\tbp allocated:  %10llu      avg:"
225410922SJeff.Bonwick@Sun.COM 	    " %6llu     compression: %6.2f\n",
2255789Sahrens 	    (u_longlong_t)tzb->zb_asize,
2256789Sahrens 	    (u_longlong_t)(tzb->zb_asize / tzb->zb_count),
2257789Sahrens 	    (double)tzb->zb_lsize / tzb->zb_asize);
225810922SJeff.Bonwick@Sun.COM 	(void) printf("\tbp deduped:    %10llu    ref>1:"
225910922SJeff.Bonwick@Sun.COM 	    " %6llu   deduplication: %6.2f\n",
226010922SJeff.Bonwick@Sun.COM 	    (u_longlong_t)zcb.zcb_dedup_asize,
226110922SJeff.Bonwick@Sun.COM 	    (u_longlong_t)zcb.zcb_dedup_blocks,
226210922SJeff.Bonwick@Sun.COM 	    (double)zcb.zcb_dedup_asize / tzb->zb_asize + 1.0);
226310922SJeff.Bonwick@Sun.COM 	(void) printf("\tSPA allocated: %10llu     used: %5.2f%%\n",
226410922SJeff.Bonwick@Sun.COM 	    (u_longlong_t)norm_alloc, 100.0 * norm_alloc / norm_space);
2265789Sahrens 
2266789Sahrens 	if (dump_opt['b'] >= 2) {
2267789Sahrens 		int l, t, level;
2268789Sahrens 		(void) printf("\nBlocks\tLSIZE\tPSIZE\tASIZE"
2269789Sahrens 		    "\t  avg\t comp\t%%Total\tType\n");
2270789Sahrens 
227110922SJeff.Bonwick@Sun.COM 		for (t = 0; t <= ZDB_OT_TOTAL; t++) {
227212296SLin.Ling@Sun.COM 			char csize[32], lsize[32], psize[32], asize[32];
227312296SLin.Ling@Sun.COM 			char avg[32];
2274789Sahrens 			char *typename;
2275789Sahrens 
227610922SJeff.Bonwick@Sun.COM 			if (t < DMU_OT_NUMTYPES)
227710922SJeff.Bonwick@Sun.COM 				typename = dmu_ot[t].ot_name;
227810922SJeff.Bonwick@Sun.COM 			else
227910922SJeff.Bonwick@Sun.COM 				typename = zdb_ot_extname[t - DMU_OT_NUMTYPES];
2280789Sahrens 
2281789Sahrens 			if (zcb.zcb_type[ZB_TOTAL][t].zb_asize == 0) {
2282789Sahrens 				(void) printf("%6s\t%5s\t%5s\t%5s"
2283789Sahrens 				    "\t%5s\t%5s\t%6s\t%s\n",
2284789Sahrens 				    "-",
2285789Sahrens 				    "-",
2286789Sahrens 				    "-",
2287789Sahrens 				    "-",
2288789Sahrens 				    "-",
2289789Sahrens 				    "-",
2290789Sahrens 				    "-",
2291789Sahrens 				    typename);
2292789Sahrens 				continue;
2293789Sahrens 			}
2294789Sahrens 
2295789Sahrens 			for (l = ZB_TOTAL - 1; l >= -1; l--) {
2296789Sahrens 				level = (l == -1 ? ZB_TOTAL : l);
2297789Sahrens 				zb = &zcb.zcb_type[level][t];
2298789Sahrens 
2299789Sahrens 				if (zb->zb_asize == 0)
2300789Sahrens 					continue;
2301789Sahrens 
2302789Sahrens 				if (dump_opt['b'] < 3 && level != ZB_TOTAL)
2303789Sahrens 					continue;
2304789Sahrens 
2305789Sahrens 				if (level == 0 && zb->zb_asize ==
2306789Sahrens 				    zcb.zcb_type[ZB_TOTAL][t].zb_asize)
2307789Sahrens 					continue;
2308789Sahrens 
230912296SLin.Ling@Sun.COM 				zdb_nicenum(zb->zb_count, csize);
231012296SLin.Ling@Sun.COM 				zdb_nicenum(zb->zb_lsize, lsize);
231112296SLin.Ling@Sun.COM 				zdb_nicenum(zb->zb_psize, psize);
231212296SLin.Ling@Sun.COM 				zdb_nicenum(zb->zb_asize, asize);
231312296SLin.Ling@Sun.COM 				zdb_nicenum(zb->zb_asize / zb->zb_count, avg);
2314789Sahrens 
2315789Sahrens 				(void) printf("%6s\t%5s\t%5s\t%5s\t%5s"
2316789Sahrens 				    "\t%5.2f\t%6.2f\t",
2317789Sahrens 				    csize, lsize, psize, asize, avg,
2318789Sahrens 				    (double)zb->zb_lsize / zb->zb_psize,
2319789Sahrens 				    100.0 * zb->zb_asize / tzb->zb_asize);
2320789Sahrens 
2321789Sahrens 				if (level == ZB_TOTAL)
2322789Sahrens 					(void) printf("%s\n", typename);
2323789Sahrens 				else
2324789Sahrens 					(void) printf("    L%d %s\n",
2325789Sahrens 					    level, typename);
2326789Sahrens 			}
2327789Sahrens 		}
2328789Sahrens 	}
2329789Sahrens 
2330789Sahrens 	(void) printf("\n");
2331789Sahrens 
2332789Sahrens 	if (leaks)
2333789Sahrens 		return (2);
2334789Sahrens 
2335789Sahrens 	if (zcb.zcb_haderrors)
2336789Sahrens 		return (3);
2337789Sahrens 
2338789Sahrens 	return (0);
2339789Sahrens }
2340789Sahrens 
234110922SJeff.Bonwick@Sun.COM typedef struct zdb_ddt_entry {
234210922SJeff.Bonwick@Sun.COM 	ddt_key_t	zdde_key;
234310922SJeff.Bonwick@Sun.COM 	uint64_t	zdde_ref_blocks;
234410922SJeff.Bonwick@Sun.COM 	uint64_t	zdde_ref_lsize;
234510922SJeff.Bonwick@Sun.COM 	uint64_t	zdde_ref_psize;
234610922SJeff.Bonwick@Sun.COM 	uint64_t	zdde_ref_dsize;
234710922SJeff.Bonwick@Sun.COM 	avl_node_t	zdde_node;
234810922SJeff.Bonwick@Sun.COM } zdb_ddt_entry_t;
234910922SJeff.Bonwick@Sun.COM 
235010922SJeff.Bonwick@Sun.COM /* ARGSUSED */
235110922SJeff.Bonwick@Sun.COM static int
zdb_ddt_add_cb(spa_t * spa,zilog_t * zilog,const blkptr_t * bp,arc_buf_t * pbuf,const zbookmark_t * zb,const dnode_phys_t * dnp,void * arg)235210922SJeff.Bonwick@Sun.COM zdb_ddt_add_cb(spa_t *spa, zilog_t *zilog, const blkptr_t *bp,
235312296SLin.Ling@Sun.COM     arc_buf_t *pbuf, const zbookmark_t *zb, const dnode_phys_t *dnp, void *arg)
235410922SJeff.Bonwick@Sun.COM {
235510922SJeff.Bonwick@Sun.COM 	avl_tree_t *t = arg;
235610922SJeff.Bonwick@Sun.COM 	avl_index_t where;
235710922SJeff.Bonwick@Sun.COM 	zdb_ddt_entry_t *zdde, zdde_search;
235810922SJeff.Bonwick@Sun.COM 
235910922SJeff.Bonwick@Sun.COM 	if (bp == NULL)
236010922SJeff.Bonwick@Sun.COM 		return (0);
236110922SJeff.Bonwick@Sun.COM 
236210922SJeff.Bonwick@Sun.COM 	if (dump_opt['S'] > 1 && zb->zb_level == ZB_ROOT_LEVEL) {
236310922SJeff.Bonwick@Sun.COM 		(void) printf("traversing objset %llu, %llu objects, "
236410922SJeff.Bonwick@Sun.COM 		    "%lu blocks so far\n",
236510922SJeff.Bonwick@Sun.COM 		    (u_longlong_t)zb->zb_objset,
236610922SJeff.Bonwick@Sun.COM 		    (u_longlong_t)bp->blk_fill,
236710922SJeff.Bonwick@Sun.COM 		    avl_numnodes(t));
236810922SJeff.Bonwick@Sun.COM 	}
236910922SJeff.Bonwick@Sun.COM 
237011125SJeff.Bonwick@Sun.COM 	if (BP_IS_HOLE(bp) || BP_GET_CHECKSUM(bp) == ZIO_CHECKSUM_OFF ||
237111125SJeff.Bonwick@Sun.COM 	    BP_GET_LEVEL(bp) > 0 || dmu_ot[BP_GET_TYPE(bp)].ot_metadata)
237210922SJeff.Bonwick@Sun.COM 		return (0);
237310922SJeff.Bonwick@Sun.COM 
237410922SJeff.Bonwick@Sun.COM 	ddt_key_fill(&zdde_search.zdde_key, bp);
237510922SJeff.Bonwick@Sun.COM 
237610922SJeff.Bonwick@Sun.COM 	zdde = avl_find(t, &zdde_search, &where);
237710922SJeff.Bonwick@Sun.COM 
237810922SJeff.Bonwick@Sun.COM 	if (zdde == NULL) {
237910922SJeff.Bonwick@Sun.COM 		zdde = umem_zalloc(sizeof (*zdde), UMEM_NOFAIL);
238010922SJeff.Bonwick@Sun.COM 		zdde->zdde_key = zdde_search.zdde_key;
238110922SJeff.Bonwick@Sun.COM 		avl_insert(t, zdde, where);
238210922SJeff.Bonwick@Sun.COM 	}
238310922SJeff.Bonwick@Sun.COM 
238410922SJeff.Bonwick@Sun.COM 	zdde->zdde_ref_blocks += 1;
238510922SJeff.Bonwick@Sun.COM 	zdde->zdde_ref_lsize += BP_GET_LSIZE(bp);
238610922SJeff.Bonwick@Sun.COM 	zdde->zdde_ref_psize += BP_GET_PSIZE(bp);
238710922SJeff.Bonwick@Sun.COM 	zdde->zdde_ref_dsize += bp_get_dsize_sync(spa, bp);
238810922SJeff.Bonwick@Sun.COM 
238910922SJeff.Bonwick@Sun.COM 	return (0);
239010922SJeff.Bonwick@Sun.COM }
239110922SJeff.Bonwick@Sun.COM 
239210922SJeff.Bonwick@Sun.COM static void
dump_simulated_ddt(spa_t * spa)239310922SJeff.Bonwick@Sun.COM dump_simulated_ddt(spa_t *spa)
239410922SJeff.Bonwick@Sun.COM {
239510922SJeff.Bonwick@Sun.COM 	avl_tree_t t;
239610922SJeff.Bonwick@Sun.COM 	void *cookie = NULL;
239710922SJeff.Bonwick@Sun.COM 	zdb_ddt_entry_t *zdde;
239810922SJeff.Bonwick@Sun.COM 	ddt_histogram_t ddh_total = { 0 };
239910922SJeff.Bonwick@Sun.COM 	ddt_stat_t dds_total = { 0 };
240010922SJeff.Bonwick@Sun.COM 
240110922SJeff.Bonwick@Sun.COM 	avl_create(&t, ddt_entry_compare,
240210922SJeff.Bonwick@Sun.COM 	    sizeof (zdb_ddt_entry_t), offsetof(zdb_ddt_entry_t, zdde_node));
240310922SJeff.Bonwick@Sun.COM 
240410922SJeff.Bonwick@Sun.COM 	spa_config_enter(spa, SCL_CONFIG, FTAG, RW_READER);
240510922SJeff.Bonwick@Sun.COM 
240611125SJeff.Bonwick@Sun.COM 	(void) traverse_pool(spa, 0, TRAVERSE_PRE | TRAVERSE_PREFETCH_METADATA,
240711125SJeff.Bonwick@Sun.COM 	    zdb_ddt_add_cb, &t);
240810922SJeff.Bonwick@Sun.COM 
240910922SJeff.Bonwick@Sun.COM 	spa_config_exit(spa, SCL_CONFIG, FTAG);
241010922SJeff.Bonwick@Sun.COM 
241110922SJeff.Bonwick@Sun.COM 	while ((zdde = avl_destroy_nodes(&t, &cookie)) != NULL) {
241210922SJeff.Bonwick@Sun.COM 		ddt_stat_t dds;
241310922SJeff.Bonwick@Sun.COM 		uint64_t refcnt = zdde->zdde_ref_blocks;
241410922SJeff.Bonwick@Sun.COM 		ASSERT(refcnt != 0);
241510922SJeff.Bonwick@Sun.COM 
241610922SJeff.Bonwick@Sun.COM 		dds.dds_blocks = zdde->zdde_ref_blocks / refcnt;
241710922SJeff.Bonwick@Sun.COM 		dds.dds_lsize = zdde->zdde_ref_lsize / refcnt;
241810922SJeff.Bonwick@Sun.COM 		dds.dds_psize = zdde->zdde_ref_psize / refcnt;
241910922SJeff.Bonwick@Sun.COM 		dds.dds_dsize = zdde->zdde_ref_dsize / refcnt;
242010922SJeff.Bonwick@Sun.COM 
242110922SJeff.Bonwick@Sun.COM 		dds.dds_ref_blocks = zdde->zdde_ref_blocks;
242210922SJeff.Bonwick@Sun.COM 		dds.dds_ref_lsize = zdde->zdde_ref_lsize;
242310922SJeff.Bonwick@Sun.COM 		dds.dds_ref_psize = zdde->zdde_ref_psize;
242410922SJeff.Bonwick@Sun.COM 		dds.dds_ref_dsize = zdde->zdde_ref_dsize;
242510922SJeff.Bonwick@Sun.COM 
242610922SJeff.Bonwick@Sun.COM 		ddt_stat_add(&ddh_total.ddh_stat[highbit(refcnt) - 1], &dds, 0);
242710922SJeff.Bonwick@Sun.COM 
242810922SJeff.Bonwick@Sun.COM 		umem_free(zdde, sizeof (*zdde));
242910922SJeff.Bonwick@Sun.COM 	}
243010922SJeff.Bonwick@Sun.COM 
243110922SJeff.Bonwick@Sun.COM 	avl_destroy(&t);
243210922SJeff.Bonwick@Sun.COM 
243310922SJeff.Bonwick@Sun.COM 	ddt_histogram_stat(&dds_total, &ddh_total);
243410922SJeff.Bonwick@Sun.COM 
243510922SJeff.Bonwick@Sun.COM 	(void) printf("Simulated DDT histogram:\n");
243610922SJeff.Bonwick@Sun.COM 
243711149SGeorge.Wilson@Sun.COM 	zpool_dump_ddt(&dds_total, &ddh_total);
243810922SJeff.Bonwick@Sun.COM 
243910922SJeff.Bonwick@Sun.COM 	dump_dedup_ratio(&dds_total);
244010922SJeff.Bonwick@Sun.COM }
244110922SJeff.Bonwick@Sun.COM 
2442789Sahrens static void
dump_zpool(spa_t * spa)2443789Sahrens dump_zpool(spa_t *spa)
2444789Sahrens {
2445789Sahrens 	dsl_pool_t *dp = spa_get_dsl(spa);
2446789Sahrens 	int rc = 0;
2447789Sahrens 
244810922SJeff.Bonwick@Sun.COM 	if (dump_opt['S']) {
244910922SJeff.Bonwick@Sun.COM 		dump_simulated_ddt(spa);
245010922SJeff.Bonwick@Sun.COM 		return;
245110922SJeff.Bonwick@Sun.COM 	}
245210922SJeff.Bonwick@Sun.COM 
245310860SVictor.Latushkin@Sun.COM 	if (!dump_opt['e'] && dump_opt['C'] > 1) {
245410860SVictor.Latushkin@Sun.COM 		(void) printf("\nCached configuration:\n");
245510860SVictor.Latushkin@Sun.COM 		dump_nvlist(spa->spa_config, 8);
245610860SVictor.Latushkin@Sun.COM 	}
245710860SVictor.Latushkin@Sun.COM 
245810860SVictor.Latushkin@Sun.COM 	if (dump_opt['C'])
245910860SVictor.Latushkin@Sun.COM 		dump_config(spa);
246010860SVictor.Latushkin@Sun.COM 
2461789Sahrens 	if (dump_opt['u'])
246211725SVictor.Latushkin@Sun.COM 		dump_uberblock(&spa->spa_uberblock, "\nUberblock:\n", "\n");
2463789Sahrens 
246410922SJeff.Bonwick@Sun.COM 	if (dump_opt['D'])
246510922SJeff.Bonwick@Sun.COM 		dump_all_ddts(spa);
246610922SJeff.Bonwick@Sun.COM 
246710861SVictor.Latushkin@Sun.COM 	if (dump_opt['d'] > 2 || dump_opt['m'])
246810861SVictor.Latushkin@Sun.COM 		dump_metaslabs(spa);
246910861SVictor.Latushkin@Sun.COM 
247010861SVictor.Latushkin@Sun.COM 	if (dump_opt['d'] || dump_opt['i']) {
2471789Sahrens 		dump_dir(dp->dp_meta_objset);
2472789Sahrens 		if (dump_opt['d'] >= 3) {
247312470SMatthew.Ahrens@Sun.COM 			dump_bpobj(&spa->spa_deferred_bpobj, "Deferred frees");
247412470SMatthew.Ahrens@Sun.COM 			if (spa_version(spa) >= SPA_VERSION_DEADLISTS) {
247512470SMatthew.Ahrens@Sun.COM 				dump_bpobj(&spa->spa_dsl_pool->dp_free_bpobj,
247612470SMatthew.Ahrens@Sun.COM 				    "Pool frees");
247712470SMatthew.Ahrens@Sun.COM 			}
2478789Sahrens 			dump_dtl(spa->spa_root_vdev, 0);
24799480SGeorge.Wilson@Sun.COM 		}
248010860SVictor.Latushkin@Sun.COM 		(void) dmu_objset_find(spa_name(spa), dump_one_dir,
248110860SVictor.Latushkin@Sun.COM 		    NULL, DS_FIND_SNAPSHOTS | DS_FIND_CHILDREN);
2482789Sahrens 	}
248310922SJeff.Bonwick@Sun.COM 	if (dump_opt['b'] || dump_opt['c'])
2484789Sahrens 		rc = dump_block_stats(spa);
2485789Sahrens 
2486789Sahrens 	if (dump_opt['s'])
2487789Sahrens 		show_pool_stats(spa);
2488789Sahrens 
248910685SGeorge.Wilson@Sun.COM 	if (dump_opt['h'])
249010685SGeorge.Wilson@Sun.COM 		dump_history(spa);
249110685SGeorge.Wilson@Sun.COM 
2492789Sahrens 	if (rc != 0)
2493789Sahrens 		exit(rc);
2494789Sahrens }
2495789Sahrens 
24961775Sbillm #define	ZDB_FLAG_CHECKSUM	0x0001
24971775Sbillm #define	ZDB_FLAG_DECOMPRESS	0x0002
24981775Sbillm #define	ZDB_FLAG_BSWAP		0x0004
24991775Sbillm #define	ZDB_FLAG_GBH		0x0008
25001775Sbillm #define	ZDB_FLAG_INDIRECT	0x0010
25011775Sbillm #define	ZDB_FLAG_PHYS		0x0020
25021775Sbillm #define	ZDB_FLAG_RAW		0x0040
25031775Sbillm #define	ZDB_FLAG_PRINT_BLKPTR	0x0080
25041775Sbillm 
25051775Sbillm int flagbits[256];
25061775Sbillm 
25071775Sbillm static void
zdb_print_blkptr(blkptr_t * bp,int flags)25081775Sbillm zdb_print_blkptr(blkptr_t *bp, int flags)
25091775Sbillm {
251010922SJeff.Bonwick@Sun.COM 	char blkbuf[BP_SPRINTF_LEN];
25111775Sbillm 
25121775Sbillm 	if (flags & ZDB_FLAG_BSWAP)
25131775Sbillm 		byteswap_uint64_array((void *)bp, sizeof (blkptr_t));
251410922SJeff.Bonwick@Sun.COM 
251510922SJeff.Bonwick@Sun.COM 	sprintf_blkptr(blkbuf, bp);
251610922SJeff.Bonwick@Sun.COM 	(void) printf("%s\n", blkbuf);
25171775Sbillm }
25181775Sbillm 
25191775Sbillm static void
zdb_dump_indirect(blkptr_t * bp,int nbps,int flags)25201775Sbillm zdb_dump_indirect(blkptr_t *bp, int nbps, int flags)
25211775Sbillm {
25221775Sbillm 	int i;
25231775Sbillm 
25241775Sbillm 	for (i = 0; i < nbps; i++)
25251775Sbillm 		zdb_print_blkptr(&bp[i], flags);
25261775Sbillm }
25271775Sbillm 
25281775Sbillm static void
zdb_dump_gbh(void * buf,int flags)25291775Sbillm zdb_dump_gbh(void *buf, int flags)
25301775Sbillm {
25311775Sbillm 	zdb_dump_indirect((blkptr_t *)buf, SPA_GBH_NBLKPTRS, flags);
25321775Sbillm }
25331775Sbillm 
25341775Sbillm static void
zdb_dump_block_raw(void * buf,uint64_t size,int flags)25351775Sbillm zdb_dump_block_raw(void *buf, uint64_t size, int flags)
25361775Sbillm {
25371775Sbillm 	if (flags & ZDB_FLAG_BSWAP)
25381775Sbillm 		byteswap_uint64_array(buf, size);
253910922SJeff.Bonwick@Sun.COM 	(void) write(1, buf, size);
25401775Sbillm }
25411775Sbillm 
25421775Sbillm static void
zdb_dump_block(char * label,void * buf,uint64_t size,int flags)25431775Sbillm zdb_dump_block(char *label, void *buf, uint64_t size, int flags)
25441775Sbillm {
25451775Sbillm 	uint64_t *d = (uint64_t *)buf;
25461775Sbillm 	int nwords = size / sizeof (uint64_t);
25471775Sbillm 	int do_bswap = !!(flags & ZDB_FLAG_BSWAP);
25481775Sbillm 	int i, j;
25491775Sbillm 	char *hdr, *c;
25501775Sbillm 
25511775Sbillm 
25521775Sbillm 	if (do_bswap)
25531775Sbillm 		hdr = " 7 6 5 4 3 2 1 0   f e d c b a 9 8";
25541775Sbillm 	else
25551775Sbillm 		hdr = " 0 1 2 3 4 5 6 7   8 9 a b c d e f";
25561775Sbillm 
25571775Sbillm 	(void) printf("\n%s\n%6s   %s  0123456789abcdef\n", label, "", hdr);
25581775Sbillm 
25591775Sbillm 	for (i = 0; i < nwords; i += 2) {
25601775Sbillm 		(void) printf("%06llx:  %016llx  %016llx  ",
25611775Sbillm 		    (u_longlong_t)(i * sizeof (uint64_t)),
25621775Sbillm 		    (u_longlong_t)(do_bswap ? BSWAP_64(d[i]) : d[i]),
25631775Sbillm 		    (u_longlong_t)(do_bswap ? BSWAP_64(d[i + 1]) : d[i + 1]));
25641775Sbillm 
25651775Sbillm 		c = (char *)&d[i];
25661775Sbillm 		for (j = 0; j < 2 * sizeof (uint64_t); j++)
25671775Sbillm 			(void) printf("%c", isprint(c[j]) ? c[j] : '.');
25681775Sbillm 		(void) printf("\n");
25691775Sbillm 	}
25701775Sbillm }
25711775Sbillm 
25721775Sbillm /*
25731775Sbillm  * There are two acceptable formats:
25741775Sbillm  *	leaf_name	  - For example: c1t0d0 or /tmp/ztest.0a
25751775Sbillm  *	child[.child]*    - For example: 0.1.1
25761775Sbillm  *
25771775Sbillm  * The second form can be used to specify arbitrary vdevs anywhere
25781775Sbillm  * in the heirarchy.  For example, in a pool with a mirror of
25791775Sbillm  * RAID-Zs, you can specify either RAID-Z vdev with 0.0 or 0.1 .
25801775Sbillm  */
25811775Sbillm static vdev_t *
zdb_vdev_lookup(vdev_t * vdev,char * path)25821775Sbillm zdb_vdev_lookup(vdev_t *vdev, char *path)
25831775Sbillm {
25841775Sbillm 	char *s, *p, *q;
25851775Sbillm 	int i;
25861775Sbillm 
25871775Sbillm 	if (vdev == NULL)
25881775Sbillm 		return (NULL);
25891775Sbillm 
25901775Sbillm 	/* First, assume the x.x.x.x format */
25911775Sbillm 	i = (int)strtoul(path, &s, 10);
25921775Sbillm 	if (s == path || (s && *s != '.' && *s != '\0'))
25931775Sbillm 		goto name;
25941775Sbillm 	if (i < 0 || i >= vdev->vdev_children)
25951775Sbillm 		return (NULL);
25961775Sbillm 
25971775Sbillm 	vdev = vdev->vdev_child[i];
25981775Sbillm 	if (*s == '\0')
25991775Sbillm 		return (vdev);
26001775Sbillm 	return (zdb_vdev_lookup(vdev, s+1));
26011775Sbillm 
26021775Sbillm name:
26031775Sbillm 	for (i = 0; i < vdev->vdev_children; i++) {
26041775Sbillm 		vdev_t *vc = vdev->vdev_child[i];
26051775Sbillm 
26061775Sbillm 		if (vc->vdev_path == NULL) {
26071775Sbillm 			vc = zdb_vdev_lookup(vc, path);
26081775Sbillm 			if (vc == NULL)
26091775Sbillm 				continue;
26101775Sbillm 			else
26111775Sbillm 				return (vc);
26121775Sbillm 		}
26131775Sbillm 
26141775Sbillm 		p = strrchr(vc->vdev_path, '/');
26151775Sbillm 		p = p ? p + 1 : vc->vdev_path;
26161775Sbillm 		q = &vc->vdev_path[strlen(vc->vdev_path) - 2];
26171775Sbillm 
26181775Sbillm 		if (strcmp(vc->vdev_path, path) == 0)
26191775Sbillm 			return (vc);
26201775Sbillm 		if (strcmp(p, path) == 0)
26211775Sbillm 			return (vc);
26221775Sbillm 		if (strcmp(q, "s0") == 0 && strncmp(p, path, q - p) == 0)
26231775Sbillm 			return (vc);
26241775Sbillm 	}
26251775Sbillm 
26261775Sbillm 	return (NULL);
26271775Sbillm }
26281775Sbillm 
26291775Sbillm /*
26301775Sbillm  * Read a block from a pool and print it out.  The syntax of the
26311775Sbillm  * block descriptor is:
26321775Sbillm  *
26331775Sbillm  *	pool:vdev_specifier:offset:size[:flags]
26341775Sbillm  *
26351775Sbillm  *	pool           - The name of the pool you wish to read from
26361775Sbillm  *	vdev_specifier - Which vdev (see comment for zdb_vdev_lookup)
26371775Sbillm  *	offset         - offset, in hex, in bytes
26381775Sbillm  *	size           - Amount of data to read, in hex, in bytes
26391775Sbillm  *	flags          - A string of characters specifying options
26401775Sbillm  *		 b: Decode a blkptr at given offset within block
26411775Sbillm  *		*c: Calculate and display checksums
264210922SJeff.Bonwick@Sun.COM  *		 d: Decompress data before dumping
26431775Sbillm  *		 e: Byteswap data before dumping
264410922SJeff.Bonwick@Sun.COM  *		 g: Display data as a gang block header
264510922SJeff.Bonwick@Sun.COM  *		 i: Display as an indirect block
26461775Sbillm  *		 p: Do I/O to physical offset
26471775Sbillm  *		 r: Dump raw data to stdout
26481775Sbillm  *
26491775Sbillm  *              * = not yet implemented
26501775Sbillm  */
26511775Sbillm static void
zdb_read_block(char * thing,spa_t * spa)265210860SVictor.Latushkin@Sun.COM zdb_read_block(char *thing, spa_t *spa)
26531775Sbillm {
265410922SJeff.Bonwick@Sun.COM 	blkptr_t blk, *bp = &blk;
265510922SJeff.Bonwick@Sun.COM 	dva_t *dva = bp->blk_dva;
26561775Sbillm 	int flags = 0;
265710922SJeff.Bonwick@Sun.COM 	uint64_t offset = 0, size = 0, psize = 0, lsize = 0, blkptr_offset = 0;
26581775Sbillm 	zio_t *zio;
26591775Sbillm 	vdev_t *vd;
266010922SJeff.Bonwick@Sun.COM 	void *pbuf, *lbuf, *buf;
266110860SVictor.Latushkin@Sun.COM 	char *s, *p, *dup, *vdev, *flagstr;
266210922SJeff.Bonwick@Sun.COM 	int i, error;
26631775Sbillm 
26641775Sbillm 	dup = strdup(thing);
26651775Sbillm 	s = strtok(dup, ":");
26661775Sbillm 	vdev = s ? s : "";
26671775Sbillm 	s = strtok(NULL, ":");
26681775Sbillm 	offset = strtoull(s ? s : "", NULL, 16);
26691775Sbillm 	s = strtok(NULL, ":");
26701775Sbillm 	size = strtoull(s ? s : "", NULL, 16);
26711775Sbillm 	s = strtok(NULL, ":");
26721775Sbillm 	flagstr = s ? s : "";
26731775Sbillm 
26741775Sbillm 	s = NULL;
26751775Sbillm 	if (size == 0)
26761775Sbillm 		s = "size must not be zero";
26771775Sbillm 	if (!IS_P2ALIGNED(size, DEV_BSIZE))
26781775Sbillm 		s = "size must be a multiple of sector size";
26791775Sbillm 	if (!IS_P2ALIGNED(offset, DEV_BSIZE))
26801775Sbillm 		s = "offset must be a multiple of sector size";
26811775Sbillm 	if (s) {
26821775Sbillm 		(void) printf("Invalid block specifier: %s  - %s\n", thing, s);
26831775Sbillm 		free(dup);
26841775Sbillm 		return;
26851775Sbillm 	}
26861775Sbillm 
26871775Sbillm 	for (s = strtok(flagstr, ":"); s; s = strtok(NULL, ":")) {
26881775Sbillm 		for (i = 0; flagstr[i]; i++) {
26892856Snd150628 			int bit = flagbits[(uchar_t)flagstr[i]];
26901775Sbillm 
26911775Sbillm 			if (bit == 0) {
26921775Sbillm 				(void) printf("***Invalid flag: %c\n",
26931775Sbillm 				    flagstr[i]);
26941775Sbillm 				continue;
26951775Sbillm 			}
26961775Sbillm 			flags |= bit;
26971775Sbillm 
26981775Sbillm 			/* If it's not something with an argument, keep going */
269910922SJeff.Bonwick@Sun.COM 			if ((bit & (ZDB_FLAG_CHECKSUM |
27001775Sbillm 			    ZDB_FLAG_PRINT_BLKPTR)) == 0)
27011775Sbillm 				continue;
27021775Sbillm 
27031775Sbillm 			p = &flagstr[i + 1];
27041775Sbillm 			if (bit == ZDB_FLAG_PRINT_BLKPTR)
27051775Sbillm 				blkptr_offset = strtoull(p, &p, 16);
27061775Sbillm 			if (*p != ':' && *p != '\0') {
27071775Sbillm 				(void) printf("***Invalid flag arg: '%s'\n", s);
27081775Sbillm 				free(dup);
27091775Sbillm 				return;
27101775Sbillm 			}
27111775Sbillm 		}
27121775Sbillm 	}
27131775Sbillm 
27141775Sbillm 	vd = zdb_vdev_lookup(spa->spa_root_vdev, vdev);
27151775Sbillm 	if (vd == NULL) {
27161775Sbillm 		(void) printf("***Invalid vdev: %s\n", vdev);
27171775Sbillm 		free(dup);
27181775Sbillm 		return;
27191775Sbillm 	} else {
27201775Sbillm 		if (vd->vdev_path)
272110922SJeff.Bonwick@Sun.COM 			(void) fprintf(stderr, "Found vdev: %s\n",
272210922SJeff.Bonwick@Sun.COM 			    vd->vdev_path);
27231775Sbillm 		else
272410922SJeff.Bonwick@Sun.COM 			(void) fprintf(stderr, "Found vdev type: %s\n",
27251775Sbillm 			    vd->vdev_ops->vdev_op_type);
27261775Sbillm 	}
27271775Sbillm 
272810922SJeff.Bonwick@Sun.COM 	psize = size;
272910922SJeff.Bonwick@Sun.COM 	lsize = size;
273010922SJeff.Bonwick@Sun.COM 
273110922SJeff.Bonwick@Sun.COM 	pbuf = umem_alloc(SPA_MAXBLOCKSIZE, UMEM_NOFAIL);
273210922SJeff.Bonwick@Sun.COM 	lbuf = umem_alloc(SPA_MAXBLOCKSIZE, UMEM_NOFAIL);
273310922SJeff.Bonwick@Sun.COM 
273410922SJeff.Bonwick@Sun.COM 	BP_ZERO(bp);
273510922SJeff.Bonwick@Sun.COM 
273610922SJeff.Bonwick@Sun.COM 	DVA_SET_VDEV(&dva[0], vd->vdev_id);
273710922SJeff.Bonwick@Sun.COM 	DVA_SET_OFFSET(&dva[0], offset);
273810922SJeff.Bonwick@Sun.COM 	DVA_SET_GANG(&dva[0], !!(flags & ZDB_FLAG_GBH));
273910922SJeff.Bonwick@Sun.COM 	DVA_SET_ASIZE(&dva[0], vdev_psize_to_asize(vd, psize));
274010922SJeff.Bonwick@Sun.COM 
274110922SJeff.Bonwick@Sun.COM 	BP_SET_BIRTH(bp, TXG_INITIAL, TXG_INITIAL);
274210922SJeff.Bonwick@Sun.COM 
274310922SJeff.Bonwick@Sun.COM 	BP_SET_LSIZE(bp, lsize);
274410922SJeff.Bonwick@Sun.COM 	BP_SET_PSIZE(bp, psize);
274510922SJeff.Bonwick@Sun.COM 	BP_SET_COMPRESS(bp, ZIO_COMPRESS_OFF);
274610922SJeff.Bonwick@Sun.COM 	BP_SET_CHECKSUM(bp, ZIO_CHECKSUM_OFF);
274710922SJeff.Bonwick@Sun.COM 	BP_SET_TYPE(bp, DMU_OT_NONE);
274810922SJeff.Bonwick@Sun.COM 	BP_SET_LEVEL(bp, 0);
274910922SJeff.Bonwick@Sun.COM 	BP_SET_DEDUP(bp, 0);
275010922SJeff.Bonwick@Sun.COM 	BP_SET_BYTEORDER(bp, ZFS_HOST_BYTEORDER);
27511775Sbillm 
27527754SJeff.Bonwick@Sun.COM 	spa_config_enter(spa, SCL_STATE, FTAG, RW_READER);
27531775Sbillm 	zio = zio_root(spa, NULL, NULL, 0);
275410922SJeff.Bonwick@Sun.COM 
275510922SJeff.Bonwick@Sun.COM 	if (vd == vd->vdev_top) {
275610922SJeff.Bonwick@Sun.COM 		/*
275710922SJeff.Bonwick@Sun.COM 		 * Treat this as a normal block read.
275810922SJeff.Bonwick@Sun.COM 		 */
275910922SJeff.Bonwick@Sun.COM 		zio_nowait(zio_read(zio, spa, bp, pbuf, psize, NULL, NULL,
276010922SJeff.Bonwick@Sun.COM 		    ZIO_PRIORITY_SYNC_READ,
276110922SJeff.Bonwick@Sun.COM 		    ZIO_FLAG_CANFAIL | ZIO_FLAG_RAW, NULL));
276210922SJeff.Bonwick@Sun.COM 	} else {
276310922SJeff.Bonwick@Sun.COM 		/*
276410922SJeff.Bonwick@Sun.COM 		 * Treat this as a vdev child I/O.
276510922SJeff.Bonwick@Sun.COM 		 */
276610922SJeff.Bonwick@Sun.COM 		zio_nowait(zio_vdev_child_io(zio, bp, vd, offset, pbuf, psize,
276710922SJeff.Bonwick@Sun.COM 		    ZIO_TYPE_READ, ZIO_PRIORITY_SYNC_READ,
276810922SJeff.Bonwick@Sun.COM 		    ZIO_FLAG_DONT_CACHE | ZIO_FLAG_DONT_QUEUE |
276910922SJeff.Bonwick@Sun.COM 		    ZIO_FLAG_DONT_PROPAGATE | ZIO_FLAG_DONT_RETRY |
277010922SJeff.Bonwick@Sun.COM 		    ZIO_FLAG_CANFAIL | ZIO_FLAG_RAW, NULL, NULL));
277110922SJeff.Bonwick@Sun.COM 	}
277210922SJeff.Bonwick@Sun.COM 
27731775Sbillm 	error = zio_wait(zio);
27747754SJeff.Bonwick@Sun.COM 	spa_config_exit(spa, SCL_STATE, FTAG);
27751775Sbillm 
27761775Sbillm 	if (error) {
27771775Sbillm 		(void) printf("Read of %s failed, error: %d\n", thing, error);
27781775Sbillm 		goto out;
27791775Sbillm 	}
27801775Sbillm 
278110922SJeff.Bonwick@Sun.COM 	if (flags & ZDB_FLAG_DECOMPRESS) {
278210922SJeff.Bonwick@Sun.COM 		/*
278310922SJeff.Bonwick@Sun.COM 		 * We don't know how the data was compressed, so just try
278410922SJeff.Bonwick@Sun.COM 		 * every decompress function at every inflated blocksize.
278510922SJeff.Bonwick@Sun.COM 		 */
278610922SJeff.Bonwick@Sun.COM 		enum zio_compress c;
278710922SJeff.Bonwick@Sun.COM 		void *pbuf2 = umem_alloc(SPA_MAXBLOCKSIZE, UMEM_NOFAIL);
278810922SJeff.Bonwick@Sun.COM 		void *lbuf2 = umem_alloc(SPA_MAXBLOCKSIZE, UMEM_NOFAIL);
278910922SJeff.Bonwick@Sun.COM 
279010922SJeff.Bonwick@Sun.COM 		bcopy(pbuf, pbuf2, psize);
279110922SJeff.Bonwick@Sun.COM 
279210922SJeff.Bonwick@Sun.COM 		VERIFY(random_get_pseudo_bytes((uint8_t *)pbuf + psize,
279310922SJeff.Bonwick@Sun.COM 		    SPA_MAXBLOCKSIZE - psize) == 0);
279410922SJeff.Bonwick@Sun.COM 
279510922SJeff.Bonwick@Sun.COM 		VERIFY(random_get_pseudo_bytes((uint8_t *)pbuf2 + psize,
279610922SJeff.Bonwick@Sun.COM 		    SPA_MAXBLOCKSIZE - psize) == 0);
279710922SJeff.Bonwick@Sun.COM 
279810922SJeff.Bonwick@Sun.COM 		for (lsize = SPA_MAXBLOCKSIZE; lsize > psize;
279910922SJeff.Bonwick@Sun.COM 		    lsize -= SPA_MINBLOCKSIZE) {
280010922SJeff.Bonwick@Sun.COM 			for (c = 0; c < ZIO_COMPRESS_FUNCTIONS; c++) {
280110922SJeff.Bonwick@Sun.COM 				if (zio_decompress_data(c, pbuf, lbuf,
280210922SJeff.Bonwick@Sun.COM 				    psize, lsize) == 0 &&
280310922SJeff.Bonwick@Sun.COM 				    zio_decompress_data(c, pbuf2, lbuf2,
280410922SJeff.Bonwick@Sun.COM 				    psize, lsize) == 0 &&
280510922SJeff.Bonwick@Sun.COM 				    bcmp(lbuf, lbuf2, lsize) == 0)
280610922SJeff.Bonwick@Sun.COM 					break;
280710922SJeff.Bonwick@Sun.COM 			}
280810922SJeff.Bonwick@Sun.COM 			if (c != ZIO_COMPRESS_FUNCTIONS)
280910922SJeff.Bonwick@Sun.COM 				break;
281010922SJeff.Bonwick@Sun.COM 			lsize -= SPA_MINBLOCKSIZE;
281110922SJeff.Bonwick@Sun.COM 		}
281210922SJeff.Bonwick@Sun.COM 
281310922SJeff.Bonwick@Sun.COM 		umem_free(pbuf2, SPA_MAXBLOCKSIZE);
281410922SJeff.Bonwick@Sun.COM 		umem_free(lbuf2, SPA_MAXBLOCKSIZE);
281510922SJeff.Bonwick@Sun.COM 
281610922SJeff.Bonwick@Sun.COM 		if (lsize <= psize) {
281710922SJeff.Bonwick@Sun.COM 			(void) printf("Decompress of %s failed\n", thing);
281810922SJeff.Bonwick@Sun.COM 			goto out;
281910922SJeff.Bonwick@Sun.COM 		}
282010922SJeff.Bonwick@Sun.COM 		buf = lbuf;
282110922SJeff.Bonwick@Sun.COM 		size = lsize;
282210922SJeff.Bonwick@Sun.COM 	} else {
282310922SJeff.Bonwick@Sun.COM 		buf = pbuf;
282410922SJeff.Bonwick@Sun.COM 		size = psize;
282510922SJeff.Bonwick@Sun.COM 	}
282610922SJeff.Bonwick@Sun.COM 
28271775Sbillm 	if (flags & ZDB_FLAG_PRINT_BLKPTR)
28281775Sbillm 		zdb_print_blkptr((blkptr_t *)(void *)
28291775Sbillm 		    ((uintptr_t)buf + (uintptr_t)blkptr_offset), flags);
28301775Sbillm 	else if (flags & ZDB_FLAG_RAW)
28311775Sbillm 		zdb_dump_block_raw(buf, size, flags);
28321775Sbillm 	else if (flags & ZDB_FLAG_INDIRECT)
28331775Sbillm 		zdb_dump_indirect((blkptr_t *)buf, size / sizeof (blkptr_t),
28341775Sbillm 		    flags);
28351775Sbillm 	else if (flags & ZDB_FLAG_GBH)
28361775Sbillm 		zdb_dump_gbh(buf, flags);
28371775Sbillm 	else
28381775Sbillm 		zdb_dump_block(thing, buf, size, flags);
28391775Sbillm 
28401775Sbillm out:
284110922SJeff.Bonwick@Sun.COM 	umem_free(pbuf, SPA_MAXBLOCKSIZE);
284210922SJeff.Bonwick@Sun.COM 	umem_free(lbuf, SPA_MAXBLOCKSIZE);
28431775Sbillm 	free(dup);
28441775Sbillm }
28451775Sbillm 
28464627Sck153898 static boolean_t
pool_match(nvlist_t * cfg,char * tgt)284710858SVictor.Latushkin@Sun.COM pool_match(nvlist_t *cfg, char *tgt)
28484627Sck153898 {
284910858SVictor.Latushkin@Sun.COM 	uint64_t v, guid = strtoull(tgt, NULL, 0);
28504627Sck153898 	char *s;
28514627Sck153898 
285210858SVictor.Latushkin@Sun.COM 	if (guid != 0) {
285310858SVictor.Latushkin@Sun.COM 		if (nvlist_lookup_uint64(cfg, ZPOOL_CONFIG_POOL_GUID, &v) == 0)
285410858SVictor.Latushkin@Sun.COM 			return (v == guid);
285510858SVictor.Latushkin@Sun.COM 	} else {
285610858SVictor.Latushkin@Sun.COM 		if (nvlist_lookup_string(cfg, ZPOOL_CONFIG_POOL_NAME, &s) == 0)
285710858SVictor.Latushkin@Sun.COM 			return (strcmp(s, tgt) == 0);
28584627Sck153898 	}
28594627Sck153898 	return (B_FALSE);
28604627Sck153898 }
28614627Sck153898 
286210858SVictor.Latushkin@Sun.COM static char *
find_zpool(char ** target,nvlist_t ** configp,int dirc,char ** dirv)286310858SVictor.Latushkin@Sun.COM find_zpool(char **target, nvlist_t **configp, int dirc, char **dirv)
28644627Sck153898 {
28654627Sck153898 	nvlist_t *pools;
28664627Sck153898 	nvlist_t *match = NULL;
286710858SVictor.Latushkin@Sun.COM 	char *name = NULL;
286810858SVictor.Latushkin@Sun.COM 	char *sepp = NULL;
286910858SVictor.Latushkin@Sun.COM 	char sep;
287010858SVictor.Latushkin@Sun.COM 	int count = 0;
287111497SMark.Musante@Sun.COM 	importargs_t args = { 0 };
287211497SMark.Musante@Sun.COM 
287311497SMark.Musante@Sun.COM 	args.paths = dirc;
287411497SMark.Musante@Sun.COM 	args.path = dirv;
287511497SMark.Musante@Sun.COM 	args.can_be_active = B_TRUE;
28764627Sck153898 
287710858SVictor.Latushkin@Sun.COM 	if ((sepp = strpbrk(*target, "/@")) != NULL) {
287810858SVictor.Latushkin@Sun.COM 		sep = *sepp;
287910858SVictor.Latushkin@Sun.COM 		*sepp = '\0';
288010858SVictor.Latushkin@Sun.COM 	}
288110858SVictor.Latushkin@Sun.COM 
288211497SMark.Musante@Sun.COM 	pools = zpool_search_import(g_zfs, &args);
28834627Sck153898 
28844627Sck153898 	if (pools != NULL) {
28854627Sck153898 		nvpair_t *elem = NULL;
28864627Sck153898 		while ((elem = nvlist_next_nvpair(pools, elem)) != NULL) {
28874627Sck153898 			verify(nvpair_value_nvlist(elem, configp) == 0);
288810858SVictor.Latushkin@Sun.COM 			if (pool_match(*configp, *target)) {
288910858SVictor.Latushkin@Sun.COM 				count++;
28904627Sck153898 				if (match != NULL) {
289110858SVictor.Latushkin@Sun.COM 					/* print previously found config */
289210858SVictor.Latushkin@Sun.COM 					if (name != NULL) {
289310858SVictor.Latushkin@Sun.COM 						(void) printf("%s\n", name);
289410858SVictor.Latushkin@Sun.COM 						dump_nvlist(match, 8);
289510858SVictor.Latushkin@Sun.COM 						name = NULL;
289610858SVictor.Latushkin@Sun.COM 					}
289710858SVictor.Latushkin@Sun.COM 					(void) printf("%s\n",
289810858SVictor.Latushkin@Sun.COM 					    nvpair_name(elem));
289910858SVictor.Latushkin@Sun.COM 					dump_nvlist(*configp, 8);
29004627Sck153898 				} else {
29014627Sck153898 					match = *configp;
290210858SVictor.Latushkin@Sun.COM 					name = nvpair_name(elem);
29034627Sck153898 				}
29044627Sck153898 			}
29054627Sck153898 		}
29064627Sck153898 	}
290710858SVictor.Latushkin@Sun.COM 	if (count > 1)
290810858SVictor.Latushkin@Sun.COM 		(void) fatal("\tMatched %d pools - use pool GUID "
290910858SVictor.Latushkin@Sun.COM 		    "instead of pool name or \n"
291010858SVictor.Latushkin@Sun.COM 		    "\tpool name part of a dataset name to select pool", count);
29114627Sck153898 
291210858SVictor.Latushkin@Sun.COM 	if (sepp)
291310858SVictor.Latushkin@Sun.COM 		*sepp = sep;
291410858SVictor.Latushkin@Sun.COM 	/*
291510858SVictor.Latushkin@Sun.COM 	 * If pool GUID was specified for pool id, replace it with pool name
291610858SVictor.Latushkin@Sun.COM 	 */
291710858SVictor.Latushkin@Sun.COM 	if (name && (strstr(*target, name) != *target)) {
291810858SVictor.Latushkin@Sun.COM 		int sz = 1 + strlen(name) + ((sepp) ? strlen(sepp) : 0);
29194627Sck153898 
292010858SVictor.Latushkin@Sun.COM 		*target = umem_alloc(sz, UMEM_NOFAIL);
292110858SVictor.Latushkin@Sun.COM 		(void) snprintf(*target, sz, "%s%s", name, sepp ? sepp : "");
292210858SVictor.Latushkin@Sun.COM 	}
292310858SVictor.Latushkin@Sun.COM 
292410858SVictor.Latushkin@Sun.COM 	*configp = name ? match : NULL;
292510858SVictor.Latushkin@Sun.COM 
292610858SVictor.Latushkin@Sun.COM 	return (name);
29274627Sck153898 }
29284627Sck153898 
2929789Sahrens int
main(int argc,char ** argv)2930789Sahrens main(int argc, char **argv)
2931789Sahrens {
2932789Sahrens 	int i, c;
2933789Sahrens 	struct rlimit rl = { 1024, 1024 };
293410858SVictor.Latushkin@Sun.COM 	spa_t *spa = NULL;
2935789Sahrens 	objset_t *os = NULL;
2936789Sahrens 	int dump_all = 1;
2937789Sahrens 	int verbose = 0;
293811727SVictor.Latushkin@Sun.COM 	int error = 0;
293910858SVictor.Latushkin@Sun.COM 	char **searchdirs = NULL;
294010858SVictor.Latushkin@Sun.COM 	int nsearch = 0;
294110858SVictor.Latushkin@Sun.COM 	char *target;
294210921STim.Haley@Sun.COM 	nvlist_t *policy = NULL;
294310921STim.Haley@Sun.COM 	uint64_t max_txg = UINT64_MAX;
294411727SVictor.Latushkin@Sun.COM 	int rewind = ZPOOL_NEVER_REWIND;
2945789Sahrens 
2946789Sahrens 	(void) setrlimit(RLIMIT_NOFILE, &rl);
29471914Scasper 	(void) enable_extended_FILE_stdio(-1, -1);
2948789Sahrens 
2949789Sahrens 	dprintf_setup(&argc, argv);
2950789Sahrens 
295112296SLin.Ling@Sun.COM 	while ((c = getopt(argc, argv, "bcdhilmsuCDRSAFLXevp:t:U:P")) != -1) {
2952789Sahrens 		switch (c) {
2953789Sahrens 		case 'b':
2954789Sahrens 		case 'c':
295510922SJeff.Bonwick@Sun.COM 		case 'd':
295610922SJeff.Bonwick@Sun.COM 		case 'h':
295710922SJeff.Bonwick@Sun.COM 		case 'i':
295810922SJeff.Bonwick@Sun.COM 		case 'l':
29599480SGeorge.Wilson@Sun.COM 		case 'm':
2960789Sahrens 		case 's':
296110922SJeff.Bonwick@Sun.COM 		case 'u':
2962789Sahrens 		case 'C':
296310922SJeff.Bonwick@Sun.COM 		case 'D':
29641775Sbillm 		case 'R':
296510922SJeff.Bonwick@Sun.COM 		case 'S':
2966789Sahrens 			dump_opt[c]++;
2967789Sahrens 			dump_all = 0;
2968789Sahrens 			break;
296911726SVictor.Latushkin@Sun.COM 		case 'A':
297011727SVictor.Latushkin@Sun.COM 		case 'F':
29718121SVictor.Latushkin@Sun.COM 		case 'L':
297211727SVictor.Latushkin@Sun.COM 		case 'X':
297310858SVictor.Latushkin@Sun.COM 		case 'e':
297412296SLin.Ling@Sun.COM 		case 'P':
29758121SVictor.Latushkin@Sun.COM 			dump_opt[c]++;
29768121SVictor.Latushkin@Sun.COM 			break;
2977789Sahrens 		case 'v':
2978789Sahrens 			verbose++;
2979789Sahrens 			break;
29804627Sck153898 		case 'p':
298110858SVictor.Latushkin@Sun.COM 			if (searchdirs == NULL) {
298210858SVictor.Latushkin@Sun.COM 				searchdirs = umem_alloc(sizeof (char *),
298310858SVictor.Latushkin@Sun.COM 				    UMEM_NOFAIL);
298410858SVictor.Latushkin@Sun.COM 			} else {
298510858SVictor.Latushkin@Sun.COM 				char **tmp = umem_alloc((nsearch + 1) *
298610858SVictor.Latushkin@Sun.COM 				    sizeof (char *), UMEM_NOFAIL);
298710858SVictor.Latushkin@Sun.COM 				bcopy(searchdirs, tmp, nsearch *
298810858SVictor.Latushkin@Sun.COM 				    sizeof (char *));
298910858SVictor.Latushkin@Sun.COM 				umem_free(searchdirs,
299010858SVictor.Latushkin@Sun.COM 				    nsearch * sizeof (char *));
299110858SVictor.Latushkin@Sun.COM 				searchdirs = tmp;
299210858SVictor.Latushkin@Sun.COM 			}
299310858SVictor.Latushkin@Sun.COM 			searchdirs[nsearch++] = optarg;
29944627Sck153898 			break;
29958188SVictor.Latushkin@Sun.COM 		case 't':
299610921STim.Haley@Sun.COM 			max_txg = strtoull(optarg, NULL, 0);
299710921STim.Haley@Sun.COM 			if (max_txg < TXG_INITIAL) {
29988188SVictor.Latushkin@Sun.COM 				(void) fprintf(stderr, "incorrect txg "
29998188SVictor.Latushkin@Sun.COM 				    "specified: %s\n", optarg);
30008188SVictor.Latushkin@Sun.COM 				usage();
30018188SVictor.Latushkin@Sun.COM 			}
30028188SVictor.Latushkin@Sun.COM 			break;
300310922SJeff.Bonwick@Sun.COM 		case 'U':
300410922SJeff.Bonwick@Sun.COM 			spa_config_path = optarg;
300510922SJeff.Bonwick@Sun.COM 			break;
3006789Sahrens 		default:
3007789Sahrens 			usage();
3008789Sahrens 			break;
3009789Sahrens 		}
3010789Sahrens 	}
3011789Sahrens 
301210858SVictor.Latushkin@Sun.COM 	if (!dump_opt['e'] && searchdirs != NULL) {
30137837SMatthew.Ahrens@Sun.COM 		(void) fprintf(stderr, "-p option requires use of -e\n");
30147837SMatthew.Ahrens@Sun.COM 		usage();
30157837SMatthew.Ahrens@Sun.COM 	}
30164627Sck153898 
3017789Sahrens 	kernel_init(FREAD);
30184627Sck153898 	g_zfs = libzfs_init();
30194787Sahrens 	ASSERT(g_zfs != NULL);
3020789Sahrens 
302110922SJeff.Bonwick@Sun.COM 	if (dump_all)
302210922SJeff.Bonwick@Sun.COM 		verbose = MAX(verbose, 1);
302310922SJeff.Bonwick@Sun.COM 
3024789Sahrens 	for (c = 0; c < 256; c++) {
302512296SLin.Ling@Sun.COM 		if (dump_all && !strchr("elAFLRSXP", c))
3026789Sahrens 			dump_opt[c] = 1;
3027789Sahrens 		if (dump_opt[c])
3028789Sahrens 			dump_opt[c] += verbose;
3029789Sahrens 	}
3030789Sahrens 
303111726SVictor.Latushkin@Sun.COM 	aok = (dump_opt['A'] == 1) || (dump_opt['A'] > 2);
303211726SVictor.Latushkin@Sun.COM 	zfs_recover = (dump_opt['A'] > 1);
303311726SVictor.Latushkin@Sun.COM 
3034789Sahrens 	argc -= optind;
3035789Sahrens 	argv += optind;
3036789Sahrens 
303710860SVictor.Latushkin@Sun.COM 	if (argc < 2 && dump_opt['R'])
303810860SVictor.Latushkin@Sun.COM 		usage();
3039789Sahrens 	if (argc < 1) {
304010858SVictor.Latushkin@Sun.COM 		if (!dump_opt['e'] && dump_opt['C']) {
30416957Sck153898 			dump_cachefile(spa_config_path);
3042789Sahrens 			return (0);
3043789Sahrens 		}
3044789Sahrens 		usage();
3045789Sahrens 	}
3046789Sahrens 
3047789Sahrens 	if (dump_opt['l']) {
3048789Sahrens 		dump_label(argv[0]);
3049789Sahrens 		return (0);
3050789Sahrens 	}
3051789Sahrens 
305211727SVictor.Latushkin@Sun.COM 	if (dump_opt['X'] || dump_opt['F'])
305311727SVictor.Latushkin@Sun.COM 		rewind = ZPOOL_DO_REWIND |
305411727SVictor.Latushkin@Sun.COM 		    (dump_opt['X'] ? ZPOOL_EXTREME_REWIND : 0);
305511727SVictor.Latushkin@Sun.COM 
305611727SVictor.Latushkin@Sun.COM 	if (nvlist_alloc(&policy, NV_UNIQUE_NAME_TYPE, 0) != 0 ||
305711727SVictor.Latushkin@Sun.COM 	    nvlist_add_uint64(policy, ZPOOL_REWIND_REQUEST_TXG, max_txg) != 0 ||
305811727SVictor.Latushkin@Sun.COM 	    nvlist_add_uint32(policy, ZPOOL_REWIND_REQUEST, rewind) != 0)
305911727SVictor.Latushkin@Sun.COM 		fatal("internal error: %s", strerror(ENOMEM));
306011727SVictor.Latushkin@Sun.COM 
30616643Seschrock 	error = 0;
306210858SVictor.Latushkin@Sun.COM 	target = argv[0];
30634627Sck153898 
306410858SVictor.Latushkin@Sun.COM 	if (dump_opt['e']) {
306510858SVictor.Latushkin@Sun.COM 		nvlist_t *cfg = NULL;
306610858SVictor.Latushkin@Sun.COM 		char *name = find_zpool(&target, &cfg, nsearch, searchdirs);
30675094Slling 
306810858SVictor.Latushkin@Sun.COM 		error = ENOENT;
306910858SVictor.Latushkin@Sun.COM 		if (name) {
307010860SVictor.Latushkin@Sun.COM 			if (dump_opt['C'] > 1) {
307110860SVictor.Latushkin@Sun.COM 				(void) printf("\nConfiguration for import:\n");
307210860SVictor.Latushkin@Sun.COM 				dump_nvlist(cfg, 8);
307310860SVictor.Latushkin@Sun.COM 			}
307411727SVictor.Latushkin@Sun.COM 			if (nvlist_add_nvlist(cfg,
307510921STim.Haley@Sun.COM 			    ZPOOL_REWIND_POLICY, policy) != 0) {
307610921STim.Haley@Sun.COM 				fatal("can't open '%s': %s",
307710921STim.Haley@Sun.COM 				    target, strerror(ENOMEM));
307810921STim.Haley@Sun.COM 			}
3079*12949SGeorge.Wilson@Sun.COM 			if ((error = spa_import(name, cfg, NULL,
3080*12949SGeorge.Wilson@Sun.COM 			    ZFS_IMPORT_MISSING_LOG)) != 0) {
3081*12949SGeorge.Wilson@Sun.COM 				error = spa_import(name, cfg, NULL,
3082*12949SGeorge.Wilson@Sun.COM 				    ZFS_IMPORT_VERBATIM);
3083*12949SGeorge.Wilson@Sun.COM 			}
30844627Sck153898 		}
30856643Seschrock 	}
30866643Seschrock 
30876643Seschrock 	if (error == 0) {
308810860SVictor.Latushkin@Sun.COM 		if (strpbrk(target, "/@") == NULL || dump_opt['R']) {
308911146SGeorge.Wilson@Sun.COM 			error = spa_open_rewind(target, &spa, FTAG, policy,
309011146SGeorge.Wilson@Sun.COM 			    NULL);
309110685SGeorge.Wilson@Sun.COM 			if (error) {
309210685SGeorge.Wilson@Sun.COM 				/*
309310685SGeorge.Wilson@Sun.COM 				 * If we're missing the log device then
309410685SGeorge.Wilson@Sun.COM 				 * try opening the pool after clearing the
309510685SGeorge.Wilson@Sun.COM 				 * log state.
309610685SGeorge.Wilson@Sun.COM 				 */
309710685SGeorge.Wilson@Sun.COM 				mutex_enter(&spa_namespace_lock);
309810858SVictor.Latushkin@Sun.COM 				if ((spa = spa_lookup(target)) != NULL &&
309910685SGeorge.Wilson@Sun.COM 				    spa->spa_log_state == SPA_LOG_MISSING) {
310010685SGeorge.Wilson@Sun.COM 					spa->spa_log_state = SPA_LOG_CLEAR;
310110685SGeorge.Wilson@Sun.COM 					error = 0;
310210685SGeorge.Wilson@Sun.COM 				}
310310685SGeorge.Wilson@Sun.COM 				mutex_exit(&spa_namespace_lock);
310410685SGeorge.Wilson@Sun.COM 
310511146SGeorge.Wilson@Sun.COM 				if (!error) {
310611146SGeorge.Wilson@Sun.COM 					error = spa_open_rewind(target, &spa,
310711146SGeorge.Wilson@Sun.COM 					    FTAG, policy, NULL);
310811146SGeorge.Wilson@Sun.COM 				}
310910685SGeorge.Wilson@Sun.COM 			}
311010860SVictor.Latushkin@Sun.COM 		} else {
311110860SVictor.Latushkin@Sun.COM 			error = dmu_objset_own(target, DMU_OST_ANY,
311210860SVictor.Latushkin@Sun.COM 			    B_TRUE, FTAG, &os);
31136643Seschrock 		}
3114789Sahrens 	}
311511146SGeorge.Wilson@Sun.COM 	nvlist_free(policy);
311611146SGeorge.Wilson@Sun.COM 
3117789Sahrens 	if (error)
311810858SVictor.Latushkin@Sun.COM 		fatal("can't open '%s': %s", target, strerror(error));
3119789Sahrens 
3120789Sahrens 	argv++;
312110860SVictor.Latushkin@Sun.COM 	argc--;
312210860SVictor.Latushkin@Sun.COM 	if (!dump_opt['R']) {
312310860SVictor.Latushkin@Sun.COM 		if (argc > 0) {
312410860SVictor.Latushkin@Sun.COM 			zopt_objects = argc;
312510860SVictor.Latushkin@Sun.COM 			zopt_object = calloc(zopt_objects, sizeof (uint64_t));
312610860SVictor.Latushkin@Sun.COM 			for (i = 0; i < zopt_objects; i++) {
312710860SVictor.Latushkin@Sun.COM 				errno = 0;
312810860SVictor.Latushkin@Sun.COM 				zopt_object[i] = strtoull(argv[i], NULL, 0);
312910860SVictor.Latushkin@Sun.COM 				if (zopt_object[i] == 0 && errno != 0)
313010861SVictor.Latushkin@Sun.COM 					fatal("bad number %s: %s",
313110860SVictor.Latushkin@Sun.COM 					    argv[i], strerror(errno));
313210860SVictor.Latushkin@Sun.COM 			}
3133789Sahrens 		}
313410860SVictor.Latushkin@Sun.COM 		(os != NULL) ? dump_dir(os) : dump_zpool(spa);
313510860SVictor.Latushkin@Sun.COM 	} else {
313610860SVictor.Latushkin@Sun.COM 		flagbits['b'] = ZDB_FLAG_PRINT_BLKPTR;
313710860SVictor.Latushkin@Sun.COM 		flagbits['c'] = ZDB_FLAG_CHECKSUM;
313810860SVictor.Latushkin@Sun.COM 		flagbits['d'] = ZDB_FLAG_DECOMPRESS;
313910860SVictor.Latushkin@Sun.COM 		flagbits['e'] = ZDB_FLAG_BSWAP;
314010860SVictor.Latushkin@Sun.COM 		flagbits['g'] = ZDB_FLAG_GBH;
314110860SVictor.Latushkin@Sun.COM 		flagbits['i'] = ZDB_FLAG_INDIRECT;
314210860SVictor.Latushkin@Sun.COM 		flagbits['p'] = ZDB_FLAG_PHYS;
314310860SVictor.Latushkin@Sun.COM 		flagbits['r'] = ZDB_FLAG_RAW;
314410860SVictor.Latushkin@Sun.COM 
314510860SVictor.Latushkin@Sun.COM 		for (i = 0; i < argc; i++)
314610860SVictor.Latushkin@Sun.COM 			zdb_read_block(argv[i], spa);
3147789Sahrens 	}
3148789Sahrens 
314910860SVictor.Latushkin@Sun.COM 	(os != NULL) ? dmu_objset_disown(os, FTAG) : spa_close(spa, FTAG);
3150789Sahrens 
31515959Smarks 	fuid_table_destroy();
315211935SMark.Shellenbaum@Sun.COM 	sa_loaded = B_FALSE;
31535959Smarks 
31544627Sck153898 	libzfs_fini(g_zfs);
3155789Sahrens 	kernel_fini();
3156789Sahrens 
3157789Sahrens 	return (0);
3158789Sahrens }
3159