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