]> git.proxmox.com Git - mirror_zfs-debian.git/blame - cmd/zdb/zdb.c
New upstream version 0.7.9
[mirror_zfs-debian.git] / cmd / zdb / zdb.c
CommitLineData
34dc7c2f
BB
1/*
2 * CDDL HEADER START
3 *
4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License (the "License").
6 * You may not use this file except in compliance with the License.
7 *
8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 * or http://www.opensolaris.org/os/licensing.
10 * See the License for the specific language governing permissions
11 * and limitations under the License.
12 *
13 * When distributing Covered Code, include this CDDL HEADER in each
14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 * If applicable, add the following below this CDDL HEADER, with the
16 * fields enclosed by brackets "[]" replaced with your own identifying
17 * information: Portions Copyright [yyyy] [name of copyright owner]
18 *
19 * CDDL HEADER END
20 */
9ae529ec 21
34dc7c2f 22/*
428870ff 23 * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
cae5b340
AX
24 * Copyright (c) 2011, 2016 by Delphix. All rights reserved.
25 * Copyright (c) 2014 Integros [integros.com]
26 * Copyright 2016 Nexenta Systems, Inc.
27 * Copyright (c) 2017 Lawrence Livermore National Security, LLC.
28 * Copyright (c) 2015, 2017, Intel Corporation.
34dc7c2f
BB
29 */
30
34dc7c2f 31#include <stdio.h>
ea04106b 32#include <unistd.h>
34dc7c2f
BB
33#include <stdio_ext.h>
34#include <stdlib.h>
35#include <ctype.h>
36#include <sys/zfs_context.h>
37#include <sys/spa.h>
38#include <sys/spa_impl.h>
39#include <sys/dmu.h>
40#include <sys/zap.h>
41#include <sys/fs/zfs.h>
42#include <sys/zfs_znode.h>
428870ff
BB
43#include <sys/zfs_sa.h>
44#include <sys/sa.h>
45#include <sys/sa_impl.h>
34dc7c2f
BB
46#include <sys/vdev.h>
47#include <sys/vdev_impl.h>
48#include <sys/metaslab_impl.h>
49#include <sys/dmu_objset.h>
50#include <sys/dsl_dir.h>
51#include <sys/dsl_dataset.h>
52#include <sys/dsl_pool.h>
53#include <sys/dbuf.h>
54#include <sys/zil.h>
55#include <sys/zil_impl.h>
56#include <sys/stat.h>
57#include <sys/resource.h>
58#include <sys/dmu_traverse.h>
59#include <sys/zio_checksum.h>
60#include <sys/zio_compress.h>
61#include <sys/zfs_fuid.h>
b128c09f 62#include <sys/arc.h>
428870ff 63#include <sys/ddt.h>
9ae529ec 64#include <sys/zfeature.h>
cae5b340
AX
65#include <sys/abd.h>
66#include <sys/blkptr.h>
a08ee875 67#include <zfs_comutil.h>
34dc7c2f
BB
68#include <libzfs.h>
69
e6f7d015
CS
70#define ZDB_COMPRESS_NAME(idx) ((idx) < ZIO_COMPRESS_FUNCTIONS ? \
71 zio_compress_table[(idx)].ci_name : "UNKNOWN")
72#define ZDB_CHECKSUM_NAME(idx) ((idx) < ZIO_CHECKSUM_FUNCTIONS ? \
73 zio_checksum_table[(idx)].ci_name : "UNKNOWN")
e6f7d015
CS
74#define ZDB_OT_TYPE(idx) ((idx) < DMU_OT_NUMTYPES ? (idx) : \
75 (((idx) == DMU_OTN_ZAP_DATA || (idx) == DMU_OTN_ZAP_METADATA) ? \
76 DMU_OT_ZAP_OTHER : DMU_OT_NUMTYPES))
428870ff 77
87dac73d
AX
78static char *
79zdb_ot_name(dmu_object_type_t type)
80{
81 if (type < DMU_OT_NUMTYPES)
82 return (dmu_ot[type].ot_name);
83 else if ((type & DMU_OT_NEWTYPE) &&
cae5b340 84 ((type & DMU_OT_BYTESWAP_MASK) < DMU_BSWAP_NUMFUNCS))
87dac73d
AX
85 return (dmu_ot_byteswap[type & DMU_OT_BYTESWAP_MASK].ob_name);
86 else
87 return ("UNKNOWN");
88}
89
cae5b340 90extern int reference_tracking_enable;
428870ff 91extern int zfs_recover;
ea04106b 92extern uint64_t zfs_arc_max, zfs_arc_meta_limit;
e10b0808 93extern int zfs_vdev_async_read_max_active;
428870ff 94
34dc7c2f
BB
95const char cmdname[] = "zdb";
96uint8_t dump_opt[256];
97
98typedef void object_viewer_t(objset_t *, uint64_t, void *data, size_t size);
99
100extern void dump_intent_log(zilog_t *);
101uint64_t *zopt_object = NULL;
102int zopt_objects = 0;
34dc7c2f 103libzfs_handle_t *g_zfs;
ea04106b 104uint64_t max_inflight = 1000;
34dc7c2f 105
e10b0808
AX
106static void snprintf_blkptr_compact(char *, size_t, const blkptr_t *);
107
34dc7c2f
BB
108/*
109 * These libumem hooks provide a reasonable set of defaults for the allocator's
110 * debugging facilities.
111 */
112const char *
0bc8fd78 113_umem_debug_init(void)
34dc7c2f
BB
114{
115 return ("default,verbose"); /* $UMEM_DEBUG setting */
116}
117
118const char *
119_umem_logging_init(void)
120{
121 return ("fail,contents"); /* $UMEM_LOGGING setting */
122}
123
124static void
125usage(void)
126{
127 (void) fprintf(stderr,
cae5b340
AX
128 "Usage:\t%s [-AbcdDFGhiLMPsvX] [-e [-V] [-p <path> ...]] "
129 "[-I <inflight I/Os>]\n"
130 "\t\t[-o <var>=<value>]... [-t <txg>] [-U <cache>] [-x <dumpdir>]\n"
131 "\t\t[<poolname> [<object> ...]]\n"
132 "\t%s [-AdiPv] [-e [-V] [-p <path> ...]] [-U <cache>] <dataset> "
133 "[<object> ...]\n"
134 "\t%s -C [-A] [-U <cache>]\n"
135 "\t%s -l [-Aqu] <device>\n"
136 "\t%s -m [-AFLPX] [-e [-V] [-p <path> ...]] [-t <txg>] "
137 "[-U <cache>]\n\t\t<poolname> [<vdev> [<metaslab> ...]]\n"
138 "\t%s -O <dataset> <path>\n"
139 "\t%s -R [-A] [-e [-V] [-p <path> ...]] [-U <cache>]\n"
140 "\t\t<poolname> <vdev>:<offset>:<size>[:<flags>]\n"
141 "\t%s -E [-A] word0:word1:...:word15\n"
142 "\t%s -S [-AP] [-e [-V] [-p <path> ...]] [-U <cache>] "
143 "<poolname>\n\n",
144 cmdname, cmdname, cmdname, cmdname, cmdname, cmdname, cmdname,
145 cmdname, cmdname);
428870ff
BB
146
147 (void) fprintf(stderr, " Dataset name must include at least one "
148 "separator character '/' or '@'\n");
149 (void) fprintf(stderr, " If dataset name is specified, only that "
150 "dataset is dumped\n");
151 (void) fprintf(stderr, " If object numbers are specified, only "
152 "those objects are dumped\n\n");
153 (void) fprintf(stderr, " Options to control amount of output:\n");
428870ff 154 (void) fprintf(stderr, " -b block statistics\n");
428870ff 155 (void) fprintf(stderr, " -c checksum all metadata (twice for "
9babb374 156 "all data) blocks\n");
cae5b340
AX
157 (void) fprintf(stderr, " -C config (or cachefile if alone)\n");
158 (void) fprintf(stderr, " -d dataset(s)\n");
428870ff 159 (void) fprintf(stderr, " -D dedup statistics\n");
cae5b340
AX
160 (void) fprintf(stderr, " -E decode and display block from an "
161 "embedded block pointer\n");
162 (void) fprintf(stderr, " -h pool history\n");
163 (void) fprintf(stderr, " -i intent logs\n");
164 (void) fprintf(stderr, " -l read label contents\n");
fb5f0bc8
BB
165 (void) fprintf(stderr, " -L disable leak tracking (do not "
166 "load spacemaps)\n");
cae5b340
AX
167 (void) fprintf(stderr, " -m metaslabs\n");
168 (void) fprintf(stderr, " -M metaslab groups\n");
169 (void) fprintf(stderr, " -O perform object lookups by path\n");
34dc7c2f 170 (void) fprintf(stderr, " -R read and display block from a "
cae5b340
AX
171 "device\n");
172 (void) fprintf(stderr, " -s report stats on zdb's I/O\n");
173 (void) fprintf(stderr, " -S simulate dedup to measure effect\n");
174 (void) fprintf(stderr, " -v verbose (applies to all "
175 "others)\n\n");
428870ff 176 (void) fprintf(stderr, " Below options are intended for use "
cae5b340 177 "with other options:\n");
428870ff
BB
178 (void) fprintf(stderr, " -A ignore assertions (-A), enable "
179 "panic recovery (-AA) or both (-AAA)\n");
428870ff
BB
180 (void) fprintf(stderr, " -e pool is exported/destroyed/"
181 "has altroot/not in a cachefile\n");
cae5b340
AX
182 (void) fprintf(stderr, " -F attempt automatic rewind within "
183 "safe range of transaction groups\n");
184 (void) fprintf(stderr, " -G dump zfs_dbgmsg buffer before "
185 "exiting\n");
186 (void) fprintf(stderr, " -I <number of inflight I/Os> -- "
187 "specify the maximum number of\n "
188 "checksumming I/Os [default is 200]\n");
189 (void) fprintf(stderr, " -o <variable>=<value> set global "
190 "variable to an unsigned 32-bit integer\n");
428870ff
BB
191 (void) fprintf(stderr, " -p <path> -- use one or more with "
192 "-e to specify path to vdev dir\n");
dd4769ad 193 (void) fprintf(stderr, " -P print numbers in parseable form\n");
cae5b340 194 (void) fprintf(stderr, " -q don't print label contents\n");
428870ff 195 (void) fprintf(stderr, " -t <txg> -- highest txg to use when "
fb5f0bc8 196 "searching for uberblocks\n");
cae5b340
AX
197 (void) fprintf(stderr, " -u uberblock\n");
198 (void) fprintf(stderr, " -U <cachefile_path> -- use alternate "
199 "cachefile\n");
200 (void) fprintf(stderr, " -V do verbatim import\n");
201 (void) fprintf(stderr, " -x <dumpdir> -- "
202 "dump all read blocks into specified directory\n");
203 (void) fprintf(stderr, " -X attempt extreme rewind (does not "
204 "work with dataset)\n");
34dc7c2f
BB
205 (void) fprintf(stderr, "Specify an option more than once (e.g. -bb) "
206 "to make only that option verbose\n");
207 (void) fprintf(stderr, "Default is to dump everything non-verbosely\n");
208 exit(1);
209}
210
cae5b340
AX
211static void
212dump_debug_buffer(void)
213{
214 if (dump_opt['G']) {
215 (void) printf("\n");
216 zfs_dbgmsg_print("zdb");
217 }
218}
219
9babb374
BB
220/*
221 * Called for usage errors that are discovered after a call to spa_open(),
222 * dmu_bonus_hold(), or pool_match(). abort() is called for other errors.
223 */
224
34dc7c2f
BB
225static void
226fatal(const char *fmt, ...)
227{
228 va_list ap;
229
230 va_start(ap, fmt);
231 (void) fprintf(stderr, "%s: ", cmdname);
232 (void) vfprintf(stderr, fmt, ap);
233 va_end(ap);
234 (void) fprintf(stderr, "\n");
235
cae5b340
AX
236 dump_debug_buffer();
237
9babb374 238 exit(1);
34dc7c2f
BB
239}
240
34dc7c2f
BB
241/* ARGSUSED */
242static void
243dump_packed_nvlist(objset_t *os, uint64_t object, void *data, size_t size)
244{
245 nvlist_t *nv;
246 size_t nvsize = *(uint64_t *)data;
247 char *packed = umem_alloc(nvsize, UMEM_NOFAIL);
248
9babb374 249 VERIFY(0 == dmu_read(os, object, 0, nvsize, packed, DMU_READ_PREFETCH));
34dc7c2f
BB
250
251 VERIFY(nvlist_unpack(packed, nvsize, &nv, 0) == 0);
252
253 umem_free(packed, nvsize);
254
255 dump_nvlist(nv, 8);
256
257 nvlist_free(nv);
258}
259
a08ee875
LG
260/* ARGSUSED */
261static void
262dump_history_offsets(objset_t *os, uint64_t object, void *data, size_t size)
263{
264 spa_history_phys_t *shp = data;
265
266 if (shp == NULL)
267 return;
268
269 (void) printf("\t\tpool_create_len = %llu\n",
270 (u_longlong_t)shp->sh_pool_create_len);
271 (void) printf("\t\tphys_max_off = %llu\n",
272 (u_longlong_t)shp->sh_phys_max_off);
273 (void) printf("\t\tbof = %llu\n",
274 (u_longlong_t)shp->sh_bof);
275 (void) printf("\t\teof = %llu\n",
276 (u_longlong_t)shp->sh_eof);
277 (void) printf("\t\trecords_lost = %llu\n",
278 (u_longlong_t)shp->sh_records_lost);
279}
280
428870ff
BB
281static void
282zdb_nicenum(uint64_t num, char *buf)
283{
284 if (dump_opt['P'])
285 (void) sprintf(buf, "%llu", (longlong_t)num);
286 else
287 nicenum(num, buf);
288}
289
ea04106b
AX
290const char histo_stars[] = "****************************************";
291const int histo_width = sizeof (histo_stars) - 1;
34dc7c2f
BB
292
293static void
ea04106b 294dump_histogram(const uint64_t *histo, int size, int offset)
34dc7c2f
BB
295{
296 int i;
ea04106b 297 int minidx = size - 1;
34dc7c2f
BB
298 int maxidx = 0;
299 uint64_t max = 0;
300
ea04106b 301 for (i = 0; i < size; i++) {
34dc7c2f
BB
302 if (histo[i] > max)
303 max = histo[i];
304 if (histo[i] > 0 && i > maxidx)
305 maxidx = i;
306 if (histo[i] > 0 && i < minidx)
307 minidx = i;
308 }
309
ea04106b
AX
310 if (max < histo_width)
311 max = histo_width;
34dc7c2f 312
ea04106b
AX
313 for (i = minidx; i <= maxidx; i++) {
314 (void) printf("\t\t\t%3u: %6llu %s\n",
315 i + offset, (u_longlong_t)histo[i],
316 &histo_stars[(max - histo[i]) * histo_width / max]);
317 }
34dc7c2f
BB
318}
319
320static void
321dump_zap_stats(objset_t *os, uint64_t object)
322{
323 int error;
324 zap_stats_t zs;
325
326 error = zap_get_stats(os, object, &zs);
327 if (error)
328 return;
329
330 if (zs.zs_ptrtbl_len == 0) {
331 ASSERT(zs.zs_num_blocks == 1);
332 (void) printf("\tmicrozap: %llu bytes, %llu entries\n",
333 (u_longlong_t)zs.zs_blocksize,
334 (u_longlong_t)zs.zs_num_entries);
335 return;
336 }
337
338 (void) printf("\tFat ZAP stats:\n");
339
340 (void) printf("\t\tPointer table:\n");
341 (void) printf("\t\t\t%llu elements\n",
342 (u_longlong_t)zs.zs_ptrtbl_len);
343 (void) printf("\t\t\tzt_blk: %llu\n",
344 (u_longlong_t)zs.zs_ptrtbl_zt_blk);
345 (void) printf("\t\t\tzt_numblks: %llu\n",
346 (u_longlong_t)zs.zs_ptrtbl_zt_numblks);
347 (void) printf("\t\t\tzt_shift: %llu\n",
348 (u_longlong_t)zs.zs_ptrtbl_zt_shift);
349 (void) printf("\t\t\tzt_blks_copied: %llu\n",
350 (u_longlong_t)zs.zs_ptrtbl_blks_copied);
351 (void) printf("\t\t\tzt_nextblk: %llu\n",
352 (u_longlong_t)zs.zs_ptrtbl_nextblk);
353
354 (void) printf("\t\tZAP entries: %llu\n",
355 (u_longlong_t)zs.zs_num_entries);
356 (void) printf("\t\tLeaf blocks: %llu\n",
357 (u_longlong_t)zs.zs_num_leafs);
358 (void) printf("\t\tTotal blocks: %llu\n",
359 (u_longlong_t)zs.zs_num_blocks);
360 (void) printf("\t\tzap_block_type: 0x%llx\n",
361 (u_longlong_t)zs.zs_block_type);
362 (void) printf("\t\tzap_magic: 0x%llx\n",
363 (u_longlong_t)zs.zs_magic);
364 (void) printf("\t\tzap_salt: 0x%llx\n",
365 (u_longlong_t)zs.zs_salt);
366
367 (void) printf("\t\tLeafs with 2^n pointers:\n");
ea04106b 368 dump_histogram(zs.zs_leafs_with_2n_pointers, ZAP_HISTOGRAM_SIZE, 0);
34dc7c2f
BB
369
370 (void) printf("\t\tBlocks with n*5 entries:\n");
ea04106b 371 dump_histogram(zs.zs_blocks_with_n5_entries, ZAP_HISTOGRAM_SIZE, 0);
34dc7c2f
BB
372
373 (void) printf("\t\tBlocks n/10 full:\n");
ea04106b 374 dump_histogram(zs.zs_blocks_n_tenths_full, ZAP_HISTOGRAM_SIZE, 0);
34dc7c2f
BB
375
376 (void) printf("\t\tEntries with n chunks:\n");
ea04106b 377 dump_histogram(zs.zs_entries_using_n_chunks, ZAP_HISTOGRAM_SIZE, 0);
34dc7c2f
BB
378
379 (void) printf("\t\tBuckets with n entries:\n");
ea04106b 380 dump_histogram(zs.zs_buckets_with_n_entries, ZAP_HISTOGRAM_SIZE, 0);
34dc7c2f
BB
381}
382
383/*ARGSUSED*/
384static void
385dump_none(objset_t *os, uint64_t object, void *data, size_t size)
386{
387}
388
428870ff
BB
389/*ARGSUSED*/
390static void
391dump_unknown(objset_t *os, uint64_t object, void *data, size_t size)
392{
393 (void) printf("\tUNKNOWN OBJECT TYPE\n");
394}
395
34dc7c2f
BB
396/*ARGSUSED*/
397void
398dump_uint8(objset_t *os, uint64_t object, void *data, size_t size)
399{
400}
401
402/*ARGSUSED*/
403static void
404dump_uint64(objset_t *os, uint64_t object, void *data, size_t size)
405{
406}
407
408/*ARGSUSED*/
409static void
410dump_zap(objset_t *os, uint64_t object, void *data, size_t size)
411{
412 zap_cursor_t zc;
413 zap_attribute_t attr;
414 void *prop;
415 int i;
416
417 dump_zap_stats(os, object);
418 (void) printf("\n");
419
420 for (zap_cursor_init(&zc, os, object);
421 zap_cursor_retrieve(&zc, &attr) == 0;
422 zap_cursor_advance(&zc)) {
423 (void) printf("\t\t%s = ", attr.za_name);
424 if (attr.za_num_integers == 0) {
425 (void) printf("\n");
426 continue;
427 }
428 prop = umem_zalloc(attr.za_num_integers *
429 attr.za_integer_length, UMEM_NOFAIL);
430 (void) zap_lookup(os, object, attr.za_name,
431 attr.za_integer_length, attr.za_num_integers, prop);
432 if (attr.za_integer_length == 1) {
433 (void) printf("%s", (char *)prop);
434 } else {
435 for (i = 0; i < attr.za_num_integers; i++) {
436 switch (attr.za_integer_length) {
437 case 2:
438 (void) printf("%u ",
439 ((uint16_t *)prop)[i]);
440 break;
441 case 4:
442 (void) printf("%u ",
443 ((uint32_t *)prop)[i]);
444 break;
445 case 8:
446 (void) printf("%lld ",
447 (u_longlong_t)((int64_t *)prop)[i]);
448 break;
449 }
450 }
451 }
452 (void) printf("\n");
453 umem_free(prop, attr.za_num_integers * attr.za_integer_length);
454 }
455 zap_cursor_fini(&zc);
456}
457
e10b0808
AX
458static void
459dump_bpobj(objset_t *os, uint64_t object, void *data, size_t size)
460{
461 bpobj_phys_t *bpop = data;
462 uint64_t i;
463 char bytes[32], comp[32], uncomp[32];
464
465 if (bpop == NULL)
466 return;
467
468 zdb_nicenum(bpop->bpo_bytes, bytes);
469 zdb_nicenum(bpop->bpo_comp, comp);
470 zdb_nicenum(bpop->bpo_uncomp, uncomp);
471
472 (void) printf("\t\tnum_blkptrs = %llu\n",
473 (u_longlong_t)bpop->bpo_num_blkptrs);
474 (void) printf("\t\tbytes = %s\n", bytes);
475 if (size >= BPOBJ_SIZE_V1) {
476 (void) printf("\t\tcomp = %s\n", comp);
477 (void) printf("\t\tuncomp = %s\n", uncomp);
478 }
479 if (size >= sizeof (*bpop)) {
480 (void) printf("\t\tsubobjs = %llu\n",
481 (u_longlong_t)bpop->bpo_subobjs);
482 (void) printf("\t\tnum_subobjs = %llu\n",
483 (u_longlong_t)bpop->bpo_num_subobjs);
484 }
485
486 if (dump_opt['d'] < 5)
487 return;
488
489 for (i = 0; i < bpop->bpo_num_blkptrs; i++) {
490 char blkbuf[BP_SPRINTF_LEN];
491 blkptr_t bp;
492
493 int err = dmu_read(os, object,
494 i * sizeof (bp), sizeof (bp), &bp, 0);
495 if (err != 0) {
496 (void) printf("got error %u from dmu_read\n", err);
497 break;
498 }
499 snprintf_blkptr_compact(blkbuf, sizeof (blkbuf), &bp);
500 (void) printf("\t%s\n", blkbuf);
501 }
502}
503
504/* ARGSUSED */
505static void
506dump_bpobj_subobjs(objset_t *os, uint64_t object, void *data, size_t size)
507{
508 dmu_object_info_t doi;
22929307 509 int64_t i;
e10b0808
AX
510
511 VERIFY0(dmu_object_info(os, object, &doi));
512 uint64_t *subobjs = kmem_alloc(doi.doi_max_offset, KM_SLEEP);
513
514 int err = dmu_read(os, object, 0, doi.doi_max_offset, subobjs, 0);
515 if (err != 0) {
516 (void) printf("got error %u from dmu_read\n", err);
517 kmem_free(subobjs, doi.doi_max_offset);
518 return;
519 }
520
521 int64_t last_nonzero = -1;
522 for (i = 0; i < doi.doi_max_offset / 8; i++) {
523 if (subobjs[i] != 0)
524 last_nonzero = i;
525 }
526
527 for (i = 0; i <= last_nonzero; i++) {
22929307 528 (void) printf("\t%llu\n", (u_longlong_t)subobjs[i]);
e10b0808
AX
529 }
530 kmem_free(subobjs, doi.doi_max_offset);
531}
532
428870ff
BB
533/*ARGSUSED*/
534static void
535dump_ddt_zap(objset_t *os, uint64_t object, void *data, size_t size)
536{
537 dump_zap_stats(os, object);
538 /* contents are printed elsewhere, properly decoded */
539}
540
541/*ARGSUSED*/
542static void
543dump_sa_attrs(objset_t *os, uint64_t object, void *data, size_t size)
544{
545 zap_cursor_t zc;
546 zap_attribute_t attr;
547
548 dump_zap_stats(os, object);
549 (void) printf("\n");
550
551 for (zap_cursor_init(&zc, os, object);
552 zap_cursor_retrieve(&zc, &attr) == 0;
553 zap_cursor_advance(&zc)) {
554 (void) printf("\t\t%s = ", attr.za_name);
555 if (attr.za_num_integers == 0) {
556 (void) printf("\n");
557 continue;
558 }
559 (void) printf(" %llx : [%d:%d:%d]\n",
560 (u_longlong_t)attr.za_first_integer,
561 (int)ATTR_LENGTH(attr.za_first_integer),
562 (int)ATTR_BSWAP(attr.za_first_integer),
563 (int)ATTR_NUM(attr.za_first_integer));
564 }
565 zap_cursor_fini(&zc);
566}
567
568/*ARGSUSED*/
569static void
570dump_sa_layouts(objset_t *os, uint64_t object, void *data, size_t size)
571{
572 zap_cursor_t zc;
573 zap_attribute_t attr;
574 uint16_t *layout_attrs;
575 int i;
576
577 dump_zap_stats(os, object);
578 (void) printf("\n");
579
580 for (zap_cursor_init(&zc, os, object);
581 zap_cursor_retrieve(&zc, &attr) == 0;
582 zap_cursor_advance(&zc)) {
583 (void) printf("\t\t%s = [", attr.za_name);
584 if (attr.za_num_integers == 0) {
585 (void) printf("\n");
586 continue;
587 }
588
589 VERIFY(attr.za_integer_length == 2);
590 layout_attrs = umem_zalloc(attr.za_num_integers *
591 attr.za_integer_length, UMEM_NOFAIL);
592
593 VERIFY(zap_lookup(os, object, attr.za_name,
594 attr.za_integer_length,
595 attr.za_num_integers, layout_attrs) == 0);
596
597 for (i = 0; i != attr.za_num_integers; i++)
598 (void) printf(" %d ", (int)layout_attrs[i]);
599 (void) printf("]\n");
600 umem_free(layout_attrs,
601 attr.za_num_integers * attr.za_integer_length);
602 }
603 zap_cursor_fini(&zc);
604}
605
34dc7c2f
BB
606/*ARGSUSED*/
607static void
608dump_zpldir(objset_t *os, uint64_t object, void *data, size_t size)
609{
610 zap_cursor_t zc;
611 zap_attribute_t attr;
612 const char *typenames[] = {
613 /* 0 */ "not specified",
614 /* 1 */ "FIFO",
615 /* 2 */ "Character Device",
616 /* 3 */ "3 (invalid)",
617 /* 4 */ "Directory",
618 /* 5 */ "5 (invalid)",
619 /* 6 */ "Block Device",
620 /* 7 */ "7 (invalid)",
621 /* 8 */ "Regular File",
622 /* 9 */ "9 (invalid)",
623 /* 10 */ "Symbolic Link",
624 /* 11 */ "11 (invalid)",
625 /* 12 */ "Socket",
626 /* 13 */ "Door",
627 /* 14 */ "Event Port",
628 /* 15 */ "15 (invalid)",
629 };
630
631 dump_zap_stats(os, object);
632 (void) printf("\n");
633
634 for (zap_cursor_init(&zc, os, object);
635 zap_cursor_retrieve(&zc, &attr) == 0;
636 zap_cursor_advance(&zc)) {
637 (void) printf("\t\t%s = %lld (type: %s)\n",
638 attr.za_name, ZFS_DIRENT_OBJ(attr.za_first_integer),
639 typenames[ZFS_DIRENT_TYPE(attr.za_first_integer)]);
640 }
641 zap_cursor_fini(&zc);
642}
643
ea04106b
AX
644int
645get_dtl_refcount(vdev_t *vd)
646{
647 int refcount = 0;
648 int c;
649
650 if (vd->vdev_ops->vdev_op_leaf) {
651 space_map_t *sm = vd->vdev_dtl_sm;
652
653 if (sm != NULL &&
654 sm->sm_dbuf->db_size == sizeof (space_map_phys_t))
655 return (1);
656 return (0);
657 }
658
659 for (c = 0; c < vd->vdev_children; c++)
660 refcount += get_dtl_refcount(vd->vdev_child[c]);
661 return (refcount);
662}
663
664int
665get_metaslab_refcount(vdev_t *vd)
666{
667 int refcount = 0;
668 int c, m;
669
670 if (vd->vdev_top == vd && !vd->vdev_removing) {
671 for (m = 0; m < vd->vdev_ms_count; m++) {
672 space_map_t *sm = vd->vdev_ms[m]->ms_sm;
673
674 if (sm != NULL &&
675 sm->sm_dbuf->db_size == sizeof (space_map_phys_t))
676 refcount++;
677 }
678 }
679 for (c = 0; c < vd->vdev_children; c++)
680 refcount += get_metaslab_refcount(vd->vdev_child[c]);
681
682 return (refcount);
683}
684
685static int
686verify_spacemap_refcounts(spa_t *spa)
687{
688 uint64_t expected_refcount = 0;
689 uint64_t actual_refcount;
690
691 (void) feature_get_refcount(spa,
692 &spa_feature_table[SPA_FEATURE_SPACEMAP_HISTOGRAM],
693 &expected_refcount);
694 actual_refcount = get_dtl_refcount(spa->spa_root_vdev);
695 actual_refcount += get_metaslab_refcount(spa->spa_root_vdev);
696
697 if (expected_refcount != actual_refcount) {
698 (void) printf("space map refcount mismatch: expected %lld != "
699 "actual %lld\n",
700 (longlong_t)expected_refcount,
701 (longlong_t)actual_refcount);
702 return (2);
703 }
704 return (0);
705}
706
34dc7c2f 707static void
ea04106b 708dump_spacemap(objset_t *os, space_map_t *sm)
34dc7c2f
BB
709{
710 uint64_t alloc, offset, entry;
34dc7c2f
BB
711 char *ddata[] = { "ALLOC", "FREE", "CONDENSE", "INVALID",
712 "INVALID", "INVALID", "INVALID", "INVALID" };
713
ea04106b 714 if (sm == NULL)
34dc7c2f
BB
715 return;
716
717 /*
718 * Print out the freelist entries in both encoded and decoded form.
719 */
720 alloc = 0;
ea04106b
AX
721 for (offset = 0; offset < space_map_length(sm);
722 offset += sizeof (entry)) {
723 uint8_t mapshift = sm->sm_shift;
724
725 VERIFY0(dmu_read(os, space_map_object(sm), offset,
9babb374 726 sizeof (entry), &entry, DMU_READ_PREFETCH));
34dc7c2f 727 if (SM_DEBUG_DECODE(entry)) {
ea04106b 728
428870ff 729 (void) printf("\t [%6llu] %s: txg %llu, pass %llu\n",
34dc7c2f
BB
730 (u_longlong_t)(offset / sizeof (entry)),
731 ddata[SM_DEBUG_ACTION_DECODE(entry)],
732 (u_longlong_t)SM_DEBUG_TXG_DECODE(entry),
733 (u_longlong_t)SM_DEBUG_SYNCPASS_DECODE(entry));
734 } else {
428870ff
BB
735 (void) printf("\t [%6llu] %c range:"
736 " %010llx-%010llx size: %06llx\n",
34dc7c2f
BB
737 (u_longlong_t)(offset / sizeof (entry)),
738 SM_TYPE_DECODE(entry) == SM_ALLOC ? 'A' : 'F',
739 (u_longlong_t)((SM_OFFSET_DECODE(entry) <<
ea04106b 740 mapshift) + sm->sm_start),
34dc7c2f 741 (u_longlong_t)((SM_OFFSET_DECODE(entry) <<
ea04106b
AX
742 mapshift) + sm->sm_start +
743 (SM_RUN_DECODE(entry) << mapshift)),
34dc7c2f
BB
744 (u_longlong_t)(SM_RUN_DECODE(entry) << mapshift));
745 if (SM_TYPE_DECODE(entry) == SM_ALLOC)
746 alloc += SM_RUN_DECODE(entry) << mapshift;
747 else
748 alloc -= SM_RUN_DECODE(entry) << mapshift;
749 }
750 }
ea04106b 751 if (alloc != space_map_allocated(sm)) {
34dc7c2f
BB
752 (void) printf("space_map_object alloc (%llu) INCONSISTENT "
753 "with space map summary (%llu)\n",
ea04106b 754 (u_longlong_t)space_map_allocated(sm), (u_longlong_t)alloc);
34dc7c2f
BB
755 }
756}
757
9babb374
BB
758static void
759dump_metaslab_stats(metaslab_t *msp)
760{
428870ff 761 char maxbuf[32];
ea04106b
AX
762 range_tree_t *rt = msp->ms_tree;
763 avl_tree_t *t = &msp->ms_size_tree;
764 int free_pct = range_tree_space(rt) * 100 / msp->ms_size;
9babb374 765
ea04106b 766 zdb_nicenum(metaslab_block_maxsize(msp), maxbuf);
9babb374 767
428870ff 768 (void) printf("\t %25s %10lu %7s %6s %4s %4d%%\n",
9babb374
BB
769 "segments", avl_numnodes(t), "maxsize", maxbuf,
770 "freepct", free_pct);
ea04106b
AX
771 (void) printf("\tIn-memory histogram:\n");
772 dump_histogram(rt->rt_histogram, RANGE_TREE_HISTOGRAM_SIZE, 0);
9babb374
BB
773}
774
34dc7c2f
BB
775static void
776dump_metaslab(metaslab_t *msp)
777{
34dc7c2f
BB
778 vdev_t *vd = msp->ms_group->mg_vd;
779 spa_t *spa = vd->vdev_spa;
ea04106b 780 space_map_t *sm = msp->ms_sm;
428870ff 781 char freebuf[32];
34dc7c2f 782
ea04106b 783 zdb_nicenum(msp->ms_size - space_map_allocated(sm), freebuf);
34dc7c2f 784
34dc7c2f 785 (void) printf(
428870ff 786 "\tmetaslab %6llu offset %12llx spacemap %6llu free %5s\n",
ea04106b
AX
787 (u_longlong_t)msp->ms_id, (u_longlong_t)msp->ms_start,
788 (u_longlong_t)space_map_object(sm), freebuf);
34dc7c2f 789
ea04106b 790 if (dump_opt['m'] > 2 && !dump_opt['L']) {
9babb374 791 mutex_enter(&msp->ms_lock);
ea04106b
AX
792 metaslab_load_wait(msp);
793 if (!msp->ms_loaded) {
794 VERIFY0(metaslab_load(msp));
795 range_tree_stat_verify(msp->ms_tree);
796 }
9babb374 797 dump_metaslab_stats(msp);
ea04106b 798 metaslab_unload(msp);
9babb374
BB
799 mutex_exit(&msp->ms_lock);
800 }
801
ea04106b
AX
802 if (dump_opt['m'] > 1 && sm != NULL &&
803 spa_feature_is_active(spa, SPA_FEATURE_SPACEMAP_HISTOGRAM)) {
804 /*
805 * The space map histogram represents free space in chunks
806 * of sm_shift (i.e. bucket 0 refers to 2^sm_shift).
807 */
808 (void) printf("\tOn-disk histogram:\t\tfragmentation %llu\n",
809 (u_longlong_t)msp->ms_fragmentation);
810 dump_histogram(sm->sm_phys->smp_histogram,
811 SPACE_MAP_HISTOGRAM_SIZE, sm->sm_shift);
812 }
813
814 if (dump_opt['d'] > 5 || dump_opt['m'] > 3) {
815 ASSERT(msp->ms_size == (1ULL << vd->vdev_ms_shift));
9babb374
BB
816
817 mutex_enter(&msp->ms_lock);
ea04106b 818 dump_spacemap(spa->spa_meta_objset, msp->ms_sm);
9babb374
BB
819 mutex_exit(&msp->ms_lock);
820 }
428870ff 821}
34dc7c2f 822
428870ff
BB
823static void
824print_vdev_metaslab_header(vdev_t *vd)
825{
826 (void) printf("\tvdev %10llu\n\t%-10s%5llu %-19s %-15s %-10s\n",
827 (u_longlong_t)vd->vdev_id,
828 "metaslabs", (u_longlong_t)vd->vdev_ms_count,
829 "offset", "spacemap", "free");
830 (void) printf("\t%15s %19s %15s %10s\n",
831 "---------------", "-------------------",
832 "---------------", "-------------");
34dc7c2f
BB
833}
834
ea04106b
AX
835static void
836dump_metaslab_groups(spa_t *spa)
837{
838 vdev_t *rvd = spa->spa_root_vdev;
839 metaslab_class_t *mc = spa_normal_class(spa);
840 uint64_t fragmentation;
841 int c;
842
843 metaslab_class_histogram_verify(mc);
844
845 for (c = 0; c < rvd->vdev_children; c++) {
846 vdev_t *tvd = rvd->vdev_child[c];
847 metaslab_group_t *mg = tvd->vdev_mg;
848
849 if (mg->mg_class != mc)
850 continue;
851
852 metaslab_group_histogram_verify(mg);
853 mg->mg_fragmentation = metaslab_group_fragmentation(mg);
854
855 (void) printf("\tvdev %10llu\t\tmetaslabs%5llu\t\t"
856 "fragmentation",
857 (u_longlong_t)tvd->vdev_id,
858 (u_longlong_t)tvd->vdev_ms_count);
859 if (mg->mg_fragmentation == ZFS_FRAG_INVALID) {
860 (void) printf("%3s\n", "-");
861 } else {
862 (void) printf("%3llu%%\n",
863 (u_longlong_t)mg->mg_fragmentation);
864 }
865 dump_histogram(mg->mg_histogram, RANGE_TREE_HISTOGRAM_SIZE, 0);
866 }
867
868 (void) printf("\tpool %s\tfragmentation", spa_name(spa));
869 fragmentation = metaslab_class_fragmentation(mc);
870 if (fragmentation == ZFS_FRAG_INVALID)
871 (void) printf("\t%3s\n", "-");
872 else
873 (void) printf("\t%3llu%%\n", (u_longlong_t)fragmentation);
874 dump_histogram(mc->mc_histogram, RANGE_TREE_HISTOGRAM_SIZE, 0);
875}
876
34dc7c2f
BB
877static void
878dump_metaslabs(spa_t *spa)
879{
428870ff
BB
880 vdev_t *vd, *rvd = spa->spa_root_vdev;
881 uint64_t m, c = 0, children = rvd->vdev_children;
34dc7c2f
BB
882
883 (void) printf("\nMetaslabs:\n");
884
428870ff
BB
885 if (!dump_opt['d'] && zopt_objects > 0) {
886 c = zopt_object[0];
887
888 if (c >= children)
889 (void) fatal("bad vdev id: %llu", (u_longlong_t)c);
34dc7c2f 890
428870ff
BB
891 if (zopt_objects > 1) {
892 vd = rvd->vdev_child[c];
893 print_vdev_metaslab_header(vd);
894
895 for (m = 1; m < zopt_objects; m++) {
896 if (zopt_object[m] < vd->vdev_ms_count)
897 dump_metaslab(
898 vd->vdev_ms[zopt_object[m]]);
899 else
900 (void) fprintf(stderr, "bad metaslab "
901 "number %llu\n",
902 (u_longlong_t)zopt_object[m]);
903 }
904 (void) printf("\n");
905 return;
906 }
907 children = c + 1;
908 }
909 for (; c < children; c++) {
910 vd = rvd->vdev_child[c];
911 print_vdev_metaslab_header(vd);
34dc7c2f 912
34dc7c2f
BB
913 for (m = 0; m < vd->vdev_ms_count; m++)
914 dump_metaslab(vd->vdev_ms[m]);
915 (void) printf("\n");
916 }
917}
918
428870ff
BB
919static void
920dump_dde(const ddt_t *ddt, const ddt_entry_t *dde, uint64_t index)
921{
922 const ddt_phys_t *ddp = dde->dde_phys;
923 const ddt_key_t *ddk = &dde->dde_key;
924 char *types[4] = { "ditto", "single", "double", "triple" };
925 char blkbuf[BP_SPRINTF_LEN];
926 blkptr_t blk;
d6320ddb 927 int p;
428870ff 928
d6320ddb 929 for (p = 0; p < DDT_PHYS_TYPES; p++, ddp++) {
428870ff
BB
930 if (ddp->ddp_phys_birth == 0)
931 continue;
932 ddt_bp_create(ddt->ddt_checksum, ddk, ddp, &blk);
ea04106b 933 snprintf_blkptr(blkbuf, sizeof (blkbuf), &blk);
428870ff
BB
934 (void) printf("index %llx refcnt %llu %s %s\n",
935 (u_longlong_t)index, (u_longlong_t)ddp->ddp_refcnt,
936 types[p], blkbuf);
937 }
938}
939
940static void
941dump_dedup_ratio(const ddt_stat_t *dds)
942{
943 double rL, rP, rD, D, dedup, compress, copies;
944
945 if (dds->dds_blocks == 0)
946 return;
947
948 rL = (double)dds->dds_ref_lsize;
949 rP = (double)dds->dds_ref_psize;
950 rD = (double)dds->dds_ref_dsize;
951 D = (double)dds->dds_dsize;
952
953 dedup = rD / D;
954 compress = rL / rP;
955 copies = rD / rP;
956
957 (void) printf("dedup = %.2f, compress = %.2f, copies = %.2f, "
958 "dedup * compress / copies = %.2f\n\n",
959 dedup, compress, copies, dedup * compress / copies);
960}
961
962static void
963dump_ddt(ddt_t *ddt, enum ddt_type type, enum ddt_class class)
964{
965 char name[DDT_NAMELEN];
966 ddt_entry_t dde;
967 uint64_t walk = 0;
968 dmu_object_info_t doi;
969 uint64_t count, dspace, mspace;
970 int error;
971
972 error = ddt_object_info(ddt, type, class, &doi);
973
974 if (error == ENOENT)
975 return;
976 ASSERT(error == 0);
977
e8fd45a0
BB
978 error = ddt_object_count(ddt, type, class, &count);
979 ASSERT(error == 0);
980 if (count == 0)
572e2857
BB
981 return;
982
428870ff
BB
983 dspace = doi.doi_physical_blocks_512 << 9;
984 mspace = doi.doi_fill_count * doi.doi_data_block_size;
985
428870ff
BB
986 ddt_object_name(ddt, type, class, name);
987
988 (void) printf("%s: %llu entries, size %llu on disk, %llu in core\n",
989 name,
990 (u_longlong_t)count,
991 (u_longlong_t)(dspace / count),
992 (u_longlong_t)(mspace / count));
993
994 if (dump_opt['D'] < 3)
995 return;
996
997 zpool_dump_ddt(NULL, &ddt->ddt_histogram[type][class]);
998
999 if (dump_opt['D'] < 4)
1000 return;
1001
1002 if (dump_opt['D'] < 5 && class == DDT_CLASS_UNIQUE)
1003 return;
1004
1005 (void) printf("%s contents:\n\n", name);
1006
1007 while ((error = ddt_object_walk(ddt, type, class, &walk, &dde)) == 0)
1008 dump_dde(ddt, &dde, walk);
1009
1010 ASSERT(error == ENOENT);
1011
1012 (void) printf("\n");
1013}
1014
1015static void
1016dump_all_ddts(spa_t *spa)
1017{
d6320ddb
BB
1018 ddt_histogram_t ddh_total;
1019 ddt_stat_t dds_total;
1020 enum zio_checksum c;
1021 enum ddt_type type;
1022 enum ddt_class class;
428870ff 1023
d6320ddb
BB
1024 bzero(&ddh_total, sizeof (ddt_histogram_t));
1025 bzero(&dds_total, sizeof (ddt_stat_t));
1026
1027 for (c = 0; c < ZIO_CHECKSUM_FUNCTIONS; c++) {
428870ff 1028 ddt_t *ddt = spa->spa_ddt[c];
d6320ddb
BB
1029 for (type = 0; type < DDT_TYPES; type++) {
1030 for (class = 0; class < DDT_CLASSES;
428870ff
BB
1031 class++) {
1032 dump_ddt(ddt, type, class);
1033 }
1034 }
1035 }
1036
1037 ddt_get_dedup_stats(spa, &dds_total);
1038
1039 if (dds_total.dds_blocks == 0) {
1040 (void) printf("All DDTs are empty\n");
1041 return;
1042 }
1043
1044 (void) printf("\n");
1045
1046 if (dump_opt['D'] > 1) {
1047 (void) printf("DDT histogram (aggregated over all DDTs):\n");
1048 ddt_get_dedup_histogram(spa, &ddh_total);
1049 zpool_dump_ddt(&dds_total, &ddh_total);
1050 }
1051
1052 dump_dedup_ratio(&dds_total);
1053}
1054
fb5f0bc8 1055static void
ea04106b 1056dump_dtl_seg(void *arg, uint64_t start, uint64_t size)
fb5f0bc8 1057{
ea04106b 1058 char *prefix = arg;
fb5f0bc8
BB
1059
1060 (void) printf("%s [%llu,%llu) length %llu\n",
1061 prefix,
1062 (u_longlong_t)start,
1063 (u_longlong_t)(start + size),
1064 (u_longlong_t)(size));
1065}
1066
34dc7c2f
BB
1067static void
1068dump_dtl(vdev_t *vd, int indent)
1069{
fb5f0bc8
BB
1070 spa_t *spa = vd->vdev_spa;
1071 boolean_t required;
1072 char *name[DTL_TYPES] = { "missing", "partial", "scrub", "outage" };
1073 char prefix[256];
d6320ddb 1074 int c, t;
fb5f0bc8 1075
428870ff 1076 spa_vdev_state_enter(spa, SCL_NONE);
fb5f0bc8
BB
1077 required = vdev_dtl_required(vd);
1078 (void) spa_vdev_state_exit(spa, NULL, 0);
34dc7c2f
BB
1079
1080 if (indent == 0)
1081 (void) printf("\nDirty time logs:\n\n");
1082
fb5f0bc8 1083 (void) printf("\t%*s%s [%s]\n", indent, "",
b128c09f 1084 vd->vdev_path ? vd->vdev_path :
fb5f0bc8
BB
1085 vd->vdev_parent ? vd->vdev_ops->vdev_op_type : spa_name(spa),
1086 required ? "DTL-required" : "DTL-expendable");
34dc7c2f 1087
d6320ddb 1088 for (t = 0; t < DTL_TYPES; t++) {
ea04106b
AX
1089 range_tree_t *rt = vd->vdev_dtl[t];
1090 if (range_tree_space(rt) == 0)
fb5f0bc8
BB
1091 continue;
1092 (void) snprintf(prefix, sizeof (prefix), "\t%*s%s",
1093 indent + 2, "", name[t]);
ea04106b
AX
1094 mutex_enter(rt->rt_lock);
1095 range_tree_walk(rt, dump_dtl_seg, prefix);
1096 mutex_exit(rt->rt_lock);
fb5f0bc8
BB
1097 if (dump_opt['d'] > 5 && vd->vdev_children == 0)
1098 dump_spacemap(spa->spa_meta_objset,
ea04106b 1099 vd->vdev_dtl_sm);
34dc7c2f
BB
1100 }
1101
d6320ddb 1102 for (c = 0; c < vd->vdev_children; c++)
34dc7c2f
BB
1103 dump_dtl(vd->vdev_child[c], indent + 4);
1104}
1105
428870ff
BB
1106static void
1107dump_history(spa_t *spa)
1108{
1109 nvlist_t **events = NULL;
e10b0808 1110 char *buf;
428870ff
BB
1111 uint64_t resid, len, off = 0;
1112 uint_t num = 0;
1113 int error;
1114 time_t tsec;
1115 struct tm t;
1116 char tbuf[30];
1117 char internalstr[MAXPATHLEN];
d6320ddb 1118 int i;
428870ff 1119
e10b0808
AX
1120 if ((buf = malloc(SPA_OLD_MAXBLOCKSIZE)) == NULL) {
1121 (void) fprintf(stderr, "%s: unable to allocate I/O buffer\n",
1122 __func__);
1123 return;
1124 }
1125
428870ff 1126 do {
e10b0808 1127 len = SPA_OLD_MAXBLOCKSIZE;
428870ff
BB
1128
1129 if ((error = spa_history_get(spa, &off, &len, buf)) != 0) {
1130 (void) fprintf(stderr, "Unable to read history: "
1131 "error %d\n", error);
e10b0808 1132 free(buf);
428870ff
BB
1133 return;
1134 }
1135
1136 if (zpool_history_unpack(buf, len, &resid, &events, &num) != 0)
1137 break;
1138
1139 off -= resid;
1140 } while (len != 0);
1141
1142 (void) printf("\nHistory:\n");
d6320ddb 1143 for (i = 0; i < num; i++) {
428870ff
BB
1144 uint64_t time, txg, ievent;
1145 char *cmd, *intstr;
a08ee875 1146 boolean_t printed = B_FALSE;
428870ff
BB
1147
1148 if (nvlist_lookup_uint64(events[i], ZPOOL_HIST_TIME,
1149 &time) != 0)
a08ee875 1150 goto next;
428870ff
BB
1151 if (nvlist_lookup_string(events[i], ZPOOL_HIST_CMD,
1152 &cmd) != 0) {
1153 if (nvlist_lookup_uint64(events[i],
1154 ZPOOL_HIST_INT_EVENT, &ievent) != 0)
a08ee875 1155 goto next;
428870ff
BB
1156 verify(nvlist_lookup_uint64(events[i],
1157 ZPOOL_HIST_TXG, &txg) == 0);
1158 verify(nvlist_lookup_string(events[i],
1159 ZPOOL_HIST_INT_STR, &intstr) == 0);
a08ee875
LG
1160 if (ievent >= ZFS_NUM_LEGACY_HISTORY_EVENTS)
1161 goto next;
428870ff
BB
1162
1163 (void) snprintf(internalstr,
1164 sizeof (internalstr),
1165 "[internal %s txg:%lld] %s",
b8864a23
BB
1166 zfs_history_event_names[ievent],
1167 (longlong_t)txg, intstr);
428870ff
BB
1168 cmd = internalstr;
1169 }
1170 tsec = time;
1171 (void) localtime_r(&tsec, &t);
1172 (void) strftime(tbuf, sizeof (tbuf), "%F.%T", &t);
1173 (void) printf("%s %s\n", tbuf, cmd);
a08ee875
LG
1174 printed = B_TRUE;
1175
1176next:
1177 if (dump_opt['h'] > 1) {
1178 if (!printed)
1179 (void) printf("unrecognized record:\n");
1180 dump_nvlist(events[i], 2);
1181 }
428870ff 1182 }
e10b0808 1183 free(buf);
428870ff
BB
1184}
1185
34dc7c2f
BB
1186/*ARGSUSED*/
1187static void
1188dump_dnode(objset_t *os, uint64_t object, void *data, size_t size)
1189{
1190}
1191
1192static uint64_t
ea04106b
AX
1193blkid2offset(const dnode_phys_t *dnp, const blkptr_t *bp,
1194 const zbookmark_phys_t *zb)
34dc7c2f 1195{
428870ff
BB
1196 if (dnp == NULL) {
1197 ASSERT(zb->zb_level < 0);
1198 if (zb->zb_object == 0)
1199 return (zb->zb_blkid);
1200 return (zb->zb_blkid * BP_GET_LSIZE(bp));
1201 }
1202
1203 ASSERT(zb->zb_level >= 0);
34dc7c2f 1204
428870ff
BB
1205 return ((zb->zb_blkid <<
1206 (zb->zb_level * (dnp->dn_indblkshift - SPA_BLKPTRSHIFT))) *
34dc7c2f
BB
1207 dnp->dn_datablkszsec << SPA_MINBLOCKSHIFT);
1208}
1209
1210static void
ea04106b 1211snprintf_blkptr_compact(char *blkbuf, size_t buflen, const blkptr_t *bp)
34dc7c2f 1212{
428870ff
BB
1213 const dva_t *dva = bp->blk_dva;
1214 int ndvas = dump_opt['d'] > 5 ? BP_GET_NDVAS(bp) : 1;
d6320ddb 1215 int i;
428870ff 1216
ea04106b
AX
1217 if (dump_opt['b'] >= 6) {
1218 snprintf_blkptr(blkbuf, buflen, bp);
1219 return;
1220 }
1221
1222 if (BP_IS_EMBEDDED(bp)) {
1223 (void) sprintf(blkbuf,
1224 "EMBEDDED et=%u %llxL/%llxP B=%llu",
1225 (int)BPE_GET_ETYPE(bp),
1226 (u_longlong_t)BPE_GET_LSIZE(bp),
1227 (u_longlong_t)BPE_GET_PSIZE(bp),
1228 (u_longlong_t)bp->blk_birth);
428870ff
BB
1229 return;
1230 }
34dc7c2f
BB
1231
1232 blkbuf[0] = '\0';
1233
d6320ddb 1234 for (i = 0; i < ndvas; i++)
ea04106b
AX
1235 (void) snprintf(blkbuf + strlen(blkbuf),
1236 buflen - strlen(blkbuf), "%llu:%llx:%llx ",
34dc7c2f
BB
1237 (u_longlong_t)DVA_GET_VDEV(&dva[i]),
1238 (u_longlong_t)DVA_GET_OFFSET(&dva[i]),
1239 (u_longlong_t)DVA_GET_ASIZE(&dva[i]));
1240
ea04106b
AX
1241 if (BP_IS_HOLE(bp)) {
1242 (void) snprintf(blkbuf + strlen(blkbuf),
1243 buflen - strlen(blkbuf),
1244 "%llxL B=%llu",
1245 (u_longlong_t)BP_GET_LSIZE(bp),
1246 (u_longlong_t)bp->blk_birth);
1247 } else {
1248 (void) snprintf(blkbuf + strlen(blkbuf),
1249 buflen - strlen(blkbuf),
1250 "%llxL/%llxP F=%llu B=%llu/%llu",
1251 (u_longlong_t)BP_GET_LSIZE(bp),
1252 (u_longlong_t)BP_GET_PSIZE(bp),
1253 (u_longlong_t)BP_GET_FILL(bp),
1254 (u_longlong_t)bp->blk_birth,
1255 (u_longlong_t)BP_PHYSICAL_BIRTH(bp));
1256 }
34dc7c2f
BB
1257}
1258
b128c09f 1259static void
ea04106b 1260print_indirect(blkptr_t *bp, const zbookmark_phys_t *zb,
b128c09f 1261 const dnode_phys_t *dnp)
34dc7c2f 1262{
b128c09f 1263 char blkbuf[BP_SPRINTF_LEN];
34dc7c2f
BB
1264 int l;
1265
ea04106b
AX
1266 if (!BP_IS_EMBEDDED(bp)) {
1267 ASSERT3U(BP_GET_TYPE(bp), ==, dnp->dn_type);
1268 ASSERT3U(BP_GET_LEVEL(bp), ==, zb->zb_level);
1269 }
34dc7c2f 1270
428870ff 1271 (void) printf("%16llx ", (u_longlong_t)blkid2offset(dnp, bp, zb));
34dc7c2f
BB
1272
1273 ASSERT(zb->zb_level >= 0);
1274
1275 for (l = dnp->dn_nlevels - 1; l >= -1; l--) {
1276 if (l == zb->zb_level) {
b128c09f 1277 (void) printf("L%llx", (u_longlong_t)zb->zb_level);
34dc7c2f 1278 } else {
b128c09f 1279 (void) printf(" ");
34dc7c2f
BB
1280 }
1281 }
1282
ea04106b 1283 snprintf_blkptr_compact(blkbuf, sizeof (blkbuf), bp);
b128c09f
BB
1284 (void) printf("%s\n", blkbuf);
1285}
1286
b128c09f
BB
1287static int
1288visit_indirect(spa_t *spa, const dnode_phys_t *dnp,
ea04106b 1289 blkptr_t *bp, const zbookmark_phys_t *zb)
b128c09f 1290{
428870ff 1291 int err = 0;
b128c09f
BB
1292
1293 if (bp->blk_birth == 0)
1294 return (0);
1295
1296 print_indirect(bp, zb, dnp);
1297
ea04106b 1298 if (BP_GET_LEVEL(bp) > 0 && !BP_IS_HOLE(bp)) {
e10b0808 1299 arc_flags_t flags = ARC_FLAG_WAIT;
b128c09f
BB
1300 int i;
1301 blkptr_t *cbp;
1302 int epb = BP_GET_LSIZE(bp) >> SPA_BLKPTRSHIFT;
1303 arc_buf_t *buf;
1304 uint64_t fill = 0;
1305
c06d4368 1306 err = arc_read(NULL, spa, bp, arc_getbuf_func, &buf,
b128c09f
BB
1307 ZIO_PRIORITY_ASYNC_READ, ZIO_FLAG_CANFAIL, &flags, zb);
1308 if (err)
1309 return (err);
428870ff 1310 ASSERT(buf->b_data);
b128c09f
BB
1311
1312 /* recursively visit blocks below this */
1313 cbp = buf->b_data;
1314 for (i = 0; i < epb; i++, cbp++) {
ea04106b 1315 zbookmark_phys_t czb;
b128c09f
BB
1316
1317 SET_BOOKMARK(&czb, zb->zb_objset, zb->zb_object,
1318 zb->zb_level - 1,
1319 zb->zb_blkid * epb + i);
1320 err = visit_indirect(spa, dnp, cbp, &czb);
1321 if (err)
1322 break;
ea04106b 1323 fill += BP_GET_FILL(cbp);
b128c09f 1324 }
fb5f0bc8 1325 if (!err)
ea04106b 1326 ASSERT3U(fill, ==, BP_GET_FILL(bp));
cae5b340 1327 arc_buf_destroy(buf, &buf);
34dc7c2f
BB
1328 }
1329
b128c09f 1330 return (err);
34dc7c2f
BB
1331}
1332
1333/*ARGSUSED*/
1334static void
b128c09f 1335dump_indirect(dnode_t *dn)
34dc7c2f 1336{
b128c09f
BB
1337 dnode_phys_t *dnp = dn->dn_phys;
1338 int j;
ea04106b 1339 zbookmark_phys_t czb;
34dc7c2f
BB
1340
1341 (void) printf("Indirect blocks:\n");
1342
428870ff 1343 SET_BOOKMARK(&czb, dmu_objset_id(dn->dn_objset),
b128c09f
BB
1344 dn->dn_object, dnp->dn_nlevels - 1, 0);
1345 for (j = 0; j < dnp->dn_nblkptr; j++) {
1346 czb.zb_blkid = j;
428870ff 1347 (void) visit_indirect(dmu_objset_spa(dn->dn_objset), dnp,
b128c09f
BB
1348 &dnp->dn_blkptr[j], &czb);
1349 }
34dc7c2f
BB
1350
1351 (void) printf("\n");
34dc7c2f
BB
1352}
1353
1354/*ARGSUSED*/
1355static void
1356dump_dsl_dir(objset_t *os, uint64_t object, void *data, size_t size)
1357{
1358 dsl_dir_phys_t *dd = data;
1359 time_t crtime;
428870ff 1360 char nice[32];
34dc7c2f
BB
1361
1362 if (dd == NULL)
1363 return;
1364
1365 ASSERT3U(size, >=, sizeof (dsl_dir_phys_t));
1366
1367 crtime = dd->dd_creation_time;
34dc7c2f
BB
1368 (void) printf("\t\tcreation_time = %s", ctime(&crtime));
1369 (void) printf("\t\thead_dataset_obj = %llu\n",
1370 (u_longlong_t)dd->dd_head_dataset_obj);
1371 (void) printf("\t\tparent_dir_obj = %llu\n",
1372 (u_longlong_t)dd->dd_parent_obj);
1373 (void) printf("\t\torigin_obj = %llu\n",
1374 (u_longlong_t)dd->dd_origin_obj);
1375 (void) printf("\t\tchild_dir_zapobj = %llu\n",
1376 (u_longlong_t)dd->dd_child_dir_zapobj);
428870ff 1377 zdb_nicenum(dd->dd_used_bytes, nice);
b128c09f 1378 (void) printf("\t\tused_bytes = %s\n", nice);
428870ff 1379 zdb_nicenum(dd->dd_compressed_bytes, nice);
b128c09f 1380 (void) printf("\t\tcompressed_bytes = %s\n", nice);
428870ff 1381 zdb_nicenum(dd->dd_uncompressed_bytes, nice);
b128c09f 1382 (void) printf("\t\tuncompressed_bytes = %s\n", nice);
428870ff 1383 zdb_nicenum(dd->dd_quota, nice);
b128c09f 1384 (void) printf("\t\tquota = %s\n", nice);
428870ff 1385 zdb_nicenum(dd->dd_reserved, nice);
b128c09f 1386 (void) printf("\t\treserved = %s\n", nice);
34dc7c2f
BB
1387 (void) printf("\t\tprops_zapobj = %llu\n",
1388 (u_longlong_t)dd->dd_props_zapobj);
1389 (void) printf("\t\tdeleg_zapobj = %llu\n",
1390 (u_longlong_t)dd->dd_deleg_zapobj);
b128c09f
BB
1391 (void) printf("\t\tflags = %llx\n",
1392 (u_longlong_t)dd->dd_flags);
1393
1394#define DO(which) \
428870ff 1395 zdb_nicenum(dd->dd_used_breakdown[DD_USED_ ## which], nice); \
b128c09f
BB
1396 (void) printf("\t\tused_breakdown[" #which "] = %s\n", nice)
1397 DO(HEAD);
1398 DO(SNAP);
1399 DO(CHILD);
1400 DO(CHILD_RSRV);
1401 DO(REFRSRV);
1402#undef DO
34dc7c2f
BB
1403}
1404
1405/*ARGSUSED*/
1406static void
1407dump_dsl_dataset(objset_t *os, uint64_t object, void *data, size_t size)
1408{
1409 dsl_dataset_phys_t *ds = data;
1410 time_t crtime;
428870ff 1411 char used[32], compressed[32], uncompressed[32], unique[32];
34dc7c2f
BB
1412 char blkbuf[BP_SPRINTF_LEN];
1413
1414 if (ds == NULL)
1415 return;
1416
1417 ASSERT(size == sizeof (*ds));
1418 crtime = ds->ds_creation_time;
9ae529ec 1419 zdb_nicenum(ds->ds_referenced_bytes, used);
428870ff
BB
1420 zdb_nicenum(ds->ds_compressed_bytes, compressed);
1421 zdb_nicenum(ds->ds_uncompressed_bytes, uncompressed);
1422 zdb_nicenum(ds->ds_unique_bytes, unique);
ea04106b 1423 snprintf_blkptr(blkbuf, sizeof (blkbuf), &ds->ds_bp);
34dc7c2f 1424
b128c09f 1425 (void) printf("\t\tdir_obj = %llu\n",
34dc7c2f
BB
1426 (u_longlong_t)ds->ds_dir_obj);
1427 (void) printf("\t\tprev_snap_obj = %llu\n",
1428 (u_longlong_t)ds->ds_prev_snap_obj);
1429 (void) printf("\t\tprev_snap_txg = %llu\n",
1430 (u_longlong_t)ds->ds_prev_snap_txg);
1431 (void) printf("\t\tnext_snap_obj = %llu\n",
1432 (u_longlong_t)ds->ds_next_snap_obj);
1433 (void) printf("\t\tsnapnames_zapobj = %llu\n",
1434 (u_longlong_t)ds->ds_snapnames_zapobj);
1435 (void) printf("\t\tnum_children = %llu\n",
1436 (u_longlong_t)ds->ds_num_children);
45d1cae3
BB
1437 (void) printf("\t\tuserrefs_obj = %llu\n",
1438 (u_longlong_t)ds->ds_userrefs_obj);
34dc7c2f
BB
1439 (void) printf("\t\tcreation_time = %s", ctime(&crtime));
1440 (void) printf("\t\tcreation_txg = %llu\n",
1441 (u_longlong_t)ds->ds_creation_txg);
1442 (void) printf("\t\tdeadlist_obj = %llu\n",
1443 (u_longlong_t)ds->ds_deadlist_obj);
1444 (void) printf("\t\tused_bytes = %s\n", used);
1445 (void) printf("\t\tcompressed_bytes = %s\n", compressed);
1446 (void) printf("\t\tuncompressed_bytes = %s\n", uncompressed);
1447 (void) printf("\t\tunique = %s\n", unique);
1448 (void) printf("\t\tfsid_guid = %llu\n",
1449 (u_longlong_t)ds->ds_fsid_guid);
1450 (void) printf("\t\tguid = %llu\n",
1451 (u_longlong_t)ds->ds_guid);
1452 (void) printf("\t\tflags = %llx\n",
1453 (u_longlong_t)ds->ds_flags);
b128c09f
BB
1454 (void) printf("\t\tnext_clones_obj = %llu\n",
1455 (u_longlong_t)ds->ds_next_clones_obj);
1456 (void) printf("\t\tprops_obj = %llu\n",
1457 (u_longlong_t)ds->ds_props_obj);
34dc7c2f
BB
1458 (void) printf("\t\tbp = %s\n", blkbuf);
1459}
1460
9ae529ec
CS
1461/* ARGSUSED */
1462static int
1463dump_bptree_cb(void *arg, const blkptr_t *bp, dmu_tx_t *tx)
1464{
1465 char blkbuf[BP_SPRINTF_LEN];
1466
1467 if (bp->blk_birth != 0) {
ea04106b 1468 snprintf_blkptr(blkbuf, sizeof (blkbuf), bp);
9ae529ec
CS
1469 (void) printf("\t%s\n", blkbuf);
1470 }
1471 return (0);
1472}
1473
1474static void
1475dump_bptree(objset_t *os, uint64_t obj, char *name)
1476{
1477 char bytes[32];
1478 bptree_phys_t *bt;
1479 dmu_buf_t *db;
1480
1481 if (dump_opt['d'] < 3)
1482 return;
1483
1484 VERIFY3U(0, ==, dmu_bonus_hold(os, obj, FTAG, &db));
1485 bt = db->db_data;
1486 zdb_nicenum(bt->bt_bytes, bytes);
1487 (void) printf("\n %s: %llu datasets, %s\n",
1488 name, (unsigned long long)(bt->bt_end - bt->bt_begin), bytes);
1489 dmu_buf_rele(db, FTAG);
1490
1491 if (dump_opt['d'] < 5)
1492 return;
1493
1494 (void) printf("\n");
1495
1496 (void) bptree_iterate(os, obj, B_FALSE, dump_bptree_cb, NULL, NULL);
1497}
1498
428870ff
BB
1499/* ARGSUSED */
1500static int
1501dump_bpobj_cb(void *arg, const blkptr_t *bp, dmu_tx_t *tx)
1502{
1503 char blkbuf[BP_SPRINTF_LEN];
1504
1505 ASSERT(bp->blk_birth != 0);
ea04106b 1506 snprintf_blkptr_compact(blkbuf, sizeof (blkbuf), bp);
428870ff
BB
1507 (void) printf("\t%s\n", blkbuf);
1508 return (0);
1509}
1510
34dc7c2f 1511static void
e10b0808 1512dump_full_bpobj(bpobj_t *bpo, char *name, int indent)
34dc7c2f 1513{
428870ff
BB
1514 char bytes[32];
1515 char comp[32];
1516 char uncomp[32];
a08ee875 1517 uint64_t i;
34dc7c2f
BB
1518
1519 if (dump_opt['d'] < 3)
1520 return;
1521
428870ff 1522 zdb_nicenum(bpo->bpo_phys->bpo_bytes, bytes);
a08ee875 1523 if (bpo->bpo_havesubobj && bpo->bpo_phys->bpo_subobjs != 0) {
428870ff
BB
1524 zdb_nicenum(bpo->bpo_phys->bpo_comp, comp);
1525 zdb_nicenum(bpo->bpo_phys->bpo_uncomp, uncomp);
a08ee875 1526 (void) printf(" %*s: object %llu, %llu local blkptrs, "
e10b0808 1527 "%llu subobjs in object, %llu, %s (%s/%s comp)\n",
a08ee875
LG
1528 indent * 8, name,
1529 (u_longlong_t)bpo->bpo_object,
1530 (u_longlong_t)bpo->bpo_phys->bpo_num_blkptrs,
428870ff 1531 (u_longlong_t)bpo->bpo_phys->bpo_num_subobjs,
e10b0808 1532 (u_longlong_t)bpo->bpo_phys->bpo_subobjs,
34dc7c2f 1533 bytes, comp, uncomp);
a08ee875
LG
1534
1535 for (i = 0; i < bpo->bpo_phys->bpo_num_subobjs; i++) {
1536 uint64_t subobj;
1537 bpobj_t subbpo;
1538 int error;
1539 VERIFY0(dmu_read(bpo->bpo_os,
1540 bpo->bpo_phys->bpo_subobjs,
1541 i * sizeof (subobj), sizeof (subobj), &subobj, 0));
1542 error = bpobj_open(&subbpo, bpo->bpo_os, subobj);
1543 if (error != 0) {
1544 (void) printf("ERROR %u while trying to open "
1545 "subobj id %llu\n",
1546 error, (u_longlong_t)subobj);
1547 continue;
1548 }
e10b0808 1549 dump_full_bpobj(&subbpo, "subobj", indent + 1);
cae5b340 1550 bpobj_close(&subbpo);
a08ee875 1551 }
34dc7c2f 1552 } else {
a08ee875
LG
1553 (void) printf(" %*s: object %llu, %llu blkptrs, %s\n",
1554 indent * 8, name,
1555 (u_longlong_t)bpo->bpo_object,
1556 (u_longlong_t)bpo->bpo_phys->bpo_num_blkptrs,
1557 bytes);
34dc7c2f
BB
1558 }
1559
428870ff 1560 if (dump_opt['d'] < 5)
34dc7c2f 1561 return;
34dc7c2f 1562
34dc7c2f 1563
a08ee875
LG
1564 if (indent == 0) {
1565 (void) bpobj_iterate_nofree(bpo, dump_bpobj_cb, NULL, NULL);
1566 (void) printf("\n");
1567 }
428870ff 1568}
34dc7c2f 1569
428870ff
BB
1570static void
1571dump_deadlist(dsl_deadlist_t *dl)
1572{
1573 dsl_deadlist_entry_t *dle;
a08ee875 1574 uint64_t unused;
428870ff
BB
1575 char bytes[32];
1576 char comp[32];
1577 char uncomp[32];
1578
1579 if (dump_opt['d'] < 3)
1580 return;
34dc7c2f 1581
ea04106b 1582 if (dl->dl_oldfmt) {
e10b0808 1583 dump_full_bpobj(&dl->dl_bpobj, "old-format deadlist", 0);
ea04106b
AX
1584 return;
1585 }
1586
428870ff
BB
1587 zdb_nicenum(dl->dl_phys->dl_used, bytes);
1588 zdb_nicenum(dl->dl_phys->dl_comp, comp);
1589 zdb_nicenum(dl->dl_phys->dl_uncomp, uncomp);
1590 (void) printf("\n Deadlist: %s (%s/%s comp)\n",
1591 bytes, comp, uncomp);
1592
1593 if (dump_opt['d'] < 4)
1594 return;
1595
1596 (void) printf("\n");
1597
a08ee875
LG
1598 /* force the tree to be loaded */
1599 dsl_deadlist_space_range(dl, 0, UINT64_MAX, &unused, &unused, &unused);
1600
428870ff
BB
1601 for (dle = avl_first(&dl->dl_tree); dle;
1602 dle = AVL_NEXT(&dl->dl_tree, dle)) {
a08ee875
LG
1603 if (dump_opt['d'] >= 5) {
1604 char buf[128];
1605 (void) snprintf(buf, sizeof (buf),
1606 "mintxg %llu -> obj %llu",
1607 (longlong_t)dle->dle_mintxg,
1608 (longlong_t)dle->dle_bpobj.bpo_object);
1609
e10b0808 1610 dump_full_bpobj(&dle->dle_bpobj, buf, 0);
a08ee875
LG
1611 } else {
1612 (void) printf("mintxg %llu -> obj %llu\n",
1613 (longlong_t)dle->dle_mintxg,
1614 (longlong_t)dle->dle_bpobj.bpo_object);
428870ff 1615
a08ee875 1616 }
428870ff 1617 }
34dc7c2f
BB
1618}
1619
1620static avl_tree_t idx_tree;
1621static avl_tree_t domain_tree;
1622static boolean_t fuid_table_loaded;
cae5b340
AX
1623static objset_t *sa_os = NULL;
1624static sa_attr_type_t *sa_attr_table = NULL;
1625
1626static int
1627open_objset(const char *path, dmu_objset_type_t type, void *tag, objset_t **osp)
1628{
1629 int err;
1630 uint64_t sa_attrs = 0;
1631 uint64_t version = 0;
1632
1633 VERIFY3P(sa_os, ==, NULL);
1634 err = dmu_objset_own(path, type, B_TRUE, tag, osp);
1635 if (err != 0) {
1636 (void) fprintf(stderr, "failed to own dataset '%s': %s\n", path,
1637 strerror(err));
1638 return (err);
1639 }
1640
1641 if (dmu_objset_type(*osp) == DMU_OST_ZFS) {
1642 (void) zap_lookup(*osp, MASTER_NODE_OBJ, ZPL_VERSION_STR,
1643 8, 1, &version);
1644 if (version >= ZPL_VERSION_SA) {
1645 (void) zap_lookup(*osp, MASTER_NODE_OBJ, ZFS_SA_ATTRS,
1646 8, 1, &sa_attrs);
1647 }
1648 err = sa_setup(*osp, sa_attrs, zfs_attr_table, ZPL_END,
1649 &sa_attr_table);
1650 if (err != 0) {
1651 (void) fprintf(stderr, "sa_setup failed: %s\n",
1652 strerror(err));
1653 dmu_objset_disown(*osp, tag);
1654 *osp = NULL;
1655 }
1656 }
1657 sa_os = *osp;
1658
1659 return (0);
1660}
1661
1662static void
1663close_objset(objset_t *os, void *tag)
1664{
1665 VERIFY3P(os, ==, sa_os);
1666 if (os->os_sa != NULL)
1667 sa_tear_down(os);
1668 dmu_objset_disown(os, tag);
1669 sa_attr_table = NULL;
1670 sa_os = NULL;
1671}
34dc7c2f
BB
1672
1673static void
0bc8fd78 1674fuid_table_destroy(void)
34dc7c2f
BB
1675{
1676 if (fuid_table_loaded) {
1677 zfs_fuid_table_destroy(&idx_tree, &domain_tree);
1678 fuid_table_loaded = B_FALSE;
1679 }
1680}
1681
1682/*
1683 * print uid or gid information.
1684 * For normal POSIX id just the id is printed in decimal format.
1685 * For CIFS files with FUID the fuid is printed in hex followed by
a08ee875 1686 * the domain-rid string.
34dc7c2f
BB
1687 */
1688static void
1689print_idstr(uint64_t id, const char *id_type)
1690{
1691 if (FUID_INDEX(id)) {
1692 char *domain;
1693
1694 domain = zfs_fuid_idx_domain(&idx_tree, FUID_INDEX(id));
1695 (void) printf("\t%s %llx [%s-%d]\n", id_type,
1696 (u_longlong_t)id, domain, (int)FUID_RID(id));
1697 } else {
1698 (void) printf("\t%s %llu\n", id_type, (u_longlong_t)id);
1699 }
1700
1701}
1702
1703static void
428870ff 1704dump_uidgid(objset_t *os, uint64_t uid, uint64_t gid)
34dc7c2f
BB
1705{
1706 uint32_t uid_idx, gid_idx;
1707
428870ff
BB
1708 uid_idx = FUID_INDEX(uid);
1709 gid_idx = FUID_INDEX(gid);
34dc7c2f
BB
1710
1711 /* Load domain table, if not already loaded */
1712 if (!fuid_table_loaded && (uid_idx || gid_idx)) {
1713 uint64_t fuid_obj;
1714
1715 /* first find the fuid object. It lives in the master node */
1716 VERIFY(zap_lookup(os, MASTER_NODE_OBJ, ZFS_FUID_TABLES,
1717 8, 1, &fuid_obj) == 0);
9babb374 1718 zfs_fuid_avl_tree_create(&idx_tree, &domain_tree);
34dc7c2f
BB
1719 (void) zfs_fuid_table_load(os, fuid_obj,
1720 &idx_tree, &domain_tree);
1721 fuid_table_loaded = B_TRUE;
1722 }
1723
428870ff
BB
1724 print_idstr(uid, "uid");
1725 print_idstr(gid, "gid");
34dc7c2f
BB
1726}
1727
c06d4368
AX
1728static void
1729dump_znode_sa_xattr(sa_handle_t *hdl)
1730{
1731 nvlist_t *sa_xattr;
1732 nvpair_t *elem = NULL;
1733 int sa_xattr_size = 0;
1734 int sa_xattr_entries = 0;
1735 int error;
1736 char *sa_xattr_packed;
1737
1738 error = sa_size(hdl, sa_attr_table[ZPL_DXATTR], &sa_xattr_size);
1739 if (error || sa_xattr_size == 0)
1740 return;
1741
1742 sa_xattr_packed = malloc(sa_xattr_size);
1743 if (sa_xattr_packed == NULL)
1744 return;
1745
1746 error = sa_lookup(hdl, sa_attr_table[ZPL_DXATTR],
1747 sa_xattr_packed, sa_xattr_size);
1748 if (error) {
1749 free(sa_xattr_packed);
1750 return;
1751 }
1752
1753 error = nvlist_unpack(sa_xattr_packed, sa_xattr_size, &sa_xattr, 0);
1754 if (error) {
1755 free(sa_xattr_packed);
1756 return;
1757 }
1758
1759 while ((elem = nvlist_next_nvpair(sa_xattr, elem)) != NULL)
1760 sa_xattr_entries++;
1761
1762 (void) printf("\tSA xattrs: %d bytes, %d entries\n\n",
1763 sa_xattr_size, sa_xattr_entries);
1764 while ((elem = nvlist_next_nvpair(sa_xattr, elem)) != NULL) {
1765 uchar_t *value;
1766 uint_t cnt, idx;
1767
1768 (void) printf("\t\t%s = ", nvpair_name(elem));
1769 nvpair_value_byte_array(elem, &value, &cnt);
a08ee875 1770 for (idx = 0; idx < cnt; ++idx) {
c06d4368
AX
1771 if (isprint(value[idx]))
1772 (void) putchar(value[idx]);
1773 else
1774 (void) printf("\\%3.3o", value[idx]);
1775 }
1776 (void) putchar('\n');
1777 }
1778
1779 nvlist_free(sa_xattr);
1780 free(sa_xattr_packed);
1781}
1782
34dc7c2f
BB
1783/*ARGSUSED*/
1784static void
1785dump_znode(objset_t *os, uint64_t object, void *data, size_t size)
1786{
34dc7c2f 1787 char path[MAXPATHLEN * 2]; /* allow for xattr and failure prefix */
428870ff
BB
1788 sa_handle_t *hdl;
1789 uint64_t xattr, rdev, gen;
1790 uint64_t uid, gid, mode, fsize, parent, links;
1791 uint64_t pflags;
1792 uint64_t acctm[2], modtm[2], chgtm[2], crtm[2];
1793 time_t z_crtime, z_atime, z_mtime, z_ctime;
1794 sa_bulk_attr_t bulk[12];
1795 int idx = 0;
34dc7c2f
BB
1796 int error;
1797
cae5b340 1798 VERIFY3P(os, ==, sa_os);
428870ff
BB
1799 if (sa_handle_get(os, object, NULL, SA_HDL_PRIVATE, &hdl)) {
1800 (void) printf("Failed to get handle for SA znode\n");
1801 return;
1802 }
1803
1804 SA_ADD_BULK_ATTR(bulk, idx, sa_attr_table[ZPL_UID], NULL, &uid, 8);
1805 SA_ADD_BULK_ATTR(bulk, idx, sa_attr_table[ZPL_GID], NULL, &gid, 8);
1806 SA_ADD_BULK_ATTR(bulk, idx, sa_attr_table[ZPL_LINKS], NULL,
1807 &links, 8);
1808 SA_ADD_BULK_ATTR(bulk, idx, sa_attr_table[ZPL_GEN], NULL, &gen, 8);
1809 SA_ADD_BULK_ATTR(bulk, idx, sa_attr_table[ZPL_MODE], NULL,
1810 &mode, 8);
1811 SA_ADD_BULK_ATTR(bulk, idx, sa_attr_table[ZPL_PARENT],
1812 NULL, &parent, 8);
1813 SA_ADD_BULK_ATTR(bulk, idx, sa_attr_table[ZPL_SIZE], NULL,
1814 &fsize, 8);
1815 SA_ADD_BULK_ATTR(bulk, idx, sa_attr_table[ZPL_ATIME], NULL,
1816 acctm, 16);
1817 SA_ADD_BULK_ATTR(bulk, idx, sa_attr_table[ZPL_MTIME], NULL,
1818 modtm, 16);
1819 SA_ADD_BULK_ATTR(bulk, idx, sa_attr_table[ZPL_CRTIME], NULL,
1820 crtm, 16);
1821 SA_ADD_BULK_ATTR(bulk, idx, sa_attr_table[ZPL_CTIME], NULL,
1822 chgtm, 16);
1823 SA_ADD_BULK_ATTR(bulk, idx, sa_attr_table[ZPL_FLAGS], NULL,
1824 &pflags, 8);
1825
1826 if (sa_bulk_lookup(hdl, bulk, idx)) {
1827 (void) sa_handle_destroy(hdl);
1828 return;
1829 }
34dc7c2f 1830
428870ff
BB
1831 z_crtime = (time_t)crtm[0];
1832 z_atime = (time_t)acctm[0];
1833 z_mtime = (time_t)modtm[0];
1834 z_ctime = (time_t)chgtm[0];
34dc7c2f 1835
cae5b340
AX
1836 if (dump_opt['d'] > 4) {
1837 error = zfs_obj_to_path(os, object, path, sizeof (path));
1838 if (error != 0) {
1839 (void) snprintf(path, sizeof (path),
1840 "\?\?\?<object#%llu>", (u_longlong_t)object);
1841 }
1842 (void) printf("\tpath %s\n", path);
1843 }
428870ff 1844 dump_uidgid(os, uid, gid);
34dc7c2f
BB
1845 (void) printf("\tatime %s", ctime(&z_atime));
1846 (void) printf("\tmtime %s", ctime(&z_mtime));
1847 (void) printf("\tctime %s", ctime(&z_ctime));
1848 (void) printf("\tcrtime %s", ctime(&z_crtime));
428870ff
BB
1849 (void) printf("\tgen %llu\n", (u_longlong_t)gen);
1850 (void) printf("\tmode %llo\n", (u_longlong_t)mode);
1851 (void) printf("\tsize %llu\n", (u_longlong_t)fsize);
1852 (void) printf("\tparent %llu\n", (u_longlong_t)parent);
1853 (void) printf("\tlinks %llu\n", (u_longlong_t)links);
1854 (void) printf("\tpflags %llx\n", (u_longlong_t)pflags);
1855 if (sa_lookup(hdl, sa_attr_table[ZPL_XATTR], &xattr,
1856 sizeof (uint64_t)) == 0)
1857 (void) printf("\txattr %llu\n", (u_longlong_t)xattr);
1858 if (sa_lookup(hdl, sa_attr_table[ZPL_RDEV], &rdev,
1859 sizeof (uint64_t)) == 0)
1860 (void) printf("\trdev 0x%016llx\n", (u_longlong_t)rdev);
c06d4368 1861 dump_znode_sa_xattr(hdl);
428870ff 1862 sa_handle_destroy(hdl);
34dc7c2f
BB
1863}
1864
1865/*ARGSUSED*/
1866static void
1867dump_acl(objset_t *os, uint64_t object, void *data, size_t size)
1868{
1869}
1870
1871/*ARGSUSED*/
1872static void
1873dump_dmu_objset(objset_t *os, uint64_t object, void *data, size_t size)
1874{
1875}
1876
428870ff 1877static object_viewer_t *object_viewer[DMU_OT_NUMTYPES + 1] = {
34dc7c2f
BB
1878 dump_none, /* unallocated */
1879 dump_zap, /* object directory */
1880 dump_uint64, /* object array */
1881 dump_none, /* packed nvlist */
1882 dump_packed_nvlist, /* packed nvlist size */
e10b0808
AX
1883 dump_none, /* bpobj */
1884 dump_bpobj, /* bpobj header */
34dc7c2f
BB
1885 dump_none, /* SPA space map header */
1886 dump_none, /* SPA space map */
1887 dump_none, /* ZIL intent log */
1888 dump_dnode, /* DMU dnode */
1889 dump_dmu_objset, /* DMU objset */
1890 dump_dsl_dir, /* DSL directory */
1891 dump_zap, /* DSL directory child map */
1892 dump_zap, /* DSL dataset snap map */
1893 dump_zap, /* DSL props */
1894 dump_dsl_dataset, /* DSL dataset */
1895 dump_znode, /* ZFS znode */
1896 dump_acl, /* ZFS V0 ACL */
1897 dump_uint8, /* ZFS plain file */
1898 dump_zpldir, /* ZFS directory */
1899 dump_zap, /* ZFS master node */
1900 dump_zap, /* ZFS delete queue */
1901 dump_uint8, /* zvol object */
1902 dump_zap, /* zvol prop */
1903 dump_uint8, /* other uint8[] */
1904 dump_uint64, /* other uint64[] */
1905 dump_zap, /* other ZAP */
1906 dump_zap, /* persistent error log */
1907 dump_uint8, /* SPA history */
a08ee875 1908 dump_history_offsets, /* SPA history offsets */
34dc7c2f
BB
1909 dump_zap, /* Pool properties */
1910 dump_zap, /* DSL permissions */
1911 dump_acl, /* ZFS ACL */
1912 dump_uint8, /* ZFS SYSACL */
1913 dump_none, /* FUID nvlist */
1914 dump_packed_nvlist, /* FUID nvlist size */
b128c09f
BB
1915 dump_zap, /* DSL dataset next clones */
1916 dump_zap, /* DSL scrub queue */
9babb374
BB
1917 dump_zap, /* ZFS user/group used */
1918 dump_zap, /* ZFS user/group quota */
45d1cae3 1919 dump_zap, /* snapshot refcount tags */
428870ff
BB
1920 dump_ddt_zap, /* DDT ZAP object */
1921 dump_zap, /* DDT statistics */
1922 dump_znode, /* SA object */
1923 dump_zap, /* SA Master Node */
1924 dump_sa_attrs, /* SA attribute registration */
1925 dump_sa_layouts, /* SA attribute layouts */
1926 dump_zap, /* DSL scrub translations */
1927 dump_none, /* fake dedup BP */
1928 dump_zap, /* deadlist */
1929 dump_none, /* deadlist hdr */
1930 dump_zap, /* dsl clones */
e10b0808 1931 dump_bpobj_subobjs, /* bpobj subobjs */
428870ff 1932 dump_unknown, /* Unknown type, must be last */
34dc7c2f
BB
1933};
1934
1935static void
cae5b340
AX
1936dump_object(objset_t *os, uint64_t object, int verbosity, int *print_header,
1937 uint64_t *dnode_slots_used)
34dc7c2f
BB
1938{
1939 dmu_buf_t *db = NULL;
1940 dmu_object_info_t doi;
1941 dnode_t *dn;
1942 void *bonus = NULL;
1943 size_t bsize = 0;
cae5b340 1944 char iblk[32], dblk[32], lsize[32], asize[32], fill[32], dnsize[32];
428870ff 1945 char bonus_size[32];
34dc7c2f
BB
1946 char aux[50];
1947 int error;
1948
1949 if (*print_header) {
cae5b340
AX
1950 (void) printf("\n%10s %3s %5s %5s %5s %6s %5s %6s %s\n",
1951 "Object", "lvl", "iblk", "dblk", "dsize", "dnsize",
1952 "lsize", "%full", "type");
34dc7c2f
BB
1953 *print_header = 0;
1954 }
1955
1956 if (object == 0) {
572e2857 1957 dn = DMU_META_DNODE(os);
34dc7c2f
BB
1958 } else {
1959 error = dmu_bonus_hold(os, object, FTAG, &db);
1960 if (error)
1961 fatal("dmu_bonus_hold(%llu) failed, errno %u",
1962 object, error);
1963 bonus = db->db_data;
1964 bsize = db->db_size;
572e2857 1965 dn = DB_DNODE((dmu_buf_impl_t *)db);
34dc7c2f
BB
1966 }
1967 dmu_object_info_from_dnode(dn, &doi);
1968
cae5b340
AX
1969 if (dnode_slots_used)
1970 *dnode_slots_used = doi.doi_dnodesize / DNODE_MIN_SIZE;
1971
428870ff
BB
1972 zdb_nicenum(doi.doi_metadata_block_size, iblk);
1973 zdb_nicenum(doi.doi_data_block_size, dblk);
1974 zdb_nicenum(doi.doi_max_offset, lsize);
1975 zdb_nicenum(doi.doi_physical_blocks_512 << 9, asize);
1976 zdb_nicenum(doi.doi_bonus_size, bonus_size);
cae5b340 1977 zdb_nicenum(doi.doi_dnodesize, dnsize);
428870ff
BB
1978 (void) sprintf(fill, "%6.2f", 100.0 * doi.doi_fill_count *
1979 doi.doi_data_block_size / (object == 0 ? DNODES_PER_BLOCK : 1) /
1980 doi.doi_max_offset);
34dc7c2f
BB
1981
1982 aux[0] = '\0';
1983
1984 if (doi.doi_checksum != ZIO_CHECKSUM_INHERIT || verbosity >= 6) {
1985 (void) snprintf(aux + strlen(aux), sizeof (aux), " (K=%s)",
428870ff 1986 ZDB_CHECKSUM_NAME(doi.doi_checksum));
34dc7c2f
BB
1987 }
1988
1989 if (doi.doi_compress != ZIO_COMPRESS_INHERIT || verbosity >= 6) {
1990 (void) snprintf(aux + strlen(aux), sizeof (aux), " (Z=%s)",
428870ff 1991 ZDB_COMPRESS_NAME(doi.doi_compress));
34dc7c2f
BB
1992 }
1993
cae5b340 1994 (void) printf("%10lld %3u %5s %5s %5s %6s %5s %6s %s%s\n",
428870ff 1995 (u_longlong_t)object, doi.doi_indirection, iblk, dblk,
cae5b340 1996 asize, dnsize, lsize, fill, zdb_ot_name(doi.doi_type), aux);
34dc7c2f
BB
1997
1998 if (doi.doi_bonus_type != DMU_OT_NONE && verbosity > 3) {
cae5b340
AX
1999 (void) printf("%10s %3s %5s %5s %5s %5s %5s %6s %s\n",
2000 "", "", "", "", "", "", bonus_size, "bonus",
87dac73d 2001 zdb_ot_name(doi.doi_bonus_type));
34dc7c2f
BB
2002 }
2003
2004 if (verbosity >= 4) {
cae5b340 2005 (void) printf("\tdnode flags: %s%s%s%s\n",
9babb374
BB
2006 (dn->dn_phys->dn_flags & DNODE_FLAG_USED_BYTES) ?
2007 "USED_BYTES " : "",
2008 (dn->dn_phys->dn_flags & DNODE_FLAG_USERUSED_ACCOUNTED) ?
428870ff 2009 "USERUSED_ACCOUNTED " : "",
cae5b340
AX
2010 (dn->dn_phys->dn_flags & DNODE_FLAG_USEROBJUSED_ACCOUNTED) ?
2011 "USEROBJUSED_ACCOUNTED " : "",
428870ff
BB
2012 (dn->dn_phys->dn_flags & DNODE_FLAG_SPILL_BLKPTR) ?
2013 "SPILL_BLKPTR" : "");
9babb374
BB
2014 (void) printf("\tdnode maxblkid: %llu\n",
2015 (longlong_t)dn->dn_phys->dn_maxblkid);
2016
428870ff
BB
2017 object_viewer[ZDB_OT_TYPE(doi.doi_bonus_type)](os, object,
2018 bonus, bsize);
2019 object_viewer[ZDB_OT_TYPE(doi.doi_type)](os, object, NULL, 0);
34dc7c2f
BB
2020 *print_header = 1;
2021 }
2022
2023 if (verbosity >= 5)
b128c09f 2024 dump_indirect(dn);
34dc7c2f
BB
2025
2026 if (verbosity >= 5) {
2027 /*
2028 * Report the list of segments that comprise the object.
2029 */
2030 uint64_t start = 0;
2031 uint64_t end;
2032 uint64_t blkfill = 1;
2033 int minlvl = 1;
2034
2035 if (dn->dn_type == DMU_OT_DNODE) {
2036 minlvl = 0;
2037 blkfill = DNODES_PER_BLOCK;
2038 }
2039
2040 for (;;) {
428870ff 2041 char segsize[32];
b128c09f
BB
2042 error = dnode_next_offset(dn,
2043 0, &start, minlvl, blkfill, 0);
34dc7c2f
BB
2044 if (error)
2045 break;
2046 end = start;
b128c09f
BB
2047 error = dnode_next_offset(dn,
2048 DNODE_FIND_HOLE, &end, minlvl, blkfill, 0);
428870ff 2049 zdb_nicenum(end - start, segsize);
34dc7c2f
BB
2050 (void) printf("\t\tsegment [%016llx, %016llx)"
2051 " size %5s\n", (u_longlong_t)start,
2052 (u_longlong_t)end, segsize);
2053 if (error)
2054 break;
2055 start = end;
2056 }
2057 }
2058
2059 if (db != NULL)
2060 dmu_buf_rele(db, FTAG);
2061}
2062
2063static char *objset_types[DMU_OST_NUMTYPES] = {
2064 "NONE", "META", "ZPL", "ZVOL", "OTHER", "ANY" };
2065
2066static void
2067dump_dir(objset_t *os)
2068{
2069 dmu_objset_stats_t dds;
2070 uint64_t object, object_count;
2071 uint64_t refdbytes, usedobjs, scratch;
428870ff 2072 char numbuf[32];
9babb374 2073 char blkbuf[BP_SPRINTF_LEN + 20];
cae5b340 2074 char osname[ZFS_MAX_DATASET_NAME_LEN];
34dc7c2f
BB
2075 char *type = "UNKNOWN";
2076 int verbosity = dump_opt['d'];
2077 int print_header = 1;
2078 int i, error;
cae5b340
AX
2079 uint64_t total_slots_used = 0;
2080 uint64_t max_slot_used = 0;
2081 uint64_t dnode_slots;
34dc7c2f 2082
a08ee875 2083 dsl_pool_config_enter(dmu_objset_pool(os), FTAG);
34dc7c2f 2084 dmu_objset_fast_stat(os, &dds);
a08ee875 2085 dsl_pool_config_exit(dmu_objset_pool(os), FTAG);
34dc7c2f
BB
2086
2087 if (dds.dds_type < DMU_OST_NUMTYPES)
2088 type = objset_types[dds.dds_type];
2089
2090 if (dds.dds_type == DMU_OST_META) {
2091 dds.dds_creation_txg = TXG_INITIAL;
ea04106b 2092 usedobjs = BP_GET_FILL(os->os_rootbp);
e10b0808
AX
2093 refdbytes = dsl_dir_phys(os->os_spa->spa_dsl_pool->dp_mos_dir)->
2094 dd_used_bytes;
34dc7c2f
BB
2095 } else {
2096 dmu_objset_space(os, &refdbytes, &scratch, &usedobjs, &scratch);
2097 }
2098
ea04106b 2099 ASSERT3U(usedobjs, ==, BP_GET_FILL(os->os_rootbp));
34dc7c2f 2100
428870ff 2101 zdb_nicenum(refdbytes, numbuf);
34dc7c2f
BB
2102
2103 if (verbosity >= 4) {
ea04106b
AX
2104 (void) snprintf(blkbuf, sizeof (blkbuf), ", rootbp ");
2105 (void) snprintf_blkptr(blkbuf + strlen(blkbuf),
2106 sizeof (blkbuf) - strlen(blkbuf), os->os_rootbp);
34dc7c2f
BB
2107 } else {
2108 blkbuf[0] = '\0';
2109 }
2110
2111 dmu_objset_name(os, osname);
2112
2113 (void) printf("Dataset %s [%s], ID %llu, cr_txg %llu, "
2114 "%s, %llu objects%s\n",
2115 osname, type, (u_longlong_t)dmu_objset_id(os),
2116 (u_longlong_t)dds.dds_creation_txg,
2117 numbuf, (u_longlong_t)usedobjs, blkbuf);
2118
34dc7c2f
BB
2119 if (zopt_objects != 0) {
2120 for (i = 0; i < zopt_objects; i++)
2121 dump_object(os, zopt_object[i], verbosity,
cae5b340 2122 &print_header, NULL);
34dc7c2f
BB
2123 (void) printf("\n");
2124 return;
2125 }
2126
428870ff
BB
2127 if (dump_opt['i'] != 0 || verbosity >= 2)
2128 dump_intent_log(dmu_objset_zil(os));
2129
2130 if (dmu_objset_ds(os) != NULL)
2131 dump_deadlist(&dmu_objset_ds(os)->ds_deadlist);
2132
2133 if (verbosity < 2)
2134 return;
2135
ea04106b 2136 if (BP_IS_HOLE(os->os_rootbp))
428870ff
BB
2137 return;
2138
cae5b340 2139 dump_object(os, 0, verbosity, &print_header, NULL);
9babb374 2140 object_count = 0;
572e2857
BB
2141 if (DMU_USERUSED_DNODE(os) != NULL &&
2142 DMU_USERUSED_DNODE(os)->dn_type != 0) {
cae5b340
AX
2143 dump_object(os, DMU_USERUSED_OBJECT, verbosity, &print_header,
2144 NULL);
2145 dump_object(os, DMU_GROUPUSED_OBJECT, verbosity, &print_header,
2146 NULL);
9babb374 2147 }
34dc7c2f
BB
2148
2149 object = 0;
2150 while ((error = dmu_object_next(os, &object, B_FALSE, 0)) == 0) {
cae5b340 2151 dump_object(os, object, verbosity, &print_header, &dnode_slots);
34dc7c2f 2152 object_count++;
cae5b340
AX
2153 total_slots_used += dnode_slots;
2154 max_slot_used = object + dnode_slots - 1;
34dc7c2f
BB
2155 }
2156
2157 ASSERT3U(object_count, ==, usedobjs);
2158
2159 (void) printf("\n");
2160
cae5b340
AX
2161 (void) printf(" Dnode slots:\n");
2162 (void) printf("\tTotal used: %10llu\n",
2163 (u_longlong_t)total_slots_used);
2164 (void) printf("\tMax used: %10llu\n",
2165 (u_longlong_t)max_slot_used);
2166 (void) printf("\tPercent empty: %10lf\n",
2167 (double)(max_slot_used - total_slots_used)*100 /
2168 (double)max_slot_used);
2169
2170 (void) printf("\n");
2171
9babb374
BB
2172 if (error != ESRCH) {
2173 (void) fprintf(stderr, "dmu_object_next() = %d\n", error);
2174 abort();
2175 }
34dc7c2f
BB
2176}
2177
2178static void
428870ff 2179dump_uberblock(uberblock_t *ub, const char *header, const char *footer)
34dc7c2f
BB
2180{
2181 time_t timestamp = ub->ub_timestamp;
2182
0e5b68e0 2183 (void) printf("%s", header ? header : "");
34dc7c2f
BB
2184 (void) printf("\tmagic = %016llx\n", (u_longlong_t)ub->ub_magic);
2185 (void) printf("\tversion = %llu\n", (u_longlong_t)ub->ub_version);
2186 (void) printf("\ttxg = %llu\n", (u_longlong_t)ub->ub_txg);
2187 (void) printf("\tguid_sum = %llu\n", (u_longlong_t)ub->ub_guid_sum);
2188 (void) printf("\ttimestamp = %llu UTC = %s",
2189 (u_longlong_t)ub->ub_timestamp, asctime(localtime(&timestamp)));
cae5b340
AX
2190
2191 (void) printf("\tmmp_magic = %016llx\n",
2192 (u_longlong_t)ub->ub_mmp_magic);
2193 if (ub->ub_mmp_magic == MMP_MAGIC)
2194 (void) printf("\tmmp_delay = %0llu\n",
2195 (u_longlong_t)ub->ub_mmp_delay);
2196
2197 if (dump_opt['u'] >= 4) {
34dc7c2f 2198 char blkbuf[BP_SPRINTF_LEN];
ea04106b 2199 snprintf_blkptr(blkbuf, sizeof (blkbuf), &ub->ub_rootbp);
34dc7c2f
BB
2200 (void) printf("\trootbp = %s\n", blkbuf);
2201 }
0e5b68e0 2202 (void) printf("%s", footer ? footer : "");
34dc7c2f
BB
2203}
2204
2205static void
428870ff 2206dump_config(spa_t *spa)
34dc7c2f 2207{
428870ff
BB
2208 dmu_buf_t *db;
2209 size_t nvsize = 0;
2210 int error = 0;
34dc7c2f 2211
34dc7c2f 2212
428870ff
BB
2213 error = dmu_bonus_hold(spa->spa_meta_objset,
2214 spa->spa_config_object, FTAG, &db);
2215
2216 if (error == 0) {
2217 nvsize = *(uint64_t *)db->db_data;
2218 dmu_buf_rele(db, FTAG);
2219
2220 (void) printf("\nMOS Configuration:\n");
2221 dump_packed_nvlist(spa->spa_meta_objset,
2222 spa->spa_config_object, (void *)&nvsize, 1);
2223 } else {
2224 (void) fprintf(stderr, "dmu_bonus_hold(%llu) failed, errno %d",
2225 (u_longlong_t)spa->spa_config_object, error);
2226 }
2227}
2228
2229static void
b128c09f
BB
2230dump_cachefile(const char *cachefile)
2231{
2232 int fd;
2233 struct stat64 statbuf;
2234 char *buf;
2235 nvlist_t *config;
2236
2237 if ((fd = open64(cachefile, O_RDONLY)) < 0) {
2238 (void) printf("cannot open '%s': %s\n", cachefile,
2239 strerror(errno));
2240 exit(1);
2241 }
2242
2243 if (fstat64(fd, &statbuf) != 0) {
2244 (void) printf("failed to stat '%s': %s\n", cachefile,
2245 strerror(errno));
2246 exit(1);
2247 }
2248
2249 if ((buf = malloc(statbuf.st_size)) == NULL) {
2250 (void) fprintf(stderr, "failed to allocate %llu bytes\n",
2251 (u_longlong_t)statbuf.st_size);
2252 exit(1);
2253 }
2254
2255 if (read(fd, buf, statbuf.st_size) != statbuf.st_size) {
2256 (void) fprintf(stderr, "failed to read %llu bytes\n",
2257 (u_longlong_t)statbuf.st_size);
2258 exit(1);
2259 }
2260
2261 (void) close(fd);
2262
2263 if (nvlist_unpack(buf, statbuf.st_size, &config, 0) != 0) {
2264 (void) fprintf(stderr, "failed to unpack nvlist\n");
2265 exit(1);
2266 }
2267
2268 free(buf);
2269
2270 dump_nvlist(config, 0);
2271
2272 nvlist_free(config);
2273}
2274
cae5b340
AX
2275/*
2276 * ZFS label nvlist stats
2277 */
2278typedef struct zdb_nvl_stats {
2279 int zns_list_count;
2280 int zns_leaf_count;
2281 size_t zns_leaf_largest;
2282 size_t zns_leaf_total;
2283 nvlist_t *zns_string;
2284 nvlist_t *zns_uint64;
2285 nvlist_t *zns_boolean;
2286} zdb_nvl_stats_t;
2287
2288static void
2289collect_nvlist_stats(nvlist_t *nvl, zdb_nvl_stats_t *stats)
2290{
2291 nvlist_t *list, **array;
2292 nvpair_t *nvp = NULL;
2293 char *name;
2294 uint_t i, items;
2295
2296 stats->zns_list_count++;
2297
2298 while ((nvp = nvlist_next_nvpair(nvl, nvp)) != NULL) {
2299 name = nvpair_name(nvp);
2300
2301 switch (nvpair_type(nvp)) {
2302 case DATA_TYPE_STRING:
2303 fnvlist_add_string(stats->zns_string, name,
2304 fnvpair_value_string(nvp));
2305 break;
2306 case DATA_TYPE_UINT64:
2307 fnvlist_add_uint64(stats->zns_uint64, name,
2308 fnvpair_value_uint64(nvp));
2309 break;
2310 case DATA_TYPE_BOOLEAN:
2311 fnvlist_add_boolean(stats->zns_boolean, name);
2312 break;
2313 case DATA_TYPE_NVLIST:
2314 if (nvpair_value_nvlist(nvp, &list) == 0)
2315 collect_nvlist_stats(list, stats);
2316 break;
2317 case DATA_TYPE_NVLIST_ARRAY:
2318 if (nvpair_value_nvlist_array(nvp, &array, &items) != 0)
2319 break;
2320
2321 for (i = 0; i < items; i++) {
2322 collect_nvlist_stats(array[i], stats);
2323
2324 /* collect stats on leaf vdev */
2325 if (strcmp(name, "children") == 0) {
2326 size_t size;
2327
2328 (void) nvlist_size(array[i], &size,
2329 NV_ENCODE_XDR);
2330 stats->zns_leaf_total += size;
2331 if (size > stats->zns_leaf_largest)
2332 stats->zns_leaf_largest = size;
2333 stats->zns_leaf_count++;
2334 }
2335 }
2336 break;
2337 default:
2338 (void) printf("skip type %d!\n", (int)nvpair_type(nvp));
2339 }
2340 }
2341}
2342
2343static void
2344dump_nvlist_stats(nvlist_t *nvl, size_t cap)
2345{
2346 zdb_nvl_stats_t stats = { 0 };
2347 size_t size, sum = 0, total;
2348 size_t noise;
2349
2350 /* requires nvlist with non-unique names for stat collection */
2351 VERIFY0(nvlist_alloc(&stats.zns_string, 0, 0));
2352 VERIFY0(nvlist_alloc(&stats.zns_uint64, 0, 0));
2353 VERIFY0(nvlist_alloc(&stats.zns_boolean, 0, 0));
2354 VERIFY0(nvlist_size(stats.zns_boolean, &noise, NV_ENCODE_XDR));
2355
2356 (void) printf("\n\nZFS Label NVList Config Stats:\n");
2357
2358 VERIFY0(nvlist_size(nvl, &total, NV_ENCODE_XDR));
2359 (void) printf(" %d bytes used, %d bytes free (using %4.1f%%)\n\n",
2360 (int)total, (int)(cap - total), 100.0 * total / cap);
2361
2362 collect_nvlist_stats(nvl, &stats);
2363
2364 VERIFY0(nvlist_size(stats.zns_uint64, &size, NV_ENCODE_XDR));
2365 size -= noise;
2366 sum += size;
2367 (void) printf("%12s %4d %6d bytes (%5.2f%%)\n", "integers:",
2368 (int)fnvlist_num_pairs(stats.zns_uint64),
2369 (int)size, 100.0 * size / total);
2370
2371 VERIFY0(nvlist_size(stats.zns_string, &size, NV_ENCODE_XDR));
2372 size -= noise;
2373 sum += size;
2374 (void) printf("%12s %4d %6d bytes (%5.2f%%)\n", "strings:",
2375 (int)fnvlist_num_pairs(stats.zns_string),
2376 (int)size, 100.0 * size / total);
2377
2378 VERIFY0(nvlist_size(stats.zns_boolean, &size, NV_ENCODE_XDR));
2379 size -= noise;
2380 sum += size;
2381 (void) printf("%12s %4d %6d bytes (%5.2f%%)\n", "booleans:",
2382 (int)fnvlist_num_pairs(stats.zns_boolean),
2383 (int)size, 100.0 * size / total);
2384
2385 size = total - sum; /* treat remainder as nvlist overhead */
2386 (void) printf("%12s %4d %6d bytes (%5.2f%%)\n\n", "nvlists:",
2387 stats.zns_list_count, (int)size, 100.0 * size / total);
2388
2389 if (stats.zns_leaf_count > 0) {
2390 size_t average = stats.zns_leaf_total / stats.zns_leaf_count;
2391
2392 (void) printf("%12s %4d %6d bytes average\n", "leaf vdevs:",
2393 stats.zns_leaf_count, (int)average);
2394 (void) printf("%24d bytes largest\n",
2395 (int)stats.zns_leaf_largest);
2396
2397 if (dump_opt['l'] >= 3 && average > 0)
2398 (void) printf(" space for %d additional leaf vdevs\n",
2399 (int)((cap - total) / average));
2400 }
2401 (void) printf("\n");
2402
2403 nvlist_free(stats.zns_string);
2404 nvlist_free(stats.zns_uint64);
2405 nvlist_free(stats.zns_boolean);
2406}
2407
2408typedef struct cksum_record {
2409 zio_cksum_t cksum;
2410 boolean_t labels[VDEV_LABELS];
2411 avl_node_t link;
2412} cksum_record_t;
2413
2414static int
2415cksum_record_compare(const void *x1, const void *x2)
2416{
2417 const cksum_record_t *l = (cksum_record_t *)x1;
2418 const cksum_record_t *r = (cksum_record_t *)x2;
2419 int arraysize = ARRAY_SIZE(l->cksum.zc_word);
2420 int difference;
2421
2422 for (int i = 0; i < arraysize; i++) {
2423 difference = AVL_CMP(l->cksum.zc_word[i], r->cksum.zc_word[i]);
2424 if (difference)
2425 break;
2426 }
2427
2428 return (difference);
2429}
2430
2431static cksum_record_t *
2432cksum_record_alloc(zio_cksum_t *cksum, int l)
2433{
2434 cksum_record_t *rec;
2435
2436 rec = umem_zalloc(sizeof (*rec), UMEM_NOFAIL);
2437 rec->cksum = *cksum;
2438 rec->labels[l] = B_TRUE;
2439
2440 return (rec);
2441}
2442
2443static cksum_record_t *
2444cksum_record_lookup(avl_tree_t *tree, zio_cksum_t *cksum)
2445{
2446 cksum_record_t lookup = { .cksum = *cksum };
2447 avl_index_t where;
2448
2449 return (avl_find(tree, &lookup, &where));
2450}
2451
2452static cksum_record_t *
2453cksum_record_insert(avl_tree_t *tree, zio_cksum_t *cksum, int l)
2454{
2455 cksum_record_t *rec;
2456
2457 rec = cksum_record_lookup(tree, cksum);
2458 if (rec) {
2459 rec->labels[l] = B_TRUE;
2460 } else {
2461 rec = cksum_record_alloc(cksum, l);
2462 avl_add(tree, rec);
2463 }
2464
2465 return (rec);
2466}
2467
2468static int
2469first_label(cksum_record_t *rec)
2470{
2471 for (int i = 0; i < VDEV_LABELS; i++)
2472 if (rec->labels[i])
2473 return (i);
2474
2475 return (-1);
2476}
2477
2478static void
2479print_label_numbers(char *prefix, cksum_record_t *rec)
2480{
2481 printf("%s", prefix);
2482 for (int i = 0; i < VDEV_LABELS; i++)
2483 if (rec->labels[i] == B_TRUE)
2484 printf("%d ", i);
2485 printf("\n");
2486}
2487
2488#define MAX_UBERBLOCK_COUNT (VDEV_UBERBLOCK_RING >> UBERBLOCK_SHIFT)
2489
2490typedef struct label {
2491 vdev_label_t label;
2492 nvlist_t *config_nv;
2493 cksum_record_t *config;
2494 cksum_record_t *uberblocks[MAX_UBERBLOCK_COUNT];
2495 boolean_t header_printed;
2496 boolean_t read_failed;
2497} label_t;
2498
2499static void
2500print_label_header(label_t *label, int l)
2501{
2502
2503 if (dump_opt['q'])
2504 return;
2505
2506 if (label->header_printed == B_TRUE)
2507 return;
2508
2509 (void) printf("------------------------------------\n");
2510 (void) printf("LABEL %d\n", l);
2511 (void) printf("------------------------------------\n");
2512
2513 label->header_printed = B_TRUE;
2514}
2515
2516static void
2517dump_config_from_label(label_t *label, size_t buflen, int l)
2518{
2519 if (dump_opt['q'])
2520 return;
2521
2522 if ((dump_opt['l'] < 3) && (first_label(label->config) != l))
2523 return;
2524
2525 print_label_header(label, l);
2526 dump_nvlist(label->config_nv, 4);
2527 print_label_numbers(" labels = ", label->config);
2528
2529 if (dump_opt['l'] >= 2)
2530 dump_nvlist_stats(label->config_nv, buflen);
2531}
2532
428870ff
BB
2533#define ZDB_MAX_UB_HEADER_SIZE 32
2534
2535static void
cae5b340 2536dump_label_uberblocks(label_t *label, uint64_t ashift, int label_num)
428870ff 2537{
cae5b340 2538
428870ff 2539 vdev_t vd;
428870ff
BB
2540 char header[ZDB_MAX_UB_HEADER_SIZE];
2541
2542 vd.vdev_ashift = ashift;
cae5b340
AX
2543 vd.vdev_top = &vd;
2544
2545 for (int i = 0; i < VDEV_UBERBLOCK_COUNT(&vd); i++) {
2546 uint64_t uoff = VDEV_UBERBLOCK_OFFSET(&vd, i);
2547 uberblock_t *ub = (void *)((char *)&label->label + uoff);
2548 cksum_record_t *rec = label->uberblocks[i];
2549
2550 if (rec == NULL) {
2551 if (dump_opt['u'] >= 2) {
2552 print_label_header(label, label_num);
2553 (void) printf(" Uberblock[%d] invalid\n", i);
2554 }
2555 continue;
2556 }
428870ff 2557
cae5b340
AX
2558 if ((dump_opt['u'] < 3) && (first_label(rec) != label_num))
2559 continue;
428870ff 2560
cae5b340
AX
2561 if ((dump_opt['u'] < 4) &&
2562 (ub->ub_mmp_magic == MMP_MAGIC) && ub->ub_mmp_delay &&
2563 (i >= VDEV_UBERBLOCK_COUNT(&vd) - MMP_BLOCKS_PER_LABEL))
428870ff 2564 continue;
cae5b340
AX
2565
2566 print_label_header(label, label_num);
428870ff 2567 (void) snprintf(header, ZDB_MAX_UB_HEADER_SIZE,
cae5b340 2568 " Uberblock[%d]\n", i);
428870ff 2569 dump_uberblock(ub, header, "");
cae5b340 2570 print_label_numbers(" labels = ", rec);
428870ff
BB
2571 }
2572}
2573
cae5b340
AX
2574static char curpath[PATH_MAX];
2575
2576/*
2577 * Iterate through the path components, recursively passing
2578 * current one's obj and remaining path until we find the obj
2579 * for the last one.
2580 */
2581static int
2582dump_path_impl(objset_t *os, uint64_t obj, char *name)
2583{
2584 int err;
2585 int header = 1;
2586 uint64_t child_obj;
2587 char *s;
2588 dmu_buf_t *db;
2589 dmu_object_info_t doi;
2590
2591 if ((s = strchr(name, '/')) != NULL)
2592 *s = '\0';
2593 err = zap_lookup(os, obj, name, 8, 1, &child_obj);
2594
2595 (void) strlcat(curpath, name, sizeof (curpath));
2596
2597 if (err != 0) {
2598 (void) fprintf(stderr, "failed to lookup %s: %s\n",
2599 curpath, strerror(err));
2600 return (err);
2601 }
2602
2603 child_obj = ZFS_DIRENT_OBJ(child_obj);
2604 err = sa_buf_hold(os, child_obj, FTAG, &db);
2605 if (err != 0) {
2606 (void) fprintf(stderr,
2607 "failed to get SA dbuf for obj %llu: %s\n",
2608 (u_longlong_t)child_obj, strerror(err));
2609 return (EINVAL);
2610 }
2611 dmu_object_info_from_db(db, &doi);
2612 sa_buf_rele(db, FTAG);
2613
2614 if (doi.doi_bonus_type != DMU_OT_SA &&
2615 doi.doi_bonus_type != DMU_OT_ZNODE) {
2616 (void) fprintf(stderr, "invalid bonus type %d for obj %llu\n",
2617 doi.doi_bonus_type, (u_longlong_t)child_obj);
2618 return (EINVAL);
2619 }
2620
2621 if (dump_opt['v'] > 6) {
2622 (void) printf("obj=%llu %s type=%d bonustype=%d\n",
2623 (u_longlong_t)child_obj, curpath, doi.doi_type,
2624 doi.doi_bonus_type);
2625 }
2626
2627 (void) strlcat(curpath, "/", sizeof (curpath));
2628
2629 switch (doi.doi_type) {
2630 case DMU_OT_DIRECTORY_CONTENTS:
2631 if (s != NULL && *(s + 1) != '\0')
2632 return (dump_path_impl(os, child_obj, s + 1));
2633 /*FALLTHROUGH*/
2634 case DMU_OT_PLAIN_FILE_CONTENTS:
2635 dump_object(os, child_obj, dump_opt['v'], &header, NULL);
2636 return (0);
2637 default:
2638 (void) fprintf(stderr, "object %llu has non-file/directory "
2639 "type %d\n", (u_longlong_t)obj, doi.doi_type);
2640 break;
2641 }
2642
2643 return (EINVAL);
2644}
2645
2646/*
2647 * Dump the blocks for the object specified by path inside the dataset.
2648 */
2649static int
2650dump_path(char *ds, char *path)
2651{
2652 int err;
2653 objset_t *os;
2654 uint64_t root_obj;
2655
2656 err = open_objset(ds, DMU_OST_ZFS, FTAG, &os);
2657 if (err != 0)
2658 return (err);
2659
2660 err = zap_lookup(os, MASTER_NODE_OBJ, ZFS_ROOT_OBJ, 8, 1, &root_obj);
2661 if (err != 0) {
2662 (void) fprintf(stderr, "can't lookup root znode: %s\n",
2663 strerror(err));
2664 dmu_objset_disown(os, FTAG);
2665 return (EINVAL);
2666 }
2667
2668 (void) snprintf(curpath, sizeof (curpath), "dataset=%s path=/", ds);
2669
2670 err = dump_path_impl(os, root_obj, path);
2671
2672 close_objset(os, FTAG);
2673 return (err);
2674}
2675
2676static int
34dc7c2f
BB
2677dump_label(const char *dev)
2678{
cae5b340
AX
2679 char path[MAXPATHLEN];
2680 label_t labels[VDEV_LABELS];
428870ff 2681 uint64_t psize, ashift;
cae5b340
AX
2682 struct stat64 statbuf;
2683 boolean_t config_found = B_FALSE;
2684 boolean_t error = B_FALSE;
2685 avl_tree_t config_tree;
2686 avl_tree_t uberblock_tree;
2687 void *node, *cookie;
2688 int fd;
34dc7c2f 2689
cae5b340
AX
2690 bzero(labels, sizeof (labels));
2691
2692 /*
2693 * Check if we were given absolute path and use it as is.
2694 * Otherwise if the provided vdev name doesn't point to a file,
2695 * try prepending expected disk paths and partition numbers.
2696 */
2697 (void) strlcpy(path, dev, sizeof (path));
2698 if (dev[0] != '/' && stat64(path, &statbuf) != 0) {
2699 int error;
2700
2701 error = zfs_resolve_shortname(dev, path, MAXPATHLEN);
2702 if (error == 0 && zfs_dev_is_whole_disk(path)) {
2703 if (zfs_append_partition(path, MAXPATHLEN) == -1)
2704 error = ENOENT;
2705 }
2706
2707 if (error || (stat64(path, &statbuf) != 0)) {
2708 (void) printf("failed to find device %s, try "
2709 "specifying absolute path instead\n", dev);
2710 return (1);
2711 }
428870ff
BB
2712 }
2713
2714 if ((fd = open64(path, O_RDONLY)) < 0) {
2715 (void) printf("cannot open '%s': %s\n", path, strerror(errno));
34dc7c2f
BB
2716 exit(1);
2717 }
2718
8d4e8140 2719 if (fstat64_blk(fd, &statbuf) != 0) {
428870ff 2720 (void) printf("failed to stat '%s': %s\n", path,
34dc7c2f 2721 strerror(errno));
428870ff
BB
2722 (void) close(fd);
2723 exit(1);
2724 }
2725
42f7b73b
AX
2726 if (S_ISBLK(statbuf.st_mode) && ioctl(fd, BLKFLSBUF) != 0)
2727 (void) printf("failed to invalidate cache '%s' : %s\n", path,
2728 strerror(errno));
2729
cae5b340
AX
2730 avl_create(&config_tree, cksum_record_compare,
2731 sizeof (cksum_record_t), offsetof(cksum_record_t, link));
2732 avl_create(&uberblock_tree, cksum_record_compare,
2733 sizeof (cksum_record_t), offsetof(cksum_record_t, link));
2734
34dc7c2f
BB
2735 psize = statbuf.st_size;
2736 psize = P2ALIGN(psize, (uint64_t)sizeof (vdev_label_t));
cae5b340 2737 ashift = SPA_MINBLOCKSHIFT;
34dc7c2f 2738
cae5b340
AX
2739 /*
2740 * 1. Read the label from disk
2741 * 2. Unpack the configuration and insert in config tree.
2742 * 3. Traverse all uberblocks and insert in uberblock tree.
2743 */
2744 for (int l = 0; l < VDEV_LABELS; l++) {
2745 label_t *label = &labels[l];
2746 char *buf = label->label.vl_vdev_phys.vp_nvlist;
2747 size_t buflen = sizeof (label->label.vl_vdev_phys.vp_nvlist);
2748 nvlist_t *config;
2749 cksum_record_t *rec;
2750 zio_cksum_t cksum;
2751 vdev_t vd;
2752
2753 if (pread64(fd, &label->label, sizeof (label->label),
2754 vdev_label_offset(psize, l, 0)) != sizeof (label->label)) {
2755 if (!dump_opt['q'])
2756 (void) printf("failed to read label %d\n", l);
2757 label->read_failed = B_TRUE;
2758 error = B_TRUE;
34dc7c2f
BB
2759 continue;
2760 }
2761
cae5b340
AX
2762 label->read_failed = B_FALSE;
2763
2764 if (nvlist_unpack(buf, buflen, &config, 0) == 0) {
428870ff 2765 nvlist_t *vdev_tree = NULL;
cae5b340 2766 size_t size;
428870ff 2767
428870ff
BB
2768 if ((nvlist_lookup_nvlist(config,
2769 ZPOOL_CONFIG_VDEV_TREE, &vdev_tree) != 0) ||
2770 (nvlist_lookup_uint64(vdev_tree,
2771 ZPOOL_CONFIG_ASHIFT, &ashift) != 0))
2772 ashift = SPA_MINBLOCKSHIFT;
cae5b340
AX
2773
2774 if (nvlist_size(config, &size, NV_ENCODE_XDR) != 0)
2775 size = buflen;
2776
2777 fletcher_4_native_varsize(buf, size, &cksum);
2778 rec = cksum_record_insert(&config_tree, &cksum, l);
2779
2780 label->config = rec;
2781 label->config_nv = config;
2782 config_found = B_TRUE;
2783 } else {
2784 error = B_TRUE;
34dc7c2f 2785 }
cae5b340
AX
2786
2787 vd.vdev_ashift = ashift;
2788 vd.vdev_top = &vd;
2789
2790 for (int i = 0; i < VDEV_UBERBLOCK_COUNT(&vd); i++) {
2791 uint64_t uoff = VDEV_UBERBLOCK_OFFSET(&vd, i);
2792 uberblock_t *ub = (void *)((char *)label + uoff);
2793
2794 if (uberblock_verify(ub))
2795 continue;
2796
2797 fletcher_4_native_varsize(ub, sizeof (*ub), &cksum);
2798 rec = cksum_record_insert(&uberblock_tree, &cksum, l);
2799
2800 label->uberblocks[i] = rec;
2801 }
2802 }
2803
2804 /*
2805 * Dump the label and uberblocks.
2806 */
2807 for (int l = 0; l < VDEV_LABELS; l++) {
2808 label_t *label = &labels[l];
2809 size_t buflen = sizeof (label->label.vl_vdev_phys.vp_nvlist);
2810
2811 if (label->read_failed == B_TRUE)
2812 continue;
2813
2814 if (label->config_nv) {
2815 dump_config_from_label(label, buflen, l);
2816 } else {
2817 if (!dump_opt['q'])
2818 (void) printf("failed to unpack label %d\n", l);
2819 }
2820
428870ff 2821 if (dump_opt['u'])
cae5b340
AX
2822 dump_label_uberblocks(label, ashift, l);
2823
2824 nvlist_free(label->config_nv);
34dc7c2f 2825 }
428870ff 2826
cae5b340
AX
2827 cookie = NULL;
2828 while ((node = avl_destroy_nodes(&config_tree, &cookie)) != NULL)
2829 umem_free(node, sizeof (cksum_record_t));
2830
2831 cookie = NULL;
2832 while ((node = avl_destroy_nodes(&uberblock_tree, &cookie)) != NULL)
2833 umem_free(node, sizeof (cksum_record_t));
2834
2835 avl_destroy(&config_tree);
2836 avl_destroy(&uberblock_tree);
2837
428870ff 2838 (void) close(fd);
cae5b340
AX
2839
2840 return (config_found == B_FALSE ? 2 :
2841 (error == B_TRUE ? 1 : 0));
34dc7c2f
BB
2842}
2843
cae5b340 2844static uint64_t dataset_feature_count[SPA_FEATURES];
e10b0808 2845
34dc7c2f
BB
2846/*ARGSUSED*/
2847static int
428870ff 2848dump_one_dir(const char *dsname, void *arg)
34dc7c2f
BB
2849{
2850 int error;
2851 objset_t *os;
cae5b340 2852 spa_feature_t f;
34dc7c2f 2853
cae5b340
AX
2854 error = open_objset(dsname, DMU_OST_ANY, FTAG, &os);
2855 if (error != 0)
34dc7c2f 2856 return (0);
cae5b340
AX
2857
2858 for (f = 0; f < SPA_FEATURES; f++) {
2859 if (!dmu_objset_ds(os)->ds_feature_inuse[f])
2860 continue;
2861 ASSERT(spa_feature_table[f].fi_flags &
2862 ZFEATURE_FLAG_PER_DATASET);
2863 dataset_feature_count[f]++;
34dc7c2f 2864 }
cae5b340 2865
34dc7c2f 2866 dump_dir(os);
cae5b340 2867 close_objset(os, FTAG);
34dc7c2f
BB
2868 fuid_table_destroy();
2869 return (0);
2870}
2871
34dc7c2f 2872/*
428870ff 2873 * Block statistics.
34dc7c2f 2874 */
e10b0808 2875#define PSIZE_HISTO_SIZE (SPA_OLD_MAXBLOCKSIZE / SPA_MINBLOCKSIZE + 2)
34dc7c2f 2876typedef struct zdb_blkstats {
ea04106b
AX
2877 uint64_t zb_asize;
2878 uint64_t zb_lsize;
2879 uint64_t zb_psize;
2880 uint64_t zb_count;
e10b0808
AX
2881 uint64_t zb_gangs;
2882 uint64_t zb_ditto_samevdev;
ea04106b 2883 uint64_t zb_psize_histogram[PSIZE_HISTO_SIZE];
34dc7c2f
BB
2884} zdb_blkstats_t;
2885
428870ff
BB
2886/*
2887 * Extended object types to report deferred frees and dedup auto-ditto blocks.
2888 */
2889#define ZDB_OT_DEFERRED (DMU_OT_NUMTYPES + 0)
2890#define ZDB_OT_DITTO (DMU_OT_NUMTYPES + 1)
9ae529ec
CS
2891#define ZDB_OT_OTHER (DMU_OT_NUMTYPES + 2)
2892#define ZDB_OT_TOTAL (DMU_OT_NUMTYPES + 3)
428870ff
BB
2893
2894static char *zdb_ot_extname[] = {
2895 "deferred free",
2896 "dedup ditto",
9ae529ec 2897 "other",
428870ff
BB
2898 "Total",
2899};
34dc7c2f 2900
b128c09f 2901#define ZB_TOTAL DN_MAX_LEVELS
34dc7c2f
BB
2902
2903typedef struct zdb_cb {
428870ff
BB
2904 zdb_blkstats_t zcb_type[ZB_TOTAL + 1][ZDB_OT_TOTAL + 1];
2905 uint64_t zcb_dedup_asize;
2906 uint64_t zcb_dedup_blocks;
ea04106b
AX
2907 uint64_t zcb_embedded_blocks[NUM_BP_EMBEDDED_TYPES];
2908 uint64_t zcb_embedded_histogram[NUM_BP_EMBEDDED_TYPES]
cae5b340 2909 [BPE_PAYLOAD_SIZE + 1];
ea04106b
AX
2910 uint64_t zcb_start;
2911 uint64_t zcb_lastprint;
2912 uint64_t zcb_totalasize;
34dc7c2f 2913 uint64_t zcb_errors[256];
34dc7c2f
BB
2914 int zcb_readfails;
2915 int zcb_haderrors;
428870ff 2916 spa_t *zcb_spa;
34dc7c2f
BB
2917} zdb_cb_t;
2918
2919static void
428870ff
BB
2920zdb_count_block(zdb_cb_t *zcb, zilog_t *zilog, const blkptr_t *bp,
2921 dmu_object_type_t type)
34dc7c2f 2922{
428870ff 2923 uint64_t refcnt = 0;
d6320ddb 2924 int i;
428870ff
BB
2925
2926 ASSERT(type < ZDB_OT_TOTAL);
2927
2928 if (zilog && zil_bp_tree_add(zilog, bp) != 0)
2929 return;
2930
d6320ddb 2931 for (i = 0; i < 4; i++) {
34dc7c2f 2932 int l = (i < 2) ? BP_GET_LEVEL(bp) : ZB_TOTAL;
428870ff 2933 int t = (i & 1) ? type : ZDB_OT_TOTAL;
e10b0808 2934 int equal;
34dc7c2f
BB
2935 zdb_blkstats_t *zb = &zcb->zcb_type[l][t];
2936
2937 zb->zb_asize += BP_GET_ASIZE(bp);
2938 zb->zb_lsize += BP_GET_LSIZE(bp);
2939 zb->zb_psize += BP_GET_PSIZE(bp);
2940 zb->zb_count++;
e10b0808
AX
2941
2942 /*
2943 * The histogram is only big enough to record blocks up to
2944 * SPA_OLD_MAXBLOCKSIZE; larger blocks go into the last,
2945 * "other", bucket.
2946 */
2947 int idx = BP_GET_PSIZE(bp) >> SPA_MINBLOCKSHIFT;
2948 idx = MIN(idx, SPA_OLD_MAXBLOCKSIZE / SPA_MINBLOCKSIZE + 1);
2949 zb->zb_psize_histogram[idx]++;
2950
2951 zb->zb_gangs += BP_COUNT_GANG(bp);
2952
2953 switch (BP_GET_NDVAS(bp)) {
2954 case 2:
2955 if (DVA_GET_VDEV(&bp->blk_dva[0]) ==
2956 DVA_GET_VDEV(&bp->blk_dva[1]))
2957 zb->zb_ditto_samevdev++;
2958 break;
2959 case 3:
2960 equal = (DVA_GET_VDEV(&bp->blk_dva[0]) ==
2961 DVA_GET_VDEV(&bp->blk_dva[1])) +
2962 (DVA_GET_VDEV(&bp->blk_dva[0]) ==
2963 DVA_GET_VDEV(&bp->blk_dva[2])) +
2964 (DVA_GET_VDEV(&bp->blk_dva[1]) ==
2965 DVA_GET_VDEV(&bp->blk_dva[2]));
2966 if (equal != 0)
2967 zb->zb_ditto_samevdev++;
2968 break;
2969 }
2970
ea04106b
AX
2971 }
2972
2973 if (BP_IS_EMBEDDED(bp)) {
2974 zcb->zcb_embedded_blocks[BPE_GET_ETYPE(bp)]++;
2975 zcb->zcb_embedded_histogram[BPE_GET_ETYPE(bp)]
2976 [BPE_GET_PSIZE(bp)]++;
2977 return;
34dc7c2f
BB
2978 }
2979
428870ff
BB
2980 if (dump_opt['L'])
2981 return;
2982
2983 if (BP_GET_DEDUP(bp)) {
2984 ddt_t *ddt;
2985 ddt_entry_t *dde;
2986
2987 ddt = ddt_select(zcb->zcb_spa, bp);
2988 ddt_enter(ddt);
2989 dde = ddt_lookup(ddt, bp, B_FALSE);
2990
2991 if (dde == NULL) {
2992 refcnt = 0;
2993 } else {
2994 ddt_phys_t *ddp = ddt_phys_select(dde, bp);
2995 ddt_phys_decref(ddp);
2996 refcnt = ddp->ddp_refcnt;
2997 if (ddt_phys_total_refcnt(dde) == 0)
2998 ddt_remove(ddt, dde);
34dc7c2f 2999 }
428870ff 3000 ddt_exit(ddt);
34dc7c2f
BB
3001 }
3002
428870ff
BB
3003 VERIFY3U(zio_wait(zio_claim(NULL, zcb->zcb_spa,
3004 refcnt ? 0 : spa_first_txg(zcb->zcb_spa),
3005 bp, NULL, NULL, ZIO_FLAG_CANFAIL)), ==, 0);
34dc7c2f
BB
3006}
3007
c06d4368
AX
3008static void
3009zdb_blkptr_done(zio_t *zio)
3010{
3011 spa_t *spa = zio->io_spa;
3012 blkptr_t *bp = zio->io_bp;
3013 int ioerr = zio->io_error;
3014 zdb_cb_t *zcb = zio->io_private;
ea04106b 3015 zbookmark_phys_t *zb = &zio->io_bookmark;
c06d4368 3016
cae5b340 3017 abd_free(zio->io_abd);
c06d4368
AX
3018
3019 mutex_enter(&spa->spa_scrub_lock);
3020 spa->spa_scrub_inflight--;
3021 cv_broadcast(&spa->spa_scrub_io_cv);
3022
3023 if (ioerr && !(zio->io_flags & ZIO_FLAG_SPECULATIVE)) {
3024 char blkbuf[BP_SPRINTF_LEN];
3025
3026 zcb->zcb_haderrors = 1;
3027 zcb->zcb_errors[ioerr]++;
3028
3029 if (dump_opt['b'] >= 2)
ea04106b 3030 snprintf_blkptr(blkbuf, sizeof (blkbuf), bp);
c06d4368
AX
3031 else
3032 blkbuf[0] = '\0';
3033
3034 (void) printf("zdb_blkptr_cb: "
3035 "Got error %d reading "
3036 "<%llu, %llu, %lld, %llx> %s -- skipping\n",
3037 ioerr,
3038 (u_longlong_t)zb->zb_objset,
3039 (u_longlong_t)zb->zb_object,
3040 (u_longlong_t)zb->zb_level,
3041 (u_longlong_t)zb->zb_blkid,
3042 blkbuf);
3043 }
3044 mutex_exit(&spa->spa_scrub_lock);
3045}
3046
34dc7c2f 3047static int
c06d4368 3048zdb_blkptr_cb(spa_t *spa, zilog_t *zilog, const blkptr_t *bp,
ea04106b 3049 const zbookmark_phys_t *zb, const dnode_phys_t *dnp, void *arg)
34dc7c2f 3050{
34dc7c2f 3051 zdb_cb_t *zcb = arg;
9babb374 3052 dmu_object_type_t type;
428870ff 3053 boolean_t is_metadata;
34dc7c2f 3054
cae5b340
AX
3055 if (bp == NULL)
3056 return (0);
3057
ea04106b
AX
3058 if (dump_opt['b'] >= 5 && bp->blk_birth > 0) {
3059 char blkbuf[BP_SPRINTF_LEN];
3060 snprintf_blkptr(blkbuf, sizeof (blkbuf), bp);
3061 (void) printf("objset %llu object %llu "
3062 "level %lld offset 0x%llx %s\n",
3063 (u_longlong_t)zb->zb_objset,
3064 (u_longlong_t)zb->zb_object,
3065 (longlong_t)zb->zb_level,
3066 (u_longlong_t)blkid2offset(dnp, bp, zb),
3067 blkbuf);
3068 }
3069
3070 if (BP_IS_HOLE(bp))
b128c09f
BB
3071 return (0);
3072
9babb374
BB
3073 type = BP_GET_TYPE(bp);
3074
9ae529ec
CS
3075 zdb_count_block(zcb, zilog, bp,
3076 (type & DMU_OT_NEWTYPE) ? ZDB_OT_OTHER : type);
b128c09f 3077
9ae529ec 3078 is_metadata = (BP_GET_LEVEL(bp) != 0 || DMU_OT_IS_METADATA(type));
428870ff 3079
ea04106b
AX
3080 if (!BP_IS_EMBEDDED(bp) &&
3081 (dump_opt['c'] > 1 || (dump_opt['c'] && is_metadata))) {
428870ff 3082 size_t size = BP_GET_PSIZE(bp);
cae5b340 3083 abd_t *abd = abd_alloc(size, B_FALSE);
428870ff 3084 int flags = ZIO_FLAG_CANFAIL | ZIO_FLAG_SCRUB | ZIO_FLAG_RAW;
9babb374 3085
428870ff
BB
3086 /* If it's an intent log block, failure is expected. */
3087 if (zb->zb_level == ZB_ZIL_LEVEL)
3088 flags |= ZIO_FLAG_SPECULATIVE;
b128c09f 3089
c06d4368
AX
3090 mutex_enter(&spa->spa_scrub_lock);
3091 while (spa->spa_scrub_inflight > max_inflight)
3092 cv_wait(&spa->spa_scrub_io_cv, &spa->spa_scrub_lock);
3093 spa->spa_scrub_inflight++;
3094 mutex_exit(&spa->spa_scrub_lock);
428870ff 3095
cae5b340 3096 zio_nowait(zio_read(NULL, spa, bp, abd, size,
c06d4368 3097 zdb_blkptr_done, zcb, ZIO_PRIORITY_ASYNC_READ, flags, zb));
34dc7c2f
BB
3098 }
3099
3100 zcb->zcb_readfails = 0;
3101
e10b0808
AX
3102 /* only call gethrtime() every 100 blocks */
3103 static int iters;
3104 if (++iters > 100)
3105 iters = 0;
3106 else
3107 return (0);
3108
3109 if (dump_opt['b'] < 5 && gethrtime() > zcb->zcb_lastprint + NANOSEC) {
ea04106b
AX
3110 uint64_t now = gethrtime();
3111 char buf[10];
3112 uint64_t bytes = zcb->zcb_type[ZB_TOTAL][ZDB_OT_TOTAL].zb_asize;
3113 int kb_per_sec =
3114 1 + bytes / (1 + ((now - zcb->zcb_start) / 1000 / 1000));
3115 int sec_remaining =
3116 (zcb->zcb_totalasize - bytes) / 1024 / kb_per_sec;
3117
cae5b340 3118 zfs_nicebytes(bytes, buf, sizeof (buf));
ea04106b
AX
3119 (void) fprintf(stderr,
3120 "\r%5s completed (%4dMB/s) "
3121 "estimated time remaining: %uhr %02umin %02usec ",
3122 buf, kb_per_sec / 1024,
3123 sec_remaining / 60 / 60,
3124 sec_remaining / 60 % 60,
3125 sec_remaining % 60);
3126
3127 zcb->zcb_lastprint = now;
34dc7c2f
BB
3128 }
3129
34dc7c2f
BB
3130 return (0);
3131}
3132
428870ff 3133static void
ea04106b 3134zdb_leak(void *arg, uint64_t start, uint64_t size)
428870ff 3135{
ea04106b 3136 vdev_t *vd = arg;
428870ff
BB
3137
3138 (void) printf("leaked space: vdev %llu, offset 0x%llx, size %llu\n",
3139 (u_longlong_t)vd->vdev_id, (u_longlong_t)start, (u_longlong_t)size);
3140}
3141
ea04106b
AX
3142static metaslab_ops_t zdb_metaslab_ops = {
3143 NULL /* alloc */
428870ff
BB
3144};
3145
3146static void
3147zdb_ddt_leak_init(spa_t *spa, zdb_cb_t *zcb)
3148{
3149 ddt_bookmark_t ddb = { 0 };
3150 ddt_entry_t dde;
3151 int error;
d6320ddb 3152 int p;
428870ff
BB
3153
3154 while ((error = ddt_walk(spa, &ddb, &dde)) == 0) {
3155 blkptr_t blk;
3156 ddt_phys_t *ddp = dde.dde_phys;
3157
3158 if (ddb.ddb_class == DDT_CLASS_UNIQUE)
3159 return;
3160
3161 ASSERT(ddt_phys_total_refcnt(&dde) > 1);
3162
d6320ddb 3163 for (p = 0; p < DDT_PHYS_TYPES; p++, ddp++) {
428870ff
BB
3164 if (ddp->ddp_phys_birth == 0)
3165 continue;
3166 ddt_bp_create(ddb.ddb_checksum,
3167 &dde.dde_key, ddp, &blk);
3168 if (p == DDT_PHYS_DITTO) {
3169 zdb_count_block(zcb, NULL, &blk, ZDB_OT_DITTO);
3170 } else {
3171 zcb->zcb_dedup_asize +=
3172 BP_GET_ASIZE(&blk) * (ddp->ddp_refcnt - 1);
3173 zcb->zcb_dedup_blocks++;
3174 }
3175 }
3176 if (!dump_opt['L']) {
3177 ddt_t *ddt = spa->spa_ddt[ddb.ddb_checksum];
3178 ddt_enter(ddt);
3179 VERIFY(ddt_lookup(ddt, &blk, B_TRUE) != NULL);
3180 ddt_exit(ddt);
3181 }
3182 }
3183
3184 ASSERT(error == ENOENT);
3185}
3186
3187static void
3188zdb_leak_init(spa_t *spa, zdb_cb_t *zcb)
3189{
3190 zcb->zcb_spa = spa;
ea04106b 3191 uint64_t c, m;
428870ff
BB
3192
3193 if (!dump_opt['L']) {
3194 vdev_t *rvd = spa->spa_root_vdev;
cae5b340
AX
3195
3196 /*
3197 * We are going to be changing the meaning of the metaslab's
3198 * ms_tree. Ensure that the allocator doesn't try to
3199 * use the tree.
3200 */
3201 spa->spa_normal_class->mc_ops = &zdb_metaslab_ops;
3202 spa->spa_log_class->mc_ops = &zdb_metaslab_ops;
3203
d6320ddb 3204 for (c = 0; c < rvd->vdev_children; c++) {
428870ff 3205 vdev_t *vd = rvd->vdev_child[c];
cae5b340 3206 ASSERTV(metaslab_group_t *mg = vd->vdev_mg);
d6320ddb 3207 for (m = 0; m < vd->vdev_ms_count; m++) {
428870ff 3208 metaslab_t *msp = vd->vdev_ms[m];
cae5b340 3209 ASSERT3P(msp->ms_group, ==, mg);
428870ff 3210 mutex_enter(&msp->ms_lock);
ea04106b
AX
3211 metaslab_unload(msp);
3212
3213 /*
3214 * For leak detection, we overload the metaslab
3215 * ms_tree to contain allocated segments
3216 * instead of free segments. As a result,
3217 * we can't use the normal metaslab_load/unload
3218 * interfaces.
3219 */
3220 if (msp->ms_sm != NULL) {
3221 (void) fprintf(stderr,
3222 "\rloading space map for "
3223 "vdev %llu of %llu, "
3224 "metaslab %llu of %llu ...",
3225 (longlong_t)c,
3226 (longlong_t)rvd->vdev_children,
3227 (longlong_t)m,
3228 (longlong_t)vd->vdev_ms_count);
3229
e10b0808
AX
3230 /*
3231 * We don't want to spend the CPU
3232 * manipulating the size-ordered
3233 * tree, so clear the range_tree
3234 * ops.
3235 */
3236 msp->ms_tree->rt_ops = NULL;
ea04106b
AX
3237 VERIFY0(space_map_load(msp->ms_sm,
3238 msp->ms_tree, SM_ALLOC));
cae5b340
AX
3239
3240 if (!msp->ms_loaded)
3241 msp->ms_loaded = B_TRUE;
ea04106b 3242 }
428870ff
BB
3243 mutex_exit(&msp->ms_lock);
3244 }
3245 }
ea04106b 3246 (void) fprintf(stderr, "\n");
428870ff
BB
3247 }
3248
3249 spa_config_enter(spa, SCL_CONFIG, FTAG, RW_READER);
3250
3251 zdb_ddt_leak_init(spa, zcb);
3252
3253 spa_config_exit(spa, SCL_CONFIG, FTAG);
3254}
3255
3256static void
3257zdb_leak_fini(spa_t *spa)
3258{
d6320ddb
BB
3259 int c, m;
3260
428870ff
BB
3261 if (!dump_opt['L']) {
3262 vdev_t *rvd = spa->spa_root_vdev;
d6320ddb 3263 for (c = 0; c < rvd->vdev_children; c++) {
428870ff 3264 vdev_t *vd = rvd->vdev_child[c];
cae5b340 3265 ASSERTV(metaslab_group_t *mg = vd->vdev_mg);
d6320ddb 3266 for (m = 0; m < vd->vdev_ms_count; m++) {
428870ff 3267 metaslab_t *msp = vd->vdev_ms[m];
cae5b340 3268 ASSERT3P(mg, ==, msp->ms_group);
428870ff 3269 mutex_enter(&msp->ms_lock);
ea04106b
AX
3270
3271 /*
3272 * The ms_tree has been overloaded to
3273 * contain allocated segments. Now that we
3274 * finished traversing all blocks, any
3275 * block that remains in the ms_tree
3276 * represents an allocated block that we
3277 * did not claim during the traversal.
3278 * Claimed blocks would have been removed
3279 * from the ms_tree.
3280 */
3281 range_tree_vacate(msp->ms_tree, zdb_leak, vd);
cae5b340
AX
3282
3283 if (msp->ms_loaded)
3284 msp->ms_loaded = B_FALSE;
ea04106b 3285
428870ff
BB
3286 mutex_exit(&msp->ms_lock);
3287 }
3288 }
3289 }
3290}
3291
3292/* ARGSUSED */
3293static int
3294count_block_cb(void *arg, const blkptr_t *bp, dmu_tx_t *tx)
3295{
3296 zdb_cb_t *zcb = arg;
3297
ea04106b 3298 if (dump_opt['b'] >= 5) {
428870ff 3299 char blkbuf[BP_SPRINTF_LEN];
ea04106b 3300 snprintf_blkptr(blkbuf, sizeof (blkbuf), bp);
428870ff
BB
3301 (void) printf("[%s] %s\n",
3302 "deferred free", blkbuf);
3303 }
3304 zdb_count_block(zcb, NULL, bp, ZDB_OT_DEFERRED);
3305 return (0);
3306}
3307
34dc7c2f
BB
3308static int
3309dump_block_stats(spa_t *spa)
3310{
2598c001 3311 zdb_cb_t zcb;
34dc7c2f 3312 zdb_blkstats_t *zb, *tzb;
428870ff
BB
3313 uint64_t norm_alloc, norm_space, total_alloc, total_found;
3314 int flags = TRAVERSE_PRE | TRAVERSE_PREFETCH_METADATA | TRAVERSE_HARD;
ea04106b 3315 boolean_t leaks = B_FALSE;
42f7b73b 3316 int e, c, err;
ea04106b 3317 bp_embedded_type_t i;
34dc7c2f 3318
ea04106b 3319 (void) printf("\nTraversing all blocks %s%s%s%s%s...\n\n",
428870ff
BB
3320 (dump_opt['c'] || !dump_opt['L']) ? "to verify " : "",
3321 (dump_opt['c'] == 1) ? "metadata " : "",
3322 dump_opt['c'] ? "checksums " : "",
3323 (dump_opt['c'] && !dump_opt['L']) ? "and verify " : "",
3324 !dump_opt['L'] ? "nothing leaked " : "");
34dc7c2f
BB
3325
3326 /*
b128c09f
BB
3327 * Load all space maps as SM_ALLOC maps, then traverse the pool
3328 * claiming each block we discover. If the pool is perfectly
3329 * consistent, the space maps will be empty when we're done.
3330 * Anything left over is a leak; any block we can't claim (because
3331 * it's not part of any space map) is a double allocation,
3332 * reference to a freed block, or an unclaimed log block.
34dc7c2f 3333 */
a08ee875 3334 bzero(&zcb, sizeof (zdb_cb_t));
428870ff 3335 zdb_leak_init(spa, &zcb);
34dc7c2f
BB
3336
3337 /*
3338 * If there's a deferred-free bplist, process that first.
3339 */
428870ff
BB
3340 (void) bpobj_iterate_nofree(&spa->spa_deferred_bpobj,
3341 count_block_cb, &zcb, NULL);
a08ee875
LG
3342 if (spa_version(spa) >= SPA_VERSION_DEADLISTS) {
3343 (void) bpobj_iterate_nofree(&spa->spa_dsl_pool->dp_free_bpobj,
3344 count_block_cb, &zcb, NULL);
3345 }
ea04106b 3346 if (spa_feature_is_active(spa, SPA_FEATURE_ASYNC_DESTROY)) {
9ae529ec
CS
3347 VERIFY3U(0, ==, bptree_iterate(spa->spa_meta_objset,
3348 spa->spa_dsl_pool->dp_bptree_obj, B_FALSE, count_block_cb,
3349 &zcb, NULL));
3350 }
34dc7c2f 3351
428870ff
BB
3352 if (dump_opt['c'] > 1)
3353 flags |= TRAVERSE_PREFETCH_DATA;
34dc7c2f 3354
ea04106b
AX
3355 zcb.zcb_totalasize = metaslab_class_get_alloc(spa_normal_class(spa));
3356 zcb.zcb_start = zcb.zcb_lastprint = gethrtime();
42f7b73b 3357 err = traverse_pool(spa, 0, flags, zdb_blkptr_cb, &zcb);
34dc7c2f 3358
c06d4368
AX
3359 /*
3360 * If we've traversed the data blocks then we need to wait for those
3361 * I/Os to complete. We leverage "The Godfather" zio to wait on
3362 * all async I/Os to complete.
3363 */
3364 if (dump_opt['c']) {
ea04106b
AX
3365 for (c = 0; c < max_ncpus; c++) {
3366 (void) zio_wait(spa->spa_async_zio_root[c]);
3367 spa->spa_async_zio_root[c] = zio_root(spa, NULL, NULL,
3368 ZIO_FLAG_CANFAIL | ZIO_FLAG_SPECULATIVE |
3369 ZIO_FLAG_GODFATHER);
3370 }
c06d4368
AX
3371 }
3372
42f7b73b
AX
3373 /*
3374 * Done after zio_wait() since zcb_haderrors is modified in
3375 * zdb_blkptr_done()
3376 */
3377 zcb.zcb_haderrors |= err;
3378
428870ff 3379 if (zcb.zcb_haderrors) {
34dc7c2f
BB
3380 (void) printf("\nError counts:\n\n");
3381 (void) printf("\t%5s %s\n", "errno", "count");
d6320ddb 3382 for (e = 0; e < 256; e++) {
34dc7c2f
BB
3383 if (zcb.zcb_errors[e] != 0) {
3384 (void) printf("\t%5d %llu\n",
3385 e, (u_longlong_t)zcb.zcb_errors[e]);
3386 }
3387 }
3388 }
3389
3390 /*
3391 * Report any leaked segments.
3392 */
428870ff 3393 zdb_leak_fini(spa);
34dc7c2f 3394
428870ff 3395 tzb = &zcb.zcb_type[ZB_TOTAL][ZDB_OT_TOTAL];
34dc7c2f 3396
428870ff
BB
3397 norm_alloc = metaslab_class_get_alloc(spa_normal_class(spa));
3398 norm_space = metaslab_class_get_space(spa_normal_class(spa));
34dc7c2f 3399
428870ff
BB
3400 total_alloc = norm_alloc + metaslab_class_get_alloc(spa_log_class(spa));
3401 total_found = tzb->zb_asize - zcb.zcb_dedup_asize;
34dc7c2f 3402
428870ff 3403 if (total_found == total_alloc) {
fb5f0bc8
BB
3404 if (!dump_opt['L'])
3405 (void) printf("\n\tNo leaks (block sum matches space"
3406 " maps exactly)\n");
34dc7c2f
BB
3407 } else {
3408 (void) printf("block traversal size %llu != alloc %llu "
fb5f0bc8 3409 "(%s %lld)\n",
428870ff
BB
3410 (u_longlong_t)total_found,
3411 (u_longlong_t)total_alloc,
fb5f0bc8 3412 (dump_opt['L']) ? "unreachable" : "leaked",
428870ff 3413 (longlong_t)(total_alloc - total_found));
ea04106b 3414 leaks = B_TRUE;
34dc7c2f
BB
3415 }
3416
3417 if (tzb->zb_count == 0)
3418 return (2);
3419
3420 (void) printf("\n");
3421 (void) printf("\tbp count: %10llu\n",
3422 (u_longlong_t)tzb->zb_count);
e10b0808
AX
3423 (void) printf("\tganged count: %10llu\n",
3424 (longlong_t)tzb->zb_gangs);
428870ff 3425 (void) printf("\tbp logical: %10llu avg: %6llu\n",
34dc7c2f
BB
3426 (u_longlong_t)tzb->zb_lsize,
3427 (u_longlong_t)(tzb->zb_lsize / tzb->zb_count));
428870ff
BB
3428 (void) printf("\tbp physical: %10llu avg:"
3429 " %6llu compression: %6.2f\n",
34dc7c2f
BB
3430 (u_longlong_t)tzb->zb_psize,
3431 (u_longlong_t)(tzb->zb_psize / tzb->zb_count),
3432 (double)tzb->zb_lsize / tzb->zb_psize);
428870ff
BB
3433 (void) printf("\tbp allocated: %10llu avg:"
3434 " %6llu compression: %6.2f\n",
34dc7c2f
BB
3435 (u_longlong_t)tzb->zb_asize,
3436 (u_longlong_t)(tzb->zb_asize / tzb->zb_count),
3437 (double)tzb->zb_lsize / tzb->zb_asize);
428870ff
BB
3438 (void) printf("\tbp deduped: %10llu ref>1:"
3439 " %6llu deduplication: %6.2f\n",
3440 (u_longlong_t)zcb.zcb_dedup_asize,
3441 (u_longlong_t)zcb.zcb_dedup_blocks,
3442 (double)zcb.zcb_dedup_asize / tzb->zb_asize + 1.0);
3443 (void) printf("\tSPA allocated: %10llu used: %5.2f%%\n",
3444 (u_longlong_t)norm_alloc, 100.0 * norm_alloc / norm_space);
34dc7c2f 3445
ea04106b
AX
3446 for (i = 0; i < NUM_BP_EMBEDDED_TYPES; i++) {
3447 if (zcb.zcb_embedded_blocks[i] == 0)
3448 continue;
3449 (void) printf("\n");
3450 (void) printf("\tadditional, non-pointer bps of type %u: "
3451 "%10llu\n",
3452 i, (u_longlong_t)zcb.zcb_embedded_blocks[i]);
3453
3454 if (dump_opt['b'] >= 3) {
3455 (void) printf("\t number of (compressed) bytes: "
3456 "number of bps\n");
3457 dump_histogram(zcb.zcb_embedded_histogram[i],
3458 sizeof (zcb.zcb_embedded_histogram[i]) /
3459 sizeof (zcb.zcb_embedded_histogram[i][0]), 0);
3460 }
3461 }
3462
e10b0808
AX
3463 if (tzb->zb_ditto_samevdev != 0) {
3464 (void) printf("\tDittoed blocks on same vdev: %llu\n",
3465 (longlong_t)tzb->zb_ditto_samevdev);
3466 }
3467
34dc7c2f
BB
3468 if (dump_opt['b'] >= 2) {
3469 int l, t, level;
3470 (void) printf("\nBlocks\tLSIZE\tPSIZE\tASIZE"
3471 "\t avg\t comp\t%%Total\tType\n");
3472
428870ff
BB
3473 for (t = 0; t <= ZDB_OT_TOTAL; t++) {
3474 char csize[32], lsize[32], psize[32], asize[32];
e10b0808 3475 char avg[32], gang[32];
34dc7c2f
BB
3476 char *typename;
3477
428870ff
BB
3478 if (t < DMU_OT_NUMTYPES)
3479 typename = dmu_ot[t].ot_name;
3480 else
3481 typename = zdb_ot_extname[t - DMU_OT_NUMTYPES];
34dc7c2f
BB
3482
3483 if (zcb.zcb_type[ZB_TOTAL][t].zb_asize == 0) {
3484 (void) printf("%6s\t%5s\t%5s\t%5s"
3485 "\t%5s\t%5s\t%6s\t%s\n",
3486 "-",
3487 "-",
3488 "-",
3489 "-",
3490 "-",
3491 "-",
3492 "-",
3493 typename);
3494 continue;
3495 }
3496
3497 for (l = ZB_TOTAL - 1; l >= -1; l--) {
3498 level = (l == -1 ? ZB_TOTAL : l);
3499 zb = &zcb.zcb_type[level][t];
3500
3501 if (zb->zb_asize == 0)
3502 continue;
3503
3504 if (dump_opt['b'] < 3 && level != ZB_TOTAL)
3505 continue;
3506
3507 if (level == 0 && zb->zb_asize ==
3508 zcb.zcb_type[ZB_TOTAL][t].zb_asize)
3509 continue;
3510
428870ff
BB
3511 zdb_nicenum(zb->zb_count, csize);
3512 zdb_nicenum(zb->zb_lsize, lsize);
3513 zdb_nicenum(zb->zb_psize, psize);
3514 zdb_nicenum(zb->zb_asize, asize);
3515 zdb_nicenum(zb->zb_asize / zb->zb_count, avg);
e10b0808 3516 zdb_nicenum(zb->zb_gangs, gang);
34dc7c2f
BB
3517
3518 (void) printf("%6s\t%5s\t%5s\t%5s\t%5s"
3519 "\t%5.2f\t%6.2f\t",
3520 csize, lsize, psize, asize, avg,
3521 (double)zb->zb_lsize / zb->zb_psize,
3522 100.0 * zb->zb_asize / tzb->zb_asize);
3523
3524 if (level == ZB_TOTAL)
3525 (void) printf("%s\n", typename);
3526 else
3527 (void) printf(" L%d %s\n",
3528 level, typename);
ea04106b 3529
e10b0808
AX
3530 if (dump_opt['b'] >= 3 && zb->zb_gangs > 0) {
3531 (void) printf("\t number of ganged "
3532 "blocks: %s\n", gang);
3533 }
3534
ea04106b
AX
3535 if (dump_opt['b'] >= 4) {
3536 (void) printf("psize "
3537 "(in 512-byte sectors): "
3538 "number of blocks\n");
3539 dump_histogram(zb->zb_psize_histogram,
3540 PSIZE_HISTO_SIZE, 0);
3541 }
34dc7c2f
BB
3542 }
3543 }
3544 }
3545
3546 (void) printf("\n");
3547
3548 if (leaks)
3549 return (2);
3550
3551 if (zcb.zcb_haderrors)
3552 return (3);
3553
3554 return (0);
3555}
3556
428870ff
BB
3557typedef struct zdb_ddt_entry {
3558 ddt_key_t zdde_key;
3559 uint64_t zdde_ref_blocks;
3560 uint64_t zdde_ref_lsize;
3561 uint64_t zdde_ref_psize;
3562 uint64_t zdde_ref_dsize;
3563 avl_node_t zdde_node;
3564} zdb_ddt_entry_t;
3565
3566/* ARGSUSED */
3567static int
3568zdb_ddt_add_cb(spa_t *spa, zilog_t *zilog, const blkptr_t *bp,
ea04106b 3569 const zbookmark_phys_t *zb, const dnode_phys_t *dnp, void *arg)
428870ff
BB
3570{
3571 avl_tree_t *t = arg;
3572 avl_index_t where;
3573 zdb_ddt_entry_t *zdde, zdde_search;
3574
cae5b340 3575 if (bp == NULL || BP_IS_HOLE(bp) || BP_IS_EMBEDDED(bp))
428870ff
BB
3576 return (0);
3577
3578 if (dump_opt['S'] > 1 && zb->zb_level == ZB_ROOT_LEVEL) {
3579 (void) printf("traversing objset %llu, %llu objects, "
3580 "%lu blocks so far\n",
3581 (u_longlong_t)zb->zb_objset,
ea04106b 3582 (u_longlong_t)BP_GET_FILL(bp),
428870ff
BB
3583 avl_numnodes(t));
3584 }
3585
3586 if (BP_IS_HOLE(bp) || BP_GET_CHECKSUM(bp) == ZIO_CHECKSUM_OFF ||
9ae529ec 3587 BP_GET_LEVEL(bp) > 0 || DMU_OT_IS_METADATA(BP_GET_TYPE(bp)))
428870ff
BB
3588 return (0);
3589
3590 ddt_key_fill(&zdde_search.zdde_key, bp);
3591
3592 zdde = avl_find(t, &zdde_search, &where);
3593
3594 if (zdde == NULL) {
3595 zdde = umem_zalloc(sizeof (*zdde), UMEM_NOFAIL);
3596 zdde->zdde_key = zdde_search.zdde_key;
3597 avl_insert(t, zdde, where);
3598 }
3599
3600 zdde->zdde_ref_blocks += 1;
3601 zdde->zdde_ref_lsize += BP_GET_LSIZE(bp);
3602 zdde->zdde_ref_psize += BP_GET_PSIZE(bp);
3603 zdde->zdde_ref_dsize += bp_get_dsize_sync(spa, bp);
3604
3605 return (0);
3606}
3607
3608static void
3609dump_simulated_ddt(spa_t *spa)
3610{
3611 avl_tree_t t;
3612 void *cookie = NULL;
3613 zdb_ddt_entry_t *zdde;
2598c001
BB
3614 ddt_histogram_t ddh_total;
3615 ddt_stat_t dds_total;
3616
3617 bzero(&ddh_total, sizeof (ddt_histogram_t));
3618 bzero(&dds_total, sizeof (ddt_stat_t));
428870ff
BB
3619
3620 avl_create(&t, ddt_entry_compare,
3621 sizeof (zdb_ddt_entry_t), offsetof(zdb_ddt_entry_t, zdde_node));
3622
3623 spa_config_enter(spa, SCL_CONFIG, FTAG, RW_READER);
3624
3625 (void) traverse_pool(spa, 0, TRAVERSE_PRE | TRAVERSE_PREFETCH_METADATA,
3626 zdb_ddt_add_cb, &t);
3627
3628 spa_config_exit(spa, SCL_CONFIG, FTAG);
3629
3630 while ((zdde = avl_destroy_nodes(&t, &cookie)) != NULL) {
3631 ddt_stat_t dds;
3632 uint64_t refcnt = zdde->zdde_ref_blocks;
3633 ASSERT(refcnt != 0);
3634
3635 dds.dds_blocks = zdde->zdde_ref_blocks / refcnt;
3636 dds.dds_lsize = zdde->zdde_ref_lsize / refcnt;
3637 dds.dds_psize = zdde->zdde_ref_psize / refcnt;
3638 dds.dds_dsize = zdde->zdde_ref_dsize / refcnt;
3639
3640 dds.dds_ref_blocks = zdde->zdde_ref_blocks;
3641 dds.dds_ref_lsize = zdde->zdde_ref_lsize;
3642 dds.dds_ref_psize = zdde->zdde_ref_psize;
3643 dds.dds_ref_dsize = zdde->zdde_ref_dsize;
3644
ea04106b
AX
3645 ddt_stat_add(&ddh_total.ddh_stat[highbit64(refcnt) - 1],
3646 &dds, 0);
428870ff
BB
3647
3648 umem_free(zdde, sizeof (*zdde));
3649 }
3650
3651 avl_destroy(&t);
3652
3653 ddt_histogram_stat(&dds_total, &ddh_total);
3654
3655 (void) printf("Simulated DDT histogram:\n");
3656
3657 zpool_dump_ddt(&dds_total, &ddh_total);
3658
3659 dump_dedup_ratio(&dds_total);
3660}
3661
34dc7c2f
BB
3662static void
3663dump_zpool(spa_t *spa)
3664{
3665 dsl_pool_t *dp = spa_get_dsl(spa);
3666 int rc = 0;
3667
428870ff
BB
3668 if (dump_opt['S']) {
3669 dump_simulated_ddt(spa);
3670 return;
3671 }
3672
3673 if (!dump_opt['e'] && dump_opt['C'] > 1) {
3674 (void) printf("\nCached configuration:\n");
3675 dump_nvlist(spa->spa_config, 8);
3676 }
3677
3678 if (dump_opt['C'])
3679 dump_config(spa);
3680
34dc7c2f 3681 if (dump_opt['u'])
428870ff 3682 dump_uberblock(&spa->spa_uberblock, "\nUberblock:\n", "\n");
34dc7c2f 3683
428870ff
BB
3684 if (dump_opt['D'])
3685 dump_all_ddts(spa);
3686
3687 if (dump_opt['d'] > 2 || dump_opt['m'])
3688 dump_metaslabs(spa);
ea04106b
AX
3689 if (dump_opt['M'])
3690 dump_metaslab_groups(spa);
428870ff
BB
3691
3692 if (dump_opt['d'] || dump_opt['i']) {
cae5b340 3693 spa_feature_t f;
e10b0808 3694
34dc7c2f
BB
3695 dump_dir(dp->dp_meta_objset);
3696 if (dump_opt['d'] >= 3) {
e10b0808 3697 dump_full_bpobj(&spa->spa_deferred_bpobj,
a08ee875 3698 "Deferred frees", 0);
428870ff 3699 if (spa_version(spa) >= SPA_VERSION_DEADLISTS) {
e10b0808
AX
3700 dump_full_bpobj(
3701 &spa->spa_dsl_pool->dp_free_bpobj,
a08ee875 3702 "Pool snapshot frees", 0);
9ae529ec
CS
3703 }
3704
3705 if (spa_feature_is_active(spa,
ea04106b 3706 SPA_FEATURE_ASYNC_DESTROY)) {
9ae529ec
CS
3707 dump_bptree(spa->spa_meta_objset,
3708 spa->spa_dsl_pool->dp_bptree_obj,
3709 "Pool dataset frees");
428870ff 3710 }
34dc7c2f 3711 dump_dtl(spa->spa_root_vdev, 0);
34dc7c2f 3712 }
428870ff
BB
3713 (void) dmu_objset_find(spa_name(spa), dump_one_dir,
3714 NULL, DS_FIND_SNAPSHOTS | DS_FIND_CHILDREN);
e10b0808 3715
cae5b340
AX
3716 for (f = 0; f < SPA_FEATURES; f++) {
3717 uint64_t refcount;
3718
3719 if (!(spa_feature_table[f].fi_flags &
3720 ZFEATURE_FLAG_PER_DATASET) ||
3721 !spa_feature_is_enabled(spa, f)) {
3722 ASSERT0(dataset_feature_count[f]);
3723 continue;
3724 }
3725 if (feature_get_refcount(spa, &spa_feature_table[f],
3726 &refcount) == ENOTSUP)
3727 continue;
3728 if (dataset_feature_count[f] != refcount) {
3729 (void) printf("%s feature refcount mismatch: "
3730 "%lld datasets != %lld refcount\n",
3731 spa_feature_table[f].fi_uname,
3732 (longlong_t)dataset_feature_count[f],
e10b0808
AX
3733 (longlong_t)refcount);
3734 rc = 2;
3735 } else {
cae5b340
AX
3736 (void) printf("Verified %s feature refcount "
3737 "of %llu is correct\n",
3738 spa_feature_table[f].fi_uname,
e10b0808
AX
3739 (longlong_t)refcount);
3740 }
3741 }
34dc7c2f 3742 }
e10b0808 3743 if (rc == 0 && (dump_opt['b'] || dump_opt['c']))
34dc7c2f
BB
3744 rc = dump_block_stats(spa);
3745
ea04106b
AX
3746 if (rc == 0)
3747 rc = verify_spacemap_refcounts(spa);
3748
34dc7c2f
BB
3749 if (dump_opt['s'])
3750 show_pool_stats(spa);
3751
428870ff
BB
3752 if (dump_opt['h'])
3753 dump_history(spa);
3754
cae5b340
AX
3755 if (rc != 0) {
3756 dump_debug_buffer();
34dc7c2f 3757 exit(rc);
cae5b340 3758 }
34dc7c2f
BB
3759}
3760
3761#define ZDB_FLAG_CHECKSUM 0x0001
3762#define ZDB_FLAG_DECOMPRESS 0x0002
3763#define ZDB_FLAG_BSWAP 0x0004
3764#define ZDB_FLAG_GBH 0x0008
3765#define ZDB_FLAG_INDIRECT 0x0010
3766#define ZDB_FLAG_PHYS 0x0020
3767#define ZDB_FLAG_RAW 0x0040
3768#define ZDB_FLAG_PRINT_BLKPTR 0x0080
3769
3770int flagbits[256];
3771
3772static void
3773zdb_print_blkptr(blkptr_t *bp, int flags)
3774{
428870ff 3775 char blkbuf[BP_SPRINTF_LEN];
34dc7c2f
BB
3776
3777 if (flags & ZDB_FLAG_BSWAP)
3778 byteswap_uint64_array((void *)bp, sizeof (blkptr_t));
428870ff 3779
ea04106b 3780 snprintf_blkptr(blkbuf, sizeof (blkbuf), bp);
428870ff 3781 (void) printf("%s\n", blkbuf);
34dc7c2f
BB
3782}
3783
3784static void
3785zdb_dump_indirect(blkptr_t *bp, int nbps, int flags)
3786{
3787 int i;
3788
3789 for (i = 0; i < nbps; i++)
3790 zdb_print_blkptr(&bp[i], flags);
3791}
3792
3793static void
3794zdb_dump_gbh(void *buf, int flags)
3795{
3796 zdb_dump_indirect((blkptr_t *)buf, SPA_GBH_NBLKPTRS, flags);
3797}
3798
3799static void
3800zdb_dump_block_raw(void *buf, uint64_t size, int flags)
3801{
3802 if (flags & ZDB_FLAG_BSWAP)
3803 byteswap_uint64_array(buf, size);
0e5b68e0 3804 VERIFY(write(fileno(stdout), buf, size) == size);
34dc7c2f
BB
3805}
3806
3807static void
3808zdb_dump_block(char *label, void *buf, uint64_t size, int flags)
3809{
3810 uint64_t *d = (uint64_t *)buf;
3811 int nwords = size / sizeof (uint64_t);
3812 int do_bswap = !!(flags & ZDB_FLAG_BSWAP);
3813 int i, j;
3814 char *hdr, *c;
3815
3816
3817 if (do_bswap)
3818 hdr = " 7 6 5 4 3 2 1 0 f e d c b a 9 8";
3819 else
3820 hdr = " 0 1 2 3 4 5 6 7 8 9 a b c d e f";
3821
3822 (void) printf("\n%s\n%6s %s 0123456789abcdef\n", label, "", hdr);
3823
cae5b340
AX
3824#ifdef _LITTLE_ENDIAN
3825 /* correct the endianness */
3826 do_bswap = !do_bswap;
3827#endif
34dc7c2f
BB
3828 for (i = 0; i < nwords; i += 2) {
3829 (void) printf("%06llx: %016llx %016llx ",
3830 (u_longlong_t)(i * sizeof (uint64_t)),
3831 (u_longlong_t)(do_bswap ? BSWAP_64(d[i]) : d[i]),
3832 (u_longlong_t)(do_bswap ? BSWAP_64(d[i + 1]) : d[i + 1]));
3833
3834 c = (char *)&d[i];
3835 for (j = 0; j < 2 * sizeof (uint64_t); j++)
3836 (void) printf("%c", isprint(c[j]) ? c[j] : '.');
3837 (void) printf("\n");
3838 }
3839}
3840
3841/*
3842 * There are two acceptable formats:
3843 * leaf_name - For example: c1t0d0 or /tmp/ztest.0a
3844 * child[.child]* - For example: 0.1.1
3845 *
3846 * The second form can be used to specify arbitrary vdevs anywhere
cae5b340 3847 * in the hierarchy. For example, in a pool with a mirror of
34dc7c2f
BB
3848 * RAID-Zs, you can specify either RAID-Z vdev with 0.0 or 0.1 .
3849 */
3850static vdev_t *
3851zdb_vdev_lookup(vdev_t *vdev, char *path)
3852{
3853 char *s, *p, *q;
3854 int i;
3855
3856 if (vdev == NULL)
3857 return (NULL);
3858
3859 /* First, assume the x.x.x.x format */
3860 i = (int)strtoul(path, &s, 10);
3861 if (s == path || (s && *s != '.' && *s != '\0'))
3862 goto name;
3863 if (i < 0 || i >= vdev->vdev_children)
3864 return (NULL);
3865
3866 vdev = vdev->vdev_child[i];
cae5b340 3867 if (s && *s == '\0')
34dc7c2f
BB
3868 return (vdev);
3869 return (zdb_vdev_lookup(vdev, s+1));
3870
3871name:
3872 for (i = 0; i < vdev->vdev_children; i++) {
3873 vdev_t *vc = vdev->vdev_child[i];
3874
3875 if (vc->vdev_path == NULL) {
3876 vc = zdb_vdev_lookup(vc, path);
3877 if (vc == NULL)
3878 continue;
3879 else
3880 return (vc);
3881 }
3882
3883 p = strrchr(vc->vdev_path, '/');
3884 p = p ? p + 1 : vc->vdev_path;
3885 q = &vc->vdev_path[strlen(vc->vdev_path) - 2];
3886
3887 if (strcmp(vc->vdev_path, path) == 0)
3888 return (vc);
3889 if (strcmp(p, path) == 0)
3890 return (vc);
3891 if (strcmp(q, "s0") == 0 && strncmp(p, path, q - p) == 0)
3892 return (vc);
3893 }
3894
3895 return (NULL);
3896}
3897
3898/*
3899 * Read a block from a pool and print it out. The syntax of the
3900 * block descriptor is:
3901 *
3902 * pool:vdev_specifier:offset:size[:flags]
3903 *
3904 * pool - The name of the pool you wish to read from
3905 * vdev_specifier - Which vdev (see comment for zdb_vdev_lookup)
3906 * offset - offset, in hex, in bytes
3907 * size - Amount of data to read, in hex, in bytes
3908 * flags - A string of characters specifying options
3909 * b: Decode a blkptr at given offset within block
3910 * *c: Calculate and display checksums
428870ff 3911 * d: Decompress data before dumping
34dc7c2f 3912 * e: Byteswap data before dumping
428870ff
BB
3913 * g: Display data as a gang block header
3914 * i: Display as an indirect block
34dc7c2f
BB
3915 * p: Do I/O to physical offset
3916 * r: Dump raw data to stdout
3917 *
3918 * * = not yet implemented
3919 */
3920static void
428870ff 3921zdb_read_block(char *thing, spa_t *spa)
34dc7c2f 3922{
428870ff
BB
3923 blkptr_t blk, *bp = &blk;
3924 dva_t *dva = bp->blk_dva;
34dc7c2f 3925 int flags = 0;
428870ff 3926 uint64_t offset = 0, size = 0, psize = 0, lsize = 0, blkptr_offset = 0;
34dc7c2f
BB
3927 zio_t *zio;
3928 vdev_t *vd;
cae5b340
AX
3929 abd_t *pabd;
3930 void *lbuf, *buf;
428870ff
BB
3931 char *s, *p, *dup, *vdev, *flagstr;
3932 int i, error;
cae5b340 3933 boolean_t borrowed = B_FALSE;
34dc7c2f
BB
3934
3935 dup = strdup(thing);
3936 s = strtok(dup, ":");
34dc7c2f
BB
3937 vdev = s ? s : "";
3938 s = strtok(NULL, ":");
3939 offset = strtoull(s ? s : "", NULL, 16);
3940 s = strtok(NULL, ":");
3941 size = strtoull(s ? s : "", NULL, 16);
3942 s = strtok(NULL, ":");
3943 flagstr = s ? s : "";
3944
3945 s = NULL;
3946 if (size == 0)
3947 s = "size must not be zero";
3948 if (!IS_P2ALIGNED(size, DEV_BSIZE))
3949 s = "size must be a multiple of sector size";
3950 if (!IS_P2ALIGNED(offset, DEV_BSIZE))
3951 s = "offset must be a multiple of sector size";
3952 if (s) {
3953 (void) printf("Invalid block specifier: %s - %s\n", thing, s);
3954 free(dup);
3955 return;
3956 }
3957
3958 for (s = strtok(flagstr, ":"); s; s = strtok(NULL, ":")) {
3959 for (i = 0; flagstr[i]; i++) {
3960 int bit = flagbits[(uchar_t)flagstr[i]];
3961
3962 if (bit == 0) {
3963 (void) printf("***Invalid flag: %c\n",
3964 flagstr[i]);
3965 continue;
3966 }
3967 flags |= bit;
3968
3969 /* If it's not something with an argument, keep going */
428870ff 3970 if ((bit & (ZDB_FLAG_CHECKSUM |
34dc7c2f
BB
3971 ZDB_FLAG_PRINT_BLKPTR)) == 0)
3972 continue;
3973
3974 p = &flagstr[i + 1];
4e820b5a 3975 if (bit == ZDB_FLAG_PRINT_BLKPTR) {
34dc7c2f 3976 blkptr_offset = strtoull(p, &p, 16);
4e820b5a
AX
3977 i = p - &flagstr[i + 1];
3978 }
34dc7c2f
BB
3979 if (*p != ':' && *p != '\0') {
3980 (void) printf("***Invalid flag arg: '%s'\n", s);
3981 free(dup);
3982 return;
3983 }
3984 }
3985 }
3986
34dc7c2f
BB
3987 vd = zdb_vdev_lookup(spa->spa_root_vdev, vdev);
3988 if (vd == NULL) {
3989 (void) printf("***Invalid vdev: %s\n", vdev);
3990 free(dup);
3991 return;
3992 } else {
3993 if (vd->vdev_path)
428870ff
BB
3994 (void) fprintf(stderr, "Found vdev: %s\n",
3995 vd->vdev_path);
34dc7c2f 3996 else
428870ff 3997 (void) fprintf(stderr, "Found vdev type: %s\n",
34dc7c2f
BB
3998 vd->vdev_ops->vdev_op_type);
3999 }
4000
428870ff
BB
4001 psize = size;
4002 lsize = size;
4003
cae5b340 4004 pabd = abd_alloc_for_io(SPA_MAXBLOCKSIZE, B_FALSE);
428870ff
BB
4005 lbuf = umem_alloc(SPA_MAXBLOCKSIZE, UMEM_NOFAIL);
4006
4007 BP_ZERO(bp);
34dc7c2f 4008
428870ff
BB
4009 DVA_SET_VDEV(&dva[0], vd->vdev_id);
4010 DVA_SET_OFFSET(&dva[0], offset);
4011 DVA_SET_GANG(&dva[0], !!(flags & ZDB_FLAG_GBH));
4012 DVA_SET_ASIZE(&dva[0], vdev_psize_to_asize(vd, psize));
4013
4014 BP_SET_BIRTH(bp, TXG_INITIAL, TXG_INITIAL);
4015
4016 BP_SET_LSIZE(bp, lsize);
4017 BP_SET_PSIZE(bp, psize);
4018 BP_SET_COMPRESS(bp, ZIO_COMPRESS_OFF);
4019 BP_SET_CHECKSUM(bp, ZIO_CHECKSUM_OFF);
4020 BP_SET_TYPE(bp, DMU_OT_NONE);
4021 BP_SET_LEVEL(bp, 0);
4022 BP_SET_DEDUP(bp, 0);
4023 BP_SET_BYTEORDER(bp, ZFS_HOST_BYTEORDER);
34dc7c2f 4024
b128c09f 4025 spa_config_enter(spa, SCL_STATE, FTAG, RW_READER);
34dc7c2f 4026 zio = zio_root(spa, NULL, NULL, 0);
428870ff
BB
4027
4028 if (vd == vd->vdev_top) {
4029 /*
4030 * Treat this as a normal block read.
4031 */
cae5b340 4032 zio_nowait(zio_read(zio, spa, bp, pabd, psize, NULL, NULL,
428870ff
BB
4033 ZIO_PRIORITY_SYNC_READ,
4034 ZIO_FLAG_CANFAIL | ZIO_FLAG_RAW, NULL));
4035 } else {
4036 /*
4037 * Treat this as a vdev child I/O.
4038 */
cae5b340
AX
4039 zio_nowait(zio_vdev_child_io(zio, bp, vd, offset, pabd,
4040 psize, ZIO_TYPE_READ, ZIO_PRIORITY_SYNC_READ,
428870ff
BB
4041 ZIO_FLAG_DONT_CACHE | ZIO_FLAG_DONT_QUEUE |
4042 ZIO_FLAG_DONT_PROPAGATE | ZIO_FLAG_DONT_RETRY |
4043 ZIO_FLAG_CANFAIL | ZIO_FLAG_RAW, NULL, NULL));
4044 }
4045
34dc7c2f 4046 error = zio_wait(zio);
b128c09f 4047 spa_config_exit(spa, SCL_STATE, FTAG);
34dc7c2f
BB
4048
4049 if (error) {
4050 (void) printf("Read of %s failed, error: %d\n", thing, error);
4051 goto out;
4052 }
4053
428870ff
BB
4054 if (flags & ZDB_FLAG_DECOMPRESS) {
4055 /*
4056 * We don't know how the data was compressed, so just try
4057 * every decompress function at every inflated blocksize.
4058 */
4059 enum zio_compress c;
428870ff
BB
4060 void *lbuf2 = umem_alloc(SPA_MAXBLOCKSIZE, UMEM_NOFAIL);
4061
cae5b340
AX
4062 /*
4063 * XXX - On the one hand, with SPA_MAXBLOCKSIZE at 16MB,
4064 * this could take a while and we should let the user know
4065 * we are not stuck. On the other hand, printing progress
4066 * info gets old after a while. What to do?
4067 */
4068 for (lsize = psize + SPA_MINBLOCKSIZE;
4069 lsize <= SPA_MAXBLOCKSIZE; lsize += SPA_MINBLOCKSIZE) {
428870ff 4070 for (c = 0; c < ZIO_COMPRESS_FUNCTIONS; c++) {
42f7b73b
AX
4071 /*
4072 * ZLE can easily decompress non zle stream.
4073 * So have an option to disable it.
4074 */
4075 if (c == ZIO_COMPRESS_ZLE &&
4076 getenv("ZDB_NO_ZLE"))
4077 continue;
4078
cae5b340
AX
4079 (void) fprintf(stderr,
4080 "Trying %05llx -> %05llx (%s)\n",
4081 (u_longlong_t)psize, (u_longlong_t)lsize,
4082 zio_compress_table[c].ci_name);
42f7b73b
AX
4083
4084 /*
4085 * We randomize lbuf2, and decompress to both
4086 * lbuf and lbuf2. This way, we will know if
4087 * decompression fill exactly to lsize.
4088 */
4089 VERIFY0(random_get_pseudo_bytes(lbuf2, lsize));
4090
cae5b340
AX
4091 if (zio_decompress_data(c, pabd,
4092 lbuf, psize, lsize) == 0 &&
42f7b73b 4093 zio_decompress_data(c, pabd,
cae5b340 4094 lbuf2, psize, lsize) == 0 &&
428870ff
BB
4095 bcmp(lbuf, lbuf2, lsize) == 0)
4096 break;
4097 }
4098 if (c != ZIO_COMPRESS_FUNCTIONS)
4099 break;
428870ff 4100 }
428870ff
BB
4101 umem_free(lbuf2, SPA_MAXBLOCKSIZE);
4102
42f7b73b 4103 if (lsize > SPA_MAXBLOCKSIZE) {
428870ff
BB
4104 (void) printf("Decompress of %s failed\n", thing);
4105 goto out;
4106 }
4107 buf = lbuf;
4108 size = lsize;
4109 } else {
428870ff 4110 size = psize;
cae5b340
AX
4111 buf = abd_borrow_buf_copy(pabd, size);
4112 borrowed = B_TRUE;
428870ff
BB
4113 }
4114
34dc7c2f
BB
4115 if (flags & ZDB_FLAG_PRINT_BLKPTR)
4116 zdb_print_blkptr((blkptr_t *)(void *)
4117 ((uintptr_t)buf + (uintptr_t)blkptr_offset), flags);
4118 else if (flags & ZDB_FLAG_RAW)
4119 zdb_dump_block_raw(buf, size, flags);
4120 else if (flags & ZDB_FLAG_INDIRECT)
4121 zdb_dump_indirect((blkptr_t *)buf, size / sizeof (blkptr_t),
4122 flags);
4123 else if (flags & ZDB_FLAG_GBH)
4124 zdb_dump_gbh(buf, flags);
4125 else
4126 zdb_dump_block(thing, buf, size, flags);
4127
cae5b340
AX
4128 if (borrowed)
4129 abd_return_buf_copy(pabd, buf, size);
4130
34dc7c2f 4131out:
cae5b340 4132 abd_free(pabd);
428870ff 4133 umem_free(lbuf, SPA_MAXBLOCKSIZE);
34dc7c2f
BB
4134 free(dup);
4135}
4136
cae5b340
AX
4137static void
4138zdb_embedded_block(char *thing)
34dc7c2f 4139{
cae5b340
AX
4140 blkptr_t bp;
4141 unsigned long long *words = (void *)&bp;
42f7b73b 4142 char *buf;
cae5b340
AX
4143 int err;
4144
42f7b73b 4145 buf = umem_alloc(SPA_MAXBLOCKSIZE, UMEM_NOFAIL);
cae5b340 4146
42f7b73b 4147 bzero(&bp, sizeof (bp));
cae5b340
AX
4148 err = sscanf(thing, "%llx:%llx:%llx:%llx:%llx:%llx:%llx:%llx:"
4149 "%llx:%llx:%llx:%llx:%llx:%llx:%llx:%llx",
4150 words + 0, words + 1, words + 2, words + 3,
4151 words + 4, words + 5, words + 6, words + 7,
4152 words + 8, words + 9, words + 10, words + 11,
4153 words + 12, words + 13, words + 14, words + 15);
4154 if (err != 16) {
4155 (void) printf("invalid input format\n");
4156 exit(1);
34dc7c2f 4157 }
cae5b340
AX
4158 ASSERT3U(BPE_GET_LSIZE(&bp), <=, SPA_MAXBLOCKSIZE);
4159 err = decode_embedded_bp(&bp, buf, BPE_GET_LSIZE(&bp));
4160 if (err != 0) {
4161 (void) printf("decode failed: %u\n", err);
4162 exit(1);
428870ff 4163 }
cae5b340 4164 zdb_dump_block_raw(buf, BPE_GET_LSIZE(&bp), 0);
42f7b73b 4165 umem_free(buf, SPA_MAXBLOCKSIZE);
34dc7c2f
BB
4166}
4167
4168int
4169main(int argc, char **argv)
4170{
4171 int i, c;
4172 struct rlimit rl = { 1024, 1024 };
428870ff 4173 spa_t *spa = NULL;
34dc7c2f 4174 objset_t *os = NULL;
34dc7c2f
BB
4175 int dump_all = 1;
4176 int verbose = 0;
428870ff
BB
4177 int error = 0;
4178 char **searchdirs = NULL;
4179 int nsearch = 0;
42f7b73b 4180 char *target, *target_pool;
428870ff
BB
4181 nvlist_t *policy = NULL;
4182 uint64_t max_txg = UINT64_MAX;
ea04106b 4183 int flags = ZFS_IMPORT_MISSING_LOG;
428870ff 4184 int rewind = ZPOOL_NEVER_REWIND;
c06d4368 4185 char *spa_config_path_env;
e10b0808 4186 boolean_t target_is_spa = B_TRUE;
34dc7c2f
BB
4187
4188 (void) setrlimit(RLIMIT_NOFILE, &rl);
4189 (void) enable_extended_FILE_stdio(-1, -1);
4190
4191 dprintf_setup(&argc, argv);
4192
c06d4368
AX
4193 /*
4194 * If there is an environment variable SPA_CONFIG_PATH it overrides
4195 * default spa_config_path setting. If -U flag is specified it will
4196 * override this environment variable settings once again.
4197 */
4198 spa_config_path_env = getenv("SPA_CONFIG_PATH");
4199 if (spa_config_path_env != NULL)
4200 spa_config_path = spa_config_path_env;
4201
cae5b340
AX
4202 while ((c = getopt(argc, argv,
4203 "AbcCdDeEFGhiI:lLmMo:Op:PqRsSt:uU:vVx:X")) != -1) {
34dc7c2f 4204 switch (c) {
34dc7c2f
BB
4205 case 'b':
4206 case 'c':
cae5b340 4207 case 'C':
428870ff 4208 case 'd':
cae5b340
AX
4209 case 'D':
4210 case 'E':
4211 case 'G':
428870ff
BB
4212 case 'h':
4213 case 'i':
4214 case 'l':
9babb374 4215 case 'm':
ea04106b 4216 case 'M':
cae5b340 4217 case 'O':
34dc7c2f 4218 case 'R':
cae5b340 4219 case 's':
428870ff 4220 case 'S':
cae5b340 4221 case 'u':
34dc7c2f
BB
4222 dump_opt[c]++;
4223 dump_all = 0;
4224 break;
428870ff 4225 case 'A':
cae5b340 4226 case 'e':
428870ff 4227 case 'F':
fb5f0bc8 4228 case 'L':
428870ff 4229 case 'P':
cae5b340
AX
4230 case 'q':
4231 case 'X':
fb5f0bc8
BB
4232 dump_opt[c]++;
4233 break;
cae5b340 4234 /* NB: Sort single match options below. */
ea04106b 4235 case 'I':
c06d4368
AX
4236 max_inflight = strtoull(optarg, NULL, 0);
4237 if (max_inflight == 0) {
4238 (void) fprintf(stderr, "maximum number "
4239 "of inflight I/Os must be greater "
4240 "than 0\n");
4241 usage();
4242 }
4243 break;
cae5b340
AX
4244 case 'o':
4245 error = set_global_var(optarg);
4246 if (error != 0)
4247 usage();
4248 break;
34dc7c2f 4249 case 'p':
428870ff
BB
4250 if (searchdirs == NULL) {
4251 searchdirs = umem_alloc(sizeof (char *),
4252 UMEM_NOFAIL);
4253 } else {
4254 char **tmp = umem_alloc((nsearch + 1) *
4255 sizeof (char *), UMEM_NOFAIL);
4256 bcopy(searchdirs, tmp, nsearch *
4257 sizeof (char *));
4258 umem_free(searchdirs,
4259 nsearch * sizeof (char *));
4260 searchdirs = tmp;
4261 }
4262 searchdirs[nsearch++] = optarg;
34dc7c2f 4263 break;
fb5f0bc8 4264 case 't':
428870ff
BB
4265 max_txg = strtoull(optarg, NULL, 0);
4266 if (max_txg < TXG_INITIAL) {
fb5f0bc8
BB
4267 (void) fprintf(stderr, "incorrect txg "
4268 "specified: %s\n", optarg);
4269 usage();
4270 }
4271 break;
428870ff
BB
4272 case 'U':
4273 spa_config_path = optarg;
cae5b340
AX
4274 if (spa_config_path[0] != '/') {
4275 (void) fprintf(stderr,
4276 "cachefile must be an absolute path "
4277 "(i.e. start with a slash)\n");
4278 usage();
4279 }
428870ff 4280 break;
ea04106b
AX
4281 case 'v':
4282 verbose++;
4283 break;
cae5b340
AX
4284 case 'V':
4285 flags = ZFS_IMPORT_VERBATIM;
4286 break;
4287 case 'x':
4288 vn_dumpdir = optarg;
4289 break;
34dc7c2f
BB
4290 default:
4291 usage();
4292 break;
4293 }
4294 }
4295
428870ff 4296 if (!dump_opt['e'] && searchdirs != NULL) {
b128c09f
BB
4297 (void) fprintf(stderr, "-p option requires use of -e\n");
4298 usage();
4299 }
34dc7c2f 4300
ea04106b
AX
4301#if defined(_LP64)
4302 /*
4303 * ZDB does not typically re-read blocks; therefore limit the ARC
4304 * to 256 MB, which can be used entirely for metadata.
4305 */
4306 zfs_arc_max = zfs_arc_meta_limit = 256 * 1024 * 1024;
4307#endif
4308
e10b0808
AX
4309 /*
4310 * "zdb -c" uses checksum-verifying scrub i/os which are async reads.
4311 * "zdb -b" uses traversal prefetch which uses async reads.
4312 * For good performance, let several of them be active at once.
4313 */
4314 zfs_vdev_async_read_max_active = 10;
4315
cae5b340
AX
4316 /*
4317 * Disable reference tracking for better performance.
4318 */
4319 reference_tracking_enable = B_FALSE;
4320
34dc7c2f 4321 kernel_init(FREAD);
e10b0808
AX
4322 if ((g_zfs = libzfs_init()) == NULL) {
4323 (void) fprintf(stderr, "%s", libzfs_error_init(errno));
9b020fd9 4324 return (1);
e10b0808 4325 }
34dc7c2f 4326
428870ff
BB
4327 if (dump_all)
4328 verbose = MAX(verbose, 1);
4329
34dc7c2f 4330 for (c = 0; c < 256; c++) {
cae5b340 4331 if (dump_all && strchr("AeEFlLOPRSX", c) == NULL)
34dc7c2f
BB
4332 dump_opt[c] = 1;
4333 if (dump_opt[c])
4334 dump_opt[c] += verbose;
4335 }
4336
428870ff
BB
4337 aok = (dump_opt['A'] == 1) || (dump_opt['A'] > 2);
4338 zfs_recover = (dump_opt['A'] > 1);
4339
34dc7c2f
BB
4340 argc -= optind;
4341 argv += optind;
4342
428870ff
BB
4343 if (argc < 2 && dump_opt['R'])
4344 usage();
cae5b340
AX
4345
4346 if (dump_opt['E']) {
4347 if (argc != 1)
4348 usage();
4349 zdb_embedded_block(argv[0]);
4350 return (0);
4351 }
4352
34dc7c2f 4353 if (argc < 1) {
428870ff 4354 if (!dump_opt['e'] && dump_opt['C']) {
b128c09f 4355 dump_cachefile(spa_config_path);
34dc7c2f
BB
4356 return (0);
4357 }
4358 usage();
4359 }
4360
cae5b340
AX
4361 if (dump_opt['l'])
4362 return (dump_label(argv[0]));
4363
4364 if (dump_opt['O']) {
4365 if (argc != 2)
4366 usage();
4367 dump_opt['v'] = verbose + 3;
4368 return (dump_path(argv[0], argv[1]));
34dc7c2f
BB
4369 }
4370
428870ff
BB
4371 if (dump_opt['X'] || dump_opt['F'])
4372 rewind = ZPOOL_DO_REWIND |
4373 (dump_opt['X'] ? ZPOOL_EXTREME_REWIND : 0);
34dc7c2f 4374
428870ff
BB
4375 if (nvlist_alloc(&policy, NV_UNIQUE_NAME_TYPE, 0) != 0 ||
4376 nvlist_add_uint64(policy, ZPOOL_REWIND_REQUEST_TXG, max_txg) != 0 ||
4377 nvlist_add_uint32(policy, ZPOOL_REWIND_REQUEST, rewind) != 0)
4378 fatal("internal error: %s", strerror(ENOMEM));
34dc7c2f 4379
b128c09f 4380 error = 0;
428870ff 4381 target = argv[0];
34dc7c2f 4382
42f7b73b
AX
4383 if (strpbrk(target, "/@") != NULL) {
4384 size_t targetlen;
4385
4386 target_pool = strdup(target);
4387 *strpbrk(target_pool, "/@") = '\0';
4388
4389 target_is_spa = B_FALSE;
4390 targetlen = strlen(target);
4391 if (targetlen && target[targetlen - 1] == '/')
4392 target[targetlen - 1] = '\0';
4393 } else {
4394 target_pool = target;
4395 }
4396
428870ff 4397 if (dump_opt['e']) {
cae5b340 4398 importargs_t args = { 0 };
428870ff 4399 nvlist_t *cfg = NULL;
34dc7c2f 4400
cae5b340
AX
4401 args.paths = nsearch;
4402 args.path = searchdirs;
4403 args.can_be_active = B_TRUE;
4404
42f7b73b
AX
4405 error = zpool_tryimport(g_zfs, target_pool, &cfg, &args);
4406
cae5b340 4407 if (error == 0) {
42f7b73b 4408
428870ff
BB
4409 if (nvlist_add_nvlist(cfg,
4410 ZPOOL_REWIND_POLICY, policy) != 0) {
4411 fatal("can't open '%s': %s",
4412 target, strerror(ENOMEM));
4413 }
cae5b340
AX
4414
4415 /*
4416 * Disable the activity check to allow examination of
4417 * active pools.
4418 */
4419 if (dump_opt['C'] > 1) {
4420 (void) printf("\nConfiguration for import:\n");
4421 dump_nvlist(cfg, 8);
4422 }
42f7b73b 4423 error = spa_import(target_pool, cfg, NULL,
cae5b340 4424 flags | ZFS_IMPORT_SKIP_MMP);
34dc7c2f 4425 }
b128c09f
BB
4426 }
4427
42f7b73b
AX
4428 if (target_pool != target)
4429 free(target_pool);
e10b0808 4430
b128c09f 4431 if (error == 0) {
e10b0808 4432 if (target_is_spa || dump_opt['R']) {
cae5b340
AX
4433 /*
4434 * Disable the activity check to allow examination of
4435 * active pools.
4436 */
4437 mutex_enter(&spa_namespace_lock);
4438 if ((spa = spa_lookup(target)) != NULL) {
4439 spa->spa_import_flags |= ZFS_IMPORT_SKIP_MMP;
4440 }
4441 mutex_exit(&spa_namespace_lock);
4442
428870ff
BB
4443 error = spa_open_rewind(target, &spa, FTAG, policy,
4444 NULL);
4445 if (error) {
4446 /*
4447 * If we're missing the log device then
4448 * try opening the pool after clearing the
4449 * log state.
4450 */
4451 mutex_enter(&spa_namespace_lock);
4452 if ((spa = spa_lookup(target)) != NULL &&
4453 spa->spa_log_state == SPA_LOG_MISSING) {
4454 spa->spa_log_state = SPA_LOG_CLEAR;
4455 error = 0;
4456 }
4457 mutex_exit(&spa_namespace_lock);
4458
4459 if (!error) {
4460 error = spa_open_rewind(target, &spa,
4461 FTAG, policy, NULL);
4462 }
4463 }
b128c09f 4464 } else {
cae5b340 4465 error = open_objset(target, DMU_OST_ANY, FTAG, &os);
b128c09f 4466 }
34dc7c2f 4467 }
428870ff 4468 nvlist_free(policy);
34dc7c2f
BB
4469
4470 if (error)
428870ff 4471 fatal("can't open '%s': %s", target, strerror(error));
34dc7c2f
BB
4472
4473 argv++;
428870ff
BB
4474 argc--;
4475 if (!dump_opt['R']) {
4476 if (argc > 0) {
4477 zopt_objects = argc;
4478 zopt_object = calloc(zopt_objects, sizeof (uint64_t));
4479 for (i = 0; i < zopt_objects; i++) {
4480 errno = 0;
4481 zopt_object[i] = strtoull(argv[i], NULL, 0);
4482 if (zopt_object[i] == 0 && errno != 0)
4483 fatal("bad number %s: %s",
4484 argv[i], strerror(errno));
4485 }
34dc7c2f 4486 }
e6f7d015
CS
4487 if (os != NULL) {
4488 dump_dir(os);
4489 } else if (zopt_objects > 0 && !dump_opt['m']) {
4490 dump_dir(spa->spa_meta_objset);
4491 } else {
4492 dump_zpool(spa);
4493 }
34dc7c2f 4494 } else {
428870ff
BB
4495 flagbits['b'] = ZDB_FLAG_PRINT_BLKPTR;
4496 flagbits['c'] = ZDB_FLAG_CHECKSUM;
4497 flagbits['d'] = ZDB_FLAG_DECOMPRESS;
4498 flagbits['e'] = ZDB_FLAG_BSWAP;
4499 flagbits['g'] = ZDB_FLAG_GBH;
4500 flagbits['i'] = ZDB_FLAG_INDIRECT;
4501 flagbits['p'] = ZDB_FLAG_PHYS;
4502 flagbits['r'] = ZDB_FLAG_RAW;
4503
4504 for (i = 0; i < argc; i++)
4505 zdb_read_block(argv[i], spa);
34dc7c2f
BB
4506 }
4507
cae5b340
AX
4508 if (os != NULL)
4509 close_objset(os, FTAG);
4510 else
4511 spa_close(spa, FTAG);
428870ff 4512
34dc7c2f 4513 fuid_table_destroy();
cae5b340
AX
4514
4515 dump_debug_buffer();
34dc7c2f
BB
4516
4517 libzfs_fini(g_zfs);
4518 kernel_fini();
4519
4520 return (0);
4521}