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