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