1fa9e4066Sahrens /*
2fa9e4066Sahrens * CDDL HEADER START
3fa9e4066Sahrens *
4fa9e4066Sahrens * The contents of this file are subject to the terms of the
5ea8dc4b6Seschrock * Common Development and Distribution License (the "License").
6ea8dc4b6Seschrock * You may not use this file except in compliance with the License.
7fa9e4066Sahrens *
8fa9e4066Sahrens * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9fa9e4066Sahrens * or http://www.opensolaris.org/os/licensing.
10fa9e4066Sahrens * See the License for the specific language governing permissions
11fa9e4066Sahrens * and limitations under the License.
12fa9e4066Sahrens *
13fa9e4066Sahrens * When distributing Covered Code, include this CDDL HEADER in each
14fa9e4066Sahrens * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15fa9e4066Sahrens * If applicable, add the following below this CDDL HEADER, with the
16fa9e4066Sahrens * fields enclosed by brackets "[]" replaced with your own identifying
17fa9e4066Sahrens * information: Portions Copyright [yyyy] [name of copyright owner]
18fa9e4066Sahrens *
19fa9e4066Sahrens * CDDL HEADER END
20fa9e4066Sahrens */
21ad135b5dSChristopher Siden
22fa9e4066Sahrens /*
238f2529deSMark Shellenbaum * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
240cc589a4SMatthew Ahrens * Copyright (c) 2011, 2015 by Delphix. All rights reserved.
25fa9e4066Sahrens */
26fa9e4066Sahrens
27fa9e4066Sahrens #include <stdio.h>
28490d05b9SMatthew Ahrens #include <unistd.h>
29004388ebScasper #include <stdio_ext.h>
30fa9e4066Sahrens #include <stdlib.h>
3144cd46caSbillm #include <ctype.h>
32fa9e4066Sahrens #include <sys/zfs_context.h>
33fa9e4066Sahrens #include <sys/spa.h>
34fa9e4066Sahrens #include <sys/spa_impl.h>
35fa9e4066Sahrens #include <sys/dmu.h>
36fa9e4066Sahrens #include <sys/zap.h>
37fa9e4066Sahrens #include <sys/fs/zfs.h>
38fa9e4066Sahrens #include <sys/zfs_znode.h>
390a586ceaSMark Shellenbaum #include <sys/zfs_sa.h>
400a586ceaSMark Shellenbaum #include <sys/sa.h>
410a586ceaSMark Shellenbaum #include <sys/sa_impl.h>
42fa9e4066Sahrens #include <sys/vdev.h>
43fa9e4066Sahrens #include <sys/vdev_impl.h>
44fa9e4066Sahrens #include <sys/metaslab_impl.h>
45fa9e4066Sahrens #include <sys/dmu_objset.h>
46fa9e4066Sahrens #include <sys/dsl_dir.h>
47fa9e4066Sahrens #include <sys/dsl_dataset.h>
48fa9e4066Sahrens #include <sys/dsl_pool.h>
49fa9e4066Sahrens #include <sys/dbuf.h>
50fa9e4066Sahrens #include <sys/zil.h>
51fa9e4066Sahrens #include <sys/zil_impl.h>
52fa9e4066Sahrens #include <sys/stat.h>
53fa9e4066Sahrens #include <sys/resource.h>
54fa9e4066Sahrens #include <sys/dmu_traverse.h>
55fa9e4066Sahrens #include <sys/zio_checksum.h>
56fa9e4066Sahrens #include <sys/zio_compress.h>
57e0d35c44Smarks #include <sys/zfs_fuid.h>
5888b7b0f2SMatthew Ahrens #include <sys/arc.h>
59b24ab676SJeff Bonwick #include <sys/ddt.h>
60ad135b5dSChristopher Siden #include <sys/zfeature.h>
614445fffbSMatthew Ahrens #include <zfs_comutil.h>
62de6628f0Sck153898 #undef verify
63de6628f0Sck153898 #include <libzfs.h>
64fa9e4066Sahrens
656de8f417SVictor Latushkin #define ZDB_COMPRESS_NAME(idx) ((idx) < ZIO_COMPRESS_FUNCTIONS ? \
666de8f417SVictor Latushkin zio_compress_table[(idx)].ci_name : "UNKNOWN")
676de8f417SVictor Latushkin #define ZDB_CHECKSUM_NAME(idx) ((idx) < ZIO_CHECKSUM_FUNCTIONS ? \
686de8f417SVictor Latushkin zio_checksum_table[(idx)].ci_name : "UNKNOWN")
696de8f417SVictor Latushkin #define ZDB_OT_NAME(idx) ((idx) < DMU_OT_NUMTYPES ? \
70ad135b5dSChristopher Siden dmu_ot[(idx)].ot_name : DMU_OT_IS_VALID(idx) ? \
71ad135b5dSChristopher Siden dmu_ot_byteswap[DMU_OT_BYTESWAP(idx)].ob_name : "UNKNOWN")
72e690fb27SChristopher Siden #define ZDB_OT_TYPE(idx) ((idx) < DMU_OT_NUMTYPES ? (idx) : \
73e690fb27SChristopher Siden (((idx) == DMU_OTN_ZAP_DATA || (idx) == DMU_OTN_ZAP_METADATA) ? \
74e690fb27SChristopher Siden DMU_OT_ZAP_OTHER : DMU_OT_NUMTYPES))
756de8f417SVictor Latushkin
76feef89cfSVictor Latushkin #ifndef lint
77994617e7SMatthew Ahrens extern boolean_t zfs_recover;
78fef64290SMatthew Ahrens extern uint64_t zfs_arc_max, zfs_arc_meta_limit;
7949b2c7b9SMatthew Ahrens extern int zfs_vdev_async_read_max_active;
80feef89cfSVictor Latushkin #else
81994617e7SMatthew Ahrens boolean_t zfs_recover;
82fef64290SMatthew Ahrens uint64_t zfs_arc_max, zfs_arc_meta_limit;
8349b2c7b9SMatthew Ahrens int zfs_vdev_async_read_max_active;
84feef89cfSVictor Latushkin #endif
85feef89cfSVictor Latushkin
86fa9e4066Sahrens const char cmdname[] = "zdb";
87fa9e4066Sahrens uint8_t dump_opt[256];
88fa9e4066Sahrens
89fa9e4066Sahrens typedef void object_viewer_t(objset_t *, uint64_t, void *data, size_t size);
90fa9e4066Sahrens
91fa9e4066Sahrens extern void dump_intent_log(zilog_t *);
92fa9e4066Sahrens uint64_t *zopt_object = NULL;
93fa9e4066Sahrens int zopt_objects = 0;
94de6628f0Sck153898 libzfs_handle_t *g_zfs;
95fef64290SMatthew Ahrens uint64_t max_inflight = 1000;
96fa9e4066Sahrens
97bf263f2aSMatthew Ahrens static void snprintf_blkptr_compact(char *, size_t, const blkptr_t *);
98bf263f2aSMatthew Ahrens
99fa9e4066Sahrens /*
100fa9e4066Sahrens * These libumem hooks provide a reasonable set of defaults for the allocator's
101fa9e4066Sahrens * debugging facilities.
102fa9e4066Sahrens */
103fa9e4066Sahrens const char *
_umem_debug_init()104fa9e4066Sahrens _umem_debug_init()
105fa9e4066Sahrens {
106fa9e4066Sahrens return ("default,verbose"); /* $UMEM_DEBUG setting */
107fa9e4066Sahrens }
108fa9e4066Sahrens
109fa9e4066Sahrens const char *
_umem_logging_init(void)110fa9e4066Sahrens _umem_logging_init(void)
111fa9e4066Sahrens {
112fa9e4066Sahrens return ("fail,contents"); /* $UMEM_LOGGING setting */
113fa9e4066Sahrens }
114fa9e4066Sahrens
115fa9e4066Sahrens static void
usage(void)116fa9e4066Sahrens usage(void)
117fa9e4066Sahrens {
118fa9e4066Sahrens (void) fprintf(stderr,
11902b647c5SGeorge Wilson "Usage: %s [-CumMdibcsDvhLXFPA] [-t txg] [-e [-p path...]] "
12002b647c5SGeorge Wilson "[-U config] [-I inflight I/Os] [-x dumpdir] poolname [object...]\n"
12131d7e8faSGeorge Wilson " %s [-divPA] [-e -p path...] [-U config] dataset "
12231d7e8faSGeorge Wilson "[object...]\n"
12302b647c5SGeorge Wilson " %s -mM [-LXFPA] [-t txg] [-e [-p path...]] [-U config] "
12490e894e2SRichard Lowe "poolname [vdev [metaslab...]]\n"
12590e894e2SRichard Lowe " %s -R [-A] [-e [-p path...]] poolname "
12690e894e2SRichard Lowe "vdev:offset:size[:flags]\n"
12731d7e8faSGeorge Wilson " %s -S [-PA] [-e [-p path...]] [-U config] poolname\n"
12890e894e2SRichard Lowe " %s -l [-uA] device\n"
12990e894e2SRichard Lowe " %s -C [-A] [-U config]\n\n",
130bbfd46c4SJeff Bonwick cmdname, cmdname, cmdname, cmdname, cmdname, cmdname, cmdname);
131fa9e4066Sahrens
1323ad6c7f9SVictor Latushkin (void) fprintf(stderr, " Dataset name must include at least one "
1333ad6c7f9SVictor Latushkin "separator character '/' or '@'\n");
1343ad6c7f9SVictor Latushkin (void) fprintf(stderr, " If dataset name is specified, only that "
1353ad6c7f9SVictor Latushkin "dataset is dumped\n");
1363ad6c7f9SVictor Latushkin (void) fprintf(stderr, " If object numbers are specified, only "
1373ad6c7f9SVictor Latushkin "those objects are dumped\n\n");
1383ad6c7f9SVictor Latushkin (void) fprintf(stderr, " Options to control amount of output:\n");
139fa9e4066Sahrens (void) fprintf(stderr, " -u uberblock\n");
1403ad6c7f9SVictor Latushkin (void) fprintf(stderr, " -d dataset(s)\n");
1413ad6c7f9SVictor Latushkin (void) fprintf(stderr, " -i intent logs\n");
14207428bdfSVictor Latushkin (void) fprintf(stderr, " -C config (or cachefile if alone)\n");
1438f18d1faSGeorge Wilson (void) fprintf(stderr, " -h pool history\n");
144fa9e4066Sahrens (void) fprintf(stderr, " -b block statistics\n");
145d6e555bdSGeorge Wilson (void) fprintf(stderr, " -m metaslabs\n");
14602b647c5SGeorge Wilson (void) fprintf(stderr, " -M metaslab groups\n");
1476365109dSVictor Latushkin (void) fprintf(stderr, " -c checksum all metadata (twice for "
1486365109dSVictor Latushkin "all data) blocks\n");
149fa9e4066Sahrens (void) fprintf(stderr, " -s report stats on zdb's I/O\n");
150f0ba89beSJeff Bonwick (void) fprintf(stderr, " -D dedup statistics\n");
151b24ab676SJeff Bonwick (void) fprintf(stderr, " -S simulate dedup to measure effect\n");
152fa9e4066Sahrens (void) fprintf(stderr, " -v verbose (applies to all others)\n");
153fa9e4066Sahrens (void) fprintf(stderr, " -l dump label contents\n");
15482a0a985SVictor Latushkin (void) fprintf(stderr, " -L disable leak tracking (do not "
15582a0a985SVictor Latushkin "load spacemaps)\n");
15644cd46caSbillm (void) fprintf(stderr, " -R read and display block from a "
1573ad6c7f9SVictor Latushkin "device\n\n");
1583ad6c7f9SVictor Latushkin (void) fprintf(stderr, " Below options are intended for use "
159dffb1e38SMatthew Ahrens "with other options:\n");
160feef89cfSVictor Latushkin (void) fprintf(stderr, " -A ignore assertions (-A), enable "
161feef89cfSVictor Latushkin "panic recovery (-AA) or both (-AAA)\n");
162c8ee1847SVictor Latushkin (void) fprintf(stderr, " -F attempt automatic rewind within "
163c8ee1847SVictor Latushkin "safe range of transaction groups\n");
1643ad6c7f9SVictor Latushkin (void) fprintf(stderr, " -U <cachefile_path> -- use alternate "
1653ad6c7f9SVictor Latushkin "cachefile\n");
166c8ee1847SVictor Latushkin (void) fprintf(stderr, " -X attempt extreme rewind (does not "
167c8ee1847SVictor Latushkin "work with dataset)\n");
1683ad6c7f9SVictor Latushkin (void) fprintf(stderr, " -e pool is exported/destroyed/"
1693ad6c7f9SVictor Latushkin "has altroot/not in a cachefile\n");
1703ad6c7f9SVictor Latushkin (void) fprintf(stderr, " -p <path> -- use one or more with "
1713ad6c7f9SVictor Latushkin "-e to specify path to vdev dir\n");
172dffb1e38SMatthew Ahrens (void) fprintf(stderr, " -x <dumpdir> -- "
173dffb1e38SMatthew Ahrens "dump all read blocks into specified directory\n");
17490e894e2SRichard Lowe (void) fprintf(stderr, " -P print numbers in parseable form\n");
1753ad6c7f9SVictor Latushkin (void) fprintf(stderr, " -t <txg> -- highest txg to use when "
1762e551927SVictor Latushkin "searching for uberblocks\n");
17702b647c5SGeorge Wilson (void) fprintf(stderr, " -I <number of inflight I/Os> -- "
178dffb1e38SMatthew Ahrens "specify the maximum number of "
179dffb1e38SMatthew Ahrens "checksumming I/Os [default is 200]\n");
180fa9e4066Sahrens (void) fprintf(stderr, "Specify an option more than once (e.g. -bb) "
181fa9e4066Sahrens "to make only that option verbose\n");
182fa9e4066Sahrens (void) fprintf(stderr, "Default is to dump everything non-verbosely\n");
183fa9e4066Sahrens exit(1);
184fa9e4066Sahrens }
185fa9e4066Sahrens
186ccba0801SRich Morris /*
187ccba0801SRich Morris * Called for usage errors that are discovered after a call to spa_open(),
188ccba0801SRich Morris * dmu_bonus_hold(), or pool_match(). abort() is called for other errors.
189ccba0801SRich Morris */
190ccba0801SRich Morris
191fa9e4066Sahrens static void
fatal(const char * fmt,...)192fa9e4066Sahrens fatal(const char *fmt, ...)
193fa9e4066Sahrens {
194fa9e4066Sahrens va_list ap;
195fa9e4066Sahrens
196fa9e4066Sahrens va_start(ap, fmt);
197fa9e4066Sahrens (void) fprintf(stderr, "%s: ", cmdname);
198fa9e4066Sahrens (void) vfprintf(stderr, fmt, ap);
199fa9e4066Sahrens va_end(ap);
200fa9e4066Sahrens (void) fprintf(stderr, "\n");
201fa9e4066Sahrens
202ccba0801SRich Morris exit(1);
203fa9e4066Sahrens }
204fa9e4066Sahrens
205fa9e4066Sahrens /* ARGSUSED */
206fa9e4066Sahrens static void
dump_packed_nvlist(objset_t * os,uint64_t object,void * data,size_t size)207fa9e4066Sahrens dump_packed_nvlist(objset_t *os, uint64_t object, void *data, size_t size)
208fa9e4066Sahrens {
209fa9e4066Sahrens nvlist_t *nv;
210fa9e4066Sahrens size_t nvsize = *(uint64_t *)data;
211fa9e4066Sahrens char *packed = umem_alloc(nvsize, UMEM_NOFAIL);
212fa9e4066Sahrens
2137bfdf011SNeil Perrin VERIFY(0 == dmu_read(os, object, 0, nvsize, packed, DMU_READ_PREFETCH));
214fa9e4066Sahrens
215fa9e4066Sahrens VERIFY(nvlist_unpack(packed, nvsize, &nv, 0) == 0);
216fa9e4066Sahrens
217fa9e4066Sahrens umem_free(packed, nvsize);
218fa9e4066Sahrens
219fa9e4066Sahrens dump_nvlist(nv, 8);
220fa9e4066Sahrens
221fa9e4066Sahrens nvlist_free(nv);
222fa9e4066Sahrens }
223fa9e4066Sahrens
2244445fffbSMatthew Ahrens /* ARGSUSED */
2254445fffbSMatthew Ahrens static void
dump_history_offsets(objset_t * os,uint64_t object,void * data,size_t size)2264445fffbSMatthew Ahrens dump_history_offsets(objset_t *os, uint64_t object, void *data, size_t size)
2274445fffbSMatthew Ahrens {
2284445fffbSMatthew Ahrens spa_history_phys_t *shp = data;
2294445fffbSMatthew Ahrens
2304445fffbSMatthew Ahrens if (shp == NULL)
2314445fffbSMatthew Ahrens return;
2324445fffbSMatthew Ahrens
2334445fffbSMatthew Ahrens (void) printf("\t\tpool_create_len = %llu\n",
2344445fffbSMatthew Ahrens (u_longlong_t)shp->sh_pool_create_len);
2354445fffbSMatthew Ahrens (void) printf("\t\tphys_max_off = %llu\n",
2364445fffbSMatthew Ahrens (u_longlong_t)shp->sh_phys_max_off);
2374445fffbSMatthew Ahrens (void) printf("\t\tbof = %llu\n",
2384445fffbSMatthew Ahrens (u_longlong_t)shp->sh_bof);
2394445fffbSMatthew Ahrens (void) printf("\t\teof = %llu\n",
2404445fffbSMatthew Ahrens (u_longlong_t)shp->sh_eof);
2414445fffbSMatthew Ahrens (void) printf("\t\trecords_lost = %llu\n",
2424445fffbSMatthew Ahrens (u_longlong_t)shp->sh_records_lost);
2434445fffbSMatthew Ahrens }
2444445fffbSMatthew Ahrens
2453f9d6ad7SLin Ling static void
zdb_nicenum(uint64_t num,char * buf)2463f9d6ad7SLin Ling zdb_nicenum(uint64_t num, char *buf)
2473f9d6ad7SLin Ling {
2483f9d6ad7SLin Ling if (dump_opt['P'])
2493f9d6ad7SLin Ling (void) sprintf(buf, "%llu", (longlong_t)num);
2503f9d6ad7SLin Ling else
2513f9d6ad7SLin Ling nicenum(num, buf);
2523f9d6ad7SLin Ling }
2533f9d6ad7SLin Ling
254490d05b9SMatthew Ahrens const char histo_stars[] = "****************************************";
255490d05b9SMatthew Ahrens const int histo_width = sizeof (histo_stars) - 1;
256fa9e4066Sahrens
257fa9e4066Sahrens static void
dump_histogram(const uint64_t * histo,int size,int offset)2580713e232SGeorge Wilson dump_histogram(const uint64_t *histo, int size, int offset)
259fa9e4066Sahrens {
260fa9e4066Sahrens int i;
261490d05b9SMatthew Ahrens int minidx = size - 1;
262fa9e4066Sahrens int maxidx = 0;
263fa9e4066Sahrens uint64_t max = 0;
264fa9e4066Sahrens
265490d05b9SMatthew Ahrens for (i = 0; i < size; i++) {
266fa9e4066Sahrens if (histo[i] > max)
267fa9e4066Sahrens max = histo[i];
268fa9e4066Sahrens if (histo[i] > 0 && i > maxidx)
269fa9e4066Sahrens maxidx = i;
270fa9e4066Sahrens if (histo[i] > 0 && i < minidx)
271fa9e4066Sahrens minidx = i;
272fa9e4066Sahrens }
273fa9e4066Sahrens
274490d05b9SMatthew Ahrens if (max < histo_width)
275490d05b9SMatthew Ahrens max = histo_width;
276fa9e4066Sahrens
277490d05b9SMatthew Ahrens for (i = minidx; i <= maxidx; i++) {
278490d05b9SMatthew Ahrens (void) printf("\t\t\t%3u: %6llu %s\n",
2790713e232SGeorge Wilson i + offset, (u_longlong_t)histo[i],
280490d05b9SMatthew Ahrens &histo_stars[(max - histo[i]) * histo_width / max]);
281490d05b9SMatthew Ahrens }
282fa9e4066Sahrens }
283fa9e4066Sahrens
284fa9e4066Sahrens static void
dump_zap_stats(objset_t * os,uint64_t object)285fa9e4066Sahrens dump_zap_stats(objset_t *os, uint64_t object)
286fa9e4066Sahrens {
287fa9e4066Sahrens int error;
288fa9e4066Sahrens zap_stats_t zs;
289fa9e4066Sahrens
290fa9e4066Sahrens error = zap_get_stats(os, object, &zs);
291fa9e4066Sahrens if (error)
292fa9e4066Sahrens return;
293fa9e4066Sahrens
294fa9e4066Sahrens if (zs.zs_ptrtbl_len == 0) {
295fa9e4066Sahrens ASSERT(zs.zs_num_blocks == 1);
296fa9e4066Sahrens (void) printf("\tmicrozap: %llu bytes, %llu entries\n",
297fa9e4066Sahrens (u_longlong_t)zs.zs_blocksize,
298fa9e4066Sahrens (u_longlong_t)zs.zs_num_entries);
299fa9e4066Sahrens return;
300fa9e4066Sahrens }
301fa9e4066Sahrens
302fa9e4066Sahrens (void) printf("\tFat ZAP stats:\n");
3038248818dSnd150628
3048248818dSnd150628 (void) printf("\t\tPointer table:\n");
3058248818dSnd150628 (void) printf("\t\t\t%llu elements\n",
306fa9e4066Sahrens (u_longlong_t)zs.zs_ptrtbl_len);
3078248818dSnd150628 (void) printf("\t\t\tzt_blk: %llu\n",
3088248818dSnd150628 (u_longlong_t)zs.zs_ptrtbl_zt_blk);
3098248818dSnd150628 (void) printf("\t\t\tzt_numblks: %llu\n",
3108248818dSnd150628 (u_longlong_t)zs.zs_ptrtbl_zt_numblks);
3118248818dSnd150628 (void) printf("\t\t\tzt_shift: %llu\n",
3128248818dSnd150628 (u_longlong_t)zs.zs_ptrtbl_zt_shift);
3138248818dSnd150628 (void) printf("\t\t\tzt_blks_copied: %llu\n",
3148248818dSnd150628 (u_longlong_t)zs.zs_ptrtbl_blks_copied);
3158248818dSnd150628 (void) printf("\t\t\tzt_nextblk: %llu\n",
3168248818dSnd150628 (u_longlong_t)zs.zs_ptrtbl_nextblk);
3178248818dSnd150628
318fa9e4066Sahrens (void) printf("\t\tZAP entries: %llu\n",
319fa9e4066Sahrens (u_longlong_t)zs.zs_num_entries);
320fa9e4066Sahrens (void) printf("\t\tLeaf blocks: %llu\n",
321fa9e4066Sahrens (u_longlong_t)zs.zs_num_leafs);
322fa9e4066Sahrens (void) printf("\t\tTotal blocks: %llu\n",
323fa9e4066Sahrens (u_longlong_t)zs.zs_num_blocks);
3248248818dSnd150628 (void) printf("\t\tzap_block_type: 0x%llx\n",
3258248818dSnd150628 (u_longlong_t)zs.zs_block_type);
3268248818dSnd150628 (void) printf("\t\tzap_magic: 0x%llx\n",
3278248818dSnd150628 (u_longlong_t)zs.zs_magic);
3288248818dSnd150628 (void) printf("\t\tzap_salt: 0x%llx\n",
3298248818dSnd150628 (u_longlong_t)zs.zs_salt);
330fa9e4066Sahrens
331fa9e4066Sahrens (void) printf("\t\tLeafs with 2^n pointers:\n");
3320713e232SGeorge Wilson dump_histogram(zs.zs_leafs_with_2n_pointers, ZAP_HISTOGRAM_SIZE, 0);
333fa9e4066Sahrens
334fa9e4066Sahrens (void) printf("\t\tBlocks with n*5 entries:\n");
3350713e232SGeorge Wilson dump_histogram(zs.zs_blocks_with_n5_entries, ZAP_HISTOGRAM_SIZE, 0);
336fa9e4066Sahrens
337fa9e4066Sahrens (void) printf("\t\tBlocks n/10 full:\n");
3380713e232SGeorge Wilson dump_histogram(zs.zs_blocks_n_tenths_full, ZAP_HISTOGRAM_SIZE, 0);
339fa9e4066Sahrens
340fa9e4066Sahrens (void) printf("\t\tEntries with n chunks:\n");
3410713e232SGeorge Wilson dump_histogram(zs.zs_entries_using_n_chunks, ZAP_HISTOGRAM_SIZE, 0);
342fa9e4066Sahrens
343fa9e4066Sahrens (void) printf("\t\tBuckets with n entries:\n");
3440713e232SGeorge Wilson dump_histogram(zs.zs_buckets_with_n_entries, ZAP_HISTOGRAM_SIZE, 0);
345fa9e4066Sahrens }
346fa9e4066Sahrens
347fa9e4066Sahrens /*ARGSUSED*/
348fa9e4066Sahrens static void
dump_none(objset_t * os,uint64_t object,void * data,size_t size)349fa9e4066Sahrens dump_none(objset_t *os, uint64_t object, void *data, size_t size)
350fa9e4066Sahrens {
351fa9e4066Sahrens }
352fa9e4066Sahrens
353fa9e4066Sahrens /*ARGSUSED*/
3546de8f417SVictor Latushkin static void
dump_unknown(objset_t * os,uint64_t object,void * data,size_t size)3556de8f417SVictor Latushkin dump_unknown(objset_t *os, uint64_t object, void *data, size_t size)
3566de8f417SVictor Latushkin {
3576de8f417SVictor Latushkin (void) printf("\tUNKNOWN OBJECT TYPE\n");
3586de8f417SVictor Latushkin }
3596de8f417SVictor Latushkin
3606de8f417SVictor Latushkin /*ARGSUSED*/
361fa9e4066Sahrens void
dump_uint8(objset_t * os,uint64_t object,void * data,size_t size)362fa9e4066Sahrens dump_uint8(objset_t *os, uint64_t object, void *data, size_t size)
363fa9e4066Sahrens {
364fa9e4066Sahrens }
365fa9e4066Sahrens
366fa9e4066Sahrens /*ARGSUSED*/
367fa9e4066Sahrens static void
dump_uint64(objset_t * os,uint64_t object,void * data,size_t size)368fa9e4066Sahrens dump_uint64(objset_t *os, uint64_t object, void *data, size_t size)
369fa9e4066Sahrens {
370fa9e4066Sahrens }
371fa9e4066Sahrens
372fa9e4066Sahrens /*ARGSUSED*/
373fa9e4066Sahrens static void
dump_zap(objset_t * os,uint64_t object,void * data,size_t size)374fa9e4066Sahrens dump_zap(objset_t *os, uint64_t object, void *data, size_t size)
375fa9e4066Sahrens {
376fa9e4066Sahrens zap_cursor_t zc;
377fa9e4066Sahrens zap_attribute_t attr;
378fa9e4066Sahrens void *prop;
379fa9e4066Sahrens int i;
380fa9e4066Sahrens
381fa9e4066Sahrens dump_zap_stats(os, object);
382fa9e4066Sahrens (void) printf("\n");
383fa9e4066Sahrens
384fa9e4066Sahrens for (zap_cursor_init(&zc, os, object);
385fa9e4066Sahrens zap_cursor_retrieve(&zc, &attr) == 0;
386fa9e4066Sahrens zap_cursor_advance(&zc)) {
387fa9e4066Sahrens (void) printf("\t\t%s = ", attr.za_name);
388fa9e4066Sahrens if (attr.za_num_integers == 0) {
389fa9e4066Sahrens (void) printf("\n");
390fa9e4066Sahrens continue;
391fa9e4066Sahrens }
392fa9e4066Sahrens prop = umem_zalloc(attr.za_num_integers *
393fa9e4066Sahrens attr.za_integer_length, UMEM_NOFAIL);
394fa9e4066Sahrens (void) zap_lookup(os, object, attr.za_name,
395fa9e4066Sahrens attr.za_integer_length, attr.za_num_integers, prop);
396fa9e4066Sahrens if (attr.za_integer_length == 1) {
397fa9e4066Sahrens (void) printf("%s", (char *)prop);
398fa9e4066Sahrens } else {
399fa9e4066Sahrens for (i = 0; i < attr.za_num_integers; i++) {
400fa9e4066Sahrens switch (attr.za_integer_length) {
401fa9e4066Sahrens case 2:
402fa9e4066Sahrens (void) printf("%u ",
403fa9e4066Sahrens ((uint16_t *)prop)[i]);
404fa9e4066Sahrens break;
405fa9e4066Sahrens case 4:
406fa9e4066Sahrens (void) printf("%u ",
407fa9e4066Sahrens ((uint32_t *)prop)[i]);
408fa9e4066Sahrens break;
409fa9e4066Sahrens case 8:
410fa9e4066Sahrens (void) printf("%lld ",
411fa9e4066Sahrens (u_longlong_t)((int64_t *)prop)[i]);
412fa9e4066Sahrens break;
413fa9e4066Sahrens }
414fa9e4066Sahrens }
415fa9e4066Sahrens }
416fa9e4066Sahrens (void) printf("\n");
417fa9e4066Sahrens umem_free(prop, attr.za_num_integers * attr.za_integer_length);
418fa9e4066Sahrens }
41987e5029aSahrens zap_cursor_fini(&zc);
420fa9e4066Sahrens }
421fa9e4066Sahrens
422bf263f2aSMatthew Ahrens static void
dump_bpobj(objset_t * os,uint64_t object,void * data,size_t size)423bf263f2aSMatthew Ahrens dump_bpobj(objset_t *os, uint64_t object, void *data, size_t size)
424bf263f2aSMatthew Ahrens {
425bf263f2aSMatthew Ahrens bpobj_phys_t *bpop = data;
426bf263f2aSMatthew Ahrens char bytes[32], comp[32], uncomp[32];
427bf263f2aSMatthew Ahrens
428bf263f2aSMatthew Ahrens if (bpop == NULL)
429bf263f2aSMatthew Ahrens return;
430bf263f2aSMatthew Ahrens
431bf263f2aSMatthew Ahrens zdb_nicenum(bpop->bpo_bytes, bytes);
432bf263f2aSMatthew Ahrens zdb_nicenum(bpop->bpo_comp, comp);
433bf263f2aSMatthew Ahrens zdb_nicenum(bpop->bpo_uncomp, uncomp);
434bf263f2aSMatthew Ahrens
435bf263f2aSMatthew Ahrens (void) printf("\t\tnum_blkptrs = %llu\n",
436bf263f2aSMatthew Ahrens (u_longlong_t)bpop->bpo_num_blkptrs);
437bf263f2aSMatthew Ahrens (void) printf("\t\tbytes = %s\n", bytes);
438bf263f2aSMatthew Ahrens if (size >= BPOBJ_SIZE_V1) {
439bf263f2aSMatthew Ahrens (void) printf("\t\tcomp = %s\n", comp);
440bf263f2aSMatthew Ahrens (void) printf("\t\tuncomp = %s\n", uncomp);
441bf263f2aSMatthew Ahrens }
442bf263f2aSMatthew Ahrens if (size >= sizeof (*bpop)) {
443bf263f2aSMatthew Ahrens (void) printf("\t\tsubobjs = %llu\n",
444bf263f2aSMatthew Ahrens (u_longlong_t)bpop->bpo_subobjs);
445bf263f2aSMatthew Ahrens (void) printf("\t\tnum_subobjs = %llu\n",
446bf263f2aSMatthew Ahrens (u_longlong_t)bpop->bpo_num_subobjs);
447bf263f2aSMatthew Ahrens }
448bf263f2aSMatthew Ahrens
449bf263f2aSMatthew Ahrens if (dump_opt['d'] < 5)
450bf263f2aSMatthew Ahrens return;
451bf263f2aSMatthew Ahrens
452bf263f2aSMatthew Ahrens for (uint64_t i = 0; i < bpop->bpo_num_blkptrs; i++) {
453bf263f2aSMatthew Ahrens char blkbuf[BP_SPRINTF_LEN];
454bf263f2aSMatthew Ahrens blkptr_t bp;
455bf263f2aSMatthew Ahrens
456bf263f2aSMatthew Ahrens int err = dmu_read(os, object,
457bf263f2aSMatthew Ahrens i * sizeof (bp), sizeof (bp), &bp, 0);
458bf263f2aSMatthew Ahrens if (err != 0) {
459bf263f2aSMatthew Ahrens (void) printf("got error %u from dmu_read\n", err);
460bf263f2aSMatthew Ahrens break;
461bf263f2aSMatthew Ahrens }
462bf263f2aSMatthew Ahrens snprintf_blkptr_compact(blkbuf, sizeof (blkbuf), &bp);
463bf263f2aSMatthew Ahrens (void) printf("\t%s\n", blkbuf);
464bf263f2aSMatthew Ahrens }
465bf263f2aSMatthew Ahrens }
466bf263f2aSMatthew Ahrens
467bf263f2aSMatthew Ahrens /* ARGSUSED */
468bf263f2aSMatthew Ahrens static void
dump_bpobj_subobjs(objset_t * os,uint64_t object,void * data,size_t size)469bf263f2aSMatthew Ahrens dump_bpobj_subobjs(objset_t *os, uint64_t object, void *data, size_t size)
470bf263f2aSMatthew Ahrens {
471bf263f2aSMatthew Ahrens dmu_object_info_t doi;
472bf263f2aSMatthew Ahrens
473bf263f2aSMatthew Ahrens VERIFY0(dmu_object_info(os, object, &doi));
474bf263f2aSMatthew Ahrens uint64_t *subobjs = kmem_alloc(doi.doi_max_offset, KM_SLEEP);
475bf263f2aSMatthew Ahrens
476bf263f2aSMatthew Ahrens int err = dmu_read(os, object, 0, doi.doi_max_offset, subobjs, 0);
477bf263f2aSMatthew Ahrens if (err != 0) {
478bf263f2aSMatthew Ahrens (void) printf("got error %u from dmu_read\n", err);
479bf263f2aSMatthew Ahrens kmem_free(subobjs, doi.doi_max_offset);
480bf263f2aSMatthew Ahrens return;
481bf263f2aSMatthew Ahrens }
482bf263f2aSMatthew Ahrens
483bf263f2aSMatthew Ahrens int64_t last_nonzero = -1;
484bf263f2aSMatthew Ahrens for (uint64_t i = 0; i < doi.doi_max_offset / 8; i++) {
485bf263f2aSMatthew Ahrens if (subobjs[i] != 0)
486bf263f2aSMatthew Ahrens last_nonzero = i;
487bf263f2aSMatthew Ahrens }
488bf263f2aSMatthew Ahrens
489bf263f2aSMatthew Ahrens for (int64_t i = 0; i <= last_nonzero; i++) {
490bf263f2aSMatthew Ahrens (void) printf("\t%llu\n", (longlong_t)subobjs[i]);
491bf263f2aSMatthew Ahrens }
492bf263f2aSMatthew Ahrens kmem_free(subobjs, doi.doi_max_offset);
493bf263f2aSMatthew Ahrens }
494bf263f2aSMatthew Ahrens
495e7437265Sahrens /*ARGSUSED*/
496e7437265Sahrens static void
dump_ddt_zap(objset_t * os,uint64_t object,void * data,size_t size)497486ae710SMatthew Ahrens dump_ddt_zap(objset_t *os, uint64_t object, void *data, size_t size)
498486ae710SMatthew Ahrens {
499486ae710SMatthew Ahrens dump_zap_stats(os, object);
500486ae710SMatthew Ahrens /* contents are printed elsewhere, properly decoded */
501486ae710SMatthew Ahrens }
502486ae710SMatthew Ahrens
503486ae710SMatthew Ahrens /*ARGSUSED*/
504486ae710SMatthew Ahrens static void
dump_sa_attrs(objset_t * os,uint64_t object,void * data,size_t size)5050a586ceaSMark Shellenbaum dump_sa_attrs(objset_t *os, uint64_t object, void *data, size_t size)
5060a586ceaSMark Shellenbaum {
5070a586ceaSMark Shellenbaum zap_cursor_t zc;
5080a586ceaSMark Shellenbaum zap_attribute_t attr;
5090a586ceaSMark Shellenbaum
5100a586ceaSMark Shellenbaum dump_zap_stats(os, object);
5110a586ceaSMark Shellenbaum (void) printf("\n");
5120a586ceaSMark Shellenbaum
5130a586ceaSMark Shellenbaum for (zap_cursor_init(&zc, os, object);
5140a586ceaSMark Shellenbaum zap_cursor_retrieve(&zc, &attr) == 0;
5150a586ceaSMark Shellenbaum zap_cursor_advance(&zc)) {
5160a586ceaSMark Shellenbaum (void) printf("\t\t%s = ", attr.za_name);
5170a586ceaSMark Shellenbaum if (attr.za_num_integers == 0) {
5180a586ceaSMark Shellenbaum (void) printf("\n");
5190a586ceaSMark Shellenbaum continue;
5200a586ceaSMark Shellenbaum }
5210a586ceaSMark Shellenbaum (void) printf(" %llx : [%d:%d:%d]\n",
5220a586ceaSMark Shellenbaum (u_longlong_t)attr.za_first_integer,
5230a586ceaSMark Shellenbaum (int)ATTR_LENGTH(attr.za_first_integer),
5240a586ceaSMark Shellenbaum (int)ATTR_BSWAP(attr.za_first_integer),
5250a586ceaSMark Shellenbaum (int)ATTR_NUM(attr.za_first_integer));
5260a586ceaSMark Shellenbaum }
5270a586ceaSMark Shellenbaum zap_cursor_fini(&zc);
5280a586ceaSMark Shellenbaum }
5290a586ceaSMark Shellenbaum
5300a586ceaSMark Shellenbaum /*ARGSUSED*/
5310a586ceaSMark Shellenbaum static void
dump_sa_layouts(objset_t * os,uint64_t object,void * data,size_t size)5320a586ceaSMark Shellenbaum dump_sa_layouts(objset_t *os, uint64_t object, void *data, size_t size)
5330a586ceaSMark Shellenbaum {
5340a586ceaSMark Shellenbaum zap_cursor_t zc;
5350a586ceaSMark Shellenbaum zap_attribute_t attr;
5360a586ceaSMark Shellenbaum uint16_t *layout_attrs;
5370a586ceaSMark Shellenbaum int i;
5380a586ceaSMark Shellenbaum
5390a586ceaSMark Shellenbaum dump_zap_stats(os, object);
5400a586ceaSMark Shellenbaum (void) printf("\n");
5410a586ceaSMark Shellenbaum
5420a586ceaSMark Shellenbaum for (zap_cursor_init(&zc, os, object);
5430a586ceaSMark Shellenbaum zap_cursor_retrieve(&zc, &attr) == 0;
5440a586ceaSMark Shellenbaum zap_cursor_advance(&zc)) {
5450a586ceaSMark Shellenbaum (void) printf("\t\t%s = [", attr.za_name);
5460a586ceaSMark Shellenbaum if (attr.za_num_integers == 0) {
5470a586ceaSMark Shellenbaum (void) printf("\n");
5480a586ceaSMark Shellenbaum continue;
5490a586ceaSMark Shellenbaum }
5500a586ceaSMark Shellenbaum
5510a586ceaSMark Shellenbaum VERIFY(attr.za_integer_length == 2);
5520a586ceaSMark Shellenbaum layout_attrs = umem_zalloc(attr.za_num_integers *
5530a586ceaSMark Shellenbaum attr.za_integer_length, UMEM_NOFAIL);
5540a586ceaSMark Shellenbaum
5550a586ceaSMark Shellenbaum VERIFY(zap_lookup(os, object, attr.za_name,
5560a586ceaSMark Shellenbaum attr.za_integer_length,
5570a586ceaSMark Shellenbaum attr.za_num_integers, layout_attrs) == 0);
5580a586ceaSMark Shellenbaum
5590a586ceaSMark Shellenbaum for (i = 0; i != attr.za_num_integers; i++)
5600a586ceaSMark Shellenbaum (void) printf(" %d ", (int)layout_attrs[i]);
5610a586ceaSMark Shellenbaum (void) printf("]\n");
5620a586ceaSMark Shellenbaum umem_free(layout_attrs,
5630a586ceaSMark Shellenbaum attr.za_num_integers * attr.za_integer_length);
5640a586ceaSMark Shellenbaum }
5650a586ceaSMark Shellenbaum zap_cursor_fini(&zc);
5660a586ceaSMark Shellenbaum }
5670a586ceaSMark Shellenbaum
5680a586ceaSMark Shellenbaum /*ARGSUSED*/
5690a586ceaSMark Shellenbaum static void
dump_zpldir(objset_t * os,uint64_t object,void * data,size_t size)570e7437265Sahrens dump_zpldir(objset_t *os, uint64_t object, void *data, size_t size)
571e7437265Sahrens {
572e7437265Sahrens zap_cursor_t zc;
573e7437265Sahrens zap_attribute_t attr;
574e7437265Sahrens const char *typenames[] = {
575e7437265Sahrens /* 0 */ "not specified",
576e7437265Sahrens /* 1 */ "FIFO",
577e7437265Sahrens /* 2 */ "Character Device",
578e7437265Sahrens /* 3 */ "3 (invalid)",
579e7437265Sahrens /* 4 */ "Directory",
580e7437265Sahrens /* 5 */ "5 (invalid)",
581e7437265Sahrens /* 6 */ "Block Device",
582e7437265Sahrens /* 7 */ "7 (invalid)",
583e7437265Sahrens /* 8 */ "Regular File",
584e7437265Sahrens /* 9 */ "9 (invalid)",
585e7437265Sahrens /* 10 */ "Symbolic Link",
586e7437265Sahrens /* 11 */ "11 (invalid)",
587e7437265Sahrens /* 12 */ "Socket",
588e7437265Sahrens /* 13 */ "Door",
589e7437265Sahrens /* 14 */ "Event Port",
590e7437265Sahrens /* 15 */ "15 (invalid)",
591e7437265Sahrens };
592e7437265Sahrens
593e7437265Sahrens dump_zap_stats(os, object);
594e7437265Sahrens (void) printf("\n");
595e7437265Sahrens
596e7437265Sahrens for (zap_cursor_init(&zc, os, object);
597e7437265Sahrens zap_cursor_retrieve(&zc, &attr) == 0;
598e7437265Sahrens zap_cursor_advance(&zc)) {
599e7437265Sahrens (void) printf("\t\t%s = %lld (type: %s)\n",
600e7437265Sahrens attr.za_name, ZFS_DIRENT_OBJ(attr.za_first_integer),
601e7437265Sahrens typenames[ZFS_DIRENT_TYPE(attr.za_first_integer)]);
602e7437265Sahrens }
603e7437265Sahrens zap_cursor_fini(&zc);
604e7437265Sahrens }
605e7437265Sahrens
6060713e232SGeorge Wilson int
get_dtl_refcount(vdev_t * vd)6070713e232SGeorge Wilson get_dtl_refcount(vdev_t *vd)
6080713e232SGeorge Wilson {
6090713e232SGeorge Wilson int refcount = 0;
6100713e232SGeorge Wilson
6110713e232SGeorge Wilson if (vd->vdev_ops->vdev_op_leaf) {
6120713e232SGeorge Wilson space_map_t *sm = vd->vdev_dtl_sm;
6130713e232SGeorge Wilson
6140713e232SGeorge Wilson if (sm != NULL &&
6150713e232SGeorge Wilson sm->sm_dbuf->db_size == sizeof (space_map_phys_t))
6160713e232SGeorge Wilson return (1);
6170713e232SGeorge Wilson return (0);
6180713e232SGeorge Wilson }
6190713e232SGeorge Wilson
6200713e232SGeorge Wilson for (int c = 0; c < vd->vdev_children; c++)
6210713e232SGeorge Wilson refcount += get_dtl_refcount(vd->vdev_child[c]);
6220713e232SGeorge Wilson return (refcount);
6230713e232SGeorge Wilson }
6240713e232SGeorge Wilson
6250713e232SGeorge Wilson int
get_metaslab_refcount(vdev_t * vd)6260713e232SGeorge Wilson get_metaslab_refcount(vdev_t *vd)
6270713e232SGeorge Wilson {
6280713e232SGeorge Wilson int refcount = 0;
6290713e232SGeorge Wilson
63002b647c5SGeorge Wilson if (vd->vdev_top == vd && !vd->vdev_removing) {
6310713e232SGeorge Wilson for (int m = 0; m < vd->vdev_ms_count; m++) {
6320713e232SGeorge Wilson space_map_t *sm = vd->vdev_ms[m]->ms_sm;
6330713e232SGeorge Wilson
6340713e232SGeorge Wilson if (sm != NULL &&
6350713e232SGeorge Wilson sm->sm_dbuf->db_size == sizeof (space_map_phys_t))
6360713e232SGeorge Wilson refcount++;
6370713e232SGeorge Wilson }
6380713e232SGeorge Wilson }
6390713e232SGeorge Wilson for (int c = 0; c < vd->vdev_children; c++)
6400713e232SGeorge Wilson refcount += get_metaslab_refcount(vd->vdev_child[c]);
6410713e232SGeorge Wilson
6420713e232SGeorge Wilson return (refcount);
6430713e232SGeorge Wilson }
6440713e232SGeorge Wilson
6450713e232SGeorge Wilson static int
verify_spacemap_refcounts(spa_t * spa)6460713e232SGeorge Wilson verify_spacemap_refcounts(spa_t *spa)
6470713e232SGeorge Wilson {
648dbc43af2SMatthew Ahrens uint64_t expected_refcount = 0;
649dbc43af2SMatthew Ahrens uint64_t actual_refcount;
6500713e232SGeorge Wilson
651dbc43af2SMatthew Ahrens (void) feature_get_refcount(spa,
652dbc43af2SMatthew Ahrens &spa_feature_table[SPA_FEATURE_SPACEMAP_HISTOGRAM],
653dbc43af2SMatthew Ahrens &expected_refcount);
6540713e232SGeorge Wilson actual_refcount = get_dtl_refcount(spa->spa_root_vdev);
6550713e232SGeorge Wilson actual_refcount += get_metaslab_refcount(spa->spa_root_vdev);
6560713e232SGeorge Wilson
6570713e232SGeorge Wilson if (expected_refcount != actual_refcount) {
658dbc43af2SMatthew Ahrens (void) printf("space map refcount mismatch: expected %lld != "
659dbc43af2SMatthew Ahrens "actual %lld\n",
660dbc43af2SMatthew Ahrens (longlong_t)expected_refcount,
661dbc43af2SMatthew Ahrens (longlong_t)actual_refcount);
6620713e232SGeorge Wilson return (2);
6630713e232SGeorge Wilson }
6640713e232SGeorge Wilson return (0);
6650713e232SGeorge Wilson }
6660713e232SGeorge Wilson
667fa9e4066Sahrens static void
dump_spacemap(objset_t * os,space_map_t * sm)6680713e232SGeorge Wilson dump_spacemap(objset_t *os, space_map_t *sm)
669fa9e4066Sahrens {
670fa9e4066Sahrens uint64_t alloc, offset, entry;
6718053a263Sck153898 char *ddata[] = { "ALLOC", "FREE", "CONDENSE", "INVALID",
6728053a263Sck153898 "INVALID", "INVALID", "INVALID", "INVALID" };
673fa9e4066Sahrens
6740713e232SGeorge Wilson if (sm == NULL)
675fa9e4066Sahrens return;
676fa9e4066Sahrens
677fa9e4066Sahrens /*
678fa9e4066Sahrens * Print out the freelist entries in both encoded and decoded form.
679fa9e4066Sahrens */
680fa9e4066Sahrens alloc = 0;
6810713e232SGeorge Wilson for (offset = 0; offset < space_map_length(sm);
6820713e232SGeorge Wilson offset += sizeof (entry)) {
6830713e232SGeorge Wilson uint8_t mapshift = sm->sm_shift;
6840713e232SGeorge Wilson
6850713e232SGeorge Wilson VERIFY0(dmu_read(os, space_map_object(sm), offset,
6867bfdf011SNeil Perrin sizeof (entry), &entry, DMU_READ_PREFETCH));
687fa9e4066Sahrens if (SM_DEBUG_DECODE(entry)) {
6880713e232SGeorge Wilson
68987219db7SVictor Latushkin (void) printf("\t [%6llu] %s: txg %llu, pass %llu\n",
690fa9e4066Sahrens (u_longlong_t)(offset / sizeof (entry)),
691fa9e4066Sahrens ddata[SM_DEBUG_ACTION_DECODE(entry)],
6925ad82045Snd150628 (u_longlong_t)SM_DEBUG_TXG_DECODE(entry),
6935ad82045Snd150628 (u_longlong_t)SM_DEBUG_SYNCPASS_DECODE(entry));
694fa9e4066Sahrens } else {
69587219db7SVictor Latushkin (void) printf("\t [%6llu] %c range:"
69687219db7SVictor Latushkin " %010llx-%010llx size: %06llx\n",
697fa9e4066Sahrens (u_longlong_t)(offset / sizeof (entry)),
698fa9e4066Sahrens SM_TYPE_DECODE(entry) == SM_ALLOC ? 'A' : 'F',
6995ad82045Snd150628 (u_longlong_t)((SM_OFFSET_DECODE(entry) <<
7000713e232SGeorge Wilson mapshift) + sm->sm_start),
7015ad82045Snd150628 (u_longlong_t)((SM_OFFSET_DECODE(entry) <<
7020713e232SGeorge Wilson mapshift) + sm->sm_start +
7030713e232SGeorge Wilson (SM_RUN_DECODE(entry) << mapshift)),
7045ad82045Snd150628 (u_longlong_t)(SM_RUN_DECODE(entry) << mapshift));
705fa9e4066Sahrens if (SM_TYPE_DECODE(entry) == SM_ALLOC)
706fa9e4066Sahrens alloc += SM_RUN_DECODE(entry) << mapshift;
707fa9e4066Sahrens else
708fa9e4066Sahrens alloc -= SM_RUN_DECODE(entry) << mapshift;
709fa9e4066Sahrens }
710fa9e4066Sahrens }
7110713e232SGeorge Wilson if (alloc != space_map_allocated(sm)) {
712fa9e4066Sahrens (void) printf("space_map_object alloc (%llu) INCONSISTENT "
713fa9e4066Sahrens "with space map summary (%llu)\n",
7140713e232SGeorge Wilson (u_longlong_t)space_map_allocated(sm), (u_longlong_t)alloc);
715fa9e4066Sahrens }
716fa9e4066Sahrens }
717fa9e4066Sahrens
718fa9e4066Sahrens static void
dump_metaslab_stats(metaslab_t * msp)719d6e555bdSGeorge Wilson dump_metaslab_stats(metaslab_t *msp)
720d6e555bdSGeorge Wilson {
7213f9d6ad7SLin Ling char maxbuf[32];
7220713e232SGeorge Wilson range_tree_t *rt = msp->ms_tree;
7230713e232SGeorge Wilson avl_tree_t *t = &msp->ms_size_tree;
7240713e232SGeorge Wilson int free_pct = range_tree_space(rt) * 100 / msp->ms_size;
725d6e555bdSGeorge Wilson
7260713e232SGeorge Wilson zdb_nicenum(metaslab_block_maxsize(msp), maxbuf);
727d6e555bdSGeorge Wilson
72887219db7SVictor Latushkin (void) printf("\t %25s %10lu %7s %6s %4s %4d%%\n",
729d6e555bdSGeorge Wilson "segments", avl_numnodes(t), "maxsize", maxbuf,
730d6e555bdSGeorge Wilson "freepct", free_pct);
7310713e232SGeorge Wilson (void) printf("\tIn-memory histogram:\n");
7320713e232SGeorge Wilson dump_histogram(rt->rt_histogram, RANGE_TREE_HISTOGRAM_SIZE, 0);
733d6e555bdSGeorge Wilson }
734d6e555bdSGeorge Wilson
735d6e555bdSGeorge Wilson static void
dump_metaslab(metaslab_t * msp)736fa9e4066Sahrens dump_metaslab(metaslab_t *msp)
737fa9e4066Sahrens {
738fa9e4066Sahrens vdev_t *vd = msp->ms_group->mg_vd;
739fa9e4066Sahrens spa_t *spa = vd->vdev_spa;
7400713e232SGeorge Wilson space_map_t *sm = msp->ms_sm;
7413f9d6ad7SLin Ling char freebuf[32];
742fa9e4066Sahrens
7430713e232SGeorge Wilson zdb_nicenum(msp->ms_size - space_map_allocated(sm), freebuf);
744fa9e4066Sahrens
745fa9e4066Sahrens (void) printf(
74687219db7SVictor Latushkin "\tmetaslab %6llu offset %12llx spacemap %6llu free %5s\n",
7470713e232SGeorge Wilson (u_longlong_t)msp->ms_id, (u_longlong_t)msp->ms_start,
7480713e232SGeorge Wilson (u_longlong_t)space_map_object(sm), freebuf);
749fa9e4066Sahrens
7500713e232SGeorge Wilson if (dump_opt['m'] > 2 && !dump_opt['L']) {
751d6e555bdSGeorge Wilson mutex_enter(&msp->ms_lock);
7520713e232SGeorge Wilson metaslab_load_wait(msp);
7530713e232SGeorge Wilson if (!msp->ms_loaded) {
7540713e232SGeorge Wilson VERIFY0(metaslab_load(msp));
7550713e232SGeorge Wilson range_tree_stat_verify(msp->ms_tree);
7560713e232SGeorge Wilson }
757d6e555bdSGeorge Wilson dump_metaslab_stats(msp);
7580713e232SGeorge Wilson metaslab_unload(msp);
759d6e555bdSGeorge Wilson mutex_exit(&msp->ms_lock);
760d6e555bdSGeorge Wilson }
761d6e555bdSGeorge Wilson
7620713e232SGeorge Wilson if (dump_opt['m'] > 1 && sm != NULL &&
763dbc43af2SMatthew Ahrens spa_feature_is_active(spa, SPA_FEATURE_SPACEMAP_HISTOGRAM)) {
7640713e232SGeorge Wilson /*
7650713e232SGeorge Wilson * The space map histogram represents free space in chunks
7660713e232SGeorge Wilson * of sm_shift (i.e. bucket 0 refers to 2^sm_shift).
7670713e232SGeorge Wilson */
76802b647c5SGeorge Wilson (void) printf("\tOn-disk histogram:\t\tfragmentation %llu\n",
76902b647c5SGeorge Wilson (u_longlong_t)msp->ms_fragmentation);
7700713e232SGeorge Wilson dump_histogram(sm->sm_phys->smp_histogram,
77102b647c5SGeorge Wilson SPACE_MAP_HISTOGRAM_SIZE, sm->sm_shift);
7720713e232SGeorge Wilson }
7730713e232SGeorge Wilson
7740713e232SGeorge Wilson if (dump_opt['d'] > 5 || dump_opt['m'] > 3) {
7750713e232SGeorge Wilson ASSERT(msp->ms_size == (1ULL << vd->vdev_ms_shift));
776fa9e4066Sahrens
777d6e555bdSGeorge Wilson mutex_enter(&msp->ms_lock);
7780713e232SGeorge Wilson dump_spacemap(spa->spa_meta_objset, msp->ms_sm);
779d6e555bdSGeorge Wilson mutex_exit(&msp->ms_lock);
780d6e555bdSGeorge Wilson }
78187219db7SVictor Latushkin }
782d6e555bdSGeorge Wilson
78387219db7SVictor Latushkin static void
print_vdev_metaslab_header(vdev_t * vd)78487219db7SVictor Latushkin print_vdev_metaslab_header(vdev_t *vd)
78587219db7SVictor Latushkin {
78687219db7SVictor Latushkin (void) printf("\tvdev %10llu\n\t%-10s%5llu %-19s %-15s %-10s\n",
78787219db7SVictor Latushkin (u_longlong_t)vd->vdev_id,
78887219db7SVictor Latushkin "metaslabs", (u_longlong_t)vd->vdev_ms_count,
78987219db7SVictor Latushkin "offset", "spacemap", "free");
79087219db7SVictor Latushkin (void) printf("\t%15s %19s %15s %10s\n",
79187219db7SVictor Latushkin "---------------", "-------------------",
79287219db7SVictor Latushkin "---------------", "-------------");
793fa9e4066Sahrens }
794fa9e4066Sahrens
795fa9e4066Sahrens static void
dump_metaslab_groups(spa_t * spa)79602b647c5SGeorge Wilson dump_metaslab_groups(spa_t *spa)
79702b647c5SGeorge Wilson {
79802b647c5SGeorge Wilson vdev_t *rvd = spa->spa_root_vdev;
79902b647c5SGeorge Wilson metaslab_class_t *mc = spa_normal_class(spa);
80002b647c5SGeorge Wilson uint64_t fragmentation;
80102b647c5SGeorge Wilson
80202b647c5SGeorge Wilson metaslab_class_histogram_verify(mc);
80302b647c5SGeorge Wilson
80402b647c5SGeorge Wilson for (int c = 0; c < rvd->vdev_children; c++) {
80502b647c5SGeorge Wilson vdev_t *tvd = rvd->vdev_child[c];
80602b647c5SGeorge Wilson metaslab_group_t *mg = tvd->vdev_mg;
80702b647c5SGeorge Wilson
80802b647c5SGeorge Wilson if (mg->mg_class != mc)
80902b647c5SGeorge Wilson continue;
81002b647c5SGeorge Wilson
81102b647c5SGeorge Wilson metaslab_group_histogram_verify(mg);
81202b647c5SGeorge Wilson mg->mg_fragmentation = metaslab_group_fragmentation(mg);
81302b647c5SGeorge Wilson
81402b647c5SGeorge Wilson (void) printf("\tvdev %10llu\t\tmetaslabs%5llu\t\t"
81502b647c5SGeorge Wilson "fragmentation",
81602b647c5SGeorge Wilson (u_longlong_t)tvd->vdev_id,
81702b647c5SGeorge Wilson (u_longlong_t)tvd->vdev_ms_count);
81802b647c5SGeorge Wilson if (mg->mg_fragmentation == ZFS_FRAG_INVALID) {
81902b647c5SGeorge Wilson (void) printf("%3s\n", "-");
82002b647c5SGeorge Wilson } else {
82102b647c5SGeorge Wilson (void) printf("%3llu%%\n",
82202b647c5SGeorge Wilson (u_longlong_t)mg->mg_fragmentation);
82302b647c5SGeorge Wilson }
82402b647c5SGeorge Wilson dump_histogram(mg->mg_histogram, RANGE_TREE_HISTOGRAM_SIZE, 0);
82502b647c5SGeorge Wilson }
82602b647c5SGeorge Wilson
82702b647c5SGeorge Wilson (void) printf("\tpool %s\tfragmentation", spa_name(spa));
82802b647c5SGeorge Wilson fragmentation = metaslab_class_fragmentation(mc);
82902b647c5SGeorge Wilson if (fragmentation == ZFS_FRAG_INVALID)
83002b647c5SGeorge Wilson (void) printf("\t%3s\n", "-");
83102b647c5SGeorge Wilson else
83202b647c5SGeorge Wilson (void) printf("\t%3llu%%\n", (u_longlong_t)fragmentation);
83302b647c5SGeorge Wilson dump_histogram(mc->mc_histogram, RANGE_TREE_HISTOGRAM_SIZE, 0);
83402b647c5SGeorge Wilson }
83502b647c5SGeorge Wilson
83602b647c5SGeorge Wilson static void
dump_metaslabs(spa_t * spa)837fa9e4066Sahrens dump_metaslabs(spa_t *spa)
838fa9e4066Sahrens {
83987219db7SVictor Latushkin vdev_t *vd, *rvd = spa->spa_root_vdev;
84087219db7SVictor Latushkin uint64_t m, c = 0, children = rvd->vdev_children;
841fa9e4066Sahrens
842fa9e4066Sahrens (void) printf("\nMetaslabs:\n");
843fa9e4066Sahrens
84487219db7SVictor Latushkin if (!dump_opt['d'] && zopt_objects > 0) {
84587219db7SVictor Latushkin c = zopt_object[0];
846fa9e4066Sahrens
84787219db7SVictor Latushkin if (c >= children)
84887219db7SVictor Latushkin (void) fatal("bad vdev id: %llu", (u_longlong_t)c);
84987219db7SVictor Latushkin
85087219db7SVictor Latushkin if (zopt_objects > 1) {
85187219db7SVictor Latushkin vd = rvd->vdev_child[c];
85287219db7SVictor Latushkin print_vdev_metaslab_header(vd);
85387219db7SVictor Latushkin
85487219db7SVictor Latushkin for (m = 1; m < zopt_objects; m++) {
85587219db7SVictor Latushkin if (zopt_object[m] < vd->vdev_ms_count)
85687219db7SVictor Latushkin dump_metaslab(
85787219db7SVictor Latushkin vd->vdev_ms[zopt_object[m]]);
85887219db7SVictor Latushkin else
85987219db7SVictor Latushkin (void) fprintf(stderr, "bad metaslab "
86087219db7SVictor Latushkin "number %llu\n",
86187219db7SVictor Latushkin (u_longlong_t)zopt_object[m]);
86287219db7SVictor Latushkin }
86387219db7SVictor Latushkin (void) printf("\n");
86487219db7SVictor Latushkin return;
86587219db7SVictor Latushkin }
86687219db7SVictor Latushkin children = c + 1;
86787219db7SVictor Latushkin }
86887219db7SVictor Latushkin for (; c < children; c++) {
86987219db7SVictor Latushkin vd = rvd->vdev_child[c];
87087219db7SVictor Latushkin print_vdev_metaslab_header(vd);
871fa9e4066Sahrens
872fa9e4066Sahrens for (m = 0; m < vd->vdev_ms_count; m++)
873fa9e4066Sahrens dump_metaslab(vd->vdev_ms[m]);
874fa9e4066Sahrens (void) printf("\n");
875fa9e4066Sahrens }
876fa9e4066Sahrens }
877fa9e4066Sahrens
878fa9e4066Sahrens static void
dump_dde(const ddt_t * ddt,const ddt_entry_t * dde,uint64_t index)879b24ab676SJeff Bonwick dump_dde(const ddt_t *ddt, const ddt_entry_t *dde, uint64_t index)
880b24ab676SJeff Bonwick {
881b24ab676SJeff Bonwick const ddt_phys_t *ddp = dde->dde_phys;
882b24ab676SJeff Bonwick const ddt_key_t *ddk = &dde->dde_key;
883b24ab676SJeff Bonwick char *types[4] = { "ditto", "single", "double", "triple" };
884b24ab676SJeff Bonwick char blkbuf[BP_SPRINTF_LEN];
885b24ab676SJeff Bonwick blkptr_t blk;
886b24ab676SJeff Bonwick
887b24ab676SJeff Bonwick for (int p = 0; p < DDT_PHYS_TYPES; p++, ddp++) {
888b24ab676SJeff Bonwick if (ddp->ddp_phys_birth == 0)
889b24ab676SJeff Bonwick continue;
890bbfd46c4SJeff Bonwick ddt_bp_create(ddt->ddt_checksum, ddk, ddp, &blk);
89149064978SMax Grossman snprintf_blkptr(blkbuf, sizeof (blkbuf), &blk);
892b24ab676SJeff Bonwick (void) printf("index %llx refcnt %llu %s %s\n",
893b24ab676SJeff Bonwick (u_longlong_t)index, (u_longlong_t)ddp->ddp_refcnt,
894b24ab676SJeff Bonwick types[p], blkbuf);
895b24ab676SJeff Bonwick }
896b24ab676SJeff Bonwick }
897b24ab676SJeff Bonwick
898b24ab676SJeff Bonwick static void
dump_dedup_ratio(const ddt_stat_t * dds)899b24ab676SJeff Bonwick dump_dedup_ratio(const ddt_stat_t *dds)
900b24ab676SJeff Bonwick {
901b24ab676SJeff Bonwick double rL, rP, rD, D, dedup, compress, copies;
902b24ab676SJeff Bonwick
903b24ab676SJeff Bonwick if (dds->dds_blocks == 0)
904b24ab676SJeff Bonwick return;
905b24ab676SJeff Bonwick
906b24ab676SJeff Bonwick rL = (double)dds->dds_ref_lsize;
907b24ab676SJeff Bonwick rP = (double)dds->dds_ref_psize;
908b24ab676SJeff Bonwick rD = (double)dds->dds_ref_dsize;
909b24ab676SJeff Bonwick D = (double)dds->dds_dsize;
910b24ab676SJeff Bonwick
911b24ab676SJeff Bonwick dedup = rD / D;
912b24ab676SJeff Bonwick compress = rL / rP;
913b24ab676SJeff Bonwick copies = rD / rP;
914b24ab676SJeff Bonwick
915b24ab676SJeff Bonwick (void) printf("dedup = %.2f, compress = %.2f, copies = %.2f, "
916b24ab676SJeff Bonwick "dedup * compress / copies = %.2f\n\n",
917b24ab676SJeff Bonwick dedup, compress, copies, dedup * compress / copies);
918b24ab676SJeff Bonwick }
919b24ab676SJeff Bonwick
920b24ab676SJeff Bonwick static void
dump_ddt(ddt_t * ddt,enum ddt_type type,enum ddt_class class)921b24ab676SJeff Bonwick dump_ddt(ddt_t *ddt, enum ddt_type type, enum ddt_class class)
922b24ab676SJeff Bonwick {
923b24ab676SJeff Bonwick char name[DDT_NAMELEN];
924b24ab676SJeff Bonwick ddt_entry_t dde;
925b24ab676SJeff Bonwick uint64_t walk = 0;
926b24ab676SJeff Bonwick dmu_object_info_t doi;
927b24ab676SJeff Bonwick uint64_t count, dspace, mspace;
928b24ab676SJeff Bonwick int error;
929b24ab676SJeff Bonwick
930b24ab676SJeff Bonwick error = ddt_object_info(ddt, type, class, &doi);
931b24ab676SJeff Bonwick
932b24ab676SJeff Bonwick if (error == ENOENT)
933b24ab676SJeff Bonwick return;
934b24ab676SJeff Bonwick ASSERT(error == 0);
935b24ab676SJeff Bonwick
9367448a079SGeorge Wilson if ((count = ddt_object_count(ddt, type, class)) == 0)
9377448a079SGeorge Wilson return;
9387448a079SGeorge Wilson
939b24ab676SJeff Bonwick dspace = doi.doi_physical_blocks_512 << 9;
940b24ab676SJeff Bonwick mspace = doi.doi_fill_count * doi.doi_data_block_size;
941b24ab676SJeff Bonwick
942b24ab676SJeff Bonwick ddt_object_name(ddt, type, class, name);
943b24ab676SJeff Bonwick
944b24ab676SJeff Bonwick (void) printf("%s: %llu entries, size %llu on disk, %llu in core\n",
945b24ab676SJeff Bonwick name,
946b24ab676SJeff Bonwick (u_longlong_t)count,
947b24ab676SJeff Bonwick (u_longlong_t)(dspace / count),
948b24ab676SJeff Bonwick (u_longlong_t)(mspace / count));
949b24ab676SJeff Bonwick
950b24ab676SJeff Bonwick if (dump_opt['D'] < 3)
951b24ab676SJeff Bonwick return;
952b24ab676SJeff Bonwick
9539eb19f4dSGeorge Wilson zpool_dump_ddt(NULL, &ddt->ddt_histogram[type][class]);
954b24ab676SJeff Bonwick
955b24ab676SJeff Bonwick if (dump_opt['D'] < 4)
956b24ab676SJeff Bonwick return;
957b24ab676SJeff Bonwick
958b24ab676SJeff Bonwick if (dump_opt['D'] < 5 && class == DDT_CLASS_UNIQUE)
959b24ab676SJeff Bonwick return;
960b24ab676SJeff Bonwick
961b24ab676SJeff Bonwick (void) printf("%s contents:\n\n", name);
962b24ab676SJeff Bonwick
963bbfd46c4SJeff Bonwick while ((error = ddt_object_walk(ddt, type, class, &walk, &dde)) == 0)
964b24ab676SJeff Bonwick dump_dde(ddt, &dde, walk);
965b24ab676SJeff Bonwick
966b24ab676SJeff Bonwick ASSERT(error == ENOENT);
967b24ab676SJeff Bonwick
968b24ab676SJeff Bonwick (void) printf("\n");
969b24ab676SJeff Bonwick }
970b24ab676SJeff Bonwick
971b24ab676SJeff Bonwick static void
dump_all_ddts(spa_t * spa)972b24ab676SJeff Bonwick dump_all_ddts(spa_t *spa)
973b24ab676SJeff Bonwick {
974b24ab676SJeff Bonwick ddt_histogram_t ddh_total = { 0 };
975b24ab676SJeff Bonwick ddt_stat_t dds_total = { 0 };
976b24ab676SJeff Bonwick
977b24ab676SJeff Bonwick for (enum zio_checksum c = 0; c < ZIO_CHECKSUM_FUNCTIONS; c++) {
978b24ab676SJeff Bonwick ddt_t *ddt = spa->spa_ddt[c];
979b24ab676SJeff Bonwick for (enum ddt_type type = 0; type < DDT_TYPES; type++) {
980b24ab676SJeff Bonwick for (enum ddt_class class = 0; class < DDT_CLASSES;
981b24ab676SJeff Bonwick class++) {
982b24ab676SJeff Bonwick dump_ddt(ddt, type, class);
983b24ab676SJeff Bonwick }
984b24ab676SJeff Bonwick }
985b24ab676SJeff Bonwick }
986b24ab676SJeff Bonwick
9879eb19f4dSGeorge Wilson ddt_get_dedup_stats(spa, &dds_total);
988b24ab676SJeff Bonwick
989b24ab676SJeff Bonwick if (dds_total.dds_blocks == 0) {
990b24ab676SJeff Bonwick (void) printf("All DDTs are empty\n");
991b24ab676SJeff Bonwick return;
992b24ab676SJeff Bonwick }
993b24ab676SJeff Bonwick
994b24ab676SJeff Bonwick (void) printf("\n");
995b24ab676SJeff Bonwick
996b24ab676SJeff Bonwick if (dump_opt['D'] > 1) {
997b24ab676SJeff Bonwick (void) printf("DDT histogram (aggregated over all DDTs):\n");
9989eb19f4dSGeorge Wilson ddt_get_dedup_histogram(spa, &ddh_total);
9999eb19f4dSGeorge Wilson zpool_dump_ddt(&dds_total, &ddh_total);
1000b24ab676SJeff Bonwick }
1001b24ab676SJeff Bonwick
1002b24ab676SJeff Bonwick dump_dedup_ratio(&dds_total);
1003b24ab676SJeff Bonwick }
1004b24ab676SJeff Bonwick
1005b24ab676SJeff Bonwick static void
dump_dtl_seg(void * arg,uint64_t start,uint64_t size)10060713e232SGeorge Wilson dump_dtl_seg(void *arg, uint64_t start, uint64_t size)
10078ad4d6ddSJeff Bonwick {
10080713e232SGeorge Wilson char *prefix = arg;
10098ad4d6ddSJeff Bonwick
10108ad4d6ddSJeff Bonwick (void) printf("%s [%llu,%llu) length %llu\n",
10118ad4d6ddSJeff Bonwick prefix,
10128ad4d6ddSJeff Bonwick (u_longlong_t)start,
10138ad4d6ddSJeff Bonwick (u_longlong_t)(start + size),
10148ad4d6ddSJeff Bonwick (u_longlong_t)(size));
10158ad4d6ddSJeff Bonwick }
10168ad4d6ddSJeff Bonwick
10178ad4d6ddSJeff Bonwick static void
dump_dtl(vdev_t * vd,int indent)1018fa9e4066Sahrens dump_dtl(vdev_t *vd, int indent)
1019fa9e4066Sahrens {
10208ad4d6ddSJeff Bonwick spa_t *spa = vd->vdev_spa;
10218ad4d6ddSJeff Bonwick boolean_t required;
10228ad4d6ddSJeff Bonwick char *name[DTL_TYPES] = { "missing", "partial", "scrub", "outage" };
10238ad4d6ddSJeff Bonwick char prefix[256];
10248ad4d6ddSJeff Bonwick
10258f18d1faSGeorge Wilson spa_vdev_state_enter(spa, SCL_NONE);
10268ad4d6ddSJeff Bonwick required = vdev_dtl_required(vd);
10278ad4d6ddSJeff Bonwick (void) spa_vdev_state_exit(spa, NULL, 0);
1028fa9e4066Sahrens
1029fa9e4066Sahrens if (indent == 0)
1030fa9e4066Sahrens (void) printf("\nDirty time logs:\n\n");
1031fa9e4066Sahrens
10328ad4d6ddSJeff Bonwick (void) printf("\t%*s%s [%s]\n", indent, "",
1033e14bb325SJeff Bonwick vd->vdev_path ? vd->vdev_path :
10348ad4d6ddSJeff Bonwick vd->vdev_parent ? vd->vdev_ops->vdev_op_type : spa_name(spa),
10358ad4d6ddSJeff Bonwick required ? "DTL-required" : "DTL-expendable");
1036fa9e4066Sahrens
10378ad4d6ddSJeff Bonwick for (int t = 0; t < DTL_TYPES; t++) {
10380713e232SGeorge Wilson range_tree_t *rt = vd->vdev_dtl[t];
10390713e232SGeorge Wilson if (range_tree_space(rt) == 0)
10408ad4d6ddSJeff Bonwick continue;
10418ad4d6ddSJeff Bonwick (void) snprintf(prefix, sizeof (prefix), "\t%*s%s",
10428ad4d6ddSJeff Bonwick indent + 2, "", name[t]);
10430713e232SGeorge Wilson mutex_enter(rt->rt_lock);
10440713e232SGeorge Wilson range_tree_walk(rt, dump_dtl_seg, prefix);
10450713e232SGeorge Wilson mutex_exit(rt->rt_lock);
10468ad4d6ddSJeff Bonwick if (dump_opt['d'] > 5 && vd->vdev_children == 0)
10470713e232SGeorge Wilson dump_spacemap(spa->spa_meta_objset, vd->vdev_dtl_sm);
1048fa9e4066Sahrens }
1049fa9e4066Sahrens
10508ad4d6ddSJeff Bonwick for (int c = 0; c < vd->vdev_children; c++)
1051fa9e4066Sahrens dump_dtl(vd->vdev_child[c], indent + 4);
1052fa9e4066Sahrens }
1053fa9e4066Sahrens
10548f18d1faSGeorge Wilson static void
dump_history(spa_t * spa)10558f18d1faSGeorge Wilson dump_history(spa_t *spa)
10568f18d1faSGeorge Wilson {
10578f18d1faSGeorge Wilson nvlist_t **events = NULL;
10588f18d1faSGeorge Wilson char buf[SPA_MAXBLOCKSIZE];
1059e4161df6SVictor Latushkin uint64_t resid, len, off = 0;
10608f18d1faSGeorge Wilson uint_t num = 0;
10618f18d1faSGeorge Wilson int error;
10628f18d1faSGeorge Wilson time_t tsec;
10638f18d1faSGeorge Wilson struct tm t;
10648f18d1faSGeorge Wilson char tbuf[30];
10658f18d1faSGeorge Wilson char internalstr[MAXPATHLEN];
10668f18d1faSGeorge Wilson
10678f18d1faSGeorge Wilson do {
1068e4161df6SVictor Latushkin len = sizeof (buf);
1069e4161df6SVictor Latushkin
10708f18d1faSGeorge Wilson if ((error = spa_history_get(spa, &off, &len, buf)) != 0) {
10718f18d1faSGeorge Wilson (void) fprintf(stderr, "Unable to read history: "
10728f18d1faSGeorge Wilson "error %d\n", error);
10738f18d1faSGeorge Wilson return;
10748f18d1faSGeorge Wilson }
10758f18d1faSGeorge Wilson
10768f18d1faSGeorge Wilson if (zpool_history_unpack(buf, len, &resid, &events, &num) != 0)
10778f18d1faSGeorge Wilson break;
10788f18d1faSGeorge Wilson
10798f18d1faSGeorge Wilson off -= resid;
10808f18d1faSGeorge Wilson } while (len != 0);
10818f18d1faSGeorge Wilson
10828f18d1faSGeorge Wilson (void) printf("\nHistory:\n");
10838f18d1faSGeorge Wilson for (int i = 0; i < num; i++) {
10848f18d1faSGeorge Wilson uint64_t time, txg, ievent;
10858f18d1faSGeorge Wilson char *cmd, *intstr;
10864445fffbSMatthew Ahrens boolean_t printed = B_FALSE;
10878f18d1faSGeorge Wilson
10888f18d1faSGeorge Wilson if (nvlist_lookup_uint64(events[i], ZPOOL_HIST_TIME,
10898f18d1faSGeorge Wilson &time) != 0)
10904445fffbSMatthew Ahrens goto next;
10918f18d1faSGeorge Wilson if (nvlist_lookup_string(events[i], ZPOOL_HIST_CMD,
10928f18d1faSGeorge Wilson &cmd) != 0) {
10938f18d1faSGeorge Wilson if (nvlist_lookup_uint64(events[i],
10948f18d1faSGeorge Wilson ZPOOL_HIST_INT_EVENT, &ievent) != 0)
10954445fffbSMatthew Ahrens goto next;
10968f18d1faSGeorge Wilson verify(nvlist_lookup_uint64(events[i],
10978f18d1faSGeorge Wilson ZPOOL_HIST_TXG, &txg) == 0);
10988f18d1faSGeorge Wilson verify(nvlist_lookup_string(events[i],
10998f18d1faSGeorge Wilson ZPOOL_HIST_INT_STR, &intstr) == 0);
11004445fffbSMatthew Ahrens if (ievent >= ZFS_NUM_LEGACY_HISTORY_EVENTS)
11014445fffbSMatthew Ahrens goto next;
11028f18d1faSGeorge Wilson
11038f18d1faSGeorge Wilson (void) snprintf(internalstr,
11048f18d1faSGeorge Wilson sizeof (internalstr),
11058f18d1faSGeorge Wilson "[internal %s txg:%lld] %s",
11063f9d6ad7SLin Ling zfs_history_event_names[ievent], txg,
11078f18d1faSGeorge Wilson intstr);
11088f18d1faSGeorge Wilson cmd = internalstr;
11098f18d1faSGeorge Wilson }
11108f18d1faSGeorge Wilson tsec = time;
11118f18d1faSGeorge Wilson (void) localtime_r(&tsec, &t);
11128f18d1faSGeorge Wilson (void) strftime(tbuf, sizeof (tbuf), "%F.%T", &t);
11138f18d1faSGeorge Wilson (void) printf("%s %s\n", tbuf, cmd);
11144445fffbSMatthew Ahrens printed = B_TRUE;
11154445fffbSMatthew Ahrens
11164445fffbSMatthew Ahrens next:
11174445fffbSMatthew Ahrens if (dump_opt['h'] > 1) {
11184445fffbSMatthew Ahrens if (!printed)
11194445fffbSMatthew Ahrens (void) printf("unrecognized record:\n");
11204445fffbSMatthew Ahrens dump_nvlist(events[i], 2);
11214445fffbSMatthew Ahrens }
11228f18d1faSGeorge Wilson }
11238f18d1faSGeorge Wilson }
11248f18d1faSGeorge Wilson
1125fa9e4066Sahrens /*ARGSUSED*/
1126fa9e4066Sahrens static void
dump_dnode(objset_t * os,uint64_t object,void * data,size_t size)1127fa9e4066Sahrens dump_dnode(objset_t *os, uint64_t object, void *data, size_t size)
1128fa9e4066Sahrens {
1129fa9e4066Sahrens }
1130fa9e4066Sahrens
1131fa9e4066Sahrens static uint64_t
blkid2offset(const dnode_phys_t * dnp,const blkptr_t * bp,const zbookmark_phys_t * zb)113205d95d03SMatthew Ahrens blkid2offset(const dnode_phys_t *dnp, const blkptr_t *bp,
113305d95d03SMatthew Ahrens const zbookmark_phys_t *zb)
1134fa9e4066Sahrens {
1135b24ab676SJeff Bonwick if (dnp == NULL) {
1136b24ab676SJeff Bonwick ASSERT(zb->zb_level < 0);
1137b24ab676SJeff Bonwick if (zb->zb_object == 0)
1138b24ab676SJeff Bonwick return (zb->zb_blkid);
1139b24ab676SJeff Bonwick return (zb->zb_blkid * BP_GET_LSIZE(bp));
1140b24ab676SJeff Bonwick }
1141fa9e4066Sahrens
1142b24ab676SJeff Bonwick ASSERT(zb->zb_level >= 0);
1143b24ab676SJeff Bonwick
1144b24ab676SJeff Bonwick return ((zb->zb_blkid <<
1145b24ab676SJeff Bonwick (zb->zb_level * (dnp->dn_indblkshift - SPA_BLKPTRSHIFT))) *
1146fa9e4066Sahrens dnp->dn_datablkszsec << SPA_MINBLOCKSHIFT);
1147fa9e4066Sahrens }
1148fa9e4066Sahrens
114944cd46caSbillm static void
snprintf_blkptr_compact(char * blkbuf,size_t buflen,const blkptr_t * bp)115049064978SMax Grossman snprintf_blkptr_compact(char *blkbuf, size_t buflen, const blkptr_t *bp)
115144cd46caSbillm {
1152cde58dbcSMatthew Ahrens const dva_t *dva = bp->blk_dva;
1153b24ab676SJeff Bonwick int ndvas = dump_opt['d'] > 5 ? BP_GET_NDVAS(bp) : 1;
1154b24ab676SJeff Bonwick
1155490d05b9SMatthew Ahrens if (dump_opt['b'] >= 6) {
115649064978SMax Grossman snprintf_blkptr(blkbuf, buflen, bp);
1157b24ab676SJeff Bonwick return;
1158b24ab676SJeff Bonwick }
115944cd46caSbillm
1160e54e0be9SMatthew Ahrens if (BP_IS_EMBEDDED(bp)) {
1161e54e0be9SMatthew Ahrens (void) sprintf(blkbuf,
1162e54e0be9SMatthew Ahrens "EMBEDDED et=%u %llxL/%llxP B=%llu",
1163e54e0be9SMatthew Ahrens (int)BPE_GET_ETYPE(bp),
1164e54e0be9SMatthew Ahrens (u_longlong_t)BPE_GET_LSIZE(bp),
1165e54e0be9SMatthew Ahrens (u_longlong_t)BPE_GET_PSIZE(bp),
1166e54e0be9SMatthew Ahrens (u_longlong_t)bp->blk_birth);
1167e54e0be9SMatthew Ahrens return;
1168e54e0be9SMatthew Ahrens }
116944cd46caSbillm
1170e54e0be9SMatthew Ahrens blkbuf[0] = '\0';
1171b24ab676SJeff Bonwick for (int i = 0; i < ndvas; i++)
117249064978SMax Grossman (void) snprintf(blkbuf + strlen(blkbuf),
117349064978SMax Grossman buflen - strlen(blkbuf), "%llu:%llx:%llx ",
117444cd46caSbillm (u_longlong_t)DVA_GET_VDEV(&dva[i]),
117544cd46caSbillm (u_longlong_t)DVA_GET_OFFSET(&dva[i]),
117644cd46caSbillm (u_longlong_t)DVA_GET_ASIZE(&dva[i]));
117744cd46caSbillm
117849064978SMax Grossman if (BP_IS_HOLE(bp)) {
117949064978SMax Grossman (void) snprintf(blkbuf + strlen(blkbuf),
118001eddf60SPrakash Surya buflen - strlen(blkbuf),
118101eddf60SPrakash Surya "%llxL B=%llu",
118201eddf60SPrakash Surya (u_longlong_t)BP_GET_LSIZE(bp),
118349064978SMax Grossman (u_longlong_t)bp->blk_birth);
118449064978SMax Grossman } else {
118549064978SMax Grossman (void) snprintf(blkbuf + strlen(blkbuf),
118649064978SMax Grossman buflen - strlen(blkbuf),
1187b24ab676SJeff Bonwick "%llxL/%llxP F=%llu B=%llu/%llu",
118844cd46caSbillm (u_longlong_t)BP_GET_LSIZE(bp),
118944cd46caSbillm (u_longlong_t)BP_GET_PSIZE(bp),
1190e54e0be9SMatthew Ahrens (u_longlong_t)BP_GET_FILL(bp),
1191b24ab676SJeff Bonwick (u_longlong_t)bp->blk_birth,
1192b24ab676SJeff Bonwick (u_longlong_t)BP_PHYSICAL_BIRTH(bp));
119344cd46caSbillm }
119449064978SMax Grossman }
119544cd46caSbillm
119688b7b0f2SMatthew Ahrens static void
print_indirect(blkptr_t * bp,const zbookmark_phys_t * zb,const dnode_phys_t * dnp)119705d95d03SMatthew Ahrens print_indirect(blkptr_t *bp, const zbookmark_phys_t *zb,
119888b7b0f2SMatthew Ahrens const dnode_phys_t *dnp)
1199fa9e4066Sahrens {
120088b7b0f2SMatthew Ahrens char blkbuf[BP_SPRINTF_LEN];
1201fa9e4066Sahrens int l;
1202fa9e4066Sahrens
1203e54e0be9SMatthew Ahrens if (!BP_IS_EMBEDDED(bp)) {
1204fa9e4066Sahrens ASSERT3U(BP_GET_TYPE(bp), ==, dnp->dn_type);
1205fa9e4066Sahrens ASSERT3U(BP_GET_LEVEL(bp), ==, zb->zb_level);
1206e54e0be9SMatthew Ahrens }
1207fa9e4066Sahrens
1208b24ab676SJeff Bonwick (void) printf("%16llx ", (u_longlong_t)blkid2offset(dnp, bp, zb));
1209fa9e4066Sahrens
1210fa9e4066Sahrens ASSERT(zb->zb_level >= 0);
1211fa9e4066Sahrens
1212fa9e4066Sahrens for (l = dnp->dn_nlevels - 1; l >= -1; l--) {
1213fa9e4066Sahrens if (l == zb->zb_level) {
121488b7b0f2SMatthew Ahrens (void) printf("L%llx", (u_longlong_t)zb->zb_level);
1215fa9e4066Sahrens } else {
121688b7b0f2SMatthew Ahrens (void) printf(" ");
1217fa9e4066Sahrens }
1218fa9e4066Sahrens }
1219fa9e4066Sahrens
122049064978SMax Grossman snprintf_blkptr_compact(blkbuf, sizeof (blkbuf), bp);
122144cd46caSbillm (void) printf("%s\n", blkbuf);
1222fa9e4066Sahrens }
1223fa9e4066Sahrens
122488b7b0f2SMatthew Ahrens static int
visit_indirect(spa_t * spa,const dnode_phys_t * dnp,blkptr_t * bp,const zbookmark_phys_t * zb)122588b7b0f2SMatthew Ahrens visit_indirect(spa_t *spa, const dnode_phys_t *dnp,
122605d95d03SMatthew Ahrens blkptr_t *bp, const zbookmark_phys_t *zb)
122788b7b0f2SMatthew Ahrens {
1228e4161df6SVictor Latushkin int err = 0;
122988b7b0f2SMatthew Ahrens
123088b7b0f2SMatthew Ahrens if (bp->blk_birth == 0)
123188b7b0f2SMatthew Ahrens return (0);
123288b7b0f2SMatthew Ahrens
123388b7b0f2SMatthew Ahrens print_indirect(bp, zb, dnp);
123488b7b0f2SMatthew Ahrens
123549064978SMax Grossman if (BP_GET_LEVEL(bp) > 0 && !BP_IS_HOLE(bp)) {
123653c4e354SGeorge Wilson arc_flags_t flags = ARC_FLAG_WAIT;
123788b7b0f2SMatthew Ahrens int i;
123888b7b0f2SMatthew Ahrens blkptr_t *cbp;
123988b7b0f2SMatthew Ahrens int epb = BP_GET_LSIZE(bp) >> SPA_BLKPTRSHIFT;
124088b7b0f2SMatthew Ahrens arc_buf_t *buf;
124188b7b0f2SMatthew Ahrens uint64_t fill = 0;
124288b7b0f2SMatthew Ahrens
12431b912ec7SGeorge Wilson err = arc_read(NULL, spa, bp, arc_getbuf_func, &buf,
124488b7b0f2SMatthew Ahrens ZIO_PRIORITY_ASYNC_READ, ZIO_FLAG_CANFAIL, &flags, zb);
124588b7b0f2SMatthew Ahrens if (err)
124688b7b0f2SMatthew Ahrens return (err);
12473f9d6ad7SLin Ling ASSERT(buf->b_data);
124888b7b0f2SMatthew Ahrens
124988b7b0f2SMatthew Ahrens /* recursively visit blocks below this */
125088b7b0f2SMatthew Ahrens cbp = buf->b_data;
125188b7b0f2SMatthew Ahrens for (i = 0; i < epb; i++, cbp++) {
125205d95d03SMatthew Ahrens zbookmark_phys_t czb;
125388b7b0f2SMatthew Ahrens
125488b7b0f2SMatthew Ahrens SET_BOOKMARK(&czb, zb->zb_objset, zb->zb_object,
125588b7b0f2SMatthew Ahrens zb->zb_level - 1,
125688b7b0f2SMatthew Ahrens zb->zb_blkid * epb + i);
125788b7b0f2SMatthew Ahrens err = visit_indirect(spa, dnp, cbp, &czb);
125888b7b0f2SMatthew Ahrens if (err)
125988b7b0f2SMatthew Ahrens break;
1260e54e0be9SMatthew Ahrens fill += BP_GET_FILL(cbp);
126188b7b0f2SMatthew Ahrens }
12628ad4d6ddSJeff Bonwick if (!err)
1263e54e0be9SMatthew Ahrens ASSERT3U(fill, ==, BP_GET_FILL(bp));
126488b7b0f2SMatthew Ahrens (void) arc_buf_remove_ref(buf, &buf);
126588b7b0f2SMatthew Ahrens }
126688b7b0f2SMatthew Ahrens
126788b7b0f2SMatthew Ahrens return (err);
1268fa9e4066Sahrens }
1269fa9e4066Sahrens
1270fa9e4066Sahrens /*ARGSUSED*/
1271fa9e4066Sahrens static void
dump_indirect(dnode_t * dn)127288b7b0f2SMatthew Ahrens dump_indirect(dnode_t *dn)
1273fa9e4066Sahrens {
127488b7b0f2SMatthew Ahrens dnode_phys_t *dnp = dn->dn_phys;
127588b7b0f2SMatthew Ahrens int j;
127605d95d03SMatthew Ahrens zbookmark_phys_t czb;
1277fa9e4066Sahrens
1278fa9e4066Sahrens (void) printf("Indirect blocks:\n");
1279fa9e4066Sahrens
1280503ad85cSMatthew Ahrens SET_BOOKMARK(&czb, dmu_objset_id(dn->dn_objset),
128188b7b0f2SMatthew Ahrens dn->dn_object, dnp->dn_nlevels - 1, 0);
128288b7b0f2SMatthew Ahrens for (j = 0; j < dnp->dn_nblkptr; j++) {
128388b7b0f2SMatthew Ahrens czb.zb_blkid = j;
1284503ad85cSMatthew Ahrens (void) visit_indirect(dmu_objset_spa(dn->dn_objset), dnp,
128588b7b0f2SMatthew Ahrens &dnp->dn_blkptr[j], &czb);
128688b7b0f2SMatthew Ahrens }
1287fa9e4066Sahrens
1288fa9e4066Sahrens (void) printf("\n");
1289fa9e4066Sahrens }
1290fa9e4066Sahrens
1291fa9e4066Sahrens /*ARGSUSED*/
1292fa9e4066Sahrens static void
dump_dsl_dir(objset_t * os,uint64_t object,void * data,size_t size)1293fa9e4066Sahrens dump_dsl_dir(objset_t *os, uint64_t object, void *data, size_t size)
1294fa9e4066Sahrens {
1295fa9e4066Sahrens dsl_dir_phys_t *dd = data;
1296fa9e4066Sahrens time_t crtime;
12973f9d6ad7SLin Ling char nice[32];
1298fa9e4066Sahrens
1299fa9e4066Sahrens if (dd == NULL)
1300fa9e4066Sahrens return;
1301fa9e4066Sahrens
1302da6c28aaSamw ASSERT3U(size, >=, sizeof (dsl_dir_phys_t));
1303fa9e4066Sahrens
1304fa9e4066Sahrens crtime = dd->dd_creation_time;
1305fa9e4066Sahrens (void) printf("\t\tcreation_time = %s", ctime(&crtime));
1306fa9e4066Sahrens (void) printf("\t\thead_dataset_obj = %llu\n",
1307fa9e4066Sahrens (u_longlong_t)dd->dd_head_dataset_obj);
1308fa9e4066Sahrens (void) printf("\t\tparent_dir_obj = %llu\n",
1309fa9e4066Sahrens (u_longlong_t)dd->dd_parent_obj);
13103cb34c60Sahrens (void) printf("\t\torigin_obj = %llu\n",
13113cb34c60Sahrens (u_longlong_t)dd->dd_origin_obj);
1312fa9e4066Sahrens (void) printf("\t\tchild_dir_zapobj = %llu\n",
1313fa9e4066Sahrens (u_longlong_t)dd->dd_child_dir_zapobj);
13143f9d6ad7SLin Ling zdb_nicenum(dd->dd_used_bytes, nice);
131574e7dc98SMatthew Ahrens (void) printf("\t\tused_bytes = %s\n", nice);
13163f9d6ad7SLin Ling zdb_nicenum(dd->dd_compressed_bytes, nice);
131774e7dc98SMatthew Ahrens (void) printf("\t\tcompressed_bytes = %s\n", nice);
13183f9d6ad7SLin Ling zdb_nicenum(dd->dd_uncompressed_bytes, nice);
131974e7dc98SMatthew Ahrens (void) printf("\t\tuncompressed_bytes = %s\n", nice);
13203f9d6ad7SLin Ling zdb_nicenum(dd->dd_quota, nice);
132174e7dc98SMatthew Ahrens (void) printf("\t\tquota = %s\n", nice);
13223f9d6ad7SLin Ling zdb_nicenum(dd->dd_reserved, nice);
132374e7dc98SMatthew Ahrens (void) printf("\t\treserved = %s\n", nice);
1324fa9e4066Sahrens (void) printf("\t\tprops_zapobj = %llu\n",
1325fa9e4066Sahrens (u_longlong_t)dd->dd_props_zapobj);
1326ecd6cf80Smarks (void) printf("\t\tdeleg_zapobj = %llu\n",
1327ecd6cf80Smarks (u_longlong_t)dd->dd_deleg_zapobj);
132874e7dc98SMatthew Ahrens (void) printf("\t\tflags = %llx\n",
132974e7dc98SMatthew Ahrens (u_longlong_t)dd->dd_flags);
133074e7dc98SMatthew Ahrens
133174e7dc98SMatthew Ahrens #define DO(which) \
13323f9d6ad7SLin Ling zdb_nicenum(dd->dd_used_breakdown[DD_USED_ ## which], nice); \
133374e7dc98SMatthew Ahrens (void) printf("\t\tused_breakdown[" #which "] = %s\n", nice)
133474e7dc98SMatthew Ahrens DO(HEAD);
133574e7dc98SMatthew Ahrens DO(SNAP);
133674e7dc98SMatthew Ahrens DO(CHILD);
133774e7dc98SMatthew Ahrens DO(CHILD_RSRV);
133874e7dc98SMatthew Ahrens DO(REFRSRV);
133974e7dc98SMatthew Ahrens #undef DO
1340fa9e4066Sahrens }
1341fa9e4066Sahrens
1342fa9e4066Sahrens /*ARGSUSED*/
1343fa9e4066Sahrens static void
dump_dsl_dataset(objset_t * os,uint64_t object,void * data,size_t size)1344fa9e4066Sahrens dump_dsl_dataset(objset_t *os, uint64_t object, void *data, size_t size)
1345fa9e4066Sahrens {
1346fa9e4066Sahrens dsl_dataset_phys_t *ds = data;
1347fa9e4066Sahrens time_t crtime;
13483f9d6ad7SLin Ling char used[32], compressed[32], uncompressed[32], unique[32];
1349fbabab8fSmaybee char blkbuf[BP_SPRINTF_LEN];
1350fa9e4066Sahrens
1351fa9e4066Sahrens if (ds == NULL)
1352fa9e4066Sahrens return;
1353fa9e4066Sahrens
1354fa9e4066Sahrens ASSERT(size == sizeof (*ds));
1355fa9e4066Sahrens crtime = ds->ds_creation_time;
1356ad135b5dSChristopher Siden zdb_nicenum(ds->ds_referenced_bytes, used);
13573f9d6ad7SLin Ling zdb_nicenum(ds->ds_compressed_bytes, compressed);
13583f9d6ad7SLin Ling zdb_nicenum(ds->ds_uncompressed_bytes, uncompressed);
13593f9d6ad7SLin Ling zdb_nicenum(ds->ds_unique_bytes, unique);
136049064978SMax Grossman snprintf_blkptr(blkbuf, sizeof (blkbuf), &ds->ds_bp);
1361fa9e4066Sahrens
1362088f3894Sahrens (void) printf("\t\tdir_obj = %llu\n",
1363fa9e4066Sahrens (u_longlong_t)ds->ds_dir_obj);
1364fa9e4066Sahrens (void) printf("\t\tprev_snap_obj = %llu\n",
1365fa9e4066Sahrens (u_longlong_t)ds->ds_prev_snap_obj);
1366fa9e4066Sahrens (void) printf("\t\tprev_snap_txg = %llu\n",
1367fa9e4066Sahrens (u_longlong_t)ds->ds_prev_snap_txg);
1368fa9e4066Sahrens (void) printf("\t\tnext_snap_obj = %llu\n",
1369fa9e4066Sahrens (u_longlong_t)ds->ds_next_snap_obj);
1370fa9e4066Sahrens (void) printf("\t\tsnapnames_zapobj = %llu\n",
1371fa9e4066Sahrens (u_longlong_t)ds->ds_snapnames_zapobj);
1372fa9e4066Sahrens (void) printf("\t\tnum_children = %llu\n",
1373fa9e4066Sahrens (u_longlong_t)ds->ds_num_children);
1374842727c2SChris Kirby (void) printf("\t\tuserrefs_obj = %llu\n",
1375842727c2SChris Kirby (u_longlong_t)ds->ds_userrefs_obj);
1376fa9e4066Sahrens (void) printf("\t\tcreation_time = %s", ctime(&crtime));
1377fa9e4066Sahrens (void) printf("\t\tcreation_txg = %llu\n",
1378fa9e4066Sahrens (u_longlong_t)ds->ds_creation_txg);
1379fa9e4066Sahrens (void) printf("\t\tdeadlist_obj = %llu\n",
1380fa9e4066Sahrens (u_longlong_t)ds->ds_deadlist_obj);
1381fa9e4066Sahrens (void) printf("\t\tused_bytes = %s\n", used);
1382fa9e4066Sahrens (void) printf("\t\tcompressed_bytes = %s\n", compressed);
1383fa9e4066Sahrens (void) printf("\t\tuncompressed_bytes = %s\n", uncompressed);
1384fa9e4066Sahrens (void) printf("\t\tunique = %s\n", unique);
1385fa9e4066Sahrens (void) printf("\t\tfsid_guid = %llu\n",
1386fa9e4066Sahrens (u_longlong_t)ds->ds_fsid_guid);
1387fa9e4066Sahrens (void) printf("\t\tguid = %llu\n",
1388fa9e4066Sahrens (u_longlong_t)ds->ds_guid);
138999653d4eSeschrock (void) printf("\t\tflags = %llx\n",
139099653d4eSeschrock (u_longlong_t)ds->ds_flags);
1391088f3894Sahrens (void) printf("\t\tnext_clones_obj = %llu\n",
1392088f3894Sahrens (u_longlong_t)ds->ds_next_clones_obj);
1393bb0ade09Sahrens (void) printf("\t\tprops_obj = %llu\n",
1394bb0ade09Sahrens (u_longlong_t)ds->ds_props_obj);
1395fa9e4066Sahrens (void) printf("\t\tbp = %s\n", blkbuf);
1396fa9e4066Sahrens }
1397fa9e4066Sahrens
1398cde58dbcSMatthew Ahrens /* ARGSUSED */
1399cde58dbcSMatthew Ahrens static int
dump_bptree_cb(void * arg,const blkptr_t * bp,dmu_tx_t * tx)1400ad135b5dSChristopher Siden dump_bptree_cb(void *arg, const blkptr_t *bp, dmu_tx_t *tx)
1401ad135b5dSChristopher Siden {
1402ad135b5dSChristopher Siden char blkbuf[BP_SPRINTF_LEN];
1403ad135b5dSChristopher Siden
1404ad135b5dSChristopher Siden if (bp->blk_birth != 0) {
140549064978SMax Grossman snprintf_blkptr(blkbuf, sizeof (blkbuf), bp);
1406ad135b5dSChristopher Siden (void) printf("\t%s\n", blkbuf);
1407ad135b5dSChristopher Siden }
1408ad135b5dSChristopher Siden return (0);
1409ad135b5dSChristopher Siden }
1410ad135b5dSChristopher Siden
1411ad135b5dSChristopher Siden static void
dump_bptree(objset_t * os,uint64_t obj,char * name)1412ad135b5dSChristopher Siden dump_bptree(objset_t *os, uint64_t obj, char *name)
1413ad135b5dSChristopher Siden {
1414ad135b5dSChristopher Siden char bytes[32];
1415ad135b5dSChristopher Siden bptree_phys_t *bt;
1416ad135b5dSChristopher Siden dmu_buf_t *db;
1417ad135b5dSChristopher Siden
1418ad135b5dSChristopher Siden if (dump_opt['d'] < 3)
1419ad135b5dSChristopher Siden return;
1420ad135b5dSChristopher Siden
1421b420f3adSRichard Lowe VERIFY3U(0, ==, dmu_bonus_hold(os, obj, FTAG, &db));
1422ad135b5dSChristopher Siden bt = db->db_data;
1423ad135b5dSChristopher Siden zdb_nicenum(bt->bt_bytes, bytes);
1424ad135b5dSChristopher Siden (void) printf("\n %s: %llu datasets, %s\n",
1425ad135b5dSChristopher Siden name, (unsigned long long)(bt->bt_end - bt->bt_begin), bytes);
1426ad135b5dSChristopher Siden dmu_buf_rele(db, FTAG);
1427ad135b5dSChristopher Siden
1428ad135b5dSChristopher Siden if (dump_opt['d'] < 5)
1429ad135b5dSChristopher Siden return;
1430ad135b5dSChristopher Siden
1431ad135b5dSChristopher Siden (void) printf("\n");
1432ad135b5dSChristopher Siden
1433ad135b5dSChristopher Siden (void) bptree_iterate(os, obj, B_FALSE, dump_bptree_cb, NULL, NULL);
1434ad135b5dSChristopher Siden }
1435ad135b5dSChristopher Siden
1436ad135b5dSChristopher Siden /* ARGSUSED */
1437ad135b5dSChristopher Siden static int
dump_bpobj_cb(void * arg,const blkptr_t * bp,dmu_tx_t * tx)1438cde58dbcSMatthew Ahrens dump_bpobj_cb(void *arg, const blkptr_t *bp, dmu_tx_t *tx)
1439fa9e4066Sahrens {
1440cde58dbcSMatthew Ahrens char blkbuf[BP_SPRINTF_LEN];
1441cde58dbcSMatthew Ahrens
1442cde58dbcSMatthew Ahrens ASSERT(bp->blk_birth != 0);
144349064978SMax Grossman snprintf_blkptr_compact(blkbuf, sizeof (blkbuf), bp);
1444cde58dbcSMatthew Ahrens (void) printf("\t%s\n", blkbuf);
1445cde58dbcSMatthew Ahrens return (0);
1446cde58dbcSMatthew Ahrens }
1447cde58dbcSMatthew Ahrens
1448cde58dbcSMatthew Ahrens static void
dump_full_bpobj(bpobj_t * bpo,char * name,int indent)1449bf263f2aSMatthew Ahrens dump_full_bpobj(bpobj_t *bpo, char *name, int indent)
1450cde58dbcSMatthew Ahrens {
14513f9d6ad7SLin Ling char bytes[32];
14523f9d6ad7SLin Ling char comp[32];
14533f9d6ad7SLin Ling char uncomp[32];
1454fa9e4066Sahrens
1455fa9e4066Sahrens if (dump_opt['d'] < 3)
1456fa9e4066Sahrens return;
1457fa9e4066Sahrens
1458cde58dbcSMatthew Ahrens zdb_nicenum(bpo->bpo_phys->bpo_bytes, bytes);
1459d0475637SMatthew Ahrens if (bpo->bpo_havesubobj && bpo->bpo_phys->bpo_subobjs != 0) {
1460cde58dbcSMatthew Ahrens zdb_nicenum(bpo->bpo_phys->bpo_comp, comp);
1461cde58dbcSMatthew Ahrens zdb_nicenum(bpo->bpo_phys->bpo_uncomp, uncomp);
1462d0475637SMatthew Ahrens (void) printf(" %*s: object %llu, %llu local blkptrs, "
1463bf263f2aSMatthew Ahrens "%llu subobjs in object %llu, %s (%s/%s comp)\n",
1464d0475637SMatthew Ahrens indent * 8, name,
1465d0475637SMatthew Ahrens (u_longlong_t)bpo->bpo_object,
1466d0475637SMatthew Ahrens (u_longlong_t)bpo->bpo_phys->bpo_num_blkptrs,
1467cde58dbcSMatthew Ahrens (u_longlong_t)bpo->bpo_phys->bpo_num_subobjs,
1468bf263f2aSMatthew Ahrens (u_longlong_t)bpo->bpo_phys->bpo_subobjs,
146999653d4eSeschrock bytes, comp, uncomp);
1470d0475637SMatthew Ahrens
1471d0475637SMatthew Ahrens for (uint64_t i = 0; i < bpo->bpo_phys->bpo_num_subobjs; i++) {
1472d0475637SMatthew Ahrens uint64_t subobj;
1473d0475637SMatthew Ahrens bpobj_t subbpo;
1474d0475637SMatthew Ahrens int error;
1475d0475637SMatthew Ahrens VERIFY0(dmu_read(bpo->bpo_os,
1476d0475637SMatthew Ahrens bpo->bpo_phys->bpo_subobjs,
1477d0475637SMatthew Ahrens i * sizeof (subobj), sizeof (subobj), &subobj, 0));
1478d0475637SMatthew Ahrens error = bpobj_open(&subbpo, bpo->bpo_os, subobj);
1479d0475637SMatthew Ahrens if (error != 0) {
1480d0475637SMatthew Ahrens (void) printf("ERROR %u while trying to open "
1481d0475637SMatthew Ahrens "subobj id %llu\n",
1482d0475637SMatthew Ahrens error, (u_longlong_t)subobj);
1483d0475637SMatthew Ahrens continue;
1484d0475637SMatthew Ahrens }
1485bf263f2aSMatthew Ahrens dump_full_bpobj(&subbpo, "subobj", indent + 1);
148677061867SMatthew Ahrens bpobj_close(&subbpo);
1487d0475637SMatthew Ahrens }
148899653d4eSeschrock } else {
1489d0475637SMatthew Ahrens (void) printf(" %*s: object %llu, %llu blkptrs, %s\n",
1490d0475637SMatthew Ahrens indent * 8, name,
1491d0475637SMatthew Ahrens (u_longlong_t)bpo->bpo_object,
1492d0475637SMatthew Ahrens (u_longlong_t)bpo->bpo_phys->bpo_num_blkptrs,
1493d0475637SMatthew Ahrens bytes);
149499653d4eSeschrock }
1495fa9e4066Sahrens
1496cde58dbcSMatthew Ahrens if (dump_opt['d'] < 5)
1497fa9e4066Sahrens return;
1498fa9e4066Sahrens
1499fa9e4066Sahrens
1500d0475637SMatthew Ahrens if (indent == 0) {
1501cde58dbcSMatthew Ahrens (void) bpobj_iterate_nofree(bpo, dump_bpobj_cb, NULL, NULL);
1502d0475637SMatthew Ahrens (void) printf("\n");
1503d0475637SMatthew Ahrens }
1504fa9e4066Sahrens }
1505fa9e4066Sahrens
1506cde58dbcSMatthew Ahrens static void
dump_deadlist(dsl_deadlist_t * dl)1507cde58dbcSMatthew Ahrens dump_deadlist(dsl_deadlist_t *dl)
1508cde58dbcSMatthew Ahrens {
1509cde58dbcSMatthew Ahrens dsl_deadlist_entry_t *dle;
1510d0475637SMatthew Ahrens uint64_t unused;
1511cde58dbcSMatthew Ahrens char bytes[32];
1512cde58dbcSMatthew Ahrens char comp[32];
1513cde58dbcSMatthew Ahrens char uncomp[32];
1514cde58dbcSMatthew Ahrens
1515cde58dbcSMatthew Ahrens if (dump_opt['d'] < 3)
1516cde58dbcSMatthew Ahrens return;
1517cde58dbcSMatthew Ahrens
1518336201faSMatthew Ahrens if (dl->dl_oldfmt) {
1519bf263f2aSMatthew Ahrens dump_full_bpobj(&dl->dl_bpobj, "old-format deadlist", 0);
1520336201faSMatthew Ahrens return;
1521336201faSMatthew Ahrens }
1522336201faSMatthew Ahrens
1523cde58dbcSMatthew Ahrens zdb_nicenum(dl->dl_phys->dl_used, bytes);
1524cde58dbcSMatthew Ahrens zdb_nicenum(dl->dl_phys->dl_comp, comp);
1525cde58dbcSMatthew Ahrens zdb_nicenum(dl->dl_phys->dl_uncomp, uncomp);
1526cde58dbcSMatthew Ahrens (void) printf("\n Deadlist: %s (%s/%s comp)\n",
1527cde58dbcSMatthew Ahrens bytes, comp, uncomp);
1528cde58dbcSMatthew Ahrens
1529cde58dbcSMatthew Ahrens if (dump_opt['d'] < 4)
1530cde58dbcSMatthew Ahrens return;
1531cde58dbcSMatthew Ahrens
1532cde58dbcSMatthew Ahrens (void) printf("\n");
1533cde58dbcSMatthew Ahrens
1534d0475637SMatthew Ahrens /* force the tree to be loaded */
1535d0475637SMatthew Ahrens dsl_deadlist_space_range(dl, 0, UINT64_MAX, &unused, &unused, &unused);
1536d0475637SMatthew Ahrens
1537cde58dbcSMatthew Ahrens for (dle = avl_first(&dl->dl_tree); dle;
1538cde58dbcSMatthew Ahrens dle = AVL_NEXT(&dl->dl_tree, dle)) {
1539d0475637SMatthew Ahrens if (dump_opt['d'] >= 5) {
1540d0475637SMatthew Ahrens char buf[128];
1541d0475637SMatthew Ahrens (void) snprintf(buf, sizeof (buf), "mintxg %llu -> ",
1542d0475637SMatthew Ahrens (longlong_t)dle->dle_mintxg,
1543d0475637SMatthew Ahrens (longlong_t)dle->dle_bpobj.bpo_object);
1544d0475637SMatthew Ahrens
1545bf263f2aSMatthew Ahrens dump_full_bpobj(&dle->dle_bpobj, buf, 0);
1546d0475637SMatthew Ahrens } else {
1547cde58dbcSMatthew Ahrens (void) printf("mintxg %llu -> obj %llu\n",
1548cde58dbcSMatthew Ahrens (longlong_t)dle->dle_mintxg,
1549cde58dbcSMatthew Ahrens (longlong_t)dle->dle_bpobj.bpo_object);
1550cde58dbcSMatthew Ahrens
1551d0475637SMatthew Ahrens }
1552cde58dbcSMatthew Ahrens }
1553fa9e4066Sahrens }
1554fa9e4066Sahrens
1555e0d35c44Smarks static avl_tree_t idx_tree;
1556e0d35c44Smarks static avl_tree_t domain_tree;
1557e0d35c44Smarks static boolean_t fuid_table_loaded;
15580a586ceaSMark Shellenbaum static boolean_t sa_loaded;
15590a586ceaSMark Shellenbaum sa_attr_type_t *sa_attr_table;
1560e0d35c44Smarks
1561e0d35c44Smarks static void
fuid_table_destroy()1562e0d35c44Smarks fuid_table_destroy()
1563e0d35c44Smarks {
1564e0d35c44Smarks if (fuid_table_loaded) {
1565e0d35c44Smarks zfs_fuid_table_destroy(&idx_tree, &domain_tree);
1566e0d35c44Smarks fuid_table_loaded = B_FALSE;
1567e0d35c44Smarks }
1568e0d35c44Smarks }
1569e0d35c44Smarks
1570e0d35c44Smarks /*
1571e0d35c44Smarks * print uid or gid information.
1572e0d35c44Smarks * For normal POSIX id just the id is printed in decimal format.
1573e0d35c44Smarks * For CIFS files with FUID the fuid is printed in hex followed by
1574d0475637SMatthew Ahrens * the domain-rid string.
1575e0d35c44Smarks */
1576e0d35c44Smarks static void
print_idstr(uint64_t id,const char * id_type)1577e0d35c44Smarks print_idstr(uint64_t id, const char *id_type)
1578e0d35c44Smarks {
1579e0d35c44Smarks if (FUID_INDEX(id)) {
1580e0d35c44Smarks char *domain;
1581e0d35c44Smarks
1582e0d35c44Smarks domain = zfs_fuid_idx_domain(&idx_tree, FUID_INDEX(id));
1583e0d35c44Smarks (void) printf("\t%s %llx [%s-%d]\n", id_type,
1584e0d35c44Smarks (u_longlong_t)id, domain, (int)FUID_RID(id));
1585e0d35c44Smarks } else {
1586e0d35c44Smarks (void) printf("\t%s %llu\n", id_type, (u_longlong_t)id);
1587e0d35c44Smarks }
1588e0d35c44Smarks
1589e0d35c44Smarks }
1590e0d35c44Smarks
1591e0d35c44Smarks static void
dump_uidgid(objset_t * os,uint64_t uid,uint64_t gid)15920a586ceaSMark Shellenbaum dump_uidgid(objset_t *os, uint64_t uid, uint64_t gid)
1593e0d35c44Smarks {
1594e0d35c44Smarks uint32_t uid_idx, gid_idx;
1595e0d35c44Smarks
15960a586ceaSMark Shellenbaum uid_idx = FUID_INDEX(uid);
15970a586ceaSMark Shellenbaum gid_idx = FUID_INDEX(gid);
1598e0d35c44Smarks
1599e0d35c44Smarks /* Load domain table, if not already loaded */
1600e0d35c44Smarks if (!fuid_table_loaded && (uid_idx || gid_idx)) {
1601e0d35c44Smarks uint64_t fuid_obj;
1602e0d35c44Smarks
1603e0d35c44Smarks /* first find the fuid object. It lives in the master node */
1604e0d35c44Smarks VERIFY(zap_lookup(os, MASTER_NODE_OBJ, ZFS_FUID_TABLES,
1605e0d35c44Smarks 8, 1, &fuid_obj) == 0);
160689459e17SMark Shellenbaum zfs_fuid_avl_tree_create(&idx_tree, &domain_tree);
1607e0d35c44Smarks (void) zfs_fuid_table_load(os, fuid_obj,
1608e0d35c44Smarks &idx_tree, &domain_tree);
1609e0d35c44Smarks fuid_table_loaded = B_TRUE;
1610e0d35c44Smarks }
1611e0d35c44Smarks
16120a586ceaSMark Shellenbaum print_idstr(uid, "uid");
16130a586ceaSMark Shellenbaum print_idstr(gid, "gid");
1614e0d35c44Smarks }
1615e0d35c44Smarks
1616fa9e4066Sahrens /*ARGSUSED*/
1617fa9e4066Sahrens static void
dump_znode(objset_t * os,uint64_t object,void * data,size_t size)1618fa9e4066Sahrens dump_znode(objset_t *os, uint64_t object, void *data, size_t size)
1619fa9e4066Sahrens {
1620fa9e4066Sahrens char path[MAXPATHLEN * 2]; /* allow for xattr and failure prefix */
16210a586ceaSMark Shellenbaum sa_handle_t *hdl;
16220a586ceaSMark Shellenbaum uint64_t xattr, rdev, gen;
16230a586ceaSMark Shellenbaum uint64_t uid, gid, mode, fsize, parent, links;
16248f2529deSMark Shellenbaum uint64_t pflags;
16250a586ceaSMark Shellenbaum uint64_t acctm[2], modtm[2], chgtm[2], crtm[2];
16260a586ceaSMark Shellenbaum time_t z_crtime, z_atime, z_mtime, z_ctime;
16278f2529deSMark Shellenbaum sa_bulk_attr_t bulk[12];
16280a586ceaSMark Shellenbaum int idx = 0;
162955434c77Sek110237 int error;
1630fa9e4066Sahrens
16310a586ceaSMark Shellenbaum if (!sa_loaded) {
16320a586ceaSMark Shellenbaum uint64_t sa_attrs = 0;
16330a586ceaSMark Shellenbaum uint64_t version;
16340a586ceaSMark Shellenbaum
16350a586ceaSMark Shellenbaum VERIFY(zap_lookup(os, MASTER_NODE_OBJ, ZPL_VERSION_STR,
16360a586ceaSMark Shellenbaum 8, 1, &version) == 0);
16370a586ceaSMark Shellenbaum if (version >= ZPL_VERSION_SA) {
16380a586ceaSMark Shellenbaum VERIFY(zap_lookup(os, MASTER_NODE_OBJ, ZFS_SA_ATTRS,
16390a586ceaSMark Shellenbaum 8, 1, &sa_attrs) == 0);
16400a586ceaSMark Shellenbaum }
16411d8ccc7bSMark Shellenbaum if ((error = sa_setup(os, sa_attrs, zfs_attr_table,
16421d8ccc7bSMark Shellenbaum ZPL_END, &sa_attr_table)) != 0) {
16431d8ccc7bSMark Shellenbaum (void) printf("sa_setup failed errno %d, can't "
16441d8ccc7bSMark Shellenbaum "display znode contents\n", error);
16451d8ccc7bSMark Shellenbaum return;
16461d8ccc7bSMark Shellenbaum }
16470a586ceaSMark Shellenbaum sa_loaded = B_TRUE;
16480a586ceaSMark Shellenbaum }
16490a586ceaSMark Shellenbaum
16500a586ceaSMark Shellenbaum if (sa_handle_get(os, object, NULL, SA_HDL_PRIVATE, &hdl)) {
16510a586ceaSMark Shellenbaum (void) printf("Failed to get handle for SA znode\n");
16520a586ceaSMark Shellenbaum return;
16530a586ceaSMark Shellenbaum }
16540a586ceaSMark Shellenbaum
16550a586ceaSMark Shellenbaum SA_ADD_BULK_ATTR(bulk, idx, sa_attr_table[ZPL_UID], NULL, &uid, 8);
16560a586ceaSMark Shellenbaum SA_ADD_BULK_ATTR(bulk, idx, sa_attr_table[ZPL_GID], NULL, &gid, 8);
16570a586ceaSMark Shellenbaum SA_ADD_BULK_ATTR(bulk, idx, sa_attr_table[ZPL_LINKS], NULL,
16580a586ceaSMark Shellenbaum &links, 8);
16590a586ceaSMark Shellenbaum SA_ADD_BULK_ATTR(bulk, idx, sa_attr_table[ZPL_GEN], NULL, &gen, 8);
16600a586ceaSMark Shellenbaum SA_ADD_BULK_ATTR(bulk, idx, sa_attr_table[ZPL_MODE], NULL,
16610a586ceaSMark Shellenbaum &mode, 8);
16620a586ceaSMark Shellenbaum SA_ADD_BULK_ATTR(bulk, idx, sa_attr_table[ZPL_PARENT],
16630a586ceaSMark Shellenbaum NULL, &parent, 8);
16640a586ceaSMark Shellenbaum SA_ADD_BULK_ATTR(bulk, idx, sa_attr_table[ZPL_SIZE], NULL,
16650a586ceaSMark Shellenbaum &fsize, 8);
16660a586ceaSMark Shellenbaum SA_ADD_BULK_ATTR(bulk, idx, sa_attr_table[ZPL_ATIME], NULL,
16670a586ceaSMark Shellenbaum acctm, 16);
16680a586ceaSMark Shellenbaum SA_ADD_BULK_ATTR(bulk, idx, sa_attr_table[ZPL_MTIME], NULL,
16690a586ceaSMark Shellenbaum modtm, 16);
16700a586ceaSMark Shellenbaum SA_ADD_BULK_ATTR(bulk, idx, sa_attr_table[ZPL_CRTIME], NULL,
16710a586ceaSMark Shellenbaum crtm, 16);
16720a586ceaSMark Shellenbaum SA_ADD_BULK_ATTR(bulk, idx, sa_attr_table[ZPL_CTIME], NULL,
16730a586ceaSMark Shellenbaum chgtm, 16);
16748f2529deSMark Shellenbaum SA_ADD_BULK_ATTR(bulk, idx, sa_attr_table[ZPL_FLAGS], NULL,
16758f2529deSMark Shellenbaum &pflags, 8);
16760a586ceaSMark Shellenbaum
16770a586ceaSMark Shellenbaum if (sa_bulk_lookup(hdl, bulk, idx)) {
16780a586ceaSMark Shellenbaum (void) sa_handle_destroy(hdl);
16790a586ceaSMark Shellenbaum return;
16800a586ceaSMark Shellenbaum }
1681fa9e4066Sahrens
168255434c77Sek110237 error = zfs_obj_to_path(os, object, path, sizeof (path));
168355434c77Sek110237 if (error != 0) {
168455434c77Sek110237 (void) snprintf(path, sizeof (path), "\?\?\?<object#%llu>",
168555434c77Sek110237 (u_longlong_t)object);
168655434c77Sek110237 }
1687fa9e4066Sahrens if (dump_opt['d'] < 3) {
168855434c77Sek110237 (void) printf("\t%s\n", path);
16890a586ceaSMark Shellenbaum (void) sa_handle_destroy(hdl);
1690fa9e4066Sahrens return;
1691fa9e4066Sahrens }
1692fa9e4066Sahrens
16930a586ceaSMark Shellenbaum z_crtime = (time_t)crtm[0];
16940a586ceaSMark Shellenbaum z_atime = (time_t)acctm[0];
16950a586ceaSMark Shellenbaum z_mtime = (time_t)modtm[0];
16960a586ceaSMark Shellenbaum z_ctime = (time_t)chgtm[0];
1697fa9e4066Sahrens
169855434c77Sek110237 (void) printf("\tpath %s\n", path);
16990a586ceaSMark Shellenbaum dump_uidgid(os, uid, gid);
1700fa9e4066Sahrens (void) printf("\tatime %s", ctime(&z_atime));
1701fa9e4066Sahrens (void) printf("\tmtime %s", ctime(&z_mtime));
1702fa9e4066Sahrens (void) printf("\tctime %s", ctime(&z_ctime));
1703fa9e4066Sahrens (void) printf("\tcrtime %s", ctime(&z_crtime));
17040a586ceaSMark Shellenbaum (void) printf("\tgen %llu\n", (u_longlong_t)gen);
17050a586ceaSMark Shellenbaum (void) printf("\tmode %llo\n", (u_longlong_t)mode);
17060a586ceaSMark Shellenbaum (void) printf("\tsize %llu\n", (u_longlong_t)fsize);
17070a586ceaSMark Shellenbaum (void) printf("\tparent %llu\n", (u_longlong_t)parent);
17080a586ceaSMark Shellenbaum (void) printf("\tlinks %llu\n", (u_longlong_t)links);
17098f2529deSMark Shellenbaum (void) printf("\tpflags %llx\n", (u_longlong_t)pflags);
17100a586ceaSMark Shellenbaum if (sa_lookup(hdl, sa_attr_table[ZPL_XATTR], &xattr,
17110a586ceaSMark Shellenbaum sizeof (uint64_t)) == 0)
17120a586ceaSMark Shellenbaum (void) printf("\txattr %llu\n", (u_longlong_t)xattr);
17130a586ceaSMark Shellenbaum if (sa_lookup(hdl, sa_attr_table[ZPL_RDEV], &rdev,
17140a586ceaSMark Shellenbaum sizeof (uint64_t)) == 0)
17150a586ceaSMark Shellenbaum (void) printf("\trdev 0x%016llx\n", (u_longlong_t)rdev);
17160a586ceaSMark Shellenbaum sa_handle_destroy(hdl);
1717fa9e4066Sahrens }
1718fa9e4066Sahrens
1719fa9e4066Sahrens /*ARGSUSED*/
1720fa9e4066Sahrens static void
dump_acl(objset_t * os,uint64_t object,void * data,size_t size)1721fa9e4066Sahrens dump_acl(objset_t *os, uint64_t object, void *data, size_t size)
1722fa9e4066Sahrens {
1723fa9e4066Sahrens }
1724fa9e4066Sahrens
1725fa9e4066Sahrens /*ARGSUSED*/
1726fa9e4066Sahrens static void
dump_dmu_objset(objset_t * os,uint64_t object,void * data,size_t size)1727fa9e4066Sahrens dump_dmu_objset(objset_t *os, uint64_t object, void *data, size_t size)
1728fa9e4066Sahrens {
1729fa9e4066Sahrens }
1730fa9e4066Sahrens
17316de8f417SVictor Latushkin static object_viewer_t *object_viewer[DMU_OT_NUMTYPES + 1] = {
1732fa9e4066Sahrens dump_none, /* unallocated */
1733fa9e4066Sahrens dump_zap, /* object directory */
1734fa9e4066Sahrens dump_uint64, /* object array */
1735fa9e4066Sahrens dump_none, /* packed nvlist */
1736fa9e4066Sahrens dump_packed_nvlist, /* packed nvlist size */
1737bf263f2aSMatthew Ahrens dump_none, /* bpobj */
1738bf263f2aSMatthew Ahrens dump_bpobj, /* bpobj header */
1739fa9e4066Sahrens dump_none, /* SPA space map header */
1740fa9e4066Sahrens dump_none, /* SPA space map */
1741fa9e4066Sahrens dump_none, /* ZIL intent log */
1742fa9e4066Sahrens dump_dnode, /* DMU dnode */
1743fa9e4066Sahrens dump_dmu_objset, /* DMU objset */
1744fa9e4066Sahrens dump_dsl_dir, /* DSL directory */
1745fa9e4066Sahrens dump_zap, /* DSL directory child map */
1746fa9e4066Sahrens dump_zap, /* DSL dataset snap map */
1747fa9e4066Sahrens dump_zap, /* DSL props */
1748fa9e4066Sahrens dump_dsl_dataset, /* DSL dataset */
1749fa9e4066Sahrens dump_znode, /* ZFS znode */
1750da6c28aaSamw dump_acl, /* ZFS V0 ACL */
1751fa9e4066Sahrens dump_uint8, /* ZFS plain file */
1752e7437265Sahrens dump_zpldir, /* ZFS directory */
1753fa9e4066Sahrens dump_zap, /* ZFS master node */
1754fa9e4066Sahrens dump_zap, /* ZFS delete queue */
1755fa9e4066Sahrens dump_uint8, /* zvol object */
1756fa9e4066Sahrens dump_zap, /* zvol prop */
1757fa9e4066Sahrens dump_uint8, /* other uint8[] */
1758fa9e4066Sahrens dump_uint64, /* other uint64[] */
1759fa9e4066Sahrens dump_zap, /* other ZAP */
1760ea8dc4b6Seschrock dump_zap, /* persistent error log */
176106eeb2adSek110237 dump_uint8, /* SPA history */
17624445fffbSMatthew Ahrens dump_history_offsets, /* SPA history offsets */
1763b1b8ab34Slling dump_zap, /* Pool properties */
1764ecd6cf80Smarks dump_zap, /* DSL permissions */
1765da6c28aaSamw dump_acl, /* ZFS ACL */
1766da6c28aaSamw dump_uint8, /* ZFS SYSACL */
1767da6c28aaSamw dump_none, /* FUID nvlist */
1768da6c28aaSamw dump_packed_nvlist, /* FUID nvlist size */
1769088f3894Sahrens dump_zap, /* DSL dataset next clones */
1770088f3894Sahrens dump_zap, /* DSL scrub queue */
177114843421SMatthew Ahrens dump_zap, /* ZFS user/group used */
177214843421SMatthew Ahrens dump_zap, /* ZFS user/group quota */
1773842727c2SChris Kirby dump_zap, /* snapshot refcount tags */
1774486ae710SMatthew Ahrens dump_ddt_zap, /* DDT ZAP object */
1775b24ab676SJeff Bonwick dump_zap, /* DDT statistics */
17760a586ceaSMark Shellenbaum dump_znode, /* SA object */
17770a586ceaSMark Shellenbaum dump_zap, /* SA Master Node */
17780a586ceaSMark Shellenbaum dump_sa_attrs, /* SA attribute registration */
17790a586ceaSMark Shellenbaum dump_sa_layouts, /* SA attribute layouts */
17803f9d6ad7SLin Ling dump_zap, /* DSL scrub translations */
17813f9d6ad7SLin Ling dump_none, /* fake dedup BP */
1782cde58dbcSMatthew Ahrens dump_zap, /* deadlist */
1783cde58dbcSMatthew Ahrens dump_none, /* deadlist hdr */
1784cde58dbcSMatthew Ahrens dump_zap, /* dsl clones */
1785bf263f2aSMatthew Ahrens dump_bpobj_subobjs, /* bpobj subobjs */
17860a586ceaSMark Shellenbaum dump_unknown, /* Unknown type, must be last */
1787fa9e4066Sahrens };
1788fa9e4066Sahrens
1789fa9e4066Sahrens static void
dump_object(objset_t * os,uint64_t object,int verbosity,int * print_header)1790fa9e4066Sahrens dump_object(objset_t *os, uint64_t object, int verbosity, int *print_header)
1791fa9e4066Sahrens {
1792fa9e4066Sahrens dmu_buf_t *db = NULL;
1793fa9e4066Sahrens dmu_object_info_t doi;
1794fa9e4066Sahrens dnode_t *dn;
1795fa9e4066Sahrens void *bonus = NULL;
1796fa9e4066Sahrens size_t bsize = 0;
17973f9d6ad7SLin Ling char iblk[32], dblk[32], lsize[32], asize[32], fill[32];
17983f9d6ad7SLin Ling char bonus_size[32];
1799fa9e4066Sahrens char aux[50];
1800fa9e4066Sahrens int error;
1801fa9e4066Sahrens
1802fa9e4066Sahrens if (*print_header) {
1803b24ab676SJeff Bonwick (void) printf("\n%10s %3s %5s %5s %5s %5s %6s %s\n",
1804b24ab676SJeff Bonwick "Object", "lvl", "iblk", "dblk", "dsize", "lsize",
1805b24ab676SJeff Bonwick "%full", "type");
1806fa9e4066Sahrens *print_header = 0;
1807fa9e4066Sahrens }
1808fa9e4066Sahrens
1809fa9e4066Sahrens if (object == 0) {
1810744947dcSTom Erickson dn = DMU_META_DNODE(os);
1811fa9e4066Sahrens } else {
1812ea8dc4b6Seschrock error = dmu_bonus_hold(os, object, FTAG, &db);
1813ea8dc4b6Seschrock if (error)
1814ea8dc4b6Seschrock fatal("dmu_bonus_hold(%llu) failed, errno %u",
1815ea8dc4b6Seschrock object, error);
1816fa9e4066Sahrens bonus = db->db_data;
1817fa9e4066Sahrens bsize = db->db_size;
1818744947dcSTom Erickson dn = DB_DNODE((dmu_buf_impl_t *)db);
1819fa9e4066Sahrens }
1820fa9e4066Sahrens dmu_object_info_from_dnode(dn, &doi);
1821fa9e4066Sahrens
18223f9d6ad7SLin Ling zdb_nicenum(doi.doi_metadata_block_size, iblk);
18233f9d6ad7SLin Ling zdb_nicenum(doi.doi_data_block_size, dblk);
18243f9d6ad7SLin Ling zdb_nicenum(doi.doi_max_offset, lsize);
18253f9d6ad7SLin Ling zdb_nicenum(doi.doi_physical_blocks_512 << 9, asize);
18263f9d6ad7SLin Ling zdb_nicenum(doi.doi_bonus_size, bonus_size);
1827b24ab676SJeff Bonwick (void) sprintf(fill, "%6.2f", 100.0 * doi.doi_fill_count *
1828bbfd46c4SJeff Bonwick doi.doi_data_block_size / (object == 0 ? DNODES_PER_BLOCK : 1) /
1829bbfd46c4SJeff Bonwick doi.doi_max_offset);
1830fa9e4066Sahrens
1831fa9e4066Sahrens aux[0] = '\0';
1832fa9e4066Sahrens
1833e7437265Sahrens if (doi.doi_checksum != ZIO_CHECKSUM_INHERIT || verbosity >= 6) {
1834fa9e4066Sahrens (void) snprintf(aux + strlen(aux), sizeof (aux), " (K=%s)",
18356de8f417SVictor Latushkin ZDB_CHECKSUM_NAME(doi.doi_checksum));
1836e7437265Sahrens }
1837fa9e4066Sahrens
1838e7437265Sahrens if (doi.doi_compress != ZIO_COMPRESS_INHERIT || verbosity >= 6) {
1839fa9e4066Sahrens (void) snprintf(aux + strlen(aux), sizeof (aux), " (Z=%s)",
18406de8f417SVictor Latushkin ZDB_COMPRESS_NAME(doi.doi_compress));
1841e7437265Sahrens }
1842fa9e4066Sahrens
1843b24ab676SJeff Bonwick (void) printf("%10lld %3u %5s %5s %5s %5s %6s %s%s\n",
1844b24ab676SJeff Bonwick (u_longlong_t)object, doi.doi_indirection, iblk, dblk,
1845b24ab676SJeff Bonwick asize, lsize, fill, ZDB_OT_NAME(doi.doi_type), aux);
1846fa9e4066Sahrens
1847fa9e4066Sahrens if (doi.doi_bonus_type != DMU_OT_NONE && verbosity > 3) {
1848b24ab676SJeff Bonwick (void) printf("%10s %3s %5s %5s %5s %5s %6s %s\n",
1849b24ab676SJeff Bonwick "", "", "", "", "", bonus_size, "bonus",
18506de8f417SVictor Latushkin ZDB_OT_NAME(doi.doi_bonus_type));
1851fa9e4066Sahrens }
1852fa9e4066Sahrens
1853fa9e4066Sahrens if (verbosity >= 4) {
18540a586ceaSMark Shellenbaum (void) printf("\tdnode flags: %s%s%s\n",
185514843421SMatthew Ahrens (dn->dn_phys->dn_flags & DNODE_FLAG_USED_BYTES) ?
185614843421SMatthew Ahrens "USED_BYTES " : "",
185714843421SMatthew Ahrens (dn->dn_phys->dn_flags & DNODE_FLAG_USERUSED_ACCOUNTED) ?
18580a586ceaSMark Shellenbaum "USERUSED_ACCOUNTED " : "",
18590a586ceaSMark Shellenbaum (dn->dn_phys->dn_flags & DNODE_FLAG_SPILL_BLKPTR) ?
18600a586ceaSMark Shellenbaum "SPILL_BLKPTR" : "");
186114843421SMatthew Ahrens (void) printf("\tdnode maxblkid: %llu\n",
186214843421SMatthew Ahrens (longlong_t)dn->dn_phys->dn_maxblkid);
186314843421SMatthew Ahrens
18646de8f417SVictor Latushkin object_viewer[ZDB_OT_TYPE(doi.doi_bonus_type)](os, object,
18656de8f417SVictor Latushkin bonus, bsize);
18666de8f417SVictor Latushkin object_viewer[ZDB_OT_TYPE(doi.doi_type)](os, object, NULL, 0);
1867fa9e4066Sahrens *print_header = 1;
1868fa9e4066Sahrens }
1869fa9e4066Sahrens
1870fa9e4066Sahrens if (verbosity >= 5)
187188b7b0f2SMatthew Ahrens dump_indirect(dn);
1872fa9e4066Sahrens
1873fa9e4066Sahrens if (verbosity >= 5) {
1874fa9e4066Sahrens /*
1875fa9e4066Sahrens * Report the list of segments that comprise the object.
1876fa9e4066Sahrens */
1877fa9e4066Sahrens uint64_t start = 0;
1878fa9e4066Sahrens uint64_t end;
1879fa9e4066Sahrens uint64_t blkfill = 1;
1880fa9e4066Sahrens int minlvl = 1;
1881fa9e4066Sahrens
1882fa9e4066Sahrens if (dn->dn_type == DMU_OT_DNODE) {
1883fa9e4066Sahrens minlvl = 0;
1884fa9e4066Sahrens blkfill = DNODES_PER_BLOCK;
1885fa9e4066Sahrens }
1886fa9e4066Sahrens
1887fa9e4066Sahrens for (;;) {
18883f9d6ad7SLin Ling char segsize[32];
1889cdb0ab79Smaybee error = dnode_next_offset(dn,
1890cdb0ab79Smaybee 0, &start, minlvl, blkfill, 0);
1891fa9e4066Sahrens if (error)
1892fa9e4066Sahrens break;
1893fa9e4066Sahrens end = start;
1894cdb0ab79Smaybee error = dnode_next_offset(dn,
1895cdb0ab79Smaybee DNODE_FIND_HOLE, &end, minlvl, blkfill, 0);
18963f9d6ad7SLin Ling zdb_nicenum(end - start, segsize);
1897fa9e4066Sahrens (void) printf("\t\tsegment [%016llx, %016llx)"
1898fa9e4066Sahrens " size %5s\n", (u_longlong_t)start,
1899fa9e4066Sahrens (u_longlong_t)end, segsize);
1900fa9e4066Sahrens if (error)
1901fa9e4066Sahrens break;
1902fa9e4066Sahrens start = end;
1903fa9e4066Sahrens }
1904fa9e4066Sahrens }
1905fa9e4066Sahrens
1906fa9e4066Sahrens if (db != NULL)
1907ea8dc4b6Seschrock dmu_buf_rele(db, FTAG);
1908fa9e4066Sahrens }
1909fa9e4066Sahrens
1910fa9e4066Sahrens static char *objset_types[DMU_OST_NUMTYPES] = {
1911fa9e4066Sahrens "NONE", "META", "ZPL", "ZVOL", "OTHER", "ANY" };
1912fa9e4066Sahrens
1913fa9e4066Sahrens static void
dump_dir(objset_t * os)1914fa9e4066Sahrens dump_dir(objset_t *os)
1915fa9e4066Sahrens {
1916fa9e4066Sahrens dmu_objset_stats_t dds;
1917fa9e4066Sahrens uint64_t object, object_count;
1918a2eea2e1Sahrens uint64_t refdbytes, usedobjs, scratch;
19193f9d6ad7SLin Ling char numbuf[32];
192014843421SMatthew Ahrens char blkbuf[BP_SPRINTF_LEN + 20];
1921*675fc291SMatthew Ahrens char osname[ZFS_MAX_DATASET_NAME_LEN];
1922fa9e4066Sahrens char *type = "UNKNOWN";
1923fa9e4066Sahrens int verbosity = dump_opt['d'];
1924fa9e4066Sahrens int print_header = 1;
1925fa9e4066Sahrens int i, error;
1926fa9e4066Sahrens
19273b2aab18SMatthew Ahrens dsl_pool_config_enter(dmu_objset_pool(os), FTAG);
1928a2eea2e1Sahrens dmu_objset_fast_stat(os, &dds);
19293b2aab18SMatthew Ahrens dsl_pool_config_exit(dmu_objset_pool(os), FTAG);
1930fa9e4066Sahrens
1931fa9e4066Sahrens if (dds.dds_type < DMU_OST_NUMTYPES)
1932fa9e4066Sahrens type = objset_types[dds.dds_type];
1933fa9e4066Sahrens
1934fa9e4066Sahrens if (dds.dds_type == DMU_OST_META) {
1935fa9e4066Sahrens dds.dds_creation_txg = TXG_INITIAL;
1936e54e0be9SMatthew Ahrens usedobjs = BP_GET_FILL(os->os_rootbp);
19378d62b223SJustin T. Gibbs refdbytes = dsl_dir_phys(os->os_spa->spa_dsl_pool->dp_mos_dir)->
19388d62b223SJustin T. Gibbs dd_used_bytes;
1939a2eea2e1Sahrens } else {
1940a2eea2e1Sahrens dmu_objset_space(os, &refdbytes, &scratch, &usedobjs, &scratch);
1941fa9e4066Sahrens }
1942fa9e4066Sahrens
1943e54e0be9SMatthew Ahrens ASSERT3U(usedobjs, ==, BP_GET_FILL(os->os_rootbp));
1944fa9e4066Sahrens
19453f9d6ad7SLin Ling zdb_nicenum(refdbytes, numbuf);
1946fa9e4066Sahrens
1947fa9e4066Sahrens if (verbosity >= 4) {
194849064978SMax Grossman (void) snprintf(blkbuf, sizeof (blkbuf), ", rootbp ");
194949064978SMax Grossman (void) snprintf_blkptr(blkbuf + strlen(blkbuf),
195049064978SMax Grossman sizeof (blkbuf) - strlen(blkbuf), os->os_rootbp);
1951fa9e4066Sahrens } else {
1952fa9e4066Sahrens blkbuf[0] = '\0';
1953fa9e4066Sahrens }
1954fa9e4066Sahrens
1955fa9e4066Sahrens dmu_objset_name(os, osname);
1956fa9e4066Sahrens
1957a2eea2e1Sahrens (void) printf("Dataset %s [%s], ID %llu, cr_txg %llu, "
1958fa9e4066Sahrens "%s, %llu objects%s\n",
1959fa9e4066Sahrens osname, type, (u_longlong_t)dmu_objset_id(os),
1960fa9e4066Sahrens (u_longlong_t)dds.dds_creation_txg,
1961a2eea2e1Sahrens numbuf, (u_longlong_t)usedobjs, blkbuf);
1962fa9e4066Sahrens
1963b24ab676SJeff Bonwick if (zopt_objects != 0) {
1964b24ab676SJeff Bonwick for (i = 0; i < zopt_objects; i++)
1965b24ab676SJeff Bonwick dump_object(os, zopt_object[i], verbosity,
1966b24ab676SJeff Bonwick &print_header);
1967b24ab676SJeff Bonwick (void) printf("\n");
1968b24ab676SJeff Bonwick return;
1969b24ab676SJeff Bonwick }
1970b24ab676SJeff Bonwick
1971b24ab676SJeff Bonwick if (dump_opt['i'] != 0 || verbosity >= 2)
1972fa9e4066Sahrens dump_intent_log(dmu_objset_zil(os));
1973fa9e4066Sahrens
1974fa9e4066Sahrens if (dmu_objset_ds(os) != NULL)
1975cde58dbcSMatthew Ahrens dump_deadlist(&dmu_objset_ds(os)->ds_deadlist);
1976fa9e4066Sahrens
1977fa9e4066Sahrens if (verbosity < 2)
1978fa9e4066Sahrens return;
1979fa9e4066Sahrens
198049064978SMax Grossman if (BP_IS_HOLE(os->os_rootbp))
1981088f3894Sahrens return;
1982088f3894Sahrens
1983fa9e4066Sahrens dump_object(os, 0, verbosity, &print_header);
198414843421SMatthew Ahrens object_count = 0;
1985744947dcSTom Erickson if (DMU_USERUSED_DNODE(os) != NULL &&
1986744947dcSTom Erickson DMU_USERUSED_DNODE(os)->dn_type != 0) {
198714843421SMatthew Ahrens dump_object(os, DMU_USERUSED_OBJECT, verbosity, &print_header);
198814843421SMatthew Ahrens dump_object(os, DMU_GROUPUSED_OBJECT, verbosity, &print_header);
198914843421SMatthew Ahrens }
1990fa9e4066Sahrens
1991fa9e4066Sahrens object = 0;
19926754306eSahrens while ((error = dmu_object_next(os, &object, B_FALSE, 0)) == 0) {
1993fa9e4066Sahrens dump_object(os, object, verbosity, &print_header);
1994fa9e4066Sahrens object_count++;
1995fa9e4066Sahrens }
1996fa9e4066Sahrens
1997a2eea2e1Sahrens ASSERT3U(object_count, ==, usedobjs);
1998fa9e4066Sahrens
1999fa9e4066Sahrens (void) printf("\n");
2000fa9e4066Sahrens
2001ccba0801SRich Morris if (error != ESRCH) {
2002ccba0801SRich Morris (void) fprintf(stderr, "dmu_object_next() = %d\n", error);
2003ccba0801SRich Morris abort();
2004ccba0801SRich Morris }
2005fa9e4066Sahrens }
2006fa9e4066Sahrens
2007fa9e4066Sahrens static void
dump_uberblock(uberblock_t * ub,const char * header,const char * footer)200853b9a4a9SVictor Latushkin dump_uberblock(uberblock_t *ub, const char *header, const char *footer)
2009fa9e4066Sahrens {
2010fa9e4066Sahrens time_t timestamp = ub->ub_timestamp;
2011fa9e4066Sahrens
201253b9a4a9SVictor Latushkin (void) printf(header ? header : "");
2013fa9e4066Sahrens (void) printf("\tmagic = %016llx\n", (u_longlong_t)ub->ub_magic);
2014fa9e4066Sahrens (void) printf("\tversion = %llu\n", (u_longlong_t)ub->ub_version);
2015fa9e4066Sahrens (void) printf("\ttxg = %llu\n", (u_longlong_t)ub->ub_txg);
2016fa9e4066Sahrens (void) printf("\tguid_sum = %llu\n", (u_longlong_t)ub->ub_guid_sum);
2017fa9e4066Sahrens (void) printf("\ttimestamp = %llu UTC = %s",
2018fa9e4066Sahrens (u_longlong_t)ub->ub_timestamp, asctime(localtime(×tamp)));
2019fa9e4066Sahrens if (dump_opt['u'] >= 3) {
2020fbabab8fSmaybee char blkbuf[BP_SPRINTF_LEN];
202149064978SMax Grossman snprintf_blkptr(blkbuf, sizeof (blkbuf), &ub->ub_rootbp);
2022fa9e4066Sahrens (void) printf("\trootbp = %s\n", blkbuf);
2023fa9e4066Sahrens }
202453b9a4a9SVictor Latushkin (void) printf(footer ? footer : "");
2025fa9e4066Sahrens }
2026fa9e4066Sahrens
2027fa9e4066Sahrens static void
dump_config(spa_t * spa)202807428bdfSVictor Latushkin dump_config(spa_t *spa)
2029fa9e4066Sahrens {
203007428bdfSVictor Latushkin dmu_buf_t *db;
203107428bdfSVictor Latushkin size_t nvsize = 0;
203207428bdfSVictor Latushkin int error = 0;
2033fa9e4066Sahrens
203407428bdfSVictor Latushkin
203507428bdfSVictor Latushkin error = dmu_bonus_hold(spa->spa_meta_objset,
203607428bdfSVictor Latushkin spa->spa_config_object, FTAG, &db);
203707428bdfSVictor Latushkin
203807428bdfSVictor Latushkin if (error == 0) {
203907428bdfSVictor Latushkin nvsize = *(uint64_t *)db->db_data;
204007428bdfSVictor Latushkin dmu_buf_rele(db, FTAG);
204107428bdfSVictor Latushkin
204207428bdfSVictor Latushkin (void) printf("\nMOS Configuration:\n");
204307428bdfSVictor Latushkin dump_packed_nvlist(spa->spa_meta_objset,
204407428bdfSVictor Latushkin spa->spa_config_object, (void *)&nvsize, 1);
204507428bdfSVictor Latushkin } else {
204607428bdfSVictor Latushkin (void) fprintf(stderr, "dmu_bonus_hold(%llu) failed, errno %d",
204707428bdfSVictor Latushkin (u_longlong_t)spa->spa_config_object, error);
2048fa9e4066Sahrens }
2049fa9e4066Sahrens }
2050fa9e4066Sahrens
2051fa9e4066Sahrens static void
dump_cachefile(const char * cachefile)2052c5904d13Seschrock dump_cachefile(const char *cachefile)
2053c5904d13Seschrock {
2054c5904d13Seschrock int fd;
2055c5904d13Seschrock struct stat64 statbuf;
2056c5904d13Seschrock char *buf;
2057c5904d13Seschrock nvlist_t *config;
2058c5904d13Seschrock
2059c5904d13Seschrock if ((fd = open64(cachefile, O_RDONLY)) < 0) {
2060c5904d13Seschrock (void) printf("cannot open '%s': %s\n", cachefile,
2061c5904d13Seschrock strerror(errno));
2062c5904d13Seschrock exit(1);
2063c5904d13Seschrock }
2064c5904d13Seschrock
2065c5904d13Seschrock if (fstat64(fd, &statbuf) != 0) {
2066c5904d13Seschrock (void) printf("failed to stat '%s': %s\n", cachefile,
2067c5904d13Seschrock strerror(errno));
2068c5904d13Seschrock exit(1);
2069c5904d13Seschrock }
2070c5904d13Seschrock
2071c5904d13Seschrock if ((buf = malloc(statbuf.st_size)) == NULL) {
2072c5904d13Seschrock (void) fprintf(stderr, "failed to allocate %llu bytes\n",
2073c5904d13Seschrock (u_longlong_t)statbuf.st_size);
2074c5904d13Seschrock exit(1);
2075c5904d13Seschrock }
2076c5904d13Seschrock
2077c5904d13Seschrock if (read(fd, buf, statbuf.st_size) != statbuf.st_size) {
2078c5904d13Seschrock (void) fprintf(stderr, "failed to read %llu bytes\n",
2079c5904d13Seschrock (u_longlong_t)statbuf.st_size);
2080c5904d13Seschrock exit(1);
2081c5904d13Seschrock }
2082c5904d13Seschrock
2083c5904d13Seschrock (void) close(fd);
2084c5904d13Seschrock
2085c5904d13Seschrock if (nvlist_unpack(buf, statbuf.st_size, &config, 0) != 0) {
2086c5904d13Seschrock (void) fprintf(stderr, "failed to unpack nvlist\n");
2087c5904d13Seschrock exit(1);
2088c5904d13Seschrock }
2089c5904d13Seschrock
2090c5904d13Seschrock free(buf);
2091c5904d13Seschrock
2092c5904d13Seschrock dump_nvlist(config, 0);
2093c5904d13Seschrock
2094c5904d13Seschrock nvlist_free(config);
2095c5904d13Seschrock }
2096c5904d13Seschrock
209753b9a4a9SVictor Latushkin #define ZDB_MAX_UB_HEADER_SIZE 32
209853b9a4a9SVictor Latushkin
209953b9a4a9SVictor Latushkin static void
dump_label_uberblocks(vdev_label_t * lbl,uint64_t ashift)210053b9a4a9SVictor Latushkin dump_label_uberblocks(vdev_label_t *lbl, uint64_t ashift)
210153b9a4a9SVictor Latushkin {
210253b9a4a9SVictor Latushkin vdev_t vd;
210353b9a4a9SVictor Latushkin vdev_t *vdp = &vd;
210453b9a4a9SVictor Latushkin char header[ZDB_MAX_UB_HEADER_SIZE];
210553b9a4a9SVictor Latushkin
210653b9a4a9SVictor Latushkin vd.vdev_ashift = ashift;
210753b9a4a9SVictor Latushkin vdp->vdev_top = vdp;
210853b9a4a9SVictor Latushkin
210953b9a4a9SVictor Latushkin for (int i = 0; i < VDEV_UBERBLOCK_COUNT(vdp); i++) {
211053b9a4a9SVictor Latushkin uint64_t uoff = VDEV_UBERBLOCK_OFFSET(vdp, i);
211153b9a4a9SVictor Latushkin uberblock_t *ub = (void *)((char *)lbl + uoff);
211253b9a4a9SVictor Latushkin
211353b9a4a9SVictor Latushkin if (uberblock_verify(ub))
211453b9a4a9SVictor Latushkin continue;
211553b9a4a9SVictor Latushkin (void) snprintf(header, ZDB_MAX_UB_HEADER_SIZE,
211653b9a4a9SVictor Latushkin "Uberblock[%d]\n", i);
211753b9a4a9SVictor Latushkin dump_uberblock(ub, header, "");
211853b9a4a9SVictor Latushkin }
211953b9a4a9SVictor Latushkin }
212053b9a4a9SVictor Latushkin
2121c5904d13Seschrock static void
dump_label(const char * dev)2122fa9e4066Sahrens dump_label(const char *dev)
2123fa9e4066Sahrens {
2124fa9e4066Sahrens int fd;
2125fa9e4066Sahrens vdev_label_t label;
2126c6065d0fSGeorge Wilson char *path, *buf = label.vl_vdev_phys.vp_nvlist;
2127fa9e4066Sahrens size_t buflen = sizeof (label.vl_vdev_phys.vp_nvlist);
2128fa9e4066Sahrens struct stat64 statbuf;
212953b9a4a9SVictor Latushkin uint64_t psize, ashift;
2130c6065d0fSGeorge Wilson int len = strlen(dev) + 1;
2131fa9e4066Sahrens
2132c6065d0fSGeorge Wilson if (strncmp(dev, "/dev/dsk/", 9) == 0) {
2133c6065d0fSGeorge Wilson len++;
2134c6065d0fSGeorge Wilson path = malloc(len);
2135c6065d0fSGeorge Wilson (void) snprintf(path, len, "%s%s", "/dev/rdsk/", dev + 9);
2136c6065d0fSGeorge Wilson } else {
2137c6065d0fSGeorge Wilson path = strdup(dev);
2138c6065d0fSGeorge Wilson }
2139c6065d0fSGeorge Wilson
2140c6065d0fSGeorge Wilson if ((fd = open64(path, O_RDONLY)) < 0) {
2141c6065d0fSGeorge Wilson (void) printf("cannot open '%s': %s\n", path, strerror(errno));
2142c6065d0fSGeorge Wilson free(path);
2143fa9e4066Sahrens exit(1);
2144fa9e4066Sahrens }
2145fa9e4066Sahrens
2146fa9e4066Sahrens if (fstat64(fd, &statbuf) != 0) {
2147c6065d0fSGeorge Wilson (void) printf("failed to stat '%s': %s\n", path,
2148fa9e4066Sahrens strerror(errno));
2149c6065d0fSGeorge Wilson free(path);
2150c6065d0fSGeorge Wilson (void) close(fd);
2151c6065d0fSGeorge Wilson exit(1);
2152c6065d0fSGeorge Wilson }
2153c6065d0fSGeorge Wilson
2154c6065d0fSGeorge Wilson if (S_ISBLK(statbuf.st_mode)) {
2155c6065d0fSGeorge Wilson (void) printf("cannot use '%s': character device required\n",
2156c6065d0fSGeorge Wilson path);
2157c6065d0fSGeorge Wilson free(path);
2158c6065d0fSGeorge Wilson (void) close(fd);
2159c6065d0fSGeorge Wilson exit(1);
2160fa9e4066Sahrens }
2161fa9e4066Sahrens
2162fa9e4066Sahrens psize = statbuf.st_size;
2163fa9e4066Sahrens psize = P2ALIGN(psize, (uint64_t)sizeof (vdev_label_t));
2164fa9e4066Sahrens
216553b9a4a9SVictor Latushkin for (int l = 0; l < VDEV_LABELS; l++) {
2166fa9e4066Sahrens nvlist_t *config = NULL;
2167fa9e4066Sahrens
2168fa9e4066Sahrens (void) printf("--------------------------------------------\n");
2169fa9e4066Sahrens (void) printf("LABEL %d\n", l);
2170fa9e4066Sahrens (void) printf("--------------------------------------------\n");
2171fa9e4066Sahrens
21720d981225Seschrock if (pread64(fd, &label, sizeof (label),
2173fa9e4066Sahrens vdev_label_offset(psize, l, 0)) != sizeof (label)) {
2174fa9e4066Sahrens (void) printf("failed to read label %d\n", l);
2175fa9e4066Sahrens continue;
2176fa9e4066Sahrens }
2177fa9e4066Sahrens
2178fa9e4066Sahrens if (nvlist_unpack(buf, buflen, &config, 0) != 0) {
2179fa9e4066Sahrens (void) printf("failed to unpack label %d\n", l);
218053b9a4a9SVictor Latushkin ashift = SPA_MINBLOCKSHIFT;
218153b9a4a9SVictor Latushkin } else {
218253b9a4a9SVictor Latushkin nvlist_t *vdev_tree = NULL;
218353b9a4a9SVictor Latushkin
2184fa9e4066Sahrens dump_nvlist(config, 4);
218553b9a4a9SVictor Latushkin if ((nvlist_lookup_nvlist(config,
218653b9a4a9SVictor Latushkin ZPOOL_CONFIG_VDEV_TREE, &vdev_tree) != 0) ||
218753b9a4a9SVictor Latushkin (nvlist_lookup_uint64(vdev_tree,
218853b9a4a9SVictor Latushkin ZPOOL_CONFIG_ASHIFT, &ashift) != 0))
218953b9a4a9SVictor Latushkin ashift = SPA_MINBLOCKSHIFT;
2190fa9e4066Sahrens nvlist_free(config);
2191fa9e4066Sahrens }
219253b9a4a9SVictor Latushkin if (dump_opt['u'])
219353b9a4a9SVictor Latushkin dump_label_uberblocks(&label, ashift);
219453b9a4a9SVictor Latushkin }
2195c6065d0fSGeorge Wilson
2196c6065d0fSGeorge Wilson free(path);
2197c6065d0fSGeorge Wilson (void) close(fd);
2198fa9e4066Sahrens }
2199fa9e4066Sahrens
22000cc589a4SMatthew Ahrens static uint64_t dataset_feature_count[SPA_FEATURES];
2201d1a98260SMatthew Ahrens
2202fa9e4066Sahrens /*ARGSUSED*/
22031d452cf5Sahrens static int
dump_one_dir(const char * dsname,void * arg)2204fd136879SMatthew Ahrens dump_one_dir(const char *dsname, void *arg)
2205fa9e4066Sahrens {
2206fa9e4066Sahrens int error;
2207fa9e4066Sahrens objset_t *os;
2208fa9e4066Sahrens
2209503ad85cSMatthew Ahrens error = dmu_objset_own(dsname, DMU_OST_ANY, B_TRUE, FTAG, &os);
2210fa9e4066Sahrens if (error) {
2211b24ab676SJeff Bonwick (void) printf("Could not open %s, error %d\n", dsname, error);
22121d452cf5Sahrens return (0);
2213fa9e4066Sahrens }
22140cc589a4SMatthew Ahrens
22150cc589a4SMatthew Ahrens for (spa_feature_t f = 0; f < SPA_FEATURES; f++) {
22160cc589a4SMatthew Ahrens if (!dmu_objset_ds(os)->ds_feature_inuse[f])
22170cc589a4SMatthew Ahrens continue;
22180cc589a4SMatthew Ahrens ASSERT(spa_feature_table[f].fi_flags &
22190cc589a4SMatthew Ahrens ZFEATURE_FLAG_PER_DATASET);
22200cc589a4SMatthew Ahrens dataset_feature_count[f]++;
22210cc589a4SMatthew Ahrens }
22220cc589a4SMatthew Ahrens
2223fa9e4066Sahrens dump_dir(os);
2224503ad85cSMatthew Ahrens dmu_objset_disown(os, FTAG);
2225e0d35c44Smarks fuid_table_destroy();
22260a586ceaSMark Shellenbaum sa_loaded = B_FALSE;
22271d452cf5Sahrens return (0);
2228fa9e4066Sahrens }
2229fa9e4066Sahrens
2230b24ab676SJeff Bonwick /*
2231b24ab676SJeff Bonwick * Block statistics.
2232b24ab676SJeff Bonwick */
2233d1a98260SMatthew Ahrens #define PSIZE_HISTO_SIZE (SPA_OLD_MAXBLOCKSIZE / SPA_MINBLOCKSIZE + 2)
2234b24ab676SJeff Bonwick typedef struct zdb_blkstats {
2235b24ab676SJeff Bonwick uint64_t zb_asize;
2236b24ab676SJeff Bonwick uint64_t zb_lsize;
2237b24ab676SJeff Bonwick uint64_t zb_psize;
2238b24ab676SJeff Bonwick uint64_t zb_count;
2239d5ee8a13SMatthew Ahrens uint64_t zb_gangs;
2240d5ee8a13SMatthew Ahrens uint64_t zb_ditto_samevdev;
2241490d05b9SMatthew Ahrens uint64_t zb_psize_histogram[PSIZE_HISTO_SIZE];
2242b24ab676SJeff Bonwick } zdb_blkstats_t;
2243b24ab676SJeff Bonwick
2244b24ab676SJeff Bonwick /*
2245b24ab676SJeff Bonwick * Extended object types to report deferred frees and dedup auto-ditto blocks.
2246b24ab676SJeff Bonwick */
2247b24ab676SJeff Bonwick #define ZDB_OT_DEFERRED (DMU_OT_NUMTYPES + 0)
2248b24ab676SJeff Bonwick #define ZDB_OT_DITTO (DMU_OT_NUMTYPES + 1)
2249ad135b5dSChristopher Siden #define ZDB_OT_OTHER (DMU_OT_NUMTYPES + 2)
2250ad135b5dSChristopher Siden #define ZDB_OT_TOTAL (DMU_OT_NUMTYPES + 3)
2251b24ab676SJeff Bonwick
2252b24ab676SJeff Bonwick static char *zdb_ot_extname[] = {
2253b24ab676SJeff Bonwick "deferred free",
2254b24ab676SJeff Bonwick "dedup ditto",
2255ad135b5dSChristopher Siden "other",
2256b24ab676SJeff Bonwick "Total",
2257b24ab676SJeff Bonwick };
2258b24ab676SJeff Bonwick
2259b24ab676SJeff Bonwick #define ZB_TOTAL DN_MAX_LEVELS
2260b24ab676SJeff Bonwick
2261b24ab676SJeff Bonwick typedef struct zdb_cb {
2262b24ab676SJeff Bonwick zdb_blkstats_t zcb_type[ZB_TOTAL + 1][ZDB_OT_TOTAL + 1];
2263b24ab676SJeff Bonwick uint64_t zcb_dedup_asize;
2264b24ab676SJeff Bonwick uint64_t zcb_dedup_blocks;
2265e54e0be9SMatthew Ahrens uint64_t zcb_embedded_blocks[NUM_BP_EMBEDDED_TYPES];
2266e54e0be9SMatthew Ahrens uint64_t zcb_embedded_histogram[NUM_BP_EMBEDDED_TYPES]
2267e54e0be9SMatthew Ahrens [BPE_PAYLOAD_SIZE];
2268490d05b9SMatthew Ahrens uint64_t zcb_start;
2269490d05b9SMatthew Ahrens uint64_t zcb_lastprint;
2270490d05b9SMatthew Ahrens uint64_t zcb_totalasize;
2271b24ab676SJeff Bonwick uint64_t zcb_errors[256];
2272b24ab676SJeff Bonwick int zcb_readfails;
2273b24ab676SJeff Bonwick int zcb_haderrors;
2274cde58dbcSMatthew Ahrens spa_t *zcb_spa;
2275b24ab676SJeff Bonwick } zdb_cb_t;
2276b24ab676SJeff Bonwick
2277b24ab676SJeff Bonwick static void
zdb_count_block(zdb_cb_t * zcb,zilog_t * zilog,const blkptr_t * bp,dmu_object_type_t type)2278cde58dbcSMatthew Ahrens zdb_count_block(zdb_cb_t *zcb, zilog_t *zilog, const blkptr_t *bp,
2279b24ab676SJeff Bonwick dmu_object_type_t type)
2280b24ab676SJeff Bonwick {
2281b24ab676SJeff Bonwick uint64_t refcnt = 0;
2282b24ab676SJeff Bonwick
2283b24ab676SJeff Bonwick ASSERT(type < ZDB_OT_TOTAL);
2284b24ab676SJeff Bonwick
2285b24ab676SJeff Bonwick if (zilog && zil_bp_tree_add(zilog, bp) != 0)
2286b24ab676SJeff Bonwick return;
2287b24ab676SJeff Bonwick
2288b24ab676SJeff Bonwick for (int i = 0; i < 4; i++) {
2289b24ab676SJeff Bonwick int l = (i < 2) ? BP_GET_LEVEL(bp) : ZB_TOTAL;
2290b24ab676SJeff Bonwick int t = (i & 1) ? type : ZDB_OT_TOTAL;
2291d5ee8a13SMatthew Ahrens int equal;
2292b24ab676SJeff Bonwick zdb_blkstats_t *zb = &zcb->zcb_type[l][t];
2293b24ab676SJeff Bonwick
2294b24ab676SJeff Bonwick zb->zb_asize += BP_GET_ASIZE(bp);
2295b24ab676SJeff Bonwick zb->zb_lsize += BP_GET_LSIZE(bp);
2296b24ab676SJeff Bonwick zb->zb_psize += BP_GET_PSIZE(bp);
2297b24ab676SJeff Bonwick zb->zb_count++;
2298d1a98260SMatthew Ahrens
2299d1a98260SMatthew Ahrens /*
2300d1a98260SMatthew Ahrens * The histogram is only big enough to record blocks up to
2301d1a98260SMatthew Ahrens * SPA_OLD_MAXBLOCKSIZE; larger blocks go into the last,
2302d1a98260SMatthew Ahrens * "other", bucket.
2303d1a98260SMatthew Ahrens */
2304d1a98260SMatthew Ahrens int idx = BP_GET_PSIZE(bp) >> SPA_MINBLOCKSHIFT;
2305d1a98260SMatthew Ahrens idx = MIN(idx, SPA_OLD_MAXBLOCKSIZE / SPA_MINBLOCKSIZE + 1);
2306d1a98260SMatthew Ahrens zb->zb_psize_histogram[idx]++;
2307d5ee8a13SMatthew Ahrens
2308d5ee8a13SMatthew Ahrens zb->zb_gangs += BP_COUNT_GANG(bp);
2309d5ee8a13SMatthew Ahrens
2310d5ee8a13SMatthew Ahrens switch (BP_GET_NDVAS(bp)) {
2311d5ee8a13SMatthew Ahrens case 2:
2312d5ee8a13SMatthew Ahrens if (DVA_GET_VDEV(&bp->blk_dva[0]) ==
2313d5ee8a13SMatthew Ahrens DVA_GET_VDEV(&bp->blk_dva[1]))
2314d5ee8a13SMatthew Ahrens zb->zb_ditto_samevdev++;
2315d5ee8a13SMatthew Ahrens break;
2316d5ee8a13SMatthew Ahrens case 3:
2317d5ee8a13SMatthew Ahrens equal = (DVA_GET_VDEV(&bp->blk_dva[0]) ==
2318d5ee8a13SMatthew Ahrens DVA_GET_VDEV(&bp->blk_dva[1])) +
2319d5ee8a13SMatthew Ahrens (DVA_GET_VDEV(&bp->blk_dva[0]) ==
2320d5ee8a13SMatthew Ahrens DVA_GET_VDEV(&bp->blk_dva[2])) +
2321d5ee8a13SMatthew Ahrens (DVA_GET_VDEV(&bp->blk_dva[1]) ==
2322d5ee8a13SMatthew Ahrens DVA_GET_VDEV(&bp->blk_dva[2]));
2323d5ee8a13SMatthew Ahrens if (equal != 0)
2324d5ee8a13SMatthew Ahrens zb->zb_ditto_samevdev++;
2325d5ee8a13SMatthew Ahrens break;
2326d5ee8a13SMatthew Ahrens }
2327d5ee8a13SMatthew Ahrens
2328b24ab676SJeff Bonwick }
2329b24ab676SJeff Bonwick
2330e54e0be9SMatthew Ahrens if (BP_IS_EMBEDDED(bp)) {
2331e54e0be9SMatthew Ahrens zcb->zcb_embedded_blocks[BPE_GET_ETYPE(bp)]++;
2332e54e0be9SMatthew Ahrens zcb->zcb_embedded_histogram[BPE_GET_ETYPE(bp)]
2333e54e0be9SMatthew Ahrens [BPE_GET_PSIZE(bp)]++;
2334e54e0be9SMatthew Ahrens return;
2335e54e0be9SMatthew Ahrens }
2336e54e0be9SMatthew Ahrens
2337b24ab676SJeff Bonwick if (dump_opt['L'])
2338b24ab676SJeff Bonwick return;
2339b24ab676SJeff Bonwick
2340b24ab676SJeff Bonwick if (BP_GET_DEDUP(bp)) {
2341b24ab676SJeff Bonwick ddt_t *ddt;
2342b24ab676SJeff Bonwick ddt_entry_t *dde;
2343b24ab676SJeff Bonwick
2344cde58dbcSMatthew Ahrens ddt = ddt_select(zcb->zcb_spa, bp);
2345b24ab676SJeff Bonwick ddt_enter(ddt);
2346b24ab676SJeff Bonwick dde = ddt_lookup(ddt, bp, B_FALSE);
2347b24ab676SJeff Bonwick
2348b24ab676SJeff Bonwick if (dde == NULL) {
2349b24ab676SJeff Bonwick refcnt = 0;
2350b24ab676SJeff Bonwick } else {
2351b24ab676SJeff Bonwick ddt_phys_t *ddp = ddt_phys_select(dde, bp);
2352b24ab676SJeff Bonwick ddt_phys_decref(ddp);
2353b24ab676SJeff Bonwick refcnt = ddp->ddp_refcnt;
2354b24ab676SJeff Bonwick if (ddt_phys_total_refcnt(dde) == 0)
2355b24ab676SJeff Bonwick ddt_remove(ddt, dde);
2356b24ab676SJeff Bonwick }
2357b24ab676SJeff Bonwick ddt_exit(ddt);
2358b24ab676SJeff Bonwick }
2359b24ab676SJeff Bonwick
2360cde58dbcSMatthew Ahrens VERIFY3U(zio_wait(zio_claim(NULL, zcb->zcb_spa,
2361cde58dbcSMatthew Ahrens refcnt ? 0 : spa_first_txg(zcb->zcb_spa),
2362b24ab676SJeff Bonwick bp, NULL, NULL, ZIO_FLAG_CANFAIL)), ==, 0);
2363b24ab676SJeff Bonwick }
2364b24ab676SJeff Bonwick
236531d7e8faSGeorge Wilson static void
zdb_blkptr_done(zio_t * zio)236631d7e8faSGeorge Wilson zdb_blkptr_done(zio_t *zio)
236731d7e8faSGeorge Wilson {
236831d7e8faSGeorge Wilson spa_t *spa = zio->io_spa;
236931d7e8faSGeorge Wilson blkptr_t *bp = zio->io_bp;
237031d7e8faSGeorge Wilson int ioerr = zio->io_error;
237131d7e8faSGeorge Wilson zdb_cb_t *zcb = zio->io_private;
237205d95d03SMatthew Ahrens zbookmark_phys_t *zb = &zio->io_bookmark;
237331d7e8faSGeorge Wilson
237431d7e8faSGeorge Wilson zio_data_buf_free(zio->io_data, zio->io_size);
237531d7e8faSGeorge Wilson
237631d7e8faSGeorge Wilson mutex_enter(&spa->spa_scrub_lock);
237731d7e8faSGeorge Wilson spa->spa_scrub_inflight--;
237831d7e8faSGeorge Wilson cv_broadcast(&spa->spa_scrub_io_cv);
237931d7e8faSGeorge Wilson
238031d7e8faSGeorge Wilson if (ioerr && !(zio->io_flags & ZIO_FLAG_SPECULATIVE)) {
238131d7e8faSGeorge Wilson char blkbuf[BP_SPRINTF_LEN];
238231d7e8faSGeorge Wilson
238331d7e8faSGeorge Wilson zcb->zcb_haderrors = 1;
238431d7e8faSGeorge Wilson zcb->zcb_errors[ioerr]++;
238531d7e8faSGeorge Wilson
238631d7e8faSGeorge Wilson if (dump_opt['b'] >= 2)
238749064978SMax Grossman snprintf_blkptr(blkbuf, sizeof (blkbuf), bp);
238831d7e8faSGeorge Wilson else
238931d7e8faSGeorge Wilson blkbuf[0] = '\0';
239031d7e8faSGeorge Wilson
239131d7e8faSGeorge Wilson (void) printf("zdb_blkptr_cb: "
239231d7e8faSGeorge Wilson "Got error %d reading "
239331d7e8faSGeorge Wilson "<%llu, %llu, %lld, %llx> %s -- skipping\n",
239431d7e8faSGeorge Wilson ioerr,
239531d7e8faSGeorge Wilson (u_longlong_t)zb->zb_objset,
239631d7e8faSGeorge Wilson (u_longlong_t)zb->zb_object,
239731d7e8faSGeorge Wilson (u_longlong_t)zb->zb_level,
239831d7e8faSGeorge Wilson (u_longlong_t)zb->zb_blkid,
239931d7e8faSGeorge Wilson blkbuf);
240031d7e8faSGeorge Wilson }
240131d7e8faSGeorge Wilson mutex_exit(&spa->spa_scrub_lock);
240231d7e8faSGeorge Wilson }
240331d7e8faSGeorge Wilson
2404b24ab676SJeff Bonwick static int
zdb_blkptr_cb(spa_t * spa,zilog_t * zilog,const blkptr_t * bp,const zbookmark_phys_t * zb,const dnode_phys_t * dnp,void * arg)24051b912ec7SGeorge Wilson zdb_blkptr_cb(spa_t *spa, zilog_t *zilog, const blkptr_t *bp,
240605d95d03SMatthew Ahrens const zbookmark_phys_t *zb, const dnode_phys_t *dnp, void *arg)
2407b24ab676SJeff Bonwick {
2408b24ab676SJeff Bonwick zdb_cb_t *zcb = arg;
2409b24ab676SJeff Bonwick dmu_object_type_t type;
2410b24ab676SJeff Bonwick boolean_t is_metadata;
2411b24ab676SJeff Bonwick
24125f9bb2f3SPaul Dagnelie if (bp == NULL)
24135f9bb2f3SPaul Dagnelie return (0);
24145f9bb2f3SPaul Dagnelie
241549064978SMax Grossman if (dump_opt['b'] >= 5 && bp->blk_birth > 0) {
241649064978SMax Grossman char blkbuf[BP_SPRINTF_LEN];
241749064978SMax Grossman snprintf_blkptr(blkbuf, sizeof (blkbuf), bp);
241849064978SMax Grossman (void) printf("objset %llu object %llu "
241949064978SMax Grossman "level %lld offset 0x%llx %s\n",
242049064978SMax Grossman (u_longlong_t)zb->zb_objset,
242149064978SMax Grossman (u_longlong_t)zb->zb_object,
242249064978SMax Grossman (longlong_t)zb->zb_level,
242349064978SMax Grossman (u_longlong_t)blkid2offset(dnp, bp, zb),
242449064978SMax Grossman blkbuf);
242549064978SMax Grossman }
242649064978SMax Grossman
242749064978SMax Grossman if (BP_IS_HOLE(bp))
2428b24ab676SJeff Bonwick return (0);
2429b24ab676SJeff Bonwick
2430b24ab676SJeff Bonwick type = BP_GET_TYPE(bp);
2431b24ab676SJeff Bonwick
2432ad135b5dSChristopher Siden zdb_count_block(zcb, zilog, bp,
2433ad135b5dSChristopher Siden (type & DMU_OT_NEWTYPE) ? ZDB_OT_OTHER : type);
2434b24ab676SJeff Bonwick
2435ad135b5dSChristopher Siden is_metadata = (BP_GET_LEVEL(bp) != 0 || DMU_OT_IS_METADATA(type));
2436b24ab676SJeff Bonwick
2437e54e0be9SMatthew Ahrens if (!BP_IS_EMBEDDED(bp) &&
2438e54e0be9SMatthew Ahrens (dump_opt['c'] > 1 || (dump_opt['c'] && is_metadata))) {
2439b24ab676SJeff Bonwick size_t size = BP_GET_PSIZE(bp);
244031d7e8faSGeorge Wilson void *data = zio_data_buf_alloc(size);
2441b24ab676SJeff Bonwick int flags = ZIO_FLAG_CANFAIL | ZIO_FLAG_SCRUB | ZIO_FLAG_RAW;
2442b24ab676SJeff Bonwick
2443b24ab676SJeff Bonwick /* If it's an intent log block, failure is expected. */
2444b24ab676SJeff Bonwick if (zb->zb_level == ZB_ZIL_LEVEL)
2445b24ab676SJeff Bonwick flags |= ZIO_FLAG_SPECULATIVE;
2446b24ab676SJeff Bonwick
244731d7e8faSGeorge Wilson mutex_enter(&spa->spa_scrub_lock);
244831d7e8faSGeorge Wilson while (spa->spa_scrub_inflight > max_inflight)
244931d7e8faSGeorge Wilson cv_wait(&spa->spa_scrub_io_cv, &spa->spa_scrub_lock);
245031d7e8faSGeorge Wilson spa->spa_scrub_inflight++;
245131d7e8faSGeorge Wilson mutex_exit(&spa->spa_scrub_lock);
2452b24ab676SJeff Bonwick
245331d7e8faSGeorge Wilson zio_nowait(zio_read(NULL, spa, bp, data, size,
245431d7e8faSGeorge Wilson zdb_blkptr_done, zcb, ZIO_PRIORITY_ASYNC_READ, flags, zb));
2455b24ab676SJeff Bonwick }
2456b24ab676SJeff Bonwick
2457b24ab676SJeff Bonwick zcb->zcb_readfails = 0;
2458b24ab676SJeff Bonwick
245949b2c7b9SMatthew Ahrens /* only call gethrtime() every 100 blocks */
246049b2c7b9SMatthew Ahrens static int iters;
246149b2c7b9SMatthew Ahrens if (++iters > 100)
246249b2c7b9SMatthew Ahrens iters = 0;
246349b2c7b9SMatthew Ahrens else
246449b2c7b9SMatthew Ahrens return (0);
246549b2c7b9SMatthew Ahrens
246649b2c7b9SMatthew Ahrens if (dump_opt['b'] < 5 && gethrtime() > zcb->zcb_lastprint + NANOSEC) {
2467490d05b9SMatthew Ahrens uint64_t now = gethrtime();
2468490d05b9SMatthew Ahrens char buf[10];
2469490d05b9SMatthew Ahrens uint64_t bytes = zcb->zcb_type[ZB_TOTAL][ZDB_OT_TOTAL].zb_asize;
2470490d05b9SMatthew Ahrens int kb_per_sec =
2471490d05b9SMatthew Ahrens 1 + bytes / (1 + ((now - zcb->zcb_start) / 1000 / 1000));
2472490d05b9SMatthew Ahrens int sec_remaining =
2473490d05b9SMatthew Ahrens (zcb->zcb_totalasize - bytes) / 1024 / kb_per_sec;
2474490d05b9SMatthew Ahrens
2475490d05b9SMatthew Ahrens zfs_nicenum(bytes, buf, sizeof (buf));
2476490d05b9SMatthew Ahrens (void) fprintf(stderr,
2477490d05b9SMatthew Ahrens "\r%5s completed (%4dMB/s) "
2478490d05b9SMatthew Ahrens "estimated time remaining: %uhr %02umin %02usec ",
2479490d05b9SMatthew Ahrens buf, kb_per_sec / 1024,
2480490d05b9SMatthew Ahrens sec_remaining / 60 / 60,
2481490d05b9SMatthew Ahrens sec_remaining / 60 % 60,
2482490d05b9SMatthew Ahrens sec_remaining % 60);
2483490d05b9SMatthew Ahrens
2484490d05b9SMatthew Ahrens zcb->zcb_lastprint = now;
2485490d05b9SMatthew Ahrens }
2486490d05b9SMatthew Ahrens
2487b24ab676SJeff Bonwick return (0);
2488b24ab676SJeff Bonwick }
2489b24ab676SJeff Bonwick
2490fa9e4066Sahrens static void
zdb_leak(void * arg,uint64_t start,uint64_t size)24910713e232SGeorge Wilson zdb_leak(void *arg, uint64_t start, uint64_t size)
2492fa9e4066Sahrens {
24930713e232SGeorge Wilson vdev_t *vd = arg;
2494fa9e4066Sahrens
2495fa9e4066Sahrens (void) printf("leaked space: vdev %llu, offset 0x%llx, size %llu\n",
2496e14bb325SJeff Bonwick (u_longlong_t)vd->vdev_id, (u_longlong_t)start, (u_longlong_t)size);
2497e14bb325SJeff Bonwick }
2498e14bb325SJeff Bonwick
24990713e232SGeorge Wilson static metaslab_ops_t zdb_metaslab_ops = {
250002b647c5SGeorge Wilson NULL /* alloc */
2501e14bb325SJeff Bonwick };
2502e14bb325SJeff Bonwick
2503e14bb325SJeff Bonwick static void
zdb_ddt_leak_init(spa_t * spa,zdb_cb_t * zcb)2504bbfd46c4SJeff Bonwick zdb_ddt_leak_init(spa_t *spa, zdb_cb_t *zcb)
2505fa9e4066Sahrens {
2506bbfd46c4SJeff Bonwick ddt_bookmark_t ddb = { 0 };
2507b24ab676SJeff Bonwick ddt_entry_t dde;
2508b24ab676SJeff Bonwick int error;
2509fa9e4066Sahrens
2510bbfd46c4SJeff Bonwick while ((error = ddt_walk(spa, &ddb, &dde)) == 0) {
2511b24ab676SJeff Bonwick blkptr_t blk;
2512b24ab676SJeff Bonwick ddt_phys_t *ddp = dde.dde_phys;
2513bbfd46c4SJeff Bonwick
2514bbfd46c4SJeff Bonwick if (ddb.ddb_class == DDT_CLASS_UNIQUE)
2515bbfd46c4SJeff Bonwick return;
2516bbfd46c4SJeff Bonwick
2517b24ab676SJeff Bonwick ASSERT(ddt_phys_total_refcnt(&dde) > 1);
2518bbfd46c4SJeff Bonwick
2519b24ab676SJeff Bonwick for (int p = 0; p < DDT_PHYS_TYPES; p++, ddp++) {
2520b24ab676SJeff Bonwick if (ddp->ddp_phys_birth == 0)
2521b24ab676SJeff Bonwick continue;
2522bbfd46c4SJeff Bonwick ddt_bp_create(ddb.ddb_checksum,
2523bbfd46c4SJeff Bonwick &dde.dde_key, ddp, &blk);
2524b24ab676SJeff Bonwick if (p == DDT_PHYS_DITTO) {
2525cde58dbcSMatthew Ahrens zdb_count_block(zcb, NULL, &blk, ZDB_OT_DITTO);
2526b24ab676SJeff Bonwick } else {
2527b24ab676SJeff Bonwick zcb->zcb_dedup_asize +=
2528b24ab676SJeff Bonwick BP_GET_ASIZE(&blk) * (ddp->ddp_refcnt - 1);
2529b24ab676SJeff Bonwick zcb->zcb_dedup_blocks++;
2530b24ab676SJeff Bonwick }
2531b24ab676SJeff Bonwick }
2532b24ab676SJeff Bonwick if (!dump_opt['L']) {
2533bbfd46c4SJeff Bonwick ddt_t *ddt = spa->spa_ddt[ddb.ddb_checksum];
2534b24ab676SJeff Bonwick ddt_enter(ddt);
2535b24ab676SJeff Bonwick VERIFY(ddt_lookup(ddt, &blk, B_TRUE) != NULL);
2536b24ab676SJeff Bonwick ddt_exit(ddt);
2537b24ab676SJeff Bonwick }
2538b24ab676SJeff Bonwick }
2539b24ab676SJeff Bonwick
2540b24ab676SJeff Bonwick ASSERT(error == ENOENT);
2541b24ab676SJeff Bonwick }
2542b24ab676SJeff Bonwick
2543b24ab676SJeff Bonwick static void
zdb_leak_init(spa_t * spa,zdb_cb_t * zcb)2544b24ab676SJeff Bonwick zdb_leak_init(spa_t *spa, zdb_cb_t *zcb)
2545b24ab676SJeff Bonwick {
2546cde58dbcSMatthew Ahrens zcb->zcb_spa = spa;
2547cde58dbcSMatthew Ahrens
2548b24ab676SJeff Bonwick if (!dump_opt['L']) {
2549b24ab676SJeff Bonwick vdev_t *rvd = spa->spa_root_vdev;
2550fef64290SMatthew Ahrens for (uint64_t c = 0; c < rvd->vdev_children; c++) {
2551e14bb325SJeff Bonwick vdev_t *vd = rvd->vdev_child[c];
2552fef64290SMatthew Ahrens for (uint64_t m = 0; m < vd->vdev_ms_count; m++) {
2553fa9e4066Sahrens metaslab_t *msp = vd->vdev_ms[m];
2554fa9e4066Sahrens mutex_enter(&msp->ms_lock);
25550713e232SGeorge Wilson metaslab_unload(msp);
25560713e232SGeorge Wilson
25570713e232SGeorge Wilson /*
25580713e232SGeorge Wilson * For leak detection, we overload the metaslab
25590713e232SGeorge Wilson * ms_tree to contain allocated segments
25600713e232SGeorge Wilson * instead of free segments. As a result,
25610713e232SGeorge Wilson * we can't use the normal metaslab_load/unload
25620713e232SGeorge Wilson * interfaces.
25630713e232SGeorge Wilson */
25640713e232SGeorge Wilson if (msp->ms_sm != NULL) {
2565fef64290SMatthew Ahrens (void) fprintf(stderr,
2566fef64290SMatthew Ahrens "\rloading space map for "
2567fef64290SMatthew Ahrens "vdev %llu of %llu, "
2568fef64290SMatthew Ahrens "metaslab %llu of %llu ...",
2569fef64290SMatthew Ahrens (longlong_t)c,
2570fef64290SMatthew Ahrens (longlong_t)rvd->vdev_children,
2571fef64290SMatthew Ahrens (longlong_t)m,
2572fef64290SMatthew Ahrens (longlong_t)vd->vdev_ms_count);
2573fef64290SMatthew Ahrens
25740713e232SGeorge Wilson msp->ms_ops = &zdb_metaslab_ops;
257549b2c7b9SMatthew Ahrens
257649b2c7b9SMatthew Ahrens /*
257749b2c7b9SMatthew Ahrens * We don't want to spend the CPU
257849b2c7b9SMatthew Ahrens * manipulating the size-ordered
257949b2c7b9SMatthew Ahrens * tree, so clear the range_tree
258049b2c7b9SMatthew Ahrens * ops.
258149b2c7b9SMatthew Ahrens */
258249b2c7b9SMatthew Ahrens msp->ms_tree->rt_ops = NULL;
25830713e232SGeorge Wilson VERIFY0(space_map_load(msp->ms_sm,
25840713e232SGeorge Wilson msp->ms_tree, SM_ALLOC));
25850713e232SGeorge Wilson msp->ms_loaded = B_TRUE;
25860713e232SGeorge Wilson }
2587fa9e4066Sahrens mutex_exit(&msp->ms_lock);
2588fa9e4066Sahrens }
2589fa9e4066Sahrens }
2590fef64290SMatthew Ahrens (void) fprintf(stderr, "\n");
2591fa9e4066Sahrens }
2592fa9e4066Sahrens
2593b24ab676SJeff Bonwick spa_config_enter(spa, SCL_CONFIG, FTAG, RW_READER);
2594b24ab676SJeff Bonwick
2595bbfd46c4SJeff Bonwick zdb_ddt_leak_init(spa, zcb);
2596b24ab676SJeff Bonwick
2597b24ab676SJeff Bonwick spa_config_exit(spa, SCL_CONFIG, FTAG);
2598b24ab676SJeff Bonwick }
2599b24ab676SJeff Bonwick
2600fa9e4066Sahrens static void
zdb_leak_fini(spa_t * spa)2601e14bb325SJeff Bonwick zdb_leak_fini(spa_t *spa)
2602fa9e4066Sahrens {
2603b24ab676SJeff Bonwick if (!dump_opt['L']) {
2604fa9e4066Sahrens vdev_t *rvd = spa->spa_root_vdev;
2605e14bb325SJeff Bonwick for (int c = 0; c < rvd->vdev_children; c++) {
2606e14bb325SJeff Bonwick vdev_t *vd = rvd->vdev_child[c];
2607e14bb325SJeff Bonwick for (int m = 0; m < vd->vdev_ms_count; m++) {
2608e14bb325SJeff Bonwick metaslab_t *msp = vd->vdev_ms[m];
2609e14bb325SJeff Bonwick mutex_enter(&msp->ms_lock);
26100713e232SGeorge Wilson
26110713e232SGeorge Wilson /*
26120713e232SGeorge Wilson * The ms_tree has been overloaded to
26130713e232SGeorge Wilson * contain allocated segments. Now that we
26140713e232SGeorge Wilson * finished traversing all blocks, any
26150713e232SGeorge Wilson * block that remains in the ms_tree
26160713e232SGeorge Wilson * represents an allocated block that we
26170713e232SGeorge Wilson * did not claim during the traversal.
26180713e232SGeorge Wilson * Claimed blocks would have been removed
26190713e232SGeorge Wilson * from the ms_tree.
26200713e232SGeorge Wilson */
26210713e232SGeorge Wilson range_tree_vacate(msp->ms_tree, zdb_leak, vd);
26220713e232SGeorge Wilson msp->ms_loaded = B_FALSE;
26230713e232SGeorge Wilson
2624e14bb325SJeff Bonwick mutex_exit(&msp->ms_lock);
2625e14bb325SJeff Bonwick }
2626e14bb325SJeff Bonwick }
2627fa9e4066Sahrens }
2628fa9e4066Sahrens }
2629fa9e4066Sahrens
2630cde58dbcSMatthew Ahrens /* ARGSUSED */
2631cde58dbcSMatthew Ahrens static int
count_block_cb(void * arg,const blkptr_t * bp,dmu_tx_t * tx)2632cde58dbcSMatthew Ahrens count_block_cb(void *arg, const blkptr_t *bp, dmu_tx_t *tx)
2633cde58dbcSMatthew Ahrens {
2634cde58dbcSMatthew Ahrens zdb_cb_t *zcb = arg;
2635cde58dbcSMatthew Ahrens
2636490d05b9SMatthew Ahrens if (dump_opt['b'] >= 5) {
2637cde58dbcSMatthew Ahrens char blkbuf[BP_SPRINTF_LEN];
263849064978SMax Grossman snprintf_blkptr(blkbuf, sizeof (blkbuf), bp);
2639cde58dbcSMatthew Ahrens (void) printf("[%s] %s\n",
2640cde58dbcSMatthew Ahrens "deferred free", blkbuf);
2641cde58dbcSMatthew Ahrens }
2642cde58dbcSMatthew Ahrens zdb_count_block(zcb, NULL, bp, ZDB_OT_DEFERRED);
2643cde58dbcSMatthew Ahrens return (0);
2644cde58dbcSMatthew Ahrens }
2645cde58dbcSMatthew Ahrens
2646fa9e4066Sahrens static int
dump_block_stats(spa_t * spa)2647fa9e4066Sahrens dump_block_stats(spa_t *spa)
2648fa9e4066Sahrens {
2649fa9e4066Sahrens zdb_cb_t zcb = { 0 };
2650fa9e4066Sahrens zdb_blkstats_t *zb, *tzb;
2651b24ab676SJeff Bonwick uint64_t norm_alloc, norm_space, total_alloc, total_found;
2652cd088ea4SVictor Latushkin int flags = TRAVERSE_PRE | TRAVERSE_PREFETCH_METADATA | TRAVERSE_HARD;
2653e54e0be9SMatthew Ahrens boolean_t leaks = B_FALSE;
2654fa9e4066Sahrens
2655490d05b9SMatthew Ahrens (void) printf("\nTraversing all blocks %s%s%s%s%s...\n\n",
265682a0a985SVictor Latushkin (dump_opt['c'] || !dump_opt['L']) ? "to verify " : "",
26576365109dSVictor Latushkin (dump_opt['c'] == 1) ? "metadata " : "",
265882a0a985SVictor Latushkin dump_opt['c'] ? "checksums " : "",
265982a0a985SVictor Latushkin (dump_opt['c'] && !dump_opt['L']) ? "and verify " : "",
266082a0a985SVictor Latushkin !dump_opt['L'] ? "nothing leaked " : "");
2661fa9e4066Sahrens
2662fa9e4066Sahrens /*
2663e14bb325SJeff Bonwick * Load all space maps as SM_ALLOC maps, then traverse the pool
2664e14bb325SJeff Bonwick * claiming each block we discover. If the pool is perfectly
2665e14bb325SJeff Bonwick * consistent, the space maps will be empty when we're done.
2666e14bb325SJeff Bonwick * Anything left over is a leak; any block we can't claim (because
2667e14bb325SJeff Bonwick * it's not part of any space map) is a double allocation,
2668e14bb325SJeff Bonwick * reference to a freed block, or an unclaimed log block.
2669fa9e4066Sahrens */
2670b24ab676SJeff Bonwick zdb_leak_init(spa, &zcb);
2671fa9e4066Sahrens
2672fa9e4066Sahrens /*
2673fa9e4066Sahrens * If there's a deferred-free bplist, process that first.
2674fa9e4066Sahrens */
2675cde58dbcSMatthew Ahrens (void) bpobj_iterate_nofree(&spa->spa_deferred_bpobj,
2676cde58dbcSMatthew Ahrens count_block_cb, &zcb, NULL);
26773b2aab18SMatthew Ahrens if (spa_version(spa) >= SPA_VERSION_DEADLISTS) {
2678cde58dbcSMatthew Ahrens (void) bpobj_iterate_nofree(&spa->spa_dsl_pool->dp_free_bpobj,
2679cde58dbcSMatthew Ahrens count_block_cb, &zcb, NULL);
26803b2aab18SMatthew Ahrens }
2681dbc43af2SMatthew Ahrens if (spa_feature_is_active(spa, SPA_FEATURE_ASYNC_DESTROY)) {
2682b420f3adSRichard Lowe VERIFY3U(0, ==, bptree_iterate(spa->spa_meta_objset,
2683ad135b5dSChristopher Siden spa->spa_dsl_pool->dp_bptree_obj, B_FALSE, count_block_cb,
2684ad135b5dSChristopher Siden &zcb, NULL));
2685ad135b5dSChristopher Siden }
2686fa9e4066Sahrens
2687bbfd46c4SJeff Bonwick if (dump_opt['c'] > 1)
2688bbfd46c4SJeff Bonwick flags |= TRAVERSE_PREFETCH_DATA;
2689bbfd46c4SJeff Bonwick
2690490d05b9SMatthew Ahrens zcb.zcb_totalasize = metaslab_class_get_alloc(spa_normal_class(spa));
2691490d05b9SMatthew Ahrens zcb.zcb_start = zcb.zcb_lastprint = gethrtime();
2692bbfd46c4SJeff Bonwick zcb.zcb_haderrors |= traverse_pool(spa, 0, flags, zdb_blkptr_cb, &zcb);
2693fa9e4066Sahrens
269431d7e8faSGeorge Wilson /*
269531d7e8faSGeorge Wilson * If we've traversed the data blocks then we need to wait for those
269631d7e8faSGeorge Wilson * I/Os to complete. We leverage "The Godfather" zio to wait on
269731d7e8faSGeorge Wilson * all async I/Os to complete.
269831d7e8faSGeorge Wilson */
269931d7e8faSGeorge Wilson if (dump_opt['c']) {
27009682648eSMatthew Ahrens for (int i = 0; i < max_ncpus; i++) {
27019682648eSMatthew Ahrens (void) zio_wait(spa->spa_async_zio_root[i]);
27029682648eSMatthew Ahrens spa->spa_async_zio_root[i] = zio_root(spa, NULL, NULL,
270331d7e8faSGeorge Wilson ZIO_FLAG_CANFAIL | ZIO_FLAG_SPECULATIVE |
270431d7e8faSGeorge Wilson ZIO_FLAG_GODFATHER);
270531d7e8faSGeorge Wilson }
27069682648eSMatthew Ahrens }
270731d7e8faSGeorge Wilson
2708b24ab676SJeff Bonwick if (zcb.zcb_haderrors) {
2709fa9e4066Sahrens (void) printf("\nError counts:\n\n");
2710fa9e4066Sahrens (void) printf("\t%5s %s\n", "errno", "count");
2711b24ab676SJeff Bonwick for (int e = 0; e < 256; e++) {
2712fa9e4066Sahrens if (zcb.zcb_errors[e] != 0) {
2713fa9e4066Sahrens (void) printf("\t%5d %llu\n",
2714fa9e4066Sahrens e, (u_longlong_t)zcb.zcb_errors[e]);
2715fa9e4066Sahrens }
2716fa9e4066Sahrens }
2717fa9e4066Sahrens }
2718fa9e4066Sahrens
2719fa9e4066Sahrens /*
2720fa9e4066Sahrens * Report any leaked segments.
2721fa9e4066Sahrens */
2722e14bb325SJeff Bonwick zdb_leak_fini(spa);
2723fa9e4066Sahrens
2724b24ab676SJeff Bonwick tzb = &zcb.zcb_type[ZB_TOTAL][ZDB_OT_TOTAL];
2725d41e7643Sek110237
2726b24ab676SJeff Bonwick norm_alloc = metaslab_class_get_alloc(spa_normal_class(spa));
2727b24ab676SJeff Bonwick norm_space = metaslab_class_get_space(spa_normal_class(spa));
2728fa9e4066Sahrens
2729b24ab676SJeff Bonwick total_alloc = norm_alloc + metaslab_class_get_alloc(spa_log_class(spa));
2730b24ab676SJeff Bonwick total_found = tzb->zb_asize - zcb.zcb_dedup_asize;
27318654d025Sperrin
2732b24ab676SJeff Bonwick if (total_found == total_alloc) {
273382a0a985SVictor Latushkin if (!dump_opt['L'])
2734fa9e4066Sahrens (void) printf("\n\tNo leaks (block sum matches space"
2735fa9e4066Sahrens " maps exactly)\n");
2736fa9e4066Sahrens } else {
2737fa9e4066Sahrens (void) printf("block traversal size %llu != alloc %llu "
273882a0a985SVictor Latushkin "(%s %lld)\n",
2739b24ab676SJeff Bonwick (u_longlong_t)total_found,
2740b24ab676SJeff Bonwick (u_longlong_t)total_alloc,
274182a0a985SVictor Latushkin (dump_opt['L']) ? "unreachable" : "leaked",
2742b24ab676SJeff Bonwick (longlong_t)(total_alloc - total_found));
2743e54e0be9SMatthew Ahrens leaks = B_TRUE;
2744fa9e4066Sahrens }
2745fa9e4066Sahrens
2746fa9e4066Sahrens if (tzb->zb_count == 0)
2747fa9e4066Sahrens return (2);
2748fa9e4066Sahrens
2749fa9e4066Sahrens (void) printf("\n");
2750fa9e4066Sahrens (void) printf("\tbp count: %10llu\n",
2751fa9e4066Sahrens (u_longlong_t)tzb->zb_count);
2752d5ee8a13SMatthew Ahrens (void) printf("\tganged count: %10llu\n",
2753d5ee8a13SMatthew Ahrens (longlong_t)tzb->zb_gangs);
2754b24ab676SJeff Bonwick (void) printf("\tbp logical: %10llu avg: %6llu\n",
2755fa9e4066Sahrens (u_longlong_t)tzb->zb_lsize,
2756fa9e4066Sahrens (u_longlong_t)(tzb->zb_lsize / tzb->zb_count));
2757b24ab676SJeff Bonwick (void) printf("\tbp physical: %10llu avg:"
2758b24ab676SJeff Bonwick " %6llu compression: %6.2f\n",
2759fa9e4066Sahrens (u_longlong_t)tzb->zb_psize,
2760fa9e4066Sahrens (u_longlong_t)(tzb->zb_psize / tzb->zb_count),
2761fa9e4066Sahrens (double)tzb->zb_lsize / tzb->zb_psize);
2762b24ab676SJeff Bonwick (void) printf("\tbp allocated: %10llu avg:"
2763b24ab676SJeff Bonwick " %6llu compression: %6.2f\n",
2764fa9e4066Sahrens (u_longlong_t)tzb->zb_asize,
2765fa9e4066Sahrens (u_longlong_t)(tzb->zb_asize / tzb->zb_count),
2766fa9e4066Sahrens (double)tzb->zb_lsize / tzb->zb_asize);
2767b24ab676SJeff Bonwick (void) printf("\tbp deduped: %10llu ref>1:"
2768b24ab676SJeff Bonwick " %6llu deduplication: %6.2f\n",
2769b24ab676SJeff Bonwick (u_longlong_t)zcb.zcb_dedup_asize,
2770b24ab676SJeff Bonwick (u_longlong_t)zcb.zcb_dedup_blocks,
2771b24ab676SJeff Bonwick (double)zcb.zcb_dedup_asize / tzb->zb_asize + 1.0);
2772b24ab676SJeff Bonwick (void) printf("\tSPA allocated: %10llu used: %5.2f%%\n",
2773b24ab676SJeff Bonwick (u_longlong_t)norm_alloc, 100.0 * norm_alloc / norm_space);
2774fa9e4066Sahrens
2775e54e0be9SMatthew Ahrens for (bp_embedded_type_t i = 0; i < NUM_BP_EMBEDDED_TYPES; i++) {
2776e54e0be9SMatthew Ahrens if (zcb.zcb_embedded_blocks[i] == 0)
2777e54e0be9SMatthew Ahrens continue;
2778e54e0be9SMatthew Ahrens (void) printf("\n");
2779e54e0be9SMatthew Ahrens (void) printf("\tadditional, non-pointer bps of type %u: "
2780e54e0be9SMatthew Ahrens "%10llu\n",
2781e54e0be9SMatthew Ahrens i, (u_longlong_t)zcb.zcb_embedded_blocks[i]);
2782e54e0be9SMatthew Ahrens
2783e54e0be9SMatthew Ahrens if (dump_opt['b'] >= 3) {
2784e54e0be9SMatthew Ahrens (void) printf("\t number of (compressed) bytes: "
2785e54e0be9SMatthew Ahrens "number of bps\n");
2786e54e0be9SMatthew Ahrens dump_histogram(zcb.zcb_embedded_histogram[i],
2787e54e0be9SMatthew Ahrens sizeof (zcb.zcb_embedded_histogram[i]) /
2788e54e0be9SMatthew Ahrens sizeof (zcb.zcb_embedded_histogram[i][0]), 0);
2789e54e0be9SMatthew Ahrens }
2790e54e0be9SMatthew Ahrens }
2791e54e0be9SMatthew Ahrens
2792d5ee8a13SMatthew Ahrens if (tzb->zb_ditto_samevdev != 0) {
2793d5ee8a13SMatthew Ahrens (void) printf("\tDittoed blocks on same vdev: %llu\n",
2794d5ee8a13SMatthew Ahrens (longlong_t)tzb->zb_ditto_samevdev);
2795d5ee8a13SMatthew Ahrens }
2796d5ee8a13SMatthew Ahrens
2797fa9e4066Sahrens if (dump_opt['b'] >= 2) {
2798fa9e4066Sahrens int l, t, level;
2799fa9e4066Sahrens (void) printf("\nBlocks\tLSIZE\tPSIZE\tASIZE"
2800fa9e4066Sahrens "\t avg\t comp\t%%Total\tType\n");
2801fa9e4066Sahrens
2802b24ab676SJeff Bonwick for (t = 0; t <= ZDB_OT_TOTAL; t++) {
28033f9d6ad7SLin Ling char csize[32], lsize[32], psize[32], asize[32];
2804d5ee8a13SMatthew Ahrens char avg[32], gang[32];
2805fa9e4066Sahrens char *typename;
2806fa9e4066Sahrens
2807b24ab676SJeff Bonwick if (t < DMU_OT_NUMTYPES)
2808b24ab676SJeff Bonwick typename = dmu_ot[t].ot_name;
2809b24ab676SJeff Bonwick else
2810b24ab676SJeff Bonwick typename = zdb_ot_extname[t - DMU_OT_NUMTYPES];
2811fa9e4066Sahrens
2812fa9e4066Sahrens if (zcb.zcb_type[ZB_TOTAL][t].zb_asize == 0) {
2813fa9e4066Sahrens (void) printf("%6s\t%5s\t%5s\t%5s"
2814fa9e4066Sahrens "\t%5s\t%5s\t%6s\t%s\n",
2815fa9e4066Sahrens "-",
2816fa9e4066Sahrens "-",
2817fa9e4066Sahrens "-",
2818fa9e4066Sahrens "-",
2819fa9e4066Sahrens "-",
2820fa9e4066Sahrens "-",
2821fa9e4066Sahrens "-",
2822fa9e4066Sahrens typename);
2823fa9e4066Sahrens continue;
2824fa9e4066Sahrens }
2825fa9e4066Sahrens
2826fa9e4066Sahrens for (l = ZB_TOTAL - 1; l >= -1; l--) {
2827fa9e4066Sahrens level = (l == -1 ? ZB_TOTAL : l);
2828fa9e4066Sahrens zb = &zcb.zcb_type[level][t];
2829fa9e4066Sahrens
2830fa9e4066Sahrens if (zb->zb_asize == 0)
2831fa9e4066Sahrens continue;
2832fa9e4066Sahrens
2833fa9e4066Sahrens if (dump_opt['b'] < 3 && level != ZB_TOTAL)
2834fa9e4066Sahrens continue;
2835fa9e4066Sahrens
2836fa9e4066Sahrens if (level == 0 && zb->zb_asize ==
2837fa9e4066Sahrens zcb.zcb_type[ZB_TOTAL][t].zb_asize)
2838fa9e4066Sahrens continue;
2839fa9e4066Sahrens
28403f9d6ad7SLin Ling zdb_nicenum(zb->zb_count, csize);
28413f9d6ad7SLin Ling zdb_nicenum(zb->zb_lsize, lsize);
28423f9d6ad7SLin Ling zdb_nicenum(zb->zb_psize, psize);
28433f9d6ad7SLin Ling zdb_nicenum(zb->zb_asize, asize);
28443f9d6ad7SLin Ling zdb_nicenum(zb->zb_asize / zb->zb_count, avg);
2845d5ee8a13SMatthew Ahrens zdb_nicenum(zb->zb_gangs, gang);
2846fa9e4066Sahrens
2847fa9e4066Sahrens (void) printf("%6s\t%5s\t%5s\t%5s\t%5s"
2848fa9e4066Sahrens "\t%5.2f\t%6.2f\t",
2849fa9e4066Sahrens csize, lsize, psize, asize, avg,
2850fa9e4066Sahrens (double)zb->zb_lsize / zb->zb_psize,
2851fa9e4066Sahrens 100.0 * zb->zb_asize / tzb->zb_asize);
2852fa9e4066Sahrens
2853fa9e4066Sahrens if (level == ZB_TOTAL)
2854fa9e4066Sahrens (void) printf("%s\n", typename);
2855fa9e4066Sahrens else
2856fa9e4066Sahrens (void) printf(" L%d %s\n",
2857fa9e4066Sahrens level, typename);
2858490d05b9SMatthew Ahrens
2859d5ee8a13SMatthew Ahrens if (dump_opt['b'] >= 3 && zb->zb_gangs > 0) {
2860d5ee8a13SMatthew Ahrens (void) printf("\t number of ganged "
2861d5ee8a13SMatthew Ahrens "blocks: %s\n", gang);
2862d5ee8a13SMatthew Ahrens }
2863d5ee8a13SMatthew Ahrens
2864490d05b9SMatthew Ahrens if (dump_opt['b'] >= 4) {
2865490d05b9SMatthew Ahrens (void) printf("psize "
2866490d05b9SMatthew Ahrens "(in 512-byte sectors): "
2867490d05b9SMatthew Ahrens "number of blocks\n");
2868490d05b9SMatthew Ahrens dump_histogram(zb->zb_psize_histogram,
28690713e232SGeorge Wilson PSIZE_HISTO_SIZE, 0);
2870490d05b9SMatthew Ahrens }
2871fa9e4066Sahrens }
2872fa9e4066Sahrens }
2873fa9e4066Sahrens }
2874fa9e4066Sahrens
2875fa9e4066Sahrens (void) printf("\n");
2876fa9e4066Sahrens
2877fa9e4066Sahrens if (leaks)
2878fa9e4066Sahrens return (2);
2879fa9e4066Sahrens
2880fa9e4066Sahrens if (zcb.zcb_haderrors)
2881fa9e4066Sahrens return (3);
2882fa9e4066Sahrens
2883fa9e4066Sahrens return (0);
2884fa9e4066Sahrens }
2885fa9e4066Sahrens
2886b24ab676SJeff Bonwick typedef struct zdb_ddt_entry {
2887b24ab676SJeff Bonwick ddt_key_t zdde_key;
2888b24ab676SJeff Bonwick uint64_t zdde_ref_blocks;
2889b24ab676SJeff Bonwick uint64_t zdde_ref_lsize;
2890b24ab676SJeff Bonwick uint64_t zdde_ref_psize;
2891b24ab676SJeff Bonwick uint64_t zdde_ref_dsize;
2892b24ab676SJeff Bonwick avl_node_t zdde_node;
2893b24ab676SJeff Bonwick } zdb_ddt_entry_t;
2894b24ab676SJeff Bonwick
2895b24ab676SJeff Bonwick /* ARGSUSED */
2896b24ab676SJeff Bonwick static int
zdb_ddt_add_cb(spa_t * spa,zilog_t * zilog,const blkptr_t * bp,const zbookmark_phys_t * zb,const dnode_phys_t * dnp,void * arg)2897b24ab676SJeff Bonwick zdb_ddt_add_cb(spa_t *spa, zilog_t *zilog, const blkptr_t *bp,
289805d95d03SMatthew Ahrens const zbookmark_phys_t *zb, const dnode_phys_t *dnp, void *arg)
2899b24ab676SJeff Bonwick {
2900b24ab676SJeff Bonwick avl_tree_t *t = arg;
2901b24ab676SJeff Bonwick avl_index_t where;
2902b24ab676SJeff Bonwick zdb_ddt_entry_t *zdde, zdde_search;
2903b24ab676SJeff Bonwick
29045f9bb2f3SPaul Dagnelie if (bp == NULL || BP_IS_HOLE(bp) || BP_IS_EMBEDDED(bp))
2905b24ab676SJeff Bonwick return (0);
2906b24ab676SJeff Bonwick
2907b24ab676SJeff Bonwick if (dump_opt['S'] > 1 && zb->zb_level == ZB_ROOT_LEVEL) {
2908b24ab676SJeff Bonwick (void) printf("traversing objset %llu, %llu objects, "
2909b24ab676SJeff Bonwick "%lu blocks so far\n",
2910b24ab676SJeff Bonwick (u_longlong_t)zb->zb_objset,
2911e54e0be9SMatthew Ahrens (u_longlong_t)BP_GET_FILL(bp),
2912b24ab676SJeff Bonwick avl_numnodes(t));
2913b24ab676SJeff Bonwick }
2914b24ab676SJeff Bonwick
2915bbfd46c4SJeff Bonwick if (BP_IS_HOLE(bp) || BP_GET_CHECKSUM(bp) == ZIO_CHECKSUM_OFF ||
2916ad135b5dSChristopher Siden BP_GET_LEVEL(bp) > 0 || DMU_OT_IS_METADATA(BP_GET_TYPE(bp)))
2917b24ab676SJeff Bonwick return (0);
2918b24ab676SJeff Bonwick
2919b24ab676SJeff Bonwick ddt_key_fill(&zdde_search.zdde_key, bp);
2920b24ab676SJeff Bonwick
2921b24ab676SJeff Bonwick zdde = avl_find(t, &zdde_search, &where);
2922b24ab676SJeff Bonwick
2923b24ab676SJeff Bonwick if (zdde == NULL) {
2924b24ab676SJeff Bonwick zdde = umem_zalloc(sizeof (*zdde), UMEM_NOFAIL);
2925b24ab676SJeff Bonwick zdde->zdde_key = zdde_search.zdde_key;
2926b24ab676SJeff Bonwick avl_insert(t, zdde, where);
2927b24ab676SJeff Bonwick }
2928b24ab676SJeff Bonwick
2929b24ab676SJeff Bonwick zdde->zdde_ref_blocks += 1;
2930b24ab676SJeff Bonwick zdde->zdde_ref_lsize += BP_GET_LSIZE(bp);
2931b24ab676SJeff Bonwick zdde->zdde_ref_psize += BP_GET_PSIZE(bp);
2932b24ab676SJeff Bonwick zdde->zdde_ref_dsize += bp_get_dsize_sync(spa, bp);
2933b24ab676SJeff Bonwick
2934b24ab676SJeff Bonwick return (0);
2935b24ab676SJeff Bonwick }
2936b24ab676SJeff Bonwick
2937b24ab676SJeff Bonwick static void
dump_simulated_ddt(spa_t * spa)2938b24ab676SJeff Bonwick dump_simulated_ddt(spa_t *spa)
2939b24ab676SJeff Bonwick {
2940b24ab676SJeff Bonwick avl_tree_t t;
2941b24ab676SJeff Bonwick void *cookie = NULL;
2942b24ab676SJeff Bonwick zdb_ddt_entry_t *zdde;
2943b24ab676SJeff Bonwick ddt_histogram_t ddh_total = { 0 };
2944b24ab676SJeff Bonwick ddt_stat_t dds_total = { 0 };
2945b24ab676SJeff Bonwick
2946b24ab676SJeff Bonwick avl_create(&t, ddt_entry_compare,
2947b24ab676SJeff Bonwick sizeof (zdb_ddt_entry_t), offsetof(zdb_ddt_entry_t, zdde_node));
2948b24ab676SJeff Bonwick
2949b24ab676SJeff Bonwick spa_config_enter(spa, SCL_CONFIG, FTAG, RW_READER);
2950b24ab676SJeff Bonwick
2951bbfd46c4SJeff Bonwick (void) traverse_pool(spa, 0, TRAVERSE_PRE | TRAVERSE_PREFETCH_METADATA,
2952bbfd46c4SJeff Bonwick zdb_ddt_add_cb, &t);
2953b24ab676SJeff Bonwick
2954b24ab676SJeff Bonwick spa_config_exit(spa, SCL_CONFIG, FTAG);
2955b24ab676SJeff Bonwick
2956b24ab676SJeff Bonwick while ((zdde = avl_destroy_nodes(&t, &cookie)) != NULL) {
2957b24ab676SJeff Bonwick ddt_stat_t dds;
2958b24ab676SJeff Bonwick uint64_t refcnt = zdde->zdde_ref_blocks;
2959b24ab676SJeff Bonwick ASSERT(refcnt != 0);
2960b24ab676SJeff Bonwick
2961b24ab676SJeff Bonwick dds.dds_blocks = zdde->zdde_ref_blocks / refcnt;
2962b24ab676SJeff Bonwick dds.dds_lsize = zdde->zdde_ref_lsize / refcnt;
2963b24ab676SJeff Bonwick dds.dds_psize = zdde->zdde_ref_psize / refcnt;
2964b24ab676SJeff Bonwick dds.dds_dsize = zdde->zdde_ref_dsize / refcnt;
2965b24ab676SJeff Bonwick
2966b24ab676SJeff Bonwick dds.dds_ref_blocks = zdde->zdde_ref_blocks;
2967b24ab676SJeff Bonwick dds.dds_ref_lsize = zdde->zdde_ref_lsize;
2968b24ab676SJeff Bonwick dds.dds_ref_psize = zdde->zdde_ref_psize;
2969b24ab676SJeff Bonwick dds.dds_ref_dsize = zdde->zdde_ref_dsize;
2970b24ab676SJeff Bonwick
2971887cdfc7SMatthew Ahrens ddt_stat_add(&ddh_total.ddh_stat[highbit64(refcnt) - 1],
2972887cdfc7SMatthew Ahrens &dds, 0);
2973b24ab676SJeff Bonwick
2974b24ab676SJeff Bonwick umem_free(zdde, sizeof (*zdde));
2975b24ab676SJeff Bonwick }
2976b24ab676SJeff Bonwick
2977b24ab676SJeff Bonwick avl_destroy(&t);
2978b24ab676SJeff Bonwick
2979b24ab676SJeff Bonwick ddt_histogram_stat(&dds_total, &ddh_total);
2980b24ab676SJeff Bonwick
2981b24ab676SJeff Bonwick (void) printf("Simulated DDT histogram:\n");
2982b24ab676SJeff Bonwick
29839eb19f4dSGeorge Wilson zpool_dump_ddt(&dds_total, &ddh_total);
2984b24ab676SJeff Bonwick
2985b24ab676SJeff Bonwick dump_dedup_ratio(&dds_total);
2986b24ab676SJeff Bonwick }
2987b24ab676SJeff Bonwick
2988fa9e4066Sahrens static void
dump_zpool(spa_t * spa)2989fa9e4066Sahrens dump_zpool(spa_t *spa)
2990fa9e4066Sahrens {
2991fa9e4066Sahrens dsl_pool_t *dp = spa_get_dsl(spa);
2992fa9e4066Sahrens int rc = 0;
2993fa9e4066Sahrens
2994b24ab676SJeff Bonwick if (dump_opt['S']) {
2995b24ab676SJeff Bonwick dump_simulated_ddt(spa);
2996b24ab676SJeff Bonwick return;
2997b24ab676SJeff Bonwick }
2998b24ab676SJeff Bonwick
299907428bdfSVictor Latushkin if (!dump_opt['e'] && dump_opt['C'] > 1) {
300007428bdfSVictor Latushkin (void) printf("\nCached configuration:\n");
300107428bdfSVictor Latushkin dump_nvlist(spa->spa_config, 8);
300207428bdfSVictor Latushkin }
300307428bdfSVictor Latushkin
300407428bdfSVictor Latushkin if (dump_opt['C'])
300507428bdfSVictor Latushkin dump_config(spa);
300607428bdfSVictor Latushkin
3007fa9e4066Sahrens if (dump_opt['u'])
300853b9a4a9SVictor Latushkin dump_uberblock(&spa->spa_uberblock, "\nUberblock:\n", "\n");
3009fa9e4066Sahrens
3010b24ab676SJeff Bonwick if (dump_opt['D'])
3011b24ab676SJeff Bonwick dump_all_ddts(spa);
3012b24ab676SJeff Bonwick
301387219db7SVictor Latushkin if (dump_opt['d'] > 2 || dump_opt['m'])
301487219db7SVictor Latushkin dump_metaslabs(spa);
301502b647c5SGeorge Wilson if (dump_opt['M'])
301602b647c5SGeorge Wilson dump_metaslab_groups(spa);
301787219db7SVictor Latushkin
301887219db7SVictor Latushkin if (dump_opt['d'] || dump_opt['i']) {
3019fa9e4066Sahrens dump_dir(dp->dp_meta_objset);
3020fa9e4066Sahrens if (dump_opt['d'] >= 3) {
3021bf263f2aSMatthew Ahrens dump_full_bpobj(&spa->spa_deferred_bpobj,
3022d0475637SMatthew Ahrens "Deferred frees", 0);
3023cde58dbcSMatthew Ahrens if (spa_version(spa) >= SPA_VERSION_DEADLISTS) {
3024bf263f2aSMatthew Ahrens dump_full_bpobj(
3025bf263f2aSMatthew Ahrens &spa->spa_dsl_pool->dp_free_bpobj,
3026d0475637SMatthew Ahrens "Pool snapshot frees", 0);
3027ad135b5dSChristopher Siden }
3028ad135b5dSChristopher Siden
3029ad135b5dSChristopher Siden if (spa_feature_is_active(spa,
3030dbc43af2SMatthew Ahrens SPA_FEATURE_ASYNC_DESTROY)) {
3031ad135b5dSChristopher Siden dump_bptree(spa->spa_meta_objset,
3032ad135b5dSChristopher Siden spa->spa_dsl_pool->dp_bptree_obj,
3033ad135b5dSChristopher Siden "Pool dataset frees");
3034cde58dbcSMatthew Ahrens }
3035fa9e4066Sahrens dump_dtl(spa->spa_root_vdev, 0);
3036fa9e4066Sahrens }
303707428bdfSVictor Latushkin (void) dmu_objset_find(spa_name(spa), dump_one_dir,
303807428bdfSVictor Latushkin NULL, DS_FIND_SNAPSHOTS | DS_FIND_CHILDREN);
3039d1a98260SMatthew Ahrens
30400cc589a4SMatthew Ahrens for (spa_feature_t f = 0; f < SPA_FEATURES; f++) {
30410cc589a4SMatthew Ahrens uint64_t refcount;
30420cc589a4SMatthew Ahrens
30430cc589a4SMatthew Ahrens if (!(spa_feature_table[f].fi_flags &
30440cc589a4SMatthew Ahrens ZFEATURE_FLAG_PER_DATASET)) {
30450cc589a4SMatthew Ahrens ASSERT0(dataset_feature_count[f]);
30460cc589a4SMatthew Ahrens continue;
30470cc589a4SMatthew Ahrens }
3048d1a98260SMatthew Ahrens (void) feature_get_refcount(spa,
30490cc589a4SMatthew Ahrens &spa_feature_table[f], &refcount);
30500cc589a4SMatthew Ahrens if (dataset_feature_count[f] != refcount) {
30510cc589a4SMatthew Ahrens (void) printf("%s feature refcount mismatch: "
30520cc589a4SMatthew Ahrens "%lld datasets != %lld refcount\n",
30530cc589a4SMatthew Ahrens spa_feature_table[f].fi_uname,
30540cc589a4SMatthew Ahrens (longlong_t)dataset_feature_count[f],
3055d1a98260SMatthew Ahrens (longlong_t)refcount);
3056d1a98260SMatthew Ahrens rc = 2;
3057d1a98260SMatthew Ahrens } else {
30580cc589a4SMatthew Ahrens (void) printf("Verified %s feature refcount "
30590cc589a4SMatthew Ahrens "of %llu is correct\n",
30600cc589a4SMatthew Ahrens spa_feature_table[f].fi_uname,
30610cc589a4SMatthew Ahrens (longlong_t)refcount);
30620cc589a4SMatthew Ahrens }
3063fa9e4066Sahrens }
3064d1a98260SMatthew Ahrens }
3065d1a98260SMatthew Ahrens if (rc == 0 && (dump_opt['b'] || dump_opt['c']))
3066fa9e4066Sahrens rc = dump_block_stats(spa);
3067fa9e4066Sahrens
30680713e232SGeorge Wilson if (rc == 0)
30690713e232SGeorge Wilson rc = verify_spacemap_refcounts(spa);
30700713e232SGeorge Wilson
3071fa9e4066Sahrens if (dump_opt['s'])
3072fa9e4066Sahrens show_pool_stats(spa);
3073fa9e4066Sahrens
30748f18d1faSGeorge Wilson if (dump_opt['h'])
30758f18d1faSGeorge Wilson dump_history(spa);
30768f18d1faSGeorge Wilson
3077fa9e4066Sahrens if (rc != 0)
3078fa9e4066Sahrens exit(rc);
3079fa9e4066Sahrens }
3080fa9e4066Sahrens
308144cd46caSbillm #define ZDB_FLAG_CHECKSUM 0x0001
308244cd46caSbillm #define ZDB_FLAG_DECOMPRESS 0x0002
308344cd46caSbillm #define ZDB_FLAG_BSWAP 0x0004
308444cd46caSbillm #define ZDB_FLAG_GBH 0x0008
308544cd46caSbillm #define ZDB_FLAG_INDIRECT 0x0010
308644cd46caSbillm #define ZDB_FLAG_PHYS 0x0020
308744cd46caSbillm #define ZDB_FLAG_RAW 0x0040
308844cd46caSbillm #define ZDB_FLAG_PRINT_BLKPTR 0x0080
308944cd46caSbillm
309044cd46caSbillm int flagbits[256];
309144cd46caSbillm
309244cd46caSbillm static void
zdb_print_blkptr(blkptr_t * bp,int flags)309344cd46caSbillm zdb_print_blkptr(blkptr_t *bp, int flags)
309444cd46caSbillm {
3095b24ab676SJeff Bonwick char blkbuf[BP_SPRINTF_LEN];
309644cd46caSbillm
309744cd46caSbillm if (flags & ZDB_FLAG_BSWAP)
309844cd46caSbillm byteswap_uint64_array((void *)bp, sizeof (blkptr_t));
3099b24ab676SJeff Bonwick
310049064978SMax Grossman snprintf_blkptr(blkbuf, sizeof (blkbuf), bp);
3101b24ab676SJeff Bonwick (void) printf("%s\n", blkbuf);
310244cd46caSbillm }
310344cd46caSbillm
310444cd46caSbillm static void
zdb_dump_indirect(blkptr_t * bp,int nbps,int flags)310544cd46caSbillm zdb_dump_indirect(blkptr_t *bp, int nbps, int flags)
310644cd46caSbillm {
310744cd46caSbillm int i;
310844cd46caSbillm
310944cd46caSbillm for (i = 0; i < nbps; i++)
311044cd46caSbillm zdb_print_blkptr(&bp[i], flags);
311144cd46caSbillm }
311244cd46caSbillm
311344cd46caSbillm static void
zdb_dump_gbh(void * buf,int flags)311444cd46caSbillm zdb_dump_gbh(void *buf, int flags)
311544cd46caSbillm {
311644cd46caSbillm zdb_dump_indirect((blkptr_t *)buf, SPA_GBH_NBLKPTRS, flags);
311744cd46caSbillm }
311844cd46caSbillm
311944cd46caSbillm static void
zdb_dump_block_raw(void * buf,uint64_t size,int flags)312044cd46caSbillm zdb_dump_block_raw(void *buf, uint64_t size, int flags)
312144cd46caSbillm {
312244cd46caSbillm if (flags & ZDB_FLAG_BSWAP)
312344cd46caSbillm byteswap_uint64_array(buf, size);
3124b24ab676SJeff Bonwick (void) write(1, buf, size);
312544cd46caSbillm }
312644cd46caSbillm
312744cd46caSbillm static void
zdb_dump_block(char * label,void * buf,uint64_t size,int flags)312844cd46caSbillm zdb_dump_block(char *label, void *buf, uint64_t size, int flags)
312944cd46caSbillm {
313044cd46caSbillm uint64_t *d = (uint64_t *)buf;
313144cd46caSbillm int nwords = size / sizeof (uint64_t);
313244cd46caSbillm int do_bswap = !!(flags & ZDB_FLAG_BSWAP);
313344cd46caSbillm int i, j;
313444cd46caSbillm char *hdr, *c;
313544cd46caSbillm
313644cd46caSbillm
313744cd46caSbillm if (do_bswap)
313844cd46caSbillm hdr = " 7 6 5 4 3 2 1 0 f e d c b a 9 8";
313944cd46caSbillm else
314044cd46caSbillm hdr = " 0 1 2 3 4 5 6 7 8 9 a b c d e f";
314144cd46caSbillm
314244cd46caSbillm (void) printf("\n%s\n%6s %s 0123456789abcdef\n", label, "", hdr);
314344cd46caSbillm
314444cd46caSbillm for (i = 0; i < nwords; i += 2) {
314544cd46caSbillm (void) printf("%06llx: %016llx %016llx ",
314644cd46caSbillm (u_longlong_t)(i * sizeof (uint64_t)),
314744cd46caSbillm (u_longlong_t)(do_bswap ? BSWAP_64(d[i]) : d[i]),
314844cd46caSbillm (u_longlong_t)(do_bswap ? BSWAP_64(d[i + 1]) : d[i + 1]));
314944cd46caSbillm
315044cd46caSbillm c = (char *)&d[i];
315144cd46caSbillm for (j = 0; j < 2 * sizeof (uint64_t); j++)
315244cd46caSbillm (void) printf("%c", isprint(c[j]) ? c[j] : '.');
315344cd46caSbillm (void) printf("\n");
315444cd46caSbillm }
315544cd46caSbillm }
315644cd46caSbillm
315744cd46caSbillm /*
315844cd46caSbillm * There are two acceptable formats:
315944cd46caSbillm * leaf_name - For example: c1t0d0 or /tmp/ztest.0a
316044cd46caSbillm * child[.child]* - For example: 0.1.1
316144cd46caSbillm *
316244cd46caSbillm * The second form can be used to specify arbitrary vdevs anywhere
316344cd46caSbillm * in the heirarchy. For example, in a pool with a mirror of
316444cd46caSbillm * RAID-Zs, you can specify either RAID-Z vdev with 0.0 or 0.1 .
316544cd46caSbillm */
316644cd46caSbillm static vdev_t *
zdb_vdev_lookup(vdev_t * vdev,char * path)316744cd46caSbillm zdb_vdev_lookup(vdev_t *vdev, char *path)
316844cd46caSbillm {
316944cd46caSbillm char *s, *p, *q;
317044cd46caSbillm int i;
317144cd46caSbillm
317244cd46caSbillm if (vdev == NULL)
317344cd46caSbillm return (NULL);
317444cd46caSbillm
317544cd46caSbillm /* First, assume the x.x.x.x format */
317644cd46caSbillm i = (int)strtoul(path, &s, 10);
317744cd46caSbillm if (s == path || (s && *s != '.' && *s != '\0'))
317844cd46caSbillm goto name;
317944cd46caSbillm if (i < 0 || i >= vdev->vdev_children)
318044cd46caSbillm return (NULL);
318144cd46caSbillm
318244cd46caSbillm vdev = vdev->vdev_child[i];
318344cd46caSbillm if (*s == '\0')
318444cd46caSbillm return (vdev);
318544cd46caSbillm return (zdb_vdev_lookup(vdev, s+1));
318644cd46caSbillm
318744cd46caSbillm name:
318844cd46caSbillm for (i = 0; i < vdev->vdev_children; i++) {
318944cd46caSbillm vdev_t *vc = vdev->vdev_child[i];
319044cd46caSbillm
319144cd46caSbillm if (vc->vdev_path == NULL) {
319244cd46caSbillm vc = zdb_vdev_lookup(vc, path);
319344cd46caSbillm if (vc == NULL)
319444cd46caSbillm continue;
319544cd46caSbillm else
319644cd46caSbillm return (vc);
319744cd46caSbillm }
319844cd46caSbillm
319944cd46caSbillm p = strrchr(vc->vdev_path, '/');
320044cd46caSbillm p = p ? p + 1 : vc->vdev_path;
320144cd46caSbillm q = &vc->vdev_path[strlen(vc->vdev_path) - 2];
320244cd46caSbillm
320344cd46caSbillm if (strcmp(vc->vdev_path, path) == 0)
320444cd46caSbillm return (vc);
320544cd46caSbillm if (strcmp(p, path) == 0)
320644cd46caSbillm return (vc);
320744cd46caSbillm if (strcmp(q, "s0") == 0 && strncmp(p, path, q - p) == 0)
320844cd46caSbillm return (vc);
320944cd46caSbillm }
321044cd46caSbillm
321144cd46caSbillm return (NULL);
321244cd46caSbillm }
321344cd46caSbillm
321444cd46caSbillm /*
321544cd46caSbillm * Read a block from a pool and print it out. The syntax of the
321644cd46caSbillm * block descriptor is:
321744cd46caSbillm *
321844cd46caSbillm * pool:vdev_specifier:offset:size[:flags]
321944cd46caSbillm *
322044cd46caSbillm * pool - The name of the pool you wish to read from
322144cd46caSbillm * vdev_specifier - Which vdev (see comment for zdb_vdev_lookup)
322244cd46caSbillm * offset - offset, in hex, in bytes
322344cd46caSbillm * size - Amount of data to read, in hex, in bytes
322444cd46caSbillm * flags - A string of characters specifying options
322544cd46caSbillm * b: Decode a blkptr at given offset within block
322644cd46caSbillm * *c: Calculate and display checksums
3227b24ab676SJeff Bonwick * d: Decompress data before dumping
322844cd46caSbillm * e: Byteswap data before dumping
3229b24ab676SJeff Bonwick * g: Display data as a gang block header
3230b24ab676SJeff Bonwick * i: Display as an indirect block
323144cd46caSbillm * p: Do I/O to physical offset
323244cd46caSbillm * r: Dump raw data to stdout
323344cd46caSbillm *
323444cd46caSbillm * * = not yet implemented
323544cd46caSbillm */
323644cd46caSbillm static void
zdb_read_block(char * thing,spa_t * spa)323707428bdfSVictor Latushkin zdb_read_block(char *thing, spa_t *spa)
323844cd46caSbillm {
3239b24ab676SJeff Bonwick blkptr_t blk, *bp = &blk;
3240b24ab676SJeff Bonwick dva_t *dva = bp->blk_dva;
324144cd46caSbillm int flags = 0;
3242b24ab676SJeff Bonwick uint64_t offset = 0, size = 0, psize = 0, lsize = 0, blkptr_offset = 0;
324344cd46caSbillm zio_t *zio;
324444cd46caSbillm vdev_t *vd;
3245b24ab676SJeff Bonwick void *pbuf, *lbuf, *buf;
324607428bdfSVictor Latushkin char *s, *p, *dup, *vdev, *flagstr;
3247b24ab676SJeff Bonwick int i, error;
324844cd46caSbillm
324944cd46caSbillm dup = strdup(thing);
325044cd46caSbillm s = strtok(dup, ":");
325144cd46caSbillm vdev = s ? s : "";
325244cd46caSbillm s = strtok(NULL, ":");
325344cd46caSbillm offset = strtoull(s ? s : "", NULL, 16);
325444cd46caSbillm s = strtok(NULL, ":");
325544cd46caSbillm size = strtoull(s ? s : "", NULL, 16);
325644cd46caSbillm s = strtok(NULL, ":");
325744cd46caSbillm flagstr = s ? s : "";
325844cd46caSbillm
325944cd46caSbillm s = NULL;
326044cd46caSbillm if (size == 0)
326144cd46caSbillm s = "size must not be zero";
326244cd46caSbillm if (!IS_P2ALIGNED(size, DEV_BSIZE))
326344cd46caSbillm s = "size must be a multiple of sector size";
326444cd46caSbillm if (!IS_P2ALIGNED(offset, DEV_BSIZE))
326544cd46caSbillm s = "offset must be a multiple of sector size";
326644cd46caSbillm if (s) {
326744cd46caSbillm (void) printf("Invalid block specifier: %s - %s\n", thing, s);
326844cd46caSbillm free(dup);
326944cd46caSbillm return;
327044cd46caSbillm }
327144cd46caSbillm
327244cd46caSbillm for (s = strtok(flagstr, ":"); s; s = strtok(NULL, ":")) {
327344cd46caSbillm for (i = 0; flagstr[i]; i++) {
32745ad82045Snd150628 int bit = flagbits[(uchar_t)flagstr[i]];
327544cd46caSbillm
327644cd46caSbillm if (bit == 0) {
327744cd46caSbillm (void) printf("***Invalid flag: %c\n",
327844cd46caSbillm flagstr[i]);
327944cd46caSbillm continue;
328044cd46caSbillm }
328144cd46caSbillm flags |= bit;
328244cd46caSbillm
328344cd46caSbillm /* If it's not something with an argument, keep going */
3284b24ab676SJeff Bonwick if ((bit & (ZDB_FLAG_CHECKSUM |
328544cd46caSbillm ZDB_FLAG_PRINT_BLKPTR)) == 0)
328644cd46caSbillm continue;
328744cd46caSbillm
328844cd46caSbillm p = &flagstr[i + 1];
328944cd46caSbillm if (bit == ZDB_FLAG_PRINT_BLKPTR)
329044cd46caSbillm blkptr_offset = strtoull(p, &p, 16);
329144cd46caSbillm if (*p != ':' && *p != '\0') {
329244cd46caSbillm (void) printf("***Invalid flag arg: '%s'\n", s);
329344cd46caSbillm free(dup);
329444cd46caSbillm return;
329544cd46caSbillm }
329644cd46caSbillm }
329744cd46caSbillm }
329844cd46caSbillm
329944cd46caSbillm vd = zdb_vdev_lookup(spa->spa_root_vdev, vdev);
330044cd46caSbillm if (vd == NULL) {
330144cd46caSbillm (void) printf("***Invalid vdev: %s\n", vdev);
330244cd46caSbillm free(dup);
330344cd46caSbillm return;
330444cd46caSbillm } else {
330544cd46caSbillm if (vd->vdev_path)
3306b24ab676SJeff Bonwick (void) fprintf(stderr, "Found vdev: %s\n",
3307b24ab676SJeff Bonwick vd->vdev_path);
330844cd46caSbillm else
3309b24ab676SJeff Bonwick (void) fprintf(stderr, "Found vdev type: %s\n",
331044cd46caSbillm vd->vdev_ops->vdev_op_type);
331144cd46caSbillm }
331244cd46caSbillm
3313b24ab676SJeff Bonwick psize = size;
3314b24ab676SJeff Bonwick lsize = size;
331544cd46caSbillm
3316b24ab676SJeff Bonwick pbuf = umem_alloc(SPA_MAXBLOCKSIZE, UMEM_NOFAIL);
3317b24ab676SJeff Bonwick lbuf = umem_alloc(SPA_MAXBLOCKSIZE, UMEM_NOFAIL);
3318b24ab676SJeff Bonwick
3319b24ab676SJeff Bonwick BP_ZERO(bp);
3320b24ab676SJeff Bonwick
3321b24ab676SJeff Bonwick DVA_SET_VDEV(&dva[0], vd->vdev_id);
3322b24ab676SJeff Bonwick DVA_SET_OFFSET(&dva[0], offset);
3323b24ab676SJeff Bonwick DVA_SET_GANG(&dva[0], !!(flags & ZDB_FLAG_GBH));
3324b24ab676SJeff Bonwick DVA_SET_ASIZE(&dva[0], vdev_psize_to_asize(vd, psize));
3325b24ab676SJeff Bonwick
3326b24ab676SJeff Bonwick BP_SET_BIRTH(bp, TXG_INITIAL, TXG_INITIAL);
3327b24ab676SJeff Bonwick
3328b24ab676SJeff Bonwick BP_SET_LSIZE(bp, lsize);
3329b24ab676SJeff Bonwick BP_SET_PSIZE(bp, psize);
3330b24ab676SJeff Bonwick BP_SET_COMPRESS(bp, ZIO_COMPRESS_OFF);
3331b24ab676SJeff Bonwick BP_SET_CHECKSUM(bp, ZIO_CHECKSUM_OFF);
3332b24ab676SJeff Bonwick BP_SET_TYPE(bp, DMU_OT_NONE);
3333b24ab676SJeff Bonwick BP_SET_LEVEL(bp, 0);
3334b24ab676SJeff Bonwick BP_SET_DEDUP(bp, 0);
3335b24ab676SJeff Bonwick BP_SET_BYTEORDER(bp, ZFS_HOST_BYTEORDER);
333644cd46caSbillm
3337e14bb325SJeff Bonwick spa_config_enter(spa, SCL_STATE, FTAG, RW_READER);
333844cd46caSbillm zio = zio_root(spa, NULL, NULL, 0);
3339b24ab676SJeff Bonwick
3340b24ab676SJeff Bonwick if (vd == vd->vdev_top) {
3341b24ab676SJeff Bonwick /*
3342b24ab676SJeff Bonwick * Treat this as a normal block read.
3343b24ab676SJeff Bonwick */
3344b24ab676SJeff Bonwick zio_nowait(zio_read(zio, spa, bp, pbuf, psize, NULL, NULL,
3345b24ab676SJeff Bonwick ZIO_PRIORITY_SYNC_READ,
3346b24ab676SJeff Bonwick ZIO_FLAG_CANFAIL | ZIO_FLAG_RAW, NULL));
3347b24ab676SJeff Bonwick } else {
3348b24ab676SJeff Bonwick /*
3349b24ab676SJeff Bonwick * Treat this as a vdev child I/O.
3350b24ab676SJeff Bonwick */
3351b24ab676SJeff Bonwick zio_nowait(zio_vdev_child_io(zio, bp, vd, offset, pbuf, psize,
3352b24ab676SJeff Bonwick ZIO_TYPE_READ, ZIO_PRIORITY_SYNC_READ,
3353b24ab676SJeff Bonwick ZIO_FLAG_DONT_CACHE | ZIO_FLAG_DONT_QUEUE |
3354b24ab676SJeff Bonwick ZIO_FLAG_DONT_PROPAGATE | ZIO_FLAG_DONT_RETRY |
3355b24ab676SJeff Bonwick ZIO_FLAG_CANFAIL | ZIO_FLAG_RAW, NULL, NULL));
3356b24ab676SJeff Bonwick }
3357b24ab676SJeff Bonwick
335844cd46caSbillm error = zio_wait(zio);
3359e14bb325SJeff Bonwick spa_config_exit(spa, SCL_STATE, FTAG);
336044cd46caSbillm
336144cd46caSbillm if (error) {
336244cd46caSbillm (void) printf("Read of %s failed, error: %d\n", thing, error);
336344cd46caSbillm goto out;
336444cd46caSbillm }
336544cd46caSbillm
3366b24ab676SJeff Bonwick if (flags & ZDB_FLAG_DECOMPRESS) {
3367b24ab676SJeff Bonwick /*
3368b24ab676SJeff Bonwick * We don't know how the data was compressed, so just try
3369b24ab676SJeff Bonwick * every decompress function at every inflated blocksize.
3370b24ab676SJeff Bonwick */
3371b24ab676SJeff Bonwick enum zio_compress c;
3372b24ab676SJeff Bonwick void *pbuf2 = umem_alloc(SPA_MAXBLOCKSIZE, UMEM_NOFAIL);
3373b24ab676SJeff Bonwick void *lbuf2 = umem_alloc(SPA_MAXBLOCKSIZE, UMEM_NOFAIL);
3374b24ab676SJeff Bonwick
3375b24ab676SJeff Bonwick bcopy(pbuf, pbuf2, psize);
3376b24ab676SJeff Bonwick
3377b24ab676SJeff Bonwick VERIFY(random_get_pseudo_bytes((uint8_t *)pbuf + psize,
3378b24ab676SJeff Bonwick SPA_MAXBLOCKSIZE - psize) == 0);
3379b24ab676SJeff Bonwick
3380b24ab676SJeff Bonwick VERIFY(random_get_pseudo_bytes((uint8_t *)pbuf2 + psize,
3381b24ab676SJeff Bonwick SPA_MAXBLOCKSIZE - psize) == 0);
3382b24ab676SJeff Bonwick
3383b24ab676SJeff Bonwick for (lsize = SPA_MAXBLOCKSIZE; lsize > psize;
3384b24ab676SJeff Bonwick lsize -= SPA_MINBLOCKSIZE) {
3385b24ab676SJeff Bonwick for (c = 0; c < ZIO_COMPRESS_FUNCTIONS; c++) {
3386b24ab676SJeff Bonwick if (zio_decompress_data(c, pbuf, lbuf,
3387b24ab676SJeff Bonwick psize, lsize) == 0 &&
3388b24ab676SJeff Bonwick zio_decompress_data(c, pbuf2, lbuf2,
3389b24ab676SJeff Bonwick psize, lsize) == 0 &&
3390b24ab676SJeff Bonwick bcmp(lbuf, lbuf2, lsize) == 0)
3391b24ab676SJeff Bonwick break;
3392b24ab676SJeff Bonwick }
3393b24ab676SJeff Bonwick if (c != ZIO_COMPRESS_FUNCTIONS)
3394b24ab676SJeff Bonwick break;
3395b24ab676SJeff Bonwick lsize -= SPA_MINBLOCKSIZE;
3396b24ab676SJeff Bonwick }
3397b24ab676SJeff Bonwick
3398b24ab676SJeff Bonwick umem_free(pbuf2, SPA_MAXBLOCKSIZE);
3399b24ab676SJeff Bonwick umem_free(lbuf2, SPA_MAXBLOCKSIZE);
3400b24ab676SJeff Bonwick
3401b24ab676SJeff Bonwick if (lsize <= psize) {
3402b24ab676SJeff Bonwick (void) printf("Decompress of %s failed\n", thing);
3403b24ab676SJeff Bonwick goto out;
3404b24ab676SJeff Bonwick }
3405b24ab676SJeff Bonwick buf = lbuf;
3406b24ab676SJeff Bonwick size = lsize;
3407b24ab676SJeff Bonwick } else {
3408b24ab676SJeff Bonwick buf = pbuf;
3409b24ab676SJeff Bonwick size = psize;
3410b24ab676SJeff Bonwick }
3411b24ab676SJeff Bonwick
341244cd46caSbillm if (flags & ZDB_FLAG_PRINT_BLKPTR)
341344cd46caSbillm zdb_print_blkptr((blkptr_t *)(void *)
341444cd46caSbillm ((uintptr_t)buf + (uintptr_t)blkptr_offset), flags);
341544cd46caSbillm else if (flags & ZDB_FLAG_RAW)
341644cd46caSbillm zdb_dump_block_raw(buf, size, flags);
341744cd46caSbillm else if (flags & ZDB_FLAG_INDIRECT)
341844cd46caSbillm zdb_dump_indirect((blkptr_t *)buf, size / sizeof (blkptr_t),
341944cd46caSbillm flags);
342044cd46caSbillm else if (flags & ZDB_FLAG_GBH)
342144cd46caSbillm zdb_dump_gbh(buf, flags);
342244cd46caSbillm else
342344cd46caSbillm zdb_dump_block(thing, buf, size, flags);
342444cd46caSbillm
342544cd46caSbillm out:
3426b24ab676SJeff Bonwick umem_free(pbuf, SPA_MAXBLOCKSIZE);
3427b24ab676SJeff Bonwick umem_free(lbuf, SPA_MAXBLOCKSIZE);
342844cd46caSbillm free(dup);
342944cd46caSbillm }
343044cd46caSbillm
3431de6628f0Sck153898 static boolean_t
pool_match(nvlist_t * cfg,char * tgt)34323ad6c7f9SVictor Latushkin pool_match(nvlist_t *cfg, char *tgt)
3433de6628f0Sck153898 {
34343ad6c7f9SVictor Latushkin uint64_t v, guid = strtoull(tgt, NULL, 0);
3435de6628f0Sck153898 char *s;
3436de6628f0Sck153898
34373ad6c7f9SVictor Latushkin if (guid != 0) {
34383ad6c7f9SVictor Latushkin if (nvlist_lookup_uint64(cfg, ZPOOL_CONFIG_POOL_GUID, &v) == 0)
34393ad6c7f9SVictor Latushkin return (v == guid);
34403ad6c7f9SVictor Latushkin } else {
34413ad6c7f9SVictor Latushkin if (nvlist_lookup_string(cfg, ZPOOL_CONFIG_POOL_NAME, &s) == 0)
3442de6628f0Sck153898 return (strcmp(s, tgt) == 0);
3443de6628f0Sck153898 }
3444de6628f0Sck153898 return (B_FALSE);
3445de6628f0Sck153898 }
3446de6628f0Sck153898
34473ad6c7f9SVictor Latushkin static char *
find_zpool(char ** target,nvlist_t ** configp,int dirc,char ** dirv)34483ad6c7f9SVictor Latushkin find_zpool(char **target, nvlist_t **configp, int dirc, char **dirv)
3449de6628f0Sck153898 {
3450de6628f0Sck153898 nvlist_t *pools;
3451de6628f0Sck153898 nvlist_t *match = NULL;
34523ad6c7f9SVictor Latushkin char *name = NULL;
34533ad6c7f9SVictor Latushkin char *sepp = NULL;
3454*675fc291SMatthew Ahrens char sep = '\0';
34553ad6c7f9SVictor Latushkin int count = 0;
3456d41c4376SMark J Musante importargs_t args = { 0 };
3457d41c4376SMark J Musante
3458d41c4376SMark J Musante args.paths = dirc;
3459d41c4376SMark J Musante args.path = dirv;
3460d41c4376SMark J Musante args.can_be_active = B_TRUE;
3461de6628f0Sck153898
34623ad6c7f9SVictor Latushkin if ((sepp = strpbrk(*target, "/@")) != NULL) {
34633ad6c7f9SVictor Latushkin sep = *sepp;
34643ad6c7f9SVictor Latushkin *sepp = '\0';
34653ad6c7f9SVictor Latushkin }
34663ad6c7f9SVictor Latushkin
3467d41c4376SMark J Musante pools = zpool_search_import(g_zfs, &args);
3468de6628f0Sck153898
3469de6628f0Sck153898 if (pools != NULL) {
3470de6628f0Sck153898 nvpair_t *elem = NULL;
3471de6628f0Sck153898 while ((elem = nvlist_next_nvpair(pools, elem)) != NULL) {
3472de6628f0Sck153898 verify(nvpair_value_nvlist(elem, configp) == 0);
34733ad6c7f9SVictor Latushkin if (pool_match(*configp, *target)) {
34743ad6c7f9SVictor Latushkin count++;
3475de6628f0Sck153898 if (match != NULL) {
34763ad6c7f9SVictor Latushkin /* print previously found config */
34773ad6c7f9SVictor Latushkin if (name != NULL) {
34783ad6c7f9SVictor Latushkin (void) printf("%s\n", name);
34793ad6c7f9SVictor Latushkin dump_nvlist(match, 8);
34803ad6c7f9SVictor Latushkin name = NULL;
34813ad6c7f9SVictor Latushkin }
34823ad6c7f9SVictor Latushkin (void) printf("%s\n",
34833ad6c7f9SVictor Latushkin nvpair_name(elem));
34843ad6c7f9SVictor Latushkin dump_nvlist(*configp, 8);
3485de6628f0Sck153898 } else {
3486de6628f0Sck153898 match = *configp;
34873ad6c7f9SVictor Latushkin name = nvpair_name(elem);
3488de6628f0Sck153898 }
3489de6628f0Sck153898 }
3490de6628f0Sck153898 }
3491de6628f0Sck153898 }
34923ad6c7f9SVictor Latushkin if (count > 1)
34933ad6c7f9SVictor Latushkin (void) fatal("\tMatched %d pools - use pool GUID "
34943ad6c7f9SVictor Latushkin "instead of pool name or \n"
34953ad6c7f9SVictor Latushkin "\tpool name part of a dataset name to select pool", count);
34963ad6c7f9SVictor Latushkin
34973ad6c7f9SVictor Latushkin if (sepp)
34983ad6c7f9SVictor Latushkin *sepp = sep;
34993ad6c7f9SVictor Latushkin /*
35003ad6c7f9SVictor Latushkin * If pool GUID was specified for pool id, replace it with pool name
35013ad6c7f9SVictor Latushkin */
35023ad6c7f9SVictor Latushkin if (name && (strstr(*target, name) != *target)) {
35033ad6c7f9SVictor Latushkin int sz = 1 + strlen(name) + ((sepp) ? strlen(sepp) : 0);
35043ad6c7f9SVictor Latushkin
35053ad6c7f9SVictor Latushkin *target = umem_alloc(sz, UMEM_NOFAIL);
35063ad6c7f9SVictor Latushkin (void) snprintf(*target, sz, "%s%s", name, sepp ? sepp : "");
35073ad6c7f9SVictor Latushkin }
3508de6628f0Sck153898
35093ad6c7f9SVictor Latushkin *configp = name ? match : NULL;
3510de6628f0Sck153898
35113ad6c7f9SVictor Latushkin return (name);
3512de6628f0Sck153898 }
3513de6628f0Sck153898
3514fa9e4066Sahrens int
main(int argc,char ** argv)3515fa9e4066Sahrens main(int argc, char **argv)
3516fa9e4066Sahrens {
3517fa9e4066Sahrens int i, c;
3518fa9e4066Sahrens struct rlimit rl = { 1024, 1024 };
35193ad6c7f9SVictor Latushkin spa_t *spa = NULL;
3520fa9e4066Sahrens objset_t *os = NULL;
3521fa9e4066Sahrens int dump_all = 1;
3522fa9e4066Sahrens int verbose = 0;
3523c8ee1847SVictor Latushkin int error = 0;
35243ad6c7f9SVictor Latushkin char **searchdirs = NULL;
35253ad6c7f9SVictor Latushkin int nsearch = 0;
35263ad6c7f9SVictor Latushkin char *target;
3527468c413aSTim Haley nvlist_t *policy = NULL;
3528468c413aSTim Haley uint64_t max_txg = UINT64_MAX;
3529c8ee1847SVictor Latushkin int rewind = ZPOOL_NEVER_REWIND;
3530fa9e4066Sahrens
3531fa9e4066Sahrens (void) setrlimit(RLIMIT_NOFILE, &rl);
3532004388ebScasper (void) enable_extended_FILE_stdio(-1, -1);
3533fa9e4066Sahrens
3534fa9e4066Sahrens dprintf_setup(&argc, argv);
3535fa9e4066Sahrens
3536dffb1e38SMatthew Ahrens while ((c = getopt(argc, argv,
353702b647c5SGeorge Wilson "bcdhilmMI:suCDRSAFLXx:evp:t:U:P")) != -1) {
3538fa9e4066Sahrens switch (c) {
3539fa9e4066Sahrens case 'b':
3540fa9e4066Sahrens case 'c':
3541b24ab676SJeff Bonwick case 'd':
3542b24ab676SJeff Bonwick case 'h':
3543b24ab676SJeff Bonwick case 'i':
3544b24ab676SJeff Bonwick case 'l':
3545d6e555bdSGeorge Wilson case 'm':
3546fa9e4066Sahrens case 's':
3547b24ab676SJeff Bonwick case 'u':
3548fa9e4066Sahrens case 'C':
3549b24ab676SJeff Bonwick case 'D':
355002b647c5SGeorge Wilson case 'M':
355144cd46caSbillm case 'R':
3552b24ab676SJeff Bonwick case 'S':
3553fa9e4066Sahrens dump_opt[c]++;
3554fa9e4066Sahrens dump_all = 0;
3555fa9e4066Sahrens break;
3556feef89cfSVictor Latushkin case 'A':
3557c8ee1847SVictor Latushkin case 'F':
355882a0a985SVictor Latushkin case 'L':
3559c8ee1847SVictor Latushkin case 'X':
35603ad6c7f9SVictor Latushkin case 'e':
35613f9d6ad7SLin Ling case 'P':
356282a0a985SVictor Latushkin dump_opt[c]++;
356382a0a985SVictor Latushkin break;
356402b647c5SGeorge Wilson case 'I':
356531d7e8faSGeorge Wilson max_inflight = strtoull(optarg, NULL, 0);
356631d7e8faSGeorge Wilson if (max_inflight == 0) {
356731d7e8faSGeorge Wilson (void) fprintf(stderr, "maximum number "
356831d7e8faSGeorge Wilson "of inflight I/Os must be greater "
356931d7e8faSGeorge Wilson "than 0\n");
357031d7e8faSGeorge Wilson usage();
357131d7e8faSGeorge Wilson }
357231d7e8faSGeorge Wilson break;
3573de6628f0Sck153898 case 'p':
35743ad6c7f9SVictor Latushkin if (searchdirs == NULL) {
35753ad6c7f9SVictor Latushkin searchdirs = umem_alloc(sizeof (char *),
35763ad6c7f9SVictor Latushkin UMEM_NOFAIL);
35773ad6c7f9SVictor Latushkin } else {
35783ad6c7f9SVictor Latushkin char **tmp = umem_alloc((nsearch + 1) *
35793ad6c7f9SVictor Latushkin sizeof (char *), UMEM_NOFAIL);
35803ad6c7f9SVictor Latushkin bcopy(searchdirs, tmp, nsearch *
35813ad6c7f9SVictor Latushkin sizeof (char *));
35823ad6c7f9SVictor Latushkin umem_free(searchdirs,
35833ad6c7f9SVictor Latushkin nsearch * sizeof (char *));
35843ad6c7f9SVictor Latushkin searchdirs = tmp;
35853ad6c7f9SVictor Latushkin }
35863ad6c7f9SVictor Latushkin searchdirs[nsearch++] = optarg;
3587de6628f0Sck153898 break;
35882e551927SVictor Latushkin case 't':
3589468c413aSTim Haley max_txg = strtoull(optarg, NULL, 0);
3590468c413aSTim Haley if (max_txg < TXG_INITIAL) {
35912e551927SVictor Latushkin (void) fprintf(stderr, "incorrect txg "
35922e551927SVictor Latushkin "specified: %s\n", optarg);
35932e551927SVictor Latushkin usage();
35942e551927SVictor Latushkin }
35952e551927SVictor Latushkin break;
3596b24ab676SJeff Bonwick case 'U':
3597b24ab676SJeff Bonwick spa_config_path = optarg;
3598b24ab676SJeff Bonwick break;
359902b647c5SGeorge Wilson case 'v':
360002b647c5SGeorge Wilson verbose++;
360102b647c5SGeorge Wilson break;
360202b647c5SGeorge Wilson case 'x':
360302b647c5SGeorge Wilson vn_dumpdir = optarg;
360402b647c5SGeorge Wilson break;
3605fa9e4066Sahrens default:
3606fa9e4066Sahrens usage();
3607fa9e4066Sahrens break;
3608fa9e4066Sahrens }
3609fa9e4066Sahrens }
3610fa9e4066Sahrens
36113ad6c7f9SVictor Latushkin if (!dump_opt['e'] && searchdirs != NULL) {
361288b7b0f2SMatthew Ahrens (void) fprintf(stderr, "-p option requires use of -e\n");
361388b7b0f2SMatthew Ahrens usage();
361488b7b0f2SMatthew Ahrens }
3615de6628f0Sck153898
3616fef64290SMatthew Ahrens /*
3617fef64290SMatthew Ahrens * ZDB does not typically re-read blocks; therefore limit the ARC
3618fef64290SMatthew Ahrens * to 256 MB, which can be used entirely for metadata.
3619fef64290SMatthew Ahrens */
3620fef64290SMatthew Ahrens zfs_arc_max = zfs_arc_meta_limit = 256 * 1024 * 1024;
3621fef64290SMatthew Ahrens
362249b2c7b9SMatthew Ahrens /*
362349b2c7b9SMatthew Ahrens * "zdb -c" uses checksum-verifying scrub i/os which are async reads.
362449b2c7b9SMatthew Ahrens * "zdb -b" uses traversal prefetch which uses async reads.
362549b2c7b9SMatthew Ahrens * For good performance, let several of them be active at once.
362649b2c7b9SMatthew Ahrens */
362749b2c7b9SMatthew Ahrens zfs_vdev_async_read_max_active = 10;
362849b2c7b9SMatthew Ahrens
3629fa9e4066Sahrens kernel_init(FREAD);
3630de6628f0Sck153898 g_zfs = libzfs_init();
363191ebeef5Sahrens ASSERT(g_zfs != NULL);
3632fa9e4066Sahrens
3633b24ab676SJeff Bonwick if (dump_all)
3634b24ab676SJeff Bonwick verbose = MAX(verbose, 1);
3635b24ab676SJeff Bonwick
3636fa9e4066Sahrens for (c = 0; c < 256; c++) {
36373f9d6ad7SLin Ling if (dump_all && !strchr("elAFLRSXP", c))
3638fa9e4066Sahrens dump_opt[c] = 1;
3639fa9e4066Sahrens if (dump_opt[c])
3640fa9e4066Sahrens dump_opt[c] += verbose;
3641fa9e4066Sahrens }
3642fa9e4066Sahrens
3643feef89cfSVictor Latushkin aok = (dump_opt['A'] == 1) || (dump_opt['A'] > 2);
3644feef89cfSVictor Latushkin zfs_recover = (dump_opt['A'] > 1);
3645feef89cfSVictor Latushkin
3646fa9e4066Sahrens argc -= optind;
3647fa9e4066Sahrens argv += optind;
3648fa9e4066Sahrens
364907428bdfSVictor Latushkin if (argc < 2 && dump_opt['R'])
365007428bdfSVictor Latushkin usage();
3651fa9e4066Sahrens if (argc < 1) {
36523ad6c7f9SVictor Latushkin if (!dump_opt['e'] && dump_opt['C']) {
3653e829d913Sck153898 dump_cachefile(spa_config_path);
3654fa9e4066Sahrens return (0);
3655fa9e4066Sahrens }
3656fa9e4066Sahrens usage();
3657fa9e4066Sahrens }
3658fa9e4066Sahrens
3659fa9e4066Sahrens if (dump_opt['l']) {
3660fa9e4066Sahrens dump_label(argv[0]);
3661fa9e4066Sahrens return (0);
3662fa9e4066Sahrens }
3663fa9e4066Sahrens
3664c8ee1847SVictor Latushkin if (dump_opt['X'] || dump_opt['F'])
3665c8ee1847SVictor Latushkin rewind = ZPOOL_DO_REWIND |
3666c8ee1847SVictor Latushkin (dump_opt['X'] ? ZPOOL_EXTREME_REWIND : 0);
3667c8ee1847SVictor Latushkin
3668c8ee1847SVictor Latushkin if (nvlist_alloc(&policy, NV_UNIQUE_NAME_TYPE, 0) != 0 ||
3669c8ee1847SVictor Latushkin nvlist_add_uint64(policy, ZPOOL_REWIND_REQUEST_TXG, max_txg) != 0 ||
3670c8ee1847SVictor Latushkin nvlist_add_uint32(policy, ZPOOL_REWIND_REQUEST, rewind) != 0)
3671c8ee1847SVictor Latushkin fatal("internal error: %s", strerror(ENOMEM));
3672c8ee1847SVictor Latushkin
3673c5904d13Seschrock error = 0;
36743ad6c7f9SVictor Latushkin target = argv[0];
3675de6628f0Sck153898
36763ad6c7f9SVictor Latushkin if (dump_opt['e']) {
36773ad6c7f9SVictor Latushkin nvlist_t *cfg = NULL;
36783ad6c7f9SVictor Latushkin char *name = find_zpool(&target, &cfg, nsearch, searchdirs);
3679c5904d13Seschrock
36803ad6c7f9SVictor Latushkin error = ENOENT;
36813ad6c7f9SVictor Latushkin if (name) {
368207428bdfSVictor Latushkin if (dump_opt['C'] > 1) {
368307428bdfSVictor Latushkin (void) printf("\nConfiguration for import:\n");
368407428bdfSVictor Latushkin dump_nvlist(cfg, 8);
368507428bdfSVictor Latushkin }
3686c8ee1847SVictor Latushkin if (nvlist_add_nvlist(cfg,
3687468c413aSTim Haley ZPOOL_REWIND_POLICY, policy) != 0) {
3688468c413aSTim Haley fatal("can't open '%s': %s",
3689468c413aSTim Haley target, strerror(ENOMEM));
3690468c413aSTim Haley }
36914b964adaSGeorge Wilson if ((error = spa_import(name, cfg, NULL,
36924b964adaSGeorge Wilson ZFS_IMPORT_MISSING_LOG)) != 0) {
36934b964adaSGeorge Wilson error = spa_import(name, cfg, NULL,
36944b964adaSGeorge Wilson ZFS_IMPORT_VERBATIM);
36954b964adaSGeorge Wilson }
3696990b4856Slling }
3697c5904d13Seschrock }
3698c5904d13Seschrock
3699c5904d13Seschrock if (error == 0) {
370007428bdfSVictor Latushkin if (strpbrk(target, "/@") == NULL || dump_opt['R']) {
370180eb36f2SGeorge Wilson error = spa_open_rewind(target, &spa, FTAG, policy,
370280eb36f2SGeorge Wilson NULL);
37038f18d1faSGeorge Wilson if (error) {
37048f18d1faSGeorge Wilson /*
37058f18d1faSGeorge Wilson * If we're missing the log device then
37068f18d1faSGeorge Wilson * try opening the pool after clearing the
37078f18d1faSGeorge Wilson * log state.
37088f18d1faSGeorge Wilson */
37098f18d1faSGeorge Wilson mutex_enter(&spa_namespace_lock);
37103ad6c7f9SVictor Latushkin if ((spa = spa_lookup(target)) != NULL &&
37118f18d1faSGeorge Wilson spa->spa_log_state == SPA_LOG_MISSING) {
37128f18d1faSGeorge Wilson spa->spa_log_state = SPA_LOG_CLEAR;
37138f18d1faSGeorge Wilson error = 0;
37148f18d1faSGeorge Wilson }
37158f18d1faSGeorge Wilson mutex_exit(&spa_namespace_lock);
37168f18d1faSGeorge Wilson
371780eb36f2SGeorge Wilson if (!error) {
371880eb36f2SGeorge Wilson error = spa_open_rewind(target, &spa,
371980eb36f2SGeorge Wilson FTAG, policy, NULL);
372080eb36f2SGeorge Wilson }
37218f18d1faSGeorge Wilson }
372207428bdfSVictor Latushkin } else {
372307428bdfSVictor Latushkin error = dmu_objset_own(target, DMU_OST_ANY,
372407428bdfSVictor Latushkin B_TRUE, FTAG, &os);
3725c5904d13Seschrock }
3726de6628f0Sck153898 }
372780eb36f2SGeorge Wilson nvlist_free(policy);
372880eb36f2SGeorge Wilson
3729fa9e4066Sahrens if (error)
37303ad6c7f9SVictor Latushkin fatal("can't open '%s': %s", target, strerror(error));
3731fa9e4066Sahrens
3732fa9e4066Sahrens argv++;
373307428bdfSVictor Latushkin argc--;
373407428bdfSVictor Latushkin if (!dump_opt['R']) {
373507428bdfSVictor Latushkin if (argc > 0) {
3736fa9e4066Sahrens zopt_objects = argc;
3737fa9e4066Sahrens zopt_object = calloc(zopt_objects, sizeof (uint64_t));
3738fa9e4066Sahrens for (i = 0; i < zopt_objects; i++) {
3739fa9e4066Sahrens errno = 0;
3740fa9e4066Sahrens zopt_object[i] = strtoull(argv[i], NULL, 0);
3741fa9e4066Sahrens if (zopt_object[i] == 0 && errno != 0)
374287219db7SVictor Latushkin fatal("bad number %s: %s",
3743fa9e4066Sahrens argv[i], strerror(errno));
3744fa9e4066Sahrens }
3745fa9e4066Sahrens }
3746e690fb27SChristopher Siden if (os != NULL) {
3747e690fb27SChristopher Siden dump_dir(os);
3748e690fb27SChristopher Siden } else if (zopt_objects > 0 && !dump_opt['m']) {
3749e690fb27SChristopher Siden dump_dir(spa->spa_meta_objset);
3750e690fb27SChristopher Siden } else {
3751e690fb27SChristopher Siden dump_zpool(spa);
3752e690fb27SChristopher Siden }
3753fa9e4066Sahrens } else {
375407428bdfSVictor Latushkin flagbits['b'] = ZDB_FLAG_PRINT_BLKPTR;
375507428bdfSVictor Latushkin flagbits['c'] = ZDB_FLAG_CHECKSUM;
375607428bdfSVictor Latushkin flagbits['d'] = ZDB_FLAG_DECOMPRESS;
375707428bdfSVictor Latushkin flagbits['e'] = ZDB_FLAG_BSWAP;
375807428bdfSVictor Latushkin flagbits['g'] = ZDB_FLAG_GBH;
375907428bdfSVictor Latushkin flagbits['i'] = ZDB_FLAG_INDIRECT;
376007428bdfSVictor Latushkin flagbits['p'] = ZDB_FLAG_PHYS;
376107428bdfSVictor Latushkin flagbits['r'] = ZDB_FLAG_RAW;
376207428bdfSVictor Latushkin
376307428bdfSVictor Latushkin for (i = 0; i < argc; i++)
376407428bdfSVictor Latushkin zdb_read_block(argv[i], spa);
3765fa9e4066Sahrens }
3766fa9e4066Sahrens
376707428bdfSVictor Latushkin (os != NULL) ? dmu_objset_disown(os, FTAG) : spa_close(spa, FTAG);
376807428bdfSVictor Latushkin
3769e0d35c44Smarks fuid_table_destroy();
37700a586ceaSMark Shellenbaum sa_loaded = B_FALSE;
3771e0d35c44Smarks
3772de6628f0Sck153898 libzfs_fini(g_zfs);
3773fa9e4066Sahrens kernel_fini();
3774fa9e4066Sahrens
3775fa9e4066Sahrens return (0);
3776fa9e4066Sahrens }
3777