Home | History | Annotate | Line # | Download | only in zdb
zdb.c revision 1.2
      1  1.1      haad /*
      2  1.1      haad  * CDDL HEADER START
      3  1.1      haad  *
      4  1.1      haad  * The contents of this file are subject to the terms of the
      5  1.1      haad  * Common Development and Distribution License (the "License").
      6  1.1      haad  * You may not use this file except in compliance with the License.
      7  1.1      haad  *
      8  1.1      haad  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
      9  1.1      haad  * or http://www.opensolaris.org/os/licensing.
     10  1.1      haad  * See the License for the specific language governing permissions
     11  1.1      haad  * and limitations under the License.
     12  1.1      haad  *
     13  1.1      haad  * When distributing Covered Code, include this CDDL HEADER in each
     14  1.1      haad  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
     15  1.1      haad  * If applicable, add the following below this CDDL HEADER, with the
     16  1.1      haad  * fields enclosed by brackets "[]" replaced with your own identifying
     17  1.1      haad  * information: Portions Copyright [yyyy] [name of copyright owner]
     18  1.1      haad  *
     19  1.1      haad  * CDDL HEADER END
     20  1.1      haad  */
     21  1.1      haad /*
     22  1.2  christos  * Copyright 2010 Sun Microsystems, Inc.  All rights reserved.
     23  1.1      haad  * Use is subject to license terms.
     24  1.1      haad  */
     25  1.1      haad 
     26  1.1      haad #include <stdio.h>
     27  1.1      haad #include <stdio_ext.h>
     28  1.1      haad #include <stdlib.h>
     29  1.1      haad #include <ctype.h>
     30  1.1      haad #include <sys/zfs_context.h>
     31  1.1      haad #include <sys/spa.h>
     32  1.1      haad #include <sys/spa_impl.h>
     33  1.1      haad #include <sys/dmu.h>
     34  1.1      haad #include <sys/zap.h>
     35  1.1      haad #include <sys/fs/zfs.h>
     36  1.1      haad #include <sys/zfs_znode.h>
     37  1.1      haad #include <sys/vdev.h>
     38  1.1      haad #include <sys/vdev_impl.h>
     39  1.1      haad #include <sys/metaslab_impl.h>
     40  1.1      haad #include <sys/dmu_objset.h>
     41  1.1      haad #include <sys/dsl_dir.h>
     42  1.1      haad #include <sys/dsl_dataset.h>
     43  1.1      haad #include <sys/dsl_pool.h>
     44  1.1      haad #include <sys/dbuf.h>
     45  1.1      haad #include <sys/zil.h>
     46  1.1      haad #include <sys/zil_impl.h>
     47  1.1      haad #include <sys/stat.h>
     48  1.1      haad #include <sys/resource.h>
     49  1.1      haad #include <sys/dmu_traverse.h>
     50  1.1      haad #include <sys/zio_checksum.h>
     51  1.1      haad #include <sys/zio_compress.h>
     52  1.1      haad #include <sys/zfs_fuid.h>
     53  1.1      haad #include <sys/arc.h>
     54  1.2  christos #include <sys/ddt.h>
     55  1.1      haad #undef ZFS_MAXNAMELEN
     56  1.1      haad #undef verify
     57  1.1      haad #include <libzfs.h>
     58  1.1      haad 
     59  1.2  christos #define	ZDB_COMPRESS_NAME(idx) ((idx) < ZIO_COMPRESS_FUNCTIONS ? \
     60  1.2  christos     zio_compress_table[(idx)].ci_name : "UNKNOWN")
     61  1.2  christos #define	ZDB_CHECKSUM_NAME(idx) ((idx) < ZIO_CHECKSUM_FUNCTIONS ? \
     62  1.2  christos     zio_checksum_table[(idx)].ci_name : "UNKNOWN")
     63  1.2  christos #define	ZDB_OT_NAME(idx) ((idx) < DMU_OT_NUMTYPES ? \
     64  1.2  christos     dmu_ot[(idx)].ot_name : "UNKNOWN")
     65  1.2  christos #define	ZDB_OT_TYPE(idx) ((idx) < DMU_OT_NUMTYPES ? (idx) : DMU_OT_NUMTYPES)
     66  1.2  christos 
     67  1.2  christos #ifndef lint
     68  1.2  christos extern int zfs_recover;
     69  1.2  christos #else
     70  1.2  christos int zfs_recover;
     71  1.2  christos #endif
     72  1.2  christos 
     73  1.1      haad const char cmdname[] = "zdb";
     74  1.1      haad uint8_t dump_opt[256];
     75  1.1      haad 
     76  1.1      haad typedef void object_viewer_t(objset_t *, uint64_t, void *data, size_t size);
     77  1.1      haad 
     78  1.1      haad extern void dump_intent_log(zilog_t *);
     79  1.1      haad uint64_t *zopt_object = NULL;
     80  1.1      haad int zopt_objects = 0;
     81  1.1      haad libzfs_handle_t *g_zfs;
     82  1.1      haad 
     83  1.1      haad /*
     84  1.1      haad  * These libumem hooks provide a reasonable set of defaults for the allocator's
     85  1.1      haad  * debugging facilities.
     86  1.1      haad  */
     87  1.1      haad const char *
     88  1.1      haad _umem_debug_init()
     89  1.1      haad {
     90  1.1      haad 	return ("default,verbose"); /* $UMEM_DEBUG setting */
     91  1.1      haad }
     92  1.1      haad 
     93  1.1      haad const char *
     94  1.1      haad _umem_logging_init(void)
     95  1.1      haad {
     96  1.1      haad 	return ("fail,contents"); /* $UMEM_LOGGING setting */
     97  1.1      haad }
     98  1.1      haad 
     99  1.1      haad static void
    100  1.1      haad usage(void)
    101  1.1      haad {
    102  1.1      haad 	(void) fprintf(stderr,
    103  1.2  christos 	    "Usage: %s [-CumdibcsvhL] poolname [object...]\n"
    104  1.2  christos 	    "       %s [-div] dataset [object...]\n"
    105  1.2  christos 	    "       %s -m [-L] poolname [vdev [metaslab...]]\n"
    106  1.2  christos 	    "       %s -R poolname vdev:offset:size[:flags]\n"
    107  1.2  christos 	    "       %s -S poolname\n"
    108  1.2  christos 	    "       %s -l [-u] device\n"
    109  1.2  christos 	    "       %s -C\n\n",
    110  1.2  christos 	    cmdname, cmdname, cmdname, cmdname, cmdname, cmdname, cmdname);
    111  1.2  christos 
    112  1.2  christos 	(void) fprintf(stderr, "    Dataset name must include at least one "
    113  1.2  christos 	    "separator character '/' or '@'\n");
    114  1.2  christos 	(void) fprintf(stderr, "    If dataset name is specified, only that "
    115  1.2  christos 	    "dataset is dumped\n");
    116  1.2  christos 	(void) fprintf(stderr, "    If object numbers are specified, only "
    117  1.2  christos 	    "those objects are dumped\n\n");
    118  1.2  christos 	(void) fprintf(stderr, "    Options to control amount of output:\n");
    119  1.2  christos 	(void) fprintf(stderr, "        -u uberblock\n");
    120  1.2  christos 	(void) fprintf(stderr, "        -d dataset(s)\n");
    121  1.2  christos 	(void) fprintf(stderr, "        -i intent logs\n");
    122  1.2  christos 	(void) fprintf(stderr, "        -C config (or cachefile if alone)\n");
    123  1.2  christos 	(void) fprintf(stderr, "        -h pool history\n");
    124  1.2  christos 	(void) fprintf(stderr, "        -b block statistics\n");
    125  1.2  christos 	(void) fprintf(stderr, "        -m metaslabs\n");
    126  1.2  christos 	(void) fprintf(stderr, "        -c checksum all metadata (twice for "
    127  1.2  christos 	    "all data) blocks\n");
    128  1.2  christos 	(void) fprintf(stderr, "        -s report stats on zdb's I/O\n");
    129  1.2  christos 	(void) fprintf(stderr, "        -S simulate dedup to measure effect\n");
    130  1.2  christos 	(void) fprintf(stderr, "        -v verbose (applies to all others)\n");
    131  1.1      haad 	(void) fprintf(stderr, "        -l dump label contents\n");
    132  1.1      haad 	(void) fprintf(stderr, "        -L disable leak tracking (do not "
    133  1.1      haad 	    "load spacemaps)\n");
    134  1.2  christos 	(void) fprintf(stderr, "        -R read and display block from a "
    135  1.2  christos 	    "device\n\n");
    136  1.2  christos 	(void) fprintf(stderr, "    Below options are intended for use "
    137  1.2  christos 	    "with other options (except -l):\n");
    138  1.2  christos 	(void) fprintf(stderr, "        -A ignore assertions (-A), enable "
    139  1.2  christos 	    "panic recovery (-AA) or both (-AAA)\n");
    140  1.2  christos 	(void) fprintf(stderr, "        -F attempt automatic rewind within "
    141  1.2  christos 	    "safe range of transaction groups\n");
    142  1.2  christos 	(void) fprintf(stderr, "        -U <cachefile_path> -- use alternate "
    143  1.1      haad 	    "cachefile\n");
    144  1.2  christos 	(void) fprintf(stderr, "        -X attempt extreme rewind (does not "
    145  1.2  christos 	    "work with dataset)\n");
    146  1.2  christos 	(void) fprintf(stderr, "        -e pool is exported/destroyed/"
    147  1.2  christos 	    "has altroot/not in a cachefile\n");
    148  1.2  christos 	(void) fprintf(stderr, "        -p <path> -- use one or more with "
    149  1.2  christos 	    "-e to specify path to vdev dir\n");
    150  1.2  christos 	(void) fprintf(stderr, "        -t <txg> -- highest txg to use when "
    151  1.2  christos 	    "searching for uberblocks\n");
    152  1.1      haad 	(void) fprintf(stderr, "Specify an option more than once (e.g. -bb) "
    153  1.1      haad 	    "to make only that option verbose\n");
    154  1.1      haad 	(void) fprintf(stderr, "Default is to dump everything non-verbosely\n");
    155  1.1      haad 	exit(1);
    156  1.1      haad }
    157  1.1      haad 
    158  1.2  christos /*
    159  1.2  christos  * Called for usage errors that are discovered after a call to spa_open(),
    160  1.2  christos  * dmu_bonus_hold(), or pool_match().  abort() is called for other errors.
    161  1.2  christos  */
    162  1.2  christos 
    163  1.1      haad static void
    164  1.1      haad fatal(const char *fmt, ...)
    165  1.1      haad {
    166  1.1      haad 	va_list ap;
    167  1.1      haad 
    168  1.1      haad 	va_start(ap, fmt);
    169  1.1      haad 	(void) fprintf(stderr, "%s: ", cmdname);
    170  1.1      haad 	(void) vfprintf(stderr, fmt, ap);
    171  1.1      haad 	va_end(ap);
    172  1.1      haad 	(void) fprintf(stderr, "\n");
    173  1.1      haad 
    174  1.2  christos 	exit(1);
    175  1.1      haad }
    176  1.1      haad 
    177  1.1      haad /* ARGSUSED */
    178  1.1      haad static void
    179  1.1      haad dump_packed_nvlist(objset_t *os, uint64_t object, void *data, size_t size)
    180  1.1      haad {
    181  1.1      haad 	nvlist_t *nv;
    182  1.1      haad 	size_t nvsize = *(uint64_t *)data;
    183  1.1      haad 	char *packed = umem_alloc(nvsize, UMEM_NOFAIL);
    184  1.1      haad 
    185  1.2  christos 	VERIFY(0 == dmu_read(os, object, 0, nvsize, packed, DMU_READ_PREFETCH));
    186  1.1      haad 
    187  1.1      haad 	VERIFY(nvlist_unpack(packed, nvsize, &nv, 0) == 0);
    188  1.1      haad 
    189  1.1      haad 	umem_free(packed, nvsize);
    190  1.1      haad 
    191  1.1      haad 	dump_nvlist(nv, 8);
    192  1.1      haad 
    193  1.1      haad 	nvlist_free(nv);
    194  1.1      haad }
    195  1.1      haad 
    196  1.1      haad const char dump_zap_stars[] = "****************************************";
    197  1.1      haad const int dump_zap_width = sizeof (dump_zap_stars) - 1;
    198  1.1      haad 
    199  1.1      haad static void
    200  1.1      haad dump_zap_histogram(uint64_t histo[ZAP_HISTOGRAM_SIZE])
    201  1.1      haad {
    202  1.1      haad 	int i;
    203  1.1      haad 	int minidx = ZAP_HISTOGRAM_SIZE - 1;
    204  1.1      haad 	int maxidx = 0;
    205  1.1      haad 	uint64_t max = 0;
    206  1.1      haad 
    207  1.1      haad 	for (i = 0; i < ZAP_HISTOGRAM_SIZE; i++) {
    208  1.1      haad 		if (histo[i] > max)
    209  1.1      haad 			max = histo[i];
    210  1.1      haad 		if (histo[i] > 0 && i > maxidx)
    211  1.1      haad 			maxidx = i;
    212  1.1      haad 		if (histo[i] > 0 && i < minidx)
    213  1.1      haad 			minidx = i;
    214  1.1      haad 	}
    215  1.1      haad 
    216  1.1      haad 	if (max < dump_zap_width)
    217  1.1      haad 		max = dump_zap_width;
    218  1.1      haad 
    219  1.1      haad 	for (i = minidx; i <= maxidx; i++)
    220  1.1      haad 		(void) printf("\t\t\t%u: %6llu %s\n", i, (u_longlong_t)histo[i],
    221  1.1      haad 		    &dump_zap_stars[(max - histo[i]) * dump_zap_width / max]);
    222  1.1      haad }
    223  1.1      haad 
    224  1.1      haad static void
    225  1.1      haad dump_zap_stats(objset_t *os, uint64_t object)
    226  1.1      haad {
    227  1.1      haad 	int error;
    228  1.1      haad 	zap_stats_t zs;
    229  1.1      haad 
    230  1.1      haad 	error = zap_get_stats(os, object, &zs);
    231  1.1      haad 	if (error)
    232  1.1      haad 		return;
    233  1.1      haad 
    234  1.1      haad 	if (zs.zs_ptrtbl_len == 0) {
    235  1.1      haad 		ASSERT(zs.zs_num_blocks == 1);
    236  1.1      haad 		(void) printf("\tmicrozap: %llu bytes, %llu entries\n",
    237  1.1      haad 		    (u_longlong_t)zs.zs_blocksize,
    238  1.1      haad 		    (u_longlong_t)zs.zs_num_entries);
    239  1.1      haad 		return;
    240  1.1      haad 	}
    241  1.1      haad 
    242  1.1      haad 	(void) printf("\tFat ZAP stats:\n");
    243  1.1      haad 
    244  1.1      haad 	(void) printf("\t\tPointer table:\n");
    245  1.1      haad 	(void) printf("\t\t\t%llu elements\n",
    246  1.1      haad 	    (u_longlong_t)zs.zs_ptrtbl_len);
    247  1.1      haad 	(void) printf("\t\t\tzt_blk: %llu\n",
    248  1.1      haad 	    (u_longlong_t)zs.zs_ptrtbl_zt_blk);
    249  1.1      haad 	(void) printf("\t\t\tzt_numblks: %llu\n",
    250  1.1      haad 	    (u_longlong_t)zs.zs_ptrtbl_zt_numblks);
    251  1.1      haad 	(void) printf("\t\t\tzt_shift: %llu\n",
    252  1.1      haad 	    (u_longlong_t)zs.zs_ptrtbl_zt_shift);
    253  1.1      haad 	(void) printf("\t\t\tzt_blks_copied: %llu\n",
    254  1.1      haad 	    (u_longlong_t)zs.zs_ptrtbl_blks_copied);
    255  1.1      haad 	(void) printf("\t\t\tzt_nextblk: %llu\n",
    256  1.1      haad 	    (u_longlong_t)zs.zs_ptrtbl_nextblk);
    257  1.1      haad 
    258  1.1      haad 	(void) printf("\t\tZAP entries: %llu\n",
    259  1.1      haad 	    (u_longlong_t)zs.zs_num_entries);
    260  1.1      haad 	(void) printf("\t\tLeaf blocks: %llu\n",
    261  1.1      haad 	    (u_longlong_t)zs.zs_num_leafs);
    262  1.1      haad 	(void) printf("\t\tTotal blocks: %llu\n",
    263  1.1      haad 	    (u_longlong_t)zs.zs_num_blocks);
    264  1.1      haad 	(void) printf("\t\tzap_block_type: 0x%llx\n",
    265  1.1      haad 	    (u_longlong_t)zs.zs_block_type);
    266  1.1      haad 	(void) printf("\t\tzap_magic: 0x%llx\n",
    267  1.1      haad 	    (u_longlong_t)zs.zs_magic);
    268  1.1      haad 	(void) printf("\t\tzap_salt: 0x%llx\n",
    269  1.1      haad 	    (u_longlong_t)zs.zs_salt);
    270  1.1      haad 
    271  1.1      haad 	(void) printf("\t\tLeafs with 2^n pointers:\n");
    272  1.1      haad 	dump_zap_histogram(zs.zs_leafs_with_2n_pointers);
    273  1.1      haad 
    274  1.1      haad 	(void) printf("\t\tBlocks with n*5 entries:\n");
    275  1.1      haad 	dump_zap_histogram(zs.zs_blocks_with_n5_entries);
    276  1.1      haad 
    277  1.1      haad 	(void) printf("\t\tBlocks n/10 full:\n");
    278  1.1      haad 	dump_zap_histogram(zs.zs_blocks_n_tenths_full);
    279  1.1      haad 
    280  1.1      haad 	(void) printf("\t\tEntries with n chunks:\n");
    281  1.1      haad 	dump_zap_histogram(zs.zs_entries_using_n_chunks);
    282  1.1      haad 
    283  1.1      haad 	(void) printf("\t\tBuckets with n entries:\n");
    284  1.1      haad 	dump_zap_histogram(zs.zs_buckets_with_n_entries);
    285  1.1      haad }
    286  1.1      haad 
    287  1.1      haad /*ARGSUSED*/
    288  1.1      haad static void
    289  1.1      haad dump_none(objset_t *os, uint64_t object, void *data, size_t size)
    290  1.1      haad {
    291  1.1      haad }
    292  1.1      haad 
    293  1.1      haad /*ARGSUSED*/
    294  1.2  christos static void
    295  1.2  christos dump_unknown(objset_t *os, uint64_t object, void *data, size_t size)
    296  1.2  christos {
    297  1.2  christos 	(void) printf("\tUNKNOWN OBJECT TYPE\n");
    298  1.2  christos }
    299  1.2  christos 
    300  1.2  christos /*ARGSUSED*/
    301  1.1      haad void
    302  1.1      haad dump_uint8(objset_t *os, uint64_t object, void *data, size_t size)
    303  1.1      haad {
    304  1.1      haad }
    305  1.1      haad 
    306  1.1      haad /*ARGSUSED*/
    307  1.1      haad static void
    308  1.1      haad dump_uint64(objset_t *os, uint64_t object, void *data, size_t size)
    309  1.1      haad {
    310  1.1      haad }
    311  1.1      haad 
    312  1.1      haad /*ARGSUSED*/
    313  1.1      haad static void
    314  1.1      haad dump_zap(objset_t *os, uint64_t object, void *data, size_t size)
    315  1.1      haad {
    316  1.1      haad 	zap_cursor_t zc;
    317  1.1      haad 	zap_attribute_t attr;
    318  1.1      haad 	void *prop;
    319  1.1      haad 	int i;
    320  1.1      haad 
    321  1.1      haad 	dump_zap_stats(os, object);
    322  1.1      haad 	(void) printf("\n");
    323  1.1      haad 
    324  1.1      haad 	for (zap_cursor_init(&zc, os, object);
    325  1.1      haad 	    zap_cursor_retrieve(&zc, &attr) == 0;
    326  1.1      haad 	    zap_cursor_advance(&zc)) {
    327  1.1      haad 		(void) printf("\t\t%s = ", attr.za_name);
    328  1.1      haad 		if (attr.za_num_integers == 0) {
    329  1.1      haad 			(void) printf("\n");
    330  1.1      haad 			continue;
    331  1.1      haad 		}
    332  1.1      haad 		prop = umem_zalloc(attr.za_num_integers *
    333  1.1      haad 		    attr.za_integer_length, UMEM_NOFAIL);
    334  1.1      haad 		(void) zap_lookup(os, object, attr.za_name,
    335  1.1      haad 		    attr.za_integer_length, attr.za_num_integers, prop);
    336  1.1      haad 		if (attr.za_integer_length == 1) {
    337  1.1      haad 			(void) printf("%s", (char *)prop);
    338  1.1      haad 		} else {
    339  1.1      haad 			for (i = 0; i < attr.za_num_integers; i++) {
    340  1.1      haad 				switch (attr.za_integer_length) {
    341  1.1      haad 				case 2:
    342  1.1      haad 					(void) printf("%u ",
    343  1.1      haad 					    ((uint16_t *)prop)[i]);
    344  1.1      haad 					break;
    345  1.1      haad 				case 4:
    346  1.1      haad 					(void) printf("%u ",
    347  1.1      haad 					    ((uint32_t *)prop)[i]);
    348  1.1      haad 					break;
    349  1.1      haad 				case 8:
    350  1.1      haad 					(void) printf("%lld ",
    351  1.1      haad 					    (u_longlong_t)((int64_t *)prop)[i]);
    352  1.1      haad 					break;
    353  1.1      haad 				}
    354  1.1      haad 			}
    355  1.1      haad 		}
    356  1.1      haad 		(void) printf("\n");
    357  1.1      haad 		umem_free(prop, attr.za_num_integers * attr.za_integer_length);
    358  1.1      haad 	}
    359  1.1      haad 	zap_cursor_fini(&zc);
    360  1.1      haad }
    361  1.1      haad 
    362  1.1      haad /*ARGSUSED*/
    363  1.1      haad static void
    364  1.2  christos dump_ddt_zap(objset_t *os, uint64_t object, void *data, size_t size)
    365  1.2  christos {
    366  1.2  christos 	dump_zap_stats(os, object);
    367  1.2  christos 	/* contents are printed elsewhere, properly decoded */
    368  1.2  christos }
    369  1.2  christos 
    370  1.2  christos /*ARGSUSED*/
    371  1.2  christos static void
    372  1.1      haad dump_zpldir(objset_t *os, uint64_t object, void *data, size_t size)
    373  1.1      haad {
    374  1.1      haad 	zap_cursor_t zc;
    375  1.1      haad 	zap_attribute_t attr;
    376  1.1      haad 	const char *typenames[] = {
    377  1.1      haad 		/* 0 */ "not specified",
    378  1.1      haad 		/* 1 */ "FIFO",
    379  1.1      haad 		/* 2 */ "Character Device",
    380  1.1      haad 		/* 3 */ "3 (invalid)",
    381  1.1      haad 		/* 4 */ "Directory",
    382  1.1      haad 		/* 5 */ "5 (invalid)",
    383  1.1      haad 		/* 6 */ "Block Device",
    384  1.1      haad 		/* 7 */ "7 (invalid)",
    385  1.1      haad 		/* 8 */ "Regular File",
    386  1.1      haad 		/* 9 */ "9 (invalid)",
    387  1.1      haad 		/* 10 */ "Symbolic Link",
    388  1.1      haad 		/* 11 */ "11 (invalid)",
    389  1.1      haad 		/* 12 */ "Socket",
    390  1.1      haad 		/* 13 */ "Door",
    391  1.1      haad 		/* 14 */ "Event Port",
    392  1.1      haad 		/* 15 */ "15 (invalid)",
    393  1.1      haad 	};
    394  1.1      haad 
    395  1.1      haad 	dump_zap_stats(os, object);
    396  1.1      haad 	(void) printf("\n");
    397  1.1      haad 
    398  1.1      haad 	for (zap_cursor_init(&zc, os, object);
    399  1.1      haad 	    zap_cursor_retrieve(&zc, &attr) == 0;
    400  1.1      haad 	    zap_cursor_advance(&zc)) {
    401  1.1      haad 		(void) printf("\t\t%s = %lld (type: %s)\n",
    402  1.1      haad 		    attr.za_name, ZFS_DIRENT_OBJ(attr.za_first_integer),
    403  1.1      haad 		    typenames[ZFS_DIRENT_TYPE(attr.za_first_integer)]);
    404  1.1      haad 	}
    405  1.1      haad 	zap_cursor_fini(&zc);
    406  1.1      haad }
    407  1.1      haad 
    408  1.1      haad static void
    409  1.1      haad dump_spacemap(objset_t *os, space_map_obj_t *smo, space_map_t *sm)
    410  1.1      haad {
    411  1.1      haad 	uint64_t alloc, offset, entry;
    412  1.1      haad 	uint8_t mapshift = sm->sm_shift;
    413  1.1      haad 	uint64_t mapstart = sm->sm_start;
    414  1.1      haad 	char *ddata[] = { "ALLOC", "FREE", "CONDENSE", "INVALID",
    415  1.1      haad 			    "INVALID", "INVALID", "INVALID", "INVALID" };
    416  1.1      haad 
    417  1.1      haad 	if (smo->smo_object == 0)
    418  1.1      haad 		return;
    419  1.1      haad 
    420  1.1      haad 	/*
    421  1.1      haad 	 * Print out the freelist entries in both encoded and decoded form.
    422  1.1      haad 	 */
    423  1.1      haad 	alloc = 0;
    424  1.1      haad 	for (offset = 0; offset < smo->smo_objsize; offset += sizeof (entry)) {
    425  1.1      haad 		VERIFY(0 == dmu_read(os, smo->smo_object, offset,
    426  1.2  christos 		    sizeof (entry), &entry, DMU_READ_PREFETCH));
    427  1.1      haad 		if (SM_DEBUG_DECODE(entry)) {
    428  1.2  christos 			(void) printf("\t    [%6llu] %s: txg %llu, pass %llu\n",
    429  1.1      haad 			    (u_longlong_t)(offset / sizeof (entry)),
    430  1.1      haad 			    ddata[SM_DEBUG_ACTION_DECODE(entry)],
    431  1.1      haad 			    (u_longlong_t)SM_DEBUG_TXG_DECODE(entry),
    432  1.1      haad 			    (u_longlong_t)SM_DEBUG_SYNCPASS_DECODE(entry));
    433  1.1      haad 		} else {
    434  1.2  christos 			(void) printf("\t    [%6llu]    %c  range:"
    435  1.2  christos 			    " %010llx-%010llx  size: %06llx\n",
    436  1.1      haad 			    (u_longlong_t)(offset / sizeof (entry)),
    437  1.1      haad 			    SM_TYPE_DECODE(entry) == SM_ALLOC ? 'A' : 'F',
    438  1.1      haad 			    (u_longlong_t)((SM_OFFSET_DECODE(entry) <<
    439  1.1      haad 			    mapshift) + mapstart),
    440  1.1      haad 			    (u_longlong_t)((SM_OFFSET_DECODE(entry) <<
    441  1.1      haad 			    mapshift) + mapstart + (SM_RUN_DECODE(entry) <<
    442  1.1      haad 			    mapshift)),
    443  1.1      haad 			    (u_longlong_t)(SM_RUN_DECODE(entry) << mapshift));
    444  1.1      haad 			if (SM_TYPE_DECODE(entry) == SM_ALLOC)
    445  1.1      haad 				alloc += SM_RUN_DECODE(entry) << mapshift;
    446  1.1      haad 			else
    447  1.1      haad 				alloc -= SM_RUN_DECODE(entry) << mapshift;
    448  1.1      haad 		}
    449  1.1      haad 	}
    450  1.1      haad 	if (alloc != smo->smo_alloc) {
    451  1.1      haad 		(void) printf("space_map_object alloc (%llu) INCONSISTENT "
    452  1.1      haad 		    "with space map summary (%llu)\n",
    453  1.1      haad 		    (u_longlong_t)smo->smo_alloc, (u_longlong_t)alloc);
    454  1.1      haad 	}
    455  1.1      haad }
    456  1.1      haad 
    457  1.1      haad static void
    458  1.2  christos dump_metaslab_stats(metaslab_t *msp)
    459  1.2  christos {
    460  1.2  christos 	char maxbuf[5];
    461  1.2  christos 	space_map_t *sm = &msp->ms_map;
    462  1.2  christos 	avl_tree_t *t = sm->sm_pp_root;
    463  1.2  christos 	int free_pct = sm->sm_space * 100 / sm->sm_size;
    464  1.2  christos 
    465  1.2  christos 	nicenum(space_map_maxsize(sm), maxbuf);
    466  1.2  christos 
    467  1.2  christos 	(void) printf("\t %25s %10lu   %7s  %6s   %4s %4d%%\n",
    468  1.2  christos 	    "segments", avl_numnodes(t), "maxsize", maxbuf,
    469  1.2  christos 	    "freepct", free_pct);
    470  1.2  christos }
    471  1.2  christos 
    472  1.2  christos static void
    473  1.1      haad dump_metaslab(metaslab_t *msp)
    474  1.1      haad {
    475  1.1      haad 	vdev_t *vd = msp->ms_group->mg_vd;
    476  1.1      haad 	spa_t *spa = vd->vdev_spa;
    477  1.2  christos 	space_map_t *sm = &msp->ms_map;
    478  1.2  christos 	space_map_obj_t *smo = &msp->ms_smo;
    479  1.2  christos 	char freebuf[5];
    480  1.2  christos 
    481  1.2  christos 	nicenum(sm->sm_size - smo->smo_alloc, freebuf);
    482  1.1      haad 
    483  1.2  christos 	(void) printf(
    484  1.2  christos 	    "\tmetaslab %6llu   offset %12llx   spacemap %6llu   free    %5s\n",
    485  1.2  christos 	    (u_longlong_t)(sm->sm_start / sm->sm_size),
    486  1.2  christos 	    (u_longlong_t)sm->sm_start, (u_longlong_t)smo->smo_object, freebuf);
    487  1.1      haad 
    488  1.2  christos 	if (dump_opt['m'] > 1 && !dump_opt['L']) {
    489  1.2  christos 		mutex_enter(&msp->ms_lock);
    490  1.2  christos 		space_map_load_wait(sm);
    491  1.2  christos 		if (!sm->sm_loaded)
    492  1.2  christos 			VERIFY(space_map_load(sm, zfs_metaslab_ops,
    493  1.2  christos 			    SM_FREE, smo, spa->spa_meta_objset) == 0);
    494  1.2  christos 		dump_metaslab_stats(msp);
    495  1.2  christos 		space_map_unload(sm);
    496  1.2  christos 		mutex_exit(&msp->ms_lock);
    497  1.1      haad 	}
    498  1.1      haad 
    499  1.2  christos 	if (dump_opt['d'] > 5 || dump_opt['m'] > 2) {
    500  1.2  christos 		ASSERT(sm->sm_size == (1ULL << vd->vdev_ms_shift));
    501  1.1      haad 
    502  1.2  christos 		mutex_enter(&msp->ms_lock);
    503  1.2  christos 		dump_spacemap(spa->spa_meta_objset, smo, sm);
    504  1.2  christos 		mutex_exit(&msp->ms_lock);
    505  1.2  christos 	}
    506  1.2  christos }
    507  1.1      haad 
    508  1.2  christos static void
    509  1.2  christos print_vdev_metaslab_header(vdev_t *vd)
    510  1.2  christos {
    511  1.2  christos 	(void) printf("\tvdev %10llu\n\t%-10s%5llu   %-19s   %-15s   %-10s\n",
    512  1.2  christos 	    (u_longlong_t)vd->vdev_id,
    513  1.2  christos 	    "metaslabs", (u_longlong_t)vd->vdev_ms_count,
    514  1.2  christos 	    "offset", "spacemap", "free");
    515  1.2  christos 	(void) printf("\t%15s   %19s   %15s   %10s\n",
    516  1.2  christos 	    "---------------", "-------------------",
    517  1.2  christos 	    "---------------", "-------------");
    518  1.1      haad }
    519  1.1      haad 
    520  1.1      haad static void
    521  1.1      haad dump_metaslabs(spa_t *spa)
    522  1.1      haad {
    523  1.2  christos 	vdev_t *vd, *rvd = spa->spa_root_vdev;
    524  1.2  christos 	uint64_t m, c = 0, children = rvd->vdev_children;
    525  1.1      haad 
    526  1.1      haad 	(void) printf("\nMetaslabs:\n");
    527  1.1      haad 
    528  1.2  christos 	if (!dump_opt['d'] && zopt_objects > 0) {
    529  1.2  christos 		c = zopt_object[0];
    530  1.1      haad 
    531  1.2  christos 		if (c >= children)
    532  1.2  christos 			(void) fatal("bad vdev id: %llu", (u_longlong_t)c);
    533  1.1      haad 
    534  1.2  christos 		if (zopt_objects > 1) {
    535  1.2  christos 			vd = rvd->vdev_child[c];
    536  1.2  christos 			print_vdev_metaslab_header(vd);
    537  1.2  christos 
    538  1.2  christos 			for (m = 1; m < zopt_objects; m++) {
    539  1.2  christos 				if (zopt_object[m] < vd->vdev_ms_count)
    540  1.2  christos 					dump_metaslab(
    541  1.2  christos 					    vd->vdev_ms[zopt_object[m]]);
    542  1.2  christos 				else
    543  1.2  christos 					(void) fprintf(stderr, "bad metaslab "
    544  1.2  christos 					    "number %llu\n",
    545  1.2  christos 					    (u_longlong_t)zopt_object[m]);
    546  1.2  christos 			}
    547  1.2  christos 			(void) printf("\n");
    548  1.2  christos 			return;
    549  1.1      haad 		}
    550  1.2  christos 		children = c + 1;
    551  1.2  christos 	}
    552  1.2  christos 	for (; c < children; c++) {
    553  1.2  christos 		vd = rvd->vdev_child[c];
    554  1.2  christos 		print_vdev_metaslab_header(vd);
    555  1.2  christos 
    556  1.1      haad 		for (m = 0; m < vd->vdev_ms_count; m++)
    557  1.1      haad 			dump_metaslab(vd->vdev_ms[m]);
    558  1.1      haad 		(void) printf("\n");
    559  1.1      haad 	}
    560  1.1      haad }
    561  1.1      haad 
    562  1.1      haad static void
    563  1.2  christos dump_dde(const ddt_t *ddt, const ddt_entry_t *dde, uint64_t index)
    564  1.2  christos {
    565  1.2  christos 	const ddt_phys_t *ddp = dde->dde_phys;
    566  1.2  christos 	const ddt_key_t *ddk = &dde->dde_key;
    567  1.2  christos 	char *types[4] = { "ditto", "single", "double", "triple" };
    568  1.2  christos 	char blkbuf[BP_SPRINTF_LEN];
    569  1.2  christos 	blkptr_t blk;
    570  1.2  christos 
    571  1.2  christos 	for (int p = 0; p < DDT_PHYS_TYPES; p++, ddp++) {
    572  1.2  christos 		if (ddp->ddp_phys_birth == 0)
    573  1.2  christos 			continue;
    574  1.2  christos 		ddt_bp_create(ddt->ddt_checksum, ddk, ddp, &blk);
    575  1.2  christos 		snprintf_blkptr(blkbuf, sizeof(blkbuf), &blk);
    576  1.2  christos 		(void) printf("index %llx refcnt %llu %s %s\n",
    577  1.2  christos 		    (u_longlong_t)index, (u_longlong_t)ddp->ddp_refcnt,
    578  1.2  christos 		    types[p], blkbuf);
    579  1.2  christos 	}
    580  1.2  christos }
    581  1.2  christos 
    582  1.2  christos static void
    583  1.2  christos dump_dedup_ratio(const ddt_stat_t *dds)
    584  1.2  christos {
    585  1.2  christos 	double rL, rP, rD, D, dedup, compress, copies;
    586  1.2  christos 
    587  1.2  christos 	if (dds->dds_blocks == 0)
    588  1.2  christos 		return;
    589  1.2  christos 
    590  1.2  christos 	rL = (double)dds->dds_ref_lsize;
    591  1.2  christos 	rP = (double)dds->dds_ref_psize;
    592  1.2  christos 	rD = (double)dds->dds_ref_dsize;
    593  1.2  christos 	D = (double)dds->dds_dsize;
    594  1.2  christos 
    595  1.2  christos 	dedup = rD / D;
    596  1.2  christos 	compress = rL / rP;
    597  1.2  christos 	copies = rD / rP;
    598  1.2  christos 
    599  1.2  christos 	(void) printf("dedup = %.2f, compress = %.2f, copies = %.2f, "
    600  1.2  christos 	    "dedup * compress / copies = %.2f\n\n",
    601  1.2  christos 	    dedup, compress, copies, dedup * compress / copies);
    602  1.2  christos }
    603  1.2  christos 
    604  1.2  christos static void
    605  1.2  christos dump_ddt(ddt_t *ddt, enum ddt_type type, enum ddt_class class)
    606  1.2  christos {
    607  1.2  christos 	char name[DDT_NAMELEN];
    608  1.2  christos 	ddt_entry_t dde;
    609  1.2  christos 	uint64_t walk = 0;
    610  1.2  christos 	dmu_object_info_t doi;
    611  1.2  christos 	uint64_t count, dspace, mspace;
    612  1.2  christos 	int error;
    613  1.2  christos 
    614  1.2  christos 	error = ddt_object_info(ddt, type, class, &doi);
    615  1.2  christos 
    616  1.2  christos 	if (error == ENOENT)
    617  1.2  christos 		return;
    618  1.2  christos 	ASSERT(error == 0);
    619  1.2  christos 
    620  1.2  christos 	count = ddt_object_count(ddt, type, class);
    621  1.2  christos 	dspace = doi.doi_physical_blocks_512 << 9;
    622  1.2  christos 	mspace = doi.doi_fill_count * doi.doi_data_block_size;
    623  1.2  christos 
    624  1.2  christos 	ASSERT(count != 0);	/* we should have destroyed it */
    625  1.2  christos 
    626  1.2  christos 	ddt_object_name(ddt, type, class, name);
    627  1.2  christos 
    628  1.2  christos 	(void) printf("%s: %llu entries, size %llu on disk, %llu in core\n",
    629  1.2  christos 	    name,
    630  1.2  christos 	    (u_longlong_t)count,
    631  1.2  christos 	    (u_longlong_t)(dspace / count),
    632  1.2  christos 	    (u_longlong_t)(mspace / count));
    633  1.2  christos 
    634  1.2  christos 	if (dump_opt['D'] < 3)
    635  1.2  christos 		return;
    636  1.2  christos 
    637  1.2  christos 	zpool_dump_ddt(NULL, &ddt->ddt_histogram[type][class]);
    638  1.2  christos 
    639  1.2  christos 	if (dump_opt['D'] < 4)
    640  1.2  christos 		return;
    641  1.2  christos 
    642  1.2  christos 	if (dump_opt['D'] < 5 && class == DDT_CLASS_UNIQUE)
    643  1.2  christos 		return;
    644  1.2  christos 
    645  1.2  christos 	(void) printf("%s contents:\n\n", name);
    646  1.2  christos 
    647  1.2  christos 	while ((error = ddt_object_walk(ddt, type, class, &walk, &dde)) == 0)
    648  1.2  christos 		dump_dde(ddt, &dde, walk);
    649  1.2  christos 
    650  1.2  christos 	ASSERT(error == ENOENT);
    651  1.2  christos 
    652  1.2  christos 	(void) printf("\n");
    653  1.2  christos }
    654  1.2  christos 
    655  1.2  christos static void
    656  1.2  christos dump_all_ddts(spa_t *spa)
    657  1.2  christos {
    658  1.2  christos 	ddt_histogram_t ddh_total = { 0 };
    659  1.2  christos 	ddt_stat_t dds_total = { 0 };
    660  1.2  christos 
    661  1.2  christos 	for (enum zio_checksum c = 0; c < ZIO_CHECKSUM_FUNCTIONS; c++) {
    662  1.2  christos 		ddt_t *ddt = spa->spa_ddt[c];
    663  1.2  christos 		for (enum ddt_type type = 0; type < DDT_TYPES; type++) {
    664  1.2  christos 			for (enum ddt_class class = 0; class < DDT_CLASSES;
    665  1.2  christos 			    class++) {
    666  1.2  christos 				dump_ddt(ddt, type, class);
    667  1.2  christos 			}
    668  1.2  christos 		}
    669  1.2  christos 	}
    670  1.2  christos 
    671  1.2  christos 	ddt_get_dedup_stats(spa, &dds_total);
    672  1.2  christos 
    673  1.2  christos 	if (dds_total.dds_blocks == 0) {
    674  1.2  christos 		(void) printf("All DDTs are empty\n");
    675  1.2  christos 		return;
    676  1.2  christos 	}
    677  1.2  christos 
    678  1.2  christos 	(void) printf("\n");
    679  1.2  christos 
    680  1.2  christos 	if (dump_opt['D'] > 1) {
    681  1.2  christos 		(void) printf("DDT histogram (aggregated over all DDTs):\n");
    682  1.2  christos 		ddt_get_dedup_histogram(spa, &ddh_total);
    683  1.2  christos 		zpool_dump_ddt(&dds_total, &ddh_total);
    684  1.2  christos 	}
    685  1.2  christos 
    686  1.2  christos 	dump_dedup_ratio(&dds_total);
    687  1.2  christos }
    688  1.2  christos 
    689  1.2  christos static void
    690  1.2  christos dump_dtl_seg(space_map_t *sm, uint64_t start, uint64_t size)
    691  1.2  christos {
    692  1.2  christos 	char *prefix = (void *)sm;
    693  1.2  christos 
    694  1.2  christos 	(void) printf("%s [%llu,%llu) length %llu\n",
    695  1.2  christos 	    prefix,
    696  1.2  christos 	    (u_longlong_t)start,
    697  1.2  christos 	    (u_longlong_t)(start + size),
    698  1.2  christos 	    (u_longlong_t)(size));
    699  1.2  christos }
    700  1.2  christos 
    701  1.2  christos static void
    702  1.1      haad dump_dtl(vdev_t *vd, int indent)
    703  1.1      haad {
    704  1.2  christos 	spa_t *spa = vd->vdev_spa;
    705  1.2  christos 	boolean_t required;
    706  1.2  christos 	char *name[DTL_TYPES] = { "missing", "partial", "scrub", "outage" };
    707  1.2  christos 	char prefix[256];
    708  1.2  christos 
    709  1.2  christos 	spa_vdev_state_enter(spa, SCL_NONE);
    710  1.2  christos 	required = vdev_dtl_required(vd);
    711  1.2  christos 	(void) spa_vdev_state_exit(spa, NULL, 0);
    712  1.1      haad 
    713  1.1      haad 	if (indent == 0)
    714  1.1      haad 		(void) printf("\nDirty time logs:\n\n");
    715  1.1      haad 
    716  1.2  christos 	(void) printf("\t%*s%s [%s]\n", indent, "",
    717  1.1      haad 	    vd->vdev_path ? vd->vdev_path :
    718  1.2  christos 	    vd->vdev_parent ? vd->vdev_ops->vdev_op_type : spa_name(spa),
    719  1.2  christos 	    required ? "DTL-required" : "DTL-expendable");
    720  1.1      haad 
    721  1.2  christos 	for (int t = 0; t < DTL_TYPES; t++) {
    722  1.2  christos 		space_map_t *sm = &vd->vdev_dtl[t];
    723  1.2  christos 		if (sm->sm_space == 0)
    724  1.2  christos 			continue;
    725  1.2  christos 		(void) snprintf(prefix, sizeof (prefix), "\t%*s%s",
    726  1.2  christos 		    indent + 2, "", name[t]);
    727  1.2  christos 		mutex_enter(sm->sm_lock);
    728  1.2  christos 		space_map_walk(sm, dump_dtl_seg, (void *)prefix);
    729  1.2  christos 		mutex_exit(sm->sm_lock);
    730  1.2  christos 		if (dump_opt['d'] > 5 && vd->vdev_children == 0)
    731  1.2  christos 			dump_spacemap(spa->spa_meta_objset,
    732  1.2  christos 			    &vd->vdev_dtl_smo, sm);
    733  1.1      haad 	}
    734  1.1      haad 
    735  1.2  christos 	for (int c = 0; c < vd->vdev_children; c++)
    736  1.2  christos 		dump_dtl(vd->vdev_child[c], indent + 4);
    737  1.2  christos }
    738  1.2  christos 
    739  1.2  christos static void
    740  1.2  christos dump_history(spa_t *spa)
    741  1.2  christos {
    742  1.2  christos 	nvlist_t **events = NULL;
    743  1.2  christos 	char buf[SPA_MAXBLOCKSIZE];
    744  1.2  christos 	uint64_t resid, len, off = 0;
    745  1.2  christos 	uint_t num = 0;
    746  1.2  christos 	int error;
    747  1.2  christos 	time_t tsec;
    748  1.2  christos 	struct tm t;
    749  1.2  christos 	char tbuf[30];
    750  1.2  christos 	char internalstr[MAXPATHLEN];
    751  1.2  christos 
    752  1.2  christos 	do {
    753  1.2  christos 		len = sizeof (buf);
    754  1.2  christos 
    755  1.2  christos 		if ((error = spa_history_get(spa, &off, &len, buf)) != 0) {
    756  1.2  christos 			(void) fprintf(stderr, "Unable to read history: "
    757  1.2  christos 			    "error %d\n", error);
    758  1.2  christos 			return;
    759  1.2  christos 		}
    760  1.2  christos 
    761  1.2  christos 		if (zpool_history_unpack(buf, len, &resid, &events, &num) != 0)
    762  1.2  christos 			break;
    763  1.2  christos 
    764  1.2  christos 		off -= resid;
    765  1.2  christos 	} while (len != 0);
    766  1.2  christos 
    767  1.2  christos 	(void) printf("\nHistory:\n");
    768  1.2  christos 	for (int i = 0; i < num; i++) {
    769  1.2  christos 		uint64_t time, txg, ievent;
    770  1.2  christos 		char *cmd, *intstr;
    771  1.2  christos 
    772  1.2  christos 		if (nvlist_lookup_uint64(events[i], ZPOOL_HIST_TIME,
    773  1.2  christos 		    &time) != 0)
    774  1.2  christos 			continue;
    775  1.2  christos 		if (nvlist_lookup_string(events[i], ZPOOL_HIST_CMD,
    776  1.2  christos 		    &cmd) != 0) {
    777  1.2  christos 			if (nvlist_lookup_uint64(events[i],
    778  1.2  christos 			    ZPOOL_HIST_INT_EVENT, &ievent) != 0)
    779  1.2  christos 				continue;
    780  1.2  christos 			verify(nvlist_lookup_uint64(events[i],
    781  1.2  christos 			    ZPOOL_HIST_TXG, &txg) == 0);
    782  1.2  christos 			verify(nvlist_lookup_string(events[i],
    783  1.2  christos 			    ZPOOL_HIST_INT_STR, &intstr) == 0);
    784  1.2  christos 			if (ievent >= LOG_END)
    785  1.2  christos 				continue;
    786  1.1      haad 
    787  1.2  christos 			(void) snprintf(internalstr,
    788  1.2  christos 			    sizeof (internalstr),
    789  1.2  christos 			    "[internal %s txg:%lld] %s",
    790  1.2  christos 			    hist_event_table[ievent], txg,
    791  1.2  christos 			    intstr);
    792  1.2  christos 			cmd = internalstr;
    793  1.2  christos 		}
    794  1.2  christos 		tsec = time;
    795  1.2  christos 		(void) localtime_r(&tsec, &t);
    796  1.2  christos 		(void) strftime(tbuf, sizeof (tbuf), "%F.%T", &t);
    797  1.2  christos 		(void) printf("%s %s\n", tbuf, cmd);
    798  1.1      haad 	}
    799  1.1      haad }
    800  1.1      haad 
    801  1.1      haad /*ARGSUSED*/
    802  1.1      haad static void
    803  1.1      haad dump_dnode(objset_t *os, uint64_t object, void *data, size_t size)
    804  1.1      haad {
    805  1.1      haad }
    806  1.1      haad 
    807  1.1      haad static uint64_t
    808  1.2  christos blkid2offset(const dnode_phys_t *dnp, const blkptr_t *bp, const zbookmark_t *zb)
    809  1.1      haad {
    810  1.2  christos 	if (dnp == NULL) {
    811  1.2  christos 		ASSERT(zb->zb_level < 0);
    812  1.2  christos 		if (zb->zb_object == 0)
    813  1.2  christos 			return (zb->zb_blkid);
    814  1.2  christos 		return (zb->zb_blkid * BP_GET_LSIZE(bp));
    815  1.2  christos 	}
    816  1.2  christos 
    817  1.2  christos 	ASSERT(zb->zb_level >= 0);
    818  1.1      haad 
    819  1.2  christos 	return ((zb->zb_blkid <<
    820  1.2  christos 	    (zb->zb_level * (dnp->dn_indblkshift - SPA_BLKPTRSHIFT))) *
    821  1.1      haad 	    dnp->dn_datablkszsec << SPA_MINBLOCKSHIFT);
    822  1.1      haad }
    823  1.1      haad 
    824  1.1      haad static void
    825  1.2  christos snprintf_blkptr_compact(char *blkbuf, size_t blklen, blkptr_t *bp)
    826  1.1      haad {
    827  1.1      haad 	dva_t *dva = bp->blk_dva;
    828  1.2  christos 	int ndvas = dump_opt['d'] > 5 ? BP_GET_NDVAS(bp) : 1;
    829  1.2  christos 	size_t len;
    830  1.2  christos 
    831  1.2  christos 	if (dump_opt['b'] >= 5) {
    832  1.2  christos 		snprintf_blkptr(blkbuf, blklen, bp);
    833  1.2  christos 		return;
    834  1.2  christos 	}
    835  1.1      haad 
    836  1.1      haad 	blkbuf[0] = '\0';
    837  1.1      haad 
    838  1.2  christos 	len = 0;
    839  1.2  christos 	for (int i = 0; i < ndvas; i++) {
    840  1.2  christos 		len += snprintf(blkbuf + len, blklen - len, "%llu:%llx:%llx ",
    841  1.1      haad 		    (u_longlong_t)DVA_GET_VDEV(&dva[i]),
    842  1.1      haad 		    (u_longlong_t)DVA_GET_OFFSET(&dva[i]),
    843  1.1      haad 		    (u_longlong_t)DVA_GET_ASIZE(&dva[i]));
    844  1.2  christos 		if (len > blklen)
    845  1.2  christos 			len = blklen;
    846  1.2  christos 	}
    847  1.1      haad 
    848  1.2  christos 	snprintf(blkbuf + len, blklen - len,
    849  1.2  christos 	    "%llxL/%llxP F=%llu B=%llu/%llu",
    850  1.1      haad 	    (u_longlong_t)BP_GET_LSIZE(bp),
    851  1.1      haad 	    (u_longlong_t)BP_GET_PSIZE(bp),
    852  1.1      haad 	    (u_longlong_t)bp->blk_fill,
    853  1.2  christos 	    (u_longlong_t)bp->blk_birth,
    854  1.2  christos 	    (u_longlong_t)BP_PHYSICAL_BIRTH(bp));
    855  1.1      haad }
    856  1.1      haad 
    857  1.1      haad static void
    858  1.1      haad print_indirect(blkptr_t *bp, const zbookmark_t *zb,
    859  1.1      haad     const dnode_phys_t *dnp)
    860  1.1      haad {
    861  1.1      haad 	char blkbuf[BP_SPRINTF_LEN];
    862  1.1      haad 	int l;
    863  1.1      haad 
    864  1.1      haad 	ASSERT3U(BP_GET_TYPE(bp), ==, dnp->dn_type);
    865  1.1      haad 	ASSERT3U(BP_GET_LEVEL(bp), ==, zb->zb_level);
    866  1.1      haad 
    867  1.2  christos 	(void) printf("%16llx ", (u_longlong_t)blkid2offset(dnp, bp, zb));
    868  1.1      haad 
    869  1.1      haad 	ASSERT(zb->zb_level >= 0);
    870  1.1      haad 
    871  1.1      haad 	for (l = dnp->dn_nlevels - 1; l >= -1; l--) {
    872  1.1      haad 		if (l == zb->zb_level) {
    873  1.1      haad 			(void) printf("L%llx", (u_longlong_t)zb->zb_level);
    874  1.1      haad 		} else {
    875  1.1      haad 			(void) printf(" ");
    876  1.1      haad 		}
    877  1.1      haad 	}
    878  1.1      haad 
    879  1.2  christos 	snprintf_blkptr_compact(blkbuf, sizeof(blkbuf), bp);
    880  1.1      haad 	(void) printf("%s\n", blkbuf);
    881  1.1      haad }
    882  1.1      haad 
    883  1.1      haad static int
    884  1.1      haad visit_indirect(spa_t *spa, const dnode_phys_t *dnp,
    885  1.1      haad     blkptr_t *bp, const zbookmark_t *zb)
    886  1.1      haad {
    887  1.2  christos 	int err = 0;
    888  1.1      haad 
    889  1.1      haad 	if (bp->blk_birth == 0)
    890  1.1      haad 		return (0);
    891  1.1      haad 
    892  1.1      haad 	print_indirect(bp, zb, dnp);
    893  1.1      haad 
    894  1.1      haad 	if (BP_GET_LEVEL(bp) > 0) {
    895  1.1      haad 		uint32_t flags = ARC_WAIT;
    896  1.1      haad 		int i;
    897  1.1      haad 		blkptr_t *cbp;
    898  1.1      haad 		int epb = BP_GET_LSIZE(bp) >> SPA_BLKPTRSHIFT;
    899  1.1      haad 		arc_buf_t *buf;
    900  1.1      haad 		uint64_t fill = 0;
    901  1.1      haad 
    902  1.1      haad 		err = arc_read_nolock(NULL, spa, bp, arc_getbuf_func, &buf,
    903  1.1      haad 		    ZIO_PRIORITY_ASYNC_READ, ZIO_FLAG_CANFAIL, &flags, zb);
    904  1.1      haad 		if (err)
    905  1.1      haad 			return (err);
    906  1.1      haad 
    907  1.1      haad 		/* recursively visit blocks below this */
    908  1.1      haad 		cbp = buf->b_data;
    909  1.1      haad 		for (i = 0; i < epb; i++, cbp++) {
    910  1.1      haad 			zbookmark_t czb;
    911  1.1      haad 
    912  1.1      haad 			SET_BOOKMARK(&czb, zb->zb_objset, zb->zb_object,
    913  1.1      haad 			    zb->zb_level - 1,
    914  1.1      haad 			    zb->zb_blkid * epb + i);
    915  1.1      haad 			err = visit_indirect(spa, dnp, cbp, &czb);
    916  1.1      haad 			if (err)
    917  1.1      haad 				break;
    918  1.1      haad 			fill += cbp->blk_fill;
    919  1.1      haad 		}
    920  1.2  christos 		if (!err)
    921  1.2  christos 			ASSERT3U(fill, ==, bp->blk_fill);
    922  1.1      haad 		(void) arc_buf_remove_ref(buf, &buf);
    923  1.1      haad 	}
    924  1.1      haad 
    925  1.1      haad 	return (err);
    926  1.1      haad }
    927  1.1      haad 
    928  1.1      haad /*ARGSUSED*/
    929  1.1      haad static void
    930  1.1      haad dump_indirect(dnode_t *dn)
    931  1.1      haad {
    932  1.1      haad 	dnode_phys_t *dnp = dn->dn_phys;
    933  1.1      haad 	int j;
    934  1.1      haad 	zbookmark_t czb;
    935  1.1      haad 
    936  1.1      haad 	(void) printf("Indirect blocks:\n");
    937  1.1      haad 
    938  1.2  christos 	SET_BOOKMARK(&czb, dmu_objset_id(dn->dn_objset),
    939  1.1      haad 	    dn->dn_object, dnp->dn_nlevels - 1, 0);
    940  1.1      haad 	for (j = 0; j < dnp->dn_nblkptr; j++) {
    941  1.1      haad 		czb.zb_blkid = j;
    942  1.2  christos 		(void) visit_indirect(dmu_objset_spa(dn->dn_objset), dnp,
    943  1.1      haad 		    &dnp->dn_blkptr[j], &czb);
    944  1.1      haad 	}
    945  1.1      haad 
    946  1.1      haad 	(void) printf("\n");
    947  1.1      haad }
    948  1.1      haad 
    949  1.1      haad /*ARGSUSED*/
    950  1.1      haad static void
    951  1.1      haad dump_dsl_dir(objset_t *os, uint64_t object, void *data, size_t size)
    952  1.1      haad {
    953  1.1      haad 	dsl_dir_phys_t *dd = data;
    954  1.1      haad 	time_t crtime;
    955  1.1      haad 	char nice[6];
    956  1.1      haad 
    957  1.1      haad 	if (dd == NULL)
    958  1.1      haad 		return;
    959  1.1      haad 
    960  1.1      haad 	ASSERT3U(size, >=, sizeof (dsl_dir_phys_t));
    961  1.1      haad 
    962  1.1      haad 	crtime = dd->dd_creation_time;
    963  1.1      haad 	(void) printf("\t\tcreation_time = %s", ctime(&crtime));
    964  1.1      haad 	(void) printf("\t\thead_dataset_obj = %llu\n",
    965  1.1      haad 	    (u_longlong_t)dd->dd_head_dataset_obj);
    966  1.1      haad 	(void) printf("\t\tparent_dir_obj = %llu\n",
    967  1.1      haad 	    (u_longlong_t)dd->dd_parent_obj);
    968  1.1      haad 	(void) printf("\t\torigin_obj = %llu\n",
    969  1.1      haad 	    (u_longlong_t)dd->dd_origin_obj);
    970  1.1      haad 	(void) printf("\t\tchild_dir_zapobj = %llu\n",
    971  1.1      haad 	    (u_longlong_t)dd->dd_child_dir_zapobj);
    972  1.1      haad 	nicenum(dd->dd_used_bytes, nice);
    973  1.1      haad 	(void) printf("\t\tused_bytes = %s\n", nice);
    974  1.1      haad 	nicenum(dd->dd_compressed_bytes, nice);
    975  1.1      haad 	(void) printf("\t\tcompressed_bytes = %s\n", nice);
    976  1.1      haad 	nicenum(dd->dd_uncompressed_bytes, nice);
    977  1.1      haad 	(void) printf("\t\tuncompressed_bytes = %s\n", nice);
    978  1.1      haad 	nicenum(dd->dd_quota, nice);
    979  1.1      haad 	(void) printf("\t\tquota = %s\n", nice);
    980  1.1      haad 	nicenum(dd->dd_reserved, nice);
    981  1.1      haad 	(void) printf("\t\treserved = %s\n", nice);
    982  1.1      haad 	(void) printf("\t\tprops_zapobj = %llu\n",
    983  1.1      haad 	    (u_longlong_t)dd->dd_props_zapobj);
    984  1.1      haad 	(void) printf("\t\tdeleg_zapobj = %llu\n",
    985  1.1      haad 	    (u_longlong_t)dd->dd_deleg_zapobj);
    986  1.1      haad 	(void) printf("\t\tflags = %llx\n",
    987  1.1      haad 	    (u_longlong_t)dd->dd_flags);
    988  1.1      haad 
    989  1.1      haad #define	DO(which) \
    990  1.1      haad 	nicenum(dd->dd_used_breakdown[DD_USED_ ## which], nice); \
    991  1.1      haad 	(void) printf("\t\tused_breakdown[" #which "] = %s\n", nice)
    992  1.1      haad 	DO(HEAD);
    993  1.1      haad 	DO(SNAP);
    994  1.1      haad 	DO(CHILD);
    995  1.1      haad 	DO(CHILD_RSRV);
    996  1.1      haad 	DO(REFRSRV);
    997  1.1      haad #undef DO
    998  1.1      haad }
    999  1.1      haad 
   1000  1.1      haad /*ARGSUSED*/
   1001  1.1      haad static void
   1002  1.1      haad dump_dsl_dataset(objset_t *os, uint64_t object, void *data, size_t size)
   1003  1.1      haad {
   1004  1.1      haad 	dsl_dataset_phys_t *ds = data;
   1005  1.1      haad 	time_t crtime;
   1006  1.1      haad 	char used[6], compressed[6], uncompressed[6], unique[6];
   1007  1.1      haad 	char blkbuf[BP_SPRINTF_LEN];
   1008  1.1      haad 
   1009  1.1      haad 	if (ds == NULL)
   1010  1.1      haad 		return;
   1011  1.1      haad 
   1012  1.1      haad 	ASSERT(size == sizeof (*ds));
   1013  1.1      haad 	crtime = ds->ds_creation_time;
   1014  1.1      haad 	nicenum(ds->ds_used_bytes, used);
   1015  1.1      haad 	nicenum(ds->ds_compressed_bytes, compressed);
   1016  1.1      haad 	nicenum(ds->ds_uncompressed_bytes, uncompressed);
   1017  1.1      haad 	nicenum(ds->ds_unique_bytes, unique);
   1018  1.2  christos 	snprintf_blkptr(blkbuf, sizeof(blkbuf), &ds->ds_bp);
   1019  1.1      haad 
   1020  1.1      haad 	(void) printf("\t\tdir_obj = %llu\n",
   1021  1.1      haad 	    (u_longlong_t)ds->ds_dir_obj);
   1022  1.1      haad 	(void) printf("\t\tprev_snap_obj = %llu\n",
   1023  1.1      haad 	    (u_longlong_t)ds->ds_prev_snap_obj);
   1024  1.1      haad 	(void) printf("\t\tprev_snap_txg = %llu\n",
   1025  1.1      haad 	    (u_longlong_t)ds->ds_prev_snap_txg);
   1026  1.1      haad 	(void) printf("\t\tnext_snap_obj = %llu\n",
   1027  1.1      haad 	    (u_longlong_t)ds->ds_next_snap_obj);
   1028  1.1      haad 	(void) printf("\t\tsnapnames_zapobj = %llu\n",
   1029  1.1      haad 	    (u_longlong_t)ds->ds_snapnames_zapobj);
   1030  1.1      haad 	(void) printf("\t\tnum_children = %llu\n",
   1031  1.1      haad 	    (u_longlong_t)ds->ds_num_children);
   1032  1.2  christos 	(void) printf("\t\tuserrefs_obj = %llu\n",
   1033  1.2  christos 	    (u_longlong_t)ds->ds_userrefs_obj);
   1034  1.1      haad 	(void) printf("\t\tcreation_time = %s", ctime(&crtime));
   1035  1.1      haad 	(void) printf("\t\tcreation_txg = %llu\n",
   1036  1.1      haad 	    (u_longlong_t)ds->ds_creation_txg);
   1037  1.1      haad 	(void) printf("\t\tdeadlist_obj = %llu\n",
   1038  1.1      haad 	    (u_longlong_t)ds->ds_deadlist_obj);
   1039  1.1      haad 	(void) printf("\t\tused_bytes = %s\n", used);
   1040  1.1      haad 	(void) printf("\t\tcompressed_bytes = %s\n", compressed);
   1041  1.1      haad 	(void) printf("\t\tuncompressed_bytes = %s\n", uncompressed);
   1042  1.1      haad 	(void) printf("\t\tunique = %s\n", unique);
   1043  1.1      haad 	(void) printf("\t\tfsid_guid = %llu\n",
   1044  1.1      haad 	    (u_longlong_t)ds->ds_fsid_guid);
   1045  1.1      haad 	(void) printf("\t\tguid = %llu\n",
   1046  1.1      haad 	    (u_longlong_t)ds->ds_guid);
   1047  1.1      haad 	(void) printf("\t\tflags = %llx\n",
   1048  1.1      haad 	    (u_longlong_t)ds->ds_flags);
   1049  1.1      haad 	(void) printf("\t\tnext_clones_obj = %llu\n",
   1050  1.1      haad 	    (u_longlong_t)ds->ds_next_clones_obj);
   1051  1.1      haad 	(void) printf("\t\tprops_obj = %llu\n",
   1052  1.1      haad 	    (u_longlong_t)ds->ds_props_obj);
   1053  1.1      haad 	(void) printf("\t\tbp = %s\n", blkbuf);
   1054  1.1      haad }
   1055  1.1      haad 
   1056  1.1      haad static void
   1057  1.1      haad dump_bplist(objset_t *mos, uint64_t object, char *name)
   1058  1.1      haad {
   1059  1.1      haad 	bplist_t bpl = { 0 };
   1060  1.1      haad 	blkptr_t blk, *bp = &blk;
   1061  1.1      haad 	uint64_t itor = 0;
   1062  1.1      haad 	char bytes[6];
   1063  1.1      haad 	char comp[6];
   1064  1.1      haad 	char uncomp[6];
   1065  1.1      haad 
   1066  1.1      haad 	if (dump_opt['d'] < 3)
   1067  1.1      haad 		return;
   1068  1.1      haad 
   1069  1.2  christos 	bplist_init(&bpl);
   1070  1.1      haad 	VERIFY(0 == bplist_open(&bpl, mos, object));
   1071  1.1      haad 	if (bplist_empty(&bpl)) {
   1072  1.1      haad 		bplist_close(&bpl);
   1073  1.2  christos 		bplist_fini(&bpl);
   1074  1.1      haad 		return;
   1075  1.1      haad 	}
   1076  1.1      haad 
   1077  1.1      haad 	nicenum(bpl.bpl_phys->bpl_bytes, bytes);
   1078  1.1      haad 	if (bpl.bpl_dbuf->db_size == sizeof (bplist_phys_t)) {
   1079  1.1      haad 		nicenum(bpl.bpl_phys->bpl_comp, comp);
   1080  1.1      haad 		nicenum(bpl.bpl_phys->bpl_uncomp, uncomp);
   1081  1.1      haad 		(void) printf("\n    %s: %llu entries, %s (%s/%s comp)\n",
   1082  1.1      haad 		    name, (u_longlong_t)bpl.bpl_phys->bpl_entries,
   1083  1.1      haad 		    bytes, comp, uncomp);
   1084  1.1      haad 	} else {
   1085  1.1      haad 		(void) printf("\n    %s: %llu entries, %s\n",
   1086  1.1      haad 		    name, (u_longlong_t)bpl.bpl_phys->bpl_entries, bytes);
   1087  1.1      haad 	}
   1088  1.1      haad 
   1089  1.1      haad 	if (dump_opt['d'] < 5) {
   1090  1.1      haad 		bplist_close(&bpl);
   1091  1.2  christos 		bplist_fini(&bpl);
   1092  1.1      haad 		return;
   1093  1.1      haad 	}
   1094  1.1      haad 
   1095  1.1      haad 	(void) printf("\n");
   1096  1.1      haad 
   1097  1.1      haad 	while (bplist_iterate(&bpl, &itor, bp) == 0) {
   1098  1.1      haad 		char blkbuf[BP_SPRINTF_LEN];
   1099  1.1      haad 
   1100  1.1      haad 		ASSERT(bp->blk_birth != 0);
   1101  1.2  christos 		snprintf_blkptr_compact(blkbuf, sizeof(blkbuf), bp);
   1102  1.1      haad 		(void) printf("\tItem %3llu: %s\n",
   1103  1.1      haad 		    (u_longlong_t)itor - 1, blkbuf);
   1104  1.1      haad 	}
   1105  1.1      haad 
   1106  1.1      haad 	bplist_close(&bpl);
   1107  1.2  christos 	bplist_fini(&bpl);
   1108  1.1      haad }
   1109  1.1      haad 
   1110  1.1      haad static avl_tree_t idx_tree;
   1111  1.1      haad static avl_tree_t domain_tree;
   1112  1.1      haad static boolean_t fuid_table_loaded;
   1113  1.1      haad 
   1114  1.1      haad static void
   1115  1.1      haad fuid_table_destroy()
   1116  1.1      haad {
   1117  1.1      haad 	if (fuid_table_loaded) {
   1118  1.1      haad 		zfs_fuid_table_destroy(&idx_tree, &domain_tree);
   1119  1.1      haad 		fuid_table_loaded = B_FALSE;
   1120  1.1      haad 	}
   1121  1.1      haad }
   1122  1.1      haad 
   1123  1.1      haad /*
   1124  1.1      haad  * print uid or gid information.
   1125  1.1      haad  * For normal POSIX id just the id is printed in decimal format.
   1126  1.1      haad  * For CIFS files with FUID the fuid is printed in hex followed by
   1127  1.1      haad  * the doman-rid string.
   1128  1.1      haad  */
   1129  1.1      haad static void
   1130  1.1      haad print_idstr(uint64_t id, const char *id_type)
   1131  1.1      haad {
   1132  1.1      haad 	if (FUID_INDEX(id)) {
   1133  1.1      haad 		char *domain;
   1134  1.1      haad 
   1135  1.1      haad 		domain = zfs_fuid_idx_domain(&idx_tree, FUID_INDEX(id));
   1136  1.1      haad 		(void) printf("\t%s     %llx [%s-%d]\n", id_type,
   1137  1.1      haad 		    (u_longlong_t)id, domain, (int)FUID_RID(id));
   1138  1.1      haad 	} else {
   1139  1.1      haad 		(void) printf("\t%s     %llu\n", id_type, (u_longlong_t)id);
   1140  1.1      haad 	}
   1141  1.1      haad 
   1142  1.1      haad }
   1143  1.1      haad 
   1144  1.1      haad static void
   1145  1.1      haad dump_uidgid(objset_t *os, znode_phys_t *zp)
   1146  1.1      haad {
   1147  1.1      haad 	uint32_t uid_idx, gid_idx;
   1148  1.1      haad 
   1149  1.1      haad 	uid_idx = FUID_INDEX(zp->zp_uid);
   1150  1.1      haad 	gid_idx = FUID_INDEX(zp->zp_gid);
   1151  1.1      haad 
   1152  1.1      haad 	/* Load domain table, if not already loaded */
   1153  1.1      haad 	if (!fuid_table_loaded && (uid_idx || gid_idx)) {
   1154  1.1      haad 		uint64_t fuid_obj;
   1155  1.1      haad 
   1156  1.1      haad 		/* first find the fuid object.  It lives in the master node */
   1157  1.1      haad 		VERIFY(zap_lookup(os, MASTER_NODE_OBJ, ZFS_FUID_TABLES,
   1158  1.1      haad 		    8, 1, &fuid_obj) == 0);
   1159  1.2  christos 		zfs_fuid_avl_tree_create(&idx_tree, &domain_tree);
   1160  1.1      haad 		(void) zfs_fuid_table_load(os, fuid_obj,
   1161  1.1      haad 		    &idx_tree, &domain_tree);
   1162  1.1      haad 		fuid_table_loaded = B_TRUE;
   1163  1.1      haad 	}
   1164  1.1      haad 
   1165  1.1      haad 	print_idstr(zp->zp_uid, "uid");
   1166  1.1      haad 	print_idstr(zp->zp_gid, "gid");
   1167  1.1      haad }
   1168  1.1      haad 
   1169  1.1      haad /*ARGSUSED*/
   1170  1.1      haad static void
   1171  1.1      haad dump_znode(objset_t *os, uint64_t object, void *data, size_t size)
   1172  1.1      haad {
   1173  1.1      haad 	znode_phys_t *zp = data;
   1174  1.1      haad 	time_t z_crtime, z_atime, z_mtime, z_ctime;
   1175  1.1      haad 	char path[MAXPATHLEN * 2];	/* allow for xattr and failure prefix */
   1176  1.1      haad 	int error;
   1177  1.1      haad 
   1178  1.1      haad 	ASSERT(size >= sizeof (znode_phys_t));
   1179  1.1      haad 
   1180  1.1      haad 	error = zfs_obj_to_path(os, object, path, sizeof (path));
   1181  1.1      haad 	if (error != 0) {
   1182  1.1      haad 		(void) snprintf(path, sizeof (path), "\?\?\?<object#%llu>",
   1183  1.1      haad 		    (u_longlong_t)object);
   1184  1.1      haad 	}
   1185  1.1      haad 
   1186  1.1      haad 	if (dump_opt['d'] < 3) {
   1187  1.1      haad 		(void) printf("\t%s\n", path);
   1188  1.1      haad 		return;
   1189  1.1      haad 	}
   1190  1.1      haad 
   1191  1.1      haad 	z_crtime = (time_t)zp->zp_crtime[0];
   1192  1.1      haad 	z_atime = (time_t)zp->zp_atime[0];
   1193  1.1      haad 	z_mtime = (time_t)zp->zp_mtime[0];
   1194  1.1      haad 	z_ctime = (time_t)zp->zp_ctime[0];
   1195  1.1      haad 
   1196  1.1      haad 	(void) printf("\tpath	%s\n", path);
   1197  1.1      haad 	dump_uidgid(os, zp);
   1198  1.1      haad 	(void) printf("\tatime	%s", ctime(&z_atime));
   1199  1.1      haad 	(void) printf("\tmtime	%s", ctime(&z_mtime));
   1200  1.1      haad 	(void) printf("\tctime	%s", ctime(&z_ctime));
   1201  1.1      haad 	(void) printf("\tcrtime	%s", ctime(&z_crtime));
   1202  1.1      haad 	(void) printf("\tgen	%llu\n", (u_longlong_t)zp->zp_gen);
   1203  1.1      haad 	(void) printf("\tmode	%llo\n", (u_longlong_t)zp->zp_mode);
   1204  1.1      haad 	(void) printf("\tsize	%llu\n", (u_longlong_t)zp->zp_size);
   1205  1.1      haad 	(void) printf("\tparent	%llu\n", (u_longlong_t)zp->zp_parent);
   1206  1.1      haad 	(void) printf("\tlinks	%llu\n", (u_longlong_t)zp->zp_links);
   1207  1.1      haad 	(void) printf("\txattr	%llu\n", (u_longlong_t)zp->zp_xattr);
   1208  1.1      haad 	(void) printf("\trdev	0x%016llx\n", (u_longlong_t)zp->zp_rdev);
   1209  1.1      haad }
   1210  1.1      haad 
   1211  1.1      haad /*ARGSUSED*/
   1212  1.1      haad static void
   1213  1.1      haad dump_acl(objset_t *os, uint64_t object, void *data, size_t size)
   1214  1.1      haad {
   1215  1.1      haad }
   1216  1.1      haad 
   1217  1.1      haad /*ARGSUSED*/
   1218  1.1      haad static void
   1219  1.1      haad dump_dmu_objset(objset_t *os, uint64_t object, void *data, size_t size)
   1220  1.1      haad {
   1221  1.1      haad }
   1222  1.1      haad 
   1223  1.2  christos static object_viewer_t *object_viewer[DMU_OT_NUMTYPES + 1] = {
   1224  1.1      haad 	dump_none,		/* unallocated			*/
   1225  1.1      haad 	dump_zap,		/* object directory		*/
   1226  1.1      haad 	dump_uint64,		/* object array			*/
   1227  1.1      haad 	dump_none,		/* packed nvlist		*/
   1228  1.1      haad 	dump_packed_nvlist,	/* packed nvlist size		*/
   1229  1.1      haad 	dump_none,		/* bplist			*/
   1230  1.1      haad 	dump_none,		/* bplist header		*/
   1231  1.1      haad 	dump_none,		/* SPA space map header		*/
   1232  1.1      haad 	dump_none,		/* SPA space map		*/
   1233  1.1      haad 	dump_none,		/* ZIL intent log		*/
   1234  1.1      haad 	dump_dnode,		/* DMU dnode			*/
   1235  1.1      haad 	dump_dmu_objset,	/* DMU objset			*/
   1236  1.1      haad 	dump_dsl_dir,		/* DSL directory		*/
   1237  1.1      haad 	dump_zap,		/* DSL directory child map	*/
   1238  1.1      haad 	dump_zap,		/* DSL dataset snap map		*/
   1239  1.1      haad 	dump_zap,		/* DSL props			*/
   1240  1.1      haad 	dump_dsl_dataset,	/* DSL dataset			*/
   1241  1.1      haad 	dump_znode,		/* ZFS znode			*/
   1242  1.1      haad 	dump_acl,		/* ZFS V0 ACL			*/
   1243  1.1      haad 	dump_uint8,		/* ZFS plain file		*/
   1244  1.1      haad 	dump_zpldir,		/* ZFS directory		*/
   1245  1.1      haad 	dump_zap,		/* ZFS master node		*/
   1246  1.1      haad 	dump_zap,		/* ZFS delete queue		*/
   1247  1.1      haad 	dump_uint8,		/* zvol object			*/
   1248  1.1      haad 	dump_zap,		/* zvol prop			*/
   1249  1.1      haad 	dump_uint8,		/* other uint8[]		*/
   1250  1.1      haad 	dump_uint64,		/* other uint64[]		*/
   1251  1.1      haad 	dump_zap,		/* other ZAP			*/
   1252  1.1      haad 	dump_zap,		/* persistent error log		*/
   1253  1.1      haad 	dump_uint8,		/* SPA history			*/
   1254  1.1      haad 	dump_uint64,		/* SPA history offsets		*/
   1255  1.1      haad 	dump_zap,		/* Pool properties		*/
   1256  1.1      haad 	dump_zap,		/* DSL permissions		*/
   1257  1.1      haad 	dump_acl,		/* ZFS ACL			*/
   1258  1.1      haad 	dump_uint8,		/* ZFS SYSACL			*/
   1259  1.1      haad 	dump_none,		/* FUID nvlist			*/
   1260  1.1      haad 	dump_packed_nvlist,	/* FUID nvlist size		*/
   1261  1.1      haad 	dump_zap,		/* DSL dataset next clones	*/
   1262  1.1      haad 	dump_zap,		/* DSL scrub queue		*/
   1263  1.2  christos 	dump_zap,		/* ZFS user/group used		*/
   1264  1.2  christos 	dump_zap,		/* ZFS user/group quota		*/
   1265  1.2  christos 	dump_zap,		/* snapshot refcount tags	*/
   1266  1.2  christos 	dump_ddt_zap,		/* DDT ZAP object		*/
   1267  1.2  christos 	dump_zap,		/* DDT statistics		*/
   1268  1.2  christos 	dump_unknown		/* Unknown type, must be last	*/
   1269  1.1      haad };
   1270  1.1      haad 
   1271  1.1      haad static void
   1272  1.1      haad dump_object(objset_t *os, uint64_t object, int verbosity, int *print_header)
   1273  1.1      haad {
   1274  1.1      haad 	dmu_buf_t *db = NULL;
   1275  1.1      haad 	dmu_object_info_t doi;
   1276  1.1      haad 	dnode_t *dn;
   1277  1.1      haad 	void *bonus = NULL;
   1278  1.1      haad 	size_t bsize = 0;
   1279  1.2  christos 	char iblk[6], dblk[6], lsize[6], asize[6], bonus_size[6], fill[7];
   1280  1.1      haad 	char aux[50];
   1281  1.1      haad 	int error;
   1282  1.1      haad 
   1283  1.1      haad 	if (*print_header) {
   1284  1.2  christos 		(void) printf("\n%10s  %3s  %5s  %5s  %5s  %5s  %6s  %s\n",
   1285  1.2  christos 		    "Object", "lvl", "iblk", "dblk", "dsize", "lsize",
   1286  1.2  christos 		    "%full", "type");
   1287  1.1      haad 		*print_header = 0;
   1288  1.1      haad 	}
   1289  1.1      haad 
   1290  1.1      haad 	if (object == 0) {
   1291  1.2  christos 		dn = os->os_meta_dnode;
   1292  1.1      haad 	} else {
   1293  1.1      haad 		error = dmu_bonus_hold(os, object, FTAG, &db);
   1294  1.1      haad 		if (error)
   1295  1.1      haad 			fatal("dmu_bonus_hold(%llu) failed, errno %u",
   1296  1.1      haad 			    object, error);
   1297  1.1      haad 		bonus = db->db_data;
   1298  1.1      haad 		bsize = db->db_size;
   1299  1.1      haad 		dn = ((dmu_buf_impl_t *)db)->db_dnode;
   1300  1.1      haad 	}
   1301  1.1      haad 	dmu_object_info_from_dnode(dn, &doi);
   1302  1.1      haad 
   1303  1.1      haad 	nicenum(doi.doi_metadata_block_size, iblk);
   1304  1.1      haad 	nicenum(doi.doi_data_block_size, dblk);
   1305  1.2  christos 	nicenum(doi.doi_max_offset, lsize);
   1306  1.2  christos 	nicenum(doi.doi_physical_blocks_512 << 9, asize);
   1307  1.1      haad 	nicenum(doi.doi_bonus_size, bonus_size);
   1308  1.2  christos 	(void) snprintf(fill, "%6.2f", 100.0 * doi.doi_fill_count *
   1309  1.2  christos 	    doi.doi_data_block_size / (object == 0 ? DNODES_PER_BLOCK : 1) /
   1310  1.2  christos 	    doi.doi_max_offset);
   1311  1.1      haad 
   1312  1.1      haad 	aux[0] = '\0';
   1313  1.1      haad 
   1314  1.1      haad 	if (doi.doi_checksum != ZIO_CHECKSUM_INHERIT || verbosity >= 6) {
   1315  1.1      haad 		(void) snprintf(aux + strlen(aux), sizeof (aux), " (K=%s)",
   1316  1.2  christos 		    ZDB_CHECKSUM_NAME(doi.doi_checksum));
   1317  1.1      haad 	}
   1318  1.1      haad 
   1319  1.1      haad 	if (doi.doi_compress != ZIO_COMPRESS_INHERIT || verbosity >= 6) {
   1320  1.1      haad 		(void) snprintf(aux + strlen(aux), sizeof (aux), " (Z=%s)",
   1321  1.2  christos 		    ZDB_COMPRESS_NAME(doi.doi_compress));
   1322  1.1      haad 	}
   1323  1.1      haad 
   1324  1.2  christos 	(void) printf("%10lld  %3u  %5s  %5s  %5s  %5s  %6s  %s%s\n",
   1325  1.2  christos 	    (u_longlong_t)object, doi.doi_indirection, iblk, dblk,
   1326  1.2  christos 	    asize, lsize, fill, ZDB_OT_NAME(doi.doi_type), aux);
   1327  1.1      haad 
   1328  1.1      haad 	if (doi.doi_bonus_type != DMU_OT_NONE && verbosity > 3) {
   1329  1.2  christos 		(void) printf("%10s  %3s  %5s  %5s  %5s  %5s  %6s  %s\n",
   1330  1.2  christos 		    "", "", "", "", "", bonus_size, "bonus",
   1331  1.2  christos 		    ZDB_OT_NAME(doi.doi_bonus_type));
   1332  1.1      haad 	}
   1333  1.1      haad 
   1334  1.1      haad 	if (verbosity >= 4) {
   1335  1.2  christos 		(void) printf("\tdnode flags: %s%s\n",
   1336  1.2  christos 		    (dn->dn_phys->dn_flags & DNODE_FLAG_USED_BYTES) ?
   1337  1.2  christos 		    "USED_BYTES " : "",
   1338  1.2  christos 		    (dn->dn_phys->dn_flags & DNODE_FLAG_USERUSED_ACCOUNTED) ?
   1339  1.2  christos 		    "USERUSED_ACCOUNTED " : "");
   1340  1.2  christos 		(void) printf("\tdnode maxblkid: %llu\n",
   1341  1.2  christos 		    (longlong_t)dn->dn_phys->dn_maxblkid);
   1342  1.2  christos 
   1343  1.2  christos 		object_viewer[ZDB_OT_TYPE(doi.doi_bonus_type)](os, object,
   1344  1.2  christos 		    bonus, bsize);
   1345  1.2  christos 		object_viewer[ZDB_OT_TYPE(doi.doi_type)](os, object, NULL, 0);
   1346  1.1      haad 		*print_header = 1;
   1347  1.1      haad 	}
   1348  1.1      haad 
   1349  1.1      haad 	if (verbosity >= 5)
   1350  1.1      haad 		dump_indirect(dn);
   1351  1.1      haad 
   1352  1.1      haad 	if (verbosity >= 5) {
   1353  1.1      haad 		/*
   1354  1.1      haad 		 * Report the list of segments that comprise the object.
   1355  1.1      haad 		 */
   1356  1.1      haad 		uint64_t start = 0;
   1357  1.1      haad 		uint64_t end;
   1358  1.1      haad 		uint64_t blkfill = 1;
   1359  1.1      haad 		int minlvl = 1;
   1360  1.1      haad 
   1361  1.1      haad 		if (dn->dn_type == DMU_OT_DNODE) {
   1362  1.1      haad 			minlvl = 0;
   1363  1.1      haad 			blkfill = DNODES_PER_BLOCK;
   1364  1.1      haad 		}
   1365  1.1      haad 
   1366  1.1      haad 		for (;;) {
   1367  1.2  christos 			char segsize[6];
   1368  1.1      haad 			error = dnode_next_offset(dn,
   1369  1.1      haad 			    0, &start, minlvl, blkfill, 0);
   1370  1.1      haad 			if (error)
   1371  1.1      haad 				break;
   1372  1.1      haad 			end = start;
   1373  1.1      haad 			error = dnode_next_offset(dn,
   1374  1.1      haad 			    DNODE_FIND_HOLE, &end, minlvl, blkfill, 0);
   1375  1.1      haad 			nicenum(end - start, segsize);
   1376  1.1      haad 			(void) printf("\t\tsegment [%016llx, %016llx)"
   1377  1.1      haad 			    " size %5s\n", (u_longlong_t)start,
   1378  1.1      haad 			    (u_longlong_t)end, segsize);
   1379  1.1      haad 			if (error)
   1380  1.1      haad 				break;
   1381  1.1      haad 			start = end;
   1382  1.1      haad 		}
   1383  1.1      haad 	}
   1384  1.1      haad 
   1385  1.1      haad 	if (db != NULL)
   1386  1.1      haad 		dmu_buf_rele(db, FTAG);
   1387  1.1      haad }
   1388  1.1      haad 
   1389  1.1      haad static char *objset_types[DMU_OST_NUMTYPES] = {
   1390  1.1      haad 	"NONE", "META", "ZPL", "ZVOL", "OTHER", "ANY" };
   1391  1.1      haad 
   1392  1.1      haad static void
   1393  1.1      haad dump_dir(objset_t *os)
   1394  1.1      haad {
   1395  1.1      haad 	dmu_objset_stats_t dds;
   1396  1.1      haad 	uint64_t object, object_count;
   1397  1.1      haad 	uint64_t refdbytes, usedobjs, scratch;
   1398  1.1      haad 	char numbuf[8];
   1399  1.2  christos 	char blkbuf[BP_SPRINTF_LEN + 20];
   1400  1.1      haad 	char osname[MAXNAMELEN];
   1401  1.1      haad 	char *type = "UNKNOWN";
   1402  1.1      haad 	int verbosity = dump_opt['d'];
   1403  1.1      haad 	int print_header = 1;
   1404  1.1      haad 	int i, error;
   1405  1.2  christos 	size_t len;
   1406  1.1      haad 
   1407  1.1      haad 	dmu_objset_fast_stat(os, &dds);
   1408  1.1      haad 
   1409  1.1      haad 	if (dds.dds_type < DMU_OST_NUMTYPES)
   1410  1.1      haad 		type = objset_types[dds.dds_type];
   1411  1.1      haad 
   1412  1.1      haad 	if (dds.dds_type == DMU_OST_META) {
   1413  1.1      haad 		dds.dds_creation_txg = TXG_INITIAL;
   1414  1.2  christos 		usedobjs = os->os_rootbp->blk_fill;
   1415  1.2  christos 		refdbytes = os->os_spa->spa_dsl_pool->
   1416  1.1      haad 		    dp_mos_dir->dd_phys->dd_used_bytes;
   1417  1.1      haad 	} else {
   1418  1.1      haad 		dmu_objset_space(os, &refdbytes, &scratch, &usedobjs, &scratch);
   1419  1.1      haad 	}
   1420  1.1      haad 
   1421  1.2  christos 	ASSERT3U(usedobjs, ==, os->os_rootbp->blk_fill);
   1422  1.1      haad 
   1423  1.1      haad 	nicenum(refdbytes, numbuf);
   1424  1.1      haad 
   1425  1.1      haad 	if (verbosity >= 4) {
   1426  1.2  christos 		len = snprintf(blkbuf, blklen, ", rootbp ");
   1427  1.2  christos 		if (len > blklen)
   1428  1.2  christos 			len = blklen;
   1429  1.2  christos 		printf_blkptr(blkbuf + len, blklen - len, os->os_rootbp);
   1430  1.1      haad 	} else {
   1431  1.1      haad 		blkbuf[0] = '\0';
   1432  1.1      haad 	}
   1433  1.1      haad 
   1434  1.1      haad 	dmu_objset_name(os, osname);
   1435  1.1      haad 
   1436  1.1      haad 	(void) printf("Dataset %s [%s], ID %llu, cr_txg %llu, "
   1437  1.1      haad 	    "%s, %llu objects%s\n",
   1438  1.1      haad 	    osname, type, (u_longlong_t)dmu_objset_id(os),
   1439  1.1      haad 	    (u_longlong_t)dds.dds_creation_txg,
   1440  1.1      haad 	    numbuf, (u_longlong_t)usedobjs, blkbuf);
   1441  1.1      haad 
   1442  1.2  christos 	if (zopt_objects != 0) {
   1443  1.2  christos 		for (i = 0; i < zopt_objects; i++)
   1444  1.2  christos 			dump_object(os, zopt_object[i], verbosity,
   1445  1.2  christos 			    &print_header);
   1446  1.2  christos 		(void) printf("\n");
   1447  1.2  christos 		return;
   1448  1.2  christos 	}
   1449  1.2  christos 
   1450  1.2  christos 	if (dump_opt['i'] != 0 || verbosity >= 2)
   1451  1.2  christos 		dump_intent_log(dmu_objset_zil(os));
   1452  1.1      haad 
   1453  1.1      haad 	if (dmu_objset_ds(os) != NULL)
   1454  1.1      haad 		dump_bplist(dmu_objset_pool(os)->dp_meta_objset,
   1455  1.1      haad 		    dmu_objset_ds(os)->ds_phys->ds_deadlist_obj, "Deadlist");
   1456  1.1      haad 
   1457  1.1      haad 	if (verbosity < 2)
   1458  1.1      haad 		return;
   1459  1.1      haad 
   1460  1.2  christos 	if (os->os_rootbp->blk_birth == 0)
   1461  1.1      haad 		return;
   1462  1.1      haad 
   1463  1.2  christos 	dump_object(os, 0, verbosity, &print_header);
   1464  1.2  christos 	object_count = 0;
   1465  1.2  christos 	if (os->os_userused_dnode &&
   1466  1.2  christos 	    os->os_userused_dnode->dn_type != 0) {
   1467  1.2  christos 		dump_object(os, DMU_USERUSED_OBJECT, verbosity, &print_header);
   1468  1.2  christos 		dump_object(os, DMU_GROUPUSED_OBJECT, verbosity, &print_header);
   1469  1.1      haad 	}
   1470  1.1      haad 
   1471  1.1      haad 	object = 0;
   1472  1.1      haad 	while ((error = dmu_object_next(os, &object, B_FALSE, 0)) == 0) {
   1473  1.1      haad 		dump_object(os, object, verbosity, &print_header);
   1474  1.1      haad 		object_count++;
   1475  1.1      haad 	}
   1476  1.1      haad 
   1477  1.1      haad 	ASSERT3U(object_count, ==, usedobjs);
   1478  1.1      haad 
   1479  1.1      haad 	(void) printf("\n");
   1480  1.1      haad 
   1481  1.2  christos 	if (error != ESRCH) {
   1482  1.2  christos 		(void) fprintf(stderr, "dmu_object_next() = %d\n", error);
   1483  1.2  christos 		abort();
   1484  1.2  christos 	}
   1485  1.1      haad }
   1486  1.1      haad 
   1487  1.1      haad static void
   1488  1.2  christos dump_uberblock(uberblock_t *ub, const char *header, const char *footer)
   1489  1.1      haad {
   1490  1.1      haad 	time_t timestamp = ub->ub_timestamp;
   1491  1.1      haad 
   1492  1.2  christos 	(void) printf(header ? header : "");
   1493  1.1      haad 	(void) printf("\tmagic = %016llx\n", (u_longlong_t)ub->ub_magic);
   1494  1.1      haad 	(void) printf("\tversion = %llu\n", (u_longlong_t)ub->ub_version);
   1495  1.1      haad 	(void) printf("\ttxg = %llu\n", (u_longlong_t)ub->ub_txg);
   1496  1.1      haad 	(void) printf("\tguid_sum = %llu\n", (u_longlong_t)ub->ub_guid_sum);
   1497  1.1      haad 	(void) printf("\ttimestamp = %llu UTC = %s",
   1498  1.1      haad 	    (u_longlong_t)ub->ub_timestamp, asctime(localtime(&timestamp)));
   1499  1.1      haad 	if (dump_opt['u'] >= 3) {
   1500  1.1      haad 		char blkbuf[BP_SPRINTF_LEN];
   1501  1.2  christos 		snprintf_blkptr(blkbuf, sizeof(blkbuf), &ub->ub_rootbp);
   1502  1.1      haad 		(void) printf("\trootbp = %s\n", blkbuf);
   1503  1.1      haad 	}
   1504  1.2  christos 	(void) printf(footer ? footer : "");
   1505  1.1      haad }
   1506  1.1      haad 
   1507  1.1      haad static void
   1508  1.2  christos dump_config(spa_t *spa)
   1509  1.1      haad {
   1510  1.2  christos 	dmu_buf_t *db;
   1511  1.2  christos 	size_t nvsize = 0;
   1512  1.2  christos 	int error = 0;
   1513  1.2  christos 
   1514  1.2  christos 
   1515  1.2  christos 	error = dmu_bonus_hold(spa->spa_meta_objset,
   1516  1.2  christos 	    spa->spa_config_object, FTAG, &db);
   1517  1.2  christos 
   1518  1.2  christos 	if (error == 0) {
   1519  1.2  christos 		nvsize = *(uint64_t *)db->db_data;
   1520  1.2  christos 		dmu_buf_rele(db, FTAG);
   1521  1.1      haad 
   1522  1.2  christos 		(void) printf("\nMOS Configuration:\n");
   1523  1.2  christos 		dump_packed_nvlist(spa->spa_meta_objset,
   1524  1.2  christos 		    spa->spa_config_object, (void *)&nvsize, 1);
   1525  1.2  christos 	} else {
   1526  1.2  christos 		(void) fprintf(stderr, "dmu_bonus_hold(%llu) failed, errno %d",
   1527  1.2  christos 		    (u_longlong_t)spa->spa_config_object, error);
   1528  1.1      haad 	}
   1529  1.1      haad }
   1530  1.1      haad 
   1531  1.1      haad static void
   1532  1.1      haad dump_cachefile(const char *cachefile)
   1533  1.1      haad {
   1534  1.1      haad 	int fd;
   1535  1.1      haad 	struct stat64 statbuf;
   1536  1.1      haad 	char *buf;
   1537  1.1      haad 	nvlist_t *config;
   1538  1.1      haad 
   1539  1.1      haad 	if ((fd = open64(cachefile, O_RDONLY)) < 0) {
   1540  1.1      haad 		(void) printf("cannot open '%s': %s\n", cachefile,
   1541  1.1      haad 		    strerror(errno));
   1542  1.1      haad 		exit(1);
   1543  1.1      haad 	}
   1544  1.1      haad 
   1545  1.1      haad 	if (fstat64(fd, &statbuf) != 0) {
   1546  1.1      haad 		(void) printf("failed to stat '%s': %s\n", cachefile,
   1547  1.1      haad 		    strerror(errno));
   1548  1.1      haad 		exit(1);
   1549  1.1      haad 	}
   1550  1.1      haad 
   1551  1.1      haad 	if ((buf = malloc(statbuf.st_size)) == NULL) {
   1552  1.1      haad 		(void) fprintf(stderr, "failed to allocate %llu bytes\n",
   1553  1.1      haad 		    (u_longlong_t)statbuf.st_size);
   1554  1.1      haad 		exit(1);
   1555  1.1      haad 	}
   1556  1.1      haad 
   1557  1.1      haad 	if (read(fd, buf, statbuf.st_size) != statbuf.st_size) {
   1558  1.1      haad 		(void) fprintf(stderr, "failed to read %llu bytes\n",
   1559  1.1      haad 		    (u_longlong_t)statbuf.st_size);
   1560  1.1      haad 		exit(1);
   1561  1.1      haad 	}
   1562  1.1      haad 
   1563  1.1      haad 	(void) close(fd);
   1564  1.1      haad 
   1565  1.1      haad 	if (nvlist_unpack(buf, statbuf.st_size, &config, 0) != 0) {
   1566  1.1      haad 		(void) fprintf(stderr, "failed to unpack nvlist\n");
   1567  1.1      haad 		exit(1);
   1568  1.1      haad 	}
   1569  1.1      haad 
   1570  1.1      haad 	free(buf);
   1571  1.1      haad 
   1572  1.1      haad 	dump_nvlist(config, 0);
   1573  1.1      haad 
   1574  1.1      haad 	nvlist_free(config);
   1575  1.1      haad }
   1576  1.1      haad 
   1577  1.2  christos #define	ZDB_MAX_UB_HEADER_SIZE 32
   1578  1.2  christos 
   1579  1.2  christos static void
   1580  1.2  christos dump_label_uberblocks(vdev_label_t *lbl, uint64_t ashift)
   1581  1.2  christos {
   1582  1.2  christos 	vdev_t vd;
   1583  1.2  christos 	vdev_t *vdp = &vd;
   1584  1.2  christos 	char header[ZDB_MAX_UB_HEADER_SIZE];
   1585  1.2  christos 
   1586  1.2  christos 	vd.vdev_ashift = ashift;
   1587  1.2  christos 	vdp->vdev_top = vdp;
   1588  1.2  christos 
   1589  1.2  christos 	for (int i = 0; i < VDEV_UBERBLOCK_COUNT(vdp); i++) {
   1590  1.2  christos 		uint64_t uoff = VDEV_UBERBLOCK_OFFSET(vdp, i);
   1591  1.2  christos 		uberblock_t *ub = (void *)((char *)lbl + uoff);
   1592  1.2  christos 
   1593  1.2  christos 		if (uberblock_verify(ub))
   1594  1.2  christos 			continue;
   1595  1.2  christos 		(void) snprintf(header, ZDB_MAX_UB_HEADER_SIZE,
   1596  1.2  christos 		    "Uberblock[%d]\n", i);
   1597  1.2  christos 		dump_uberblock(ub, header, "");
   1598  1.2  christos 	}
   1599  1.2  christos }
   1600  1.2  christos 
   1601  1.1      haad static void
   1602  1.1      haad dump_label(const char *dev)
   1603  1.1      haad {
   1604  1.1      haad 	int fd;
   1605  1.1      haad 	vdev_label_t label;
   1606  1.1      haad 	char *buf = label.vl_vdev_phys.vp_nvlist;
   1607  1.1      haad 	size_t buflen = sizeof (label.vl_vdev_phys.vp_nvlist);
   1608  1.1      haad 	struct stat64 statbuf;
   1609  1.2  christos 	uint64_t psize, ashift;
   1610  1.1      haad 
   1611  1.1      haad 	if ((fd = open64(dev, O_RDONLY)) < 0) {
   1612  1.1      haad 		(void) printf("cannot open '%s': %s\n", dev, strerror(errno));
   1613  1.1      haad 		exit(1);
   1614  1.1      haad 	}
   1615  1.1      haad 
   1616  1.1      haad 	if (fstat64(fd, &statbuf) != 0) {
   1617  1.1      haad 		(void) printf("failed to stat '%s': %s\n", dev,
   1618  1.1      haad 		    strerror(errno));
   1619  1.1      haad 	}
   1620  1.1      haad 
   1621  1.1      haad 	psize = statbuf.st_size;
   1622  1.1      haad 	psize = P2ALIGN(psize, (uint64_t)sizeof (vdev_label_t));
   1623  1.1      haad 
   1624  1.2  christos 	for (int l = 0; l < VDEV_LABELS; l++) {
   1625  1.1      haad 		nvlist_t *config = NULL;
   1626  1.1      haad 
   1627  1.1      haad 		(void) printf("--------------------------------------------\n");
   1628  1.1      haad 		(void) printf("LABEL %d\n", l);
   1629  1.1      haad 		(void) printf("--------------------------------------------\n");
   1630  1.1      haad 
   1631  1.1      haad 		if (pread64(fd, &label, sizeof (label),
   1632  1.1      haad 		    vdev_label_offset(psize, l, 0)) != sizeof (label)) {
   1633  1.1      haad 			(void) printf("failed to read label %d\n", l);
   1634  1.1      haad 			continue;
   1635  1.1      haad 		}
   1636  1.1      haad 
   1637  1.1      haad 		if (nvlist_unpack(buf, buflen, &config, 0) != 0) {
   1638  1.1      haad 			(void) printf("failed to unpack label %d\n", l);
   1639  1.2  christos 			ashift = SPA_MINBLOCKSHIFT;
   1640  1.2  christos 		} else {
   1641  1.2  christos 			nvlist_t *vdev_tree = NULL;
   1642  1.2  christos 
   1643  1.2  christos 			dump_nvlist(config, 4);
   1644  1.2  christos 			if ((nvlist_lookup_nvlist(config,
   1645  1.2  christos 			    ZPOOL_CONFIG_VDEV_TREE, &vdev_tree) != 0) ||
   1646  1.2  christos 			    (nvlist_lookup_uint64(vdev_tree,
   1647  1.2  christos 			    ZPOOL_CONFIG_ASHIFT, &ashift) != 0))
   1648  1.2  christos 				ashift = SPA_MINBLOCKSHIFT;
   1649  1.2  christos 			nvlist_free(config);
   1650  1.1      haad 		}
   1651  1.2  christos 		if (dump_opt['u'])
   1652  1.2  christos 			dump_label_uberblocks(&label, ashift);
   1653  1.1      haad 	}
   1654  1.1      haad }
   1655  1.1      haad 
   1656  1.1      haad /*ARGSUSED*/
   1657  1.1      haad static int
   1658  1.2  christos dump_one_dir(const char *dsname, void *arg)
   1659  1.1      haad {
   1660  1.1      haad 	int error;
   1661  1.1      haad 	objset_t *os;
   1662  1.1      haad 
   1663  1.2  christos 	error = dmu_objset_own(dsname, DMU_OST_ANY, B_TRUE, FTAG, &os);
   1664  1.1      haad 	if (error) {
   1665  1.2  christos 		(void) printf("Could not open %s, error %d\n", dsname, error);
   1666  1.1      haad 		return (0);
   1667  1.1      haad 	}
   1668  1.1      haad 	dump_dir(os);
   1669  1.2  christos 	dmu_objset_disown(os, FTAG);
   1670  1.1      haad 	fuid_table_destroy();
   1671  1.1      haad 	return (0);
   1672  1.1      haad }
   1673  1.1      haad 
   1674  1.2  christos /*
   1675  1.2  christos  * Block statistics.
   1676  1.2  christos  */
   1677  1.2  christos typedef struct zdb_blkstats {
   1678  1.2  christos 	uint64_t	zb_asize;
   1679  1.2  christos 	uint64_t	zb_lsize;
   1680  1.2  christos 	uint64_t	zb_psize;
   1681  1.2  christos 	uint64_t	zb_count;
   1682  1.2  christos } zdb_blkstats_t;
   1683  1.1      haad 
   1684  1.2  christos /*
   1685  1.2  christos  * Extended object types to report deferred frees and dedup auto-ditto blocks.
   1686  1.2  christos  */
   1687  1.2  christos #define	ZDB_OT_DEFERRED	(DMU_OT_NUMTYPES + 0)
   1688  1.2  christos #define	ZDB_OT_DITTO	(DMU_OT_NUMTYPES + 1)
   1689  1.2  christos #define	ZDB_OT_TOTAL	(DMU_OT_NUMTYPES + 2)
   1690  1.2  christos 
   1691  1.2  christos static char *zdb_ot_extname[] = {
   1692  1.2  christos 	"deferred free",
   1693  1.2  christos 	"dedup ditto",
   1694  1.2  christos 	"Total",
   1695  1.2  christos };
   1696  1.1      haad 
   1697  1.2  christos #define	ZB_TOTAL	DN_MAX_LEVELS
   1698  1.1      haad 
   1699  1.2  christos typedef struct zdb_cb {
   1700  1.2  christos 	zdb_blkstats_t	zcb_type[ZB_TOTAL + 1][ZDB_OT_TOTAL + 1];
   1701  1.2  christos 	uint64_t	zcb_dedup_asize;
   1702  1.2  christos 	uint64_t	zcb_dedup_blocks;
   1703  1.2  christos 	uint64_t	zcb_errors[256];
   1704  1.2  christos 	int		zcb_readfails;
   1705  1.2  christos 	int		zcb_haderrors;
   1706  1.2  christos } zdb_cb_t;
   1707  1.1      haad 
   1708  1.1      haad static void
   1709  1.2  christos zdb_count_block(spa_t *spa, zilog_t *zilog, zdb_cb_t *zcb, const blkptr_t *bp,
   1710  1.2  christos     dmu_object_type_t type)
   1711  1.1      haad {
   1712  1.2  christos 	uint64_t refcnt = 0;
   1713  1.1      haad 
   1714  1.2  christos 	ASSERT(type < ZDB_OT_TOTAL);
   1715  1.1      haad 
   1716  1.2  christos 	if (zilog && zil_bp_tree_add(zilog, bp) != 0)
   1717  1.2  christos 		return;
   1718  1.1      haad 
   1719  1.2  christos 	for (int i = 0; i < 4; i++) {
   1720  1.2  christos 		int l = (i < 2) ? BP_GET_LEVEL(bp) : ZB_TOTAL;
   1721  1.2  christos 		int t = (i & 1) ? type : ZDB_OT_TOTAL;
   1722  1.2  christos 		zdb_blkstats_t *zb = &zcb->zcb_type[l][t];
   1723  1.1      haad 
   1724  1.1      haad 		zb->zb_asize += BP_GET_ASIZE(bp);
   1725  1.1      haad 		zb->zb_lsize += BP_GET_LSIZE(bp);
   1726  1.1      haad 		zb->zb_psize += BP_GET_PSIZE(bp);
   1727  1.1      haad 		zb->zb_count++;
   1728  1.1      haad 	}
   1729  1.1      haad 
   1730  1.2  christos 	if (dump_opt['L'])
   1731  1.2  christos 		return;
   1732  1.1      haad 
   1733  1.2  christos 	if (BP_GET_DEDUP(bp)) {
   1734  1.2  christos 		ddt_t *ddt;
   1735  1.2  christos 		ddt_entry_t *dde;
   1736  1.2  christos 
   1737  1.2  christos 		ddt = ddt_select(spa, bp);
   1738  1.2  christos 		ddt_enter(ddt);
   1739  1.2  christos 		dde = ddt_lookup(ddt, bp, B_FALSE);
   1740  1.1      haad 
   1741  1.2  christos 		if (dde == NULL) {
   1742  1.2  christos 			refcnt = 0;
   1743  1.2  christos 		} else {
   1744  1.2  christos 			ddt_phys_t *ddp = ddt_phys_select(dde, bp);
   1745  1.2  christos 			ddt_phys_decref(ddp);
   1746  1.2  christos 			refcnt = ddp->ddp_refcnt;
   1747  1.2  christos 			if (ddt_phys_total_refcnt(dde) == 0)
   1748  1.2  christos 				ddt_remove(ddt, dde);
   1749  1.2  christos 		}
   1750  1.2  christos 		ddt_exit(ddt);
   1751  1.1      haad 	}
   1752  1.1      haad 
   1753  1.2  christos 	VERIFY3U(zio_wait(zio_claim(NULL, spa,
   1754  1.2  christos 	    refcnt ? 0 : spa_first_txg(spa),
   1755  1.2  christos 	    bp, NULL, NULL, ZIO_FLAG_CANFAIL)), ==, 0);
   1756  1.1      haad }
   1757  1.1      haad 
   1758  1.1      haad static int
   1759  1.2  christos zdb_blkptr_cb(spa_t *spa, zilog_t *zilog, const blkptr_t *bp,
   1760  1.2  christos     const zbookmark_t *zb, const dnode_phys_t *dnp, void *arg)
   1761  1.1      haad {
   1762  1.1      haad 	zdb_cb_t *zcb = arg;
   1763  1.1      haad 	char blkbuf[BP_SPRINTF_LEN];
   1764  1.2  christos 	dmu_object_type_t type;
   1765  1.2  christos 	boolean_t is_metadata;
   1766  1.1      haad 
   1767  1.1      haad 	if (bp == NULL)
   1768  1.1      haad 		return (0);
   1769  1.1      haad 
   1770  1.2  christos 	type = BP_GET_TYPE(bp);
   1771  1.2  christos 
   1772  1.2  christos 	zdb_count_block(spa, zilog, zcb, bp, type);
   1773  1.1      haad 
   1774  1.2  christos 	is_metadata = (BP_GET_LEVEL(bp) != 0 || dmu_ot[type].ot_metadata);
   1775  1.2  christos 
   1776  1.2  christos 	if (dump_opt['c'] > 1 || (dump_opt['c'] && is_metadata)) {
   1777  1.2  christos 		int ioerr;
   1778  1.2  christos 		size_t size = BP_GET_PSIZE(bp);
   1779  1.2  christos 		void *data = malloc(size);
   1780  1.2  christos 		int flags = ZIO_FLAG_CANFAIL | ZIO_FLAG_SCRUB | ZIO_FLAG_RAW;
   1781  1.2  christos 
   1782  1.2  christos 		/* If it's an intent log block, failure is expected. */
   1783  1.2  christos 		if (zb->zb_level == ZB_ZIL_LEVEL)
   1784  1.2  christos 			flags |= ZIO_FLAG_SPECULATIVE;
   1785  1.1      haad 
   1786  1.1      haad 		ioerr = zio_wait(zio_read(NULL, spa, bp, data, size,
   1787  1.2  christos 		    NULL, NULL, ZIO_PRIORITY_ASYNC_READ, flags, zb));
   1788  1.2  christos 
   1789  1.1      haad 		free(data);
   1790  1.1      haad 
   1791  1.2  christos 		if (ioerr && !(flags & ZIO_FLAG_SPECULATIVE)) {
   1792  1.1      haad 			zcb->zcb_haderrors = 1;
   1793  1.1      haad 			zcb->zcb_errors[ioerr]++;
   1794  1.1      haad 
   1795  1.1      haad 			if (dump_opt['b'] >= 2)
   1796  1.2  christos 				snprintf_blkptr(blkbuf, sizeof(blkbuf), bp);
   1797  1.1      haad 			else
   1798  1.1      haad 				blkbuf[0] = '\0';
   1799  1.1      haad 
   1800  1.2  christos 			(void) printf("zdb_blkptr_cb: "
   1801  1.2  christos 			    "Got error %d reading "
   1802  1.2  christos 			    "<%llu, %llu, %lld, %llx> %s -- skipping\n",
   1803  1.2  christos 			    ioerr,
   1804  1.2  christos 			    (u_longlong_t)zb->zb_objset,
   1805  1.2  christos 			    (u_longlong_t)zb->zb_object,
   1806  1.2  christos 			    (u_longlong_t)zb->zb_level,
   1807  1.2  christos 			    (u_longlong_t)zb->zb_blkid,
   1808  1.2  christos 			    blkbuf);
   1809  1.1      haad 		}
   1810  1.1      haad 	}
   1811  1.1      haad 
   1812  1.1      haad 	zcb->zcb_readfails = 0;
   1813  1.1      haad 
   1814  1.1      haad 	if (dump_opt['b'] >= 4) {
   1815  1.2  christos 		snprintf_blkptr(blkbuf, sizeof(blkbuf), bp);
   1816  1.2  christos 		(void) printf("objset %llu object %llu "
   1817  1.2  christos 		    "level %lld offset 0x%llx %s\n",
   1818  1.1      haad 		    (u_longlong_t)zb->zb_objset,
   1819  1.1      haad 		    (u_longlong_t)zb->zb_object,
   1820  1.2  christos 		    (longlong_t)zb->zb_level,
   1821  1.2  christos 		    (u_longlong_t)blkid2offset(dnp, bp, zb),
   1822  1.1      haad 		    blkbuf);
   1823  1.1      haad 	}
   1824  1.1      haad 
   1825  1.1      haad 	return (0);
   1826  1.1      haad }
   1827  1.1      haad 
   1828  1.2  christos static void
   1829  1.2  christos zdb_leak(space_map_t *sm, uint64_t start, uint64_t size)
   1830  1.2  christos {
   1831  1.2  christos 	vdev_t *vd = sm->sm_ppd;
   1832  1.2  christos 
   1833  1.2  christos 	(void) printf("leaked space: vdev %llu, offset 0x%llx, size %llu\n",
   1834  1.2  christos 	    (u_longlong_t)vd->vdev_id, (u_longlong_t)start, (u_longlong_t)size);
   1835  1.2  christos }
   1836  1.2  christos 
   1837  1.2  christos /* ARGSUSED */
   1838  1.2  christos static void
   1839  1.2  christos zdb_space_map_load(space_map_t *sm)
   1840  1.2  christos {
   1841  1.2  christos }
   1842  1.2  christos 
   1843  1.2  christos static void
   1844  1.2  christos zdb_space_map_unload(space_map_t *sm)
   1845  1.2  christos {
   1846  1.2  christos 	space_map_vacate(sm, zdb_leak, sm);
   1847  1.2  christos }
   1848  1.2  christos 
   1849  1.2  christos /* ARGSUSED */
   1850  1.2  christos static void
   1851  1.2  christos zdb_space_map_claim(space_map_t *sm, uint64_t start, uint64_t size)
   1852  1.2  christos {
   1853  1.2  christos }
   1854  1.2  christos 
   1855  1.2  christos static space_map_ops_t zdb_space_map_ops = {
   1856  1.2  christos 	zdb_space_map_load,
   1857  1.2  christos 	zdb_space_map_unload,
   1858  1.2  christos 	NULL,	/* alloc */
   1859  1.2  christos 	zdb_space_map_claim,
   1860  1.2  christos 	NULL,	/* free */
   1861  1.2  christos 	NULL	/* maxsize */
   1862  1.2  christos };
   1863  1.2  christos 
   1864  1.2  christos static void
   1865  1.2  christos zdb_ddt_leak_init(spa_t *spa, zdb_cb_t *zcb)
   1866  1.2  christos {
   1867  1.2  christos 	ddt_bookmark_t ddb = { 0 };
   1868  1.2  christos 	ddt_entry_t dde;
   1869  1.2  christos 	int error;
   1870  1.2  christos 
   1871  1.2  christos 	while ((error = ddt_walk(spa, &ddb, &dde)) == 0) {
   1872  1.2  christos 		blkptr_t blk;
   1873  1.2  christos 		ddt_phys_t *ddp = dde.dde_phys;
   1874  1.2  christos 
   1875  1.2  christos 		if (ddb.ddb_class == DDT_CLASS_UNIQUE)
   1876  1.2  christos 			return;
   1877  1.2  christos 
   1878  1.2  christos 		ASSERT(ddt_phys_total_refcnt(&dde) > 1);
   1879  1.2  christos 
   1880  1.2  christos 		for (int p = 0; p < DDT_PHYS_TYPES; p++, ddp++) {
   1881  1.2  christos 			if (ddp->ddp_phys_birth == 0)
   1882  1.2  christos 				continue;
   1883  1.2  christos 			ddt_bp_create(ddb.ddb_checksum,
   1884  1.2  christos 			    &dde.dde_key, ddp, &blk);
   1885  1.2  christos 			if (p == DDT_PHYS_DITTO) {
   1886  1.2  christos 				zdb_count_block(spa, NULL, zcb, &blk,
   1887  1.2  christos 				    ZDB_OT_DITTO);
   1888  1.2  christos 			} else {
   1889  1.2  christos 				zcb->zcb_dedup_asize +=
   1890  1.2  christos 				    BP_GET_ASIZE(&blk) * (ddp->ddp_refcnt - 1);
   1891  1.2  christos 				zcb->zcb_dedup_blocks++;
   1892  1.2  christos 			}
   1893  1.2  christos 		}
   1894  1.2  christos 		if (!dump_opt['L']) {
   1895  1.2  christos 			ddt_t *ddt = spa->spa_ddt[ddb.ddb_checksum];
   1896  1.2  christos 			ddt_enter(ddt);
   1897  1.2  christos 			VERIFY(ddt_lookup(ddt, &blk, B_TRUE) != NULL);
   1898  1.2  christos 			ddt_exit(ddt);
   1899  1.2  christos 		}
   1900  1.2  christos 	}
   1901  1.2  christos 
   1902  1.2  christos 	ASSERT(error == ENOENT);
   1903  1.2  christos }
   1904  1.2  christos 
   1905  1.2  christos static void
   1906  1.2  christos zdb_leak_init(spa_t *spa, zdb_cb_t *zcb)
   1907  1.2  christos {
   1908  1.2  christos 	if (!dump_opt['L']) {
   1909  1.2  christos 		vdev_t *rvd = spa->spa_root_vdev;
   1910  1.2  christos 		for (int c = 0; c < rvd->vdev_children; c++) {
   1911  1.2  christos 			vdev_t *vd = rvd->vdev_child[c];
   1912  1.2  christos 			for (int m = 0; m < vd->vdev_ms_count; m++) {
   1913  1.2  christos 				metaslab_t *msp = vd->vdev_ms[m];
   1914  1.2  christos 				mutex_enter(&msp->ms_lock);
   1915  1.2  christos 				space_map_unload(&msp->ms_map);
   1916  1.2  christos 				VERIFY(space_map_load(&msp->ms_map,
   1917  1.2  christos 				    &zdb_space_map_ops, SM_ALLOC, &msp->ms_smo,
   1918  1.2  christos 				    spa->spa_meta_objset) == 0);
   1919  1.2  christos 				msp->ms_map.sm_ppd = vd;
   1920  1.2  christos 				mutex_exit(&msp->ms_lock);
   1921  1.2  christos 			}
   1922  1.2  christos 		}
   1923  1.2  christos 	}
   1924  1.2  christos 
   1925  1.2  christos 	spa_config_enter(spa, SCL_CONFIG, FTAG, RW_READER);
   1926  1.2  christos 
   1927  1.2  christos 	zdb_ddt_leak_init(spa, zcb);
   1928  1.2  christos 
   1929  1.2  christos 	spa_config_exit(spa, SCL_CONFIG, FTAG);
   1930  1.2  christos }
   1931  1.2  christos 
   1932  1.2  christos static void
   1933  1.2  christos zdb_leak_fini(spa_t *spa)
   1934  1.2  christos {
   1935  1.2  christos 	if (!dump_opt['L']) {
   1936  1.2  christos 		vdev_t *rvd = spa->spa_root_vdev;
   1937  1.2  christos 		for (int c = 0; c < rvd->vdev_children; c++) {
   1938  1.2  christos 			vdev_t *vd = rvd->vdev_child[c];
   1939  1.2  christos 			for (int m = 0; m < vd->vdev_ms_count; m++) {
   1940  1.2  christos 				metaslab_t *msp = vd->vdev_ms[m];
   1941  1.2  christos 				mutex_enter(&msp->ms_lock);
   1942  1.2  christos 				space_map_unload(&msp->ms_map);
   1943  1.2  christos 				mutex_exit(&msp->ms_lock);
   1944  1.2  christos 			}
   1945  1.2  christos 		}
   1946  1.2  christos 	}
   1947  1.2  christos }
   1948  1.2  christos 
   1949  1.1      haad static int
   1950  1.1      haad dump_block_stats(spa_t *spa)
   1951  1.1      haad {
   1952  1.1      haad 	zdb_cb_t zcb = { 0 };
   1953  1.1      haad 	zdb_blkstats_t *zb, *tzb;
   1954  1.2  christos 	uint64_t norm_alloc, norm_space, total_alloc, total_found;
   1955  1.2  christos 	int flags = TRAVERSE_PRE | TRAVERSE_PREFETCH_METADATA | TRAVERSE_HARD;
   1956  1.1      haad 	int leaks = 0;
   1957  1.1      haad 
   1958  1.2  christos 	(void) printf("\nTraversing all blocks %s%s%s%s%s...\n",
   1959  1.2  christos 	    (dump_opt['c'] || !dump_opt['L']) ? "to verify " : "",
   1960  1.2  christos 	    (dump_opt['c'] == 1) ? "metadata " : "",
   1961  1.2  christos 	    dump_opt['c'] ? "checksums " : "",
   1962  1.2  christos 	    (dump_opt['c'] && !dump_opt['L']) ? "and verify " : "",
   1963  1.2  christos 	    !dump_opt['L'] ? "nothing leaked " : "");
   1964  1.1      haad 
   1965  1.1      haad 	/*
   1966  1.1      haad 	 * Load all space maps as SM_ALLOC maps, then traverse the pool
   1967  1.1      haad 	 * claiming each block we discover.  If the pool is perfectly
   1968  1.1      haad 	 * consistent, the space maps will be empty when we're done.
   1969  1.1      haad 	 * Anything left over is a leak; any block we can't claim (because
   1970  1.1      haad 	 * it's not part of any space map) is a double allocation,
   1971  1.1      haad 	 * reference to a freed block, or an unclaimed log block.
   1972  1.1      haad 	 */
   1973  1.2  christos 	zdb_leak_init(spa, &zcb);
   1974  1.1      haad 
   1975  1.1      haad 	/*
   1976  1.1      haad 	 * If there's a deferred-free bplist, process that first.
   1977  1.1      haad 	 */
   1978  1.2  christos 	if (spa->spa_deferred_bplist_obj != 0) {
   1979  1.2  christos 		bplist_t *bpl = &spa->spa_deferred_bplist;
   1980  1.1      haad 		blkptr_t blk;
   1981  1.1      haad 		uint64_t itor = 0;
   1982  1.1      haad 
   1983  1.1      haad 		VERIFY(0 == bplist_open(bpl, spa->spa_meta_objset,
   1984  1.2  christos 		    spa->spa_deferred_bplist_obj));
   1985  1.1      haad 
   1986  1.1      haad 		while (bplist_iterate(bpl, &itor, &blk) == 0) {
   1987  1.1      haad 			if (dump_opt['b'] >= 4) {
   1988  1.1      haad 				char blkbuf[BP_SPRINTF_LEN];
   1989  1.2  christos 				snprintf_blkptr(blkbuf, sizeof(blkbuf), &blk);
   1990  1.1      haad 				(void) printf("[%s] %s\n",
   1991  1.1      haad 				    "deferred free", blkbuf);
   1992  1.1      haad 			}
   1993  1.2  christos 			zdb_count_block(spa, NULL, &zcb, &blk, ZDB_OT_DEFERRED);
   1994  1.1      haad 		}
   1995  1.1      haad 
   1996  1.1      haad 		bplist_close(bpl);
   1997  1.1      haad 	}
   1998  1.1      haad 
   1999  1.2  christos 	if (dump_opt['c'] > 1)
   2000  1.2  christos 		flags |= TRAVERSE_PREFETCH_DATA;
   2001  1.2  christos 
   2002  1.2  christos 	zcb.zcb_haderrors |= traverse_pool(spa, 0, flags, zdb_blkptr_cb, &zcb);
   2003  1.1      haad 
   2004  1.2  christos 	if (zcb.zcb_haderrors) {
   2005  1.1      haad 		(void) printf("\nError counts:\n\n");
   2006  1.1      haad 		(void) printf("\t%5s  %s\n", "errno", "count");
   2007  1.2  christos 		for (int e = 0; e < 256; e++) {
   2008  1.1      haad 			if (zcb.zcb_errors[e] != 0) {
   2009  1.1      haad 				(void) printf("\t%5d  %llu\n",
   2010  1.1      haad 				    e, (u_longlong_t)zcb.zcb_errors[e]);
   2011  1.1      haad 			}
   2012  1.1      haad 		}
   2013  1.1      haad 	}
   2014  1.1      haad 
   2015  1.1      haad 	/*
   2016  1.1      haad 	 * Report any leaked segments.
   2017  1.1      haad 	 */
   2018  1.2  christos 	zdb_leak_fini(spa);
   2019  1.1      haad 
   2020  1.2  christos 	tzb = &zcb.zcb_type[ZB_TOTAL][ZDB_OT_TOTAL];
   2021  1.1      haad 
   2022  1.2  christos 	norm_alloc = metaslab_class_get_alloc(spa_normal_class(spa));
   2023  1.2  christos 	norm_space = metaslab_class_get_space(spa_normal_class(spa));
   2024  1.1      haad 
   2025  1.2  christos 	total_alloc = norm_alloc + metaslab_class_get_alloc(spa_log_class(spa));
   2026  1.2  christos 	total_found = tzb->zb_asize - zcb.zcb_dedup_asize;
   2027  1.1      haad 
   2028  1.2  christos 	if (total_found == total_alloc) {
   2029  1.1      haad 		if (!dump_opt['L'])
   2030  1.1      haad 			(void) printf("\n\tNo leaks (block sum matches space"
   2031  1.1      haad 			    " maps exactly)\n");
   2032  1.1      haad 	} else {
   2033  1.1      haad 		(void) printf("block traversal size %llu != alloc %llu "
   2034  1.1      haad 		    "(%s %lld)\n",
   2035  1.2  christos 		    (u_longlong_t)total_found,
   2036  1.2  christos 		    (u_longlong_t)total_alloc,
   2037  1.1      haad 		    (dump_opt['L']) ? "unreachable" : "leaked",
   2038  1.2  christos 		    (longlong_t)(total_alloc - total_found));
   2039  1.1      haad 		leaks = 1;
   2040  1.1      haad 	}
   2041  1.1      haad 
   2042  1.1      haad 	if (tzb->zb_count == 0)
   2043  1.1      haad 		return (2);
   2044  1.1      haad 
   2045  1.1      haad 	(void) printf("\n");
   2046  1.1      haad 	(void) printf("\tbp count:      %10llu\n",
   2047  1.1      haad 	    (u_longlong_t)tzb->zb_count);
   2048  1.2  christos 	(void) printf("\tbp logical:    %10llu      avg: %6llu\n",
   2049  1.1      haad 	    (u_longlong_t)tzb->zb_lsize,
   2050  1.1      haad 	    (u_longlong_t)(tzb->zb_lsize / tzb->zb_count));
   2051  1.2  christos 	(void) printf("\tbp physical:   %10llu      avg:"
   2052  1.2  christos 	    " %6llu     compression: %6.2f\n",
   2053  1.1      haad 	    (u_longlong_t)tzb->zb_psize,
   2054  1.1      haad 	    (u_longlong_t)(tzb->zb_psize / tzb->zb_count),
   2055  1.1      haad 	    (double)tzb->zb_lsize / tzb->zb_psize);
   2056  1.2  christos 	(void) printf("\tbp allocated:  %10llu      avg:"
   2057  1.2  christos 	    " %6llu     compression: %6.2f\n",
   2058  1.1      haad 	    (u_longlong_t)tzb->zb_asize,
   2059  1.1      haad 	    (u_longlong_t)(tzb->zb_asize / tzb->zb_count),
   2060  1.1      haad 	    (double)tzb->zb_lsize / tzb->zb_asize);
   2061  1.2  christos 	(void) printf("\tbp deduped:    %10llu    ref>1:"
   2062  1.2  christos 	    " %6llu   deduplication: %6.2f\n",
   2063  1.2  christos 	    (u_longlong_t)zcb.zcb_dedup_asize,
   2064  1.2  christos 	    (u_longlong_t)zcb.zcb_dedup_blocks,
   2065  1.2  christos 	    (double)zcb.zcb_dedup_asize / tzb->zb_asize + 1.0);
   2066  1.2  christos 	(void) printf("\tSPA allocated: %10llu     used: %5.2f%%\n",
   2067  1.2  christos 	    (u_longlong_t)norm_alloc, 100.0 * norm_alloc / norm_space);
   2068  1.1      haad 
   2069  1.1      haad 	if (dump_opt['b'] >= 2) {
   2070  1.1      haad 		int l, t, level;
   2071  1.1      haad 		(void) printf("\nBlocks\tLSIZE\tPSIZE\tASIZE"
   2072  1.1      haad 		    "\t  avg\t comp\t%%Total\tType\n");
   2073  1.1      haad 
   2074  1.2  christos 		for (t = 0; t <= ZDB_OT_TOTAL; t++) {
   2075  1.1      haad 			char csize[6], lsize[6], psize[6], asize[6], avg[6];
   2076  1.1      haad 			char *typename;
   2077  1.1      haad 
   2078  1.2  christos 			if (t < DMU_OT_NUMTYPES)
   2079  1.2  christos 				typename = dmu_ot[t].ot_name;
   2080  1.2  christos 			else
   2081  1.2  christos 				typename = zdb_ot_extname[t - DMU_OT_NUMTYPES];
   2082  1.1      haad 
   2083  1.1      haad 			if (zcb.zcb_type[ZB_TOTAL][t].zb_asize == 0) {
   2084  1.1      haad 				(void) printf("%6s\t%5s\t%5s\t%5s"
   2085  1.1      haad 				    "\t%5s\t%5s\t%6s\t%s\n",
   2086  1.1      haad 				    "-",
   2087  1.1      haad 				    "-",
   2088  1.1      haad 				    "-",
   2089  1.1      haad 				    "-",
   2090  1.1      haad 				    "-",
   2091  1.1      haad 				    "-",
   2092  1.1      haad 				    "-",
   2093  1.1      haad 				    typename);
   2094  1.1      haad 				continue;
   2095  1.1      haad 			}
   2096  1.1      haad 
   2097  1.1      haad 			for (l = ZB_TOTAL - 1; l >= -1; l--) {
   2098  1.1      haad 				level = (l == -1 ? ZB_TOTAL : l);
   2099  1.1      haad 				zb = &zcb.zcb_type[level][t];
   2100  1.1      haad 
   2101  1.1      haad 				if (zb->zb_asize == 0)
   2102  1.1      haad 					continue;
   2103  1.1      haad 
   2104  1.1      haad 				if (dump_opt['b'] < 3 && level != ZB_TOTAL)
   2105  1.1      haad 					continue;
   2106  1.1      haad 
   2107  1.1      haad 				if (level == 0 && zb->zb_asize ==
   2108  1.1      haad 				    zcb.zcb_type[ZB_TOTAL][t].zb_asize)
   2109  1.1      haad 					continue;
   2110  1.1      haad 
   2111  1.1      haad 				nicenum(zb->zb_count, csize);
   2112  1.1      haad 				nicenum(zb->zb_lsize, lsize);
   2113  1.1      haad 				nicenum(zb->zb_psize, psize);
   2114  1.1      haad 				nicenum(zb->zb_asize, asize);
   2115  1.1      haad 				nicenum(zb->zb_asize / zb->zb_count, avg);
   2116  1.1      haad 
   2117  1.1      haad 				(void) printf("%6s\t%5s\t%5s\t%5s\t%5s"
   2118  1.1      haad 				    "\t%5.2f\t%6.2f\t",
   2119  1.1      haad 				    csize, lsize, psize, asize, avg,
   2120  1.1      haad 				    (double)zb->zb_lsize / zb->zb_psize,
   2121  1.1      haad 				    100.0 * zb->zb_asize / tzb->zb_asize);
   2122  1.1      haad 
   2123  1.1      haad 				if (level == ZB_TOTAL)
   2124  1.1      haad 					(void) printf("%s\n", typename);
   2125  1.1      haad 				else
   2126  1.1      haad 					(void) printf("    L%d %s\n",
   2127  1.1      haad 					    level, typename);
   2128  1.1      haad 			}
   2129  1.1      haad 		}
   2130  1.1      haad 	}
   2131  1.1      haad 
   2132  1.1      haad 	(void) printf("\n");
   2133  1.1      haad 
   2134  1.1      haad 	if (leaks)
   2135  1.1      haad 		return (2);
   2136  1.1      haad 
   2137  1.1      haad 	if (zcb.zcb_haderrors)
   2138  1.1      haad 		return (3);
   2139  1.1      haad 
   2140  1.1      haad 	return (0);
   2141  1.1      haad }
   2142  1.1      haad 
   2143  1.2  christos typedef struct zdb_ddt_entry {
   2144  1.2  christos 	ddt_key_t	zdde_key;
   2145  1.2  christos 	uint64_t	zdde_ref_blocks;
   2146  1.2  christos 	uint64_t	zdde_ref_lsize;
   2147  1.2  christos 	uint64_t	zdde_ref_psize;
   2148  1.2  christos 	uint64_t	zdde_ref_dsize;
   2149  1.2  christos 	avl_node_t	zdde_node;
   2150  1.2  christos } zdb_ddt_entry_t;
   2151  1.2  christos 
   2152  1.2  christos /* ARGSUSED */
   2153  1.2  christos static int
   2154  1.2  christos zdb_ddt_add_cb(spa_t *spa, zilog_t *zilog, const blkptr_t *bp,
   2155  1.2  christos     const zbookmark_t *zb, const dnode_phys_t *dnp, void *arg)
   2156  1.2  christos {
   2157  1.2  christos 	avl_tree_t *t = arg;
   2158  1.2  christos 	avl_index_t where;
   2159  1.2  christos 	zdb_ddt_entry_t *zdde, zdde_search;
   2160  1.2  christos 
   2161  1.2  christos 	if (bp == NULL)
   2162  1.2  christos 		return (0);
   2163  1.2  christos 
   2164  1.2  christos 	if (dump_opt['S'] > 1 && zb->zb_level == ZB_ROOT_LEVEL) {
   2165  1.2  christos 		(void) printf("traversing objset %llu, %llu objects, "
   2166  1.2  christos 		    "%lu blocks so far\n",
   2167  1.2  christos 		    (u_longlong_t)zb->zb_objset,
   2168  1.2  christos 		    (u_longlong_t)bp->blk_fill,
   2169  1.2  christos 		    avl_numnodes(t));
   2170  1.2  christos 	}
   2171  1.2  christos 
   2172  1.2  christos 	if (BP_IS_HOLE(bp) || BP_GET_CHECKSUM(bp) == ZIO_CHECKSUM_OFF ||
   2173  1.2  christos 	    BP_GET_LEVEL(bp) > 0 || dmu_ot[BP_GET_TYPE(bp)].ot_metadata)
   2174  1.2  christos 		return (0);
   2175  1.2  christos 
   2176  1.2  christos 	ddt_key_fill(&zdde_search.zdde_key, bp);
   2177  1.2  christos 
   2178  1.2  christos 	zdde = avl_find(t, &zdde_search, &where);
   2179  1.2  christos 
   2180  1.2  christos 	if (zdde == NULL) {
   2181  1.2  christos 		zdde = umem_zalloc(sizeof (*zdde), UMEM_NOFAIL);
   2182  1.2  christos 		zdde->zdde_key = zdde_search.zdde_key;
   2183  1.2  christos 		avl_insert(t, zdde, where);
   2184  1.2  christos 	}
   2185  1.2  christos 
   2186  1.2  christos 	zdde->zdde_ref_blocks += 1;
   2187  1.2  christos 	zdde->zdde_ref_lsize += BP_GET_LSIZE(bp);
   2188  1.2  christos 	zdde->zdde_ref_psize += BP_GET_PSIZE(bp);
   2189  1.2  christos 	zdde->zdde_ref_dsize += bp_get_dsize_sync(spa, bp);
   2190  1.2  christos 
   2191  1.2  christos 	return (0);
   2192  1.2  christos }
   2193  1.2  christos 
   2194  1.2  christos static void
   2195  1.2  christos dump_simulated_ddt(spa_t *spa)
   2196  1.2  christos {
   2197  1.2  christos 	avl_tree_t t;
   2198  1.2  christos 	void *cookie = NULL;
   2199  1.2  christos 	zdb_ddt_entry_t *zdde;
   2200  1.2  christos 	ddt_histogram_t ddh_total = { 0 };
   2201  1.2  christos 	ddt_stat_t dds_total = { 0 };
   2202  1.2  christos 
   2203  1.2  christos 	avl_create(&t, ddt_entry_compare,
   2204  1.2  christos 	    sizeof (zdb_ddt_entry_t), offsetof(zdb_ddt_entry_t, zdde_node));
   2205  1.2  christos 
   2206  1.2  christos 	spa_config_enter(spa, SCL_CONFIG, FTAG, RW_READER);
   2207  1.2  christos 
   2208  1.2  christos 	(void) traverse_pool(spa, 0, TRAVERSE_PRE | TRAVERSE_PREFETCH_METADATA,
   2209  1.2  christos 	    zdb_ddt_add_cb, &t);
   2210  1.2  christos 
   2211  1.2  christos 	spa_config_exit(spa, SCL_CONFIG, FTAG);
   2212  1.2  christos 
   2213  1.2  christos 	while ((zdde = avl_destroy_nodes(&t, &cookie)) != NULL) {
   2214  1.2  christos 		ddt_stat_t dds;
   2215  1.2  christos 		uint64_t refcnt = zdde->zdde_ref_blocks;
   2216  1.2  christos 		ASSERT(refcnt != 0);
   2217  1.2  christos 
   2218  1.2  christos 		dds.dds_blocks = zdde->zdde_ref_blocks / refcnt;
   2219  1.2  christos 		dds.dds_lsize = zdde->zdde_ref_lsize / refcnt;
   2220  1.2  christos 		dds.dds_psize = zdde->zdde_ref_psize / refcnt;
   2221  1.2  christos 		dds.dds_dsize = zdde->zdde_ref_dsize / refcnt;
   2222  1.2  christos 
   2223  1.2  christos 		dds.dds_ref_blocks = zdde->zdde_ref_blocks;
   2224  1.2  christos 		dds.dds_ref_lsize = zdde->zdde_ref_lsize;
   2225  1.2  christos 		dds.dds_ref_psize = zdde->zdde_ref_psize;
   2226  1.2  christos 		dds.dds_ref_dsize = zdde->zdde_ref_dsize;
   2227  1.2  christos 
   2228  1.2  christos 		ddt_stat_add(&ddh_total.ddh_stat[highbit(refcnt) - 1], &dds, 0);
   2229  1.2  christos 
   2230  1.2  christos 		umem_free(zdde, sizeof (*zdde));
   2231  1.2  christos 	}
   2232  1.2  christos 
   2233  1.2  christos 	avl_destroy(&t);
   2234  1.2  christos 
   2235  1.2  christos 	ddt_histogram_stat(&dds_total, &ddh_total);
   2236  1.2  christos 
   2237  1.2  christos 	(void) printf("Simulated DDT histogram:\n");
   2238  1.2  christos 
   2239  1.2  christos 	zpool_dump_ddt(&dds_total, &ddh_total);
   2240  1.2  christos 
   2241  1.2  christos 	dump_dedup_ratio(&dds_total);
   2242  1.2  christos }
   2243  1.2  christos 
   2244  1.1      haad static void
   2245  1.1      haad dump_zpool(spa_t *spa)
   2246  1.1      haad {
   2247  1.1      haad 	dsl_pool_t *dp = spa_get_dsl(spa);
   2248  1.1      haad 	int rc = 0;
   2249  1.1      haad 
   2250  1.2  christos 	if (dump_opt['S']) {
   2251  1.2  christos 		dump_simulated_ddt(spa);
   2252  1.2  christos 		return;
   2253  1.2  christos 	}
   2254  1.2  christos 
   2255  1.2  christos 	if (!dump_opt['e'] && dump_opt['C'] > 1) {
   2256  1.2  christos 		(void) printf("\nCached configuration:\n");
   2257  1.2  christos 		dump_nvlist(spa->spa_config, 8);
   2258  1.2  christos 	}
   2259  1.2  christos 
   2260  1.2  christos 	if (dump_opt['C'])
   2261  1.2  christos 		dump_config(spa);
   2262  1.2  christos 
   2263  1.1      haad 	if (dump_opt['u'])
   2264  1.2  christos 		dump_uberblock(&spa->spa_uberblock, "\nUberblock:\n", "\n");
   2265  1.2  christos 
   2266  1.2  christos 	if (dump_opt['D'])
   2267  1.2  christos 		dump_all_ddts(spa);
   2268  1.2  christos 
   2269  1.2  christos 	if (dump_opt['d'] > 2 || dump_opt['m'])
   2270  1.2  christos 		dump_metaslabs(spa);
   2271  1.1      haad 
   2272  1.1      haad 	if (dump_opt['d'] || dump_opt['i']) {
   2273  1.1      haad 		dump_dir(dp->dp_meta_objset);
   2274  1.1      haad 		if (dump_opt['d'] >= 3) {
   2275  1.1      haad 			dump_bplist(dp->dp_meta_objset,
   2276  1.2  christos 			    spa->spa_deferred_bplist_obj, "Deferred frees");
   2277  1.1      haad 			dump_dtl(spa->spa_root_vdev, 0);
   2278  1.1      haad 		}
   2279  1.2  christos 		(void) dmu_objset_find(spa_name(spa), dump_one_dir,
   2280  1.2  christos 		    NULL, DS_FIND_SNAPSHOTS | DS_FIND_CHILDREN);
   2281  1.1      haad 	}
   2282  1.2  christos 	if (dump_opt['b'] || dump_opt['c'])
   2283  1.1      haad 		rc = dump_block_stats(spa);
   2284  1.1      haad 
   2285  1.1      haad 	if (dump_opt['s'])
   2286  1.1      haad 		show_pool_stats(spa);
   2287  1.1      haad 
   2288  1.2  christos 	if (dump_opt['h'])
   2289  1.2  christos 		dump_history(spa);
   2290  1.2  christos 
   2291  1.1      haad 	if (rc != 0)
   2292  1.1      haad 		exit(rc);
   2293  1.1      haad }
   2294  1.1      haad 
   2295  1.1      haad #define	ZDB_FLAG_CHECKSUM	0x0001
   2296  1.1      haad #define	ZDB_FLAG_DECOMPRESS	0x0002
   2297  1.1      haad #define	ZDB_FLAG_BSWAP		0x0004
   2298  1.1      haad #define	ZDB_FLAG_GBH		0x0008
   2299  1.1      haad #define	ZDB_FLAG_INDIRECT	0x0010
   2300  1.1      haad #define	ZDB_FLAG_PHYS		0x0020
   2301  1.1      haad #define	ZDB_FLAG_RAW		0x0040
   2302  1.1      haad #define	ZDB_FLAG_PRINT_BLKPTR	0x0080
   2303  1.1      haad 
   2304  1.1      haad int flagbits[256];
   2305  1.1      haad 
   2306  1.1      haad static void
   2307  1.1      haad zdb_print_blkptr(blkptr_t *bp, int flags)
   2308  1.1      haad {
   2309  1.2  christos 	char blkbuf[BP_SPRINTF_LEN];
   2310  1.1      haad 
   2311  1.1      haad 	if (flags & ZDB_FLAG_BSWAP)
   2312  1.1      haad 		byteswap_uint64_array((void *)bp, sizeof (blkptr_t));
   2313  1.2  christos 
   2314  1.2  christos 	snprintf_blkptr(blkbuf, sizeof(blkbuf), bp);
   2315  1.2  christos 	(void) printf("%s\n", blkbuf);
   2316  1.1      haad }
   2317  1.1      haad 
   2318  1.1      haad static void
   2319  1.1      haad zdb_dump_indirect(blkptr_t *bp, int nbps, int flags)
   2320  1.1      haad {
   2321  1.1      haad 	int i;
   2322  1.1      haad 
   2323  1.1      haad 	for (i = 0; i < nbps; i++)
   2324  1.1      haad 		zdb_print_blkptr(&bp[i], flags);
   2325  1.1      haad }
   2326  1.1      haad 
   2327  1.1      haad static void
   2328  1.1      haad zdb_dump_gbh(void *buf, int flags)
   2329  1.1      haad {
   2330  1.1      haad 	zdb_dump_indirect((blkptr_t *)buf, SPA_GBH_NBLKPTRS, flags);
   2331  1.1      haad }
   2332  1.1      haad 
   2333  1.1      haad static void
   2334  1.1      haad zdb_dump_block_raw(void *buf, uint64_t size, int flags)
   2335  1.1      haad {
   2336  1.1      haad 	if (flags & ZDB_FLAG_BSWAP)
   2337  1.1      haad 		byteswap_uint64_array(buf, size);
   2338  1.2  christos 	(void) write(1, buf, size);
   2339  1.1      haad }
   2340  1.1      haad 
   2341  1.1      haad static void
   2342  1.1      haad zdb_dump_block(char *label, void *buf, uint64_t size, int flags)
   2343  1.1      haad {
   2344  1.1      haad 	uint64_t *d = (uint64_t *)buf;
   2345  1.1      haad 	int nwords = size / sizeof (uint64_t);
   2346  1.1      haad 	int do_bswap = !!(flags & ZDB_FLAG_BSWAP);
   2347  1.1      haad 	int i, j;
   2348  1.1      haad 	char *hdr, *c;
   2349  1.1      haad 
   2350  1.1      haad 
   2351  1.1      haad 	if (do_bswap)
   2352  1.1      haad 		hdr = " 7 6 5 4 3 2 1 0   f e d c b a 9 8";
   2353  1.1      haad 	else
   2354  1.1      haad 		hdr = " 0 1 2 3 4 5 6 7   8 9 a b c d e f";
   2355  1.1      haad 
   2356  1.1      haad 	(void) printf("\n%s\n%6s   %s  0123456789abcdef\n", label, "", hdr);
   2357  1.1      haad 
   2358  1.1      haad 	for (i = 0; i < nwords; i += 2) {
   2359  1.1      haad 		(void) printf("%06llx:  %016llx  %016llx  ",
   2360  1.1      haad 		    (u_longlong_t)(i * sizeof (uint64_t)),
   2361  1.1      haad 		    (u_longlong_t)(do_bswap ? BSWAP_64(d[i]) : d[i]),
   2362  1.1      haad 		    (u_longlong_t)(do_bswap ? BSWAP_64(d[i + 1]) : d[i + 1]));
   2363  1.1      haad 
   2364  1.1      haad 		c = (char *)&d[i];
   2365  1.1      haad 		for (j = 0; j < 2 * sizeof (uint64_t); j++)
   2366  1.1      haad 			(void) printf("%c", isprint(c[j]) ? c[j] : '.');
   2367  1.1      haad 		(void) printf("\n");
   2368  1.1      haad 	}
   2369  1.1      haad }
   2370  1.1      haad 
   2371  1.1      haad /*
   2372  1.1      haad  * There are two acceptable formats:
   2373  1.1      haad  *	leaf_name	  - For example: c1t0d0 or /tmp/ztest.0a
   2374  1.1      haad  *	child[.child]*    - For example: 0.1.1
   2375  1.1      haad  *
   2376  1.1      haad  * The second form can be used to specify arbitrary vdevs anywhere
   2377  1.1      haad  * in the heirarchy.  For example, in a pool with a mirror of
   2378  1.1      haad  * RAID-Zs, you can specify either RAID-Z vdev with 0.0 or 0.1 .
   2379  1.1      haad  */
   2380  1.1      haad static vdev_t *
   2381  1.1      haad zdb_vdev_lookup(vdev_t *vdev, char *path)
   2382  1.1      haad {
   2383  1.1      haad 	char *s, *p, *q;
   2384  1.1      haad 	int i;
   2385  1.1      haad 
   2386  1.1      haad 	if (vdev == NULL)
   2387  1.1      haad 		return (NULL);
   2388  1.1      haad 
   2389  1.1      haad 	/* First, assume the x.x.x.x format */
   2390  1.1      haad 	i = (int)strtoul(path, &s, 10);
   2391  1.1      haad 	if (s == path || (s && *s != '.' && *s != '\0'))
   2392  1.1      haad 		goto name;
   2393  1.1      haad 	if (i < 0 || i >= vdev->vdev_children)
   2394  1.1      haad 		return (NULL);
   2395  1.1      haad 
   2396  1.1      haad 	vdev = vdev->vdev_child[i];
   2397  1.1      haad 	if (*s == '\0')
   2398  1.1      haad 		return (vdev);
   2399  1.1      haad 	return (zdb_vdev_lookup(vdev, s+1));
   2400  1.1      haad 
   2401  1.1      haad name:
   2402  1.1      haad 	for (i = 0; i < vdev->vdev_children; i++) {
   2403  1.1      haad 		vdev_t *vc = vdev->vdev_child[i];
   2404  1.1      haad 
   2405  1.1      haad 		if (vc->vdev_path == NULL) {
   2406  1.1      haad 			vc = zdb_vdev_lookup(vc, path);
   2407  1.1      haad 			if (vc == NULL)
   2408  1.1      haad 				continue;
   2409  1.1      haad 			else
   2410  1.1      haad 				return (vc);
   2411  1.1      haad 		}
   2412  1.1      haad 
   2413  1.1      haad 		p = strrchr(vc->vdev_path, '/');
   2414  1.1      haad 		p = p ? p + 1 : vc->vdev_path;
   2415  1.1      haad 		q = &vc->vdev_path[strlen(vc->vdev_path) - 2];
   2416  1.1      haad 
   2417  1.1      haad 		if (strcmp(vc->vdev_path, path) == 0)
   2418  1.1      haad 			return (vc);
   2419  1.1      haad 		if (strcmp(p, path) == 0)
   2420  1.1      haad 			return (vc);
   2421  1.1      haad 		if (strcmp(q, "s0") == 0 && strncmp(p, path, q - p) == 0)
   2422  1.1      haad 			return (vc);
   2423  1.1      haad 	}
   2424  1.1      haad 
   2425  1.1      haad 	return (NULL);
   2426  1.1      haad }
   2427  1.1      haad 
   2428  1.1      haad /*
   2429  1.1      haad  * Read a block from a pool and print it out.  The syntax of the
   2430  1.1      haad  * block descriptor is:
   2431  1.1      haad  *
   2432  1.1      haad  *	pool:vdev_specifier:offset:size[:flags]
   2433  1.1      haad  *
   2434  1.1      haad  *	pool           - The name of the pool you wish to read from
   2435  1.1      haad  *	vdev_specifier - Which vdev (see comment for zdb_vdev_lookup)
   2436  1.1      haad  *	offset         - offset, in hex, in bytes
   2437  1.1      haad  *	size           - Amount of data to read, in hex, in bytes
   2438  1.1      haad  *	flags          - A string of characters specifying options
   2439  1.1      haad  *		 b: Decode a blkptr at given offset within block
   2440  1.1      haad  *		*c: Calculate and display checksums
   2441  1.2  christos  *		 d: Decompress data before dumping
   2442  1.1      haad  *		 e: Byteswap data before dumping
   2443  1.2  christos  *		 g: Display data as a gang block header
   2444  1.2  christos  *		 i: Display as an indirect block
   2445  1.1      haad  *		 p: Do I/O to physical offset
   2446  1.1      haad  *		 r: Dump raw data to stdout
   2447  1.1      haad  *
   2448  1.1      haad  *              * = not yet implemented
   2449  1.1      haad  */
   2450  1.1      haad static void
   2451  1.2  christos zdb_read_block(char *thing, spa_t *spa)
   2452  1.1      haad {
   2453  1.2  christos 	blkptr_t blk, *bp = &blk;
   2454  1.2  christos 	dva_t *dva = bp->blk_dva;
   2455  1.1      haad 	int flags = 0;
   2456  1.2  christos 	uint64_t offset = 0, size = 0, psize = 0, lsize = 0, blkptr_offset = 0;
   2457  1.1      haad 	zio_t *zio;
   2458  1.1      haad 	vdev_t *vd;
   2459  1.2  christos 	void *pbuf, *lbuf, *buf;
   2460  1.2  christos 	char *s, *p, *dup, *vdev, *flagstr;
   2461  1.2  christos 	int i, error;
   2462  1.1      haad 
   2463  1.1      haad 	dup = strdup(thing);
   2464  1.1      haad 	s = strtok(dup, ":");
   2465  1.1      haad 	vdev = s ? s : "";
   2466  1.1      haad 	s = strtok(NULL, ":");
   2467  1.1      haad 	offset = strtoull(s ? s : "", NULL, 16);
   2468  1.1      haad 	s = strtok(NULL, ":");
   2469  1.1      haad 	size = strtoull(s ? s : "", NULL, 16);
   2470  1.1      haad 	s = strtok(NULL, ":");
   2471  1.1      haad 	flagstr = s ? s : "";
   2472  1.1      haad 
   2473  1.1      haad 	s = NULL;
   2474  1.1      haad 	if (size == 0)
   2475  1.1      haad 		s = "size must not be zero";
   2476  1.1      haad 	if (!IS_P2ALIGNED(size, DEV_BSIZE))
   2477  1.1      haad 		s = "size must be a multiple of sector size";
   2478  1.1      haad 	if (!IS_P2ALIGNED(offset, DEV_BSIZE))
   2479  1.1      haad 		s = "offset must be a multiple of sector size";
   2480  1.1      haad 	if (s) {
   2481  1.1      haad 		(void) printf("Invalid block specifier: %s  - %s\n", thing, s);
   2482  1.1      haad 		free(dup);
   2483  1.1      haad 		return;
   2484  1.1      haad 	}
   2485  1.1      haad 
   2486  1.1      haad 	for (s = strtok(flagstr, ":"); s; s = strtok(NULL, ":")) {
   2487  1.1      haad 		for (i = 0; flagstr[i]; i++) {
   2488  1.1      haad 			int bit = flagbits[(uchar_t)flagstr[i]];
   2489  1.1      haad 
   2490  1.1      haad 			if (bit == 0) {
   2491  1.1      haad 				(void) printf("***Invalid flag: %c\n",
   2492  1.1      haad 				    flagstr[i]);
   2493  1.1      haad 				continue;
   2494  1.1      haad 			}
   2495  1.1      haad 			flags |= bit;
   2496  1.1      haad 
   2497  1.1      haad 			/* If it's not something with an argument, keep going */
   2498  1.2  christos 			if ((bit & (ZDB_FLAG_CHECKSUM |
   2499  1.1      haad 			    ZDB_FLAG_PRINT_BLKPTR)) == 0)
   2500  1.1      haad 				continue;
   2501  1.1      haad 
   2502  1.1      haad 			p = &flagstr[i + 1];
   2503  1.1      haad 			if (bit == ZDB_FLAG_PRINT_BLKPTR)
   2504  1.1      haad 				blkptr_offset = strtoull(p, &p, 16);
   2505  1.1      haad 			if (*p != ':' && *p != '\0') {
   2506  1.1      haad 				(void) printf("***Invalid flag arg: '%s'\n", s);
   2507  1.1      haad 				free(dup);
   2508  1.1      haad 				return;
   2509  1.1      haad 			}
   2510  1.1      haad 		}
   2511  1.1      haad 	}
   2512  1.1      haad 
   2513  1.1      haad 	vd = zdb_vdev_lookup(spa->spa_root_vdev, vdev);
   2514  1.1      haad 	if (vd == NULL) {
   2515  1.1      haad 		(void) printf("***Invalid vdev: %s\n", vdev);
   2516  1.1      haad 		free(dup);
   2517  1.1      haad 		return;
   2518  1.1      haad 	} else {
   2519  1.1      haad 		if (vd->vdev_path)
   2520  1.2  christos 			(void) fprintf(stderr, "Found vdev: %s\n",
   2521  1.2  christos 			    vd->vdev_path);
   2522  1.1      haad 		else
   2523  1.2  christos 			(void) fprintf(stderr, "Found vdev type: %s\n",
   2524  1.1      haad 			    vd->vdev_ops->vdev_op_type);
   2525  1.1      haad 	}
   2526  1.1      haad 
   2527  1.2  christos 	psize = size;
   2528  1.2  christos 	lsize = size;
   2529  1.2  christos 
   2530  1.2  christos 	pbuf = umem_alloc(SPA_MAXBLOCKSIZE, UMEM_NOFAIL);
   2531  1.2  christos 	lbuf = umem_alloc(SPA_MAXBLOCKSIZE, UMEM_NOFAIL);
   2532  1.2  christos 
   2533  1.2  christos 	BP_ZERO(bp);
   2534  1.1      haad 
   2535  1.2  christos 	DVA_SET_VDEV(&dva[0], vd->vdev_id);
   2536  1.2  christos 	DVA_SET_OFFSET(&dva[0], offset);
   2537  1.2  christos 	DVA_SET_GANG(&dva[0], !!(flags & ZDB_FLAG_GBH));
   2538  1.2  christos 	DVA_SET_ASIZE(&dva[0], vdev_psize_to_asize(vd, psize));
   2539  1.2  christos 
   2540  1.2  christos 	BP_SET_BIRTH(bp, TXG_INITIAL, TXG_INITIAL);
   2541  1.2  christos 
   2542  1.2  christos 	BP_SET_LSIZE(bp, lsize);
   2543  1.2  christos 	BP_SET_PSIZE(bp, psize);
   2544  1.2  christos 	BP_SET_COMPRESS(bp, ZIO_COMPRESS_OFF);
   2545  1.2  christos 	BP_SET_CHECKSUM(bp, ZIO_CHECKSUM_OFF);
   2546  1.2  christos 	BP_SET_TYPE(bp, DMU_OT_NONE);
   2547  1.2  christos 	BP_SET_LEVEL(bp, 0);
   2548  1.2  christos 	BP_SET_DEDUP(bp, 0);
   2549  1.2  christos 	BP_SET_BYTEORDER(bp, ZFS_HOST_BYTEORDER);
   2550  1.1      haad 
   2551  1.1      haad 	spa_config_enter(spa, SCL_STATE, FTAG, RW_READER);
   2552  1.1      haad 	zio = zio_root(spa, NULL, NULL, 0);
   2553  1.2  christos 
   2554  1.2  christos 	if (vd == vd->vdev_top) {
   2555  1.2  christos 		/*
   2556  1.2  christos 		 * Treat this as a normal block read.
   2557  1.2  christos 		 */
   2558  1.2  christos 		zio_nowait(zio_read(zio, spa, bp, pbuf, psize, NULL, NULL,
   2559  1.2  christos 		    ZIO_PRIORITY_SYNC_READ,
   2560  1.2  christos 		    ZIO_FLAG_CANFAIL | ZIO_FLAG_RAW, NULL));
   2561  1.2  christos 	} else {
   2562  1.2  christos 		/*
   2563  1.2  christos 		 * Treat this as a vdev child I/O.
   2564  1.2  christos 		 */
   2565  1.2  christos 		zio_nowait(zio_vdev_child_io(zio, bp, vd, offset, pbuf, psize,
   2566  1.2  christos 		    ZIO_TYPE_READ, ZIO_PRIORITY_SYNC_READ,
   2567  1.2  christos 		    ZIO_FLAG_DONT_CACHE | ZIO_FLAG_DONT_QUEUE |
   2568  1.2  christos 		    ZIO_FLAG_DONT_PROPAGATE | ZIO_FLAG_DONT_RETRY |
   2569  1.2  christos 		    ZIO_FLAG_CANFAIL | ZIO_FLAG_RAW, NULL, NULL));
   2570  1.2  christos 	}
   2571  1.2  christos 
   2572  1.1      haad 	error = zio_wait(zio);
   2573  1.1      haad 	spa_config_exit(spa, SCL_STATE, FTAG);
   2574  1.1      haad 
   2575  1.1      haad 	if (error) {
   2576  1.1      haad 		(void) printf("Read of %s failed, error: %d\n", thing, error);
   2577  1.1      haad 		goto out;
   2578  1.1      haad 	}
   2579  1.1      haad 
   2580  1.2  christos 	if (flags & ZDB_FLAG_DECOMPRESS) {
   2581  1.2  christos 		/*
   2582  1.2  christos 		 * We don't know how the data was compressed, so just try
   2583  1.2  christos 		 * every decompress function at every inflated blocksize.
   2584  1.2  christos 		 */
   2585  1.2  christos 		enum zio_compress c;
   2586  1.2  christos 		void *pbuf2 = umem_alloc(SPA_MAXBLOCKSIZE, UMEM_NOFAIL);
   2587  1.2  christos 		void *lbuf2 = umem_alloc(SPA_MAXBLOCKSIZE, UMEM_NOFAIL);
   2588  1.2  christos 
   2589  1.2  christos 		bcopy(pbuf, pbuf2, psize);
   2590  1.2  christos 
   2591  1.2  christos 		VERIFY(random_get_pseudo_bytes((uint8_t *)pbuf + psize,
   2592  1.2  christos 		    SPA_MAXBLOCKSIZE - psize) == 0);
   2593  1.2  christos 
   2594  1.2  christos 		VERIFY(random_get_pseudo_bytes((uint8_t *)pbuf2 + psize,
   2595  1.2  christos 		    SPA_MAXBLOCKSIZE - psize) == 0);
   2596  1.2  christos 
   2597  1.2  christos 		for (lsize = SPA_MAXBLOCKSIZE; lsize > psize;
   2598  1.2  christos 		    lsize -= SPA_MINBLOCKSIZE) {
   2599  1.2  christos 			for (c = 0; c < ZIO_COMPRESS_FUNCTIONS; c++) {
   2600  1.2  christos 				if (zio_decompress_data(c, pbuf, lbuf,
   2601  1.2  christos 				    psize, lsize) == 0 &&
   2602  1.2  christos 				    zio_decompress_data(c, pbuf2, lbuf2,
   2603  1.2  christos 				    psize, lsize) == 0 &&
   2604  1.2  christos 				    bcmp(lbuf, lbuf2, lsize) == 0)
   2605  1.2  christos 					break;
   2606  1.2  christos 			}
   2607  1.2  christos 			if (c != ZIO_COMPRESS_FUNCTIONS)
   2608  1.2  christos 				break;
   2609  1.2  christos 			lsize -= SPA_MINBLOCKSIZE;
   2610  1.2  christos 		}
   2611  1.2  christos 
   2612  1.2  christos 		umem_free(pbuf2, SPA_MAXBLOCKSIZE);
   2613  1.2  christos 		umem_free(lbuf2, SPA_MAXBLOCKSIZE);
   2614  1.2  christos 
   2615  1.2  christos 		if (lsize <= psize) {
   2616  1.2  christos 			(void) printf("Decompress of %s failed\n", thing);
   2617  1.2  christos 			goto out;
   2618  1.2  christos 		}
   2619  1.2  christos 		buf = lbuf;
   2620  1.2  christos 		size = lsize;
   2621  1.2  christos 	} else {
   2622  1.2  christos 		buf = pbuf;
   2623  1.2  christos 		size = psize;
   2624  1.2  christos 	}
   2625  1.2  christos 
   2626  1.1      haad 	if (flags & ZDB_FLAG_PRINT_BLKPTR)
   2627  1.1      haad 		zdb_print_blkptr((blkptr_t *)(void *)
   2628  1.1      haad 		    ((uintptr_t)buf + (uintptr_t)blkptr_offset), flags);
   2629  1.1      haad 	else if (flags & ZDB_FLAG_RAW)
   2630  1.1      haad 		zdb_dump_block_raw(buf, size, flags);
   2631  1.1      haad 	else if (flags & ZDB_FLAG_INDIRECT)
   2632  1.1      haad 		zdb_dump_indirect((blkptr_t *)buf, size / sizeof (blkptr_t),
   2633  1.1      haad 		    flags);
   2634  1.1      haad 	else if (flags & ZDB_FLAG_GBH)
   2635  1.1      haad 		zdb_dump_gbh(buf, flags);
   2636  1.1      haad 	else
   2637  1.1      haad 		zdb_dump_block(thing, buf, size, flags);
   2638  1.1      haad 
   2639  1.1      haad out:
   2640  1.2  christos 	umem_free(pbuf, SPA_MAXBLOCKSIZE);
   2641  1.2  christos 	umem_free(lbuf, SPA_MAXBLOCKSIZE);
   2642  1.1      haad 	free(dup);
   2643  1.1      haad }
   2644  1.1      haad 
   2645  1.1      haad static boolean_t
   2646  1.2  christos pool_match(nvlist_t *cfg, char *tgt)
   2647  1.1      haad {
   2648  1.2  christos 	uint64_t v, guid = strtoull(tgt, NULL, 0);
   2649  1.1      haad 	char *s;
   2650  1.1      haad 
   2651  1.1      haad 	if (guid != 0) {
   2652  1.2  christos 		if (nvlist_lookup_uint64(cfg, ZPOOL_CONFIG_POOL_GUID, &v) == 0)
   2653  1.2  christos 			return (v == guid);
   2654  1.1      haad 	} else {
   2655  1.2  christos 		if (nvlist_lookup_string(cfg, ZPOOL_CONFIG_POOL_NAME, &s) == 0)
   2656  1.2  christos 			return (strcmp(s, tgt) == 0);
   2657  1.1      haad 	}
   2658  1.2  christos 	return (B_FALSE);
   2659  1.1      haad }
   2660  1.1      haad 
   2661  1.2  christos static char *
   2662  1.2  christos find_zpool(char **target, nvlist_t **configp, int dirc, char **dirv)
   2663  1.1      haad {
   2664  1.1      haad 	nvlist_t *pools;
   2665  1.1      haad 	nvlist_t *match = NULL;
   2666  1.2  christos 	char *name = NULL;
   2667  1.2  christos 	char *sepp = NULL;
   2668  1.2  christos 	char sep;
   2669  1.2  christos 	int count = 0;
   2670  1.2  christos 	importargs_t args = { 0 };
   2671  1.2  christos 
   2672  1.2  christos 	args.paths = dirc;
   2673  1.2  christos 	args.path = dirv;
   2674  1.2  christos 	args.can_be_active = B_TRUE;
   2675  1.2  christos 
   2676  1.2  christos 	if ((sepp = strpbrk(*target, "/@")) != NULL) {
   2677  1.2  christos 		sep = *sepp;
   2678  1.2  christos 		*sepp = '\0';
   2679  1.2  christos 	}
   2680  1.1      haad 
   2681  1.2  christos 	pools = zpool_search_import(g_zfs, &args);
   2682  1.1      haad 
   2683  1.1      haad 	if (pools != NULL) {
   2684  1.1      haad 		nvpair_t *elem = NULL;
   2685  1.1      haad 		while ((elem = nvlist_next_nvpair(pools, elem)) != NULL) {
   2686  1.1      haad 			verify(nvpair_value_nvlist(elem, configp) == 0);
   2687  1.2  christos 			if (pool_match(*configp, *target)) {
   2688  1.2  christos 				count++;
   2689  1.1      haad 				if (match != NULL) {
   2690  1.2  christos 					/* print previously found config */
   2691  1.2  christos 					if (name != NULL) {
   2692  1.2  christos 						(void) printf("%s\n", name);
   2693  1.2  christos 						dump_nvlist(match, 8);
   2694  1.2  christos 						name = NULL;
   2695  1.2  christos 					}
   2696  1.2  christos 					(void) printf("%s\n",
   2697  1.2  christos 					    nvpair_name(elem));
   2698  1.2  christos 					dump_nvlist(*configp, 8);
   2699  1.1      haad 				} else {
   2700  1.1      haad 					match = *configp;
   2701  1.2  christos 					name = nvpair_name(elem);
   2702  1.1      haad 				}
   2703  1.1      haad 			}
   2704  1.1      haad 		}
   2705  1.1      haad 	}
   2706  1.2  christos 	if (count > 1)
   2707  1.2  christos 		(void) fatal("\tMatched %d pools - use pool GUID "
   2708  1.2  christos 		    "instead of pool name or \n"
   2709  1.2  christos 		    "\tpool name part of a dataset name to select pool", count);
   2710  1.1      haad 
   2711  1.2  christos 	if (sepp)
   2712  1.2  christos 		*sepp = sep;
   2713  1.2  christos 	/*
   2714  1.2  christos 	 * If pool GUID was specified for pool id, replace it with pool name
   2715  1.2  christos 	 */
   2716  1.2  christos 	if (name && (strstr(*target, name) != *target)) {
   2717  1.2  christos 		int sz = 1 + strlen(name) + ((sepp) ? strlen(sepp) : 0);
   2718  1.2  christos 
   2719  1.2  christos 		*target = umem_alloc(sz, UMEM_NOFAIL);
   2720  1.2  christos 		(void) snprintf(*target, sz, "%s%s", name, sepp ? sepp : "");
   2721  1.2  christos 	}
   2722  1.1      haad 
   2723  1.2  christos 	*configp = name ? match : NULL;
   2724  1.2  christos 
   2725  1.2  christos 	return (name);
   2726  1.1      haad }
   2727  1.1      haad 
   2728  1.1      haad int
   2729  1.1      haad main(int argc, char **argv)
   2730  1.1      haad {
   2731  1.1      haad 	int i, c;
   2732  1.1      haad 	struct rlimit rl = { 1024, 1024 };
   2733  1.2  christos 	spa_t *spa = NULL;
   2734  1.1      haad 	objset_t *os = NULL;
   2735  1.1      haad 	int dump_all = 1;
   2736  1.1      haad 	int verbose = 0;
   2737  1.2  christos 	int error = 0;
   2738  1.2  christos 	char **searchdirs = NULL;
   2739  1.2  christos 	int nsearch = 0;
   2740  1.2  christos 	char *target;
   2741  1.2  christos 	nvlist_t *policy = NULL;
   2742  1.2  christos 	uint64_t max_txg = UINT64_MAX;
   2743  1.2  christos 	int rewind = ZPOOL_NEVER_REWIND;
   2744  1.1      haad 
   2745  1.1      haad 	(void) setrlimit(RLIMIT_NOFILE, &rl);
   2746  1.1      haad 	(void) enable_extended_FILE_stdio(-1, -1);
   2747  1.1      haad 
   2748  1.1      haad 	dprintf_setup(&argc, argv);
   2749  1.1      haad 
   2750  1.2  christos 	while ((c = getopt(argc, argv, "bcdhilmsuCDRSAFLXevp:t:U:")) != -1) {
   2751  1.1      haad 		switch (c) {
   2752  1.2  christos 		case 'b':
   2753  1.2  christos 		case 'c':
   2754  1.1      haad 		case 'd':
   2755  1.2  christos 		case 'h':
   2756  1.1      haad 		case 'i':
   2757  1.2  christos 		case 'l':
   2758  1.2  christos 		case 'm':
   2759  1.1      haad 		case 's':
   2760  1.2  christos 		case 'u':
   2761  1.1      haad 		case 'C':
   2762  1.2  christos 		case 'D':
   2763  1.1      haad 		case 'R':
   2764  1.2  christos 		case 'S':
   2765  1.1      haad 			dump_opt[c]++;
   2766  1.1      haad 			dump_all = 0;
   2767  1.1      haad 			break;
   2768  1.2  christos 		case 'A':
   2769  1.2  christos 		case 'F':
   2770  1.1      haad 		case 'L':
   2771  1.2  christos 		case 'X':
   2772  1.2  christos 		case 'e':
   2773  1.1      haad 			dump_opt[c]++;
   2774  1.1      haad 			break;
   2775  1.1      haad 		case 'v':
   2776  1.1      haad 			verbose++;
   2777  1.1      haad 			break;
   2778  1.1      haad 		case 'p':
   2779  1.2  christos 			if (searchdirs == NULL) {
   2780  1.2  christos 				searchdirs = umem_alloc(sizeof (char *),
   2781  1.2  christos 				    UMEM_NOFAIL);
   2782  1.2  christos 			} else {
   2783  1.2  christos 				char **tmp = umem_alloc((nsearch + 1) *
   2784  1.2  christos 				    sizeof (char *), UMEM_NOFAIL);
   2785  1.2  christos 				bcopy(searchdirs, tmp, nsearch *
   2786  1.2  christos 				    sizeof (char *));
   2787  1.2  christos 				umem_free(searchdirs,
   2788  1.2  christos 				    nsearch * sizeof (char *));
   2789  1.2  christos 				searchdirs = tmp;
   2790  1.2  christos 			}
   2791  1.2  christos 			searchdirs[nsearch++] = optarg;
   2792  1.1      haad 			break;
   2793  1.2  christos 		case 't':
   2794  1.2  christos 			max_txg = strtoull(optarg, NULL, 0);
   2795  1.2  christos 			if (max_txg < TXG_INITIAL) {
   2796  1.2  christos 				(void) fprintf(stderr, "incorrect txg "
   2797  1.2  christos 				    "specified: %s\n", optarg);
   2798  1.1      haad 				usage();
   2799  1.2  christos 			}
   2800  1.2  christos 			break;
   2801  1.2  christos 		case 'U':
   2802  1.2  christos 			spa_config_path = optarg;
   2803  1.1      haad 			break;
   2804  1.1      haad 		default:
   2805  1.1      haad 			usage();
   2806  1.1      haad 			break;
   2807  1.1      haad 		}
   2808  1.1      haad 	}
   2809  1.1      haad 
   2810  1.2  christos 	if (!dump_opt['e'] && searchdirs != NULL) {
   2811  1.1      haad 		(void) fprintf(stderr, "-p option requires use of -e\n");
   2812  1.1      haad 		usage();
   2813  1.1      haad 	}
   2814  1.1      haad 
   2815  1.1      haad 	kernel_init(FREAD);
   2816  1.1      haad 	g_zfs = libzfs_init();
   2817  1.1      haad 	ASSERT(g_zfs != NULL);
   2818  1.1      haad 
   2819  1.2  christos 	if (dump_all)
   2820  1.2  christos 		verbose = MAX(verbose, 1);
   2821  1.2  christos 
   2822  1.1      haad 	for (c = 0; c < 256; c++) {
   2823  1.2  christos 		if (dump_all && !strchr("elAFLRSX", c))
   2824  1.1      haad 			dump_opt[c] = 1;
   2825  1.1      haad 		if (dump_opt[c])
   2826  1.1      haad 			dump_opt[c] += verbose;
   2827  1.1      haad 	}
   2828  1.1      haad 
   2829  1.2  christos 	aok = (dump_opt['A'] == 1) || (dump_opt['A'] > 2);
   2830  1.2  christos 	zfs_recover = (dump_opt['A'] > 1);
   2831  1.2  christos 
   2832  1.1      haad 	argc -= optind;
   2833  1.1      haad 	argv += optind;
   2834  1.1      haad 
   2835  1.2  christos 	if (argc < 2 && dump_opt['R'])
   2836  1.2  christos 		usage();
   2837  1.1      haad 	if (argc < 1) {
   2838  1.2  christos 		if (!dump_opt['e'] && dump_opt['C']) {
   2839  1.1      haad 			dump_cachefile(spa_config_path);
   2840  1.1      haad 			return (0);
   2841  1.1      haad 		}
   2842  1.1      haad 		usage();
   2843  1.1      haad 	}
   2844  1.1      haad 
   2845  1.1      haad 	if (dump_opt['l']) {
   2846  1.1      haad 		dump_label(argv[0]);
   2847  1.1      haad 		return (0);
   2848  1.1      haad 	}
   2849  1.1      haad 
   2850  1.2  christos 	if (dump_opt['X'] || dump_opt['F'])
   2851  1.2  christos 		rewind = ZPOOL_DO_REWIND |
   2852  1.2  christos 		    (dump_opt['X'] ? ZPOOL_EXTREME_REWIND : 0);
   2853  1.2  christos 
   2854  1.2  christos 	if (nvlist_alloc(&policy, NV_UNIQUE_NAME_TYPE, 0) != 0 ||
   2855  1.2  christos 	    nvlist_add_uint64(policy, ZPOOL_REWIND_REQUEST_TXG, max_txg) != 0 ||
   2856  1.2  christos 	    nvlist_add_uint32(policy, ZPOOL_REWIND_REQUEST, rewind) != 0)
   2857  1.2  christos 		fatal("internal error: %s", strerror(ENOMEM));
   2858  1.1      haad 
   2859  1.1      haad 	error = 0;
   2860  1.2  christos 	target = argv[0];
   2861  1.1      haad 
   2862  1.2  christos 	if (dump_opt['e']) {
   2863  1.2  christos 		nvlist_t *cfg = NULL;
   2864  1.2  christos 		char *name = find_zpool(&target, &cfg, nsearch, searchdirs);
   2865  1.2  christos 
   2866  1.2  christos 		error = ENOENT;
   2867  1.2  christos 		if (name) {
   2868  1.2  christos 			if (dump_opt['C'] > 1) {
   2869  1.2  christos 				(void) printf("\nConfiguration for import:\n");
   2870  1.2  christos 				dump_nvlist(cfg, 8);
   2871  1.2  christos 			}
   2872  1.2  christos 			if (nvlist_add_nvlist(cfg,
   2873  1.2  christos 			    ZPOOL_REWIND_POLICY, policy) != 0) {
   2874  1.2  christos 				fatal("can't open '%s': %s",
   2875  1.2  christos 				    target, strerror(ENOMEM));
   2876  1.1      haad 			}
   2877  1.2  christos 			if ((error = spa_import(name, cfg, NULL)) != 0)
   2878  1.2  christos 				error = spa_import_verbatim(name, cfg, NULL);
   2879  1.1      haad 		}
   2880  1.1      haad 	}
   2881  1.1      haad 
   2882  1.1      haad 	if (error == 0) {
   2883  1.2  christos 		if (strpbrk(target, "/@") == NULL || dump_opt['R']) {
   2884  1.2  christos 			error = spa_open_rewind(target, &spa, FTAG, policy,
   2885  1.2  christos 			    NULL);
   2886  1.2  christos 			if (error) {
   2887  1.2  christos 				/*
   2888  1.2  christos 				 * If we're missing the log device then
   2889  1.2  christos 				 * try opening the pool after clearing the
   2890  1.2  christos 				 * log state.
   2891  1.2  christos 				 */
   2892  1.2  christos 				mutex_enter(&spa_namespace_lock);
   2893  1.2  christos 				if ((spa = spa_lookup(target)) != NULL &&
   2894  1.2  christos 				    spa->spa_log_state == SPA_LOG_MISSING) {
   2895  1.2  christos 					spa->spa_log_state = SPA_LOG_CLEAR;
   2896  1.2  christos 					error = 0;
   2897  1.2  christos 				}
   2898  1.2  christos 				mutex_exit(&spa_namespace_lock);
   2899  1.2  christos 
   2900  1.2  christos 				if (!error) {
   2901  1.2  christos 					error = spa_open_rewind(target, &spa,
   2902  1.2  christos 					    FTAG, policy, NULL);
   2903  1.2  christos 				}
   2904  1.2  christos 			}
   2905  1.1      haad 		} else {
   2906  1.2  christos 			error = dmu_objset_own(target, DMU_OST_ANY,
   2907  1.2  christos 			    B_TRUE, FTAG, &os);
   2908  1.1      haad 		}
   2909  1.1      haad 	}
   2910  1.2  christos 	nvlist_free(policy);
   2911  1.1      haad 
   2912  1.1      haad 	if (error)
   2913  1.2  christos 		fatal("can't open '%s': %s", target, strerror(error));
   2914  1.1      haad 
   2915  1.1      haad 	argv++;
   2916  1.2  christos 	argc--;
   2917  1.2  christos 	if (!dump_opt['R']) {
   2918  1.2  christos 		if (argc > 0) {
   2919  1.2  christos 			zopt_objects = argc;
   2920  1.2  christos 			zopt_object = calloc(zopt_objects, sizeof (uint64_t));
   2921  1.2  christos 			for (i = 0; i < zopt_objects; i++) {
   2922  1.2  christos 				errno = 0;
   2923  1.2  christos 				zopt_object[i] = strtoull(argv[i], NULL, 0);
   2924  1.2  christos 				if (zopt_object[i] == 0 && errno != 0)
   2925  1.2  christos 					fatal("bad number %s: %s",
   2926  1.2  christos 					    argv[i], strerror(errno));
   2927  1.2  christos 			}
   2928  1.1      haad 		}
   2929  1.2  christos 		(os != NULL) ? dump_dir(os) : dump_zpool(spa);
   2930  1.2  christos 	} else {
   2931  1.2  christos 		flagbits['b'] = ZDB_FLAG_PRINT_BLKPTR;
   2932  1.2  christos 		flagbits['c'] = ZDB_FLAG_CHECKSUM;
   2933  1.2  christos 		flagbits['d'] = ZDB_FLAG_DECOMPRESS;
   2934  1.2  christos 		flagbits['e'] = ZDB_FLAG_BSWAP;
   2935  1.2  christos 		flagbits['g'] = ZDB_FLAG_GBH;
   2936  1.2  christos 		flagbits['i'] = ZDB_FLAG_INDIRECT;
   2937  1.2  christos 		flagbits['p'] = ZDB_FLAG_PHYS;
   2938  1.2  christos 		flagbits['r'] = ZDB_FLAG_RAW;
   2939  1.2  christos 
   2940  1.2  christos 		for (i = 0; i < argc; i++)
   2941  1.2  christos 			zdb_read_block(argv[i], spa);
   2942  1.1      haad 	}
   2943  1.1      haad 
   2944  1.2  christos 	(os != NULL) ? dmu_objset_disown(os, FTAG) : spa_close(spa, FTAG);
   2945  1.1      haad 
   2946  1.1      haad 	fuid_table_destroy();
   2947  1.1      haad 
   2948  1.1      haad 	libzfs_fini(g_zfs);
   2949  1.1      haad 	kernel_fini();
   2950  1.1      haad 
   2951  1.1      haad 	return (0);
   2952  1.1      haad }
   2953