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